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;
43 struct part_request *part_req;
44 unsigned int num_requests;
45 unsigned int finished_requests;
47 const char **referrals;
50 static struct partition_context *partition_init_ctx(struct ldb_module *module, struct ldb_request *req)
52 struct partition_context *ac;
54 ac = talloc_zero(req, struct partition_context);
56 ldb_set_errstring(ldb_module_get_ctx(module), "Out of Memory");
67 * helper functions to call the next module in chain
69 int partition_request(struct ldb_module *module, struct ldb_request *request)
71 if ((module && ldb_module_flags(ldb_module_get_ctx(module)) & LDB_FLG_ENABLE_TRACING)) { \
72 const struct dsdb_control_current_partition *partition = NULL;
73 struct ldb_control *partition_ctrl = ldb_request_get_control(request, DSDB_CONTROL_CURRENT_PARTITION_OID);
75 partition = talloc_get_type(partition_ctrl->data,
76 struct dsdb_control_current_partition);
79 if (partition != NULL) {
80 ldb_debug(ldb_module_get_ctx(module), LDB_DEBUG_TRACE, "partition_request() -> %s",
81 ldb_dn_get_linearized(partition->dn));
83 ldb_debug(ldb_module_get_ctx(module), LDB_DEBUG_TRACE, "partition_request() -> (metadata partition)");
87 return ldb_next_request(module, request);
90 static struct dsdb_partition *find_partition(struct partition_private_data *data,
92 struct ldb_request *req)
95 struct ldb_control *partition_ctrl;
97 /* see if the request has the partition DN specified in a
98 * control. The repl_meta_data module can specify this to
99 * ensure that replication happens to the right partition
101 partition_ctrl = ldb_request_get_control(req, DSDB_CONTROL_CURRENT_PARTITION_OID);
102 if (partition_ctrl) {
103 const struct dsdb_control_current_partition *partition;
104 partition = talloc_get_type(partition_ctrl->data,
105 struct dsdb_control_current_partition);
106 if (partition != NULL) {
115 /* Look at base DN */
116 /* Figure out which partition it is under */
117 /* Skip the lot if 'data' isn't here yet (initialisation) */
118 for (i=0; data && data->partitions && data->partitions[i]; i++) {
119 if (ldb_dn_compare_base(data->partitions[i]->ctrl->dn, dn) == 0) {
120 return data->partitions[i];
128 * fire the caller's callback for every entry, but only send 'done' once.
130 static int partition_req_callback(struct ldb_request *req,
131 struct ldb_reply *ares)
133 struct partition_context *ac;
134 struct ldb_module *module;
135 struct ldb_request *nreq;
137 struct partition_private_data *data;
138 struct ldb_control *partition_ctrl;
140 ac = talloc_get_type(req->context, struct partition_context);
141 data = talloc_get_type(ldb_module_get_private(ac->module), struct partition_private_data);
144 return ldb_module_done(ac->req, NULL, NULL,
145 LDB_ERR_OPERATIONS_ERROR);
148 partition_ctrl = ldb_request_get_control(req, DSDB_CONTROL_CURRENT_PARTITION_OID);
149 if (partition_ctrl && (ac->num_requests == 1 || ares->type == LDB_REPLY_ENTRY)) {
150 /* If we didn't fan this request out to mulitple partitions,
151 * or this is an individual search result, we can
152 * deterministically tell the caller what partition this was
153 * written to (repl_meta_data likes to know) */
154 ret = ldb_reply_add_control(ares,
155 DSDB_CONTROL_CURRENT_PARTITION_OID,
156 false, partition_ctrl->data);
157 if (ret != LDB_SUCCESS) {
158 return ldb_module_done(ac->req, NULL, NULL,
163 if (ares->error != LDB_SUCCESS) {
164 return ldb_module_done(ac->req, ares->controls,
165 ares->response, ares->error);
168 switch (ares->type) {
169 case LDB_REPLY_REFERRAL:
170 return ldb_module_send_referral(ac->req, ares->referral);
172 case LDB_REPLY_ENTRY:
173 if (ac->req->operation != LDB_SEARCH) {
174 ldb_set_errstring(ldb_module_get_ctx(ac->module),
175 "partition_req_callback:"
176 " Unsupported reply type for this request");
177 return ldb_module_done(ac->req, NULL, NULL,
178 LDB_ERR_OPERATIONS_ERROR);
181 return ldb_module_send_entry(ac->req, ares->message, ares->controls);
184 if (ac->req->operation == LDB_EXTENDED) {
185 /* FIXME: check for ares->response, replmd does not fill it ! */
186 if (ares->response) {
187 if (strcmp(ares->response->oid, LDB_EXTENDED_START_TLS_OID) != 0) {
188 ldb_set_errstring(ldb_module_get_ctx(ac->module),
189 "partition_req_callback:"
190 " Unknown extended reply, "
191 "only supports START_TLS");
193 return ldb_module_done(ac->req, NULL, NULL,
194 LDB_ERR_OPERATIONS_ERROR);
199 ac->finished_requests++;
200 if (ac->finished_requests == ac->num_requests) {
201 /* Send back referrals if they do exist (search ops) */
202 if (ac->referrals != NULL) {
204 for (ref = ac->referrals; *ref != NULL; ++ref) {
205 ret = ldb_module_send_referral(ac->req,
206 talloc_strdup(ac->req, *ref));
207 if (ret != LDB_SUCCESS) {
208 return ldb_module_done(ac->req, NULL, NULL,
214 /* this was the last one, call callback */
215 return ldb_module_done(ac->req, ares->controls,
220 /* not the last, now call the next one */
221 module = ac->part_req[ac->finished_requests].module;
222 nreq = ac->part_req[ac->finished_requests].req;
224 ret = partition_request(module, nreq);
225 if (ret != LDB_SUCCESS) {
227 return ldb_module_done(ac->req, NULL, NULL, ret);
237 static int partition_prep_request(struct partition_context *ac,
238 struct dsdb_partition *partition)
241 struct ldb_request *req;
243 ac->part_req = talloc_realloc(ac, ac->part_req,
245 ac->num_requests + 1);
246 if (ac->part_req == NULL) {
247 return ldb_oom(ldb_module_get_ctx(ac->module));
250 switch (ac->req->operation) {
252 ret = ldb_build_search_req_ex(&req, ldb_module_get_ctx(ac->module),
254 ac->req->op.search.base,
255 ac->req->op.search.scope,
256 ac->req->op.search.tree,
257 ac->req->op.search.attrs,
259 ac, partition_req_callback,
261 LDB_REQ_SET_LOCATION(req);
264 ret = ldb_build_add_req(&req, ldb_module_get_ctx(ac->module), ac->part_req,
265 ac->req->op.add.message,
267 ac, partition_req_callback,
269 LDB_REQ_SET_LOCATION(req);
272 ret = ldb_build_mod_req(&req, ldb_module_get_ctx(ac->module), ac->part_req,
273 ac->req->op.mod.message,
275 ac, partition_req_callback,
277 LDB_REQ_SET_LOCATION(req);
280 ret = ldb_build_del_req(&req, ldb_module_get_ctx(ac->module), ac->part_req,
283 ac, partition_req_callback,
285 LDB_REQ_SET_LOCATION(req);
288 ret = ldb_build_rename_req(&req, ldb_module_get_ctx(ac->module), ac->part_req,
289 ac->req->op.rename.olddn,
290 ac->req->op.rename.newdn,
292 ac, partition_req_callback,
294 LDB_REQ_SET_LOCATION(req);
297 ret = ldb_build_extended_req(&req, ldb_module_get_ctx(ac->module),
299 ac->req->op.extended.oid,
300 ac->req->op.extended.data,
302 ac, partition_req_callback,
304 LDB_REQ_SET_LOCATION(req);
307 ldb_set_errstring(ldb_module_get_ctx(ac->module),
308 "Unsupported request type!");
309 ret = LDB_ERR_UNWILLING_TO_PERFORM;
312 if (ret != LDB_SUCCESS) {
316 ac->part_req[ac->num_requests].req = req;
318 if (ac->req->controls) {
319 req->controls = talloc_memdup(req, ac->req->controls,
320 talloc_get_size(ac->req->controls));
321 if (req->controls == NULL) {
322 return ldb_oom(ldb_module_get_ctx(ac->module));
327 ac->part_req[ac->num_requests].module = partition->module;
329 if (!ldb_request_get_control(req, DSDB_CONTROL_CURRENT_PARTITION_OID)) {
330 ret = ldb_request_add_control(req,
331 DSDB_CONTROL_CURRENT_PARTITION_OID,
332 false, partition->ctrl);
333 if (ret != LDB_SUCCESS) {
338 if (req->operation == LDB_SEARCH) {
339 /* If the search is for 'more' than this partition,
340 * then change the basedn, so a remote LDAP server
342 if (ldb_dn_compare_base(partition->ctrl->dn,
343 req->op.search.base) != 0) {
344 req->op.search.base = partition->ctrl->dn;
349 /* make sure you put the module here, or
350 * or ldb_next_request() will skip a module */
351 ac->part_req[ac->num_requests].module = ac->module;
359 static int partition_call_first(struct partition_context *ac)
361 return partition_request(ac->part_req[0].module, ac->part_req[0].req);
365 * Send a request down to all the partitions
367 static int partition_send_all(struct ldb_module *module,
368 struct partition_context *ac,
369 struct ldb_request *req)
372 struct partition_private_data *data = talloc_get_type(ldb_module_get_private(module),
373 struct partition_private_data);
374 int ret = partition_prep_request(ac, NULL);
375 if (ret != LDB_SUCCESS) {
378 for (i=0; data && data->partitions && data->partitions[i]; i++) {
379 ret = partition_prep_request(ac, data->partitions[i]);
380 if (ret != LDB_SUCCESS) {
385 /* fire the first one */
386 return partition_call_first(ac);
390 * Figure out which backend a request needs to be aimed at. Some
391 * requests must be replicated to all backends
393 static int partition_replicate(struct ldb_module *module, struct ldb_request *req, struct ldb_dn *dn)
395 struct partition_context *ac;
398 struct dsdb_partition *partition;
399 struct partition_private_data *data = talloc_get_type(ldb_module_get_private(module),
400 struct partition_private_data);
402 /* if we aren't initialised yet go further */
403 if (!data || !data->partitions) {
404 return ldb_next_request(module, req);
407 if (ldb_dn_is_special(dn)) {
408 /* Is this a special DN, we need to replicate to every backend? */
409 for (i=0; data->replicate && data->replicate[i]; i++) {
410 if (ldb_dn_compare(data->replicate[i],
413 ac = partition_init_ctx(module, req);
415 return ldb_operr(ldb_module_get_ctx(module));
418 return partition_send_all(module, ac, req);
423 /* Otherwise, we need to find the partition to fire it to */
426 partition = find_partition(data, dn, req);
429 * if we haven't found a matching partition
430 * pass the request to the main ldb
432 * TODO: we should maybe return an error here
433 * if it's not a special dn
436 return ldb_next_request(module, req);
439 ac = partition_init_ctx(module, req);
441 return ldb_operr(ldb_module_get_ctx(module));
444 /* we need to add a control but we never touch the original request */
445 ret = partition_prep_request(ac, partition);
446 if (ret != LDB_SUCCESS) {
450 /* fire the first one */
451 return partition_call_first(ac);
455 static int partition_search(struct ldb_module *module, struct ldb_request *req)
457 struct ldb_control **saved_controls;
459 struct partition_private_data *data = talloc_get_type(ldb_module_get_private(module),
460 struct partition_private_data);
461 struct partition_context *ac;
462 struct ldb_context *ldb;
463 struct loadparm_context *lp_ctx;
465 struct ldb_control *search_control = ldb_request_get_control(req, LDB_CONTROL_SEARCH_OPTIONS_OID);
466 struct ldb_control *domain_scope_control = ldb_request_get_control(req, LDB_CONTROL_DOMAIN_SCOPE_OID);
468 struct ldb_search_options_control *search_options = NULL;
469 struct dsdb_partition *p;
472 bool domain_scope = false, phantom_root = false;
474 /* see if we are still up-to-date */
475 ret = partition_reload_if_required(module, data, req);
476 if (ret != LDB_SUCCESS) {
480 p = find_partition(data, NULL, req);
482 /* the caller specified what partition they want the
483 * search - just pass it on
485 return ldb_next_request(p->module, req);
488 /* Get back the search options from the search control, and mark it as
489 * non-critical (to make backends and also dcpromo happy).
491 if (search_control) {
492 search_options = talloc_get_type(search_control->data, struct ldb_search_options_control);
493 search_control->critical = 0;
497 /* Remove the "domain_scope" control, so we don't confuse a backend
499 if (domain_scope_control && !ldb_save_controls(domain_scope_control, req, &saved_controls)) {
500 return ldb_oom(ldb_module_get_ctx(module));
503 /* if we aren't initialised yet go further */
504 if (!data || !data->partitions) {
505 return ldb_next_request(module, req);
508 /* Locate the options */
509 domain_scope = (search_options
510 && (search_options->search_options & LDB_SEARCH_OPTION_DOMAIN_SCOPE))
511 || domain_scope_control;
512 phantom_root = search_options
513 && (search_options->search_options & LDB_SEARCH_OPTION_PHANTOM_ROOT);
515 /* Remove handled options from the search control flag */
516 if (search_options) {
517 search_options->search_options = search_options->search_options
518 & ~LDB_SEARCH_OPTION_DOMAIN_SCOPE
519 & ~LDB_SEARCH_OPTION_PHANTOM_ROOT;
522 ac = partition_init_ctx(module, req);
524 return ldb_operr(ldb_module_get_ctx(module));
527 ldb = ldb_module_get_ctx(ac->module);
528 lp_ctx = talloc_get_type(ldb_get_opaque(ldb, "loadparm"),
529 struct loadparm_context);
531 /* Search from the base DN */
532 if (ldb_dn_is_null(req->op.search.base)) {
533 return partition_send_all(module, ac, req);
536 for (i=0; data->partitions[i]; i++) {
537 bool match = false, stop = false;
540 /* Phantom root: Find all partitions under the
541 * search base. We match if:
543 * 1) the DN we are looking for exactly matches a
544 * certain partition and always stop
545 * 2) the DN we are looking for is a parent of certain
546 * partitions and it isn't a scope base search
547 * 3) the DN we are looking for is a child of a certain
548 * partition and always stop
549 * - we don't need to go any further up in the
552 if (ldb_dn_compare(data->partitions[i]->ctrl->dn,
553 req->op.search.base) == 0) {
558 (ldb_dn_compare_base(req->op.search.base,
559 data->partitions[i]->ctrl->dn) == 0 &&
560 req->op.search.scope != LDB_SCOPE_BASE)) {
564 ldb_dn_compare_base(data->partitions[i]->ctrl->dn,
565 req->op.search.base) == 0) {
567 stop = true; /* note that this relies on partition ordering */
570 /* Domain scope: Find all partitions under the search
573 * We generate referral candidates if we haven't
574 * specified the domain scope control, haven't a base
575 * search* scope and the DN we are looking for is a real
576 * predecessor of certain partitions. When a new
577 * referral candidate is nearer to the DN than an
578 * existing one delete the latter (we want to have only
579 * the closest ones). When we checked this for all
580 * candidates we have the final referrals.
582 * We match if the DN we are looking for is a child of
583 * a certain partition or the partition
584 * DN itself - we don't need to go any further
585 * up in the hierarchy!
587 if ((!domain_scope) &&
588 (req->op.search.scope != LDB_SCOPE_BASE) &&
589 (ldb_dn_compare_base(req->op.search.base,
590 data->partitions[i]->ctrl->dn) == 0) &&
591 (ldb_dn_compare(req->op.search.base,
592 data->partitions[i]->ctrl->dn) != 0)) {
593 char *ref = talloc_asprintf(ac,
595 lpcfg_dnsdomain(lp_ctx),
596 ldb_dn_get_linearized(data->partitions[i]->ctrl->dn),
597 req->op.search.scope == LDB_SCOPE_ONELEVEL ? "??base" : "");
603 /* Initialise the referrals list */
604 if (ac->referrals == NULL) {
605 ac->referrals = (const char **) str_list_make_empty(ac);
606 if (ac->referrals == NULL) {
611 /* Check if the new referral candidate is
612 * closer to the base DN than already
613 * saved ones and delete the latters */
615 while (ac->referrals[j] != NULL) {
616 if (strstr(ac->referrals[j],
617 ldb_dn_get_linearized(data->partitions[i]->ctrl->dn)) != NULL) {
618 str_list_remove(ac->referrals,
625 /* Add our new candidate */
626 ac->referrals = str_list_add(ac->referrals, ref);
630 if (ac->referrals == NULL) {
634 if (ldb_dn_compare_base(data->partitions[i]->ctrl->dn, req->op.search.base) == 0) {
636 stop = true; /* note that this relies on partition ordering */
641 ret = partition_prep_request(ac, data->partitions[i]);
642 if (ret != LDB_SUCCESS) {
650 /* Perhaps we didn't match any partitions. Try the main partition */
651 if (ac->num_requests == 0) {
653 return ldb_next_request(module, req);
656 /* fire the first one */
657 return partition_call_first(ac);
661 static int partition_add(struct ldb_module *module, struct ldb_request *req)
663 return partition_replicate(module, req, req->op.add.message->dn);
667 static int partition_modify(struct ldb_module *module, struct ldb_request *req)
669 return partition_replicate(module, req, req->op.mod.message->dn);
673 static int partition_delete(struct ldb_module *module, struct ldb_request *req)
675 return partition_replicate(module, req, req->op.del.dn);
679 static int partition_rename(struct ldb_module *module, struct ldb_request *req)
682 struct dsdb_partition *backend, *backend2;
684 struct partition_private_data *data = talloc_get_type(ldb_module_get_private(module),
685 struct partition_private_data);
687 /* Skip the lot if 'data' isn't here yet (initialisation) */
689 return ldb_operr(ldb_module_get_ctx(module));
692 backend = find_partition(data, req->op.rename.olddn, req);
693 backend2 = find_partition(data, req->op.rename.newdn, req);
695 if ((backend && !backend2) || (!backend && backend2)) {
696 return LDB_ERR_AFFECTS_MULTIPLE_DSAS;
699 if (backend != backend2) {
700 ldb_asprintf_errstring(ldb_module_get_ctx(module),
701 "Cannot rename from %s in %s to %s in %s: %s",
702 ldb_dn_get_linearized(req->op.rename.olddn),
703 ldb_dn_get_linearized(backend->ctrl->dn),
704 ldb_dn_get_linearized(req->op.rename.newdn),
705 ldb_dn_get_linearized(backend2->ctrl->dn),
706 ldb_strerror(LDB_ERR_AFFECTS_MULTIPLE_DSAS));
707 return LDB_ERR_AFFECTS_MULTIPLE_DSAS;
710 return partition_replicate(module, req, req->op.rename.olddn);
713 /* start a transaction */
714 static int partition_start_trans(struct ldb_module *module)
718 struct partition_private_data *data = talloc_get_type(ldb_module_get_private(module),
719 struct partition_private_data);
720 /* Look at base DN */
721 /* Figure out which partition it is under */
722 /* Skip the lot if 'data' isn't here yet (initialization) */
723 if ((module && ldb_module_flags(ldb_module_get_ctx(module)) & LDB_FLG_ENABLE_TRACING)) {
724 ldb_debug(ldb_module_get_ctx(module), LDB_DEBUG_TRACE, "partition_start_trans() -> (metadata partition)");
726 ret = ldb_next_start_trans(module);
727 if (ret != LDB_SUCCESS) {
731 ret = partition_reload_if_required(module, data, NULL);
732 if (ret != LDB_SUCCESS) {
736 for (i=0; data && data->partitions && data->partitions[i]; i++) {
737 if ((module && ldb_module_flags(ldb_module_get_ctx(module)) & LDB_FLG_ENABLE_TRACING)) {
738 ldb_debug(ldb_module_get_ctx(module), LDB_DEBUG_TRACE, "partition_start_trans() -> %s",
739 ldb_dn_get_linearized(data->partitions[i]->ctrl->dn));
741 ret = ldb_next_start_trans(data->partitions[i]->module);
742 if (ret != LDB_SUCCESS) {
743 /* Back it out, if it fails on one */
744 for (i--; i >= 0; i--) {
745 ldb_next_del_trans(data->partitions[i]->module);
747 ldb_next_del_trans(module);
752 data->in_transaction++;
757 /* prepare for a commit */
758 static int partition_prepare_commit(struct ldb_module *module)
761 struct partition_private_data *data = talloc_get_type(ldb_module_get_private(module),
762 struct partition_private_data);
764 for (i=0; data && data->partitions && data->partitions[i]; i++) {
767 if ((module && ldb_module_flags(ldb_module_get_ctx(module)) & LDB_FLG_ENABLE_TRACING)) {
768 ldb_debug(ldb_module_get_ctx(module), LDB_DEBUG_TRACE, "partition_prepare_commit() -> %s",
769 ldb_dn_get_linearized(data->partitions[i]->ctrl->dn));
771 ret = ldb_next_prepare_commit(data->partitions[i]->module);
772 if (ret != LDB_SUCCESS) {
773 ldb_asprintf_errstring(ldb_module_get_ctx(module), "prepare_commit error on %s: %s",
774 ldb_dn_get_linearized(data->partitions[i]->ctrl->dn),
775 ldb_errstring(ldb_module_get_ctx(module)));
780 if ((module && ldb_module_flags(ldb_module_get_ctx(module)) & LDB_FLG_ENABLE_TRACING)) {
781 ldb_debug(ldb_module_get_ctx(module), LDB_DEBUG_TRACE, "partition_prepare_commit() -> (metadata partition)");
783 return ldb_next_prepare_commit(module);
787 /* end a transaction */
788 static int partition_end_trans(struct ldb_module *module)
792 struct partition_private_data *data = talloc_get_type(ldb_module_get_private(module),
793 struct partition_private_data);
797 if (data->in_transaction == 0) {
798 DEBUG(0,("partition end transaction mismatch\n"));
799 ret = LDB_ERR_OPERATIONS_ERROR;
801 data->in_transaction--;
804 for (i=0; data && data->partitions && data->partitions[i]; i++) {
805 if ((module && ldb_module_flags(ldb_module_get_ctx(module)) & LDB_FLG_ENABLE_TRACING)) {
806 ldb_debug(ldb_module_get_ctx(module), LDB_DEBUG_TRACE, "partition_end_trans() -> %s",
807 ldb_dn_get_linearized(data->partitions[i]->ctrl->dn));
809 ret2 = ldb_next_end_trans(data->partitions[i]->module);
810 if (ret2 != LDB_SUCCESS) {
811 ldb_asprintf_errstring(ldb_module_get_ctx(module), "end_trans error on %s: %s",
812 ldb_dn_get_linearized(data->partitions[i]->ctrl->dn),
813 ldb_errstring(ldb_module_get_ctx(module)));
818 if ((module && ldb_module_flags(ldb_module_get_ctx(module)) & LDB_FLG_ENABLE_TRACING)) {
819 ldb_debug(ldb_module_get_ctx(module), LDB_DEBUG_TRACE, "partition_end_trans() -> (metadata partition)");
821 ret2 = ldb_next_end_trans(module);
822 if (ret2 != LDB_SUCCESS) {
828 /* delete a transaction */
829 static int partition_del_trans(struct ldb_module *module)
831 int ret, final_ret = LDB_SUCCESS;
833 struct partition_private_data *data = talloc_get_type(ldb_module_get_private(module),
834 struct partition_private_data);
835 for (i=0; data && data->partitions && data->partitions[i]; i++) {
836 if ((module && ldb_module_flags(ldb_module_get_ctx(module)) & LDB_FLG_ENABLE_TRACING)) {
837 ldb_debug(ldb_module_get_ctx(module), LDB_DEBUG_TRACE, "partition_del_trans() -> %s",
838 ldb_dn_get_linearized(data->partitions[i]->ctrl->dn));
840 ret = ldb_next_del_trans(data->partitions[i]->module);
841 if (ret != LDB_SUCCESS) {
842 ldb_asprintf_errstring(ldb_module_get_ctx(module), "del_trans error on %s: %s",
843 ldb_dn_get_linearized(data->partitions[i]->ctrl->dn),
844 ldb_errstring(ldb_module_get_ctx(module)));
849 if (data->in_transaction == 0) {
850 DEBUG(0,("partition del transaction mismatch\n"));
851 return ldb_operr(ldb_module_get_ctx(module));
853 data->in_transaction--;
855 if ((module && ldb_module_flags(ldb_module_get_ctx(module)) & LDB_FLG_ENABLE_TRACING)) {
856 ldb_debug(ldb_module_get_ctx(module), LDB_DEBUG_TRACE, "partition_del_trans() -> (metadata partition)");
858 ret = ldb_next_del_trans(module);
859 if (ret != LDB_SUCCESS) {
865 int partition_primary_sequence_number(struct ldb_module *module, TALLOC_CTX *mem_ctx,
866 enum ldb_sequence_type type, uint64_t *seq_number)
869 struct ldb_result *res;
870 struct ldb_seqnum_request *tseq;
871 struct ldb_request *treq;
872 struct ldb_seqnum_result *seqr;
873 res = talloc_zero(mem_ctx, struct ldb_result);
875 return ldb_oom(ldb_module_get_ctx(module));
877 tseq = talloc_zero(res, struct ldb_seqnum_request);
880 return ldb_oom(ldb_module_get_ctx(module));
884 ret = ldb_build_extended_req(&treq, ldb_module_get_ctx(module), res,
885 LDB_EXTENDED_SEQUENCE_NUMBER,
889 ldb_extended_default_callback,
891 LDB_REQ_SET_LOCATION(treq);
892 if (ret != LDB_SUCCESS) {
897 ret = ldb_next_request(module, treq);
898 if (ret != LDB_SUCCESS) {
902 ret = ldb_wait(treq->handle, LDB_WAIT_ALL);
903 if (ret != LDB_SUCCESS) {
908 seqr = talloc_get_type(res->extended->data,
909 struct ldb_seqnum_result);
910 if (seqr->flags & LDB_SEQ_TIMESTAMP_SEQUENCE) {
911 ret = LDB_ERR_OPERATIONS_ERROR;
912 ldb_set_errstring(ldb_module_get_ctx(module), "Primary backend in partitions module returned a timestamp based seq number (must return a normal number)");
916 *seq_number = seqr->seq_num;
922 /* FIXME: This function is still semi-async */
923 static int partition_sequence_number(struct ldb_module *module, struct ldb_request *req)
927 uint64_t seq_number = 0;
928 uint64_t timestamp_sequence = 0;
929 uint64_t timestamp = 0;
930 struct partition_private_data *data = talloc_get_type(ldb_module_get_private(module),
931 struct partition_private_data);
932 struct ldb_seqnum_request *seq;
933 struct ldb_seqnum_result *seqr;
934 struct ldb_request *treq;
935 struct ldb_seqnum_request *tseq;
936 struct ldb_seqnum_result *tseqr;
937 struct ldb_extended *ext;
938 struct ldb_result *res;
939 struct dsdb_partition *p;
941 p = find_partition(data, NULL, req);
943 /* the caller specified what partition they want the
944 * sequence number operation on - just pass it on
946 return ldb_next_request(p->module, req);
949 seq = talloc_get_type(req->op.extended.data, struct ldb_seqnum_request);
953 case LDB_SEQ_HIGHEST_SEQ:
955 ret = partition_primary_sequence_number(module, req, seq->type, &seq_number);
956 if (ret != LDB_SUCCESS) {
960 /* Skip the lot if 'data' isn't here yet (initialisation) */
961 for (i=0; data && data->partitions && data->partitions[i]; i++) {
963 res = talloc_zero(req, struct ldb_result);
965 return ldb_oom(ldb_module_get_ctx(module));
967 tseq = talloc_zero(res, struct ldb_seqnum_request);
970 return ldb_oom(ldb_module_get_ctx(module));
972 tseq->type = seq->type;
974 ret = ldb_build_extended_req(&treq, ldb_module_get_ctx(module), res,
975 LDB_EXTENDED_SEQUENCE_NUMBER,
979 ldb_extended_default_callback,
981 LDB_REQ_SET_LOCATION(treq);
982 if (ret != LDB_SUCCESS) {
987 ret = ldb_request_add_control(treq,
988 DSDB_CONTROL_CURRENT_PARTITION_OID,
989 false, data->partitions[i]->ctrl);
990 if (ret != LDB_SUCCESS) {
995 ret = partition_request(data->partitions[i]->module, treq);
996 if (ret != LDB_SUCCESS) {
1000 ret = ldb_wait(treq->handle, LDB_WAIT_ALL);
1001 if (ret != LDB_SUCCESS) {
1005 tseqr = talloc_get_type(res->extended->data,
1006 struct ldb_seqnum_result);
1007 if (tseqr->flags & LDB_SEQ_TIMESTAMP_SEQUENCE) {
1008 timestamp_sequence = MAX(timestamp_sequence,
1011 seq_number += tseqr->seq_num;
1016 case LDB_SEQ_HIGHEST_TIMESTAMP:
1018 res = talloc_zero(req, struct ldb_result);
1020 return ldb_oom(ldb_module_get_ctx(module));
1023 tseq = talloc_zero(res, struct ldb_seqnum_request);
1026 return ldb_oom(ldb_module_get_ctx(module));
1028 tseq->type = LDB_SEQ_HIGHEST_TIMESTAMP;
1030 ret = ldb_build_extended_req(&treq, ldb_module_get_ctx(module), res,
1031 LDB_EXTENDED_SEQUENCE_NUMBER,
1035 ldb_extended_default_callback,
1037 LDB_REQ_SET_LOCATION(treq);
1038 if (ret != LDB_SUCCESS) {
1043 ret = ldb_next_request(module, treq);
1044 if (ret != LDB_SUCCESS) {
1048 ret = ldb_wait(treq->handle, LDB_WAIT_ALL);
1049 if (ret != LDB_SUCCESS) {
1054 tseqr = talloc_get_type(res->extended->data,
1055 struct ldb_seqnum_result);
1056 timestamp = tseqr->seq_num;
1060 /* Skip the lot if 'data' isn't here yet (initialisation) */
1061 for (i=0; data && data->partitions && data->partitions[i]; i++) {
1063 res = talloc_zero(req, struct ldb_result);
1065 return ldb_oom(ldb_module_get_ctx(module));
1068 tseq = talloc_zero(res, struct ldb_seqnum_request);
1071 return ldb_oom(ldb_module_get_ctx(module));
1073 tseq->type = LDB_SEQ_HIGHEST_TIMESTAMP;
1075 ret = ldb_build_extended_req(&treq, ldb_module_get_ctx(module), res,
1076 LDB_EXTENDED_SEQUENCE_NUMBER,
1080 ldb_extended_default_callback,
1082 LDB_REQ_SET_LOCATION(treq);
1083 if (ret != LDB_SUCCESS) {
1088 ret = ldb_request_add_control(treq,
1089 DSDB_CONTROL_CURRENT_PARTITION_OID,
1090 false, data->partitions[i]->ctrl);
1091 if (ret != LDB_SUCCESS) {
1096 ret = partition_request(data->partitions[i]->module, treq);
1097 if (ret != LDB_SUCCESS) {
1101 ret = ldb_wait(treq->handle, LDB_WAIT_ALL);
1102 if (ret != LDB_SUCCESS) {
1107 tseqr = talloc_get_type(res->extended->data,
1108 struct ldb_seqnum_result);
1109 timestamp = MAX(timestamp, tseqr->seq_num);
1117 ext = talloc_zero(req, struct ldb_extended);
1119 return ldb_oom(ldb_module_get_ctx(module));
1121 seqr = talloc_zero(ext, struct ldb_seqnum_result);
1124 return ldb_oom(ldb_module_get_ctx(module));
1126 ext->oid = LDB_EXTENDED_SEQUENCE_NUMBER;
1129 switch (seq->type) {
1131 case LDB_SEQ_HIGHEST_SEQ:
1133 /* Has someone above set a timebase sequence? */
1134 if (timestamp_sequence) {
1135 seqr->seq_num = (((unsigned long long)timestamp << 24) | (seq_number & 0xFFFFFF));
1137 seqr->seq_num = seq_number;
1140 if (timestamp_sequence > seqr->seq_num) {
1141 seqr->seq_num = timestamp_sequence;
1142 seqr->flags |= LDB_SEQ_TIMESTAMP_SEQUENCE;
1145 seqr->flags |= LDB_SEQ_GLOBAL_SEQUENCE;
1147 case LDB_SEQ_HIGHEST_TIMESTAMP:
1148 seqr->seq_num = timestamp;
1152 if (seq->type == LDB_SEQ_NEXT) {
1156 /* send request done */
1157 return ldb_module_done(req, NULL, ext, LDB_SUCCESS);
1161 static int partition_extended(struct ldb_module *module, struct ldb_request *req)
1163 struct partition_private_data *data = talloc_get_type(ldb_module_get_private(module),
1164 struct partition_private_data);
1165 struct partition_context *ac;
1168 /* if we aren't initialised yet go further */
1170 return ldb_next_request(module, req);
1173 /* see if we are still up-to-date */
1174 ret = partition_reload_if_required(module, data, req);
1175 if (ret != LDB_SUCCESS) {
1179 if (strcmp(req->op.extended.oid, LDB_EXTENDED_SEQUENCE_NUMBER) == 0) {
1180 return partition_sequence_number(module, req);
1183 if (strcmp(req->op.extended.oid, DSDB_EXTENDED_CREATE_PARTITION_OID) == 0) {
1184 return partition_create(module, req);
1188 * as the extended operation has no dn
1189 * we need to send it to all partitions
1192 ac = partition_init_ctx(module, req);
1194 return ldb_operr(ldb_module_get_ctx(module));
1197 return partition_send_all(module, ac, req);
1200 static const struct ldb_module_ops ldb_partition_module_ops = {
1201 .name = "partition",
1202 .init_context = partition_init,
1203 .search = partition_search,
1204 .add = partition_add,
1205 .modify = partition_modify,
1206 .del = partition_delete,
1207 .rename = partition_rename,
1208 .extended = partition_extended,
1209 .start_transaction = partition_start_trans,
1210 .prepare_commit = partition_prepare_commit,
1211 .end_transaction = partition_end_trans,
1212 .del_transaction = partition_del_trans,
1215 int ldb_partition_module_init(const char *version)
1217 LDB_MODULE_CHECK_VERSION(version);
1218 return ldb_register_module(&ldb_partition_module_ops);