s3-groupdb: fix enum_aliasmem in ldb branch.
[Samba.git] / source / winbindd / idmap.c
blobd601210ecfb59711f36f4221696b62cd760ffb1d
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 struct idmap_backend {
32 const char *name;
33 struct idmap_methods *methods;
34 struct idmap_backend *prev, *next;
37 struct idmap_alloc_backend {
38 const char *name;
39 struct idmap_alloc_methods *methods;
40 struct idmap_alloc_backend *prev, *next;
43 struct idmap_cache_ctx;
45 struct idmap_alloc_context {
46 const char *params;
47 struct idmap_alloc_methods *methods;
48 bool initialized;
51 static TALLOC_CTX *idmap_ctx = NULL;
52 static struct idmap_cache_ctx *idmap_cache;
54 static struct idmap_backend *backends = NULL;
55 static struct idmap_domain **idmap_domains = NULL;
56 static int num_domains = 0;
57 static int pdb_dom_num = -1;
58 static int def_dom_num = -1;
60 static struct idmap_alloc_backend *alloc_backends = NULL;
61 static struct idmap_alloc_context *idmap_alloc_ctx = NULL;
63 #define IDMAP_CHECK_RET(ret) do { \
64 if ( ! NT_STATUS_IS_OK(ret)) { \
65 DEBUG(2, ("ERROR: NTSTATUS = 0x%08x\n", NT_STATUS_V(ret))); \
66 goto done; \
67 } } while(0)
68 #define IDMAP_REPORT_RET(ret) do { \
69 if ( ! NT_STATUS_IS_OK(ret)) { \
70 DEBUG(2, ("ERROR: NTSTATUS = 0x%08x\n", NT_STATUS_V(ret))); \
71 } } while(0)
72 #define IDMAP_CHECK_ALLOC(mem) do { \
73 if (!mem) { \
74 DEBUG(0, ("Out of memory!\n")); ret = NT_STATUS_NO_MEMORY; \
75 goto done; \
76 } } while(0)
78 static struct idmap_methods *get_methods(const char *name)
80 struct idmap_backend *b;
82 for (b = backends; b; b = b->next) {
83 if (strequal(b->name, name)) {
84 return b->methods;
88 return NULL;
91 static struct idmap_alloc_methods *get_alloc_methods(const char *name)
93 struct idmap_alloc_backend *b;
95 for (b = alloc_backends; b; b = b->next) {
96 if (strequal(b->name, name)) {
97 return b->methods;
101 return NULL;
104 bool idmap_is_offline(void)
106 return ( lp_winbind_offline_logon() &&
107 get_global_winbindd_state_offline() );
110 /**********************************************************************
111 Allow a module to register itself as a method.
112 **********************************************************************/
114 NTSTATUS smb_register_idmap(int version, const char *name,
115 struct idmap_methods *methods)
117 struct idmap_methods *test;
118 struct idmap_backend *entry;
120 if (!idmap_ctx) {
121 return NT_STATUS_INTERNAL_DB_ERROR;
124 if ((version != SMB_IDMAP_INTERFACE_VERSION)) {
125 DEBUG(0, ("Failed to register idmap module.\n"
126 "The module was compiled against "
127 "SMB_IDMAP_INTERFACE_VERSION %d,\n"
128 "current SMB_IDMAP_INTERFACE_VERSION is %d.\n"
129 "Please recompile against the current version "
130 "of samba!\n",
131 version, SMB_IDMAP_INTERFACE_VERSION));
132 return NT_STATUS_OBJECT_TYPE_MISMATCH;
135 if (!name || !name[0] || !methods) {
136 DEBUG(0,("Called with NULL pointer or empty name!\n"));
137 return NT_STATUS_INVALID_PARAMETER;
140 test = get_methods(name);
141 if (test) {
142 DEBUG(0,("Idmap module %s already registered!\n", name));
143 return NT_STATUS_OBJECT_NAME_COLLISION;
146 entry = talloc(idmap_ctx, struct idmap_backend);
147 if ( ! entry) {
148 DEBUG(0,("Out of memory!\n"));
149 return NT_STATUS_NO_MEMORY;
151 entry->name = talloc_strdup(idmap_ctx, name);
152 if ( ! entry->name) {
153 DEBUG(0,("Out of memory!\n"));
154 return NT_STATUS_NO_MEMORY;
156 entry->methods = methods;
158 DLIST_ADD(backends, entry);
159 DEBUG(5, ("Successfully added idmap backend '%s'\n", name));
160 return NT_STATUS_OK;
163 /**********************************************************************
164 Allow a module to register itself as a method.
165 **********************************************************************/
167 NTSTATUS smb_register_idmap_alloc(int version, const char *name,
168 struct idmap_alloc_methods *methods)
170 struct idmap_alloc_methods *test;
171 struct idmap_alloc_backend *entry;
173 if (!idmap_ctx) {
174 return NT_STATUS_INTERNAL_DB_ERROR;
177 if ((version != SMB_IDMAP_INTERFACE_VERSION)) {
178 DEBUG(0, ("Failed to register idmap alloc module.\n"
179 "The module was compiled against "
180 "SMB_IDMAP_INTERFACE_VERSION %d,\n"
181 "current SMB_IDMAP_INTERFACE_VERSION is %d.\n"
182 "Please recompile against the current version "
183 "of samba!\n",
184 version, SMB_IDMAP_INTERFACE_VERSION));
185 return NT_STATUS_OBJECT_TYPE_MISMATCH;
188 if (!name || !name[0] || !methods) {
189 DEBUG(0,("Called with NULL pointer or empty name!\n"));
190 return NT_STATUS_INVALID_PARAMETER;
193 test = get_alloc_methods(name);
194 if (test) {
195 DEBUG(0,("idmap_alloc module %s already registered!\n", name));
196 return NT_STATUS_OBJECT_NAME_COLLISION;
199 entry = talloc(idmap_ctx, struct idmap_alloc_backend);
200 if ( ! entry) {
201 DEBUG(0,("Out of memory!\n"));
202 return NT_STATUS_NO_MEMORY;
204 entry->name = talloc_strdup(idmap_ctx, name);
205 if ( ! entry->name) {
206 DEBUG(0,("Out of memory!\n"));
207 return NT_STATUS_NO_MEMORY;
209 entry->methods = methods;
211 DLIST_ADD(alloc_backends, entry);
212 DEBUG(5, ("Successfully added idmap alloc backend '%s'\n", name));
213 return NT_STATUS_OK;
216 static int close_domain_destructor(struct idmap_domain *dom)
218 NTSTATUS ret;
220 ret = dom->methods->close_fn(dom);
221 if (!NT_STATUS_IS_OK(ret)) {
222 DEBUG(3, ("Failed to close idmap domain [%s]!\n", dom->name));
225 return 0;
228 /**************************************************************************
229 Shutdown.
230 **************************************************************************/
232 NTSTATUS idmap_close(void)
234 /* close the alloc backend first before freeing idmap_ctx */
235 if (idmap_alloc_ctx) {
236 idmap_alloc_ctx->methods->close_fn();
237 idmap_alloc_ctx->methods = NULL;
239 alloc_backends = NULL;
241 /* this talloc_free call will fire the talloc destructors
242 * that will free all active backends resources */
243 TALLOC_FREE(idmap_ctx);
244 idmap_cache = NULL;
245 idmap_domains = NULL;
246 backends = NULL;
248 return NT_STATUS_OK;
251 /****************************************************************************
252 ****************************************************************************/
254 NTSTATUS idmap_init_cache(void)
256 /* Always initialize the cache. We'll have to delay initialization
257 of backends if we are offline */
259 if ( idmap_ctx ) {
260 return NT_STATUS_OK;
263 if ( (idmap_ctx = talloc_named_const(NULL, 0, "idmap_ctx")) == NULL ) {
264 return NT_STATUS_NO_MEMORY;
267 if ( (idmap_cache = idmap_cache_init(idmap_ctx)) == NULL ) {
268 return NT_STATUS_UNSUCCESSFUL;
271 return NT_STATUS_OK;
274 /****************************************************************************
275 ****************************************************************************/
277 NTSTATUS idmap_init(void)
279 NTSTATUS ret;
280 static NTSTATUS idmap_init_status = NT_STATUS_UNSUCCESSFUL;
281 struct idmap_domain *dom;
282 char *compat_backend = NULL;
283 char *compat_params = NULL;
284 const char **dom_list = NULL;
285 const char *default_domain = NULL;
286 char *alloc_backend = NULL;
287 bool default_already_defined = False;
288 bool pri_dom_is_in_list = False;
289 int compat = 0;
290 int i;
292 ret = idmap_init_cache();
293 if (!NT_STATUS_IS_OK(ret))
294 return ret;
296 if (NT_STATUS_IS_OK(idmap_init_status)) {
297 return NT_STATUS_OK;
300 /* We can't reliably call intialization code here unless
301 we are online. But return NT_STATUS_OK so the upper
302 level code doesn't abort idmap lookups. */
304 if ( get_global_winbindd_state_offline() ) {
305 idmap_init_status = NT_STATUS_FILE_IS_OFFLINE;
306 return NT_STATUS_OK;
309 static_init_idmap;
311 dom_list = lp_idmap_domains();
313 if ( lp_idmap_backend() ) {
314 const char **compat_list = lp_idmap_backend();
315 char *p = NULL;
316 const char *q = NULL;
318 if ( dom_list ) {
319 DEBUG(0, ("WARNING: idmap backend and idmap domains are"
320 " mutually exclusive!\n"));
321 DEBUGADD(0,("idmap backend option will be IGNORED!\n"));
322 } else {
323 compat = 1;
325 compat_backend = talloc_strdup(idmap_ctx, *compat_list);
327 /* strip any leading idmap_ prefix of */
328 if (strncmp(*compat_list, "idmap_", 6) == 0 ) {
329 q = *compat_list += 6;
330 DEBUG(0, ("WARNING: idmap backend uses obsolete"
331 " and deprecated 'idmap_' prefix.\n"
332 "Please replace 'idmap_%s' by '%s' in"
333 " %s\n", q, q, get_dyn_CONFIGFILE()));
334 compat_backend = talloc_strdup(idmap_ctx, q);
335 } else {
336 compat_backend = talloc_strdup(idmap_ctx,
337 *compat_list);
340 if (compat_backend == NULL ) {
341 ret = NT_STATUS_NO_MEMORY;
342 goto done;
345 /* separate the backend and module arguements */
346 if ((p = strchr(compat_backend, ':')) != NULL) {
347 *p = '\0';
348 compat_params = p + 1;
351 } else if ( !dom_list ) {
352 /* Back compatible: without idmap domains and explicit
353 idmap backend. Taking default idmap backend: tdb */
355 compat = 1;
356 compat_backend = talloc_strdup( idmap_ctx, "tdb");
357 compat_params = compat_backend;
360 if ( ! dom_list) {
361 /* generate a list with our main domain */
362 const char ** dl;
364 dl = talloc_array(idmap_ctx, const char *, 2);
365 if (dl == NULL) {
366 ret = NT_STATUS_NO_MEMORY;
367 goto done;
369 dl[0] = talloc_strdup(dl, lp_workgroup());
370 if (dl[0] == NULL) {
371 ret = NT_STATUS_NO_MEMORY;
372 goto done;
375 /* terminate */
376 dl[1] = NULL;
378 dom_list = dl;
379 default_domain = dl[0];
382 /***************************
383 * initialize idmap domains
385 DEBUG(1, ("Initializing idmap domains\n"));
387 for (i=0, num_domains=0; dom_list[i]; i++) {
388 const char *parm_backend;
389 char *config_option;
391 /* ignore BUILTIN and local MACHINE domains */
392 if (strequal(dom_list[i], "BUILTIN")
393 || strequal(dom_list[i], get_global_sam_name()))
395 DEBUG(0,("idmap_init: Ignoring domain %s\n",
396 dom_list[i]));
397 continue;
400 if ((dom_list[i] != default_domain) &&
401 strequal(dom_list[i], lp_workgroup())) {
402 pri_dom_is_in_list = True;
404 /* init domain */
406 dom = TALLOC_ZERO_P(idmap_ctx, struct idmap_domain);
407 IDMAP_CHECK_ALLOC(dom);
409 dom->name = talloc_strdup(dom, dom_list[i]);
410 IDMAP_CHECK_ALLOC(dom->name);
412 config_option = talloc_asprintf(dom, "idmap config %s",
413 dom->name);
414 IDMAP_CHECK_ALLOC(config_option);
416 /* default or specific ? */
418 dom->default_domain = lp_parm_bool(-1, config_option,
419 "default", False);
421 if (dom->default_domain ||
422 (default_domain && strequal(dom_list[i], default_domain))) {
424 /* make sure this is set even when we match
425 * default_domain */
426 dom->default_domain = True;
428 if (default_already_defined) {
429 DEBUG(1, ("ERROR: Multiple domains defined as"
430 " default!\n"));
431 ret = NT_STATUS_INVALID_PARAMETER;
432 goto done;
435 default_already_defined = True;
439 dom->readonly = lp_parm_bool(-1, config_option,
440 "readonly", False);
442 /* find associated backend (default: tdb) */
443 if (compat) {
444 parm_backend = talloc_strdup(idmap_ctx, compat_backend);
445 } else {
446 parm_backend = talloc_strdup(idmap_ctx,
447 lp_parm_const_string(
448 -1, config_option,
449 "backend", "tdb"));
451 IDMAP_CHECK_ALLOC(parm_backend);
453 /* get the backend methods for this domain */
454 dom->methods = get_methods(parm_backend);
456 if ( ! dom->methods) {
457 ret = smb_probe_module("idmap", parm_backend);
458 if (NT_STATUS_IS_OK(ret)) {
459 dom->methods = get_methods(parm_backend);
462 if ( ! dom->methods) {
463 DEBUG(0, ("ERROR: Could not get methods for "
464 "backend %s\n", parm_backend));
465 ret = NT_STATUS_UNSUCCESSFUL;
466 goto done;
469 /* check the set_mapping function exists otherwise mark the
470 * module as readonly */
471 if ( ! dom->methods->set_mapping) {
472 DEBUG(5, ("Forcing to readonly, as this module can't"
473 " store arbitrary mappings.\n"));
474 dom->readonly = True;
477 /* now that we have methods,
478 * set the destructor for this domain */
479 talloc_set_destructor(dom, close_domain_destructor);
481 if (compat_params) {
482 dom->params = talloc_strdup(dom, compat_params);
483 IDMAP_CHECK_ALLOC(dom->params);
484 } else {
485 dom->params = NULL;
488 /* Finally instance a backend copy for this domain */
489 ret = dom->methods->init(dom);
490 if ( ! NT_STATUS_IS_OK(ret)) {
491 DEBUG(0, ("ERROR: Initialization failed for backend "
492 "%s (domain %s), deferred!\n",
493 parm_backend, dom->name));
495 idmap_domains = talloc_realloc(idmap_ctx, idmap_domains,
496 struct idmap_domain *, i+1);
497 if ( ! idmap_domains) {
498 DEBUG(0, ("Out of memory!\n"));
499 ret = NT_STATUS_NO_MEMORY;
500 goto done;
502 idmap_domains[num_domains] = dom;
504 /* save default domain position for future uses */
505 if (dom->default_domain) {
506 def_dom_num = num_domains;
509 /* Bump counter to next available slot */
511 num_domains++;
513 DEBUG(10, ("Domain %s - Backend %s - %sdefault - %sreadonly\n",
514 dom->name, parm_backend,
515 dom->default_domain?"":"not ",
516 dom->readonly?"":"not "));
518 talloc_free(config_option);
521 /* on DCs we need to add idmap_tdb as the default backend if compat is
522 * defined (when the old implicit configuration is used)
523 * This is not done in the previous loop a on member server we exclude
524 * the local domain. But on a DC the local domain is the only domain
525 * available therefore we are left with no default domain */
526 if (((lp_server_role() == ROLE_DOMAIN_PDC) ||
527 (lp_server_role() == ROLE_DOMAIN_BDC)) &&
528 ((num_domains == 0) && (compat == 1))) {
530 dom = TALLOC_ZERO_P(idmap_ctx, struct idmap_domain);
531 IDMAP_CHECK_ALLOC(dom);
533 dom->name = talloc_strdup(dom, "__default__");
534 IDMAP_CHECK_ALLOC(dom->name);
536 dom->default_domain = True;
537 dom->readonly = False;
539 /* get the backend methods for this domain */
540 dom->methods = get_methods(compat_backend);
541 if ( ! dom->methods) {
542 ret = smb_probe_module("idmap", compat_backend);
543 if (NT_STATUS_IS_OK(ret)) {
544 dom->methods = get_methods(compat_backend);
547 if ( ! dom->methods) {
548 DEBUG(0, ("ERROR: Could not get methods for "
549 "backend %s\n", compat_backend));
550 ret = NT_STATUS_UNSUCCESSFUL;
551 goto done;
554 /* now that we have methods,
555 * set the destructor for this domain */
556 talloc_set_destructor(dom, close_domain_destructor);
558 dom->params = talloc_strdup(dom, compat_params);
559 IDMAP_CHECK_ALLOC(dom->params);
561 /* Finally instance a backend copy for this domain */
562 ret = dom->methods->init(dom);
563 if ( ! NT_STATUS_IS_OK(ret)) {
564 DEBUG(0, ("ERROR: Initialization failed for backend "
565 "%s (domain %s), deferred!\n",
566 compat_backend, dom->name));
568 idmap_domains = talloc_realloc(idmap_ctx, idmap_domains,
569 struct idmap_domain *, 2);
570 if ( ! idmap_domains) {
571 DEBUG(0, ("Out of memory!\n"));
572 ret = NT_STATUS_NO_MEMORY;
573 goto done;
575 idmap_domains[num_domains] = dom;
577 def_dom_num = num_domains;
579 /* Bump counter to next available slot */
581 num_domains++;
583 DEBUG(10, ("Domain %s - Backend %s - %sdefault - %sreadonly\n",
584 dom->name, compat_backend,
585 dom->default_domain?"":"not ",
586 dom->readonly?"":"not "));
589 /* automatically add idmap_nss backend if needed */
590 if ((lp_server_role() == ROLE_DOMAIN_MEMBER) &&
591 ( ! pri_dom_is_in_list) &&
592 lp_winbind_trusted_domains_only()) {
594 dom = TALLOC_ZERO_P(idmap_ctx, struct idmap_domain);
595 IDMAP_CHECK_ALLOC(dom);
597 dom->name = talloc_strdup(dom, lp_workgroup());
598 IDMAP_CHECK_ALLOC(dom->name);
600 dom->default_domain = False;
601 dom->readonly = True;
603 /* get the backend methods for nss */
604 dom->methods = get_methods("nss");
606 /* (the nss module is always statically linked) */
607 if ( ! dom->methods) {
608 DEBUG(0, ("ERROR: No methods for idmap_nss ?!\n"));
609 ret = NT_STATUS_UNSUCCESSFUL;
610 goto done;
613 /* now that we have methods,
614 * set the destructor for this domain */
615 talloc_set_destructor(dom, close_domain_destructor);
617 if (compat_params) {
618 dom->params = talloc_strdup(dom, compat_params);
619 IDMAP_CHECK_ALLOC(dom->params);
620 } else {
621 dom->params = NULL;
624 /* Finally instance a backend copy for this domain */
625 ret = dom->methods->init(dom);
626 if ( ! NT_STATUS_IS_OK(ret)) {
627 DEBUG(0, ("ERROR: Init. failed for idmap_nss ?!\n"));
628 ret = NT_STATUS_UNSUCCESSFUL;
629 goto done;
632 idmap_domains = talloc_realloc(idmap_ctx,
633 idmap_domains,
634 struct idmap_domain *,
635 num_domains+1);
636 if ( ! idmap_domains) {
637 DEBUG(0, ("Out of memory!\n"));
638 ret = NT_STATUS_NO_MEMORY;
639 goto done;
641 idmap_domains[num_domains] = dom;
643 DEBUG(10, ("Domain %s - Backend nss - not default - readonly\n",
644 dom->name ));
646 num_domains++;
649 /**** automatically add idmap_passdb backend ****/
650 dom = TALLOC_ZERO_P(idmap_ctx, struct idmap_domain);
651 IDMAP_CHECK_ALLOC(dom);
653 dom->name = talloc_strdup(dom, get_global_sam_name());
654 IDMAP_CHECK_ALLOC(dom->name);
656 dom->default_domain = False;
657 dom->readonly = True;
659 /* get the backend methods for passdb */
660 dom->methods = get_methods("passdb");
662 /* (the passdb module is always statically linked) */
663 if ( ! dom->methods) {
664 DEBUG(0, ("ERROR: No methods for idmap_passdb ?!\n"));
665 ret = NT_STATUS_UNSUCCESSFUL;
666 goto done;
669 /* now that we have methods, set the destructor for this domain */
670 talloc_set_destructor(dom, close_domain_destructor);
672 if (compat_params) {
673 dom->params = talloc_strdup(dom, compat_params);
674 IDMAP_CHECK_ALLOC(dom->params);
675 } else {
676 dom->params = NULL;
679 /* Finally instance a backend copy for this domain */
680 ret = dom->methods->init(dom);
681 if ( ! NT_STATUS_IS_OK(ret)) {
682 DEBUG(0, ("ERROR: Init. failed for idmap_passdb ?!\n"));
683 ret = NT_STATUS_UNSUCCESSFUL;
684 goto done;
687 idmap_domains = talloc_realloc(idmap_ctx,
688 idmap_domains,
689 struct idmap_domain *,
690 num_domains+1);
691 if ( ! idmap_domains) {
692 DEBUG(0, ("Out of memory!\n"));
693 ret = NT_STATUS_NO_MEMORY;
694 goto done;
696 idmap_domains[num_domains] = dom;
698 /* needed to handle special BUILTIN and wellknown SIDs cases */
699 pdb_dom_num = num_domains;
701 DEBUG(10, ("Domain %s - Backend passdb - not default - readonly\n",
702 dom->name));
704 num_domains++;
705 /**** finished adding idmap_passdb backend ****/
707 /* sort domains so that the default is the last one */
708 /* don't sort if no default domain defined */
709 if (def_dom_num != -1 && def_dom_num != num_domains-1) {
710 /* default is not last, move it */
711 struct idmap_domain *tmp;
713 if (pdb_dom_num > def_dom_num) {
714 pdb_dom_num --;
716 } else if (pdb_dom_num == def_dom_num) { /* ?? */
717 pdb_dom_num = num_domains - 1;
720 tmp = idmap_domains[def_dom_num];
722 for (i = def_dom_num; i < num_domains-1; i++) {
723 idmap_domains[i] = idmap_domains[i+1];
725 idmap_domains[i] = tmp;
726 def_dom_num = i;
730 /* Initialize alloc module */
732 DEBUG(3, ("Initializing idmap alloc module\n"));
734 alloc_backend = NULL;
735 if (compat) {
736 alloc_backend = talloc_strdup(idmap_ctx, compat_backend);
737 } else {
738 char *ab = lp_idmap_alloc_backend();
740 if (ab && (ab[0] != '\0')) {
741 alloc_backend = talloc_strdup(idmap_ctx,
742 lp_idmap_alloc_backend());
746 if ( alloc_backend ) {
748 idmap_alloc_ctx = TALLOC_ZERO_P(idmap_ctx,
749 struct idmap_alloc_context);
750 IDMAP_CHECK_ALLOC(idmap_alloc_ctx);
752 idmap_alloc_ctx->methods = get_alloc_methods(alloc_backend);
753 if ( ! idmap_alloc_ctx->methods) {
754 ret = smb_probe_module("idmap", alloc_backend);
755 if (NT_STATUS_IS_OK(ret)) {
756 idmap_alloc_ctx->methods =
757 get_alloc_methods(alloc_backend);
760 if (idmap_alloc_ctx->methods) {
762 if (compat_params) {
763 idmap_alloc_ctx->params =
764 talloc_strdup(idmap_alloc_ctx,
765 compat_params);
766 IDMAP_CHECK_ALLOC(idmap_alloc_ctx->params);
767 } else {
768 idmap_alloc_ctx->params = NULL;
771 ret = idmap_alloc_ctx->methods->init(idmap_alloc_ctx->params);
772 if ( ! NT_STATUS_IS_OK(ret)) {
773 DEBUG(0, ("ERROR: Initialization failed for "
774 "alloc backend %s, deferred!\n",
775 alloc_backend));
776 } else {
777 idmap_alloc_ctx->initialized = True;
779 } else {
780 DEBUG(2, ("idmap_init: Unable to get methods for "
781 "alloc backend %s\n",
782 alloc_backend));
783 /* certain compat backends are just readonly */
784 if ( compat ) {
785 TALLOC_FREE(idmap_alloc_ctx);
786 ret = NT_STATUS_OK;
787 } else {
788 ret = NT_STATUS_UNSUCCESSFUL;
793 /* cleanup temporary strings */
794 TALLOC_FREE( compat_backend );
796 idmap_init_status = NT_STATUS_OK;
798 return ret;
800 done:
801 DEBUG(0, ("Aborting IDMAP Initialization ...\n"));
802 idmap_close();
804 return ret;
807 static NTSTATUS idmap_alloc_init(void)
809 NTSTATUS ret;
811 if (! NT_STATUS_IS_OK(ret = idmap_init())) {
812 return ret;
815 if ( ! idmap_alloc_ctx) {
816 return NT_STATUS_NOT_SUPPORTED;
819 if ( ! idmap_alloc_ctx->initialized) {
820 ret = idmap_alloc_ctx->methods->init(idmap_alloc_ctx->params);
821 if ( ! NT_STATUS_IS_OK(ret)) {
822 DEBUG(0, ("ERROR: Initialization failed for alloc "
823 "backend, deferred!\n"));
824 return ret;
825 } else {
826 idmap_alloc_ctx->initialized = True;
830 return NT_STATUS_OK;
833 /**************************************************************************
834 idmap allocator interface functions
835 **************************************************************************/
837 NTSTATUS idmap_allocate_uid(struct unixid *id)
839 NTSTATUS ret;
841 if (! NT_STATUS_IS_OK(ret = idmap_alloc_init())) {
842 return ret;
845 id->type = ID_TYPE_UID;
846 return idmap_alloc_ctx->methods->allocate_id(id);
849 NTSTATUS idmap_allocate_gid(struct unixid *id)
851 NTSTATUS ret;
853 if (! NT_STATUS_IS_OK(ret = idmap_alloc_init())) {
854 return ret;
857 id->type = ID_TYPE_GID;
858 return idmap_alloc_ctx->methods->allocate_id(id);
861 NTSTATUS idmap_set_uid_hwm(struct unixid *id)
863 NTSTATUS ret;
865 if (! NT_STATUS_IS_OK(ret = idmap_alloc_init())) {
866 return ret;
869 id->type = ID_TYPE_UID;
870 return idmap_alloc_ctx->methods->set_id_hwm(id);
873 NTSTATUS idmap_set_gid_hwm(struct unixid *id)
875 NTSTATUS ret;
877 if (! NT_STATUS_IS_OK(ret = idmap_alloc_init())) {
878 return ret;
881 id->type = ID_TYPE_GID;
882 return idmap_alloc_ctx->methods->set_id_hwm(id);
885 /******************************************************************************
886 Lookup an idmap_domain give a full user or group SID
887 ******************************************************************************/
889 static struct idmap_domain* find_idmap_domain_from_sid( DOM_SID *account_sid )
891 DOM_SID domain_sid;
892 uint32_t rid;
893 struct winbindd_domain *domain = NULL;
894 int i;
896 /* 1. Handle BUILTIN or Special SIDs and prevent them from
897 falling into the default domain space (if we have a
898 configured passdb backend. */
900 if ( (pdb_dom_num != -1) &&
901 (sid_check_is_in_builtin(account_sid) ||
902 sid_check_is_in_wellknown_domain(account_sid) ||
903 sid_check_is_in_unix_groups(account_sid) ||
904 sid_check_is_in_unix_users(account_sid)) )
906 return idmap_domains[pdb_dom_num];
909 /* 2. Lookup the winbindd_domain from the account_sid */
911 sid_copy( &domain_sid, account_sid );
912 sid_split_rid( &domain_sid, &rid );
913 domain = find_domain_from_sid_noinit( &domain_sid );
915 for (i = 0; domain && i < num_domains; i++) {
916 if ( strequal( idmap_domains[i]->name, domain->name ) ) {
917 return idmap_domains[i];
921 /* 3. Fall back to the default domain */
923 if ( def_dom_num != -1 ) {
924 return idmap_domains[def_dom_num];
927 return NULL;
930 /******************************************************************************
931 Lookup an index given an idmap_domain pointer
932 ******************************************************************************/
934 static uint32_t find_idmap_domain_index( struct idmap_domain *id_domain)
936 int i;
938 for (i = 0; i < num_domains; i++) {
939 if ( idmap_domains[i] == id_domain )
940 return i;
943 return -1;
947 /*********************************************************
948 Check if creating a mapping is permitted for the domain
949 *********************************************************/
951 static NTSTATUS idmap_can_map(const struct id_map *map,
952 struct idmap_domain **ret_dom)
954 struct idmap_domain *dom;
956 /* Check we do not create mappings for our own local domain,
957 * or BUILTIN or special SIDs */
958 if ((sid_compare_domain(map->sid, get_global_sam_sid()) == 0) ||
959 sid_check_is_in_builtin(map->sid) ||
960 sid_check_is_in_wellknown_domain(map->sid) ||
961 sid_check_is_in_unix_users(map->sid) ||
962 sid_check_is_in_unix_groups(map->sid) )
964 DEBUG(10, ("We are not supposed to create mappings for our own "
965 "domains (local, builtin, specials)\n"));
966 return NT_STATUS_UNSUCCESSFUL;
969 /* Special check for trusted domain only = Yes */
970 if (lp_winbind_trusted_domains_only()) {
971 struct winbindd_domain *wdom = find_our_domain();
972 if (wdom && (sid_compare_domain(map->sid, &wdom->sid) == 0)) {
973 DEBUG(10, ("We are not supposed to create mappings for "
974 "our primary domain when <trusted domain "
975 "only> is True\n"));
976 DEBUGADD(10, ("Leave [%s] unmapped\n",
977 sid_string_dbg(map->sid)));
978 return NT_STATUS_UNSUCCESSFUL;
982 if ( (dom = find_idmap_domain_from_sid( map->sid )) == NULL ) {
983 /* huh, couldn't find a suitable domain,
984 * let's just leave it unmapped */
985 DEBUG(10, ("Could not find idmap backend for SID %s\n",
986 sid_string_dbg(map->sid)));
987 return NT_STATUS_NO_SUCH_DOMAIN;
990 if (dom->readonly) {
991 /* ouch the domain is read only,
992 * let's just leave it unmapped */
993 DEBUG(10, ("idmap backend for SID %s is READONLY!\n",
994 sid_string_dbg(map->sid)));
995 return NT_STATUS_UNSUCCESSFUL;
998 *ret_dom = dom;
999 return NT_STATUS_OK;
1002 static NTSTATUS idmap_new_mapping(TALLOC_CTX *ctx, struct id_map *map)
1004 NTSTATUS ret;
1005 struct idmap_domain *dom;
1007 /* If we are offline we cannot lookup SIDs, deny mapping */
1008 if (idmap_is_offline()) {
1009 return NT_STATUS_FILE_IS_OFFLINE;
1012 ret = idmap_can_map(map, &dom);
1013 if ( ! NT_STATUS_IS_OK(ret)) {
1014 return NT_STATUS_NONE_MAPPED;
1017 /* check if this is a valid SID and then map it */
1018 switch (map->xid.type) {
1019 case ID_TYPE_UID:
1020 ret = idmap_allocate_uid(&map->xid);
1021 if ( ! NT_STATUS_IS_OK(ret)) {
1022 /* can't allocate id, let's just leave it unmapped */
1023 DEBUG(2, ("uid allocation failed! "
1024 "Can't create mapping\n"));
1025 return NT_STATUS_NONE_MAPPED;
1027 break;
1028 case ID_TYPE_GID:
1029 ret = idmap_allocate_gid(&map->xid);
1030 if ( ! NT_STATUS_IS_OK(ret)) {
1031 /* can't allocate id, let's just leave it unmapped */
1032 DEBUG(2, ("gid allocation failed! "
1033 "Can't create mapping\n"));
1034 return NT_STATUS_NONE_MAPPED;
1036 break;
1037 default:
1038 /* invalid sid, let's just leave it unmapped */
1039 DEBUG(3,("idmap_new_mapping: Refusing to create a "
1040 "mapping for an unspecified ID type.\n"));
1041 return NT_STATUS_NONE_MAPPED;
1044 /* ok, got a new id, let's set a mapping */
1045 map->status = ID_MAPPED;
1047 DEBUG(10, ("Setting mapping: %s <-> %s %lu\n",
1048 sid_string_dbg(map->sid),
1049 (map->xid.type == ID_TYPE_UID) ? "UID" : "GID",
1050 (unsigned long)map->xid.id));
1051 ret = dom->methods->set_mapping(dom, map);
1053 if ( ! NT_STATUS_IS_OK(ret)) {
1054 /* something wrong here :-( */
1055 DEBUG(2, ("Failed to commit mapping\n!"));
1057 /* TODO: would it make sense to have an "unalloc_id function?" */
1059 return NT_STATUS_NONE_MAPPED;
1062 return NT_STATUS_OK;
1065 static NTSTATUS idmap_backends_set_mapping(const struct id_map *map)
1067 struct idmap_domain *dom;
1068 NTSTATUS ret;
1070 DEBUG(10, ("Setting mapping %s <-> %s %lu\n",
1071 sid_string_dbg(map->sid),
1072 (map->xid.type == ID_TYPE_UID) ? "UID" : "GID",
1073 (unsigned long)map->xid.id));
1075 ret = idmap_can_map(map, &dom);
1076 if ( ! NT_STATUS_IS_OK(ret)) {
1077 return ret;
1080 DEBUG(10,("set_mapping for domain %s\n", dom->name ));
1082 return dom->methods->set_mapping(dom, map);
1085 static NTSTATUS idmap_backends_unixids_to_sids(struct id_map **ids)
1087 struct idmap_domain *dom;
1088 struct id_map **unmapped;
1089 struct id_map **_ids;
1090 TALLOC_CTX *ctx;
1091 NTSTATUS ret;
1092 int i, u, n;
1094 if (!ids || !*ids) {
1095 DEBUG(1, ("Invalid list of maps\n"));
1096 return NT_STATUS_INVALID_PARAMETER;
1099 ctx = talloc_named_const(NULL, 0, "idmap_backends_unixids_to_sids ctx");
1100 if ( ! ctx) {
1101 DEBUG(0, ("Out of memory!\n"));
1102 return NT_STATUS_NO_MEMORY;
1105 DEBUG(10, ("Query backends to map ids->sids\n"));
1107 /* start from the default (the last one) and then if there are still
1108 * unmapped entries cycle through the others */
1110 _ids = ids;
1112 unmapped = NULL;
1113 for (n = num_domains-1; n >= 0; n--) { /* cycle backwards */
1115 dom = idmap_domains[n];
1117 DEBUG(10, ("Query sids from domain %s\n", dom->name));
1119 ret = dom->methods->unixids_to_sids(dom, _ids);
1120 IDMAP_REPORT_RET(ret);
1122 unmapped = NULL;
1124 for (i = 0, u = 0; _ids[i]; i++) {
1125 if (_ids[i]->status != ID_MAPPED) {
1126 unmapped = talloc_realloc(ctx, unmapped,
1127 struct id_map *, u + 2);
1128 IDMAP_CHECK_ALLOC(unmapped);
1129 unmapped[u] = _ids[i];
1130 u++;
1133 if (unmapped) {
1134 /* terminate the unmapped list */
1135 unmapped[u] = NULL;
1136 } else { /* no more entries, get out */
1137 break;
1140 _ids = unmapped;
1144 if (unmapped) {
1145 /* there are still unmapped ids,
1146 * map them to the unix users/groups domains */
1147 /* except for expired entries,
1148 * these will be returned as valid (offline mode) */
1149 for (i = 0; unmapped[i]; i++) {
1150 if (unmapped[i]->status == ID_EXPIRED) continue;
1151 switch (unmapped[i]->xid.type) {
1152 case ID_TYPE_UID:
1153 uid_to_unix_users_sid(
1154 (uid_t)unmapped[i]->xid.id,
1155 unmapped[i]->sid);
1156 unmapped[i]->status = ID_MAPPED;
1157 break;
1158 case ID_TYPE_GID:
1159 gid_to_unix_groups_sid(
1160 (gid_t)unmapped[i]->xid.id,
1161 unmapped[i]->sid);
1162 unmapped[i]->status = ID_MAPPED;
1163 break;
1164 default: /* what?! */
1165 unmapped[i]->status = ID_UNKNOWN;
1166 break;
1171 ret = NT_STATUS_OK;
1173 done:
1174 talloc_free(ctx);
1175 return ret;
1178 static NTSTATUS idmap_backends_sids_to_unixids(struct id_map **ids)
1180 struct id_map ***dom_ids;
1181 struct idmap_domain *dom;
1182 TALLOC_CTX *ctx;
1183 NTSTATUS ret;
1184 int i, *counters;
1186 if ( (ctx = talloc_named_const(NULL, 0, "be_sids_to_ids")) == NULL ) {
1187 DEBUG(1, ("failed to allocate talloc context, OOM?\n"));
1188 return NT_STATUS_NO_MEMORY;
1191 DEBUG(10, ("Query backends to map sids->ids\n"));
1193 /* split list per domain */
1194 if (num_domains == 0) {
1195 DEBUG(1, ("No domains available?\n"));
1196 return NT_STATUS_UNSUCCESSFUL;
1199 dom_ids = TALLOC_ZERO_ARRAY(ctx, struct id_map **, num_domains);
1200 IDMAP_CHECK_ALLOC(dom_ids);
1201 counters = TALLOC_ZERO_ARRAY(ctx, int, num_domains);
1202 IDMAP_CHECK_ALLOC(counters);
1204 /* partition the requests by domain */
1206 for (i = 0; ids[i]; i++) {
1207 uint32_t idx;
1209 if ((dom = find_idmap_domain_from_sid(ids[i]->sid)) == NULL) {
1210 /* no available idmap_domain. Move on */
1211 continue;
1214 DEBUG(10,("SID %s is being handled by %s\n",
1215 sid_string_dbg(ids[i]->sid),
1216 dom ? dom->name : "none" ));
1218 idx = find_idmap_domain_index( dom );
1219 SMB_ASSERT( idx != -1 );
1221 dom_ids[idx] = talloc_realloc(ctx, dom_ids[idx],
1222 struct id_map *,
1223 counters[idx] + 2);
1224 IDMAP_CHECK_ALLOC(dom_ids[idx]);
1226 dom_ids[idx][counters[idx]] = ids[i];
1227 counters[idx]++;
1228 dom_ids[idx][counters[idx]] = NULL;
1231 /* All the ids have been dispatched in the right queues.
1232 Let's cycle through the filled ones */
1234 for (i = 0; i < num_domains; i++) {
1235 if (dom_ids[i]) {
1236 dom = idmap_domains[i];
1237 DEBUG(10, ("Query ids from domain %s\n", dom->name));
1238 ret = dom->methods->sids_to_unixids(dom, dom_ids[i]);
1239 IDMAP_REPORT_RET(ret);
1243 /* ok all the backends have been contacted at this point */
1244 /* let's see if we have any unmapped SID left and act accordingly */
1246 for (i = 0; ids[i]; i++) {
1247 /* NOTE: this will NOT touch ID_EXPIRED entries that the backend
1248 * was not able to confirm/deny (offline mode) */
1249 if (ids[i]->status == ID_UNKNOWN ||
1250 ids[i]->status == ID_UNMAPPED) {
1251 /* ok this is an unmapped one, see if we can map it */
1252 ret = idmap_new_mapping(ctx, ids[i]);
1253 if (NT_STATUS_IS_OK(ret)) {
1254 /* successfully mapped */
1255 ids[i]->status = ID_MAPPED;
1256 } else
1257 if (NT_STATUS_EQUAL(ret, NT_STATUS_NONE_MAPPED)) {
1258 /* could not map it */
1259 ids[i]->status = ID_UNMAPPED;
1260 } else {
1261 /* Something very bad happened down there
1262 * OR we are offline */
1263 ids[i]->status = ID_UNKNOWN;
1268 ret = NT_STATUS_OK;
1270 done:
1271 talloc_free(ctx);
1272 return ret;
1275 /**************************************************************************
1276 idmap interface functions
1277 **************************************************************************/
1279 NTSTATUS idmap_unixids_to_sids(struct id_map **ids)
1281 TALLOC_CTX *ctx;
1282 NTSTATUS ret;
1283 struct id_map **bids;
1284 int i, bi;
1285 int bn = 0;
1286 struct winbindd_domain *our_domain = find_our_domain();
1288 if (! NT_STATUS_IS_OK(ret = idmap_init())) {
1289 return ret;
1292 if (!ids || !*ids) {
1293 DEBUG(1, ("Invalid list of maps\n"));
1294 return NT_STATUS_INVALID_PARAMETER;
1297 ctx = talloc_named_const(NULL, 0, "idmap_unixids_to_sids ctx");
1298 if ( ! ctx) {
1299 DEBUG(1, ("failed to allocate talloc context, OOM?\n"));
1300 return NT_STATUS_NO_MEMORY;
1303 /* no ids to be asked to the backends by default */
1304 bids = NULL;
1305 bi = 0;
1307 for (i = 0; ids[i]; i++) {
1309 if ( ! ids[i]->sid) {
1310 DEBUG(1, ("invalid null SID in id_map array"));
1311 talloc_free(ctx);
1312 return NT_STATUS_INVALID_PARAMETER;
1315 ret = idmap_cache_map_id(idmap_cache, ids[i]);
1317 if ( ! NT_STATUS_IS_OK(ret)) {
1319 if ( ! bids) {
1320 /* alloc space for ids to be resolved by
1321 * backends (realloc ten by ten) */
1322 bids = TALLOC_ARRAY(ctx, struct id_map *, 10);
1323 if ( ! bids) {
1324 DEBUG(1, ("Out of memory!\n"));
1325 talloc_free(ctx);
1326 return NT_STATUS_NO_MEMORY;
1328 bn = 10;
1331 /* add this id to the ones to be retrieved
1332 * from the backends */
1333 bids[bi] = ids[i];
1334 bi++;
1336 /* check if we need to allocate new space
1337 * on the rids array */
1338 if (bi == bn) {
1339 bn += 10;
1340 bids = talloc_realloc(ctx, bids,
1341 struct id_map *, bn);
1342 if ( ! bids) {
1343 DEBUG(1, ("Out of memory!\n"));
1344 talloc_free(ctx);
1345 return NT_STATUS_NO_MEMORY;
1349 /* make sure the last element is NULL */
1350 bids[bi] = NULL;
1354 /* let's see if there is any id mapping to be retieved
1355 * from the backends */
1356 if (bids) {
1357 bool online;
1359 /* Only do query if we are online */
1360 online = !IS_DOMAIN_OFFLINE(our_domain);
1361 if (online) {
1362 ret = idmap_backends_unixids_to_sids(bids);
1363 IDMAP_CHECK_RET(ret);
1366 /* update the cache */
1367 for (i = 0; i < bi; i++) {
1368 if (bids[i]->status == ID_MAPPED) {
1369 ret = idmap_cache_set(idmap_cache, bids[i]);
1370 } else if (bids[i]->status == ID_EXPIRED) {
1371 /* the cache returned an expired entry and the
1372 * backend was not able to clear the situation
1373 * (offline). This handles a previous
1374 * NT_STATUS_SYNCHRONIZATION_REQUIRED
1375 * for disconnected mode, */
1376 bids[i]->status = ID_MAPPED;
1377 } else if (bids[i]->status == ID_UNKNOWN) {
1378 /* something bad here. We were not able to
1379 * handle this for some reason, mark it as
1380 * unmapped and hope next time things will
1381 * settle down. */
1382 bids[i]->status = ID_UNMAPPED;
1383 } else if (online) { /* unmapped */
1384 ret = idmap_cache_set_negative_id(idmap_cache,
1385 bids[i]);
1387 IDMAP_CHECK_RET(ret);
1391 ret = NT_STATUS_OK;
1392 done:
1393 talloc_free(ctx);
1394 return ret;
1397 NTSTATUS idmap_sids_to_unixids(struct id_map **ids)
1399 TALLOC_CTX *ctx;
1400 NTSTATUS ret;
1401 struct id_map **bids;
1402 int i, bi;
1403 int bn = 0;
1404 struct winbindd_domain *our_domain = find_our_domain();
1406 if (! NT_STATUS_IS_OK(ret = idmap_init())) {
1407 return ret;
1410 if (!ids || !*ids) {
1411 DEBUG(1, ("Invalid list of maps\n"));
1412 return NT_STATUS_INVALID_PARAMETER;
1415 ctx = talloc_named_const(NULL, 0, "idmap_sids_to_unixids ctx");
1416 if ( ! ctx) {
1417 DEBUG(1, ("failed to allocate talloc context, OOM?\n"));
1418 return NT_STATUS_NO_MEMORY;
1421 /* no ids to be asked to the backends by default */
1422 bids = NULL;
1423 bi = 0;
1425 for (i = 0; ids[i]; i++) {
1427 if ( ! ids[i]->sid) {
1428 DEBUG(1, ("invalid null SID in id_map array\n"));
1429 talloc_free(ctx);
1430 return NT_STATUS_INVALID_PARAMETER;
1433 ret = idmap_cache_map_sid(idmap_cache, ids[i]);
1435 if ( ! NT_STATUS_IS_OK(ret)) {
1437 if ( ! bids) {
1438 /* alloc space for ids to be resolved
1439 by backends (realloc ten by ten) */
1440 bids = TALLOC_ARRAY(ctx, struct id_map *, 10);
1441 if ( ! bids) {
1442 DEBUG(1, ("Out of memory!\n"));
1443 talloc_free(ctx);
1444 return NT_STATUS_NO_MEMORY;
1446 bn = 10;
1449 /* add this id to the ones to be retrieved
1450 * from the backends */
1451 bids[bi] = ids[i];
1452 bi++;
1454 /* check if we need to allocate new space
1455 * on the ids array */
1456 if (bi == bn) {
1457 bn += 10;
1458 bids = talloc_realloc(ctx, bids,
1459 struct id_map *, bn);
1460 if ( ! bids) {
1461 DEBUG(1, ("Out of memory!\n"));
1462 talloc_free(ctx);
1463 return NT_STATUS_NO_MEMORY;
1467 /* make sure the last element is NULL */
1468 bids[bi] = NULL;
1472 /* let's see if there is any id mapping to be retieved
1473 * from the backends */
1474 if (bids) {
1475 bool online;
1477 /* Only do query if we are online */
1478 online = !IS_DOMAIN_OFFLINE(our_domain);
1479 if (online) {
1480 ret = idmap_backends_sids_to_unixids(bids);
1481 IDMAP_CHECK_RET(ret);
1484 /* update the cache */
1485 for (i = 0; bids[i]; i++) {
1486 if (bids[i]->status == ID_MAPPED) {
1487 ret = idmap_cache_set(idmap_cache, bids[i]);
1488 } else if (bids[i]->status == ID_EXPIRED) {
1489 /* the cache returned an expired entry and the
1490 * backend was not able to clear the situation
1491 * (offline). This handles a previous
1492 * NT_STATUS_SYNCHRONIZATION_REQUIRED
1493 * for disconnected mode, */
1494 bids[i]->status = ID_MAPPED;
1495 } else if (bids[i]->status == ID_UNKNOWN) {
1496 /* something bad here. We were not able to
1497 * handle this for some reason, mark it as
1498 * unmapped and hope next time things will
1499 * settle down. */
1500 bids[i]->status = ID_UNMAPPED;
1501 } else if (online) { /* unmapped */
1502 ret = idmap_cache_set_negative_sid(idmap_cache,
1503 bids[i]);
1505 IDMAP_CHECK_RET(ret);
1509 ret = NT_STATUS_OK;
1510 done:
1511 talloc_free(ctx);
1512 return ret;
1515 NTSTATUS idmap_set_mapping(const struct id_map *id)
1517 TALLOC_CTX *ctx;
1518 NTSTATUS ret;
1520 if (! NT_STATUS_IS_OK(ret = idmap_init())) {
1521 return ret;
1524 /* sanity checks */
1525 if ((id->sid == NULL) || (id->status != ID_MAPPED)) {
1526 DEBUG(1, ("NULL SID or unmapped entry\n"));
1527 return NT_STATUS_INVALID_PARAMETER;
1530 /* TODO: check uid/gid range ? */
1532 ctx = talloc_named_const(NULL, 0, "idmap_set_mapping ctx");
1533 if ( ! ctx) {
1534 DEBUG(1, ("failed to allocate talloc context, OOM?\n"));
1535 return NT_STATUS_NO_MEMORY;
1538 /* set the new mapping */
1539 ret = idmap_backends_set_mapping(id);
1540 IDMAP_CHECK_RET(ret);
1542 /* set the mapping in the cache */
1543 ret = idmap_cache_set(idmap_cache, id);
1544 IDMAP_CHECK_RET(ret);
1546 done:
1547 talloc_free(ctx);
1548 return ret;
1551 char *idmap_fetch_secret(const char *backend, bool alloc,
1552 const char *domain, const char *identity)
1554 char *tmp, *ret;
1555 int r;
1557 if (alloc) {
1558 r = asprintf(&tmp, "IDMAP_ALLOC_%s", backend);
1559 } else {
1560 r = asprintf(&tmp, "IDMAP_%s_%s", backend, domain);
1563 if (r < 0)
1564 return NULL;
1566 strupper_m(tmp); /* make sure the key is case insensitive */
1567 ret = secrets_fetch_generic(tmp, identity);
1569 SAFE_FREE(tmp);
1571 return ret;