r22448: grab idmap compat patch from 3.0.25 (will recur 3.0.25rc2 for this)
[Samba.git] / source / nsswitch / idmap.c
blob7a74f744b46fccd21d05c377c3d552addcf0d3c1
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
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 2 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, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include "includes.h"
25 #include "winbindd.h"
27 #undef DBGC_CLASS
28 #define DBGC_CLASS DBGC_IDMAP
30 static_decl_idmap;
32 struct idmap_backend {
33 const char *name;
34 struct idmap_methods *methods;
35 struct idmap_backend *prev, *next;
38 struct idmap_alloc_backend {
39 const char *name;
40 struct idmap_alloc_methods *methods;
41 struct idmap_alloc_backend *prev, *next;
44 struct idmap_cache_ctx;
46 struct idmap_alloc_context {
47 const char *params;
48 struct idmap_alloc_methods *methods;
49 BOOL initialized;
52 static TALLOC_CTX *idmap_ctx = NULL;
53 static struct idmap_cache_ctx *idmap_cache;
55 static struct idmap_backend *backends = NULL;
56 static struct idmap_domain **idmap_domains = NULL;
57 static int num_domains = 0;
58 static int pdb_dom_num = -1;
59 static int def_dom_num = -1;
61 static struct idmap_alloc_backend *alloc_backends = NULL;
62 static struct idmap_alloc_context *idmap_alloc_ctx = NULL;
64 #define IDMAP_CHECK_RET(ret) do { if ( ! NT_STATUS_IS_OK(ret)) { DEBUG(2, ("ERROR: NTSTATUS = 0x%08x\n", NT_STATUS_V(ret))); goto done; } } while(0)
65 #define IDMAP_REPORT_RET(ret) do { if ( ! NT_STATUS_IS_OK(ret)) { DEBUG(2, ("ERROR: NTSTATUS = 0x%08x\n", NT_STATUS_V(ret))); } } while(0)
66 #define IDMAP_CHECK_ALLOC(mem) do { if (!mem) { DEBUG(0, ("Out of memory!\n")); ret = NT_STATUS_NO_MEMORY; goto done; } } while(0)
68 static struct idmap_methods *get_methods(struct idmap_backend *be, const char *name)
70 struct idmap_backend *b;
72 for (b = be; b; b = b->next) {
73 if (strequal(b->name, name)) {
74 return b->methods;
78 return NULL;
81 static struct idmap_alloc_methods *get_alloc_methods(struct idmap_alloc_backend *be, const char *name)
83 struct idmap_alloc_backend *b;
85 for (b = be; b; b = b->next) {
86 if (strequal(b->name, name)) {
87 return b->methods;
91 return NULL;
94 BOOL idmap_is_offline(void)
96 return ( lp_winbind_offline_logon() &&
97 get_global_winbindd_state_offline() );
100 /**********************************************************************
101 Allow a module to register itself as a method.
102 **********************************************************************/
104 NTSTATUS smb_register_idmap(int version, const char *name, struct idmap_methods *methods)
106 struct idmap_methods *test;
107 struct idmap_backend *entry;
109 if (!idmap_ctx) {
110 return NT_STATUS_INTERNAL_DB_ERROR;
113 if ((version != SMB_IDMAP_INTERFACE_VERSION)) {
114 DEBUG(0, ("Failed to register idmap module.\n"
115 "The module was compiled against SMB_IDMAP_INTERFACE_VERSION %d,\n"
116 "current SMB_IDMAP_INTERFACE_VERSION is %d.\n"
117 "Please recompile against the current version of samba!\n",
118 version, SMB_IDMAP_INTERFACE_VERSION));
119 return NT_STATUS_OBJECT_TYPE_MISMATCH;
122 if (!name || !name[0] || !methods) {
123 DEBUG(0,("Called with NULL pointer or empty name!\n"));
124 return NT_STATUS_INVALID_PARAMETER;
127 test = get_methods(backends, name);
128 if (test) {
129 DEBUG(0,("Idmap module %s already registered!\n", name));
130 return NT_STATUS_OBJECT_NAME_COLLISION;
133 entry = talloc(idmap_ctx, struct idmap_backend);
134 if ( ! entry) {
135 DEBUG(0,("Out of memory!\n"));
136 return NT_STATUS_NO_MEMORY;
138 entry->name = talloc_strdup(idmap_ctx, name);
139 if ( ! entry->name) {
140 DEBUG(0,("Out of memory!\n"));
141 return NT_STATUS_NO_MEMORY;
143 entry->methods = methods;
145 DLIST_ADD(backends, entry);
146 DEBUG(5, ("Successfully added idmap backend '%s'\n", name));
147 return NT_STATUS_OK;
150 /**********************************************************************
151 Allow a module to register itself as a method.
152 **********************************************************************/
154 NTSTATUS smb_register_idmap_alloc(int version, const char *name, struct idmap_alloc_methods *methods)
156 struct idmap_alloc_methods *test;
157 struct idmap_alloc_backend *entry;
159 if (!idmap_ctx) {
160 return NT_STATUS_INTERNAL_DB_ERROR;
163 if ((version != SMB_IDMAP_INTERFACE_VERSION)) {
164 DEBUG(0, ("Failed to register idmap alloc module.\n"
165 "The module was compiled against SMB_IDMAP_INTERFACE_VERSION %d,\n"
166 "current SMB_IDMAP_INTERFACE_VERSION is %d.\n"
167 "Please recompile against the current version of samba!\n",
168 version, SMB_IDMAP_INTERFACE_VERSION));
169 return NT_STATUS_OBJECT_TYPE_MISMATCH;
172 if (!name || !name[0] || !methods) {
173 DEBUG(0,("Called with NULL pointer or empty name!\n"));
174 return NT_STATUS_INVALID_PARAMETER;
177 test = get_alloc_methods(alloc_backends, name);
178 if (test) {
179 DEBUG(0,("idmap_alloc module %s already registered!\n", name));
180 return NT_STATUS_OBJECT_NAME_COLLISION;
183 entry = talloc(idmap_ctx, struct idmap_alloc_backend);
184 if ( ! entry) {
185 DEBUG(0,("Out of memory!\n"));
186 return NT_STATUS_NO_MEMORY;
188 entry->name = talloc_strdup(idmap_ctx, name);
189 if ( ! entry->name) {
190 DEBUG(0,("Out of memory!\n"));
191 return NT_STATUS_NO_MEMORY;
193 entry->methods = methods;
195 DLIST_ADD(alloc_backends, entry);
196 DEBUG(5, ("Successfully added idmap alloc backend '%s'\n", name));
197 return NT_STATUS_OK;
200 static int close_domain_destructor(struct idmap_domain *dom)
202 NTSTATUS ret;
204 ret = dom->methods->close_fn(dom);
205 if (!NT_STATUS_IS_OK(ret)) {
206 DEBUG(3, ("Failed to close idmap domain [%s]!\n", dom->name));
209 return 0;
212 /**************************************************************************
213 Shutdown.
214 **************************************************************************/
216 NTSTATUS idmap_close(void)
218 /* close the alloc backend first before freeing idmap_ctx */
219 if (idmap_alloc_ctx) {
220 idmap_alloc_ctx->methods->close_fn();
221 idmap_alloc_ctx->methods = NULL;
223 alloc_backends = NULL;
225 /* this talloc_free call will fire the talloc destructors
226 * that will free all active backends resources */
227 TALLOC_FREE(idmap_ctx);
228 idmap_cache = NULL;
229 idmap_domains = NULL;
230 backends = NULL;
232 return NT_STATUS_OK;
235 /**********************************************************************
236 Initialise idmap cache and a remote backend (if configured).
237 **********************************************************************/
239 static const char *idmap_default_domain[] = { "default domain", NULL };
241 /****************************************************************************
242 ****************************************************************************/
244 NTSTATUS idmap_init_cache(void)
246 /* Always initialize the cache. We'll have to delay initialization
247 of backends if we are offline */
249 if ( idmap_ctx ) {
250 return NT_STATUS_OK;
253 if ( (idmap_ctx = talloc_named_const(NULL, 0, "idmap_ctx")) == NULL ) {
254 return NT_STATUS_NO_MEMORY;
257 if ( (idmap_cache = idmap_cache_init(idmap_ctx)) == NULL ) {
258 return NT_STATUS_UNSUCCESSFUL;
261 return NT_STATUS_OK;
264 /****************************************************************************
265 ****************************************************************************/
267 NTSTATUS idmap_init(void)
269 NTSTATUS ret;
270 static NTSTATUS idmap_init_status = NT_STATUS_UNSUCCESSFUL;
271 struct idmap_domain *dom;
272 char *compat_backend = NULL;
273 char *compat_params = NULL;
274 const char **dom_list = NULL;
275 char *alloc_backend = NULL;
276 BOOL default_already_defined = False;
277 BOOL pri_dom_is_in_list = False;
278 int compat = 0;
279 int i;
281 ret = idmap_init_cache();
282 if ( !NT_STATUS_IS_OK(ret) )
283 return ret;
285 if (NT_STATUS_IS_OK(idmap_init_status))
286 return NT_STATUS_OK;
288 static_init_idmap;
290 dom_list = lp_idmap_domains();
292 if ( dom_list && lp_idmap_backend() ) {
293 DEBUG(0, ("WARNING: idmap backend and idmap domains are "
294 "mutually excusive!\n"));
295 DEBUGADD(0,("idmap backend option will be IGNORED!\n"));
296 } else if ( lp_idmap_backend() ) {
297 const char **compat_list = lp_idmap_backend();
298 char *p = NULL;
299 const char *q = NULL;
301 compat = 1;
303 if ( (compat_backend = talloc_strdup( idmap_ctx, *compat_list )) == NULL ) {
304 ret = NT_STATUS_NO_MEMORY;
305 goto done;
308 /* strip any leading idmap_ prefix of */
309 if (strncmp(*compat_list, "idmap_", 6) == 0 ) {
310 q = *compat_list += 6;
311 DEBUG(0, ("WARNING: idmap backend uses obsolete and "
312 "deprecated 'idmap_' prefix.\n"
313 "Please replace 'idmap_%s' by '%s' in %s\n",
314 q, q, dyn_CONFIGFILE));
315 compat_backend = talloc_strdup( idmap_ctx, q);
316 } else {
317 compat_backend = talloc_strdup( idmap_ctx, *compat_list);
320 /* separate the backend and module arguements */
321 if ((p = strchr(compat_backend, ':')) != NULL) {
322 *p = '\0';
323 compat_params = p + 1;
325 } else {
326 /* Back compatible: without idmap domains and explicit
327 idmap backend. Taking default idmap backend: tdb */
329 compat = 1;
330 compat_backend = talloc_strdup( idmap_ctx, "tdb");
331 compat_params = compat_backend;
335 if ( ! dom_list) {
336 dom_list = idmap_default_domain;
339 /***************************
340 * initialize idmap domains
342 DEBUG(1, ("Initializing idmap domains\n"));
344 for (i = 0; dom_list[i]; i++) {
345 const char *parm_backend;
346 char *config_option;
348 /* ignore BUILTIN and local MACHINE domains */
349 if ( strequal(dom_list[i], "BUILTIN")
350 || strequal(dom_list[i], get_global_sam_name() ) )
352 DEBUG(0,("idmap_init: Ignoring invalid domain %s\n",
353 dom_list[i]));
354 continue;
357 if (strequal(dom_list[i], lp_workgroup())) {
358 pri_dom_is_in_list = True;
360 /* init domain */
362 dom = talloc_zero(idmap_ctx, struct idmap_domain);
363 IDMAP_CHECK_ALLOC(dom);
365 dom->name = talloc_strdup(dom, dom_list[i]);
366 IDMAP_CHECK_ALLOC(dom->name);
368 config_option = talloc_asprintf(dom, "idmap config %s", dom->name);
369 IDMAP_CHECK_ALLOC(config_option);
371 /* default or specific ? */
373 dom->default_domain = lp_parm_bool(-1, config_option, "default", False);
375 if (dom->default_domain ||
376 strequal(dom_list[i], idmap_default_domain[0])) {
378 /* make sure this is set even when we match idmap_default_domain[0] */
379 dom->default_domain = True;
381 if (default_already_defined) {
382 DEBUG(1, ("ERROR: Multiple domains defined as default!\n"));
383 ret = NT_STATUS_INVALID_PARAMETER;
384 goto done;
387 default_already_defined = True;
391 dom->readonly = lp_parm_bool(-1, config_option, "readonly", False);
393 /* find associated backend (default: tdb) */
394 if (compat) {
395 parm_backend = talloc_strdup(idmap_ctx, compat_backend);
396 } else {
397 parm_backend = talloc_strdup(idmap_ctx,
398 lp_parm_const_string(-1, config_option, "backend", "tdb"));
400 IDMAP_CHECK_ALLOC(parm_backend);
402 /* get the backend methods for this domain */
403 dom->methods = get_methods(backends, parm_backend);
405 if ( ! dom->methods) {
406 ret = smb_probe_module("idmap", parm_backend);
407 if (NT_STATUS_IS_OK(ret)) {
408 dom->methods = get_methods(backends, parm_backend);
411 if ( ! dom->methods) {
412 DEBUG(0, ("ERROR: Could not get methods for backend %s\n", parm_backend));
413 ret = NT_STATUS_UNSUCCESSFUL;
414 goto done;
417 /* check the set_mapping function exists otherwise mark the module as readonly */
418 if ( ! dom->methods->set_mapping) {
419 DEBUG(5, ("Forcing to readonly, as ithis module can't store arbitrary mappings.\n"));
420 dom->readonly = True;
423 /* now that we have methods, set the destructor for this domain */
424 talloc_set_destructor(dom, close_domain_destructor);
426 if (compat_params) {
427 dom->params = talloc_strdup(dom, compat_params);
428 IDMAP_CHECK_ALLOC(dom->params);
429 } else {
430 dom->params = NULL;
433 /* Finally instance a backend copy for this domain */
434 ret = dom->methods->init(dom);
435 if ( ! NT_STATUS_IS_OK(ret)) {
436 DEBUG(0, ("ERROR: Initialization failed for backend %s (domain %s), deferred!\n",
437 parm_backend, dom->name));
439 idmap_domains = talloc_realloc(idmap_ctx, idmap_domains, struct idmap_domain *, i+1);
440 if ( ! idmap_domains) {
441 DEBUG(0, ("Out of memory!\n"));
442 ret = NT_STATUS_NO_MEMORY;
443 goto done;
445 idmap_domains[i] = dom;
447 if (dom->default_domain) { /* save default domain position for future uses */
448 def_dom_num = i;
451 DEBUG(10, ("Domain %s - Backend %s - %sdefault - %sreadonly\n",
452 dom->name, parm_backend,
453 dom->default_domain?"":"not ", dom->readonly?"":"not "));
455 talloc_free(config_option);
458 /* save the number of domains we have */
459 num_domains = i;
461 /* automatically add idmap_nss backend if needed */
462 if ((lp_server_role() == ROLE_DOMAIN_MEMBER) &&
463 ( ! pri_dom_is_in_list) &&
464 lp_winbind_trusted_domains_only()) {
466 dom = talloc_zero(idmap_ctx, struct idmap_domain);
467 IDMAP_CHECK_ALLOC(dom);
469 dom->name = talloc_strdup(dom, lp_workgroup());
470 IDMAP_CHECK_ALLOC(dom->name);
472 dom->default_domain = False;
473 dom->readonly = True;
475 /* get the backend methods for passdb */
476 dom->methods = get_methods(backends, "nss");
478 /* (the nss module is always statically linked) */
479 if ( ! dom->methods) {
480 DEBUG(0, ("ERROR: Could not get methods for idmap_nss ?!\n"));
481 ret = NT_STATUS_UNSUCCESSFUL;
482 goto done;
485 /* now that we have methods, set the destructor for this domain */
486 talloc_set_destructor(dom, close_domain_destructor);
488 if (compat_params) {
489 dom->params = talloc_strdup(dom, compat_params);
490 IDMAP_CHECK_ALLOC(dom->params);
491 } else {
492 dom->params = NULL;
495 /* Finally instance a backend copy for this domain */
496 ret = dom->methods->init(dom);
497 if ( ! NT_STATUS_IS_OK(ret)) {
498 DEBUG(0, ("ERROR: Initialization failed for idmap_nss ?!\n"));
499 ret = NT_STATUS_UNSUCCESSFUL;
500 goto done;
503 idmap_domains = talloc_realloc(idmap_ctx, idmap_domains, struct idmap_domain *, num_domains+1);
504 if ( ! idmap_domains) {
505 DEBUG(0, ("Out of memory!\n"));
506 ret = NT_STATUS_NO_MEMORY;
507 goto done;
509 idmap_domains[num_domains] = dom;
511 DEBUG(10, ("Domain %s - Backend nss - not default - readonly\n", dom->name ));
513 num_domains++;
516 /**** automatically add idmap_passdb backend ****/
517 dom = talloc_zero(idmap_ctx, struct idmap_domain);
518 IDMAP_CHECK_ALLOC(dom);
520 dom->name = talloc_strdup(dom, get_global_sam_name());
521 IDMAP_CHECK_ALLOC(dom->name);
523 dom->default_domain = False;
524 dom->readonly = True;
526 /* get the backend methods for passdb */
527 dom->methods = get_methods(backends, "passdb");
529 /* (the passdb module is always statically linked) */
530 if ( ! dom->methods) {
531 DEBUG(0, ("ERROR: Could not get methods for idmap_passdb ?!\n"));
532 ret = NT_STATUS_UNSUCCESSFUL;
533 goto done;
536 /* now that we have methods, set the destructor for this domain */
537 talloc_set_destructor(dom, close_domain_destructor);
539 if (compat_params) {
540 dom->params = talloc_strdup(dom, compat_params);
541 IDMAP_CHECK_ALLOC(dom->params);
542 } else {
543 dom->params = NULL;
546 /* Finally instance a backend copy for this domain */
547 ret = dom->methods->init(dom);
548 if ( ! NT_STATUS_IS_OK(ret)) {
549 DEBUG(0, ("ERROR: Initialization failed for idmap_passdb ?!\n"));
550 ret = NT_STATUS_UNSUCCESSFUL;
551 goto done;
554 idmap_domains = talloc_realloc(idmap_ctx, idmap_domains, struct idmap_domain *, num_domains+1);
555 if ( ! idmap_domains) {
556 DEBUG(0, ("Out of memory!\n"));
557 ret = NT_STATUS_NO_MEMORY;
558 goto done;
560 idmap_domains[num_domains] = dom;
562 /* needed to handle special BUILTIN and wellknown SIDs cases */
563 pdb_dom_num = num_domains;
565 DEBUG(10, ("Domain %s - Backend passdb - not default - readonly\n", dom->name));
567 num_domains++;
568 /**** finished adding idmap_passdb backend ****/
570 /* sort domains so that the default is the last one */
571 /* don't sort if no default domain defined */
572 if (def_dom_num != -1 && def_dom_num != num_domains-1) { /* default is not last, move it */
573 struct idmap_domain *tmp;
575 if (pdb_dom_num > def_dom_num) {
576 pdb_dom_num --;
578 } else if (pdb_dom_num == def_dom_num) { /* ?? */
579 pdb_dom_num = num_domains - 1;
582 tmp = idmap_domains[def_dom_num];
584 for (i = def_dom_num; i < num_domains-1; i++) {
585 idmap_domains[i] = idmap_domains[i+1];
587 idmap_domains[i] = tmp;
588 def_dom_num = i;
592 /* Initialize alloc module */
594 DEBUG(3, ("Initializing idmap alloc module\n"));
596 alloc_backend = NULL;
597 if (compat) {
598 alloc_backend = talloc_strdup(idmap_ctx, compat_backend);
599 } else {
600 char *ab = lp_idmap_alloc_backend();
602 if (ab && (ab[0] != '\0')) {
603 alloc_backend = talloc_strdup(idmap_ctx, lp_idmap_alloc_backend());
607 if ( alloc_backend ) {
609 idmap_alloc_ctx = talloc_zero(idmap_ctx, struct idmap_alloc_context);
610 IDMAP_CHECK_ALLOC(idmap_alloc_ctx);
612 idmap_alloc_ctx->methods = get_alloc_methods(alloc_backends, alloc_backend);
613 if ( ! idmap_alloc_ctx->methods) {
614 ret = smb_probe_module("idmap", alloc_backend);
615 if (NT_STATUS_IS_OK(ret)) {
616 idmap_alloc_ctx->methods = get_alloc_methods(alloc_backends, alloc_backend);
619 if (idmap_alloc_ctx->methods) {
621 if (compat_params) {
622 idmap_alloc_ctx->params = talloc_strdup(idmap_alloc_ctx, compat_params);
623 IDMAP_CHECK_ALLOC(idmap_alloc_ctx->params);
624 } else {
625 idmap_alloc_ctx->params = NULL;
628 ret = idmap_alloc_ctx->methods->init(idmap_alloc_ctx->params);
629 if ( ! NT_STATUS_IS_OK(ret)) {
630 DEBUG(0, ("ERROR: Initialization failed for alloc "
631 "backend %s, deferred!\n", alloc_backend));
632 } else {
633 idmap_alloc_ctx->initialized = True;
635 } else {
636 DEBUG(2, ("idmap_init: Unable to get methods for alloc backend %s\n",
637 alloc_backend));
638 /* certain compat backends are just readonly */
639 if ( compat ) {
640 TALLOC_FREE(idmap_alloc_ctx);
641 ret = NT_STATUS_OK;
642 } else {
643 ret = NT_STATUS_UNSUCCESSFUL;
648 /* cleanpu temporary strings */
649 TALLOC_FREE( compat_backend );
651 idmap_init_status = NT_STATUS_OK;
653 return ret;
655 done:
656 DEBUG(0, ("Aborting IDMAP Initialization ...\n"));
657 idmap_close();
659 return ret;
662 static NTSTATUS idmap_alloc_init(void)
664 NTSTATUS ret;
666 if (! NT_STATUS_IS_OK(ret = idmap_init())) {
667 return ret;
670 if ( ! idmap_alloc_ctx) {
671 return NT_STATUS_NOT_SUPPORTED;
674 if ( ! idmap_alloc_ctx->initialized) {
675 ret = idmap_alloc_ctx->methods->init(idmap_alloc_ctx->params);
676 if ( ! NT_STATUS_IS_OK(ret)) {
677 DEBUG(0, ("ERROR: Initialization failed for alloc "
678 "backend, deferred!\n"));
679 return ret;
680 } else {
681 idmap_alloc_ctx->initialized = True;
685 return NT_STATUS_OK;
688 /**************************************************************************
689 idmap allocator interface functions
690 **************************************************************************/
692 NTSTATUS idmap_allocate_uid(struct unixid *id)
694 NTSTATUS ret;
696 if (! NT_STATUS_IS_OK(ret = idmap_alloc_init())) {
697 return ret;
700 id->type = ID_TYPE_UID;
701 return idmap_alloc_ctx->methods->allocate_id(id);
704 NTSTATUS idmap_allocate_gid(struct unixid *id)
706 NTSTATUS ret;
708 if (! NT_STATUS_IS_OK(ret = idmap_alloc_init())) {
709 return ret;
712 id->type = ID_TYPE_GID;
713 return idmap_alloc_ctx->methods->allocate_id(id);
716 NTSTATUS idmap_set_uid_hwm(struct unixid *id)
718 NTSTATUS ret;
720 if (! NT_STATUS_IS_OK(ret = idmap_alloc_init())) {
721 return ret;
724 id->type = ID_TYPE_UID;
725 return idmap_alloc_ctx->methods->set_id_hwm(id);
728 NTSTATUS idmap_set_gid_hwm(struct unixid *id)
730 NTSTATUS ret;
732 if (! NT_STATUS_IS_OK(ret = idmap_alloc_init())) {
733 return ret;
736 id->type = ID_TYPE_GID;
737 return idmap_alloc_ctx->methods->set_id_hwm(id);
740 /******************************************************************************
741 Lookup an idmap_domain give a full user or group SID
742 ******************************************************************************/
744 static struct idmap_domain* find_idmap_domain_from_sid( DOM_SID *account_sid )
746 DOM_SID domain_sid;
747 uint32 rid;
748 struct winbindd_domain *domain = NULL;
749 int i;
751 /* 1. Handle BUILTIN or Special SIDs and prevent them from
752 falling into the default domain space (if we have a
753 configured passdb backend. */
755 if ( (pdb_dom_num != -1) &&
756 (sid_check_is_in_builtin(account_sid) ||
757 sid_check_is_in_wellknown_domain(account_sid) ||
758 sid_check_is_in_unix_groups(account_sid) ||
759 sid_check_is_in_unix_users(account_sid)) )
761 return idmap_domains[pdb_dom_num];
764 /* 2. Lookup the winbindd_domain from the account_sid */
766 sid_copy( &domain_sid, account_sid );
767 sid_split_rid( &domain_sid, &rid );
768 domain = find_domain_from_sid_noinit( &domain_sid );
770 for (i = 0; domain && i < num_domains; i++) {
771 if ( strequal( idmap_domains[i]->name, domain->name ) ) {
772 return idmap_domains[i];
776 /* 3. Fall back to the default domain */
778 if ( def_dom_num != -1 ) {
779 return idmap_domains[def_dom_num];
782 return NULL;
785 /******************************************************************************
786 Lookup an index given an idmap_domain pointer
787 ******************************************************************************/
789 static uint32 find_idmap_domain_index( struct idmap_domain *id_domain)
791 int i;
793 for (i = 0; i < num_domains; i++) {
794 if ( idmap_domains[i] == id_domain )
795 return i;
798 return -1;
802 /*********************************************************
803 Check if creating a mapping is permitted for the domain
804 *********************************************************/
806 static NTSTATUS idmap_can_map(const struct id_map *map, struct idmap_domain **ret_dom)
808 struct idmap_domain *dom;
810 /* Check we do not create mappings for our own local domain, or BUILTIN or special SIDs */
811 if ((sid_compare_domain(map->sid, get_global_sam_sid()) == 0) ||
812 sid_check_is_in_builtin(map->sid) ||
813 sid_check_is_in_wellknown_domain(map->sid)) {
814 DEBUG(10, ("We are not supposed to create mappings for our own domains (local, builtin, specials)\n"));
815 return NT_STATUS_UNSUCCESSFUL;
818 /* Special check for trusted domain only = Yes */
819 if (lp_winbind_trusted_domains_only()) {
820 struct winbindd_domain *wdom = find_our_domain();
821 if (wdom && (sid_compare_domain(map->sid, &wdom->sid) == 0)) {
822 DEBUG(10, ("We are not supposed to create mappings for our primary domain when <trusted domain only> is True\n"));
823 DEBUGADD(10, ("Leave [%s] unmapped\n", sid_string_static(map->sid)));
824 return NT_STATUS_UNSUCCESSFUL;
828 if ( (dom = find_idmap_domain_from_sid( map->sid )) == NULL ) {
829 /* huh, couldn't find a suitable domain, let's just leave it unmapped */
830 DEBUG(10, ("Could not find idmap backend for SID %s", sid_string_static(map->sid)));
831 return NT_STATUS_NO_SUCH_DOMAIN;
834 if (dom->readonly) {
835 /* ouch the domain is read only, let's just leave it unmapped */
836 DEBUG(10, ("idmap backend for SID %s is READONLY!\n", sid_string_static(map->sid)));
837 return NT_STATUS_UNSUCCESSFUL;
840 *ret_dom = dom;
841 return NT_STATUS_OK;
844 static NTSTATUS idmap_new_mapping(TALLOC_CTX *ctx, struct id_map *map)
846 NTSTATUS ret;
847 struct idmap_domain *dom;
849 /* If we are offline we cannot lookup SIDs, deny mapping */
850 if (idmap_is_offline()) {
851 return NT_STATUS_FILE_IS_OFFLINE;
854 ret = idmap_can_map(map, &dom);
855 if ( ! NT_STATUS_IS_OK(ret)) {
856 return NT_STATUS_NONE_MAPPED;
859 /* check if this is a valid SID and then map it */
860 switch (map->xid.type) {
861 case ID_TYPE_UID:
862 ret = idmap_allocate_uid(&map->xid);
863 if ( ! NT_STATUS_IS_OK(ret)) {
864 /* can't allocate id, let's just leave it unmapped */
865 DEBUG(2, ("uid allocation failed! Can't create mapping\n"));
866 return NT_STATUS_NONE_MAPPED;
868 break;
869 case ID_TYPE_GID:
870 ret = idmap_allocate_gid(&map->xid);
871 if ( ! NT_STATUS_IS_OK(ret)) {
872 /* can't allocate id, let's just leave it unmapped */
873 DEBUG(2, ("gid allocation failed! Can't create mapping\n"));
874 return NT_STATUS_NONE_MAPPED;
876 break;
877 default:
878 /* invalid sid, let's just leave it unmapped */
879 DEBUG(3,("idmap_new_mapping: Refusing to create a "
880 "mapping for an unspecified ID type.\n"));
881 return NT_STATUS_NONE_MAPPED;
884 /* ok, got a new id, let's set a mapping */
885 map->status = ID_MAPPED;
887 DEBUG(10, ("Setting mapping: %s <-> %s %lu\n",
888 sid_string_static(map->sid),
889 (map->xid.type == ID_TYPE_UID) ? "UID" : "GID",
890 (unsigned long)map->xid.id));
891 ret = dom->methods->set_mapping(dom, map);
893 if ( ! NT_STATUS_IS_OK(ret)) {
894 /* something wrong here :-( */
895 DEBUG(2, ("Failed to commit mapping\n!"));
897 /* TODO: would it make sense to have an "unalloc_id function?" */
899 return NT_STATUS_NONE_MAPPED;
902 return NT_STATUS_OK;
905 static NTSTATUS idmap_backends_set_mapping(const struct id_map *map)
907 struct idmap_domain *dom;
908 NTSTATUS ret;
910 DEBUG(10, ("Setting mapping %s <-> %s %lu\n",
911 sid_string_static(map->sid),
912 (map->xid.type == ID_TYPE_UID) ? "UID" : "GID",
913 (unsigned long)map->xid.id));
915 ret = idmap_can_map(map, &dom);
916 if ( ! NT_STATUS_IS_OK(ret)) {
917 return ret;
920 DEBUG(10,("set_mapping for domain %s\n", dom->name ));
922 return dom->methods->set_mapping(dom, map);
925 static NTSTATUS idmap_backends_unixids_to_sids(struct id_map **ids)
927 struct idmap_domain *dom;
928 struct id_map **unmapped;
929 struct id_map **_ids;
930 TALLOC_CTX *ctx;
931 NTSTATUS ret;
932 int i, u, n;
934 if (!ids || !*ids) {
935 DEBUG(1, ("Invalid list of maps\n"));
936 return NT_STATUS_INVALID_PARAMETER;
939 ctx = talloc_named_const(NULL, 0, "idmap_backends_unixids_to_sids ctx");
940 if ( ! ctx) {
941 DEBUG(0, ("Out of memory!\n"));
942 return NT_STATUS_NO_MEMORY;
945 DEBUG(10, ("Query backends to map ids->sids\n"));
947 /* start from the default (the last one) and then if there are still
948 * unmapped entries cycle through the others */
950 _ids = ids;
952 unmapped = NULL;
953 for (n = num_domains-1; n >= 0; n--) { /* cycle backwards */
955 dom = idmap_domains[n];
957 DEBUG(10, ("Query sids from domain %s\n", dom->name));
959 ret = dom->methods->unixids_to_sids(dom, _ids);
960 IDMAP_REPORT_RET(ret);
962 unmapped = NULL;
964 for (i = 0, u = 0; _ids[i]; i++) {
965 if (_ids[i]->status != ID_MAPPED) {
966 unmapped = talloc_realloc(ctx, unmapped, struct id_map *, u + 2);
967 IDMAP_CHECK_ALLOC(unmapped);
968 unmapped[u] = _ids[i];
969 u++;
972 if (unmapped) {
973 /* terminate the unmapped list */
974 unmapped[u] = NULL;
975 } else { /* no more entries, get out */
976 break;
979 _ids = unmapped;
983 if (unmapped) {
984 /* there are still unmapped ids, map them to the unix users/groups domains */
985 /* except for expired entries, these will be returned as valid (offline mode) */
986 for (i = 0; unmapped[i]; i++) {
987 if (unmapped[i]->status == ID_EXPIRED) continue;
988 switch (unmapped[i]->xid.type) {
989 case ID_TYPE_UID:
990 uid_to_unix_users_sid((uid_t)unmapped[i]->xid.id, unmapped[i]->sid);
991 unmapped[i]->status = ID_MAPPED;
992 break;
993 case ID_TYPE_GID:
994 gid_to_unix_groups_sid((gid_t)unmapped[i]->xid.id, unmapped[i]->sid);
995 unmapped[i]->status = ID_MAPPED;
996 break;
997 default: /* what?! */
998 unmapped[i]->status = ID_UNKNOWN;
999 break;
1004 ret = NT_STATUS_OK;
1006 done:
1007 talloc_free(ctx);
1008 return ret;
1011 static NTSTATUS idmap_backends_sids_to_unixids(struct id_map **ids)
1013 struct id_map ***dom_ids;
1014 struct idmap_domain *dom;
1015 TALLOC_CTX *ctx;
1016 NTSTATUS ret;
1017 int i, *counters;
1019 if ( (ctx = talloc_named_const(NULL, 0, "be_sids_to_ids")) == NULL ) {
1020 DEBUG(1, ("failed to allocate talloc context, OOM?\n"));
1021 return NT_STATUS_NO_MEMORY;
1024 DEBUG(10, ("Query backends to map sids->ids\n"));
1026 /* split list per domain */
1028 dom_ids = talloc_zero_array(ctx, struct id_map **, num_domains);
1029 IDMAP_CHECK_ALLOC(dom_ids);
1030 counters = talloc_zero_array(ctx, int, num_domains);
1032 /* partition the requests by domain */
1034 for (i = 0; ids[i]; i++) {
1035 uint32 idx;
1037 if ( (dom = find_idmap_domain_from_sid( ids[i]->sid )) == NULL ) {
1038 /* no available idmap_domain. Move on */
1039 continue;
1042 DEBUG(10,("SID %s is being handled by %s\n",
1043 sid_string_static(ids[i]->sid),
1044 dom ? dom->name : "none" ));
1046 idx = find_idmap_domain_index( dom );
1047 SMB_ASSERT( idx != -1 );
1049 dom_ids[idx] = talloc_realloc(ctx, dom_ids[idx],
1050 struct id_map *, counters[idx] + 2);
1051 IDMAP_CHECK_ALLOC(dom_ids[idx]);
1053 dom_ids[idx][counters[idx]] = ids[i];
1054 counters[idx]++;
1055 dom_ids[idx][counters[idx]] = NULL;
1058 /* All the ids have been dispatched in the right queues.
1059 Let's cycle through the filled ones */
1061 for (i = 0; i < num_domains; i++) {
1062 if (dom_ids[i]) {
1063 dom = idmap_domains[i];
1064 DEBUG(10, ("Query ids from domain %s\n", dom->name));
1065 ret = dom->methods->sids_to_unixids(dom, dom_ids[i]);
1066 IDMAP_REPORT_RET(ret);
1070 /* ok all the backends have been contacted at this point */
1071 /* let's see if we have any unmapped SID left and act accordingly */
1073 for (i = 0; ids[i]; i++) {
1074 /* NOTE: this will NOT touch ID_EXPIRED entries that the backend
1075 * was not able to confirm/deny (offline mode) */
1076 if (ids[i]->status == ID_UNKNOWN || ids[i]->status == ID_UNMAPPED) {
1077 /* ok this is an unmapped one, see if we can map it */
1078 ret = idmap_new_mapping(ctx, ids[i]);
1079 if (NT_STATUS_IS_OK(ret)) {
1080 /* successfully mapped */
1081 ids[i]->status = ID_MAPPED;
1082 } else if (NT_STATUS_EQUAL(ret, NT_STATUS_NONE_MAPPED)) {
1083 /* could not map it */
1084 ids[i]->status = ID_UNMAPPED;
1085 } else {
1086 /* Something very bad happened down there
1087 * OR we are offline */
1088 ids[i]->status = ID_UNKNOWN;
1093 ret = NT_STATUS_OK;
1095 done:
1096 talloc_free(ctx);
1097 return ret;
1100 /**************************************************************************
1101 idmap interface functions
1102 **************************************************************************/
1104 NTSTATUS idmap_unixids_to_sids(struct id_map **ids)
1106 TALLOC_CTX *ctx;
1107 NTSTATUS ret;
1108 struct id_map **bids;
1109 int i, bi;
1110 int bn = 0;
1112 if (! NT_STATUS_IS_OK(ret = idmap_init())) {
1113 return ret;
1116 if (!ids || !*ids) {
1117 DEBUG(1, ("Invalid list of maps\n"));
1118 return NT_STATUS_INVALID_PARAMETER;
1121 ctx = talloc_named_const(NULL, 0, "idmap_unixids_to_sids ctx");
1122 if ( ! ctx) {
1123 DEBUG(1, ("failed to allocate talloc context, OOM?\n"));
1124 return NT_STATUS_NO_MEMORY;
1127 /* no ids to be asked to the backends by default */
1128 bids = NULL;
1129 bi = 0;
1131 for (i = 0; ids[i]; i++) {
1133 if ( ! ids[i]->sid) {
1134 DEBUG(1, ("invalid null SID in id_map array"));
1135 talloc_free(ctx);
1136 return NT_STATUS_INVALID_PARAMETER;
1139 ret = idmap_cache_map_id(idmap_cache, ids[i]);
1141 if ( ! NT_STATUS_IS_OK(ret)) {
1143 if ( ! bids) {
1144 /* alloc space for ids to be resolved by backends (realloc ten by ten) */
1145 bids = talloc_array(ctx, struct id_map *, 10);
1146 if ( ! bids) {
1147 DEBUG(1, ("Out of memory!\n"));
1148 talloc_free(ctx);
1149 return NT_STATUS_NO_MEMORY;
1151 bn = 10;
1154 /* add this id to the ones to be retrieved from the backends */
1155 bids[bi] = ids[i];
1156 bi++;
1158 /* check if we need to allocate new space on the rids array */
1159 if (bi == bn) {
1160 bn += 10;
1161 bids = talloc_realloc(ctx, bids, struct id_map *, bn);
1162 if ( ! bids) {
1163 DEBUG(1, ("Out of memory!\n"));
1164 talloc_free(ctx);
1165 return NT_STATUS_NO_MEMORY;
1169 /* make sure the last element is NULL */
1170 bids[bi] = NULL;
1174 /* let's see if there is any id mapping to be retieved from the backends */
1175 if (bi) {
1177 ret = idmap_backends_unixids_to_sids(bids);
1178 IDMAP_CHECK_RET(ret);
1180 /* update the cache */
1181 for (i = 0; i < bi; i++) {
1182 if (bids[i]->status == ID_MAPPED) {
1183 ret = idmap_cache_set(idmap_cache, bids[i]);
1184 } else if (bids[i]->status == ID_EXPIRED) {
1185 /* the cache returned an expired entry and the backend was
1186 * was not able to clear the situation (offline).
1187 * This handles a previous NT_STATUS_SYNCHRONIZATION_REQUIRED
1188 * for disconnected mode, */
1189 bids[i]->status = ID_MAPPED;
1190 } else if (bids[i]->status == ID_UNKNOWN) {
1191 /* something bad here. We were not able to handle this for some
1192 * reason, mark it as unmapped and hope next time things will
1193 * settle down. */
1194 bids[i]->status = ID_UNMAPPED;
1195 } else { /* unmapped */
1196 ret = idmap_cache_set_negative_id(idmap_cache, bids[i]);
1198 IDMAP_CHECK_RET(ret);
1202 ret = NT_STATUS_OK;
1203 done:
1204 talloc_free(ctx);
1205 return ret;
1208 NTSTATUS idmap_sids_to_unixids(struct id_map **ids)
1210 TALLOC_CTX *ctx;
1211 NTSTATUS ret;
1212 struct id_map **bids;
1213 int i, bi;
1214 int bn = 0;
1216 if (! NT_STATUS_IS_OK(ret = idmap_init())) {
1217 return ret;
1220 if (!ids || !*ids) {
1221 DEBUG(1, ("Invalid list of maps\n"));
1222 return NT_STATUS_INVALID_PARAMETER;
1225 ctx = talloc_named_const(NULL, 0, "idmap_sids_to_unixids ctx");
1226 if ( ! ctx) {
1227 DEBUG(1, ("failed to allocate talloc context, OOM?\n"));
1228 return NT_STATUS_NO_MEMORY;
1231 /* no ids to be asked to the backends by default */
1232 bids = NULL;
1233 bi = 0;
1235 for (i = 0; ids[i]; i++) {
1237 if ( ! ids[i]->sid) {
1238 DEBUG(1, ("invalid null SID in id_map array\n"));
1239 talloc_free(ctx);
1240 return NT_STATUS_INVALID_PARAMETER;
1243 ret = idmap_cache_map_sid(idmap_cache, ids[i]);
1245 if ( ! NT_STATUS_IS_OK(ret)) {
1247 if ( ! bids) {
1248 /* alloc space for ids to be resolved
1249 by backends (realloc ten by ten) */
1250 bids = talloc_array(ctx, struct id_map *, 10);
1251 if ( ! bids) {
1252 DEBUG(1, ("Out of memory!\n"));
1253 talloc_free(ctx);
1254 return NT_STATUS_NO_MEMORY;
1256 bn = 10;
1259 /* add this id to the ones to be retrieved from the backends */
1260 bids[bi] = ids[i];
1261 bi++;
1263 /* check if we need to allocate new space on the ids array */
1264 if (bi == bn) {
1265 bn += 10;
1266 bids = talloc_realloc(ctx, bids, struct id_map *, bn);
1267 if ( ! bids) {
1268 DEBUG(1, ("Out of memory!\n"));
1269 talloc_free(ctx);
1270 return NT_STATUS_NO_MEMORY;
1274 /* make sure the last element is NULL */
1275 bids[bi] = NULL;
1279 /* let's see if there is any id mapping to be retieved from the backends */
1280 if (bids) {
1282 ret = idmap_backends_sids_to_unixids(bids);
1283 IDMAP_CHECK_RET(ret);
1285 /* update the cache */
1286 for (i = 0; bids[i]; i++) {
1287 if (bids[i]->status == ID_MAPPED) {
1288 ret = idmap_cache_set(idmap_cache, bids[i]);
1289 } else if (bids[i]->status == ID_EXPIRED) {
1290 /* the cache returned an expired entry and the backend was
1291 * was not able to clear the situation (offline).
1292 * This handles a previous NT_STATUS_SYNCHRONIZATION_REQUIRED
1293 * for disconnected mode, */
1294 bids[i]->status = ID_MAPPED;
1295 } else if (bids[i]->status == ID_UNKNOWN) {
1296 /* something bad here. We were not able to handle this for some
1297 * reason, mark it as unmapped and hope next time things will
1298 * settle down. */
1299 bids[i]->status = ID_UNMAPPED;
1300 } else { /* unmapped */
1301 ret = idmap_cache_set_negative_sid(idmap_cache, bids[i]);
1303 IDMAP_CHECK_RET(ret);
1307 ret = NT_STATUS_OK;
1308 done:
1309 talloc_free(ctx);
1310 return ret;
1313 NTSTATUS idmap_set_mapping(const struct id_map *id)
1315 TALLOC_CTX *ctx;
1316 NTSTATUS ret;
1318 if (! NT_STATUS_IS_OK(ret = idmap_init())) {
1319 return ret;
1322 /* sanity checks */
1323 if ((id->sid == NULL) || (id->status != ID_MAPPED)) {
1324 DEBUG(1, ("NULL SID or unmapped entry\n"));
1325 return NT_STATUS_INVALID_PARAMETER;
1328 /* TODO: check uid/gid range ? */
1330 ctx = talloc_named_const(NULL, 0, "idmap_set_mapping ctx");
1331 if ( ! ctx) {
1332 DEBUG(1, ("failed to allocate talloc context, OOM?\n"));
1333 return NT_STATUS_NO_MEMORY;
1336 /* set the new mapping */
1337 ret = idmap_backends_set_mapping(id);
1338 IDMAP_CHECK_RET(ret);
1340 /* set the mapping in the cache */
1341 ret = idmap_cache_set(idmap_cache, id);
1342 IDMAP_CHECK_RET(ret);
1344 done:
1345 talloc_free(ctx);
1346 return ret;
1349 /**************************************************************************
1350 Dump backend status.
1351 **************************************************************************/
1353 void idmap_dump_maps(char *logfile)
1355 NTSTATUS ret;
1356 struct unixid allid;
1357 struct id_map *maps;
1358 int num_maps;
1359 FILE *dump;
1360 int i;
1362 if (! NT_STATUS_IS_OK(ret = idmap_init())) {
1363 return;
1366 dump = fopen(logfile, "w");
1367 if ( ! dump) {
1368 DEBUG(0, ("Unable to open open stream for file [%s], errno: %d\n", logfile, errno));
1369 return;
1372 if (NT_STATUS_IS_OK(ret = idmap_alloc_init())) {
1373 allid.type = ID_TYPE_UID;
1374 allid.id = 0;
1375 idmap_alloc_ctx->methods->get_id_hwm(&allid);
1376 fprintf(dump, "USER HWM %lu\n", (unsigned long)allid.id);
1378 allid.type = ID_TYPE_GID;
1379 allid.id = 0;
1380 idmap_alloc_ctx->methods->get_id_hwm(&allid);
1381 fprintf(dump, "GROUP HWM %lu\n", (unsigned long)allid.id);
1384 maps = talloc(idmap_ctx, struct id_map);
1385 num_maps = 0;
1387 for (i = 0; i < num_domains; i++) {
1388 if (idmap_domains[i]->methods->dump_data) {
1389 idmap_domains[i]->methods->dump_data(idmap_domains[i], &maps, &num_maps);
1393 for (i = 0; i < num_maps; i++) {
1394 switch (maps[i].xid.type) {
1395 case ID_TYPE_UID:
1396 fprintf(dump, "UID %lu %s\n",
1397 (unsigned long)maps[i].xid.id,
1398 sid_string_static(maps[i].sid));
1399 break;
1400 case ID_TYPE_GID:
1401 fprintf(dump, "GID %lu %s\n",
1402 (unsigned long)maps[i].xid.id,
1403 sid_string_static(maps[i].sid));
1404 break;
1405 case ID_TYPE_NOT_SPECIFIED:
1406 break;
1410 fflush(dump);
1411 fclose(dump);
1414 char *idmap_fetch_secret(const char *backend, bool alloc,
1415 const char *domain, const char *identity)
1417 char *tmp, *ret;
1418 int r;
1420 if (alloc) {
1421 r = asprintf(&tmp, "IDMAP_ALLOC_%s", backend);
1422 } else {
1423 r = asprintf(&tmp, "IDMAP_%s_%s", backend, domain);
1426 if (r < 0)
1427 return NULL;
1429 strupper_m(tmp); /* make sure the key is case insensitive */
1430 ret = secrets_fetch_generic(tmp, identity);
1432 SAFE_FREE( tmp );
1434 return ret;