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"
35 struct ldb_module *module;
36 struct ldb_request *req;
39 struct partition_context {
40 struct ldb_module *module;
41 struct ldb_request *req;
44 struct part_request *part_req;
46 int finished_requests;
49 static struct partition_context *partition_init_ctx(struct ldb_module *module, struct ldb_request *req)
51 struct partition_context *ac;
53 ac = talloc_zero(req, struct partition_context);
55 ldb_set_errstring(ldb_module_get_ctx(module), "Out of Memory");
66 * helper functions to call the next module in chain
69 int partition_request(struct ldb_module *module, struct ldb_request *request)
72 switch (request->operation) {
74 PARTITION_FIND_OP(module, search);
75 ret = module->ops->search(module, request);
78 PARTITION_FIND_OP(module, add);
79 ret = module->ops->add(module, request);
82 PARTITION_FIND_OP(module, modify);
83 ret = module->ops->modify(module, request);
86 PARTITION_FIND_OP(module, del);
87 ret = module->ops->del(module, request);
90 PARTITION_FIND_OP(module, rename);
91 ret = module->ops->rename(module, request);
94 PARTITION_FIND_OP(module, extended);
95 ret = module->ops->extended(module, request);
98 PARTITION_FIND_OP(module, request);
99 ret = module->ops->request(module, request);
102 if (ret == LDB_SUCCESS) {
105 if (!ldb_errstring(ldb_module_get_ctx(module))) {
106 /* Set a default error string, to place the blame somewhere */
107 ldb_asprintf_errstring(ldb_module_get_ctx(module),
108 "error in module %s: %s (%d)",
110 ldb_strerror(ret), ret);
115 static struct dsdb_partition *find_partition(struct partition_private_data *data,
117 struct ldb_request *req)
120 struct ldb_control *partition_ctrl;
122 /* see if the request has the partition DN specified in a
123 * control. The repl_meta_data module can specify this to
124 * ensure that replication happens to the right partition
126 partition_ctrl = ldb_request_get_control(req, DSDB_CONTROL_CURRENT_PARTITION_OID);
127 if (partition_ctrl) {
128 const struct dsdb_control_current_partition *partition;
129 partition = talloc_get_type(partition_ctrl->data,
130 struct dsdb_control_current_partition);
131 if (partition != NULL) {
140 /* Look at base DN */
141 /* Figure out which partition it is under */
142 /* Skip the lot if 'data' isn't here yet (initialisation) */
143 for (i=0; data && data->partitions && data->partitions[i]; i++) {
144 if (ldb_dn_compare_base(data->partitions[i]->ctrl->dn, dn) == 0) {
145 return data->partitions[i];
153 * fire the caller's callback for every entry, but only send 'done' once.
155 static int partition_req_callback(struct ldb_request *req,
156 struct ldb_reply *ares)
158 struct partition_context *ac;
159 struct ldb_module *module;
160 struct ldb_request *nreq;
162 struct partition_private_data *data;
163 struct ldb_control *partition_ctrl;
165 ac = talloc_get_type(req->context, struct partition_context);
166 data = talloc_get_type(ac->module->private_data, struct partition_private_data);
169 return ldb_module_done(ac->req, NULL, NULL,
170 LDB_ERR_OPERATIONS_ERROR);
173 partition_ctrl = ldb_request_get_control(req, DSDB_CONTROL_CURRENT_PARTITION_OID);
174 if (partition_ctrl && (ac->num_requests == 1 || ares->type == LDB_REPLY_ENTRY)) {
175 /* If we didn't fan this request out to mulitple partitions,
176 * or this is an individual search result, we can
177 * deterministily tell the caller what partition this was
178 * written to (repl_meta_data likes to know) */
179 ret = ldb_reply_add_control(ares,
180 DSDB_CONTROL_CURRENT_PARTITION_OID,
181 false, partition_ctrl->data);
182 if (ret != LDB_SUCCESS) {
183 return ldb_module_done(ac->req, NULL, NULL,
188 if (ares->error != LDB_SUCCESS && !ac->got_success) {
189 return ldb_module_done(ac->req, ares->controls,
190 ares->response, ares->error);
193 switch (ares->type) {
194 case LDB_REPLY_REFERRAL:
195 /* ignore referrals for now */
198 case LDB_REPLY_ENTRY:
199 if (ac->req->operation != LDB_SEARCH) {
200 ldb_set_errstring(ldb_module_get_ctx(ac->module),
201 "partition_req_callback:"
202 " Unsupported reply type for this request");
203 return ldb_module_done(ac->req, NULL, NULL,
204 LDB_ERR_OPERATIONS_ERROR);
207 return ldb_module_send_entry(ac->req, ares->message, ares->controls);
210 if (ares->error == LDB_SUCCESS) {
211 ac->got_success = true;
213 if (ac->req->operation == LDB_EXTENDED) {
214 /* FIXME: check for ares->response, replmd does not fill it ! */
215 if (ares->response) {
216 if (strcmp(ares->response->oid, LDB_EXTENDED_START_TLS_OID) != 0) {
217 ldb_set_errstring(ldb_module_get_ctx(ac->module),
218 "partition_req_callback:"
219 " Unknown extended reply, "
220 "only supports START_TLS");
222 return ldb_module_done(ac->req, NULL, NULL,
223 LDB_ERR_OPERATIONS_ERROR);
228 ac->finished_requests++;
229 if (ac->finished_requests == ac->num_requests) {
230 /* this was the last one, call callback */
231 return ldb_module_done(ac->req, ares->controls,
233 ac->got_success?LDB_SUCCESS:ares->error);
236 /* not the last, now call the next one */
237 module = ac->part_req[ac->finished_requests].module;
238 nreq = ac->part_req[ac->finished_requests].req;
240 ret = partition_request(module, nreq);
241 if (ret != LDB_SUCCESS) {
243 return ldb_module_done(ac->req, NULL, NULL, ret);
253 static int partition_prep_request(struct partition_context *ac,
254 struct dsdb_partition *partition)
257 struct ldb_request *req;
259 ac->part_req = talloc_realloc(ac, ac->part_req,
261 ac->num_requests + 1);
262 if (ac->part_req == NULL) {
263 ldb_oom(ldb_module_get_ctx(ac->module));
264 return LDB_ERR_OPERATIONS_ERROR;
267 switch (ac->req->operation) {
269 ret = ldb_build_search_req_ex(&req, ldb_module_get_ctx(ac->module),
271 ac->req->op.search.base,
272 ac->req->op.search.scope,
273 ac->req->op.search.tree,
274 ac->req->op.search.attrs,
276 ac, partition_req_callback,
280 ret = ldb_build_add_req(&req, ldb_module_get_ctx(ac->module), ac->part_req,
281 ac->req->op.add.message,
283 ac, partition_req_callback,
287 ret = ldb_build_mod_req(&req, ldb_module_get_ctx(ac->module), ac->part_req,
288 ac->req->op.mod.message,
290 ac, partition_req_callback,
294 ret = ldb_build_del_req(&req, ldb_module_get_ctx(ac->module), ac->part_req,
297 ac, partition_req_callback,
301 ret = ldb_build_rename_req(&req, ldb_module_get_ctx(ac->module), ac->part_req,
302 ac->req->op.rename.olddn,
303 ac->req->op.rename.newdn,
305 ac, partition_req_callback,
309 ret = ldb_build_extended_req(&req, ldb_module_get_ctx(ac->module),
311 ac->req->op.extended.oid,
312 ac->req->op.extended.data,
314 ac, partition_req_callback,
318 ldb_set_errstring(ldb_module_get_ctx(ac->module),
319 "Unsupported request type!");
320 ret = LDB_ERR_UNWILLING_TO_PERFORM;
323 if (ret != LDB_SUCCESS) {
327 ac->part_req[ac->num_requests].req = req;
329 if (ac->req->controls) {
330 req->controls = talloc_memdup(req, ac->req->controls,
331 talloc_get_size(ac->req->controls));
332 if (req->controls == NULL) {
333 ldb_oom(ldb_module_get_ctx(ac->module));
334 return LDB_ERR_OPERATIONS_ERROR;
339 ac->part_req[ac->num_requests].module = partition->module;
341 if (!ldb_request_get_control(req, DSDB_CONTROL_CURRENT_PARTITION_OID)) {
342 ret = ldb_request_add_control(req,
343 DSDB_CONTROL_CURRENT_PARTITION_OID,
344 false, partition->ctrl);
345 if (ret != LDB_SUCCESS) {
350 if (req->operation == LDB_SEARCH) {
351 /* If the search is for 'more' than this partition,
352 * then change the basedn, so a remote LDAP server
354 if (ldb_dn_compare_base(partition->ctrl->dn,
355 req->op.search.base) != 0) {
356 req->op.search.base = partition->ctrl->dn;
361 /* make sure you put the NEXT module here, or
362 * partition_request() will simply loop forever on itself */
363 ac->part_req[ac->num_requests].module = ac->module->next;
371 static int partition_call_first(struct partition_context *ac)
373 return partition_request(ac->part_req[0].module, ac->part_req[0].req);
377 * Send a request down to all the partitions
379 static int partition_send_all(struct ldb_module *module,
380 struct partition_context *ac,
381 struct ldb_request *req)
384 struct partition_private_data *data = talloc_get_type(module->private_data,
385 struct partition_private_data);
386 int ret = partition_prep_request(ac, NULL);
387 if (ret != LDB_SUCCESS) {
390 for (i=0; data && data->partitions && data->partitions[i]; i++) {
391 ret = partition_prep_request(ac, data->partitions[i]);
392 if (ret != LDB_SUCCESS) {
397 /* fire the first one */
398 return partition_call_first(ac);
402 * Figure out which backend a request needs to be aimed at. Some
403 * requests must be replicated to all backends
405 static int partition_replicate(struct ldb_module *module, struct ldb_request *req, struct ldb_dn *dn)
407 struct partition_context *ac;
410 struct dsdb_partition *partition;
411 struct partition_private_data *data = talloc_get_type(module->private_data,
412 struct partition_private_data);
413 if (!data || !data->partitions) {
414 return ldb_next_request(module, req);
417 if (req->operation != LDB_SEARCH) {
418 /* Is this a special DN, we need to replicate to every backend? */
419 for (i=0; data->replicate && data->replicate[i]; i++) {
420 if (ldb_dn_compare(data->replicate[i],
423 ac = partition_init_ctx(module, req);
425 return LDB_ERR_OPERATIONS_ERROR;
428 return partition_send_all(module, ac, req);
433 /* Otherwise, we need to find the partition to fire it to */
436 partition = find_partition(data, dn, req);
439 * if we haven't found a matching partition
440 * pass the request to the main ldb
442 * TODO: we should maybe return an error here
443 * if it's not a special dn
446 return ldb_next_request(module, req);
449 ac = partition_init_ctx(module, req);
451 return LDB_ERR_OPERATIONS_ERROR;
454 /* we need to add a control but we never touch the original request */
455 ret = partition_prep_request(ac, partition);
456 if (ret != LDB_SUCCESS) {
460 /* fire the first one */
461 return partition_call_first(ac);
465 static int partition_search(struct ldb_module *module, struct ldb_request *req)
468 struct ldb_control **saved_controls;
470 struct partition_private_data *data = talloc_get_type(module->private_data,
471 struct partition_private_data);
475 /* (later) consider if we should be searching multiple
476 * partitions (for 'invisible' partition behaviour */
478 struct ldb_control *search_control = ldb_request_get_control(req, LDB_CONTROL_SEARCH_OPTIONS_OID);
479 struct ldb_control *domain_scope_control = ldb_request_get_control(req, LDB_CONTROL_DOMAIN_SCOPE_OID);
481 struct ldb_search_options_control *search_options = NULL;
482 struct dsdb_partition *p;
484 ret = partition_reload_if_required(module, data);
485 if (ret != LDB_SUCCESS) {
489 p = find_partition(data, NULL, req);
491 /* the caller specified what partition they want the
492 * search - just pass it on
494 return ldb_next_request(p->module, req);
498 if (search_control) {
499 search_options = talloc_get_type(search_control->data, struct ldb_search_options_control);
502 /* Remove the domain_scope control, so we don't confuse a backend server */
503 if (domain_scope_control && !save_controls(domain_scope_control, req, &saved_controls)) {
504 ldb_oom(ldb_module_get_ctx(module));
505 return LDB_ERR_OPERATIONS_ERROR;
509 * for now pass down the LDB_CONTROL_SEARCH_OPTIONS_OID control
510 * down as uncritical to make windows 2008 dcpromo happy.
512 if (search_control) {
513 search_control->critical = 0;
517 Generate referrals (look for a partition under this DN) if we don't have the above control specified
520 if (search_options && (search_options->search_options & LDB_SEARCH_OPTION_PHANTOM_ROOT)) {
522 struct partition_context *ac;
523 if ((search_options->search_options & ~LDB_SEARCH_OPTION_PHANTOM_ROOT) == 0) {
524 /* We have processed this flag, so we are done with this control now */
526 /* Remove search control, so we don't confuse a backend server */
527 if (search_control && !save_controls(search_control, req, &saved_controls)) {
528 ldb_oom(ldb_module_get_ctx(module));
529 return LDB_ERR_OPERATIONS_ERROR;
532 ac = partition_init_ctx(module, req);
534 return LDB_ERR_OPERATIONS_ERROR;
537 /* Search from the base DN */
538 if (!req->op.search.base || ldb_dn_is_null(req->op.search.base)) {
539 return partition_send_all(module, ac, req);
541 for (i=0; data && data->partitions && data->partitions[i]; i++) {
542 bool match = false, stop = false;
543 /* Find all partitions under the search base
547 1) the DN we are looking for exactly matches the partition
549 2) the DN we are looking for is a parent of the partition and it isn't
552 3) the DN we are looking for is a child of the partition
554 if (ldb_dn_compare(data->partitions[i]->ctrl->dn, req->op.search.base) == 0) {
556 if (req->op.search.scope == LDB_SCOPE_BASE) {
561 (ldb_dn_compare_base(req->op.search.base, data->partitions[i]->ctrl->dn) == 0 &&
562 req->op.search.scope != LDB_SCOPE_BASE)) {
566 ldb_dn_compare_base(data->partitions[i]->ctrl->dn, req->op.search.base) == 0) {
568 stop = true; /* note that this relies on partition ordering */
571 ret = partition_prep_request(ac, data->partitions[i]);
572 if (ret != LDB_SUCCESS) {
579 /* Perhaps we didn't match any partitions. Try the main partition, only */
580 if (ac->num_requests == 0) {
582 return ldb_next_request(module, req);
585 /* fire the first one */
586 return partition_call_first(ac);
589 /* Handle this like all other requests */
590 if (search_control && (search_options->search_options & ~LDB_SEARCH_OPTION_PHANTOM_ROOT) == 0) {
591 /* We have processed this flag, so we are done with this control now */
593 /* Remove search control, so we don't confuse a backend server */
594 if (search_control && !save_controls(search_control, req, &saved_controls)) {
595 ldb_oom(ldb_module_get_ctx(module));
596 return LDB_ERR_OPERATIONS_ERROR;
600 return partition_replicate(module, req, req->op.search.base);
605 static int partition_add(struct ldb_module *module, struct ldb_request *req)
607 return partition_replicate(module, req, req->op.add.message->dn);
611 static int partition_modify(struct ldb_module *module, struct ldb_request *req)
613 return partition_replicate(module, req, req->op.mod.message->dn);
617 static int partition_delete(struct ldb_module *module, struct ldb_request *req)
619 return partition_replicate(module, req, req->op.del.dn);
623 static int partition_rename(struct ldb_module *module, struct ldb_request *req)
626 struct dsdb_partition *backend, *backend2;
628 struct partition_private_data *data = talloc_get_type(module->private_data,
629 struct partition_private_data);
631 /* Skip the lot if 'data' isn't here yet (initialisation) */
633 return LDB_ERR_OPERATIONS_ERROR;
636 backend = find_partition(data, req->op.rename.olddn, req);
637 backend2 = find_partition(data, req->op.rename.newdn, req);
639 if ((backend && !backend2) || (!backend && backend2)) {
640 return LDB_ERR_AFFECTS_MULTIPLE_DSAS;
643 if (backend != backend2) {
644 ldb_asprintf_errstring(ldb_module_get_ctx(module),
645 "Cannot rename from %s in %s to %s in %s: %s",
646 ldb_dn_get_linearized(req->op.rename.olddn),
647 ldb_dn_get_linearized(backend->ctrl->dn),
648 ldb_dn_get_linearized(req->op.rename.newdn),
649 ldb_dn_get_linearized(backend2->ctrl->dn),
650 ldb_strerror(LDB_ERR_AFFECTS_MULTIPLE_DSAS));
651 return LDB_ERR_AFFECTS_MULTIPLE_DSAS;
654 return partition_replicate(module, req, req->op.rename.olddn);
657 /* start a transaction */
658 static int partition_start_trans(struct ldb_module *module)
661 struct partition_private_data *data = talloc_get_type(module->private_data,
662 struct partition_private_data);
663 /* Look at base DN */
664 /* Figure out which partition it is under */
665 /* Skip the lot if 'data' isn't here yet (initialization) */
666 ret = ldb_next_start_trans(module);
667 if (ret != LDB_SUCCESS) {
671 ret = partition_reload_if_required(module, data);
672 if (ret != LDB_SUCCESS) {
676 for (i=0; data && data->partitions && data->partitions[i]; i++) {
677 struct ldb_module *next = data->partitions[i]->module;
678 PARTITION_FIND_OP(next, start_transaction);
680 ret = next->ops->start_transaction(next);
681 if (ret != LDB_SUCCESS) {
682 /* Back it out, if it fails on one */
683 for (i--; i >= 0; i--) {
684 next = data->partitions[i]->module;
685 PARTITION_FIND_OP(next, del_transaction);
687 next->ops->del_transaction(next);
689 ldb_next_del_trans(module);
696 /* prepare for a commit */
697 static int partition_prepare_commit(struct ldb_module *module)
700 struct partition_private_data *data = talloc_get_type(module->private_data,
701 struct partition_private_data);
703 for (i=0; data && data->partitions && data->partitions[i]; i++) {
704 struct ldb_module *next_prepare = data->partitions[i]->module;
707 PARTITION_FIND_OP_NOERROR(next_prepare, prepare_commit);
708 if (next_prepare == NULL) {
712 ret = next_prepare->ops->prepare_commit(next_prepare);
713 if (ret != LDB_SUCCESS) {
718 return ldb_next_prepare_commit(module);
722 /* end a transaction */
723 static int partition_end_trans(struct ldb_module *module)
726 struct partition_private_data *data = talloc_get_type(module->private_data,
727 struct partition_private_data);
728 for (i=0; data && data->partitions && data->partitions[i]; i++) {
729 struct ldb_module *next_end = data->partitions[i]->module;
732 PARTITION_FIND_OP(next_end, end_transaction);
734 ret = next_end->ops->end_transaction(next_end);
735 if (ret != LDB_SUCCESS) {
740 return ldb_next_end_trans(module);
743 /* delete a transaction */
744 static int partition_del_trans(struct ldb_module *module)
746 int i, ret, final_ret = LDB_SUCCESS;
747 struct partition_private_data *data = talloc_get_type(module->private_data,
748 struct partition_private_data);
749 for (i=0; data && data->partitions && data->partitions[i]; i++) {
750 struct ldb_module *next = data->partitions[i]->module;
751 PARTITION_FIND_OP(next, del_transaction);
753 ret = next->ops->del_transaction(next);
754 if (ret != LDB_SUCCESS) {
759 ret = ldb_next_del_trans(module);
760 if (ret != LDB_SUCCESS) {
766 int partition_primary_sequence_number(struct ldb_module *module, TALLOC_CTX *mem_ctx,
767 enum ldb_sequence_type type, uint64_t *seq_number)
770 struct ldb_result *res;
771 struct ldb_seqnum_request *tseq;
772 struct ldb_request *treq;
773 struct ldb_seqnum_result *seqr;
774 res = talloc_zero(mem_ctx, struct ldb_result);
776 return LDB_ERR_OPERATIONS_ERROR;
778 tseq = talloc_zero(res, struct ldb_seqnum_request);
781 return LDB_ERR_OPERATIONS_ERROR;
785 ret = ldb_build_extended_req(&treq, ldb_module_get_ctx(module), res,
786 LDB_EXTENDED_SEQUENCE_NUMBER,
790 ldb_extended_default_callback,
792 if (ret != LDB_SUCCESS) {
797 ret = ldb_next_request(module, treq);
798 if (ret != LDB_SUCCESS) {
802 ret = ldb_wait(treq->handle, LDB_WAIT_ALL);
803 if (ret != LDB_SUCCESS) {
808 seqr = talloc_get_type(res->extended->data,
809 struct ldb_seqnum_result);
810 if (seqr->flags & LDB_SEQ_TIMESTAMP_SEQUENCE) {
811 ret = LDB_ERR_OPERATIONS_ERROR;
812 ldb_set_errstring(ldb_module_get_ctx(module), "Primary backend in partitions module returned a timestamp based seq number (must return a normal number)");
816 *seq_number = seqr->seq_num;
822 /* FIXME: This function is still semi-async */
823 static int partition_sequence_number(struct ldb_module *module, struct ldb_request *req)
826 uint64_t seq_number = 0;
827 uint64_t timestamp_sequence = 0;
828 uint64_t timestamp = 0;
829 struct partition_private_data *data = talloc_get_type(module->private_data,
830 struct partition_private_data);
831 struct ldb_seqnum_request *seq;
832 struct ldb_seqnum_result *seqr;
833 struct ldb_request *treq;
834 struct ldb_seqnum_request *tseq;
835 struct ldb_seqnum_result *tseqr;
836 struct ldb_extended *ext;
837 struct ldb_result *res;
838 struct dsdb_partition *p;
840 p = find_partition(data, NULL, req);
842 /* the caller specified what partition they want the
843 * sequence number operation on - just pass it on
845 return ldb_next_request(p->module, req);
848 seq = talloc_get_type(req->op.extended.data, struct ldb_seqnum_request);
852 case LDB_SEQ_HIGHEST_SEQ:
854 ret = partition_primary_sequence_number(module, req, seq->type, &seq_number);
855 if (ret != LDB_SUCCESS) {
859 /* Skip the lot if 'data' isn't here yet (initialisation) */
860 for (i=0; data && data->partitions && data->partitions[i]; i++) {
862 res = talloc_zero(req, struct ldb_result);
864 return LDB_ERR_OPERATIONS_ERROR;
866 tseq = talloc_zero(res, struct ldb_seqnum_request);
869 return LDB_ERR_OPERATIONS_ERROR;
871 tseq->type = seq->type;
873 ret = ldb_build_extended_req(&treq, ldb_module_get_ctx(module), res,
874 LDB_EXTENDED_SEQUENCE_NUMBER,
878 ldb_extended_default_callback,
880 if (ret != LDB_SUCCESS) {
885 if (!ldb_request_get_control(treq, DSDB_CONTROL_CURRENT_PARTITION_OID)) {
886 ret = ldb_request_add_control(treq,
887 DSDB_CONTROL_CURRENT_PARTITION_OID,
888 false, data->partitions[i]->ctrl);
889 if (ret != LDB_SUCCESS) {
895 ret = partition_request(data->partitions[i]->module, treq);
896 if (ret != LDB_SUCCESS) {
900 ret = ldb_wait(treq->handle, LDB_WAIT_ALL);
901 if (ret != LDB_SUCCESS) {
905 tseqr = talloc_get_type(res->extended->data,
906 struct ldb_seqnum_result);
907 if (tseqr->flags & LDB_SEQ_TIMESTAMP_SEQUENCE) {
908 timestamp_sequence = MAX(timestamp_sequence,
911 seq_number += tseqr->seq_num;
916 case LDB_SEQ_HIGHEST_TIMESTAMP:
918 res = talloc_zero(req, struct ldb_result);
920 return LDB_ERR_OPERATIONS_ERROR;
923 tseq = talloc_zero(res, struct ldb_seqnum_request);
926 return LDB_ERR_OPERATIONS_ERROR;
928 tseq->type = LDB_SEQ_HIGHEST_TIMESTAMP;
930 ret = ldb_build_extended_req(&treq, ldb_module_get_ctx(module), res,
931 LDB_EXTENDED_SEQUENCE_NUMBER,
935 ldb_extended_default_callback,
937 if (ret != LDB_SUCCESS) {
942 ret = ldb_next_request(module, treq);
943 if (ret != LDB_SUCCESS) {
947 ret = ldb_wait(treq->handle, LDB_WAIT_ALL);
948 if (ret != LDB_SUCCESS) {
953 tseqr = talloc_get_type(res->extended->data,
954 struct ldb_seqnum_result);
955 timestamp = tseqr->seq_num;
959 /* Skip the lot if 'data' isn't here yet (initialisation) */
960 for (i=0; data && data->partitions && data->partitions[i]; i++) {
962 res = talloc_zero(req, struct ldb_result);
964 return LDB_ERR_OPERATIONS_ERROR;
967 tseq = talloc_zero(res, struct ldb_seqnum_request);
970 return LDB_ERR_OPERATIONS_ERROR;
972 tseq->type = LDB_SEQ_HIGHEST_TIMESTAMP;
974 ret = ldb_build_extended_req(&treq, ldb_module_get_ctx(module), res,
975 LDB_EXTENDED_SEQUENCE_NUMBER,
979 ldb_extended_default_callback,
981 if (ret != LDB_SUCCESS) {
986 if (!ldb_request_get_control(treq, DSDB_CONTROL_CURRENT_PARTITION_OID)) {
987 ret = ldb_request_add_control(treq,
988 DSDB_CONTROL_CURRENT_PARTITION_OID,
989 false, data->partitions[i]->ctrl);
990 if (ret != LDB_SUCCESS) {
996 ret = partition_request(data->partitions[i]->module, treq);
997 if (ret != LDB_SUCCESS) {
1001 ret = ldb_wait(treq->handle, LDB_WAIT_ALL);
1002 if (ret != LDB_SUCCESS) {
1007 tseqr = talloc_get_type(res->extended->data,
1008 struct ldb_seqnum_result);
1009 timestamp = MAX(timestamp, tseqr->seq_num);
1017 ext = talloc_zero(req, struct ldb_extended);
1019 return LDB_ERR_OPERATIONS_ERROR;
1021 seqr = talloc_zero(ext, struct ldb_seqnum_result);
1024 return LDB_ERR_OPERATIONS_ERROR;
1026 ext->oid = LDB_EXTENDED_SEQUENCE_NUMBER;
1029 switch (seq->type) {
1031 case LDB_SEQ_HIGHEST_SEQ:
1033 /* Has someone above set a timebase sequence? */
1034 if (timestamp_sequence) {
1035 seqr->seq_num = (((unsigned long long)timestamp << 24) | (seq_number & 0xFFFFFF));
1037 seqr->seq_num = seq_number;
1040 if (timestamp_sequence > seqr->seq_num) {
1041 seqr->seq_num = timestamp_sequence;
1042 seqr->flags |= LDB_SEQ_TIMESTAMP_SEQUENCE;
1045 seqr->flags |= LDB_SEQ_GLOBAL_SEQUENCE;
1047 case LDB_SEQ_HIGHEST_TIMESTAMP:
1048 seqr->seq_num = timestamp;
1052 if (seq->type == LDB_SEQ_NEXT) {
1056 /* send request done */
1057 return ldb_module_done(req, NULL, ext, LDB_SUCCESS);
1060 static int partition_extended_schema_update_now(struct ldb_module *module, struct ldb_request *req)
1062 struct dsdb_partition *partition;
1063 struct partition_private_data *data;
1064 struct ldb_dn *schema_dn;
1065 struct partition_context *ac;
1068 schema_dn = talloc_get_type(req->op.extended.data, struct ldb_dn);
1070 ldb_debug(ldb_module_get_ctx(module), LDB_DEBUG_FATAL, "partition_extended: invalid extended data\n");
1071 return LDB_ERR_PROTOCOL_ERROR;
1074 data = talloc_get_type(module->private_data, struct partition_private_data);
1076 return LDB_ERR_OPERATIONS_ERROR;
1079 partition = find_partition( data, schema_dn, req);
1081 return ldb_next_request(module, req);
1084 ac = partition_init_ctx(module, req);
1086 return LDB_ERR_OPERATIONS_ERROR;
1089 /* we need to add a control but we never touch the original request */
1090 ret = partition_prep_request(ac, partition);
1091 if (ret != LDB_SUCCESS) {
1095 /* fire the first one */
1096 ret = partition_call_first(ac);
1098 if (ret != LDB_SUCCESS){
1102 return ldb_request_done(req, ret);
1107 static int partition_extended(struct ldb_module *module, struct ldb_request *req)
1109 struct partition_private_data *data;
1110 struct partition_context *ac;
1113 data = talloc_get_type(module->private_data, struct partition_private_data);
1115 return ldb_next_request(module, req);
1118 ret = partition_reload_if_required(module, data);
1119 if (ret != LDB_SUCCESS) {
1123 if (strcmp(req->op.extended.oid, LDB_EXTENDED_SEQUENCE_NUMBER) == 0) {
1124 return partition_sequence_number(module, req);
1127 if (strcmp(req->op.extended.oid, DSDB_EXTENDED_CREATE_PARTITION_OID) == 0) {
1128 return partition_create(module, req);
1131 /* forward schemaUpdateNow operation to schema_fsmo module*/
1132 if (strcmp(req->op.extended.oid, DSDB_EXTENDED_SCHEMA_UPDATE_NOW_OID) == 0) {
1133 return partition_extended_schema_update_now( module, req );
1137 * as the extended operation has no dn
1138 * we need to send it to all partitions
1141 ac = partition_init_ctx(module, req);
1143 return LDB_ERR_OPERATIONS_ERROR;
1146 return partition_send_all(module, ac, req);
1149 _PUBLIC_ const struct ldb_module_ops ldb_partition_module_ops = {
1150 .name = "partition",
1151 .init_context = partition_init,
1152 .search = partition_search,
1153 .add = partition_add,
1154 .modify = partition_modify,
1155 .del = partition_delete,
1156 .rename = partition_rename,
1157 .extended = partition_extended,
1158 .start_transaction = partition_start_trans,
1159 .prepare_commit = partition_prepare_commit,
1160 .end_transaction = partition_end_trans,
1161 .del_transaction = partition_del_trans,