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_request *orig_req;
50 struct ldb_request **down_req;
52 int finished_requests;
55 static struct partition_context *partition_init_handle(struct ldb_request *req, struct ldb_module *module)
57 struct partition_context *ac;
60 h = talloc_zero(req, struct ldb_handle);
62 ldb_set_errstring(module->ldb, "Out of Memory");
68 ac = talloc_zero(h, struct partition_context);
70 ldb_set_errstring(module->ldb, "Out of Memory");
85 static struct ldb_module *make_module_for_next_request(TALLOC_CTX *mem_ctx,
86 struct ldb_context *ldb,
87 struct ldb_module *module)
89 struct ldb_module *current;
90 static const struct ldb_module_ops ops; /* zero */
91 current = talloc_zero(mem_ctx, struct ldb_module);
92 if (current == NULL) {
99 current->next = module;
103 static struct dsdb_control_current_partition *find_partition(struct partition_private_data *data,
108 /* Look at base DN */
109 /* Figure out which partition it is under */
110 /* Skip the lot if 'data' isn't here yet (initialistion) */
111 for (i=0; data && data->partitions && data->partitions[i]; i++) {
112 if (ldb_dn_compare_base(data->partitions[i]->dn, dn) == 0) {
113 return data->partitions[i];
121 * fire the caller's callback for every entry, but only send 'done' once.
123 static int partition_search_callback(struct ldb_context *ldb, void *context, struct ldb_reply *ares)
125 struct partition_context *ac;
127 ac = talloc_get_type(context, struct partition_context);
129 if (ares->type == LDB_REPLY_ENTRY) {
130 return ac->orig_req->callback(ldb, ac->orig_req->context, ares);
132 ac->finished_requests++;
133 if (ac->finished_requests == ac->num_requests) {
134 return ac->orig_req->callback(ldb, ac->orig_req->context, ares);
143 * only fire the 'last' callback, and only for START-TLS for now
145 static int partition_other_callback(struct ldb_context *ldb, void *context, struct ldb_reply *ares)
147 struct partition_context *ac;
149 ac = talloc_get_type(context, struct partition_context);
151 if (!ac->orig_req->callback) {
157 || (ares->type == LDB_REPLY_EXTENDED
158 && strcmp(ares->response->oid, LDB_EXTENDED_START_TLS_OID))) {
159 ac->finished_requests++;
160 if (ac->finished_requests == ac->num_requests) {
161 return ac->orig_req->callback(ldb, ac->orig_req->context, ares);
166 ldb_set_errstring(ldb, "partition_other_callback: Unknown reply type, only supports START_TLS");
168 return LDB_ERR_OPERATIONS_ERROR;
172 static int partition_send_request(struct partition_context *ac, struct ldb_control *remove_control,
173 struct dsdb_control_current_partition *partition)
176 struct ldb_module *backend;
177 struct ldb_request *req;
178 struct ldb_control **saved_controls;
181 backend = make_module_for_next_request(ac, ac->module->ldb, partition->module);
183 backend = ac->module;
186 ac->down_req = talloc_realloc(ac, ac->down_req,
187 struct ldb_request *, ac->num_requests + 1);
189 ldb_oom(ac->module->ldb);
190 return LDB_ERR_OPERATIONS_ERROR;
192 req = ac->down_req[ac->num_requests] = talloc(ac, struct ldb_request);
194 ldb_oom(ac->module->ldb);
195 return LDB_ERR_OPERATIONS_ERROR;
198 *req = *ac->orig_req; /* copy the request */
203 ac->orig_req->controls, talloc_get_size(ac->orig_req->controls));
204 if (req->controls == NULL) {
205 ldb_oom(ac->module->ldb);
206 return LDB_ERR_OPERATIONS_ERROR;
210 if (req->operation == LDB_SEARCH) {
211 /* If the search is for 'more' than this partition,
212 * then change the basedn, so a remote LDAP server
215 if (ldb_dn_compare_base(partition->dn, req->op.search.base) != 0) {
216 req->op.search.base = partition->dn;
219 req->op.search.base = NULL;
221 req->callback = partition_search_callback;
224 req->callback = partition_other_callback;
228 /* Remove a control, so we don't confuse a backend server */
229 if (remove_control && !save_controls(remove_control, req, &saved_controls)) {
230 ldb_oom(ac->module->ldb);
231 return LDB_ERR_OPERATIONS_ERROR;
235 ret = ldb_request_add_control(req, DSDB_CONTROL_CURRENT_PARTITION_OID, false, partition);
236 if (ret != LDB_SUCCESS) {
241 /* Spray off search requests the backend */
242 ret = ldb_next_request(backend, req);
243 if (ret != LDB_SUCCESS) {
252 * Send a request down to all the partitions
254 static int partition_send_all(struct ldb_module *module,
255 struct partition_context *ac,
256 struct ldb_control *remove_control,
257 struct ldb_request *req)
260 struct partition_private_data *data = talloc_get_type(module->private_data,
261 struct partition_private_data);
262 int ret = partition_send_request(ac, remove_control, NULL);
263 if (ret != LDB_SUCCESS) {
266 for (i=0; data && data->partitions && data->partitions[i]; i++) {
267 ret = partition_send_request(ac, remove_control, data->partitions[i]);
268 if (ret != LDB_SUCCESS) {
276 * Figure out which backend a request needs to be aimed at. Some
277 * requests must be replicated to all backends
279 static int partition_replicate(struct ldb_module *module, struct ldb_request *req, struct ldb_dn *dn)
283 struct dsdb_control_current_partition *partition;
284 struct ldb_module *backend;
285 struct partition_private_data *data = talloc_get_type(module->private_data,
286 struct partition_private_data);
288 if (req->operation != LDB_SEARCH) {
289 /* Is this a special DN, we need to replicate to every backend? */
290 for (i=0; data->replicate && data->replicate[i]; i++) {
291 if (ldb_dn_compare(data->replicate[i],
293 struct partition_context *ac;
295 ac = partition_init_handle(req, module);
297 return LDB_ERR_OPERATIONS_ERROR;
300 return partition_send_all(module, ac, NULL, req);
305 /* Otherwise, we need to find the partition to fire it to */
308 partition = find_partition(data, dn);
311 * if we haven't found a matching partition
312 * pass the request to the main ldb
314 * TODO: we should maybe return an error here
315 * if it's not a special dn
317 return ldb_next_request(module, req);
320 backend = make_module_for_next_request(req, module->ldb, partition->module);
322 return LDB_ERR_OPERATIONS_ERROR;
325 ret = ldb_request_add_control(req, DSDB_CONTROL_CURRENT_PARTITION_OID, false, partition);
326 if (ret != LDB_SUCCESS) {
331 return ldb_next_request(backend, req);
335 static int partition_search(struct ldb_module *module, struct ldb_request *req)
338 struct partition_private_data *data = talloc_get_type(module->private_data,
339 struct partition_private_data);
342 /* (later) consider if we should be searching multiple
343 * partitions (for 'invisible' partition behaviour */
344 struct ldb_control *search_control = ldb_request_get_control(req, LDB_CONTROL_SEARCH_OPTIONS_OID);
346 struct ldb_search_options_control *search_options = NULL;
347 if (search_control) {
348 search_options = talloc_get_type(search_control->data, struct ldb_search_options_control);
351 if (search_options && (search_options->search_options & LDB_SEARCH_OPTION_PHANTOM_ROOT)) {
353 struct partition_context *ac;
354 struct ldb_control *remove_control = NULL;
355 if ((search_options->search_options & ~LDB_SEARCH_OPTION_PHANTOM_ROOT) == 0) {
356 /* We have processed this flag, so we are done with this control now */
357 remove_control = search_control;
359 ac = partition_init_handle(req, module);
361 return LDB_ERR_OPERATIONS_ERROR;
364 /* Search from the base DN */
365 if (!req->op.search.base || ldb_dn_is_null(req->op.search.base)) {
366 return partition_send_all(module, ac, remove_control, req);
368 for (i=0; data && data->partitions && data->partitions[i]; i++) {
369 /* Find all partitions under the search base */
370 if (ldb_dn_compare_base(req->op.search.base, data->partitions[i]->dn) == 0) {
371 ret = partition_send_request(ac, remove_control, data->partitions[i]);
372 if (ret != LDB_SUCCESS) {
378 /* Perhaps we didn't match any partitions. Try the main partition, only */
379 if (ac->num_requests == 0) {
381 return ldb_next_request(module, req);
386 /* Handle this like all other requests */
387 return partition_replicate(module, req, req->op.search.base);
392 static int partition_add(struct ldb_module *module, struct ldb_request *req)
394 return partition_replicate(module, req, req->op.add.message->dn);
398 static int partition_modify(struct ldb_module *module, struct ldb_request *req)
400 return partition_replicate(module, req, req->op.mod.message->dn);
404 static int partition_delete(struct ldb_module *module, struct ldb_request *req)
406 return partition_replicate(module, req, req->op.del.dn);
410 static int partition_rename(struct ldb_module *module, struct ldb_request *req)
414 struct dsdb_control_current_partition *backend, *backend2;
416 struct partition_private_data *data = talloc_get_type(module->private_data,
417 struct partition_private_data);
419 /* Skip the lot if 'data' isn't here yet (initialistion) */
421 return LDB_ERR_OPERATIONS_ERROR;
424 backend = find_partition(data, req->op.rename.olddn);
425 backend2 = find_partition(data, req->op.rename.newdn);
427 if ((backend && !backend2) || (!backend && backend2)) {
428 return LDB_ERR_AFFECTS_MULTIPLE_DSAS;
431 if (backend != backend2) {
432 ldb_asprintf_errstring(module->ldb,
433 "Cannot rename from %s in %s to %s in %s: %s",
434 ldb_dn_get_linearized(req->op.rename.olddn),
435 ldb_dn_get_linearized(backend->dn),
436 ldb_dn_get_linearized(req->op.rename.newdn),
437 ldb_dn_get_linearized(backend2->dn),
438 ldb_strerror(LDB_ERR_AFFECTS_MULTIPLE_DSAS));
439 return LDB_ERR_AFFECTS_MULTIPLE_DSAS;
442 for (i=0; data && data->partitions && data->partitions[i]; i++) {
443 if (ldb_dn_compare_base(req->op.rename.olddn, data->partitions[i]->dn) == 0) {
449 ldb_asprintf_errstring(module->ldb,
450 "Cannot rename from %s to %s, subtree rename would cross partition %s: %s",
451 ldb_dn_get_linearized(req->op.rename.olddn),
452 ldb_dn_get_linearized(req->op.rename.newdn),
453 ldb_dn_get_linearized(data->partitions[matched]->dn),
454 ldb_strerror(LDB_ERR_AFFECTS_MULTIPLE_DSAS));
455 return LDB_ERR_AFFECTS_MULTIPLE_DSAS;
458 return partition_replicate(module, req, req->op.rename.olddn);
461 /* start a transaction */
462 static int partition_start_trans(struct ldb_module *module)
465 struct partition_private_data *data = talloc_get_type(module->private_data,
466 struct partition_private_data);
467 /* Look at base DN */
468 /* Figure out which partition it is under */
469 /* Skip the lot if 'data' isn't here yet (initialistion) */
470 ret = ldb_next_start_trans(module);
471 if (ret != LDB_SUCCESS) {
475 for (i=0; data && data->partitions && data->partitions[i]; i++) {
476 struct ldb_module *next = make_module_for_next_request(module, module->ldb, data->partitions[i]->module);
478 ret = ldb_next_start_trans(next);
480 if (ret != LDB_SUCCESS) {
481 /* Back it out, if it fails on one */
482 for (i--; i >= 0; i--) {
483 next = make_module_for_next_request(module, module->ldb, data->partitions[i]->module);
484 ldb_next_del_trans(next);
493 /* end a transaction */
494 static int partition_end_trans(struct ldb_module *module)
496 int i, ret, ret2 = LDB_SUCCESS;
497 struct partition_private_data *data = talloc_get_type(module->private_data,
498 struct partition_private_data);
499 ret = ldb_next_end_trans(module);
500 if (ret != LDB_SUCCESS) {
504 /* Look at base DN */
505 /* Figure out which partition it is under */
506 /* Skip the lot if 'data' isn't here yet (initialistion) */
507 for (i=0; data && data->partitions && data->partitions[i]; i++) {
508 struct ldb_module *next = make_module_for_next_request(module, module->ldb, data->partitions[i]->module);
510 ret = ldb_next_end_trans(next);
512 if (ret != LDB_SUCCESS) {
517 if (ret != LDB_SUCCESS) {
518 /* Back it out, if it fails on one */
519 for (i=0; data && data->partitions && data->partitions[i]; i++) {
520 struct ldb_module *next = make_module_for_next_request(module, module->ldb, data->partitions[i]->module);
521 ldb_next_del_trans(next);
528 /* delete a transaction */
529 static int partition_del_trans(struct ldb_module *module)
531 int i, ret, ret2 = LDB_SUCCESS;
532 struct partition_private_data *data = talloc_get_type(module->private_data,
533 struct partition_private_data);
534 ret = ldb_next_del_trans(module);
535 if (ret != LDB_SUCCESS) {
539 /* Look at base DN */
540 /* Figure out which partition it is under */
541 /* Skip the lot if 'data' isn't here yet (initialistion) */
542 for (i=0; data && data->partitions && data->partitions[i]; i++) {
543 struct ldb_module *next = make_module_for_next_request(module, module->ldb, data->partitions[i]->module);
545 ret = ldb_next_del_trans(next);
547 if (ret != LDB_SUCCESS) {
554 static int partition_sequence_number(struct ldb_module *module, struct ldb_request *req)
557 uint64_t seq_number = 0;
558 uint64_t timestamp_sequence = 0;
559 uint64_t timestamp = 0;
560 struct partition_private_data *data = talloc_get_type(module->private_data,
561 struct partition_private_data);
563 switch (req->op.seq_num.type) {
565 case LDB_SEQ_HIGHEST_SEQ:
566 ret = ldb_next_request(module, req);
567 if (ret != LDB_SUCCESS) {
570 if (req->op.seq_num.flags & LDB_SEQ_TIMESTAMP_SEQUENCE) {
571 timestamp_sequence = req->op.seq_num.seq_num;
573 seq_number = seq_number + req->op.seq_num.seq_num;
576 /* Look at base DN */
577 /* Figure out which partition it is under */
578 /* Skip the lot if 'data' isn't here yet (initialistion) */
579 for (i=0; data && data->partitions && data->partitions[i]; i++) {
580 struct ldb_module *next = make_module_for_next_request(req, module->ldb, data->partitions[i]->module);
582 ret = ldb_next_request(next, req);
584 if (ret != LDB_SUCCESS) {
587 if (req->op.seq_num.flags & LDB_SEQ_TIMESTAMP_SEQUENCE) {
588 timestamp_sequence = MAX(timestamp_sequence, req->op.seq_num.seq_num);
590 seq_number = seq_number + req->op.seq_num.seq_num;
594 case LDB_SEQ_HIGHEST_TIMESTAMP:
596 struct ldb_request *date_req = talloc(req, struct ldb_request);
598 return LDB_ERR_OPERATIONS_ERROR;
601 date_req->op.seq_num.flags = LDB_SEQ_HIGHEST_TIMESTAMP;
603 ret = ldb_next_request(module, date_req);
604 if (ret != LDB_SUCCESS) {
607 timestamp = date_req->op.seq_num.seq_num;
609 /* Look at base DN */
610 /* Figure out which partition it is under */
611 /* Skip the lot if 'data' isn't here yet (initialistion) */
612 for (i=0; data && data->partitions && data->partitions[i]; i++) {
613 struct ldb_module *next = make_module_for_next_request(req, module->ldb, data->partitions[i]->module);
615 ret = ldb_next_request(next, date_req);
617 if (ret != LDB_SUCCESS) {
620 timestamp = MAX(timestamp, date_req->op.seq_num.seq_num);
626 switch (req->op.seq_num.flags) {
628 case LDB_SEQ_HIGHEST_SEQ:
630 req->op.seq_num.flags = 0;
632 /* Has someone above set a timebase sequence? */
633 if (timestamp_sequence) {
634 req->op.seq_num.seq_num = (((unsigned long long)timestamp << 24) | (seq_number & 0xFFFFFF));
636 req->op.seq_num.seq_num = seq_number;
639 if (timestamp_sequence > req->op.seq_num.seq_num) {
640 req->op.seq_num.seq_num = timestamp_sequence;
641 req->op.seq_num.flags |= LDB_SEQ_TIMESTAMP_SEQUENCE;
644 req->op.seq_num.flags |= LDB_SEQ_GLOBAL_SEQUENCE;
646 case LDB_SEQ_HIGHEST_TIMESTAMP:
647 req->op.seq_num.seq_num = timestamp;
651 switch (req->op.seq_num.flags) {
653 req->op.seq_num.seq_num++;
658 static int partition_extended_replicated_objects(struct ldb_module *module, struct ldb_request *req)
660 struct dsdb_extended_replicated_objects *ext;
662 ext = talloc_get_type(req->op.extended.data, struct dsdb_extended_replicated_objects);
664 ldb_debug(module->ldb, LDB_DEBUG_FATAL, "partition_extended_replicated_objects: invalid extended data\n");
665 return LDB_ERR_PROTOCOL_ERROR;
668 if (ext->version != DSDB_EXTENDED_REPLICATED_OBJECTS_VERSION) {
669 ldb_debug(module->ldb, LDB_DEBUG_FATAL, "partition_extended_replicated_objects: extended data invalid version [%u != %u]\n",
670 ext->version, DSDB_EXTENDED_REPLICATED_OBJECTS_VERSION);
671 return LDB_ERR_PROTOCOL_ERROR;
674 return partition_replicate(module, req, ext->partition_dn);
678 static int partition_extended(struct ldb_module *module, struct ldb_request *req)
680 struct partition_context *ac;
682 if (strcmp(req->op.extended.oid, DSDB_EXTENDED_REPLICATED_OBJECTS_OID) == 0) {
683 return partition_extended_replicated_objects(module, req);
687 * as the extended operation has no dn
688 * we need to send it to all partitions
691 ac = partition_init_handle(req, module);
693 return LDB_ERR_OPERATIONS_ERROR;
696 return partition_send_all(module, ac, NULL, req);
699 static int sort_compare(void *void1,
700 void *void2, void *opaque)
702 struct dsdb_control_current_partition **pp1 =
703 (struct dsdb_control_current_partition **)void1;
704 struct dsdb_control_current_partition **pp2 =
705 (struct dsdb_control_current_partition **)void2;
706 struct dsdb_control_current_partition *partition1 = talloc_get_type(*pp1,
707 struct dsdb_control_current_partition);
708 struct dsdb_control_current_partition *partition2 = talloc_get_type(*pp2,
709 struct dsdb_control_current_partition);
711 return ldb_dn_compare(partition1->dn, partition2->dn);
714 static int partition_init(struct ldb_module *module)
717 TALLOC_CTX *mem_ctx = talloc_new(module);
718 const char *attrs[] = { "partition", "replicateEntries", "modules", NULL };
719 struct ldb_result *res;
720 struct ldb_message *msg;
721 struct ldb_message_element *partition_attributes;
722 struct ldb_message_element *replicate_attributes;
723 struct ldb_message_element *modules_attributes;
725 struct partition_private_data *data;
728 return LDB_ERR_OPERATIONS_ERROR;
731 data = talloc(mem_ctx, struct partition_private_data);
733 return LDB_ERR_OPERATIONS_ERROR;
736 ret = ldb_search(module->ldb, ldb_dn_new(mem_ctx, module->ldb, "@PARTITION"),
740 if (ret != LDB_SUCCESS) {
741 talloc_free(mem_ctx);
744 talloc_steal(mem_ctx, res);
745 if (res->count == 0) {
746 talloc_free(mem_ctx);
747 return ldb_next_init(module);
750 if (res->count > 1) {
751 talloc_free(mem_ctx);
752 return LDB_ERR_CONSTRAINT_VIOLATION;
757 partition_attributes = ldb_msg_find_element(msg, "partition");
758 if (!partition_attributes) {
759 ldb_set_errstring(module->ldb, "partition_init: no partitions specified");
760 talloc_free(mem_ctx);
761 return LDB_ERR_CONSTRAINT_VIOLATION;
763 data->partitions = talloc_array(data, struct dsdb_control_current_partition *, partition_attributes->num_values + 1);
764 if (!data->partitions) {
765 talloc_free(mem_ctx);
766 return LDB_ERR_OPERATIONS_ERROR;
768 for (i=0; i < partition_attributes->num_values; i++) {
769 char *base = talloc_strdup(data->partitions, (char *)partition_attributes->values[i].data);
770 char *p = strchr(base, ':');
772 ldb_asprintf_errstring(module->ldb,
774 "invalid form for partition record (missing ':'): %s", base);
775 talloc_free(mem_ctx);
776 return LDB_ERR_CONSTRAINT_VIOLATION;
781 ldb_asprintf_errstring(module->ldb,
783 "invalid form for partition record (missing backend database): %s", base);
784 talloc_free(mem_ctx);
785 return LDB_ERR_CONSTRAINT_VIOLATION;
787 data->partitions[i] = talloc(data->partitions, struct dsdb_control_current_partition);
788 if (!data->partitions[i]) {
789 talloc_free(mem_ctx);
790 return LDB_ERR_OPERATIONS_ERROR;
792 data->partitions[i]->version = DSDB_CONTROL_CURRENT_PARTITION_VERSION;
794 data->partitions[i]->dn = ldb_dn_new(data->partitions[i], module->ldb, base);
795 if (!data->partitions[i]->dn) {
796 ldb_asprintf_errstring(module->ldb,
797 "partition_init: invalid DN in partition record: %s", base);
798 talloc_free(mem_ctx);
799 return LDB_ERR_CONSTRAINT_VIOLATION;
802 data->partitions[i]->backend = samdb_relative_path(module->ldb,
805 if (!data->partitions[i]->backend) {
806 ldb_asprintf_errstring(module->ldb,
807 "partition_init: unable to determine an relative path for partition: %s", base);
808 talloc_free(mem_ctx);
810 ret = ldb_connect_backend(module->ldb, data->partitions[i]->backend, NULL, &data->partitions[i]->module);
811 if (ret != LDB_SUCCESS) {
812 talloc_free(mem_ctx);
816 data->partitions[i] = NULL;
818 /* sort these into order, most to least specific */
819 ldb_qsort(data->partitions, partition_attributes->num_values, sizeof(*data->partitions),
820 module->ldb, sort_compare);
822 for (i=0; data->partitions[i]; i++) {
823 struct ldb_request *req;
824 req = talloc_zero(mem_ctx, struct ldb_request);
826 ldb_debug(module->ldb, LDB_DEBUG_ERROR, "partition: Out of memory!\n");
827 talloc_free(mem_ctx);
828 return LDB_ERR_OPERATIONS_ERROR;
831 req->operation = LDB_REQ_REGISTER_PARTITION;
832 req->op.reg_partition.dn = data->partitions[i]->dn;
834 ret = ldb_request(module->ldb, req);
835 if (ret != LDB_SUCCESS) {
836 ldb_debug(module->ldb, LDB_DEBUG_ERROR, "partition: Unable to register partition with rootdse!\n");
837 talloc_free(mem_ctx);
838 return LDB_ERR_OTHER;
843 replicate_attributes = ldb_msg_find_element(msg, "replicateEntries");
844 if (!replicate_attributes) {
845 data->replicate = NULL;
847 data->replicate = talloc_array(data, struct ldb_dn *, replicate_attributes->num_values + 1);
848 if (!data->replicate) {
849 talloc_free(mem_ctx);
850 return LDB_ERR_OPERATIONS_ERROR;
853 for (i=0; i < replicate_attributes->num_values; i++) {
854 data->replicate[i] = ldb_dn_new(data->replicate, module->ldb, (const char *)replicate_attributes->values[i].data);
855 if (!ldb_dn_validate(data->replicate[i])) {
856 ldb_asprintf_errstring(module->ldb,
858 "invalid DN in partition replicate record: %s",
859 replicate_attributes->values[i].data);
860 talloc_free(mem_ctx);
861 return LDB_ERR_CONSTRAINT_VIOLATION;
864 data->replicate[i] = NULL;
867 /* Make the private data available to any searches the modules may trigger in initialisation */
868 module->private_data = data;
869 talloc_steal(module, data);
871 modules_attributes = ldb_msg_find_element(msg, "modules");
872 if (modules_attributes) {
873 for (i=0; i < modules_attributes->num_values; i++) {
874 struct ldb_dn *base_dn;
876 struct dsdb_control_current_partition *partition = NULL;
877 const char **modules = NULL;
879 char *base = talloc_strdup(data->partitions, (char *)modules_attributes->values[i].data);
880 char *p = strchr(base, ':');
882 ldb_asprintf_errstring(module->ldb,
884 "invalid form for partition module record (missing ':'): %s", base);
885 talloc_free(mem_ctx);
886 return LDB_ERR_CONSTRAINT_VIOLATION;
891 ldb_asprintf_errstring(module->ldb,
893 "invalid form for partition module record (missing backend database): %s", base);
894 talloc_free(mem_ctx);
895 return LDB_ERR_CONSTRAINT_VIOLATION;
898 modules = ldb_modules_list_from_string(module->ldb, mem_ctx,
901 base_dn = ldb_dn_new(mem_ctx, module->ldb, base);
902 if (!ldb_dn_validate(base_dn)) {
903 talloc_free(mem_ctx);
904 return LDB_ERR_OPERATIONS_ERROR;
907 for (partition_idx = 0; data->partitions[partition_idx]; partition_idx++) {
908 if (ldb_dn_compare(data->partitions[partition_idx]->dn, base_dn) == 0) {
909 partition = data->partitions[partition_idx];
915 ldb_asprintf_errstring(module->ldb,
917 "invalid form for partition module record (no such partition): %s", base);
918 talloc_free(mem_ctx);
919 return LDB_ERR_CONSTRAINT_VIOLATION;
922 ret = ldb_load_modules_list(module->ldb, modules, partition->module, &partition->module);
923 if (ret != LDB_SUCCESS) {
924 ldb_asprintf_errstring(module->ldb,
926 "loading backend for %s failed: %s",
927 base, ldb_errstring(module->ldb));
928 talloc_free(mem_ctx);
931 ret = ldb_init_module_chain(module->ldb, partition->module);
932 if (ret != LDB_SUCCESS) {
933 ldb_asprintf_errstring(module->ldb,
935 "initialising backend for %s failed: %s",
936 base, ldb_errstring(module->ldb));
937 talloc_free(mem_ctx);
943 talloc_free(mem_ctx);
944 return ldb_next_init(module);
947 static int partition_wait_none(struct ldb_handle *handle) {
948 struct partition_context *ac;
952 if (!handle || !handle->private_data) {
953 return LDB_ERR_OPERATIONS_ERROR;
956 if (handle->state == LDB_ASYNC_DONE) {
957 return handle->status;
960 handle->state = LDB_ASYNC_PENDING;
961 handle->status = LDB_SUCCESS;
963 ac = talloc_get_type(handle->private_data, struct partition_context);
965 for (i=0; i < ac->num_requests; i++) {
966 ret = ldb_wait(ac->down_req[i]->handle, LDB_WAIT_NONE);
968 if (ret != LDB_SUCCESS) {
969 handle->status = ret;
972 if (ac->down_req[i]->handle->status != LDB_SUCCESS) {
973 handle->status = ac->down_req[i]->handle->status;
977 if (ac->down_req[i]->handle->state != LDB_ASYNC_DONE) {
985 handle->state = LDB_ASYNC_DONE;
990 static int partition_wait_all(struct ldb_handle *handle) {
994 while (handle->state != LDB_ASYNC_DONE) {
995 ret = partition_wait_none(handle);
996 if (ret != LDB_SUCCESS) {
1001 return handle->status;
1004 static int partition_wait(struct ldb_handle *handle, enum ldb_wait_type type)
1006 if (type == LDB_WAIT_ALL) {
1007 return partition_wait_all(handle);
1009 return partition_wait_none(handle);
1013 static const struct ldb_module_ops partition_ops = {
1014 .name = "partition",
1015 .init_context = partition_init,
1016 .search = partition_search,
1017 .add = partition_add,
1018 .modify = partition_modify,
1019 .del = partition_delete,
1020 .rename = partition_rename,
1021 .extended = partition_extended,
1022 .sequence_number = partition_sequence_number,
1023 .start_transaction = partition_start_trans,
1024 .end_transaction = partition_end_trans,
1025 .del_transaction = partition_del_trans,
1026 .wait = partition_wait
1029 int ldb_partition_init(void)
1031 return ldb_register_module(&partition_ops);