s4-dsdb: give us an invocationID when in standalone mode
[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                 ldb_oom(ldb);
53                 return LDB_ERR_OPERATIONS_ERROR;
54         }
55
56         rootdse_dn = ldb_dn_new(tmp_ctx, ldb, "@ROOTDSE");
57         if (!rootdse_dn) {
58                 talloc_free(tmp_ctx);
59                 ldb_oom(ldb);
60                 return LDB_ERR_OPERATIONS_ERROR;
61         }
62
63         ret = dsdb_module_search_dn(module, tmp_ctx, &rootdse_res, rootdse_dn, rootdse_attrs, 0);
64         if (ret != LDB_SUCCESS) {
65                 talloc_free(tmp_ctx);
66                 return ret;
67         }
68
69         talloc_steal(mem_ctx, rootdse_res->msgs);
70         *msg = rootdse_res->msgs[0];
71
72         talloc_free(tmp_ctx);
73
74         return ret;
75 }
76
77 static int prepare_modules_line(struct ldb_context *ldb,
78                                 TALLOC_CTX *mem_ctx,
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)
82 {
83         int ret;
84         const char **backend_full_list;
85         const char *backend_dn;
86         char *mod_list_string;
87         char *full_string;
88         TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
89         if (!tmp_ctx) {
90                 ldb_oom(ldb);
91                 return LDB_ERR_OPERATIONS_ERROR;
92         }
93
94         if (backend_attr) {
95                 backend_dn = ldb_msg_find_attr_as_string(rootdse_msg, backend_attr, NULL);
96                 if (!backend_dn) {
97                         ldb_asprintf_errstring(ldb,
98                                                "samba_dsdb_init: "
99                                                "unable to read %s from %s:%s",
100                                                backend_attr, ldb_dn_get_linearized(rootdse_msg->dn),
101                                                ldb_errstring(ldb));
102                         return LDB_ERR_CONSTRAINT_VIOLATION;
103                 }
104         } else {
105                 backend_dn = "*";
106         }
107
108         if (backend_mod) {
109                 backend_full_list = (const char **)str_list_make_single(tmp_ctx, backend_mod);
110         } else {
111                 backend_full_list = (const char **)str_list_make_empty(tmp_ctx);
112         }
113         if (!backend_full_list) {
114                 talloc_free(tmp_ctx);
115                 ldb_oom(ldb);
116                 return LDB_ERR_OPERATIONS_ERROR;
117         }
118
119         backend_full_list = str_list_append_const(backend_full_list, backend_mod_list);
120         if (!backend_full_list) {
121                 talloc_free(tmp_ctx);
122                 ldb_oom(ldb);
123                 return LDB_ERR_OPERATIONS_ERROR;
124         }
125
126         mod_list_string = str_list_join(tmp_ctx, backend_full_list, ',');
127         if (!mod_list_string) {
128                 talloc_free(tmp_ctx);
129                 ldb_oom(ldb);
130                 return LDB_ERR_OPERATIONS_ERROR;
131         }
132
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);
136         return ret;
137 }
138
139
140
141 /*
142   initialise the invocationID for a standalone server
143  */
144 static int initialise_invocation_id(struct ldb_module *module, struct GUID *guid)
145 {
146         struct ldb_message *msg;
147         struct ldb_context *ldb = ldb_module_get_ctx(module);
148         int ret;
149
150         *guid = GUID_random();
151
152         msg = ldb_msg_new(module);
153         if (msg == NULL) {
154                 ldb_module_oom(module);
155                 return LDB_ERR_OPERATIONS_ERROR;
156         }
157         msg->dn = ldb_dn_new(msg, ldb, "@SAMBA_DSDB");
158         if (!msg->dn) {
159                 ldb_module_oom(module);
160                 talloc_free(msg);
161                 return LDB_ERR_OPERATIONS_ERROR;
162         }
163         ret = dsdb_msg_add_guid(msg, guid, "invocationID");
164         if (ret != LDB_SUCCESS) {
165                 ldb_module_oom(module);
166                 talloc_free(msg);
167                 return ret;
168         }
169         msg->elements[0].flags = LDB_FLAG_MOD_ADD;
170
171         ret = ldb_modify(ldb, msg);
172         if (ret != LDB_SUCCESS) {
173                 ldb_asprintf_errstring(ldb, "Failed to setup standalone invocationID - %s",
174                                        ldb_errstring(ldb));
175                 talloc_free(msg);
176                 return ret;
177         }
178
179         DEBUG(1,("Initialised standalone invocationID to %s\n",
180                  GUID_string(msg, guid)));
181
182         talloc_free(msg);
183
184         return LDB_SUCCESS;
185 }
186
187
188 static int samba_dsdb_init(struct ldb_module *module)
189 {
190         struct ldb_context *ldb = ldb_module_get_ctx(module);
191         int ret, len, i;
192         TALLOC_CTX *tmp_ctx = talloc_new(module);
193         struct ldb_result *res;
194         struct ldb_message *rootdse_msg, *partition_msg;
195         struct ldb_dn *samba_dsdb_dn;
196         struct ldb_module *backend_module, *module_chain;
197         const char **final_module_list, **reverse_module_list;
198         /*
199           Add modules to the list to activate them by default
200           beware often order is important
201
202           Some Known ordering constraints:
203           - rootdse must be first, as it makes redirects from "" -> cn=rootdse
204           - extended_dn_in must be before objectclass.c, as it resolves the DN
205           - objectclass must be before password_hash, because password_hash checks
206             that the objectclass is of type person (filled in by objectclass
207             module when expanding the objectclass list)
208           - partition must be last
209           - each partition has its own module list then
210
211           The list is presented here as a set of declarations to show the
212           stack visually - the code below then handles the creation of the list
213           based on the parameters loaded from the database.
214         */
215         static const char *modules_list[] = {"resolve_oids",
216                                              "rootdse",
217                                              "lazy_commit",
218                                              "paged_results",
219                                              "ranged_results",
220                                              "anr",
221                                              "server_sort",
222                                              "asq",
223                                              "extended_dn_store",
224                                              "extended_dn_in",
225                                              "rdn_name",
226                                              "objectclass",
227                                              "descriptor",
228                                              "acl",
229                                              "samldb",
230                                              "password_hash",
231                                              "operational",
232                                              "kludge_acl",
233                                              "schema_load",
234                                              "instancetype",
235                                              NULL };
236
237         const char *objectguid_module;
238         /* if serverrole == "domain controller": */
239         const char *repl_meta_data = "repl_meta_data";
240         /*    else: */
241         const char *objectguid = "objectguid";
242
243         const char **link_modules;
244         static const char *tdb_modules_list[] = {
245                 "subtree_rename",
246                 "subtree_delete",
247                 "linked_attributes",
248                 NULL};
249
250         const char *extended_dn_module;
251         const char *extended_dn_module_ldb = "extended_dn_out_ldb";
252         const char *extended_dn_module_fds = "extended_dn_out_fds";
253         const char *extended_dn_module_openldap = "extended_dn_out_openldap";
254
255         static const char *modules_list2[] = {"show_deleted",
256                                               "new_partition",
257                                               "partition",
258                                               NULL };
259
260         const char **backend_modules;
261         static const char *fedora_ds_backend_modules[] = {
262                 "nsuniqueid", "paged_searches", NULL };
263         static const char *openldap_backend_modules[] = {
264                 "entryuuid", "paged_searches", NULL };
265
266         static const char *samba_dsdb_attrs[] = { "backendType", "serverRole", "invocationID", NULL };
267         const char *backendType, *serverRole;
268
269         if (!tmp_ctx) {
270                 ldb_oom(ldb);
271                 return LDB_ERR_OPERATIONS_ERROR;
272         }
273
274         samba_dsdb_dn = ldb_dn_new(tmp_ctx, ldb, "@SAMBA_DSDB");
275         if (!samba_dsdb_dn) {
276                 talloc_free(tmp_ctx);
277                 ldb_oom(ldb);
278                 return LDB_ERR_OPERATIONS_ERROR;
279         }
280
281 #define CHECK_LDB_RET(check_ret)                                \
282         do {                                                    \
283                 if (check_ret != LDB_SUCCESS) {                 \
284                         talloc_free(tmp_ctx);                   \
285                         return check_ret;                       \
286                 }                                               \
287         } while (0)
288
289         ret = dsdb_module_search_dn(module, tmp_ctx, &res, samba_dsdb_dn, samba_dsdb_attrs, 0);
290         if (ret == LDB_ERR_NO_SUCH_OBJECT) {
291                 backendType = "ldb";
292                 serverRole = "domain controller";
293         } else if (ret == LDB_SUCCESS) {
294                 backendType = ldb_msg_find_attr_as_string(res->msgs[0], "backendType", "ldb");
295                 serverRole = ldb_msg_find_attr_as_string(res->msgs[0], "serverRole", "domain controller");
296         } else {
297                 talloc_free(tmp_ctx);
298                 return ret;
299         }
300
301         if (strcmp(serverRole, "standalone") == 0 ||
302             strcmp(serverRole, "member server") == 0) {
303                 struct GUID *guid;
304
305                 guid = talloc(module, struct GUID);
306                 if (!guid) {
307                         ldb_module_oom(module);
308                         return LDB_ERR_OPERATIONS_ERROR;
309                 }
310
311                 *guid = samdb_result_guid(res->msgs[0], "invocationID");
312                 if (GUID_all_zero(guid)) {
313                         ret = initialise_invocation_id(module, guid);
314                         if (ret != LDB_SUCCESS) {
315                                 talloc_free(tmp_ctx);
316                                 return ret;
317                         }
318                 }
319
320                 /* cache the domain_sid in the ldb. See the matching
321                  * code in samdb_ntds_invocation_id() */
322                 ret = ldb_set_opaque(ldb, "cache.invocation_id", guid);
323                 if (ret != LDB_SUCCESS) {
324                         talloc_free(tmp_ctx);
325                         return ret;
326                 }
327         }
328
329         backend_modules = NULL;
330         if (strcasecmp(backendType, "ldb") == 0) {
331                 if (strcasecmp(serverRole, "dc") == 0 || strcasecmp(serverRole, "domain controller") == 0) {
332                         objectguid_module = repl_meta_data;
333                 } else {
334                         objectguid_module = objectguid;
335                 }
336                 extended_dn_module = extended_dn_module_ldb;
337                 link_modules = tdb_modules_list;
338         } else {
339                 objectguid_module = NULL;
340                 link_modules = NULL;
341                 if (strcasecmp(backendType, "fedora-ds") == 0) {
342                         backend_modules = fedora_ds_backend_modules;
343                         extended_dn_module = extended_dn_module_fds;
344                 } else if (strcasecmp(backendType, "openldap") == 0) {
345                         backend_modules = openldap_backend_modules;
346                         extended_dn_module = extended_dn_module_openldap;
347                 }
348         }
349
350 #define CHECK_MODULE_LIST \
351         do {                                                    \
352                 if (!final_module_list) {                       \
353                         talloc_free(tmp_ctx);                   \
354                         ldb_oom(ldb);                           \
355                         return LDB_ERR_OPERATIONS_ERROR;        \
356                 }                                               \
357         } while (0)
358
359         final_module_list = str_list_copy_const(tmp_ctx, modules_list);
360         CHECK_MODULE_LIST;
361
362         final_module_list = str_list_add_const(final_module_list, objectguid_module);
363         CHECK_MODULE_LIST;
364
365         final_module_list = str_list_append_const(final_module_list, link_modules);
366         CHECK_MODULE_LIST;
367
368         final_module_list = str_list_add_const(final_module_list, extended_dn_module);
369         CHECK_MODULE_LIST;
370
371         final_module_list = str_list_append_const(final_module_list, modules_list2);
372         CHECK_MODULE_LIST;
373
374
375         ret = read_at_rootdse_record(ldb, module, tmp_ctx, &rootdse_msg);
376         CHECK_LDB_RET(ret);
377
378         partition_msg = ldb_msg_new(tmp_ctx);
379         partition_msg->dn = ldb_dn_new(partition_msg, ldb, "@" DSDB_OPAQUE_PARTITION_MODULE_MSG_OPAQUE_NAME);
380
381         ret = prepare_modules_line(ldb, tmp_ctx,
382                                    rootdse_msg,
383                                    partition_msg, "defaultNamingContext",
384                                    "pdc_fsmo", backend_modules);
385         CHECK_LDB_RET(ret);
386
387         ret = prepare_modules_line(ldb, tmp_ctx,
388                                    rootdse_msg,
389                                    partition_msg, "configurationNamingContext",
390                                    "naming_fsmo", backend_modules);
391         CHECK_LDB_RET(ret);
392
393         ret = prepare_modules_line(ldb, tmp_ctx,
394                                    rootdse_msg,
395                                    partition_msg, "schemaNamingContext",
396                                    "schema_data", backend_modules);
397         CHECK_LDB_RET(ret);
398
399         ret = prepare_modules_line(ldb, tmp_ctx,
400                                    rootdse_msg,
401                                    partition_msg, NULL,
402                                    NULL, backend_modules);
403         CHECK_LDB_RET(ret);
404
405         ret = ldb_set_opaque(ldb, DSDB_OPAQUE_PARTITION_MODULE_MSG_OPAQUE_NAME, partition_msg);
406         CHECK_LDB_RET(ret);
407
408         talloc_steal(ldb, partition_msg);
409
410         /* Now prepare the module chain.  Oddly, we must give it to ldb_load_modules_list in REVERSE */
411         for (len = 0; final_module_list[len]; len++) { /* noop */};
412
413         reverse_module_list = talloc_array(tmp_ctx, const char *, len+1);
414         if (!reverse_module_list) {
415                 talloc_free(tmp_ctx);
416                 ldb_oom(ldb);
417                 return LDB_ERR_OPERATIONS_ERROR;
418         }
419         for (i=0; i < len; i++) {
420                 reverse_module_list[i] = final_module_list[(len - 1) - i];
421         }
422         reverse_module_list[i] = NULL;
423
424         /* The backend (at least until the partitions module
425          * reconfigures things) is the next module in the currently
426          * loaded chain */
427         backend_module = module->next;
428         ret = ldb_load_modules_list(ldb, reverse_module_list, backend_module, &module_chain);
429         CHECK_LDB_RET(ret);
430
431         talloc_free(tmp_ctx);
432         /* Set this as the 'next' module, so that we effectivly append it to module chain */
433         module->next = module_chain;
434
435         return ldb_next_init(module);
436 }
437
438 const struct ldb_module_ops ldb_samba_dsdb_module_ops = {
439         .name              = "samba_dsdb",
440         .init_context      = samba_dsdb_init,
441 };