s3-libads: Improve service principle guessing.
[Samba.git] / lib / ldb / ldb_map / ldb_map.c
blob66b00592921e994164d33462a3d995d92f94eda6
1 /*
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
10 ** under the LGPL
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/>.
27 /*
28 * Name: ldb
30 * Component: ldb ldb_map module
32 * Description: Map portions of data into a different format on a
33 * remote partition.
35 * Author: Jelmer Vernooij, Martin Kuehl
38 #include "replace.h"
39 #include "system/filesys.h"
40 #include "system/time.h"
41 #include "ldb_map.h"
42 #include "ldb_map_private.h"
44 #ifndef _PUBLIC_
45 #define _PUBLIC_
46 #endif
48 /* Description of the provided ldb requests:
49 - special attribute 'isMapped'
51 - search:
52 - if parse tree can be split
53 - search remote records w/ remote attrs and parse tree
54 - otherwise
55 - enumerate all remote records
56 - for each remote result
57 - map remote result to local message
58 - search local result
59 - is present
60 - merge local into remote result
61 - run callback on merged result
62 - otherwise
63 - run callback on remote result
65 - add:
66 - split message into local and remote part
67 - if local message is not empty
68 - add isMapped to local message
69 - add local message
70 - add remote message
72 - modify:
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
77 - if present
78 - modify local record
79 - otherwise
80 - add local message
81 - modify remote record
83 - delete:
84 - search for local record
85 - if present
86 - delete local record
87 - delete remote record
89 - rename:
90 - search for local record
91 - if present
92 - rename 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);
120 if (ac == NULL) {
121 ldb_set_errstring(ldb, "Out of Memory");
122 return NULL;
125 ac->module = module;
126 ac->req = req;
128 return ac;
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) {
140 return false;
143 return true;
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)) {
153 talloc_free(new_dn);
154 return NULL;
157 /* may be we don't need to rebase at all */
158 if ( ! data->remote_base_dn || ! data->local_base_dn) {
159 return new_dn;
162 if ( ! ldb_dn_remove_base_components(new_dn, ldb_dn_get_comp_num(data->remote_base_dn))) {
163 talloc_free(new_dn);
164 return NULL;
167 if ( ! ldb_dn_add_base(new_dn, data->local_base_dn)) {
168 talloc_free(new_dn);
169 return NULL;
172 return new_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)) {
182 talloc_free(new_dn);
183 return NULL;
186 /* may be we don't need to rebase at all */
187 if ( ! data->remote_base_dn || ! data->local_base_dn) {
188 return new_dn;
191 if ( ! ldb_dn_remove_base_components(new_dn, ldb_dn_get_comp_num(data->local_base_dn))) {
192 talloc_free(new_dn);
193 return NULL;
196 if ( ! ldb_dn_add_base(new_dn, data->remote_base_dn)) {
197 talloc_free(new_dn);
198 return NULL;
201 return new_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) {
215 case LDB_SEARCH:
216 if (request->op.search.base) {
217 request->op.search.base = ldb_dn_rebase_remote(request, data, request->op.search.base);
218 } else {
219 request->op.search.base = data->remote_base_dn;
220 /* TODO: adjust scope? */
222 break;
224 case LDB_ADD:
225 msg = ldb_msg_copy_shallow(request, request->op.add.message);
226 if (msg == NULL) {
227 return LDB_ERR_OPERATIONS_ERROR;
229 msg->dn = ldb_dn_rebase_remote(msg, data, msg->dn);
230 request->op.add.message = msg;
231 break;
233 case LDB_MODIFY:
234 msg = ldb_msg_copy_shallow(request, request->op.mod.message);
235 if (msg == NULL) {
236 return LDB_ERR_OPERATIONS_ERROR;
238 msg->dn = ldb_dn_rebase_remote(msg, data, msg->dn);
239 request->op.mod.message = msg;
240 break;
242 case LDB_DELETE:
243 request->op.del.dn = ldb_dn_rebase_remote(request, data, request->op.del.dn);
244 break;
246 case LDB_RENAME:
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);
249 break;
251 default:
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)
267 unsigned int i;
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];
275 return NULL;
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)
281 unsigned int i;
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];
289 return NULL;
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)
295 unsigned int i;
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];
308 return NULL;
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;
316 unsigned int i, j;
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];
324 switch (map->type) {
325 case LDB_MAP_IGNORE:
326 break;
328 case LDB_MAP_KEEP:
329 if (ldb_attr_cmp(map->local_name, name) == 0) {
330 return map;
332 break;
334 case LDB_MAP_RENAME:
335 case LDB_MAP_RENDROP:
336 case LDB_MAP_CONVERT:
337 if (ldb_attr_cmp(map->u.rename.remote_name, name) == 0) {
338 return map;
340 break;
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) {
345 return map;
348 break;
352 /* We didn't find it, so return the wildcard record if one was configured */
353 return wildcard;
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);
365 if (map == NULL) {
366 return false;
368 if (map->type == LDB_MAP_IGNORE) {
369 return false;
372 return true;
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)
378 if (map == NULL) {
379 return talloc_strdup(mem_ctx, attr);
382 switch (map->type) {
383 case LDB_MAP_KEEP:
384 return talloc_strdup(mem_ctx, attr);
386 case LDB_MAP_RENAME:
387 case LDB_MAP_RENDROP:
388 case LDB_MAP_CONVERT:
389 return talloc_strdup(mem_ctx, map->u.rename.remote_name);
391 default:
392 return NULL;
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)
399 if (map == NULL) {
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) {
422 map_oom(module);
423 return -1;
426 for (k = 0; k < j; k++) {
427 (*attrs)[i + k] = more_attrs[k];
430 (*attrs)[i+k] = NULL;
432 return 0;
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);
461 /* Mapping DNs
462 * =========== */
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) {
470 return true;
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;
484 const char *name;
485 struct ldb_val value;
486 int i, ret;
488 if (dn == NULL) {
489 return NULL;
492 ldb = ldb_module_get_ctx(module);
494 newdn = ldb_dn_copy(mem_ctx, dn);
495 if (newdn == NULL) {
496 map_oom(module);
497 return NULL;
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... */
505 if (map == NULL) {
506 map_type = LDB_MAP_KEEP;
507 } else {
508 map_type = map->type;
511 switch (map_type) {
512 case LDB_MAP_IGNORE:
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));
517 goto failed;
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));
524 goto failed;
526 /* fall through */
527 case LDB_MAP_KEEP:
528 case LDB_MAP_RENAME:
529 case LDB_MAP_RENDROP:
530 name = map_attr_map_local(newdn, map, ldb_dn_get_component_name(dn, i));
531 if (name == NULL) goto failed;
533 value = ldb_val_map_local(module, newdn, map, ldb_dn_get_component_val(dn, i));
534 if (value.data == NULL) goto failed;
536 ret = ldb_dn_set_component(newdn, i, name, value);
537 if (ret != LDB_SUCCESS) {
538 goto failed;
541 break;
545 return newdn;
547 failed:
548 talloc_free(newdn);
549 return NULL;
552 /* Map a DN into the local partition. */
553 struct ldb_dn *ldb_dn_map_remote(struct ldb_module *module, void *mem_ctx, struct ldb_dn *dn)
555 const struct ldb_map_context *data = map_get_context(module);
556 struct ldb_context *ldb;
557 struct ldb_dn *newdn;
558 const struct ldb_map_attribute *map;
559 enum ldb_map_attr_type map_type;
560 const char *name;
561 struct ldb_val value;
562 int i, ret;
564 if (dn == NULL) {
565 return NULL;
568 ldb = ldb_module_get_ctx(module);
570 newdn = ldb_dn_copy(mem_ctx, dn);
571 if (newdn == NULL) {
572 map_oom(module);
573 return NULL;
576 /* For each RDN, map the component name and possibly the value */
577 for (i = 0; i < ldb_dn_get_comp_num(newdn); i++) {
578 map = map_attr_find_remote(data, ldb_dn_get_component_name(dn, i));
580 /* Unknown attribute - leave this RDN as is and hope the best... */
581 if (map == NULL) {
582 map_type = LDB_MAP_KEEP;
583 } else {
584 map_type = map->type;
587 switch (map_type) {
588 case LDB_MAP_IGNORE:
589 case LDB_MAP_GENERATE:
590 ldb_debug(ldb, LDB_DEBUG_ERROR, "ldb_map: "
591 "LDB_MAP_IGNORE/LDB_MAP_GENERATE attribute '%s' "
592 "used in DN!", ldb_dn_get_component_name(dn, i));
593 goto failed;
595 case LDB_MAP_CONVERT:
596 if (map->u.convert.convert_remote == NULL) {
597 ldb_debug(ldb, LDB_DEBUG_ERROR, "ldb_map: "
598 "'convert_remote' not set for attribute '%s' "
599 "used in DN!", ldb_dn_get_component_name(dn, i));
600 goto failed;
602 /* fall through */
603 case LDB_MAP_KEEP:
604 case LDB_MAP_RENAME:
605 case LDB_MAP_RENDROP:
606 name = map_attr_map_remote(newdn, map, ldb_dn_get_component_name(dn, i));
607 if (name == NULL) goto failed;
609 value = ldb_val_map_remote(module, newdn, map, ldb_dn_get_component_val(dn, i));
610 if (value.data == NULL) goto failed;
612 ret = ldb_dn_set_component(newdn, i, name, value);
613 if (ret != LDB_SUCCESS) {
614 goto failed;
617 break;
621 return newdn;
623 failed:
624 talloc_free(newdn);
625 return NULL;
628 /* Map a DN and its base into the local partition. */
629 /* TODO: This should not be required with GUIDs. */
630 struct ldb_dn *ldb_dn_map_rebase_remote(struct ldb_module *module, void *mem_ctx, struct ldb_dn *dn)
632 const struct ldb_map_context *data = map_get_context(module);
633 struct ldb_dn *dn1, *dn2;
635 dn1 = ldb_dn_rebase_local(mem_ctx, data, dn);
636 dn2 = ldb_dn_map_remote(module, mem_ctx, dn1);
638 talloc_free(dn1);
639 return dn2;
643 /* Converting DNs and objectClasses (as ldb values)
644 * ================================================ */
646 /* Map a DN contained in an ldb value into the remote partition. */
647 static struct ldb_val ldb_dn_convert_local(struct ldb_module *module, void *mem_ctx, const struct ldb_val *val)
649 struct ldb_context *ldb;
650 struct ldb_dn *dn, *newdn;
651 struct ldb_val newval;
653 ldb = ldb_module_get_ctx(module);
655 dn = ldb_dn_from_ldb_val(mem_ctx, ldb, val);
656 if (! ldb_dn_validate(dn)) {
657 newval.length = 0;
658 newval.data = NULL;
659 talloc_free(dn);
660 return newval;
662 newdn = ldb_dn_map_local(module, mem_ctx, dn);
663 talloc_free(dn);
665 newval.length = 0;
666 newval.data = (uint8_t *)ldb_dn_alloc_linearized(mem_ctx, newdn);
667 if (newval.data) {
668 newval.length = strlen((char *)newval.data);
670 talloc_free(newdn);
672 return newval;
675 /* Map a DN contained in an ldb value into the local partition. */
676 static struct ldb_val ldb_dn_convert_remote(struct ldb_module *module, void *mem_ctx, const struct ldb_val *val)
678 struct ldb_context *ldb;
679 struct ldb_dn *dn, *newdn;
680 struct ldb_val newval;
682 ldb = ldb_module_get_ctx(module);
684 dn = ldb_dn_from_ldb_val(mem_ctx, ldb, val);
685 if (! ldb_dn_validate(dn)) {
686 newval.length = 0;
687 newval.data = NULL;
688 talloc_free(dn);
689 return newval;
691 newdn = ldb_dn_map_remote(module, mem_ctx, dn);
692 talloc_free(dn);
694 newval.length = 0;
695 newval.data = (uint8_t *)ldb_dn_alloc_linearized(mem_ctx, newdn);
696 if (newval.data) {
697 newval.length = strlen((char *)newval.data);
699 talloc_free(newdn);
701 return newval;
704 /* Map an objectClass into the remote partition. */
705 static struct ldb_val map_objectclass_convert_local(struct ldb_module *module, void *mem_ctx, const struct ldb_val *val)
707 const struct ldb_map_context *data = map_get_context(module);
708 const char *name = (char *)val->data;
709 const struct ldb_map_objectclass *map = map_objectclass_find_local(data, name);
710 struct ldb_val newval;
712 if (map) {
713 newval.data = (uint8_t*)talloc_strdup(mem_ctx, map->remote_name);
714 newval.length = strlen((char *)newval.data);
715 return newval;
718 return ldb_val_dup(mem_ctx, val);
721 /* Generate a remote message with a mapped objectClass. */
722 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)
724 const struct ldb_map_context *data = map_get_context(module);
725 struct ldb_context *ldb;
726 struct ldb_message_element *el, *oc;
727 struct ldb_val val;
728 bool found_extensibleObject = false;
729 unsigned int i;
731 ldb = ldb_module_get_ctx(module);
733 /* Find old local objectClass */
734 oc = ldb_msg_find_element(old, "objectClass");
735 if (oc == NULL) {
736 return;
739 /* Prepare new element */
740 el = talloc_zero(remote, struct ldb_message_element);
741 if (el == NULL) {
742 ldb_oom(ldb);
743 return; /* TODO: fail? */
746 /* Copy local objectClass element, reverse space for an extra value */
747 el->num_values = oc->num_values + 1;
748 el->values = talloc_array(el, struct ldb_val, el->num_values);
749 if (el->values == NULL) {
750 talloc_free(el);
751 ldb_oom(ldb);
752 return; /* TODO: fail? */
755 /* Copy local element name "objectClass" */
756 el->name = talloc_strdup(el, local_attr);
758 /* Convert all local objectClasses */
759 for (i = 0; i < el->num_values - 1; i++) {
760 el->values[i] = map_objectclass_convert_local(module, el->values, &oc->values[i]);
761 if (ldb_attr_cmp((char *)el->values[i].data, data->add_objectclass) == 0) {
762 found_extensibleObject = true;
766 if (!found_extensibleObject) {
767 val.data = (uint8_t *)talloc_strdup(el->values, data->add_objectclass);
768 val.length = strlen((char *)val.data);
770 /* Append additional objectClass data->add_objectclass */
771 el->values[i] = val;
772 } else {
773 el->num_values--;
776 /* Add new objectClass to remote message */
777 ldb_msg_add(remote, el, 0);
780 /* Map an objectClass into the local partition. */
781 static struct ldb_val map_objectclass_convert_remote(struct ldb_module *module, void *mem_ctx, const struct ldb_val *val)
783 const struct ldb_map_context *data = map_get_context(module);
784 const char *name = (char *)val->data;
785 const struct ldb_map_objectclass *map = map_objectclass_find_remote(data, name);
786 struct ldb_val newval;
788 if (map) {
789 newval.data = (uint8_t*)talloc_strdup(mem_ctx, map->local_name);
790 newval.length = strlen((char *)newval.data);
791 return newval;
794 return ldb_val_dup(mem_ctx, val);
797 /* Generate a local message with a mapped objectClass. */
798 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)
800 const struct ldb_map_context *data = map_get_context(module);
801 struct ldb_context *ldb;
802 struct ldb_message_element *el, *oc;
803 struct ldb_val val;
804 unsigned int i;
806 ldb = ldb_module_get_ctx(module);
808 /* Find old remote objectClass */
809 oc = ldb_msg_find_element(remote, "objectClass");
810 if (oc == NULL) {
811 return NULL;
814 /* Prepare new element */
815 el = talloc_zero(mem_ctx, struct ldb_message_element);
816 if (el == NULL) {
817 ldb_oom(ldb);
818 return NULL;
821 /* Copy remote objectClass element */
822 el->num_values = oc->num_values;
823 el->values = talloc_array(el, struct ldb_val, el->num_values);
824 if (el->values == NULL) {
825 talloc_free(el);
826 ldb_oom(ldb);
827 return NULL;
830 /* Copy remote element name "objectClass" */
831 el->name = talloc_strdup(el, local_attr);
833 /* Convert all remote objectClasses */
834 for (i = 0; i < el->num_values; i++) {
835 el->values[i] = map_objectclass_convert_remote(module, el->values, &oc->values[i]);
838 val.data = (uint8_t *)talloc_strdup(el->values, data->add_objectclass);
839 val.length = strlen((char *)val.data);
841 /* Remove last value if it was the string in data->add_objectclass (eg samba4top, extensibleObject) */
842 if (ldb_val_equal_exact(&val, &el->values[i-1])) {
843 el->num_values--;
844 el->values = talloc_realloc(el, el->values, struct ldb_val, el->num_values);
845 if (el->values == NULL) {
846 talloc_free(el);
847 ldb_oom(ldb);
848 return NULL;
852 return el;
855 static const struct ldb_map_attribute objectclass_convert_map = {
856 .local_name = "objectClass",
857 .type = LDB_MAP_CONVERT,
858 .u = {
859 .convert = {
860 .remote_name = "objectClass",
861 .convert_local = map_objectclass_convert_local,
862 .convert_remote = map_objectclass_convert_remote,
868 /* Mappings for searches on objectClass= assuming a one-to-one
869 * mapping. Needed because this is a generate operator for the
870 * add/modify code */
871 static int map_objectclass_convert_operator(struct ldb_module *module, void *mem_ctx,
872 struct ldb_parse_tree **new, const struct ldb_parse_tree *tree)
875 return map_subtree_collect_remote_simple(module, mem_ctx, new, tree, &objectclass_convert_map);
878 /* Auxiliary request construction
879 * ============================== */
881 /* Build a request to search a record by its DN. */
882 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)
884 struct ldb_parse_tree *search_tree;
885 struct ldb_context *ldb;
886 struct ldb_request *req;
887 int ret;
889 ldb = ldb_module_get_ctx(ac->module);
891 if (tree) {
892 search_tree = tree;
893 } else {
894 search_tree = ldb_parse_tree(ac, NULL);
895 if (search_tree == NULL) {
896 return NULL;
900 ret = ldb_build_search_req_ex(&req, ldb, ac,
901 dn, LDB_SCOPE_BASE,
902 search_tree, attrs,
903 NULL,
904 context, callback,
905 ac->req);
906 LDB_REQ_SET_LOCATION(req);
907 if (ret != LDB_SUCCESS) {
908 return NULL;
911 return req;
914 /* Build a request to update the 'IS_MAPPED' attribute */
915 struct ldb_request *map_build_fixup_req(struct map_context *ac,
916 struct ldb_dn *olddn,
917 struct ldb_dn *newdn,
918 void *context,
919 ldb_map_callback_t callback)
921 struct ldb_context *ldb;
922 struct ldb_request *req;
923 struct ldb_message *msg;
924 const char *dn;
925 int ret;
927 ldb = ldb_module_get_ctx(ac->module);
929 /* Prepare message */
930 msg = ldb_msg_new(ac);
931 if (msg == NULL) {
932 map_oom(ac->module);
933 return NULL;
936 /* Update local 'IS_MAPPED' to the new remote DN */
937 msg->dn = ldb_dn_copy(msg, olddn);
938 dn = ldb_dn_alloc_linearized(msg, newdn);
939 if ( ! dn || ! ldb_dn_validate(msg->dn)) {
940 goto failed;
942 if (ldb_msg_add_empty(msg, IS_MAPPED, LDB_FLAG_MOD_REPLACE, NULL) != 0) {
943 goto failed;
945 if (ldb_msg_add_string(msg, IS_MAPPED, dn) != 0) {
946 goto failed;
949 /* Prepare request */
950 ret = ldb_build_mod_req(&req, ldb,
951 ac, msg, NULL,
952 context, callback,
953 ac->req);
954 LDB_REQ_SET_LOCATION(req);
955 if (ret != LDB_SUCCESS) {
956 goto failed;
958 talloc_steal(req, msg);
960 return req;
961 failed:
962 talloc_free(msg);
963 return NULL;
966 /* Module initialization
967 * ===================== */
970 /* Builtin mappings for DNs and objectClasses */
971 static const struct ldb_map_attribute builtin_attribute_maps[] = {
973 .local_name = "dn",
974 .type = LDB_MAP_CONVERT,
975 .u = {
976 .convert = {
977 .remote_name = "dn",
978 .convert_local = ldb_dn_convert_local,
979 .convert_remote = ldb_dn_convert_remote,
984 .local_name = NULL,
988 static const struct ldb_map_attribute objectclass_attribute_map = {
989 .local_name = "objectClass",
990 .type = LDB_MAP_GENERATE,
991 .convert_operator = map_objectclass_convert_operator,
992 .u = {
993 .generate = {
994 .remote_names = { "objectClass", NULL },
995 .generate_local = map_objectclass_generate_local,
996 .generate_remote = map_objectclass_generate_remote,
1002 /* Find the special 'MAP_DN_NAME' record and store local and remote
1003 * base DNs in private data. */
1004 static int map_init_dns(struct ldb_module *module, struct ldb_map_context *data, const char *name)
1006 static const char * const attrs[] = { MAP_DN_FROM, MAP_DN_TO, NULL };
1007 struct ldb_context *ldb;
1008 struct ldb_dn *dn;
1009 struct ldb_message *msg;
1010 struct ldb_result *res;
1011 int ret;
1013 if (!name) {
1014 data->local_base_dn = NULL;
1015 data->remote_base_dn = NULL;
1016 return LDB_SUCCESS;
1019 ldb = ldb_module_get_ctx(module);
1021 dn = ldb_dn_new_fmt(data, ldb, "%s=%s", MAP_DN_NAME, name);
1022 if ( ! ldb_dn_validate(dn)) {
1023 ldb_debug(ldb, LDB_DEBUG_ERROR, "ldb_map: "
1024 "Failed to construct '%s' DN!", MAP_DN_NAME);
1025 return LDB_ERR_OPERATIONS_ERROR;
1028 ret = ldb_search(ldb, data, &res, dn, LDB_SCOPE_BASE, attrs, NULL);
1029 talloc_free(dn);
1030 if (ret != LDB_SUCCESS) {
1031 return ret;
1033 if (res->count == 0) {
1034 ldb_debug(ldb, LDB_DEBUG_ERROR, "ldb_map: "
1035 "No results for '%s=%s'!", MAP_DN_NAME, name);
1036 talloc_free(res);
1037 return LDB_ERR_CONSTRAINT_VIOLATION;
1039 if (res->count > 1) {
1040 ldb_debug(ldb, LDB_DEBUG_ERROR, "ldb_map: "
1041 "Too many results for '%s=%s'!", MAP_DN_NAME, name);
1042 talloc_free(res);
1043 return LDB_ERR_CONSTRAINT_VIOLATION;
1046 msg = res->msgs[0];
1047 data->local_base_dn = ldb_msg_find_attr_as_dn(ldb, data, msg, MAP_DN_FROM);
1048 data->remote_base_dn = ldb_msg_find_attr_as_dn(ldb, data, msg, MAP_DN_TO);
1049 talloc_free(res);
1051 return LDB_SUCCESS;
1054 /* Store attribute maps and objectClass maps in private data. */
1055 static int map_init_maps(struct ldb_module *module, struct ldb_map_context *data,
1056 const struct ldb_map_attribute *attrs,
1057 const struct ldb_map_objectclass *ocls,
1058 const char * const *wildcard_attributes)
1060 unsigned int i, j, last;
1061 last = 0;
1063 /* Count specified attribute maps */
1064 for (i = 0; attrs[i].local_name; i++) /* noop */ ;
1065 /* Count built-in attribute maps */
1066 for (j = 0; builtin_attribute_maps[j].local_name; j++) /* noop */ ;
1068 /* Store list of attribute maps */
1069 data->attribute_maps = talloc_array(data, struct ldb_map_attribute, i+j+2);
1070 if (data->attribute_maps == NULL) {
1071 map_oom(module);
1072 return LDB_ERR_OPERATIONS_ERROR;
1075 /* Specified ones go first */
1076 for (i = 0; attrs[i].local_name; i++) {
1077 data->attribute_maps[last] = attrs[i];
1078 last++;
1081 /* Built-in ones go last */
1082 for (i = 0; builtin_attribute_maps[i].local_name; i++) {
1083 data->attribute_maps[last] = builtin_attribute_maps[i];
1084 last++;
1087 if (data->add_objectclass) {
1088 /* ObjectClass one is very last, if required */
1089 data->attribute_maps[last] = objectclass_attribute_map;
1090 last++;
1091 } else if (ocls) {
1092 data->attribute_maps[last] = objectclass_convert_map;
1093 last++;
1096 /* Ensure 'local_name == NULL' for the last entry */
1097 memset(&data->attribute_maps[last], 0, sizeof(struct ldb_map_attribute));
1099 /* Store list of objectClass maps */
1100 data->objectclass_maps = ocls;
1102 data->wildcard_attributes = wildcard_attributes;
1104 return LDB_SUCCESS;
1107 /* Initialize global private data. */
1108 _PUBLIC_ int ldb_map_init(struct ldb_module *module, const struct ldb_map_attribute *attrs,
1109 const struct ldb_map_objectclass *ocls,
1110 const char * const *wildcard_attributes,
1111 const char *add_objectclass,
1112 const char *name)
1114 struct map_private *data;
1115 int ret;
1117 /* Prepare private data */
1118 data = talloc_zero(module, struct map_private);
1119 if (data == NULL) {
1120 map_oom(module);
1121 return LDB_ERR_OPERATIONS_ERROR;
1124 ldb_module_set_private(module, data);
1126 data->context = talloc_zero(data, struct ldb_map_context);
1127 if (!data->context) {
1128 map_oom(module);
1129 return LDB_ERR_OPERATIONS_ERROR;
1132 /* Store local and remote baseDNs */
1133 ret = map_init_dns(module, data->context, name);
1134 if (ret != LDB_SUCCESS) {
1135 talloc_free(data);
1136 return ret;
1139 data->context->add_objectclass = add_objectclass;
1141 /* Store list of attribute and objectClass maps */
1142 ret = map_init_maps(module, data->context, attrs, ocls, wildcard_attributes);
1143 if (ret != LDB_SUCCESS) {
1144 talloc_free(data);
1145 return ret;
1148 return LDB_SUCCESS;