Diff

Differences From Artifact [c1baa849cb]:

To Artifact [d47c38f008]:


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
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







-
+







-
+

















-
+

-
+







/*
 * Loop through the ssh connection and authentication process.
 */
static void ssh_scp_init(void)
{
    if (scp_ssh_socket == INVALID_SOCKET)
	return;
    while (!back->sendok()) {
    while (!back->sendok(backhandle)) {
	fd_set readfds;
	FD_ZERO(&readfds);
	FD_SET(scp_ssh_socket, &readfds);
	if (select(1, &readfds, NULL, NULL, NULL) < 0)
	    return;		       /* doom */
	select_result((WPARAM) scp_ssh_socket, (LPARAM) FD_READ);
    }
    using_sftp = !ssh_fallback_cmd;
    using_sftp = !ssh_fallback_cmd(backhandle);
}

/*
 *  Print an error message and exit after closing the SSH link.
 */
static void bump(char *fmt, ...)
{
    char str[0x100];		       /* Make the size big enough */
    va_list ap;
    va_start(ap, fmt);
    strcpy(str, "Fatal: ");
    vsprintf(str + strlen(str), fmt, ap);
    va_end(ap);
    strcat(str, "\n");
    tell_str(stderr, str);
    errs++;

    if (back != NULL && back->socket() != NULL) {
    if (back != NULL && back->socket(backhandle) != NULL) {
	char ch;
	back->special(TS_EOF);
	back->special(backhandle, TS_EOF);
	ssh_scp_recv(&ch, 1);
    }

    if (gui_mode) {
	unsigned int msg_id = WM_RET_ERR_CNT;
	if (list)
	    msg_id = WM_LS_RET_ERR_CNT;
561
562
563
564
565
566
567
568

569
570
571
572
573
574
575
561
562
563
564
565
566
567

568
569
570
571
572
573
574
575







-
+







    cfg.ssh_subsys = TRUE;
    cfg.remote_cmd_ptr2 = cmd;
    cfg.ssh_subsys2 = FALSE;
    cfg.nopty = TRUE;

    back = &ssh_backend;

    err = back->init(NULL, cfg.host, cfg.port, &realhost, 0);
    err = back->init(NULL, &backhandle, cfg.host, cfg.port, &realhost, 0);
    if (err != NULL)
	bump("ssh_init: %s", err);
    ssh_scp_init();
    if (verbose && realhost != NULL)
	tell_user(stderr, "Connected to %s\n", realhost);
    sfree(realhost);
}
708
709
710
711
712
713
714
715

716
717
718
719
720
721
722
708
709
710
711
712
713
714

715
716
717
718
719
720
721
722







-
+








int sftp_recvdata(char *buf, int len)
{
    return ssh_scp_recv(buf, len);
}
int sftp_senddata(char *buf, int len)
{
    back->send((unsigned char *) buf, len);
    back->send(backhandle, (unsigned char *) buf, len);
    return 1;
}

/* ----------------------------------------------------------------------
 * sftp-based replacement for the hacky `pscp -ls'.
 */
static int sftp_ls_compare(const void *av, const void *bv)
820
821
822
823
824
825
826
827

828
829
830
831
832
833
834
820
821
822
823
824
825
826

827
828
829
830
831
832
833
834







-
+







	 * directory.
	 */
	struct fxp_attrs attrs;

	if (!fxp_init()) {
	    tell_user(stderr, "unable to initialise SFTP: %s", fxp_error());
	    errs++;
	    return 1;
	    return;
	}

	if (!fxp_stat(target, &attrs) ||
	    !(attrs.flags & SSH_FILEXFER_ATTR_PERMISSIONS))
	    scp_sftp_targetisdir = 0;
	else
	    scp_sftp_targetisdir = (attrs.permissions & 0040000) != 0;
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
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







-
-
+
+














-
+







}

int scp_send_errmsg(char *str)
{
    if (using_sftp) {
	/* do nothing; we never need to send our errors to the server */
    } else {
	back->send("\001", 1);	       /* scp protocol error prefix */
	back->send(str, strlen(str));
	back->send(backhandle, "\001", 1);/* scp protocol error prefix */
	back->send(backhandle, str, strlen(str));
    }
    return 0;			       /* can't fail */
}

int scp_send_filetimes(unsigned long mtime, unsigned long atime)
{
    if (using_sftp) {
	scp_sftp_mtime = mtime;
	scp_sftp_atime = atime;
	scp_has_times = 1;
	return 0;
    } else {
	char buf[80];
	sprintf(buf, "T%lu 0 %lu 0\n", mtime, atime);
	back->send(buf, strlen(buf));
	back->send(backhandle, buf, strlen(buf));
	return response();
    }
}

int scp_send_filename(char *name, unsigned long size, int modes)
{
    if (using_sftp) {
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
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







-
-
-
+
+
+


















-
+










-
+







	}
	scp_sftp_fileoffset = uint64_make(0, 0);
	sfree(fullname);
	return 0;
    } else {
	char buf[40];
	sprintf(buf, "C%04o %lu ", modes, size);
	back->send(buf, strlen(buf));
	back->send(name, strlen(name));
	back->send("\n", 1);
	back->send(backhandle, buf, strlen(buf));
	back->send(backhandle, name, strlen(name));
	back->send(backhandle, "\n", 1);
	return response();
    }
}

int scp_send_filedata(char *data, int len)
{
    if (using_sftp) {
	if (!scp_sftp_filehandle) {
	    return 1;
	}
	if (!fxp_write(scp_sftp_filehandle, data, scp_sftp_fileoffset, len)) {
	    tell_user(stderr, "error while writing: %s\n", fxp_error());
	    errs++;
	    return 1;
	}
	scp_sftp_fileoffset = uint64_add32(scp_sftp_fileoffset, len);
	return 0;
    } else {
	int bufsize = back->send(data, len);
	int bufsize = back->send(backhandle, data, len);

	/*
	 * If the network transfer is backing up - that is, the
	 * remote site is not accepting data as fast as we can
	 * produce it - then we must loop on network events until
	 * we have space in the buffer again.
	 */
	while (bufsize > MAX_SCP_BUFSIZE) {
	    if (!scp_process_network_event())
		return 1;
	    bufsize = back->sendbuffer();
	    bufsize = back->sendbuffer(backhandle);
	}

	return 0;
    }
}

int scp_send_finish(void)
949
950
951
952
953
954
955
956

957
958
959
960
961
962
963
949
950
951
952
953
954
955

956
957
958
959
960
961
962
963







-
+







		errs++;
	    }
	}
	fxp_close(scp_sftp_filehandle);
	scp_has_times = 0;
	return 0;
    } else {
	back->send("", 1);
	back->send(backhandle, "", 1);
	return response();
    }
}

char *scp_save_remotepath(void)
{
    if (using_sftp)
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
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







-
-
-
+
+
+










-
+








	scp_sftp_remotepath = fullname;

	return 0;
    } else {
	char buf[40];
	sprintf(buf, "D%04o 0 ", modes);
	back->send(buf, strlen(buf));
	back->send(name, strlen(name));
	back->send("\n", 1);
	back->send(backhandle, buf, strlen(buf));
	back->send(backhandle, name, strlen(name));
	back->send(backhandle, "\n", 1);
	return response();
    }
}

int scp_send_enddir(void)
{
    if (using_sftp) {
	sfree(scp_sftp_remotepath);
	return 0;
    } else {
	back->send("E\n", 2);
	back->send(backhandle, "E\n", 2);
	return response();
    }
}

/*
 * Yes, I know; I have an scp_sink_setup _and_ an scp_sink_init.
 * That's bad. The difference is that scp_sink_setup is called once
1114
1115
1116
1117
1118
1119
1120
1121

1122
1123
1124
1125
1126
1127
1128
1114
1115
1116
1117
1118
1119
1120

1121
1122
1123
1124
1125
1126
1127
1128







-
+







    }
    return 0;
}

int scp_sink_init(void)
{
    if (!using_sftp) {
	back->send("", 1);
	back->send(backhandle, "", 1);
    }
    return 0;
}

#define SCP_SINK_FILE   1
#define SCP_SINK_DIR    2
#define SCP_SINK_ENDDIR 3
1400
1401
1402
1403
1404
1405
1406
1407

1408
1409
1410
1411
1412
1413
1414

1415
1416
1417
1418
1419
1420
1421
1400
1401
1402
1403
1404
1405
1406

1407
1408
1409
1410
1411
1412
1413

1414
1415
1416
1417
1418
1419
1420
1421







-
+






-
+







	      case '\01':		       /* error */
		tell_user(stderr, "%s\n", act->buf);
		errs++;
		continue;		       /* go round again */
	      case '\02':		       /* fatal error */
		bump("%s", act->buf);
	      case 'E':
		back->send("", 1);
		back->send(backhandle, "", 1);
		act->action = SCP_SINK_ENDDIR;
		return 0;
	      case 'T':
		if (sscanf(act->buf, "%ld %*d %ld %*d",
			   &act->mtime, &act->atime) == 2) {
		    act->settime = 1;
		    back->send("", 1);
		    back->send(backhandle, "", 1);
		    continue;	       /* go round again */
		}
		bump("Protocol error: Illegal time format");
	      case 'C':
	      case 'D':
		act->action = (action == 'C' ? SCP_SINK_FILE : SCP_SINK_DIR);
		break;
1451
1452
1453
1454
1455
1456
1457
1458

1459
1460
1461
1462
1463
1464
1465
1451
1452
1453
1454
1455
1456
1457

1458
1459
1460
1461
1462
1463
1464
1465







-
+







	    errs++;
	    return 1;
	}
	scp_sftp_fileoffset = uint64_make(0, 0);
	sfree(scp_sftp_currentname);
	return 0;
    } else {
	back->send("", 1);
	back->send(backhandle, "", 1);
	return 0;		       /* can't fail */
    }
}

int scp_recv_filedata(char *data, int len)
{
    if (using_sftp) {
1483
1484
1485
1486
1487
1488
1489
1490

1491
1492
1493
1494
1495
1496
1497
1483
1484
1485
1486
1487
1488
1489

1490
1491
1492
1493
1494
1495
1496
1497







-
+








int scp_finish_filerecv(void)
{
    if (using_sftp) {
	fxp_close(scp_sftp_filehandle);
	return 0;
    } else {
	back->send("", 1);
	back->send(backhandle, "", 1);
	return response();
    }
}

/* ----------------------------------------------------------------------
 *  Send an error message to the other side and to the screen.
 *  Increment error counter.
2230
2231
2232
2233
2234
2235
2236
2237

2238
2239

2240
2241
2242
2243
2244
2245
2246
2230
2231
2232
2233
2234
2235
2236

2237
2238

2239
2240
2241
2242
2243
2244
2245
2246







-
+

-
+








	if (colon(argv[argc - 1]) != NULL)
	    toremote(argc, argv);
	else
	    tolocal(argc, argv);
    }

    if (back != NULL && back->socket() != NULL) {
    if (back != NULL && back->socket(backhandle) != NULL) {
	char ch;
	back->special(TS_EOF);
	back->special(backhandle, TS_EOF);
	ssh_scp_recv(&ch, 1);
    }
    WSACleanup();
    random_save_seed();

    /* GUI Adaptation - August 2000 */
    if (gui_mode) {