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