s4:dsdb Make samba_dsdb easier to use in upgrades - assume default values
[Samba.git] / source4 / dsdb / samdb / ldb_modules / samba_dsdb.c
blobee7e42ef9bc81b47b180a4e704ec2fa6d5350223
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 "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"
42 static int read_at_rootdse_record(struct ldb_context *ldb, struct ldb_module *module, TALLOC_CTX *mem_ctx,
43 struct ldb_message **msg)
45 int ret;
46 static const char *rootdse_attrs[] = { "defaultNamingContext", "configurationNamingContext", "schemaNamingContext", NULL };
47 struct ldb_result *rootdse_res;
48 struct ldb_dn *rootdse_dn;
49 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
50 if (!tmp_ctx) {
51 ldb_oom(ldb);
52 return LDB_ERR_OPERATIONS_ERROR;
55 rootdse_dn = ldb_dn_new(tmp_ctx, ldb, "@ROOTDSE");
56 if (!rootdse_dn) {
57 talloc_free(tmp_ctx);
58 ldb_oom(ldb);
59 return LDB_ERR_OPERATIONS_ERROR;
62 ret = dsdb_module_search_dn(module, tmp_ctx, &rootdse_res, rootdse_dn, rootdse_attrs, 0);
63 if (ret != LDB_SUCCESS) {
64 talloc_free(tmp_ctx);
65 return ret;
68 talloc_steal(mem_ctx, rootdse_res->msgs);
69 *msg = rootdse_res->msgs[0];
71 talloc_free(tmp_ctx);
73 return ret;
76 static int prepare_modules_line(struct ldb_context *ldb,
77 TALLOC_CTX *mem_ctx,
78 const struct ldb_message *rootdse_msg,
79 struct ldb_message *msg, const char *backend_attr,
80 const char *backend_mod, const char **backend_mod_list)
82 int ret;
83 const char **backend_full_list;
84 const char *backend_dn;
85 char *mod_list_string;
86 char *full_string;
87 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
88 if (!tmp_ctx) {
89 ldb_oom(ldb);
90 return LDB_ERR_OPERATIONS_ERROR;
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 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);
112 if (!backend_full_list) {
113 talloc_free(tmp_ctx);
114 ldb_oom(ldb);
115 return LDB_ERR_OPERATIONS_ERROR;
118 backend_full_list = str_list_append_const(backend_full_list, backend_mod_list);
119 if (!backend_full_list) {
120 talloc_free(tmp_ctx);
121 ldb_oom(ldb);
122 return LDB_ERR_OPERATIONS_ERROR;
125 mod_list_string = str_list_join(tmp_ctx, backend_full_list, ',');
126 if (!mod_list_string) {
127 talloc_free(tmp_ctx);
128 ldb_oom(ldb);
129 return LDB_ERR_OPERATIONS_ERROR;
132 full_string = talloc_asprintf(tmp_ctx, "%s:%s", backend_dn, mod_list_string);
133 ret = ldb_msg_add_steal_string(msg, "modules", full_string);
134 talloc_free(tmp_ctx);
135 return ret;
138 static int samba_dsdb_init(struct ldb_module *module)
140 struct ldb_context *ldb = ldb_module_get_ctx(module);
141 int ret, len, i;
142 TALLOC_CTX *tmp_ctx = talloc_new(module);
143 struct ldb_result *res;
144 struct ldb_message *rootdse_msg, *partition_msg;
145 struct ldb_dn *samba_dsdb_dn;
146 struct ldb_module *backend_module, *module_chain;
147 const char **final_module_list, **reverse_module_list;
149 Add modules to the list to activate them by default
150 beware often order is important
152 Some Known ordering constraints:
153 - rootdse must be first, as it makes redirects from "" -> cn=rootdse
154 - extended_dn_in must be before objectclass.c, as it resolves the DN
155 - objectclass must be before password_hash, because password_hash checks
156 that the objectclass is of type person (filled in by objectclass
157 module when expanding the objectclass list)
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 "lazy_commit",
168 "paged_results",
169 "ranged_results",
170 "anr",
171 "server_sort",
172 "asq",
173 "extended_dn_store",
174 "extended_dn_in",
175 "rdn_name",
176 "objectclass",
177 "descriptor",
178 "acl",
179 "samldb",
180 "password_hash",
181 "operational",
182 "kludge_acl",
183 "schema_load",
184 "instancetype",
185 NULL };
187 const char *objectguid_module;
188 /* if serverrole == "domain controller": */
189 const char *repl_meta_data = "repl_meta_data";
190 /* else: */
191 const char *objectguid = "objectguid";
193 const char **link_modules;
194 static const char *tdb_modules_list[] = {
195 "subtree_rename",
196 "subtree_delete",
197 "linked_attributes",
198 NULL};
200 const char *extended_dn_module;
201 const char *extended_dn_module_ldb = "extended_dn_out_ldb";
202 const char *extended_dn_module_fds = "extended_dn_out_fds";
203 const char *extended_dn_module_openldap = "extended_dn_out_openldap";
205 static const char *modules_list2[] = {"show_deleted",
206 "new_partition",
207 "partition",
208 NULL };
210 const char **backend_modules;
211 static const char *fedora_ds_backend_modules[] = {
212 "nsuniqueid", "paged_searches", NULL };
213 static const char *openldap_backend_modules[] = {
214 "entryuuid", "paged_searches", NULL };
216 static const char *samba_dsdb_attrs[] = { "backendType", "serverRole", NULL };
217 const char *backendType, *serverRole;
219 if (!tmp_ctx) {
220 ldb_oom(ldb);
221 return LDB_ERR_OPERATIONS_ERROR;
224 samba_dsdb_dn = ldb_dn_new(tmp_ctx, ldb, "@SAMBA_DSDB");
225 if (!samba_dsdb_dn) {
226 talloc_free(tmp_ctx);
227 ldb_oom(ldb);
228 return LDB_ERR_OPERATIONS_ERROR;
231 #define CHECK_LDB_RET(check_ret) \
232 do { \
233 if (check_ret != LDB_SUCCESS) { \
234 talloc_free(tmp_ctx); \
235 return check_ret; \
237 } while (0)
239 ret = dsdb_module_search_dn(module, tmp_ctx, &res, samba_dsdb_dn, samba_dsdb_attrs, 0);
240 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
241 backendType = "ldb";
242 serverRole = "domain controller";
243 } else if (ret == LDB_SUCCESS) {
244 backendType = ldb_msg_find_attr_as_string(res->msgs[0], "backendType", "ldb");
245 serverRole = ldb_msg_find_attr_as_string(res->msgs[0], "serverRole", "domain controller");
246 } else {
247 talloc_free(tmp_ctx);
248 return ret;
251 backend_modules = NULL;
252 if (strcasecmp(backendType, "ldb") == 0) {
253 if (strcasecmp(serverRole, "dc") == 0 || strcasecmp(serverRole, "domain controller") == 0) {
254 objectguid_module = repl_meta_data;
255 } else {
256 objectguid_module = objectguid;
258 extended_dn_module = extended_dn_module_ldb;
259 link_modules = tdb_modules_list;
260 } else {
261 objectguid_module = NULL;
262 link_modules = NULL;
263 if (strcasecmp(backendType, "fedora-ds") == 0) {
264 backend_modules = fedora_ds_backend_modules;
265 extended_dn_module = extended_dn_module_fds;
266 } else if (strcasecmp(backendType, "openldap") == 0) {
267 backend_modules = openldap_backend_modules;
268 extended_dn_module = extended_dn_module_openldap;
272 #define CHECK_MODULE_LIST \
273 do { \
274 if (!final_module_list) { \
275 talloc_free(tmp_ctx); \
276 ldb_oom(ldb); \
277 return LDB_ERR_OPERATIONS_ERROR; \
279 } while (0)
281 final_module_list = str_list_copy_const(tmp_ctx, modules_list);
282 CHECK_MODULE_LIST;
284 final_module_list = str_list_add_const(final_module_list, objectguid_module);
285 CHECK_MODULE_LIST;
287 final_module_list = str_list_append_const(final_module_list, link_modules);
288 CHECK_MODULE_LIST;
290 final_module_list = str_list_add_const(final_module_list, extended_dn_module);
291 CHECK_MODULE_LIST;
293 final_module_list = str_list_append_const(final_module_list, modules_list2);
294 CHECK_MODULE_LIST;
297 ret = read_at_rootdse_record(ldb, module, tmp_ctx, &rootdse_msg);
298 CHECK_LDB_RET(ret);
300 partition_msg = ldb_msg_new(tmp_ctx);
301 partition_msg->dn = ldb_dn_new(partition_msg, ldb, "@" DSDB_OPAQUE_PARTITION_MODULE_MSG_OPAQUE_NAME);
303 ret = prepare_modules_line(ldb, tmp_ctx,
304 rootdse_msg,
305 partition_msg, "defaultNamingContext",
306 "pdc_fsmo", backend_modules);
307 CHECK_LDB_RET(ret);
309 ret = prepare_modules_line(ldb, tmp_ctx,
310 rootdse_msg,
311 partition_msg, "configurationNamingContext",
312 "naming_fsmo", backend_modules);
313 CHECK_LDB_RET(ret);
315 ret = prepare_modules_line(ldb, tmp_ctx,
316 rootdse_msg,
317 partition_msg, "schemaNamingContext",
318 "schema_data", backend_modules);
319 CHECK_LDB_RET(ret);
321 ret = prepare_modules_line(ldb, tmp_ctx,
322 rootdse_msg,
323 partition_msg, NULL,
324 NULL, backend_modules);
325 CHECK_LDB_RET(ret);
327 ret = ldb_set_opaque(ldb, DSDB_OPAQUE_PARTITION_MODULE_MSG_OPAQUE_NAME, partition_msg);
328 CHECK_LDB_RET(ret);
330 talloc_steal(ldb, partition_msg);
332 /* Now prepare the module chain. Oddly, we must give it to ldb_load_modules_list in REVERSE */
333 for (len = 0; final_module_list[len]; len++) { /* noop */};
335 reverse_module_list = talloc_array(tmp_ctx, const char *, len+1);
336 if (!reverse_module_list) {
337 talloc_free(tmp_ctx);
338 ldb_oom(ldb);
339 return LDB_ERR_OPERATIONS_ERROR;
341 for (i=0; i < len; i++) {
342 reverse_module_list[i] = final_module_list[(len - 1) - i];
344 reverse_module_list[i] = NULL;
346 /* The backend (at least until the partitions module
347 * reconfigures things) is the next module in the currently
348 * loaded chain */
349 backend_module = module->next;
350 ret = ldb_load_modules_list(ldb, reverse_module_list, backend_module, &module_chain);
351 CHECK_LDB_RET(ret);
353 talloc_free(tmp_ctx);
354 /* Set this as the 'next' module, so that we effectivly append it to module chain */
355 module->next = module_chain;
357 return ldb_next_init(module);
360 const struct ldb_module_ops ldb_samba_dsdb_module_ops = {
361 .name = "samba_dsdb",
362 .init_context = samba_dsdb_init,