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
32 * Pointer to the backend methods. Modules register themselves here via
36 struct idmap_backend
{
38 struct idmap_methods
*methods
;
39 struct idmap_backend
*prev
, *next
;
41 static struct idmap_backend
*backends
= NULL
;
44 * Pointer to the alloc backend methods. Modules register themselves here via
45 * smb_register_idmap_alloc.
47 struct idmap_alloc_backend
{
49 struct idmap_alloc_methods
*methods
;
50 struct idmap_alloc_backend
*prev
, *next
;
52 static struct idmap_alloc_backend
*alloc_backends
= NULL
;
55 * The idmap alloc context that is configured via "idmap alloc
56 * backend". Defaults to "idmap backend" in case the module (tdb, ldap) also
57 * provides alloc methods.
59 struct idmap_alloc_context
{
60 struct idmap_alloc_methods
*methods
;
62 static struct idmap_alloc_context
*idmap_alloc_ctx
= NULL
;
65 * Default idmap domain configured via "idmap backend".
67 static struct idmap_domain
*default_idmap_domain
;
70 * Passdb idmap domain, not configurable. winbind must always give passdb a
73 static struct idmap_domain
*passdb_idmap_domain
;
76 * List of specially configured idmap domains. This list is filled on demand
77 * in the winbind idmap child when the parent winbind figures out via the
78 * special range parameter or via the domain SID that a special "idmap config
79 * domain" configuration is present.
81 static struct idmap_domain
**idmap_domains
= NULL
;
82 static int num_domains
= 0;
84 static struct idmap_methods
*get_methods(const char *name
)
86 struct idmap_backend
*b
;
88 for (b
= backends
; b
; b
= b
->next
) {
89 if (strequal(b
->name
, name
)) {
97 static struct idmap_alloc_methods
*get_alloc_methods(const char *name
)
99 struct idmap_alloc_backend
*b
;
101 for (b
= alloc_backends
; b
; b
= b
->next
) {
102 if (strequal(b
->name
, name
)) {
110 bool idmap_is_offline(void)
112 return ( lp_winbind_offline_logon() &&
113 get_global_winbindd_state_offline() );
116 bool idmap_is_online(void)
118 return !idmap_is_offline();
121 /**********************************************************************
122 Allow a module to register itself as a method.
123 **********************************************************************/
125 NTSTATUS
smb_register_idmap(int version
, const char *name
,
126 struct idmap_methods
*methods
)
128 struct idmap_backend
*entry
;
130 if ((version
!= SMB_IDMAP_INTERFACE_VERSION
)) {
131 DEBUG(0, ("Failed to register idmap module.\n"
132 "The module was compiled against "
133 "SMB_IDMAP_INTERFACE_VERSION %d,\n"
134 "current SMB_IDMAP_INTERFACE_VERSION is %d.\n"
135 "Please recompile against the current version "
137 version
, SMB_IDMAP_INTERFACE_VERSION
));
138 return NT_STATUS_OBJECT_TYPE_MISMATCH
;
141 if (!name
|| !name
[0] || !methods
) {
142 DEBUG(0,("Called with NULL pointer or empty name!\n"));
143 return NT_STATUS_INVALID_PARAMETER
;
146 for (entry
= backends
; entry
!= NULL
; entry
= entry
->next
) {
147 if (strequal(entry
->name
, name
)) {
148 DEBUG(0,("Idmap module %s already registered!\n",
150 return NT_STATUS_OBJECT_NAME_COLLISION
;
154 entry
= talloc(NULL
, struct idmap_backend
);
156 DEBUG(0,("Out of memory!\n"));
158 return NT_STATUS_NO_MEMORY
;
160 entry
->name
= talloc_strdup(entry
, name
);
161 if ( ! entry
->name
) {
162 DEBUG(0,("Out of memory!\n"));
164 return NT_STATUS_NO_MEMORY
;
166 entry
->methods
= methods
;
168 DLIST_ADD(backends
, entry
);
169 DEBUG(5, ("Successfully added idmap backend '%s'\n", name
));
173 /**********************************************************************
174 Allow a module to register itself as an alloc method.
175 **********************************************************************/
177 NTSTATUS
smb_register_idmap_alloc(int version
, const char *name
,
178 struct idmap_alloc_methods
*methods
)
180 struct idmap_alloc_methods
*test
;
181 struct idmap_alloc_backend
*entry
;
183 if ((version
!= SMB_IDMAP_INTERFACE_VERSION
)) {
184 DEBUG(0, ("Failed to register idmap alloc module.\n"
185 "The module was compiled against "
186 "SMB_IDMAP_INTERFACE_VERSION %d,\n"
187 "current SMB_IDMAP_INTERFACE_VERSION is %d.\n"
188 "Please recompile against the current version "
190 version
, SMB_IDMAP_INTERFACE_VERSION
));
191 return NT_STATUS_OBJECT_TYPE_MISMATCH
;
194 if (!name
|| !name
[0] || !methods
) {
195 DEBUG(0,("Called with NULL pointer or empty name!\n"));
196 return NT_STATUS_INVALID_PARAMETER
;
199 test
= get_alloc_methods(name
);
201 DEBUG(0,("idmap_alloc module %s already registered!\n", name
));
202 return NT_STATUS_OBJECT_NAME_COLLISION
;
205 entry
= talloc(NULL
, struct idmap_alloc_backend
);
207 DEBUG(0,("Out of memory!\n"));
208 return NT_STATUS_NO_MEMORY
;
210 entry
->name
= talloc_strdup(entry
, name
);
211 if ( ! entry
->name
) {
212 DEBUG(0,("Out of memory!\n"));
213 return NT_STATUS_NO_MEMORY
;
215 entry
->methods
= methods
;
217 DLIST_ADD(alloc_backends
, entry
);
218 DEBUG(5, ("Successfully added idmap alloc backend '%s'\n", name
));
222 static int close_domain_destructor(struct idmap_domain
*dom
)
226 ret
= dom
->methods
->close_fn(dom
);
227 if (!NT_STATUS_IS_OK(ret
)) {
228 DEBUG(3, ("Failed to close idmap domain [%s]!\n", dom
->name
));
234 static bool parse_idmap_module(TALLOC_CTX
*mem_ctx
, const char *param
,
235 char **pmodulename
, char **pargs
)
240 if (strncmp(param
, "idmap_", 6) == 0) {
242 DEBUG(1, ("idmap_init: idmap backend uses deprecated "
243 "'idmap_' prefix. Please replace 'idmap_%s' by "
244 "'%s'\n", param
, param
));
247 modulename
= talloc_strdup(mem_ctx
, param
);
248 if (modulename
== NULL
) {
252 args
= strchr(modulename
, ':');
254 *pmodulename
= modulename
;
261 args
= talloc_strdup(mem_ctx
, args
+1);
263 TALLOC_FREE(modulename
);
267 *pmodulename
= modulename
;
273 * Initialize a domain structure
274 * @param[in] mem_ctx memory context for the result
275 * @param[in] domainname which domain is this for
276 * @param[in] modulename which backend module
277 * @param[in] params parameter to pass to the init function
278 * @result The initialized structure
280 static struct idmap_domain
*idmap_init_domain(TALLOC_CTX
*mem_ctx
,
281 const char *domainname
,
282 const char *modulename
,
285 struct idmap_domain
*result
;
288 result
= talloc_zero(mem_ctx
, struct idmap_domain
);
289 if (result
== NULL
) {
290 DEBUG(0, ("talloc failed\n"));
294 result
->name
= talloc_strdup(result
, domainname
);
295 if (result
->name
== NULL
) {
296 DEBUG(0, ("talloc failed\n"));
300 result
->methods
= get_methods(modulename
);
301 if (result
->methods
== NULL
) {
302 DEBUG(3, ("idmap backend %s not found\n", modulename
));
304 status
= smb_probe_module("idmap", modulename
);
305 if (!NT_STATUS_IS_OK(status
)) {
306 DEBUG(3, ("Could not probe idmap module %s\n",
311 result
->methods
= get_methods(modulename
);
313 if (result
->methods
== NULL
) {
314 DEBUG(1, ("idmap backend %s not found\n", modulename
));
318 status
= result
->methods
->init(result
, params
);
319 if (!NT_STATUS_IS_OK(status
)) {
320 DEBUG(1, ("idmap initialization returned %s\n",
325 talloc_set_destructor(result
, close_domain_destructor
);
335 * Initialize the default domain structure
336 * @param[in] mem_ctx memory context for the result
337 * @result The default domain structure
339 * This routine takes the module name from the "idmap backend" parameter,
340 * passing a possible parameter like ldap:ldap://ldap-url/ to the module.
343 static struct idmap_domain
*idmap_init_default_domain(TALLOC_CTX
*mem_ctx
)
345 struct idmap_domain
*result
;
349 DEBUG(10, ("idmap_init_default_domain: calling static_init_idmap\n"));
353 if (!parse_idmap_module(talloc_tos(), lp_idmap_backend(), &modulename
,
355 DEBUG(1, ("parse_idmap_module failed\n"));
359 DEBUG(3, ("idmap_init: using '%s' as remote backend\n", modulename
));
361 result
= idmap_init_domain(mem_ctx
, "*", modulename
, params
);
362 if (result
== NULL
) {
366 TALLOC_FREE(modulename
);
371 TALLOC_FREE(modulename
);
378 * Initialize a named domain structure
379 * @param[in] mem_ctx memory context for the result
380 * @param[in] domname the domain name
381 * @result The default domain structure
383 * This routine looks at the "idmap config <domname>" parameters to figure out
387 static struct idmap_domain
*idmap_init_named_domain(TALLOC_CTX
*mem_ctx
,
390 struct idmap_domain
*result
= NULL
;
394 config_option
= talloc_asprintf(talloc_tos(), "idmap config %s",
396 if (config_option
== NULL
) {
397 DEBUG(0, ("talloc failed\n"));
401 backend
= lp_parm_const_string(-1, config_option
, "backend", NULL
);
402 if (backend
== NULL
) {
403 DEBUG(1, ("no backend defined for %s\n", config_option
));
407 result
= idmap_init_domain(mem_ctx
, domname
, backend
, NULL
);
408 if (result
== NULL
) {
412 TALLOC_FREE(config_option
);
416 TALLOC_FREE(config_option
);
422 * Initialize the passdb domain structure
423 * @param[in] mem_ctx memory context for the result
424 * @result The default domain structure
426 * No config, passdb has its own configuration.
429 static struct idmap_domain
*idmap_init_passdb_domain(TALLOC_CTX
*mem_ctx
)
431 if (passdb_idmap_domain
!= NULL
) {
432 return passdb_idmap_domain
;
435 passdb_idmap_domain
= idmap_init_domain(NULL
, get_global_sam_name(),
437 if (passdb_idmap_domain
== NULL
) {
438 DEBUG(1, ("Could not init passdb idmap domain\n"));
441 return passdb_idmap_domain
;
445 * Find a domain struct according to a domain name
446 * @param[in] domname Domain name to get the config for
447 * @result The default domain structure that fits
449 * This is the central routine in the winbindd-idmap child to pick the correct
450 * domain for looking up IDs. If domname is NULL or empty, we use the default
451 * domain. If it contains something, we try to use idmap_init_named_domain()
452 * to fetch the correct backend.
454 * The choice about "domname" is being made by the winbind parent, look at the
455 * "have_idmap_config" of "struct winbindd_domain" which is set in
456 * add_trusted_domain.
459 static struct idmap_domain
*idmap_find_domain(const char *domname
)
461 struct idmap_domain
*result
;
465 * Always init the default domain, we can't go without one
467 if (default_idmap_domain
== NULL
) {
468 default_idmap_domain
= idmap_init_default_domain(NULL
);
470 if (default_idmap_domain
== NULL
) {
474 if ((domname
== NULL
) || (domname
[0] == '\0')) {
475 return default_idmap_domain
;
478 for (i
=0; i
<num_domains
; i
++) {
479 if (strequal(idmap_domains
[i
]->name
, domname
)) {
480 return idmap_domains
[i
];
484 if (idmap_domains
== NULL
) {
486 * talloc context for all idmap domains
488 idmap_domains
= TALLOC_ARRAY(NULL
, struct idmap_domain
*, 1);
491 if (idmap_domains
== NULL
) {
492 DEBUG(0, ("talloc failed\n"));
496 result
= idmap_init_named_domain(idmap_domains
, domname
);
497 if (result
== NULL
) {
499 * Could not init that domain -- try the default one
501 return default_idmap_domain
;
504 ADD_TO_ARRAY(idmap_domains
, struct idmap_domain
*, result
,
505 &idmap_domains
, &num_domains
);
509 void idmap_close(void)
511 if (idmap_alloc_ctx
) {
512 idmap_alloc_ctx
->methods
->close_fn();
513 idmap_alloc_ctx
->methods
= NULL
;
515 alloc_backends
= NULL
;
516 TALLOC_FREE(default_idmap_domain
);
517 TALLOC_FREE(passdb_idmap_domain
);
518 TALLOC_FREE(idmap_domains
);
523 * Initialize the idmap alloc backend
524 * @param[out] ctx Where to put the alloc_ctx?
525 * @result Did it work fine?
527 * This routine first looks at "idmap alloc backend" and if that is not
528 * defined, it uses "idmap backend" for the module name.
530 static NTSTATUS
idmap_alloc_init(struct idmap_alloc_context
**ctx
)
533 char *modulename
, *params
;
534 NTSTATUS ret
= NT_STATUS_NO_MEMORY
;;
536 if (idmap_alloc_ctx
!= NULL
) {
537 *ctx
= idmap_alloc_ctx
;
541 idmap_alloc_ctx
= talloc(NULL
, struct idmap_alloc_context
);
542 if (idmap_alloc_ctx
== NULL
) {
543 DEBUG(0, ("talloc failed\n"));
547 backend
= lp_idmap_alloc_backend();
548 if ((backend
== NULL
) || (backend
[0] == '\0')) {
549 backend
= lp_idmap_backend();
552 if (backend
== NULL
) {
553 DEBUG(3, ("no idmap alloc backend defined\n"));
554 ret
= NT_STATUS_INVALID_PARAMETER
;
558 if (!parse_idmap_module(idmap_alloc_ctx
, backend
, &modulename
,
560 DEBUG(1, ("parse_idmap_module %s failed\n", backend
));
564 idmap_alloc_ctx
->methods
= get_alloc_methods(modulename
);
566 if (idmap_alloc_ctx
->methods
== NULL
) {
567 ret
= smb_probe_module("idmap", modulename
);
568 if (NT_STATUS_IS_OK(ret
)) {
569 idmap_alloc_ctx
->methods
=
570 get_alloc_methods(modulename
);
574 if (idmap_alloc_ctx
->methods
== NULL
) {
575 DEBUG(1, ("could not find idmap alloc module %s\n", backend
));
576 ret
= NT_STATUS_INVALID_PARAMETER
;
580 ret
= idmap_alloc_ctx
->methods
->init(params
);
582 if (!NT_STATUS_IS_OK(ret
)) {
583 DEBUG(0, ("ERROR: Initialization failed for alloc "
584 "backend, deferred!\n"));
588 TALLOC_FREE(modulename
);
591 *ctx
= idmap_alloc_ctx
;
595 TALLOC_FREE(idmap_alloc_ctx
);
599 /**************************************************************************
600 idmap allocator interface functions
601 **************************************************************************/
603 NTSTATUS
idmap_allocate_uid(struct unixid
*id
)
605 struct idmap_alloc_context
*ctx
;
608 if (!NT_STATUS_IS_OK(ret
= idmap_alloc_init(&ctx
))) {
612 id
->type
= ID_TYPE_UID
;
613 return ctx
->methods
->allocate_id(id
);
616 NTSTATUS
idmap_allocate_gid(struct unixid
*id
)
618 struct idmap_alloc_context
*ctx
;
621 if (!NT_STATUS_IS_OK(ret
= idmap_alloc_init(&ctx
))) {
625 id
->type
= ID_TYPE_GID
;
626 return ctx
->methods
->allocate_id(id
);
629 NTSTATUS
idmap_set_uid_hwm(struct unixid
*id
)
631 struct idmap_alloc_context
*ctx
;
634 if (!NT_STATUS_IS_OK(ret
= idmap_alloc_init(&ctx
))) {
638 id
->type
= ID_TYPE_UID
;
639 return ctx
->methods
->set_id_hwm(id
);
642 NTSTATUS
idmap_set_gid_hwm(struct unixid
*id
)
644 struct idmap_alloc_context
*ctx
;
647 if (!NT_STATUS_IS_OK(ret
= idmap_alloc_init(&ctx
))) {
651 id
->type
= ID_TYPE_GID
;
652 return ctx
->methods
->set_id_hwm(id
);
655 NTSTATUS
idmap_new_mapping(const struct dom_sid
*psid
, enum id_type type
,
659 struct idmap_domain
*dom
;
663 dom
= idmap_find_domain(NULL
);
665 DEBUG(3, ("no default domain, no place to write\n"));
666 return NT_STATUS_ACCESS_DENIED
;
668 if (dom
->methods
->set_mapping
== NULL
) {
669 DEBUG(3, ("default domain not writable\n"));
670 return NT_STATUS_MEDIA_WRITE_PROTECTED
;
673 sid_copy(&sid
, psid
);
679 status
= idmap_allocate_uid(&map
.xid
);
682 status
= idmap_allocate_gid(&map
.xid
);
685 status
= NT_STATUS_INVALID_PARAMETER
;
689 if (!NT_STATUS_IS_OK(status
)) {
690 DEBUG(3, ("Could not allocate id: %s\n", nt_errstr(status
)));
694 map
.status
= ID_MAPPED
;
696 DEBUG(10, ("Setting mapping: %s <-> %s %lu\n",
697 sid_string_dbg(map
.sid
),
698 (map
.xid
.type
== ID_TYPE_UID
) ? "UID" : "GID",
699 (unsigned long)map
.xid
.id
));
701 status
= dom
->methods
->set_mapping(dom
, &map
);
703 if (NT_STATUS_EQUAL(status
, NT_STATUS_OBJECT_NAME_COLLISION
)) {
704 struct id_map
*ids
[2];
705 DEBUG(5, ("Mapping for %s exists - retrying to map sid\n",
706 sid_string_dbg(map
.sid
)));
709 status
= dom
->methods
->sids_to_unixids(dom
, ids
);
712 if (!NT_STATUS_IS_OK(status
)) {
713 DEBUG(3, ("Could not store the new mapping: %s\n",
723 NTSTATUS
idmap_backends_unixid_to_sid(const char *domname
, struct id_map
*id
)
725 struct idmap_domain
*dom
;
726 struct id_map
*maps
[2];
732 * Always give passdb a chance first
735 dom
= idmap_init_passdb_domain(NULL
);
737 && NT_STATUS_IS_OK(dom
->methods
->unixids_to_sids(dom
, maps
))) {
741 dom
= idmap_find_domain(domname
);
743 return NT_STATUS_NONE_MAPPED
;
746 return dom
->methods
->unixids_to_sids(dom
, maps
);
749 NTSTATUS
idmap_backends_sid_to_unixid(const char *domain
, struct id_map
*id
)
751 struct idmap_domain
*dom
;
752 struct id_map
*maps
[2];
757 if (sid_check_is_in_builtin(id
->sid
)
758 || (sid_check_is_in_our_domain(id
->sid
))) {
760 dom
= idmap_init_passdb_domain(NULL
);
762 return NT_STATUS_NONE_MAPPED
;
764 return dom
->methods
->sids_to_unixids(dom
, maps
);
767 dom
= idmap_find_domain(domain
);
769 return NT_STATUS_NONE_MAPPED
;
772 return dom
->methods
->sids_to_unixids(dom
, maps
);
775 NTSTATUS
idmap_set_mapping(const struct id_map
*map
)
777 struct idmap_domain
*dom
;
779 dom
= idmap_find_domain(NULL
);
781 DEBUG(3, ("no default domain, no place to write\n"));
782 return NT_STATUS_ACCESS_DENIED
;
784 if (dom
->methods
->set_mapping
== NULL
) {
785 DEBUG(3, ("default domain not writable\n"));
786 return NT_STATUS_MEDIA_WRITE_PROTECTED
;
789 return dom
->methods
->set_mapping(dom
, map
);
792 NTSTATUS
idmap_remove_mapping(const struct id_map
*map
)
794 struct idmap_domain
*dom
;
796 dom
= idmap_find_domain(NULL
);
798 DEBUG(3, ("no default domain, no place to write\n"));
799 return NT_STATUS_ACCESS_DENIED
;
801 if (dom
->methods
->remove_mapping
== NULL
) {
802 DEBUG(3, ("default domain not writable\n"));
803 return NT_STATUS_MEDIA_WRITE_PROTECTED
;
806 return dom
->methods
->remove_mapping(dom
, map
);