841f71067617c037afdea6a4fd4dc93d2bfcd504
[nivanova/samba-autobuild/.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    Copyright (C) Michael Adam 2010
9
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 3 of the License, or
13    (at your option) any later version.
14
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with this program.  If not, see <http://www.gnu.org/licenses/>.
22 */
23
24 #include "includes.h"
25 #include "winbindd.h"
26 #include "idmap.h"
27 #include "lib/util_sid_passdb.h"
28 #include "passdb.h"
29
30 #undef DBGC_CLASS
31 #define DBGC_CLASS DBGC_IDMAP
32
33 static_decl_idmap;
34
35 static void idmap_init(void)
36 {
37         static bool initialized;
38
39         if (initialized) {
40                 return;
41         }
42
43         DEBUG(10, ("idmap_init(): calling static_init_idmap\n"));
44
45         static_init_idmap;
46
47         initialized = true;
48 }
49
50 /**
51  * Pointer to the backend methods. Modules register themselves here via
52  * smb_register_idmap.
53  */
54
55 struct idmap_backend {
56         const char *name;
57         struct idmap_methods *methods;
58         struct idmap_backend *prev, *next;
59 };
60 static struct idmap_backend *backends = NULL;
61
62 /**
63  * Default idmap domain configured via "idmap backend".
64  */
65 static struct idmap_domain *default_idmap_domain;
66
67 /**
68  * Passdb idmap domain, not configurable. winbind must always give passdb a
69  * chance to map ids.
70  */
71 static struct idmap_domain *passdb_idmap_domain;
72
73 /**
74  * List of specially configured idmap domains. This list is filled on demand
75  * in the winbind idmap child when the parent winbind figures out via the
76  * special range parameter or via the domain SID that a special "idmap config
77  * domain" configuration is present.
78  */
79 static struct idmap_domain **idmap_domains = NULL;
80 static int num_domains = 0;
81
82 static struct idmap_methods *get_methods(const char *name)
83 {
84         struct idmap_backend *b;
85
86         for (b = backends; b; b = b->next) {
87                 if (strequal(b->name, name)) {
88                         return b->methods;
89                 }
90         }
91
92         return NULL;
93 }
94
95 bool idmap_is_offline(void)
96 {
97         return ( lp_winbind_offline_logon() &&
98              get_global_winbindd_state_offline() );
99 }
100
101 bool idmap_is_online(void)
102 {
103         return !idmap_is_offline();
104 }
105
106 /**********************************************************************
107  Allow a module to register itself as a method.
108 **********************************************************************/
109
110 NTSTATUS smb_register_idmap(int version, const char *name,
111                             struct idmap_methods *methods)
112 {
113         struct idmap_backend *entry;
114
115         if ((version != SMB_IDMAP_INTERFACE_VERSION)) {
116                 DEBUG(0, ("Failed to register idmap module.\n"
117                           "The module was compiled against "
118                           "SMB_IDMAP_INTERFACE_VERSION %d,\n"
119                           "current SMB_IDMAP_INTERFACE_VERSION is %d.\n"
120                           "Please recompile against the current version "
121                           "of samba!\n",
122                           version, SMB_IDMAP_INTERFACE_VERSION));
123                 return NT_STATUS_OBJECT_TYPE_MISMATCH;
124         }
125
126         if (!name || !name[0] || !methods) {
127                 DEBUG(0,("Called with NULL pointer or empty name!\n"));
128                 return NT_STATUS_INVALID_PARAMETER;
129         }
130
131         for (entry = backends; entry != NULL; entry = entry->next) {
132                 if (strequal(entry->name, name)) {
133                         DEBUG(5,("Idmap module %s already registered!\n",
134                                  name));
135                         return NT_STATUS_OBJECT_NAME_COLLISION;
136                 }
137         }
138
139         entry = talloc(NULL, struct idmap_backend);
140         if ( ! entry) {
141                 DEBUG(0,("Out of memory!\n"));
142                 TALLOC_FREE(entry);
143                 return NT_STATUS_NO_MEMORY;
144         }
145         entry->name = talloc_strdup(entry, name);
146         if ( ! entry->name) {
147                 DEBUG(0,("Out of memory!\n"));
148                 TALLOC_FREE(entry);
149                 return NT_STATUS_NO_MEMORY;
150         }
151         entry->methods = methods;
152
153         DLIST_ADD(backends, entry);
154         DEBUG(5, ("Successfully added idmap backend '%s'\n", name));
155         return NT_STATUS_OK;
156 }
157
158 /**
159  * Initialize a domain structure
160  * @param[in] mem_ctx           memory context for the result
161  * @param[in] domainname        which domain is this for
162  * @param[in] modulename        which backend module
163  * @param[in] check_range       whether range checking should be done
164  * @result The initialized structure
165  */
166 static struct idmap_domain *idmap_init_domain(TALLOC_CTX *mem_ctx,
167                                               const char *domainname,
168                                               const char *modulename,
169                                               bool check_range)
170 {
171         struct idmap_domain *result;
172         NTSTATUS status;
173         char *config_option = NULL;
174         const char *range;
175         unsigned low_id, high_id;
176
177         result = talloc_zero(mem_ctx, struct idmap_domain);
178         if (result == NULL) {
179                 DEBUG(0, ("talloc failed\n"));
180                 return NULL;
181         }
182
183         result->name = talloc_strdup(result, domainname);
184         if (result->name == NULL) {
185                 DEBUG(0, ("talloc failed\n"));
186                 goto fail;
187         }
188
189         /*
190          * Check whether the requested backend module exists and
191          * load the methods.
192          */
193
194         result->methods = get_methods(modulename);
195         if (result->methods == NULL) {
196                 DEBUG(3, ("idmap backend %s not found\n", modulename));
197
198                 status = smb_probe_module("idmap", modulename);
199                 if (!NT_STATUS_IS_OK(status)) {
200                         DEBUG(3, ("Could not probe idmap module %s\n",
201                                   modulename));
202                         goto fail;
203                 }
204
205                 result->methods = get_methods(modulename);
206         }
207         if (result->methods == NULL) {
208                 DEBUG(1, ("idmap backend %s not found\n", modulename));
209                 goto fail;
210         }
211
212         /*
213          * load ranges and read only information from the config
214          */
215
216         config_option = talloc_asprintf(result, "idmap config %s",
217                                         result->name);
218         if (config_option == NULL) {
219                 DEBUG(0, ("Out of memory!\n"));
220                 goto fail;
221         }
222
223         result->read_only = lp_parm_bool(-1, config_option, "read only", false);
224         range = lp_parm_const_string(-1, config_option, "range", NULL);
225
226         talloc_free(config_option);
227
228         if (range == NULL) {
229                 if (check_range) {
230                         DEBUG(1, ("idmap range not specified for domain %s\n",
231                                   result->name));
232                         goto fail;
233                 }
234         } else if (sscanf(range, "%u - %u", &low_id, &high_id) != 2)
235         {
236                 DEBUG(1, ("invalid range '%s' specified for domain "
237                           "'%s'\n", range, result->name));
238                 if (check_range) {
239                         goto fail;
240                 }
241         } else if (low_id > high_id) {
242                 DEBUG(1, ("Error: invalid idmap range detected: %u - %u\n",
243                           low_id, high_id));
244                 if (check_range) {
245                         goto fail;
246                 }
247         }
248
249         result->low_id = low_id;
250         result->high_id = high_id;
251
252         status = result->methods->init(result);
253         if (!NT_STATUS_IS_OK(status)) {
254                 DEBUG(1, ("idmap initialization returned %s\n",
255                           nt_errstr(status)));
256                 goto fail;
257         }
258
259         return result;
260
261 fail:
262         TALLOC_FREE(result);
263         return NULL;
264 }
265
266 /**
267  * Initialize a named domain structure
268  * @param[in] mem_ctx           memory context for the result
269  * @param[in] domname           the domain name
270  * @result The default domain structure
271  *
272  * This routine looks at the "idmap config <domname>" parameters to figure out
273  * the configuration.
274  */
275
276 static struct idmap_domain *idmap_init_named_domain(TALLOC_CTX *mem_ctx,
277                                                     const char *domname)
278 {
279         struct idmap_domain *result = NULL;
280         char *config_option;
281         const char *backend;
282
283         idmap_init();
284
285         config_option = talloc_asprintf(talloc_tos(), "idmap config %s",
286                                         domname);
287         if (config_option == NULL) {
288                 DEBUG(0, ("talloc failed\n"));
289                 goto fail;
290         }
291
292         backend = lp_parm_const_string(-1, config_option, "backend", NULL);
293         if (backend == NULL) {
294                 DEBUG(10, ("no idmap backend configured for domain '%s'\n",
295                            domname));
296                 goto fail;
297         }
298
299         result = idmap_init_domain(mem_ctx, domname, backend, true);
300         if (result == NULL) {
301                 goto fail;
302         }
303
304         TALLOC_FREE(config_option);
305         return result;
306
307 fail:
308         TALLOC_FREE(config_option);
309         TALLOC_FREE(result);
310         return NULL;
311 }
312
313 /**
314  * Initialize the default domain structure
315  * @param[in] mem_ctx           memory context for the result
316  * @result The default domain structure
317  *
318  * This routine takes the module name from the "idmap backend" parameter,
319  * passing a possible parameter like ldap:ldap://ldap-url/ to the module.
320  */
321
322 static struct idmap_domain *idmap_init_default_domain(TALLOC_CTX *mem_ctx)
323 {
324         return idmap_init_named_domain(mem_ctx, "*");
325 }
326
327 /**
328  * Initialize the passdb domain structure
329  * @param[in] mem_ctx           memory context for the result
330  * @result The default domain structure
331  *
332  * No config, passdb has its own configuration.
333  */
334
335 static struct idmap_domain *idmap_passdb_domain(TALLOC_CTX *mem_ctx)
336 {
337         idmap_init();
338
339         if (!pdb_is_responsible_for_everything_else()) {
340                 /*
341                  * Always init the default domain, we can't go without one
342                  */
343                 if (default_idmap_domain == NULL) {
344                         default_idmap_domain = idmap_init_default_domain(NULL);
345                 }
346                 if (default_idmap_domain == NULL) {
347                         return NULL;
348                 }
349         }
350
351         if (passdb_idmap_domain != NULL) {
352                 return passdb_idmap_domain;
353         }
354
355         passdb_idmap_domain = idmap_init_domain(mem_ctx, get_global_sam_name(),
356                                                 "passdb", false);
357         if (passdb_idmap_domain == NULL) {
358                 DEBUG(1, ("Could not init passdb idmap domain\n"));
359         }
360
361         return passdb_idmap_domain;
362 }
363
364 /**
365  * Find a domain struct according to a domain name
366  * @param[in] domname           Domain name to get the config for
367  * @result The default domain structure that fits
368  *
369  * This is the central routine in the winbindd-idmap child to pick the correct
370  * domain for looking up IDs. If domname is NULL or empty, we use the default
371  * domain. If it contains something, we try to use idmap_init_named_domain()
372  * to fetch the correct backend.
373  *
374  * The choice about "domname" is being made by the winbind parent, look at the
375  * "have_idmap_config" of "struct winbindd_domain" which is set in
376  * add_trusted_domain.
377  */
378
379 static struct idmap_domain *idmap_find_domain(const char *domname)
380 {
381         struct idmap_domain *result;
382         int i;
383
384         DEBUG(10, ("idmap_find_domain called for domain '%s'\n",
385                    domname?domname:"NULL"));
386
387         idmap_init();
388
389         /*
390          * Always init the default domain, we can't go without one
391          */
392         if (default_idmap_domain == NULL) {
393                 default_idmap_domain = idmap_init_default_domain(NULL);
394         }
395         if (default_idmap_domain == NULL) {
396                 return NULL;
397         }
398
399         if ((domname == NULL) || (domname[0] == '\0')) {
400                 return default_idmap_domain;
401         }
402
403         for (i=0; i<num_domains; i++) {
404                 if (strequal(idmap_domains[i]->name, domname)) {
405                         return idmap_domains[i];
406                 }
407         }
408
409         if (idmap_domains == NULL) {
410                 /*
411                  * talloc context for all idmap domains
412                  */
413                 idmap_domains = talloc_array(NULL, struct idmap_domain *, 1);
414         }
415
416         if (idmap_domains == NULL) {
417                 DEBUG(0, ("talloc failed\n"));
418                 return NULL;
419         }
420
421         result = idmap_init_named_domain(idmap_domains, domname);
422         if (result == NULL) {
423                 /*
424                  * Could not init that domain -- try the default one
425                  */
426                 return default_idmap_domain;
427         }
428
429         ADD_TO_ARRAY(idmap_domains, struct idmap_domain *, result,
430                      &idmap_domains, &num_domains);
431         return result;
432 }
433
434 struct idmap_domain *idmap_find_domain_with_sid(const char *domname,
435                                                 const struct dom_sid *sid)
436 {
437         idmap_init();
438
439         if (sid_check_is_for_passdb(sid)) {
440                 return idmap_passdb_domain(NULL);
441         }
442
443         return idmap_find_domain(domname);
444 }
445
446 void idmap_close(void)
447 {
448         TALLOC_FREE(default_idmap_domain);
449         TALLOC_FREE(passdb_idmap_domain);
450         TALLOC_FREE(idmap_domains);
451         num_domains = 0;
452 }
453
454 /**************************************************************************
455  idmap allocator interface functions
456 **************************************************************************/
457
458 static NTSTATUS idmap_allocate_unixid(struct unixid *id)
459 {
460         struct idmap_domain *dom;
461         NTSTATUS ret;
462
463         dom = idmap_find_domain(NULL);
464
465         if (dom == NULL) {
466                 return NT_STATUS_UNSUCCESSFUL;
467         }
468
469         if (dom->methods->allocate_id == NULL) {
470                 return NT_STATUS_NOT_IMPLEMENTED;
471         }
472
473         ret = dom->methods->allocate_id(dom, id);
474
475         return ret;
476 }
477
478
479 NTSTATUS idmap_allocate_uid(struct unixid *id)
480 {
481         id->type = ID_TYPE_UID;
482         return idmap_allocate_unixid(id);
483 }
484
485 NTSTATUS idmap_allocate_gid(struct unixid *id)
486 {
487         id->type = ID_TYPE_GID;
488         return idmap_allocate_unixid(id);
489 }
490
491 NTSTATUS idmap_backends_unixid_to_sid(const char *domname, struct id_map *id)
492 {
493         struct idmap_domain *dom;
494         struct id_map *maps[2];
495
496          DEBUG(10, ("idmap_backend_unixid_to_sid: domain = '%s', xid = %d "
497                     "(type %d)\n",
498                     domname?domname:"NULL", id->xid.id, id->xid.type));
499
500         maps[0] = id;
501         maps[1] = NULL;
502
503         /*
504          * Always give passdb a chance first
505          */
506
507         dom = idmap_passdb_domain(NULL);
508         if ((dom != NULL)
509             && NT_STATUS_IS_OK(dom->methods->unixids_to_sids(dom, maps))
510             && id->status == ID_MAPPED) {
511                 return NT_STATUS_OK;
512         }
513
514         dom = idmap_find_domain(domname);
515         if (dom == NULL) {
516                 return NT_STATUS_NONE_MAPPED;
517         }
518
519         return dom->methods->unixids_to_sids(dom, maps);
520 }