4 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2006
5 Copyright (C) Stefan Metzmacher <metze@samba.org> 2007
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 * Component: ldb partitions module
26 * Description: Implement LDAP partitions
28 * Author: Andrew Bartlett
29 * Author: Stefan Metzmacher
32 #include "dsdb/samdb/ldb_modules/partition.h"
33 static int partition_sort_compare(const void *v1, const void *v2)
35 const struct dsdb_partition *p1;
36 const struct dsdb_partition *p2;
38 p1 = *((struct dsdb_partition * const*)v1);
39 p2 = *((struct dsdb_partition * const*)v2);
41 return ldb_dn_compare(p1->ctrl->dn, p2->ctrl->dn);
44 /* Load the list of DNs that we must replicate to all partitions */
45 static int partition_load_replicate_dns(struct ldb_context *ldb, struct partition_private_data *data, struct ldb_message *msg)
47 struct ldb_message_element *replicate_attributes = ldb_msg_find_element(msg, "replicateEntries");
49 talloc_free(data->replicate);
50 if (!replicate_attributes) {
51 data->replicate = NULL;
54 data->replicate = talloc_array(data, struct ldb_dn *, replicate_attributes->num_values + 1);
55 if (!data->replicate) {
56 return LDB_ERR_OPERATIONS_ERROR;
59 for (i=0; i < replicate_attributes->num_values; i++) {
60 data->replicate[i] = ldb_dn_from_ldb_val(data->replicate, ldb, &replicate_attributes->values[i]);
61 if (!ldb_dn_validate(data->replicate[i])) {
62 ldb_asprintf_errstring(ldb,
64 "invalid DN in partition replicate record: %s",
65 replicate_attributes->values[i].data);
66 return LDB_ERR_CONSTRAINT_VIOLATION;
69 data->replicate[i] = NULL;
74 /* Load the list of modules for the partitions */
75 static int partition_load_modules(struct ldb_context *ldb,
76 struct partition_private_data *data, struct ldb_message *msg)
79 struct ldb_message_element *modules_attributes = ldb_msg_find_element(msg, "modules");
80 talloc_free(data->modules);
81 if (!modules_attributes) {
85 data->modules = talloc_array(data, struct partition_module *, modules_attributes->num_values + 1);
88 return LDB_ERR_OPERATIONS_ERROR;
91 for (i=0; i < modules_attributes->num_values; i++) {
95 data->modules[i] = talloc(data->modules, struct partition_module);
96 if (!data->modules[i]) {
98 return LDB_ERR_OPERATIONS_ERROR;
101 base = talloc_strdup(data->partitions, (char *)modules_attributes->values[i].data);
102 p = strchr(base, ':');
104 ldb_asprintf_errstring(ldb,
105 "partition_load_modules: "
106 "invalid form for partition module record (missing ':'): %s", base);
107 return LDB_ERR_CONSTRAINT_VIOLATION;
111 data->modules[i]->modules = ldb_modules_list_from_string(ldb, data->modules[i],
114 if (strcmp(base, "*") == 0) {
115 data->modules[i]->dn = NULL;
117 data->modules[i]->dn = ldb_dn_new(data->modules[i], ldb, base);
118 if (!data->modules[i]->dn || !ldb_dn_validate(data->modules[i]->dn)) {
119 return LDB_ERR_OPERATIONS_ERROR;
126 static int partition_reload_metadata(struct ldb_module *module, struct partition_private_data *data, TALLOC_CTX *mem_ctx, struct ldb_message **_msg)
129 struct ldb_message *msg;
130 struct ldb_result *res;
131 struct ldb_context *ldb = ldb_module_get_ctx(module);
132 const char *attrs[] = { "partition", "replicateEntries", "modules", NULL };
133 /* perform search for @PARTITION, looking for module, replicateEntries and ldapBackend */
134 ret = dsdb_module_search_dn(module, mem_ctx, &res,
135 ldb_dn_new(mem_ctx, ldb, DSDB_PARTITION_DN),
137 if (ret != LDB_SUCCESS) {
143 ret = partition_load_replicate_dns(ldb, data, msg);
144 if (ret != LDB_SUCCESS) {
148 ret = partition_load_modules(ldb, data, msg);
149 if (ret != LDB_SUCCESS) {
153 data->ldapBackend = talloc_steal(data, ldb_msg_find_attr_as_string(msg, "ldapBackend", NULL));
162 static const char **find_modules_for_dn(struct partition_private_data *data, struct ldb_dn *dn)
165 struct partition_module *default_mod = NULL;
166 for (i=0; data->modules && data->modules[i]; i++) {
167 if (!data->modules[i]->dn) {
168 default_mod = data->modules[i];
169 } else if (ldb_dn_compare(dn, data->modules[i]->dn) == 0) {
170 return data->modules[i]->modules;
174 return default_mod->modules;
180 static int new_partition_from_dn(struct ldb_context *ldb, struct partition_private_data *data,
183 struct dsdb_partition **partition) {
184 const char *backend_name;
185 const char *full_backend;
186 struct dsdb_control_current_partition *ctrl;
187 struct ldb_module *module;
188 const char **modules;
191 (*partition) = talloc(mem_ctx, struct dsdb_partition);
193 return LDB_ERR_OPERATIONS_ERROR;
196 (*partition)->ctrl = ctrl = talloc((*partition), struct dsdb_control_current_partition);
198 talloc_free(*partition);
200 return LDB_ERR_OPERATIONS_ERROR;
203 /* See if an LDAP backend has been specified */
204 if (data->ldapBackend) {
205 backend_name = data->ldapBackend;
208 /* the backend LDB is the DN (base64 encoded if not 'plain') followed by .ldb */
210 const char *backend_dn = ldb_dn_get_casefold(dn);
211 char *base64_dn = NULL;
212 for (p = backend_dn; *p; p++) {
213 /* We have such a strict check because I don't want shell metacharacters in the file name, nor ../ */
214 if (!(isalnum(*p) || *p == ' ' || *p == '=' || *p == ',')) {
219 backend_dn = base64_dn = ldb_base64_encode(data, backend_dn, strlen(backend_dn));
222 backend_name = talloc_asprintf(data, "%s.ldb", backend_dn);
224 talloc_free(base64_dn);
228 ctrl->version = DSDB_CONTROL_CURRENT_PARTITION_VERSION;
229 ctrl->dn = talloc_steal(ctrl, dn);
231 full_backend = samdb_relative_path(ldb,
235 ldb_asprintf_errstring(ldb_module_get_ctx(module),
236 "partition_init: unable to determine an relative path for partition: %s", backend_name);
237 talloc_free(*partition);
238 return LDB_ERR_OPERATIONS_ERROR;
241 ret = ldb_connect_backend(ldb, full_backend, NULL, &module);
242 if (ret != LDB_SUCCESS) {
246 modules = find_modules_for_dn(data, dn);
248 ret = ldb_load_modules_list(ldb, modules, module, &(*partition)->module);
249 if (ret != LDB_SUCCESS) {
250 ldb_asprintf_errstring(ldb,
252 "loading backend for %s failed: %s",
253 ldb_dn_get_linearized(dn), ldb_errstring(ldb));
254 talloc_free(*partition);
257 ret = ldb_init_module_chain(ldb, (*partition)->module);
258 if (ret != LDB_SUCCESS) {
259 ldb_asprintf_errstring(ldb,
261 "initialising backend for %s failed: %s",
262 ldb_dn_get_linearized(dn), ldb_errstring(ldb));
263 talloc_free(*partition);
267 talloc_steal((*partition), (*partition)->module);
272 static int partition_register(struct ldb_context *ldb, struct dsdb_control_current_partition *ctrl, TALLOC_CTX *mem_ctx)
274 struct ldb_request *req;
277 req = talloc_zero(mem_ctx, struct ldb_request);
280 return LDB_ERR_OPERATIONS_ERROR;
283 req->operation = LDB_REQ_REGISTER_PARTITION;
284 req->op.reg_partition.dn = ctrl->dn;
285 req->callback = ldb_op_default_callback;
287 ldb_set_timeout(ldb, req, 0);
289 req->handle = ldb_handle_new(req, ldb);
290 if (req->handle == NULL) {
291 return LDB_ERR_OPERATIONS_ERROR;
294 ret = ldb_request(ldb, req);
295 if (ret == LDB_SUCCESS) {
296 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
298 if (ret != LDB_SUCCESS) {
299 ldb_debug(ldb, LDB_DEBUG_ERROR, "partition: Unable to register partition with rootdse!\n");
300 talloc_free(mem_ctx);
301 return LDB_ERR_OTHER;
308 int partition_create(struct ldb_module *module, struct ldb_request *req)
311 struct ldb_context *ldb = ldb_module_get_ctx(module);
312 struct ldb_request *mod_req, *last_req;
313 struct ldb_message *mod_msg;
314 struct partition_private_data *data;
315 struct dsdb_partition *partition;
317 /* Check if this is already a partition */
319 struct dsdb_create_partition_exop *ex_op = talloc_get_type(req->op.extended.data, struct dsdb_create_partition_exop);
320 struct ldb_dn *dn = ex_op->new_dn;
322 data = talloc_get_type(module->private_data, struct partition_private_data);
324 return LDB_ERR_OPERATIONS_ERROR;
328 /* We are not going to create a partition before we are even set up */
329 return LDB_ERR_UNWILLING_TO_PERFORM;
332 for (i=0; data->partitions && data->partitions[i]; i++) {
333 if (ldb_dn_compare(data->partitions[i]->ctrl->dn, dn) == 0) {
334 return LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS;
338 mod_msg = ldb_msg_new(req);
341 return LDB_ERR_OPERATIONS_ERROR;
344 mod_msg->dn = ldb_dn_new(mod_msg, ldb, DSDB_PARTITION_DN);
345 ret = ldb_msg_add_empty(mod_msg, DSDB_PARTITION_ATTR, LDB_FLAG_MOD_ADD, NULL);
346 if (ret != LDB_SUCCESS) {
349 ret = ldb_msg_add_string(mod_msg, DSDB_PARTITION_ATTR, ldb_dn_get_casefold(dn));
350 if (ret != LDB_SUCCESS) {
354 /* Perform modify on @PARTITION record */
355 ret = ldb_build_mod_req(&mod_req, ldb, req, mod_msg, NULL, NULL,
356 ldb_op_default_callback, req);
358 if (ret != LDB_SUCCESS) {
362 ret = ldb_next_request(module, mod_req);
363 if (ret == LDB_SUCCESS) {
364 ret = ldb_wait(mod_req->handle, LDB_WAIT_ALL);
367 if (ret != LDB_SUCCESS) {
371 ret = partition_reload_metadata(module, data, req, NULL);
372 if (ret != LDB_SUCCESS) {
376 /* Make a partition structure for this new partition, so we can copy in the template structure */
377 ret = new_partition_from_dn(ldb, data, req, ldb_dn_copy(req, dn), &partition);
378 if (ret != LDB_SUCCESS) {
382 /* Start a transaction on the DB (as it won't be in one being brand new) */
384 struct ldb_module *next = partition->module;
385 PARTITION_FIND_OP(next, start_transaction);
387 ret = next->ops->start_transaction(next);
388 if (ret != LDB_SUCCESS) {
393 /* otherwise, for each replicate, copy from main partition. If we get an error, we report it up the chain */
395 for (i=0; data->replicate && data->replicate[i]; i++) {
396 struct ldb_result *replicate_res;
397 struct ldb_request *add_req;
398 ret = dsdb_module_search_dn(module, req, &replicate_res,
401 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
404 if (ret != LDB_SUCCESS) {
405 ldb_asprintf_errstring(ldb,
406 "Failed to search for %s from " DSDB_PARTITION_DN
407 " replicateEntries for new partition at %s: %s",
408 ldb_dn_get_linearized(data->replicate[i]),
409 ldb_dn_get_linearized(partition->ctrl->dn),
414 /* Build add request */
415 ret = ldb_build_add_req(&add_req, ldb, replicate_res, replicate_res->msgs[0], NULL, NULL,
416 ldb_op_default_callback, last_req);
418 if (ret != LDB_SUCCESS) {
419 /* return directly, this is a very unlikely error */
423 ret = ldb_next_request(partition->module, add_req);
425 if (ret == LDB_SUCCESS) {
426 ret = ldb_wait(add_req->handle, LDB_WAIT_ALL);
433 case LDB_ERR_ENTRY_ALREADY_EXISTS:
434 /* Handle this case specially - if the
435 * metadata already exists, replace it */
437 struct ldb_request *del_req;
439 /* Don't leave a confusing string in the ldb_errstring() */
440 ldb_reset_err_string(ldb);
441 /* Build del request */
442 ret = ldb_build_del_req(&del_req, ldb, replicate_res, replicate_res->msgs[0]->dn, NULL, NULL,
443 ldb_op_default_callback, last_req);
445 if (ret != LDB_SUCCESS) {
446 /* return directly, this is a very unlikely error */
450 ret = ldb_next_request(partition->module, del_req);
453 if (ret == LDB_SUCCESS) {
454 ret = ldb_wait(del_req->handle, LDB_WAIT_ALL);
456 if (ret != LDB_SUCCESS) {
457 ldb_asprintf_errstring(ldb,
458 "Failed to delete (for re-add) %s from " DSDB_PARTITION_DN
459 " replicateEntries in new partition at %s: %s",
460 ldb_dn_get_linearized(data->replicate[i]),
461 ldb_dn_get_linearized(partition->ctrl->dn),
466 /* Build add request */
467 ret = ldb_build_add_req(&add_req, ldb, replicate_res, replicate_res->msgs[0], NULL, NULL,
468 ldb_op_default_callback, last_req);
470 if (ret != LDB_SUCCESS) {
471 /* return directly, this is a very unlikely error */
475 /* do the add again */
476 ret = ldb_next_request(partition->module, add_req);
479 if (ret == LDB_SUCCESS) {
480 ret = ldb_wait(add_req->handle, LDB_WAIT_ALL);
483 if (ret != LDB_SUCCESS) {
484 ldb_asprintf_errstring(ldb,
485 "Failed to add (after delete) %s from " DSDB_PARTITION_DN
486 " replicateEntries to new partition at %s: %s",
487 ldb_dn_get_linearized(data->replicate[i]),
488 ldb_dn_get_linearized(partition->ctrl->dn),
496 ldb_asprintf_errstring(ldb,
497 "Failed to add %s from " DSDB_PARTITION_DN
498 " replicateEntries to new partition at %s: %s",
499 ldb_dn_get_linearized(data->replicate[i]),
500 ldb_dn_get_linearized(partition->ctrl->dn),
506 /* And around again, for the next thing we must merge */
509 /* Count the partitions */
510 for (i=0; data->partitions && data->partitions[i]; i++) { /* noop */};
512 /* Add partition to list of partitions */
513 data->partitions = talloc_realloc(data, data->partitions, struct dsdb_partition *, i + 2);
514 if (!data->partitions) {
516 return LDB_ERR_OPERATIONS_ERROR;
518 data->partitions[i] = talloc_steal(data->partitions, partition);
519 data->partitions[i+1] = NULL;
521 /* Sort again (should use binary insert) */
522 qsort(data->partitions, i+1,
523 sizeof(*data->partitions), partition_sort_compare);
525 ret = partition_register(ldb, partition->ctrl, req);
526 if (ret != LDB_SUCCESS) {
530 /* send request done */
531 return ldb_module_done(req, NULL, NULL, LDB_SUCCESS);
535 int partition_init(struct ldb_module *module)
538 TALLOC_CTX *mem_ctx = talloc_new(module);
539 struct ldb_context *ldb = ldb_module_get_ctx(module);
540 struct ldb_message *msg;
541 struct ldb_message_element *partition_attributes;
543 struct partition_private_data *data;
546 return LDB_ERR_OPERATIONS_ERROR;
549 data = talloc_zero(mem_ctx, struct partition_private_data);
551 return LDB_ERR_OPERATIONS_ERROR;
554 ret = partition_reload_metadata(module, data, mem_ctx, &msg);
555 if (ret != LDB_SUCCESS) {
559 partition_attributes = ldb_msg_find_element(msg, "partition");
560 if (!partition_attributes) {
561 data->partitions = NULL;
563 data->partitions = talloc_array(data, struct dsdb_partition *, partition_attributes->num_values + 1);
564 if (!data->partitions) {
565 ldb_oom(ldb_module_get_ctx(module));
566 talloc_free(mem_ctx);
567 return LDB_ERR_OPERATIONS_ERROR;
570 for (i=0; partition_attributes && i < partition_attributes->num_values; i++) {
571 struct ldb_dn *dn = ldb_dn_from_ldb_val(mem_ctx, ldb, &partition_attributes->values[i]);
573 ldb_asprintf_errstring(ldb_module_get_ctx(module),
574 "partition_init: invalid DN in partition record: %s", (const char *)partition_attributes->values[i].data);
575 talloc_free(mem_ctx);
576 return LDB_ERR_CONSTRAINT_VIOLATION;
579 ret = new_partition_from_dn(ldb, data, data->partitions, dn, &data->partitions[i]);
580 if (ret != LDB_SUCCESS) {
581 talloc_free(mem_ctx);
586 if (data->partitions) {
587 data->partitions[i] = NULL;
589 /* sort these into order, most to least specific */
590 qsort(data->partitions, partition_attributes->num_values,
591 sizeof(*data->partitions), partition_sort_compare);
594 for (i=0; data->partitions && data->partitions[i]; i++) {
595 ret = partition_register(ldb, data->partitions[i]->ctrl, mem_ctx);
596 if (ret != LDB_SUCCESS) {
601 ret = ldb_mod_register_control(module, LDB_CONTROL_DOMAIN_SCOPE_OID);
602 if (ret != LDB_SUCCESS) {
603 ldb_debug(ldb_module_get_ctx(module), LDB_DEBUG_ERROR,
604 "partition: Unable to register control with rootdse!\n");
605 return LDB_ERR_OPERATIONS_ERROR;
608 ret = ldb_mod_register_control(module, LDB_CONTROL_SEARCH_OPTIONS_OID);
609 if (ret != LDB_SUCCESS) {
610 ldb_debug(ldb_module_get_ctx(module), LDB_DEBUG_ERROR,
611 "partition: Unable to register control with rootdse!\n");
612 return LDB_ERR_OPERATIONS_ERROR;
615 module->private_data = talloc_steal(module, data);
617 talloc_free(mem_ctx);
618 return ldb_next_init(module);