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
7 Copyright (C) Simo Sorce <idra@samba.org> 2008
9 ** NOTE! The following LGPL license applies to the ldb
10 ** library. This does NOT imply that all of Samba is released
13 This library is free software; you can redistribute it and/or
14 modify it under the terms of the GNU Lesser General Public
15 License as published by the Free Software Foundation; either
16 version 3 of the License, or (at your option) any later version.
18 This library is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 Lesser General Public License for more details.
23 You should have received a copy of the GNU Lesser General Public
24 License along with this library; if not, see <http://www.gnu.org/licenses/>.
28 #include "ldb_includes.h"
30 #include "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(discard_const_p(char, 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
->values
= talloc_array(el
, struct ldb_val
, old
->num_values
);
232 if (el
->values
== NULL
) {
238 el
->name
= talloc_strdup(el
, attr_name
);
239 if (el
->name
== NULL
) {
245 for (i
= 0; i
< old
->num_values
; i
++) {
246 el
->values
[i
] = ldb_val_map_remote(module
, el
->values
, map
, &old
->values
[i
]);
247 /* Conversions might fail, in which case bail */
248 if (!el
->values
[i
].data
) {
258 /* Merge a remote message element into a local message. */
259 static int ldb_msg_el_merge(struct ldb_module
*module
, struct ldb_message
*local
,
260 struct ldb_message
*remote
, const char *attr_name
)
262 const struct ldb_map_context
*data
= map_get_context(module
);
263 const struct ldb_map_attribute
*map
;
264 struct ldb_message_element
*old
, *el
=NULL
;
265 const char *remote_name
= NULL
;
266 struct ldb_context
*ldb
;
268 ldb
= ldb_module_get_ctx(module
);
270 /* We handle wildcards in ldb_msg_el_merge_wildcard */
271 if (ldb_attr_cmp(attr_name
, "*") == 0) {
275 map
= map_attr_find_local(data
, attr_name
);
277 /* Unknown attribute in remote message:
278 * skip, attribute was probably auto-generated */
287 remote_name
= map
->u
.convert
.remote_name
;
290 remote_name
= attr_name
;
293 remote_name
= map
->u
.rename
.remote_name
;
304 if (map
->u
.convert
.convert_remote
== NULL
) {
305 ldb_debug(ldb
, LDB_DEBUG_ERROR
, "ldb_map: "
306 "Skipping attribute '%s': "
307 "'convert_remote' not set",
314 old
= ldb_msg_find_element(remote
, remote_name
);
316 el
= ldb_msg_el_map_remote(module
, local
, map
, attr_name
, old
);
318 return LDB_ERR_NO_SUCH_ATTRIBUTE
;
323 if (map
->u
.generate
.generate_local
== NULL
) {
324 ldb_debug(ldb
, LDB_DEBUG_ERROR
, "ldb_map: "
325 "Skipping attribute '%s': "
326 "'generate_local' not set",
331 el
= map
->u
.generate
.generate_local(module
, local
, attr_name
, remote
);
333 /* Generation failure is probably due to lack of source attributes */
334 return LDB_ERR_NO_SUCH_ATTRIBUTE
;
340 return LDB_ERR_NO_SUCH_ATTRIBUTE
;
343 return ldb_msg_replace(local
, el
);
346 /* Handle wildcard parts of merging a remote message element into a local message. */
347 static int ldb_msg_el_merge_wildcard(struct ldb_module
*module
, struct ldb_message
*local
,
348 struct ldb_message
*remote
)
350 const struct ldb_map_context
*data
= map_get_context(module
);
351 const struct ldb_map_attribute
*map
= map_attr_find_local(data
, "*");
352 struct ldb_message_element
*el
=NULL
;
355 /* Perhaps we have a mapping for "*" */
356 if (map
&& map
->type
== MAP_KEEP
) {
357 /* We copy everything over, and hope that anything with a
358 more specific rule is overwritten */
359 for (i
= 0; i
< remote
->num_elements
; i
++) {
360 el
= ldb_msg_el_map_remote(module
, local
, map
, remote
->elements
[i
].name
,
361 &remote
->elements
[i
]);
363 return LDB_ERR_OPERATIONS_ERROR
;
366 ret
= ldb_msg_replace(local
, el
);
373 /* Now walk the list of possible mappings, and apply each */
374 for (i
= 0; data
->attribute_maps
[i
].local_name
; i
++) {
375 ret
= ldb_msg_el_merge(module
, local
, remote
,
376 data
->attribute_maps
[i
].local_name
);
377 if (ret
== LDB_ERR_NO_SUCH_ATTRIBUTE
) {
390 * ================ */
392 /* Merge two local messages into a single one. */
393 static int ldb_msg_merge_local(struct ldb_module
*module
, struct ldb_message
*msg1
, struct ldb_message
*msg2
)
397 for (i
= 0; i
< msg2
->num_elements
; i
++) {
398 ret
= ldb_msg_replace(msg1
, &msg2
->elements
[i
]);
407 /* Merge a local and a remote message into a single local one. */
408 static int ldb_msg_merge_remote(struct map_context
*ac
, struct ldb_message
*local
,
409 struct ldb_message
*remote
)
412 const char * const *attrs
= ac
->all_attrs
;
414 ret
= ldb_msg_el_merge_wildcard(ac
->module
, local
, remote
);
420 for (i
= 0; attrs
&& attrs
[i
]; i
++) {
421 if (ldb_attr_cmp(attrs
[i
], "*") == 0) {
422 ret
= ldb_msg_el_merge_wildcard(ac
->module
, local
, remote
);
430 /* Try to map each attribute back;
431 * Add to local message is possible,
432 * Overwrite old local attribute if necessary */
433 for (i
= 0; attrs
&& attrs
[i
]; i
++) {
434 ret
= ldb_msg_el_merge(ac
->module
, local
, remote
,
436 if (ret
== LDB_ERR_NO_SUCH_ATTRIBUTE
) {
445 /* Mapping search results
446 * ====================== */
448 /* Map a search result back into the local partition. */
449 static int map_reply_remote(struct map_context
*ac
, struct ldb_reply
*ares
)
451 struct ldb_message
*msg
;
455 /* There is no result message, skip */
456 if (ares
->type
!= LDB_REPLY_ENTRY
) {
460 /* Create a new result message */
461 msg
= ldb_msg_new(ares
);
467 /* Merge remote message into new message */
468 ret
= ldb_msg_merge_remote(ac
, msg
, ares
->message
);
474 /* Create corresponding local DN */
475 dn
= ldb_dn_map_rebase_remote(ac
->module
, msg
, ares
->message
->dn
);
482 /* Store new message with new DN as the result */
483 talloc_free(ares
->message
);
489 /* Mapping parse trees
490 * =================== */
492 /* Check whether a parse tree can safely be split in two. */
493 static bool ldb_parse_tree_check_splittable(const struct ldb_parse_tree
*tree
)
495 const struct ldb_parse_tree
*subtree
= tree
;
499 switch (subtree
->operation
) {
502 subtree
= subtree
->u
.isnot
.child
;
506 return !negate
; /* if negate: False */
509 return negate
; /* if negate: True */
512 return true; /* simple parse tree */
516 return true; /* no parse tree */
519 /* Collect a list of attributes required to match a given parse tree. */
520 static int ldb_parse_tree_collect_attrs(struct ldb_module
*module
, void *mem_ctx
, const char ***attrs
, const struct ldb_parse_tree
*tree
)
522 const char **new_attrs
;
529 switch (tree
->operation
) {
531 case LDB_OP_AND
: /* attributes stored in list of subtrees */
532 for (i
= 0; i
< tree
->u
.list
.num_elements
; i
++) {
533 ret
= ldb_parse_tree_collect_attrs(module
, mem_ctx
,
534 attrs
, tree
->u
.list
.elements
[i
]);
541 case LDB_OP_NOT
: /* attributes stored in single subtree */
542 return ldb_parse_tree_collect_attrs(module
, mem_ctx
, attrs
, tree
->u
.isnot
.child
);
544 default: /* single attribute in tree */
545 new_attrs
= ldb_attr_list_copy_add(mem_ctx
, *attrs
, tree
->u
.equality
.attr
);
554 static int map_subtree_select_local(struct ldb_module
*module
, void *mem_ctx
, struct ldb_parse_tree
**new, const struct ldb_parse_tree
*tree
);
556 /* Select a negated subtree that queries attributes in the local partition */
557 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
)
559 struct ldb_parse_tree
*child
;
562 /* Prepare new tree */
563 *new = talloc_memdup(mem_ctx
, tree
, sizeof(struct ldb_parse_tree
));
569 /* Generate new subtree */
570 ret
= map_subtree_select_local(module
, *new, &child
, tree
->u
.isnot
.child
);
576 /* Prune tree without subtree */
583 (*new)->u
.isnot
.child
= child
;
588 /* Select a list of subtrees that query attributes in the local partition */
589 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
)
593 /* Prepare new tree */
594 *new = talloc_memdup(mem_ctx
, tree
, sizeof(struct ldb_parse_tree
));
600 /* Prepare list of subtrees */
601 (*new)->u
.list
.num_elements
= 0;
602 (*new)->u
.list
.elements
= talloc_array(*new, struct ldb_parse_tree
*, tree
->u
.list
.num_elements
);
603 if ((*new)->u
.list
.elements
== NULL
) {
609 /* Generate new list of subtrees */
611 for (i
= 0; i
< tree
->u
.list
.num_elements
; i
++) {
612 struct ldb_parse_tree
*child
;
613 ret
= map_subtree_select_local(module
, *new, &child
, tree
->u
.list
.elements
[i
]);
620 (*new)->u
.list
.elements
[j
] = child
;
625 /* Prune tree without subtrees */
632 /* Fix subtree list size */
633 (*new)->u
.list
.num_elements
= j
;
634 (*new)->u
.list
.elements
= talloc_realloc(*new, (*new)->u
.list
.elements
, struct ldb_parse_tree
*, (*new)->u
.list
.num_elements
);
639 /* Select a simple subtree that queries attributes in the local partition */
640 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
)
642 /* Prepare new tree */
643 *new = talloc_memdup(mem_ctx
, tree
, sizeof(struct ldb_parse_tree
));
652 /* Select subtrees that query attributes in the local partition */
653 static int map_subtree_select_local(struct ldb_module
*module
, void *mem_ctx
, struct ldb_parse_tree
**new, const struct ldb_parse_tree
*tree
)
655 const struct ldb_map_context
*data
= map_get_context(module
);
661 if (tree
->operation
== LDB_OP_NOT
) {
662 return map_subtree_select_local_not(module
, mem_ctx
, new, tree
);
665 if (tree
->operation
== LDB_OP_AND
|| tree
->operation
== LDB_OP_OR
) {
666 return map_subtree_select_local_list(module
, mem_ctx
, new, tree
);
669 if (map_attr_check_remote(data
, tree
->u
.equality
.attr
)) {
674 return map_subtree_select_local_simple(module
, mem_ctx
, new, tree
);
677 static int map_subtree_collect_remote(struct ldb_module
*module
, void *mem_ctx
, struct ldb_parse_tree
**new, const struct ldb_parse_tree
*tree
);
679 /* Collect a negated subtree that queries attributes in the remote partition */
680 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
)
682 struct ldb_parse_tree
*child
;
685 /* Prepare new tree */
686 *new = talloc_memdup(mem_ctx
, tree
, sizeof(struct ldb_parse_tree
));
692 /* Generate new subtree */
693 ret
= map_subtree_collect_remote(module
, *new, &child
, tree
->u
.isnot
.child
);
699 /* Prune tree without subtree */
706 (*new)->u
.isnot
.child
= child
;
711 /* Collect a list of subtrees that query attributes in the remote partition */
712 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
)
716 /* Prepare new tree */
717 *new = talloc_memdup(mem_ctx
, tree
, sizeof(struct ldb_parse_tree
));
723 /* Prepare list of subtrees */
724 (*new)->u
.list
.num_elements
= 0;
725 (*new)->u
.list
.elements
= talloc_array(*new, struct ldb_parse_tree
*, tree
->u
.list
.num_elements
);
726 if ((*new)->u
.list
.elements
== NULL
) {
732 /* Generate new list of subtrees */
734 for (i
= 0; i
< tree
->u
.list
.num_elements
; i
++) {
735 struct ldb_parse_tree
*child
;
736 ret
= map_subtree_collect_remote(module
, *new, &child
, tree
->u
.list
.elements
[i
]);
743 (*new)->u
.list
.elements
[j
] = child
;
748 /* Prune tree without subtrees */
755 /* Fix subtree list size */
756 (*new)->u
.list
.num_elements
= j
;
757 (*new)->u
.list
.elements
= talloc_realloc(*new, (*new)->u
.list
.elements
, struct ldb_parse_tree
*, (*new)->u
.list
.num_elements
);
762 /* Collect a simple subtree that queries attributes in the remote partition */
763 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
)
767 /* Prepare new tree */
768 *new = talloc(mem_ctx
, struct ldb_parse_tree
);
775 if (map
->type
== MAP_KEEP
) {
776 /* Nothing to do here */
780 /* Store attribute and value in new tree */
781 switch (tree
->operation
) {
783 attr
= map_attr_map_local(*new, map
, tree
->u
.present
.attr
);
784 (*new)->u
.present
.attr
= attr
;
786 case LDB_OP_SUBSTRING
:
788 attr
= map_attr_map_local(*new, map
, tree
->u
.substring
.attr
);
789 (*new)->u
.substring
.attr
= attr
;
792 case LDB_OP_EQUALITY
:
793 attr
= map_attr_map_local(*new, map
, tree
->u
.equality
.attr
);
794 (*new)->u
.equality
.attr
= attr
;
799 attr
= map_attr_map_local(*new, map
, tree
->u
.comparison
.attr
);
800 (*new)->u
.comparison
.attr
= attr
;
802 case LDB_OP_EXTENDED
:
803 attr
= map_attr_map_local(*new, map
, tree
->u
.extended
.attr
);
804 (*new)->u
.extended
.attr
= attr
;
806 default: /* unknown kind of simple subtree */
817 if (map
->type
== MAP_RENAME
) {
818 /* Nothing more to do here, the attribute has been renamed */
822 /* Store attribute and value in new tree */
823 switch (tree
->operation
) {
826 case LDB_OP_SUBSTRING
:
830 (*new)->u
.substring
.chunks
= NULL
;
831 for (i
=0; tree
->u
.substring
.chunks
[i
]; i
++) {
832 (*new)->u
.substring
.chunks
= talloc_realloc(*new, (*new)->u
.substring
.chunks
, struct ldb_val
*, i
+2);
833 if (!(*new)->u
.substring
.chunks
) {
838 (*new)->u
.substring
.chunks
[i
] = talloc(*new, struct ldb_val
);
839 if (!(*new)->u
.substring
.chunks
[i
]) {
844 *(*new)->u
.substring
.chunks
[i
] = ldb_val_map_local(module
, *new, map
, tree
->u
.substring
.chunks
[i
]);
845 (*new)->u
.substring
.chunks
[i
+1] = NULL
;
849 case LDB_OP_EQUALITY
:
850 (*new)->u
.equality
.value
= ldb_val_map_local(module
, *new, map
, &tree
->u
.equality
.value
);
855 (*new)->u
.comparison
.value
= ldb_val_map_local(module
, *new, map
, &tree
->u
.comparison
.value
);
857 case LDB_OP_EXTENDED
:
858 (*new)->u
.extended
.value
= ldb_val_map_local(module
, *new, map
, &tree
->u
.extended
.value
);
859 (*new)->u
.extended
.rule_id
= talloc_strdup(*new, tree
->u
.extended
.rule_id
);
861 default: /* unknown kind of simple subtree */
869 /* Collect subtrees that query attributes in the remote partition */
870 static int map_subtree_collect_remote(struct ldb_module
*module
, void *mem_ctx
, struct ldb_parse_tree
**new, const struct ldb_parse_tree
*tree
)
872 const struct ldb_map_context
*data
= map_get_context(module
);
873 const struct ldb_map_attribute
*map
;
874 struct ldb_context
*ldb
;
876 ldb
= ldb_module_get_ctx(module
);
882 if (tree
->operation
== LDB_OP_NOT
) {
883 return map_subtree_collect_remote_not(module
, mem_ctx
, new, tree
);
886 if ((tree
->operation
== LDB_OP_AND
) || (tree
->operation
== LDB_OP_OR
)) {
887 return map_subtree_collect_remote_list(module
, mem_ctx
, new, tree
);
890 if (!map_attr_check_remote(data
, tree
->u
.equality
.attr
)) {
895 map
= map_attr_find_local(data
, tree
->u
.equality
.attr
);
896 if (map
->convert_operator
) {
897 return map
->convert_operator(module
, mem_ctx
, new, tree
);
900 if (map
->type
== MAP_GENERATE
) {
901 ldb_debug(ldb
, LDB_DEBUG_WARNING
, "ldb_map: "
902 "Skipping attribute '%s': "
903 "'convert_operator' not set",
904 tree
->u
.equality
.attr
);
909 return map_subtree_collect_remote_simple(module
, mem_ctx
, new, tree
, map
);
912 /* Split subtrees that query attributes in the local partition from
913 * those that query the remote partition. */
914 static int ldb_parse_tree_partition(struct ldb_module
*module
,
916 struct ldb_parse_tree
**local_tree
,
917 struct ldb_parse_tree
**remote_tree
,
918 const struct ldb_parse_tree
*tree
)
925 /* No original tree */
930 /* Generate local tree */
931 ret
= map_subtree_select_local(module
, mem_ctx
, local_tree
, tree
);
936 /* Generate remote tree */
937 ret
= map_subtree_collect_remote(module
, mem_ctx
, remote_tree
, tree
);
939 talloc_free(*local_tree
);
946 /* Collect a list of attributes required either explicitly from a
947 * given list or implicitly from a given parse tree; split the
948 * collected list into local and remote parts. */
949 static int map_attrs_collect_and_partition(struct ldb_module
*module
, struct map_context
*ac
,
950 const char * const *search_attrs
,
951 const struct ldb_parse_tree
*tree
)
954 const char **tree_attrs
;
955 const char **remote_attrs
;
956 const char **local_attrs
;
959 /* There is no tree, just partition the searched attributes */
961 ret
= map_attrs_partition(module
, ac
,
962 &local_attrs
, &remote_attrs
, search_attrs
);
964 ac
->local_attrs
= local_attrs
;
965 ac
->remote_attrs
= remote_attrs
;
966 ac
->all_attrs
= search_attrs
;
971 /* Create context for temporary memory */
972 tmp_ctx
= talloc_new(ac
);
973 if (tmp_ctx
== NULL
) {
977 /* Prepare list of attributes from tree */
978 tree_attrs
= talloc_array(tmp_ctx
, const char *, 1);
979 if (tree_attrs
== NULL
) {
980 talloc_free(tmp_ctx
);
983 tree_attrs
[0] = NULL
;
985 /* Collect attributes from tree */
986 ret
= ldb_parse_tree_collect_attrs(module
, tmp_ctx
, &tree_attrs
, tree
);
991 /* Merge attributes from search operation */
992 ret
= map_attrs_merge(module
, tmp_ctx
, &tree_attrs
, search_attrs
);
997 /* Split local from remote attributes */
998 ret
= map_attrs_partition(module
, ac
, &local_attrs
,
999 &remote_attrs
, tree_attrs
);
1002 ac
->local_attrs
= local_attrs
;
1003 ac
->remote_attrs
= remote_attrs
;
1004 talloc_steal(ac
, tree_attrs
);
1005 ac
->all_attrs
= tree_attrs
;
1008 /* Free temporary memory */
1009 talloc_free(tmp_ctx
);
1018 /* Outbound requests: search
1019 * ========================= */
1021 static int map_remote_search_callback(struct ldb_request
*req
,
1022 struct ldb_reply
*ares
);
1023 static int map_local_merge_callback(struct ldb_request
*req
,
1024 struct ldb_reply
*ares
);
1025 static int map_search_local(struct map_context
*ac
);
1027 static int map_save_entry(struct map_context
*ac
, struct ldb_reply
*ares
)
1029 struct map_reply
*mr
;
1031 mr
= talloc_zero(ac
, struct map_reply
);
1033 map_oom(ac
->module
);
1034 return LDB_ERR_OPERATIONS_ERROR
;
1036 mr
->remote
= talloc_steal(mr
, ares
);
1037 if (ac
->r_current
) {
1038 ac
->r_current
->next
= mr
;
1048 /* Pass a merged search result up the callback chain. */
1049 int map_return_entry(struct map_context
*ac
, struct ldb_reply
*ares
)
1051 struct ldb_message_element
*el
;
1052 const char * const *attrs
;
1053 struct ldb_context
*ldb
;
1056 ldb
= ldb_module_get_ctx(ac
->module
);
1058 /* Merged result doesn't match original query, skip */
1059 if (!ldb_match_msg(ldb
, ares
->message
,
1060 ac
->req
->op
.search
.tree
,
1061 ac
->req
->op
.search
.base
,
1062 ac
->req
->op
.search
.scope
)) {
1063 ldb_debug(ldb
, LDB_DEBUG_TRACE
, "ldb_map: "
1064 "Skipping record '%s': "
1065 "doesn't match original search",
1066 ldb_dn_get_linearized(ares
->message
->dn
));
1070 /* Limit result to requested attrs */
1071 if (ac
->req
->op
.search
.attrs
&&
1072 (! ldb_attr_in_list(ac
->req
->op
.search
.attrs
, "*"))) {
1074 attrs
= ac
->req
->op
.search
.attrs
;
1077 while (i
< ares
->message
->num_elements
) {
1079 el
= &ares
->message
->elements
[i
];
1080 if ( ! ldb_attr_in_list(attrs
, el
->name
)) {
1081 ldb_msg_remove_element(ares
->message
, el
);
1088 return ldb_module_send_entry(ac
->req
, ares
->message
, ares
->controls
);
1091 /* Search a record. */
1092 int map_search(struct ldb_module
*module
, struct ldb_request
*req
)
1094 struct ldb_parse_tree
*remote_tree
;
1095 struct ldb_parse_tree
*local_tree
;
1096 struct ldb_request
*remote_req
;
1097 struct ldb_context
*ldb
;
1098 struct map_context
*ac
;
1101 const char *wildcard
[] = { "*", NULL
};
1102 const char * const *attrs
;
1104 ldb
= ldb_module_get_ctx(module
);
1106 /* if we're not yet initialized, go to the next module */
1107 if (!ldb_module_get_private(module
))
1108 return ldb_next_request(module
, req
);
1110 /* Do not manipulate our control entries */
1111 if (ldb_dn_is_special(req
->op
.search
.base
)) {
1112 return ldb_next_request(module
, req
);
1115 /* No mapping requested, skip to next module */
1116 if ((req
->op
.search
.base
) && (!ldb_dn_check_local(module
, req
->op
.search
.base
))) {
1117 return ldb_next_request(module
, req
);
1120 /* TODO: How can we be sure about which partition we are
1121 * targetting when there is no search base? */
1123 /* Prepare context and handle */
1124 ac
= map_init_context(module
, req
);
1126 return LDB_ERR_OPERATIONS_ERROR
;
1129 /* It is easier to deal with the two different ways of
1130 * expressing the wildcard in the same codepath */
1131 attrs
= req
->op
.search
.attrs
;
1132 if (attrs
== NULL
) {
1136 /* Split local from remote attrs */
1137 ret
= map_attrs_collect_and_partition(module
, ac
,
1138 attrs
, req
->op
.search
.tree
);
1140 return LDB_ERR_OPERATIONS_ERROR
;
1143 /* Split local from remote tree */
1144 ret
= ldb_parse_tree_partition(module
, ac
,
1145 &local_tree
, &remote_tree
,
1146 req
->op
.search
.tree
);
1148 return LDB_ERR_OPERATIONS_ERROR
;
1151 if (((local_tree
!= NULL
) && (remote_tree
!= NULL
)) &&
1152 (!ldb_parse_tree_check_splittable(req
->op
.search
.tree
))) {
1153 /* The query can't safely be split, enumerate the remote partition */
1158 if (local_tree
== NULL
) {
1159 /* Construct default local parse tree */
1160 local_tree
= talloc_zero(ac
, struct ldb_parse_tree
);
1161 if (local_tree
== NULL
) {
1162 map_oom(ac
->module
);
1163 return LDB_ERR_OPERATIONS_ERROR
;
1166 local_tree
->operation
= LDB_OP_PRESENT
;
1167 local_tree
->u
.present
.attr
= talloc_strdup(local_tree
, IS_MAPPED
);
1169 if (remote_tree
== NULL
) {
1170 /* Construct default remote parse tree */
1171 remote_tree
= ldb_parse_tree(ac
, NULL
);
1172 if (remote_tree
== NULL
) {
1173 return LDB_ERR_OPERATIONS_ERROR
;
1177 ac
->local_tree
= local_tree
;
1179 /* Prepare the remote operation */
1180 ret
= ldb_build_search_req_ex(&remote_req
, ldb
, ac
,
1181 req
->op
.search
.base
,
1182 req
->op
.search
.scope
,
1186 ac
, map_remote_search_callback
,
1188 if (ret
!= LDB_SUCCESS
) {
1189 return LDB_ERR_OPERATIONS_ERROR
;
1192 return ldb_next_remote_request(module
, remote_req
);
1195 /* Now, search the local part of a remote search result. */
1196 static int map_remote_search_callback(struct ldb_request
*req
,
1197 struct ldb_reply
*ares
)
1199 struct map_context
*ac
;
1202 ac
= talloc_get_type(req
->context
, struct map_context
);
1205 return ldb_module_done(ac
->req
, NULL
, NULL
,
1206 LDB_ERR_OPERATIONS_ERROR
);
1208 if (ares
->error
!= LDB_SUCCESS
) {
1209 return ldb_module_done(ac
->req
, ares
->controls
,
1210 ares
->response
, ares
->error
);
1213 switch (ares
->type
) {
1214 case LDB_REPLY_REFERRAL
:
1216 /* ignore referrals */
1220 case LDB_REPLY_ENTRY
:
1222 /* Map result record into a local message */
1223 ret
= map_reply_remote(ac
, ares
);
1226 return ldb_module_done(ac
->req
, NULL
, NULL
,
1227 LDB_ERR_OPERATIONS_ERROR
);
1230 /* if we have no local db, then we can just return the reply to
1231 * the upper layer, otherwise we must save it and process it
1232 * when all replies ahve been gathered */
1233 if ( ! map_check_local_db(ac
->module
)) {
1234 ret
= map_return_entry(ac
, ares
);
1236 ret
= map_save_entry(ac
,ares
);
1239 if (ret
!= LDB_SUCCESS
) {
1241 return ldb_module_done(ac
->req
, NULL
, NULL
,
1242 LDB_ERR_OPERATIONS_ERROR
);
1246 case LDB_REPLY_DONE
:
1248 if ( ! map_check_local_db(ac
->module
)) {
1249 return ldb_module_done(ac
->req
, ares
->controls
,
1250 ares
->response
, LDB_SUCCESS
);
1255 /* reset the pointer to the start of the list */
1256 ac
->r_current
= ac
->r_list
;
1258 /* no entry just return */
1259 if (ac
->r_current
== NULL
) {
1260 return ldb_module_done(ac
->req
, ares
->controls
,
1261 ares
->response
, LDB_SUCCESS
);
1264 ret
= map_search_local(ac
);
1265 if (ret
!= LDB_SUCCESS
) {
1266 return ldb_module_done(ac
->req
, NULL
, NULL
, ret
);
1273 static int map_search_local(struct map_context
*ac
)
1275 struct ldb_request
*search_req
;
1277 if (ac
->r_current
== NULL
|| ac
->r_current
->remote
== NULL
) {
1278 return LDB_ERR_OPERATIONS_ERROR
;
1281 /* Prepare local search request */
1282 /* TODO: use GUIDs here instead? */
1283 search_req
= map_search_base_req(ac
,
1284 ac
->r_current
->remote
->message
->dn
,
1286 ac
, map_local_merge_callback
);
1287 if (search_req
== NULL
) {
1288 return LDB_ERR_OPERATIONS_ERROR
;
1291 return ldb_next_request(ac
->module
, search_req
);
1294 /* Merge the remote and local parts of a search result. */
1295 int map_local_merge_callback(struct ldb_request
*req
, struct ldb_reply
*ares
)
1297 struct ldb_context
*ldb
;
1298 struct map_context
*ac
;
1301 ac
= talloc_get_type(req
->context
, struct map_context
);
1302 ldb
= ldb_module_get_ctx(ac
->module
);
1305 return ldb_module_done(ac
->req
, NULL
, NULL
,
1306 LDB_ERR_OPERATIONS_ERROR
);
1308 if (ares
->error
!= LDB_SUCCESS
) {
1309 return ldb_module_done(ac
->req
, ares
->controls
,
1310 ares
->response
, ares
->error
);
1313 switch (ares
->type
) {
1314 case LDB_REPLY_ENTRY
:
1315 /* We have already found a local record */
1316 if (ac
->r_current
->local
) {
1318 ldb_set_errstring(ldb
, "ldb_map: Too many results!");
1319 return ldb_module_done(ac
->req
, NULL
, NULL
,
1320 LDB_ERR_OPERATIONS_ERROR
);
1323 /* Store local result */
1324 ac
->r_current
->local
= talloc_steal(ac
->r_current
, ares
);
1328 case LDB_REPLY_REFERRAL
:
1329 /* ignore referrals */
1333 case LDB_REPLY_DONE
:
1336 /* No local record found, map and send remote record */
1337 if (ac
->r_current
->local
!= NULL
) {
1338 /* Merge remote into local message */
1339 ret
= ldb_msg_merge_local(ac
->module
,
1340 ac
->r_current
->local
->message
,
1341 ac
->r_current
->remote
->message
);
1342 if (ret
== LDB_SUCCESS
) {
1343 ret
= map_return_entry(ac
, ac
->r_current
->local
);
1345 if (ret
!= LDB_SUCCESS
) {
1346 return ldb_module_done(ac
->req
, NULL
, NULL
,
1347 LDB_ERR_OPERATIONS_ERROR
);
1350 ret
= map_return_entry(ac
, ac
->r_current
->remote
);
1351 if (ret
!= LDB_SUCCESS
) {
1352 return ldb_module_done(ac
->req
,
1357 if (ac
->r_current
->next
!= NULL
) {
1358 ac
->r_current
= ac
->r_current
->next
;
1359 if (ac
->r_current
->remote
->type
== LDB_REPLY_ENTRY
) {
1360 ret
= map_search_local(ac
);
1361 if (ret
!= LDB_SUCCESS
) {
1362 return ldb_module_done(ac
->req
,
1369 /* ok we are done with all search, finally it is time to
1370 * finish operations for this module */
1371 return ldb_module_done(ac
->req
,
1372 ac
->r_current
->remote
->controls
,
1373 ac
->r_current
->remote
->response
,
1374 ac
->r_current
->remote
->error
);