smbd: ?True:False is pretty pointless :-)
[Samba.git] / lib / ldb / ldb_map / ldb_map_outbound.c
blob1ee2dfe12de912f6fd64ceefad0429b65b23ac99
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 int 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;
333 /* fall through */
334 case LDB_MAP_KEEP:
335 case LDB_MAP_RENAME:
336 case LDB_MAP_RENDROP:
337 old = ldb_msg_find_element(remote, remote_name);
338 if (old) {
339 el = ldb_msg_el_map_remote(module, local, map, attr_name, old);
340 } else {
341 return LDB_ERR_NO_SUCH_ATTRIBUTE;
343 break;
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",
350 attr_name);
351 return LDB_SUCCESS;
354 el = map->u.generate.generate_local(module, local, attr_name, remote);
355 if (!el) {
356 /* Generation failure is probably due to lack of source attributes */
357 return LDB_ERR_NO_SUCH_ATTRIBUTE;
359 break;
362 if (el == NULL) {
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;
376 unsigned int i;
377 int ret;
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]);
386 if (el == NULL) {
387 return LDB_ERR_OPERATIONS_ERROR;
390 ret = ldb_msg_replace(local, el);
391 if (ret) {
392 return ret;
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) {
402 continue;
403 } else if (ret) {
404 return ret;
405 } else {
406 continue;
410 return LDB_SUCCESS;
413 /* Mapping messages
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)
419 unsigned int i;
420 int ret;
422 for (i = 0; i < msg2->num_elements; i++) {
423 ret = ldb_msg_replace(msg1, &msg2->elements[i]);
424 if (ret) {
425 return ret;
429 return LDB_SUCCESS;
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)
436 unsigned int i;
437 int ret;
438 const char * const *attrs = ac->all_attrs;
439 if (!attrs) {
440 ret = ldb_msg_el_merge_wildcard(ac->module, local, remote);
441 if (ret) {
442 return ret;
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);
449 if (ret) {
450 return ret;
452 break;
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,
461 attrs[i]);
462 if (ret == LDB_ERR_NO_SUCH_ATTRIBUTE) {
463 } else if (ret) {
464 return ret;
468 return LDB_SUCCESS;
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;
478 struct ldb_dn *dn;
479 int ret;
481 /* There is no result message, skip */
482 if (ares->type != LDB_REPLY_ENTRY) {
483 return 0;
486 /* Create a new result message */
487 msg = ldb_msg_new(ares);
488 if (msg == NULL) {
489 map_oom(ac->module);
490 return LDB_ERR_OPERATIONS_ERROR;
493 /* Merge remote message into new message */
494 ret = ldb_msg_merge_remote(ac, msg, ares->message);
495 if (ret) {
496 talloc_free(msg);
497 return ret;
500 /* Create corresponding local DN */
501 dn = ldb_dn_map_rebase_remote(ac->module, msg, ares->message->dn);
502 if (dn == NULL) {
503 talloc_free(msg);
504 return LDB_ERR_OPERATIONS_ERROR;
506 msg->dn = dn;
508 /* Store new message with new DN as the result */
509 talloc_free(ares->message);
510 ares->message = msg;
512 return 0;
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;
522 bool negate = false;
524 while (subtree) {
525 switch (subtree->operation) {
526 case LDB_OP_NOT:
527 negate = !negate;
528 subtree = subtree->u.isnot.child;
529 continue;
531 case LDB_OP_AND:
532 return !negate; /* if negate: False */
534 case LDB_OP_OR:
535 return negate; /* if negate: True */
537 default:
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;
549 unsigned int i;
550 int ret;
552 if (tree == NULL) {
553 return 0;
556 switch (tree->operation) {
557 case LDB_OP_OR:
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]);
562 if (ret) {
563 return ret;
566 return 0;
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);
573 talloc_free(*attrs);
574 *attrs = new_attrs;
575 return 0;
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;
585 int ret;
587 /* Prepare new tree */
588 *new = talloc_memdup(mem_ctx, tree, sizeof(struct ldb_parse_tree));
589 if (*new == NULL) {
590 map_oom(module);
591 return LDB_ERR_OPERATIONS_ERROR;
594 /* Generate new subtree */
595 ret = map_subtree_select_local(module, *new, &child, tree->u.isnot.child);
596 if (ret) {
597 talloc_free(*new);
598 return ret;
601 /* Prune tree without subtree */
602 if (child == NULL) {
603 talloc_free(*new);
604 *new = NULL;
605 return 0;
608 (*new)->u.isnot.child = child;
610 return ret;
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)
616 unsigned int i, j;
617 int ret=0;
619 /* Prepare new tree */
620 *new = talloc_memdup(mem_ctx, tree, sizeof(struct ldb_parse_tree));
621 if (*new == NULL) {
622 map_oom(module);
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) {
630 map_oom(module);
631 talloc_free(*new);
632 return LDB_ERR_OPERATIONS_ERROR;
635 /* Generate new list of subtrees */
636 j = 0;
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]);
640 if (ret) {
641 talloc_free(*new);
642 return ret;
645 if (child) {
646 (*new)->u.list.elements[j] = child;
647 j++;
651 /* Prune tree without subtrees */
652 if (j == 0) {
653 talloc_free(*new);
654 *new = NULL;
655 return 0;
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);
662 return ret;
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));
670 if (*new == NULL) {
671 map_oom(module);
672 return LDB_ERR_OPERATIONS_ERROR;
675 return 0;
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);
683 if (tree == NULL) {
684 return 0;
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)) {
696 *new = NULL;
697 return 0;
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;
709 int ret;
711 /* Prepare new tree */
712 *new = talloc_memdup(mem_ctx, tree, sizeof(struct ldb_parse_tree));
713 if (*new == NULL) {
714 map_oom(module);
715 return LDB_ERR_OPERATIONS_ERROR;
718 /* Generate new subtree */
719 ret = map_subtree_collect_remote(module, *new, &child, tree->u.isnot.child);
720 if (ret) {
721 talloc_free(*new);
722 return ret;
725 /* Prune tree without subtree */
726 if (child == NULL) {
727 talloc_free(*new);
728 *new = NULL;
729 return 0;
732 (*new)->u.isnot.child = child;
734 return ret;
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)
740 unsigned int i, j;
741 int ret=0;
743 /* Prepare new tree */
744 *new = talloc_memdup(mem_ctx, tree, sizeof(struct ldb_parse_tree));
745 if (*new == NULL) {
746 map_oom(module);
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) {
754 map_oom(module);
755 talloc_free(*new);
756 return LDB_ERR_OPERATIONS_ERROR;
759 /* Generate new list of subtrees */
760 j = 0;
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]);
764 if (ret) {
765 talloc_free(*new);
766 return ret;
769 if (child) {
770 (*new)->u.list.elements[j] = child;
771 j++;
775 /* Prune tree without subtrees */
776 if (j == 0) {
777 talloc_free(*new);
778 *new = NULL;
779 return 0;
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);
786 return ret;
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)
792 const char *attr;
794 /* Prepare new tree */
795 *new = talloc(mem_ctx, struct ldb_parse_tree);
796 if (*new == NULL) {
797 map_oom(module);
798 return LDB_ERR_OPERATIONS_ERROR;
800 **new = *tree;
802 if (map->type == LDB_MAP_KEEP) {
803 /* Nothing to do here */
804 return 0;
807 /* Store attribute and value in new tree */
808 switch (tree->operation) {
809 case LDB_OP_PRESENT:
810 attr = map_attr_map_local(*new, map, tree->u.present.attr);
811 (*new)->u.present.attr = attr;
812 break;
813 case LDB_OP_SUBSTRING:
815 attr = map_attr_map_local(*new, map, tree->u.substring.attr);
816 (*new)->u.substring.attr = attr;
817 break;
819 case LDB_OP_EQUALITY:
820 attr = map_attr_map_local(*new, map, tree->u.equality.attr);
821 (*new)->u.equality.attr = attr;
822 break;
823 case LDB_OP_LESS:
824 case LDB_OP_GREATER:
825 case LDB_OP_APPROX:
826 attr = map_attr_map_local(*new, map, tree->u.comparison.attr);
827 (*new)->u.comparison.attr = attr;
828 break;
829 case LDB_OP_EXTENDED:
830 attr = map_attr_map_local(*new, map, tree->u.extended.attr);
831 (*new)->u.extended.attr = attr;
832 break;
833 default: /* unknown kind of simple subtree */
834 talloc_free(*new);
835 return LDB_ERR_OPERATIONS_ERROR;
838 if (attr == NULL) {
839 talloc_free(*new);
840 *new = NULL;
841 return 0;
844 if (map->type == LDB_MAP_RENAME || map->type == LDB_MAP_RENDROP) {
845 /* Nothing more to do here, the attribute has been renamed */
846 return 0;
849 /* Store attribute and value in new tree */
850 switch (tree->operation) {
851 case LDB_OP_PRESENT:
852 break;
853 case LDB_OP_SUBSTRING:
855 int i;
856 /* Map value */
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) {
861 talloc_free(*new);
862 *new = NULL;
863 return 0;
865 (*new)->u.substring.chunks[i] = talloc(*new, struct ldb_val);
866 if (!(*new)->u.substring.chunks[i]) {
867 talloc_free(*new);
868 *new = NULL;
869 return 0;
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;
874 break;
876 case LDB_OP_EQUALITY:
877 (*new)->u.equality.value = ldb_val_map_local(module, *new, map, &tree->u.equality.value);
878 break;
879 case LDB_OP_LESS:
880 case LDB_OP_GREATER:
881 case LDB_OP_APPROX:
882 (*new)->u.comparison.value = ldb_val_map_local(module, *new, map, &tree->u.comparison.value);
883 break;
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);
887 break;
888 default: /* unknown kind of simple subtree */
889 talloc_free(*new);
890 return LDB_ERR_OPERATIONS_ERROR;
893 return 0;
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);
905 if (tree == NULL) {
906 return 0;
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)) {
918 *new = NULL;
919 return 0;
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);
932 *new = NULL;
933 return 0;
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,
942 void *mem_ctx,
943 struct ldb_parse_tree **local_tree,
944 struct ldb_parse_tree **remote_tree,
945 const struct ldb_parse_tree *tree)
947 int ret;
949 *local_tree = NULL;
950 *remote_tree = NULL;
952 /* No original tree */
953 if (tree == NULL) {
954 return 0;
957 /* Generate local tree */
958 ret = map_subtree_select_local(module, mem_ctx, local_tree, tree);
959 if (ret) {
960 return ret;
963 /* Generate remote tree */
964 ret = map_subtree_collect_remote(module, mem_ctx, remote_tree, tree);
965 if (ret) {
966 talloc_free(*local_tree);
967 return ret;
970 return 0;
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)
980 void *tmp_ctx;
981 const char **tree_attrs;
982 const char **remote_attrs;
983 const char **local_attrs;
984 int ret;
986 /* There is no tree, just partition the searched attributes */
987 if (tree == NULL) {
988 ret = map_attrs_partition(module, ac,
989 &local_attrs, &remote_attrs, search_attrs);
990 if (ret == 0) {
991 ac->local_attrs = local_attrs;
992 ac->remote_attrs = remote_attrs;
993 ac->all_attrs = search_attrs;
995 return ret;
998 /* Create context for temporary memory */
999 tmp_ctx = talloc_new(ac);
1000 if (tmp_ctx == NULL) {
1001 goto oom;
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);
1008 goto oom;
1010 tree_attrs[0] = NULL;
1012 /* Collect attributes from tree */
1013 ret = ldb_parse_tree_collect_attrs(module, tmp_ctx, &tree_attrs, tree);
1014 if (ret) {
1015 goto done;
1018 /* Merge attributes from search operation */
1019 ret = map_attrs_merge(module, tmp_ctx, &tree_attrs, search_attrs);
1020 if (ret) {
1021 goto done;
1024 /* Split local from remote attributes */
1025 ret = map_attrs_partition(module, ac, &local_attrs,
1026 &remote_attrs, tree_attrs);
1028 if (ret == 0) {
1029 ac->local_attrs = local_attrs;
1030 ac->remote_attrs = remote_attrs;
1031 talloc_steal(ac, tree_attrs);
1032 ac->all_attrs = tree_attrs;
1034 done:
1035 /* Free temporary memory */
1036 talloc_free(tmp_ctx);
1037 return ret;
1039 oom:
1040 map_oom(module);
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);
1059 if (mr == NULL) {
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;
1066 } else {
1067 /* first entry */
1068 ac->r_list = mr;
1070 ac->r_current = mr;
1072 return LDB_SUCCESS;
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;
1081 unsigned int i;
1082 int ret;
1083 bool matched;
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,
1092 &matched);
1093 if (ret != LDB_SUCCESS) return ret;
1094 if (!matched) {
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));
1099 return LDB_SUCCESS;
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;
1107 i = 0;
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);
1114 } else {
1115 i++;
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;
1131 int ret;
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);
1157 if (ac == NULL) {
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) {
1165 attrs = wildcard;
1168 /* Split local from remote attrs */
1169 ret = map_attrs_collect_and_partition(module, ac,
1170 attrs, req->op.search.tree);
1171 if (ret) {
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);
1179 if (ret) {
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 */
1186 local_tree = NULL;
1187 remote_tree = NULL;
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,
1215 remote_tree,
1216 ac->remote_attrs,
1217 req->controls,
1218 ac, map_remote_search_callback,
1219 req);
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;
1233 int ret;
1235 ac = talloc_get_type(req->context, struct map_context);
1237 if (!ares) {
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 */
1250 talloc_free(ares);
1251 return LDB_SUCCESS;
1253 case LDB_REPLY_ENTRY:
1255 /* Map result record into a local message */
1256 ret = map_reply_remote(ac, ares);
1257 if (ret) {
1258 talloc_free(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);
1268 } else {
1269 ret = map_save_entry(ac,ares);
1272 if (ret != LDB_SUCCESS) {
1273 talloc_free(ares);
1274 return ldb_module_done(ac->req, NULL, NULL, ret);
1276 break;
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);
1292 talloc_free(ares);
1293 return ret;
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);
1304 return LDB_SUCCESS;
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,
1319 NULL, NULL,
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;
1333 int ret;
1335 ac = talloc_get_type(req->context, struct map_context);
1336 ldb = ldb_module_get_ctx(ac->module);
1338 if (!ares) {
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) {
1351 talloc_free(ares);
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);
1360 break;
1362 case LDB_REPLY_REFERRAL:
1363 /* ignore referrals */
1364 talloc_free(ares);
1365 break;
1367 case LDB_REPLY_DONE:
1368 /* We don't need the local 'ares', but we will use the remote one from below */
1369 talloc_free(ares);
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);
1384 } else {
1385 ret = map_return_entry(ac, ac->r_current->remote);
1386 if (ret != LDB_SUCCESS) {
1387 return ldb_module_done(ac->req,
1388 NULL, NULL, ret);
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,
1398 NULL, NULL, ret);
1400 break;
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);
1412 return LDB_SUCCESS;