Merge branch 'master' of ctdb into 'master' of samba
[sfrench/samba-autobuild/.git] / source4 / dsdb / samdb / ldb_modules / samba_dsdb.c
1 /*
2    Samba4 module loading module
3
4    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2009
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 /*
21  *  Name: ldb
22  *
23  *  Component: Samba4 module loading module
24  *
25  *  Description: Implement a single 'module' in the ldb database,
26  *  which loads the remaining modules based on 'choice of configuration' attributes
27  *
28  *  This is to avoid forcing a reprovision of the ldb databases when we change the internal structure of the code
29  *
30  *  Author: Andrew Bartlett
31  */
32
33 #include "includes.h"
34 #include <ldb.h>
35 #include <ldb_errors.h>
36 #include <ldb_module.h>
37 #include "dsdb/samdb/ldb_modules/util.h"
38 #include "dsdb/samdb/samdb.h"
39 #include "librpc/ndr/libndr.h"
40 #include "auth/credentials/credentials.h"
41 #include "param/secrets.h"
42 #include "lib/ldb-samba/ldb_wrap.h"
43
44 static int read_at_rootdse_record(struct ldb_context *ldb, struct ldb_module *module, TALLOC_CTX *mem_ctx,
45                                   struct ldb_message **msg, struct ldb_request *parent)
46 {
47         int ret;
48         static const char *rootdse_attrs[] = { "defaultNamingContext", "configurationNamingContext", "schemaNamingContext", NULL };
49         struct ldb_result *rootdse_res;
50         struct ldb_dn *rootdse_dn;
51         TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
52         if (!tmp_ctx) {
53                 return ldb_oom(ldb);
54         }
55
56         rootdse_dn = ldb_dn_new(tmp_ctx, ldb, "@ROOTDSE");
57         if (!rootdse_dn) {
58                 talloc_free(tmp_ctx);
59                 return ldb_oom(ldb);
60         }
61
62         ret = dsdb_module_search_dn(module, tmp_ctx, &rootdse_res, rootdse_dn,
63                                     rootdse_attrs, DSDB_FLAG_NEXT_MODULE, parent);
64         if (ret != LDB_SUCCESS) {
65                 talloc_free(tmp_ctx);
66                 return ret;
67         }
68
69         talloc_steal(mem_ctx, rootdse_res->msgs);
70         *msg = rootdse_res->msgs[0];
71
72         talloc_free(tmp_ctx);
73
74         return ret;
75 }
76
77 static int prepare_modules_line(struct ldb_context *ldb,
78                                 TALLOC_CTX *mem_ctx,
79                                 const struct ldb_message *rootdse_msg,
80                                 struct ldb_message *msg, const char *backend_attr,
81                                 const char *backend_mod, const char **backend_mod_list)
82 {
83         int ret;
84         const char **backend_full_list;
85         const char *backend_dn;
86         char *mod_list_string;
87         char *full_string;
88         TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
89         if (!tmp_ctx) {
90                 return ldb_oom(ldb);
91         }
92
93         if (backend_attr) {
94                 backend_dn = ldb_msg_find_attr_as_string(rootdse_msg, backend_attr, NULL);
95                 if (!backend_dn) {
96                         ldb_asprintf_errstring(ldb,
97                                                "samba_dsdb_init: "
98                                                "unable to read %s from %s:%s",
99                                                backend_attr, ldb_dn_get_linearized(rootdse_msg->dn),
100                                                ldb_errstring(ldb));
101                         return LDB_ERR_CONSTRAINT_VIOLATION;
102                 }
103         } else {
104                 backend_dn = "*";
105         }
106
107         if (backend_mod) {
108                 backend_full_list = (const char **)str_list_make_single(tmp_ctx, backend_mod);
109         } else {
110                 backend_full_list = (const char **)str_list_make_empty(tmp_ctx);
111         }
112         if (!backend_full_list) {
113                 talloc_free(tmp_ctx);
114                 return ldb_oom(ldb);
115         }
116
117         backend_full_list = str_list_append_const(backend_full_list, backend_mod_list);
118         if (!backend_full_list) {
119                 talloc_free(tmp_ctx);
120                 return ldb_oom(ldb);
121         }
122
123         mod_list_string = str_list_join(tmp_ctx, backend_full_list, ',');
124         if (!mod_list_string) {
125                 talloc_free(tmp_ctx);
126                 return ldb_oom(ldb);
127         }
128
129         full_string = talloc_asprintf(tmp_ctx, "%s:%s", backend_dn, mod_list_string);
130         ret = ldb_msg_add_steal_string(msg, "modules", full_string);
131         talloc_free(tmp_ctx);
132         return ret;
133 }
134
135 /*
136  * Force overwrite of the credentials with those
137  * specified in secrets.ldb, to connect across the
138  * ldapi socket to an LDAP backend
139  */
140
141 static int set_ldap_credentials(struct ldb_context *ldb, bool use_external)
142 {
143         const char *secrets_ldb_path, *sam_ldb_path;
144         char *private_dir, *p, *error_string;
145         struct ldb_context *secrets_ldb;
146         struct cli_credentials *cred;
147         struct loadparm_context *lp_ctx = ldb_get_opaque(ldb, "loadparm");
148         TALLOC_CTX *tmp_ctx = talloc_new(ldb);
149
150         if (!tmp_ctx) {
151                 return ldb_oom(ldb);
152         }
153
154         cred = cli_credentials_init(ldb);
155         if (!cred) {
156                 talloc_free(tmp_ctx);
157                 return ldb_oom(ldb);
158         }
159         cli_credentials_set_anonymous(cred);
160         if (use_external) {
161                 cli_credentials_set_forced_sasl_mech(cred, "EXTERNAL");
162         } else {
163                 cli_credentials_set_forced_sasl_mech(cred, "DIGEST-MD5");
164
165                 /*
166                  * We don't want to use krb5 to talk to our samdb - recursion
167                  * here would be bad, and this account isn't in the KDC
168                  * anyway
169                  */
170                 cli_credentials_set_kerberos_state(cred, CRED_DONT_USE_KERBEROS);
171
172                 /*
173                  * Work out where *our* secrets.ldb is.  It must be in
174                  * the same directory as sam.ldb
175                  */
176                 sam_ldb_path = (const char *)ldb_get_opaque(ldb, "ldb_url");
177                 if (!sam_ldb_path) {
178                         talloc_free(tmp_ctx);
179                         return ldb_operr(ldb);
180                 }
181                 if (strncmp("tdb://", sam_ldb_path, 6) == 0) {
182                         sam_ldb_path += 6;
183                 }
184                 private_dir = talloc_strdup(tmp_ctx, sam_ldb_path);
185                 p = strrchr(private_dir, '/');
186                 if (p) {
187                         *p = '\0';
188                 } else {
189                         private_dir = talloc_strdup(tmp_ctx, ".");
190                 }
191
192                 secrets_ldb_path = talloc_asprintf(private_dir, "tdb://%s/secrets.ldb",
193                                                    private_dir);
194
195                 if (!secrets_ldb_path) {
196                         talloc_free(tmp_ctx);
197                         return ldb_oom(ldb);
198                 }
199
200                 /*
201                  * Now that we have found the location, connect to
202                  * secrets.ldb so we can read the SamDB Credentials
203                  * record
204                  */
205                 secrets_ldb = ldb_wrap_connect(tmp_ctx, NULL, lp_ctx, secrets_ldb_path,
206                                                NULL, NULL, 0);
207
208                 if (!NT_STATUS_IS_OK(cli_credentials_set_secrets(cred, NULL, secrets_ldb, NULL,
209                                                                  SECRETS_LDAP_FILTER, &error_string))) {
210                         ldb_asprintf_errstring(ldb, "Failed to read LDAP backend password from %s", secrets_ldb_path);
211                         talloc_free(tmp_ctx);
212                         return LDB_ERR_STRONG_AUTH_REQUIRED;
213                 }
214         }
215
216         /*
217          * Finally overwrite any supplied credentials with
218          * these ones, as only secrets.ldb contains the magic
219          * credentials to talk on the ldapi socket
220          */
221         if (ldb_set_opaque(ldb, "credentials", cred)) {
222                 talloc_free(tmp_ctx);
223                 return ldb_operr(ldb);
224         }
225         talloc_free(tmp_ctx);
226         return LDB_SUCCESS;
227 }
228
229 static int samba_dsdb_init(struct ldb_module *module)
230 {
231         struct ldb_context *ldb = ldb_module_get_ctx(module);
232         int ret, len, i;
233         TALLOC_CTX *tmp_ctx = talloc_new(module);
234         struct ldb_result *res;
235         struct ldb_message *rootdse_msg, *partition_msg;
236         struct ldb_dn *samba_dsdb_dn, *partition_dn;
237         struct ldb_module *backend_module, *module_chain;
238         const char **final_module_list, **reverse_module_list;
239         /*
240           Add modules to the list to activate them by default
241           beware often order is important
242
243           Some Known ordering constraints:
244           - rootdse must be first, as it makes redirects from "" -> cn=rootdse
245           - extended_dn_in must be before objectclass.c, as it resolves the DN
246           - objectclass must be before password_hash and samldb since these LDB
247             modules require the expanded "objectClass" list
248           - objectclass must be before descriptor and acl, as both assume that
249             objectClass values are sorted
250           - objectclass_attrs must be behind operational in order to see all
251             attributes (the operational module protects and therefore
252             suppresses per default some important ones)
253           - partition must be last
254           - each partition has its own module list then
255
256           The list is presented here as a set of declarations to show the
257           stack visually - the code below then handles the creation of the list
258           based on the parameters loaded from the database.
259         */
260         static const char *modules_list1[] = {"resolve_oids",
261                                              "rootdse",
262                                              "schema_load",
263                                              "lazy_commit",
264                                              "dirsync",
265                                              "paged_results",
266                                              "ranged_results",
267                                              "anr",
268                                              "server_sort",
269                                              "asq",
270                                              "extended_dn_store",
271                                              NULL };
272         /* extended_dn_in or extended_dn_in_openldap goes here */
273         static const char *modules_list1a[] = {"objectclass",
274                                              "descriptor",
275                                              "acl",
276                                              "aclread",
277                                              "samldb",
278                                              "password_hash",
279                                              "operational",
280                                              "instancetype",
281                                              "objectclass_attrs",
282                                              NULL };
283
284         const char **link_modules;
285         static const char *fedora_ds_modules[] = {
286                 "rdn_name", NULL };
287         static const char *openldap_modules[] = {
288                 NULL };
289         static const char *tdb_modules_list[] = {
290                 "rdn_name",
291                 "subtree_delete",
292                 "repl_meta_data",
293                 "subtree_rename",
294                 "linked_attributes",
295                 NULL};
296
297         const char *extended_dn_module;
298         const char *extended_dn_module_ldb = "extended_dn_out_ldb";
299         const char *extended_dn_module_fds = "extended_dn_out_fds";
300         const char *extended_dn_module_openldap = "extended_dn_out_openldap";
301         const char *extended_dn_in_module = "extended_dn_in";
302
303         static const char *modules_list2[] = {"show_deleted",
304                                               "new_partition",
305                                               "partition",
306                                               NULL };
307
308         const char **backend_modules;
309         static const char *fedora_ds_backend_modules[] = {
310                 "nsuniqueid", "paged_searches", "simple_dn", NULL };
311         static const char *openldap_backend_modules[] = {
312                 "entryuuid", "simple_dn", NULL };
313
314         static const char *samba_dsdb_attrs[] = { "backendType", NULL };
315         static const char *partition_attrs[] = { "ldapBackend", NULL };
316         const char *backendType, *backendUrl;
317         bool use_sasl_external = false;
318
319         if (!tmp_ctx) {
320                 return ldb_oom(ldb);
321         }
322
323         ret = ldb_register_samba_handlers(ldb);
324         if (ret != LDB_SUCCESS) {
325                 talloc_free(tmp_ctx);
326                 return ret;
327         }
328
329         samba_dsdb_dn = ldb_dn_new(tmp_ctx, ldb, "@SAMBA_DSDB");
330         if (!samba_dsdb_dn) {
331                 talloc_free(tmp_ctx);
332                 return ldb_oom(ldb);
333         }
334
335         partition_dn = ldb_dn_new(tmp_ctx, ldb, DSDB_PARTITION_DN);
336         if (!partition_dn) {
337                 talloc_free(tmp_ctx);
338                 return ldb_oom(ldb);
339         }
340
341 #define CHECK_LDB_RET(check_ret)                                \
342         do {                                                    \
343                 if (check_ret != LDB_SUCCESS) {                 \
344                         talloc_free(tmp_ctx);                   \
345                         return check_ret;                       \
346                 }                                               \
347         } while (0)
348
349         ret = dsdb_module_search_dn(module, tmp_ctx, &res, samba_dsdb_dn,
350                                     samba_dsdb_attrs, DSDB_FLAG_NEXT_MODULE, NULL);
351         if (ret == LDB_ERR_NO_SUCH_OBJECT) {
352                 backendType = "ldb";
353         } else if (ret == LDB_SUCCESS) {
354                 backendType = ldb_msg_find_attr_as_string(res->msgs[0], "backendType", "ldb");
355         } else {
356                 talloc_free(tmp_ctx);
357                 return ret;
358         }
359
360         backend_modules = NULL;
361         if (strcasecmp(backendType, "ldb") == 0) {
362                 extended_dn_module = extended_dn_module_ldb;
363                 link_modules = tdb_modules_list;
364         } else {
365                 struct cli_credentials *cred;
366                 bool is_ldapi = false;
367
368                 ret = dsdb_module_search_dn(module, tmp_ctx, &res, partition_dn,
369                                             partition_attrs, DSDB_FLAG_NEXT_MODULE, NULL);
370                 if (ret == LDB_SUCCESS) {
371                         backendUrl = ldb_msg_find_attr_as_string(res->msgs[0], "ldapBackend", "ldapi://");
372                         if (!strncasecmp(backendUrl, "ldapi://", sizeof("ldapi://")-1)) {
373                                 is_ldapi = true;
374                         }
375                 } else if (ret != LDB_ERR_NO_SUCH_OBJECT) {
376                         talloc_free(tmp_ctx);
377                         return ret;
378                 }
379                 if (strcasecmp(backendType, "fedora-ds") == 0) {
380                         link_modules = fedora_ds_modules;
381                         backend_modules = fedora_ds_backend_modules;
382                         extended_dn_module = extended_dn_module_fds;
383                 } else if (strcasecmp(backendType, "openldap") == 0) {
384                         link_modules = openldap_modules;
385                         backend_modules = openldap_backend_modules;
386                         extended_dn_module = extended_dn_module_openldap;
387                         extended_dn_in_module = "extended_dn_in_openldap";
388                         if (is_ldapi) {
389                                 use_sasl_external = true;
390                         }
391                 } else {
392                         return ldb_error(ldb, LDB_ERR_OPERATIONS_ERROR, "invalid backend type");
393                 }
394                 ret = ldb_set_opaque(ldb, "readOnlySchema", (void*)1);
395                 if (ret != LDB_SUCCESS) {
396                         ldb_set_errstring(ldb, "Failed to set readOnlySchema opaque");
397                 }
398
399                 cred = ldb_get_opaque(ldb, "credentials");
400                 if (!cred || !cli_credentials_authentication_requested(cred)) {
401                         ret = set_ldap_credentials(ldb, use_sasl_external);
402                         if (ret != LDB_SUCCESS) {
403                                 return ret;
404                         }
405                 }
406         }
407
408 #define CHECK_MODULE_LIST \
409         do {                                                    \
410                 if (!final_module_list) {                       \
411                         talloc_free(tmp_ctx);                   \
412                         return ldb_oom(ldb);                    \
413                 }                                               \
414         } while (0)
415
416         final_module_list = str_list_copy_const(tmp_ctx, modules_list1);
417         CHECK_MODULE_LIST;
418
419         final_module_list = str_list_add_const(final_module_list, extended_dn_in_module);
420         CHECK_MODULE_LIST;
421
422         final_module_list = str_list_append_const(final_module_list, modules_list1a);
423         CHECK_MODULE_LIST;
424
425         final_module_list = str_list_append_const(final_module_list, link_modules);
426         CHECK_MODULE_LIST;
427
428         final_module_list = str_list_add_const(final_module_list, extended_dn_module);
429         CHECK_MODULE_LIST;
430
431         final_module_list = str_list_append_const(final_module_list, modules_list2);
432         CHECK_MODULE_LIST;
433
434
435         ret = read_at_rootdse_record(ldb, module, tmp_ctx, &rootdse_msg, NULL);
436         CHECK_LDB_RET(ret);
437
438         partition_msg = ldb_msg_new(tmp_ctx);
439         partition_msg->dn = ldb_dn_new(partition_msg, ldb, "@" DSDB_OPAQUE_PARTITION_MODULE_MSG_OPAQUE_NAME);
440
441         ret = prepare_modules_line(ldb, tmp_ctx,
442                                    rootdse_msg,
443                                    partition_msg, "schemaNamingContext",
444                                    "schema_data", backend_modules);
445         CHECK_LDB_RET(ret);
446
447         ret = prepare_modules_line(ldb, tmp_ctx,
448                                    rootdse_msg,
449                                    partition_msg, NULL,
450                                    NULL, backend_modules);
451         CHECK_LDB_RET(ret);
452
453         ret = ldb_set_opaque(ldb, DSDB_OPAQUE_PARTITION_MODULE_MSG_OPAQUE_NAME, partition_msg);
454         CHECK_LDB_RET(ret);
455
456         talloc_steal(ldb, partition_msg);
457
458         /* Now prepare the module chain.  Oddly, we must give it to ldb_load_modules_list in REVERSE */
459         for (len = 0; final_module_list[len]; len++) { /* noop */};
460
461         reverse_module_list = talloc_array(tmp_ctx, const char *, len+1);
462         if (!reverse_module_list) {
463                 talloc_free(tmp_ctx);
464                 return ldb_oom(ldb);
465         }
466         for (i=0; i < len; i++) {
467                 reverse_module_list[i] = final_module_list[(len - 1) - i];
468         }
469         reverse_module_list[i] = NULL;
470
471         /* The backend (at least until the partitions module
472          * reconfigures things) is the next module in the currently
473          * loaded chain */
474         backend_module = ldb_module_next(module);
475         ret = ldb_module_load_list(ldb, reverse_module_list, backend_module, &module_chain);
476         CHECK_LDB_RET(ret);
477
478         talloc_free(tmp_ctx);
479         /* Set this as the 'next' module, so that we effectivly append it to module chain */
480         ldb_module_set_next(module, module_chain);
481
482         return ldb_next_init(module);
483 }
484
485 static const struct ldb_module_ops ldb_samba_dsdb_module_ops = {
486         .name              = "samba_dsdb",
487         .init_context      = samba_dsdb_init,
488 };
489
490 int ldb_samba_dsdb_module_init(const char *version)
491 {
492         LDB_MODULE_CHECK_VERSION(version);
493         return ldb_register_module(&ldb_samba_dsdb_module_ops);
494 }