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/>.
28 #define DBGC_CLASS DBGC_IDMAP
32 static void idmap_init(void)
34 static bool initialized;
40 DEBUG(10, ("idmap_init(): calling static_init_idmap\n"));
48 * Pointer to the backend methods. Modules register themselves here via
52 struct idmap_backend {
54 struct idmap_methods *methods;
55 struct idmap_backend *prev, *next;
57 static struct idmap_backend *backends = NULL;
60 * Default idmap domain configured via "idmap backend".
62 static struct idmap_domain *default_idmap_domain;
65 * Passdb idmap domain, not configurable. winbind must always give passdb a
68 static struct idmap_domain *passdb_idmap_domain;
71 * List of specially configured idmap domains. This list is filled on demand
72 * in the winbind idmap child when the parent winbind figures out via the
73 * special range parameter or via the domain SID that a special "idmap config
74 * domain" configuration is present.
76 static struct idmap_domain **idmap_domains = NULL;
77 static int num_domains = 0;
79 static struct idmap_methods *get_methods(const char *name)
81 struct idmap_backend *b;
83 for (b = backends; b; b = b->next) {
84 if (strequal(b->name, name)) {
92 bool idmap_is_offline(void)
94 return ( lp_winbind_offline_logon() &&
95 get_global_winbindd_state_offline() );
98 bool idmap_is_online(void)
100 return !idmap_is_offline();
103 /**********************************************************************
104 Allow a module to register itself as a method.
105 **********************************************************************/
107 NTSTATUS smb_register_idmap(int version, const char *name,
108 struct idmap_methods *methods)
110 struct idmap_backend *entry;
112 if ((version != SMB_IDMAP_INTERFACE_VERSION)) {
113 DEBUG(0, ("Failed to register idmap module.\n"
114 "The module was compiled against "
115 "SMB_IDMAP_INTERFACE_VERSION %d,\n"
116 "current SMB_IDMAP_INTERFACE_VERSION is %d.\n"
117 "Please recompile against the current version "
119 version, SMB_IDMAP_INTERFACE_VERSION));
120 return NT_STATUS_OBJECT_TYPE_MISMATCH;
123 if (!name || !name[0] || !methods) {
124 DEBUG(0,("Called with NULL pointer or empty name!\n"));
125 return NT_STATUS_INVALID_PARAMETER;
128 for (entry = backends; entry != NULL; entry = entry->next) {
129 if (strequal(entry->name, name)) {
130 DEBUG(0,("Idmap module %s already registered!\n",
132 return NT_STATUS_OBJECT_NAME_COLLISION;
136 entry = talloc(NULL, struct idmap_backend);
138 DEBUG(0,("Out of memory!\n"));
140 return NT_STATUS_NO_MEMORY;
142 entry->name = talloc_strdup(entry, name);
143 if ( ! entry->name) {
144 DEBUG(0,("Out of memory!\n"));
146 return NT_STATUS_NO_MEMORY;
148 entry->methods = methods;
150 DLIST_ADD(backends, entry);
151 DEBUG(5, ("Successfully added idmap backend '%s'\n", name));
155 static int close_domain_destructor(struct idmap_domain *dom)
159 ret = dom->methods->close_fn(dom);
160 if (!NT_STATUS_IS_OK(ret)) {
161 DEBUG(3, ("Failed to close idmap domain [%s]!\n", dom->name));
167 static bool parse_idmap_module(TALLOC_CTX *mem_ctx, const char *param,
168 char **pmodulename, char **pargs)
173 if (strncmp(param, "idmap_", 6) == 0) {
175 DEBUG(1, ("idmap_init: idmap backend uses deprecated "
176 "'idmap_' prefix. Please replace 'idmap_%s' by "
177 "'%s'\n", param, param));
180 modulename = talloc_strdup(mem_ctx, param);
181 if (modulename == NULL) {
185 args = strchr(modulename, ':');
187 *pmodulename = modulename;
194 args = talloc_strdup(mem_ctx, args+1);
196 TALLOC_FREE(modulename);
200 *pmodulename = modulename;
206 * Initialize a domain structure
207 * @param[in] mem_ctx memory context for the result
208 * @param[in] domainname which domain is this for
209 * @param[in] modulename which backend module
210 * @param[in] params parameter to pass to the init function
211 * @param[in] check_range whether range checking should be done
212 * @result The initialized structure
214 static struct idmap_domain *idmap_init_domain(TALLOC_CTX *mem_ctx,
215 const char *domainname,
216 const char *modulename,
220 struct idmap_domain *result;
223 result = talloc_zero(mem_ctx, struct idmap_domain);
224 if (result == NULL) {
225 DEBUG(0, ("talloc failed\n"));
229 result->name = talloc_strdup(result, domainname);
230 if (result->name == NULL) {
231 DEBUG(0, ("talloc failed\n"));
236 * load ranges and read only information from the config
238 if (strequal(result->name, "*")) {
240 * The default domain "*" is configured differently
241 * from named domains.
251 if (!lp_idmap_uid(&low_uid, &high_uid)) {
252 DEBUG(1, ("'idmap uid' not set!\n"));
258 result->low_id = low_uid;
259 result->high_id = high_uid;
261 if (!lp_idmap_gid(&low_gid, &high_gid)) {
262 DEBUG(1, ("'idmap gid' not set!\n"));
268 if ((low_gid != low_uid) || (high_gid != high_uid)) {
269 DEBUG(1, ("Warning: 'idmap uid' and 'idmap gid'"
270 " ranges do not agree -- building "
272 result->low_id = MAX(result->low_id, low_gid);
273 result->high_id = MIN(result->high_id, high_gid);
276 result->read_only = lp_idmap_read_only();
278 char *config_option = NULL;
281 config_option = talloc_asprintf(result, "idmap config %s",
283 if (config_option == NULL) {
284 DEBUG(0, ("Out of memory!\n"));
288 range = lp_parm_const_string(-1, config_option, "range", NULL);
290 DEBUG(1, ("idmap range not specified for domain %s\n",
295 } else if (sscanf(range, "%u - %u", &result->low_id,
296 &result->high_id) != 2)
298 DEBUG(1, ("invalid range '%s' specified for domain "
299 "'%s'\n", range, result->name));
305 result->read_only = lp_parm_bool(-1, config_option, "read only",
308 talloc_free(config_option);
311 if (result->low_id > result->high_id) {
312 DEBUG(1, ("Error: invalid idmap range detected: %lu - %lu\n",
313 (unsigned long)result->low_id,
314 (unsigned long)result->high_id));
320 result->methods = get_methods(modulename);
321 if (result->methods == NULL) {
322 DEBUG(3, ("idmap backend %s not found\n", modulename));
324 status = smb_probe_module("idmap", modulename);
325 if (!NT_STATUS_IS_OK(status)) {
326 DEBUG(3, ("Could not probe idmap module %s\n",
331 result->methods = get_methods(modulename);
333 if (result->methods == NULL) {
334 DEBUG(1, ("idmap backend %s not found\n", modulename));
338 status = result->methods->init(result, params);
339 if (!NT_STATUS_IS_OK(status)) {
340 DEBUG(1, ("idmap initialization returned %s\n",
345 talloc_set_destructor(result, close_domain_destructor);
355 * Initialize the default domain structure
356 * @param[in] mem_ctx memory context for the result
357 * @result The default domain structure
359 * This routine takes the module name from the "idmap backend" parameter,
360 * passing a possible parameter like ldap:ldap://ldap-url/ to the module.
363 static struct idmap_domain *idmap_init_default_domain(TALLOC_CTX *mem_ctx)
365 struct idmap_domain *result;
371 if (!parse_idmap_module(talloc_tos(), lp_idmap_backend(), &modulename,
373 DEBUG(1, ("parse_idmap_module failed\n"));
377 DEBUG(3, ("idmap_init: using '%s' as remote backend\n", modulename));
379 result = idmap_init_domain(mem_ctx, "*", modulename, params, true);
380 if (result == NULL) {
384 TALLOC_FREE(modulename);
389 TALLOC_FREE(modulename);
396 * Initialize a named domain structure
397 * @param[in] mem_ctx memory context for the result
398 * @param[in] domname the domain name
399 * @result The default domain structure
401 * This routine looks at the "idmap config <domname>" parameters to figure out
405 static struct idmap_domain *idmap_init_named_domain(TALLOC_CTX *mem_ctx,
408 struct idmap_domain *result = NULL;
412 config_option = talloc_asprintf(talloc_tos(), "idmap config %s",
414 if (config_option == NULL) {
415 DEBUG(0, ("talloc failed\n"));
419 backend = lp_parm_const_string(-1, config_option, "backend", NULL);
420 if (backend == NULL) {
421 DEBUG(1, ("no backend defined for %s\n", config_option));
425 result = idmap_init_domain(mem_ctx, domname, backend, NULL, true);
426 if (result == NULL) {
430 TALLOC_FREE(config_option);
434 TALLOC_FREE(config_option);
440 * Initialize the passdb domain structure
441 * @param[in] mem_ctx memory context for the result
442 * @result The default domain structure
444 * No config, passdb has its own configuration.
447 static struct idmap_domain *idmap_init_passdb_domain(TALLOC_CTX *mem_ctx)
451 if (passdb_idmap_domain != NULL) {
452 return passdb_idmap_domain;
455 passdb_idmap_domain = idmap_init_domain(NULL, get_global_sam_name(),
456 "passdb", NULL, false);
457 if (passdb_idmap_domain == NULL) {
458 DEBUG(1, ("Could not init passdb idmap domain\n"));
461 return passdb_idmap_domain;
465 * Find a domain struct according to a domain name
466 * @param[in] domname Domain name to get the config for
467 * @result The default domain structure that fits
469 * This is the central routine in the winbindd-idmap child to pick the correct
470 * domain for looking up IDs. If domname is NULL or empty, we use the default
471 * domain. If it contains something, we try to use idmap_init_named_domain()
472 * to fetch the correct backend.
474 * The choice about "domname" is being made by the winbind parent, look at the
475 * "have_idmap_config" of "struct winbindd_domain" which is set in
476 * add_trusted_domain.
479 static struct idmap_domain *idmap_find_domain(const char *domname)
481 struct idmap_domain *result;
484 DEBUG(10, ("idmap_find_domain called for domain '%s'\n",
485 domname?domname:"NULL"));
488 * Always init the default domain, we can't go without one
490 if (default_idmap_domain == NULL) {
491 default_idmap_domain = idmap_init_default_domain(NULL);
493 if (default_idmap_domain == NULL) {
497 if ((domname == NULL) || (domname[0] == '\0')) {
498 return default_idmap_domain;
501 for (i=0; i<num_domains; i++) {
502 if (strequal(idmap_domains[i]->name, domname)) {
503 return idmap_domains[i];
507 if (idmap_domains == NULL) {
509 * talloc context for all idmap domains
511 idmap_domains = TALLOC_ARRAY(NULL, struct idmap_domain *, 1);
514 if (idmap_domains == NULL) {
515 DEBUG(0, ("talloc failed\n"));
519 result = idmap_init_named_domain(idmap_domains, domname);
520 if (result == NULL) {
522 * Could not init that domain -- try the default one
524 return default_idmap_domain;
527 ADD_TO_ARRAY(idmap_domains, struct idmap_domain *, result,
528 &idmap_domains, &num_domains);
532 void idmap_close(void)
534 TALLOC_FREE(default_idmap_domain);
535 TALLOC_FREE(passdb_idmap_domain);
536 TALLOC_FREE(idmap_domains);
540 /**************************************************************************
541 idmap allocator interface functions
542 **************************************************************************/
544 static NTSTATUS idmap_allocate_unixid(struct unixid *id)
546 struct idmap_domain *dom;
549 dom = idmap_find_domain(NULL);
552 return NT_STATUS_UNSUCCESSFUL;
555 if (dom->methods->allocate_id == NULL) {
556 return NT_STATUS_NOT_IMPLEMENTED;
559 ret = dom->methods->allocate_id(dom, id);
565 NTSTATUS idmap_allocate_uid(struct unixid *id)
567 id->type = ID_TYPE_UID;
568 return idmap_allocate_unixid(id);
571 NTSTATUS idmap_allocate_gid(struct unixid *id)
573 id->type = ID_TYPE_GID;
574 return idmap_allocate_unixid(id);
577 NTSTATUS idmap_backends_unixid_to_sid(const char *domname, struct id_map *id)
579 struct idmap_domain *dom;
580 struct id_map *maps[2];
582 DEBUG(10, ("idmap_backend_unixid_to_sid: domain = '%s', xid = %d "
584 domname?domname:"NULL", id->xid.id, id->xid.type));
590 * Always give passdb a chance first
593 dom = idmap_init_passdb_domain(NULL);
595 && NT_STATUS_IS_OK(dom->methods->unixids_to_sids(dom, maps))
596 && id->status == ID_MAPPED) {
600 dom = idmap_find_domain(domname);
602 return NT_STATUS_NONE_MAPPED;
605 return dom->methods->unixids_to_sids(dom, maps);
608 NTSTATUS idmap_backends_sid_to_unixid(const char *domain, struct id_map *id)
610 struct idmap_domain *dom;
611 struct id_map *maps[2];
613 DEBUG(10, ("idmap_backends_sid_to_unixid: domain = '%s', sid = [%s]\n",
614 domain?domain:"NULL", sid_string_dbg(id->sid)));
619 if (sid_check_is_in_builtin(id->sid)
620 || (sid_check_is_in_our_domain(id->sid)))
624 DEBUG(10, ("asking passdb...\n"));
626 dom = idmap_init_passdb_domain(NULL);
628 return NT_STATUS_NONE_MAPPED;
630 status = dom->methods->sids_to_unixids(dom, maps);
632 if (NT_STATUS_IS_OK(status) && id->status == ID_MAPPED) {
636 DEBUG(10, ("passdb could not map, asking backends...\n"));
639 dom = idmap_find_domain(domain);
641 return NT_STATUS_NONE_MAPPED;
644 return dom->methods->sids_to_unixids(dom, maps);