s3:winbindd: make sure we only call static_init_idmap once
[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         /*
445          * Always init the default domain, we can't go without one
446          */
447         if (default_idmap_domain == NULL) {
448                 default_idmap_domain = idmap_init_default_domain(NULL);
449         }
450         if (default_idmap_domain == NULL) {
451                 return NULL;
452         }
453
454         if (passdb_idmap_domain != NULL) {
455                 return passdb_idmap_domain;
456         }
457
458         passdb_idmap_domain = idmap_init_domain(NULL, get_global_sam_name(),
459                                                 "passdb", NULL);
460         if (passdb_idmap_domain == NULL) {
461                 DEBUG(1, ("Could not init passdb idmap domain\n"));
462         }
463
464         return passdb_idmap_domain;
465 }
466
467 /**
468  * Find a domain struct according to a domain name
469  * @param[in] domname           Domain name to get the config for
470  * @result The default domain structure that fits
471  *
472  * This is the central routine in the winbindd-idmap child to pick the correct
473  * domain for looking up IDs. If domname is NULL or empty, we use the default
474  * domain. If it contains something, we try to use idmap_init_named_domain()
475  * to fetch the correct backend.
476  *
477  * The choice about "domname" is being made by the winbind parent, look at the
478  * "have_idmap_config" of "struct winbindd_domain" which is set in
479  * add_trusted_domain.
480  */
481
482 static struct idmap_domain *idmap_find_domain(const char *domname)
483 {
484         struct idmap_domain *result;
485         int i;
486
487         DEBUG(10, ("idmap_find_domain called for domain '%s'\n",
488                    domname?domname:"NULL"));
489
490         /*
491          * Always init the default domain, we can't go without one
492          */
493         if (default_idmap_domain == NULL) {
494                 default_idmap_domain = idmap_init_default_domain(NULL);
495         }
496         if (default_idmap_domain == NULL) {
497                 return NULL;
498         }
499
500         if ((domname == NULL) || (domname[0] == '\0')) {
501                 return default_idmap_domain;
502         }
503
504         for (i=0; i<num_domains; i++) {
505                 if (strequal(idmap_domains[i]->name, domname)) {
506                         return idmap_domains[i];
507                 }
508         }
509
510         if (idmap_domains == NULL) {
511                 /*
512                  * talloc context for all idmap domains
513                  */
514                 idmap_domains = TALLOC_ARRAY(NULL, struct idmap_domain *, 1);
515         }
516
517         if (idmap_domains == NULL) {
518                 DEBUG(0, ("talloc failed\n"));
519                 return NULL;
520         }
521
522         result = idmap_init_named_domain(idmap_domains, domname);
523         if (result == NULL) {
524                 /*
525                  * Could not init that domain -- try the default one
526                  */
527                 return default_idmap_domain;
528         }
529
530         ADD_TO_ARRAY(idmap_domains, struct idmap_domain *, result,
531                      &idmap_domains, &num_domains);
532         return result;
533 }
534
535 void idmap_close(void)
536 {
537         if (idmap_alloc_ctx) {
538                 idmap_alloc_ctx->methods->close_fn();
539                 idmap_alloc_ctx->methods = NULL;
540         }
541         alloc_backends = NULL;
542         TALLOC_FREE(default_idmap_domain);
543         TALLOC_FREE(passdb_idmap_domain);
544         TALLOC_FREE(idmap_domains);
545         num_domains = 0;
546 }
547
548 /**
549  * Initialize the idmap alloc backend
550  * @param[out] ctx              Where to put the alloc_ctx?
551  * @result Did it work fine?
552  *
553  * This routine first looks at "idmap alloc backend" and if that is not
554  * defined, it uses "idmap backend" for the module name.
555  */
556 static NTSTATUS idmap_alloc_init(struct idmap_alloc_context **ctx)
557 {
558         const char *backend;
559         char *modulename, *params;
560         NTSTATUS ret = NT_STATUS_NO_MEMORY;;
561
562         idmap_init();
563
564         if (idmap_alloc_ctx != NULL) {
565                 *ctx = idmap_alloc_ctx;
566                 return NT_STATUS_OK;
567         }
568
569         idmap_alloc_ctx = talloc(NULL, struct idmap_alloc_context);
570         if (idmap_alloc_ctx == NULL) {
571                 DEBUG(0, ("talloc failed\n"));
572                 goto fail;
573         }
574
575         backend = lp_idmap_alloc_backend();
576         if ((backend == NULL) || (backend[0] == '\0')) {
577                 backend = lp_idmap_backend();
578         }
579
580         if (backend == NULL) {
581                 DEBUG(3, ("no idmap alloc backend defined\n"));
582                 ret = NT_STATUS_INVALID_PARAMETER;
583                 goto fail;
584         }
585
586         if (!parse_idmap_module(idmap_alloc_ctx, backend, &modulename,
587                                 &params)) {
588                 DEBUG(1, ("parse_idmap_module %s failed\n", backend));
589                 goto fail;
590         }
591
592         idmap_alloc_ctx->methods = get_alloc_methods(modulename);
593
594         if (idmap_alloc_ctx->methods == NULL) {
595                 ret = smb_probe_module("idmap", modulename);
596                 if (NT_STATUS_IS_OK(ret)) {
597                         idmap_alloc_ctx->methods =
598                                 get_alloc_methods(modulename);
599                 }
600         }
601
602         if (idmap_alloc_ctx->methods == NULL) {
603                 DEBUG(1, ("could not find idmap alloc module %s\n", backend));
604                 ret = NT_STATUS_INVALID_PARAMETER;
605                 goto fail;
606         }
607
608         ret = idmap_alloc_ctx->methods->init(params);
609
610         if (!NT_STATUS_IS_OK(ret)) {
611                 DEBUG(0, ("ERROR: Initialization failed for alloc "
612                           "backend, deferred!\n"));
613                 goto fail;
614         }
615
616         TALLOC_FREE(modulename);
617         TALLOC_FREE(params);
618
619         *ctx = idmap_alloc_ctx;
620         return NT_STATUS_OK;
621
622 fail:
623         TALLOC_FREE(idmap_alloc_ctx);
624         return ret;
625 }
626
627 /**************************************************************************
628  idmap allocator interface functions
629 **************************************************************************/
630
631 NTSTATUS idmap_allocate_uid(struct unixid *id)
632 {
633         struct idmap_alloc_context *ctx;
634         NTSTATUS ret;
635
636         if (!NT_STATUS_IS_OK(ret = idmap_alloc_init(&ctx))) {
637                 return ret;
638         }
639
640         id->type = ID_TYPE_UID;
641         return ctx->methods->allocate_id(id);
642 }
643
644 NTSTATUS idmap_allocate_gid(struct unixid *id)
645 {
646         struct idmap_alloc_context *ctx;
647         NTSTATUS ret;
648
649         if (!NT_STATUS_IS_OK(ret = idmap_alloc_init(&ctx))) {
650                 return ret;
651         }
652
653         id->type = ID_TYPE_GID;
654         return ctx->methods->allocate_id(id);
655 }
656
657 NTSTATUS idmap_set_uid_hwm(struct unixid *id)
658 {
659         struct idmap_alloc_context *ctx;
660         NTSTATUS ret;
661
662         if (!NT_STATUS_IS_OK(ret = idmap_alloc_init(&ctx))) {
663                 return ret;
664         }
665
666         id->type = ID_TYPE_UID;
667         return ctx->methods->set_id_hwm(id);
668 }
669
670 NTSTATUS idmap_set_gid_hwm(struct unixid *id)
671 {
672         struct idmap_alloc_context *ctx;
673         NTSTATUS ret;
674
675         if (!NT_STATUS_IS_OK(ret = idmap_alloc_init(&ctx))) {
676                 return ret;
677         }
678
679         id->type = ID_TYPE_GID;
680         return ctx->methods->set_id_hwm(id);
681 }
682
683 NTSTATUS idmap_new_mapping(const struct dom_sid *psid, enum id_type type,
684                            struct unixid *pxid)
685 {
686         struct dom_sid sid;
687         struct idmap_domain *dom;
688         struct id_map map;
689         NTSTATUS status;
690
691         dom = idmap_find_domain(NULL);
692         if (dom == NULL) {
693                 DEBUG(3, ("no default domain, no place to write\n"));
694                 return NT_STATUS_ACCESS_DENIED;
695         }
696         if (dom->methods->set_mapping == NULL) {
697                 DEBUG(3, ("default domain not writable\n"));
698                 return NT_STATUS_MEDIA_WRITE_PROTECTED;
699         }
700
701         sid_copy(&sid, psid);
702         map.sid = &sid;
703         map.xid.type = type;
704
705         switch (type) {
706         case ID_TYPE_UID:
707                 status = idmap_allocate_uid(&map.xid);
708                 break;
709         case ID_TYPE_GID:
710                 status = idmap_allocate_gid(&map.xid);
711                 break;
712         default:
713                 status = NT_STATUS_INVALID_PARAMETER;
714                 break;
715         }
716
717         if (!NT_STATUS_IS_OK(status)) {
718                 DEBUG(3, ("Could not allocate id: %s\n", nt_errstr(status)));
719                 return status;
720         }
721
722         map.status = ID_MAPPED;
723
724         DEBUG(10, ("Setting mapping: %s <-> %s %lu\n",
725                    sid_string_dbg(map.sid),
726                    (map.xid.type == ID_TYPE_UID) ? "UID" : "GID",
727                    (unsigned long)map.xid.id));
728
729         status = dom->methods->set_mapping(dom, &map);
730
731         if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_COLLISION)) {
732                 struct id_map *ids[2];
733                 DEBUG(5, ("Mapping for %s exists - retrying to map sid\n",
734                           sid_string_dbg(map.sid)));
735                 ids[0] = &map;
736                 ids[1] = NULL;
737                 status = dom->methods->sids_to_unixids(dom, ids);
738         }
739
740         if (!NT_STATUS_IS_OK(status)) {
741                 DEBUG(3, ("Could not store the new mapping: %s\n",
742                           nt_errstr(status)));
743                 return status;
744         }
745
746         *pxid = map.xid;
747
748         return NT_STATUS_OK;
749 }
750
751 NTSTATUS idmap_backends_unixid_to_sid(const char *domname, struct id_map *id)
752 {
753         struct idmap_domain *dom;
754         struct id_map *maps[2];
755
756          DEBUG(10, ("idmap_backend_unixid_to_sid: domain = '%s', xid = %d "
757                     "(type %d)\n",
758                     domname?domname:"NULL", id->xid.id, id->xid.type));
759
760         maps[0] = id;
761         maps[1] = NULL;
762
763         /*
764          * Always give passdb a chance first
765          */
766
767         dom = idmap_init_passdb_domain(NULL);
768         if ((dom != NULL)
769             && NT_STATUS_IS_OK(dom->methods->unixids_to_sids(dom, maps))
770             && id->status == ID_MAPPED) {
771                 return NT_STATUS_OK;
772         }
773
774         dom = idmap_find_domain(domname);
775         if (dom == NULL) {
776                 return NT_STATUS_NONE_MAPPED;
777         }
778
779         return dom->methods->unixids_to_sids(dom, maps);
780 }
781
782 NTSTATUS idmap_backends_sid_to_unixid(const char *domain, struct id_map *id)
783 {
784         struct idmap_domain *dom;
785         struct id_map *maps[2];
786
787          DEBUG(10, ("idmap_backends_sid_to_unixid: domain = '%s', sid = [%s]\n",
788                     domain?domain:"NULL", sid_string_dbg(id->sid)));
789
790         maps[0] = id;
791         maps[1] = NULL;
792
793         if (sid_check_is_in_builtin(id->sid)
794             || (sid_check_is_in_our_domain(id->sid))) {
795
796                 dom = idmap_init_passdb_domain(NULL);
797                 if (dom == NULL) {
798                         return NT_STATUS_NONE_MAPPED;
799                 }
800                 return dom->methods->sids_to_unixids(dom, maps);
801         }
802
803         dom = idmap_find_domain(domain);
804         if (dom == NULL) {
805                 return NT_STATUS_NONE_MAPPED;
806         }
807
808         return dom->methods->sids_to_unixids(dom, maps);
809 }
810
811 NTSTATUS idmap_set_mapping(const struct id_map *map)
812 {
813         struct idmap_domain *dom;
814
815         dom = idmap_find_domain(NULL);
816         if (dom == NULL) {
817                 DEBUG(3, ("no default domain, no place to write\n"));
818                 return NT_STATUS_ACCESS_DENIED;
819         }
820         if (dom->methods->set_mapping == NULL) {
821                 DEBUG(3, ("default domain not writable\n"));
822                 return NT_STATUS_MEDIA_WRITE_PROTECTED;
823         }
824
825         return dom->methods->set_mapping(dom, map);
826 }
827
828 NTSTATUS idmap_remove_mapping(const struct id_map *map)
829 {
830         struct idmap_domain *dom;
831
832         dom = idmap_find_domain(NULL);
833         if (dom == NULL) {
834                 DEBUG(3, ("no default domain, no place to write\n"));
835                 return NT_STATUS_ACCESS_DENIED;
836         }
837         if (dom->methods->remove_mapping == NULL) {
838                 DEBUG(3, ("default domain not writable\n"));
839                 return NT_STATUS_MEDIA_WRITE_PROTECTED;
840         }
841
842         return dom->methods->remove_mapping(dom, map);
843 }