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/>.
27 #include "lib/util_sid_passdb.h"
31 #define DBGC_CLASS DBGC_IDMAP
36 * Pointer to the backend methods. Modules register themselves here via
40 struct idmap_backend {
42 struct idmap_methods *methods;
43 struct idmap_backend *prev, *next;
45 static struct idmap_backend *backends = NULL;
48 * Default idmap domain configured via "idmap backend".
50 static struct idmap_domain *default_idmap_domain;
53 * Passdb idmap domain, not configurable. winbind must always give passdb a
56 static struct idmap_domain *passdb_idmap_domain;
59 * List of specially configured idmap domains. This list is filled on demand
60 * in the winbind idmap child when the parent winbind figures out via the
61 * special range parameter or via the domain SID that a special "idmap config
62 * domain" configuration is present.
64 static struct idmap_domain **idmap_domains = NULL;
65 static int num_domains = 0;
67 static struct idmap_domain *idmap_init_named_domain(TALLOC_CTX *mem_ctx,
69 static struct idmap_domain *idmap_init_domain(TALLOC_CTX *mem_ctx,
70 const char *domainname,
71 const char *modulename,
73 static bool idmap_found_domain_backend(
74 const char *string, regmatch_t matches[], void *private_data);
76 static bool idmap_init(void)
78 static bool initialized;
85 DEBUG(10, ("idmap_init(): calling static_init_idmap\n"));
91 if (!pdb_is_responsible_for_everything_else()) {
92 default_idmap_domain = idmap_init_named_domain(NULL, "*");
93 if (default_idmap_domain == NULL) {
98 passdb_idmap_domain = idmap_init_domain(
99 NULL, get_global_sam_name(), "passdb", false);
100 if (passdb_idmap_domain == NULL) {
101 TALLOC_FREE(default_idmap_domain);
105 idmap_domains = talloc_array(NULL, struct idmap_domain *, 0);
106 if (idmap_domains == NULL) {
107 TALLOC_FREE(passdb_idmap_domain);
108 TALLOC_FREE(default_idmap_domain);
112 ret = lp_wi_scan_global_parametrics(
113 "idmapconfig\\(.*\\):backend", 2,
114 idmap_found_domain_backend, NULL);
116 DBG_WARNING("wi_scan_global_parametrics returned %d\n", ret);
123 static bool idmap_found_domain_backend(
124 const char *string, regmatch_t matches[], void *private_data)
126 if (matches[1].rm_so == -1) {
127 DBG_WARNING("Found match, but no name??\n");
132 struct idmap_domain *dom, **tmp;
133 regoff_t len = matches[1].rm_eo - matches[1].rm_so;
136 memcpy(domname, string + matches[1].rm_so, len);
139 DBG_DEBUG("Found idmap domain \"%s\"\n", domname);
141 if (strcmp(domname, "*") == 0) {
145 dom = idmap_init_named_domain(idmap_domains, domname);
147 DBG_NOTICE("Could not init idmap domain %s\n",
152 tmp = talloc_realloc(idmap_domains, idmap_domains,
153 struct idmap_domain *, num_domains + 1);
155 DBG_WARNING("talloc_realloc failed\n");
160 idmap_domains[num_domains] = dom;
167 static struct idmap_methods *get_methods(const char *name)
169 struct idmap_backend *b;
171 for (b = backends; b; b = b->next) {
172 if (strequal(b->name, name)) {
180 bool idmap_is_offline(void)
182 return ( lp_winbind_offline_logon() &&
183 get_global_winbindd_state_offline() );
186 bool idmap_is_online(void)
188 return !idmap_is_offline();
191 /**********************************************************************
192 Allow a module to register itself as a method.
193 **********************************************************************/
195 NTSTATUS smb_register_idmap(int version, const char *name,
196 struct idmap_methods *methods)
198 struct idmap_backend *entry;
200 if ((version != SMB_IDMAP_INTERFACE_VERSION)) {
201 DEBUG(0, ("Failed to register idmap module.\n"
202 "The module was compiled against "
203 "SMB_IDMAP_INTERFACE_VERSION %d,\n"
204 "current SMB_IDMAP_INTERFACE_VERSION is %d.\n"
205 "Please recompile against the current version "
207 version, SMB_IDMAP_INTERFACE_VERSION));
208 return NT_STATUS_OBJECT_TYPE_MISMATCH;
211 if (!name || !name[0] || !methods) {
212 DEBUG(0,("Called with NULL pointer or empty name!\n"));
213 return NT_STATUS_INVALID_PARAMETER;
216 for (entry = backends; entry != NULL; entry = entry->next) {
217 if (strequal(entry->name, name)) {
218 DEBUG(5,("Idmap module %s already registered!\n",
220 return NT_STATUS_OBJECT_NAME_COLLISION;
224 entry = talloc(NULL, struct idmap_backend);
226 DEBUG(0,("Out of memory!\n"));
228 return NT_STATUS_NO_MEMORY;
230 entry->name = talloc_strdup(entry, name);
231 if ( ! entry->name) {
232 DEBUG(0,("Out of memory!\n"));
234 return NT_STATUS_NO_MEMORY;
236 entry->methods = methods;
238 DLIST_ADD(backends, entry);
239 DEBUG(5, ("Successfully added idmap backend '%s'\n", name));
244 * Initialize a domain structure
245 * @param[in] mem_ctx memory context for the result
246 * @param[in] domainname which domain is this for
247 * @param[in] modulename which backend module
248 * @param[in] check_range whether range checking should be done
249 * @result The initialized structure
251 static struct idmap_domain *idmap_init_domain(TALLOC_CTX *mem_ctx,
252 const char *domainname,
253 const char *modulename,
256 struct idmap_domain *result;
258 char *config_option = NULL;
261 unsigned high_id = 0;
263 result = talloc_zero(mem_ctx, struct idmap_domain);
264 if (result == NULL) {
265 DEBUG(0, ("talloc failed\n"));
269 result->name = talloc_strdup(result, domainname);
270 if (result->name == NULL) {
271 DEBUG(0, ("talloc failed\n"));
276 * Check whether the requested backend module exists and
280 result->methods = get_methods(modulename);
281 if (result->methods == NULL) {
282 DEBUG(3, ("idmap backend %s not found\n", modulename));
284 status = smb_probe_module("idmap", modulename);
285 if (!NT_STATUS_IS_OK(status)) {
286 DEBUG(3, ("Could not probe idmap module %s\n",
291 result->methods = get_methods(modulename);
293 if (result->methods == NULL) {
294 DEBUG(1, ("idmap backend %s not found\n", modulename));
299 * load ranges and read only information from the config
302 config_option = talloc_asprintf(result, "idmap config %s",
304 if (config_option == NULL) {
305 DEBUG(0, ("Out of memory!\n"));
309 result->read_only = lp_parm_bool(-1, config_option, "read only", false);
310 range = lp_parm_const_string(-1, config_option, "range", NULL);
312 talloc_free(config_option);
316 DEBUG(1, ("idmap range not specified for domain %s\n",
320 } else if (sscanf(range, "%u - %u", &low_id, &high_id) != 2)
322 DEBUG(1, ("invalid range '%s' specified for domain "
323 "'%s'\n", range, result->name));
327 } else if (low_id > high_id) {
328 DEBUG(1, ("Error: invalid idmap range detected: %u - %u\n",
335 result->low_id = low_id;
336 result->high_id = high_id;
338 status = result->methods->init(result);
339 if (!NT_STATUS_IS_OK(status)) {
340 DEBUG(1, ("idmap initialization returned %s\n",
353 * Initialize a named domain structure
354 * @param[in] mem_ctx memory context for the result
355 * @param[in] domname the domain name
356 * @result The default domain structure
358 * This routine looks at the "idmap config <domname>" parameters to figure out
362 static struct idmap_domain *idmap_init_named_domain(TALLOC_CTX *mem_ctx,
365 struct idmap_domain *result = NULL;
375 config_option = talloc_asprintf(talloc_tos(), "idmap config %s",
377 if (config_option == NULL) {
378 DEBUG(0, ("talloc failed\n"));
382 backend = lp_parm_const_string(-1, config_option, "backend", NULL);
383 if (backend == NULL) {
384 DEBUG(10, ("no idmap backend configured for domain '%s'\n",
389 result = idmap_init_domain(mem_ctx, domname, backend, true);
390 if (result == NULL) {
394 TALLOC_FREE(config_option);
398 TALLOC_FREE(config_option);
404 * Find a domain struct according to a domain name
405 * @param[in] domname Domain name to get the config for
406 * @result The default domain structure that fits
408 * This is the central routine in the winbindd-idmap child to pick the correct
409 * domain for looking up IDs. If domname is NULL or empty, we use the default
410 * domain. If it contains something, we try to use idmap_init_named_domain()
411 * to fetch the correct backend.
413 * The choice about "domname" is being made by the winbind parent, look at the
414 * "have_idmap_config" of "struct winbindd_domain" which is set in
415 * add_trusted_domain.
418 static struct idmap_domain *idmap_find_domain(const char *domname)
423 DEBUG(10, ("idmap_find_domain called for domain '%s'\n",
424 domname?domname:"NULL"));
431 if ((domname == NULL) || (domname[0] == '\0')) {
432 return default_idmap_domain;
435 for (i=0; i<num_domains; i++) {
436 if (strequal(idmap_domains[i]->name, domname)) {
437 return idmap_domains[i];
441 return default_idmap_domain;
444 struct idmap_domain *idmap_find_domain_with_sid(const char *domname,
445 const struct dom_sid *sid)
454 if (sid_check_is_for_passdb(sid)) {
455 return passdb_idmap_domain;
458 return idmap_find_domain(domname);
461 void idmap_close(void)
463 TALLOC_FREE(default_idmap_domain);
464 TALLOC_FREE(passdb_idmap_domain);
465 TALLOC_FREE(idmap_domains);
469 /**************************************************************************
470 idmap allocator interface functions
471 **************************************************************************/
473 static NTSTATUS idmap_allocate_unixid(struct unixid *id)
475 struct idmap_domain *dom;
478 dom = idmap_find_domain(NULL);
481 return NT_STATUS_UNSUCCESSFUL;
484 if (dom->methods->allocate_id == NULL) {
485 return NT_STATUS_NOT_IMPLEMENTED;
488 ret = dom->methods->allocate_id(dom, id);
494 NTSTATUS idmap_allocate_uid(struct unixid *id)
496 id->type = ID_TYPE_UID;
497 return idmap_allocate_unixid(id);
500 NTSTATUS idmap_allocate_gid(struct unixid *id)
502 id->type = ID_TYPE_GID;
503 return idmap_allocate_unixid(id);
506 NTSTATUS idmap_backends_unixid_to_sid(struct id_map *id)
508 struct idmap_domain *dom;
509 struct id_map *maps[2];
515 return NT_STATUS_NONE_MAPPED;
518 DEBUG(10, ("idmap_backend_unixid_to_sid: xid = %d (type %d)\n",
519 id->xid.id, id->xid.type));
525 * Always give passdb a chance first
528 dom = passdb_idmap_domain;
530 && NT_STATUS_IS_OK(dom->methods->unixids_to_sids(dom, maps))
531 && id->status == ID_MAPPED) {
535 dom = default_idmap_domain;
537 for (i=0; i<num_domains; i++) {
538 if ((id->xid.id >= idmap_domains[i]->low_id) &&
539 (id->xid.id <= idmap_domains[i]->high_id)) {
540 dom = idmap_domains[i];
546 return NT_STATUS_NONE_MAPPED;
549 return dom->methods->unixids_to_sids(dom, maps);