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/>.
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
34 #include "lib/ldb/include/ldb.h"
35 #include "lib/ldb/include/ldb_errors.h"
36 #include "lib/ldb/include/ldb_module.h"
37 #include "lib/ldb/include/ldb_private.h"
39 #include "dsdb/samdb/ldb_modules/util.h"
40 #include "dsdb/samdb/samdb.h"
41 #include "librpc/ndr/libndr.h"
43 static int read_at_rootdse_record(struct ldb_context *ldb, struct ldb_module *module, TALLOC_CTX *mem_ctx,
44 struct ldb_message **msg)
47 static const char *rootdse_attrs[] = { "defaultNamingContext", "configurationNamingContext", "schemaNamingContext", NULL };
48 struct ldb_result *rootdse_res;
49 struct ldb_dn *rootdse_dn;
50 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
53 return LDB_ERR_OPERATIONS_ERROR;
56 rootdse_dn = ldb_dn_new(tmp_ctx, ldb, "@ROOTDSE");
60 return LDB_ERR_OPERATIONS_ERROR;
63 ret = dsdb_module_search_dn(module, tmp_ctx, &rootdse_res, rootdse_dn, rootdse_attrs, 0);
64 if (ret != LDB_SUCCESS) {
69 talloc_steal(mem_ctx, rootdse_res->msgs);
70 *msg = rootdse_res->msgs[0];
77 static int prepare_modules_line(struct ldb_context *ldb,
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)
84 const char **backend_full_list;
85 const char *backend_dn;
86 char *mod_list_string;
88 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
91 return LDB_ERR_OPERATIONS_ERROR;
95 backend_dn = ldb_msg_find_attr_as_string(rootdse_msg, backend_attr, NULL);
97 ldb_asprintf_errstring(ldb,
99 "unable to read %s from %s:%s",
100 backend_attr, ldb_dn_get_linearized(rootdse_msg->dn),
102 return LDB_ERR_CONSTRAINT_VIOLATION;
109 backend_full_list = (const char **)str_list_make_single(tmp_ctx, backend_mod);
111 backend_full_list = (const char **)str_list_make_empty(tmp_ctx);
113 if (!backend_full_list) {
114 talloc_free(tmp_ctx);
116 return LDB_ERR_OPERATIONS_ERROR;
119 backend_full_list = str_list_append_const(backend_full_list, backend_mod_list);
120 if (!backend_full_list) {
121 talloc_free(tmp_ctx);
123 return LDB_ERR_OPERATIONS_ERROR;
126 mod_list_string = str_list_join(tmp_ctx, backend_full_list, ',');
127 if (!mod_list_string) {
128 talloc_free(tmp_ctx);
130 return LDB_ERR_OPERATIONS_ERROR;
133 full_string = talloc_asprintf(tmp_ctx, "%s:%s", backend_dn, mod_list_string);
134 ret = ldb_msg_add_steal_string(msg, "modules", full_string);
135 talloc_free(tmp_ctx);
141 static int samba_dsdb_init(struct ldb_module *module)
143 struct ldb_context *ldb = ldb_module_get_ctx(module);
145 TALLOC_CTX *tmp_ctx = talloc_new(module);
146 struct ldb_result *res;
147 struct ldb_message *rootdse_msg, *partition_msg;
148 struct ldb_dn *samba_dsdb_dn;
149 struct ldb_module *backend_module, *module_chain;
150 const char **final_module_list, **reverse_module_list;
152 Add modules to the list to activate them by default
153 beware often order is important
155 Some Known ordering constraints:
156 - rootdse must be first, as it makes redirects from "" -> cn=rootdse
157 - extended_dn_in must be before objectclass.c, as it resolves the DN
158 - objectclass must be before password_hash, because password_hash checks
159 that the objectclass is of type person (filled in by objectclass
160 module when expanding the objectclass list)
161 - partition must be last
162 - each partition has its own module list then
164 The list is presented here as a set of declarations to show the
165 stack visually - the code below then handles the creation of the list
166 based on the parameters loaded from the database.
168 static const char *modules_list[] = {"resolve_oids",
190 const char **link_modules;
191 static const char *fedora_ds_modules[] = {
193 static const char *openldap_modules[] = {
195 static const char *tdb_modules_list[] = {
203 const char *extended_dn_module;
204 const char *extended_dn_module_ldb = "extended_dn_out_ldb";
205 const char *extended_dn_module_fds = "extended_dn_out_fds";
206 const char *extended_dn_module_openldap = "extended_dn_out_openldap";
208 static const char *modules_list2[] = {"show_deleted",
213 const char **backend_modules;
214 static const char *fedora_ds_backend_modules[] = {
215 "nsuniqueid", "paged_searches", NULL };
216 static const char *openldap_backend_modules[] = {
217 "entryuuid", "paged_searches", NULL };
219 static const char *samba_dsdb_attrs[] = { "backendType", "serverRole", NULL };
220 const char *backendType, *serverRole;
224 return LDB_ERR_OPERATIONS_ERROR;
227 samba_dsdb_dn = ldb_dn_new(tmp_ctx, ldb, "@SAMBA_DSDB");
228 if (!samba_dsdb_dn) {
229 talloc_free(tmp_ctx);
231 return LDB_ERR_OPERATIONS_ERROR;
234 #define CHECK_LDB_RET(check_ret) \
236 if (check_ret != LDB_SUCCESS) { \
237 talloc_free(tmp_ctx); \
242 ret = dsdb_module_search_dn(module, tmp_ctx, &res, samba_dsdb_dn, samba_dsdb_attrs, 0);
243 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
245 serverRole = "domain controller";
246 } else if (ret == LDB_SUCCESS) {
247 backendType = ldb_msg_find_attr_as_string(res->msgs[0], "backendType", "ldb");
248 serverRole = ldb_msg_find_attr_as_string(res->msgs[0], "serverRole", "domain controller");
250 talloc_free(tmp_ctx);
254 backend_modules = NULL;
255 if (strcasecmp(backendType, "ldb") == 0) {
256 extended_dn_module = extended_dn_module_ldb;
257 link_modules = tdb_modules_list;
259 if (strcasecmp(backendType, "fedora-ds") == 0) {
260 link_modules = fedora_ds_modules;
261 backend_modules = fedora_ds_backend_modules;
262 extended_dn_module = extended_dn_module_fds;
263 } else if (strcasecmp(backendType, "openldap") == 0) {
264 link_modules = openldap_modules;
265 backend_modules = openldap_backend_modules;
266 extended_dn_module = extended_dn_module_openldap;
270 #define CHECK_MODULE_LIST \
272 if (!final_module_list) { \
273 talloc_free(tmp_ctx); \
275 return LDB_ERR_OPERATIONS_ERROR; \
279 final_module_list = str_list_copy_const(tmp_ctx, modules_list);
282 final_module_list = str_list_append_const(final_module_list, link_modules);
285 final_module_list = str_list_add_const(final_module_list, extended_dn_module);
288 final_module_list = str_list_append_const(final_module_list, modules_list2);
292 ret = read_at_rootdse_record(ldb, module, tmp_ctx, &rootdse_msg);
295 partition_msg = ldb_msg_new(tmp_ctx);
296 partition_msg->dn = ldb_dn_new(partition_msg, ldb, "@" DSDB_OPAQUE_PARTITION_MODULE_MSG_OPAQUE_NAME);
298 ret = prepare_modules_line(ldb, tmp_ctx,
300 partition_msg, "defaultNamingContext",
301 "pdc_fsmo", backend_modules);
304 ret = prepare_modules_line(ldb, tmp_ctx,
306 partition_msg, "configurationNamingContext",
307 "naming_fsmo", backend_modules);
310 ret = prepare_modules_line(ldb, tmp_ctx,
312 partition_msg, "schemaNamingContext",
313 "schema_data", backend_modules);
316 ret = prepare_modules_line(ldb, tmp_ctx,
319 NULL, backend_modules);
322 ret = ldb_set_opaque(ldb, DSDB_OPAQUE_PARTITION_MODULE_MSG_OPAQUE_NAME, partition_msg);
325 talloc_steal(ldb, partition_msg);
327 /* Now prepare the module chain. Oddly, we must give it to ldb_load_modules_list in REVERSE */
328 for (len = 0; final_module_list[len]; len++) { /* noop */};
330 reverse_module_list = talloc_array(tmp_ctx, const char *, len+1);
331 if (!reverse_module_list) {
332 talloc_free(tmp_ctx);
334 return LDB_ERR_OPERATIONS_ERROR;
336 for (i=0; i < len; i++) {
337 reverse_module_list[i] = final_module_list[(len - 1) - i];
339 reverse_module_list[i] = NULL;
341 /* The backend (at least until the partitions module
342 * reconfigures things) is the next module in the currently
344 backend_module = module->next;
345 ret = ldb_load_modules_list(ldb, reverse_module_list, backend_module, &module_chain);
348 talloc_free(tmp_ctx);
349 /* Set this as the 'next' module, so that we effectivly append it to module chain */
350 module->next = module_chain;
352 return ldb_next_init(module);
355 const struct ldb_module_ops ldb_samba_dsdb_module_ops = {
356 .name = "samba_dsdb",
357 .init_context = samba_dsdb_init,