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 * Default idmap domain configured via "idmap backend".
46 static struct idmap_domain *default_idmap_domain;
49 * Passdb idmap domain, not configurable. winbind must always give passdb a
52 static struct idmap_domain *passdb_idmap_domain;
55 * List of specially configured idmap domains. This list is filled on demand
56 * in the winbind idmap child when the parent winbind figures out via the
57 * special range parameter or via the domain SID that a special "idmap config
58 * domain" configuration is present.
60 static struct idmap_domain **idmap_domains = NULL;
61 static int num_domains = 0;
63 static struct idmap_methods *get_methods(const char *name)
65 struct idmap_backend *b;
67 for (b = backends; b; b = b->next) {
68 if (strequal(b->name, name)) {
76 bool idmap_is_offline(void)
78 return ( lp_winbind_offline_logon() &&
79 get_global_winbindd_state_offline() );
82 bool idmap_is_online(void)
84 return !idmap_is_offline();
87 /**********************************************************************
88 Allow a module to register itself as a method.
89 **********************************************************************/
91 NTSTATUS smb_register_idmap(int version, const char *name,
92 struct idmap_methods *methods)
94 struct idmap_backend *entry;
96 if ((version != SMB_IDMAP_INTERFACE_VERSION)) {
97 DEBUG(0, ("Failed to register idmap module.\n"
98 "The module was compiled against "
99 "SMB_IDMAP_INTERFACE_VERSION %d,\n"
100 "current SMB_IDMAP_INTERFACE_VERSION is %d.\n"
101 "Please recompile against the current version "
103 version, SMB_IDMAP_INTERFACE_VERSION));
104 return NT_STATUS_OBJECT_TYPE_MISMATCH;
107 if (!name || !name[0] || !methods) {
108 DEBUG(0,("Called with NULL pointer or empty name!\n"));
109 return NT_STATUS_INVALID_PARAMETER;
112 for (entry = backends; entry != NULL; entry = entry->next) {
113 if (strequal(entry->name, name)) {
114 DEBUG(0,("Idmap module %s already registered!\n",
116 return NT_STATUS_OBJECT_NAME_COLLISION;
120 entry = talloc(NULL, struct idmap_backend);
122 DEBUG(0,("Out of memory!\n"));
124 return NT_STATUS_NO_MEMORY;
126 entry->name = talloc_strdup(entry, name);
127 if ( ! entry->name) {
128 DEBUG(0,("Out of memory!\n"));
130 return NT_STATUS_NO_MEMORY;
132 entry->methods = methods;
134 DLIST_ADD(backends, entry);
135 DEBUG(5, ("Successfully added idmap backend '%s'\n", name));
139 static int close_domain_destructor(struct idmap_domain *dom)
143 ret = dom->methods->close_fn(dom);
144 if (!NT_STATUS_IS_OK(ret)) {
145 DEBUG(3, ("Failed to close idmap domain [%s]!\n", dom->name));
151 static bool parse_idmap_module(TALLOC_CTX *mem_ctx, const char *param,
152 char **pmodulename, char **pargs)
157 if (strncmp(param, "idmap_", 6) == 0) {
159 DEBUG(1, ("idmap_init: idmap backend uses deprecated "
160 "'idmap_' prefix. Please replace 'idmap_%s' by "
161 "'%s'\n", param, param));
164 modulename = talloc_strdup(mem_ctx, param);
165 if (modulename == NULL) {
169 args = strchr(modulename, ':');
171 *pmodulename = modulename;
178 args = talloc_strdup(mem_ctx, args+1);
180 TALLOC_FREE(modulename);
184 *pmodulename = modulename;
190 * Initialize a domain structure
191 * @param[in] mem_ctx memory context for the result
192 * @param[in] domainname which domain is this for
193 * @param[in] modulename which backend module
194 * @param[in] params parameter to pass to the init function
195 * @param[in] check_range whether range checking should be done
196 * @result The initialized structure
198 static struct idmap_domain *idmap_init_domain(TALLOC_CTX *mem_ctx,
199 const char *domainname,
200 const char *modulename,
204 struct idmap_domain *result;
207 result = talloc_zero(mem_ctx, struct idmap_domain);
208 if (result == NULL) {
209 DEBUG(0, ("talloc failed\n"));
213 result->name = talloc_strdup(result, domainname);
214 if (result->name == NULL) {
215 DEBUG(0, ("talloc failed\n"));
220 * load ranges and read only information from the config
222 if (strequal(result->name, "*")) {
224 * The default domain "*" is configured differently
225 * from named domains.
235 if (!lp_idmap_uid(&low_uid, &high_uid)) {
236 DEBUG(1, ("'idmap uid' not set!\n"));
242 result->low_id = low_uid;
243 result->high_id = high_uid;
245 if (!lp_idmap_gid(&low_gid, &high_gid)) {
246 DEBUG(1, ("'idmap gid' not set!\n"));
252 if ((low_gid != low_uid) || (high_gid != high_uid)) {
253 DEBUG(1, ("Warning: 'idmap uid' and 'idmap gid'"
254 " ranges do not agree -- building "
256 result->low_id = MAX(result->low_id, low_gid);
257 result->high_id = MIN(result->high_id, high_gid);
260 result->read_only = lp_idmap_read_only();
262 char *config_option = NULL;
265 config_option = talloc_asprintf(result, "idmap config %s",
267 if (config_option == NULL) {
268 DEBUG(0, ("Out of memory!\n"));
272 range = lp_parm_const_string(-1, config_option, "range", NULL);
274 DEBUG(1, ("idmap range not specified for domain %s\n",
279 } else if (sscanf(range, "%u - %u", &result->low_id,
280 &result->high_id) != 2)
282 DEBUG(1, ("invalid range '%s' specified for domain "
283 "'%s'\n", range, result->name));
289 result->read_only = lp_parm_bool(-1, config_option, "read only",
292 talloc_free(config_option);
295 if (result->low_id > result->high_id) {
296 DEBUG(1, ("Error: invalid idmap range detected: %lu - %lu\n",
297 (unsigned long)result->low_id,
298 (unsigned long)result->high_id));
304 result->methods = get_methods(modulename);
305 if (result->methods == NULL) {
306 DEBUG(3, ("idmap backend %s not found\n", modulename));
308 status = smb_probe_module("idmap", modulename);
309 if (!NT_STATUS_IS_OK(status)) {
310 DEBUG(3, ("Could not probe idmap module %s\n",
315 result->methods = get_methods(modulename);
317 if (result->methods == NULL) {
318 DEBUG(1, ("idmap backend %s not found\n", modulename));
322 status = result->methods->init(result, params);
323 if (!NT_STATUS_IS_OK(status)) {
324 DEBUG(1, ("idmap initialization returned %s\n",
329 talloc_set_destructor(result, close_domain_destructor);
339 * Initialize the default domain structure
340 * @param[in] mem_ctx memory context for the result
341 * @result The default domain structure
343 * This routine takes the module name from the "idmap backend" parameter,
344 * passing a possible parameter like ldap:ldap://ldap-url/ to the module.
347 static struct idmap_domain *idmap_init_default_domain(TALLOC_CTX *mem_ctx)
349 struct idmap_domain *result;
353 DEBUG(10, ("idmap_init_default_domain: calling static_init_idmap\n"));
357 if (!parse_idmap_module(talloc_tos(), lp_idmap_backend(), &modulename,
359 DEBUG(1, ("parse_idmap_module failed\n"));
363 DEBUG(3, ("idmap_init: using '%s' as remote backend\n", modulename));
365 result = idmap_init_domain(mem_ctx, "*", modulename, params, true);
366 if (result == NULL) {
370 TALLOC_FREE(modulename);
375 TALLOC_FREE(modulename);
382 * Initialize a named domain structure
383 * @param[in] mem_ctx memory context for the result
384 * @param[in] domname the domain name
385 * @result The default domain structure
387 * This routine looks at the "idmap config <domname>" parameters to figure out
391 static struct idmap_domain *idmap_init_named_domain(TALLOC_CTX *mem_ctx,
394 struct idmap_domain *result = NULL;
398 config_option = talloc_asprintf(talloc_tos(), "idmap config %s",
400 if (config_option == NULL) {
401 DEBUG(0, ("talloc failed\n"));
405 backend = lp_parm_const_string(-1, config_option, "backend", NULL);
406 if (backend == NULL) {
407 DEBUG(1, ("no backend defined for %s\n", config_option));
411 result = idmap_init_domain(mem_ctx, domname, backend, NULL, true);
412 if (result == NULL) {
416 TALLOC_FREE(config_option);
420 TALLOC_FREE(config_option);
426 * Initialize the passdb domain structure
427 * @param[in] mem_ctx memory context for the result
428 * @result The default domain structure
430 * No config, passdb has its own configuration.
433 static struct idmap_domain *idmap_init_passdb_domain(TALLOC_CTX *mem_ctx)
435 DEBUG(10, ("idmap_init_passdb_domain: calling static_init_idmap\n"));
439 if (passdb_idmap_domain != NULL) {
440 return passdb_idmap_domain;
443 passdb_idmap_domain = idmap_init_domain(NULL, get_global_sam_name(),
444 "passdb", NULL, false);
445 if (passdb_idmap_domain == NULL) {
446 DEBUG(1, ("Could not init passdb idmap domain\n"));
449 return passdb_idmap_domain;
453 * Find a domain struct according to a domain name
454 * @param[in] domname Domain name to get the config for
455 * @result The default domain structure that fits
457 * This is the central routine in the winbindd-idmap child to pick the correct
458 * domain for looking up IDs. If domname is NULL or empty, we use the default
459 * domain. If it contains something, we try to use idmap_init_named_domain()
460 * to fetch the correct backend.
462 * The choice about "domname" is being made by the winbind parent, look at the
463 * "have_idmap_config" of "struct winbindd_domain" which is set in
464 * add_trusted_domain.
467 static struct idmap_domain *idmap_find_domain(const char *domname)
469 struct idmap_domain *result;
472 DEBUG(10, ("idmap_find_domain called for domain '%s'\n",
473 domname?domname:"NULL"));
476 * Always init the default domain, we can't go without one
478 if (default_idmap_domain == NULL) {
479 default_idmap_domain = idmap_init_default_domain(NULL);
481 if (default_idmap_domain == NULL) {
485 if ((domname == NULL) || (domname[0] == '\0')) {
486 return default_idmap_domain;
489 for (i=0; i<num_domains; i++) {
490 if (strequal(idmap_domains[i]->name, domname)) {
491 return idmap_domains[i];
495 if (idmap_domains == NULL) {
497 * talloc context for all idmap domains
499 idmap_domains = TALLOC_ARRAY(NULL, struct idmap_domain *, 1);
502 if (idmap_domains == NULL) {
503 DEBUG(0, ("talloc failed\n"));
507 result = idmap_init_named_domain(idmap_domains, domname);
508 if (result == NULL) {
510 * Could not init that domain -- try the default one
512 return default_idmap_domain;
515 ADD_TO_ARRAY(idmap_domains, struct idmap_domain *, result,
516 &idmap_domains, &num_domains);
520 void idmap_close(void)
522 TALLOC_FREE(default_idmap_domain);
523 TALLOC_FREE(passdb_idmap_domain);
524 TALLOC_FREE(idmap_domains);
528 /**************************************************************************
529 idmap allocator interface functions
530 **************************************************************************/
532 static NTSTATUS idmap_allocate_unixid(struct unixid *id)
534 struct idmap_domain *dom;
537 dom = idmap_find_domain(NULL);
540 return NT_STATUS_UNSUCCESSFUL;
543 if (dom->methods->allocate_id == NULL) {
544 return NT_STATUS_NOT_IMPLEMENTED;
547 ret = dom->methods->allocate_id(dom, id);
553 NTSTATUS idmap_allocate_uid(struct unixid *id)
555 id->type = ID_TYPE_UID;
556 return idmap_allocate_unixid(id);
559 NTSTATUS idmap_allocate_gid(struct unixid *id)
561 id->type = ID_TYPE_GID;
562 return idmap_allocate_unixid(id);
565 NTSTATUS idmap_backends_unixid_to_sid(const char *domname, struct id_map *id)
567 struct idmap_domain *dom;
568 struct id_map *maps[2];
570 DEBUG(10, ("idmap_backend_unixid_to_sid: domain = '%s', xid = %d "
572 domname?domname:"NULL", id->xid.id, id->xid.type));
578 * Always give passdb a chance first
581 dom = idmap_init_passdb_domain(NULL);
583 && NT_STATUS_IS_OK(dom->methods->unixids_to_sids(dom, maps))
584 && id->status == ID_MAPPED) {
588 dom = idmap_find_domain(domname);
590 return NT_STATUS_NONE_MAPPED;
593 return dom->methods->unixids_to_sids(dom, maps);
596 NTSTATUS idmap_backends_sid_to_unixid(const char *domain, struct id_map *id)
598 struct idmap_domain *dom;
599 struct id_map *maps[2];
601 DEBUG(10, ("idmap_backends_sid_to_unixid: domain = '%s', sid = [%s]\n",
602 domain?domain:"NULL", sid_string_dbg(id->sid)));
607 if (sid_check_is_in_builtin(id->sid)
608 || (sid_check_is_in_our_domain(id->sid))) {
610 dom = idmap_init_passdb_domain(NULL);
612 return NT_STATUS_NONE_MAPPED;
614 return dom->methods->sids_to_unixids(dom, maps);
617 dom = idmap_find_domain(domain);
619 return NT_STATUS_NONE_MAPPED;
622 return dom->methods->sids_to_unixids(dom, maps);