9536981393768ff27a15481f78eb8b7cfde04b36
[garming/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 "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"
38
39 #include "dsdb/samdb/ldb_modules/util.h"
40 #include "dsdb/samdb/samdb.h"
41 #include "librpc/ndr/libndr.h"
42
43 static int read_at_rootdse_record(struct ldb_context *ldb, struct ldb_module *module, TALLOC_CTX *mem_ctx,
44                                   struct ldb_message **msg)
45 {
46         int ret;
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);
51         if (!tmp_ctx) {
52                 return ldb_oom(ldb);
53         }
54
55         rootdse_dn = ldb_dn_new(tmp_ctx, ldb, "@ROOTDSE");
56         if (!rootdse_dn) {
57                 talloc_free(tmp_ctx);
58                 return ldb_oom(ldb);
59         }
60
61         ret = dsdb_module_search_dn(module, tmp_ctx, &rootdse_res, rootdse_dn,
62                                     rootdse_attrs, DSDB_FLAG_NEXT_MODULE);
63         if (ret != LDB_SUCCESS) {
64                 talloc_free(tmp_ctx);
65                 return ret;
66         }
67
68         talloc_steal(mem_ctx, rootdse_res->msgs);
69         *msg = rootdse_res->msgs[0];
70
71         talloc_free(tmp_ctx);
72
73         return ret;
74 }
75
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)
81 {
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                 return ldb_oom(ldb);
90         }
91
92         if (backend_attr) {
93                 backend_dn = ldb_msg_find_attr_as_string(rootdse_msg, backend_attr, NULL);
94                 if (!backend_dn) {
95                         ldb_asprintf_errstring(ldb,
96                                                "samba_dsdb_init: "
97                                                "unable to read %s from %s:%s",
98                                                backend_attr, ldb_dn_get_linearized(rootdse_msg->dn),
99                                                ldb_errstring(ldb));
100                         return LDB_ERR_CONSTRAINT_VIOLATION;
101                 }
102         } else {
103                 backend_dn = "*";
104         }
105
106         if (backend_mod) {
107                 backend_full_list = (const char **)str_list_make_single(tmp_ctx, backend_mod);
108         } else {
109                 backend_full_list = (const char **)str_list_make_empty(tmp_ctx);
110         }
111         if (!backend_full_list) {
112                 talloc_free(tmp_ctx);
113                 return ldb_oom(ldb);
114         }
115
116         backend_full_list = str_list_append_const(backend_full_list, backend_mod_list);
117         if (!backend_full_list) {
118                 talloc_free(tmp_ctx);
119                 return ldb_oom(ldb);
120         }
121
122         mod_list_string = str_list_join(tmp_ctx, backend_full_list, ',');
123         if (!mod_list_string) {
124                 talloc_free(tmp_ctx);
125                 return ldb_oom(ldb);
126         }
127
128         full_string = talloc_asprintf(tmp_ctx, "%s:%s", backend_dn, mod_list_string);
129         ret = ldb_msg_add_steal_string(msg, "modules", full_string);
130         talloc_free(tmp_ctx);
131         return ret;
132 }
133
134
135
136 static int samba_dsdb_init(struct ldb_module *module)
137 {
138         struct ldb_context *ldb = ldb_module_get_ctx(module);
139         int ret, len, i;
140         TALLOC_CTX *tmp_ctx = talloc_new(module);
141         struct ldb_result *res;
142         struct ldb_message *rootdse_msg, *partition_msg;
143         struct ldb_dn *samba_dsdb_dn;
144         struct ldb_module *backend_module, *module_chain;
145         const char **final_module_list, **reverse_module_list;
146         /*
147           Add modules to the list to activate them by default
148           beware often order is important
149
150           Some Known ordering constraints:
151           - rootdse must be first, as it makes redirects from "" -> cn=rootdse
152           - extended_dn_in must be before objectclass.c, as it resolves the DN
153           - objectclass must be before password_hash and samldb since these LDB
154             modules require the expanded "objectClass" list
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
160
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.
164         */
165         static const char *modules_list[] = {"resolve_oids",
166                                              "rootdse",
167                                              "aclread",
168                                              "lazy_commit",
169                                              "paged_results",
170                                              "ranged_results",
171                                              "anr",
172                                              "server_sort",
173                                              "asq",
174                                              "extended_dn_store",
175                                              "extended_dn_in",
176                                              "objectclass",
177                                              "descriptor",
178                                              "acl",
179                                              "samldb",
180                                              "password_hash",
181                                              "operational",
182                                              "schema_load",
183                                              "instancetype",
184                                              "objectclass_attrs",
185                                              NULL };
186
187         const char **link_modules;
188         static const char *fedora_ds_modules[] = {
189                 "rdn_name", NULL };
190         static const char *openldap_modules[] = {
191                 NULL };
192         static const char *tdb_modules_list[] = {
193                 "rdn_name",
194                 "subtree_delete",
195                 "repl_meta_data",
196                 "subtree_rename",
197                 "linked_attributes",
198                 NULL};
199
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";
204
205         static const char *modules_list2[] = {"show_deleted",
206                                               "new_partition",
207                                               "partition",
208                                               NULL };
209
210         const char **backend_modules;
211         static const char *fedora_ds_backend_modules[] = {
212                 "nsuniqueid", "paged_searches", "simple_dn", NULL };
213         static const char *openldap_backend_modules[] = {
214                 "entryuuid", "paged_searches", "simple_dn", NULL };
215
216         static const char *samba_dsdb_attrs[] = { "backendType", "serverRole", NULL };
217         const char *backendType, *serverRole;
218
219         if (!tmp_ctx) {
220                 return ldb_oom(ldb);
221         }
222
223         samba_dsdb_dn = ldb_dn_new(tmp_ctx, ldb, "@SAMBA_DSDB");
224         if (!samba_dsdb_dn) {
225                 talloc_free(tmp_ctx);
226                 return ldb_oom(ldb);
227         }
228
229 #define CHECK_LDB_RET(check_ret)                                \
230         do {                                                    \
231                 if (check_ret != LDB_SUCCESS) {                 \
232                         talloc_free(tmp_ctx);                   \
233                         return check_ret;                       \
234                 }                                               \
235         } while (0)
236
237         ret = dsdb_module_search_dn(module, tmp_ctx, &res, samba_dsdb_dn,
238                                     samba_dsdb_attrs, DSDB_FLAG_NEXT_MODULE);
239         if (ret == LDB_ERR_NO_SUCH_OBJECT) {
240                 backendType = "ldb";
241                 serverRole = "domain controller";
242         } else if (ret == LDB_SUCCESS) {
243                 backendType = ldb_msg_find_attr_as_string(res->msgs[0], "backendType", "ldb");
244                 serverRole = ldb_msg_find_attr_as_string(res->msgs[0], "serverRole", "domain controller");
245         } else {
246                 talloc_free(tmp_ctx);
247                 return ret;
248         }
249
250         backend_modules = NULL;
251         if (strcasecmp(backendType, "ldb") == 0) {
252                 extended_dn_module = extended_dn_module_ldb;
253                 link_modules = tdb_modules_list;
254         } else {
255                 if (strcasecmp(backendType, "fedora-ds") == 0) {
256                         link_modules = fedora_ds_modules;
257                         backend_modules = fedora_ds_backend_modules;
258                         extended_dn_module = extended_dn_module_fds;
259                 } else if (strcasecmp(backendType, "openldap") == 0) {
260                         link_modules = openldap_modules;
261                         backend_modules = openldap_backend_modules;
262                         extended_dn_module = extended_dn_module_openldap;
263                 } else {
264                         return ldb_error(ldb, LDB_ERR_OPERATIONS_ERROR, "invalid backend type");
265                 }
266                 ret = ldb_set_opaque(ldb, "readOnlySchema", (void*)1);
267                 if (ret != LDB_SUCCESS) {
268                         ldb_set_errstring(ldb, "Failed to set readOnlySchema opaque");
269                 }
270         }
271
272 #define CHECK_MODULE_LIST \
273         do {                                                    \
274                 if (!final_module_list) {                       \
275                         talloc_free(tmp_ctx);                   \
276                         return ldb_oom(ldb);                    \
277                 }                                               \
278         } while (0)
279
280         final_module_list = str_list_copy_const(tmp_ctx, modules_list);
281         CHECK_MODULE_LIST;
282
283         final_module_list = str_list_append_const(final_module_list, link_modules);
284         CHECK_MODULE_LIST;
285
286         final_module_list = str_list_add_const(final_module_list, extended_dn_module);
287         CHECK_MODULE_LIST;
288
289         final_module_list = str_list_append_const(final_module_list, modules_list2);
290         CHECK_MODULE_LIST;
291
292
293         ret = read_at_rootdse_record(ldb, module, tmp_ctx, &rootdse_msg);
294         CHECK_LDB_RET(ret);
295
296         partition_msg = ldb_msg_new(tmp_ctx);
297         partition_msg->dn = ldb_dn_new(partition_msg, ldb, "@" DSDB_OPAQUE_PARTITION_MODULE_MSG_OPAQUE_NAME);
298
299         ret = prepare_modules_line(ldb, tmp_ctx,
300                                    rootdse_msg,
301                                    partition_msg, "defaultNamingContext",
302                                    "pdc_fsmo", backend_modules);
303         CHECK_LDB_RET(ret);
304
305         ret = prepare_modules_line(ldb, tmp_ctx,
306                                    rootdse_msg,
307                                    partition_msg, "configurationNamingContext",
308                                    "naming_fsmo", backend_modules);
309         CHECK_LDB_RET(ret);
310
311         ret = prepare_modules_line(ldb, tmp_ctx,
312                                    rootdse_msg,
313                                    partition_msg, "schemaNamingContext",
314                                    "schema_data", backend_modules);
315         CHECK_LDB_RET(ret);
316
317         ret = prepare_modules_line(ldb, tmp_ctx,
318                                    rootdse_msg,
319                                    partition_msg, NULL,
320                                    NULL, backend_modules);
321         CHECK_LDB_RET(ret);
322
323         ret = ldb_set_opaque(ldb, DSDB_OPAQUE_PARTITION_MODULE_MSG_OPAQUE_NAME, partition_msg);
324         CHECK_LDB_RET(ret);
325
326         talloc_steal(ldb, partition_msg);
327
328         /* Now prepare the module chain.  Oddly, we must give it to ldb_load_modules_list in REVERSE */
329         for (len = 0; final_module_list[len]; len++) { /* noop */};
330
331         reverse_module_list = talloc_array(tmp_ctx, const char *, len+1);
332         if (!reverse_module_list) {
333                 talloc_free(tmp_ctx);
334                 return ldb_oom(ldb);
335         }
336         for (i=0; i < len; i++) {
337                 reverse_module_list[i] = final_module_list[(len - 1) - i];
338         }
339         reverse_module_list[i] = NULL;
340
341         /* The backend (at least until the partitions module
342          * reconfigures things) is the next module in the currently
343          * loaded chain */
344         backend_module = module->next;
345         ret = ldb_load_modules_list(ldb, reverse_module_list, backend_module, &module_chain);
346         CHECK_LDB_RET(ret);
347
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;
351
352         return ldb_next_init(module);
353 }
354
355 _PUBLIC_ const struct ldb_module_ops ldb_samba_dsdb_module_ops = {
356         .name              = "samba_dsdb",
357         .init_context      = samba_dsdb_init,
358 };