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
39 #include "system/filesys.h"
40 #include "system/time.h"
42 #include "ldb_map_private.h"
48 /* Description of the provided ldb requests:
49 - special attribute 'isMapped'
52 - if parse tree can be split
53 - search remote records w/ remote attrs and parse tree
55 - enumerate all remote records
56 - for each remote result
57 - map remote result to local message
60 - merge local into remote result
61 - run callback on merged result
63 - run callback on remote result
66 - split message into local and remote part
67 - if local message is not empty
68 - add isMapped to local message
73 - split message into local and remote part
74 - if local message is not empty
75 - add isMapped to local message
76 - search for local record
81 - modify remote record
84 - search for local record
87 - delete remote record
90 - search for local record
93 - modify local isMapped
94 - rename remote record
99 /* Private data structures
100 * ======================= */
102 /* Global private data */
103 /* Extract mappings from private data. */
104 const struct ldb_map_context
*map_get_context(struct ldb_module
*module
)
106 const struct map_private
*data
= talloc_get_type(ldb_module_get_private(module
), struct map_private
);
107 return data
->context
;
110 /* Create a generic request context. */
111 struct map_context
*map_init_context(struct ldb_module
*module
,
112 struct ldb_request
*req
)
114 struct ldb_context
*ldb
;
115 struct map_context
*ac
;
117 ldb
= ldb_module_get_ctx(module
);
119 ac
= talloc_zero(req
, struct map_context
);
121 ldb_set_errstring(ldb
, "Out of Memory");
131 /* Dealing with DNs for different partitions
132 * ========================================= */
134 /* Check whether any data should be stored in the local partition. */
135 bool map_check_local_db(struct ldb_module
*module
)
137 const struct ldb_map_context
*data
= map_get_context(module
);
139 if (!data
->remote_base_dn
|| !data
->local_base_dn
) {
146 /* Copy a DN with the base DN of the local partition. */
147 static struct ldb_dn
*ldb_dn_rebase_local(void *mem_ctx
, const struct ldb_map_context
*data
, struct ldb_dn
*dn
)
149 struct ldb_dn
*new_dn
;
151 new_dn
= ldb_dn_copy(mem_ctx
, dn
);
152 if ( ! ldb_dn_validate(new_dn
)) {
157 /* may be we don't need to rebase at all */
158 if ( ! data
->remote_base_dn
|| ! data
->local_base_dn
) {
162 if ( ! ldb_dn_remove_base_components(new_dn
, ldb_dn_get_comp_num(data
->remote_base_dn
))) {
167 if ( ! ldb_dn_add_base(new_dn
, data
->local_base_dn
)) {
175 /* Copy a DN with the base DN of the remote partition. */
176 static struct ldb_dn
*ldb_dn_rebase_remote(void *mem_ctx
, const struct ldb_map_context
*data
, struct ldb_dn
*dn
)
178 struct ldb_dn
*new_dn
;
180 new_dn
= ldb_dn_copy(mem_ctx
, dn
);
181 if ( ! ldb_dn_validate(new_dn
)) {
186 /* may be we don't need to rebase at all */
187 if ( ! data
->remote_base_dn
|| ! data
->local_base_dn
) {
191 if ( ! ldb_dn_remove_base_components(new_dn
, ldb_dn_get_comp_num(data
->local_base_dn
))) {
196 if ( ! ldb_dn_add_base(new_dn
, data
->remote_base_dn
)) {
204 /* Run a request and make sure it targets the remote partition. */
205 /* TODO: free old DNs and messages? */
206 int ldb_next_remote_request(struct ldb_module
*module
, struct ldb_request
*request
)
208 const struct ldb_map_context
*data
= map_get_context(module
);
209 struct ldb_context
*ldb
;
210 struct ldb_message
*msg
;
212 ldb
= ldb_module_get_ctx(module
);
214 switch (request
->operation
) {
216 if (request
->op
.search
.base
) {
217 request
->op
.search
.base
= ldb_dn_rebase_remote(request
, data
, request
->op
.search
.base
);
219 request
->op
.search
.base
= data
->remote_base_dn
;
220 /* TODO: adjust scope? */
225 msg
= ldb_msg_copy_shallow(request
, request
->op
.add
.message
);
227 return LDB_ERR_OPERATIONS_ERROR
;
229 msg
->dn
= ldb_dn_rebase_remote(msg
, data
, msg
->dn
);
230 request
->op
.add
.message
= msg
;
234 msg
= ldb_msg_copy_shallow(request
, request
->op
.mod
.message
);
236 return LDB_ERR_OPERATIONS_ERROR
;
238 msg
->dn
= ldb_dn_rebase_remote(msg
, data
, msg
->dn
);
239 request
->op
.mod
.message
= msg
;
243 request
->op
.del
.dn
= ldb_dn_rebase_remote(request
, data
, request
->op
.del
.dn
);
247 request
->op
.rename
.olddn
= ldb_dn_rebase_remote(request
, data
, request
->op
.rename
.olddn
);
248 request
->op
.rename
.newdn
= ldb_dn_rebase_remote(request
, data
, request
->op
.rename
.newdn
);
252 ldb_debug(ldb
, LDB_DEBUG_ERROR
, "ldb_map: "
253 "Invalid remote request!");
254 return LDB_ERR_OPERATIONS_ERROR
;
257 return ldb_next_request(module
, request
);
261 /* Finding mappings for attributes and objectClasses
262 * ================================================= */
264 /* Find an objectClass mapping by the local name. */
265 static const struct ldb_map_objectclass
*map_objectclass_find_local(const struct ldb_map_context
*data
, const char *name
)
269 for (i
= 0; data
->objectclass_maps
&& data
->objectclass_maps
[i
].local_name
; i
++) {
270 if (ldb_attr_cmp(data
->objectclass_maps
[i
].local_name
, name
) == 0) {
271 return &data
->objectclass_maps
[i
];
278 /* Find an objectClass mapping by the remote name. */
279 static const struct ldb_map_objectclass
*map_objectclass_find_remote(const struct ldb_map_context
*data
, const char *name
)
283 for (i
= 0; data
->objectclass_maps
&& data
->objectclass_maps
[i
].remote_name
; i
++) {
284 if (ldb_attr_cmp(data
->objectclass_maps
[i
].remote_name
, name
) == 0) {
285 return &data
->objectclass_maps
[i
];
292 /* Find an attribute mapping by the local name. */
293 const struct ldb_map_attribute
*map_attr_find_local(const struct ldb_map_context
*data
, const char *name
)
297 for (i
= 0; data
->attribute_maps
[i
].local_name
; i
++) {
298 if (ldb_attr_cmp(data
->attribute_maps
[i
].local_name
, name
) == 0) {
299 return &data
->attribute_maps
[i
];
302 for (i
= 0; data
->attribute_maps
[i
].local_name
; i
++) {
303 if (ldb_attr_cmp(data
->attribute_maps
[i
].local_name
, "*") == 0) {
304 return &data
->attribute_maps
[i
];
311 /* Find an attribute mapping by the remote name. */
312 const struct ldb_map_attribute
*map_attr_find_remote(const struct ldb_map_context
*data
, const char *name
)
314 const struct ldb_map_attribute
*map
;
315 const struct ldb_map_attribute
*wildcard
= NULL
;
318 for (i
= 0; data
->attribute_maps
[i
].local_name
; i
++) {
319 map
= &data
->attribute_maps
[i
];
320 if (ldb_attr_cmp(map
->local_name
, "*") == 0) {
321 wildcard
= &data
->attribute_maps
[i
];
329 if (ldb_attr_cmp(map
->local_name
, name
) == 0) {
335 case LDB_MAP_RENDROP
:
336 case LDB_MAP_CONVERT
:
337 if (ldb_attr_cmp(map
->u
.rename
.remote_name
, name
) == 0) {
342 case LDB_MAP_GENERATE
:
343 for (j
= 0; map
->u
.generate
.remote_names
[j
]; j
++) {
344 if (ldb_attr_cmp(map
->u
.generate
.remote_names
[j
], name
) == 0) {
352 /* We didn't find it, so return the wildcard record if one was configured */
357 /* Mapping attributes
358 * ================== */
360 /* Check whether an attribute will be mapped into the remote partition. */
361 bool map_attr_check_remote(const struct ldb_map_context
*data
, const char *attr
)
363 const struct ldb_map_attribute
*map
= map_attr_find_local(data
, attr
);
368 if (map
->type
== LDB_MAP_IGNORE
) {
375 /* Map an attribute name into the remote partition. */
376 const char *map_attr_map_local(void *mem_ctx
, const struct ldb_map_attribute
*map
, const char *attr
)
379 return talloc_strdup(mem_ctx
, attr
);
384 return talloc_strdup(mem_ctx
, attr
);
387 case LDB_MAP_RENDROP
:
388 case LDB_MAP_CONVERT
:
389 return talloc_strdup(mem_ctx
, map
->u
.rename
.remote_name
);
396 /* Map an attribute name back into the local partition. */
397 const char *map_attr_map_remote(void *mem_ctx
, const struct ldb_map_attribute
*map
, const char *attr
)
400 return talloc_strdup(mem_ctx
, attr
);
403 if (map
->type
== LDB_MAP_KEEP
) {
404 return talloc_strdup(mem_ctx
, attr
);
407 return talloc_strdup(mem_ctx
, map
->local_name
);
411 /* Merge two lists of attributes into a single one. */
412 int map_attrs_merge(struct ldb_module
*module
, void *mem_ctx
,
413 const char ***attrs
, const char * const *more_attrs
)
415 unsigned int i
, j
, k
;
417 for (i
= 0; *attrs
&& (*attrs
)[i
]; i
++) /* noop */ ;
418 for (j
= 0; more_attrs
&& more_attrs
[j
]; j
++) /* noop */ ;
420 *attrs
= talloc_realloc(mem_ctx
, *attrs
, const char *, i
+j
+1);
421 if (*attrs
== NULL
) {
426 for (k
= 0; k
< j
; k
++) {
427 (*attrs
)[i
+ k
] = more_attrs
[k
];
430 (*attrs
)[i
+k
] = NULL
;
435 /* Mapping ldb values
436 * ================== */
438 /* Map an ldb value into the remote partition. */
439 struct ldb_val
ldb_val_map_local(struct ldb_module
*module
, void *mem_ctx
,
440 const struct ldb_map_attribute
*map
, const struct ldb_val
*val
)
442 if (map
&& (map
->type
== LDB_MAP_CONVERT
) && (map
->u
.convert
.convert_local
)) {
443 return map
->u
.convert
.convert_local(module
, mem_ctx
, val
);
446 return ldb_val_dup(mem_ctx
, val
);
449 /* Map an ldb value back into the local partition. */
450 struct ldb_val
ldb_val_map_remote(struct ldb_module
*module
, void *mem_ctx
,
451 const struct ldb_map_attribute
*map
, const struct ldb_val
*val
)
453 if (map
&& (map
->type
== LDB_MAP_CONVERT
) && (map
->u
.convert
.convert_remote
)) {
454 return map
->u
.convert
.convert_remote(module
, mem_ctx
, val
);
457 return ldb_val_dup(mem_ctx
, val
);
464 /* Check whether a DN is below the local baseDN. */
465 bool ldb_dn_check_local(struct ldb_module
*module
, struct ldb_dn
*dn
)
467 const struct ldb_map_context
*data
= map_get_context(module
);
469 if (!data
->local_base_dn
) {
473 return ldb_dn_compare_base(data
->local_base_dn
, dn
) == 0;
476 /* Map a DN into the remote partition. */
477 struct ldb_dn
*ldb_dn_map_local(struct ldb_module
*module
, void *mem_ctx
, struct ldb_dn
*dn
)
479 const struct ldb_map_context
*data
= map_get_context(module
);
480 struct ldb_context
*ldb
;
481 struct ldb_dn
*newdn
;
482 const struct ldb_map_attribute
*map
;
483 enum ldb_map_attr_type map_type
;
485 struct ldb_val value
;
492 ldb
= ldb_module_get_ctx(module
);
494 newdn
= ldb_dn_copy(mem_ctx
, dn
);
500 /* For each RDN, map the component name and possibly the value */
501 for (i
= 0; i
< ldb_dn_get_comp_num(newdn
); i
++) {
502 map
= map_attr_find_local(data
, ldb_dn_get_component_name(dn
, i
));
504 /* Unknown attribute - leave this RDN as is and hope the best... */
506 map_type
= LDB_MAP_KEEP
;
508 map_type
= map
->type
;
513 case LDB_MAP_GENERATE
:
514 ldb_debug(ldb
, LDB_DEBUG_ERROR
, "ldb_map: "
515 "LDB_MAP_IGNORE/LDB_MAP_GENERATE attribute '%s' "
516 "used in DN!", ldb_dn_get_component_name(dn
, i
));
519 case LDB_MAP_CONVERT
:
520 if (map
->u
.convert
.convert_local
== NULL
) {
521 ldb_debug(ldb
, LDB_DEBUG_ERROR
, "ldb_map: "
522 "'convert_local' not set for attribute '%s' "
523 "used in DN!", ldb_dn_get_component_name(dn
, i
));
530 case LDB_MAP_RENDROP
:
531 name
= map_attr_map_local(newdn
, map
, ldb_dn_get_component_name(dn
, i
));
532 if (name
== NULL
) goto failed
;
534 value
= ldb_val_map_local(module
, newdn
, map
, ldb_dn_get_component_val(dn
, i
));
535 if (value
.data
== NULL
) goto failed
;
537 ret
= ldb_dn_set_component(newdn
, i
, name
, value
);
538 if (ret
!= LDB_SUCCESS
) {
553 /* Map a DN into the local partition. */
554 struct ldb_dn
*ldb_dn_map_remote(struct ldb_module
*module
, void *mem_ctx
, struct ldb_dn
*dn
)
556 const struct ldb_map_context
*data
= map_get_context(module
);
557 struct ldb_context
*ldb
;
558 struct ldb_dn
*newdn
;
559 const struct ldb_map_attribute
*map
;
560 enum ldb_map_attr_type map_type
;
562 struct ldb_val value
;
569 ldb
= ldb_module_get_ctx(module
);
571 newdn
= ldb_dn_copy(mem_ctx
, dn
);
577 /* For each RDN, map the component name and possibly the value */
578 for (i
= 0; i
< ldb_dn_get_comp_num(newdn
); i
++) {
579 map
= map_attr_find_remote(data
, ldb_dn_get_component_name(dn
, i
));
581 /* Unknown attribute - leave this RDN as is and hope the best... */
583 map_type
= LDB_MAP_KEEP
;
585 map_type
= map
->type
;
590 case LDB_MAP_GENERATE
:
591 ldb_debug(ldb
, LDB_DEBUG_ERROR
, "ldb_map: "
592 "LDB_MAP_IGNORE/LDB_MAP_GENERATE attribute '%s' "
593 "used in DN!", ldb_dn_get_component_name(dn
, i
));
596 case LDB_MAP_CONVERT
:
597 if (map
->u
.convert
.convert_remote
== NULL
) {
598 ldb_debug(ldb
, LDB_DEBUG_ERROR
, "ldb_map: "
599 "'convert_remote' not set for attribute '%s' "
600 "used in DN!", ldb_dn_get_component_name(dn
, i
));
607 case LDB_MAP_RENDROP
:
608 name
= map_attr_map_remote(newdn
, map
, ldb_dn_get_component_name(dn
, i
));
609 if (name
== NULL
) goto failed
;
611 value
= ldb_val_map_remote(module
, newdn
, map
, ldb_dn_get_component_val(dn
, i
));
612 if (value
.data
== NULL
) goto failed
;
614 ret
= ldb_dn_set_component(newdn
, i
, name
, value
);
615 if (ret
!= LDB_SUCCESS
) {
630 /* Map a DN and its base into the local partition. */
631 /* TODO: This should not be required with GUIDs. */
632 struct ldb_dn
*ldb_dn_map_rebase_remote(struct ldb_module
*module
, void *mem_ctx
, struct ldb_dn
*dn
)
634 const struct ldb_map_context
*data
= map_get_context(module
);
635 struct ldb_dn
*dn1
, *dn2
;
637 dn1
= ldb_dn_rebase_local(mem_ctx
, data
, dn
);
638 dn2
= ldb_dn_map_remote(module
, mem_ctx
, dn1
);
645 /* Converting DNs and objectClasses (as ldb values)
646 * ================================================ */
648 /* Map a DN contained in an ldb value into the remote partition. */
649 static struct ldb_val
ldb_dn_convert_local(struct ldb_module
*module
, void *mem_ctx
, const struct ldb_val
*val
)
651 struct ldb_context
*ldb
;
652 struct ldb_dn
*dn
, *newdn
;
653 struct ldb_val newval
;
655 ldb
= ldb_module_get_ctx(module
);
657 dn
= ldb_dn_from_ldb_val(mem_ctx
, ldb
, val
);
658 if (! ldb_dn_validate(dn
)) {
664 newdn
= ldb_dn_map_local(module
, mem_ctx
, dn
);
668 newval
.data
= (uint8_t *)ldb_dn_alloc_linearized(mem_ctx
, newdn
);
670 newval
.length
= strlen((char *)newval
.data
);
677 /* Map a DN contained in an ldb value into the local partition. */
678 static struct ldb_val
ldb_dn_convert_remote(struct ldb_module
*module
, void *mem_ctx
, const struct ldb_val
*val
)
680 struct ldb_context
*ldb
;
681 struct ldb_dn
*dn
, *newdn
;
682 struct ldb_val newval
;
684 ldb
= ldb_module_get_ctx(module
);
686 dn
= ldb_dn_from_ldb_val(mem_ctx
, ldb
, val
);
687 if (! ldb_dn_validate(dn
)) {
693 newdn
= ldb_dn_map_remote(module
, mem_ctx
, dn
);
697 newval
.data
= (uint8_t *)ldb_dn_alloc_linearized(mem_ctx
, newdn
);
699 newval
.length
= strlen((char *)newval
.data
);
706 /* Map an objectClass into the remote partition. */
707 static struct ldb_val
map_objectclass_convert_local(struct ldb_module
*module
, void *mem_ctx
, const struct ldb_val
*val
)
709 const struct ldb_map_context
*data
= map_get_context(module
);
710 const char *name
= (char *)val
->data
;
711 const struct ldb_map_objectclass
*map
= map_objectclass_find_local(data
, name
);
712 struct ldb_val newval
;
715 newval
.data
= (uint8_t*)talloc_strdup(mem_ctx
, map
->remote_name
);
716 newval
.length
= strlen((char *)newval
.data
);
720 return ldb_val_dup(mem_ctx
, val
);
723 /* Generate a remote message with a mapped objectClass. */
724 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
)
726 const struct ldb_map_context
*data
= map_get_context(module
);
727 struct ldb_context
*ldb
;
728 struct ldb_message_element
*el
, *oc
;
730 bool found_extensibleObject
= false;
734 ldb
= ldb_module_get_ctx(module
);
736 /* Find old local objectClass */
737 oc
= ldb_msg_find_element(old
, "objectClass");
742 /* Prepare new element */
743 el
= talloc_zero(remote
, struct ldb_message_element
);
746 return; /* TODO: fail? */
749 /* Copy local objectClass element, reverse space for an extra value */
750 el
->num_values
= oc
->num_values
+ 1;
751 el
->values
= talloc_array(el
, struct ldb_val
, el
->num_values
);
752 if (el
->values
== NULL
) {
755 return; /* TODO: fail? */
758 /* Copy local element name "objectClass" */
759 el
->name
= talloc_strdup(el
, local_attr
);
761 /* Convert all local objectClasses */
762 for (i
= 0; i
< el
->num_values
- 1; i
++) {
763 el
->values
[i
] = map_objectclass_convert_local(module
, el
->values
, &oc
->values
[i
]);
764 if (ldb_attr_cmp((char *)el
->values
[i
].data
, data
->add_objectclass
) == 0) {
765 found_extensibleObject
= true;
769 if (!found_extensibleObject
) {
770 val
.data
= (uint8_t *)talloc_strdup(el
->values
, data
->add_objectclass
);
771 val
.length
= strlen((char *)val
.data
);
773 /* Append additional objectClass data->add_objectclass */
779 /* Add new objectClass to remote message */
780 ret
= ldb_msg_add(remote
, el
, 0);
781 if (ret
!= LDB_SUCCESS
) {
787 /* Map an objectClass into the local partition. */
788 static struct ldb_val
map_objectclass_convert_remote(struct ldb_module
*module
, void *mem_ctx
, const struct ldb_val
*val
)
790 const struct ldb_map_context
*data
= map_get_context(module
);
791 const char *name
= (char *)val
->data
;
792 const struct ldb_map_objectclass
*map
= map_objectclass_find_remote(data
, name
);
793 struct ldb_val newval
;
796 newval
.data
= (uint8_t*)talloc_strdup(mem_ctx
, map
->local_name
);
797 newval
.length
= strlen((char *)newval
.data
);
801 return ldb_val_dup(mem_ctx
, val
);
804 /* Generate a local message with a mapped objectClass. */
805 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
)
807 const struct ldb_map_context
*data
= map_get_context(module
);
808 struct ldb_context
*ldb
;
809 struct ldb_message_element
*el
, *oc
;
813 ldb
= ldb_module_get_ctx(module
);
815 /* Find old remote objectClass */
816 oc
= ldb_msg_find_element(remote
, "objectClass");
821 /* Prepare new element */
822 el
= talloc_zero(mem_ctx
, struct ldb_message_element
);
828 /* Copy remote objectClass element */
829 el
->num_values
= oc
->num_values
;
830 el
->values
= talloc_array(el
, struct ldb_val
, el
->num_values
);
831 if (el
->values
== NULL
) {
837 /* Copy remote element name "objectClass" */
838 el
->name
= talloc_strdup(el
, local_attr
);
840 /* Convert all remote objectClasses */
841 for (i
= 0; i
< el
->num_values
; i
++) {
842 el
->values
[i
] = map_objectclass_convert_remote(module
, el
->values
, &oc
->values
[i
]);
845 val
.data
= (uint8_t *)talloc_strdup(el
->values
, data
->add_objectclass
);
846 val
.length
= strlen((char *)val
.data
);
848 /* Remove last value if it was the string in data->add_objectclass (eg samba4top, extensibleObject) */
849 if (ldb_val_equal_exact(&val
, &el
->values
[i
-1])) {
851 el
->values
= talloc_realloc(el
, el
->values
, struct ldb_val
, el
->num_values
);
852 if (el
->values
== NULL
) {
862 static const struct ldb_map_attribute objectclass_convert_map
= {
863 .local_name
= "objectClass",
864 .type
= LDB_MAP_CONVERT
,
867 .remote_name
= "objectClass",
868 .convert_local
= map_objectclass_convert_local
,
869 .convert_remote
= map_objectclass_convert_remote
,
875 /* Mappings for searches on objectClass= assuming a one-to-one
876 * mapping. Needed because this is a generate operator for the
878 static int map_objectclass_convert_operator(struct ldb_module
*module
, void *mem_ctx
,
879 struct ldb_parse_tree
**new, const struct ldb_parse_tree
*tree
)
882 return map_subtree_collect_remote_simple(module
, mem_ctx
, new, tree
, &objectclass_convert_map
);
885 /* Auxiliary request construction
886 * ============================== */
888 /* Build a request to search a record by its DN. */
889 struct ldb_request
*map_search_base_req(struct map_context
*ac
, struct ldb_dn
*dn
, const char * const *attrs
, struct ldb_parse_tree
*tree
, void *context
, ldb_map_callback_t callback
)
891 struct ldb_parse_tree
*search_tree
;
892 struct ldb_context
*ldb
;
893 struct ldb_request
*req
;
896 ldb
= ldb_module_get_ctx(ac
->module
);
901 search_tree
= ldb_parse_tree(ac
, NULL
);
902 if (search_tree
== NULL
) {
907 ret
= ldb_build_search_req_ex(&req
, ldb
, ac
,
913 LDB_REQ_SET_LOCATION(req
);
914 if (ret
!= LDB_SUCCESS
) {
921 /* Build a request to update the 'IS_MAPPED' attribute */
922 struct ldb_request
*map_build_fixup_req(struct map_context
*ac
,
923 struct ldb_dn
*olddn
,
924 struct ldb_dn
*newdn
,
926 ldb_map_callback_t callback
)
928 struct ldb_context
*ldb
;
929 struct ldb_request
*req
;
930 struct ldb_message
*msg
;
934 ldb
= ldb_module_get_ctx(ac
->module
);
936 /* Prepare message */
937 msg
= ldb_msg_new(ac
);
943 /* Update local 'IS_MAPPED' to the new remote DN */
944 msg
->dn
= ldb_dn_copy(msg
, olddn
);
945 dn
= ldb_dn_alloc_linearized(msg
, newdn
);
946 if ( ! dn
|| ! ldb_dn_validate(msg
->dn
)) {
949 if (ldb_msg_append_string(msg
, IS_MAPPED
, dn
, LDB_FLAG_MOD_REPLACE
) != 0) {
953 /* Prepare request */
954 ret
= ldb_build_mod_req(&req
, ldb
,
958 LDB_REQ_SET_LOCATION(req
);
959 if (ret
!= LDB_SUCCESS
) {
962 talloc_steal(req
, msg
);
970 /* Module initialization
971 * ===================== */
974 /* Builtin mappings for DNs and objectClasses */
975 static const struct ldb_map_attribute builtin_attribute_maps
[] = {
978 .type
= LDB_MAP_CONVERT
,
982 .convert_local
= ldb_dn_convert_local
,
983 .convert_remote
= ldb_dn_convert_remote
,
992 static const struct ldb_map_attribute objectclass_attribute_map
= {
993 .local_name
= "objectClass",
994 .type
= LDB_MAP_GENERATE
,
995 .convert_operator
= map_objectclass_convert_operator
,
998 .remote_names
= { "objectClass", NULL
},
999 .generate_local
= map_objectclass_generate_local
,
1000 .generate_remote
= map_objectclass_generate_remote
,
1006 /* Find the special 'MAP_DN_NAME' record and store local and remote
1007 * base DNs in private data. */
1008 static int map_init_dns(struct ldb_module
*module
, struct ldb_map_context
*data
, const char *name
)
1010 static const char * const attrs
[] = { MAP_DN_FROM
, MAP_DN_TO
, NULL
};
1011 struct ldb_context
*ldb
;
1013 struct ldb_message
*msg
;
1014 struct ldb_result
*res
;
1018 data
->local_base_dn
= NULL
;
1019 data
->remote_base_dn
= NULL
;
1023 ldb
= ldb_module_get_ctx(module
);
1025 dn
= ldb_dn_new_fmt(data
, ldb
, "%s=%s", MAP_DN_NAME
, name
);
1026 if ( ! ldb_dn_validate(dn
)) {
1027 ldb_debug(ldb
, LDB_DEBUG_ERROR
, "ldb_map: "
1028 "Failed to construct '%s' DN!", MAP_DN_NAME
);
1029 return LDB_ERR_OPERATIONS_ERROR
;
1032 ret
= ldb_search(ldb
, data
, &res
, dn
, LDB_SCOPE_BASE
, attrs
, NULL
);
1034 if (ret
!= LDB_SUCCESS
) {
1037 if (res
->count
== 0) {
1038 ldb_debug(ldb
, LDB_DEBUG_ERROR
, "ldb_map: "
1039 "No results for '%s=%s'!", MAP_DN_NAME
, name
);
1041 return LDB_ERR_CONSTRAINT_VIOLATION
;
1043 if (res
->count
> 1) {
1044 ldb_debug(ldb
, LDB_DEBUG_ERROR
, "ldb_map: "
1045 "Too many results for '%s=%s'!", MAP_DN_NAME
, name
);
1047 return LDB_ERR_CONSTRAINT_VIOLATION
;
1051 data
->local_base_dn
= ldb_msg_find_attr_as_dn(ldb
, data
, msg
, MAP_DN_FROM
);
1052 data
->remote_base_dn
= ldb_msg_find_attr_as_dn(ldb
, data
, msg
, MAP_DN_TO
);
1058 /* Store attribute maps and objectClass maps in private data. */
1059 static int map_init_maps(struct ldb_module
*module
, struct ldb_map_context
*data
,
1060 const struct ldb_map_attribute
*attrs
,
1061 const struct ldb_map_objectclass
*ocls
,
1062 const char * const *wildcard_attributes
)
1064 unsigned int i
, j
, last
;
1067 /* Count specified attribute maps */
1068 for (i
= 0; attrs
[i
].local_name
; i
++) /* noop */ ;
1069 /* Count built-in attribute maps */
1070 for (j
= 0; builtin_attribute_maps
[j
].local_name
; j
++) /* noop */ ;
1072 /* Store list of attribute maps */
1073 data
->attribute_maps
= talloc_array(data
, struct ldb_map_attribute
, i
+j
+2);
1074 if (data
->attribute_maps
== NULL
) {
1076 return LDB_ERR_OPERATIONS_ERROR
;
1079 /* Specified ones go first */
1080 for (i
= 0; attrs
[i
].local_name
; i
++) {
1081 data
->attribute_maps
[last
] = attrs
[i
];
1085 /* Built-in ones go last */
1086 for (i
= 0; builtin_attribute_maps
[i
].local_name
; i
++) {
1087 data
->attribute_maps
[last
] = builtin_attribute_maps
[i
];
1091 if (data
->add_objectclass
) {
1092 /* ObjectClass one is very last, if required */
1093 data
->attribute_maps
[last
] = objectclass_attribute_map
;
1096 data
->attribute_maps
[last
] = objectclass_convert_map
;
1100 /* Ensure 'local_name == NULL' for the last entry */
1101 memset(&data
->attribute_maps
[last
], 0, sizeof(struct ldb_map_attribute
));
1103 /* Store list of objectClass maps */
1104 data
->objectclass_maps
= ocls
;
1106 data
->wildcard_attributes
= wildcard_attributes
;
1111 /* Initialize global private data. */
1112 _PUBLIC_
int ldb_map_init(struct ldb_module
*module
, const struct ldb_map_attribute
*attrs
,
1113 const struct ldb_map_objectclass
*ocls
,
1114 const char * const *wildcard_attributes
,
1115 const char *add_objectclass
,
1118 struct map_private
*data
;
1121 /* Prepare private data */
1122 data
= talloc_zero(module
, struct map_private
);
1125 return LDB_ERR_OPERATIONS_ERROR
;
1128 ldb_module_set_private(module
, data
);
1130 data
->context
= talloc_zero(data
, struct ldb_map_context
);
1131 if (!data
->context
) {
1133 return LDB_ERR_OPERATIONS_ERROR
;
1136 /* Store local and remote baseDNs */
1137 ret
= map_init_dns(module
, data
->context
, name
);
1138 if (ret
!= LDB_SUCCESS
) {
1143 data
->context
->add_objectclass
= add_objectclass
;
1145 /* Store list of attribute and objectClass maps */
1146 ret
= map_init_maps(module
, data
->context
, attrs
, ocls
, wildcard_attributes
);
1147 if (ret
!= LDB_SUCCESS
) {