2 Unix SMB/CIFS implementation.
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.
28 #define DBGC_CLASS DBGC_IDMAP
32 struct idmap_backend {
34 struct idmap_methods *methods;
35 struct idmap_backend *prev, *next;
38 struct idmap_alloc_backend {
40 struct idmap_alloc_methods *methods;
41 struct idmap_alloc_backend *prev, *next;
44 struct idmap_cache_ctx;
46 struct idmap_alloc_context {
48 struct idmap_alloc_methods *methods;
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)) {
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)) {
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;
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);
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);
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));
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;
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);
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);
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));
200 static int close_domain_destructor(struct idmap_domain *dom)
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));
212 /**************************************************************************
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);
229 idmap_domains = NULL;
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 */
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;
264 /****************************************************************************
265 ****************************************************************************/
267 NTSTATUS idmap_init(void)
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;
281 ret = idmap_init_cache();
282 if ( !NT_STATUS_IS_OK(ret) )
285 if (NT_STATUS_IS_OK(idmap_init_status))
290 dom_list = lp_idmap_domains();
292 if ( lp_idmap_backend() ) {
293 const char **compat_list = lp_idmap_backend();
295 const char *q = NULL;
298 DEBUG(0, ("WARNING: idmap backend and idmap domains are "
299 "mutually excusive!\n"));
300 DEBUGADD(0,("idmap backend option will be IGNORED!\n"));
304 if ( (compat_backend = talloc_strdup( idmap_ctx, *compat_list )) == NULL ) {
305 ret = NT_STATUS_NO_MEMORY;
309 /* strip any leading idmap_ prefix of */
310 if (strncmp(*compat_list, "idmap_", 6) == 0 ) {
311 q = *compat_list += 6;
312 DEBUG(0, ("WARNING: idmap backend uses obsolete and "
313 "deprecated 'idmap_' prefix.\n"
314 "Please replace 'idmap_%s' by '%s' in %s\n",
315 q, q, dyn_CONFIGFILE));
316 compat_backend = talloc_strdup( idmap_ctx, q);
318 compat_backend = talloc_strdup( idmap_ctx, *compat_list);
321 /* separate the backend and module arguements */
322 if ((p = strchr(compat_backend, ':')) != NULL) {
324 compat_params = p + 1;
327 } else if ( !dom_list ) {
328 /* Back compatible: without idmap domains and explicit
329 idmap backend. Taking default idmap backend: tdb */
332 compat_backend = talloc_strdup( idmap_ctx, "tdb");
333 compat_params = compat_backend;
337 dom_list = idmap_default_domain;
340 /***************************
341 * initialize idmap domains
343 DEBUG(1, ("Initializing idmap domains\n"));
345 for (i = 0; dom_list[i]; i++) {
346 const char *parm_backend;
349 /* ignore BUILTIN and local MACHINE domains */
350 if ( strequal(dom_list[i], "BUILTIN")
351 || strequal(dom_list[i], get_global_sam_name() ) )
353 DEBUG(0,("idmap_init: Ignoring invalid domain %s\n",
358 if (strequal(dom_list[i], lp_workgroup())) {
359 pri_dom_is_in_list = True;
363 dom = TALLOC_ZERO_P(idmap_ctx, struct idmap_domain);
364 IDMAP_CHECK_ALLOC(dom);
366 dom->name = talloc_strdup(dom, dom_list[i]);
367 IDMAP_CHECK_ALLOC(dom->name);
369 config_option = talloc_asprintf(dom, "idmap config %s", dom->name);
370 IDMAP_CHECK_ALLOC(config_option);
372 /* default or specific ? */
374 dom->default_domain = lp_parm_bool(-1, config_option, "default", False);
376 if (dom->default_domain ||
377 strequal(dom_list[i], idmap_default_domain[0])) {
379 /* make sure this is set even when we match idmap_default_domain[0] */
380 dom->default_domain = True;
382 if (default_already_defined) {
383 DEBUG(1, ("ERROR: Multiple domains defined as default!\n"));
384 ret = NT_STATUS_INVALID_PARAMETER;
388 default_already_defined = True;
392 dom->readonly = lp_parm_bool(-1, config_option, "readonly", False);
394 /* find associated backend (default: tdb) */
396 parm_backend = talloc_strdup(idmap_ctx, compat_backend);
398 parm_backend = talloc_strdup(idmap_ctx,
399 lp_parm_const_string(-1, config_option, "backend", "tdb"));
401 IDMAP_CHECK_ALLOC(parm_backend);
403 /* get the backend methods for this domain */
404 dom->methods = get_methods(backends, parm_backend);
406 if ( ! dom->methods) {
407 ret = smb_probe_module("idmap", parm_backend);
408 if (NT_STATUS_IS_OK(ret)) {
409 dom->methods = get_methods(backends, parm_backend);
412 if ( ! dom->methods) {
413 DEBUG(0, ("ERROR: Could not get methods for backend %s\n", parm_backend));
414 ret = NT_STATUS_UNSUCCESSFUL;
418 /* check the set_mapping function exists otherwise mark the module as readonly */
419 if ( ! dom->methods->set_mapping) {
420 DEBUG(5, ("Forcing to readonly, as ithis module can't store arbitrary mappings.\n"));
421 dom->readonly = True;
424 /* now that we have methods, set the destructor for this domain */
425 talloc_set_destructor(dom, close_domain_destructor);
428 dom->params = talloc_strdup(dom, compat_params);
429 IDMAP_CHECK_ALLOC(dom->params);
434 /* Finally instance a backend copy for this domain */
435 ret = dom->methods->init(dom);
436 if ( ! NT_STATUS_IS_OK(ret)) {
437 DEBUG(0, ("ERROR: Initialization failed for backend %s (domain %s), deferred!\n",
438 parm_backend, dom->name));
440 idmap_domains = talloc_realloc(idmap_ctx, idmap_domains, struct idmap_domain *, i+1);
441 if ( ! idmap_domains) {
442 DEBUG(0, ("Out of memory!\n"));
443 ret = NT_STATUS_NO_MEMORY;
446 idmap_domains[i] = dom;
448 if (dom->default_domain) { /* save default domain position for future uses */
452 DEBUG(10, ("Domain %s - Backend %s - %sdefault - %sreadonly\n",
453 dom->name, parm_backend,
454 dom->default_domain?"":"not ", dom->readonly?"":"not "));
456 talloc_free(config_option);
459 /* save the number of domains we have */
462 /* automatically add idmap_nss backend if needed */
463 if ((lp_server_role() == ROLE_DOMAIN_MEMBER) &&
464 ( ! pri_dom_is_in_list) &&
465 lp_winbind_trusted_domains_only()) {
467 dom = TALLOC_ZERO_P(idmap_ctx, struct idmap_domain);
468 IDMAP_CHECK_ALLOC(dom);
470 dom->name = talloc_strdup(dom, lp_workgroup());
471 IDMAP_CHECK_ALLOC(dom->name);
473 dom->default_domain = False;
474 dom->readonly = True;
476 /* get the backend methods for passdb */
477 dom->methods = get_methods(backends, "nss");
479 /* (the nss module is always statically linked) */
480 if ( ! dom->methods) {
481 DEBUG(0, ("ERROR: Could not get methods for idmap_nss ?!\n"));
482 ret = NT_STATUS_UNSUCCESSFUL;
486 /* now that we have methods, set the destructor for this domain */
487 talloc_set_destructor(dom, close_domain_destructor);
490 dom->params = talloc_strdup(dom, compat_params);
491 IDMAP_CHECK_ALLOC(dom->params);
496 /* Finally instance a backend copy for this domain */
497 ret = dom->methods->init(dom);
498 if ( ! NT_STATUS_IS_OK(ret)) {
499 DEBUG(0, ("ERROR: Initialization failed for idmap_nss ?!\n"));
500 ret = NT_STATUS_UNSUCCESSFUL;
504 idmap_domains = talloc_realloc(idmap_ctx, idmap_domains, struct idmap_domain *, num_domains+1);
505 if ( ! idmap_domains) {
506 DEBUG(0, ("Out of memory!\n"));
507 ret = NT_STATUS_NO_MEMORY;
510 idmap_domains[num_domains] = dom;
512 DEBUG(10, ("Domain %s - Backend nss - not default - readonly\n", dom->name ));
517 /**** automatically add idmap_passdb backend ****/
518 dom = TALLOC_ZERO_P(idmap_ctx, struct idmap_domain);
519 IDMAP_CHECK_ALLOC(dom);
521 dom->name = talloc_strdup(dom, get_global_sam_name());
522 IDMAP_CHECK_ALLOC(dom->name);
524 dom->default_domain = False;
525 dom->readonly = True;
527 /* get the backend methods for passdb */
528 dom->methods = get_methods(backends, "passdb");
530 /* (the passdb module is always statically linked) */
531 if ( ! dom->methods) {
532 DEBUG(0, ("ERROR: Could not get methods for idmap_passdb ?!\n"));
533 ret = NT_STATUS_UNSUCCESSFUL;
537 /* now that we have methods, set the destructor for this domain */
538 talloc_set_destructor(dom, close_domain_destructor);
541 dom->params = talloc_strdup(dom, compat_params);
542 IDMAP_CHECK_ALLOC(dom->params);
547 /* Finally instance a backend copy for this domain */
548 ret = dom->methods->init(dom);
549 if ( ! NT_STATUS_IS_OK(ret)) {
550 DEBUG(0, ("ERROR: Initialization failed for idmap_passdb ?!\n"));
551 ret = NT_STATUS_UNSUCCESSFUL;
555 idmap_domains = talloc_realloc(idmap_ctx, idmap_domains, struct idmap_domain *, num_domains+1);
556 if ( ! idmap_domains) {
557 DEBUG(0, ("Out of memory!\n"));
558 ret = NT_STATUS_NO_MEMORY;
561 idmap_domains[num_domains] = dom;
563 /* needed to handle special BUILTIN and wellknown SIDs cases */
564 pdb_dom_num = num_domains;
566 DEBUG(10, ("Domain %s - Backend passdb - not default - readonly\n", dom->name));
569 /**** finished adding idmap_passdb backend ****/
571 /* sort domains so that the default is the last one */
572 /* don't sort if no default domain defined */
573 if (def_dom_num != -1 && def_dom_num != num_domains-1) { /* default is not last, move it */
574 struct idmap_domain *tmp;
576 if (pdb_dom_num > def_dom_num) {
579 } else if (pdb_dom_num == def_dom_num) { /* ?? */
580 pdb_dom_num = num_domains - 1;
583 tmp = idmap_domains[def_dom_num];
585 for (i = def_dom_num; i < num_domains-1; i++) {
586 idmap_domains[i] = idmap_domains[i+1];
588 idmap_domains[i] = tmp;
593 /* Initialize alloc module */
595 DEBUG(3, ("Initializing idmap alloc module\n"));
597 alloc_backend = NULL;
599 alloc_backend = talloc_strdup(idmap_ctx, compat_backend);
601 char *ab = lp_idmap_alloc_backend();
603 if (ab && (ab[0] != '\0')) {
604 alloc_backend = talloc_strdup(idmap_ctx, lp_idmap_alloc_backend());
608 if ( alloc_backend ) {
610 idmap_alloc_ctx = TALLOC_ZERO_P(idmap_ctx, struct idmap_alloc_context);
611 IDMAP_CHECK_ALLOC(idmap_alloc_ctx);
613 idmap_alloc_ctx->methods = get_alloc_methods(alloc_backends, alloc_backend);
614 if ( ! idmap_alloc_ctx->methods) {
615 ret = smb_probe_module("idmap", alloc_backend);
616 if (NT_STATUS_IS_OK(ret)) {
617 idmap_alloc_ctx->methods = get_alloc_methods(alloc_backends, alloc_backend);
620 if (idmap_alloc_ctx->methods) {
623 idmap_alloc_ctx->params = talloc_strdup(idmap_alloc_ctx, compat_params);
624 IDMAP_CHECK_ALLOC(idmap_alloc_ctx->params);
626 idmap_alloc_ctx->params = NULL;
629 ret = idmap_alloc_ctx->methods->init(idmap_alloc_ctx->params);
630 if ( ! NT_STATUS_IS_OK(ret)) {
631 DEBUG(0, ("ERROR: Initialization failed for alloc "
632 "backend %s, deferred!\n", alloc_backend));
634 idmap_alloc_ctx->initialized = True;
637 DEBUG(2, ("idmap_init: Unable to get methods for alloc backend %s\n",
639 /* certain compat backends are just readonly */
641 TALLOC_FREE(idmap_alloc_ctx);
644 ret = NT_STATUS_UNSUCCESSFUL;
649 /* cleanpu temporary strings */
650 TALLOC_FREE( compat_backend );
652 idmap_init_status = NT_STATUS_OK;
657 DEBUG(0, ("Aborting IDMAP Initialization ...\n"));
663 static NTSTATUS idmap_alloc_init(void)
667 if (! NT_STATUS_IS_OK(ret = idmap_init())) {
671 if ( ! idmap_alloc_ctx) {
672 return NT_STATUS_NOT_SUPPORTED;
675 if ( ! idmap_alloc_ctx->initialized) {
676 ret = idmap_alloc_ctx->methods->init(idmap_alloc_ctx->params);
677 if ( ! NT_STATUS_IS_OK(ret)) {
678 DEBUG(0, ("ERROR: Initialization failed for alloc "
679 "backend, deferred!\n"));
682 idmap_alloc_ctx->initialized = True;
689 /**************************************************************************
690 idmap allocator interface functions
691 **************************************************************************/
693 NTSTATUS idmap_allocate_uid(struct unixid *id)
697 if (! NT_STATUS_IS_OK(ret = idmap_alloc_init())) {
701 id->type = ID_TYPE_UID;
702 return idmap_alloc_ctx->methods->allocate_id(id);
705 NTSTATUS idmap_allocate_gid(struct unixid *id)
709 if (! NT_STATUS_IS_OK(ret = idmap_alloc_init())) {
713 id->type = ID_TYPE_GID;
714 return idmap_alloc_ctx->methods->allocate_id(id);
717 NTSTATUS idmap_set_uid_hwm(struct unixid *id)
721 if (! NT_STATUS_IS_OK(ret = idmap_alloc_init())) {
725 id->type = ID_TYPE_UID;
726 return idmap_alloc_ctx->methods->set_id_hwm(id);
729 NTSTATUS idmap_set_gid_hwm(struct unixid *id)
733 if (! NT_STATUS_IS_OK(ret = idmap_alloc_init())) {
737 id->type = ID_TYPE_GID;
738 return idmap_alloc_ctx->methods->set_id_hwm(id);
741 /******************************************************************************
742 Lookup an idmap_domain give a full user or group SID
743 ******************************************************************************/
745 static struct idmap_domain* find_idmap_domain_from_sid( DOM_SID *account_sid )
749 struct winbindd_domain *domain = NULL;
752 /* 1. Handle BUILTIN or Special SIDs and prevent them from
753 falling into the default domain space (if we have a
754 configured passdb backend. */
756 if ( (pdb_dom_num != -1) &&
757 (sid_check_is_in_builtin(account_sid) ||
758 sid_check_is_in_wellknown_domain(account_sid) ||
759 sid_check_is_in_unix_groups(account_sid) ||
760 sid_check_is_in_unix_users(account_sid)) )
762 return idmap_domains[pdb_dom_num];
765 /* 2. Lookup the winbindd_domain from the account_sid */
767 sid_copy( &domain_sid, account_sid );
768 sid_split_rid( &domain_sid, &rid );
769 domain = find_domain_from_sid_noinit( &domain_sid );
771 for (i = 0; domain && i < num_domains; i++) {
772 if ( strequal( idmap_domains[i]->name, domain->name ) ) {
773 return idmap_domains[i];
777 /* 3. Fall back to the default domain */
779 if ( def_dom_num != -1 ) {
780 return idmap_domains[def_dom_num];
786 /******************************************************************************
787 Lookup an index given an idmap_domain pointer
788 ******************************************************************************/
790 static uint32 find_idmap_domain_index( struct idmap_domain *id_domain)
794 for (i = 0; i < num_domains; i++) {
795 if ( idmap_domains[i] == id_domain )
803 /*********************************************************
804 Check if creating a mapping is permitted for the domain
805 *********************************************************/
807 static NTSTATUS idmap_can_map(const struct id_map *map, struct idmap_domain **ret_dom)
809 struct idmap_domain *dom;
811 /* Check we do not create mappings for our own local domain, or BUILTIN or special SIDs */
812 if ((sid_compare_domain(map->sid, get_global_sam_sid()) == 0) ||
813 sid_check_is_in_builtin(map->sid) ||
814 sid_check_is_in_wellknown_domain(map->sid)) {
815 DEBUG(10, ("We are not supposed to create mappings for our own domains (local, builtin, specials)\n"));
816 return NT_STATUS_UNSUCCESSFUL;
819 /* Special check for trusted domain only = Yes */
820 if (lp_winbind_trusted_domains_only()) {
821 struct winbindd_domain *wdom = find_our_domain();
822 if (wdom && (sid_compare_domain(map->sid, &wdom->sid) == 0)) {
823 DEBUG(10, ("We are not supposed to create mappings for our primary domain when <trusted domain only> is True\n"));
824 DEBUGADD(10, ("Leave [%s] unmapped\n", sid_string_static(map->sid)));
825 return NT_STATUS_UNSUCCESSFUL;
829 if ( (dom = find_idmap_domain_from_sid( map->sid )) == NULL ) {
830 /* huh, couldn't find a suitable domain, let's just leave it unmapped */
831 DEBUG(10, ("Could not find idmap backend for SID %s", sid_string_static(map->sid)));
832 return NT_STATUS_NO_SUCH_DOMAIN;
836 /* ouch the domain is read only, let's just leave it unmapped */
837 DEBUG(10, ("idmap backend for SID %s is READONLY!\n", sid_string_static(map->sid)));
838 return NT_STATUS_UNSUCCESSFUL;
845 static NTSTATUS idmap_new_mapping(TALLOC_CTX *ctx, struct id_map *map)
848 struct idmap_domain *dom;
850 /* If we are offline we cannot lookup SIDs, deny mapping */
851 if (idmap_is_offline()) {
852 return NT_STATUS_FILE_IS_OFFLINE;
855 ret = idmap_can_map(map, &dom);
856 if ( ! NT_STATUS_IS_OK(ret)) {
857 return NT_STATUS_NONE_MAPPED;
860 /* check if this is a valid SID and then map it */
861 switch (map->xid.type) {
863 ret = idmap_allocate_uid(&map->xid);
864 if ( ! NT_STATUS_IS_OK(ret)) {
865 /* can't allocate id, let's just leave it unmapped */
866 DEBUG(2, ("uid allocation failed! Can't create mapping\n"));
867 return NT_STATUS_NONE_MAPPED;
871 ret = idmap_allocate_gid(&map->xid);
872 if ( ! NT_STATUS_IS_OK(ret)) {
873 /* can't allocate id, let's just leave it unmapped */
874 DEBUG(2, ("gid allocation failed! Can't create mapping\n"));
875 return NT_STATUS_NONE_MAPPED;
879 /* invalid sid, let's just leave it unmapped */
880 DEBUG(3,("idmap_new_mapping: Refusing to create a "
881 "mapping for an unspecified ID type.\n"));
882 return NT_STATUS_NONE_MAPPED;
885 /* ok, got a new id, let's set a mapping */
886 map->status = ID_MAPPED;
888 DEBUG(10, ("Setting mapping: %s <-> %s %lu\n",
889 sid_string_static(map->sid),
890 (map->xid.type == ID_TYPE_UID) ? "UID" : "GID",
891 (unsigned long)map->xid.id));
892 ret = dom->methods->set_mapping(dom, map);
894 if ( ! NT_STATUS_IS_OK(ret)) {
895 /* something wrong here :-( */
896 DEBUG(2, ("Failed to commit mapping\n!"));
898 /* TODO: would it make sense to have an "unalloc_id function?" */
900 return NT_STATUS_NONE_MAPPED;
906 static NTSTATUS idmap_backends_set_mapping(const struct id_map *map)
908 struct idmap_domain *dom;
911 DEBUG(10, ("Setting mapping %s <-> %s %lu\n",
912 sid_string_static(map->sid),
913 (map->xid.type == ID_TYPE_UID) ? "UID" : "GID",
914 (unsigned long)map->xid.id));
916 ret = idmap_can_map(map, &dom);
917 if ( ! NT_STATUS_IS_OK(ret)) {
921 DEBUG(10,("set_mapping for domain %s\n", dom->name ));
923 return dom->methods->set_mapping(dom, map);
926 static NTSTATUS idmap_backends_unixids_to_sids(struct id_map **ids)
928 struct idmap_domain *dom;
929 struct id_map **unmapped;
930 struct id_map **_ids;
936 DEBUG(1, ("Invalid list of maps\n"));
937 return NT_STATUS_INVALID_PARAMETER;
940 ctx = talloc_named_const(NULL, 0, "idmap_backends_unixids_to_sids ctx");
942 DEBUG(0, ("Out of memory!\n"));
943 return NT_STATUS_NO_MEMORY;
946 DEBUG(10, ("Query backends to map ids->sids\n"));
948 /* start from the default (the last one) and then if there are still
949 * unmapped entries cycle through the others */
954 for (n = num_domains-1; n >= 0; n--) { /* cycle backwards */
956 dom = idmap_domains[n];
958 DEBUG(10, ("Query sids from domain %s\n", dom->name));
960 ret = dom->methods->unixids_to_sids(dom, _ids);
961 IDMAP_REPORT_RET(ret);
965 for (i = 0, u = 0; _ids[i]; i++) {
966 if (_ids[i]->status != ID_MAPPED) {
967 unmapped = talloc_realloc(ctx, unmapped, struct id_map *, u + 2);
968 IDMAP_CHECK_ALLOC(unmapped);
969 unmapped[u] = _ids[i];
974 /* terminate the unmapped list */
976 } else { /* no more entries, get out */
985 /* there are still unmapped ids, map them to the unix users/groups domains */
986 /* except for expired entries, these will be returned as valid (offline mode) */
987 for (i = 0; unmapped[i]; i++) {
988 if (unmapped[i]->status == ID_EXPIRED) continue;
989 switch (unmapped[i]->xid.type) {
991 uid_to_unix_users_sid((uid_t)unmapped[i]->xid.id, unmapped[i]->sid);
992 unmapped[i]->status = ID_MAPPED;
995 gid_to_unix_groups_sid((gid_t)unmapped[i]->xid.id, unmapped[i]->sid);
996 unmapped[i]->status = ID_MAPPED;
998 default: /* what?! */
999 unmapped[i]->status = ID_UNKNOWN;
1012 static NTSTATUS idmap_backends_sids_to_unixids(struct id_map **ids)
1014 struct id_map ***dom_ids;
1015 struct idmap_domain *dom;
1020 if ( (ctx = talloc_named_const(NULL, 0, "be_sids_to_ids")) == NULL ) {
1021 DEBUG(1, ("failed to allocate talloc context, OOM?\n"));
1022 return NT_STATUS_NO_MEMORY;
1025 DEBUG(10, ("Query backends to map sids->ids\n"));
1027 /* split list per domain */
1029 dom_ids = TALLOC_ZERO_ARRAY(ctx, struct id_map **, num_domains);
1030 IDMAP_CHECK_ALLOC(dom_ids);
1031 counters = TALLOC_ZERO_ARRAY(ctx, int, num_domains);
1033 /* partition the requests by domain */
1035 for (i = 0; ids[i]; i++) {
1038 if ( (dom = find_idmap_domain_from_sid( ids[i]->sid )) == NULL ) {
1039 /* no available idmap_domain. Move on */
1043 DEBUG(10,("SID %s is being handled by %s\n",
1044 sid_string_static(ids[i]->sid),
1045 dom ? dom->name : "none" ));
1047 idx = find_idmap_domain_index( dom );
1048 SMB_ASSERT( idx != -1 );
1050 dom_ids[idx] = talloc_realloc(ctx, dom_ids[idx],
1051 struct id_map *, counters[idx] + 2);
1052 IDMAP_CHECK_ALLOC(dom_ids[idx]);
1054 dom_ids[idx][counters[idx]] = ids[i];
1056 dom_ids[idx][counters[idx]] = NULL;
1059 /* All the ids have been dispatched in the right queues.
1060 Let's cycle through the filled ones */
1062 for (i = 0; i < num_domains; i++) {
1064 dom = idmap_domains[i];
1065 DEBUG(10, ("Query ids from domain %s\n", dom->name));
1066 ret = dom->methods->sids_to_unixids(dom, dom_ids[i]);
1067 IDMAP_REPORT_RET(ret);
1071 /* ok all the backends have been contacted at this point */
1072 /* let's see if we have any unmapped SID left and act accordingly */
1074 for (i = 0; ids[i]; i++) {
1075 /* NOTE: this will NOT touch ID_EXPIRED entries that the backend
1076 * was not able to confirm/deny (offline mode) */
1077 if (ids[i]->status == ID_UNKNOWN || ids[i]->status == ID_UNMAPPED) {
1078 /* ok this is an unmapped one, see if we can map it */
1079 ret = idmap_new_mapping(ctx, ids[i]);
1080 if (NT_STATUS_IS_OK(ret)) {
1081 /* successfully mapped */
1082 ids[i]->status = ID_MAPPED;
1083 } else if (NT_STATUS_EQUAL(ret, NT_STATUS_NONE_MAPPED)) {
1084 /* could not map it */
1085 ids[i]->status = ID_UNMAPPED;
1087 /* Something very bad happened down there
1088 * OR we are offline */
1089 ids[i]->status = ID_UNKNOWN;
1101 /**************************************************************************
1102 idmap interface functions
1103 **************************************************************************/
1105 NTSTATUS idmap_unixids_to_sids(struct id_map **ids)
1109 struct id_map **bids;
1113 if (! NT_STATUS_IS_OK(ret = idmap_init())) {
1117 if (!ids || !*ids) {
1118 DEBUG(1, ("Invalid list of maps\n"));
1119 return NT_STATUS_INVALID_PARAMETER;
1122 ctx = talloc_named_const(NULL, 0, "idmap_unixids_to_sids ctx");
1124 DEBUG(1, ("failed to allocate talloc context, OOM?\n"));
1125 return NT_STATUS_NO_MEMORY;
1128 /* no ids to be asked to the backends by default */
1132 for (i = 0; ids[i]; i++) {
1134 if ( ! ids[i]->sid) {
1135 DEBUG(1, ("invalid null SID in id_map array"));
1137 return NT_STATUS_INVALID_PARAMETER;
1140 ret = idmap_cache_map_id(idmap_cache, ids[i]);
1142 if ( ! NT_STATUS_IS_OK(ret)) {
1145 /* alloc space for ids to be resolved by backends (realloc ten by ten) */
1146 bids = TALLOC_ARRAY(ctx, struct id_map *, 10);
1148 DEBUG(1, ("Out of memory!\n"));
1150 return NT_STATUS_NO_MEMORY;
1155 /* add this id to the ones to be retrieved from the backends */
1159 /* check if we need to allocate new space on the rids array */
1162 bids = talloc_realloc(ctx, bids, struct id_map *, bn);
1164 DEBUG(1, ("Out of memory!\n"));
1166 return NT_STATUS_NO_MEMORY;
1170 /* make sure the last element is NULL */
1175 /* let's see if there is any id mapping to be retieved from the backends */
1178 ret = idmap_backends_unixids_to_sids(bids);
1179 IDMAP_CHECK_RET(ret);
1181 /* update the cache */
1182 for (i = 0; i < bi; i++) {
1183 if (bids[i]->status == ID_MAPPED) {
1184 ret = idmap_cache_set(idmap_cache, bids[i]);
1185 } else if (bids[i]->status == ID_EXPIRED) {
1186 /* the cache returned an expired entry and the backend was
1187 * was not able to clear the situation (offline).
1188 * This handles a previous NT_STATUS_SYNCHRONIZATION_REQUIRED
1189 * for disconnected mode, */
1190 bids[i]->status = ID_MAPPED;
1191 } else if (bids[i]->status == ID_UNKNOWN) {
1192 /* something bad here. We were not able to handle this for some
1193 * reason, mark it as unmapped and hope next time things will
1195 bids[i]->status = ID_UNMAPPED;
1196 } else { /* unmapped */
1197 ret = idmap_cache_set_negative_id(idmap_cache, bids[i]);
1199 IDMAP_CHECK_RET(ret);
1209 NTSTATUS idmap_sids_to_unixids(struct id_map **ids)
1213 struct id_map **bids;
1217 if (! NT_STATUS_IS_OK(ret = idmap_init())) {
1221 if (!ids || !*ids) {
1222 DEBUG(1, ("Invalid list of maps\n"));
1223 return NT_STATUS_INVALID_PARAMETER;
1226 ctx = talloc_named_const(NULL, 0, "idmap_sids_to_unixids ctx");
1228 DEBUG(1, ("failed to allocate talloc context, OOM?\n"));
1229 return NT_STATUS_NO_MEMORY;
1232 /* no ids to be asked to the backends by default */
1236 for (i = 0; ids[i]; i++) {
1238 if ( ! ids[i]->sid) {
1239 DEBUG(1, ("invalid null SID in id_map array\n"));
1241 return NT_STATUS_INVALID_PARAMETER;
1244 ret = idmap_cache_map_sid(idmap_cache, ids[i]);
1246 if ( ! NT_STATUS_IS_OK(ret)) {
1249 /* alloc space for ids to be resolved
1250 by backends (realloc ten by ten) */
1251 bids = TALLOC_ARRAY(ctx, struct id_map *, 10);
1253 DEBUG(1, ("Out of memory!\n"));
1255 return NT_STATUS_NO_MEMORY;
1260 /* add this id to the ones to be retrieved from the backends */
1264 /* check if we need to allocate new space on the ids array */
1267 bids = talloc_realloc(ctx, bids, struct id_map *, bn);
1269 DEBUG(1, ("Out of memory!\n"));
1271 return NT_STATUS_NO_MEMORY;
1275 /* make sure the last element is NULL */
1280 /* let's see if there is any id mapping to be retieved from the backends */
1283 ret = idmap_backends_sids_to_unixids(bids);
1284 IDMAP_CHECK_RET(ret);
1286 /* update the cache */
1287 for (i = 0; bids[i]; i++) {
1288 if (bids[i]->status == ID_MAPPED) {
1289 ret = idmap_cache_set(idmap_cache, bids[i]);
1290 } else if (bids[i]->status == ID_EXPIRED) {
1291 /* the cache returned an expired entry and the backend was
1292 * was not able to clear the situation (offline).
1293 * This handles a previous NT_STATUS_SYNCHRONIZATION_REQUIRED
1294 * for disconnected mode, */
1295 bids[i]->status = ID_MAPPED;
1296 } else if (bids[i]->status == ID_UNKNOWN) {
1297 /* something bad here. We were not able to handle this for some
1298 * reason, mark it as unmapped and hope next time things will
1300 bids[i]->status = ID_UNMAPPED;
1301 } else { /* unmapped */
1302 ret = idmap_cache_set_negative_sid(idmap_cache, bids[i]);
1304 IDMAP_CHECK_RET(ret);
1314 NTSTATUS idmap_set_mapping(const struct id_map *id)
1319 if (! NT_STATUS_IS_OK(ret = idmap_init())) {
1324 if ((id->sid == NULL) || (id->status != ID_MAPPED)) {
1325 DEBUG(1, ("NULL SID or unmapped entry\n"));
1326 return NT_STATUS_INVALID_PARAMETER;
1329 /* TODO: check uid/gid range ? */
1331 ctx = talloc_named_const(NULL, 0, "idmap_set_mapping ctx");
1333 DEBUG(1, ("failed to allocate talloc context, OOM?\n"));
1334 return NT_STATUS_NO_MEMORY;
1337 /* set the new mapping */
1338 ret = idmap_backends_set_mapping(id);
1339 IDMAP_CHECK_RET(ret);
1341 /* set the mapping in the cache */
1342 ret = idmap_cache_set(idmap_cache, id);
1343 IDMAP_CHECK_RET(ret);
1350 /**************************************************************************
1351 Dump backend status.
1352 **************************************************************************/
1354 void idmap_dump_maps(char *logfile)
1357 struct unixid allid;
1358 struct id_map *maps;
1363 if (! NT_STATUS_IS_OK(ret = idmap_init())) {
1367 dump = fopen(logfile, "w");
1369 DEBUG(0, ("Unable to open open stream for file [%s], errno: %d\n", logfile, errno));
1373 if (NT_STATUS_IS_OK(ret = idmap_alloc_init())) {
1374 allid.type = ID_TYPE_UID;
1376 idmap_alloc_ctx->methods->get_id_hwm(&allid);
1377 fprintf(dump, "USER HWM %lu\n", (unsigned long)allid.id);
1379 allid.type = ID_TYPE_GID;
1381 idmap_alloc_ctx->methods->get_id_hwm(&allid);
1382 fprintf(dump, "GROUP HWM %lu\n", (unsigned long)allid.id);
1385 maps = talloc(idmap_ctx, struct id_map);
1388 for (i = 0; i < num_domains; i++) {
1389 if (idmap_domains[i]->methods->dump_data) {
1390 idmap_domains[i]->methods->dump_data(idmap_domains[i], &maps, &num_maps);
1394 for (i = 0; i < num_maps; i++) {
1395 switch (maps[i].xid.type) {
1397 fprintf(dump, "UID %lu %s\n",
1398 (unsigned long)maps[i].xid.id,
1399 sid_string_static(maps[i].sid));
1402 fprintf(dump, "GID %lu %s\n",
1403 (unsigned long)maps[i].xid.id,
1404 sid_string_static(maps[i].sid));
1406 case ID_TYPE_NOT_SPECIFIED:
1415 char *idmap_fetch_secret(const char *backend, bool alloc,
1416 const char *domain, const char *identity)
1422 r = asprintf(&tmp, "IDMAP_ALLOC_%s", backend);
1424 r = asprintf(&tmp, "IDMAP_%s_%s", backend, domain);
1430 strupper_m(tmp); /* make sure the key is case insensitive */
1431 ret = secrets_fetch_generic(tmp, identity);