idmap rewrite
[samba.git] / source / 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 struct idmap_backend {
32         const char *name;
33         struct idmap_methods *methods;
34         struct idmap_backend *prev, *next;
35 };
36
37 struct idmap_alloc_backend {
38         const char *name;
39         struct idmap_alloc_methods *methods;
40         struct idmap_alloc_backend *prev, *next;
41 };
42
43 struct idmap_alloc_context {
44         struct idmap_alloc_methods *methods;
45 };
46
47 /*
48  * Lists for the module initializations
49  */
50 static struct idmap_backend *backends = NULL;
51 static struct idmap_alloc_backend *alloc_backends = NULL;
52
53
54 static struct idmap_domain *default_idmap_domain;
55 static struct idmap_domain *passdb_idmap_domain;
56
57 static struct idmap_domain **idmap_domains = NULL;
58 static int num_domains = 0;
59
60 static struct idmap_alloc_context *idmap_alloc_ctx = NULL;
61
62 static struct idmap_methods *get_methods(struct idmap_backend *be,
63                                          const char *name)
64 {
65         struct idmap_backend *b;
66
67         for (b = be; b; b = b->next) {
68                 if (strequal(b->name, name)) {
69                         return b->methods;
70                 }
71         }
72
73         return NULL;
74 }
75
76 static struct idmap_alloc_methods *get_alloc_methods(
77                                                 struct idmap_alloc_backend *be,
78                                                 const char *name)
79 {
80         struct idmap_alloc_backend *b;
81
82         for (b = be; b; b = b->next) {
83                 if (strequal(b->name, name)) {
84                         return b->methods;
85                 }
86         }
87
88         return NULL;
89 }
90
91 bool idmap_is_offline(void)
92 {
93         return ( lp_winbind_offline_logon() &&
94              get_global_winbindd_state_offline() );
95 }
96
97 bool idmap_is_online(void)
98 {
99         return !idmap_is_offline();
100 }
101
102 /**********************************************************************
103  Allow a module to register itself as a method.
104 **********************************************************************/
105
106 NTSTATUS smb_register_idmap(int version, const char *name,
107                             struct idmap_methods *methods)
108 {
109         struct idmap_backend *entry;
110
111         if ((version != SMB_IDMAP_INTERFACE_VERSION)) {
112                 DEBUG(0, ("Failed to register idmap module.\n"
113                           "The module was compiled against "
114                           "SMB_IDMAP_INTERFACE_VERSION %d,\n"
115                           "current SMB_IDMAP_INTERFACE_VERSION is %d.\n"
116                           "Please recompile against the current version "
117                           "of samba!\n",
118                           version, SMB_IDMAP_INTERFACE_VERSION));
119                 return NT_STATUS_OBJECT_TYPE_MISMATCH;
120         }
121
122         if (!name || !name[0] || !methods) {
123                 DEBUG(0,("Called with NULL pointer or empty name!\n"));
124                 return NT_STATUS_INVALID_PARAMETER;
125         }
126
127         for (entry = backends; entry != NULL; entry = entry->next) {
128                 if (strequal(entry->name, name)) {
129                         DEBUG(0,("Idmap module %s already registered!\n", name));
130                         return NT_STATUS_OBJECT_NAME_COLLISION;
131                 }
132         }
133
134         entry = talloc(NULL, struct idmap_backend);
135         if ( ! entry) {
136                 DEBUG(0,("Out of memory!\n"));
137                 TALLOC_FREE(entry);
138                 return NT_STATUS_NO_MEMORY;
139         }
140         entry->name = talloc_strdup(entry, name);
141         if ( ! entry->name) {
142                 DEBUG(0,("Out of memory!\n"));
143                 TALLOC_FREE(entry);
144                 return NT_STATUS_NO_MEMORY;
145         }
146         entry->methods = methods;
147
148         DLIST_ADD(backends, entry);
149         DEBUG(5, ("Successfully added idmap backend '%s'\n", name));
150         return NT_STATUS_OK;
151 }
152
153 /**********************************************************************
154  Allow a module to register itself as a method.
155 **********************************************************************/
156
157 NTSTATUS smb_register_idmap_alloc(int version, const char *name,
158                                   struct idmap_alloc_methods *methods)
159 {
160         struct idmap_alloc_methods *test;
161         struct idmap_alloc_backend *entry;
162
163         if ((version != SMB_IDMAP_INTERFACE_VERSION)) {
164                 DEBUG(0, ("Failed to register idmap alloc module.\n"
165                           "The module was compiled against "
166                           "SMB_IDMAP_INTERFACE_VERSION %d,\n"
167                           "current SMB_IDMAP_INTERFACE_VERSION is %d.\n"
168                           "Please recompile against the current version "
169                           "of samba!\n",
170                           version, SMB_IDMAP_INTERFACE_VERSION));
171                 return NT_STATUS_OBJECT_TYPE_MISMATCH;
172         }
173
174         if (!name || !name[0] || !methods) {
175                 DEBUG(0,("Called with NULL pointer or empty name!\n"));
176                 return NT_STATUS_INVALID_PARAMETER;
177         }
178
179         test = get_alloc_methods(alloc_backends, name);
180         if (test) {
181                 DEBUG(0,("idmap_alloc module %s already registered!\n", name));
182                 return NT_STATUS_OBJECT_NAME_COLLISION;
183         }
184
185         entry = talloc(NULL, struct idmap_alloc_backend);
186         if ( ! entry) {
187                 DEBUG(0,("Out of memory!\n"));
188                 return NT_STATUS_NO_MEMORY;
189         }
190         entry->name = talloc_strdup(entry, name);
191         if ( ! entry->name) {
192                 DEBUG(0,("Out of memory!\n"));
193                 return NT_STATUS_NO_MEMORY;
194         }
195         entry->methods = methods;
196
197         DLIST_ADD(alloc_backends, entry);
198         DEBUG(5, ("Successfully added idmap alloc backend '%s'\n", name));
199         return NT_STATUS_OK;
200 }
201
202 static int close_domain_destructor(struct idmap_domain *dom)
203 {
204         NTSTATUS ret;
205
206         ret = dom->methods->close_fn(dom);
207         if (!NT_STATUS_IS_OK(ret)) {
208                 DEBUG(3, ("Failed to close idmap domain [%s]!\n", dom->name));
209         }
210
211         return 0;
212 }
213
214 static bool parse_idmap_module(TALLOC_CTX *mem_ctx, const char *param,
215                                char **pmodulename, char **pargs)
216 {
217         char *modulename;
218         char *args;
219
220         if (strncmp(param, "idmap_", 6) == 0) {
221                 param += 6;
222                 DEBUG(1, ("idmap_init: idmap backend uses deprecated "
223                           "'idmap_' prefix.  Please replace 'idmap_%s' by "
224                           "'%s'\n", param, param));
225         }
226
227         modulename = talloc_strdup(mem_ctx, param);
228         if (modulename == NULL) {
229                 return false;
230         }
231
232         args = strchr(modulename, ':');
233         if (args == NULL) {
234                 *pmodulename = modulename;
235                 *pargs = NULL;
236                 return true;
237         }
238
239         *args = '\0';
240
241         args = talloc_strdup(mem_ctx, args+1);
242         if (args == NULL) {
243                 TALLOC_FREE(modulename);
244                 return false;
245         }
246
247         *pmodulename = modulename;
248         *pargs = args;
249         return true;
250 }
251
252 static struct idmap_domain *idmap_init_domain(TALLOC_CTX *mem_ctx,
253                                               const char *domainname,
254                                               const char *modulename,
255                                               const char *params)
256 {
257         struct idmap_domain *result;
258         NTSTATUS status;
259
260         result = talloc_zero(mem_ctx, struct idmap_domain);
261         if (result == NULL) {
262                 DEBUG(0, ("talloc failed\n"));
263                 return NULL;
264         }
265
266         result->name = talloc_strdup(result, domainname);
267         if (result->name == NULL) {
268                 DEBUG(0, ("talloc failed\n"));
269                 goto fail;
270         }
271
272         result->methods = get_methods(backends, modulename);
273         if (result->methods == NULL) {
274                 DEBUG(3, ("idmap backend %s not found\n", modulename));
275
276                 status = smb_probe_module("idmap", modulename);
277                 if (!NT_STATUS_IS_OK(status)) {
278                         DEBUG(3, ("Could not probe idmap module %s\n",
279                                   modulename));
280                         goto fail;
281                 }
282
283                 result->methods = get_methods(backends, modulename);
284         }
285         if (result->methods == NULL) {
286                 DEBUG(1, ("idmap backend %s not found\n", modulename));
287                 goto fail;
288         }
289
290         status = result->methods->init(result, params);
291         if (!NT_STATUS_IS_OK(status)) {
292                 DEBUG(1, ("idmap initialization returned %s\n",
293                           nt_errstr(status)));
294                 goto fail;
295         }
296
297         talloc_set_destructor(result, close_domain_destructor);
298
299         return result;
300
301 fail:
302         TALLOC_FREE(result);
303         return NULL;
304 }
305
306 static struct idmap_domain *idmap_init_default_domain(TALLOC_CTX *mem_ctx)
307 {
308         struct idmap_domain *result;
309         char *modulename;
310         char *params;
311
312         DEBUG(10, ("idmap_init_default_domain: calling static_init_idmap\n"));
313
314         static_init_idmap;
315
316         if (!parse_idmap_module(talloc_tos(), lp_idmap_backend(), &modulename,
317                                 &params)) {
318                 DEBUG(1, ("parse_idmap_module failed\n"));
319                 return NULL;
320         }
321
322         DEBUG(3, ("idmap_init: using '%s' as remote backend\n", modulename));
323
324         result = idmap_init_domain(mem_ctx, "*", modulename, params);
325         if (result == NULL) {
326                 goto fail;
327         }
328
329         TALLOC_FREE(modulename);
330         TALLOC_FREE(params);
331         return result;
332
333 fail:
334         TALLOC_FREE(modulename);
335         TALLOC_FREE(params);
336         TALLOC_FREE(result);
337         return NULL;
338 }
339
340 static struct idmap_domain *idmap_init_named_domain(TALLOC_CTX *mem_ctx,
341                                                     const char *domname)
342 {
343         struct idmap_domain *result = NULL;
344         char *config_option;
345         const char *backend;
346
347         config_option = talloc_asprintf(talloc_tos(), "idmap config %s",
348                                         domname);
349         if (config_option == NULL) {
350                 DEBUG(0, ("talloc failed\n"));
351                 goto fail;
352         }
353
354         backend = lp_parm_const_string(-1, config_option, "backend", NULL);
355         if (backend == NULL) {
356                 DEBUG(1, ("no backend defined for %s\n", config_option));
357                 goto fail;
358         }
359
360         result = idmap_init_domain(mem_ctx, domname, backend, NULL);
361         if (result == NULL) {
362                 goto fail;
363         }
364
365         TALLOC_FREE(config_option);
366         return result;
367
368 fail:
369         TALLOC_FREE(config_option);
370         TALLOC_FREE(result);
371         return NULL;
372 }
373
374 static struct idmap_domain *idmap_init_passdb_domain(TALLOC_CTX *mem_ctx)
375 {
376         if (passdb_idmap_domain != NULL) {
377                 return passdb_idmap_domain;
378         }
379
380         passdb_idmap_domain = idmap_init_domain(NULL, get_global_sam_name(),
381                                                 "passdb", NULL);
382         if (passdb_idmap_domain == NULL) {
383                 DEBUG(1, ("Could not init passdb idmap domain\n"));
384         }
385
386         return passdb_idmap_domain;
387 }
388
389 static struct idmap_domain *idmap_find_domain(const char *domname)
390 {
391         struct idmap_domain *result;
392         int i;
393
394         /*
395          * Always init the default domain, we can't go without one
396          */
397         if (default_idmap_domain == NULL) {
398                 default_idmap_domain = idmap_init_default_domain(NULL);
399         }
400         if (default_idmap_domain == NULL) {
401                 return NULL;
402         }
403
404         if ((domname == NULL) || (domname[0] == '\0')) {
405                 return default_idmap_domain;
406         }
407
408         for (i=0; i<num_domains; i++) {
409                 if (strequal(idmap_domains[i]->name, domname)) {
410                         return idmap_domains[i];
411                 }
412         }
413
414         if (idmap_domains == NULL) {
415                 /*
416                  * talloc context for all idmap domains
417                  */
418                 idmap_domains = TALLOC_ARRAY(NULL, struct idmap_domain *, 1);
419         }
420
421         if (idmap_domains == NULL) {
422                 DEBUG(0, ("talloc failed\n"));
423                 return NULL;
424         }
425
426         result = idmap_init_named_domain(idmap_domains, domname);
427         if (result == NULL) {
428                 /*
429                  * Could not init that domain -- try the default one
430                  */
431                 return default_idmap_domain;
432         }
433
434         ADD_TO_ARRAY(idmap_domains, struct idmap_domain *, result,
435                      &idmap_domains, &num_domains);
436         return result;
437 }
438
439 void idmap_close(void)
440 {
441         if (idmap_alloc_ctx) {
442                 idmap_alloc_ctx->methods->close_fn();
443                 idmap_alloc_ctx->methods = NULL;
444         }
445         alloc_backends = NULL;
446         TALLOC_FREE(default_idmap_domain);
447         TALLOC_FREE(passdb_idmap_domain);
448         TALLOC_FREE(idmap_domains);
449         num_domains = 0;
450 }
451
452 static NTSTATUS idmap_alloc_init(struct idmap_alloc_context **ctx)
453 {
454         const char *backend;
455         char *modulename, *params;
456         NTSTATUS ret = NT_STATUS_NO_MEMORY;;
457
458         if (idmap_alloc_ctx != NULL) {
459                 *ctx = idmap_alloc_ctx;
460                 return NT_STATUS_OK;
461         }
462
463         idmap_alloc_ctx = talloc(NULL, struct idmap_alloc_context);
464         if (idmap_alloc_ctx == NULL) {
465                 DEBUG(0, ("talloc failed\n"));
466                 goto fail;
467         }
468
469         backend = lp_idmap_alloc_backend();
470         if ((backend == NULL) || (backend[0] == '\0')) {
471                 backend = lp_idmap_backend();
472         }
473
474         if (backend == NULL) {
475                 DEBUG(3, ("no idmap alloc backend defined\n"));
476                 ret = NT_STATUS_INVALID_PARAMETER;
477                 goto fail;
478         }
479
480         if (!parse_idmap_module(idmap_alloc_ctx, backend, &modulename,
481                                 &params)) {
482                 DEBUG(1, ("parse_idmap_module %s failed\n", backend));
483                 goto fail;
484         }
485
486         idmap_alloc_ctx->methods = get_alloc_methods(alloc_backends,
487                                                      modulename);
488
489         if (idmap_alloc_ctx->methods == NULL) {
490                 ret = smb_probe_module("idmap", modulename);
491                 if (NT_STATUS_IS_OK(ret)) {
492                         idmap_alloc_ctx->methods =
493                                 get_alloc_methods(alloc_backends,
494                                                   modulename);
495                 }
496         }
497
498         if (idmap_alloc_ctx->methods == NULL) {
499                 DEBUG(1, ("could not find idmap alloc module %s\n", backend));
500                 ret = NT_STATUS_INVALID_PARAMETER;
501                 goto fail;
502         }
503
504         ret = idmap_alloc_ctx->methods->init(params);
505
506         if (!NT_STATUS_IS_OK(ret)) {
507                 DEBUG(0, ("ERROR: Initialization failed for alloc "
508                           "backend, deferred!\n"));
509                 goto fail;
510         }
511
512         TALLOC_FREE(modulename);
513         TALLOC_FREE(params);
514
515         *ctx = idmap_alloc_ctx;
516         return NT_STATUS_OK;
517
518 fail:
519         TALLOC_FREE(idmap_alloc_ctx);
520         return ret;
521 }
522
523 /**************************************************************************
524  idmap allocator interface functions
525 **************************************************************************/
526
527 NTSTATUS idmap_allocate_uid(struct unixid *id)
528 {
529         struct idmap_alloc_context *ctx;
530         NTSTATUS ret;
531
532         if (!NT_STATUS_IS_OK(ret = idmap_alloc_init(&ctx))) {
533                 return ret;
534         }
535
536         id->type = ID_TYPE_UID;
537         return ctx->methods->allocate_id(id);
538 }
539
540 NTSTATUS idmap_allocate_gid(struct unixid *id)
541 {
542         struct idmap_alloc_context *ctx;
543         NTSTATUS ret;
544
545         if (!NT_STATUS_IS_OK(ret = idmap_alloc_init(&ctx))) {
546                 return ret;
547         }
548
549         id->type = ID_TYPE_GID;
550         return ctx->methods->allocate_id(id);
551 }
552
553 NTSTATUS idmap_set_uid_hwm(struct unixid *id)
554 {
555         struct idmap_alloc_context *ctx;
556         NTSTATUS ret;
557
558         if (!NT_STATUS_IS_OK(ret = idmap_alloc_init(&ctx))) {
559                 return ret;
560         }
561
562         id->type = ID_TYPE_UID;
563         return ctx->methods->set_id_hwm(id);
564 }
565
566 NTSTATUS idmap_set_gid_hwm(struct unixid *id)
567 {
568         struct idmap_alloc_context *ctx;
569         NTSTATUS ret;
570
571         if (!NT_STATUS_IS_OK(ret = idmap_alloc_init(&ctx))) {
572                 return ret;
573         }
574
575         id->type = ID_TYPE_GID;
576         return ctx->methods->set_id_hwm(id);
577 }
578
579 NTSTATUS idmap_new_mapping(const struct dom_sid *psid, enum id_type type,
580                            struct unixid *pxid)
581 {
582         struct dom_sid sid;
583         struct idmap_domain *dom;
584         struct id_map map;
585         NTSTATUS status;
586
587         dom = idmap_find_domain(NULL);
588         if (dom == NULL) {
589                 DEBUG(3, ("no default domain, no place to write\n"));
590                 return NT_STATUS_ACCESS_DENIED;
591         }
592         if (dom->methods->set_mapping == NULL) {
593                 DEBUG(3, ("default domain not writable\n"));
594                 return NT_STATUS_MEDIA_WRITE_PROTECTED;
595         }
596
597         sid_copy(&sid, psid);
598         map.sid = &sid;
599         map.xid.type = type;
600
601         switch (type) {
602         case ID_TYPE_UID:
603                 status = idmap_allocate_uid(&map.xid);
604                 break;
605         case ID_TYPE_GID:
606                 status = idmap_allocate_gid(&map.xid);
607                 break;
608         default:
609                 status = NT_STATUS_INVALID_PARAMETER;
610                 break;
611         }
612
613         if (!NT_STATUS_IS_OK(status)) {
614                 DEBUG(3, ("Could not allocate id: %s\n", nt_errstr(status)));
615                 return status;
616         }
617
618         map.status = ID_MAPPED;
619
620         DEBUG(10, ("Setting mapping: %s <-> %s %lu\n",
621                    sid_string_dbg(map.sid),
622                    (map.xid.type == ID_TYPE_UID) ? "UID" : "GID",
623                    (unsigned long)map.xid.id));
624
625         status = dom->methods->set_mapping(dom, &map);
626
627         if (!NT_STATUS_IS_OK(status)) {
628                 DEBUG(3, ("Could not store the new mapping: %s\n",
629                           nt_errstr(status)));
630                 return status;
631         }
632
633         *pxid = map.xid;
634
635         return NT_STATUS_OK;
636 }
637
638 NTSTATUS idmap_backends_unixid_to_sid(const char *domname, struct id_map *id)
639 {
640         struct idmap_domain *dom;
641         struct id_map *maps[2];
642
643         maps[0] = id;
644         maps[1] = NULL;
645
646         /*
647          * Always give passdb a chance first
648          */
649
650         dom = idmap_init_passdb_domain(NULL);
651         if ((dom != NULL)
652             && NT_STATUS_IS_OK(dom->methods->unixids_to_sids(dom, maps))) {
653                 return NT_STATUS_OK;
654         }
655
656         dom = idmap_find_domain(domname);
657         if (dom == NULL) {
658                 return NT_STATUS_NONE_MAPPED;
659         }
660
661         return dom->methods->unixids_to_sids(dom, maps);
662 }
663
664 NTSTATUS idmap_backends_sid_to_unixid(const char *domain, struct id_map *id)
665 {
666         struct idmap_domain *dom;
667         struct id_map *maps[2];
668
669         maps[0] = id;
670         maps[1] = NULL;
671
672         if (sid_check_is_in_builtin(id->sid)
673             || (sid_check_is_in_our_domain(id->sid))) {
674
675                 dom = idmap_init_passdb_domain(NULL);
676                 if (dom == NULL) {
677                         return NT_STATUS_NONE_MAPPED;
678                 }
679                 return dom->methods->sids_to_unixids(dom, maps);
680         }
681
682         dom = idmap_find_domain(domain);
683         if (dom == NULL) {
684                 return NT_STATUS_NONE_MAPPED;
685         }
686
687         return dom->methods->sids_to_unixids(dom, maps);
688 }
689
690 NTSTATUS idmap_set_mapping(const struct id_map *map)
691 {
692         struct idmap_domain *dom;
693
694         dom = idmap_find_domain(NULL);
695         if (dom == NULL) {
696                 DEBUG(3, ("no default domain, no place to write\n"));
697                 return NT_STATUS_ACCESS_DENIED;
698         }
699         if (dom->methods->set_mapping == NULL) {
700                 DEBUG(3, ("default domain not writable\n"));
701                 return NT_STATUS_MEDIA_WRITE_PROTECTED;
702         }
703
704         return dom->methods->set_mapping(dom, map);
705 }