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 /* Copy a DN with the base DN of the local partition. */
185 static struct ldb_dn *ldb_dn_rebase_local(void *mem_ctx, const struct ldb_map_context *data, struct ldb_dn *dn)
187 struct ldb_dn *new_dn;
189 new_dn = ldb_dn_copy(mem_ctx, dn);
190 if ( ! ldb_dn_validate(new_dn)) {
195 /* may be we don't need to rebase at all */
196 if ( ! data->remote_base_dn || ! data->local_base_dn) {
200 if ( ! ldb_dn_remove_base_components(new_dn, ldb_dn_get_comp_num(data->remote_base_dn))) {
205 if ( ! ldb_dn_add_base(new_dn, data->local_base_dn)) {
213 /* Copy a DN with the base DN of the remote partition. */
214 static struct ldb_dn *ldb_dn_rebase_remote(void *mem_ctx, const struct ldb_map_context *data, struct ldb_dn *dn)
216 struct ldb_dn *new_dn;
218 new_dn = ldb_dn_copy(mem_ctx, dn);
219 if ( ! ldb_dn_validate(new_dn)) {
224 /* may be we don't need to rebase at all */
225 if ( ! data->remote_base_dn || ! data->local_base_dn) {
229 if ( ! ldb_dn_remove_base_components(new_dn, ldb_dn_get_comp_num(data->local_base_dn))) {
234 if ( ! ldb_dn_add_base(new_dn, data->remote_base_dn)) {
242 /* Run a request and make sure it targets the remote partition. */
243 /* TODO: free old DNs and messages? */
244 int ldb_next_remote_request(struct ldb_module *module, struct ldb_request *request)
246 const struct ldb_map_context *data = map_get_context(module);
247 struct ldb_message *msg;
249 switch (request->operation) {
251 if (request->op.search.base) {
252 request->op.search.base = ldb_dn_rebase_remote(request, data, request->op.search.base);
254 request->op.search.base = data->remote_base_dn;
255 /* TODO: adjust scope? */
260 msg = ldb_msg_copy_shallow(request, request->op.add.message);
261 msg->dn = ldb_dn_rebase_remote(msg, data, msg->dn);
262 request->op.add.message = msg;
266 msg = ldb_msg_copy_shallow(request, request->op.mod.message);
267 msg->dn = ldb_dn_rebase_remote(msg, data, msg->dn);
268 request->op.mod.message = msg;
272 request->op.del.dn = ldb_dn_rebase_remote(request, data, request->op.del.dn);
276 request->op.rename.olddn = ldb_dn_rebase_remote(request, data, request->op.rename.olddn);
277 request->op.rename.newdn = ldb_dn_rebase_remote(request, data, request->op.rename.newdn);
281 ldb_debug(module->ldb, LDB_DEBUG_ERROR, "ldb_map: "
282 "Invalid remote request!\n");
283 return LDB_ERR_OPERATIONS_ERROR;
286 return ldb_next_request(module, request);
290 /* Finding mappings for attributes and objectClasses
291 * ================================================= */
293 /* Find an objectClass mapping by the local name. */
294 static const struct ldb_map_objectclass *map_objectclass_find_local(const struct ldb_map_context *data, const char *name)
298 for (i = 0; data->objectclass_maps && data->objectclass_maps[i].local_name; i++) {
299 if (ldb_attr_cmp(data->objectclass_maps[i].local_name, name) == 0) {
300 return &data->objectclass_maps[i];
307 /* Find an objectClass mapping by the remote name. */
308 static const struct ldb_map_objectclass *map_objectclass_find_remote(const struct ldb_map_context *data, const char *name)
312 for (i = 0; data->objectclass_maps && data->objectclass_maps[i].remote_name; i++) {
313 if (ldb_attr_cmp(data->objectclass_maps[i].remote_name, name) == 0) {
314 return &data->objectclass_maps[i];
321 /* Find an attribute mapping by the local name. */
322 const struct ldb_map_attribute *map_attr_find_local(const struct ldb_map_context *data, const char *name)
326 for (i = 0; data->attribute_maps[i].local_name; i++) {
327 if (ldb_attr_cmp(data->attribute_maps[i].local_name, name) == 0) {
328 return &data->attribute_maps[i];
331 for (i = 0; data->attribute_maps[i].local_name; i++) {
332 if (ldb_attr_cmp(data->attribute_maps[i].local_name, "*") == 0) {
333 return &data->attribute_maps[i];
340 /* Find an attribute mapping by the remote name. */
341 const struct ldb_map_attribute *map_attr_find_remote(const struct ldb_map_context *data, const char *name)
343 const struct ldb_map_attribute *map;
344 const struct ldb_map_attribute *wildcard = NULL;
347 for (i = 0; data->attribute_maps[i].local_name; i++) {
348 map = &data->attribute_maps[i];
349 if (ldb_attr_cmp(map->local_name, "*") == 0) {
350 wildcard = &data->attribute_maps[i];
358 if (ldb_attr_cmp(map->local_name, name) == 0) {
365 if (ldb_attr_cmp(map->u.rename.remote_name, name) == 0) {
371 for (j = 0; map->u.generate.remote_names && map->u.generate.remote_names[j]; j++) {
372 if (ldb_attr_cmp(map->u.generate.remote_names[j], name) == 0) {
380 /* We didn't find it, so return the wildcard record if one was configured */
385 /* Mapping attributes
386 * ================== */
388 /* Check whether an attribute will be mapped into the remote partition. */
389 BOOL map_attr_check_remote(const struct ldb_map_context *data, const char *attr)
391 const struct ldb_map_attribute *map = map_attr_find_local(data, attr);
396 if (map->type == MAP_IGNORE) {
403 /* Map an attribute name into the remote partition. */
404 const char *map_attr_map_local(void *mem_ctx, const struct ldb_map_attribute *map, const char *attr)
407 return talloc_strdup(mem_ctx, attr);
412 return talloc_strdup(mem_ctx, attr);
416 return talloc_strdup(mem_ctx, map->u.rename.remote_name);
423 /* Map an attribute name back into the local partition. */
424 const char *map_attr_map_remote(void *mem_ctx, const struct ldb_map_attribute *map, const char *attr)
427 return talloc_strdup(mem_ctx, attr);
430 if (map->type == MAP_KEEP) {
431 return talloc_strdup(mem_ctx, attr);
434 return talloc_strdup(mem_ctx, map->local_name);
438 /* Merge two lists of attributes into a single one. */
439 int map_attrs_merge(struct ldb_module *module, void *mem_ctx,
440 const char ***attrs, const char * const *more_attrs)
444 for (i = 0; *attrs && (*attrs)[i]; i++) /* noop */ ;
445 for (j = 0; more_attrs && more_attrs[j]; j++) /* noop */ ;
447 *attrs = talloc_realloc(mem_ctx, *attrs, const char *, i+j+1);
448 if (*attrs == NULL) {
453 for (k = 0; k < j; k++) {
454 (*attrs)[i + k] = more_attrs[k];
457 (*attrs)[i+k] = NULL;
462 /* Mapping ldb values
463 * ================== */
465 /* Map an ldb value into the remote partition. */
466 struct ldb_val ldb_val_map_local(struct ldb_module *module, void *mem_ctx,
467 const struct ldb_map_attribute *map, const struct ldb_val *val)
469 if (map && (map->type == MAP_CONVERT) && (map->u.convert.convert_local)) {
470 return map->u.convert.convert_local(module, mem_ctx, val);
473 return ldb_val_dup(mem_ctx, val);
476 /* Map an ldb value back into the local partition. */
477 struct ldb_val ldb_val_map_remote(struct ldb_module *module, void *mem_ctx,
478 const struct ldb_map_attribute *map, const struct ldb_val *val)
480 if (map && (map->type == MAP_CONVERT) && (map->u.convert.convert_remote)) {
481 return map->u.convert.convert_remote(module, mem_ctx, val);
484 return ldb_val_dup(mem_ctx, val);
491 /* Check whether a DN is below the local baseDN. */
492 BOOL ldb_dn_check_local(struct ldb_module *module, struct ldb_dn *dn)
494 const struct ldb_map_context *data = map_get_context(module);
496 if (!data->local_base_dn) {
500 return ldb_dn_compare_base(data->local_base_dn, dn) == 0;
503 /* Map a DN into the remote partition. */
504 struct ldb_dn *ldb_dn_map_local(struct ldb_module *module, void *mem_ctx, struct ldb_dn *dn)
506 const struct ldb_map_context *data = map_get_context(module);
507 struct ldb_dn *newdn;
508 const struct ldb_map_attribute *map;
509 enum ldb_map_attr_type map_type;
511 struct ldb_val value;
518 newdn = ldb_dn_copy(mem_ctx, dn);
524 /* For each RDN, map the component name and possibly the value */
525 for (i = 0; i < ldb_dn_get_comp_num(newdn); i++) {
526 map = map_attr_find_local(data, ldb_dn_get_component_name(dn, i));
528 /* Unknown attribute - leave this RDN as is and hope the best... */
532 map_type = map->type;
538 ldb_debug(module->ldb, LDB_DEBUG_ERROR, "ldb_map: "
539 "MAP_IGNORE/MAP_GENERATE attribute '%s' "
540 "used in DN!\n", ldb_dn_get_component_name(dn, i));
544 if (map->u.convert.convert_local == NULL) {
545 ldb_debug(module->ldb, LDB_DEBUG_ERROR, "ldb_map: "
546 "'convert_local' not set for attribute '%s' "
547 "used in DN!\n", ldb_dn_get_component_name(dn, i));
553 name = map_attr_map_local(newdn, map, ldb_dn_get_component_name(dn, i));
554 if (name == NULL) goto failed;
556 value = ldb_val_map_local(module, newdn, map, ldb_dn_get_component_val(dn, i));
557 if (value.data == NULL) goto failed;
559 ret = ldb_dn_set_component(newdn, i, name, value);
560 if (ret != LDB_SUCCESS) {
575 /* Map a DN into the local partition. */
576 struct ldb_dn *ldb_dn_map_remote(struct ldb_module *module, void *mem_ctx, struct ldb_dn *dn)
578 const struct ldb_map_context *data = map_get_context(module);
579 struct ldb_dn *newdn;
580 const struct ldb_map_attribute *map;
581 enum ldb_map_attr_type map_type;
583 struct ldb_val value;
590 newdn = ldb_dn_copy(mem_ctx, dn);
596 /* For each RDN, map the component name and possibly the value */
597 for (i = 0; i < ldb_dn_get_comp_num(newdn); i++) {
598 map = map_attr_find_remote(data, ldb_dn_get_component_name(dn, i));
600 /* Unknown attribute - leave this RDN as is and hope the best... */
604 map_type = map->type;
610 ldb_debug(module->ldb, LDB_DEBUG_ERROR, "ldb_map: "
611 "MAP_IGNORE/MAP_GENERATE attribute '%s' "
612 "used in DN!\n", ldb_dn_get_component_name(dn, i));
616 if (map->u.convert.convert_remote == NULL) {
617 ldb_debug(module->ldb, LDB_DEBUG_ERROR, "ldb_map: "
618 "'convert_remote' not set for attribute '%s' "
619 "used in DN!\n", ldb_dn_get_component_name(dn, i));
625 name = map_attr_map_remote(newdn, map, ldb_dn_get_component_name(dn, i));
626 if (name == NULL) goto failed;
628 value = ldb_val_map_remote(module, newdn, map, ldb_dn_get_component_val(dn, i));
629 if (value.data == NULL) goto failed;
631 ret = ldb_dn_set_component(newdn, i, name, value);
632 if (ret != LDB_SUCCESS) {
647 /* Map a DN and its base into the local partition. */
648 /* TODO: This should not be required with GUIDs. */
649 struct ldb_dn *ldb_dn_map_rebase_remote(struct ldb_module *module, void *mem_ctx, struct ldb_dn *dn)
651 const struct ldb_map_context *data = map_get_context(module);
652 struct ldb_dn *dn1, *dn2;
654 dn1 = ldb_dn_rebase_local(mem_ctx, data, dn);
655 dn2 = ldb_dn_map_remote(module, mem_ctx, dn1);
662 /* Converting DNs and objectClasses (as ldb values)
663 * ================================================ */
665 /* Map a DN contained in an ldb value into the remote partition. */
666 static struct ldb_val ldb_dn_convert_local(struct ldb_module *module, void *mem_ctx, const struct ldb_val *val)
668 struct ldb_dn *dn, *newdn;
669 struct ldb_val newval;
671 dn = ldb_dn_new(mem_ctx, module->ldb, (char *)val->data);
672 if (! ldb_dn_validate(dn)) {
678 newdn = ldb_dn_map_local(module, mem_ctx, dn);
682 newval.data = (uint8_t *)ldb_dn_alloc_linearized(mem_ctx, newdn);
684 newval.length = strlen((char *)newval.data);
691 /* Map a DN contained in an ldb value into the local partition. */
692 static struct ldb_val ldb_dn_convert_remote(struct ldb_module *module, void *mem_ctx, const struct ldb_val *val)
694 struct ldb_dn *dn, *newdn;
695 struct ldb_val newval;
697 dn = ldb_dn_new(mem_ctx, module->ldb, (char *)val->data);
698 if (! ldb_dn_validate(dn)) {
704 newdn = ldb_dn_map_remote(module, mem_ctx, dn);
708 newval.data = (uint8_t *)ldb_dn_alloc_linearized(mem_ctx, newdn);
710 newval.length = strlen((char *)newval.data);
717 /* Map an objectClass into the remote partition. */
718 static struct ldb_val map_objectclass_convert_local(struct ldb_module *module, void *mem_ctx, const struct ldb_val *val)
720 const struct ldb_map_context *data = map_get_context(module);
721 const char *name = (char *)val->data;
722 const struct ldb_map_objectclass *map = map_objectclass_find_local(data, name);
723 struct ldb_val newval;
726 newval.data = (uint8_t*)talloc_strdup(mem_ctx, map->remote_name);
727 newval.length = strlen((char *)newval.data);
731 return ldb_val_dup(mem_ctx, val);
734 /* Generate a remote message with a mapped objectClass. */
735 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)
737 struct ldb_message_element *el, *oc;
739 BOOL found_extensibleObject = False;
742 /* Find old local objectClass */
743 oc = ldb_msg_find_element(old, "objectClass");
748 /* Prepare new element */
749 el = talloc_zero(remote, struct ldb_message_element);
751 ldb_oom(module->ldb);
752 return; /* TODO: fail? */
755 /* Copy local objectClass element, reverse space for an extra value */
756 el->num_values = oc->num_values + 1;
757 el->values = talloc_array(el, struct ldb_val, el->num_values);
758 if (el->values == NULL) {
760 ldb_oom(module->ldb);
761 return; /* TODO: fail? */
764 /* Copy local element name "objectClass" */
765 el->name = talloc_strdup(el, local_attr);
767 /* Convert all local objectClasses */
768 for (i = 0; i < el->num_values - 1; i++) {
769 el->values[i] = map_objectclass_convert_local(module, el->values, &oc->values[i]);
770 if (ldb_attr_cmp((char *)el->values[i].data, "extensibleObject") == 0) {
771 found_extensibleObject = True;
775 if (!found_extensibleObject) {
776 val.data = (uint8_t *)talloc_strdup(el->values, "extensibleObject");
777 val.length = strlen((char *)val.data);
779 /* Append additional objectClass "extensibleObject" */
785 /* Add new objectClass to remote message */
786 ldb_msg_add(remote, el, 0);
789 /* Map an objectClass into the local partition. */
790 static struct ldb_val map_objectclass_convert_remote(struct ldb_module *module, void *mem_ctx, const struct ldb_val *val)
792 const struct ldb_map_context *data = map_get_context(module);
793 const char *name = (char *)val->data;
794 const struct ldb_map_objectclass *map = map_objectclass_find_remote(data, name);
795 struct ldb_val newval;
798 newval.data = (uint8_t*)talloc_strdup(mem_ctx, map->local_name);
799 newval.length = strlen((char *)newval.data);
803 return ldb_val_dup(mem_ctx, val);
806 /* Generate a local message with a mapped objectClass. */
807 static struct ldb_message_element *map_objectclass_generate_local(struct ldb_module *module, void *mem_ctx, const char *local_attr, const struct ldb_message *remote)
809 struct ldb_message_element *el, *oc;
813 /* Find old remote objectClass */
814 oc = ldb_msg_find_element(remote, "objectClass");
819 /* Prepare new element */
820 el = talloc_zero(mem_ctx, struct ldb_message_element);
822 ldb_oom(module->ldb);
826 /* Copy remote objectClass element */
827 el->num_values = oc->num_values;
828 el->values = talloc_array(el, struct ldb_val, el->num_values);
829 if (el->values == NULL) {
831 ldb_oom(module->ldb);
835 /* Copy remote element name "objectClass" */
836 el->name = talloc_strdup(el, local_attr);
838 /* Convert all remote objectClasses */
839 for (i = 0; i < el->num_values; i++) {
840 el->values[i] = map_objectclass_convert_remote(module, el->values, &oc->values[i]);
843 val.data = (uint8_t *)talloc_strdup(el->values, "extensibleObject");
844 val.length = strlen((char *)val.data);
846 /* Remove last value if it was "extensibleObject" */
847 if (ldb_val_equal_exact(&val, &el->values[i-1])) {
849 el->values = talloc_realloc(el, el->values, struct ldb_val, el->num_values);
850 if (el->values == NULL) {
852 ldb_oom(module->ldb);
860 /* Mappings for searches on objectClass= assuming a one-to-one
861 * mapping. Needed because this is a generate operator for the
863 static int map_objectclass_convert_operator(struct ldb_module *module, void *mem_ctx,
864 struct ldb_parse_tree **new, const struct ldb_parse_tree *tree)
867 static const struct ldb_map_attribute objectclass_map = {
868 .local_name = "objectClass",
872 .remote_name = "objectClass",
873 .convert_local = map_objectclass_convert_local,
874 .convert_remote = map_objectclass_convert_remote,
879 return map_subtree_collect_remote_simple(module, mem_ctx, new, tree, &objectclass_map);
882 /* Auxiliary request construction
883 * ============================== */
885 /* Store the DN of a single search result in context. */
886 static int map_search_self_callback(struct ldb_context *ldb, void *context, struct ldb_reply *ares)
888 struct map_context *ac;
890 if (context == NULL || ares == NULL) {
891 ldb_set_errstring(ldb, talloc_asprintf(ldb, "NULL Context or Result in callback"));
892 return LDB_ERR_OPERATIONS_ERROR;
895 ac = talloc_get_type(context, struct map_context);
897 /* We are interested only in the single reply */
898 if (ares->type != LDB_REPLY_ENTRY) {
903 /* We have already found a remote DN */
905 ldb_set_errstring(ldb, talloc_asprintf(ldb, "Too many results to base search"));
907 return LDB_ERR_OPERATIONS_ERROR;
911 ac->local_dn = ares->message->dn;
916 /* Build a request to search a record by its DN. */
917 struct ldb_request *map_search_base_req(struct map_context *ac, struct ldb_dn *dn, const char * const *attrs, const struct ldb_parse_tree *tree, void *context, ldb_search_callback callback)
919 struct ldb_request *req;
921 req = talloc_zero(ac, struct ldb_request);
927 req->operation = LDB_SEARCH;
928 req->op.search.base = dn;
929 req->op.search.scope = LDB_SCOPE_BASE;
930 req->op.search.attrs = attrs;
933 req->op.search.tree = tree;
935 req->op.search.tree = ldb_parse_tree(req, NULL);
936 if (req->op.search.tree == NULL) {
942 req->controls = NULL;
943 req->context = context;
944 req->callback = callback;
945 ldb_set_timeout_from_prev_req(ac->module->ldb, ac->orig_req, req);
950 /* Build a request to search the local record by its DN. */
951 struct ldb_request *map_search_self_req(struct map_context *ac, struct ldb_dn *dn)
953 /* attrs[] is returned from this function in
954 * ac->search_req->op.search.attrs, so it must be static, as
955 * otherwise the compiler can put it on the stack */
956 static const char * const attrs[] = { IS_MAPPED, NULL };
957 struct ldb_parse_tree *tree;
959 /* Limit search to records with 'IS_MAPPED' present */
960 /* TODO: `tree = ldb_parse_tree(ac, IS_MAPPED);' won't do. */
961 tree = talloc_zero(ac, struct ldb_parse_tree);
967 tree->operation = LDB_OP_PRESENT;
968 tree->u.present.attr = talloc_strdup(tree, IS_MAPPED);
970 return map_search_base_req(ac, dn, attrs, tree, ac, map_search_self_callback);
973 /* Build a request to update the 'IS_MAPPED' attribute */
974 struct ldb_request *map_build_fixup_req(struct map_context *ac, struct ldb_dn *olddn, struct ldb_dn *newdn)
976 struct ldb_request *req;
977 struct ldb_message *msg;
980 /* Prepare request */
981 req = talloc_zero(ac, struct ldb_request);
987 /* Prepare message */
988 msg = ldb_msg_new(req);
994 /* Update local 'IS_MAPPED' to the new remote DN */
995 msg->dn = ldb_dn_copy(msg, olddn);
996 dn = ldb_dn_alloc_linearized(msg, newdn);
997 if ( ! dn || ! ldb_dn_validate(msg->dn)) {
1000 if (ldb_msg_add_empty(msg, IS_MAPPED, LDB_FLAG_MOD_REPLACE, NULL) != 0) {
1003 if (ldb_msg_add_string(msg, IS_MAPPED, dn) != 0) {
1007 req->operation = LDB_MODIFY;
1008 req->op.mod.message = msg;
1009 req->controls = NULL;
1011 req->context = NULL;
1012 req->callback = NULL;
1022 /* Asynchronous call structure
1023 * =========================== */
1025 /* Figure out which request is currently pending. */
1026 static struct ldb_request *map_get_req(struct map_context *ac)
1029 case MAP_SEARCH_SELF_MODIFY:
1030 case MAP_SEARCH_SELF_DELETE:
1031 case MAP_SEARCH_SELF_RENAME:
1032 return ac->search_req;
1034 case MAP_ADD_REMOTE:
1035 case MAP_MODIFY_REMOTE:
1036 case MAP_DELETE_REMOTE:
1037 case MAP_RENAME_REMOTE:
1038 return ac->remote_req;
1040 case MAP_RENAME_FIXUP:
1041 return ac->down_req;
1044 case MAP_MODIFY_LOCAL:
1045 case MAP_DELETE_LOCAL:
1046 case MAP_RENAME_LOCAL:
1047 return ac->local_req;
1049 case MAP_SEARCH_REMOTE:
1054 return NULL; /* unreachable; silences a warning */
1057 typedef int (*map_next_function)(struct ldb_handle *handle);
1059 /* Figure out the next request to run. */
1060 static map_next_function map_get_next(struct map_context *ac)
1063 case MAP_SEARCH_REMOTE:
1067 return map_add_do_remote;
1068 case MAP_ADD_REMOTE:
1071 case MAP_SEARCH_SELF_MODIFY:
1072 return map_modify_do_local;
1073 case MAP_MODIFY_LOCAL:
1074 return map_modify_do_remote;
1075 case MAP_MODIFY_REMOTE:
1078 case MAP_SEARCH_SELF_DELETE:
1079 return map_delete_do_local;
1080 case MAP_DELETE_LOCAL:
1081 return map_delete_do_remote;
1082 case MAP_DELETE_REMOTE:
1085 case MAP_SEARCH_SELF_RENAME:
1086 return map_rename_do_local;
1087 case MAP_RENAME_LOCAL:
1088 return map_rename_do_fixup;
1089 case MAP_RENAME_FIXUP:
1090 return map_rename_do_remote;
1091 case MAP_RENAME_REMOTE:
1095 return NULL; /* unreachable; silences a warning */
1098 /* Wait for the current pending request to finish and continue with the next. */
1099 static int map_wait_next(struct ldb_handle *handle)
1101 struct map_context *ac;
1102 struct ldb_request *req;
1103 map_next_function next;
1106 if (handle == NULL || handle->private_data == NULL) {
1107 return LDB_ERR_OPERATIONS_ERROR;
1110 if (handle->state == LDB_ASYNC_DONE) {
1111 return handle->status;
1114 handle->state = LDB_ASYNC_PENDING;
1115 handle->status = LDB_SUCCESS;
1117 ac = talloc_get_type(handle->private_data, struct map_context);
1119 if (ac->step == MAP_SEARCH_REMOTE) {
1121 for (i = 0; i < ac->num_searches; i++) {
1122 req = ac->search_reqs[i];
1123 ret = ldb_wait(req->handle, LDB_WAIT_NONE);
1125 if (ret != LDB_SUCCESS) {
1126 handle->status = ret;
1129 if (req->handle->status != LDB_SUCCESS) {
1130 handle->status = req->handle->status;
1133 if (req->handle->state != LDB_ASYNC_DONE) {
1139 req = map_get_req(ac);
1141 ret = ldb_wait(req->handle, LDB_WAIT_NONE);
1143 if (ret != LDB_SUCCESS) {
1144 handle->status = ret;
1147 if (req->handle->status != LDB_SUCCESS) {
1148 handle->status = req->handle->status;
1151 if (req->handle->state != LDB_ASYNC_DONE) {
1155 next = map_get_next(ac);
1157 return next(handle);
1164 handle->state = LDB_ASYNC_DONE;
1168 /* Wait for all current pending requests to finish. */
1169 static int map_wait_all(struct ldb_handle *handle)
1173 while (handle->state != LDB_ASYNC_DONE) {
1174 ret = map_wait_next(handle);
1175 if (ret != LDB_SUCCESS) {
1180 return handle->status;
1183 /* Wait for pending requests to finish. */
1184 static int map_wait(struct ldb_handle *handle, enum ldb_wait_type type)
1186 if (type == LDB_WAIT_ALL) {
1187 return map_wait_all(handle);
1189 return map_wait_next(handle);
1194 /* Module initialization
1195 * ===================== */
1197 /* Provided module operations */
1198 static const struct ldb_module_ops map_ops = {
1201 .modify = map_modify,
1203 .rename = map_rename,
1204 .search = map_search,
1208 /* Builtin mappings for DNs and objectClasses */
1209 static const struct ldb_map_attribute builtin_attribute_maps[] = {
1212 .type = MAP_CONVERT,
1215 .remote_name = "dn",
1216 .convert_local = ldb_dn_convert_local,
1217 .convert_remote = ldb_dn_convert_remote,
1222 .local_name = "objectClass",
1223 .type = MAP_GENERATE,
1224 .convert_operator = map_objectclass_convert_operator,
1227 .remote_names = { "objectClass", NULL },
1228 .generate_local = map_objectclass_generate_local,
1229 .generate_remote = map_objectclass_generate_remote,
1238 /* Find the special 'MAP_DN_NAME' record and store local and remote
1239 * base DNs in private data. */
1240 static int map_init_dns(struct ldb_module *module, struct ldb_map_context *data, const char *name)
1242 static const char * const attrs[] = { MAP_DN_FROM, MAP_DN_TO, NULL };
1244 struct ldb_message *msg;
1245 struct ldb_result *res;
1249 data->local_base_dn = NULL;
1250 data->remote_base_dn = NULL;
1254 dn = ldb_dn_new_fmt(data, module->ldb, "%s=%s", MAP_DN_NAME, name);
1255 if ( ! ldb_dn_validate(dn)) {
1256 ldb_debug(module->ldb, LDB_DEBUG_ERROR, "ldb_map: "
1257 "Failed to construct '%s' DN!\n", MAP_DN_NAME);
1258 return LDB_ERR_OPERATIONS_ERROR;
1261 ret = ldb_search(module->ldb, dn, LDB_SCOPE_BASE, NULL, attrs, &res);
1263 if (ret != LDB_SUCCESS) {
1266 if (res->count == 0) {
1267 ldb_debug(module->ldb, LDB_DEBUG_ERROR, "ldb_map: "
1268 "No results for '%s=%s'!\n", MAP_DN_NAME, name);
1270 return LDB_ERR_CONSTRAINT_VIOLATION;
1272 if (res->count > 1) {
1273 ldb_debug(module->ldb, LDB_DEBUG_ERROR, "ldb_map: "
1274 "Too many results for '%s=%s'!\n", MAP_DN_NAME, name);
1276 return LDB_ERR_CONSTRAINT_VIOLATION;
1280 data->local_base_dn = ldb_msg_find_attr_as_dn(module->ldb, data, msg, MAP_DN_FROM);
1281 data->remote_base_dn = ldb_msg_find_attr_as_dn(module->ldb, data, msg, MAP_DN_TO);
1287 /* Store attribute maps and objectClass maps in private data. */
1288 static int map_init_maps(struct ldb_module *module, struct ldb_map_context *data,
1289 const struct ldb_map_attribute *attrs,
1290 const struct ldb_map_objectclass *ocls,
1291 const char * const *wildcard_attributes)
1296 /* Count specified attribute maps */
1297 for (i = 0; attrs[i].local_name; i++) /* noop */ ;
1298 /* Count built-in attribute maps */
1299 for (j = 0; builtin_attribute_maps[j].local_name; j++) /* noop */ ;
1301 /* Store list of attribute maps */
1302 data->attribute_maps = talloc_array(data, struct ldb_map_attribute, i+j+1);
1303 if (data->attribute_maps == NULL) {
1305 return LDB_ERR_OPERATIONS_ERROR;
1308 /* Specified ones go first */
1309 for (i = 0; attrs[i].local_name; i++) {
1310 data->attribute_maps[last] = attrs[i];
1314 /* Built-in ones go last */
1315 for (i = 0; builtin_attribute_maps[i].local_name; i++) {
1316 data->attribute_maps[last] = builtin_attribute_maps[i];
1320 /* Ensure 'local_name == NULL' for the last entry */
1321 memset(&data->attribute_maps[last], 0, sizeof(struct ldb_map_attribute));
1323 /* Store list of objectClass maps */
1324 data->objectclass_maps = ocls;
1326 data->wildcard_attributes = wildcard_attributes;
1331 /* Copy the list of provided module operations. */
1332 _PUBLIC_ struct ldb_module_ops ldb_map_get_ops(void)
1337 /* Initialize global private data. */
1338 _PUBLIC_ int ldb_map_init(struct ldb_module *module, const struct ldb_map_attribute *attrs,
1339 const struct ldb_map_objectclass *ocls,
1340 const char * const *wildcard_attributes,
1343 struct map_private *data;
1346 /* Prepare private data */
1347 data = talloc_zero(module, struct map_private);
1350 return LDB_ERR_OPERATIONS_ERROR;
1353 module->private_data = data;
1355 data->context = talloc_zero(data, struct ldb_map_context);
1356 if (!data->context) {
1358 return LDB_ERR_OPERATIONS_ERROR;
1361 /* Store local and remote baseDNs */
1362 ret = map_init_dns(module, data->context, name);
1363 if (ret != LDB_SUCCESS) {
1368 /* Store list of attribute and objectClass maps */
1369 ret = map_init_maps(module, data->context, attrs, ocls, wildcard_attributes);
1370 if (ret != LDB_SUCCESS) {
1378 /* Usage note for initialization of this module:
1380 * ldb_map is meant to be used from a different module that sets up
1381 * the mappings and gets registered in ldb.
1383 * 'ldb_map_init' initializes the private data of this module and
1384 * stores the attribute and objectClass maps in there. It also looks
1385 * up the '@MAP' special DN so requests can be redirected to the
1388 * This function should be called from the 'init_context' op of the
1389 * module using ldb_map.
1391 * 'ldb_map_get_ops' returns a copy of ldb_maps module operations.
1393 * It should be called from the initialize function of the using
1394 * module, which should then override the 'init_context' op with a
1395 * function making the appropriate calls to 'ldb_map_init'.