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
31 struct idmap_backend {
33 struct idmap_methods *methods;
34 struct idmap_backend *prev, *next;
37 struct idmap_alloc_backend {
39 struct idmap_alloc_methods *methods;
40 struct idmap_alloc_backend *prev, *next;
43 struct idmap_alloc_context {
44 struct idmap_alloc_methods *methods;
48 * Lists for the module initializations
50 static struct idmap_backend *backends = NULL;
51 static struct idmap_alloc_backend *alloc_backends = NULL;
54 static struct idmap_domain *default_idmap_domain;
55 static struct idmap_domain *passdb_idmap_domain;
57 static struct idmap_domain **idmap_domains = NULL;
58 static int num_domains = 0;
60 static struct idmap_alloc_context *idmap_alloc_ctx = NULL;
62 static struct idmap_methods *get_methods(struct idmap_backend *be,
65 struct idmap_backend *b;
67 for (b = be; b; b = b->next) {
68 if (strequal(b->name, name)) {
76 static struct idmap_alloc_methods *get_alloc_methods(
77 struct idmap_alloc_backend *be,
80 struct idmap_alloc_backend *b;
82 for (b = be; b; b = b->next) {
83 if (strequal(b->name, name)) {
91 bool idmap_is_offline(void)
93 return ( lp_winbind_offline_logon() &&
94 get_global_winbindd_state_offline() );
97 bool idmap_is_online(void)
99 return !idmap_is_offline();
102 /**********************************************************************
103 Allow a module to register itself as a method.
104 **********************************************************************/
106 NTSTATUS smb_register_idmap(int version, const char *name,
107 struct idmap_methods *methods)
109 struct idmap_backend *entry;
111 if ((version != SMB_IDMAP_INTERFACE_VERSION)) {
112 DEBUG(0, ("Failed to register idmap module.\n"
113 "The module was compiled against "
114 "SMB_IDMAP_INTERFACE_VERSION %d,\n"
115 "current SMB_IDMAP_INTERFACE_VERSION is %d.\n"
116 "Please recompile against the current version "
118 version, SMB_IDMAP_INTERFACE_VERSION));
119 return NT_STATUS_OBJECT_TYPE_MISMATCH;
122 if (!name || !name[0] || !methods) {
123 DEBUG(0,("Called with NULL pointer or empty name!\n"));
124 return NT_STATUS_INVALID_PARAMETER;
127 for (entry = backends; entry != NULL; entry = entry->next) {
128 if (strequal(entry->name, name)) {
129 DEBUG(0,("Idmap module %s already registered!\n", name));
130 return NT_STATUS_OBJECT_NAME_COLLISION;
134 entry = talloc(NULL, struct idmap_backend);
136 DEBUG(0,("Out of memory!\n"));
138 return NT_STATUS_NO_MEMORY;
140 entry->name = talloc_strdup(entry, name);
141 if ( ! entry->name) {
142 DEBUG(0,("Out of memory!\n"));
144 return NT_STATUS_NO_MEMORY;
146 entry->methods = methods;
148 DLIST_ADD(backends, entry);
149 DEBUG(5, ("Successfully added idmap backend '%s'\n", name));
153 /**********************************************************************
154 Allow a module to register itself as a method.
155 **********************************************************************/
157 NTSTATUS smb_register_idmap_alloc(int version, const char *name,
158 struct idmap_alloc_methods *methods)
160 struct idmap_alloc_methods *test;
161 struct idmap_alloc_backend *entry;
163 if ((version != SMB_IDMAP_INTERFACE_VERSION)) {
164 DEBUG(0, ("Failed to register idmap alloc module.\n"
165 "The module was compiled against "
166 "SMB_IDMAP_INTERFACE_VERSION %d,\n"
167 "current SMB_IDMAP_INTERFACE_VERSION is %d.\n"
168 "Please recompile against the current version "
170 version, SMB_IDMAP_INTERFACE_VERSION));
171 return NT_STATUS_OBJECT_TYPE_MISMATCH;
174 if (!name || !name[0] || !methods) {
175 DEBUG(0,("Called with NULL pointer or empty name!\n"));
176 return NT_STATUS_INVALID_PARAMETER;
179 test = get_alloc_methods(alloc_backends, name);
181 DEBUG(0,("idmap_alloc module %s already registered!\n", name));
182 return NT_STATUS_OBJECT_NAME_COLLISION;
185 entry = talloc(NULL, struct idmap_alloc_backend);
187 DEBUG(0,("Out of memory!\n"));
188 return NT_STATUS_NO_MEMORY;
190 entry->name = talloc_strdup(entry, name);
191 if ( ! entry->name) {
192 DEBUG(0,("Out of memory!\n"));
193 return NT_STATUS_NO_MEMORY;
195 entry->methods = methods;
197 DLIST_ADD(alloc_backends, entry);
198 DEBUG(5, ("Successfully added idmap alloc backend '%s'\n", name));
202 static int close_domain_destructor(struct idmap_domain *dom)
206 ret = dom->methods->close_fn(dom);
207 if (!NT_STATUS_IS_OK(ret)) {
208 DEBUG(3, ("Failed to close idmap domain [%s]!\n", dom->name));
214 static bool parse_idmap_module(TALLOC_CTX *mem_ctx, const char *param,
215 char **pmodulename, char **pargs)
220 if (strncmp(param, "idmap_", 6) == 0) {
222 DEBUG(1, ("idmap_init: idmap backend uses deprecated "
223 "'idmap_' prefix. Please replace 'idmap_%s' by "
224 "'%s'\n", param, param));
227 modulename = talloc_strdup(mem_ctx, param);
228 if (modulename == NULL) {
232 args = strchr(modulename, ':');
234 *pmodulename = modulename;
241 args = talloc_strdup(mem_ctx, args+1);
243 TALLOC_FREE(modulename);
247 *pmodulename = modulename;
252 static struct idmap_domain *idmap_init_domain(TALLOC_CTX *mem_ctx,
253 const char *domainname,
254 const char *modulename,
257 struct idmap_domain *result;
260 result = talloc_zero(mem_ctx, struct idmap_domain);
261 if (result == NULL) {
262 DEBUG(0, ("talloc failed\n"));
266 result->name = talloc_strdup(result, domainname);
267 if (result->name == NULL) {
268 DEBUG(0, ("talloc failed\n"));
272 result->methods = get_methods(backends, modulename);
273 if (result->methods == NULL) {
274 DEBUG(3, ("idmap backend %s not found\n", modulename));
276 status = smb_probe_module("idmap", modulename);
277 if (!NT_STATUS_IS_OK(status)) {
278 DEBUG(3, ("Could not probe idmap module %s\n",
283 result->methods = get_methods(backends, modulename);
285 if (result->methods == NULL) {
286 DEBUG(1, ("idmap backend %s not found\n", modulename));
290 status = result->methods->init(result, params);
291 if (!NT_STATUS_IS_OK(status)) {
292 DEBUG(1, ("idmap initialization returned %s\n",
297 talloc_set_destructor(result, close_domain_destructor);
306 static struct idmap_domain *idmap_init_default_domain(TALLOC_CTX *mem_ctx)
308 struct idmap_domain *result;
312 DEBUG(10, ("idmap_init_default_domain: calling static_init_idmap\n"));
316 if (!parse_idmap_module(talloc_tos(), lp_idmap_backend(), &modulename,
318 DEBUG(1, ("parse_idmap_module failed\n"));
322 DEBUG(3, ("idmap_init: using '%s' as remote backend\n", modulename));
324 result = idmap_init_domain(mem_ctx, "*", modulename, params);
325 if (result == NULL) {
329 TALLOC_FREE(modulename);
334 TALLOC_FREE(modulename);
340 static struct idmap_domain *idmap_init_named_domain(TALLOC_CTX *mem_ctx,
343 struct idmap_domain *result = NULL;
347 config_option = talloc_asprintf(talloc_tos(), "idmap config %s",
349 if (config_option == NULL) {
350 DEBUG(0, ("talloc failed\n"));
354 backend = lp_parm_const_string(-1, config_option, "backend", NULL);
355 if (backend == NULL) {
356 DEBUG(1, ("no backend defined for %s\n", config_option));
360 result = idmap_init_domain(mem_ctx, domname, backend, NULL);
361 if (result == NULL) {
365 TALLOC_FREE(config_option);
369 TALLOC_FREE(config_option);
374 static struct idmap_domain *idmap_init_passdb_domain(TALLOC_CTX *mem_ctx)
376 if (passdb_idmap_domain != NULL) {
377 return passdb_idmap_domain;
380 passdb_idmap_domain = idmap_init_domain(NULL, get_global_sam_name(),
382 if (passdb_idmap_domain == NULL) {
383 DEBUG(1, ("Could not init passdb idmap domain\n"));
386 return passdb_idmap_domain;
389 static struct idmap_domain *idmap_find_domain(const char *domname)
391 struct idmap_domain *result;
395 * Always init the default domain, we can't go without one
397 if (default_idmap_domain == NULL) {
398 default_idmap_domain = idmap_init_default_domain(NULL);
400 if (default_idmap_domain == NULL) {
404 if ((domname == NULL) || (domname[0] == '\0')) {
405 return default_idmap_domain;
408 for (i=0; i<num_domains; i++) {
409 if (strequal(idmap_domains[i]->name, domname)) {
410 return idmap_domains[i];
414 if (idmap_domains == NULL) {
416 * talloc context for all idmap domains
418 idmap_domains = TALLOC_ARRAY(NULL, struct idmap_domain *, 1);
421 if (idmap_domains == NULL) {
422 DEBUG(0, ("talloc failed\n"));
426 result = idmap_init_named_domain(idmap_domains, domname);
427 if (result == NULL) {
429 * Could not init that domain -- try the default one
431 return default_idmap_domain;
434 ADD_TO_ARRAY(idmap_domains, struct idmap_domain *, result,
435 &idmap_domains, &num_domains);
439 void idmap_close(void)
441 if (idmap_alloc_ctx) {
442 idmap_alloc_ctx->methods->close_fn();
443 idmap_alloc_ctx->methods = NULL;
445 alloc_backends = NULL;
446 TALLOC_FREE(default_idmap_domain);
447 TALLOC_FREE(passdb_idmap_domain);
448 TALLOC_FREE(idmap_domains);
452 static NTSTATUS idmap_alloc_init(struct idmap_alloc_context **ctx)
455 char *modulename, *params;
456 NTSTATUS ret = NT_STATUS_NO_MEMORY;;
458 if (idmap_alloc_ctx != NULL) {
459 *ctx = idmap_alloc_ctx;
463 idmap_alloc_ctx = talloc(NULL, struct idmap_alloc_context);
464 if (idmap_alloc_ctx == NULL) {
465 DEBUG(0, ("talloc failed\n"));
469 backend = lp_idmap_alloc_backend();
470 if ((backend == NULL) || (backend[0] == '\0')) {
471 backend = lp_idmap_backend();
474 if (backend == NULL) {
475 DEBUG(3, ("no idmap alloc backend defined\n"));
476 ret = NT_STATUS_INVALID_PARAMETER;
480 if (!parse_idmap_module(idmap_alloc_ctx, backend, &modulename,
482 DEBUG(1, ("parse_idmap_module %s failed\n", backend));
486 idmap_alloc_ctx->methods = get_alloc_methods(alloc_backends,
489 if (idmap_alloc_ctx->methods == NULL) {
490 ret = smb_probe_module("idmap", modulename);
491 if (NT_STATUS_IS_OK(ret)) {
492 idmap_alloc_ctx->methods =
493 get_alloc_methods(alloc_backends,
498 if (idmap_alloc_ctx->methods == NULL) {
499 DEBUG(1, ("could not find idmap alloc module %s\n", backend));
500 ret = NT_STATUS_INVALID_PARAMETER;
504 ret = idmap_alloc_ctx->methods->init(params);
506 if (!NT_STATUS_IS_OK(ret)) {
507 DEBUG(0, ("ERROR: Initialization failed for alloc "
508 "backend, deferred!\n"));
512 TALLOC_FREE(modulename);
515 *ctx = idmap_alloc_ctx;
519 TALLOC_FREE(idmap_alloc_ctx);
523 /**************************************************************************
524 idmap allocator interface functions
525 **************************************************************************/
527 NTSTATUS idmap_allocate_uid(struct unixid *id)
529 struct idmap_alloc_context *ctx;
532 if (!NT_STATUS_IS_OK(ret = idmap_alloc_init(&ctx))) {
536 id->type = ID_TYPE_UID;
537 return ctx->methods->allocate_id(id);
540 NTSTATUS idmap_allocate_gid(struct unixid *id)
542 struct idmap_alloc_context *ctx;
545 if (!NT_STATUS_IS_OK(ret = idmap_alloc_init(&ctx))) {
549 id->type = ID_TYPE_GID;
550 return ctx->methods->allocate_id(id);
553 NTSTATUS idmap_set_uid_hwm(struct unixid *id)
555 struct idmap_alloc_context *ctx;
558 if (!NT_STATUS_IS_OK(ret = idmap_alloc_init(&ctx))) {
562 id->type = ID_TYPE_UID;
563 return ctx->methods->set_id_hwm(id);
566 NTSTATUS idmap_set_gid_hwm(struct unixid *id)
568 struct idmap_alloc_context *ctx;
571 if (!NT_STATUS_IS_OK(ret = idmap_alloc_init(&ctx))) {
575 id->type = ID_TYPE_GID;
576 return ctx->methods->set_id_hwm(id);
579 NTSTATUS idmap_new_mapping(const struct dom_sid *psid, enum id_type type,
583 struct idmap_domain *dom;
587 dom = idmap_find_domain(NULL);
589 DEBUG(3, ("no default domain, no place to write\n"));
590 return NT_STATUS_ACCESS_DENIED;
592 if (dom->methods->set_mapping == NULL) {
593 DEBUG(3, ("default domain not writable\n"));
594 return NT_STATUS_MEDIA_WRITE_PROTECTED;
597 sid_copy(&sid, psid);
603 status = idmap_allocate_uid(&map.xid);
606 status = idmap_allocate_gid(&map.xid);
609 status = NT_STATUS_INVALID_PARAMETER;
613 if (!NT_STATUS_IS_OK(status)) {
614 DEBUG(3, ("Could not allocate id: %s\n", nt_errstr(status)));
618 map.status = ID_MAPPED;
620 DEBUG(10, ("Setting mapping: %s <-> %s %lu\n",
621 sid_string_dbg(map.sid),
622 (map.xid.type == ID_TYPE_UID) ? "UID" : "GID",
623 (unsigned long)map.xid.id));
625 status = dom->methods->set_mapping(dom, &map);
627 if (!NT_STATUS_IS_OK(status)) {
628 DEBUG(3, ("Could not store the new mapping: %s\n",
638 NTSTATUS idmap_backends_unixid_to_sid(const char *domname, struct id_map *id)
640 struct idmap_domain *dom;
641 struct id_map *maps[2];
647 * Always give passdb a chance first
650 dom = idmap_init_passdb_domain(NULL);
652 && NT_STATUS_IS_OK(dom->methods->unixids_to_sids(dom, maps))) {
656 dom = idmap_find_domain(domname);
658 return NT_STATUS_NONE_MAPPED;
661 return dom->methods->unixids_to_sids(dom, maps);
664 NTSTATUS idmap_backends_sid_to_unixid(const char *domain, struct id_map *id)
666 struct idmap_domain *dom;
667 struct id_map *maps[2];
672 if (sid_check_is_in_builtin(id->sid)
673 || (sid_check_is_in_our_domain(id->sid))) {
675 dom = idmap_init_passdb_domain(NULL);
677 return NT_STATUS_NONE_MAPPED;
679 return dom->methods->sids_to_unixids(dom, maps);
682 dom = idmap_find_domain(domain);
684 return NT_STATUS_NONE_MAPPED;
687 return dom->methods->sids_to_unixids(dom, maps);
690 NTSTATUS idmap_set_mapping(const struct id_map *map)
692 struct idmap_domain *dom;
694 dom = idmap_find_domain(NULL);
696 DEBUG(3, ("no default domain, no place to write\n"));
697 return NT_STATUS_ACCESS_DENIED;
699 if (dom->methods->set_mapping == NULL) {
700 DEBUG(3, ("default domain not writable\n"));
701 return NT_STATUS_MEDIA_WRITE_PROTECTED;
704 return dom->methods->set_mapping(dom, map);