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