summaryrefslogtreecommitdiffstats
path: root/ldb/ldb_map/ldb_map_outbound.c
blob: 5588eaaf46edbee36cb1b5e9d95de7869c4ced42 (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
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
/*
   ldb database mapping module

   Copyright (C) Jelmer Vernooij 2005
   Copyright (C) Martin Kuehl <mkhl@samba.org> 2006
   Copyright (C) Andrew Bartlett <abartlet@samba.org> 2006
   Copyright (C) Simo Sorce <idra@samba.org> 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/>.

*/

#include "ldb_includes.h"

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


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

/* Select attributes that stay in the local partition. */
static const char **map_attrs_select_local(struct ldb_module *module, void *mem_ctx, const char * const *attrs)
{
	const struct ldb_map_context *data = map_get_context(module);
	const char **result;
	int i, last;

	if (attrs == NULL)
		return NULL;

	last = 0;
	result = talloc_array(mem_ctx, const char *, 1);
	if (result == NULL) {
		goto failed;
	}
	result[0] = NULL;

	for (i = 0; attrs[i]; i++) {
		/* Wildcards and ignored attributes are kept locally */
		if ((ldb_attr_cmp(attrs[i], "*") == 0) ||
		    (!map_attr_check_remote(data, attrs[i]))) {
			result = talloc_realloc(mem_ctx, result, const char *, last+2);
			if (result == NULL) {
				goto failed;
			}

			result[last] = talloc_strdup(result, attrs[i]);
			result[last+1] = NULL;
			last++;
		}
	}

	return result;

failed:
	talloc_free(result);
	map_oom(module);
	return NULL;
}

/* Collect attributes that are mapped into the remote partition. */
static const char **map_attrs_collect_remote(struct ldb_module *module, void *mem_ctx, 
					     const char * const *attrs)
{
	const struct ldb_map_context *data = map_get_context(module);
	const char **result;
	const struct ldb_map_attribute *map;
	const char *name=NULL;
	int i, j, last;
	int ret;

	last = 0;
	result = talloc_array(mem_ctx, const char *, 1);
	if (result == NULL) {
		goto failed;
	}
	result[0] = NULL;

	for (i = 0; attrs[i]; i++) {
		/* Wildcards are kept remotely, too */
		if (ldb_attr_cmp(attrs[i], "*") == 0) {
			const char **new_attrs = NULL;
			ret = map_attrs_merge(module, mem_ctx, &new_attrs, attrs);
			if (ret != LDB_SUCCESS) {
				goto failed;
			}
			ret = map_attrs_merge(module, mem_ctx, &new_attrs, data->wildcard_attributes);
			if (ret != LDB_SUCCESS) {
				goto failed;
			}

			attrs = new_attrs;
			break;
		}
	}

	for (i = 0; attrs[i]; i++) {
		/* Wildcards are kept remotely, too */
		if (ldb_attr_cmp(attrs[i], "*") == 0) {
			/* Add all 'include in wildcard' attributes */
			name = attrs[i];
			goto named;
		}

		/* Add remote names of mapped attrs */
		map = map_attr_find_local(data, attrs[i]);
		if (map == NULL) {
			continue;
		}

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

		case MAP_KEEP:
			name = attrs[i];
			goto named;

		case MAP_RENAME:
		case MAP_CONVERT:
			name = map->u.rename.remote_name;
			goto named;

		case MAP_GENERATE:
			/* Add all remote names of "generate" attrs */
			for (j = 0; map->u.generate.remote_names[j]; j++) {
				result = talloc_realloc(mem_ctx, result, const char *, last+2);
				if (result == NULL) {
					goto failed;
				}

				result[last] = talloc_strdup(result, map->u.generate.remote_names[j]);
				result[last+1] = NULL;
				last++;
			}
			continue;
		}

	named:	/* We found a single remote name, add that */
		result = talloc_realloc(mem_ctx, result, const char *, last+2);
		if (result == NULL) {
			goto failed;
		}

		result[last] = talloc_strdup(result, name);
		result[last+1] = NULL;
		last++;
	}

	return result;

failed:
	talloc_free(result);
	map_oom(module);
	return NULL;
}

/* Split attributes that stay in the local partition from those that
 * are mapped into the remote partition. */
static int map_attrs_partition(struct ldb_module *module, void *mem_ctx, const char ***local_attrs, const char ***remote_attrs, const char * const *attrs)
{
	*local_attrs = map_attrs_select_local(module, mem_ctx, attrs);
	*remote_attrs = map_attrs_collect_remote(module, mem_ctx, attrs);

	return 0;
}

/* Mapping message elements
 * ======================== */

/* Add an element to a message, overwriting any old identically named elements. */
static int ldb_msg_replace(struct ldb_message *msg, const struct ldb_message_element *el)
{
	struct ldb_message_element *old;

	old = ldb_msg_find_element(msg, el->name);

	/* no local result, add as new element */
	if (old == NULL) {
		if (ldb_msg_add_empty(msg, el->name, 0, &old) != 0) {
			return -1;
		}
		talloc_free(discard_const_p(char, old->name));
	}

	/* copy new element */
	*old = *el;

	/* and make sure we reference the contents */
	if (!talloc_reference(msg->elements, el->name)) {
		return -1;
	}
	if (!talloc_reference(msg->elements, el->values)) {
		return -1;
	}

	return 0;
}

/* Map a message element back into the local partition. */
static struct ldb_message_element *ldb_msg_el_map_remote(struct ldb_module *module, 
							 void *mem_ctx, 
							 const struct ldb_map_attribute *map, 
							 const char *attr_name,
							 const struct ldb_message_element *old)
{
	struct ldb_message_element *el;
	int i;

	el = talloc_zero(mem_ctx, struct ldb_message_element);
	if (el == NULL) {
		map_oom(module);
		return NULL;
	}

	el->values = talloc_array(el, struct ldb_val, old->num_values);
	if (el->values == NULL) {
		talloc_free(el);
		map_oom(module);
		return NULL;
	}

	el->name = talloc_strdup(el, attr_name);
	if (el->name == NULL) {
		talloc_free(el);
		map_oom(module);
		return NULL;
	}

	for (i = 0; i < old->num_values; i++) {
		el->values[i] = ldb_val_map_remote(module, el->values, map, &old->values[i]);
		/* Conversions might fail, in which case bail */
		if (!el->values[i].data) {
			talloc_free(el);
			return NULL;
		}
		el->num_values++;
	}

	return el;
}

/* Merge a remote message element into a local message. */
static int ldb_msg_el_merge(struct ldb_module *module, struct ldb_message *local, 
			    struct ldb_message *remote, const char *attr_name)
{
	const struct ldb_map_context *data = map_get_context(module);
	const struct ldb_map_attribute *map;
	struct ldb_message_element *old, *el=NULL;
	const char *remote_name = NULL;

	/* We handle wildcards in ldb_msg_el_merge_wildcard */
	if (ldb_attr_cmp(attr_name, "*") == 0) {
		return LDB_SUCCESS;
	}

	map = map_attr_find_local(data, attr_name);

	/* Unknown attribute in remote message:
	 * skip, attribute was probably auto-generated */
	if (map == NULL) {
		return LDB_SUCCESS;
	}

	switch (map->type) {
	case MAP_IGNORE:
		break;
	case MAP_CONVERT:
		remote_name = map->u.convert.remote_name;
		break;
	case MAP_KEEP:
		remote_name = attr_name;
		break;
	case MAP_RENAME:
		remote_name = map->u.rename.remote_name;
		break;
	case MAP_GENERATE:
		break;
	}

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

	case MAP_CONVERT:
		if (map->u.convert.convert_remote == NULL) {
			ldb_debug(module->ldb, LDB_DEBUG_ERROR, "ldb_map: "
				  "Skipping attribute '%s': "
				  "'convert_remote' not set\n",
				  attr_name);
			return LDB_SUCCESS;
		}
		/* fall through */
	case MAP_KEEP:
	case MAP_RENAME:
		old = ldb_msg_find_element(remote, remote_name);
		if (old) {
			el = ldb_msg_el_map_remote(module, local, map, attr_name, old);
		} else {
			return LDB_ERR_NO_SUCH_ATTRIBUTE;
		}
		break;

	case MAP_GENERATE:
		if (map->u.generate.generate_local == NULL) {
			ldb_debug(module->ldb, LDB_DEBUG_ERROR, "ldb_map: "
				  "Skipping attribute '%s': "
				  "'generate_local' not set\n",
				  attr_name);
			return LDB_SUCCESS;
		}

		el = map->u.generate.generate_local(module, local, attr_name, remote);
		if (!el) {
			/* Generation failure is probably due to lack of source attributes */
			return LDB_ERR_NO_SUCH_ATTRIBUTE;
		}
		break;
	}

	if (el == NULL) {
		return LDB_ERR_NO_SUCH_ATTRIBUTE;
	}

	return ldb_msg_replace(local, el);
}

/* Handle wildcard parts of merging a remote message element into a local message. */
static int ldb_msg_el_merge_wildcard(struct ldb_module *module, struct ldb_message *local, 
				     struct ldb_message *remote)
{
	const struct ldb_map_context *data = map_get_context(module);
	const struct ldb_map_attribute *map = map_attr_find_local(data, "*");
	struct ldb_message_element *el=NULL;
	int i, ret;

	/* Perhaps we have a mapping for "*" */
	if (map && map->type == MAP_KEEP) {
		/* We copy everything over, and hope that anything with a 
		   more specific rule is overwritten */
		for (i = 0; i < remote->num_elements; i++) {
			el = ldb_msg_el_map_remote(module, local, map, remote->elements[i].name,
						   &remote->elements[i]);
			if (el == NULL) {
				return LDB_ERR_OPERATIONS_ERROR;
			}
			
			ret = ldb_msg_replace(local, el);
			if (ret) {
				return ret;
			}
		}
	}
	
	/* Now walk the list of possible mappings, and apply each */
	for (i = 0; data->attribute_maps[i].local_name; i++) {
		ret = ldb_msg_el_merge(module, local, remote, 
				       data->attribute_maps[i].local_name);
		if (ret == LDB_ERR_NO_SUCH_ATTRIBUTE) {
			continue;
		} else if (ret) {
			return ret;
		} else {
			continue;
		}
	}

	return LDB_SUCCESS;
}

/* Mapping messages
 * ================ */

/* Merge two local messages into a single one. */
static int ldb_msg_merge_local(struct ldb_module *module, struct ldb_message *msg1, struct ldb_message *msg2)
{
	int i, ret;

	for (i = 0; i < msg2->num_elements; i++) {
		ret = ldb_msg_replace(msg1, &msg2->elements[i]);
		if (ret) {
			return ret;
		}
	}

	return LDB_SUCCESS;
}

/* Merge a local and a remote message into a single local one. */
static int ldb_msg_merge_remote(struct map_context *ac, struct ldb_message *local, 
				struct ldb_message *remote)
{
	int i, ret;
	const char * const *attrs = ac->all_attrs;
	if (!attrs) {
		ret = ldb_msg_el_merge_wildcard(ac->module, local, remote);
		if (ret) {
			return ret;
		}
	}

	for (i = 0; attrs && attrs[i]; i++) {
		if (ldb_attr_cmp(attrs[i], "*") == 0) {
			ret = ldb_msg_el_merge_wildcard(ac->module, local, remote);
			if (ret) {
				return ret;
			}
			break;
		}
	}

	/* Try to map each attribute back;
	 * Add to local message is possible,
	 * Overwrite old local attribute if necessary */
	for (i = 0; attrs && attrs[i]; i++) {
		ret = ldb_msg_el_merge(ac->module, local, remote, 
				       attrs[i]);
		if (ret == LDB_ERR_NO_SUCH_ATTRIBUTE) {
		} else if (ret) {
			return ret;
		}
	}

	return LDB_SUCCESS;
}

/* Mapping search results
 * ====================== */

/* Map a search result back into the local partition. */
static int map_reply_remote(struct map_context *ac, struct ldb_reply *ares)
{
	struct ldb_message *msg;
	struct ldb_dn *dn;
	int ret;

	/* There is no result message, skip */
	if (ares->type != LDB_REPLY_ENTRY) {
		return 0;
	}

	/* Create a new result message */
	msg = ldb_msg_new(ares);
	if (msg == NULL) {
		map_oom(ac->module);
		return -1;
	}

	/* Merge remote message into new message */
	ret = ldb_msg_merge_remote(ac, msg, ares->message);
	if (ret) {
		talloc_free(msg);
		return ret;
	}

	/* Create corresponding local DN */
	dn = ldb_dn_map_rebase_remote(ac->module, msg, ares->message->dn);
	if (dn == NULL) {
		talloc_free(msg);
		return -1;
	}
	msg->dn = dn;

	/* Store new message with new DN as the result */
	talloc_free(ares->message);
	ares->message = msg;

	return 0;
}

/* Mapping parse trees
 * =================== */

/* Check whether a parse tree can safely be split in two. */
static bool ldb_parse_tree_check_splittable(const struct ldb_parse_tree *tree)
{
	const struct ldb_parse_tree *subtree = tree;
	bool negate = false;

	while (subtree) {
		switch (subtree->operation) {
		case LDB_OP_NOT:
			negate = !negate;
			subtree = subtree->u.isnot.child;
			continue;

		case LDB_OP_AND:
			return !negate;	/* if negate: False */

		case LDB_OP_OR:
			return negate;	/* if negate: True */

		default:
			return true;	/* simple parse tree */
		}
	}

	return true;			/* no parse tree */
}

/* Collect a list of attributes required to match a given parse tree. */
static int ldb_parse_tree_collect_attrs(struct ldb_module *module, void *mem_ctx, const char ***attrs, const struct ldb_parse_tree *tree)
{
	const char **new_attrs;
	int i, ret;

	if (tree == NULL) {
		return 0;
	}

	switch (tree->operation) {
	case LDB_OP_OR:
	case LDB_OP_AND:		/* attributes stored in list of subtrees */
		for (i = 0; i < tree->u.list.num_elements; i++) {
			ret = ldb_parse_tree_collect_attrs(module, mem_ctx, 
							   attrs, tree->u.list.elements[i]);
			if (ret) {
				return ret;
			}
		}
		return 0;

	case LDB_OP_NOT:		/* attributes stored in single subtree */
		return ldb_parse_tree_collect_attrs(module, mem_ctx, attrs, tree->u.isnot.child);

	default:			/* single attribute in tree */
		new_attrs = ldb_attr_list_copy_add(mem_ctx, *attrs, tree->u.equality.attr);
		talloc_free(*attrs);
		*attrs = new_attrs;
		return 0;
	}

	return -1;
}

static int map_subtree_select_local(struct ldb_module *module, void *mem_ctx, struct ldb_parse_tree **new, const struct ldb_parse_tree *tree);

/* Select a negated subtree that queries attributes in the local partition */
static int map_subtree_select_local_not(struct ldb_module *module, void *mem_ctx, struct ldb_parse_tree **new, const struct ldb_parse_tree *tree)
{
	struct ldb_parse_tree *child;
	int ret;

	/* Prepare new tree */
	*new = talloc_memdup(mem_ctx, tree, sizeof(struct ldb_parse_tree));
	if (*new == NULL) {
		map_oom(module);
		return -1;
	}

	/* Generate new subtree */
	ret = map_subtree_select_local(module, *new, &child, tree->u.isnot.child);
	if (ret) {
		talloc_free(*new);
		return ret;
	}

	/* Prune tree without subtree */
	if (child == NULL) {
		talloc_free(*new);
		*new = NULL;
		return 0;
	}

	(*new)->u.isnot.child = child;

	return ret;
}

/* Select a list of subtrees that query attributes in the local partition */
static int map_subtree_select_local_list(struct ldb_module *module, void *mem_ctx, struct ldb_parse_tree **new, const struct ldb_parse_tree *tree)
{
	int i, j, ret=0;

	/* Prepare new tree */
	*new = talloc_memdup(mem_ctx, tree, sizeof(struct ldb_parse_tree));
	if (*new == NULL) {
		map_oom(module);
		return -1;
	}

	/* Prepare list of subtrees */
	(*new)->u.list.num_elements = 0;
	(*new)->u.list.elements = talloc_array(*new, struct ldb_parse_tree *, tree->u.list.num_elements);
	if ((*new)->u.list.elements == NULL) {
		map_oom(module);
		talloc_free(*new);
		return -1;
	}

	/* Generate new list of subtrees */
	j = 0;
	for (i = 0; i < tree->u.list.num_elements; i++) {
		struct ldb_parse_tree *child;
		ret = map_subtree_select_local(module, *new, &child, tree->u.list.elements[i]);
		if (ret) {
			talloc_free(*new);
			return ret;
		}

		if (child) {
			(*new)->u.list.elements[j] = child;
			j++;
		}
	}

	/* Prune tree without subtrees */
	if (j == 0) {
		talloc_free(*new);
		*new = NULL;
		return 0;
	}

	/* Fix subtree list size */
	(*new)->u.list.num_elements = j;
	(*new)->u.list.elements = talloc_realloc(*new, (*new)->u.list.elements, struct ldb_parse_tree *, (*new)->u.list.num_elements);

	return ret;
}

/* Select a simple subtree that queries attributes in the local partition */
static int map_subtree_select_local_simple(struct ldb_module *module, void *mem_ctx, struct ldb_parse_tree **new, const struct ldb_parse_tree *tree)
{
	/* Prepare new tree */
	*new = talloc_memdup(mem_ctx, tree, sizeof(struct ldb_parse_tree));
	if (*new == NULL) {
		map_oom(module);
		return -1;
	}

	return 0;
}

/* Select subtrees that query attributes in the local partition */
static int map_subtree_select_local(struct ldb_module *module, void *mem_ctx, struct ldb_parse_tree **new, const struct ldb_parse_tree *tree)
{
	const struct ldb_map_context *data = map_get_context(module);

	if (tree == NULL) {
		return 0;
	}

	if (tree->operation == LDB_OP_NOT) {
		return map_subtree_select_local_not(module, mem_ctx, new, tree);
	}

	if (tree->operation == LDB_OP_AND || tree->operation == LDB_OP_OR) {
		return map_subtree_select_local_list(module, mem_ctx, new, tree);
	}

	if (map_attr_check_remote(data, tree->u.equality.attr)) {
		*new = NULL;
		return 0;
	}

	return map_subtree_select_local_simple(module, mem_ctx, new, tree);
}

static int map_subtree_collect_remote(struct ldb_module *module, void *mem_ctx, struct ldb_parse_tree **new, const struct ldb_parse_tree *tree);

/* Collect a negated subtree that queries attributes in the remote partition */
static int map_subtree_collect_remote_not(struct ldb_module *module, void *mem_ctx, struct ldb_parse_tree **new, const struct ldb_parse_tree *tree)
{
	struct ldb_parse_tree *child;
	int ret;

	/* Prepare new tree */
	*new = talloc_memdup(mem_ctx, tree, sizeof(struct ldb_parse_tree));
	if (*new == NULL) {
		map_oom(module);
		return -1;
	}

	/* Generate new subtree */
	ret = map_subtree_collect_remote(module, *new, &child, tree->u.isnot.child);
	if (ret) {
		talloc_free(*new);
		return ret;
	}

	/* Prune tree without subtree */
	if (child == NULL) {
		talloc_free(*new);
		*new = NULL;
		return 0;
	}

	(*new)->u.isnot.child = child;

	return ret;
}

/* Collect a list of subtrees that query attributes in the remote partition */
static int map_subtree_collect_remote_list(struct ldb_module *module, void *mem_ctx, struct ldb_parse_tree **new, const struct ldb_parse_tree *tree)
{
	int i, j, ret=0;

	/* Prepare new tree */
	*new = talloc_memdup(mem_ctx, tree, sizeof(struct ldb_parse_tree));
	if (*new == NULL) {
		map_oom(module);
		return -1;
	}

	/* Prepare list of subtrees */
	(*new)->u.list.num_elements = 0;
	(*new)->u.list.elements = talloc_array(*new, struct ldb_parse_tree *, tree->u.list.num_elements);
	if ((*new)->u.list.elements == NULL) {
		map_oom(module);
		talloc_free(*new);
		return -1;
	}

	/* Generate new list of subtrees */
	j = 0;
	for (i = 0; i < tree->u.list.num_elements; i++) {
		struct ldb_parse_tree *child;
		ret = map_subtree_collect_remote(module, *new, &child, tree->u.list.elements[i]);
		if (ret) {
			talloc_free(*new);
			return ret;
		}

		if (child) {
			(*new)->u.list.elements[j] = child;
			j++;
		}
	}

	/* Prune tree without subtrees */
	if (j == 0) {
		talloc_free(*new);
		*new = NULL;
		return 0;
	}

	/* Fix subtree list size */
	(*new)->u.list.num_elements = j;
	(*new)->u.list.elements = talloc_realloc(*new, (*new)->u.list.elements, struct ldb_parse_tree *, (*new)->u.list.num_elements);

	return ret;
}

/* Collect a simple subtree that queries attributes in the remote partition */
int map_subtree_collect_remote_simple(struct ldb_module *module, void *mem_ctx, struct ldb_parse_tree **new, const struct ldb_parse_tree *tree, const struct ldb_map_attribute *map)
{
	const char *attr;

	/* Prepare new tree */
	*new = talloc(mem_ctx, struct ldb_parse_tree);
	if (*new == NULL) {
		map_oom(module);
		return -1;
	}
	**new = *tree;
	
	if (map->type == MAP_KEEP) {
		/* Nothing to do here */
		return 0;
	}

	/* Store attribute and value in new tree */
	switch (tree->operation) {
	case LDB_OP_PRESENT:
		attr = map_attr_map_local(*new, map, tree->u.present.attr);
		(*new)->u.present.attr = attr;
		break;
	case LDB_OP_SUBSTRING:
	{
		attr = map_attr_map_local(*new, map, tree->u.substring.attr);
		(*new)->u.substring.attr = attr;
		break;
	}
	case LDB_OP_EQUALITY:
		attr = map_attr_map_local(*new, map, tree->u.equality.attr);
		(*new)->u.equality.attr = attr;
		break;
	case LDB_OP_LESS:
	case LDB_OP_GREATER:
	case LDB_OP_APPROX:
		attr = map_attr_map_local(*new, map, tree->u.comparison.attr);
		(*new)->u.comparison.attr = attr;
		break;
	case LDB_OP_EXTENDED:
		attr = map_attr_map_local(*new, map, tree->u.extended.attr);
		(*new)->u.extended.attr = attr;
		break;
	default:			/* unknown kind of simple subtree */
		talloc_free(*new);
		return -1;
	}

	if (attr == NULL) {
		talloc_free(*new);
		*new = NULL;
		return 0;
	}

	if (map->type == MAP_RENAME) {
		/* Nothing more to do here, the attribute has been renamed */
		return 0;
	}

	/* Store attribute and value in new tree */
	switch (tree->operation) {
	case LDB_OP_PRESENT:
		break;
	case LDB_OP_SUBSTRING:
	{
		int i;
		/* Map value */
		(*new)->u.substring.chunks = NULL;
		for (i=0; tree->u.substring.chunks[i]; i++) {
			(*new)->u.substring.chunks = talloc_realloc(*new, (*new)->u.substring.chunks, struct ldb_val *, i+2);
			if (!(*new)->u.substring.chunks) {
				talloc_free(*new);
				*new = NULL;
				return 0;
			}
			(*new)->u.substring.chunks[i] = talloc(*new, struct ldb_val);
			if (!(*new)->u.substring.chunks[i]) {
				talloc_free(*new);
				*new = NULL;
				return 0;
			}
			*(*new)->u.substring.chunks[i] = ldb_val_map_local(module, *new, map, tree->u.substring.chunks[i]);
			(*new)->u.substring.chunks[i+1] = NULL;
		}
		break;
	}
	case LDB_OP_EQUALITY:
		(*new)->u.equality.value = ldb_val_map_local(module, *new, map, &tree->u.equality.value);
		break;
	case LDB_OP_LESS:
	case LDB_OP_GREATER:
	case LDB_OP_APPROX:
		(*new)->u.comparison.value = ldb_val_map_local(module, *new, map, &tree->u.comparison.value);
		break;
	case LDB_OP_EXTENDED:
		(*new)->u.extended.value = ldb_val_map_local(module, *new, map, &tree->u.extended.value);
		(*new)->u.extended.rule_id = talloc_strdup(*new, tree->u.extended.rule_id);
		break;
	default:			/* unknown kind of simple subtree */
		talloc_free(*new);
		return -1;
	}

	return 0;
}

/* Collect subtrees that query attributes in the remote partition */
static int map_subtree_collect_remote(struct ldb_module *module, void *mem_ctx, struct ldb_parse_tree **new, const struct ldb_parse_tree *tree)
{
	const struct ldb_map_context *data = map_get_context(module);
	const struct ldb_map_attribute *map;

	if (tree == NULL) {
		return 0;
	}

	if (tree->operation == LDB_OP_NOT) {
		return map_subtree_collect_remote_not(module, mem_ctx, new, tree);
	}

	if ((tree->operation == LDB_OP_AND) || (tree->operation == LDB_OP_OR)) {
		return map_subtree_collect_remote_list(module, mem_ctx, new, tree);
	}

	if (!map_attr_check_remote(data, tree->u.equality.attr)) {
		*new = NULL;
		return 0;
	}

	map = map_attr_find_local(data, tree->u.equality.attr);
	if (map->convert_operator) {
		return map->convert_operator(module, mem_ctx, new, tree);
	}

	if (map->type == MAP_GENERATE) {
		ldb_debug(module->ldb, LDB_DEBUG_WARNING, "ldb_map: "
			  "Skipping attribute '%s': "
			  "'convert_operator' not set\n",
			  tree->u.equality.attr);
		*new = NULL;
		return 0;
	}

	return map_subtree_collect_remote_simple(module, mem_ctx, new, tree, map);
}

/* Split subtrees that query attributes in the local partition from
 * those that query the remote partition. */
static int ldb_parse_tree_partition(struct ldb_module *module,
					void *mem_ctx,
					struct ldb_parse_tree **local_tree,
					struct ldb_parse_tree **remote_tree,
					const struct ldb_parse_tree *tree)
{
	int ret;

	*local_tree = NULL;
	*remote_tree = NULL;

	/* No original tree */
	if (tree == NULL) {
		return 0;
	}

	/* Generate local tree */
	ret = map_subtree_select_local(module, mem_ctx, local_tree, tree);
	if (ret) {
		return ret;
	}

	/* Generate remote tree */
	ret = map_subtree_collect_remote(module, mem_ctx, remote_tree, tree);
	if (ret) {
		talloc_free(*local_tree);
		return ret;
	}

	return 0;
}

/* Collect a list of attributes required either explicitly from a
 * given list or implicitly  from a given parse tree; split the
 * collected list into local and remote parts. */
static int map_attrs_collect_and_partition(struct ldb_module *module, struct map_context *ac,
					   const char * const *search_attrs, 
					   const struct ldb_parse_tree *tree)
{
	void *tmp_ctx;
	const char **tree_attrs;
	const char **remote_attrs;
	const char **local_attrs;
	int ret;

	/* There is no tree, just partition the searched attributes */
	if (tree == NULL) {
		ret = map_attrs_partition(module, ac, 
					  &local_attrs, &remote_attrs, search_attrs);
		if (ret == 0) {
			ac->local_attrs = local_attrs;
			ac->remote_attrs = remote_attrs;
			ac->all_attrs = search_attrs;
		}
		return ret; 
	}

	/* Create context for temporary memory */
	tmp_ctx = talloc_new(ac);
	if (tmp_ctx == NULL) {
		goto oom;
	}

	/* Prepare list of attributes from tree */
	tree_attrs = talloc_array(tmp_ctx, const char *, 1);
	if (tree_attrs == NULL) {
		talloc_free(tmp_ctx);
		goto oom;
	}
	tree_attrs[0] = NULL;

	/* Collect attributes from tree */
	ret = ldb_parse_tree_collect_attrs(module, tmp_ctx, &tree_attrs, tree);
	if (ret) {
		goto done;
	}

	/* Merge attributes from search operation */
	ret = map_attrs_merge(module, tmp_ctx, &tree_attrs, search_attrs);
	if (ret) {
		goto done;
	}

	/* Split local from remote attributes */
	ret = map_attrs_partition(module, ac, &local_attrs, 
				  &remote_attrs, tree_attrs);
	
	if (ret == 0) {
		ac->local_attrs = local_attrs;
		ac->remote_attrs = remote_attrs;
		talloc_steal(ac, tree_attrs);
		ac->all_attrs = tree_attrs;
	}
done:
	/* Free temporary memory */
	talloc_free(tmp_ctx);
	return ret;

oom:
	map_oom(module);
	return -1;
}


/* Outbound requests: search
 * ========================= */

static int map_remote_search_callback(struct ldb_request *req,
					struct ldb_reply *ares);
static int map_local_merge_callback(struct ldb_request *req,
					struct ldb_reply *ares);
static int map_search_local(struct map_context *ac);

static int map_save_entry(struct map_context *ac, struct ldb_reply *ares)
{
	struct map_reply *mr;

	mr = talloc_zero(ac, struct map_reply);
	if (mr == NULL) {
		map_oom(ac->module);
		return LDB_ERR_OPERATIONS_ERROR;
	}
	mr->remote = talloc_steal(mr, ares);
	if (ac->r_current) {
		ac->r_current->next = mr;
	} else {
		/* first entry */
		ac->r_list = mr;
	}
	ac->r_current = mr;

	return LDB_SUCCESS;
}

/* Pass a merged search result up the callback chain. */
int map_return_entry(struct map_context *ac, struct ldb_reply *ares)
{
	struct ldb_message_element *el;
	const char * const *attrs;
	int i;

	/* Merged result doesn't match original query, skip */
	if (!ldb_match_msg(ac->module->ldb, ares->message,
			   ac->req->op.search.tree,
			   ac->req->op.search.base,
			   ac->req->op.search.scope)) {
		ldb_debug(ac->module->ldb, LDB_DEBUG_TRACE, "ldb_map: "
			  "Skipping record '%s': "
			  "doesn't match original search\n",
			  ldb_dn_get_linearized(ares->message->dn));
		return LDB_SUCCESS;
	}

	/* Limit result to requested attrs */
	if (ac->req->op.search.attrs &&
	    (! ldb_attr_in_list(ac->req->op.search.attrs, "*"))) {

		attrs = ac->req->op.search.attrs;
		i = 0;

		while (i < ares->message->num_elements) {

			el = &ares->message->elements[i];
			if ( ! ldb_attr_in_list(attrs, el->name)) {
				ldb_msg_remove_element(ares->message, el);
			} else {
				i++;
			}
		}
	}

	return ldb_module_send_entry(ac->req, ares->message, ares->controls);
}

/* Search a record. */
int map_search(struct ldb_module *module, struct ldb_request *req)
{
	struct ldb_parse_tree *remote_tree;
	struct ldb_parse_tree *local_tree;
	struct ldb_request *remote_req;
	struct map_context *ac;
	int ret;

	const char *wildcard[] = { "*", NULL };
	const char * const *attrs;

	if (!module->private_data) /* if we're not yet initialized, go to the next module */
		return ldb_next_request(module, req);

	/* Do not manipulate our control entries */
	if (ldb_dn_is_special(req->op.search.base)) {
		return ldb_next_request(module, req);
	}

	/* No mapping requested, skip to next module */
	if ((req->op.search.base) && (!ldb_dn_check_local(module, req->op.search.base))) {
		return ldb_next_request(module, req);
	}

	/* TODO: How can we be sure about which partition we are
	 *	 targetting when there is no search base? */

	/* Prepare context and handle */
	ac = map_init_context(module, req);
	if (ac == NULL) {
		return LDB_ERR_OPERATIONS_ERROR;
	}

	/* It is easier to deal with the two different ways of
	 * expressing the wildcard in the same codepath */
	attrs = req->op.search.attrs;
	if (attrs == NULL) {
		attrs = wildcard;
	}

	/* Split local from remote attrs */
	ret = map_attrs_collect_and_partition(module, ac, 
					      attrs, req->op.search.tree);
	if (ret) {
		return LDB_ERR_OPERATIONS_ERROR;
	}

	/* Split local from remote tree */
	ret = ldb_parse_tree_partition(module, ac,
				       &local_tree, &remote_tree,
				       req->op.search.tree);
	if (ret) {
		return LDB_ERR_OPERATIONS_ERROR;
	}

	if (((local_tree != NULL) && (remote_tree != NULL)) &&
	    (!ldb_parse_tree_check_splittable(req->op.search.tree))) {
		/* The query can't safely be split, enumerate the remote partition */
		local_tree = NULL;
		remote_tree = NULL;
	}

	if (local_tree == NULL) {
		/* Construct default local parse tree */
		local_tree = talloc_zero(ac, struct ldb_parse_tree);
		if (local_tree == NULL) {
			map_oom(ac->module);
			return LDB_ERR_OPERATIONS_ERROR;
		}

		local_tree->operation = LDB_OP_PRESENT;
		local_tree->u.present.attr = talloc_strdup(local_tree, IS_MAPPED);
	}
	if (remote_tree == NULL) {
		/* Construct default remote parse tree */
		remote_tree = ldb_parse_tree(ac, NULL);
		if (remote_tree == NULL) {
			return LDB_ERR_OPERATIONS_ERROR;
		}
	}

	ac->local_tree = local_tree;

	/* Prepare the remote operation */
	ret = ldb_build_search_req_ex(&remote_req, module->ldb, ac,
				      req->op.search.base,
				      req->op.search.scope,
				      remote_tree,
				      ac->remote_attrs,
				      req->controls,
				      ac, map_remote_search_callback,
				      req);
	if (ret != LDB_SUCCESS) {
		return LDB_ERR_OPERATIONS_ERROR;
	}

	return ldb_next_remote_request(module, remote_req);
}

/* Now, search the local part of a remote search result. */
static int map_remote_search_callback(struct ldb_request *req,
					struct ldb_reply *ares)
{
	struct map_context *ac;
	int ret;

	ac = talloc_get_type(req->context, struct map_context);

	if (!ares) {
		return ldb_module_done(ac->req, NULL, NULL,
					LDB_ERR_OPERATIONS_ERROR);
	}
	if (ares->error != LDB_SUCCESS) {
		return ldb_module_done(ac->req, ares->controls,
					ares->response, ares->error);
	}

	switch (ares->type) {
	case LDB_REPLY_REFERRAL:

		/* ignore referrals */
		talloc_free(ares);
		return LDB_SUCCESS;

	case LDB_REPLY_ENTRY:

		/* Map result record into a local message */
		ret = map_reply_remote(ac, ares);
		if (ret) {
			talloc_free(ares);
			return ldb_module_done(ac->req, NULL, NULL,
						LDB_ERR_OPERATIONS_ERROR);
		}

		/* if we have no local db, then we can just return the reply to
		 * the upper layer, otherwise we must save it and process it
		 * when all replies ahve been gathered */
		if ( ! map_check_local_db(ac->module)) {
			ret = map_return_entry(ac, ares);
		} else {
			ret = map_save_entry(ac,ares);
		}

		if (ret != LDB_SUCCESS) {
			talloc_free(ares);
			return ldb_module_done(ac->req, NULL, NULL,
						LDB_ERR_OPERATIONS_ERROR);
		}
		break;

	case LDB_REPLY_DONE:

		if ( ! map_check_local_db(ac->module)) {
			return ldb_module_done(ac->req, ares->controls,
						ares->response, LDB_SUCCESS);
		}

		talloc_free(ares);

		/* reset the pointer to the start of the list */
		ac->r_current = ac->r_list;

		/* no entry just return */
		if (ac->r_current == NULL) {
			return ldb_module_done(ac->req, ares->controls,
						ares->response, LDB_SUCCESS);
		}

		ret = map_search_local(ac);
		if (ret != LDB_SUCCESS) {
			return ldb_module_done(ac->req, NULL, NULL, ret);
		}
	}

	return LDB_SUCCESS;
}

static int map_search_local(struct map_context *ac)
{
	struct ldb_request *search_req;

	if (ac->r_current == NULL || ac->r_current->remote == NULL) {
		return LDB_ERR_OPERATIONS_ERROR;
	}

	/* Prepare local search request */
	/* TODO: use GUIDs here instead? */
	search_req = map_search_base_req(ac,
					 ac->r_current->remote->message->dn,
					 NULL, NULL,
					 ac, map_local_merge_callback);
	if (search_req == NULL) {
		return LDB_ERR_OPERATIONS_ERROR;
	}

	return ldb_next_request(ac->module, search_req);
}

/* Merge the remote and local parts of a search result. */
int map_local_merge_callback(struct ldb_request *req, struct ldb_reply *ares)
{
	struct map_context *ac;
	int ret;

	ac = talloc_get_type(req->context, struct map_context);

	if (!ares) {
		return ldb_module_done(ac->req, NULL, NULL,
					LDB_ERR_OPERATIONS_ERROR);
	}
	if (ares->error != LDB_SUCCESS) {
		return ldb_module_done(ac->req, ares->controls,
					ares->response, ares->error);
	}

	switch (ares->type) {
	case LDB_REPLY_ENTRY:
		/* We have already found a local record */
		if (ac->r_current->local) {
			talloc_free(ares);
			ldb_set_errstring(ac->module->ldb, "ldb_map: Too many results!");
			return ldb_module_done(ac->req, NULL, NULL,
						LDB_ERR_OPERATIONS_ERROR);
		}

		/* Store local result */
		ac->r_current->local = talloc_steal(ac->r_current, ares);

		break;

	case LDB_REPLY_REFERRAL:
		/* ignore referrals */
		talloc_free(ares);
		break;

	case LDB_REPLY_DONE:
		talloc_free(ares);

		/* No local record found, map and send remote record */
		if (ac->r_current->local != NULL) {
			/* Merge remote into local message */
			ret = ldb_msg_merge_local(ac->module,
						  ac->r_current->local->message,
						  ac->r_current->remote->message);
			if (ret == LDB_SUCCESS) {
				ret = map_return_entry(ac, ac->r_current->local);
			}
			if (ret != LDB_SUCCESS) {
				return ldb_module_done(ac->req, NULL, NULL,
							LDB_ERR_OPERATIONS_ERROR);
			}
		} else {
			ret = map_return_entry(ac, ac->r_current->remote);
			if (ret != LDB_SUCCESS) {
				return ldb_module_done(ac->req,
							NULL, NULL, ret);
			}
		}

		if (ac->r_current->next != NULL) {
			ac->r_current = ac->r_current->next;
			if (ac->r_current->remote->type == LDB_REPLY_ENTRY) {
				ret = map_search_local(ac);
				if (ret != LDB_SUCCESS) {
					return ldb_module_done(ac->req,
							       NULL, NULL, ret);
				}
				break;
			}
		}

		/* ok we are done with all search, finally it is time to
		 * finish operations for this module */
		return ldb_module_done(ac->req,
					ac->r_current->remote->controls,
					ac->r_current->remote->response,
					ac->r_current->remote->error);
	}

	return LDB_SUCCESS;
}