summaryrefslogtreecommitdiffstats
path: root/runtime
diff options
context:
space:
mode:
authorRainer Gerhards <rgerhards@adiscon.com>2010-10-22 14:16:38 +0200
committerRainer Gerhards <rgerhards@adiscon.com>2010-10-22 14:16:38 +0200
commitcd5190ad73fa8f69da8824c80337a9af2b51c94c (patch)
tree3ebe0b0b6f9fc28c9b79223668d4b7a8225318c2 /runtime
parentf76b826989cea5fdfbb7b313557775cf082a398e (diff)
preparing for 5.6.0
Diffstat (limited to 'runtime')
0 files changed, 0 insertions, 0 deletions
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759
/** BEGIN COPYRIGHT BLOCK
 * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
 * Copyright (C) 2005 Red Hat, Inc.
 * All rights reserved.
 * END COPYRIGHT BLOCK **/
/*
 * create_instance.c: Routines for creating an instance of a Directory Server
 *
 * These routines are not thread safe.
 * 
 * Rob McCool
 */

#define GW_CONF 1
#define PB_CONF 2

#include "create_instance.h"
#include "cfg_sspt.h"

#include <stdio.h>
#include <stdarg.h>
#include <string.h>

#include <sys/stat.h>
#include <ctype.h>

#define PATH_SIZE 1024
#define ERR_SIZE 8192

/* delay time in seconds between referential integrity updates
    0 means continues */
#define REFERINT_DELAY 0

/* 1=log changes for replaction, 0=don't replicate changes */
#define REFERINT_LOG_CHANGES 0

#include "dsalib.h"
#include "dirver.h"

#include "nspr.h"
#include "plstr.h"

#ifdef XP_WIN32
#define NOT_ABSOLUTE_PATH(str) \
  ((str[0] != '/') && (str[0] != '\\') && (str[2] != '/') && (str[2] != '\\'))
#define EADDRINUSE WSAEADDRINUSE
#define EACCES WSAEACCES
#include <winsock.h>
#include <io.h>
#include <regparms.h>
#include <nt/ntos.h>
#define SHLIB_EXT "dll"
#else
#define NOT_ABSOLUTE_PATH(str) (str[0] != '/')
#include <errno.h>
#include <sys/types.h>

#if !defined(HPUX) && !defined(LINUX2_0)
#include <sys/select.h>  /* FD_SETSIZE */
#else
#include <sys/types.h>   /* FD_SETSIZE is in types.h on HPUX */
#endif

#if !defined(_WIN32) && !defined(AIX)
#include <sys/resource.h> /* get/setrlimit stuff */
#endif

#include <sys/socket.h>  /* socket flags */
#include <netinet/in.h>  /* sockaddr_in */
#include <arpa/inet.h>   /* inet_addr */
#ifdef HPUX
#define SHLIB_EXT "sl"
#else
#define SHLIB_EXT "so"
#endif

#endif

/* 
   NT doesn't strictly need these, but the libadmin API which is emulated
   below uses them.
 */
#define NEWSCRIPT_MODE 0755
#define NEWFILE_MODE 0644
#define NEWDIR_MODE 0755
#define NEWSECDIR_MODE 0700

#include <stdarg.h>

#ifdef XP_WIN32

OS_TYPE NS_WINAPI INFO_GetOperatingSystem ();
DWORD NS_WINAPI SERVICE_ReinstallNTService( LPCTSTR szServiceName, 
                                            LPCTSTR szServiceDisplayName,
                                            LPCTSTR szServiceExe );


#endif
static void ds_gen_index(FILE* f, char* belowdn);
static char *ds_gen_orgchart_conf(char *sroot, char *cs_path, server_config_s *cf);
static char *ds_gen_gw_conf(char *sroot, char *cs_path, server_config_s *cf, int conf_type);
static char *install_ds(char *sroot, server_config_s *cf, char *param_name);

static int write_ldap_info(char *slapd_server_root, server_config_s *cf);
#if defined (BUILD_PRESENCE)
static char *gen_presence_init_script(char *sroot, server_config_s *cf, 
                                         char *cs_path);
static int init_presence(char *sroot, server_config_s *cf, char *cs_path);
#endif

static char *make_error(char *fmt, ...)
{
    static char errbuf[ERR_SIZE];
    va_list args;

    va_start(args, fmt);
    PR_vsnprintf(errbuf, sizeof(errbuf), fmt, args);
    va_end(args);
    return errbuf;
}


/* This is to determine if we can skip the port number checks.  During
migration or server cloning, we may want to copy over an old configuration,
including the old port number, which may not currently have permission to
use; if we don't need to start the server right away, we can skip
certain checks
*/
static int needToStartServer(server_config_s *cf)
{
    if (cf && (
       (cf->cfg_sspt && !strcmp(cf->cfg_sspt, "1")) ||
       (cf->start_server && !strcmp(cf->start_server, "1"))
    ))
    {
        return 1;
    }

    return 0;
}

static char *
myStrdup(const char *s)
{
    if (s == NULL)
    return (char *)s;

    return strdup(s);
}

static int getSuiteSpotUserGroup(server_config_s* cf)
{
#ifdef XP_UNIX
    static const char *ssUsersFile = "shared/config/ssusers.conf";
    char realFile[PATH_SIZE];
    char buf[1024];
    FILE *fp = NULL;
    int status = 1;

    if (cf->servuser)
        return 0;

    PR_snprintf(realFile, sizeof(realFile), "%s/%s", cf->sroot, ssUsersFile);
    if (!(fp = fopen(realFile, "r")))
        return 1;

    while (fgets(buf, sizeof(buf), fp))
    {
        char *p = NULL;

        if (buf[0] == '#' || buf[0] == '\n')
            continue;

        buf[strlen(buf) - 1] = 0;
        if (NULL != (p = strstr(buf, "SuiteSpotUser")))
        {
            p += strlen("SuiteSpotUser");
            while (ldap_utf8isspace(p))
            LDAP_UTF8INC(p);
            cf->servuser = strdup(p);
            status = 0;
            break;
        }
    }

    if (fp)
        fclose(fp);

    return status;
#else
    return 0;
#endif
}

/* ----------------------- Create default settings ------------------------ */


void set_defaults(char *sroot, char *hn, server_config_s *conf)
{
    char *id = 0, *t = 0;

    conf->sroot = sroot;

    if (hn)
    {
        if( (t = strchr(hn, '.')) )
            *t = '\0';
		id = PR_smprintf("%s", hn);
        if(t)
            *t = '.';
    }

    conf->servname = hn;
    conf->bindaddr = "";
    conf->servport = "80";
    conf->cfg_sspt = NULL;
    conf->suitespot3x_uid = NULL;
    conf->cfg_sspt_uid = NULL;
    conf->cfg_sspt_uidpw = NULL;
    conf->servport = "389";
    conf->secserv = "off";
    conf->secservport = "636";
    conf->ntsynch = "off";
    conf->ntsynchssl = "on";
    conf->ntsynchport = "5009";
    conf->rootpw = "";
    conf->roothashedpw = "";
    conf->loglevel = NULL;
    if (getenv("DEBUG_DS_LOG_LEVEL"))
        conf->loglevel = getenv("DEBUG_DS_LOG_LEVEL");
    conf->suffix = "dc=example, dc=com";
#ifndef DONT_ALWAYS_CREATE_NETSCAPEROOT
    conf->netscaperoot = name_netscaperootDN;
#endif /* DONT_ALWAYS_CREATE_NETSCAPEROOT */
#define CREATE_SAMPLE_SUFFIX
#ifdef CREATE_SAMPLE_SUFFIX
    conf->samplesuffix = "dc=example, dc=com";
#endif /* CREATE_SAMPLE_SUFFIX */
#ifdef TEST_CONFIG
    conf->netscaperoot = "cn=config40";
#endif /* TEST_CONFIG */

#define ROOT_RDN    "cn=Directory Manager"
    conf->rootdn = ROOT_RDN;
/*    conf->rootdn = malloc(strlen(ROOT_RDN) + 2 + strlen(conf->suffix) + 1);
    sprintf(conf->rootdn, "%s, %s", ROOT_RDN, conf->suffix);*/
    conf->servid = id;

#ifdef XP_UNIX
    conf->servuser = NULL;
#ifdef THREAD_NSPR_KERNEL
    conf->numprocs = "1";
    conf->maxthreads = "128";
#else
    conf->numprocs = "4";
    conf->maxthreads = "32";
#endif
#else /* XP_WIN32 */
    conf->maxthreads = "32";
#endif
    conf->minthreads = "4";

    conf->upgradingServer = 0;
    
    conf->start_server = "1";
    conf->admin_domain = NULL;
    conf->config_ldap_url = NULL;
    conf->user_ldap_url = NULL;
    conf->use_existing_config_ds = 0;
    conf->use_existing_user_ds = 0;
    conf->consumerdn = NULL;
    conf->disable_schema_checking = NULL;
    conf->install_ldif_file = NULL;
}


/* ----------------- Sanity check a server configuration ------------------ */


char *create_instance_checkport(char *, char *);
char *create_instance_checkuser(char *);
int create_instance_numbers(char *);
int create_instance_exists(char *fn);
char *create_instance_copy(char *, char *, int);
char *create_instance_concatenate(char *, char *, int);
int create_instance_mkdir(char *, int);
char *create_instance_mkdir_p(char *, int);

#if defined( SOLARIS )
/*
 * Solaris 9+ specific installation
 */
int create_instance_symlink(char *, char *);
#endif /* SOLARIS */


/*
  returns NULL if the given dn is a valid dn, or an error string
*/
static char *
isAValidDN(const char *dn_to_test)
{
    char *t = 0;

    if (!dn_to_test || !*dn_to_test)
    {
        t = "No value specified for the parameter.";
    }
    else
    {
        char **rdnList = ldap_explode_dn(dn_to_test, 0);
        char **rdnNoTypes = ldap_explode_dn(dn_to_test, 1);
        if (!rdnList || !rdnList[0] || !rdnNoTypes || !rdnNoTypes[0] ||
            !*rdnNoTypes[0] || !PL_strcasecmp(rdnList[0], rdnNoTypes[0]))
        {
            t = make_error("The given value [%s] is not a valid DN.",
                   dn_to_test);
        }
        if (rdnList)
            ldap_value_free(rdnList);
        if (rdnNoTypes)
            ldap_value_free(rdnNoTypes);
    }

    if (t)
        return t;

    return NULL;
}

/*
  prints a message if the given dn uses LDAPv2 style quoting
*/
void
checkForLDAPv2Quoting(const char *dn_to_test)
{
    if (ds_dn_uses_LDAPv2_quoting(dn_to_test))
    {
        char *newdn = strdup(dn_to_test);
        char *t;
        dn_normalize_convert(newdn);
        t = make_error(
            "The given value [%s] is quoted in the deprecated LDAPv2 style\n"
            "quoting format.  It will be automatically converted to use the\n"
            "LDAPv3 style escaped format [%s].", dn_to_test, newdn);
        free(newdn);
        ds_show_message(t);
    }

    return;
}

/*
  returns NULL if the given string contains no 8 bit chars, otherwise an
  error message
*/
static char *
contains8BitChars(const char *s)
{
    char *t = 0;

    if (s && *s)
    {
        for (; !t && *s; ++s)
        {
            if (*s & 0x80)
            {
                t = make_error("The given value [%s] contains invalid 8 bit characters.",
                   s);
            }
        }
    }

    return t;
}

static char *sanity_check(server_config_s *cf, char *param_name)
{
    char *t, fn[PATH_SIZE];
    register int x;

    if (!param_name)
        return "Parameter param_name is null";

    /* if we don't need to start the server right away, we can skip the
    port number checks
    */
    if (!needToStartServer(cf))
    {
        if( (t = create_instance_checkport(cf->bindaddr, cf->servport)) )
        {
            PL_strncpyz(param_name, "servport", BIG_LINE);
            return t;
        }

        if ( cf->secserv && (strcmp(cf->secserv, "on") == 0) && (cf->secservport != NULL) &&
         (*(cf->secservport) != '\0') ) {
            if ( (t = create_instance_checkport(cf->bindaddr, cf->secservport)) ) {
                PL_strncpyz(param_name, "secservport", BIG_LINE);
                return t;
            }
        }
        if ( cf->ntsynch && (strcmp(cf->ntsynch, "on") == 0) && (cf->ntsynchport != NULL) &&
         (*(cf->ntsynchport) != '\0') ) {
            if ( (t = create_instance_checkport(cf->bindaddr, cf->ntsynchport)) ) {
                PL_strncpyz(param_name, "ntsynchport", BIG_LINE);
                return t;
            }
        }
    }

    /* is the server identifier good? */
    for(x=0; cf->servid[x]; x++)  {
        if(strchr("/ &;`'\"|*!?~<>^()[]{}$\\", cf->servid[x]))  {
        PL_strncpyz(param_name, "servid", BIG_LINE);
            return make_error("You used a shell-specific character in "
                              "your server id (the character was %c).", 
                              cf->servid[x]);
        }
    }
    /* has that identifier already been used? */
    PR_snprintf(fn, sizeof(fn), "%s%c%s-%s", cf->sroot, FILE_PATHSEP, 
            PRODUCT_NAME, cf->servid);

/* Not an error to upgrade! ???
   if ( !cf->upgradingServer ) {
       if(create_instance_exists(fn)) {
           PL_strncpyz(param_name, "servid", BIG_LINE);
           return make_error ("A server named '%s' already exists."
           "\nPlease choose another server identifier.",
           cf->servid);
       }
   }
   */

#ifdef XP_UNIX
    if( (t = create_instance_checkuser(cf->servuser)) )
    {
        PL_strncpyz(param_name, "servuser", BIG_LINE);
        return t;
    }
#endif

    /* make sure some drooling imbecile doesn't put in bogus numbers */
#ifdef XP_UNIX
    if((!create_instance_numbers(cf->numprocs)) || (atoi(cf->numprocs) <= 0))
    {
        PL_strncpyz(param_name, "numprocs", BIG_LINE);
        return ("The number of processes must be not be zero or "
                "negative.");
    }
#endif
    if((!create_instance_numbers(cf->maxthreads)) || (atoi(cf->maxthreads) <= 0))
    {
        PL_strncpyz(param_name, "maxthreads", BIG_LINE);
        return ("The maximum threads must be not be zero or negative.");
    }
    if((!create_instance_numbers(cf->minthreads)) || (atoi(cf->minthreads) <= 0))
    {
        PL_strncpyz(param_name, "minthreads", BIG_LINE);
        return ("The minumum threads must be not be zero or negative.");
    }

    if((atoi(cf->minthreads)) > (atoi(cf->maxthreads)))
    {
        PL_strncpyz(param_name, "minthreads", BIG_LINE);
        return ("Minimum threads must be less than maximum threads.");
    }

    /* see if the DN parameters are valid DNs */
    if (!cf->use_existing_user_ds && (t = isAValidDN(cf->suffix)))
    {
        PL_strncpyz(param_name, "suffix", BIG_LINE);
        return t;
    }
    checkForLDAPv2Quoting(cf->suffix);

    if (NULL != (t = isAValidDN(cf->rootdn)))
    {
        PL_strncpyz(param_name, "rootdn", BIG_LINE);
        return t;
    }
    checkForLDAPv2Quoting(cf->rootdn);

    if (cf->replicationdn && *cf->replicationdn && (t = isAValidDN(cf->replicationdn)))
    {
        PL_strncpyz(param_name, "replicationdn", BIG_LINE);
        return t;
    }
    checkForLDAPv2Quoting(cf->replicationdn);

    if (cf->consumerdn && *cf->consumerdn && (t = isAValidDN(cf->consumerdn)))
    {
        PL_strncpyz(param_name, "consumerdn", BIG_LINE);
        return t;
    }
    checkForLDAPv2Quoting(cf->consumerdn);

    if (cf->changelogsuffix && *cf->changelogsuffix &&
        (t = isAValidDN(cf->changelogsuffix)))
    {
        PL_strncpyz(param_name, "changelogsuffix", BIG_LINE);
        return t;
    }
    checkForLDAPv2Quoting(cf->changelogsuffix);

    if (cf->netscaperoot && *cf->netscaperoot &&
        (t = isAValidDN(cf->netscaperoot)))
    {
        PL_strncpyz(param_name, "netscaperoot", BIG_LINE);
        return t;
    }
    checkForLDAPv2Quoting(cf->netscaperoot);

    if (cf->samplesuffix && *cf->samplesuffix &&
        (t = isAValidDN(cf->samplesuffix)))
    {
        PL_strncpyz(param_name, "samplesuffix", BIG_LINE);
        return t;
    }
    checkForLDAPv2Quoting(cf->samplesuffix);

    if (NULL != (t = contains8BitChars(cf->rootpw)))
    {
        PL_strncpyz(param_name, "rootpw", BIG_LINE);
        return t;
    }

    if (NULL != (t = contains8BitChars(cf->cfg_sspt_uidpw)))
    {
        PL_strncpyz(param_name, "cfg_sspt_uidpw", BIG_LINE);
        return t;
    }

    if (NULL != (t = contains8BitChars(cf->replicationpw)))
    {
        PL_strncpyz(param_name, "replicationpw", BIG_LINE);
        return t;
    }

    if (NULL != (t = contains8BitChars(cf->consumerpw)))
    {
        PL_strncpyz(param_name, "consumerpw", BIG_LINE);
        return t;
    }

    if (cf->cfg_sspt_uid && *cf->cfg_sspt_uid)
    {
    /*
      If it is a valid DN, ok.  Otherwise, it should be a uid, and should
      be checked for 8 bit chars
    */
        if (NULL != (t = isAValidDN(cf->cfg_sspt_uid)))
        {
            if (NULL != (t = contains8BitChars(cf->cfg_sspt_uid)))
            {
                PL_strncpyz(param_name, "cfg_sspt_uid", BIG_LINE);
                return t;
            }
        }
        else
            checkForLDAPv2Quoting(cf->cfg_sspt_uid);
    }

    return NULL;
}

/* ----- From a configuration, set up a new server in the server root ----- */

/* ------------------ UNIX utilities for server creation ------------------ */

#ifdef XP_UNIX

#include <unistd.h>
#include <pwd.h>

char*
chownfile (struct passwd* pw, char* fn)
{
    if (pw != NULL && chown (fn, pw->pw_uid, pw->pw_gid) == -1) {
        if (pw->pw_name != NULL) {
            return make_error ("Could not change owner of %s to %s.",
                   fn, pw->pw_name);
        } else {
            return make_error ("Could not change owner of %s to (UID %li, GID %li).",
                   fn, (long)(pw->pw_uid), (long)(pw->pw_gid));
        }
    }
    return NULL;
}

char *chownlogs(char *sroot, char *user)
{
    struct passwd *pw;
    char fn[PATH_SIZE];
    if(user && *user && !geteuid())  {
        if(!(pw = getpwnam(user)))
            return make_error("Could not find UID and GID of user '%s'.", 
                              user);
        PR_snprintf(fn, sizeof(fn), "%s%clogs", sroot, FILE_PATHSEP);
        return chownfile (pw, fn);
    }
    return NULL;
}

char *chownconfig(char *sroot, char *user)
{
    struct passwd *pw;
    char fn[PATH_SIZE];
    if(user && *user && !geteuid())  {
        if(!(pw = getpwnam(user)))
            return make_error("Could not find UID and GID of user '%s'.", 
                              user);
        PR_snprintf(fn, sizeof(fn), "%s%cconfig", sroot, FILE_PATHSEP);
        return chownfile (pw, fn);
    }
    return NULL;
}

#else

#define chownfile(a, b) 
#define chownlogs(a, b) 
#define chownconfig(a, b) 
#define chownsearch(a, b) 

#endif

char *gen_script(char *s_root, char *name, char *fmt, ...)
{
    char fn[PATH_SIZE];
    FILE *f;
    char *shell = "/bin/sh";
    va_list args;

    PR_snprintf(fn, sizeof(fn), "%s%c%s", s_root, FILE_PATHSEP, name);
    if(!(f = fopen(fn, "w")))  
        return make_error("Could not write to %s (%s).", fn, ds_system_errmsg());
    va_start(args, fmt);
#if !defined( XP_WIN32 )
#if defined( OSF1 )
    /*
    The standard /bin/sh has some rather strange behavior with "$@",
    so use the posix version wherever possible.  OSF1 4.0D should
    always have this one available.
    */
    if (!access("/usr/bin/posix/sh", 0))
        shell = "/usr/bin/posix/sh";
#endif /* OSF1 */
    fprintf(f, "#!%s\n\n", shell);
    /*
    Neutralize shared library access.
    
    On HP-UX, SHLIB_PATH is the historical variable.
    However on HP-UX 64 bit, LD_LIBRARY_PATH is also used.
    We unset both too.
    */
#if defined( SOLARIS ) || defined( OSF1 ) || defined( LINUX2_0 )
    fprintf(f, "unset LD_LIBRARY_PATH\n");
#endif
#if defined( HPUX )
    fprintf(f, "unset SHLIB_PATH\n");
    fprintf(f, "unset LD_LIBRARY_PATH\n");
#endif
#if defined( AIX )
    fprintf(f, "unset LIBPATH\n");
#endif
#endif
    vfprintf(f, fmt, args);

#if defined( XP_UNIX )
    fchmod(fileno(f), NEWSCRIPT_MODE);
#endif
    fclose(f);
#if defined( XP_WIN32 )
    chmod( fn, NEWSCRIPT_MODE);
#endif
    return NULL;
}

char *gen_perl_script(char *s_root, char *cs_path, char *name, char *fmt, ...)
{
    char myperl[PATH_SIZE];
    char fn[PATH_SIZE];
    FILE *f;
    va_list args;

	if (PR_FAILURE == PR_Access(cs_path, PR_ACCESS_EXISTS)) {
		printf("Notice: %s does not exist, skipping %s . . .\n", cs_path, name);
		return NULL;
	}

    PR_snprintf(fn, sizeof(fn), "%s%c%s", cs_path, FILE_PATHSEP, name);
    PR_snprintf(myperl, sizeof(myperl), "%s%cbin%cslapd%cadmin%cbin%cperl",
                    s_root, FILE_PATHSEP, FILE_PATHSEP,
                    FILE_PATHSEP, FILE_PATHSEP, FILE_PATHSEP);
    if(!(f = fopen(fn, "w")))  
        return make_error("Could not write to %s (%s).", fn, ds_system_errmsg());
    va_start(args, fmt);
#if !defined( XP_WIN32 )
    fprintf(f, "#!%s\n\n", myperl);
#endif
    vfprintf(f, fmt, args);

#if defined( XP_UNIX )
    fchmod(fileno(f), NEWSCRIPT_MODE);
#endif
    fclose(f);
#if defined( XP_WIN32 )
    chmod( fn, NEWSCRIPT_MODE);
#endif

    return NULL;
}

char *gen_perl_script_auto(char *s_root, char *cs_path, char *name,
                           server_config_s *cf)
{
    char myperl[PATH_SIZE];
    char fn[PATH_SIZE], ofn[PATH_SIZE];
    const char *table[10][2];

	if (PR_FAILURE == PR_Access(cs_path, PR_ACCESS_EXISTS)) {
		printf("Notice: %s does not exist, skipping %s . . .\n", cs_path, name);
		return NULL;
	}

    PR_snprintf(ofn, sizeof(ofn), "%s%cbin%cslapd%cadmin%cscripts%ctemplate-%s", s_root,
            FILE_PATHSEP, FILE_PATHSEP, FILE_PATHSEP, FILE_PATHSEP,
            FILE_PATHSEP, name);
    PR_snprintf(fn, sizeof(fn), "%s%c%s", cs_path, FILE_PATHSEP, name);
    PR_snprintf(myperl, sizeof(myperl), "!%s%cbin%cslapd%cadmin%cbin%cperl",
            s_root, FILE_PATHSEP, FILE_PATHSEP,
            FILE_PATHSEP, FILE_PATHSEP, FILE_PATHSEP);

    table[0][0] = "DS-ROOT";
    table[0][1] = s_root;
    table[1][0] = "MY-DS-ROOT";
    table[1][1] = cs_path;
    table[2][0] = "SEP";
    table[2][1] = FILE_PATHSEPP;
    table[3][0] = "SERVER-NAME";
    table[3][1] = cf->servname;
    table[4][0] = "SERVER-PORT";
    table[4][1] = cf->servport;
    table[5][0] = "PERL-EXEC";
    table[6][0] = "DEV-NULL";
#if !defined( XP_WIN32 )
    table[5][1] = myperl;
    table[6][1] = " /dev/null ";
#else
    table[5][1] = " perl script";
    table[6][1] = " NUL ";
#endif
    table[7][0] = "ROOT-DN";
    table[7][1] = cf->rootdn;
    table[8][0] = table[8][1] = NULL;

    if (generate_script(ofn, fn, NEWSCRIPT_MODE, table) != 0) {
        return make_error("Could not write %s to %s (%s).", ofn, fn,
                          ds_system_errmsg());
    }

    return NULL;
}

char *gen_perl_script_auto_for_migration(char *s_root, char *cs_path, char *name,
                           server_config_s *cf)
{
    char myperl[PATH_SIZE];
    char fn[PATH_SIZE], ofn[PATH_SIZE];
    const char *table[10][2];

    PR_snprintf(ofn, sizeof(ofn), "%s%cbin%cslapd%cadmin%cscripts%ctemplate-%s", s_root,
            FILE_PATHSEP, FILE_PATHSEP, FILE_PATHSEP, FILE_PATHSEP,
            FILE_PATHSEP, name);
    PR_snprintf(fn, sizeof(fn), "%s%cbin%cslapd%cadmin%cbin%c%s", s_root, FILE_PATHSEP, 
            FILE_PATHSEP, FILE_PATHSEP, FILE_PATHSEP, FILE_PATHSEP, name);
    PR_snprintf(myperl, sizeof(myperl), "!%s%cbin%cslapd%cadmin%cbin%cperl",
            s_root, FILE_PATHSEP, FILE_PATHSEP,
            FILE_PATHSEP, FILE_PATHSEP, FILE_PATHSEP);

    table[0][0] = "DS-ROOT";
    table[0][1] = s_root;
    table[1][0] = "MY-DS-ROOT";
    table[1][1] = cs_path;
    table[2][0] = "SEP";
    table[2][1] = FILE_PATHSEPP;
    table[3][0] = "SERVER-NAME";
    table[3][1] = cf->servname;
    table[4][0] = "SERVER-PORT";
    table[4][1] = cf->servport;
    table[5][0] = "PERL-EXEC";
    table[6][0] = "DEV-NULL";
#if !defined( XP_WIN32 )
    table[5][1] = myperl;
    table[6][1] = " /dev/null ";
#else
    table[5][1] = " perl script";
    table[6][1] = " NUL ";
#endif
    table[7][0] = "ROOT-DN";
    table[7][1] = cf->rootdn;
    table[8][0] = table[8][1] = NULL;

    if (generate_script(ofn, fn, NEWSCRIPT_MODE, table) != 0) {
        return make_error("Could not write %s to %s (%s).", ofn, fn,
                          ds_system_errmsg());
    }

    return NULL;
}

/* ------------------ NT utilities for server creation ------------------ */

#ifdef XP_WIN32

char *
service_exists(char *servid)
{
    DWORD status, lasterror = 0;
    char szServiceName[MAX_PATH] = {0};
    PR_snprintf(szServiceName, sizeof(szServiceName),"%s-%s", SVR_ID_SERVICE, servid);
    /* if the service already exists, error */
    status = SERVICE_GetNTServiceStatus(szServiceName, &lasterror );
    if ( (lasterror == ERROR_SERVICE_DOES_NOT_EXIST) || 
        (status == SERVRET_ERROR) || (status == SERVRET_REMOVED) ) {
         return 0;
    } else {    return
        make_error("Server %s already exists: cannot create another.  "
                   "Please choose a different name or delete the "
                   "existing server.",
                   szServiceName);
    }

    return 0;
}

void setup_nteventlogging(char *szServiceId, char *szMessageFile)
{
    HKEY hKey;
    char szKey[MAX_PATH];
    DWORD dwData;

    PR_snprintf(szKey, sizeof(szKey), "SYSTEM\\CurrentControlSet\\Services\\EventLog\\Application\\%s", szServiceId);

    if(RegCreateKey(HKEY_LOCAL_MACHINE, szKey, &hKey) == ERROR_SUCCESS)
    {
        if(RegSetValueEx(hKey, "EventMessageFile", 0, REG_SZ, (LPBYTE)szMessageFile, strlen(szMessageFile) + 1) == ERROR_SUCCESS)
        {
            dwData = EVENTLOG_ERROR_TYPE | EVENTLOG_WARNING_TYPE | EVENTLOG_INFORMATION_TYPE;
            RegSetValueEx(hKey, "TypesSupported", 0, REG_DWORD, (LPBYTE) &dwData, sizeof(DWORD));
        }
        RegCloseKey(hKey);
    }
}

    
char *add_ntservice(server_config_s *cf)
{
    char szMessageFile[MAX_PATH];
    char szServiceExe[MAX_PATH], szServiceDisplayName[MAX_PATH], szServiceName[MAX_PATH];
    DWORD dwLastError;
    
    PR_snprintf(szServiceExe, sizeof(szServiceExe), "%s/bin/%s/server/%s", cf->sroot, 
              SVR_DIR_ROOT, SVR_EXE);
    PR_snprintf(szServiceName, sizeof(szServiceName),"%s-%s", SVR_ID_SERVICE, cf->servid);
    PR_snprintf(szServiceDisplayName, sizeof(szServiceDisplayName), "%s (%s)", SVR_NAME_FULL_VERSION, 
              cf->servid);

    /* install new service - if already installed, try and remove and 
        then reinstall */
    dwLastError = SERVICE_ReinstallNTService( szServiceName, 
        szServiceDisplayName, szServiceExe );
    if ( dwLastError != NO_ERROR ) {
         return make_error ( "While installing %s Service, the "
                "NT Service Manager reported error %d (%s)", 
                szServiceDisplayName, dwLastError, ds_system_errmsg() );
    }

    // setup event logging registry keys, do this after service creation
    PR_snprintf(szMessageFile, sizeof(szMessageFile), "%s\\bin\\%s\\server\\%s", cf->sroot, 
        SVR_DIR_ROOT, "slapdmessages30.dll");
    setup_nteventlogging(szServiceName, szMessageFile);

    // TODO: add perfmon setup code -ahakim 11/22/96
    return NULL;
}

char *setup_ntserver(server_config_s *cf)
{
    char line[MAX_PATH], *sroot = cf->sroot;
    char subdir[MAX_PATH];
    char NumValuesBuf[3];
    DWORD Result;
    HKEY hServerKey;
    DWORD NumValues;
    DWORD iterator;
    int value_already_exists = 0;
    DWORD type_buffer;
    char  value_data_buffer[MAX_PATH];
    DWORD sizeof_value_data_buffer;

    /* MLM - Adding ACL directories authdb and authdb/default */
    PR_snprintf(subdir, sizeof(subdir), "%s%cauthdb", sroot, FILE_PATHSEP); 
    if( (create_instance_mkdir(subdir, NEWDIR_MODE)) )
        return make_error("mkdir %s failed (%s)", subdir, ds_system_errmsg());

    PR_snprintf(subdir, sizeof(subdir), "%s%cauthdb%cdefault", sroot, FILE_PATHSEP, FILE_PATHSEP); 
    if( (create_instance_mkdir(subdir, NEWDIR_MODE)) )
        return make_error("mkdir %s failed (%s)", subdir, ds_system_errmsg());

    /* Create DS-nickname (corresponding to ServiceID) key in registry */
    PR_snprintf(line, sizeof(line), "%s\\%s\\%s-%s", KEY_SOFTWARE_NETSCAPE, SVR_KEY_ROOT, 
        SVR_ID_SERVICE, cf->servid);

    Result = RegCreateKey(HKEY_LOCAL_MACHINE, line, &hServerKey);
    if (Result != ERROR_SUCCESS) {
        return make_error("Could not create registry server key %s - error %d (%s)",
            line, GetLastError(), ds_system_errmsg());
    }
    
    // note that SVR_ID_PRODUCT is being used here, which is of the form dsX
    // as opposed to SVR_ID_SERVICE, which is of the form dsX30
    PR_snprintf(line, sizeof(line), "%s\\%s-%s\\config", sroot, SVR_ID_PRODUCT, cf->servid);
    Result = RegSetValueEx(hServerKey, VALUE_CONFIG_PATH, 0, REG_SZ, 
        line, strlen(line) + 1);

    RegCloseKey(hServerKey);

     /* Create SNMP key in registry */
    PR_snprintf(line, sizeof(line), "%s\\%s\\%s", KEY_SOFTWARE_NETSCAPE, SVR_KEY_ROOT, 
        KEY_SNMP_CURRENTVERSION);

    Result = RegCreateKey(HKEY_LOCAL_MACHINE, line, &hServerKey);
    if (Result != ERROR_SUCCESS) {
        return make_error("Could not create registry server key %s - error %d (%s)",
            line, GetLastError(), ds_system_errmsg());
    }
    
    
    /* Create the SNMP Pathname value */
    PR_snprintf(line, sizeof(line), "%s\\%s", sroot, SNMP_PATH);
    Result = RegSetValueEx(hServerKey, VALUE_APP_PATH, 0, REG_SZ, 
        line, strlen(line) + 1);    
    RegCloseKey(hServerKey);

    /* write SNMP extension agent value to Microsoft SNMP Part of Registry)  */
    PR_snprintf(line, sizeof(line), "%s\\%s", KEY_SERVICES, KEY_SNMP_SERVICE); 
    Result = RegOpenKeyEx(HKEY_LOCAL_MACHINE, 
                          line,
                          0,
                          KEY_ALL_ACCESS,
                          &hServerKey);
    /* if its there set the value, otherwise go on to the next thing  */
    if (Result == ERROR_SUCCESS) 
    {
        /* extension agents should have linearly increasing value, 
        make sure it doesn't already exist, find last one and increment 
        value for new key */

        PR_snprintf(line, sizeof(line), "%s\\%s\\%s",    KEY_SOFTWARE_NETSCAPE, SVR_KEY_ROOT, KEY_SNMP_CURRENTVERSION);

        Result = RegQueryInfoKey(hServerKey, NULL, NULL, NULL, NULL, NULL,
                                 NULL, &NumValues, NULL, NULL, NULL, NULL);
      
        if (Result == ERROR_SUCCESS){
            for(iterator = 0; iterator <= NumValues; iterator++)
            {
                /* initialize to max size to avoid
                   ERROR_MORE_DATA because size gets set
                   to actual size of key after call 
                   to RegQueryValueEx, previously there
                   was a bug if last key was smaller
                   than this one it would return ERROR_MORE_DATA
                   and it would not find the key if it was already there
                */
                sizeof_value_data_buffer=MAX_PATH;
                PR_snprintf(NumValuesBuf, sizeof(NumValuesBuf), "%d", iterator);
                Result = RegQueryValueEx(hServerKey,
                                      NumValuesBuf,
                                      NULL,
                                      &type_buffer,
                                      value_data_buffer,
                                      &sizeof_value_data_buffer
                                     );

                if(!strcmp(value_data_buffer, line))
                {
                    value_already_exists = 1;
                }
            }                
        }
                
        if(!value_already_exists)
        {
              PR_snprintf(NumValuesBuf, sizeof(NumValuesBuf), "%d", NumValues + 1); 
            Result = RegSetValueEx(hServerKey, NumValuesBuf, 0, REG_SZ,
                                 line, strlen(line) + 1);

            /* couldn't set this value, so there is a real problem */
            if (Result != ERROR_SUCCESS)
            {
                return make_error("Could not set value %s (%d)",
                line, Result);
            }
        }

    }
    RegCloseKey(hServerKey);
        
    return NULL;
}
#endif

/* ---------------------- Create configuration files ---------------------- */


char *create_server(server_config_s *cf, char *param_name)
{
    char line[PATH_SIZE], *t, *sroot = cf->sroot;
    char subdir[PATH_SIZE];

#if defined( SOLARIS )
    /*
     * Solaris 9+ specific installation 
     */
    char otherline[PATH_SIZE];
    char subdirvar[PATH_SIZE];
    char subdiretc[PATH_SIZE];
    char *sub;
#endif /* SOLARIS */

    if (param_name)
        param_name[0] = 0; /* init to empty string */

#ifdef XP_UNIX
    if (!cf->servuser)
        getSuiteSpotUserGroup(cf);
#else
    /* Abort if the service exists on NT */
    if (t = service_exists(cf->servid)) {
        PL_strncpyz(param_name, "servid", BIG_LINE);
        return t;
    }
#endif

    if( (t = sanity_check(cf, param_name)) )
        return t;

    /* Create slapd-nickname directory */
    PR_snprintf(subdir, sizeof(subdir), "%s%c"PRODUCT_NAME"-%s", sroot, FILE_PATHSEP, 
                 cf->servid);
    if( (create_instance_mkdir(subdir, NEWDIR_MODE)) )
        return make_error("mkdir %s failed (%s)", subdir, ds_system_errmsg());
    
    /* Create slapd-nickname/config directory */
    PR_snprintf(line, sizeof(line), "%s%cconfig", subdir, FILE_PATHSEP);
    if( (create_instance_mkdir(line, NEWDIR_MODE)) ) 
        return make_error("mkdir %s failed (%s)", line, ds_system_errmsg());

    /* Create slapd-nickname/config/schema directory */
    PR_snprintf(line, sizeof(line), "%s%cconfig%cschema", subdir, FILE_PATHSEP, FILE_PATHSEP);
    if( (create_instance_mkdir(line, NEWDIR_MODE)) ) 
        return make_error("mkdir %s failed (%s)", line, ds_system_errmsg());

#if defined (BUILD_PRESENCE)
    /* Create slapd-nickname/config/presence directory */
    PR_snprintf(line, sizeof(line), "%s%cconfig%cpresence", subdir, FILE_PATHSEP, FILE_PATHSEP);
    if( (create_instance_mkdir(line, NEWDIR_MODE)) ) 
        return make_error("mkdir %s failed (%s)", line, ds_system_errmsg());
#endif

    /* Create slapd-nickname/logs directory */
    PR_snprintf(line, sizeof(line), "%s%clogs", subdir, FILE_PATHSEP);
    if( (create_instance_mkdir(line, NEWSECDIR_MODE)) )
        return make_error("mkdir %s failed (%s)", line, ds_system_errmsg());

    /* Create httpacl directory */
    PR_snprintf(line, sizeof(line), "%s%chttpacl", cf->sroot, FILE_PATHSEP); 
    if( (create_instance_mkdir(line, NEWDIR_MODE)) )
        return make_error("mkdir %s failed (%s)", line, ds_system_errmsg());

#ifdef XP_UNIX
    /* Start/stop/rotate/restart scripts */
    if (getenv("USE_DEBUGGER"))
    {
        char *debugger = getenv("DSINST_DEBUGGER");
        char *debugger_command = getenv("DSINST_DEBUGGER_CMD");
        if (! debugger) {
            debugger = "/tools/ns/workshop/bin/dbx";
        }
        if (! debugger_command) {
            debugger_command = "echo"; /* e.g. do nothing */
        }
#ifdef OSF1
        printf("-D %s -i %s/logs/pid -d %s -z\n", subdir, subdir,
               cf->loglevel ? cf->loglevel : "0");
        t = gen_script(subdir, START_SCRIPT,
               "\n"
               "# Script that starts the ns-slapd server.\n"
               "# Exit status can be:\n"
               "#       0: Server started successfully\n"
               "#       1: Server could not be started\n"
               "#       2: Server already running\n"
               "\n"
               "NETSITE_ROOT=%s\n"
               "export NETSITE_ROOT\n"
               "PIDFILE=%s/logs/pid\n"
               "if test -f $PIDFILE ; then\n"
               "    PID=`cat $PIDFILE`\n"
               "    if kill -0 $PID > /dev/null 2>&1 ; then\n"
               "        echo There is an ns-slapd process already running: $PID\n"
               "        exit 2;\n"
               "    else\n"
               "        rm -f $PIDFILE\n"
               "    fi\n"
               "fi\n"
               "cd %s/bin/%s/server; ./%s -D %s -i %s/logs/pid -d %s -z \"$@\" &\n"
               "loop_counter=1\n"
               "max_count=120\n"
               "while test $loop_counter -le $max_count; do\n"
               "    loop_counter=`expr $loop_counter + 1`\n"
               "    if test ! -f $PIDFILE ; then\n"
               "        sleep 1;\n"
               "    else\n"
               "        PID=`cat $PIDFILE`\n"
            /* rbyrne: setupsdk takes any message here as an error:
                "        echo Server has been started. ns-slapd process started: $PID\n"*/
               "        exit 0;\n"
               "    fi\n"
               "done\n"
               "echo Server not running!! Failed to start ns-slapd process.\n"
               "exit 1\n",
               sroot, subdir, sroot, PRODUCT_NAME, PRODUCT_BIN, subdir,
               subdir,
           cf->loglevel ? cf->loglevel : "0"
        );
/*
        t = gen_script(subdir, START_SCRIPT,
               "NETSITE_ROOT=%s\n"
               "export NETSITE_ROOT\n"
               "cd %s/bin/%s/server; /usr/bin/X11/xterm -fn 10x20 -sb -sl 2000 -e /bin/ladebug "
               "-I /u/richm/ds50/ldapserver/ldap/servers/slapd/back-ldbm "
               "-I /u/richm/ds50/ldapserver/ldap/servers/slapd "
               "%s &\n",
               sroot, sroot, PRODUCT_NAME, PRODUCT_BIN
        );
*/
#else
        t = gen_script(subdir, START_SCRIPT,
               "\n"
               "# Script that starts the ns-slapd server.\n"
               "# Exit status can be:\n"
               "#       0: Server started successfully\n"
               "#       1: Server could not be started\n"
               "#       2: Server already running\n"
               "\n"
               "NETSITE_ROOT=%s\n"
               "export NETSITE_ROOT\n"
               "PIDFILE=%s/logs/pid\n"
               "if test -f $PIDFILE ; then\n"
               "    PID=`cat $PIDFILE`\n"
               "    if kill -0 $PID > /dev/null 2>&1 ; then\n"
               "        echo There is an ns-slapd process already running: $PID\n"
               "        exit 2;\n"
               "    else\n"
               "        rm -f $PIDFILE\n"
               "    fi\n"
               "fi\n"
               "if [ -x /usr/local/bin/xterm ]; then\n"
               "  xterm=/usr/local/bin/xterm\n"
               "else\n"
               "  xterm=/usr/openwin/bin/xterm\n"
               "fi\n"
               "cd %s/bin/%s/server; $xterm -title debugger -e %s -c \"dbxenv follow_fork_mode child ; stop in main ; %s ; run -D %s -i %s/logs/pid -d %s -z $*\" %s &\n"
               "loop_counter=1\n"
               "max_count=120\n"
               "while test $loop_counter -le $max_count; do\n"
               "    loop_counter=`expr $loop_counter + 1`\n"
               "    if test ! -f $PIDFILE ; then\n"
               "        sleep 1;\n"
               "    else\n"
               "        PID=`cat $PIDFILE`\n"
                /* rbyrne: setupsdk takes any message here as an error:
               "        echo Server has been started. ns-slapd process started: $PID\n"*/
               "        exit 0;\n"
               "    fi\n"
               "done\n"
               "echo Server not running!! Failed to start ns-slapd process.\n"
               "exit 1\n",
               sroot, subdir, sroot, PRODUCT_NAME, debugger, debugger_command,
               subdir,
               subdir, cf->loglevel ? cf->loglevel : "0", PRODUCT_BIN
        );
#endif
    }
    else
    {
        t = gen_script(subdir, START_SCRIPT,
            "\n"
            "# Script that starts the ns-slapd server.\n"
            "# Exit status can be:\n"
            "#       0: Server started successfully\n"
            "#       1: Server could not be started\n"
            "#       2: Server already running\n"
            "\n"
            "NETSITE_ROOT=%s\n"
            "export NETSITE_ROOT\n"
            "PIDFILE=%s/logs/pid\n"
            "STARTPIDFILE=%s/logs/startpid\n"
            "if test -f $STARTPIDFILE ; then\n"
            "    PID=`cat $STARTPIDFILE`\n"
            "    if kill -0 $PID > /dev/null 2>&1 ; then\n"
            "        echo There is an ns-slapd process already running: $PID\n"
            "        exit 2;\n"
            "    else\n"
            "        rm -f $STARTPIDFILE\n"
            "    fi\n"
            "fi\n"
            "if test -f $PIDFILE ; then\n"
            "    PID=`cat $PIDFILE`\n"
            "    if kill -0 $PID > /dev/null 2>&1 ; then\n"
            "        echo There is an ns-slapd process already running: $PID\n"
            "        exit 2;\n"
            "    else\n"
            "        rm -f $PIDFILE\n"
            "    fi\n"
            "fi\n"
            "cd %s/bin/%s/server; ./%s -D %s -i %s/logs/pid -w $STARTPIDFILE \"$@\"\n"
            "if [ $? -ne 0 ]; then\n"
            "    exit 1\n"
            "fi\n"
            "\n"
            "loop_counter=1\n"
            "# wait for 10 seconds for the start pid file to appear\n"
            "max_count=10\n"
            "while test $loop_counter -le $max_count; do\n"
            "    loop_counter=`expr $loop_counter + 1`\n"
            "    if test ! -f $STARTPIDFILE ; then\n"
            "        sleep 1;\n"
            "    else\n"
            "        PID=`cat $STARTPIDFILE`\n"
            "    fi\n"
            "done\n"
            "if test ! -f $STARTPIDFILE ; then\n"
            "    echo Server failed to start !!! Please check errors log for problems\n"
            "    exit 1\n"
            "fi\n"
            "loop_counter=1\n"
            "# wait for 10 minutes (600 times 1 seconds)\n"
            "max_count=600\n" /* 10 minutes */
            "while test $loop_counter -le $max_count; do\n"
            "    loop_counter=`expr $loop_counter + 1`\n"
            "    if test ! -f $PIDFILE ; then\n"
            "        if kill -0 $PID > /dev/null 2>&1 ; then\n"
            "            sleep 1\n"
            "    else\n"
            "        echo Server failed to start !!! Please check errors log for problems\n"
            "        exit 1\n"
            "    fi\n"
            "    else\n"
            "        PID=`cat $PIDFILE`\n"
            /* rbyrne: setupsdk takes any message here as an error:
            "        echo Server has been started. ns-slapd process started: $PID\n"*/
            "        exit 0;\n"
            "    fi\n"
            "done\n"
            "echo Server not running!! Failed to start ns-slapd process.  Please check the errors log for problems.\n"
            "exit 1\n",
            sroot, subdir, subdir, sroot, PRODUCT_NAME, PRODUCT_BIN, subdir,
            subdir
        );
    }
    if(t) return t;
    
    t = gen_script(subdir, STOP_SCRIPT,
           "\n"
           "# Script that stops the ns-slapd server.\n"
           "# Exit status can be:\n"
           "#       0: Server stopped successfully\n"
           "#       1: Server could not be stopped\n"
           "#       2: Server was not running\n"
           "\n"
           "PIDFILE=%s/logs/pid\n"
           "if test ! -f $PIDFILE ; then\n"
           "    echo No ns-slapd PID file found. Server is probably not running\n"
           "    exit 2\n"
           "fi\n"
           "PID=`cat $PIDFILE`\n"
           "# see if the server is already stopped\n"
           "kill -0 $PID > /dev/null 2>&1 || {\n"
           "    echo Server not running\n"
           "    if test -f $PIDFILE ; then\n"
           "        rm -f $PIDFILE\n"
           "    fi\n"
           "    exit 2\n"
           "}\n"
           "# server is running - kill it\n"
           "kill $PID\n"
           "loop_counter=1\n"
           "# wait for 10 minutes (600 times 1 second)\n"
           "max_count=600\n" /* 10 minutes */
           "while test $loop_counter -le $max_count; do\n"
           "    loop_counter=`expr $loop_counter + 1`\n"
           "    if kill -0 $PID > /dev/null 2>&1 ; then\n"
           "        sleep 1;\n"
           "    else\n"
           "        if test -f $PIDFILE ; then\n"
           "            rm -f $PIDFILE\n"
           "        fi\n"
            /* rbyrne: setupsdk takes any message here as an error:
               "        echo Server has been stopped. ns-slapd process stopped: $PID\n"*/
           "        exit 0\n"
           "    fi\n"
           "done\n"
           "if test -f $PIDFILE ; then\n"
           "    echo Server still running!! Failed to stop the ns-slapd process: $PID.  Please check the errors log for problems.\n"
           "fi\n"
           "exit 1\n",
            subdir);
    if(t) return t;

    t = gen_script(subdir, RESTART_SCRIPT,
           "\n"
           "# Script that restarts the ns-slapd server.\n"
           "# Exit status can be:\n"
           "#       0: Server restarted successfully\n"
           "#       1: Server could not be started\n"
           "#       2: Server started successfully (was not running)\n"
           "#       3: Server could not be stopped\n"
           "\n"
           "server_already_stopped=0\n"
           "%s/stop-slapd\n"
           "status=$?\n"
           "if [ $status -eq 1 ] ; then\n"
           "    exit 3;\n"
           "else\n"
           "   if [ $status -eq 2 ] ; then\n"
           "        server_already_stopped=1\n"
           "   fi\n"
           "fi\n"
           "%s/start-slapd\n"
           "status=$?\n"
           "if [ $server_already_stopped -eq 1 ] && [ $status -eq 0 ] ; then\n"
           "    exit 2;\n"
           "fi\n"
           "exit $status\n",
           subdir, subdir );
    if(t) return t;

    /* logs subdir owned by server user */
    if( (t = chownlogs(subdir, cf->servuser)) )
        return t;

    /* config subdir owned by server user */
    if( (t = chownconfig(subdir, cf->servuser)) )
        return t;

#else  /* XP_WIN32 */
    /* Windows platforms have some extra setup */
    if( (t = setup_ntserver(cf)) )
        return t;

    /* generate start script */
    t = gen_script(subdir, START_SCRIPT".bat", "net start slapd-%s\n", cf->servid);
    if(t) return t;

    /* generate stop script */
    t = gen_script(subdir, STOP_SCRIPT".bat", "net stop slapd-%s\n", cf->servid);
    if(t) return t;

    /* generate restart script */
    t = gen_script(subdir, RESTART_SCRIPT".bat", "net stop slapd-%s\n"
                   "net start slapd-%s\n", cf->servid, cf->servid);
    if(t) return t;


#endif /* XP_WIN32 */

#ifdef XP_WIN32

    if ( INFO_GetOperatingSystem () == OS_WINNT ) {

        if( (t =  add_ntservice(cf)) )
            return t;
    }
#endif

    /* Create subdirectories and config files for directory server */
    if( (t = install_ds(sroot, cf, param_name)) )
        return t;

    /* XXXrobm using link to start script instead of automatically doing it */
    return NULL;
}




/* ------------------------- Copied from libadmin ------------------------- */


/*
   These replace the versions in libadmin to allow error returns. 

   XXXrobm because libadmin calls itself a lot, I'm replacing ALL the
   functions this file requires
 */


int create_instance_exists(char *fn)
{
    struct stat finfo;

    if(stat(fn, &finfo) < 0)
        return 0;
    else
        return 1;
}


int create_instance_mkdir(char *dir, int mode)
{
    if(!create_instance_exists(dir)) {
#ifdef XP_UNIX
        if(mkdir(dir, mode) == -1)
#else  /* XP_WIN32 */
        if(!CreateDirectory(dir, NULL))
#endif /* XP_WIN32 */
            return -1;
    }
    return 0;
}


char *create_instance_mkdir_p(char *dir, int mode)
{
    static char errmsg[ERR_SIZE];
    struct stat fi;
    char *t;

#ifdef XP_UNIX
    t = dir + 1;
#else /* XP_WIN32 */
    t = dir + 3;
#endif /* XP_WIN32 */

    while(1) {
        t = strchr(t, FILE_PATHSEP);

        if(t) *t = '\0';
        if(stat(dir, &fi) == -1) {
            if(create_instance_mkdir(dir, mode) == -1) {
                PR_snprintf(errmsg, sizeof(errmsg), "mkdir %s failed (%s)", dir, ds_system_errmsg());
                return errmsg;
            }
        }
        if(t)
        {
            *t = FILE_PATHSEP;
            LDAP_UTF8INC(t);
        }
        else break;
    }
    return NULL;
}


int create_instance_numbers(char *target)
{
       char *p;
    for(p=target; *p; LDAP_UTF8INC(p) )
    {
        if(!ldap_utf8isdigit(p))
            return 0;
    }
    return 1;
}

#if defined( SOLARIS )
/*
 * Solaris 9+ specific installation
 */
int create_instance_symlink(char *actualpath, char *sympath)
{
    if(symlink(actualpath, sympath) == -1)
            return -1;
    return 0;
}
#endif /* SOLARIS */


/* --------------------------------- try* --------------------------------- */


/* robm This doesn't use net_ abstractions because they drag in SSL */
int trybind(char *addr, int port)
{
    int sd;
    struct sockaddr_in sa_server;
    int ret;

#ifdef XP_WIN32
    WSADATA wsd;

    if(WSAStartup(MAKEWORD(1, 1), &wsd) != 0)
        return -1;
#endif

    if ((sd = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP)) == -1)
        goto you_lose;

    if (addr == NULL)
        addr = "127.0.0.1"; /* use the local loopback address */

    memset((char *) &sa_server, 0, sizeof(sa_server));
    sa_server.sin_family=AF_INET;
    sa_server.sin_addr.s_addr = inet_addr(addr);
    sa_server.sin_port=htons((short)port);
    ret = connect(sd, (struct sockaddr *) &sa_server,sizeof(sa_server));
    if (ret == -1)
        ret = 0; /* could not connect, so port is not in use; that's good */
    else
    {
        ret = -1; /* connection succeeded, port in use, bad */
        errno = EADDRINUSE;
    }
#ifdef XP_UNIX
    close(sd);
#else
    closesocket(sd);
    WSACleanup();
#endif
    return ret;

you_lose:
#ifdef XP_WIN32
    WSACleanup();
#endif
    return -1;
}


#ifdef XP_UNIX
#include <pwd.h>
#include <fcntl.h>

int tryuser(char *user)
{
    struct passwd *pw;
    char fn[128];
    int fd, ret;

    setpwent();
    if(!(pw = getpwnam(user)))
        return -1;

    endpwent();

    if(geteuid())
        return 0;

    PR_snprintf(fn, sizeof(fn), "/tmp/trychown.%ld", (long)getpid());
    if( (fd = creat(fn, 0777)) == -1)
        return 0;         /* Hmm. */
    ret = chown(fn, pw->pw_uid, pw->pw_gid);
    close(fd);
    unlink(fn);
    return (ret == -1 ? -2 : 0);
}
#endif /* XP_UNIX */


/* --------------------------- create_instance_check* ---------------------------- */


char *create_instance_checkport(char *addr, char *sport)
{
    int port;

    port = atoi(sport);
    if((port < 1) || (port > 65535)) {
        return ("Valid port numbers are between 1 and 65535");
    }
    if(trybind(addr, port) == -1) {
        if(errno == EADDRINUSE)    {
            return make_error("Port %d is already in use", port);
        } 
        /* XXXrobm if admin server not running as root, you lose. */
        else if(errno == EACCES) {
            return ("Ports below 1024 require super user access.  "
                    "You must run the installation as root to install "
                    "on that port.");
        } else {
            ds_report_warning(DS_WARNING, "port", "That port is not available");
        }
    }
    return NULL;
}

#ifdef XP_UNIX
char *create_instance_checkuser(char *user)
{
    if (user && *user) switch(tryuser(user)) {
      case -1:
        return make_error ("Can't find a user named '%s'."
               "\nPlease select or create another user.",
               user);
      case -2:
        return make_error ("Can't change a file to be owned by %s."
               "\nPlease select or create another user.",
               user);
    }
    return NULL;
}
#endif


/* --------------------------------- copy --------------------------------- */

#define COPY_BUFFER_SIZE        4096

#ifdef XP_UNIX

 
char *create_instance_copy(char *sfile, char *dfile, int mode)
{
    int sfd, dfd, len;
    struct stat fi;
 
    char copy_buffer[COPY_BUFFER_SIZE];
    unsigned long read_len;
 
/* Make sure we're in the right umask */
    umask(022);

    if( (sfd = open(sfile, O_RDONLY)) == -1)
        return make_error("Cannot open %s for reading (%s)", sfile, 
                          ds_system_errmsg());
 
    fstat(sfd, &fi);
    if(!(S_ISREG(fi.st_mode))) {
        close(sfd);
        return make_error("%s is not a regular file", sfile);
    }
    len = fi.st_size;
 
    if( (dfd = open(dfile, O_RDWR | O_CREAT | O_TRUNC, mode)) == -1)
        return make_error("Cannot open file %s for writing (%s)", dfile, 
                          ds_system_errmsg());
 
    while(len) {
        read_len = len>COPY_BUFFER_SIZE?COPY_BUFFER_SIZE:len;
 
        if ( (read_len = read(sfd, copy_buffer, read_len)) == -1) {
            make_error("Cannot read from file %s (%s)", 
                       sfile, ds_system_errmsg());
        }
 
        if ( write(dfd, copy_buffer, read_len) != read_len) {
            make_error("Error writing to file %s from copy of %s (%s)",
                       dfile, sfile, ds_system_errmsg());
        }
 
        len -= read_len;
    }
    close(sfd);
    close(dfd);
    /* BERT! */
    return NULL;
}

#else /* XP_WIN32 */
char *create_instance_copy(char *sfile, char *dfile, int mode)
{
    HANDLE sfd, dfd, MapHandle;
    PCHAR fp;
    PCHAR fpBase;
    DWORD BytesWritten = 0;
    DWORD len;

    if( (sfd = CreateFile(sfile, GENERIC_READ,
                FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
                OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL))
                        == INVALID_HANDLE_VALUE) {
        return make_error("Cannot open file %s for reading (%s)", sfile,
                          ds_system_errmsg());
    }
    len = GetFileSize(sfd, NULL);
    if( (MapHandle = CreateFileMapping(sfd, NULL, PAGE_READONLY,
                0, 0, NULL)) == NULL) {
        return make_error("Cannot create file mapping of %s (%s)", sfile,
                          ds_system_errmsg());
    }
    if (!(fpBase = fp = MapViewOfFile(MapHandle, FILE_MAP_READ, 0, 0, 0))) {
        return make_error("Cannot map file %s (%s)", sfile, ds_system_errmsg());
    }
    if( (dfd = CreateFile(dfile, GENERIC_READ | GENERIC_WRITE,
        FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,OPEN_ALWAYS,
        FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE) {
        return make_error("Cannot open destination file %s for writing (%s)",
                          dfile, ds_system_errmsg());
    }
    while ( len) {
        if(!WriteFile(dfd, fp, len, &BytesWritten, NULL)) {
            return ("Cannot write new file %s (%s)", dfile, ds_system_errmsg());
        }
        len -= BytesWritten;
        fp += BytesWritten;
    }
    CloseHandle(sfd);
    UnmapViewOfFile(fpBase);
    CloseHandle(MapHandle);
    FlushFileBuffers(dfd);
    CloseHandle(dfd);
    /* BERT! */
    return NULL;
}
#endif

static int
file_is_type_x(const char *dirname, const char *filename, PRFileType x)
{
    struct PRFileInfo inf;
    int status = 0;
    char *fullpath = PR_smprintf("%s/%s", dirname, filename);
    if (PR_SUCCESS == PR_GetFileInfo(fullpath, &inf) &&
        inf.type == x)
        status = 1;

    PR_smprintf_free(fullpath);

    return status;
}

/* return true if the given path and file corresponds to a directory */
static int
is_a_dir(const char *dirname, const char *filename)
{
    return file_is_type_x(dirname, filename, PR_FILE_DIRECTORY);
}

#if 0 /* not being used */
/* return true if the given path and file corresponds to a regular file */
static int
is_a_file(const char *dirname, const char *filename)
{
    return file_is_type_x(dirname, filename, PR_FILE_FILE);
}
#endif

static char *
ds_copy_group_files_using_mode(char *src_dir, char *dest_dir, 
                   char *filter, int use_mode)
{
    char *t = 0;
    PRDir *ds = 0;
    PRDirEntry *d = 0;
    char src_file[PATH_SIZE], dest_file[PATH_SIZE], fullname[PATH_SIZE];

    if(!(ds = PR_OpenDir(src_dir))) {
        return make_error("Can't read directory %s (%s)", src_dir, ds_system_errmsg());
    }
    while( (d = PR_ReadDir(ds, 0)) ) {
        if(d->name[0] != '.') {
            if(!filter || strstr(d->name, filter)) {
                PR_snprintf(fullname, sizeof(fullname), "%s/%s", src_dir, d->name);
                if(PR_SUCCESS != PR_Access(fullname, PR_ACCESS_EXISTS))
                    continue;
                PR_snprintf(src_file, sizeof(src_file),  "%s%c%s", src_dir,  FILE_PATHSEP, d->name);
                PR_snprintf(dest_file, sizeof(dest_file), "%s%c%s", dest_dir, FILE_PATHSEP, d->name);
                if(is_a_dir(src_dir, d->name)) {
                    char *sub_src_dir = strdup(src_file);
                    char *sub_dest_dir = strdup(dest_file);
                    if( (t = create_instance_mkdir_p(sub_dest_dir, NEWDIR_MODE)) )
                        return(t);
                    if( (t = ds_copy_group_files_using_mode(sub_src_dir, sub_dest_dir, filter, use_mode)) )
                        return t;
                    free(sub_src_dir);
                    free(sub_dest_dir);
                }
                else {
                    if( (t = create_instance_copy(src_file, dest_file, use_mode)) )
                        return t;
                }
            }
        }
    }
    PR_CloseDir(ds);
    return(NULL);
}

static char *
ds_copy_group_files(char *src_dir, char *dest_dir, char *filter)
{
    return ds_copy_group_files_using_mode(src_dir, dest_dir, filter, 
                                          NEWFILE_MODE);
}

#if 0 /* not being used */
static char *
ds_copy_group_bins(char *src_dir, char *dest_dir, char *filter, 
                   int use_mode)
{
    return ds_copy_group_files_using_mode(src_dir, dest_dir, filter, 
                                          NEWSCRIPT_MODE);
}
#endif

/* this macro was copied from libldap/tmplout.c */
#define HREF_CHAR_ACCEPTABLE( c )    (( c >= '-' && c <= '9' ) ||    \
                     ( c >= '@' && c <= 'Z' ) ||    \
                     ( c == '_' ) ||        \
                     ( c >= 'a' && c <= 'z' ))

/* this function is based on libldap/tmplout.c:strcat_escaped */
void fputs_escaped(char *s, FILE *fp)
{
    char    *hexdig = "0123456789ABCDEF";
    register unsigned char c;
    for ( ; (c = *(unsigned char*)s); ++s ) {
        if ( HREF_CHAR_ACCEPTABLE( c )) {
            putc( c, fp );
        } else {
            fprintf( fp, "%%%c%c", hexdig[ (c >> 4) & 0x0F ], hexdig[ c & 0x0F ] );
        }
    }
}

/* ------------- Create config files for Directory Server -------------- */

char *ds_cre_subdirs(char *sroot, server_config_s *cf, char *cs_path,
             struct passwd* pw)
{
    char subdir[PATH_SIZE], *t = NULL;

    /* create subdir <a_server>/db */
    PR_snprintf(subdir, sizeof(subdir), "%s%cdb", cs_path, FILE_PATHSEP);
    if( (t = create_instance_mkdir_p(subdir, NEWDIR_MODE)) )
        return(t);
    chownfile (pw, subdir);

    /* create subdir <a_server>/ldif */
    PR_snprintf(subdir, sizeof(subdir), "%s%cldif", cs_path, FILE_PATHSEP);
    if( (t = create_instance_mkdir_p(subdir, NEWDIR_MODE)) )
        return(t);
    chownfile (pw, subdir);

    /* create subdir <a_server>/dsml */
    PR_snprintf(subdir, sizeof(subdir), "%s%cdsml", cs_path, FILE_PATHSEP);
    if( (t = create_instance_mkdir_p(subdir, NEWDIR_MODE)) )
        return(t);
    chownfile (pw, subdir);

    /* create subdir <a_server>/bak */
    PR_snprintf(subdir, sizeof(subdir), "%s%cbak", cs_path, FILE_PATHSEP);
    if( (t = create_instance_mkdir_p(subdir, NEWDIR_MODE)) )
        return(t);
    chownfile (pw, subdir);

    /* Create slapd-nickname/confbak directory */
    PR_snprintf(subdir, sizeof(subdir), "%s%cconfbak", cs_path, FILE_PATHSEP);
    if( (t=create_instance_mkdir_p(subdir, NEWDIR_MODE)) )
        return(t);
    chownfile (pw, subdir);

    /* create subdir <server_root>/dsgw/context */
    PR_snprintf(subdir, sizeof(subdir), "%s%cclients", sroot, FILE_PATHSEP);
    if (is_a_dir(subdir, "dsgw")) { /* only create dsgw stuff if we are installing it */
        PR_snprintf(subdir, sizeof(subdir), "%s%cclients%cdsgw%ccontext", sroot, FILE_PATHSEP,FILE_PATHSEP,FILE_PATHSEP);
        if( (t = create_instance_mkdir_p(subdir, NEWDIR_MODE)) )
            return(t);
    }

  /* create subdir <server_root>/bin/slapd/authck */
    PR_snprintf(subdir, sizeof(subdir), "%s%cbin%cslapd%cauthck", sroot, FILE_PATHSEP, FILE_PATHSEP, FILE_PATHSEP);
    if( (t = create_instance_mkdir_p(subdir, NEWDIR_MODE)) )
        return(t);

    return (t);
}

#define CREATE_LDIF2DB() \
    gen_perl_script_auto(mysroot, mycs_path, "ldif2db.pl", cf)

#define CREATE_DB2INDEX() \
    gen_perl_script_auto(mysroot, mycs_path, "db2index.pl", cf)

#define CREATE_DB2LDIF() \
    gen_perl_script_auto(mysroot, mycs_path, "db2ldif.pl", cf)

#define CREATE_DB2BAK() \
    gen_perl_script_auto(mysroot, mycs_path, "db2bak.pl", cf)

#define CREATE_BAK2DB() \
    gen_perl_script_auto(mysroot, mycs_path, "bak2db.pl", cf)

#define CREATE_VERIFYDB() \
    gen_perl_script_auto(mysroot, mycs_path, "verify-db.pl", cf)

#define CREATE_REPL_MONITOR_CGI() \
    gen_perl_script_auto(mysroot, cgics_path, "repl-monitor-cgi.pl", cf)

#define CREATE_ACCOUNT_INACT(_commandName) \
    gen_perl_script_auto(mysroot, cs_path, _commandName, cf)

#define CREATE_DSML() \
    gen_perl_script_auto(mysroot, mycs_path, "dsml-activate.pl", cf)

#define CREATE_MIGRATETO5() \
    gen_perl_script_auto_for_migration(mysroot, mycs_path, "migrateTo5", cf)

#define CREATE_MIGRATE50TO51() \
    gen_perl_script_auto_for_migration(mysroot, mycs_path, "migrate50to51", cf)

#define CREATE_MIGRATEINSTANCE5() \
    gen_perl_script_auto_for_migration(mysroot, mycs_path, "migrateInstance5", cf)

#define CREATE_MIGRATE5TO6() \
    gen_perl_script_auto_for_migration(mysroot, mycs_path, "migrate5to6", cf)

#define CREATE_MIGRATEINSTANCE6() \
    gen_perl_script_auto_for_migration(mysroot, mycs_path, "migrateInstance6", cf)

#define CREATE_MIGRATETO6() \
    gen_perl_script_auto_for_migration(mysroot, mycs_path, "migrateTo6", cf)

#define CREATE_MIGRATE5TO7() \
    gen_perl_script_auto_for_migration(mysroot, mycs_path, "migrate5to7", cf)

#define CREATE_MIGRATE6TO7() \
    gen_perl_script_auto_for_migration(mysroot, mycs_path, "migrate6to7", cf)

#define CREATE_MIGRATEINSTANCE7() \
    gen_perl_script_auto_for_migration(mysroot, mycs_path, "migrateInstance7", cf)

#define CREATE_MIGRATETO7() \
    gen_perl_script_auto_for_migration(mysroot, mycs_path, "migrateTo7", cf)

#define CREATE_NEWPWPOLICY() \
    gen_perl_script_auto(mysroot, mycs_path, "ns-newpwpolicy.pl", cf)

#ifdef XP_UNIX
char *ds_gen_scripts(char *sroot, server_config_s *cf, char *cs_path)
{
    char *t = NULL;
    char server[PATH_SIZE], admin[PATH_SIZE], tools[PATH_SIZE];
    char cgics_path[PATH_SIZE];
    char *cl_scripts[7] = {"dsstop", "dsstart", "dsrestart", "dsrestore", "dsbackup", "dsimport", "dsexport"};
    char *cl_javafiles[7] = {"DSStop", "DSStart", "DSRestart", "DSRestore", "DSBackup", "DSImport", "DSExport"};
    int  cls = 0; /*Index into commandline script names and java names - RJP*/
    char *mysroot, *mycs_path;

#if defined( SOLARIS )
    /*
     * Solaris 9+ specific installation
     */
    char fn[PATH_SIZE];
#endif /* SOLARIS */

    mysroot = sroot;
    mycs_path = cs_path;

    PR_snprintf(server, sizeof(server), "%s/bin/"PRODUCT_NAME"/server", sroot);    
    PR_snprintf(admin, sizeof(admin), "%s/bin/"PRODUCT_NAME"/admin/bin", sroot);    
    PR_snprintf(tools, sizeof(tools), "%s/shared/bin", sroot);    
    PR_snprintf(cgics_path, sizeof(cgics_path), "%s%cbin%cadmin%cadmin%cbin", sroot,
            FILE_PATHSEP, FILE_PATHSEP, FILE_PATHSEP, FILE_PATHSEP);

    t = gen_script(cs_path, "monitor", 
           "if [ \"x$1\" != \"x\" ];\nthen MDN=\"$1\";\nelse MDN=\"cn=monitor\";\n fi\n"

           "cd %s\nPATH=%s:$PATH;export PATH\n"
           "ldapsearch -p %s -b \"$MDN\" -s base \"objectClass=*\"\n",
                   tools, tools, cf->servport);
    if(t) return t;
    
    t = gen_script(cs_path, "saveconfig", 
        "cd %s\n"
        "echo saving configuration ...\n"
        "conf_ldif=%s/confbak/`date +%%Y_%%m_%%d_%%H%%M%%S`.ldif\n"
        "./ns-slapd db2ldif -N -D %s "
        "-s \"%s\" -a $conf_ldif -n NetscapeRoot 2>&1\n"
        "if [ \"$?\" -ge 1 ] \nthen\n"
        "    echo Error occurred while saving configuration\n"
        "    exit 1\n"
        "fi\n"
        "exit 0\n",
        server, cs_path, cs_path, cf->netscaperoot);
    if(t) return t;
    
    t = gen_script(cs_path, "restoreconfig", 
        "cd %s\n"
        "conf_ldif=`ls -1t %s/confbak/*.ldif | head -1`\n"
        "if [ -z \"$conf_ldif\" ]\n"
        "then\n"
        "    echo No configuration to restore in %s/confbak ; exit 1\n"
        "fi\n"
        "echo Restoring $conf_ldif\n"
        "./ns-slapd ldif2db -D %s"
        " -i $conf_ldif -n NetscapeRoot 2>&1\n"
        "exit $?\n",
        server, cs_path, cs_path, cs_path);
    if(t) return t;
    
    t = gen_script(cs_path, "ldif2db", 
        "cd %s\n"
        "if [ $# -lt 4 ]\nthen\n"
        "\techo \"Usage: ldif2db -n backend_instance | {-s includesuffix}* [{-x excludesuffix}*]\"\n"
        "\techo \"               {-i ldiffile}* [-O]\"\n"
        "\techo \"Note: either \\\"-n backend_instance\\\" or \\\"-s includesuffix\\\" and \\\"-i ldiffile\\\" are required.\"\n"
        "\texit 1\n"
        "fi\n\n"
        "echo importing data ...\n"
        "./ns-slapd ldif2db -D %s \"$@\" 2>&1\n"
        "exit $?\n",
        server, cs_path);
    if(t) return t;

#if defined(UPGRADEDB)
    t = gen_script(cs_path, "upgradedb", 
        "cd %s\n"
        "if [ \"$#\" -eq 1 ]\nthen\n"
        "\tbak_dir=$1\nelse\n"
        "\tbak_dir=%s/bak/upgradedb_`date +%%Y_%%m_%%d_%%H_%%M_%%S`\nfi\n\n"
        "echo upgrade index files ...\n"
        "./ns-slapd upgradedb -D %s -a $bak_dir\n",
        server, cs_path, cs_path);
    if(t) return t;
#endif

    /* new code for dsml import */
    t = gen_script(cs_path, "dsml2db", 
        "cd %s\n"
        "if [ $# -lt 4 ]\nthen\n"
        "\techo \"Usage: dsml2db -n backend_instance | {-s includesuffix}* [{-x excludesuffix}*]\"\n"
        "\techo \"               {-i dsmlfile}\"\n"
        "\techo \"Note: either \\\"-n backend_instance\\\" or \\\"-s includesuffix\\\" and \\\"-i dsmlfile\\\" are required.\"\n"
        "\texit 1\n"
        "fi\n\n"
        "set_dsml=0\n"
        "dsml_file=\"mydummy\"\n"                
        "space=\" \"\n"
        "i=0\n"
        "for arg in \"$@\"\ndo\n"
        "\tif [ \"$arg\" = '-i' ];\n\tthen\n"
        "\t\tset_dsml=1\n"
        "\telif [ $set_dsml -eq 1 ];\n\tthen\n"
        "\t\tdsml_file=$arg\n"
        "\t\tset_dsml=2\n"
        "\telse\n"                
        "\t\teval a$i=\\\"$arg\\\"\n"
        "\t\ti=`expr $i + 1`\n"
        "\tfi\n"
        "done\n"
        "max=$i; i=0;\n"
        "shift $#\n"
        "while [ $i -lt $max ]; do\n"
        "\teval arg=\\$a$i\n"
        "\tset -- \"$@\" \"$arg\"\n"
        "\ti=`expr $i + 1`\n"
        "done\n"
        "\tif [ $dsml_file = \"mydummy\" ]\n\tthen\n\t"
        "echo \"Need a DSML file as input\""
        "\n\t\t exit 1"
        "\n\tfi\n"
        "\tif [ -f $dsml_file ] && [ -r $dsml_file ]\n\tthen\n"
        "\t\t%s/bin/base/jre/bin/java -Dverify=true -classpath %s/java/jars/crimson.jar:%s/java/ldapjdk.jar:%s/java/jars/xmltools.jar com.netscape.xmltools.DSML2LDIF $dsml_file\n"
        "\t\tif [ $? = 0 ]; then\n"
        "\t\techo importing data ...\n"
        "\t\t%s/bin/base/jre/bin/java -classpath %s/java/jars/crimson.jar:%s/java/ldapjdk.jar:%s/java/jars/xmltools.jar com.netscape.xmltools.DSML2LDIF $dsml_file | ./ns-slapd ldif2db -D %s \"$@\" -i -\n"
        "\t\texit $?\n"
        "\t\tfi\n"
        "\telse\n"
        "\t\techo \"File $dsml_file invalid. Absolute path is required.\"\n\t\texit 1\n"        
        "\tfi\n",
        server,sroot,sroot,sroot,sroot,sroot,sroot,sroot,sroot,cs_path);
    if(t) return t;
        
    t = gen_script(cs_path, "ldif2ldap", 
           "cd %s\n"
           "./ldapmodify -a -p %s -D \"$1\" -w \"$2\" -f $3\n",
           tools, cf->servport);
    if(t) return t;
    
    t = CREATE_LDIF2DB();
    if(t) return t;

    t = CREATE_DB2INDEX();
    if(t) return t;
/*
    t = CREATE_MIGRATETO5();
    if(t) return t;
    
    t = CREATE_MIGRATE50TO51();
    if(t) return t;

    t = CREATE_MIGRATEINSTANCE5();
    if(t) return t;

    t = CREATE_MIGRATE5TO6();
    if(t) return t;

    t = CREATE_MIGRATEINSTANCE6();
    if(t) return t;

    t = CREATE_MIGRATETO6();
    if(t) return t;
*/    

    t = CREATE_MIGRATE5TO7();
    if(t) return t;

    t = CREATE_MIGRATE6TO7();
    if(t) return t;

    t = CREATE_MIGRATEINSTANCE7();
    if(t) return t;

    t = CREATE_MIGRATETO7();
    if(t) return t;

    t = gen_script(cs_path, "getpwenc", 
           "cd %s\n"
           "PATH=%s:$PATH;export PATH\n"
           "if [ $# -lt 2 ]\n"
           "then\n"
           "\techo \"Usage: getpwenc scheme passwd\"\n"
           "\texit 1\n"
           "fi\n\n"
           "pwdhash -D %s -H -s \"$@\"\n",
           server, server, cs_path);
    if(t) return t;
    
    t = gen_script(cs_path, "db2ldif", 
           "cd %s\n"
           "if [ \"$#\" -lt 2 ];\nthen\n"
           "\techo \"Usage: db2ldif {-n backend_instance}* | {-s includesuffix}*\"\n"
           "\techo \"               [{-x excludesuffix}*] [-a outputfile]\"\n"
           "\techo \"               [-N] [-r] [-C] [-u] [-U] [-m] [-M] [-1]\"\n"
           "\techo \"Note: either \\\"-n backend_instance\\\" or \\\"-s includesuffix\\\" is required.\"\n"
           "\texit 1\n"
           "fi\n\n"
           "set_ldif=0\n"
           "ldif_file=\"mydummy\"\n"
           "for arg in \"$@\"\ndo\n"
           "\tif [ \"$arg\" = '-a' ];\n\tthen\n"
           "\t\tset_ldif=1\n"
           "\telif [ $set_ldif -eq 1 ];\n\tthen\n"
           "\t\tldif_file=$arg\n"
           "\t\tset_ldif=2\n"
           "\tfi\n"
           "done\n"
           "if [ $ldif_file = \"mydummy\" ]\nthen\n"
           "\tldif_file=%s/ldif/`date +%%Y_%%m_%%d_%%H%%M%%S`.ldif\nfi\n"
           "if [ $set_ldif -eq 2 ]\nthen\n"
           "./ns-slapd db2ldif -D %s \"$@\"\nelse\n"
           "./ns-slapd db2ldif -D %s -a $ldif_file \"$@\"\nfi\n",
           server, cs_path, cs_path, cs_path);
    if(t) return t;

    /* new code for dsml export */
    t = gen_script(cs_path, "db2dsml", 
           "cd %s\n"
           "if [ \"$#\" -lt 2 ];\nthen\n"
           "\techo \"Usage: db2dsml {-n backend_instance} | {-s includesuffix}*\"\n"
           "\techo \"               [{-x excludesuffix}*] [-a outputfile]\"\n"
           "\techo \"               [-u]\"\n"
           "\techo \"Note: either \\\"-n backend_instance\\\" or \\\"-s includesuffix\\\" is required.\"\n"
           "\texit 1\n"
           "fi\n\n"
           "set_dsml=0\n"
           "dsml_file=\"mydummy\"\n"
           "arg_list=\"\"\n"
           "space=\" \"\n"
           "for arg in \"$@\"\ndo\n"
           "\tif [ \"$arg\" = '-a' ];\n\tthen\n"
           "\t\tset_dsml=1\n"
           "\telif [ $set_dsml -eq 1 ];\n\tthen\n"
           "\t\tdsml_file=$arg\n"
           "\t\tset_dsml=2\n"
           "\telse\n"           
           "\t\targ_list=$arg_list$space$arg\n"
           "\tfi\n"
           "done\n"           
           "if [ $dsml_file = \"mydummy\" ]\nthen\n"
           "\tdsml_file=%s/dsml/`date +%%Y_%%m_%%d_%%H%%M%%S`.dsml\n"
           "\techo dsmlfile: $dsml_file\n"
           "fi\n"
           "%s/bin/base/jre/bin/java -Dverify=true -classpath %s/java/ldapjdk.jar:%s/java/jars/xmltools.jar com.netscape.xmltools.LDIF2DSML -s -o $dsml_file \n"
           "if [ $? = 0 ]; then\n"
           "\t./ns-slapd db2ldif -D %s \"$@\" -a - | %s/bin/base/jre/bin/java -classpath %s/java/ldapjdk.jar:%s/java/jars/xmltools.jar com.netscape.xmltools.LDIF2DSML -s -o $dsml_file \n"
           "fi\n",
            server, cs_path, sroot, sroot, sroot, cs_path, sroot, sroot, sroot);
    if(t) return t;

    t = CREATE_DB2LDIF();
    if(t) return t;

#if defined(UPGRADEDB)
    t = gen_script(cs_path, "db2index", 
           "cd %s\n"
           "if [ $# -eq 0 ]\n"
           "then\n"
           "\tbak_dir=%s/bak/reindex_`date +%%Y_%%m_%%d_%%H_%%M_%%S`\n"
           "\t./ns-slapd upgradedb -D %s -f -a \"$bak_dir\"\n"
           "elif [ $# -lt 4 ]\n"
           "then\n"
           "\techo \"Usage: db2index [-n backend_instance | {-s includesuffix}* -t attribute[:indextypes[:matchingrules]] -T vlvattribute]\"\n"
           "\texit 1\n"
           "else\n"
           "\t./ns-slapd db2index -D %s \"$@\"\n"
           "fi\n\n",
           server, cs_path, cs_path, cs_path);
    if(t) return t;
#endif

    t = gen_script(cs_path, "vlvindex", 
           "cd %s\n"
           "if [ $# -lt 4 ]\n"
           "then\n"
           "\techo \"Usage: vlvindex -n backend_instance | {-s includesuffix}* -T attribute\"\n"
           "\techo Note: either \\\"-n backend_instance\\\" or \\\"-s includesuffix\\\" are required.\n"
           "\texit 1\n"
           "fi\n\n"
           "./ns-slapd db2index -D %s \"$@\"\n",
           server, cs_path);
    if(t) return t;

    t = gen_script(cs_path, "db2bak", 
           "cd %s\n"
           "if [ \"$#\" -eq 1 ]\nthen\n"
           "\tbak_dir=$1\nelse\n"
           "\tbak_dir=%s/bak/`date +%%Y_%%m_%%d_%%H_%%M_%%S`\nfi\n\n"
           "./ns-slapd db2archive -D %s -a $bak_dir\n",
           server, cs_path, cs_path);
    if(t) return t;

    t = CREATE_DB2BAK();
    if(t) return t;
    
    t = gen_script(cs_path, "bak2db", 
           "if [ $# -lt 1 ] || [ $# -gt 3 ]\nthen\n"
           "    echo \"Usage: bak2db archivedir [-n backendname]\"\n"
           "    exit 1\n"
           "else\n"
           "	archivedir=$1\n"
           "	shift\n"
           "fi\n"
           "while getopts \"n:\" flag\ndo\n"
           "	case $flag in\n"
           "		n) bename=$OPTARG;;\n"
           "		*) echo \"Usage: bak2db archivedir [-n backendname]\"; exit 2;;\n"
           "	esac\n"
           "done\n\n"
           "if [ 1 = `expr $archivedir : \"\\/\"` ]\nthen\n"
           "    archivedir=$archivedir\n"
           "else\n"
           "    # relative\n"
           "    archivedir=`pwd`/$archivedir\nfi\n\n"
           "cd %s\n"
           "if [ \"$#\" -eq 2 ]\nthen\n"
           "    ./ns-slapd archive2db -D %s -a $archivedir -n $bename\n"
           "else\n"
           "    ./ns-slapd archive2db -D %s -a $archivedir\n"		   
           "fi\n",
           server, cs_path, cs_path);
    if(t) return t;

    t = CREATE_BAK2DB();
    if(t) return t;

    t = CREATE_VERIFYDB();
    if(t) return t;

    t = CREATE_REPL_MONITOR_CGI();
    if(t) return t;

    t = CREATE_ACCOUNT_INACT("ns-inactivate.pl");
    if(t) return t;

    t = CREATE_ACCOUNT_INACT("ns-activate.pl");
    if(t) return t;

    t = CREATE_ACCOUNT_INACT("ns-accountstatus.pl");
    if(t) return t;

    t = CREATE_DSML();
    if(t) return t;

    t = CREATE_NEWPWPOLICY();
    if(t) return t;

    t = gen_script(cs_path, "suffix2instance",
           "cd %s\n"
           "if [ $# -lt 2 ]\n"
           "then\n"
           "\techo Usage: suffix2instance {-s includesuffix}*\n"
           "\texit 1\n"
           "fi\n\n"
           "./ns-slapd suffix2instance -D %s \"$@\" 2>&1\n",
           server, cs_path);
    if(t) return t;

    /*Generate the java commandline tools in bin/slapd/server*/
    for (cls = 0; cls < 7; cls++) {
        t = gen_script(server, cl_scripts[cls], 
             "cd %s\n\n"
             "lang=${LANG:=en}\n"
             "while [ $# -ge 1 ]\n"
             "do\n"
             "    if [ $1 = '-l' ]\n"
             "    then\n"
             "        shift\n"
             "        lang=$1\n"
             "    else\n"
             "        arg=\"$arg $1\"\n"
             "    fi\n"
             "    shift\n"
             "done\n"
             "./bin/base/jre/bin/jre -classpath ./bin/base/jre/lib:"
             "./bin/base/jre/lib/rt.jar:./bin/base/jre/lib/i18n.jar:"
             "./java/base.jar:./java/jars/ds40.jar:./java/jars/ds40_${lang}.jar:"
             "./java/swingall.jar:./java/ssl.zip:"
             "./java/ldapjdk.jar:./java/mcc40.jar:./java/mcc40_${lang}.jar:"
             "./java/nmclf40.jar:./java/nmclf40_${lang}.jar"
             " com.netscape.admin.dirserv.cmdln.%s $arg\n", 
             sroot, cl_javafiles[cls]);
        if(t) return t;
    }



    return (t);
}
#else
char *ds_gen_scripts(char *sroot, server_config_s *cf, char *cs_path)
{
    char *t = NULL;
    char server[PATH_SIZE], admin[PATH_SIZE], tools[PATH_SIZE];
    char cgics_path[PATH_SIZE];
    char *cl_scripts[7] = {"dsstop.bat", "dsstart.bat", "dsrestart.bat", "dsrestore.bat", "dsbackup.bat", "dsimport.bat", "dsexport.bat"};
    char *cl_javafiles[7] = {"DSStop", "DSStart", "DSRestart", "DSRestore", "DSBackup", "DSImport", "DSExport"};
    int  cls = 0; /*Index into commandline script names and java names - RJP*/
    char *mysroot, *mycs_path;

    {
        char *p, *q;
        int n;

        for (n = 0, p = sroot; p = strchr(p, '/'); n++, p++) ;
        for (p = sroot; p = strchr(p, '\\'); n++, p++) ;
        mysroot = (char *)malloc(strlen(sroot) + n + 1);
        for (p = sroot, q = mysroot; *p; p++, q++) {
            if ('/' == *p || '\\' == *p) {
                *q++ = '\\';
                *q = '\\';
            } else
                *q = *p;
        }
        *q = '\0';

        for (n = 0, p = cs_path; p = strchr(p, '/'); n++, p++) ;
        for (p = cs_path; p = strchr(p, '\\'); n++, p++) ;
        mycs_path = (char *)malloc(strlen(cs_path) + n + 1);
        for (p = cs_path, q = mycs_path; *p; p++, q++) {
            if ('/' == *p || '\\' == *p) {
                *q++ = '\\';
                *q = '\\';
            } else
                *q = *p;
        }
        *q = '\0';
    }

    PR_snprintf(server, sizeof(server), "%s/bin/"PRODUCT_NAME"/server", sroot);    
    PR_snprintf(admin, sizeof(admin), "%s/bin/"PRODUCT_NAME"/admin/bin", sroot);    
    PR_snprintf(tools, sizeof(tools), "%s/shared/bin", sroot);    
    PR_snprintf(cgics_path, sizeof(cgics_path), "%s/bin/admin/admin/bin", sroot);

    ds_unixtodospath( cs_path );
    ds_unixtodospath( server );
    ds_unixtodospath( admin );
    ds_unixtodospath( sroot );
    ds_unixtodospath( tools );
    ds_unixtodospath( cgics_path );

    t = gen_script(cs_path, "monitor.bat", 
           "@echo off\n"
           "setlocal\n"
           "set rc=0\n"
           "if %%1.==. goto noparam\n"
           "\"%s\\ldapsearch\" -p %s -b %%1 "
           "-s base \"objectClass=*\"\n"
           "set rc=%%errorlevel%%\n"
           "goto proceed\n"
           ":noparam\n"
           "\"%s\\ldapsearch\" -p %s -b \"cn=monitor\" "
           "-s base \"objectClass=*\"\n"
           "set rc=%%errorlevel%%\n"
           ":proceed\n"
           "if defined MKSARGS exit %%rc%%\n"
           "exit /b %%rc%%\n",
           tools, cf->servport, tools, cf->servport);
    if(t) return t;
    
    t = gen_script(cs_path, "saveconfig.bat", 
           "@echo off\n"
           "setlocal\n"
           "set rc=0\n"
           "PATH=\"%s\";%%PATH%%\n"
           "namegen\n"
           "call bstart\n"
           "set config_ldif=%s\\confbak\\%%DATESTR%%.ldif\n"
           "call bend\n"
           "del bend.bat\n"
           "slapd db2ldif -s \"%s\" -a \"%%config_ldif%%\" -N"
           " -D \"%s\" -n NetscapeRoot 2>&1\n"
           "set rc=%%errorlevel%%\n"
           "if %%rc%%==0 goto done\n"
           "echo Error occurred while saving configuration\n"
        ":done\n"
           "if defined MKSARGS exit %%rc%%\n"
           "exit /b %%rc%%\n",
           server, cs_path, cf->netscaperoot, cs_path);
    if(t) return t;
    
    t = gen_script(cs_path, "restoreconfig.bat", 
           "@echo off\n"
           "setlocal\n"
           "set rc=0\n"
           "PATH=\"%s\";%%PATH%%\n"
           "set latestscript=%s\\latest_config.bat\n"
           "if EXIST \"%%latestscript%%\" del \"%%latestscript%%\"\n"
           "latest_file \"%s\\confbak\\*.ldif\" \"%%latestscript%%\"\n"
           "if not EXIST \"%%latestscript%%\" goto noconfig\n"
           "call \"%%latestscript%%\"\n"
           "del \"%%latestscript%%\"\n"
           "slapd ldif2db -D \"%s\" -i \"%%LATEST_FILE%%\""
           " -n NetscapeRoot 2>&1\n"
           "set rc=%%errorlevel%%\n"
           "if %%rc%%==0 goto done\n"
           "echo Error occurred while saving configuration\n"
           "goto done\n"
        ":noconfig\n"
           "set rc=0\n" /* no error */
           "echo No configuration to restore in %s\\confbak\n"
        ":done\n"
           "if defined MKSARGS exit %%rc%%\n"
           "exit /b %%rc%%\n",
           server, cs_path, cs_path, cs_path, cs_path);
    if(t) return t;
    
    t = gen_script(cs_path, "ldif2db.bat", 
                "@if not \"%%echo%%\" == \"on\" echo off\n"
                "setlocal\n"
                "set rc=0\n"
                   "PATH=\"%s\";%%PATH%%\n\n"
                "set noconfig=0\n"
                "if [%%2] == [] goto incorrect\n"
                "if [%%3] == [] goto incorrect\n"
                "if [%%4] == [] goto incorrect\n\n"
                "set args=\n"
                ":getargs\n"
                "if [%%1] == [] goto import\n"
                "set args=%%args%% %%1\n"
                "shift\n"
                "goto getargs\n\n"
                ":incorrect\n"
                ":usage\n"
                "echo \"Usage: ldif2db -n backend_instance | {-s \"includesuffix\"}* "
                "{-i ldif-file}* [-O] [{-x \"excludesuffix\"}*]\"\n"
                "set rc=1\n"
                "goto done\n\n"
                ":import\n"
                "echo importing data ...\n"
                   "slapd ldif2db -D \"%s\" %%args%% 2>&1\n\n"
                "set rc=%%errorlevel%%\n"
                ":done\n"
                "if defined MKSARGS exit %%rc%%\n"
                "exit /b %%rc%%\n",
                   server, cs_path);
    if(t) return t;

    /* new code for dsml import */
    t = gen_script(cs_path, "dsml2db.bat", 
                "@if not \"%%echo%%\" == \"on\" echo off\n"
                "setlocal\n"
                "set rc=0\n"
                   "PATH=\"%s\";%%PATH%%\n\n"
                "set noconfig=0\n"
                "if [%%2] == [] goto incorrect\n"
                "if [%%3] == [] goto incorrect\n"
                "if [%%4] == [] goto incorrect\n\n"
                "set args=\n"
                "goto getargs\n"
                ":setdsml\n"
                "set dsmlfile=\n"
                "set dsmlfile=%%2\n"
                "shift\n"
                "shift\n"
                "goto getargs\n"
                ":getargs\n"
                "if [%%1] == [] goto import\n"
                "if [%%1] == [-i] goto setdsml\n"
                "set args=%%args%% %%1\n"
                "shift\n"
                "goto getargs\n\n"
                ":incorrect\n"
                ":usage\n"
                "echo \"Usage: dsml2db -n backend_instance | {-s \"includesuffix\"}* "
                "{-i dsml-file} [{-x \"excludesuffix\"}*]\"\n"
                "set rc=1\n"
                "goto done\n\n"
                ":import\n"
                "%s\\bin\\base\\jre\\bin\\java -Dverify=true -classpath \".;%s\\java\\ldapjdk.jar;%s\\java\\jars\\crimson.jar;%s\\java\\jars\\xmltools.jar\" com.netscape.xmltools.DSML2LDIF %%dsmlfile%%\n"
                "set rc=%%errorlevel%%\n"
                "if %%rc%%==0 goto realimport else goto done\n"
                ":realimport\n"
                "echo importing data ...\n"
                   "%s\\bin\\base\\jre\\bin\\java -classpath \".;%s\\java\\ldapjdk.jar;%s\\java\\jars\\crimson.jar;%s\\java\\jars\\xmltools.jar\" com.netscape.xmltools.DSML2LDIF %%dsmlfile%% | slapd ldif2db -D \"%s\" -i - %%args%% 2>&1\n\n"
                "set rc=%%errorlevel%%\n"
                ":done\n"
                "if defined MKSARGS exit %%rc%%\n"
                "exit /b %%rc%%\n",
                   server, sroot, sroot, sroot, sroot, sroot, sroot, sroot, sroot, cs_path);
    if(t) return t;
    
    t = gen_script(cs_path, "ldif2ldap.bat", 
           "@echo off\n"
           "\"%s\\ldapmodify\" -a -p %s -D %%1 -w %%2 -f %%3\n",
           tools, cf->servport);
    if(t) return t;

    t = CREATE_LDIF2DB();
    if(t) return t;
    
    t = CREATE_DB2INDEX();
    if(t) return t;

/*    
    t = CREATE_MIGRATETO5();
    if(t) return t;
    
    t = CREATE_MIGRATE50TO51();
    if(t) return t;

    t = CREATE_MIGRATEINSTANCE5();
    if(t) return t;

    t = CREATE_MIGRATETO6();
    if(t) return t;
    
    t = CREATE_MIGRATE5TO6();
    if(t) return t;

    t = CREATE_MIGRATEINSTANCE6();
    if(t) return t;
*/
    t = CREATE_MIGRATE5TO7();
    if(t) return t;

    t = CREATE_MIGRATE6TO7();
    if(t) return t;

    t = CREATE_MIGRATEINSTANCE7();
    if(t) return t;

    t = CREATE_MIGRATETO7();
    if(t) return t;

    t = gen_script(cs_path, "getpwenc.bat", 
           "@echo off\n"
           "\"%s\\pwdhash\" -D \"%s\" -H -s %%1 %%2\n",
           server, cs_path);
    if(t) return t;
    
    t = gen_script(cs_path, "db2ldif.bat", 
            "@if not \"%%echo%%\" == \"on\" echo off\n\n"
            "setlocal\n"
            "set rc=0\n"
            "PATH=\"%s\";%%PATH%%\n\n"
            "if [%%2] == [] goto err\n\n"
            "set arg=\n"
            "set ldif_file=\n\n"
            ":again\n"
            "if \"%%1\" == \"\" goto next\n"
            "if \"%%1\" == \"-n\" goto doubletag\n"
            "if \"%%1\" == \"-s\" goto doubletag\n"
            "if \"%%1\" == \"-x\" goto doubletag\n"
            "if \"%%1\" == \"-a\" goto setldif\n"
            "if \"%%1\" == \"-N\" goto singletag\n"
            "if \"%%1\" == \"-r\" goto singletag\n"
            "if \"%%1\" == \"-C\" goto singletag\n"
            "if \"%%1\" == \"-u\" goto singletag\n"
            "if \"%%1\" == \"-m\" goto singletag\n"
            "if \"%%1\" == \"-o\" goto singletag\n"
            "if \"%%1\" == \"-U\" goto singletag\n"
            "if \"%%1\" == \"-M\" goto singletag\n"
            "if \"%%1\" == \"-E\" goto singletag\n"
            "goto next\n\n"
            ":doubletag\n"
            "set arg=%%1 %%2 %%arg%%\n"
            "shift\n"
            "shift\n"
            "goto again\n\n"
            ":singletag\n"
            "set arg=%%1 %%arg%%\n"
            "shift\n"
            "goto again\n\n"
            ":setldif\n"
            "set ldif_file=%%2\n"
            "shift\n"
            "shift\n"
            "goto again\n\n"
            ":next\n"
            "if not \"%%ldif_file%%\" == \"\" goto givenldif\n\n"
            "namegen\n"
            "call bstart\n"
            "set ldif_file=\"%s\\ldif\\%%DATESTR%%.ldif\"\n"
            "call bend\n"
            "del bend.bat\n\n"
            ":givenldif\n"
            "\"%s\\slapd\" db2ldif -D \"%s\" -a %%ldif_file%% %%arg%%\n"
            "set rc=%%errorlevel%%\n"
            "goto done\n\n"
            ":err\n"
            "echo \"Usage: db2ldif -n backend_instance | "
            "{-s \"includesuffix\"}* [{-x \"excludesuffix\"}*] [-N] [-r] [-C] "
            "[-u] [-U] [-m] [-M] [-1] [-a outputfile]\"\n\n"
            "set rc=1\n"
            ":done\n"
            "if defined MKSARGS exit %%rc%%\n"
            "exit /b %%rc%%\n",
                server, cs_path, server, cs_path);
    if(t) return t;

    t = CREATE_DB2LDIF();
    if(t) return t;

    /* new code for dsml export */
    t = gen_script(cs_path, "db2dsml.bat", 
            "@if not \"%%echo%%\" == \"on\" echo off\n\n"
            "setlocal\n"
            "set rc=0\n"
            "PATH=\"%s\";%%PATH%%\n\n"
            "if [%%2] == [] goto err\n\n"
            "set arg=\n"
            "set dsml_file=\n\n"
            ":again\n"
            "if \"%%1\" == \"\" goto next\n"
            "if \"%%1\" == \"-n\" goto doubletag\n"
            "if \"%%1\" == \"-s\" goto doubletag\n"
            "if \"%%1\" == \"-x\" goto doubletag\n"
            "if \"%%1\" == \"-a\" goto setdsml\n"
            "if \"%%1\" == \"-N\" goto singletag\n"
            "if \"%%1\" == \"-r\" goto singletag\n"
            "if \"%%1\" == \"-C\" goto singletag\n"
            "if \"%%1\" == \"-u\" goto singletag\n"
            "if \"%%1\" == \"-m\" goto singletag\n"
            "if \"%%1\" == \"-o\" goto singletag\n"
            "if \"%%1\" == \"-U\" goto singletag\n"
            "if \"%%1\" == \"-M\" goto singletag\n"
            "goto next\n\n"
            ":doubletag\n"
            "set arg=%%1 %%2 %%arg%%\n"
            "shift\n"
            "shift\n"
            "goto again\n\n"
            ":singletag\n"
            "set arg=%%1 %%arg%%\n"
            "shift\n"
            "goto again\n\n"
            ":setdsml\n"
            "set dsml_file=%%2\n"
            "shift\n"
            "shift\n"
            "goto again\n\n"
            ":next\n"
            "if not \"%%dsml_file%%\" == \"\" goto givendsml\n\n"
            "namegen\n"
            "call bstart\n"
            "set dsml_file=\"%s\\dsml\\%%DATESTR%%.dsml\"\n"
            "echo dsmlfile: %%dsml_file%%\n"
            "call bend\n"
            "del bend.bat\n\n"
            ":givendsml\n"
            "%s\\bin\\base\\jre\\bin\\java -Dverify=true -classpath \".;%s\\java\\ldapjdk.jar;%s\\java\\jars\\xmltools.jar\" com.netscape.xmltools.LDIF2DSML -s -o %%dsml_file%%\n"
            "set rc=%%errorlevel%%\n"
            "if %%rc%%==0 goto realimport else goto done\n\n"
            ":realimport\n"
            "\"%s\\slapd\" db2ldif -D \"%s\" -a - -1 %%arg%% | %s\\bin\\base\\jre\\bin\\java -classpath \".;%s\\java\\ldapjdk.jar;%s\\java\\jars\\xmltools.jar\" com.netscape.xmltools.LDIF2DSML -s -o %%dsml_file%%\n"
            "set rc=%%errorlevel%%\n"
            "goto done\n\n"
            ":err\n"
            "echo \"Usage: db2dsml -n backend_instance | "
            "{-s \"includesuffix\"}* [{-x \"excludesuffix\"}*]"
            "[-u] [-a outputfile]\"\n\n"
            "set rc=1\n"
            ":done\n"
            "if defined MKSARGS exit %%rc%%\n"
            "exit /b %%rc%%\n",
                server, cs_path, sroot, sroot, sroot, server, cs_path, sroot, sroot, sroot);
    if(t) return t;  
        
    t = gen_script(cs_path, "db2bak.bat", 
           "@echo off\n"
           "setlocal\n"
           "set rc=0\n"
           "PATH=\"%s\";%%PATH%%\n"
           "if %%1.==. goto nobak\n"
           "set bakdir=%%1\n"
           "goto backup\n"
           ":nobak\n"
           "namegen\n"
           "call bstart\n"
           "set bakdir=\"%s\\bak\\%%DATESTR%%\"\n"
           "call bend\n"
           "del bend.bat\n"
           ":backup\n"
           "\"%s\\slapd\" db2archive -D \"%s\" -a %%bakdir%% "
           "%%2 %%3 %%4 %%5 %%6 %%7 %%8\n"
           "set rc=%%errorlevel%%\n"
           ":done\n"
           "if defined MKSARGS exit %%rc%%\n"
           "exit /b %%rc%%\n",
           server, cs_path, server, cs_path);
    if(t) return t;

    t = CREATE_DB2BAK();
    if(t) return t;
    
#if defined(UPGRADEDB)
    t = gen_script(cs_path, "db2index.bat", 
           "@echo off\n"
       "setlocal\n"
       "set rc=0\n"
           "PATH=\"%s\";%%PATH%%\n"
           "if %%1.==. goto indexall\n\n"
           "if %%2.==. goto err\n"
           "if %%3.==. goto err\n\n"
           "set bakdir=%%1\n"
           "goto backup\n\n"
           ":indexall\n"
           "namegen\n"
           "call bstart\n"
           "set bakdir=\"%s\\bak\\%%DATESTR%%\"\n"
           "call bend\n"
           "del bend.bat\n"
           "\"%s\\slapd\" upgradedb -D \"%s\" -f -a %%bakdir%%\n"
           "set rc=%%errorlevel%%\n"
           "goto done\n\n"
           ":backup\n"
           "\"%s\\slapd\" db2index -D \"%s\" " 
           "%%1 %%2 %%3 %%4 %%5 %%6 %%7 %%8\n"
           "set rc=%%errorlevel%%\n"
           "goto done\n\n"
           ":err\n"
           "echo \"Usage: db2index [-n backend_instance | {-s instancesuffix}* -t attribute[:indextypes[:matchingrules]] -T vlvattribute]\"\n\n"
           "set rc=1\n"
           ":done\n"
           "if defined MKSARGS exit %%rc%%\n"
           "exit /b %%rc%%\n",
           server, cs_path, server, cs_path, server, cs_path);
    if(t) return t;
#endif

    t = gen_script(cs_path, "vlvindex.bat", 
           "@echo off\n"
            "setlocal\n"
            "set rc=0\n"
            "if [%%2] == [] goto usage\n"
            "if [%%3] == [] goto usage\n"
            "if [%%4] == [] goto usage\n\n"
           "\"%s\\slapd\" db2index -D \"%s\" \"%%@\"\n"
            "set rc=%%errorlevel%%\n"
            "goto done\n\n"
            ":usage\n"
            "echo \"Usage: vlvindex -n backend_instance | {-s includesuffix}* {-T attribute}\"\n\n"
            "set rc=1\n"
            ":done\n"
            "if defined MKSARGS exit %%rc%%\n"
            "exit /b %%rc%%\n",
           server, cs_path);
    if(t) return t;
    
    t = gen_script(cs_path, "bak2db.bat", 
            "@echo off\n"
            "pushd & setlocal\n\n"
            "if [%%1] == [] (goto :usage)\n"
            "if not [%%4] == [] (goto :usage)\n\n"
            "set archivedir=%%1\n"
            "set rc=0\n\n"
            ":getopts\n"
            "shift\n"
            "if [%%1]==[] (goto :main)\n"
            "if [%%1]==[-n] (if not [%%2]==[] (set bename=%%2) else (goto :usage)) else (goto :getopts)\n\n"
            ":main\n"
            "call :relative %%archivedir%%\n"
            "if defined bename (\n"
            "\"%s\\slapd\" archive2db -D \"%s\" -a %%archivedir%% -n %%bename%%\n"
            ") else (\n"
            "\"%s\\slapd\" archive2db -D \"%s\" -a %%archivedir%%\n"
            ")\n"
            "set rc=%%ERRORLEVEL%%\n"
            "popd\n"
            "goto :done\n\n"
            "goto :EOF\n"
            ":usage\n"
            "echo %%0 archivedir [-n backendname]\n"
            "goto :done\n\n"
            "goto :EOF\n"
            ":relative\n"
            "set archivedir=%%~f1\n\n"
            "goto :EOF\n"
            ":done\n"
            "if defined MKSARGS exit %%rc%%\n"
            "exit /b %%rc%%\n",
            server, cs_path, server, cs_path);
    if(t) return t;

#if defined(UPGRADEDB)
    t = gen_script(cs_path, "upgradedb.bat", 
            "@echo off\n"
        "setlocal\n"
        "set rc=0\n"
            "PATH=\"%s\";%%PATH%%\n"
            "if %%1.==. goto nobak\n"
            "set bakdir=%%1\n"
            "goto backup\n"
            ":nobak\n"
            "namegen\n"
            "call bstart\n"
            "set bakdir=\"%s\\bak\\upgradedb_%%DATESTR%%\"\n"
            "call bend\n"
            "del bend.bat\n"
            ":backup\n"
            "\"%s\\slapd\" upgradedb -D \"%s\" -a %%bakdir%% "
            "%%2 %%3 %%4 %%5 %%6 %%7 %%8\n"
            "set rc=%%errorlevel%%\n"
            ":done\n"
            "if defined MKSARGS exit %%rc%%\n"
            "exit /b %%rc%%\n",
            server, cs_path, server, cs_path);
    if(t) return t;
#endif

    t = CREATE_BAK2DB();
    if(t) return t;

    t = CREATE_VERIFYDB();
    if(t) return t;

    t = CREATE_REPL_MONITOR_CGI();
    if(t) return t;

    t = gen_script(cs_path, "suffix2instance.bat",
           "@if not \"%%echo%%\" == \"on\" echo off\n\n"
           "setlocal\n"
           "set rc=0\n"
           "PATH=\"%s\";%%PATH%%\n\n"
           "if [%%2] == [] goto err\n\n"
           "set arg=\n\n"
           ":again\n"
           "if \"%%1\" == \"\" goto next\n"
           "if \"%%1\" == \"-s\" goto doubletag\n"
           "shift\n"
           "goto again\n\n"
           ":doubletag\n"
           "set arg=%%1 %%2 %%arg%%\n"
           "shift\n"
           "shift\n"
           "goto again\n\n"
           ":next\n"
           "\"%s\\slapd\" suffix2instance -D \"%s\" %%arg%%\n"
           "set rc=%%errorlevel%%\n"
           "goto done\n\n"
           ":err\n"
           "echo Usage: suffix2instance {-s \"suffix\"}*\n\n"
           "set rc=1\n"
           ":done\n"
           "if defined MKSARGS exit %%rc%%\n"
           "exit /b %%rc%%\n",
           server, server, cs_path);
    if(t) return t;

    t = CREATE_ACCOUNT_INACT("ns-inactivate.pl");
    if(t) return t;

    t = CREATE_ACCOUNT_INACT("ns-activate.pl");
    if(t) return t;

    t = CREATE_ACCOUNT_INACT("ns-accountstatus.pl");
    if(t) return t;

    t = CREATE_DSML();
    if(t) return t;

    t = gen_script(cs_path, "dsml-activate.bat",
           "@echo off\n"
           "setlocal\n"
           "PATH=%s\\bin\\slapd\\admin\\bin;%%PATH%%\n"
           "perl \"%s\\dsml-activate.pl\" %%*\n"
           "set rc=%%errorlevel%%\n"
           "if defined MKSARGS exit %%rc%%\n"
           "exit /b %%rc%%\n",
           sroot, cs_path);
    if(t) return t;



    t = CREATE_NEWPWPOLICY();
    if(t) return t;

    t = gen_script(cs_path, "ns-newpwpolicy.cmd",
           "@echo off\n"
           "setlocal\n"
           "PATH=%s\\bin\\slapd\\admin\\bin;%%PATH%%\n"
           "perl \"%s\\ns-newpwpolicy.pl\" %%*\n"
           "set rc=%%errorlevel%%\n"
           "if defined MKSARGS exit %%rc%%\n"
           "exit /b %%rc%%\n",
           sroot, cs_path);
    if(t) return t;

    free(mysroot);
    free(mycs_path);

    /*Generate the java commandline tools in bin/slapd/server*/
    for (cls = 0; cls < 7; cls++) {
        t = gen_script(server, cl_scripts[cls], 
             "@echo off\npushd \"%s\"\n\n"
             "setlocal\n"
             "set LANG=en\n"
             "set arg=\n"
             "set rc=0\n"
             ":getarg\n"
             "if %%1.==. goto start\n"
             "if %%1==-l goto getlang\n"
             "set arg=%%arg%% %%1\n"
             "shift\n"
             "goto getarg\n"
             ":getlang\n"
             "shift\n"
             "set LANG=%%1\n"
             "shift\n"
             "goto getarg\n"
             ":start\n"
             ".\\bin\\base\\jre\\bin\\jre  -classpath "
             ".;.\\java;.\\bin\\base\\jre\\lib;"
             ".\\bin\\base\\jre\\lib\\rt.jar;.\\bin\\base\\jre\\lib\\i18n.jar;"
             ".\\java\\base.jar;.\\java\\jars\\ds40.jar;.\\java\\jars\\ds40_%%LANG%%.jar;"
             ".\\java\\swingall.jar;.\\java\\ssl.zip;"
             ".\\java\\ldapjdk.jar;.\\java\\mcc40.jar;.\\java\\mcc40_%%LANG%%.jar;"
             ".\\java\\nmclf40.jar;.\\java\\nmclf40_%%LANG%%.jar "
             "com.netscape.admin.dirserv.cmdln.%s  %%arg%%\n"
             "set rc=%%errorlevel%%\n"
             "popd\n"
             "if defined MKSARGS exit %%rc%%\n"
             "exit /b %%rc%%\n", 
             sroot, cl_javafiles[cls]);             
        if(t) return t;
    }



    return (t);
}
#endif


void
suffix_gen_conf(FILE* f, char * suffix, char *be_name)
{
    char* belowdn;
    
    fprintf(f, "dn: cn=%s,cn=ldbm database,cn=plugins,cn=config\n", be_name);
    fprintf(f, "objectclass: top\n");
    fprintf(f, "objectclass: extensibleObject\n");
    fprintf(f, "objectclass: nsBackendInstance\n");
    fprintf(f, "nsslapd-cachesize: -1\n");
    fprintf(f, "nsslapd-cachememsize: 10485760\n");
    fprintf(f, "nsslapd-suffix: %s\n", suffix);
    fprintf(f, "cn: %s\n", be_name);
    fprintf(f, "\n");

    fprintf(f, "dn: cn=monitor,cn=%s,cn=ldbm database,cn=plugins,cn=config\n", be_name);
    fprintf(f, "objectclass: top\n");
    fprintf(f, "objectclass: extensibleObject\n");
    fprintf(f, "cn: monitor\n");
    fprintf(f, "\n");

    fprintf(f, "dn: cn=\"%s\",cn=mapping tree,cn=config\n", suffix);
    fprintf(f, "objectclass: top\n");
    fprintf(f, "objectclass: extensibleObject\n");
    fprintf(f, "objectclass: nsMappingTree\n");
    fprintf(f, "cn: \"%s\"\n", suffix);
    fprintf(f, "nsslapd-state: backend\n");
    fprintf(f, "nsslapd-backend: %s\n", be_name);
    fprintf(f, "\n");

    /* Parent entry for attribute encryption config entries */

    fprintf(f, "dn: cn=encrypted attributes,cn=%s,cn=ldbm database,cn=plugins,cn=config\n", be_name);
    fprintf(f, "objectclass: top\n");
    fprintf(f, "objectclass: extensibleObject\n");
    fprintf(f, "cn: encrypted attributes\n");
    fprintf(f, "\n");

    /* Parent entry for attribute encryption keys */

    fprintf(f, "dn: cn=encrypted attribute keys,cn=%s,cn=ldbm database,cn=plugins,cn=config\n", be_name);
    fprintf(f, "objectclass: top\n");
    fprintf(f, "objectclass: extensibleObject\n");
    fprintf(f, "cn: encrypted attributes keys\n");
    fprintf(f, "\n");

    /* Indexes for the ldbm instance */

    fprintf(f, "dn: cn=index,cn=%s,cn=ldbm database,cn=plugins,cn=config\n", be_name);
    fprintf(f, "objectclass: top\n");
    fprintf(f, "objectclass: extensibleObject\n");
    fprintf(f, "cn: index\n");
    fprintf(f, "\n");

    belowdn = PR_smprintf("cn=index,cn=%s,cn=ldbm database,cn=plugins,cn=config", be_name);
    ds_gen_index(f, belowdn);
	PR_smprintf_free(belowdn);
    
    /* done with ldbm entries */
}

#define MKSYNTAX(_name,_fn) do { \
    fprintf(f, "dn: cn=%s,cn=plugins,cn=config\n", (_name)); \
    fprintf(f, "objectclass: top\n"); \
    fprintf(f, "objectclass: nsSlapdPlugin\n"); \
    fprintf(f, "objectclass: extensibleObject\n"); \
    fprintf(f, "cn: %s\n",(_name)); \
    fprintf(f, "nsslapd-pluginpath: %s/lib/syntax-plugin%s\n", sroot, shared_lib); \
    fprintf(f, "nsslapd-plugininitfunc: %s\n", (_fn)); \
    fprintf(f, "nsslapd-plugintype: syntax\n"); \
    fprintf(f, "nsslapd-pluginenabled: on\n"); \
    fprintf(f, "\n"); \
  } while (0)

char *ds_gen_confs(char *sroot, server_config_s *cf, 
    char *cs_path)
{
    char* t = NULL;
    char src[PATH_SIZE], dest[PATH_SIZE];
    char fn[PATH_SIZE], line[1024];
    FILE *f = 0, *srcf = 0;
    int  rootdse = 0;
    char *shared_lib;

    PR_snprintf(fn, sizeof(fn), "%s%cconfig%cdse.ldif", cs_path, FILE_PATHSEP, FILE_PATHSEP);
    if(!(f = fopen(fn, "w")))
        return make_error("Can't write to %s (%s)", fn, ds_system_errmsg());

#if defined( XP_WIN32 )
    shared_lib = ".dll";
#else
#ifdef HPUX
    shared_lib = ".sl";
#else
#ifdef AIX
#if OSVERSION >= 4200
    shared_lib = ".so";
#else
    shared_lib = "_shr.a";
#endif
#else
    shared_lib = ".so";
#endif
#endif
#endif

    fprintf(f, "dn: cn=config\n");
    fprintf(f, "cn: config\n");
    fprintf(f, "objectclass:top\n");
    fprintf(f, "objectclass:extensibleObject\n");
    fprintf(f, "objectclass:nsslapdConfig\n");
    fprintf(f, "nsslapd-accesslog-logging-enabled: on\n");
    fprintf(f, "nsslapd-accesslog-maxlogsperdir: 10\n");
    fprintf(f, "nsslapd-accesslog-mode: 600\n");
    fprintf(f, "nsslapd-accesslog-maxlogsize: 100\n");
    fprintf(f, "nsslapd-accesslog-logrotationtime: 1\n");
    fprintf(f, "nsslapd-accesslog-logrotationtimeunit: day\n");
    fprintf(f, "nsslapd-accesslog-logrotationsync-enabled: off\n");
    fprintf(f, "nsslapd-accesslog-logrotationsynchour: 0\n");
    fprintf(f, "nsslapd-accesslog-logrotationsyncmin: 0\n");
    fprintf(f, "nsslapd-accesslog: %s/logs/access\n", cs_path);
    fprintf(f, "nsslapd-enquote-sup-oc: off\n");
    fprintf(f, "nsslapd-localhost: %s\n", cf->servname);
    fprintf(f, "nsslapd-schemacheck: %s\n",
            (cf->disable_schema_checking && !strcmp(cf->disable_schema_checking, "1")) ? "off" : "on");
    fprintf(f, "nsslapd-rewrite-rfc1274: off\n");
    fprintf(f, "nsslapd-return-exact-case: on\n");
    fprintf(f, "nsslapd-ssl-check-hostname: on\n");
    fprintf(f, "nsslapd-port: %s\n", cf->servport);
#if !defined( XP_WIN32 )
    if (cf->servuser && *(cf->servuser)) {
        fprintf(f, "nsslapd-localuser: %s\n", cf->servuser);
    }
#endif
    fprintf(f, "nsslapd-errorlog-logging-enabled: on\n");
    fprintf(f, "nsslapd-errorlog-mode: 600\n");
    fprintf(f, "nsslapd-errorlog-maxlogsperdir: 2\n");
    fprintf(f, "nsslapd-errorlog-maxlogsize: 100\n");
    fprintf(f, "nsslapd-errorlog-logrotationtime: 1\n");
    fprintf(f, "nsslapd-errorlog-logrotationtimeunit: week\n");
    fprintf(f, "nsslapd-errorlog-logrotationsync-enabled: off\n");
    fprintf(f, "nsslapd-errorlog-logrotationsynchour: 0\n");
    fprintf(f, "nsslapd-errorlog-logrotationsyncmin: 0\n");
    fprintf(f, "nsslapd-errorlog: %s/logs/errors\n", cs_path);
    if (cf->loglevel)
        fprintf(f, "nsslapd-errorlog-level: %s\n", cf->loglevel);
    fprintf(f, "nsslapd-auditlog: %s/logs/audit\n", cs_path);
    fprintf(f, "nsslapd-auditlog-mode: 600\n");
    fprintf(f, "nsslapd-auditlog-maxlogsize: 100\n");
    fprintf(f, "nsslapd-auditlog-logrotationtime: 1\n");
    fprintf(f, "nsslapd-auditlog-logrotationtimeunit: day\n");
    fprintf(f, "nsslapd-rootdn: %s\n", cf->rootdn);
#if !defined(_WIN32) && !defined(AIX)
    {
        unsigned int maxdescriptors = FD_SETSIZE;
        struct rlimit rl;
        if (getrlimit(RLIMIT_NOFILE, &rl) == 0)
            maxdescriptors = (unsigned int)rl.rlim_max;
        fprintf(f, "nsslapd-maxdescriptors: %d\n", maxdescriptors);
    }
#endif
    fprintf(f, "nsslapd-max-filter-nest-level: 40\n" );
    fprintf(f, "nsslapd-rootpw: %s\n", cf->roothashedpw);
    if (getenv("DEBUG_SINGLE_THREADED"))
        fprintf(f, "nsslapd-threadnumber: 1\n");
    fprintf(f, "\n");

    fprintf(f, "dn: cn=plugins, cn=config\nobjectclass: top\nobjectclass: nsContainer\ncn: plugins\n");
    fprintf(f, "\n");

    fprintf(f, "dn: cn=Password Storage Schemes,cn=plugins, cn=config\n");
    fprintf(f, "objectclass: top\nobjectclass: nsContainer\ncn: Password Storage Schemes\n");
    fprintf(f, "\n");

    fprintf(f, "dn: cn=SSHA,cn=Password Storage Schemes,cn=plugins,cn=config\n");
    fprintf(f, "objectclass: top\n");
    fprintf(f, "objectclass: nsSlapdPlugin\n");
    fprintf(f, "cn: SSHA\n");
    fprintf(f, "nsslapd-pluginpath: %s/lib/pwdstorage-plugin%s\n", sroot, shared_lib);
    fprintf(f, "nsslapd-plugininitfunc: ssha_pwd_storage_scheme_init\n");
    fprintf(f, "nsslapd-plugintype: pwdstoragescheme\n");
    fprintf(f, "nsslapd-pluginenabled: on\n");
    fprintf(f, "\n");

    fprintf(f, "dn: cn=SHA,cn=Password Storage Schemes,cn=plugins,cn=config\n");
    fprintf(f, "objectclass: top\n");
    fprintf(f, "objectclass: nsSlapdPlugin\n");
    fprintf(f, "cn: SHA\n");
    fprintf(f, "nsslapd-pluginpath: %s/lib/pwdstorage-plugin%s\n", sroot, shared_lib);
    fprintf(f, "nsslapd-plugininitfunc: sha_pwd_storage_scheme_init\n");
    fprintf(f, "nsslapd-plugintype: pwdstoragescheme\n");
    fprintf(f, "nsslapd-pluginenabled: on\n");
    fprintf(f, "\n");

    fprintf(f, "dn: cn=CRYPT,cn=Password Storage Schemes,cn=plugins,cn=config\n");
    fprintf(f, "objectclass: top\n");
    fprintf(f, "objectclass: nsSlapdPlugin\n");
    fprintf(f, "cn: CRYPT\n");
    fprintf(f, "nsslapd-pluginpath: %s/lib/pwdstorage-plugin%s\n", sroot, shared_lib);
    fprintf(f, "nsslapd-plugininitfunc: crypt_pwd_storage_scheme_init\n");
    fprintf(f, "nsslapd-plugintype: pwdstoragescheme\n");
    fprintf(f, "nsslapd-pluginenabled: on\n");
    fprintf(f, "\n");

    fprintf(f, "dn: cn=CLEAR,cn=Password Storage Schemes,cn=plugins,cn=config\n");
    fprintf(f, "objectclass: top\n");
    fprintf(f, "objectclass: nsSlapdPlugin\n");
    fprintf(f, "cn: CLEAR\n");
    fprintf(f, "nsslapd-pluginpath: %s/lib/pwdstorage-plugin%s\n", sroot, shared_lib);
    fprintf(f, "nsslapd-plugininitfunc: clear_pwd_storage_scheme_init\n");
    fprintf(f, "nsslapd-plugintype: pwdstoragescheme\n");
    fprintf(f, "nsslapd-pluginenabled: on\n");
    fprintf(f, "\n");

    fprintf(f, "dn: cn=NS-MTA-MD5,cn=Password Storage Schemes,cn=plugins,cn=config\n");
    fprintf(f, "objectclass: top\n");
    fprintf(f, "objectclass: nsSlapdPlugin\n");
    fprintf(f, "cn: NS-MTA-MD5\n");
    fprintf(f, "nsslapd-pluginpath: %s/lib/pwdstorage-plugin%s\n", sroot, shared_lib);
    fprintf(f, "nsslapd-plugininitfunc: ns_mta_md5_pwd_storage_scheme_init\n");
    fprintf(f, "nsslapd-plugintype: pwdstoragescheme\n");
    fprintf(f, "nsslapd-pluginenabled: on\n");
    fprintf(f, "\n");

    fprintf(f, "dn: cn=DES,cn=Password Storage Schemes,cn=plugins,cn=config\n");
    fprintf(f, "objectclass: top\n");
    fprintf(f, "objectclass: nsSlapdPlugin\n");
    fprintf(f, "objectclass: extensibleObject\n");
    fprintf(f, "cn: DES\n");
    fprintf(f, "nsslapd-pluginpath: %s/lib/des-plugin%s\n", sroot, shared_lib);
    fprintf(f, "nsslapd-plugininitfunc: des_init\n");
    fprintf(f, "nsslapd-plugintype: reverpwdstoragescheme\n");
    fprintf(f, "nsslapd-pluginenabled: on\n");
    fprintf(f, "nsslapd-pluginarg0: nsmultiplexorcredentials\n");
    fprintf(f, "nsslapd-pluginarg1: nsds5ReplicaCredentials\n");
    fprintf(f, "nsslapd-pluginid: des-storage-scheme\n");
    fprintf(f, "\n");
    
    MKSYNTAX("Case Ignore String Syntax","cis_init");
    MKSYNTAX("Case Exact String Syntax","ces_init");
    MKSYNTAX("Space Insensitive String Syntax","sicis_init");
    MKSYNTAX("Binary Syntax","bin_init");
    MKSYNTAX("Octet String Syntax","octetstring_init");
    MKSYNTAX("Boolean Syntax","boolean_init");
    MKSYNTAX("Generalized Time Syntax","time_init");
    MKSYNTAX("Telephone Syntax","tel_init");
    MKSYNTAX("Integer Syntax","int_init");
    MKSYNTAX("Distinguished Name Syntax","dn_init");
    MKSYNTAX("OID Syntax","oid_init");
    MKSYNTAX("URI Syntax","uri_init");
    MKSYNTAX("JPEG Syntax","jpeg_init");
    MKSYNTAX("Country String Syntax","country_init");
    MKSYNTAX("Postal Address Syntax","postal_init");

    fprintf(f, "dn: cn=State Change Plugin,cn=plugins,cn=config\n");
    fprintf(f, "objectclass: top\n");
    fprintf(f, "objectclass: nsSlapdPlugin\n");
    fprintf(f, "objectclass: extensibleObject\n");
    fprintf(f, "cn: State Change Plugin\n");
    fprintf(f, "nsslapd-pluginpath: %s/lib/statechange-plugin%s\n", sroot, shared_lib);
    fprintf(f, "nsslapd-plugininitfunc: statechange_init\n");
    fprintf(f, "nsslapd-plugintype: postoperation\n");
    fprintf(f, "nsslapd-pluginenabled: on\n");
    fprintf(f, "\n");

    fprintf(f, "dn: cn=Roles Plugin,cn=plugins,cn=config\n");
    fprintf(f, "objectclass: top\n");
    fprintf(f, "objectclass: nsSlapdPlugin\n");
    fprintf(f, "objectclass: extensibleObject\n");
    fprintf(f, "cn: Roles Plugin\n");
    fprintf(f, "nsslapd-pluginpath: %s/lib/roles-plugin%s\n", sroot, shared_lib);
    fprintf(f, "nsslapd-plugininitfunc: roles_init\n");
     fprintf(f, "nsslapd-plugintype: postoperation\n");
    fprintf(f, "nsslapd-pluginenabled: on\n");
    fprintf(f, "nsslapd-plugin-depends-on-type: database\n");
    fprintf(f, "nsslapd-plugin-depends-on-named: State Change Plugin\n");
    fprintf(f, "nsslapd-plugin-depends-on-named: Views\n");
    fprintf(f, "\n");

    fprintf(f, "dn: cn=ACL Plugin,cn=plugins,cn=config\n");
    fprintf(f, "objectclass: top\n");
    fprintf(f, "objectclass: nsSlapdPlugin\n");
    fprintf(f, "objectclass: extensibleObject\n");
    fprintf(f, "cn: ACL Plugin\n");
    fprintf(f, "nsslapd-pluginpath: %s/lib/acl-plugin%s\n", sroot, shared_lib);
    fprintf(f, "nsslapd-plugininitfunc: acl_init\n");
    fprintf(f, "nsslapd-plugintype: accesscontrol\n");
    fprintf(f, "nsslapd-pluginenabled: on\n");
    fprintf(f, "nsslapd-plugin-depends-on-type: database\n");
    fprintf(f, "\n");

    fprintf(f, "dn: cn=ACL preoperation,cn=plugins,cn=config\n");
    fprintf(f, "objectclass: top\n");
    fprintf(f, "objectclass: nsSlapdPlugin\n");
    fprintf(f, "objectclass: extensibleObject\n");
    fprintf(f, "cn: ACL preoperation\n");
    fprintf(f, "nsslapd-pluginpath: %s/lib/acl-plugin%s\n", sroot, shared_lib);
    fprintf(f, "nsslapd-plugininitfunc: acl_preopInit\n");
    fprintf(f, "nsslapd-plugintype: preoperation\n");
    fprintf(f, "nsslapd-pluginenabled: on\n");
    fprintf(f, "nsslapd-plugin-depends-on-type: database\n");
    fprintf(f, "\n");

    fprintf(f, "dn: cn=Legacy Replication Plugin,cn=plugins,cn=config\n");
    fprintf(f, "objectclass: top\n");
    fprintf(f, "objectclass: nsSlapdPlugin\n");
    fprintf(f, "objectclass: extensibleObject\n");
    fprintf(f, "cn: Legacy Replication Plugin\n");
    fprintf(f, "nsslapd-pluginpath: %s/lib/replication-plugin%s\n", sroot, shared_lib);
    fprintf(f, "nsslapd-plugininitfunc: replication_legacy_plugin_init\n");
    fprintf(f, "nsslapd-plugintype: object\n");
    fprintf(f, "nsslapd-pluginenabled: on\n");
    fprintf(f, "nsslapd-plugin-depends-on-type: database\n");
    fprintf(f, "nsslapd-plugin-depends-on-named: Multimaster Replication Plugin\n");
    fprintf(f, "\n");

    fprintf(f, "dn: cn=Multimaster Replication Plugin,cn=plugins,cn=config\n");
    fprintf(f, "objectclass: top\n");
    fprintf(f, "objectclass: nsSlapdPlugin\n");
    fprintf(f, "objectclass: extensibleObject\n");
    fprintf(f, "cn: Multimaster Replication Plugin\n");
    fprintf(f, "nsslapd-pluginpath: %s/lib/replication-plugin%s\n", sroot, shared_lib);
    fprintf(f, "nsslapd-plugininitfunc: replication_multimaster_plugin_init\n");
    fprintf(f, "nsslapd-plugintype: object\n");
    fprintf(f, "nsslapd-pluginenabled: on\n");
    fprintf(f, "nsslapd-plugin-depends-on-named: ldbm database\n");
    fprintf(f, "nsslapd-plugin-depends-on-named: DES\n");
    fprintf(f, "\n");

    fprintf(f, "dn: cn=Retro Changelog Plugin,cn=plugins,cn=config\n");
    fprintf(f, "objectclass: top\n");
    fprintf(f, "objectclass: nsSlapdPlugin\n");
    fprintf(f, "objectclass: extensibleObject\n");
    fprintf(f, "cn: Retro Changelog Plugin\n");
    fprintf(f, "nsslapd-pluginpath: %s/lib/retrocl-plugin%s\n", sroot, shared_lib);
    fprintf(f, "nsslapd-plugininitfunc: retrocl_plugin_init\n");
    fprintf(f, "nsslapd-plugintype: object\n");
    fprintf(f, "nsslapd-pluginenabled: off\n");
    fprintf(f, "nsslapd-plugin-depends-on-type: database\n");
    fprintf(f, "\n");


    /* cos needs to be placed before other same type'ed plugins (postoperation) */
    fprintf(f, "dn: cn=Class of Service,cn=plugins,cn=config\n");
    fprintf(f, "objectclass: top\n");
    fprintf(f, "objectclass: nsSlapdPlugin\n");
    fprintf(f, "objectclass: extensibleObject\n");
    fprintf(f, "cn: Class of Service\n");
    fprintf(f, "nsslapd-pluginpath: %s/lib/cos-plugin%s\n", sroot, shared_lib);
    fprintf(f, "nsslapd-plugininitfunc: cos_init\n");
    fprintf(f, "nsslapd-plugintype: postoperation\n");
    fprintf(f, "nsslapd-pluginenabled: on\n");
    fprintf(f, "nsslapd-plugin-depends-on-type: database\n");
    fprintf(f, "nsslapd-plugin-depends-on-named: State Change Plugin\n");
    fprintf(f, "nsslapd-plugin-depends-on-named: Views\n");
    fprintf(f, "\n");

    fprintf(f, "dn: cn=Views,cn=plugins,cn=config\n");
    fprintf(f, "objectclass: top\n");
    fprintf(f, "objectclass: nsSlapdPlugin\n");
    fprintf(f, "objectclass: extensibleObject\n");
    fprintf(f, "cn: Views\n");
    fprintf(f, "nsslapd-pluginpath: %s/lib/views-plugin%s\n", sroot, shared_lib);
    fprintf(f, "nsslapd-plugininitfunc: views_init\n");
    fprintf(f, "nsslapd-plugintype: object\n");
    fprintf(f, "nsslapd-pluginenabled: on\n");
    fprintf(f, "nsslapd-plugin-depends-on-type: database\n");
    fprintf(f, "nsslapd-plugin-depends-on-named: State Change Plugin\n");
    fprintf(f, "\n");

    /* 
     * LP: Turn referential integrity plugin OFF by default 
     * defect 518862 
     */
    fprintf(f, "dn: cn=referential integrity postoperation,cn=plugins,cn=config\n");
    fprintf(f, "objectclass: top\n");
    fprintf(f, "objectclass: nsSlapdPlugin\n");
    fprintf(f, "objectclass: extensibleObject\n");
    fprintf(f, "cn: referential integrity postoperation\n");
    fprintf(f, "nsslapd-pluginpath: %s/lib/referint-plugin%s\n", sroot, shared_lib);
    fprintf(f, "nsslapd-plugininitfunc: referint_postop_init\n");
    fprintf(f, "nsslapd-plugintype: postoperation\n");
    fprintf(f, "nsslapd-pluginenabled: off\n");
    fprintf(f, "nsslapd-pluginArg0: %d\n", REFERINT_DELAY);
    fprintf(f, "nsslapd-pluginArg1: %s/logs/referint\n", cs_path);
    fprintf(f, "nsslapd-pluginArg2: %d\n", REFERINT_LOG_CHANGES);
    fprintf(f, "nsslapd-pluginArg3: member\n");
    fprintf(f, "nsslapd-pluginArg4: uniquemember\n");
     fprintf(f, "nsslapd-pluginArg5: owner\n");
     fprintf(f, "nsslapd-pluginArg6: seeAlso\n");
    fprintf(f, "nsslapd-plugin-depends-on-type: database\n");
    fprintf(f, "\n");
/*
    NT synch is dead as of 5.0

    fprintf(f, "dn: cn=ntSynchService preoperation,cn=plugins,cn=config\n");
    fprintf(f, "objectclass: top\n");
    fprintf(f, "objectclass: nsSlapdPlugin\n");
    fprintf(f, "objectclass: extensibleObject\n");
    fprintf(f, "cn: ntSynchService preoperation\n");
    fprintf(f, "nsslapd-pluginpath: %s/lib/ntsynch-plugin%s\n", sroot, shared_lib);
    fprintf(f, "nsslapd-plugininitfunc: libntsynch_plugin_preop_init\n");
    fprintf(f, "nsslapd-plugintype: preoperation\n");
    fprintf(f, "nsslapd-pluginenabled: on\n");
    fprintf(f, "\n");

    fprintf(f, "dn: cn=ntSynchService postoperation,cn=plugins,cn=config\n");
    fprintf(f, "objectclass: top\n");
    fprintf(f, "objectclass: nsSlapdPlugin\n");
    fprintf(f, "objectclass: extensibleObject\n");
    fprintf(f, "cn: ntSynchService postoperation\n");
    fprintf(f, "nsslapd-pluginpath: %s/lib/ntsynch-plugin%s\n", sroot, shared_lib);
    fprintf(f, "nsslapd-plugininitfunc: libntsynch_plugin_postop_init\n");
    fprintf(f, "nsslapd-plugintype: postoperation\n");
    fprintf(f, "nsslapd-pluginenabled: on\n");
    fprintf(f, "\n");
*/
    if (!cf->use_existing_user_ds) {
        t = cf->suffix;
    } else {
        t = cf->netscaperoot;
    }

    /* 
     * LP: Turn attribute uniqueness plugin OFF by default 
     * defect 518862 
     */
    fprintf(f, "dn: cn=attribute uniqueness,cn=plugins,cn=config\n");
    fprintf(f, "objectclass: top\n");
    fprintf(f, "objectclass: nsSlapdPlugin\n");
    fprintf(f, "objectclass: extensibleObject\n");
    fprintf(f, "cn: attribute uniqueness\n");
    fprintf(f, "nsslapd-pluginpath: %s/lib/attr-unique-plugin%s\n", sroot, shared_lib);
    fprintf(f, "nsslapd-plugininitfunc: NSUniqueAttr_Init\n");
    fprintf(f, "nsslapd-plugintype: preoperation\n");
    fprintf(f, "nsslapd-pluginenabled: off\n");
    fprintf(f, "nsslapd-pluginarg0: uid\n");
    fprintf(f, "nsslapd-pluginarg1: %s\n", t);
    fprintf(f, "nsslapd-plugin-depends-on-type: database\n");
    fprintf(f, "\n");

    fprintf(f, "dn: cn=7-bit check,cn=plugins,cn=config\n");
    fprintf(f, "objectclass: top\n");
    fprintf(f, "objectclass: nsSlapdPlugin\n");
    fprintf(f, "objectclass: extensibleObject\n");
    fprintf(f, "cn: 7-bit check\n");
    fprintf(f, "nsslapd-pluginpath: %s/lib/attr-unique-plugin%s\n", sroot, shared_lib);
    fprintf(f, "nsslapd-plugininitfunc: NS7bitAttr_Init\n");
    fprintf(f, "nsslapd-plugintype: preoperation\n");
    fprintf(f, "nsslapd-pluginenabled: on\n");
    fprintf(f, "nsslapd-pluginarg0: uid\n");
    fprintf(f, "nsslapd-pluginarg1: mail\n");
    fprintf(f, "nsslapd-pluginarg2: userpassword\n");
    fprintf(f, "nsslapd-pluginarg3: ,\n");
    fprintf(f, "nsslapd-pluginarg4: %s\n", t);
    fprintf(f, "nsslapd-plugin-depends-on-type: database\n");
    fprintf(f, "\n");

    t = 0;

    fprintf(f, "dn: cn=Internationalization Plugin,cn=plugins,cn=config\n");
    fprintf(f, "objectclass: top\n");
    fprintf(f, "objectclass: nsSlapdPlugin\n");
    fprintf(f, "objectclass: extensibleObject\n");
    fprintf(f, "cn: Internationalization Plugin\n");
    fprintf(f, "nsslapd-pluginpath: %s/lib/liblcoll%s\n", sroot, shared_lib);
    fprintf(f, "nsslapd-plugininitfunc: orderingRule_init\n");
    fprintf(f, "nsslapd-plugintype: matchingRule\n");
    fprintf(f, "nsslapd-pluginenabled: on\n");
    fprintf(f, "nsslapd-pluginarg0: %s/config/slapd-collations.conf\n", cs_path);
    fprintf(f, "\n");

    /* The HTTP client plugin */
    fprintf(f, "dn: cn=HTTP Client,cn=plugins,cn=config\n");
    fprintf(f, "objectclass: top\n");
    fprintf(f, "objectclass: nsSlapdPlugin\n");
    fprintf(f, "objectclass: extensibleObject\n");
    fprintf(f, "cn: HTTP Client\n");
    fprintf(f, "nsslapd-pluginpath: %s/lib/http-client-plugin%s\n", sroot, shared_lib);
    fprintf(f, "nsslapd-plugininitfunc: http_client_init\n");
    fprintf(f, "nsslapd-plugintype: preoperation\n");
    fprintf(f, "nsslapd-pluginenabled: on\n");
    fprintf(f, "nsslapd-plugin-depends-on-type: database\n");
    fprintf(f, "\n");

#if defined (BUILD_PRESENCE)
    /* The IM presence plugin root */
    fprintf(f, "dn: cn=Presence,cn=plugins,cn=config\n");
    fprintf(f, "objectclass: top\n");
    fprintf(f, "objectclass: nsSlapdPlugin\n");
    fprintf(f, "objectclass: extensibleObject\n");
    fprintf(f, "cn: Presence\n");
    fprintf(f, "nsslapd-pluginpath: %s/lib/presence-plugin%s\n", sroot, shared_lib);
    fprintf(f, "nsslapd-plugininitfunc: presence_init\n");
    fprintf(f, "nsslapd-plugintype: preoperation\n");
    fprintf(f, "nsslapd-pluginenabled: on\n");
    fprintf(f, "nsslapd-plugin-depends-on-type: database\n");
    fprintf(f, "nsslapd-plugin-depends-on-named: HTTP Client\n");
    fprintf(f, "\n");

    /* The AIM presence plugin */
    fprintf(f, "dn: cn=AIM Presence,cn=Presence,cn=plugins,cn=config\n");
    fprintf(f, "objectclass: top\n");
    fprintf(f, "objectclass: extensibleObject\n");
    fprintf(f, "cn: AIM Presence\n");
    fprintf(f, "nsim-id: nsAIMid\n");
    fprintf(f, "nsim-urltext: http://big.oscar.aol.com/$nsaimid?on_url=http://online&off_url=http://offline\n");
    fprintf(f, "nsim-urlgraphic: http://big.oscar.aol.com/$nsaimid?on_url=http://online&off_url=http://offline\n");
    fprintf(f, "nsim-onvaluemaptext: http://online\n");
    fprintf(f, "nsim-offvaluemaptext: http://offline\n");
    fprintf(f, "nsim-urltextreturntype: TEXT\n");
    fprintf(f, "nsim-urlgraphicreturntype: TEXT\n");
    fprintf(f, "nsim-requestmethod: REDIRECT\n");
    fprintf(f, "nsim-statustext: nsAIMStatusText\n");
    fprintf(f, "nsim-statusgraphic: nsAIMStatusGraphic\n");
    fprintf(f, "\n");

    /* The ICQ presence plugin */
    fprintf(f, "dn: cn=ICQ Presence,cn=Presence,cn=plugins,cn=config\n");
    fprintf(f, "objectclass: top\n");
    fprintf(f, "objectclass: extensibleObject\n");
    fprintf(f, "cn: ICQ Presence\n");
    fprintf(f, "nsim-id: nsICQid\n");
    fprintf(f, "nsim-urltext: http://online.mirabilis.com/scripts/online.dll?icq=$nsicqid&img=5\n");
    fprintf(f, "nsim-urlgraphic: http://online.mirabilis.com/scripts/online.dll?icq=$nsicqid&img=5\n");
    fprintf(f, "nsim-onvaluemaptext: /lib/image/0,,4367,00.gif\n");
    fprintf(f, "nsim-offvaluemaptext: /lib/image/0,,4349,00.gif\n");
    fprintf(f, "nsim-urltextreturntype: TEXT\n");
    fprintf(f, "nsim-urlgraphicreturntype: TEXT\n");
    fprintf(f, "nsim-requestmethod: REDIRECT\n");
    fprintf(f, "nsim-statustext: nsICQStatusText\n");
    fprintf(f, "nsim-statusgraphic: nsICQStatusGraphic\n");
    fprintf(f, "\n");

    /* The Yahoo presence plugin */
    fprintf(f, "dn: cn=Yahoo Presence,cn=Presence,cn=plugins,cn=config\n");
    fprintf(f, "objectclass: top\n");
    fprintf(f, "objectclass: extensibleObject\n");
    fprintf(f, "cn: Yahoo Presence\n");
    fprintf(f, "nsim-id: nsYIMid\n");
    fprintf(f, "nsim-urltext: http://opi.yahoo.com/online?u=$nsyimid&m=t\n");
    fprintf(f, "nsim-urlgraphic: http://opi.yahoo.com/online?u=$nsyimid&m=g&t=0\n");
    fprintf(f, "nsim-onvaluemaptext: $nsyimid is ONLINE\n");
    fprintf(f, "nsim-offvaluemaptext: $nsyimid is NOT ONLINE\n");
    fprintf(f, "nsim-urltextreturntype: TEXT\n");
    fprintf(f, "nsim-urlgraphicreturntype: BINARY\n");
    fprintf(f, "nsim-requestmethod: GET\n");
    fprintf(f, "nsim-statustext: nsYIMStatusText\n");
    fprintf(f, "nsim-statusgraphic: nsYIMStatusGraphic\n");
    fprintf(f, "\n");
#endif

        /* enable pass thru authentication */
    if (cf->use_existing_config_ds || cf->use_existing_user_ds)
    {
        LDAPURLDesc *desc = 0;
        char *url = cf->use_existing_config_ds ? cf->config_ldap_url :
            cf->user_ldap_url;
        if (url && !ldap_url_parse(url, &desc) && desc)
        {
            char *suffix = desc->lud_dn;
            char *service = !strncmp(url, "ldaps:", strlen("ldaps:")) ?
                "ldaps" : "ldap";
            if (cf->use_existing_config_ds)
            {
                suffix = cf->netscaperoot;
            }

            suffix = ds_URL_encode(suffix);
            fprintf(f, "dn: cn=Pass Through Authentication,cn=plugins,cn=config\n");
            fprintf(f, "objectclass: top\n");
            fprintf(f, "objectclass: nsSlapdPlugin\n");
            fprintf(f, "objectclass: extensibleObject\n");
            fprintf(f, "cn: Pass Through Authentication\n");
            fprintf(f, "nsslapd-pluginpath: %s/lib/passthru-plugin%s\n", sroot, shared_lib);
            fprintf(f, "nsslapd-plugininitfunc: passthruauth_init\n");
            fprintf(f, "nsslapd-plugintype: preoperation\n");
            fprintf(f, "nsslapd-pluginenabled: on\n");
            fprintf(f, "nsslapd-pluginarg0: %s://%s:%d/%s\n", service, desc->lud_host, desc->lud_port,
                    suffix);
            fprintf(f, "nsslapd-plugin-depends-on-type: database\n");
            fprintf(f, "\n");
            free(suffix);
            ldap_free_urldesc(desc);
        }
    }

#ifdef BUILD_PAM_PASSTHRU
#if !defined( XP_WIN32 )
	/* PAM Pass Through Auth plugin - off by default */
	fprintf(f, "dn: cn=PAM Pass Through Auth,cn=plugins,cn=config\n");
	fprintf(f, "objectclass: top\n");
	fprintf(f, "objectclass: nsSlapdPlugin\n");
	fprintf(f, "objectclass: extensibleObject\n");
	fprintf(f, "objectclass: pamConfig\n");
	fprintf(f, "cn: PAM Pass Through Auth\n");
	fprintf(f, "nsslapd-pluginpath: %s/lib/pam-passthru-plugin%s\n", sroot, shared_lib);
	fprintf(f, "nsslapd-plugininitfunc: pam_passthruauth_init\n");
	fprintf(f, "nsslapd-plugintype: preoperation\n");
	fprintf(f, "nsslapd-pluginenabled: off\n");
	fprintf(f, "nsslapd-pluginLoadGlobal: true\n");
	fprintf(f, "nsslapd-plugin-depends-on-type: database\n");
	fprintf(f, "pamMissingSuffix: ALLOW\n");
	if (cf->netscaperoot) {
		fprintf(f, "pamExcludeSuffix: %s\n", cf->netscaperoot);
	}
	fprintf(f, "pamExcludeSuffix: cn=config\n");
	fprintf(f, "pamMapMethod: RDN\n");
	fprintf(f, "pamFallback: FALSE\n");
	fprintf(f, "pamSecure: TRUE\n");
	fprintf(f, "pamService: ldapserver\n");
	fprintf(f, "\n");
#endif /* NO PAM FOR WINDOWS */
#endif /* BUILD_PAM_PASSTHRU */

    fprintf(f, "dn: cn=ldbm database,cn=plugins,cn=config\n");
    fprintf(f, "objectclass: top\n");
    fprintf(f, "objectclass: nsSlapdPlugin\n");
    fprintf(f, "objectclass: extensibleObject\n");
    fprintf(f, "cn: ldbm database\n");
    fprintf(f, "nsslapd-pluginpath: %s/lib/libback-ldbm%s\n", sroot, shared_lib);
    fprintf(f, "nsslapd-plugininitfunc: ldbm_back_init\n");
    fprintf(f, "nsslapd-plugintype: database\n");
    fprintf(f, "nsslapd-pluginenabled: on\n");
    fprintf(f, "nsslapd-plugin-depends-on-type: Syntax\n");
    fprintf(f, "nsslapd-plugin-depends-on-type: matchingRule\n");
    fprintf(f, "\n");

    if (strlen(cf->suffix) == 0){
        rootdse = 1;
    }

    /* Entries for the ldbm plugin */
    fprintf(f, "dn: cn=config,cn=ldbm database,cn=plugins,cn=config\n");
    fprintf(f, "objectclass: top\n");
    fprintf(f, "objectclass: extensibleObject\n");
    fprintf(f, "cn: config\n");
    fprintf(f, "nsslapd-lookthroughlimit: 5000\n");
    fprintf(f, "nsslapd-mode: 600\n");
    fprintf(f, "nsslapd-directory: %s/db\n", cs_path);
    fprintf(f, "nsslapd-dbcachesize: 10485760\n");
    /* will be default from 6.2 or 6.11... */
    if (getenv("USE_OLD_IDL_SWITCH")) {
        fprintf(f, "nsslapd-idl-switch: old\n");
    }
    fprintf(f, "\n");

    /* Placeholder for the default user-defined ldbm indexes */
    fprintf(f, "dn: cn=default indexes, cn=config,cn=ldbm database,cn=plugins,cn=config\n");
    fprintf(f, "objectclass: top\n");
    fprintf(f, "objectclass: extensibleObject\n");
    fprintf(f, "cn: default indexes\n");
    fprintf(f, "\n");

    /* default user-defined ldbm indexes */
    ds_gen_index(f, "cn=default indexes, cn=config,cn=ldbm database,cn=plugins,cn=config");




    fprintf(f, "dn: cn=monitor, cn=ldbm database, cn=plugins, cn=config\n");
    fprintf(f, "objectclass: top\n");
    fprintf(f, "objectclass: extensibleObject\n");
    fprintf(f, "cn: monitor\n");
    fprintf(f, "\n");

        fprintf(f, "dn: cn=database, cn=monitor, cn=ldbm database, cn=plugins, cn=config\n");
        fprintf(f, "objectclass: top\n");
        fprintf(f, "objectclass: extensibleObject\n");
        fprintf(f, "cn: database\n");
        fprintf(f, "\n");

    /* Entries for the chaining backend plugin */
    fprintf(f, "dn: cn=chaining database,cn=plugins,cn=config\n");
    fprintf(f, "objectclass: top\n");
    fprintf(f, "objectclass: nsSlapdPlugin\n");
    fprintf(f, "objectclass: extensibleObject\n");
    fprintf(f, "cn: chaining database\n");
    fprintf(f, "nsslapd-pluginpath: %s/lib/chainingdb-plugin%s\n", sroot, shared_lib);
    fprintf(f, "nsslapd-plugininitfunc: chaining_back_init\n");
    fprintf(f, "nsslapd-plugintype: database\n");
    fprintf(f, "nsslapd-pluginenabled: on\n");
    fprintf(f, "\n");

    fprintf(f, "dn: cn=config,cn=chaining database,cn=plugins,cn=config\n");
    fprintf(f, "objectclass: top\n");
    fprintf(f, "objectclass: extensibleObject\n");
    fprintf(f, "cn: config\n");
        fprintf(f, "nsTransmittedControls: 2.16.840.1.113730.3.4.2\n");
        fprintf(f, "nsTransmittedControls: 2.16.840.1.113730.3.4.9\n");
        fprintf(f, "nsTransmittedControls: 1.2.840.113556.1.4.473\n");
        fprintf(f, "nsTransmittedControls: 1.3.6.1.4.1.1466.29539.12\n");
    fprintf(f, "nsPossibleChainingComponents: cn=resource limits,cn=components,cn=config\n");
    fprintf(f, "nsPossibleChainingComponents: cn=certificate-based authentication,cn=components,cn=config\n");
    fprintf(f, "nsPossibleChainingComponents: cn=ACL Plugin,cn=plugins,cn=config\n");
    fprintf(f, "nsPossibleChainingComponents: cn=old plugin,cn=plugins,cn=config\n");
    fprintf(f, "nsPossibleChainingComponents: cn=referential integrity postoperation,cn=plugins,cn=config\n");
    fprintf(f, "nsPossibleChainingComponents: cn=attribute uniqueness,cn=plugins,cn=config\n");
    fprintf(f, "\n");

    free(t);
    t = NULL;

    /* suffix for the mapping tree */
    fprintf(f, "dn: cn=mapping tree,cn=config\n");
    fprintf(f, "objectclass: top\n");
    fprintf(f, "objectclass: extensibleObject\n");
    fprintf(f, "cn: mapping tree\n");
    fprintf(f, "\n");

    fprintf(f, "dn: cn=tasks,cn=config\n");
    fprintf(f, "objectclass: top\n");
    fprintf(f, "objectclass: extensibleObject\n");
    fprintf(f, "cn: tasks\n");
    fprintf(f, "\n");

    /* Entries for the ldbm instances and mapping tree */
    if ( cf->netscaperoot && !cf->use_existing_config_ds)
    {
        suffix_gen_conf(f, cf->netscaperoot, "NetscapeRoot");
    }

    if (!cf->use_existing_user_ds)
    {
        suffix_gen_conf(f, cf->suffix, "userRoot");
    }
    
    if ( cf->samplesuffix && cf->suffix && PL_strcasecmp(cf->samplesuffix, cf->suffix))
    {
        suffix_gen_conf(f, cf->samplesuffix, "sampleRoot");
    }

    if ( cf->testconfig && cf->suffix && PL_strcasecmp(cf->testconfig, cf->suffix))
    {
        suffix_gen_conf(f, cf->testconfig, "testRoot");
    }


    /* tasks */
    fprintf(f, "dn: cn=import,cn=tasks,cn=config\n");
    fprintf(f, "objectclass: top\n");
    fprintf(f, "objectclass: extensibleObject\n");
    fprintf(f, "cn: import\n");
    fprintf(f, "\n");

    fprintf(f, "dn: cn=export,cn=tasks,cn=config\n");
    fprintf(f, "objectclass: top\n");
    fprintf(f, "objectclass: extensibleObject\n");
    fprintf(f, "cn: export\n");
    fprintf(f, "\n");

    fprintf(f, "dn: cn=backup,cn=tasks,cn=config\n");
    fprintf(f, "objectclass: top\n");
    fprintf(f, "objectclass: extensibleObject\n");
    fprintf(f, "cn: backup\n");
    fprintf(f, "\n");

    fprintf(f, "dn: cn=restore,cn=tasks,cn=config\n");
    fprintf(f, "objectclass: top\n");
    fprintf(f, "objectclass: extensibleObject\n");
    fprintf(f, "cn: restore\n");
    fprintf(f, "\n");

#if defined(UPGRADEDB)
    fprintf(f, "dn: cn=upgradedb,cn=tasks,cn=config\n");
    fprintf(f, "objectclass: top\n");
    fprintf(f, "objectclass: extensibleObject\n");
    fprintf(f, "cn: upgradedb\n");
    fprintf(f, "\n");
#endif
    /* END of tasks */


    fprintf(f, "dn: cn=replication,cn=config\n");
    fprintf(f, "objectclass: top\n");
    fprintf(f, "objectclass: extensibleObject\n");
    fprintf(f, "cn: replication\n");
    fprintf(f, "\n");

    if( cf->replicationdn && *(cf->replicationdn) ) 
    {
        fprintf(f, "dn: cn=replication4,cn=replication,cn=config\n");
        fprintf(f, "cn: replication4\n");
        fprintf(f, "objectclass: top\n");
        fprintf(f, "objectclass: nsConsumer4Config\n");
        fprintf(f, "nsslapd-updatedn: %s\n", cf->replicationdn);
        fprintf(f, "nsslapd-updatepw: %s\n", cf->replicationhashedpw);
        fprintf(f, "\n");
    }

    if(cf->changelogdir && *(cf->changelogdir) )
    {
        fprintf(f, "dn: cn=changelog4,cn=config\n");
        fprintf(f, "cn: changelog4\n");
        fprintf(f, "objectclass: top\n");
        fprintf(f, "objectclass: nsChangelog4Config\n");
        fprintf(f, "nsslapd-changelogdir: %s\n", cf->changelogdir);
        fprintf(f, "nsslapd-changelogsuffix: %s\n", cf->changelogsuffix);
        fprintf(f, "nsslapd-changelogmaxage: 2d\n");
        fprintf(f, "\n");

        /* create the changelog directory */
        if( (t = create_instance_mkdir_p(cf->changelogdir, NEWDIR_MODE)) )
            return(t);
    }

    fclose (f);
    
    PR_snprintf(src, sizeof(src), "%s%cconfig%cdse.ldif", cs_path, FILE_PATHSEP, FILE_PATHSEP);
    PR_snprintf(fn, sizeof(fn), "%s%cconfig%cdse_original.ldif", cs_path, FILE_PATHSEP, FILE_PATHSEP);
    create_instance_copy(src, fn, 0600);

    /*
     * generate slapd-collations.conf
     */
    PR_snprintf(src, sizeof(src), "%s%cbin%c"PRODUCT_NAME"%cinstall%cconfig%c%s-collations.conf",
            sroot, FILE_PATHSEP, FILE_PATHSEP, FILE_PATHSEP, FILE_PATHSEP, 
            FILE_PATHSEP, PRODUCT_NAME);
    PR_snprintf(dest, sizeof(dest), "%s%cconfig%c%s-collations.conf", cs_path, FILE_PATHSEP,
            FILE_PATHSEP, PRODUCT_NAME);
    if (!(srcf = fopen(src, "r"))) {
        return make_error("Can't read from %s (%s)", src, ds_system_errmsg());
    }
    if (!(f = fopen(dest, "w"))) {
        return make_error("Can't write to %s (%s)", dest, ds_system_errmsg());
    }
    while (fgets(line, sizeof(line), srcf)) {
        if ((line[0] != '\0') && (fputs(line, f) == EOF)) {
            make_error("Error writing to file %s from copy of %s (%s)",
                       dest, src, ds_system_errmsg());
        }
    }
    if (!feof(srcf)) {
        make_error("Error reading from file %s (%s)", src, ds_system_errmsg());
    }
    fclose(srcf);
    fclose(f);

    PR_snprintf(src, sizeof(src), "%s/bin/slapd/install/schema", sroot);
    PR_snprintf(dest, sizeof(dest), "%s/config/schema", cs_path);
    if (NULL != (t = ds_copy_group_files(src, dest, 0)))
        return t;
        
#if defined (BUILD_PRESENCE)
    PR_snprintf(src, sizeof(src), "%s/bin/slapd/install/presence", sroot);
    PR_snprintf(dest, sizeof(dest), "%s/config/presence", cs_path);
    if (t = ds_copy_group_files(src, dest, 0))
        return t;
#endif

    /* Generate the orgchart configuration */
    PR_snprintf(src, sizeof(src), "%s/clients", sroot);
    if (is_a_dir(src, "orgchart")) {
        if (NULL != (t = ds_gen_orgchart_conf(sroot, cs_path, cf))) {
            return t;
        }
    }
        
    /* Generate dsgw.conf */
    PR_snprintf(src, sizeof(src), "%s/clients", sroot);
    if (is_a_dir(src, "dsgw")) {
        if (NULL != (t = ds_gen_gw_conf(sroot, cs_path, cf, GW_CONF))) {
            return t;
        }

        /* Generate pb.conf */
        if (NULL != (t = ds_gen_gw_conf(sroot, cs_path, cf, PB_CONF))) {
            return t;
        }
    }

    return NULL; /* Everything worked fine */
}

/*
 * Function: ds_gen_gw_conf
 *
 * Returns: pointer to error message, or NULL if all went well
 *
 * Description: This generates the gateway configuration files 
 *              for the regular gateway stuff and for the phonebook.
 *
 * Author: RJP
 *
 */
static char *
ds_gen_gw_conf(char *sroot, char *cs_path, server_config_s *cf, int conf_type)
{
    char dest[PATH_SIZE];
    char src[PATH_SIZE];
    char line[1024];
    FILE *f = NULL;
    FILE *srcf = NULL;
    char *t = NULL;
    const char *ctxt;
   
    if (conf_type == GW_CONF) {
        ctxt = "dsgw";
    } else {
        ctxt = "pb";
    }
    /*
     * generate .../dsgw/context/[dsgw|pb].conf by creating the file, placing
     * install-specific config. file lines at the start of file, and then
     * copying the rest from NS-HOME/dsgw/config/dsgw.tmpl
     */

    PR_snprintf(dest, sizeof(dest), "%s%cclients%cdsgw%ccontext%c%s.conf", sroot, FILE_PATHSEP,FILE_PATHSEP,
        FILE_PATHSEP, FILE_PATHSEP, ctxt);


    /* If the config file already exists, just return success */
    if (create_instance_exists(dest)) {
        return(NULL);
    }
    
    /* Attempt to open that bad boy */
    if(!(f = fopen(dest, "w"))) {
        return make_error("Can't write to %s (%s)", dest, ds_system_errmsg());
    }

    /* Write out the appropriate values */
    fprintf(f, "# Used by Netscape Directory Server Gateway\n");
    fprintf(f, "baseurl\t\"ldap://%s:%s/", cf->servname, cf->servport);
    fputs_escaped(cf->suffix, f);
    fputs("\"\n\n",f);
    if (cf->rootdn && *(cf->rootdn)) {
        t = ds_enquote_config_value(DS_ROOTDN, cf->rootdn);
        fprintf(f, "dirmgr\t%s\n\n", t );
        if (t != cf->rootdn) free(t);
    }

    t = ds_enquote_config_value(DS_SUFFIX, cf->suffix);
    fprintf(f, "location-suffix\t%s\n\n", t);
    if (t != cf->suffix) free(t);


    fprintf(f, "securitypath\t\"%s%calias%c%s-cert.db\"\n\n", sroot, FILE_PATHSEP, FILE_PATHSEP, ctxt );

    fprintf(f, "# The url base to the orgchart application.\n#No link from the DSGW to the orgchart will appear in the UI if this configuration line is commented out.\n");
    fprintf(f, "url-orgchart-base\thttp://%s:%s/clients/orgchart/bin/org?context=%s&data=\n\n", cf->servname, cf->adminport ? cf->adminport : "80", ctxt);

    /* copy in template */
    if (conf_type == GW_CONF) {
        PR_snprintf(src, sizeof(src), "%s%cclients%cdsgw%cconfig%cdsgw.tmpl",
            sroot, FILE_PATHSEP, FILE_PATHSEP, FILE_PATHSEP, FILE_PATHSEP);
    } else if (conf_type == PB_CONF) {
        PR_snprintf(src, sizeof(src), "%s%cclients%cdsgw%cpbconfig%cpb.tmpl",
            sroot, FILE_PATHSEP,FILE_PATHSEP, FILE_PATHSEP, FILE_PATHSEP);
    } else {
        /*This should never, ever happen if this function is called correctly*/
        fclose(f);
        return make_error("Unknown gateway config file requested");
    }


    /* Try to open the dsgw.conf template file (dsgw.tmpl) */
    if(!(srcf = fopen(src, "r"))) {
        fclose(f);
        return make_error("Can't read %s (%s)", src, ds_system_errmsg());
    }
    
    while(fgets(line, sizeof(line), srcf)) {
        fputs(line, f);
    }

    fclose(srcf);
    fclose(f);

    /* Generate default.conf */
    if (conf_type == GW_CONF) {
        struct passwd* pw = NULL;
        char defaultconf[PATH_SIZE];

#if !defined( XP_WIN32 )
        /* find the server's UID and GID */
        if (cf->servuser && *(cf->servuser)) {
            if ((pw = getpwnam (cf->servuser)) == NULL) {
                return make_error("Could not find UID and GID of user '%s'.", cf->servuser);
            } else if (pw->pw_name == NULL) {
                pw->pw_name = cf->servuser;
            }
        }
#endif

        PR_snprintf(defaultconf, sizeof(defaultconf), "%s%cclients%cdsgw%ccontext%cdefault.conf", sroot, 
          FILE_PATHSEP,FILE_PATHSEP, FILE_PATHSEP, FILE_PATHSEP);

        create_instance_copy(dest, defaultconf, NEWFILE_MODE);
        chownfile (pw, defaultconf);
    }
    unlink(src);

    return NULL;
}


/*
 * Function: ds_gen_orgchart_conf
 *
 * Returns: pointer to error message, or NULL if all went well
 *
 * Description: This generates the orgchart configuration file
 *
 * Author: RJP
 *
 */
static char *
ds_gen_orgchart_conf(char *sroot, char *cs_path, server_config_s *cf)
{
    char dest[PATH_SIZE];
    char src[PATH_SIZE];
    char line[1024];
    FILE *f = NULL;
    FILE *srcf = NULL;
   
    /*
     * generate .../clients/orgchart/config.txt by creating the file, placing
     * install-specific config. file lines at the start of file, and then
     * copying the rest from NS-HOME/clients/orgchart/config.tmpl
     */
    PR_snprintf(dest, sizeof(dest), "%s%cclients%corgchart%cconfig.txt", sroot, FILE_PATHSEP,
        FILE_PATHSEP, FILE_PATHSEP );
    PR_snprintf(src, sizeof(src), "%s%cclients%corgchart%cconfig.tmpl", sroot, FILE_PATHSEP, 
        FILE_PATHSEP, FILE_PATHSEP);

    /* If the config file already exists, just return success */
    if (create_instance_exists(dest)) {
        return(NULL);
    }
    
    /* Attempt to open that bad boy */
    if(!(f = fopen(dest, "w"))) {
        return make_error("Cannot write to %s (%s)", dest, ds_system_errmsg());
    }

    /* Write out the appropriate values */
    fprintf(f, "#############\n#\n#\n");
    fprintf(f, "#  Configuration file for Netscape Directory Server Org Chart\n");
    fprintf(f, "#  ----------------------------------------------------------\n#\n#\n");
    fprintf(f, "#############\n\n\n#\n");
    fprintf(f, "#   Blank lines in this file, as well as lines that\n");
    fprintf(f, "#   start with at least one \"#\" character, are both ignored.\n");
    fprintf(f, "#\n#\n");
    fprintf(f, "#   Name/Value pairs below are (and need to be) separated with\n");
    fprintf(f, "#   one or more tabs  (or spaces)\n");
    fprintf(f, "#\n");

    fprintf(f, "ldap-host\t%s\n", cf->servname);
    fprintf(f, "ldap-port\t%s\n", cf->servport);
    fprintf(f, "ldap-search-base\t%s\n\n", cf->suffix); 

    fprintf(f, "#\n#  If you would like to have the phonebook icon visible, you must\n");
    fprintf(f, "#  supply the partial phonebook URL below, which will have each\n");
    fprintf(f, "#  given user's DN attribute value concatenated to the end.\n");
    fprintf(f, "#\n#  For example, you could specify below something close to:\n");
    fprintf(f, "#\n#      url-phonebook-base      http://hostname.domain.com/dsgw/bin/dosearch?context=default&hp=localhost&dn=\n#\n\n");
    fprintf(f, "url-phonebook-base\thttp://%s:%s/clients/dsgw/bin/dosearch?context=pb&hp=%s:%s&dn=\n\n",cf->servname, cf->adminport ? cf->adminport : "80", cf->servname, cf->servport);
    
    /* Try to open the config.txt template file (config.tmpl) */
    if(!(srcf = fopen(src, "r"))) {
        fclose(f);
        return make_error("Can't read %s (%s)", src, ds_system_errmsg());
    }
    
    while(fgets(line, sizeof(line), srcf)) {
        fputs(line, f);
    }

    fclose(srcf);
    fclose(f);

    unlink(src);    
    return NULL;
}

#if defined (BUILD_PRESENCE)
/*
 * Function: gen_presence_init
 *
 * Description: Creates a script to initialize images for use in the IM
 * Presence plugin.
 */
#define    PRESENCE_LDIF "init_presence_images.ldif"
static char *gen_presence_init_script(char *sroot, server_config_s *cf, 
    char *cs_path)
{
    char fn[PATH_SIZE];
    char dir[PATH_SIZE];
    FILE *f;

    PR_snprintf(dir, sizeof(dir), "%s%cconfig%cpresence",
            cs_path, FILE_PATHSEP, FILE_PATHSEP);
    PR_snprintf(fn, sizeof(fn), "%s%c%s",
            dir, FILE_PATHSEP, PRESENCE_LDIF);

    if(!(f = fopen(fn, "w")))  
        return make_error("Could not write to %s (%s).", fn, ds_system_errmsg());

    fprintf( f,
             "dn:cn=ICQ Presence,cn=Presence,cn=plugins,cn=config\n"
             "changeType:modify\n"
             "replace:nsim-onvaluemapgraphic\n"
             "nsim-onvaluemapgraphic: %s%cicq-online.gif\n"
             "\n"
             "dn:cn=ICQ Presence,cn=Presence,cn=plugins,cn=config\n"
             "changeType:modify\n"
             "replace:nsim-offvaluemapgraphic\n"
             "nsim-offvaluemapgraphic: %s%cicq-offline.gif\n"
             "\n"
             "dn:cn=ICQ Presence,cn=Presence,cn=plugins,cn=config\n"
             "changeType:modify\n"
             "replace:nsim-disabledvaluemapgraphic\n"
             "nsim-disabledvaluemapgraphic: %s%cicq-disabled.gif\n"
             "\n"
             "dn:cn=AIM Presence,cn=Presence,cn=plugins,cn=config\n"
             "changeType:modify\n"
             "replace:nsim-onvaluemapgraphic\n"
             "nsim-onvaluemapgraphic: %s%caim-online.gif\n"
             "\n"
             "dn:cn=AIM Presence,cn=Presence,cn=plugins,cn=config\n"
             "changeType:modify\n"
             "replace:nsim-offvaluemapgraphic\n"
             "nsim-offvaluemapgraphic: %s%caim-offline.gif\n"
             "\n"
             "dn:cn=AIM Presence,cn=Presence,cn=plugins,cn=config\n"
             "changeType:modify\n"
             "replace:nsim-disabledvaluemapgraphic\n"
             "nsim-disabledvaluemapgraphic: %s%caim-offline.gif\n"
             "\n"
             "dn:cn=Yahoo Presence,cn=Presence,cn=plugins,cn=config\n"
             "changeType:modify\n"
             "replace:nsim-offvaluemapgraphic\n"
             "nsim-offvaluemapgraphic: %s%cyahoo-offline.gif\n"
             "\n"
             "dn:cn=Yahoo Presence,cn=Presence,cn=plugins,cn=config\n"
             "changeType:modify\n"
             "replace:nsim-onvaluemapgraphic\n"
             "nsim-onvaluemapgraphic: %s%cyahoo-online.gif\n"
             "\n"
             "dn:cn=Yahoo Presence,cn=Presence,cn=plugins,cn=config\n"
             "changeType:modify\n"
             "replace:nsim-disabledvaluemapgraphic\n"
             "nsim-disabledvaluemapgraphic: %s%cyahoo-offline.gif\n",
             dir, FILE_PATHSEP,
             dir, FILE_PATHSEP,
             dir, FILE_PATHSEP,
             dir, FILE_PATHSEP,
             dir, FILE_PATHSEP,
             dir, FILE_PATHSEP,
             dir, FILE_PATHSEP,
             dir, FILE_PATHSEP,
             dir, FILE_PATHSEP
        );
    fclose(f);
    return NULL;
}

/*
 * Function init_presence
 *
 * Description: Runs ldapmodify to initialize the images used by the
 * IM presence plugin
 */
static int init_presence(char *sroot, server_config_s *cf, char *cs_path)
{
    char cmd[PATH_SIZE];
    char tools[PATH_SIZE];
    char precmd[PATH_SIZE];

    precmd[0] = 0;
    PR_snprintf(tools, sizeof(tools), "%s%cshared%cbin", sroot, FILE_PATHSEP, FILE_PATHSEP);    

#ifdef XP_UNIX
    PR_snprintf(precmd, sizeof(precmd), "cd %s;", tools);
#endif

    PR_snprintf(cmd, sizeof(cmd), "%s%s%cldapmodify -q -p %d -b -D \"%s\" -w \"%s\" "
            "-f %s%s%cconfig%cpresence%c%s%s",
            precmd,
            tools, FILE_PATHSEP,
            atoi(cf->servport),
            cf->rootdn,
            cf->rootpw,
            ENQUOTE, cs_path, FILE_PATHSEP, FILE_PATHSEP, FILE_PATHSEP,
            PRESENCE_LDIF, ENQUOTE);
    return ds_exec_and_report( cmd );
}
#endif

/*
 * Function: ds_gen_index
 *
 * Description: This generates the default index list.
 * This function is passed the parent entry below which the nsIndex
 * entries must be created. This allows to use it when creating:
 *    - the default index list (ie belowdn = cn=default indexes,cn=config...)
 *    - the userRoot backend (ie belowdn = cn=index,cn=userRoot...)
 *
 */
static void
ds_gen_index(FILE* f, char* belowdn)
{
#define MKINDEX(_name, _inst, _sys, _type1, _type2, _type3) do { \
	char *_type2str = (_type2), *_type3str = (_type3); \
    fprintf(f, "dn: cn=%s,%s\n", (_name), (_inst)); \
    fprintf(f, "objectclass: top\n"); \
    fprintf(f, "objectclass: nsIndex\n"); \
    fprintf(f, "cn: %s\n", (_name)); \
    fprintf(f, "nssystemindex: %s\n", (_sys) ? "true" : "false"); \
	if (_type1) \
        fprintf(f, "nsindextype: %s\n", (_type1)); \
    if (_type2str) \
        fprintf(f, "nsindextype: %s\n", _type2str); \
    if (_type3str) \
        fprintf(f, "nsindextype: %s\n", _type3str); \
    fprintf(f, "\n"); \
} while (0)

    MKINDEX("aci", belowdn, 1, "pres", NULL, NULL);
    MKINDEX("cn", belowdn, 0, "pres", "eq", "sub");
    MKINDEX("entrydn", belowdn, 1, "eq", NULL, NULL);
    MKINDEX("givenName", belowdn, 0, "pres", "eq", "sub");
    MKINDEX("mail", belowdn, 0, "pres", "eq", "sub");
    MKINDEX("mailAlternateAddress", belowdn, 0, "eq", NULL, NULL);
    MKINDEX("mailHost", belowdn, 0, "eq", NULL, NULL);
    MKINDEX("member", belowdn, 0, "eq", NULL, NULL);
    MKINDEX("nsCalXItemId", belowdn, 0, "pres", "eq", "sub");
    MKINDEX("nsLIProfileName", belowdn, 0, "eq", NULL, NULL);
    MKINDEX("nsUniqueId", belowdn, 1, "eq", NULL, NULL);
    MKINDEX("nswcalCALID", belowdn, 0, "eq", NULL, NULL);
    MKINDEX("numsubordinates", belowdn, 1, "pres", NULL, NULL);
    MKINDEX("objectclass", belowdn, 1, "eq", NULL, NULL);
    MKINDEX("owner", belowdn, 0, "eq", NULL, NULL);
    MKINDEX("parentid", belowdn, 1, "eq", NULL, NULL);
    MKINDEX("pipstatus", belowdn, 0, "eq", NULL, NULL);
    MKINDEX("pipuid", belowdn, 0, "pres", NULL, NULL);
    MKINDEX("seeAlso", belowdn, 0, "eq", NULL, NULL);
    MKINDEX("sn", belowdn, 0, "pres", "eq", "sub");
    MKINDEX("telephoneNumber", belowdn, 0, "pres", "eq", "sub");
    MKINDEX("uid", belowdn, 0, "eq", NULL, NULL);
    MKINDEX("uniquemember", belowdn, 0, "eq", NULL, NULL);
}



static char *install_ds(char *sroot, server_config_s *cf, char *param_name)
{
    SLAPD_CONFIG slapd_conf;
    QUERY_VARS query_vars;
    char *t, src[PATH_SIZE], dest[PATH_SIZE], big_line[PATH_SIZE];
    char cs_path[PATH_SIZE];
    struct passwd* pw = NULL;
    int isrunning;
    int status = 0;
#ifdef XP_WIN32
    WSADATA    wsadata;
#endif

#if !defined( XP_WIN32 )
    /* find the server's UID and GID */
    if (cf->servuser && *(cf->servuser)) {
        if ((pw = getpwnam (cf->servuser)) == NULL) {
            PL_strncpyz(param_name, "servuser", BIG_LINE);
            return make_error("Could not find UID and GID of user '%s'.", 
                      cf->servuser);
        } else if (pw->pw_name == NULL) {
            pw->pw_name = cf->servuser;
        }
    }
#endif
    
    PR_snprintf(cs_path, sizeof(cs_path), "%s%c"PRODUCT_NAME"-%s", sroot, FILE_PATHSEP, cf->servid);

    /* create all <a_server>/<subdirs> */
    if ( (t = ds_cre_subdirs(sroot, cf, cs_path, pw)) )
        return(t);

    /* Generate all scripts */
    if ( (t = ds_gen_scripts(sroot, cf, cs_path)) )
        return(t);

#if defined( XP_WIN32 )
    ds_dostounixpath( sroot );
    ds_dostounixpath( cs_path );
#endif

    /* Generate all conf files */
    if ( (t = ds_gen_confs(sroot, cf, cs_path)) )
        return(t);

    PR_snprintf(src, sizeof(src), "%s%cbin%c"PRODUCT_NAME"%cinstall%cldif%cExample.ldif", sroot, FILE_PATHSEP, FILE_PATHSEP, FILE_PATHSEP, FILE_PATHSEP, 
        FILE_PATHSEP);
    PR_snprintf(dest, sizeof(dest), "%s%cldif%cExample.ldif", cs_path, FILE_PATHSEP, FILE_PATHSEP);
    create_instance_copy(src, dest, NEWFILE_MODE);
    chownfile (pw, dest);

    PR_snprintf(src, sizeof(src), "%s%cbin%c"PRODUCT_NAME"%cinstall%cldif%cExample-roles.ldif", sroot, FILE_PATHSEP, FILE_PATHSEP, FILE_PATHSEP, FILE_PATHSEP, 
        FILE_PATHSEP);
    PR_snprintf(dest, sizeof(dest), "%s%cldif%cExample-roles.ldif", cs_path, FILE_PATHSEP, FILE_PATHSEP);
    create_instance_copy(src, dest, NEWFILE_MODE);
    chownfile (pw, dest);

    PR_snprintf(src, sizeof(src), "%s%cbin%c"PRODUCT_NAME"%cinstall%cldif%cExample-views.ldif", sroot, FILE_PATHSEP, FILE_PATHSEP, FILE_PATHSEP, FILE_PATHSEP, 
        FILE_PATHSEP);
    PR_snprintf(dest, sizeof(dest), "%s%cldif%cExample-views.ldif", cs_path, FILE_PATHSEP, FILE_PATHSEP);
    create_instance_copy(src, dest, NEWFILE_MODE);
    chownfile (pw, dest);

    PR_snprintf(src, sizeof(src), "%s%cbin%c"PRODUCT_NAME"%cinstall%cldif%cEuropean.ldif", sroot, FILE_PATHSEP, FILE_PATHSEP, FILE_PATHSEP, FILE_PATHSEP, 
    FILE_PATHSEP);
    PR_snprintf(dest, sizeof(dest), "%s%cldif%cEuropean.ldif", cs_path, FILE_PATHSEP, FILE_PATHSEP);
    create_instance_copy(src, dest, NEWFILE_MODE);
    chownfile (pw, dest);

    /* new code for dsml sample files */
    PR_snprintf(src, sizeof(src), "%s%cbin%c"PRODUCT_NAME"%cinstall%cdsml%cExample.dsml", sroot, FILE_PATHSEP, FILE_PATHSEP, FILE_PATHSEP, FILE_PATHSEP, 
        FILE_PATHSEP);
    PR_snprintf(dest, sizeof(dest), "%s%cdsml%cExample.dsml", cs_path, FILE_PATHSEP, FILE_PATHSEP);
    create_instance_copy(src, dest, NEWFILE_MODE);
    chownfile (pw, dest);

    PR_snprintf(src, sizeof(src), "%s%cbin%c"PRODUCT_NAME"%cinstall%cdsml%cExample-roles.dsml", sroot, FILE_PATHSEP, FILE_PATHSEP, FILE_PATHSEP, FILE_PATHSEP, 
        FILE_PATHSEP);
    PR_snprintf(dest, sizeof(dest), "%s%cdsml%cExample-roles.dsml", cs_path, FILE_PATHSEP, FILE_PATHSEP);
    create_instance_copy(src, dest, NEWFILE_MODE);
    chownfile (pw, dest);

    PR_snprintf(src, sizeof(src), "%s%cbin%c"PRODUCT_NAME"%cinstall%cdsml%cEuropean.dsml", sroot, FILE_PATHSEP, FILE_PATHSEP, FILE_PATHSEP, FILE_PATHSEP, 
        FILE_PATHSEP);
    PR_snprintf(dest, sizeof(dest), "%s%cdsml%cEuropean.dsml", cs_path, FILE_PATHSEP, FILE_PATHSEP);
    create_instance_copy(src, dest, NEWFILE_MODE);
    chownfile (pw, dest);

    /*
      If the user has specified an LDIF file to use to initialize the database,
      load it now
    */
    if (cf->install_ldif_file && !access(cf->install_ldif_file, 0))
    {
    char msg[2*PATH_SIZE] = {0};
    int status = ds_ldif2db_backend_subtree(cf->install_ldif_file, NULL, cf->suffix);
    if (status)
        PR_snprintf(msg, sizeof(msg), "The file %s could not be loaded",
            cf->install_ldif_file);
    else
        PR_snprintf(msg, sizeof(msg), "The file %s was successfully loaded",
            cf->install_ldif_file);
    ds_show_message(msg);
    free(cf->install_ldif_file);
    cf->install_ldif_file = NULL;
    }

    /*
      All of the config files have been written, and the server should
      be ready to go.  Start the server if the user specified to start
      it or if we are configuring the server to serve as the repository
      for SuiteSpot (Mission Control) information
      Only attempt to start the server if the port is not in use
      */
    if(needToStartServer(cf) &&
       !(t = create_instance_checkport(cf->bindaddr, cf->servport)))
    {
    PR_snprintf(big_line, sizeof(big_line),"SERVER_NAMES=slapd-%s",cf->servid);
    putenv(big_line);

    isrunning = ds_get_updown_status();

    if (isrunning != DS_SERVER_UP)
    {
        int start_status = 0;
        int verbose = 1;
        char instance_dir[PATH_SIZE], errorlog[PATH_SIZE];

        if (getenv("USE_DEBUGGER"))
        verbose = 0;
        /* slapd-nickname directory */
        PR_snprintf(instance_dir, sizeof(instance_dir), "%s%c"PRODUCT_NAME"-%s", sroot, FILE_PATHSEP, 
            cf->servid);
        /* error log file */
        PR_snprintf(errorlog, sizeof(errorlog), "%s%clogs%cerrors", instance_dir, FILE_PATHSEP,
            FILE_PATHSEP);
        start_status = ds_bring_up_server_install(verbose, instance_dir, errorlog);

        if (start_status != DS_SERVER_UP)
        {
        /*
          If we were going to configure the server for SuiteSpot (Mission
          Control), the server must be running.  Therefore, it is a very
          bad thing, and we want to exit with a non zero exit code so the
          caller will know something went wrong.
          Otherwise, if the user just wanted to start the server for some
          reason, just exit with a zero and the messages printed will
          let the user know the server wasn't started.
          */
        char *msg;
        if (start_status == DS_SERVER_PORT_IN_USE)
            msg = "The server could not be started because the port is in use.";
        else if (start_status == DS_SERVER_MAX_SEMAPHORES)
            msg = "No more servers may be installed on this system.\nPlease refer to documentation for information about how to\nincrease the number of installed servers per system.";
        else if (start_status == DS_SERVER_CORRUPTED_DB)
            msg = "The server could not be started because the database is corrupted.";
        else if (start_status == DS_SERVER_NO_RESOURCES)
            msg = "The server could not be started because the operating system is out of resources (e.g. CPU memory).";
        else if (start_status == DS_SERVER_COULD_NOT_START)
            msg = "The server could not be started due to invalid command syntax or operating system resource limits.";
        else
            msg = "The server could not be started.";

        if( cf->cfg_sspt && !strcmp(cf->cfg_sspt, "1") )
        {
            ds_report_error(DS_SYSTEM_ERROR, "server", msg);
            return msg;
        }
        else
        {
            ds_show_message(msg);
            return 0;
        }
        }
        else
        {
        ds_show_message("Your new directory server has been started.");
        }
    }

    /* write ldap.conf */
    write_ldap_info( sroot, cf );

#ifdef XP_UNIX
    ds_become_localuser_name (cf->servuser);
#endif
#ifdef XP_WIN32
    if( errno = WSAStartup(0x0101, &wsadata ) != 0 )
    {
        char szTmp[512];
    /*replaced errno > -1 && errno < sys_nerr ? sys_errlist[errno] :
                    "unknown" with strerror(errno)*/
        PR_snprintf(szTmp, sizeof(szTmp), "Error: Windows Sockets initialization failed errno %d (%s)<br>\n", errno,
            strerror(errno), 0 );
            
        fprintf (stdout, szTmp);
        return 0;
    }
#endif /* XP_WIN32 */

    memset( &query_vars, 0, sizeof(query_vars) );
    if (!cf->use_existing_user_ds)
        query_vars.suffix = myStrdup( cf->suffix );
    query_vars.ssAdmID = myStrdup( cf->cfg_sspt_uid );
    query_vars.ssAdmPW1 = myStrdup( cf->cfg_sspt_uidpw );
    query_vars.ssAdmPW2 = myStrdup( cf->cfg_sspt_uidpw );
    query_vars.rootDN = myStrdup( cf->rootdn ); 
    query_vars.rootPW = myStrdup( cf->rootpw );
    query_vars.admin_domain =
        myStrdup( cf->admin_domain );
    query_vars.netscaperoot = myStrdup( cf->netscaperoot );
    query_vars.testconfig = myStrdup( cf->testconfig );
    query_vars.consumerDN = myStrdup(cf->consumerdn);
    query_vars.consumerPW = myStrdup(cf->consumerhashedpw);
    if (cf->cfg_sspt && !strcmp(cf->cfg_sspt, "1"))
        query_vars.cfg_sspt = 1;
    else
        query_vars.cfg_sspt = 0;

    if (cf->suitespot3x_uid)
        query_vars.config_admin_uid = myStrdup(cf->suitespot3x_uid);
    else
        query_vars.config_admin_uid = myStrdup(cf->cfg_sspt_uid);

    memset(&slapd_conf, 0, sizeof(SLAPD_CONFIG));
    if (sroot)
        PL_strncpyz(slapd_conf.slapd_server_root, sroot, sizeof(slapd_conf.slapd_server_root));
    if (cf->servport)
        slapd_conf.port = atoi(cf->servport);
    if (cf->servname)
        PL_strncpyz(slapd_conf.host, cf->servname, sizeof(slapd_conf.host));

    status = config_suitespot(&slapd_conf, &query_vars);
    if (status == -1) /* invalid or null arguments or configuration */
        return "Invalid arguments for server configuration.";
    }
    else if (t) /* just notify the user about the port conflict */
    {
    ds_show_message(t);
    }

#if defined (BUILD_PRESENCE)
    /* Create script for initializing IM Presence images */
    if ((NULL == t) && (0 == status))
    {
        if ( (t = gen_presence_init_script(sroot, cf, cs_path)) )
            return(t);
        /* Initialize IM Presence images */
        status = init_presence(sroot, cf, cs_path);
        if (status)
            return make_error ("ds_exec_and_report() failed (%d).", status);
    }
#endif

    if (status)
    return make_error ("Could not configure server (%d).", status);

    return(NULL);
}

/* write_ldap_info() : writes ldap.conf */

static int
write_ldap_info( char *slapd_server_root, server_config_s *cf)
{
    FILE* fp;
    int ret = 0;

    char* fmt = "%s/shared/config/ldap.conf";
    char* infoFileName;

    if (!slapd_server_root) {
      return -1;
    }
    
    infoFileName = PR_smprintf(fmt, slapd_server_root);

    if ((fp = fopen(infoFileName, "w")) == NULL)
    {
        ret = -1;
    }
    else
    {
        fprintf(fp, "url\tldap://%s:%d/",
                cf->servname, atoi(cf->servport));
    
        if (cf->suffix)
            fprintf(fp, "%s", cf->suffix);

        fprintf(fp, "\n");
        
        if (cf->cfg_sspt_uid) {
            fprintf(fp, "admnm\t%s\n", cf->cfg_sspt_uid);
        }

        fclose(fp);
    }
    PR_smprintf_free(infoFileName);

    return ret;
}

/* ----------- Create a new server from configuration variables ----------- */


int create_config(server_config_s *cf)
{
    char *t = NULL;
	char error_param[BIG_LINE] = {0};

    t = create_server(cf, error_param);
    if(t)
	{
		char *msg;
		if (error_param[0])
		{
			msg = PR_smprintf("%s.error:could not create server %s - %s",
							  error_param, cf->servid, t);
		}
		else
		{
			msg = PR_smprintf("error:could not create server %s - %s",
							  cf->servid, t);
		}
		ds_show_message(msg);
		PR_smprintf_free(msg);
	}
	else
	{
		ds_show_message("Created new Directory Server");
		return 0;
	}

    return 1;
}


/* ------ check passwords are same and satisfy minimum length policy------- */
static int check_passwords(char *pw1, char *pw2)
{
    if (strcmp (pw1, pw2) != 0) {
	    ds_report_error (DS_INCORRECT_USAGE, " different passwords",
			  "Enter the password again."
			  "  The two passwords you entered are different.");
		return 1;
	}
	
    if ( ((int) strlen(pw1)) < 8 ) {
	    ds_report_error (DS_INCORRECT_USAGE, " password too short",
			  "The password must be at least 8 characters long.");
		return 1;
	}

	return 0;
}

/* ------ Parse the results of a form and create a server from them ------- */


int parse_form(server_config_s *cf)
{
    char *rm = getenv("REQUEST_METHOD");
    char *qs = getenv("QUERY_STRING");
    char* cfg_sspt_uid_pw1;
    char* cfg_sspt_uid_pw2;
    LDAPURLDesc *desc = 0;
    char *temp = 0;

    if (!(cf->sroot = getenv("NETSITE_ROOT"))) {
		ds_report_error (DS_INCORRECT_USAGE, " NETSITE_ROOT environment variable not set.",
						 "The environment variable NETSITE_ROOT must be set to the server root directory.");
		return 1;
	}

    if (rm && qs && !strcmp(rm, "GET"))
	{
		ds_get_begin(qs);
	}
    else if (ds_post_begin(stdin))
	{
		return 1;
	}

    if (rm)
	{
		printf("Content-type: text/plain\n\n");
	}
    /* else we are being called from server installation; no output */

    if (!(cf->servname = ds_a_get_cgi_var("servname", "Server Name",
										  "Please give a hostname for your server.")))
	{
		return 1;
	}

	cf->bindaddr = ds_a_get_cgi_var("bindaddr", NULL, NULL);
    if (!(cf->servport = ds_a_get_cgi_var("servport", "Server Port",
										  "Please specify the TCP port number for this server.")))
	{
		return 1;
	}

    cf->cfg_sspt = ds_a_get_cgi_var("cfg_sspt", NULL, NULL);
    cf->cfg_sspt_uid = ds_a_get_cgi_var("cfg_sspt_uid", NULL, NULL);
    if (cf->cfg_sspt_uid && *(cf->cfg_sspt_uid) &&
		!(cf->cfg_sspt_uidpw = ds_a_get_cgi_var("cfg_sspt_uid_pw", NULL, NULL)))
	{

		if (!(cfg_sspt_uid_pw1 = ds_a_get_cgi_var("cfg_sspt_uid_pw1", "Password",
												  "Enter the password for the Mission Control Administrator's account.")))
		{
			return 1;
		}

		if (!(cfg_sspt_uid_pw2 = ds_a_get_cgi_var("cfg_sspt_uid_pw2", "Password",
												  "Enter the password for the Mission Control Administrator account, "
												  "twice.")))
		{
			return 1;
		}

		if (strcmp (cfg_sspt_uid_pw1, cfg_sspt_uid_pw2) != 0)
		{