2 ldb database mapping module
4 Copyright (C) Jelmer Vernooij 2005
5 Copyright (C) Martin Kuehl <mkhl@samba.org> 2006
7 ** NOTE! The following LGPL license applies to the ldb
8 ** library. This does NOT imply that all of Samba is released
11 This library is free software; you can redistribute it and/or
12 modify it under the terms of the GNU Lesser General Public
13 License as published by the Free Software Foundation; either
14 version 3 of the License, or (at your option) any later version.
16 This library 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 GNU
19 Lesser General Public License for more details.
21 You should have received a copy of the GNU Lesser General Public
22 License along with this library; if not, see <http://www.gnu.org/licenses/>.
29 * Component: ldb ldb_map module
31 * Description: Map portions of data into a different format on a
34 * Author: Jelmer Vernooij, Martin Kuehl
37 #include "ldb_includes.h"
40 #include "ldb_map_private.h"
46 /* Description of the provided ldb requests:
47 - special attribute 'isMapped'
50 - if parse tree can be split
51 - search remote records w/ remote attrs and parse tree
53 - enumerate all remote records
54 - for each remote result
55 - map remote result to local message
58 - merge local into remote result
59 - run callback on merged result
61 - run callback on remote result
64 - split message into local and remote part
65 - if local message is not empty
66 - add isMapped to local message
71 - split message into local and remote part
72 - if local message is not empty
73 - add isMapped to local message
74 - search for local record
79 - modify remote record
82 - search for local record
85 - delete remote record
88 - search for local record
91 - modify local isMapped
92 - rename remote record
97 /* Private data structures
98 * ======================= */
100 /* Global private data */
101 /* Extract mappings from private data. */
102 const struct ldb_map_context *map_get_context(struct ldb_module *module)
104 const struct map_private *data = talloc_get_type(module->private_data, struct map_private);
105 return data->context;
108 /* Create a generic request context. */
109 static struct map_context *map_init_context(struct ldb_handle *h, struct ldb_request *req)
111 struct map_context *ac;
113 ac = talloc_zero(h, struct map_context);
119 ac->module = h->module;
125 /* Create a search request context. */
126 struct map_search_context *map_init_search_context(struct map_context *ac, struct ldb_reply *ares)
128 struct map_search_context *sc;
130 sc = talloc_zero(ac, struct map_search_context);
137 sc->local_res = NULL;
138 sc->remote_res = ares;
143 /* Create a request context and handle. */
144 struct ldb_handle *map_init_handle(struct ldb_request *req, struct ldb_module *module)
146 struct map_context *ac;
147 struct ldb_handle *h;
149 h = talloc_zero(req, struct ldb_handle);
157 ac = map_init_context(h, req);
163 h->private_data = (void *)ac;
165 h->state = LDB_ASYNC_INIT;
166 h->status = LDB_SUCCESS;
172 /* Dealing with DNs for different partitions
173 * ========================================= */
175 /* Check whether any data should be stored in the local partition. */
176 bool map_check_local_db(struct ldb_module *module)
178 const struct ldb_map_context *data = map_get_context(module);
180 if (!data->remote_base_dn || !data->local_base_dn) {
187 /* Copy a DN with the base DN of the local partition. */
188 static struct ldb_dn *ldb_dn_rebase_local(void *mem_ctx, const struct ldb_map_context *data, struct ldb_dn *dn)
190 struct ldb_dn *new_dn;
192 new_dn = ldb_dn_copy(mem_ctx, dn);
193 if ( ! ldb_dn_validate(new_dn)) {
198 /* may be we don't need to rebase at all */
199 if ( ! data->remote_base_dn || ! data->local_base_dn) {
203 if ( ! ldb_dn_remove_base_components(new_dn, ldb_dn_get_comp_num(data->remote_base_dn))) {
208 if ( ! ldb_dn_add_base(new_dn, data->local_base_dn)) {
216 /* Copy a DN with the base DN of the remote partition. */
217 static struct ldb_dn *ldb_dn_rebase_remote(void *mem_ctx, const struct ldb_map_context *data, struct ldb_dn *dn)
219 struct ldb_dn *new_dn;
221 new_dn = ldb_dn_copy(mem_ctx, dn);
222 if ( ! ldb_dn_validate(new_dn)) {
227 /* may be we don't need to rebase at all */
228 if ( ! data->remote_base_dn || ! data->local_base_dn) {
232 if ( ! ldb_dn_remove_base_components(new_dn, ldb_dn_get_comp_num(data->local_base_dn))) {
237 if ( ! ldb_dn_add_base(new_dn, data->remote_base_dn)) {
245 /* Run a request and make sure it targets the remote partition. */
246 /* TODO: free old DNs and messages? */
247 int ldb_next_remote_request(struct ldb_module *module, struct ldb_request *request)
249 const struct ldb_map_context *data = map_get_context(module);
250 struct ldb_message *msg;
252 switch (request->operation) {
254 if (request->op.search.base) {
255 request->op.search.base = ldb_dn_rebase_remote(request, data, request->op.search.base);
257 request->op.search.base = data->remote_base_dn;
258 /* TODO: adjust scope? */
263 msg = ldb_msg_copy_shallow(request, request->op.add.message);
264 msg->dn = ldb_dn_rebase_remote(msg, data, msg->dn);
265 request->op.add.message = msg;
269 msg = ldb_msg_copy_shallow(request, request->op.mod.message);
270 msg->dn = ldb_dn_rebase_remote(msg, data, msg->dn);
271 request->op.mod.message = msg;
275 request->op.del.dn = ldb_dn_rebase_remote(request, data, request->op.del.dn);
279 request->op.rename.olddn = ldb_dn_rebase_remote(request, data, request->op.rename.olddn);
280 request->op.rename.newdn = ldb_dn_rebase_remote(request, data, request->op.rename.newdn);
284 ldb_debug(module->ldb, LDB_DEBUG_ERROR, "ldb_map: "
285 "Invalid remote request!\n");
286 return LDB_ERR_OPERATIONS_ERROR;
289 return ldb_next_request(module, request);
293 /* Finding mappings for attributes and objectClasses
294 * ================================================= */
296 /* Find an objectClass mapping by the local name. */
297 static const struct ldb_map_objectclass *map_objectclass_find_local(const struct ldb_map_context *data, const char *name)
301 for (i = 0; data->objectclass_maps && data->objectclass_maps[i].local_name; i++) {
302 if (ldb_attr_cmp(data->objectclass_maps[i].local_name, name) == 0) {
303 return &data->objectclass_maps[i];
310 /* Find an objectClass mapping by the remote name. */
311 static const struct ldb_map_objectclass *map_objectclass_find_remote(const struct ldb_map_context *data, const char *name)
315 for (i = 0; data->objectclass_maps && data->objectclass_maps[i].remote_name; i++) {
316 if (ldb_attr_cmp(data->objectclass_maps[i].remote_name, name) == 0) {
317 return &data->objectclass_maps[i];
324 /* Find an attribute mapping by the local name. */
325 const struct ldb_map_attribute *map_attr_find_local(const struct ldb_map_context *data, const char *name)
329 for (i = 0; data->attribute_maps[i].local_name; i++) {
330 if (ldb_attr_cmp(data->attribute_maps[i].local_name, name) == 0) {
331 return &data->attribute_maps[i];
334 for (i = 0; data->attribute_maps[i].local_name; i++) {
335 if (ldb_attr_cmp(data->attribute_maps[i].local_name, "*") == 0) {
336 return &data->attribute_maps[i];
343 /* Find an attribute mapping by the remote name. */
344 const struct ldb_map_attribute *map_attr_find_remote(const struct ldb_map_context *data, const char *name)
346 const struct ldb_map_attribute *map;
347 const struct ldb_map_attribute *wildcard = NULL;
350 for (i = 0; data->attribute_maps[i].local_name; i++) {
351 map = &data->attribute_maps[i];
352 if (ldb_attr_cmp(map->local_name, "*") == 0) {
353 wildcard = &data->attribute_maps[i];
361 if (ldb_attr_cmp(map->local_name, name) == 0) {
368 if (ldb_attr_cmp(map->u.rename.remote_name, name) == 0) {
374 for (j = 0; map->u.generate.remote_names && map->u.generate.remote_names[j]; j++) {
375 if (ldb_attr_cmp(map->u.generate.remote_names[j], name) == 0) {
383 /* We didn't find it, so return the wildcard record if one was configured */
388 /* Mapping attributes
389 * ================== */
391 /* Check whether an attribute will be mapped into the remote partition. */
392 bool map_attr_check_remote(const struct ldb_map_context *data, const char *attr)
394 const struct ldb_map_attribute *map = map_attr_find_local(data, attr);
399 if (map->type == MAP_IGNORE) {
406 /* Map an attribute name into the remote partition. */
407 const char *map_attr_map_local(void *mem_ctx, const struct ldb_map_attribute *map, const char *attr)
410 return talloc_strdup(mem_ctx, attr);
415 return talloc_strdup(mem_ctx, attr);
419 return talloc_strdup(mem_ctx, map->u.rename.remote_name);
426 /* Map an attribute name back into the local partition. */
427 const char *map_attr_map_remote(void *mem_ctx, const struct ldb_map_attribute *map, const char *attr)
430 return talloc_strdup(mem_ctx, attr);
433 if (map->type == MAP_KEEP) {
434 return talloc_strdup(mem_ctx, attr);
437 return talloc_strdup(mem_ctx, map->local_name);
441 /* Merge two lists of attributes into a single one. */
442 int map_attrs_merge(struct ldb_module *module, void *mem_ctx,
443 const char ***attrs, const char * const *more_attrs)
447 for (i = 0; *attrs && (*attrs)[i]; i++) /* noop */ ;
448 for (j = 0; more_attrs && more_attrs[j]; j++) /* noop */ ;
450 *attrs = talloc_realloc(mem_ctx, *attrs, const char *, i+j+1);
451 if (*attrs == NULL) {
456 for (k = 0; k < j; k++) {
457 (*attrs)[i + k] = more_attrs[k];
460 (*attrs)[i+k] = NULL;
465 /* Mapping ldb values
466 * ================== */
468 /* Map an ldb value into the remote partition. */
469 struct ldb_val ldb_val_map_local(struct ldb_module *module, void *mem_ctx,
470 const struct ldb_map_attribute *map, const struct ldb_val *val)
472 if (map && (map->type == MAP_CONVERT) && (map->u.convert.convert_local)) {
473 return map->u.convert.convert_local(module, mem_ctx, val);
476 return ldb_val_dup(mem_ctx, val);
479 /* Map an ldb value back into the local partition. */
480 struct ldb_val ldb_val_map_remote(struct ldb_module *module, void *mem_ctx,
481 const struct ldb_map_attribute *map, const struct ldb_val *val)
483 if (map && (map->type == MAP_CONVERT) && (map->u.convert.convert_remote)) {
484 return map->u.convert.convert_remote(module, mem_ctx, val);
487 return ldb_val_dup(mem_ctx, val);
494 /* Check whether a DN is below the local baseDN. */
495 bool ldb_dn_check_local(struct ldb_module *module, struct ldb_dn *dn)
497 const struct ldb_map_context *data = map_get_context(module);
499 if (!data->local_base_dn) {
503 return ldb_dn_compare_base(data->local_base_dn, dn) == 0;
506 /* Map a DN into the remote partition. */
507 struct ldb_dn *ldb_dn_map_local(struct ldb_module *module, void *mem_ctx, struct ldb_dn *dn)
509 const struct ldb_map_context *data = map_get_context(module);
510 struct ldb_dn *newdn;
511 const struct ldb_map_attribute *map;
512 enum ldb_map_attr_type map_type;
514 struct ldb_val value;
521 newdn = ldb_dn_copy(mem_ctx, dn);
527 /* For each RDN, map the component name and possibly the value */
528 for (i = 0; i < ldb_dn_get_comp_num(newdn); i++) {
529 map = map_attr_find_local(data, ldb_dn_get_component_name(dn, i));
531 /* Unknown attribute - leave this RDN as is and hope the best... */
535 map_type = map->type;
541 ldb_debug(module->ldb, LDB_DEBUG_ERROR, "ldb_map: "
542 "MAP_IGNORE/MAP_GENERATE attribute '%s' "
543 "used in DN!\n", ldb_dn_get_component_name(dn, i));
547 if (map->u.convert.convert_local == NULL) {
548 ldb_debug(module->ldb, LDB_DEBUG_ERROR, "ldb_map: "
549 "'convert_local' not set for attribute '%s' "
550 "used in DN!\n", ldb_dn_get_component_name(dn, i));
556 name = map_attr_map_local(newdn, map, ldb_dn_get_component_name(dn, i));
557 if (name == NULL) goto failed;
559 value = ldb_val_map_local(module, newdn, map, ldb_dn_get_component_val(dn, i));
560 if (value.data == NULL) goto failed;
562 ret = ldb_dn_set_component(newdn, i, name, value);
563 if (ret != LDB_SUCCESS) {
578 /* Map a DN into the local partition. */
579 struct ldb_dn *ldb_dn_map_remote(struct ldb_module *module, void *mem_ctx, struct ldb_dn *dn)
581 const struct ldb_map_context *data = map_get_context(module);
582 struct ldb_dn *newdn;
583 const struct ldb_map_attribute *map;
584 enum ldb_map_attr_type map_type;
586 struct ldb_val value;
593 newdn = ldb_dn_copy(mem_ctx, dn);
599 /* For each RDN, map the component name and possibly the value */
600 for (i = 0; i < ldb_dn_get_comp_num(newdn); i++) {
601 map = map_attr_find_remote(data, ldb_dn_get_component_name(dn, i));
603 /* Unknown attribute - leave this RDN as is and hope the best... */
607 map_type = map->type;
613 ldb_debug(module->ldb, LDB_DEBUG_ERROR, "ldb_map: "
614 "MAP_IGNORE/MAP_GENERATE attribute '%s' "
615 "used in DN!\n", ldb_dn_get_component_name(dn, i));
619 if (map->u.convert.convert_remote == NULL) {
620 ldb_debug(module->ldb, LDB_DEBUG_ERROR, "ldb_map: "
621 "'convert_remote' not set for attribute '%s' "
622 "used in DN!\n", ldb_dn_get_component_name(dn, i));
628 name = map_attr_map_remote(newdn, map, ldb_dn_get_component_name(dn, i));
629 if (name == NULL) goto failed;
631 value = ldb_val_map_remote(module, newdn, map, ldb_dn_get_component_val(dn, i));
632 if (value.data == NULL) goto failed;
634 ret = ldb_dn_set_component(newdn, i, name, value);
635 if (ret != LDB_SUCCESS) {
650 /* Map a DN and its base into the local partition. */
651 /* TODO: This should not be required with GUIDs. */
652 struct ldb_dn *ldb_dn_map_rebase_remote(struct ldb_module *module, void *mem_ctx, struct ldb_dn *dn)
654 const struct ldb_map_context *data = map_get_context(module);
655 struct ldb_dn *dn1, *dn2;
657 dn1 = ldb_dn_rebase_local(mem_ctx, data, dn);
658 dn2 = ldb_dn_map_remote(module, mem_ctx, dn1);
665 /* Converting DNs and objectClasses (as ldb values)
666 * ================================================ */
668 /* Map a DN contained in an ldb value into the remote partition. */
669 static struct ldb_val ldb_dn_convert_local(struct ldb_module *module, void *mem_ctx, const struct ldb_val *val)
671 struct ldb_dn *dn, *newdn;
672 struct ldb_val newval;
674 dn = ldb_dn_new(mem_ctx, module->ldb, (char *)val->data);
675 if (! ldb_dn_validate(dn)) {
681 newdn = ldb_dn_map_local(module, mem_ctx, dn);
685 newval.data = (uint8_t *)ldb_dn_alloc_linearized(mem_ctx, newdn);
687 newval.length = strlen((char *)newval.data);
694 /* Map a DN contained in an ldb value into the local partition. */
695 static struct ldb_val ldb_dn_convert_remote(struct ldb_module *module, void *mem_ctx, const struct ldb_val *val)
697 struct ldb_dn *dn, *newdn;
698 struct ldb_val newval;
700 dn = ldb_dn_new(mem_ctx, module->ldb, (char *)val->data);
701 if (! ldb_dn_validate(dn)) {
707 newdn = ldb_dn_map_remote(module, mem_ctx, dn);
711 newval.data = (uint8_t *)ldb_dn_alloc_linearized(mem_ctx, newdn);
713 newval.length = strlen((char *)newval.data);
720 /* Map an objectClass into the remote partition. */
721 static struct ldb_val map_objectclass_convert_local(struct ldb_module *module, void *mem_ctx, const struct ldb_val *val)
723 const struct ldb_map_context *data = map_get_context(module);
724 const char *name = (char *)val->data;
725 const struct ldb_map_objectclass *map = map_objectclass_find_local(data, name);
726 struct ldb_val newval;
729 newval.data = (uint8_t*)talloc_strdup(mem_ctx, map->remote_name);
730 newval.length = strlen((char *)newval.data);
734 return ldb_val_dup(mem_ctx, val);
737 /* Generate a remote message with a mapped objectClass. */
738 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)
740 const struct ldb_map_context *data = map_get_context(module);
741 struct ldb_message_element *el, *oc;
743 bool found_extensibleObject = false;
746 /* Find old local objectClass */
747 oc = ldb_msg_find_element(old, "objectClass");
752 /* Prepare new element */
753 el = talloc_zero(remote, struct ldb_message_element);
755 ldb_oom(module->ldb);
756 return; /* TODO: fail? */
759 /* Copy local objectClass element, reverse space for an extra value */
760 el->num_values = oc->num_values + 1;
761 el->values = talloc_array(el, struct ldb_val, el->num_values);
762 if (el->values == NULL) {
764 ldb_oom(module->ldb);
765 return; /* TODO: fail? */
768 /* Copy local element name "objectClass" */
769 el->name = talloc_strdup(el, local_attr);
771 /* Convert all local objectClasses */
772 for (i = 0; i < el->num_values - 1; i++) {
773 el->values[i] = map_objectclass_convert_local(module, el->values, &oc->values[i]);
774 if (ldb_attr_cmp((char *)el->values[i].data, data->add_objectclass) == 0) {
775 found_extensibleObject = true;
779 if (!found_extensibleObject) {
780 val.data = (uint8_t *)talloc_strdup(el->values, data->add_objectclass);
781 val.length = strlen((char *)val.data);
783 /* Append additional objectClass data->add_objectclass */
789 /* Add new objectClass to remote message */
790 ldb_msg_add(remote, el, 0);
793 /* Map an objectClass into the local partition. */
794 static struct ldb_val map_objectclass_convert_remote(struct ldb_module *module, void *mem_ctx, const struct ldb_val *val)
796 const struct ldb_map_context *data = map_get_context(module);
797 const char *name = (char *)val->data;
798 const struct ldb_map_objectclass *map = map_objectclass_find_remote(data, name);
799 struct ldb_val newval;
802 newval.data = (uint8_t*)talloc_strdup(mem_ctx, map->local_name);
803 newval.length = strlen((char *)newval.data);
807 return ldb_val_dup(mem_ctx, val);
810 /* Generate a local message with a mapped objectClass. */
811 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)
813 struct ldb_message_element *el, *oc;
817 /* Find old remote objectClass */
818 oc = ldb_msg_find_element(remote, "objectClass");
823 /* Prepare new element */
824 el = talloc_zero(mem_ctx, struct ldb_message_element);
826 ldb_oom(module->ldb);
830 /* Copy remote objectClass element */
831 el->num_values = oc->num_values;
832 el->values = talloc_array(el, struct ldb_val, el->num_values);
833 if (el->values == NULL) {
835 ldb_oom(module->ldb);
839 /* Copy remote element name "objectClass" */
840 el->name = talloc_strdup(el, local_attr);
842 /* Convert all remote objectClasses */
843 for (i = 0; i < el->num_values; i++) {
844 el->values[i] = map_objectclass_convert_remote(module, el->values, &oc->values[i]);
847 val.data = (uint8_t *)talloc_strdup(el->values, "extensibleObject");
848 val.length = strlen((char *)val.data);
850 /* Remove last value if it was "extensibleObject" */
851 if (ldb_val_equal_exact(&val, &el->values[i-1])) {
853 el->values = talloc_realloc(el, el->values, struct ldb_val, el->num_values);
854 if (el->values == NULL) {
856 ldb_oom(module->ldb);
864 static const struct ldb_map_attribute objectclass_convert_map = {
865 .local_name = "objectClass",
869 .remote_name = "objectClass",
870 .convert_local = map_objectclass_convert_local,
871 .convert_remote = map_objectclass_convert_remote,
877 /* Mappings for searches on objectClass= assuming a one-to-one
878 * mapping. Needed because this is a generate operator for the
880 static int map_objectclass_convert_operator(struct ldb_module *module, void *mem_ctx,
881 struct ldb_parse_tree **new, const struct ldb_parse_tree *tree)
884 return map_subtree_collect_remote_simple(module, mem_ctx, new, tree, &objectclass_convert_map);
887 /* Auxiliary request construction
888 * ============================== */
890 /* Store the DN of a single search result in context. */
891 static int map_search_self_callback(struct ldb_context *ldb, void *context, struct ldb_reply *ares)
893 struct map_context *ac;
895 if (context == NULL || ares == NULL) {
896 ldb_set_errstring(ldb, talloc_asprintf(ldb, "NULL Context or Result in callback"));
897 return LDB_ERR_OPERATIONS_ERROR;
900 ac = talloc_get_type(context, struct map_context);
902 /* We are interested only in the single reply */
903 if (ares->type != LDB_REPLY_ENTRY) {
908 /* We have already found a remote DN */
910 ldb_set_errstring(ldb, talloc_asprintf(ldb, "Too many results to base search"));
912 return LDB_ERR_OPERATIONS_ERROR;
916 ac->local_dn = ares->message->dn;
921 /* Build a request to search a record by its DN. */
922 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)
924 struct ldb_request *req;
926 req = talloc_zero(ac, struct ldb_request);
932 req->operation = LDB_SEARCH;
933 req->op.search.base = dn;
934 req->op.search.scope = LDB_SCOPE_BASE;
935 req->op.search.attrs = attrs;
938 req->op.search.tree = tree;
940 req->op.search.tree = ldb_parse_tree(req, NULL);
941 if (req->op.search.tree == NULL) {
947 req->controls = NULL;
948 req->context = context;
949 req->callback = callback;
950 ldb_set_timeout_from_prev_req(ac->module->ldb, ac->orig_req, req);
955 /* Build a request to search the local record by its DN. */
956 struct ldb_request *map_search_self_req(struct map_context *ac, struct ldb_dn *dn)
958 /* attrs[] is returned from this function in
959 * ac->search_req->op.search.attrs, so it must be static, as
960 * otherwise the compiler can put it on the stack */
961 static const char * const attrs[] = { IS_MAPPED, NULL };
962 struct ldb_parse_tree *tree;
964 /* Limit search to records with 'IS_MAPPED' present */
965 /* TODO: `tree = ldb_parse_tree(ac, IS_MAPPED);' won't do. */
966 tree = talloc_zero(ac, struct ldb_parse_tree);
972 tree->operation = LDB_OP_PRESENT;
973 tree->u.present.attr = talloc_strdup(tree, IS_MAPPED);
975 return map_search_base_req(ac, dn, attrs, tree, ac, map_search_self_callback);
978 /* Build a request to update the 'IS_MAPPED' attribute */
979 struct ldb_request *map_build_fixup_req(struct map_context *ac, struct ldb_dn *olddn, struct ldb_dn *newdn)
981 struct ldb_request *req;
982 struct ldb_message *msg;
985 /* Prepare request */
986 req = talloc_zero(ac, struct ldb_request);
992 /* Prepare message */
993 msg = ldb_msg_new(req);
999 /* Update local 'IS_MAPPED' to the new remote DN */
1000 msg->dn = ldb_dn_copy(msg, olddn);
1001 dn = ldb_dn_alloc_linearized(msg, newdn);
1002 if ( ! dn || ! ldb_dn_validate(msg->dn)) {
1005 if (ldb_msg_add_empty(msg, IS_MAPPED, LDB_FLAG_MOD_REPLACE, NULL) != 0) {
1008 if (ldb_msg_add_string(msg, IS_MAPPED, dn) != 0) {
1012 req->operation = LDB_MODIFY;
1013 req->op.mod.message = msg;
1014 req->controls = NULL;
1016 req->context = NULL;
1017 req->callback = NULL;
1027 /* Asynchronous call structure
1028 * =========================== */
1030 /* Figure out which request is currently pending. */
1031 static struct ldb_request *map_get_req(struct map_context *ac)
1034 case MAP_SEARCH_SELF_MODIFY:
1035 case MAP_SEARCH_SELF_DELETE:
1036 case MAP_SEARCH_SELF_RENAME:
1037 return ac->search_req;
1039 case MAP_ADD_REMOTE:
1040 case MAP_MODIFY_REMOTE:
1041 case MAP_DELETE_REMOTE:
1042 case MAP_RENAME_REMOTE:
1043 return ac->remote_req;
1045 case MAP_RENAME_FIXUP:
1046 return ac->down_req;
1049 case MAP_MODIFY_LOCAL:
1050 case MAP_DELETE_LOCAL:
1051 case MAP_RENAME_LOCAL:
1052 return ac->local_req;
1054 case MAP_SEARCH_REMOTE:
1059 return NULL; /* unreachable; silences a warning */
1062 typedef int (*map_next_function)(struct ldb_handle *handle);
1064 /* Figure out the next request to run. */
1065 static map_next_function map_get_next(struct map_context *ac)
1068 case MAP_SEARCH_REMOTE:
1072 return map_add_do_remote;
1073 case MAP_ADD_REMOTE:
1076 case MAP_SEARCH_SELF_MODIFY:
1077 return map_modify_do_local;
1078 case MAP_MODIFY_LOCAL:
1079 return map_modify_do_remote;
1080 case MAP_MODIFY_REMOTE:
1083 case MAP_SEARCH_SELF_DELETE:
1084 return map_delete_do_local;
1085 case MAP_DELETE_LOCAL:
1086 return map_delete_do_remote;
1087 case MAP_DELETE_REMOTE:
1090 case MAP_SEARCH_SELF_RENAME:
1091 return map_rename_do_local;
1092 case MAP_RENAME_LOCAL:
1093 return map_rename_do_fixup;
1094 case MAP_RENAME_FIXUP:
1095 return map_rename_do_remote;
1096 case MAP_RENAME_REMOTE:
1100 return NULL; /* unreachable; silences a warning */
1103 /* Wait for the current pending request to finish and continue with the next. */
1104 static int map_wait_next(struct ldb_handle *handle)
1106 struct map_context *ac;
1107 struct ldb_request *req;
1108 map_next_function next;
1111 if (handle == NULL || handle->private_data == NULL) {
1112 return LDB_ERR_OPERATIONS_ERROR;
1115 if (handle->state == LDB_ASYNC_DONE) {
1116 return handle->status;
1119 handle->state = LDB_ASYNC_PENDING;
1120 handle->status = LDB_SUCCESS;
1122 ac = talloc_get_type(handle->private_data, struct map_context);
1124 if (ac->step == MAP_SEARCH_REMOTE) {
1126 for (i = 0; i < ac->num_searches; i++) {
1127 req = ac->search_reqs[i];
1128 ret = ldb_wait(req->handle, LDB_WAIT_NONE);
1130 if (ret != LDB_SUCCESS) {
1131 handle->status = ret;
1134 if (req->handle->status != LDB_SUCCESS) {
1135 handle->status = req->handle->status;
1138 if (req->handle->state != LDB_ASYNC_DONE) {
1144 req = map_get_req(ac);
1146 ret = ldb_wait(req->handle, LDB_WAIT_NONE);
1148 if (ret != LDB_SUCCESS) {
1149 handle->status = ret;
1152 if (req->handle->status != LDB_SUCCESS) {
1153 handle->status = req->handle->status;
1156 if (req->handle->state != LDB_ASYNC_DONE) {
1160 next = map_get_next(ac);
1162 return next(handle);
1169 handle->state = LDB_ASYNC_DONE;
1173 /* Wait for all current pending requests to finish. */
1174 static int map_wait_all(struct ldb_handle *handle)
1178 while (handle->state != LDB_ASYNC_DONE) {
1179 ret = map_wait_next(handle);
1180 if (ret != LDB_SUCCESS) {
1185 return handle->status;
1188 /* Wait for pending requests to finish. */
1189 int map_wait(struct ldb_handle *handle, enum ldb_wait_type type)
1191 if (type == LDB_WAIT_ALL) {
1192 return map_wait_all(handle);
1194 return map_wait_next(handle);
1199 /* Module initialization
1200 * ===================== */
1203 /* Builtin mappings for DNs and objectClasses */
1204 static const struct ldb_map_attribute builtin_attribute_maps[] = {
1207 .type = MAP_CONVERT,
1210 .remote_name = "dn",
1211 .convert_local = ldb_dn_convert_local,
1212 .convert_remote = ldb_dn_convert_remote,
1221 static const struct ldb_map_attribute objectclass_attribute_map = {
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,
1235 /* Find the special 'MAP_DN_NAME' record and store local and remote
1236 * base DNs in private data. */
1237 static int map_init_dns(struct ldb_module *module, struct ldb_map_context *data, const char *name)
1239 static const char * const attrs[] = { MAP_DN_FROM, MAP_DN_TO, NULL };
1241 struct ldb_message *msg;
1242 struct ldb_result *res;
1246 data->local_base_dn = NULL;
1247 data->remote_base_dn = NULL;
1251 dn = ldb_dn_new_fmt(data, module->ldb, "%s=%s", MAP_DN_NAME, name);
1252 if ( ! ldb_dn_validate(dn)) {
1253 ldb_debug(module->ldb, LDB_DEBUG_ERROR, "ldb_map: "
1254 "Failed to construct '%s' DN!\n", MAP_DN_NAME);
1255 return LDB_ERR_OPERATIONS_ERROR;
1258 ret = ldb_search(module->ldb, dn, LDB_SCOPE_BASE, NULL, attrs, &res);
1260 if (ret != LDB_SUCCESS) {
1263 if (res->count == 0) {
1264 ldb_debug(module->ldb, LDB_DEBUG_ERROR, "ldb_map: "
1265 "No results for '%s=%s'!\n", MAP_DN_NAME, name);
1267 return LDB_ERR_CONSTRAINT_VIOLATION;
1269 if (res->count > 1) {
1270 ldb_debug(module->ldb, LDB_DEBUG_ERROR, "ldb_map: "
1271 "Too many results for '%s=%s'!\n", MAP_DN_NAME, name);
1273 return LDB_ERR_CONSTRAINT_VIOLATION;
1277 data->local_base_dn = ldb_msg_find_attr_as_dn(module->ldb, data, msg, MAP_DN_FROM);
1278 data->remote_base_dn = ldb_msg_find_attr_as_dn(module->ldb, data, msg, MAP_DN_TO);
1284 /* Store attribute maps and objectClass maps in private data. */
1285 static int map_init_maps(struct ldb_module *module, struct ldb_map_context *data,
1286 const struct ldb_map_attribute *attrs,
1287 const struct ldb_map_objectclass *ocls,
1288 const char * const *wildcard_attributes)
1293 /* Count specified attribute maps */
1294 for (i = 0; attrs[i].local_name; i++) /* noop */ ;
1295 /* Count built-in attribute maps */
1296 for (j = 0; builtin_attribute_maps[j].local_name; j++) /* noop */ ;
1298 /* Store list of attribute maps */
1299 data->attribute_maps = talloc_array(data, struct ldb_map_attribute, i+j+2);
1300 if (data->attribute_maps == NULL) {
1302 return LDB_ERR_OPERATIONS_ERROR;
1305 /* Specified ones go first */
1306 for (i = 0; attrs[i].local_name; i++) {
1307 data->attribute_maps[last] = attrs[i];
1311 /* Built-in ones go last */
1312 for (i = 0; builtin_attribute_maps[i].local_name; i++) {
1313 data->attribute_maps[last] = builtin_attribute_maps[i];
1317 if (data->add_objectclass) {
1318 /* ObjectClass one is very last, if required */
1319 data->attribute_maps[last] = objectclass_attribute_map;
1322 data->attribute_maps[last] = objectclass_convert_map;
1326 /* Ensure 'local_name == NULL' for the last entry */
1327 memset(&data->attribute_maps[last], 0, sizeof(struct ldb_map_attribute));
1329 /* Store list of objectClass maps */
1330 data->objectclass_maps = ocls;
1332 data->wildcard_attributes = wildcard_attributes;
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,
1341 const char *add_objectclass,
1344 struct map_private *data;
1347 /* Prepare private data */
1348 data = talloc_zero(module, struct map_private);
1351 return LDB_ERR_OPERATIONS_ERROR;
1354 module->private_data = data;
1356 data->context = talloc_zero(data, struct ldb_map_context);
1357 if (!data->context) {
1359 return LDB_ERR_OPERATIONS_ERROR;
1362 /* Store local and remote baseDNs */
1363 ret = map_init_dns(module, data->context, name);
1364 if (ret != LDB_SUCCESS) {
1369 data->context->add_objectclass = add_objectclass;
1371 /* Store list of attribute and objectClass maps */
1372 ret = map_init_maps(module, data->context, attrs, ocls, wildcard_attributes);
1373 if (ret != LDB_SUCCESS) {