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 ** NOTE! The following LGPL license applies to the ldb
9 ** library. This does NOT imply that all of Samba is released
12 This library is free software; you can redistribute it and/or
13 modify it under the terms of the GNU Lesser General Public
14 License as published by the Free Software Foundation; either
15 version 3 of the License, or (at your option) any later version.
17 This library 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 GNU
20 Lesser General Public License for more details.
22 You should have received a copy of the GNU Lesser General Public
23 License along with this library; if not, write to the Free Software
24 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 #include "ldb_includes.h"
31 #include "ldb_map_private.h"
35 * ================== */
37 /* Select attributes that stay in the local partition. */
38 static const char **map_attrs_select_local(struct ldb_module *module, void *mem_ctx, const char * const *attrs)
40 const struct ldb_map_context *data = map_get_context(module);
48 result = talloc_array(mem_ctx, const char *, 1);
54 for (i = 0; attrs[i]; i++) {
55 /* Wildcards and ignored attributes are kept locally */
56 if ((ldb_attr_cmp(attrs[i], "*") == 0) ||
57 (!map_attr_check_remote(data, attrs[i]))) {
58 result = talloc_realloc(mem_ctx, result, const char *, last+2);
63 result[last] = talloc_strdup(result, attrs[i]);
64 result[last+1] = NULL;
77 /* Collect attributes that are mapped into the remote partition. */
78 static const char **map_attrs_collect_remote(struct ldb_module *module, void *mem_ctx,
79 const char * const *attrs)
81 const struct ldb_map_context *data = map_get_context(module);
83 const struct ldb_map_attribute *map;
84 const char *name=NULL;
89 result = talloc_array(mem_ctx, const char *, 1);
95 for (i = 0; attrs[i]; i++) {
96 /* Wildcards are kept remotely, too */
97 if (ldb_attr_cmp(attrs[i], "*") == 0) {
98 const char **new_attrs = NULL;
99 ret = map_attrs_merge(module, mem_ctx, &new_attrs, attrs);
100 if (ret != LDB_SUCCESS) {
103 ret = map_attrs_merge(module, mem_ctx, &new_attrs, data->wildcard_attributes);
104 if (ret != LDB_SUCCESS) {
113 for (i = 0; attrs[i]; i++) {
114 /* Wildcards are kept remotely, too */
115 if (ldb_attr_cmp(attrs[i], "*") == 0) {
116 /* Add all 'include in wildcard' attributes */
121 /* Add remote names of mapped attrs */
122 map = map_attr_find_local(data, attrs[i]);
137 name = map->u.rename.remote_name;
141 /* Add all remote names of "generate" attrs */
142 for (j = 0; map->u.generate.remote_names[j]; j++) {
143 result = talloc_realloc(mem_ctx, result, const char *, last+2);
144 if (result == NULL) {
148 result[last] = talloc_strdup(result, map->u.generate.remote_names[j]);
149 result[last+1] = NULL;
155 named: /* We found a single remote name, add that */
156 result = talloc_realloc(mem_ctx, result, const char *, last+2);
157 if (result == NULL) {
161 result[last] = talloc_strdup(result, name);
162 result[last+1] = NULL;
174 /* Split attributes that stay in the local partition from those that
175 * are mapped into the remote partition. */
176 static int map_attrs_partition(struct ldb_module *module, void *mem_ctx, const char ***local_attrs, const char ***remote_attrs, const char * const *attrs)
178 *local_attrs = map_attrs_select_local(module, mem_ctx, attrs);
179 *remote_attrs = map_attrs_collect_remote(module, mem_ctx, attrs);
184 /* Mapping message elements
185 * ======================== */
187 /* Add an element to a message, overwriting any old identically named elements. */
188 static int ldb_msg_replace(struct ldb_message *msg, const struct ldb_message_element *el)
190 struct ldb_message_element *old;
192 old = ldb_msg_find_element(msg, el->name);
194 /* no local result, add as new element */
196 if (ldb_msg_add_empty(msg, el->name, 0, &old) != 0) {
199 talloc_free(old->name);
202 /* copy new element */
205 /* and make sure we reference the contents */
206 if (!talloc_reference(msg->elements, el->name)) {
209 if (!talloc_reference(msg->elements, el->values)) {
216 /* Map a message element back into the local partition. */
217 static struct ldb_message_element *ldb_msg_el_map_remote(struct ldb_module *module,
219 const struct ldb_map_attribute *map,
220 const char *attr_name,
221 const struct ldb_message_element *old)
223 struct ldb_message_element *el;
226 el = talloc_zero(mem_ctx, struct ldb_message_element);
232 el->values = talloc_array(el, struct ldb_val, old->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 < old->num_values; i++) {
247 el->values[i] = ldb_val_map_remote(module, el->values, map, &old->values[i]);
248 /* Conversions might fail, in which case bail */
249 if (!el->values[i].data) {
259 /* Merge a remote message element into a local message. */
260 static int ldb_msg_el_merge(struct ldb_module *module, struct ldb_message *local,
261 struct ldb_message *remote, const char *attr_name)
263 const struct ldb_map_context *data = map_get_context(module);
264 const struct ldb_map_attribute *map;
265 struct ldb_message_element *old, *el=NULL;
266 const char *remote_name = NULL;
268 /* We handle wildcards in ldb_msg_el_merge_wildcard */
269 if (ldb_attr_cmp(attr_name, "*") == 0) {
273 map = map_attr_find_local(data, attr_name);
275 /* Unknown attribute in remote message:
276 * skip, attribute was probably auto-generated */
285 remote_name = map->u.convert.remote_name;
288 remote_name = attr_name;
291 remote_name = map->u.rename.remote_name;
302 if (map->u.convert.convert_remote == NULL) {
303 ldb_debug(module->ldb, LDB_DEBUG_ERROR, "ldb_map: "
304 "Skipping attribute '%s': "
305 "'convert_remote' not set\n",
312 old = ldb_msg_find_element(remote, remote_name);
314 el = ldb_msg_el_map_remote(module, local, map, attr_name, old);
316 return LDB_ERR_NO_SUCH_ATTRIBUTE;
321 if (map->u.generate.generate_local == NULL) {
322 ldb_debug(module->ldb, LDB_DEBUG_ERROR, "ldb_map: "
323 "Skipping attribute '%s': "
324 "'generate_local' not set\n",
329 el = map->u.generate.generate_local(module, local, attr_name, remote);
331 /* Generation failure is probably due to lack of source attributes */
332 return LDB_ERR_NO_SUCH_ATTRIBUTE;
338 return LDB_ERR_NO_SUCH_ATTRIBUTE;
341 return ldb_msg_replace(local, el);
344 /* Handle wildcard parts of merging a remote message element into a local message. */
345 static int ldb_msg_el_merge_wildcard(struct ldb_module *module, struct ldb_message *local,
346 struct ldb_message *remote)
348 const struct ldb_map_context *data = map_get_context(module);
349 const struct ldb_map_attribute *map = map_attr_find_local(data, "*");
350 struct ldb_message_element *el=NULL;
353 /* Perhaps we have a mapping for "*" */
354 if (map && map->type == MAP_KEEP) {
355 /* We copy everything over, and hope that anything with a
356 more specific rule is overwritten */
357 for (i = 0; i < remote->num_elements; i++) {
358 el = ldb_msg_el_map_remote(module, local, map, remote->elements[i].name,
359 &remote->elements[i]);
361 return LDB_ERR_OPERATIONS_ERROR;
364 ret = ldb_msg_replace(local, el);
371 /* Now walk the list of possible mappings, and apply each */
372 for (i = 0; data->attribute_maps[i].local_name; i++) {
373 ret = ldb_msg_el_merge(module, local, remote,
374 data->attribute_maps[i].local_name);
375 if (ret == LDB_ERR_NO_SUCH_ATTRIBUTE) {
388 * ================ */
390 /* Merge two local messages into a single one. */
391 static int ldb_msg_merge_local(struct ldb_module *module, struct ldb_message *msg1, struct ldb_message *msg2)
395 for (i = 0; i < msg2->num_elements; i++) {
396 ret = ldb_msg_replace(msg1, &msg2->elements[i]);
405 /* Merge a local and a remote message into a single local one. */
406 static int ldb_msg_merge_remote(struct map_context *ac, struct ldb_message *local,
407 struct ldb_message *remote)
410 const char * const *attrs = ac->all_attrs;
412 ret = ldb_msg_el_merge_wildcard(ac->module, local, remote);
418 for (i = 0; attrs && attrs[i]; i++) {
419 if (ldb_attr_cmp(attrs[i], "*") == 0) {
420 ret = ldb_msg_el_merge_wildcard(ac->module, local, remote);
428 /* Try to map each attribute back;
429 * Add to local message is possible,
430 * Overwrite old local attribute if necessary */
431 for (i = 0; attrs && attrs[i]; i++) {
432 ret = ldb_msg_el_merge(ac->module, local, remote,
434 if (ret == LDB_ERR_NO_SUCH_ATTRIBUTE) {
443 /* Mapping search results
444 * ====================== */
446 /* Map a search result back into the local partition. */
447 static int map_reply_remote(struct map_context *ac, struct ldb_reply *ares)
449 struct ldb_message *msg;
453 /* There is no result message, skip */
454 if (ares->type != LDB_REPLY_ENTRY) {
458 /* Create a new result message */
459 msg = ldb_msg_new(ares);
465 /* Merge remote message into new message */
466 ret = ldb_msg_merge_remote(ac, msg, ares->message);
472 /* Create corresponding local DN */
473 dn = ldb_dn_map_rebase_remote(ac->module, msg, ares->message->dn);
480 /* Store new message with new DN as the result */
481 talloc_free(ares->message);
487 /* Mapping parse trees
488 * =================== */
490 /* Check whether a parse tree can safely be split in two. */
491 static bool ldb_parse_tree_check_splittable(const struct ldb_parse_tree *tree)
493 const struct ldb_parse_tree *subtree = tree;
497 switch (subtree->operation) {
500 subtree = subtree->u.isnot.child;
504 return !negate; /* if negate: False */
507 return negate; /* if negate: True */
510 return true; /* simple parse tree */
514 return true; /* no parse tree */
517 /* Collect a list of attributes required to match a given parse tree. */
518 static int ldb_parse_tree_collect_attrs(struct ldb_module *module, void *mem_ctx, const char ***attrs, const struct ldb_parse_tree *tree)
520 const char **new_attrs;
527 switch (tree->operation) {
529 case LDB_OP_AND: /* attributes stored in list of subtrees */
530 for (i = 0; i < tree->u.list.num_elements; i++) {
531 ret = ldb_parse_tree_collect_attrs(module, mem_ctx,
532 attrs, tree->u.list.elements[i]);
539 case LDB_OP_NOT: /* attributes stored in single subtree */
540 return ldb_parse_tree_collect_attrs(module, mem_ctx, attrs, tree->u.isnot.child);
542 default: /* single attribute in tree */
543 new_attrs = ldb_attr_list_copy_add(mem_ctx, *attrs, tree->u.equality.attr);
552 static int map_subtree_select_local(struct ldb_module *module, void *mem_ctx, struct ldb_parse_tree **new, const struct ldb_parse_tree *tree);
554 /* Select a negated subtree that queries attributes in the local partition */
555 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)
557 struct ldb_parse_tree *child;
560 /* Prepare new tree */
561 *new = talloc_memdup(mem_ctx, tree, sizeof(struct ldb_parse_tree));
567 /* Generate new subtree */
568 ret = map_subtree_select_local(module, *new, &child, tree->u.isnot.child);
574 /* Prune tree without subtree */
581 (*new)->u.isnot.child = child;
586 /* Select a list of subtrees that query attributes in the local partition */
587 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)
591 /* Prepare new tree */
592 *new = talloc_memdup(mem_ctx, tree, sizeof(struct ldb_parse_tree));
598 /* Prepare list of subtrees */
599 (*new)->u.list.num_elements = 0;
600 (*new)->u.list.elements = talloc_array(*new, struct ldb_parse_tree *, tree->u.list.num_elements);
601 if ((*new)->u.list.elements == NULL) {
607 /* Generate new list of subtrees */
609 for (i = 0; i < tree->u.list.num_elements; i++) {
610 struct ldb_parse_tree *child;
611 ret = map_subtree_select_local(module, *new, &child, tree->u.list.elements[i]);
618 (*new)->u.list.elements[j] = child;
623 /* Prune tree without subtrees */
630 /* Fix subtree list size */
631 (*new)->u.list.num_elements = j;
632 (*new)->u.list.elements = talloc_realloc(*new, (*new)->u.list.elements, struct ldb_parse_tree *, (*new)->u.list.num_elements);
637 /* Select a simple subtree that queries attributes in the local partition */
638 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)
640 /* Prepare new tree */
641 *new = talloc_memdup(mem_ctx, tree, sizeof(struct ldb_parse_tree));
650 /* Select subtrees that query attributes in the local partition */
651 static int map_subtree_select_local(struct ldb_module *module, void *mem_ctx, struct ldb_parse_tree **new, const struct ldb_parse_tree *tree)
653 const struct ldb_map_context *data = map_get_context(module);
659 if (tree->operation == LDB_OP_NOT) {
660 return map_subtree_select_local_not(module, mem_ctx, new, tree);
663 if (tree->operation == LDB_OP_AND || tree->operation == LDB_OP_OR) {
664 return map_subtree_select_local_list(module, mem_ctx, new, tree);
667 if (map_attr_check_remote(data, tree->u.equality.attr)) {
672 return map_subtree_select_local_simple(module, mem_ctx, new, tree);
675 static int map_subtree_collect_remote(struct ldb_module *module, void *mem_ctx, struct ldb_parse_tree **new, const struct ldb_parse_tree *tree);
677 /* Collect a negated subtree that queries attributes in the remote partition */
678 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)
680 struct ldb_parse_tree *child;
683 /* Prepare new tree */
684 *new = talloc_memdup(mem_ctx, tree, sizeof(struct ldb_parse_tree));
690 /* Generate new subtree */
691 ret = map_subtree_collect_remote(module, *new, &child, tree->u.isnot.child);
697 /* Prune tree without subtree */
704 (*new)->u.isnot.child = child;
709 /* Collect a list of subtrees that query attributes in the remote partition */
710 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)
714 /* Prepare new tree */
715 *new = talloc_memdup(mem_ctx, tree, sizeof(struct ldb_parse_tree));
721 /* Prepare list of subtrees */
722 (*new)->u.list.num_elements = 0;
723 (*new)->u.list.elements = talloc_array(*new, struct ldb_parse_tree *, tree->u.list.num_elements);
724 if ((*new)->u.list.elements == NULL) {
730 /* Generate new list of subtrees */
732 for (i = 0; i < tree->u.list.num_elements; i++) {
733 struct ldb_parse_tree *child;
734 ret = map_subtree_collect_remote(module, *new, &child, tree->u.list.elements[i]);
741 (*new)->u.list.elements[j] = child;
746 /* Prune tree without subtrees */
753 /* Fix subtree list size */
754 (*new)->u.list.num_elements = j;
755 (*new)->u.list.elements = talloc_realloc(*new, (*new)->u.list.elements, struct ldb_parse_tree *, (*new)->u.list.num_elements);
760 /* Collect a simple subtree that queries attributes in the remote partition */
761 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)
765 /* Prepare new tree */
766 *new = talloc(mem_ctx, struct ldb_parse_tree);
773 if (map->type == MAP_KEEP) {
774 /* Nothing to do here */
778 /* Store attribute and value in new tree */
779 switch (tree->operation) {
781 attr = map_attr_map_local(*new, map, tree->u.present.attr);
782 (*new)->u.present.attr = attr;
784 case LDB_OP_SUBSTRING:
786 attr = map_attr_map_local(*new, map, tree->u.substring.attr);
787 (*new)->u.substring.attr = attr;
790 case LDB_OP_EQUALITY:
791 attr = map_attr_map_local(*new, map, tree->u.equality.attr);
792 (*new)->u.equality.attr = attr;
797 attr = map_attr_map_local(*new, map, tree->u.comparison.attr);
798 (*new)->u.comparison.attr = attr;
800 case LDB_OP_EXTENDED:
801 attr = map_attr_map_local(*new, map, tree->u.extended.attr);
802 (*new)->u.extended.attr = attr;
804 default: /* unknown kind of simple subtree */
815 if (map->type == MAP_RENAME) {
816 /* Nothing more to do here, the attribute has been renamed */
820 /* Store attribute and value in new tree */
821 switch (tree->operation) {
824 case LDB_OP_SUBSTRING:
828 (*new)->u.substring.chunks = NULL;
829 for (i=0; tree->u.substring.chunks[i]; i++) {
830 (*new)->u.substring.chunks = talloc_realloc(*new, (*new)->u.substring.chunks, struct ldb_val *, i+2);
831 if (!(*new)->u.substring.chunks) {
836 (*new)->u.substring.chunks[i] = talloc(*new, struct ldb_val);
837 if (!(*new)->u.substring.chunks[i]) {
842 *(*new)->u.substring.chunks[i] = ldb_val_map_local(module, *new, map, tree->u.substring.chunks[i]);
843 (*new)->u.substring.chunks[i+1] = NULL;
847 case LDB_OP_EQUALITY:
848 (*new)->u.equality.value = ldb_val_map_local(module, *new, map, &tree->u.equality.value);
853 (*new)->u.comparison.value = ldb_val_map_local(module, *new, map, &tree->u.comparison.value);
855 case LDB_OP_EXTENDED:
856 (*new)->u.extended.value = ldb_val_map_local(module, *new, map, &tree->u.extended.value);
857 (*new)->u.extended.rule_id = talloc_strdup(*new, tree->u.extended.rule_id);
859 default: /* unknown kind of simple subtree */
867 /* Collect subtrees that query attributes in the remote partition */
868 static int map_subtree_collect_remote(struct ldb_module *module, void *mem_ctx, struct ldb_parse_tree **new, const struct ldb_parse_tree *tree)
870 const struct ldb_map_context *data = map_get_context(module);
871 const struct ldb_map_attribute *map;
877 if (tree->operation == LDB_OP_NOT) {
878 return map_subtree_collect_remote_not(module, mem_ctx, new, tree);
881 if ((tree->operation == LDB_OP_AND) || (tree->operation == LDB_OP_OR)) {
882 return map_subtree_collect_remote_list(module, mem_ctx, new, tree);
885 if (!map_attr_check_remote(data, tree->u.equality.attr)) {
890 map = map_attr_find_local(data, tree->u.equality.attr);
891 if (map->convert_operator) {
892 return map->convert_operator(module, mem_ctx, new, tree);
895 if (map->type == MAP_GENERATE) {
896 ldb_debug(module->ldb, LDB_DEBUG_WARNING, "ldb_map: "
897 "Skipping attribute '%s': "
898 "'convert_operator' not set\n",
899 tree->u.equality.attr);
904 return map_subtree_collect_remote_simple(module, mem_ctx, new, tree, map);
907 /* Split subtrees that query attributes in the local partition from
908 * those that query the remote partition. */
909 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)
916 /* No original tree */
921 /* Generate local tree */
922 ret = map_subtree_select_local(module, local_ctx, local_tree, tree);
927 /* Generate remote tree */
928 ret = map_subtree_collect_remote(module, remote_ctx, remote_tree, tree);
930 talloc_free(*local_tree);
937 /* Collect a list of attributes required either explicitly from a
938 * given list or implicitly from a given parse tree; split the
939 * collected list into local and remote parts. */
940 static int map_attrs_collect_and_partition(struct ldb_module *module, struct map_context *ac,
941 const char * const *search_attrs,
942 const struct ldb_parse_tree *tree)
945 const char **tree_attrs;
946 const char **remote_attrs;
947 const char **local_attrs;
950 /* There is no tree, just partition the searched attributes */
952 ret = map_attrs_partition(module, ac,
953 &local_attrs, &remote_attrs, search_attrs);
955 ac->local_attrs = local_attrs;
956 ac->remote_attrs = remote_attrs;
957 ac->all_attrs = search_attrs;
962 /* Create context for temporary memory */
963 tmp_ctx = talloc_new(ac);
964 if (tmp_ctx == NULL) {
968 /* Prepare list of attributes from tree */
969 tree_attrs = talloc_array(tmp_ctx, const char *, 1);
970 if (tree_attrs == NULL) {
971 talloc_free(tmp_ctx);
974 tree_attrs[0] = NULL;
976 /* Collect attributes from tree */
977 ret = ldb_parse_tree_collect_attrs(module, tmp_ctx, &tree_attrs, tree);
982 /* Merge attributes from search operation */
983 ret = map_attrs_merge(module, tmp_ctx, &tree_attrs, search_attrs);
988 /* Split local from remote attributes */
989 ret = map_attrs_partition(module, ac, &local_attrs,
990 &remote_attrs, tree_attrs);
993 ac->local_attrs = local_attrs;
994 ac->remote_attrs = remote_attrs;
995 talloc_steal(ac, tree_attrs);
996 ac->all_attrs = tree_attrs;
999 /* Free temporary memory */
1000 talloc_free(tmp_ctx);
1009 /* Outbound requests: search
1010 * ========================= */
1012 /* Pass a merged search result up the callback chain. */
1013 int map_up_callback(struct ldb_context *ldb, const struct ldb_request *req, struct ldb_reply *ares)
1017 /* No callback registered, stop */
1018 if (req->callback == NULL) {
1022 /* Only records need special treatment */
1023 if (ares->type != LDB_REPLY_ENTRY) {
1024 return req->callback(ldb, req->context, ares);
1027 /* Merged result doesn't match original query, skip */
1028 if (!ldb_match_msg(ldb, ares->message, req->op.search.tree, req->op.search.base, req->op.search.scope)) {
1029 ldb_debug(ldb, LDB_DEBUG_TRACE, "ldb_map: "
1030 "Skipping record '%s': "
1031 "doesn't match original search\n",
1032 ldb_dn_get_linearized(ares->message->dn));
1036 /* Limit result to requested attrs */
1037 if ((req->op.search.attrs) && (!ldb_attr_in_list(req->op.search.attrs, "*"))) {
1038 for (i = 0; i < ares->message->num_elements; ) {
1039 struct ldb_message_element *el = &ares->message->elements[i];
1040 if (!ldb_attr_in_list(req->op.search.attrs, el->name)) {
1041 ldb_msg_remove_element(ares->message, el);
1048 return req->callback(ldb, req->context, ares);
1051 /* Merge the remote and local parts of a search result. */
1052 int map_local_merge_callback(struct ldb_context *ldb, void *context, struct ldb_reply *ares)
1054 struct map_search_context *sc;
1057 if (context == NULL || ares == NULL) {
1058 ldb_set_errstring(ldb, talloc_asprintf(ldb, "ldb_map: "
1059 "NULL Context or Result in `map_local_merge_callback`"));
1060 return LDB_ERR_OPERATIONS_ERROR;
1063 sc = talloc_get_type(context, struct map_search_context);
1065 switch (ares->type) {
1066 case LDB_REPLY_ENTRY:
1067 /* We have already found a local record */
1068 if (sc->local_res) {
1069 ldb_set_errstring(ldb, talloc_asprintf(ldb, "ldb_map: "
1070 "Too many results to base search for local entry"));
1072 return LDB_ERR_OPERATIONS_ERROR;
1075 /* Store local result */
1076 sc->local_res = ares;
1078 /* Merge remote into local message */
1079 ret = ldb_msg_merge_local(sc->ac->module, ares->message, sc->remote_res->message);
1082 return LDB_ERR_OPERATIONS_ERROR;
1085 return map_up_callback(ldb, sc->ac->orig_req, ares);
1087 case LDB_REPLY_DONE:
1088 /* No local record found, continue with remote record */
1089 if (sc->local_res == NULL) {
1090 return map_up_callback(ldb, sc->ac->orig_req, sc->remote_res);
1095 ldb_set_errstring(ldb, talloc_asprintf(ldb, "ldb_map: "
1096 "Unexpected result type in base search for local entry"));
1098 return LDB_ERR_OPERATIONS_ERROR;
1102 /* Search the local part of a remote search result. */
1103 int map_remote_search_callback(struct ldb_context *ldb, void *context, struct ldb_reply *ares)
1105 struct map_context *ac;
1106 struct map_search_context *sc;
1107 struct ldb_request *req;
1110 if (context == NULL || ares == NULL) {
1111 ldb_set_errstring(ldb, talloc_asprintf(ldb, "ldb_map: "
1112 "NULL Context or Result in `map_remote_search_callback`"));
1113 return LDB_ERR_OPERATIONS_ERROR;
1116 ac = talloc_get_type(context, struct map_context);
1118 /* It's not a record, stop searching */
1119 if (ares->type != LDB_REPLY_ENTRY) {
1120 return map_up_callback(ldb, ac->orig_req, ares);
1123 /* Map result record into a local message */
1124 ret = map_reply_remote(ac, ares);
1127 return LDB_ERR_OPERATIONS_ERROR;
1130 /* There is no local db, stop searching */
1131 if (!map_check_local_db(ac->module)) {
1132 return map_up_callback(ldb, ac->orig_req, ares);
1135 /* Prepare local search context */
1136 sc = map_init_search_context(ac, ares);
1139 return LDB_ERR_OPERATIONS_ERROR;
1142 /* Prepare local search request */
1143 /* TODO: use GUIDs here instead? */
1145 ac->search_reqs = talloc_realloc(ac, ac->search_reqs, struct ldb_request *, ac->num_searches + 2);
1146 if (ac->search_reqs == NULL) {
1148 return LDB_ERR_OPERATIONS_ERROR;
1151 ac->search_reqs[ac->num_searches]
1152 = req = map_search_base_req(ac, ares->message->dn,
1153 NULL, NULL, sc, map_local_merge_callback);
1157 return LDB_ERR_OPERATIONS_ERROR;
1160 ac->search_reqs[ac->num_searches] = NULL;
1162 return ldb_next_request(ac->module, req);
1165 /* Search a record. */
1166 int map_search(struct ldb_module *module, struct ldb_request *req)
1168 struct ldb_handle *h;
1169 struct map_context *ac;
1170 struct ldb_parse_tree *local_tree, *remote_tree;
1173 const char *wildcard[] = { "*", NULL };
1174 const char * const *attrs;
1176 if (!module->private_data) /* if we're not yet initialized, go to the next module */
1177 return ldb_next_request(module, req);
1179 /* Do not manipulate our control entries */
1180 if (ldb_dn_is_special(req->op.search.base))
1181 return ldb_next_request(module, req);
1183 /* No mapping requested, skip to next module */
1184 if ((req->op.search.base) && (!ldb_dn_check_local(module, req->op.search.base))) {
1185 return ldb_next_request(module, req);
1188 /* TODO: How can we be sure about which partition we are
1189 * targetting when there is no search base? */
1191 /* Prepare context and handle */
1192 h = map_init_handle(req, module);
1194 return LDB_ERR_OPERATIONS_ERROR;
1196 ac = talloc_get_type(h->private_data, struct map_context);
1198 ac->search_reqs = talloc_array(ac, struct ldb_request *, 2);
1199 if (ac->search_reqs == NULL) {
1201 return LDB_ERR_OPERATIONS_ERROR;
1203 ac->num_searches = 1;
1204 ac->search_reqs[1] = NULL;
1206 /* Prepare the remote operation */
1207 ac->search_reqs[0] = talloc(ac, struct ldb_request);
1208 if (ac->search_reqs[0] == NULL) {
1212 *(ac->search_reqs[0]) = *req; /* copy the request */
1214 ac->search_reqs[0]->handle = h; /* return our own handle to deal with this call */
1216 ac->search_reqs[0]->context = ac;
1217 ac->search_reqs[0]->callback = map_remote_search_callback;
1219 /* It is easier to deal with the two different ways of
1220 * expressing the wildcard in the same codepath */
1221 attrs = req->op.search.attrs;
1222 if (attrs == NULL) {
1226 /* Split local from remote attrs */
1227 ret = map_attrs_collect_and_partition(module, ac,
1228 attrs, req->op.search.tree);
1233 ac->search_reqs[0]->op.search.attrs = ac->remote_attrs;
1235 /* Split local from remote tree */
1236 ret = ldb_parse_tree_partition(module, ac, ac->search_reqs[0],
1237 &local_tree, &remote_tree,
1238 req->op.search.tree);
1243 if (((local_tree != NULL) && (remote_tree != NULL)) &&
1244 (!ldb_parse_tree_check_splittable(req->op.search.tree))) {
1245 /* The query can't safely be split, enumerate the remote partition */
1250 if (local_tree == NULL) {
1251 /* Construct default local parse tree */
1252 local_tree = talloc_zero(ac, struct ldb_parse_tree);
1253 if (local_tree == NULL) {
1254 map_oom(ac->module);
1258 local_tree->operation = LDB_OP_PRESENT;
1259 local_tree->u.present.attr = talloc_strdup(local_tree, IS_MAPPED);
1261 if (remote_tree == NULL) {
1262 /* Construct default remote parse tree */
1263 remote_tree = ldb_parse_tree(ac->search_reqs[0], NULL);
1264 if (remote_tree == NULL) {
1269 ac->local_tree = local_tree;
1270 ac->search_reqs[0]->op.search.tree = remote_tree;
1272 ldb_set_timeout_from_prev_req(module->ldb, req, ac->search_reqs[0]);
1274 h->state = LDB_ASYNC_INIT;
1275 h->status = LDB_SUCCESS;
1277 ac->step = MAP_SEARCH_REMOTE;
1279 ret = ldb_next_remote_request(module, ac->search_reqs[0]);
1280 if (ret == LDB_SUCCESS) {
1289 return LDB_ERR_OPERATIONS_ERROR;