dsdb: Disable tombstone_reanimation module until we isolate what causes flaky tests
[Samba.git] / source4 / dsdb / samdb / ldb_modules / samba_dsdb.c
blob26c583ef58a16fd6d8ce6704352d9b73f50eb79e
1 /*
2 Samba4 module loading module
4 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2009
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.
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.
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/>.
21 * Name: ldb
23 * Component: Samba4 module loading module
25 * Description: Implement a single 'module' in the ldb database,
26 * which loads the remaining modules based on 'choice of configuration' attributes
28 * This is to avoid forcing a reprovision of the ldb databases when we change the internal structure of the code
30 * Author: Andrew Bartlett
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"
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)
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);
56 rootdse_dn = ldb_dn_new(tmp_ctx, ldb, "@ROOTDSE");
57 if (!rootdse_dn) {
58 talloc_free(tmp_ctx);
59 return ldb_oom(ldb);
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;
69 talloc_steal(mem_ctx, rootdse_res->msgs);
70 *msg = rootdse_res->msgs[0];
72 talloc_free(tmp_ctx);
74 return ret;
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)
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);
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;
103 } else {
104 backend_dn = "*";
107 if (backend_mod) {
108 char **b = str_list_make_single(tmp_ctx, backend_mod);
109 backend_full_list = discard_const_p(const char *, b);
110 } else {
111 char **b = str_list_make_empty(tmp_ctx);
112 backend_full_list = discard_const_p(const char *, b);
114 if (!backend_full_list) {
115 talloc_free(tmp_ctx);
116 return ldb_oom(ldb);
119 backend_full_list = str_list_append_const(backend_full_list, backend_mod_list);
120 if (!backend_full_list) {
121 talloc_free(tmp_ctx);
122 return ldb_oom(ldb);
125 mod_list_string = str_list_join(tmp_ctx, backend_full_list, ',');
126 if (!mod_list_string) {
127 talloc_free(tmp_ctx);
128 return ldb_oom(ldb);
131 full_string = talloc_asprintf(tmp_ctx, "%s:%s", backend_dn, mod_list_string);
132 ret = ldb_msg_add_steal_string(msg, "modules", full_string);
133 talloc_free(tmp_ctx);
134 return ret;
138 * Force overwrite of the credentials with those
139 * specified in secrets.ldb, to connect across the
140 * ldapi socket to an LDAP backend
143 static int set_ldap_credentials(struct ldb_context *ldb, bool use_external)
145 const char *secrets_ldb_path, *sam_ldb_path;
146 char *private_dir, *p, *error_string;
147 struct ldb_context *secrets_ldb;
148 struct cli_credentials *cred;
149 struct loadparm_context *lp_ctx = ldb_get_opaque(ldb, "loadparm");
150 TALLOC_CTX *tmp_ctx = talloc_new(ldb);
152 if (!tmp_ctx) {
153 return ldb_oom(ldb);
156 cred = cli_credentials_init(ldb);
157 if (!cred) {
158 talloc_free(tmp_ctx);
159 return ldb_oom(ldb);
161 cli_credentials_set_anonymous(cred);
162 if (use_external) {
163 cli_credentials_set_forced_sasl_mech(cred, "EXTERNAL");
164 } else {
165 cli_credentials_set_forced_sasl_mech(cred, "DIGEST-MD5");
168 * We don't want to use krb5 to talk to our samdb - recursion
169 * here would be bad, and this account isn't in the KDC
170 * anyway
172 cli_credentials_set_kerberos_state(cred, CRED_DONT_USE_KERBEROS);
175 * Work out where *our* secrets.ldb is. It must be in
176 * the same directory as sam.ldb
178 sam_ldb_path = (const char *)ldb_get_opaque(ldb, "ldb_url");
179 if (!sam_ldb_path) {
180 talloc_free(tmp_ctx);
181 return ldb_operr(ldb);
183 if (strncmp("tdb://", sam_ldb_path, 6) == 0) {
184 sam_ldb_path += 6;
186 private_dir = talloc_strdup(tmp_ctx, sam_ldb_path);
187 p = strrchr(private_dir, '/');
188 if (p) {
189 *p = '\0';
190 } else {
191 private_dir = talloc_strdup(tmp_ctx, ".");
194 secrets_ldb_path = talloc_asprintf(private_dir, "tdb://%s/secrets.ldb",
195 private_dir);
197 if (!secrets_ldb_path) {
198 talloc_free(tmp_ctx);
199 return ldb_oom(ldb);
203 * Now that we have found the location, connect to
204 * secrets.ldb so we can read the SamDB Credentials
205 * record
207 secrets_ldb = ldb_wrap_connect(tmp_ctx, NULL, lp_ctx, secrets_ldb_path,
208 NULL, NULL, 0);
210 if (!NT_STATUS_IS_OK(cli_credentials_set_secrets(cred, NULL, secrets_ldb, NULL,
211 SECRETS_LDAP_FILTER, &error_string))) {
212 ldb_asprintf_errstring(ldb, "Failed to read LDAP backend password from %s", secrets_ldb_path);
213 talloc_free(tmp_ctx);
214 return LDB_ERR_STRONG_AUTH_REQUIRED;
219 * Finally overwrite any supplied credentials with
220 * these ones, as only secrets.ldb contains the magic
221 * credentials to talk on the ldapi socket
223 if (ldb_set_opaque(ldb, "credentials", cred)) {
224 talloc_free(tmp_ctx);
225 return ldb_operr(ldb);
227 talloc_free(tmp_ctx);
228 return LDB_SUCCESS;
231 static int samba_dsdb_init(struct ldb_module *module)
233 struct ldb_context *ldb = ldb_module_get_ctx(module);
234 int ret, len, i;
235 TALLOC_CTX *tmp_ctx = talloc_new(module);
236 struct ldb_result *res;
237 struct ldb_message *rootdse_msg = NULL, *partition_msg;
238 struct ldb_dn *samba_dsdb_dn, *partition_dn;
239 struct ldb_module *backend_module, *module_chain;
240 const char **final_module_list, **reverse_module_list;
242 Add modules to the list to activate them by default
243 beware often order is important
245 Some Known ordering constraints:
246 - rootdse must be first, as it makes redirects from "" -> cn=rootdse
247 - extended_dn_in must be before objectclass.c, as it resolves the DN
248 - objectclass must be before password_hash and samldb since these LDB
249 modules require the expanded "objectClass" list
250 - objectclass must be before descriptor and acl, as both assume that
251 objectClass values are sorted
252 - objectclass_attrs must be behind operational in order to see all
253 attributes (the operational module protects and therefore
254 suppresses per default some important ones)
255 - partition must be last
256 - each partition has its own module list then
258 The list is presented here as a set of declarations to show the
259 stack visually - the code below then handles the creation of the list
260 based on the parameters loaded from the database.
262 static const char *modules_list1[] = {"resolve_oids",
263 "rootdse",
264 "schema_load",
265 "lazy_commit",
266 "dirsync",
267 "paged_results",
268 "ranged_results",
269 "anr",
270 "server_sort",
271 "asq",
272 "extended_dn_store",
273 NULL };
274 /* extended_dn_in or extended_dn_in_openldap goes here */
275 static const char *modules_list1a[] = {"objectclass",
276 "descriptor",
277 "acl",
278 "aclread",
279 "samldb",
280 "password_hash",
281 "operational",
282 "instancetype",
283 "objectclass_attrs",
284 NULL };
286 const char **link_modules;
287 static const char *fedora_ds_modules[] = {
288 "rdn_name", NULL };
289 static const char *openldap_modules[] = {
290 NULL };
291 static const char *tdb_modules_list[] = {
292 "rdn_name",
293 "subtree_delete",
294 "repl_meta_data",
295 "subtree_rename",
296 "linked_attributes",
297 NULL};
299 const char *extended_dn_module;
300 const char *extended_dn_module_ldb = "extended_dn_out_ldb";
301 const char *extended_dn_module_fds = "extended_dn_out_fds";
302 const char *extended_dn_module_openldap = "extended_dn_out_openldap";
303 const char *extended_dn_in_module = "extended_dn_in";
305 static const char *modules_list2[] = {"dns_notify",
306 "show_deleted",
307 "new_partition",
308 "partition",
309 NULL };
311 const char **backend_modules;
312 static const char *fedora_ds_backend_modules[] = {
313 "nsuniqueid", "paged_searches", "simple_dn", NULL };
314 static const char *openldap_backend_modules[] = {
315 "entryuuid", "simple_dn", NULL };
317 static const char *samba_dsdb_attrs[] = { "backendType", NULL };
318 static const char *partition_attrs[] = { "ldapBackend", NULL };
319 const char *backendType, *backendUrl;
320 bool use_sasl_external = false;
322 if (!tmp_ctx) {
323 return ldb_oom(ldb);
326 ret = ldb_register_samba_handlers(ldb);
327 if (ret != LDB_SUCCESS) {
328 talloc_free(tmp_ctx);
329 return ret;
332 samba_dsdb_dn = ldb_dn_new(tmp_ctx, ldb, "@SAMBA_DSDB");
333 if (!samba_dsdb_dn) {
334 talloc_free(tmp_ctx);
335 return ldb_oom(ldb);
338 partition_dn = ldb_dn_new(tmp_ctx, ldb, DSDB_PARTITION_DN);
339 if (!partition_dn) {
340 talloc_free(tmp_ctx);
341 return ldb_oom(ldb);
344 #define CHECK_LDB_RET(check_ret) \
345 do { \
346 if (check_ret != LDB_SUCCESS) { \
347 talloc_free(tmp_ctx); \
348 return check_ret; \
350 } while (0)
352 ret = dsdb_module_search_dn(module, tmp_ctx, &res, samba_dsdb_dn,
353 samba_dsdb_attrs, DSDB_FLAG_NEXT_MODULE, NULL);
354 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
355 backendType = "ldb";
356 } else if (ret == LDB_SUCCESS) {
357 backendType = ldb_msg_find_attr_as_string(res->msgs[0], "backendType", "ldb");
358 } else {
359 talloc_free(tmp_ctx);
360 return ret;
363 backend_modules = NULL;
364 if (strcasecmp(backendType, "ldb") == 0) {
365 extended_dn_module = extended_dn_module_ldb;
366 link_modules = tdb_modules_list;
367 } else {
368 struct cli_credentials *cred;
369 bool is_ldapi = false;
371 ret = dsdb_module_search_dn(module, tmp_ctx, &res, partition_dn,
372 partition_attrs, DSDB_FLAG_NEXT_MODULE, NULL);
373 if (ret == LDB_SUCCESS) {
374 backendUrl = ldb_msg_find_attr_as_string(res->msgs[0], "ldapBackend", "ldapi://");
375 if (!strncasecmp(backendUrl, "ldapi://", sizeof("ldapi://")-1)) {
376 is_ldapi = true;
378 } else if (ret != LDB_ERR_NO_SUCH_OBJECT) {
379 talloc_free(tmp_ctx);
380 return ret;
382 if (strcasecmp(backendType, "fedora-ds") == 0) {
383 link_modules = fedora_ds_modules;
384 backend_modules = fedora_ds_backend_modules;
385 extended_dn_module = extended_dn_module_fds;
386 } else if (strcasecmp(backendType, "openldap") == 0) {
387 link_modules = openldap_modules;
388 backend_modules = openldap_backend_modules;
389 extended_dn_module = extended_dn_module_openldap;
390 extended_dn_in_module = "extended_dn_in_openldap";
391 if (is_ldapi) {
392 use_sasl_external = true;
394 } else {
395 return ldb_error(ldb, LDB_ERR_OPERATIONS_ERROR, "invalid backend type");
397 ret = ldb_set_opaque(ldb, "readOnlySchema", (void*)1);
398 if (ret != LDB_SUCCESS) {
399 ldb_set_errstring(ldb, "Failed to set readOnlySchema opaque");
402 cred = ldb_get_opaque(ldb, "credentials");
403 if (!cred || !cli_credentials_authentication_requested(cred)) {
404 ret = set_ldap_credentials(ldb, use_sasl_external);
405 if (ret != LDB_SUCCESS) {
406 return ret;
411 #define CHECK_MODULE_LIST \
412 do { \
413 if (!final_module_list) { \
414 talloc_free(tmp_ctx); \
415 return ldb_oom(ldb); \
417 } while (0)
419 final_module_list = str_list_copy_const(tmp_ctx, modules_list1);
420 CHECK_MODULE_LIST;
422 final_module_list = str_list_add_const(final_module_list, extended_dn_in_module);
423 CHECK_MODULE_LIST;
425 final_module_list = str_list_append_const(final_module_list, modules_list1a);
426 CHECK_MODULE_LIST;
428 final_module_list = str_list_append_const(final_module_list, link_modules);
429 CHECK_MODULE_LIST;
431 final_module_list = str_list_add_const(final_module_list, extended_dn_module);
432 CHECK_MODULE_LIST;
434 final_module_list = str_list_append_const(final_module_list, modules_list2);
435 CHECK_MODULE_LIST;
438 ret = read_at_rootdse_record(ldb, module, tmp_ctx, &rootdse_msg, NULL);
439 CHECK_LDB_RET(ret);
441 partition_msg = ldb_msg_new(tmp_ctx);
442 partition_msg->dn = ldb_dn_new(partition_msg, ldb, "@" DSDB_OPAQUE_PARTITION_MODULE_MSG_OPAQUE_NAME);
444 ret = prepare_modules_line(ldb, tmp_ctx,
445 rootdse_msg,
446 partition_msg, "schemaNamingContext",
447 "schema_data", backend_modules);
448 CHECK_LDB_RET(ret);
450 ret = prepare_modules_line(ldb, tmp_ctx,
451 rootdse_msg,
452 partition_msg, NULL,
453 NULL, backend_modules);
454 CHECK_LDB_RET(ret);
456 ret = ldb_set_opaque(ldb, DSDB_OPAQUE_PARTITION_MODULE_MSG_OPAQUE_NAME, partition_msg);
457 CHECK_LDB_RET(ret);
459 talloc_steal(ldb, partition_msg);
461 /* Now prepare the module chain. Oddly, we must give it to ldb_load_modules_list in REVERSE */
462 for (len = 0; final_module_list[len]; len++) { /* noop */};
464 reverse_module_list = talloc_array(tmp_ctx, const char *, len+1);
465 if (!reverse_module_list) {
466 talloc_free(tmp_ctx);
467 return ldb_oom(ldb);
469 for (i=0; i < len; i++) {
470 reverse_module_list[i] = final_module_list[(len - 1) - i];
472 reverse_module_list[i] = NULL;
474 /* The backend (at least until the partitions module
475 * reconfigures things) is the next module in the currently
476 * loaded chain */
477 backend_module = ldb_module_next(module);
478 ret = ldb_module_load_list(ldb, reverse_module_list, backend_module, &module_chain);
479 CHECK_LDB_RET(ret);
481 talloc_free(tmp_ctx);
482 /* Set this as the 'next' module, so that we effectivly append it to module chain */
483 ldb_module_set_next(module, module_chain);
485 return ldb_next_init(module);
488 static const struct ldb_module_ops ldb_samba_dsdb_module_ops = {
489 .name = "samba_dsdb",
490 .init_context = samba_dsdb_init,
493 int ldb_samba_dsdb_module_init(const char *version)
495 LDB_MODULE_CHECK_VERSION(version);
496 return ldb_register_module(&ldb_samba_dsdb_module_ops);