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) {
729 full_name = talloc_asprintf(mem_ctx, "%s/%s", path, name);
734 static int partition_init(struct ldb_module *module)
737 TALLOC_CTX *mem_ctx = talloc_new(module);
738 static const char *attrs[] = { "partition", "replicateEntries", "modules", NULL };
739 struct ldb_result *res;
740 struct ldb_message *msg;
741 struct ldb_message_element *partition_attributes;
742 struct ldb_message_element *replicate_attributes;
743 struct ldb_message_element *modules_attributes;
745 struct partition_private_data *data;
748 return LDB_ERR_OPERATIONS_ERROR;
751 data = talloc(mem_ctx, struct partition_private_data);
753 return LDB_ERR_OPERATIONS_ERROR;
756 ret = ldb_search(module->ldb, ldb_dn_new(mem_ctx, module->ldb, "@PARTITION"),
760 if (ret != LDB_SUCCESS) {
761 talloc_free(mem_ctx);
764 talloc_steal(mem_ctx, res);
765 if (res->count == 0) {
766 talloc_free(mem_ctx);
767 return ldb_next_init(module);
770 if (res->count > 1) {
771 talloc_free(mem_ctx);
772 return LDB_ERR_CONSTRAINT_VIOLATION;
777 partition_attributes = ldb_msg_find_element(msg, "partition");
778 if (!partition_attributes) {
779 ldb_set_errstring(module->ldb, "partition_init: no partitions specified");
780 talloc_free(mem_ctx);
781 return LDB_ERR_CONSTRAINT_VIOLATION;
783 data->partitions = talloc_array(data, struct dsdb_control_current_partition *, partition_attributes->num_values + 1);
784 if (!data->partitions) {
785 talloc_free(mem_ctx);
786 return LDB_ERR_OPERATIONS_ERROR;
788 for (i=0; i < partition_attributes->num_values; i++) {
789 char *base = talloc_strdup(data->partitions, (char *)partition_attributes->values[i].data);
790 char *p = strchr(base, ':');
792 ldb_asprintf_errstring(module->ldb,
794 "invalid form for partition record (missing ':'): %s", base);
795 talloc_free(mem_ctx);
796 return LDB_ERR_CONSTRAINT_VIOLATION;
801 ldb_asprintf_errstring(module->ldb,
803 "invalid form for partition record (missing backend database): %s", base);
804 talloc_free(mem_ctx);
805 return LDB_ERR_CONSTRAINT_VIOLATION;
807 data->partitions[i] = talloc(data->partitions, struct dsdb_control_current_partition);
808 if (!data->partitions[i]) {
809 talloc_free(mem_ctx);
810 return LDB_ERR_OPERATIONS_ERROR;
812 data->partitions[i]->version = DSDB_CONTROL_CURRENT_PARTITION_VERSION;
814 data->partitions[i]->dn = ldb_dn_new(data->partitions[i], module->ldb, base);
815 if (!data->partitions[i]->dn) {
816 ldb_asprintf_errstring(module->ldb,
817 "partition_init: invalid DN in partition record: %s", base);
818 talloc_free(mem_ctx);
819 return LDB_ERR_CONSTRAINT_VIOLATION;
822 data->partitions[i]->backend = relative_path(module,
825 ret = ldb_connect_backend(module->ldb, data->partitions[i]->backend, NULL, &data->partitions[i]->module);
826 if (ret != LDB_SUCCESS) {
827 talloc_free(mem_ctx);
831 data->partitions[i] = NULL;
833 /* sort these into order, most to least specific */
834 ldb_qsort(data->partitions, partition_attributes->num_values, sizeof(*data->partitions),
835 module->ldb, sort_compare);
837 for (i=0; data->partitions[i]; i++) {
838 struct ldb_request *req;
839 req = talloc_zero(mem_ctx, struct ldb_request);
841 ldb_debug(module->ldb, LDB_DEBUG_ERROR, "partition: Out of memory!\n");
842 talloc_free(mem_ctx);
843 return LDB_ERR_OPERATIONS_ERROR;
846 req->operation = LDB_REQ_REGISTER_PARTITION;
847 req->op.reg_partition.dn = data->partitions[i]->dn;
849 ret = ldb_request(module->ldb, req);
850 if (ret != LDB_SUCCESS) {
851 ldb_debug(module->ldb, LDB_DEBUG_ERROR, "partition: Unable to register partition with rootdse!\n");
852 talloc_free(mem_ctx);
853 return LDB_ERR_OTHER;
858 replicate_attributes = ldb_msg_find_element(msg, "replicateEntries");
859 if (!replicate_attributes) {
860 data->replicate = NULL;
862 data->replicate = talloc_array(data, struct ldb_dn *, replicate_attributes->num_values + 1);
863 if (!data->replicate) {
864 talloc_free(mem_ctx);
865 return LDB_ERR_OPERATIONS_ERROR;
868 for (i=0; i < replicate_attributes->num_values; i++) {
869 data->replicate[i] = ldb_dn_new(data->replicate, module->ldb, (const char *)replicate_attributes->values[i].data);
870 if (!ldb_dn_validate(data->replicate[i])) {
871 ldb_asprintf_errstring(module->ldb,
873 "invalid DN in partition replicate record: %s",
874 replicate_attributes->values[i].data);
875 talloc_free(mem_ctx);
876 return LDB_ERR_CONSTRAINT_VIOLATION;
879 data->replicate[i] = NULL;
882 /* Make the private data available to any searches the modules may trigger in initialisation */
883 module->private_data = data;
884 talloc_steal(module, data);
886 modules_attributes = ldb_msg_find_element(msg, "modules");
887 if (modules_attributes) {
888 for (i=0; i < modules_attributes->num_values; i++) {
889 struct ldb_dn *base_dn;
891 struct dsdb_control_current_partition *partition = NULL;
892 const char **modules = NULL;
894 char *base = talloc_strdup(data->partitions, (char *)modules_attributes->values[i].data);
895 char *p = strchr(base, ':');
897 ldb_asprintf_errstring(module->ldb,
899 "invalid form for partition module record (missing ':'): %s", base);
900 talloc_free(mem_ctx);
901 return LDB_ERR_CONSTRAINT_VIOLATION;
906 ldb_asprintf_errstring(module->ldb,
908 "invalid form for partition module record (missing backend database): %s", base);
909 talloc_free(mem_ctx);
910 return LDB_ERR_CONSTRAINT_VIOLATION;
913 modules = ldb_modules_list_from_string(module->ldb, mem_ctx,
916 base_dn = ldb_dn_new(mem_ctx, module->ldb, base);
917 if (!ldb_dn_validate(base_dn)) {
918 talloc_free(mem_ctx);
919 return LDB_ERR_OPERATIONS_ERROR;
922 for (partition_idx = 0; data->partitions[partition_idx]; partition_idx++) {
923 if (ldb_dn_compare(data->partitions[partition_idx]->dn, base_dn) == 0) {
924 partition = data->partitions[partition_idx];
930 ldb_asprintf_errstring(module->ldb,
932 "invalid form for partition module record (no such partition): %s", base);
933 talloc_free(mem_ctx);
934 return LDB_ERR_CONSTRAINT_VIOLATION;
937 ret = ldb_load_modules_list(module->ldb, modules, partition->module, &partition->module);
938 if (ret != LDB_SUCCESS) {
939 ldb_asprintf_errstring(module->ldb,
941 "loading backend for %s failed: %s",
942 base, ldb_errstring(module->ldb));
943 talloc_free(mem_ctx);
946 ret = ldb_init_module_chain(module->ldb, partition->module);
947 if (ret != LDB_SUCCESS) {
948 ldb_asprintf_errstring(module->ldb,
950 "initialising backend for %s failed: %s",
951 base, ldb_errstring(module->ldb));
952 talloc_free(mem_ctx);
958 talloc_free(mem_ctx);
959 return ldb_next_init(module);
962 static int partition_wait_none(struct ldb_handle *handle) {
963 struct partition_context *ac;
967 if (!handle || !handle->private_data) {
968 return LDB_ERR_OPERATIONS_ERROR;
971 if (handle->state == LDB_ASYNC_DONE) {
972 return handle->status;
975 handle->state = LDB_ASYNC_PENDING;
976 handle->status = LDB_SUCCESS;
978 ac = talloc_get_type(handle->private_data, struct partition_context);
980 for (i=0; i < ac->num_requests; i++) {
981 ret = ldb_wait(ac->down_req[i]->handle, LDB_WAIT_NONE);
983 if (ret != LDB_SUCCESS) {
984 handle->status = ret;
987 if (ac->down_req[i]->handle->status != LDB_SUCCESS) {
988 handle->status = ac->down_req[i]->handle->status;
992 if (ac->down_req[i]->handle->state != LDB_ASYNC_DONE) {
1000 handle->state = LDB_ASYNC_DONE;
1005 static int partition_wait_all(struct ldb_handle *handle) {
1009 while (handle->state != LDB_ASYNC_DONE) {
1010 ret = partition_wait_none(handle);
1011 if (ret != LDB_SUCCESS) {
1016 return handle->status;
1019 static int partition_wait(struct ldb_handle *handle, enum ldb_wait_type type)
1021 if (type == LDB_WAIT_ALL) {
1022 return partition_wait_all(handle);
1024 return partition_wait_none(handle);
1028 static const struct ldb_module_ops partition_ops = {
1029 .name = "partition",
1030 .init_context = partition_init,
1031 .search = partition_search,
1032 .add = partition_add,
1033 .modify = partition_modify,
1034 .del = partition_delete,
1035 .rename = partition_rename,
1036 .extended = partition_extended,
1037 .sequence_number = partition_sequence_number,
1038 .start_transaction = partition_start_trans,
1039 .end_transaction = partition_end_trans,
1040 .del_transaction = partition_del_trans,
1041 .wait = partition_wait
1044 int ldb_partition_init(void)
1046 return ldb_register_module(&partition_ops);