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",
336 case LDB_MAP_RENDROP
:
337 old
= ldb_msg_find_element(remote
, remote_name
);
339 el
= ldb_msg_el_map_remote(module
, local
, map
, attr_name
, old
);
341 return LDB_ERR_NO_SUCH_ATTRIBUTE
;
345 case LDB_MAP_GENERATE
:
346 if (map
->u
.generate
.generate_local
== NULL
) {
347 ldb_debug(ldb
, LDB_DEBUG_ERROR
, "ldb_map: "
348 "Skipping attribute '%s': "
349 "'generate_local' not set",
354 el
= map
->u
.generate
.generate_local(module
, local
, attr_name
, remote
);
356 /* Generation failure is probably due to lack of source attributes */
357 return LDB_ERR_NO_SUCH_ATTRIBUTE
;
363 return LDB_ERR_NO_SUCH_ATTRIBUTE
;
366 return ldb_msg_replace(local
, el
);
369 /* Handle wildcard parts of merging a remote message element into a local message. */
370 static int ldb_msg_el_merge_wildcard(struct ldb_module
*module
, struct ldb_message
*local
,
371 struct ldb_message
*remote
)
373 const struct ldb_map_context
*data
= map_get_context(module
);
374 const struct ldb_map_attribute
*map
= map_attr_find_local(data
, "*");
375 struct ldb_message_element
*el
=NULL
;
379 /* Perhaps we have a mapping for "*" */
380 if (map
&& map
->type
== LDB_MAP_KEEP
) {
381 /* We copy everything over, and hope that anything with a
382 more specific rule is overwritten */
383 for (i
= 0; i
< remote
->num_elements
; i
++) {
384 el
= ldb_msg_el_map_remote(module
, local
, map
, remote
->elements
[i
].name
,
385 &remote
->elements
[i
]);
387 return LDB_ERR_OPERATIONS_ERROR
;
390 ret
= ldb_msg_replace(local
, el
);
397 /* Now walk the list of possible mappings, and apply each */
398 for (i
= 0; data
->attribute_maps
[i
].local_name
; i
++) {
399 ret
= ldb_msg_el_merge(module
, local
, remote
,
400 data
->attribute_maps
[i
].local_name
);
401 if (ret
== LDB_ERR_NO_SUCH_ATTRIBUTE
) {
414 * ================ */
416 /* Merge two local messages into a single one. */
417 static int ldb_msg_merge_local(struct ldb_module
*module
, struct ldb_message
*msg1
, struct ldb_message
*msg2
)
422 for (i
= 0; i
< msg2
->num_elements
; i
++) {
423 ret
= ldb_msg_replace(msg1
, &msg2
->elements
[i
]);
432 /* Merge a local and a remote message into a single local one. */
433 static int ldb_msg_merge_remote(struct map_context
*ac
, struct ldb_message
*local
,
434 struct ldb_message
*remote
)
438 const char * const *attrs
= ac
->all_attrs
;
440 ret
= ldb_msg_el_merge_wildcard(ac
->module
, local
, remote
);
446 for (i
= 0; attrs
&& attrs
[i
]; i
++) {
447 if (ldb_attr_cmp(attrs
[i
], "*") == 0) {
448 ret
= ldb_msg_el_merge_wildcard(ac
->module
, local
, remote
);
456 /* Try to map each attribute back;
457 * Add to local message is possible,
458 * Overwrite old local attribute if necessary */
459 for (i
= 0; attrs
&& attrs
[i
]; i
++) {
460 ret
= ldb_msg_el_merge(ac
->module
, local
, remote
,
462 if (ret
== LDB_ERR_NO_SUCH_ATTRIBUTE
) {
471 /* Mapping search results
472 * ====================== */
474 /* Map a search result back into the local partition. */
475 static int map_reply_remote(struct map_context
*ac
, struct ldb_reply
*ares
)
477 struct ldb_message
*msg
;
481 /* There is no result message, skip */
482 if (ares
->type
!= LDB_REPLY_ENTRY
) {
486 /* Create a new result message */
487 msg
= ldb_msg_new(ares
);
490 return LDB_ERR_OPERATIONS_ERROR
;
493 /* Merge remote message into new message */
494 ret
= ldb_msg_merge_remote(ac
, msg
, ares
->message
);
500 /* Create corresponding local DN */
501 dn
= ldb_dn_map_rebase_remote(ac
->module
, msg
, ares
->message
->dn
);
504 return LDB_ERR_OPERATIONS_ERROR
;
508 /* Store new message with new DN as the result */
509 talloc_free(ares
->message
);
515 /* Mapping parse trees
516 * =================== */
518 /* Check whether a parse tree can safely be split in two. */
519 static bool ldb_parse_tree_check_splittable(const struct ldb_parse_tree
*tree
)
521 const struct ldb_parse_tree
*subtree
= tree
;
525 switch (subtree
->operation
) {
528 subtree
= subtree
->u
.isnot
.child
;
532 return !negate
; /* if negate: False */
535 return negate
; /* if negate: True */
538 return true; /* simple parse tree */
542 return true; /* no parse tree */
545 /* Collect a list of attributes required to match a given parse tree. */
546 static int ldb_parse_tree_collect_attrs(struct ldb_module
*module
, void *mem_ctx
, const char ***attrs
, const struct ldb_parse_tree
*tree
)
548 const char **new_attrs
;
556 switch (tree
->operation
) {
558 case LDB_OP_AND
: /* attributes stored in list of subtrees */
559 for (i
= 0; i
< tree
->u
.list
.num_elements
; i
++) {
560 ret
= ldb_parse_tree_collect_attrs(module
, mem_ctx
,
561 attrs
, tree
->u
.list
.elements
[i
]);
568 case LDB_OP_NOT
: /* attributes stored in single subtree */
569 return ldb_parse_tree_collect_attrs(module
, mem_ctx
, attrs
, tree
->u
.isnot
.child
);
571 default: /* single attribute in tree */
572 new_attrs
= ldb_attr_list_copy_add(mem_ctx
, *attrs
, tree
->u
.equality
.attr
);
579 static int map_subtree_select_local(struct ldb_module
*module
, void *mem_ctx
, struct ldb_parse_tree
**new, const struct ldb_parse_tree
*tree
);
581 /* Select a negated subtree that queries attributes in the local partition */
582 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
)
584 struct ldb_parse_tree
*child
;
587 /* Prepare new tree */
588 *new = talloc_memdup(mem_ctx
, tree
, sizeof(struct ldb_parse_tree
));
591 return LDB_ERR_OPERATIONS_ERROR
;
594 /* Generate new subtree */
595 ret
= map_subtree_select_local(module
, *new, &child
, tree
->u
.isnot
.child
);
601 /* Prune tree without subtree */
608 (*new)->u
.isnot
.child
= child
;
613 /* Select a list of subtrees that query attributes in the local partition */
614 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
)
619 /* Prepare new tree */
620 *new = talloc_memdup(mem_ctx
, tree
, sizeof(struct ldb_parse_tree
));
623 return LDB_ERR_OPERATIONS_ERROR
;
626 /* Prepare list of subtrees */
627 (*new)->u
.list
.num_elements
= 0;
628 (*new)->u
.list
.elements
= talloc_array(*new, struct ldb_parse_tree
*, tree
->u
.list
.num_elements
);
629 if ((*new)->u
.list
.elements
== NULL
) {
632 return LDB_ERR_OPERATIONS_ERROR
;
635 /* Generate new list of subtrees */
637 for (i
= 0; i
< tree
->u
.list
.num_elements
; i
++) {
638 struct ldb_parse_tree
*child
;
639 ret
= map_subtree_select_local(module
, *new, &child
, tree
->u
.list
.elements
[i
]);
646 (*new)->u
.list
.elements
[j
] = child
;
651 /* Prune tree without subtrees */
658 /* Fix subtree list size */
659 (*new)->u
.list
.num_elements
= j
;
660 (*new)->u
.list
.elements
= talloc_realloc(*new, (*new)->u
.list
.elements
, struct ldb_parse_tree
*, (*new)->u
.list
.num_elements
);
665 /* Select a simple subtree that queries attributes in the local partition */
666 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
)
668 /* Prepare new tree */
669 *new = talloc_memdup(mem_ctx
, tree
, sizeof(struct ldb_parse_tree
));
672 return LDB_ERR_OPERATIONS_ERROR
;
678 /* Select subtrees that query attributes in the local partition */
679 static int map_subtree_select_local(struct ldb_module
*module
, void *mem_ctx
, struct ldb_parse_tree
**new, const struct ldb_parse_tree
*tree
)
681 const struct ldb_map_context
*data
= map_get_context(module
);
687 if (tree
->operation
== LDB_OP_NOT
) {
688 return map_subtree_select_local_not(module
, mem_ctx
, new, tree
);
691 if (tree
->operation
== LDB_OP_AND
|| tree
->operation
== LDB_OP_OR
) {
692 return map_subtree_select_local_list(module
, mem_ctx
, new, tree
);
695 if (map_attr_check_remote(data
, tree
->u
.equality
.attr
)) {
700 return map_subtree_select_local_simple(module
, mem_ctx
, new, tree
);
703 static int map_subtree_collect_remote(struct ldb_module
*module
, void *mem_ctx
, struct ldb_parse_tree
**new, const struct ldb_parse_tree
*tree
);
705 /* Collect a negated subtree that queries attributes in the remote partition */
706 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
)
708 struct ldb_parse_tree
*child
;
711 /* Prepare new tree */
712 *new = talloc_memdup(mem_ctx
, tree
, sizeof(struct ldb_parse_tree
));
715 return LDB_ERR_OPERATIONS_ERROR
;
718 /* Generate new subtree */
719 ret
= map_subtree_collect_remote(module
, *new, &child
, tree
->u
.isnot
.child
);
725 /* Prune tree without subtree */
732 (*new)->u
.isnot
.child
= child
;
737 /* Collect a list of subtrees that query attributes in the remote partition */
738 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
)
743 /* Prepare new tree */
744 *new = talloc_memdup(mem_ctx
, tree
, sizeof(struct ldb_parse_tree
));
747 return LDB_ERR_OPERATIONS_ERROR
;
750 /* Prepare list of subtrees */
751 (*new)->u
.list
.num_elements
= 0;
752 (*new)->u
.list
.elements
= talloc_array(*new, struct ldb_parse_tree
*, tree
->u
.list
.num_elements
);
753 if ((*new)->u
.list
.elements
== NULL
) {
756 return LDB_ERR_OPERATIONS_ERROR
;
759 /* Generate new list of subtrees */
761 for (i
= 0; i
< tree
->u
.list
.num_elements
; i
++) {
762 struct ldb_parse_tree
*child
;
763 ret
= map_subtree_collect_remote(module
, *new, &child
, tree
->u
.list
.elements
[i
]);
770 (*new)->u
.list
.elements
[j
] = child
;
775 /* Prune tree without subtrees */
782 /* Fix subtree list size */
783 (*new)->u
.list
.num_elements
= j
;
784 (*new)->u
.list
.elements
= talloc_realloc(*new, (*new)->u
.list
.elements
, struct ldb_parse_tree
*, (*new)->u
.list
.num_elements
);
789 /* Collect a simple subtree that queries attributes in the remote partition */
790 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
)
794 /* Prepare new tree */
795 *new = talloc(mem_ctx
, struct ldb_parse_tree
);
798 return LDB_ERR_OPERATIONS_ERROR
;
802 if (map
->type
== LDB_MAP_KEEP
) {
803 /* Nothing to do here */
807 /* Store attribute and value in new tree */
808 switch (tree
->operation
) {
810 attr
= map_attr_map_local(*new, map
, tree
->u
.present
.attr
);
811 (*new)->u
.present
.attr
= attr
;
813 case LDB_OP_SUBSTRING
:
815 attr
= map_attr_map_local(*new, map
, tree
->u
.substring
.attr
);
816 (*new)->u
.substring
.attr
= attr
;
819 case LDB_OP_EQUALITY
:
820 attr
= map_attr_map_local(*new, map
, tree
->u
.equality
.attr
);
821 (*new)->u
.equality
.attr
= attr
;
826 attr
= map_attr_map_local(*new, map
, tree
->u
.comparison
.attr
);
827 (*new)->u
.comparison
.attr
= attr
;
829 case LDB_OP_EXTENDED
:
830 attr
= map_attr_map_local(*new, map
, tree
->u
.extended
.attr
);
831 (*new)->u
.extended
.attr
= attr
;
833 default: /* unknown kind of simple subtree */
835 return LDB_ERR_OPERATIONS_ERROR
;
844 if (map
->type
== LDB_MAP_RENAME
|| map
->type
== LDB_MAP_RENDROP
) {
845 /* Nothing more to do here, the attribute has been renamed */
849 /* Store attribute and value in new tree */
850 switch (tree
->operation
) {
853 case LDB_OP_SUBSTRING
:
857 (*new)->u
.substring
.chunks
= NULL
;
858 for (i
=0; tree
->u
.substring
.chunks
&& tree
->u
.substring
.chunks
[i
]; i
++) {
859 (*new)->u
.substring
.chunks
= talloc_realloc(*new, (*new)->u
.substring
.chunks
, struct ldb_val
*, i
+2);
860 if (!(*new)->u
.substring
.chunks
) {
865 (*new)->u
.substring
.chunks
[i
] = talloc(*new, struct ldb_val
);
866 if (!(*new)->u
.substring
.chunks
[i
]) {
871 *(*new)->u
.substring
.chunks
[i
] = ldb_val_map_local(module
, *new, map
, tree
->u
.substring
.chunks
[i
]);
872 (*new)->u
.substring
.chunks
[i
+1] = NULL
;
876 case LDB_OP_EQUALITY
:
877 (*new)->u
.equality
.value
= ldb_val_map_local(module
, *new, map
, &tree
->u
.equality
.value
);
882 (*new)->u
.comparison
.value
= ldb_val_map_local(module
, *new, map
, &tree
->u
.comparison
.value
);
884 case LDB_OP_EXTENDED
:
885 (*new)->u
.extended
.value
= ldb_val_map_local(module
, *new, map
, &tree
->u
.extended
.value
);
886 (*new)->u
.extended
.rule_id
= talloc_strdup(*new, tree
->u
.extended
.rule_id
);
888 default: /* unknown kind of simple subtree */
890 return LDB_ERR_OPERATIONS_ERROR
;
896 /* Collect subtrees that query attributes in the remote partition */
897 static int map_subtree_collect_remote(struct ldb_module
*module
, void *mem_ctx
, struct ldb_parse_tree
**new, const struct ldb_parse_tree
*tree
)
899 const struct ldb_map_context
*data
= map_get_context(module
);
900 const struct ldb_map_attribute
*map
;
901 struct ldb_context
*ldb
;
903 ldb
= ldb_module_get_ctx(module
);
909 if (tree
->operation
== LDB_OP_NOT
) {
910 return map_subtree_collect_remote_not(module
, mem_ctx
, new, tree
);
913 if ((tree
->operation
== LDB_OP_AND
) || (tree
->operation
== LDB_OP_OR
)) {
914 return map_subtree_collect_remote_list(module
, mem_ctx
, new, tree
);
917 if (!map_attr_check_remote(data
, tree
->u
.equality
.attr
)) {
922 map
= map_attr_find_local(data
, tree
->u
.equality
.attr
);
923 if (map
->convert_operator
) {
924 return map
->convert_operator(module
, mem_ctx
, new, tree
);
927 if (map
->type
== LDB_MAP_GENERATE
) {
928 ldb_debug(ldb
, LDB_DEBUG_WARNING
, "ldb_map: "
929 "Skipping attribute '%s': "
930 "'convert_operator' not set",
931 tree
->u
.equality
.attr
);
936 return map_subtree_collect_remote_simple(module
, mem_ctx
, new, tree
, map
);
939 /* Split subtrees that query attributes in the local partition from
940 * those that query the remote partition. */
941 static int ldb_parse_tree_partition(struct ldb_module
*module
,
943 struct ldb_parse_tree
**local_tree
,
944 struct ldb_parse_tree
**remote_tree
,
945 const struct ldb_parse_tree
*tree
)
952 /* No original tree */
957 /* Generate local tree */
958 ret
= map_subtree_select_local(module
, mem_ctx
, local_tree
, tree
);
963 /* Generate remote tree */
964 ret
= map_subtree_collect_remote(module
, mem_ctx
, remote_tree
, tree
);
966 talloc_free(*local_tree
);
973 /* Collect a list of attributes required either explicitly from a
974 * given list or implicitly from a given parse tree; split the
975 * collected list into local and remote parts. */
976 static int map_attrs_collect_and_partition(struct ldb_module
*module
, struct map_context
*ac
,
977 const char * const *search_attrs
,
978 const struct ldb_parse_tree
*tree
)
981 const char **tree_attrs
;
982 const char **remote_attrs
;
983 const char **local_attrs
;
986 /* There is no tree, just partition the searched attributes */
988 ret
= map_attrs_partition(module
, ac
,
989 &local_attrs
, &remote_attrs
, search_attrs
);
991 ac
->local_attrs
= local_attrs
;
992 ac
->remote_attrs
= remote_attrs
;
993 ac
->all_attrs
= search_attrs
;
998 /* Create context for temporary memory */
999 tmp_ctx
= talloc_new(ac
);
1000 if (tmp_ctx
== NULL
) {
1004 /* Prepare list of attributes from tree */
1005 tree_attrs
= talloc_array(tmp_ctx
, const char *, 1);
1006 if (tree_attrs
== NULL
) {
1007 talloc_free(tmp_ctx
);
1010 tree_attrs
[0] = NULL
;
1012 /* Collect attributes from tree */
1013 ret
= ldb_parse_tree_collect_attrs(module
, tmp_ctx
, &tree_attrs
, tree
);
1018 /* Merge attributes from search operation */
1019 ret
= map_attrs_merge(module
, tmp_ctx
, &tree_attrs
, search_attrs
);
1024 /* Split local from remote attributes */
1025 ret
= map_attrs_partition(module
, ac
, &local_attrs
,
1026 &remote_attrs
, tree_attrs
);
1029 ac
->local_attrs
= local_attrs
;
1030 ac
->remote_attrs
= remote_attrs
;
1031 talloc_steal(ac
, tree_attrs
);
1032 ac
->all_attrs
= tree_attrs
;
1035 /* Free temporary memory */
1036 talloc_free(tmp_ctx
);
1041 return LDB_ERR_OPERATIONS_ERROR
;
1045 /* Outbound requests: search
1046 * ========================= */
1048 static int map_remote_search_callback(struct ldb_request
*req
,
1049 struct ldb_reply
*ares
);
1050 static int map_local_merge_callback(struct ldb_request
*req
,
1051 struct ldb_reply
*ares
);
1052 static int map_search_local(struct map_context
*ac
);
1054 static int map_save_entry(struct map_context
*ac
, struct ldb_reply
*ares
)
1056 struct map_reply
*mr
;
1058 mr
= talloc_zero(ac
, struct map_reply
);
1060 map_oom(ac
->module
);
1061 return LDB_ERR_OPERATIONS_ERROR
;
1063 mr
->remote
= talloc_steal(mr
, ares
);
1064 if (ac
->r_current
) {
1065 ac
->r_current
->next
= mr
;
1075 /* Pass a merged search result up the callback chain. */
1076 int map_return_entry(struct map_context
*ac
, struct ldb_reply
*ares
)
1078 struct ldb_message_element
*el
;
1079 const char * const *attrs
;
1080 struct ldb_context
*ldb
;
1085 ldb
= ldb_module_get_ctx(ac
->module
);
1087 /* Merged result doesn't match original query, skip */
1088 ret
= ldb_match_msg_error(ldb
, ares
->message
,
1089 ac
->req
->op
.search
.tree
,
1090 ac
->req
->op
.search
.base
,
1091 ac
->req
->op
.search
.scope
,
1093 if (ret
!= LDB_SUCCESS
) return ret
;
1095 ldb_debug(ldb
, LDB_DEBUG_TRACE
, "ldb_map: "
1096 "Skipping record '%s': "
1097 "doesn't match original search",
1098 ldb_dn_get_linearized(ares
->message
->dn
));
1102 /* Limit result to requested attrs */
1103 if (ac
->req
->op
.search
.attrs
&&
1104 (! ldb_attr_in_list(ac
->req
->op
.search
.attrs
, "*"))) {
1106 attrs
= ac
->req
->op
.search
.attrs
;
1109 while (i
< ares
->message
->num_elements
) {
1111 el
= &ares
->message
->elements
[i
];
1112 if ( ! ldb_attr_in_list(attrs
, el
->name
)) {
1113 ldb_msg_remove_element(ares
->message
, el
);
1120 return ldb_module_send_entry(ac
->req
, ares
->message
, ares
->controls
);
1123 /* Search a record. */
1124 int ldb_map_search(struct ldb_module
*module
, struct ldb_request
*req
)
1126 struct ldb_parse_tree
*remote_tree
;
1127 struct ldb_parse_tree
*local_tree
;
1128 struct ldb_request
*remote_req
;
1129 struct ldb_context
*ldb
;
1130 struct map_context
*ac
;
1133 const char *wildcard
[] = { "*", NULL
};
1134 const char * const *attrs
;
1136 ldb
= ldb_module_get_ctx(module
);
1138 /* if we're not yet initialized, go to the next module */
1139 if (!ldb_module_get_private(module
))
1140 return ldb_next_request(module
, req
);
1142 /* Do not manipulate our control entries */
1143 if (ldb_dn_is_special(req
->op
.search
.base
)) {
1144 return ldb_next_request(module
, req
);
1147 /* No mapping requested, skip to next module */
1148 if ((req
->op
.search
.base
) && (!ldb_dn_check_local(module
, req
->op
.search
.base
))) {
1149 return ldb_next_request(module
, req
);
1152 /* TODO: How can we be sure about which partition we are
1153 * targetting when there is no search base? */
1155 /* Prepare context and handle */
1156 ac
= map_init_context(module
, req
);
1158 return LDB_ERR_OPERATIONS_ERROR
;
1161 /* It is easier to deal with the two different ways of
1162 * expressing the wildcard in the same codepath */
1163 attrs
= req
->op
.search
.attrs
;
1164 if (attrs
== NULL
) {
1168 /* Split local from remote attrs */
1169 ret
= map_attrs_collect_and_partition(module
, ac
,
1170 attrs
, req
->op
.search
.tree
);
1172 return LDB_ERR_OPERATIONS_ERROR
;
1175 /* Split local from remote tree */
1176 ret
= ldb_parse_tree_partition(module
, ac
,
1177 &local_tree
, &remote_tree
,
1178 req
->op
.search
.tree
);
1180 return LDB_ERR_OPERATIONS_ERROR
;
1183 if (((local_tree
!= NULL
) && (remote_tree
!= NULL
)) &&
1184 (!ldb_parse_tree_check_splittable(req
->op
.search
.tree
))) {
1185 /* The query can't safely be split, enumerate the remote partition */
1190 if (local_tree
== NULL
) {
1191 /* Construct default local parse tree */
1192 local_tree
= talloc_zero(ac
, struct ldb_parse_tree
);
1193 if (local_tree
== NULL
) {
1194 map_oom(ac
->module
);
1195 return LDB_ERR_OPERATIONS_ERROR
;
1198 local_tree
->operation
= LDB_OP_PRESENT
;
1199 local_tree
->u
.present
.attr
= talloc_strdup(local_tree
, IS_MAPPED
);
1201 if (remote_tree
== NULL
) {
1202 /* Construct default remote parse tree */
1203 remote_tree
= ldb_parse_tree(ac
, NULL
);
1204 if (remote_tree
== NULL
) {
1205 return LDB_ERR_OPERATIONS_ERROR
;
1209 ac
->local_tree
= local_tree
;
1211 /* Prepare the remote operation */
1212 ret
= ldb_build_search_req_ex(&remote_req
, ldb
, ac
,
1213 req
->op
.search
.base
,
1214 req
->op
.search
.scope
,
1218 ac
, map_remote_search_callback
,
1220 LDB_REQ_SET_LOCATION(remote_req
);
1221 if (ret
!= LDB_SUCCESS
) {
1222 return LDB_ERR_OPERATIONS_ERROR
;
1225 return ldb_next_remote_request(module
, remote_req
);
1228 /* Now, search the local part of a remote search result. */
1229 static int map_remote_search_callback(struct ldb_request
*req
,
1230 struct ldb_reply
*ares
)
1232 struct map_context
*ac
;
1235 ac
= talloc_get_type(req
->context
, struct map_context
);
1238 return ldb_module_done(ac
->req
, NULL
, NULL
,
1239 LDB_ERR_OPERATIONS_ERROR
);
1241 if (ares
->error
!= LDB_SUCCESS
) {
1242 return ldb_module_done(ac
->req
, ares
->controls
,
1243 ares
->response
, ares
->error
);
1246 switch (ares
->type
) {
1247 case LDB_REPLY_REFERRAL
:
1249 /* ignore referrals */
1253 case LDB_REPLY_ENTRY
:
1255 /* Map result record into a local message */
1256 ret
= map_reply_remote(ac
, ares
);
1259 return ldb_module_done(ac
->req
, NULL
, NULL
,
1260 LDB_ERR_OPERATIONS_ERROR
);
1263 /* if we have no local db, then we can just return the reply to
1264 * the upper layer, otherwise we must save it and process it
1265 * when all replies ahve been gathered */
1266 if ( ! map_check_local_db(ac
->module
)) {
1267 ret
= map_return_entry(ac
, ares
);
1269 ret
= map_save_entry(ac
,ares
);
1272 if (ret
!= LDB_SUCCESS
) {
1274 return ldb_module_done(ac
->req
, NULL
, NULL
, ret
);
1278 case LDB_REPLY_DONE
:
1280 if ( ! map_check_local_db(ac
->module
)) {
1281 return ldb_module_done(ac
->req
, ares
->controls
,
1282 ares
->response
, LDB_SUCCESS
);
1285 /* reset the pointer to the start of the list */
1286 ac
->r_current
= ac
->r_list
;
1288 /* no entry just return */
1289 if (ac
->r_current
== NULL
) {
1290 ret
= ldb_module_done(ac
->req
, ares
->controls
,
1291 ares
->response
, LDB_SUCCESS
);
1296 ac
->remote_done_ares
= talloc_steal(ac
, ares
);
1298 ret
= map_search_local(ac
);
1299 if (ret
!= LDB_SUCCESS
) {
1300 return ldb_module_done(ac
->req
, NULL
, NULL
, ret
);
1307 static int map_search_local(struct map_context
*ac
)
1309 struct ldb_request
*search_req
;
1311 if (ac
->r_current
== NULL
|| ac
->r_current
->remote
== NULL
) {
1312 return LDB_ERR_OPERATIONS_ERROR
;
1315 /* Prepare local search request */
1316 /* TODO: use GUIDs here instead? */
1317 search_req
= map_search_base_req(ac
,
1318 ac
->r_current
->remote
->message
->dn
,
1320 ac
, map_local_merge_callback
);
1321 if (search_req
== NULL
) {
1322 return LDB_ERR_OPERATIONS_ERROR
;
1325 return ldb_next_request(ac
->module
, search_req
);
1328 /* Merge the remote and local parts of a search result. */
1329 int map_local_merge_callback(struct ldb_request
*req
, struct ldb_reply
*ares
)
1331 struct ldb_context
*ldb
;
1332 struct map_context
*ac
;
1335 ac
= talloc_get_type(req
->context
, struct map_context
);
1336 ldb
= ldb_module_get_ctx(ac
->module
);
1339 return ldb_module_done(ac
->req
, NULL
, NULL
,
1340 LDB_ERR_OPERATIONS_ERROR
);
1342 if (ares
->error
!= LDB_SUCCESS
) {
1343 return ldb_module_done(ac
->req
, ares
->controls
,
1344 ares
->response
, ares
->error
);
1347 switch (ares
->type
) {
1348 case LDB_REPLY_ENTRY
:
1349 /* We have already found a local record */
1350 if (ac
->r_current
->local
) {
1352 ldb_set_errstring(ldb
, "ldb_map: Too many results!");
1353 return ldb_module_done(ac
->req
, NULL
, NULL
,
1354 LDB_ERR_OPERATIONS_ERROR
);
1357 /* Store local result */
1358 ac
->r_current
->local
= talloc_steal(ac
->r_current
, ares
);
1362 case LDB_REPLY_REFERRAL
:
1363 /* ignore referrals */
1367 case LDB_REPLY_DONE
:
1368 /* We don't need the local 'ares', but we will use the remote one from below */
1371 /* No local record found, map and send remote record */
1372 if (ac
->r_current
->local
!= NULL
) {
1373 /* Merge remote into local message */
1374 ret
= ldb_msg_merge_local(ac
->module
,
1375 ac
->r_current
->local
->message
,
1376 ac
->r_current
->remote
->message
);
1377 if (ret
== LDB_SUCCESS
) {
1378 ret
= map_return_entry(ac
, ac
->r_current
->local
);
1380 if (ret
!= LDB_SUCCESS
) {
1381 return ldb_module_done(ac
->req
, NULL
, NULL
,
1382 LDB_ERR_OPERATIONS_ERROR
);
1385 ret
= map_return_entry(ac
, ac
->r_current
->remote
);
1386 if (ret
!= LDB_SUCCESS
) {
1387 return ldb_module_done(ac
->req
,
1392 if (ac
->r_current
->next
!= NULL
) {
1393 ac
->r_current
= ac
->r_current
->next
;
1394 if (ac
->r_current
->remote
->type
== LDB_REPLY_ENTRY
) {
1395 ret
= map_search_local(ac
);
1396 if (ret
!= LDB_SUCCESS
) {
1397 return ldb_module_done(ac
->req
,
1404 /* ok we are done with all search, finally it is time to
1405 * finish operations for this module */
1406 return ldb_module_done(ac
->req
,
1407 ac
->remote_done_ares
->controls
,
1408 ac
->remote_done_ares
->response
,
1409 ac
->remote_done_ares
->error
);