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
|
/*
* Copyright (c) 2001 Lucas Fisher <ljfisher@purdue.edu>
* Copyright (c) 2009 Andreas Schneider <mail@cynapses.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License (LGPL) as published by the Free Software Foundation;
* either version 2 of the License, or (at your option) any later
* version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "kio_sftp.h"
#include <config-runtime.h>
#include "kio_smtp_debug.h"
#include <cerrno>
#include <cstring>
#include <utime.h>
#include <QtCore/QCoreApplication>
#include <QtCore/QDir>
#include <QtCore/QFile>
#include <QtCore/QVarLengthArray>
#include <QtCore/QMimeType>
#include <QtCore/QMimeDatabase>
#include <kuser.h>
#include <kmessagebox.h>
#include <klocalizedstring.h>
#include <kconfiggroup.h>
#include <kio/ioslave_defaults.h>
#define KIO_SFTP_SPECIAL_TIMEOUT 30
#define ZERO_STRUCTP(x) do { if ((x) != NULL) memset((char *)(x), 0, sizeof(*(x))); } while(0)
// How big should each data packet be? Definitely not bigger than 64kb or
// you will overflow the 2 byte size variable in a sftp packet.
#define MAX_XFER_BUF_SIZE (60 * 1024)
#define KIO_SFTP_DB 7120
// Maximum amount of data which can be sent from the KIOSlave in one chunk
// see TransferJob::slotDataReq (max_size variable) for the value
#define MAX_TRANSFER_SIZE (14 * 1024 * 1024)
using namespace KIO;
extern "C"
{
int Q_DECL_EXPORT kdemain( int argc, char **argv )
{
QCoreApplication app(argc, argv);
app.setApplicationName("kio_sftp");
qCDebug(KIO_SFTP_LOG) << "*** Starting kio_sftp ";
if (argc != 4) {
qCDebug(KIO_SFTP_LOG) << "Usage: kio_sftp protocol domain-socket1 domain-socket2";
exit(-1);
}
sftpProtocol slave(argv[2], argv[3]);
slave.dispatchLoop();
qCDebug(KIO_SFTP_LOG) << "*** kio_sftp Done";
return 0;
}
}
// Converts SSH error into KIO error
static int toKIOError (const int err)
{
switch (err) {
case SSH_FX_OK:
break;
case SSH_FX_NO_SUCH_FILE:
case SSH_FX_NO_SUCH_PATH:
return KIO::ERR_DOES_NOT_EXIST;
case SSH_FX_PERMISSION_DENIED:
return KIO::ERR_ACCESS_DENIED;
case SSH_FX_FILE_ALREADY_EXISTS:
return KIO::ERR_FILE_ALREADY_EXIST;
case SSH_FX_INVALID_HANDLE:
return KIO::ERR_MALFORMED_URL;
case SSH_FX_OP_UNSUPPORTED:
return KIO::ERR_UNSUPPORTED_ACTION;
case SSH_FX_BAD_MESSAGE:
return KIO::ERR_UNKNOWN;
default:
return KIO::ERR_INTERNAL;
}
return 0;
}
// Writes 'len' bytes from 'buf' to the file handle 'fd'.
static int writeToFile(int fd, const char *buf, size_t len)
{
while (len > 0) {
ssize_t written = write(fd, buf, len);
if (written >= 0) {
buf += written;
len -= written;
continue;
}
switch(errno) {
case EINTR:
case EAGAIN:
continue;
case EPIPE:
return ERR_CONNECTION_BROKEN;
case ENOSPC:
return ERR_DISK_FULL;
default:
return ERR_COULD_NOT_WRITE;
}
}
return 0;
}
static int seekPos(int fd, KIO::fileoffset_t pos, int mode)
{
KIO::fileoffset_t offset = -1;
while ((offset = QT_LSEEK(fd, pos, mode)) == EAGAIN);
return offset;
}
static bool wasUsernameChanged(const QString& username, const KIO::AuthInfo& info)
{
QString loginName (username);
// If username is empty, assume the current logged in username. Why ?
// Because libssh's SSH_OPTIONS_USER will default to that when it is not
// set and it won't be set unless the user explicitly typed a user user
// name as part of the request URL.
if (loginName.isEmpty()) {
KUser u;
loginName = u.loginName();
}
return (loginName != info.username);
}
// The callback function for libssh
static int auth_callback(const char *prompt, char *buf, size_t len,
int echo, int verify, void *userdata)
{
if (userdata == nullptr) {
return -1;
}
sftpProtocol *slave = (sftpProtocol *) userdata;
if (slave->auth_callback(prompt, buf, len, echo, verify, userdata) < 0) {
return -1;
}
return 0;
}
static void log_callback(int priority, const char *function, const char *buffer,
void *userdata)
{
if (userdata == nullptr) {
return;
}
sftpProtocol *slave = (sftpProtocol *) userdata;
slave->log_callback(priority, function, buffer, userdata);
}
int sftpProtocol::auth_callback(const char *prompt, char *buf, size_t len,
int echo, int verify, void *userdata) {
// unused variables
(void) echo;
(void) verify;
(void) userdata;
QString errMsg;
if (!mPublicKeyAuthInfo) {
mPublicKeyAuthInfo = new KIO::AuthInfo;
} else {
errMsg = i18n("Incorrect or invalid passphrase");
}
mPublicKeyAuthInfo->url.setScheme(QLatin1String("sftp"));
mPublicKeyAuthInfo->url.setHost(mHost);
if (mPort > 0 && mPort != DEFAULT_SFTP_PORT) {
mPublicKeyAuthInfo->url.setPort(mPort);
}
mPublicKeyAuthInfo->url.setUserName(mUsername);
QUrl u (mPublicKeyAuthInfo->url);
u.setPath(QString());
mPublicKeyAuthInfo->comment = u.url();
mPublicKeyAuthInfo->readOnly = true;
mPublicKeyAuthInfo->prompt = QString::fromUtf8(prompt);
mPublicKeyAuthInfo->keepPassword = false; // don't save passwords for public key,
// that's the task of ssh-agent.
mPublicKeyAuthInfo->setExtraField(QLatin1String("hide-username-line"), true);
mPublicKeyAuthInfo->setModified(false);
qCDebug(KIO_SFTP_LOG) << "Entering authentication callback, prompt=" << mPublicKeyAuthInfo->prompt;
if (openPasswordDialogV2(*mPublicKeyAuthInfo, errMsg) != 0) {
qCDebug(KIO_SFTP_LOG) << "User canceled public key passpharse dialog";
return -1;
}
strncpy(buf, mPublicKeyAuthInfo->password.toUtf8().constData(), len - 1);
mPublicKeyAuthInfo->password.fill('x');
mPublicKeyAuthInfo->password.clear();
return 0;
}
void sftpProtocol::log_callback(int priority, const char *function, const char *buffer,
void *userdata)
{
(void) userdata;
qCDebug(KIO_SFTP_LOG) << "[" << function << "] (" << priority << ") " << buffer;
}
void sftpProtocol::virtual_hook(int id, void *data)
{
switch(id) {
case SlaveBase::GetFileSystemFreeSpace: {
QUrl *url = static_cast<QUrl *>(data);
fileSystemFreeSpace(*url);
} break;
default:
SlaveBase::virtual_hook(id, data);
}
}
int sftpProtocol::authenticateKeyboardInteractive(AuthInfo &info) {
int err = ssh_userauth_kbdint(mSession, nullptr, nullptr);
while (err == SSH_AUTH_INFO) {
const QString name = QString::fromUtf8(ssh_userauth_kbdint_getname(mSession));
const QString instruction = QString::fromUtf8(ssh_userauth_kbdint_getinstruction(mSession));
const int n = ssh_userauth_kbdint_getnprompts(mSession);
qCDebug(KIO_SFTP_LOG) << "name=" << name << " instruction=" << instruction << " prompts=" << n;
for (int i = 0; i < n; ++i) {
char echo;
const char *answer = "";
const QString prompt = QString::fromUtf8(ssh_userauth_kbdint_getprompt(mSession, i, &echo));
qCDebug(KIO_SFTP_LOG) << "prompt=" << prompt << " echo=" << QString::number(echo);
if (echo) {
// See RFC4256 Section 3.3 User Interface
KIO::AuthInfo infoKbdInt;
infoKbdInt.url.setScheme("sftp");
infoKbdInt.url.setHost(mHost);
if (mPort > 0 && mPort != DEFAULT_SFTP_PORT) {
infoKbdInt.url.setPort(mPort);
}
if (!name.isEmpty()) {
infoKbdInt.caption = QString(i18n("SFTP Login") + " - " + name);
} else {
infoKbdInt.caption = i18n("SFTP Login");
}
infoKbdInt.comment = "sftp://" + mUsername + "@" + mHost;
QString newPrompt;
if (!instruction.isEmpty()) {
newPrompt = instruction + "<br /><br />";
}
newPrompt.append(prompt);
infoKbdInt.prompt = newPrompt;
infoKbdInt.readOnly = false;
infoKbdInt.keepPassword = false;
if (openPasswordDialogV2(infoKbdInt, i18n("Use the username input field to answer this question.")) == 0) {
qCDebug(KIO_SFTP_LOG) << "Got the answer from the password dialog";
answer = info.username.toUtf8().constData();
}
if (ssh_userauth_kbdint_setanswer(mSession, i, answer) < 0) {
qCDebug(KIO_SFTP_LOG) << "An error occurred setting the answer: "
<< ssh_get_error(mSession);
return SSH_AUTH_ERROR;
}
break;
} else {
if (prompt.startsWith(QLatin1String("password:"), Qt::CaseInsensitive)) {
info.prompt = i18n("Please enter your password.");
} else {
info.prompt = prompt;
}
info.comment = info.url.url();
info.commentLabel = i18n("Site:");
info.setExtraField(QLatin1String("hide-username-line"), true);
if (openPasswordDialogV2(info) == 0) {
qCDebug(KIO_SFTP_LOG) << "Got the answer from the password dialog";
answer = info.password.toUtf8().constData();
}
if (ssh_userauth_kbdint_setanswer(mSession, i, answer) < 0) {
qCDebug(KIO_SFTP_LOG) << "An error occurred setting the answer: "
<< ssh_get_error(mSession);
return SSH_AUTH_ERROR;
}
}
}
err = ssh_userauth_kbdint(mSession, nullptr, nullptr);
}
return err;
}
void sftpProtocol::reportError(const QUrl &url, const int err) {
qCDebug(KIO_SFTP_LOG) << "url = " << url << " - err=" << err;
const int kioError = toKIOError(err);
if (kioError)
error(kioError, url.toDisplayString());
}
bool sftpProtocol::createUDSEntry(const QString &filename, const QByteArray &path,
UDSEntry &entry, short int details) {
mode_t type;
mode_t access;
char *link;
bool isBrokenLink = false;
long long fileType = S_IFREG;
long long size = 0LL;
Q_ASSERT(entry.count() == 0);
sftp_attributes sb = sftp_lstat(mSftp, path.constData());
if (sb == nullptr) {
return false;
}
entry.insert(KIO::UDSEntry::UDS_NAME, filename);
if (sb->type == SSH_FILEXFER_TYPE_SYMLINK) {
link = sftp_readlink(mSftp, path.constData());
if (link == nullptr) {
sftp_attributes_free(sb);
return false;
}
entry.insert(KIO::UDSEntry::UDS_LINK_DEST, QFile::decodeName(link));
free(link);
// A symlink -> follow it only if details > 1
if (details > 1) {
sftp_attributes sb2 = sftp_stat(mSftp, path.constData());
if (sb2 == nullptr) {
isBrokenLink = true;
} else {
sftp_attributes_free(sb);
sb = sb2;
}
}
}
if (isBrokenLink) {
// It is a link pointing to nowhere
fileType = S_IFMT - 1;
access = S_IRWXU | S_IRWXG | S_IRWXO;
size = 0LL;
} else {
switch (sb->type) {
case SSH_FILEXFER_TYPE_REGULAR:
fileType = S_IFREG;
break;
case SSH_FILEXFER_TYPE_DIRECTORY:
fileType = S_IFDIR;
break;
case SSH_FILEXFER_TYPE_SYMLINK:
fileType = S_IFLNK;
break;
case SSH_FILEXFER_TYPE_SPECIAL:
case SSH_FILEXFER_TYPE_UNKNOWN:
fileType = S_IFMT - 1;
break;
}
access = sb->permissions & 07777;
size = sb->size;
}
entry.insert(KIO::UDSEntry::UDS_FILE_TYPE, fileType);
entry.insert(KIO::UDSEntry::UDS_ACCESS, access);
entry.insert(KIO::UDSEntry::UDS_SIZE, size);
if (details > 0) {
if (sb->owner) {
entry.insert(KIO::UDSEntry::UDS_USER, QString::fromUtf8(sb->owner));
} else {
entry.insert(KIO::UDSEntry::UDS_USER, QString::number(sb->uid));
}
if (sb->group) {
entry.insert(KIO::UDSEntry::UDS_GROUP, QString::fromUtf8(sb->group));
} else {
entry.insert(KIO::UDSEntry::UDS_GROUP, QString::number(sb->gid));
}
entry.insert(KIO::UDSEntry::UDS_ACCESS_TIME, sb->atime);
entry.insert(KIO::UDSEntry::UDS_MODIFICATION_TIME, sb->mtime);
entry.insert(KIO::UDSEntry::UDS_CREATION_TIME, sb->createtime);
}
sftp_attributes_free(sb);
return true;
}
QString sftpProtocol::canonicalizePath(const QString &path) {
qCDebug(KIO_SFTP_LOG) << "Path to canonicalize: " << path;
QString cPath;
char *sPath = nullptr;
if (path.isEmpty()) {
return cPath;
}
sPath = sftp_canonicalize_path(mSftp, path.toUtf8().constData());
if (sPath == nullptr) {
qCDebug(KIO_SFTP_LOG) << "Could not canonicalize path: " << path;
return cPath;
}
cPath = QFile::decodeName(sPath);
delete sPath;
qCDebug(KIO_SFTP_LOG) << "Canonicalized path: " << cPath;
return cPath;
}
sftpProtocol::sftpProtocol(const QByteArray &pool_socket, const QByteArray &app_socket)
: SlaveBase("kio_sftp", pool_socket, app_socket),
mConnected(false), mPort(-1), mSession(nullptr), mSftp(nullptr), mPublicKeyAuthInfo(nullptr) {
#ifndef Q_OS_WIN
qCDebug(KIO_SFTP_LOG) << "pid = " << getpid();
qCDebug(KIO_SFTP_LOG) << "debug = " << getenv("KIO_SFTP_LOG_VERBOSITY");
#endif
mCallbacks = (ssh_callbacks) malloc(sizeof(struct ssh_callbacks_struct));
if (mCallbacks == nullptr) {
error(KIO::ERR_OUT_OF_MEMORY, i18n("Could not allocate callbacks"));
return;
}
ZERO_STRUCTP(mCallbacks);
mCallbacks->userdata = this;
mCallbacks->auth_function = ::auth_callback;
ssh_callbacks_init(mCallbacks);
char *verbosity = getenv("KIO_SFTP_LOG_VERBOSITY");
if (verbosity != nullptr) {
int level = atoi(verbosity);
int rc;
rc = ssh_set_log_level(level);
if (rc != SSH_OK) {
error(KIO::ERR_INTERNAL, i18n("Could not set log verbosity."));
return;
}
rc = ssh_set_log_userdata(this);
if (rc != SSH_OK) {
error(KIO::ERR_INTERNAL, i18n("Could not set log userdata."));
return;
}
rc = ssh_set_log_callback(::log_callback);
if (rc != SSH_OK) {
error(KIO::ERR_INTERNAL, i18n("Could not set log callback."));
return;
}
}
}
sftpProtocol::~sftpProtocol() {
#ifndef Q_OS_WIN
qCDebug(KIO_SFTP_LOG) << "pid = " << getpid();
#endif
closeConnection();
delete mCallbacks;
delete mPublicKeyAuthInfo; // for precaution
/* cleanup and shut down cryto stuff */
ssh_finalize();
}
void sftpProtocol::setHost(const QString& host, quint16 port, const QString& user, const QString& pass) {
qCDebug(KIO_SFTP_LOG) << user << "@" << host << ":" << port;
// Close connection if the request is to another server...
if (host != mHost || port != mPort ||
user != mUsername || pass != mPassword) {
closeConnection();
}
mHost = host;
mPort = port;
mUsername = user;
mPassword = pass;
}
bool sftpProtocol::sftpOpenConnection (const AuthInfo& info)
{
mSession = ssh_new();
if (mSession == nullptr) {
error(KIO::ERR_OUT_OF_MEMORY, i18n("Could not create a new SSH session."));
return false;
}
long timeout_sec = 30, timeout_usec = 0;
qCDebug(KIO_SFTP_LOG) << "Creating the SSH session and setting options";
// Set timeout
int rc = ssh_options_set(mSession, SSH_OPTIONS_TIMEOUT, &timeout_sec);
if (rc < 0) {
error(KIO::ERR_INTERNAL, i18n("Could not set a timeout."));
return false;
}
rc = ssh_options_set(mSession, SSH_OPTIONS_TIMEOUT_USEC, &timeout_usec);
if (rc < 0) {
error(KIO::ERR_INTERNAL, i18n("Could not set a timeout."));
return false;
}
// Don't use any compression
rc = ssh_options_set(mSession, SSH_OPTIONS_COMPRESSION_C_S, "none");
if (rc < 0) {
error(KIO::ERR_INTERNAL, i18n("Could not set compression."));
return false;
}
rc = ssh_options_set(mSession, SSH_OPTIONS_COMPRESSION_S_C, "none");
if (rc < 0) {
error(KIO::ERR_INTERNAL, i18n("Could not set compression."));
return false;
}
// Set host and port
rc = ssh_options_set(mSession, SSH_OPTIONS_HOST, mHost.toUtf8().constData());
if (rc < 0) {
error(KIO::ERR_INTERNAL, i18n("Could not set host."));
return false;
}
if (mPort > 0) {
rc = ssh_options_set(mSession, SSH_OPTIONS_PORT, &mPort);
if (rc < 0) {
error(KIO::ERR_INTERNAL, i18n("Could not set port."));
return false;
}
}
// Set the username
if (!info.username.isEmpty()) {
rc = ssh_options_set(mSession, SSH_OPTIONS_USER, info.username.toUtf8().constData());
if (rc < 0) {
error(KIO::ERR_INTERNAL, i18n("Could not set username."));
return false;
}
}
// Read ~/.ssh/config
rc = ssh_options_parse_config(mSession, nullptr);
if (rc < 0) {
error(KIO::ERR_INTERNAL, i18n("Could not parse the config file."));
return false;
}
ssh_set_callbacks(mSession, mCallbacks);
qCDebug(KIO_SFTP_LOG) << "Trying to connect to the SSH server";
unsigned int effectivePort;
if (mPort > 0) {
effectivePort = mPort;
} else {
effectivePort = DEFAULT_SFTP_PORT;
ssh_options_get_port(mSession, &effectivePort);
}
qCDebug(KIO_SFTP_LOG) << "username=" << mUsername << ", host=" << mHost << ", port=" << effectivePort;
infoMessage(xi18n("Opening SFTP connection to host %1:%2", mHost, QString::number(effectivePort)));
/* try to connect */
rc = ssh_connect(mSession);
if (rc < 0) {
error(KIO::ERR_SLAVE_DEFINED, QString::fromUtf8(ssh_get_error(mSession)));
closeConnection();
return false;
}
return true;
}
void sftpProtocol::openConnection() {
if (mConnected) {
return;
}
if (mHost.isEmpty()) {
qCDebug(KIO_SFTP_LOG) << "openConnection(): Need hostname...";
error(KIO::ERR_UNKNOWN_HOST, QString());
return;
}
AuthInfo info;
info.url.setScheme("sftp");
info.url.setHost(mHost);
if ( mPort > 0 && mPort != DEFAULT_SFTP_PORT ) {
info.url.setPort(mPort);
}
info.url.setUserName(mUsername);
info.username = mUsername;
// Check for cached authentication info if no password is specified...
if (mPassword.isEmpty()) {
qCDebug(KIO_SFTP_LOG) << "checking cache: info.username =" << info.username
<< ", info.url =" << info.url.toDisplayString();
checkCachedAuthentication(info);
} else {
info.password = mPassword;
}
// Start the ssh connection.
QString msg; // msg for dialog box
QString caption; // dialog box caption
unsigned char *hash = nullptr; // the server hash
ssh_key srv_pubkey;
char *hexa;
size_t hlen;
int rc, state;
// Attempt to start a ssh session and establish a connection with the server.
if (!sftpOpenConnection(info)) {
return;
}
qCDebug(KIO_SFTP_LOG) << "Getting the SSH server hash";
/* get the hash */
rc = ssh_get_publickey(mSession, &srv_pubkey);
if (rc < 0) {
error(KIO::ERR_SLAVE_DEFINED, QString::fromUtf8(ssh_get_error(mSession)));
closeConnection();
return;
}
rc = ssh_get_publickey_hash(srv_pubkey,
SSH_PUBLICKEY_HASH_SHA1,
&hash,
&hlen);
ssh_key_free(srv_pubkey);
if (rc < 0) {
error(KIO::ERR_SLAVE_DEFINED,
i18n("Could not create hash from server public key"));
closeConnection();
return;
}
qCDebug(KIO_SFTP_LOG) << "Checking if the SSH server is known";
/* check the server public key hash */
state = ssh_is_server_known(mSession);
switch (state) {
case SSH_SERVER_KNOWN_OK:
break;
case SSH_SERVER_FOUND_OTHER:
ssh_string_free_char((char *)hash);
error(KIO::ERR_SLAVE_DEFINED, i18n("The host key for this server was "
"not found, but another type of key exists.\n"
"An attacker might change the default server key to confuse your "
"client into thinking the key does not exist.\n"
"Please contact your system administrator.\n%1", QString::fromUtf8(ssh_get_error(mSession))));
closeConnection();
return;
case SSH_SERVER_KNOWN_CHANGED:
hexa = ssh_get_hexa(hash, hlen);
ssh_string_free_char((char *)hash);
/* TODO print known_hosts file, port? */
error(KIO::ERR_SLAVE_DEFINED, i18n("The host key for the server %1 has changed.\n"
"This could either mean that DNS SPOOFING is happening or the IP "
"address for the host and its host key have changed at the same time.\n"
"The fingerprint for the key sent by the remote host is:\n %2\n"
"Please contact your system administrator.\n%3",
mHost, QString::fromUtf8(hexa), QString::fromUtf8(ssh_get_error(mSession))));
ssh_string_free_char(hexa);
closeConnection();
return;
case SSH_SERVER_FILE_NOT_FOUND:
case SSH_SERVER_NOT_KNOWN:
hexa = ssh_get_hexa(hash, hlen);
ssh_string_free_char((char *)hash);
caption = i18n("Warning: Cannot verify host's identity.");
msg = i18n("The authenticity of host %1 cannot be established.\n"
"The key fingerprint is: %2\n"
"Are you sure you want to continue connecting?", mHost, hexa);
ssh_string_free_char(hexa);
if (KMessageBox::Yes != messageBox(WarningYesNo, msg, caption)) {
closeConnection();
error(KIO::ERR_USER_CANCELED, QString());
return;
}
/* write the known_hosts file */
qCDebug(KIO_SFTP_LOG) << "Adding server to known_hosts file.";
if (ssh_write_knownhost(mSession) < 0) {
error(KIO::ERR_USER_CANCELED, QString::fromUtf8(ssh_get_error(mSession)));
closeConnection();
return;
}
break;
case SSH_SERVER_ERROR:
ssh_string_free_char((char *)hash);
error(KIO::ERR_SLAVE_DEFINED, QString::fromUtf8(ssh_get_error(mSession)));
return;
}
qCDebug(KIO_SFTP_LOG) << "Trying to authenticate with the server";
// Try to login without authentication
rc = ssh_userauth_none(mSession, nullptr);
if (rc == SSH_AUTH_ERROR) {
closeConnection();
error(KIO::ERR_COULD_NOT_LOGIN, i18n("Authentication failed."));
return;
}
// This NEEDS to be called after ssh_userauth_none() !!!
int method = ssh_auth_list(mSession);
if (rc != SSH_AUTH_SUCCESS && method == 0) {
closeConnection();
error(KIO::ERR_COULD_NOT_LOGIN, i18n("Authentication failed. The server "
"didn't send any authentication methods"));
return;
}
// Try to authenticate with public key first
if (rc != SSH_AUTH_SUCCESS && (method & SSH_AUTH_METHOD_PUBLICKEY)) {
qCDebug(KIO_SFTP_LOG) << "Trying to authenticate with public key";
for(;;) {
rc = ssh_userauth_publickey_auto(mSession, nullptr, nullptr);
if (rc == SSH_AUTH_ERROR) {
qCDebug(KIO_SFTP_LOG) << "Public key authentication failed:" <<
QString::fromUtf8(ssh_get_error(mSession));
closeConnection();
clearPubKeyAuthInfo();
error(KIO::ERR_COULD_NOT_LOGIN, i18n("Authentication failed."));
return;
} else if (rc != SSH_AUTH_DENIED || !mPublicKeyAuthInfo || !mPublicKeyAuthInfo->isModified()) {
clearPubKeyAuthInfo();
break;
}
}
}
// Try to authenticate with GSSAPI
if (rc != SSH_AUTH_SUCCESS && (method & SSH_AUTH_METHOD_GSSAPI_MIC)) {
qCDebug(KIO_SFTP_LOG) << "Trying to authenticate with GSSAPI";
rc = ssh_userauth_gssapi(mSession);
if (rc == SSH_AUTH_ERROR) {
qCDebug(KIO_SFTP_LOG) << "Public key authentication failed:" <<
QString::fromUtf8(ssh_get_error(mSession));
closeConnection();
error(KIO::ERR_COULD_NOT_LOGIN, i18n("Authentication failed."));
return;
}
}
// Try to authenticate with keyboard interactive
if (rc != SSH_AUTH_SUCCESS && (method & SSH_AUTH_METHOD_INTERACTIVE)) {
qCDebug(KIO_SFTP_LOG) << "Trying to authenticate with keyboard interactive";
AuthInfo info2 (info);
rc = authenticateKeyboardInteractive(info2);
if (rc == SSH_AUTH_SUCCESS) {
info = info2;
} else if (rc == SSH_AUTH_ERROR) {
qCDebug(KIO_SFTP_LOG) << "Keyboard interactive authentication failed:"
<< QString::fromUtf8(ssh_get_error(mSession));
closeConnection();
error(KIO::ERR_COULD_NOT_LOGIN, i18n("Authentication failed."));
return;
}
}
// Try to authenticate with password
if (rc != SSH_AUTH_SUCCESS && (method & SSH_AUTH_METHOD_PASSWORD)) {
qCDebug(KIO_SFTP_LOG) << "Trying to authenticate with password";
info.caption = i18n("SFTP Login");
info.prompt = i18n("Please enter your username and password.");
info.comment = info.url.url();
info.commentLabel = i18n("Site:");
bool isFirstLoginAttempt = true;
for(;;) {
if (!isFirstLoginAttempt || info.password.isEmpty()) {
info.keepPassword = true; // make the "keep Password" check box visible to the user.
info.setModified(false);
QString username (info.username);
const QString errMsg(isFirstLoginAttempt ? QString() : i18n("Incorrect username or password"));
qCDebug(KIO_SFTP_LOG) << "Username:" << username << "first attempt?"
<< isFirstLoginAttempt << "error:" << errMsg;
// Handle user canceled or dialog failed to open...
int errCode = openPasswordDialogV2(info, errMsg);
if (errCode != 0) {
qCDebug(KIO_SFTP_LOG) << "User canceled password/retry dialog";
closeConnection();
error(errCode, QString());
return;
}
// If the user name changes, we have to restablish connection again
// since the user name must always be set before calling ssh_connect.
if (wasUsernameChanged(username, info)) {
qCDebug(KIO_SFTP_LOG) << "Username changed to" << info.username;
if (!info.url.userName().isEmpty()) {
info.url.setUserName(info.username);
}
closeConnection();
if (!sftpOpenConnection(info)) {
return;
}
}
}
rc = ssh_userauth_password(mSession, info.username.toUtf8().constData(), info.password.toUtf8().constData());
if (rc == SSH_AUTH_SUCCESS) {
break;
} else if (rc == SSH_AUTH_ERROR) {
qCDebug(KIO_SFTP_LOG) << "Password authentication failed:"
<< QString::fromUtf8(ssh_get_error(mSession));
closeConnection();
error(KIO::ERR_COULD_NOT_LOGIN, i18n("Authentication failed."));
return;
}
isFirstLoginAttempt = false; // failed attempt to login.
info.password.clear(); // clear the password after failed attempts.
}
}
// If we're still not authenticated then we need to leave.
if (rc != SSH_AUTH_SUCCESS) {
error(KIO::ERR_COULD_NOT_LOGIN, i18n("Authentication failed."));
return;
}
// start sftp session
qCDebug(KIO_SFTP_LOG) << "Trying to request the sftp session";
mSftp = sftp_new(mSession);
if (mSftp == nullptr) {
closeConnection();
error(KIO::ERR_COULD_NOT_LOGIN, i18n("Unable to request the SFTP subsystem. "
"Make sure SFTP is enabled on the server."));
return;
}
qCDebug(KIO_SFTP_LOG) << "Trying to initialize the sftp session";
if (sftp_init(mSftp) < 0) {
closeConnection();
error(KIO::ERR_COULD_NOT_LOGIN, i18n("Could not initialize the SFTP session."));
return;
}
// Login succeeded!
infoMessage(i18n("Successfully connected to %1", mHost));
if (info.keepPassword) {
qCDebug(KIO_SFTP_LOG) << "Caching info.username = " << info.username
<< ", info.url = " << info.url.toDisplayString();
cacheAuthentication(info);
}
// Update the original username in case it was changed!
if (!mUsername.isEmpty()) {
mUsername = info.username;
}
setTimeoutSpecialCommand(KIO_SFTP_SPECIAL_TIMEOUT);
mConnected = true;
connected();
info.password.fill('x');
info.password.clear();
}
void sftpProtocol::closeConnection() {
qCDebug(KIO_SFTP_LOG);
if (mSftp) {
sftp_free(mSftp);
mSftp = nullptr;
}
if (mSession) {
ssh_disconnect(mSession);
mSession = nullptr;
}
mConnected = false;
}
void sftpProtocol::special(const QByteArray &) {
int rc;
qCDebug(KIO_SFTP_LOG) << "special(): polling";
if (!mSftp)
return;
/*
* ssh_channel_poll() returns the number of bytes that may be read on the
* channel. It does so by checking the input buffer and eventually the
* network socket for data to read. If the input buffer is not empty, it
* will not probe the network (and such not read packets nor reply to
* keepalives).
*
* As ssh_channel_poll can act on two specific buffers (a channel has two
* different stream: stdio and stderr), polling for data on the stderr
* stream has more chance of not being in the problematic case (data left
* in the buffer). Checking the return value (for >0) would be a good idea
* to debug the problem.
*/
rc = ssh_channel_poll(mSftp->channel, 0);
if (rc > 0) {
rc = ssh_channel_poll(mSftp->channel, 1);
}
if (rc < 0) {
qCDebug(KIO_SFTP_LOG) << "ssh_channel_poll failed: " << ssh_get_error(mSession);
}
setTimeoutSpecialCommand(KIO_SFTP_SPECIAL_TIMEOUT);
}
void sftpProtocol::open(const QUrl &url, QIODevice::OpenMode mode) {
qCDebug(KIO_SFTP_LOG) << "open: " << url;
if (!sftpLogin()) {
return;
}
const QString path = url.path();
const QByteArray path_c = path.toUtf8();
sftp_attributes sb = sftp_lstat(mSftp, path_c.constData());
if (sb == nullptr) {
reportError(url, sftp_get_error(mSftp));
return;
}
switch (sb->type) {
case SSH_FILEXFER_TYPE_DIRECTORY:
error(KIO::ERR_IS_DIRECTORY, url.toDisplayString());
sftp_attributes_free(sb);
return;
case SSH_FILEXFER_TYPE_SPECIAL:
case SSH_FILEXFER_TYPE_UNKNOWN:
error(KIO::ERR_CANNOT_OPEN_FOR_READING, url.toDisplayString());
sftp_attributes_free(sb);
return;
case SSH_FILEXFER_TYPE_SYMLINK:
case SSH_FILEXFER_TYPE_REGULAR:
break;
}
KIO::filesize_t fileSize = sb->size;
sftp_attributes_free(sb);
int flags = 0;
if (mode & QIODevice::ReadOnly) {
if (mode & QIODevice::WriteOnly) {
flags = O_RDWR | O_CREAT;
} else {
flags = O_RDONLY;
}
} else if (mode & QIODevice::WriteOnly) {
flags = O_WRONLY | O_CREAT;
}
if (mode & QIODevice::Append) {
flags |= O_APPEND;
} else if (mode & QIODevice::Truncate) {
flags |= O_TRUNC;
}
if (flags & O_CREAT) {
mOpenFile = sftp_open(mSftp, path_c.constData(), flags, 0644);
} else {
mOpenFile = sftp_open(mSftp, path_c.constData(), flags, 0);
}
if (mOpenFile == nullptr) {
error(KIO::ERR_CANNOT_OPEN_FOR_READING, path);
return;
}
// Determine the mimetype of the file to be retrieved, and emit it.
// This is mandatory in all slaves (for KRun/BrowserRun to work).
// If we're not opening the file ReadOnly or ReadWrite, don't attempt to
// read the file and send the mimetype.
if (mode & QIODevice::ReadOnly) {
size_t bytesRequested = 1024;
ssize_t bytesRead = 0;
QVarLengthArray<char> buffer(bytesRequested);
bytesRead = sftp_read(mOpenFile, buffer.data(), bytesRequested);
if (bytesRead < 0) {
error(KIO::ERR_COULD_NOT_READ, mOpenUrl.toDisplayString());
close();
return;
} else {
QByteArray fileData = QByteArray::fromRawData(buffer.data(), bytesRead);
QMimeDatabase db;
QMimeType mime = db.mimeTypeForFileNameAndData(mOpenUrl.fileName(), fileData);
emit mimeType(mime.name());
// Go back to the beginning of the file.
sftp_rewind(mOpenFile);
}
}
mOpenUrl = url;
openOffset = 0;
totalSize(fileSize);
position(0);
opened();
}
void sftpProtocol::read(KIO::filesize_t bytes) {
qCDebug(KIO_SFTP_LOG) << "read, offset = " << openOffset << ", bytes = " << bytes;
Q_ASSERT(mOpenFile != nullptr);
QVarLengthArray<char> buffer(bytes);
ssize_t bytesRead = sftp_read(mOpenFile, buffer.data(), bytes);
Q_ASSERT(bytesRead <= static_cast<ssize_t>(bytes));
if (bytesRead < 0) {
qCDebug(KIO_SFTP_LOG) << "Could not read " << mOpenUrl;
error(KIO::ERR_COULD_NOT_READ, mOpenUrl.toDisplayString());
close();
return;
}
const QByteArray fileData = QByteArray::fromRawData(buffer.data(), bytesRead);
data(fileData);
}
void sftpProtocol::write(const QByteArray &data) {
qCDebug(KIO_SFTP_LOG) << "write, offset = " << openOffset << ", bytes = " << data.size();
Q_ASSERT(mOpenFile != nullptr);
ssize_t bytesWritten = sftp_write(mOpenFile, data.data(), data.size());
if (bytesWritten < 0) {
qCDebug(KIO_SFTP_LOG) << "Could not write to " << mOpenUrl;
error(KIO::ERR_COULD_NOT_WRITE, mOpenUrl.toDisplayString());
close();
return;
}
written(bytesWritten);
}
void sftpProtocol::seek(KIO::filesize_t offset) {
qCDebug(KIO_SFTP_LOG) << "seek, offset = " << offset;
Q_ASSERT(mOpenFile != nullptr);
if (sftp_seek64(mOpenFile, static_cast<uint64_t>(offset)) < 0) {
error(KIO::ERR_COULD_NOT_SEEK, mOpenUrl.path());
close();
}
position(sftp_tell64(mOpenFile));
}
void sftpProtocol::close() {
sftp_close(mOpenFile);
mOpenFile = nullptr;
finished();
}
void sftpProtocol::get(const QUrl& url) {
qCDebug(KIO_SFTP_LOG) << url;
int errorCode = 0;
const sftpProtocol::StatusCode cs = sftpGet(url, errorCode);
if (cs == sftpProtocol::Success) {
finished();
return;
}
// The call to sftpGet should only return server side errors since the file
// descriptor parameter is set to -1.
if (cs == sftpProtocol::ServerError && errorCode) {
error(errorCode, url.toDisplayString());
}
}
sftpProtocol::StatusCode sftpProtocol::sftpGet(const QUrl& url, int& errorCode, KIO::fileoffset_t offset, int fd) {
qCDebug(KIO_SFTP_LOG) << url;
if (!sftpLogin()) {
return sftpProtocol::ServerError;
}
QByteArray path = url.path().toUtf8();
sftp_file file = nullptr;
KIO::filesize_t totalbytesread = 0;
QByteArray filedata;
sftp_attributes sb = sftp_lstat(mSftp, path.constData());
if (sb == nullptr) {
errorCode = toKIOError(sftp_get_error(mSftp));
return sftpProtocol::ServerError;
}
switch (sb->type) {
case SSH_FILEXFER_TYPE_DIRECTORY:
errorCode = KIO::ERR_IS_DIRECTORY;
sftp_attributes_free(sb);
return sftpProtocol::ServerError;
case SSH_FILEXFER_TYPE_SPECIAL:
case SSH_FILEXFER_TYPE_UNKNOWN:
errorCode = KIO::ERR_CANNOT_OPEN_FOR_READING;
sftp_attributes_free(sb);
return sftpProtocol::ServerError;
case SSH_FILEXFER_TYPE_SYMLINK:
case SSH_FILEXFER_TYPE_REGULAR:
break;
}
// Open file
file = sftp_open(mSftp, path.constData(), O_RDONLY, 0);
if (file == nullptr) {
errorCode = KIO::ERR_CANNOT_OPEN_FOR_READING;
sftp_attributes_free(sb);
return sftpProtocol::ServerError;
}
char mimeTypeBuf[1024];
ssize_t bytesread = sftp_read(file, mimeTypeBuf, sizeof(mimeTypeBuf));
if (bytesread < 0) {
errorCode = KIO::ERR_COULD_NOT_READ;
return sftpProtocol::ServerError;
} else {
QMimeDatabase db;
QMimeType mime = db.mimeTypeForFileNameAndData(url.fileName(), QByteArray(mimeTypeBuf, bytesread));
if (!mime.isDefault()) {
emit mimeType(mime.name());
} else {
mime = db.mimeTypeForUrl(url);
emit mimeType(mime.name());
}
sftp_rewind(file);
}
// Set the total size
totalSize(sb->size);
// If offset is not specified, check the "resume" meta-data.
if (offset < 0) {
const QString resumeOffsetStr = metaData(QLatin1String("resume"));
if (!resumeOffsetStr.isEmpty()) {
bool ok;
qlonglong resumeOffset = resumeOffsetStr.toLongLong(&ok);
if (ok) {
offset = resumeOffset;
}
}
}
// If we can resume, offset the buffer properly.
if (offset > 0 && ((unsigned long long) offset < sb->size))
{
if (sftp_seek64(file, offset) == 0) {
canResume();
totalbytesread = offset;
qCDebug(KIO_SFTP_LOG) << "Resume offset: " << QString::number(offset);
}
}
bytesread = 0;
sftpProtocol::GetRequest request(file, sb);
for (;;) {
// Enqueue get requests
if (!request.enqueueChunks()) {
errorCode = KIO::ERR_COULD_NOT_READ;
return sftpProtocol::ServerError;
}
filedata.clear();
bytesread = request.readChunks(filedata);
// Read pending get requests
if (bytesread == -1) {
errorCode = KIO::ERR_COULD_NOT_READ;
return sftpProtocol::ServerError;
} else if (bytesread == 0) {
if (file->eof)
break;
else
continue;
}
if (fd == -1) {
data(filedata);
} else if ((errorCode = writeToFile(fd, filedata.constData(), filedata.size())) != 0) {
return sftpProtocol::ClientError;
}
// increment total bytes read
totalbytesread += filedata.length();
processedSize(totalbytesread);
}
if (fd == -1)
data(QByteArray());
processedSize(static_cast<KIO::filesize_t>(sb->size));
return sftpProtocol::Success;
}
void sftpProtocol::put(const QUrl& url, int permissions, KIO::JobFlags flags) {
qCDebug(KIO_SFTP_LOG) << url << ", permissions =" << permissions
<< ", overwrite =" << (flags & KIO::Overwrite)
<< ", resume =" << (flags & KIO::Resume);
qCDebug(KIO_SFTP_LOG) << url;
int errorCode = 0;
const sftpProtocol::StatusCode cs = sftpPut(url, permissions, flags, errorCode);
if (cs == sftpProtocol::Success) {
finished();
return;
}
// The call to sftpPut should only return server side errors since the file
// descriptor parameter is set to -1.
if (cs == sftpProtocol::ServerError && errorCode) {
error(errorCode, url.toDisplayString());
}
}
sftpProtocol::StatusCode sftpProtocol::sftpPut(const QUrl& url, int permissions, JobFlags flags, int& errorCode, int fd) {
qCDebug(KIO_SFTP_LOG) << url << ", permissions =" << permissions
<< ", overwrite =" << (flags & KIO::Overwrite)
<< ", resume =" << (flags & KIO::Resume);
if (!sftpLogin()) {
return sftpProtocol::ServerError;
}
const QString dest_orig = url.path();
const QByteArray dest_orig_c = dest_orig.toUtf8();
const QString dest_part = dest_orig + ".part";
const QByteArray dest_part_c = dest_part.toUtf8();
uid_t owner = 0;
gid_t group = 0;
sftp_attributes sb = sftp_lstat(mSftp, dest_orig_c.constData());
const bool bOrigExists = (sb != nullptr);
bool bPartExists = false;
const bool bMarkPartial = config()->readEntry("MarkPartial", true);
// Don't change permissions of the original file
if (bOrigExists) {
permissions = sb->permissions;
owner = sb->uid;
group = sb->gid;
}
if (bMarkPartial) {
sftp_attributes sbPart = sftp_lstat(mSftp, dest_part_c.constData());
bPartExists = (sbPart != nullptr);
if (bPartExists && !(flags & KIO::Resume) && !(flags & KIO::Overwrite) &&
sbPart->size > 0 && sbPart->type == SSH_FILEXFER_TYPE_REGULAR) {
if (fd == -1) {
// Maybe we can use this partial file for resuming
// Tell about the size we have, and the app will tell us
// if it's ok to resume or not.
qCDebug(KIO_SFTP_LOG) << "calling canResume with " << sbPart->size;
flags |= canResume(sbPart->size) ? KIO::Resume : KIO::DefaultFlags;
qCDebug(KIO_SFTP_LOG) << "put got answer " << (flags & KIO::Resume);
} else {
KIO::filesize_t pos = seekPos(fd, sbPart->size, SEEK_SET);
if (pos != sbPart->size) {
qCDebug(KIO_SFTP_LOG) << "Failed to seek to" << sbPart->size << "bytes in source file. Reason given" << strerror(errno);
sftp_attributes_free(sb);
sftp_attributes_free(sbPart);
errorCode = ERR_COULD_NOT_SEEK;
return sftpProtocol::ClientError;
}
flags |= KIO::Resume;
}
qCDebug(KIO_SFTP_LOG) << "Resuming at" << sbPart->size;
sftp_attributes_free(sbPart);
}
}
if (bOrigExists && !(flags & KIO::Overwrite) && !(flags & KIO::Resume)) {
if (sb->type == SSH_FILEXFER_TYPE_DIRECTORY) {
errorCode = KIO::ERR_DIR_ALREADY_EXIST;
} else {
errorCode = KIO::ERR_FILE_ALREADY_EXIST;
}
sftp_attributes_free(sb);
return sftpProtocol::ServerError;
}
QByteArray dest;
int result = -1;
sftp_file file = nullptr;
StatusCode cs = sftpProtocol::Success;
KIO::fileoffset_t totalBytesSent = 0;
// Loop until we got 0 (end of data)
do {
QByteArray buffer;
if (fd == -1) {
dataReq(); // Request for data
result = readData(buffer);
} else {
char buf[MAX_XFER_BUF_SIZE]; //
result = ::read(fd, buf, sizeof(buf));
if(result < 0) {
errorCode = ERR_COULD_NOT_READ;
cs = sftpProtocol::ClientError;
break;
}
buffer = QByteArray(buf, result);
}
if (result >= 0) {
if (dest.isEmpty()) {
if (bMarkPartial) {
qCDebug(KIO_SFTP_LOG) << "Appending .part extension to" << dest_orig;
dest = dest_part_c;
if (bPartExists && !(flags & KIO::Resume)) {
qCDebug(KIO_SFTP_LOG) << "Deleting partial file" << dest_part;
sftp_unlink(mSftp, dest_part_c.constData());
// Catch errors when we try to open the file.
}
} else {
dest = dest_orig_c; // Will be automatically truncated below...
} // bMarkPartial
if ((flags & KIO::Resume)) {
sftp_attributes fstat;
qCDebug(KIO_SFTP_LOG) << "Trying to append: " << dest;
file = sftp_open(mSftp, dest.constData(), O_RDWR, 0); // append if resuming
if (file) {
fstat = sftp_fstat(file);
if (fstat) {
sftp_seek64(file, fstat->size); // Seek to end TODO
totalBytesSent += fstat->size;
sftp_attributes_free(fstat);
}
}
} else {
mode_t initialMode;
if (permissions != -1) {
initialMode = permissions | S_IWUSR | S_IRUSR;
} else {
initialMode = 0644;
}
qCDebug(KIO_SFTP_LOG) << "Trying to open:" << QString(dest) << ", mode=" << QString::number(initialMode);
file = sftp_open(mSftp, dest.constData(), O_CREAT | O_TRUNC | O_WRONLY, initialMode);
} // flags & KIO::Resume
if (file == nullptr) {
qCDebug(KIO_SFTP_LOG) << "COULD NOT WRITE " << QString(dest)
<< ", permissions=" << permissions
<< ", error=" << ssh_get_error(mSession);
if (sftp_get_error(mSftp) == SSH_FX_PERMISSION_DENIED) {
errorCode = KIO::ERR_WRITE_ACCESS_DENIED;
} else {
errorCode = KIO::ERR_CANNOT_OPEN_FOR_WRITING;
}
cs = sftpProtocol::ServerError;
result = -1;
continue;
} // file
} // dest.isEmpty
ssize_t bytesWritten = sftp_write(file, buffer.data(), buffer.size());
if (bytesWritten < 0) {
errorCode = KIO::ERR_COULD_NOT_WRITE;
result = -1;
} else {
totalBytesSent += bytesWritten;
emit processedSize(totalBytesSent);
}
} // result
} while (result > 0);
sftp_attributes_free(sb);
// An error occurred deal with it.
if (result < 0) {
qCDebug(KIO_SFTP_LOG) << "Error during 'put'. Aborting.";
if (file != nullptr) {
sftp_close(file);
sftp_attributes attr = sftp_stat(mSftp, dest.constData());
if (bMarkPartial && attr != nullptr) {
size_t size = config()->readEntry("MinimumKeepSize", DEFAULT_MINIMUM_KEEP_SIZE);
if (attr->size < size) {
sftp_unlink(mSftp, dest.constData());
}
}
sftp_attributes_free(attr);
}
//::exit(255);
return cs;
}
if (file == nullptr) { // we got nothing to write out, so we never opened the file
return sftpProtocol::Success;
}
if (sftp_close(file) < 0) {
qCWarning(KIO_SFTP_LOG) << "Error when closing file descriptor";
error(KIO::ERR_COULD_NOT_WRITE, dest_orig);
return sftpProtocol::ServerError;
}
// after full download rename the file back to original name
if (bMarkPartial) {
// If the original URL is a symlink and we were asked to overwrite it,
// remove the symlink first. This ensures that we do not overwrite the
// current source if the symlink points to it.
if ((flags & KIO::Overwrite)) {
sftp_unlink(mSftp, dest_orig_c.constData());
}
if (sftp_rename(mSftp, dest.constData(), dest_orig_c.constData()) < 0) {
qCWarning(KIO_SFTP_LOG) << " Couldn't rename " << dest << " to " << dest_orig;
errorCode = KIO::ERR_CANNOT_RENAME_PARTIAL;
return sftpProtocol::ServerError;
}
}
// set final permissions
if (permissions != -1 && !(flags & KIO::Resume)) {
qCDebug(KIO_SFTP_LOG) << "Trying to set final permissions of " << dest_orig << " to " << QString::number(permissions);
if (sftp_chmod(mSftp, dest_orig_c.constData(), permissions) < 0) {
errorCode = -1; // force copy to call sftpSendWarning...
return sftpProtocol::ServerError;
}
}
// set original owner and group
if (bOrigExists) {
qCDebug(KIO_SFTP_LOG) << "Trying to restore original owner and group of " << dest_orig;
if (sftp_chown(mSftp, dest_orig_c.constData(), owner, group) < 0) {
qCWarning(KIO_SFTP_LOG) << "Could not change owner and group for" << dest_orig;
// warning(i18n( "Could not change owner and group for\n%1", dest_orig));
}
}
// set modification time
const QString mtimeStr = metaData("modified");
if (!mtimeStr.isEmpty()) {
QDateTime dt = QDateTime::fromString(mtimeStr, Qt::ISODate);
if (dt.isValid()) {
struct timeval times[2];
sftp_attributes attr = sftp_lstat(mSftp, dest_orig_c.constData());
if (attr != nullptr) {
times[0].tv_sec = attr->atime; //// access time, unchanged
times[1].tv_sec = dt.toTime_t(); // modification time
times[0].tv_usec = times[1].tv_usec = 0;
qCDebug(KIO_SFTP_LOG) << "Trying to restore mtime for " << dest_orig << " to: " << mtimeStr;
result = sftp_utimes(mSftp, dest_orig_c.constData(), times);
if (result < 0) {
qCWarning(KIO_SFTP_LOG) << "Failed to set mtime for" << dest_orig;
}
sftp_attributes_free(attr);
}
}
}
return sftpProtocol::Success;
}
void sftpProtocol::copy(const QUrl &src, const QUrl &dest, int permissions, KIO::JobFlags flags)
{
qCDebug(KIO_SFTP_LOG) << src << " -> " << dest << " , permissions = " << QString::number(permissions)
<< ", overwrite = " << (flags & KIO::Overwrite)
<< ", resume = " << (flags & KIO::Resume);
QString sCopyFile;
int errorCode = 0;
StatusCode cs = sftpProtocol::ClientError;
const bool isSourceLocal = src.isLocalFile();
const bool isDestinationLocal = dest.isLocalFile();
if (!isSourceLocal && isDestinationLocal) { // sftp -> file
sCopyFile = dest.toLocalFile();
cs = sftpCopyGet(src, sCopyFile, permissions, flags, errorCode);
if (cs == sftpProtocol::ServerError)
sCopyFile = src.url();
} else if (isSourceLocal && !isDestinationLocal) { // file -> sftp
sCopyFile = src.toLocalFile();
cs = sftpCopyPut(dest, sCopyFile, permissions, flags, errorCode);
if (cs == sftpProtocol::ServerError)
sCopyFile = dest.url();
} else {
errorCode = KIO::ERR_UNSUPPORTED_ACTION;
sCopyFile.clear();
}
// On success or errorcode < 0, emit the finished signal and
// send a warning message to the client if errorCode < 0.
if (cs == sftpProtocol::Success || errorCode < 0) {
if (errorCode < 0)
sftpSendWarning(errorCode, sCopyFile);
finished();
return;
}
if (errorCode) {
error(errorCode, sCopyFile);
}
}
sftpProtocol::StatusCode sftpProtocol::sftpCopyGet(const QUrl& url, const QString& sCopyFile, int permissions, KIO::JobFlags flags, int& errorCode)
{
qCDebug(KIO_SFTP_LOG) << url << "->" << sCopyFile << ", permissions=" << permissions;
// check if destination is ok ...
QT_STATBUF buff;
const bool bDestExists = (QT_STAT(QFile::encodeName(sCopyFile), &buff) != -1);
if(bDestExists) {
if(S_ISDIR(buff.st_mode)) {
errorCode = ERR_IS_DIRECTORY;
return sftpProtocol::ClientError;
}
if(!(flags & KIO::Overwrite)) {
errorCode = ERR_FILE_ALREADY_EXIST;
return sftpProtocol::ClientError;
}
}
bool bResume = false;
const QString sPart = sCopyFile + QLatin1String(".part"); // do we have a ".part" file?
const bool bPartExists = (QT_STAT(QFile::encodeName(sPart), &buff) != -1);
const bool bMarkPartial = config()->readEntry("MarkPartial", true);
const QString dest = (bMarkPartial ? sPart : sCopyFile);
if (bMarkPartial && bPartExists && buff.st_size > 0) {
if (S_ISDIR(buff.st_mode)) {
errorCode = ERR_DIR_ALREADY_EXIST;
return sftpProtocol::ClientError; // client side error
}
bResume = canResume( buff.st_size );
}
if (bPartExists && !bResume) // get rid of an unwanted ".part" file
QFile::remove(sPart);
// WABA: Make sure that we keep writing permissions ourselves,
// otherwise we can be in for a surprise on NFS.
mode_t initialMode;
if (permissions != -1)
initialMode = permissions | S_IWUSR;
else
initialMode = 0666;
// open the output file ...
int fd = -1;
KIO::fileoffset_t offset = 0;
if (bResume) {
fd = QT_OPEN( QFile::encodeName(sPart), O_RDWR ); // append if resuming
offset = seekPos(fd, 0, SEEK_END);
if(offset < 0) {
errorCode = ERR_CANNOT_RESUME;
::close(fd);
return sftpProtocol::ClientError; // client side error
}
qCDebug(KIO_SFTP_LOG) << "resuming at" << offset;
}
else {
fd = QT_OPEN(QFile::encodeName(dest), O_CREAT | O_TRUNC | O_WRONLY, initialMode);
}
if (fd == -1) {
qCDebug(KIO_SFTP_LOG) << "could not write to" << sCopyFile;
errorCode = (errno == EACCES) ? ERR_WRITE_ACCESS_DENIED : ERR_CANNOT_OPEN_FOR_WRITING;
return sftpProtocol::ClientError;
}
StatusCode result = sftpGet(url, errorCode, offset, fd);
if( ::close(fd) && result == sftpProtocol::Success ) {
errorCode = ERR_COULD_NOT_WRITE;
result = sftpProtocol::ClientError;
}
// handle renaming or deletion of a partial file ...
if (bMarkPartial) {
if (result == sftpProtocol::Success) { // rename ".part" on success
if ( !QFile::rename( QFile::encodeName(sPart), sCopyFile ) ) {
// If rename fails, try removing the destination first if it exists.
if (!bDestExists || !QFile::remove(sCopyFile) || !QFile::rename(sPart, sCopyFile)) {
qCDebug(KIO_SFTP_LOG) << "cannot rename " << sPart << " to " << sCopyFile;
errorCode = ERR_CANNOT_RENAME_PARTIAL;
result = sftpProtocol::ClientError;
}
}
}
else if (QT_STAT( QFile::encodeName(sPart), &buff ) == 0) { // should a very small ".part" be deleted?
const int size = config()->readEntry("MinimumKeepSize", DEFAULT_MINIMUM_KEEP_SIZE);
if (buff.st_size < size)
QFile::remove(sPart);
}
}
const QString mtimeStr = metaData("modified");
if (!mtimeStr.isEmpty()) {
QDateTime dt = QDateTime::fromString(mtimeStr, Qt::ISODate);
if (dt.isValid()) {
struct utimbuf utbuf;
utbuf.actime = buff.st_atime; // access time, unchanged
utbuf.modtime = dt.toTime_t(); // modification time
utime(QFile::encodeName(sCopyFile), &utbuf);
}
}
return result;
}
sftpProtocol::StatusCode sftpProtocol::sftpCopyPut(const QUrl& url, const QString& sCopyFile, int permissions, JobFlags flags, int& errorCode)
{
qCDebug(KIO_SFTP_LOG) << sCopyFile << "->" << url << ", permissions=" << permissions << ", flags" << flags;
// check if source is ok ...
QT_STATBUF buff;
bool bSrcExists = (QT_STAT(QFile::encodeName(sCopyFile), &buff) != -1);
if (bSrcExists) {
if (S_ISDIR(buff.st_mode)) {
errorCode = ERR_IS_DIRECTORY;
return sftpProtocol::ClientError;
}
} else {
errorCode = ERR_DOES_NOT_EXIST;
return sftpProtocol::ClientError;
}
const int fd = QT_OPEN(QFile::encodeName(sCopyFile), O_RDONLY);
if(fd == -1)
{
errorCode = ERR_CANNOT_OPEN_FOR_READING;
return sftpProtocol::ClientError;
}
totalSize(buff.st_size);
// delegate the real work (errorCode gets status) ...
StatusCode ret = sftpPut(url, permissions, flags, errorCode, fd);
::close(fd);
return ret;
}
void sftpProtocol::stat(const QUrl& url) {
qCDebug(KIO_SFTP_LOG) << url;
if (!sftpLogin()) {
return;
}
if (url.path().isEmpty() || QDir::isRelativePath(url.path()) ||
url.path().contains("/./") || url.path().contains("/../")) {
QString cPath;
if (!url.path().isEmpty()) {
cPath = canonicalizePath(url.path());
} else {
cPath = canonicalizePath(QLatin1String("."));
}
if (cPath.isEmpty()) {
error(KIO::ERR_MALFORMED_URL, url.toDisplayString());
return;
}
QUrl redir(url);
redir.setPath(cPath);
redirection(redir);
qCDebug(KIO_SFTP_LOG) << "redirecting to " << redir.url();
finished();
return;
}
QByteArray path = url.path().toUtf8();
const QString sDetails = metaData(QLatin1String("details"));
const int details = sDetails.isEmpty() ? 2 : sDetails.toInt();
UDSEntry entry;
entry.clear();
if (!createUDSEntry(url.fileName(), path, entry, details)) {
error(KIO::ERR_DOES_NOT_EXIST, url.toDisplayString());
return;
}
statEntry(entry);
finished();
}
void sftpProtocol::mimetype(const QUrl& url){
qCDebug(KIO_SFTP_LOG) << url;
if (!sftpLogin()) {
return;
}
// open() feeds the mimetype
open(url, QIODevice::ReadOnly);
close();
finished();
}
void sftpProtocol::listDir(const QUrl& url) {
qCDebug(KIO_SFTP_LOG) << "list directory: " << url;
if (!sftpLogin()) {
return;
}
if (url.path().isEmpty() || QDir::isRelativePath(url.path()) ||
url.path().contains("/./") || url.path().contains("/../")) {
QString cPath;
if (!url.path().isEmpty() ) {
cPath = canonicalizePath(url.path());
} else {
cPath = canonicalizePath(QStringLiteral("."));
}
if (cPath.isEmpty()) {
error(KIO::ERR_MALFORMED_URL, url.toDisplayString());
return;
}
QUrl redir(url);
redir.setPath(cPath);
redirection(redir);
qCDebug(KIO_SFTP_LOG) << "redirecting to " << redir.url();
finished();
return;
}
QByteArray path = url.path().toUtf8();
sftp_dir dp = sftp_opendir(mSftp, path.constData());
if (dp == nullptr) {
reportError(url, sftp_get_error(mSftp));
return;
}
sftp_attributes dirent = nullptr;
const QString sDetails = metaData(QLatin1String("details"));
const int details = sDetails.isEmpty() ? 2 : sDetails.toInt();
UDSEntry entry;
qCDebug(KIO_SFTP_LOG) << "readdir: " << path << ", details: " << QString::number(details);
for (;;) {
mode_t access;
mode_t type;
char *link;
bool isBrokenLink = false;
long long fileType = S_IFREG;
long long size = 0LL;
dirent = sftp_readdir(mSftp, dp);
if (dirent == nullptr) {
break;
}
entry.clear();
entry.insert(KIO::UDSEntry::UDS_NAME, QFile::decodeName(dirent->name));
if (dirent->type == SSH_FILEXFER_TYPE_SYMLINK) {
QByteArray file = path + '/' + QFile::decodeName(dirent->name).toUtf8();
link = sftp_readlink(mSftp, file.constData());
if (link == nullptr) {
sftp_attributes_free(dirent);
error(KIO::ERR_INTERNAL, i18n("Could not read link: %1", QString::fromUtf8(file)));
return;
}
entry.insert(KIO::UDSEntry::UDS_LINK_DEST, QFile::decodeName(link));
free(link);
// A symlink -> follow it only if details > 1
if (details > 1) {
sftp_attributes sb = sftp_stat(mSftp, file.constData());
if (sb == nullptr) {
isBrokenLink = true;
} else {
sftp_attributes_free(dirent);
dirent = sb;
}
}
}
if (isBrokenLink) {
// It is a link pointing to nowhere
fileType = S_IFMT - 1;
access = S_IRWXU | S_IRWXG | S_IRWXO;
size = 0LL;
} else {
switch (dirent->type) {
case SSH_FILEXFER_TYPE_REGULAR:
fileType = S_IFREG;
break;
case SSH_FILEXFER_TYPE_DIRECTORY:
fileType = S_IFDIR;
break;
case SSH_FILEXFER_TYPE_SYMLINK:
fileType = S_IFLNK;
break;
case SSH_FILEXFER_TYPE_SPECIAL:
case SSH_FILEXFER_TYPE_UNKNOWN:
break;
}
access = dirent->permissions & 07777;
size = dirent->size;
}
entry.insert(KIO::UDSEntry::UDS_FILE_TYPE, fileType);
entry.insert(KIO::UDSEntry::UDS_ACCESS, access);
entry.insert(KIO::UDSEntry::UDS_SIZE, size);
if (details > 0) {
if (dirent->owner) {
entry.insert(KIO::UDSEntry::UDS_USER, QString::fromUtf8(dirent->owner));
} else {
entry.insert(KIO::UDSEntry::UDS_USER, QString::number(dirent->uid));
}
if (dirent->group) {
entry.insert(KIO::UDSEntry::UDS_GROUP, QString::fromUtf8(dirent->group));
} else {
entry.insert(KIO::UDSEntry::UDS_GROUP, QString::number(dirent->gid));
}
entry.insert(KIO::UDSEntry::UDS_ACCESS_TIME, dirent->atime);
entry.insert(KIO::UDSEntry::UDS_MODIFICATION_TIME, dirent->mtime);
entry.insert(KIO::UDSEntry::UDS_CREATION_TIME, dirent->createtime);
}
sftp_attributes_free(dirent);
listEntry(entry);
} // for ever
sftp_closedir(dp);
finished();
}
void sftpProtocol::mkdir(const QUrl &url, int permissions) {
qCDebug(KIO_SFTP_LOG) << "create directory: " << url;
if (!sftpLogin()) {
return;
}
if (url.path().isEmpty()) {
error(KIO::ERR_MALFORMED_URL, url.toDisplayString());
return;
}
const QString path = url.path();
const QByteArray path_c = path.toUtf8();
// Remove existing file or symlink, if requested.
if (metaData(QLatin1String("overwrite")) == QLatin1String("true")) {
qCDebug(KIO_SFTP_LOG) << "overwrite set, remove existing file or symlink: " << url;
sftp_unlink(mSftp, path_c.constData());
}
qCDebug(KIO_SFTP_LOG) << "Trying to create directory: " << path;
sftp_attributes sb = sftp_lstat(mSftp, path_c.constData());
if (sb == nullptr) {
if (sftp_mkdir(mSftp, path_c.constData(), 0777) < 0) {
reportError(url, sftp_get_error(mSftp));
sftp_attributes_free(sb);
return;
} else {
qCDebug(KIO_SFTP_LOG) << "Successfully created directory: " << url;
if (permissions != -1) {
chmod(url, permissions);
} else {
finished();
}
sftp_attributes_free(sb);
return;
}
}
if (sb->type == SSH_FILEXFER_TYPE_DIRECTORY) {
error(KIO::ERR_DIR_ALREADY_EXIST, path);
} else {
error(KIO::ERR_FILE_ALREADY_EXIST, path);
}
sftp_attributes_free(sb);
return;
}
void sftpProtocol::rename(const QUrl& src, const QUrl& dest, KIO::JobFlags flags) {
qCDebug(KIO_SFTP_LOG) << "rename " << src << " to " << dest << flags;
if (!sftpLogin()) {
return;
}
QByteArray qsrc = src.path().toUtf8();
QByteArray qdest = dest.path().toUtf8();
sftp_attributes sb = sftp_lstat(mSftp, qdest.constData());
if (sb != nullptr) {
if (!(flags & KIO::Overwrite)) {
if (sb->type == SSH_FILEXFER_TYPE_DIRECTORY) {
error(KIO::ERR_DIR_ALREADY_EXIST, dest.url());
} else {
error(KIO::ERR_FILE_ALREADY_EXIST, dest.url());
}
sftp_attributes_free(sb);
return;
}
// Delete the existing destination file/dir...
if (sb->type == SSH_FILEXFER_TYPE_DIRECTORY) {
if (sftp_rmdir(mSftp, qdest.constData()) < 0) {
reportError(dest, sftp_get_error(mSftp));
return;
}
} else {
if (sftp_unlink(mSftp, qdest.constData()) < 0) {
reportError(dest, sftp_get_error(mSftp));
return;
}
}
}
sftp_attributes_free(sb);
if (sftp_rename(mSftp, qsrc.constData(), qdest.constData()) < 0) {
reportError(dest, sftp_get_error(mSftp));
return;
}
finished();
}
void sftpProtocol::symlink(const QString &target, const QUrl &dest, KIO::JobFlags flags) {
qCDebug(KIO_SFTP_LOG) << "link " << target << "->" << dest
<< ", overwrite = " << (flags & KIO::Overwrite)
<< ", resume = " << (flags & KIO::Resume);
if (!sftpLogin()) {
return;
}
QByteArray t = target.toUtf8();
QByteArray d = dest.path().toUtf8();
bool failed = false;
if (sftp_symlink(mSftp, t.constData(), d.constData()) < 0) {
if (flags == KIO::Overwrite) {
sftp_attributes sb = sftp_lstat(mSftp, d.constData());
if (sb == nullptr) {
failed = true;
} else {
if (sftp_unlink(mSftp, d.constData()) < 0) {
failed = true;
} else {
if (sftp_symlink(mSftp, t.constData(), d.constData()) < 0) {
failed = true;
}
}
}
sftp_attributes_free(sb);
}
}
if (failed) {
reportError(dest, sftp_get_error(mSftp));
return;
}
finished();
}
void sftpProtocol::chmod(const QUrl& url, int permissions) {
qCDebug(KIO_SFTP_LOG) << "change permission of " << url << " to " << QString::number(permissions);
if (!sftpLogin()) {
return;
}
QByteArray path = url.path().toUtf8();
if (sftp_chmod(mSftp, path.constData(), permissions) < 0) {
reportError(url, sftp_get_error(mSftp));
return;
}
finished();
}
void sftpProtocol::del(const QUrl &url, bool isfile){
qCDebug(KIO_SFTP_LOG) << "deleting " << (isfile ? "file: " : "directory: ") << url;
if (!sftpLogin()) {
return;
}
QByteArray path = url.path().toUtf8();
if (isfile) {
if (sftp_unlink(mSftp, path.constData()) < 0) {
reportError(url, sftp_get_error(mSftp));
return;
}
} else {
if (sftp_rmdir(mSftp, path.constData()) < 0) {
reportError(url, sftp_get_error(mSftp));
return;
}
}
finished();
}
void sftpProtocol::slave_status() {
qCDebug(KIO_SFTP_LOG) << "connected to " << mHost << "?: " << mConnected;
slaveStatus((mConnected ? mHost : QString()), mConnected);
}
sftpProtocol::GetRequest::GetRequest(sftp_file file, sftp_attributes sb, ushort maxPendingRequests)
:mFile(file), mSb(sb), mMaxPendingRequests(maxPendingRequests) {
}
bool sftpProtocol::GetRequest::enqueueChunks() {
sftpProtocol::GetRequest::Request request;
qCDebug(KIO_SFTP_LOG) << "enqueueChunks";
while (pendingRequests.count() < mMaxPendingRequests) {
request.expectedLength = MAX_XFER_BUF_SIZE;
request.startOffset = mFile->offset;
request.id = sftp_async_read_begin(mFile, request.expectedLength);
if (request.id < 0) {
if (pendingRequests.isEmpty()) {
return false;
} else {
break;
}
}
pendingRequests.enqueue(request);
if (mFile->offset >= mSb->size) {
// Do not add any more chunks if the offset is larger than the given file size.
// However this is done after adding a request as the remote file size may
// have changed in the meantime.
break;
}
}
qCDebug(KIO_SFTP_LOG) << "enqueueChunks done" << QString::number(pendingRequests.size());
return true;
}
int sftpProtocol::GetRequest::readChunks(QByteArray &data) {
int totalRead = 0;
ssize_t bytesread = 0;
while (!pendingRequests.isEmpty()) {
sftpProtocol::GetRequest::Request &request = pendingRequests.head();
int dataSize = data.size() + request.expectedLength;
data.resize(dataSize);
if (data.size() < dataSize) {
// Could not allocate enough memory - skip current chunk
data.resize(dataSize - request.expectedLength);
break;
}
bytesread = sftp_async_read(mFile, data.data() + totalRead, request.expectedLength, request.id);
// qCDebug(KIO_SFTP_LOG) << "bytesread=" << QString::number(bytesread);
if (bytesread == 0 || bytesread == SSH_AGAIN) {
// Done reading or timeout
data.resize(data.size() - request.expectedLength);
if (bytesread == 0) {
pendingRequests.dequeue(); // This frees QByteArray &data!
}
break;
} else if (bytesread == SSH_ERROR) {
return -1;
}
totalRead += bytesread;
if (bytesread < request.expectedLength) {
int rc;
// If less data is read than expected - requeue the request
data.resize(data.size() - (request.expectedLength - bytesread));
// Modify current request
request.expectedLength -= bytesread;
request.startOffset += bytesread;
rc = sftp_seek64(mFile, request.startOffset);
if (rc < 0) {
// Failed to continue reading
return -1;
}
request.id = sftp_async_read_begin(mFile, request.expectedLength);
if (request.id < 0) {
// Failed to dispatch rerequest
return -1;
}
return totalRead;
}
pendingRequests.dequeue();
}
return totalRead;
}
sftpProtocol::GetRequest::~GetRequest() {
sftpProtocol::GetRequest::Request request;
char buf[MAX_XFER_BUF_SIZE];
// Remove pending reads to avoid memory leaks
while (!pendingRequests.isEmpty()) {
request = pendingRequests.dequeue();
sftp_async_read(mFile, buf, request.expectedLength, request.id);
}
// Close channel & free attributes
sftp_close(mFile);
sftp_attributes_free(mSb);
}
void sftpProtocol::requiresUserNameRedirection()
{
QUrl redirectUrl;
redirectUrl.setScheme( QLatin1String("sftp") );
redirectUrl.setUserName( mUsername );
redirectUrl.setPassword( mPassword );
redirectUrl.setHost( mHost );
if (mPort > 0 && mPort != DEFAULT_SFTP_PORT) {
redirectUrl.setPort( mPort );
}
qCDebug(KIO_SFTP_LOG) << "redirecting to" << redirectUrl;
redirection( redirectUrl );
}
bool sftpProtocol::sftpLogin()
{
const QString origUsername = mUsername;
openConnection();
qCDebug(KIO_SFTP_LOG) << "connected ?" << mConnected << "username: old=" << origUsername << "new=" << mUsername;
if (!origUsername.isEmpty() && origUsername != mUsername) {
requiresUserNameRedirection();
finished();
return false;
}
return mConnected;
}
void sftpProtocol::sftpSendWarning(int errorCode, const QString& url)
{
switch (errorCode) {
case -1:
warning(i18n( "Could not change permissions for\n%1", url));
break;
default:
break;
}
}
void sftpProtocol::clearPubKeyAuthInfo()
{
if (mPublicKeyAuthInfo) {
delete mPublicKeyAuthInfo;
mPublicKeyAuthInfo = nullptr;
}
}
void sftpProtocol::fileSystemFreeSpace(const QUrl& url)
{
qCDebug(KIO_SFTP_LOG) << "file system free space of" << url;
if (!sftpLogin()) {
return;
}
if (sftp_extension_supported(mSftp, "statvfs@openssh.com", "2") == 0) {
error(ERR_UNSUPPORTED_ACTION, QString());
return;
}
const QByteArray path = url.path().toUtf8();
sftp_statvfs_t statvfs = sftp_statvfs(mSftp, path.constData());
if (statvfs == nullptr) {
reportError(url, sftp_get_error(mSftp));
return;
}
setMetaData(QString::fromLatin1("total"), QString::number(statvfs->f_frsize * statvfs->f_blocks));
setMetaData(QString::fromLatin1("available"), QString::number(statvfs->f_frsize * statvfs->f_bavail));
sftp_statvfs_free(statvfs);
finished();
}
|