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
8 Copyright (C) Michael Adam 2010
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
29 #define DBGC_CLASS DBGC_IDMAP
33 static void idmap_init(void)
35 static bool initialized
;
41 DEBUG(10, ("idmap_init(): calling static_init_idmap\n"));
49 * Pointer to the backend methods. Modules register themselves here via
53 struct idmap_backend
{
55 struct idmap_methods
*methods
;
56 struct idmap_backend
*prev
, *next
;
58 static struct idmap_backend
*backends
= NULL
;
61 * Default idmap domain configured via "idmap backend".
63 static struct idmap_domain
*default_idmap_domain
;
66 * Passdb idmap domain, not configurable. winbind must always give passdb a
69 static struct idmap_domain
*passdb_idmap_domain
;
72 * List of specially configured idmap domains. This list is filled on demand
73 * in the winbind idmap child when the parent winbind figures out via the
74 * special range parameter or via the domain SID that a special "idmap config
75 * domain" configuration is present.
77 static struct idmap_domain
**idmap_domains
= NULL
;
78 static int num_domains
= 0;
80 static struct idmap_methods
*get_methods(const char *name
)
82 struct idmap_backend
*b
;
84 for (b
= backends
; b
; b
= b
->next
) {
85 if (strequal(b
->name
, name
)) {
93 bool idmap_is_offline(void)
95 return ( lp_winbind_offline_logon() &&
96 get_global_winbindd_state_offline() );
99 bool idmap_is_online(void)
101 return !idmap_is_offline();
104 /**********************************************************************
105 Allow a module to register itself as a method.
106 **********************************************************************/
108 NTSTATUS
smb_register_idmap(int version
, const char *name
,
109 struct idmap_methods
*methods
)
111 struct idmap_backend
*entry
;
113 if ((version
!= SMB_IDMAP_INTERFACE_VERSION
)) {
114 DEBUG(0, ("Failed to register idmap module.\n"
115 "The module was compiled against "
116 "SMB_IDMAP_INTERFACE_VERSION %d,\n"
117 "current SMB_IDMAP_INTERFACE_VERSION is %d.\n"
118 "Please recompile against the current version "
120 version
, SMB_IDMAP_INTERFACE_VERSION
));
121 return NT_STATUS_OBJECT_TYPE_MISMATCH
;
124 if (!name
|| !name
[0] || !methods
) {
125 DEBUG(0,("Called with NULL pointer or empty name!\n"));
126 return NT_STATUS_INVALID_PARAMETER
;
129 for (entry
= backends
; entry
!= NULL
; entry
= entry
->next
) {
130 if (strequal(entry
->name
, name
)) {
131 DEBUG(0,("Idmap module %s already registered!\n",
133 return NT_STATUS_OBJECT_NAME_COLLISION
;
137 entry
= talloc(NULL
, struct idmap_backend
);
139 DEBUG(0,("Out of memory!\n"));
141 return NT_STATUS_NO_MEMORY
;
143 entry
->name
= talloc_strdup(entry
, name
);
144 if ( ! entry
->name
) {
145 DEBUG(0,("Out of memory!\n"));
147 return NT_STATUS_NO_MEMORY
;
149 entry
->methods
= methods
;
151 DLIST_ADD(backends
, entry
);
152 DEBUG(5, ("Successfully added idmap backend '%s'\n", name
));
156 static int close_domain_destructor(struct idmap_domain
*dom
)
160 ret
= dom
->methods
->close_fn(dom
);
161 if (!NT_STATUS_IS_OK(ret
)) {
162 DEBUG(3, ("Failed to close idmap domain [%s]!\n", dom
->name
));
168 static bool parse_idmap_module(TALLOC_CTX
*mem_ctx
, const char *param
,
169 char **pmodulename
, char **pargs
)
174 if (strncmp(param
, "idmap_", 6) == 0) {
176 DEBUG(1, ("idmap_init: idmap backend uses deprecated "
177 "'idmap_' prefix. Please replace 'idmap_%s' by "
178 "'%s'\n", param
, param
));
181 modulename
= talloc_strdup(mem_ctx
, param
);
182 if (modulename
== NULL
) {
186 args
= strchr(modulename
, ':');
188 *pmodulename
= modulename
;
195 args
= talloc_strdup(mem_ctx
, args
+1);
197 TALLOC_FREE(modulename
);
201 *pmodulename
= modulename
;
207 * Initialize a domain structure
208 * @param[in] mem_ctx memory context for the result
209 * @param[in] domainname which domain is this for
210 * @param[in] modulename which backend module
211 * @param[in] params parameter to pass to the init function
212 * @param[in] check_range whether range checking should be done
213 * @result The initialized structure
215 static struct idmap_domain
*idmap_init_domain(TALLOC_CTX
*mem_ctx
,
216 const char *domainname
,
217 const char *modulename
,
221 struct idmap_domain
*result
;
224 result
= talloc_zero(mem_ctx
, struct idmap_domain
);
225 if (result
== NULL
) {
226 DEBUG(0, ("talloc failed\n"));
230 result
->name
= talloc_strdup(result
, domainname
);
231 if (result
->name
== NULL
) {
232 DEBUG(0, ("talloc failed\n"));
237 * load ranges and read only information from the config
239 if (strequal(result
->name
, "*")) {
241 * The default domain "*" is configured differently
242 * from named domains.
252 if (!lp_idmap_uid(&low_uid
, &high_uid
)) {
253 DEBUG(1, ("'idmap uid' not set!\n"));
259 result
->low_id
= low_uid
;
260 result
->high_id
= high_uid
;
262 if (!lp_idmap_gid(&low_gid
, &high_gid
)) {
263 DEBUG(1, ("'idmap gid' not set!\n"));
269 if ((low_gid
!= low_uid
) || (high_gid
!= high_uid
)) {
270 DEBUG(1, ("Warning: 'idmap uid' and 'idmap gid'"
271 " ranges do not agree -- building "
273 result
->low_id
= MAX(result
->low_id
, low_gid
);
274 result
->high_id
= MIN(result
->high_id
, high_gid
);
277 result
->read_only
= lp_idmap_read_only();
279 char *config_option
= NULL
;
282 config_option
= talloc_asprintf(result
, "idmap config %s",
284 if (config_option
== NULL
) {
285 DEBUG(0, ("Out of memory!\n"));
289 range
= lp_parm_const_string(-1, config_option
, "range", NULL
);
291 DEBUG(1, ("idmap range not specified for domain %s\n",
296 } else if (sscanf(range
, "%u - %u", &result
->low_id
,
297 &result
->high_id
) != 2)
299 DEBUG(1, ("invalid range '%s' specified for domain "
300 "'%s'\n", range
, result
->name
));
306 result
->read_only
= lp_parm_bool(-1, config_option
, "read only",
309 talloc_free(config_option
);
312 if (result
->low_id
> result
->high_id
) {
313 DEBUG(1, ("Error: invalid idmap range detected: %lu - %lu\n",
314 (unsigned long)result
->low_id
,
315 (unsigned long)result
->high_id
));
321 result
->methods
= get_methods(modulename
);
322 if (result
->methods
== NULL
) {
323 DEBUG(3, ("idmap backend %s not found\n", modulename
));
325 status
= smb_probe_module("idmap", modulename
);
326 if (!NT_STATUS_IS_OK(status
)) {
327 DEBUG(3, ("Could not probe idmap module %s\n",
332 result
->methods
= get_methods(modulename
);
334 if (result
->methods
== NULL
) {
335 DEBUG(1, ("idmap backend %s not found\n", modulename
));
339 status
= result
->methods
->init(result
, params
);
340 if (!NT_STATUS_IS_OK(status
)) {
341 DEBUG(1, ("idmap initialization returned %s\n",
346 talloc_set_destructor(result
, close_domain_destructor
);
356 * Initialize the default domain structure
357 * @param[in] mem_ctx memory context for the result
358 * @result The default domain structure
360 * This routine takes the module name from the "idmap backend" parameter,
361 * passing a possible parameter like ldap:ldap://ldap-url/ to the module.
364 static struct idmap_domain
*idmap_init_default_domain(TALLOC_CTX
*mem_ctx
)
366 struct idmap_domain
*result
;
372 if (!parse_idmap_module(talloc_tos(), lp_idmap_backend(), &modulename
,
374 DEBUG(1, ("parse_idmap_module failed\n"));
378 DEBUG(3, ("idmap_init: using '%s' as remote backend\n", modulename
));
380 result
= idmap_init_domain(mem_ctx
, "*", modulename
, params
, true);
381 if (result
== NULL
) {
385 TALLOC_FREE(modulename
);
390 TALLOC_FREE(modulename
);
397 * Initialize a named domain structure
398 * @param[in] mem_ctx memory context for the result
399 * @param[in] domname the domain name
400 * @result The default domain structure
402 * This routine looks at the "idmap config <domname>" parameters to figure out
406 static struct idmap_domain
*idmap_init_named_domain(TALLOC_CTX
*mem_ctx
,
409 struct idmap_domain
*result
= NULL
;
413 config_option
= talloc_asprintf(talloc_tos(), "idmap config %s",
415 if (config_option
== NULL
) {
416 DEBUG(0, ("talloc failed\n"));
420 backend
= lp_parm_const_string(-1, config_option
, "backend", NULL
);
421 if (backend
== NULL
) {
422 DEBUG(1, ("no backend defined for %s\n", config_option
));
426 result
= idmap_init_domain(mem_ctx
, domname
, backend
, NULL
, true);
427 if (result
== NULL
) {
431 TALLOC_FREE(config_option
);
435 TALLOC_FREE(config_option
);
441 * Initialize the passdb domain structure
442 * @param[in] mem_ctx memory context for the result
443 * @result The default domain structure
445 * No config, passdb has its own configuration.
448 static struct idmap_domain
*idmap_init_passdb_domain(TALLOC_CTX
*mem_ctx
)
453 * Always init the default domain, we can't go without one
455 if (default_idmap_domain
== NULL
) {
456 default_idmap_domain
= idmap_init_default_domain(NULL
);
458 if (default_idmap_domain
== NULL
) {
462 if (passdb_idmap_domain
!= NULL
) {
463 return passdb_idmap_domain
;
466 passdb_idmap_domain
= idmap_init_domain(NULL
, get_global_sam_name(),
467 "passdb", NULL
, false);
468 if (passdb_idmap_domain
== NULL
) {
469 DEBUG(1, ("Could not init passdb idmap domain\n"));
472 return passdb_idmap_domain
;
476 * Find a domain struct according to a domain name
477 * @param[in] domname Domain name to get the config for
478 * @result The default domain structure that fits
480 * This is the central routine in the winbindd-idmap child to pick the correct
481 * domain for looking up IDs. If domname is NULL or empty, we use the default
482 * domain. If it contains something, we try to use idmap_init_named_domain()
483 * to fetch the correct backend.
485 * The choice about "domname" is being made by the winbind parent, look at the
486 * "have_idmap_config" of "struct winbindd_domain" which is set in
487 * add_trusted_domain.
490 static struct idmap_domain
*idmap_find_domain(const char *domname
)
492 struct idmap_domain
*result
;
495 DEBUG(10, ("idmap_find_domain called for domain '%s'\n",
496 domname
?domname
:"NULL"));
499 * Always init the default domain, we can't go without one
501 if (default_idmap_domain
== NULL
) {
502 default_idmap_domain
= idmap_init_default_domain(NULL
);
504 if (default_idmap_domain
== NULL
) {
508 if ((domname
== NULL
) || (domname
[0] == '\0')) {
509 return default_idmap_domain
;
512 for (i
=0; i
<num_domains
; i
++) {
513 if (strequal(idmap_domains
[i
]->name
, domname
)) {
514 return idmap_domains
[i
];
518 if (idmap_domains
== NULL
) {
520 * talloc context for all idmap domains
522 idmap_domains
= TALLOC_ARRAY(NULL
, struct idmap_domain
*, 1);
525 if (idmap_domains
== NULL
) {
526 DEBUG(0, ("talloc failed\n"));
530 result
= idmap_init_named_domain(idmap_domains
, domname
);
531 if (result
== NULL
) {
533 * Could not init that domain -- try the default one
535 return default_idmap_domain
;
538 ADD_TO_ARRAY(idmap_domains
, struct idmap_domain
*, result
,
539 &idmap_domains
, &num_domains
);
543 void idmap_close(void)
545 TALLOC_FREE(default_idmap_domain
);
546 TALLOC_FREE(passdb_idmap_domain
);
547 TALLOC_FREE(idmap_domains
);
551 /**************************************************************************
552 idmap allocator interface functions
553 **************************************************************************/
555 static NTSTATUS
idmap_allocate_unixid(struct unixid
*id
)
557 struct idmap_domain
*dom
;
560 dom
= idmap_find_domain(NULL
);
563 return NT_STATUS_UNSUCCESSFUL
;
566 if (dom
->methods
->allocate_id
== NULL
) {
567 return NT_STATUS_NOT_IMPLEMENTED
;
570 ret
= dom
->methods
->allocate_id(dom
, id
);
576 NTSTATUS
idmap_allocate_uid(struct unixid
*id
)
578 id
->type
= ID_TYPE_UID
;
579 return idmap_allocate_unixid(id
);
582 NTSTATUS
idmap_allocate_gid(struct unixid
*id
)
584 id
->type
= ID_TYPE_GID
;
585 return idmap_allocate_unixid(id
);
588 NTSTATUS
idmap_backends_unixid_to_sid(const char *domname
, struct id_map
*id
)
590 struct idmap_domain
*dom
;
591 struct id_map
*maps
[2];
593 DEBUG(10, ("idmap_backend_unixid_to_sid: domain = '%s', xid = %d "
595 domname
?domname
:"NULL", id
->xid
.id
, id
->xid
.type
));
601 * Always give passdb a chance first
604 dom
= idmap_init_passdb_domain(NULL
);
606 && NT_STATUS_IS_OK(dom
->methods
->unixids_to_sids(dom
, maps
))
607 && id
->status
== ID_MAPPED
) {
611 dom
= idmap_find_domain(domname
);
613 return NT_STATUS_NONE_MAPPED
;
616 return dom
->methods
->unixids_to_sids(dom
, maps
);
619 NTSTATUS
idmap_backends_sid_to_unixid(const char *domain
, struct id_map
*id
)
621 struct idmap_domain
*dom
;
622 struct id_map
*maps
[2];
624 DEBUG(10, ("idmap_backends_sid_to_unixid: domain = '%s', sid = [%s]\n",
625 domain
?domain
:"NULL", sid_string_dbg(id
->sid
)));
630 if (sid_check_is_in_builtin(id
->sid
)
631 || (sid_check_is_in_our_domain(id
->sid
)))
635 DEBUG(10, ("asking passdb...\n"));
637 dom
= idmap_init_passdb_domain(NULL
);
639 return NT_STATUS_NONE_MAPPED
;
641 status
= dom
->methods
->sids_to_unixids(dom
, maps
);
643 if (NT_STATUS_IS_OK(status
) && id
->status
== ID_MAPPED
) {
647 DEBUG(10, ("passdb could not map.\n"));
649 return NT_STATUS_NONE_MAPPED
;
652 dom
= idmap_find_domain(domain
);
654 return NT_STATUS_NONE_MAPPED
;
657 return dom
->methods
->sids_to_unixids(dom
, maps
);