tevent: Add lib/tevent as include directory.
[Samba.git] / source3 / winbindd / idmap.c
blob76310e02baa6721355747dd2b0a05c8f674f8486
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"
28 #undef DBGC_CLASS
29 #define DBGC_CLASS DBGC_IDMAP
31 static_decl_idmap;
33 static void idmap_init(void)
35 static bool initialized;
37 if (initialized) {
38 return;
41 DEBUG(10, ("idmap_init(): calling static_init_idmap\n"));
43 static_init_idmap;
45 initialized = true;
48 /**
49 * Pointer to the backend methods. Modules register themselves here via
50 * smb_register_idmap.
53 struct idmap_backend {
54 const char *name;
55 struct idmap_methods *methods;
56 struct idmap_backend *prev, *next;
58 static struct idmap_backend *backends = NULL;
60 /**
61 * Default idmap domain configured via "idmap backend".
63 static struct idmap_domain *default_idmap_domain;
65 /**
66 * Passdb idmap domain, not configurable. winbind must always give passdb a
67 * chance to map ids.
69 static struct idmap_domain *passdb_idmap_domain;
71 /**
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)) {
86 return b->methods;
90 return NULL;
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 "
119 "of samba!\n",
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",
132 name));
133 return NT_STATUS_OBJECT_NAME_COLLISION;
137 entry = talloc(NULL, struct idmap_backend);
138 if ( ! entry) {
139 DEBUG(0,("Out of memory!\n"));
140 TALLOC_FREE(entry);
141 return NT_STATUS_NO_MEMORY;
143 entry->name = talloc_strdup(entry, name);
144 if ( ! entry->name) {
145 DEBUG(0,("Out of memory!\n"));
146 TALLOC_FREE(entry);
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));
153 return NT_STATUS_OK;
156 static int close_domain_destructor(struct idmap_domain *dom)
158 NTSTATUS ret;
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));
165 return 0;
168 static bool parse_idmap_module(TALLOC_CTX *mem_ctx, const char *param,
169 char **pmodulename, char **pargs)
171 char *modulename;
172 char *args;
174 if (strncmp(param, "idmap_", 6) == 0) {
175 param += 6;
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) {
183 return false;
186 args = strchr(modulename, ':');
187 if (args == NULL) {
188 *pmodulename = modulename;
189 *pargs = NULL;
190 return true;
193 *args = '\0';
195 args = talloc_strdup(mem_ctx, args+1);
196 if (args == NULL) {
197 TALLOC_FREE(modulename);
198 return false;
201 *pmodulename = modulename;
202 *pargs = args;
203 return true;
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,
218 const char *params,
219 bool check_range)
221 struct idmap_domain *result;
222 NTSTATUS status;
224 result = talloc_zero(mem_ctx, struct idmap_domain);
225 if (result == NULL) {
226 DEBUG(0, ("talloc failed\n"));
227 return NULL;
230 result->name = talloc_strdup(result, domainname);
231 if (result->name == NULL) {
232 DEBUG(0, ("talloc failed\n"));
233 goto fail;
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.
244 uid_t low_uid = 0;
245 uid_t high_uid = 0;
246 gid_t low_gid = 0;
247 gid_t high_gid = 0;
249 result->low_id = 0;
250 result->high_id = 0;
252 if (!lp_idmap_uid(&low_uid, &high_uid)) {
253 DEBUG(1, ("'idmap uid' not set!\n"));
254 if (check_range) {
255 goto fail;
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"));
264 if (check_range) {
265 goto fail;
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 "
272 "intersection\n"));
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();
278 } else {
279 char *config_option = NULL;
280 const char *range;
282 config_option = talloc_asprintf(result, "idmap config %s",
283 result->name);
284 if (config_option == NULL) {
285 DEBUG(0, ("Out of memory!\n"));
286 goto fail;
289 range = lp_parm_const_string(-1, config_option, "range", NULL);
290 if (range == NULL) {
291 DEBUG(1, ("idmap range not specified for domain %s\n",
292 result ->name));
293 if (check_range) {
294 goto fail;
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));
301 if (check_range) {
302 goto fail;
306 result->read_only = lp_parm_bool(-1, config_option, "read only",
307 false);
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));
316 if (check_range) {
317 goto fail;
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",
328 modulename));
329 goto fail;
332 result->methods = get_methods(modulename);
334 if (result->methods == NULL) {
335 DEBUG(1, ("idmap backend %s not found\n", modulename));
336 goto fail;
339 status = result->methods->init(result, params);
340 if (!NT_STATUS_IS_OK(status)) {
341 DEBUG(1, ("idmap initialization returned %s\n",
342 nt_errstr(status)));
343 goto fail;
346 talloc_set_destructor(result, close_domain_destructor);
348 return result;
350 fail:
351 TALLOC_FREE(result);
352 return NULL;
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;
367 char *modulename;
368 char *params;
370 idmap_init();
372 if (!parse_idmap_module(talloc_tos(), lp_idmap_backend(), &modulename,
373 &params)) {
374 DEBUG(1, ("parse_idmap_module failed\n"));
375 return NULL;
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) {
382 goto fail;
385 TALLOC_FREE(modulename);
386 TALLOC_FREE(params);
387 return result;
389 fail:
390 TALLOC_FREE(modulename);
391 TALLOC_FREE(params);
392 TALLOC_FREE(result);
393 return NULL;
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
403 * the configuration.
406 static struct idmap_domain *idmap_init_named_domain(TALLOC_CTX *mem_ctx,
407 const char *domname)
409 struct idmap_domain *result = NULL;
410 char *config_option;
411 const char *backend;
413 config_option = talloc_asprintf(talloc_tos(), "idmap config %s",
414 domname);
415 if (config_option == NULL) {
416 DEBUG(0, ("talloc failed\n"));
417 goto fail;
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));
423 goto fail;
426 result = idmap_init_domain(mem_ctx, domname, backend, NULL, true);
427 if (result == NULL) {
428 goto fail;
431 TALLOC_FREE(config_option);
432 return result;
434 fail:
435 TALLOC_FREE(config_option);
436 TALLOC_FREE(result);
437 return NULL;
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)
450 idmap_init();
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) {
459 return 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;
493 int i;
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) {
505 return 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"));
527 return NULL;
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);
540 return result;
543 void idmap_close(void)
545 TALLOC_FREE(default_idmap_domain);
546 TALLOC_FREE(passdb_idmap_domain);
547 TALLOC_FREE(idmap_domains);
548 num_domains = 0;
551 /**************************************************************************
552 idmap allocator interface functions
553 **************************************************************************/
555 static NTSTATUS idmap_allocate_unixid(struct unixid *id)
557 struct idmap_domain *dom;
558 NTSTATUS ret;
560 dom = idmap_find_domain(NULL);
562 if (dom == 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);
572 return ret;
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 "
594 "(type %d)\n",
595 domname?domname:"NULL", id->xid.id, id->xid.type));
597 maps[0] = id;
598 maps[1] = NULL;
601 * Always give passdb a chance first
604 dom = idmap_init_passdb_domain(NULL);
605 if ((dom != NULL)
606 && NT_STATUS_IS_OK(dom->methods->unixids_to_sids(dom, maps))
607 && id->status == ID_MAPPED) {
608 return NT_STATUS_OK;
611 dom = idmap_find_domain(domname);
612 if (dom == NULL) {
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)));
627 maps[0] = id;
628 maps[1] = NULL;
630 if (sid_check_is_in_builtin(id->sid)
631 || (sid_check_is_in_our_domain(id->sid)))
633 NTSTATUS status;
635 DEBUG(10, ("asking passdb...\n"));
637 dom = idmap_init_passdb_domain(NULL);
638 if (dom == 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) {
644 return status;
647 DEBUG(10, ("passdb could not map.\n"));
649 return NT_STATUS_NONE_MAPPED;
652 dom = idmap_find_domain(domain);
653 if (dom == NULL) {
654 return NT_STATUS_NONE_MAPPED;
657 return dom->methods->sids_to_unixids(dom, maps);