1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359
|
/* vi:ai:et:ts=8 sw=2
*/
/*
* wzdftpd - a modular and cool ftp server
* Copyright (C) 2002-2004 Pierre Chifflier
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* As a special exemption, Pierre Chifflier
* and other respective copyright holders give permission to link this program
* with OpenSSL, and distribute the resulting executable, without including
* the source code for OpenSSL in the source distribution.
*/
/** \file wzd_ClientThread.h
* \brief Main loop of wzdftpd client.
*
* This file contains the code which is executed by threads, and
* most of the core FTP functions (see RFC 959).
*/
#include "wzd_all.h"
#ifdef WIN32
#include <winsock2.h>
#include <ws2tcpip.h>
#include <io.h>
#include <sys/utime.h>
#else
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h> /* gethostbyaddr */
#endif /* WIN32 */
/** \todo XXX FIXME remove this line and use correct types !!!!
* this is used to convert char* to struct in6_addr
*/
#define PORCUS_CAST(x) ( ((struct in6_addr*)(x)) )
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
#ifdef HAVE_UTIME_H
# include <utime.h>
#endif
#ifndef WIN32
#include <unistd.h>
#include <pthread.h>
#endif
#ifndef HAVE_STRTOK_R
# include "libwzd-base/wzd_strtok_r.h"
#endif
#include "wzd_structs.h"
#include "wzd_fs.h"
#include "wzd_ip.h"
#include "wzd_log.h"
#include "wzd_misc.h"
#include "wzd_mod.h"
#include "wzd_data.h"
#include "wzd_messages.h"
#include "wzd_vfs.h"
#include "wzd_configfile.h"
#include "wzd_crc32.h"
#include "wzd_events.h"
#include "wzd_file.h"
#include "wzd_group.h"
#include "wzd_libmain.h"
#include "wzd_login.h"
#include "wzd_perm.h"
#include "wzd_protocol.h"
#include "wzd_ratio.h"
#include "wzd_section.h"
#include "wzd_site.h"
#include "wzd_string.h"
#include "wzd_socket.h"
#include "wzd_tls.h"
#include "wzd_user.h"
#include "wzd_utf8.h"
#include "ls.h"
#include "wzd_ClientThread.h"
#include <libwzd-auth/wzd_base64.h>
#include <libwzd-auth/wzd_md5.h>
#include "wzd_debug.h"
#define TELNET_SYNCH 242
#define TELNET_IP 244
#define BUFFER_LEN 4096
/*************** clear_read **************************/
/** \brief Non-blocking read function
*
* Try to read length bytes in non-blocking mode for timeout seconds
* max. If timeout is null, performs a blocking read.
*/
int clear_read(fd_t sock, char *msg, size_t length, int flags, unsigned int timeout, void * vcontext)
{
/* wzd_context_t * context = (wzd_context_t*)vcontext;*/
int ret;
int save_errno;
fd_set fds, efds;
struct timeval tv;
if (timeout==0)
ret = recv(sock,msg,length,0);
else {
while (1) {
FD_ZERO(&fds);
FD_ZERO(&efds);
FD_SET(sock,&fds);
FD_SET(sock,&efds);
tv.tv_sec = timeout; tv.tv_usec = 0;
#if defined(_MSC_VER)
ret = select(0,&fds,NULL,&efds,&tv);
#else
ret = select(sock+1,&fds,NULL,&efds,&tv);
#endif
save_errno = errno;
if (FD_ISSET(sock,&fds)) /* ok */
break;
if (FD_ISSET(sock,&efds)) {
if (save_errno == EINTR) continue;
out_log(LEVEL_CRITICAL,"Error during recv: %s\n",strerror(save_errno));
return -1;
}
if (!FD_ISSET(sock,&fds)) /* timeout */
return 0;
break;
}
ret = recv(sock,msg,length,0);
} /* timeout */
return ret;
}
/*************** clear_write *************************/
/** \brief Non-blocking write function
*
* Try to write length bytes in non-blocking mode for timeout seconds
* max. If timeout is null, performs a blocking write.
*/
int clear_write(fd_t sock, const char *msg, size_t length, int flags, unsigned int timeout, void * vcontext)
{
/* wzd_context_t * context = (wzd_context_t*)vcontext;*/
int ret;
int done;
int save_errno;
fd_set fds, efds;
struct timeval tv;
done=0;
while (length>0) {
if (timeout==0)
ret = send(sock,msg+done,length,0);
else {
while (1) {
FD_ZERO(&fds);
FD_ZERO(&efds);
FD_SET(sock,&fds);
FD_SET(sock,&efds);
tv.tv_sec = timeout; tv.tv_usec = 0;
#if defined(_MSC_VER)
ret = select(0,NULL,&fds,&efds,&tv);
#else
ret = select(sock+1,NULL,&fds,&efds,&tv);
#endif
save_errno = errno;
if (FD_ISSET(sock,&fds)) /* break */
break;
if (FD_ISSET(sock,&efds)) {
if (save_errno == EINTR) continue;
out_log(LEVEL_CRITICAL,"Error during send: %s\n",strerror(save_errno));
return -1;
}
if (!FD_ISSET(sock,&fds)) /* timeout */
{
out_log(LEVEL_CRITICAL,"Timeout during send\n");
return 0;
}
break;
}
ret = send(sock,msg+done,length,0);
if (ret==-1) return ret;
} /* timeout */
done += ret;
length -= ret;
}
return done;
}
/***************** client_die ************************/
/** \brief Cleanup code
*
* Called whenever a connection with a client is closed (for any reason).
* Closes all files/sockets.
*/
void client_die(wzd_context_t * context)
{
#ifdef DEBUG
out_log(LEVEL_FLOOD,"client_die(context = %p)\n",context);
#endif
if (context == NULL) return;
if (context->magic != CONTEXT_MAGIC) {
#ifdef DEBUG
out_err(LEVEL_HIGH,"clientThread: context->magic is invalid at exit\n");
#endif
return;
}
/* close opened files */
if (context->current_action.current_file != (fd_t)-1) {
data_end_transfer( (context->current_action.token == TOK_STOR) /* is_upload */, 0 /* end_ok */, context);
}
{
wzd_user_t * user = GetUserByID(context->userid);
wzd_string_t * event_args = NULL;
if (user) {
event_args = STR(user->username);
event_send(mainConfig->event_mgr, EVENT_LOGOUT, 0, event_args, context);
str_deallocate(event_args);
}
}
out_log(LEVEL_INFO,"Client dying (socket %d)\n",context->controlfd);
/* close existing pasv connections */
if (context->pasvsock != (fd_t)-1) {
socket_close(context->pasvsock);
FD_UNREGISTER(context->pasvsock,"Client PASV socket");
context->pasvsock = -1;
}
if (context->datafd != (fd_t)-1) {
#if defined(HAVE_OPENSSL) || defined(HAVE_GNUTLS)
/* if TLS, shutdown TLS before closing data connection */
tls_close_data(context);
#endif
socket_close(context->datafd);
FD_UNREGISTER(context->datafd,"Client data fd");
}
context->datafd = -1;
#if defined(HAVE_OPENSSL) || defined(HAVE_GNUTLS)
/* if TLS, shutdown TLS before closing control connection */
tls_free(context);
#endif
socket_close(context->controlfd);
FD_UNREGISTER(context->controlfd,"Client socket");
context->controlfd = -1;
context_remove(context_list,context);
}
/*************** check_timeout ***********************/
int check_timeout(wzd_context_t * context)
{
time_t t, delay;
wzd_group_t *gptr;
unsigned int i;
int ret;
wzd_user_t * user;
user = GetUserByID(context->userid);
WZD_ASSERT( user != NULL);
if (user == NULL) return 0;
/* reset global ul/dl counters */
mainConfig->global_ul_limiter.bytes_transfered = 0;
#ifndef _MSC_VER
gettimeofday(&(mainConfig->global_ul_limiter.current_time),NULL);
mainConfig->global_dl_limiter.bytes_transfered = 0;
gettimeofday(&(mainConfig->global_dl_limiter.current_time),NULL);
#else
_ftime(&(mainConfig->global_ul_limiter.current_time));
mainConfig->global_dl_limiter.bytes_transfered = 0;
_ftime(&(mainConfig->global_dl_limiter.current_time));
#endif
/* check the timeout of control connection */
t = time(NULL);
delay = t - context->idle_time_start;
/* check timeout if transfer in progress ? */
if (context->current_action.token == TOK_STOR || context->current_action.token == TOK_RETR)
{
time_t data_delay;
data_delay = t - context->idle_time_data_start;
if (data_delay > HARD_XFER_TIMEOUT) {
/* send events here allow sfv checker to mark file as bad if
* partially uploaded
*/
{
wzd_string_t * event_args = str_allocate();
str_sprintf(event_args,"%s %s",user->username,context->current_action.arg);
event_send(mainConfig->event_mgr, EVENT_POSTUPLOAD, 0, event_args, context);
str_deallocate(event_args);
}
file_close(context->current_action.current_file,context);
FD_UNREGISTER(context->current_action.current_file,"Client file (RETR or STOR)");
context->current_action.current_file = -1;
context->current_action.bytesnow = 0;
context->current_action.token = TOK_UNKNOWN;
data_close(context);
ret = send_message(426,context);
/* limiter_free(context->current_limiter);
context->current_limiter = NULL;*/
}
/* during a xfer, connection timeouts are not checked */
return 0;
}
/* if user has 'idle' flag we check nothing */
if (user->flags && strchr(user->flags,FLAG_IDLE))
return 0;
/* first we check user specific timeout */
if (user->max_idle_time>0) {
if (delay > (time_t)user->max_idle_time) {
/* TIMEOUT ! */
send_message_with_args(421,context,"Timeout, closing connection");
{
char inet_str[256];
int af = (context->family == WZD_INET6) ? AF_INET6 : AF_INET;
inet_str[0] = '\0';
inet_ntop(af,context->hostip,inet_str,sizeof(inet_str));
log_message("TIMEOUT","%s (%s) timed out after being idle %d seconds",
user->username,
inet_str,
(int)delay
);
}
kill_child_new(context->pid_child,context);
#ifdef WZD_MULTIPROCESS
exit(0);
#else /* WZD_MULTIPROCESS */
return 0;
#endif /* WZD_MULTIPROCESS */
}
}
/* next we check for all groups */
for (i=0; i<user->group_num; i++) {
gptr = GetGroupByID(user->groups[i]);
if (gptr && gptr->max_idle_time > 0) {
if (delay > (time_t)gptr->max_idle_time) {
/* TIMEOUT ! */
send_message_with_args(421,context,"Timeout, closing connection");
{
char inet_str[256];
int af = (context->family == WZD_INET6) ? AF_INET6 : AF_INET;
inet_str[0] = '\0';
inet_ntop(af,context->hostip,inet_str,sizeof(inet_str));
log_message("TIMEOUT","%s (%s) timed out after being idle %d seconds",
user->username,
inet_str,
(int)delay
);
}
context->exitclient = 1;
#ifdef WZD_MULTIPROCESS
exit(0);
#else /* WZD_MULTIPROCESS */
return 1;
#endif /* WZD_MULTIPROCESS */
}
} /* if max_idle_time*/
}
return 0;
}
/*************** do_chdir ****************************/
int do_chdir(const char * wanted_path, wzd_context_t *context)
{
int ret;
char allowed[WZD_MAX_PATH],path[WZD_MAX_PATH], * ptr;
fs_filestat_t buf;
wzd_user_t * user;
user = GetUserByID(context->userid);
if ( !(user->userperms & RIGHT_CWD) ) return E_NOPERM;
if (!wanted_path) return E_WRONGPATH;
ret = checkpath_new(wanted_path,path,context);
if (ret) return ret;
snprintf(allowed,WZD_MAX_PATH,"%s/",user->rootpath);
/* deny retrieve to permissions file */
if (is_hidden_file(path))
return E_FILE_FORBIDDEN;
REMOVE_TRAILING_SLASH(path);
if (!fs_file_stat(path,&buf)) {
if (S_ISDIR(buf.mode)) {
char buffer[WZD_MAX_PATH], buffer2[WZD_MAX_PATH];
if (wanted_path[0] == '/') { /* absolute path */
wzd_strncpy(buffer,wanted_path,WZD_MAX_PATH);
} else {
wzd_strncpy(buffer,context->currentpath,WZD_MAX_PATH);
if (buffer[strlen(buffer)-1] != '/')
strlcat(buffer,"/",WZD_MAX_PATH);
strlcat(buffer,wanted_path,WZD_MAX_PATH);
}
stripdir(buffer,buffer2,WZD_MAX_PATH-1);
/*out_err(LEVEL_INFO,"DIR: %s NEW DIR: %s\n",buffer,buffer2);*/
wzd_strncpy(context->currentpath,buffer2,WZD_MAX_PATH-1);
}
else return E_NOTDIR;
}
else return E_FILE_NOEXIST;
ptr = stripdir(context->currentpath,path,sizeof(path));
if (ptr) {
wzd_strncpy(context->currentpath,path,WZD_MAX_PATH-1);
}
return E_OK;
}
/*************** childtimeout ************************/
void childtimeout(int nr)
{
exit(0);
}
/*************** waitaccept **************************/
int waitaccept(wzd_context_t * context)
{
fd_set fds;
struct timeval tv;
fd_t sock;
unsigned char remote_host[16];
unsigned int remote_port;
{
wzd_user_t * user;
user = GetUserByID(context->userid);
if (user && strchr(user->flags,FLAG_TLS_DATA) && context->tls_data_mode != TLS_PRIV) {
send_message_with_args(501,context,"Your class must use encrypted data connections");
return -1;
}
}
sock = context->pasvsock;
do {
FD_ZERO(&fds);
FD_SET(sock,&fds);
tv.tv_sec=HARD_XFER_TIMEOUT; tv.tv_usec=0L; /* FIXME - HARD_XFER_TIMEOUT should be a variable */
if (select(sock+1,&fds,NULL,NULL,&tv) <= 0) {
out_err(LEVEL_FLOOD,"accept timeout to client %s:%d.\n",__FILE__,__LINE__);
FD_UNREGISTER(context->pasvsock,"Client PASV socket");
socket_close(context->pasvsock);
context->pasvsock = -1;
send_message_with_args(501,context,"PASV timeout");
return -1;
}
} while (!FD_ISSET(sock,&fds));
sock = socket_accept(context->pasvsock, remote_host, &remote_port, &context->datafamily);
if (sock == (fd_t)-1) {
out_err(LEVEL_FLOOD,"accept failed to client %s:%d.\n",__FILE__,__LINE__);
out_err(LEVEL_FLOOD,"errno is %d:%s.\n",errno,strerror(errno));
FD_UNREGISTER(context->pasvsock,"Client PASV socket");
socket_close(context->pasvsock);
context->pasvsock = -1;
send_message_with_args(501,context,"PASV timeout");
return -1;
}
#if defined(HAVE_OPENSSL) || defined(HAVE_GNUTLS)
if (context->tls_data_mode == TLS_PRIV) {
int ret;
ret = tls_init_datamode(sock, context);
if (ret) {
out_err(LEVEL_INFO,"WARNING TLS data negotiation failed with client %s:%d.\n",__FILE__,__LINE__);
FD_UNREGISTER(context->pasvsock,"Client PASV socket");
socket_close(context->pasvsock);
context->pasvsock = -1;
socket_close(sock);
sock = -1;
send_message_with_args(421,context,"Data connection closed (SSL/TLS negotiation failed).");
return -1;
}
}
#endif
socket_close (context->pasvsock);
FD_UNREGISTER(context->pasvsock,"Client PASV socket");
context->pasvsock = sock;
context->datafd = sock;
context->datamode = DATA_PASV;
return sock;
}
/*************** waitconnect *************************/
int waitconnect(wzd_context_t * context)
{
int sock;
int ret;
{
wzd_user_t * user;
user = GetUserByID(context->userid);
if (user && strchr(user->flags,FLAG_TLS_DATA) && context->tls_data_mode != TLS_PRIV) {
send_message_with_args(501,context,"Your class must use encrypted data connections");
return -1;
}
}
if (context->datafamily == WZD_INET4)
{
/** \todo TODO XXX FIXME check ipv4 IP at this point ! */
ret = send_message(150,context); /* about to open data connection */
sock = socket_connect(context->dataip,context->datafamily,context->dataport,context->localport-1,context->controlfd,HARD_XFER_TIMEOUT);
if (sock == -1) {
ret = send_message(425,context);
return -1;
}
#if defined(HAVE_OPENSSL) || defined(HAVE_GNUTLS)
if (context->tls_data_mode == TLS_PRIV) {
ret = tls_init_datamode(sock, context);
if (ret) {
send_message_with_args(421,context,"Data connection closed (SSL/TLS negotiation failed).");
return -1;
}
}
#endif
} /* context->datafamily == WZD_INET4 */
#if defined(IPV6_SUPPORT)
else if (context->datafamily == WZD_INET6)
{
/** \todo TODO XXX FIXME check ipv6 IP at this point ! */
ret = send_message(150,context); /* about to open data connection */
sock = socket_connect(context->dataip,context->datafamily,context->dataport,context->localport-1,context->controlfd,HARD_XFER_TIMEOUT);
if (sock == -1) {
out_log(LEVEL_FLOOD,"Error establishing PORT connection: %s (%d)\n",strerror(errno),errno);
ret = send_message(425,context);
return -1;
}
#if defined(HAVE_OPENSSL) || defined(HAVE_GNUTLS)
if (context->tls_data_mode == TLS_PRIV) {
ret = tls_init_datamode(sock, context);
if (ret) {
send_message_with_args(421,context,"Data connection closed (SSL/TLS negotiation failed).");
return -1;
}
}
#endif
} /* context->datafamily == WZD_INET6 */
#endif /* IPV6_SUPPORT */
else
{
out_err(LEVEL_CRITICAL,"Invalid protocol %s:%d\n",__FILE__,__LINE__);
ret = send_message(425,context);
return -1;
}
return sock;
}
/*************** list_callback ***********************/
int list_callback(fd_t sock, wzd_context_t * context, char *line)
{
fd_set fds;
struct timeval tv;
do {
FD_ZERO(&fds);
FD_SET(sock,&fds);
tv.tv_sec=HARD_XFER_TIMEOUT; tv.tv_usec=0L; /* FIXME - HARD_XFER_TIMEOUT should be a variable */
if (select(sock+1,NULL,&fds,NULL,&tv) <= 0) {
out_err(LEVEL_FLOOD,"LIST timeout to client.\n");
socket_close(sock);
send_message_with_args(501,context,"LIST timeout");
return 0;
}
} while (!FD_ISSET(sock,&fds));
#if defined(HAVE_OPENSSL) || defined(HAVE_GNUTLS)
if (context->tls_data_mode == TLS_CLEAR)
clear_write(sock,line,strlen(line),0,HARD_XFER_TIMEOUT,context);
else
#endif
(context->write_fct)(sock,line,strlen(line),0,HARD_XFER_TIMEOUT,context);
return 1;
}
/*************** do_list *****************************/
int do_list(wzd_string_t *name, wzd_string_t *arg, wzd_context_t * context)
{
char mask[1024],cmd[WZD_MAX_PATH], *path;
int ret,n;
fd_t sock;
char nullch[8];
char * cmask;
const char * param;
wzd_user_t * user;
enum list_type_t listtype;
user = GetUserByID(context->userid);
if ( !(user->userperms & RIGHT_LIST) ) {
ret = send_message_with_args(550,context,"LIST","No access");
return E_NOPERM;
}
if (!str_checklength(arg, 0, WZD_MAX_PATH-10))
{
ret = send_message_with_args(501,context,"Argument or parameter too big.");
return E_PARAM_BIG;
}
param = str_tochar(arg);
if (context->pasvsock == (fd_t)-1 && context->dataport == 0)
{
ret = send_message_with_args(501,context,"No data connection available.");
return E_NO_DATA_CTX;
}
if (context->state == STATE_XFER) {
ret = send_message(491,context);
return E_XFER_PROGRESS;
}
if (strcasecmp(str_tochar(name),"nlst")==0)
listtype = LIST_TYPE_SHORT;
else
listtype = LIST_TYPE_LONG;
context->resume = 0;
strcpy(nullch,".");
mask[0] = '\0';
if (param) {
while (param[0]=='-') {
n=1;
while (param[n]!=' ' && param[n]!=0) {
switch (param[n]) {
case 'a':
listtype |= LIST_SHOW_HIDDEN;
}
n++;
}
if (param[n]==' ') param = param+n+1;
else param = param+n;
}
wzd_strncpy(cmd,param,sizeof(cmd));
if (cmd[0] != '\0' && cmd[strlen(cmd)-1]=='/') cmd[strlen(cmd)-1]='\0';
if (strrchr(cmd,'*') || strrchr(cmd,'?')) /* wildcards */
{
char *ptr;
if (strrchr(cmd,'/')) { /* probably not in current path - need to readjust path */
if (strrchr(cmd,'/') > strrchr(cmd,'*')) {
/* char / is AFTER *, dir style: toto / * / .., we refuse */
ret = send_message_with_args(501,context,"You can't put wildcards in the middle of path, only in the last part.");
return 1;
}
ptr = strrchr(cmd,'/');
strncpy(cmd,ptr+1,WZD_MAX_PATH);
*ptr = '\0';
} else { /* simple wildcard */
strncpy(mask,cmd,sizeof(mask));
cmd[0] = '\0';
}
}
if (strrchr(cmd,'*') || strrchr(cmd,'?')) { /* wildcards in path ? ough */
ret = send_message_with_args(501,context,"You can't put wildcards in the middle of path, only in the last part.");
return E_PARAM_INVALID;
}
} else { /* no param, assume list of current dir */
cmd[0] = '\0';
param = nullch;
}
if (param[0]=='/') param++;
if (param[0]=='/') {
ret = send_message_with_args(501,context,"Too many / in the path - is it a joke ?");
return E_PARAM_INVALID;
}
cmask = strrchr(mask,'/');
if (cmask) { /* search file in path (with /), but without wildcards */
*cmask='\0';
strlcat(cmd,"/",WZD_MAX_PATH);
strlcat(cmd,mask,WZD_MAX_PATH);
strncpy(mask,cmask,sizeof(mask));
}
/*#ifdef DEBUG
printf("path before: '%s'\n",cmd);
#endif*/
path = wzd_malloc(WZD_MAX_PATH+1);
if (checkpath_new(cmd,path,context) || !strncmp(mask,"..",2)) {
ret = send_message_with_args(501,context,"invalid filter/path");
wzd_free(path);
return E_PARAM_INVALID;
}
REMOVE_TRAILING_SLASH(path);
/*#ifdef DEBUG
printf("path: '%s'\n",path);
#endif*/
/* CHECK PERM */
ret = _checkPerm(path,RIGHT_LIST,user);
if (ret) { /* no access */
ret = send_message_with_args(550,context,"LIST","No access");
wzd_free(path);
return E_NOPERM;
}
if (context->pasvsock == (fd_t)-1) { /* PORT ! */
/** \todo TODO check that ip is correct - no trying to fxp LIST ??!! */
sock = waitconnect(context);
if (sock == (fd_t)-1) {
/* note: reply is done in waitconnect() */
wzd_free(path);
return E_CONNECTTIMEOUT;
}
} else { /* PASV ! */
ret = send_message(150,context); /* about to open data connection */
if ((sock=waitaccept(context)) == (fd_t)-1) {
/* note: reply is done in waitaccept() */
wzd_free(path);
return E_PASV_FAILED;
}
context->pasvsock = -1;
}
FD_REGISTER(sock,"Client LIST socket");
context->state = STATE_XFER;
if (strlen(mask)==0) strcpy(mask,"*");
if (list(sock,context,listtype,path,mask,list_callback))
ret = send_message(226,context);
else
ret = send_message_with_args(501,context,"Error processing list");
wzd_free(path);
#if defined(HAVE_OPENSSL) || defined(HAVE_GNUTLS)
if (context->tls_data_mode == TLS_PRIV)
ret = tls_close_data(context);
#endif
ret = socket_close(sock);
FD_UNREGISTER(sock,"Client LIST socket");
context->datafd = -1;
context->idle_time_start = time(NULL);
context->state = STATE_UNKNOWN;
return E_OK;
}
/*************** do_mlsd *****************************/
int do_mlsd(wzd_string_t *name, wzd_string_t *param, wzd_context_t * context)
{
int ret;
wzd_user_t * user;
fd_t sock;
char * path;
user = GetUserByID(context->userid);
if ( !(user->userperms & RIGHT_LIST) ) {
ret = send_message_with_args(550,context,"MLSD","No access");
return E_NOPERM;
}
if (context->pasvsock == (fd_t)-1 && context->dataport == 0)
{
ret = send_message_with_args(501,context,"No data connection available.");
return E_NO_DATA_CTX;
}
if (context->state == STATE_XFER) {
ret = send_message(491,context);
return E_XFER_PROGRESS;
}
path = wzd_malloc(WZD_MAX_PATH+1);
if (checkpath_new(str_tochar(param),path,context)) {
ret = send_message_with_args(501,context,"invalid path");
wzd_free(path);
return E_PARAM_INVALID;
}
REMOVE_TRAILING_SLASH(path);
/* CHECK PERM */
ret = _checkPerm(path,RIGHT_LIST,user);
if (ret) { /* no access */
ret = send_message_with_args(550,context,"LIST","No access");
wzd_free(path);
return E_NOPERM;
}
if (context->pasvsock == (fd_t)-1) { /* PORT ! */
/** \todo TODO check that ip is correct - no trying to fxp LIST ??!! */
sock = waitconnect(context);
if (sock == (fd_t)-1) {
/* note: reply is done in waitconnect() */
wzd_free(path);
return E_CONNECTTIMEOUT;
}
} else { /* PASV ! */
ret = send_message(150,context); /* about to open data connection */
if ((sock=waitaccept(context)) == (fd_t)-1) {
/* note: reply is done in waitaccept() */
wzd_free(path);
return E_PASV_FAILED;
}
context->pasvsock = -1;
}
FD_REGISTER(sock,"Client MLSD socket");
context->state = STATE_XFER;
if (!mlsd_directory(path,sock,list_callback,context))
ret = send_message(226,context);
else
ret = send_message_with_args(501,context,"Error processing list");
wzd_free(path);
#if defined(HAVE_OPENSSL) || defined(HAVE_GNUTLS)
if (context->tls_data_mode == TLS_PRIV)
ret = tls_close_data(context);
#endif
ret = socket_close(sock);
FD_UNREGISTER(sock,"Client MLSD socket");
context->datafd = -1;
context->idle_time_start = time(NULL);
context->state = STATE_UNKNOWN;
return E_OK;
}
/*************** do_mlst *****************************/
int do_mlst(wzd_string_t *name, wzd_string_t *param, wzd_context_t * context)
{
int ret;
wzd_user_t * user;
char * path;
char * str;
user = GetUserByID(context->userid);
/* stat has the same behaviour as LIST */
if ( !(user->userperms & RIGHT_LIST) ) {
ret = send_message_with_args(550,context,"MLST","No access");
return E_NOPERM;
}
if (!param || strlen(str_tochar(param))==0)
{
ret = send_message_with_args(501,context,"usage: MLST filename");
return E_PARAM_BIG;
}
if (!str_checklength(param, 1, WZD_MAX_PATH-10))
{
ret = send_message_with_args(501,context,"Argument or parameter too big.");
return E_PARAM_BIG;
}
context->state = STATE_COMMAND;
path = wzd_malloc(WZD_MAX_PATH+1);
if (checkpath_new(str_tochar(param),path,context)) {
ret = send_message_with_args(550,context,"incorrect file name",str_tochar(param));
wzd_free(path);
return E_PARAM_INVALID;
}
REMOVE_TRAILING_SLASH(path);
if ( (str = mlst_single_file(path, context)) == NULL) {
ret = send_message_with_args(501,context,"Error occured");
wzd_free(path);
return E_PARAM_INVALID;
}
strcat(str,"\r\n"); /* TODO check size */
{
wzd_string_t * buffer = str_allocate();
str_sprintf(buffer,"250- Listing %s\r\n",str_tochar(param));
send_message_raw(str_tochar(buffer),context);
str_deallocate(buffer);
}
send_message_raw(str,context);
ret = send_message_raw("250 End\r\n",context);
context->idle_time_start = time(NULL);
context->state = STATE_UNKNOWN;
wzd_free(path);
wzd_free(str);
return E_OK;
}
/*************** do_opts *****************************/
int do_opts(wzd_string_t *name, wzd_string_t *param, wzd_context_t * context)
{
const char *ptr;
int ret;
ptr = str_tochar(param);
if (strncasecmp(ptr,"UTF8",4)==0)
{
ptr += 4;
if (*ptr++ != ' ') goto label_opts_error;
#ifdef HAVE_UTF8
if (strncasecmp(ptr,"ON",2)==0)
{
context->connection_flags |= CONNECTION_UTF8;
ret = send_message_with_args(200, context, "UTF8 OPTS ON");
return 0;
}
else if (strncasecmp(ptr,"OFF",2)==0)
{
context->connection_flags &= ~(CONNECTION_UTF8);
ret = send_message_with_args(200, context, "UTF8 OPTS OFF");
return 0;
}
#endif
/* let it go to error return */
} /* UTF8 */
if (strncasecmp(ptr,"MLST",4)==0)
{
/** \todo XXX FIXME implement options support for MLST */
ret = send_message_with_args(200, context, "MLST OPTS Type;Size;Modify;Perm;UNIX.mode;");
return 0;
} /* MLST */
label_opts_error:
ret = send_message_with_args(501,context,"OPTS option not recognized");
return 0;
}
/*************** do_stat *****************************/
int do_stat(wzd_string_t *name, wzd_string_t *arg, wzd_context_t * context)
{
char mask[1024],cmd[WZD_MAX_PATH], *path;
int ret,n;
fd_t sock;
char nullch[8];
char * cmask;
const char *param;
wzd_user_t * user;
enum list_type_t listtype;
tls_data_mode_t old_data_mode;
user = GetUserByID(context->userid);
/* stat has the same behaviour as LIST */
if ( !(user->userperms & RIGHT_LIST) ) {
ret = send_message_with_args(550,context,"LIST","No access");
return E_NOPERM;
}
if (!str_checklength(arg, 1, WZD_MAX_PATH-10))
{
ret = send_message_with_args(501,context,"Argument or parameter too big.");
return E_PARAM_BIG;
}
param = str_tochar(arg);
listtype = LIST_TYPE_LONG;
context->resume = 0;
context->state = STATE_COMMAND;
strcpy(nullch,".");
mask[0] = '\0';
if (param) {
while (param[0]=='-') {
n=1;
while (param[n]!=' ' && param[n]!=0) {
switch (param[n]) {
case 'a':
listtype |= LIST_SHOW_HIDDEN;
}
n++;
}
if (param[n]==' ') param = param+n+1;
else param = param+n;
}
wzd_strncpy(cmd,param,sizeof(cmd));
if (cmd[strlen(cmd)-1]=='/') cmd[strlen(cmd)-1]='\0';
if (strrchr(cmd,'*') || strrchr(cmd,'?')) /* wildcards */
{
char *ptr;
if (strrchr(cmd,'/')) { /* probably not in current path - need to readjust path */
if (strrchr(cmd,'/') > strrchr(cmd,'*')) {
/* char / is AFTER *, dir style: toto / * / .., we refuse */
ret = send_message_with_args(501,context,"You can't put wildcards in the middle of path, only in the last part.");
return 1;
}
ptr = strrchr(cmd,'/');
strncpy(cmd,ptr+1,WZD_MAX_PATH);
*ptr = '\0';
} else { /* simple wildcard */
strncpy(mask,cmd,sizeof(mask));
cmd[0] = '\0';
}
}
if (strrchr(cmd,'*') || strrchr(cmd,'?')) { /* wildcards in path ? ough */
ret = send_message_with_args(501,context,"You can't put wildcards in the middle of path, only in the last part.");
return E_PARAM_INVALID;
}
} else { /* no param, assume list of current dir */
cmd[0] = '\0';
param = nullch;
}
if (param[0]=='/') param++;
if (param[0]=='/') {
ret = send_message_with_args(501,context,"Too many / in the path - is it a joke ?");
return E_PARAM_INVALID;
}
cmask = strrchr(mask,'/');
if (cmask) { /* search file in path (with /), but without wildcards */
*cmask='\0';
strlcat(cmd,"/",WZD_MAX_PATH);
strlcat(cmd,mask,WZD_MAX_PATH);
strncpy(mask,cmask,sizeof(mask));
}
/*#ifdef DEBUG
printf("path before: '%s'\n",cmd);
#endif*/
path = wzd_malloc(WZD_MAX_PATH + 1);
if (checkpath_new(cmd,path,context) || !strncmp(mask,"..",2)) {
ret = send_message_with_args(501,context,"invalid filter/path");
wzd_free(path);
return E_PARAM_INVALID;
}
REMOVE_TRAILING_SLASH(path);
/*#ifdef DEBUG
printf("path: '%s'\n",path);
#endif*/
/* CHECK PERM */
ret = _checkPerm(path,RIGHT_LIST,user);
if (ret) { /* no access */
ret = send_message_with_args(550,context,"STAT","No access");
wzd_free(path);
return E_NOPERM;
}
sock = context->controlfd;
if (strlen(mask)==0) strcpy(mask,"*");
/* \todo XXX FIXME horrible workaround to avoid sending clear data inside ssl stream */
old_data_mode = context->tls_data_mode;
context->tls_data_mode = (context->connection_flags & CONNECTION_TLS) ? TLS_PRIV : TLS_CLEAR;
send_message_raw("213-Status of .:\r\n",context);
send_message_raw("total 0\r\n",context);
if (list(sock,context,listtype,path,mask,list_callback))
ret = send_message_raw("213 End of Status\r\n",context);
else
ret = send_message_raw("213 Error processing list\r\n",context);
context->idle_time_start = time(NULL);
context->state = STATE_UNKNOWN;
context->tls_data_mode = old_data_mode;
wzd_free(path);
return E_OK;
}
/*************** do_mkdir ****************************/
int do_mkdir(wzd_string_t *name, wzd_string_t *arg, wzd_context_t * context)
{
char * cmd = NULL, * path = NULL;
char * buffer = NULL;
int ret;
wzd_user_t * user;
const char *param;
if (!str_checklength(arg,1,WZD_MAX_PATH-1))
{
ret = send_message_with_args(501,context,"invalid path");
return E_PARAM_INVALID;
}
param = str_tochar(arg);
cmd = wzd_malloc(WZD_MAX_PATH+1);
path = wzd_malloc(WZD_MAX_PATH+1);
buffer = wzd_malloc(WZD_MAX_PATH+1);
user = GetUserByID(context->userid);
if ( !(user->userperms & RIGHT_MKDIR) ) { ret = E_NOPERM; goto label_error_mkdir; }
if (strcmp(param,"/")==0) { ret = E_WRONGPATH; goto label_error_mkdir; }
if (param[0] != '/') {
strcpy(cmd,".");
if (checkpath_new(cmd,path,context)) { ret = E_WRONGPATH; goto label_error_mkdir; }
if (path[strlen(path)-1]!='/') strcat(path,"/");
strlcat(path,param,WZD_MAX_PATH);
} else {
wzd_strncpy(cmd,param,WZD_MAX_PATH);
ret = checkpath_new(cmd,path,context);
if (ret != E_FILE_NOEXIST) { ret = E_WRONGPATH; goto label_error_mkdir; }
if (path[strlen(path)-1]!='/') strcat(path,"/");
/* if (path[strlen(path)-1]=='/') path[strlen(path)-1]='\0';*/
}
REMOVE_TRAILING_SLASH(path);
ret = checkpath_new(param,buffer,context);
if (ret != E_FILE_NOEXIST) goto label_error_mkdir;
#if DEBUG
if (ret || errno) {
if (ret != E_FILE_NOEXIST)
out_err(LEVEL_FLOOD,"Making directory '%s' (%d, %s %d %d)\n",buffer,ret,strerror(errno),errno,ENOENT);
switch (ret) {
case E_USER_IDONTEXIST: out_log(LEVEL_HIGH,"mkdir: user does not exist !\n"); break;
case E_PARAM_NULL: out_log(LEVEL_HIGH,"mkdir: no input parameter\n"); break;
case E_PARAM_BIG: out_log(LEVEL_HIGH,"mkdir: parameter too long\n"); break;
case E_WRONGPATH: out_log(LEVEL_HIGH,"mkdir: wrong path\n"); break;
case E_FILE_NOEXIST: break; /* not an error ! */
case E_NOPERM: out_log(LEVEL_HIGH,"mkdir: no permission\n"); break;
default:
break;
}
}
else
out_err(LEVEL_FLOOD,"Making directory '%s' (%d)\n",buffer,ret);
#endif
{
wzd_string_t * event_args = str_allocate();
str_sprintf(event_args,"%s %s",user->username,buffer);
ret = event_send(mainConfig->event_mgr, EVENT_PREMKDIR, 0, event_args, context);
str_deallocate(event_args);
}
if (ret != EVENT_OK && ret != EVENT_BREAK) {
out_log(LEVEL_NORMAL, "Mkdir denied by hook (returned %d)\n", ret);
ret = send_message_with_args(501,context,"Mkdir denied");
return E_XFER_REJECTED;
}
if (buffer[strlen(buffer)-1]=='/')
buffer[strlen(buffer)-1]='\0';
/* deny retrieve to permissions file */
if (is_hidden_file(path)) {
wzd_free(buffer);
wzd_free(path);
wzd_free(cmd);
ret = send_message_with_args(553,context,"forbidden !");
return E_FILE_FORBIDDEN;
}
/** \bug why this test ? it breaks mkdir inside symlinks ! */
/* if (strcmp(path,buffer) != 0) { ret = E_MKDIR_PARSE; goto label_error_mkdir; }*/
/* check section path-filter */
{
char *ptr;
wzd_section_t * section;
wzd_strncpy(path,buffer,WZD_MAX_PATH);
ptr = strrchr(path,'/');
if (ptr && ptr!=&path[0]) {
*ptr='\0';
/* we can reuse cmd */
if (param[0] != '/') {
unsigned int length;
strncpy(cmd,context->currentpath,WZD_MAX_PATH-1-strlen(param));
length = strlen(cmd);
if (cmd[length-1]!='/') {
cmd[length++] = '/';
}
strncpy(cmd+length,param,WZD_MAX_PATH-1-length);
} else {
strncpy(cmd,param,WZD_MAX_PATH);
}
/* we need to give the ftp-relative path here */
section = section_find(mainConfig->section_list,cmd);
if (section && !section_check_filter(section,ptr+1))
{
out_err(LEVEL_FLOOD,"path %s does not match path-filter\n",path);
ret = send_message_with_args(553,context,"dirname does not match pathfilter");
wzd_free(buffer);
wzd_free(path);
wzd_free(cmd);
return E_MKDIR_PATHFILTER;
}
}
}
context->current_action.token = TOK_MKD;
strncpy(context->current_action.arg,buffer,HARD_LAST_COMMAND_LENGTH);
context->current_action.current_file = -1;
ret = file_mkdir(buffer,0755,context); /* TODO umask ? - should have a variable here */
if (ret) {
if (ret != E_NOPERM)
out_err(LEVEL_FLOOD,"mkdir returned %d (%s)\n",errno,strerror(errno));
goto label_error_mkdir; /* keep current ret value for later use */
} else {
const char *groupname=NULL;
if (user->group_num > 0) {
groupname = GetGroupByID(user->groups[0])->groupname;
}
file_chown(buffer,user->username,groupname,context);
/* send message header */
send_message_raw("257- command ok\r\n",context);
{
wzd_string_t * event_args = STR(buffer);
event_send(mainConfig->event_mgr, EVENT_MKDIR, 257, event_args, context);
str_deallocate(event_args);
}
ret = send_message_with_args(257,context,param,"created");
if (param[0] != '/') {
strcpy(buffer,context->currentpath);
strlcat(buffer,"/",WZD_MAX_PATH);
strlcat(buffer,param,WZD_MAX_PATH);
} else {
strcpy(buffer,param);
}
stripdir(buffer,path,WZD_MAX_PATH-1);
log_message("NEWDIR","\"%s\" \"%s\" \"%s\" \"%s\"",
path, /* ftp-absolute path */
user->username,
(groupname)?groupname:"No Group",
user->tagline
);
}
context->idle_time_start = time(NULL);
wzd_free(buffer);
wzd_free(path);
wzd_free(cmd);
return E_OK;
label_error_mkdir:
if (ret == E_NOPERM)
snprintf(buffer,WZD_MAX_PATH-1,"could not create dir: permission denied");
else
snprintf(buffer,WZD_MAX_PATH-1,"could not create dir '%s' (%d)",(param)?param:"(NULL)",ret);
send_message_with_args(553,context,buffer);
wzd_free(buffer);
wzd_free(path);
wzd_free(cmd);
return ret;
}
/*************** do_rmdir ****************************/
int do_rmdir(wzd_string_t *name, wzd_string_t * arg, wzd_context_t * context)
{
char path[WZD_MAX_PATH], buffer[WZD_MAX_PATH];
fs_filestat_t s;
int ret;
wzd_user_t * user;
const char *param;
if (!str_checklength(arg,1,WZD_MAX_PATH-1))
{
ret = send_message_with_args(501,context,"invalid path");
return E_PARAM_INVALID;
}
param = str_tochar(arg);
user = GetUserByID(context->userid);
if ( !(user->userperms & RIGHT_RMDIR) ) { ret = E_NOPERM;; goto label_error_rmdir; }
if (checkpath_new(param,path,context)) { ret = E_WRONGPATH; goto label_error_rmdir; }
/* if path is / terminated, lstat will return the dir itself in case
* of a symlink
*/
if (path[strlen(path)-1]=='/')
path[strlen(path)-1]='\0';
/* deny retrieve to permissions file */
if (is_hidden_file(path)) {
ret = send_message_with_args(553,context,"forbidden !");
return E_FILE_FORBIDDEN;
}
if (fs_file_lstat(path,&s)) { ret = E_FILE_NOEXIST; goto label_error_rmdir; }
if (!S_ISDIR(s.mode)) {
ret = send_message_with_args(553,context,"not a directory");
return E_NOTDIR;
}
/* check permissions */
ret = file_rmdir(path,context);
if (ret) {
out_err(LEVEL_FLOOD,"rmdir returned %d (%s)\n",errno,strerror(errno));
ret = E_PARAM_INVALID; goto label_error_rmdir;
} else {
/* send message header */
send_message_raw("258- command ok\r\n",context);
{
wzd_string_t * event_args = str_allocate();
str_sprintf(event_args,"%s %s",user->username,path);
event_send(mainConfig->event_mgr, EVENT_RMDIR, 258, event_args, context);
str_deallocate(event_args);
}
ret = send_message_with_args(258,context,param,"removed");
{
const char *groupname=NULL;
char tbuf[WZD_MAX_PATH], path[WZD_MAX_PATH];
if (user->group_num > 0) {
groupname = GetGroupByID(user->groups[0])->groupname;
}
if (param[0] != '/') {
strcpy(tbuf,context->currentpath);
strlcat(tbuf,"/",WZD_MAX_PATH);
strlcat(tbuf,param,WZD_MAX_PATH);
} else {
strcpy(tbuf,param);
}
stripdir(tbuf,path,WZD_MAX_PATH-1);
log_message("DELDIR","\"%s\" \"%s\" \"%s\" \"%s\"",
path, /* ftp-absolute path */
user->username,
(groupname)?groupname:"No Group",
user->tagline
);
}
}
context->idle_time_start = time(NULL);
return E_OK;
label_error_rmdir:
snprintf(buffer,WZD_MAX_PATH-1,"could not delete dir '%s'",(param)?param:"(NULL)");
send_message_with_args(553,context,buffer);
return ret;
}
/*************** do_port *****************************/
int do_port(wzd_string_t *name, wzd_string_t *args, wzd_context_t * context)
{
int a0,a1,a2,a3;
unsigned int p1, p2;
int ret;
if (context->pasvsock != (fd_t)-1) {
socket_close(context->pasvsock);
context->pasvsock = -1;
}
if (!args) {
ret = send_message_with_args(501,context,"Invalid parameters");
return E_PARAM_NULL;
}
if ((sscanf(str_tochar(args),"%d,%d,%d,%d,%d,%d",
&a0,&a1,&a2,&a3,
&p1,&p2))<6) {
ret = send_message(502,context);
return E_PARAM_INVALID;
}
context->dataip[0] = (unsigned char)a0;
context->dataip[1] = (unsigned char)a1;
context->dataip[2] = (unsigned char)a2;
context->dataip[3] = (unsigned char)a3;
context->dataport = ((p1&0xff)<<8) | (p2&0xff);
context->datafamily = WZD_INET4;
ret = send_message_with_args(200,context,"Command okay");
return E_OK;
}
/*************** do_pasv *****************************/
int do_pasv(wzd_string_t *name, wzd_string_t *args, wzd_context_t * context)
{
int ret;
unsigned long addr;
unsigned int size,port;
struct sockaddr_in sai;
unsigned char *myip;
unsigned char pasv_bind_ip[16];
unsigned char buffer[16];
int offset=0;
int count=0;
size = sizeof(struct sockaddr_in);
port = mainConfig->pasv_low_range; /* use pasv range min */
/* close existing pasv connections */
if (context->pasvsock != (fd_t)-1) {
socket_close(context->pasvsock);
FD_UNREGISTER(context->pasvsock,"Client PASV socket");
/* port = context->pasvsock+1; *//* FIXME force change of socket */
context->pasvsock = -1;
}
/* create socket */
if ((context->pasvsock=socket(AF_INET,SOCK_STREAM,0)) == (fd_t)-1) {
context->pasvsock = -1;
ret = send_message(425,context);
return E_NO_DATA_CTX;
}
myip = getmyip(context->controlfd, context->family, buffer); /* FIXME use a variable to get pasv ip ? */
if (mainConfig->pasv_ip[0] == 0) {
#if defined(IPV6_SUPPORT)
if (IN6_IS_ADDR_V4MAPPED(PORCUS_CAST(myip)) )
memcpy(pasv_bind_ip,myip+12,4);
else
#endif /* IPV6_SUPPORT */
memcpy(pasv_bind_ip,myip,4);
} else {
#if defined(IPV6_SUPPORT)
if (IN6_IS_ADDR_V4MAPPED(PORCUS_CAST(context->hostip)))
offset = 12;
#endif
/* do NOT send pasv_ip if used from private network */
if (context->hostip[offset+0]==10 ||
(context->hostip[offset+0] == 172 && context->hostip[offset+1] == 16) ||
(context->hostip[offset+0] == 192 && context->hostip[offset+1] == 168 && context->hostip[offset+2] == 0) ||
(context->hostip[offset+0] == 127 && context->hostip[offset+1] == 0 && context->hostip[offset+2] == 0 && context->hostip[offset+3] == 1))
{
#if defined(IPV6_SUPPORT)
if (IN6_IS_ADDR_V4MAPPED(PORCUS_CAST(myip)))
memcpy(pasv_bind_ip,myip+12,4);
else
#endif /* IPV6_SUPPORT */
memcpy(pasv_bind_ip,myip,4);
}
else
#if defined(IPV6_SUPPORT)
if (IN6_IS_ADDR_V4MAPPED(PORCUS_CAST(mainConfig->pasv_ip)))
memcpy(pasv_bind_ip,mainConfig->pasv_ip+12,4);
else
#endif /* IPV6_SUPPORT */
memcpy(pasv_bind_ip,mainConfig->pasv_ip,4);
}
/* out_err(LEVEL_CRITICAL,"PASV_IP: %d.%d.%d.%d\n",
pasv_bind_ip[0], pasv_bind_ip[1], pasv_bind_ip[2], pasv_bind_ip[3]);*/
port = mainConfig->pasv_low_range; /* use pasv range min */
count = mainConfig->pasv_high_range - mainConfig->pasv_low_range;
#ifndef WIN32
port = port + (random()) % count; /* we try to change starting port for random */
#else
port = port + (rand()) % count; /* we try to change starting port for random */
#endif
while (count > 0) { /* use pasv range max */
memset(&sai,0,size);
sai.sin_family = AF_INET;
sai.sin_port = htons((unsigned short)port);
/* XXX TODO FIXME bind to specific address works, but not for NAT */
/* XXX TODO FIXME always bind to 'myip' ?! */
addr = INADDR_ANY;
/* memcpy( (void*)&addr, pasv_bind_ip, sizeof(unsigned long));*/
memcpy(&sai.sin_addr.s_addr,&addr,sizeof(unsigned long));
if (bind(context->pasvsock,(struct sockaddr *)&sai,size)==0) break;
port++; /* retry with next port */
if (port >= mainConfig->pasv_high_range)
port = mainConfig->pasv_low_range;
/** \bug this could create an infinite loop */
}
if (port < mainConfig->pasv_low_range || port > mainConfig->pasv_high_range)
{
out_log(LEVEL_HIGH, "PASV: found port out of range !! (%d not in [%d , %d])\n",
port, mainConfig->pasv_low_range, mainConfig->pasv_high_range);
}
if (port >= 65536) {
socket_close(context->pasvsock);
context->pasvsock = -1;
ret = send_message(425,context);
return E_NO_DATA_CTX;
}
if (listen(context->pasvsock,1)<0) {
out_log(LEVEL_CRITICAL,"Major error during listen: errno %d error %s\n",errno,strerror(errno));
socket_close(context->pasvsock);
context->pasvsock = -1;
ret = send_message(425,context);
return E_NO_DATA_CTX;
}
FD_REGISTER(context->pasvsock,"Client PASV socket");
context->datafamily = WZD_INET4;
myip = getmyip(context->controlfd, context->family, buffer); /* FIXME use a variable to get pasv ip ? */
ret = send_message_with_args(227,context,pasv_bind_ip[0], pasv_bind_ip[1], pasv_bind_ip[2], pasv_bind_ip[3],(port>>8)&0xff, port&0xff);
#if 0
if (mainConfig->pasv_ip[0] == 0) {
ret = send_message_with_args(227,context,myip[0], myip[1], myip[2], myip[3],(port>>8)&0xff, port&0xff);
} else {
/* do NOT send pasv_ip if used from private network */
if (context->hostip[0]==10 ||
(context->hostip[0] == 172 && context->hostip[1] == 16) ||
(context->hostip[0] == 192 && context->hostip[1] == 168 && context->hostip[2] == 0) ||
(context->hostip[0] == 127 && context->hostip[1] == 0 && context->hostip[2] == 0 && context->hostip[3] == 1))
ret = send_message_with_args(227,context,myip[0], myip[1], myip[2], myip[3],(port>>8)&0xff, port&0xff);
else
ret = send_message_with_args(227,context,mainConfig->pasv_ip[0], mainConfig->pasv_ip[1],
mainConfig->pasv_ip[2], mainConfig->pasv_ip[3],(port>>8)&0xff, port&0xff);
}
#endif
if (strcasecmp("cpsv",str_tochar(name))==0)
context->tls_role = TLS_CLIENT_MODE;
return E_OK;
}
/*************** do_eprt *****************************/
int do_eprt(wzd_string_t *name, wzd_string_t *arg, wzd_context_t * context)
{
#if defined(IPV6_SUPPORT)
int ret;
char sep;
char net_prt;
char * net_addr, * s_tcp_port;
char * ptr;
unsigned int tcp_port;
struct in_addr addr4;
struct in6_addr addr6;
char * param, * orig_param;
if (context->pasvsock != (fd_t)-1) {
socket_close(context->pasvsock);
context->pasvsock = -1;
}
/* context->resume = 0; */
if (!arg || strlen(str_tochar(arg)) <= 7) {
ret = send_message(502,context);
ret = send_message_with_args(501,context,"Invalid argument");
return E_PARAM_INVALID;
}
orig_param = param = strdup(str_tochar(arg));
sep = *param++;
net_prt = *param++;
if ( (*param++) != sep || (net_prt != '1' && net_prt != '2') ) {
ret = send_message_with_args(501,context,"Invalid argument");
free(orig_param);
return E_PARAM_INVALID;
}
net_addr = param;
while (*param && (*param) != sep ) param++;
if ( !*param ) {
ret = send_message_with_args(501,context,"Invalid argument");
free(orig_param);
return E_PARAM_INVALID;
}
*param = '\0';
param++;
s_tcp_port = param;
while (*param && (*param) != sep ) param++;
if ( !*param || *param != sep ) {
ret = send_message_with_args(501,context,"Invalid argument");
free(orig_param);
return E_PARAM_INVALID;
}
*param = '\0';
tcp_port = strtoul(s_tcp_port,&ptr,0);
if (*ptr) {
ret = send_message_with_args(501,context,"Invalid port");
free(orig_param);
return E_PARAM_INVALID;
}
/* resolve net_addr to context->dataip */
switch (net_prt - '0') {
case WZD_INET4:
if ( (ret=inet_pton(AF_INET,net_addr,&addr4)) <= 0 )
{
ret = send_message_with_args(501,context,"Invalid host");
free(orig_param);
return E_PARAM_INVALID;
}
memcpy(context->dataip,(const char *)&addr4.s_addr,4);
break;
case WZD_INET6:
if ( (ret=inet_pton(AF_INET6,net_addr,&addr6)) <= 0 )
{
ret = send_message_with_args(501,context,"Invalid host");
free(orig_param);
return E_PARAM_INVALID;
}
memcpy(context->dataip,addr6.s6_addr,16);
break;
default:
ret = send_message_with_args(501,context,"Invalid protocol");
free(orig_param);
return E_PARAM_INVALID;
}
context->dataport = tcp_port;
context->datafamily = net_prt - '0';
free(param);
ret = send_message_with_args(200,context,"Command okay");
#else /* defined(IPV6_SUPPORT) */
send_message(502,context);
#endif
return E_OK;
}
/*************** do_epsv *****************************/
int do_epsv(wzd_string_t *name, wzd_string_t *arg, wzd_context_t * context)
{
int ret;
unsigned int size,port;
#if defined(IPV6_SUPPORT)
struct sockaddr_in6 sai6;
#else
struct sockaddr_in sai;
unsigned long addr;
#endif
unsigned char *myip;
unsigned char pasv_bind_ip[16];
unsigned char buffer[16];
#if !defined(IPV6_SUPPORT)
size = sizeof(struct sockaddr_in);
#else
size = sizeof(struct sockaddr_in6);
#endif
port = mainConfig->pasv_low_range; /* use pasv range min */
/* close existing pasv connections */
if (context->pasvsock != (fd_t)-1) {
socket_close(context->pasvsock);
/* port = context->pasvsock+1; *//* FIXME force change of socket */
context->pasvsock = -1;
}
/* create socket */
#if !defined(IPV6_SUPPORT)
if ((context->pasvsock = socket(PF_INET,SOCK_STREAM,0)) == (fd_t)-1)
#else
if ((context->pasvsock = socket(PF_INET6,SOCK_STREAM,0)) == (fd_t)-1)
#endif
{
context->pasvsock = -1;
ret = send_message(425,context);
return E_NO_DATA_CTX;
}
myip = getmyip(context->controlfd, context->family, buffer); /* FIXME use a variable to get pasv ip ? */
if (mainConfig->pasv_ip[0] == 0) {
memcpy(pasv_bind_ip,myip,sizeof(pasv_bind_ip));
} else {
/* do NOT send pasv_ip if used from private network */
/** \todo TODO XXX FIXME private networks are not the same in ipv6 */
if (context->hostip[0]==10 ||
(context->hostip[0] == 172 && context->hostip[1] == 16) ||
(context->hostip[0] == 192 && context->hostip[1] == 168 && context->hostip[2] == 0) ||
(context->hostip[0] == 127 && context->hostip[1] == 0 && context->hostip[2] == 0 && context->hostip[3] == 1))
memcpy(pasv_bind_ip,myip,sizeof(pasv_bind_ip));
else
memcpy(pasv_bind_ip,mainConfig->pasv_ip,sizeof(pasv_bind_ip));
}
/* out_err(LEVEL_CRITICAL,"PASV_IP: %d.%d.%d.%d\n",
pasv_bind_ip[0], pasv_bind_ip[1], pasv_bind_ip[2], pasv_bind_ip[3]);*/
while (port < mainConfig->pasv_high_range) { /* use pasv range max */
#if !defined(IPV6_SUPPORT)
memset(&sai,0,size);
sai.sin_family = AF_INET;
sai.sin_port = htons((unsigned short)port);
/* XXX TODO FIXME bind to specific address works, but not for NAT */
/* XXX TODO FIXME always bind to 'myip' ?! */
addr = INADDR_ANY;
/* memcpy( (void*)&addr, pasv_bind_ip, sizeof(unsigned long));*/
memcpy(&sai.sin_addr.s_addr,&addr,sizeof(unsigned long));
if (bind(context->pasvsock,(struct sockaddr *)&sai,size)==0) break;
#else /* IPV6_SUPPORT */
memset(&sai6,0,size);
sai6.sin6_family = AF_INET6;
sai6.sin6_port = htons(port);
sai6.sin6_flowinfo = 0;
/* sai6.sin6_addr = in6addr_any;*/ /* FIXME VISUAL */
memset(&sai6.sin6_addr,0,16);
/* XXX TODO FIXME bind to specific address works, but not for NAT */
/* XXX TODO FIXME always bind to 'myip' ?! */
/* addr = INADDR_ANY;*/
/* memcpy(&sai.sin_addr.s_addr,&addr,sizeof(unsigned long));*/
if (bind(context->pasvsock,(struct sockaddr *)&sai6,size)==0) break;
#endif /* IPV6_SUPPORT */
port++; /* retry with next port */
}
if (port >= 65536) {
socket_close(context->pasvsock);
context->pasvsock = -1;
ret = send_message(425,context);
return E_NO_DATA_CTX;
}
if (listen(context->pasvsock,1)<0) {
out_log(LEVEL_CRITICAL,"Major error during listen: errno %d error %s\n",errno,strerror(errno));
socket_close(context->pasvsock);
context->pasvsock = -1;
ret = send_message(425,context);
return E_NO_DATA_CTX;
}
FD_REGISTER(context->pasvsock,"Client PASV socket");
myip = getmyip(context->controlfd, context->family, buffer); /* FIXME use a variable to get pasv ip ? */
#if !defined(IPV6_SUPPORT)
context->datafamily = WZD_INET4;
ret = send_message_with_args(227,context,pasv_bind_ip[0], pasv_bind_ip[1], pasv_bind_ip[2], pasv_bind_ip[3],(port>>8)&0xff, port&0xff);
#else
context->datafamily = WZD_INET6;
{
char buf[256];
snprintf(buf,256,"229 Entering Passive Mode (|||%d|)\r\n",port);
ret = send_message_raw(buf,context);
}
#endif
#if 0
if (mainConfig->pasv_ip[0] == 0) {
ret = send_message_with_args(227,context,myip[0], myip[1], myip[2], myip[3],(port>>8)&0xff, port&0xff);
} else {
/* do NOT send pasv_ip if used from private network */
if (context->hostip[0]==10 ||
(context->hostip[0] == 172 && context->hostip[1] == 16) ||
(context->hostip[0] == 192 && context->hostip[1] == 168 && context->hostip[2] == 0) ||
(context->hostip[0] == 127 && context->hostip[1] == 0 && context->hostip[2] == 0 && context->hostip[3] == 1))
ret = send_message_with_args(227,context,myip[0], myip[1], myip[2], myip[3],(port>>8)&0xff, port&0xff);
else
ret = send_message_with_args(227,context,mainConfig->pasv_ip[0], mainConfig->pasv_ip[1],
mainConfig->pasv_ip[2], mainConfig->pasv_ip[3],(port>>8)&0xff, port&0xff);
}
#endif
return E_OK;
}
/*************** do_retr *****************************/
/** \brief Prepares a data retrieval transfer.
*
* Ensures that a data connection is available, checks user permissions,
* sends EVENT_PREDOWNLOAD, and opens file.
* The real transfer is handled by data_execute().
*/
int do_retr(wzd_string_t *name, wzd_string_t *arg, wzd_context_t * context)
{
char path[WZD_MAX_PATH];
int fd;
u64_t bytestot, bytesnow, byteslast;
fd_t sock;
int ret;
wzd_user_t * user;
const char *param;
connection_state_t restorestate;
param = str_tochar(arg);
user = GetUserByID(context->userid);
if ( !(user->userperms & RIGHT_RETR) ) {
ret = send_message_with_args(550,context,"RETR","No access");
return E_NOPERM;
}
/* TODO FIXME send all error or any in this function ! */
/* we must have a data connetion */
if ((context->pasvsock == (fd_t)-1) && (context->dataport == 0)) {
ret = send_message_with_args(501,context,"No data connection available - issue PORT or PASV first");
return E_NO_DATA_CTX;
}
if (context->state == STATE_XFER) {
ret = send_message(491,context);
return E_XFER_PROGRESS;
}
if (!param || strlen(param)==0) {
ret = send_message_with_args(501,context,"Incorrect filename");
return E_PARAM_INVALID;
}
if (strlen(param)>WZD_MAX_PATH-1) {
ret = send_message_with_args(501,context,"Filename too long");
return E_PARAM_BIG;
}
/*
* Ignore some checkpath_new errorst for the moment,
* test_path will do this after the predownload hook runs
* in case the hook changes something
*/
ret = checkpath_new(param,path,context);
if ((ret != 0) && (ret != E_NOPERM) && (ret != E_FILE_NOEXIST))
{
ret = send_message_with_args(501,context,"Invalid file name");
return E_PARAM_INVALID;
}
/* we need to put context into TOK_RETR state before the hook runs
* so that any cookie parsing in the hook works correctly
*/
restorestate = context->current_action.token;
context->current_action.token = TOK_RETR;
strncpy(context->current_action.arg,path,HARD_LAST_COMMAND_LENGTH);
{
wzd_string_t * event_args = str_allocate();
str_sprintf(event_args,"%s %s",user->username,path);
ret = event_send(mainConfig->event_mgr, EVENT_PREDOWNLOAD, 0, event_args, context);
str_deallocate(event_args);
}
if (ret != EVENT_OK && ret != EVENT_BREAK) {
out_log(LEVEL_NORMAL, "Download denied by hook (returned %d)\n", ret);
ret = send_message_with_args(501,context,"Download denied");
context->current_action.token = restorestate;
return E_XFER_REJECTED;
}
/* restore the context state in case we exit before downloading*/
context->current_action.token = restorestate;
if (test_path(path,context)) {
ret = send_message_with_args(501,context,"Invalid file name");
return E_PARAM_INVALID;
}
/* trailing / ? */
if (path[strlen(path)-1]=='/')
path[strlen(path)-1] = '\0';
/* deny retrieve to permissions file */
if (is_hidden_file(path)) {
ret = send_message_with_args(501,context,"Forbidden");
return E_FILE_FORBIDDEN;
}
/* check user ratio */
if (ratio_check_download(path,context)) {
ret = send_message_with_args(501,context,"Insufficient credits - Upload first");
return E_CREDS_INSUFF;
}
if ((fd=file_open(path,O_RDONLY,RIGHT_RETR,context))==-1) { /* XXX allow access to files being uploaded ? */
ret = send_message_with_args(550,context,param,"nonexistant file or permission denied");
/* socket_close(sock);*/
return E_FILE_NOEXIST;
}
FD_REGISTER(fd,"Client file (RETR)");
/* get length */
bytestot = file_seek(fd,0,SEEK_END);
if ((off_t)bytestot == (off_t)-1) /* happens with 0-length files */
bytestot = 0;
bytesnow = byteslast=context->resume;
if (context->pasvsock == (fd_t)-1) { /* PORT ! */
/* \todo TODO IP-check needed (FXP ?!) */
sock = waitconnect(context);
if (sock == (fd_t)-1) {
file_close(fd,context);
FD_UNREGISTER(fd,"Client file (RETR)");
/* note: reply is done in waitconnect() */
return E_CONNECTTIMEOUT;
}
} else { /* PASV ! */
/* FIXME */
/* sprintf(cmd, "150 Opening BINARY data connection for '%s' (%ld bytes).\r\n",
param, bytestot);*/
ret = send_message(150,context);
if ((sock=waitaccept(context)) == (fd_t)-1) {
file_close(fd,context);
FD_UNREGISTER(fd,"Client file (RETR)");
/* note: reply is done in waitaccept() */
return E_PASV_FAILED;
}
}
FD_REGISTER(sock,"Client data socket (RETR)");
context->datafd = sock;
file_seek(fd,(fs_off_t)context->resume,SEEK_SET);
out_log(LEVEL_FLOOD,"Download: User %s starts downloading %s (%" PRIu64 " bytes)\n", user->username,param,bytestot);
context->state = STATE_XFER;
context->current_action.token = TOK_RETR;
strncpy(context->current_action.arg,path,HARD_LAST_COMMAND_LENGTH);
context->current_action.current_file = fd;
context->current_action.bytesnow = 0;
context->idle_time_data_start = context->current_action.tm_start = time(NULL);
gettimeofday(&context->current_action.tv_start,NULL);
/* if (user->max_dl_speed)
context->current_limiter = limiter_new(user->max_dl_speed);
else
context->current_limiter = NULL;*/
/* if (user->max_dl_speed)
{*/
context->current_dl_limiter.maxspeed = user->max_dl_speed;
context->current_dl_limiter.bytes_transfered = 0;
#ifndef _MSC_VER
gettimeofday(&context->current_dl_limiter.current_time,NULL);
#else
_ftime(&context->current_dl_limiter.current_time);
#endif
/* }
else
context->current_dl_limiter.maxspeed = 0;*/
/* we increment the counter of downloaded files at the beggining
* of the download
*/
user->stats.files_dl_total++;
context->resume=0;
context->idle_time_start = time(NULL);
return E_OK;
}
/*************** do_stor *****************************/
/** \brief Store file on the FTP server
*
* Check permissions, open a data connection and stores data
* in a file on the server. If the file does not exist, it is created,
* otherwise it depends if a resume marker was received (see REST).
*
* The corresponding FTP commands are STOR (RFC959 p29) and APPE (RFC959 p29)
*/
int do_stor(wzd_string_t *name, wzd_string_t *arg, wzd_context_t * context)
{
char path[WZD_MAX_PATH],path2[WZD_MAX_PATH];
int fd;
u64_t bytesnow, byteslast;
fd_t sock;
int ret;
wzd_user_t * user;
const char *param;
unsigned long open_flags;
param = str_tochar(arg);
user = GetUserByID(context->userid);
if ( !(user->userperms & RIGHT_STOR) ) {
ret = send_message_with_args(550,context,"STOR","No access");
return E_NOPERM;
}
/* TODO FIXME send all error or any in this function ! */
/* we must have a data connection */
if ((context->pasvsock == (fd_t)-1) && (context->dataport == 0)) {
ret = send_message_with_args(503,context,"Issue PORT or PASV First");
return E_NO_DATA_CTX;
}
if (context->state == STATE_XFER) {
ret = send_message(491,context);
return E_XFER_PROGRESS;
}
if (!param || strlen(param)==0) {
ret = send_message_with_args(501,context,"Incorrect filename");
return E_PARAM_INVALID;
}
if (strlen(param)>WZD_MAX_PATH-1) {
ret = send_message_with_args(501,context,"Filename too long");
return E_PARAM_BIG;
}
if (param[0]=='/') { /* absolute path */
strcpy(path,user->rootpath);
} else { /* absolute path */
/* FIXME these 2 lines forbids STOR dir/filename style - normal ? */
/* XXX if (strrchr(param,'/'))
param = strrchr(param,'/')+1; XXX */
strcpy(path2,".");
if (checkpath_new(path2,path,context)) {
ret = send_message_with_args(501,context,"Incorrect filename");
return E_PARAM_INVALID;
}
if (path[strlen(path)-1] != '/') strcat(path,"/");
} /* absolute path */
strlcat(path,param,WZD_MAX_PATH);
/* TODO call checkpath again ? see do_mkdir */
/* deny retrieve to permissions file */
if (is_hidden_file(path)) {
ret = send_message_with_args(501,context,"Forbidden");
return E_FILE_FORBIDDEN;
}
{
wzd_string_t * event_args = str_allocate();
str_sprintf(event_args,"%s %s",user->username,path);
ret = event_send(mainConfig->event_mgr, EVENT_PREUPLOAD, 0, event_args, context);
str_deallocate(event_args);
}
if (ret != EVENT_OK && ret != EVENT_BREAK) {
out_log(LEVEL_NORMAL, "Upload denied by hook (returned %d)\n", ret);
ret = send_message_with_args(501,context,"Upload denied");
return E_XFER_REJECTED;
}
/* overwrite protection */
/* TODO make permissions per-dir + per-group + per-user ? */
/* if (context->userinfo.perms & PERM_OVERWRITE) {
fp=file_open(path,"r",RIGHT_STOR,context),
if (!fp) {
fclose(fp);
return 2;
}
}*/
if (strcasecmp(str_tochar(name),"appe")==0)
context->resume = (unsigned long)-1;
open_flags = O_WRONLY|O_CREAT;
/** If we don't resume a previous upload, we have to truncate the current file
* or we won't be able to overwrite a file by a smaller one
*/
if (context->resume == 0)
open_flags |= O_TRUNC;
if ((fd=file_open(path,open_flags,RIGHT_STOR,context))==-1) { /* XXX allow access to files being uploaded ? */
ret = send_message_with_args(501,context,"nonexistant file or permission denied");
return E_FILE_NOEXIST;
}
FD_REGISTER(fd,"Client file (STOR)");
if (context->pasvsock == (fd_t)-1) { /* PORT ! */
/* \todo TODO IP-check needed (FXP ?!) */
sock = waitconnect(context);
if (sock == (fd_t)-1) {
file_close(fd,context);
FD_UNREGISTER(fd,"Client file (STOR)");
/* note: reply is done in waitconnect() */
return E_CONNECTTIMEOUT;
}
} else { /* PASV ! */
/* FIXME */
/* sprintf(cmd, "150 Opening BINARY data connection for '%s'.\r\n",
param);*/
ret = send_message(150,context);
if ((sock=waitaccept(context)) == (fd_t)-1) {
file_close(fd,context);
FD_UNREGISTER(fd,"Client file (STOR)");
/* note: reply is done in waitaccept() */
return E_PASV_FAILED;
}
}
FD_REGISTER(sock,"Client data socket (STOR)");
context->datafd = sock;
/* set owner */
{
const char *groupname=NULL;
if (user->group_num > 0) {
groupname = GetGroupByID(user->groups[0])->groupname;
}
file_chown (path,user->username,groupname,context);
}
bytesnow = byteslast = 0;
if (context->resume == (unsigned long)-1)
file_seek(fd,0,SEEK_END);
else
file_seek(fd,(fs_off_t)context->resume,SEEK_SET);
out_err(LEVEL_FLOOD,"Download: User %s starts uploading %s\n",
user->username,param);
context->state = STATE_XFER;
context->current_action.token = TOK_STOR;
strncpy(context->current_action.arg,path,HARD_LAST_COMMAND_LENGTH);
context->current_action.current_file = fd;
context->current_action.bytesnow = 0;
context->idle_time_data_start = context->current_action.tm_start = time(NULL);
gettimeofday(&context->current_action.tv_start,NULL);
context->current_ul_limiter.maxspeed = user->max_ul_speed;
context->current_ul_limiter.bytes_transfered = 0;
#ifndef WIN32 /* FIXME VISUAL */
gettimeofday(&context->current_ul_limiter.current_time,NULL);
#else
_ftime(&context->current_ul_limiter.current_time);
#endif
context->resume=0;
context->idle_time_start = time(NULL);
return E_OK;
}
/*************** do_mdtm *****************************/
int do_mdtm(wzd_string_t *name, wzd_string_t *param, wzd_context_t * context)
{
char path[WZD_MAX_PATH], tm[32];
fs_filestat_t s;
int ret;
if (!str_checklength(param,1,WZD_MAX_PATH-1)) {
ret = send_message_with_args(501,context,"Incorrect argument");
return E_PARAM_INVALID;
}
if (!checkpath_new(str_tochar(param),path,context)) {
if (path[strlen(path)-1]=='/')
path[strlen(path)-1]='\0';
/* deny retrieve to permissions file */
if (is_hidden_file(path)) {
ret = send_message_with_args(501,context,"Forbidden");
return E_FILE_FORBIDDEN;
}
if (fs_file_stat(path,&s)==0) {
context->resume = 0L;
strftime(tm,sizeof(tm),"%Y%m%d%H%M%S",gmtime(&s.mtime));
ret = send_message_with_args(213,context,tm);
return E_OK;
}
}
ret = send_message_with_args(501,context,"File inexistant or no access ?");
return E_FILE_NOEXIST;
}
/*************** do_size *****************************/
int do_moda(wzd_string_t *name, wzd_string_t *param, wzd_context_t * context)
{
#ifdef HAVE_STRPTIME
extern char *strptime (__const char *__restrict __s,
__const char *__restrict __fmt, struct tm *__tp);
#endif
int ret, command_ok=0;
char * facts, * fact, * value, * ptr;
struct tm tm_atime, tm_mtime;
struct utimbuf utime_buf = {0, 0};
char * filename;
char path[WZD_MAX_PATH];
if (!param) {
ret = send_message_with_args(501,context,"Invalid syntax");
return E_PARAM_INVALID;
}
facts = strdup(str_tochar(param));
filename = strstr(facts,"; ");
if (!filename) {
free(facts);
ret = send_message_with_args(501,context,"Invalid syntax");
return E_PARAM_INVALID;
}
filename++; /* skip ';' */
*filename++ = '\0';
if (checkpath_new(filename,path,context)) {
free(facts);
ret = send_message_with_args(501,context,"Invalid filename");
return E_PARAM_INVALID;
}
if (path[strlen(path)-1]=='/')
path[strlen(path)-1]='\0';
/** \todo XXX open file to avoid race conditions */
fact = strtok_r(facts,"=",&ptr);
if (!fact) {
free(facts);
ret = send_message_with_args(501,context,"Invalid syntax");
return E_PARAM_INVALID;
}
while (fact) {
value = strtok_r(NULL,";",&ptr);
if (!value) {
free(facts);
ret = send_message_with_args(501,context,"Invalid syntax");
return E_PARAM_INVALID;
}
/* test 'fact' and make action */
/** \todo XXX it would be a good idea to make 'atomic' modifications, or to lock file ! */
/**** accessed *******/
if (strcmp(fact,"accessed")==0) {
memset(&tm_atime,0,sizeof(struct tm));
ptr=strptime(value,"%Y%m%d%H%M%S",&tm_atime);
if (ptr == NULL || *ptr != '\0') {
snprintf(path,WZD_MAX_PATH,"Invalid value for fact '%s', aborting",fact);
ret = send_message_with_args(501,context,path);
return E_PARAM_INVALID;
}
utime_buf.actime = mktime(&tm_atime);
ret = utime(path,&utime_buf);
if (ret) {
snprintf(path,WZD_MAX_PATH,"Error in fact %s: '%s', aborting",fact,value);
free(facts);
ret = send_message_with_args(501,context,path);
return E_PARAM_INVALID;
}
command_ok++;
} else
/**** modify *******/
if (strcmp(fact,"modify")==0) {
memset(&tm_mtime,0,sizeof(struct tm));
ptr=strptime(value,"%Y%m%d%H%M%S",&tm_mtime);
if (ptr == NULL || *ptr != '\0') {
snprintf(path,WZD_MAX_PATH,"Invalid value for fact '%s', aborting",fact);
ret = send_message_with_args(501,context,path);
return E_PARAM_INVALID;
}
utime_buf.modtime = mktime(&tm_mtime);
ret = utime(path,&utime_buf);
if (ret) {
snprintf(path,WZD_MAX_PATH,"Error in fact %s: '%s', aborting",fact,value);
free(facts);
ret = send_message_with_args(501,context,path);
return E_PARAM_INVALID;
}
command_ok++;
} else
/**** unknown *******/
{
snprintf(path,WZD_MAX_PATH,"Unsupported fact '%s', aborting",fact);
free(facts);
ret = send_message_with_args(501,context,path);
return E_PARAM_INVALID;
}
fact = strtok_r(NULL,"=",&ptr);
}
free(facts);
if (command_ok)
ret = send_message_with_args(200,context,"Command okay");
else
ret = send_message_with_args(501,context,"Not yet implemented");
return E_PARAM_INVALID;
}
/*************** do_size *****************************/
int do_size(wzd_string_t *name, wzd_string_t *param, wzd_context_t * context)
{
char path[WZD_MAX_PATH];
char buffer[1024];
fs_filestat_t s;
int ret;
if (!str_checklength(param,1,WZD_MAX_PATH-1)) {
ret = send_message_with_args(501,context,"Incorrect argument");
return E_PARAM_INVALID;
}
if (!checkpath_new(str_tochar(param),path,context)) {
if (path[strlen(path)-1]=='/')
path[strlen(path)-1]='\0';
/* deny retrieve to permissions file */
if (is_hidden_file(path)) {
ret = send_message_with_args(501,context,"Forbidden");
return E_FILE_FORBIDDEN;
}
if (fs_file_stat(path,&s)==0) {
snprintf(buffer,1024,"%" PRIu64,s.size);
ret = send_message_with_args(213,context,buffer);
return E_OK;
}
}
ret = send_message_with_args(501,context,"File inexistant or no access ?");
return E_FILE_NOEXIST;
}
/*************** do_abor *****************************/
/** \brief Abort current transfer
*
* Abort current service command and any associated transfer of data.
* The command connection is not closed, but the data connection is closed.
*
* The corresponding FTP command is ABOR (RFC959 p30)
*/
int do_abor(wzd_string_t *name, wzd_string_t *arg, wzd_context_t * context)
{
int ret;
wzd_user_t * user;
user = GetUserByID(context->userid);
if (context->pasvsock != (fd_t)-1 && context->datafd != context->pasvsock) {
socket_close(context->pasvsock);
FD_UNREGISTER(context->pasvsock,"Client PASV socket");
context->pasvsock=-1;
}
if (context->current_action.current_file != (fd_t)-1) {
/* transfer aborted, we should send a 426 */
ret = send_message(426,context);
out_xferlog(context, 0 /* incomplete */);
if (context->current_action.token == TOK_STOR || context->current_action.token == TOK_RETR) {
data_end_transfer((context->current_action.token == TOK_STOR) ? 1:0 /* is_upload */, 0 /* end_ok */, context);
}
}
ret = send_message(226,context);
return E_OK;
}
/*************** do_cwd ******************************/
int do_cwd(wzd_string_t *name, wzd_string_t *arg, wzd_context_t * context)
{
int ret;
const char *param;
param = str_tochar(arg);
context->resume = 0;
if (strcmp(str_tochar(name),"cdup")==0) param="..";
if (param == NULL) {
param = "/";
}
/* avoir error if current is "/" and action is ".." */
if (!strcmp("..",param)
&& ( !strcmp("/",context->currentpath)
|| ( (strlen(context->currentpath)<=3) && (context->currentpath[2]==':') ) )
) {
ret = send_message_with_args(250,context,context->currentpath," now current directory.");
return E_OK;
}
if ( (ret=do_chdir(param,context)) ) {
switch (ret) {
case E_NOTDIR:
ret = send_message_with_args(550,context,param?param:"(null)","Not a directory");
break;
case E_WRONGPATH:
ret = send_message_with_args(550,context,param?param:"(null)","Invalid path");
break;
case E_FILE_NOEXIST:
ret = send_message_with_args(550,context,param?param:"(null)","No such file or directory (no access ?)");
break;
case E_FILE_FORBIDDEN:
case E_NOPERM:
ret = send_message_with_args(550,context,param?param:"(null)","Negative on that, Houston (access denied)");
break;
default:
ret = send_message_with_args(550,context,param?param:"(null)","chdir FAILED");
break;
}
return E_OK;
}
/** \bug we have to ensure that the reply is RFC compliant */
ret = send_message_with_args(250,context,context->currentpath," now current directory.");
return E_OK;
}
/*************** do_dele *****************************/
int do_dele(wzd_string_t *name, wzd_string_t *param, wzd_context_t * context)
{
char path[WZD_MAX_PATH];
int ret;
fs_filestat_t s;
u64_t file_size;
wzd_user_t * user, * owner;
if (!str_checklength(param,1,WZD_MAX_PATH-1)) {
ret = send_message_with_args(501,context,"Syntax error");
return E_PARAM_INVALID;
}
user = GetUserByID(context->userid);
if (!user) {
ret = send_message_with_args(501,context,"Mama says I don't exist !");
return E_USER_IDONTEXIST;
}
if ( !(user->userperms & RIGHT_DELE) ) {
ret = send_message_with_args(501,context,"Permission denied");
return E_NOPERM;
}
if (checkpath_new(str_tochar(param),path,context)) {
ret = send_message_with_args(501,context,"Permission denied or inexistant file");
return E_FILE_NOEXIST;
}
if (path[strlen(path)-1]=='/') path[strlen(path)-1]='\0';
/* deny retrieve to permissions file */
if (is_hidden_file(path)) {
ret = send_message_with_args(501,context,"Forbidden");
return E_FILE_FORBIDDEN;
}
if (fs_file_lstat(path,&s)) {
/* non-existent file ? */
ret = send_message_with_args(501,context,"File does not exist");
return E_FILE_NOEXIST;
}
if (S_ISDIR(s.mode)) {
ret = send_message_with_args(501,context,"This is a directory !");
return E_ISDIR;
}
if (S_ISREG(s.mode))
file_size = s.size;
else
file_size = 0;
owner = file_getowner(path,context);
context->current_action.token = TOK_DELE;
out_err(LEVEL_FLOOD,"Removing file '%s'\n",path);
ret = file_remove(path,context);
/* decrement user credits and upload stats */
/* we should adjust stats for REAL OWNER of file */
if (!ret && file_size)
{
if (owner) {
if (strcmp(owner->username,"nobody"))
{
if (owner->ratio) {
if (owner->credits > owner->ratio*file_size)
owner->credits -= (owner->ratio * file_size);
else
owner->credits = 0;
}
if (owner->stats.bytes_ul_total > file_size)
owner->stats.bytes_ul_total -= file_size;
else
owner->stats.bytes_ul_total = 0;
if (owner->stats.files_ul_total)
owner->stats.files_ul_total--;
}
}
}
if (!ret) {
send_message_raw("250- command ok\r\n",context);
{
wzd_string_t * event_args = STR(path);
event_send(mainConfig->event_mgr, EVENT_DELE, 250, event_args, context);
str_deallocate(event_args);
}
ret = send_message_with_args(250,context,"DELE"," command successfull");
context->idle_time_start = time(NULL);
} else
ret = send_message_with_args(501,context,"DELE failed");
context->current_action.token = TOK_UNKNOWN;
return ret;
}
/*************** do_pret *****************************/
int do_pret(wzd_string_t *name, wzd_string_t *param, wzd_context_t * context)
{
int ret;
/* TODO XXX FIXME PRET *MUST* be sent before the PASV command */
/* TODO check next token (RETR STOR STOU LIST NLST APPE) and
* run specific commands ...
*/
/* e.g: if RETR, open file to have it in cache ? */
ret = send_message_with_args(200,context,"Command OK");
return E_OK;
}
/*************** do_print_message ********************/
int do_print_message(wzd_string_t *name, wzd_string_t *filename, wzd_context_t * context)
{
int cmd;
int ret;
char buffer[WZD_BUFFER_LEN];
wzd_string_t * str;
cmd = identify_token(str_tochar(name));
switch (cmd) {
case TOK_PWD:
context->resume = 0;
/** \todo allow msg 257 customization */
/*ret = send_message(257,context);*/
str = str_allocate();
str_sprintf(str,"257 \"%s\" is current directory.\r\n",context->currentpath);
#ifdef HAVE_UTF8
if (context->connection_flags & CONNECTION_UTF8)
{
if (!str_is_valid_utf8(str))
str_local_to_utf8(str,local_charset());
}
#endif
ret = send_message_raw(str_tochar(str),context);
str_deallocate(str);
break;
case TOK_ALLO:
case TOK_NOOP:
ret = send_message_with_args(200,context,"Command okay");
break;
case TOK_FEAT:
snprintf(buffer,sizeof(buffer),"Extensions supported:\n%s",SUPPORTED_FEATURES);
ret = send_message_with_args(211,context,buffer);
break;
case TOK_SYST:
context->resume = 0;
ret = send_message(215,context);
break;
}
return E_OK;
}
#if defined(HAVE_OPENSSL) || defined(HAVE_GNUTLS)
/*************** do_pbsz *****************************/
int do_pbsz(wzd_string_t *name, wzd_string_t *param, wzd_context_t * context)
{
int ret;
const char *arg;
arg = str_tochar(param);
/** \todo TOK_BSZ: if user is NOT in TLS mode, insult him */
/** \todo TOK_BSZ: use argument */
ret = send_message_with_args(200,context,"PBSZ command OK");
return E_OK;
}
#else
int do_pbsz(wzd_string_t *name, wzd_string_t *arg, wzd_context_t * context)
{
send_message(502,context);
return E_PARAM_INVALID;
}
#endif
#if defined(HAVE_OPENSSL) || defined(HAVE_GNUTLS)
/*************** do_prot *****************************/
int do_prot(wzd_string_t *name, wzd_string_t *param, wzd_context_t * context)
{
int ret;
const char *arg;
arg = str_tochar(param);
/** \todo TOK_PROT: if user is NOT in TLS mode, insult him */
if (strcasecmp("P",arg)==0)
context->tls_data_mode = TLS_PRIV;
else if (strcasecmp("C",arg)==0)
context->tls_data_mode = TLS_CLEAR;
else {
ret = send_message_with_args(550,context,"PROT","must be C or P");
return E_PARAM_INVALID;
}
ret = send_message_with_args(200,context,"PROT command OK");
return E_OK;
}
#else
int do_prot(wzd_string_t *name, wzd_string_t *arg, wzd_context_t * context)
{
send_message(502,context);
return E_PARAM_INVALID;
}
#endif
#if defined(HAVE_OPENSSL) || defined(HAVE_GNUTLS)
/*************** do_sscn *****************************/
int do_sscn(wzd_string_t *name, wzd_string_t *param, wzd_context_t * context)
{
int ret;
const char *arg;
arg = str_tochar(param);
if (!arg || strlen(arg)==0 || strcasecmp(arg,"off")==0) {
context->tls_role = TLS_SERVER_MODE;
ret = send_message_with_args(200,context,"SSCN:SERVER METHOD");
return E_OK;
}
if (strcasecmp(arg,"on")==0) {
context->tls_role = TLS_CLIENT_MODE;
ret = send_message_with_args(200,context,"SSCN:CLIENT METHOD");
return E_OK;
}
ret = send_message_with_args(550,context,"SSCN","Invalid argument");
return E_PARAM_INVALID;
}
#else
int do_sscn(wzd_string_t *name, wzd_string_t *arg, wzd_context_t * context)
{
return E_PARAM_INVALID;
}
#endif
/*************** do_quit *****************************/
int do_quit(wzd_string_t *name, wzd_string_t *arg, wzd_context_t * context)
{
int ret;
ret = send_message(221,context);
{
const char * groupname = NULL;
wzd_user_t * user;
const char * remote_host;
struct hostent *h;
char inet_str[256];
int af = (context->family == WZD_INET6) ? AF_INET6 : AF_INET;
user = GetUserByID(context->userid);
if (user->group_num > 0) groupname = GetGroupByID(user->groups[0])->groupname;
inet_str[0] = '\0';
inet_ntop(af,context->hostip,inet_str,sizeof(inet_str));
h = gethostbyaddr((char*)&context->hostip,sizeof(context->hostip),af);
if (h==NULL)
remote_host = inet_str;
else
remote_host = h->h_name;
log_message("LOGOUT","%s (%s) \"%s\" \"%s\" \"%s\"",
remote_host,
inet_str,
user->username,
(groupname)?groupname:"No Group",
user->tagline
);
}
context->exitclient=1;
/* check if pending xfers */
return E_OK;
}
/*************** do_rest *****************************/
int do_rest(wzd_string_t *name, wzd_string_t *arg, wzd_context_t * context)
{
int ret;
u64_t ull;
char *ptr;
if (!arg) {
ret = send_message_with_args(501,context,"Invalid REST marker");
return E_PARAM_INVALID;
}
ull = strtoull(str_tochar(arg), &ptr, 0);
if (ptr==str_tochar(arg) || *ptr!='\0')
{
ret = send_message_with_args(501,context,"Invalid REST marker");
return E_PARAM_INVALID;
} else {
char buf[256];
snprintf(buf,256,"Restarting at %" PRIu64 ". Send STORE or RETRIEVE.",ull);
ret = send_message_with_args(350,context,buf);
context->resume = ull;
}
return E_OK;
}
/*************** do_rnfr *****************************/
int do_rnfr(wzd_string_t *name, wzd_string_t *filename, wzd_context_t * context)
{
char path[WZD_MAX_PATH];
int ret;
wzd_user_t * user;
user = GetUserByID(context->userid);
if (!user || !(user->userperms & RIGHT_RNFR)) {
ret = send_message_with_args(550,context,"RNFR","permission denied");
return E_FILE_NOEXIST;
}
if (!filename || strlen(str_tochar(filename))==0 || strlen(str_tochar(filename))>=WZD_MAX_PATH || checkpath_new(str_tochar(filename),path,context)) {
ret = send_message_with_args(550,context,"RNFR","file does not exist");
return E_FILE_NOEXIST;
}
if (path[strlen(path)-1]=='/') path[strlen(path)-1]='\0';
/* deny retrieve to permissions file */
if (is_hidden_file(path)) {
ret = send_message_with_args(501,context,"Forbidden");
return E_FILE_FORBIDDEN;
}
context->current_action.token = TOK_RNFR;
strncpy(context->current_action.arg,path,HARD_LAST_COMMAND_LENGTH);
context->current_action.current_file = -1;
context->current_action.bytesnow = 0;
context->current_action.tm_start = time(NULL);
ret = send_message_with_args(350,context,"OK, send RNTO");
return E_OK;
}
/*************** do_rnto *****************************/
int do_rnto(wzd_string_t *name, wzd_string_t *filename, wzd_context_t * context)
{
char path[WZD_MAX_PATH];
int ret;
wzd_user_t * user;
user = GetUserByID(context->userid);
if (!user || !(user->userperms & RIGHT_RNFR)) {
ret = send_message_with_args(550,context,"RNTO","permission denied");
return E_FILE_NOEXIST;
}
if (!filename || strlen(str_tochar(filename))==0 || strlen(str_tochar(filename))>=WZD_MAX_PATH) {
ret = send_message_with_args(553,context,"RNTO","wrong file name ?");
return E_PARAM_INVALID;
}
if (context->current_action.token != TOK_RNFR) {
ret = send_message_with_args(553,context,"RNTO","send RNFR before !");
return E_PARAM_INVALID;
}
checkpath_new(str_tochar(filename),path,context);
if (path[strlen(path)-1]=='/') path[strlen(path)-1]='\0';
/* deny retrieve to permissions file */
if (is_hidden_file(path)) {
ret = send_message_with_args(501,context,"Forbidden");
return E_FILE_FORBIDDEN;
}
context->current_action.token = TOK_UNKNOWN;
context->current_action.current_file = -1;
context->current_action.bytesnow = 0;
ret = file_rename(context->current_action.arg,path,context);
if (ret) {
ret = send_message_with_args(550,context,"RNTO","command failed");
} else {
ret = send_message_with_args(250,context,"RNTO"," command OK");
context->idle_time_start = time(NULL);
}
return E_OK;
}
/*************** do_type *****************************/
int do_type(wzd_string_t *name, wzd_string_t *param, wzd_context_t * context)
{
int ret;
context->resume = 0;
if (!param) {
ret = send_message_with_args(501,context,"Invalid TYPE marker");
return E_PARAM_INVALID;
}
if (strcasecmp(str_tochar(param),"I")==0)
context->current_xfer_type = BINARY;
else if (strcasecmp(str_tochar(param),"A")==0)
context->current_xfer_type = ASCII;
else {
ret = send_message(502,context);
return E_PARAM_INVALID;
}
ret = send_message_with_args(200,context,"Command okay");
return E_OK;
}
/*************** do_xcrc *****************************/
int do_xcrc(wzd_string_t *name, wzd_string_t *arg, wzd_context_t * context)
{
char path[WZD_MAX_PATH];
char buffer[1024];
const char * ptr;
char * ptest;
fs_filestat_t s;
int ret;
unsigned long crc = 0;
unsigned long startpos = 0;
unsigned long length = (unsigned long)-1;
const char *param;
if (!str_checklength(arg,1,WZD_MAX_PATH-1)) {
ret = send_message_with_args(501,context,"Syntax error");
return E_PARAM_INVALID;
}
param = str_tochar(arg);
/* get filename and args:
* "filename" must be quoted
* startpos and length are optional
*/
ptr = param;
if (*ptr == '"') {
ptr++;
while (*ptr && *ptr != '"') ptr++;
if (!*ptr) {
ret = send_message_with_args(501,context,"Syntax error");
return E_PARAM_INVALID;
}
memcpy(buffer,param+1,ptr-param-1);
buffer[ptr-param-1] = '\0';
ptr++;
/* optional: read startpos AND length */
startpos = strtoul(ptr,&ptest,0);
if (ptest && ptest != ptr)
{
ptr = ptest;
length = strtoul(ptr,&ptest,0);
if (!ptest || ptest == ptr) {
ret = send_message_with_args(501,context,"Syntax error");
return E_PARAM_INVALID;
} else { /* optional: read start checksum */
ptr = ptest;
crc = strtoul(ptr,&ptest,16);
if (!ptest || ptest == ptr)
crc = 0;
}
} else
startpos = 0;
param = buffer;
}
if (!checkpath_new(param,path,context)) {
if (path[strlen(path)-1]=='/')
path[strlen(path)-1]='\0';
/* deny retrieve to permissions file */
if (is_hidden_file(path)) {
ret = send_message_with_args(501,context,"Forbidden");
return E_FILE_FORBIDDEN;
}
if (fs_file_stat(path,&s)==0) {
ret = calc_crc32(path,&crc,startpos,length);
snprintf(buffer,1024,"%lX",crc);
/* snprintf(buffer,1024,"%d %lX\r\n",250,crc);*/
/* ret = send_message_raw(buffer,context);*/
ret = send_message_with_args(250,context,buffer,"");
return E_OK;
}
}
ret = send_message_with_args(550,context,"XCRC","File inexistant or no access ?");
return E_FILE_NOEXIST;
}
/*************** do_xmd5 *****************************/
int do_xmd5(wzd_string_t *name, wzd_string_t *arg, wzd_context_t * context)
{
char path[WZD_MAX_PATH];
char buffer[1024];
const char * ptr;
char * ptest;
fs_filestat_t s;
int ret;
unsigned char crc[16];
char md5str[33];
unsigned long startpos = 0;
unsigned long length = (unsigned long)-1;
unsigned int i;
const char *param;
if (!str_checklength(arg,1,WZD_MAX_PATH-1)) {
ret = send_message_with_args(501,context,"Syntax error");
return E_PARAM_INVALID;
}
param = str_tochar(arg);
for (i=0; i<16; i++)
crc[i] = 0;
/* get filename and args:
* "filename" must be quoted
* startpos and length are optional
*/
ptr = param;
if (*ptr == '"') {
ptr++;
while (*ptr && *ptr != '"') ptr++;
if (!*ptr) {
ret = send_message_with_args(501,context,"Syntax error");
return E_PARAM_INVALID;
}
memcpy(buffer,param+1,ptr-param-1);
buffer[ptr-param-1] = '\0';
ptr++;
/* optional: read startpos AND length */
startpos = strtoul(ptr,&ptest,0);
if (ptest && ptest != ptr)
{
ptr = ptest;
length = strtoul(ptr,&ptest,0);
if (!ptest || ptest == ptr) {
ret = send_message_with_args(501,context,"Syntax error");
return E_PARAM_INVALID;
} else { /* optional: read start checksum */
ptr = ptest;
strtomd5((char*)ptr,&ptest,crc);
if (!ptest || ptest == ptr)
memset(crc,0,16);
}
} else
startpos = 0;
param = buffer;
}
if (!checkpath_new(param,path,context)) {
if (path[strlen(path)-1]=='/')
path[strlen(path)-1]='\0';
/* deny retrieve to permissions file */
if (is_hidden_file(path)) {
ret = send_message_with_args(501,context,"Forbidden");
return E_FILE_FORBIDDEN;
}
if (fs_file_stat(path,&s)==0) {
ret = calc_md5(path,crc,startpos,length);
for (i=0; i<16; i++)
snprintf(md5str+i*2,3,"%02x",crc[i]);
ret = send_message_with_args(250,context,md5str,"");
return E_OK;
}
}
ret = send_message_with_args(550,context,"XMD5","File inexistant or no access ?");
return E_FILE_NOEXIST;
}
/*************** do_help *****************************/
int do_help(wzd_string_t *name, wzd_string_t *arg, wzd_context_t * context)
{
/* TODO maybe add HELP SITE? */
send_message_with_args(214,context);
return E_OK;
}
/*****************************************************/
/*************** client main proc ********************/
/*****************************************************/
/** @brief Client main loop
*
* Calls do_login(context) to handle the login, and then enters the main
* loop.
*
* Each loop consist of checking if the control connection is ready for
* reading, and if data connection is ready for reading/writing. If both
* are ready, the control connection is always handled first.
* Data are handled in the separate function data_execute().
*
* Control data are first translated to current charset if needed, then the
* first token is parsed and sent to commands_find() to identify the command.
*
* The exit is done using client_die().
*/
void * clientThreadProc(void *arg)
{
struct timeval tv;
fd_set fds_r,fds_w,efds;
unsigned long max_wait_time;
wzd_context_t * context;
char *buffer = NULL;
int save_errno;
fd_t sockfd;
int ret;
wzd_user_t * user;
wzd_command_t * command;
wzd_string_t * command_buffer;
struct ftp_command_t * ftp_command;
#ifndef _MSC_VER
int oldtype;
#endif
context = arg;
sockfd = context->controlfd;
context->last_file.name[0] = '\0';
context->last_file.token = TOK_UNKNOWN;
context->data_buffer = wzd_malloc(mainConfig->data_buffer_length);
#ifdef WIN32
context->thread_id = GetCurrentThreadId();
#else
context->thread_id = pthread_self();
#endif
out_log(LEVEL_INFO,"Client speaking to socket %d\n",sockfd);
#ifndef WIN32
#ifdef WZD_MULTITHREAD
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &oldtype);
pthread_cleanup_push((void (*) (void *))client_die, (void *) context);
#endif /* WZD_MULTITHREAD */
#endif
ret = do_login(context);
if (ret) {
#if defined (WIN32)
client_die(context);
#else
/* on other platforms, the cleanup function will be executed
* using pthread_cleanup_pop
*/
pthread_exit(NULL);
#endif /* WIN32 */
return NULL;
}
context->state = STATE_COMMAND;
user = GetUserByID(context->userid);
/* user+pass ok */
send_message_raw("230-command ok\r\n",context);
{
wzd_string_t * event_args = STR(user->username);
event_send(mainConfig->event_mgr, EVENT_LOGIN, 230, event_args, context);
str_deallocate(event_args);
}
ret = send_message(230,context);
/* update last login time */
time(&user->last_login);
context->control_buffer = buffer = malloc(WZD_BUFFER_LEN);
/* get value for server tick */
max_wait_time = config_get_integer(mainConfig->cfg_file, "GLOBAL", "client tick", &ret);
if (ret != CF_OK) {
max_wait_time = DEFAULT_CLIENT_TICK;
}
/* main loop */
context->exitclient=0;
context->idle_time_start = time(NULL);
user = GetUserByID(context->userid);
while (!context->exitclient) {
#ifdef DEBUG
if (check_context(context) != 0) {
out_log(LEVEL_CRITICAL,"CRITICAL check_context failed\n");
context->exitclient = 1;
break;
}
#endif /* DEBUG */
save_errno = 666;
/* 1. read */
FD_ZERO(&fds_r);
FD_ZERO(&fds_w);
FD_ZERO(&efds);
/* set control fd */
FD_SET(sockfd,&fds_r);
FD_SET(sockfd,&efds);
/* set data fd */
ret = data_set_fd(context,&fds_r,&fds_w,&efds);
if ((signed)sockfd > ret) ret = sockfd;
tv.tv_sec=max_wait_time; tv.tv_usec=0L;
/* bug in windows implementation of select(): when aborting a data connection,
* next calls to select() always return immediatly, causing wzdftpd
* to use 100% cpu (infinite loop).
* The solution is not to use efds
*/
/* ret = select(ret+1,&fds_r,&fds_w,&efds,&tv);*/
ret = select(ret+1,&fds_r,&fds_w,NULL,&tv);
FD_ZERO(&efds);
save_errno = errno;
if (ret==-1) {
if (errno == EINTR) continue;
else {
out_log(LEVEL_CRITICAL,"Major error during recv: control fd %d errno %d error %s\n",sockfd,save_errno,strerror(save_errno));
context->exitclient = 1;
}
}
/* TODO XXX FIXME is this empty if() intentional ?? */
if (FD_ISSET(sockfd,&efds)) {
/* if (save_errno == EINTR) continue;*/
/* out_log(LEVEL_CRITICAL,"Major error during recv: errno %d error %s\n",save_errno,strerror(save_errno));*/
/*out_err(LEVEL_CRITICAL,"ret %d sockfd: %d %d datafd %d %d\n",ret,sockfd,FD_ISSET(sockfd,&efds),context->datafd,FD_ISSET(context->datafd,&efds));
out_err(LEVEL_CRITICAL,"read %d %d write %d %d error %d %d\n",FD_ISSET(sockfd,&fds_r),FD_ISSET(context->datafd,&fds_r),
FD_ISSET(sockfd,&fds_w),FD_ISSET(context->datafd,&fds_w),
FD_ISSET(sockfd,&efds),FD_ISSET(context->datafd,&efds));*/
/* continue;*/
}
ret = data_check_fd(context,&fds_r,&fds_w,&efds);
if (ret == -1) {
/* we had an error reading data connection */
/** \todo should be remove data descriptors and so ? */
}
if (!FD_ISSET(sockfd,&fds_r)) {
/* we check for data iff control is not set - control is prior */
if (ret==1) {
if (context->current_action.token == TOK_UNKNOWN) {
/* we are receiving / sending data without RETR/STOR */
continue;
}
/* we have data ready */
ret = data_execute(context,user,&fds_r,&fds_w);
continue;
}
/* nothing to read */
/* XXX CHECK FOR TIMEOUT: control & data if needed */
/* check timeout */
if (check_timeout(context)) break;
continue;
}
ret = (context->read_fct)(sockfd,buffer,WZD_BUFFER_LEN-1,0,0,context); /* timeout = 0, we know there's something to read */
/* remote host has closed session */
if (ret==0 || ret==-1) {
out_log(LEVEL_FLOOD,"Host disconnected improperly!\n");
context->exitclient=1;
break;
}
/* this replace the memset (bzero ?) some lines before */
buffer[ret] = '\0';
if (buffer[0]=='\0') continue;
if (buffer[0]=='\xff') {
const char * ptr = buffer;
char * ptr2;
/* skip telnet characters */
while (*ptr != '\0' &&
((unsigned char)*ptr == 255 || (unsigned char)*ptr == TELNET_IP || (unsigned char)*ptr == TELNET_SYNCH))
ptr++;
/* TODO replace this by a working memmove or copy characters directly */
ptr2 = strdup(ptr);
wzd_strncpy(buffer,ptr2,WZD_BUFFER_LEN-1);
free(ptr2);
}
command_buffer = STR(buffer);
str_trim_right(command_buffer);
set_action(context,str_tochar(command_buffer));
/* context->idle_time_start = time(NULL);*/
#ifdef DEBUG
out_err(LEVEL_FLOOD,"<thread %ld> <- '%s'\n",(unsigned long)context->pid_child,str_tochar(command_buffer));
#endif
/* reset current reply */
reply_clear(context);
/* parse and identify command */
ftp_command = parse_ftp_command(command_buffer);
if (ftp_command != NULL) {
command = ftp_command->command;
/** For FTP commands, the default permission (if not specified)
* is to ALLOW users to use command, unless restricted !
*/
if (command->perms && commands_check_permission(command,context)) {
ret = send_message_with_args(501,context,"Permission Denied");
free_ftp_command(ftp_command);
continue;
}
if (command->command)
ret = (*(command->command))(ftp_command->command_name,ftp_command->args,context);
else { /* external command */
char buffer_command[4096];
wzd_group_t * group = NULL;
if (user->group_num > 0) group = GetGroupByID(user->groups[0]);
cookie_parse_buffer(str_tochar(command->external_command), user, group, context, buffer_command, sizeof(buffer_command));
chop(buffer_command);
/* add arguments given on CLI to event */
if (str_length(ftp_command->args)>0) {
strlcat(buffer_command, " ", sizeof(buffer_command));
strlcat(buffer_command, str_tochar(ftp_command->args), sizeof(buffer_command));
}
ret = event_exec(buffer_command,context);
}
/** \todo When all functions use reply_push, test reply and send error if -1 */
ret = reply_send(context);
} else { /* no command found */
ret = send_message(502,context);
str_deallocate(command_buffer);
}
free_ftp_command(ftp_command);
} /* while (!exitclient) */
#ifdef WZD_MULTITHREAD
#ifndef WIN32
pthread_cleanup_pop(1); /* 1 means the cleanup fct is executed !*/
#else
client_die(context);
#endif /* _MSC_VER */
#else /* WZD_MULTITHREAD */
client_die(context);
#endif /* WZD_MULTITHREAD */
return NULL;
}
|