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
|
/** BEGIN COPYRIGHT BLOCK
* Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
* Copyright (C) 2005 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
/*
* dse.c - DSE (DSA-Specific Entry) persistent storage.
*
* The DSE store is an LDIF file contained in the file dse.ldif.
* The file is located in the directory specified with '-D'
* when staring the server.
*
* In core, the DSEs are stored in an AVL tree, keyed on
* DN. Whenever a modification is made to a DSE, the
* in-core entry is updated, then dse_write_file() is
* called to commit the changes to disk.
*
* This is designed for a small number of DSEs, say
* a maximum of 10 or 20. If large numbers of DSEs
* need to be stored, this approach of writing out
* the entire contents on every modification will
* be insufficient.
*
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <prio.h>
#include <prcountr.h>
#include "slap.h"
#include <pwd.h>
/* Needed to access read_config_dse */
#include "proto-slap.h"
#include <unistd.h> /* provides fsync/close */
/* #define SLAPI_DSE_DEBUG */ /* define this to force trace log */
/* messages to always be logged */
#ifdef SLAPI_DSE_DEBUG
#define SLAPI_DSE_TRACELEVEL SLAPI_LOG_INFO
#else /* SLAPI_DSE_DEBUG */
#define SLAPI_DSE_TRACELEVEL SLAPI_LOG_TRACE
#endif /* SLAPI_DSE_DEBUG */
#define SCHEMA_VIOLATION -2
#define STOP_TRAVERSAL -2
/* This is returned by dupentry_replace if the duplicate entry was found and
replaced. This is returned up through the avl_insert() in
dse_replace_entry(). Otherwise, if avl_insert() returns 0, the
entry was added i.e. a duplicate was not found.
*/
#define DSE_ENTRY_WAS_REPLACED -3
/* This is returned by dupentry_merge if the duplicate entry was found and
merged. This is returned up through the avl_insert() in dse_add_entry_pb().
Otherwise, if avl_insert() returns 0, the
entry was added i.e. a duplicate was not found.
*/
#define DSE_ENTRY_WAS_MERGED -4
/* some functions can be used either from within a lock or "standalone" */
#define DSE_USE_LOCK 1
#define DSE_NO_LOCK 0
struct dse_callback
{
int operation;
int flags;
Slapi_DN *base;
int scope;
char *filter; /* NULL means match all entries */
Slapi_Filter *slapifilter; /* NULL means match all entries */
int (*fn)(Slapi_PBlock *, Slapi_Entry *, Slapi_Entry *, int *, char *, void *);
void *fn_arg;
struct slapdplugin *plugin;
struct dse_callback *next;
};
struct dse
{
char *dse_filename; /* these are the primary files which get read from */
char *dse_tmpfile; /* and written to when changes are made via LDAP */
char *dse_fileback; /* contain the latest info, just before a new change */
char *dse_filestartOK; /* contain the latest info with which the server has successfully started */
char *dse_configdir; /* The location of config files - allows us to fsync the dir post rename */
Avlnode *dse_tree;
struct dse_callback *dse_callback;
Slapi_RWLock *dse_rwlock; /* a read-write lock to protect the whole dse backend */
char **dse_filelist; /* these are additional read only files used to */
/* initialize the dse */
int dse_is_updateable; /* if non-zero, this DSE can be written to */
int dse_readonly_error_reported; /* used to ensure that read-only errors are logged only once */
};
struct dse_node
{
Slapi_Entry *entry;
};
/* search set stuff - used to pass search results to the frontend */
typedef struct dse_search_set
{
DataList dl;
int current_entry;
} dse_search_set;
static int dse_permission_to_write(struct dse *pdse, int loglevel);
static int dse_write_file_nolock(struct dse *pdse);
static int dse_apply_nolock(struct dse *pdse, IFP fp, caddr_t arg);
static int dse_replace_entry(struct dse *pdse, Slapi_Entry *e, int write_file, int use_lock);
static dse_search_set *dse_search_set_new(void);
static void dse_search_set_delete(dse_search_set *ss);
static void dse_search_set_clean(dse_search_set *ss);
static void dse_free_entry(void **data);
static void dse_search_set_add_entry(dse_search_set *ss, Slapi_Entry *e);
static Slapi_Entry *dse_search_set_get_next_entry(dse_search_set *ss);
static int dse_add_entry_pb(struct dse *pdse, Slapi_Entry *e, Slapi_PBlock *pb);
static struct dse_node *dse_find_node(struct dse *pdse, const Slapi_DN *dn);
static int dse_modify_plugin(Slapi_Entry *pre_entry, Slapi_Entry *post_entry, char *returntext);
static int dse_add_plugin(Slapi_Entry *entry, char *returntext);
static int dse_delete_plugin(Slapi_Entry *entry, char *returntext);
static int dse_pre_modify_plugin(Slapi_Entry *entryBefore, Slapi_Entry *entryAfter, LDAPMod **mods);
/*
richm: In almost all modes e.g. db2ldif, ldif2db, etc. we do not need/want
to write out the dse.ldif and ldbm.ldif files. The only mode which really
needs to write out the file is the regular server mode. The variable
dont_ever_write_dse_files tells dse_write_file_nolock whether or not to write
the .ldif file for the entry. The default is 1, which means never write the
file. The server, when it starts up in regular mode, must call
dse_unset_dont_ever_write_dse_files() to enable this file to be written
*/
static int dont_ever_write_dse_files = 1;
/* Forward declarations */
static int entry_dn_cmp(caddr_t d1, caddr_t d2);
static int dupentry_disallow(caddr_t d1, caddr_t d2);
static int dupentry_merge(caddr_t d1, caddr_t d2);
static int dse_write_entry(caddr_t data, caddr_t arg);
static int ldif_record_end(char *p);
static int dse_call_callback(struct dse *pdse, Slapi_PBlock *pb, int operation, int flags, Slapi_Entry *entryBefore, Slapi_Entry *entryAfter, int *returncode, char *returntext);
/*
* Map a DN onto a dse_node.
* Returns NULL if not found.
* You must have a read or write lock on the dse_rwlock while
* using the returned node.
*/
static struct dse_node *
dse_find_node(struct dse *pdse, const Slapi_DN *dn)
{
struct dse_node *n = NULL;
if (NULL != dn) {
struct dse_node searchNode;
Slapi_Entry *fe = slapi_entry_alloc();
slapi_entry_init(fe, NULL, NULL);
slapi_entry_set_sdn(fe, dn);
searchNode.entry = fe;
n = (struct dse_node *)avl_find(pdse->dse_tree, &searchNode, entry_dn_cmp);
slapi_entry_free(fe);
}
return n;
}
static int counters_created = 0;
PR_DEFINE_COUNTER(dse_entries_exist);
/*
* Map a DN onto a real Entry.
* Returns NULL if not found.
*/
static Slapi_Entry *
dse_get_entry_copy(struct dse *pdse, const Slapi_DN *dn, int use_lock)
{
Slapi_Entry *e = NULL;
struct dse_node *n;
if (use_lock == DSE_USE_LOCK && pdse->dse_rwlock) {
slapi_rwlock_rdlock(pdse->dse_rwlock);
}
n = dse_find_node(pdse, dn);
if (n != NULL) {
e = slapi_entry_dup(n->entry);
}
if (use_lock == DSE_USE_LOCK && pdse->dse_rwlock) {
slapi_rwlock_unlock(pdse->dse_rwlock);
}
return e;
}
static struct dse_callback *
dse_callback_new(int operation,
int flags,
const Slapi_DN *base,
int scope,
const char *filter,
dseCallbackFn fn,
void *fn_arg,
struct slapdplugin *plugin)
{
struct dse_callback *p = NULL;
p = (struct dse_callback *)slapi_ch_calloc(1, sizeof(struct dse_callback));
if (p != NULL) {
p->operation = operation;
p->flags = flags;
p->base = slapi_sdn_dup(base);
p->scope = scope;
if (NULL == filter) {
p->filter = NULL;
p->slapifilter = NULL;
} else {
p->filter = slapi_ch_strdup(filter);
p->slapifilter = slapi_str2filter(p->filter);
filter_normalize(p->slapifilter);
}
p->fn = fn;
p->fn_arg = fn_arg;
p->plugin = plugin;
p->next = NULL;
}
return p;
}
static void
dse_callback_delete(struct dse_callback **pp)
{
if (pp != NULL) {
slapi_sdn_free(&((*pp)->base));
slapi_ch_free((void **)&((*pp)->filter));
slapi_filter_free((*pp)->slapifilter, 1);
slapi_ch_free((void **)pp);
}
}
static void
dse_callback_deletelist(struct dse_callback **pp)
{
if (pp != NULL) {
struct dse_callback *p, *n;
for (p = *pp; p != NULL;) {
n = p->next;
dse_callback_delete(&p);
p = n;
}
}
}
/*
Makes a copy of the entry passed in, so it's const
*/
static struct dse_node *
dse_node_new(const Slapi_Entry *entry)
{
struct dse_node *p = NULL;
p = (struct dse_node *)slapi_ch_malloc(sizeof(struct dse_node));
if (p != NULL) {
p->entry = slapi_entry_dup(entry);
}
if (!counters_created) {
PR_CREATE_COUNTER(dse_entries_exist, "DSE", "entries", "");
counters_created = 1;
}
PR_INCREMENT_COUNTER(dse_entries_exist);
return p;
}
static void
dse_node_delete(struct dse_node **pp)
{
slapi_entry_free((*pp)->entry);
slapi_ch_free((void **)&(*pp));
PR_DECREMENT_COUNTER(dse_entries_exist);
}
static void
dse_callback_addtolist(struct dse_callback **pplist, struct dse_callback *p)
{
if (pplist != NULL) {
p->next = NULL;
if (*pplist == NULL) {
*pplist = p;
} else {
struct dse_callback *t = *pplist;
for (; t->next != NULL; t = t->next)
;
t->next = p;
}
}
}
static void
dse_callback_removefromlist(struct dse_callback **pplist, int operation, int flags, const Slapi_DN *base, int scope, const char *filter, dseCallbackFn fn)
{
if (pplist != NULL) {
struct dse_callback *t = *pplist;
struct dse_callback *prev = NULL;
for (; t != NULL;) {
if ((t->operation & operation) && (t->flags & flags) &&
(t->fn == fn) && (scope == t->scope) &&
(slapi_sdn_compare(base, t->base) == 0) &&
((NULL == filter && NULL == t->filter) || /* both are NULL OR */
((filter && t->filter) && /* both are not NULL AND match. */
(strcasecmp(filter, t->filter) == 0)))) {
if (prev == NULL) {
*pplist = t->next;
} else {
prev->next = t->next;
}
dse_callback_delete(&t);
t = NULL;
} else {
prev = t;
t = t->next;
}
}
}
}
/*
* Create a new dse structure.
*/
struct dse *
dse_new(char *filename, char *tmpfilename, char *backfilename, char *startokfilename, const char *configdir)
{
struct dse *pdse = NULL;
char *realconfigdir = NULL;
if (configdir != NULL) {
realconfigdir = slapi_ch_strdup(configdir);
} else {
realconfigdir = config_get_configdir();
}
if (realconfigdir != NULL) {
pdse = (struct dse *)slapi_ch_calloc(1, sizeof(struct dse));
if (pdse != NULL) {
pdse->dse_rwlock = slapi_new_rwlock();
/* Set the full path name for the config DSE entry */
if (!strstr(filename, realconfigdir)) {
pdse->dse_filename = slapi_ch_smprintf("%s/%s", realconfigdir, filename);
} else {
pdse->dse_filename = slapi_ch_strdup(filename);
}
if (!strstr(tmpfilename, realconfigdir)) {
pdse->dse_tmpfile = slapi_ch_smprintf("%s/%s", realconfigdir, tmpfilename);
} else {
pdse->dse_tmpfile = slapi_ch_strdup(tmpfilename);
}
pdse->dse_configdir = slapi_ch_strdup(realconfigdir);
if (backfilename != NULL) {
if (!strstr(backfilename, realconfigdir)) {
pdse->dse_fileback = slapi_ch_smprintf("%s/%s", realconfigdir, backfilename);
} else {
pdse->dse_fileback = slapi_ch_strdup(backfilename);
}
} else {
pdse->dse_fileback = NULL;
}
if (startokfilename != NULL) {
if (!strstr(startokfilename, realconfigdir)) {
pdse->dse_filestartOK = slapi_ch_smprintf("%s/%s", realconfigdir, startokfilename);
} else {
pdse->dse_filestartOK = slapi_ch_strdup(startokfilename);
}
} else {
pdse->dse_filestartOK = NULL;
}
pdse->dse_tree = NULL;
pdse->dse_callback = NULL;
pdse->dse_is_updateable = dse_permission_to_write(pdse,
SLAPI_LOG_TRACE);
}
slapi_ch_free((void **)&realconfigdir);
}
return pdse;
}
/*
* Create a new dse structure with a file list
*/
struct dse *
dse_new_with_filelist(char *filename, char *tmpfilename, char *backfilename, char *startokfilename, const char *configdir, char **filelist)
{
struct dse *newdse = dse_new(filename, tmpfilename, backfilename, startokfilename, configdir);
newdse->dse_filelist = filelist;
return newdse;
}
static int
dse_internal_delete_entry(caddr_t data, caddr_t arg __attribute__((unused)))
{
struct dse_node *n = (struct dse_node *)data;
dse_node_delete(&n);
return 0;
}
/*
* Get rid of a dse structure.
*/
int
dse_destroy(struct dse *pdse)
{
int nentries = 0;
if (NULL == pdse) {
return 0; /* no one checks this return value */
}
if (pdse->dse_rwlock)
slapi_rwlock_wrlock(pdse->dse_rwlock);
slapi_ch_free((void **)&(pdse->dse_filename));
slapi_ch_free((void **)&(pdse->dse_tmpfile));
slapi_ch_free((void **)&(pdse->dse_fileback));
slapi_ch_free((void **)&(pdse->dse_filestartOK));
slapi_ch_free((void **)&(pdse->dse_configdir));
dse_callback_deletelist(&pdse->dse_callback);
charray_free(pdse->dse_filelist);
nentries = avl_free(pdse->dse_tree, dse_internal_delete_entry);
if (pdse->dse_rwlock) {
slapi_rwlock_unlock(pdse->dse_rwlock);
slapi_destroy_rwlock(pdse->dse_rwlock);
}
slapi_ch_free((void **)&pdse);
slapi_log_err(SLAPI_DSE_TRACELEVEL, "dse_destroy", "Removed [%d] entries from the dse tree.\n",
nentries);
return 0; /* no one checks this return value */
}
/*
* Get rid of a dse structure.
*/
int
dse_deletedse(Slapi_PBlock *pb)
{
struct dse *pdse = NULL;
slapi_pblock_get(pb, SLAPI_PLUGIN_PRIVATE, &pdse);
if (pdse) {
dse_destroy(pdse);
}
/* data is freed, so make sure no one tries to use it */
slapi_pblock_set(pb, SLAPI_PLUGIN_PRIVATE, NULL);
return 0;
}
static char *subordinatecount = "numsubordinates";
/*
* Get the number of subordinates for this entry.
*/
static size_t
dse_numsubordinates(Slapi_Entry *entry)
{
int ret = 0;
Slapi_Attr *read_attr = NULL;
size_t current_sub_count = 0;
/* Get the present value of the subcount attr, or 0 if not present */
ret = slapi_entry_attr_find(entry, subordinatecount, &read_attr);
if (0 == ret) {
/* decode the value */
Slapi_Value *sval;
slapi_attr_first_value(read_attr, &sval);
if (sval != NULL) {
const struct berval *bval = slapi_value_get_berval(sval);
if (NULL != bval)
current_sub_count = atol(bval->bv_val);
}
}
return current_sub_count;
}
/*
* Update the numsubordinates count.
* mod_op is either an Add or Delete.
*/
static void
dse_updateNumSubordinates(Slapi_Entry *entry, int op)
{
int ret = 0;
int mod_op = 0;
Slapi_Attr *read_attr = NULL;
size_t current_sub_count = 0;
int already_present = 0;
/* For now, we're only interested in subordinatecount.
We first examine the present value for the attribute.
If it isn't present and we're adding, we assign value 1 to the attribute and add it.
If it is present, we increment or decrement depending upon whether we're adding or deleting.
If the value after decrementing is zero, we remove it.
*/
/* Get the present value of the subcount attr, or 0 if not present */
ret = slapi_entry_attr_find(entry, subordinatecount, &read_attr);
if (0 == ret) {
/* decode the value */
Slapi_Value *sval;
slapi_attr_first_value(read_attr, &sval);
if (sval != NULL) {
const struct berval *bval = slapi_value_get_berval(sval);
if (bval != NULL) {
already_present = 1;
current_sub_count = atol(bval->bv_val);
}
}
}
/* are we adding ? */
if ((SLAPI_OPERATION_ADD == op) && !already_present) {
/* If so, and the parent entry does not already have a subcount attribute, we need to add it */
mod_op = LDAP_MOD_ADD;
} else {
if (SLAPI_OPERATION_DELETE == op) {
if (!already_present) {
/* This means that something is wrong---deleting a child but no subcount present on parent */
slapi_log_err(SLAPI_LOG_ERR, "dse_updateNumSubordinates",
"numsubordinates assertion failure\n");
return;
} else {
if (current_sub_count == 1) {
mod_op = LDAP_MOD_DELETE;
} else {
mod_op = LDAP_MOD_REPLACE;
}
}
} else {
mod_op = LDAP_MOD_REPLACE;
}
}
/* Now compute the new value */
if (SLAPI_OPERATION_ADD == op) {
current_sub_count++;
} else {
current_sub_count--;
}
{
char value_buffer[22] = {0}; /* enough digits for 2^64 children */
struct berval *vals[2];
struct berval val;
vals[0] = &val;
vals[1] = NULL;
sprintf(value_buffer, "%lu", (long unsigned int)current_sub_count);
val.bv_val = value_buffer;
val.bv_len = strlen(val.bv_val);
switch (mod_op) {
case LDAP_MOD_ADD:
attrlist_merge(&entry->e_attrs, subordinatecount, vals);
break;
case LDAP_MOD_REPLACE:
attrlist_replace(&entry->e_attrs, subordinatecount, vals);
break;
case LDAP_MOD_DELETE:
attrlist_delete(&entry->e_attrs, subordinatecount);
break;
}
}
}
/* the write lock should always be acquired before calling this function */
static void
dse_updateNumSubOfParent(struct dse *pdse, const Slapi_DN *child, int op)
{
Slapi_DN parent;
slapi_sdn_init(&parent);
slapi_sdn_get_parent(child, &parent);
if (!slapi_sdn_isempty(&parent)) {
/* no lock because caller should already have the write lock */
Slapi_Entry *parententry = dse_get_entry_copy(pdse, &parent, DSE_NO_LOCK);
if (parententry != NULL) {
/* Decrement the numsubordinate count of the parent entry */
dse_updateNumSubordinates(parententry, op);
/* no lock because caller should always have the write lock */
dse_replace_entry(pdse, parententry, 0, DSE_NO_LOCK);
slapi_entry_free(parententry);
}
}
slapi_sdn_done(&parent);
}
/* check if a file is valid, or if a provided backup file can be used.
* there is no way to determine if the file contents is usable, the only
* checks that can be done is that the file exists and that it is not size 0
*/
int
dse_check_file(char *filename, char *backupname)
{
int rc = 0; /* Fail */
PRFileInfo64 prfinfo;
if (PR_GetFileInfo64(filename, &prfinfo) == PR_SUCCESS) {
if (prfinfo.size > 0) {
/* File exists and has content. */
return 1;
} else {
slapi_log_err(SLAPI_LOG_INFO, "dse_check_file",
"The config %s has zero length. Attempting restore ... \n", filename);
rc = PR_Delete(filename);
}
} else {
slapi_log_err(SLAPI_LOG_INFO, "dse_check_file",
"The config %s can not be accessed. Attempting restore ... (reason: %d)\n", filename, rc);
}
if (backupname) {
if (PR_GetFileInfo64(backupname, &prfinfo) != PR_SUCCESS) {
slapi_log_err(SLAPI_LOG_INFO, "dse_check_file",
"The backup %s can not be accessed. Check it exists and permissions.\n", backupname);
return 0;
}
if (prfinfo.size <= 0) {
slapi_log_err(SLAPI_LOG_ERR, "dse_check_file",
"The backup file %s has zero length, refusing to restore it.\n", backupname);
return 0;
}
rc = PR_Rename(backupname, filename);
if (rc != PR_SUCCESS) {
slapi_log_err(SLAPI_LOG_INFO, "dse_check_file",
"The configuration file %s was NOT able to be restored from %s, error %d\n", filename, backupname, rc);
return 0;
}
slapi_log_err(SLAPI_LOG_INFO, "dse_check_file",
"The configuration file %s was restored from backup %s\n", filename, backupname);
return 1;
} else {
slapi_log_err(SLAPI_LOG_INFO, "dse_check_file", "No backup filename provided.\n");
return 0;
}
}
static int
dse_read_one_file(struct dse *pdse, const char *filename, Slapi_PBlock *pb, int primary_file)
{
Slapi_Entry *e = NULL;
char *entrystr = NULL;
char *buf = NULL;
char *lastp = NULL;
int rc = 0; /* Fail */
PRInt32 nr = 0;
PRFileInfo64 prfinfo;
PRFileDesc *prfd = 0;
int schema_flags = 0;
slapi_pblock_get(pb, SLAPI_SCHEMA_FLAGS, &schema_flags);
if ((NULL != pdse) && (NULL != filename)) {
/* check if the "real" file exists and cam be used, if not try tmp as backup */
rc = dse_check_file((char *)filename, pdse->dse_tmpfile);
if (!rc) {
rc = dse_check_file((char *)filename, pdse->dse_fileback);
}
if ((rc = PR_GetFileInfo64(filename, &prfinfo)) != PR_SUCCESS) {
slapi_log_err(SLAPI_LOG_ERR, "dse_read_one_file",
"The configuration file %s could not be accessed, error %d\n",
filename, rc);
rc = 0; /* Fail */
} else if ((prfd = PR_Open(filename, PR_RDONLY, SLAPD_DEFAULT_FILE_MODE)) == NULL) {
slapi_log_err(SLAPI_LOG_ERR, "dse_read_one_file",
"The configuration file %s could not be read. " SLAPI_COMPONENT_NAME_NSPR " %d (%s)\n",
filename,
PR_GetError(), slapd_pr_strerror(PR_GetError()));
rc = 0; /* Fail */
} else {
int done = 0;
/* read the entire file into core */
buf = slapi_ch_malloc(prfinfo.size + 1);
if ((nr = slapi_read_buffer(prfd, buf, prfinfo.size)) < 0) {
slapi_log_err(SLAPI_LOG_ERR, "dse_read_one_file",
"Could only read %d of %ld bytes from config file %s\n",
nr, (long int)prfinfo.size, filename);
rc = 0; /* Fail */
done = 1;
}
(void)PR_Close(prfd);
buf[nr] = '\0';
if (!done) {
int lineno = 1;
int lines = 0;
int dont_check_dups = 0;
int str2entry_flags = SLAPI_STR2ENTRY_EXPAND_OBJECTCLASSES |
SLAPI_STR2ENTRY_NOT_WELL_FORMED_LDIF;
if (schema_flags & DSE_SCHEMA_LOCKED)
str2entry_flags |= SLAPI_STR2ENTRY_NO_SCHEMA_LOCK;
PR_ASSERT(pb);
slapi_pblock_get(pb, SLAPI_DSE_DONT_CHECK_DUPS, &dont_check_dups);
if (!dont_check_dups) {
str2entry_flags |= SLAPI_STR2ENTRY_REMOVEDUPVALS;
}
/* Convert LDIF to entry structures */
rc = 1; /* assume we will succeed */
while ((entrystr = dse_read_next_entry(buf, &lastp)) != NULL) {
char *p, *q;
char errbuf[1024];
size_t estrlen = strlen(entrystr);
size_t cpylen =
(estrlen < sizeof(errbuf)) ? estrlen : sizeof(errbuf) - 1;
memcpy(errbuf, entrystr, cpylen);
errbuf[cpylen] = '\0';
lines = 1;
p = entrystr;
while ((q = strchr(p, '\n'))) {
p = q + 1;
lines++;
}
e = slapi_str2entry(entrystr, str2entry_flags);
if (e != NULL) {
int returncode = 0;
char returntext[SLAPI_DSE_RETURNTEXT_SIZE] = {0};
slapi_log_err(SLAPI_LOG_TRACE, "dse_read_one_file",
" processing entry \"%s\" in file %s%s "
"(lineno: %d)\n",
slapi_entry_get_dn_const(e), filename,
primary_file ? " (primary file)" : "",
lineno);
/* remove the numsubordinates attr, which may be bogus */
slapi_entry_attr_delete(e, subordinatecount);
/* set the "primary file" flag if appropriate */
slapi_pblock_set(pb, SLAPI_DSE_IS_PRIMARY_FILE, &primary_file);
if (dse_call_callback(pdse, pb, DSE_OPERATION_READ,
DSE_FLAG_PREOP, e, NULL, &returncode,
returntext) == SLAPI_DSE_CALLBACK_OK) {
/*
* This will free the entry if not added, so it is
* definitely consumed by this call
*/
if (dse_add_entry_pb(pdse, e, pb) == SCHEMA_VIOLATION) {
/* schema violation, return failure */
rc = 0;
}
} else /* free entry if not used */
{
slapi_log_err(SLAPI_LOG_FATAL,
"dse_read_one_file",
"The entry %s in file %s "
"(lineno: %d) is invalid, "
"error code %d (%s) - %s\n",
slapi_entry_get_dn_const(e),
filename, lineno, returncode,
ldap_err2string(returncode),
returntext);
slapi_entry_free(e);
rc = 0; /* failure */
}
} else {
slapi_log_err(SLAPI_LOG_ERR, "dse_read_one_file",
"Parsing entry (lineno: %d) "
"in file %s failed.\n",
lineno, filename);
slapi_log_err(SLAPI_LOG_ERR, "dse_read_one_file",
"Invalid section [%s%s]\n",
errbuf, cpylen == estrlen ? "" : " ...");
rc = 0; /* failure */
}
lineno += lines + 1 /* 1 is for a blank line. */;
}
}
slapi_ch_free((void **)&buf);
}
}
return rc;
}
/*
* Read the file we were initialised with into memory.
* If not NULL call entry_filter_fn on each entry as it's read.
* The function is free to modify the entry before it's places
* into the AVL tree. True means add the entry. False means don't.
*
* Return 1 for OK, 0 for Fail.
*/
int
dse_read_file(struct dse *pdse, Slapi_PBlock *pb)
{
int rc = 1; /* Good */
int ii;
char **filelist = 0;
char *filename = 0;
filelist = charray_dup(pdse->dse_filelist);
filename = slapi_ch_strdup(pdse->dse_filename);
for (ii = 0; rc && filelist && filelist[ii]; ++ii) {
if (strcasecmp(filename, filelist[ii]) != 0) {
rc = dse_read_one_file(pdse, filelist[ii], pb, 0 /* not primary */);
}
}
if (rc) {
rc = dse_read_one_file(pdse, filename, pb, 1 /* primary file */);
}
charray_free(filelist);
slapi_ch_free((void **)&filename);
return rc;
}
/*
* Structure to carry context information whilst
* traversing the tree writing the entries to disk.
*/
typedef struct _fpw
{
PRFileDesc *fpw_prfd;
int fpw_rc;
struct dse *fpw_pdse;
} FPWrapper;
static int
dse_rw_permission_to_one_file(const char *name, int loglevel)
{
PRErrorCode prerr = 0;
const char *accesstype = "";
if (NULL == name) {
return 1; /* file won't be used -- return "sufficient permission" */
}
if (PR_Access(name, PR_ACCESS_EXISTS) == PR_SUCCESS) {
/* file exists: check for read and write permission */
if (PR_Access(name, PR_ACCESS_WRITE_OK) != PR_SUCCESS) {
prerr = PR_GetError();
accesstype = "write";
} else if (PR_Access(name, PR_ACCESS_READ_OK) != PR_SUCCESS) {
prerr = PR_GetError();
accesstype = "read";
}
} else {
/* file does not exist: make sure we can create it */
PRFileDesc *prfd;
prfd = PR_Open(name, PR_RDWR | PR_CREATE_FILE | PR_TRUNCATE,
SLAPD_DEFAULT_FILE_MODE);
if (NULL == prfd) {
prerr = PR_GetError();
accesstype = "create";
} else {
PR_Close(prfd);
PR_Delete(name);
}
}
if (prerr != 0) {
slapi_log_err(loglevel,
"dse_rw_permission_to_one_file", "Unable to %s \"%s\": " SLAPI_COMPONENT_NAME_NSPR " error %d (%s)\n",
accesstype, name, prerr, slapd_pr_strerror(prerr));
return 0; /* insufficient permission */
} else {
return 1; /* sufficient permission */
}
}
/*
* Check that we have permission to write to all the files that
* dse_write_file_nolock() uses.
* Returns a non-zero value if sufficient permission and 0 if not.
*/
static int
dse_permission_to_write(struct dse *pdse, int loglevel)
{
int rc = 1; /* sufficient permission */
if (NULL != pdse->dse_filename) {
if (!dse_rw_permission_to_one_file(pdse->dse_filename, loglevel) ||
!dse_rw_permission_to_one_file(pdse->dse_fileback, loglevel) ||
!dse_rw_permission_to_one_file(pdse->dse_tmpfile, loglevel)) {
rc = 0; /* insufficient permission */
}
}
return rc;
}
/*
* Check for read-only status and return an appropriate error to the
* LDAP client.
* Returns 0 if no error was returned and non-zero if one was.
*/
static int
dse_check_for_readonly_error(Slapi_PBlock *pb, struct dse *pdse)
{
int rc = 0; /* default: no error */
if (pdse->dse_rwlock)
slapi_rwlock_rdlock(pdse->dse_rwlock);
if (!pdse->dse_is_updateable) {
if (!pdse->dse_readonly_error_reported) {
if (NULL != pdse->dse_filename) {
slapi_log_err(SLAPI_LOG_ERR, "dse_check_for_readonly_error",
"The DSE database stored in \"%s\" is not writeable\n",
pdse->dse_filename);
/* log the details too */
(void)dse_permission_to_write(pdse, SLAPI_LOG_ERR);
}
pdse->dse_readonly_error_reported = 1;
}
rc = 1; /* return an error to the client */
}
if (pdse->dse_rwlock)
slapi_rwlock_unlock(pdse->dse_rwlock);
if (rc != 0) {
slapi_send_ldap_result(pb, LDAP_UNWILLING_TO_PERFORM, NULL,
"DSE database is read-only", 0, NULL);
}
return rc; /* no error */
}
/*
* Write the AVL tree of entries back to the LDIF file.
*/
static int
dse_write_file_nolock(struct dse *pdse)
{
FPWrapper fpw;
int rc = 0;
if (dont_ever_write_dse_files) {
return rc;
}
fpw.fpw_rc = 0;
fpw.fpw_prfd = NULL;
if (NULL != pdse->dse_filename) {
if ((fpw.fpw_prfd = PR_Open(pdse->dse_tmpfile, PR_RDWR | PR_CREATE_FILE | PR_TRUNCATE, SLAPD_DEFAULT_FILE_MODE)) == NULL) {
rc = PR_GetOSError();
slapi_log_err(SLAPI_LOG_ERR, "dse_write_file_nolock", "Cannot open "
"temporary DSE file \"%s\" for update: OS error %d (%s)\n",
pdse->dse_tmpfile, rc, slapd_system_strerror(rc));
} else {
fpw.fpw_pdse = pdse;
if (avl_apply(pdse->dse_tree, dse_write_entry, &fpw, STOP_TRAVERSAL, AVL_INORDER) == STOP_TRAVERSAL) {
rc = fpw.fpw_rc;
slapi_log_err(SLAPI_LOG_ERR, "dse_write_file_nolock", "Cannot write "
" temporary DSE file \"%s\": OS error %d (%s)\n",
pdse->dse_tmpfile, rc, slapd_system_strerror(rc));
(void)PR_Close(fpw.fpw_prfd);
fpw.fpw_prfd = NULL;
} else {
(void)PR_Close(fpw.fpw_prfd);
fpw.fpw_prfd = NULL;
if (pdse->dse_fileback != NULL) {
rc = slapi_destructive_rename(pdse->dse_filename, pdse->dse_fileback);
if (rc != 0) {
slapi_log_err(SLAPI_LOG_ERR, "dse_write_file_nolock", "Cannot backup"
" DSE file \"%s\" to \"%s\": OS error %d (%s)\n",
pdse->dse_filename, pdse->dse_fileback,
rc, slapd_system_strerror(rc));
}
}
rc = slapi_destructive_rename(pdse->dse_tmpfile, pdse->dse_filename);
if (rc != 0) {
slapi_log_err(SLAPI_LOG_ERR, "dse_write_file_nolock", "Cannot rename"
" temporary DSE file \"%s\" to \"%s\":"
" OS error %d (%s)\n",
pdse->dse_tmpfile, pdse->dse_filename,
rc, slapd_system_strerror(rc));
}
/*
* We have now written to the tmp location, and renamed it
* we need to open and fsync the dir to make the rename stick.
*/
int fp_configdir =
#ifdef O_PATH
open(pdse->dse_configdir, O_PATH | O_DIRECTORY)
#else
open(pdse->dse_configdir, O_RDONLY | O_DIRECTORY)
#endif
;
if (fp_configdir != -1) {
fsync(fp_configdir);
close(fp_configdir);
}
}
}
if (fpw.fpw_prfd)
(void)PR_Close(fpw.fpw_prfd);
}
return rc;
}
/*
* Local function for writing an entry to a file.
* Called by the AVL code during traversal.
*/
static int
dse_write_entry(caddr_t data, caddr_t arg)
{
struct dse_node *n = (struct dse_node *)data;
FPWrapper *fpw = (FPWrapper *)arg;
char *s;
PRInt32 len;
if (NULL != n && NULL != n->entry) {
int returncode;
char returntext[SLAPI_DSE_RETURNTEXT_SIZE] = "";
/* need to make a duplicate here for two reasons:
1) we don't want to hold on to the raw data in the node for any longer
than we have to; we will usually be inside the dse write lock, but . . .
2) the write callback may modify the entry, so we want to pass it a
writeable copy rather than the raw avl tree data pointer
*/
Slapi_Entry *ec = slapi_entry_dup(n->entry);
if (dse_call_callback(fpw->fpw_pdse, NULL, DSE_OPERATION_WRITE,
DSE_FLAG_PREOP, ec, NULL, &returncode, returntext) == SLAPI_DSE_CALLBACK_OK) {
/*
* 3-August-2000 mcs: We used to pass the SLAPI_DUMP_NOOPATTRS
* option to slapi_entry2str_with_options() so that operational
* attributes were NOT stored in the DSE LDIF files. But now
* we store all attribute types.
*/
if ((s = slapi_entry2str_with_options(ec, &len, 0)) != NULL) {
if (slapi_write_buffer(fpw->fpw_prfd, s, len) != len) {
fpw->fpw_rc = PR_GetOSError();
;
slapi_ch_free((void **)&s);
return STOP_TRAVERSAL;
}
if (slapi_write_buffer(fpw->fpw_prfd, "\n", 1) != 1) {
fpw->fpw_rc = PR_GetOSError();
;
slapi_ch_free((void **)&s);
return STOP_TRAVERSAL;
}
slapi_ch_free((void **)&s);
}
}
slapi_entry_free(ec);
}
return 0;
}
/*
* Adds an entry to the dse backend. The passed in entry will be
* free'd always.
*
* return -1 for duplicate entry
* return -2 for schema violation (SCHEMA_VIOLATION)
*/
static int
dse_add_entry_pb(struct dse *pdse, Slapi_Entry *e, Slapi_PBlock *pb)
{
int dont_write_file = 0, merge = 0; /* defaults */
int rc = 0;
struct dse_node *n = dse_node_new(e); /* copies e */
Slapi_Entry *schemacheckentry = NULL; /* to use for schema checking */
PR_ASSERT(pb);
slapi_pblock_get(pb, SLAPI_DSE_DONT_WRITE_WHEN_ADDING, &dont_write_file);
slapi_pblock_get(pb, SLAPI_DSE_MERGE_WHEN_ADDING, &merge);
/* keep write lock during both tree update and file write operations */
if (pdse->dse_rwlock)
slapi_rwlock_wrlock(pdse->dse_rwlock);
if (merge) {
rc = avl_insert(&(pdse->dse_tree), n, entry_dn_cmp, dupentry_merge);
} else {
rc = avl_insert(&(pdse->dse_tree), n, entry_dn_cmp, dupentry_disallow);
}
if (-1 != rc) {
/* update num sub of parent with no lock; we already hold the write lock */
if (0 == rc) { /* entry was added, not merged; update numsub */
/* easter egg entry - don't update num sub */
if (strcmp(slapi_entry_get_ndn(e), "ou=red hat directory server team,cn=monitor") != 0) {
dse_updateNumSubOfParent(pdse, slapi_entry_get_sdn_const(e),
SLAPI_OPERATION_ADD);
}
} else { /* entry was merged, free temp unused data */
dse_node_delete(&n);
}
if (!dont_write_file) {
dse_write_file_nolock(pdse);
}
} else { /* duplicate entry ignored */
dse_node_delete(&n); /* This also deletes the contained entry */
}
if (pdse->dse_rwlock)
slapi_rwlock_unlock(pdse->dse_rwlock);
if (rc == -1) {
/* duplicate entry ignored */
schemacheckentry = dse_get_entry_copy(pdse,
slapi_entry_get_sdn_const(e),
DSE_USE_LOCK);
} else /* entry added or merged */
{
/* entry was added or merged */
if (0 == rc) /* 0 return means entry was added, not merged */
{
/* save a search of the tree, since we added the entry, the
contents of e should be the same as what is in the tree */
schemacheckentry = slapi_entry_dup(e);
} else /* merged */
{
/* schema check the new merged entry, so get it from the tree */
schemacheckentry = dse_get_entry_copy(pdse,
slapi_entry_get_sdn_const(e),
DSE_USE_LOCK);
}
}
if (NULL != schemacheckentry) {
/*
* Verify that the new or merged entry conforms to the schema.
* Errors are logged by slapi_entry_schema_check().
*/
if (slapi_entry_schema_check(pb, schemacheckentry)) {
rc = SCHEMA_VIOLATION;
}
slapi_entry_free(schemacheckentry);
}
/* Callers expect e (SLAPI_ADD_ENTRY) to be freed */
/* This function duplicates 'e' for dse_node 'n' and schemacheckentry.
* 'e' should not have been consumed */
slapi_entry_free(e);
return rc;
}
/*
* Local function for comparing two entries by DN. Store the entries
* so that when they are printed out, the child entries are below their
* ancestor entries
*/
static int
entry_dn_cmp(caddr_t d1, caddr_t d2)
{
struct dse_node *n1 = (struct dse_node *)d1;
struct dse_node *n2 = (struct dse_node *)d2;
const Slapi_DN *dn1 = slapi_entry_get_sdn_const(n1->entry);
const Slapi_DN *dn2 = slapi_entry_get_sdn_const(n2->entry);
int retval = slapi_sdn_compare(dn1, dn2);
if (retval != 0) {
if (slapi_sdn_issuffix(dn1, dn2)) {
retval = 1;
} else if (slapi_sdn_issuffix(dn2, dn1)) {
retval = -1;
} else {
/* put fewer rdns before more rdns */
int rc = 0;
char **dnlist1 = slapi_ldap_explode_dn(slapi_sdn_get_ndn(dn1), 0);
char **dnlist2 = slapi_ldap_explode_dn(slapi_sdn_get_ndn(dn2), 0);
int len1 = 0;
int len2 = 0;
if (dnlist1)
for (len1 = 0; dnlist1[len1]; ++len1)
;
if (dnlist2)
for (len2 = 0; dnlist2[len2]; ++len2)
;
if (len1 == len2) {
len1--;
for (; (rc == 0) && (len1 >= 0); --len1) {
rc = strcmp(dnlist1[len1], dnlist2[len1]);
}
if (rc)
retval = rc;
} else
retval = len1 - len2;
if (dnlist1)
slapi_ldap_value_free(dnlist1);
if (dnlist2)
slapi_ldap_value_free(dnlist2);
}
}
/* else entries are equal if dns are equal */
return retval;
}
static int
dupentry_disallow(caddr_t d1 __attribute__((unused)), caddr_t d2 __attribute__((unused)))
{
return -1;
}
static int
dupentry_replace(caddr_t d1, caddr_t d2)
{
/*
* Hack attack: since we don't have the address of the pointer
* in the avl node, we have to replace the e_dn and e_attrs
* members of the entry which is in the AVL tree with our
* new entry DN and attrs. We then point the "new" entry's
* e_dn and e_attrs pointers to point to the values we just
* replaced, on the assumption that the caller will be freeing
* these.
*/
struct dse_node *n1 = (struct dse_node *)d1; /* OLD */
struct dse_node *n2 = (struct dse_node *)d2; /* NEW */
Slapi_Entry *e = n1->entry;
n1->entry = n2->entry;
n2->entry = e;
return DSE_ENTRY_WAS_REPLACED;
}
static int
dupentry_merge(caddr_t d1, caddr_t d2)
{
struct dse_node *n1 = (struct dse_node *)d1; /* OLD */
struct dse_node *n2 = (struct dse_node *)d2; /* NEW */
Slapi_Entry *e1 = n1->entry;
Slapi_Entry *e2 = n2->entry;
int rc = 0;
Slapi_Attr *newattr = 0;
for (rc = slapi_entry_first_attr(e2, &newattr);
!rc && newattr;
rc = slapi_entry_next_attr(e2, newattr, &newattr)) {
char *type = 0;
slapi_attr_get_type(newattr, &type);
if (type) {
/* insure there are no duplicate values in e1 */
rc = slapi_entry_merge_values_sv(e1, type,
attr_get_present_values(newattr));
}
}
return DSE_ENTRY_WAS_MERGED;
}
/*
* Add an entry to the DSE without locking the DSE avl tree.
* Replaces the entry if it already exists.
*
* The given entry e is never consumed. It is the responsibility of the
* caller to free it when it is no longer needed.
*
* The write_file flag is used if we want to update the entry in memory
* but we do not want to write out the file. For example, if we update
* the numsubordinates in the entry, this is an operational attribute that
* we do not want saved to disk.
*/
static int
dse_replace_entry(struct dse *pdse, Slapi_Entry *e, int write_file, int use_lock)
{
int rc = -1;
if (NULL != e) {
struct dse_node *n = dse_node_new(e);
if (use_lock && pdse->dse_rwlock)
slapi_rwlock_wrlock(pdse->dse_rwlock);
rc = avl_insert(&(pdse->dse_tree), n, entry_dn_cmp, dupentry_replace);
if (write_file)
dse_write_file_nolock(pdse);
/* If the entry was replaced i.e. not added as a new entry, we need to
free the old data, which is set in dupentry_replace */
if (DSE_ENTRY_WAS_REPLACED == rc) {
dse_node_delete(&n);
rc = 0; /* for return to caller */
}
if (use_lock && pdse->dse_rwlock)
slapi_rwlock_unlock(pdse->dse_rwlock);
}
return rc;
}
/*
* Return -1 if p does not point to a valid LDIF
* end-of-record delimiter (a NULL, two newlines, or two
* pairs of CRLF). Otherwise, return the length of
* the delimiter found.
*/
static int
ldif_record_end(char *p)
{
if (NULL != p) {
if ('\0' == *p) {
return 0;
} else if ('\n' == *p && '\n' == *(p + 1)) {
return 2;
} else if ('\r' == *p && '\n' == *(p + 1) && '\r' == *(p + 2) && '\n' == *(p + 3)) {
return 4;
}
}
return -1;
}
char *
dse_read_next_entry(char *buf, char **lastp)
{
char *p, *start;
if (NULL == buf) {
*lastp = NULL;
return NULL;
}
p = start = (NULL == *lastp) ? buf : *lastp;
/* Skip over any leading record delimiters */
while ('\n' == *p || '\r' == *p) {
p++;
}
if ('\0' == *p) {
*lastp = NULL;
return NULL;
}
while ('\0' != *p) {
int rc;
if ((rc = ldif_record_end(p)) >= 0) {
/* Found end of LDIF record */
*p = '\0';
p += rc;
break;
} else {
p++;
}
}
*lastp = p;
return start;
}
/*
* Apply the function to each entry. The caller is responsible for locking
* the rwlock in the dse for the appropriate type of operation e.g. for
* searching, a read lock, for modifying in place, a write lock
*/
static int
dse_apply_nolock(struct dse *pdse, IFP fp, caddr_t arg)
{
avl_apply(pdse->dse_tree, fp, arg, STOP_TRAVERSAL, AVL_INORDER);
return 1;
}
/*
* Remove the entry from the tree.
* Returns 1 if entry is removed and 0 if not.
*/
static int
dse_delete_entry(struct dse *pdse, Slapi_PBlock *pb, const Slapi_Entry *e)
{
int dont_write_file = 0;
struct dse_node *n = dse_node_new(e);
struct dse_node *deleted_node = NULL;
slapi_pblock_get(pb, SLAPI_DSE_DONT_WRITE_WHEN_ADDING, &dont_write_file);
/* keep write lock for both tree deleting and file writing */
if (pdse->dse_rwlock)
slapi_rwlock_wrlock(pdse->dse_rwlock);
if ((deleted_node = (struct dse_node *)avl_delete(&pdse->dse_tree, n, entry_dn_cmp))) {
dse_node_delete(&deleted_node);
}
dse_node_delete(&n);
if (!dont_write_file) {
/* Decrement the numsubordinate count of the parent entry */
dse_updateNumSubOfParent(pdse, slapi_entry_get_sdn_const(e),
SLAPI_OPERATION_DELETE);
dse_write_file_nolock(pdse);
}
if (pdse->dse_rwlock)
slapi_rwlock_unlock(pdse->dse_rwlock);
return 1;
}
/*
* Returns a SLAPI_BIND_xxx retun code.
*/
int
dse_bind(Slapi_PBlock *pb) /* JCM There should only be one exit point from this function! */
{
ber_tag_t method; /* The bind method */
struct berval *cred; /* The bind credentials */
Slapi_Value **bvals;
struct dse *pdse;
Slapi_Attr *attr;
Slapi_DN *sdn = NULL;
Slapi_Entry *ec = NULL;
/*Get the parameters*/
if (slapi_pblock_get(pb, SLAPI_PLUGIN_PRIVATE, &pdse) < 0 ||
slapi_pblock_get(pb, SLAPI_BIND_TARGET_SDN, &sdn) < 0 ||
slapi_pblock_get(pb, SLAPI_BIND_METHOD, &method) < 0 ||
slapi_pblock_get(pb, SLAPI_BIND_CREDENTIALS, &cred) < 0) {
slapi_send_ldap_result(pb, LDAP_OPERATIONS_ERROR, NULL, NULL, 0, NULL);
return SLAPI_BIND_FAIL;
}
/* always allow noauth simple binds */
if (method == LDAP_AUTH_SIMPLE && cred->bv_len == 0) {
/*
* report success to client, but return
* SLAPI_BIND_FAIL so we don't
* authorize based on noauth credentials
*/
slapi_send_ldap_result(pb, LDAP_SUCCESS, NULL, NULL, 0, NULL);
return (SLAPI_BIND_FAIL);
}
ec = dse_get_entry_copy(pdse, sdn, DSE_USE_LOCK);
if (ec == NULL) {
slapi_pblock_set(pb, SLAPI_PB_RESULT_TEXT, "Entry does not exist");
slapi_send_ldap_result(pb, LDAP_INVALID_CREDENTIALS, NULL, NULL, 0, NULL);
return (SLAPI_BIND_FAIL);
}
switch (method) {
case LDAP_AUTH_SIMPLE: {
Slapi_Value cv;
if (slapi_entry_attr_find(ec, "userpassword", &attr) != 0) {
slapi_pblock_set(pb, SLAPI_PB_RESULT_TEXT, "Entry does not have userpassword set");
slapi_send_ldap_result(pb, LDAP_INVALID_CREDENTIALS, NULL, NULL, 0, NULL);
slapi_entry_free(ec);
return SLAPI_BIND_FAIL;
}
bvals = attr_get_present_values(attr);
slapi_value_init_berval(&cv, cred);
if (slapi_pw_find_sv(bvals, &cv) != 0) {
slapi_pblock_set(pb, SLAPI_PB_RESULT_TEXT, "Invalid credentials");
slapi_send_ldap_result(pb, LDAP_INVALID_CREDENTIALS, NULL, NULL, 0, NULL);
slapi_entry_free(ec);
value_done(&cv);
return SLAPI_BIND_FAIL;
}
value_done(&cv);
} break;
default:
slapi_send_ldap_result(pb, LDAP_STRONG_AUTH_NOT_SUPPORTED, NULL, "auth method not supported", 0, NULL);
slapi_entry_free(ec);
return SLAPI_BIND_FAIL;
}
slapi_entry_free(ec);
/* success: front end will send result */
return SLAPI_BIND_SUCCESS;
}
int
dse_unbind(Slapi_PBlock *pb __attribute__((unused)))
{
return 0;
}
/*
* This structure is simply to pass parameters to dse_search_filter_entry.
*/
struct magicSearchStuff
{
Slapi_PBlock *pb;
struct dse *pdse;
int scope;
const Slapi_DN *basedn;
Slapi_Filter *filter;
int nentries;
char **attrs; /*Attributes*/
int attrsonly; /*Should we just return the attributes found?*/
dse_search_set *ss; /* for the temporary results - to pass to the dse search callbacks */
};
/*
* The function which is called on each node of the AVL tree.
*/
static int
dse_search_filter_entry(caddr_t data, caddr_t arg)
{
struct dse_node *n = (struct dse_node *)data;
struct magicSearchStuff *p = (struct magicSearchStuff *)arg;
if (slapi_sdn_scope_test(slapi_entry_get_sdn_const(n->entry), p->basedn, p->scope)) {
if (slapi_vattr_filter_test(p->pb, n->entry, p->filter, 1 /* verify access */) == 0) {
Slapi_Entry *ec = slapi_entry_dup(n->entry);
p->nentries++;
if (!p->ss) {
p->ss = dse_search_set_new();
}
dse_search_set_add_entry(p->ss, ec); /* consumes the entry */
} else {
/*
slapd_log_error_proc("dse_search_filter_entry",
"filter test failed: dn %s did not match filter %d\n",
slapi_entry_get_dn_const(n->entry), p->filter->f_choice);
*/
}
} else {
/*
slapd_log_error_proc("dse_search_filter_entry",
"scope test failed: dn %s is not in scope %d of dn [%s]\n",
slapi_entry_get_dn_const(n->entry), p->scope,
slapi_sdn_get_dn(p->basedn));
*/
}
return 0;
}
/*
* The function which kicks off the traversal of the AVL tree.
* Returns the number of entries returned.
*/
/* jcm: Not very efficient if there are many DSE entries. */
/* jcm: It applies the filter to every node in the tree regardless */
static int
do_dse_search(struct dse *pdse, Slapi_PBlock *pb, int scope, const Slapi_DN *basedn, Slapi_Filter *filter, char **attrs, int attrsonly)
{
struct magicSearchStuff stuff;
stuff.pb = pb;
stuff.pdse = pdse;
stuff.scope = scope;
stuff.basedn = basedn;
stuff.filter = filter;
stuff.nentries = 0;
stuff.attrs = attrs;
stuff.attrsonly = attrsonly;
stuff.ss = NULL;
Operation *pb_op = NULL;
slapi_pblock_get(pb, SLAPI_OPERATION, &pb_op);
/*
* If this is a persistent search and the client is only interested in
* entries that change, we skip looking through the DSE entries.
*/
if (pb_op == NULL || !operation_is_flag_set(pb_op, OP_FLAG_PS_CHANGESONLY)) {
if (pdse->dse_rwlock)
slapi_rwlock_rdlock(pdse->dse_rwlock);
dse_apply_nolock(pdse, dse_search_filter_entry, (caddr_t)&stuff);
if (pdse->dse_rwlock)
slapi_rwlock_unlock(pdse->dse_rwlock);
}
if (stuff.ss) /* something was found which matched our criteria */
{
Slapi_Entry *e = NULL;
for (e = dse_search_set_get_next_entry(stuff.ss);
e;
e = dse_search_set_get_next_entry(stuff.ss)) {
int returncode = 0;
char returntext[SLAPI_DSE_RETURNTEXT_SIZE] = "";
if (dse_call_callback(pdse, pb, SLAPI_OPERATION_SEARCH,
DSE_FLAG_PREOP, e, NULL, &returncode, returntext) == SLAPI_DSE_CALLBACK_OK) {
dse_search_set *ss = NULL;
slapi_pblock_get(pb, SLAPI_SEARCH_RESULT_SET, &ss);
/* if this is the first entry - allocate dse_search_set structure */
if (ss == NULL) {
ss = dse_search_set_new();
slapi_pblock_set(pb, SLAPI_SEARCH_RESULT_SET, ss);
}
/* make another reference to e (stuff.ss references it too)
the stuff.ss reference is removed by dse_search_set_clean()
below, leaving ss as the sole owner of the memory */
dse_search_set_add_entry(ss, e);
} else {
stuff.nentries--; /* rejected entry */
/* this leaves a freed pointer in stuff.ss, but that's ok because
it should never be referenced, and the reference is removed by
the call to dse_search_set_clean() below */
slapi_entry_free(e);
}
}
dse_search_set_clean(stuff.ss);
}
/* the pblock ss now contains the "real" search result set and the copies of
the entries allocated in dse_search_filter_entry; any entries rejected by
the search callback were freed above by the call to slapi_entry_free() */
return stuff.nentries;
}
/*
* -1 means something went wrong.
* 0 means everything went ok.
*/
int
dse_search(Slapi_PBlock *pb) /* JCM There should only be one exit point from this function! */
{
Slapi_Filter *filter;
Slapi_DN *basesdn = NULL;
struct dse *pdse;
char **attrs;
const char *ndn = NULL;
char returntext[SLAPI_DSE_RETURNTEXT_SIZE] = "";
int attrsonly;
int estimate = 0; /* estimated search result set size */
int isrootdse = 0;
int returncode = LDAP_SUCCESS;
int scope;
/*
* Get private information created in the init routine.
* Also get the parameters of the search operation. These come
* more or less directly from the client.
*/
if (slapi_pblock_get(pb, SLAPI_PLUGIN_PRIVATE, &pdse) < 0 ||
slapi_pblock_get(pb, SLAPI_SEARCH_TARGET_SDN, &basesdn) < 0 ||
slapi_pblock_get(pb, SLAPI_SEARCH_SCOPE, &scope) < 0 ||
slapi_pblock_get(pb, SLAPI_SEARCH_FILTER, &filter) < 0 ||
slapi_pblock_get(pb, SLAPI_SEARCH_ATTRS, &attrs) < 0 ||
slapi_pblock_get(pb, SLAPI_SEARCH_ATTRSONLY, &attrsonly) < 0) {
slapi_send_ldap_result(pb, LDAP_OPERATIONS_ERROR, NULL, NULL, 0, NULL);
return (-1);
}
/*
* Sadly the root dse is still a special case. We must not allow
* acl checks on it, or allow onelevel or subtree searches on it.
*/
isrootdse = slapi_sdn_isempty(basesdn);
/* Hopefully this plugin DN mapping can be removed in 3.x */
ndn = slapi_sdn_get_ndn(basesdn);
if (strstr(ndn, "aster replication plugin,cn=plugins,cn=config")) {
/* Map the old "problematic" name to the new one */
slapi_sdn_free(&basesdn);
basesdn = slapi_sdn_new_dn_byval("cn=Multisupplier Replication Plugin,cn=plugins,cn=config");
slapi_pblock_set(pb, SLAPI_SEARCH_TARGET_SDN, basesdn);
}
/*
* Now optimise the filter for use: note that unlike ldbm_search,
* because we don't change the outer filter container, we don't need
* to set back into pb.
*/
slapi_filter_optimise(filter);
switch (scope) {
case LDAP_SCOPE_BASE: {
Slapi_Entry *baseentry = NULL;
baseentry = dse_get_entry_copy(pdse, basesdn, DSE_USE_LOCK);
if (baseentry == NULL) {
slapi_send_ldap_result(pb, LDAP_NO_SUCH_OBJECT, NULL, NULL, 0, NULL);
slapi_log_err(SLAPI_LOG_PLUGIN, "dse_search", "node %s was not found\n",
slapi_sdn_get_dn(basesdn));
return -1;
}
/*
* We don't want to do an acl check for the root dse... because the acl
* code thinks it's a suffix of every target... so every acl applies to
* the root dse... which is wrong.
*/
if (slapi_vattr_filter_test(pb, baseentry, filter, !isrootdse /* verify access */) == 0) {
/* Callbacks modify a copy of the entry */
if (dse_call_callback(pdse, pb, SLAPI_OPERATION_SEARCH,
DSE_FLAG_PREOP, baseentry, NULL, &returncode, returntext) == SLAPI_DSE_CALLBACK_OK) {
dse_search_set *ss;
ss = dse_search_set_new();
slapi_pblock_set(pb, SLAPI_SEARCH_RESULT_SET, ss);
dse_search_set_add_entry(ss, baseentry); /* consumes the entry */
baseentry = NULL;
estimate = 1; /* scope base */
}
}
slapi_entry_free(baseentry);
} break;
case LDAP_SCOPE_ONELEVEL:
/* FALL THROUGH */
case LDAP_SCOPE_SUBTREE:
if (!isrootdse) {
estimate = do_dse_search(pdse, pb, scope, basesdn, filter, attrs, attrsonly);
}
break;
}
slapi_pblock_set(pb, SLAPI_SEARCH_RESULT_SET_SIZE_ESTIMATE, &estimate);
/* Search is done, send LDAP_SUCCESS */
return 0;
}
int32_t
dse_compare(Slapi_PBlock *pb)
{
/*
* Inspired largely by ldbm_compare.c. Allow schema aware comparison
* of entries in the DSE, including cn=config.
*/
backend *be = NULL;
char *type = NULL;
struct berval *bval = NULL;
Slapi_DN *sdn = NULL;
struct dse *pdse = NULL;
Slapi_Entry *ec = NULL;
Slapi_Value compare_value = {0};
slapi_pblock_get(pb, SLAPI_BACKEND, &be);
slapi_pblock_get(pb, SLAPI_PLUGIN_PRIVATE, &pdse);
slapi_pblock_get(pb, SLAPI_TARGET_SDN, &sdn);
slapi_pblock_get(pb, SLAPI_COMPARE_TYPE, &type);
slapi_pblock_get(pb, SLAPI_COMPARE_VALUE, &bval);
/* get the entry */
ec = dse_get_entry_copy(pdse, sdn, DSE_USE_LOCK);
if (ec == NULL) {
slapi_send_ldap_result(pb, LDAP_NO_SUCH_OBJECT, NULL, NULL, 0, NULL);
return -1;
}
/* Access control check */
int32_t err = slapi_access_allowed(pb, ec, type, bval, SLAPI_ACL_COMPARE);
if (err != LDAP_SUCCESS) {
slapi_entry_free(ec);
slapi_send_ldap_result(pb, err, NULL, NULL, 0, NULL);
return -1;
}
/* If cn=config, setup the entry with ALL values we could check from defaults */
Slapi_DN config_dn;
slapi_sdn_init_ndn_byref(&config_dn, "cn=config");
if (slapi_sdn_compare(&config_dn, sdn) == 0) {
read_config_dse(pb, ec, NULL, &err, NULL, NULL);
/*
* read_config_dse, and in turn, config_set_entry always returns
* a 1 here, which is probably dse_callback related.
*/
if (err != 1) {
slapi_send_ldap_result(pb, LDAP_OPERATIONS_ERROR, NULL, NULL, 0, NULL);
return -1;
}
/*
* cn=config is now populated
*/
}
slapi_sdn_done(&config_dn);
/* Do the schema aware check. */
slapi_value_init_berval(&compare_value, bval);
int32_t result = 0;
err = slapi_vattr_value_compare(ec, type, &compare_value, &result, 0);
/* We have the results, now free and then send. */
slapi_entry_free(ec);
value_done(&compare_value);
/* Format the result as expected. */
if (err != LDAP_SUCCESS) {
if (SLAPI_VIRTUALATTRS_NOT_FOUND == err) {
slapi_send_ldap_result(pb, LDAP_NO_SUCH_ATTRIBUTE, NULL, NULL, 0, NULL);
} else {
/* Some other problem, call it an operations error */
slapi_send_ldap_result(pb, LDAP_OPERATIONS_ERROR, NULL, NULL, 0, NULL);
}
return -1;
} else {
if (result != 0) {
slapi_send_ldap_result(pb, LDAP_COMPARE_TRUE, NULL, NULL, 0, NULL);
} else {
slapi_send_ldap_result(pb, LDAP_COMPARE_FALSE, NULL, NULL, 0, NULL);
}
}
return 0;
}
/*
* -1 means something went wrong.
* 0 means everything went ok.
*/
static int
dse_modify_return(int rv, Slapi_Entry *ec, Slapi_Entry *ecc)
{
slapi_entry_free(ec);
slapi_entry_free(ecc);
return rv;
}
int
dse_modify(Slapi_PBlock *pb) /* JCM There should only be one exit point from this function! */
{
int err; /*House keeping stuff*/
LDAPMod **mods; /*Used to apply the modifications*/
LDAPMod **original_mods = NULL; /* some mods can be removed by callback, save them for later logging */
char *errbuf = NULL; /* To get error back */
struct dse *pdse;
Slapi_Entry *ec = NULL;
Slapi_Entry *ecc = NULL;
int returncode = LDAP_SUCCESS;
char returntext[SLAPI_DSE_RETURNTEXT_SIZE] = "";
Slapi_DN *sdn = NULL;
Slapi_DN *old_repl_sdn = NULL;
int dont_write_file = 0; /* default */
int rc = SLAPI_DSE_CALLBACK_DO_NOT_APPLY;
int retval = -1;
int need_be_postop = 0;
int plugin_started = 0;
int internal_op = 0;
int fixup_op = 0;
PRBool global_lock_owned = PR_FALSE;
Operation *pb_op = NULL;
PR_ASSERT(pb);
if (slapi_pblock_get(pb, SLAPI_PLUGIN_PRIVATE, &pdse) < 0 ||
/* slapi_pblock_get( pb, SLAPI_MODIFY_TARGET, &dn ) < 0 || */
slapi_pblock_get(pb, SLAPI_MODIFY_TARGET_SDN, &sdn) < 0 ||
slapi_pblock_get(pb, SLAPI_MODIFY_MODS, &mods) < 0 || (NULL == pdse)) {
returncode = LDAP_OPERATIONS_ERROR;
goto done;
}
slapi_pblock_get(pb, SLAPI_DSE_DONT_WRITE_WHEN_ADDING, &dont_write_file);
if (!dont_write_file && dse_check_for_readonly_error(pb, pdse)) {
/* already returned result */
return retval;
}
/* Hopefully this plugin DN mapping can be removed in 3.x */
old_repl_sdn = slapi_sdn_new_dn_byval("cn=Multimaster Replication Plugin,cn=plugins,cn=config");
if(slapi_sdn_compare(sdn, old_repl_sdn) == 0) {
/* Map the old name to the new one */
slapi_sdn_free(&sdn);
sdn = slapi_sdn_new_dn_byval("cn=Multisupplier Replication Plugin,cn=plugins,cn=config");
slapi_pblock_set(pb, SLAPI_MODIFY_TARGET_SDN, sdn);
}
slapi_sdn_free(&old_repl_sdn);
slapi_pblock_get(pb, SLAPI_OPERATION, &pb_op);
if (pb_op){
internal_op = operation_is_flag_set(pb_op, OP_FLAG_INTERNAL);
fixup_op = operation_is_flag_set(pb_op, SLAPI_OP_FLAG_FIXUP);
}
/* Find the entry we are about to modify. */
ec = dse_get_entry_copy(pdse, sdn, DSE_USE_LOCK);
if (ec == NULL) {
returncode = LDAP_NO_SUCH_OBJECT;
goto done;
}
/* Check acl */
err = plugin_call_acl_mods_access(pb, ec, mods, &errbuf);
if (err != LDAP_SUCCESS) {
returncode = err;
if (errbuf) {
PL_strncpyz(returntext, errbuf, sizeof(returntext));
slapi_ch_free_string(&errbuf);
}
goto done;
}
/* Save away a copy of the entry, before modifications */
slapi_pblock_set(pb, SLAPI_ENTRY_PRE_OP, slapi_entry_dup(ec)); /* JCM - When does this get free'd? */
/* richm - it is freed in modify.c */
/* Modify a copy of the entry*/
ecc = slapi_entry_dup(ec);
err = entry_apply_mods(ecc, mods);
/* Possibly acquire the global backend lock */
if (global_backend_lock_requested()) {
global_backend_lock_lock();
global_lock_owned = PR_TRUE;
}
original_mods = copy_mods(mods);
/* XXXmcs: should we expand objectclass values here?? */
/* give the dse callbacks the first crack at the modify */
rc = dse_call_callback(pdse, pb, SLAPI_OPERATION_MODIFY, DSE_FLAG_PREOP, ec, ecc, &returncode, returntext);
if (SLAPI_DSE_CALLBACK_OK == rc) {
int plugin_rc;
/* next, give the be plugins a crack at it */
slapi_pblock_set(pb, SLAPI_RESULT_CODE, &returncode);
slapi_pblock_set(pb, SLAPI_MODIFY_EXISTING_ENTRY, ecc);
plugin_rc = plugin_call_plugins(pb, SLAPI_PLUGIN_BE_PRE_MODIFY_FN);
need_be_postop = 1; /* if the be preops were called, have to call the be postops too */
if (!returncode) {
slapi_pblock_get(pb, SLAPI_RESULT_CODE, &returncode);
}
if (!plugin_rc && !returncode) {
/* finally, give the betxn plugins a crack at it */
plugin_rc = plugin_call_plugins(pb, SLAPI_PLUGIN_BE_TXN_PRE_MODIFY_FN);
if (!returncode) {
slapi_pblock_get(pb, SLAPI_RESULT_CODE, &returncode);
}
if (plugin_rc || returncode) {
slapi_log_err(SLAPI_DSE_TRACELEVEL,
"dse_modify", "SLAPI_PLUGIN_BE_TXN_PRE_MODIFY_FN failed - rc %d LDAP error %d:%s\n",
plugin_rc, returncode, ldap_err2string(returncode));
}
} else {
slapi_log_err(SLAPI_DSE_TRACELEVEL,
"dse_modify", "SLAPI_PLUGIN_BE_PRE_MODIFY_FN failed - rc %d LDAP error %d:%s\n",
rc, returncode, ldap_err2string(returncode));
}
if (plugin_rc || returncode) {
char *ldap_result_message = NULL;
rc = SLAPI_DSE_CALLBACK_ERROR;
if (!returncode) {
slapi_log_err(SLAPI_DSE_TRACELEVEL,
"dse_modify", "PRE_MODIFY plugin returned non-zero but did not set an LDAP error\n");
returncode = LDAP_OPERATIONS_ERROR;
}
if (!returntext[0]) {
slapi_pblock_get(pb, SLAPI_PB_RESULT_TEXT, &ldap_result_message);
if (ldap_result_message && ldap_result_message[0]) {
PL_strncpyz(returntext, ldap_result_message, sizeof(returntext));
}
}
} else {
/*
* If we are using dynamic plugins, and we are modifying a plugin
* we need to do some additional checks. First, check if we are
* enabling/disabling a plugin. Then make sure the plugin still
* starts after applying the plugin changes.
*/
rc = SLAPI_DSE_CALLBACK_OK;
if (slapi_entry_attr_hasvalue(ec, SLAPI_ATTR_OBJECTCLASS, "nsSlapdPlugin")) {
if (config_get_dynamic_plugins() || fixup_op) {
if ((plugin_started = dse_modify_plugin(ec, ecc, returntext)) == -1) {
returncode = LDAP_UNWILLING_TO_PERFORM;
rc = SLAPI_DSE_CALLBACK_ERROR;
retval = -1;
goto done;
}
/*
* If this is not a internal operation, make sure the plugin
* can be restarted.
*/
if (!internal_op) {
if (dse_pre_modify_plugin(ec, ecc, mods)) {
char *errtext;
slapi_pblock_get(pb, SLAPI_PB_RESULT_TEXT, &errtext);
if (errtext) {
PL_strncpyz(returntext,
"Failed to apply plugin config change, "
"check the errors log for more info.",
sizeof(returntext));
}
returncode = LDAP_UNWILLING_TO_PERFORM;
rc = SLAPI_DSE_CALLBACK_ERROR;
retval = -1;
goto done;
}
}
} else {
slapi_log_err(SLAPI_LOG_NOTICE, "dse_modify",
"A plugin has been enabled or disabled, "
"but nsslapd-dynamic-plugins is off. A server restart is required to change this plugin state.\n");
} /* end config_get_dynamic_plugins */
} /* end has nsSlapdPlugin */
}
}
switch (rc) {
case SLAPI_DSE_CALLBACK_ERROR:
/* Error occured in the callback -- return error code from callback */
goto done;
break;
case SLAPI_DSE_CALLBACK_DO_NOT_APPLY:
/* Callback says don't apply the changes -- return Success */
returncode = LDAP_SUCCESS;
returntext[0] = '\0';
retval = 0;
goto done;
break;
case SLAPI_DSE_CALLBACK_OK: {
/* The callback may alter the mods in the pblock. This happens
for example in the schema code. Since the schema attributes
are managed exclusively by the schema code, we should not
apply those mods. However, for reasons unknown to me, we
must in the general case call entry_apply_mods before calling
the modify callback above. In the case of schema, the schema
code will remove the schema attributes from the mods. So, we
reapply the mods to the entry for the attributes we manage in
the dse code (e.g. aci)
*/
int reapply_mods = 0; /* default is to not reapply entry_apply_mods */
slapi_pblock_get(pb, SLAPI_DSE_REAPPLY_MODS, &reapply_mods);
/* Callback says apply the changes */
if (reapply_mods) {
LDAPMod **modsagain = NULL; /*Used to apply the modifications*/
slapi_pblock_get(pb, SLAPI_MODIFY_MODS, &modsagain);
if (NULL != modsagain) {
/* the dse modify callback must have modified ecc back to it's
original state, before the earlier apply_mods, but without the
attributes it did not want us to apply mods to */
err = entry_apply_mods(ecc, modsagain);
}
}
if (err != 0) {
returncode = err;
returntext[0] = '\0';
retval = -1;
goto done;
}
break;
}
}
/* We're applying the mods... check that the entry still obeys the schema */
if (slapi_entry_schema_check(pb, ecc) != 0) {
char *errtext;
slapi_pblock_get(pb, SLAPI_PB_RESULT_TEXT, &errtext);
if (errtext) {
PL_strncpyz(returntext, errtext, sizeof(returntext));
}
returncode = LDAP_OBJECT_CLASS_VIOLATION;
retval = -1;
goto done;
}
/* Check if the attribute values in the mods obey the syntaxes */
if (slapi_mods_syntax_check(pb, mods, 0) != 0) {
char *errtext;
slapi_pblock_get(pb, SLAPI_PB_RESULT_TEXT, &errtext);
if (errtext) {
PL_strncpyz(returntext, errtext, sizeof(returntext));
}
returncode = LDAP_INVALID_SYNTAX;
retval = -1;
goto done;
}
/* Change the entry itself both on disk and in the AVL tree */
/* dse_replace_entry free's the existing entry. */
if (dse_replace_entry(pdse, ecc, !dont_write_file, DSE_USE_LOCK) != 0) {
returncode = LDAP_OPERATIONS_ERROR;
retval = -1;
goto done;
}
retval = 0; /* so far, so good */
slapi_pblock_set(pb, SLAPI_ENTRY_POST_OP, slapi_entry_dup(ecc)); /* JCM - When does this get free'd? */
/* richm - it is freed in modify.c */
/* give the dse callbacks the first crack at the modify */
rc = dse_call_callback(pdse, pb, SLAPI_OPERATION_MODIFY, DSE_FLAG_POSTOP, ec, ecc, &returncode, returntext);
done:
if (rc != SLAPI_DSE_CALLBACK_DO_NOT_APPLY) {
/* make sure OPRETURN is set */
slapi_pblock_get(pb, SLAPI_PLUGIN_OPRETURN, &err);
if ((retval || returncode) && !err) {
slapi_pblock_set(pb, SLAPI_PLUGIN_OPRETURN, retval ? &retval : &returncode);
}
/* next, give the betxn plugins a crack at it */
slapi_pblock_set(pb, SLAPI_RESULT_CODE, &returncode);
slapi_pblock_set(pb, SLAPI_MODIFY_EXISTING_ENTRY, ecc);
if (need_be_postop) {
plugin_call_plugins(pb, SLAPI_PLUGIN_BE_TXN_POST_MODIFY_FN);
if (!returncode) {
slapi_pblock_get(pb, SLAPI_RESULT_CODE, &returncode);
}
if (returncode && !returntext[0]) {
char *ldap_result_message = NULL;
slapi_pblock_get(pb, SLAPI_PB_RESULT_TEXT, &ldap_result_message);
if (ldap_result_message && ldap_result_message[0]) {
PL_strncpyz(returntext, ldap_result_message, sizeof(returntext));
}
}
plugin_call_plugins(pb, SLAPI_PLUGIN_BE_POST_MODIFY_FN);
if (!returncode) {
slapi_pblock_get(pb, SLAPI_RESULT_CODE, &returncode);
}
}
} else {
/* It should not happen but just be paranoiac, do not
* forget to call the postop if needed
*/
if (need_be_postop) {
plugin_call_plugins(pb, SLAPI_PLUGIN_BE_TXN_POST_MODIFY_FN);
if (!returncode) {
slapi_pblock_get(pb, SLAPI_RESULT_CODE, &returncode);
}
plugin_call_plugins(pb, SLAPI_PLUGIN_BE_POST_MODIFY_FN);
if (!returncode) {
slapi_pblock_get(pb, SLAPI_RESULT_CODE, &returncode);
}
}
}
/* time to restore original mods */
if (original_mods) {
LDAPMod **mods_from_callback;
slapi_pblock_get(pb, SLAPI_MODIFY_MODS, &mods_from_callback);
ldap_mods_free(mods_from_callback, 1 /* Free the Array and the Elements */);
slapi_pblock_set(pb, SLAPI_MODIFY_MODS, original_mods);
}
if (global_lock_owned) {
global_backend_lock_unlock();
}
slapi_send_ldap_result(pb, returncode, NULL, returntext[0] ? returntext : NULL, 0, NULL);
return dse_modify_return(retval, ec, ecc);
}
static int
dse_pre_modify_plugin(Slapi_Entry *entryBefore, Slapi_Entry *entryAfter, LDAPMod **mods)
{
const char *enabled = NULL;
int restart_plugin = 1;
int rc = 0;
int i;
/*
* Only check the mods if the plugin is enabled - no need to restart a plugin if it's not running.
*/
if ((enabled = slapi_entry_attr_get_ref(entryBefore, ATTR_PLUGIN_ENABLED)) &&
!strcasecmp(enabled, "on")) {
for (i = 0; mods && mods[i]; i++) {
if (strcasecmp(mods[i]->mod_type, ATTR_PLUGIN_ENABLED) == 0) {
/* we already stop/started the pugin - don't do it again */
restart_plugin = 0;
break;
}
}
if (restart_plugin) { /* for all other plugin config changes, restart the plugin */
if (plugin_restart(entryBefore, entryAfter) != LDAP_SUCCESS) {
slapi_log_err(SLAPI_LOG_ERR, "dse_pre_modify_plugin",
"The configuration change for plugin (%s) could not be applied.\n",
slapi_entry_get_dn(entryBefore));
rc = -1;
}
}
}
return rc;
}
/*
* If this is modifying a plugin, check if we are disabling/enabling it - update the
* global plugins as needed.
*
* Return 1 if the plugin was successfully started
* Return 2 if the plugin was successfully stopped
* Return -1 on error
* Return 0 if nothing was done
*/
static int
dse_modify_plugin(Slapi_Entry *pre_entry, Slapi_Entry *post_entry, char *returntext)
{
int rc = LDAP_SUCCESS;
if (slapi_entry_attr_hasvalue(pre_entry, "nsslapd-pluginEnabled", "on") &&
slapi_entry_attr_hasvalue(post_entry, "nsslapd-pluginEnabled", "off")) {
/*
* Plugin has been disabled
*/
if (plugin_delete(post_entry, returntext, 0 /* not locked */)) {
rc = -1;
} else {
rc = 2; /* plugin disabled */
slapi_log_err(SLAPI_LOG_PLUGIN, "dse_modify_plugin", "Disabled plugin (%s)\n",
slapi_entry_get_dn(post_entry));
}
} else if (slapi_entry_attr_hasvalue(pre_entry, "nsslapd-pluginEnabled", "off") &&
slapi_entry_attr_hasvalue(post_entry, "nsslapd-pluginEnabled", "on")) {
/*
* Plugin has been enabled
*/
if (plugin_add(post_entry, returntext, 0 /* not locked */)) {
rc = -1;
} else {
rc = 1; /* plugin started */
slapi_log_err(SLAPI_LOG_PLUGIN, "dse_modify_plugin", "Enabled plugin (%s)\n",
slapi_entry_get_dn(post_entry));
}
}
return rc;
}
/*
* Add the plugin to the global plugin list
*/
static int
dse_add_plugin(Slapi_Entry *entry, char *returntext)
{
int rc = LDAP_SUCCESS;
if (!slapi_entry_attr_hasvalue(entry, SLAPI_ATTR_OBJECTCLASS, "nsSlapdPlugin") ||
!config_get_dynamic_plugins()) {
/*
* This is not a plugin, or we are not allowing dynamic updates.
*/
return rc;
}
rc = plugin_add(entry, returntext, 0 /* not locked */);
return rc;
}
/*
* Delete the plugin from the global plugin list
*/
static int
dse_delete_plugin(Slapi_Entry *entry, char *returntext)
{
int rc = LDAP_SUCCESS;
if (!slapi_entry_attr_hasvalue(entry, SLAPI_ATTR_OBJECTCLASS, "nsSlapdPlugin") ||
slapi_entry_attr_hasvalue(entry, "nsslapd-PluginEnabled", "off") ||
!config_get_dynamic_plugins()) {
/*
* This is not a plugin, this plugin was not enabled to begin with, or we
* are not allowing dynamic updates .
*/
return rc;
}
rc = plugin_delete(entry, returntext, 0 /* not locked */);
return rc;
}
static int
dse_add_return(int rv, Slapi_Entry *e)
{
slapi_entry_free(e);
return rv;
}
/*
* -1 means something went wrong.
* 0 means everything went ok.
*/
int
dse_add(Slapi_PBlock *pb) /* JCM There should only be one exit point from this function! */
{
Slapi_Entry *e = NULL; /*The new entry to add*/
Slapi_Entry *e_copy = NULL; /* copy of added entry */
char *errbuf = NULL;
int rc = LDAP_SUCCESS;
int error = -1;
int dont_write_file = 0; /* default */
struct dse *pdse;
int returncode = LDAP_SUCCESS;
char returntext[SLAPI_DSE_RETURNTEXT_SIZE] = "";
Slapi_DN *sdn = NULL;
Slapi_DN parent;
int need_be_postop = 0;
PRBool global_lock_owned = PR_FALSE;
/*
* Get the database, the dn and the entry to add
*/
if (slapi_pblock_get(pb, SLAPI_PLUGIN_PRIVATE, &pdse) < 0 ||
slapi_pblock_get(pb, SLAPI_ADD_TARGET_SDN, &sdn) < 0 ||
slapi_pblock_get(pb, SLAPI_ADD_ENTRY, &e) < 0 || (NULL == pdse)) {
rc = LDAP_OPERATIONS_ERROR;
goto done;
}
slapi_pblock_get(pb, SLAPI_DSE_DONT_WRITE_WHEN_ADDING, &dont_write_file);
if (!dont_write_file && dse_check_for_readonly_error(pb, pdse)) {
return (error); /* result already sent */
}
/*
* Check to make sure the entry passes the schema check
*/
if (slapi_entry_schema_check(pb, e) != 0) {
char *errtext;
slapi_log_err(SLAPI_DSE_TRACELEVEL,
"dse_add", "entry failed schema check\n");
slapi_pblock_get(pb, SLAPI_PB_RESULT_TEXT, &errtext);
if (errtext && errtext[0]) {
PL_strncpyz(returntext, errtext, sizeof(returntext));
}
rc = LDAP_OBJECT_CLASS_VIOLATION;
e = NULL; /* caller will free upon error */
goto done;
}
/* Check if the attribute values in the entry obey the syntaxes */
if (slapi_entry_syntax_check(pb, e, 0) != 0) {
char *errtext;
slapi_log_err(SLAPI_DSE_TRACELEVEL,
"dse_add", "entry failed syntax check\n");
slapi_pblock_get(pb, SLAPI_PB_RESULT_TEXT, &errtext);
if (errtext && errtext[0]) {
PL_strncpyz(returntext, errtext, sizeof(returntext));
}
rc = LDAP_INVALID_SYNTAX;
e = NULL; /* caller will free upon error */
goto done;
}
/*
* Attempt to find this dn.
*/
{
Slapi_Entry *existingentry = dse_get_entry_copy(pdse, sdn, DSE_USE_LOCK);
if (existingentry != NULL) {
/*
* If we've reached this code, there is an entry
* whose dn matches dn, so tell the user and return
*/
slapi_entry_free(existingentry);
rc = LDAP_ALREADY_EXISTS;
e = NULL; /* caller will free upon error */
goto done;
}
}
/*
* Get the parent dn and see if the corresponding entry exists.
* If the parent does not exist, only allow the "root" user to
* add the entry.
*/
slapi_sdn_init(&parent);
slapi_sdn_get_parent(sdn, &parent);
if (!slapi_sdn_isempty(&parent)) {
Slapi_Entry *parententry = NULL;
parententry = dse_get_entry_copy(pdse, &parent, DSE_USE_LOCK);
if (parententry == NULL) {
rc = LDAP_NO_SUCH_OBJECT;
slapi_log_err(SLAPI_DSE_TRACELEVEL, " dse_add", "Narent does not exist\n");
slapi_sdn_done(&parent);
e = NULL; /* caller will free upon error */
goto done;
}
rc = plugin_call_acl_plugin(pb, parententry, NULL, NULL, SLAPI_ACL_ADD, ACLPLUGIN_ACCESS_DEFAULT, &errbuf);
slapi_entry_free(parententry);
if (rc != LDAP_SUCCESS) {
slapi_log_err(SLAPI_DSE_TRACELEVEL, "dse_add", "No access to parent\n");
if (errbuf && errbuf[0]) {
PL_strncpyz(returntext, errbuf, sizeof(returntext));
}
slapi_ch_free_string(&errbuf);
slapi_sdn_done(&parent);
e = NULL; /* caller will free upon error */
goto done;
}
} else {
/* no parent */
int isroot;
slapi_pblock_get(pb, SLAPI_REQUESTOR_ISROOT, &isroot);
if (!isroot) {
slapi_log_err(SLAPI_DSE_TRACELEVEL, "dse_add", "No parent and not root\n");
rc = LDAP_INSUFFICIENT_ACCESS;
slapi_sdn_done(&parent);
e = NULL; /* caller will free upon error */
goto done;
}
}
slapi_sdn_done(&parent);
/*
* Before we add the entry, find out if the syntax of the aci
* aci attribute values are correct or not. We don't want to add
* the entry if the syntax is incorrect.
*/
if (plugin_call_acl_verify_syntax(pb, e, &errbuf) != 0) {
if (errbuf && errbuf[0]) {
PL_strncpyz(returntext, errbuf, sizeof(returntext));
slapi_ch_free_string(&errbuf);
}
rc = LDAP_INVALID_SYNTAX;
e = NULL; /* caller will free upon error */
goto done;
}
/* Possibly acquire the global backend lock */
if (global_backend_lock_requested()) {
global_backend_lock_lock();
global_lock_owned = PR_TRUE;
}
if (dse_call_callback(pdse, pb, SLAPI_OPERATION_ADD, DSE_FLAG_PREOP, e,
NULL, &returncode, returntext) != SLAPI_DSE_CALLBACK_OK) {
if (!returncode) {
slapi_log_err(SLAPI_LOG_ERR, "dse_add",
"DSE PREOP callback returned error but did not set returncode\n");
returncode = LDAP_OPERATIONS_ERROR;
}
rc = returncode;
e = NULL; /* caller will free upon error */
goto done;
}
/* next, give the be plugins a crack at it */
slapi_pblock_set(pb, SLAPI_RESULT_CODE, &returncode);
plugin_call_plugins(pb, SLAPI_PLUGIN_BE_PRE_ADD_FN);
need_be_postop = 1; /* have to call be postops now */
if (!returncode) {
slapi_pblock_get(pb, SLAPI_RESULT_CODE, &returncode);
}
if (!returncode) {
/* finally, give the betxn plugins a crack at it */
plugin_call_plugins(pb, SLAPI_PLUGIN_BE_TXN_PRE_ADD_FN);
if (!returncode) {
slapi_pblock_get(pb, SLAPI_RESULT_CODE, &returncode);
}
}
if (returncode) {
if (!returntext[0]) {
char *ldap_result_message = NULL;
slapi_pblock_get(pb, SLAPI_PB_RESULT_TEXT, &ldap_result_message);
if (ldap_result_message && ldap_result_message[0]) {
PL_strncpyz(returntext, ldap_result_message, sizeof(returntext));
}
}
rc = returncode;
e = NULL; /* caller will free upon error */
goto done;
}
/*
* Check if we are adding a plugin
*/
if (dse_add_plugin(e, returntext)) {
returncode = LDAP_UNWILLING_TO_PERFORM;
goto done;
}
/* make copy for postop fns because add_entry_pb consumes the given entry */
e_copy = slapi_entry_dup(e);
if (dse_add_entry_pb(pdse, e_copy, pb) != 0) {
rc = LDAP_OPERATIONS_ERROR;
e = NULL; /* caller will free upon error */
goto done;
}
/* The postop must be called after the write lock is released. */
dse_call_callback(pdse, pb, SLAPI_OPERATION_ADD, DSE_FLAG_POSTOP, e, NULL, &returncode, returntext);
done:
if (e) {
slapi_pblock_set(pb, SLAPI_ENTRY_POST_OP, slapi_entry_dup(e));
}
/* make sure OPRETURN and RESULT_CODE are set */
slapi_pblock_get(pb, SLAPI_PLUGIN_OPRETURN, &error);
if (rc || returncode) {
if (!error) {
slapi_pblock_set(pb, SLAPI_PLUGIN_OPRETURN, rc ? &rc : &returncode);
}
if (!returncode) {
returncode = rc;
}
}
if (need_be_postop) {
/* next, give the be txn plugins a crack at it */
slapi_pblock_set(pb, SLAPI_RESULT_CODE, &returncode);
plugin_call_plugins(pb, SLAPI_PLUGIN_BE_TXN_POST_ADD_FN);
/* finally, give the be plugins a crack at it */
plugin_call_plugins(pb, SLAPI_PLUGIN_BE_POST_ADD_FN);
if (!returncode) {
slapi_pblock_get(pb, SLAPI_RESULT_CODE, &returncode);
}
}
if (global_lock_owned) {
global_backend_lock_unlock();
}
slapi_send_ldap_result(pb, returncode, NULL, returntext[0] ? returntext : NULL, 0, NULL);
return dse_add_return(rc, e);
}
/*
* -1 means something went wrong.
* 0 means everything went ok.
*/
static int
dse_delete_return(int rv, Slapi_Entry *ec)
{
slapi_entry_free(ec);
return rv;
}
int
dse_delete(Slapi_PBlock *pb) /* JCM There should only be one exit point from this function! */
{
int rc = -1;
int dont_write_file = 0; /* default */
struct dse *pdse = NULL;
int returncode = LDAP_SUCCESS;
char returntext[SLAPI_DSE_RETURNTEXT_SIZE] = "";
char *entry_str = "entry";
char *errbuf = NULL;
char *attrs[2] = {NULL, NULL};
Slapi_DN *sdn = NULL;
Slapi_Entry *ec = NULL; /* copy of entry to delete */
Slapi_Entry *orig_entry = NULL;
int need_be_postop = 0;
PRBool global_lock_owned = PR_FALSE;
/*
* Get the database and the dn
*/
if (slapi_pblock_get(pb, SLAPI_PLUGIN_PRIVATE, &pdse) < 0 ||
slapi_pblock_get(pb, SLAPI_DELETE_TARGET_SDN, &sdn) < 0 ||
(pdse == NULL)) {
returncode = LDAP_OPERATIONS_ERROR;
goto done;
}
slapi_pblock_get(pb, SLAPI_DSE_DONT_WRITE_WHEN_ADDING, &dont_write_file);
if (!dont_write_file && dse_check_for_readonly_error(pb, pdse)) {
return (rc); /* result already sent */
}
ec = dse_get_entry_copy(pdse, sdn, DSE_USE_LOCK);
if (ec == NULL) {
returncode = LDAP_NO_SUCH_OBJECT;
goto done;
}
/*
* Check if this node has any children.
*/
if (dse_numsubordinates(ec) > 0) {
returncode = LDAP_NOT_ALLOWED_ON_NONLEAF;
goto done;
}
/*
* Check the access
*/
attrs[0] = entry_str;
attrs[1] = NULL;
returncode = plugin_call_acl_plugin(pb, ec, attrs, NULL, SLAPI_ACL_DELETE, ACLPLUGIN_ACCESS_DEFAULT, &errbuf);
if (returncode != LDAP_SUCCESS) {
if (errbuf && errbuf[0]) {
PL_strncpyz(returntext, errbuf, sizeof(returntext));
}
slapi_ch_free_string(&errbuf);
goto done;
}
/* Possibly acquire the global backend lock */
if (global_backend_lock_requested()) {
global_backend_lock_lock();
global_lock_owned = PR_TRUE;
}
if (dse_call_callback(pdse, pb, SLAPI_OPERATION_DELETE, DSE_FLAG_PREOP, ec, NULL, &returncode, returntext) == SLAPI_DSE_CALLBACK_OK) {
slapi_pblock_set(pb, SLAPI_DELETE_BEPREOP_ENTRY, ec);
slapi_pblock_set(pb, SLAPI_RESULT_CODE, &returncode);
plugin_call_plugins(pb, SLAPI_PLUGIN_BE_PRE_DELETE_FN);
need_be_postop = 1;
if (!returncode) {
slapi_pblock_get(pb, SLAPI_RESULT_CODE, &returncode);
}
if (!returncode) {
plugin_call_plugins(pb, SLAPI_PLUGIN_BE_TXN_PRE_DELETE_FN);
if (!returncode) {
slapi_pblock_get(pb, SLAPI_RESULT_CODE, &returncode);
}
if (!returncode) {
if (dse_delete_entry(pdse, pb, ec) == 0) {
returncode = LDAP_OPERATIONS_ERROR;
}
}
}
/* Setting SLAPI_ENTRY_PRE_OP here,
* since some betxn postop may need the pre op entry. */
slapi_pblock_set(pb, SLAPI_ENTRY_PRE_OP, slapi_entry_dup(ec));
} else {
goto done;
}
dse_call_callback(pdse, pb, SLAPI_OPERATION_DELETE, DSE_FLAG_POSTOP, ec, NULL, &returncode, returntext);
done:
slapi_pblock_get(pb, SLAPI_DELETE_BEPOSTOP_ENTRY, &orig_entry);
/* coverity false positive:
* Cvar_deref_model: Passing null pointer "ec" to "slapi_pblock_set", which dereferences it.
* but ec is not dereferenced in SLAPI_DELETE_BEPOSTOP_ENTRY case so lets ignore this one.
*/
/* coverity[var_deref_model] */
slapi_pblock_set(pb, SLAPI_DELETE_BEPOSTOP_ENTRY, ec);
/* make sure OPRETURN and RESULT_CODE are set */
slapi_pblock_get(pb, SLAPI_PLUGIN_OPRETURN, &rc);
if (returncode || rc) {
if (!rc) {
slapi_pblock_set(pb, SLAPI_PLUGIN_OPRETURN, &returncode);
}
if (!returncode) {
returncode = rc;
}
}
if (need_be_postop) {
plugin_call_plugins(pb, SLAPI_PLUGIN_BE_TXN_POST_DELETE_FN);
if (!returncode) {
slapi_pblock_get(pb, SLAPI_RESULT_CODE, &returncode);
}
/* finally, give the be plugins a crack at it */
plugin_call_plugins(pb, SLAPI_PLUGIN_BE_POST_DELETE_FN);
if (!returncode) {
slapi_pblock_get(pb, SLAPI_RESULT_CODE, &returncode);
}
}
if (global_lock_owned) {
global_backend_lock_unlock();
}
if (returncode && !returntext[0]) {
char *ldap_result_message = NULL;
slapi_pblock_get(pb, SLAPI_PB_RESULT_TEXT, &ldap_result_message);
if (ldap_result_message && ldap_result_message[0]) {
PL_strncpyz(returntext, ldap_result_message, sizeof(returntext));
}
}
/*
* Check if we are deleting a plugin
*/
if (returncode == LDAP_SUCCESS) {
if (dse_delete_plugin(ec, returntext)) {
rc = LDAP_UNWILLING_TO_PERFORM;
}
}
/* coverity false positive:
* var_deref_model: Passing null pointer "orig_entry" to "slapi_pblock_set", which dereferences it.
* but orig_entry is not dereferenced in SLAPI_DELETE_BEPOSTOP_ENTRY case so lets ignore this one.
*/
/* coverity[var_deref_model] */
slapi_pblock_set(pb, SLAPI_DELETE_BEPOSTOP_ENTRY, orig_entry);
slapi_send_ldap_result(pb, returncode, NULL, returntext, 0, NULL);
return dse_delete_return(returncode, ec);
}
struct dse_callback *
dse_register_callback(struct dse *pdse,
int operation,
int flags,
const Slapi_DN *base,
int scope,
const char *filter,
dseCallbackFn fn,
void *fn_arg,
struct slapdplugin *plugin)
{
struct dse_callback *callback = dse_callback_new(operation, flags, base, scope, filter, fn, fn_arg, plugin);
dse_callback_addtolist(&pdse->dse_callback, callback);
return callback;
}
void
dse_remove_callback(struct dse *pdse, int operation, int flags, const Slapi_DN *base, int scope, const char *filter, dseCallbackFn fn)
{
dse_callback_removefromlist(&pdse->dse_callback, operation, flags, base, scope, filter, fn);
}
/*
* Return values:
* SLAPI_DSE_CALLBACK_ERROR -- Callback failed.
* SLAPI_DSE_CALLBACK_OK -- OK, do it.
* SLAPI_DSE_CALLBACK_DO_NOT_APPLY -- No error, but don't apply changes.
*/
static int
dse_call_callback(struct dse *pdse, Slapi_PBlock *pb, int operation, int flags, Slapi_Entry *entryBefore, Slapi_Entry *entryAfter, int *returncode, char *returntext)
{
/* ONREPL callbacks can potentially modify pblock parameters like backend
* which would cause problems during request processing. We need to save
* "important" fields before calls and restoring them afterwards */
int rc = SLAPI_DSE_CALLBACK_OK;
if (pdse->dse_callback != NULL) {
struct dse_callback *p = pdse->dse_callback;
struct dse_callback *next = NULL;
int result = SLAPI_DSE_CALLBACK_OK;
while (p != NULL) {
next = p->next;
if ((p->operation & operation) && (p->flags & flags)) {
if (slapi_sdn_scope_test(slapi_entry_get_sdn_const(entryBefore), p->base, p->scope)) {
if (NULL == p->slapifilter || slapi_vattr_filter_test(pb, entryBefore, p->slapifilter, 0) == 0) {
struct slapdplugin *plugin = p->plugin;
int plugin_started = 1;
if (plugin) {
/* this is a plugin callback, update the operation counter */
slapi_plugin_op_started(plugin);
if (!plugin->plg_started) {
/* must be a task function being called */
result = SLAPI_DSE_CALLBACK_ERROR;
PR_snprintf(returntext, SLAPI_DSE_RETURNTEXT_SIZE,
"Task entry (%s) could not added because the (%s) plugin is disabled.",
slapi_entry_get_dn(entryBefore), p->plugin->plg_dn);
plugin_started = 0;
}
}
if (plugin_started) {
result = (*p->fn)(pb, entryBefore, entryAfter, returncode, returntext, p->fn_arg);
}
if (result < rc) {
rc = result;
}
slapi_plugin_op_finished(plugin);
}
}
}
p = next;
}
}
return rc;
}
int
slapi_config_register_callback(int operation,
int flags,
const char *base,
int scope,
const char *filter,
dseCallbackFn fn,
void *fn_arg)
{
return slapi_config_register_callback_plugin(operation, flags, base, scope, filter, fn, fn_arg, NULL);
}
/*
* We pass in the pblock so we can update the operation counter for "dynamic plugins".
*/
int
slapi_config_register_callback_plugin(int operation,
int flags,
const char *base,
int scope,
const char *filter,
dseCallbackFn fn,
void *fn_arg,
Slapi_PBlock *pb)
{
int rc = 0;
Slapi_Backend *be = slapi_be_select_by_instance_name(DSE_BACKEND);
if (be != NULL) {
struct dse *pdse = (struct dse *)be->be_database->plg_private;
if (pdse != NULL) {
Slapi_DN dn;
slapi_sdn_init_dn_byref(&dn, base);
if (pb != NULL) {
/* if a pblock was passed, this is a plugin, so set the f_arg as the plugin */
struct slapdplugin *pb_plugin = NULL;
slapi_pblock_get(pb, SLAPI_PLUGIN, &pb_plugin);
rc = (NULL != dse_register_callback(pdse, operation, flags, &dn, scope, filter, fn,
(void *)pb_plugin, pb_plugin));
} else {
rc = (NULL != dse_register_callback(pdse, operation, flags, &dn, scope, filter, fn,
fn_arg, NULL));
}
slapi_sdn_done(&dn);
}
}
return rc;
}
int
slapi_config_remove_callback(int operation, int flags, const char *base, int scope, const char *filter, dseCallbackFn fn)
{
int rc = 0;
Slapi_Backend *be = slapi_be_select_by_instance_name(DSE_BACKEND);
if (be != NULL) {
struct dse *pdse = (struct dse *)be->be_database->plg_private;
if (pdse != NULL) {
Slapi_DN dn;
slapi_sdn_init_dn_byref(&dn, base);
dse_remove_callback(pdse, operation, flags, &dn, scope, filter, fn);
slapi_sdn_done(&dn);
rc = 1;
}
}
return rc;
}
void
dse_set_dont_ever_write_dse_files()
{
dont_ever_write_dse_files = 1;
}
void
dse_unset_dont_ever_write_dse_files()
{
dont_ever_write_dse_files = 0;
}
static dse_search_set *
dse_search_set_new(void)
{
dse_search_set *ss;
ss = (dse_search_set *)slapi_ch_malloc(sizeof(*ss));
if (ss) {
dl_init(&ss->dl, 0);
ss->current_entry = -1;
}
return ss;
}
/* This is similar to delete, but it does not free the entries contained in the
search set. This is useful in do_dse_search when we copy the entries from
1 search set to the other. */
static void
dse_search_set_clean(dse_search_set *ss)
{
if (ss) {
dl_cleanup(&ss->dl, NULL);
slapi_ch_free((void **)&ss);
}
}
void
dse_search_set_release(void **ss)
{
dse_search_set_delete(*(dse_search_set **)ss);
}
void
dse_prev_search_results(void *vp)
{
Slapi_PBlock *pb = (Slapi_PBlock *)vp;
dse_search_set *ss;
slapi_pblock_get(pb, SLAPI_SEARCH_RESULT_SET, &ss);
if (ss) {
dl_get_prev(&ss->dl, &ss->current_entry);
}
}
static void
dse_search_set_delete(dse_search_set *ss)
{
if (ss) {
dl_cleanup(&ss->dl, dse_free_entry);
slapi_ch_free((void **)&ss);
}
}
static void
dse_free_entry(void **data)
{
Slapi_Entry **e;
if (data) {
e = (Slapi_Entry **)data;
if (*e)
slapi_entry_free(*e);
}
}
static void
dse_search_set_add_entry(dse_search_set *ss, Slapi_Entry *e)
{
PR_ASSERT(ss && e);
dl_add(&ss->dl, e);
}
static Slapi_Entry *
dse_search_set_get_next_entry(dse_search_set *ss)
{
PR_ASSERT(ss);
if (ss->current_entry == -1)
return (dl_get_first(&ss->dl, &ss->current_entry));
else
return (dl_get_next(&ss->dl, &ss->current_entry));
}
int
dse_next_search_entry(Slapi_PBlock *pb)
{
dse_search_set *ss;
Slapi_Entry *e;
slapi_pblock_get(pb, SLAPI_SEARCH_RESULT_SET, &ss);
/* no entries to return */
if (ss == NULL) {
slapi_pblock_set(pb, SLAPI_SEARCH_RESULT_ENTRY, NULL);
return 0;
}
e = dse_search_set_get_next_entry(ss);
slapi_pblock_set(pb, SLAPI_SEARCH_RESULT_ENTRY, e);
/* we reached the end of the list */
if (e == NULL) {
pagedresults_set_search_result_pb(pb, NULL, 0);
dse_search_set_delete(ss);
slapi_pblock_set(pb, SLAPI_SEARCH_RESULT_SET, NULL);
}
return 0;
}
|