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 const struct ldb_map_context
*data
= map_get_context(module
);
814 struct ldb_message_element
*el
, *oc
;
818 /* Find old remote objectClass */
819 oc
= ldb_msg_find_element(remote
, "objectClass");
824 /* Prepare new element */
825 el
= talloc_zero(mem_ctx
, struct ldb_message_element
);
827 ldb_oom(module
->ldb
);
831 /* Copy remote objectClass element */
832 el
->num_values
= oc
->num_values
;
833 el
->values
= talloc_array(el
, struct ldb_val
, el
->num_values
);
834 if (el
->values
== NULL
) {
836 ldb_oom(module
->ldb
);
840 /* Copy remote element name "objectClass" */
841 el
->name
= talloc_strdup(el
, local_attr
);
843 /* Convert all remote objectClasses */
844 for (i
= 0; i
< el
->num_values
; i
++) {
845 el
->values
[i
] = map_objectclass_convert_remote(module
, el
->values
, &oc
->values
[i
]);
848 val
.data
= (uint8_t *)talloc_strdup(el
->values
, data
->add_objectclass
);
849 val
.length
= strlen((char *)val
.data
);
851 /* Remove last value if it was the string in data->add_objectclass (eg samba4top, extensibleObject) */
852 if (ldb_val_equal_exact(&val
, &el
->values
[i
-1])) {
854 el
->values
= talloc_realloc(el
, el
->values
, struct ldb_val
, el
->num_values
);
855 if (el
->values
== NULL
) {
857 ldb_oom(module
->ldb
);
865 static const struct ldb_map_attribute objectclass_convert_map
= {
866 .local_name
= "objectClass",
870 .remote_name
= "objectClass",
871 .convert_local
= map_objectclass_convert_local
,
872 .convert_remote
= map_objectclass_convert_remote
,
878 /* Mappings for searches on objectClass= assuming a one-to-one
879 * mapping. Needed because this is a generate operator for the
881 static int map_objectclass_convert_operator(struct ldb_module
*module
, void *mem_ctx
,
882 struct ldb_parse_tree
**new, const struct ldb_parse_tree
*tree
)
885 return map_subtree_collect_remote_simple(module
, mem_ctx
, new, tree
, &objectclass_convert_map
);
888 /* Auxiliary request construction
889 * ============================== */
891 /* Store the DN of a single search result in context. */
892 static int map_search_self_callback(struct ldb_context
*ldb
, void *context
, struct ldb_reply
*ares
)
894 struct map_context
*ac
;
896 if (context
== NULL
|| ares
== NULL
) {
897 ldb_set_errstring(ldb
, talloc_asprintf(ldb
, "NULL Context or Result in callback"));
898 return LDB_ERR_OPERATIONS_ERROR
;
901 ac
= talloc_get_type(context
, struct map_context
);
903 /* We are interested only in the single reply */
904 if (ares
->type
!= LDB_REPLY_ENTRY
) {
909 /* We have already found a remote DN */
911 ldb_set_errstring(ldb
, talloc_asprintf(ldb
, "Too many results to base search"));
913 return LDB_ERR_OPERATIONS_ERROR
;
917 ac
->local_dn
= ares
->message
->dn
;
922 /* Build a request to search a record by its DN. */
923 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
)
925 struct ldb_request
*req
;
927 req
= talloc_zero(ac
, struct ldb_request
);
933 req
->operation
= LDB_SEARCH
;
934 req
->op
.search
.base
= dn
;
935 req
->op
.search
.scope
= LDB_SCOPE_BASE
;
936 req
->op
.search
.attrs
= attrs
;
939 req
->op
.search
.tree
= tree
;
941 req
->op
.search
.tree
= ldb_parse_tree(req
, NULL
);
942 if (req
->op
.search
.tree
== NULL
) {
948 req
->controls
= NULL
;
949 req
->context
= context
;
950 req
->callback
= callback
;
951 ldb_set_timeout_from_prev_req(ac
->module
->ldb
, ac
->orig_req
, req
);
956 /* Build a request to search the local record by its DN. */
957 struct ldb_request
*map_search_self_req(struct map_context
*ac
, struct ldb_dn
*dn
)
959 /* attrs[] is returned from this function in
960 * ac->search_req->op.search.attrs, so it must be static, as
961 * otherwise the compiler can put it on the stack */
962 static const char * const attrs
[] = { IS_MAPPED
, NULL
};
963 struct ldb_parse_tree
*tree
;
965 /* Limit search to records with 'IS_MAPPED' present */
966 /* TODO: `tree = ldb_parse_tree(ac, IS_MAPPED);' won't do. */
967 tree
= talloc_zero(ac
, struct ldb_parse_tree
);
973 tree
->operation
= LDB_OP_PRESENT
;
974 tree
->u
.present
.attr
= talloc_strdup(tree
, IS_MAPPED
);
976 return map_search_base_req(ac
, dn
, attrs
, tree
, ac
, map_search_self_callback
);
979 /* Build a request to update the 'IS_MAPPED' attribute */
980 struct ldb_request
*map_build_fixup_req(struct map_context
*ac
, struct ldb_dn
*olddn
, struct ldb_dn
*newdn
)
982 struct ldb_request
*req
;
983 struct ldb_message
*msg
;
986 /* Prepare request */
987 req
= talloc_zero(ac
, struct ldb_request
);
993 /* Prepare message */
994 msg
= ldb_msg_new(req
);
1000 /* Update local 'IS_MAPPED' to the new remote DN */
1001 msg
->dn
= ldb_dn_copy(msg
, olddn
);
1002 dn
= ldb_dn_alloc_linearized(msg
, newdn
);
1003 if ( ! dn
|| ! ldb_dn_validate(msg
->dn
)) {
1006 if (ldb_msg_add_empty(msg
, IS_MAPPED
, LDB_FLAG_MOD_REPLACE
, NULL
) != 0) {
1009 if (ldb_msg_add_string(msg
, IS_MAPPED
, dn
) != 0) {
1013 req
->operation
= LDB_MODIFY
;
1014 req
->op
.mod
.message
= msg
;
1015 req
->controls
= NULL
;
1017 req
->context
= NULL
;
1018 req
->callback
= NULL
;
1028 /* Asynchronous call structure
1029 * =========================== */
1031 /* Figure out which request is currently pending. */
1032 static struct ldb_request
*map_get_req(struct map_context
*ac
)
1035 case MAP_SEARCH_SELF_MODIFY
:
1036 case MAP_SEARCH_SELF_DELETE
:
1037 case MAP_SEARCH_SELF_RENAME
:
1038 return ac
->search_req
;
1040 case MAP_ADD_REMOTE
:
1041 case MAP_MODIFY_REMOTE
:
1042 case MAP_DELETE_REMOTE
:
1043 case MAP_RENAME_REMOTE
:
1044 return ac
->remote_req
;
1046 case MAP_RENAME_FIXUP
:
1047 return ac
->down_req
;
1050 case MAP_MODIFY_LOCAL
:
1051 case MAP_DELETE_LOCAL
:
1052 case MAP_RENAME_LOCAL
:
1053 return ac
->local_req
;
1055 case MAP_SEARCH_REMOTE
:
1060 return NULL
; /* unreachable; silences a warning */
1063 typedef int (*map_next_function
)(struct ldb_handle
*handle
);
1065 /* Figure out the next request to run. */
1066 static map_next_function
map_get_next(struct map_context
*ac
)
1069 case MAP_SEARCH_REMOTE
:
1073 return map_add_do_remote
;
1074 case MAP_ADD_REMOTE
:
1077 case MAP_SEARCH_SELF_MODIFY
:
1078 return map_modify_do_local
;
1079 case MAP_MODIFY_LOCAL
:
1080 return map_modify_do_remote
;
1081 case MAP_MODIFY_REMOTE
:
1084 case MAP_SEARCH_SELF_DELETE
:
1085 return map_delete_do_local
;
1086 case MAP_DELETE_LOCAL
:
1087 return map_delete_do_remote
;
1088 case MAP_DELETE_REMOTE
:
1091 case MAP_SEARCH_SELF_RENAME
:
1092 return map_rename_do_local
;
1093 case MAP_RENAME_LOCAL
:
1094 return map_rename_do_fixup
;
1095 case MAP_RENAME_FIXUP
:
1096 return map_rename_do_remote
;
1097 case MAP_RENAME_REMOTE
:
1101 return NULL
; /* unreachable; silences a warning */
1104 /* Wait for the current pending request to finish and continue with the next. */
1105 static int map_wait_next(struct ldb_handle
*handle
)
1107 struct map_context
*ac
;
1108 struct ldb_request
*req
;
1109 map_next_function next
;
1112 if (handle
== NULL
|| handle
->private_data
== NULL
) {
1113 return LDB_ERR_OPERATIONS_ERROR
;
1116 if (handle
->state
== LDB_ASYNC_DONE
) {
1117 return handle
->status
;
1120 handle
->state
= LDB_ASYNC_PENDING
;
1121 handle
->status
= LDB_SUCCESS
;
1123 ac
= talloc_get_type(handle
->private_data
, struct map_context
);
1125 if (ac
->step
== MAP_SEARCH_REMOTE
) {
1127 for (i
= 0; i
< ac
->num_searches
; i
++) {
1128 req
= ac
->search_reqs
[i
];
1129 ret
= ldb_wait(req
->handle
, LDB_WAIT_NONE
);
1131 if (ret
!= LDB_SUCCESS
) {
1132 handle
->status
= ret
;
1135 if (req
->handle
->status
!= LDB_SUCCESS
) {
1136 handle
->status
= req
->handle
->status
;
1139 if (req
->handle
->state
!= LDB_ASYNC_DONE
) {
1145 req
= map_get_req(ac
);
1147 ret
= ldb_wait(req
->handle
, LDB_WAIT_NONE
);
1149 if (ret
!= LDB_SUCCESS
) {
1150 handle
->status
= ret
;
1153 if (req
->handle
->status
!= LDB_SUCCESS
) {
1154 handle
->status
= req
->handle
->status
;
1157 if (req
->handle
->state
!= LDB_ASYNC_DONE
) {
1161 next
= map_get_next(ac
);
1163 return next(handle
);
1170 handle
->state
= LDB_ASYNC_DONE
;
1174 /* Wait for all current pending requests to finish. */
1175 static int map_wait_all(struct ldb_handle
*handle
)
1179 while (handle
->state
!= LDB_ASYNC_DONE
) {
1180 ret
= map_wait_next(handle
);
1181 if (ret
!= LDB_SUCCESS
) {
1186 return handle
->status
;
1189 /* Wait for pending requests to finish. */
1190 int map_wait(struct ldb_handle
*handle
, enum ldb_wait_type type
)
1192 if (type
== LDB_WAIT_ALL
) {
1193 return map_wait_all(handle
);
1195 return map_wait_next(handle
);
1200 /* Module initialization
1201 * ===================== */
1204 /* Builtin mappings for DNs and objectClasses */
1205 static const struct ldb_map_attribute builtin_attribute_maps
[] = {
1208 .type
= MAP_CONVERT
,
1211 .remote_name
= "dn",
1212 .convert_local
= ldb_dn_convert_local
,
1213 .convert_remote
= ldb_dn_convert_remote
,
1222 static const struct ldb_map_attribute objectclass_attribute_map
= {
1223 .local_name
= "objectClass",
1224 .type
= MAP_GENERATE
,
1225 .convert_operator
= map_objectclass_convert_operator
,
1228 .remote_names
= { "objectClass", NULL
},
1229 .generate_local
= map_objectclass_generate_local
,
1230 .generate_remote
= map_objectclass_generate_remote
,
1236 /* Find the special 'MAP_DN_NAME' record and store local and remote
1237 * base DNs in private data. */
1238 static int map_init_dns(struct ldb_module
*module
, struct ldb_map_context
*data
, const char *name
)
1240 static const char * const attrs
[] = { MAP_DN_FROM
, MAP_DN_TO
, NULL
};
1242 struct ldb_message
*msg
;
1243 struct ldb_result
*res
;
1247 data
->local_base_dn
= NULL
;
1248 data
->remote_base_dn
= NULL
;
1252 dn
= ldb_dn_new_fmt(data
, module
->ldb
, "%s=%s", MAP_DN_NAME
, name
);
1253 if ( ! ldb_dn_validate(dn
)) {
1254 ldb_debug(module
->ldb
, LDB_DEBUG_ERROR
, "ldb_map: "
1255 "Failed to construct '%s' DN!\n", MAP_DN_NAME
);
1256 return LDB_ERR_OPERATIONS_ERROR
;
1259 ret
= ldb_search(module
->ldb
, data
, &res
, dn
, LDB_SCOPE_BASE
, attrs
, NULL
);
1261 if (ret
!= LDB_SUCCESS
) {
1264 if (res
->count
== 0) {
1265 ldb_debug(module
->ldb
, LDB_DEBUG_ERROR
, "ldb_map: "
1266 "No results for '%s=%s'!\n", MAP_DN_NAME
, name
);
1268 return LDB_ERR_CONSTRAINT_VIOLATION
;
1270 if (res
->count
> 1) {
1271 ldb_debug(module
->ldb
, LDB_DEBUG_ERROR
, "ldb_map: "
1272 "Too many results for '%s=%s'!\n", MAP_DN_NAME
, name
);
1274 return LDB_ERR_CONSTRAINT_VIOLATION
;
1278 data
->local_base_dn
= ldb_msg_find_attr_as_dn(module
->ldb
, data
, msg
, MAP_DN_FROM
);
1279 data
->remote_base_dn
= ldb_msg_find_attr_as_dn(module
->ldb
, data
, msg
, MAP_DN_TO
);
1285 /* Store attribute maps and objectClass maps in private data. */
1286 static int map_init_maps(struct ldb_module
*module
, struct ldb_map_context
*data
,
1287 const struct ldb_map_attribute
*attrs
,
1288 const struct ldb_map_objectclass
*ocls
,
1289 const char * const *wildcard_attributes
)
1294 /* Count specified attribute maps */
1295 for (i
= 0; attrs
[i
].local_name
; i
++) /* noop */ ;
1296 /* Count built-in attribute maps */
1297 for (j
= 0; builtin_attribute_maps
[j
].local_name
; j
++) /* noop */ ;
1299 /* Store list of attribute maps */
1300 data
->attribute_maps
= talloc_array(data
, struct ldb_map_attribute
, i
+j
+2);
1301 if (data
->attribute_maps
== NULL
) {
1303 return LDB_ERR_OPERATIONS_ERROR
;
1306 /* Specified ones go first */
1307 for (i
= 0; attrs
[i
].local_name
; i
++) {
1308 data
->attribute_maps
[last
] = attrs
[i
];
1312 /* Built-in ones go last */
1313 for (i
= 0; builtin_attribute_maps
[i
].local_name
; i
++) {
1314 data
->attribute_maps
[last
] = builtin_attribute_maps
[i
];
1318 if (data
->add_objectclass
) {
1319 /* ObjectClass one is very last, if required */
1320 data
->attribute_maps
[last
] = objectclass_attribute_map
;
1323 data
->attribute_maps
[last
] = objectclass_convert_map
;
1327 /* Ensure 'local_name == NULL' for the last entry */
1328 memset(&data
->attribute_maps
[last
], 0, sizeof(struct ldb_map_attribute
));
1330 /* Store list of objectClass maps */
1331 data
->objectclass_maps
= ocls
;
1333 data
->wildcard_attributes
= wildcard_attributes
;
1338 /* Initialize global private data. */
1339 _PUBLIC_
int ldb_map_init(struct ldb_module
*module
, const struct ldb_map_attribute
*attrs
,
1340 const struct ldb_map_objectclass
*ocls
,
1341 const char * const *wildcard_attributes
,
1342 const char *add_objectclass
,
1345 struct map_private
*data
;
1348 /* Prepare private data */
1349 data
= talloc_zero(module
, struct map_private
);
1352 return LDB_ERR_OPERATIONS_ERROR
;
1355 module
->private_data
= data
;
1357 data
->context
= talloc_zero(data
, struct ldb_map_context
);
1358 if (!data
->context
) {
1360 return LDB_ERR_OPERATIONS_ERROR
;
1363 /* Store local and remote baseDNs */
1364 ret
= map_init_dns(module
, data
->context
, name
);
1365 if (ret
!= LDB_SUCCESS
) {
1370 data
->context
->add_objectclass
= add_objectclass
;
1372 /* Store list of attribute and objectClass maps */
1373 ret
= map_init_maps(module
, data
->context
, attrs
, ocls
, wildcard_attributes
);
1374 if (ret
!= LDB_SUCCESS
) {