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 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774
|
/** BEGIN COPYRIGHT BLOCK
* Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
* Copyright (C) 2021 Red Hat, Inc.
* All rights reserved.
*
* License: GPL (version 3 or any later version).
* See LICENSE for details.
* 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, int rootmode);
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 */
int mtn_dstr_plg_rootmode; /* determines how to process root updates in distribution */
mtn_distrib_fct mtn_dstr_plg; /* pointer to the actual ditribution function */
void *mtn_extension; /* plugins can extend a mapping tree node */
};
/*
* A temporary value used to sort the order of mapping tree nodes
* and how they should be built into a suffix.
*/
struct mt_suffix_ord
{
Slapi_DN *mtn_subtree;
size_t index;
};
#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 Slapi_RWLock *myLock = NULL; /* global lock on the mapping tree structures */
static mapping_tree_node *mapping_tree_root = NULL;
static int32_t mapping_tree_inited = 0;
static int32_t 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 */
/* Note: This DN is no need to be normalized. */
#define MAPPING_TREE_BASE_DN "cn=mapping tree,cn=config"
void mtn_wlock(void);
void mtn_lock(void);
void mtn_unlock(void);
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, size_t ebuflen);
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);
static int _mtn_update_config_param(int op, char *type, char *strvalue);
#ifdef DEBUG
#ifdef USE_DUMP_MAPPING_TREE
static void dump_mapping_tree(mapping_tree_node *parent, int depth);
#endif
#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(void)
{
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, Slapi_DN *target_sdn)
{
int i;
struct berval bv0, bv1, *bvals[3];
Slapi_Entry *anEntry;
if (url_array == NULL)
return NULL;
anEntry = slapi_entry_alloc();
slapi_entry_set_sdn(anEntry, target_sdn);
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, int plg_rootmode)
{
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, 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_rootmode = plg_rootmode;
node->mtn_dstr_plg = plg;
slapi_log_err(SLAPI_LOG_TRACE, "mapping_tree_node_new",
"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)
{
child->mtn_brother = parent->mtn_children;
parent->mtn_children = child;
#ifdef DEBUG
#ifdef USE_DUMP_MAPPING_TREE
dump_mapping_tree(mapping_tree_root, 0);
#endif
#endif
}
/* 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 *origcn = NULL;
char *cn = NULL;
Slapi_Value *val = NULL;
Slapi_DN *subtree = NULL;
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
*/
origcn = cn = slapi_ch_strdup(slapi_value_get_string(val));
if (cn) {
if (*cn == '"') {
char *ptr = NULL;
cn++; /* skipping the starting '"' */
ptr = PL_strnrchr(cn, '"', strlen(cn));
if (ptr) {
*ptr = '\0';
}
}
subtree = slapi_sdn_new_dn_byval(cn);
slapi_ch_free_string(&origcn);
}
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 {
slapi_log_err(SLAPI_LOG_WARNING, "mtn_state_to_int",
"Unknown state, %s, for mapping tree node %s."
" Defaulting to DISABLED\n",
state_string, slapi_entry_get_dn(entry));
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);
hint = slapi_attr_first_value(attr, &val);
if (NULL == val) {
slapi_log_err(SLAPI_LOG_WARNING, "mtn_get_referral_from_entry",
"The nsslapd-referral attribute has no value for the mapping tree node %s\n",
slapi_entry_get_dn(entry));
return NULL;
}
referral = (char **)slapi_ch_malloc(sizeof(char *) * (nb + 1));
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)) {
slapi_log_err(SLAPI_LOG_ERR, "get_backends_from_attr",
"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);
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:
* Free the data allocated for mapping tree node arrays
*/
static void
free_mapping_tree_node_arrays(backend ***be_list, char ***be_names, int **be_states, int *be_list_count)
{
int i;
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, mapping_tree_node *parent_node, PRBool check_be)
{
Slapi_DN *subtree = NULL;
const char *tmp_ndn;
int be_list_count = 0;
int be_list_size = 0;
backend **be_list = NULL;
char **be_names = NULL;
int *be_states = NULL;
char *plugin_funct = NULL;
char *plugin_lib = NULL;
int plugin_rootmode = CHAIN_ROOT_UPDATE_REJECT;
mtn_distrib_fct plugin = NULL;
char **referral = NULL;
int state = MTN_DISABLED;
Slapi_Attr *attr = NULL;
mapping_tree_node *node = NULL;
int rc = 0;
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) {
slapi_log_err(SLAPI_LOG_WARNING, "mapping_tree_entry_add",
"Unable to determine the subtree represented by the mapping tree node %s\n",
slapi_entry_get_dn(entry));
return lderr;
}
/* Verify there is a matching backend for this suffix */
if (check_be) {
const char *mt_be_name;
char *cookie = NULL;
int32_t found_be = 0;
/* get the backend name for this mapping tree node */
mt_be_name = slapi_entry_attr_get_ref(entry, "nsslapd-backend");
be = slapi_get_first_backend(&cookie);
while (be) {
char *be_name = slapi_be_get_name(be);
if (mt_be_name && be_name &&
strcasecmp(be_name, mt_be_name) == 0 &&
slapi_sdn_compare(subtree, be->be_suffix) == 0)
{
found_be = 1;
break;
}
be = (backend *)slapi_get_next_backend(cookie);
}
slapi_ch_free((void **)&cookie);
if (!found_be) {
slapi_log_err(SLAPI_LOG_ERR, "mapping_tree_entry_add",
"The subtree %s does not match any existing backends, and will not be created.\n",
slapi_sdn_get_dn(subtree));
slapi_sdn_free(&subtree);
return LDAP_UNWILLING_TO_PERFORM;
}
}
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_err(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)) {
slapi_log_err(SLAPI_LOG_WARNING, "mapping_tree_entry_add",
"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));
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)) {
goto free_and_return;
}
if (NULL == be_list) {
slapi_log_err(SLAPI_LOG_WARNING, "mapping_tree_entry_add",
"The nsslapd-backend attribute has no value for the mapping tree node %s\n",
slapi_entry_get_dn(entry));
continue;
}
} else if (!strcasecmp(type, "nsslapd-referral")) {
slapi_ch_array_free(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) {
slapi_log_err(SLAPI_LOG_WARNING, "mapping_tree_entry_add",
"Can't determine the state of the mapping tree node %s\n",
slapi_entry_get_dn(entry));
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) {
slapi_log_err(SLAPI_LOG_WARNING, "mapping_tree_entry_add",
"The nsslapd-distribution-plugin attribute has no value for the mapping tree node %s\n",
slapi_entry_get_dn(entry));
continue;
}
slapi_ch_free_string(&plugin_lib);
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) {
slapi_log_err(SLAPI_LOG_WARNING, "mapping_tree_entry_add",
"The nsslapd-distribution-plugin attribute has no value for the mapping tree node %s\n",
slapi_entry_get_dn(entry));
continue;
}
slapi_ch_free_string(&plugin_funct);
plugin_funct = slapi_ch_strdup(slapi_value_get_string(val));
} else if (!strcasecmp(type, "nsslapd-distribution-root-update")) {
const char *sval;
slapi_attr_first_value(attr, &val);
if (NULL == val) {
slapi_log_err(SLAPI_LOG_WARNING, "mapping_tree_entry_add",
"The nsslapd-distribution-plugin attribute has no value for the mapping tree node %s\n",
slapi_entry_get_dn(entry));
continue;
}
sval = slapi_value_get_string(val);
if (strcmp(sval, "reject") == 0)
plugin_rootmode = CHAIN_ROOT_UPDATE_REJECT;
else if (strcmp(sval, "local") == 0)
plugin_rootmode = CHAIN_ROOT_UPDATE_LOCAL;
else if (strcmp(sval, "referral") == 0)
plugin_rootmode = CHAIN_ROOT_UPDATE_REFERRAL;
else
slapi_log_err(SLAPI_LOG_WARNING, "mapping_tree_entry_add",
"The nsslapd-distribution-root-update attribute has undefined value (%s) for the mapping tree node %s\n",
sval, slapi_entry_get_dn(entry));
}
}
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) {
slapi_log_err(SLAPI_LOG_ERR, "mapping_tree_entry_add",
"Default container has not been created for the NULL SUFFIX node.\n");
lderr = -1;
goto free_and_return;
}
be_list_size = 1;
be_list_count = 0;
/* We're in a loop and potentially overwriting these pointers so free them first */
free_mapping_tree_node_arrays(&be_list, &be_names, &be_states, &be_list_count);
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(DEFBACKEND_NAME); /* "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)) {
slapi_log_err(SLAPI_LOG_ERR,
"Node %s must define a backend\n",
slapi_entry_get_dn(entry), 0, 0);
goto free_and_return;
}
if (((state == MTN_REFERRAL) || (state == MTN_REFERRAL_ON_UPDATE)) && (referral == NULL)) {
slapi_log_err(SLAPI_LOG_ERR,
"Node %s must define referrals to be in referral state\n",
slapi_entry_get_dn(entry), 0, 0);
goto free_and_return;
}
if (plugin_lib && plugin_funct) {
plugin = (mtn_distrib_fct)sym_load(plugin_lib, plugin_funct,
"Entry Distribution", 1);
if (plugin == NULL) {
slapi_log_err(SLAPI_LOG_ERR, "mapping_tree_entry_add",
"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()));
goto free_and_return;
}
} else if ((plugin_lib == NULL) && (plugin_funct == NULL)) {
/* nothing configured -> OK */
plugin = NULL;
} else {
/* only one parameter configured -> ERROR */
slapi_log_err(SLAPI_LOG_ERR, "mapping_tree_entry_add",
"Node %s must define both lib and funct for distribution plugin\n",
slapi_entry_get_dn(entry));
goto free_and_return;
}
/* Now we can create the node for this mapping tree entry. */
/* subtree is consumed. */
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, plugin_rootmode);
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_err(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;
free_and_return:
slapi_sdn_free(&subtree);
slapi_ch_array_free(referral);
slapi_ch_free_string(&plugin_funct);
slapi_ch_free_string(&plugin_lib);
free_mapping_tree_node_arrays(&be_list, &be_names, &be_states, &be_list_count);
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);
}
static int
mt_suffix_ord_cmp(const void *p1, const void *p2)
{
const struct mt_suffix_ord *m1 = p1;
const struct mt_suffix_ord *m2 = p2;
const char *ndn1 = slapi_sdn_get_ndn(m1->mtn_subtree);
const char *ndn2 = slapi_sdn_get_ndn(m2->mtn_subtree);
if (ndn1 == ndn2) {
return 0;
} else if (ndn1 == NULL) {
return -1;
} else if (ndn2 == NULL) {
return 1;
}
size_t l1 = strlen(ndn1);
size_t l2 = strlen(ndn2);
if (l1 == l2) {
return 0;
} else if (l1 < l2) {
return -1;
} else {
return 1;
}
}
/*
* Handles compatibility option to revert github issue 4373 change
* https://github.com/389ds/389-ds-base/issues/4373
* and be able to disconnect a sub-suffix from its parent
* by adding orphan: true in the mapping tree entry
*/
static int
is_orphan(const Slapi_Entry *entry)
{
Slapi_Value *v = NULL;
Slapi_Attr *a = NULL;
struct berval bv;
bv.bv_val = "true";
bv.bv_len = 4;
slapi_entry_attr_find(entry, "orphan", &a);
return (a && VALUE_PRESENT == attr_value_find_wsi(a, &bv, &v));
}
static int
mapping_tree_node_build_tree(void)
{
Slapi_Entry **entries = NULL;
char *filter = NULL;
int result = 0;
Slapi_PBlock *pb = slapi_pblock_new();
filter = slapi_ch_smprintf("(objectclass=nsMappingTree)");
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, &result);
if (result != LDAP_SUCCESS) {
slapi_log_err(SLAPI_LOG_WARNING, "mapping_tree_node_build_tree",
"Mapping tree unable to read %s: %d\n", MAPPING_TREE_BASE_DN, result);
result = -1;
goto build_tree_done;
}
slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries);
if (NULL == entries) {
slapi_log_err(SLAPI_LOG_WARNING, "mapping_tree_node_build_tree",
"No mapping tree node entries found under %s\n", MAPPING_TREE_BASE_DN);
result = -1;
goto build_tree_done;
}
/*
* Sort the entries by their suffix. We do this by making a temp
* array, and putting in a copy of the suffix and it's index to the
* entries. We then use this sorted suffix array to then offset through
* all the entries.
*/
int ent_count = 0;
slapi_pblock_get(pb, SLAPI_NENTRIES, (void *)&ent_count);
if (ent_count == 0) {
/*
* It is invalid to do a calloc of 0, so if we have no entries. we just return.
*/
result = 0;
goto build_tree_done;
}
struct mt_suffix_ord *ordered_suffixes = (struct mt_suffix_ord *)slapi_ch_calloc(ent_count, sizeof(struct mt_suffix_ord));
/* Assert the last value is null, and that we don't sigsegv */
PR_ASSERT(entries[ent_count] == NULL);
for (size_t i = 0; i < ent_count; i++) {
/* Set where we are in entries */
ordered_suffixes[i].index = i;
/* Add the suffix */
ordered_suffixes[i].mtn_subtree = get_subtree_from_entry(entries[i]);
}
/* Sort the suffix refs */
qsort(ordered_suffixes, ent_count, sizeof(struct mt_suffix_ord), mt_suffix_ord_cmp);
for (size_t i = 0; i < ent_count; i++) {
struct mt_suffix_ord *m1 = &(ordered_suffixes[i]);
mapping_tree_node *child = NULL;
/* Locate the parent of this suffix. */
mapping_tree_node *parent = slapi_get_mapping_tree_node_by_dn(m1->mtn_subtree);
if (parent == NULL || is_orphan(entries[m1->index])) {
parent = mapping_tree_root;
}
/* Create the MT node for it */
PR_ASSERT(entries[m1->index]);
if (mapping_tree_entry_add(entries[m1->index], &child, parent, PR_FALSE) != LDAP_SUCCESS) {
slapi_log_err(SLAPI_LOG_ERR, "mapping_tree_node_build_tree",
"Could not add mapping tree node %s\n",
slapi_entry_get_dn(entries[m1->index]));
result = -1;
break;
}
/* attach the node to it's parent. */
PR_ASSERT(child->mtn_parent == parent);
mapping_tree_node_add_child(parent, child);
}
/* Finally cleanup. */
for (size_t i = 0; i < ent_count; i++) {
slapi_sdn_free(&(ordered_suffixes[i].mtn_subtree));
}
slapi_ch_free((void **)&ordered_suffixes);
build_tree_done:
slapi_free_search_results_internal(pb);
slapi_pblock_destroy(pb);
slapi_ch_free((void **)&filter);
return result;
}
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 __attribute__((unused)),
Slapi_Entry *entryAfter,
int *returncode,
char *returntext,
void *arg __attribute__((unused)))
{
LDAPMod **mods;
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_rootmode = CHAIN_ROOT_UPDATE_REJECT;
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 (size_t i = 0; (mods != NULL) && (mods[i] != NULL); i++) {
if (strcasecmp(mods[i]->mod_type, "cn") == 0) {
mapping_tree_node *parent_node;
/* if we are deleting this attribute the new parent
* node will be mapping_tree_root
*/
if (SLAPI_IS_MOD_DELETE(mods[i]->mod_op)) {
parent_node = mapping_tree_root;
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, "cn") == 0) &&
SLAPI_IS_MOD_ADD(mods[i]->mod_op)) {
/* Allow to add an additional cn.
* e.g., cn: "<suffix>" for the backward compatibility.
* No need to update the mapping tree node itself.
*/
/*
* We don't allow renaming backend suffixes, so this won't
* cause the tree to relocate the node.
*/
continue;
}
} 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_mapping_tree_node_arrays(&backends, &be_names, &be_states, &be_list_count);
slapi_sdn_free(&subtree);
slapi_ch_free_string(&plugin_fct);
slapi_ch_free_string(&plugin_lib);
*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_mapping_tree_node_arrays(&backends, &be_names, &be_states, &be_list_count);
slapi_sdn_free(&subtree);
slapi_ch_free_string(&plugin_fct);
slapi_ch_free_string(&plugin_lib);
return SLAPI_DSE_CALLBACK_ERROR;
}
/* free any old data */
free_mapping_tree_node_arrays(&node->mtn_be, &node->mtn_backend_names, &node->mtn_be_states, &node->mtn_be_count);
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 *state_val;
const char *new_state;
Slapi_Attr *state_attr;
/* state change
* for now only allow replace
*/
if (!SLAPI_IS_MOD_REPLACE(mods[i]->mod_op)) {
PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE, "must use replace operation to change state\n");
*returncode = LDAP_UNWILLING_TO_PERFORM;
slapi_sdn_free(&subtree);
slapi_ch_free_string(&plugin_fct);
slapi_ch_free_string(&plugin_lib);
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;
slapi_ch_free_string(&plugin_fct);
slapi_ch_free_string(&plugin_lib);
return SLAPI_DSE_CALLBACK_ERROR;
}
slapi_entry_attr_find(entryAfter, "nsslapd-state", &state_attr);
slapi_attr_first_value(state_attr, &state_val);
new_state = slapi_value_get_string(state_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);
slapi_ch_free_string(&plugin_fct);
slapi_ch_free_string(&plugin_lib);
*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", &state_attr)) {
PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE, "need to set nsslapd-referral before moving to referral state\n");
slapi_sdn_free(&subtree);
slapi_ch_free_string(&plugin_fct);
slapi_ch_free_string(&plugin_lib);
*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 (SLAPI_IS_MOD_REPLACE(mods[i]->mod_op) || SLAPI_IS_MOD_ADD(mods[i]->mod_op)) {
/* 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, subtree);
} else if (SLAPI_IS_MOD_DELETE(mods[i]->mod_op)) {
/* 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);
slapi_ch_free_string(&plugin_fct);
slapi_ch_free_string(&plugin_lib);
return SLAPI_DSE_CALLBACK_ERROR;
}
mtn_free_referral_in_node(node);
} else {
*returncode = LDAP_OPERATIONS_ERROR;
mtn_unlock();
slapi_sdn_free(&subtree);
slapi_ch_free_string(&plugin_fct);
slapi_ch_free_string(&plugin_lib);
return SLAPI_DSE_CALLBACK_ERROR;
}
mtn_unlock();
slapi_sdn_free(&subtree);
slapi_ch_free_string(&plugin_fct);
slapi_ch_free_string(&plugin_lib);
*returncode = LDAP_SUCCESS;
return SLAPI_DSE_CALLBACK_OK;
} else if (strcasecmp(mods[i]->mod_type,
"nsslapd-distribution-funct") == 0) {
if (SLAPI_IS_MOD_REPLACE(mods[i]->mod_op) || SLAPI_IS_MOD_ADD(mods[i]->mod_op)) {
slapi_entry_attr_find(entryAfter,
"nsslapd-distribution-funct", &attr);
slapi_attr_first_value(attr, &val);
slapi_ch_free_string(&plugin_fct);
if (NULL == val) {
slapi_log_err(SLAPI_LOG_WARNING, "mapping_tree_entry_modify_callback",
"The nsslapd-distribution-funct attribute"
" has no value for the mapping tree node %s\n",
slapi_entry_get_dn(entryAfter));
}
plugin_fct = slapi_ch_strdup(slapi_value_get_string(val));
} else if (SLAPI_IS_MOD_DELETE(mods[i]->mod_op)) {
slapi_ch_free_string(&plugin_fct);
}
plugin_flag = 1;
} else if (strcasecmp(mods[i]->mod_type,
"nsslapd-distribution-plugin") == 0) {
if (SLAPI_IS_MOD_REPLACE(mods[i]->mod_op) || SLAPI_IS_MOD_ADD(mods[i]->mod_op)) {
slapi_entry_attr_find(entryAfter,
"nsslapd-distribution-plugin", &attr);
slapi_attr_first_value(attr, &val);
slapi_ch_free_string(&plugin_lib);
if (NULL == val) {
slapi_log_err(SLAPI_LOG_WARNING, "mapping_tree_entry_modify_callback",
"The nsslapd-distribution-plugin attribute"
" has no value for the mapping tree node %s\n",
slapi_entry_get_dn(entryAfter));
}
plugin_lib = slapi_ch_strdup(slapi_value_get_string(val));
} else if (SLAPI_IS_MOD_DELETE(mods[i]->mod_op)) {
slapi_ch_free_string(&plugin_lib);
}
plugin_flag = 1;
} else if (strcasecmp(mods[i]->mod_type,
"nsslapd-distribution-root-update") == 0) {
if (SLAPI_IS_MOD_REPLACE(mods[i]->mod_op) || SLAPI_IS_MOD_ADD(mods[i]->mod_op)) {
const char *sval;
slapi_entry_attr_find(entryAfter,
"nsslapd-distribution-root-update", &attr);
slapi_attr_first_value(attr, &val);
if (NULL == val) {
slapi_log_err(SLAPI_LOG_WARNING, "mapping_tree_entry_modify_callback",
"The nsslapd-distribution-root-update attribute"
" has no value for the mapping tree node %s\n",
slapi_entry_get_dn(entryAfter));
plugin_rootmode = CHAIN_ROOT_UPDATE_REJECT;
} else {
sval = slapi_value_get_string(val);
if (strcmp(sval, "reject") == 0)
plugin_rootmode = CHAIN_ROOT_UPDATE_REJECT;
else if (strcmp(sval, "local") == 0)
plugin_rootmode = CHAIN_ROOT_UPDATE_LOCAL;
else if (strcmp(sval, "referral") == 0)
plugin_rootmode = CHAIN_ROOT_UPDATE_REFERRAL;
}
} else if (SLAPI_IS_MOD_DELETE(mods[i]->mod_op)) {
plugin_rootmode = CHAIN_ROOT_UPDATE_REJECT; /* default */
}
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_rootmode = plugin_rootmode;
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 __attribute__((unused)),
Slapi_Entry *entryBefore,
Slapi_Entry *e __attribute__((unused)),
int *returncode,
char *returntext __attribute__((unused)),
void *arg __attribute__((unused)))
{
mapping_tree_node *node = NULL;
int i;
backend *be;
/*
* Previously this function would not take the MT lock assuming that due to the single pointer
* pointer update this would be "atomic". This is not true, and especially on weakly ordered
* platforms, this could lead to corruption of the tree during online modifications. As a result
* the lock is now taken during this operation.
*/
Slapi_DN *subtree = get_subtree_from_entry(entryBefore);
if (subtree == NULL) {
slapi_log_err(SLAPI_LOG_WARNING, "mapping_tree_entry_add_callback",
"Unable to determine the subtree represented by the mapping tree node %s\n",
slapi_entry_get_dn(entryBefore));
return SLAPI_DSE_CALLBACK_ERROR;
}
slapi_rwlock_wrlock(myLock);
mapping_tree_node *parent = slapi_get_mapping_tree_node_by_dn(subtree);
if (parent == NULL) {
parent = mapping_tree_root;
}
slapi_sdn_free(&subtree);
*returncode = mapping_tree_entry_add(entryBefore, &node, parent, PR_TRUE);
if (LDAP_SUCCESS != *returncode || !node) {
slapi_rwlock_unlock(myLock);
return SLAPI_DSE_CALLBACK_ERROR;
}
PR_ASSERT(node->mtn_parent == parent);
if (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);
}
slapi_rwlock_unlock(myLock);
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);
/*
* Check defaultNamingContext is set.
* If it is not set, set the to-be-added suffix to the config param.
*/
if (NULL == config_get_default_naming_context()) {
char *suffix =
slapi_rdn_get_value(slapi_entry_get_nrdn_const(entryBefore));
char *escaped = slapi_ch_strdup(suffix);
if (suffix && escaped) {
strcpy_unescape_value(escaped, suffix);
}
if (escaped) {
int rc = _mtn_update_config_param(LDAP_MOD_REPLACE,
CONFIG_DEFAULT_NAMING_CONTEXT,
escaped);
if (rc) {
slapi_log_err(SLAPI_LOG_ERR, "mapping_tree_entry_add_callback",
"Setting %s to %s failed: RC=%d\n",
escaped, CONFIG_DEFAULT_NAMING_CONTEXT, rc);
}
}
slapi_ch_free_string(&suffix);
slapi_ch_free_string(&escaped);
}
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);
if (tmp_node)
tmp_node->mtn_brother = node->mtn_brother;
}
node->mtn_brother = NULL;
}
int
mapping_tree_entry_delete_callback(Slapi_PBlock *pb __attribute__((unused)),
Slapi_Entry *entryBefore,
Slapi_Entry *e __attribute__((unused)),
int *returncode,
char *returntext,
void *arg __attribute__((unused)))
{
int result = SLAPI_DSE_CALLBACK_OK;
mapping_tree_node *node = NULL;
Slapi_DN *subtree;
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();
/* Remove defaultNamingContext if it is the to-be-deleted suffix.
* It should be done outside of mtn lock. */
if (SLAPI_DSE_CALLBACK_OK == result) {
char *default_naming_context = config_get_default_naming_context();
char *suffix, *escaped;
if (default_naming_context) {
suffix =
slapi_rdn_get_value(slapi_entry_get_nrdn_const(entryBefore));
escaped = slapi_ch_strdup(suffix);
if (suffix && escaped) {
strcpy_unescape_value(escaped, suffix);
}
if (escaped && (0 == strcasecmp(escaped, default_naming_context))) {
/*
* We can not delete the default naming attribute, so instead
* replace it only if there is another suffix available
*/
void *suffix_node = NULL;
Slapi_DN *sdn;
sdn = slapi_get_first_suffix(&suffix_node, 0);
if (sdn) {
char *replacement_suffix = (char *)slapi_sdn_get_dn(sdn);
int rc = _mtn_update_config_param(LDAP_MOD_REPLACE,
CONFIG_DEFAULT_NAMING_CONTEXT,
replacement_suffix);
if (rc) {
slapi_log_err(SLAPI_LOG_ERR,
"mapping_tree_entry_delete_callback",
"replacing config param %s failed: RC=%d\n",
CONFIG_DEFAULT_NAMING_CONTEXT, rc);
}
if (LDAP_SUCCESS == rc) {
char errorbuf[SLAPI_DSE_RETURNTEXT_SIZE] = {0};
/* Replacing defaultNamingContext from cn=config entry
* was successful. The replace does not reset the
* global parameter. We need to reset it separately. */
if (config_set_default_naming_context(
CONFIG_DEFAULT_NAMING_CONTEXT,
replacement_suffix, errorbuf, CONFIG_APPLY)) {
slapi_log_err(SLAPI_LOG_ERR, "mapping_tree_entry_delete_callback",
"Setting %s tp %s failed. %s\n",
CONFIG_DEFAULT_NAMING_CONTEXT, replacement_suffix, errorbuf);
}
}
}
}
slapi_ch_free_string(&suffix);
slapi_ch_free_string(&escaped);
}
}
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 (size_t 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 *));
int *be_states = (int *)slapi_ch_malloc(sizeof(int));
be_list[0] = be;
be_states[0] = SLAPI_BE_STATE_ON;
dn = slapi_sdn_new_dn_byval(subtree);
node = mapping_tree_node_new(
dn,
be_list,
NULL, /* backend_name */
be_states, /* be state */
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, 0); /* no distribution */
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 (slapi_atomic_load_32(&mapping_tree_freed, __ATOMIC_RELAXED)) {
/* shutdown has been detected */
return 0;
}
/* ONREPL - I have moved this up because otherwise we can endup calling this
* function recursively */
if (myLock != NULL) {
return 0;
}
myLock = slapi_new_rwlock();
slapi_rwlock_wrlock(myLock);
/* Should be fenced by the rwlock. */
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);
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(DSE_SCHEMA);
node = add_internal_mapping_tree_node("cn=schema", be, mapping_tree_root);
mapping_tree_node_add_child(mapping_tree_root, node);
slapi_rwlock_unlock(myLock);
/*
* 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_build_tree which has the logic to handle
* setting up from cn=config entries.
*/
if (mapping_tree_node_build_tree()) {
return -1;
}
slapi_rwlock_wrlock(myLock);
mtn_create_extension(mapping_tree_root);
slapi_rwlock_unlock(myLock);
/* 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) {
free_mapping_tree_node_arrays(&((*node)->mtn_be), &((*node)->mtn_backend_names),
&((*node)->mtn_be_states), &((*node)->mtn_be_count));
}
slapi_ch_free_string(&((*node)->mtn_dstr_plg_lib));
slapi_ch_free_string(&((*node)->mtn_dstr_plg_name));
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);
slapi_atomic_store_32(&mapping_tree_freed, 1, __ATOMIC_RELAXED);
}
/* 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,
size_t ebuflen,
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, ebuflen);
}
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,
size_t ebuflen,
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, ebuflen);
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, ebuflen);
}
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);
/* requestor dn is not set in pblock, so the distribution plugin
* will return index >= 0 */
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) {
/*
* Use get_dn rather than get_ndn, because an issue in get_ndn exists
* where ndn can be set to 0x8
*/
const char *dn = slapi_sdn_get_dn(sdn);
if (dn && ('\0' == *dn)) {
return 1;
}
}
return 0;
}
/* Checks if a write operation for a particular DN would
* require a referral to be sent. */
int
slapi_dn_write_needs_referral(Slapi_DN *target_sdn, Slapi_Entry **referral)
{
mapping_tree_node *target_node = NULL;
int ret = 0;
if (slapi_atomic_load_32(&mapping_tree_freed, __ATOMIC_RELAXED)) {
/* shutdown detected */
goto done;
}
PR_ASSERT(mapping_tree_inited == 1);
if (target_sdn) {
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;
}
/* See if we need to return a referral. */
if ((target_node->mtn_state == MTN_REFERRAL) ||
(target_node->mtn_state == MTN_REFERRAL_ON_UPDATE)) {
*referral = (target_node->mtn_referral_entry ? slapi_entry_dup(target_node->mtn_referral_entry) : NULL);
if (*referral) {
ret = 1;
}
}
mtn_unlock();
}
done:
return ret;
}
/*
* 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. SLAPI_DSE_RETURNTEXT_SIZE 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, size_t ebuflen)
{
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;
int fixup = 0;
if (slapi_atomic_load_32(&mapping_tree_freed, __ATOMIC_RELAXED)) {
/* 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);
fixup = operation_is_flag_set(op, OP_FLAG_TOMBSTONE_FIXUP);
PR_ASSERT(mapping_tree_inited == 1);
be[0] = NULL;
if (referral) {
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 && !be_isdeleted(*be)) {
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, ebuflen);
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_isdeleted(*be) &&
(((*be)->be_readonly && !fixup) ||
((slapi_config_get_readonly() && !fixup) &&
!slapi_be_private(*be)))) {
unsigned long be_op_type = operation_get_type(op);
if ((be_op_type != SLAPI_OPERATION_SEARCH) &&
(be_op_type != SLAPI_OPERATION_COMPARE) &&
(be_op_type != SLAPI_OPERATION_BIND) &&
(be_op_type != SLAPI_OPERATION_UNBIND)) {
if (errorbuf) {
PL_strncpyz(errorbuf, slapi_config_get_readonly() ? "Server is read-only" : "database is read-only", ebuflen);
}
ret = LDAP_UNWILLING_TO_PERFORM;
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, size_t ebuflen)
{
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;
Slapi_Backend *be;
Slapi_Entry *referral;
int scope = LDAP_SCOPE_BASE;
Slapi_DN *sdn = NULL;
int flag_partial_result = 0;
int op_type;
if (slapi_atomic_load_32(&mapping_tree_freed, __ATOMIC_RELAXED)) {
return LDAP_OPERATIONS_ERROR;
}
if (errorbuf) {
errorbuf[0] = '\0';
}
/* get the operational parameters */
slapi_pblock_get(pb, SLAPI_SEARCH_TARGET_SDN, &sdn);
if (NULL == sdn) {
slapi_log_err(SLAPI_LOG_ERR, "slapi_mapping_tree_select_all",
"Null target DN");
return LDAP_OPERATIONS_ERROR;
}
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);
PR_ASSERT(mapping_tree_inited == 1);
mtn_lock();
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_isdeleted(be)) {
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, ebuflen, scope);
while ((node) && (be_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, DEFBACKEND_NAME, 8)))
{
if (be && !be_isdeleted(be)) {
/* wrong backend or referall, ignore it */
slapi_log_err(SLAPI_LOG_ARGS, "slapi_mapping_tree_select_all",
"mapping tree release backend : %s\n",
slapi_be_get_name(be));
slapi_be_Unlock(be);
}
} else {
if (be && !be_isdeleted(be)) {
if (be_index == BE_LIST_SIZE) { /* error - too many backends */
slapi_create_errormsg(errorbuf, ebuflen,
"Error: too many backends match search request - cannot proceed");
slapi_log_err(SLAPI_LOG_ERR, "slapi_mapping_tree_select_all",
"Too many backends match search request - cannot proceed");
ret_code = LDAP_ADMINLIMIT_EXCEEDED;
break;
} else {
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, ebuflen, scope);
}
mtn_unlock();
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_err(SLAPI_LOG_ARGS, "slapi_mapping_tree_free_all",
"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, size_t ebuflen)
{
Slapi_DN *target_sdn = NULL;
Slapi_DN dn_newdn;
Slapi_Backend *new_be = NULL;
Slapi_Backend *def_be = defbackend_get_backend();
Slapi_Entry *new_referral = NULL;
mapping_tree_node *target_node;
int index;
Slapi_Operation *op;
int ret;
int need_unlock = 0;
if (slapi_atomic_load_32(&mapping_tree_freed, __ATOMIC_RELAXED)) {
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, ebuflen);
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, ebuflen);
if (ret)
goto unlock_and_return;
if (*be) {
/* suffix is a part of mapping tree. We should not free it */
const Slapi_DN *suffix = slapi_get_suffix_by_dn(target_sdn);
if ((*be != def_be) && (NULL == suffix)) {
slapi_create_errormsg(errorbuf, ebuflen,
"Target entry \"%s\" does not exist\n", slapi_sdn_get_dn(target_sdn));
ret = LDAP_NO_SUCH_OBJECT;
goto unlock_and_return;
}
if (suffix && (0 == slapi_sdn_compare(target_sdn, suffix))) {
/* target_sdn is a suffix */
const Slapi_DN *new_suffix = NULL;
/* new_suffix is a part of mapping tree. We should not free it */
new_suffix = slapi_get_suffix_by_dn(&dn_newdn);
if (!slapi_be_exist((const Slapi_DN *)&dn_newdn)) {
/* new_be is an empty backend */
slapi_create_errormsg(errorbuf, ebuflen, "Backend for suffix \"%s\" does not exist\n", newdn);
ret = LDAP_NO_SUCH_OBJECT;
goto unlock_and_return;
}
if (0 == slapi_sdn_compare(&dn_newdn, new_suffix)) {
ret = LDAP_ALREADY_EXISTS;
slapi_create_errormsg(errorbuf, ebuflen, "Suffix \"%s\" already exists\n", newdn);
goto unlock_and_return;
}
ret = LDAP_NAMING_VIOLATION;
slapi_create_errormsg(errorbuf, ebuflen, "Cannot rename suffix \"%s\"\n", slapi_sdn_get_dn(target_sdn));
goto unlock_and_return;
} else {
if ((*be != new_be) || mtn_sdn_has_child(target_sdn)) {
slapi_create_errormsg(errorbuf, ebuflen, "Cannot move entries across backends\n");
ret = LDAP_AFFECTS_MULTIPLE_DSAS;
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 && *be && !be_isdeleted(*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, target_node->mtn_dstr_plg_rootmode);
if (index == SLAPI_BE_ALL_BACKENDS) {
/* special value to indicate all backends must be scanned
* start with first one
*/
index = 0;
}
/* check if distribution plugi returned a special mode for
* updates as root */
else if (index == SLAPI_BE_NO_BACKEND || index == SLAPI_BE_REMOTE_BACKEND) {
/* nothing special to do */
}
/* paranoid check, never trust another programmer */
else if ((index >= target_node->mtn_be_count) || (index < 0)) {
slapi_log_err(SLAPI_LOG_WARNING, "mtn_get_be_distributed",
"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
*/
slapi_log_err(SLAPI_LOG_WARNING, "mtn_get_be_distributed",
"Distribution plugin not configured at node : %s\n",
slapi_sdn_get_ndn(target_node->mtn_subtree));
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, size_t ebuflen)
{
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 (slapi_atomic_load_32(&mapping_tree_freed, __ATOMIC_RELAXED)) {
/* 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) {
slapi_create_errormsg(errorbuf, ebuflen,
"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_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 */
operation_is_flag_set(op, SLAPI_OP_FLAG_BYPASS_REFERRALS); /* 6.1 fix to allow internal updates from plugins on 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 (referral) {
*referral = NULL;
}
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 == SLAPI_BE_NO_BACKEND) {
result = LDAP_UNWILLING_TO_PERFORM;
}
}
}
if (*index == SLAPI_BE_REMOTE_BACKEND) {
*be = NULL;
if (referral) {
*referral = (target_node->mtn_referral_entry ? slapi_entry_dup(target_node->mtn_referral_entry) : NULL);
}
(*index)++;
} else if ((*index == SLAPI_BE_NO_BACKEND) || (*index >= target_node->mtn_be_count)) {
/* we have already returned all backends -> return NULL */
*be = NULL;
if (referral) {
*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. */
/* WARNING: internal memory dse backends don't provide NAMES */
if (target_node->mtn_backend_names != NULL) {
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) {
slapi_log_err(SLAPI_LOG_BACKLDBM, "mtn_get_be",
"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]);
/* 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)) {
slapi_log_err(SLAPI_LOG_TRACE, "mtn_get_be",
"Operation attempted on backend in OFFLINE state : %s\n",
target_node->mtn_backend_names ? target_node->mtn_backend_names[*index] : "Unknown backend");
result = LDAP_OPERATIONS_ERROR;
*be = defbackend_get_backend();
}
if (flag_stop) {
*index = SLAPI_BE_NO_BACKEND;
} else {
(*index)++;
}
}
}
} 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;
if (referral) {
*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;
if (referral) {
*referral = (target_node->mtn_referral_entry ? slapi_entry_dup(target_node->mtn_referral_entry) : NULL);
}
(*index)++;
if (NULL == target_node->mtn_referral_entry) {
slapi_create_errormsg(errorbuf, ebuflen, "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 && !be_isdeleted(*be)) {
slapi_log_err(SLAPI_LOG_ARGS, "mtn_get_be",
"mapping tree selected backend : %s\n",
slapi_be_get_name(*be));
slapi_be_Rlock(*be);
} else if (referral && *referral) {
slapi_log_err(SLAPI_LOG_ARGS, "mtn_get_be",
"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 (slapi_atomic_load_32(&mapping_tree_freed, __ATOMIC_RELAXED)) {
/* 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 (slapi_atomic_load_32(&mapping_tree_freed, __ATOMIC_RELAXED)) {
/* shutdown detected */
return NULL;
}
if (NULL == dn) {
/* bad mapping tree entry operation */
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 (slapi_atomic_load_32(&mapping_tree_freed, __ATOMIC_RELAXED)) {
/* 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 (slapi_atomic_load_32(&mapping_tree_freed, __ATOMIC_RELAXED)) {
/* 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 = NULL;
if (slapi_atomic_load_32(&mapping_tree_freed, __ATOMIC_RELAXED)) {
/* shutdown detected */
return NULL;
}
if (root == NULL)
return NULL;
/* This function converts the old DN style to the new one. */
dn = slapi_create_dn_string("cn=\"%s\",%s",
slapi_sdn_get_udn(root), MAPPING_TREE_BASE_DN);
if (NULL == dn) {
slapi_log_err(SLAPI_LOG_ERR,
"slapi_get_mapping_tree_node_configdn",
"Failed to crate mapping tree dn for %s\n",
slapi_sdn_get_dn(root));
return NULL;
}
return dn;
}
Slapi_DN *
slapi_get_mapping_tree_node_configsdn(const Slapi_DN *root)
{
char *dn = NULL;
Slapi_DN *sdn = NULL;
if (slapi_atomic_load_32(&mapping_tree_freed, __ATOMIC_RELAXED)) {
/* shutdown detected */
return NULL;
}
if (root == NULL)
return NULL;
/* This function converts the old DN style to the new one. */
dn = slapi_create_dn_string("cn=\"%s\",%s",
slapi_sdn_get_udn(root), MAPPING_TREE_BASE_DN);
if (NULL == dn) {
slapi_log_err(SLAPI_LOG_ERR,
"slapi_get_mapping_tree_node_configsdn",
"Failed to crate mapping tree dn for %s\n",
slapi_sdn_get_dn(root));
return NULL;
}
sdn = slapi_sdn_new_normdn_passin(dn);
return sdn;
}
/*
* 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) && (node->mtn_be != NULL))
be = node->mtn_be[0];
else
be = NULL;
if (be == NULL)
be = defbackend_get_backend();
return be;
}
Slapi_Backend *
slapi_be_select_exact(const Slapi_DN *sdn)
{
Slapi_Backend *be = NULL;
mapping_tree_node *node = NULL;
if (!sdn) {
slapi_log_err(SLAPI_LOG_ERR, "slapi_be_select_exact",
"Empty Slapi_DN is given.\n");
return NULL;
}
node = slapi_get_mapping_tree_node_by_dn(sdn);
if (node && node->mtn_be) {
be = node->mtn_be[0];
}
return be;
}
/* Check if the dn targets an internal reserved backends */
int
slapi_on_internal_backends(const Slapi_DN *sdn)
{
char *backend_names[] = {DSE_BACKEND, DSE_SCHEMA};
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
* Return value: 1 if reserved.
* 0 if not reserved.
* -1 in error case.
*/
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);
if (NULL == op) {
return -1;
}
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;
if ((NULL == node) || (NULL == mapping_tree_root)) {
return NULL;
}
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;
if ((NULL == node) || (NULL == mapping_tree_root)) {
return NULL;
}
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);
}
/* get mapping tree node recursively */
Slapi_DN *
slapi_get_next_suffix_ext(void **node, int show_private)
{
mapping_tree_node *next_node = NULL;
if (NULL == node) {
return NULL;
}
next_node = *node;
if (next_node == NULL) {
return NULL;
}
if (next_node->mtn_children) {
next_node = next_node->mtn_children;
} else if (next_node->mtn_brother) {
next_node = next_node->mtn_brother;
} else {
next_node = next_node->mtn_parent;
if (next_node) {
next_node = next_node->mtn_brother;
}
}
while (next_node && (next_node->mtn_private && (show_private == 0)))
next_node = next_node->mtn_brother;
if (next_node) {
*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;
}
/* Return value is a part mapping tree; Don't free it. */
const Slapi_DN *
slapi_get_suffix_by_dn(const Slapi_DN *dn)
{
mapping_tree_node *node = slapi_get_mapping_tree_node_by_dn(dn);
const Slapi_DN *suffix = NULL;
if (node) {
suffix = (const Slapi_DN *)slapi_mtn_get_dn(node);
}
return suffix;
}
/*
* 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_Mods smods;
int rc = LDAP_SUCCESS, i = 0, j = 0;
Slapi_DN *node_sdn;
char **values = NULL;
int do_modify = 0;
slapi_mods_init(&smods, 0);
node_sdn = slapi_get_mapping_tree_node_configsdn(sdn);
if (!node_sdn) {
/* 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) {
Slapi_PBlock *pb = slapi_pblock_new();
slapi_modify_internal_set_pb_ext(pb, node_sdn,
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_pblock_destroy(pb);
}
slapi_mods_done(&smods);
slapi_sdn_free(&node_sdn);
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_Mods smods;
int rc = LDAP_SUCCESS;
Slapi_DN *node_sdn;
char *value;
if (NULL == state) {
return LDAP_OPERATIONS_ERROR;
}
node_sdn = slapi_get_mapping_tree_node_configsdn(sdn);
if (!node_sdn) {
/* 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 */
goto bail;
}
}
/* Otherwise, means that the state has changed, modify it */
Slapi_PBlock *pb = slapi_pblock_new();
slapi_mods_init(&smods, 1);
slapi_mods_add(&smods, LDAP_MOD_REPLACE, "nsslapd-state", strlen(state), state);
slapi_modify_internal_set_pb_ext(pb, node_sdn,
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_pblock_destroy(pb);
bail:
slapi_ch_free_string(&value);
slapi_sdn_free(&node_sdn);
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 = slapi_pblock_new();
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);
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);
slapi_pblock_destroy(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_string(&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)
{
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 */
slapi_attr_first_value(attr, &val);
state = slapi_ch_strdup(slapi_value_get_string(val));
slapi_attr_free(&attr);
}
slapi_ch_free_string(&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) {
slapi_log_err(SLAPI_LOG_TRACE, "mtn_internal_be_set_state",
"Backend %s is not declared in mapping tree\n",
be_name);
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
*/
slapi_log_err(SLAPI_LOG_TRACE, "mtn_internal_be_set_state",
"Backend %s is not declared in mapping node entry\n", be_name);
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(void)
{
slapi_rwlock_wrlock(myLock);
#ifdef DEBUG
lock_count--;
slapi_log_err(SLAPI_LOG_ARGS, "mtn_wlock", "lock count : %d\n", lock_count);
#endif
}
void
mtn_lock(void)
{
slapi_rwlock_rdlock(myLock);
#ifdef DEBUG
lock_count++;
slapi_log_err(SLAPI_LOG_ARGS, "mtn_lock", "lock count : %d\n", lock_count);
#endif
}
void
mtn_unlock(void)
{
#ifdef DEBUG
if (lock_count > 0)
lock_count--;
else if (lock_count < 0)
lock_count++;
else
lock_count = (int)11111111; /* this happening means problems */
slapi_log_err(SLAPI_LOG_ARGS, "mtn_unlock", "lock count : %d\n", lock_count);
#endif
slapi_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_err(SLAPI_LOG_ARGS, "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_err(SLAPI_LOG_ARGS, "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
#ifdef USE_DUMP_MAPPING_TREE
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) {
slapi_log_err(SLAPI_LOG_DEBUG, "dump_mapping_tree", "\n");
}
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) {
slapi_log_err(SLAPI_LOG_DEBUG, "dump_mapping_tree", "MT_DUMP: %s%s (0x%x)\n",
dump_indent, "none", current);
} else {
slapi_log_err(SLAPI_LOG_DEBUG, "dump_mapping_tree", "MT_DUMP: %s%s (0x%x)\n",
dump_indent, current->mtn_subtree->dn, current);
}
dump_mapping_tree(current, depth + 1);
}
return;
}
#endif /* USE_DUMP_MAPPING_TREE */
#endif /* DEBUG */
/* helper function to set/remove the config param in cn=config */
static int
_mtn_update_config_param(int op, char *type, char *strvalue)
{
Slapi_DN sdn;
Slapi_Mods smods;
LDAPMod **mods;
int rc = LDAP_PARAM_ERROR;
slapi_mods_init(&smods, 0);
switch (op) {
case LDAP_MOD_DELETE:
slapi_mods_add(&smods, op, type, 0, NULL);
break;
case LDAP_MOD_ADD:
case LDAP_MOD_REPLACE:
slapi_mods_add_string(&smods, op, type, strvalue);
break;
default:
return rc;
}
Slapi_PBlock *confpb = slapi_pblock_new();
slapi_sdn_init_ndn_byref(&sdn, SLAPD_CONFIG_DN);
slapi_modify_internal_set_pb_ext(confpb, &sdn,
slapi_mods_get_ldapmods_byref(&smods),
NULL, NULL,
(void *)plugin_get_default_component_id(), 0);
slapi_modify_internal_pb(confpb);
slapi_pblock_get(confpb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
slapi_sdn_done(&sdn);
/* need to free passed out mods
* since the internal modify could realloced mods. */
slapi_pblock_get(confpb, SLAPI_MODIFY_MODS, &mods);
ldap_mods_free(mods, 1 /* Free the Array and the Elements */);
slapi_pblock_destroy(confpb);
return rc;
}
|