Search for location of waf script
[Samba.git] / lib / ldb / ldb_map / ldb_map_outbound.c
blob1f1a7e801422c4c34f8ae1073751b126ed24e7bd
1 /*
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
11 ** under the LGPL
13 This library is free software; you can redistribute it and/or
14 modify it under the terms of the GNU Lesser General Public
15 License as published by the Free Software Foundation; either
16 version 3 of the License, or (at your option) any later version.
18 This library is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 Lesser General Public License for more details.
23 You should have received a copy of the GNU Lesser General Public
24 License along with this library; if not, see <http://www.gnu.org/licenses/>.
28 #include "replace.h"
29 #include "system/filesys.h"
30 #include "system/time.h"
31 #include "ldb_map.h"
32 #include "ldb_map_private.h"
35 /* Mapping attributes
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);
42 const char **result;
43 unsigned int i, last;
45 if (attrs == NULL)
46 return NULL;
48 last = 0;
49 result = talloc_array(mem_ctx, const char *, 1);
50 if (result == NULL) {
51 goto failed;
53 result[0] = NULL;
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);
60 if (result == NULL) {
61 goto failed;
64 result[last] = talloc_strdup(result, attrs[i]);
65 result[last+1] = NULL;
66 last++;
70 return result;
72 failed:
73 talloc_free(result);
74 map_oom(module);
75 return 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);
83 const char **result;
84 const struct ldb_map_attribute *map;
85 const char *name=NULL;
86 unsigned int i, j, last;
87 int ret;
89 last = 0;
90 result = talloc_array(mem_ctx, const char *, 1);
91 if (result == NULL) {
92 goto failed;
94 result[0] = NULL;
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) {
102 goto failed;
104 ret = map_attrs_merge(module, mem_ctx, &new_attrs, data->wildcard_attributes);
105 if (ret != LDB_SUCCESS) {
106 goto failed;
109 attrs = new_attrs;
110 break;
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 */
118 name = attrs[i];
119 goto named;
122 /* Add remote names of mapped attrs */
123 map = map_attr_find_local(data, attrs[i]);
124 if (map == NULL) {
125 continue;
128 switch (map->type) {
129 case LDB_MAP_IGNORE:
130 continue;
132 case LDB_MAP_KEEP:
133 name = attrs[i];
134 goto named;
136 case LDB_MAP_RENAME:
137 case LDB_MAP_RENDROP:
138 case LDB_MAP_CONVERT:
139 name = map->u.rename.remote_name;
140 goto named;
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) {
147 goto failed;
150 result[last] = talloc_strdup(result, map->u.generate.remote_names[j]);
151 result[last+1] = NULL;
152 last++;
154 continue;
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) {
160 goto failed;
163 result[last] = talloc_strdup(result, name);
164 result[last+1] = NULL;
165 last++;
168 return result;
170 failed:
171 talloc_free(result);
172 map_oom(module);
173 return 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);
183 return 0;
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;
193 unsigned j;
194 old = ldb_msg_find_element(msg, el->name);
196 /* no local result, add as new element */
197 if (old == NULL) {
198 if (ldb_msg_add_empty(msg, el->name, 0, &old) != 0) {
199 return LDB_ERR_OPERATIONS_ERROR;
202 else {
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;
219 return 0;
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,
224 void *mem_ctx,
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;
232 unsigned int i;
234 el = talloc_zero(mem_ctx, struct ldb_message_element);
235 if (el == NULL) {
236 map_oom(module);
237 return NULL;
240 el->values = talloc_array(el, struct ldb_val, old->num_values);
241 if (el->values == NULL) {
242 talloc_free(el);
243 map_oom(module);
244 return 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;
255 break;
259 el->name = talloc_strdup(el, local_attr_name);
260 if (el->name == NULL) {
261 talloc_free(el);
262 map_oom(module);
263 return 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) {
270 talloc_free(el);
271 return NULL;
273 el->num_values++;
276 return el;
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) {
293 return LDB_SUCCESS;
296 map = map_attr_find_local(data, attr_name);
298 /* Unknown attribute in remote message:
299 * skip, attribute was probably auto-generated */
300 if (map == NULL) {
301 return LDB_SUCCESS;
304 switch (map->type) {
305 case LDB_MAP_IGNORE:
306 break;
307 case LDB_MAP_CONVERT:
308 remote_name = map->u.convert.remote_name;
309 break;
310 case LDB_MAP_KEEP:
311 remote_name = attr_name;
312 break;
313 case LDB_MAP_RENAME:
314 case LDB_MAP_RENDROP:
315 remote_name = map->u.rename.remote_name;
316 break;
317 case LDB_MAP_GENERATE:
318 break;
321 switch (map->type) {
322 case LDB_MAP_IGNORE:
323 return LDB_SUCCESS;
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",
330 attr_name);
331 return LDB_SUCCESS;
334 FALL_THROUGH;
335 case LDB_MAP_KEEP:
336 case LDB_MAP_RENAME:
337 case LDB_MAP_RENDROP:
338 old = ldb_msg_find_element(remote, remote_name);
339 if (old) {
340 el = ldb_msg_el_map_remote(module, local, map, attr_name, old);
341 } else {
342 return LDB_ERR_NO_SUCH_ATTRIBUTE;
344 break;
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",
351 attr_name);
352 return LDB_SUCCESS;
355 el = map->u.generate.generate_local(module, local, attr_name, remote);
356 if (!el) {
357 /* Generation failure is probably due to lack of source attributes */
358 return LDB_ERR_NO_SUCH_ATTRIBUTE;
360 break;
363 if (el == NULL) {
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;
377 unsigned int i;
378 int ret;
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]);
387 if (el == NULL) {
388 return LDB_ERR_OPERATIONS_ERROR;
391 ret = ldb_msg_replace(local, el);
392 if (ret) {
393 return ret;
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) {
403 continue;
404 } else if (ret) {
405 return ret;
406 } else {
407 continue;
411 return LDB_SUCCESS;
414 /* Mapping messages
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)
420 unsigned int i;
421 int ret;
423 for (i = 0; i < msg2->num_elements; i++) {
424 ret = ldb_msg_replace(msg1, &msg2->elements[i]);
425 if (ret) {
426 return ret;
430 return LDB_SUCCESS;
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)
437 unsigned int i;
438 int ret;
439 const char * const *attrs = ac->all_attrs;
440 if (!attrs) {
441 ret = ldb_msg_el_merge_wildcard(ac->module, local, remote);
442 if (ret) {
443 return ret;
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);
450 if (ret) {
451 return ret;
453 break;
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,
462 attrs[i]);
463 if (ret == LDB_ERR_NO_SUCH_ATTRIBUTE) {
464 } else if (ret) {
465 return ret;
469 return LDB_SUCCESS;
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;
479 struct ldb_dn *dn;
480 int ret;
482 /* There is no result message, skip */
483 if (ares->type != LDB_REPLY_ENTRY) {
484 return 0;
487 /* Create a new result message */
488 msg = ldb_msg_new(ares);
489 if (msg == NULL) {
490 map_oom(ac->module);
491 return LDB_ERR_OPERATIONS_ERROR;
494 /* Merge remote message into new message */
495 ret = ldb_msg_merge_remote(ac, msg, ares->message);
496 if (ret) {
497 talloc_free(msg);
498 return ret;
501 /* Create corresponding local DN */
502 dn = ldb_dn_map_rebase_remote(ac->module, msg, ares->message->dn);
503 if (dn == NULL) {
504 talloc_free(msg);
505 return LDB_ERR_OPERATIONS_ERROR;
507 msg->dn = dn;
509 /* Store new message with new DN as the result */
510 talloc_free(ares->message);
511 ares->message = msg;
513 return 0;
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;
523 bool negate = false;
525 while (subtree) {
526 switch (subtree->operation) {
527 case LDB_OP_NOT:
528 negate = !negate;
529 subtree = subtree->u.isnot.child;
530 continue;
532 case LDB_OP_AND:
533 return !negate; /* if negate: False */
535 case LDB_OP_OR:
536 return negate; /* if negate: True */
538 default:
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;
550 unsigned int i;
551 int ret;
553 if (tree == NULL) {
554 return 0;
557 switch (tree->operation) {
558 case LDB_OP_OR:
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]);
563 if (ret) {
564 return ret;
567 return 0;
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);
574 talloc_free(*attrs);
575 *attrs = new_attrs;
576 return 0;
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;
586 int ret;
588 /* Prepare new tree */
589 *new = talloc_memdup(mem_ctx, tree, sizeof(struct ldb_parse_tree));
590 if (*new == NULL) {
591 map_oom(module);
592 return LDB_ERR_OPERATIONS_ERROR;
595 /* Generate new subtree */
596 ret = map_subtree_select_local(module, *new, &child, tree->u.isnot.child);
597 if (ret) {
598 talloc_free(*new);
599 return ret;
602 /* Prune tree without subtree */
603 if (child == NULL) {
604 talloc_free(*new);
605 *new = NULL;
606 return 0;
609 (*new)->u.isnot.child = child;
611 return ret;
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)
617 unsigned int i, j;
618 int ret=0;
620 /* Prepare new tree */
621 *new = talloc_memdup(mem_ctx, tree, sizeof(struct ldb_parse_tree));
622 if (*new == NULL) {
623 map_oom(module);
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) {
631 map_oom(module);
632 talloc_free(*new);
633 return LDB_ERR_OPERATIONS_ERROR;
636 /* Generate new list of subtrees */
637 j = 0;
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]);
641 if (ret) {
642 talloc_free(*new);
643 return ret;
646 if (child) {
647 (*new)->u.list.elements[j] = child;
648 j++;
652 /* Prune tree without subtrees */
653 if (j == 0) {
654 talloc_free(*new);
655 *new = NULL;
656 return 0;
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);
663 return ret;
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));
671 if (*new == NULL) {
672 map_oom(module);
673 return LDB_ERR_OPERATIONS_ERROR;
676 return 0;
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);
684 if (tree == NULL) {
685 return 0;
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)) {
697 *new = NULL;
698 return 0;
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;
710 int ret;
712 /* Prepare new tree */
713 *new = talloc_memdup(mem_ctx, tree, sizeof(struct ldb_parse_tree));
714 if (*new == NULL) {
715 map_oom(module);
716 return LDB_ERR_OPERATIONS_ERROR;
719 /* Generate new subtree */
720 ret = map_subtree_collect_remote(module, *new, &child, tree->u.isnot.child);
721 if (ret) {
722 talloc_free(*new);
723 return ret;
726 /* Prune tree without subtree */
727 if (child == NULL) {
728 talloc_free(*new);
729 *new = NULL;
730 return 0;
733 (*new)->u.isnot.child = child;
735 return ret;
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)
741 unsigned int i, j;
742 int ret=0;
744 /* Prepare new tree */
745 *new = talloc_memdup(mem_ctx, tree, sizeof(struct ldb_parse_tree));
746 if (*new == NULL) {
747 map_oom(module);
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) {
755 map_oom(module);
756 talloc_free(*new);
757 return LDB_ERR_OPERATIONS_ERROR;
760 /* Generate new list of subtrees */
761 j = 0;
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]);
765 if (ret) {
766 talloc_free(*new);
767 return ret;
770 if (child) {
771 (*new)->u.list.elements[j] = child;
772 j++;
776 /* Prune tree without subtrees */
777 if (j == 0) {
778 talloc_free(*new);
779 *new = NULL;
780 return 0;
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);
787 return ret;
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)
793 const char *attr;
795 /* Prepare new tree */
796 *new = talloc(mem_ctx, struct ldb_parse_tree);
797 if (*new == NULL) {
798 map_oom(module);
799 return LDB_ERR_OPERATIONS_ERROR;
801 **new = *tree;
803 if (map->type == LDB_MAP_KEEP) {
804 /* Nothing to do here */
805 return 0;
808 /* Store attribute and value in new tree */
809 switch (tree->operation) {
810 case LDB_OP_PRESENT:
811 attr = map_attr_map_local(*new, map, tree->u.present.attr);
812 (*new)->u.present.attr = attr;
813 break;
814 case LDB_OP_SUBSTRING:
816 attr = map_attr_map_local(*new, map, tree->u.substring.attr);
817 (*new)->u.substring.attr = attr;
818 break;
820 case LDB_OP_EQUALITY:
821 attr = map_attr_map_local(*new, map, tree->u.equality.attr);
822 (*new)->u.equality.attr = attr;
823 break;
824 case LDB_OP_LESS:
825 case LDB_OP_GREATER:
826 case LDB_OP_APPROX:
827 attr = map_attr_map_local(*new, map, tree->u.comparison.attr);
828 (*new)->u.comparison.attr = attr;
829 break;
830 case LDB_OP_EXTENDED:
831 attr = map_attr_map_local(*new, map, tree->u.extended.attr);
832 (*new)->u.extended.attr = attr;
833 break;
834 default: /* unknown kind of simple subtree */
835 talloc_free(*new);
836 return LDB_ERR_OPERATIONS_ERROR;
839 if (attr == NULL) {
840 talloc_free(*new);
841 *new = NULL;
842 return 0;
845 if (map->type == LDB_MAP_RENAME || map->type == LDB_MAP_RENDROP) {
846 /* Nothing more to do here, the attribute has been renamed */
847 return 0;
850 /* Store attribute and value in new tree */
851 switch (tree->operation) {
852 case LDB_OP_PRESENT:
853 break;
854 case LDB_OP_SUBSTRING:
856 int i;
857 /* Map value */
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) {
862 talloc_free(*new);
863 *new = NULL;
864 return 0;
866 (*new)->u.substring.chunks[i] = talloc(*new, struct ldb_val);
867 if (!(*new)->u.substring.chunks[i]) {
868 talloc_free(*new);
869 *new = NULL;
870 return 0;
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;
875 break;
877 case LDB_OP_EQUALITY:
878 (*new)->u.equality.value = ldb_val_map_local(module, *new, map, &tree->u.equality.value);
879 break;
880 case LDB_OP_LESS:
881 case LDB_OP_GREATER:
882 case LDB_OP_APPROX:
883 (*new)->u.comparison.value = ldb_val_map_local(module, *new, map, &tree->u.comparison.value);
884 break;
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);
888 break;
889 default: /* unknown kind of simple subtree */
890 talloc_free(*new);
891 return LDB_ERR_OPERATIONS_ERROR;
894 return 0;
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);
906 if (tree == NULL) {
907 return 0;
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)) {
919 *new = NULL;
920 return 0;
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);
933 *new = NULL;
934 return 0;
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,
943 void *mem_ctx,
944 struct ldb_parse_tree **local_tree,
945 struct ldb_parse_tree **remote_tree,
946 const struct ldb_parse_tree *tree)
948 int ret;
950 *local_tree = NULL;
951 *remote_tree = NULL;
953 /* No original tree */
954 if (tree == NULL) {
955 return 0;
958 /* Generate local tree */
959 ret = map_subtree_select_local(module, mem_ctx, local_tree, tree);
960 if (ret) {
961 return ret;
964 /* Generate remote tree */
965 ret = map_subtree_collect_remote(module, mem_ctx, remote_tree, tree);
966 if (ret) {
967 talloc_free(*local_tree);
968 return ret;
971 return 0;
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)
981 void *tmp_ctx;
982 const char **tree_attrs;
983 const char **remote_attrs;
984 const char **local_attrs;
985 int ret;
987 /* There is no tree, just partition the searched attributes */
988 if (tree == NULL) {
989 ret = map_attrs_partition(module, ac,
990 &local_attrs, &remote_attrs, search_attrs);
991 if (ret == 0) {
992 ac->local_attrs = local_attrs;
993 ac->remote_attrs = remote_attrs;
994 ac->all_attrs = search_attrs;
996 return ret;
999 /* Create context for temporary memory */
1000 tmp_ctx = talloc_new(ac);
1001 if (tmp_ctx == NULL) {
1002 goto oom;
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);
1009 goto oom;
1011 tree_attrs[0] = NULL;
1013 /* Collect attributes from tree */
1014 ret = ldb_parse_tree_collect_attrs(module, tmp_ctx, &tree_attrs, tree);
1015 if (ret) {
1016 goto done;
1019 /* Merge attributes from search operation */
1020 ret = map_attrs_merge(module, tmp_ctx, &tree_attrs, search_attrs);
1021 if (ret) {
1022 goto done;
1025 /* Split local from remote attributes */
1026 ret = map_attrs_partition(module, ac, &local_attrs,
1027 &remote_attrs, tree_attrs);
1029 if (ret == 0) {
1030 ac->local_attrs = local_attrs;
1031 ac->remote_attrs = remote_attrs;
1032 talloc_steal(ac, tree_attrs);
1033 ac->all_attrs = tree_attrs;
1035 done:
1036 /* Free temporary memory */
1037 talloc_free(tmp_ctx);
1038 return ret;
1040 oom:
1041 map_oom(module);
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);
1060 if (mr == NULL) {
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;
1067 } else {
1068 /* first entry */
1069 ac->r_list = mr;
1071 ac->r_current = mr;
1073 return LDB_SUCCESS;
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;
1082 unsigned int i;
1083 int ret;
1084 bool matched;
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,
1093 &matched);
1094 if (ret != LDB_SUCCESS) return ret;
1095 if (!matched) {
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));
1100 return LDB_SUCCESS;
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;
1108 i = 0;
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);
1115 } else {
1116 i++;
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;
1132 int ret;
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);
1158 if (ac == NULL) {
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) {
1166 attrs = wildcard;
1169 /* Split local from remote attrs */
1170 ret = map_attrs_collect_and_partition(module, ac,
1171 attrs, req->op.search.tree);
1172 if (ret) {
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);
1180 if (ret) {
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 */
1187 local_tree = NULL;
1188 remote_tree = NULL;
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,
1216 remote_tree,
1217 ac->remote_attrs,
1218 req->controls,
1219 ac, map_remote_search_callback,
1220 req);
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;
1234 int ret;
1236 ac = talloc_get_type(req->context, struct map_context);
1238 if (!ares) {
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 */
1251 talloc_free(ares);
1252 return LDB_SUCCESS;
1254 case LDB_REPLY_ENTRY:
1256 /* Map result record into a local message */
1257 ret = map_reply_remote(ac, ares);
1258 if (ret) {
1259 talloc_free(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);
1269 } else {
1270 ret = map_save_entry(ac,ares);
1273 if (ret != LDB_SUCCESS) {
1274 talloc_free(ares);
1275 return ldb_module_done(ac->req, NULL, NULL, ret);
1277 break;
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);
1293 talloc_free(ares);
1294 return ret;
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);
1305 return LDB_SUCCESS;
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,
1320 NULL, NULL,
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;
1334 int ret;
1336 ac = talloc_get_type(req->context, struct map_context);
1337 ldb = ldb_module_get_ctx(ac->module);
1339 if (!ares) {
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) {
1352 talloc_free(ares);
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);
1361 break;
1363 case LDB_REPLY_REFERRAL:
1364 /* ignore referrals */
1365 talloc_free(ares);
1366 break;
1368 case LDB_REPLY_DONE:
1369 /* We don't need the local 'ares', but we will use the remote one from below */
1370 talloc_free(ares);
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);
1385 } else {
1386 ret = map_return_entry(ac, ac->r_current->remote);
1387 if (ret != LDB_SUCCESS) {
1388 return ldb_module_done(ac->req,
1389 NULL, NULL, ret);
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,
1399 NULL, NULL, ret);
1401 break;
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);
1413 return LDB_SUCCESS;