| ︙ | | |
314
315
316
317
318
319
320
321
322
323
324
325
326
327
|
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
|
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
|
back->special(backhandle, TS_EOF);
sent_eof = TRUE;
ssh_scp_recv((unsigned char *) &ch, 1);
}
cleanup_exit(1);
}
/*
* Wait for the reply to a single SFTP request. Parallels the same
* function in psftp.c (but isn't centralised into sftp.c because the
* latter module handles SFTP only and shouldn't assume that SFTP is
* the only thing going on by calling connection_fatal).
*/
struct sftp_packet *sftp_wait_for_reply(struct sftp_request *req)
{
struct sftp_packet *pktin;
struct sftp_request *rreq;
sftp_register(req);
pktin = sftp_recv();
if (pktin == NULL)
connection_fatal(NULL, "did not receive SFTP response packet "
"from server");
rreq = sftp_find_request(pktin);
if (rreq != req)
connection_fatal(NULL, "unable to understand SFTP response packet "
"from server: %s", fxp_error());
return pktin;
}
/*
* Open an SSH connection to user@host and execute cmd.
*/
static void do_cmd(char *host, char *user, char *cmd)
{
const char *err;
|
| ︙ | | |
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
|
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
|
-
+
-
-
+
+
-
-
+
-
-
+
+
-
-
+
|
}
void scp_sftp_listdir(char *dirname)
{
struct fxp_handle *dirh;
struct fxp_names *names;
struct fxp_name *ournames;
struct sftp_packet *pktin;
struct sftp_request *req, *rreq;
struct sftp_request *req;
int nnames, namesize;
int i;
if (!fxp_init()) {
tell_user(stderr, "unable to initialise SFTP: %s", fxp_error());
errs++;
return;
}
printf("Listing directory %s\n", dirname);
sftp_register(req = fxp_opendir_send(dirname));
rreq = sftp_find_request(pktin = sftp_recv());
req = fxp_opendir_send(dirname);
pktin = sftp_wait_for_reply(req);
assert(rreq == req);
dirh = fxp_opendir_recv(pktin, rreq);
dirh = fxp_opendir_recv(pktin, req);
if (dirh == NULL) {
printf("Unable to open %s: %s\n", dirname, fxp_error());
} else {
nnames = namesize = 0;
ournames = NULL;
while (1) {
sftp_register(req = fxp_readdir_send(dirh));
rreq = sftp_find_request(pktin = sftp_recv());
req = fxp_readdir_send(dirh);
pktin = sftp_wait_for_reply(req);
assert(rreq == req);
names = fxp_readdir_recv(pktin, rreq);
names = fxp_readdir_recv(pktin, req);
if (names == NULL) {
if (fxp_error_type() == SSH_FX_EOF)
break;
printf("Reading directory %s: %s\n", dirname, fxp_error());
break;
}
|
| ︙ | | |
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
|
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
|
-
-
+
+
-
-
+
|
}
for (i = 0; i < names->nnames; i++)
ournames[nnames++] = names->names[i];
names->nnames = 0; /* prevent free_names */
fxp_free_names(names);
}
sftp_register(req = fxp_close_send(dirh));
rreq = sftp_find_request(pktin = sftp_recv());
req = fxp_close_send(dirh);
pktin = sftp_wait_for_reply(req);
assert(rreq == req);
fxp_close_recv(pktin, rreq);
fxp_close_recv(pktin, req);
/*
* Now we have our filenames. Sort them by actual file
* name, and then output the longname parts.
*/
qsort(ournames, nnames, sizeof(*ournames), sftp_ls_compare);
|
| ︙ | | |
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
|
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
|
-
+
-
-
+
+
-
-
+
|
{
if (using_sftp) {
/*
* Find out whether the target filespec is in fact a
* directory.
*/
struct sftp_packet *pktin;
struct sftp_request *req, *rreq;
struct sftp_request *req;
struct fxp_attrs attrs;
int ret;
if (!fxp_init()) {
tell_user(stderr, "unable to initialise SFTP: %s", fxp_error());
errs++;
return 1;
}
sftp_register(req = fxp_stat_send(target));
rreq = sftp_find_request(pktin = sftp_recv());
req = fxp_stat_send(target);
pktin = sftp_wait_for_reply(req);
assert(rreq == req);
ret = fxp_stat_recv(pktin, rreq, &attrs);
ret = fxp_stat_recv(pktin, req, &attrs);
if (!ret || !(attrs.flags & SSH_FILEXFER_ATTR_PERMISSIONS))
scp_sftp_targetisdir = 0;
else
scp_sftp_targetisdir = (attrs.permissions & 0040000) != 0;
if (shouldbedir && !scp_sftp_targetisdir) {
|
| ︙ | | |
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
|
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
|
-
+
-
-
-
-
+
+
+
+
-
-
+
|
}
int scp_send_filename(char *name, uint64 size, int permissions)
{
if (using_sftp) {
char *fullname;
struct sftp_packet *pktin;
struct sftp_request *req, *rreq;
struct sftp_request *req;
struct fxp_attrs attrs;
if (scp_sftp_targetisdir) {
fullname = dupcat(scp_sftp_remotepath, "/", name, NULL);
} else {
fullname = dupstr(scp_sftp_remotepath);
}
attrs.flags = 0;
PUT_PERMISSIONS(attrs, permissions);
sftp_register(req = fxp_open_send(fullname, SSH_FXF_WRITE |
SSH_FXF_CREAT | SSH_FXF_TRUNC,
&attrs));
rreq = sftp_find_request(pktin = sftp_recv());
req = fxp_open_send(fullname,
SSH_FXF_WRITE | SSH_FXF_CREAT | SSH_FXF_TRUNC,
&attrs);
pktin = sftp_wait_for_reply(req);
assert(rreq == req);
scp_sftp_filehandle = fxp_open_recv(pktin, rreq);
scp_sftp_filehandle = fxp_open_recv(pktin, req);
if (!scp_sftp_filehandle) {
tell_user(stderr, "pscp: unable to open %s: %s",
fullname, fxp_error());
errs++;
return 1;
}
|
| ︙ | | |
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
|
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
|
-
+
|
if (!scp_sftp_filehandle) {
return 1;
}
while (!xfer_upload_ready(scp_sftp_xfer)) {
pktin = sftp_recv();
ret = xfer_upload_gotpkt(scp_sftp_xfer, pktin);
if (!ret) {
if (ret <= 0) {
tell_user(stderr, "error while writing: %s\n", fxp_error());
errs++;
return 1;
}
}
xfer_upload_data(scp_sftp_xfer, data, len);
|
| ︙ | | |
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
|
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
|
-
+
-
+
+
+
+
+
+
-
-
+
+
-
-
+
-
-
+
+
-
-
+
|
}
int scp_send_finish(void)
{
if (using_sftp) {
struct fxp_attrs attrs;
struct sftp_packet *pktin;
struct sftp_request *req, *rreq;
struct sftp_request *req;
int ret;
while (!xfer_done(scp_sftp_xfer)) {
pktin = sftp_recv();
xfer_upload_gotpkt(scp_sftp_xfer, pktin);
ret = xfer_upload_gotpkt(scp_sftp_xfer, pktin);
if (ret <= 0) {
tell_user(stderr, "error while writing: %s\n", fxp_error());
errs++;
return 1;
}
}
xfer_cleanup(scp_sftp_xfer);
if (!scp_sftp_filehandle) {
return 1;
}
if (scp_has_times) {
attrs.flags = SSH_FILEXFER_ATTR_ACMODTIME;
attrs.atime = scp_sftp_atime;
attrs.mtime = scp_sftp_mtime;
sftp_register(req = fxp_fsetstat_send(scp_sftp_filehandle, attrs));
rreq = sftp_find_request(pktin = sftp_recv());
req = fxp_fsetstat_send(scp_sftp_filehandle, attrs);
pktin = sftp_wait_for_reply(req);
assert(rreq == req);
ret = fxp_fsetstat_recv(pktin, rreq);
ret = fxp_fsetstat_recv(pktin, req);
if (!ret) {
tell_user(stderr, "unable to set file times: %s\n", fxp_error());
errs++;
}
}
sftp_register(req = fxp_close_send(scp_sftp_filehandle));
rreq = sftp_find_request(pktin = sftp_recv());
req = fxp_close_send(scp_sftp_filehandle);
pktin = sftp_wait_for_reply(req);
assert(rreq == req);
fxp_close_recv(pktin, rreq);
fxp_close_recv(pktin, req);
scp_has_times = 0;
return 0;
} else {
back->send(backhandle, "", 1);
return response();
}
}
|
| ︙ | | |
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
|
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
|
-
+
-
-
+
+
-
-
+
-
-
+
+
-
-
+
|
int scp_send_dirname(char *name, int modes)
{
if (using_sftp) {
char *fullname;
char const *err;
struct fxp_attrs attrs;
struct sftp_packet *pktin;
struct sftp_request *req, *rreq;
struct sftp_request *req;
int ret;
if (scp_sftp_targetisdir) {
fullname = dupcat(scp_sftp_remotepath, "/", name, NULL);
} else {
fullname = dupstr(scp_sftp_remotepath);
}
/*
* We don't worry about whether we managed to create the
* directory, because if it exists already it's OK just to
* use it. Instead, we will stat it afterwards, and if it
* exists and is a directory we will assume we were either
* successful or it didn't matter.
*/
sftp_register(req = fxp_mkdir_send(fullname));
rreq = sftp_find_request(pktin = sftp_recv());
req = fxp_mkdir_send(fullname);
pktin = sftp_wait_for_reply(req);
assert(rreq == req);
ret = fxp_mkdir_recv(pktin, rreq);
ret = fxp_mkdir_recv(pktin, req);
if (!ret)
err = fxp_error();
else
err = "server reported no error";
sftp_register(req = fxp_stat_send(fullname));
rreq = sftp_find_request(pktin = sftp_recv());
req = fxp_stat_send(fullname);
pktin = sftp_wait_for_reply(req);
assert(rreq == req);
ret = fxp_stat_recv(pktin, rreq, &attrs);
ret = fxp_stat_recv(pktin, req, &attrs);
if (!ret || !(attrs.flags & SSH_FILEXFER_ATTR_PERMISSIONS) ||
!(attrs.permissions & 0040000)) {
tell_user(stderr, "unable to create directory %s: %s",
fullname, err);
errs++;
return 1;
|
| ︙ | | |
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
|
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
|
-
+
|
int scp_get_sink_action(struct scp_sink_action *act)
{
if (using_sftp) {
char *fname;
int must_free_fname;
struct fxp_attrs attrs;
struct sftp_packet *pktin;
struct sftp_request *req, *rreq;
struct sftp_request *req;
int ret;
if (!scp_sftp_dirstack_head) {
if (!scp_sftp_donethistarget) {
/*
* Simple case: we are only dealing with one file.
*/
|
| ︙ | | |
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
|
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
|
-
-
+
+
-
-
+
|
}
}
/*
* Now we have a filename. Stat it, and see if it's a file
* or a directory.
*/
sftp_register(req = fxp_stat_send(fname));
rreq = sftp_find_request(pktin = sftp_recv());
req = fxp_stat_send(fname);
pktin = sftp_wait_for_reply(req);
assert(rreq == req);
ret = fxp_stat_recv(pktin, rreq, &attrs);
ret = fxp_stat_recv(pktin, req, &attrs);
if (!ret || !(attrs.flags & SSH_FILEXFER_ATTR_PERMISSIONS)) {
tell_user(stderr, "unable to identify %s: %s", fname,
ret ? "file type not supplied" : fxp_error());
errs++;
return 1;
}
|
| ︙ | | |
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
|
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
|
-
-
+
+
-
-
+
-
-
+
+
-
-
+
|
* matching them against a wildcard if present.
*
* If targetisdir is _already_ set (meaning we're
* already in the middle of going through another such
* list), we must push the other (target,namelist) pair
* on a stack.
*/
sftp_register(req = fxp_opendir_send(fname));
rreq = sftp_find_request(pktin = sftp_recv());
req = fxp_opendir_send(fname);
pktin = sftp_wait_for_reply(req);
assert(rreq == req);
dirhandle = fxp_opendir_recv(pktin, rreq);
dirhandle = fxp_opendir_recv(pktin, req);
if (!dirhandle) {
tell_user(stderr, "scp: unable to open directory %s: %s",
fname, fxp_error());
if (must_free_fname) sfree(fname);
errs++;
return 1;
}
nnames = namesize = 0;
ournames = NULL;
while (1) {
int i;
sftp_register(req = fxp_readdir_send(dirhandle));
rreq = sftp_find_request(pktin = sftp_recv());
req = fxp_readdir_send(dirhandle);
pktin = sftp_wait_for_reply(req);
assert(rreq == req);
names = fxp_readdir_recv(pktin, rreq);
names = fxp_readdir_recv(pktin, req);
if (names == NULL) {
if (fxp_error_type() == SSH_FX_EOF)
break;
tell_user(stderr, "scp: reading directory %s: %s\n",
fname, fxp_error());
if (must_free_fname) sfree(fname);
|
| ︙ | | |
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
|
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
|
-
-
+
+
-
-
+
|
names->names[i].filename);
} else
ournames[nnames++] = names->names[i];
}
names->nnames = 0; /* prevent free_names */
fxp_free_names(names);
}
sftp_register(req = fxp_close_send(dirhandle));
rreq = sftp_find_request(pktin = sftp_recv());
req = fxp_close_send(dirhandle);
pktin = sftp_wait_for_reply(req);
assert(rreq == req);
fxp_close_recv(pktin, rreq);
fxp_close_recv(pktin, req);
newitem = snew(struct scp_sftp_dirstack);
newitem->next = scp_sftp_dirstack_head;
newitem->names = ournames;
newitem->namepos = 0;
newitem->namelen = nnames;
if (must_free_fname)
|
| ︙ | | |
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
|
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
|
-
+
-
+
-
-
+
-
-
+
|
}
}
int scp_accept_filexfer(void)
{
if (using_sftp) {
struct sftp_packet *pktin;
struct sftp_request *req, *rreq;
struct sftp_request *req;
sftp_register(req = fxp_open_send(scp_sftp_currentname, SSH_FXF_READ,
req = fxp_open_send(scp_sftp_currentname, SSH_FXF_READ, NULL);
NULL));
rreq = sftp_find_request(pktin = sftp_recv());
pktin = sftp_wait_for_reply(req);
assert(rreq == req);
scp_sftp_filehandle = fxp_open_recv(pktin, rreq);
scp_sftp_filehandle = fxp_open_recv(pktin, req);
if (!scp_sftp_filehandle) {
tell_user(stderr, "pscp: unable to open %s: %s",
scp_sftp_currentname, fxp_error());
errs++;
return 1;
}
|
| ︙ | | |
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
|
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
|
-
-
+
|
struct sftp_packet *pktin;
int ret, actuallen;
void *vbuf;
xfer_download_queue(scp_sftp_xfer);
pktin = sftp_recv();
ret = xfer_download_gotpkt(scp_sftp_xfer, pktin);
if (ret < 0) {
if (ret <= 0) {
tell_user(stderr, "pscp: error while reading: %s", fxp_error());
errs++;
return -1;
}
if (xfer_download_data(scp_sftp_xfer, &vbuf, &actuallen)) {
/*
|
| ︙ | | |
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
|
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
|
-
+
-
+
-
+
+
+
+
+
+
-
-
+
+
-
-
+
|
}
}
int scp_finish_filerecv(void)
{
if (using_sftp) {
struct sftp_packet *pktin;
struct sftp_request *req, *rreq;
struct sftp_request *req;
/*
* Ensure that xfer_done() will work correctly, so we can
* clean up any outstanding requests from the file
* transfer.
*/
xfer_set_error(scp_sftp_xfer);
while (!xfer_done(scp_sftp_xfer)) {
void *vbuf;
int len;
int ret, len;
pktin = sftp_recv();
xfer_download_gotpkt(scp_sftp_xfer, pktin);
ret = xfer_download_gotpkt(scp_sftp_xfer, pktin);
if (ret <= 0) {
tell_user(stderr, "pscp: error while reading: %s", fxp_error());
errs++;
return -1;
}
if (xfer_download_data(scp_sftp_xfer, &vbuf, &len))
sfree(vbuf);
}
xfer_cleanup(scp_sftp_xfer);
sftp_register(req = fxp_close_send(scp_sftp_filehandle));
rreq = sftp_find_request(pktin = sftp_recv());
req = fxp_close_send(scp_sftp_filehandle);
pktin = sftp_wait_for_reply(req);
assert(rreq == req);
fxp_close_recv(pktin, rreq);
fxp_close_recv(pktin, req);
return 0;
} else {
back->send(backhandle, "", 1);
return response();
}
}
|
| ︙ | | |