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
;
203 talloc_free(old
->values
);
206 old
->values
= talloc_array(msg
->elements
, struct ldb_val
, el
->num_values
);
207 old
->num_values
= el
->num_values
;
208 if (old
->values
== NULL
) {
209 return LDB_ERR_OPERATIONS_ERROR
;
211 /* copy the values into the element */
212 for (j
=0;j
<el
->num_values
;j
++) {
213 old
->values
[j
] = ldb_val_dup(old
->values
, &el
->values
[j
]);
214 if (old
->values
[j
].data
== NULL
&& el
->values
[j
].length
!= 0) {
215 return LDB_ERR_OPERATIONS_ERROR
;
222 /* Map a message element back into the local partition. */
223 static struct ldb_message_element
*ldb_msg_el_map_remote(struct ldb_module
*module
,
225 const struct ldb_map_attribute
*map
,
226 const char *attr_name
,
227 const struct ldb_message_element
*old
)
229 const struct ldb_map_context
*data
= map_get_context(module
);
230 const char *local_attr_name
= attr_name
;
231 struct ldb_message_element
*el
;
234 el
= talloc_zero(mem_ctx
, struct ldb_message_element
);
240 el
->values
= talloc_array(el
, struct ldb_val
, old
->num_values
);
241 if (el
->values
== NULL
) {
247 for (i
= 0; data
->attribute_maps
[i
].local_name
; i
++) {
248 struct ldb_map_attribute
*am
= &data
->attribute_maps
[i
];
249 if (((am
->type
== LDB_MAP_RENAME
|| am
->type
== LDB_MAP_RENDROP
) &&
250 !strcmp(am
->u
.rename
.remote_name
, attr_name
))
251 || (am
->type
== LDB_MAP_CONVERT
&&
252 !strcmp(am
->u
.convert
.remote_name
, attr_name
))) {
254 local_attr_name
= am
->local_name
;
259 el
->name
= talloc_strdup(el
, local_attr_name
);
260 if (el
->name
== NULL
) {
266 for (i
= 0; i
< old
->num_values
; i
++) {
267 el
->values
[i
] = ldb_val_map_remote(module
, el
->values
, map
, &old
->values
[i
]);
268 /* Conversions might fail, in which case bail */
269 if (!el
->values
[i
].data
) {
279 /* Merge a remote message element into a local message. */
280 static int ldb_msg_el_merge(struct ldb_module
*module
, struct ldb_message
*local
,
281 struct ldb_message
*remote
, const char *attr_name
)
283 const struct ldb_map_context
*data
= map_get_context(module
);
284 const struct ldb_map_attribute
*map
;
285 struct ldb_message_element
*old
, *el
=NULL
;
286 const char *remote_name
= NULL
;
287 struct ldb_context
*ldb
;
289 ldb
= ldb_module_get_ctx(module
);
291 /* We handle wildcards in ldb_msg_el_merge_wildcard */
292 if (ldb_attr_cmp(attr_name
, "*") == 0) {
296 map
= map_attr_find_local(data
, attr_name
);
298 /* Unknown attribute in remote message:
299 * skip, attribute was probably auto-generated */
307 case LDB_MAP_CONVERT
:
308 remote_name
= map
->u
.convert
.remote_name
;
311 remote_name
= attr_name
;
314 case LDB_MAP_RENDROP
:
315 remote_name
= map
->u
.rename
.remote_name
;
317 case LDB_MAP_GENERATE
:
325 case LDB_MAP_CONVERT
:
326 if (map
->u
.convert
.convert_remote
== NULL
) {
327 ldb_debug(ldb
, LDB_DEBUG_ERROR
, "ldb_map: "
328 "Skipping attribute '%s': "
329 "'convert_remote' not set",
337 case LDB_MAP_RENDROP
:
338 old
= ldb_msg_find_element(remote
, remote_name
);
340 el
= ldb_msg_el_map_remote(module
, local
, map
, attr_name
, old
);
342 return LDB_ERR_NO_SUCH_ATTRIBUTE
;
346 case LDB_MAP_GENERATE
:
347 if (map
->u
.generate
.generate_local
== NULL
) {
348 ldb_debug(ldb
, LDB_DEBUG_ERROR
, "ldb_map: "
349 "Skipping attribute '%s': "
350 "'generate_local' not set",
355 el
= map
->u
.generate
.generate_local(module
, local
, attr_name
, remote
);
357 /* Generation failure is probably due to lack of source attributes */
358 return LDB_ERR_NO_SUCH_ATTRIBUTE
;
364 return LDB_ERR_NO_SUCH_ATTRIBUTE
;
367 return ldb_msg_replace(local
, el
);
370 /* Handle wildcard parts of merging a remote message element into a local message. */
371 static int ldb_msg_el_merge_wildcard(struct ldb_module
*module
, struct ldb_message
*local
,
372 struct ldb_message
*remote
)
374 const struct ldb_map_context
*data
= map_get_context(module
);
375 const struct ldb_map_attribute
*map
= map_attr_find_local(data
, "*");
376 struct ldb_message_element
*el
=NULL
;
380 /* Perhaps we have a mapping for "*" */
381 if (map
&& map
->type
== LDB_MAP_KEEP
) {
382 /* We copy everything over, and hope that anything with a
383 more specific rule is overwritten */
384 for (i
= 0; i
< remote
->num_elements
; i
++) {
385 el
= ldb_msg_el_map_remote(module
, local
, map
, remote
->elements
[i
].name
,
386 &remote
->elements
[i
]);
388 return LDB_ERR_OPERATIONS_ERROR
;
391 ret
= ldb_msg_replace(local
, el
);
398 /* Now walk the list of possible mappings, and apply each */
399 for (i
= 0; data
->attribute_maps
[i
].local_name
; i
++) {
400 ret
= ldb_msg_el_merge(module
, local
, remote
,
401 data
->attribute_maps
[i
].local_name
);
402 if (ret
== LDB_ERR_NO_SUCH_ATTRIBUTE
) {
415 * ================ */
417 /* Merge two local messages into a single one. */
418 static int ldb_msg_merge_local(struct ldb_module
*module
, struct ldb_message
*msg1
, struct ldb_message
*msg2
)
423 for (i
= 0; i
< msg2
->num_elements
; i
++) {
424 ret
= ldb_msg_replace(msg1
, &msg2
->elements
[i
]);
433 /* Merge a local and a remote message into a single local one. */
434 static int ldb_msg_merge_remote(struct map_context
*ac
, struct ldb_message
*local
,
435 struct ldb_message
*remote
)
439 const char * const *attrs
= ac
->all_attrs
;
441 ret
= ldb_msg_el_merge_wildcard(ac
->module
, local
, remote
);
447 for (i
= 0; attrs
&& attrs
[i
]; i
++) {
448 if (ldb_attr_cmp(attrs
[i
], "*") == 0) {
449 ret
= ldb_msg_el_merge_wildcard(ac
->module
, local
, remote
);
457 /* Try to map each attribute back;
458 * Add to local message is possible,
459 * Overwrite old local attribute if necessary */
460 for (i
= 0; attrs
&& attrs
[i
]; i
++) {
461 ret
= ldb_msg_el_merge(ac
->module
, local
, remote
,
463 if (ret
== LDB_ERR_NO_SUCH_ATTRIBUTE
) {
472 /* Mapping search results
473 * ====================== */
475 /* Map a search result back into the local partition. */
476 static int map_reply_remote(struct map_context
*ac
, struct ldb_reply
*ares
)
478 struct ldb_message
*msg
;
482 /* There is no result message, skip */
483 if (ares
->type
!= LDB_REPLY_ENTRY
) {
487 /* Create a new result message */
488 msg
= ldb_msg_new(ares
);
491 return LDB_ERR_OPERATIONS_ERROR
;
494 /* Merge remote message into new message */
495 ret
= ldb_msg_merge_remote(ac
, msg
, ares
->message
);
501 /* Create corresponding local DN */
502 dn
= ldb_dn_map_rebase_remote(ac
->module
, msg
, ares
->message
->dn
);
505 return LDB_ERR_OPERATIONS_ERROR
;
509 /* Store new message with new DN as the result */
510 talloc_free(ares
->message
);
516 /* Mapping parse trees
517 * =================== */
519 /* Check whether a parse tree can safely be split in two. */
520 static bool ldb_parse_tree_check_splittable(const struct ldb_parse_tree
*tree
)
522 const struct ldb_parse_tree
*subtree
= tree
;
526 switch (subtree
->operation
) {
529 subtree
= subtree
->u
.isnot
.child
;
533 return !negate
; /* if negate: False */
536 return negate
; /* if negate: True */
539 return true; /* simple parse tree */
543 return true; /* no parse tree */
546 /* Collect a list of attributes required to match a given parse tree. */
547 static int ldb_parse_tree_collect_attrs(struct ldb_module
*module
, void *mem_ctx
, const char ***attrs
, const struct ldb_parse_tree
*tree
)
549 const char **new_attrs
;
557 switch (tree
->operation
) {
559 case LDB_OP_AND
: /* attributes stored in list of subtrees */
560 for (i
= 0; i
< tree
->u
.list
.num_elements
; i
++) {
561 ret
= ldb_parse_tree_collect_attrs(module
, mem_ctx
,
562 attrs
, tree
->u
.list
.elements
[i
]);
569 case LDB_OP_NOT
: /* attributes stored in single subtree */
570 return ldb_parse_tree_collect_attrs(module
, mem_ctx
, attrs
, tree
->u
.isnot
.child
);
572 default: /* single attribute in tree */
573 new_attrs
= ldb_attr_list_copy_add(mem_ctx
, *attrs
, tree
->u
.equality
.attr
);
580 static int map_subtree_select_local(struct ldb_module
*module
, void *mem_ctx
, struct ldb_parse_tree
**new, const struct ldb_parse_tree
*tree
);
582 /* Select a negated subtree that queries attributes in the local partition */
583 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
)
585 struct ldb_parse_tree
*child
;
588 /* Prepare new tree */
589 *new = talloc_memdup(mem_ctx
, tree
, sizeof(struct ldb_parse_tree
));
592 return LDB_ERR_OPERATIONS_ERROR
;
595 /* Generate new subtree */
596 ret
= map_subtree_select_local(module
, *new, &child
, tree
->u
.isnot
.child
);
602 /* Prune tree without subtree */
609 (*new)->u
.isnot
.child
= child
;
614 /* Select a list of subtrees that query attributes in the local partition */
615 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
)
620 /* Prepare new tree */
621 *new = talloc_memdup(mem_ctx
, tree
, sizeof(struct ldb_parse_tree
));
624 return LDB_ERR_OPERATIONS_ERROR
;
627 /* Prepare list of subtrees */
628 (*new)->u
.list
.num_elements
= 0;
629 (*new)->u
.list
.elements
= talloc_array(*new, struct ldb_parse_tree
*, tree
->u
.list
.num_elements
);
630 if ((*new)->u
.list
.elements
== NULL
) {
633 return LDB_ERR_OPERATIONS_ERROR
;
636 /* Generate new list of subtrees */
638 for (i
= 0; i
< tree
->u
.list
.num_elements
; i
++) {
639 struct ldb_parse_tree
*child
= NULL
;
640 ret
= map_subtree_select_local(module
, *new, &child
, tree
->u
.list
.elements
[i
]);
647 (*new)->u
.list
.elements
[j
] = child
;
652 /* Prune tree without subtrees */
659 /* Fix subtree list size */
660 (*new)->u
.list
.num_elements
= j
;
661 (*new)->u
.list
.elements
= talloc_realloc(*new, (*new)->u
.list
.elements
, struct ldb_parse_tree
*, (*new)->u
.list
.num_elements
);
666 /* Select a simple subtree that queries attributes in the local partition */
667 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
)
669 /* Prepare new tree */
670 *new = talloc_memdup(mem_ctx
, tree
, sizeof(struct ldb_parse_tree
));
673 return LDB_ERR_OPERATIONS_ERROR
;
679 /* Select subtrees that query attributes in the local partition */
680 static int map_subtree_select_local(struct ldb_module
*module
, void *mem_ctx
, struct ldb_parse_tree
**new, const struct ldb_parse_tree
*tree
)
682 const struct ldb_map_context
*data
= map_get_context(module
);
688 if (tree
->operation
== LDB_OP_NOT
) {
689 return map_subtree_select_local_not(module
, mem_ctx
, new, tree
);
692 if (tree
->operation
== LDB_OP_AND
|| tree
->operation
== LDB_OP_OR
) {
693 return map_subtree_select_local_list(module
, mem_ctx
, new, tree
);
696 if (map_attr_check_remote(data
, tree
->u
.equality
.attr
)) {
701 return map_subtree_select_local_simple(module
, mem_ctx
, new, tree
);
704 static int map_subtree_collect_remote(struct ldb_module
*module
, void *mem_ctx
, struct ldb_parse_tree
**new, const struct ldb_parse_tree
*tree
);
706 /* Collect a negated subtree that queries attributes in the remote partition */
707 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
)
709 struct ldb_parse_tree
*child
;
712 /* Prepare new tree */
713 *new = talloc_memdup(mem_ctx
, tree
, sizeof(struct ldb_parse_tree
));
716 return LDB_ERR_OPERATIONS_ERROR
;
719 /* Generate new subtree */
720 ret
= map_subtree_collect_remote(module
, *new, &child
, tree
->u
.isnot
.child
);
726 /* Prune tree without subtree */
733 (*new)->u
.isnot
.child
= child
;
738 /* Collect a list of subtrees that query attributes in the remote partition */
739 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
)
744 /* Prepare new tree */
745 *new = talloc_memdup(mem_ctx
, tree
, sizeof(struct ldb_parse_tree
));
748 return LDB_ERR_OPERATIONS_ERROR
;
751 /* Prepare list of subtrees */
752 (*new)->u
.list
.num_elements
= 0;
753 (*new)->u
.list
.elements
= talloc_array(*new, struct ldb_parse_tree
*, tree
->u
.list
.num_elements
);
754 if ((*new)->u
.list
.elements
== NULL
) {
757 return LDB_ERR_OPERATIONS_ERROR
;
760 /* Generate new list of subtrees */
762 for (i
= 0; i
< tree
->u
.list
.num_elements
; i
++) {
763 struct ldb_parse_tree
*child
;
764 ret
= map_subtree_collect_remote(module
, *new, &child
, tree
->u
.list
.elements
[i
]);
771 (*new)->u
.list
.elements
[j
] = child
;
776 /* Prune tree without subtrees */
783 /* Fix subtree list size */
784 (*new)->u
.list
.num_elements
= j
;
785 (*new)->u
.list
.elements
= talloc_realloc(*new, (*new)->u
.list
.elements
, struct ldb_parse_tree
*, (*new)->u
.list
.num_elements
);
790 /* Collect a simple subtree that queries attributes in the remote partition */
791 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
)
795 /* Prepare new tree */
796 *new = talloc(mem_ctx
, struct ldb_parse_tree
);
799 return LDB_ERR_OPERATIONS_ERROR
;
803 if (map
->type
== LDB_MAP_KEEP
) {
804 /* Nothing to do here */
808 /* Store attribute and value in new tree */
809 switch (tree
->operation
) {
811 attr
= map_attr_map_local(*new, map
, tree
->u
.present
.attr
);
812 (*new)->u
.present
.attr
= attr
;
814 case LDB_OP_SUBSTRING
:
816 attr
= map_attr_map_local(*new, map
, tree
->u
.substring
.attr
);
817 (*new)->u
.substring
.attr
= attr
;
820 case LDB_OP_EQUALITY
:
821 attr
= map_attr_map_local(*new, map
, tree
->u
.equality
.attr
);
822 (*new)->u
.equality
.attr
= attr
;
827 attr
= map_attr_map_local(*new, map
, tree
->u
.comparison
.attr
);
828 (*new)->u
.comparison
.attr
= attr
;
830 case LDB_OP_EXTENDED
:
831 attr
= map_attr_map_local(*new, map
, tree
->u
.extended
.attr
);
832 (*new)->u
.extended
.attr
= attr
;
834 default: /* unknown kind of simple subtree */
836 return LDB_ERR_OPERATIONS_ERROR
;
845 if (map
->type
== LDB_MAP_RENAME
|| map
->type
== LDB_MAP_RENDROP
) {
846 /* Nothing more to do here, the attribute has been renamed */
850 /* Store attribute and value in new tree */
851 switch (tree
->operation
) {
854 case LDB_OP_SUBSTRING
:
858 (*new)->u
.substring
.chunks
= NULL
;
859 for (i
=0; tree
->u
.substring
.chunks
&& tree
->u
.substring
.chunks
[i
]; i
++) {
860 (*new)->u
.substring
.chunks
= talloc_realloc(*new, (*new)->u
.substring
.chunks
, struct ldb_val
*, i
+2);
861 if (!(*new)->u
.substring
.chunks
) {
866 (*new)->u
.substring
.chunks
[i
] = talloc(*new, struct ldb_val
);
867 if (!(*new)->u
.substring
.chunks
[i
]) {
872 *(*new)->u
.substring
.chunks
[i
] = ldb_val_map_local(module
, *new, map
, tree
->u
.substring
.chunks
[i
]);
873 (*new)->u
.substring
.chunks
[i
+1] = NULL
;
877 case LDB_OP_EQUALITY
:
878 (*new)->u
.equality
.value
= ldb_val_map_local(module
, *new, map
, &tree
->u
.equality
.value
);
883 (*new)->u
.comparison
.value
= ldb_val_map_local(module
, *new, map
, &tree
->u
.comparison
.value
);
885 case LDB_OP_EXTENDED
:
886 (*new)->u
.extended
.value
= ldb_val_map_local(module
, *new, map
, &tree
->u
.extended
.value
);
887 (*new)->u
.extended
.rule_id
= talloc_strdup(*new, tree
->u
.extended
.rule_id
);
889 default: /* unknown kind of simple subtree */
891 return LDB_ERR_OPERATIONS_ERROR
;
897 /* Collect subtrees that query attributes in the remote partition */
898 static int map_subtree_collect_remote(struct ldb_module
*module
, void *mem_ctx
, struct ldb_parse_tree
**new, const struct ldb_parse_tree
*tree
)
900 const struct ldb_map_context
*data
= map_get_context(module
);
901 const struct ldb_map_attribute
*map
;
902 struct ldb_context
*ldb
;
904 ldb
= ldb_module_get_ctx(module
);
910 if (tree
->operation
== LDB_OP_NOT
) {
911 return map_subtree_collect_remote_not(module
, mem_ctx
, new, tree
);
914 if ((tree
->operation
== LDB_OP_AND
) || (tree
->operation
== LDB_OP_OR
)) {
915 return map_subtree_collect_remote_list(module
, mem_ctx
, new, tree
);
918 if (!map_attr_check_remote(data
, tree
->u
.equality
.attr
)) {
923 map
= map_attr_find_local(data
, tree
->u
.equality
.attr
);
924 if (map
->convert_operator
) {
925 return map
->convert_operator(module
, mem_ctx
, new, tree
);
928 if (map
->type
== LDB_MAP_GENERATE
) {
929 ldb_debug(ldb
, LDB_DEBUG_WARNING
, "ldb_map: "
930 "Skipping attribute '%s': "
931 "'convert_operator' not set",
932 tree
->u
.equality
.attr
);
937 return map_subtree_collect_remote_simple(module
, mem_ctx
, new, tree
, map
);
940 /* Split subtrees that query attributes in the local partition from
941 * those that query the remote partition. */
942 static int ldb_parse_tree_partition(struct ldb_module
*module
,
944 struct ldb_parse_tree
**local_tree
,
945 struct ldb_parse_tree
**remote_tree
,
946 const struct ldb_parse_tree
*tree
)
953 /* No original tree */
958 /* Generate local tree */
959 ret
= map_subtree_select_local(module
, mem_ctx
, local_tree
, tree
);
964 /* Generate remote tree */
965 ret
= map_subtree_collect_remote(module
, mem_ctx
, remote_tree
, tree
);
967 talloc_free(*local_tree
);
974 /* Collect a list of attributes required either explicitly from a
975 * given list or implicitly from a given parse tree; split the
976 * collected list into local and remote parts. */
977 static int map_attrs_collect_and_partition(struct ldb_module
*module
, struct map_context
*ac
,
978 const char * const *search_attrs
,
979 const struct ldb_parse_tree
*tree
)
982 const char **tree_attrs
;
983 const char **remote_attrs
;
984 const char **local_attrs
;
987 /* There is no tree, just partition the searched attributes */
989 ret
= map_attrs_partition(module
, ac
,
990 &local_attrs
, &remote_attrs
, search_attrs
);
992 ac
->local_attrs
= local_attrs
;
993 ac
->remote_attrs
= remote_attrs
;
994 ac
->all_attrs
= search_attrs
;
999 /* Create context for temporary memory */
1000 tmp_ctx
= talloc_new(ac
);
1001 if (tmp_ctx
== NULL
) {
1005 /* Prepare list of attributes from tree */
1006 tree_attrs
= talloc_array(tmp_ctx
, const char *, 1);
1007 if (tree_attrs
== NULL
) {
1008 talloc_free(tmp_ctx
);
1011 tree_attrs
[0] = NULL
;
1013 /* Collect attributes from tree */
1014 ret
= ldb_parse_tree_collect_attrs(module
, tmp_ctx
, &tree_attrs
, tree
);
1019 /* Merge attributes from search operation */
1020 ret
= map_attrs_merge(module
, tmp_ctx
, &tree_attrs
, search_attrs
);
1025 /* Split local from remote attributes */
1026 ret
= map_attrs_partition(module
, ac
, &local_attrs
,
1027 &remote_attrs
, tree_attrs
);
1030 ac
->local_attrs
= local_attrs
;
1031 ac
->remote_attrs
= remote_attrs
;
1032 talloc_steal(ac
, tree_attrs
);
1033 ac
->all_attrs
= tree_attrs
;
1036 /* Free temporary memory */
1037 talloc_free(tmp_ctx
);
1042 return LDB_ERR_OPERATIONS_ERROR
;
1046 /* Outbound requests: search
1047 * ========================= */
1049 static int map_remote_search_callback(struct ldb_request
*req
,
1050 struct ldb_reply
*ares
);
1051 static int map_local_merge_callback(struct ldb_request
*req
,
1052 struct ldb_reply
*ares
);
1053 static int map_search_local(struct map_context
*ac
);
1055 static int map_save_entry(struct map_context
*ac
, struct ldb_reply
*ares
)
1057 struct map_reply
*mr
;
1059 mr
= talloc_zero(ac
, struct map_reply
);
1061 map_oom(ac
->module
);
1062 return LDB_ERR_OPERATIONS_ERROR
;
1064 mr
->remote
= talloc_steal(mr
, ares
);
1065 if (ac
->r_current
) {
1066 ac
->r_current
->next
= mr
;
1076 /* Pass a merged search result up the callback chain. */
1077 int map_return_entry(struct map_context
*ac
, struct ldb_reply
*ares
)
1079 struct ldb_message_element
*el
;
1080 const char * const *attrs
;
1081 struct ldb_context
*ldb
;
1086 ldb
= ldb_module_get_ctx(ac
->module
);
1088 /* Merged result doesn't match original query, skip */
1089 ret
= ldb_match_msg_error(ldb
, ares
->message
,
1090 ac
->req
->op
.search
.tree
,
1091 ac
->req
->op
.search
.base
,
1092 ac
->req
->op
.search
.scope
,
1094 if (ret
!= LDB_SUCCESS
) return ret
;
1096 ldb_debug(ldb
, LDB_DEBUG_TRACE
, "ldb_map: "
1097 "Skipping record '%s': "
1098 "doesn't match original search",
1099 ldb_dn_get_linearized(ares
->message
->dn
));
1103 /* Limit result to requested attrs */
1104 if (ac
->req
->op
.search
.attrs
&&
1105 (! ldb_attr_in_list(ac
->req
->op
.search
.attrs
, "*"))) {
1107 attrs
= ac
->req
->op
.search
.attrs
;
1110 while (i
< ares
->message
->num_elements
) {
1112 el
= &ares
->message
->elements
[i
];
1113 if ( ! ldb_attr_in_list(attrs
, el
->name
)) {
1114 ldb_msg_remove_element(ares
->message
, el
);
1121 return ldb_module_send_entry(ac
->req
, ares
->message
, ares
->controls
);
1124 /* Search a record. */
1125 int ldb_map_search(struct ldb_module
*module
, struct ldb_request
*req
)
1127 struct ldb_parse_tree
*remote_tree
;
1128 struct ldb_parse_tree
*local_tree
;
1129 struct ldb_request
*remote_req
;
1130 struct ldb_context
*ldb
;
1131 struct map_context
*ac
;
1134 const char *wildcard
[] = { "*", NULL
};
1135 const char * const *attrs
;
1137 ldb
= ldb_module_get_ctx(module
);
1139 /* if we're not yet initialized, go to the next module */
1140 if (!ldb_module_get_private(module
))
1141 return ldb_next_request(module
, req
);
1143 /* Do not manipulate our control entries */
1144 if (ldb_dn_is_special(req
->op
.search
.base
)) {
1145 return ldb_next_request(module
, req
);
1148 /* No mapping requested, skip to next module */
1149 if ((req
->op
.search
.base
) && (!ldb_dn_check_local(module
, req
->op
.search
.base
))) {
1150 return ldb_next_request(module
, req
);
1153 /* TODO: How can we be sure about which partition we are
1154 * targetting when there is no search base? */
1156 /* Prepare context and handle */
1157 ac
= map_init_context(module
, req
);
1159 return LDB_ERR_OPERATIONS_ERROR
;
1162 /* It is easier to deal with the two different ways of
1163 * expressing the wildcard in the same codepath */
1164 attrs
= req
->op
.search
.attrs
;
1165 if (attrs
== NULL
) {
1169 /* Split local from remote attrs */
1170 ret
= map_attrs_collect_and_partition(module
, ac
,
1171 attrs
, req
->op
.search
.tree
);
1173 return LDB_ERR_OPERATIONS_ERROR
;
1176 /* Split local from remote tree */
1177 ret
= ldb_parse_tree_partition(module
, ac
,
1178 &local_tree
, &remote_tree
,
1179 req
->op
.search
.tree
);
1181 return LDB_ERR_OPERATIONS_ERROR
;
1184 if (((local_tree
!= NULL
) && (remote_tree
!= NULL
)) &&
1185 (!ldb_parse_tree_check_splittable(req
->op
.search
.tree
))) {
1186 /* The query can't safely be split, enumerate the remote partition */
1191 if (local_tree
== NULL
) {
1192 /* Construct default local parse tree */
1193 local_tree
= talloc_zero(ac
, struct ldb_parse_tree
);
1194 if (local_tree
== NULL
) {
1195 map_oom(ac
->module
);
1196 return LDB_ERR_OPERATIONS_ERROR
;
1199 local_tree
->operation
= LDB_OP_PRESENT
;
1200 local_tree
->u
.present
.attr
= talloc_strdup(local_tree
, IS_MAPPED
);
1202 if (remote_tree
== NULL
) {
1203 /* Construct default remote parse tree */
1204 remote_tree
= ldb_parse_tree(ac
, NULL
);
1205 if (remote_tree
== NULL
) {
1206 return LDB_ERR_OPERATIONS_ERROR
;
1210 ac
->local_tree
= local_tree
;
1212 /* Prepare the remote operation */
1213 ret
= ldb_build_search_req_ex(&remote_req
, ldb
, ac
,
1214 req
->op
.search
.base
,
1215 req
->op
.search
.scope
,
1219 ac
, map_remote_search_callback
,
1221 LDB_REQ_SET_LOCATION(remote_req
);
1222 if (ret
!= LDB_SUCCESS
) {
1223 return LDB_ERR_OPERATIONS_ERROR
;
1226 return ldb_next_remote_request(module
, remote_req
);
1229 /* Now, search the local part of a remote search result. */
1230 static int map_remote_search_callback(struct ldb_request
*req
,
1231 struct ldb_reply
*ares
)
1233 struct map_context
*ac
;
1236 ac
= talloc_get_type(req
->context
, struct map_context
);
1239 return ldb_module_done(ac
->req
, NULL
, NULL
,
1240 LDB_ERR_OPERATIONS_ERROR
);
1242 if (ares
->error
!= LDB_SUCCESS
) {
1243 return ldb_module_done(ac
->req
, ares
->controls
,
1244 ares
->response
, ares
->error
);
1247 switch (ares
->type
) {
1248 case LDB_REPLY_REFERRAL
:
1250 /* ignore referrals */
1254 case LDB_REPLY_ENTRY
:
1256 /* Map result record into a local message */
1257 ret
= map_reply_remote(ac
, ares
);
1260 return ldb_module_done(ac
->req
, NULL
, NULL
,
1261 LDB_ERR_OPERATIONS_ERROR
);
1264 /* if we have no local db, then we can just return the reply to
1265 * the upper layer, otherwise we must save it and process it
1266 * when all replies ahve been gathered */
1267 if ( ! map_check_local_db(ac
->module
)) {
1268 ret
= map_return_entry(ac
, ares
);
1270 ret
= map_save_entry(ac
,ares
);
1273 if (ret
!= LDB_SUCCESS
) {
1275 return ldb_module_done(ac
->req
, NULL
, NULL
, ret
);
1279 case LDB_REPLY_DONE
:
1281 if ( ! map_check_local_db(ac
->module
)) {
1282 return ldb_module_done(ac
->req
, ares
->controls
,
1283 ares
->response
, LDB_SUCCESS
);
1286 /* reset the pointer to the start of the list */
1287 ac
->r_current
= ac
->r_list
;
1289 /* no entry just return */
1290 if (ac
->r_current
== NULL
) {
1291 ret
= ldb_module_done(ac
->req
, ares
->controls
,
1292 ares
->response
, LDB_SUCCESS
);
1297 ac
->remote_done_ares
= talloc_steal(ac
, ares
);
1299 ret
= map_search_local(ac
);
1300 if (ret
!= LDB_SUCCESS
) {
1301 return ldb_module_done(ac
->req
, NULL
, NULL
, ret
);
1308 static int map_search_local(struct map_context
*ac
)
1310 struct ldb_request
*search_req
;
1312 if (ac
->r_current
== NULL
|| ac
->r_current
->remote
== NULL
) {
1313 return LDB_ERR_OPERATIONS_ERROR
;
1316 /* Prepare local search request */
1317 /* TODO: use GUIDs here instead? */
1318 search_req
= map_search_base_req(ac
,
1319 ac
->r_current
->remote
->message
->dn
,
1321 ac
, map_local_merge_callback
);
1322 if (search_req
== NULL
) {
1323 return LDB_ERR_OPERATIONS_ERROR
;
1326 return ldb_next_request(ac
->module
, search_req
);
1329 /* Merge the remote and local parts of a search result. */
1330 int map_local_merge_callback(struct ldb_request
*req
, struct ldb_reply
*ares
)
1332 struct ldb_context
*ldb
;
1333 struct map_context
*ac
;
1336 ac
= talloc_get_type(req
->context
, struct map_context
);
1337 ldb
= ldb_module_get_ctx(ac
->module
);
1340 return ldb_module_done(ac
->req
, NULL
, NULL
,
1341 LDB_ERR_OPERATIONS_ERROR
);
1343 if (ares
->error
!= LDB_SUCCESS
) {
1344 return ldb_module_done(ac
->req
, ares
->controls
,
1345 ares
->response
, ares
->error
);
1348 switch (ares
->type
) {
1349 case LDB_REPLY_ENTRY
:
1350 /* We have already found a local record */
1351 if (ac
->r_current
->local
) {
1353 ldb_set_errstring(ldb
, "ldb_map: Too many results!");
1354 return ldb_module_done(ac
->req
, NULL
, NULL
,
1355 LDB_ERR_OPERATIONS_ERROR
);
1358 /* Store local result */
1359 ac
->r_current
->local
= talloc_steal(ac
->r_current
, ares
);
1363 case LDB_REPLY_REFERRAL
:
1364 /* ignore referrals */
1368 case LDB_REPLY_DONE
:
1369 /* We don't need the local 'ares', but we will use the remote one from below */
1372 /* No local record found, map and send remote record */
1373 if (ac
->r_current
->local
!= NULL
) {
1374 /* Merge remote into local message */
1375 ret
= ldb_msg_merge_local(ac
->module
,
1376 ac
->r_current
->local
->message
,
1377 ac
->r_current
->remote
->message
);
1378 if (ret
== LDB_SUCCESS
) {
1379 ret
= map_return_entry(ac
, ac
->r_current
->local
);
1381 if (ret
!= LDB_SUCCESS
) {
1382 return ldb_module_done(ac
->req
, NULL
, NULL
,
1383 LDB_ERR_OPERATIONS_ERROR
);
1386 ret
= map_return_entry(ac
, ac
->r_current
->remote
);
1387 if (ret
!= LDB_SUCCESS
) {
1388 return ldb_module_done(ac
->req
,
1393 if (ac
->r_current
->next
!= NULL
) {
1394 ac
->r_current
= ac
->r_current
->next
;
1395 if (ac
->r_current
->remote
->type
== LDB_REPLY_ENTRY
) {
1396 ret
= map_search_local(ac
);
1397 if (ret
!= LDB_SUCCESS
) {
1398 return ldb_module_done(ac
->req
,
1405 /* ok we are done with all search, finally it is time to
1406 * finish operations for this module */
1407 return ldb_module_done(ac
->req
,
1408 ac
->remote_done_ares
->controls
,
1409 ac
->remote_done_ares
->response
,
1410 ac
->remote_done_ares
->error
);