r19430: merge recent ldb changes from Samba4. This includes memory leak fixes
[Samba.git] / source / lib / ldb / modules / ldb_map.c
blobf9ae66a2aa5b0d1ada01ea8f041156bcdea5919c
1 /*
2 ldb database mapping module
4 Copyright (C) Jelmer Vernooij 2005
5 Copyright (C) Martin Kuehl <mkhl@samba.org> 2006
7 * NOTICE: this module is NOT released under the GNU LGPL license as
8 * other ldb code. This module is release under the GNU GPL v2 or
9 * later license.
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 /*
27 * Name: ldb
29 * Component: ldb ldb_map module
31 * Description: Map portions of data into a different format on a
32 * remote partition.
34 * Author: Jelmer Vernooij, Martin Kuehl
37 #include "includes.h"
38 #include "ldb/include/includes.h"
40 #include "ldb/modules/ldb_map.h"
41 #include "ldb/modules/ldb_map_private.h"
43 /* Description of the provided ldb requests:
44 - special attribute 'isMapped'
46 - search:
47 - if parse tree can be split
48 - search remote records w/ remote attrs and parse tree
49 - otherwise
50 - enumerate all remote records
51 - for each remote result
52 - map remote result to local message
53 - search local result
54 - is present
55 - merge local into remote result
56 - run callback on merged result
57 - otherwise
58 - run callback on remote result
60 - add:
61 - split message into local and remote part
62 - if local message is not empty
63 - add isMapped to local message
64 - add local message
65 - add remote message
67 - modify:
68 - split message into local and remote part
69 - if local message is not empty
70 - add isMapped to local message
71 - search for local record
72 - if present
73 - modify local record
74 - otherwise
75 - add local message
76 - modify remote record
78 - delete:
79 - search for local record
80 - if present
81 - delete local record
82 - delete remote record
84 - rename:
85 - search for local record
86 - if present
87 - rename local record
88 - modify local isMapped
89 - rename remote record
94 /* Private data structures
95 * ======================= */
97 /* Global private data */
98 /* Extract mappings from private data. */
99 const struct ldb_map_context *map_get_context(struct ldb_module *module)
101 const struct map_private *data = talloc_get_type(module->private_data, struct map_private);
102 return data->context;
105 /* Create a generic request context. */
106 static struct map_context *map_init_context(struct ldb_handle *h, struct ldb_request *req)
108 struct map_context *ac;
110 ac = talloc_zero(h, struct map_context);
111 if (ac == NULL) {
112 map_oom(h->module);
113 return NULL;
116 ac->module = h->module;
117 ac->orig_req = req;
119 return ac;
122 /* Create a search request context. */
123 struct map_search_context *map_init_search_context(struct map_context *ac, struct ldb_reply *ares)
125 struct map_search_context *sc;
127 sc = talloc_zero(ac, struct map_search_context);
128 if (sc == NULL) {
129 map_oom(ac->module);
130 return NULL;
133 sc->ac = ac;
134 sc->local_res = NULL;
135 sc->remote_res = ares;
137 return sc;
140 /* Create a request context and handle. */
141 struct ldb_handle *map_init_handle(struct ldb_request *req, struct ldb_module *module)
143 struct map_context *ac;
144 struct ldb_handle *h;
146 h = talloc_zero(req, struct ldb_handle);
147 if (h == NULL) {
148 map_oom(module);
149 return NULL;
152 h->module = module;
154 ac = map_init_context(h, req);
155 if (ac == NULL) {
156 talloc_free(h);
157 return NULL;
160 h->private_data = (void *)ac;
162 h->state = LDB_ASYNC_INIT;
163 h->status = LDB_SUCCESS;
165 return h;
169 /* Dealing with DNs for different partitions
170 * ========================================= */
172 /* Check whether any data should be stored in the local partition. */
173 BOOL map_check_local_db(struct ldb_module *module)
175 const struct ldb_map_context *data = map_get_context(module);
177 if (!data->remote_base_dn || !data->local_base_dn) {
178 return False;
181 return True;
184 /* WARK: verbatim copy from ldb_dn.c */
185 static struct ldb_dn_component ldb_dn_copy_component(void *mem_ctx, struct ldb_dn_component *src)
187 struct ldb_dn_component dst;
189 memset(&dst, 0, sizeof(dst));
191 if (src == NULL) {
192 return dst;
195 dst.value = ldb_val_dup(mem_ctx, &(src->value));
196 if (dst.value.data == NULL) {
197 return dst;
200 dst.name = talloc_strdup(mem_ctx, src->name);
201 if (dst.name == NULL) {
202 talloc_free(dst.value.data);
205 return dst;
208 /* Copy a DN but replace the old with the new base DN. */
209 static struct ldb_dn *ldb_dn_rebase(void *mem_ctx, const struct ldb_dn *old, const struct ldb_dn *old_base, const struct ldb_dn *new_base)
211 struct ldb_dn *new;
212 int i, offset;
214 /* Perhaps we don't need to rebase at all? */
215 if (!old_base || !new_base) {
216 return ldb_dn_copy(mem_ctx, old);
219 offset = old->comp_num - old_base->comp_num;
220 new = ldb_dn_copy_partial(mem_ctx, new_base, offset + new_base->comp_num);
221 for (i = 0; i < offset; i++) {
222 new->components[i] = ldb_dn_copy_component(new->components, &(old->components[i]));
225 return new;
228 /* Copy a DN with the base DN of the local partition. */
229 static struct ldb_dn *ldb_dn_rebase_local(void *mem_ctx, const struct ldb_map_context *data, const struct ldb_dn *dn)
231 return ldb_dn_rebase(mem_ctx, dn, data->remote_base_dn, data->local_base_dn);
234 /* Copy a DN with the base DN of the remote partition. */
235 static struct ldb_dn *ldb_dn_rebase_remote(void *mem_ctx, const struct ldb_map_context *data, const struct ldb_dn *dn)
237 return ldb_dn_rebase(mem_ctx, dn, data->local_base_dn, data->remote_base_dn);
240 /* Run a request and make sure it targets the remote partition. */
241 /* TODO: free old DNs and messages? */
242 int ldb_next_remote_request(struct ldb_module *module, struct ldb_request *request)
244 const struct ldb_map_context *data = map_get_context(module);
245 struct ldb_message *msg;
247 switch (request->operation) {
248 case LDB_SEARCH:
249 if (request->op.search.base) {
250 request->op.search.base = ldb_dn_rebase_remote(request, data, request->op.search.base);
251 } else {
252 request->op.search.base = data->remote_base_dn;
253 /* TODO: adjust scope? */
255 break;
257 case LDB_ADD:
258 msg = ldb_msg_copy_shallow(request, request->op.add.message);
259 msg->dn = ldb_dn_rebase_remote(msg, data, msg->dn);
260 request->op.add.message = msg;
261 break;
263 case LDB_MODIFY:
264 msg = ldb_msg_copy_shallow(request, request->op.mod.message);
265 msg->dn = ldb_dn_rebase_remote(msg, data, msg->dn);
266 request->op.mod.message = msg;
267 break;
269 case LDB_DELETE:
270 request->op.del.dn = ldb_dn_rebase_remote(request, data, request->op.del.dn);
271 break;
273 case LDB_RENAME:
274 request->op.rename.olddn = ldb_dn_rebase_remote(request, data, request->op.rename.olddn);
275 request->op.rename.newdn = ldb_dn_rebase_remote(request, data, request->op.rename.newdn);
276 break;
278 default:
279 ldb_debug(module->ldb, LDB_DEBUG_ERROR, "ldb_map: "
280 "Invalid remote request!\n");
281 return LDB_ERR_OPERATIONS_ERROR;
284 return ldb_next_request(module, request);
288 /* Finding mappings for attributes and objectClasses
289 * ================================================= */
291 /* Find an objectClass mapping by the local name. */
292 static const struct ldb_map_objectclass *map_objectclass_find_local(const struct ldb_map_context *data, const char *name)
294 int i;
296 for (i = 0; data->objectclass_maps && data->objectclass_maps[i].local_name; i++) {
297 if (ldb_attr_cmp(data->objectclass_maps[i].local_name, name) == 0) {
298 return &data->objectclass_maps[i];
302 return NULL;
305 /* Find an objectClass mapping by the remote name. */
306 static const struct ldb_map_objectclass *map_objectclass_find_remote(const struct ldb_map_context *data, const char *name)
308 int i;
310 for (i = 0; data->objectclass_maps && data->objectclass_maps[i].remote_name; i++) {
311 if (ldb_attr_cmp(data->objectclass_maps[i].remote_name, name) == 0) {
312 return &data->objectclass_maps[i];
316 return NULL;
319 /* Find an attribute mapping by the local name. */
320 const struct ldb_map_attribute *map_attr_find_local(const struct ldb_map_context *data, const char *name)
322 int i;
324 for (i = 0; data->attribute_maps[i].local_name; i++) {
325 if (ldb_attr_cmp(data->attribute_maps[i].local_name, name) == 0) {
326 return &data->attribute_maps[i];
329 for (i = 0; data->attribute_maps[i].local_name; i++) {
330 if (ldb_attr_cmp(data->attribute_maps[i].local_name, "*") == 0) {
331 return &data->attribute_maps[i];
335 return NULL;
338 /* Find an attribute mapping by the remote name. */
339 const struct ldb_map_attribute *map_attr_find_remote(const struct ldb_map_context *data, const char *name)
341 const struct ldb_map_attribute *map;
342 const struct ldb_map_attribute *wildcard = NULL;
343 int i, j;
345 for (i = 0; data->attribute_maps[i].local_name; i++) {
346 map = &data->attribute_maps[i];
347 if (ldb_attr_cmp(map->local_name, "*") == 0) {
348 wildcard = &data->attribute_maps[i];
351 switch (map->type) {
352 case MAP_IGNORE:
353 break;
355 case MAP_KEEP:
356 if (ldb_attr_cmp(map->local_name, name) == 0) {
357 return map;
359 break;
361 case MAP_RENAME:
362 case MAP_CONVERT:
363 if (ldb_attr_cmp(map->u.rename.remote_name, name) == 0) {
364 return map;
366 break;
368 case MAP_GENERATE:
369 for (j = 0; map->u.generate.remote_names && map->u.generate.remote_names[j]; j++) {
370 if (ldb_attr_cmp(map->u.generate.remote_names[j], name) == 0) {
371 return map;
374 break;
378 /* We didn't find it, so return the wildcard record if one was configured */
379 return wildcard;
383 /* Mapping attributes
384 * ================== */
386 /* Check whether an attribute will be mapped into the remote partition. */
387 BOOL map_attr_check_remote(const struct ldb_map_context *data, const char *attr)
389 const struct ldb_map_attribute *map = map_attr_find_local(data, attr);
391 if (map == NULL) {
392 return False;
394 if (map->type == MAP_IGNORE) {
395 return False;
398 return True;
401 /* Map an attribute name into the remote partition. */
402 const char *map_attr_map_local(void *mem_ctx, const struct ldb_map_attribute *map, const char *attr)
404 if (map == NULL) {
405 return talloc_strdup(mem_ctx, attr);
408 switch (map->type) {
409 case MAP_KEEP:
410 return talloc_strdup(mem_ctx, attr);
412 case MAP_RENAME:
413 case MAP_CONVERT:
414 return talloc_strdup(mem_ctx, map->u.rename.remote_name);
416 default:
417 return NULL;
421 /* Map an attribute name back into the local partition. */
422 const char *map_attr_map_remote(void *mem_ctx, const struct ldb_map_attribute *map, const char *attr)
424 if (map == NULL) {
425 return talloc_strdup(mem_ctx, attr);
428 if (map->type == MAP_KEEP) {
429 return talloc_strdup(mem_ctx, attr);
432 return talloc_strdup(mem_ctx, map->local_name);
436 /* Merge two lists of attributes into a single one. */
437 int map_attrs_merge(struct ldb_module *module, void *mem_ctx, const char ***attrs, const char * const *more_attrs)
439 int i, j, k;
441 for (i = 0; *attrs && (*attrs)[i]; i++) /* noop */ ;
442 for (j = 0; more_attrs && more_attrs[j]; j++) /* noop */ ;
444 *attrs = talloc_realloc(mem_ctx, *attrs, const char *, i+j+1);
445 if (*attrs == NULL) {
446 map_oom(module);
447 return -1;
450 for (k = 0; k < j; k++) {
451 (*attrs)[i+k] = more_attrs[k];
454 (*attrs)[i+k] = NULL;
456 return 0;
459 /* Mapping ldb values
460 * ================== */
462 /* Map an ldb value into the remote partition. */
463 struct ldb_val ldb_val_map_local(struct ldb_module *module, void *mem_ctx, const struct ldb_map_attribute *map, struct ldb_val val)
465 if (map && (map->type == MAP_CONVERT) && (map->u.convert.convert_local)) {
466 return map->u.convert.convert_local(module, mem_ctx, &val);
469 return ldb_val_dup(mem_ctx, &val);
472 /* Map an ldb value back into the local partition. */
473 struct ldb_val ldb_val_map_remote(struct ldb_module *module, void *mem_ctx, const struct ldb_map_attribute *map, struct ldb_val val)
475 if (map && (map->type == MAP_CONVERT) && (map->u.convert.convert_remote)) {
476 return map->u.convert.convert_remote(module, mem_ctx, &val);
479 return ldb_val_dup(mem_ctx, &val);
483 /* Mapping DNs
484 * =========== */
486 /* Check whether a DN is below the local baseDN. */
487 BOOL ldb_dn_check_local(struct ldb_module *module, const struct ldb_dn *dn)
489 const struct ldb_map_context *data = map_get_context(module);
491 if (!data->local_base_dn) {
492 return True;
495 return ldb_dn_compare_base(module->ldb, data->local_base_dn, dn) == 0;
498 /* Map a DN into the remote partition. */
499 struct ldb_dn *ldb_dn_map_local(struct ldb_module *module, void *mem_ctx, const struct ldb_dn *dn)
501 const struct ldb_map_context *data = map_get_context(module);
502 struct ldb_dn *newdn;
503 struct ldb_dn_component *old, *new;
504 const struct ldb_map_attribute *map;
505 enum ldb_map_attr_type map_type;
506 int i;
508 if (dn == NULL) {
509 return NULL;
512 newdn = ldb_dn_copy(mem_ctx, dn);
513 if (newdn == NULL) {
514 map_oom(module);
515 return NULL;
518 /* For each RDN, map the component name and possibly the value */
519 for (i = 0; i < newdn->comp_num; i++) {
520 old = &dn->components[i];
521 new = &newdn->components[i];
522 map = map_attr_find_local(data, old->name);
524 /* Unknown attribute - leave this RDN as is and hope the best... */
525 if (map == NULL) {
526 map_type = MAP_KEEP;
527 } else {
528 map_type = map->type;
531 switch (map_type) {
532 case MAP_IGNORE:
533 case MAP_GENERATE:
534 ldb_debug(module->ldb, LDB_DEBUG_ERROR, "ldb_map: "
535 "MAP_IGNORE/MAP_GENERATE attribute '%s' "
536 "used in DN!\n", old->name);
537 goto failed;
539 case MAP_CONVERT:
540 if (map->u.convert.convert_local == NULL) {
541 ldb_debug(module->ldb, LDB_DEBUG_ERROR, "ldb_map: "
542 "'convert_local' not set for attribute '%s' "
543 "used in DN!\n", old->name);
544 goto failed;
546 /* fall through */
547 case MAP_KEEP:
548 case MAP_RENAME:
549 new->name = discard_const_p(char, map_attr_map_local(newdn->components, map, old->name));
550 new->value = ldb_val_map_local(module, newdn->components, map, old->value);
551 break;
555 return newdn;
557 failed:
558 talloc_free(newdn);
559 return NULL;
562 /* Map a DN into the local partition. */
563 struct ldb_dn *ldb_dn_map_remote(struct ldb_module *module, void *mem_ctx, const struct ldb_dn *dn)
565 const struct ldb_map_context *data = map_get_context(module);
566 struct ldb_dn *newdn;
567 struct ldb_dn_component *old, *new;
568 const struct ldb_map_attribute *map;
569 enum ldb_map_attr_type map_type;
570 int i;
572 if (dn == NULL) {
573 return NULL;
576 newdn = ldb_dn_copy(mem_ctx, dn);
577 if (newdn == NULL) {
578 map_oom(module);
579 return NULL;
582 /* For each RDN, map the component name and possibly the value */
583 for (i = 0; i < newdn->comp_num; i++) {
584 old = &dn->components[i];
585 new = &newdn->components[i];
586 map = map_attr_find_remote(data, old->name);
588 /* Unknown attribute - leave this RDN as is and hope the best... */
589 if (map == NULL) {
590 map_type = MAP_KEEP;
591 } else {
592 map_type = map->type;
595 switch (map_type) {
596 case MAP_IGNORE:
597 case MAP_GENERATE:
598 ldb_debug(module->ldb, LDB_DEBUG_ERROR, "ldb_map: "
599 "MAP_IGNORE/MAP_GENERATE attribute '%s' "
600 "used in DN!\n", old->name);
601 goto failed;
603 case MAP_CONVERT:
604 if (map->u.convert.convert_remote == NULL) {
605 ldb_debug(module->ldb, LDB_DEBUG_ERROR, "ldb_map: "
606 "'convert_remote' not set for attribute '%s' "
607 "used in DN!\n", old->name);
608 goto failed;
610 /* fall through */
611 case MAP_KEEP:
612 case MAP_RENAME:
613 new->name = discard_const_p(char, map_attr_map_remote(newdn->components, map, old->name));
614 new->value = ldb_val_map_remote(module, newdn->components, map, old->value);
615 break;
619 return newdn;
621 failed:
622 talloc_free(newdn);
623 return NULL;
626 /* Map a DN and its base into the local partition. */
627 /* TODO: This should not be required with GUIDs. */
628 struct ldb_dn *ldb_dn_map_rebase_remote(struct ldb_module *module, void *mem_ctx, const struct ldb_dn *dn)
630 const struct ldb_map_context *data = map_get_context(module);
631 struct ldb_dn *dn1, *dn2;
633 dn1 = ldb_dn_rebase_local(mem_ctx, data, dn);
634 dn2 = ldb_dn_map_remote(module, mem_ctx, dn1);
636 talloc_free(dn1);
637 return dn2;
641 /* Converting DNs and objectClasses (as ldb values)
642 * ================================================ */
644 /* Map a DN contained in an ldb value into the remote partition. */
645 static struct ldb_val ldb_dn_convert_local(struct ldb_module *module, void *mem_ctx, const struct ldb_val *val)
647 struct ldb_dn *dn, *newdn;
648 struct ldb_val newval;
650 dn = ldb_dn_explode(mem_ctx, (char *)val->data);
651 newdn = ldb_dn_map_local(module, mem_ctx, dn);
652 talloc_free(dn);
654 newval.length = 0;
655 newval.data = (uint8_t *)ldb_dn_linearize(mem_ctx, newdn);
656 if (newval.data) {
657 newval.length = strlen((char *)newval.data);
659 talloc_free(newdn);
661 return newval;
664 /* Map a DN contained in an ldb value into the local partition. */
665 static struct ldb_val ldb_dn_convert_remote(struct ldb_module *module, void *mem_ctx, const struct ldb_val *val)
667 struct ldb_dn *dn, *newdn;
668 struct ldb_val newval;
670 dn = ldb_dn_explode(mem_ctx, (char *)val->data);
671 newdn = ldb_dn_map_remote(module, mem_ctx, dn);
672 talloc_free(dn);
674 newval.length = 0;
675 newval.data = (uint8_t *)ldb_dn_linearize(mem_ctx, newdn);
676 if (newval.data) {
677 newval.length = strlen((char *)newval.data);
679 talloc_free(newdn);
681 return newval;
684 /* Map an objectClass into the remote partition. */
685 static struct ldb_val map_objectclass_convert_local(struct ldb_module *module, void *mem_ctx, const struct ldb_val *val)
687 const struct ldb_map_context *data = map_get_context(module);
688 const char *name = (char *)val->data;
689 const struct ldb_map_objectclass *map = map_objectclass_find_local(data, name);
690 struct ldb_val newval;
692 if (map) {
693 newval.data = (uint8_t*)talloc_strdup(mem_ctx, map->remote_name);
694 newval.length = strlen((char *)newval.data);
695 return newval;
698 return ldb_val_dup(mem_ctx, val);
701 /* Generate a remote message with a mapped objectClass. */
702 static void map_objectclass_generate_remote(struct ldb_module *module, const char *local_attr, const struct ldb_message *old, struct ldb_message *remote, struct ldb_message *local)
704 struct ldb_message_element *el, *oc;
705 struct ldb_val val;
706 BOOL found_extensibleObject = False;
707 int i;
709 /* Find old local objectClass */
710 oc = ldb_msg_find_element(old, local_attr);
711 if (oc == NULL) {
712 return;
715 /* Prepare new element */
716 el = talloc_zero(remote, struct ldb_message_element);
717 if (el == NULL) {
718 ldb_oom(module->ldb);
719 return; /* TODO: fail? */
722 /* Copy local objectClass element, reverse space for an extra value */
723 el->num_values = oc->num_values + 1;
724 el->values = talloc_array(el, struct ldb_val, el->num_values);
725 if (el->values == NULL) {
726 talloc_free(el);
727 ldb_oom(module->ldb);
728 return; /* TODO: fail? */
731 /* Copy local element name "objectClass" */
732 el->name = talloc_strdup(el, local_attr);
734 /* Convert all local objectClasses */
735 for (i = 0; i < el->num_values - 1; i++) {
736 el->values[i] = map_objectclass_convert_local(module, el->values, &oc->values[i]);
737 if (ldb_attr_cmp((char *)el->values[i].data, "extensibleObject") == 0) {
738 found_extensibleObject = True;
742 if (!found_extensibleObject) {
743 val.data = (uint8_t *)talloc_strdup(el->values, "extensibleObject");
744 val.length = strlen((char *)val.data);
746 /* Append additional objectClass "extensibleObject" */
747 el->values[i] = val;
748 } else {
749 el->num_values--;
752 /* Add new objectClass to remote message */
753 ldb_msg_add(remote, el, 0);
756 /* Map an objectClass into the local partition. */
757 static struct ldb_val map_objectclass_convert_remote(struct ldb_module *module, void *mem_ctx, const struct ldb_val *val)
759 const struct ldb_map_context *data = map_get_context(module);
760 const char *name = (char *)val->data;
761 const struct ldb_map_objectclass *map = map_objectclass_find_remote(data, name);
762 struct ldb_val newval;
764 if (map) {
765 newval.data = (uint8_t*)talloc_strdup(mem_ctx, map->local_name);
766 newval.length = strlen((char *)newval.data);
767 return newval;
770 return ldb_val_dup(mem_ctx, val);
773 /* Generate a local message with a mapped objectClass. */
774 static struct ldb_message_element *map_objectclass_generate_local(struct ldb_module *module, void *mem_ctx, const char *remote_attr, const struct ldb_message *remote)
776 struct ldb_message_element *el, *oc;
777 struct ldb_val val;
778 int i;
780 /* Find old remote objectClass */
781 oc = ldb_msg_find_element(remote, remote_attr);
782 if (oc == NULL) {
783 return NULL;
786 /* Prepare new element */
787 el = talloc_zero(mem_ctx, struct ldb_message_element);
788 if (el == NULL) {
789 ldb_oom(module->ldb);
790 return NULL;
793 /* Copy remote objectClass element */
794 el->num_values = oc->num_values;
795 el->values = talloc_array(el, struct ldb_val, el->num_values);
796 if (el->values == NULL) {
797 talloc_free(el);
798 ldb_oom(module->ldb);
799 return NULL;
802 /* Copy remote element name "objectClass" */
803 el->name = talloc_strdup(el, remote_attr);
805 /* Convert all remote objectClasses */
806 for (i = 0; i < el->num_values; i++) {
807 el->values[i] = map_objectclass_convert_remote(module, el->values, &oc->values[i]);
810 val.data = (uint8_t *)talloc_strdup(el->values, "extensibleObject");
811 val.length = strlen((char *)val.data);
813 /* Remove last value if it was "extensibleObject" */
814 if (ldb_val_equal_exact(&val, &el->values[i-1])) {
815 el->num_values--;
816 el->values = talloc_realloc(el, el->values, struct ldb_val, el->num_values);
817 if (el->values == NULL) {
818 talloc_free(el);
819 ldb_oom(module->ldb);
820 return NULL;
824 return el;
827 /* Mappings for searches on objectClass= assuming a one-to-one
828 * mapping. Needed because this is a generate operator for the
829 * add/modify code */
830 static int map_objectclass_convert_operator(struct ldb_module *module, void *mem_ctx,
831 struct ldb_parse_tree **new, const struct ldb_parse_tree *tree)
834 static const struct ldb_map_attribute objectclass_map = {
835 .local_name = "objectClass",
836 .type = MAP_CONVERT,
837 .u = {
838 .convert = {
839 .remote_name = "objectClass",
840 .convert_local = map_objectclass_convert_local,
841 .convert_remote = map_objectclass_convert_remote,
846 return map_subtree_collect_remote_simple(module, mem_ctx, new, tree, &objectclass_map);
849 /* Auxiliary request construction
850 * ============================== */
852 /* Store the DN of a single search result in context. */
853 static int map_search_self_callback(struct ldb_context *ldb, void *context, struct ldb_reply *ares)
855 struct map_context *ac;
857 if (context == NULL || ares == NULL) {
858 ldb_set_errstring(ldb, talloc_asprintf(ldb, "NULL Context or Result in callback"));
859 return LDB_ERR_OPERATIONS_ERROR;
862 ac = talloc_get_type(context, struct map_context);
864 /* We are interested only in the single reply */
865 if (ares->type != LDB_REPLY_ENTRY) {
866 talloc_free(ares);
867 return LDB_SUCCESS;
870 /* We have already found a remote DN */
871 if (ac->local_dn) {
872 ldb_set_errstring(ldb, talloc_asprintf(ldb, "Too many results to base search"));
873 talloc_free(ares);
874 return LDB_ERR_OPERATIONS_ERROR;
877 /* Store local DN */
878 ac->local_dn = ares->message->dn;
880 return LDB_SUCCESS;
883 /* Build a request to search a record by its DN. */
884 struct ldb_request *map_search_base_req(struct map_context *ac, const struct ldb_dn *dn, const char * const *attrs, const struct ldb_parse_tree *tree, void *context, ldb_search_callback callback)
886 struct ldb_request *req;
888 req = talloc_zero(ac, struct ldb_request);
889 if (req == NULL) {
890 map_oom(ac->module);
891 return NULL;
894 req->operation = LDB_SEARCH;
895 req->op.search.base = dn;
896 req->op.search.scope = LDB_SCOPE_BASE;
897 req->op.search.attrs = attrs;
899 if (tree) {
900 req->op.search.tree = tree;
901 } else {
902 req->op.search.tree = ldb_parse_tree(req, NULL);
903 if (req->op.search.tree == NULL) {
904 talloc_free(req);
905 return NULL;
909 req->controls = NULL;
910 req->context = context;
911 req->callback = callback;
912 ldb_set_timeout_from_prev_req(ac->module->ldb, ac->orig_req, req);
914 return req;
917 /* Build a request to search the local record by its DN. */
918 struct ldb_request *map_search_self_req(struct map_context *ac, const struct ldb_dn *dn)
920 /* attrs[] is returned from this function in
921 * ac->search_req->op.search.attrs, so it must be static, as
922 * otherwise the compiler can put it on the stack */
923 static const char * const attrs[] = { IS_MAPPED, NULL };
924 struct ldb_parse_tree *tree;
926 /* Limit search to records with 'IS_MAPPED' present */
927 /* TODO: `tree = ldb_parse_tree(ac, IS_MAPPED);' won't do. */
928 tree = talloc_zero(ac, struct ldb_parse_tree);
929 if (tree == NULL) {
930 map_oom(ac->module);
931 return NULL;
934 tree->operation = LDB_OP_PRESENT;
935 tree->u.present.attr = talloc_strdup(tree, IS_MAPPED);
937 return map_search_base_req(ac, dn, attrs, tree, ac, map_search_self_callback);
940 /* Build a request to update the 'IS_MAPPED' attribute */
941 struct ldb_request *map_build_fixup_req(struct map_context *ac, const struct ldb_dn *olddn, const struct ldb_dn *newdn)
943 struct ldb_request *req;
944 struct ldb_message *msg;
945 const char *dn;
947 /* Prepare request */
948 req = talloc_zero(ac, struct ldb_request);
949 if (req == NULL) {
950 map_oom(ac->module);
951 return NULL;
954 /* Prepare message */
955 msg = ldb_msg_new(req);
956 if (msg == NULL) {
957 map_oom(ac->module);
958 goto failed;
961 /* Update local 'IS_MAPPED' to the new remote DN */
962 msg->dn = discard_const_p(struct ldb_dn, olddn);
963 dn = ldb_dn_linearize(msg, newdn);
964 if (dn == NULL) {
965 goto failed;
967 if (ldb_msg_add_empty(msg, IS_MAPPED, LDB_FLAG_MOD_REPLACE) != 0) {
968 goto failed;
970 if (ldb_msg_add_string(msg, IS_MAPPED, dn) != 0) {
971 goto failed;
974 req->operation = LDB_MODIFY;
975 req->op.mod.message = msg;
976 req->controls = NULL;
977 req->handle = NULL;
978 req->context = NULL;
979 req->callback = NULL;
981 return req;
983 failed:
984 talloc_free(req);
985 return NULL;
989 /* Asynchronous call structure
990 * =========================== */
992 /* Figure out which request is currently pending. */
993 static struct ldb_request *map_get_req(struct map_context *ac)
995 switch (ac->step) {
996 case MAP_SEARCH_SELF_MODIFY:
997 case MAP_SEARCH_SELF_DELETE:
998 case MAP_SEARCH_SELF_RENAME:
999 return ac->search_req;
1001 case MAP_ADD_REMOTE:
1002 case MAP_MODIFY_REMOTE:
1003 case MAP_DELETE_REMOTE:
1004 case MAP_RENAME_REMOTE:
1005 return ac->remote_req;
1007 case MAP_RENAME_FIXUP:
1008 return ac->down_req;
1010 case MAP_ADD_LOCAL:
1011 case MAP_MODIFY_LOCAL:
1012 case MAP_DELETE_LOCAL:
1013 case MAP_RENAME_LOCAL:
1014 return ac->local_req;
1016 case MAP_SEARCH_REMOTE:
1017 /* Can't happen */
1018 break;
1021 return NULL; /* unreachable; silences a warning */
1024 typedef int (*map_next_function)(struct ldb_handle *handle);
1026 /* Figure out the next request to run. */
1027 static map_next_function map_get_next(struct map_context *ac)
1029 switch (ac->step) {
1030 case MAP_SEARCH_REMOTE:
1031 return NULL;
1033 case MAP_ADD_LOCAL:
1034 return map_add_do_remote;
1035 case MAP_ADD_REMOTE:
1036 return NULL;
1038 case MAP_SEARCH_SELF_MODIFY:
1039 return map_modify_do_local;
1040 case MAP_MODIFY_LOCAL:
1041 return map_modify_do_remote;
1042 case MAP_MODIFY_REMOTE:
1043 return NULL;
1045 case MAP_SEARCH_SELF_DELETE:
1046 return map_delete_do_local;
1047 case MAP_DELETE_LOCAL:
1048 return map_delete_do_remote;
1049 case MAP_DELETE_REMOTE:
1050 return NULL;
1052 case MAP_SEARCH_SELF_RENAME:
1053 return map_rename_do_local;
1054 case MAP_RENAME_LOCAL:
1055 return map_rename_do_fixup;
1056 case MAP_RENAME_FIXUP:
1057 return map_rename_do_remote;
1058 case MAP_RENAME_REMOTE:
1059 return NULL;
1062 return NULL; /* unreachable; silences a warning */
1065 /* Wait for the current pending request to finish and continue with the next. */
1066 static int map_wait_next(struct ldb_handle *handle)
1068 struct map_context *ac;
1069 struct ldb_request *req;
1070 map_next_function next;
1071 int ret;
1073 if (handle == NULL || handle->private_data == NULL) {
1074 return LDB_ERR_OPERATIONS_ERROR;
1077 if (handle->state == LDB_ASYNC_DONE) {
1078 return handle->status;
1081 handle->state = LDB_ASYNC_PENDING;
1082 handle->status = LDB_SUCCESS;
1084 ac = talloc_get_type(handle->private_data, struct map_context);
1086 if (ac->step == MAP_SEARCH_REMOTE) {
1087 int i;
1088 for (i = 0; i < ac->num_searches; i++) {
1089 req = ac->search_reqs[i];
1090 ret = ldb_wait(req->handle, LDB_WAIT_NONE);
1092 if (ret != LDB_SUCCESS) {
1093 handle->status = ret;
1094 goto done;
1096 if (req->handle->status != LDB_SUCCESS) {
1097 handle->status = req->handle->status;
1098 goto done;
1100 if (req->handle->state != LDB_ASYNC_DONE) {
1101 return LDB_SUCCESS;
1104 } else {
1106 req = map_get_req(ac);
1108 ret = ldb_wait(req->handle, LDB_WAIT_NONE);
1110 if (ret != LDB_SUCCESS) {
1111 handle->status = ret;
1112 goto done;
1114 if (req->handle->status != LDB_SUCCESS) {
1115 handle->status = req->handle->status;
1116 goto done;
1118 if (req->handle->state != LDB_ASYNC_DONE) {
1119 return LDB_SUCCESS;
1122 next = map_get_next(ac);
1123 if (next) {
1124 return next(handle);
1128 ret = LDB_SUCCESS;
1130 done:
1131 handle->state = LDB_ASYNC_DONE;
1132 return ret;
1135 /* Wait for all current pending requests to finish. */
1136 static int map_wait_all(struct ldb_handle *handle)
1138 int ret;
1140 while (handle->state != LDB_ASYNC_DONE) {
1141 ret = map_wait_next(handle);
1142 if (ret != LDB_SUCCESS) {
1143 return ret;
1147 return handle->status;
1150 /* Wait for pending requests to finish. */
1151 static int map_wait(struct ldb_handle *handle, enum ldb_wait_type type)
1153 if (type == LDB_WAIT_ALL) {
1154 return map_wait_all(handle);
1155 } else {
1156 return map_wait_next(handle);
1161 /* Module initialization
1162 * ===================== */
1164 /* Provided module operations */
1165 static const struct ldb_module_ops map_ops = {
1166 .name = "ldb_map",
1167 .add = map_add,
1168 .modify = map_modify,
1169 .del = map_delete,
1170 .rename = map_rename,
1171 .search = map_search,
1172 .wait = map_wait,
1175 /* Builtin mappings for DNs and objectClasses */
1176 static const struct ldb_map_attribute builtin_attribute_maps[] = {
1178 .local_name = "dn",
1179 .type = MAP_CONVERT,
1180 .u = {
1181 .convert = {
1182 .remote_name = "dn",
1183 .convert_local = ldb_dn_convert_local,
1184 .convert_remote = ldb_dn_convert_remote,
1189 .local_name = "objectClass",
1190 .type = MAP_GENERATE,
1191 .convert_operator = map_objectclass_convert_operator,
1192 .u = {
1193 .generate = {
1194 .remote_names = { "objectClass", NULL },
1195 .generate_local = map_objectclass_generate_local,
1196 .generate_remote = map_objectclass_generate_remote,
1201 .local_name = NULL,
1205 /* Find the special 'MAP_DN_NAME' record and store local and remote
1206 * base DNs in private data. */
1207 static int map_init_dns(struct ldb_module *module, struct ldb_map_context *data, const char *name)
1209 static const char * const attrs[] = { MAP_DN_FROM, MAP_DN_TO, NULL };
1210 struct ldb_dn *dn;
1211 struct ldb_message *msg;
1212 struct ldb_result *res;
1213 int ret;
1215 if (!name) {
1216 data->local_base_dn = NULL;
1217 data->remote_base_dn = NULL;
1218 return LDB_SUCCESS;
1221 dn = ldb_dn_string_compose(data, NULL, "%s=%s", MAP_DN_NAME, name);
1222 if (dn == NULL) {
1223 ldb_debug(module->ldb, LDB_DEBUG_ERROR, "ldb_map: "
1224 "Failed to construct '%s' DN!\n", MAP_DN_NAME);
1225 return LDB_ERR_OPERATIONS_ERROR;
1228 ret = ldb_search(module->ldb, dn, LDB_SCOPE_BASE, NULL, attrs, &res);
1229 talloc_free(dn);
1230 if (ret != LDB_SUCCESS) {
1231 return ret;
1233 if (res->count == 0) {
1234 ldb_debug(module->ldb, LDB_DEBUG_ERROR, "ldb_map: "
1235 "No results for '%s=%s'!\n", MAP_DN_NAME, name);
1236 talloc_free(res);
1237 return LDB_ERR_CONSTRAINT_VIOLATION;
1239 if (res->count > 1) {
1240 ldb_debug(module->ldb, LDB_DEBUG_ERROR, "ldb_map: "
1241 "Too many results for '%s=%s'!\n", MAP_DN_NAME, name);
1242 talloc_free(res);
1243 return LDB_ERR_CONSTRAINT_VIOLATION;
1246 msg = res->msgs[0];
1247 data->local_base_dn = ldb_msg_find_attr_as_dn(data, msg, MAP_DN_FROM);
1248 data->remote_base_dn = ldb_msg_find_attr_as_dn(data, msg, MAP_DN_TO);
1249 talloc_free(res);
1251 return LDB_SUCCESS;
1254 /* Store attribute maps and objectClass maps in private data. */
1255 static int map_init_maps(struct ldb_module *module, struct ldb_map_context *data,
1256 const struct ldb_map_attribute *attrs,
1257 const struct ldb_map_objectclass *ocls,
1258 const char * const *wildcard_attributes)
1260 int i, j, last;
1261 last = 0;
1263 /* Count specified attribute maps */
1264 for (i = 0; attrs[i].local_name; i++) /* noop */ ;
1265 /* Count built-in attribute maps */
1266 for (j = 0; builtin_attribute_maps[j].local_name; j++) /* noop */ ;
1268 /* Store list of attribute maps */
1269 data->attribute_maps = talloc_array(data, struct ldb_map_attribute, i+j+1);
1270 if (data->attribute_maps == NULL) {
1271 map_oom(module);
1272 return LDB_ERR_OPERATIONS_ERROR;
1275 /* Specified ones go first */
1276 for (i = 0; attrs[i].local_name; i++) {
1277 data->attribute_maps[last] = attrs[i];
1278 last++;
1281 /* Built-in ones go last */
1282 for (i = 0; builtin_attribute_maps[i].local_name; i++) {
1283 data->attribute_maps[last] = builtin_attribute_maps[i];
1284 last++;
1287 /* Ensure 'local_name == NULL' for the last entry */
1288 memset(&data->attribute_maps[last], 0, sizeof(struct ldb_map_attribute));
1290 /* Store list of objectClass maps */
1291 data->objectclass_maps = ocls;
1293 data->wildcard_attributes = wildcard_attributes;
1295 return LDB_SUCCESS;
1298 /* Copy the list of provided module operations. */
1299 struct ldb_module_ops ldb_map_get_ops(void)
1301 return map_ops;
1304 /* Initialize global private data. */
1305 int ldb_map_init(struct ldb_module *module, const struct ldb_map_attribute *attrs,
1306 const struct ldb_map_objectclass *ocls,
1307 const char * const *wildcard_attributes,
1308 const char *name)
1310 struct map_private *data;
1311 int ret;
1313 /* Prepare private data */
1314 data = talloc_zero(module, struct map_private);
1315 if (data == NULL) {
1316 map_oom(module);
1317 return LDB_ERR_OPERATIONS_ERROR;
1320 module->private_data = data;
1322 data->context = talloc_zero(data, struct ldb_map_context);
1323 if (!data->context) {
1324 map_oom(module);
1325 return LDB_ERR_OPERATIONS_ERROR;
1328 /* Store local and remote baseDNs */
1329 ret = map_init_dns(module, data->context, name);
1330 if (ret != LDB_SUCCESS) {
1331 talloc_free(data);
1332 return ret;
1335 /* Store list of attribute and objectClass maps */
1336 ret = map_init_maps(module, data->context, attrs, ocls, wildcard_attributes);
1337 if (ret != LDB_SUCCESS) {
1338 talloc_free(data);
1339 return ret;
1342 return LDB_SUCCESS;
1345 /* Usage note for initialization of this module:
1347 * ldb_map is meant to be used from a different module that sets up
1348 * the mappings and gets registered in ldb.
1350 * 'ldb_map_init' initializes the private data of this module and
1351 * stores the attribute and objectClass maps in there. It also looks
1352 * up the '@MAP' special DN so requests can be redirected to the
1353 * remote partition.
1355 * This function should be called from the 'init_context' op of the
1356 * module using ldb_map.
1358 * 'ldb_map_get_ops' returns a copy of ldb_maps module operations.
1360 * It should be called from the initialize function of the using
1361 * module, which should then override the 'init_context' op with a
1362 * function making the appropriate calls to 'ldb_map_init'.