source: branches/samba-3.2.x/source/winbindd/idmap.c@ 206

Last change on this file since 206 was 204, checked in by Herwig Bauernfeind, 16 years ago

Update 3.2 branch to 3.2.4

File size: 40.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
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.
13
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.
18
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/>.
21*/
22
23#include "includes.h"
24#include "winbindd.h"
25
26#undef DBGC_CLASS
27#define DBGC_CLASS DBGC_IDMAP
28
29static_decl_idmap;
30
31struct idmap_backend {
32 const char *name;
33 struct idmap_methods *methods;
34 struct idmap_backend *prev, *next;
35};
36
37struct idmap_alloc_backend {
38 const char *name;
39 struct idmap_alloc_methods *methods;
40 struct idmap_alloc_backend *prev, *next;
41};
42
43struct idmap_cache_ctx;
44
45struct idmap_alloc_context {
46 const char *params;
47 struct idmap_alloc_methods *methods;
48 bool initialized;
49};
50
51static TALLOC_CTX *idmap_ctx = NULL;
52static struct idmap_cache_ctx *idmap_cache;
53
54static struct idmap_backend *backends = NULL;
55static struct idmap_domain **idmap_domains = NULL;
56static int num_domains = 0;
57static int pdb_dom_num = -1;
58static int def_dom_num = -1;
59
60static struct idmap_alloc_backend *alloc_backends = NULL;
61static struct idmap_alloc_context *idmap_alloc_ctx = NULL;
62
63#define IDMAP_CHECK_RET(ret) do { \
64 if ( ! NT_STATUS_IS_OK(ret)) { \
65 DEBUG(2, ("ERROR: NTSTATUS = 0x%08x\n", NT_STATUS_V(ret))); \
66 goto done; \
67 } } while(0)
68#define IDMAP_REPORT_RET(ret) do { \
69 if ( ! NT_STATUS_IS_OK(ret)) { \
70 DEBUG(2, ("ERROR: NTSTATUS = 0x%08x\n", NT_STATUS_V(ret))); \
71 } } while(0)
72#define IDMAP_CHECK_ALLOC(mem) do { \
73 if (!mem) { \
74 DEBUG(0, ("Out of memory!\n")); ret = NT_STATUS_NO_MEMORY; \
75 goto done; \
76 } } while(0)
77
78static struct idmap_methods *get_methods(const char *name)
79{
80 struct idmap_backend *b;
81
82 for (b = backends; b; b = b->next) {
83 if (strequal(b->name, name)) {
84 return b->methods;
85 }
86 }
87
88 return NULL;
89}
90
91static struct idmap_alloc_methods *get_alloc_methods(const char *name)
92{
93 struct idmap_alloc_backend *b;
94
95 for (b = alloc_backends; b; b = b->next) {
96 if (strequal(b->name, name)) {
97 return b->methods;
98 }
99 }
100
101 return NULL;
102}
103
104bool idmap_is_offline(void)
105{
106 return ( lp_winbind_offline_logon() &&
107 get_global_winbindd_state_offline() );
108}
109
110/**********************************************************************
111 Allow a module to register itself as a method.
112**********************************************************************/
113
114NTSTATUS smb_register_idmap(int version, const char *name,
115 struct idmap_methods *methods)
116{
117 struct idmap_methods *test;
118 struct idmap_backend *entry;
119
120 if (!idmap_ctx) {
121 return NT_STATUS_INTERNAL_DB_ERROR;
122 }
123
124 if ((version != SMB_IDMAP_INTERFACE_VERSION)) {
125 DEBUG(0, ("Failed to register idmap module.\n"
126 "The module was compiled against "
127 "SMB_IDMAP_INTERFACE_VERSION %d,\n"
128 "current SMB_IDMAP_INTERFACE_VERSION is %d.\n"
129 "Please recompile against the current version "
130 "of samba!\n",
131 version, SMB_IDMAP_INTERFACE_VERSION));
132 return NT_STATUS_OBJECT_TYPE_MISMATCH;
133 }
134
135 if (!name || !name[0] || !methods) {
136 DEBUG(0,("Called with NULL pointer or empty name!\n"));
137 return NT_STATUS_INVALID_PARAMETER;
138 }
139
140 test = get_methods(name);
141 if (test) {
142 DEBUG(0,("Idmap module %s already registered!\n", name));
143 return NT_STATUS_OBJECT_NAME_COLLISION;
144 }
145
146 entry = talloc(idmap_ctx, struct idmap_backend);
147 if ( ! entry) {
148 DEBUG(0,("Out of memory!\n"));
149 return NT_STATUS_NO_MEMORY;
150 }
151 entry->name = talloc_strdup(idmap_ctx, name);
152 if ( ! entry->name) {
153 DEBUG(0,("Out of memory!\n"));
154 return NT_STATUS_NO_MEMORY;
155 }
156 entry->methods = methods;
157
158 DLIST_ADD(backends, entry);
159 DEBUG(5, ("Successfully added idmap backend '%s'\n", name));
160 return NT_STATUS_OK;
161}
162
163/**********************************************************************
164 Allow a module to register itself as a method.
165**********************************************************************/
166
167NTSTATUS smb_register_idmap_alloc(int version, const char *name,
168 struct idmap_alloc_methods *methods)
169{
170 struct idmap_alloc_methods *test;
171 struct idmap_alloc_backend *entry;
172
173 if (!idmap_ctx) {
174 return NT_STATUS_INTERNAL_DB_ERROR;
175 }
176
177 if ((version != SMB_IDMAP_INTERFACE_VERSION)) {
178 DEBUG(0, ("Failed to register idmap alloc module.\n"
179 "The module was compiled against "
180 "SMB_IDMAP_INTERFACE_VERSION %d,\n"
181 "current SMB_IDMAP_INTERFACE_VERSION is %d.\n"
182 "Please recompile against the current version "
183 "of samba!\n",
184 version, SMB_IDMAP_INTERFACE_VERSION));
185 return NT_STATUS_OBJECT_TYPE_MISMATCH;
186 }
187
188 if (!name || !name[0] || !methods) {
189 DEBUG(0,("Called with NULL pointer or empty name!\n"));
190 return NT_STATUS_INVALID_PARAMETER;
191 }
192
193 test = get_alloc_methods(name);
194 if (test) {
195 DEBUG(0,("idmap_alloc module %s already registered!\n", name));
196 return NT_STATUS_OBJECT_NAME_COLLISION;
197 }
198
199 entry = talloc(idmap_ctx, struct idmap_alloc_backend);
200 if ( ! entry) {
201 DEBUG(0,("Out of memory!\n"));
202 return NT_STATUS_NO_MEMORY;
203 }
204 entry->name = talloc_strdup(idmap_ctx, name);
205 if ( ! entry->name) {
206 DEBUG(0,("Out of memory!\n"));
207 return NT_STATUS_NO_MEMORY;
208 }
209 entry->methods = methods;
210
211 DLIST_ADD(alloc_backends, entry);
212 DEBUG(5, ("Successfully added idmap alloc backend '%s'\n", name));
213 return NT_STATUS_OK;
214}
215
216static int close_domain_destructor(struct idmap_domain *dom)
217{
218 NTSTATUS ret;
219
220 ret = dom->methods->close_fn(dom);
221 if (!NT_STATUS_IS_OK(ret)) {
222 DEBUG(3, ("Failed to close idmap domain [%s]!\n", dom->name));
223 }
224
225 return 0;
226}
227
228/**************************************************************************
229 Shutdown.
230**************************************************************************/
231
232NTSTATUS idmap_close(void)
233{
234 /* close the alloc backend first before freeing idmap_ctx */
235 if (idmap_alloc_ctx) {
236 idmap_alloc_ctx->methods->close_fn();
237 idmap_alloc_ctx->methods = NULL;
238 }
239 alloc_backends = NULL;
240
241 /* this talloc_free call will fire the talloc destructors
242 * that will free all active backends resources */
243 TALLOC_FREE(idmap_ctx);
244 idmap_cache = NULL;
245 idmap_domains = NULL;
246 backends = NULL;
247
248 return NT_STATUS_OK;
249}
250
251/****************************************************************************
252 ****************************************************************************/
253
254NTSTATUS idmap_init_cache(void)
255{
256 /* Always initialize the cache. We'll have to delay initialization
257 of backends if we are offline */
258
259 if ( idmap_ctx ) {
260 return NT_STATUS_OK;
261 }
262
263 if ( (idmap_ctx = talloc_named_const(NULL, 0, "idmap_ctx")) == NULL ) {
264 return NT_STATUS_NO_MEMORY;
265 }
266
267 if ( (idmap_cache = idmap_cache_init(idmap_ctx)) == NULL ) {
268 return NT_STATUS_UNSUCCESSFUL;
269 }
270
271 return NT_STATUS_OK;
272}
273
274/****************************************************************************
275 ****************************************************************************/
276
277NTSTATUS idmap_init(void)
278{
279 NTSTATUS ret;
280 static NTSTATUS idmap_init_status = NT_STATUS_UNSUCCESSFUL;
281 struct idmap_domain *dom;
282 char *compat_backend = NULL;
283 char *compat_params = NULL;
284 const char **dom_list = NULL;
285 const char *default_domain = NULL;
286 char *alloc_backend = NULL;
287 bool default_already_defined = False;
288 bool pri_dom_is_in_list = False;
289 int compat = 0;
290 int i;
291
292 ret = idmap_init_cache();
293 if (!NT_STATUS_IS_OK(ret))
294 return ret;
295
296 if (NT_STATUS_IS_OK(idmap_init_status)) {
297 return NT_STATUS_OK;
298 }
299
300 /* We can't reliably call intialization code here unless
301 we are online. But return NT_STATUS_OK so the upper
302 level code doesn't abort idmap lookups. */
303
304 if ( get_global_winbindd_state_offline() ) {
305 idmap_init_status = NT_STATUS_FILE_IS_OFFLINE;
306 return NT_STATUS_OK;
307 }
308
309 static_init_idmap;
310
311 dom_list = lp_idmap_domains();
312
313 if ( lp_idmap_backend() ) {
314 const char **compat_list = lp_idmap_backend();
315 char *p = NULL;
316 const char *q = NULL;
317
318 if ( dom_list ) {
319 DEBUG(0, ("WARNING: idmap backend and idmap domains are"
320 " mutually exclusive!\n"));
321 DEBUGADD(0,("idmap backend option will be IGNORED!\n"));
322 } else {
323 compat = 1;
324
325 compat_backend = talloc_strdup(idmap_ctx, *compat_list);
326
327 /* strip any leading idmap_ prefix of */
328 if (strncmp(*compat_list, "idmap_", 6) == 0 ) {
329 q = *compat_list += 6;
330 DEBUG(0, ("WARNING: idmap backend uses obsolete"
331 " and deprecated 'idmap_' prefix.\n"
332 "Please replace 'idmap_%s' by '%s' in"
333 " %s\n", q, q, get_dyn_CONFIGFILE()));
334 compat_backend = talloc_strdup(idmap_ctx, q);
335 } else {
336 compat_backend = talloc_strdup(idmap_ctx,
337 *compat_list);
338 }
339
340 if (compat_backend == NULL ) {
341 ret = NT_STATUS_NO_MEMORY;
342 goto done;
343 }
344
345 /* separate the backend and module arguements */
346 if ((p = strchr(compat_backend, ':')) != NULL) {
347 *p = '\0';
348 compat_params = p + 1;
349 }
350 }
351 } else if ( !dom_list ) {
352 /* Back compatible: without idmap domains and explicit
353 idmap backend. Taking default idmap backend: tdb */
354
355 compat = 1;
356 compat_backend = talloc_strdup( idmap_ctx, "tdb");
357 compat_params = compat_backend;
358 }
359
360 if ( ! dom_list) {
361 /* generate a list with our main domain */
362 const char ** dl;
363
364 dl = talloc_array(idmap_ctx, const char *, 2);
365 if (dl == NULL) {
366 ret = NT_STATUS_NO_MEMORY;
367 goto done;
368 }
369 dl[0] = talloc_strdup(dl, lp_workgroup());
370 if (dl[0] == NULL) {
371 ret = NT_STATUS_NO_MEMORY;
372 goto done;
373 }
374
375 /* terminate */
376 dl[1] = NULL;
377
378 dom_list = dl;
379 default_domain = dl[0];
380 }
381
382 /***************************
383 * initialize idmap domains
384 */
385 DEBUG(1, ("Initializing idmap domains\n"));
386
387 for (i=0, num_domains=0; dom_list[i]; i++) {
388 const char *parm_backend;
389 char *config_option;
390
391 /* ignore BUILTIN and local MACHINE domains */
392 if (strequal(dom_list[i], "BUILTIN")
393 || strequal(dom_list[i], get_global_sam_name()))
394 {
395 DEBUG(0,("idmap_init: Ignoring domain %s\n",
396 dom_list[i]));
397 continue;
398 }
399
400 if ((dom_list[i] != default_domain) &&
401 strequal(dom_list[i], lp_workgroup())) {
402 pri_dom_is_in_list = True;
403 }
404 /* init domain */
405
406 dom = TALLOC_ZERO_P(idmap_ctx, struct idmap_domain);
407 IDMAP_CHECK_ALLOC(dom);
408
409 dom->name = talloc_strdup(dom, dom_list[i]);
410 IDMAP_CHECK_ALLOC(dom->name);
411
412 config_option = talloc_asprintf(dom, "idmap config %s",
413 dom->name);
414 IDMAP_CHECK_ALLOC(config_option);
415
416 /* default or specific ? */
417
418 dom->default_domain = lp_parm_bool(-1, config_option,
419 "default", False);
420
421 if (dom->default_domain ||
422 (default_domain && strequal(dom_list[i], default_domain))) {
423
424 /* make sure this is set even when we match
425 * default_domain */
426 dom->default_domain = True;
427
428 if (default_already_defined) {
429 DEBUG(1, ("ERROR: Multiple domains defined as"
430 " default!\n"));
431 ret = NT_STATUS_INVALID_PARAMETER;
432 goto done;
433 }
434
435 default_already_defined = True;
436
437 }
438
439 dom->readonly = lp_parm_bool(-1, config_option,
440 "readonly", False);
441
442 /* find associated backend (default: tdb) */
443 if (compat) {
444 parm_backend = talloc_strdup(idmap_ctx, compat_backend);
445 } else {
446 parm_backend = talloc_strdup(idmap_ctx,
447 lp_parm_const_string(
448 -1, config_option,
449 "backend", "tdb"));
450 }
451 IDMAP_CHECK_ALLOC(parm_backend);
452
453 /* get the backend methods for this domain */
454 dom->methods = get_methods(parm_backend);
455
456 if ( ! dom->methods) {
457 ret = smb_probe_module("idmap", parm_backend);
458 if (NT_STATUS_IS_OK(ret)) {
459 dom->methods = get_methods(parm_backend);
460 }
461 }
462 if ( ! dom->methods) {
463 DEBUG(0, ("ERROR: Could not get methods for "
464 "backend %s\n", parm_backend));
465 ret = NT_STATUS_UNSUCCESSFUL;
466 goto done;
467 }
468
469 /* check the set_mapping function exists otherwise mark the
470 * module as readonly */
471 if ( ! dom->methods->set_mapping) {
472 DEBUG(5, ("Forcing to readonly, as this module can't"
473 " store arbitrary mappings.\n"));
474 dom->readonly = True;
475 }
476
477 /* now that we have methods,
478 * set the destructor for this domain */
479 talloc_set_destructor(dom, close_domain_destructor);
480
481 if (compat_params) {
482 dom->params = talloc_strdup(dom, compat_params);
483 IDMAP_CHECK_ALLOC(dom->params);
484 } else {
485 dom->params = NULL;
486 }
487
488 /* Finally instance a backend copy for this domain */
489 ret = dom->methods->init(dom);
490 if ( ! NT_STATUS_IS_OK(ret)) {
491 DEBUG(0, ("ERROR: Initialization failed for backend "
492 "%s (domain %s), deferred!\n",
493 parm_backend, dom->name));
494 }
495 idmap_domains = talloc_realloc(idmap_ctx, idmap_domains,
496 struct idmap_domain *, i+1);
497 if ( ! idmap_domains) {
498 DEBUG(0, ("Out of memory!\n"));
499 ret = NT_STATUS_NO_MEMORY;
500 goto done;
501 }
502 idmap_domains[num_domains] = dom;
503
504 /* save default domain position for future uses */
505 if (dom->default_domain) {
506 def_dom_num = num_domains;
507 }
508
509 /* Bump counter to next available slot */
510
511 num_domains++;
512
513 DEBUG(10, ("Domain %s - Backend %s - %sdefault - %sreadonly\n",
514 dom->name, parm_backend,
515 dom->default_domain?"":"not ",
516 dom->readonly?"":"not "));
517
518 talloc_free(config_option);
519 }
520
521 /* on DCs we need to add idmap_tdb as the default backend if compat is
522 * defined (when the old implicit configuration is used)
523 * This is not done in the previous loop a on member server we exclude
524 * the local domain. But on a DC the local domain is the only domain
525 * available therefore we are left with no default domain */
526 if (((lp_server_role() == ROLE_DOMAIN_PDC) ||
527 (lp_server_role() == ROLE_DOMAIN_BDC)) &&
528 ((num_domains == 0) && (compat == 1))) {
529
530 dom = TALLOC_ZERO_P(idmap_ctx, struct idmap_domain);
531 IDMAP_CHECK_ALLOC(dom);
532
533 dom->name = talloc_strdup(dom, "__default__");
534 IDMAP_CHECK_ALLOC(dom->name);
535
536 dom->default_domain = True;
537 dom->readonly = False;
538
539 /* get the backend methods for this domain */
540 dom->methods = get_methods(compat_backend);
541 if ( ! dom->methods) {
542 ret = smb_probe_module("idmap", compat_backend);
543 if (NT_STATUS_IS_OK(ret)) {
544 dom->methods = get_methods(compat_backend);
545 }
546 }
547 if ( ! dom->methods) {
548 DEBUG(0, ("ERROR: Could not get methods for "
549 "backend %s\n", compat_backend));
550 ret = NT_STATUS_UNSUCCESSFUL;
551 goto done;
552 }
553
554 /* now that we have methods,
555 * set the destructor for this domain */
556 talloc_set_destructor(dom, close_domain_destructor);
557
558 dom->params = talloc_strdup(dom, compat_params);
559 IDMAP_CHECK_ALLOC(dom->params);
560
561 /* Finally instance a backend copy for this domain */
562 ret = dom->methods->init(dom);
563 if ( ! NT_STATUS_IS_OK(ret)) {
564 DEBUG(0, ("ERROR: Initialization failed for backend "
565 "%s (domain %s), deferred!\n",
566 compat_backend, dom->name));
567 }
568 idmap_domains = talloc_realloc(idmap_ctx, idmap_domains,
569 struct idmap_domain *, 2);
570 if ( ! idmap_domains) {
571 DEBUG(0, ("Out of memory!\n"));
572 ret = NT_STATUS_NO_MEMORY;
573 goto done;
574 }
575 idmap_domains[num_domains] = dom;
576
577 def_dom_num = num_domains;
578
579 /* Bump counter to next available slot */
580
581 num_domains++;
582
583 DEBUG(10, ("Domain %s - Backend %s - %sdefault - %sreadonly\n",
584 dom->name, compat_backend,
585 dom->default_domain?"":"not ",
586 dom->readonly?"":"not "));
587 }
588
589 /* automatically add idmap_nss backend if needed */
590 if ((lp_server_role() == ROLE_DOMAIN_MEMBER) &&
591 ( ! pri_dom_is_in_list) &&
592 lp_winbind_trusted_domains_only()) {
593
594 dom = TALLOC_ZERO_P(idmap_ctx, struct idmap_domain);
595 IDMAP_CHECK_ALLOC(dom);
596
597 dom->name = talloc_strdup(dom, lp_workgroup());
598 IDMAP_CHECK_ALLOC(dom->name);
599
600 dom->default_domain = False;
601 dom->readonly = True;
602
603 /* get the backend methods for nss */
604 dom->methods = get_methods("nss");
605
606 /* (the nss module is always statically linked) */
607 if ( ! dom->methods) {
608 DEBUG(0, ("ERROR: No methods for idmap_nss ?!\n"));
609 ret = NT_STATUS_UNSUCCESSFUL;
610 goto done;
611 }
612
613 /* now that we have methods,
614 * set the destructor for this domain */
615 talloc_set_destructor(dom, close_domain_destructor);
616
617 if (compat_params) {
618 dom->params = talloc_strdup(dom, compat_params);
619 IDMAP_CHECK_ALLOC(dom->params);
620 } else {
621 dom->params = NULL;
622 }
623
624 /* Finally instance a backend copy for this domain */
625 ret = dom->methods->init(dom);
626 if ( ! NT_STATUS_IS_OK(ret)) {
627 DEBUG(0, ("ERROR: Init. failed for idmap_nss ?!\n"));
628 ret = NT_STATUS_UNSUCCESSFUL;
629 goto done;
630 }
631
632 idmap_domains = talloc_realloc(idmap_ctx,
633 idmap_domains,
634 struct idmap_domain *,
635 num_domains+1);
636 if ( ! idmap_domains) {
637 DEBUG(0, ("Out of memory!\n"));
638 ret = NT_STATUS_NO_MEMORY;
639 goto done;
640 }
641 idmap_domains[num_domains] = dom;
642
643 DEBUG(10, ("Domain %s - Backend nss - not default - readonly\n",
644 dom->name ));
645
646 num_domains++;
647 }
648
649 /**** automatically add idmap_passdb backend ****/
650 dom = TALLOC_ZERO_P(idmap_ctx, struct idmap_domain);
651 IDMAP_CHECK_ALLOC(dom);
652
653 dom->name = talloc_strdup(dom, get_global_sam_name());
654 IDMAP_CHECK_ALLOC(dom->name);
655
656 dom->default_domain = False;
657 dom->readonly = True;
658
659 /* get the backend methods for passdb */
660 dom->methods = get_methods("passdb");
661
662 /* (the passdb module is always statically linked) */
663 if ( ! dom->methods) {
664 DEBUG(0, ("ERROR: No methods for idmap_passdb ?!\n"));
665 ret = NT_STATUS_UNSUCCESSFUL;
666 goto done;
667 }
668
669 /* now that we have methods, set the destructor for this domain */
670 talloc_set_destructor(dom, close_domain_destructor);
671
672 if (compat_params) {
673 dom->params = talloc_strdup(dom, compat_params);
674 IDMAP_CHECK_ALLOC(dom->params);
675 } else {
676 dom->params = NULL;
677 }
678
679 /* Finally instance a backend copy for this domain */
680 ret = dom->methods->init(dom);
681 if ( ! NT_STATUS_IS_OK(ret)) {
682 DEBUG(0, ("ERROR: Init. failed for idmap_passdb ?!\n"));
683 ret = NT_STATUS_UNSUCCESSFUL;
684 goto done;
685 }
686
687 idmap_domains = talloc_realloc(idmap_ctx,
688 idmap_domains,
689 struct idmap_domain *,
690 num_domains+1);
691 if ( ! idmap_domains) {
692 DEBUG(0, ("Out of memory!\n"));
693 ret = NT_STATUS_NO_MEMORY;
694 goto done;
695 }
696 idmap_domains[num_domains] = dom;
697
698 /* needed to handle special BUILTIN and wellknown SIDs cases */
699 pdb_dom_num = num_domains;
700
701 DEBUG(10, ("Domain %s - Backend passdb - not default - readonly\n",
702 dom->name));
703
704 num_domains++;
705 /**** finished adding idmap_passdb backend ****/
706
707 /* sort domains so that the default is the last one */
708 /* don't sort if no default domain defined */
709 if (def_dom_num != -1 && def_dom_num != num_domains-1) {
710 /* default is not last, move it */
711 struct idmap_domain *tmp;
712
713 if (pdb_dom_num > def_dom_num) {
714 pdb_dom_num --;
715
716 } else if (pdb_dom_num == def_dom_num) { /* ?? */
717 pdb_dom_num = num_domains - 1;
718 }
719
720 tmp = idmap_domains[def_dom_num];
721
722 for (i = def_dom_num; i < num_domains-1; i++) {
723 idmap_domains[i] = idmap_domains[i+1];
724 }
725 idmap_domains[i] = tmp;
726 def_dom_num = i;
727 }
728
729
730 /* Initialize alloc module */
731
732 DEBUG(3, ("Initializing idmap alloc module\n"));
733
734 alloc_backend = NULL;
735 if (compat) {
736 alloc_backend = talloc_strdup(idmap_ctx, compat_backend);
737 } else {
738 char *ab = lp_idmap_alloc_backend();
739
740 if (ab && (ab[0] != '\0')) {
741 alloc_backend = talloc_strdup(idmap_ctx,
742 lp_idmap_alloc_backend());
743 }
744 }
745
746 if ( alloc_backend ) {
747
748 idmap_alloc_ctx = TALLOC_ZERO_P(idmap_ctx,
749 struct idmap_alloc_context);
750 IDMAP_CHECK_ALLOC(idmap_alloc_ctx);
751
752 idmap_alloc_ctx->methods = get_alloc_methods(alloc_backend);
753 if ( ! idmap_alloc_ctx->methods) {
754 ret = smb_probe_module("idmap", alloc_backend);
755 if (NT_STATUS_IS_OK(ret)) {
756 idmap_alloc_ctx->methods =
757 get_alloc_methods(alloc_backend);
758 }
759 }
760 if (idmap_alloc_ctx->methods) {
761
762 if (compat_params) {
763 idmap_alloc_ctx->params =
764 talloc_strdup(idmap_alloc_ctx,
765 compat_params);
766 IDMAP_CHECK_ALLOC(idmap_alloc_ctx->params);
767 } else {
768 idmap_alloc_ctx->params = NULL;
769 }
770
771 ret = idmap_alloc_ctx->methods->init(idmap_alloc_ctx->params);
772 if ( ! NT_STATUS_IS_OK(ret)) {
773 DEBUG(0, ("ERROR: Initialization failed for "
774 "alloc backend %s, deferred!\n",
775 alloc_backend));
776 } else {
777 idmap_alloc_ctx->initialized = True;
778 }
779 } else {
780 DEBUG(2, ("idmap_init: Unable to get methods for "
781 "alloc backend %s\n",
782 alloc_backend));
783 /* certain compat backends are just readonly */
784 if ( compat ) {
785 TALLOC_FREE(idmap_alloc_ctx);
786 ret = NT_STATUS_OK;
787 } else {
788 ret = NT_STATUS_UNSUCCESSFUL;
789 }
790 }
791 }
792
793 /* cleanup temporary strings */
794 TALLOC_FREE( compat_backend );
795
796 idmap_init_status = NT_STATUS_OK;
797
798 return ret;
799
800done:
801 DEBUG(0, ("Aborting IDMAP Initialization ...\n"));
802 idmap_close();
803
804 return ret;
805}
806
807static NTSTATUS idmap_alloc_init(void)
808{
809 NTSTATUS ret;
810
811 if (! NT_STATUS_IS_OK(ret = idmap_init())) {
812 return ret;
813 }
814
815 if ( ! idmap_alloc_ctx) {
816 return NT_STATUS_NOT_SUPPORTED;
817 }
818
819 if ( ! idmap_alloc_ctx->initialized) {
820 ret = idmap_alloc_ctx->methods->init(idmap_alloc_ctx->params);
821 if ( ! NT_STATUS_IS_OK(ret)) {
822 DEBUG(0, ("ERROR: Initialization failed for alloc "
823 "backend, deferred!\n"));
824 return ret;
825 } else {
826 idmap_alloc_ctx->initialized = True;
827 }
828 }
829
830 return NT_STATUS_OK;
831}
832
833/**************************************************************************
834 idmap allocator interface functions
835**************************************************************************/
836
837NTSTATUS idmap_allocate_uid(struct unixid *id)
838{
839 NTSTATUS ret;
840
841 if (! NT_STATUS_IS_OK(ret = idmap_alloc_init())) {
842 return ret;
843 }
844
845 id->type = ID_TYPE_UID;
846 return idmap_alloc_ctx->methods->allocate_id(id);
847}
848
849NTSTATUS idmap_allocate_gid(struct unixid *id)
850{
851 NTSTATUS ret;
852
853 if (! NT_STATUS_IS_OK(ret = idmap_alloc_init())) {
854 return ret;
855 }
856
857 id->type = ID_TYPE_GID;
858 return idmap_alloc_ctx->methods->allocate_id(id);
859}
860
861NTSTATUS idmap_set_uid_hwm(struct unixid *id)
862{
863 NTSTATUS ret;
864
865 if (! NT_STATUS_IS_OK(ret = idmap_alloc_init())) {
866 return ret;
867 }
868
869 id->type = ID_TYPE_UID;
870 return idmap_alloc_ctx->methods->set_id_hwm(id);
871}
872
873NTSTATUS idmap_set_gid_hwm(struct unixid *id)
874{
875 NTSTATUS ret;
876
877 if (! NT_STATUS_IS_OK(ret = idmap_alloc_init())) {
878 return ret;
879 }
880
881 id->type = ID_TYPE_GID;
882 return idmap_alloc_ctx->methods->set_id_hwm(id);
883}
884
885/******************************************************************************
886 Lookup an idmap_domain give a full user or group SID
887 ******************************************************************************/
888
889static struct idmap_domain* find_idmap_domain_from_sid( DOM_SID *account_sid )
890{
891 DOM_SID domain_sid;
892 uint32_t rid;
893 struct winbindd_domain *domain = NULL;
894 int i;
895
896 /* 1. Handle BUILTIN or Special SIDs and prevent them from
897 falling into the default domain space (if we have a
898 configured passdb backend. */
899
900 if ( (pdb_dom_num != -1) &&
901 (sid_check_is_in_builtin(account_sid) ||
902 sid_check_is_in_wellknown_domain(account_sid) ||
903 sid_check_is_in_unix_groups(account_sid) ||
904 sid_check_is_in_unix_users(account_sid)) )
905 {
906 return idmap_domains[pdb_dom_num];
907 }
908
909 /* 2. Lookup the winbindd_domain from the account_sid */
910
911 sid_copy( &domain_sid, account_sid );
912 sid_split_rid( &domain_sid, &rid );
913 domain = find_domain_from_sid_noinit( &domain_sid );
914
915 for (i = 0; domain && i < num_domains; i++) {
916 if ( strequal( idmap_domains[i]->name, domain->name ) ) {
917 return idmap_domains[i];
918 }
919 }
920
921 /* 3. Fall back to the default domain */
922
923 if ( def_dom_num != -1 ) {
924 return idmap_domains[def_dom_num];
925 }
926
927 return NULL;
928}
929
930/******************************************************************************
931 Lookup an index given an idmap_domain pointer
932 ******************************************************************************/
933
934static uint32_t find_idmap_domain_index( struct idmap_domain *id_domain)
935{
936 int i;
937
938 for (i = 0; i < num_domains; i++) {
939 if ( idmap_domains[i] == id_domain )
940 return i;
941 }
942
943 return -1;
944}
945
946
947/*********************************************************
948 Check if creating a mapping is permitted for the domain
949*********************************************************/
950
951static NTSTATUS idmap_can_map(const struct id_map *map,
952 struct idmap_domain **ret_dom)
953{
954 struct idmap_domain *dom;
955
956 /* Check we do not create mappings for our own local domain,
957 * or BUILTIN or special SIDs */
958 if ((sid_compare_domain(map->sid, get_global_sam_sid()) == 0) ||
959 sid_check_is_in_builtin(map->sid) ||
960 sid_check_is_in_wellknown_domain(map->sid) ||
961 sid_check_is_in_unix_users(map->sid) ||
962 sid_check_is_in_unix_groups(map->sid) )
963 {
964 DEBUG(10, ("We are not supposed to create mappings for our own "
965 "domains (local, builtin, specials)\n"));
966 return NT_STATUS_UNSUCCESSFUL;
967 }
968
969 /* Special check for trusted domain only = Yes */
970 if (lp_winbind_trusted_domains_only()) {
971 struct winbindd_domain *wdom = find_our_domain();
972 if (wdom && (sid_compare_domain(map->sid, &wdom->sid) == 0)) {
973 DEBUG(10, ("We are not supposed to create mappings for "
974 "our primary domain when <trusted domain "
975 "only> is True\n"));
976 DEBUGADD(10, ("Leave [%s] unmapped\n",
977 sid_string_dbg(map->sid)));
978 return NT_STATUS_UNSUCCESSFUL;
979 }
980 }
981
982 if ( (dom = find_idmap_domain_from_sid( map->sid )) == NULL ) {
983 /* huh, couldn't find a suitable domain,
984 * let's just leave it unmapped */
985 DEBUG(10, ("Could not find idmap backend for SID %s\n",
986 sid_string_dbg(map->sid)));
987 return NT_STATUS_NO_SUCH_DOMAIN;
988 }
989
990 if (dom->readonly) {
991 /* ouch the domain is read only,
992 * let's just leave it unmapped */
993 DEBUG(10, ("idmap backend for SID %s is READONLY!\n",
994 sid_string_dbg(map->sid)));
995 return NT_STATUS_UNSUCCESSFUL;
996 }
997
998 *ret_dom = dom;
999 return NT_STATUS_OK;
1000}
1001
1002static NTSTATUS idmap_new_mapping(TALLOC_CTX *ctx, struct id_map *map)
1003{
1004 NTSTATUS ret;
1005 struct idmap_domain *dom;
1006
1007 /* If we are offline we cannot lookup SIDs, deny mapping */
1008 if (idmap_is_offline()) {
1009 return NT_STATUS_FILE_IS_OFFLINE;
1010 }
1011
1012 ret = idmap_can_map(map, &dom);
1013 if ( ! NT_STATUS_IS_OK(ret)) {
1014 return NT_STATUS_NONE_MAPPED;
1015 }
1016
1017 /* check if this is a valid SID and then map it */
1018 switch (map->xid.type) {
1019 case ID_TYPE_UID:
1020 ret = idmap_allocate_uid(&map->xid);
1021 if ( ! NT_STATUS_IS_OK(ret)) {
1022 /* can't allocate id, let's just leave it unmapped */
1023 DEBUG(2, ("uid allocation failed! "
1024 "Can't create mapping\n"));
1025 return NT_STATUS_NONE_MAPPED;
1026 }
1027 break;
1028 case ID_TYPE_GID:
1029 ret = idmap_allocate_gid(&map->xid);
1030 if ( ! NT_STATUS_IS_OK(ret)) {
1031 /* can't allocate id, let's just leave it unmapped */
1032 DEBUG(2, ("gid allocation failed! "
1033 "Can't create mapping\n"));
1034 return NT_STATUS_NONE_MAPPED;
1035 }
1036 break;
1037 default:
1038 /* invalid sid, let's just leave it unmapped */
1039 DEBUG(3,("idmap_new_mapping: Refusing to create a "
1040 "mapping for an unspecified ID type.\n"));
1041 return NT_STATUS_NONE_MAPPED;
1042 }
1043
1044 /* ok, got a new id, let's set a mapping */
1045 map->status = ID_MAPPED;
1046
1047 DEBUG(10, ("Setting mapping: %s <-> %s %lu\n",
1048 sid_string_dbg(map->sid),
1049 (map->xid.type == ID_TYPE_UID) ? "UID" : "GID",
1050 (unsigned long)map->xid.id));
1051 ret = dom->methods->set_mapping(dom, map);
1052
1053 if ( ! NT_STATUS_IS_OK(ret)) {
1054 /* something wrong here :-( */
1055 DEBUG(2, ("Failed to commit mapping\n!"));
1056
1057 /* TODO: would it make sense to have an "unalloc_id function?" */
1058
1059 return NT_STATUS_NONE_MAPPED;
1060 }
1061
1062 return NT_STATUS_OK;
1063}
1064
1065static NTSTATUS idmap_backends_set_mapping(const struct id_map *map)
1066{
1067 struct idmap_domain *dom;
1068 NTSTATUS ret;
1069
1070 DEBUG(10, ("Setting mapping %s <-> %s %lu\n",
1071 sid_string_dbg(map->sid),
1072 (map->xid.type == ID_TYPE_UID) ? "UID" : "GID",
1073 (unsigned long)map->xid.id));
1074
1075 ret = idmap_can_map(map, &dom);
1076 if ( ! NT_STATUS_IS_OK(ret)) {
1077 return ret;
1078 }
1079
1080 DEBUG(10,("set_mapping for domain %s\n", dom->name ));
1081
1082 return dom->methods->set_mapping(dom, map);
1083}
1084
1085static NTSTATUS idmap_backends_unixids_to_sids(struct id_map **ids)
1086{
1087 struct idmap_domain *dom;
1088 struct id_map **unmapped;
1089 struct id_map **_ids;
1090 TALLOC_CTX *ctx;
1091 NTSTATUS ret;
1092 int i, u, n;
1093
1094 if (!ids || !*ids) {
1095 DEBUG(1, ("Invalid list of maps\n"));
1096 return NT_STATUS_INVALID_PARAMETER;
1097 }
1098
1099 ctx = talloc_named_const(NULL, 0, "idmap_backends_unixids_to_sids ctx");
1100 if ( ! ctx) {
1101 DEBUG(0, ("Out of memory!\n"));
1102 return NT_STATUS_NO_MEMORY;
1103 }
1104
1105 DEBUG(10, ("Query backends to map ids->sids\n"));
1106
1107 /* start from the default (the last one) and then if there are still
1108 * unmapped entries cycle through the others */
1109
1110 _ids = ids;
1111
1112 unmapped = NULL;
1113 for (n = num_domains-1; n >= 0; n--) { /* cycle backwards */
1114
1115 dom = idmap_domains[n];
1116
1117 DEBUG(10, ("Query sids from domain %s\n", dom->name));
1118
1119 ret = dom->methods->unixids_to_sids(dom, _ids);
1120 IDMAP_REPORT_RET(ret);
1121
1122 unmapped = NULL;
1123
1124 for (i = 0, u = 0; _ids[i]; i++) {
1125 if (_ids[i]->status != ID_MAPPED) {
1126 unmapped = talloc_realloc(ctx, unmapped,
1127 struct id_map *, u + 2);
1128 IDMAP_CHECK_ALLOC(unmapped);
1129 unmapped[u] = _ids[i];
1130 u++;
1131 }
1132 }
1133 if (unmapped) {
1134 /* terminate the unmapped list */
1135 unmapped[u] = NULL;
1136 } else { /* no more entries, get out */
1137 break;
1138 }
1139
1140 _ids = unmapped;
1141
1142 }
1143
1144 if (unmapped) {
1145 /* there are still unmapped ids,
1146 * map them to the unix users/groups domains */
1147 /* except for expired entries,
1148 * these will be returned as valid (offline mode) */
1149 for (i = 0; unmapped[i]; i++) {
1150 if (unmapped[i]->status == ID_EXPIRED) continue;
1151 switch (unmapped[i]->xid.type) {
1152 case ID_TYPE_UID:
1153 uid_to_unix_users_sid(
1154 (uid_t)unmapped[i]->xid.id,
1155 unmapped[i]->sid);
1156 unmapped[i]->status = ID_MAPPED;
1157 break;
1158 case ID_TYPE_GID:
1159 gid_to_unix_groups_sid(
1160 (gid_t)unmapped[i]->xid.id,
1161 unmapped[i]->sid);
1162 unmapped[i]->status = ID_MAPPED;
1163 break;
1164 default: /* what?! */
1165 unmapped[i]->status = ID_UNKNOWN;
1166 break;
1167 }
1168 }
1169 }
1170
1171 ret = NT_STATUS_OK;
1172
1173done:
1174 talloc_free(ctx);
1175 return ret;
1176}
1177
1178static NTSTATUS idmap_backends_sids_to_unixids(struct id_map **ids)
1179{
1180 struct id_map ***dom_ids;
1181 struct idmap_domain *dom;
1182 TALLOC_CTX *ctx;
1183 NTSTATUS ret;
1184 int i, *counters;
1185
1186 if ( (ctx = talloc_named_const(NULL, 0, "be_sids_to_ids")) == NULL ) {
1187 DEBUG(1, ("failed to allocate talloc context, OOM?\n"));
1188 return NT_STATUS_NO_MEMORY;
1189 }
1190
1191 DEBUG(10, ("Query backends to map sids->ids\n"));
1192
1193 /* split list per domain */
1194 if (num_domains == 0) {
1195 DEBUG(1, ("No domains available?\n"));
1196 return NT_STATUS_UNSUCCESSFUL;
1197 }
1198
1199 dom_ids = TALLOC_ZERO_ARRAY(ctx, struct id_map **, num_domains);
1200 IDMAP_CHECK_ALLOC(dom_ids);
1201 counters = TALLOC_ZERO_ARRAY(ctx, int, num_domains);
1202 IDMAP_CHECK_ALLOC(counters);
1203
1204 /* partition the requests by domain */
1205
1206 for (i = 0; ids[i]; i++) {
1207 uint32_t idx;
1208
1209 if ((dom = find_idmap_domain_from_sid(ids[i]->sid)) == NULL) {
1210 /* no available idmap_domain. Move on */
1211 continue;
1212 }
1213
1214 DEBUG(10,("SID %s is being handled by %s\n",
1215 sid_string_dbg(ids[i]->sid),
1216 dom ? dom->name : "none" ));
1217
1218 idx = find_idmap_domain_index( dom );
1219 SMB_ASSERT( idx != -1 );
1220
1221 dom_ids[idx] = talloc_realloc(ctx, dom_ids[idx],
1222 struct id_map *,
1223 counters[idx] + 2);
1224 IDMAP_CHECK_ALLOC(dom_ids[idx]);
1225
1226 dom_ids[idx][counters[idx]] = ids[i];
1227 counters[idx]++;
1228 dom_ids[idx][counters[idx]] = NULL;
1229 }
1230
1231 /* All the ids have been dispatched in the right queues.
1232 Let's cycle through the filled ones */
1233
1234 for (i = 0; i < num_domains; i++) {
1235 if (dom_ids[i]) {
1236 dom = idmap_domains[i];
1237 DEBUG(10, ("Query ids from domain %s\n", dom->name));
1238 ret = dom->methods->sids_to_unixids(dom, dom_ids[i]);
1239 IDMAP_REPORT_RET(ret);
1240 }
1241 }
1242
1243 /* ok all the backends have been contacted at this point */
1244 /* let's see if we have any unmapped SID left and act accordingly */
1245
1246 for (i = 0; ids[i]; i++) {
1247 /* NOTE: this will NOT touch ID_EXPIRED entries that the backend
1248 * was not able to confirm/deny (offline mode) */
1249 if (ids[i]->status == ID_UNKNOWN ||
1250 ids[i]->status == ID_UNMAPPED) {
1251 /* ok this is an unmapped one, see if we can map it */
1252 ret = idmap_new_mapping(ctx, ids[i]);
1253 if (NT_STATUS_IS_OK(ret)) {
1254 /* successfully mapped */
1255 ids[i]->status = ID_MAPPED;
1256 } else
1257 if (NT_STATUS_EQUAL(ret, NT_STATUS_NONE_MAPPED)) {
1258 /* could not map it */
1259 ids[i]->status = ID_UNMAPPED;
1260 } else {
1261 /* Something very bad happened down there
1262 * OR we are offline */
1263 ids[i]->status = ID_UNKNOWN;
1264 }
1265 }
1266 }
1267
1268 ret = NT_STATUS_OK;
1269
1270done:
1271 talloc_free(ctx);
1272 return ret;
1273}
1274
1275/**************************************************************************
1276 idmap interface functions
1277**************************************************************************/
1278
1279NTSTATUS idmap_unixids_to_sids(struct id_map **ids)
1280{
1281 TALLOC_CTX *ctx;
1282 NTSTATUS ret;
1283 struct id_map **bids;
1284 int i, bi;
1285 int bn = 0;
1286 struct winbindd_domain *our_domain = find_our_domain();
1287
1288 if (! NT_STATUS_IS_OK(ret = idmap_init())) {
1289 return ret;
1290 }
1291
1292 if (!ids || !*ids) {
1293 DEBUG(1, ("Invalid list of maps\n"));
1294 return NT_STATUS_INVALID_PARAMETER;
1295 }
1296
1297 ctx = talloc_named_const(NULL, 0, "idmap_unixids_to_sids ctx");
1298 if ( ! ctx) {
1299 DEBUG(1, ("failed to allocate talloc context, OOM?\n"));
1300 return NT_STATUS_NO_MEMORY;
1301 }
1302
1303 /* no ids to be asked to the backends by default */
1304 bids = NULL;
1305 bi = 0;
1306
1307 for (i = 0; ids[i]; i++) {
1308
1309 if ( ! ids[i]->sid) {
1310 DEBUG(1, ("invalid null SID in id_map array"));
1311 talloc_free(ctx);
1312 return NT_STATUS_INVALID_PARAMETER;
1313 }
1314
1315 ret = idmap_cache_map_id(idmap_cache, ids[i]);
1316
1317 if ( ! NT_STATUS_IS_OK(ret)) {
1318
1319 if ( ! bids) {
1320 /* alloc space for ids to be resolved by
1321 * backends (realloc ten by ten) */
1322 bids = TALLOC_ARRAY(ctx, struct id_map *, 10);
1323 if ( ! bids) {
1324 DEBUG(1, ("Out of memory!\n"));
1325 talloc_free(ctx);
1326 return NT_STATUS_NO_MEMORY;
1327 }
1328 bn = 10;
1329 }
1330
1331 /* add this id to the ones to be retrieved
1332 * from the backends */
1333 bids[bi] = ids[i];
1334 bi++;
1335
1336 /* check if we need to allocate new space
1337 * on the rids array */
1338 if (bi == bn) {
1339 bn += 10;
1340 bids = talloc_realloc(ctx, bids,
1341 struct id_map *, bn);
1342 if ( ! bids) {
1343 DEBUG(1, ("Out of memory!\n"));
1344 talloc_free(ctx);
1345 return NT_STATUS_NO_MEMORY;
1346 }
1347 }
1348
1349 /* make sure the last element is NULL */
1350 bids[bi] = NULL;
1351 }
1352 }
1353
1354 /* let's see if there is any id mapping to be retieved
1355 * from the backends */
1356 if (bids) {
1357 bool online;
1358
1359 /* Only do query if we are online */
1360 online = !IS_DOMAIN_OFFLINE(our_domain);
1361 if (online) {
1362 ret = idmap_backends_unixids_to_sids(bids);
1363 IDMAP_CHECK_RET(ret);
1364 }
1365
1366 /* update the cache */
1367 for (i = 0; i < bi; i++) {
1368 if (bids[i]->status == ID_MAPPED) {
1369 ret = idmap_cache_set(idmap_cache, bids[i]);
1370 } else if (bids[i]->status == ID_EXPIRED) {
1371 /* the cache returned an expired entry and the
1372 * backend was not able to clear the situation
1373 * (offline). This handles a previous
1374 * NT_STATUS_SYNCHRONIZATION_REQUIRED
1375 * for disconnected mode, */
1376 bids[i]->status = ID_MAPPED;
1377 } else if (bids[i]->status == ID_UNKNOWN) {
1378 /* something bad here. We were not able to
1379 * handle this for some reason, mark it as
1380 * unmapped and hope next time things will
1381 * settle down. */
1382 bids[i]->status = ID_UNMAPPED;
1383 } else if (online) { /* unmapped */
1384 ret = idmap_cache_set_negative_id(idmap_cache,
1385 bids[i]);
1386 }
1387 IDMAP_CHECK_RET(ret);
1388 }
1389 }
1390
1391 ret = NT_STATUS_OK;
1392done:
1393 talloc_free(ctx);
1394 return ret;
1395}
1396
1397NTSTATUS idmap_sids_to_unixids(struct id_map **ids)
1398{
1399 TALLOC_CTX *ctx;
1400 NTSTATUS ret;
1401 struct id_map **bids;
1402 int i, bi;
1403 int bn = 0;
1404 struct winbindd_domain *our_domain = find_our_domain();
1405
1406 if (! NT_STATUS_IS_OK(ret = idmap_init())) {
1407 return ret;
1408 }
1409
1410 if (!ids || !*ids) {
1411 DEBUG(1, ("Invalid list of maps\n"));
1412 return NT_STATUS_INVALID_PARAMETER;
1413 }
1414
1415 ctx = talloc_named_const(NULL, 0, "idmap_sids_to_unixids ctx");
1416 if ( ! ctx) {
1417 DEBUG(1, ("failed to allocate talloc context, OOM?\n"));
1418 return NT_STATUS_NO_MEMORY;
1419 }
1420
1421 /* no ids to be asked to the backends by default */
1422 bids = NULL;
1423 bi = 0;
1424
1425 for (i = 0; ids[i]; i++) {
1426
1427 if ( ! ids[i]->sid) {
1428 DEBUG(1, ("invalid null SID in id_map array\n"));
1429 talloc_free(ctx);
1430 return NT_STATUS_INVALID_PARAMETER;
1431 }
1432
1433 ret = idmap_cache_map_sid(idmap_cache, ids[i]);
1434
1435 if ( ! NT_STATUS_IS_OK(ret)) {
1436
1437 if ( ! bids) {
1438 /* alloc space for ids to be resolved
1439 by backends (realloc ten by ten) */
1440 bids = TALLOC_ARRAY(ctx, struct id_map *, 10);
1441 if ( ! bids) {
1442 DEBUG(1, ("Out of memory!\n"));
1443 talloc_free(ctx);
1444 return NT_STATUS_NO_MEMORY;
1445 }
1446 bn = 10;
1447 }
1448
1449 /* add this id to the ones to be retrieved
1450 * from the backends */
1451 bids[bi] = ids[i];
1452 bi++;
1453
1454 /* check if we need to allocate new space
1455 * on the ids array */
1456 if (bi == bn) {
1457 bn += 10;
1458 bids = talloc_realloc(ctx, bids,
1459 struct id_map *, bn);
1460 if ( ! bids) {
1461 DEBUG(1, ("Out of memory!\n"));
1462 talloc_free(ctx);
1463 return NT_STATUS_NO_MEMORY;
1464 }
1465 }
1466
1467 /* make sure the last element is NULL */
1468 bids[bi] = NULL;
1469 }
1470 }
1471
1472 /* let's see if there is any id mapping to be retieved
1473 * from the backends */
1474 if (bids) {
1475 bool online;
1476
1477 /* Only do query if we are online */
1478 online = !IS_DOMAIN_OFFLINE(our_domain);
1479 if (online) {
1480 ret = idmap_backends_sids_to_unixids(bids);
1481 IDMAP_CHECK_RET(ret);
1482 }
1483
1484 /* update the cache */
1485 for (i = 0; bids[i]; i++) {
1486 if (bids[i]->status == ID_MAPPED) {
1487 ret = idmap_cache_set(idmap_cache, bids[i]);
1488 } else if (bids[i]->status == ID_EXPIRED) {
1489 /* the cache returned an expired entry and the
1490 * backend was not able to clear the situation
1491 * (offline). This handles a previous
1492 * NT_STATUS_SYNCHRONIZATION_REQUIRED
1493 * for disconnected mode, */
1494 bids[i]->status = ID_MAPPED;
1495 } else if (bids[i]->status == ID_UNKNOWN) {
1496 /* something bad here. We were not able to
1497 * handle this for some reason, mark it as
1498 * unmapped and hope next time things will
1499 * settle down. */
1500 bids[i]->status = ID_UNMAPPED;
1501 } else if (online) { /* unmapped */
1502 ret = idmap_cache_set_negative_sid(idmap_cache,
1503 bids[i]);
1504 }
1505 IDMAP_CHECK_RET(ret);
1506 }
1507 }
1508
1509 ret = NT_STATUS_OK;
1510done:
1511 talloc_free(ctx);
1512 return ret;
1513}
1514
1515NTSTATUS idmap_set_mapping(const struct id_map *id)
1516{
1517 TALLOC_CTX *ctx;
1518 NTSTATUS ret;
1519
1520 if (! NT_STATUS_IS_OK(ret = idmap_init())) {
1521 return ret;
1522 }
1523
1524 /* sanity checks */
1525 if ((id->sid == NULL) || (id->status != ID_MAPPED)) {
1526 DEBUG(1, ("NULL SID or unmapped entry\n"));
1527 return NT_STATUS_INVALID_PARAMETER;
1528 }
1529
1530 /* TODO: check uid/gid range ? */
1531
1532 ctx = talloc_named_const(NULL, 0, "idmap_set_mapping ctx");
1533 if ( ! ctx) {
1534 DEBUG(1, ("failed to allocate talloc context, OOM?\n"));
1535 return NT_STATUS_NO_MEMORY;
1536 }
1537
1538 /* set the new mapping */
1539 ret = idmap_backends_set_mapping(id);
1540 IDMAP_CHECK_RET(ret);
1541
1542 /* set the mapping in the cache */
1543 ret = idmap_cache_set(idmap_cache, id);
1544 IDMAP_CHECK_RET(ret);
1545
1546done:
1547 talloc_free(ctx);
1548 return ret;
1549}
1550
1551char *idmap_fetch_secret(const char *backend, bool alloc,
1552 const char *domain, const char *identity)
1553{
1554 char *tmp, *ret;
1555 int r;
1556
1557 if (alloc) {
1558 r = asprintf(&tmp, "IDMAP_ALLOC_%s", backend);
1559 } else {
1560 r = asprintf(&tmp, "IDMAP_%s_%s", backend, domain);
1561 }
1562
1563 if (r < 0)
1564 return NULL;
1565
1566 strupper_m(tmp); /* make sure the key is case insensitive */
1567 ret = secrets_fetch_generic(tmp, identity);
1568
1569 SAFE_FREE(tmp);
1570
1571 return ret;
1572}
1573
Note: See TracBrowser for help on using the repository browser.