Some doxygen comments for idmap
[Samba/gebeck_regimport.git] / source3 / winbindd / idmap.c
blobc23919fb188064c46fc3ae8ed7a82a567a82a0f4
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
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/>.
23 #include "includes.h"
24 #include "winbindd.h"
26 #undef DBGC_CLASS
27 #define DBGC_CLASS DBGC_IDMAP
29 static_decl_idmap;
31 /**
32 * Pointer to the backend methods. Modules register themselves here via
33 * smb_register_idmap.
36 struct idmap_backend {
37 const char *name;
38 struct idmap_methods *methods;
39 struct idmap_backend *prev, *next;
41 static struct idmap_backend *backends = NULL;
43 /**
44 * Pointer to the alloc backend methods. Modules register themselves here via
45 * smb_register_idmap_alloc.
47 struct idmap_alloc_backend {
48 const char *name;
49 struct idmap_alloc_methods *methods;
50 struct idmap_alloc_backend *prev, *next;
52 static struct idmap_alloc_backend *alloc_backends = NULL;
54 /**
55 * The idmap alloc context that is configured via "idmap alloc
56 * backend". Defaults to "idmap backend" in case the module (tdb, ldap) also
57 * provides alloc methods.
59 struct idmap_alloc_context {
60 struct idmap_alloc_methods *methods;
62 static struct idmap_alloc_context *idmap_alloc_ctx = NULL;
64 /**
65 * Default idmap domain configured via "idmap backend".
67 static struct idmap_domain *default_idmap_domain;
69 /**
70 * Passdb idmap domain, not configurable. winbind must always give passdb a
71 * chance to map ids.
73 static struct idmap_domain *passdb_idmap_domain;
75 /**
76 * List of specially configured idmap domains. This list is filled on demand
77 * in the winbind idmap child when the parent winbind figures out via the
78 * special range parameter or via the domain SID that a special "idmap config
79 * domain" configuration is present.
81 static struct idmap_domain **idmap_domains = NULL;
82 static int num_domains = 0;
84 static struct idmap_methods *get_methods(struct idmap_backend *be,
85 const char *name)
87 struct idmap_backend *b;
89 for (b = be; b; b = b->next) {
90 if (strequal(b->name, name)) {
91 return b->methods;
95 return NULL;
98 static struct idmap_alloc_methods *get_alloc_methods(
99 struct idmap_alloc_backend *be,
100 const char *name)
102 struct idmap_alloc_backend *b;
104 for (b = be; b; b = b->next) {
105 if (strequal(b->name, name)) {
106 return b->methods;
110 return NULL;
113 bool idmap_is_offline(void)
115 return ( lp_winbind_offline_logon() &&
116 get_global_winbindd_state_offline() );
119 bool idmap_is_online(void)
121 return !idmap_is_offline();
124 /**********************************************************************
125 Allow a module to register itself as a method.
126 **********************************************************************/
128 NTSTATUS smb_register_idmap(int version, const char *name,
129 struct idmap_methods *methods)
131 struct idmap_backend *entry;
133 if ((version != SMB_IDMAP_INTERFACE_VERSION)) {
134 DEBUG(0, ("Failed to register idmap module.\n"
135 "The module was compiled against "
136 "SMB_IDMAP_INTERFACE_VERSION %d,\n"
137 "current SMB_IDMAP_INTERFACE_VERSION is %d.\n"
138 "Please recompile against the current version "
139 "of samba!\n",
140 version, SMB_IDMAP_INTERFACE_VERSION));
141 return NT_STATUS_OBJECT_TYPE_MISMATCH;
144 if (!name || !name[0] || !methods) {
145 DEBUG(0,("Called with NULL pointer or empty name!\n"));
146 return NT_STATUS_INVALID_PARAMETER;
149 for (entry = backends; entry != NULL; entry = entry->next) {
150 if (strequal(entry->name, name)) {
151 DEBUG(0,("Idmap module %s already registered!\n",
152 name));
153 return NT_STATUS_OBJECT_NAME_COLLISION;
157 entry = talloc(NULL, struct idmap_backend);
158 if ( ! entry) {
159 DEBUG(0,("Out of memory!\n"));
160 TALLOC_FREE(entry);
161 return NT_STATUS_NO_MEMORY;
163 entry->name = talloc_strdup(entry, name);
164 if ( ! entry->name) {
165 DEBUG(0,("Out of memory!\n"));
166 TALLOC_FREE(entry);
167 return NT_STATUS_NO_MEMORY;
169 entry->methods = methods;
171 DLIST_ADD(backends, entry);
172 DEBUG(5, ("Successfully added idmap backend '%s'\n", name));
173 return NT_STATUS_OK;
176 /**********************************************************************
177 Allow a module to register itself as an alloc method.
178 **********************************************************************/
180 NTSTATUS smb_register_idmap_alloc(int version, const char *name,
181 struct idmap_alloc_methods *methods)
183 struct idmap_alloc_methods *test;
184 struct idmap_alloc_backend *entry;
186 if ((version != SMB_IDMAP_INTERFACE_VERSION)) {
187 DEBUG(0, ("Failed to register idmap alloc module.\n"
188 "The module was compiled against "
189 "SMB_IDMAP_INTERFACE_VERSION %d,\n"
190 "current SMB_IDMAP_INTERFACE_VERSION is %d.\n"
191 "Please recompile against the current version "
192 "of samba!\n",
193 version, SMB_IDMAP_INTERFACE_VERSION));
194 return NT_STATUS_OBJECT_TYPE_MISMATCH;
197 if (!name || !name[0] || !methods) {
198 DEBUG(0,("Called with NULL pointer or empty name!\n"));
199 return NT_STATUS_INVALID_PARAMETER;
202 test = get_alloc_methods(alloc_backends, name);
203 if (test) {
204 DEBUG(0,("idmap_alloc module %s already registered!\n", name));
205 return NT_STATUS_OBJECT_NAME_COLLISION;
208 entry = talloc(NULL, struct idmap_alloc_backend);
209 if ( ! entry) {
210 DEBUG(0,("Out of memory!\n"));
211 return NT_STATUS_NO_MEMORY;
213 entry->name = talloc_strdup(entry, name);
214 if ( ! entry->name) {
215 DEBUG(0,("Out of memory!\n"));
216 return NT_STATUS_NO_MEMORY;
218 entry->methods = methods;
220 DLIST_ADD(alloc_backends, entry);
221 DEBUG(5, ("Successfully added idmap alloc backend '%s'\n", name));
222 return NT_STATUS_OK;
225 static int close_domain_destructor(struct idmap_domain *dom)
227 NTSTATUS ret;
229 ret = dom->methods->close_fn(dom);
230 if (!NT_STATUS_IS_OK(ret)) {
231 DEBUG(3, ("Failed to close idmap domain [%s]!\n", dom->name));
234 return 0;
237 static bool parse_idmap_module(TALLOC_CTX *mem_ctx, const char *param,
238 char **pmodulename, char **pargs)
240 char *modulename;
241 char *args;
243 if (strncmp(param, "idmap_", 6) == 0) {
244 param += 6;
245 DEBUG(1, ("idmap_init: idmap backend uses deprecated "
246 "'idmap_' prefix. Please replace 'idmap_%s' by "
247 "'%s'\n", param, param));
250 modulename = talloc_strdup(mem_ctx, param);
251 if (modulename == NULL) {
252 return false;
255 args = strchr(modulename, ':');
256 if (args == NULL) {
257 *pmodulename = modulename;
258 *pargs = NULL;
259 return true;
262 *args = '\0';
264 args = talloc_strdup(mem_ctx, args+1);
265 if (args == NULL) {
266 TALLOC_FREE(modulename);
267 return false;
270 *pmodulename = modulename;
271 *pargs = args;
272 return true;
276 * Initialize a domain structure
277 * @param[in] mem_ctx memory context for the result
278 * @param[in] domainname which domain is this for
279 * @param[in] modulename which backend module
280 * @param[in] params parameter to pass to the init function
281 * @result The initialized structure
283 static struct idmap_domain *idmap_init_domain(TALLOC_CTX *mem_ctx,
284 const char *domainname,
285 const char *modulename,
286 const char *params)
288 struct idmap_domain *result;
289 NTSTATUS status;
291 result = talloc_zero(mem_ctx, struct idmap_domain);
292 if (result == NULL) {
293 DEBUG(0, ("talloc failed\n"));
294 return NULL;
297 result->name = talloc_strdup(result, domainname);
298 if (result->name == NULL) {
299 DEBUG(0, ("talloc failed\n"));
300 goto fail;
303 result->methods = get_methods(backends, modulename);
304 if (result->methods == NULL) {
305 DEBUG(3, ("idmap backend %s not found\n", modulename));
307 status = smb_probe_module("idmap", modulename);
308 if (!NT_STATUS_IS_OK(status)) {
309 DEBUG(3, ("Could not probe idmap module %s\n",
310 modulename));
311 goto fail;
314 result->methods = get_methods(backends, modulename);
316 if (result->methods == NULL) {
317 DEBUG(1, ("idmap backend %s not found\n", modulename));
318 goto fail;
321 status = result->methods->init(result, params);
322 if (!NT_STATUS_IS_OK(status)) {
323 DEBUG(1, ("idmap initialization returned %s\n",
324 nt_errstr(status)));
325 goto fail;
328 talloc_set_destructor(result, close_domain_destructor);
330 return result;
332 fail:
333 TALLOC_FREE(result);
334 return NULL;
338 * Initialize the default domain structure
339 * @param[in] mem_ctx memory context for the result
340 * @result The default domain structure
342 * This routine takes the module name from the "idmap backend" parameter,
343 * passing a possible parameter like ldap:ldap://ldap-url/ to the module.
346 static struct idmap_domain *idmap_init_default_domain(TALLOC_CTX *mem_ctx)
348 struct idmap_domain *result;
349 char *modulename;
350 char *params;
352 DEBUG(10, ("idmap_init_default_domain: calling static_init_idmap\n"));
354 static_init_idmap;
356 if (!parse_idmap_module(talloc_tos(), lp_idmap_backend(), &modulename,
357 &params)) {
358 DEBUG(1, ("parse_idmap_module failed\n"));
359 return NULL;
362 DEBUG(3, ("idmap_init: using '%s' as remote backend\n", modulename));
364 result = idmap_init_domain(mem_ctx, "*", modulename, params);
365 if (result == NULL) {
366 goto fail;
369 TALLOC_FREE(modulename);
370 TALLOC_FREE(params);
371 return result;
373 fail:
374 TALLOC_FREE(modulename);
375 TALLOC_FREE(params);
376 TALLOC_FREE(result);
377 return NULL;
381 * Initialize a named domain structure
382 * @param[in] mem_ctx memory context for the result
383 * @param[in] domname the domain name
384 * @result The default domain structure
386 * This routine looks at the "idmap config <domname>" parameters to figure out
387 * the configuration.
390 static struct idmap_domain *idmap_init_named_domain(TALLOC_CTX *mem_ctx,
391 const char *domname)
393 struct idmap_domain *result = NULL;
394 char *config_option;
395 const char *backend;
397 config_option = talloc_asprintf(talloc_tos(), "idmap config %s",
398 domname);
399 if (config_option == NULL) {
400 DEBUG(0, ("talloc failed\n"));
401 goto fail;
404 backend = lp_parm_const_string(-1, config_option, "backend", NULL);
405 if (backend == NULL) {
406 DEBUG(1, ("no backend defined for %s\n", config_option));
407 goto fail;
410 result = idmap_init_domain(mem_ctx, domname, backend, NULL);
411 if (result == NULL) {
412 goto fail;
415 TALLOC_FREE(config_option);
416 return result;
418 fail:
419 TALLOC_FREE(config_option);
420 TALLOC_FREE(result);
421 return NULL;
425 * Initialize the passdb domain structure
426 * @param[in] mem_ctx memory context for the result
427 * @result The default domain structure
429 * No config, passdb has its own configuration.
432 static struct idmap_domain *idmap_init_passdb_domain(TALLOC_CTX *mem_ctx)
434 if (passdb_idmap_domain != NULL) {
435 return passdb_idmap_domain;
438 passdb_idmap_domain = idmap_init_domain(NULL, get_global_sam_name(),
439 "passdb", NULL);
440 if (passdb_idmap_domain == NULL) {
441 DEBUG(1, ("Could not init passdb idmap domain\n"));
444 return passdb_idmap_domain;
448 * Find a domain struct according to a domain name
449 * @param[in] domname Domain name to get the config for
450 * @result The default domain structure that fits
452 * This is the central routine in the winbindd-idmap child to pick the correct
453 * domain for looking up IDs. If domname is NULL or empty, we use the default
454 * domain. If it contains something, we try to use idmap_init_named_domain()
455 * to fetch the correct backend.
457 * The choice about "domname" is being made by the winbind parent, look at the
458 * "have_idmap_config" of "struct winbindd_domain" which is set in
459 * add_trusted_domain.
462 static struct idmap_domain *idmap_find_domain(const char *domname)
464 struct idmap_domain *result;
465 int i;
468 * Always init the default domain, we can't go without one
470 if (default_idmap_domain == NULL) {
471 default_idmap_domain = idmap_init_default_domain(NULL);
473 if (default_idmap_domain == NULL) {
474 return NULL;
477 if ((domname == NULL) || (domname[0] == '\0')) {
478 return default_idmap_domain;
481 for (i=0; i<num_domains; i++) {
482 if (strequal(idmap_domains[i]->name, domname)) {
483 return idmap_domains[i];
487 if (idmap_domains == NULL) {
489 * talloc context for all idmap domains
491 idmap_domains = TALLOC_ARRAY(NULL, struct idmap_domain *, 1);
494 if (idmap_domains == NULL) {
495 DEBUG(0, ("talloc failed\n"));
496 return NULL;
499 result = idmap_init_named_domain(idmap_domains, domname);
500 if (result == NULL) {
502 * Could not init that domain -- try the default one
504 return default_idmap_domain;
507 ADD_TO_ARRAY(idmap_domains, struct idmap_domain *, result,
508 &idmap_domains, &num_domains);
509 return result;
512 void idmap_close(void)
514 if (idmap_alloc_ctx) {
515 idmap_alloc_ctx->methods->close_fn();
516 idmap_alloc_ctx->methods = NULL;
518 alloc_backends = NULL;
519 TALLOC_FREE(default_idmap_domain);
520 TALLOC_FREE(passdb_idmap_domain);
521 TALLOC_FREE(idmap_domains);
522 num_domains = 0;
526 * Initialize the idmap alloc backend
527 * @param[out] ctx Where to put the alloc_ctx?
528 * @result Did it work fine?
530 * This routine first looks at "idmap alloc backend" and if that is not
531 * defined, it uses "idmap backend" for the module name.
533 static NTSTATUS idmap_alloc_init(struct idmap_alloc_context **ctx)
535 const char *backend;
536 char *modulename, *params;
537 NTSTATUS ret = NT_STATUS_NO_MEMORY;;
539 if (idmap_alloc_ctx != NULL) {
540 *ctx = idmap_alloc_ctx;
541 return NT_STATUS_OK;
544 idmap_alloc_ctx = talloc(NULL, struct idmap_alloc_context);
545 if (idmap_alloc_ctx == NULL) {
546 DEBUG(0, ("talloc failed\n"));
547 goto fail;
550 backend = lp_idmap_alloc_backend();
551 if ((backend == NULL) || (backend[0] == '\0')) {
552 backend = lp_idmap_backend();
555 if (backend == NULL) {
556 DEBUG(3, ("no idmap alloc backend defined\n"));
557 ret = NT_STATUS_INVALID_PARAMETER;
558 goto fail;
561 if (!parse_idmap_module(idmap_alloc_ctx, backend, &modulename,
562 &params)) {
563 DEBUG(1, ("parse_idmap_module %s failed\n", backend));
564 goto fail;
567 idmap_alloc_ctx->methods = get_alloc_methods(alloc_backends,
568 modulename);
570 if (idmap_alloc_ctx->methods == NULL) {
571 ret = smb_probe_module("idmap", modulename);
572 if (NT_STATUS_IS_OK(ret)) {
573 idmap_alloc_ctx->methods =
574 get_alloc_methods(alloc_backends,
575 modulename);
579 if (idmap_alloc_ctx->methods == NULL) {
580 DEBUG(1, ("could not find idmap alloc module %s\n", backend));
581 ret = NT_STATUS_INVALID_PARAMETER;
582 goto fail;
585 ret = idmap_alloc_ctx->methods->init(params);
587 if (!NT_STATUS_IS_OK(ret)) {
588 DEBUG(0, ("ERROR: Initialization failed for alloc "
589 "backend, deferred!\n"));
590 goto fail;
593 TALLOC_FREE(modulename);
594 TALLOC_FREE(params);
596 *ctx = idmap_alloc_ctx;
597 return NT_STATUS_OK;
599 fail:
600 TALLOC_FREE(idmap_alloc_ctx);
601 return ret;
604 /**************************************************************************
605 idmap allocator interface functions
606 **************************************************************************/
608 NTSTATUS idmap_allocate_uid(struct unixid *id)
610 struct idmap_alloc_context *ctx;
611 NTSTATUS ret;
613 if (!NT_STATUS_IS_OK(ret = idmap_alloc_init(&ctx))) {
614 return ret;
617 id->type = ID_TYPE_UID;
618 return ctx->methods->allocate_id(id);
621 NTSTATUS idmap_allocate_gid(struct unixid *id)
623 struct idmap_alloc_context *ctx;
624 NTSTATUS ret;
626 if (!NT_STATUS_IS_OK(ret = idmap_alloc_init(&ctx))) {
627 return ret;
630 id->type = ID_TYPE_GID;
631 return ctx->methods->allocate_id(id);
634 NTSTATUS idmap_set_uid_hwm(struct unixid *id)
636 struct idmap_alloc_context *ctx;
637 NTSTATUS ret;
639 if (!NT_STATUS_IS_OK(ret = idmap_alloc_init(&ctx))) {
640 return ret;
643 id->type = ID_TYPE_UID;
644 return ctx->methods->set_id_hwm(id);
647 NTSTATUS idmap_set_gid_hwm(struct unixid *id)
649 struct idmap_alloc_context *ctx;
650 NTSTATUS ret;
652 if (!NT_STATUS_IS_OK(ret = idmap_alloc_init(&ctx))) {
653 return ret;
656 id->type = ID_TYPE_GID;
657 return ctx->methods->set_id_hwm(id);
660 NTSTATUS idmap_new_mapping(const struct dom_sid *psid, enum id_type type,
661 struct unixid *pxid)
663 struct dom_sid sid;
664 struct idmap_domain *dom;
665 struct id_map map;
666 NTSTATUS status;
668 dom = idmap_find_domain(NULL);
669 if (dom == NULL) {
670 DEBUG(3, ("no default domain, no place to write\n"));
671 return NT_STATUS_ACCESS_DENIED;
673 if (dom->methods->set_mapping == NULL) {
674 DEBUG(3, ("default domain not writable\n"));
675 return NT_STATUS_MEDIA_WRITE_PROTECTED;
678 sid_copy(&sid, psid);
679 map.sid = &sid;
680 map.xid.type = type;
682 switch (type) {
683 case ID_TYPE_UID:
684 status = idmap_allocate_uid(&map.xid);
685 break;
686 case ID_TYPE_GID:
687 status = idmap_allocate_gid(&map.xid);
688 break;
689 default:
690 status = NT_STATUS_INVALID_PARAMETER;
691 break;
694 if (!NT_STATUS_IS_OK(status)) {
695 DEBUG(3, ("Could not allocate id: %s\n", nt_errstr(status)));
696 return status;
699 map.status = ID_MAPPED;
701 DEBUG(10, ("Setting mapping: %s <-> %s %lu\n",
702 sid_string_dbg(map.sid),
703 (map.xid.type == ID_TYPE_UID) ? "UID" : "GID",
704 (unsigned long)map.xid.id));
706 status = dom->methods->set_mapping(dom, &map);
708 if (!NT_STATUS_IS_OK(status)) {
709 DEBUG(3, ("Could not store the new mapping: %s\n",
710 nt_errstr(status)));
711 return status;
714 *pxid = map.xid;
716 return NT_STATUS_OK;
719 NTSTATUS idmap_backends_unixid_to_sid(const char *domname, struct id_map *id)
721 struct idmap_domain *dom;
722 struct id_map *maps[2];
724 maps[0] = id;
725 maps[1] = NULL;
728 * Always give passdb a chance first
731 dom = idmap_init_passdb_domain(NULL);
732 if ((dom != NULL)
733 && NT_STATUS_IS_OK(dom->methods->unixids_to_sids(dom, maps))) {
734 return NT_STATUS_OK;
737 dom = idmap_find_domain(domname);
738 if (dom == NULL) {
739 return NT_STATUS_NONE_MAPPED;
742 return dom->methods->unixids_to_sids(dom, maps);
745 NTSTATUS idmap_backends_sid_to_unixid(const char *domain, struct id_map *id)
747 struct idmap_domain *dom;
748 struct id_map *maps[2];
750 maps[0] = id;
751 maps[1] = NULL;
753 if (sid_check_is_in_builtin(id->sid)
754 || (sid_check_is_in_our_domain(id->sid))) {
756 dom = idmap_init_passdb_domain(NULL);
757 if (dom == NULL) {
758 return NT_STATUS_NONE_MAPPED;
760 return dom->methods->sids_to_unixids(dom, maps);
763 dom = idmap_find_domain(domain);
764 if (dom == NULL) {
765 return NT_STATUS_NONE_MAPPED;
768 return dom->methods->sids_to_unixids(dom, maps);
771 NTSTATUS idmap_set_mapping(const struct id_map *map)
773 struct idmap_domain *dom;
775 dom = idmap_find_domain(NULL);
776 if (dom == NULL) {
777 DEBUG(3, ("no default domain, no place to write\n"));
778 return NT_STATUS_ACCESS_DENIED;
780 if (dom->methods->set_mapping == NULL) {
781 DEBUG(3, ("default domain not writable\n"));
782 return NT_STATUS_MEDIA_WRITE_PROTECTED;
785 return dom->methods->set_mapping(dom, map);