idmap: Remove "domname" from idmap_backends_unixid_to_sid
[Samba.git] / source3 / winbindd / idmap.c
blob8de8990d6e6f6bbe2f5878c56949caefd1f799a6
1 /*
2 Unix SMB/CIFS implementation.
3 ID Mapping
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/>.
24 #include "includes.h"
25 #include "winbindd.h"
26 #include "idmap.h"
27 #include "lib/util_sid_passdb.h"
28 #include "passdb.h"
30 #undef DBGC_CLASS
31 #define DBGC_CLASS DBGC_IDMAP
33 static_decl_idmap;
35 /**
36 * Pointer to the backend methods. Modules register themselves here via
37 * smb_register_idmap.
40 struct idmap_backend {
41 const char *name;
42 struct idmap_methods *methods;
43 struct idmap_backend *prev, *next;
45 static struct idmap_backend *backends = NULL;
47 /**
48 * Default idmap domain configured via "idmap backend".
50 static struct idmap_domain *default_idmap_domain;
52 /**
53 * Passdb idmap domain, not configurable. winbind must always give passdb a
54 * chance to map ids.
56 static struct idmap_domain *passdb_idmap_domain;
58 /**
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,
68 const char *domname);
69 static struct idmap_domain *idmap_init_domain(TALLOC_CTX *mem_ctx,
70 const char *domainname,
71 const char *modulename,
72 bool check_range);
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;
79 int ret;
81 if (initialized) {
82 return true;
85 DEBUG(10, ("idmap_init(): calling static_init_idmap\n"));
87 static_init_idmap;
89 initialized = true;
91 if (!pdb_is_responsible_for_everything_else()) {
92 default_idmap_domain = idmap_init_named_domain(NULL, "*");
93 if (default_idmap_domain == NULL) {
94 return false;
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);
102 return false;
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);
109 return false;
112 ret = lp_wi_scan_global_parametrics(
113 "idmapconfig\\(.*\\):backend", 2,
114 idmap_found_domain_backend, NULL);
115 if (ret != 0) {
116 DBG_WARNING("wi_scan_global_parametrics returned %d\n", ret);
117 return false;
120 return true;
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");
128 return false;
132 struct idmap_domain *dom, **tmp;
133 regoff_t len = matches[1].rm_eo - matches[1].rm_so;
134 char domname[len+1];
136 memcpy(domname, string + matches[1].rm_so, len);
137 domname[len] = '\0';
139 DBG_DEBUG("Found idmap domain \"%s\"\n", domname);
141 if (strcmp(domname, "*") == 0) {
142 return false;
145 dom = idmap_init_named_domain(idmap_domains, domname);
146 if (dom == NULL) {
147 DBG_NOTICE("Could not init idmap domain %s\n",
148 domname);
151 tmp = talloc_realloc(idmap_domains, idmap_domains,
152 struct idmap_domain *, num_domains + 1);
153 if (tmp == NULL) {
154 DBG_WARNING("talloc_realloc failed\n");
155 TALLOC_FREE(dom);
156 return false;
158 idmap_domains = tmp;
159 idmap_domains[num_domains] = dom;
160 num_domains += 1;
163 return false;
166 static struct idmap_methods *get_methods(const char *name)
168 struct idmap_backend *b;
170 for (b = backends; b; b = b->next) {
171 if (strequal(b->name, name)) {
172 return b->methods;
176 return NULL;
179 bool idmap_is_offline(void)
181 return ( lp_winbind_offline_logon() &&
182 get_global_winbindd_state_offline() );
185 bool idmap_is_online(void)
187 return !idmap_is_offline();
190 /**********************************************************************
191 Allow a module to register itself as a method.
192 **********************************************************************/
194 NTSTATUS smb_register_idmap(int version, const char *name,
195 struct idmap_methods *methods)
197 struct idmap_backend *entry;
199 if ((version != SMB_IDMAP_INTERFACE_VERSION)) {
200 DEBUG(0, ("Failed to register idmap module.\n"
201 "The module was compiled against "
202 "SMB_IDMAP_INTERFACE_VERSION %d,\n"
203 "current SMB_IDMAP_INTERFACE_VERSION is %d.\n"
204 "Please recompile against the current version "
205 "of samba!\n",
206 version, SMB_IDMAP_INTERFACE_VERSION));
207 return NT_STATUS_OBJECT_TYPE_MISMATCH;
210 if (!name || !name[0] || !methods) {
211 DEBUG(0,("Called with NULL pointer or empty name!\n"));
212 return NT_STATUS_INVALID_PARAMETER;
215 for (entry = backends; entry != NULL; entry = entry->next) {
216 if (strequal(entry->name, name)) {
217 DEBUG(5,("Idmap module %s already registered!\n",
218 name));
219 return NT_STATUS_OBJECT_NAME_COLLISION;
223 entry = talloc(NULL, struct idmap_backend);
224 if ( ! entry) {
225 DEBUG(0,("Out of memory!\n"));
226 TALLOC_FREE(entry);
227 return NT_STATUS_NO_MEMORY;
229 entry->name = talloc_strdup(entry, name);
230 if ( ! entry->name) {
231 DEBUG(0,("Out of memory!\n"));
232 TALLOC_FREE(entry);
233 return NT_STATUS_NO_MEMORY;
235 entry->methods = methods;
237 DLIST_ADD(backends, entry);
238 DEBUG(5, ("Successfully added idmap backend '%s'\n", name));
239 return NT_STATUS_OK;
243 * Initialize a domain structure
244 * @param[in] mem_ctx memory context for the result
245 * @param[in] domainname which domain is this for
246 * @param[in] modulename which backend module
247 * @param[in] check_range whether range checking should be done
248 * @result The initialized structure
250 static struct idmap_domain *idmap_init_domain(TALLOC_CTX *mem_ctx,
251 const char *domainname,
252 const char *modulename,
253 bool check_range)
255 struct idmap_domain *result;
256 NTSTATUS status;
257 char *config_option = NULL;
258 const char *range;
259 unsigned low_id = 0;
260 unsigned high_id = 0;
262 result = talloc_zero(mem_ctx, struct idmap_domain);
263 if (result == NULL) {
264 DEBUG(0, ("talloc failed\n"));
265 return NULL;
268 result->name = talloc_strdup(result, domainname);
269 if (result->name == NULL) {
270 DEBUG(0, ("talloc failed\n"));
271 goto fail;
275 * Check whether the requested backend module exists and
276 * load the methods.
279 result->methods = get_methods(modulename);
280 if (result->methods == NULL) {
281 DEBUG(3, ("idmap backend %s not found\n", modulename));
283 status = smb_probe_module("idmap", modulename);
284 if (!NT_STATUS_IS_OK(status)) {
285 DEBUG(3, ("Could not probe idmap module %s\n",
286 modulename));
287 goto fail;
290 result->methods = get_methods(modulename);
292 if (result->methods == NULL) {
293 DEBUG(1, ("idmap backend %s not found\n", modulename));
294 goto fail;
298 * load ranges and read only information from the config
301 config_option = talloc_asprintf(result, "idmap config %s",
302 result->name);
303 if (config_option == NULL) {
304 DEBUG(0, ("Out of memory!\n"));
305 goto fail;
308 result->read_only = lp_parm_bool(-1, config_option, "read only", false);
309 range = lp_parm_const_string(-1, config_option, "range", NULL);
311 talloc_free(config_option);
313 if (range == NULL) {
314 if (check_range) {
315 DEBUG(1, ("idmap range not specified for domain %s\n",
316 result->name));
317 goto fail;
319 } else if (sscanf(range, "%u - %u", &low_id, &high_id) != 2)
321 DEBUG(1, ("invalid range '%s' specified for domain "
322 "'%s'\n", range, result->name));
323 if (check_range) {
324 goto fail;
326 } else if (low_id > high_id) {
327 DEBUG(1, ("Error: invalid idmap range detected: %u - %u\n",
328 low_id, high_id));
329 if (check_range) {
330 goto fail;
334 result->low_id = low_id;
335 result->high_id = high_id;
337 status = result->methods->init(result);
338 if (!NT_STATUS_IS_OK(status)) {
339 DEBUG(1, ("idmap initialization returned %s\n",
340 nt_errstr(status)));
341 goto fail;
344 return result;
346 fail:
347 TALLOC_FREE(result);
348 return NULL;
352 * Initialize a named domain structure
353 * @param[in] mem_ctx memory context for the result
354 * @param[in] domname the domain name
355 * @result The default domain structure
357 * This routine looks at the "idmap config <domname>" parameters to figure out
358 * the configuration.
361 static struct idmap_domain *idmap_init_named_domain(TALLOC_CTX *mem_ctx,
362 const char *domname)
364 struct idmap_domain *result = NULL;
365 char *config_option;
366 const char *backend;
367 bool ok;
369 ok = idmap_init();
370 if (!ok) {
371 return NULL;
374 config_option = talloc_asprintf(talloc_tos(), "idmap config %s",
375 domname);
376 if (config_option == NULL) {
377 DEBUG(0, ("talloc failed\n"));
378 goto fail;
381 backend = lp_parm_const_string(-1, config_option, "backend", NULL);
382 if (backend == NULL) {
383 DEBUG(10, ("no idmap backend configured for domain '%s'\n",
384 domname));
385 goto fail;
388 result = idmap_init_domain(mem_ctx, domname, backend, true);
389 if (result == NULL) {
390 goto fail;
393 TALLOC_FREE(config_option);
394 return result;
396 fail:
397 TALLOC_FREE(config_option);
398 TALLOC_FREE(result);
399 return NULL;
403 * Find a domain struct according to a domain name
404 * @param[in] domname Domain name to get the config for
405 * @result The default domain structure that fits
407 * This is the central routine in the winbindd-idmap child to pick the correct
408 * domain for looking up IDs. If domname is NULL or empty, we use the default
409 * domain. If it contains something, we try to use idmap_init_named_domain()
410 * to fetch the correct backend.
412 * The choice about "domname" is being made by the winbind parent, look at the
413 * "have_idmap_config" of "struct winbindd_domain" which is set in
414 * add_trusted_domain.
417 static struct idmap_domain *idmap_find_domain(const char *domname)
419 bool ok;
420 int i;
422 DEBUG(10, ("idmap_find_domain called for domain '%s'\n",
423 domname?domname:"NULL"));
425 ok = idmap_init();
426 if (!ok) {
427 return NULL;
430 if ((domname == NULL) || (domname[0] == '\0')) {
431 return default_idmap_domain;
434 for (i=0; i<num_domains; i++) {
435 if (strequal(idmap_domains[i]->name, domname)) {
436 return idmap_domains[i];
440 return default_idmap_domain;
443 struct idmap_domain *idmap_find_domain_with_sid(const char *domname,
444 const struct dom_sid *sid)
446 bool ok;
448 ok = idmap_init();
449 if (!ok) {
450 return NULL;
453 if (sid_check_is_for_passdb(sid)) {
454 return passdb_idmap_domain;
457 return idmap_find_domain(domname);
460 void idmap_close(void)
462 TALLOC_FREE(default_idmap_domain);
463 TALLOC_FREE(passdb_idmap_domain);
464 TALLOC_FREE(idmap_domains);
465 num_domains = 0;
468 /**************************************************************************
469 idmap allocator interface functions
470 **************************************************************************/
472 static NTSTATUS idmap_allocate_unixid(struct unixid *id)
474 struct idmap_domain *dom;
475 NTSTATUS ret;
477 dom = idmap_find_domain(NULL);
479 if (dom == NULL) {
480 return NT_STATUS_UNSUCCESSFUL;
483 if (dom->methods->allocate_id == NULL) {
484 return NT_STATUS_NOT_IMPLEMENTED;
487 ret = dom->methods->allocate_id(dom, id);
489 return ret;
493 NTSTATUS idmap_allocate_uid(struct unixid *id)
495 id->type = ID_TYPE_UID;
496 return idmap_allocate_unixid(id);
499 NTSTATUS idmap_allocate_gid(struct unixid *id)
501 id->type = ID_TYPE_GID;
502 return idmap_allocate_unixid(id);
505 NTSTATUS idmap_backends_unixid_to_sid(struct id_map *id)
507 struct idmap_domain *dom;
508 struct id_map *maps[2];
509 bool ok;
510 int i;
512 ok = idmap_init();
513 if (!ok) {
514 return NT_STATUS_NONE_MAPPED;
517 DEBUG(10, ("idmap_backend_unixid_to_sid: xid = %d (type %d)\n",
518 id->xid.id, id->xid.type));
520 maps[0] = id;
521 maps[1] = NULL;
524 * Always give passdb a chance first
527 dom = passdb_idmap_domain;
528 if ((dom != NULL)
529 && NT_STATUS_IS_OK(dom->methods->unixids_to_sids(dom, maps))
530 && id->status == ID_MAPPED) {
531 return NT_STATUS_OK;
534 dom = default_idmap_domain;
536 for (i=0; i<num_domains; i++) {
537 if ((id->xid.id >= idmap_domains[i]->low_id) &&
538 (id->xid.id <= idmap_domains[i]->high_id)) {
539 dom = idmap_domains[i];
540 break;
544 if (dom == NULL) {
545 return NT_STATUS_NONE_MAPPED;
548 return dom->methods->unixids_to_sids(dom, maps);