s4-dsdb: use ldb_operr() in the dsdb code
[nivanova/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, rootdse_attrs, 0);
62         if (ret != LDB_SUCCESS) {
63                 talloc_free(tmp_ctx);
64                 return ret;
65         }
66
67         talloc_steal(mem_ctx, rootdse_res->msgs);
68         *msg = rootdse_res->msgs[0];
69
70         talloc_free(tmp_ctx);
71
72         return ret;
73 }
74
75 static int prepare_modules_line(struct ldb_context *ldb,
76                                 TALLOC_CTX *mem_ctx,
77                                 const struct ldb_message *rootdse_msg,
78                                 struct ldb_message *msg, const char *backend_attr,
79                                 const char *backend_mod, const char **backend_mod_list)
80 {
81         int ret;
82         const char **backend_full_list;
83         const char *backend_dn;
84         char *mod_list_string;
85         char *full_string;
86         TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
87         if (!tmp_ctx) {
88                 return ldb_oom(ldb);
89         }
90
91         if (backend_attr) {
92                 backend_dn = ldb_msg_find_attr_as_string(rootdse_msg, backend_attr, NULL);
93                 if (!backend_dn) {
94                         ldb_asprintf_errstring(ldb,
95                                                "samba_dsdb_init: "
96                                                "unable to read %s from %s:%s",
97                                                backend_attr, ldb_dn_get_linearized(rootdse_msg->dn),
98                                                ldb_errstring(ldb));
99                         return LDB_ERR_CONSTRAINT_VIOLATION;
100                 }
101         } else {
102                 backend_dn = "*";
103         }
104
105         if (backend_mod) {
106                 backend_full_list = (const char **)str_list_make_single(tmp_ctx, backend_mod);
107         } else {
108                 backend_full_list = (const char **)str_list_make_empty(tmp_ctx);
109         }
110         if (!backend_full_list) {
111                 talloc_free(tmp_ctx);
112                 return ldb_oom(ldb);
113         }
114
115         backend_full_list = str_list_append_const(backend_full_list, backend_mod_list);
116         if (!backend_full_list) {
117                 talloc_free(tmp_ctx);
118                 return ldb_oom(ldb);
119         }
120
121         mod_list_string = str_list_join(tmp_ctx, backend_full_list, ',');
122         if (!mod_list_string) {
123                 talloc_free(tmp_ctx);
124                 return ldb_oom(ldb);
125         }
126
127         full_string = talloc_asprintf(tmp_ctx, "%s:%s", backend_dn, mod_list_string);
128         ret = ldb_msg_add_steal_string(msg, "modules", full_string);
129         talloc_free(tmp_ctx);
130         return ret;
131 }
132
133
134
135 static int samba_dsdb_init(struct ldb_module *module)
136 {
137         struct ldb_context *ldb = ldb_module_get_ctx(module);
138         int ret, len, i;
139         TALLOC_CTX *tmp_ctx = talloc_new(module);
140         struct ldb_result *res;
141         struct ldb_message *rootdse_msg, *partition_msg;
142         struct ldb_dn *samba_dsdb_dn;
143         struct ldb_module *backend_module, *module_chain;
144         const char **final_module_list, **reverse_module_list;
145         /*
146           Add modules to the list to activate them by default
147           beware often order is important
148
149           Some Known ordering constraints:
150           - rootdse must be first, as it makes redirects from "" -> cn=rootdse
151           - extended_dn_in must be before objectclass.c, as it resolves the DN
152           - objectclass must be before password_hash and samldb since these LDB
153             modules require the expanded "objectClass" list
154           - objectclass_attrs must be behind operational in order to see all
155             attributes (the operational module protects and therefore
156             suppresses per default some important ones)
157           - partition must be last
158           - each partition has its own module list then
159
160           The list is presented here as a set of declarations to show the
161           stack visually - the code below then handles the creation of the list
162           based on the parameters loaded from the database.
163         */
164         static const char *modules_list[] = {"resolve_oids",
165                                              "rootdse",
166                                              "lazy_commit",
167                                              "paged_results",
168                                              "ranged_results",
169                                              "anr",
170                                              "server_sort",
171                                              "asq",
172                                              "extended_dn_store",
173                                              "extended_dn_in",
174                                              "objectclass",
175                                              "descriptor",
176                                              "acl",
177                                              "samldb",
178                                              "password_hash",
179                                              "operational",
180                                              "kludge_acl",
181                                              "schema_load",
182                                              "instancetype",
183                                              "objectclass_attrs",
184                                              NULL };
185
186         const char **link_modules;
187         static const char *fedora_ds_modules[] = {
188                 "rdn_name", NULL };
189         static const char *openldap_modules[] = {
190                 NULL };
191         static const char *tdb_modules_list[] = {
192                 "rdn_name",
193                 "subtree_delete",
194                 "repl_meta_data",
195                 "subtree_rename",
196                 "linked_attributes",
197                 NULL};
198
199         const char *extended_dn_module;
200         const char *extended_dn_module_ldb = "extended_dn_out_ldb";
201         const char *extended_dn_module_fds = "extended_dn_out_fds";
202         const char *extended_dn_module_openldap = "extended_dn_out_openldap";
203
204         static const char *modules_list2[] = {"show_deleted",
205                                               "new_partition",
206                                               "partition",
207                                               NULL };
208
209         const char **backend_modules;
210         static const char *fedora_ds_backend_modules[] = {
211                 "nsuniqueid", "paged_searches", NULL };
212         static const char *openldap_backend_modules[] = {
213                 "entryuuid", "paged_searches", NULL };
214
215         static const char *samba_dsdb_attrs[] = { "backendType", "serverRole", NULL };
216         const char *backendType, *serverRole;
217
218         if (!tmp_ctx) {
219                 return ldb_oom(ldb);
220         }
221
222         samba_dsdb_dn = ldb_dn_new(tmp_ctx, ldb, "@SAMBA_DSDB");
223         if (!samba_dsdb_dn) {
224                 talloc_free(tmp_ctx);
225                 return ldb_oom(ldb);
226         }
227
228 #define CHECK_LDB_RET(check_ret)                                \
229         do {                                                    \
230                 if (check_ret != LDB_SUCCESS) {                 \
231                         talloc_free(tmp_ctx);                   \
232                         return check_ret;                       \
233                 }                                               \
234         } while (0)
235
236         ret = dsdb_module_search_dn(module, tmp_ctx, &res, samba_dsdb_dn, samba_dsdb_attrs, 0);
237         if (ret == LDB_ERR_NO_SUCH_OBJECT) {
238                 backendType = "ldb";
239                 serverRole = "domain controller";
240         } else if (ret == LDB_SUCCESS) {
241                 backendType = ldb_msg_find_attr_as_string(res->msgs[0], "backendType", "ldb");
242                 serverRole = ldb_msg_find_attr_as_string(res->msgs[0], "serverRole", "domain controller");
243         } else {
244                 talloc_free(tmp_ctx);
245                 return ret;
246         }
247
248         backend_modules = NULL;
249         if (strcasecmp(backendType, "ldb") == 0) {
250                 extended_dn_module = extended_dn_module_ldb;
251                 link_modules = tdb_modules_list;
252         } else {
253                 if (strcasecmp(backendType, "fedora-ds") == 0) {
254                         link_modules = fedora_ds_modules;
255                         backend_modules = fedora_ds_backend_modules;
256                         extended_dn_module = extended_dn_module_fds;
257                 } else if (strcasecmp(backendType, "openldap") == 0) {
258                         link_modules = openldap_modules;
259                         backend_modules = openldap_backend_modules;
260                         extended_dn_module = extended_dn_module_openldap;
261                 }
262         }
263
264 #define CHECK_MODULE_LIST \
265         do {                                                    \
266                 if (!final_module_list) {                       \
267                         talloc_free(tmp_ctx);                   \
268                         return ldb_oom(ldb);                    \
269                 }                                               \
270         } while (0)
271
272         final_module_list = str_list_copy_const(tmp_ctx, modules_list);
273         CHECK_MODULE_LIST;
274
275         final_module_list = str_list_append_const(final_module_list, link_modules);
276         CHECK_MODULE_LIST;
277
278         final_module_list = str_list_add_const(final_module_list, extended_dn_module);
279         CHECK_MODULE_LIST;
280
281         final_module_list = str_list_append_const(final_module_list, modules_list2);
282         CHECK_MODULE_LIST;
283
284
285         ret = read_at_rootdse_record(ldb, module, tmp_ctx, &rootdse_msg);
286         CHECK_LDB_RET(ret);
287
288         partition_msg = ldb_msg_new(tmp_ctx);
289         partition_msg->dn = ldb_dn_new(partition_msg, ldb, "@" DSDB_OPAQUE_PARTITION_MODULE_MSG_OPAQUE_NAME);
290
291         ret = prepare_modules_line(ldb, tmp_ctx,
292                                    rootdse_msg,
293                                    partition_msg, "defaultNamingContext",
294                                    "pdc_fsmo", backend_modules);
295         CHECK_LDB_RET(ret);
296
297         ret = prepare_modules_line(ldb, tmp_ctx,
298                                    rootdse_msg,
299                                    partition_msg, "configurationNamingContext",
300                                    "naming_fsmo", backend_modules);
301         CHECK_LDB_RET(ret);
302
303         ret = prepare_modules_line(ldb, tmp_ctx,
304                                    rootdse_msg,
305                                    partition_msg, "schemaNamingContext",
306                                    "schema_data", backend_modules);
307         CHECK_LDB_RET(ret);
308
309         ret = prepare_modules_line(ldb, tmp_ctx,
310                                    rootdse_msg,
311                                    partition_msg, NULL,
312                                    NULL, backend_modules);
313         CHECK_LDB_RET(ret);
314
315         ret = ldb_set_opaque(ldb, DSDB_OPAQUE_PARTITION_MODULE_MSG_OPAQUE_NAME, partition_msg);
316         CHECK_LDB_RET(ret);
317
318         talloc_steal(ldb, partition_msg);
319
320         /* Now prepare the module chain.  Oddly, we must give it to ldb_load_modules_list in REVERSE */
321         for (len = 0; final_module_list[len]; len++) { /* noop */};
322
323         reverse_module_list = talloc_array(tmp_ctx, const char *, len+1);
324         if (!reverse_module_list) {
325                 talloc_free(tmp_ctx);
326                 return ldb_oom(ldb);
327         }
328         for (i=0; i < len; i++) {
329                 reverse_module_list[i] = final_module_list[(len - 1) - i];
330         }
331         reverse_module_list[i] = NULL;
332
333         /* The backend (at least until the partitions module
334          * reconfigures things) is the next module in the currently
335          * loaded chain */
336         backend_module = module->next;
337         ret = ldb_load_modules_list(ldb, reverse_module_list, backend_module, &module_chain);
338         CHECK_LDB_RET(ret);
339
340         talloc_free(tmp_ctx);
341         /* Set this as the 'next' module, so that we effectivly append it to module chain */
342         module->next = module_chain;
343
344         return ldb_next_init(module);
345 }
346
347 _PUBLIC_ const struct ldb_module_ops ldb_samba_dsdb_module_ops = {
348         .name              = "samba_dsdb",
349         .init_context      = samba_dsdb_init,
350 };