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
|
/* Roxen FTP protocol.
*
* $Id: ftp.pike,v 1.95 1998/05/09 18:59:22 grubba Exp $
*
* Written by:
* Pontus Hagland <law@lysator.liu.se>,
* David Hedbor <neotron@idonex.se>,
* Henrik Grubbstrm <grubba@idonex.se> and
* Marcus Comstedt <marcus@idonex.se>
*
* Some of the features:
*
* * All files are parsed the same way as if they were fetched via WWW.
*
* * If someone logs in with a non-anonymous name, normal
* authentification is done. This means that you for example can
* use .htaccess files to limit access to different directories.
*
* * You can have 'user ftp directories'. Just add a user database
* and a user filesystem. Notice that _normal_ non-anonymous ftp
* should not be used, due to security reasons.
*/
/* TODO
*
* ABOR Abort transfer in progress.
*/
/*
* Relevant RFC's:
*
* RFC 764 TELNET PROTOCOL SPECIFICATION
* RFC 959 FILE TRANSFER PROTOCOL (FTP)
* RFC 1123 Requirements for Internet Hosts -- Application and Support
*
* More or less obsolete RFC's:
*
* RFC 542 File Transfer Protocol for the ARPA Network
* RFC 561 Standardizing Network Mail Headers
* RFC 607 Comments on the File Transfer Protocol
* RFC 614 Response to RFC 607, "Comments on the File Transfer Protocol"
* RFC 640 Revised FTP Reply Codes
* RFC 691 One More Try on the FTP
* RFC 724 Proposed Official Standard for the
* Format of ARPA Network Messages
* RFC 724 STANDARD FOR THE FORMAT OF
* ARPA NETWORK TEXT MESSAGES(1)
* RFC 737 FTP Extension: XSEN
* RFC 743 FTP extension: XRSQ/XRCP
* RFC 751 SURVEY OF FTP MAIL AND MLFL
* RFC 754 Out-of-Net Host Addresses for Mail
* RFC 765 FILE TRANSFER PROTOCOL
* RFC 775 DIRECTORY ORIENTED FTP COMMANDS
*/
inherit "http"; /* For the variables and such.. (Per) */
inherit "roxenlib";
#include <config.h>
#include <module.h>
#include <stat.h>
import Array;
#define perror roxen_perror
// #define FTP_LS_DEBUG
#ifdef FTP_LS_DEBUG
#define DWRITE(X) roxen_perror(X)
#else /* DEBUG */
#define DWRITE(X)
#endif /* DEBUG */
string controlport_addr, dataport_addr, cwd ="/";
int controlport_port, dataport_port;
object cmd_fd, pasv_port;
object curr_pipe=0;
int GRUK = random(predef::time(1));
function(object,mixed:void) pasv_callback;
mixed pasv_arg;
array(object) pasv_accepted;
array(string|int) session_auth = 0;
string username="", mode="A";
int restart_point = 0;
string oldcmd;
#undef QUERY
#define QUERY(X) roxen->variables->X[VAR_VALUE]
#define Query(X) conf->variables[X][VAR_VALUE] /* Per */
#define DESCRIBE_MODE(m) (m=="I"?"BINARY":"ASCII")
/********************************/
/* private functions */
void reply(string X)
{
conf->hsent += strlen(X);
cmd_fd->write(replace(X, "\n","\r\n"));
}
private string reply_enumerate(string s,string num)
{
string *ss;
ss=s/"\n";
while (sizeof(ss)&&ss[-1]=="") ss=ss[0..sizeof(ss)-2];
if (sizeof(ss)>1)
return num+"-"+(ss[0..sizeof(ss)-2]*("\n"+num+"-"))+
"\n"+num+" "+ss[-1]+"\n";
return num+" "+ss[-1]+"\n";
}
private static multiset|int allowed_shells = 0;
private int check_shell(string shell)
{
if (Query("shells") != "") {
if (!allowed_shells) {
object(Stdio.File) file = Stdio.File();
if (file->open(Query("shells"), "r")) {
allowed_shells = aggregate_multiset(@(map(file->read(0x7fffffff)/"\n",
lambda(string line) {
return((((line/"#")[0])/"" - ({" ", "\t"}))*"");
} )-({""})));
#ifdef DEBUG
perror(sprintf("ftp.pike: allowed_shells:%O\n", allowed_shells));
#endif /* DEBUG */
} else {
perror(sprintf("ftp.pike: Failed to open shell database (\"%s\")\n",
Query("shells")));
return(0);
}
}
return(allowed_shells[shell]);
}
return(1);
}
/********************************/
/* public methods */
void end(string|void);
void disconnect()
{
if(objectp(pipe) && pipe != Simulate.previous_object())
destruct(pipe);
cmd_fd = 0;
if(pasv_port) {
destruct(pasv_port);
pasv_port = 0;
}
destruct(this_object());
}
void end(string|void s)
{
if(objectp(cmd_fd))
{
if(s)
cmd_fd->write(s);
destruct(cmd_fd);
}
disconnect();
}
/* We got some data on a socket.
* ================================================= */
void got_data(mixed fooid, string s);
mapping internal_error(array err)
{
roxen->nwrite("Internal server error: " +
describe_backtrace(err) + "\n");
cmd_fd->write(reply_enumerate("Internal server error: "+
describe_backtrace(err)+"\n"+
"Service not available, please try again","421"));
}
mapping(string:array) stat_cache = ([]);
array my_stat_file(string f, string|void d)
{
if (d) {
f = combine_path(d, f);
}
array st;
if ((st = stat_cache[f]) && st[1]) {
if (_time(1) - st[0] < 3600) {
// Keep stats one hour.
return(st[1]);
}
}
stat_cache[f] = ({ _time(1), st = roxen->stat_file(f, this_object()) });
return(st);
}
string name_from_uid(int uid)
{
array(string) user = conf->auth_module &&
conf->auth_module->user_from_uid(uid);
return (user && user[0]) || (uid?((string)uid):"root");
}
constant decode_mode = ({
({ S_IRUSR, S_IRUSR, 1, "r" }),
({ S_IWUSR, S_IWUSR, 2, "w" }),
({ S_IXUSR|S_ISUID, S_IXUSR, 3, "x" }),
({ S_IXUSR|S_ISUID, S_ISUID, 3, "S" }),
({ S_IXUSR|S_ISUID, S_IXUSR|S_ISUID, 3, "s" }),
({ S_IRGRP, S_IRGRP, 4, "r" }),
({ S_IWGRP, S_IWGRP, 5, "w" }),
({ S_IXGRP|S_ISGID, S_IXGRP, 6, "x" }),
({ S_IXGRP|S_ISGID, S_ISGID, 6, "S" }),
({ S_IXGRP|S_ISGID, S_IXGRP|S_ISGID, 6, "s" }),
({ S_IROTH, S_IROTH, 7, "r" }),
({ S_IWOTH, S_IWOTH, 8, "w" }),
({ S_IXOTH|S_ISVTX, S_IXOTH, 9, "x" }),
({ S_IXOTH|S_ISVTX, S_ISVTX, 9, "T" }),
({ S_IXOTH|S_ISVTX, S_IXOTH|S_ISVTX, 9, "t" })
});
/********************************/
/* Flags for the simulated 'ls' */
#define LS_FLAG_A 1
#define LS_FLAG_a 2
#define LS_FLAG_C 4
#define LS_FLAG_d 8
#define LS_FLAG_F 16
#define LS_FLAG_f 32
#define LS_FLAG_G 64
#define LS_FLAG_l 128
#define LS_FLAG_n 256
#define LS_FLAG_r 512
#define LS_FLAG_R 1024
#define LS_FLAG_t 2048
#define LS_FLAG_U 4096
string file_ls(array (int) st, string file, int flags)
{
DWRITE(sprintf("file_ls(\"%s\", %08x)\n", file, flags));
int mode = st[0] & 007777;
array(string) perm = "----------"/"";
if (st[1] < -1) {
perm[0] = "d";
}
foreach(decode_mode, array(string|int) info) {
if ((mode & info[0]) == info[1]) {
perm[info[2]] = info[3];
}
}
string ct = ctime(st[-4]);
if (flags & LS_FLAG_G) {
// No group.
return sprintf("%s 1 %-10s %12d %s %s %s\n", perm*"",
(string)((flags & LS_FLAG_n)?st[-2]:name_from_uid(st[-2])),
(st[1]<0? 512:st[1]), ct[4..9], ct[11..15], file);
} else {
return sprintf("%s 1 %-10s %-6d%12d %s %s %s\n", perm*"",
(string)((flags & LS_FLAG_n)?st[-2]:name_from_uid(st[-2])),
st[-1], (st[1]<0? 512:st[1]), ct[4..9], ct[11..15], file);
}
}
class ls_program {
constant decode_flags =
([
"A":LS_FLAG_A,
"a":(LS_FLAG_a|LS_FLAG_A),
"C":LS_FLAG_C,
"d":LS_FLAG_d,
"F":LS_FLAG_F,
"f":(LS_FLAG_a|LS_FLAG_A|LS_FLAG_U),
"G":LS_FLAG_G,
"l":LS_FLAG_l,
"n":LS_FLAG_n,
"o":(LS_FLAG_l|LS_FLAG_G),
"r":LS_FLAG_r,
"R":LS_FLAG_R,
"t":LS_FLAG_t,
"U":LS_FLAG_U
]);
object id;
// NOTE: base is modified destructably!
array(string) my_combine_path_array(array(string) base, string part)
{
if ((part == ".") || (part == "")) {
if ((part == "") && (!sizeof(base))) {
return(({""}));
} else {
return(base);
}
} else if ((part == "..") && sizeof(base) &&
(base[-1] != "..") && (base[-1] != "")) {
base[-1] = part;
return(base);
} else {
return(base + ({ part }));
}
}
string my_combine_path(string base, string part)
{
if ((sizeof(part) && (part[0] == '/')) ||
(sizeof(base) && (base[0] == '/'))) {
return(combine_path(base, part));
}
// Combine two relative paths.
int i;
array(string) arr = ((base/"/") + (part/"/")) - ({ ".", "" });
foreach(arr, string part) {
if ((part == "..") && i && (arr[i-1] != "..")) {
i--;
} else {
arr[i++] = part;
}
}
if (i) {
return(arr[..i-1]*"/");
} else {
return("");
}
}
string list_files(mapping(string:array(mixed)) _files, string dir, int flags)
{
DWRITE(sprintf("list_files(\"%s\", 0x%08x)\n", dir, flags));
int i;
array(string) file_order = indices(_files);
if (!(flags & LS_FLAG_U)) {
if (flags & LS_FLAG_t) {
array(int) times = allocate(sizeof(file_order));
for (i=0; i < sizeof(file_order); i++) {
array st = _files[file_order[i]];
if (st) {
times[i] = st[-4];
} else {
file_order[i] = 0;
}
}
sort(times, file_order);
if (!(flags & LS_FLAG_r)) {
reverse(file_order);
}
} else {
sort(file_order);
if (flags & LS_FLAG_r) {
file_order = reverse(file_order);
}
}
file_order -= ({ 0 });
}
string res = "";
int total = 0;
foreach(file_order, string short) {
array st = _files[short];
if (st) {
if (flags & LS_FLAG_F) {
if (st[1] < 0) {
// directory
short += "/";
} else if (st[0] & 0111) {
// executable
short += "*";
}
}
if (flags & LS_FLAG_l) {
res += id->file_ls(st, short, flags);
if (st[1] < 0) {
total += 1; // One block
} else {
total += (st[1]+1023)/1024; // Blocks are 1KB.
}
} else {
res += short + "\n";
}
}
}
if (flags & LS_FLAG_l) {
res = sprintf("total %d\n", total) + res;
} else if (flags & LS_FLAG_C) {
res = sprintf("%-#79s\n", res);
}
return(res);
}
object(Stack.stack) dir_stack = Stack.stack();
int name_directories;
int flags;
class list_stream {
static private function read_callback;
static private function close_callback;
static private object(ADT.queue) data = ADT.queue();
mixed nb_id;
int sent;
int query_fd() { return -1; }
void write_out()
{
DWRITE("list_stream->write_out()\n");
while (!data->is_empty()) {
string block = data->get();
if (block) {
DWRITE(sprintf("list_stream->write_out(): Sending \"%s\"\n",
block));
if (sizeof(block)) {
read_callback(nb_id, block);
if (this_object()) {
sent += sizeof(block);
} else {
DWRITE(sprintf("list_stream->write:out(): DESTRUCTED!\n"));
}
}
} else {
DWRITE(sprintf("list_stream->write_out(): EOD\n"));
// End of data marker
call_out(close_callback, 0, nb_id);
return;
}
}
}
void write(string s)
{
DWRITE(sprintf("list_stream->write(\"%O\")\n", (string)s));
// write 0 to mark end of stream.
if (!s || sizeof(s)) {
data->put(s && replace(s, "\n", "\r\n"));
if (read_callback) {
write_out();
}
}
}
void set_nonblocking(function _read_cb,
function _write_cb,
function _close_cb)
{
read_callback = _read_cb;
close_callback = _close_cb;
if (!data->is_empty()) {
call_out(write_out, 0);
}
}
object id;
void create(object _id)
{
DWRITE("list_stream()\n");
id = _id;
}
void destroy()
{
DWRITE("list_stream() TERMINATING\n");
catch { id->ls_session = 0; };
roxen->log(([ "error": 200, "len": sent ]), id);
}
void close()
{
// _verify_internals();
DWRITE("list_stream->close()\n");
catch { id->ls_session = 0; };
if (this_object()) {
read_callback = 0;
close_callback = 0;
if (this_object()) {
// Paranoia.
catch { destruct(); };
}
}
}
void set_blocking() {}
};
object(list_stream) output;
int|void do_assynch_dir_ls()
{
DWRITE("do_assynch_dir_ls()\n");
if (output) {
if (dir_stack->ptr) {
string short = dir_stack->pop();
string long = combine_path(id->cwd, short);
if ((!sizeof(long)) || (long[-1] != '/')) {
long += "/";
}
mapping(string:array(mixed)) _dir = id->conf->find_dir_stat(long, id);
DWRITE(sprintf("do_assynch_dir_ls(): find_dir_stat(\"%s\") => %O\n",
long, _dir));
if ((flags & LS_FLAG_a) &&
(long != "/")) {
if (_dir) {
_dir[".."] = roxen->stat_file(combine_path(long,"../"), id);
} else {
_dir = ([ "..":roxen->stat_file(combine_path(long,"../"), id) ]);
}
}
string s = "\n";
if (_dir && sizeof(_dir)) {
if (!(flags & LS_FLAG_A)) {
foreach(indices(_dir), string f) {
if (sizeof(f) && (f[0] == '.')) {
m_delete(_dir, f);
}
}
} else if (!(flags & LS_FLAG_a)) {
foreach(indices(_dir), string f) {
if ((f - ".") == "") {
m_delete(_dir, f);
}
}
}
if (flags & LS_FLAG_R) {
foreach(indices(_dir), string f) {
if (!((<".","..">)[f])) {
array(mixed) st = _dir[f];
if (st && (st[1] < 0)) {
m_delete(_dir, f);
if (short[-1] != '/') {
f = short + "/" + f;
} else {
f = short + f;
}
_dir[f] = st;
name_directories=1;
dir_stack->push(f);
}
}
}
}
if (sizeof(_dir)) {
s = list_files(_dir, combine_path(id->cwd, short)+"/", flags);
}
} else {
DWRITE("do_assynch_dir_ls(): NO FILES!\n");
}
if (name_directories) {
s = "\n" + short + ":\n" + s;
}
if (s != "") {
output->write(s);
}
// Call me again.
#ifdef THREADS
return 1;
#else
call_out(do_assynch_dir_ls, 0);
#endif /* THREADS */
} else {
output->write(0);
}
}
}
void do_ls(mapping(string:mixed) args)
{
DWRITE("do_ls()\n");
if (output) {
foreach(indices(args), string short) {
array st = id->my_stat_file(id->not_query =
combine_path(id->cwd, short));
if (st && (st[1] < -1)) {
// Directory
if (!(flags & LS_FLAG_d)) {
dir_stack->push(short);
m_delete(args, short);
}
} else if (!st || (st[1] == -1)) {
// Not found
output->write(short + " not found\n");
m_delete(args, short);
}
}
string files = "";
if (sizeof(args)) {
foreach(indices(args), string s) {
args[s] = id->my_stat_file(combine_path(id->cwd, s));
}
output->write(files = list_files(args, id->cwd, flags));
}
if ((dir_stack->ptr > 1) || (sizeof(files))) {
name_directories = 1;
}
#ifdef THREADS
// No need to do it asynchronously, we'd just tie up the backend.
while(do_assynch_dir_ls())
;
#else /* THREADS */
call_out(do_assynch_dir_ls, 0);
#endif /* THREADS */
}
}
array(string) glob_expand_command_line(string arg)
{
DWRITE(sprintf("glob_expand_command_line(\"%s\")\n", arg));
array(string|array(string)) args = (replace(arg, "\t", " ")/" ") -
({ "" });
int index;
id->method="LIST";
for(index = 0; index < sizeof(args); index++) {
// Glob-expand args[index]
array (int) st;
if (replace(args[index], ({"*", "?"}), ({ "", "" })) != args[index]) {
// Globs in the file-name.
array(string|array(string)) matches = ({ ({ }) });
multiset(string) paths; // Used to filter out duplicates.
int i;
foreach(my_combine_path("", args[index])/"/", string part) {
paths = (<>);
if (replace(part, ({"*", "?"}), ({ "", "" })) != part) {
// Got a glob.
array(array(string)) new_matches = ({});
foreach(matches, array(string) path) {
array(string) dir;
dir = roxen->find_dir(combine_path(id->cwd, path*"/")+"/", id);
if (dir && sizeof(dir)) {
dir = glob(part, dir);
if ((< '*', '?' >)[part[0]]) {
// Glob-expanding does not expand to files starting with '.'
dir = Array.filter(dir, lambda(string f) {
return (sizeof(f) && (f[0] != '.'));
});
}
foreach(sort(dir), string f) {
array(string) arr = my_combine_path_array(path, f);
string p = arr*"/";
if (!paths[p]) {
paths[p] = 1;
new_matches += ({ arr });
}
}
}
}
matches = new_matches;
} else {
// No glob
// Just add the part. Modify matches in-place.
for(i=0; i<sizeof(matches); i++) {
matches[i] = my_combine_path_array(matches[i], part);
string path = matches[i]*"/";
if (paths[path]) {
matches[i] = 0;
} else {
paths[path] = 1;
}
}
matches -= ({ 0 });
}
if (!sizeof(matches)) {
break;
}
}
if (sizeof(matches)) {
// Array => string
for (i=0; i < sizeof(matches); i++) {
matches[i] *= "/";
}
// Filter out non-existing or forbiden files/directories
matches = Array.filter(matches, lambda(string short) {
id->not_query = combine_path(id->cwd, short);
return(id->my_stat_file(id->not_query));
});
if (sizeof(matches)) {
args[index] = matches;
}
}
}
if (stringp(args[index])) {
// No glob
args[index] = ({ my_combine_path("", args[index]) });
}
}
return(args * ({}));
}
void destroy()
{
DWRITE("ls_program() TERMINATED\n");
if (output) {
destruct(output);
}
}
void create(string arg, object _id)
{
DWRITE(sprintf("ls_program(\"%s\")\n", arg));
mixed err;
err = catch {
id = _id;
array(string) args = glob_expand_command_line(arg);
args = ({ "ls" }) + args;
array options;
if (err = catch {
options = Getopt.find_all_options(args, ({
({ "A", Getopt.NO_ARG, ({ "-A", "--almost-all" })}),
({ "a", Getopt.NO_ARG, ({ "-a", "--all" })}),
({ "C", Getopt.NO_ARG, "-C" }),
({ "d", Getopt.NO_ARG, ({ "-d", "--directory" })}),
({ "F", Getopt.NO_ARG, ({ "-F", "--classify" })}),
({ "f", Getopt.NO_ARG, "-f" }),
({ "G", Getopt.NO_ARG, ({ "-G", "--no-group" })}),
({ "g", Getopt.NO_ARG, "-g" }),
({ "L", Getopt.NO_ARG, ({ "-L", "--dereference" })}),
({ "l", Getopt.NO_ARG, "-l" }),
({ "n", Getopt.NO_ARG, ({ "-n", "--numeric-uid-gid" })}),
({ "o", Getopt.NO_ARG, "-o" }),
({ "r", Getopt.NO_ARG, ({ "-r", "--reverse" })}),
({ "R", Getopt.NO_ARG, ({ "-R", "--recursive" })}),
({ "t", Getopt.NO_ARG, "-t" }),
({ "U", Getopt.NO_ARG, "-U" }),
}), 1, 1);
}) {
id->reply(id->reply_enumerate(err[0], "550"));
return;
}
foreach(options, array(mixed) option) {
flags |= decode_flags[option[0]];
}
if (flags & LS_FLAG_d) {
flags &= ~LS_FLAG_R;
}
if (flags & LS_FLAG_f) {
flags &= ~LS_FLAG_l;
}
if (err = catch {
args = Getopt.get_args(args, 1, 1)[1..];
}) {
id->reply(id->reply_enumerate(err[0], "550"));
return;
}
if (!sizeof(args)) {
args = ({ "./" });
}
output = list_stream(id);
id->connect_and_send(([ "file":output ]));
do_ls(mkmapping(args, args));
return;
};
err = sprintf("ftp: builtin_ls: Internal error:\n%s\n",
describe_backtrace(err));
report_error(err);
#ifdef MODULE_DEBUG
id->reply(id->reply_enumerate(err, "550"));
#else
id->reply("550 ftp: builtin_ls: Internal error\n");
#endif /* MODULE_DEBUG */
}
};
void pasv_accept_callback(mixed id)
{
if(pasv_port) {
object fd = pasv_port->accept();
if(fd) {
// On a multihomed server host, the default data transfer port
// (L-1) MUST be associated with the same local IP address as
// the corresponding control connection to port L.
// RFC 1123 4.1.2.12
array(string) remote = (fd->query_address()||"? ?")/" ";
#ifdef FD_DEBUG
mark_fd(fd->query_fd(),
"ftp communication: -> "+remote[0]+":"+remote[1]);
#endif
if(pasv_callback) {
pasv_callback(fd, pasv_arg);
pasv_callback = 0;
} else
pasv_accepted += ({ fd });
}
}
}
void ftp_async_accept(function(object,mixed:void) callback, mixed arg)
{
if(sizeof(pasv_accepted)) {
callback(pasv_accepted[0], arg);
pasv_accepted = pasv_accepted[1..];
} else {
pasv_callback = callback;
pasv_arg = arg;
}
}
void done_callback(object fd)
{
if(fd)
{
if (fd->set_blocking) {
fd->set_blocking(); // Force close() to flush any buffers.
}
fd->close();
destruct(fd);
}
curr_pipe = 0;
reply("226 Transfer complete.\n");
cmd_fd->set_read_callback(got_data);
cmd_fd->set_write_callback(lambda(){});
cmd_fd->set_close_callback(end);
#ifdef FD_DEBUG
mark_fd(cmd_fd->query_fd(), GRUK+" cmd channel not sending data");
#endif
}
void connected_to_send(object fd,mapping file)
{
object pipe=roxen->pipe();
if(!file->len)
file->len = file->data?(stringp(file->data)?strlen(file->data):0):0;
if(fd)
{
if (file->len) {
reply(sprintf("150 Opening "+DESCRIBE_MODE(mode)+" data connection for %s "
"(%d bytes).\n", not_query, file->len));
} else {
reply(sprintf("150 Opening "+DESCRIBE_MODE(mode)+" mode data connection for %s\n",
not_query));
}
}
else
{
reply("425 Can't build data connect: Connection refused.\n");
return;
}
if(mode == "A")
{
if(objectp(file->file) && file->file->read)
{
// The list_stream object doesn't have a read method,
// but converts to ASCII anyway, so we don't have to do
// anything about it.
if(!file->data) file->data="";
file->data += file->file->read();
file->file=0;
}
if (file->data) {
file->data = replace(file->data, "\n", "\r\n");
}
}
if(stringp(file->data)) pipe->write(file->data);
if(file->file) {
file->file->set_blocking();
pipe->input(file->file);
}
curr_pipe = pipe;
pipe->set_done_callback(done_callback, fd);
pipe->output(fd);
}
inherit "socket";
void ftp_async_connect(function(object,mixed:void) fun, mixed arg)
{
// More or less copied from socket.pike
object(Stdio.File) f = Stdio.File();
string|int local_addr = (cmd_fd->query_address(1)/" ")[0];
object privs;
if(controlport_port-1 < 1024)
privs = Privs("FTP: Opening the data connection on " + local_addr +
":" + (controlport_port-1) + ".");
if(!f->open_socket(controlport_port-1, local_addr))
{
privs = 0;
#ifdef FTP_DEBUG
perror("ftp: socket("+(controlport_port-1)+") failed. Trying with any port.\n");
#endif
gc();
if (!f->open_socket()) {
#ifdef FTP_DEBUG
perror("ftp: socket() failed. Out of sockets?\n");
#endif
fun(0, arg);
destruct(f);
return;
}
}
privs = 0;
f->set_id( ({ fun, ({ arg }), f }) );
f->set_nonblocking(0, lambda(array args) {
#ifdef FTP_DEBUG
perror("ftp: async_connect ok.\n");
#endif
args[2]->set_id(0);
args[0](args[2], @args[1]);
}, lambda(array args) {
#ifdef FTP_DEBUG
perror("ftp: connect_and_send failed\n");
#endif
args[2]->set_id(0);
destruct(args[2]);
args[0](0, @args[1]);
});
#ifdef FD_DEBUG
mark_fd(f->query_fd(), "ftp communication: " + local_addr + ":" +
(controlport_port - 1) + " -> " +
dataport_addr + ":" + dataport_port);
#endif
if(catch(f->connect(dataport_addr, dataport_port)))
{
#ifdef FTP_DEBUG
perror("ftp: Illegal internet address in connect in async comm.\n");
#endif
fun(0, arg);
destruct(f);
return;
}
}
void connect_and_send(mapping file)
{
if(pasv_port)
ftp_async_accept(connected_to_send, file);
else
ftp_async_connect(connected_to_send, file);
}
class put_file_wrapper {
inherit Stdio.File;
static object id;
static string response;
static string gotdata;
static int done, recvd;
static function other_read_callback;
static string mode;
int bytes_received()
{
return recvd;
}
int close(string|void how)
{
if(how != "w" && !done) {
id->reply(response);
done = 1;
id->file = 0;
id->my_fd = 0;
}
return (how? ::close(how) : ::close());
}
string read(mixed ... args)
{
string r = ::read(@args);
if(stringp(r) && (mode=="A")) r = replace(r, "\r\n", "\n");
if(stringp(r))
recvd += sizeof(r);
return r;
}
static mixed my_read_callback(mixed id, string data)
{
if(stringp(data) && (mode=="A")) data = replace(data, "\r\n", "\n");
if(stringp(data))
recvd += sizeof(data);
return other_read_callback(id, data);
}
void set_read_callback(function read_callback)
{
if(read_callback) {
other_read_callback = read_callback;
::set_read_callback(my_read_callback);
} else
::set_read_callback(read_callback);
}
void set_nonblocking(function ... args)
{
if(sizeof(args) && args[0]) {
other_read_callback = args[0];
::set_nonblocking(my_read_callback, @args[1..]);
} else
::set_nonblocking(@args);
}
int write(string data)
{
int n, code;
string msg;
gotdata += data;
while((n=search(gotdata, "\n"))>=0) {
if(3==sscanf(gotdata[..n], "HTTP/%*s %d %[^\r\n]", code, msg)
&& code>199) {
if(code < 300)
code = 200;
else
code = 550;
response = code + " " + msg + "\n";
}
gotdata = gotdata[n+1..];
}
if(mode=="A") gotdata = replace(gotdata, "\n", "\r\n");
return strlen(data);
}
void create(object i, object f, string m)
{
id = i;
mode=m;
assign(f);
response = "200 Stored.\n";
gotdata = "";
done = 0;
recvd = 0;
}
}
int open_file(string arg, int|void noport);
void connected_to_receive(object fd, string arg)
{
if(fd)
{
reply(sprintf("150 Opening "+DESCRIBE_MODE(mode)+" mode data connection for %s.\n", arg));
}
else
{
reply("425 Can't build data connect: Connection refused.\n");
return;
}
method = "PUT";
my_fd = put_file_wrapper(this_object(), fd, mode);
data = 0;
misc->len = 0x7fffffff;
if(open_file(arg)) {
if(!(file->pipe)) {
fd->close();
switch(file->error) {
case 401:
reply("532 "+arg+": Need account for storing files.\n");
break;
case 501:
reply("502 "+arg+": Command not implemented.\n");
break;
default:
reply("550 "+arg+": Error opening file.\n");
}
}
} else
fd->close();
}
void connect_and_receive(string arg)
{
if(pasv_port)
ftp_async_accept(connected_to_receive, arg);
else
ftp_async_connect(connected_to_receive, arg);
}
int open_file(string arg, int|void noport)
{
file=0;
array (int) st;
if(!noport)
if(!dataport_addr || !dataport_port)
{
reply("425 Can't build data connect: Connection refused.\n");
return 0;
}
if(arg[0] == '~')
this_object()->not_query = combine_path("/", arg);
else if(arg[0] == '/')
this_object()->not_query = simplify_path(arg);
else
this_object()->not_query = combine_path(cwd, arg);
if(!file || (file->full_path != not_query))
{
if(file && file->file)
destruct(file->file);
file=0;
foreach(conf->first_modules(), function funp)
if(file = funp( this_object())) break;
mixed err;
if (!file) {
st = my_stat_file(not_query);
if(st && st[1] < 0) {
reply("550 "+arg+": not a plain file.\n");
file = 0;
return 0;
} else if(err = catch(file = roxen->get_file(this_object()))) {
roxen_perror(sprintf("FTP: Error opening file\n%s\n",
describe_backtrace(err)));
// Lots of paranoia.
catch {
reply("550 "+arg+": Error, can't open file.\n");
};
catch {
file = 0;
};
return 0;
}
}
}
if(!file || (file->error && (file->error/100 != 2))) {
switch(misc->error_code) {
case 401:
case 403:
reply("550 "+arg+": Access denied.\n");
break;
case 405:
reply("550 "+arg+": Method not allowed.\n");
break;
default:
reply("550 "+arg+": No such file or directory.\n");
break;
}
return 0;
} else
file->full_path = not_query;
if(!file->len)
{
if(file->data) file->len = strlen(file->data);
if(objectp(file->file)) file->len += file->file->stat()[1];
}
if(file->len > 0)
conf->sent += file->len;
if(file->error == 403)
{
reply("550 "+arg+": Permission denied by rule.\n");
roxen->log(file, this_object());
return 0;
}
if(file->error == 302 || file->error == 301)
{
reply("550 "+arg+": Redirect to "+file->Location+".\n");
roxen->log(file, this_object());
return 0;
}
return 1;
}
mapping(string:string) cmd_help = ([
// Commands in the order from RFC 959
"user":"<sp> username (Change user)",
"pass":"<sp> password (Change password)",
// "acct":"<sp> <account-information> (Account)",
"cwd":"[ <sp> directory-name ] (Change working directory)",
"cdup":"(Change to parent directory)",
// "smnt":"<sp> <pathname> (Structure mount)",
"quit":"(Terminate service)",
"rein":"(Reinitialize)",
"port":"<sp> b0, b1, b2, b3, b4 (Set port IP and number)",
"pasv":"(Set server in passive mode)",
"type":"<sp> [ A | E | I | L ] (Ascii, Ebcdic, Image, Local)",
// "stru":"<sp> <structure-code> (File structure)",
// "mode":"<sp> <mode-code> (Transfer mode)",
"retr":"<sp> file-name (Retreive file)",
"stor":"<sp> file-name (Store file)",
// "stou":"(Store file with unique name)",
// "appe":"<sp> <pathname> (Append file)",
// "allo":"<sp> <decimal-integer> [<sp> R <sp> <decimal-integer>]"
// " (Allocate space for file)",
"rest":"<sp> marker (Set restart marker)",
// "rnfr":"<sp> <pathname> (Rename from)",
// "rnto":"<sp> <pathname> (Rename to)",
// "abor":"(Abort current transmission)",
"dele":"<sp> file-name (Delete file)",
// "rmd":"<sp> <pathname> (Remove directory)",
// "mkd":"<sp> <pathname> (Make directory)",
"pwd":"(Return current directory)",
"list":"[ <sp> path-name ] (List directory)",
"nlst":"[ <sp> path-name ] (List directory)",
// "site":"<sp> <string> (Site parameters)", // Has separate help
"syst":"(Get type of operating system)",
"stat":"<sp> path-name (Status for file)",
"help":"[ <sp> <string> ] (Give help)",
"noop":"(No operation)",
// Old "Experimental commands"
// Required by RFC 1123 4.1.3.1
// "xmkd":"<sp> path-name (Make directory)",
// "xrmd":"<sp> path-name (Remove directory)",
"xpwd":"(Return current directory)",
"xcwd":"[ <sp> directory-name ] (Change working directory)",
"xcup":"(Change to parent directory)",
// The following aren't in RFC 959
"mdtm":"<sp> file-name (Modification time)",
"size":"<sp> path-name (Size)",
// These are in RFC 765 but not in RFC 959
// "mail":"[<sp> <recipient name>] (Mail to user)",
// "msnd":"[<sp> <recipient name>] (Mail send to terminal)",
// "msom":"[<sp> <recipient name>] (Mail send to terminal or mailbox)",
// "msam":"[<sp> <recipient name>] (Mail send to terminal and mailbox)",
// "mrsq":"(Mail recipient scheme question)",
// "mrcp":"(Mail recipient)",
// This one is referenced in a lot of old RFCs
// "mlfl":"(Mail file)",
// These are in RFC 737
// "xsen":"[<sp> <recipient name>] (Send to terminal)",
// "xsem":"[<sp> <recipient name>] (Send, mail if can't)",
// "xmas":"[<sp> <recipient name>] (Mail and send)",
// These are in RFC 743
// "xrsq":"[<SP> <scheme>] (Scheme selection)",
// "xrcp":"<SP> <recipient name> (Recipient specification)",
]);
mapping(string:string) site_help = ([
"prestate":"<sp> prestate"
]);
void timeout()
{
// Recomended by RFC 1123 4.1.3.2
reply("421 Timeout (3600 seconds): closing connection.\n");
end();
}
object ls_session;
#if constant(thread_create)
object(Thread.Mutex) handler_lock = Thread.Mutex();
#endif /* constant(thread_create) */
string partial = "";
void handle_data(string s, mixed key)
{
// roxen_perror(sprintf("FTP: handle_data():%O\n", ({ s })));
string cmdlin;
time = _time(1);
if (!objectp(cmd_fd)) {
if (objectp(key))
destruct(key);
return;
}
array y;
conf->received += strlen(s);
remove_call_out(timeout);
call_out(timeout, 3600);
remoteaddr = (cmd_fd->query_address()/" ")[0];
supports = (< "ftp", "images", "tables", >);
prot = "FTP";
method = "GET";
// However, a server-FTP MUST be capable of
// accepting and refusing Telnet negotiations (i.e., sending
// DON'T/WON'T). RFC 1123 4.1.2.12
// FIXME: Not supported yet.
array a = s/"\377";
if (sizeof(a) > 1) {
int i = 1;
string answer = "";
while (i < sizeof(a)) {
if (!sizeof(a[i])) {
a[i] = "\377"; // Not likely...
i++;
} else {
int j = i;
switch(a[i][0]) {
default: // Unknown
// FIXME: Should probably have a warning here.
break;
case 240: // SE
break;
case 241: // NOP
break;
case 242: // Data Mark
break;
case 243: // Break
destruct();
break;
case 244: // Interrupt Process
break;
case 245: // Abort Output
break;
case 246: // Are You There
answer += "\377\361"; // NOP
break;
case 247: // Erase character
while (j--) {
if (sizeof(a[j])) {
a[j] = a[j][..sizeof(a[j])-2];
break;
}
}
break;
case 248: // Erase line
while (j--) {
int off;
if ((off = search(reverse(a[j]), "\n\r")) == -1) {
a[j] = "";
} else {
a[j] = a[j][..sizeof(a[j])-(off+1)];
break;
}
}
break;
case 249: // Go ahead
break;
case 250: // SB
break;
case 251: // WILL
answer += "\377\376"+a[i][1..1]; // DON'T xxx
a[i] = a[i][1..]; // Need to strip one extra char.
break;
case 252: // WON'T
break;
case 253: // DO
answer += "\377\374"+a[i][1..1]; // WON'T xxx
a[i] = a[i][1..]; // Need to strip one extra char.
break;
case 254: // DON'T
break;
}
a[i] = a[i][1..];
}
}
if (sizeof(answer)) {
reply(answer);
}
s = a*"";
}
// A single read() can contain multiple or partial commands
// RFC 1123 4.1.2.10
if (sizeof(partial)) {
s = partial + s;
}
while (sscanf(s,"%s\r\n%s",cmdlin,s)==2)
{
string cmd,arg;
// Lets hope the ftp-module doesn't store things in misc between
// requests.
// IT DOES! /grubba
mapping new_misc = ([]);
foreach(({ "trace_enter", "trace_leave",
"uid", "gid", "gecos", "home", "shell" }), string s) {
if (misc[s]) {
new_misc[s] = misc[s];
}
}
misc = new_misc;
if (sscanf(cmdlin,"%s %s",cmd,arg)<2)
arg="",cmd=cmdlin;
cmd=lower_case(cmd);
#ifdef DEBUG
if(cmd == "pass")
perror("recieved 'PASS xxxxxxxx' "+GRUK+"\n");
else
perror("recieved '"+cmdlin+"' "+GRUK+"\n");
#endif
if (!conf->extra_statistics->ftp) {
conf->extra_statistics->ftp = (["commands":([ cmd:1 ])]);
} else if (!conf->extra_statistics->ftp->commands) {
conf->extra_statistics->ftp->commands = ([ cmd:1 ]);
} else {
conf->extra_statistics->ftp->commands[cmd]++;
}
if (!((session_auth && session_auth[0]) ||
Query("anonymous_ftp") ||
(< "user", "pass", "rein" >)[cmd])) {
reply("530 Please login with USER and PASS.\n");
continue;
}
switch (cmd) {
case "rein":
reply("220 Server ready for new user.\n");
session_auth = 0;
rawauth = 0;
auth = 0;
cwd = "/";
misc = ([]);
stat_cache = roxen->query_var(conf->name + ":ftp:stat_cache") || ([]);
break;
case "user":
session_auth = 0;
auth = 0;
stat_cache = roxen->query_var(conf->name + ":ftp:stat_cache") || ([]);
cwd = "/";
misc = ([]);
if(!arg || arg == "ftp" || arg == "anonymous") {
if (Query("anonymous_ftp")) {
reply("230 Anonymous ftp, at your service\n");
} else {
reply("532 Anonymous ftp disabled\n");
}
rawauth = 0;
} else {
rawauth = username = arg;
reply(sprintf("331 Password required for %s.\n", arg));
}
break;
case "pass":
if(!rawauth) {
if (Query("anonymous_ftp")) {
reply("230 Guest login ok, access restrictions apply.\n");
} else {
reply("503 Login with USER first.\n");
}
} else {
method="LOGIN";
y = ({ "Basic", username+":"+arg});
realauth = y[1];
// Use own stat cache.
stat_cache = ([]);
if(conf && conf->auth_module) {
y = conf->auth_module->auth( y, this_object() );
if (y[0] == 1) {
/* Authentification successfull */
if (!Query("named_ftp") || !check_shell(misc->shell)) {
reply("532 You are not allowed to use named-ftp. Try using anonymous\n");
/* roxen->(({ "error":403, "len":-1 ]), this_object()); */
break;
}
} else if (!Query("anonymous_ftp")) {
reply(sprintf("530 User %s access denied.\n", username));
break;
}
} else if (!Query("anonymous_ftp")) {
reply("532 Need account to login.\n");
break;
}
session_auth = auth = y;
if(auth[0] == 1) {
if (stringp(misc->home)) {
// Check if it is possible to cd to the users home-directory.
if ((misc->home == "") || (misc->home[-1] != '/')) {
misc->home += "/";
}
array(int) st = my_stat_file(misc->home);
if (st && (st[1] < 0)) {
cwd = misc->home;
}
}
reply("230 User "+username+" logged in.\n");
} else
reply("230 Guest user "+username+" logged in.\n");
/* roxen->log(([ "error": 202, "len":-1 ]), this_object()); */
}
break;
case "quit":
reply("221 Bye! It was nice talking to you!\n");
end();
if (objectp(key))
destruct(key);
return;
case "abor":
if (curr_pipe) {
destruct(curr_pipe);
curr_pipe = 0;
reply("426 Data transmission terminated.\n");
}
reply("226 'ABOR' Completed.\n");
break;
case "noop":
reply("220 Nothing done ok\n");
break;
case "syst":
reply("215 UNIX Type: L8: Roxen Challenger Information Server\n");
break;
case "pwd":
case "xpwd":
reply("257 \""+cwd+"\" is current directory.\n");
break;
case "cdup":
case "xcup":
arg = "..";
case "cwd":
case "xcwd":
string ncwd, f;
array (int) st;
array (string) dir;
if(!arg || !strlen(arg))
{
reply ("500 Syntax: CWD <new directory>\n");
break;
}
if(arg[0] == '~')
ncwd = combine_path("/", arg);
else if(arg[0] == '/')
ncwd = simplify_path(arg);
else
ncwd = combine_path(cwd, arg);
if ((ncwd == "") || (ncwd[-1] != '/')) {
ncwd += "/";
}
// Restore auth-info
auth = session_auth;
st = my_stat_file(ncwd);
if(!st) {
reply("550 "+arg+": No such file or directory, or access denied.\n");
break;
}
if(st[1] > -1) {
reply("550 "+arg+": Not a directory.\n");
break;
}
cwd = ncwd;
not_query = cwd;
if(dir = roxen->find_dir(cwd, this_object()))
{
string message = "";
array (string) readme = ({});
foreach(dir, f)
{
if(f == ".message")
message = roxen->try_get_file(cwd + f, this_object()) ||"";
if(f[0..5] == "README")
{
if(st = my_stat_file(cwd + f))
readme += ({ sprintf("Please read the file %s\n it was last "
"modified on %s - %d days ago\n",
f, ctime(st[-4]) - "\n",
(time - st[-4]) / 86400) });
}
}
if(sizeof(readme))
message += "\n" + readme * "\n";
if(strlen(message))
reply(reply_enumerate(message+"\nCWD command successful.\n","250"));
else
reply("250 CWD command successful.\n");
break;
}
reply("250 CWD command successful.\n");
break;
case "type":
// I and L8 are required by RFC 1123 4.1.2.1
array(string) a = arg/" ";
switch(a[0]) {
case "E":
reply("504 EBCDIC mode not supported\n");
break;
case "L":
if ((sizeof(a) > 1) && (a[1] != "8")) {
reply("504 Only 8bit LOCAL mode supported\n");
break;
}
case "I":
mode="I";
reply("200 Using BINARY mode for transferring files\n");
break;
case "A":
mode="A";
reply("200 Using ASCII mode for transferring files\n");
break;
default:
reply("504 Only ASCII and BINARY modes supported\n");
break;
}
break;
case "port":
int a,b,c,d,e,f;
if (sscanf(arg,"%d,%d,%d,%d,%d,%d",a,b,c,d,e,f)<6)
reply("501 I don't understand your parameters\n");
else {
dataport_addr=sprintf("%d.%d.%d.%d",a,b,c,d);
dataport_port=e*256+f;
if (pasv_port) {
destruct(pasv_port);
}
reply("200 PORT command ok ("+dataport_addr+
" port "+dataport_port+")\n");
}
break;
case "nlst":
case "list":
int flags = 0;
mapping f;
if (cmd == "list") {
arg = "-l " + arg;
}
// Count this as a request.
conf->requests++;
// Restore auth-info
auth = session_auth;
if(!dataport_addr || !dataport_port)
{
reply("425 Can't build data connect: Connection refused.\n");
break;
}
ls_session = ls_program(arg, this_object());
#if 0
if(sscanf(arg, "-%s %s", args, arg)!=2)
{
if(!strlen(arg))
arg = cwd;
else if(arg[0] == '-')
{
args = arg;
arg = cwd;
}
}
string file_arg;
if(arg[0] == '~')
file_arg = combine_path("/", arg);
else if(arg[0] == '/')
file_arg = simplify_path(arg);
else
file_arg = combine_path(cwd, arg);
if(args) {
foreach((args/""), string flg) {
flags |= decode_flags[flg];
}
}
// This is needed to get .htaccess to be read from the correct directory
if (file_arg[-1] != '/') {
array st = my_stat_file(file_arg);
if (st && (st[1]<0)) {
file_arg+="/";
}
}
not_query = file_arg;
foreach(conf->first_modules(), function funp)
if(f = funp( this_object())) break;
if(!f)
{
f = ([ "data":list_file(arg, flags) ]);
if(f->data == 0)
reply("550 "+arg+": No such file or directory.\n");
else if(f->data == -1)
reply("550 "+arg+": Permission denied.\n");
else
connect_and_send(f);
} else {
reply(reply_enumerate("Permission denied\n"+(f->data||""), "550"));
}
#endif /* 0 */
break;
case "rest":
if (!arg || !strlen(arg)) {
reply("501 'REST': Missing argument\n");
break;
}
restart_point = (int)arg;
reply("350 REST OK\n");
break;
case "retr":
// Count this as a request
conf->requests++;
string f;
if(!arg || !strlen(arg))
{
reply("501 'RETR': Missing argument\n");
break;
}
// Restore auth-info
auth = session_auth;
if(!open_file(arg))
break;
if (restart_point && (oldcmd == "rest")) {
if (!(file->file && file->file->seek &&
(file->file->seek(restart_point) != -1))) {
reply("550 'RETR': Error restoring restart point\n");
break;
}
}
restart_point = 0;
connect_and_send(file);
roxen->log(file, this_object());
break;
case "stat":
// According to RFC 1123 4.1.3.3, this command can be sent during
// a file-transfer.
// Count this as a request
conf->requests++;
string|int dirlist;
if(!arg || !strlen(arg))
{
reply("501 'STAT': Missing argument\n");
break;
}
method="HEAD";
reply("211-status of "+arg+":\n");
// Restore auth-info
auth = session_auth;
not_query = arg = combine_path(cwd, arg);
foreach(conf->first_modules(), function funp)
if(f = funp( this_object())) break;
if(f) dirlist = -1;
else {
array st = my_stat_file(arg);
dirlist = st && file_ls(st, arg, 0);
}
if(!dirlist)
{
reply("Unknown file: "+arg+" doesn't exist.\n");
} else if(dirlist == -1)
reply("Access denied\n");
else
reply(dirlist);
reply("211 End of Status\n");
break;
case "mdtm":
// Count this as a request
conf->requests++;
string fname;
if(!arg || !strlen(arg))
{
reply("501 'MDTM': Missing argument\n");
break;
}
method="HEAD";
// Restore auth-info
auth = session_auth;
not_query = fname = combine_path(cwd, arg);
foreach(conf->first_modules(), function funp)
if(f = funp( this_object())) break;
array st = my_stat_file(fname);
if (st) {
mapping m = localtime(st[3]);
reply(sprintf("213 %04d%02d%02d%02d%02d%02d\n",
1900 + m->year, 1 + m->mon, m->mday,
m->hour, m->min, m->sec));
} else {
reply("550 "+fname+": No such file or directory.\n");
}
break;
case "size":
// Count this a request
conf->requests++;
if(!arg || !strlen(arg))
{
reply("501 'SIZE': Missing argument\n");
break;
}
// Restore auth-info
auth = session_auth;
if(!open_file(arg))
break;
reply("213 "+ file->len +"\n");
break;
case "stor": // Store file..
// Count this as a request
conf->requests++;
string f;
if(!arg || !strlen(arg))
{
reply("501 'STOR': Missing argument\n");
break;
}
// Restore auth-info
auth = session_auth;
connect_and_receive(arg);
break;
case "dele":
// Count this as a request
conf->requests++;
if(!arg || !strlen(arg))
{
reply("501 'DELE': Missing argument\n");
break;
}
// Restore auth-info
auth = session_auth;
method = "DELETE";
data = 0;
misc->len = 0;
if(open_file(arg, 1))
reply("254 Delete completed\n");
break;
case "pasv":
// Required by RFC 1123 4.1.2.6
if(pasv_port)
destruct(pasv_port);
pasv_port = Stdio.Port(0, pasv_accept_callback);
int port=(int)((pasv_port->query_address()/" ")[1]);
reply("227 Entering Passive Mode. "+replace(controlport_addr, ".", ",")+
","+(port>>8)+","+(port&0xff)+"\n");
break;
case "help":
if(!arg || !strlen(arg)) {
reply(reply_enumerate(sprintf("The following commands are recognized:"
"\n%-#72;8s\n",
map(sort(indices(cmd_help)),
upper_case)*"\n"), "214"));
break;
}
if(cmd_help[lower_case(arg)]) {
reply(reply_enumerate("Syntax: "+upper_case(arg)+" "+
cmd_help[lower_case(arg)]+"\n", "214"));
break;
}
if(2==sscanf(lower_case(arg), "site%*[ ]%s", arg)) {
if(!strlen(arg)) {
reply(reply_enumerate(sprintf("The following SITE commands are "
"recognized:\n%-#72;8s\n",
map(sort(indices(site_help)),
upper_case)*"\n"), "214"));
break;
}
if(site_help[lower_case(arg)]) {
reply(reply_enumerate("Syntax: "+upper_case(arg)+" "+
site_help[lower_case(arg)]+"\n", "214"));
break;
}
}
reply("502 Unknown command "+upper_case(arg)+".\n");
break;
// Extended commands
case "site":
// Site specific commands are required to be part of the site command
// by RFC 1123 4.1.2.8
array(string) arr;
if (!arg || !strlen(arg) || !sizeof(arr = (arg/" ")-({""}))) {
reply(reply_enumerate("The following site dependant commands are available:\n"
"SITE PRESTATE <prestate>\tSet the prestate.\n",
"220"));
break;
}
if (lower_case(arr[0]) != "prestate") {
reply("504 Bad SITE command\n");
break;
}
if (sizeof(arr) == 1) {
reply("220 Prestate cleared\n");
prestate = (<>);
break;
}
prestate = aggregate_multiset(@((arr[1..]*" ")/","-({""})));
reply("220 Prestate set\n");
break;
case "":
/* The empty command, some stupid ftp-proxies send this. */
break;
case "mkd":
method = "MKDIR";
data = 0;
misc->len = 0;
if (open_file(arg, 1))
reply("254 Mkdir successful\n");
break;
default:
reply("502 command '"+ cmd +"' unimplemented.\n");
}
catch {
oldcmd = cmd;
};
}
catch {
partial = s;
};
if (objectp(key))
destruct(key);
}
void got_data(mixed fooid, string s)
{
mixed key;
#if constant(thread_create)
// NOTE: It's always the backend which locks, so we need to force
// the lock to avoid a "Recursive mutex" error in case it is locked.
key = handler_lock->lock(1);
#endif /* constant(thread_create) */
// Support for threading. The key is needed to get the correct order.
roxen->handle(handle_data, s, key);
}
int is_connection;
void destroy()
{
if (is_connection) {
conf->misc->ftp_users_now--;
}
if (ls_session) {
destruct(ls_session);
}
}
void create(object f, object c)
{
if(f)
{
string fi;
conf = c;
stat_cache = roxen->query_var(conf->name + ":ftp:stat_cache");
if (!stat_cache) {
roxen->set_var(conf->name + ":ftp:stat_cache", stat_cache = ([]));
}
is_connection=1;
conf->misc->ftp_users++;
conf->misc->ftp_users_now++;
cmd_fd = f;
cmd_fd->set_id(0);
cmd_fd->set_read_callback(got_data);
cmd_fd->set_write_callback(lambda(){});
cmd_fd->set_close_callback(end);
sscanf(cmd_fd->query_address(17)||"", "%s %d",
controlport_addr, controlport_port);
sscanf(cmd_fd->query_address()||"", "%s %d", dataport_addr, dataport_port);
pasv_port = 0;
pasv_callback = 0;
pasv_accepted = ({ });
not_query = "/welcome.msg";
call_out(timeout, 3600);
#if 0
if((fi = roxen->try_get_file("/welcome.msg", this_object()))||
(fi = roxen->try_get_file("/.message", this_object())))
reply(reply_enumerate(fi, "220"));
else
#endif /* 0 */
reply(reply_enumerate(Query("FTPWelcome"),"220"));
}
}
|