Changeset 988 for vendor/current/source3/winbindd/idmap.c
- Timestamp:
- Nov 24, 2016, 1:14:11 PM (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
vendor/current/source3/winbindd/idmap.c
r740 r988 25 25 #include "winbindd.h" 26 26 #include "idmap.h" 27 #include "passdb/machine_sid.h" 27 #include "lib/util_sid_passdb.h" 28 #include "passdb.h" 28 29 29 30 #undef DBGC_CLASS … … 31 32 32 33 static_decl_idmap; 33 34 static void idmap_init(void)35 {36 static bool initialized;37 38 if (initialized) {39 return;40 }41 42 DEBUG(10, ("idmap_init(): calling static_init_idmap\n"));43 44 static_init_idmap;45 46 initialized = true;47 }48 34 49 35 /** … … 78 64 static struct idmap_domain **idmap_domains = NULL; 79 65 static int num_domains = 0; 66 67 static struct idmap_domain *idmap_init_named_domain(TALLOC_CTX *mem_ctx, 68 const char *domname); 69 static struct idmap_domain *idmap_init_domain(TALLOC_CTX *mem_ctx, 70 const char *domainname, 71 const char *modulename, 72 bool check_range); 73 static bool idmap_found_domain_backend( 74 const char *string, regmatch_t matches[], void *private_data); 75 76 static bool idmap_init(void) 77 { 78 static bool initialized; 79 int ret; 80 81 if (initialized) { 82 return true; 83 } 84 85 DEBUG(10, ("idmap_init(): calling static_init_idmap\n")); 86 87 static_init_idmap; 88 89 initialized = true; 90 91 if (!pdb_is_responsible_for_everything_else()) { 92 default_idmap_domain = idmap_init_named_domain(NULL, "*"); 93 if (default_idmap_domain == NULL) { 94 return false; 95 } 96 } 97 98 passdb_idmap_domain = idmap_init_domain( 99 NULL, get_global_sam_name(), "passdb", false); 100 if (passdb_idmap_domain == NULL) { 101 TALLOC_FREE(default_idmap_domain); 102 return false; 103 } 104 105 idmap_domains = talloc_array(NULL, struct idmap_domain *, 0); 106 if (idmap_domains == NULL) { 107 TALLOC_FREE(passdb_idmap_domain); 108 TALLOC_FREE(default_idmap_domain); 109 return false; 110 } 111 112 ret = lp_wi_scan_global_parametrics( 113 "idmapconfig\\(.*\\):backend", 2, 114 idmap_found_domain_backend, NULL); 115 if (ret != 0) { 116 DBG_WARNING("wi_scan_global_parametrics returned %d\n", ret); 117 return false; 118 } 119 120 return true; 121 } 122 123 bool domain_has_idmap_config(const char *domname) 124 { 125 int i; 126 char *config_option; 127 const char *range = NULL; 128 const char *backend = NULL; 129 bool ok; 130 131 ok = idmap_init(); 132 if (!ok) { 133 return false; 134 } 135 136 for (i=0; i<num_domains; i++) { 137 if (strequal(idmap_domains[i]->name, domname)) { 138 return true; 139 } 140 } 141 142 /* fallback: also check loadparm */ 143 144 config_option = talloc_asprintf(talloc_tos(), "idmap config %s", 145 domname); 146 if (config_option == NULL) { 147 DEBUG(0, ("out of memory\n")); 148 return false; 149 } 150 151 range = lp_parm_const_string(-1, config_option, "range", NULL); 152 backend = lp_parm_const_string(-1, config_option, "backend", NULL); 153 if (range != NULL && backend != NULL) { 154 DEBUG(5, ("idmap configuration specified for domain '%s'\n", 155 domname)); 156 TALLOC_FREE(config_option); 157 return true; 158 } 159 160 TALLOC_FREE(config_option); 161 return false; 162 } 163 164 static bool idmap_found_domain_backend( 165 const char *string, regmatch_t matches[], void *private_data) 166 { 167 if (matches[1].rm_so == -1) { 168 DBG_WARNING("Found match, but no name??\n"); 169 return false; 170 } 171 172 { 173 struct idmap_domain *dom, **tmp; 174 regoff_t len = matches[1].rm_eo - matches[1].rm_so; 175 char domname[len+1]; 176 177 memcpy(domname, string + matches[1].rm_so, len); 178 domname[len] = '\0'; 179 180 DBG_DEBUG("Found idmap domain \"%s\"\n", domname); 181 182 if (strcmp(domname, "*") == 0) { 183 return false; 184 } 185 186 dom = idmap_init_named_domain(idmap_domains, domname); 187 if (dom == NULL) { 188 DBG_NOTICE("Could not init idmap domain %s\n", 189 domname); 190 return false; 191 } 192 193 tmp = talloc_realloc(idmap_domains, idmap_domains, 194 struct idmap_domain *, num_domains + 1); 195 if (tmp == NULL) { 196 DBG_WARNING("talloc_realloc failed\n"); 197 TALLOC_FREE(dom); 198 return false; 199 } 200 idmap_domains = tmp; 201 idmap_domains[num_domains] = dom; 202 num_domains += 1; 203 } 204 205 return false; 206 } 80 207 81 208 static struct idmap_methods *get_methods(const char *name) … … 130 257 for (entry = backends; entry != NULL; entry = entry->next) { 131 258 if (strequal(entry->name, name)) { 132 DEBUG( 0,("Idmap module %s already registered!\n",259 DEBUG(5,("Idmap module %s already registered!\n", 133 260 name)); 134 261 return NT_STATUS_OBJECT_NAME_COLLISION; … … 172 299 char *config_option = NULL; 173 300 const char *range; 301 unsigned low_id = 0; 302 unsigned high_id = 0; 174 303 175 304 result = talloc_zero(mem_ctx, struct idmap_domain); … … 186 315 187 316 /* 317 * Check whether the requested backend module exists and 318 * load the methods. 319 */ 320 321 result->methods = get_methods(modulename); 322 if (result->methods == NULL) { 323 DEBUG(3, ("idmap backend %s not found\n", modulename)); 324 325 status = smb_probe_module("idmap", modulename); 326 if (!NT_STATUS_IS_OK(status)) { 327 DEBUG(3, ("Could not probe idmap module %s\n", 328 modulename)); 329 goto fail; 330 } 331 332 result->methods = get_methods(modulename); 333 } 334 if (result->methods == NULL) { 335 DEBUG(1, ("idmap backend %s not found\n", modulename)); 336 goto fail; 337 } 338 339 /* 188 340 * load ranges and read only information from the config 189 341 */ … … 196 348 } 197 349 350 result->read_only = lp_parm_bool(-1, config_option, "read only", false); 198 351 range = lp_parm_const_string(-1, config_option, "range", NULL); 352 353 talloc_free(config_option); 354 199 355 if (range == NULL) { 200 DEBUG(1, ("idmap range not specified for domain %s\n",201 result->name));202 356 if (check_range) { 357 DEBUG(1, ("idmap range not specified for domain %s\n", 358 result->name)); 203 359 goto fail; 204 360 } 205 } else if (sscanf(range, "%u - %u", &result->low_id, 206 &result->high_id) != 2) 361 } else if (sscanf(range, "%u - %u", &low_id, &high_id) != 2) 207 362 { 208 363 DEBUG(1, ("invalid range '%s' specified for domain " … … 211 366 goto fail; 212 367 } 213 } 214 215 result->read_only = lp_parm_bool(-1, config_option, "read only", false); 216 217 talloc_free(config_option); 218 219 if (result->low_id > result->high_id) { 220 DEBUG(1, ("Error: invalid idmap range detected: %lu - %lu\n", 221 (unsigned long)result->low_id, 222 (unsigned long)result->high_id)); 368 } else if (low_id > high_id) { 369 DEBUG(1, ("Error: invalid idmap range detected: %u - %u\n", 370 low_id, high_id)); 223 371 if (check_range) { 224 372 goto fail; … … 226 374 } 227 375 228 result->methods = get_methods(modulename); 229 if (result->methods == NULL) { 230 DEBUG(3, ("idmap backend %s not found\n", modulename)); 231 232 status = smb_probe_module("idmap", modulename); 233 if (!NT_STATUS_IS_OK(status)) { 234 DEBUG(3, ("Could not probe idmap module %s\n", 235 modulename)); 236 goto fail; 237 } 238 239 result->methods = get_methods(modulename); 240 } 241 if (result->methods == NULL) { 242 DEBUG(1, ("idmap backend %s not found\n", modulename)); 243 goto fail; 244 } 376 result->low_id = low_id; 377 result->high_id = high_id; 245 378 246 379 status = result->methods->init(result); … … 274 407 char *config_option; 275 408 const char *backend; 276 277 idmap_init(); 409 bool ok; 410 411 ok = idmap_init(); 412 if (!ok) { 413 return NULL; 414 } 278 415 279 416 config_option = talloc_asprintf(talloc_tos(), "idmap config %s", … … 286 423 backend = lp_parm_const_string(-1, config_option, "backend", NULL); 287 424 if (backend == NULL) { 288 DEBUG(1, ("no backend defined for %s\n", config_option)); 425 DEBUG(10, ("no idmap backend configured for domain '%s'\n", 426 domname)); 289 427 goto fail; 290 428 } … … 302 440 TALLOC_FREE(result); 303 441 return NULL; 304 }305 306 /**307 * Initialize the default domain structure308 * @param[in] mem_ctx memory context for the result309 * @result The default domain structure310 *311 * This routine takes the module name from the "idmap backend" parameter,312 * passing a possible parameter like ldap:ldap://ldap-url/ to the module.313 */314 315 static struct idmap_domain *idmap_init_default_domain(TALLOC_CTX *mem_ctx)316 {317 return idmap_init_named_domain(mem_ctx, "*");318 }319 320 /**321 * Initialize the passdb domain structure322 * @param[in] mem_ctx memory context for the result323 * @result The default domain structure324 *325 * No config, passdb has its own configuration.326 */327 328 static struct idmap_domain *idmap_init_passdb_domain(TALLOC_CTX *mem_ctx)329 {330 idmap_init();331 332 /*333 * Always init the default domain, we can't go without one334 */335 if (default_idmap_domain == NULL) {336 default_idmap_domain = idmap_init_default_domain(NULL);337 }338 if (default_idmap_domain == NULL) {339 return NULL;340 }341 342 if (passdb_idmap_domain != NULL) {343 return passdb_idmap_domain;344 }345 346 passdb_idmap_domain = idmap_init_domain(NULL, get_global_sam_name(),347 "passdb", false);348 if (passdb_idmap_domain == NULL) {349 DEBUG(1, ("Could not init passdb idmap domain\n"));350 }351 352 return passdb_idmap_domain;353 442 } 354 443 … … 368 457 */ 369 458 370 st ruct idmap_domain *idmap_find_domain(const char *domname)371 { 372 struct idmap_domain *result;459 static struct idmap_domain *idmap_find_domain(const char *domname) 460 { 461 bool ok; 373 462 int i; 374 463 … … 376 465 domname?domname:"NULL")); 377 466 378 idmap_init(); 379 380 /* 381 * Always init the default domain, we can't go without one 382 */ 383 if (default_idmap_domain == NULL) { 384 default_idmap_domain = idmap_init_default_domain(NULL); 385 } 386 if (default_idmap_domain == NULL) { 467 ok = idmap_init(); 468 if (!ok) { 387 469 return NULL; 388 470 } … … 398 480 } 399 481 400 if (idmap_domains == NULL) { 401 /* 402 * talloc context for all idmap domains 403 */ 404 idmap_domains = TALLOC_ARRAY(NULL, struct idmap_domain *, 1); 405 } 406 407 if (idmap_domains == NULL) { 408 DEBUG(0, ("talloc failed\n")); 482 return default_idmap_domain; 483 } 484 485 struct idmap_domain *idmap_find_domain_with_sid(const char *domname, 486 const struct dom_sid *sid) 487 { 488 bool ok; 489 490 ok = idmap_init(); 491 if (!ok) { 409 492 return NULL; 410 493 } 411 494 412 result = idmap_init_named_domain(idmap_domains, domname); 413 if (result == NULL) { 414 /* 415 * Could not init that domain -- try the default one 416 */ 417 return default_idmap_domain; 418 } 419 420 ADD_TO_ARRAY(idmap_domains, struct idmap_domain *, result, 421 &idmap_domains, &num_domains); 422 return result; 495 if (sid_check_is_for_passdb(sid)) { 496 return passdb_idmap_domain; 497 } 498 499 return idmap_find_domain(domname); 423 500 } 424 501 … … 468 545 } 469 546 470 NTSTATUS idmap_backends_unixid_to_sid( const char *domname,struct id_map *id)547 NTSTATUS idmap_backends_unixid_to_sid(struct id_map *id) 471 548 { 472 549 struct idmap_domain *dom; 473 550 struct id_map *maps[2]; 474 475 DEBUG(10, ("idmap_backend_unixid_to_sid: domain = '%s', xid = %d " 476 "(type %d)\n", 477 domname?domname:"NULL", id->xid.id, id->xid.type)); 551 bool ok; 552 int i; 553 554 ok = idmap_init(); 555 if (!ok) { 556 return NT_STATUS_NONE_MAPPED; 557 } 558 559 DEBUG(10, ("idmap_backend_unixid_to_sid: xid = %d (type %d)\n", 560 id->xid.id, id->xid.type)); 478 561 479 562 maps[0] = id; … … 484 567 */ 485 568 486 dom = idmap_init_passdb_domain(NULL);569 dom = passdb_idmap_domain; 487 570 if ((dom != NULL) 488 571 && NT_STATUS_IS_OK(dom->methods->unixids_to_sids(dom, maps)) … … 491 574 } 492 575 493 dom = idmap_find_domain(domname); 576 dom = default_idmap_domain; 577 578 for (i=0; i<num_domains; i++) { 579 if ((id->xid.id >= idmap_domains[i]->low_id) && 580 (id->xid.id <= idmap_domains[i]->high_id)) { 581 dom = idmap_domains[i]; 582 break; 583 } 584 } 585 494 586 if (dom == NULL) { 495 587 return NT_STATUS_NONE_MAPPED; … … 498 590 return dom->methods->unixids_to_sids(dom, maps); 499 591 } 500 501 NTSTATUS idmap_backends_sid_to_unixid(const char *domain, struct id_map *id)502 {503 struct idmap_domain *dom;504 struct id_map *maps[2];505 506 DEBUG(10, ("idmap_backends_sid_to_unixid: domain = '%s', sid = [%s]\n",507 domain?domain:"NULL", sid_string_dbg(id->sid)));508 509 maps[0] = id;510 maps[1] = NULL;511 512 if (sid_check_is_in_builtin(id->sid)513 || (sid_check_is_in_our_domain(id->sid)))514 {515 NTSTATUS status;516 517 DEBUG(10, ("asking passdb...\n"));518 519 dom = idmap_init_passdb_domain(NULL);520 if (dom == NULL) {521 return NT_STATUS_NONE_MAPPED;522 }523 status = dom->methods->sids_to_unixids(dom, maps);524 525 if (NT_STATUS_IS_OK(status) && id->status == ID_MAPPED) {526 return status;527 }528 529 DEBUG(10, ("passdb could not map.\n"));530 531 return NT_STATUS_NONE_MAPPED;532 }533 534 dom = idmap_find_domain(domain);535 if (dom == NULL) {536 return NT_STATUS_NONE_MAPPED;537 }538 539 return dom->methods->sids_to_unixids(dom, maps);540 }
Note:
See TracChangeset
for help on using the changeset viewer.