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/>.
29 #include "system/filesys.h"
30 #include "system/time.h"
32 #include "ldb_map_private.h"
36 * ================== */
38 /* Select attributes that stay in the local partition. */
39 static const char **map_attrs_select_local(struct ldb_module
*module
, void *mem_ctx
, const char * const *attrs
)
41 const struct ldb_map_context
*data
= map_get_context(module
);
49 result
= talloc_array(mem_ctx
, const char *, 1);
55 for (i
= 0; attrs
[i
]; i
++) {
56 /* Wildcards and ignored attributes are kept locally */
57 if ((ldb_attr_cmp(attrs
[i
], "*") == 0) ||
58 (!map_attr_check_remote(data
, attrs
[i
]))) {
59 result
= talloc_realloc(mem_ctx
, result
, const char *, last
+2);
64 result
[last
] = talloc_strdup(result
, attrs
[i
]);
65 result
[last
+1] = NULL
;
78 /* Collect attributes that are mapped into the remote partition. */
79 static const char **map_attrs_collect_remote(struct ldb_module
*module
, void *mem_ctx
,
80 const char * const *attrs
)
82 const struct ldb_map_context
*data
= map_get_context(module
);
84 const struct ldb_map_attribute
*map
;
85 const char *name
=NULL
;
86 unsigned int i
, j
, last
;
90 result
= talloc_array(mem_ctx
, const char *, 1);
96 for (i
= 0; attrs
[i
]; i
++) {
97 /* Wildcards are kept remotely, too */
98 if (ldb_attr_cmp(attrs
[i
], "*") == 0) {
99 const char **new_attrs
= NULL
;
100 ret
= map_attrs_merge(module
, mem_ctx
, &new_attrs
, attrs
);
101 if (ret
!= LDB_SUCCESS
) {
104 ret
= map_attrs_merge(module
, mem_ctx
, &new_attrs
, data
->wildcard_attributes
);
105 if (ret
!= LDB_SUCCESS
) {
114 for (i
= 0; attrs
[i
]; i
++) {
115 /* Wildcards are kept remotely, too */
116 if (ldb_attr_cmp(attrs
[i
], "*") == 0) {
117 /* Add all 'include in wildcard' attributes */
122 /* Add remote names of mapped attrs */
123 map
= map_attr_find_local(data
, attrs
[i
]);
137 case LDB_MAP_RENDROP
:
138 case LDB_MAP_CONVERT
:
139 name
= map
->u
.rename
.remote_name
;
142 case LDB_MAP_GENERATE
:
143 /* Add all remote names of "generate" attrs */
144 for (j
= 0; map
->u
.generate
.remote_names
[j
]; j
++) {
145 result
= talloc_realloc(mem_ctx
, result
, const char *, last
+2);
146 if (result
== NULL
) {
150 result
[last
] = talloc_strdup(result
, map
->u
.generate
.remote_names
[j
]);
151 result
[last
+1] = NULL
;
157 named
: /* We found a single remote name, add that */
158 result
= talloc_realloc(mem_ctx
, result
, const char *, last
+2);
159 if (result
== NULL
) {
163 result
[last
] = talloc_strdup(result
, name
);
164 result
[last
+1] = NULL
;
176 /* Split attributes that stay in the local partition from those that
177 * are mapped into the remote partition. */
178 static int map_attrs_partition(struct ldb_module
*module
, void *mem_ctx
, const char ***local_attrs
, const char ***remote_attrs
, const char * const *attrs
)
180 *local_attrs
= map_attrs_select_local(module
, mem_ctx
, attrs
);
181 *remote_attrs
= map_attrs_collect_remote(module
, mem_ctx
, attrs
);
186 /* Mapping message elements
187 * ======================== */
189 /* Add an element to a message, overwriting any old identically named elements. */
190 static int ldb_msg_replace(struct ldb_message
*msg
, const struct ldb_message_element
*el
)
192 struct ldb_message_element
*old
;
194 old
= ldb_msg_find_element(msg
, el
->name
);
196 /* no local result, add as new element */
198 if (ldb_msg_add_empty(msg
, el
->name
, 0, &old
) != 0) {
199 return LDB_ERR_OPERATIONS_ERROR
;
201 talloc_free(discard_const_p(char, old
->name
));
204 /* copy new element */
207 /* and make sure we reference the contents */
208 if (!talloc_reference(msg
->elements
, el
->name
)) {
209 return LDB_ERR_OPERATIONS_ERROR
;
211 if (!talloc_reference(msg
->elements
, el
->values
)) {
212 return LDB_ERR_OPERATIONS_ERROR
;
218 /* Map a message element back into the local partition. */
219 static struct ldb_message_element
*ldb_msg_el_map_remote(struct ldb_module
*module
,
221 const struct ldb_map_attribute
*map
,
222 const char *attr_name
,
223 const struct ldb_message_element
*old
)
225 const struct ldb_map_context
*data
= map_get_context(module
);
226 const char *local_attr_name
= attr_name
;
227 struct ldb_message_element
*el
;
230 el
= talloc_zero(mem_ctx
, struct ldb_message_element
);
236 el
->values
= talloc_array(el
, struct ldb_val
, old
->num_values
);
237 if (el
->values
== NULL
) {
243 for (i
= 0; data
->attribute_maps
[i
].local_name
; i
++) {
244 struct ldb_map_attribute
*am
= &data
->attribute_maps
[i
];
245 if (((am
->type
== LDB_MAP_RENAME
|| am
->type
== LDB_MAP_RENDROP
) &&
246 !strcmp(am
->u
.rename
.remote_name
, attr_name
))
247 || (am
->type
== LDB_MAP_CONVERT
&&
248 !strcmp(am
->u
.convert
.remote_name
, attr_name
))) {
250 local_attr_name
= am
->local_name
;
255 el
->name
= talloc_strdup(el
, local_attr_name
);
256 if (el
->name
== NULL
) {
262 for (i
= 0; i
< old
->num_values
; i
++) {
263 el
->values
[i
] = ldb_val_map_remote(module
, el
->values
, map
, &old
->values
[i
]);
264 /* Conversions might fail, in which case bail */
265 if (!el
->values
[i
].data
) {
275 /* Merge a remote message element into a local message. */
276 static int ldb_msg_el_merge(struct ldb_module
*module
, struct ldb_message
*local
,
277 struct ldb_message
*remote
, const char *attr_name
)
279 const struct ldb_map_context
*data
= map_get_context(module
);
280 const struct ldb_map_attribute
*map
;
281 struct ldb_message_element
*old
, *el
=NULL
;
282 const char *remote_name
= NULL
;
283 struct ldb_context
*ldb
;
285 ldb
= ldb_module_get_ctx(module
);
287 /* We handle wildcards in ldb_msg_el_merge_wildcard */
288 if (ldb_attr_cmp(attr_name
, "*") == 0) {
292 map
= map_attr_find_local(data
, attr_name
);
294 /* Unknown attribute in remote message:
295 * skip, attribute was probably auto-generated */
303 case LDB_MAP_CONVERT
:
304 remote_name
= map
->u
.convert
.remote_name
;
307 remote_name
= attr_name
;
310 case LDB_MAP_RENDROP
:
311 remote_name
= map
->u
.rename
.remote_name
;
313 case LDB_MAP_GENERATE
:
321 case LDB_MAP_CONVERT
:
322 if (map
->u
.convert
.convert_remote
== NULL
) {
323 ldb_debug(ldb
, LDB_DEBUG_ERROR
, "ldb_map: "
324 "Skipping attribute '%s': "
325 "'convert_remote' not set",
332 case LDB_MAP_RENDROP
:
333 old
= ldb_msg_find_element(remote
, remote_name
);
335 el
= ldb_msg_el_map_remote(module
, local
, map
, attr_name
, old
);
337 return LDB_ERR_NO_SUCH_ATTRIBUTE
;
341 case LDB_MAP_GENERATE
:
342 if (map
->u
.generate
.generate_local
== NULL
) {
343 ldb_debug(ldb
, LDB_DEBUG_ERROR
, "ldb_map: "
344 "Skipping attribute '%s': "
345 "'generate_local' not set",
350 el
= map
->u
.generate
.generate_local(module
, local
, attr_name
, remote
);
352 /* Generation failure is probably due to lack of source attributes */
353 return LDB_ERR_NO_SUCH_ATTRIBUTE
;
359 return LDB_ERR_NO_SUCH_ATTRIBUTE
;
362 return ldb_msg_replace(local
, el
);
365 /* Handle wildcard parts of merging a remote message element into a local message. */
366 static int ldb_msg_el_merge_wildcard(struct ldb_module
*module
, struct ldb_message
*local
,
367 struct ldb_message
*remote
)
369 const struct ldb_map_context
*data
= map_get_context(module
);
370 const struct ldb_map_attribute
*map
= map_attr_find_local(data
, "*");
371 struct ldb_message_element
*el
=NULL
;
375 /* Perhaps we have a mapping for "*" */
376 if (map
&& map
->type
== LDB_MAP_KEEP
) {
377 /* We copy everything over, and hope that anything with a
378 more specific rule is overwritten */
379 for (i
= 0; i
< remote
->num_elements
; i
++) {
380 el
= ldb_msg_el_map_remote(module
, local
, map
, remote
->elements
[i
].name
,
381 &remote
->elements
[i
]);
383 return LDB_ERR_OPERATIONS_ERROR
;
386 ret
= ldb_msg_replace(local
, el
);
393 /* Now walk the list of possible mappings, and apply each */
394 for (i
= 0; data
->attribute_maps
[i
].local_name
; i
++) {
395 ret
= ldb_msg_el_merge(module
, local
, remote
,
396 data
->attribute_maps
[i
].local_name
);
397 if (ret
== LDB_ERR_NO_SUCH_ATTRIBUTE
) {
410 * ================ */
412 /* Merge two local messages into a single one. */
413 static int ldb_msg_merge_local(struct ldb_module
*module
, struct ldb_message
*msg1
, struct ldb_message
*msg2
)
418 for (i
= 0; i
< msg2
->num_elements
; i
++) {
419 ret
= ldb_msg_replace(msg1
, &msg2
->elements
[i
]);
428 /* Merge a local and a remote message into a single local one. */
429 static int ldb_msg_merge_remote(struct map_context
*ac
, struct ldb_message
*local
,
430 struct ldb_message
*remote
)
434 const char * const *attrs
= ac
->all_attrs
;
436 ret
= ldb_msg_el_merge_wildcard(ac
->module
, local
, remote
);
442 for (i
= 0; attrs
&& attrs
[i
]; i
++) {
443 if (ldb_attr_cmp(attrs
[i
], "*") == 0) {
444 ret
= ldb_msg_el_merge_wildcard(ac
->module
, local
, remote
);
452 /* Try to map each attribute back;
453 * Add to local message is possible,
454 * Overwrite old local attribute if necessary */
455 for (i
= 0; attrs
&& attrs
[i
]; i
++) {
456 ret
= ldb_msg_el_merge(ac
->module
, local
, remote
,
458 if (ret
== LDB_ERR_NO_SUCH_ATTRIBUTE
) {
467 /* Mapping search results
468 * ====================== */
470 /* Map a search result back into the local partition. */
471 static int map_reply_remote(struct map_context
*ac
, struct ldb_reply
*ares
)
473 struct ldb_message
*msg
;
477 /* There is no result message, skip */
478 if (ares
->type
!= LDB_REPLY_ENTRY
) {
482 /* Create a new result message */
483 msg
= ldb_msg_new(ares
);
486 return LDB_ERR_OPERATIONS_ERROR
;
489 /* Merge remote message into new message */
490 ret
= ldb_msg_merge_remote(ac
, msg
, ares
->message
);
496 /* Create corresponding local DN */
497 dn
= ldb_dn_map_rebase_remote(ac
->module
, msg
, ares
->message
->dn
);
500 return LDB_ERR_OPERATIONS_ERROR
;
504 /* Store new message with new DN as the result */
505 talloc_free(ares
->message
);
511 /* Mapping parse trees
512 * =================== */
514 /* Check whether a parse tree can safely be split in two. */
515 static bool ldb_parse_tree_check_splittable(const struct ldb_parse_tree
*tree
)
517 const struct ldb_parse_tree
*subtree
= tree
;
521 switch (subtree
->operation
) {
524 subtree
= subtree
->u
.isnot
.child
;
528 return !negate
; /* if negate: False */
531 return negate
; /* if negate: True */
534 return true; /* simple parse tree */
538 return true; /* no parse tree */
541 /* Collect a list of attributes required to match a given parse tree. */
542 static int ldb_parse_tree_collect_attrs(struct ldb_module
*module
, void *mem_ctx
, const char ***attrs
, const struct ldb_parse_tree
*tree
)
544 const char **new_attrs
;
552 switch (tree
->operation
) {
554 case LDB_OP_AND
: /* attributes stored in list of subtrees */
555 for (i
= 0; i
< tree
->u
.list
.num_elements
; i
++) {
556 ret
= ldb_parse_tree_collect_attrs(module
, mem_ctx
,
557 attrs
, tree
->u
.list
.elements
[i
]);
564 case LDB_OP_NOT
: /* attributes stored in single subtree */
565 return ldb_parse_tree_collect_attrs(module
, mem_ctx
, attrs
, tree
->u
.isnot
.child
);
567 default: /* single attribute in tree */
568 new_attrs
= ldb_attr_list_copy_add(mem_ctx
, *attrs
, tree
->u
.equality
.attr
);
575 static int map_subtree_select_local(struct ldb_module
*module
, void *mem_ctx
, struct ldb_parse_tree
**new, const struct ldb_parse_tree
*tree
);
577 /* Select a negated subtree that queries attributes in the local partition */
578 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
)
580 struct ldb_parse_tree
*child
;
583 /* Prepare new tree */
584 *new = talloc_memdup(mem_ctx
, tree
, sizeof(struct ldb_parse_tree
));
587 return LDB_ERR_OPERATIONS_ERROR
;
590 /* Generate new subtree */
591 ret
= map_subtree_select_local(module
, *new, &child
, tree
->u
.isnot
.child
);
597 /* Prune tree without subtree */
604 (*new)->u
.isnot
.child
= child
;
609 /* Select a list of subtrees that query attributes in the local partition */
610 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
)
615 /* Prepare new tree */
616 *new = talloc_memdup(mem_ctx
, tree
, sizeof(struct ldb_parse_tree
));
619 return LDB_ERR_OPERATIONS_ERROR
;
622 /* Prepare list of subtrees */
623 (*new)->u
.list
.num_elements
= 0;
624 (*new)->u
.list
.elements
= talloc_array(*new, struct ldb_parse_tree
*, tree
->u
.list
.num_elements
);
625 if ((*new)->u
.list
.elements
== NULL
) {
628 return LDB_ERR_OPERATIONS_ERROR
;
631 /* Generate new list of subtrees */
633 for (i
= 0; i
< tree
->u
.list
.num_elements
; i
++) {
634 struct ldb_parse_tree
*child
;
635 ret
= map_subtree_select_local(module
, *new, &child
, tree
->u
.list
.elements
[i
]);
642 (*new)->u
.list
.elements
[j
] = child
;
647 /* Prune tree without subtrees */
654 /* Fix subtree list size */
655 (*new)->u
.list
.num_elements
= j
;
656 (*new)->u
.list
.elements
= talloc_realloc(*new, (*new)->u
.list
.elements
, struct ldb_parse_tree
*, (*new)->u
.list
.num_elements
);
661 /* Select a simple subtree that queries attributes in the local partition */
662 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
)
664 /* Prepare new tree */
665 *new = talloc_memdup(mem_ctx
, tree
, sizeof(struct ldb_parse_tree
));
668 return LDB_ERR_OPERATIONS_ERROR
;
674 /* Select subtrees that query attributes in the local partition */
675 static int map_subtree_select_local(struct ldb_module
*module
, void *mem_ctx
, struct ldb_parse_tree
**new, const struct ldb_parse_tree
*tree
)
677 const struct ldb_map_context
*data
= map_get_context(module
);
683 if (tree
->operation
== LDB_OP_NOT
) {
684 return map_subtree_select_local_not(module
, mem_ctx
, new, tree
);
687 if (tree
->operation
== LDB_OP_AND
|| tree
->operation
== LDB_OP_OR
) {
688 return map_subtree_select_local_list(module
, mem_ctx
, new, tree
);
691 if (map_attr_check_remote(data
, tree
->u
.equality
.attr
)) {
696 return map_subtree_select_local_simple(module
, mem_ctx
, new, tree
);
699 static int map_subtree_collect_remote(struct ldb_module
*module
, void *mem_ctx
, struct ldb_parse_tree
**new, const struct ldb_parse_tree
*tree
);
701 /* Collect a negated subtree that queries attributes in the remote partition */
702 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
)
704 struct ldb_parse_tree
*child
;
707 /* Prepare new tree */
708 *new = talloc_memdup(mem_ctx
, tree
, sizeof(struct ldb_parse_tree
));
711 return LDB_ERR_OPERATIONS_ERROR
;
714 /* Generate new subtree */
715 ret
= map_subtree_collect_remote(module
, *new, &child
, tree
->u
.isnot
.child
);
721 /* Prune tree without subtree */
728 (*new)->u
.isnot
.child
= child
;
733 /* Collect a list of subtrees that query attributes in the remote partition */
734 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
)
739 /* Prepare new tree */
740 *new = talloc_memdup(mem_ctx
, tree
, sizeof(struct ldb_parse_tree
));
743 return LDB_ERR_OPERATIONS_ERROR
;
746 /* Prepare list of subtrees */
747 (*new)->u
.list
.num_elements
= 0;
748 (*new)->u
.list
.elements
= talloc_array(*new, struct ldb_parse_tree
*, tree
->u
.list
.num_elements
);
749 if ((*new)->u
.list
.elements
== NULL
) {
752 return LDB_ERR_OPERATIONS_ERROR
;
755 /* Generate new list of subtrees */
757 for (i
= 0; i
< tree
->u
.list
.num_elements
; i
++) {
758 struct ldb_parse_tree
*child
;
759 ret
= map_subtree_collect_remote(module
, *new, &child
, tree
->u
.list
.elements
[i
]);
766 (*new)->u
.list
.elements
[j
] = child
;
771 /* Prune tree without subtrees */
778 /* Fix subtree list size */
779 (*new)->u
.list
.num_elements
= j
;
780 (*new)->u
.list
.elements
= talloc_realloc(*new, (*new)->u
.list
.elements
, struct ldb_parse_tree
*, (*new)->u
.list
.num_elements
);
785 /* Collect a simple subtree that queries attributes in the remote partition */
786 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
)
790 /* Prepare new tree */
791 *new = talloc(mem_ctx
, struct ldb_parse_tree
);
794 return LDB_ERR_OPERATIONS_ERROR
;
798 if (map
->type
== LDB_MAP_KEEP
) {
799 /* Nothing to do here */
803 /* Store attribute and value in new tree */
804 switch (tree
->operation
) {
806 attr
= map_attr_map_local(*new, map
, tree
->u
.present
.attr
);
807 (*new)->u
.present
.attr
= attr
;
809 case LDB_OP_SUBSTRING
:
811 attr
= map_attr_map_local(*new, map
, tree
->u
.substring
.attr
);
812 (*new)->u
.substring
.attr
= attr
;
815 case LDB_OP_EQUALITY
:
816 attr
= map_attr_map_local(*new, map
, tree
->u
.equality
.attr
);
817 (*new)->u
.equality
.attr
= attr
;
822 attr
= map_attr_map_local(*new, map
, tree
->u
.comparison
.attr
);
823 (*new)->u
.comparison
.attr
= attr
;
825 case LDB_OP_EXTENDED
:
826 attr
= map_attr_map_local(*new, map
, tree
->u
.extended
.attr
);
827 (*new)->u
.extended
.attr
= attr
;
829 default: /* unknown kind of simple subtree */
831 return LDB_ERR_OPERATIONS_ERROR
;
840 if (map
->type
== LDB_MAP_RENAME
|| map
->type
== LDB_MAP_RENDROP
) {
841 /* Nothing more to do here, the attribute has been renamed */
845 /* Store attribute and value in new tree */
846 switch (tree
->operation
) {
849 case LDB_OP_SUBSTRING
:
853 (*new)->u
.substring
.chunks
= NULL
;
854 for (i
=0; tree
->u
.substring
.chunks
&& tree
->u
.substring
.chunks
[i
]; i
++) {
855 (*new)->u
.substring
.chunks
= talloc_realloc(*new, (*new)->u
.substring
.chunks
, struct ldb_val
*, i
+2);
856 if (!(*new)->u
.substring
.chunks
) {
861 (*new)->u
.substring
.chunks
[i
] = talloc(*new, struct ldb_val
);
862 if (!(*new)->u
.substring
.chunks
[i
]) {
867 *(*new)->u
.substring
.chunks
[i
] = ldb_val_map_local(module
, *new, map
, tree
->u
.substring
.chunks
[i
]);
868 (*new)->u
.substring
.chunks
[i
+1] = NULL
;
872 case LDB_OP_EQUALITY
:
873 (*new)->u
.equality
.value
= ldb_val_map_local(module
, *new, map
, &tree
->u
.equality
.value
);
878 (*new)->u
.comparison
.value
= ldb_val_map_local(module
, *new, map
, &tree
->u
.comparison
.value
);
880 case LDB_OP_EXTENDED
:
881 (*new)->u
.extended
.value
= ldb_val_map_local(module
, *new, map
, &tree
->u
.extended
.value
);
882 (*new)->u
.extended
.rule_id
= talloc_strdup(*new, tree
->u
.extended
.rule_id
);
884 default: /* unknown kind of simple subtree */
886 return LDB_ERR_OPERATIONS_ERROR
;
892 /* Collect subtrees that query attributes in the remote partition */
893 static int map_subtree_collect_remote(struct ldb_module
*module
, void *mem_ctx
, struct ldb_parse_tree
**new, const struct ldb_parse_tree
*tree
)
895 const struct ldb_map_context
*data
= map_get_context(module
);
896 const struct ldb_map_attribute
*map
;
897 struct ldb_context
*ldb
;
899 ldb
= ldb_module_get_ctx(module
);
905 if (tree
->operation
== LDB_OP_NOT
) {
906 return map_subtree_collect_remote_not(module
, mem_ctx
, new, tree
);
909 if ((tree
->operation
== LDB_OP_AND
) || (tree
->operation
== LDB_OP_OR
)) {
910 return map_subtree_collect_remote_list(module
, mem_ctx
, new, tree
);
913 if (!map_attr_check_remote(data
, tree
->u
.equality
.attr
)) {
918 map
= map_attr_find_local(data
, tree
->u
.equality
.attr
);
919 if (map
->convert_operator
) {
920 return map
->convert_operator(module
, mem_ctx
, new, tree
);
923 if (map
->type
== LDB_MAP_GENERATE
) {
924 ldb_debug(ldb
, LDB_DEBUG_WARNING
, "ldb_map: "
925 "Skipping attribute '%s': "
926 "'convert_operator' not set",
927 tree
->u
.equality
.attr
);
932 return map_subtree_collect_remote_simple(module
, mem_ctx
, new, tree
, map
);
935 /* Split subtrees that query attributes in the local partition from
936 * those that query the remote partition. */
937 static int ldb_parse_tree_partition(struct ldb_module
*module
,
939 struct ldb_parse_tree
**local_tree
,
940 struct ldb_parse_tree
**remote_tree
,
941 const struct ldb_parse_tree
*tree
)
948 /* No original tree */
953 /* Generate local tree */
954 ret
= map_subtree_select_local(module
, mem_ctx
, local_tree
, tree
);
959 /* Generate remote tree */
960 ret
= map_subtree_collect_remote(module
, mem_ctx
, remote_tree
, tree
);
962 talloc_free(*local_tree
);
969 /* Collect a list of attributes required either explicitly from a
970 * given list or implicitly from a given parse tree; split the
971 * collected list into local and remote parts. */
972 static int map_attrs_collect_and_partition(struct ldb_module
*module
, struct map_context
*ac
,
973 const char * const *search_attrs
,
974 const struct ldb_parse_tree
*tree
)
977 const char **tree_attrs
;
978 const char **remote_attrs
;
979 const char **local_attrs
;
982 /* There is no tree, just partition the searched attributes */
984 ret
= map_attrs_partition(module
, ac
,
985 &local_attrs
, &remote_attrs
, search_attrs
);
987 ac
->local_attrs
= local_attrs
;
988 ac
->remote_attrs
= remote_attrs
;
989 ac
->all_attrs
= search_attrs
;
994 /* Create context for temporary memory */
995 tmp_ctx
= talloc_new(ac
);
996 if (tmp_ctx
== NULL
) {
1000 /* Prepare list of attributes from tree */
1001 tree_attrs
= talloc_array(tmp_ctx
, const char *, 1);
1002 if (tree_attrs
== NULL
) {
1003 talloc_free(tmp_ctx
);
1006 tree_attrs
[0] = NULL
;
1008 /* Collect attributes from tree */
1009 ret
= ldb_parse_tree_collect_attrs(module
, tmp_ctx
, &tree_attrs
, tree
);
1014 /* Merge attributes from search operation */
1015 ret
= map_attrs_merge(module
, tmp_ctx
, &tree_attrs
, search_attrs
);
1020 /* Split local from remote attributes */
1021 ret
= map_attrs_partition(module
, ac
, &local_attrs
,
1022 &remote_attrs
, tree_attrs
);
1025 ac
->local_attrs
= local_attrs
;
1026 ac
->remote_attrs
= remote_attrs
;
1027 talloc_steal(ac
, tree_attrs
);
1028 ac
->all_attrs
= tree_attrs
;
1031 /* Free temporary memory */
1032 talloc_free(tmp_ctx
);
1037 return LDB_ERR_OPERATIONS_ERROR
;
1041 /* Outbound requests: search
1042 * ========================= */
1044 static int map_remote_search_callback(struct ldb_request
*req
,
1045 struct ldb_reply
*ares
);
1046 static int map_local_merge_callback(struct ldb_request
*req
,
1047 struct ldb_reply
*ares
);
1048 static int map_search_local(struct map_context
*ac
);
1050 static int map_save_entry(struct map_context
*ac
, struct ldb_reply
*ares
)
1052 struct map_reply
*mr
;
1054 mr
= talloc_zero(ac
, struct map_reply
);
1056 map_oom(ac
->module
);
1057 return LDB_ERR_OPERATIONS_ERROR
;
1059 mr
->remote
= talloc_steal(mr
, ares
);
1060 if (ac
->r_current
) {
1061 ac
->r_current
->next
= mr
;
1071 /* Pass a merged search result up the callback chain. */
1072 int map_return_entry(struct map_context
*ac
, struct ldb_reply
*ares
)
1074 struct ldb_message_element
*el
;
1075 const char * const *attrs
;
1076 struct ldb_context
*ldb
;
1081 ldb
= ldb_module_get_ctx(ac
->module
);
1083 /* Merged result doesn't match original query, skip */
1084 ret
= ldb_match_msg_error(ldb
, ares
->message
,
1085 ac
->req
->op
.search
.tree
,
1086 ac
->req
->op
.search
.base
,
1087 ac
->req
->op
.search
.scope
,
1089 if (ret
!= LDB_SUCCESS
) return ret
;
1091 ldb_debug(ldb
, LDB_DEBUG_TRACE
, "ldb_map: "
1092 "Skipping record '%s': "
1093 "doesn't match original search",
1094 ldb_dn_get_linearized(ares
->message
->dn
));
1098 /* Limit result to requested attrs */
1099 if (ac
->req
->op
.search
.attrs
&&
1100 (! ldb_attr_in_list(ac
->req
->op
.search
.attrs
, "*"))) {
1102 attrs
= ac
->req
->op
.search
.attrs
;
1105 while (i
< ares
->message
->num_elements
) {
1107 el
= &ares
->message
->elements
[i
];
1108 if ( ! ldb_attr_in_list(attrs
, el
->name
)) {
1109 ldb_msg_remove_element(ares
->message
, el
);
1116 return ldb_module_send_entry(ac
->req
, ares
->message
, ares
->controls
);
1119 /* Search a record. */
1120 int ldb_map_search(struct ldb_module
*module
, struct ldb_request
*req
)
1122 struct ldb_parse_tree
*remote_tree
;
1123 struct ldb_parse_tree
*local_tree
;
1124 struct ldb_request
*remote_req
;
1125 struct ldb_context
*ldb
;
1126 struct map_context
*ac
;
1129 const char *wildcard
[] = { "*", NULL
};
1130 const char * const *attrs
;
1132 ldb
= ldb_module_get_ctx(module
);
1134 /* if we're not yet initialized, go to the next module */
1135 if (!ldb_module_get_private(module
))
1136 return ldb_next_request(module
, req
);
1138 /* Do not manipulate our control entries */
1139 if (ldb_dn_is_special(req
->op
.search
.base
)) {
1140 return ldb_next_request(module
, req
);
1143 /* No mapping requested, skip to next module */
1144 if ((req
->op
.search
.base
) && (!ldb_dn_check_local(module
, req
->op
.search
.base
))) {
1145 return ldb_next_request(module
, req
);
1148 /* TODO: How can we be sure about which partition we are
1149 * targetting when there is no search base? */
1151 /* Prepare context and handle */
1152 ac
= map_init_context(module
, req
);
1154 return LDB_ERR_OPERATIONS_ERROR
;
1157 /* It is easier to deal with the two different ways of
1158 * expressing the wildcard in the same codepath */
1159 attrs
= req
->op
.search
.attrs
;
1160 if (attrs
== NULL
) {
1164 /* Split local from remote attrs */
1165 ret
= map_attrs_collect_and_partition(module
, ac
,
1166 attrs
, req
->op
.search
.tree
);
1168 return LDB_ERR_OPERATIONS_ERROR
;
1171 /* Split local from remote tree */
1172 ret
= ldb_parse_tree_partition(module
, ac
,
1173 &local_tree
, &remote_tree
,
1174 req
->op
.search
.tree
);
1176 return LDB_ERR_OPERATIONS_ERROR
;
1179 if (((local_tree
!= NULL
) && (remote_tree
!= NULL
)) &&
1180 (!ldb_parse_tree_check_splittable(req
->op
.search
.tree
))) {
1181 /* The query can't safely be split, enumerate the remote partition */
1186 if (local_tree
== NULL
) {
1187 /* Construct default local parse tree */
1188 local_tree
= talloc_zero(ac
, struct ldb_parse_tree
);
1189 if (local_tree
== NULL
) {
1190 map_oom(ac
->module
);
1191 return LDB_ERR_OPERATIONS_ERROR
;
1194 local_tree
->operation
= LDB_OP_PRESENT
;
1195 local_tree
->u
.present
.attr
= talloc_strdup(local_tree
, IS_MAPPED
);
1197 if (remote_tree
== NULL
) {
1198 /* Construct default remote parse tree */
1199 remote_tree
= ldb_parse_tree(ac
, NULL
);
1200 if (remote_tree
== NULL
) {
1201 return LDB_ERR_OPERATIONS_ERROR
;
1205 ac
->local_tree
= local_tree
;
1207 /* Prepare the remote operation */
1208 ret
= ldb_build_search_req_ex(&remote_req
, ldb
, ac
,
1209 req
->op
.search
.base
,
1210 req
->op
.search
.scope
,
1214 ac
, map_remote_search_callback
,
1216 LDB_REQ_SET_LOCATION(remote_req
);
1217 if (ret
!= LDB_SUCCESS
) {
1218 return LDB_ERR_OPERATIONS_ERROR
;
1221 return ldb_next_remote_request(module
, remote_req
);
1224 /* Now, search the local part of a remote search result. */
1225 static int map_remote_search_callback(struct ldb_request
*req
,
1226 struct ldb_reply
*ares
)
1228 struct map_context
*ac
;
1231 ac
= talloc_get_type(req
->context
, struct map_context
);
1234 return ldb_module_done(ac
->req
, NULL
, NULL
,
1235 LDB_ERR_OPERATIONS_ERROR
);
1237 if (ares
->error
!= LDB_SUCCESS
) {
1238 return ldb_module_done(ac
->req
, ares
->controls
,
1239 ares
->response
, ares
->error
);
1242 switch (ares
->type
) {
1243 case LDB_REPLY_REFERRAL
:
1245 /* ignore referrals */
1249 case LDB_REPLY_ENTRY
:
1251 /* Map result record into a local message */
1252 ret
= map_reply_remote(ac
, ares
);
1255 return ldb_module_done(ac
->req
, NULL
, NULL
,
1256 LDB_ERR_OPERATIONS_ERROR
);
1259 /* if we have no local db, then we can just return the reply to
1260 * the upper layer, otherwise we must save it and process it
1261 * when all replies ahve been gathered */
1262 if ( ! map_check_local_db(ac
->module
)) {
1263 ret
= map_return_entry(ac
, ares
);
1265 ret
= map_save_entry(ac
,ares
);
1268 if (ret
!= LDB_SUCCESS
) {
1270 return ldb_module_done(ac
->req
, NULL
, NULL
, ret
);
1274 case LDB_REPLY_DONE
:
1276 if ( ! map_check_local_db(ac
->module
)) {
1277 return ldb_module_done(ac
->req
, ares
->controls
,
1278 ares
->response
, LDB_SUCCESS
);
1281 /* reset the pointer to the start of the list */
1282 ac
->r_current
= ac
->r_list
;
1284 /* no entry just return */
1285 if (ac
->r_current
== NULL
) {
1286 ret
= ldb_module_done(ac
->req
, ares
->controls
,
1287 ares
->response
, LDB_SUCCESS
);
1292 ac
->remote_done_ares
= talloc_steal(ac
, ares
);
1294 ret
= map_search_local(ac
);
1295 if (ret
!= LDB_SUCCESS
) {
1296 return ldb_module_done(ac
->req
, NULL
, NULL
, ret
);
1303 static int map_search_local(struct map_context
*ac
)
1305 struct ldb_request
*search_req
;
1307 if (ac
->r_current
== NULL
|| ac
->r_current
->remote
== NULL
) {
1308 return LDB_ERR_OPERATIONS_ERROR
;
1311 /* Prepare local search request */
1312 /* TODO: use GUIDs here instead? */
1313 search_req
= map_search_base_req(ac
,
1314 ac
->r_current
->remote
->message
->dn
,
1316 ac
, map_local_merge_callback
);
1317 if (search_req
== NULL
) {
1318 return LDB_ERR_OPERATIONS_ERROR
;
1321 return ldb_next_request(ac
->module
, search_req
);
1324 /* Merge the remote and local parts of a search result. */
1325 int map_local_merge_callback(struct ldb_request
*req
, struct ldb_reply
*ares
)
1327 struct ldb_context
*ldb
;
1328 struct map_context
*ac
;
1331 ac
= talloc_get_type(req
->context
, struct map_context
);
1332 ldb
= ldb_module_get_ctx(ac
->module
);
1335 return ldb_module_done(ac
->req
, NULL
, NULL
,
1336 LDB_ERR_OPERATIONS_ERROR
);
1338 if (ares
->error
!= LDB_SUCCESS
) {
1339 return ldb_module_done(ac
->req
, ares
->controls
,
1340 ares
->response
, ares
->error
);
1343 switch (ares
->type
) {
1344 case LDB_REPLY_ENTRY
:
1345 /* We have already found a local record */
1346 if (ac
->r_current
->local
) {
1348 ldb_set_errstring(ldb
, "ldb_map: Too many results!");
1349 return ldb_module_done(ac
->req
, NULL
, NULL
,
1350 LDB_ERR_OPERATIONS_ERROR
);
1353 /* Store local result */
1354 ac
->r_current
->local
= talloc_steal(ac
->r_current
, ares
);
1358 case LDB_REPLY_REFERRAL
:
1359 /* ignore referrals */
1363 case LDB_REPLY_DONE
:
1364 /* We don't need the local 'ares', but we will use the remote one from below */
1367 /* No local record found, map and send remote record */
1368 if (ac
->r_current
->local
!= NULL
) {
1369 /* Merge remote into local message */
1370 ret
= ldb_msg_merge_local(ac
->module
,
1371 ac
->r_current
->local
->message
,
1372 ac
->r_current
->remote
->message
);
1373 if (ret
== LDB_SUCCESS
) {
1374 ret
= map_return_entry(ac
, ac
->r_current
->local
);
1376 if (ret
!= LDB_SUCCESS
) {
1377 return ldb_module_done(ac
->req
, NULL
, NULL
,
1378 LDB_ERR_OPERATIONS_ERROR
);
1381 ret
= map_return_entry(ac
, ac
->r_current
->remote
);
1382 if (ret
!= LDB_SUCCESS
) {
1383 return ldb_module_done(ac
->req
,
1388 if (ac
->r_current
->next
!= NULL
) {
1389 ac
->r_current
= ac
->r_current
->next
;
1390 if (ac
->r_current
->remote
->type
== LDB_REPLY_ENTRY
) {
1391 ret
= map_search_local(ac
);
1392 if (ret
!= LDB_SUCCESS
) {
1393 return ldb_module_done(ac
->req
,
1400 /* ok we are done with all search, finally it is time to
1401 * finish operations for this module */
1402 return ldb_module_done(ac
->req
,
1403 ac
->remote_done_ares
->controls
,
1404 ac
->remote_done_ares
->response
,
1405 ac
->remote_done_ares
->error
);