summaryrefslogtreecommitdiffstats
path: root/ldb/ldb_map/ldb_map.c
blob: 72d8378a076c14f489c1e3c04cb8adb1f808c164 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
/*
   ldb database mapping module

   Copyright (C) Jelmer Vernooij 2005
   Copyright (C) Martin Kuehl <mkhl@samba.org> 2006
   Copyright (C) Simo Sorce 2008

     ** NOTE! The following LGPL license applies to the ldb
     ** library. This does NOT imply that all of Samba is released
     ** under the LGPL
   
   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 3 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with this library; if not, see <http://www.gnu.org/licenses/>.

*/

/* 
 *  Name: ldb
 *
 *  Component: ldb ldb_map module
 *
 *  Description: Map portions of data into a different format on a
 *  remote partition.
 *
 *  Author: Jelmer Vernooij, Martin Kuehl
 */

#include "ldb_includes.h"

#include "ldb_map.h"
#include "ldb_map_private.h"

#ifndef _PUBLIC_
#define _PUBLIC_
#endif

/* Description of the provided ldb requests:
 - special attribute 'isMapped'

 - search:
     - if parse tree can be split
         - search remote records w/ remote attrs and parse tree
     - otherwise
         - enumerate all remote records
     - for each remote result
         - map remote result to local message
         - search local result
         - is present
             - merge local into remote result
             - run callback on merged result
         - otherwise
             - run callback on remote result

 - add:
     - split message into local and remote part
     - if local message is not empty
         - add isMapped to local message
         - add local message
     - add remote message

 - modify:
     - split message into local and remote part
     - if local message is not empty
         - add isMapped to local message
         - search for local record
         - if present
             - modify local record
         - otherwise
             - add local message
     - modify remote record

 - delete:
     - search for local record
     - if present
         - delete local record
     - delete remote record

 - rename:
     - search for local record
     - if present
         - rename local record
         - modify local isMapped
     - rename remote record
*/



/* Private data structures
 * ======================= */

/* Global private data */
/* Extract mappings from private data. */
const struct ldb_map_context *map_get_context(struct ldb_module *module)
{
	const struct map_private *data = talloc_get_type(module->private_data, struct map_private);
	return data->context;
}

/* Create a generic request context. */
struct map_context *map_init_context(struct ldb_module *module,
					struct ldb_request *req)
{
	struct map_context *ac;

	ac = talloc_zero(req, struct map_context);
	if (ac == NULL) {
		ldb_set_errstring(module->ldb, "Out of Memory");
		return NULL;
	}

	ac->module = module;
	ac->req = req;

	return ac;
}

/* Dealing with DNs for different partitions
 * ========================================= */

/* Check whether any data should be stored in the local partition. */
bool map_check_local_db(struct ldb_module *module)
{
	const struct ldb_map_context *data = map_get_context(module);

	if (!data->remote_base_dn || !data->local_base_dn) {
		return false;
	}

	return true;
}

/* Copy a DN with the base DN of the local partition. */
static struct ldb_dn *ldb_dn_rebase_local(void *mem_ctx, const struct ldb_map_context *data, struct ldb_dn *dn)
{
	struct ldb_dn *new_dn;

	new_dn = ldb_dn_copy(mem_ctx, dn);
	if ( ! ldb_dn_validate(new_dn)) {
		talloc_free(new_dn);
		return NULL;
	}

	/* may be we don't need to rebase at all */
	if ( ! data->remote_base_dn || ! data->local_base_dn) {
		return new_dn;
	}

	if ( ! ldb_dn_remove_base_components(new_dn, ldb_dn_get_comp_num(data->remote_base_dn))) {
		talloc_free(new_dn);
		return NULL;
	}

	if ( ! ldb_dn_add_base(new_dn, data->local_base_dn)) {
		talloc_free(new_dn);
		return NULL;
	}

	return new_dn;
}

/* Copy a DN with the base DN of the remote partition. */
static struct ldb_dn *ldb_dn_rebase_remote(void *mem_ctx, const struct ldb_map_context *data, struct ldb_dn *dn)
{
	struct ldb_dn *new_dn;

	new_dn = ldb_dn_copy(mem_ctx, dn);
	if ( ! ldb_dn_validate(new_dn)) {
		talloc_free(new_dn);
		return NULL;
	}

	/* may be we don't need to rebase at all */
	if ( ! data->remote_base_dn || ! data->local_base_dn) {
		return new_dn;
	}

	if ( ! ldb_dn_remove_base_components(new_dn, ldb_dn_get_comp_num(data->local_base_dn))) {
		talloc_free(new_dn);
		return NULL;
	}

	if ( ! ldb_dn_add_base(new_dn, data->remote_base_dn)) {
		talloc_free(new_dn);
		return NULL;
	}

	return new_dn;
}

/* Run a request and make sure it targets the remote partition. */
/* TODO: free old DNs and messages? */
int ldb_next_remote_request(struct ldb_module *module, struct ldb_request *request)
{
	const struct ldb_map_context *data = map_get_context(module);
	struct ldb_message *msg;

	switch (request->operation) {
	case LDB_SEARCH:
		if (request->op.search.base) {
			request->op.search.base = ldb_dn_rebase_remote(request, data, request->op.search.base);
		} else {
			request->op.search.base = data->remote_base_dn;
			/* TODO: adjust scope? */
		}
		break;

	case LDB_ADD:
		msg = ldb_msg_copy_shallow(request, request->op.add.message);
		msg->dn = ldb_dn_rebase_remote(msg, data, msg->dn);
		request->op.add.message = msg;
		break;

	case LDB_MODIFY:
		msg = ldb_msg_copy_shallow(request, request->op.mod.message);
		msg->dn = ldb_dn_rebase_remote(msg, data, msg->dn);
		request->op.mod.message = msg;
		break;

	case LDB_DELETE:
		request->op.del.dn = ldb_dn_rebase_remote(request, data, request->op.del.dn);
		break;

	case LDB_RENAME:
		request->op.rename.olddn = ldb_dn_rebase_remote(request, data, request->op.rename.olddn);
		request->op.rename.newdn = ldb_dn_rebase_remote(request, data, request->op.rename.newdn);
		break;

	default:
		ldb_debug(module->ldb, LDB_DEBUG_ERROR, "ldb_map: "
			  "Invalid remote request!\n");
		return LDB_ERR_OPERATIONS_ERROR;
	}

	return ldb_next_request(module, request);
}


/* Finding mappings for attributes and objectClasses
 * ================================================= */

/* Find an objectClass mapping by the local name. */
static const struct ldb_map_objectclass *map_objectclass_find_local(const struct ldb_map_context *data, const char *name)
{
	int i;

	for (i = 0; data->objectclass_maps && data->objectclass_maps[i].local_name; i++) {
		if (ldb_attr_cmp(data->objectclass_maps[i].local_name, name) == 0) {
			return &data->objectclass_maps[i];
		}
	}

	return NULL;
}

/* Find an objectClass mapping by the remote name. */
static const struct ldb_map_objectclass *map_objectclass_find_remote(const struct ldb_map_context *data, const char *name)
{
	int i;

	for (i = 0; data->objectclass_maps && data->objectclass_maps[i].remote_name; i++) {
		if (ldb_attr_cmp(data->objectclass_maps[i].remote_name, name) == 0) {
			return &data->objectclass_maps[i];
		}
	}

	return NULL;
}

/* Find an attribute mapping by the local name. */
const struct ldb_map_attribute *map_attr_find_local(const struct ldb_map_context *data, const char *name)
{
	int i;

	for (i = 0; data->attribute_maps[i].local_name; i++) {
		if (ldb_attr_cmp(data->attribute_maps[i].local_name, name) == 0) {
			return &data->attribute_maps[i];
		}
	}
	for (i = 0; data->attribute_maps[i].local_name; i++) {
		if (ldb_attr_cmp(data->attribute_maps[i].local_name, "*") == 0) {
			return &data->attribute_maps[i];
		}
	}

	return NULL;
}

/* Find an attribute mapping by the remote name. */
const struct ldb_map_attribute *map_attr_find_remote(const struct ldb_map_context *data, const char *name)
{
	const struct ldb_map_attribute *map;
	const struct ldb_map_attribute *wildcard = NULL;
	int i, j;

	for (i = 0; data->attribute_maps[i].local_name; i++) {
		map = &data->attribute_maps[i];
		if (ldb_attr_cmp(map->local_name, "*") == 0) {
			wildcard = &data->attribute_maps[i];
		}

		switch (map->type) {
		case MAP_IGNORE:
			break;

		case MAP_KEEP:
			if (ldb_attr_cmp(map->local_name, name) == 0) {
				return map;
			}
			break;

		case MAP_RENAME:
		case MAP_CONVERT:
			if (ldb_attr_cmp(map->u.rename.remote_name, name) == 0) {
				return map;
			}
			break;

		case MAP_GENERATE:
			for (j = 0; map->u.generate.remote_names && map->u.generate.remote_names[j]; j++) {
				if (ldb_attr_cmp(map->u.generate.remote_names[j], name) == 0) {
					return map;
				}
			}
			break;
		}
	}

	/* We didn't find it, so return the wildcard record if one was configured */
	return wildcard;
}


/* Mapping attributes
 * ================== */

/* Check whether an attribute will be mapped into the remote partition. */
bool map_attr_check_remote(const struct ldb_map_context *data, const char *attr)
{
	const struct ldb_map_attribute *map = map_attr_find_local(data, attr);

	if (map == NULL) {
		return false;
	}
	if (map->type == MAP_IGNORE) {
		return false;
	}

	return true;
}

/* Map an attribute name into the remote partition. */
const char *map_attr_map_local(void *mem_ctx, const struct ldb_map_attribute *map, const char *attr)
{
	if (map == NULL) {
		return talloc_strdup(mem_ctx, attr);
	}

	switch (map->type) {
	case MAP_KEEP:
		return talloc_strdup(mem_ctx, attr);

	case MAP_RENAME:
	case MAP_CONVERT:
		return talloc_strdup(mem_ctx, map->u.rename.remote_name);

	default:
		return NULL;
	}
}

/* Map an attribute name back into the local partition. */
const char *map_attr_map_remote(void *mem_ctx, const struct ldb_map_attribute *map, const char *attr)
{
	if (map == NULL) {
		return talloc_strdup(mem_ctx, attr);
	}

	if (map->type == MAP_KEEP) {
		return talloc_strdup(mem_ctx, attr);
	}

	return talloc_strdup(mem_ctx, map->local_name);
}


/* Merge two lists of attributes into a single one. */
int map_attrs_merge(struct ldb_module *module, void *mem_ctx, 
		    const char ***attrs, const char * const *more_attrs)
{
	int i, j, k;

	for (i = 0; *attrs && (*attrs)[i]; i++) /* noop */ ;
	for (j = 0; more_attrs && more_attrs[j]; j++) /* noop */ ;
	
	*attrs = talloc_realloc(mem_ctx, *attrs, const char *, i+j+1);
	if (*attrs == NULL) {
		map_oom(module);
		return -1;
	}

	for (k = 0; k < j; k++) {
		(*attrs)[i + k] = more_attrs[k];
	}

	(*attrs)[i+k] = NULL;

	return 0;
}

/* Mapping ldb values
 * ================== */

/* Map an ldb value into the remote partition. */
struct ldb_val ldb_val_map_local(struct ldb_module *module, void *mem_ctx, 
				 const struct ldb_map_attribute *map, const struct ldb_val *val)
{
	if (map && (map->type == MAP_CONVERT) && (map->u.convert.convert_local)) {
		return map->u.convert.convert_local(module, mem_ctx, val);
	}

	return ldb_val_dup(mem_ctx, val);
}

/* Map an ldb value back into the local partition. */
struct ldb_val ldb_val_map_remote(struct ldb_module *module, void *mem_ctx, 
				  const struct ldb_map_attribute *map, const struct ldb_val *val)
{
	if (map && (map->type == MAP_CONVERT) && (map->u.convert.convert_remote)) {
		return map->u.convert.convert_remote(module, mem_ctx, val);
	}

	return ldb_val_dup(mem_ctx, val);
}


/* Mapping DNs
 * =========== */

/* Check whether a DN is below the local baseDN. */
bool ldb_dn_check_local(struct ldb_module *module, struct ldb_dn *dn)
{
	const struct ldb_map_context *data = map_get_context(module);

	if (!data->local_base_dn) {
		return true;
	}

	return ldb_dn_compare_base(data->local_base_dn, dn) == 0;
}

/* Map a DN into the remote partition. */
struct ldb_dn *ldb_dn_map_local(struct ldb_module *module, void *mem_ctx, struct ldb_dn *dn)
{
	const struct ldb_map_context *data = map_get_context(module);
	struct ldb_dn *newdn;
	const struct ldb_map_attribute *map;
	enum ldb_map_attr_type map_type;
	const char *name;
	struct ldb_val value;
	int i, ret;

	if (dn == NULL) {
		return NULL;
	}

	newdn = ldb_dn_copy(mem_ctx, dn);
	if (newdn == NULL) {
		map_oom(module);
		return NULL;
	}

	/* For each RDN, map the component name and possibly the value */
	for (i = 0; i < ldb_dn_get_comp_num(newdn); i++) {
		map = map_attr_find_local(data, ldb_dn_get_component_name(dn, i));

		/* Unknown attribute - leave this RDN as is and hope the best... */
		if (map == NULL) {
			map_type = MAP_KEEP;
		} else {
			map_type = map->type;
		}

		switch (map_type) {
		case MAP_IGNORE:
		case MAP_GENERATE:
			ldb_debug(module->ldb, LDB_DEBUG_ERROR, "ldb_map: "
				  "MAP_IGNORE/MAP_GENERATE attribute '%s' "
				  "used in DN!\n", ldb_dn_get_component_name(dn, i));
			goto failed;

		case MAP_CONVERT:
			if (map->u.convert.convert_local == NULL) {
				ldb_debug(module->ldb, LDB_DEBUG_ERROR, "ldb_map: "
					  "'convert_local' not set for attribute '%s' "
					  "used in DN!\n", ldb_dn_get_component_name(dn, i));
				goto failed;
			}
			/* fall through */
		case MAP_KEEP:
		case MAP_RENAME:
			name = map_attr_map_local(newdn, map, ldb_dn_get_component_name(dn, i));
			if (name == NULL) goto failed;

			value = ldb_val_map_local(module, newdn, map, ldb_dn_get_component_val(dn, i));
			if (value.data == NULL) goto failed;

			ret = ldb_dn_set_component(newdn, i, name, value);
			if (ret != LDB_SUCCESS) {
				goto failed;
			}

			break;
		}
	}

	return newdn;

failed:
	talloc_free(newdn);
	return NULL;
}

/* Map a DN into the local partition. */
struct ldb_dn *ldb_dn_map_remote(struct ldb_module *module, void *mem_ctx, struct ldb_dn *dn)
{
	const struct ldb_map_context *data = map_get_context(module);
	struct ldb_dn *newdn;
	const struct ldb_map_attribute *map;
	enum ldb_map_attr_type map_type;
	const char *name;
	struct ldb_val value;
	int i, ret;

	if (dn == NULL) {
		return NULL;
	}

	newdn = ldb_dn_copy(mem_ctx, dn);
	if (newdn == NULL) {
		map_oom(module);
		return NULL;
	}

	/* For each RDN, map the component name and possibly the value */
	for (i = 0; i < ldb_dn_get_comp_num(newdn); i++) {
		map = map_attr_find_remote(data, ldb_dn_get_component_name(dn, i));

		/* Unknown attribute - leave this RDN as is and hope the best... */
		if (map == NULL) {
			map_type = MAP_KEEP;
		} else {
			map_type = map->type;
		}

		switch (map_type) {
		case MAP_IGNORE:
		case MAP_GENERATE:
			ldb_debug(module->ldb, LDB_DEBUG_ERROR, "ldb_map: "
				  "MAP_IGNORE/MAP_GENERATE attribute '%s' "
				  "used in DN!\n", ldb_dn_get_component_name(dn, i));
			goto failed;

		case MAP_CONVERT:
			if (map->u.convert.convert_remote == NULL) {
				ldb_debug(module->ldb, LDB_DEBUG_ERROR, "ldb_map: "
					  "'convert_remote' not set for attribute '%s' "
					  "used in DN!\n", ldb_dn_get_component_name(dn, i));
				goto failed;
			}
			/* fall through */
		case MAP_KEEP:
		case MAP_RENAME:
			name = map_attr_map_remote(newdn, map, ldb_dn_get_component_name(dn, i));
			if (name == NULL) goto failed;

			value = ldb_val_map_remote(module, newdn, map, ldb_dn_get_component_val(dn, i));
			if (value.data == NULL) goto failed;

			ret = ldb_dn_set_component(newdn, i, name, value);
			if (ret != LDB_SUCCESS) {
				goto failed;
			}

			break;
		}
	}

	return newdn;

failed:
	talloc_free(newdn);
	return NULL;
}

/* Map a DN and its base into the local partition. */
/* TODO: This should not be required with GUIDs. */
struct ldb_dn *ldb_dn_map_rebase_remote(struct ldb_module *module, void *mem_ctx, struct ldb_dn *dn)
{
	const struct ldb_map_context *data = map_get_context(module);
	struct ldb_dn *dn1, *dn2;

	dn1 = ldb_dn_rebase_local(mem_ctx, data, dn);
	dn2 = ldb_dn_map_remote(module, mem_ctx, dn1);

	talloc_free(dn1);
	return dn2;
}


/* Converting DNs and objectClasses (as ldb values)
 * ================================================ */

/* Map a DN contained in an ldb value into the remote partition. */
static struct ldb_val ldb_dn_convert_local(struct ldb_module *module, void *mem_ctx, const struct ldb_val *val)
{
	struct ldb_dn *dn, *newdn;
	struct ldb_val newval;

	dn = ldb_dn_from_ldb_val(mem_ctx, module->ldb, val);
	if (! ldb_dn_validate(dn)) {
		newval.length = 0;
		newval.data = NULL;
		talloc_free(dn);
		return newval;
	}
	newdn = ldb_dn_map_local(module, mem_ctx, dn);
	talloc_free(dn);

	newval.length = 0;
	newval.data = (uint8_t *)ldb_dn_alloc_linearized(mem_ctx, newdn);
	if (newval.data) {
		newval.length = strlen((char *)newval.data);
	}
	talloc_free(newdn);

	return newval;
}

/* Map a DN contained in an ldb value into the local partition. */
static struct ldb_val ldb_dn_convert_remote(struct ldb_module *module, void *mem_ctx, const struct ldb_val *val)
{
	struct ldb_dn *dn, *newdn;
	struct ldb_val newval;

	dn = ldb_dn_from_ldb_val(mem_ctx, module->ldb, val);
	if (! ldb_dn_validate(dn)) {
		newval.length = 0;
		newval.data = NULL;
		talloc_free(dn);
		return newval;
	}
	newdn = ldb_dn_map_remote(module, mem_ctx, dn);
	talloc_free(dn);

	newval.length = 0;
	newval.data = (uint8_t *)ldb_dn_alloc_linearized(mem_ctx, newdn);
	if (newval.data) {
		newval.length = strlen((char *)newval.data);
	}
	talloc_free(newdn);

	return newval;
}

/* Map an objectClass into the remote partition. */
static struct ldb_val map_objectclass_convert_local(struct ldb_module *module, void *mem_ctx, const struct ldb_val *val)
{
	const struct ldb_map_context *data = map_get_context(module);
	const char *name = (char *)val->data;
	const struct ldb_map_objectclass *map = map_objectclass_find_local(data, name);
	struct ldb_val newval;

	if (map) {
		newval.data = (uint8_t*)talloc_strdup(mem_ctx, map->remote_name);
		newval.length = strlen((char *)newval.data);
		return newval;
	}

	return ldb_val_dup(mem_ctx, val);
}

/* Generate a remote message with a mapped objectClass. */
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)
{
	const struct ldb_map_context *data = map_get_context(module);
	struct ldb_message_element *el, *oc;
	struct ldb_val val;
	bool found_extensibleObject = false;
	int i;

	/* Find old local objectClass */
	oc = ldb_msg_find_element(old, "objectClass");
	if (oc == NULL) {
		return;
	}

	/* Prepare new element */
	el = talloc_zero(remote, struct ldb_message_element);
	if (el == NULL) {
		ldb_oom(module->ldb);
		return;			/* TODO: fail? */
	}

	/* Copy local objectClass element, reverse space for an extra value */
	el->num_values = oc->num_values + 1;
	el->values = talloc_array(el, struct ldb_val, el->num_values);
	if (el->values == NULL) {
		talloc_free(el);
		ldb_oom(module->ldb);
		return;			/* TODO: fail? */
	}

	/* Copy local element name "objectClass" */
	el->name = talloc_strdup(el, local_attr);

	/* Convert all local objectClasses */
	for (i = 0; i < el->num_values - 1; i++) {
		el->values[i] = map_objectclass_convert_local(module, el->values, &oc->values[i]);
		if (ldb_attr_cmp((char *)el->values[i].data, data->add_objectclass) == 0) {
			found_extensibleObject = true;
		}
	}

	if (!found_extensibleObject) {
		val.data = (uint8_t *)talloc_strdup(el->values, data->add_objectclass);
		val.length = strlen((char *)val.data);

		/* Append additional objectClass data->add_objectclass */
		el->values[i] = val;
	} else {
		el->num_values--;
	}

	/* Add new objectClass to remote message */
	ldb_msg_add(remote, el, 0);
}

/* Map an objectClass into the local partition. */
static struct ldb_val map_objectclass_convert_remote(struct ldb_module *module, void *mem_ctx, const struct ldb_val *val)
{
	const struct ldb_map_context *data = map_get_context(module);
	const char *name = (char *)val->data;
	const struct ldb_map_objectclass *map = map_objectclass_find_remote(data, name);
	struct ldb_val newval;

	if (map) {
		newval.data = (uint8_t*)talloc_strdup(mem_ctx, map->local_name);
		newval.length = strlen((char *)newval.data);
		return newval;
	}

	return ldb_val_dup(mem_ctx, val);
}

/* Generate a local message with a mapped objectClass. */
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)
{
	const struct ldb_map_context *data = map_get_context(module);
	struct ldb_message_element *el, *oc;
	struct ldb_val val;
	int i;

	/* Find old remote objectClass */
	oc = ldb_msg_find_element(remote, "objectClass");
	if (oc == NULL) {
		return NULL;
	}

	/* Prepare new element */
	el = talloc_zero(mem_ctx, struct ldb_message_element);
	if (el == NULL) {
		ldb_oom(module->ldb);
		return NULL;
	}

	/* Copy remote objectClass element */
	el->num_values = oc->num_values;
	el->values = talloc_array(el, struct ldb_val, el->num_values);
	if (el->values == NULL) {
		talloc_free(el);
		ldb_oom(module->ldb);
		return NULL;
	}

	/* Copy remote element name "objectClass" */
	el->name = talloc_strdup(el, local_attr);

	/* Convert all remote objectClasses */
	for (i = 0; i < el->num_values; i++) {
		el->values[i] = map_objectclass_convert_remote(module, el->values, &oc->values[i]);
	}

	val.data = (uint8_t *)talloc_strdup(el->values, data->add_objectclass);
	val.length = strlen((char *)val.data);

	/* Remove last value if it was the string in data->add_objectclass (eg samba4top, extensibleObject) */
	if (ldb_val_equal_exact(&val, &el->values[i-1])) {
		el->num_values--;
		el->values = talloc_realloc(el, el->values, struct ldb_val, el->num_values);
		if (el->values == NULL) {
			talloc_free(el);
			ldb_oom(module->ldb);
			return NULL;
		}
	}

	return el;
}

static const struct ldb_map_attribute objectclass_convert_map = {
	.local_name = "objectClass",
	.type = MAP_CONVERT,
	.u = {
		.convert = {
			.remote_name = "objectClass",
			.convert_local = map_objectclass_convert_local,
			.convert_remote = map_objectclass_convert_remote,
		},
	},
};


/* Mappings for searches on objectClass= assuming a one-to-one
 * mapping.  Needed because this is a generate operator for the
 * add/modify code */
static int map_objectclass_convert_operator(struct ldb_module *module, void *mem_ctx, 
					    struct ldb_parse_tree **new, const struct ldb_parse_tree *tree) 
{
	
	return map_subtree_collect_remote_simple(module, mem_ctx, new, tree, &objectclass_convert_map);
}

/* Auxiliary request construction
 * ============================== */

/* Build a request to search a record by its DN. */
struct ldb_request *map_search_base_req(struct map_context *ac, struct ldb_dn *dn, const char * const *attrs, const struct ldb_parse_tree *tree, void *context, ldb_map_callback_t callback)
{
	const struct ldb_parse_tree *search_tree;
	struct ldb_request *req;
	int ret;

	if (tree) {
		search_tree = tree;
	} else {
		search_tree = ldb_parse_tree(ac, NULL);
		if (search_tree == NULL) {
			return NULL;
		}
	}

	ret = ldb_build_search_req_ex(&req, ac->module->ldb, ac,
					dn, LDB_SCOPE_BASE,
					search_tree, attrs,
					NULL,
					context, callback,
					ac->req);
	if (ret != LDB_SUCCESS) {
		return NULL;
	}

	return req;
}

/* Build a request to update the 'IS_MAPPED' attribute */
struct ldb_request *map_build_fixup_req(struct map_context *ac,
					struct ldb_dn *olddn,
					struct ldb_dn *newdn,
					void *context,
					ldb_map_callback_t callback)
{
	struct ldb_request *req;
	struct ldb_message *msg;
	const char *dn;
	int ret;

	/* Prepare message */
	msg = ldb_msg_new(ac);
	if (msg == NULL) {
		map_oom(ac->module);
		return NULL;
	}

	/* Update local 'IS_MAPPED' to the new remote DN */
	msg->dn = ldb_dn_copy(msg, olddn);
	dn = ldb_dn_alloc_linearized(msg, newdn);
	if ( ! dn || ! ldb_dn_validate(msg->dn)) {
		goto failed;
	}
	if (ldb_msg_add_empty(msg, IS_MAPPED, LDB_FLAG_MOD_REPLACE, NULL) != 0) {
		goto failed;
	}
	if (ldb_msg_add_string(msg, IS_MAPPED, dn) != 0) {
		goto failed;
	}

	/* Prepare request */
	ret = ldb_build_mod_req(&req, ac->module->ldb,
				ac, msg, NULL,
				context, callback,
				ac->req);
	if (ret != LDB_SUCCESS) {
		goto failed;
	}
	talloc_steal(req, msg);

	return req;
failed:
	talloc_free(msg);
	return NULL;
}

/* Module initialization
 * ===================== */


/* Builtin mappings for DNs and objectClasses */
static const struct ldb_map_attribute builtin_attribute_maps[] = {
	{
		.local_name = "dn",
		.type = MAP_CONVERT,
		.u = {
			.convert = {
				 .remote_name = "dn",
				 .convert_local = ldb_dn_convert_local,
				 .convert_remote = ldb_dn_convert_remote,
			 },
		},
	},
	{
		.local_name = NULL,
	}
};

static const struct ldb_map_attribute objectclass_attribute_map	= {
	.local_name = "objectClass",
	.type = MAP_GENERATE,
	.convert_operator = map_objectclass_convert_operator,
	.u = {
		.generate = {
			.remote_names = { "objectClass", NULL },
			.generate_local = map_objectclass_generate_local,
			.generate_remote = map_objectclass_generate_remote,
		},
	},
};


/* Find the special 'MAP_DN_NAME' record and store local and remote
 * base DNs in private data. */
static int map_init_dns(struct ldb_module *module, struct ldb_map_context *data, const char *name)
{
	static const char * const attrs[] = { MAP_DN_FROM, MAP_DN_TO, NULL };
	struct ldb_dn *dn;
	struct ldb_message *msg;
	struct ldb_result *res;
	int ret;

	if (!name) {
		data->local_base_dn = NULL;
		data->remote_base_dn = NULL;
		return LDB_SUCCESS;
	}

	dn = ldb_dn_new_fmt(data, module->ldb, "%s=%s", MAP_DN_NAME, name);
	if ( ! ldb_dn_validate(dn)) {
		ldb_debug(module->ldb, LDB_DEBUG_ERROR, "ldb_map: "
			  "Failed to construct '%s' DN!\n", MAP_DN_NAME);
		return LDB_ERR_OPERATIONS_ERROR;
	}

	ret = ldb_search(module->ldb, data, &res, dn, LDB_SCOPE_BASE, attrs, NULL);
	talloc_free(dn);
	if (ret != LDB_SUCCESS) {
		return ret;
	}
	if (res->count == 0) {
		ldb_debug(module->ldb, LDB_DEBUG_ERROR, "ldb_map: "
			  "No results for '%s=%s'!\n", MAP_DN_NAME, name);
		talloc_free(res);
		return LDB_ERR_CONSTRAINT_VIOLATION;
	}
	if (res->count > 1) {
		ldb_debug(module->ldb, LDB_DEBUG_ERROR, "ldb_map: "
			  "Too many results for '%s=%s'!\n", MAP_DN_NAME, name);
		talloc_free(res);
		return LDB_ERR_CONSTRAINT_VIOLATION;
	}

	msg = res->msgs[0];
	data->local_base_dn = ldb_msg_find_attr_as_dn(module->ldb, data, msg, MAP_DN_FROM);
	data->remote_base_dn = ldb_msg_find_attr_as_dn(module->ldb, data, msg, MAP_DN_TO);
	talloc_free(res);

	return LDB_SUCCESS;
}

/* Store attribute maps and objectClass maps in private data. */
static int map_init_maps(struct ldb_module *module, struct ldb_map_context *data, 
			 const struct ldb_map_attribute *attrs, 
			 const struct ldb_map_objectclass *ocls, 
			 const char * const *wildcard_attributes)
{
	int i, j, last;
	last = 0;

	/* Count specified attribute maps */
	for (i = 0; attrs[i].local_name; i++) /* noop */ ;
	/* Count built-in attribute maps */
	for (j = 0; builtin_attribute_maps[j].local_name; j++) /* noop */ ;

	/* Store list of attribute maps */
	data->attribute_maps = talloc_array(data, struct ldb_map_attribute, i+j+2);
	if (data->attribute_maps == NULL) {
		map_oom(module);
		return LDB_ERR_OPERATIONS_ERROR;
	}

	/* Specified ones go first */
	for (i = 0; attrs[i].local_name; i++) {
		data->attribute_maps[last] = attrs[i];
		last++;
	}

	/* Built-in ones go last */
	for (i = 0; builtin_attribute_maps[i].local_name; i++) {
		data->attribute_maps[last] = builtin_attribute_maps[i];
		last++;
	}

	if (data->add_objectclass) {
		/* ObjectClass one is very last, if required */
		data->attribute_maps[last] = objectclass_attribute_map;
		last++;
	} else if (ocls) {
		data->attribute_maps[last] = objectclass_convert_map;
		last++;
	}

	/* Ensure 'local_name == NULL' for the last entry */
	memset(&data->attribute_maps[last], 0, sizeof(struct ldb_map_attribute));

	/* Store list of objectClass maps */
	data->objectclass_maps = ocls;

	data->wildcard_attributes = wildcard_attributes;

	return LDB_SUCCESS;
}

/* Initialize global private data. */
_PUBLIC_ int ldb_map_init(struct ldb_module *module, const struct ldb_map_attribute *attrs, 
			  const struct ldb_map_objectclass *ocls,
			  const char * const *wildcard_attributes,
			  const char *add_objectclass,
			  const char *name)
{
	struct map_private *data;
	int ret;

	/* Prepare private data */
	data = talloc_zero(module, struct map_private);
	if (data == NULL) {
		map_oom(module);
		return LDB_ERR_OPERATIONS_ERROR;
	}

	module->private_data = data;

	data->context = talloc_zero(data, struct ldb_map_context);
	if (!data->context) {
		map_oom(module);
		return LDB_ERR_OPERATIONS_ERROR;		
	}

	/* Store local and remote baseDNs */
	ret = map_init_dns(module, data->context, name);
	if (ret != LDB_SUCCESS) {
		talloc_free(data);
		return ret;
	}

	data->context->add_objectclass = add_objectclass;

	/* Store list of attribute and objectClass maps */
	ret = map_init_maps(module, data->context, attrs, ocls, wildcard_attributes);
	if (ret != LDB_SUCCESS) {
		talloc_free(data);
		return ret;
	}

	return LDB_SUCCESS;
}