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-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 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 { \
65 if ( ! NT_STATUS_IS_OK(ret)) { \
66 DEBUG(2, ("ERROR: NTSTATUS = 0x%08x\n", NT_STATUS_V(ret))); \
69 #define IDMAP_REPORT_RET(ret) do { \
70 if ( ! NT_STATUS_IS_OK(ret)) { \
71 DEBUG(2, ("ERROR: NTSTATUS = 0x%08x\n", NT_STATUS_V(ret))); \
73 #define IDMAP_CHECK_ALLOC(mem) do { \
75 DEBUG(0, ("Out of memory!\n")); ret = NT_STATUS_NO_MEMORY; \
79 static struct idmap_methods
*get_methods(struct idmap_backend
*be
,
82 struct idmap_backend
*b
;
84 for (b
= be
; b
; b
= b
->next
) {
85 if (strequal(b
->name
, name
)) {
93 static struct idmap_alloc_methods
*get_alloc_methods(
94 struct idmap_alloc_backend
*be
,
97 struct idmap_alloc_backend
*b
;
99 for (b
= be
; b
; b
= b
->next
) {
100 if (strequal(b
->name
, name
)) {
108 BOOL
idmap_is_offline(void)
110 return ( lp_winbind_offline_logon() &&
111 get_global_winbindd_state_offline() );
114 /**********************************************************************
115 Allow a module to register itself as a method.
116 **********************************************************************/
118 NTSTATUS
smb_register_idmap(int version
, const char *name
,
119 struct idmap_methods
*methods
)
121 struct idmap_methods
*test
;
122 struct idmap_backend
*entry
;
125 return NT_STATUS_INTERNAL_DB_ERROR
;
128 if ((version
!= SMB_IDMAP_INTERFACE_VERSION
)) {
129 DEBUG(0, ("Failed to register idmap module.\n"
130 "The module was compiled against "
131 "SMB_IDMAP_INTERFACE_VERSION %d,\n"
132 "current SMB_IDMAP_INTERFACE_VERSION is %d.\n"
133 "Please recompile against the current version "
135 version
, SMB_IDMAP_INTERFACE_VERSION
));
136 return NT_STATUS_OBJECT_TYPE_MISMATCH
;
139 if (!name
|| !name
[0] || !methods
) {
140 DEBUG(0,("Called with NULL pointer or empty name!\n"));
141 return NT_STATUS_INVALID_PARAMETER
;
144 test
= get_methods(backends
, name
);
146 DEBUG(0,("Idmap module %s already registered!\n", name
));
147 return NT_STATUS_OBJECT_NAME_COLLISION
;
150 entry
= talloc(idmap_ctx
, struct idmap_backend
);
152 DEBUG(0,("Out of memory!\n"));
153 return NT_STATUS_NO_MEMORY
;
155 entry
->name
= talloc_strdup(idmap_ctx
, name
);
156 if ( ! entry
->name
) {
157 DEBUG(0,("Out of memory!\n"));
158 return NT_STATUS_NO_MEMORY
;
160 entry
->methods
= methods
;
162 DLIST_ADD(backends
, entry
);
163 DEBUG(5, ("Successfully added idmap backend '%s'\n", name
));
167 /**********************************************************************
168 Allow a module to register itself as a method.
169 **********************************************************************/
171 NTSTATUS
smb_register_idmap_alloc(int version
, const char *name
,
172 struct idmap_alloc_methods
*methods
)
174 struct idmap_alloc_methods
*test
;
175 struct idmap_alloc_backend
*entry
;
178 return NT_STATUS_INTERNAL_DB_ERROR
;
181 if ((version
!= SMB_IDMAP_INTERFACE_VERSION
)) {
182 DEBUG(0, ("Failed to register idmap alloc module.\n"
183 "The module was compiled against "
184 "SMB_IDMAP_INTERFACE_VERSION %d,\n"
185 "current SMB_IDMAP_INTERFACE_VERSION is %d.\n"
186 "Please recompile against the current version "
188 version
, SMB_IDMAP_INTERFACE_VERSION
));
189 return NT_STATUS_OBJECT_TYPE_MISMATCH
;
192 if (!name
|| !name
[0] || !methods
) {
193 DEBUG(0,("Called with NULL pointer or empty name!\n"));
194 return NT_STATUS_INVALID_PARAMETER
;
197 test
= get_alloc_methods(alloc_backends
, name
);
199 DEBUG(0,("idmap_alloc module %s already registered!\n", name
));
200 return NT_STATUS_OBJECT_NAME_COLLISION
;
203 entry
= talloc(idmap_ctx
, struct idmap_alloc_backend
);
205 DEBUG(0,("Out of memory!\n"));
206 return NT_STATUS_NO_MEMORY
;
208 entry
->name
= talloc_strdup(idmap_ctx
, name
);
209 if ( ! entry
->name
) {
210 DEBUG(0,("Out of memory!\n"));
211 return NT_STATUS_NO_MEMORY
;
213 entry
->methods
= methods
;
215 DLIST_ADD(alloc_backends
, entry
);
216 DEBUG(5, ("Successfully added idmap alloc backend '%s'\n", name
));
220 static int close_domain_destructor(struct idmap_domain
*dom
)
224 ret
= dom
->methods
->close_fn(dom
);
225 if (!NT_STATUS_IS_OK(ret
)) {
226 DEBUG(3, ("Failed to close idmap domain [%s]!\n", dom
->name
));
232 /**************************************************************************
234 **************************************************************************/
236 NTSTATUS
idmap_close(void)
238 /* close the alloc backend first before freeing idmap_ctx */
239 if (idmap_alloc_ctx
) {
240 idmap_alloc_ctx
->methods
->close_fn();
241 idmap_alloc_ctx
->methods
= NULL
;
243 alloc_backends
= NULL
;
245 /* this talloc_free call will fire the talloc destructors
246 * that will free all active backends resources */
247 TALLOC_FREE(idmap_ctx
);
249 idmap_domains
= NULL
;
255 /**********************************************************************
256 Initialise idmap cache and a remote backend (if configured).
257 **********************************************************************/
259 static const char *idmap_default_domain
[] = { "default domain", NULL
};
261 /****************************************************************************
262 ****************************************************************************/
264 NTSTATUS
idmap_init_cache(void)
266 /* Always initialize the cache. We'll have to delay initialization
267 of backends if we are offline */
273 if ( (idmap_ctx
= talloc_named_const(NULL
, 0, "idmap_ctx")) == NULL
) {
274 return NT_STATUS_NO_MEMORY
;
277 if ( (idmap_cache
= idmap_cache_init(idmap_ctx
)) == NULL
) {
278 return NT_STATUS_UNSUCCESSFUL
;
284 /****************************************************************************
285 ****************************************************************************/
287 NTSTATUS
idmap_init(void)
290 static NTSTATUS idmap_init_status
= NT_STATUS_UNSUCCESSFUL
;
291 struct idmap_domain
*dom
;
292 char *compat_backend
= NULL
;
293 char *compat_params
= NULL
;
294 const char **dom_list
= NULL
;
295 char *alloc_backend
= NULL
;
296 BOOL default_already_defined
= False
;
297 BOOL pri_dom_is_in_list
= False
;
301 ret
= idmap_init_cache();
302 if (!NT_STATUS_IS_OK(ret
))
305 if (NT_STATUS_IS_OK(idmap_init_status
))
310 dom_list
= lp_idmap_domains();
312 if ( lp_idmap_backend() ) {
313 const char **compat_list
= lp_idmap_backend();
315 const char *q
= NULL
;
318 DEBUG(0, ("WARNING: idmap backend and idmap domains "
319 "are mutually excusive!\n"));
320 DEBUGADD(0,("idmap backend option will be IGNORED!\n"));
324 compat_backend
= talloc_strdup(idmap_ctx
, *compat_list
);
325 if (compat_backend
== NULL
) {
326 ret
= NT_STATUS_NO_MEMORY
;
330 /* strip any leading idmap_ prefix of */
331 if (strncmp(*compat_list
, "idmap_", 6) == 0 ) {
332 q
= *compat_list
+= 6;
333 DEBUG(0, ("WARNING: idmap backend uses obsolete"
334 " and deprecated 'idmap_' prefix.\n"
335 "Please replace 'idmap_%s' by '%s' in"
336 " %s\n", q
, q
, dyn_CONFIGFILE
));
337 compat_backend
= talloc_strdup(idmap_ctx
, q
);
339 compat_backend
= talloc_strdup(idmap_ctx
,
343 /* separate the backend and module arguements */
344 if ((p
= strchr(compat_backend
, ':')) != NULL
) {
346 compat_params
= p
+ 1;
349 } else if ( !dom_list
) {
350 /* Back compatible: without idmap domains and explicit
351 idmap backend. Taking default idmap backend: tdb */
354 compat_backend
= talloc_strdup( idmap_ctx
, "tdb");
355 compat_params
= compat_backend
;
359 dom_list
= idmap_default_domain
;
362 /***************************
363 * initialize idmap domains
365 DEBUG(1, ("Initializing idmap domains\n"));
367 for (i
= 0; dom_list
[i
]; i
++) {
368 const char *parm_backend
;
371 /* ignore BUILTIN and local MACHINE domains */
372 if (strequal(dom_list
[i
], "BUILTIN")
373 || strequal(dom_list
[i
], get_global_sam_name()))
375 DEBUG(0,("idmap_init: Ignoring invalid domain %s\n",
380 if (strequal(dom_list
[i
], lp_workgroup())) {
381 pri_dom_is_in_list
= True
;
385 dom
= TALLOC_ZERO_P(idmap_ctx
, struct idmap_domain
);
386 IDMAP_CHECK_ALLOC(dom
);
388 dom
->name
= talloc_strdup(dom
, dom_list
[i
]);
389 IDMAP_CHECK_ALLOC(dom
->name
);
391 config_option
= talloc_asprintf(dom
, "idmap config %s",
393 IDMAP_CHECK_ALLOC(config_option
);
395 /* default or specific ? */
397 dom
->default_domain
= lp_parm_bool(-1, config_option
,
400 if (dom
->default_domain
||
401 strequal(dom_list
[i
], idmap_default_domain
[0])) {
403 /* make sure this is set even when we match
404 * idmap_default_domain[0] */
405 dom
->default_domain
= True
;
407 if (default_already_defined
) {
408 DEBUG(1, ("ERROR: Multiple domains defined as"
410 ret
= NT_STATUS_INVALID_PARAMETER
;
414 default_already_defined
= True
;
418 dom
->readonly
= lp_parm_bool(-1, config_option
,
421 /* find associated backend (default: tdb) */
423 parm_backend
= talloc_strdup(idmap_ctx
, compat_backend
);
425 parm_backend
= talloc_strdup(idmap_ctx
,
426 lp_parm_const_string(
430 IDMAP_CHECK_ALLOC(parm_backend
);
432 /* get the backend methods for this domain */
433 dom
->methods
= get_methods(backends
, parm_backend
);
435 if ( ! dom
->methods
) {
436 ret
= smb_probe_module("idmap", parm_backend
);
437 if (NT_STATUS_IS_OK(ret
)) {
438 dom
->methods
= get_methods(backends
,
442 if ( ! dom
->methods
) {
443 DEBUG(0, ("ERROR: Could not get methods for "
444 "backend %s\n", parm_backend
));
445 ret
= NT_STATUS_UNSUCCESSFUL
;
449 /* check the set_mapping function exists otherwise mark the
450 * module as readonly */
451 if ( ! dom
->methods
->set_mapping
) {
452 DEBUG(5, ("Forcing to readonly, as this module can't"
453 " store arbitrary mappings.\n"));
454 dom
->readonly
= True
;
457 /* now that we have methods,
458 * set the destructor for this domain */
459 talloc_set_destructor(dom
, close_domain_destructor
);
462 dom
->params
= talloc_strdup(dom
, compat_params
);
463 IDMAP_CHECK_ALLOC(dom
->params
);
468 /* Finally instance a backend copy for this domain */
469 ret
= dom
->methods
->init(dom
);
470 if ( ! NT_STATUS_IS_OK(ret
)) {
471 DEBUG(0, ("ERROR: Initialization failed for backend "
472 "%s (domain %s), deferred!\n",
473 parm_backend
, dom
->name
));
475 idmap_domains
= talloc_realloc(idmap_ctx
, idmap_domains
,
476 struct idmap_domain
*, i
+1);
477 if ( ! idmap_domains
) {
478 DEBUG(0, ("Out of memory!\n"));
479 ret
= NT_STATUS_NO_MEMORY
;
482 idmap_domains
[i
] = dom
;
484 /* save default domain position for future uses */
485 if (dom
->default_domain
) {
489 DEBUG(10, ("Domain %s - Backend %s - %sdefault - %sreadonly\n",
490 dom
->name
, parm_backend
,
491 dom
->default_domain
?"":"not ",
492 dom
->readonly
?"":"not "));
494 talloc_free(config_option
);
497 /* save the number of domains we have */
500 /* automatically add idmap_nss backend if needed */
501 if ((lp_server_role() == ROLE_DOMAIN_MEMBER
) &&
502 ( ! pri_dom_is_in_list
) &&
503 lp_winbind_trusted_domains_only()) {
505 dom
= TALLOC_ZERO_P(idmap_ctx
, struct idmap_domain
);
506 IDMAP_CHECK_ALLOC(dom
);
508 dom
->name
= talloc_strdup(dom
, lp_workgroup());
509 IDMAP_CHECK_ALLOC(dom
->name
);
511 dom
->default_domain
= False
;
512 dom
->readonly
= True
;
514 /* get the backend methods for passdb */
515 dom
->methods
= get_methods(backends
, "nss");
517 /* (the nss module is always statically linked) */
518 if ( ! dom
->methods
) {
519 DEBUG(0, ("ERROR: No methods for idmap_nss ?!\n"));
520 ret
= NT_STATUS_UNSUCCESSFUL
;
524 /* now that we have methods,
525 * set the destructor for this domain */
526 talloc_set_destructor(dom
, close_domain_destructor
);
529 dom
->params
= talloc_strdup(dom
, compat_params
);
530 IDMAP_CHECK_ALLOC(dom
->params
);
535 /* Finally instance a backend copy for this domain */
536 ret
= dom
->methods
->init(dom
);
537 if ( ! NT_STATUS_IS_OK(ret
)) {
538 DEBUG(0, ("ERROR: Init. failed for idmap_nss ?!\n"));
539 ret
= NT_STATUS_UNSUCCESSFUL
;
543 idmap_domains
= talloc_realloc(idmap_ctx
,
545 struct idmap_domain
*,
547 if ( ! idmap_domains
) {
548 DEBUG(0, ("Out of memory!\n"));
549 ret
= NT_STATUS_NO_MEMORY
;
552 idmap_domains
[num_domains
] = dom
;
554 DEBUG(10, ("Domain %s - Backend nss - not default - readonly\n",
560 /**** automatically add idmap_passdb backend ****/
561 dom
= TALLOC_ZERO_P(idmap_ctx
, struct idmap_domain
);
562 IDMAP_CHECK_ALLOC(dom
);
564 dom
->name
= talloc_strdup(dom
, get_global_sam_name());
565 IDMAP_CHECK_ALLOC(dom
->name
);
567 dom
->default_domain
= False
;
568 dom
->readonly
= True
;
570 /* get the backend methods for passdb */
571 dom
->methods
= get_methods(backends
, "passdb");
573 /* (the passdb module is always statically linked) */
574 if ( ! dom
->methods
) {
575 DEBUG(0, ("ERROR: No methods for idmap_passdb ?!\n"));
576 ret
= NT_STATUS_UNSUCCESSFUL
;
580 /* now that we have methods, set the destructor for this domain */
581 talloc_set_destructor(dom
, close_domain_destructor
);
584 dom
->params
= talloc_strdup(dom
, compat_params
);
585 IDMAP_CHECK_ALLOC(dom
->params
);
590 /* Finally instance a backend copy for this domain */
591 ret
= dom
->methods
->init(dom
);
592 if ( ! NT_STATUS_IS_OK(ret
)) {
593 DEBUG(0, ("ERROR: Init. failed for idmap_passdb ?!\n"));
594 ret
= NT_STATUS_UNSUCCESSFUL
;
598 idmap_domains
= talloc_realloc(idmap_ctx
,
600 struct idmap_domain
*,
602 if ( ! idmap_domains
) {
603 DEBUG(0, ("Out of memory!\n"));
604 ret
= NT_STATUS_NO_MEMORY
;
607 idmap_domains
[num_domains
] = dom
;
609 /* needed to handle special BUILTIN and wellknown SIDs cases */
610 pdb_dom_num
= num_domains
;
612 DEBUG(10, ("Domain %s - Backend passdb - not default - readonly\n",
616 /**** finished adding idmap_passdb backend ****/
618 /* sort domains so that the default is the last one */
619 /* don't sort if no default domain defined */
620 if (def_dom_num
!= -1 && def_dom_num
!= num_domains
-1) {
621 /* default is not last, move it */
622 struct idmap_domain
*tmp
;
624 if (pdb_dom_num
> def_dom_num
) {
627 } else if (pdb_dom_num
== def_dom_num
) { /* ?? */
628 pdb_dom_num
= num_domains
- 1;
631 tmp
= idmap_domains
[def_dom_num
];
633 for (i
= def_dom_num
; i
< num_domains
-1; i
++) {
634 idmap_domains
[i
] = idmap_domains
[i
+1];
636 idmap_domains
[i
] = tmp
;
641 /* Initialize alloc module */
643 DEBUG(3, ("Initializing idmap alloc module\n"));
645 alloc_backend
= NULL
;
647 alloc_backend
= talloc_strdup(idmap_ctx
, compat_backend
);
649 char *ab
= lp_idmap_alloc_backend();
651 if (ab
&& (ab
[0] != '\0')) {
652 alloc_backend
= talloc_strdup(idmap_ctx
,
653 lp_idmap_alloc_backend());
657 if ( alloc_backend
) {
659 idmap_alloc_ctx
= TALLOC_ZERO_P(idmap_ctx
,
660 struct idmap_alloc_context
);
661 IDMAP_CHECK_ALLOC(idmap_alloc_ctx
);
663 idmap_alloc_ctx
->methods
= get_alloc_methods(alloc_backends
,
665 if ( ! idmap_alloc_ctx
->methods
) {
666 ret
= smb_probe_module("idmap", alloc_backend
);
667 if (NT_STATUS_IS_OK(ret
)) {
668 idmap_alloc_ctx
->methods
=
669 get_alloc_methods(alloc_backends
,
673 if (idmap_alloc_ctx
->methods
) {
676 idmap_alloc_ctx
->params
=
677 talloc_strdup(idmap_alloc_ctx
,
679 IDMAP_CHECK_ALLOC(idmap_alloc_ctx
->params
);
681 idmap_alloc_ctx
->params
= NULL
;
684 ret
= idmap_alloc_ctx
->methods
->init(idmap_alloc_ctx
->params
);
685 if ( ! NT_STATUS_IS_OK(ret
)) {
686 DEBUG(0, ("ERROR: Initialization failed for "
687 "alloc backend %s, deferred!\n",
690 idmap_alloc_ctx
->initialized
= True
;
693 DEBUG(2, ("idmap_init: Unable to get methods for "
694 "alloc backend %s\n",
696 /* certain compat backends are just readonly */
698 TALLOC_FREE(idmap_alloc_ctx
);
701 ret
= NT_STATUS_UNSUCCESSFUL
;
706 /* cleanpu temporary strings */
707 TALLOC_FREE( compat_backend
);
709 idmap_init_status
= NT_STATUS_OK
;
714 DEBUG(0, ("Aborting IDMAP Initialization ...\n"));
720 static NTSTATUS
idmap_alloc_init(void)
724 if (! NT_STATUS_IS_OK(ret
= idmap_init())) {
728 if ( ! idmap_alloc_ctx
) {
729 return NT_STATUS_NOT_SUPPORTED
;
732 if ( ! idmap_alloc_ctx
->initialized
) {
733 ret
= idmap_alloc_ctx
->methods
->init(idmap_alloc_ctx
->params
);
734 if ( ! NT_STATUS_IS_OK(ret
)) {
735 DEBUG(0, ("ERROR: Initialization failed for alloc "
736 "backend, deferred!\n"));
739 idmap_alloc_ctx
->initialized
= True
;
746 /**************************************************************************
747 idmap allocator interface functions
748 **************************************************************************/
750 NTSTATUS
idmap_allocate_uid(struct unixid
*id
)
754 if (! NT_STATUS_IS_OK(ret
= idmap_alloc_init())) {
758 id
->type
= ID_TYPE_UID
;
759 return idmap_alloc_ctx
->methods
->allocate_id(id
);
762 NTSTATUS
idmap_allocate_gid(struct unixid
*id
)
766 if (! NT_STATUS_IS_OK(ret
= idmap_alloc_init())) {
770 id
->type
= ID_TYPE_GID
;
771 return idmap_alloc_ctx
->methods
->allocate_id(id
);
774 NTSTATUS
idmap_set_uid_hwm(struct unixid
*id
)
778 if (! NT_STATUS_IS_OK(ret
= idmap_alloc_init())) {
782 id
->type
= ID_TYPE_UID
;
783 return idmap_alloc_ctx
->methods
->set_id_hwm(id
);
786 NTSTATUS
idmap_set_gid_hwm(struct unixid
*id
)
790 if (! NT_STATUS_IS_OK(ret
= idmap_alloc_init())) {
794 id
->type
= ID_TYPE_GID
;
795 return idmap_alloc_ctx
->methods
->set_id_hwm(id
);
798 /******************************************************************************
799 Lookup an idmap_domain give a full user or group SID
800 ******************************************************************************/
802 static struct idmap_domain
* find_idmap_domain_from_sid( DOM_SID
*account_sid
)
806 struct winbindd_domain
*domain
= NULL
;
809 /* 1. Handle BUILTIN or Special SIDs and prevent them from
810 falling into the default domain space (if we have a
811 configured passdb backend. */
813 if ( (pdb_dom_num
!= -1) &&
814 (sid_check_is_in_builtin(account_sid
) ||
815 sid_check_is_in_wellknown_domain(account_sid
) ||
816 sid_check_is_in_unix_groups(account_sid
) ||
817 sid_check_is_in_unix_users(account_sid
)) )
819 return idmap_domains
[pdb_dom_num
];
822 /* 2. Lookup the winbindd_domain from the account_sid */
824 sid_copy( &domain_sid
, account_sid
);
825 sid_split_rid( &domain_sid
, &rid
);
826 domain
= find_domain_from_sid_noinit( &domain_sid
);
828 for (i
= 0; domain
&& i
< num_domains
; i
++) {
829 if ( strequal( idmap_domains
[i
]->name
, domain
->name
) ) {
830 return idmap_domains
[i
];
834 /* 3. Fall back to the default domain */
836 if ( def_dom_num
!= -1 ) {
837 return idmap_domains
[def_dom_num
];
843 /******************************************************************************
844 Lookup an index given an idmap_domain pointer
845 ******************************************************************************/
847 static uint32
find_idmap_domain_index( struct idmap_domain
*id_domain
)
851 for (i
= 0; i
< num_domains
; i
++) {
852 if ( idmap_domains
[i
] == id_domain
)
860 /*********************************************************
861 Check if creating a mapping is permitted for the domain
862 *********************************************************/
864 static NTSTATUS
idmap_can_map(const struct id_map
*map
,
865 struct idmap_domain
**ret_dom
)
867 struct idmap_domain
*dom
;
869 /* Check we do not create mappings for our own local domain,
870 * or BUILTIN or special SIDs */
871 if ((sid_compare_domain(map
->sid
, get_global_sam_sid()) == 0) ||
872 sid_check_is_in_builtin(map
->sid
) ||
873 sid_check_is_in_wellknown_domain(map
->sid
)) {
874 DEBUG(10, ("We are not supposed to create mappings for "
875 "our own domains (local, builtin, specials)\n"));
876 return NT_STATUS_UNSUCCESSFUL
;
879 /* Special check for trusted domain only = Yes */
880 if (lp_winbind_trusted_domains_only()) {
881 struct winbindd_domain
*wdom
= find_our_domain();
882 if (wdom
&& (sid_compare_domain(map
->sid
, &wdom
->sid
) == 0)) {
883 DEBUG(10, ("We are not supposed to create mappings for "
884 "our primary domain when <trusted domain "
886 DEBUGADD(10, ("Leave [%s] unmapped\n",
887 sid_string_static(map
->sid
)));
888 return NT_STATUS_UNSUCCESSFUL
;
892 if ( (dom
= find_idmap_domain_from_sid( map
->sid
)) == NULL
) {
893 /* huh, couldn't find a suitable domain,
894 * let's just leave it unmapped */
895 DEBUG(10, ("Could not find idmap backend for SID %s",
896 sid_string_static(map
->sid
)));
897 return NT_STATUS_NO_SUCH_DOMAIN
;
901 /* ouch the domain is read only,
902 * let's just leave it unmapped */
903 DEBUG(10, ("idmap backend for SID %s is READONLY!\n",
904 sid_string_static(map
->sid
)));
905 return NT_STATUS_UNSUCCESSFUL
;
912 static NTSTATUS
idmap_new_mapping(TALLOC_CTX
*ctx
, struct id_map
*map
)
915 struct idmap_domain
*dom
;
917 /* If we are offline we cannot lookup SIDs, deny mapping */
918 if (idmap_is_offline()) {
919 return NT_STATUS_FILE_IS_OFFLINE
;
922 ret
= idmap_can_map(map
, &dom
);
923 if ( ! NT_STATUS_IS_OK(ret
)) {
924 return NT_STATUS_NONE_MAPPED
;
927 /* check if this is a valid SID and then map it */
928 switch (map
->xid
.type
) {
930 ret
= idmap_allocate_uid(&map
->xid
);
931 if ( ! NT_STATUS_IS_OK(ret
)) {
932 /* can't allocate id, let's just leave it unmapped */
933 DEBUG(2, ("uid allocation failed! "
934 "Can't create mapping\n"));
935 return NT_STATUS_NONE_MAPPED
;
939 ret
= idmap_allocate_gid(&map
->xid
);
940 if ( ! NT_STATUS_IS_OK(ret
)) {
941 /* can't allocate id, let's just leave it unmapped */
942 DEBUG(2, ("gid allocation failed! "
943 "Can't create mapping\n"));
944 return NT_STATUS_NONE_MAPPED
;
948 /* invalid sid, let's just leave it unmapped */
949 DEBUG(3,("idmap_new_mapping: Refusing to create a "
950 "mapping for an unspecified ID type.\n"));
951 return NT_STATUS_NONE_MAPPED
;
954 /* ok, got a new id, let's set a mapping */
955 map
->status
= ID_MAPPED
;
957 DEBUG(10, ("Setting mapping: %s <-> %s %lu\n",
958 sid_string_static(map
->sid
),
959 (map
->xid
.type
== ID_TYPE_UID
) ? "UID" : "GID",
960 (unsigned long)map
->xid
.id
));
961 ret
= dom
->methods
->set_mapping(dom
, map
);
963 if ( ! NT_STATUS_IS_OK(ret
)) {
964 /* something wrong here :-( */
965 DEBUG(2, ("Failed to commit mapping\n!"));
967 /* TODO: would it make sense to have an "unalloc_id function?" */
969 return NT_STATUS_NONE_MAPPED
;
975 static NTSTATUS
idmap_backends_set_mapping(const struct id_map
*map
)
977 struct idmap_domain
*dom
;
980 DEBUG(10, ("Setting mapping %s <-> %s %lu\n",
981 sid_string_static(map
->sid
),
982 (map
->xid
.type
== ID_TYPE_UID
) ? "UID" : "GID",
983 (unsigned long)map
->xid
.id
));
985 ret
= idmap_can_map(map
, &dom
);
986 if ( ! NT_STATUS_IS_OK(ret
)) {
990 DEBUG(10,("set_mapping for domain %s\n", dom
->name
));
992 return dom
->methods
->set_mapping(dom
, map
);
995 static NTSTATUS
idmap_backends_unixids_to_sids(struct id_map
**ids
)
997 struct idmap_domain
*dom
;
998 struct id_map
**unmapped
;
999 struct id_map
**_ids
;
1004 if (!ids
|| !*ids
) {
1005 DEBUG(1, ("Invalid list of maps\n"));
1006 return NT_STATUS_INVALID_PARAMETER
;
1009 ctx
= talloc_named_const(NULL
, 0, "idmap_backends_unixids_to_sids ctx");
1011 DEBUG(0, ("Out of memory!\n"));
1012 return NT_STATUS_NO_MEMORY
;
1015 DEBUG(10, ("Query backends to map ids->sids\n"));
1017 /* start from the default (the last one) and then if there are still
1018 * unmapped entries cycle through the others */
1023 for (n
= num_domains
-1; n
>= 0; n
--) { /* cycle backwards */
1025 dom
= idmap_domains
[n
];
1027 DEBUG(10, ("Query sids from domain %s\n", dom
->name
));
1029 ret
= dom
->methods
->unixids_to_sids(dom
, _ids
);
1030 IDMAP_REPORT_RET(ret
);
1034 for (i
= 0, u
= 0; _ids
[i
]; i
++) {
1035 if (_ids
[i
]->status
!= ID_MAPPED
) {
1036 unmapped
= talloc_realloc(ctx
, unmapped
,
1037 struct id_map
*, u
+ 2);
1038 IDMAP_CHECK_ALLOC(unmapped
);
1039 unmapped
[u
] = _ids
[i
];
1044 /* terminate the unmapped list */
1046 } else { /* no more entries, get out */
1055 /* there are still unmapped ids,
1056 * map them to the unix users/groups domains */
1057 /* except for expired entries,
1058 * these will be returned as valid (offline mode) */
1059 for (i
= 0; unmapped
[i
]; i
++) {
1060 if (unmapped
[i
]->status
== ID_EXPIRED
) continue;
1061 switch (unmapped
[i
]->xid
.type
) {
1063 uid_to_unix_users_sid(
1064 (uid_t
)unmapped
[i
]->xid
.id
,
1066 unmapped
[i
]->status
= ID_MAPPED
;
1069 gid_to_unix_groups_sid(
1070 (gid_t
)unmapped
[i
]->xid
.id
,
1072 unmapped
[i
]->status
= ID_MAPPED
;
1074 default: /* what?! */
1075 unmapped
[i
]->status
= ID_UNKNOWN
;
1088 static NTSTATUS
idmap_backends_sids_to_unixids(struct id_map
**ids
)
1090 struct id_map
***dom_ids
;
1091 struct idmap_domain
*dom
;
1096 if ( (ctx
= talloc_named_const(NULL
, 0, "be_sids_to_ids")) == NULL
) {
1097 DEBUG(1, ("failed to allocate talloc context, OOM?\n"));
1098 return NT_STATUS_NO_MEMORY
;
1101 DEBUG(10, ("Query backends to map sids->ids\n"));
1103 /* split list per domain */
1104 if (num_domains
== 0) {
1105 DEBUG(1, ("No domains available?\n"));
1106 return NT_STATUS_UNSUCCESSFUL
;
1109 dom_ids
= TALLOC_ZERO_ARRAY(ctx
, struct id_map
**, num_domains
);
1110 IDMAP_CHECK_ALLOC(dom_ids
);
1111 counters
= TALLOC_ZERO_ARRAY(ctx
, int, num_domains
);
1112 IDMAP_CHECK_ALLOC(counters
);
1114 /* partition the requests by domain */
1116 for (i
= 0; ids
[i
]; i
++) {
1119 if ((dom
= find_idmap_domain_from_sid(ids
[i
]->sid
)) == NULL
) {
1120 /* no available idmap_domain. Move on */
1124 DEBUG(10,("SID %s is being handled by %s\n",
1125 sid_string_static(ids
[i
]->sid
),
1126 dom
? dom
->name
: "none" ));
1128 idx
= find_idmap_domain_index( dom
);
1129 SMB_ASSERT( idx
!= -1 );
1131 dom_ids
[idx
] = talloc_realloc(ctx
, dom_ids
[idx
],
1134 IDMAP_CHECK_ALLOC(dom_ids
[idx
]);
1136 dom_ids
[idx
][counters
[idx
]] = ids
[i
];
1138 dom_ids
[idx
][counters
[idx
]] = NULL
;
1141 /* All the ids have been dispatched in the right queues.
1142 Let's cycle through the filled ones */
1144 for (i
= 0; i
< num_domains
; i
++) {
1146 dom
= idmap_domains
[i
];
1147 DEBUG(10, ("Query ids from domain %s\n", dom
->name
));
1148 ret
= dom
->methods
->sids_to_unixids(dom
, dom_ids
[i
]);
1149 IDMAP_REPORT_RET(ret
);
1153 /* ok all the backends have been contacted at this point */
1154 /* let's see if we have any unmapped SID left and act accordingly */
1156 for (i
= 0; ids
[i
]; i
++) {
1157 /* NOTE: this will NOT touch ID_EXPIRED entries that the backend
1158 * was not able to confirm/deny (offline mode) */
1159 if (ids
[i
]->status
== ID_UNKNOWN
||
1160 ids
[i
]->status
== ID_UNMAPPED
) {
1161 /* ok this is an unmapped one, see if we can map it */
1162 ret
= idmap_new_mapping(ctx
, ids
[i
]);
1163 if (NT_STATUS_IS_OK(ret
)) {
1164 /* successfully mapped */
1165 ids
[i
]->status
= ID_MAPPED
;
1167 if (NT_STATUS_EQUAL(ret
, NT_STATUS_NONE_MAPPED
)) {
1168 /* could not map it */
1169 ids
[i
]->status
= ID_UNMAPPED
;
1171 /* Something very bad happened down there
1172 * OR we are offline */
1173 ids
[i
]->status
= ID_UNKNOWN
;
1185 /**************************************************************************
1186 idmap interface functions
1187 **************************************************************************/
1189 NTSTATUS
idmap_unixids_to_sids(struct id_map
**ids
)
1193 struct id_map
**bids
;
1197 if (! NT_STATUS_IS_OK(ret
= idmap_init())) {
1201 if (!ids
|| !*ids
) {
1202 DEBUG(1, ("Invalid list of maps\n"));
1203 return NT_STATUS_INVALID_PARAMETER
;
1206 ctx
= talloc_named_const(NULL
, 0, "idmap_unixids_to_sids ctx");
1208 DEBUG(1, ("failed to allocate talloc context, OOM?\n"));
1209 return NT_STATUS_NO_MEMORY
;
1212 /* no ids to be asked to the backends by default */
1216 for (i
= 0; ids
[i
]; i
++) {
1218 if ( ! ids
[i
]->sid
) {
1219 DEBUG(1, ("invalid null SID in id_map array"));
1221 return NT_STATUS_INVALID_PARAMETER
;
1224 ret
= idmap_cache_map_id(idmap_cache
, ids
[i
]);
1226 if ( ! NT_STATUS_IS_OK(ret
)) {
1229 /* alloc space for ids to be resolved by
1230 * backends (realloc ten by ten) */
1231 bids
= TALLOC_ARRAY(ctx
, struct id_map
*, 10);
1233 DEBUG(1, ("Out of memory!\n"));
1235 return NT_STATUS_NO_MEMORY
;
1240 /* add this id to the ones to be retrieved
1241 * from the backends */
1245 /* check if we need to allocate new space
1246 * on the rids array */
1249 bids
= talloc_realloc(ctx
, bids
,
1250 struct id_map
*, bn
);
1252 DEBUG(1, ("Out of memory!\n"));
1254 return NT_STATUS_NO_MEMORY
;
1258 /* make sure the last element is NULL */
1263 /* let's see if there is any id mapping to be retieved
1264 * from the backends */
1267 ret
= idmap_backends_unixids_to_sids(bids
);
1268 IDMAP_CHECK_RET(ret
);
1270 /* update the cache */
1271 for (i
= 0; i
< bi
; i
++) {
1272 if (bids
[i
]->status
== ID_MAPPED
) {
1273 ret
= idmap_cache_set(idmap_cache
, bids
[i
]);
1274 } else if (bids
[i
]->status
== ID_EXPIRED
) {
1275 /* the cache returned an expired entry and the
1276 * backend was not able to clear the situation
1277 * (offline). This handles a previous
1278 * NT_STATUS_SYNCHRONIZATION_REQUIRED
1279 * for disconnected mode, */
1280 bids
[i
]->status
= ID_MAPPED
;
1281 } else if (bids
[i
]->status
== ID_UNKNOWN
) {
1282 /* something bad here. We were not able to
1283 * handle this for some reason, mark it as
1284 * unmapped and hope next time things will
1286 bids
[i
]->status
= ID_UNMAPPED
;
1287 } else { /* unmapped */
1288 ret
= idmap_cache_set_negative_id(idmap_cache
,
1291 IDMAP_CHECK_RET(ret
);
1301 NTSTATUS
idmap_sids_to_unixids(struct id_map
**ids
)
1305 struct id_map
**bids
;
1309 if (! NT_STATUS_IS_OK(ret
= idmap_init())) {
1313 if (!ids
|| !*ids
) {
1314 DEBUG(1, ("Invalid list of maps\n"));
1315 return NT_STATUS_INVALID_PARAMETER
;
1318 ctx
= talloc_named_const(NULL
, 0, "idmap_sids_to_unixids ctx");
1320 DEBUG(1, ("failed to allocate talloc context, OOM?\n"));
1321 return NT_STATUS_NO_MEMORY
;
1324 /* no ids to be asked to the backends by default */
1328 for (i
= 0; ids
[i
]; i
++) {
1330 if ( ! ids
[i
]->sid
) {
1331 DEBUG(1, ("invalid null SID in id_map array\n"));
1333 return NT_STATUS_INVALID_PARAMETER
;
1336 ret
= idmap_cache_map_sid(idmap_cache
, ids
[i
]);
1338 if ( ! NT_STATUS_IS_OK(ret
)) {
1341 /* alloc space for ids to be resolved
1342 by backends (realloc ten by ten) */
1343 bids
= TALLOC_ARRAY(ctx
, struct id_map
*, 10);
1345 DEBUG(1, ("Out of memory!\n"));
1347 return NT_STATUS_NO_MEMORY
;
1352 /* add this id to the ones to be retrieved
1353 * from the backends */
1357 /* check if we need to allocate new space
1358 * on the ids array */
1361 bids
= talloc_realloc(ctx
, bids
,
1362 struct id_map
*, bn
);
1364 DEBUG(1, ("Out of memory!\n"));
1366 return NT_STATUS_NO_MEMORY
;
1370 /* make sure the last element is NULL */
1375 /* let's see if there is any id mapping to be retieved
1376 * from the backends */
1379 ret
= idmap_backends_sids_to_unixids(bids
);
1380 IDMAP_CHECK_RET(ret
);
1382 /* update the cache */
1383 for (i
= 0; bids
[i
]; i
++) {
1384 if (bids
[i
]->status
== ID_MAPPED
) {
1385 ret
= idmap_cache_set(idmap_cache
, bids
[i
]);
1386 } else if (bids
[i
]->status
== ID_EXPIRED
) {
1387 /* the cache returned an expired entry and the
1388 * backend was not able to clear the situation
1389 * (offline). This handles a previous
1390 * NT_STATUS_SYNCHRONIZATION_REQUIRED
1391 * for disconnected mode, */
1392 bids
[i
]->status
= ID_MAPPED
;
1393 } else if (bids
[i
]->status
== ID_UNKNOWN
) {
1394 /* something bad here. We were not able to
1395 * handle this for some reason, mark it as
1396 * unmapped and hope next time things will
1398 bids
[i
]->status
= ID_UNMAPPED
;
1399 } else { /* unmapped */
1400 ret
= idmap_cache_set_negative_sid(idmap_cache
,
1403 IDMAP_CHECK_RET(ret
);
1413 NTSTATUS
idmap_set_mapping(const struct id_map
*id
)
1418 if (! NT_STATUS_IS_OK(ret
= idmap_init())) {
1423 if ((id
->sid
== NULL
) || (id
->status
!= ID_MAPPED
)) {
1424 DEBUG(1, ("NULL SID or unmapped entry\n"));
1425 return NT_STATUS_INVALID_PARAMETER
;
1428 /* TODO: check uid/gid range ? */
1430 ctx
= talloc_named_const(NULL
, 0, "idmap_set_mapping ctx");
1432 DEBUG(1, ("failed to allocate talloc context, OOM?\n"));
1433 return NT_STATUS_NO_MEMORY
;
1436 /* set the new mapping */
1437 ret
= idmap_backends_set_mapping(id
);
1438 IDMAP_CHECK_RET(ret
);
1440 /* set the mapping in the cache */
1441 ret
= idmap_cache_set(idmap_cache
, id
);
1442 IDMAP_CHECK_RET(ret
);
1449 /**************************************************************************
1450 Dump backend status.
1451 **************************************************************************/
1453 void idmap_dump_maps(char *logfile
)
1456 struct unixid allid
;
1457 struct id_map
*maps
;
1462 if (! NT_STATUS_IS_OK(ret
= idmap_init())) {
1466 dump
= fopen(logfile
, "w");
1468 DEBUG(0, ("Unable to open open stream for file [%s], "
1469 "errno: %d\n", logfile
, errno
));
1473 if (NT_STATUS_IS_OK(ret
= idmap_alloc_init())) {
1474 allid
.type
= ID_TYPE_UID
;
1476 idmap_alloc_ctx
->methods
->get_id_hwm(&allid
);
1477 fprintf(dump
, "USER HWM %lu\n", (unsigned long)allid
.id
);
1479 allid
.type
= ID_TYPE_GID
;
1481 idmap_alloc_ctx
->methods
->get_id_hwm(&allid
);
1482 fprintf(dump
, "GROUP HWM %lu\n", (unsigned long)allid
.id
);
1485 maps
= talloc(idmap_ctx
, struct id_map
);
1488 for (i
= 0; i
< num_domains
; i
++) {
1489 if (idmap_domains
[i
]->methods
->dump_data
) {
1490 idmap_domains
[i
]->methods
->dump_data(idmap_domains
[i
],
1495 for (i
= 0; i
< num_maps
; i
++) {
1496 switch (maps
[i
].xid
.type
) {
1498 fprintf(dump
, "UID %lu %s\n",
1499 (unsigned long)maps
[i
].xid
.id
,
1500 sid_string_static(maps
[i
].sid
));
1503 fprintf(dump
, "GID %lu %s\n",
1504 (unsigned long)maps
[i
].xid
.id
,
1505 sid_string_static(maps
[i
].sid
));
1507 case ID_TYPE_NOT_SPECIFIED
:
1516 char *idmap_fetch_secret(const char *backend
, bool alloc
,
1517 const char *domain
, const char *identity
)
1523 r
= asprintf(&tmp
, "IDMAP_ALLOC_%s", backend
);
1525 r
= asprintf(&tmp
, "IDMAP_%s_%s", backend
, domain
);
1531 strupper_m(tmp
); /* make sure the key is case insensitive */
1532 ret
= secrets_fetch_generic(tmp
, identity
);