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
|
# -*- perl -*-
use 5.010;
# use utf8;
use Encode;
use Symbol qw(gensym);
use Cwd qw(abs_path);
use Fcntl qw(:flock :seek :mode);
use IO::Handle;
use IPC::Open3;
use Digest::MD5 qw(md5_hex);
# use Digest::SHA qw(hmac_sha512 hmac_sha512_base64);
use File::Basename;
use Sys::Hostname;
use Scalar::Util qw(tainted);
# set and untaint ENV if not in CLI (fexsrv provides clean ENV)
unless (-t) {
foreach my $v (keys %ENV) {
($ENV{$v}) = ($ENV{$v} =~ /(.*)/s) if defined $ENV{$v};
}
$ENV{PATH} = $ENV{HOME}.'/bin:/usr/local/bin:/bin:/usr/bin';
$ENV{IFS} = " \t\n";
$ENV{BASH_ENV} = '';
}
unless ($FEXLIB = $ENV{FEXLIB} and -d $FEXLIB) {
die "$0: found no FEXLIB - fexsrv needs full path\n"
}
$FEXLIB =~ s:/+:/:g;
$FEXLIB =~ s:/$::;
# $FEXHOME is top-level directory of F*EX installation or vhost
# $ENV{HOME} is login-directory of user fex
# in default-installation both are equal, but they may differ
$FEXHOME = $ENV{FEXHOME} or $ENV{FEXHOME} = $FEXHOME = dirname($FEXLIB);
$RA = $ENV{REMOTE_ADDR} || 0;
umask 0077;
$| = 1;
binmode(STDOUT,':raw');
binmode(STDIN,':raw');
# defaults
$hostname = gethostname();
$tmpdir = $ENV{TMPDIR} || '/var/tmp';
$spooldir = $FEXHOME.'/spool';
$docdir = $FEXHOME.'/htdocs';
$logdir = $spooldir;
$autodelete = 'YES';
$overwrite = 'YES';
$limited_download = 'YES'; # multiple downloads only from same client
$fex_yourself = 'YES'; # allow SENDER = RECIPIENT
$keep_default = 5; # days
$keep_max = 99;
$recipient_quota = 0; # MB
$sender_quota = 0; # MB
$timeout = 30; # seconds
$bs = 2**16; # I/O blocksize (64 kB)
$DS = 60*60*24; # seconds in a day
$MB = 1024*1024; # binary Mega
$vp = '\d{8}_\d{6}'; # date_time version pattern # should be: $vrx
$arx = 'tar|tgz|zip|7z|gz'; # archive container regexp
$mrx = '[\w!^=~#_:.*+-]+\@\w[\w.-]*\.[a-zA-Z]+'; # mail regexp
$sendmail = '/usr/lib/sendmail';
$sendmail = '/usr/sbin/sendmail' unless -x $sendmail;
$mailmode = 'auto';
$bcc = 'fex';
$default_locale = '';
$charset = 'UTF-8';
$fop_auth = 0;
$mail_authid = 'YES';
$archive_sharing = 'NO';
$document_exchange = 'NO';
$fex_sender = 'NO';
$share_logging = 1;
$force_https = 0;
$debug = 0;
## $enforce_EUCD = 'NO';
@forbidden_user_agents = ('FDM');
# https://securityheaders.io/
# https://scotthelme.co.uk/hardening-your-http-response-headers/
# https://scotthelme.co.uk/a-new-security-header-referrer-policy/
# http://content-security-policy.com/
@extra_header = (
# "Content-Security-Policy: sandbox allow-forms allow-scripts",
"Content-Security-Policy: script-src 'self' 'unsafe-inline'",
"Referrer-Policy: origin-when-cross-origin",
"X-Frame-Options: SAMEORIGIN",
"X-XSS-Protection: 1; mode=block",
"X-Content-Type-Options: nosniff",
);
push @extra_header,'X-Epoch: '.time();
$style = slurp("$FEXLIB/style.css") || qqq(qq(
'ul {'
' list-style: disc outside none;'
' padding: 0px 0px 0px 15px;'
' margin: 0;'
'}'
'table {'
' border-collapse: collapse;'
'}'
'table, th, td {'
# ' border-width: 1px;'
# ' border-color: black;'
# ' border-style: solid;'
# ' border: 1px solid black;'
' padding: 3px;'
'}'
));
$FHS = -f '/etc/fex/fex.ph' and -d '/usr/share/fex/lib';
# Debian FHS
if ($FHS) {
$ENV{FEXHOME} = $FEXHOME = '/usr/share/fex';
$spooldir = '/var/spool/fex';
$logdir = '/var/log/fex';
$docdir = '/var/lib/fex/htdocs';
$notify_newrelease = '';
}
# allowed download managers (HTTP User-Agent)
$adlm = '^(Axel|fex)';
# local config
require "$FEXLIB/fex.ph" or die "$0: cannot load $FEXLIB/fex.ph - $!";
$ENV{FEXSERVER} = $hostname;
$keep_default = $keep if $keep; # backward compatibilty to older fex.ph
## $enforce_EUCD = 0 if $enforce_EUCD !~ /y/i;
chdir $spooldir or die "$spooldir - $!\n";
$fop_auth = 0 if $fop_auth =~ /no/i;
$mail_authid = 0 if $mail_authid =~ /no/i;
$force_https = 0 if $force_https =~ /no/i;
$debug = 0 if $debug =~ /no/i;
$archive_sharing = 0 if $archive_sharing =~ /no/i;
$document_exchange = 0 if $document_exchange =~ /no/i;
$share_logging = 0 if $share_logging =~ /no/i;
$fex_sender = 0 if $fex_sender =~ /no/i;
@logdir = ($logdir) unless @logdir;
$logdir = $logdir[0];
# allowed multi download recipients: from any ip, any times
if (@mailing_lists) {
$amdl = '^('.join('|',map { quotewild($_) } @mailing_lists).')$';
} else {
$amdl = '^-$';
}
# check for name based virtual host
$vhost = vhost($ENV{'HTTP_HOST'});
$RB = 0; # read POST bytes
push @doc_dirs,$docdir;
foreach my $ld (glob "$FEXHOME/locale/*/htdocs") {
push @doc_dirs,$ld;
}
$nomail = ($mailmode =~ /^MANUAL|nomail$/i);
if (not $nomail and not -x $sendmail) {
http_die("found no sendmail");
}
http_die("cannot determine the server hostname") unless $hostname;
$ENV{PROTO} = 'http' unless $ENV{PROTO};
$keep = $keep_default ||= $keep || 5;
$purge ||= 3*$keep;
$fra = $ENV{REMOTE_ADDR} || '';
$sid = $ENV{SID} || '';
$dkeydir = "$spooldir/.dkeys"; # download keys
$ukeydir = "$spooldir/.ukeys"; # upload keys
$akeydir = "$spooldir/.akeys"; # authentification keys
$skeydir = "$spooldir/.skeys"; # subuser authentification keys
$gkeydir = "$spooldir/.gkeys"; # group authentification keys
$xkeydir = "$spooldir/.xkeys"; # extra download keys
$lockdir = "$spooldir/.locks"; # download lock files
unless (-d $akeydir) {
mkdir $akeydir or http_die("$akeydir - $!");
}
if ($RA and $max_fail) {
mkdirp("$spooldir/.fail");
$faillog = "$spooldir/.fail/$RA";
}
# $ENV{SERVER_ADMIN} may be set empty in fex.ph!
$admin ||= $ENV{SERVER_ADMIN} || 'fex@'.$hostname;
$ENV{SERVER_ADMIN} ||= $admin;
$mdomain ||= '';
if (my $cookie = $ENV{HTTP_COOKIE}) {
if ($cookie =~ /\bakey=(\w+)/ and -f "$akeydir/$1/@") {
$akey = $1;
}
}
if (@locales) {
if ($default_locale and not grep /^$default_locale$/,@locales) {
push @locales,$default_locale;
}
if (@locales == 1) {
$default_locale = $locales[0];
}
}
$default_locale ||= 'english';
# $durl is first default fop download URL
# @durl is optional mandatory fop download URL list (from fex.ph)
unless ($durl) {
my $host = '';
my $port = 80;
my $xinetd = '/etc/xinetd.d/fex';
if (@durl) {
if ($ENV{PROTO} eq 'https') {
($durl) = grep /https:/,@durl;
} else {
$durl = $durl[0];
}
} elsif ($ENV{HTTP_HOST} and $ENV{PROTO}) {
($host,$port) = split(':',$ENV{HTTP_HOST}||'');
$host = $hostname;
unless ($port) {
$port = 80;
if (open $xinetd,$xinetd) {
while (<$xinetd>) {
if (/^\s*port\s*=\s*(\d+)/) {
$port = $1;
last;
}
}
close $xinetd;
}
}
# use same protocal as uploader for download
if ($ENV{PROTO} eq 'https' and $port == 443 or $port == 80) {
$durl = "$ENV{PROTO}://$host/fop";
} else {
$durl = "$ENV{PROTO}://$host:$port/fop";
}
} else {
if (open $xinetd,$xinetd) {
while (<$xinetd>) {
if (/^\s*port\s*=\s*(\d+)/) {
$port = $1;
last;
}
}
close $xinetd;
}
if ($port == 80) {
$durl = "http://$hostname/fop";
} else {
$durl = "http://$hostname:$port/fop";
}
}
}
@durl = ($durl) unless @durl;
sub reexec {
exec($FEXHOME.'/bin/fexsrv') if $ENV{KEEP_ALIVE};
exit;
}
sub jsredirect {
$url = shift;
$cont = shift || 'request accepted: continue';
http_header('200 ok');
print html_header($head||$ENV{SERVER_NAME});
pq(qq(
'<script type="text/javascript">'
' window.location.replace("$url");'
'</script>'
'<noscript>'
' <h3><a href="$url">$cont</a></h3>'
'</noscript>'
'</body></html>'
));
&reexec;
}
sub debug {
print header(),"<pre>\n";
print "file = $file\n";
foreach $v (keys %ENV) {
print $v,' = "',$ENV{$v},"\"\n";
}
print "</pre><p>\n";
}
sub ssplit {
local $_ = "@_";
return split;
};
sub nvt_print {
local $_;
foreach (@_) {
my $x = $_;
$x = encode_utf8($x) if utf8::is_utf8($x);
syswrite STDOUT,"$x\r\n";
}
}
sub http_header {
my $status = shift;
my $msg = $status;
return if $HTTP_HEADER;
$HTTP_HEADER = $status;
$msg =~ s/^\d+\s*//;
nvt_print("HTTP/1.1 $status");
nvt_print("X-Message: $msg");
# nvt_print("X-SID: $ENV{SID}") if $ENV{SID};
nvt_print("Server: fexsrv");
nvt_print("Expires: 0");
nvt_print("Cache-Control: no-cache");
if ($force_https) {
# https://www.owasp.org/index.php/HTTP_Strict_Transport_Security
# https://scotthelme.co.uk/hsts-the-missing-link-in-tls/
nvt_print("Strict-Transport-Security: max-age=2851200; preload");
}
nvt_print($_) foreach(@extra_header);
if ($from and $id and not $akey and my $rid = readline1("$from/@")) {
$akey = genakey($from) if $id eq $rid;
}
if ($akey and -l "$akeydir/$akey") {
my $login = basename(readlink("$akeydir/$akey"));
nvt_print("Set-Cookie: akey=$akey; path=/; Max-Age=44444; Discard");
nvt_print("Set-Cookie: login=$login; path=/");
}
# if ($skey) {
# nvt_print("Set-Cookie: skey=$skey; Max-Age=9999; Discard");
# }
if ($locale) {
nvt_print("Set-Cookie: locale=$locale");
}
unless (grep /^Content-Type:/i,@_) {
# nvt_print("Content-Type: text/html; charset=ISO-8859-1");
nvt_print("Content-Type: text/html; charset=$charset");
}
nvt_print(@_,'');
}
sub html_header {
my $title = shift;
my $header = 'header.html';
my $fua = $ENV{HTTP_USER_AGENT}||'';
my $head;
my $h1;
# binmode(STDOUT,':utf8'); # for text/html !
$h1 = $title;
$title =~ s/<.*?>//g;
$style =~ s/\n+$//;
# http://www.w3.org/TR/html401/struct/global.html
# http://www.w3.org/International/O-charset
if ($fua =~ /^fex/) {
$head = "<html><body>\n";
} else {
$head = qqq(qq(
'<html>'
'<head>'
' <meta http-equiv="expires" content="0">'
' <meta http-equiv="Content-Type" content="text/html;charset=$charset">'
' <title>$title</title>'
'</head>'
'<style>'
'$style'
'</style>'
));
# '<!-- <style type="text/css">\@import "/fex.css";</style> -->'
if ($0 =~ /fexdev/) { $head .= "<body bgcolor=\"pink\">\n" }
else { $head .= "<body>\n" }
$h1 =~ s:F\*EX:<a href="/index.html">F*EX</a>:;
if (open $header,'<',"$docdir/$header") {
$head .= $_ while <$header>;
close $header;
}
$head .= &$prolog($title) if defined($prolog);
if (@H1_extra) {
$head .= sprintf(
qq'<h1><a href="%s"><img align=center src="%s" border=0></a>%s</h1>\n',
$H1_extra[0],$H1_extra[1]||'',$h1
);
} else {
$head .= "<h1>$h1</h1>\n";
}
$head .= qqq(qq(
'<font color="red">'
'<h2 id="jswarning">PLEASE ENABLE JAVASCRIPT TO USE THIS SERVICE</h2>'
'</font>'
'<script>'
'document.getElementById("jswarning").innerHTML = "";'
'</script>'
''
));
}
# Safari does not show popups (javascript window.open)
# if (($ENV{HTTP_USER_AGENT}||'') =~ /Apple.*Safari/) {
# $head .= qqq(qq(
# '<font color="orange">'
# '<h2>Your webbrowser (Safari) is not supported</h2>'
# '</font>'
# ));
# }
return $head;
}
sub http_error {
my $error = shift;
my $msg = shift;
my $URL = $ENV{REQUEST_URL}||'';
my $URI = $ENV{REQUEST_URI}||'';
my $isodate = isodate(time);
$URI =~ s/\?.*//;
if ($error eq 400) {
http_error_header("400 Bad Request");
nvt_print("Your request $URL is not acceptable.");
} elsif ($error eq 403) {
http_error_header("403 Forbidden");
if ($msg) {
nvt_print($msg);
} else {
nvt_print("You have no permission to request $URL");
}
} elsif ($error eq 404) {
http_error_header("404 Not Found");
nvt_print("The requested URI $URI was not found on this server.");
} elsif ($error eq 413) {
http_error_header("413 Payload Too Large");
nvt_print("Your HTTP header is too large.");
} elsif ($error eq 416) {
http_error_header("416 Requested Range Not Satisfiable");
} elsif ($error eq 503) {
http_error_header("503 Service Unavailable");
# nvt_print("No Perl ipv6 support on this server.");
} else {
http_error_header("555 Unknown Error");
nvt_print("The requested URL $URL produced an internal error.");
}
pq(qq(
'<p><hr><p>'
'$isodate<br>'
'$ENV{HTTP_HOST}<p>'
'<address>'
' <a href="mailto:$admin">$admin</a>'
'</address>'
'</body></html>'
));
exit;
}
sub http_error_header {
my $error = shift;
my $uri = $ENV{REQUEST_URI};
errorlog("$uri ==> $error") if $uri;
nvt_print(
"HTTP/1.1 $error",
"Connection: close",
"Content-Type: text/html; charset=iso-8859-1",
"",
'<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">',
"<html>",
"<head><title>$error</title></head>",
"<body>",
"<h1>$error</h1>",
);
}
sub html_error {
my $error = shift;
my $msg = "@_";
my @msg = @_;
my $isodate = isodate(time);
local $_;
$x = encode_utf8($msg) if utf8::is_utf8($msg);
$msg =~ s/[\s\n]+/ /g;
$msg =~ s/<.+?>//g; # remove HTML
foreach (@msg) {
$_ = encode_utf8($_) if utf8::is_utf8($_);
s/<script.*?>//gi;
}
errorlog($msg);
$SIG{ALRM} = sub {
$SIG{__DIE__} = 'DEFAULT';
die "TIMEOUT\n";
};
alarm($timeout);
# cannot send standard HTTP Status-Code 400, because stupid
# Internet Explorer then refuses to display HTML body!
http_header("666 Bad Request - $msg");
print html_header($error);
print 'ERROR: ',join("<p>\n",@msg),"\n";
pq(qq(
'<p><hr><p>'
'$isodate<br>'
'$ENV{HTTP_HOST}<p>'
'<address>'
' <a href="mailto:$admin">$admin</a>'
'</address>'
'</body></html>'
));
exit;
}
sub http_die {
# not in CGI mode
unless ($ENV{GATEWAY_INTERFACE}) {
warn "$0: @_\n"; # must not die, because of fex_cleanup!
return;
}
debuglog("die: @_");
# create special error file on upload
if ($uid) {
my $ukey = "$spooldir/.ukeys/$uid";
$ukey .= "/error" if -d $ukey;
unlink $ukey;
if (open $ukey,'>',$ukey) {
print {$ukey} join("\n",@_),"\n";
close $ukey;
}
}
html_error($error||'',@_);
}
sub check_maint {
if (my $status = readlink '@MAINTENANCE') {
my $isodate = isodate(time);
http_header('666 MAINTENANCE');
print html_header($head||'');
pq(qq(
"<center>"
"<h1>Server is in maintenance mode</h1>"
"<h3>($status)</h3>"
"</center>"
"<p><hr><p>"
"<address>"
"Contact: $admin<br>"
"$ENV{HTTP_HOST} $isodate"
"</address>"
"</body></html>"
));
exit;
}
}
sub check_status {
my $user = shift;
$user = lc $user;
$user .= '@'.$mdomain if $mdomain and $user !~ /@/;
if (my $disabled = slurp("$user/\@DISABLED")) {
if ($disabled =~ s/^!//) {
$disabled = "$user is disabled: $disabled";
} else {
$disabled = "$user is disabled";
}
my $isodate = isodate(time);
http_header('666 DISABLED');
print html_header($head);
pq(qq(
"<h3>$disabled</h3>"
"Contact $admin for details"
"<p><hr><p>"
"<address>$ENV{HTTP_HOST} $isodate</address>"
"</body></html>"
));
exit;
}
}
sub isodate {
my $time = shift;
my $isodate = '????-??-?? ??:??:??';
if ($time) {
my @d = localtime $time;
$isodate = sprintf('%d-%02d-%02d %02d:%02d:%02d',
$d[5]+1900,$d[4]+1,$d[3],$d[2],$d[1],$d[0]);
}
return $isodate;
}
sub genakey {
my $user = shift;
my $fua = $ENV{HTTP_USER_AGENT}||'';
my $akey;
$akey = $ENV{AKEY} || randstring(16);
if ($fua !~ /^fex/) {
unlink "$akeydir/$akey";
symlink "../$user","$akeydir/$akey";
}
return $akey;
}
sub encode_Q {
my $s = shift;
$s =~ s{([\=\x00-\x20\x7F-\xA0])}{sprintf("=%02X",ord($1))}eog;
return $s;
}
# from MIME::Base64::Perl
sub decode_b64 {
local $_ = shift;
my $uu = '';
my ($i,$l);
tr|A-Za-z0-9+=/||cd;
s/=+$//;
tr|A-Za-z0-9+/| -_|;
return '' unless length;
$l = (length)-60;
for ($i = 0; $i <= $l; $i += 60) {
$uu .= "M" . substr($_,$i,60);
}
$_ = substr($_,$i);
$uu .= chr(32+(length)*3/4) . $_ if $_;
return unpack ("u",$uu);
}
# short base64 encoding
sub b64 {
local $_ = '';
my $x = 0;
pos($_[0]) = 0;
$_ = join '',map(pack('u',$_)=~ /^.(\S*)/, ($_[0]=~/(.{1,45})/gs));
tr|\` -_|AA-Za-z0-9+/|;
$x = (3 - length($_[0]) % 3) % 3;
s/.{$x}$//;
return $_;
}
# a secure "rm -rf":
# never remove '.' or '..'
# restricted to $spooldir
# returns number of deleted objects
sub rmrf {
my @files = @_;
my @caller;
my $spool = untaint(abs_path($spooldir));
my ($file,$dir);
my $n = 0;
local $_;
@caller = caller(1) or @caller = caller;
foreach $file (@files) {
next unless lstat $file;
my $afile = abs_path($file)||'';
if ($file =~ m:^/: and $afile !~ m:^\Q$spool/: or
$afile eq $spool or "/$file/" =~ m:\Q/../:)
{
http_die(
sprintf "rmrf() called by %s::%s() line %s with illegal '%s'",
$caller[1],$caller[3]||'',$caller[2],$file
);
}
if (tainted($file)) {
http_die(
sprintf "rmrf() called by %s::%s() line %s with tainted '%s'",
$caller[1],$caller[3]||'',$caller[2],$file
);
}
}
foreach $file (@files) {
if (lstat $file) {
system qw'rm -rf',$file;
$n++ unless lstat $file;
}
}
return $n;
}
## does not work because of UTF8 vs ISO-Latin filenames :-(
# sub rmrf {
# my @files = @_;
# my $dels = 0;
# my @caller = caller(0);
# my $spool = untaint(abs_path($spooldir));
# my ($file,$dir);
# local $_;
#
# foreach $file (@files) {
# next unless lstat $file;
# my $afile = abs_path($file)||'';
# if ($file =~ m:^/: and $afile !~ m:^\Q$spool/:
# or $afile eq $spool or "/$file/" =~ m:\Q/../:)
# {
# http_die(
# sprintf "rmrf() called by %s::%s() line %s with illegal '%s'",
# $caller[1],$caller[3],$caller[2],$file
# );
# }
# if (tainted($file)) {
# http_die(
# sprintf "rmrf() called by %s::%s() line %s with tainted '%s'",
# $caller[1],$caller[3],$caller[2],$file
# );
# }
# if (-d $file and not -l $file) {
# $dir = $file;
# opendir $dir,$dir or next;
# while (defined(my $file = readdir $dir)) {
# next if $file eq '.' or $file eq '..';
# if ($charset =~ /utf/i) {
# $file = untaint(decode_utf8($file));
# } else {
# $file = untaint($file);
# }
# $dels += rmrf("$dir/$file");
# }
# closedir $dir;
# rmdir $dir and $dels++;
# } else {
# unlink $file and $dels++;
# }
# }
# return $dels;
# }
# remove recursive files matching regular expression
# return number of removed files
sub rmrx {
my $rx = shift;
my @files = @_;
my $dels = 0;
my @caller;
my $spool = untaint(abs_path($spooldir));
local $_;
@caller = caller(1) or @caller = caller;
foreach (@files) {
if (tainted($_)) {
http_die(
sprintf "rmrx() called by %s::%s() line %s with tainted '%s'",
$caller[1],$caller[3]||'',$caller[2],$_
);
}
if (m:^/: and abs_path($_) !~ m:^\Q$spool/:) {
http_die(
sprintf "rmrx() called by %s::%s() line %s with illegal '%s'",
$caller[1],$caller[3]||'',$caller[2],$_
);
}
$_ = untaint(abs_path('.')) if $_ eq '.';
next if /(^|\/)\.\.?$/;
if (-d and not -l) {
my $dir = $_;
if (basename($dir) =~ /$rx/) {
$dels += rmrf($dir);
} else {
opendir $dir,$dir or next;
while (defined(my $file = readdir $dir)) {
next if $file eq '.' or $file eq '..';
$dels += rmrx($rx,"$dir/".untaint($file));
}
closedir $dir;
}
} else {
if (basename($_) =~ /$rx/) {
unlink($_) and $dels++;
}
}
}
return $dels;
}
# remove recursive non-regular files (symlinks, etc)
sub doxunweed {
my $dir = shift;
my @caller;
my $spool = untaint(abs_path($spooldir));
local $_;
@caller = caller(1) or @caller = caller;
$dir = untaint(abs_path('.')) if $dir eq '.';
if (tainted($dir)) {
http_die(
sprintf "doxunweed() called by %s::%s() line %s with tainted '%s'",
$caller[1],$caller[3]||'',$caller[2],$dir
);
}
$dir = untaint(abs_path($dir));
if ($dir !~ m:^\Q$spool\E/.+\@.+/DOX/.+:) {
http_die(
sprintf "doxunweed() called by %s::%s() line %s with illegal '%s'",
$caller[1],$caller[3]||'',$caller[2],$dir
);
}
if (opendir $dir,$dir) {
while (defined(my $file = readdir $dir)) {
next if $file eq '.' or $file eq '..';
my $dfile = untaint("$dir/$file");
if (-l $dfile) {
unlink $dfile;
} elsif (-d $dfile) {
doxunweed($dfile) if $file ne '.fexdox';
} elsif (not -f $dfile) {
unlink $dfile;
}
}
closedir $dir;
}
}
# rename recursive files matching regular expression
# substitute character is '_'
sub rrrx {
my $rx = shift;
my @files = @_;
local $_;
foreach (@files) {
if (-d and not -l) {
my $dir = $_;
opendir $dir,$dir or next;
while (defined(my $file = readdir $dir)) {
next if $file eq '.' or $file eq '..';
$file = untaint($file);
rrrx($rx,"$dir/$file");
}
closedir $dir;
} else {
my $bname = basename($_);
my $dname = dirname($_);
$bname = s/$rx/_/g;
if ($_ ne "$dname/$bname") {
rename $_,"$dname/$bname";
}
}
}
}
sub gethostname {
my $hostname = hostname;
my $domain;
local $_;
unless ($hostname) {
$_ = `hostname 2>/dev/null`;
$hostname = /(.+)/ ? $1 : '';
}
if ($hostname !~ /\./ and open my $rc,'/etc/resolv.conf') {
while (<$rc>) {
if (/^\s*domain\s+([\w.-]+)/) {
$domain = $1;
last;
}
if (/^\s*search\s+([\w.-]+)/) {
$domain = $1;
}
}
close $rc;
$hostname .= ".$domain" if $domain;
}
if ($hostname !~ /\./ and $admin and $admin =~ /\@([\w.-]+)/) {
$hostname .= '.'.$1;
}
return $hostname;
}
# strip off path names (Windows or UNIX)
sub strip_path {
local $_ = shift;
s/.*\\// if /^([A-Z]:)?\\/;
s:.*/::;
return $_;
}
sub searchpath {
my $prg = shift;
my @PATH = split(':',$ENV{PATH});
if ($prg =~ m:/:) {
return $prg if -x $prg and -f $prg;
return '';
}
foreach my $path (@PATH) {
return $prg if -x "$path/$prg" and -f "$path/$prg";
}
return '';
}
# substitute all critical chars
sub normalize {
local $_ = shift;
return '' unless defined $_;
# we need perl native utf8 (see perldoc utf8)
$_ = decode_utf8($_) unless utf8::is_utf8($_);
s/[\r\n\t]+/ /g;
s/[\x00-\x1F\x80-\x9F]/_/g;
s/^\s+//;
s/\s+$//;
return encode_utf8($_);
}
# substitute all critcal chars
sub normalize_html {
local $_ = shift;
return '' unless defined $_;
$_ = normalize($_);
s/[\"<>]//g;
return $_;
}
# substitute all critcal chars with underscore
sub normalize_filename {
local $_ = shift;
# we need native utf8
$_ = decode_utf8($_) unless utf8::is_utf8($_);
$_ = strip_path($_);
# substitute all critcal chars with underscore
s/^\./_/;
s/[:&<>\$\`\|\\]/_/g;
s/[^a-z_\d@^~.,=+-]/_/gi;
return encode_utf8(untaint($_));
}
sub normalize_email {
local $_ = lc shift;
s/[^\w_.+=!~#^\@\-]//g;
s/^\./_/;
/(.*)/;
return $1;
}
sub normalize_user {
my $user = shift;
$user = lc(urldecode(despace($user)));
$user .= '@'.$mdomain if $mdomain and $user !~ /@/;
$user =~ s:/:_:g;
checkaddress($user) or http_die("$user is not a valid email address");
return untaint($user);
}
sub untaint {
local $_ = shift;
if (defined $_) {
/(.*)/s;
return $1;
} else {
@x = caller;
die "untaint() @x";
}
}
sub checkchars {
my $input = shift;
local $_ = shift;
if (/^([|+.])/) {
http_die("\"$1\" is not allowed at beginning of $input");
}
if (/([\/\"\'\\<>;])/) {
http_die(sprintf("\"&#%s;\" is not allowed in %s",ord($1),$input));
}
if (/(\|)$/) {
http_die("\"$1\" is not allowed at end of $input");
}
if (/[\000-\037]/) {
http_die("control characters are not allowed in $input");
}
/(.*)/;
return $1;
}
sub checkaddress {
my $a = shift;
my $re;
local $_;
local ($domain,$dns);
$a =~ s/:\w+=.*//; # remove options from address
return $a if $a eq 'anonymous';
$a .= '@'.$mdomain if $mdomain and $a !~ /@/;
$re = '^[.@-]|@.*@|local(host|domain)$|[(){}<>/;,*?&"`\'\s\[\]\|\\\\]';
if ($a =~ /$re/i) {
debuglog("$a has illegal syntax ($re)");
return '';
}
if ($a !~ /.\@[\w.-]+$/) {
debuglog("$a is not an email-address");
return '';
}
if ($a =~ /[\x00-\x20\x7f]/) {
debuglog("$a has non-printable character");
return '';
}
if ($a =~ /[\xa0-\xff]/) {
debuglog("$a has 8-bit character");
return '';
}
return $a if not $checkaddress or $checkaddress =~ /n/i;
$re = '^[\w!^=~#%:.+-]+\@(\w[\w.-]*\.[a-z]+)$';
if ($a =~ /$re/i) {
$domain = $dns = $1;
{
local $SIG{__DIE__} = sub { die "\n" };
eval q{
use Net::DNS;
$dns = Net::DNS::Resolver->new->query($domain)||mx($domain);
unless ($dns or mx('uni-stuttgart.de')) {
http_die("Internal error: bad resolver");
}
}
};
if ($dns) {
return untaint($a);
} else {
debuglog("no A or MX DNS record found for $domain");
return '';
}
} else {
debuglog("$a does not match email regexp ($re)");
return '';
}
}
# check forbidden addresses
sub checkforbidden {
my $a = shift;
my ($fr,$pr);
local $_;
$a .= '@'.$mdomain if $mdomain and $a !~ /@/;
return $a if -d "$spooldir/$a"; # ok, if user already exists
if (@forbidden_recipients) {
foreach (@forbidden_recipients) {
$fr = quotewild($_);
# skip public recipients
if (@public_recipients) {
foreach $pr (@public_recipients) {
return $a if $a eq lc $pr;
}
}
return '' if $a =~ /^$fr$/i;
}
}
return $a;
}
sub randstring {
my $n = shift;
my @rc = ('A'..'Z','a'..'z',0..9 );
my $rn = @rc;
my $rs;
for (1..$n) { $rs .= $rc[int(rand($rn))] };
return $rs;
}
# emulate mkdir -p
sub mkdirp {
my $dir = shift;
unless (-d $dir) {
$dir =~ s:/+$::;
if (length($dir)) {
mkdirp(dirname($dir));
mkdir $dir,0770 or http_die("mkdir $dir - $!");
} else {
http_die("cannot mkdir /");
}
}
}
# hash with SID
sub sidhash {
my ($rid,$id) = @_;
if ($rid and $ENV{SID} and $id =~ /^MD5H:/) {
$rid = 'MD5H:'.md5_hex($rid.$ENV{SID});
}
return $rid;
}
# test if ip is in iplist (ipv4/ipv6)
# iplist is an array with ips and ip-ranges
sub ipin {
my ($ip,@list) = @_;
my ($i,$ia,$ib);
$ipe = lc(ipe($ip));
map { lc } @list;
foreach $i (@list) {
if ($ip =~ /\./ and $i =~ /\./ or $ip =~ /:/ and $i =~ /:/) {
if ($i =~ /(.+)-(.+)/) {
($ia,$ib) = ($1,$2);
$ia = ipe($ia);
$ib = ipe($ib);
return $ip if $ipe ge $ia and $ipe le $ib;
} else {
return $ip if $ipe eq ipe($i);
}
}
}
return '';
}
# ip expand (ipv4/ipv6)
sub ipe {
local $_ = shift;
if (/^\d+\.\d+\.\d+\.\d+$/) {
s/\b(\d\d?)\b/sprintf "%03d",$1/ge;
} elsif (/^[:\w]+:\w+$/) {
s/\b(\w+)\b/sprintf "%04s",$1/ge;
s/^:/0000:/;
while (s/::/::0000:/) { last if length > 39 }
s/::/:/;
} else {
$_ = '';
}
return $_;
}
sub filename {
my $file = shift;
my $filename;
if (open $file,'<',"$file/filename") {
$filename = <$file>||'';
chomp $filename;
close $file;
}
unless ($filename) {
$filename = $file;
$filename =~ s:.*/::;
}
return $filename;
}
# emulate a "find $dir"
sub find {
my $dir = shift;
my @files = ();
local $_;
if (opendir $dir,$dir) {
while (defined($_ = readdir $dir)) {
next if /^\.\.?$/;
my $df = "$dir/$_";
push @files,$df;
push @files,find($df) if -d $df and not -l $df;
}
closedir $dir;
}
return @files;
}
sub hexencode {
local $_ = shift;
s/%/%25/g;
s/([^!-~])/sprintf "%%%X",ord($1)/ige;
return $_;
}
sub hexdecode {
local $_ = shift;
s/%([a-f0-9]{2})/chr(hex($1))/ige;
return $_;
}
sub urlencode {
local $_ = shift;
# $_ = encode_utf8($_) if utf8::is_utf8($_);
s/(^[.~]|[^\d\/a-z_@.,=:~^+-])/sprintf "%%%X",ord($1)/ige;
return $_;
}
sub urldecode {
local $_ = shift;
s/%([a-f0-9]{2})/chr(hex($1))/gie;
return $_;
}
sub htmlencode {
local $_ = shift;
s/&/&/g;
s/</</g;
return $_;
}
sub htmlquote {
local $_ = shift;
s/&/&/g;
s/</</g;
s/\"/"/g;
s/\'/'/g;
return $_;
}
# shell pattern to Perl regular expression
sub shp2prx {
local $_ = shift;
s:([^\w\[\]^/*?-]):\\$1:g;
s:([^*])\*([^*]):$1\[^/\]*$2:g;
s:([^*])\*$:$1\[^/\]*:;
s:^\*([^*]):[^/]*$1:;
s:\*\*:.*:g;
s:\?:.:g;
return $_;
}
# file archive sharing log
sub faslog {
my $shared = shift;
my $msg = "@_";
my $log = "$shared/log";
# do not log special share _
return if basename($shared) eq '_';
$msg =~ s/\n/ /g;
$msg =~ s/\s+$//;
$msg = sprintf "%s %s %s\n",isodate(time),$RA,$msg;
if ($share_logging and open $log,'>>',$log) {
flock $log,LOCK_EX;
seek $log,0,SEEK_END;
printf {$log} $msg;
close $log;
}
}
# file and document log
sub fdlog {
my ($log,$file,$s,$size) = @_;
my $ra = $RA||'-';
my $msg;
$ra .= '/'.$ENV{HTTP_X_FORWARDED_FOR} if $ENV{HTTP_X_FORWARDED_FOR};
$ra =~ s/\s//g;
$file =~ s:/data$::;
$msg = sprintf "%s [%s_%s] %s %s %s/%s\n",
isodate(time),$$,$ENV{REQUESTCOUNT},$ra,encode_Q($file),$s,$size;
writelog($log,$msg);
}
# extra (debug) log
sub xdbglog {
my @caller;
my $log = '/tmp/fex.log';
if (open $log,'>>',$log) {
@caller = caller(1) or @caller = caller;
printf {$log} "%s %s::%s() line %s:\n",
isodate(time),$caller[1],$caller[3]||'',$caller[2];
print {$log} "@_\n";
close $log;
}
}
sub debuglog {
my $prg = $0;
local $_;
return unless $debug and @_;
unless ($debuglog and fileno $debuglog) {
my $ddir = "$spooldir/.debug";
mkdir $ddir,0770 unless -d $ddir;
$prg =~ s:.*/::;
$prg = untaint($prg);
$debuglog = sprintf("%s/%s_%s_%s.%s",
$ddir,time,$$,$ENV{REQUESTCOUNT}||0,$prg);
$debuglog =~ s/\s/_/g;
# http://perldoc.perl.org/perlunifaq.html#What-is-a-%22wide-character%22%3f
# open $debuglog,'>>:encoding(UTF-8)',$debuglog or return;
open $debuglog,'>>',$debuglog or return;
# binmode($debuglog,":utf8");
autoflush $debuglog 1;
# printf {$debuglog} "\n### %s ###\n",isodate(time);
}
while ($_ = shift @_) {
$_ = encode_utf8($_) if utf8::is_utf8($_);
s/\n*$/\n/;
# s/<.+?>//g; # remove HTML # can be <COMMENT>!
print {$debuglog} $_;
print "DEBUG: $_" if -t;
}
}
# extra debug log
sub errorlog {
my $prg = $0;
my $msg = "@_";
my $ra = $RA||'-';
$ra .= '/'.$ENV{HTTP_X_FORWARDED_FOR} if $ENV{HTTP_X_FORWARDED_FOR};
$ra =~ s/\s//g;
$prg =~ s:.*/::;
$msg =~ s/[\r\n]+$//;
$msg =~ s/[\r\n]+/ /;
$msg =~ s/\s*<p>.*//;
$msg = sprintf "%s %s %s %s\n",isodate(time),$prg,$ra,$msg;
writelog('error.log',$msg);
}
sub writelog {
my $log = shift;
my $msg = shift;
$msg = encode_utf8($msg) if utf8::is_utf8($msg);
foreach my $logdir (@logdir) {
if (open $log,'>>',"$logdir/$log") {
flock $log,LOCK_EX;
seek $log,0,SEEK_END;
print {$log} $msg;
close $log;
}
}
}
# failed authentification log
sub faillog {
my $request = shift;
my $n = 1;
if ($RA and $faillog and open $faillog,"+>>$faillog") {
flock($faillog,LOCK_EX);
seek $faillog,0,SEEK_SET;
$n++ while <$faillog>;
printf {$faillog} "%s %s\n",isodate(time),$request;
close $faillog;
if ($max_fail and $n > $max_fail and
not ($ENV{HTTP_X_FORWARDED_FOR}||$ENV{HTTP_VIA}) and
($ENV{HTTP_USER_AGENT}||'') !~ /^fex/)
{
if ($max_fail_handler) {
&$max_fail_handler($RA);
} else {
http_die(
"Too many login failures from $RA : IP BLOCKED",
"Contact $admin for details."
);
}
}
}
}
# remove all white space
sub despace {
local $_ = shift;
s/\s//g;
return $_;
}
# superquoting
sub qqq {
local $_ = shift;
my ($s,@s);
my $i = '';
my $q = "[\'\"]"; # quote delimiter chars " and '
# remove comment
while (s/\n#.*?\n/\n/) {}
# remove first newline and look for default indention
s/^((\d+)?)?\n// and $i = ' ' x ($2||0);
# remove trailing spaces at end
s/[ \t]*?$//;
@s = split "\n";
# first line have a quote delimiter char?
if (/^\s+$q/) {
# remove heading spaces and delimiter chars
foreach (@s) {
s/^\s*$q//;
s/$q\s*$//;
}
} else {
# find the line with the fewest heading spaces (and count them)
# (beware of tabs!)
$s = length;
foreach (@s) {
if (/^( *)\S/ and length($1) < $s) { $s = length($1) };
}
# adjust indention
foreach (@s) {
s/^ {$s}/$i/;
}
}
return join("\n",@s)."\n";
}
# print superquoted
sub pq {
my $H = STDOUT;
local $_;
if (@_ > 1 and defined fileno $_[0]) { $H = shift }
$_ = qqq(@_);
# print needs need binary data, not wide characters
## binmode($H,':utf8'); # does not work correctly
$_ = encode_utf8($_) if utf8::is_utf8($_);
print {$H} $_;
}
# print needs need binary data, not wide characters
sub printb {
local $_;
foreach (@_) {
print utf8::is_utf8($_) ? encode_utf8($_) : $_;
}
}
# check sender quota
sub check_sender_quota {
my $sender = shift;
my $squota = $sender_quota||0;
my $du = 0;
my ($file,$size,%file,$data,$upload,$share);
local $_;
if (open $qf,'<',"$spooldir/$sender/\@QUOTA") {
while (<$qf>) {
s/#.*//;
$squota = $1 if /sender.*?(\d+)/i;
}
close $qf;
}
foreach $file (glob "$spooldir/*/$sender/*") {
$data = "$file/data";
$upload = "$file/upload";
if (not -l $data and $size = -s $data) {
# count hard links only once (= same inode)
my $i = (stat($data))[1]||0;
unless ($file{$i}) {
$du += $size;
$file{$i} = $i;
}
} elsif (-f $upload) {
# count hard links only once (= same inode)
my $i = (stat($upload))[1]||0;
unless ($file{$i}) {
$du += readlink("$file/size")||0;
$file{$i} = $i;
}
}
}
foreach $size (glob "$sender/SHARE/*/archives/*/*/size") {
$du += readlink($size)||0;
}
foreach (glob("$sender/DOX/.*.du")) {
if (m:(.+)/\.(.+)\.du: and -d "$1/$2" and my $s = readlink) {
$du += $s*1024;
}
}
return($squota,int($du/$MB));
}
# check recipient quota
sub check_recipient_quota {
my $recipient = shift;
my $rquota = $recipient_quota||0;
my $du = 0;
my ($file,$size,$share);
local $_;
if (open my $qf,'<',"$recipient/\@QUOTA") {
while (<$qf>) {
s/#.*//;
$rquota = $1 if /recipient.*?(\d+)/i;
}
close $qf;
}
foreach $file (glob "$recipient/*/*") {
if (-f "$file/upload" and $size = readlink "$file/size") {
$du += $size;
} elsif (not -l "$file/data" and $size = -s "$file/data") {
$du += $size;
}
}
foreach $size (glob "$recipient/SHARE/*/archives/*/*/size") {
$du += readlink($size)||0;
}
foreach my $d (glob("$recipient/DOX/.*.du")) {
$du += (readlink($d)||0)*1024;
}
return($rquota,int($du/$MB));
}
# dox disk usage
sub doxdu {
my $user = shift;
my $folder = shift;
my $doxd = "$spooldir/$user/DOX";
my $duf;
my @folder = ();
local $_;
if (-d $doxd) {
if (defined $folder) {
@folder = ("$doxd/$folder");
} else {
foreach my $f (glob "$doxd/*") {
push @folder,$f if -l $f and -d $f;
}
}
foreach my $f (@folder) {
if ($f =~ m:(.+)/(.+):) {
$duf = "$1/.$2.du";
} else {
next;
}
unlink $duf;
if (-l $f and -d $f) {
if (my @f = grep m:\Q$f\E_\d+_\d+$:,glob($f.'_*')) {
symlink untaint(ddu(@f)),$duf;
}
}
}
}
}
# directory disk usage (kB)
sub ddu {
my $du = 0;
local %DU;
local $ddu = sub {
my $dir = shift;
my $du = 0;
opendir $dir,$dir or return 0;
while (defined (my $file = readdir $dir)) {
next if $file eq '.' or $file eq '..';
my $df = "$dir/$file";
my @s = lstat($df) or next;
if (-l _) {
$du += 4;
next;
}
if (-d _) {
$du += 4;
next if $file =~ /^\.snapshots?$/;
$du += &$ddu($df);
next;
}
if (-f _) {
my $i = "$s[0]:$s[1]";
$du += $DU{$i} ? 4 : int(($s[7]+4095)/4096)*4;
$DU{$i}++;
next;
}
$du += 4;
}
closedir $dir;
return $du;
};
foreach my $dir (@_) {
$du += &$ddu($dir);
}
return $du;
}
sub getline {
my $file = shift;
local $_;
chomp($_ = <$file> // '');
return $_;
}
sub readline1 {
my $file = shift;
local $_;
if (open $file,$file) {
$_ = <$file> // '';
close $file;
chomp;
}
return $_;
}
# (shell) wildcard matching
sub wcmatch {
local $_ = shift;
my $p = quotemeta shift;
$p =~ s/\\\*/.*/g;
$p =~ s/\\\?/./g;
$p =~ s/\\\[/[/g;
$p =~ s/\\\]/]/g;
return /$p/;
}
sub logout {
my $logout;
if ($skey) { $logout = "/fup?logout=skey:$skey" }
elsif ($gkey) { $logout = "/fup?logout=gkey:$gkey" }
elsif ($akey) { $logout = "/fup?logout=akey:$akey" }
else { $logout = "/fup?logout" }
return qqq(qq(
'<p>'
'<form name="logout" action="$logout">'
' <input type="submit" name="logout" value="logout">'
'</form>'
'<p>'
));
}
# print data dump of global or local variables in HTML
# input musst be a string, eg: '%ENV'
sub DD {
my $v = shift;
local $_;
$n =~ s/.//;
$_ = eval(qq(use Data::Dumper;Data::Dumper->Dump([\\$v])));
s/\$VAR1/$v/;
s/&/&/g;
s/</</g;
print "<pre>\n$_\n</pre>\n";
}
# make symlink
sub mksymlink {
my ($file,$link) = @_;
unlink $file;
return symlink untaint($link),$file;
}
# copy file (and modify) or symlink
# returns chomped file contents or link name
# preserves permissions and time stamps
sub copy {
my ($from,$to,$sx,$ss) = @_;
my $link;
local $/;
local $_;
$to .= '/'.basename($from) if -d $to;
if (defined($link = readlink $from)) {
mksymlink($to,$link);
return $link;
} else {
open $from,'<',$from or return;
open $to,'>',$to or return;
$_ = <$from>;
close $from;
s/$sx/$ss/g if defined $ss;
print {$to} $_;
close $to or http_die("internal error: $to - $!");
if (my @s = stat($from)) {
chmod $s[2],$to;
utime @s[8,9],$to unless $mod;
}
chomp;
return $_;
}
}
sub slurp {
my $file = shift;
local $_;
local $/;
if (open $file,$file) {
$_ = <$file>;
close $file;
}
return $_;
}
# read one line from STDIN (net socket) and assign it to $_
# return number of read bytes
# also set global variable $RB (read bytes)
sub nvt_read {
my $len = 0;
if (defined ($_ = <STDIN>)) {
debuglog($_);
$len = length;
$RB += $len;
s/\r?\n//;
}
return $len;
}
# read forward to given pattern
sub nvt_skip_to {
my $pattern = shift;
while (&nvt_read) { return if /$pattern/ }
}
# HTTP GET and POST parameters
# (not used by fup)
# fills global variable %PARAM :
# normal parameter is $PARAM{$parameter}
# file parameter is $PARAM{$parameter}{filename} $PARAM{$parameter}{data}
sub parse_parameters {
my $cl = $ENV{X_CONTENT_LENGTH} || $ENV{CONTENT_LENGTH} || 0;
my $data = '';
my $filename;
local $_;
our %PARAM = ();
if ($cl > 128*$MB) {
http_die("request too large");
}
# binmode(STDIN,':raw');
$_ = $ENV{QUERY_STRING};
s/%5E/^/g;
if (/&/) {
foreach (split(/&/)) {
if (/(.+?)=(.*)/) { $PARAM{$1} = $2 }
else { $PARAM{$_} = $_ }
}
} elsif (/\?/) {
foreach (split(/\?/)) {
if (/(.+?)=(.*)/) { $PARAM{$1} = $2 }
else { $PARAM{$_} = $_ }
}
} else {
if (/(.+?)=(.*)/) { $PARAM{$1} = $2 }
else { $PARAM{$_} = $_ }
}
$_ = $ENV{CONTENT_TYPE}||'';
if ($ENV{REQUEST_METHOD} eq 'POST' and /boundary=\"?([\w\-\+\/_]+)/) {
my $boundary = $1;
while ($RB<$cl and &nvt_read) { last if /^--\Q$boundary/ }
# continuation lines are not checked!
while ($RB<$cl and &nvt_read) {
$filename = '';
if (/^Content-Disposition:.*\s*filename="(.+?)"/i) {
$filename = $1;
}
if (/^Content-Disposition:\s*form-data;\s*name="(.+?)"/i) {
my $p = $1;
# skip rest of mime part header
while ($RB<$cl and &nvt_read) { last if /^\s*$/ }
$data = '';
while (<STDIN>) {
if ($p =~ /password/i) {
debuglog('*' x length)
} else {
debuglog($_)
}
$RB += length;
last if /^--\Q$boundary/;
$data .= $_;
}
unless (defined $_) { die "premature end of HTTP POST\n" }
$data =~ s/\r?\n$//;
if ($filename) {
$PARAM{$p}{filename} = $filename;
$PARAM{$p}{data} = $data;
} else {
$PARAM{$p} = $data;
}
last if /^--\Q$boundary--/;
}
}
}
}
# name based virtual host?
sub vhost {
my $hh = shift; # HTTP_HOST
my $vhost;
my $locale = $ENV{LOCALE};
# memorized vhost? (default is in fex.ph)
%vhost = split(':',$ENV{VHOST}) if $ENV{VHOST};
if (%vhost and $hh and $hh =~ s/^([\w\.-]+).*/$1/) {
if ($vhost = $vhost{$hh} and -f "$vhost/lib/fex.ph") {
$ENV{VHOST} = "$hh:$vhost"; # memorize vhost for next run
$ENV{FEXLIB} = $FEXLIB = "$vhost/lib";
$logdir = $spooldir = "$vhost/spool";
$docdir = "$vhost/htdocs";
@logdir = ($logdir);
if ($locale and -e "$vhost/locale/$locale/lib/fex.ph") {
$ENV{FEXLIB} = $FEXLIB = "$vhost/locale/$locale/lib";
}
require "$FEXLIB/fex.ph" or die "$0: cannot load $FEXLIB/fex.ph - $!";
$ENV{SERVER_NAME} = $hostname;
@doc_dirs = ($docdir);
foreach my $ld (glob "$FEXHOME/locale/*/htdocs") {
push @doc_dirs,$ld;
}
return $vhost;
}
}
}
sub gpg_encrypt {
my ($plain,$to,$keyring,$from) = @_;
my ($pid,$pi,$po,$pe,$enc,$err);
local $_;
$pe = gensym;
$keyring =~ s/[;<>()|\`]//;
$to =~ s/[;<>()|\`]//;
$pid = open3($po,$pi,$pe,untaint(
"gpg --batch --trust-model always --keyring '$keyring'".
" -a -e -r '$bcc' -r '$to'"
)) or return;
print {$po} "\n",$plain,"\n";
close $po;
$enc .= $_ while <$pi>;
$err .= $_ while <$pe>;
errorlog("($from --> $to) $err") if $err;
close $pi;
close $pe;
waitpid($pid,0);
return $enc;
}
sub fcrypt {
my $encryption = shift;
my $salt = shift || randstring(16);
my $password = shift;
my $hash;
my $i = 1;
$password =~ s/\s//;
if ($encryption eq '64kHMACSHA512B') {
$hash = hmac_sha512($password,$salt);
foreach (2..2**16-1) {
$i++;
$hash = hmac_sha512($password,$hash.$i);
}
$i++;
$hash = hmac_sha512_base64($password,$hash.$i);
} elsif (ref($$encryption) eq 'CODE') {
$hash = &$$encryption($password,$salt);
} else {
http_die("unknown encryption $encryption");
exit 1;
}
return "$encryption $salt $hash";
}
## example for fex.ph:
# $encryption = 'NONE';
# $NONE = sub { return $_[1] };
sub move {
our ($move);
my $ddir = pop;
my $move_errors = '';
my @dstat = stat $ddir or return "cannot stat $ddir - $!\n";
unless (-d $ddir) {
return "destination $ddir is not a directory\n";
}
unless (-w $ddir) {
return "destination directory $ddir is not writable\n";
}
local $move = sub {
my $sdir = shift;
my @sstat;
my $dfile;
local $_;
foreach (my @__ = @_) {
unless (@sstat = lstat) {
$move_errors .= "cannot stat '$_' - $!\n";
next;
}
if ($sstat[0] != $dstat[0]) {
$move_errors .= "cannot link $_ to $ddir - different filesystem\n";
next;
}
s:/+:/:g;
s:/$::;
$dfile = "$ddir/$sdir".basename($_);
$dfile =~ s:/+:/:g;
if (-l or not -d) {
if (-d $dfile and not -l $dfile) {
if ($opt_f) {
system qw'rm -rf',$dfile;
} else {
$move_errors .= "cannot replace directory '$dfile'\n";
next;
}
}
if (my @s = lstat $dfile) {
if ("@s" eq "@sstat") {
unless (unlink) {
$move_errors .= "cannot remove '$_' - $!\n";
}
next;
}
unless (unlink $dfile) {
$move_errors .= "cannot remove '$dfile' - $!\n";
next;
}
}
if (rename $_,$dfile) {
print "'$_' -> '$dfile'\n" if $opt_v;
} else {
$move_errors .= "cannot move '$_' to '$dfile' - $!\n";
next;
}
} elsif (-d) {
if (-d $dfile) {
my $dir = $_;
my @files = ();
if (opendir $dir,$dir) {
@files = grep { not /^\.\.?$/ } readdir($dir);
closedir $dir;
@files = sort { lc $a cmp lc $b } @files;
foreach (@files) {
/(.+)/;
$_ = $dir.'/'.decode_utf8($1);
}
&$move($sdir.basename($dir).'/',@files);
unless (rmdir $dir) {
$move_errors .= "cannot rmdir $dir - $!\n";
next;
}
} else {
$move_errors .= "cannot read $dir - $!\n";
next;
}
} else {
if (lstat $dfile) {
unlink $dfile;
}
if (rename $_,$dfile) {
print "'$_' -> '$dfile'\n" if $opt_v;
} else {
$move_errors .= "cannot move '$_' to '$dfile' - $!\n";
next;
}
}
} else {
$move_errors .= "unknown file type '$_'\n";
next;
}
}
};
&$move('',@_);
return $move_errors;
}
sub mtime {
my @s = stat(shift) or return;
return $s[9];
}
sub lmtime {
my @s = lstat(shift) or return;
return @s?$s[9]:0;
}
# wildcard * to perl regexp
sub quotewild {
local $_ = quotemeta shift;
s/\\\*/.*/g; # allow wildcard *
return $_;
}
sub readlink_ {
my $file = shift;
return (readlink($file)||'');
}
sub asort {
return sort { lc($a) cmp lc($b) } @_;
}
sub mhfrom {
my $mfrom = shift;
my $hfrom;
my $fex_sender = $::fex_sender;
$mfrom .= '@'.$mdomain if $mdomain and $mfrom !~ /@/;
$hfrom = $mfrom;
$hfrom =~ s/@/(at)/g;
if ($fex_sender and @local_domains) {
foreach my $ld (@local_domains) {
$fex_sender = 0 if $mfrom =~ /\@\Q$ld\E$|\@.+\.\Q$ld\E$/;
}
}
if ($fex_sender) {
$mfrom =~ s/\@/_/;
$mfrom = "fex+$mfrom\@$hostname";
}
return ($mfrom,$hfrom);
}
# extract locale functions into hash of subroutine references
# e.g. \&german ==> $notify{german}
sub locale_functions {
my $locale = shift;
local $/;
local $_;
if ($locale and open my $fexpp,"$FEXHOME/locale/$locale/lib/fex.pp") {
$_ = <$fexpp>;
s/.*\n(\#\#\# locale functions)/$1/s;
# sub xx {} ==> xx{$locale} = sub {}
s/\nsub (\w+)/\n\$$1\{$locale\} = sub/gs;
s/\n\}\n/\n\};\n/gs;
eval $_;
close $fexpp;
}
}
sub notify_locale {
my $dkey = shift;
my $status = shift || 'new';
my ($to,$keep,$locale,$file,$filename,$comment,$autodelete,$replyto,$mtime);
local $_;
if ($dkey =~ m:/.+/.+/:) {
$file = $dkey;
$dkey = readlink("$file/dkey")
or http_die("internal error: no $file/dkey");
} else {
$file = readlink("$dkeydir/$dkey")
or http_die("internal error: no DKEY $dkey");
}
$file = untaint($file);
$file =~ s:^\.\./::;
$filename = filename($file);
$to = $file;
$to =~ s:/.*::;
$mtime = mtime("$file/data") or http_die("internal error: no $file/data");
$comment = slurp("$file/comment") || '';
$replyto = readlink "$file/replyto" || '';
$autodelete = readlink "$file/autodelete"
|| readlink "$to/\@AUTODELETE"
|| $::autodelete;
$keep = readlink "$file/keep"
|| readlink "$to/\@KEEP"
|| $keep_default;
$locale = readlink "$to/\@LOCALE" || readlink "$file/locale" || 'english';
$_ = untaint("$FEXHOME/locale/$locale/lib/lf.pl");
require if -f;
$notify{'english'} = \¬ify; # local default notify function, not via lf.pl
unless ($notify{$locale}) {
$locale = 'english';
$notify{$locale} ||= \¬ify;
}
# return notify(
return &{$notify{$locale}}(
status => $status,
dkey => $dkey,
filename => $filename,
keep => $keep-int((time-$mtime)/$DS),
comment => $comment,
autodelete => $autodelete,
replyto => $replyto,
);
}
########################### locale functions ###########################
# Will be extracted by install process and saved in $FEXHOME/lib/lf.pl #
# You cannot modify them here without re-installing! #
########################################################################
# locale function!
sub notify {
# my ($status,$dkey,$filename,$keep,$warn,$comment,$autodelete) = @_;
my %P = @_;
my ($to,$from,$file,$mimefilename,$receiver,$warn,$comment,$status);
my ($size,$bytes,$header,$data,$replyto,$uurl,$alist,$filed,$share);
my ($mfrom,$mto,$dfrom,$dto,$hfrom);
my $proto = 'http';
my $days = 0;
my $durl = $::durl;
my $autodelete = '';
my ($index,$tools);
my $fileid = 0;
my $fua = $ENV{HTTP_USER_AGENT}||'';
my $warning = '';
my $disclaimer = '';
my $download = '';
my $keyring;
my $boundary = randstring(16);
my ($body,$enc_body);
return if $nomail;
$status = $P{status};
$warn = $P{warn}||2;
$autodelete = $P{autodelete}||$::autodelete;
$comment = $P{comment}||'';
$comment = encode_utf8($P{comment}||'') if utf8::is_utf8($comment);
$comment =~ s/^!\*!//; # multi download allow flag
chomp $comment;
$index = $tools = $durl;
$index =~ s/fop$/index.html/;
$tools =~ s/fop$/tools.html/;
if ($share = $P{share}) {
$filed = $P{filed};
$to = $P{to};
$from = $P{from};
$file = $filename = $P{filename};
$durl = $P{durl};
} else {
$filed = untaint(readlink("$dkeydir/$P{dkey}"));
$filed =~ s/^\.\.\///;
($to,$from,$file) = split('/',$filed);
$filename = strip_path($P{filename});
}
return '' unless $to;
return '' unless $from;
return '' unless length($filename);
# make download protocal same as upload protocol
if ($uurl = readlink("$filed/uurl") and $uurl =~ /^(https?):/) {
$proto = $1;
$durl =~ s/^https?:/$proto:/;
$durl =~ s/:\d+\b// if $durl =~ /^https:/;
}
($mfrom,$hfrom) = mhfrom($from);
$mto = $to;
$replyto = $P{replyto}||$from;
$keyring = $to.'/@GPG';
$header = "From: <$mfrom> ($hfrom via F*EX service $hostname)\n";
$header .= "Reply-To: <$replyto>\n" if $replyto ne $mfrom;
$header .= "To: <$mto>\n";
$data = "$filed/data";
$alist = "$filed/alist";
$size = $bytes = -s $data;
return unless $size;
$warning =
"We recommend fexget or fexit for download,\n".
"because these clients can resume the download after an interruption.\n".
"See $tools";
# if ($nowarning) {
# $warning = '';
# } else {
# $warning =
# "Please avoid download with Internet Explorer, ".
# "because it has too many bugs.\n\n";
# }
if ($filename =~ /\.(tar|tgz|tar\.gz|zip|7z|arj|rar)$/) {
$warning .= "\n\n".
"$filename is a container file.\n".
"You can unpack it for example with 7zip ".
"(http://www.7-zip.org/download.html)";
}
if ($limited_download =~ /^y/i and $from ne $to) {
$warning .=
"\n\n".
'This download link only works for you, you cannot distribute it.';
}
if ($size < 2048) {
$size = "$size Bytes";
} elsif ($size/1024 < 2048) {
$size = int($size/1024)." kB";
} else {
$size = int($size/$MB)." MB";
}
if ($autodelete eq 'YES') {
$autodelete = "WARNING: After download (or view with a web browser!), "
. "the file will be deleted!";
} elsif ($autodelete eq 'DELAY') {
$autodelete = "WARNING: When you download the file it will be deleted "
. "soon afterwards!";
} else {
$autodelete = '';
}
if (-s $keyring) {
$mimefilename = '';
} else {
$mimefilename = $filename;
if ($mimefilename =~ s/([\?\=\x00-\x1F\x7F-\xFF])/sprintf("=%02X",ord($1))/eog) {
$mimefilename =~ s/ /_/g;
$mimefilename = '=?UTF-8?Q?'.$mimefilename.'?=';
}
}
unless ($fileid = readlink("$filed/id")) {
my @s = stat($data);
$fileid = @s ? $s[1].$s[9] : 0;
}
if ($share) {
$header .= "Subject: new F*EX share archive: $share:$mimefilename\n";
} elsif ($status =~ /new/) {
$days = $P{keep};
$header .= "Subject: F*EX-upload: $mimefilename\n";
} else {
$days = $warn;
$header .= "Subject: reminder F*EX-upload: $mimefilename\n";
}
$header .= "X-FEX-Client-Address: $fra\n" if $fra;
$header .= "X-FEX-Client-Agent: $fua\n" if $fua;
if ($share) {
$download = "$durl\n";
} else {
foreach my $u (@durl?@durl:($durl)) {
my $durl = sprintf("%s/%s/%s",$u,$P{dkey},urlencode($filename));
$header .= "X-FEX-URL: $durl\n" unless -s $keyring;
$download .= "$durl\n";
}
}
$header .=
"X-FEX-Filesize: $bytes\n".
"X-FEX-File-ID: $fileid\n".
"X-FEX-Fexmaster: $admin\n".
"X-Mailer: F*EX\n".
"MIME-Version: 1.0\n";
if ($comment =~ s/^\[(\@(.*?))\]\s*//) {
$receiver = "group $1";
if ($_ = readlink "$from/\@GROUP/$2" and m:^../../(.+?)/:) {
$receiver .= " (maintainer: $1)";
}
} else {
$receiver = 'you';
}
if ($days == 1) { $days .= " day" }
else { $days .= " days" }
# explicite sender set in fex.ph?
if ($sender_from) {
# obsoleted feature!
map { s/^From: <\Q$mfrom/From: <$sender_from/ } $header;
open $sendmail,'|-',$sendmail,$mto,$bcc
or http_die("cannot start sendmail - $!");
} else {
# for special remote domains do not use same domain in From,
# because remote MTA will probably reject this email
$dfrom = $1 if $mfrom =~ /@(.+)/;
$dto = $1 if $mto =~ /@(.+)/;
if ($dfrom and $dto and @remote_domains and
grep { $dfrom =~ /(^|\.)$_$/ and $dto =~ /(^|\.)$_$/ } @remote_domains)
{
$header =~ s/(From: <)\Q$mfrom\E(.*?)\n/$1$admin$2\nReply-To: $mfrom\n/;
open $sendmail,'|-',$sendmail,$mto,$bcc
or http_die("cannot start sendmail - $!");
} else {
open $sendmail,'|-',$sendmail,'-f',$mfrom,$mto,$bcc
or http_die("cannot start sendmail - $!");
}
}
if (length($comment)) {
$comment = '. ' if $comment eq '.';
$comment = "\n$comment\n"
}
if ($share) {
$disclaimer = "\n$::disclaimer\n" if $::disclaimer;
$body = qqq(qq(
'$comment'
'$from has uploaded the archive file'
' "$filename" ($size)'
'for share $share. See'
''
'$download'
'$disclaimer'
));
} else {
if ($comment =~ s/\n!(shortmail|\.)!\s*//i
or (readlink("$to/\@NOTIFICATION")||'') =~ /short/i
) {
$body = $comment."\n".$download."\n".$size;
} else {
$disclaimer = slurp("$from/\@DISCLAIMER") || qqq(qq(
'$warning'
''
'F*EX is not an archive, it is a transfer system for personal files.'
'For more information see $index'
''
'Questions? ==> F*EX admin: $admin'
));
$disclaimer .= "\n$::disclaimer\n" if $::disclaimer;
$body = qqq(qq(
'$comment'
'$from has uploaded the file'
' "$filename" ($size)'
'for $receiver. Use'
''
'$download'
'to download this file within $days.'
''
'$autodelete'
''
'$disclaimer'
));
}
}
$body =~ s/\n\n+/\n\n/g;
if (-s $keyring) {
$enc_body = gpg_encrypt($body,$to,$keyring,$from);
}
if ($enc_body) {
# RFC3156
$header .= qqq(qq(
'Content-Type: multipart/encrypted; protocol="application/pgp-encrypted";'
'\tboundary="$boundary"'
'Content-Disposition: inline'
));
$body = qqq(qq(
'--$boundary'
'Content-Type: application/pgp-encrypted'
'Content-Disposition: attachment'
''
'Version: 1'
''
'--$boundary'
'Content-Type: application/octet-stream'
'Content-Disposition: inline; filename="fex.pgp"'
''
'$enc_body'
'--$boundary--'
));
} else {
$header .=
"Content-Type: text/plain; charset=UTF-8\n".
"Content-Transfer-Encoding: 8bit\n";
}
print {$sendmail} $header,"\n",$body;
close $sendmail and return $to;
http_die("cannot send notification email (sendmail error $!)");
}
# locale function!
sub reactivation {
my ($expire,$user) = @_;
my $fexsend = "$FEXHOME/bin/fexsend";
my $reactivation = "$FEXLIB/reactivation.txt";
return if $nomail;
if (-x $fexsend) {
if ($locale) {
my $lr = "$FEXHOME/locale/$locale/lib/reactivation.txt";
$reactivation = $lr if -f $lr and -s $lr;
}
$fexsend .= " -M -D -k 30 -C"
." 'Your F*EX account $user has been inactive for $expire days,"
." you must download this file to reactivate it."
." Otherwise your account will be deleted.'"
." $reactivation $user";
# on error show STDOUT and STDERR
my $fo = `$fexsend 2>&1`;
warn $fexsend."\n".$fo if $?;
} else {
warn "$0: cannot execute $fexsend for reactivation()\n";
}
}
1;
|