s4-dsdb Fix urgent_replication test not to set an invalid userAccountControl
[Samba/gebeck_regimport.git] / source3 / winbindd / idmap.c
blob102f4ebb7fca7f80eba0c2877962765c206a4bb4
1 /*
2 Unix SMB/CIFS implementation.
3 ID Mapping
4 Copyright (C) Tim Potter 2000
5 Copyright (C) Jim McDonough <jmcd@us.ibm.com> 2003
6 Copyright (C) Simo Sorce 2003-2007
7 Copyright (C) Jeremy Allison 2006
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "includes.h"
24 #include "winbindd.h"
25 #include "idmap.h"
27 #undef DBGC_CLASS
28 #define DBGC_CLASS DBGC_IDMAP
30 static_decl_idmap;
32 static void idmap_init(void)
34 static bool initialized;
36 if (initialized) {
37 return;
40 DEBUG(10, ("idmap_init(): calling static_init_idmap\n"));
42 static_init_idmap;
44 initialized = true;
47 /**
48 * Pointer to the backend methods. Modules register themselves here via
49 * smb_register_idmap.
52 struct idmap_backend {
53 const char *name;
54 struct idmap_methods *methods;
55 struct idmap_backend *prev, *next;
57 static struct idmap_backend *backends = NULL;
59 /**
60 * Default idmap domain configured via "idmap backend".
62 static struct idmap_domain *default_idmap_domain;
64 /**
65 * Passdb idmap domain, not configurable. winbind must always give passdb a
66 * chance to map ids.
68 static struct idmap_domain *passdb_idmap_domain;
70 /**
71 * List of specially configured idmap domains. This list is filled on demand
72 * in the winbind idmap child when the parent winbind figures out via the
73 * special range parameter or via the domain SID that a special "idmap config
74 * domain" configuration is present.
76 static struct idmap_domain **idmap_domains = NULL;
77 static int num_domains = 0;
79 static struct idmap_methods *get_methods(const char *name)
81 struct idmap_backend *b;
83 for (b = backends; b; b = b->next) {
84 if (strequal(b->name, name)) {
85 return b->methods;
89 return NULL;
92 bool idmap_is_offline(void)
94 return ( lp_winbind_offline_logon() &&
95 get_global_winbindd_state_offline() );
98 bool idmap_is_online(void)
100 return !idmap_is_offline();
103 /**********************************************************************
104 Allow a module to register itself as a method.
105 **********************************************************************/
107 NTSTATUS smb_register_idmap(int version, const char *name,
108 struct idmap_methods *methods)
110 struct idmap_backend *entry;
112 if ((version != SMB_IDMAP_INTERFACE_VERSION)) {
113 DEBUG(0, ("Failed to register idmap module.\n"
114 "The module was compiled against "
115 "SMB_IDMAP_INTERFACE_VERSION %d,\n"
116 "current SMB_IDMAP_INTERFACE_VERSION is %d.\n"
117 "Please recompile against the current version "
118 "of samba!\n",
119 version, SMB_IDMAP_INTERFACE_VERSION));
120 return NT_STATUS_OBJECT_TYPE_MISMATCH;
123 if (!name || !name[0] || !methods) {
124 DEBUG(0,("Called with NULL pointer or empty name!\n"));
125 return NT_STATUS_INVALID_PARAMETER;
128 for (entry = backends; entry != NULL; entry = entry->next) {
129 if (strequal(entry->name, name)) {
130 DEBUG(0,("Idmap module %s already registered!\n",
131 name));
132 return NT_STATUS_OBJECT_NAME_COLLISION;
136 entry = talloc(NULL, struct idmap_backend);
137 if ( ! entry) {
138 DEBUG(0,("Out of memory!\n"));
139 TALLOC_FREE(entry);
140 return NT_STATUS_NO_MEMORY;
142 entry->name = talloc_strdup(entry, name);
143 if ( ! entry->name) {
144 DEBUG(0,("Out of memory!\n"));
145 TALLOC_FREE(entry);
146 return NT_STATUS_NO_MEMORY;
148 entry->methods = methods;
150 DLIST_ADD(backends, entry);
151 DEBUG(5, ("Successfully added idmap backend '%s'\n", name));
152 return NT_STATUS_OK;
155 static int close_domain_destructor(struct idmap_domain *dom)
157 NTSTATUS ret;
159 ret = dom->methods->close_fn(dom);
160 if (!NT_STATUS_IS_OK(ret)) {
161 DEBUG(3, ("Failed to close idmap domain [%s]!\n", dom->name));
164 return 0;
167 static bool parse_idmap_module(TALLOC_CTX *mem_ctx, const char *param,
168 char **pmodulename, char **pargs)
170 char *modulename;
171 char *args;
173 if (strncmp(param, "idmap_", 6) == 0) {
174 param += 6;
175 DEBUG(1, ("idmap_init: idmap backend uses deprecated "
176 "'idmap_' prefix. Please replace 'idmap_%s' by "
177 "'%s'\n", param, param));
180 modulename = talloc_strdup(mem_ctx, param);
181 if (modulename == NULL) {
182 return false;
185 args = strchr(modulename, ':');
186 if (args == NULL) {
187 *pmodulename = modulename;
188 *pargs = NULL;
189 return true;
192 *args = '\0';
194 args = talloc_strdup(mem_ctx, args+1);
195 if (args == NULL) {
196 TALLOC_FREE(modulename);
197 return false;
200 *pmodulename = modulename;
201 *pargs = args;
202 return true;
206 * Initialize a domain structure
207 * @param[in] mem_ctx memory context for the result
208 * @param[in] domainname which domain is this for
209 * @param[in] modulename which backend module
210 * @param[in] params parameter to pass to the init function
211 * @param[in] check_range whether range checking should be done
212 * @result The initialized structure
214 static struct idmap_domain *idmap_init_domain(TALLOC_CTX *mem_ctx,
215 const char *domainname,
216 const char *modulename,
217 const char *params,
218 bool check_range)
220 struct idmap_domain *result;
221 NTSTATUS status;
223 result = talloc_zero(mem_ctx, struct idmap_domain);
224 if (result == NULL) {
225 DEBUG(0, ("talloc failed\n"));
226 return NULL;
229 result->name = talloc_strdup(result, domainname);
230 if (result->name == NULL) {
231 DEBUG(0, ("talloc failed\n"));
232 goto fail;
236 * load ranges and read only information from the config
238 if (strequal(result->name, "*")) {
240 * The default domain "*" is configured differently
241 * from named domains.
243 uid_t low_uid = 0;
244 uid_t high_uid = 0;
245 gid_t low_gid = 0;
246 gid_t high_gid = 0;
248 result->low_id = 0;
249 result->high_id = 0;
251 if (!lp_idmap_uid(&low_uid, &high_uid)) {
252 DEBUG(1, ("'idmap uid' not set!\n"));
253 if (check_range) {
254 goto fail;
258 result->low_id = low_uid;
259 result->high_id = high_uid;
261 if (!lp_idmap_gid(&low_gid, &high_gid)) {
262 DEBUG(1, ("'idmap gid' not set!\n"));
263 if (check_range) {
264 goto fail;
268 if ((low_gid != low_uid) || (high_gid != high_uid)) {
269 DEBUG(1, ("Warning: 'idmap uid' and 'idmap gid'"
270 " ranges do not agree -- building "
271 "intersection\n"));
272 result->low_id = MAX(result->low_id, low_gid);
273 result->high_id = MIN(result->high_id, high_gid);
276 result->read_only = lp_idmap_read_only();
277 } else {
278 char *config_option = NULL;
279 const char *range;
281 config_option = talloc_asprintf(result, "idmap config %s",
282 result->name);
283 if (config_option == NULL) {
284 DEBUG(0, ("Out of memory!\n"));
285 goto fail;
288 range = lp_parm_const_string(-1, config_option, "range", NULL);
289 if (range == NULL) {
290 DEBUG(1, ("idmap range not specified for domain %s\n",
291 result ->name));
292 if (check_range) {
293 goto fail;
295 } else if (sscanf(range, "%u - %u", &result->low_id,
296 &result->high_id) != 2)
298 DEBUG(1, ("invalid range '%s' specified for domain "
299 "'%s'\n", range, result->name));
300 if (check_range) {
301 goto fail;
305 result->read_only = lp_parm_bool(-1, config_option, "read only",
306 false);
308 talloc_free(config_option);
311 if (result->low_id > result->high_id) {
312 DEBUG(1, ("Error: invalid idmap range detected: %lu - %lu\n",
313 (unsigned long)result->low_id,
314 (unsigned long)result->high_id));
315 if (check_range) {
316 goto fail;
320 result->methods = get_methods(modulename);
321 if (result->methods == NULL) {
322 DEBUG(3, ("idmap backend %s not found\n", modulename));
324 status = smb_probe_module("idmap", modulename);
325 if (!NT_STATUS_IS_OK(status)) {
326 DEBUG(3, ("Could not probe idmap module %s\n",
327 modulename));
328 goto fail;
331 result->methods = get_methods(modulename);
333 if (result->methods == NULL) {
334 DEBUG(1, ("idmap backend %s not found\n", modulename));
335 goto fail;
338 status = result->methods->init(result, params);
339 if (!NT_STATUS_IS_OK(status)) {
340 DEBUG(1, ("idmap initialization returned %s\n",
341 nt_errstr(status)));
342 goto fail;
345 talloc_set_destructor(result, close_domain_destructor);
347 return result;
349 fail:
350 TALLOC_FREE(result);
351 return NULL;
355 * Initialize the default domain structure
356 * @param[in] mem_ctx memory context for the result
357 * @result The default domain structure
359 * This routine takes the module name from the "idmap backend" parameter,
360 * passing a possible parameter like ldap:ldap://ldap-url/ to the module.
363 static struct idmap_domain *idmap_init_default_domain(TALLOC_CTX *mem_ctx)
365 struct idmap_domain *result;
366 char *modulename;
367 char *params;
369 idmap_init();
371 if (!parse_idmap_module(talloc_tos(), lp_idmap_backend(), &modulename,
372 &params)) {
373 DEBUG(1, ("parse_idmap_module failed\n"));
374 return NULL;
377 DEBUG(3, ("idmap_init: using '%s' as remote backend\n", modulename));
379 result = idmap_init_domain(mem_ctx, "*", modulename, params, true);
380 if (result == NULL) {
381 goto fail;
384 TALLOC_FREE(modulename);
385 TALLOC_FREE(params);
386 return result;
388 fail:
389 TALLOC_FREE(modulename);
390 TALLOC_FREE(params);
391 TALLOC_FREE(result);
392 return NULL;
396 * Initialize a named domain structure
397 * @param[in] mem_ctx memory context for the result
398 * @param[in] domname the domain name
399 * @result The default domain structure
401 * This routine looks at the "idmap config <domname>" parameters to figure out
402 * the configuration.
405 static struct idmap_domain *idmap_init_named_domain(TALLOC_CTX *mem_ctx,
406 const char *domname)
408 struct idmap_domain *result = NULL;
409 char *config_option;
410 const char *backend;
412 config_option = talloc_asprintf(talloc_tos(), "idmap config %s",
413 domname);
414 if (config_option == NULL) {
415 DEBUG(0, ("talloc failed\n"));
416 goto fail;
419 backend = lp_parm_const_string(-1, config_option, "backend", NULL);
420 if (backend == NULL) {
421 DEBUG(1, ("no backend defined for %s\n", config_option));
422 goto fail;
425 result = idmap_init_domain(mem_ctx, domname, backend, NULL, true);
426 if (result == NULL) {
427 goto fail;
430 TALLOC_FREE(config_option);
431 return result;
433 fail:
434 TALLOC_FREE(config_option);
435 TALLOC_FREE(result);
436 return NULL;
440 * Initialize the passdb domain structure
441 * @param[in] mem_ctx memory context for the result
442 * @result The default domain structure
444 * No config, passdb has its own configuration.
447 static struct idmap_domain *idmap_init_passdb_domain(TALLOC_CTX *mem_ctx)
449 idmap_init();
451 if (passdb_idmap_domain != NULL) {
452 return passdb_idmap_domain;
455 passdb_idmap_domain = idmap_init_domain(NULL, get_global_sam_name(),
456 "passdb", NULL, false);
457 if (passdb_idmap_domain == NULL) {
458 DEBUG(1, ("Could not init passdb idmap domain\n"));
461 return passdb_idmap_domain;
465 * Find a domain struct according to a domain name
466 * @param[in] domname Domain name to get the config for
467 * @result The default domain structure that fits
469 * This is the central routine in the winbindd-idmap child to pick the correct
470 * domain for looking up IDs. If domname is NULL or empty, we use the default
471 * domain. If it contains something, we try to use idmap_init_named_domain()
472 * to fetch the correct backend.
474 * The choice about "domname" is being made by the winbind parent, look at the
475 * "have_idmap_config" of "struct winbindd_domain" which is set in
476 * add_trusted_domain.
479 static struct idmap_domain *idmap_find_domain(const char *domname)
481 struct idmap_domain *result;
482 int i;
484 DEBUG(10, ("idmap_find_domain called for domain '%s'\n",
485 domname?domname:"NULL"));
488 * Always init the default domain, we can't go without one
490 if (default_idmap_domain == NULL) {
491 default_idmap_domain = idmap_init_default_domain(NULL);
493 if (default_idmap_domain == NULL) {
494 return NULL;
497 if ((domname == NULL) || (domname[0] == '\0')) {
498 return default_idmap_domain;
501 for (i=0; i<num_domains; i++) {
502 if (strequal(idmap_domains[i]->name, domname)) {
503 return idmap_domains[i];
507 if (idmap_domains == NULL) {
509 * talloc context for all idmap domains
511 idmap_domains = TALLOC_ARRAY(NULL, struct idmap_domain *, 1);
514 if (idmap_domains == NULL) {
515 DEBUG(0, ("talloc failed\n"));
516 return NULL;
519 result = idmap_init_named_domain(idmap_domains, domname);
520 if (result == NULL) {
522 * Could not init that domain -- try the default one
524 return default_idmap_domain;
527 ADD_TO_ARRAY(idmap_domains, struct idmap_domain *, result,
528 &idmap_domains, &num_domains);
529 return result;
532 void idmap_close(void)
534 TALLOC_FREE(default_idmap_domain);
535 TALLOC_FREE(passdb_idmap_domain);
536 TALLOC_FREE(idmap_domains);
537 num_domains = 0;
540 /**************************************************************************
541 idmap allocator interface functions
542 **************************************************************************/
544 static NTSTATUS idmap_allocate_unixid(struct unixid *id)
546 struct idmap_domain *dom;
547 NTSTATUS ret;
549 dom = idmap_find_domain(NULL);
551 if (dom == NULL) {
552 return NT_STATUS_UNSUCCESSFUL;
555 if (dom->methods->allocate_id == NULL) {
556 return NT_STATUS_NOT_IMPLEMENTED;
559 ret = dom->methods->allocate_id(dom, id);
561 return ret;
565 NTSTATUS idmap_allocate_uid(struct unixid *id)
567 id->type = ID_TYPE_UID;
568 return idmap_allocate_unixid(id);
571 NTSTATUS idmap_allocate_gid(struct unixid *id)
573 id->type = ID_TYPE_GID;
574 return idmap_allocate_unixid(id);
577 NTSTATUS idmap_backends_unixid_to_sid(const char *domname, struct id_map *id)
579 struct idmap_domain *dom;
580 struct id_map *maps[2];
582 DEBUG(10, ("idmap_backend_unixid_to_sid: domain = '%s', xid = %d "
583 "(type %d)\n",
584 domname?domname:"NULL", id->xid.id, id->xid.type));
586 maps[0] = id;
587 maps[1] = NULL;
590 * Always give passdb a chance first
593 dom = idmap_init_passdb_domain(NULL);
594 if ((dom != NULL)
595 && NT_STATUS_IS_OK(dom->methods->unixids_to_sids(dom, maps))
596 && id->status == ID_MAPPED) {
597 return NT_STATUS_OK;
600 dom = idmap_find_domain(domname);
601 if (dom == NULL) {
602 return NT_STATUS_NONE_MAPPED;
605 return dom->methods->unixids_to_sids(dom, maps);
608 NTSTATUS idmap_backends_sid_to_unixid(const char *domain, struct id_map *id)
610 struct idmap_domain *dom;
611 struct id_map *maps[2];
613 DEBUG(10, ("idmap_backends_sid_to_unixid: domain = '%s', sid = [%s]\n",
614 domain?domain:"NULL", sid_string_dbg(id->sid)));
616 maps[0] = id;
617 maps[1] = NULL;
619 if (sid_check_is_in_builtin(id->sid)
620 || (sid_check_is_in_our_domain(id->sid)))
622 NTSTATUS status;
624 DEBUG(10, ("asking passdb...\n"));
626 dom = idmap_init_passdb_domain(NULL);
627 if (dom == NULL) {
628 return NT_STATUS_NONE_MAPPED;
630 status = dom->methods->sids_to_unixids(dom, maps);
632 if (NT_STATUS_IS_OK(status) && id->status == ID_MAPPED) {
633 return status;
636 DEBUG(10, ("passdb could not map, asking backends...\n"));
639 dom = idmap_find_domain(domain);
640 if (dom == NULL) {
641 return NT_STATUS_NONE_MAPPED;
644 return dom->methods->sids_to_unixids(dom, maps);