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