532a90ed3d8df147296e49ed7d8ad20e65b8ffa9
[metze/samba/wip.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 /**
32  * Pointer to the backend methods. Modules register themselves here via
33  * smb_register_idmap.
34  */
35
36 struct idmap_backend {
37         const char *name;
38         struct idmap_methods *methods;
39         struct idmap_backend *prev, *next;
40 };
41 static struct idmap_backend *backends = NULL;
42
43 /**
44  * Default idmap domain configured via "idmap backend".
45  */
46 static struct idmap_domain *default_idmap_domain;
47
48 /**
49  * Passdb idmap domain, not configurable. winbind must always give passdb a
50  * chance to map ids.
51  */
52 static struct idmap_domain *passdb_idmap_domain;
53
54 /**
55  * List of specially configured idmap domains. This list is filled on demand
56  * in the winbind idmap child when the parent winbind figures out via the
57  * special range parameter or via the domain SID that a special "idmap config
58  * domain" configuration is present.
59  */
60 static struct idmap_domain **idmap_domains = NULL;
61 static int num_domains = 0;
62
63 static struct idmap_methods *get_methods(const char *name)
64 {
65         struct idmap_backend *b;
66
67         for (b = backends; b; b = b->next) {
68                 if (strequal(b->name, name)) {
69                         return b->methods;
70                 }
71         }
72
73         return NULL;
74 }
75
76 bool idmap_is_offline(void)
77 {
78         return ( lp_winbind_offline_logon() &&
79              get_global_winbindd_state_offline() );
80 }
81
82 bool idmap_is_online(void)
83 {
84         return !idmap_is_offline();
85 }
86
87 /**********************************************************************
88  Allow a module to register itself as a method.
89 **********************************************************************/
90
91 NTSTATUS smb_register_idmap(int version, const char *name,
92                             struct idmap_methods *methods)
93 {
94         struct idmap_backend *entry;
95
96         if ((version != SMB_IDMAP_INTERFACE_VERSION)) {
97                 DEBUG(0, ("Failed to register idmap module.\n"
98                           "The module was compiled against "
99                           "SMB_IDMAP_INTERFACE_VERSION %d,\n"
100                           "current SMB_IDMAP_INTERFACE_VERSION is %d.\n"
101                           "Please recompile against the current version "
102                           "of samba!\n",
103                           version, SMB_IDMAP_INTERFACE_VERSION));
104                 return NT_STATUS_OBJECT_TYPE_MISMATCH;
105         }
106
107         if (!name || !name[0] || !methods) {
108                 DEBUG(0,("Called with NULL pointer or empty name!\n"));
109                 return NT_STATUS_INVALID_PARAMETER;
110         }
111
112         for (entry = backends; entry != NULL; entry = entry->next) {
113                 if (strequal(entry->name, name)) {
114                         DEBUG(0,("Idmap module %s already registered!\n",
115                                  name));
116                         return NT_STATUS_OBJECT_NAME_COLLISION;
117                 }
118         }
119
120         entry = talloc(NULL, struct idmap_backend);
121         if ( ! entry) {
122                 DEBUG(0,("Out of memory!\n"));
123                 TALLOC_FREE(entry);
124                 return NT_STATUS_NO_MEMORY;
125         }
126         entry->name = talloc_strdup(entry, name);
127         if ( ! entry->name) {
128                 DEBUG(0,("Out of memory!\n"));
129                 TALLOC_FREE(entry);
130                 return NT_STATUS_NO_MEMORY;
131         }
132         entry->methods = methods;
133
134         DLIST_ADD(backends, entry);
135         DEBUG(5, ("Successfully added idmap backend '%s'\n", name));
136         return NT_STATUS_OK;
137 }
138
139 static int close_domain_destructor(struct idmap_domain *dom)
140 {
141         NTSTATUS ret;
142
143         ret = dom->methods->close_fn(dom);
144         if (!NT_STATUS_IS_OK(ret)) {
145                 DEBUG(3, ("Failed to close idmap domain [%s]!\n", dom->name));
146         }
147
148         return 0;
149 }
150
151 static bool parse_idmap_module(TALLOC_CTX *mem_ctx, const char *param,
152                                char **pmodulename, char **pargs)
153 {
154         char *modulename;
155         char *args;
156
157         if (strncmp(param, "idmap_", 6) == 0) {
158                 param += 6;
159                 DEBUG(1, ("idmap_init: idmap backend uses deprecated "
160                           "'idmap_' prefix.  Please replace 'idmap_%s' by "
161                           "'%s'\n", param, param));
162         }
163
164         modulename = talloc_strdup(mem_ctx, param);
165         if (modulename == NULL) {
166                 return false;
167         }
168
169         args = strchr(modulename, ':');
170         if (args == NULL) {
171                 *pmodulename = modulename;
172                 *pargs = NULL;
173                 return true;
174         }
175
176         *args = '\0';
177
178         args = talloc_strdup(mem_ctx, args+1);
179         if (args == NULL) {
180                 TALLOC_FREE(modulename);
181                 return false;
182         }
183
184         *pmodulename = modulename;
185         *pargs = args;
186         return true;
187 }
188
189 /**
190  * Initialize a domain structure
191  * @param[in] mem_ctx           memory context for the result
192  * @param[in] domainname        which domain is this for
193  * @param[in] modulename        which backend module
194  * @param[in] params            parameter to pass to the init function
195  * @param[in] check_range       whether range checking should be done
196  * @result The initialized structure
197  */
198 static struct idmap_domain *idmap_init_domain(TALLOC_CTX *mem_ctx,
199                                               const char *domainname,
200                                               const char *modulename,
201                                               const char *params,
202                                               bool check_range)
203 {
204         struct idmap_domain *result;
205         NTSTATUS status;
206
207         result = talloc_zero(mem_ctx, struct idmap_domain);
208         if (result == NULL) {
209                 DEBUG(0, ("talloc failed\n"));
210                 return NULL;
211         }
212
213         result->name = talloc_strdup(result, domainname);
214         if (result->name == NULL) {
215                 DEBUG(0, ("talloc failed\n"));
216                 goto fail;
217         }
218
219         /*
220          * load ranges and read only information from the config
221          */
222         if (strequal(result->name, "*")) {
223                 /*
224                  * The default domain "*" is configured differently
225                  * from named domains.
226                  */
227                 uid_t low_uid = 0;
228                 uid_t high_uid = 0;
229                 gid_t low_gid = 0;
230                 gid_t high_gid = 0;
231
232                 result->low_id = 0;
233                 result->high_id = 0;
234
235                 if (!lp_idmap_uid(&low_uid, &high_uid)) {
236                         DEBUG(1, ("'idmap uid' not set!\n"));
237                         if (check_range) {
238                                 goto fail;
239                         }
240                 }
241
242                 result->low_id = low_uid;
243                 result->high_id = high_uid;
244
245                 if (!lp_idmap_gid(&low_gid, &high_gid)) {
246                         DEBUG(1, ("'idmap gid' not set!\n"));
247                         if (check_range) {
248                                 goto fail;
249                         }
250                 }
251
252                 if ((low_gid != low_uid) || (high_gid != high_uid)) {
253                         DEBUG(1, ("Warning: 'idmap uid' and 'idmap gid'"
254                               " ranges do not agree -- building "
255                               "intersection\n"));
256                         result->low_id = MAX(result->low_id, low_gid);
257                         result->high_id = MIN(result->high_id, high_gid);
258                 }
259
260                 result->read_only = lp_idmap_read_only();
261         } else {
262                 char *config_option = NULL;
263                 const char *range;
264
265                 config_option = talloc_asprintf(result, "idmap config %s",
266                                                 result->name);
267                 if (config_option == NULL) {
268                         DEBUG(0, ("Out of memory!\n"));
269                         goto fail;
270                 }
271
272                 range = lp_parm_const_string(-1, config_option, "range", NULL);
273                 if (range == NULL) {
274                         DEBUG(1, ("idmap range not specified for domain %s\n",
275                                   result ->name));
276                         if (check_range) {
277                                 goto fail;
278                         }
279                 } else if (sscanf(range, "%u - %u", &result->low_id,
280                                   &result->high_id) != 2)
281                 {
282                         DEBUG(1, ("invalid range '%s' specified for domain "
283                                   "'%s'\n", range, result->name));
284                         if (check_range) {
285                                 goto fail;
286                         }
287                 }
288
289                 result->read_only = lp_parm_bool(-1, config_option, "read only",
290                                                  false);
291
292                 talloc_free(config_option);
293         }
294
295         if (result->low_id > result->high_id) {
296                 DEBUG(1, ("Error: invalid idmap range detected: %lu - %lu\n",
297                           (unsigned long)result->low_id,
298                           (unsigned long)result->high_id));
299                 if (check_range) {
300                         goto fail;
301                 }
302         }
303
304         result->methods = get_methods(modulename);
305         if (result->methods == NULL) {
306                 DEBUG(3, ("idmap backend %s not found\n", modulename));
307
308                 status = smb_probe_module("idmap", modulename);
309                 if (!NT_STATUS_IS_OK(status)) {
310                         DEBUG(3, ("Could not probe idmap module %s\n",
311                                   modulename));
312                         goto fail;
313                 }
314
315                 result->methods = get_methods(modulename);
316         }
317         if (result->methods == NULL) {
318                 DEBUG(1, ("idmap backend %s not found\n", modulename));
319                 goto fail;
320         }
321
322         status = result->methods->init(result, params);
323         if (!NT_STATUS_IS_OK(status)) {
324                 DEBUG(1, ("idmap initialization returned %s\n",
325                           nt_errstr(status)));
326                 goto fail;
327         }
328
329         talloc_set_destructor(result, close_domain_destructor);
330
331         return result;
332
333 fail:
334         TALLOC_FREE(result);
335         return NULL;
336 }
337
338 /**
339  * Initialize the default domain structure
340  * @param[in] mem_ctx           memory context for the result
341  * @result The default domain structure
342  *
343  * This routine takes the module name from the "idmap backend" parameter,
344  * passing a possible parameter like ldap:ldap://ldap-url/ to the module.
345  */
346
347 static struct idmap_domain *idmap_init_default_domain(TALLOC_CTX *mem_ctx)
348 {
349         struct idmap_domain *result;
350         char *modulename;
351         char *params;
352
353         DEBUG(10, ("idmap_init_default_domain: calling static_init_idmap\n"));
354
355         static_init_idmap;
356
357         if (!parse_idmap_module(talloc_tos(), lp_idmap_backend(), &modulename,
358                                 &params)) {
359                 DEBUG(1, ("parse_idmap_module failed\n"));
360                 return NULL;
361         }
362
363         DEBUG(3, ("idmap_init: using '%s' as remote backend\n", modulename));
364
365         result = idmap_init_domain(mem_ctx, "*", modulename, params, true);
366         if (result == NULL) {
367                 goto fail;
368         }
369
370         TALLOC_FREE(modulename);
371         TALLOC_FREE(params);
372         return result;
373
374 fail:
375         TALLOC_FREE(modulename);
376         TALLOC_FREE(params);
377         TALLOC_FREE(result);
378         return NULL;
379 }
380
381 /**
382  * Initialize a named domain structure
383  * @param[in] mem_ctx           memory context for the result
384  * @param[in] domname           the domain name
385  * @result The default domain structure
386  *
387  * This routine looks at the "idmap config <domname>" parameters to figure out
388  * the configuration.
389  */
390
391 static struct idmap_domain *idmap_init_named_domain(TALLOC_CTX *mem_ctx,
392                                                     const char *domname)
393 {
394         struct idmap_domain *result = NULL;
395         char *config_option;
396         const char *backend;
397
398         config_option = talloc_asprintf(talloc_tos(), "idmap config %s",
399                                         domname);
400         if (config_option == NULL) {
401                 DEBUG(0, ("talloc failed\n"));
402                 goto fail;
403         }
404
405         backend = lp_parm_const_string(-1, config_option, "backend", NULL);
406         if (backend == NULL) {
407                 DEBUG(1, ("no backend defined for %s\n", config_option));
408                 goto fail;
409         }
410
411         result = idmap_init_domain(mem_ctx, domname, backend, NULL, true);
412         if (result == NULL) {
413                 goto fail;
414         }
415
416         TALLOC_FREE(config_option);
417         return result;
418
419 fail:
420         TALLOC_FREE(config_option);
421         TALLOC_FREE(result);
422         return NULL;
423 }
424
425 /**
426  * Initialize the passdb domain structure
427  * @param[in] mem_ctx           memory context for the result
428  * @result The default domain structure
429  *
430  * No config, passdb has its own configuration.
431  */
432
433 static struct idmap_domain *idmap_init_passdb_domain(TALLOC_CTX *mem_ctx)
434 {
435         DEBUG(10, ("idmap_init_passdb_domain: calling static_init_idmap\n"));
436
437         static_init_idmap;
438
439         if (passdb_idmap_domain != NULL) {
440                 return passdb_idmap_domain;
441         }
442
443         passdb_idmap_domain = idmap_init_domain(NULL, get_global_sam_name(),
444                                                 "passdb", NULL, false);
445         if (passdb_idmap_domain == NULL) {
446                 DEBUG(1, ("Could not init passdb idmap domain\n"));
447         }
448
449         return passdb_idmap_domain;
450 }
451
452 /**
453  * Find a domain struct according to a domain name
454  * @param[in] domname           Domain name to get the config for
455  * @result The default domain structure that fits
456  *
457  * This is the central routine in the winbindd-idmap child to pick the correct
458  * domain for looking up IDs. If domname is NULL or empty, we use the default
459  * domain. If it contains something, we try to use idmap_init_named_domain()
460  * to fetch the correct backend.
461  *
462  * The choice about "domname" is being made by the winbind parent, look at the
463  * "have_idmap_config" of "struct winbindd_domain" which is set in
464  * add_trusted_domain.
465  */
466
467 static struct idmap_domain *idmap_find_domain(const char *domname)
468 {
469         struct idmap_domain *result;
470         int i;
471
472         DEBUG(10, ("idmap_find_domain called for domain '%s'\n",
473                    domname?domname:"NULL"));
474
475         /*
476          * Always init the default domain, we can't go without one
477          */
478         if (default_idmap_domain == NULL) {
479                 default_idmap_domain = idmap_init_default_domain(NULL);
480         }
481         if (default_idmap_domain == NULL) {
482                 return NULL;
483         }
484
485         if ((domname == NULL) || (domname[0] == '\0')) {
486                 return default_idmap_domain;
487         }
488
489         for (i=0; i<num_domains; i++) {
490                 if (strequal(idmap_domains[i]->name, domname)) {
491                         return idmap_domains[i];
492                 }
493         }
494
495         if (idmap_domains == NULL) {
496                 /*
497                  * talloc context for all idmap domains
498                  */
499                 idmap_domains = TALLOC_ARRAY(NULL, struct idmap_domain *, 1);
500         }
501
502         if (idmap_domains == NULL) {
503                 DEBUG(0, ("talloc failed\n"));
504                 return NULL;
505         }
506
507         result = idmap_init_named_domain(idmap_domains, domname);
508         if (result == NULL) {
509                 /*
510                  * Could not init that domain -- try the default one
511                  */
512                 return default_idmap_domain;
513         }
514
515         ADD_TO_ARRAY(idmap_domains, struct idmap_domain *, result,
516                      &idmap_domains, &num_domains);
517         return result;
518 }
519
520 void idmap_close(void)
521 {
522         TALLOC_FREE(default_idmap_domain);
523         TALLOC_FREE(passdb_idmap_domain);
524         TALLOC_FREE(idmap_domains);
525         num_domains = 0;
526 }
527
528 /**************************************************************************
529  idmap allocator interface functions
530 **************************************************************************/
531
532 static NTSTATUS idmap_allocate_unixid(struct unixid *id)
533 {
534         struct idmap_domain *dom;
535         NTSTATUS ret;
536
537         dom = idmap_find_domain(NULL);
538
539         if (dom == NULL) {
540                 return NT_STATUS_UNSUCCESSFUL;
541         }
542
543         if (dom->methods->allocate_id == NULL) {
544                 return NT_STATUS_NOT_IMPLEMENTED;
545         }
546
547         ret = dom->methods->allocate_id(dom, id);
548
549         return ret;
550 }
551
552
553 NTSTATUS idmap_allocate_uid(struct unixid *id)
554 {
555         id->type = ID_TYPE_UID;
556         return idmap_allocate_unixid(id);
557 }
558
559 NTSTATUS idmap_allocate_gid(struct unixid *id)
560 {
561         id->type = ID_TYPE_GID;
562         return idmap_allocate_unixid(id);
563 }
564
565 NTSTATUS idmap_backends_unixid_to_sid(const char *domname, struct id_map *id)
566 {
567         struct idmap_domain *dom;
568         struct id_map *maps[2];
569
570          DEBUG(10, ("idmap_backend_unixid_to_sid: domain = '%s', xid = %d "
571                     "(type %d)\n",
572                     domname?domname:"NULL", id->xid.id, id->xid.type));
573
574         maps[0] = id;
575         maps[1] = NULL;
576
577         /*
578          * Always give passdb a chance first
579          */
580
581         dom = idmap_init_passdb_domain(NULL);
582         if ((dom != NULL)
583             && NT_STATUS_IS_OK(dom->methods->unixids_to_sids(dom, maps))
584             && id->status == ID_MAPPED) {
585                 return NT_STATUS_OK;
586         }
587
588         dom = idmap_find_domain(domname);
589         if (dom == NULL) {
590                 return NT_STATUS_NONE_MAPPED;
591         }
592
593         return dom->methods->unixids_to_sids(dom, maps);
594 }
595
596 NTSTATUS idmap_backends_sid_to_unixid(const char *domain, struct id_map *id)
597 {
598         struct idmap_domain *dom;
599         struct id_map *maps[2];
600
601          DEBUG(10, ("idmap_backends_sid_to_unixid: domain = '%s', sid = [%s]\n",
602                     domain?domain:"NULL", sid_string_dbg(id->sid)));
603
604         maps[0] = id;
605         maps[1] = NULL;
606
607         if (sid_check_is_in_builtin(id->sid)
608             || (sid_check_is_in_our_domain(id->sid))) {
609
610                 dom = idmap_init_passdb_domain(NULL);
611                 if (dom == NULL) {
612                         return NT_STATUS_NONE_MAPPED;
613                 }
614                 return dom->methods->sids_to_unixids(dom, maps);
615         }
616
617         dom = idmap_find_domain(domain);
618         if (dom == NULL) {
619                 return NT_STATUS_NONE_MAPPED;
620         }
621
622         return dom->methods->sids_to_unixids(dom, maps);
623 }