s3:winbindd: make sure we only call static_init_idmap once
[Samba.git] / source3 / winbindd / idmap.c
blob2414dab224ed1a9ed3436450899ae61fb770e0cd
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"
26 #undef DBGC_CLASS
27 #define DBGC_CLASS DBGC_IDMAP
29 static_decl_idmap;
31 static void idmap_init(void)
33 static bool initialized;
35 if (initialized) {
36 return;
39 DEBUG(10, ("idmap_init(): calling static_init_idmap\n"));
41 static_init_idmap;
43 initialized = true;
46 /**
47 * Pointer to the backend methods. Modules register themselves here via
48 * smb_register_idmap.
51 struct idmap_backend {
52 const char *name;
53 struct idmap_methods *methods;
54 struct idmap_backend *prev, *next;
56 static struct idmap_backend *backends = NULL;
58 /**
59 * Pointer to the alloc backend methods. Modules register themselves here via
60 * smb_register_idmap_alloc.
62 struct idmap_alloc_backend {
63 const char *name;
64 struct idmap_alloc_methods *methods;
65 struct idmap_alloc_backend *prev, *next;
67 static struct idmap_alloc_backend *alloc_backends = NULL;
69 /**
70 * The idmap alloc context that is configured via "idmap alloc
71 * backend". Defaults to "idmap backend" in case the module (tdb, ldap) also
72 * provides alloc methods.
74 struct idmap_alloc_context {
75 struct idmap_alloc_methods *methods;
77 static struct idmap_alloc_context *idmap_alloc_ctx = NULL;
79 /**
80 * Default idmap domain configured via "idmap backend".
82 static struct idmap_domain *default_idmap_domain;
84 /**
85 * Passdb idmap domain, not configurable. winbind must always give passdb a
86 * chance to map ids.
88 static struct idmap_domain *passdb_idmap_domain;
90 /**
91 * List of specially configured idmap domains. This list is filled on demand
92 * in the winbind idmap child when the parent winbind figures out via the
93 * special range parameter or via the domain SID that a special "idmap config
94 * domain" configuration is present.
96 static struct idmap_domain **idmap_domains = NULL;
97 static int num_domains = 0;
99 static struct idmap_methods *get_methods(const char *name)
101 struct idmap_backend *b;
103 for (b = backends; b; b = b->next) {
104 if (strequal(b->name, name)) {
105 return b->methods;
109 return NULL;
112 static struct idmap_alloc_methods *get_alloc_methods(const char *name)
114 struct idmap_alloc_backend *b;
116 for (b = alloc_backends; b; b = b->next) {
117 if (strequal(b->name, name)) {
118 return b->methods;
122 return NULL;
125 bool idmap_is_offline(void)
127 return ( lp_winbind_offline_logon() &&
128 get_global_winbindd_state_offline() );
131 bool idmap_is_online(void)
133 return !idmap_is_offline();
136 /**********************************************************************
137 Allow a module to register itself as a method.
138 **********************************************************************/
140 NTSTATUS smb_register_idmap(int version, const char *name,
141 struct idmap_methods *methods)
143 struct idmap_backend *entry;
145 if ((version != SMB_IDMAP_INTERFACE_VERSION)) {
146 DEBUG(0, ("Failed to register idmap module.\n"
147 "The module was compiled against "
148 "SMB_IDMAP_INTERFACE_VERSION %d,\n"
149 "current SMB_IDMAP_INTERFACE_VERSION is %d.\n"
150 "Please recompile against the current version "
151 "of samba!\n",
152 version, SMB_IDMAP_INTERFACE_VERSION));
153 return NT_STATUS_OBJECT_TYPE_MISMATCH;
156 if (!name || !name[0] || !methods) {
157 DEBUG(0,("Called with NULL pointer or empty name!\n"));
158 return NT_STATUS_INVALID_PARAMETER;
161 for (entry = backends; entry != NULL; entry = entry->next) {
162 if (strequal(entry->name, name)) {
163 DEBUG(0,("Idmap module %s already registered!\n",
164 name));
165 return NT_STATUS_OBJECT_NAME_COLLISION;
169 entry = talloc(NULL, struct idmap_backend);
170 if ( ! entry) {
171 DEBUG(0,("Out of memory!\n"));
172 TALLOC_FREE(entry);
173 return NT_STATUS_NO_MEMORY;
175 entry->name = talloc_strdup(entry, name);
176 if ( ! entry->name) {
177 DEBUG(0,("Out of memory!\n"));
178 TALLOC_FREE(entry);
179 return NT_STATUS_NO_MEMORY;
181 entry->methods = methods;
183 DLIST_ADD(backends, entry);
184 DEBUG(5, ("Successfully added idmap backend '%s'\n", name));
185 return NT_STATUS_OK;
188 /**********************************************************************
189 Allow a module to register itself as an alloc method.
190 **********************************************************************/
192 NTSTATUS smb_register_idmap_alloc(int version, const char *name,
193 struct idmap_alloc_methods *methods)
195 struct idmap_alloc_methods *test;
196 struct idmap_alloc_backend *entry;
198 if ((version != SMB_IDMAP_INTERFACE_VERSION)) {
199 DEBUG(0, ("Failed to register idmap alloc module.\n"
200 "The module was compiled against "
201 "SMB_IDMAP_INTERFACE_VERSION %d,\n"
202 "current SMB_IDMAP_INTERFACE_VERSION is %d.\n"
203 "Please recompile against the current version "
204 "of samba!\n",
205 version, SMB_IDMAP_INTERFACE_VERSION));
206 return NT_STATUS_OBJECT_TYPE_MISMATCH;
209 if (!name || !name[0] || !methods) {
210 DEBUG(0,("Called with NULL pointer or empty name!\n"));
211 return NT_STATUS_INVALID_PARAMETER;
214 test = get_alloc_methods(name);
215 if (test) {
216 DEBUG(0,("idmap_alloc module %s already registered!\n", name));
217 return NT_STATUS_OBJECT_NAME_COLLISION;
220 entry = talloc(NULL, struct idmap_alloc_backend);
221 if ( ! entry) {
222 DEBUG(0,("Out of memory!\n"));
223 return NT_STATUS_NO_MEMORY;
225 entry->name = talloc_strdup(entry, name);
226 if ( ! entry->name) {
227 DEBUG(0,("Out of memory!\n"));
228 return NT_STATUS_NO_MEMORY;
230 entry->methods = methods;
232 DLIST_ADD(alloc_backends, entry);
233 DEBUG(5, ("Successfully added idmap alloc backend '%s'\n", name));
234 return NT_STATUS_OK;
237 static int close_domain_destructor(struct idmap_domain *dom)
239 NTSTATUS ret;
241 ret = dom->methods->close_fn(dom);
242 if (!NT_STATUS_IS_OK(ret)) {
243 DEBUG(3, ("Failed to close idmap domain [%s]!\n", dom->name));
246 return 0;
249 static bool parse_idmap_module(TALLOC_CTX *mem_ctx, const char *param,
250 char **pmodulename, char **pargs)
252 char *modulename;
253 char *args;
255 if (strncmp(param, "idmap_", 6) == 0) {
256 param += 6;
257 DEBUG(1, ("idmap_init: idmap backend uses deprecated "
258 "'idmap_' prefix. Please replace 'idmap_%s' by "
259 "'%s'\n", param, param));
262 modulename = talloc_strdup(mem_ctx, param);
263 if (modulename == NULL) {
264 return false;
267 args = strchr(modulename, ':');
268 if (args == NULL) {
269 *pmodulename = modulename;
270 *pargs = NULL;
271 return true;
274 *args = '\0';
276 args = talloc_strdup(mem_ctx, args+1);
277 if (args == NULL) {
278 TALLOC_FREE(modulename);
279 return false;
282 *pmodulename = modulename;
283 *pargs = args;
284 return true;
288 * Initialize a domain structure
289 * @param[in] mem_ctx memory context for the result
290 * @param[in] domainname which domain is this for
291 * @param[in] modulename which backend module
292 * @param[in] params parameter to pass to the init function
293 * @result The initialized structure
295 static struct idmap_domain *idmap_init_domain(TALLOC_CTX *mem_ctx,
296 const char *domainname,
297 const char *modulename,
298 const char *params)
300 struct idmap_domain *result;
301 NTSTATUS status;
303 result = talloc_zero(mem_ctx, struct idmap_domain);
304 if (result == NULL) {
305 DEBUG(0, ("talloc failed\n"));
306 return NULL;
309 result->name = talloc_strdup(result, domainname);
310 if (result->name == NULL) {
311 DEBUG(0, ("talloc failed\n"));
312 goto fail;
315 result->methods = get_methods(modulename);
316 if (result->methods == NULL) {
317 DEBUG(3, ("idmap backend %s not found\n", modulename));
319 status = smb_probe_module("idmap", modulename);
320 if (!NT_STATUS_IS_OK(status)) {
321 DEBUG(3, ("Could not probe idmap module %s\n",
322 modulename));
323 goto fail;
326 result->methods = get_methods(modulename);
328 if (result->methods == NULL) {
329 DEBUG(1, ("idmap backend %s not found\n", modulename));
330 goto fail;
333 status = result->methods->init(result, params);
334 if (!NT_STATUS_IS_OK(status)) {
335 DEBUG(1, ("idmap initialization returned %s\n",
336 nt_errstr(status)));
337 goto fail;
340 talloc_set_destructor(result, close_domain_destructor);
342 return result;
344 fail:
345 TALLOC_FREE(result);
346 return NULL;
350 * Initialize the default domain structure
351 * @param[in] mem_ctx memory context for the result
352 * @result The default domain structure
354 * This routine takes the module name from the "idmap backend" parameter,
355 * passing a possible parameter like ldap:ldap://ldap-url/ to the module.
358 static struct idmap_domain *idmap_init_default_domain(TALLOC_CTX *mem_ctx)
360 struct idmap_domain *result;
361 char *modulename;
362 char *params;
364 idmap_init();
366 if (!parse_idmap_module(talloc_tos(), lp_idmap_backend(), &modulename,
367 &params)) {
368 DEBUG(1, ("parse_idmap_module failed\n"));
369 return NULL;
372 DEBUG(3, ("idmap_init: using '%s' as remote backend\n", modulename));
374 result = idmap_init_domain(mem_ctx, "*", modulename, params);
375 if (result == NULL) {
376 goto fail;
379 TALLOC_FREE(modulename);
380 TALLOC_FREE(params);
381 return result;
383 fail:
384 TALLOC_FREE(modulename);
385 TALLOC_FREE(params);
386 TALLOC_FREE(result);
387 return NULL;
391 * Initialize a named domain structure
392 * @param[in] mem_ctx memory context for the result
393 * @param[in] domname the domain name
394 * @result The default domain structure
396 * This routine looks at the "idmap config <domname>" parameters to figure out
397 * the configuration.
400 static struct idmap_domain *idmap_init_named_domain(TALLOC_CTX *mem_ctx,
401 const char *domname)
403 struct idmap_domain *result = NULL;
404 char *config_option;
405 const char *backend;
407 config_option = talloc_asprintf(talloc_tos(), "idmap config %s",
408 domname);
409 if (config_option == NULL) {
410 DEBUG(0, ("talloc failed\n"));
411 goto fail;
414 backend = lp_parm_const_string(-1, config_option, "backend", NULL);
415 if (backend == NULL) {
416 DEBUG(1, ("no backend defined for %s\n", config_option));
417 goto fail;
420 result = idmap_init_domain(mem_ctx, domname, backend, NULL);
421 if (result == NULL) {
422 goto fail;
425 TALLOC_FREE(config_option);
426 return result;
428 fail:
429 TALLOC_FREE(config_option);
430 TALLOC_FREE(result);
431 return NULL;
435 * Initialize the passdb domain structure
436 * @param[in] mem_ctx memory context for the result
437 * @result The default domain structure
439 * No config, passdb has its own configuration.
442 static struct idmap_domain *idmap_init_passdb_domain(TALLOC_CTX *mem_ctx)
445 * Always init the default domain, we can't go without one
447 if (default_idmap_domain == NULL) {
448 default_idmap_domain = idmap_init_default_domain(NULL);
450 if (default_idmap_domain == NULL) {
451 return NULL;
454 if (passdb_idmap_domain != NULL) {
455 return passdb_idmap_domain;
458 passdb_idmap_domain = idmap_init_domain(NULL, get_global_sam_name(),
459 "passdb", NULL);
460 if (passdb_idmap_domain == NULL) {
461 DEBUG(1, ("Could not init passdb idmap domain\n"));
464 return passdb_idmap_domain;
468 * Find a domain struct according to a domain name
469 * @param[in] domname Domain name to get the config for
470 * @result The default domain structure that fits
472 * This is the central routine in the winbindd-idmap child to pick the correct
473 * domain for looking up IDs. If domname is NULL or empty, we use the default
474 * domain. If it contains something, we try to use idmap_init_named_domain()
475 * to fetch the correct backend.
477 * The choice about "domname" is being made by the winbind parent, look at the
478 * "have_idmap_config" of "struct winbindd_domain" which is set in
479 * add_trusted_domain.
482 static struct idmap_domain *idmap_find_domain(const char *domname)
484 struct idmap_domain *result;
485 int i;
487 DEBUG(10, ("idmap_find_domain called for domain '%s'\n",
488 domname?domname:"NULL"));
491 * Always init the default domain, we can't go without one
493 if (default_idmap_domain == NULL) {
494 default_idmap_domain = idmap_init_default_domain(NULL);
496 if (default_idmap_domain == NULL) {
497 return NULL;
500 if ((domname == NULL) || (domname[0] == '\0')) {
501 return default_idmap_domain;
504 for (i=0; i<num_domains; i++) {
505 if (strequal(idmap_domains[i]->name, domname)) {
506 return idmap_domains[i];
510 if (idmap_domains == NULL) {
512 * talloc context for all idmap domains
514 idmap_domains = TALLOC_ARRAY(NULL, struct idmap_domain *, 1);
517 if (idmap_domains == NULL) {
518 DEBUG(0, ("talloc failed\n"));
519 return NULL;
522 result = idmap_init_named_domain(idmap_domains, domname);
523 if (result == NULL) {
525 * Could not init that domain -- try the default one
527 return default_idmap_domain;
530 ADD_TO_ARRAY(idmap_domains, struct idmap_domain *, result,
531 &idmap_domains, &num_domains);
532 return result;
535 void idmap_close(void)
537 if (idmap_alloc_ctx) {
538 idmap_alloc_ctx->methods->close_fn();
539 idmap_alloc_ctx->methods = NULL;
541 alloc_backends = NULL;
542 TALLOC_FREE(default_idmap_domain);
543 TALLOC_FREE(passdb_idmap_domain);
544 TALLOC_FREE(idmap_domains);
545 num_domains = 0;
549 * Initialize the idmap alloc backend
550 * @param[out] ctx Where to put the alloc_ctx?
551 * @result Did it work fine?
553 * This routine first looks at "idmap alloc backend" and if that is not
554 * defined, it uses "idmap backend" for the module name.
556 static NTSTATUS idmap_alloc_init(struct idmap_alloc_context **ctx)
558 const char *backend;
559 char *modulename, *params;
560 NTSTATUS ret = NT_STATUS_NO_MEMORY;;
562 idmap_init();
564 if (idmap_alloc_ctx != NULL) {
565 *ctx = idmap_alloc_ctx;
566 return NT_STATUS_OK;
569 idmap_alloc_ctx = talloc(NULL, struct idmap_alloc_context);
570 if (idmap_alloc_ctx == NULL) {
571 DEBUG(0, ("talloc failed\n"));
572 goto fail;
575 backend = lp_idmap_alloc_backend();
576 if ((backend == NULL) || (backend[0] == '\0')) {
577 backend = lp_idmap_backend();
580 if (backend == NULL) {
581 DEBUG(3, ("no idmap alloc backend defined\n"));
582 ret = NT_STATUS_INVALID_PARAMETER;
583 goto fail;
586 if (!parse_idmap_module(idmap_alloc_ctx, backend, &modulename,
587 &params)) {
588 DEBUG(1, ("parse_idmap_module %s failed\n", backend));
589 goto fail;
592 idmap_alloc_ctx->methods = get_alloc_methods(modulename);
594 if (idmap_alloc_ctx->methods == NULL) {
595 ret = smb_probe_module("idmap", modulename);
596 if (NT_STATUS_IS_OK(ret)) {
597 idmap_alloc_ctx->methods =
598 get_alloc_methods(modulename);
602 if (idmap_alloc_ctx->methods == NULL) {
603 DEBUG(1, ("could not find idmap alloc module %s\n", backend));
604 ret = NT_STATUS_INVALID_PARAMETER;
605 goto fail;
608 ret = idmap_alloc_ctx->methods->init(params);
610 if (!NT_STATUS_IS_OK(ret)) {
611 DEBUG(0, ("ERROR: Initialization failed for alloc "
612 "backend, deferred!\n"));
613 goto fail;
616 TALLOC_FREE(modulename);
617 TALLOC_FREE(params);
619 *ctx = idmap_alloc_ctx;
620 return NT_STATUS_OK;
622 fail:
623 TALLOC_FREE(idmap_alloc_ctx);
624 return ret;
627 /**************************************************************************
628 idmap allocator interface functions
629 **************************************************************************/
631 NTSTATUS idmap_allocate_uid(struct unixid *id)
633 struct idmap_alloc_context *ctx;
634 NTSTATUS ret;
636 if (!NT_STATUS_IS_OK(ret = idmap_alloc_init(&ctx))) {
637 return ret;
640 id->type = ID_TYPE_UID;
641 return ctx->methods->allocate_id(id);
644 NTSTATUS idmap_allocate_gid(struct unixid *id)
646 struct idmap_alloc_context *ctx;
647 NTSTATUS ret;
649 if (!NT_STATUS_IS_OK(ret = idmap_alloc_init(&ctx))) {
650 return ret;
653 id->type = ID_TYPE_GID;
654 return ctx->methods->allocate_id(id);
657 NTSTATUS idmap_set_uid_hwm(struct unixid *id)
659 struct idmap_alloc_context *ctx;
660 NTSTATUS ret;
662 if (!NT_STATUS_IS_OK(ret = idmap_alloc_init(&ctx))) {
663 return ret;
666 id->type = ID_TYPE_UID;
667 return ctx->methods->set_id_hwm(id);
670 NTSTATUS idmap_set_gid_hwm(struct unixid *id)
672 struct idmap_alloc_context *ctx;
673 NTSTATUS ret;
675 if (!NT_STATUS_IS_OK(ret = idmap_alloc_init(&ctx))) {
676 return ret;
679 id->type = ID_TYPE_GID;
680 return ctx->methods->set_id_hwm(id);
683 NTSTATUS idmap_new_mapping(const struct dom_sid *psid, enum id_type type,
684 struct unixid *pxid)
686 struct dom_sid sid;
687 struct idmap_domain *dom;
688 struct id_map map;
689 NTSTATUS status;
691 dom = idmap_find_domain(NULL);
692 if (dom == NULL) {
693 DEBUG(3, ("no default domain, no place to write\n"));
694 return NT_STATUS_ACCESS_DENIED;
696 if (dom->methods->set_mapping == NULL) {
697 DEBUG(3, ("default domain not writable\n"));
698 return NT_STATUS_MEDIA_WRITE_PROTECTED;
701 sid_copy(&sid, psid);
702 map.sid = &sid;
703 map.xid.type = type;
705 switch (type) {
706 case ID_TYPE_UID:
707 status = idmap_allocate_uid(&map.xid);
708 break;
709 case ID_TYPE_GID:
710 status = idmap_allocate_gid(&map.xid);
711 break;
712 default:
713 status = NT_STATUS_INVALID_PARAMETER;
714 break;
717 if (!NT_STATUS_IS_OK(status)) {
718 DEBUG(3, ("Could not allocate id: %s\n", nt_errstr(status)));
719 return status;
722 map.status = ID_MAPPED;
724 DEBUG(10, ("Setting mapping: %s <-> %s %lu\n",
725 sid_string_dbg(map.sid),
726 (map.xid.type == ID_TYPE_UID) ? "UID" : "GID",
727 (unsigned long)map.xid.id));
729 status = dom->methods->set_mapping(dom, &map);
731 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_COLLISION)) {
732 struct id_map *ids[2];
733 DEBUG(5, ("Mapping for %s exists - retrying to map sid\n",
734 sid_string_dbg(map.sid)));
735 ids[0] = &map;
736 ids[1] = NULL;
737 status = dom->methods->sids_to_unixids(dom, ids);
740 if (!NT_STATUS_IS_OK(status)) {
741 DEBUG(3, ("Could not store the new mapping: %s\n",
742 nt_errstr(status)));
743 return status;
746 *pxid = map.xid;
748 return NT_STATUS_OK;
751 NTSTATUS idmap_backends_unixid_to_sid(const char *domname, struct id_map *id)
753 struct idmap_domain *dom;
754 struct id_map *maps[2];
756 DEBUG(10, ("idmap_backend_unixid_to_sid: domain = '%s', xid = %d "
757 "(type %d)\n",
758 domname?domname:"NULL", id->xid.id, id->xid.type));
760 maps[0] = id;
761 maps[1] = NULL;
764 * Always give passdb a chance first
767 dom = idmap_init_passdb_domain(NULL);
768 if ((dom != NULL)
769 && NT_STATUS_IS_OK(dom->methods->unixids_to_sids(dom, maps))
770 && id->status == ID_MAPPED) {
771 return NT_STATUS_OK;
774 dom = idmap_find_domain(domname);
775 if (dom == NULL) {
776 return NT_STATUS_NONE_MAPPED;
779 return dom->methods->unixids_to_sids(dom, maps);
782 NTSTATUS idmap_backends_sid_to_unixid(const char *domain, struct id_map *id)
784 struct idmap_domain *dom;
785 struct id_map *maps[2];
787 DEBUG(10, ("idmap_backends_sid_to_unixid: domain = '%s', sid = [%s]\n",
788 domain?domain:"NULL", sid_string_dbg(id->sid)));
790 maps[0] = id;
791 maps[1] = NULL;
793 if (sid_check_is_in_builtin(id->sid)
794 || (sid_check_is_in_our_domain(id->sid))) {
796 dom = idmap_init_passdb_domain(NULL);
797 if (dom == NULL) {
798 return NT_STATUS_NONE_MAPPED;
800 return dom->methods->sids_to_unixids(dom, maps);
803 dom = idmap_find_domain(domain);
804 if (dom == NULL) {
805 return NT_STATUS_NONE_MAPPED;
808 return dom->methods->sids_to_unixids(dom, maps);
811 NTSTATUS idmap_set_mapping(const struct id_map *map)
813 struct idmap_domain *dom;
815 dom = idmap_find_domain(NULL);
816 if (dom == NULL) {
817 DEBUG(3, ("no default domain, no place to write\n"));
818 return NT_STATUS_ACCESS_DENIED;
820 if (dom->methods->set_mapping == NULL) {
821 DEBUG(3, ("default domain not writable\n"));
822 return NT_STATUS_MEDIA_WRITE_PROTECTED;
825 return dom->methods->set_mapping(dom, map);
828 NTSTATUS idmap_remove_mapping(const struct id_map *map)
830 struct idmap_domain *dom;
832 dom = idmap_find_domain(NULL);
833 if (dom == NULL) {
834 DEBUG(3, ("no default domain, no place to write\n"));
835 return NT_STATUS_ACCESS_DENIED;
837 if (dom->methods->remove_mapping == NULL) {
838 DEBUG(3, ("default domain not writable\n"));
839 return NT_STATUS_MEDIA_WRITE_PROTECTED;
842 return dom->methods->remove_mapping(dom, map);