summaryrefslogtreecommitdiffstats
path: root/ldap/servers/slapd/mapping_tree.c
blob: 0fbfb555c7d04ef12bf8cb3a9d21819e20e0f98f (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
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
/** BEGIN COPYRIGHT BLOCK
 * This Program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation; version 2 of the License.
 * 
 * This Program 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 General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA 02111-1307 USA.
 * 
 * In addition, as a special exception, Red Hat, Inc. gives You the additional
 * right to link the code of this Program with code not covered under the GNU
 * General Public License ("Non-GPL Code") and to distribute linked combinations
 * including the two, subject to the limitations in this paragraph. Non-GPL Code
 * permitted under this exception must only link to the code of this Program
 * through those well defined interfaces identified in the file named EXCEPTION
 * found in the source code files (the "Approved Interfaces"). The files of
 * Non-GPL Code may instantiate templates or use macros or inline functions from
 * the Approved Interfaces without causing the resulting work to be covered by
 * the GNU General Public License. Only Red Hat, Inc. may make changes or
 * additions to the list of Approved Interfaces. You must obey the GNU General
 * Public License in all respects for all of the Program code and other code used
 * in conjunction with the Program except the Non-GPL Code covered by this
 * exception. If you modify this file, you may extend this exception to your
 * version of the file, but you are not obligated to do so. If you do not wish to
 * provide this exception without modification, you must delete this exception
 * statement from your version and license this file solely under the GPL without
 * exception. 
 * 
 * 
 * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
 * Copyright (C) 2005 Red Hat, Inc.
 * All rights reserved.
 * END COPYRIGHT BLOCK **/

#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

/* mapping_tree.c - Maps the DIT onto backends and/or referrals. */


#include "slap.h"

/* distribution plugin prototype */
typedef int (* mtn_distrib_fct) (Slapi_PBlock *pb, Slapi_DN * target_dn,
         char **mtn_be_names, int be_count, Slapi_DN * mtn_node_dn, int *mtn_be_states);

struct mt_node
{
    Slapi_DN *mtn_subtree;      /* dn for the node's subtree */
    Slapi_Backend **mtn_be;     /* backend pointer - the list of backends implementing this 
                                 * node usually there is only one back-end here, there can
                                 * be several only when distribution is done on this node
                                 */
    int *mtn_be_states;            /* states for the backends in table mtn_be */
    char **mtn_backend_names;   /* list of backend names */
    int mtn_be_list_size;        /* size of the previous three tables */
    int mtn_be_count;                /* number of backends implementing this node */
    char **mtn_referral;            /* referral or list of referrals */
    Slapi_Entry *mtn_referral_entry;            /* referral or list of referrals */
    struct mt_node *mtn_children; 
    struct mt_node *mtn_parent;
    struct mt_node *mtn_brother;  /* list of other nodes with the same parent */
    int mtn_state;
    int mtn_private;              /* Never show this node to the user. only used for 
                                   * cn=config, cn=schema and root node */
    char * mtn_dstr_plg_lib;      /* distribution plugin library name */
    char * mtn_dstr_plg_name;      /* distribution plugin function name */
    mtn_distrib_fct mtn_dstr_plg;          /* pointer to the actual ditribution function */
    void *mtn_extension;          /* plugins can extend a mapping tree node */
};

#define BE_LIST_INIT_SIZE 10
#define BE_LIST_INCREMENT 10

/* Mapping tree locking strategy
 *
 * There are two goals for the mapping tree locking 
 * - protect the mapping tree structures 
 * - prevent stop or re-initialisation of a backend which is currently
 *   used by an LDAP operation
 * This must be done without preventing parallelisation of LDAP operations
 *
 * we use
 * - one RW lock will be used to protect access to mapping tree structures 
 *   accessed throuh mtn_lock(), mtn_unlock(), mtn_wlock()
 * - one RW lock for each backend. This lock is taken in READ for each
 *   LDAP operation in progree on the backend
 *   and is taken in write for administrative operations like stop or 
 *   disable the backend
 *   accessed through slapi_be_Rlock(), slapi_be_Wlock(), slapi_be_Unlock()
 * - a state flag for each backend : mtn_be_states[]
 *   this state is set to SLAPI_BE_STATE_DELETE or SLAPI_BE_STATE_OFFLINE at the beginning
 *   of stop/disable operation to ensure that no new operation starts 
 *   while the backend is stopped/disabled
 *
 * The algorithme for LDAP OPERATIONS is :
 *
 *      lock mapping tree in read mode 
 *      get backend    
 *      check that backend is not in SLAPI_BE_STATE_DELETE or SLAPI_BE_STATE_OFFLINE state
 *      lock backend in read mode
 *      unlock mapping tree 
 *      do LDAP operation
 *      release backend lock
 *
 * The algorithme for maintenance operation is 
 *      lock mapping tree in write mode
 *      set state to SLAPI_BE_STATE_DELETE or SLAPI_BE_STATE_OFFLINE
 *      unlock mapping tree
 *      get backend lock in write mode
 *      release backend lock 
 *
 */
static PRRWLock    *myLock;    /* global lock on the mapping tree structures */


static mapping_tree_node *mapping_tree_root = NULL;
static int mapping_tree_inited = 0;
static int mapping_tree_freed = 0;
static int extension_type = -1;    /* type returned from the factory */

/* The different states a mapping tree node can be in. */
#define    MTN_DISABLED                 0    /* The server acts like the node isn't there. */
#define MTN_BACKEND                  1    /* This node represents a backend. */
#define MTN_REFERRAL                 2    /* A referral is returned instead of a backend. */
#define MTN_REFERRAL_ON_UPDATE         3    /* A referral is returned for update operations. */
#define MTN_CONTAINER                 4    /* This node represents a container for backends. */ 

/* Need to add a modifier flag to the state - such as round robin */

#define MAPPING_TREE_BASE_DN "cn=mapping tree,cn=config"
#define MAPPING_TREE_PARENT_ATTRIBUTE "nsslapd-parent-suffix"

void mtn_wlock();
void mtn_lock();
void mtn_unlock();

static mapping_tree_node * mtn_get_mapping_tree_node_by_entry(
    mapping_tree_node* node, const Slapi_DN *dn);
static void mtn_remove_node(mapping_tree_node * node);
static void mtn_free_node (mapping_tree_node **node);
static int mtn_get_be_distributed(Slapi_PBlock *pb,
    mapping_tree_node * target_node, Slapi_DN *target_sdn, int * flag_stop);
static int mtn_get_be(mapping_tree_node *target_node, Slapi_PBlock *pb,
    Slapi_Backend **be, int * index, Slapi_Entry **referral, char *errorbuf);
static mapping_tree_node * mtn_get_next_node(mapping_tree_node * node,
    mapping_tree_node * node_list, int scope);
static mapping_tree_node * mtn_get_first_node(mapping_tree_node * node,
    int scope);
static mapping_tree_node *
    get_mapping_tree_node_by_name(mapping_tree_node * node, char * be_name);

#ifdef DEBUG
static void dump_mapping_tree(mapping_tree_node *parent, int depth);
#endif

/* structure and static local variable used to store the
 * list of plugins that have registered to a callback when backend state
 * change
 */
struct mtn_be_ch_list { 
    void * handle;
    struct mtn_be_ch_list * next;
    slapi_backend_state_change_fnptr fnct;
    };

static struct mtn_be_ch_list * mtn_plug_list = NULL;

/* API for registering to a callback when backend state change */
void slapi_register_backend_state_change(void * handle, slapi_backend_state_change_fnptr funct)
{
     struct mtn_be_ch_list * new_be_ch_plg;
     new_be_ch_plg = (struct mtn_be_ch_list *)
                         slapi_ch_malloc(sizeof(struct mtn_be_ch_list));
     new_be_ch_plg->next = mtn_plug_list;
     new_be_ch_plg->handle = handle;
     new_be_ch_plg->fnct = funct;
     mtn_plug_list = new_be_ch_plg;
}

/* To unregister all the state change callbacks registered on the mapping tree */
int slapi_unregister_backend_state_change_all()
{
    struct mtn_be_ch_list *cur_be_ch_plg;
    while (mtn_plug_list)
    {
        cur_be_ch_plg = mtn_plug_list;
        mtn_plug_list = mtn_plug_list->next;
        slapi_ch_free((void **) &cur_be_ch_plg);
    }
    return 1;
}


int slapi_unregister_backend_state_change(void * handle)
{
    struct mtn_be_ch_list * cur_be_ch_plg = mtn_plug_list;
    struct mtn_be_ch_list * prev_be_ch_plg = mtn_plug_list;
    while (cur_be_ch_plg)
    {
        if (cur_be_ch_plg->handle == handle)
        {
            if (cur_be_ch_plg == mtn_plug_list)
            {
                mtn_plug_list = mtn_plug_list->next;
                slapi_ch_free((void **) &cur_be_ch_plg);
                return 0;
            }
            else
            {
                prev_be_ch_plg->next = cur_be_ch_plg->next;
                slapi_ch_free((void **) &cur_be_ch_plg);
                return 0;
            }
        }
        prev_be_ch_plg = cur_be_ch_plg;
        cur_be_ch_plg = cur_be_ch_plg->next;
    }
    return 1;
}

void mtn_be_state_change(char * be_name, int old_state, int new_state)
{
    struct mtn_be_ch_list * cur_be_ch_plg = mtn_plug_list;

    while (cur_be_ch_plg)
    {
        (* (cur_be_ch_plg->fnct))(cur_be_ch_plg->handle, be_name,
             old_state, new_state);
        cur_be_ch_plg = cur_be_ch_plg->next;
    }
}


Slapi_DN* slapi_mtn_get_dn(mapping_tree_node *node)
{
    return (node->mtn_subtree);
}

/* this will turn an array of url into a referral entry */
static Slapi_Entry *
referral2entry(char ** url_array, const char *target)
{
    int i;
    struct berval bv0,bv1,*bvals[3];
    Slapi_Entry *anEntry;

    if (url_array == NULL)
        return NULL;
        
    anEntry = slapi_entry_alloc();
    slapi_entry_set_dn(anEntry,slapi_ch_strdup(target));

    bvals[2]=NULL;
    bvals[1]=&bv1;
    bv1.bv_val="referral";
    bv1.bv_len=strlen(bv1.bv_val);
    bvals[0]=&bv0;
    bv0.bv_val="top";
    bv0.bv_len=strlen(bv0.bv_val);
    slapi_entry_add_values( anEntry, "objectClass", bvals);

    bvals[1]=NULL;
    for (i=0; url_array[i]; i++) {
        bv0.bv_val=url_array[i];
        bv0.bv_len=strlen(bv0.bv_val);
        slapi_entry_attr_merge( anEntry, "ref", bvals);
    }
    return anEntry;
}


/* mapping tree node extension */
int
mapping_tree_get_extension_type ()
{
    if(extension_type==-1)
    {
        /* The factory is given the name of the object type, in
         * return for a type handle. Whenever the object is created
         * or destroyed the factory is called with the handle so
         * that it may call the constructors or destructors registered
         * with it.
         */
        extension_type= factory_register_type(SLAPI_EXT_MTNODE, 
                                              offsetof(mapping_tree_node, mtn_extension));
    }
    return extension_type;
}

static mapping_tree_node *
mapping_tree_node_new(Slapi_DN *dn, Slapi_Backend **be, char **backend_names, int *be_states,
             int count, int size,
             char **referral, mapping_tree_node *parent,
             int state, int private, char *plg_lib, char *plg_fct,
             mtn_distrib_fct plg)
{
    Slapi_RDN rdn;
    mapping_tree_node *node;
    node = (mapping_tree_node *) slapi_ch_calloc(1, sizeof(mapping_tree_node));
    node->mtn_subtree = dn;
    node->mtn_be = be;
    node->mtn_be_states = be_states;
    node->mtn_backend_names = backend_names;
    node->mtn_referral = referral;
    node->mtn_referral_entry = referral2entry(referral, slapi_sdn_get_dn(dn)) ;
    node->mtn_parent = parent;
    node->mtn_children = NULL;
    node->mtn_brother = NULL;
    node->mtn_state = state;
    node->mtn_private = private;
    node->mtn_be_list_size = size;
    node->mtn_be_count = count;
    /* We use this count of the rdn components in the mapping tree to help
     * when selecting a mapping tree node for a dn. */
    slapi_rdn_init_sdn(&rdn,dn);
    slapi_rdn_done(&rdn);
    node->mtn_dstr_plg_lib = plg_lib;
    node->mtn_dstr_plg_name = plg_fct;
    node->mtn_dstr_plg = plg;

    slapi_log_error(SLAPI_LOG_TRACE, "mapping_tree",
                    "Created new mapping tree node for suffix [%s] backend [%s] [%p]\n",
                    slapi_sdn_get_dn(dn),
                    backend_names && backend_names[0] ? backend_names[0] : "null",
                    be ? be[0] : NULL);

    return node;
}

/* 
 * Description:
 * Adds a mapping tree node to the child list of another mapping tree node.
 * 
 * Arguments:
 * parent and child are pointers to mapping tree nodes.  child will be added
 * to parent's child list.  For now, the child is added to the head of the 
 * linked list.  Later we may come up a way to ordering the entries in the
 * list.
 *
 * Returns:
 * nothing
 */
static void 
mapping_tree_node_add_child(mapping_tree_node *parent, mapping_tree_node* child)
{
    /* WARNING:
     * As for now the mapping tree is not locked when a child is added 
     * this is possible only because the child is added into the mapping
     * the structure by a single operation after being fully initialized
     * should this be changed, the lock policy would have to be checked
     * see mapping_tree_entry_add_callback() 
     */
    child->mtn_brother = parent->mtn_children;
    parent->mtn_children = child;
    /* for debugging: dump_mapping_tree(mapping_tree_root, 0); */
}

static Slapi_DN *
get_parent_from_entry(Slapi_Entry * entry)
{
    Slapi_Attr *attr = NULL;
    char * parent;
    Slapi_Value *val = NULL;
    Slapi_DN *parent_sdn;

    if (slapi_entry_attr_find(entry, MAPPING_TREE_PARENT_ATTRIBUTE, &attr))
        return NULL;

    slapi_attr_first_value(attr, &val);

    parent = slapi_ch_strdup(slapi_value_get_string(val));
    if(parent[0]=='\"')
    {
        parent[strlen(parent) - 1] = '\0';
        parent_sdn = slapi_sdn_new_dn_byval(parent+1);
    }
    else
        parent_sdn = slapi_sdn_new_dn_byval(parent);
    slapi_ch_free((void **) &parent);
    
    return parent_sdn;
}

/* extract the subtree managed by a mapping tree entry from the entry
 */
static Slapi_DN *
get_subtree_from_entry(Slapi_Entry * entry)
{
    Slapi_Attr *attr = NULL;
    char * cn;
    Slapi_Value *val = NULL;
    Slapi_DN *subtree;

    if (slapi_entry_attr_find(entry, "cn", &attr))
        return NULL;

    /* should check that there is only one value for cn attribute */
    slapi_attr_first_value(attr, &val);

    /* The value of cn is the dn of the subtree for this node. 
     * There is a slight problem though.  The cn value is 
     * quoted.  We have to remove the quotes here.  I'm sure
     * there is a proper way to do this, but for now we'll
     * just assume that the first and last chars are ".  Later
     * we'll have to revisit this because things could be a 
     * lot more complicated.  Especially if there are quotes 
     * in the dn of the subtree root dn! */
    /* JCM - Need to dequote correctly. */
    /* GB : I think removing the first and last " in the cn value
     * is the right stuff to do
     */
    cn = slapi_ch_strdup(slapi_value_get_string(val));
    if(cn[0]=='\"')
    {
        cn[strlen(cn) - 1] = '\0';
        subtree = slapi_sdn_new_dn_byval(cn+1);
    }
    else
        subtree = slapi_sdn_new_dn_byval(cn);
    slapi_ch_free((void **) &cn);
    
    return subtree;
}

static int 
mtn_state_to_int(const char * state_string, Slapi_Entry *entry)
{
    if (!strcasecmp(state_string, "disabled")) {
        return MTN_DISABLED;
    } else if (!strcasecmp(state_string, "backend")) {
        return MTN_BACKEND;
    } else if (!strcasecmp(state_string, "referral")) {
        return MTN_REFERRAL;
    } else if (!strcasecmp(state_string, "referral on update")) {
        return MTN_REFERRAL_ON_UPDATE;
    } else if (!strcasecmp(state_string, "container")) {
        return MTN_CONTAINER;
    } else {
        LDAPDebug(LDAP_DEBUG_ANY,
                "Warning: Unknown state, %s, for mapping tree node %s."
                " Defaulting to DISABLED\n",
                state_string, slapi_entry_get_dn(entry), 0);
        return MTN_DISABLED;
    }
}

static char **
mtn_get_referral_from_entry(Slapi_Entry * entry)
{
    int nb;
    int hint;
    char ** referral;
    Slapi_Attr * attr;
    Slapi_Value *val = NULL;

    if (slapi_entry_attr_find(entry, "nsslapd-referral", &attr))
        return NULL;

    slapi_attr_get_numvalues(attr, &nb);
    referral = (char **) slapi_ch_malloc(sizeof(char *) * (nb+1));
    hint = slapi_attr_first_value(attr, &val);
    if (NULL == val) {
        LDAPDebug(LDAP_DEBUG_ANY, "Warning: The nsslapd-referral attribute has no value for the mapping tree node %s\n", slapi_entry_get_dn(entry), 0, 0);
    return NULL;
    }

    nb = 0;
    while (val)
    {
        referral[nb++] = slapi_ch_strdup(slapi_value_get_string(val));
        hint = slapi_attr_next_value(attr, hint, &val);
    }
    referral[nb] = NULL;

    return referral;
}
            
static int
get_backends_from_attr(Slapi_Attr *attr, backend ***be_list, char ***be_names,
    int ** be_states, int *be_list_count, int *be_list_size,
    mapping_tree_node * node)
{
    Slapi_Value *val = NULL;
    backend *tmp_be = NULL;
    char * tmp_backend_name;
    int hint;
    mapping_tree_node* new_node;

    *be_list_size = BE_LIST_INIT_SIZE;
    *be_list_count = 0;

    *be_list = (backend **) slapi_ch_malloc(sizeof(backend *) * BE_LIST_INIT_SIZE);
    *be_names = (char **) slapi_ch_malloc(sizeof(char *) * BE_LIST_INIT_SIZE);
    *be_states = (int *) slapi_ch_malloc(sizeof(int) * BE_LIST_INIT_SIZE);

    hint = slapi_attr_first_value(attr, &val);
    if (NULL == val) {
        slapi_ch_free ((void **)be_list);
        *be_list = NULL;
        return 0;
    }

    while (val)
    {
        tmp_backend_name = (char *) slapi_ch_strdup(slapi_value_get_string(val));
           if (*be_list_count >= *be_list_size)
        {
            (*be_list_size) += BE_LIST_INCREMENT;
            *be_names = (char **) slapi_ch_realloc((char *) (*be_names),
                                     sizeof(char *) * (*be_list_size));
            *be_list = (backend **) slapi_ch_realloc((char *) (*be_list),
                                     sizeof(backend *) * (*be_list_size));
            *be_states = (int  *) slapi_ch_realloc((char *) (*be_states),
                                     sizeof(int) * (*be_list_size));
        }
        (*be_names)[*be_list_count] = tmp_backend_name;

        /* set backend as started by default */
        (*be_states)[*be_list_count] = SLAPI_BE_STATE_ON;

        /* We now need to find the backend with name backend_name. */
        tmp_be= slapi_be_select_by_instance_name(tmp_backend_name);
        new_node = get_mapping_tree_node_by_name(mapping_tree_root,
            tmp_backend_name);
        if (new_node && (new_node != node))
        {
                   LDAPDebug(LDAP_DEBUG_ANY,
                   "ERROR: backend %s is already pointed to by a mapping tree"
                " node.  Only one mapping tree node can point to a backend\n",
                   tmp_backend_name, 0, 0);
                tmp_be = NULL;
                return -1;
        }
        if(tmp_be!=NULL)
        {
            tmp_be->be_mapped = 1;
            (*be_list)[*be_list_count] = tmp_be;
        }
        else
           {
            /* It's just not here yet. That's OK. We'll fix it up at runtime. */
            (*be_list)[*be_list_count] = NULL;
        }
        (*be_list_count) ++;
           hint = slapi_attr_next_value(attr, hint, &val);
    }

    return 0;
}

/* 
 * Description:
 * Release the memory allocated by the routine above.
 * Call this when the backend not put into structure and need to cleanup these tmp allocations
 */
static void
free_get_backends_from_attr(backend ***be_list, char ***be_names,
    int ** be_states, int *be_list_count)
{
    int i;

    /* sanity check */
    PR_ASSERT (be_list != NULL && be_names != NULL && be_states != NULL && be_list_count != NULL);

    if (*be_names != NULL) for (i = 0; i < *be_list_count; ++i) {
        slapi_ch_free ((void **)&((*be_names)[i]));
    }
    slapi_ch_free ((void **)be_names);
    slapi_ch_free ((void **)be_list);
    slapi_ch_free ((void **)be_states);
    *be_list_count = 0;
}

/* 
 * Description:
 * Takes an entry and creates a mapping tree node from it.  Loops through the
 * attributes, pulling needed info from them.  Right now, each node can only
 * have one backend and one referral.  Once we move to supporting more than
 * one node and more than one referral, this function will need to be 
 * massaged a little.
 * 
 * We should make a objectclass for a mapping tree node entry.  That way
 * schema checking would make this function more robust.
 *
 * Arguments:
 * A mapping tree node entry read in from the DIT.
 *
 * Returns:
 * An LDAP result code (LDAP_SUCCESS if all goes well).
 * If the return value is LDAP_SUCCESS, *newnodep is set to the new mapping
 * tree node (guaranteed to be non-NULL).
 */
static int
mapping_tree_entry_add(Slapi_Entry *entry, mapping_tree_node **newnodep )
{
    Slapi_DN *subtree = NULL;
    const char *tmp_ndn;
    int be_list_count = 0;
    int be_list_size;
    backend **be_list = NULL;
    char **be_names = NULL;
    int * be_states = NULL;
    char * plugin_funct = NULL;
    char * plugin_lib = NULL;
    mtn_distrib_fct plugin = NULL;
    
    char **referral = NULL;
    int state = MTN_DISABLED;
    Slapi_Attr *attr = NULL;
    mapping_tree_node *node = NULL;
    mapping_tree_node *parent_node = mapping_tree_root;
    int rc;
    int lderr = LDAP_UNWILLING_TO_PERFORM;    /* our default result code */
    char *tmp_backend_name;
    Slapi_Backend *be;

    PR_ASSERT(newnodep != NULL);
    *newnodep = NULL;

    subtree = get_subtree_from_entry(entry);
    /* Make sure we know the root dn of the subtree for this node. */
    if (NULL == subtree) {
        LDAPDebug(LDAP_DEBUG_ANY, "Warning: Unable to determine the subtree represented by the mapping tree node %s\n",
            slapi_entry_get_dn(entry), 0, 0);
        return lderr;
    }
    
    tmp_ndn = slapi_sdn_get_ndn( subtree );
    if (tmp_ndn && ( '\0' == *tmp_ndn)) {
     
       /* This entry is associated with the "" subtree.  Treat this is
        * a special case (no parent; will replace the internal root
        * node (mapping_tree_root) with data from this entry).
        */
        slapi_log_error( SLAPI_LOG_ARGS, "mapping_tree_entry_add", "NULL suffix\n" );
        parent_node = NULL;
    }


    /* Make sure a node does not already exist for this subtree */
    if (  parent_node != NULL && NULL != mtn_get_mapping_tree_node_by_entry(mapping_tree_root,
                subtree)) {
        LDAPDebug(LDAP_DEBUG_ANY, "Warning: a mapping tree node for the"
            " subtree %s already exists; unable to add the node %s\n",
            slapi_sdn_get_dn(subtree), slapi_entry_get_dn(entry), 0);
        slapi_sdn_free(&subtree);
        return LDAP_ALREADY_EXISTS;
    }

    /* Loop through the attributes and handle the ones we care about. */
    for (rc = slapi_entry_first_attr(entry, &attr);
        !rc && attr;
        rc = slapi_entry_next_attr(entry, attr, &attr)) {
        
        char *type = NULL;
        Slapi_Value *val = NULL;

        slapi_attr_get_type(attr, &type);
        if (NULL == type) {
            /* strange... I wonder if we should give a warning here? */
            continue;
        }

        if (!strcasecmp(type, "nsslapd-backend")) {

            if (get_backends_from_attr(attr, &be_list, &be_names, &be_states,
                                       &be_list_count, &be_list_size, NULL)) {
                free_get_backends_from_attr(&be_list, &be_names, &be_states, &be_list_count);
                slapi_sdn_free(&subtree);
                return lderr;
            }

            if (NULL == be_list)
            {
                LDAPDebug(LDAP_DEBUG_ANY,
                "Warning: The nsslapd-backend attribute has no value for the mapping tree node %s\n",
                    slapi_entry_get_dn(entry), 0, 0);
                continue;
            }

        } else if (!strcasecmp(type, "nsslapd-referral")) {
            referral = mtn_get_referral_from_entry(entry);

        } else if (!strcasecmp(type, "nsslapd-state")) {
            const char *state_string;
            
            slapi_attr_first_value(attr, &val);
            if (NULL == val) {
                LDAPDebug(LDAP_DEBUG_ANY, "Warning: Can't determine the state of the mapping tree node %s\n", 
                    slapi_entry_get_dn(entry), 0, 0);
                continue;
            }
            /* Convert the string representation for the state to an int */
            state_string = slapi_value_get_string(val);
            state = mtn_state_to_int(state_string, entry);

        } else if (!strcasecmp(type, "nsslapd-distribution-plugin")) {
            slapi_attr_first_value(attr, &val);
            if (NULL == val) {
                LDAPDebug(LDAP_DEBUG_ANY, "Warning: The nsslapd-distribution-plugin attribute has no value for the mapping tree node %s\n",
                    slapi_entry_get_dn(entry), 0, 0);
                continue;
            }
            plugin_lib = slapi_ch_strdup(slapi_value_get_string(val));
        } else if (!strcasecmp(type, "nsslapd-distribution-funct")) {
            slapi_attr_first_value(attr, &val);
            if (NULL == val) {
                LDAPDebug(LDAP_DEBUG_ANY, "Warning: The nsslapd-distribution-plugin attribute has no value for the mapping tree node %s\n",
                    slapi_entry_get_dn(entry), 0, 0);
                continue;
            }
            plugin_funct = slapi_ch_strdup(slapi_value_get_string(val));
        } else if (!strcasecmp(type, MAPPING_TREE_PARENT_ATTRIBUTE)) {
            Slapi_DN *parent_node_dn = get_parent_from_entry(entry);
            parent_node = mtn_get_mapping_tree_node_by_entry(
                mapping_tree_root, parent_node_dn);

            if (parent_node == NULL)
            {
                parent_node = mapping_tree_root;
                LDAPDebug(LDAP_DEBUG_ANY,
                    "Warning: could not find parent for %s defaulting to root\n",
                     slapi_entry_get_dn(entry), 0, 0);
            }
            slapi_sdn_free(&parent_node_dn);
        }
    }

    if (state == MTN_CONTAINER)
    {
        /* this can be extended later to include the general 
           null suffix, */     
        /* The "default" backend is used by the container node */
        be = defbackend_get_backend();
        if(be == NULL)
        {
             LDAPDebug(LDAP_DEBUG_ANY,
                        "ERROR: default container has not been created for the NULL SUFFIX node.\n",
                         0, 0, 0);
             return -1;
        }

        be_list_size = 1;
        be_list_count = 0;

        be_list = (backend **) slapi_ch_calloc(1, sizeof(backend *) );
        be_names = (char **) slapi_ch_calloc(1, sizeof(char *) );
        be_states = (int *) slapi_ch_calloc(1, sizeof(int) );

        tmp_backend_name = (char *) slapi_ch_strdup("default"); /* "NULL_CONTAINER" */
        (be_names)[be_list_count] = tmp_backend_name;

        /* set backend as started by default */
        (be_states)[be_list_count] = SLAPI_BE_STATE_ON;

        be->be_mapped = 1;
        (be_list)[be_list_count] = be; 
        be_list_count++;

    }
    /* check that all required attributes for the givene state are there :
     * state backend -> need nsslapd-backend attribute
     * state referral or referral on update  -> need nsslapd-referral attribute
     */
    
    if (((state == MTN_BACKEND) || (state == MTN_REFERRAL_ON_UPDATE))
         && (be_names == NULL))
    {
        LDAPDebug(LDAP_DEBUG_ANY,
        "ERROR: node %s must define a backend\n",
        slapi_entry_get_dn(entry), 0, 0);
        slapi_sdn_free(&subtree);
        free_get_backends_from_attr(&be_list, &be_names, &be_states, &be_list_count);
        return lderr;
    }
    if (((state == MTN_REFERRAL) || (state == MTN_REFERRAL_ON_UPDATE))
        && (referral == NULL))
    {
        LDAPDebug(LDAP_DEBUG_ANY,
        "ERROR: node %s must define referrals to be in referral state\n",
        slapi_entry_get_dn(entry), 0, 0);
        slapi_sdn_free(&subtree);
        free_get_backends_from_attr(&be_list, &be_names, &be_states, &be_list_count);
        return lderr;
    }

    if (plugin_lib && plugin_funct)
    {
        plugin = (mtn_distrib_fct)sym_load(plugin_lib, plugin_funct,
                                           "Entry Distribution", 1);
        if (plugin == NULL)
        {
            LDAPDebug(LDAP_DEBUG_ANY,
                "ERROR: node %s cannot find distribution plugin. "
                SLAPI_COMPONENT_NAME_NSPR " %d (%s)\n",
                slapi_entry_get_dn(entry), PR_GetError(), slapd_pr_strerror(PR_GetError()));
                slapi_sdn_free(&subtree);
            slapi_ch_free((void **) &plugin_funct);
            slapi_ch_free((void **) &plugin_lib);
            free_get_backends_from_attr(&be_list, &be_names, &be_states, &be_list_count);
            return lderr;
        }
    }
    else if ((plugin_lib == NULL) && (plugin_funct == NULL))
    {
        /* nothing configured -> OK */
        plugin = NULL;
    }
    else
    {
        /* only one parameter configured -> ERROR */
        LDAPDebug(LDAP_DEBUG_ANY,
            "ERROR: node %s must define both lib and funct"
            " for distribution plugin\n",
            slapi_entry_get_dn(entry), 0, 0);
        slapi_sdn_free(&subtree);
        slapi_ch_free((void **) &plugin_funct);
        slapi_ch_free((void **) &plugin_lib);
        free_get_backends_from_attr(&be_list, &be_names, &be_states, &be_list_count);
        return lderr;
    }
        
    /* Now we can create the node for this mapping tree entry. */
    node= mapping_tree_node_new(subtree, be_list, be_names, be_states, be_list_count,
             be_list_size, referral, parent_node, state,
             0 /* Normal node. People can see and change it. */,
             plugin_lib, plugin_funct, plugin);

    tmp_ndn = slapi_sdn_get_ndn( subtree );
    if ( NULL != node && NULL == parent_node && tmp_ndn 
                                && ('\0' == *tmp_ndn )) {
        /* The new node is actually the "" node.  Replace the root
         * node with this new one by copying all information (we can't
         * free the root node completely because children of the root
         * node hold pointers to it in their mtn_parent field).
         */

        slapi_log_error( SLAPI_LOG_ARGS, "mapping_tree_entry_add", "fix up NULL suffix\n" );

        node->mtn_children = mapping_tree_root->mtn_children;
        node->mtn_brother = mapping_tree_root->mtn_brother;
        *mapping_tree_root = *node;     /* struct copy */
        slapi_ch_free( (void **)&node );
        node = mapping_tree_root;
    }


    if ( NULL != node ) {
        lderr = LDAP_SUCCESS;
        *newnodep = node;
    }

    return lderr;
}

/*
 * Recursive procedure used to create node extensions once the mapping tree
 * is fully initialized
 * This is done after full init of the mapping tree so that the extensions can do
 * searches
 */
void mtn_create_extension(mapping_tree_node *node)
{
    if (node == NULL)
        return;

    node->mtn_extension = factory_create_extension(mapping_tree_get_extension_type(),
                        node, NULL /* parent */);

    mtn_create_extension(node->mtn_children);
    mtn_create_extension(node->mtn_brother);
}


/* 
 * Description:
 * Does the main work of building the in memory mapping tree form the entries
 * in the DIT.  This function is called recursively on all the given nodes
 * children to build up the tree.  Basically it does an internal search for
 * all the entries who have the target node as a parent.
 * 
 * Arguments:
 * The target node and a flag that tells if it's the root of the tree.
 *
 * Returns:
 * Nothing
 */
static int 
mapping_tree_node_get_children(mapping_tree_node *target, int is_root)
{
    Slapi_PBlock *pb;
    char * filter = NULL;
    int res;
    Slapi_Entry **entries = NULL;
    int x;
    int result = 0;

    pb = slapi_pblock_new();
    
    /* Remember that the root node of the mapping tree is the NULL suffix.
     * Since we don't really support it, children of the root node won't
     * have a MAPPING_TREE_PARENT_ATTRIBUTE. */
    if (is_root) {
        filter = slapi_ch_smprintf("(&(objectclass=nsMappingTree)(!(%s=*)))",
                 MAPPING_TREE_PARENT_ATTRIBUTE);
    } else {
        filter = slapi_ch_smprintf("(&(objectclass=nsMappingTree)(%s=\"%s\"))",
            MAPPING_TREE_PARENT_ATTRIBUTE, 
            slapi_sdn_get_dn(target->mtn_subtree));
    }

    slapi_search_internal_set_pb(pb, MAPPING_TREE_BASE_DN, LDAP_SCOPE_ONELEVEL,
        filter, NULL, 0, NULL, NULL, (void *) plugin_get_default_component_id(), 0);
    slapi_search_internal_pb(pb);
    slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &res);

    if (res != LDAP_SUCCESS) {
        LDAPDebug(LDAP_DEBUG_ANY, "WARNING: Mapping tree unable to read %s: %d\n", 
            MAPPING_TREE_BASE_DN, res, 0);
        result = -1;
        goto done;
    }

    /* We now create the mapping tree node and call this function for each
     * of the target's children. */
    slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries);
    if (NULL == entries) {
        LDAPDebug(LDAP_DEBUG_ANY, "WARNING: No mapping tree node entries found under %s\n",
            MAPPING_TREE_BASE_DN, 0, 0);
        result = -1;
        goto done;
    }

    for(x = 0; entries[x] != NULL; x++) {
        mapping_tree_node *child = NULL;

        if (LDAP_SUCCESS != mapping_tree_entry_add(entries[x], &child)) {
            LDAPDebug(LDAP_DEBUG_ANY,
             "ERROR: could not add mapping tree node %s\n",
            slapi_entry_get_dn(entries[x]), 0, 0);
            result = -1;
            goto done;
        }
        if(target == child){
            /* the mapping tree root got replaced 
             * nothing to do 
             */ 
        } else {

             child->mtn_parent = target;
             mapping_tree_node_add_child(target, child);
        }

            
        if (mapping_tree_node_get_children(child, 0 /* not the root node */))
        {
            result = -1;
            goto done;
        }
    }
    slapi_free_search_results_internal(pb);

done:
    slapi_pblock_destroy(pb);
    if (filter)
        slapi_ch_free((void **) &filter);
    return result;
}

/* 
 * Description:
 * A first attempt at walking over the mapping tree and making sure things
 * make sense.  Right now it just makes sure that each parent node has a 
 * subtree that is the suffix of its children's subtrees.  This function
 * is called recursively.
 * 
 * Arguments:
 * The root node of the mapping tree.
 *
 * Returns:
 * Nothing - it just prints warnings.  This should probably change.
 */
static void 
mapping_tree_node_validate(mapping_tree_node *node)
{
    mapping_tree_node *child_entry;

    /* Call this function for all of nodes children */
    for (child_entry = node->mtn_children; child_entry; child_entry = child_entry->mtn_brother) {
        mapping_tree_node_validate(child_entry);
    }

    if (node->mtn_parent) {
        if (!slapi_sdn_issuffix(node->mtn_subtree, node->mtn_parent->mtn_subtree)) {
            LDAPDebug(LDAP_DEBUG_ANY,
                "Warning: Invalid mapping tree.  %s can not be a child of %s\n",
                slapi_sdn_get_ndn(node->mtn_subtree), 
                slapi_sdn_get_ndn(node->mtn_parent->mtn_subtree), 0);
        }
    }
}

static void
mtn_free_referral_in_node (mapping_tree_node *node)
{
    char ** referral = node->mtn_referral;
    
    if (referral)
    {
        int i;

        for (i=0; referral[i]; i++)
            slapi_ch_free((void **) &(referral[i]));
        slapi_ch_free((void **) &referral);
    }
    if (node->mtn_referral_entry)
        slapi_entry_free(node->mtn_referral_entry);

    node->mtn_referral = NULL;
    node->mtn_referral_entry = NULL;
}

int mapping_tree_entry_modify_callback(Slapi_PBlock *pb, Slapi_Entry* entryBefore, Slapi_Entry* entryAfter, int *returncode, char *returntext, void *arg) 
{
    LDAPMod **mods;
    int i;
    mapping_tree_node * node;
    Slapi_DN * subtree;
    Slapi_Attr * attr;
    Slapi_Value *val = NULL;
    int be_list_count = 0;
    int be_list_size = 0;
    backend **backends = NULL;
    char **be_names = NULL;
    int * be_states = NULL;
    char * plugin_fct = NULL;
    char * plugin_lib = NULL;
    int plugin_flag = 0;
    mtn_distrib_fct plugin = NULL;

    slapi_pblock_get(pb, SLAPI_MODIFY_MODS, &mods);
    subtree = get_subtree_from_entry(entryAfter);
    node = mtn_get_mapping_tree_node_by_entry(mapping_tree_root, subtree);
    if (node == NULL)
    {
        /* should never happen */
        *returncode = LDAP_OPERATIONS_ERROR;
        return SLAPI_DSE_CALLBACK_ERROR;
    }

    for (i = 0; mods[i] != NULL; i++) {
        if ( (strcasecmp(mods[i]->mod_type, "cn") == 0) ||
             (strcasecmp(mods[i]->mod_type,
                 MAPPING_TREE_PARENT_ATTRIBUTE) == 0) )
        {
            mapping_tree_node * parent_node;
            /* if we are deleting this attribute the new parent 
             * node will be mapping_tree_root
             */
            if ((mods[i]->mod_op & ~LDAP_MOD_BVALUES) == LDAP_MOD_DELETE)
            {
                parent_node = mapping_tree_root;
            }
            else
            {
                /* we have to find the new parent node */
                Slapi_DN *parent_node_dn;
                parent_node_dn = get_parent_from_entry(entryAfter);
                parent_node = mtn_get_mapping_tree_node_by_entry(
                    mapping_tree_root, parent_node_dn);
                if (parent_node == NULL)
                {
                    parent_node = mapping_tree_root;
                    LDAPDebug(LDAP_DEBUG_ANY,
                          "Error: could not find parent for %s\n",
                          slapi_entry_get_dn(entryAfter), 0, 0);
                    slapi_sdn_free(&subtree);
                    *returncode = LDAP_UNWILLING_TO_PERFORM;
                    return SLAPI_DSE_CALLBACK_ERROR;
                }
                slapi_sdn_free(&parent_node_dn);
            }

            mtn_wlock();
            /* modifying the parent of a node means moving it to an 
             * other place of the tree
             * this can be done simply by removing it from its old place and 
             * moving it to the new one
             */
            mtn_remove_node(node);
            mapping_tree_node_add_child(parent_node, node);
            node->mtn_parent = parent_node;
            mtn_unlock();

        }
        else if (strcasecmp(mods[i]->mod_type, "nsslapd-backend" ) == 0)
        {
            slapi_entry_attr_find(entryAfter, "nsslapd-backend", &attr);
            if (NULL == attr)
            {
                /* if nsslapd-backend attribute is empty all backends have
                 * been suppressed, set backend list to NULL
                 * checks on the state are done a bit later 
                 */
                backends = NULL;
                be_names = NULL;
                be_states = NULL;
                be_list_count = 0;
                be_list_size = 0;
            }
            else if (get_backends_from_attr(attr, &backends, &be_names,
                         &be_states, &be_list_count, &be_list_size, node))
            {
                free_get_backends_from_attr(&backends, &be_names, &be_states, &be_list_count);
                slapi_sdn_free(&subtree);
                *returncode = LDAP_UNWILLING_TO_PERFORM;
                return SLAPI_DSE_CALLBACK_ERROR;
            }

            mtn_wlock();

            if ((backends == NULL) && (node->mtn_state == MTN_BACKEND))
            {
                PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE, "mapping tree entry need at least one nsslapd-backend\n");
                *returncode = LDAP_UNWILLING_TO_PERFORM;
                mtn_unlock();
                free_get_backends_from_attr(&backends, &be_names, &be_states, &be_list_count);
                slapi_sdn_free(&subtree);
                return SLAPI_DSE_CALLBACK_ERROR;
            }

            node->mtn_be_states = be_states;
            node->mtn_be = backends;
            node->mtn_backend_names = be_names;
            node->mtn_be_count = be_list_count;
            node->mtn_be_list_size = be_list_size;

            mtn_unlock();
            
        }
        else if (strcasecmp(mods[i]->mod_type, "nsslapd-state" ) == 0)
        {
            Slapi_Value * val;
            const char * new_state;
            Slapi_Attr * attr;

            /* state change
             * for now only allow replace
             */
            if ((mods[i]->mod_op & ~LDAP_MOD_BVALUES) != LDAP_MOD_REPLACE)
            {
                PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE, "must use replace operation to change state\n");
                *returncode = LDAP_UNWILLING_TO_PERFORM;
                slapi_sdn_free(&subtree);
                return SLAPI_DSE_CALLBACK_ERROR;
            }
            if ((mods[i]->mod_bvalues == NULL) || (mods[i]->mod_bvalues[0] == NULL))
            {
                slapi_sdn_free(&subtree);
                *returncode = LDAP_OPERATIONS_ERROR;
                return SLAPI_DSE_CALLBACK_ERROR;
            }

            slapi_entry_attr_find(entryAfter, "nsslapd-state", &attr);
            slapi_attr_first_value(attr, &val);
            new_state = slapi_value_get_string(val);

            if (mtn_state_to_int(new_state, entryAfter) == MTN_BACKEND)
            {
                if (slapi_entry_attr_find(entryAfter, "nsslapd-backend", &attr))
                {
                    PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE, "need to set nsslapd-backend before moving to backend state\n");
                    slapi_sdn_free(&subtree);
                    *returncode = LDAP_UNWILLING_TO_PERFORM;
                    return SLAPI_DSE_CALLBACK_ERROR;
                }
            }

            if ((mtn_state_to_int(new_state, entryAfter) == MTN_REFERRAL) ||
                 (mtn_state_to_int(new_state, entryAfter) == MTN_REFERRAL_ON_UPDATE))
            {
                if (slapi_entry_attr_find(entryAfter, "nsslapd-referral", &attr))
                {
                    PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE, "need to set nsslapd-referral before moving to referral state\n");
                    slapi_sdn_free(&subtree);
                    *returncode = LDAP_UNWILLING_TO_PERFORM;
                    return SLAPI_DSE_CALLBACK_ERROR;
                }
            }

            mtn_wlock();

            node->mtn_state = mtn_state_to_int(new_state, entryAfter);
                    
            mtn_unlock();
        }
        else if (strcasecmp(mods[i]->mod_type, "nsslapd-referral" ) == 0)
        {
            char ** referral;

            mtn_wlock();

            if (((mods[i]->mod_op & ~LDAP_MOD_BVALUES) == LDAP_MOD_REPLACE)
                || ((mods[i]->mod_op & ~LDAP_MOD_BVALUES) == LDAP_MOD_ADD))
            {
                /* delete old referrals, set new ones */
                mtn_free_referral_in_node(node);
                referral = mtn_get_referral_from_entry(entryAfter);
                node->mtn_referral = referral;
                node->mtn_referral_entry =
                     referral2entry(referral, slapi_sdn_get_dn(subtree));
            } else if ((mods[i]->mod_op & ~LDAP_MOD_BVALUES) == LDAP_MOD_DELETE)
            {
                /* it is not OK to delete the referrals if they are still
                 * used 
                 */
                if ((node->mtn_state == MTN_REFERRAL) ||
                    (node->mtn_state == MTN_REFERRAL_ON_UPDATE))
                {
                    PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE,
                             "cannot delete referrals in this state\n");
                    *returncode = LDAP_UNWILLING_TO_PERFORM;
                    mtn_unlock();
                    slapi_sdn_free(&subtree);
                    return SLAPI_DSE_CALLBACK_ERROR;
                }

                mtn_free_referral_in_node(node);

            } else
            {
                *returncode = LDAP_OPERATIONS_ERROR;
                mtn_unlock();
                slapi_sdn_free(&subtree);
                return SLAPI_DSE_CALLBACK_ERROR;
            }

            mtn_unlock();
            slapi_sdn_free(&subtree);
            *returncode = LDAP_SUCCESS;
            return SLAPI_DSE_CALLBACK_OK;
        }
        else if (strcasecmp(mods[i]->mod_type,
                         "nsslapd-distribution-funct" ) == 0)
        {
            if (((mods[i]->mod_op & ~LDAP_MOD_BVALUES) == LDAP_MOD_REPLACE)
                || ((mods[i]->mod_op & ~LDAP_MOD_BVALUES) == LDAP_MOD_ADD))
            {
                slapi_entry_attr_find(entryAfter,
                             "nsslapd-distribution-funct", &attr);
                slapi_attr_first_value(attr, &val);
                if (NULL == val) {
                    LDAPDebug(LDAP_DEBUG_ANY,
                    "Warning: The nsslapd-distribution-funct attribute"
                    " has no value for the mapping tree node %s\n",
                    slapi_entry_get_dn(entryAfter), 0, 0);
                    plugin_fct = NULL;
                }
                plugin_fct = slapi_ch_strdup(slapi_value_get_string(val));
            }
            else if ((mods[i]->mod_op & ~LDAP_MOD_BVALUES) == LDAP_MOD_DELETE)
            {
                plugin_fct = NULL;
            }
            plugin_flag = 1;
        }
        else if (strcasecmp(mods[i]->mod_type,
                         "nsslapd-distribution-plugin" ) == 0)
        {
            if (((mods[i]->mod_op & ~LDAP_MOD_BVALUES) == LDAP_MOD_REPLACE)
                || ((mods[i]->mod_op & ~LDAP_MOD_BVALUES) == LDAP_MOD_ADD))
            {
                slapi_entry_attr_find(entryAfter,
                             "nsslapd-distribution-plugin", &attr);
                slapi_attr_first_value(attr, &val);
                if (NULL == val) {
                    LDAPDebug(LDAP_DEBUG_ANY,
                        "Warning: The nsslapd-distribution-plugin attribute"
                        " has no value for the mapping tree node %s\n",
                    slapi_entry_get_dn(entryAfter), 0, 0);
                    plugin_lib = NULL;
                }
                plugin_lib = slapi_ch_strdup(slapi_value_get_string(val));
            }
            else if ((mods[i]->mod_op & ~LDAP_MOD_BVALUES) == LDAP_MOD_DELETE)
            {
                plugin_lib = NULL;
            }
            plugin_flag = 1;

        }
    }

    /* if distribution plugin has been configured or modified
     * check that the library and function exist 
     * and if yes apply the modifications
     */
    if (plugin_flag)
    {
        if (plugin_lib && plugin_fct)
        {
            plugin = (mtn_distrib_fct) sym_load(plugin_lib, plugin_fct, "Entry Distribution", 1);

            if (plugin == NULL)
            {
                PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE, "cannot find distribution plugin\n");
                slapi_ch_free((void **) &plugin_fct);
                slapi_ch_free((void **) &plugin_lib);
                slapi_sdn_free(&subtree);
                *returncode = LDAP_UNWILLING_TO_PERFORM;
                return SLAPI_DSE_CALLBACK_ERROR;
            }
        }
        else if ((plugin_lib == NULL) && (plugin_fct == NULL))
        {
            /* nothing configured -> OK */
            plugin = NULL;
        }
        else
        {
            /* only one parameter configured -> ERROR */
            PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE,
                 "must define distribution function and library\n");
            slapi_ch_free((void **) &plugin_fct);
            slapi_ch_free((void **) &plugin_lib);
            slapi_sdn_free(&subtree);
            *returncode = LDAP_UNWILLING_TO_PERFORM;
            return SLAPI_DSE_CALLBACK_ERROR;
        }
        
        mtn_wlock();
        if (node->mtn_dstr_plg_lib)
            slapi_ch_free((void **) &node->mtn_dstr_plg_lib);
        node->mtn_dstr_plg_lib = plugin_lib;
        if (node->mtn_dstr_plg_name)
            slapi_ch_free((void **) &node->mtn_dstr_plg_name);
        node->mtn_dstr_plg_name = plugin_fct;
        node->mtn_dstr_plg = plugin;
        mtn_unlock();
    }

    slapi_sdn_free(&subtree);
    return SLAPI_DSE_CALLBACK_OK;
}

int mapping_tree_entry_add_callback(Slapi_PBlock *pb, Slapi_Entry* entryBefore, Slapi_Entry* e, int *returncode, char *returntext, void *arg) 
{
    mapping_tree_node *node;
    int i;
    backend * be;

    /* WARNING
     * for adds we don't need to grab the mapping tree global lock,
     * because the add operation in the tree is atomic because
     * only one pointer is updated in the tree.
     * Should the mapping tree stucture change, this  would have to
     * be checked again 
     */
    *returncode = mapping_tree_entry_add(entryBefore, &node);
    if (LDAP_SUCCESS != *returncode)
    {
        return SLAPI_DSE_CALLBACK_ERROR;
    }

    if(node && node->mtn_parent != NULL && node != mapping_tree_root )
    {
     /* If the node has a parent and the node is not the mapping tree root,
      * then add it as a child node. Note that the special case when the
      * node is the mapping tree root and has no parent is handled inside
      * the mapping_tree_entry_add() function by replacing the contents of
      * the mapping tree root node with information from the add request.
      */ 
      mapping_tree_node_add_child(node->mtn_parent, node);
    }

    for (i = 0; ((i < node->mtn_be_count) && (node->mtn_backend_names) &&
        (node->mtn_backend_names[i])); i++)
    {
        if ((be = slapi_be_select_by_instance_name(node->mtn_backend_names[i])) 
                && (be->be_state == BE_STATE_STARTED))
        {
            mtn_be_state_change(node->mtn_backend_names[i], SLAPI_BE_STATE_DELETE,
                node->mtn_be_states[i]);
        }
    }

    node->mtn_extension = factory_create_extension(mapping_tree_get_extension_type(), node, NULL);

    return SLAPI_DSE_CALLBACK_OK;
}

/* utility function to remove a node from the tree of mapping_tree_node
 */
static void mtn_remove_node(mapping_tree_node * node)
{
    if (node->mtn_parent->mtn_children == node)
        node->mtn_parent->mtn_children = node->mtn_brother;
    else
    {
        mapping_tree_node * tmp_node = node->mtn_parent->mtn_children;
        while (tmp_node && (tmp_node->mtn_brother != node))
            tmp_node = tmp_node->mtn_brother;

        PR_ASSERT(tmp_node != NULL);

        tmp_node->mtn_brother = node->mtn_brother;
    }
    node->mtn_brother = NULL;
}

int mapping_tree_entry_delete_callback(Slapi_PBlock *pb, Slapi_Entry* entryBefore, Slapi_Entry* e, int *returncode, char *returntext, void *arg) 
{
    int result;
    mapping_tree_node *node = NULL;
    Slapi_DN * subtree;
    int i;
    int removed = 0;

    mtn_wlock();
    subtree = get_subtree_from_entry(entryBefore);

    if (subtree == NULL)
    {
        /* there is no cn attribute in this entry
         * -> this is not a mapping tree node
         * -> nothing to do
         */
        result = SLAPI_DSE_CALLBACK_OK;
        goto done;
    }

    node = slapi_get_mapping_tree_node_by_dn(subtree);
    if (node == NULL)
    {
        /* should never happen */
        *returncode = LDAP_OPERATIONS_ERROR;
        result = SLAPI_DSE_CALLBACK_ERROR;
        goto done;
    }

    if (slapi_sdn_compare(subtree, node->mtn_subtree))
    {
        /* There is no node associated to this entry
         * -> nothing to do
         */
        result = SLAPI_DSE_CALLBACK_OK;
        goto done;
    }
    
    /* if node has children we must refuse the delete */
    if (node->mtn_children)
    {
        result = SLAPI_DSE_CALLBACK_ERROR;
        *returncode = LDAP_UNWILLING_TO_PERFORM;
        PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE, "this node has some children");
        goto done;
    }

    /* at this point the node should be different from mapping_tree_root
     * and therefore have a parent 
     */
    PR_ASSERT(node->mtn_parent != NULL);

    /* lets get the node out of the mapping tree */
    mtn_remove_node(node);

    result = SLAPI_DSE_CALLBACK_OK;
    removed = 1;
    
done:
    mtn_unlock();
    slapi_sdn_free(&subtree);
    if (SLAPI_DSE_CALLBACK_OK == result && removed)
    {
        /* Signal the plugins that a new backend-suffix has been deleted
         * rq : we have to unlock the mapping tree in that case because
         * most of the plugins will try to search upon this notification
         * and should we keep the lock we would end with a dead-lock
         */
        for (i = 0; ((i < node->mtn_be_count) && (node->mtn_backend_names) &&
            (node->mtn_backend_names[i])); i++)
        {
            if ((node->mtn_be_states[i] != SLAPI_BE_STATE_DELETE) && 
                (NULL != slapi_be_select_by_instance_name(
                    node->mtn_backend_names[i])))
            {
                mtn_be_state_change(node->mtn_backend_names[i],
                    node->mtn_be_states[i], SLAPI_BE_STATE_DELETE);
            }
        }

        /* at this point the node is out of the mapping tree, 
         * we can now free the structure
         */
        mtn_free_node(&node);
    }
    return result;
}

/*
 * Add an internal mapping tree node.
 */
static mapping_tree_node * 
add_internal_mapping_tree_node(const char *subtree, Slapi_Backend *be, mapping_tree_node *parent)
{
    Slapi_DN *dn;
    mapping_tree_node *node;
    backend ** be_list = (backend **) slapi_ch_malloc(sizeof(backend **));

    be_list[0] = be;

    dn = slapi_sdn_new_dn_byval(subtree);
    node= mapping_tree_node_new(
            dn,
            be_list,
            NULL, /* backend_name */
            NULL,
            1,    /* number of backends at this node */
            1,    /* size of backend list structure */
            NULL, /* referral */
            parent,
            MTN_BACKEND,
            1, /* The config  node is a private node.
                *  People can't see or change it. */
            NULL, NULL, NULL);
    return node;
}

/* 
 * Description:
 * Inits the mapping tree.  The mapping tree is rooted at a node with
 * subtree "".  Think of this node as the node for the NULL suffix
 * even though we don't really support it.  This function will 
 * create the root node and then consult the DIT for the rest of 
 * the nodes.  It will also add the node for cn=config.
 *
 * One thing to note... Until the mapping tree is inited.  We use
 * slapi_be_select for all our selection needs.  To read in the mapping
 * tree from the DIT, we need to some internal operations.  These 
 * operations need to use slapi_be_select.
 * 
 * Arguments:
 * Nothing
 *
 * Returns:
 * Right now it always returns 0.  This will most likely change.  Right
 * now, we just log warnings when ever something goes wrong.
 */
int 
mapping_tree_init()
{
    Slapi_Backend *be;
    mapping_tree_node *node;
    /* Create the root of the mapping tree. */

    /* The root of the mapping tree is the NULL suffix.  It's always there,
     * but, because we don't really support it, we won't have an entry in
     * the dit for the NULL suffix mapping tree node. */

    /* Once we support the NULL suffix we should do something more clever here.
     * For now will use the current backend we use for "" */

    /* I'm not really sure what the state of the root node should be.  The root
     * node will end up being selected if none of the suffices for the backends
     * would work with the target.  For now when the root node is selected, 
     * the default backend will be returned.  (The special case where the
     * target dn is "" is handled differently.) */
    
    /* we call this function from a single thread, so it should be ok */

    if(mapping_tree_freed){
    /* shutdown has been detected */
      return 0;
    }

    if (mapping_tree_inited)
        return 0;

    /* ONREPL - I have moved this up because otherwise we can endup calling this 
     * function recursively */

    mapping_tree_inited = 1;

    slapi_register_supported_control(MTN_CONTROL_USE_ONE_BACKEND_OID,
                     SLAPI_OPERATION_SEARCH);
    slapi_register_supported_control(MTN_CONTROL_USE_ONE_BACKEND_EXT_OID,
                     SLAPI_OPERATION_SEARCH);

    myLock = PR_NewRWLock(PR_RWLOCK_RANK_NONE, "mapping tree");

    be= slapi_be_select_by_instance_name(DSE_BACKEND);
    mapping_tree_root= add_internal_mapping_tree_node("", be, NULL);

    /* We also need to add the config and schema backends to the mapping tree.
     * They are special in that users will not know about it's node in the 
     * mapping tree.  This is to prevent them from disableing it or 
     * returning a referral for it. */
    node= add_internal_mapping_tree_node("cn=config", be, mapping_tree_root);
    mapping_tree_node_add_child(mapping_tree_root, node);
    node= add_internal_mapping_tree_node("cn=monitor", be, mapping_tree_root);
    mapping_tree_node_add_child(mapping_tree_root, node);
    be= slapi_be_select_by_instance_name( "schema-internal" );
    node= add_internal_mapping_tree_node("cn=schema", be, mapping_tree_root);
    mapping_tree_node_add_child(mapping_tree_root, node);

    /* 
     * Now we need to look under cn=mapping tree, cn=config to find the rest
     * of the mapping tree entries.
     * Builds the mapping tree from entries in the DIT.  This function just
     * calls mapping_tree_node_get_children with the special case for the
     * root node.
     */
    if (mapping_tree_node_get_children(mapping_tree_root, 1))
        return -1;

    mtn_create_extension(mapping_tree_root);

    /* setup the dse callback functions for the ldbm instance config entry */
    {
        slapi_config_register_callback(SLAPI_OPERATION_MODIFY,
             DSE_FLAG_PREOP, MAPPING_TREE_BASE_DN, LDAP_SCOPE_SUBTREE,
             "(objectclass=nsMappingTree)",
             mapping_tree_entry_modify_callback, NULL);
        slapi_config_register_callback(SLAPI_OPERATION_ADD,
            DSE_FLAG_PREOP, MAPPING_TREE_BASE_DN, LDAP_SCOPE_SUBTREE,
            "(objectclass=nsMappingTree)", mapping_tree_entry_add_callback,
            NULL);
        slapi_config_register_callback(SLAPI_OPERATION_DELETE,
            DSE_FLAG_PREOP, MAPPING_TREE_BASE_DN, LDAP_SCOPE_SUBTREE,
            "(objectclass=nsMappingTree)", mapping_tree_entry_delete_callback,
            NULL);
    }
    return 0;
}

static void
mtn_free_node (mapping_tree_node **node)
{
    mapping_tree_node *child = (*node)->mtn_children;

    /* free children first */
    while  (child)
    {
        mapping_tree_node * tmp_child = child->mtn_brother;
        mtn_free_node (&child);
        child = tmp_child;
    }
    (*node)->mtn_children = NULL;
    (*node)->mtn_parent = NULL;

    /* free this node */
    /* ONREPL - not quite sure which fields should be freed. For now,
       only freeing fields explicitely allocated in the new_node function */
    factory_destroy_extension (mapping_tree_get_extension_type(), *node, NULL,
                               &((*node)->mtn_extension));

    slapi_sdn_free(&((*node)->mtn_subtree));

    mtn_free_referral_in_node(*node);

    if ((*node)->mtn_be_count > 0)
    {
        if ((*node)->mtn_be)
            slapi_ch_free((void **) &((*node)->mtn_be));
    
        if ((*node)->mtn_backend_names)
            slapi_ch_free((void **) &((*node)->mtn_backend_names));

        if ((*node)->mtn_be_states)
            slapi_ch_free((void **) &((*node)->mtn_be_states));
    }

    slapi_ch_free ((void**) node);
}

/* Description: frees the tree; should be called when the server shuts down
 */
void 
mapping_tree_free ()
{
    /* unregister dse callbacks */
    slapi_config_remove_callback(SLAPI_OPERATION_MODIFY, DSE_FLAG_PREOP, MAPPING_TREE_BASE_DN, LDAP_SCOPE_BASE, "(objectclass=*)", mapping_tree_entry_modify_callback);
    slapi_config_remove_callback(SLAPI_OPERATION_ADD, DSE_FLAG_PREOP, MAPPING_TREE_BASE_DN, LDAP_SCOPE_BASE, "(objectclass=*)", mapping_tree_entry_add_callback);
    slapi_config_remove_callback(SLAPI_OPERATION_DELETE, DSE_FLAG_PREOP, MAPPING_TREE_BASE_DN, LDAP_SCOPE_BASE, "(objectclass=*)", mapping_tree_entry_delete_callback);

    /* The state change plugins registered on the mapping tree
     * should not get any state change information 
     * - unregister all those callbacks
     */ 
    slapi_unregister_backend_state_change_all();
    /* recursively free tree nodes */
    mtn_free_node (&mapping_tree_root);
    mapping_tree_freed = 1;
}

/* This function returns the first node to parse when a search is done 
 * on a given node in the mapping tree
 */
static mapping_tree_node *
mtn_get_first_node(mapping_tree_node * node, int scope)
{
    if (node == NULL) 
        return NULL;

    /* never climb down the tree from base "" */
    if (node == mapping_tree_root)
    {
        return node;
    }

    if (scope == LDAP_SCOPE_BASE)
        return node;

    if (scope == LDAP_SCOPE_ONELEVEL)
    {
         if (node->mtn_children)
            return node->mtn_children;
        else
            return node;
    }

    while (node->mtn_children)
        node = node->mtn_children;

    return node;
}

int slapi_mtn_get_first_be(mapping_tree_node * node_list,
         mapping_tree_node ** node, Slapi_PBlock *pb, Slapi_Backend **be,
         int * be_index, Slapi_Entry **referral, char *errorbuf, int scope)
{
    *node = mtn_get_first_node(node_list, scope);
    if (scope == LDAP_SCOPE_BASE)
        *be_index = -1;
    else
        *be_index = 0;

    return mtn_get_be(*node, pb, be, be_index, referral, errorbuf);
}

int slapi_mtn_get_next_be(mapping_tree_node * node_list,
         mapping_tree_node ** node, Slapi_PBlock *pb, Slapi_Backend **be,
         int * be_index, Slapi_Entry **referral, char *errorbuf, int scope)
{
    int rc;

    if (((*node)->mtn_parent == NULL) ||  /* -> node has been deleted */
        (scope == LDAP_SCOPE_BASE)) 

    {
        *node = NULL;
        *be = NULL;
        *referral = NULL;
        return 0;
    }
        
    /* never climb down the tree from the rootDSE */
    if (node_list == mapping_tree_root)
    {
        *node = NULL;
        *be = NULL;
        *referral = NULL;
        return 0;
    }

    rc = mtn_get_be(*node, pb, be, be_index, referral, errorbuf);

    if (rc != LDAP_SUCCESS)
    {
        *node = mtn_get_next_node(*node, node_list, scope);
        return rc;
    }

    if  ((*be == NULL) && (*referral == NULL))
    {
        *node = mtn_get_next_node(*node, node_list, scope);
        if (*node == NULL)
        {
            *be = NULL;
            return 0;
        }
        *be_index = 0;
        return mtn_get_be(*node, pb, be, be_index, referral, errorbuf);
    }

    return LDAP_SUCCESS;
}

/* This function returns the next node to parse when a subtree search is done 
 * on a given node in the mapping tree
 */
static mapping_tree_node *
mtn_get_next_node(mapping_tree_node * node, mapping_tree_node * node_list, int scope)
{
    if (scope == LDAP_SCOPE_BASE)
        return NULL;

    /* if we are back to the top of the subtree searched then we have finished */
    if (node == node_list)
        node = NULL;

    else if (node->mtn_brother)
    {
        node = node->mtn_brother;
        if (scope == LDAP_SCOPE_SUBTREE)
            while (node->mtn_children)
                node = node->mtn_children;
    }
    else
        node = node->mtn_parent;

    return node;
}

/* Description :
 * return 0 if the given entry does not have any child node in the mapping tree
 * != otherwise
 *
 */
int
mtn_sdn_has_child(Slapi_DN *target_sdn)
{
    mapping_tree_node *node;

    /* algo : get the target node for the given dn
     * then loop through all its child  to check if one of them is below 
     * the target dn
     */
    node = slapi_get_mapping_tree_node_by_dn(target_sdn);
    
    /* if there is no node for this dn then there is no child either */
    if (node == NULL)
        return 0;

    node = node->mtn_children;
    while (node)
    {
        if (slapi_sdn_issuffix(node->mtn_subtree, target_sdn))
            return 1;
        node = node->mtn_brother;
    }
    return 0;
}


/* Description:
 * Find the backend that would be used to store a dn.
 */
Slapi_Backend *slapi_mapping_tree_find_backend_for_sdn(Slapi_DN *sdn)
{
    mapping_tree_node *target_node;
    Slapi_Backend *be;
    int flag_stop = 0, index;
    Slapi_PBlock *pb;
    Slapi_Operation *op;

    mtn_lock();
    target_node = slapi_get_mapping_tree_node_by_dn(sdn);

    if ((target_node == mapping_tree_root) &&
        (slapi_sdn_get_ndn_len(sdn) > 0)) {
        /* couldn't find a matching node */
        be = defbackend_get_backend();
        goto done;
    }

    if ((target_node == NULL) || (target_node->mtn_be_count == 0)) {
        /* no backend configured for this node */
        be = NULL;
        goto done;
    }

    if (target_node->mtn_be_count == 1) {
        /* not distributed, so we've already found it */
        if (target_node->mtn_be[0] == NULL) {
            target_node->mtn_be[0] = slapi_be_select_by_instance_name(
                target_node->mtn_backend_names[0]);
        }
        be = target_node->mtn_be[0];
        goto done;
    }

    /* have to call the distribution plugin */
    be = defbackend_get_backend();
    pb = slapi_pblock_new();
    if (!pb) {
        goto done;
    }
    op = internal_operation_new(SLAPI_OPERATION_ADD, 0);
    if (!op) {
        slapi_pblock_destroy(pb);
        goto done;
    }
    operation_set_target_spec(op, sdn);
    slapi_pblock_set(pb, SLAPI_OPERATION, op);
    index = mtn_get_be_distributed(pb, target_node, sdn, &flag_stop);
    slapi_pblock_destroy(pb);   /* also frees the operation */

    if (target_node->mtn_be[index] == NULL) {
        target_node->mtn_be[index] = slapi_be_select_by_instance_name(
            target_node->mtn_backend_names[index]);
    }
    be = target_node->mtn_be[index];

done:
    mtn_unlock();
    return be;
}

/* Check if the target dn is '\0' - the null dn */
static int sdn_is_nulldn(const Slapi_DN *sdn){

    if(sdn){
                const char *dn= slapi_sdn_get_ndn(sdn); 
        if(dn && ( '\0' == *dn)){
                return 1;    
        }
    }
    return 0;
}

/* 
 * Description:
 * The reason we have a mapping tree.  This function selects a backend or
 * referral to handle a given request.  Uses the target of the operation to
 * find a mapping tree node, then based on the operation type, bind dn, state
 * of the node, etc. it selects a backend or referral.
 * 
 * In this initial implementation of the mapping tree, each node can only have
 * one backend and one referral.  Later we should change this so each node has
 * a list of backends and a list of referrals.  Then we should add a modifier
 * to the state of the node.  For example, MTN_MODIFIER_ROUND_ROBIN could be a
 * modifer on the way a backend or referral is returned from the lists.
 *
 * Arguments:
 * pb is the pblock being used to service the operation.
 * be is an output param that will be set to the selected backend.
 * referral is an output param that will be set to the selected referral.
 * errorbuf is a pointer to a buffer that an error string will be written to
 *    if there is an error.  The caller is responsible for passing in a big 
 *    enough chunk of memory.  BUFSIZ should be fine.  If errorbuf is NULL, 
 *    no error string is written to it.  The string returned in errorbuf
 *    would be a good candidate for sending back to the client to describe the
 *    error.
 *
 * Returns:
 * LDAP_SUCCESS on success, other LDAP result codes if there is a problem.
 */
int slapi_mapping_tree_select(Slapi_PBlock *pb, Slapi_Backend **be, Slapi_Entry **referral, char *errorbuf)
{
    Slapi_DN *target_sdn = NULL;
    mapping_tree_node *target_node;
    Slapi_Operation *op;
    int index;
    int ret;
    int scope=LDAP_SCOPE_BASE;
    int op_type;
    

    if(mapping_tree_freed){
        /* shutdown detected */ 
        return LDAP_OPERATIONS_ERROR;
    }


    if (errorbuf) {
        errorbuf[0] = '\0';
    }

    slapi_pblock_get(pb, SLAPI_OPERATION, &op);
    slapi_pblock_get(pb, SLAPI_OPERATION_TYPE, &op_type);
    slapi_pblock_get(pb, SLAPI_SEARCH_SCOPE, &scope);

    /* Get the target for this op */
    target_sdn = operation_get_target_spec (op);

    if(!mapping_tree_inited) {
        mapping_tree_init();
    } 

    be[0] = NULL;
    referral[0] = NULL;

    mtn_lock();

    /* Get the mapping tree node that is the best match for the target dn. */
    target_node = slapi_get_mapping_tree_node_by_dn(target_sdn);
    if (target_node == NULL)
        target_node = mapping_tree_root;

    /* The processing of the base scope root DSE search and all other LDAP operations on "" 
     *  will be transferred to the internal DSE backend 
     */
    if( sdn_is_nulldn(target_sdn) &&
        ((op_type == SLAPI_OPERATION_SEARCH) && (scope == LDAP_SCOPE_BASE)  ||
         (op_type != SLAPI_OPERATION_SEARCH)) ) {

        mtn_unlock();
        *be = slapi_be_select_by_instance_name(DSE_BACKEND);
        if(*be != NULL)
        {
            ret  = LDAP_SUCCESS;
            slapi_be_Rlock(*be);    /* also done inside mtn_get_be() below */
        } else {
            ret = LDAP_OPERATIONS_ERROR;
        }
        return ret;
    }

    /* index == -1 is used to specify that we want only one backend not a list
     * used for BASE search, ADD, DELETE, MODIFY
     */
    index = -1;
    ret = mtn_get_be(target_node, pb, be, &index, referral, errorbuf);
    slapi_pblock_set(pb, SLAPI_BACKEND_COUNT, &index);

    mtn_unlock();

    /* if a backend was returned, make sure that all non-search operations
     * fail if the backend is read-only, 
     * or if the whole server is readonly AND backend is public (!private)
     */
    if ((ret == LDAP_SUCCESS) && *be &&
        ((*be)->be_readonly ||
        (slapi_config_get_readonly() && !slapi_be_private(*be)))) {
        unsigned long op_type = operation_get_type(op);

        if ((op_type != SLAPI_OPERATION_SEARCH) &&
            (op_type != SLAPI_OPERATION_COMPARE) &&
            (op_type != SLAPI_OPERATION_BIND) && 
            (op_type != SLAPI_OPERATION_UNBIND)) {
            ret = LDAP_UNWILLING_TO_PERFORM;
            PL_strncpyz(errorbuf, slapi_config_get_readonly() ? 
                    "Server is read-only" :
                    "database is read-only", BUFSIZ);
            slapi_be_Unlock(*be);
            *be = NULL;
        }
    }

    return ret;
}

int slapi_mapping_tree_select_all(Slapi_PBlock *pb, Slapi_Backend **be_list,
        Slapi_Entry **referral_list, char *errorbuf)
{
    Slapi_DN *target_sdn = NULL;
    mapping_tree_node *node_list;
    mapping_tree_node *node;
    Slapi_Operation *op;
    int index;
    int ret;
    int ret_code = LDAP_SUCCESS;
    int be_index = 0 ;
    int referral_index = 0 ;
    int be_list_size = 0;
    int referral_list_size = 0;
    Slapi_Backend * be;
    Slapi_Entry * referral;
    int scope = LDAP_SCOPE_BASE;
    Slapi_DN sdn;
    char *base;
    int flag_partial_result = 0;
    int op_type;
    
    if(mapping_tree_freed){
        return LDAP_OPERATIONS_ERROR;
    }

    if (errorbuf) {
        errorbuf[0] = '\0';
    }

    /* get the operational parameters */
    slapi_pblock_get(pb, SLAPI_SEARCH_TARGET, &base);
    slapi_sdn_init_dn_ndn_byref(&sdn, base); /* normalized in opshared.c */
    slapi_pblock_get(pb, SLAPI_OPERATION, &op);
    target_sdn = operation_get_target_spec (op);
    slapi_pblock_get(pb, SLAPI_OPERATION_TYPE, &op_type);
    slapi_pblock_get(pb, SLAPI_SEARCH_SCOPE, &scope);

    if(!mapping_tree_inited){
        mapping_tree_init();
    } 

    mtn_lock();

    be_list_size = BE_LIST_SIZE;
    referral_list_size = BE_LIST_SIZE;
    be_list[0] = NULL;
    referral_list[0] = NULL;
        
    /* Get the mapping tree node that is the best match for the target dn. */
    node_list = slapi_get_mapping_tree_node_by_dn(target_sdn);
    if (node_list == NULL)
        node_list = mapping_tree_root;

    if( sdn_is_nulldn(target_sdn) &&  ( op_type == SLAPI_OPERATION_SEARCH)
            && (scope == LDAP_SCOPE_BASE) )  { 
        mtn_unlock();
        be = slapi_be_select_by_instance_name(DSE_BACKEND);
        if(be != NULL)
        {
            be_list[0]=be;
            be_list[1] = NULL;
            ret_code  = LDAP_SUCCESS;
            slapi_be_Rlock(be); /* also done inside mtn_get_be() below */
        } else {
            ret_code = LDAP_OPERATIONS_ERROR;
        }
        return ret_code;
    }

    ret = slapi_mtn_get_first_be(node_list, &node, pb, &be, &index, &referral, errorbuf, scope);

    while ((node) &&(index < BE_LIST_SIZE))
    {
        if (ret != LDAP_SUCCESS)
        {
            /* flag we have problems at least on part of the tree */
            flag_partial_result = 1;
        }
        else if ( ( ((!slapi_sdn_issuffix(&sdn, slapi_mtn_get_dn(node))
            && !slapi_sdn_issuffix(slapi_mtn_get_dn(node), &sdn))) 
            || ((node_list == mapping_tree_root) && node->mtn_private
            && (scope != LDAP_SCOPE_BASE)) )
                    && (!be || strncmp(be->be_name, "default", 8)))
        {
            if (be)
            {
                /* wrong backend or referall, ignore it */
                slapi_log_error(SLAPI_LOG_ARGS, NULL,
                     "mapping tree release backend : %s\n",
                     slapi_be_get_name(be));
                slapi_be_Unlock(be);
            }
        }
        else
        {
            if (be)
            {
                    be_list[be_index++]=be;
            }

            if (referral)
            {
                referral_list[referral_index++] = referral;

                /* if we hit a referral at the base of the search 
                 * we must return a REFERRAL error with only this referral
                 * all backend or referral below this node are ignored 
                 */
                if (slapi_sdn_issuffix(target_sdn, slapi_mtn_get_dn(node)))
                {
                    ret_code = LDAP_REFERRAL;
                    break;    /* get out of the while loop */
                }
            }
        }

        ret = slapi_mtn_get_next_be(node_list, &node, pb, &be, &index,
                     &referral, errorbuf, scope);
    }
    mtn_unlock();
    slapi_sdn_done(&sdn);
    be_list[be_index] = NULL;
    referral_list[referral_index] = NULL;

    if (flag_partial_result)
    {
        /* if no node in active has been found -> return LDAP_OPERATIONS_ERROR
         * but if only part of the nodes are disabled
         * do not return an error to allow directory browser to work OK 
         * in the console
         * It would be better to return a meaningfull error
         * unfortunately LDAP_PARTIAL_RESULTS is not usable because
         * it is already used for V2 referrals
         * leave no error for now and fix this later
         */
        if ((be_index == 0) && (referral_index == 0))
            return LDAP_OPERATIONS_ERROR;
        else
            return ret_code;
    }
    else
        return ret_code;
}

void slapi_mapping_tree_free_all(Slapi_Backend **be_list, Slapi_Entry **referral_list)
{
    int index = 0;

    /* go through the list of all backends that was used for the operation
     * and unlock them 
     * go through the list of referrals and free them
     * free the two tables that were used to store the two lists 
     */
    if (be_list[index] != NULL)
    {
        Slapi_Backend * be;
    
        while ((be = be_list[index++]))
        {
            slapi_log_error(SLAPI_LOG_ARGS, NULL, "mapping tree release backend : %s\n", slapi_be_get_name(be));
            slapi_be_Unlock(be);
        }
    }

    index = 0;
    if (referral_list[index] != NULL)
    {
        Slapi_Entry * referral;
        while ((referral = referral_list[index++]))
        {
            slapi_entry_free(referral);
        }
    }
}


/* same as slapi_mapping_tree_select() but will also check that the supplied
 * newdn is in the same backend
 */
int slapi_mapping_tree_select_and_check(Slapi_PBlock *pb,char *newdn, Slapi_Backend **be, Slapi_Entry **referral, char *errorbuf)
{
    Slapi_DN *target_sdn = NULL;
    Slapi_DN dn_newdn;
    Slapi_Backend * new_be = NULL;
    Slapi_Entry * new_referral = NULL;
    mapping_tree_node *target_node;
    int index;
    Slapi_Operation *op;
    int ret;
    int need_unlock = 0;
    
    if(mapping_tree_freed){
        return LDAP_OPERATIONS_ERROR;
     }

    slapi_sdn_init(&dn_newdn);
    slapi_pblock_get(pb, SLAPI_OPERATION, &op);
    target_sdn = operation_get_target_spec (op);

    * referral = NULL;
    ret = slapi_mapping_tree_select(pb, be, referral, errorbuf); 
    if (ret)
        goto unlock_and_return;

    slapi_sdn_init_dn_byref(&dn_newdn,newdn);

    /* acquire lock now, after slapi_mapping_tree_select() which also locks,
       because we are accessing mt internals */
    mtn_lock();
    need_unlock = 1; /* we have now acquired the lock */
    target_node = slapi_get_mapping_tree_node_by_dn(&dn_newdn);
    if (target_node == NULL)
        target_node = mapping_tree_root;
    index = -1;
    ret = mtn_get_be(target_node, pb, &new_be, &index, &new_referral, errorbuf);
    if (ret)
        goto unlock_and_return;

    if ((*be) && ((*be != new_be) || mtn_sdn_has_child(target_sdn)))
    {
        ret = LDAP_UNWILLING_TO_PERFORM;
        PR_snprintf(errorbuf, BUFSIZ, "Cannot move entries accross backends\n");
        goto unlock_and_return;
    }

unlock_and_return:
    /* if slapi_mapping_tree_select failed, we won't have the lock */
    if (need_unlock) {
        mtn_unlock();
    }

    slapi_sdn_done(&dn_newdn);

    if (new_be)
        slapi_be_Unlock(new_be);

    if (new_referral)
        slapi_entry_free(new_referral);

    if (ret != LDAP_SUCCESS)
    {
        if (*be)
        {
            slapi_be_Unlock(*be);
            *be = NULL;
        }
        if (*referral)
        {
            slapi_entry_free(*referral);
            *referral = NULL;
        }
    }

    return ret;
}

/*
 * allow to solve the distribution problem when several back-ends are defined 
 */
static int
mtn_get_be_distributed(Slapi_PBlock *pb, mapping_tree_node * target_node,
    Slapi_DN *target_sdn, int * flag_stop)
{
    int index;
    *flag_stop = 0;

    if (target_node->mtn_dstr_plg)
    {
        index = (*target_node->mtn_dstr_plg)(pb, target_sdn,
                 target_node->mtn_backend_names, target_node->mtn_be_count,
                 target_node->mtn_subtree, target_node->mtn_be_states);

        if (index == SLAPI_BE_ALL_BACKENDS)
        {
            /* special value to indicate all backends must be scanned
             * start with first one 
             */
            index = 0;
        }
        /* paranoid check, never trust another programmer */
        else if ((index >= target_node->mtn_be_count) || (index < 0))
        {
                LDAPDebug(LDAP_DEBUG_ANY,
                    "Warning: distribution plugin returned wrong backend"
                    " : %d for entry %s at node %s\n",
                     index, slapi_sdn_get_ndn(target_sdn),
                     slapi_sdn_get_ndn(target_node->mtn_subtree));
            index = 0;
        }
        else 
        {
            /* only one backend to scan 
             * set flag_stop to indicate we must stop the search here 
             */
            *flag_stop = 1;
        }
    }
    else 
    {
        /* there is several backends but no distribution function
         * return the first backend 
         */
        LDAPDebug(LDAP_DEBUG_ANY,
        "Warning: distribution plugin not configured at node : %s\n",
        slapi_sdn_get_ndn(target_node->mtn_subtree), 0, 0);
        index = 0;
    }

    return index;
}
/* 
 * this function is in charge of choosing the right backend for a given
 * mapping tree node
 * In case when several backends are used it is in charge of the spanning the 
 * request among all the backend or choosing the only backend to use depending
 * on the type and scope of the LDAP operation
 *
 * index == -1 is used to specify that we want only the one best backend
 * used for BASE search, ADD, DELETE, MODIFY
 * index >0 means we are doing a SUBTREE or ONELEVEL search and that the be in
 * that position must be returned
 */
static int mtn_get_be(mapping_tree_node *target_node, Slapi_PBlock *pb,
   Slapi_Backend **be, int * index, Slapi_Entry **referral, char *errorbuf)
{
    Slapi_DN *target_sdn;
    Slapi_Operation *op;
    int result = LDAP_SUCCESS;
    int override_referral = 0;
    unsigned long op_type;
    int flag_stop = 0;
    struct slapi_componentid *cid = NULL;

    if(mapping_tree_freed){
        /* shut down detected */
        return LDAP_OPERATIONS_ERROR; 
    }
    /* Get usefull stuff like the type of operation, target dn */
    slapi_pblock_get(pb, SLAPI_OPERATION, &op);
    op_type = operation_get_type(op);
    target_sdn = operation_get_target_spec (op);

    if (target_node->mtn_state == MTN_DISABLED) {
		if (errorbuf) {
			PR_snprintf(errorbuf, BUFSIZ,
                "Warning: Operation attempted on a disabled node : %s\n",
				slapi_sdn_get_dn(target_node->mtn_subtree));
		}
        result = LDAP_OPERATIONS_ERROR;
        return result;
    }

    slapi_pblock_get (pb, SLAPI_PLUGIN_IDENTITY, &cid);

    override_referral =
        ((cid != NULL) && (pw_get_componentID() != NULL) && (pw_get_componentID() == cid)) ||
        operation_is_flag_set(op, OP_FLAG_LEGACY_REPLICATION_DN) || /* 4.0 lgacy update */
        operation_is_flag_set(op, OP_FLAG_REPLICATED) || /* 5.0 replication update */
        operation_is_flag_set(op, OP_FLAG_TOMBSTONE_ENTRY); /* 5.1 fix to enable tombstone delete on a R-O consumer */
    if ((target_node->mtn_state == MTN_BACKEND) ||
    (target_node->mtn_state == MTN_CONTAINER ) ||
        ((target_node->mtn_state == MTN_REFERRAL_ON_UPDATE) && 
         ((SLAPI_OPERATION_SEARCH == op_type)||(SLAPI_OPERATION_BIND == op_type) || 
         (SLAPI_OPERATION_UNBIND == op_type) || (SLAPI_OPERATION_COMPARE == op_type))) ||
        override_referral) {
        if ((target_node == mapping_tree_root) ){
            /* If we got here, then we couldn't find a matching node 
             * for the target. We'll use the default backend.  Once
             * we fully support the NULL suffix, we should do something more
             * clever here.
             */
             *be = defbackend_get_backend(); 
    
        } else {
            if ((*index == -1) || (*index == 0)) {
                /* In this case, we are doing 
                 * a READ, ADD, MODIDY or DELETE on a single entry
                 * or we are starting a SEARCH 
                 * if there is several possible backend we want to apply
                 * the distribution plugin
                 */
                if (target_node->mtn_be_count <= 1) {
                    /* there is only one backend no choice possible */
                    *index = 0;
                } else {
                    *index = mtn_get_be_distributed(pb, target_node,
                         target_sdn, &flag_stop);
                }
            }

            if ((*index == -2) || (*index >= target_node->mtn_be_count)) {
        /* we have already returned all backends -> return NULL */
                *be = NULL;
                *referral = NULL;
            } else {
                /* return next backend, increment index */
                *be = target_node->mtn_be[*index];
                if(*be==NULL) {
                    if (NULL != target_node->mtn_be_states &&
                        target_node->mtn_be_states[*index] == SLAPI_BE_STATE_DELETE) {
                        /* This MTN is being deleted */
                        *be = defbackend_get_backend();
                    } else {
                        /* This MTN has not been linked to its backend
                         * instance yet. */
                        target_node->mtn_be[*index] =
                            slapi_be_select_by_instance_name(
                                target_node->mtn_backend_names[*index]);
                        *be = target_node->mtn_be[*index];
                        if(*be==NULL) {
                            LDAPDebug(LDAP_DEBUG_ANY,
             "Warning: Mapping tree node entry for %s point to "
             "an unknown backend : %s\n",
             slapi_sdn_get_dn(target_node->mtn_subtree),
             target_node->mtn_backend_names[*index], 0);
                            /* Well there's still not backend instance for
                             * this MTN, so let's have the default backend
                             * deal with this.
                             */
                            *be = defbackend_get_backend();
                        }
                    }
                }
                if ((target_node->mtn_be_states) &&
                    (target_node->mtn_be_states[*index] == SLAPI_BE_STATE_OFFLINE)) {
                    LDAPDebug(LDAP_DEBUG_TRACE,
                        "Warning: Operation attempted on backend in OFFLINE "
                        "state : %s\n",
                         target_node->mtn_backend_names[*index], 0, 0);
            result = LDAP_OPERATIONS_ERROR;
                    *be = defbackend_get_backend();
                }
                if (flag_stop)
                    *index = -2;
                else
                    (*index)++;
            }
        }        
        *referral = NULL;
    } else {
        /* otherwise we must return the referral
         * if ((target_node->mtn_state == MTN_REFERRAL) ||
         * (target_node->mtn_state == MTN_REFERRAL_ON_UPDATE)) */

        if (*index > 0) {
            /* we have already returned this referral
             * send back NULL to jump to next node 
             */
            *be = NULL;
            *referral = NULL;
            result = LDAP_SUCCESS;
        } else {
            /* first time we hit this referral -> return it
             * set the be variable to NULL to indicate we use a referral
             * and increment index to rememeber later that we already
             * returned this referral
             */
            *be = NULL;
            *referral = (target_node->mtn_referral_entry ?
                         slapi_entry_dup(target_node->mtn_referral_entry) :
                         NULL);
            (*index)++;
            if (NULL == *referral) {
                if (errorbuf) {
                    PR_snprintf(errorbuf, BUFSIZ,
                    "Mapping tree node for %s is set to return a referral,"
                        " but no referral is configured for it",
                        slapi_sdn_get_ndn(target_node->mtn_subtree));
                }
                result = LDAP_OPERATIONS_ERROR;
            } else {
                result = LDAP_SUCCESS;
            }
        }
    }

    if (result == LDAP_SUCCESS) {
        if (*be) {
            slapi_log_error(SLAPI_LOG_ARGS, NULL,
                "mapping tree selected backend : %s\n",
                slapi_be_get_name(*be));
            slapi_be_Rlock(*be);
        } else if (*referral) {
            slapi_log_error(SLAPI_LOG_ARGS, NULL,
                "mapping tree selected referral at node : %s\n",
                slapi_sdn_get_dn(target_node->mtn_subtree));
        }
    }

    return result;
}

/* 
 * Description:
 * Finds the best match for the targetdn from the children of parent.  Uses 
 * slapi_sdn_issuffix and the number of rdns to pick the best node.
 * 
 * Arguments:
 * parent is a pointer to a mapping tree node.
 * targetdn is the dn we're trying to find the best match for.
 *
 * Returns:
 * A pointer to the child of parent that best matches the targetdn.  NULL
 * if there were no good matches.
 */
static mapping_tree_node *best_matching_child(mapping_tree_node *parent,
     const Slapi_DN *targetdn)
{
    mapping_tree_node *highest_match_node = NULL;
    mapping_tree_node *current;

    if(mapping_tree_freed){
        /* shutdown detected */
        return NULL;
    }
    for (current = parent->mtn_children; current;
        current = current->mtn_brother) {
        if (slapi_sdn_issuffix(targetdn, current->mtn_subtree)) {
            if ( (highest_match_node == NULL) ||
                 ((slapi_sdn_get_ndn_len(current->mtn_subtree)) >
                 slapi_sdn_get_ndn_len(highest_match_node->mtn_subtree)) ) {
                highest_match_node = current;
            }
        }
    }

    return highest_match_node;
}


/* 
 * look for the exact mapping tree node corresponding to a given entry dn
 */
static mapping_tree_node *
mtn_get_mapping_tree_node_by_entry(mapping_tree_node* node, const Slapi_DN *dn)
{
    mapping_tree_node *found_node = NULL;

    if(mapping_tree_freed){
        /* shutdown detected */
        return NULL;
    }

    if  (slapi_sdn_compare(node->mtn_subtree, dn) == 0)
    {
        return node;
    }

    if (node->mtn_children)
    {
        found_node = mtn_get_mapping_tree_node_by_entry(node->mtn_children, dn);
        if (found_node)
            return found_node;
    }

    if (node->mtn_brother)
    {
        found_node = mtn_get_mapping_tree_node_by_entry(node->mtn_brother, dn);
    }
    return found_node;
}
/* 
 * Description:
 * Gets a mapping tree node that best matches the given dn.  If the root
 * node is returned and the target dn is not "", then no match was found. 
 * 
 * Arguments:
 * dn is the target of the search.
 *
 * Returns:
 * The best matching node for the dn 
 * if nothing match, NULL is returned
 */
mapping_tree_node *
slapi_get_mapping_tree_node_by_dn(const Slapi_DN *dn)
{
    mapping_tree_node *current_best_match = mapping_tree_root;
    mapping_tree_node *next_best_match = mapping_tree_root;

    if(mapping_tree_freed){
        /* shutdown detected */
        return NULL;
    }
    /* Handle special case where the dn is "" and the mapping root
     * does not belong to the frontend-internal (DSE_BACKEND); 
     * it has been assigned to a different backend.
     * e.g: a container backend 
     */
    if ( sdn_is_nulldn(dn) && mapping_tree_root && mapping_tree_root->mtn_be[0] &&
         mapping_tree_root->mtn_be[0] != slapi_be_select_by_instance_name(DSE_BACKEND)) {
        return( mapping_tree_root );
    }

    /* Start at the root and walk down the tree to find the best match. */
    while (next_best_match) {
        current_best_match = next_best_match;
        next_best_match = best_matching_child(current_best_match, dn);
    }

    if (current_best_match == mapping_tree_root)
        return NULL;
    else
        return current_best_match;
}


static  mapping_tree_node *
get_mapping_tree_node_by_name(mapping_tree_node * node, char * be_name)
{
    int i;
    mapping_tree_node *found_node = NULL;

    if(mapping_tree_freed){
        /* shutdown detected */
        return NULL;
    }
    /* now search the backend in this node */
    i = 0;
    while ( ( i < node->mtn_be_count) &&
            (node->mtn_backend_names) &&
            (node->mtn_backend_names[i]) &&
            (strcmp(node->mtn_backend_names[i],be_name)))
    {
        i++;
    }

    if  ((i < node->mtn_be_count) &&
            (node->mtn_backend_names != NULL) &&
            (node->mtn_backend_names[i] != NULL))
    {
        return node;
    }

    if (node->mtn_children)
    {
        found_node = get_mapping_tree_node_by_name(node->mtn_children, be_name);
        if (found_node)
            return found_node;
    }

    if (node->mtn_brother)
    {
        found_node = get_mapping_tree_node_by_name(node->mtn_brother, be_name);
    }
    return found_node;
}

/* 
 * Description: construct the dn of the configuration entry for the
 *              node originated at the root. The function just constructs
 *              the dn it does not verify that the entry actually exist.
 *              The format of the dn is
 *                  cn="<normalized root>",cn=mapping tree,cn=config
 *              
 * Arguments:   root - root of the node
 *
 * Returns:     dn of the configuration entry if successful and null otherwise.
 */
char* 
slapi_get_mapping_tree_node_configdn (const Slapi_DN *root)
{
    char *dn;

    if(mapping_tree_freed){
        /* shutdown detected */
        return NULL;
    }
    if (root == NULL)
        return NULL;

    dn = slapi_ch_smprintf("cn=\"%s\",%s", slapi_sdn_get_dn(root), MAPPING_TREE_BASE_DN);

    return dn;
}

/* 
 * Description: this function returns root of the subtree to which the node applies
 *              
 * Arguments:   node - mapping tree node
 *
 * Returns:     root of the subtree if function is successful and NULL otherwise.
 */

const Slapi_DN* slapi_get_mapping_tree_node_root (const mapping_tree_node *node)
{
    if (node)
        return node->mtn_subtree;
    else
        return NULL;
}

/* GB : there is a potential problems with this function 
 * when several backends are used
 */
PRBool slapi_mapping_tree_node_is_set (const mapping_tree_node *node, PRUint32 flag)
{
    if (flag & SLAPI_MTN_LOCAL)
        return PR_TRUE;

    if (flag & SLAPI_MTN_PRIVATE)
        return ((node->mtn_be_count>0) && node->mtn_be && node->mtn_be[0] && node->mtn_private);

    if (flag & SLAPI_MTN_READONLY)
        return ((node->mtn_be_count>0) &&  node->mtn_be && node->mtn_be[0] && node->mtn_be[0]->be_readonly);

    return PR_FALSE;
}

/* 
 * Description: this function returns root of the subtree to which the node applies
 *              
 * Arguments:   node
 *
 * Returns:     dn of the parent of mapping tree node configuration entry.
 */

const char* slapi_get_mapping_tree_config_root ()
{
    return MAPPING_TREE_BASE_DN;
}

/*
 * slapi_be_select() finds the backend that should be used to service dn.
 * If no backend with an appropriate suffix is configured, the default backend
 * is returned.  This function never returns NULL.
 */
Slapi_Backend *
slapi_be_select( const Slapi_DN *sdn ) /* JCM - The name of this should change??? */
{
    Slapi_Backend *be;
    mapping_tree_node *node= slapi_get_mapping_tree_node_by_dn(sdn);
    if(node!=NULL)
        be= node->mtn_be[0];
    else
        be = NULL;

    if(be==NULL)
        be= defbackend_get_backend();

    return be;
}

/* Check if the dn targets an internal reserved backends */
int
slapi_on_internal_backends(const Slapi_DN *sdn)
{
        char *backend_names[] = {"frontend-internal", "schema-internal"};
        int internal = 1;
        int numOfInternalBackends = 2;                           
        int count;
                                                                 
        Slapi_Backend *internal_be;
                                                                 
        Slapi_Backend *be = slapi_be_select(sdn);
    
        
        for (count=0; count < numOfInternalBackends ; ++count){ 
        /* the internal backends are always in the begining of the list 
         * so should not be very inefficient 
         */ 
            internal_be = slapi_be_select_by_instance_name(backend_names[count]);
            if(be == internal_be){
                return internal; 
            }
        }
    return 0;
}   

/* Some of the operations are not allowed from the plugins
 * but default to specialized use of those operations 
 * e.g rootDse search, ConfigRoot searches
 * cn=config, cn=schema etc
 */

int slapi_op_reserved(Slapi_PBlock *pb)
{   
    int scope=LDAP_SCOPE_BASE;
    int reservedOp=0;
    int op_type;
    Slapi_Operation *op = NULL;
    Slapi_DN *target_sdn=NULL;
        
    slapi_pblock_get(pb, SLAPI_OPERATION, &op);
    slapi_pblock_get(pb, SLAPI_OPERATION_TYPE, &op_type);
    /* Get the target for this op */
    target_sdn = operation_get_target_spec (op);

    if( op_type == SLAPI_OPERATION_SEARCH){
        slapi_pblock_get(pb, SLAPI_SEARCH_SCOPE, &scope);
        if( sdn_is_nulldn(target_sdn) && (scope == LDAP_SCOPE_BASE) ){
                reservedOp = 1;
        } 
                
    }            
    
    if(slapi_on_internal_backends(target_sdn)){
        reservedOp = 1;
    }
                    
    return reservedOp;
}



/* 
 * Returns the name of the Backend that contains specified DN, 
 * if only one matches. Otherwise returns NULL
 * The name is pointing to the mapping tree structure 
 * and should not be altered.
 */
const char *
slapi_mtn_get_backend_name( const Slapi_DN *sdn)
{
    mapping_tree_node *node= slapi_get_mapping_tree_node_by_dn(sdn);
    if ((node != NULL) && 
        (node->mtn_be_count == 1) &&
        (node->mtn_backend_names != NULL))
        /* There's only one name, return it */
        return node->mtn_backend_names[0];
    else
        return NULL;
}

/* Check if the backend that contains specified DN exists */
int 
slapi_be_exist(const Slapi_DN *sdn) /* JCM - The name of this should change??? */
{
    Slapi_Backend *def_be = defbackend_get_backend();
    Slapi_Backend *be = slapi_be_select (sdn);

    return (be != def_be);
}

/* The two following functions can be used to 
 * parse the list of the root suffix of the DIT
 * Using 
 */
Slapi_DN *
slapi_get_first_suffix(void ** node, int show_private)
{
    mapping_tree_node * first_node = mapping_tree_root->mtn_children;
    *node = (void * ) first_node ;
    while (first_node && (first_node->mtn_private && (show_private == 0)))
            first_node = first_node->mtn_brother;
    return (first_node ? first_node->mtn_subtree : NULL);
}

Slapi_DN * 
slapi_get_next_suffix(void ** node, int show_private)
{
    mapping_tree_node * next_node = *node;

    if (next_node == NULL)
        return NULL;
        
    next_node = next_node->mtn_brother;
    while (next_node && (next_node->mtn_private && (show_private == 0)))
            next_node = next_node->mtn_brother;
    *node = next_node;
    return (next_node ? next_node->mtn_subtree : NULL);
}

/* check if a suffix is a root of the DIT
 * return 1 if yes, 0 if no
 */
int slapi_is_root_suffix(Slapi_DN * dn)
{
    void * node;
    Slapi_DN * suffix = slapi_get_first_suffix (&node, 1);

    while (suffix)
    {
        if ( slapi_sdn_compare(dn, suffix) == 0 )
            return 1;
        suffix = slapi_get_next_suffix(&node, 1);
    }
    return 0 ;
}

/*
 * set referrals for the node
 * notes :
 *  - referral is consumed by this function
 *  - node must exist before calling this function
 *  - mapping tree node state is not changed by this function
 */
int
slapi_mtn_set_referral(const Slapi_DN *sdn, char ** referral)
{
    Slapi_PBlock pb;
    Slapi_Mods smods;
    int rc = LDAP_SUCCESS,i = 0, j = 0;
    char * node_dn;
    char **values = NULL;
    int do_modify = 0;

    slapi_mods_init (&smods, 0);
    node_dn = slapi_get_mapping_tree_node_configdn(sdn);
    if(!node_dn){
     /* shutdown has been detected */
        return LDAP_OPERATIONS_ERROR;
    }
    
    if ((referral == NULL) || (referral[0] == NULL))
    {
        /* NULL referral means we want to delete existing referral 
         */
        slapi_mods_add(&smods, LDAP_MOD_DELETE, "nsslapd-referral", 0, NULL);
        do_modify = 1;
    }
    else
    {
        int changes = 1;
        int referralCount = 0;

        for(; referral[referralCount]; referralCount++);
        if ( (values = slapi_mtn_get_referral(sdn)) != NULL )
        {
            /* Check if there are differences between current values and values to be set */
            
            for (i=0; values[i]; i++);
            if (i == referralCount) {
                changes = 0;
                for (i=0;values[i];i++){
                    int found = 0;
                    for (j=0;referral[j];j++){
                        if (strcmp(values[i], referral[j]) == 0){
                            found = 1;
                            break;
                        }
                    }
                    if (!found) {
                        changes = 1;
                        break;
                    }
                }
            }

            i=0;
            while(values[i])
                slapi_ch_free((void**)&values[i++]);
            slapi_ch_free((void**)&values);

        }
        if (changes){
            Slapi_Value *val;
            Slapi_Value ** svals = NULL;

            do_modify = 1;
            for (j =0; referral[j];j++) {
                val = slapi_value_new_string(referral[j]);
                valuearray_add_value(&svals, val);
                slapi_value_free(&val);
            }
            slapi_mods_add_mod_values(&smods, LDAP_MOD_REPLACE, "nsslapd-referral", svals);
            valuearray_free(&svals);
        }
    }
    
    if ( do_modify )
    {
        pblock_init (&pb);
        slapi_modify_internal_set_pb (&pb, node_dn,
                                slapi_mods_get_ldapmods_byref(&smods), NULL,
                                NULL, (void *) plugin_get_default_component_id(), 0);
        slapi_modify_internal_pb (&pb);

        slapi_pblock_get (&pb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
        pblock_done(&pb);
    }

    slapi_mods_done(&smods);
    slapi_ch_free((void **) &node_dn);

    return rc;
} 

/*
 * Change the state of a mapping tree node entry
 * notes :
 *   - sdn argument is the dn of the subtree of the DIT managed by this node 
 *     not the dn of the mapping tree entry
 *   - mapping tree node must exist before calling this function
 */
int
slapi_mtn_set_state(const Slapi_DN *sdn, char *state)
{
    Slapi_PBlock pb;
    Slapi_Mods smods;
    int rc = LDAP_SUCCESS;
    char * node_dn;
    char * value;

    node_dn = slapi_get_mapping_tree_node_configdn(sdn);
    if(!node_dn){
     /* shutdown has been detected */
        return LDAP_OPERATIONS_ERROR;
    }

    if ( (value = slapi_mtn_get_state(sdn)) != NULL )
    {
        if ( strcasecmp(value, state) == 0 )
        {
            /* Same state, don't change anything */
            slapi_ch_free((void **) &value);
            slapi_ch_free((void **) &node_dn);
            return rc;
        }
    }
    
    /* Otherwise, means that the state has changed, modify it */
    slapi_mods_init (&smods, 1);
    slapi_mods_add(&smods, LDAP_MOD_REPLACE, "nsslapd-state", strlen(state), state);
    pblock_init (&pb);
    slapi_modify_internal_set_pb (&pb, node_dn,
                            slapi_mods_get_ldapmods_byref(&smods), NULL,
                            NULL, (void *) plugin_get_default_component_id(), 0);
    slapi_modify_internal_pb (&pb);

    slapi_pblock_get (&pb, SLAPI_PLUGIN_INTOP_RESULT, &rc);

    slapi_mods_done(&smods);
    slapi_ch_free((void **) &node_dn);
    pblock_done(&pb);
    slapi_ch_free((void **) &value);

    return rc;
}

/*
  returns a copy of the attr - the caller must slapi_attr_free it
*/
Slapi_Attr *
mtn_get_attr(char* node_dn, char * type)
{
    Slapi_PBlock pb;
    int res = 0;
    Slapi_Entry **entries = NULL;
    Slapi_Attr *attr = NULL;
    Slapi_Attr *ret_attr = NULL;
    char **attrs = NULL;

    attrs = (char **)slapi_ch_calloc(2, sizeof(char *));
    attrs[0] = slapi_ch_strdup(type);
    pblock_init(&pb);
    slapi_search_internal_set_pb(&pb, node_dn, LDAP_SCOPE_BASE,
        "objectclass=nsMappingTree", attrs, 0, NULL, NULL,
         (void *) plugin_get_default_component_id(), 0);
    slapi_search_internal_pb(&pb);
    slapi_pblock_get(&pb, SLAPI_PLUGIN_INTOP_RESULT, &res);

    if (res != LDAP_SUCCESS) {
        goto done;
    }

    slapi_pblock_get(&pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries);
    if (NULL == entries || NULL == entries[0]) {
        goto done;
    }

    /* always at most one entry entries[0] */
    res = slapi_entry_attr_find(entries[0], type, &attr);
    if (res == 0)
        /* we need to make a copy here so we can free the search results */
        ret_attr = slapi_attr_dup(attr);

    slapi_free_search_results_internal(&pb);

done:
    slapi_ch_free((void **)&attrs[0]);
    slapi_ch_free((void **)&attrs);
    pblock_done(&pb);
    return ret_attr;
}

/*
 * Get the referral associated to the mapping tree node entry
 * notes :
 *   - sdn argument is the dn of the subtree of the DIT managed by this node 
 *     not the dn of the mapping tree entry
 *   - return NULL if no referral 
 *   - caller is reponsible for freeing the returned referrals
 */
char **
slapi_mtn_get_referral(const Slapi_DN *sdn)
{
    int i, hint, nb;
    char * node_dn;
    Slapi_Attr *attr;
    char ** referral = NULL;
    Slapi_Value *val = NULL;

    node_dn = slapi_get_mapping_tree_node_configdn(sdn);
    if(!node_dn){
     /* shutdown has been detected */
        return NULL;
    }

    attr = mtn_get_attr(node_dn, "nsslapd-referral");

    if (attr)
    {
        /* if there are some referrals set in the entry build a list 
         * to be returned to the caller 
         */
        slapi_attr_get_numvalues(attr, &nb);
        referral = (char**) slapi_ch_malloc(sizeof(char*) * (nb+1));
        hint = slapi_attr_first_value(attr, &val);
        i = 0;

        while (val)
        {
            referral[i++] = slapi_ch_strdup(slapi_value_get_string(val));
            hint = slapi_attr_next_value(attr, hint, &val);
        }
        referral[i] = NULL;
        slapi_attr_free(&attr);
    }

    slapi_ch_free((void **) &node_dn);
    return referral;
}

/*
 * Get the state of a mapping tree node entry
 * notes :
 *   - sdn argument is the dn of the subtree of the DIT managed by this node 
 *     not the dn of the mapping tree entry
 *     - the state is return in a newly allocated string that must be freed by
 *     the caller
 */
char *
slapi_mtn_get_state(const Slapi_DN *sdn)
{
    int hint;
    char * node_dn;
    Slapi_Attr *attr;
    char * state = NULL;
    Slapi_Value *val = NULL;

    node_dn = slapi_get_mapping_tree_node_configdn(sdn);
    if(!node_dn){
     /* shutdown has been detected */
        return NULL;
    }

    attr = mtn_get_attr(node_dn, "nsslapd-state");

    if (attr)
    {
        /* entry state was found */
        hint = slapi_attr_first_value(attr, &val);
        state = slapi_ch_strdup(slapi_value_get_string(val));
        slapi_attr_free(&attr);
    }

    slapi_ch_free((void **) &node_dn);
    return state;
}

static void
mtn_internal_be_set_state(Slapi_Backend *be, int state)
{
    mapping_tree_node * node;
    char * be_name;
    int i;
    int change_callback = 0;
    int old_state;

    mtn_wlock();
    be_name = slapi_ch_strdup(slapi_be_get_name(be));
    node = get_mapping_tree_node_by_name(mapping_tree_root, be_name);
    if (node == NULL)
    {
        LDAPDebug(LDAP_DEBUG_TRACE,
            "Warning: backend %s is not declared in mapping tree\n",
             be_name, 0 ,0);
        goto done;
    }
    
        
    /* now search the backend in this node */
    i = 0;
    while ( (i < node->mtn_be_count) &&
            (node->mtn_backend_names) &&
            (node->mtn_backend_names[i]) &&
            (strcmp(node->mtn_backend_names[i],be_name)))
    {
        i++;
    }

    if  ( (i >= node->mtn_be_count) || (node->mtn_backend_names == NULL) ||
          (node->mtn_backend_names[i] == NULL) )
    {
        /* backend is not declared in the mapping tree node
         * print out a warning
         */
        LDAPDebug(LDAP_DEBUG_TRACE,
            "Warning: backend %s is not declared in mapping node entry\n",
             be_name, 0 ,0);
        goto done;
    }

    change_callback = 1;
    old_state = node->mtn_be_states[i];

    /* OK we found the backend at last, now do the real job: set the state */
    switch (state)
    {
        case SLAPI_BE_STATE_OFFLINE:
            node->mtn_be[i] = be;
            node->mtn_be_states[i] = SLAPI_BE_STATE_OFFLINE;
        break;

        case SLAPI_BE_STATE_ON:
            node->mtn_be[i] = be;
            node->mtn_be_states[i] = SLAPI_BE_STATE_ON;
        break;

        case SLAPI_BE_STATE_DELETE:
            node->mtn_be[i] = NULL;
            node->mtn_be_states[i] = SLAPI_BE_STATE_DELETE;
        break;
    }    

done:
    mtn_unlock();
    if (change_callback)
        mtn_be_state_change(be_name, old_state, state);
    slapi_ch_free( (void **) &be_name);
}

/*
 * This procedure must be called by previously stopped backends
 * to signal that they have started and are ready to process requests
 * The backend must be fully ready to handle requests before calling this 
 * procedure
 * At startup tiem it is not mandatory for the backends to 
 * call this procedure: backends are assumed on by default
 */
void
slapi_mtn_be_started(Slapi_Backend *be)
{
    /* Find the node where this backend stay
     * then update the backend structure
     * In the long term, the backend should have only one suffix and
     * stay in only one node as for now, check all suffixes 
     * Rq : since mapping tree is initiatized very soon in the server
     * startup, we can be sure at that time that the mapping
     * tree is initialized
     */

    mtn_internal_be_set_state(be, SLAPI_BE_STATE_ON);
}

/* these procedure can be called when backends need to be put in maintenance mode
 * after call to slapi_mtn_be_disable, the backend will still be known
 * by a server but the mapping tree won't route requests to it anymore 
 * The slapi_mtn_be_enable function enable to route requests to the backend
 * again 
 * the slapi_mtn_be_disable function only returns when there is no more 
 * request in progress in the backend
 */
void
slapi_mtn_be_disable(Slapi_Backend *be)
{
    mtn_internal_be_set_state(be, SLAPI_BE_STATE_OFFLINE);

    /* the two following lines can seem weird, but they allow to check that no 
     * LDAP operation is in progress on the backend
     */
    slapi_be_Wlock(be);
    slapi_be_Unlock(be);
}

void
slapi_mtn_be_enable(Slapi_Backend *be)
{
    mtn_internal_be_set_state(be, SLAPI_BE_STATE_ON);
}

/*
 * This procedure must be called by backends before stopping
 * if some operations are in progress when this procedure
 * is called, this procedure will block until completion
 * of these operations
 * The backend must wait return from this procedure before stopping operation
 * Backends must serve operation until the return from this procedure.
 * Once this procedure return they will not be issued request anymore
 * and they have been removed from the server list of backends
 * It is also the bakend responsability to free the Slapi_Backend structures
 * that was given by slapi_be_new at startup time.
 * Should the backend start again, it would need to issue slapi_be_new again
 */
void
slapi_mtn_be_stopping(Slapi_Backend *be)
{
    mtn_internal_be_set_state(be, SLAPI_BE_STATE_DELETE);

    /* the two following lines can seem weird, but they allow to check that no 
     * LDAP operation is in progress on the backend
     */
    slapi_be_Wlock(be);
    slapi_be_Unlock(be);

    slapi_be_stopping(be);
}

/*
 * Switch a backend into read-only mode, or back to read-write mode.
 * To switch to read-only mode, we need to wait for all pending operations
 * to finish.
 */
void
slapi_mtn_be_set_readonly(Slapi_Backend *be, int readonly)
{
    if (readonly) {
        slapi_be_Wlock(be);
        slapi_be_set_readonly(be, 1);
        slapi_be_Unlock(be);
    } else {
        slapi_be_set_readonly(be, 0);
    }
}


#ifdef DEBUG
static int lock_count = 0;
#endif

void mtn_wlock()
{
    PR_RWLock_Wlock(myLock);
#ifdef DEBUG
    lock_count--;
    LDAPDebug(LDAP_DEBUG_ARGS, "mtn_wlock : lock count : %d\n", lock_count, 0, 0);
#endif
}

void mtn_lock()
{
    PR_RWLock_Rlock(myLock);
#ifdef DEBUG
    lock_count++;
    LDAPDebug(LDAP_DEBUG_ARGS, "mtn_lock : lock count : %d\n", lock_count, 0, 0);
#endif
}

void mtn_unlock()
{
    
#ifdef DEBUG
    if (lock_count > 0)
        lock_count--;
    else if (lock_count < 0)
        lock_count++;
    else
        lock_count = (int) 11111111;   /* this happening means problems */
    LDAPDebug(LDAP_DEBUG_ARGS, "mtn_unlock : lock count : %d\n", lock_count, 0, 0);
#endif
    PR_RWLock_Unlock(myLock);
}

#ifdef TEST_FOR_REGISTER_CHANGE
void my_test_fnct1(void *handle, char *be_name, int old_state, int new_state)
{
        slapi_log_error(SLAPI_LOG_ARGS, NULL,
        "my_test_fnct1 : handle %d, be %s, old state %d, new state %d\n",
        handle,be_name, old_state, new_state);

        if (old_state == 2)
            slapi_unregister_backend_state_change(handle);
}

void my_test_fnct2(void *handle, char *be_name, int old_state, int new_state)
{
        slapi_log_error(SLAPI_LOG_ARGS, NULL,
        "my_test_fnct2 : handle %d, be %s, old state %d, new state %d\n",
        handle, be_name, old_state, new_state);
}

void test_register()
{
    slapi_register_backend_state_change((void *) 1234, my_test_fnct1);
    slapi_register_backend_state_change((void *) 4321, my_test_fnct2);
}
#endif

#ifdef DEBUG
static void dump_mapping_tree(mapping_tree_node *parent, int depth)
{
    mapping_tree_node *current = NULL;
    static char dump_indent[256];
    int i;

    if (depth == 0)
    {
        LDAPDebug(LDAP_DEBUG_ANY, "dump_mapping_tree\n", 0, 0, 0);
    }
    dump_indent[0] = '\0';
    for (i = 0; i < depth; i++)
        PL_strcatn(dump_indent, sizeof(dump_indent), "  ");
    for (current = parent->mtn_children; current;
         current = current->mtn_brother)
    {
        if (strlen(current->mtn_subtree->dn) == 0)
        {
            LDAPDebug(LDAP_DEBUG_ANY, "MT_DUMP: %s%s (0x%x)\n",
                dump_indent, "none", current);
        }
        else
        {
            LDAPDebug(LDAP_DEBUG_ANY, "MT_DUMP: %s%s (0x%x)\n",
                dump_indent, current->mtn_subtree->dn, current);
        }
        dump_mapping_tree(current, depth+1);
    }
    return;
}
#endif