s3: Fix Coverity ID 2195: NO_EFFECT
[Samba.git] / source3 / winbindd / idmap.c
blob49d7c3aa6122f2911ef099419c4f77226e6ef806
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;
157 * Initialize a domain structure
158 * @param[in] mem_ctx memory context for the result
159 * @param[in] domainname which domain is this for
160 * @param[in] modulename which backend module
161 * @param[in] check_range whether range checking should be done
162 * @result The initialized structure
164 static struct idmap_domain *idmap_init_domain(TALLOC_CTX *mem_ctx,
165 const char *domainname,
166 const char *modulename,
167 bool check_range)
169 struct idmap_domain *result;
170 NTSTATUS status;
171 char *config_option = NULL;
172 const char *range;
174 result = talloc_zero(mem_ctx, struct idmap_domain);
175 if (result == NULL) {
176 DEBUG(0, ("talloc failed\n"));
177 return NULL;
180 result->name = talloc_strdup(result, domainname);
181 if (result->name == NULL) {
182 DEBUG(0, ("talloc failed\n"));
183 goto fail;
187 * load ranges and read only information from the config
190 config_option = talloc_asprintf(result, "idmap config %s",
191 result->name);
192 if (config_option == NULL) {
193 DEBUG(0, ("Out of memory!\n"));
194 goto fail;
197 range = lp_parm_const_string(-1, config_option, "range", NULL);
198 if (range == NULL) {
199 DEBUG(1, ("idmap range not specified for domain %s\n",
200 result->name));
201 if (check_range) {
202 goto fail;
204 } else if (sscanf(range, "%u - %u", &result->low_id,
205 &result->high_id) != 2)
207 DEBUG(1, ("invalid range '%s' specified for domain "
208 "'%s'\n", range, result->name));
209 if (check_range) {
210 goto fail;
214 result->read_only = lp_parm_bool(-1, config_option, "read only", false);
216 talloc_free(config_option);
218 if (result->low_id > result->high_id) {
219 DEBUG(1, ("Error: invalid idmap range detected: %lu - %lu\n",
220 (unsigned long)result->low_id,
221 (unsigned long)result->high_id));
222 if (check_range) {
223 goto fail;
227 result->methods = get_methods(modulename);
228 if (result->methods == NULL) {
229 DEBUG(3, ("idmap backend %s not found\n", modulename));
231 status = smb_probe_module("idmap", modulename);
232 if (!NT_STATUS_IS_OK(status)) {
233 DEBUG(3, ("Could not probe idmap module %s\n",
234 modulename));
235 goto fail;
238 result->methods = get_methods(modulename);
240 if (result->methods == NULL) {
241 DEBUG(1, ("idmap backend %s not found\n", modulename));
242 goto fail;
245 status = result->methods->init(result);
246 if (!NT_STATUS_IS_OK(status)) {
247 DEBUG(1, ("idmap initialization returned %s\n",
248 nt_errstr(status)));
249 goto fail;
252 return result;
254 fail:
255 TALLOC_FREE(result);
256 return NULL;
260 * Initialize a named domain structure
261 * @param[in] mem_ctx memory context for the result
262 * @param[in] domname the domain name
263 * @result The default domain structure
265 * This routine looks at the "idmap config <domname>" parameters to figure out
266 * the configuration.
269 static struct idmap_domain *idmap_init_named_domain(TALLOC_CTX *mem_ctx,
270 const char *domname)
272 struct idmap_domain *result = NULL;
273 char *config_option;
274 const char *backend;
276 idmap_init();
278 config_option = talloc_asprintf(talloc_tos(), "idmap config %s",
279 domname);
280 if (config_option == NULL) {
281 DEBUG(0, ("talloc failed\n"));
282 goto fail;
285 backend = lp_parm_const_string(-1, config_option, "backend", NULL);
286 if (backend == NULL) {
287 DEBUG(1, ("no backend defined for %s\n", config_option));
288 goto fail;
291 result = idmap_init_domain(mem_ctx, domname, backend, true);
292 if (result == NULL) {
293 goto fail;
296 TALLOC_FREE(config_option);
297 return result;
299 fail:
300 TALLOC_FREE(config_option);
301 TALLOC_FREE(result);
302 return NULL;
306 * Initialize the default domain structure
307 * @param[in] mem_ctx memory context for the result
308 * @result The default domain structure
310 * This routine takes the module name from the "idmap backend" parameter,
311 * passing a possible parameter like ldap:ldap://ldap-url/ to the module.
314 static struct idmap_domain *idmap_init_default_domain(TALLOC_CTX *mem_ctx)
316 return idmap_init_named_domain(mem_ctx, "*");
320 * Initialize the passdb domain structure
321 * @param[in] mem_ctx memory context for the result
322 * @result The default domain structure
324 * No config, passdb has its own configuration.
327 static struct idmap_domain *idmap_init_passdb_domain(TALLOC_CTX *mem_ctx)
329 idmap_init();
332 * Always init the default domain, we can't go without one
334 if (default_idmap_domain == NULL) {
335 default_idmap_domain = idmap_init_default_domain(NULL);
337 if (default_idmap_domain == NULL) {
338 return NULL;
341 if (passdb_idmap_domain != NULL) {
342 return passdb_idmap_domain;
345 passdb_idmap_domain = idmap_init_domain(NULL, get_global_sam_name(),
346 "passdb", false);
347 if (passdb_idmap_domain == NULL) {
348 DEBUG(1, ("Could not init passdb idmap domain\n"));
351 return passdb_idmap_domain;
355 * Find a domain struct according to a domain name
356 * @param[in] domname Domain name to get the config for
357 * @result The default domain structure that fits
359 * This is the central routine in the winbindd-idmap child to pick the correct
360 * domain for looking up IDs. If domname is NULL or empty, we use the default
361 * domain. If it contains something, we try to use idmap_init_named_domain()
362 * to fetch the correct backend.
364 * The choice about "domname" is being made by the winbind parent, look at the
365 * "have_idmap_config" of "struct winbindd_domain" which is set in
366 * add_trusted_domain.
369 static struct idmap_domain *idmap_find_domain(const char *domname)
371 struct idmap_domain *result;
372 int i;
374 DEBUG(10, ("idmap_find_domain called for domain '%s'\n",
375 domname?domname:"NULL"));
377 idmap_init();
380 * Always init the default domain, we can't go without one
382 if (default_idmap_domain == NULL) {
383 default_idmap_domain = idmap_init_default_domain(NULL);
385 if (default_idmap_domain == NULL) {
386 return NULL;
389 if ((domname == NULL) || (domname[0] == '\0')) {
390 return default_idmap_domain;
393 for (i=0; i<num_domains; i++) {
394 if (strequal(idmap_domains[i]->name, domname)) {
395 return idmap_domains[i];
399 if (idmap_domains == NULL) {
401 * talloc context for all idmap domains
403 idmap_domains = TALLOC_ARRAY(NULL, struct idmap_domain *, 1);
406 if (idmap_domains == NULL) {
407 DEBUG(0, ("talloc failed\n"));
408 return NULL;
411 result = idmap_init_named_domain(idmap_domains, domname);
412 if (result == NULL) {
414 * Could not init that domain -- try the default one
416 return default_idmap_domain;
419 ADD_TO_ARRAY(idmap_domains, struct idmap_domain *, result,
420 &idmap_domains, &num_domains);
421 return result;
424 void idmap_close(void)
426 TALLOC_FREE(default_idmap_domain);
427 TALLOC_FREE(passdb_idmap_domain);
428 TALLOC_FREE(idmap_domains);
429 num_domains = 0;
432 /**************************************************************************
433 idmap allocator interface functions
434 **************************************************************************/
436 static NTSTATUS idmap_allocate_unixid(struct unixid *id)
438 struct idmap_domain *dom;
439 NTSTATUS ret;
441 dom = idmap_find_domain(NULL);
443 if (dom == NULL) {
444 return NT_STATUS_UNSUCCESSFUL;
447 if (dom->methods->allocate_id == NULL) {
448 return NT_STATUS_NOT_IMPLEMENTED;
451 ret = dom->methods->allocate_id(dom, id);
453 return ret;
457 NTSTATUS idmap_allocate_uid(struct unixid *id)
459 id->type = ID_TYPE_UID;
460 return idmap_allocate_unixid(id);
463 NTSTATUS idmap_allocate_gid(struct unixid *id)
465 id->type = ID_TYPE_GID;
466 return idmap_allocate_unixid(id);
469 NTSTATUS idmap_backends_unixid_to_sid(const char *domname, struct id_map *id)
471 struct idmap_domain *dom;
472 struct id_map *maps[2];
474 DEBUG(10, ("idmap_backend_unixid_to_sid: domain = '%s', xid = %d "
475 "(type %d)\n",
476 domname?domname:"NULL", id->xid.id, id->xid.type));
478 maps[0] = id;
479 maps[1] = NULL;
482 * Always give passdb a chance first
485 dom = idmap_init_passdb_domain(NULL);
486 if ((dom != NULL)
487 && NT_STATUS_IS_OK(dom->methods->unixids_to_sids(dom, maps))
488 && id->status == ID_MAPPED) {
489 return NT_STATUS_OK;
492 dom = idmap_find_domain(domname);
493 if (dom == NULL) {
494 return NT_STATUS_NONE_MAPPED;
497 return dom->methods->unixids_to_sids(dom, maps);
500 NTSTATUS idmap_backends_sid_to_unixid(const char *domain, struct id_map *id)
502 struct idmap_domain *dom;
503 struct id_map *maps[2];
505 DEBUG(10, ("idmap_backends_sid_to_unixid: domain = '%s', sid = [%s]\n",
506 domain?domain:"NULL", sid_string_dbg(id->sid)));
508 maps[0] = id;
509 maps[1] = NULL;
511 if (sid_check_is_in_builtin(id->sid)
512 || (sid_check_is_in_our_domain(id->sid)))
514 NTSTATUS status;
516 DEBUG(10, ("asking passdb...\n"));
518 dom = idmap_init_passdb_domain(NULL);
519 if (dom == NULL) {
520 return NT_STATUS_NONE_MAPPED;
522 status = dom->methods->sids_to_unixids(dom, maps);
524 if (NT_STATUS_IS_OK(status) && id->status == ID_MAPPED) {
525 return status;
528 DEBUG(10, ("passdb could not map.\n"));
530 return NT_STATUS_NONE_MAPPED;
533 dom = idmap_find_domain(domain);
534 if (dom == NULL) {
535 return NT_STATUS_NONE_MAPPED;
538 return dom->methods->sids_to_unixids(dom, maps);