2 ldb database mapping module
4 Copyright (C) Jelmer Vernooij 2005
5 Copyright (C) Martin Kuehl <mkhl@samba.org> 2006
6 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2006
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/>.
27 #include "ldb/include/includes.h"
29 #include "ldb/modules/ldb_map.h"
30 #include "ldb/modules/ldb_map_private.h"
34 * ================== */
36 /* Select attributes that stay in the local partition. */
37 static const char **map_attrs_select_local(struct ldb_module *module, void *mem_ctx, const char * const *attrs)
39 const struct ldb_map_context *data = map_get_context(module);
47 result = talloc_array(mem_ctx, const char *, 1);
53 for (i = 0; attrs[i]; i++) {
54 /* Wildcards and ignored attributes are kept locally */
55 if ((ldb_attr_cmp(attrs[i], "*") == 0) ||
56 (!map_attr_check_remote(data, attrs[i]))) {
57 result = talloc_realloc(mem_ctx, result, const char *, last+2);
62 result[last] = talloc_strdup(result, attrs[i]);
63 result[last+1] = NULL;
76 /* Collect attributes that are mapped into the remote partition. */
77 static const char **map_attrs_collect_remote(struct ldb_module *module, void *mem_ctx,
78 const char * const *attrs)
80 const struct ldb_map_context *data = map_get_context(module);
82 const struct ldb_map_attribute *map;
83 const char *name=NULL;
88 result = talloc_array(mem_ctx, const char *, 1);
94 for (i = 0; attrs[i]; i++) {
95 /* Wildcards are kept remotely, too */
96 if (ldb_attr_cmp(attrs[i], "*") == 0) {
97 const char **new_attrs = NULL;
98 ret = map_attrs_merge(module, mem_ctx, &new_attrs, attrs);
99 if (ret != LDB_SUCCESS) {
102 ret = map_attrs_merge(module, mem_ctx, &new_attrs, data->wildcard_attributes);
103 if (ret != LDB_SUCCESS) {
112 for (i = 0; attrs[i]; i++) {
113 /* Wildcards are kept remotely, too */
114 if (ldb_attr_cmp(attrs[i], "*") == 0) {
115 /* Add all 'include in wildcard' attributes */
120 /* Add remote names of mapped attrs */
121 map = map_attr_find_local(data, attrs[i]);
136 name = map->u.rename.remote_name;
140 /* Add all remote names of "generate" attrs */
141 for (j = 0; map->u.generate.remote_names[j]; j++) {
142 result = talloc_realloc(mem_ctx, result, const char *, last+2);
143 if (result == NULL) {
147 result[last] = talloc_strdup(result, map->u.generate.remote_names[j]);
148 result[last+1] = NULL;
154 named: /* We found a single remote name, add that */
155 result = talloc_realloc(mem_ctx, result, const char *, last+2);
156 if (result == NULL) {
160 result[last] = talloc_strdup(result, name);
161 result[last+1] = NULL;
173 /* Split attributes that stay in the local partition from those that
174 * are mapped into the remote partition. */
175 static int map_attrs_partition(struct ldb_module *module, void *mem_ctx, const char ***local_attrs, const char ***remote_attrs, const char * const *attrs)
177 *local_attrs = map_attrs_select_local(module, mem_ctx, attrs);
178 *remote_attrs = map_attrs_collect_remote(module, mem_ctx, attrs);
183 /* Mapping message elements
184 * ======================== */
186 /* Add an element to a message, overwriting any old identically named elements. */
187 static int ldb_msg_replace(struct ldb_message *msg, const struct ldb_message_element *el)
189 struct ldb_message_element *old;
191 old = ldb_msg_find_element(msg, el->name);
193 /* no local result, add as new element */
195 if (ldb_msg_add_empty(msg, el->name, 0, &old) != 0) {
198 talloc_free(old->name);
201 /* copy new element */
204 /* and make sure we reference the contents */
205 if (!talloc_reference(msg->elements, el->name)) {
208 if (!talloc_reference(msg->elements, el->values)) {
215 /* Map a message element back into the local partition. */
216 static struct ldb_message_element *ldb_msg_el_map_remote(struct ldb_module *module,
218 const struct ldb_map_attribute *map,
219 const char *attr_name,
220 const struct ldb_message_element *old)
222 struct ldb_message_element *el;
225 el = talloc_zero(mem_ctx, struct ldb_message_element);
231 el->num_values = old->num_values;
232 el->values = talloc_array(el, struct ldb_val, el->num_values);
233 if (el->values == NULL) {
239 el->name = talloc_strdup(el, attr_name);
240 if (el->name == NULL) {
246 for (i = 0; i < el->num_values; i++) {
247 el->values[i] = ldb_val_map_remote(module, el->values, map, &old->values[i]);
253 /* Merge a remote message element into a local message. */
254 static int ldb_msg_el_merge(struct ldb_module *module, struct ldb_message *local,
255 struct ldb_message *remote, const char *attr_name)
257 const struct ldb_map_context *data = map_get_context(module);
258 const struct ldb_map_attribute *map;
259 struct ldb_message_element *old, *el=NULL;
260 const char *remote_name = NULL;
262 /* We handle wildcards in ldb_msg_el_merge_wildcard */
263 if (ldb_attr_cmp(attr_name, "*") == 0) {
267 map = map_attr_find_local(data, attr_name);
269 /* Unknown attribute in remote message:
270 * skip, attribute was probably auto-generated */
279 remote_name = map->u.convert.remote_name;
282 remote_name = attr_name;
285 remote_name = map->u.rename.remote_name;
296 if (map->u.convert.convert_remote == NULL) {
297 ldb_debug(module->ldb, LDB_DEBUG_ERROR, "ldb_map: "
298 "Skipping attribute '%s': "
299 "'convert_remote' not set\n",
306 old = ldb_msg_find_element(remote, remote_name);
308 el = ldb_msg_el_map_remote(module, local, map, attr_name, old);
310 return LDB_ERR_NO_SUCH_ATTRIBUTE;
315 if (map->u.generate.generate_local == NULL) {
316 ldb_debug(module->ldb, LDB_DEBUG_ERROR, "ldb_map: "
317 "Skipping attribute '%s': "
318 "'generate_local' not set\n",
323 el = map->u.generate.generate_local(module, local, attr_name, remote);
325 /* Generation failure is probably due to lack of source attributes */
326 return LDB_ERR_NO_SUCH_ATTRIBUTE;
332 return LDB_ERR_OPERATIONS_ERROR;
335 return ldb_msg_replace(local, el);
338 /* Handle wildcard parts of merging a remote message element into a local message. */
339 static int ldb_msg_el_merge_wildcard(struct ldb_module *module, struct ldb_message *local,
340 struct ldb_message *remote)
342 const struct ldb_map_context *data = map_get_context(module);
343 const struct ldb_map_attribute *map = map_attr_find_local(data, "*");
344 struct ldb_message_element *el=NULL;
347 /* Perhaps we have a mapping for "*" */
348 if (map && map->type == MAP_KEEP) {
349 /* We copy everything over, and hope that anything with a
350 more specific rule is overwritten */
351 for (i = 0; i < remote->num_elements; i++) {
352 el = ldb_msg_el_map_remote(module, local, map, remote->elements[i].name,
353 &remote->elements[i]);
355 return LDB_ERR_OPERATIONS_ERROR;
358 ret = ldb_msg_replace(local, el);
365 /* Now walk the list of possible mappings, and apply each */
366 for (i = 0; data->attribute_maps[i].local_name; i++) {
367 ret = ldb_msg_el_merge(module, local, remote,
368 data->attribute_maps[i].local_name);
369 if (ret == LDB_ERR_NO_SUCH_ATTRIBUTE) {
382 * ================ */
384 /* Merge two local messages into a single one. */
385 static int ldb_msg_merge_local(struct ldb_module *module, struct ldb_message *msg1, struct ldb_message *msg2)
389 for (i = 0; i < msg2->num_elements; i++) {
390 ret = ldb_msg_replace(msg1, &msg2->elements[i]);
399 /* Merge a local and a remote message into a single local one. */
400 static int ldb_msg_merge_remote(struct map_context *ac, struct ldb_message *local,
401 struct ldb_message *remote)
404 const char * const *attrs = ac->all_attrs;
406 ret = ldb_msg_el_merge_wildcard(ac->module, local, remote);
412 for (i = 0; attrs && attrs[i]; i++) {
413 if (ldb_attr_cmp(attrs[i], "*") == 0) {
414 ret = ldb_msg_el_merge_wildcard(ac->module, local, remote);
422 /* Try to map each attribute back;
423 * Add to local message is possible,
424 * Overwrite old local attribute if necessary */
425 for (i = 0; attrs && attrs[i]; i++) {
426 ret = ldb_msg_el_merge(ac->module, local, remote,
428 if (ret == LDB_ERR_NO_SUCH_ATTRIBUTE) {
437 /* Mapping search results
438 * ====================== */
440 /* Map a search result back into the local partition. */
441 static int map_reply_remote(struct map_context *ac, struct ldb_reply *ares)
443 struct ldb_message *msg;
447 /* There is no result message, skip */
448 if (ares->type != LDB_REPLY_ENTRY) {
452 /* Create a new result message */
453 msg = ldb_msg_new(ares);
459 /* Merge remote message into new message */
460 ret = ldb_msg_merge_remote(ac, msg, ares->message);
466 /* Create corresponding local DN */
467 dn = ldb_dn_map_rebase_remote(ac->module, msg, ares->message->dn);
474 /* Store new message with new DN as the result */
475 talloc_free(ares->message);
481 /* Mapping parse trees
482 * =================== */
484 /* Check whether a parse tree can safely be split in two. */
485 static BOOL ldb_parse_tree_check_splittable(const struct ldb_parse_tree *tree)
487 const struct ldb_parse_tree *subtree = tree;
491 switch (subtree->operation) {
494 subtree = subtree->u.isnot.child;
498 return !negate; /* if negate: False */
501 return negate; /* if negate: True */
504 return True; /* simple parse tree */
508 return True; /* no parse tree */
511 /* Collect a list of attributes required to match a given parse tree. */
512 static int ldb_parse_tree_collect_attrs(struct ldb_module *module, void *mem_ctx, const char ***attrs, const struct ldb_parse_tree *tree)
514 const char **new_attrs;
521 switch (tree->operation) {
523 case LDB_OP_AND: /* attributes stored in list of subtrees */
524 for (i = 0; i < tree->u.list.num_elements; i++) {
525 ret = ldb_parse_tree_collect_attrs(module, mem_ctx,
526 attrs, tree->u.list.elements[i]);
533 case LDB_OP_NOT: /* attributes stored in single subtree */
534 return ldb_parse_tree_collect_attrs(module, mem_ctx, attrs, tree->u.isnot.child);
536 default: /* single attribute in tree */
537 new_attrs = ldb_attr_list_copy_add(mem_ctx, *attrs, tree->u.equality.attr);
546 static int map_subtree_select_local(struct ldb_module *module, void *mem_ctx, struct ldb_parse_tree **new, const struct ldb_parse_tree *tree);
548 /* Select a negated subtree that queries attributes in the local partition */
549 static int map_subtree_select_local_not(struct ldb_module *module, void *mem_ctx, struct ldb_parse_tree **new, const struct ldb_parse_tree *tree)
551 struct ldb_parse_tree *child;
554 /* Prepare new tree */
555 *new = talloc_memdup(mem_ctx, tree, sizeof(struct ldb_parse_tree));
561 /* Generate new subtree */
562 ret = map_subtree_select_local(module, *new, &child, tree->u.isnot.child);
568 /* Prune tree without subtree */
575 (*new)->u.isnot.child = child;
580 /* Select a list of subtrees that query attributes in the local partition */
581 static int map_subtree_select_local_list(struct ldb_module *module, void *mem_ctx, struct ldb_parse_tree **new, const struct ldb_parse_tree *tree)
585 /* Prepare new tree */
586 *new = talloc_memdup(mem_ctx, tree, sizeof(struct ldb_parse_tree));
592 /* Prepare list of subtrees */
593 (*new)->u.list.num_elements = 0;
594 (*new)->u.list.elements = talloc_array(*new, struct ldb_parse_tree *, tree->u.list.num_elements);
595 if ((*new)->u.list.elements == NULL) {
601 /* Generate new list of subtrees */
603 for (i = 0; i < tree->u.list.num_elements; i++) {
604 struct ldb_parse_tree *child;
605 ret = map_subtree_select_local(module, *new, &child, tree->u.list.elements[i]);
612 (*new)->u.list.elements[j] = child;
617 /* Prune tree without subtrees */
624 /* Fix subtree list size */
625 (*new)->u.list.num_elements = j;
626 (*new)->u.list.elements = talloc_realloc(*new, (*new)->u.list.elements, struct ldb_parse_tree *, (*new)->u.list.num_elements);
631 /* Select a simple subtree that queries attributes in the local partition */
632 static int map_subtree_select_local_simple(struct ldb_module *module, void *mem_ctx, struct ldb_parse_tree **new, const struct ldb_parse_tree *tree)
634 /* Prepare new tree */
635 *new = talloc_memdup(mem_ctx, tree, sizeof(struct ldb_parse_tree));
644 /* Select subtrees that query attributes in the local partition */
645 static int map_subtree_select_local(struct ldb_module *module, void *mem_ctx, struct ldb_parse_tree **new, const struct ldb_parse_tree *tree)
647 const struct ldb_map_context *data = map_get_context(module);
653 if (tree->operation == LDB_OP_NOT) {
654 return map_subtree_select_local_not(module, mem_ctx, new, tree);
657 if (tree->operation == LDB_OP_AND || tree->operation == LDB_OP_OR) {
658 return map_subtree_select_local_list(module, mem_ctx, new, tree);
661 if (map_attr_check_remote(data, tree->u.equality.attr)) {
666 return map_subtree_select_local_simple(module, mem_ctx, new, tree);
669 static int map_subtree_collect_remote(struct ldb_module *module, void *mem_ctx, struct ldb_parse_tree **new, const struct ldb_parse_tree *tree);
671 /* Collect a negated subtree that queries attributes in the remote partition */
672 static int map_subtree_collect_remote_not(struct ldb_module *module, void *mem_ctx, struct ldb_parse_tree **new, const struct ldb_parse_tree *tree)
674 struct ldb_parse_tree *child;
677 /* Prepare new tree */
678 *new = talloc_memdup(mem_ctx, tree, sizeof(struct ldb_parse_tree));
684 /* Generate new subtree */
685 ret = map_subtree_collect_remote(module, *new, &child, tree->u.isnot.child);
691 /* Prune tree without subtree */
698 (*new)->u.isnot.child = child;
703 /* Collect a list of subtrees that query attributes in the remote partition */
704 static int map_subtree_collect_remote_list(struct ldb_module *module, void *mem_ctx, struct ldb_parse_tree **new, const struct ldb_parse_tree *tree)
708 /* Prepare new tree */
709 *new = talloc_memdup(mem_ctx, tree, sizeof(struct ldb_parse_tree));
715 /* Prepare list of subtrees */
716 (*new)->u.list.num_elements = 0;
717 (*new)->u.list.elements = talloc_array(*new, struct ldb_parse_tree *, tree->u.list.num_elements);
718 if ((*new)->u.list.elements == NULL) {
724 /* Generate new list of subtrees */
726 for (i = 0; i < tree->u.list.num_elements; i++) {
727 struct ldb_parse_tree *child;
728 ret = map_subtree_collect_remote(module, *new, &child, tree->u.list.elements[i]);
735 (*new)->u.list.elements[j] = child;
740 /* Prune tree without subtrees */
747 /* Fix subtree list size */
748 (*new)->u.list.num_elements = j;
749 (*new)->u.list.elements = talloc_realloc(*new, (*new)->u.list.elements, struct ldb_parse_tree *, (*new)->u.list.num_elements);
754 /* Collect a simple subtree that queries attributes in the remote partition */
755 int map_subtree_collect_remote_simple(struct ldb_module *module, void *mem_ctx, struct ldb_parse_tree **new, const struct ldb_parse_tree *tree, const struct ldb_map_attribute *map)
759 /* Prepare new tree */
760 *new = talloc(mem_ctx, struct ldb_parse_tree);
767 if (map->type == MAP_KEEP) {
768 /* Nothing to do here */
772 /* Store attribute and value in new tree */
773 switch (tree->operation) {
775 attr = map_attr_map_local(*new, map, tree->u.present.attr);
776 (*new)->u.present.attr = attr;
778 case LDB_OP_SUBSTRING:
780 attr = map_attr_map_local(*new, map, tree->u.substring.attr);
781 (*new)->u.substring.attr = attr;
784 case LDB_OP_EQUALITY:
785 attr = map_attr_map_local(*new, map, tree->u.equality.attr);
786 (*new)->u.equality.attr = attr;
791 attr = map_attr_map_local(*new, map, tree->u.comparison.attr);
792 (*new)->u.comparison.attr = attr;
794 case LDB_OP_EXTENDED:
795 attr = map_attr_map_local(*new, map, tree->u.extended.attr);
796 (*new)->u.extended.attr = attr;
798 default: /* unknown kind of simple subtree */
809 if (map->type == MAP_RENAME) {
810 /* Nothing more to do here, the attribute has been renamed */
814 /* Store attribute and value in new tree */
815 switch (tree->operation) {
818 case LDB_OP_SUBSTRING:
822 (*new)->u.substring.chunks = NULL;
823 for (i=0; tree->u.substring.chunks[i]; i++) {
824 (*new)->u.substring.chunks = talloc_realloc(*new, (*new)->u.substring.chunks, struct ldb_val *, i+2);
825 if (!(*new)->u.substring.chunks) {
830 (*new)->u.substring.chunks[i] = talloc(*new, struct ldb_val);
831 if (!(*new)->u.substring.chunks[i]) {
836 *(*new)->u.substring.chunks[i] = ldb_val_map_local(module, *new, map, tree->u.substring.chunks[i]);
837 (*new)->u.substring.chunks[i+1] = NULL;
841 case LDB_OP_EQUALITY:
842 (*new)->u.equality.value = ldb_val_map_local(module, *new, map, &tree->u.equality.value);
847 (*new)->u.comparison.value = ldb_val_map_local(module, *new, map, &tree->u.comparison.value);
849 case LDB_OP_EXTENDED:
850 (*new)->u.extended.value = ldb_val_map_local(module, *new, map, &tree->u.extended.value);
851 (*new)->u.extended.rule_id = talloc_strdup(*new, tree->u.extended.rule_id);
853 default: /* unknown kind of simple subtree */
861 /* Collect subtrees that query attributes in the remote partition */
862 static int map_subtree_collect_remote(struct ldb_module *module, void *mem_ctx, struct ldb_parse_tree **new, const struct ldb_parse_tree *tree)
864 const struct ldb_map_context *data = map_get_context(module);
865 const struct ldb_map_attribute *map;
871 if (tree->operation == LDB_OP_NOT) {
872 return map_subtree_collect_remote_not(module, mem_ctx, new, tree);
875 if ((tree->operation == LDB_OP_AND) || (tree->operation == LDB_OP_OR)) {
876 return map_subtree_collect_remote_list(module, mem_ctx, new, tree);
879 if (!map_attr_check_remote(data, tree->u.equality.attr)) {
884 map = map_attr_find_local(data, tree->u.equality.attr);
885 if (map->convert_operator) {
886 return map->convert_operator(module, mem_ctx, new, tree);
889 if (map->type == MAP_GENERATE) {
890 ldb_debug(module->ldb, LDB_DEBUG_WARNING, "ldb_map: "
891 "Skipping attribute '%s': "
892 "'convert_operator' not set\n",
893 tree->u.equality.attr);
898 return map_subtree_collect_remote_simple(module, mem_ctx, new, tree, map);
901 /* Split subtrees that query attributes in the local partition from
902 * those that query the remote partition. */
903 static int ldb_parse_tree_partition(struct ldb_module *module, void *local_ctx, void *remote_ctx, struct ldb_parse_tree **local_tree, struct ldb_parse_tree **remote_tree, const struct ldb_parse_tree *tree)
910 /* No original tree */
915 /* Generate local tree */
916 ret = map_subtree_select_local(module, local_ctx, local_tree, tree);
921 /* Generate remote tree */
922 ret = map_subtree_collect_remote(module, remote_ctx, remote_tree, tree);
924 talloc_free(*local_tree);
931 /* Collect a list of attributes required either explicitly from a
932 * given list or implicitly from a given parse tree; split the
933 * collected list into local and remote parts. */
934 static int map_attrs_collect_and_partition(struct ldb_module *module, struct map_context *ac,
935 const char * const *search_attrs,
936 const struct ldb_parse_tree *tree)
939 const char **tree_attrs;
940 const char **remote_attrs;
941 const char **local_attrs;
944 /* Clear initial lists of partitioned attributes */
946 /* Clear initial lists of partitioned attributes */
948 /* There is no tree, just partition the searched attributes */
950 ret = map_attrs_partition(module, ac,
951 &local_attrs, &remote_attrs, search_attrs);
953 ac->local_attrs = local_attrs;
954 ac->remote_attrs = remote_attrs;
955 ac->all_attrs = search_attrs;
960 /* Create context for temporary memory */
961 tmp_ctx = talloc_new(ac);
962 if (tmp_ctx == NULL) {
966 /* Prepare list of attributes from tree */
967 tree_attrs = talloc_array(tmp_ctx, const char *, 1);
968 if (tree_attrs == NULL) {
969 talloc_free(tmp_ctx);
972 tree_attrs[0] = NULL;
974 /* Collect attributes from tree */
975 ret = ldb_parse_tree_collect_attrs(module, tmp_ctx, &tree_attrs, tree);
980 /* Merge attributes from search operation */
981 ret = map_attrs_merge(module, tmp_ctx, &tree_attrs, search_attrs);
986 /* Split local from remote attributes */
987 ret = map_attrs_partition(module, ac, &local_attrs,
988 &remote_attrs, tree_attrs);
991 ac->local_attrs = local_attrs;
992 ac->remote_attrs = remote_attrs;
993 talloc_steal(ac, tree_attrs);
994 ac->all_attrs = tree_attrs;
997 /* Free temporary memory */
998 talloc_free(tmp_ctx);
1007 /* Outbound requests: search
1008 * ========================= */
1010 /* Pass a merged search result up the callback chain. */
1011 int map_up_callback(struct ldb_context *ldb, const struct ldb_request *req, struct ldb_reply *ares)
1015 /* No callback registered, stop */
1016 if (req->callback == NULL) {
1020 /* Only records need special treatment */
1021 if (ares->type != LDB_REPLY_ENTRY) {
1022 return req->callback(ldb, req->context, ares);
1025 /* Merged result doesn't match original query, skip */
1026 if (!ldb_match_msg(ldb, ares->message, req->op.search.tree, req->op.search.base, req->op.search.scope)) {
1027 ldb_debug(ldb, LDB_DEBUG_TRACE, "ldb_map: "
1028 "Skipping record '%s': "
1029 "doesn't match original search\n",
1030 ldb_dn_linearize(ldb, ares->message->dn));
1034 /* Limit result to requested attrs */
1035 if ((req->op.search.attrs) && (!ldb_attr_in_list(req->op.search.attrs, "*"))) {
1036 for (i = 0; i < ares->message->num_elements; ) {
1037 struct ldb_message_element *el = &ares->message->elements[i];
1038 if (!ldb_attr_in_list(req->op.search.attrs, el->name)) {
1039 ldb_msg_remove_element(ares->message, el);
1046 return req->callback(ldb, req->context, ares);
1049 /* Merge the remote and local parts of a search result. */
1050 int map_local_merge_callback(struct ldb_context *ldb, void *context, struct ldb_reply *ares)
1052 struct map_search_context *sc;
1055 if (context == NULL || ares == NULL) {
1056 ldb_set_errstring(ldb, talloc_asprintf(ldb, "ldb_map: "
1057 "NULL Context or Result in `map_local_merge_callback`"));
1058 return LDB_ERR_OPERATIONS_ERROR;
1061 sc = talloc_get_type(context, struct map_search_context);
1063 switch (ares->type) {
1064 case LDB_REPLY_ENTRY:
1065 /* We have already found a local record */
1066 if (sc->local_res) {
1067 ldb_set_errstring(ldb, talloc_asprintf(ldb, "ldb_map: "
1068 "Too many results to base search for local entry"));
1070 return LDB_ERR_OPERATIONS_ERROR;
1073 /* Store local result */
1074 sc->local_res = ares;
1076 /* Merge remote into local message */
1077 ret = ldb_msg_merge_local(sc->ac->module, ares->message, sc->remote_res->message);
1080 return LDB_ERR_OPERATIONS_ERROR;
1083 return map_up_callback(ldb, sc->ac->orig_req, ares);
1085 case LDB_REPLY_DONE:
1086 /* No local record found, continue with remote record */
1087 if (sc->local_res == NULL) {
1088 return map_up_callback(ldb, sc->ac->orig_req, sc->remote_res);
1093 ldb_set_errstring(ldb, talloc_asprintf(ldb, "ldb_map: "
1094 "Unexpected result type in base search for local entry"));
1096 return LDB_ERR_OPERATIONS_ERROR;
1100 /* Search the local part of a remote search result. */
1101 int map_remote_search_callback(struct ldb_context *ldb, void *context, struct ldb_reply *ares)
1103 struct map_context *ac;
1104 struct map_search_context *sc;
1105 struct ldb_request *req;
1108 if (context == NULL || ares == NULL) {
1109 ldb_set_errstring(ldb, talloc_asprintf(ldb, "ldb_map: "
1110 "NULL Context or Result in `map_remote_search_callback`"));
1111 return LDB_ERR_OPERATIONS_ERROR;
1114 ac = talloc_get_type(context, struct map_context);
1116 /* It's not a record, stop searching */
1117 if (ares->type != LDB_REPLY_ENTRY) {
1118 return map_up_callback(ldb, ac->orig_req, ares);
1121 /* Map result record into a local message */
1122 ret = map_reply_remote(ac, ares);
1125 return LDB_ERR_OPERATIONS_ERROR;
1128 /* There is no local db, stop searching */
1129 if (!map_check_local_db(ac->module)) {
1130 return map_up_callback(ldb, ac->orig_req, ares);
1133 /* Prepare local search context */
1134 sc = map_init_search_context(ac, ares);
1137 return LDB_ERR_OPERATIONS_ERROR;
1140 /* Prepare local search request */
1141 /* TODO: use GUIDs here instead? */
1143 ac->search_reqs = talloc_realloc(ac, ac->search_reqs, struct ldb_request *, ac->num_searches + 2);
1144 if (ac->search_reqs == NULL) {
1146 return LDB_ERR_OPERATIONS_ERROR;
1149 ac->search_reqs[ac->num_searches]
1150 = req = map_search_base_req(ac, ares->message->dn,
1151 NULL, NULL, sc, map_local_merge_callback);
1155 return LDB_ERR_OPERATIONS_ERROR;
1158 ac->search_reqs[ac->num_searches] = NULL;
1160 return ldb_next_request(ac->module, req);
1163 /* Search a record. */
1164 int map_search(struct ldb_module *module, struct ldb_request *req)
1166 struct ldb_handle *h;
1167 struct map_context *ac;
1168 struct ldb_parse_tree *local_tree, *remote_tree;
1171 const char *wildcard[] = { "*", NULL };
1172 const char * const *attrs;
1174 /* Do not manipulate our control entries */
1175 if (ldb_dn_is_special(req->op.search.base))
1176 return ldb_next_request(module, req);
1178 /* No mapping requested, skip to next module */
1179 if ((req->op.search.base) && (!ldb_dn_check_local(module, req->op.search.base))) {
1180 return ldb_next_request(module, req);
1183 /* TODO: How can we be sure about which partition we are
1184 * targetting when there is no search base? */
1186 /* Prepare context and handle */
1187 h = map_init_handle(req, module);
1189 return LDB_ERR_OPERATIONS_ERROR;
1191 ac = talloc_get_type(h->private_data, struct map_context);
1193 ac->search_reqs = talloc_array(ac, struct ldb_request *, 2);
1194 if (ac->search_reqs == NULL) {
1196 return LDB_ERR_OPERATIONS_ERROR;
1198 ac->num_searches = 1;
1199 ac->search_reqs[1] = NULL;
1201 /* Prepare the remote operation */
1202 ac->search_reqs[0] = talloc(ac, struct ldb_request);
1203 if (ac->search_reqs[0] == NULL) {
1207 *(ac->search_reqs[0]) = *req; /* copy the request */
1209 ac->search_reqs[0]->handle = h; /* return our own handle to deal with this call */
1211 ac->search_reqs[0]->context = ac;
1212 ac->search_reqs[0]->callback = map_remote_search_callback;
1214 /* It is easier to deal with the two different ways of
1215 * expressing the wildcard in the same codepath */
1216 attrs = req->op.search.attrs;
1217 if (attrs == NULL) {
1221 /* Split local from remote attrs */
1222 ret = map_attrs_collect_and_partition(module, ac,
1223 attrs, req->op.search.tree);
1228 ac->search_reqs[0]->op.search.attrs = ac->remote_attrs;
1230 /* Split local from remote tree */
1231 ret = ldb_parse_tree_partition(module, ac, ac->search_reqs[0],
1232 &local_tree, &remote_tree,
1233 req->op.search.tree);
1238 if (((local_tree != NULL) && (remote_tree != NULL)) &&
1239 (!ldb_parse_tree_check_splittable(req->op.search.tree))) {
1240 /* The query can't safely be split, enumerate the remote partition */
1245 if (local_tree == NULL) {
1246 /* Construct default local parse tree */
1247 local_tree = talloc_zero(ac, struct ldb_parse_tree);
1248 if (local_tree == NULL) {
1249 map_oom(ac->module);
1253 local_tree->operation = LDB_OP_PRESENT;
1254 local_tree->u.present.attr = talloc_strdup(local_tree, IS_MAPPED);
1256 if (remote_tree == NULL) {
1257 /* Construct default remote parse tree */
1258 remote_tree = ldb_parse_tree(ac->search_reqs[0], NULL);
1259 if (remote_tree == NULL) {
1264 ac->local_tree = local_tree;
1265 ac->search_reqs[0]->op.search.tree = remote_tree;
1267 ldb_set_timeout_from_prev_req(module->ldb, req, ac->search_reqs[0]);
1269 h->state = LDB_ASYNC_INIT;
1270 h->status = LDB_SUCCESS;
1272 ac->step = MAP_SEARCH_REMOTE;
1274 ret = ldb_next_remote_request(module, ac->search_reqs[0]);
1275 if (ret == LDB_SUCCESS) {
1284 return LDB_ERR_OPERATIONS_ERROR;