summaryrefslogtreecommitdiff
path: root/source4/lib/ldb/ldb_map/ldb_map.c
blob: 5ae4a435d86a29f4b6dcf7590d2e6a3941471ba9 (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
/* 
   ldb database library - map backend

   Copyright (C) Jelmer Vernooij 2005

     ** 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 2 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, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#include "includes.h"
#include "lib/ldb/include/ldb.h"
#include "lib/ldb/include/ldb_private.h"
#include "lib/ldb/ldb_map/ldb_map.h"

/* TODO:
 *  - objectclass hint in ldb_map_attribute 
 *     for use when multiple remote attributes (independant of each other)
 *     map to one local attribute. E.g.: (uid, gidNumber) -> unixName
 *     (use MAP_GENERATE instead ?) 
 */

static const struct ldb_map_attribute builtin_attribute_maps[];

struct map_private {
	struct ldb_map_context context;
	const char *last_err_string;
};


/* find an attribute by the local name */
static const struct ldb_map_attribute *map_find_attr_local(struct ldb_map_context *privdat, const char *attr)
{
	int i;

	for (i = 0; privdat->attribute_maps[i].local_name; i++) {
		if (!ldb_attr_cmp(privdat->attribute_maps[i].local_name, attr)) 
			return &privdat->attribute_maps[i];
	}

	return NULL;
}

/* find an attribute by the remote name */
static const struct ldb_map_attribute *map_find_attr_remote(struct ldb_map_context *privdat, const char *attr)
{
	int i;

	for (i = 0; privdat->attribute_maps[i].local_name; i++) {
		if (privdat->attribute_maps[i].type == MAP_IGNORE)
			continue;

		if (privdat->attribute_maps[i].type == MAP_GENERATE)
			continue;

		if (privdat->attribute_maps[i].type == MAP_KEEP &&
			ldb_attr_cmp(privdat->attribute_maps[i].local_name, attr) == 0)
			return &privdat->attribute_maps[i];

		if ((privdat->attribute_maps[i].type == MAP_RENAME ||
			privdat->attribute_maps[i].type == MAP_CONVERT) &&
			ldb_attr_cmp(privdat->attribute_maps[i].u.rename.remote_name, attr) == 0) 
			return &privdat->attribute_maps[i];

	}

	return NULL;
}

static struct ldb_parse_tree *ldb_map_parse_tree(struct ldb_module *module, TALLOC_CTX *ctx, const struct ldb_parse_tree *tree)
{
	int i;
	const struct ldb_map_attribute *attr;
	struct ldb_parse_tree *new_tree;
	enum ldb_map_attr_type map_type;
	struct ldb_val value, newvalue;
	struct ldb_map_context *privdat = &((struct map_private *)module->private_data)->context;

	if (tree == NULL)
		return NULL;
	

	/* Find attr in question and:
	 *  - if it has a convert_operator function, run that
	 *  - otherwise, replace attr name with required[0] */

	if (tree->operation == LDB_OP_AND || 
		tree->operation == LDB_OP_OR) {
		
		new_tree = talloc_memdup(ctx, tree, sizeof(*tree));
		new_tree->u.list.elements = talloc_array(new_tree, struct ldb_parse_tree *, tree->u.list.num_elements);
		for (i = 0; i < new_tree->u.list.num_elements; i++) {
			new_tree->u.list.elements[i] = ldb_map_parse_tree(module, new_tree, tree->u.list.elements[i]);
		}

		return new_tree;
	}
		
	if (tree->operation == LDB_OP_NOT) {
		new_tree = talloc_memdup(ctx, tree, sizeof(*tree));
		new_tree->u.isnot.child = ldb_map_parse_tree(module, new_tree, tree->u.isnot.child);
		return new_tree;
	}

	/* tree->operation is LDB_OP_EQUALITY, LDB_OP_SUBSTRING, LDB_OP_GREATER,
	 * LDB_OP_LESS, LDB_OP_APPROX, LDB_OP_PRESENT or LDB_OP_EXTENDED
	 *
	 * (all have attr as the first element)
	 */

	attr = map_find_attr_local(privdat, tree->u.equality.attr);

	if (!attr) {
		DEBUG(0, ("Unable to find local attribute '%s', leaving as is\n", tree->u.equality.attr));
		map_type = MAP_KEEP;
	} else {
		map_type = attr->type;
	}

	if (attr && attr->convert_operator) {
		/* Run convert_operator */
		return attr->convert_operator(privdat, module, tree);
	}

	if (map_type == MAP_IGNORE)
		return NULL;

	if (map_type == MAP_GENERATE) {
		DEBUG(0, ("Can't do conversion for MAP_GENERATE in map_parse_tree without convert_operator for '%s'\n", tree->u.equality.attr));
		return NULL;
	}

	if (tree->operation == LDB_OP_EQUALITY) {
		value = tree->u.equality.value;
	} else if (tree->operation == LDB_OP_LESS || tree->operation == LDB_OP_GREATER ||
			   tree->operation == LDB_OP_APPROX) {
		value = tree->u.comparison.value;
	} else if (tree->operation == LDB_OP_EXTENDED) {
		value = tree->u.extended.value;
	}
	
	new_tree = talloc_memdup(ctx, tree, sizeof(*tree));

	if (map_type == MAP_KEEP) {
		new_tree->u.equality.attr = talloc_strdup(new_tree, tree->u.equality.attr);
	} else { /* MAP_RENAME / MAP_CONVERT */
		new_tree->u.equality.attr = talloc_strdup(new_tree, attr->u.rename.remote_name);
	}

	if (new_tree->operation == LDB_OP_PRESENT) 
		return new_tree;
		
	if (new_tree->operation == LDB_OP_SUBSTRING) {
		new_tree->u.substring.chunks = NULL; /* FIXME! */
		return new_tree;
	}

	if (map_type == MAP_CONVERT) {
		newvalue = attr->u.convert.convert_local(privdat, new_tree, &value);
	} else {
		newvalue = ldb_val_dup(new_tree, &value);
	}

	if (new_tree->operation == LDB_OP_EQUALITY) {
		new_tree->u.equality.value = newvalue;
	} else if (new_tree->operation == LDB_OP_LESS || new_tree->operation == LDB_OP_GREATER ||
			   new_tree->operation == LDB_OP_APPROX) {
		new_tree->u.comparison.value = newvalue;
	} else if (new_tree->operation == LDB_OP_EXTENDED) {
		new_tree->u.extended.value = newvalue;
		new_tree->u.extended.rule_id = talloc_strdup(new_tree, tree->u.extended.rule_id);
	}
	
	return new_tree;
}

/* Remote DN -> Local DN */
static struct ldb_dn *map_remote_dn(struct ldb_map_context *privdat, TALLOC_CTX *ctx, const struct ldb_dn *dn)
{
	struct ldb_dn *newdn;
	int i;

	if (dn == NULL)
		return NULL;

	newdn = talloc_memdup(ctx, dn, sizeof(*dn));
	if (!newdn) 
		return NULL;

	newdn->components = talloc_array(newdn, struct ldb_dn_component, newdn->comp_num); 

	if (!newdn->components)
		return NULL;

	/* For each rdn, map the attribute name and possibly the 
	 * complete rdn */
	
	for (i = 0; i < dn->comp_num; i++) {
		const struct ldb_map_attribute *attr = map_find_attr_remote(privdat, dn->components[i].name);
		enum ldb_map_attr_type map_type;

		/* Unknown attribute - leave this dn as is and hope the best... */
		if (!attr) map_type = MAP_KEEP;
		else map_type = attr->type;
			
		switch (map_type) { 
		case MAP_IGNORE:
		case MAP_GENERATE:
			DEBUG(0, ("Local MAP_IGNORE or MAP_GENERATE attribute '%s' used in DN!", dn->components[i].name));
			talloc_free(newdn);
			return NULL;

		case MAP_KEEP:
			newdn->components[i].name = talloc_strdup(newdn->components, dn->components[i].name);
			newdn->components[i].value = ldb_val_dup(newdn->components, &dn->components[i].value);
			break;
			
		case MAP_CONVERT:
			newdn->components[i].name = talloc_strdup(newdn->components, attr->local_name);
			newdn->components[i].value = attr->u.convert.convert_remote(privdat, ctx, &dn->components[i].value);
			break;
			
		case MAP_RENAME:
			newdn->components[i].name = talloc_strdup(newdn->components, attr->local_name);
			newdn->components[i].value = ldb_val_dup(newdn->components, &dn->components[i].value);
			break;
		}
	}
	return newdn;
}

/* Local DN -> Remote DN */
static struct ldb_dn *map_local_dn(struct ldb_map_context *privdat, TALLOC_CTX *ctx, const struct ldb_dn *dn)
{	
	struct ldb_dn *newdn;
	int i;

	if (dn == NULL)
		return NULL;

	newdn = talloc_memdup(ctx, dn, sizeof(*dn));
	if (!newdn) 
		return NULL;

	newdn->components = talloc_array(newdn, struct ldb_dn_component, newdn->comp_num); 

	if (!newdn->components)
		return NULL;

	/* For each rdn, map the attribute name and possibly the 
	 * complete rdn using an equality convert_operator call */
	
	for (i = 0; i < dn->comp_num; i++) {
		const struct ldb_map_attribute *attr = map_find_attr_local(privdat, dn->components[i].name);
		enum ldb_map_attr_type map_type;

		/* Unknown attribute - leave this dn as is and hope the best... */
		if (!attr) map_type = MAP_KEEP; else map_type = attr->type;
		
		switch (map_type) 
		{
			case MAP_IGNORE: 
			case MAP_GENERATE:
			DEBUG(0, ("Local MAP_IGNORE/MAP_GENERATE attribute '%s' used in DN!", dn->components[i].name));
			talloc_free(newdn);
			return NULL;

			case MAP_CONVERT: 
				newdn->components[i].name = talloc_strdup(newdn->components, attr->u.convert.remote_name);
				newdn->components[i].value = attr->u.convert.convert_local(privdat, newdn->components, &dn->components[i].value);
			break;
			
			case MAP_RENAME:
				newdn->components[i].name = talloc_strdup(newdn->components, attr->u.rename.remote_name);
				newdn->components[i].value = ldb_val_dup(newdn->components, &dn->components[i].value);
			break;

			case MAP_KEEP:
				newdn->components[i].name = talloc_strdup(newdn->components, dn->components[i].name);
				newdn->components[i].value = ldb_val_dup(newdn->components, &dn->components[i].value);
			continue;
		}
	}

	return newdn;
}

/* Loop over ldb_map_attribute array and add remote_names */
static const char **ldb_map_attrs(struct ldb_module *module, const char *const attrs[])
{
	int i;
	const char **ret;
	int ar_size = 0, last_element = 0;
	struct ldb_map_context *privdat = &((struct map_private *)module->private_data)->context;

	if (attrs == NULL) 
		return NULL;

	/* Start with good guess of number of elements */
	for (i = 0; attrs[i]; i++);

	ret = talloc_array(module, const char *, i);
	ar_size = i;

	for (i = 0; attrs[i]; i++) {
		int j;
		const struct ldb_map_attribute *attr = map_find_attr_local(privdat, attrs[i]);
		enum ldb_map_attr_type map_type;

		if (!attr) {
			DEBUG(0, ("Local attribute '%s' does not have a definition!\n", attrs[i]));
			map_type = MAP_IGNORE;
		} else map_type = attr->type;

		switch (map_type)
		{ 
			case MAP_IGNORE: break;
			case MAP_KEEP: 
				if (last_element >= ar_size) {
					ret = talloc_realloc(module, ret, const char *, ar_size+1);
					ar_size++;
				}
				ret[last_element] = attr->local_name;
				last_element++;
				break;

			case MAP_RENAME:
			case MAP_CONVERT:
				if (last_element >= ar_size) {
					ret = talloc_realloc(module, ret, const char *, ar_size+1);
					ar_size++;
				}
				ret[last_element] = attr->u.rename.remote_name;
				last_element++;
				break;

			case MAP_GENERATE:
				/* Add remote_names[] for this attribute to the list of 
				 * attributes to request from the remote server */
				for (j = 0; attr->u.generate.remote_names[j]; j++) {
					if (last_element >= ar_size) {
						ret = talloc_realloc(module, ret, const char *, ar_size+1);
						ar_size++;
					}
					ret[last_element] = attr->u.generate.remote_names[j];			
					last_element++;
				}
				break;
		} 
	}
	
	if (last_element >= ar_size) {
		ret = talloc_realloc(module, ret, const char *, ar_size+1);
		ar_size++;
	}

	ret[last_element] = NULL;

	return ret;
}

static const char **available_local_attributes(struct ldb_module *module, const struct ldb_message *msg)
{
	struct ldb_map_context *privdat = &((struct map_private *)module->private_data)->context;
	int i, j;
	int count = 0;
	const char **ret = talloc_array(module, const char *, 1);

	ret[0] = NULL;

	for (i = 0; privdat->attribute_maps[i].local_name; i++) {
		BOOL avail = False;
		const struct ldb_map_attribute *attr = &privdat->attribute_maps[i];

		/* If all remote attributes for this attribute are present, add the 
		 * local one to the list */
		
		switch (attr->type) {
		case MAP_IGNORE: break;
		case MAP_KEEP: 
				avail = (ldb_msg_find_ldb_val(msg, attr->local_name) != NULL); 
				break;
				
		case MAP_RENAME:
		case MAP_CONVERT:
				avail = (ldb_msg_find_ldb_val(msg, attr->u.rename.remote_name) != NULL);
				break;

		case MAP_GENERATE:
				avail = True;
				for (j = 0; attr->u.generate.remote_names[j]; j++) {
					avail &= (ldb_msg_find_ldb_val(msg, attr->u.generate.remote_names[j]) != NULL);
				}
				break;
		}

		if (!avail)
			continue;

		ret = talloc_realloc(module, ret, const char *, count+2);
		ret[count] = attr->local_name;
		ret[count+1] = NULL;
		count++;
	}

	return ret;
}

/* Used for search */
static struct ldb_message *ldb_map_message_incoming(struct ldb_module *module, const char * const*attrs, const struct ldb_message *mi)
{
	int i, j;
	struct ldb_message *msg = talloc_zero(module, struct ldb_message);
	struct ldb_message_element *elm, *oldelm;
	struct ldb_map_context *privdat = &((struct map_private *)module->private_data)->context;
	const char **newattrs = NULL;

	msg->dn = map_remote_dn(privdat, module, mi->dn);

	/* Loop over attrs, find in ldb_map_attribute array and 
	 * run generate() */

	if (attrs == NULL) {
		/* Generate list of the local attributes that /can/ be generated
		 * using the specific remote attributes */

		attrs = newattrs = available_local_attributes(module, mi);
	}

	for (i = 0; attrs[i]; i++) {
		const struct ldb_map_attribute *attr = map_find_attr_local(privdat, attrs[i]);
		enum ldb_map_attr_type map_type;

		if (!attr) {
			DEBUG(0, ("Unable to find local attribute '%s' when generating incoming message\n", attrs[i]));
			map_type = MAP_IGNORE;
		} else map_type = attr->type;

		switch (map_type) {
			case MAP_IGNORE:break;
			case MAP_RENAME:
				oldelm = ldb_msg_find_element(mi, attr->u.rename.remote_name);
				if (!oldelm) continue;

				elm = talloc(msg, struct ldb_message_element);
				elm->name = talloc_strdup(elm, attr->local_name);
				elm->num_values = oldelm->num_values;
				elm->values = talloc_reference(elm, oldelm->values);

				ldb_msg_add(module->ldb, msg, elm, oldelm->flags);
				break;
				
			case MAP_CONVERT:
				oldelm = ldb_msg_find_element(mi, attr->u.rename.remote_name);
				if (!oldelm) continue;

				elm = talloc(msg, struct ldb_message_element);
				elm->name = talloc_strdup(elm, attr->local_name);
				elm->num_values = oldelm->num_values;
				elm->values = talloc_array(elm, struct ldb_val, elm->num_values);

				for (j = 0; j < oldelm->num_values; j++)
					elm->values[j] = attr->u.convert.convert_remote(privdat, elm, &oldelm->values[j]);

				ldb_msg_add(module->ldb, msg, elm, oldelm->flags);
				break;

			case MAP_KEEP:
				oldelm = ldb_msg_find_element(mi, attr->local_name);
				if (!oldelm) continue;
				
				elm = talloc(msg, struct ldb_message_element);

				elm->num_values = oldelm->num_values;
				elm->values = talloc_reference(elm, oldelm->values);
				elm->name = talloc_strdup(elm, oldelm->name);

				ldb_msg_add(module->ldb, msg, elm, oldelm->flags);
				break;

			case MAP_GENERATE:
				elm = attr->u.generate.generate_local(privdat, msg, attr->local_name, mi);
				if (!elm) continue;

				ldb_msg_add(module->ldb, msg, elm, elm->flags);
				break;
			default: 
				DEBUG(0, ("Unknown attr->type for %s", attr->local_name));
				break;
		}
	}

	talloc_free(newattrs);

	return msg;
}

/* Used for add, modify */
static struct ldb_message *ldb_map_message_outgoing(struct ldb_module *module, const struct ldb_message *mo)
{
	struct ldb_map_context *privdat = &((struct map_private *)module->private_data)->context;
	struct ldb_message *msg = talloc_zero(module, struct ldb_message);
	struct ldb_message_element *elm;
	int i,j;
	
	msg->private_data = mo->private_data;
	
	msg->dn = map_local_dn(privdat, module, mo->dn);

	/* Loop over mi and call generate_remote for each attribute */
	for (i = 0; i < mo->num_elements; i++) {
		const struct ldb_map_attribute *attr = map_find_attr_local(privdat, mo->elements[i].name);
		enum ldb_map_attr_type map_type;

		if (!attr) {
			DEBUG(0, ("Undefined local attribute '%s', ignoring\n", mo->elements[i].name));
			map_type = MAP_IGNORE;
			continue;
		} else map_type = attr->type;

		switch (map_type) {
		case MAP_IGNORE: break;
		case MAP_RENAME:
			elm = talloc(msg, struct ldb_message_element);

			elm->name = talloc_strdup(elm, attr->u.rename.remote_name);
			elm->num_values = mo->elements[i].num_values;
			elm->values = talloc_reference(elm, mo->elements[i].values);

			ldb_msg_add(module->ldb, msg, elm, mo->elements[i].flags);
			break;

		case MAP_CONVERT:
			elm = talloc(msg, struct ldb_message_element);

			elm->name = talloc_strdup(elm, attr->u.rename.remote_name);
			elm->num_values = mo->elements[i].num_values;
			elm->values = talloc_array(elm, struct ldb_val, elm->num_values);
			
			for (j = 0; j < elm->num_values; j++) {
				elm->values[j] = attr->u.convert.convert_local(privdat, msg, &mo->elements[i].values[j]);
			}

			ldb_msg_add(module->ldb, msg, elm, mo->elements[i].flags);
			break;

		case MAP_KEEP:
			elm = talloc(msg, struct ldb_message_element);

			elm->num_values = mo->elements[i].num_values;
			elm->values = talloc_reference(elm, mo->elements[i].values);
			elm->name = talloc_strdup(elm, mo->elements[i].name);
			
			ldb_msg_add(module->ldb, msg, elm, mo->elements[i].flags);	
			break;

		case MAP_GENERATE:
			attr->u.generate.generate_remote(privdat, attr->local_name, mo, msg);
			break;
		} 
	}

	return msg;
}

/*
  rename a record
*/
static int map_rename(struct ldb_module *module, const struct ldb_dn *olddn, const struct ldb_dn *newdn)
{
	struct ldb_map_context *privdat = &((struct map_private *)module->private_data)->context;
	struct ldb_dn *n_olddn, *n_newdn;
	int ret;
	
	n_olddn = map_local_dn(privdat, module, olddn);
	n_newdn = map_local_dn(privdat, module, newdn);

	ret = ldb_next_rename_record(module, n_olddn, n_newdn);

	talloc_free(n_olddn);
	talloc_free(n_newdn);
	
	return ret;
}

/*
  delete a record
*/
static int map_delete(struct ldb_module *module, const struct ldb_dn *dn)
{
	struct ldb_map_context *privdat = &((struct map_private *)module->private_data)->context;
	struct ldb_dn *newdn;
	int ret;

	newdn = map_local_dn(privdat, module, dn);

	ret = ldb_next_delete_record(module, newdn);

	talloc_free(newdn);

	return ret;
}

/*
  search for matching records using a ldb_parse_tree
*/
static int map_search_bytree(struct ldb_module *module, const struct ldb_dn *base,
			      enum ldb_scope scope, struct ldb_parse_tree *tree,
			      const char * const *attrs, struct ldb_message ***res)
{
	int ret;
	const char **newattrs;
	struct ldb_parse_tree *new_tree;
	struct ldb_dn *new_base;
	struct ldb_message **newres;
	struct ldb_map_context *privdat = &((struct map_private *)module->private_data)->context;
	int i;

	new_tree = ldb_map_parse_tree(module, module, tree);
	newattrs = ldb_map_attrs(module, attrs); 
	new_base = map_local_dn(privdat, module, base);

	ret = ldb_next_search_bytree(module, new_base, scope, new_tree, newattrs, &newres);

	talloc_free(new_base);
	talloc_free(new_tree);
	talloc_free(newattrs);

	*res = talloc_array(module, struct ldb_message *, ret);

	for (i = 0; i < ret; i++) {
		(*res)[i] = ldb_map_message_incoming(module, attrs, newres[i]);
		talloc_free(newres[i]);
	}

	return ret;
}
/*
  search for matching records
*/
static int map_search(struct ldb_module *module, const struct ldb_dn *base,
		       enum ldb_scope scope, const char *expression,
		       const char * const *attrs, struct ldb_message ***res)
{
	struct map_private *map = module->private_data;
	struct ldb_parse_tree *tree;
	int ret;

	tree = ldb_parse_tree(NULL, expression);
	if (tree == NULL) {
		map->last_err_string = "expression parse failed";
		return -1;
	}

	ret = map_search_bytree(module, base, scope, tree, attrs, res);
	talloc_free(tree);
	return ret;
}

/*
  add a record
*/
static int map_add(struct ldb_module *module, const struct ldb_message *msg)
{
	struct ldb_message *nmsg = ldb_map_message_outgoing(module, msg);
	int ret;

	ret = ldb_next_add_record(module, nmsg);

	talloc_free(nmsg);

	return ret;
}


/*
  modify a record
*/
static int map_modify(struct ldb_module *module, const struct ldb_message *msg)
{
	struct ldb_message *nmsg = ldb_map_message_outgoing(module, msg);
	int ret;

	ret = ldb_next_modify_record(module, nmsg);

	talloc_free(nmsg);

	return ret;
}

static int map_lock(struct ldb_module *module, const char *lockname)
{
	return ldb_next_named_lock(module, lockname);
}

static int map_unlock(struct ldb_module *module, const char *lockname)
{
	return ldb_next_named_unlock(module, lockname);
}

/*
  return extended error information
*/
static const char *map_errstring(struct ldb_module *module)
{
	struct map_private *map = module->private_data;
	
	if (map->last_err_string)
		return map->last_err_string;

	return ldb_next_errstring(module);
}

static const struct ldb_module_ops map_ops = {
	.name          = "map",
	.search        = map_search,
	.search_bytree = map_search_bytree,
	.add_record    = map_add,
	.modify_record = map_modify,
	.delete_record = map_delete,
	.rename_record = map_rename,
	.named_lock    = map_lock,
	.named_unlock  = map_unlock,
	.errstring     = map_errstring
};

/* the init function */
struct ldb_module *ldb_map_init(struct ldb_context *ldb, const struct ldb_map_attribute *attrs, const struct ldb_map_objectclass *ocls, const char *options[])
{
	int i, j;
	struct ldb_module *ctx;
	struct map_private *data;

	ctx = talloc(ldb, struct ldb_module);
	if (!ctx)
		return NULL;

	data = talloc(ctx, struct map_private);
	if (!data) {
		talloc_free(ctx);
		return NULL;
	}

	data->last_err_string = NULL;

	/* Get list of attribute maps */
	j = 0;
	data->context.attribute_maps = NULL;

	for (i = 0; attrs[i].local_name; i++) {
		data->context.attribute_maps = talloc_realloc(data, data->context.attribute_maps, struct ldb_map_attribute, j+1);
		data->context.attribute_maps[j] = attrs[i];
		j++;
	}

	for (i = 0; builtin_attribute_maps[i].local_name; i++) {
		data->context.attribute_maps = talloc_realloc(data, data->context.attribute_maps, struct ldb_map_attribute, j+1);
		data->context.attribute_maps[j] = builtin_attribute_maps[i];
		j++;
	}

	data->context.attribute_maps = talloc_realloc(data, data->context.attribute_maps, struct ldb_map_attribute, j+1);
	ZERO_STRUCT(data->context.attribute_maps[j].local_name);

	data->context.objectclass_maps = ocls;
	ctx->private_data = data;
	ctx->ldb = ldb;
	ctx->prev = ctx->next = NULL;
	ctx->ops = &map_ops;

	return ctx;
}

static struct ldb_val map_convert_local_dn(struct ldb_map_context *map, TALLOC_CTX *ctx, const struct ldb_val *val)
{
	struct ldb_dn *dn, *newdn;;
	struct ldb_val *newval;

	dn = ldb_dn_explode(ctx, (char *)val->data);

	newdn = map_local_dn(map, ctx, dn);

	talloc_free(dn);

	newval = talloc(ctx, struct ldb_val);
	newval->data = (uint8_t *)ldb_dn_linearize(ctx, newdn);
	newval->length = strlen((char *)newval->data);

	talloc_free(newdn);

	return *newval;
}

static struct ldb_val map_convert_remote_dn(struct ldb_map_context *map, TALLOC_CTX *ctx, const struct ldb_val *val)
{
	struct ldb_dn *dn, *newdn;;
	struct ldb_val *newval;

	dn = ldb_dn_explode(ctx, (char *)val->data);

	newdn = map_remote_dn(map, ctx, dn);

	talloc_free(dn);

	newval = talloc(ctx, struct ldb_val);
	newval->data = (uint8_t *)ldb_dn_linearize(ctx, newdn);
	newval->length = strlen((char *)newval->data);

	talloc_free(newdn);

	return *newval;
}

static struct ldb_val map_convert_local_objectclass(struct ldb_map_context *map, TALLOC_CTX *ctx, const struct ldb_val *val)
{
	int i;

	for (i = 0; map->objectclass_maps[i].local_name; i++) {
		if (!strcmp(map->objectclass_maps[i].local_name, (char *)val->data)) {
			struct ldb_val newval;
			newval.data = (uint8_t*)talloc_strdup(ctx, map->objectclass_maps[i].remote_name);
			newval.length = strlen((char *)newval.data);

			return ldb_val_dup(ctx, &newval);
		}
	}

	DEBUG(1, ("Unable to map local object class '%s'\n", (char *)val->data));
	return ldb_val_dup(ctx, val); 
}

static struct ldb_val map_convert_remote_objectclass(struct ldb_map_context *map, TALLOC_CTX *ctx, const struct ldb_val *val)
{
	int i;

	for (i = 0; map->objectclass_maps[i].remote_name; i++) {
		if (!strcmp(map->objectclass_maps[i].remote_name, (char *)val->data)) {
			struct ldb_val newval;
			newval.data = (uint8_t*)talloc_strdup(ctx, map->objectclass_maps[i].local_name);
			newval.length = strlen((char *)newval.data);

			return ldb_val_dup(ctx, &newval);
		}
	}

	DEBUG(1, ("Unable to map remote object class '%s'\n", (char *)val->data));
	return ldb_val_dup(ctx, val); 
}

static const struct ldb_map_attribute builtin_attribute_maps[] = {
	{
		.local_name = "dn",
		.type = MAP_CONVERT,
		.u.convert.remote_name = "dn",
		.u.convert.convert_local = map_convert_local_dn,
		.u.convert.convert_remote = map_convert_remote_dn,
	},
	{
		.local_name = "objectclass",
		.type = MAP_CONVERT,
		.u.convert.remote_name = "objectclass",
		.u.convert.convert_local = map_convert_local_objectclass,
		.u.convert.convert_remote = map_convert_remote_objectclass,
	},
	{
		.local_name = NULL,
	}
};