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
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.
29 * Component: ldb ldb_map module
31 * Description: Map portions of data into a different format on a
34 * Author: Jelmer Vernooij, Martin Kuehl
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'
47 - if parse tree can be split
48 - search remote records w/ remote attrs and parse tree
50 - enumerate all remote records
51 - for each remote result
52 - map remote result to local message
55 - merge local into remote result
56 - run callback on merged result
58 - run callback on remote result
61 - split message into local and remote part
62 - if local message is not empty
63 - add isMapped to local message
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
76 - modify remote record
79 - search for local record
82 - delete remote record
85 - search for 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);
116 ac->module = h->module;
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);
134 sc->local_res = NULL;
135 sc->remote_res = ares;
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);
154 ac = map_init_context(h, req);
160 h->private_data = (void *)ac;
162 h->state = LDB_ASYNC_INIT;
163 h->status = LDB_SUCCESS;
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) {
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));
195 dst.value = ldb_val_dup(mem_ctx, &(src->value));
196 if (dst.value.data == NULL) {
200 dst.name = talloc_strdup(mem_ctx, src->name);
201 if (dst.name == NULL) {
202 talloc_free(dst.value.data);
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)
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]));
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) {
249 if (request->op.search.base) {
250 request->op.search.base = ldb_dn_rebase_remote(request, data, request->op.search.base);
252 request->op.search.base = data->remote_base_dn;
253 /* TODO: adjust scope? */
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;
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;
270 request->op.del.dn = ldb_dn_rebase_remote(request, data, request->op.del.dn);
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);
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)
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];
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)
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];
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)
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];
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 *wildcard = NULL;
344 for (i = 0; data->attribute_maps[i].local_name; i++) {
345 if (ldb_attr_cmp(data->attribute_maps[i].local_name, "*") == 0) {
346 wildcard = &data->attribute_maps[i];
349 switch (data->attribute_maps[i].type) {
354 if (ldb_attr_cmp(data->attribute_maps[i].local_name, name) == 0) {
355 return &data->attribute_maps[i];
361 if (ldb_attr_cmp(data->attribute_maps[i].u.rename.remote_name, name) == 0) {
362 return &data->attribute_maps[i];
367 for (j = 0; data->attribute_maps[i].u.generate.remote_names[j]; j++) {
368 if (ldb_attr_cmp(data->attribute_maps[i].u.generate.remote_names[j], name) == 0) {
369 return &data->attribute_maps[i];
376 /* We didn't find it, so return the wildcard record if one was configured */
381 /* Mapping attributes
382 * ================== */
384 /* Check whether an attribute will be mapped into the remote partition. */
385 BOOL map_attr_check_remote(const struct ldb_map_context *data, const char *attr)
387 const struct ldb_map_attribute *map = map_attr_find_local(data, attr);
392 if (map->type == MAP_IGNORE) {
399 /* Map an attribute name into the remote partition. */
400 const char *map_attr_map_local(void *mem_ctx, const struct ldb_map_attribute *map, const char *attr)
403 return talloc_strdup(mem_ctx, attr);
408 return talloc_strdup(mem_ctx, attr);
412 return talloc_strdup(mem_ctx, map->u.rename.remote_name);
419 /* Map an attribute name back into the local partition. */
420 const char *map_attr_map_remote(void *mem_ctx, const struct ldb_map_attribute *map, const char *attr)
423 return talloc_strdup(mem_ctx, attr);
426 if (map->type == MAP_KEEP) {
427 return talloc_strdup(mem_ctx, attr);
430 return talloc_strdup(mem_ctx, map->local_name);
433 /* Mapping ldb values
434 * ================== */
436 /* Map an ldb value into the remote partition. */
437 struct ldb_val ldb_val_map_local(struct ldb_module *module, void *mem_ctx, const struct ldb_map_attribute *map, struct ldb_val val)
439 if (map && (map->type == MAP_CONVERT) && (map->u.convert.convert_local)) {
440 return map->u.convert.convert_local(module, mem_ctx, &val);
443 return ldb_val_dup(mem_ctx, &val);
446 /* Map an ldb value back into the local partition. */
447 struct ldb_val ldb_val_map_remote(struct ldb_module *module, void *mem_ctx, const struct ldb_map_attribute *map, struct ldb_val val)
449 if (map && (map->type == MAP_CONVERT) && (map->u.convert.convert_remote)) {
450 return map->u.convert.convert_remote(module, mem_ctx, &val);
453 return ldb_val_dup(mem_ctx, &val);
460 /* Check whether a DN is below the local baseDN. */
461 BOOL ldb_dn_check_local(struct ldb_module *module, const struct ldb_dn *dn)
463 const struct ldb_map_context *data = map_get_context(module);
465 if (!data->local_base_dn) {
469 return ldb_dn_compare_base(module->ldb, data->local_base_dn, dn) == 0;
472 /* Map a DN into the remote partition. */
473 struct ldb_dn *ldb_dn_map_local(struct ldb_module *module, void *mem_ctx, const struct ldb_dn *dn)
475 const struct ldb_map_context *data = map_get_context(module);
476 struct ldb_dn *newdn;
477 struct ldb_dn_component *old, *new;
478 const struct ldb_map_attribute *map;
479 enum ldb_map_attr_type map_type;
486 newdn = ldb_dn_copy(mem_ctx, dn);
492 /* For each RDN, map the component name and possibly the value */
493 for (i = 0; i < newdn->comp_num; i++) {
494 old = &dn->components[i];
495 new = &newdn->components[i];
496 map = map_attr_find_local(data, old->name);
498 /* Unknown attribute - leave this RDN as is and hope the best... */
502 map_type = map->type;
508 ldb_debug(module->ldb, LDB_DEBUG_ERROR, "ldb_map: "
509 "MAP_IGNORE/MAP_GENERATE attribute '%s' "
510 "used in DN!\n", old->name);
514 if (map->u.convert.convert_local == NULL) {
515 ldb_debug(module->ldb, LDB_DEBUG_ERROR, "ldb_map: "
516 "'convert_local' not set for attribute '%s' "
517 "used in DN!\n", old->name);
523 new->name = discard_const_p(char, map_attr_map_local(newdn->components, map, old->name));
524 new->value = ldb_val_map_local(module, newdn->components, map, old->value);
536 /* Map a DN into the local partition. */
537 struct ldb_dn *ldb_dn_map_remote(struct ldb_module *module, void *mem_ctx, const struct ldb_dn *dn)
539 const struct ldb_map_context *data = map_get_context(module);
540 struct ldb_dn *newdn;
541 struct ldb_dn_component *old, *new;
542 const struct ldb_map_attribute *map;
543 enum ldb_map_attr_type map_type;
550 newdn = ldb_dn_copy(mem_ctx, dn);
556 /* For each RDN, map the component name and possibly the value */
557 for (i = 0; i < newdn->comp_num; i++) {
558 old = &dn->components[i];
559 new = &newdn->components[i];
560 map = map_attr_find_remote(data, old->name);
562 /* Unknown attribute - leave this RDN as is and hope the best... */
566 map_type = map->type;
572 ldb_debug(module->ldb, LDB_DEBUG_ERROR, "ldb_map: "
573 "MAP_IGNORE/MAP_GENERATE attribute '%s' "
574 "used in DN!\n", old->name);
578 if (map->u.convert.convert_remote == NULL) {
579 ldb_debug(module->ldb, LDB_DEBUG_ERROR, "ldb_map: "
580 "'convert_remote' not set for attribute '%s' "
581 "used in DN!\n", old->name);
587 new->name = discard_const_p(char, map_attr_map_remote(newdn->components, map, old->name));
588 new->value = ldb_val_map_remote(module, newdn->components, map, old->value);
600 /* Map a DN and its base into the local partition. */
601 /* TODO: This should not be required with GUIDs. */
602 struct ldb_dn *ldb_dn_map_rebase_remote(struct ldb_module *module, void *mem_ctx, const struct ldb_dn *dn)
604 const struct ldb_map_context *data = map_get_context(module);
605 struct ldb_dn *dn1, *dn2;
607 dn1 = ldb_dn_rebase_local(mem_ctx, data, dn);
608 dn2 = ldb_dn_map_remote(module, mem_ctx, dn1);
615 /* Converting DNs and objectClasses (as ldb values)
616 * ================================================ */
618 /* Map a DN contained in an ldb value into the remote partition. */
619 static struct ldb_val ldb_dn_convert_local(struct ldb_module *module, void *mem_ctx, const struct ldb_val *val)
621 struct ldb_dn *dn, *newdn;
622 struct ldb_val newval;
624 dn = ldb_dn_explode(mem_ctx, (char *)val->data);
625 newdn = ldb_dn_map_local(module, mem_ctx, dn);
629 newval.data = (uint8_t *)ldb_dn_linearize(mem_ctx, newdn);
631 newval.length = strlen((char *)newval.data);
638 /* Map a DN contained in an ldb value into the local partition. */
639 static struct ldb_val ldb_dn_convert_remote(struct ldb_module *module, void *mem_ctx, const struct ldb_val *val)
641 struct ldb_dn *dn, *newdn;
642 struct ldb_val newval;
644 dn = ldb_dn_explode(mem_ctx, (char *)val->data);
645 newdn = ldb_dn_map_remote(module, mem_ctx, dn);
649 newval.data = (uint8_t *)ldb_dn_linearize(mem_ctx, newdn);
651 newval.length = strlen((char *)newval.data);
658 /* Map an objectClass into the remote partition. */
659 static struct ldb_val map_objectclass_convert_local(struct ldb_module *module, void *mem_ctx, const struct ldb_val *val)
661 const struct ldb_map_context *data = map_get_context(module);
662 const char *name = (char *)val->data;
663 const struct ldb_map_objectclass *map = map_objectclass_find_local(data, name);
664 struct ldb_val newval;
667 newval.data = (uint8_t*)talloc_strdup(mem_ctx, map->remote_name);
668 newval.length = strlen((char *)newval.data);
672 return ldb_val_dup(mem_ctx, val);
675 /* Generate a remote message with a mapped objectClass. */
676 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)
678 struct ldb_message_element *el, *oc;
680 BOOL found_extensibleObject = False;
683 /* Find old local objectClass */
684 oc = ldb_msg_find_element(old, local_attr);
689 /* Prepare new element */
690 el = talloc_zero(remote, struct ldb_message_element);
692 ldb_oom(module->ldb);
693 return; /* TODO: fail? */
696 /* Copy local objectClass element, reverse space for an extra value */
697 el->num_values = oc->num_values + 1;
698 el->values = talloc_array(el, struct ldb_val, el->num_values);
699 if (el->values == NULL) {
701 ldb_oom(module->ldb);
702 return; /* TODO: fail? */
705 /* Copy local element name "objectClass" */
706 el->name = talloc_strdup(el, local_attr);
708 /* Convert all local objectClasses */
709 for (i = 0; i < el->num_values - 1; i++) {
710 el->values[i] = map_objectclass_convert_local(module, el->values, &oc->values[i]);
711 if (ldb_attr_cmp((char *)el->values[i].data, "extensibleObject") == 0) {
712 found_extensibleObject = True;
716 if (!found_extensibleObject) {
717 val.data = (uint8_t *)talloc_strdup(el->values, "extensibleObject");
718 val.length = strlen((char *)val.data);
720 /* Append additional objectClass "extensibleObject" */
726 /* Add new objectClass to remote message */
727 ldb_msg_add(remote, el, 0);
730 /* Map an objectClass into the local partition. */
731 static struct ldb_val map_objectclass_convert_remote(struct ldb_module *module, void *mem_ctx, const struct ldb_val *val)
733 const struct ldb_map_context *data = map_get_context(module);
734 const char *name = (char *)val->data;
735 const struct ldb_map_objectclass *map = map_objectclass_find_remote(data, name);
736 struct ldb_val newval;
739 newval.data = (uint8_t*)talloc_strdup(mem_ctx, map->local_name);
740 newval.length = strlen((char *)newval.data);
744 return ldb_val_dup(mem_ctx, val);
747 /* Generate a local message with a mapped objectClass. */
748 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)
750 struct ldb_message_element *el, *oc;
754 /* Find old remote objectClass */
755 oc = ldb_msg_find_element(remote, remote_attr);
760 /* Prepare new element */
761 el = talloc_zero(mem_ctx, struct ldb_message_element);
763 ldb_oom(module->ldb);
767 /* Copy remote objectClass element */
768 el->num_values = oc->num_values;
769 el->values = talloc_array(el, struct ldb_val, el->num_values);
770 if (el->values == NULL) {
772 ldb_oom(module->ldb);
776 /* Copy remote element name "objectClass" */
777 el->name = talloc_strdup(el, remote_attr);
779 /* Convert all remote objectClasses */
780 for (i = 0; i < el->num_values; i++) {
781 el->values[i] = map_objectclass_convert_remote(module, el->values, &oc->values[i]);
784 val.data = (uint8_t *)talloc_strdup(el->values, "extensibleObject");
785 val.length = strlen((char *)val.data);
787 /* Remove last value if it was "extensibleObject" */
788 if (ldb_val_equal_exact(&val, &el->values[i-1])) {
790 el->values = talloc_realloc(el, el->values, struct ldb_val, el->num_values);
791 if (el->values == NULL) {
793 ldb_oom(module->ldb);
803 /* Auxiliary request construction
804 * ============================== */
806 /* Store the DN of a single search result in context. */
807 static int map_search_self_callback(struct ldb_context *ldb, void *context, struct ldb_reply *ares)
809 struct map_context *ac;
811 if (context == NULL || ares == NULL) {
812 ldb_set_errstring(ldb, talloc_asprintf(ldb, "NULL Context or Result in callback"));
813 return LDB_ERR_OPERATIONS_ERROR;
816 ac = talloc_get_type(context, struct map_context);
818 /* We are interested only in the single reply */
819 if (ares->type != LDB_REPLY_ENTRY) {
824 /* We have already found a remote DN */
826 ldb_set_errstring(ldb, talloc_asprintf(ldb, "Too many results to base search"));
828 return LDB_ERR_OPERATIONS_ERROR;
832 ac->local_dn = ares->message->dn;
837 /* Build a request to search a record by its DN. */
838 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)
840 struct ldb_request *req;
842 req = talloc_zero(ac, struct ldb_request);
848 req->operation = LDB_SEARCH;
849 req->op.search.base = dn;
850 req->op.search.scope = LDB_SCOPE_BASE;
851 req->op.search.attrs = attrs;
854 req->op.search.tree = tree;
856 req->op.search.tree = ldb_parse_tree(req, NULL);
857 if (req->op.search.tree == NULL) {
863 req->controls = NULL;
864 req->context = context;
865 req->callback = callback;
866 ldb_set_timeout_from_prev_req(ac->module->ldb, ac->orig_req, req);
871 /* Build a request to search the local record by its DN. */
872 struct ldb_request *map_search_self_req(struct map_context *ac, const struct ldb_dn *dn)
874 /* attrs[] is returned from this function in
875 * ac->search_req->op.search.attrs, so it must be static, as
876 * otherwise the compiler can put it on the stack */
877 static const char * const attrs[] = { IS_MAPPED, NULL };
878 struct ldb_parse_tree *tree;
880 /* Limit search to records with 'IS_MAPPED' present */
881 /* TODO: `tree = ldb_parse_tree(ac, IS_MAPPED);' won't do. */
882 tree = talloc_zero(ac, struct ldb_parse_tree);
888 tree->operation = LDB_OP_PRESENT;
889 tree->u.present.attr = talloc_strdup(tree, IS_MAPPED);
891 return map_search_base_req(ac, dn, attrs, tree, ac, map_search_self_callback);
894 /* Build a request to update the 'IS_MAPPED' attribute */
895 struct ldb_request *map_build_fixup_req(struct map_context *ac, const struct ldb_dn *olddn, const struct ldb_dn *newdn)
897 struct ldb_request *req;
898 struct ldb_message *msg;
901 /* Prepare request */
902 req = talloc_zero(ac, struct ldb_request);
908 /* Prepare message */
909 msg = ldb_msg_new(req);
915 /* Update local 'IS_MAPPED' to the new remote DN */
916 msg->dn = discard_const_p(struct ldb_dn, olddn);
917 dn = ldb_dn_linearize(msg, newdn);
921 if (ldb_msg_add_empty(msg, IS_MAPPED, LDB_FLAG_MOD_REPLACE) != 0) {
924 if (ldb_msg_add_string(msg, IS_MAPPED, dn) != 0) {
928 req->operation = LDB_MODIFY;
929 req->op.mod.message = msg;
930 req->controls = NULL;
933 req->callback = NULL;
943 /* Asynchronous call structure
944 * =========================== */
946 /* Figure out which request is currently pending. */
947 static struct ldb_request *map_get_req(struct map_context *ac)
950 case MAP_SEARCH_SELF_MODIFY:
951 case MAP_SEARCH_SELF_DELETE:
952 case MAP_SEARCH_SELF_RENAME:
953 return ac->search_req;
956 case MAP_MODIFY_REMOTE:
957 case MAP_DELETE_REMOTE:
958 case MAP_RENAME_REMOTE:
959 return ac->remote_req;
961 case MAP_RENAME_FIXUP:
965 case MAP_MODIFY_LOCAL:
966 case MAP_DELETE_LOCAL:
967 case MAP_RENAME_LOCAL:
968 return ac->local_req;
970 case MAP_SEARCH_REMOTE:
975 return NULL; /* unreachable; silences a warning */
978 typedef int (*map_next_function)(struct ldb_handle *handle);
980 /* Figure out the next request to run. */
981 static map_next_function map_get_next(struct map_context *ac)
984 case MAP_SEARCH_REMOTE:
988 return map_add_do_remote;
992 case MAP_SEARCH_SELF_MODIFY:
993 return map_modify_do_local;
994 case MAP_MODIFY_LOCAL:
995 return map_modify_do_remote;
996 case MAP_MODIFY_REMOTE:
999 case MAP_SEARCH_SELF_DELETE:
1000 return map_delete_do_local;
1001 case MAP_DELETE_LOCAL:
1002 return map_delete_do_remote;
1003 case MAP_DELETE_REMOTE:
1006 case MAP_SEARCH_SELF_RENAME:
1007 return map_rename_do_local;
1008 case MAP_RENAME_LOCAL:
1009 return map_rename_do_fixup;
1010 case MAP_RENAME_FIXUP:
1011 return map_rename_do_remote;
1012 case MAP_RENAME_REMOTE:
1016 return NULL; /* unreachable; silences a warning */
1019 /* Wait for the current pending request to finish and continue with the next. */
1020 static int map_wait_next(struct ldb_handle *handle)
1022 struct map_context *ac;
1023 struct ldb_request *req;
1024 map_next_function next;
1027 if (handle == NULL || handle->private_data == NULL) {
1028 return LDB_ERR_OPERATIONS_ERROR;
1031 if (handle->state == LDB_ASYNC_DONE) {
1032 return handle->status;
1035 handle->state = LDB_ASYNC_PENDING;
1036 handle->status = LDB_SUCCESS;
1038 ac = talloc_get_type(handle->private_data, struct map_context);
1040 if (ac->step == MAP_SEARCH_REMOTE) {
1042 for (i = 0; i < ac->num_searches; i++) {
1043 req = ac->search_reqs[i];
1044 ret = ldb_wait(req->handle, LDB_WAIT_NONE);
1046 if (ret != LDB_SUCCESS) {
1047 handle->status = ret;
1050 if (req->handle->status != LDB_SUCCESS) {
1051 handle->status = req->handle->status;
1054 if (req->handle->state != LDB_ASYNC_DONE) {
1060 req = map_get_req(ac);
1062 ret = ldb_wait(req->handle, LDB_WAIT_NONE);
1064 if (ret != LDB_SUCCESS) {
1065 handle->status = ret;
1068 if (req->handle->status != LDB_SUCCESS) {
1069 handle->status = req->handle->status;
1072 if (req->handle->state != LDB_ASYNC_DONE) {
1076 next = map_get_next(ac);
1078 return next(handle);
1085 handle->state = LDB_ASYNC_DONE;
1089 /* Wait for all current pending requests to finish. */
1090 static int map_wait_all(struct ldb_handle *handle)
1094 while (handle->state != LDB_ASYNC_DONE) {
1095 ret = map_wait_next(handle);
1096 if (ret != LDB_SUCCESS) {
1101 return handle->status;
1104 /* Wait for pending requests to finish. */
1105 static int map_wait(struct ldb_handle *handle, enum ldb_wait_type type)
1107 if (type == LDB_WAIT_ALL) {
1108 return map_wait_all(handle);
1110 return map_wait_next(handle);
1115 /* Module initialization
1116 * ===================== */
1118 /* Provided module operations */
1119 static const struct ldb_module_ops map_ops = {
1122 .modify = map_modify,
1124 .rename = map_rename,
1125 .search = map_search,
1129 /* Builtin mappings for DNs and objectClasses */
1130 static const struct ldb_map_attribute builtin_attribute_maps[] = {
1133 .type = MAP_CONVERT,
1136 .remote_name = "dn",
1137 .convert_local = ldb_dn_convert_local,
1138 .convert_remote = ldb_dn_convert_remote,
1143 .local_name = "objectclass",
1144 .type = MAP_GENERATE,
1147 .remote_names = { "objectclass", NULL },
1148 .generate_local = map_objectclass_generate_local,
1149 .generate_remote = map_objectclass_generate_remote,
1158 /* Find the special 'MAP_DN_NAME' record and store local and remote
1159 * base DNs in private data. */
1160 static int map_init_dns(struct ldb_module *module, struct ldb_map_context *data, const char *name)
1162 static const char * const attrs[] = { MAP_DN_FROM, MAP_DN_TO, NULL };
1164 struct ldb_message *msg;
1165 struct ldb_result *res;
1169 data->local_base_dn = NULL;
1170 data->remote_base_dn = NULL;
1174 dn = ldb_dn_string_compose(data, NULL, "%s=%s", MAP_DN_NAME, name);
1176 ldb_debug(module->ldb, LDB_DEBUG_ERROR, "ldb_map: "
1177 "Failed to construct '%s' DN!\n", MAP_DN_NAME);
1178 return LDB_ERR_OPERATIONS_ERROR;
1181 ret = ldb_search(module->ldb, dn, LDB_SCOPE_BASE, NULL, attrs, &res);
1183 if (ret != LDB_SUCCESS) {
1186 if (res->count == 0) {
1187 ldb_debug(module->ldb, LDB_DEBUG_ERROR, "ldb_map: "
1188 "No results for '%s=%s'!\n", MAP_DN_NAME, name);
1189 return LDB_ERR_CONSTRAINT_VIOLATION;
1191 if (res->count > 1) {
1192 ldb_debug(module->ldb, LDB_DEBUG_ERROR, "ldb_map: "
1193 "Too many results for '%s=%s'!\n", MAP_DN_NAME, name);
1194 return LDB_ERR_CONSTRAINT_VIOLATION;
1198 data->local_base_dn = ldb_msg_find_attr_as_dn(data, msg, MAP_DN_FROM);
1199 data->remote_base_dn = ldb_msg_find_attr_as_dn(data, msg, MAP_DN_TO);
1205 /* Store attribute maps and objectClass maps in private data. */
1206 static int map_init_maps(struct ldb_module *module, struct ldb_map_context *data, const struct ldb_map_attribute *attrs, const struct ldb_map_objectclass *ocls)
1211 /* Count specified attribute maps */
1212 for (i = 0; attrs[i].local_name; i++) /* noop */ ;
1213 /* Count built-in attribute maps */
1214 for (j = 0; builtin_attribute_maps[j].local_name; j++) /* noop */ ;
1216 /* Store list of attribute maps */
1217 data->attribute_maps = talloc_array(data, struct ldb_map_attribute, i+j+1);
1218 if (data->attribute_maps == NULL) {
1220 return LDB_ERR_OPERATIONS_ERROR;
1223 /* Specified ones go first */
1224 for (i = 0; attrs[i].local_name; i++) {
1225 data->attribute_maps[last] = attrs[i];
1229 /* Built-in ones go last */
1230 for (i = 0; builtin_attribute_maps[i].local_name; i++) {
1231 data->attribute_maps[last] = builtin_attribute_maps[i];
1235 /* Ensure 'local_name == NULL' for the last entry */
1236 memset(&data->attribute_maps[last], 0, sizeof(struct ldb_map_attribute));
1238 /* Store list of objectClass maps */
1239 data->objectclass_maps = ocls;
1244 /* Copy the list of provided module operations. */
1245 struct ldb_module_ops ldb_map_get_ops(void)
1250 /* Initialize global private data. */
1251 int ldb_map_init(struct ldb_module *module, const struct ldb_map_attribute *attrs, const struct ldb_map_objectclass *ocls, const char *name)
1253 struct map_private *data;
1256 /* Prepare private data */
1257 data = talloc_zero(module, struct map_private);
1260 return LDB_ERR_OPERATIONS_ERROR;
1263 module->private_data = data;
1265 /* Store local and remote baseDNs */
1266 ret = map_init_dns(module, &(data->context), name);
1267 if (ret != LDB_SUCCESS) {
1272 /* Store list of attribute and objectClass maps */
1273 ret = map_init_maps(module, &(data->context), attrs, ocls);
1274 if (ret != LDB_SUCCESS) {
1282 /* Usage note for initialization of this module:
1284 * ldb_map is meant to be used from a different module that sets up
1285 * the mappings and gets registered in ldb.
1287 * 'ldb_map_init' initializes the private data of this module and
1288 * stores the attribute and objectClass maps in there. It also looks
1289 * up the '@MAP' special DN so requests can be redirected to the
1292 * This function should be called from the 'init_context' op of the
1293 * module using ldb_map.
1295 * 'ldb_map_get_ops' returns a copy of ldb_maps module operations.
1297 * It should be called from the initialize function of the using
1298 * module, which should then override the 'init_context' op with a
1299 * function making the appropriate calls to 'ldb_map_init'.