2 ldb database mapping module
4 Copyright (C) Jelmer Vernooij 2005
5 Copyright (C) Martin Kuehl <mkhl@samba.org> 2006
6 Copyright (C) Simo Sorce 2008
8 ** NOTE! The following LGPL license applies to the ldb
9 ** library. This does NOT imply that all of Samba is released
12 This library is free software; you can redistribute it and/or
13 modify it under the terms of the GNU Lesser General Public
14 License as published by the Free Software Foundation; either
15 version 3 of the License, or (at your option) any later version.
17 This library is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 Lesser General Public License for more details.
22 You should have received a copy of the GNU Lesser General Public
23 License along with this library; if not, see <http://www.gnu.org/licenses/>.
30 * Component: ldb ldb_map module
32 * Description: Map portions of data into a different format on a
35 * Author: Jelmer Vernooij, Martin Kuehl
38 #include "ldb_includes.h"
41 #include "ldb_map_private.h"
47 /* Description of the provided ldb requests:
48 - special attribute 'isMapped'
51 - if parse tree can be split
52 - search remote records w/ remote attrs and parse tree
54 - enumerate all remote records
55 - for each remote result
56 - map remote result to local message
59 - merge local into remote result
60 - run callback on merged result
62 - run callback on remote result
65 - split message into local and remote part
66 - if local message is not empty
67 - add isMapped to local message
72 - split message into local and remote part
73 - if local message is not empty
74 - add isMapped to local message
75 - search for local record
80 - modify remote record
83 - search for local record
86 - delete remote record
89 - search for local record
92 - modify local isMapped
93 - rename remote record
98 /* Private data structures
99 * ======================= */
101 /* Global private data */
102 /* Extract mappings from private data. */
103 const struct ldb_map_context *map_get_context(struct ldb_module *module)
105 const struct map_private *data = talloc_get_type(module->private_data, struct map_private);
106 return data->context;
109 /* Create a generic request context. */
110 struct map_context *map_init_context(struct ldb_module *module,
111 struct ldb_request *req)
113 struct map_context *ac;
115 ac = talloc_zero(req, struct map_context);
117 ldb_set_errstring(module->ldb, "Out of Memory");
127 /* Dealing with DNs for different partitions
128 * ========================================= */
130 /* Check whether any data should be stored in the local partition. */
131 bool map_check_local_db(struct ldb_module *module)
133 const struct ldb_map_context *data = map_get_context(module);
135 if (!data->remote_base_dn || !data->local_base_dn) {
142 /* Copy a DN with the base DN of the local partition. */
143 static struct ldb_dn *ldb_dn_rebase_local(void *mem_ctx, const struct ldb_map_context *data, struct ldb_dn *dn)
145 struct ldb_dn *new_dn;
147 new_dn = ldb_dn_copy(mem_ctx, dn);
148 if ( ! ldb_dn_validate(new_dn)) {
153 /* may be we don't need to rebase at all */
154 if ( ! data->remote_base_dn || ! data->local_base_dn) {
158 if ( ! ldb_dn_remove_base_components(new_dn, ldb_dn_get_comp_num(data->remote_base_dn))) {
163 if ( ! ldb_dn_add_base(new_dn, data->local_base_dn)) {
171 /* Copy a DN with the base DN of the remote partition. */
172 static struct ldb_dn *ldb_dn_rebase_remote(void *mem_ctx, const struct ldb_map_context *data, struct ldb_dn *dn)
174 struct ldb_dn *new_dn;
176 new_dn = ldb_dn_copy(mem_ctx, dn);
177 if ( ! ldb_dn_validate(new_dn)) {
182 /* may be we don't need to rebase at all */
183 if ( ! data->remote_base_dn || ! data->local_base_dn) {
187 if ( ! ldb_dn_remove_base_components(new_dn, ldb_dn_get_comp_num(data->local_base_dn))) {
192 if ( ! ldb_dn_add_base(new_dn, data->remote_base_dn)) {
200 /* Run a request and make sure it targets the remote partition. */
201 /* TODO: free old DNs and messages? */
202 int ldb_next_remote_request(struct ldb_module *module, struct ldb_request *request)
204 const struct ldb_map_context *data = map_get_context(module);
205 struct ldb_message *msg;
207 switch (request->operation) {
209 if (request->op.search.base) {
210 request->op.search.base = ldb_dn_rebase_remote(request, data, request->op.search.base);
212 request->op.search.base = data->remote_base_dn;
213 /* TODO: adjust scope? */
218 msg = ldb_msg_copy_shallow(request, request->op.add.message);
219 msg->dn = ldb_dn_rebase_remote(msg, data, msg->dn);
220 request->op.add.message = msg;
224 msg = ldb_msg_copy_shallow(request, request->op.mod.message);
225 msg->dn = ldb_dn_rebase_remote(msg, data, msg->dn);
226 request->op.mod.message = msg;
230 request->op.del.dn = ldb_dn_rebase_remote(request, data, request->op.del.dn);
234 request->op.rename.olddn = ldb_dn_rebase_remote(request, data, request->op.rename.olddn);
235 request->op.rename.newdn = ldb_dn_rebase_remote(request, data, request->op.rename.newdn);
239 ldb_debug(module->ldb, LDB_DEBUG_ERROR, "ldb_map: "
240 "Invalid remote request!\n");
241 return LDB_ERR_OPERATIONS_ERROR;
244 return ldb_next_request(module, request);
248 /* Finding mappings for attributes and objectClasses
249 * ================================================= */
251 /* Find an objectClass mapping by the local name. */
252 static const struct ldb_map_objectclass *map_objectclass_find_local(const struct ldb_map_context *data, const char *name)
256 for (i = 0; data->objectclass_maps && data->objectclass_maps[i].local_name; i++) {
257 if (ldb_attr_cmp(data->objectclass_maps[i].local_name, name) == 0) {
258 return &data->objectclass_maps[i];
265 /* Find an objectClass mapping by the remote name. */
266 static const struct ldb_map_objectclass *map_objectclass_find_remote(const struct ldb_map_context *data, const char *name)
270 for (i = 0; data->objectclass_maps && data->objectclass_maps[i].remote_name; i++) {
271 if (ldb_attr_cmp(data->objectclass_maps[i].remote_name, name) == 0) {
272 return &data->objectclass_maps[i];
279 /* Find an attribute mapping by the local name. */
280 const struct ldb_map_attribute *map_attr_find_local(const struct ldb_map_context *data, const char *name)
284 for (i = 0; data->attribute_maps[i].local_name; i++) {
285 if (ldb_attr_cmp(data->attribute_maps[i].local_name, name) == 0) {
286 return &data->attribute_maps[i];
289 for (i = 0; data->attribute_maps[i].local_name; i++) {
290 if (ldb_attr_cmp(data->attribute_maps[i].local_name, "*") == 0) {
291 return &data->attribute_maps[i];
298 /* Find an attribute mapping by the remote name. */
299 const struct ldb_map_attribute *map_attr_find_remote(const struct ldb_map_context *data, const char *name)
301 const struct ldb_map_attribute *map;
302 const struct ldb_map_attribute *wildcard = NULL;
305 for (i = 0; data->attribute_maps[i].local_name; i++) {
306 map = &data->attribute_maps[i];
307 if (ldb_attr_cmp(map->local_name, "*") == 0) {
308 wildcard = &data->attribute_maps[i];
316 if (ldb_attr_cmp(map->local_name, name) == 0) {
323 if (ldb_attr_cmp(map->u.rename.remote_name, name) == 0) {
329 for (j = 0; map->u.generate.remote_names && map->u.generate.remote_names[j]; j++) {
330 if (ldb_attr_cmp(map->u.generate.remote_names[j], name) == 0) {
338 /* We didn't find it, so return the wildcard record if one was configured */
343 /* Mapping attributes
344 * ================== */
346 /* Check whether an attribute will be mapped into the remote partition. */
347 bool map_attr_check_remote(const struct ldb_map_context *data, const char *attr)
349 const struct ldb_map_attribute *map = map_attr_find_local(data, attr);
354 if (map->type == MAP_IGNORE) {
361 /* Map an attribute name into the remote partition. */
362 const char *map_attr_map_local(void *mem_ctx, const struct ldb_map_attribute *map, const char *attr)
365 return talloc_strdup(mem_ctx, attr);
370 return talloc_strdup(mem_ctx, attr);
374 return talloc_strdup(mem_ctx, map->u.rename.remote_name);
381 /* Map an attribute name back into the local partition. */
382 const char *map_attr_map_remote(void *mem_ctx, const struct ldb_map_attribute *map, const char *attr)
385 return talloc_strdup(mem_ctx, attr);
388 if (map->type == MAP_KEEP) {
389 return talloc_strdup(mem_ctx, attr);
392 return talloc_strdup(mem_ctx, map->local_name);
396 /* Merge two lists of attributes into a single one. */
397 int map_attrs_merge(struct ldb_module *module, void *mem_ctx,
398 const char ***attrs, const char * const *more_attrs)
402 for (i = 0; *attrs && (*attrs)[i]; i++) /* noop */ ;
403 for (j = 0; more_attrs && more_attrs[j]; j++) /* noop */ ;
405 *attrs = talloc_realloc(mem_ctx, *attrs, const char *, i+j+1);
406 if (*attrs == NULL) {
411 for (k = 0; k < j; k++) {
412 (*attrs)[i + k] = more_attrs[k];
415 (*attrs)[i+k] = NULL;
420 /* Mapping ldb values
421 * ================== */
423 /* Map an ldb value into the remote partition. */
424 struct ldb_val ldb_val_map_local(struct ldb_module *module, void *mem_ctx,
425 const struct ldb_map_attribute *map, const struct ldb_val *val)
427 if (map && (map->type == MAP_CONVERT) && (map->u.convert.convert_local)) {
428 return map->u.convert.convert_local(module, mem_ctx, val);
431 return ldb_val_dup(mem_ctx, val);
434 /* Map an ldb value back into the local partition. */
435 struct ldb_val ldb_val_map_remote(struct ldb_module *module, void *mem_ctx,
436 const struct ldb_map_attribute *map, const struct ldb_val *val)
438 if (map && (map->type == MAP_CONVERT) && (map->u.convert.convert_remote)) {
439 return map->u.convert.convert_remote(module, mem_ctx, val);
442 return ldb_val_dup(mem_ctx, val);
449 /* Check whether a DN is below the local baseDN. */
450 bool ldb_dn_check_local(struct ldb_module *module, struct ldb_dn *dn)
452 const struct ldb_map_context *data = map_get_context(module);
454 if (!data->local_base_dn) {
458 return ldb_dn_compare_base(data->local_base_dn, dn) == 0;
461 /* Map a DN into the remote partition. */
462 struct ldb_dn *ldb_dn_map_local(struct ldb_module *module, void *mem_ctx, struct ldb_dn *dn)
464 const struct ldb_map_context *data = map_get_context(module);
465 struct ldb_dn *newdn;
466 const struct ldb_map_attribute *map;
467 enum ldb_map_attr_type map_type;
469 struct ldb_val value;
476 newdn = ldb_dn_copy(mem_ctx, dn);
482 /* For each RDN, map the component name and possibly the value */
483 for (i = 0; i < ldb_dn_get_comp_num(newdn); i++) {
484 map = map_attr_find_local(data, ldb_dn_get_component_name(dn, i));
486 /* Unknown attribute - leave this RDN as is and hope the best... */
490 map_type = map->type;
496 ldb_debug(module->ldb, LDB_DEBUG_ERROR, "ldb_map: "
497 "MAP_IGNORE/MAP_GENERATE attribute '%s' "
498 "used in DN!\n", ldb_dn_get_component_name(dn, i));
502 if (map->u.convert.convert_local == NULL) {
503 ldb_debug(module->ldb, LDB_DEBUG_ERROR, "ldb_map: "
504 "'convert_local' not set for attribute '%s' "
505 "used in DN!\n", ldb_dn_get_component_name(dn, i));
511 name = map_attr_map_local(newdn, map, ldb_dn_get_component_name(dn, i));
512 if (name == NULL) goto failed;
514 value = ldb_val_map_local(module, newdn, map, ldb_dn_get_component_val(dn, i));
515 if (value.data == NULL) goto failed;
517 ret = ldb_dn_set_component(newdn, i, name, value);
518 if (ret != LDB_SUCCESS) {
533 /* Map a DN into the local partition. */
534 struct ldb_dn *ldb_dn_map_remote(struct ldb_module *module, void *mem_ctx, struct ldb_dn *dn)
536 const struct ldb_map_context *data = map_get_context(module);
537 struct ldb_dn *newdn;
538 const struct ldb_map_attribute *map;
539 enum ldb_map_attr_type map_type;
541 struct ldb_val value;
548 newdn = ldb_dn_copy(mem_ctx, dn);
554 /* For each RDN, map the component name and possibly the value */
555 for (i = 0; i < ldb_dn_get_comp_num(newdn); i++) {
556 map = map_attr_find_remote(data, ldb_dn_get_component_name(dn, i));
558 /* Unknown attribute - leave this RDN as is and hope the best... */
562 map_type = map->type;
568 ldb_debug(module->ldb, LDB_DEBUG_ERROR, "ldb_map: "
569 "MAP_IGNORE/MAP_GENERATE attribute '%s' "
570 "used in DN!\n", ldb_dn_get_component_name(dn, i));
574 if (map->u.convert.convert_remote == NULL) {
575 ldb_debug(module->ldb, LDB_DEBUG_ERROR, "ldb_map: "
576 "'convert_remote' not set for attribute '%s' "
577 "used in DN!\n", ldb_dn_get_component_name(dn, i));
583 name = map_attr_map_remote(newdn, map, ldb_dn_get_component_name(dn, i));
584 if (name == NULL) goto failed;
586 value = ldb_val_map_remote(module, newdn, map, ldb_dn_get_component_val(dn, i));
587 if (value.data == NULL) goto failed;
589 ret = ldb_dn_set_component(newdn, i, name, value);
590 if (ret != LDB_SUCCESS) {
605 /* Map a DN and its base into the local partition. */
606 /* TODO: This should not be required with GUIDs. */
607 struct ldb_dn *ldb_dn_map_rebase_remote(struct ldb_module *module, void *mem_ctx, struct ldb_dn *dn)
609 const struct ldb_map_context *data = map_get_context(module);
610 struct ldb_dn *dn1, *dn2;
612 dn1 = ldb_dn_rebase_local(mem_ctx, data, dn);
613 dn2 = ldb_dn_map_remote(module, mem_ctx, dn1);
620 /* Converting DNs and objectClasses (as ldb values)
621 * ================================================ */
623 /* Map a DN contained in an ldb value into the remote partition. */
624 static struct ldb_val ldb_dn_convert_local(struct ldb_module *module, void *mem_ctx, const struct ldb_val *val)
626 struct ldb_dn *dn, *newdn;
627 struct ldb_val newval;
629 dn = ldb_dn_from_ldb_val(mem_ctx, module->ldb, val);
630 if (! ldb_dn_validate(dn)) {
636 newdn = ldb_dn_map_local(module, mem_ctx, dn);
640 newval.data = (uint8_t *)ldb_dn_alloc_linearized(mem_ctx, newdn);
642 newval.length = strlen((char *)newval.data);
649 /* Map a DN contained in an ldb value into the local partition. */
650 static struct ldb_val ldb_dn_convert_remote(struct ldb_module *module, void *mem_ctx, const struct ldb_val *val)
652 struct ldb_dn *dn, *newdn;
653 struct ldb_val newval;
655 dn = ldb_dn_from_ldb_val(mem_ctx, module->ldb, val);
656 if (! ldb_dn_validate(dn)) {
662 newdn = ldb_dn_map_remote(module, mem_ctx, dn);
666 newval.data = (uint8_t *)ldb_dn_alloc_linearized(mem_ctx, newdn);
668 newval.length = strlen((char *)newval.data);
675 /* Map an objectClass into the remote partition. */
676 static struct ldb_val map_objectclass_convert_local(struct ldb_module *module, void *mem_ctx, const struct ldb_val *val)
678 const struct ldb_map_context *data = map_get_context(module);
679 const char *name = (char *)val->data;
680 const struct ldb_map_objectclass *map = map_objectclass_find_local(data, name);
681 struct ldb_val newval;
684 newval.data = (uint8_t*)talloc_strdup(mem_ctx, map->remote_name);
685 newval.length = strlen((char *)newval.data);
689 return ldb_val_dup(mem_ctx, val);
692 /* Generate a remote message with a mapped objectClass. */
693 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)
695 const struct ldb_map_context *data = map_get_context(module);
696 struct ldb_message_element *el, *oc;
698 bool found_extensibleObject = false;
701 /* Find old local objectClass */
702 oc = ldb_msg_find_element(old, "objectClass");
707 /* Prepare new element */
708 el = talloc_zero(remote, struct ldb_message_element);
710 ldb_oom(module->ldb);
711 return; /* TODO: fail? */
714 /* Copy local objectClass element, reverse space for an extra value */
715 el->num_values = oc->num_values + 1;
716 el->values = talloc_array(el, struct ldb_val, el->num_values);
717 if (el->values == NULL) {
719 ldb_oom(module->ldb);
720 return; /* TODO: fail? */
723 /* Copy local element name "objectClass" */
724 el->name = talloc_strdup(el, local_attr);
726 /* Convert all local objectClasses */
727 for (i = 0; i < el->num_values - 1; i++) {
728 el->values[i] = map_objectclass_convert_local(module, el->values, &oc->values[i]);
729 if (ldb_attr_cmp((char *)el->values[i].data, data->add_objectclass) == 0) {
730 found_extensibleObject = true;
734 if (!found_extensibleObject) {
735 val.data = (uint8_t *)talloc_strdup(el->values, data->add_objectclass);
736 val.length = strlen((char *)val.data);
738 /* Append additional objectClass data->add_objectclass */
744 /* Add new objectClass to remote message */
745 ldb_msg_add(remote, el, 0);
748 /* Map an objectClass into the local partition. */
749 static struct ldb_val map_objectclass_convert_remote(struct ldb_module *module, void *mem_ctx, const struct ldb_val *val)
751 const struct ldb_map_context *data = map_get_context(module);
752 const char *name = (char *)val->data;
753 const struct ldb_map_objectclass *map = map_objectclass_find_remote(data, name);
754 struct ldb_val newval;
757 newval.data = (uint8_t*)talloc_strdup(mem_ctx, map->local_name);
758 newval.length = strlen((char *)newval.data);
762 return ldb_val_dup(mem_ctx, val);
765 /* Generate a local message with a mapped objectClass. */
766 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)
768 const struct ldb_map_context *data = map_get_context(module);
769 struct ldb_message_element *el, *oc;
773 /* Find old remote objectClass */
774 oc = ldb_msg_find_element(remote, "objectClass");
779 /* Prepare new element */
780 el = talloc_zero(mem_ctx, struct ldb_message_element);
782 ldb_oom(module->ldb);
786 /* Copy remote objectClass element */
787 el->num_values = oc->num_values;
788 el->values = talloc_array(el, struct ldb_val, el->num_values);
789 if (el->values == NULL) {
791 ldb_oom(module->ldb);
795 /* Copy remote element name "objectClass" */
796 el->name = talloc_strdup(el, local_attr);
798 /* Convert all remote objectClasses */
799 for (i = 0; i < el->num_values; i++) {
800 el->values[i] = map_objectclass_convert_remote(module, el->values, &oc->values[i]);
803 val.data = (uint8_t *)talloc_strdup(el->values, data->add_objectclass);
804 val.length = strlen((char *)val.data);
806 /* Remove last value if it was the string in data->add_objectclass (eg samba4top, extensibleObject) */
807 if (ldb_val_equal_exact(&val, &el->values[i-1])) {
809 el->values = talloc_realloc(el, el->values, struct ldb_val, el->num_values);
810 if (el->values == NULL) {
812 ldb_oom(module->ldb);
820 static const struct ldb_map_attribute objectclass_convert_map = {
821 .local_name = "objectClass",
825 .remote_name = "objectClass",
826 .convert_local = map_objectclass_convert_local,
827 .convert_remote = map_objectclass_convert_remote,
833 /* Mappings for searches on objectClass= assuming a one-to-one
834 * mapping. Needed because this is a generate operator for the
836 static int map_objectclass_convert_operator(struct ldb_module *module, void *mem_ctx,
837 struct ldb_parse_tree **new, const struct ldb_parse_tree *tree)
840 return map_subtree_collect_remote_simple(module, mem_ctx, new, tree, &objectclass_convert_map);
843 /* Auxiliary request construction
844 * ============================== */
846 /* Build a request to search a record by its DN. */
847 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_map_callback_t callback)
849 const struct ldb_parse_tree *search_tree;
850 struct ldb_request *req;
856 search_tree = ldb_parse_tree(ac, NULL);
857 if (search_tree == NULL) {
862 ret = ldb_build_search_req_ex(&req, ac->module->ldb, ac,
868 if (ret != LDB_SUCCESS) {
875 /* Build a request to update the 'IS_MAPPED' attribute */
876 struct ldb_request *map_build_fixup_req(struct map_context *ac,
877 struct ldb_dn *olddn,
878 struct ldb_dn *newdn,
880 ldb_map_callback_t callback)
882 struct ldb_request *req;
883 struct ldb_message *msg;
887 /* Prepare message */
888 msg = ldb_msg_new(ac);
894 /* Update local 'IS_MAPPED' to the new remote DN */
895 msg->dn = ldb_dn_copy(msg, olddn);
896 dn = ldb_dn_alloc_linearized(msg, newdn);
897 if ( ! dn || ! ldb_dn_validate(msg->dn)) {
900 if (ldb_msg_add_empty(msg, IS_MAPPED, LDB_FLAG_MOD_REPLACE, NULL) != 0) {
903 if (ldb_msg_add_string(msg, IS_MAPPED, dn) != 0) {
907 /* Prepare request */
908 ret = ldb_build_mod_req(&req, ac->module->ldb,
912 if (ret != LDB_SUCCESS) {
915 talloc_steal(req, msg);
923 /* Module initialization
924 * ===================== */
927 /* Builtin mappings for DNs and objectClasses */
928 static const struct ldb_map_attribute builtin_attribute_maps[] = {
935 .convert_local = ldb_dn_convert_local,
936 .convert_remote = ldb_dn_convert_remote,
945 static const struct ldb_map_attribute objectclass_attribute_map = {
946 .local_name = "objectClass",
947 .type = MAP_GENERATE,
948 .convert_operator = map_objectclass_convert_operator,
951 .remote_names = { "objectClass", NULL },
952 .generate_local = map_objectclass_generate_local,
953 .generate_remote = map_objectclass_generate_remote,
959 /* Find the special 'MAP_DN_NAME' record and store local and remote
960 * base DNs in private data. */
961 static int map_init_dns(struct ldb_module *module, struct ldb_map_context *data, const char *name)
963 static const char * const attrs[] = { MAP_DN_FROM, MAP_DN_TO, NULL };
965 struct ldb_message *msg;
966 struct ldb_result *res;
970 data->local_base_dn = NULL;
971 data->remote_base_dn = NULL;
975 dn = ldb_dn_new_fmt(data, module->ldb, "%s=%s", MAP_DN_NAME, name);
976 if ( ! ldb_dn_validate(dn)) {
977 ldb_debug(module->ldb, LDB_DEBUG_ERROR, "ldb_map: "
978 "Failed to construct '%s' DN!\n", MAP_DN_NAME);
979 return LDB_ERR_OPERATIONS_ERROR;
982 ret = ldb_search(module->ldb, data, &res, dn, LDB_SCOPE_BASE, attrs, NULL);
984 if (ret != LDB_SUCCESS) {
987 if (res->count == 0) {
988 ldb_debug(module->ldb, LDB_DEBUG_ERROR, "ldb_map: "
989 "No results for '%s=%s'!\n", MAP_DN_NAME, name);
991 return LDB_ERR_CONSTRAINT_VIOLATION;
993 if (res->count > 1) {
994 ldb_debug(module->ldb, LDB_DEBUG_ERROR, "ldb_map: "
995 "Too many results for '%s=%s'!\n", MAP_DN_NAME, name);
997 return LDB_ERR_CONSTRAINT_VIOLATION;
1001 data->local_base_dn = ldb_msg_find_attr_as_dn(module->ldb, data, msg, MAP_DN_FROM);
1002 data->remote_base_dn = ldb_msg_find_attr_as_dn(module->ldb, data, msg, MAP_DN_TO);
1008 /* Store attribute maps and objectClass maps in private data. */
1009 static int map_init_maps(struct ldb_module *module, struct ldb_map_context *data,
1010 const struct ldb_map_attribute *attrs,
1011 const struct ldb_map_objectclass *ocls,
1012 const char * const *wildcard_attributes)
1017 /* Count specified attribute maps */
1018 for (i = 0; attrs[i].local_name; i++) /* noop */ ;
1019 /* Count built-in attribute maps */
1020 for (j = 0; builtin_attribute_maps[j].local_name; j++) /* noop */ ;
1022 /* Store list of attribute maps */
1023 data->attribute_maps = talloc_array(data, struct ldb_map_attribute, i+j+2);
1024 if (data->attribute_maps == NULL) {
1026 return LDB_ERR_OPERATIONS_ERROR;
1029 /* Specified ones go first */
1030 for (i = 0; attrs[i].local_name; i++) {
1031 data->attribute_maps[last] = attrs[i];
1035 /* Built-in ones go last */
1036 for (i = 0; builtin_attribute_maps[i].local_name; i++) {
1037 data->attribute_maps[last] = builtin_attribute_maps[i];
1041 if (data->add_objectclass) {
1042 /* ObjectClass one is very last, if required */
1043 data->attribute_maps[last] = objectclass_attribute_map;
1046 data->attribute_maps[last] = objectclass_convert_map;
1050 /* Ensure 'local_name == NULL' for the last entry */
1051 memset(&data->attribute_maps[last], 0, sizeof(struct ldb_map_attribute));
1053 /* Store list of objectClass maps */
1054 data->objectclass_maps = ocls;
1056 data->wildcard_attributes = wildcard_attributes;
1061 /* Initialize global private data. */
1062 _PUBLIC_ int ldb_map_init(struct ldb_module *module, const struct ldb_map_attribute *attrs,
1063 const struct ldb_map_objectclass *ocls,
1064 const char * const *wildcard_attributes,
1065 const char *add_objectclass,
1068 struct map_private *data;
1071 /* Prepare private data */
1072 data = talloc_zero(module, struct map_private);
1075 return LDB_ERR_OPERATIONS_ERROR;
1078 module->private_data = data;
1080 data->context = talloc_zero(data, struct ldb_map_context);
1081 if (!data->context) {
1083 return LDB_ERR_OPERATIONS_ERROR;
1086 /* Store local and remote baseDNs */
1087 ret = map_init_dns(module, data->context, name);
1088 if (ret != LDB_SUCCESS) {
1093 data->context->add_objectclass = add_objectclass;
1095 /* Store list of attribute and objectClass maps */
1096 ret = map_init_maps(module, data->context, attrs, ocls, wildcard_attributes);
1097 if (ret != LDB_SUCCESS) {