102f4ebb7fca7f80eba0c2877962765c206a4bb4
[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 #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         if (passdb_idmap_domain != NULL) {
452                 return passdb_idmap_domain;
453         }
454
455         passdb_idmap_domain = idmap_init_domain(NULL, get_global_sam_name(),
456                                                 "passdb", NULL, false);
457         if (passdb_idmap_domain == NULL) {
458                 DEBUG(1, ("Could not init passdb idmap domain\n"));
459         }
460
461         return passdb_idmap_domain;
462 }
463
464 /**
465  * Find a domain struct according to a domain name
466  * @param[in] domname           Domain name to get the config for
467  * @result The default domain structure that fits
468  *
469  * This is the central routine in the winbindd-idmap child to pick the correct
470  * domain for looking up IDs. If domname is NULL or empty, we use the default
471  * domain. If it contains something, we try to use idmap_init_named_domain()
472  * to fetch the correct backend.
473  *
474  * The choice about "domname" is being made by the winbind parent, look at the
475  * "have_idmap_config" of "struct winbindd_domain" which is set in
476  * add_trusted_domain.
477  */
478
479 static struct idmap_domain *idmap_find_domain(const char *domname)
480 {
481         struct idmap_domain *result;
482         int i;
483
484         DEBUG(10, ("idmap_find_domain called for domain '%s'\n",
485                    domname?domname:"NULL"));
486
487         /*
488          * Always init the default domain, we can't go without one
489          */
490         if (default_idmap_domain == NULL) {
491                 default_idmap_domain = idmap_init_default_domain(NULL);
492         }
493         if (default_idmap_domain == NULL) {
494                 return NULL;
495         }
496
497         if ((domname == NULL) || (domname[0] == '\0')) {
498                 return default_idmap_domain;
499         }
500
501         for (i=0; i<num_domains; i++) {
502                 if (strequal(idmap_domains[i]->name, domname)) {
503                         return idmap_domains[i];
504                 }
505         }
506
507         if (idmap_domains == NULL) {
508                 /*
509                  * talloc context for all idmap domains
510                  */
511                 idmap_domains = TALLOC_ARRAY(NULL, struct idmap_domain *, 1);
512         }
513
514         if (idmap_domains == NULL) {
515                 DEBUG(0, ("talloc failed\n"));
516                 return NULL;
517         }
518
519         result = idmap_init_named_domain(idmap_domains, domname);
520         if (result == NULL) {
521                 /*
522                  * Could not init that domain -- try the default one
523                  */
524                 return default_idmap_domain;
525         }
526
527         ADD_TO_ARRAY(idmap_domains, struct idmap_domain *, result,
528                      &idmap_domains, &num_domains);
529         return result;
530 }
531
532 void idmap_close(void)
533 {
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                 NTSTATUS status;
623
624                 DEBUG(10, ("asking passdb...\n"));
625
626                 dom = idmap_init_passdb_domain(NULL);
627                 if (dom == NULL) {
628                         return NT_STATUS_NONE_MAPPED;
629                 }
630                 status = dom->methods->sids_to_unixids(dom, maps);
631
632                 if (NT_STATUS_IS_OK(status) && id->status == ID_MAPPED) {
633                         return status;
634                 }
635
636                 DEBUG(10, ("passdb could not map, asking backends...\n"));
637         }
638
639         dom = idmap_find_domain(domain);
640         if (dom == NULL) {
641                 return NT_STATUS_NONE_MAPPED;
642         }
643
644         return dom->methods->sids_to_unixids(dom, maps);
645 }