summaryrefslogtreecommitdiffstats
path: root/src/backend.c
blob: 4fda175ee815f30f6527fd0e33f8139b7eab6672 (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
#ifdef HAVE_CONFIG_H
#include "../config.h"
#endif

#include <sys/types.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include <nspr.h>
#include <secport.h>
#include <plarenas.h>
#include <dirsrv/slapi-plugin.h>

#include "backend.h"
#include "plugin.h"
#include "map.h"
#include "format.h"
#include "defaults.h"

/* The data we ask the map cache to keep, for us, for each map. */
struct backend_map_data {
	struct plugin_state *state;
	char *domain, *map, **bases, *filter, *key_attr, *value_fmt;
};

static char **
backend_dup_strlist(char **strlist)
{
	int i, l;
	char **ret, *s;
	/* Handle the NULL case. */
	if (strlist == NULL) {
		return NULL;
	}
	/* Count the amount of space needed for the strings. */
	for (i = 0, l = 0; (strlist[i] != NULL); i++) {
		l += (strlen(strlist[i]) + 1);
	}
	/* No strings = no list. */
	if (i == 0) {
		return NULL;
	}
	/* Allocate space for the array of pointers (with NULL terminator) and
	 * then the string data. */
	ret = malloc((i + 1) * sizeof(char *) + l);
	if (ret != NULL) {
		/* Figure out where the string data will start. */
		s = (char *) &ret[i + 1];
		for (i = 0; (strlist[i] != NULL); i++) {
			/* Set the address of this string, copy the data
			 * around, and then prepare the address of the next
			 * string. */
			ret[i] = s;
			strcpy(s, strlist[i]);
			s += (strlen(s) + 1);
		}
		/* NULL-terminate the array. */
		ret[i] = NULL;
	}
	return ret;
}

static void
backend_free_map_data(void *data)
{
	struct backend_map_data *map_data = data;
	if (map_data != NULL) {
		free(map_data->domain);
		free(map_data->map);
		free(map_data->bases);
		free(map_data->filter);
		free(map_data->key_attr);
		free(map_data->value_fmt);
		free(map_data);
	}
}

static struct backend_map_data *
backend_copy_cb_data(const struct backend_map_data *data)
{
	struct backend_map_data *ret;
	ret = malloc(sizeof(*ret));
	if (ret == NULL) {
		return NULL;
	}
	ret->state = data->state;
	ret->domain = strdup(data->domain);
	ret->map = strdup(data->map);
	ret->bases = backend_dup_strlist(data->bases);
	ret->filter = strdup(data->filter);
	ret->key_attr = strdup(data->key_attr);
	ret->value_fmt = strdup(data->value_fmt);
	if ((ret->domain == NULL) ||
	    (ret->map == NULL) ||
	    (ret->bases == NULL) ||
	    (ret->filter == NULL) ||
	    (ret->key_attr == NULL) ||
	    (ret->value_fmt == NULL)) {
		backend_free_map_data(ret);
		return NULL;
	} return ret;
}

/* Given a map-entry directory entry, determine which keys it should have,
 * determine which value should be associated with those keys, and add them to
 * the map cache. */
static int
backend_map_entry_cb(Slapi_Entry *e, void *callback_data)
{
	struct backend_map_data *data;
	char *key, *value, *dn, **visited_dn_list;
	int i, j;
	data = callback_data;
	/* Pull out the key and value for the entry. */
	visited_dn_list = NULL;
	key = format_get_data(NULL, e, data->key_attr, &visited_dn_list);
	value = format_get_data(NULL, e, data->value_fmt, &visited_dn_list);
	/* Pull out the NDN of this entry. */
	dn = slapi_entry_get_ndn(e);
	/* If we actually generated a value, then set it for all keys. */
	if ((key != NULL) && (value != NULL)) {
		/* For each key, set the value. */
		slapi_log_error(SLAPI_LOG_PLUGIN,
				data->state->plugin_desc->spd_id,
				"setting domain/map/key/value "
				"\"%s\"/\"%s\"/\"%s\"(\"%s\")=\"%s\"\n",
				data->domain, data->map,
				key, dn, value);
		map_data_set_entry(data->state, data->domain, data->map,
				   dn, (const char **) visited_dn_list,
				   -1, key,
				   -1, value);
	} else {
		slapi_log_error(SLAPI_LOG_PLUGIN,
				data->state->plugin_desc->spd_id,
				"no value for %s\n", dn);
		slapi_log_error(SLAPI_LOG_PLUGIN,
				data->state->plugin_desc->spd_id,
				"unsetting domain/map/id"
				"\"%s\"/\"%s\"/(\"%s\")\n",
				data->domain, data->map, dn);
		map_data_unset_entry_id(data->state,
					data->domain, data->map,
					dn);
	}
	format_free_ndn_list(visited_dn_list);
	format_free_data(value);
	format_free_data(key);
	return 0;
}

/*
 * Generate a copy of the filter string, with specific sequences replaced:
 * %d -> name of the domain
 * %m -> name of the map
 * %% -> literal '%'
 */
static char *
backend_map_config_filter(const char *fmt, const char *domain, const char *map)
{
	char *ret;
	int i, j, l;
	l = 0;
	for (i = 0; fmt[i] != '\0'; i++) {
		if (fmt[i] == '%') {
			switch (fmt[i + 1]) {
			case 'd':
				l += strlen(domain);
				i++;
				break;
			case 'm':
				l += strlen(map);
				i++;
				break;
			case '%':
				l++;
				i++;
				break;
			default:
				l++;
				break;
			}
		} else {
			l++;
		}
	}
	ret = malloc(l + 1);
	for (i = j = 0; fmt[i] != '\0'; i++) {
		if (fmt[i] == '%') {
			switch (fmt[i + 1]) {
			case 'd':
				strcpy(ret + j, domain);
				i++;
				j += strlen(domain);
				break;
			case 'm':
				strcpy(ret + j, map);
				i++;
				j += strlen(map);
				break;
			case '%':
				i++;
				ret[j++] = fmt[i];
				break;
			default:
				ret[j++] = fmt[i];
				break;
			}
		} else {
			ret[j++] = fmt[i];
		}
	}
	ret[j] = '\0';
	return ret;
}

/* Given a directory server entry which represents a map's configuration, set
 * up and populate the map. */
static int
backend_map_config_entry(struct plugin_state *state, Slapi_Entry *e,
			 const char *domain, const char *map)
{
	Slapi_PBlock *pb;
	char **base;
	int i;
	char *entry_filter, *use_entry_filter, *key_attribute, *value_attribute;
	char *use_key_attribute, *use_value_attribute;
	char *use_attrs[3]; /* XXX */
	struct backend_map_data cb_data;

	pb = slapi_pblock_new();
	/* Read the NIS entry search base, entry search filter, and the names
	 * of the key and value attributes. */
	base = slapi_entry_attr_get_charray(e, "base");
	entry_filter = slapi_entry_attr_get_charptr(e, "filter");
	key_attribute = slapi_entry_attr_get_charptr(e, "key");
	value_attribute = slapi_entry_attr_get_charptr(e, "value");
	/* Perform substitutions on the filter, so that it can include the map
	 * name without having to explicitly list it.  We need this because
	 * RFC2307bis actually stores the map name in each entry, so it's
	 * useful to be able to filter on it. */
	use_entry_filter = backend_map_config_filter(entry_filter ?
						     entry_filter :
						     DEFAULT_ENTRY_FILTER,
						     domain, map);
	/* If the key and value attributes aren't set, we'll use compiled-in
	 * defaults. */
	use_key_attribute = key_attribute ?
			    key_attribute :
			    DEFAULT_KEY_ATTRIBUTE;
	use_value_attribute = value_attribute ?
			      value_attribute :
			      DEFAULT_VALUE_ATTRIBUTE;
	/* Now get ready to search for entries which need to be in the map. */
	use_attrs[0] = use_key_attribute; /* XXX */
	use_attrs[1] = use_value_attribute; /* XXX */
	use_attrs[2] = NULL; /* XXX */
	cb_data.state = state;
	cb_data.domain = (char *) domain;
	cb_data.map = (char *) map;
	cb_data.key_attr = use_key_attribute;
	cb_data.value_fmt = use_value_attribute;
	cb_data.filter = use_entry_filter;
	cb_data.bases = base;
	slapi_log_error(SLAPI_LOG_PLUGIN,
			state->plugin_desc->spd_id,
			"initializing map %s in %s (2)\n",
			map, domain);
	map_data_set_map(state, domain, map,
			 backend_copy_cb_data(&cb_data),
			 &backend_free_map_data);
	map_data_clear_map(state, domain, map);
	/* Search under each base in turn, adding the matching directory
	 * entries to the NIS maps. */
	for (i = 0; (base != NULL) && (base[i] != NULL); i++) {
		slapi_log_error(SLAPI_LOG_PLUGIN,
				state->plugin_desc->spd_id,
				"searching %s for %s\n",
				base[i], use_entry_filter);
		slapi_search_internal_set_pb(pb,
					     base[i],
					     LDAP_SCOPE_SUB,
					     use_entry_filter,
					     use_attrs, 0,
					     NULL,
					     NULL,
					     state->plugin_identity,
					     0);
		slapi_search_internal_callback_pb(pb, &cb_data,
						  NULL,
						  backend_map_entry_cb,
						  NULL);
		slapi_free_search_results_internal(pb);
	}
	/* Clean up. */
	free(use_entry_filter);
	slapi_ch_free_string(&value_attribute);
	slapi_ch_free_string(&key_attribute);
	slapi_ch_free_string(&entry_filter);
	slapi_ch_array_free(base);
	slapi_pblock_destroy(pb);
	return 0;
}

/* Process a map configuration directory entry.  Pull out the domain and map
 * names which are valid for this configuration and configure such a map for
 * each in turn. */
static int
backend_map_config_entry_cb(Slapi_Entry *e, void *callback_data)
{
	char **domains, **maps;
	int i, j, ret;
	struct plugin_state *state;

	state = callback_data;
	domains = slapi_entry_attr_get_charray(e, "domain");
	maps = slapi_entry_attr_get_charray(e, "map");
	for (i = 0; (domains != NULL) && (domains[i] != NULL); i++) {
		for (j = 0; (maps != NULL) && (maps[j] != NULL); j++) {
			slapi_log_error(SLAPI_LOG_PLUGIN,
					state->plugin_desc->spd_id,
					"initializing map %s in %s\n",
					maps[j], domains[i]);
			ret = backend_map_config_entry(state, e,
						       domains[i],
						       maps[j]);
		}
	}
	slapi_ch_array_free(maps);
	slapi_ch_array_free(domains);
	return 0;
}

/* Scan for the list of configured domains and maps. */
void
backend_startup(struct plugin_state *state)
{
	Slapi_PBlock *pb;
	char *attrs = NULL;
	pb = slapi_pblock_new();
	slapi_log_error(SLAPI_LOG_PLUGIN,
			state->plugin_desc->spd_id,
			"searching \"%s\" for maps\n", state->plugin_base);
	slapi_search_internal_set_pb(pb,
				     state->plugin_base,
				     LDAP_SCOPE_ONE,
				     "(objectclass=*)",
				     NULL, 0,
				     NULL,
				     NULL,
				     state->plugin_identity,
				     0);
	slapi_search_internal_callback_pb(pb, state,
					  NULL,
					  backend_map_config_entry_cb,
					  NULL);
	slapi_free_search_results_internal(pb);
	slapi_pblock_destroy(pb);
}

/* Our postoperation callbacks. */
static PRBool
backend_entry_matches_map(struct backend_map_data *map_data, Slapi_Entry *e)
{
	Slapi_DN *base_sdn;
	const Slapi_DN *entry_sdn;
	Slapi_Filter *filter;
	int i;
	/* Decide if the directory server entry belongs in this map.  That
	 * means that it must be contained by one of the bases of the map. */
	entry_sdn = slapi_sdn_new_ndn_byref(slapi_entry_get_ndn(e));
	if (entry_sdn == NULL) {
		return PR_FALSE;
	} else {
		/* Check each base in turn. */
		for (i = 0;
		     (map_data->bases != NULL) && (map_data->bases[i] != NULL);
		     i++) {
			base_sdn = slapi_sdn_new_dn_byval(map_data->bases[i]);
			if (base_sdn == NULL) {
				return PR_FALSE;
			} else {
				if (slapi_sdn_scope_test(entry_sdn,
							 base_sdn,
							 LDAP_SCOPE_SUB) == 0) {
					/* The entry is not contained by the
					 * base -- go on to try the next one. */
					slapi_sdn_free(&base_sdn);
					continue;
				}
				/* The entry is contained by the base. */
				slapi_sdn_free(&base_sdn);
				break;
			}
		}
		/* If we ran out of bases to check, it doesn't match. */
		if ((map_data->bases == NULL) || (map_data->bases[i] == NULL)) {
			return PR_FALSE;
		}
	}
	/* If it's contained by a search base, compare it to the filter. */
	filter = slapi_str2filter(map_data->filter);
	if (filter == NULL) {
		return PR_FALSE;
	} else {
		if (slapi_filter_test_simple(e, filter) != 0) {
			/* Didn't match -- return. */
			slapi_filter_free(filter, 1);
			return PR_FALSE;
		}
		slapi_filter_free(filter, 1);
	}
	return PR_TRUE;
}

/* Add any map entries which correspond to a directory server entry in this
 * map. */

struct backend_add_entry_cbdata {
	Slapi_PBlock *pb;
	Slapi_Entry *e;
	char *dn;
};

static PRBool
backend_add_entry_cb(const char *domain, const char *map, void *backend_data,
		     void *cbdata_ptr)
{
	Slapi_DN *base_sdn, *entry_sdn;
	Slapi_Filter *filter;
	char **visited_dn_list, *key, *value;
	struct backend_map_data *map_data;
	struct backend_add_entry_cbdata *cbdata;

	map_data = backend_data;
	cbdata = cbdata_ptr;

	/* If the entry doesn't match the map, skip it. */
	if (!backend_entry_matches_map(map_data, cbdata->e)) {
		slapi_log_error(SLAPI_LOG_PLUGIN,
				map_data->state->plugin_desc->spd_id,
				"entry \"%s\" does not belong in "
				"\"%s\"/\"%s\"\n",
				cbdata->dn, domain, map);
		map_data_unset_entry_id(map_data->state,
					map_data->domain, map_data->map,
					cbdata->dn);
		return PR_TRUE;
	}

	/* Pull out the key and value. */
	visited_dn_list = NULL;
	key = format_get_data(cbdata->pb, cbdata->e, map_data->key_attr,
			      &visited_dn_list);
	value = format_get_data(cbdata->pb, cbdata->e, map_data->value_fmt,
				&visited_dn_list);

	/* If we actually generated a value, then set it for all keys. */
	if ((key != NULL) && (value != NULL)) {
		/* For each key, set the value. */
		slapi_log_error(SLAPI_LOG_PLUGIN,
				map_data->state->plugin_desc->spd_id,
				"setting domain/map/key/value "
				"\"%s\"/\"%s\"/\"%s\"(\"%s\")=\"%s\"\n",
				domain, map,
				key, cbdata->dn, value);
		map_data_set_entry(map_data->state, domain, map,
				   cbdata->dn, (const char **) visited_dn_list,
				   -1, key,
				   -1, value);
	} else {
		slapi_log_error(SLAPI_LOG_PLUGIN,
				map_data->state->plugin_desc->spd_id,
				"no value for \"%s\"\n", cbdata->dn);
		slapi_log_error(SLAPI_LOG_PLUGIN,
				map_data->state->plugin_desc->spd_id,
				"unsetting domain/map/id"
				"\"%s\"/\"%s\"/(\"%s\")\n",
				map_data->domain, map_data->map, cbdata->dn);
		map_data_unset_entry_id(map_data->state,
					map_data->domain, map_data->map,
					cbdata->dn);
	}
	format_free_ndn_list(visited_dn_list);
	format_free_data(value);
	format_free_data(key);
	return PR_TRUE;
}

static int
backend_add_cb(Slapi_PBlock *pb)
{
	struct plugin_state *state;
	struct backend_add_entry_cbdata cbdata;

	slapi_pblock_get(pb, SLAPI_PLUGIN_PRIVATE, &state);
	slapi_pblock_get(pb, SLAPI_ADD_TARGET, &cbdata.dn);
	slapi_pblock_get(pb, SLAPI_ADD_ENTRY, &cbdata.e);
	cbdata.pb = pb;
	slapi_log_error(SLAPI_LOG_PLUGIN, state->plugin_desc->spd_id,
			"added \"%s\"\n", cbdata.dn);
	/* Add map entries which corresponded to this directory server
	 * entry. */
	if (!map_data_foreach_map(state, NULL, backend_add_entry_cb, &cbdata)) {
		slapi_log_error(SLAPI_LOG_PLUGIN, state->plugin_desc->spd_id,
				"error adding map entries corresponding to "
				"\"%s\"\n", cbdata.dn);
	}
	/* If it's a map configuration entry, add and populate the map. XXX */
	return 0;
}

struct backend_modify_entry_cbdata {
	Slapi_PBlock *pb;
	LDAPMod **mods;
	Slapi_Entry *e;
	char *dn;
};

static PRBool
backend_modify_entry_cb(const char *domain, const char *map, void *backend_data,
			void *cbdata_ptr)
{
	Slapi_DN *base_sdn, *entry_sdn;
	Slapi_Filter *filter;
	char **visited_dn_list, *key, *value;
	struct backend_map_data *map_data;
	struct backend_modify_entry_cbdata *cbdata;

	map_data = backend_data;
	cbdata = cbdata_ptr;

	/* If the entry doesn't match the map, skip it. */
	if (!backend_entry_matches_map(map_data, cbdata->e)) {
		slapi_log_error(SLAPI_LOG_PLUGIN,
				map_data->state->plugin_desc->spd_id,
				"clearing domain/map/id "
				"\"%s\"/\"%s\"/(\"%s\")\n",
				map_data->domain, map_data->map, cbdata->dn);
		map_data_unset_entry_id(map_data->state,
					map_data->domain, map_data->map,
					cbdata->dn);
	} else {
		/* Pull out the key and value. */
		visited_dn_list = NULL;
		key = format_get_data(cbdata->pb, cbdata->e,
				      map_data->key_attr, &visited_dn_list);
		value = format_get_data(cbdata->pb, cbdata->e,
					map_data->value_fmt, &visited_dn_list);
		/* If we actually generated a value, then set it for all keys.
		 * */
		if ((key != NULL) && (value != NULL)) {
			/* For each key, set the value. */
			slapi_log_error(SLAPI_LOG_PLUGIN,
					map_data->state->plugin_desc->spd_id,
					"setting domain/map/key/value "
					"\"%s\"/\"%s\"/\"%s\"(\"%s\")=\"%s\"\n",
					domain, map,
					key, cbdata->dn, value);
			map_data_set_entry(map_data->state, domain, map,
					   cbdata->dn,
					   (const char **) visited_dn_list,
					   -1, key,
					   -1, value);
		} else {
			if (key == NULL) {
				slapi_log_error(SLAPI_LOG_PLUGIN,
						map_data->state->plugin_desc->spd_id,
						"no key for \"%s\"\n",
						cbdata->dn);
			}
			if (value == NULL) {
				slapi_log_error(SLAPI_LOG_PLUGIN,
						map_data->state->plugin_desc->spd_id,
						"no value for \"%s\"\n",
						cbdata->dn);
			}
			slapi_log_error(SLAPI_LOG_PLUGIN,
					map_data->state->plugin_desc->spd_id,
					"unsetting domain/map/id"
					"\"%s\"/\"%s\"/(\"%s\")\n",
					map_data->domain, map_data->map,
					cbdata->dn);
			map_data_unset_entry_id(map_data->state,
						map_data->domain, map_data->map,
						cbdata->dn);
		}
		format_free_ndn_list(visited_dn_list);
		format_free_data(value);
		format_free_data(key);
	}
	return PR_TRUE;
}

static int
backend_modify_cb(Slapi_PBlock *pb)
{
	struct plugin_state *state;
	struct backend_modify_entry_cbdata cbdata;
	slapi_pblock_get(pb, SLAPI_PLUGIN_PRIVATE, &state);
	slapi_pblock_get(pb, SLAPI_MODIFY_TARGET, &cbdata.dn);
	slapi_pblock_get(pb, SLAPI_MODIFY_MODS, &cbdata.mods);
	slapi_pblock_get(pb, SLAPI_ENTRY_POST_OP, &cbdata.e);
	cbdata.pb = pb;
	slapi_log_error(SLAPI_LOG_PLUGIN, state->plugin_desc->spd_id,
			"modified \"%s\"\n", cbdata.dn);
	/* Modify map entries which corresponded to this directory server
	 * entry. */
	if (!map_data_foreach_map(state, NULL,
				  backend_modify_entry_cb, &cbdata)) {
		slapi_log_error(SLAPI_LOG_PLUGIN, state->plugin_desc->spd_id,
				"error modifying map entries corresponding to "
				"\"%s\"\n", cbdata.dn);
	}
	/* If it's a map configuration entry, reconfigure, clear, and
	 * repopulate the map. XXX */
	return 0;
}

struct backend_modrdn_entry_cbdata {
	Slapi_PBlock *pb;
	Slapi_Entry *e_pre, *e_post;
	char *dn_pre, *dn_post;
};

static PRBool
backend_modrdn_entry_cb(const char *domain, const char *map, void *backend_data,
			void *cbdata_ptr)
{
	Slapi_DN *base_sdn, *entry_sdn;
	Slapi_Filter *filter;
	char **visited_dn_list, *key_pre, *value_pre, *key_post, *value_post;
	struct backend_map_data *map_data;
	struct backend_modrdn_entry_cbdata *cbdata;
	PRBool matched_pre, matched_post;

	map_data = backend_data;
	cbdata = cbdata_ptr;

	matched_pre = backend_entry_matches_map(map_data, cbdata->e_pre);
	matched_post = backend_entry_matches_map(map_data, cbdata->e_post);

	/* Pull out the key and value for the old entry. */
	key_pre = format_get_data(cbdata->pb, cbdata->e_pre,
				  map_data->key_attr, NULL);
	value_pre = format_get_data(cbdata->pb, cbdata->e_pre,
				    map_data->value_fmt, NULL);

	/* Pull out the key and value for the new entry. */
	visited_dn_list = NULL;
	key_post = format_get_data(cbdata->pb, cbdata->e_post,
				   map_data->key_attr, &visited_dn_list);
	value_post = format_get_data(cbdata->pb, cbdata->e_post,
				     map_data->value_fmt, &visited_dn_list);

	/* Now decide what to set, or unset, in this map. */
	if (!matched_post || (key_post == NULL) || (value_post == NULL)) {
		/* If it's no longer a match for the map (it might never have
		 * been), clear the entry. */
		slapi_log_error(SLAPI_LOG_PLUGIN,
				map_data->state->plugin_desc->spd_id,
				"clearing domain/map/id "
				"\"%s\"/\"%s\"/(\"%s\")\n",
				map_data->domain, map_data->map,
				cbdata->dn_pre);
		map_data_unset_entry_id(map_data->state,
					map_data->domain, map_data->map,
					cbdata->dn_pre);
	} else {
		if (!matched_pre || (key_pre == NULL) || (value_pre == NULL)) {
			if ((key_post != NULL) && (value_post != NULL)) {
				/* If it's suddenly become a match, set it. */
				slapi_log_error(SLAPI_LOG_PLUGIN,
						map_data->state->plugin_desc->spd_id,
						"setting domain/map/key/value "
						"\"%s\"/\"%s\"/\"%s\"(\"%s\")="
						"\"%s\"\n",
						domain, map,
						key_post, cbdata->dn_post,
						value_post);
				map_data_set_entry(map_data->state, domain, map,
						   cbdata->dn_post,
						   (const char **) visited_dn_list,
						   -1, key_post,
						   -1, value_post);
			} else {
				/* Not a match then, not a match now. */
				slapi_log_error(SLAPI_LOG_PLUGIN,
						map_data->state->plugin_desc->spd_id,
						"clearing domain/map/id "
						"\"%s\"/\"%s\"/(\"%s\")\n",
						map_data->domain, map_data->map,
						cbdata->dn_pre);
				map_data_unset_entry_id(map_data->state,
							map_data->domain,
							map_data->map,
							cbdata->dn_pre);
				slapi_log_error(SLAPI_LOG_PLUGIN,
						map_data->state->plugin_desc->spd_id,
						"clearing domain/map/id "
						"\"%s\"/\"%s\"/(\"%s\")\n",
						map_data->domain, map_data->map,
						cbdata->dn_post);
				map_data_unset_entry_id(map_data->state,
							map_data->domain,
							map_data->map,
							cbdata->dn_post);
			}
		} else {
			/* Unset the old entry, set the new entry. */
			slapi_log_error(SLAPI_LOG_PLUGIN,
					map_data->state->plugin_desc->spd_id,
					"unsetting domain/map/id "
					"\"%s\"/\"%s\"/(\"%s\")\n",
					map_data->domain, map_data->map,
					cbdata->dn_pre);
			map_data_unset_entry_id(map_data->state,
						map_data->domain,
						map_data->map,
						cbdata->dn_pre);
			slapi_log_error(SLAPI_LOG_PLUGIN,
					map_data->state->plugin_desc->spd_id,
					"setting domain/map/key/value "
					"\"%s\"/\"%s\"/\"%s\"(\"%s\")="
					"\"%s\"\n",
					domain, map,
					key_post, cbdata->dn_post,
					value_post);
			map_data_set_entry(map_data->state, domain, map,
					   cbdata->dn_post,
					   (const char **) visited_dn_list,
					   -1, key_post,
					   -1, value_post);
		}
	}

	format_free_ndn_list(visited_dn_list);
	format_free_data(value_pre);
	format_free_data(key_pre);
	format_free_data(value_post);
	format_free_data(key_post);

	return PR_TRUE;
}

static int
backend_modrdn_cb(Slapi_PBlock *pb)
{
	struct plugin_state *state;
	struct backend_modrdn_entry_cbdata cbdata;
	slapi_pblock_get(pb, SLAPI_PLUGIN_PRIVATE, &state);
	slapi_pblock_get(pb, SLAPI_ENTRY_PRE_OP, &cbdata.e_pre);
	slapi_pblock_get(pb, SLAPI_ENTRY_POST_OP, &cbdata.e_post);
	cbdata.dn_pre = slapi_entry_get_ndn(cbdata.e_pre);
	cbdata.dn_post = slapi_entry_get_ndn(cbdata.e_post);
	cbdata.pb = pb;
	slapi_log_error(SLAPI_LOG_PLUGIN, state->plugin_desc->spd_id,
			"renamed \"%s\" to \"%s\"\n",
			cbdata.dn_pre, cbdata.dn_post);
	/* Modify map entries which corresponded to this directory server
	 * entry. */
	if (!map_data_foreach_map(state, NULL,
				  backend_modrdn_entry_cb, &cbdata)) {
		slapi_log_error(SLAPI_LOG_PLUGIN, state->plugin_desc->spd_id,
				"error renaming map entries corresponding to "
				"\"%s\"\n", cbdata.dn_post);
	}
	/* If it's a map configuration entry, reconfigure, clear, and
	 * repopulate the map. XXX */
	return 0;
}

/* Delete any map entries which correspond to a directory server entry in this
 * map. */

struct backend_delete_entry_cbdata {
	Slapi_PBlock *pb;
	char *dn;
};

static PRBool
backend_delete_entry_cb(const char *domain, const char *map, void *backend_data,
			void *cbdata_ptr)
{
	struct backend_map_data *map_data;
	struct backend_delete_entry_cbdata *cbdata;
	map_data = backend_data;
	cbdata = cbdata_ptr;
	slapi_log_error(SLAPI_LOG_PLUGIN,
			map_data->state->plugin_desc->spd_id,
			"unsetting domain/map/id"
			"\"%s\"/\"%s\"=\"%s\"/\"%s\"/(\"%s\")\n",
			domain, map,
			map_data->domain, map_data->map,
			cbdata->dn);
	map_data_unset_entry_id(map_data->state, domain, map, cbdata->dn);
	return PR_TRUE;
}

/* Called by the server when a directory server entry is deleted. */
static int
backend_delete_cb(Slapi_PBlock *pb)
{
	struct plugin_state *state;
	struct backend_delete_entry_cbdata cbdata;
	slapi_pblock_get(pb, SLAPI_PLUGIN_PRIVATE, &state);
	slapi_pblock_get(pb, SLAPI_DELETE_TARGET, &cbdata.dn);
	cbdata.pb = pb;
	slapi_log_error(SLAPI_LOG_PLUGIN, state->plugin_desc->spd_id,
			"deleted \"%s\"\n", cbdata.dn);
	/* Remove map entries which corresponded to this directory server
	 * entry. */
	if (!map_data_foreach_map(state, NULL,
				  backend_delete_entry_cb, &cbdata)) {
		slapi_log_error(SLAPI_LOG_PLUGIN, state->plugin_desc->spd_id,
				"error removing map entries corresponding to "
				"\"%s\"\n", cbdata.dn);
	}
	/* If it's a map configuration entry, remove the map. XXX */
	return 0;
}

/* Set our post-op callbacks. */
void
backend_init(Slapi_PBlock *pb, struct plugin_state *state)
{
	slapi_log_error(SLAPI_LOG_PLUGIN, state->plugin_desc->spd_id,
			"hooking up postoperation callbacks\n");
	if (slapi_pblock_set(pb, SLAPI_PLUGIN_POST_ADD_FN,
			     backend_add_cb) != 0) {
		slapi_log_error(SLAPI_LOG_PLUGIN, state->plugin_desc->spd_id,
				"error hooking up add callback\n");
	}
	if (slapi_pblock_set(pb, SLAPI_PLUGIN_POST_MODIFY_FN,
			     backend_modify_cb) != 0) {
		slapi_log_error(SLAPI_LOG_PLUGIN, state->plugin_desc->spd_id,
				"error hooking up modify callback\n");
	}
	if (slapi_pblock_set(pb, SLAPI_PLUGIN_POST_MODRDN_FN,
			     backend_modrdn_cb) != 0) {
		slapi_log_error(SLAPI_LOG_PLUGIN, state->plugin_desc->spd_id,
				"error hooking up modrdn callback\n");
	}
	if (slapi_pblock_set(pb, SLAPI_PLUGIN_POST_DELETE_FN,
			     backend_delete_cb) != 0) {
		slapi_log_error(SLAPI_LOG_PLUGIN, state->plugin_desc->spd_id,
				"error hooking up delete callback\n");
	}
}

void *
xmemdup(char *region, int size)
{
	char *ret;
	ret = malloc(size + 1);
	if (ret != NULL) {
		memcpy(ret, region, size);
		ret[size] = '\0';
	}
	return ret;
}