Merge ldb_search() and ldb_search_exp_fmt() into a simgle function.
[Samba.git] / source4 / lib / ldb / ldb_map / ldb_map.c
blobe9129c812b27cd354aea138a693ac3c53646c7c9
1 /*
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
9 ** under the LGPL
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/>.
26 /*
27 * Name: ldb
29 * Component: ldb ldb_map module
31 * Description: Map portions of data into a different format on a
32 * remote partition.
34 * Author: Jelmer Vernooij, Martin Kuehl
37 #include "ldb_includes.h"
39 #include "ldb_map.h"
40 #include "ldb_map_private.h"
42 #ifndef _PUBLIC_
43 #define _PUBLIC_
44 #endif
46 /* Description of the provided ldb requests:
47 - special attribute 'isMapped'
49 - search:
50 - if parse tree can be split
51 - search remote records w/ remote attrs and parse tree
52 - otherwise
53 - enumerate all remote records
54 - for each remote result
55 - map remote result to local message
56 - search local result
57 - is present
58 - merge local into remote result
59 - run callback on merged result
60 - otherwise
61 - run callback on remote result
63 - add:
64 - split message into local and remote part
65 - if local message is not empty
66 - add isMapped to local message
67 - add local message
68 - add remote message
70 - modify:
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
75 - if present
76 - modify local record
77 - otherwise
78 - add local message
79 - modify remote record
81 - delete:
82 - search for local record
83 - if present
84 - delete local record
85 - delete remote record
87 - rename:
88 - search for local record
89 - if present
90 - rename 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);
114 if (ac == NULL) {
115 map_oom(h->module);
116 return NULL;
119 ac->module = h->module;
120 ac->orig_req = req;
122 return ac;
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);
131 if (sc == NULL) {
132 map_oom(ac->module);
133 return NULL;
136 sc->ac = ac;
137 sc->local_res = NULL;
138 sc->remote_res = ares;
140 return sc;
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);
150 if (h == NULL) {
151 map_oom(module);
152 return NULL;
155 h->module = module;
157 ac = map_init_context(h, req);
158 if (ac == NULL) {
159 talloc_free(h);
160 return NULL;
163 h->private_data = (void *)ac;
165 h->state = LDB_ASYNC_INIT;
166 h->status = LDB_SUCCESS;
168 return h;
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) {
181 return false;
184 return true;
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)) {
194 talloc_free(new_dn);
195 return NULL;
198 /* may be we don't need to rebase at all */
199 if ( ! data->remote_base_dn || ! data->local_base_dn) {
200 return new_dn;
203 if ( ! ldb_dn_remove_base_components(new_dn, ldb_dn_get_comp_num(data->remote_base_dn))) {
204 talloc_free(new_dn);
205 return NULL;
208 if ( ! ldb_dn_add_base(new_dn, data->local_base_dn)) {
209 talloc_free(new_dn);
210 return NULL;
213 return new_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)) {
223 talloc_free(new_dn);
224 return NULL;
227 /* may be we don't need to rebase at all */
228 if ( ! data->remote_base_dn || ! data->local_base_dn) {
229 return new_dn;
232 if ( ! ldb_dn_remove_base_components(new_dn, ldb_dn_get_comp_num(data->local_base_dn))) {
233 talloc_free(new_dn);
234 return NULL;
237 if ( ! ldb_dn_add_base(new_dn, data->remote_base_dn)) {
238 talloc_free(new_dn);
239 return NULL;
242 return new_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) {
253 case LDB_SEARCH:
254 if (request->op.search.base) {
255 request->op.search.base = ldb_dn_rebase_remote(request, data, request->op.search.base);
256 } else {
257 request->op.search.base = data->remote_base_dn;
258 /* TODO: adjust scope? */
260 break;
262 case LDB_ADD:
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;
266 break;
268 case LDB_MODIFY:
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;
272 break;
274 case LDB_DELETE:
275 request->op.del.dn = ldb_dn_rebase_remote(request, data, request->op.del.dn);
276 break;
278 case LDB_RENAME:
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);
281 break;
283 default:
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)
299 int i;
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];
307 return NULL;
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)
313 int i;
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];
321 return NULL;
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)
327 int i;
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];
340 return NULL;
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;
348 int i, j;
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];
356 switch (map->type) {
357 case MAP_IGNORE:
358 break;
360 case MAP_KEEP:
361 if (ldb_attr_cmp(map->local_name, name) == 0) {
362 return map;
364 break;
366 case MAP_RENAME:
367 case MAP_CONVERT:
368 if (ldb_attr_cmp(map->u.rename.remote_name, name) == 0) {
369 return map;
371 break;
373 case MAP_GENERATE:
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) {
376 return map;
379 break;
383 /* We didn't find it, so return the wildcard record if one was configured */
384 return wildcard;
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);
396 if (map == NULL) {
397 return false;
399 if (map->type == MAP_IGNORE) {
400 return false;
403 return true;
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)
409 if (map == NULL) {
410 return talloc_strdup(mem_ctx, attr);
413 switch (map->type) {
414 case MAP_KEEP:
415 return talloc_strdup(mem_ctx, attr);
417 case MAP_RENAME:
418 case MAP_CONVERT:
419 return talloc_strdup(mem_ctx, map->u.rename.remote_name);
421 default:
422 return NULL;
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)
429 if (map == NULL) {
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)
445 int i, j, k;
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) {
452 map_oom(module);
453 return -1;
456 for (k = 0; k < j; k++) {
457 (*attrs)[i + k] = more_attrs[k];
460 (*attrs)[i+k] = NULL;
462 return 0;
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);
491 /* Mapping DNs
492 * =========== */
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) {
500 return true;
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;
513 const char *name;
514 struct ldb_val value;
515 int i, ret;
517 if (dn == NULL) {
518 return NULL;
521 newdn = ldb_dn_copy(mem_ctx, dn);
522 if (newdn == NULL) {
523 map_oom(module);
524 return NULL;
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... */
532 if (map == NULL) {
533 map_type = MAP_KEEP;
534 } else {
535 map_type = map->type;
538 switch (map_type) {
539 case MAP_IGNORE:
540 case MAP_GENERATE:
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));
544 goto failed;
546 case MAP_CONVERT:
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));
551 goto failed;
553 /* fall through */
554 case MAP_KEEP:
555 case MAP_RENAME:
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) {
564 goto failed;
567 break;
571 return newdn;
573 failed:
574 talloc_free(newdn);
575 return NULL;
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;
585 const char *name;
586 struct ldb_val value;
587 int i, ret;
589 if (dn == NULL) {
590 return NULL;
593 newdn = ldb_dn_copy(mem_ctx, dn);
594 if (newdn == NULL) {
595 map_oom(module);
596 return NULL;
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... */
604 if (map == NULL) {
605 map_type = MAP_KEEP;
606 } else {
607 map_type = map->type;
610 switch (map_type) {
611 case MAP_IGNORE:
612 case MAP_GENERATE:
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));
616 goto failed;
618 case MAP_CONVERT:
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));
623 goto failed;
625 /* fall through */
626 case MAP_KEEP:
627 case MAP_RENAME:
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) {
636 goto failed;
639 break;
643 return newdn;
645 failed:
646 talloc_free(newdn);
647 return NULL;
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);
660 talloc_free(dn1);
661 return dn2;
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)) {
676 newval.length = 0;
677 newval.data = NULL;
678 talloc_free(dn);
679 return newval;
681 newdn = ldb_dn_map_local(module, mem_ctx, dn);
682 talloc_free(dn);
684 newval.length = 0;
685 newval.data = (uint8_t *)ldb_dn_alloc_linearized(mem_ctx, newdn);
686 if (newval.data) {
687 newval.length = strlen((char *)newval.data);
689 talloc_free(newdn);
691 return newval;
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)) {
702 newval.length = 0;
703 newval.data = NULL;
704 talloc_free(dn);
705 return newval;
707 newdn = ldb_dn_map_remote(module, mem_ctx, dn);
708 talloc_free(dn);
710 newval.length = 0;
711 newval.data = (uint8_t *)ldb_dn_alloc_linearized(mem_ctx, newdn);
712 if (newval.data) {
713 newval.length = strlen((char *)newval.data);
715 talloc_free(newdn);
717 return newval;
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;
728 if (map) {
729 newval.data = (uint8_t*)talloc_strdup(mem_ctx, map->remote_name);
730 newval.length = strlen((char *)newval.data);
731 return newval;
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;
742 struct ldb_val val;
743 bool found_extensibleObject = false;
744 int i;
746 /* Find old local objectClass */
747 oc = ldb_msg_find_element(old, "objectClass");
748 if (oc == NULL) {
749 return;
752 /* Prepare new element */
753 el = talloc_zero(remote, struct ldb_message_element);
754 if (el == NULL) {
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) {
763 talloc_free(el);
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 */
784 el->values[i] = val;
785 } else {
786 el->num_values--;
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;
801 if (map) {
802 newval.data = (uint8_t*)talloc_strdup(mem_ctx, map->local_name);
803 newval.length = strlen((char *)newval.data);
804 return newval;
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;
815 struct ldb_val val;
816 int i;
818 /* Find old remote objectClass */
819 oc = ldb_msg_find_element(remote, "objectClass");
820 if (oc == NULL) {
821 return NULL;
824 /* Prepare new element */
825 el = talloc_zero(mem_ctx, struct ldb_message_element);
826 if (el == NULL) {
827 ldb_oom(module->ldb);
828 return NULL;
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) {
835 talloc_free(el);
836 ldb_oom(module->ldb);
837 return NULL;
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])) {
853 el->num_values--;
854 el->values = talloc_realloc(el, el->values, struct ldb_val, el->num_values);
855 if (el->values == NULL) {
856 talloc_free(el);
857 ldb_oom(module->ldb);
858 return NULL;
862 return el;
865 static const struct ldb_map_attribute objectclass_convert_map = {
866 .local_name = "objectClass",
867 .type = MAP_CONVERT,
868 .u = {
869 .convert = {
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
880 * add/modify code */
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) {
905 talloc_free(ares);
906 return LDB_SUCCESS;
909 /* We have already found a remote DN */
910 if (ac->local_dn) {
911 ldb_set_errstring(ldb, talloc_asprintf(ldb, "Too many results to base search"));
912 talloc_free(ares);
913 return LDB_ERR_OPERATIONS_ERROR;
916 /* Store local DN */
917 ac->local_dn = ares->message->dn;
919 return LDB_SUCCESS;
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);
928 if (req == NULL) {
929 map_oom(ac->module);
930 return NULL;
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;
938 if (tree) {
939 req->op.search.tree = tree;
940 } else {
941 req->op.search.tree = ldb_parse_tree(req, NULL);
942 if (req->op.search.tree == NULL) {
943 talloc_free(req);
944 return 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);
953 return 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);
968 if (tree == NULL) {
969 map_oom(ac->module);
970 return NULL;
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;
984 const char *dn;
986 /* Prepare request */
987 req = talloc_zero(ac, struct ldb_request);
988 if (req == NULL) {
989 map_oom(ac->module);
990 return NULL;
993 /* Prepare message */
994 msg = ldb_msg_new(req);
995 if (msg == NULL) {
996 map_oom(ac->module);
997 goto failed;
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)) {
1004 goto failed;
1006 if (ldb_msg_add_empty(msg, IS_MAPPED, LDB_FLAG_MOD_REPLACE, NULL) != 0) {
1007 goto failed;
1009 if (ldb_msg_add_string(msg, IS_MAPPED, dn) != 0) {
1010 goto failed;
1013 req->operation = LDB_MODIFY;
1014 req->op.mod.message = msg;
1015 req->controls = NULL;
1016 req->handle = NULL;
1017 req->context = NULL;
1018 req->callback = NULL;
1020 return req;
1022 failed:
1023 talloc_free(req);
1024 return 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)
1034 switch (ac->step) {
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;
1049 case MAP_ADD_LOCAL:
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:
1056 /* Can't happen */
1057 break;
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)
1068 switch (ac->step) {
1069 case MAP_SEARCH_REMOTE:
1070 return NULL;
1072 case MAP_ADD_LOCAL:
1073 return map_add_do_remote;
1074 case MAP_ADD_REMOTE:
1075 return NULL;
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:
1082 return NULL;
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:
1089 return NULL;
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:
1098 return NULL;
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;
1110 int ret;
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) {
1126 int i;
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;
1133 goto done;
1135 if (req->handle->status != LDB_SUCCESS) {
1136 handle->status = req->handle->status;
1137 goto done;
1139 if (req->handle->state != LDB_ASYNC_DONE) {
1140 return LDB_SUCCESS;
1143 } else {
1145 req = map_get_req(ac);
1147 ret = ldb_wait(req->handle, LDB_WAIT_NONE);
1149 if (ret != LDB_SUCCESS) {
1150 handle->status = ret;
1151 goto done;
1153 if (req->handle->status != LDB_SUCCESS) {
1154 handle->status = req->handle->status;
1155 goto done;
1157 if (req->handle->state != LDB_ASYNC_DONE) {
1158 return LDB_SUCCESS;
1161 next = map_get_next(ac);
1162 if (next) {
1163 return next(handle);
1167 ret = LDB_SUCCESS;
1169 done:
1170 handle->state = LDB_ASYNC_DONE;
1171 return ret;
1174 /* Wait for all current pending requests to finish. */
1175 static int map_wait_all(struct ldb_handle *handle)
1177 int ret;
1179 while (handle->state != LDB_ASYNC_DONE) {
1180 ret = map_wait_next(handle);
1181 if (ret != LDB_SUCCESS) {
1182 return ret;
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);
1194 } else {
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[] = {
1207 .local_name = "dn",
1208 .type = MAP_CONVERT,
1209 .u = {
1210 .convert = {
1211 .remote_name = "dn",
1212 .convert_local = ldb_dn_convert_local,
1213 .convert_remote = ldb_dn_convert_remote,
1218 .local_name = NULL,
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,
1226 .u = {
1227 .generate = {
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 };
1241 struct ldb_dn *dn;
1242 struct ldb_message *msg;
1243 struct ldb_result *res;
1244 int ret;
1246 if (!name) {
1247 data->local_base_dn = NULL;
1248 data->remote_base_dn = NULL;
1249 return LDB_SUCCESS;
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);
1260 talloc_free(dn);
1261 if (ret != LDB_SUCCESS) {
1262 return ret;
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);
1267 talloc_free(res);
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);
1273 talloc_free(res);
1274 return LDB_ERR_CONSTRAINT_VIOLATION;
1277 msg = res->msgs[0];
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);
1280 talloc_free(res);
1282 return LDB_SUCCESS;
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)
1291 int i, j, last;
1292 last = 0;
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) {
1302 map_oom(module);
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];
1309 last++;
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];
1315 last++;
1318 if (data->add_objectclass) {
1319 /* ObjectClass one is very last, if required */
1320 data->attribute_maps[last] = objectclass_attribute_map;
1321 last++;
1322 } else if (ocls) {
1323 data->attribute_maps[last] = objectclass_convert_map;
1324 last++;
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;
1335 return LDB_SUCCESS;
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,
1343 const char *name)
1345 struct map_private *data;
1346 int ret;
1348 /* Prepare private data */
1349 data = talloc_zero(module, struct map_private);
1350 if (data == NULL) {
1351 map_oom(module);
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) {
1359 map_oom(module);
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) {
1366 talloc_free(data);
1367 return ret;
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) {
1375 talloc_free(data);
1376 return ret;
1379 return LDB_SUCCESS;