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