Preparatory work for allowing PSCP to work over SFTP as well as old-
[u/mdw/putty] / scp.c
1 /*
2 * scp.c - Scp (Secure Copy) client for PuTTY.
3 * Joris van Rantwijk, Simon Tatham
4 *
5 * This is mainly based on ssh-1.2.26/scp.c by Timo Rinne & Tatu Ylonen.
6 * They, in turn, used stuff from BSD rcp.
7 *
8 * Adaptations to enable connecting a GUI by L. Gunnarsson - Sept 2000
9 */
10
11 #include <windows.h>
12 #ifndef AUTO_WINSOCK
13 #ifdef WINSOCK_TWO
14 #include <winsock2.h>
15 #else
16 #include <winsock.h>
17 #endif
18 #endif
19 #include <stdlib.h>
20 #include <stdio.h>
21 #include <string.h>
22 #include <time.h>
23 #include <assert.h>
24 /* GUI Adaptation - Sept 2000 */
25 #include <winuser.h>
26 #include <winbase.h>
27
28 #define PUTTY_DO_GLOBALS
29 #include "putty.h"
30 #include "winstuff.h"
31 #include "storage.h"
32
33 #define TIME_POSIX_TO_WIN(t, ft) (*(LONGLONG*)&(ft) = \
34 ((LONGLONG) (t) + (LONGLONG) 11644473600) * (LONGLONG) 10000000)
35 #define TIME_WIN_TO_POSIX(ft, t) ((t) = (unsigned long) \
36 ((*(LONGLONG*)&(ft)) / (LONGLONG) 10000000 - (LONGLONG) 11644473600))
37
38 /* GUI Adaptation - Sept 2000 */
39 #define WM_APP_BASE 0x8000
40 #define WM_STD_OUT_CHAR ( WM_APP_BASE+400 )
41 #define WM_STD_ERR_CHAR ( WM_APP_BASE+401 )
42 #define WM_STATS_CHAR ( WM_APP_BASE+402 )
43 #define WM_STATS_SIZE ( WM_APP_BASE+403 )
44 #define WM_STATS_PERCENT ( WM_APP_BASE+404 )
45 #define WM_STATS_ELAPSED ( WM_APP_BASE+405 )
46 #define WM_RET_ERR_CNT ( WM_APP_BASE+406 )
47 #define WM_LS_RET_ERR_CNT ( WM_APP_BASE+407 )
48
49 static int list = 0;
50 static int verbose = 0;
51 static int recursive = 0;
52 static int preserve = 0;
53 static int targetshouldbedirectory = 0;
54 static int statistics = 1;
55 static int portnumber = 0;
56 static int prev_stats_len = 0;
57 static char *password = NULL;
58 static int errs = 0;
59 /* GUI Adaptation - Sept 2000 */
60 #define NAME_STR_MAX 2048
61 static char statname[NAME_STR_MAX + 1];
62 static unsigned long statsize = 0;
63 static int statperct = 0;
64 static unsigned long statelapsed = 0;
65 static int gui_mode = 0;
66 static char *gui_hwnd = NULL;
67
68 static void source(char *src);
69 static void rsource(char *src);
70 static void sink(char *targ, char *src);
71 /* GUI Adaptation - Sept 2000 */
72 static void tell_char(FILE * stream, char c);
73 static void tell_str(FILE * stream, char *str);
74 static void tell_user(FILE * stream, char *fmt, ...);
75 static void gui_update_stats(char *name, unsigned long size,
76 int percentage, unsigned long elapsed);
77
78 /*
79 * The maximum amount of queued data we accept before we stop and
80 * wait for the server to process some.
81 */
82 #define MAX_SCP_BUFSIZE 16384
83
84 void logevent(char *string)
85 {
86 }
87
88 void ldisc_send(char *buf, int len)
89 {
90 /*
91 * This is only here because of the calls to ldisc_send(NULL,
92 * 0) in ssh.c. Nothing in PSCP actually needs to use the ldisc
93 * as an ldisc. So if we get called with any real data, I want
94 * to know about it.
95 */
96 assert(len == 0);
97 }
98
99 void verify_ssh_host_key(char *host, int port, char *keytype,
100 char *keystr, char *fingerprint)
101 {
102 int ret;
103 HANDLE hin;
104 DWORD savemode, i;
105
106 static const char absentmsg[] =
107 "The server's host key is not cached in the registry. You\n"
108 "have no guarantee that the server is the computer you\n"
109 "think it is.\n"
110 "The server's key fingerprint is:\n"
111 "%s\n"
112 "If you trust this host, enter \"y\" to add the key to\n"
113 "PuTTY's cache and carry on connecting.\n"
114 "If you want to carry on connecting just once, without\n"
115 "adding the key to the cache, enter \"n\".\n"
116 "If you do not trust this host, press Return to abandon the\n"
117 "connection.\n"
118 "Store key in cache? (y/n) ";
119
120 static const char wrongmsg[] =
121 "WARNING - POTENTIAL SECURITY BREACH!\n"
122 "The server's host key does not match the one PuTTY has\n"
123 "cached in the registry. This means that either the\n"
124 "server administrator has changed the host key, or you\n"
125 "have actually connected to another computer pretending\n"
126 "to be the server.\n"
127 "The new key fingerprint is:\n"
128 "%s\n"
129 "If you were expecting this change and trust the new key,\n"
130 "enter \"y\" to update PuTTY's cache and continue connecting.\n"
131 "If you want to carry on connecting but without updating\n"
132 "the cache, enter \"n\".\n"
133 "If you want to abandon the connection completely, press\n"
134 "Return to cancel. Pressing Return is the ONLY guaranteed\n"
135 "safe choice.\n"
136 "Update cached key? (y/n, Return cancels connection) ";
137
138 static const char abandoned[] = "Connection abandoned.\n";
139
140 char line[32];
141
142 /*
143 * Verify the key against the registry.
144 */
145 ret = verify_host_key(host, port, keytype, keystr);
146
147 if (ret == 0) /* success - key matched OK */
148 return;
149
150 if (ret == 2) { /* key was different */
151 fprintf(stderr, wrongmsg, fingerprint);
152 fflush(stderr);
153 }
154 if (ret == 1) { /* key was absent */
155 fprintf(stderr, absentmsg, fingerprint);
156 fflush(stderr);
157 }
158
159 hin = GetStdHandle(STD_INPUT_HANDLE);
160 GetConsoleMode(hin, &savemode);
161 SetConsoleMode(hin, (savemode | ENABLE_ECHO_INPUT |
162 ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT));
163 ReadFile(hin, line, sizeof(line) - 1, &i, NULL);
164 SetConsoleMode(hin, savemode);
165
166 if (line[0] != '\0' && line[0] != '\r' && line[0] != '\n') {
167 if (line[0] == 'y' || line[0] == 'Y')
168 store_host_key(host, port, keytype, keystr);
169 } else {
170 fprintf(stderr, abandoned);
171 exit(0);
172 }
173 }
174
175 /*
176 * Ask whether the selected cipher is acceptable (since it was
177 * below the configured 'warn' threshold).
178 * cs: 0 = both ways, 1 = client->server, 2 = server->client
179 */
180 void askcipher(char *ciphername, int cs)
181 {
182 HANDLE hin;
183 DWORD savemode, i;
184
185 static const char msg[] =
186 "The first %scipher supported by the server is\n"
187 "%s, which is below the configured warning threshold.\n"
188 "Continue with connection? (y/n) ";
189 static const char abandoned[] = "Connection abandoned.\n";
190
191 char line[32];
192
193 fprintf(stderr, msg,
194 (cs == 0) ? "" :
195 (cs == 1) ? "client-to-server " :
196 "server-to-client ",
197 ciphername);
198 fflush(stderr);
199
200 hin = GetStdHandle(STD_INPUT_HANDLE);
201 GetConsoleMode(hin, &savemode);
202 SetConsoleMode(hin, (savemode | ENABLE_ECHO_INPUT |
203 ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT));
204 ReadFile(hin, line, sizeof(line) - 1, &i, NULL);
205 SetConsoleMode(hin, savemode);
206
207 if (line[0] == 'y' || line[0] == 'Y') {
208 return;
209 } else {
210 fprintf(stderr, abandoned);
211 exit(0);
212 }
213 }
214
215 /* GUI Adaptation - Sept 2000 */
216 static void send_msg(HWND h, UINT message, WPARAM wParam)
217 {
218 while (!PostMessage(h, message, wParam, 0))
219 SleepEx(1000, TRUE);
220 }
221
222 static void tell_char(FILE * stream, char c)
223 {
224 if (!gui_mode)
225 fputc(c, stream);
226 else {
227 unsigned int msg_id = WM_STD_OUT_CHAR;
228 if (stream == stderr)
229 msg_id = WM_STD_ERR_CHAR;
230 send_msg((HWND) atoi(gui_hwnd), msg_id, (WPARAM) c);
231 }
232 }
233
234 static void tell_str(FILE * stream, char *str)
235 {
236 unsigned int i;
237
238 for (i = 0; i < strlen(str); ++i)
239 tell_char(stream, str[i]);
240 }
241
242 static void tell_user(FILE * stream, char *fmt, ...)
243 {
244 char str[0x100]; /* Make the size big enough */
245 va_list ap;
246 va_start(ap, fmt);
247 vsprintf(str, fmt, ap);
248 va_end(ap);
249 strcat(str, "\n");
250 tell_str(stream, str);
251 }
252
253 static void gui_update_stats(char *name, unsigned long size,
254 int percentage, unsigned long elapsed)
255 {
256 unsigned int i;
257
258 if (strcmp(name, statname) != 0) {
259 for (i = 0; i < strlen(name); ++i)
260 send_msg((HWND) atoi(gui_hwnd), WM_STATS_CHAR,
261 (WPARAM) name[i]);
262 send_msg((HWND) atoi(gui_hwnd), WM_STATS_CHAR, (WPARAM) '\n');
263 strcpy(statname, name);
264 }
265 if (statsize != size) {
266 send_msg((HWND) atoi(gui_hwnd), WM_STATS_SIZE, (WPARAM) size);
267 statsize = size;
268 }
269 if (statelapsed != elapsed) {
270 send_msg((HWND) atoi(gui_hwnd), WM_STATS_ELAPSED,
271 (WPARAM) elapsed);
272 statelapsed = elapsed;
273 }
274 if (statperct != percentage) {
275 send_msg((HWND) atoi(gui_hwnd), WM_STATS_PERCENT,
276 (WPARAM) percentage);
277 statperct = percentage;
278 }
279 }
280
281 /*
282 * Print an error message and perform a fatal exit.
283 */
284 void fatalbox(char *fmt, ...)
285 {
286 char str[0x100]; /* Make the size big enough */
287 va_list ap;
288 va_start(ap, fmt);
289 strcpy(str, "Fatal: ");
290 vsprintf(str + strlen(str), fmt, ap);
291 va_end(ap);
292 strcat(str, "\n");
293 tell_str(stderr, str);
294 errs++;
295
296 if (gui_mode) {
297 unsigned int msg_id = WM_RET_ERR_CNT;
298 if (list)
299 msg_id = WM_LS_RET_ERR_CNT;
300 while (!PostMessage
301 ((HWND) atoi(gui_hwnd), msg_id, (WPARAM) errs,
302 0 /*lParam */ ))SleepEx(1000, TRUE);
303 }
304
305 exit(1);
306 }
307 void connection_fatal(char *fmt, ...)
308 {
309 char str[0x100]; /* Make the size big enough */
310 va_list ap;
311 va_start(ap, fmt);
312 strcpy(str, "Fatal: ");
313 vsprintf(str + strlen(str), fmt, ap);
314 va_end(ap);
315 strcat(str, "\n");
316 tell_str(stderr, str);
317 errs++;
318
319 if (gui_mode) {
320 unsigned int msg_id = WM_RET_ERR_CNT;
321 if (list)
322 msg_id = WM_LS_RET_ERR_CNT;
323 while (!PostMessage
324 ((HWND) atoi(gui_hwnd), msg_id, (WPARAM) errs,
325 0 /*lParam */ ))SleepEx(1000, TRUE);
326 }
327
328 exit(1);
329 }
330
331 /*
332 * Be told what socket we're supposed to be using.
333 */
334 static SOCKET scp_ssh_socket;
335 char *do_select(SOCKET skt, int startup)
336 {
337 if (startup)
338 scp_ssh_socket = skt;
339 else
340 scp_ssh_socket = INVALID_SOCKET;
341 return NULL;
342 }
343 extern int select_result(WPARAM, LPARAM);
344
345 /*
346 * Receive a block of data from the SSH link. Block until all data
347 * is available.
348 *
349 * To do this, we repeatedly call the SSH protocol module, with our
350 * own trap in from_backend() to catch the data that comes back. We
351 * do this until we have enough data.
352 */
353
354 static unsigned char *outptr; /* where to put the data */
355 static unsigned outlen; /* how much data required */
356 static unsigned char *pending = NULL; /* any spare data */
357 static unsigned pendlen = 0, pendsize = 0; /* length and phys. size of buffer */
358 int from_backend(int is_stderr, char *data, int datalen)
359 {
360 unsigned char *p = (unsigned char *) data;
361 unsigned len = (unsigned) datalen;
362
363 /*
364 * stderr data is just spouted to local stderr and otherwise
365 * ignored.
366 */
367 if (is_stderr) {
368 fwrite(data, 1, len, stderr);
369 return 0;
370 }
371
372 inbuf_head = 0;
373
374 /*
375 * If this is before the real session begins, just return.
376 */
377 if (!outptr)
378 return 0;
379
380 if (outlen > 0) {
381 unsigned used = outlen;
382 if (used > len)
383 used = len;
384 memcpy(outptr, p, used);
385 outptr += used;
386 outlen -= used;
387 p += used;
388 len -= used;
389 }
390
391 if (len > 0) {
392 if (pendsize < pendlen + len) {
393 pendsize = pendlen + len + 4096;
394 pending = (pending ? srealloc(pending, pendsize) :
395 smalloc(pendsize));
396 if (!pending)
397 fatalbox("Out of memory");
398 }
399 memcpy(pending + pendlen, p, len);
400 pendlen += len;
401 }
402
403 return 0;
404 }
405 static int scp_process_network_event(void)
406 {
407 fd_set readfds;
408
409 FD_ZERO(&readfds);
410 FD_SET(scp_ssh_socket, &readfds);
411 if (select(1, &readfds, NULL, NULL, NULL) < 0)
412 return 0; /* doom */
413 select_result((WPARAM) scp_ssh_socket, (LPARAM) FD_READ);
414 return 1;
415 }
416 static int ssh_scp_recv(unsigned char *buf, int len)
417 {
418 outptr = buf;
419 outlen = len;
420
421 /*
422 * See if the pending-input block contains some of what we
423 * need.
424 */
425 if (pendlen > 0) {
426 unsigned pendused = pendlen;
427 if (pendused > outlen)
428 pendused = outlen;
429 memcpy(outptr, pending, pendused);
430 memmove(pending, pending + pendused, pendlen - pendused);
431 outptr += pendused;
432 outlen -= pendused;
433 pendlen -= pendused;
434 if (pendlen == 0) {
435 pendsize = 0;
436 sfree(pending);
437 pending = NULL;
438 }
439 if (outlen == 0)
440 return len;
441 }
442
443 while (outlen > 0) {
444 if (!scp_process_network_event())
445 return 0; /* doom */
446 }
447
448 return len;
449 }
450
451 /*
452 * Loop through the ssh connection and authentication process.
453 */
454 static void ssh_scp_init(void)
455 {
456 if (scp_ssh_socket == INVALID_SOCKET)
457 return;
458 while (!back->sendok()) {
459 fd_set readfds;
460 FD_ZERO(&readfds);
461 FD_SET(scp_ssh_socket, &readfds);
462 if (select(1, &readfds, NULL, NULL, NULL) < 0)
463 return; /* doom */
464 select_result((WPARAM) scp_ssh_socket, (LPARAM) FD_READ);
465 }
466 }
467
468 /*
469 * Print an error message and exit after closing the SSH link.
470 */
471 static void bump(char *fmt, ...)
472 {
473 char str[0x100]; /* Make the size big enough */
474 va_list ap;
475 va_start(ap, fmt);
476 strcpy(str, "Fatal: ");
477 vsprintf(str + strlen(str), fmt, ap);
478 va_end(ap);
479 strcat(str, "\n");
480 tell_str(stderr, str);
481 errs++;
482
483 if (back != NULL && back->socket() != NULL) {
484 char ch;
485 back->special(TS_EOF);
486 ssh_scp_recv(&ch, 1);
487 }
488
489 if (gui_mode) {
490 unsigned int msg_id = WM_RET_ERR_CNT;
491 if (list)
492 msg_id = WM_LS_RET_ERR_CNT;
493 while (!PostMessage
494 ((HWND) atoi(gui_hwnd), msg_id, (WPARAM) errs,
495 0 /*lParam */ ))SleepEx(1000, TRUE);
496 }
497
498 exit(1);
499 }
500
501 static int get_line(const char *prompt, char *str, int maxlen, int is_pw)
502 {
503 HANDLE hin, hout;
504 DWORD savemode, newmode, i;
505
506 if (is_pw && password) {
507 static int tried_once = 0;
508
509 if (tried_once) {
510 return 0;
511 } else {
512 strncpy(str, password, maxlen);
513 str[maxlen - 1] = '\0';
514 tried_once = 1;
515 return 1;
516 }
517 }
518
519 /* GUI Adaptation - Sept 2000 */
520 if (gui_mode) {
521 if (maxlen > 0)
522 str[0] = '\0';
523 } else {
524 hin = GetStdHandle(STD_INPUT_HANDLE);
525 hout = GetStdHandle(STD_OUTPUT_HANDLE);
526 if (hin == INVALID_HANDLE_VALUE || hout == INVALID_HANDLE_VALUE)
527 bump("Cannot get standard input/output handles");
528
529 GetConsoleMode(hin, &savemode);
530 newmode = savemode | ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT;
531 if (is_pw)
532 newmode &= ~ENABLE_ECHO_INPUT;
533 else
534 newmode |= ENABLE_ECHO_INPUT;
535 SetConsoleMode(hin, newmode);
536
537 WriteFile(hout, prompt, strlen(prompt), &i, NULL);
538 ReadFile(hin, str, maxlen - 1, &i, NULL);
539
540 SetConsoleMode(hin, savemode);
541
542 if ((int) i > maxlen)
543 i = maxlen - 1;
544 else
545 i = i - 2;
546 str[i] = '\0';
547
548 if (is_pw)
549 WriteFile(hout, "\r\n", 2, &i, NULL);
550 }
551
552 return 1;
553 }
554
555 /*
556 * Open an SSH connection to user@host and execute cmd.
557 */
558 static void do_cmd(char *host, char *user, char *cmd)
559 {
560 char *err, *realhost;
561 DWORD namelen;
562
563 if (host == NULL || host[0] == '\0')
564 bump("Empty host name");
565
566 /* Try to load settings for this host */
567 do_defaults(host, &cfg);
568 if (cfg.host[0] == '\0') {
569 /* No settings for this host; use defaults */
570 do_defaults(NULL, &cfg);
571 strncpy(cfg.host, host, sizeof(cfg.host) - 1);
572 cfg.host[sizeof(cfg.host) - 1] = '\0';
573 cfg.port = 22;
574 }
575
576 /* Set username */
577 if (user != NULL && user[0] != '\0') {
578 strncpy(cfg.username, user, sizeof(cfg.username) - 1);
579 cfg.username[sizeof(cfg.username) - 1] = '\0';
580 } else if (cfg.username[0] == '\0') {
581 namelen = 0;
582 if (GetUserName(user, &namelen) == FALSE)
583 bump("Empty user name");
584 user = smalloc(namelen * sizeof(char));
585 GetUserName(user, &namelen);
586 if (verbose)
587 tell_user(stderr, "Guessing user name: %s", user);
588 strncpy(cfg.username, user, sizeof(cfg.username) - 1);
589 cfg.username[sizeof(cfg.username) - 1] = '\0';
590 free(user);
591 }
592
593 if (cfg.protocol != PROT_SSH)
594 cfg.port = 22;
595
596 if (portnumber)
597 cfg.port = portnumber;
598
599 strncpy(cfg.remote_cmd, cmd, sizeof(cfg.remote_cmd));
600 cfg.remote_cmd[sizeof(cfg.remote_cmd) - 1] = '\0';
601 cfg.nopty = TRUE;
602
603 back = &ssh_backend;
604
605 err = back->init(cfg.host, cfg.port, &realhost);
606 if (err != NULL)
607 bump("ssh_init: %s", err);
608 ssh_scp_init();
609 if (verbose && realhost != NULL)
610 tell_user(stderr, "Connected to %s\n", realhost);
611 sfree(realhost);
612 }
613
614 /*
615 * Update statistic information about current file.
616 */
617 static void print_stats(char *name, unsigned long size, unsigned long done,
618 time_t start, time_t now)
619 {
620 float ratebs;
621 unsigned long eta;
622 char etastr[10];
623 int pct;
624 int len;
625
626 /* GUI Adaptation - Sept 2000 */
627 if (gui_mode)
628 gui_update_stats(name, size, (int) (100 * (done * 1.0 / size)),
629 (unsigned long) difftime(now, start));
630 else {
631 if (now > start)
632 ratebs = (float) done / (now - start);
633 else
634 ratebs = (float) done;
635
636 if (ratebs < 1.0)
637 eta = size - done;
638 else
639 eta = (unsigned long) ((size - done) / ratebs);
640 sprintf(etastr, "%02ld:%02ld:%02ld",
641 eta / 3600, (eta % 3600) / 60, eta % 60);
642
643 pct = (int) (100.0 * (float) done / size);
644
645 len = printf("\r%-25.25s | %10ld kB | %5.1f kB/s | ETA: %8s | %3d%%",
646 name, done / 1024, ratebs / 1024.0, etastr, pct);
647 if (len < prev_stats_len)
648 printf("%*s", prev_stats_len - len, "");
649 prev_stats_len = len;
650
651 if (done == size)
652 printf("\n");
653 }
654 }
655
656 /*
657 * Find a colon in str and return a pointer to the colon.
658 * This is used to separate hostname from filename.
659 */
660 static char *colon(char *str)
661 {
662 /* We ignore a leading colon, since the hostname cannot be
663 empty. We also ignore a colon as second character because
664 of filenames like f:myfile.txt. */
665 if (str[0] == '\0' || str[0] == ':' || str[1] == ':')
666 return (NULL);
667 while (*str != '\0' && *str != ':' && *str != '/' && *str != '\\')
668 str++;
669 if (*str == ':')
670 return (str);
671 else
672 return (NULL);
673 }
674
675 /*
676 * Wait for a response from the other side.
677 * Return 0 if ok, -1 if error.
678 */
679 static int response(void)
680 {
681 char ch, resp, rbuf[2048];
682 int p;
683
684 if (ssh_scp_recv(&resp, 1) <= 0)
685 bump("Lost connection");
686
687 p = 0;
688 switch (resp) {
689 case 0: /* ok */
690 return (0);
691 default:
692 rbuf[p++] = resp;
693 /* fallthrough */
694 case 1: /* error */
695 case 2: /* fatal error */
696 do {
697 if (ssh_scp_recv(&ch, 1) <= 0)
698 bump("Protocol error: Lost connection");
699 rbuf[p++] = ch;
700 } while (p < sizeof(rbuf) && ch != '\n');
701 rbuf[p - 1] = '\0';
702 if (resp == 1)
703 tell_user(stderr, "%s\n", rbuf);
704 else
705 bump("%s", rbuf);
706 errs++;
707 return (-1);
708 }
709 }
710
711 /* ----------------------------------------------------------------------
712 * Helper routines that contain the actual SCP protocol elements,
713 * so they can be switched to use SFTP.
714 */
715
716 int scp_send_errmsg(char *str)
717 {
718 back->send("\001", 1); /* scp protocol error prefix */
719 back->send(str, strlen(str));
720 return 0; /* can't fail */
721 }
722
723 int scp_send_filetimes(unsigned long mtime, unsigned long atime)
724 {
725 char buf[80];
726 sprintf(buf, "T%lu 0 %lu 0\n", mtime, atime);
727 back->send(buf, strlen(buf));
728 return response();
729 }
730
731 int scp_send_filename(char *name, unsigned long size, int modes)
732 {
733 char buf[40];
734 sprintf(buf, "C%04o %lu ", modes, size);
735 back->send(buf, strlen(buf));
736 back->send(name, strlen(name));
737 back->send("\n", 1);
738 return response();
739 }
740
741 int scp_send_filedata(char *data, int len)
742 {
743 int bufsize = back->send(data, len);
744
745 /*
746 * If the network transfer is backing up - that is, the remote
747 * site is not accepting data as fast as we can produce it -
748 * then we must loop on network events until we have space in
749 * the buffer again.
750 */
751 while (bufsize > MAX_SCP_BUFSIZE) {
752 if (!scp_process_network_event())
753 return 1;
754 bufsize = back->sendbuffer();
755 }
756
757 return 0;
758 }
759
760 int scp_send_finish(void)
761 {
762 back->send("", 1);
763 return response();
764 }
765
766 int scp_send_dirname(char *name, int modes)
767 {
768 char buf[40];
769 sprintf(buf, "D%04o 0 ", modes);
770 back->send(buf, strlen(buf));
771 back->send(name, strlen(name));
772 back->send("\n", 1);
773 return response();
774 }
775
776 int scp_send_enddir(void)
777 {
778 back->send("E\n", 2);
779 return response();
780 }
781
782 int scp_sink_init(void)
783 {
784 back->send("", 1);
785 return 0;
786 }
787
788 #define SCP_SINK_FILE 1
789 #define SCP_SINK_DIR 2
790 #define SCP_SINK_ENDDIR 3
791 struct scp_sink_action {
792 int action; /* FILE, DIR, ENDDIR */
793 char *buf; /* will need freeing after use */
794 char *name; /* filename or dirname (not ENDDIR) */
795 int mode; /* access mode (not ENDDIR) */
796 unsigned long size; /* file size (not ENDDIR) */
797 int settime; /* 1 if atime and mtime are filled */
798 unsigned long atime, mtime; /* access times for the file */
799 };
800
801 int scp_get_sink_action(struct scp_sink_action *act)
802 {
803 int done = 0;
804 int i, bufsize;
805 int action;
806 char ch;
807
808 act->settime = 0;
809 act->buf = NULL;
810 bufsize = 0;
811
812 while (!done) {
813 if (ssh_scp_recv(&ch, 1) <= 0)
814 return 1;
815 if (ch == '\n')
816 bump("Protocol error: Unexpected newline");
817 i = 0;
818 action = ch;
819 do {
820 if (ssh_scp_recv(&ch, 1) <= 0)
821 bump("Lost connection");
822 if (i >= bufsize) {
823 bufsize = i + 128;
824 act->buf = srealloc(act->buf, bufsize);
825 }
826 act->buf[i++] = ch;
827 } while (ch != '\n');
828 act->buf[i - 1] = '\0';
829 switch (action) {
830 case '\01': /* error */
831 tell_user(stderr, "%s\n", act->buf);
832 errs++;
833 continue; /* go round again */
834 case '\02': /* fatal error */
835 bump("%s", act->buf);
836 case 'E':
837 back->send("", 1);
838 act->action = SCP_SINK_ENDDIR;
839 return 0;
840 case 'T':
841 if (sscanf(act->buf, "%ld %*d %ld %*d",
842 &act->mtime, &act->atime) == 2) {
843 act->settime = 1;
844 back->send("", 1);
845 continue; /* go round again */
846 }
847 bump("Protocol error: Illegal time format");
848 case 'C':
849 case 'D':
850 act->action = (action == 'C' ? SCP_SINK_FILE : SCP_SINK_DIR);
851 break;
852 default:
853 bump("Protocol error: Expected control record");
854 }
855 /*
856 * We will go round this loop only once, unless we hit
857 * `continue' above.
858 */
859 done = 1;
860 }
861
862 /*
863 * If we get here, we must have seen SCP_SINK_FILE or
864 * SCP_SINK_DIR.
865 */
866 if (sscanf(act->buf, "%o %lu %n", &act->mode, &act->size, &i) != 2)
867 bump("Protocol error: Illegal file descriptor format");
868 act->name = act->buf + i;
869 return 0;
870 }
871
872 int scp_accept_filexfer(void)
873 {
874 back->send("", 1);
875 return 0; /* can't fail */
876 }
877
878 int scp_recv_filedata(char *data, int len)
879 {
880 return ssh_scp_recv(data, len);
881 }
882
883 int scp_finish_filerecv(void)
884 {
885 back->send("", 1);
886 return response();
887 }
888
889 /* ----------------------------------------------------------------------
890 * Send an error message to the other side and to the screen.
891 * Increment error counter.
892 */
893 static void run_err(const char *fmt, ...)
894 {
895 char str[2048];
896 va_list ap;
897 va_start(ap, fmt);
898 errs++;
899 strcpy(str, "scp: ");
900 vsprintf(str + strlen(str), fmt, ap);
901 strcat(str, "\n");
902 scp_send_errmsg(str);
903 tell_user(stderr, "%s", str);
904 va_end(ap);
905 }
906
907 /*
908 * Execute the source part of the SCP protocol.
909 */
910 static void source(char *src)
911 {
912 char buf[2048];
913 unsigned long size;
914 char *last;
915 HANDLE f;
916 DWORD attr;
917 unsigned long i;
918 unsigned long stat_bytes;
919 time_t stat_starttime, stat_lasttime;
920
921 attr = GetFileAttributes(src);
922 if (attr == (DWORD) - 1) {
923 run_err("%s: No such file or directory", src);
924 return;
925 }
926
927 if ((attr & FILE_ATTRIBUTE_DIRECTORY) != 0) {
928 if (recursive) {
929 /*
930 * Avoid . and .. directories.
931 */
932 char *p;
933 p = strrchr(src, '/');
934 if (!p)
935 p = strrchr(src, '\\');
936 if (!p)
937 p = src;
938 else
939 p++;
940 if (!strcmp(p, ".") || !strcmp(p, ".."))
941 /* skip . and .. */ ;
942 else
943 rsource(src);
944 } else {
945 run_err("%s: not a regular file", src);
946 }
947 return;
948 }
949
950 if ((last = strrchr(src, '/')) == NULL)
951 last = src;
952 else
953 last++;
954 if (strrchr(last, '\\') != NULL)
955 last = strrchr(last, '\\') + 1;
956 if (last == src && strchr(src, ':') != NULL)
957 last = strchr(src, ':') + 1;
958
959 f = CreateFile(src, GENERIC_READ, FILE_SHARE_READ, NULL,
960 OPEN_EXISTING, 0, 0);
961 if (f == INVALID_HANDLE_VALUE) {
962 run_err("%s: Cannot open file", src);
963 return;
964 }
965
966 if (preserve) {
967 FILETIME actime, wrtime;
968 unsigned long mtime, atime;
969 GetFileTime(f, NULL, &actime, &wrtime);
970 TIME_WIN_TO_POSIX(actime, atime);
971 TIME_WIN_TO_POSIX(wrtime, mtime);
972 if (scp_send_filetimes(mtime, atime))
973 return;
974 }
975
976 size = GetFileSize(f, NULL);
977 if (verbose)
978 tell_user(stderr, "Sending file %s, size=%lu", last, size);
979 if (scp_send_filename(last, size, 0644))
980 return;
981
982 stat_bytes = 0;
983 stat_starttime = time(NULL);
984 stat_lasttime = 0;
985
986 for (i = 0; i < size; i += 4096) {
987 char transbuf[4096];
988 DWORD j, k = 4096;
989
990 if (i + k > size)
991 k = size - i;
992 if (!ReadFile(f, transbuf, k, &j, NULL) || j != k) {
993 if (statistics)
994 printf("\n");
995 bump("%s: Read error", src);
996 }
997 if (scp_send_filedata(transbuf, k))
998 bump("%s: Network error occurred", src);
999
1000 if (statistics) {
1001 stat_bytes += k;
1002 if (time(NULL) != stat_lasttime || i + k == size) {
1003 stat_lasttime = time(NULL);
1004 print_stats(last, size, stat_bytes,
1005 stat_starttime, stat_lasttime);
1006 }
1007 }
1008
1009 }
1010 CloseHandle(f);
1011
1012 (void) scp_send_finish();
1013 }
1014
1015 /*
1016 * Recursively send the contents of a directory.
1017 */
1018 static void rsource(char *src)
1019 {
1020 char buf[2048];
1021 char *last;
1022 HANDLE dir;
1023 WIN32_FIND_DATA fdat;
1024 int ok;
1025
1026 if ((last = strrchr(src, '/')) == NULL)
1027 last = src;
1028 else
1029 last++;
1030 if (strrchr(last, '\\') != NULL)
1031 last = strrchr(last, '\\') + 1;
1032 if (last == src && strchr(src, ':') != NULL)
1033 last = strchr(src, ':') + 1;
1034
1035 /* maybe send filetime */
1036
1037 if (verbose)
1038 tell_user(stderr, "Entering directory: %s", last);
1039 if (scp_send_dirname(last, 0755))
1040 return;
1041
1042 sprintf(buf, "%s/*", src);
1043 dir = FindFirstFile(buf, &fdat);
1044 ok = (dir != INVALID_HANDLE_VALUE);
1045 while (ok) {
1046 if (strcmp(fdat.cFileName, ".") == 0 ||
1047 strcmp(fdat.cFileName, "..") == 0) {
1048 } else if (strlen(src) + 1 + strlen(fdat.cFileName) >= sizeof(buf)) {
1049 run_err("%s/%s: Name too long", src, fdat.cFileName);
1050 } else {
1051 sprintf(buf, "%s/%s", src, fdat.cFileName);
1052 source(buf);
1053 }
1054 ok = FindNextFile(dir, &fdat);
1055 }
1056 FindClose(dir);
1057
1058 (void) scp_send_enddir();
1059 }
1060
1061 /*
1062 * Execute the sink part of the SCP protocol.
1063 */
1064 static void sink(char *targ, char *src)
1065 {
1066 char buf[2048];
1067 char namebuf[2048];
1068 char ch;
1069 int targisdir = 0;
1070 int settime;
1071 int exists;
1072 DWORD attr;
1073 HANDLE f;
1074 unsigned long received;
1075 int wrerror = 0;
1076 unsigned long stat_bytes;
1077 time_t stat_starttime, stat_lasttime;
1078 char *stat_name;
1079
1080 attr = GetFileAttributes(targ);
1081 if (attr != (DWORD) - 1 && (attr & FILE_ATTRIBUTE_DIRECTORY) != 0)
1082 targisdir = 1;
1083
1084 if (targetshouldbedirectory && !targisdir)
1085 bump("%s: Not a directory", targ);
1086
1087 scp_sink_init();
1088 while (1) {
1089 struct scp_sink_action act;
1090 if (scp_get_sink_action(&act))
1091 return;
1092
1093 if (act.action == SCP_SINK_ENDDIR)
1094 return;
1095 /* Security fix: ensure the file ends up where we asked for it. */
1096 if (targisdir) {
1097 char t[2048];
1098 char *p;
1099 strcpy(t, targ);
1100 if (targ[0] != '\0')
1101 strcat(t, "/");
1102 p = act.name + strlen(act.name);
1103 while (p > act.name && p[-1] != '/' && p[-1] != '\\')
1104 p--;
1105 strcat(t, p);
1106 strcpy(namebuf, t);
1107 } else {
1108 strcpy(namebuf, targ);
1109 }
1110 attr = GetFileAttributes(namebuf);
1111 exists = (attr != (DWORD) - 1);
1112
1113 if (act.action == SCP_SINK_DIR) {
1114 if (exists && (attr & FILE_ATTRIBUTE_DIRECTORY) == 0) {
1115 run_err("%s: Not a directory", namebuf);
1116 continue;
1117 }
1118 if (!exists) {
1119 if (!CreateDirectory(namebuf, NULL)) {
1120 run_err("%s: Cannot create directory", namebuf);
1121 continue;
1122 }
1123 }
1124 sink(namebuf, NULL);
1125 /* can we set the timestamp for directories ? */
1126 continue;
1127 }
1128
1129 f = CreateFile(namebuf, GENERIC_WRITE, 0, NULL,
1130 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
1131 if (f == INVALID_HANDLE_VALUE) {
1132 run_err("%s: Cannot create file", namebuf);
1133 continue;
1134 }
1135
1136 if (scp_accept_filexfer())
1137 return;
1138
1139 stat_bytes = 0;
1140 stat_starttime = time(NULL);
1141 stat_lasttime = 0;
1142 if ((stat_name = strrchr(namebuf, '/')) == NULL)
1143 stat_name = namebuf;
1144 else
1145 stat_name++;
1146 if (strrchr(stat_name, '\\') != NULL)
1147 stat_name = strrchr(stat_name, '\\') + 1;
1148
1149 received = 0;
1150 while (received < act.size) {
1151 char transbuf[4096];
1152 DWORD blksize, read, written;
1153 blksize = 4096;
1154 if (blksize > act.size - received)
1155 blksize = act.size - received;
1156 read = scp_recv_filedata(transbuf, blksize);
1157 if (read <= 0)
1158 bump("Lost connection");
1159 if (wrerror)
1160 continue;
1161 if (!WriteFile(f, transbuf, read, &written, NULL) ||
1162 written != read) {
1163 wrerror = 1;
1164 /* FIXME: in sftp we can actually abort the transfer */
1165 if (statistics)
1166 printf("\r%-25.25s | %50s\n",
1167 stat_name,
1168 "Write error.. waiting for end of file");
1169 continue;
1170 }
1171 if (statistics) {
1172 stat_bytes += read;
1173 if (time(NULL) > stat_lasttime ||
1174 received + read == act.size) {
1175 stat_lasttime = time(NULL);
1176 print_stats(stat_name, act.size, stat_bytes,
1177 stat_starttime, stat_lasttime);
1178 }
1179 }
1180 received += read;
1181 }
1182 if (act.settime) {
1183 FILETIME actime, wrtime;
1184 TIME_POSIX_TO_WIN(act.atime, actime);
1185 TIME_POSIX_TO_WIN(act.mtime, wrtime);
1186 SetFileTime(f, NULL, &actime, &wrtime);
1187 }
1188
1189 CloseHandle(f);
1190 if (wrerror) {
1191 run_err("%s: Write error", namebuf);
1192 continue;
1193 }
1194 (void) scp_finish_filerecv();
1195 }
1196 }
1197
1198 /*
1199 * We will copy local files to a remote server.
1200 */
1201 static void toremote(int argc, char *argv[])
1202 {
1203 char *src, *targ, *host, *user;
1204 char *cmd;
1205 int i;
1206
1207 targ = argv[argc - 1];
1208
1209 /* Separate host from filename */
1210 host = targ;
1211 targ = colon(targ);
1212 if (targ == NULL)
1213 bump("targ == NULL in toremote()");
1214 *targ++ = '\0';
1215 if (*targ == '\0')
1216 targ = ".";
1217 /* Substitute "." for emtpy target */
1218
1219 /* Separate host and username */
1220 user = host;
1221 host = strrchr(host, '@');
1222 if (host == NULL) {
1223 host = user;
1224 user = NULL;
1225 } else {
1226 *host++ = '\0';
1227 if (*user == '\0')
1228 user = NULL;
1229 }
1230
1231 if (argc == 2) {
1232 /* Find out if the source filespec covers multiple files
1233 if so, we should set the targetshouldbedirectory flag */
1234 HANDLE fh;
1235 WIN32_FIND_DATA fdat;
1236 if (colon(argv[0]) != NULL)
1237 bump("%s: Remote to remote not supported", argv[0]);
1238 fh = FindFirstFile(argv[0], &fdat);
1239 if (fh == INVALID_HANDLE_VALUE)
1240 bump("%s: No such file or directory\n", argv[0]);
1241 if (FindNextFile(fh, &fdat))
1242 targetshouldbedirectory = 1;
1243 FindClose(fh);
1244 }
1245
1246 cmd = smalloc(strlen(targ) + 100);
1247 sprintf(cmd, "scp%s%s%s%s -t %s",
1248 verbose ? " -v" : "",
1249 recursive ? " -r" : "",
1250 preserve ? " -p" : "",
1251 targetshouldbedirectory ? " -d" : "", targ);
1252 do_cmd(host, user, cmd);
1253 sfree(cmd);
1254
1255 (void) response();
1256
1257 for (i = 0; i < argc - 1; i++) {
1258 HANDLE dir;
1259 WIN32_FIND_DATA fdat;
1260 src = argv[i];
1261 if (colon(src) != NULL) {
1262 tell_user(stderr, "%s: Remote to remote not supported\n", src);
1263 errs++;
1264 continue;
1265 }
1266 dir = FindFirstFile(src, &fdat);
1267 if (dir == INVALID_HANDLE_VALUE) {
1268 run_err("%s: No such file or directory", src);
1269 continue;
1270 }
1271 do {
1272 char *last;
1273 char namebuf[2048];
1274 /*
1275 * Ensure that . and .. are never matched by wildcards,
1276 * but only by deliberate action.
1277 */
1278 if (!strcmp(fdat.cFileName, ".") ||
1279 !strcmp(fdat.cFileName, "..")) {
1280 /*
1281 * Find*File has returned a special dir. We require
1282 * that _either_ `src' ends in a backslash followed
1283 * by that string, _or_ `src' is precisely that
1284 * string.
1285 */
1286 int len = strlen(src), dlen = strlen(fdat.cFileName);
1287 if (len == dlen && !strcmp(src, fdat.cFileName)) {
1288 /* ok */ ;
1289 } else if (len > dlen + 1 && src[len - dlen - 1] == '\\' &&
1290 !strcmp(src + len - dlen, fdat.cFileName)) {
1291 /* ok */ ;
1292 } else
1293 continue; /* ignore this one */
1294 }
1295 if (strlen(src) + strlen(fdat.cFileName) >= sizeof(namebuf)) {
1296 tell_user(stderr, "%s: Name too long", src);
1297 continue;
1298 }
1299 strcpy(namebuf, src);
1300 if ((last = strrchr(namebuf, '/')) == NULL)
1301 last = namebuf;
1302 else
1303 last++;
1304 if (strrchr(last, '\\') != NULL)
1305 last = strrchr(last, '\\') + 1;
1306 if (last == namebuf && strrchr(namebuf, ':') != NULL)
1307 last = strchr(namebuf, ':') + 1;
1308 strcpy(last, fdat.cFileName);
1309 source(namebuf);
1310 } while (FindNextFile(dir, &fdat));
1311 FindClose(dir);
1312 }
1313 }
1314
1315 /*
1316 * We will copy files from a remote server to the local machine.
1317 */
1318 static void tolocal(int argc, char *argv[])
1319 {
1320 char *src, *targ, *host, *user;
1321 char *cmd;
1322
1323 if (argc != 2)
1324 bump("More than one remote source not supported");
1325
1326 src = argv[0];
1327 targ = argv[1];
1328
1329 /* Separate host from filename */
1330 host = src;
1331 src = colon(src);
1332 if (src == NULL)
1333 bump("Local to local copy not supported");
1334 *src++ = '\0';
1335 if (*src == '\0')
1336 src = ".";
1337 /* Substitute "." for empty filename */
1338
1339 /* Separate username and hostname */
1340 user = host;
1341 host = strrchr(host, '@');
1342 if (host == NULL) {
1343 host = user;
1344 user = NULL;
1345 } else {
1346 *host++ = '\0';
1347 if (*user == '\0')
1348 user = NULL;
1349 }
1350
1351 cmd = smalloc(strlen(src) + 100);
1352 sprintf(cmd, "scp%s%s%s%s -f %s",
1353 verbose ? " -v" : "",
1354 recursive ? " -r" : "",
1355 preserve ? " -p" : "",
1356 targetshouldbedirectory ? " -d" : "", src);
1357 do_cmd(host, user, cmd);
1358 sfree(cmd);
1359
1360 sink(targ, src);
1361 }
1362
1363 /*
1364 * We will issue a list command to get a remote directory.
1365 */
1366 static void get_dir_list(int argc, char *argv[])
1367 {
1368 char *src, *host, *user;
1369 char *cmd, *p, *q;
1370 char c;
1371
1372 src = argv[0];
1373
1374 /* Separate host from filename */
1375 host = src;
1376 src = colon(src);
1377 if (src == NULL)
1378 bump("Local to local copy not supported");
1379 *src++ = '\0';
1380 if (*src == '\0')
1381 src = ".";
1382 /* Substitute "." for empty filename */
1383
1384 /* Separate username and hostname */
1385 user = host;
1386 host = strrchr(host, '@');
1387 if (host == NULL) {
1388 host = user;
1389 user = NULL;
1390 } else {
1391 *host++ = '\0';
1392 if (*user == '\0')
1393 user = NULL;
1394 }
1395
1396 cmd = smalloc(4 * strlen(src) + 100);
1397 strcpy(cmd, "ls -la '");
1398 p = cmd + strlen(cmd);
1399 for (q = src; *q; q++) {
1400 if (*q == '\'') {
1401 *p++ = '\'';
1402 *p++ = '\\';
1403 *p++ = '\'';
1404 *p++ = '\'';
1405 } else {
1406 *p++ = *q;
1407 }
1408 }
1409 *p++ = '\'';
1410 *p = '\0';
1411
1412 do_cmd(host, user, cmd);
1413 sfree(cmd);
1414
1415 while (ssh_scp_recv(&c, 1) > 0)
1416 tell_char(stdout, c);
1417 }
1418
1419 /*
1420 * Initialize the Win$ock driver.
1421 */
1422 static void init_winsock(void)
1423 {
1424 WORD winsock_ver;
1425 WSADATA wsadata;
1426
1427 winsock_ver = MAKEWORD(1, 1);
1428 if (WSAStartup(winsock_ver, &wsadata))
1429 bump("Unable to initialise WinSock");
1430 if (LOBYTE(wsadata.wVersion) != 1 || HIBYTE(wsadata.wVersion) != 1)
1431 bump("WinSock version is incompatible with 1.1");
1432 }
1433
1434 /*
1435 * Short description of parameters.
1436 */
1437 static void usage(void)
1438 {
1439 printf("PuTTY Secure Copy client\n");
1440 printf("%s\n", ver);
1441 printf("Usage: pscp [options] [user@]host:source target\n");
1442 printf
1443 (" pscp [options] source [source...] [user@]host:target\n");
1444 printf(" pscp [options] -ls user@host:filespec\n");
1445 printf("Options:\n");
1446 printf(" -p preserve file attributes\n");
1447 printf(" -q quiet, don't show statistics\n");
1448 printf(" -r copy directories recursively\n");
1449 printf(" -v show verbose messages\n");
1450 printf(" -P port connect to specified port\n");
1451 printf(" -pw passw login with specified password\n");
1452 #if 0
1453 /*
1454 * -gui is an internal option, used by GUI front ends to get
1455 * pscp to pass progress reports back to them. It's not an
1456 * ordinary user-accessible option, so it shouldn't be part of
1457 * the command-line help. The only people who need to know
1458 * about it are programmers, and they can read the source.
1459 */
1460 printf
1461 (" -gui hWnd GUI mode with the windows handle for receiving messages\n");
1462 #endif
1463 exit(1);
1464 }
1465
1466 /*
1467 * Main program (no, really?)
1468 */
1469 int main(int argc, char *argv[])
1470 {
1471 int i;
1472
1473 default_protocol = PROT_TELNET;
1474
1475 flags = FLAG_STDERR;
1476 ssh_get_line = &get_line;
1477 init_winsock();
1478 sk_init();
1479
1480 for (i = 1; i < argc; i++) {
1481 if (argv[i][0] != '-')
1482 break;
1483 if (strcmp(argv[i], "-v") == 0)
1484 verbose = 1, flags |= FLAG_VERBOSE;
1485 else if (strcmp(argv[i], "-r") == 0)
1486 recursive = 1;
1487 else if (strcmp(argv[i], "-p") == 0)
1488 preserve = 1;
1489 else if (strcmp(argv[i], "-q") == 0)
1490 statistics = 0;
1491 else if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "-?") == 0)
1492 usage();
1493 else if (strcmp(argv[i], "-P") == 0 && i + 1 < argc)
1494 portnumber = atoi(argv[++i]);
1495 else if (strcmp(argv[i], "-pw") == 0 && i + 1 < argc)
1496 password = argv[++i];
1497 else if (strcmp(argv[i], "-gui") == 0 && i + 1 < argc) {
1498 gui_hwnd = argv[++i];
1499 gui_mode = 1;
1500 } else if (strcmp(argv[i], "-ls") == 0)
1501 list = 1;
1502 else if (strcmp(argv[i], "--") == 0) {
1503 i++;
1504 break;
1505 } else
1506 usage();
1507 }
1508 argc -= i;
1509 argv += i;
1510 back = NULL;
1511
1512 if (list) {
1513 if (argc != 1)
1514 usage();
1515 get_dir_list(argc, argv);
1516
1517 } else {
1518
1519 if (argc < 2)
1520 usage();
1521 if (argc > 2)
1522 targetshouldbedirectory = 1;
1523
1524 if (colon(argv[argc - 1]) != NULL)
1525 toremote(argc, argv);
1526 else
1527 tolocal(argc, argv);
1528 }
1529
1530 if (back != NULL && back->socket() != NULL) {
1531 char ch;
1532 back->special(TS_EOF);
1533 ssh_scp_recv(&ch, 1);
1534 }
1535 WSACleanup();
1536 random_save_seed();
1537
1538 /* GUI Adaptation - August 2000 */
1539 if (gui_mode) {
1540 unsigned int msg_id = WM_RET_ERR_CNT;
1541 if (list)
1542 msg_id = WM_LS_RET_ERR_CNT;
1543 while (!PostMessage
1544 ((HWND) atoi(gui_hwnd), msg_id, (WPARAM) errs,
1545 0 /*lParam */ ))SleepEx(1000, TRUE);
1546 }
1547 return (errs == 0 ? 0 : 1);
1548 }
1549
1550 /* end */