5 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2006
6 Copyright (C) Stefan Metzmacher <metze@samba.org> 2007
8 * NOTICE: this module is NOT released under the GNU LGPL license as
9 * other ldb code. This module is release under the GNU GPL v2 or
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 3 of the License, or
15 (at your option) any later version.
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program. If not, see <http://www.gnu.org/licenses/>.
29 * Component: ldb partitions module
31 * Description: Implement LDAP partitions
33 * Author: Andrew Bartlett
34 * Author: Stefan Metzmacher
38 #include "ldb/include/ldb_includes.h"
39 #include "dsdb/samdb/samdb.h"
41 struct partition_private_data {
42 struct dsdb_control_current_partition **partitions;
43 struct ldb_dn **replicate;
46 struct partition_context {
47 struct ldb_module *module;
48 struct ldb_handle *handle;
49 struct ldb_request *orig_req;
51 struct ldb_request **down_req;
53 int finished_requests;
56 static struct partition_context *partition_init_handle(struct ldb_request *req, struct ldb_module *module)
58 struct partition_context *ac;
61 h = talloc_zero(req, struct ldb_handle);
63 ldb_set_errstring(module->ldb, "Out of Memory");
69 ac = talloc_zero(h, struct partition_context);
71 ldb_set_errstring(module->ldb, "Out of Memory");
87 static struct ldb_module *make_module_for_next_request(TALLOC_CTX *mem_ctx,
88 struct ldb_context *ldb,
89 struct ldb_module *module)
91 struct ldb_module *current;
92 static const struct ldb_module_ops ops; /* zero */
93 current = talloc_zero(mem_ctx, struct ldb_module);
94 if (current == NULL) {
100 current->prev = NULL;
101 current->next = module;
105 static struct dsdb_control_current_partition *find_partition(struct partition_private_data *data,
110 /* Look at base DN */
111 /* Figure out which partition it is under */
112 /* Skip the lot if 'data' isn't here yet (initialistion) */
113 for (i=0; data && data->partitions && data->partitions[i]; i++) {
114 if (ldb_dn_compare_base(data->partitions[i]->dn, dn) == 0) {
115 return data->partitions[i];
123 fire the caller's callback for every entry, but only send 'done' once.
125 static int partition_search_callback(struct ldb_context *ldb, void *context, struct ldb_reply *ares)
127 struct partition_context *ac;
129 if (!context || !ares) {
130 ldb_set_errstring(ldb, "partition_search_callback: NULL Context or Result in 'search' callback");
134 ac = talloc_get_type(context, struct partition_context);
136 if (ares->type == LDB_REPLY_ENTRY) {
137 return ac->orig_req->callback(ldb, ac->orig_req->context, ares);
139 ac->finished_requests++;
140 if (ac->finished_requests == ac->num_requests) {
141 return ac->orig_req->callback(ldb, ac->orig_req->context, ares);
149 return LDB_ERR_OPERATIONS_ERROR;
153 only fire the 'last' callback, and only for START-TLS for now
155 static int partition_other_callback(struct ldb_context *ldb, void *context, struct ldb_reply *ares)
157 struct partition_context *ac;
160 ldb_set_errstring(ldb, "partition_other_callback: NULL Context in 'other' callback");
164 ac = talloc_get_type(context, struct partition_context);
166 if (!ac->orig_req->callback) {
172 || (ares->type == LDB_REPLY_EXTENDED
173 && strcmp(ares->response->oid, LDB_EXTENDED_START_TLS_OID))) {
174 ac->finished_requests++;
175 if (ac->finished_requests == ac->num_requests) {
176 return ac->orig_req->callback(ldb, ac->orig_req->context, ares);
181 ldb_set_errstring(ldb, "partition_other_callback: Unknown reply type, only supports START_TLS");
184 return LDB_ERR_OPERATIONS_ERROR;
188 static int partition_send_request(struct partition_context *ac, struct ldb_control *remove_control,
189 struct dsdb_control_current_partition *partition)
192 struct ldb_module *backend;
193 struct ldb_request *req;
194 struct ldb_control **saved_controls;
197 backend = make_module_for_next_request(ac, ac->module->ldb, partition->module);
199 backend = ac->module;
202 ac->down_req = talloc_realloc(ac, ac->down_req,
203 struct ldb_request *, ac->num_requests + 1);
205 ldb_oom(ac->module->ldb);
206 return LDB_ERR_OPERATIONS_ERROR;
208 req = ac->down_req[ac->num_requests] = talloc(ac, struct ldb_request);
210 ldb_oom(ac->module->ldb);
211 return LDB_ERR_OPERATIONS_ERROR;
214 *req = *ac->orig_req; /* copy the request */
219 ac->orig_req->controls, talloc_get_size(ac->orig_req->controls));
220 if (req->controls == NULL) {
221 ldb_oom(ac->module->ldb);
222 return LDB_ERR_OPERATIONS_ERROR;
226 if (req->operation == LDB_SEARCH) {
227 /* If the search is for 'more' than this partition,
228 * then change the basedn, so a remote LDAP server
231 if (ldb_dn_compare_base(partition->dn, req->op.search.base) != 0) {
232 req->op.search.base = partition->dn;
235 req->op.search.base = NULL;
237 req->callback = partition_search_callback;
240 req->callback = partition_other_callback;
244 /* Remove a control, so we don't confuse a backend server */
245 if (remove_control && !save_controls(remove_control, req, &saved_controls)) {
246 ldb_oom(ac->module->ldb);
247 return LDB_ERR_OPERATIONS_ERROR;
251 ret = ldb_request_add_control(req, DSDB_CONTROL_CURRENT_PARTITION_OID, false, partition);
252 if (ret != LDB_SUCCESS) {
257 /* Spray off search requests to all backends */
258 ret = ldb_next_request(backend, req);
259 if (ret != LDB_SUCCESS) {
267 /* Send a request down to all the partitions */
268 static int partition_send_all(struct ldb_module *module,
269 struct partition_context *ac,
270 struct ldb_control *remove_control,
271 struct ldb_request *req)
274 struct partition_private_data *data = talloc_get_type(module->private_data,
275 struct partition_private_data);
276 int ret = partition_send_request(ac, remove_control, NULL);
277 if (ret != LDB_SUCCESS) {
280 for (i=0; data && data->partitions && data->partitions[i]; i++) {
281 ret = partition_send_request(ac, remove_control, data->partitions[i]);
282 if (ret != LDB_SUCCESS) {
289 /* Figure out which backend a request needs to be aimed at. Some
290 * requests must be replicated to all backends */
291 static int partition_replicate(struct ldb_module *module, struct ldb_request *req, struct ldb_dn *dn)
295 struct dsdb_control_current_partition *partition;
296 struct ldb_module *backend;
297 struct partition_private_data *data = talloc_get_type(module->private_data,
298 struct partition_private_data);
300 if (req->operation != LDB_SEARCH) {
301 /* Is this a special DN, we need to replicate to every backend? */
302 for (i=0; data->replicate && data->replicate[i]; i++) {
303 if (ldb_dn_compare(data->replicate[i],
305 struct partition_context *ac;
307 ac = partition_init_handle(req, module);
309 return LDB_ERR_OPERATIONS_ERROR;
312 return partition_send_all(module, ac, NULL, req);
317 /* Otherwise, we need to find the partition to fire it to */
320 partition = find_partition(data, dn);
323 * if we haven't found a matching partition
324 * pass the request to the main ldb
326 * TODO: we should maybe return an error here
327 * if it's not a special dn
329 return ldb_next_request(module, req);
332 backend = make_module_for_next_request(req, module->ldb, partition->module);
334 return LDB_ERR_OPERATIONS_ERROR;
337 ret = ldb_request_add_control(req, DSDB_CONTROL_CURRENT_PARTITION_OID, false, partition);
338 if (ret != LDB_SUCCESS) {
343 return ldb_next_request(backend, req);
347 static int partition_search(struct ldb_module *module, struct ldb_request *req)
350 struct partition_private_data *data = talloc_get_type(module->private_data,
351 struct partition_private_data);
354 /* (later) consider if we should be searching multiple
355 * partitions (for 'invisible' partition behaviour */
356 struct ldb_control *search_control = ldb_request_get_control(req, LDB_CONTROL_SEARCH_OPTIONS_OID);
358 struct ldb_search_options_control *search_options = NULL;
359 if (search_control) {
360 search_options = talloc_get_type(search_control->data, struct ldb_search_options_control);
363 if (search_options && (search_options->search_options & LDB_SEARCH_OPTION_PHANTOM_ROOT)) {
365 struct partition_context *ac;
366 struct ldb_control *remove_control = NULL;
367 if ((search_options->search_options & ~LDB_SEARCH_OPTION_PHANTOM_ROOT) == 0) {
368 /* We have processed this flag, so we are done with this control now */
369 remove_control = search_control;
371 ac = partition_init_handle(req, module);
373 return LDB_ERR_OPERATIONS_ERROR;
376 /* Search from the base DN */
377 if (!req->op.search.base || ldb_dn_is_null(req->op.search.base)) {
378 return partition_send_all(module, ac, remove_control, req);
380 for (i=0; data && data->partitions && data->partitions[i]; i++) {
381 /* Find all partitions under the search base */
382 if (ldb_dn_compare_base(req->op.search.base, data->partitions[i]->dn) == 0) {
383 ret = partition_send_request(ac, remove_control, data->partitions[i]);
384 if (ret != LDB_SUCCESS) {
390 /* Perhaps we didn't match any partitions. Try the main partition, only */
391 if (ac->num_requests == 0) {
393 return ldb_next_request(module, req);
398 /* Handle this like all other requests */
399 return partition_replicate(module, req, req->op.search.base);
404 static int partition_add(struct ldb_module *module, struct ldb_request *req)
406 return partition_replicate(module, req, req->op.add.message->dn);
410 static int partition_modify(struct ldb_module *module, struct ldb_request *req)
412 return partition_replicate(module, req, req->op.mod.message->dn);
416 static int partition_delete(struct ldb_module *module, struct ldb_request *req)
418 return partition_replicate(module, req, req->op.del.dn);
422 static int partition_rename(struct ldb_module *module, struct ldb_request *req)
425 struct dsdb_control_current_partition *backend, *backend2;
427 struct partition_private_data *data = talloc_get_type(module->private_data,
428 struct partition_private_data);
430 /* Skip the lot if 'data' isn't here yet (initialistion) */
432 return LDB_ERR_OPERATIONS_ERROR;
435 backend = find_partition(data, req->op.rename.olddn);
436 backend2 = find_partition(data, req->op.rename.newdn);
438 if ((backend && !backend2) || (!backend && backend2)) {
439 return LDB_ERR_AFFECTS_MULTIPLE_DSAS;
442 if (backend != backend2) {
443 ldb_asprintf_errstring(module->ldb,
444 "Cannot rename from %s in %s to %s in %s: %s",
445 ldb_dn_get_linearized(req->op.rename.olddn),
446 ldb_dn_get_linearized(backend->dn),
447 ldb_dn_get_linearized(req->op.rename.newdn),
448 ldb_dn_get_linearized(backend2->dn),
449 ldb_strerror(LDB_ERR_AFFECTS_MULTIPLE_DSAS));
450 return LDB_ERR_AFFECTS_MULTIPLE_DSAS;
453 return partition_replicate(module, req, req->op.rename.olddn);
456 /* start a transaction */
457 static int partition_start_trans(struct ldb_module *module)
460 struct partition_private_data *data = talloc_get_type(module->private_data,
461 struct partition_private_data);
462 /* Look at base DN */
463 /* Figure out which partition it is under */
464 /* Skip the lot if 'data' isn't here yet (initialistion) */
465 ret = ldb_next_start_trans(module);
466 if (ret != LDB_SUCCESS) {
470 for (i=0; data && data->partitions && data->partitions[i]; i++) {
471 struct ldb_module *next = make_module_for_next_request(module, module->ldb, data->partitions[i]->module);
473 ret = ldb_next_start_trans(next);
475 if (ret != LDB_SUCCESS) {
476 /* Back it out, if it fails on one */
477 for (i--; i >= 0; i--) {
478 next = make_module_for_next_request(module, module->ldb, data->partitions[i]->module);
479 ldb_next_del_trans(next);
488 /* end a transaction */
489 static int partition_end_trans(struct ldb_module *module)
491 int i, ret, ret2 = LDB_SUCCESS;
492 struct partition_private_data *data = talloc_get_type(module->private_data,
493 struct partition_private_data);
494 ret = ldb_next_end_trans(module);
495 if (ret != LDB_SUCCESS) {
499 /* Look at base DN */
500 /* Figure out which partition it is under */
501 /* Skip the lot if 'data' isn't here yet (initialistion) */
502 for (i=0; data && data->partitions && data->partitions[i]; i++) {
503 struct ldb_module *next = make_module_for_next_request(module, module->ldb, data->partitions[i]->module);
505 ret = ldb_next_end_trans(next);
507 if (ret != LDB_SUCCESS) {
512 if (ret != LDB_SUCCESS) {
513 /* Back it out, if it fails on one */
514 for (i=0; data && data->partitions && data->partitions[i]; i++) {
515 struct ldb_module *next = make_module_for_next_request(module, module->ldb, data->partitions[i]->module);
516 ldb_next_del_trans(next);
523 /* delete a transaction */
524 static int partition_del_trans(struct ldb_module *module)
526 int i, ret, ret2 = LDB_SUCCESS;
527 struct partition_private_data *data = talloc_get_type(module->private_data,
528 struct partition_private_data);
529 ret = ldb_next_del_trans(module);
530 if (ret != LDB_SUCCESS) {
534 /* Look at base DN */
535 /* Figure out which partition it is under */
536 /* Skip the lot if 'data' isn't here yet (initialistion) */
537 for (i=0; data && data->partitions && data->partitions[i]; i++) {
538 struct ldb_module *next = make_module_for_next_request(module, module->ldb, data->partitions[i]->module);
540 ret = ldb_next_del_trans(next);
542 if (ret != LDB_SUCCESS) {
549 static int partition_sequence_number(struct ldb_module *module, struct ldb_request *req)
552 uint64_t seq_number = 0;
553 uint64_t timestamp_sequence = 0;
554 uint64_t timestamp = 0;
555 struct partition_private_data *data = talloc_get_type(module->private_data,
556 struct partition_private_data);
558 switch (req->op.seq_num.type) {
560 case LDB_SEQ_HIGHEST_SEQ:
561 ret = ldb_next_request(module, req);
562 if (ret != LDB_SUCCESS) {
565 if (req->op.seq_num.flags & LDB_SEQ_TIMESTAMP_SEQUENCE) {
566 timestamp_sequence = req->op.seq_num.seq_num;
568 seq_number = seq_number + req->op.seq_num.seq_num;
571 /* Look at base DN */
572 /* Figure out which partition it is under */
573 /* Skip the lot if 'data' isn't here yet (initialistion) */
574 for (i=0; data && data->partitions && data->partitions[i]; i++) {
575 struct ldb_module *next = make_module_for_next_request(req, module->ldb, data->partitions[i]->module);
577 ret = ldb_next_request(next, req);
579 if (ret != LDB_SUCCESS) {
582 if (req->op.seq_num.flags & LDB_SEQ_TIMESTAMP_SEQUENCE) {
583 timestamp_sequence = MAX(timestamp_sequence, req->op.seq_num.seq_num);
585 seq_number = seq_number + req->op.seq_num.seq_num;
589 case LDB_SEQ_HIGHEST_TIMESTAMP:
591 struct ldb_request *date_req = talloc(req, struct ldb_request);
593 return LDB_ERR_OPERATIONS_ERROR;
596 date_req->op.seq_num.flags = LDB_SEQ_HIGHEST_TIMESTAMP;
598 ret = ldb_next_request(module, date_req);
599 if (ret != LDB_SUCCESS) {
602 timestamp = date_req->op.seq_num.seq_num;
604 /* Look at base DN */
605 /* Figure out which partition it is under */
606 /* Skip the lot if 'data' isn't here yet (initialistion) */
607 for (i=0; data && data->partitions && data->partitions[i]; i++) {
608 struct ldb_module *next = make_module_for_next_request(req, module->ldb, data->partitions[i]->module);
610 ret = ldb_next_request(next, date_req);
612 if (ret != LDB_SUCCESS) {
615 timestamp = MAX(timestamp, date_req->op.seq_num.seq_num);
621 switch (req->op.seq_num.flags) {
623 case LDB_SEQ_HIGHEST_SEQ:
625 req->op.seq_num.flags = 0;
627 /* Has someone above set a timebase sequence? */
628 if (timestamp_sequence) {
629 req->op.seq_num.seq_num = (((unsigned long long)timestamp << 24) | (seq_number & 0xFFFFFF));
631 req->op.seq_num.seq_num = seq_number;
634 if (timestamp_sequence > req->op.seq_num.seq_num) {
635 req->op.seq_num.seq_num = timestamp_sequence;
636 req->op.seq_num.flags |= LDB_SEQ_TIMESTAMP_SEQUENCE;
639 req->op.seq_num.flags |= LDB_SEQ_GLOBAL_SEQUENCE;
641 case LDB_SEQ_HIGHEST_TIMESTAMP:
642 req->op.seq_num.seq_num = timestamp;
646 switch (req->op.seq_num.flags) {
648 req->op.seq_num.seq_num++;
653 static int partition_extended_replicated_objects(struct ldb_module *module, struct ldb_request *req)
655 struct dsdb_extended_replicated_objects *ext;
657 ext = talloc_get_type(req->op.extended.data, struct dsdb_extended_replicated_objects);
659 ldb_debug(module->ldb, LDB_DEBUG_FATAL, "partition_extended_replicated_objects: invalid extended data\n");
660 return LDB_ERR_PROTOCOL_ERROR;
663 if (ext->version != DSDB_EXTENDED_REPLICATED_OBJECTS_VERSION) {
664 ldb_debug(module->ldb, LDB_DEBUG_FATAL, "partition_extended_replicated_objects: extended data invalid version [%u != %u]\n",
665 ext->version, DSDB_EXTENDED_REPLICATED_OBJECTS_VERSION);
666 return LDB_ERR_PROTOCOL_ERROR;
669 return partition_replicate(module, req, ext->partition_dn);
673 static int partition_extended(struct ldb_module *module, struct ldb_request *req)
675 struct partition_context *ac;
677 if (strcmp(req->op.extended.oid, DSDB_EXTENDED_REPLICATED_OBJECTS_OID) == 0) {
678 return partition_extended_replicated_objects(module, req);
682 * as the extended operation has no dn
683 * we need to send it to all partitions
686 ac = partition_init_handle(req, module);
688 return LDB_ERR_OPERATIONS_ERROR;
691 return partition_send_all(module, ac, NULL, req);
694 static int sort_compare(void *void1,
695 void *void2, void *opaque)
697 struct dsdb_control_current_partition **pp1 = void1;
698 struct dsdb_control_current_partition **pp2 = void2;
699 struct dsdb_control_current_partition *partition1 = talloc_get_type(*pp1,
700 struct dsdb_control_current_partition);
701 struct dsdb_control_current_partition *partition2 = talloc_get_type(*pp2,
702 struct dsdb_control_current_partition);
704 return ldb_dn_compare(partition1->dn, partition2->dn);
707 static const char *relative_path(struct ldb_module *module,
711 const char *base_url = ldb_get_opaque(module->ldb, "ldb_url");
712 char *path, *p, *full_name;
716 if (name[0] == 0 || name[0] == '/' || strstr(name, ":/")) {
717 return talloc_strdup(mem_ctx, name);
719 path = talloc_strdup(mem_ctx, base_url);
723 if ( (p = strrchr(path, '/')) != NULL) {
725 full_name = talloc_asprintf(mem_ctx, "%s/%s", path, name);
727 full_name = talloc_asprintf(mem_ctx, "./%s", name);
733 static int partition_init(struct ldb_module *module)
736 TALLOC_CTX *mem_ctx = talloc_new(module);
737 static const char *attrs[] = { "partition", "replicateEntries", "modules", NULL };
738 struct ldb_result *res;
739 struct ldb_message *msg;
740 struct ldb_message_element *partition_attributes;
741 struct ldb_message_element *replicate_attributes;
742 struct ldb_message_element *modules_attributes;
744 struct partition_private_data *data;
747 return LDB_ERR_OPERATIONS_ERROR;
750 data = talloc(mem_ctx, struct partition_private_data);
752 return LDB_ERR_OPERATIONS_ERROR;
755 ret = ldb_search(module->ldb, ldb_dn_new(mem_ctx, module->ldb, "@PARTITION"),
759 if (ret != LDB_SUCCESS) {
760 talloc_free(mem_ctx);
763 talloc_steal(mem_ctx, res);
764 if (res->count == 0) {
765 talloc_free(mem_ctx);
766 return ldb_next_init(module);
769 if (res->count > 1) {
770 talloc_free(mem_ctx);
771 return LDB_ERR_CONSTRAINT_VIOLATION;
776 partition_attributes = ldb_msg_find_element(msg, "partition");
777 if (!partition_attributes) {
778 ldb_set_errstring(module->ldb, "partition_init: no partitions specified");
779 talloc_free(mem_ctx);
780 return LDB_ERR_CONSTRAINT_VIOLATION;
782 data->partitions = talloc_array(data, struct dsdb_control_current_partition *, partition_attributes->num_values + 1);
783 if (!data->partitions) {
784 talloc_free(mem_ctx);
785 return LDB_ERR_OPERATIONS_ERROR;
787 for (i=0; i < partition_attributes->num_values; i++) {
788 char *base = talloc_strdup(data->partitions, (char *)partition_attributes->values[i].data);
789 char *p = strchr(base, ':');
791 ldb_asprintf_errstring(module->ldb,
793 "invalid form for partition record (missing ':'): %s", base);
794 talloc_free(mem_ctx);
795 return LDB_ERR_CONSTRAINT_VIOLATION;
800 ldb_asprintf_errstring(module->ldb,
802 "invalid form for partition record (missing backend database): %s", base);
803 talloc_free(mem_ctx);
804 return LDB_ERR_CONSTRAINT_VIOLATION;
806 data->partitions[i] = talloc(data->partitions, struct dsdb_control_current_partition);
807 if (!data->partitions[i]) {
808 talloc_free(mem_ctx);
809 return LDB_ERR_OPERATIONS_ERROR;
811 data->partitions[i]->version = DSDB_CONTROL_CURRENT_PARTITION_VERSION;
813 data->partitions[i]->dn = ldb_dn_new(data->partitions[i], module->ldb, base);
814 if (!data->partitions[i]->dn) {
815 ldb_asprintf_errstring(module->ldb,
816 "partition_init: invalid DN in partition record: %s", base);
817 talloc_free(mem_ctx);
818 return LDB_ERR_CONSTRAINT_VIOLATION;
821 data->partitions[i]->backend = relative_path(module,
824 ret = ldb_connect_backend(module->ldb, data->partitions[i]->backend, NULL, &data->partitions[i]->module);
825 if (ret != LDB_SUCCESS) {
826 talloc_free(mem_ctx);
830 data->partitions[i] = NULL;
832 /* sort these into order, most to least specific */
833 ldb_qsort(data->partitions, partition_attributes->num_values, sizeof(*data->partitions),
834 module->ldb, sort_compare);
836 for (i=0; data->partitions[i]; i++) {
837 struct ldb_request *req;
838 req = talloc_zero(mem_ctx, struct ldb_request);
840 ldb_debug(module->ldb, LDB_DEBUG_ERROR, "partition: Out of memory!\n");
841 talloc_free(mem_ctx);
842 return LDB_ERR_OPERATIONS_ERROR;
845 req->operation = LDB_REQ_REGISTER_PARTITION;
846 req->op.reg_partition.dn = data->partitions[i]->dn;
848 ret = ldb_request(module->ldb, req);
849 if (ret != LDB_SUCCESS) {
850 ldb_debug(module->ldb, LDB_DEBUG_ERROR, "partition: Unable to register partition with rootdse!\n");
851 talloc_free(mem_ctx);
852 return LDB_ERR_OTHER;
857 replicate_attributes = ldb_msg_find_element(msg, "replicateEntries");
858 if (!replicate_attributes) {
859 data->replicate = NULL;
861 data->replicate = talloc_array(data, struct ldb_dn *, replicate_attributes->num_values + 1);
862 if (!data->replicate) {
863 talloc_free(mem_ctx);
864 return LDB_ERR_OPERATIONS_ERROR;
867 for (i=0; i < replicate_attributes->num_values; i++) {
868 data->replicate[i] = ldb_dn_new(data->replicate, module->ldb, (const char *)replicate_attributes->values[i].data);
869 if (!ldb_dn_validate(data->replicate[i])) {
870 ldb_asprintf_errstring(module->ldb,
872 "invalid DN in partition replicate record: %s",
873 replicate_attributes->values[i].data);
874 talloc_free(mem_ctx);
875 return LDB_ERR_CONSTRAINT_VIOLATION;
878 data->replicate[i] = NULL;
881 /* Make the private data available to any searches the modules may trigger in initialisation */
882 module->private_data = data;
883 talloc_steal(module, data);
885 modules_attributes = ldb_msg_find_element(msg, "modules");
886 if (modules_attributes) {
887 for (i=0; i < modules_attributes->num_values; i++) {
888 struct ldb_dn *base_dn;
890 struct dsdb_control_current_partition *partition = NULL;
891 const char **modules = NULL;
893 char *base = talloc_strdup(data->partitions, (char *)modules_attributes->values[i].data);
894 char *p = strchr(base, ':');
896 ldb_asprintf_errstring(module->ldb,
898 "invalid form for partition module record (missing ':'): %s", base);
899 talloc_free(mem_ctx);
900 return LDB_ERR_CONSTRAINT_VIOLATION;
905 ldb_asprintf_errstring(module->ldb,
907 "invalid form for partition module record (missing backend database): %s", base);
908 talloc_free(mem_ctx);
909 return LDB_ERR_CONSTRAINT_VIOLATION;
912 modules = ldb_modules_list_from_string(module->ldb, mem_ctx,
915 base_dn = ldb_dn_new(mem_ctx, module->ldb, base);
916 if (!ldb_dn_validate(base_dn)) {
917 talloc_free(mem_ctx);
918 return LDB_ERR_OPERATIONS_ERROR;
921 for (partition_idx = 0; data->partitions[partition_idx]; partition_idx++) {
922 if (ldb_dn_compare(data->partitions[partition_idx]->dn, base_dn) == 0) {
923 partition = data->partitions[partition_idx];
929 ldb_asprintf_errstring(module->ldb,
931 "invalid form for partition module record (no such partition): %s", base);
932 talloc_free(mem_ctx);
933 return LDB_ERR_CONSTRAINT_VIOLATION;
936 ret = ldb_load_modules_list(module->ldb, modules, partition->module, &partition->module);
937 if (ret != LDB_SUCCESS) {
938 ldb_asprintf_errstring(module->ldb,
940 "loading backend for %s failed: %s",
941 base, ldb_errstring(module->ldb));
942 talloc_free(mem_ctx);
945 ret = ldb_init_module_chain(module->ldb, partition->module);
946 if (ret != LDB_SUCCESS) {
947 ldb_asprintf_errstring(module->ldb,
949 "initialising backend for %s failed: %s",
950 base, ldb_errstring(module->ldb));
951 talloc_free(mem_ctx);
957 talloc_free(mem_ctx);
958 return ldb_next_init(module);
961 static int partition_wait_none(struct ldb_handle *handle) {
962 struct partition_context *ac;
966 if (!handle || !handle->private_data) {
967 return LDB_ERR_OPERATIONS_ERROR;
970 if (handle->state == LDB_ASYNC_DONE) {
971 return handle->status;
974 handle->state = LDB_ASYNC_PENDING;
975 handle->status = LDB_SUCCESS;
977 ac = talloc_get_type(handle->private_data, struct partition_context);
979 for (i=0; i < ac->num_requests; i++) {
980 ret = ldb_wait(ac->down_req[i]->handle, LDB_WAIT_NONE);
982 if (ret != LDB_SUCCESS) {
983 handle->status = ret;
986 if (ac->down_req[i]->handle->status != LDB_SUCCESS) {
987 handle->status = ac->down_req[i]->handle->status;
991 if (ac->down_req[i]->handle->state != LDB_ASYNC_DONE) {
999 handle->state = LDB_ASYNC_DONE;
1004 static int partition_wait_all(struct ldb_handle *handle) {
1008 while (handle->state != LDB_ASYNC_DONE) {
1009 ret = partition_wait_none(handle);
1010 if (ret != LDB_SUCCESS) {
1015 return handle->status;
1018 static int partition_wait(struct ldb_handle *handle, enum ldb_wait_type type)
1020 if (type == LDB_WAIT_ALL) {
1021 return partition_wait_all(handle);
1023 return partition_wait_none(handle);
1027 static const struct ldb_module_ops partition_ops = {
1028 .name = "partition",
1029 .init_context = partition_init,
1030 .search = partition_search,
1031 .add = partition_add,
1032 .modify = partition_modify,
1033 .del = partition_delete,
1034 .rename = partition_rename,
1035 .extended = partition_extended,
1036 .sequence_number = partition_sequence_number,
1037 .start_transaction = partition_start_trans,
1038 .end_transaction = partition_end_trans,
1039 .del_transaction = partition_del_trans,
1040 .wait = partition_wait
1043 int ldb_partition_init(void)
1045 return ldb_register_module(&partition_ops);