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
|
{==============================================================================|
| Project : Ararat Synapse | 004.000.000 |
|==============================================================================|
| Content: FTP client |
|==============================================================================|
| Copyright (c)1999-2011, Lukas Gebauer |
| All rights reserved. |
| |
| Redistribution and use in source and binary forms, with or without |
| modification, are permitted provided that the following conditions are met: |
| |
| Redistributions of source code must retain the above copyright notice, this |
| list of conditions and the following disclaimer. |
| |
| Redistributions in binary form must reproduce the above copyright notice, |
| this list of conditions and the following disclaimer in the documentation |
| and/or other materials provided with the distribution. |
| |
| Neither the name of Lukas Gebauer nor the names of its contributors may |
| be used to endorse or promote products derived from this software without |
| specific prior written permission. |
| |
| THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR |
| ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH |
| DAMAGE. |
|==============================================================================|
| The Initial Developer of the Original Code is Lukas Gebauer (Czech Republic).|
| Portions created by Lukas Gebauer are Copyright (c) 1999-2010. |
| All Rights Reserved. |
|==============================================================================|
| Contributor(s): |
| Petr Esner <petr.esner@atlas.cz> |
|==============================================================================|
| History: see HISTORY.HTM from distribution package |
| (Found at URL: http://www.ararat.cz/synapse/) |
|==============================================================================}
{: @abstract(FTP client protocol)
Used RFC: RFC-959, RFC-2228, RFC-2428
}
{$IFDEF FPC}
{$MODE DELPHI}
{$ENDIF}
{$H+}
{$TYPEINFO ON}// Borland changed defualt Visibility from Public to Published
// and it requires RTTI to be generated $M+
{$M+}
{$IFDEF UNICODE}
{$WARN IMPLICIT_STRING_CAST OFF}
{$WARN IMPLICIT_STRING_CAST_LOSS OFF}
{$ENDIF}
unit ftpsend;
interface
uses
SysUtils, Classes,
blcksock, synautil, synaip, synsock;
const
cFtpProtocol = '21';
cFtpDataProtocol = '20';
{:Terminating value for TLogonActions}
FTP_OK = 255;
{:Terminating value for TLogonActions}
FTP_ERR = 254;
type
{:Array for holding definition of logon sequence.}
TLogonActions = array [0..17] of byte;
{:Procedural type for OnStatus event. Sender is calling @link(TFTPSend) object.
Value is FTP command or reply to this comand. (if it is reply, Response
is @True).}
TFTPStatus = procedure(Sender: TObject; Response: Boolean;
const Value: string) of object;
{: @abstract(Object for holding file information) parsed from directory
listing of FTP server.}
TFTPListRec = class(TObject)
private
FFileName: String;
FDirectory: Boolean;
FReadable: Boolean;
FFileSize: int64;
FFileTime: TDateTime;
FOriginalLine: string;
FMask: string;
FPermission: String;
public
{: You can assign another TFTPListRec to this object.}
procedure Assign(Value: TFTPListRec); virtual;
{:name of file}
property FileName: string read FFileName write FFileName;
{:if name is subdirectory not file.}
property Directory: Boolean read FDirectory write FDirectory;
{:if you have rights to read}
property Readable: Boolean read FReadable write FReadable;
{:size of file in bytes}
property FileSize: int64 read FFileSize write FFileSize;
{:date and time of file. Local server timezone is used. Any timezone
conversions was not done!}
property FileTime: TDateTime read FFileTime write FFileTime;
{:original unparsed line}
property OriginalLine: string read FOriginalLine write FOriginalLine;
{:mask what was used for parsing}
property Mask: string read FMask write FMask;
{:permission string (depending on used mask!)}
property Permission: string read FPermission write FPermission;
end;
{:@abstract(This is TList of TFTPListRec objects.)
This object is used for holding lististing of all files information in listed
directory on FTP server.}
TFTPList = class(TObject)
protected
FList: TList;
FLines: TStringList;
FMasks: TStringList;
FUnparsedLines: TStringList;
Monthnames: string;
BlockSize: string;
DirFlagValue: string;
FileName: string;
VMSFileName: string;
Day: string;
Month: string;
ThreeMonth: string;
YearTime: string;
Year: string;
Hours: string;
HoursModif: Ansistring;
Minutes: string;
Seconds: string;
Size: Ansistring;
Permissions: Ansistring;
DirFlag: string;
function GetListItem(Index: integer): TFTPListRec; virtual;
function ParseEPLF(Value: string): Boolean; virtual;
procedure ClearStore; virtual;
function ParseByMask(Value, NextValue, Mask: ansistring): Integer; virtual;
function CheckValues: Boolean; virtual;
procedure FillRecord(const Value: TFTPListRec); virtual;
public
{:Constructor. You not need create this object, it is created by TFTPSend
class as their property.}
constructor Create;
destructor Destroy; override;
{:Clear list.}
procedure Clear; virtual;
{:count of holded @link(TFTPListRec) objects}
function Count: integer; virtual;
{:Assigns one list to another}
procedure Assign(Value: TFTPList); virtual;
{:try to parse raw directory listing in @link(lines) to list of
@link(TFTPListRec).}
procedure ParseLines; virtual;
{:By this property you have access to list of @link(TFTPListRec).
This is for compatibility only. Please, use @link(Items) instead.}
property List: TList read FList;
{:By this property you have access to list of @link(TFTPListRec).}
property Items[Index: Integer]: TFTPListRec read GetListItem; default;
{:Set of lines with RAW directory listing for @link(parseLines)}
property Lines: TStringList read FLines;
{:Set of masks for directory listing parser. It is predefined by default,
however you can modify it as you need. (for example, you can add your own
definition mask.) Mask is same as mask used in TotalCommander.}
property Masks: TStringList read FMasks;
{:After @link(ParseLines) it holding lines what was not sucessfully parsed.}
property UnparsedLines: TStringList read FUnparsedLines;
end;
{:@abstract(Implementation of FTP protocol.)
Note: Are you missing properties for setting Username and Password? Look to
parent @link(TSynaClient) object! (Username and Password have default values
for "anonymous" FTP login)
Are you missing properties for specify server address and port? Look to
parent @link(TSynaClient) too!}
TFTPSend = class(TSynaClient)
protected
FOnStatus: TFTPStatus;
FSock: TTCPBlockSocket;
FDSock: TTCPBlockSocket;
FResultCode: Integer;
FResultString: string;
FFullResult: TStringList;
FAccount: string;
FFWHost: string;
FFWPort: string;
FFWUsername: string;
FFWPassword: string;
FFWMode: integer;
FDataStream: TMemoryStream;
FDataIP: string;
FDataPort: string;
FDirectFile: Boolean;
FDirectFileName: string;
FCanResume: Boolean;
FPassiveMode: Boolean;
FForceDefaultPort: Boolean;
FForceOldPort: Boolean;
FFtpList: TFTPList;
FBinaryMode: Boolean;
FAutoTLS: Boolean;
FIsTLS: Boolean;
FIsDataTLS: Boolean;
FTLSonData: Boolean;
FFullSSL: Boolean;
function Auth(Mode: integer): Boolean; virtual;
function Connect: Boolean; virtual;
function InternalStor(const Command: string; RestoreAt: int64): Boolean; virtual;
function DataSocket: Boolean; virtual;
function AcceptDataSocket: Boolean; virtual;
procedure DoStatus(Response: Boolean; const Value: string); virtual;
public
{:Custom definition of login sequence. You can use this when you set
@link(FWMode) to value -1.}
CustomLogon: TLogonActions;
constructor Create;
destructor Destroy; override;
{:Waits and read FTP server response. You need this only in special cases!}
function ReadResult: Integer; virtual;
{:Parse remote side information of data channel from value string (returned
by PASV command). This function you need only in special cases!}
procedure ParseRemote(Value: string); virtual;
{:Parse remote side information of data channel from value string (returned
by EPSV command). This function you need only in special cases!}
procedure ParseRemoteEPSV(Value: string); virtual;
{:Send Value as FTP command to FTP server. Returned result code is result of
this function.
This command is good for sending site specific command, or non-standard
commands.}
function FTPCommand(const Value: string): integer; virtual;
{:Connect and logon to FTP server. If you specify any FireWall, connect to
firewall and throw them connect to FTP server. Login sequence depending on
@link(FWMode).}
function Login: Boolean; virtual;
{:Logoff and disconnect from FTP server.}
function Logout: Boolean; virtual;
{:Break current transmission of data. (You can call this method from
Sock.OnStatus event, or from another thread.)}
procedure Abort; virtual;
{:Break current transmission of data. It is same as Abort, but it send abort
telnet commands prior ABOR FTP command. Some servers need it. (You can call
this method from Sock.OnStatus event, or from another thread.)}
procedure TelnetAbort; virtual;
{:Download directory listing of Directory on FTP server. If Directory is
empty string, download listing of current working directory.
If NameList is @true, download only names of files in directory.
(internally use NLST command instead LIST command)
If NameList is @false, returned list is also parsed to @link(FTPList)
property.}
function List(Directory: string; NameList: Boolean): Boolean; virtual;
{:Read data from FileName on FTP server. If Restore is @true and server
supports resume dowloads, download is resumed. (received is only rest
of file)}
function RetrieveFile(const FileName: string; Restore: Boolean): Boolean; virtual;
{:Send data to FileName on FTP server. If Restore is @true and server
supports resume upload, upload is resumed. (send only rest of file)
In this case if remote file is same length as local file, nothing will be
done. If remote file is larger then local, resume is disabled and file is
transfered from begin!}
function StoreFile(const FileName: string; Restore: Boolean): Boolean; virtual;
{:Send data to FTP server and assing unique name for this file.}
function StoreUniqueFile: Boolean; virtual;
{:Append data to FileName on FTP server.}
function AppendFile(const FileName: string): Boolean; virtual;
{:Rename on FTP server file with OldName to NewName.}
function RenameFile(const OldName, NewName: string): Boolean; virtual;
{:Delete file FileName on FTP server.}
function DeleteFile(const FileName: string): Boolean; virtual;
{:Return size of Filename file on FTP server. If command failed (i.e. not
implemented), return -1.}
function FileSize(const FileName: string): int64; virtual;
{:Send NOOP command to FTP server for preserve of disconnect by inactivity
timeout.}
function NoOp: Boolean; virtual;
{:Change currect working directory to Directory on FTP server.}
function ChangeWorkingDir(const Directory: string): Boolean; virtual;
{:walk to upper directory on FTP server.}
function ChangeToParentDir: Boolean; virtual;
{:walk to root directory on FTP server. (May not work with all servers properly!)}
function ChangeToRootDir: Boolean; virtual;
{:Delete Directory on FTP server.}
function DeleteDir(const Directory: string): Boolean; virtual;
{:Create Directory on FTP server.}
function CreateDir(const Directory: string): Boolean; virtual;
{:Return current working directory on FTP server.}
function GetCurrentDir: String; virtual;
{:Establish data channel to FTP server and retrieve data.
This function you need only in special cases, i.e. when you need to implement
some special unsupported FTP command!}
function DataRead(const DestStream: TStream): Boolean; virtual;
{:Establish data channel to FTP server and send data.
This function you need only in special cases, i.e. when you need to implement
some special unsupported FTP command.}
function DataWrite(const SourceStream: TStream): Boolean; virtual;
published
{:After FTP command contains result number of this operation.}
property ResultCode: Integer read FResultCode;
{:After FTP command contains main line of result.}
property ResultString: string read FResultString;
{:After any FTP command it contains all lines of FTP server reply.}
property FullResult: TStringList read FFullResult;
{:Account information used in some cases inside login sequence.}
property Account: string read FAccount Write FAccount;
{:Address of firewall. If empty string (default), firewall not used.}
property FWHost: string read FFWHost Write FFWHost;
{:port of firewall. standard value is same port as ftp server used. (21)}
property FWPort: string read FFWPort Write FFWPort;
{:Username for login to firewall. (if needed)}
property FWUsername: string read FFWUsername Write FFWUsername;
{:password for login to firewall. (if needed)}
property FWPassword: string read FFWPassword Write FFWPassword;
{:Type of Firewall. Used only if you set some firewall address. Supported
predefined firewall login sequences are described by comments in source
file where you can see pseudocode decribing each sequence.}
property FWMode: integer read FFWMode Write FFWMode;
{:Socket object used for TCP/IP operation on control channel. Good for
seting OnStatus hook, etc.}
property Sock: TTCPBlockSocket read FSock;
{:Socket object used for TCP/IP operation on data channel. Good for seting
OnStatus hook, etc.}
property DSock: TTCPBlockSocket read FDSock;
{:If you not use @link(DirectFile) mode, all data transfers is made to or
from this stream.}
property DataStream: TMemoryStream read FDataStream;
{:After data connection is established, contains remote side IP of this
connection.}
property DataIP: string read FDataIP;
{:After data connection is established, contains remote side port of this
connection.}
property DataPort: string read FDataPort;
{:Mode of data handling by data connection. If @False, all data operations
are made to or from @link(DataStream) TMemoryStream.
If @true, data operations is made directly to file in your disk. (filename
is specified by @link(DirectFileName) property.) Dafault is @False!}
property DirectFile: Boolean read FDirectFile Write FDirectFile;
{:Filename for direct disk data operations.}
property DirectFileName: string read FDirectFileName Write FDirectFileName;
{:Indicate after @link(Login) if remote server support resume downloads and
uploads.}
property CanResume: Boolean read FCanResume;
{:If true (default value), all transfers is made by passive method.
It is safer method for various firewalls.}
property PassiveMode: Boolean read FPassiveMode Write FPassiveMode;
{:Force to listen for dataconnection on standard port (20). Default is @false,
dataconnections will be made to any non-standard port reported by PORT FTP
command. This setting is not used, if you use passive mode.}
property ForceDefaultPort: Boolean read FForceDefaultPort Write FForceDefaultPort;
{:When is @true, then is disabled EPSV and EPRT support. However without this
commands you cannot use IPv6! (Disabling of this commands is needed only
when you are behind some crap firewall/NAT.}
property ForceOldPort: Boolean read FForceOldPort Write FForceOldPort;
{:You may set this hook for monitoring FTP commands and replies.}
property OnStatus: TFTPStatus read FOnStatus write FOnStatus;
{:After LIST command is here parsed list of files in given directory.}
property FtpList: TFTPList read FFtpList;
{:if @true (default), then data transfers is in binary mode. If this is set
to @false, then ASCII mode is used.}
property BinaryMode: Boolean read FBinaryMode Write FBinaryMode;
{:if is true, then if server support upgrade to SSL/TLS mode, then use them.}
property AutoTLS: Boolean read FAutoTLS Write FAutoTLS;
{:if server listen on SSL/TLS port, then you set this to true.}
property FullSSL: Boolean read FFullSSL Write FFullSSL;
{:Signalise, if control channel is in SSL/TLS mode.}
property IsTLS: Boolean read FIsTLS;
{:Signalise, if data transfers is in SSL/TLS mode.}
property IsDataTLS: Boolean read FIsDataTLS;
{:If @true (default), then try to use SSL/TLS on data transfers too.
If @false, then SSL/TLS is used only for control connection.}
property TLSonData: Boolean read FTLSonData write FTLSonData;
end;
{:A very useful function, and example of use can be found in the TFtpSend object.
Dowload specified file from FTP server to LocalFile.}
function FtpGetFile(const IP, Port, FileName, LocalFile,
User, Pass: string): Boolean;
{:A very useful function, and example of use can be found in the TFtpSend object.
Upload specified LocalFile to FTP server.}
function FtpPutFile(const IP, Port, FileName, LocalFile,
User, Pass: string): Boolean;
{:A very useful function, and example of use can be found in the TFtpSend object.
Initiate transfer of file between two FTP servers.}
function FtpInterServerTransfer(
const FromIP, FromPort, FromFile, FromUser, FromPass: string;
const ToIP, ToPort, ToFile, ToUser, ToPass: string): Boolean;
implementation
constructor TFTPSend.Create;
begin
inherited Create;
FFullResult := TStringList.Create;
FDataStream := TMemoryStream.Create;
FSock := TTCPBlockSocket.Create;
FSock.Owner := self;
FSock.ConvertLineEnd := True;
FDSock := TTCPBlockSocket.Create;
FDSock.Owner := self;
FFtpList := TFTPList.Create;
FTimeout := 300000;
FTargetPort := cFtpProtocol;
FUsername := 'anonymous';
FPassword := 'anonymous@' + FSock.LocalName;
FDirectFile := False;
FPassiveMode := True;
FForceDefaultPort := False;
FForceOldPort := false;
FAccount := '';
FFWHost := '';
FFWPort := cFtpProtocol;
FFWUsername := '';
FFWPassword := '';
FFWMode := 0;
FBinaryMode := True;
FAutoTLS := False;
FFullSSL := False;
FIsTLS := False;
FIsDataTLS := False;
FTLSonData := True;
end;
destructor TFTPSend.Destroy;
begin
FDSock.Free;
FSock.Free;
FFTPList.Free;
FDataStream.Free;
FFullResult.Free;
inherited Destroy;
end;
procedure TFTPSend.DoStatus(Response: Boolean; const Value: string);
begin
if assigned(OnStatus) then
OnStatus(Self, Response, Value);
end;
function TFTPSend.ReadResult: Integer;
var
s, c: AnsiString;
begin
FFullResult.Clear;
c := '';
repeat
s := FSock.RecvString(FTimeout);
if c = '' then
if length(s) > 3 then
if s[4] in [' ', '-'] then
c :=Copy(s, 1, 3);
FResultString := s;
FFullResult.Add(s);
DoStatus(True, s);
if FSock.LastError <> 0 then
Break;
until (c <> '') and (Pos(c + ' ', s) = 1);
Result := StrToIntDef(c, 0);
FResultCode := Result;
end;
function TFTPSend.FTPCommand(const Value: string): integer;
begin
FSock.Purge;
FSock.SendString(Value + CRLF);
DoStatus(False, Value);
Result := ReadResult;
end;
// based on idea by Petr Esner <petr.esner@atlas.cz>
function TFTPSend.Auth(Mode: integer): Boolean;
const
//if not USER <username> then
// if not PASS <password> then
// if not ACCT <account> then ERROR!
//OK!
Action0: TLogonActions =
(0, FTP_OK, 3,
1, FTP_OK, 6,
2, FTP_OK, FTP_ERR,
0, 0, 0, 0, 0, 0, 0, 0, 0);
//if not USER <FWusername> then
// if not PASS <FWPassword> then ERROR!
//if SITE <FTPServer> then ERROR!
//if not USER <username> then
// if not PASS <password> then
// if not ACCT <account> then ERROR!
//OK!
Action1: TLogonActions =
(3, 6, 3,
4, 6, FTP_ERR,
5, FTP_ERR, 9,
0, FTP_OK, 12,
1, FTP_OK, 15,
2, FTP_OK, FTP_ERR);
//if not USER <FWusername> then
// if not PASS <FWPassword> then ERROR!
//if USER <UserName>'@'<FTPServer> then OK!
//if not PASS <password> then
// if not ACCT <account> then ERROR!
//OK!
Action2: TLogonActions =
(3, 6, 3,
4, 6, FTP_ERR,
6, FTP_OK, 9,
1, FTP_OK, 12,
2, FTP_OK, FTP_ERR,
0, 0, 0);
//if not USER <FWusername> then
// if not PASS <FWPassword> then ERROR!
//if not USER <username> then
// if not PASS <password> then
// if not ACCT <account> then ERROR!
//OK!
Action3: TLogonActions =
(3, 6, 3,
4, 6, FTP_ERR,
0, FTP_OK, 9,
1, FTP_OK, 12,
2, FTP_OK, FTP_ERR,
0, 0, 0);
//OPEN <FTPserver>
//if not USER <username> then
// if not PASS <password> then
// if not ACCT <account> then ERROR!
//OK!
Action4: TLogonActions =
(7, 3, 3,
0, FTP_OK, 6,
1, FTP_OK, 9,
2, FTP_OK, FTP_ERR,
0, 0, 0, 0, 0, 0);
//if USER <UserName>'@'<FTPServer> then OK!
//if not PASS <password> then
// if not ACCT <account> then ERROR!
//OK!
Action5: TLogonActions =
(6, FTP_OK, 3,
1, FTP_OK, 6,
2, FTP_OK, FTP_ERR,
0, 0, 0, 0, 0, 0, 0, 0, 0);
//if not USER <FWUserName>@<FTPServer> then
// if not PASS <FWPassword> then ERROR!
//if not USER <username> then
// if not PASS <password> then
// if not ACCT <account> then ERROR!
//OK!
Action6: TLogonActions =
(8, 6, 3,
4, 6, FTP_ERR,
0, FTP_OK, 9,
1, FTP_OK, 12,
2, FTP_OK, FTP_ERR,
0, 0, 0);
//if USER <UserName>@<FTPServer> <FWUserName> then ERROR!
//if not PASS <password> then
// if not ACCT <account> then ERROR!
//OK!
Action7: TLogonActions =
(9, FTP_ERR, 3,
1, FTP_OK, 6,
2, FTP_OK, FTP_ERR,
0, 0, 0, 0, 0, 0, 0, 0, 0);
//if not USER <UserName>@<FWUserName>@<FTPServer> then
// if not PASS <Password>@<FWPassword> then
// if not ACCT <account> then ERROR!
//OK!
Action8: TLogonActions =
(10, FTP_OK, 3,
11, FTP_OK, 6,
2, FTP_OK, FTP_ERR,
0, 0, 0, 0, 0, 0, 0, 0, 0);
var
FTPServer: string;
LogonActions: TLogonActions;
i: integer;
s: string;
x: integer;
begin
Result := False;
if FFWHost = '' then
Mode := 0;
if (FTargetPort = cFtpProtocol) or (FTargetPort = '21') then
FTPServer := FTargetHost
else
FTPServer := FTargetHost + ':' + FTargetPort;
case Mode of
-1:
LogonActions := CustomLogon;
1:
LogonActions := Action1;
2:
LogonActions := Action2;
3:
LogonActions := Action3;
4:
LogonActions := Action4;
5:
LogonActions := Action5;
6:
LogonActions := Action6;
7:
LogonActions := Action7;
8:
LogonActions := Action8;
else
LogonActions := Action0;
end;
i := 0;
repeat
case LogonActions[i] of
0: s := 'USER ' + FUserName;
1: s := 'PASS ' + FPassword;
2: s := 'ACCT ' + FAccount;
3: s := 'USER ' + FFWUserName;
4: s := 'PASS ' + FFWPassword;
5: s := 'SITE ' + FTPServer;
6: s := 'USER ' + FUserName + '@' + FTPServer;
7: s := 'OPEN ' + FTPServer;
8: s := 'USER ' + FFWUserName + '@' + FTPServer;
9: s := 'USER ' + FUserName + '@' + FTPServer + ' ' + FFWUserName;
10: s := 'USER ' + FUserName + '@' + FFWUserName + '@' + FTPServer;
11: s := 'PASS ' + FPassword + '@' + FFWPassword;
end;
x := FTPCommand(s);
x := x div 100;
if (x <> 2) and (x <> 3) then
Exit;
i := LogonActions[i + x - 1];
case i of
FTP_ERR:
Exit;
FTP_OK:
begin
Result := True;
Exit;
end;
end;
until False;
end;
function TFTPSend.Connect: Boolean;
begin
FSock.CloseSocket;
FSock.Bind(FIPInterface, cAnyPort);
if FSock.LastError = 0 then
if FFWHost = '' then
FSock.Connect(FTargetHost, FTargetPort)
else
FSock.Connect(FFWHost, FFWPort);
if FSock.LastError = 0 then
if FFullSSL then
FSock.SSLDoConnect;
Result := FSock.LastError = 0;
end;
function TFTPSend.Login: Boolean;
var
x: integer;
begin
Result := False;
FCanResume := False;
if not Connect then
Exit;
FIsTLS := FFullSSL;
FIsDataTLS := False;
repeat
x := ReadResult div 100;
until x <> 1;
if x <> 2 then
Exit;
if FAutoTLS and not(FIsTLS) then
if (FTPCommand('AUTH TLS') div 100) = 2 then
begin
FSock.SSLDoConnect;
FIsTLS := FSock.LastError = 0;
if not FIsTLS then
begin
Result := False;
Exit;
end;
end;
if not Auth(FFWMode) then
Exit;
if FIsTLS then
begin
FTPCommand('PBSZ 0');
if FTLSonData then
FIsDataTLS := (FTPCommand('PROT P') div 100) = 2;
if not FIsDataTLS then
FTPCommand('PROT C');
end;
FTPCommand('TYPE I');
FTPCommand('STRU F');
FTPCommand('MODE S');
if FTPCommand('REST 0') = 350 then
if FTPCommand('REST 1') = 350 then
begin
FTPCommand('REST 0');
FCanResume := True;
end;
Result := True;
end;
function TFTPSend.Logout: Boolean;
begin
Result := (FTPCommand('QUIT') div 100) = 2;
FSock.CloseSocket;
end;
procedure TFTPSend.ParseRemote(Value: string);
var
n: integer;
nb, ne: integer;
s: string;
x: integer;
begin
Value := trim(Value);
nb := Pos('(',Value);
ne := Pos(')',Value);
if (nb = 0) or (ne = 0) then
begin
nb:=RPos(' ',Value);
s:=Copy(Value, nb + 1, Length(Value) - nb);
end
else
begin
s:=Copy(Value,nb+1,ne-nb-1);
end;
for n := 1 to 4 do
if n = 1 then
FDataIP := Fetch(s, ',')
else
FDataIP := FDataIP + '.' + Fetch(s, ',');
x := StrToIntDef(Fetch(s, ','), 0) * 256;
x := x + StrToIntDef(Fetch(s, ','), 0);
FDataPort := IntToStr(x);
end;
procedure TFTPSend.ParseRemoteEPSV(Value: string);
var
n: integer;
s, v: AnsiString;
begin
s := SeparateRight(Value, '(');
s := Trim(SeparateLeft(s, ')'));
Delete(s, Length(s), 1);
v := '';
for n := Length(s) downto 1 do
if s[n] in ['0'..'9'] then
v := s[n] + v
else
Break;
FDataPort := v;
FDataIP := FTargetHost;
end;
function TFTPSend.DataSocket: boolean;
var
s: string;
begin
Result := False;
if FIsDataTLS then
FPassiveMode := True;
if FPassiveMode then
begin
if FSock.IP6used then
s := '2'
else
s := '1';
if FSock.IP6used and not(FForceOldPort) and ((FTPCommand('EPSV ' + s) div 100) = 2) then
begin
ParseRemoteEPSV(FResultString);
end
else
if FSock.IP6used then
Exit
else
begin
if (FTPCommand('PASV') div 100) <> 2 then
Exit;
ParseRemote(FResultString);
end;
FDSock.CloseSocket;
FDSock.Bind(FIPInterface, cAnyPort);
FDSock.Connect(FDataIP, FDataPort);
Result := FDSock.LastError = 0;
end
else
begin
FDSock.CloseSocket;
if FForceDefaultPort then
s := cFtpDataProtocol
else
s := '0';
//data conection from same interface as command connection
FDSock.Bind(FSock.GetLocalSinIP, s);
if FDSock.LastError <> 0 then
Exit;
FDSock.SetLinger(True, 10000);
FDSock.Listen;
FDSock.GetSins;
FDataIP := FDSock.GetLocalSinIP;
FDataIP := FDSock.ResolveName(FDataIP);
FDataPort := IntToStr(FDSock.GetLocalSinPort);
if FSock.IP6used and (not FForceOldPort) then
begin
if IsIp6(FDataIP) then
s := '2'
else
s := '1';
s := 'EPRT |' + s +'|' + FDataIP + '|' + FDataPort + '|';
Result := (FTPCommand(s) div 100) = 2;
end;
if not Result and IsIP(FDataIP) then
begin
s := ReplaceString(FDataIP, '.', ',');
s := 'PORT ' + s + ',' + IntToStr(FDSock.GetLocalSinPort div 256)
+ ',' + IntToStr(FDSock.GetLocalSinPort mod 256);
Result := (FTPCommand(s) div 100) = 2;
end;
end;
end;
function TFTPSend.AcceptDataSocket: Boolean;
var
x: TSocket;
begin
if FPassiveMode then
Result := True
else
begin
Result := False;
if FDSock.CanRead(FTimeout) then
begin
x := FDSock.Accept;
if not FDSock.UsingSocks then
FDSock.CloseSocket;
FDSock.Socket := x;
Result := True;
end;
end;
if Result and FIsDataTLS then
begin
FDSock.SSL.Assign(FSock.SSL);
FDSock.SSLDoConnect;
Result := FDSock.LastError = 0;
end;
end;
function TFTPSend.DataRead(const DestStream: TStream): Boolean;
var
x: integer;
begin
Result := False;
try
if not AcceptDataSocket then
Exit;
FDSock.RecvStreamRaw(DestStream, FTimeout);
FDSock.CloseSocket;
x := ReadResult;
Result := (x div 100) = 2;
finally
FDSock.CloseSocket;
end;
end;
function TFTPSend.DataWrite(const SourceStream: TStream): Boolean;
var
x: integer;
b: Boolean;
begin
Result := False;
try
if not AcceptDataSocket then
Exit;
FDSock.SendStreamRaw(SourceStream);
b := FDSock.LastError = 0;
FDSock.CloseSocket;
x := ReadResult;
Result := b and ((x div 100) = 2);
finally
FDSock.CloseSocket;
end;
end;
function TFTPSend.List(Directory: string; NameList: Boolean): Boolean;
var
x: integer;
begin
Result := False;
FDataStream.Clear;
FFTPList.Clear;
if Directory <> '' then
Directory := ' ' + Directory;
FTPCommand('TYPE A');
if not DataSocket then
Exit;
if NameList then
x := FTPCommand('NLST' + Directory)
else
x := FTPCommand('LIST' + Directory);
if (x div 100) <> 1 then
Exit;
Result := DataRead(FDataStream);
if (not NameList) and Result then
begin
FDataStream.Position := 0;
FFTPList.Lines.LoadFromStream(FDataStream);
FFTPList.ParseLines;
end;
FDataStream.Position := 0;
end;
function TFTPSend.RetrieveFile(const FileName: string; Restore: Boolean): Boolean;
var
RetrStream: TStream;
begin
Result := False;
if FileName = '' then
Exit;
if not DataSocket then
Exit;
Restore := Restore and FCanResume;
if FDirectFile then
if Restore and FileExists(FDirectFileName) then
RetrStream := TFileStream.Create(FDirectFileName,
fmOpenReadWrite or fmShareExclusive)
else
RetrStream := TFileStream.Create(FDirectFileName,
fmCreate or fmShareDenyWrite)
else
RetrStream := FDataStream;
try
if FBinaryMode then
FTPCommand('TYPE I')
else
FTPCommand('TYPE A');
if Restore then
begin
RetrStream.Position := RetrStream.Size;
if (FTPCommand('REST ' + IntToStr(RetrStream.Size)) div 100) <> 3 then
Exit;
end
else
if RetrStream is TMemoryStream then
TMemoryStream(RetrStream).Clear;
if (FTPCommand('RETR ' + FileName) div 100) <> 1 then
Exit;
Result := DataRead(RetrStream);
if not FDirectFile then
RetrStream.Position := 0;
finally
if FDirectFile then
RetrStream.Free;
end;
end;
function TFTPSend.InternalStor(const Command: string; RestoreAt: int64): Boolean;
var
SendStream: TStream;
StorSize: int64;
begin
Result := False;
if FDirectFile then
if not FileExists(FDirectFileName) then
Exit
else
SendStream := TFileStream.Create(FDirectFileName,
fmOpenRead or fmShareDenyWrite)
else
SendStream := FDataStream;
try
if not DataSocket then
Exit;
if FBinaryMode then
FTPCommand('TYPE I')
else
FTPCommand('TYPE A');
StorSize := SendStream.Size;
if not FCanResume then
RestoreAt := 0;
if (StorSize > 0) and (RestoreAt = StorSize) then
begin
Result := True;
Exit;
end;
if RestoreAt > StorSize then
RestoreAt := 0;
FTPCommand('ALLO ' + IntToStr(StorSize - RestoreAt));
if FCanResume then
if (FTPCommand('REST ' + IntToStr(RestoreAt)) div 100) <> 3 then
Exit;
SendStream.Position := RestoreAt;
if (FTPCommand(Command) div 100) <> 1 then
Exit;
Result := DataWrite(SendStream);
finally
if FDirectFile then
SendStream.Free;
end;
end;
function TFTPSend.StoreFile(const FileName: string; Restore: Boolean): Boolean;
var
RestoreAt: int64;
begin
Result := False;
if FileName = '' then
Exit;
RestoreAt := 0;
Restore := Restore and FCanResume;
if Restore then
begin
RestoreAt := Self.FileSize(FileName);
if RestoreAt < 0 then
RestoreAt := 0;
end;
Result := InternalStor('STOR ' + FileName, RestoreAt);
end;
function TFTPSend.StoreUniqueFile: Boolean;
begin
Result := InternalStor('STOU', 0);
end;
function TFTPSend.AppendFile(const FileName: string): Boolean;
begin
Result := False;
if FileName = '' then
Exit;
Result := InternalStor('APPE ' + FileName, 0);
end;
function TFTPSend.NoOp: Boolean;
begin
Result := (FTPCommand('NOOP') div 100) = 2;
end;
function TFTPSend.RenameFile(const OldName, NewName: string): Boolean;
begin
Result := False;
if (FTPCommand('RNFR ' + OldName) div 100) <> 3 then
Exit;
Result := (FTPCommand('RNTO ' + NewName) div 100) = 2;
end;
function TFTPSend.DeleteFile(const FileName: string): Boolean;
begin
Result := (FTPCommand('DELE ' + FileName) div 100) = 2;
end;
function TFTPSend.FileSize(const FileName: string): int64;
var
s: string;
begin
Result := -1;
if (FTPCommand('SIZE ' + FileName) div 100) = 2 then
begin
s := Trim(SeparateRight(ResultString, ' '));
s := Trim(SeparateLeft(s, ' '));
{$IFDEF VER100}
Result := StrToIntDef(s, -1);
{$ELSE}
Result := StrToInt64Def(s, -1);
{$ENDIF}
end;
end;
function TFTPSend.ChangeWorkingDir(const Directory: string): Boolean;
begin
Result := (FTPCommand('CWD ' + Directory) div 100) = 2;
end;
function TFTPSend.ChangeToParentDir: Boolean;
begin
Result := (FTPCommand('CDUP') div 100) = 2;
end;
function TFTPSend.ChangeToRootDir: Boolean;
begin
Result := ChangeWorkingDir('/');
end;
function TFTPSend.DeleteDir(const Directory: string): Boolean;
begin
Result := (FTPCommand('RMD ' + Directory) div 100) = 2;
end;
function TFTPSend.CreateDir(const Directory: string): Boolean;
begin
Result := (FTPCommand('MKD ' + Directory) div 100) = 2;
end;
function TFTPSend.GetCurrentDir: String;
begin
Result := '';
if (FTPCommand('PWD') div 100) = 2 then
begin
Result := SeparateRight(FResultString, '"');
Result := Trim(Separateleft(Result, '"'));
end;
end;
procedure TFTPSend.Abort;
begin
FSock.SendString('ABOR' + CRLF);
FDSock.StopFlag := True;
end;
procedure TFTPSend.TelnetAbort;
begin
FSock.SendString(#$FF + #$F4 + #$FF + #$F2);
Abort;
end;
{==============================================================================}
procedure TFTPListRec.Assign(Value: TFTPListRec);
begin
FFileName := Value.FileName;
FDirectory := Value.Directory;
FReadable := Value.Readable;
FFileSize := Value.FileSize;
FFileTime := Value.FileTime;
FOriginalLine := Value.OriginalLine;
FMask := Value.Mask;
end;
constructor TFTPList.Create;
begin
inherited Create;
FList := TList.Create;
FLines := TStringList.Create;
FMasks := TStringList.Create;
FUnparsedLines := TStringList.Create;
//various UNIX
FMasks.add('pppppppppp $!!!S*$TTT$DD$hh mm ss$YYYY$n*');
FMasks.add('pppppppppp $!!!S*$DD$TTT$hh mm ss$YYYY$n*');
FMasks.add('pppppppppp $!!!S*$TTT$DD$UUUUU$n*'); //mostly used UNIX format
FMasks.add('pppppppppp $!!!S*$DD$TTT$UUUUU$n*');
//MacOS
FMasks.add('pppppppppp $!!S*$TTT$DD$UUUUU$n*');
FMasks.add('pppppppppp $!S*$TTT$DD$UUUUU$n*');
//Novell
FMasks.add('d $!S*$TTT$DD$UUUUU$n*');
//Windows
FMasks.add('MM DD YY hh mmH !S* n*');
FMasks.add('MM DD YY hh mmH $ d!n*');
FMasks.add('MM DD YYYY hh mmH !S* n*');
FMasks.add('MM DD YYYY hh mmH $ d!n*');
FMasks.add('DD MM YYYY hh mmH !S* n*');
FMasks.add('DD MM YYYY hh mmH $ d!n*');
//VMS
FMasks.add('v*$ DD TTT YYYY hh mm');
FMasks.add('v*$!DD TTT YYYY hh mm');
FMasks.add('n*$ YYYY MM DD hh mm$S*');
//AS400
FMasks.add('!S*$MM DD YY hh mm ss !n*');
FMasks.add('!S*$DD MM YY hh mm ss !n*');
FMasks.add('n*!S*$MM DD YY hh mm ss d');
FMasks.add('n*!S*$DD MM YY hh mm ss d');
//VxWorks
FMasks.add('$S* TTT DD YYYY hh mm ss $n* $ d');
FMasks.add('$S* TTT DD YYYY hh mm ss $n*');
//Distinct
FMasks.add('d $S*$TTT DD YYYY hh mm$n*');
FMasks.add('d $S*$TTT DD$hh mm$n*');
//PC-NFSD
FMasks.add('nnnnnnnn.nnn dSSSSSSSSSSS MM DD YY hh mmH');
//VOS
FMasks.add('- SSSSS YY MM DD hh mm ss n*');
FMasks.add('- d= SSSSS YY MM DD hh mm ss n*');
//Unissys ClearPath
FMasks.add('nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn SSSSSSSSS MM DD YYYY hh mm');
FMasks.add('n*\x SSSSSSSSS MM DD YYYY hh mm');
//IBM
FMasks.add('- SSSSSSSSSSSS d MM DD YYYY hh mm n*');
//OS9
FMasks.add('- YY MM DD hhmm d SSSSSSSSS n*');
//tandem
FMasks.add('nnnnnnnn SSSSSSS DD TTT YY hh mm ss');
//MVS
FMasks.add('- YYYY MM DD SSSSS d=O n*');
//BullGCOS8
FMasks.add(' $S* MM DD YY hh mm ss !n*');
FMasks.add('d $S* MM DD YY !n*');
//BullGCOS7
FMasks.add(' TTT DD YYYY n*');
FMasks.add(' d n*');
end;
destructor TFTPList.Destroy;
begin
Clear;
FList.Free;
FLines.Free;
FMasks.Free;
FUnparsedLines.Free;
inherited Destroy;
end;
procedure TFTPList.Clear;
var
n:integer;
begin
for n := 0 to FList.Count - 1 do
if Assigned(FList[n]) then
TFTPListRec(FList[n]).Free;
FList.Clear;
FLines.Clear;
FUnparsedLines.Clear;
end;
function TFTPList.Count: integer;
begin
Result := FList.Count;
end;
function TFTPList.GetListItem(Index: integer): TFTPListRec;
begin
Result := nil;
if Index < Count then
Result := TFTPListRec(FList[Index]);
end;
procedure TFTPList.Assign(Value: TFTPList);
var
flr: TFTPListRec;
n: integer;
begin
Clear;
for n := 0 to Value.Count - 1 do
begin
flr := TFTPListRec.Create;
flr.Assign(Value[n]);
Flist.Add(flr);
end;
Lines.Assign(Value.Lines);
Masks.Assign(Value.Masks);
UnparsedLines.Assign(Value.UnparsedLines);
end;
procedure TFTPList.ClearStore;
begin
Monthnames := '';
BlockSize := '';
DirFlagValue := '';
FileName := '';
VMSFileName := '';
Day := '';
Month := '';
ThreeMonth := '';
YearTime := '';
Year := '';
Hours := '';
HoursModif := '';
Minutes := '';
Seconds := '';
Size := '';
Permissions := '';
DirFlag := '';
end;
function TFTPList.ParseByMask(Value, NextValue, Mask: AnsiString): Integer;
var
Ivalue, IMask: integer;
MaskC, LastMaskC: AnsiChar;
c: AnsiChar;
s: string;
begin
ClearStore;
Result := 0;
if Value = '' then
Exit;
if Mask = '' then
Exit;
Ivalue := 1;
IMask := 1;
Result := 1;
LastMaskC := ' ';
while Imask <= Length(mask) do
begin
if (Mask[Imask] <> '*') and (Ivalue > Length(Value)) then
begin
Result := 0;
Exit;
end;
MaskC := Mask[Imask];
if Ivalue > Length(Value) then
Exit;
c := Value[Ivalue];
case MaskC of
'n':
FileName := FileName + c;
'v':
VMSFileName := VMSFileName + c;
'.':
begin
if c in ['.', ' '] then
FileName := TrimSP(FileName) + '.'
else
begin
Result := 0;
Exit;
end;
end;
'D':
Day := Day + c;
'M':
Month := Month + c;
'T':
ThreeMonth := ThreeMonth + c;
'U':
YearTime := YearTime + c;
'Y':
Year := Year + c;
'h':
Hours := Hours + c;
'H':
HoursModif := HoursModif + c;
'm':
Minutes := Minutes + c;
's':
Seconds := Seconds + c;
'S':
Size := Size + c;
'p':
Permissions := Permissions + c;
'd':
DirFlag := DirFlag + c;
'x':
if c <> ' ' then
begin
Result := 0;
Exit;
end;
'*':
begin
s := '';
if LastMaskC in ['n', 'v'] then
begin
if Imask = Length(Mask) then
s := Copy(Value, IValue, Maxint)
else
while IValue <= Length(Value) do
begin
if Value[Ivalue] = ' ' then
break;
s := s + Value[Ivalue];
Inc(Ivalue);
end;
if LastMaskC = 'n' then
FileName := FileName + s
else
VMSFileName := VMSFileName + s;
end
else
begin
while IValue <= Length(Value) do
begin
if not(Value[Ivalue] in ['0'..'9']) then
break;
s := s + Value[Ivalue];
Inc(Ivalue);
end;
case LastMaskC of
'S':
Size := Size + s;
end;
end;
Dec(IValue);
end;
'!':
begin
while IValue <= Length(Value) do
begin
if Value[Ivalue] = ' ' then
break;
Inc(Ivalue);
end;
while IValue <= Length(Value) do
begin
if Value[Ivalue] <> ' ' then
break;
Inc(Ivalue);
end;
Dec(IValue);
end;
'$':
begin
while IValue <= Length(Value) do
begin
if not(Value[Ivalue] in [' ', #9]) then
break;
Inc(Ivalue);
end;
Dec(IValue);
end;
'=':
begin
s := '';
case LastmaskC of
'S':
begin
while Imask <= Length(Mask) do
begin
if not(Mask[Imask] in ['0'..'9']) then
break;
s := s + Mask[Imask];
Inc(Imask);
end;
Dec(Imask);
BlockSize := s;
end;
'T':
begin
Monthnames := Copy(Mask, IMask, 12 * 3);
Inc(IMask, 12 * 3);
end;
'd':
begin
Inc(Imask);
DirFlagValue := Mask[Imask];
end;
end;
end;
'\':
begin
Value := NextValue;
IValue := 0;
Result := 2;
end;
end;
Inc(Ivalue);
Inc(Imask);
LastMaskC := MaskC;
end;
end;
function TFTPList.CheckValues: Boolean;
var
x, n: integer;
begin
Result := false;
if FileName <> '' then
begin
if pos('?', VMSFilename) > 0 then
Exit;
if pos('*', VMSFilename) > 0 then
Exit;
end;
if VMSFileName <> '' then
if pos(';', VMSFilename) <= 0 then
Exit;
if (FileName = '') and (VMSFileName = '') then
Exit;
if Permissions <> '' then
begin
if length(Permissions) <> 10 then
Exit;
for n := 1 to 10 do
if not(Permissions[n] in
['a', 'b', 'c', 'd', 'h', 'l', 'p', 'r', 's', 't', 'w', 'x', 'y', '-']) then
Exit;
end;
if Day <> '' then
begin
Day := TrimSP(Day);
x := StrToIntDef(day, -1);
if (x < 1) or (x > 31) then
Exit;
end;
if Month <> '' then
begin
Month := TrimSP(Month);
x := StrToIntDef(Month, -1);
if (x < 1) or (x > 12) then
Exit;
end;
if Hours <> '' then
begin
Hours := TrimSP(Hours);
x := StrToIntDef(Hours, -1);
if (x < 0) or (x > 24) then
Exit;
end;
if HoursModif <> '' then
begin
if not (HoursModif[1] in ['a', 'A', 'p', 'P']) then
Exit;
end;
if Minutes <> '' then
begin
Minutes := TrimSP(Minutes);
x := StrToIntDef(Minutes, -1);
if (x < 0) or (x > 59) then
Exit;
end;
if Seconds <> '' then
begin
Seconds := TrimSP(Seconds);
x := StrToIntDef(Seconds, -1);
if (x < 0) or (x > 59) then
Exit;
end;
if Size <> '' then
begin
Size := TrimSP(Size);
for n := 1 to Length(Size) do
if not (Size[n] in ['0'..'9']) then
Exit;
end;
if length(Monthnames) = (12 * 3) then
for n := 1 to 12 do
CustomMonthNames[n] := Copy(Monthnames, ((n - 1) * 3) + 1, 3);
if ThreeMonth <> '' then
begin
x := GetMonthNumber(ThreeMonth);
if (x = 0) then
Exit;
end;
if YearTime <> '' then
begin
YearTime := ReplaceString(YearTime, '-', ':');
if pos(':', YearTime) > 0 then
begin
if (GetTimeFromstr(YearTime) = -1) then
Exit;
end
else
begin
YearTime := TrimSP(YearTime);
x := StrToIntDef(YearTime, -1);
if (x = -1) then
Exit;
if (x < 1900) or (x > 2100) then
Exit;
end;
end;
if Year <> '' then
begin
Year := TrimSP(Year);
x := StrToIntDef(Year, -1);
if (x = -1) then
Exit;
if Length(Year) = 4 then
begin
if not((x > 1900) and (x < 2100)) then
Exit;
end
else
if Length(Year) = 2 then
begin
if not((x >= 0) and (x <= 99)) then
Exit;
end
else
if Length(Year) = 3 then
begin
if not((x >= 100) and (x <= 110)) then
Exit;
end
else
Exit;
end;
Result := True;
end;
procedure TFTPList.FillRecord(const Value: TFTPListRec);
var
s: string;
x: integer;
myear: Word;
mmonth: Word;
mday: Word;
mhours, mminutes, mseconds: word;
n: integer;
begin
s := DirFlagValue;
if s = '' then
s := 'D';
s := Uppercase(s);
Value.Directory := s = Uppercase(DirFlag);
if FileName <> '' then
Value.FileName := SeparateLeft(Filename, ' -> ');
if VMSFileName <> '' then
begin
Value.FileName := VMSFilename;
Value.Directory := Pos('.DIR;',VMSFilename) > 0;
end;
Value.FileName := TrimSPRight(Value.FileName);
Value.Readable := not Value.Directory;
if BlockSize <> '' then
x := StrToIntDef(BlockSize, 1)
else
x := 1;
{$IFDEF VER100}
Value.FileSize := x * StrToIntDef(Size, 0);
{$ELSE}
Value.FileSize := x * StrToInt64Def(Size, 0);
{$ENDIF}
DecodeDate(Date,myear,mmonth,mday);
mhours := 0;
mminutes := 0;
mseconds := 0;
if Day <> '' then
mday := StrToIntDef(day, 1);
if Month <> '' then
mmonth := StrToIntDef(Month, 1);
if length(Monthnames) = (12 * 3) then
for n := 1 to 12 do
CustomMonthNames[n] := Copy(Monthnames, ((n - 1) * 3) + 1, 3);
if ThreeMonth <> '' then
mmonth := GetMonthNumber(ThreeMonth);
if Year <> '' then
begin
myear := StrToIntDef(Year, 0);
if (myear <= 99) and (myear > 50) then
myear := myear + 1900;
if myear <= 50 then
myear := myear + 2000;
end;
if YearTime <> '' then
begin
if pos(':', YearTime) > 0 then
begin
YearTime := TrimSP(YearTime);
mhours := StrToIntDef(Separateleft(YearTime, ':'), 0);
mminutes := StrToIntDef(SeparateRight(YearTime, ':'), 0);
if (Encodedate(myear, mmonth, mday)
+ EncodeTime(mHours, mminutes, 0, 0)) > now then
Dec(mYear);
end
else
myear := StrToIntDef(YearTime, 0);
end;
if Minutes <> '' then
mminutes := StrToIntDef(Minutes, 0);
if Seconds <> '' then
mseconds := StrToIntDef(Seconds, 0);
if Hours <> '' then
begin
mHours := StrToIntDef(Hours, 0);
if HoursModif <> '' then
if Uppercase(HoursModif[1]) = 'P' then
if mHours <> 12 then
mHours := MHours + 12;
end;
Value.FileTime := Encodedate(myear, mmonth, mday)
+ EncodeTime(mHours, mminutes, mseconds, 0);
if Permissions <> '' then
begin
Value.Permission := Permissions;
Value.Readable := Uppercase(permissions)[2] = 'R';
if Uppercase(permissions)[1] = 'D' then
begin
Value.Directory := True;
Value.Readable := false;
end
else
if Uppercase(permissions)[1] = 'L' then
Value.Directory := True;
end;
end;
function TFTPList.ParseEPLF(Value: string): Boolean;
var
s, os: string;
flr: TFTPListRec;
begin
Result := False;
if Value <> '' then
if Value[1] = '+' then
begin
os := Value;
Delete(Value, 1, 1);
flr := TFTPListRec.create;
flr.FileName := SeparateRight(Value, #9);
s := Fetch(Value, ',');
while s <> '' do
begin
if s[1] = #9 then
Break;
case s[1] of
'/':
flr.Directory := true;
'r':
flr.Readable := true;
's':
{$IFDEF VER100}
flr.FileSize := StrToIntDef(Copy(s, 2, Length(s) - 1), 0);
{$ELSE}
flr.FileSize := StrToInt64Def(Copy(s, 2, Length(s) - 1), 0);
{$ENDIF}
'm':
flr.FileTime := (StrToIntDef(Copy(s, 2, Length(s) - 1), 0) / 86400)
+ 25569;
end;
s := Fetch(Value, ',');
end;
if flr.FileName <> '' then
if (flr.Directory and ((flr.FileName = '.') or (flr.FileName = '..')))
or (flr.FileName = '') then
flr.free
else
begin
flr.OriginalLine := os;
flr.Mask := 'EPLF';
Flist.Add(flr);
Result := True;
end;
end;
end;
procedure TFTPList.ParseLines;
var
flr: TFTPListRec;
n, m: Integer;
S: string;
x: integer;
b: Boolean;
begin
n := 0;
while n < Lines.Count do
begin
if n = Lines.Count - 1 then
s := ''
else
s := Lines[n + 1];
b := False;
x := 0;
if ParseEPLF(Lines[n]) then
begin
b := True;
x := 1;
end
else
for m := 0 to Masks.Count - 1 do
begin
x := ParseByMask(Lines[n], s, Masks[m]);
if x > 0 then
if CheckValues then
begin
flr := TFTPListRec.create;
FillRecord(flr);
flr.OriginalLine := Lines[n];
flr.Mask := Masks[m];
if flr.Directory and ((flr.FileName = '.') or (flr.FileName = '..')) then
flr.free
else
Flist.Add(flr);
b := True;
Break;
end;
end;
if not b then
FUnparsedLines.Add(Lines[n]);
Inc(n);
if x > 1 then
Inc(n, x - 1);
end;
end;
{==============================================================================}
function FtpGetFile(const IP, Port, FileName, LocalFile,
User, Pass: string): Boolean;
begin
Result := False;
with TFTPSend.Create do
try
if User <> '' then
begin
Username := User;
Password := Pass;
end;
TargetHost := IP;
TargetPort := Port;
if not Login then
Exit;
DirectFileName := LocalFile;
DirectFile:=True;
Result := RetrieveFile(FileName, False);
Logout;
finally
Free;
end;
end;
function FtpPutFile(const IP, Port, FileName, LocalFile,
User, Pass: string): Boolean;
begin
Result := False;
with TFTPSend.Create do
try
if User <> '' then
begin
Username := User;
Password := Pass;
end;
TargetHost := IP;
TargetPort := Port;
if not Login then
Exit;
DirectFileName := LocalFile;
DirectFile:=True;
Result := StoreFile(FileName, False);
Logout;
finally
Free;
end;
end;
function FtpInterServerTransfer(
const FromIP, FromPort, FromFile, FromUser, FromPass: string;
const ToIP, ToPort, ToFile, ToUser, ToPass: string): Boolean;
var
FromFTP, ToFTP: TFTPSend;
s: string;
x: integer;
begin
Result := False;
FromFTP := TFTPSend.Create;
toFTP := TFTPSend.Create;
try
if FromUser <> '' then
begin
FromFTP.Username := FromUser;
FromFTP.Password := FromPass;
end;
if ToUser <> '' then
begin
ToFTP.Username := ToUser;
ToFTP.Password := ToPass;
end;
FromFTP.TargetHost := FromIP;
FromFTP.TargetPort := FromPort;
ToFTP.TargetHost := ToIP;
ToFTP.TargetPort := ToPort;
if not FromFTP.Login then
Exit;
if not ToFTP.Login then
Exit;
if (FromFTP.FTPCommand('PASV') div 100) <> 2 then
Exit;
FromFTP.ParseRemote(FromFTP.ResultString);
s := ReplaceString(FromFTP.DataIP, '.', ',');
s := 'PORT ' + s + ',' + IntToStr(StrToIntDef(FromFTP.DataPort, 0) div 256)
+ ',' + IntToStr(StrToIntDef(FromFTP.DataPort, 0) mod 256);
if (ToFTP.FTPCommand(s) div 100) <> 2 then
Exit;
x := ToFTP.FTPCommand('RETR ' + FromFile);
if (x div 100) <> 1 then
Exit;
x := FromFTP.FTPCommand('STOR ' + ToFile);
if (x div 100) <> 1 then
Exit;
FromFTP.Timeout := 21600000;
x := FromFTP.ReadResult;
if (x div 100) <> 2 then
Exit;
ToFTP.Timeout := 21600000;
x := ToFTP.ReadResult;
if (x div 100) <> 2 then
Exit;
Result := True;
finally
ToFTP.Free;
FromFTP.Free;
end;
end;
end.
|