s3:winbindd: make sure we only call static_init_idmap once
[amitay/samba.git] / source3 / winbindd / idmap.c
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
29 static_decl_idmap;
30
31 static void idmap_init(void)
32 {
33         static bool initialized;
34
35         if (initialized) {
36                 return;
37         }
38
39         DEBUG(10, ("idmap_init(): calling static_init_idmap\n"));
40
41         static_init_idmap;
42
43         initialized = true;
44 }
45
46 /**
47  * Pointer to the backend methods. Modules register themselves here via
48  * smb_register_idmap.
49  */
50
51 struct idmap_backend {
52         const char *name;
53         struct idmap_methods *methods;
54         struct idmap_backend *prev, *next;
55 };
56 static struct idmap_backend *backends = NULL;
57
58 /**
59  * Pointer to the alloc backend methods. Modules register themselves here via
60  * smb_register_idmap_alloc.
61  */
62 struct idmap_alloc_backend {
63         const char *name;
64         struct idmap_alloc_methods *methods;
65         struct idmap_alloc_backend *prev, *next;
66 };
67 static struct idmap_alloc_backend *alloc_backends = NULL;
68
69 /**
70  * The idmap alloc context that is configured via "idmap alloc
71  * backend". Defaults to "idmap backend" in case the module (tdb, ldap) also
72  * provides alloc methods.
73  */
74 struct idmap_alloc_context {
75         struct idmap_alloc_methods *methods;
76 };
77 static struct idmap_alloc_context *idmap_alloc_ctx = NULL;
78
79 /**
80  * Default idmap domain configured via "idmap backend".
81  */
82 static struct idmap_domain *default_idmap_domain;
83
84 /**
85  * Passdb idmap domain, not configurable. winbind must always give passdb a
86  * chance to map ids.
87  */
88 static struct idmap_domain *passdb_idmap_domain;
89
90 /**
91  * List of specially configured idmap domains. This list is filled on demand
92  * in the winbind idmap child when the parent winbind figures out via the
93  * special range parameter or via the domain SID that a special "idmap config
94  * domain" configuration is present.
95  */
96 static struct idmap_domain **idmap_domains = NULL;
97 static int num_domains = 0;
98
99 static struct idmap_methods *get_methods(const char *name)
100 {
101         struct idmap_backend *b;
102
103         for (b = backends; b; b = b->next) {
104                 if (strequal(b->name, name)) {
105                         return b->methods;
106                 }
107         }
108
109         return NULL;
110 }
111
112 static struct idmap_alloc_methods *get_alloc_methods(const char *name)
113 {
114         struct idmap_alloc_backend *b;
115
116         for (b = alloc_backends; b; b = b->next) {
117                 if (strequal(b->name, name)) {
118                         return b->methods;
119                 }
120         }
121
122         return NULL;
123 }
124
125 bool idmap_is_offline(void)
126 {
127         return ( lp_winbind_offline_logon() &&
128              get_global_winbindd_state_offline() );
129 }
130
131 bool idmap_is_online(void)
132 {
133         return !idmap_is_offline();
134 }
135
136 /**********************************************************************
137  Allow a module to register itself as a method.
138 **********************************************************************/
139
140 NTSTATUS smb_register_idmap(int version, const char *name,
141                             struct idmap_methods *methods)
142 {
143         struct idmap_backend *entry;
144
145         if ((version != SMB_IDMAP_INTERFACE_VERSION)) {
146                 DEBUG(0, ("Failed to register idmap module.\n"
147                           "The module was compiled against "
148                           "SMB_IDMAP_INTERFACE_VERSION %d,\n"
149                           "current SMB_IDMAP_INTERFACE_VERSION is %d.\n"
150                           "Please recompile against the current version "
151                           "of samba!\n",
152                           version, SMB_IDMAP_INTERFACE_VERSION));
153                 return NT_STATUS_OBJECT_TYPE_MISMATCH;
154         }
155
156         if (!name || !name[0] || !methods) {
157                 DEBUG(0,("Called with NULL pointer or empty name!\n"));
158                 return NT_STATUS_INVALID_PARAMETER;
159         }
160
161         for (entry = backends; entry != NULL; entry = entry->next) {
162                 if (strequal(entry->name, name)) {
163                         DEBUG(0,("Idmap module %s already registered!\n",
164                                  name));
165                         return NT_STATUS_OBJECT_NAME_COLLISION;
166                 }
167         }
168
169         entry = talloc(NULL, struct idmap_backend);
170         if ( ! entry) {
171                 DEBUG(0,("Out of memory!\n"));
172                 TALLOC_FREE(entry);
173                 return NT_STATUS_NO_MEMORY;
174         }
175         entry->name = talloc_strdup(entry, name);
176         if ( ! entry->name) {
177                 DEBUG(0,("Out of memory!\n"));
178                 TALLOC_FREE(entry);
179                 return NT_STATUS_NO_MEMORY;
180         }
181         entry->methods = methods;
182
183         DLIST_ADD(backends, entry);
184         DEBUG(5, ("Successfully added idmap backend '%s'\n", name));
185         return NT_STATUS_OK;
186 }
187
188 /**********************************************************************
189  Allow a module to register itself as an alloc method.
190 **********************************************************************/
191
192 NTSTATUS smb_register_idmap_alloc(int version, const char *name,
193                                   struct idmap_alloc_methods *methods)
194 {
195         struct idmap_alloc_methods *test;
196         struct idmap_alloc_backend *entry;
197
198         if ((version != SMB_IDMAP_INTERFACE_VERSION)) {
199                 DEBUG(0, ("Failed to register idmap alloc module.\n"
200                           "The module was compiled against "
201                           "SMB_IDMAP_INTERFACE_VERSION %d,\n"
202                           "current SMB_IDMAP_INTERFACE_VERSION is %d.\n"
203                           "Please recompile against the current version "
204                           "of samba!\n",
205                           version, SMB_IDMAP_INTERFACE_VERSION));
206                 return NT_STATUS_OBJECT_TYPE_MISMATCH;
207         }
208
209         if (!name || !name[0] || !methods) {
210                 DEBUG(0,("Called with NULL pointer or empty name!\n"));
211                 return NT_STATUS_INVALID_PARAMETER;
212         }
213
214         test = get_alloc_methods(name);
215         if (test) {
216                 DEBUG(0,("idmap_alloc module %s already registered!\n", name));
217                 return NT_STATUS_OBJECT_NAME_COLLISION;
218         }
219
220         entry = talloc(NULL, struct idmap_alloc_backend);
221         if ( ! entry) {
222                 DEBUG(0,("Out of memory!\n"));
223                 return NT_STATUS_NO_MEMORY;
224         }
225         entry->name = talloc_strdup(entry, name);
226         if ( ! entry->name) {
227                 DEBUG(0,("Out of memory!\n"));
228                 return NT_STATUS_NO_MEMORY;
229         }
230         entry->methods = methods;
231
232         DLIST_ADD(alloc_backends, entry);
233         DEBUG(5, ("Successfully added idmap alloc backend '%s'\n", name));
234         return NT_STATUS_OK;
235 }
236
237 static int close_domain_destructor(struct idmap_domain *dom)
238 {
239         NTSTATUS ret;
240
241         ret = dom->methods->close_fn(dom);
242         if (!NT_STATUS_IS_OK(ret)) {
243                 DEBUG(3, ("Failed to close idmap domain [%s]!\n", dom->name));
244         }
245
246         return 0;
247 }
248
249 static bool parse_idmap_module(TALLOC_CTX *mem_ctx, const char *param,
250                                char **pmodulename, char **pargs)
251 {
252         char *modulename;
253         char *args;
254
255         if (strncmp(param, "idmap_", 6) == 0) {
256                 param += 6;
257                 DEBUG(1, ("idmap_init: idmap backend uses deprecated "
258                           "'idmap_' prefix.  Please replace 'idmap_%s' by "
259                           "'%s'\n", param, param));
260         }
261
262         modulename = talloc_strdup(mem_ctx, param);
263         if (modulename == NULL) {
264                 return false;
265         }
266
267         args = strchr(modulename, ':');
268         if (args == NULL) {
269                 *pmodulename = modulename;
270                 *pargs = NULL;
271                 return true;
272         }
273
274         *args = '\0';
275
276         args = talloc_strdup(mem_ctx, args+1);
277         if (args == NULL) {
278                 TALLOC_FREE(modulename);
279                 return false;
280         }
281
282         *pmodulename = modulename;
283         *pargs = args;
284         return true;
285 }
286
287 /**
288  * Initialize a domain structure
289  * @param[in] mem_ctx           memory context for the result
290  * @param[in] domainname        which domain is this for
291  * @param[in] modulename        which backend module
292  * @param[in] params            parameter to pass to the init function
293  * @result The initialized structure
294  */
295 static struct idmap_domain *idmap_init_domain(TALLOC_CTX *mem_ctx,
296                                               const char *domainname,
297                                               const char *modulename,
298                                               const char *params)
299 {
300         struct idmap_domain *result;
301         NTSTATUS status;
302
303         result = talloc_zero(mem_ctx, struct idmap_domain);
304         if (result == NULL) {
305                 DEBUG(0, ("talloc failed\n"));
306                 return NULL;
307         }
308
309         result->name = talloc_strdup(result, domainname);
310         if (result->name == NULL) {
311                 DEBUG(0, ("talloc failed\n"));
312                 goto fail;
313         }
314
315         result->methods = get_methods(modulename);
316         if (result->methods == NULL) {
317                 DEBUG(3, ("idmap backend %s not found\n", modulename));
318
319                 status = smb_probe_module("idmap", modulename);
320                 if (!NT_STATUS_IS_OK(status)) {
321                         DEBUG(3, ("Could not probe idmap module %s\n",
322                                   modulename));
323                         goto fail;
324                 }
325
326                 result->methods = get_methods(modulename);
327         }
328         if (result->methods == NULL) {
329                 DEBUG(1, ("idmap backend %s not found\n", modulename));
330                 goto fail;
331         }
332
333         status = result->methods->init(result, params);
334         if (!NT_STATUS_IS_OK(status)) {
335                 DEBUG(1, ("idmap initialization returned %s\n",
336                           nt_errstr(status)));
337                 goto fail;
338         }
339
340         talloc_set_destructor(result, close_domain_destructor);
341
342         return result;
343
344 fail:
345         TALLOC_FREE(result);
346         return NULL;
347 }
348
349 /**
350  * Initialize the default domain structure
351  * @param[in] mem_ctx           memory context for the result
352  * @result The default domain structure
353  *
354  * This routine takes the module name from the "idmap backend" parameter,
355  * passing a possible parameter like ldap:ldap://ldap-url/ to the module.
356  */
357
358 static struct idmap_domain *idmap_init_default_domain(TALLOC_CTX *mem_ctx)
359 {
360         struct idmap_domain *result;
361         char *modulename;
362         char *params;
363
364         idmap_init();
365
366         if (!parse_idmap_module(talloc_tos(), lp_idmap_backend(), &modulename,
367                                 &params)) {
368                 DEBUG(1, ("parse_idmap_module failed\n"));
369                 return NULL;
370         }
371
372         DEBUG(3, ("idmap_init: using '%s' as remote backend\n", modulename));
373
374         result = idmap_init_domain(mem_ctx, "*", modulename, params);
375         if (result == NULL) {
376                 goto fail;
377         }
378
379         TALLOC_FREE(modulename);
380         TALLOC_FREE(params);
381         return result;
382
383 fail:
384         TALLOC_FREE(modulename);
385         TALLOC_FREE(params);
386         TALLOC_FREE(result);
387         return NULL;
388 }
389
390 /**
391  * Initialize a named domain structure
392  * @param[in] mem_ctx           memory context for the result
393  * @param[in] domname           the domain name
394  * @result The default domain structure
395  *
396  * This routine looks at the "idmap config <domname>" parameters to figure out
397  * the configuration.
398  */
399
400 static struct idmap_domain *idmap_init_named_domain(TALLOC_CTX *mem_ctx,
401                                                     const char *domname)
402 {
403         struct idmap_domain *result = NULL;
404         char *config_option;
405         const char *backend;
406
407         config_option = talloc_asprintf(talloc_tos(), "idmap config %s",
408                                         domname);
409         if (config_option == NULL) {
410                 DEBUG(0, ("talloc failed\n"));
411                 goto fail;
412         }
413
414         backend = lp_parm_const_string(-1, config_option, "backend", NULL);
415         if (backend == NULL) {
416                 DEBUG(1, ("no backend defined for %s\n", config_option));
417                 goto fail;
418         }
419
420         result = idmap_init_domain(mem_ctx, domname, backend, NULL);
421         if (result == NULL) {
422                 goto fail;
423         }
424
425         TALLOC_FREE(config_option);
426         return result;
427
428 fail:
429         TALLOC_FREE(config_option);
430         TALLOC_FREE(result);
431         return NULL;
432 }
433
434 /**
435  * Initialize the passdb domain structure
436  * @param[in] mem_ctx           memory context for the result
437  * @result The default domain structure
438  *
439  * No config, passdb has its own configuration.
440  */
441
442 static struct idmap_domain *idmap_init_passdb_domain(TALLOC_CTX *mem_ctx)
443 {
444         idmap_init();
445
446         if (passdb_idmap_domain != NULL) {
447                 return passdb_idmap_domain;
448         }
449
450         passdb_idmap_domain = idmap_init_domain(NULL, get_global_sam_name(),
451                                                 "passdb", NULL);
452         if (passdb_idmap_domain == NULL) {
453                 DEBUG(1, ("Could not init passdb idmap domain\n"));
454         }
455
456         return passdb_idmap_domain;
457 }
458
459 /**
460  * Find a domain struct according to a domain name
461  * @param[in] domname           Domain name to get the config for
462  * @result The default domain structure that fits
463  *
464  * This is the central routine in the winbindd-idmap child to pick the correct
465  * domain for looking up IDs. If domname is NULL or empty, we use the default
466  * domain. If it contains something, we try to use idmap_init_named_domain()
467  * to fetch the correct backend.
468  *
469  * The choice about "domname" is being made by the winbind parent, look at the
470  * "have_idmap_config" of "struct winbindd_domain" which is set in
471  * add_trusted_domain.
472  */
473
474 static struct idmap_domain *idmap_find_domain(const char *domname)
475 {
476         struct idmap_domain *result;
477         int i;
478
479         DEBUG(10, ("idmap_find_domain called for domain '%s'\n",
480                    domname?domname:"NULL"));
481
482         /*
483          * Always init the default domain, we can't go without one
484          */
485         if (default_idmap_domain == NULL) {
486                 default_idmap_domain = idmap_init_default_domain(NULL);
487         }
488         if (default_idmap_domain == NULL) {
489                 return NULL;
490         }
491
492         if ((domname == NULL) || (domname[0] == '\0')) {
493                 return default_idmap_domain;
494         }
495
496         for (i=0; i<num_domains; i++) {
497                 if (strequal(idmap_domains[i]->name, domname)) {
498                         return idmap_domains[i];
499                 }
500         }
501
502         if (idmap_domains == NULL) {
503                 /*
504                  * talloc context for all idmap domains
505                  */
506                 idmap_domains = TALLOC_ARRAY(NULL, struct idmap_domain *, 1);
507         }
508
509         if (idmap_domains == NULL) {
510                 DEBUG(0, ("talloc failed\n"));
511                 return NULL;
512         }
513
514         result = idmap_init_named_domain(idmap_domains, domname);
515         if (result == NULL) {
516                 /*
517                  * Could not init that domain -- try the default one
518                  */
519                 return default_idmap_domain;
520         }
521
522         ADD_TO_ARRAY(idmap_domains, struct idmap_domain *, result,
523                      &idmap_domains, &num_domains);
524         return result;
525 }
526
527 void idmap_close(void)
528 {
529         if (idmap_alloc_ctx) {
530                 idmap_alloc_ctx->methods->close_fn();
531                 idmap_alloc_ctx->methods = NULL;
532         }
533         alloc_backends = NULL;
534         TALLOC_FREE(default_idmap_domain);
535         TALLOC_FREE(passdb_idmap_domain);
536         TALLOC_FREE(idmap_domains);
537         num_domains = 0;
538 }
539
540 /**
541  * Initialize the idmap alloc backend
542  * @param[out] ctx              Where to put the alloc_ctx?
543  * @result Did it work fine?
544  *
545  * This routine first looks at "idmap alloc backend" and if that is not
546  * defined, it uses "idmap backend" for the module name.
547  */
548 static NTSTATUS idmap_alloc_init(struct idmap_alloc_context **ctx)
549 {
550         const char *backend;
551         char *modulename, *params;
552         NTSTATUS ret = NT_STATUS_NO_MEMORY;;
553
554         idmap_init();
555
556         if (idmap_alloc_ctx != NULL) {
557                 *ctx = idmap_alloc_ctx;
558                 return NT_STATUS_OK;
559         }
560
561         idmap_alloc_ctx = talloc(NULL, struct idmap_alloc_context);
562         if (idmap_alloc_ctx == NULL) {
563                 DEBUG(0, ("talloc failed\n"));
564                 goto fail;
565         }
566
567         backend = lp_idmap_alloc_backend();
568         if ((backend == NULL) || (backend[0] == '\0')) {
569                 backend = lp_idmap_backend();
570         }
571
572         if (backend == NULL) {
573                 DEBUG(3, ("no idmap alloc backend defined\n"));
574                 ret = NT_STATUS_INVALID_PARAMETER;
575                 goto fail;
576         }
577
578         if (!parse_idmap_module(idmap_alloc_ctx, backend, &modulename,
579                                 &params)) {
580                 DEBUG(1, ("parse_idmap_module %s failed\n", backend));
581                 goto fail;
582         }
583
584         idmap_alloc_ctx->methods = get_alloc_methods(modulename);
585
586         if (idmap_alloc_ctx->methods == NULL) {
587                 ret = smb_probe_module("idmap", modulename);
588                 if (NT_STATUS_IS_OK(ret)) {
589                         idmap_alloc_ctx->methods =
590                                 get_alloc_methods(modulename);
591                 }
592         }
593
594         if (idmap_alloc_ctx->methods == NULL) {
595                 DEBUG(1, ("could not find idmap alloc module %s\n", backend));
596                 ret = NT_STATUS_INVALID_PARAMETER;
597                 goto fail;
598         }
599
600         ret = idmap_alloc_ctx->methods->init(params);
601
602         if (!NT_STATUS_IS_OK(ret)) {
603                 DEBUG(0, ("ERROR: Initialization failed for alloc "
604                           "backend, deferred!\n"));
605                 goto fail;
606         }
607
608         TALLOC_FREE(modulename);
609         TALLOC_FREE(params);
610
611         *ctx = idmap_alloc_ctx;
612         return NT_STATUS_OK;
613
614 fail:
615         TALLOC_FREE(idmap_alloc_ctx);
616         return ret;
617 }
618
619 /**************************************************************************
620  idmap allocator interface functions
621 **************************************************************************/
622
623 NTSTATUS idmap_allocate_uid(struct unixid *id)
624 {
625         struct idmap_alloc_context *ctx;
626         NTSTATUS ret;
627
628         if (!NT_STATUS_IS_OK(ret = idmap_alloc_init(&ctx))) {
629                 return ret;
630         }
631
632         id->type = ID_TYPE_UID;
633         return ctx->methods->allocate_id(id);
634 }
635
636 NTSTATUS idmap_allocate_gid(struct unixid *id)
637 {
638         struct idmap_alloc_context *ctx;
639         NTSTATUS ret;
640
641         if (!NT_STATUS_IS_OK(ret = idmap_alloc_init(&ctx))) {
642                 return ret;
643         }
644
645         id->type = ID_TYPE_GID;
646         return ctx->methods->allocate_id(id);
647 }
648
649 NTSTATUS idmap_set_uid_hwm(struct unixid *id)
650 {
651         struct idmap_alloc_context *ctx;
652         NTSTATUS ret;
653
654         if (!NT_STATUS_IS_OK(ret = idmap_alloc_init(&ctx))) {
655                 return ret;
656         }
657
658         id->type = ID_TYPE_UID;
659         return ctx->methods->set_id_hwm(id);
660 }
661
662 NTSTATUS idmap_set_gid_hwm(struct unixid *id)
663 {
664         struct idmap_alloc_context *ctx;
665         NTSTATUS ret;
666
667         if (!NT_STATUS_IS_OK(ret = idmap_alloc_init(&ctx))) {
668                 return ret;
669         }
670
671         id->type = ID_TYPE_GID;
672         return ctx->methods->set_id_hwm(id);
673 }
674
675 NTSTATUS idmap_new_mapping(const struct dom_sid *psid, enum id_type type,
676                            struct unixid *pxid)
677 {
678         struct dom_sid sid;
679         struct idmap_domain *dom;
680         struct id_map map;
681         NTSTATUS status;
682
683         dom = idmap_find_domain(NULL);
684         if (dom == NULL) {
685                 DEBUG(3, ("no default domain, no place to write\n"));
686                 return NT_STATUS_ACCESS_DENIED;
687         }
688         if (dom->methods->set_mapping == NULL) {
689                 DEBUG(3, ("default domain not writable\n"));
690                 return NT_STATUS_MEDIA_WRITE_PROTECTED;
691         }
692
693         sid_copy(&sid, psid);
694         map.sid = &sid;
695         map.xid.type = type;
696
697         switch (type) {
698         case ID_TYPE_UID:
699                 status = idmap_allocate_uid(&map.xid);
700                 break;
701         case ID_TYPE_GID:
702                 status = idmap_allocate_gid(&map.xid);
703                 break;
704         default:
705                 status = NT_STATUS_INVALID_PARAMETER;
706                 break;
707         }
708
709         if (!NT_STATUS_IS_OK(status)) {
710                 DEBUG(3, ("Could not allocate id: %s\n", nt_errstr(status)));
711                 return status;
712         }
713
714         map.status = ID_MAPPED;
715
716         DEBUG(10, ("Setting mapping: %s <-> %s %lu\n",
717                    sid_string_dbg(map.sid),
718                    (map.xid.type == ID_TYPE_UID) ? "UID" : "GID",
719                    (unsigned long)map.xid.id));
720
721         status = dom->methods->set_mapping(dom, &map);
722
723         if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_COLLISION)) {
724                 struct id_map *ids[2];
725                 DEBUG(5, ("Mapping for %s exists - retrying to map sid\n",
726                           sid_string_dbg(map.sid)));
727                 ids[0] = &map;
728                 ids[1] = NULL;
729                 status = dom->methods->sids_to_unixids(dom, ids);
730         }
731
732         if (!NT_STATUS_IS_OK(status)) {
733                 DEBUG(3, ("Could not store the new mapping: %s\n",
734                           nt_errstr(status)));
735                 return status;
736         }
737
738         *pxid = map.xid;
739
740         return NT_STATUS_OK;
741 }
742
743 NTSTATUS idmap_backends_unixid_to_sid(const char *domname, struct id_map *id)
744 {
745         struct idmap_domain *dom;
746         struct id_map *maps[2];
747
748          DEBUG(10, ("idmap_backend_unixid_to_sid: domain = '%s', xid = %d "
749                     "(type %d)\n",
750                     domname?domname:"NULL", id->xid.id, id->xid.type));
751
752         maps[0] = id;
753         maps[1] = NULL;
754
755         /*
756          * Always give passdb a chance first
757          */
758
759         dom = idmap_init_passdb_domain(NULL);
760         if ((dom != NULL)
761             && NT_STATUS_IS_OK(dom->methods->unixids_to_sids(dom, maps))
762             && id->status == ID_MAPPED) {
763                 return NT_STATUS_OK;
764         }
765
766         dom = idmap_find_domain(domname);
767         if (dom == NULL) {
768                 return NT_STATUS_NONE_MAPPED;
769         }
770
771         return dom->methods->unixids_to_sids(dom, maps);
772 }
773
774 NTSTATUS idmap_backends_sid_to_unixid(const char *domain, struct id_map *id)
775 {
776         struct idmap_domain *dom;
777         struct id_map *maps[2];
778
779          DEBUG(10, ("idmap_backends_sid_to_unixid: domain = '%s', sid = [%s]\n",
780                     domain?domain:"NULL", sid_string_dbg(id->sid)));
781
782         maps[0] = id;
783         maps[1] = NULL;
784
785         if (sid_check_is_in_builtin(id->sid)
786             || (sid_check_is_in_our_domain(id->sid))) {
787
788                 dom = idmap_init_passdb_domain(NULL);
789                 if (dom == NULL) {
790                         return NT_STATUS_NONE_MAPPED;
791                 }
792                 return dom->methods->sids_to_unixids(dom, maps);
793         }
794
795         dom = idmap_find_domain(domain);
796         if (dom == NULL) {
797                 return NT_STATUS_NONE_MAPPED;
798         }
799
800         return dom->methods->sids_to_unixids(dom, maps);
801 }
802
803 NTSTATUS idmap_set_mapping(const struct id_map *map)
804 {
805         struct idmap_domain *dom;
806
807         dom = idmap_find_domain(NULL);
808         if (dom == NULL) {
809                 DEBUG(3, ("no default domain, no place to write\n"));
810                 return NT_STATUS_ACCESS_DENIED;
811         }
812         if (dom->methods->set_mapping == NULL) {
813                 DEBUG(3, ("default domain not writable\n"));
814                 return NT_STATUS_MEDIA_WRITE_PROTECTED;
815         }
816
817         return dom->methods->set_mapping(dom, map);
818 }
819
820 NTSTATUS idmap_remove_mapping(const struct id_map *map)
821 {
822         struct idmap_domain *dom;
823
824         dom = idmap_find_domain(NULL);
825         if (dom == NULL) {
826                 DEBUG(3, ("no default domain, no place to write\n"));
827                 return NT_STATUS_ACCESS_DENIED;
828         }
829         if (dom->methods->remove_mapping == NULL) {
830                 DEBUG(3, ("default domain not writable\n"));
831                 return NT_STATUS_MEDIA_WRITE_PROTECTED;
832         }
833
834         return dom->methods->remove_mapping(dom, map);
835 }