s4-dsdb: move schema_load at the top of module stack
[Samba/gbeck.git] / source4 / dsdb / samdb / ldb_modules / samba_dsdb.c
blob361059f0eea79f51c65f90cc330e18003a24d20a
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"
41 static int read_at_rootdse_record(struct ldb_context *ldb, struct ldb_module *module, TALLOC_CTX *mem_ctx,
42 struct ldb_message **msg, struct ldb_request *parent)
44 int ret;
45 static const char *rootdse_attrs[] = { "defaultNamingContext", "configurationNamingContext", "schemaNamingContext", NULL };
46 struct ldb_result *rootdse_res;
47 struct ldb_dn *rootdse_dn;
48 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
49 if (!tmp_ctx) {
50 return ldb_oom(ldb);
53 rootdse_dn = ldb_dn_new(tmp_ctx, ldb, "@ROOTDSE");
54 if (!rootdse_dn) {
55 talloc_free(tmp_ctx);
56 return ldb_oom(ldb);
59 ret = dsdb_module_search_dn(module, tmp_ctx, &rootdse_res, rootdse_dn,
60 rootdse_attrs, DSDB_FLAG_NEXT_MODULE, parent);
61 if (ret != LDB_SUCCESS) {
62 talloc_free(tmp_ctx);
63 return ret;
66 talloc_steal(mem_ctx, rootdse_res->msgs);
67 *msg = rootdse_res->msgs[0];
69 talloc_free(tmp_ctx);
71 return ret;
74 static int prepare_modules_line(struct ldb_context *ldb,
75 TALLOC_CTX *mem_ctx,
76 const struct ldb_message *rootdse_msg,
77 struct ldb_message *msg, const char *backend_attr,
78 const char *backend_mod, const char **backend_mod_list)
80 int ret;
81 const char **backend_full_list;
82 const char *backend_dn;
83 char *mod_list_string;
84 char *full_string;
85 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
86 if (!tmp_ctx) {
87 return ldb_oom(ldb);
90 if (backend_attr) {
91 backend_dn = ldb_msg_find_attr_as_string(rootdse_msg, backend_attr, NULL);
92 if (!backend_dn) {
93 ldb_asprintf_errstring(ldb,
94 "samba_dsdb_init: "
95 "unable to read %s from %s:%s",
96 backend_attr, ldb_dn_get_linearized(rootdse_msg->dn),
97 ldb_errstring(ldb));
98 return LDB_ERR_CONSTRAINT_VIOLATION;
100 } else {
101 backend_dn = "*";
104 if (backend_mod) {
105 backend_full_list = (const char **)str_list_make_single(tmp_ctx, backend_mod);
106 } else {
107 backend_full_list = (const char **)str_list_make_empty(tmp_ctx);
109 if (!backend_full_list) {
110 talloc_free(tmp_ctx);
111 return ldb_oom(ldb);
114 backend_full_list = str_list_append_const(backend_full_list, backend_mod_list);
115 if (!backend_full_list) {
116 talloc_free(tmp_ctx);
117 return ldb_oom(ldb);
120 mod_list_string = str_list_join(tmp_ctx, backend_full_list, ',');
121 if (!mod_list_string) {
122 talloc_free(tmp_ctx);
123 return ldb_oom(ldb);
126 full_string = talloc_asprintf(tmp_ctx, "%s:%s", backend_dn, mod_list_string);
127 ret = ldb_msg_add_steal_string(msg, "modules", full_string);
128 talloc_free(tmp_ctx);
129 return ret;
134 static int samba_dsdb_init(struct ldb_module *module)
136 struct ldb_context *ldb = ldb_module_get_ctx(module);
137 int ret, len, i;
138 TALLOC_CTX *tmp_ctx = talloc_new(module);
139 struct ldb_result *res;
140 struct ldb_message *rootdse_msg, *partition_msg;
141 struct ldb_dn *samba_dsdb_dn;
142 struct ldb_module *backend_module, *module_chain;
143 const char **final_module_list, **reverse_module_list;
145 Add modules to the list to activate them by default
146 beware often order is important
148 Some Known ordering constraints:
149 - rootdse must be first, as it makes redirects from "" -> cn=rootdse
150 - extended_dn_in must be before objectclass.c, as it resolves the DN
151 - objectclass must be before password_hash and samldb since these LDB
152 modules require the expanded "objectClass" list
153 - objectclass must be before descriptor, as descriptor assumes that
154 objectClass values are sorted
155 - objectclass_attrs must be behind operational in order to see all
156 attributes (the operational module protects and therefore
157 suppresses per default some important ones)
158 - partition must be last
159 - each partition has its own module list then
161 The list is presented here as a set of declarations to show the
162 stack visually - the code below then handles the creation of the list
163 based on the parameters loaded from the database.
165 static const char *modules_list[] = {"resolve_oids",
166 "rootdse",
167 "schema_load",
168 "lazy_commit",
169 "dirsync",
170 "paged_results",
171 "ranged_results",
172 "anr",
173 "server_sort",
174 "asq",
175 "extended_dn_store",
176 "extended_dn_in",
177 "objectclass",
178 "descriptor",
179 "acl",
180 "aclread",
181 "samldb",
182 "password_hash",
183 "operational",
184 "instancetype",
185 "objectclass_attrs",
186 NULL };
188 const char **link_modules;
189 static const char *fedora_ds_modules[] = {
190 "rdn_name", NULL };
191 static const char *openldap_modules[] = {
192 NULL };
193 static const char *tdb_modules_list[] = {
194 "rdn_name",
195 "subtree_delete",
196 "repl_meta_data",
197 "subtree_rename",
198 "linked_attributes",
199 NULL};
201 const char *extended_dn_module;
202 const char *extended_dn_module_ldb = "extended_dn_out_ldb";
203 const char *extended_dn_module_fds = "extended_dn_out_fds";
204 const char *extended_dn_module_openldap = "extended_dn_out_openldap";
206 static const char *modules_list2[] = {"show_deleted",
207 "new_partition",
208 "partition",
209 NULL };
211 const char **backend_modules;
212 static const char *fedora_ds_backend_modules[] = {
213 "nsuniqueid", "paged_searches", "simple_dn", NULL };
214 static const char *openldap_backend_modules[] = {
215 "entryuuid", "paged_searches", "simple_dn", NULL };
217 static const char *samba_dsdb_attrs[] = { "backendType", "serverRole", NULL };
218 const char *backendType, *serverRole;
220 if (!tmp_ctx) {
221 return ldb_oom(ldb);
224 ret = ldb_register_samba_handlers(ldb);
225 if (ret != LDB_SUCCESS) {
226 talloc_free(tmp_ctx);
227 return ret;
230 samba_dsdb_dn = ldb_dn_new(tmp_ctx, ldb, "@SAMBA_DSDB");
231 if (!samba_dsdb_dn) {
232 talloc_free(tmp_ctx);
233 return ldb_oom(ldb);
236 #define CHECK_LDB_RET(check_ret) \
237 do { \
238 if (check_ret != LDB_SUCCESS) { \
239 talloc_free(tmp_ctx); \
240 return check_ret; \
242 } while (0)
244 ret = dsdb_module_search_dn(module, tmp_ctx, &res, samba_dsdb_dn,
245 samba_dsdb_attrs, DSDB_FLAG_NEXT_MODULE, NULL);
246 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
247 backendType = "ldb";
248 serverRole = "domain controller";
249 } else if (ret == LDB_SUCCESS) {
250 backendType = ldb_msg_find_attr_as_string(res->msgs[0], "backendType", "ldb");
251 serverRole = ldb_msg_find_attr_as_string(res->msgs[0], "serverRole", "domain controller");
252 } else {
253 talloc_free(tmp_ctx);
254 return ret;
257 backend_modules = NULL;
258 if (strcasecmp(backendType, "ldb") == 0) {
259 extended_dn_module = extended_dn_module_ldb;
260 link_modules = tdb_modules_list;
261 } else {
262 if (strcasecmp(backendType, "fedora-ds") == 0) {
263 link_modules = fedora_ds_modules;
264 backend_modules = fedora_ds_backend_modules;
265 extended_dn_module = extended_dn_module_fds;
266 } else if (strcasecmp(backendType, "openldap") == 0) {
267 link_modules = openldap_modules;
268 backend_modules = openldap_backend_modules;
269 extended_dn_module = extended_dn_module_openldap;
270 } else {
271 return ldb_error(ldb, LDB_ERR_OPERATIONS_ERROR, "invalid backend type");
273 ret = ldb_set_opaque(ldb, "readOnlySchema", (void*)1);
274 if (ret != LDB_SUCCESS) {
275 ldb_set_errstring(ldb, "Failed to set readOnlySchema opaque");
279 #define CHECK_MODULE_LIST \
280 do { \
281 if (!final_module_list) { \
282 talloc_free(tmp_ctx); \
283 return ldb_oom(ldb); \
285 } while (0)
287 final_module_list = str_list_copy_const(tmp_ctx, modules_list);
288 CHECK_MODULE_LIST;
290 final_module_list = str_list_append_const(final_module_list, link_modules);
291 CHECK_MODULE_LIST;
293 final_module_list = str_list_add_const(final_module_list, extended_dn_module);
294 CHECK_MODULE_LIST;
296 final_module_list = str_list_append_const(final_module_list, modules_list2);
297 CHECK_MODULE_LIST;
300 ret = read_at_rootdse_record(ldb, module, tmp_ctx, &rootdse_msg, NULL);
301 CHECK_LDB_RET(ret);
303 partition_msg = ldb_msg_new(tmp_ctx);
304 partition_msg->dn = ldb_dn_new(partition_msg, ldb, "@" DSDB_OPAQUE_PARTITION_MODULE_MSG_OPAQUE_NAME);
306 ret = prepare_modules_line(ldb, tmp_ctx,
307 rootdse_msg,
308 partition_msg, "schemaNamingContext",
309 "schema_data", backend_modules);
310 CHECK_LDB_RET(ret);
312 ret = prepare_modules_line(ldb, tmp_ctx,
313 rootdse_msg,
314 partition_msg, NULL,
315 NULL, backend_modules);
316 CHECK_LDB_RET(ret);
318 ret = ldb_set_opaque(ldb, DSDB_OPAQUE_PARTITION_MODULE_MSG_OPAQUE_NAME, partition_msg);
319 CHECK_LDB_RET(ret);
321 talloc_steal(ldb, partition_msg);
323 /* Now prepare the module chain. Oddly, we must give it to ldb_load_modules_list in REVERSE */
324 for (len = 0; final_module_list[len]; len++) { /* noop */};
326 reverse_module_list = talloc_array(tmp_ctx, const char *, len+1);
327 if (!reverse_module_list) {
328 talloc_free(tmp_ctx);
329 return ldb_oom(ldb);
331 for (i=0; i < len; i++) {
332 reverse_module_list[i] = final_module_list[(len - 1) - i];
334 reverse_module_list[i] = NULL;
336 /* The backend (at least until the partitions module
337 * reconfigures things) is the next module in the currently
338 * loaded chain */
339 backend_module = ldb_module_next(module);
340 ret = ldb_module_load_list(ldb, reverse_module_list, backend_module, &module_chain);
341 CHECK_LDB_RET(ret);
343 talloc_free(tmp_ctx);
344 /* Set this as the 'next' module, so that we effectivly append it to module chain */
345 ldb_module_set_next(module, module_chain);
347 return ldb_next_init(module);
350 static const struct ldb_module_ops ldb_samba_dsdb_module_ops = {
351 .name = "samba_dsdb",
352 .init_context = samba_dsdb_init,
355 int ldb_samba_dsdb_module_init(const char *version)
357 LDB_MODULE_CHECK_VERSION(version);
358 return ldb_register_module(&ldb_samba_dsdb_module_ops);