source: vendor/current/source3/winbindd/idmap.c

Last change on this file was 988, checked in by Silvan Scherrer, 9 years ago

Samba Server: update vendor to version 4.4.3

File size: 13.6 KB
Line 
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
9
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.
14
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.
19
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/>.
22*/
23
24#include "includes.h"
25#include "winbindd.h"
26#include "idmap.h"
27#include "lib/util_sid_passdb.h"
28#include "passdb.h"
29
30#undef DBGC_CLASS
31#define DBGC_CLASS DBGC_IDMAP
32
33static_decl_idmap;
34
35/**
36 * Pointer to the backend methods. Modules register themselves here via
37 * smb_register_idmap.
38 */
39
40struct idmap_backend {
41 const char *name;
42 struct idmap_methods *methods;
43 struct idmap_backend *prev, *next;
44};
45static struct idmap_backend *backends = NULL;
46
47/**
48 * Default idmap domain configured via "idmap backend".
49 */
50static struct idmap_domain *default_idmap_domain;
51
52/**
53 * Passdb idmap domain, not configurable. winbind must always give passdb a
54 * chance to map ids.
55 */
56static struct idmap_domain *passdb_idmap_domain;
57
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.
63 */
64static struct idmap_domain **idmap_domains = NULL;
65static int num_domains = 0;
66
67static struct idmap_domain *idmap_init_named_domain(TALLOC_CTX *mem_ctx,
68 const char *domname);
69static struct idmap_domain *idmap_init_domain(TALLOC_CTX *mem_ctx,
70 const char *domainname,
71 const char *modulename,
72 bool check_range);
73static bool idmap_found_domain_backend(
74 const char *string, regmatch_t matches[], void *private_data);
75
76static bool idmap_init(void)
77{
78 static bool initialized;
79 int ret;
80
81 if (initialized) {
82 return true;
83 }
84
85 DEBUG(10, ("idmap_init(): calling static_init_idmap\n"));
86
87 static_init_idmap;
88
89 initialized = true;
90
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;
95 }
96 }
97
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;
103 }
104
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;
110 }
111
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;
118 }
119
120 return true;
121}
122
123bool domain_has_idmap_config(const char *domname)
124{
125 int i;
126 char *config_option;
127 const char *range = NULL;
128 const char *backend = NULL;
129 bool ok;
130
131 ok = idmap_init();
132 if (!ok) {
133 return false;
134 }
135
136 for (i=0; i<num_domains; i++) {
137 if (strequal(idmap_domains[i]->name, domname)) {
138 return true;
139 }
140 }
141
142 /* fallback: also check loadparm */
143
144 config_option = talloc_asprintf(talloc_tos(), "idmap config %s",
145 domname);
146 if (config_option == NULL) {
147 DEBUG(0, ("out of memory\n"));
148 return false;
149 }
150
151 range = lp_parm_const_string(-1, config_option, "range", NULL);
152 backend = lp_parm_const_string(-1, config_option, "backend", NULL);
153 if (range != NULL && backend != NULL) {
154 DEBUG(5, ("idmap configuration specified for domain '%s'\n",
155 domname));
156 TALLOC_FREE(config_option);
157 return true;
158 }
159
160 TALLOC_FREE(config_option);
161 return false;
162}
163
164static bool idmap_found_domain_backend(
165 const char *string, regmatch_t matches[], void *private_data)
166{
167 if (matches[1].rm_so == -1) {
168 DBG_WARNING("Found match, but no name??\n");
169 return false;
170 }
171
172 {
173 struct idmap_domain *dom, **tmp;
174 regoff_t len = matches[1].rm_eo - matches[1].rm_so;
175 char domname[len+1];
176
177 memcpy(domname, string + matches[1].rm_so, len);
178 domname[len] = '\0';
179
180 DBG_DEBUG("Found idmap domain \"%s\"\n", domname);
181
182 if (strcmp(domname, "*") == 0) {
183 return false;
184 }
185
186 dom = idmap_init_named_domain(idmap_domains, domname);
187 if (dom == NULL) {
188 DBG_NOTICE("Could not init idmap domain %s\n",
189 domname);
190 return false;
191 }
192
193 tmp = talloc_realloc(idmap_domains, idmap_domains,
194 struct idmap_domain *, num_domains + 1);
195 if (tmp == NULL) {
196 DBG_WARNING("talloc_realloc failed\n");
197 TALLOC_FREE(dom);
198 return false;
199 }
200 idmap_domains = tmp;
201 idmap_domains[num_domains] = dom;
202 num_domains += 1;
203 }
204
205 return false;
206}
207
208static struct idmap_methods *get_methods(const char *name)
209{
210 struct idmap_backend *b;
211
212 for (b = backends; b; b = b->next) {
213 if (strequal(b->name, name)) {
214 return b->methods;
215 }
216 }
217
218 return NULL;
219}
220
221bool idmap_is_offline(void)
222{
223 return ( lp_winbind_offline_logon() &&
224 get_global_winbindd_state_offline() );
225}
226
227bool idmap_is_online(void)
228{
229 return !idmap_is_offline();
230}
231
232/**********************************************************************
233 Allow a module to register itself as a method.
234**********************************************************************/
235
236NTSTATUS smb_register_idmap(int version, const char *name,
237 struct idmap_methods *methods)
238{
239 struct idmap_backend *entry;
240
241 if ((version != SMB_IDMAP_INTERFACE_VERSION)) {
242 DEBUG(0, ("Failed to register idmap module.\n"
243 "The module was compiled against "
244 "SMB_IDMAP_INTERFACE_VERSION %d,\n"
245 "current SMB_IDMAP_INTERFACE_VERSION is %d.\n"
246 "Please recompile against the current version "
247 "of samba!\n",
248 version, SMB_IDMAP_INTERFACE_VERSION));
249 return NT_STATUS_OBJECT_TYPE_MISMATCH;
250 }
251
252 if (!name || !name[0] || !methods) {
253 DEBUG(0,("Called with NULL pointer or empty name!\n"));
254 return NT_STATUS_INVALID_PARAMETER;
255 }
256
257 for (entry = backends; entry != NULL; entry = entry->next) {
258 if (strequal(entry->name, name)) {
259 DEBUG(5,("Idmap module %s already registered!\n",
260 name));
261 return NT_STATUS_OBJECT_NAME_COLLISION;
262 }
263 }
264
265 entry = talloc(NULL, struct idmap_backend);
266 if ( ! entry) {
267 DEBUG(0,("Out of memory!\n"));
268 TALLOC_FREE(entry);
269 return NT_STATUS_NO_MEMORY;
270 }
271 entry->name = talloc_strdup(entry, name);
272 if ( ! entry->name) {
273 DEBUG(0,("Out of memory!\n"));
274 TALLOC_FREE(entry);
275 return NT_STATUS_NO_MEMORY;
276 }
277 entry->methods = methods;
278
279 DLIST_ADD(backends, entry);
280 DEBUG(5, ("Successfully added idmap backend '%s'\n", name));
281 return NT_STATUS_OK;
282}
283
284/**
285 * Initialize a domain structure
286 * @param[in] mem_ctx memory context for the result
287 * @param[in] domainname which domain is this for
288 * @param[in] modulename which backend module
289 * @param[in] check_range whether range checking should be done
290 * @result The initialized structure
291 */
292static struct idmap_domain *idmap_init_domain(TALLOC_CTX *mem_ctx,
293 const char *domainname,
294 const char *modulename,
295 bool check_range)
296{
297 struct idmap_domain *result;
298 NTSTATUS status;
299 char *config_option = NULL;
300 const char *range;
301 unsigned low_id = 0;
302 unsigned high_id = 0;
303
304 result = talloc_zero(mem_ctx, struct idmap_domain);
305 if (result == NULL) {
306 DEBUG(0, ("talloc failed\n"));
307 return NULL;
308 }
309
310 result->name = talloc_strdup(result, domainname);
311 if (result->name == NULL) {
312 DEBUG(0, ("talloc failed\n"));
313 goto fail;
314 }
315
316 /*
317 * Check whether the requested backend module exists and
318 * load the methods.
319 */
320
321 result->methods = get_methods(modulename);
322 if (result->methods == NULL) {
323 DEBUG(3, ("idmap backend %s not found\n", modulename));
324
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;
330 }
331
332 result->methods = get_methods(modulename);
333 }
334 if (result->methods == NULL) {
335 DEBUG(1, ("idmap backend %s not found\n", modulename));
336 goto fail;
337 }
338
339 /*
340 * load ranges and read only information from the config
341 */
342
343 config_option = talloc_asprintf(result, "idmap config %s",
344 result->name);
345 if (config_option == NULL) {
346 DEBUG(0, ("Out of memory!\n"));
347 goto fail;
348 }
349
350 result->read_only = lp_parm_bool(-1, config_option, "read only", false);
351 range = lp_parm_const_string(-1, config_option, "range", NULL);
352
353 talloc_free(config_option);
354
355 if (range == NULL) {
356 if (check_range) {
357 DEBUG(1, ("idmap range not specified for domain %s\n",
358 result->name));
359 goto fail;
360 }
361 } else if (sscanf(range, "%u - %u", &low_id, &high_id) != 2)
362 {
363 DEBUG(1, ("invalid range '%s' specified for domain "
364 "'%s'\n", range, result->name));
365 if (check_range) {
366 goto fail;
367 }
368 } else if (low_id > high_id) {
369 DEBUG(1, ("Error: invalid idmap range detected: %u - %u\n",
370 low_id, high_id));
371 if (check_range) {
372 goto fail;
373 }
374 }
375
376 result->low_id = low_id;
377 result->high_id = high_id;
378
379 status = result->methods->init(result);
380 if (!NT_STATUS_IS_OK(status)) {
381 DEBUG(1, ("idmap initialization returned %s\n",
382 nt_errstr(status)));
383 goto fail;
384 }
385
386 return result;
387
388fail:
389 TALLOC_FREE(result);
390 return NULL;
391}
392
393/**
394 * Initialize a named domain structure
395 * @param[in] mem_ctx memory context for the result
396 * @param[in] domname the domain name
397 * @result The default domain structure
398 *
399 * This routine looks at the "idmap config <domname>" parameters to figure out
400 * the configuration.
401 */
402
403static struct idmap_domain *idmap_init_named_domain(TALLOC_CTX *mem_ctx,
404 const char *domname)
405{
406 struct idmap_domain *result = NULL;
407 char *config_option;
408 const char *backend;
409 bool ok;
410
411 ok = idmap_init();
412 if (!ok) {
413 return NULL;
414 }
415
416 config_option = talloc_asprintf(talloc_tos(), "idmap config %s",
417 domname);
418 if (config_option == NULL) {
419 DEBUG(0, ("talloc failed\n"));
420 goto fail;
421 }
422
423 backend = lp_parm_const_string(-1, config_option, "backend", NULL);
424 if (backend == NULL) {
425 DEBUG(10, ("no idmap backend configured for domain '%s'\n",
426 domname));
427 goto fail;
428 }
429
430 result = idmap_init_domain(mem_ctx, domname, backend, true);
431 if (result == NULL) {
432 goto fail;
433 }
434
435 TALLOC_FREE(config_option);
436 return result;
437
438fail:
439 TALLOC_FREE(config_option);
440 TALLOC_FREE(result);
441 return NULL;
442}
443
444/**
445 * Find a domain struct according to a domain name
446 * @param[in] domname Domain name to get the config for
447 * @result The default domain structure that fits
448 *
449 * This is the central routine in the winbindd-idmap child to pick the correct
450 * domain for looking up IDs. If domname is NULL or empty, we use the default
451 * domain. If it contains something, we try to use idmap_init_named_domain()
452 * to fetch the correct backend.
453 *
454 * The choice about "domname" is being made by the winbind parent, look at the
455 * "have_idmap_config" of "struct winbindd_domain" which is set in
456 * add_trusted_domain.
457 */
458
459static struct idmap_domain *idmap_find_domain(const char *domname)
460{
461 bool ok;
462 int i;
463
464 DEBUG(10, ("idmap_find_domain called for domain '%s'\n",
465 domname?domname:"NULL"));
466
467 ok = idmap_init();
468 if (!ok) {
469 return NULL;
470 }
471
472 if ((domname == NULL) || (domname[0] == '\0')) {
473 return default_idmap_domain;
474 }
475
476 for (i=0; i<num_domains; i++) {
477 if (strequal(idmap_domains[i]->name, domname)) {
478 return idmap_domains[i];
479 }
480 }
481
482 return default_idmap_domain;
483}
484
485struct idmap_domain *idmap_find_domain_with_sid(const char *domname,
486 const struct dom_sid *sid)
487{
488 bool ok;
489
490 ok = idmap_init();
491 if (!ok) {
492 return NULL;
493 }
494
495 if (sid_check_is_for_passdb(sid)) {
496 return passdb_idmap_domain;
497 }
498
499 return idmap_find_domain(domname);
500}
501
502void idmap_close(void)
503{
504 TALLOC_FREE(default_idmap_domain);
505 TALLOC_FREE(passdb_idmap_domain);
506 TALLOC_FREE(idmap_domains);
507 num_domains = 0;
508}
509
510/**************************************************************************
511 idmap allocator interface functions
512**************************************************************************/
513
514static NTSTATUS idmap_allocate_unixid(struct unixid *id)
515{
516 struct idmap_domain *dom;
517 NTSTATUS ret;
518
519 dom = idmap_find_domain(NULL);
520
521 if (dom == NULL) {
522 return NT_STATUS_UNSUCCESSFUL;
523 }
524
525 if (dom->methods->allocate_id == NULL) {
526 return NT_STATUS_NOT_IMPLEMENTED;
527 }
528
529 ret = dom->methods->allocate_id(dom, id);
530
531 return ret;
532}
533
534
535NTSTATUS idmap_allocate_uid(struct unixid *id)
536{
537 id->type = ID_TYPE_UID;
538 return idmap_allocate_unixid(id);
539}
540
541NTSTATUS idmap_allocate_gid(struct unixid *id)
542{
543 id->type = ID_TYPE_GID;
544 return idmap_allocate_unixid(id);
545}
546
547NTSTATUS idmap_backends_unixid_to_sid(struct id_map *id)
548{
549 struct idmap_domain *dom;
550 struct id_map *maps[2];
551 bool ok;
552 int i;
553
554 ok = idmap_init();
555 if (!ok) {
556 return NT_STATUS_NONE_MAPPED;
557 }
558
559 DEBUG(10, ("idmap_backend_unixid_to_sid: xid = %d (type %d)\n",
560 id->xid.id, id->xid.type));
561
562 maps[0] = id;
563 maps[1] = NULL;
564
565 /*
566 * Always give passdb a chance first
567 */
568
569 dom = passdb_idmap_domain;
570 if ((dom != NULL)
571 && NT_STATUS_IS_OK(dom->methods->unixids_to_sids(dom, maps))
572 && id->status == ID_MAPPED) {
573 return NT_STATUS_OK;
574 }
575
576 dom = default_idmap_domain;
577
578 for (i=0; i<num_domains; i++) {
579 if ((id->xid.id >= idmap_domains[i]->low_id) &&
580 (id->xid.id <= idmap_domains[i]->high_id)) {
581 dom = idmap_domains[i];
582 break;
583 }
584 }
585
586 if (dom == NULL) {
587 return NT_STATUS_NONE_MAPPED;
588 }
589
590 return dom->methods->unixids_to_sids(dom, maps);
591}
Note: See TracBrowser for help on using the repository browser.