s3:idmap: remove unused idmap_alloc_init().
[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  idmap allocator interface functions
542 **************************************************************************/
543
544 static NTSTATUS idmap_allocate_unixid(struct unixid *id)
545 {
546         struct idmap_domain *dom;
547         NTSTATUS ret;
548
549         dom = idmap_find_domain(NULL);
550
551         if (dom == NULL) {
552                 return NT_STATUS_UNSUCCESSFUL;
553         }
554
555         if (dom->methods->allocate_id == NULL) {
556                 return NT_STATUS_NOT_IMPLEMENTED;
557         }
558
559         ret = dom->methods->allocate_id(dom, id);
560
561         return ret;
562 }
563
564
565 NTSTATUS idmap_allocate_uid(struct unixid *id)
566 {
567         id->type = ID_TYPE_UID;
568         return idmap_allocate_unixid(id);
569 }
570
571 NTSTATUS idmap_allocate_gid(struct unixid *id)
572 {
573         id->type = ID_TYPE_GID;
574         return idmap_allocate_unixid(id);
575 }
576
577 NTSTATUS idmap_backends_unixid_to_sid(const char *domname, struct id_map *id)
578 {
579         struct idmap_domain *dom;
580         struct id_map *maps[2];
581
582          DEBUG(10, ("idmap_backend_unixid_to_sid: domain = '%s', xid = %d "
583                     "(type %d)\n",
584                     domname?domname:"NULL", id->xid.id, id->xid.type));
585
586         maps[0] = id;
587         maps[1] = NULL;
588
589         /*
590          * Always give passdb a chance first
591          */
592
593         dom = idmap_init_passdb_domain(NULL);
594         if ((dom != NULL)
595             && NT_STATUS_IS_OK(dom->methods->unixids_to_sids(dom, maps))
596             && id->status == ID_MAPPED) {
597                 return NT_STATUS_OK;
598         }
599
600         dom = idmap_find_domain(domname);
601         if (dom == NULL) {
602                 return NT_STATUS_NONE_MAPPED;
603         }
604
605         return dom->methods->unixids_to_sids(dom, maps);
606 }
607
608 NTSTATUS idmap_backends_sid_to_unixid(const char *domain, struct id_map *id)
609 {
610         struct idmap_domain *dom;
611         struct id_map *maps[2];
612
613          DEBUG(10, ("idmap_backends_sid_to_unixid: domain = '%s', sid = [%s]\n",
614                     domain?domain:"NULL", sid_string_dbg(id->sid)));
615
616         maps[0] = id;
617         maps[1] = NULL;
618
619         if (sid_check_is_in_builtin(id->sid)
620             || (sid_check_is_in_our_domain(id->sid))) {
621
622                 dom = idmap_init_passdb_domain(NULL);
623                 if (dom == NULL) {
624                         return NT_STATUS_NONE_MAPPED;
625                 }
626                 return dom->methods->sids_to_unixids(dom, maps);
627         }
628
629         dom = idmap_find_domain(domain);
630         if (dom == NULL) {
631                 return NT_STATUS_NONE_MAPPED;
632         }
633
634         return dom->methods->sids_to_unixids(dom, maps);
635 }