Implement OpenSSH-compatible RSA key fingerprints and use them throughout
[u/mdw/putty] / scp.c
CommitLineData
07d9aa13 1/*
2 * scp.c - Scp (Secure Copy) client for PuTTY.
fb09bf1c 3 * Joris van Rantwijk, Simon Tatham
07d9aa13 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.
cc87246d 7 *
8 * Adaptations to enable connecting a GUI by L. Gunnarsson - Sept 2000
07d9aa13 9 */
10
07d9aa13 11#include <windows.h>
4d331a77 12#ifndef AUTO_WINSOCK
13#ifdef WINSOCK_TWO
14#include <winsock2.h>
15#else
07d9aa13 16#include <winsock.h>
4d331a77 17#endif
18#endif
07d9aa13 19#include <stdlib.h>
20#include <stdio.h>
21#include <string.h>
22#include <time.h>
cc87246d 23/* GUI Adaptation - Sept 2000 */
24#include <winuser.h>
25#include <winbase.h>
07d9aa13 26
27#define PUTTY_DO_GLOBALS
28#include "putty.h"
29#include "scp.h"
30
07d9aa13 31#define TIME_POSIX_TO_WIN(t, ft) (*(LONGLONG*)&(ft) = \
c51a56e2 32 ((LONGLONG) (t) + (LONGLONG) 11644473600) * (LONGLONG) 10000000)
07d9aa13 33#define TIME_WIN_TO_POSIX(ft, t) ((t) = (unsigned long) \
c51a56e2 34 ((*(LONGLONG*)&(ft)) / (LONGLONG) 10000000 - (LONGLONG) 11644473600))
07d9aa13 35
cc87246d 36/* GUI Adaptation - Sept 2000 */
37#define WM_APP_BASE 0x8000
38#define WM_STD_OUT_CHAR ( WM_APP_BASE+400 )
39#define WM_STD_ERR_CHAR ( WM_APP_BASE+401 )
40#define WM_STATS_CHAR ( WM_APP_BASE+402 )
41#define WM_STATS_SIZE ( WM_APP_BASE+403 )
42#define WM_STATS_PERCENT ( WM_APP_BASE+404 )
43#define WM_STATS_ELAPSED ( WM_APP_BASE+405 )
44#define WM_RET_ERR_CNT ( WM_APP_BASE+406 )
45#define WM_LS_RET_ERR_CNT ( WM_APP_BASE+407 )
46
fb09bf1c 47static int verbose = 0;
07d9aa13 48static int recursive = 0;
49static int preserve = 0;
50static int targetshouldbedirectory = 0;
51static int statistics = 1;
b8a19193 52static int portnumber = 0;
53static char *password = NULL;
07d9aa13 54static int errs = 0;
55static int connection_open = 0;
cc87246d 56/* GUI Adaptation - Sept 2000 */
57#define NAME_STR_MAX 2048
58static char statname[NAME_STR_MAX+1];
59static unsigned long statsize = 0;
60static int statperct = 0;
61static time_t statelapsed = 0;
62static int gui_mode = 0;
63static char *gui_hwnd = NULL;
07d9aa13 64
65static void source(char *src);
66static void rsource(char *src);
67static void sink(char *targ);
cc87246d 68/* GUI Adaptation - Sept 2000 */
69static void tell_char(FILE *stream, char c);
70static void tell_str(FILE *stream, char *str);
71static void tell_user(FILE *stream, char *fmt, ...);
72static void send_char_msg(unsigned int msg_id, char c);
73static void send_str_msg(unsigned int msg_id, char *str);
74static void gui_update_stats(char *name, unsigned long size, int percentage, time_t elapsed);
07d9aa13 75
07d9aa13 76/*
989b10e9 77 * These functions are needed to link with other modules, but
78 * (should) never get called.
fb09bf1c 79 */
989b10e9 80void term_out(void) { abort(); }
81void begin_session(void) { }
82void write_clip (void *data, int len) { }
83void term_deselect(void) { }
fb09bf1c 84
cc87246d 85/* GUI Adaptation - Sept 2000 */
86void send_msg(HWND h, UINT message, WPARAM wParam)
87{
88 while (!PostMessage( h, message, wParam, 0))
89 SleepEx(1000,TRUE);
90}
91
92void tell_char(FILE *stream, char c)
93{
94 if (!gui_mode)
95 fputc(c, stream);
96 else
97 {
98 unsigned int msg_id = WM_STD_OUT_CHAR;
99 if (stream = stderr) msg_id = WM_STD_ERR_CHAR;
100 send_msg( (HWND)atoi(gui_hwnd), msg_id, (WPARAM)c );
101 }
102}
103
104void tell_str(FILE *stream, char *str)
105{
106 unsigned int i;
107
108 for( i = 0; i < strlen(str); ++i )
109 tell_char(stream, str[i]);
110}
111
112void tell_user(FILE *stream, char *fmt, ...)
113{
114 char str[0x100]; /* Make the size big enough */
115 va_list ap;
116 va_start(ap, fmt);
117 vsprintf(str, fmt, ap);
118 va_end(ap);
119 strcat(str, "\n");
120 tell_str(stream, str);
121}
122
123void gui_update_stats(char *name, unsigned long size, int percentage, time_t elapsed)
124{
125 unsigned int i;
126
127 if (strcmp(name,statname) != 0)
128 {
129 for( i = 0; i < strlen(name); ++i )
130 send_msg( (HWND)atoi(gui_hwnd), WM_STATS_CHAR, (WPARAM)name[i]);
131 send_msg( (HWND)atoi(gui_hwnd), WM_STATS_CHAR, (WPARAM)'\n' );
132 strcpy(statname,name);
133 }
134 if (statsize != size)
135 {
136 send_msg( (HWND)atoi(gui_hwnd), WM_STATS_SIZE, (WPARAM)size );
137 statsize = size;
138 }
139 if (statelapsed != elapsed)
140 {
141 send_msg( (HWND)atoi(gui_hwnd), WM_STATS_ELAPSED, (WPARAM)elapsed );
142 statelapsed = elapsed;
143 }
144 if (statperct != percentage)
145 {
146 send_msg( (HWND)atoi(gui_hwnd), WM_STATS_PERCENT, (WPARAM)percentage );
147 statperct = percentage;
148 }
149}
150
fb09bf1c 151/*
07d9aa13 152 * Print an error message and perform a fatal exit.
153 */
154void fatalbox(char *fmt, ...)
155{
cc87246d 156 char str[0x100]; /* Make the size big enough */
c51a56e2 157 va_list ap;
158 va_start(ap, fmt);
cc87246d 159 strcpy(str, "Fatal:");
160 vsprintf(str+strlen(str), fmt, ap);
c51a56e2 161 va_end(ap);
cc87246d 162 strcat(str, "\n");
163 tell_str(stderr, str);
164
c51a56e2 165 exit(1);
07d9aa13 166}
8d5de777 167void connection_fatal(char *fmt, ...)
168{
169 char str[0x100]; /* Make the size big enough */
170 va_list ap;
171 va_start(ap, fmt);
172 strcpy(str, "Fatal:");
173 vsprintf(str+strlen(str), fmt, ap);
174 va_end(ap);
175 strcat(str, "\n");
176 tell_str(stderr, str);
177
178 exit(1);
179}
07d9aa13 180
07d9aa13 181/*
182 * Print an error message and exit after closing the SSH link.
183 */
184static void bump(char *fmt, ...)
185{
cc87246d 186 char str[0x100]; /* Make the size big enough */
c51a56e2 187 va_list ap;
188 va_start(ap, fmt);
cc87246d 189 strcpy(str, "Fatal:");
190 vsprintf(str+strlen(str), fmt, ap);
c51a56e2 191 va_end(ap);
cc87246d 192 strcat(str, "\n");
193 tell_str(stderr, str);
194
c51a56e2 195 if (connection_open) {
196 char ch;
fb09bf1c 197 ssh_scp_send_eof();
198 ssh_scp_recv(&ch, 1);
c51a56e2 199 }
200 exit(1);
07d9aa13 201}
202
85ee8208 203static int get_password(const char *prompt, char *str, int maxlen)
07d9aa13 204{
c51a56e2 205 HANDLE hin, hout;
b8a19193 206 DWORD savemode, i;
207
208 if (password) {
85ee8208 209 static int tried_once = 0;
210
211 if (tried_once) {
212 return 0;
213 } else {
214 strncpy(str, password, maxlen);
215 str[maxlen-1] = '\0';
216 tried_once = 1;
217 return 1;
218 }
b8a19193 219 }
07d9aa13 220
cc87246d 221 /* GUI Adaptation - Sept 2000 */
222 if (gui_mode) {
223 if (maxlen>0) str[0] = '\0';
224 } else {
225 hin = GetStdHandle(STD_INPUT_HANDLE);
226 hout = GetStdHandle(STD_OUTPUT_HANDLE);
227 if (hin == INVALID_HANDLE_VALUE || hout == INVALID_HANDLE_VALUE)
228 bump("Cannot get standard input/output handles");
07d9aa13 229
cc87246d 230 GetConsoleMode(hin, &savemode);
231 SetConsoleMode(hin, (savemode & (~ENABLE_ECHO_INPUT)) |
232 ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT);
07d9aa13 233
cc87246d 234 WriteFile(hout, prompt, strlen(prompt), &i, NULL);
235 ReadFile(hin, str, maxlen-1, &i, NULL);
07d9aa13 236
cc87246d 237 SetConsoleMode(hin, savemode);
07d9aa13 238
cc87246d 239 if ((int)i > maxlen) i = maxlen-1; else i = i - 2;
240 str[i] = '\0';
07d9aa13 241
cc87246d 242 WriteFile(hout, "\r\n", 2, &i, NULL);
243 }
85ee8208 244
245 return 1;
07d9aa13 246}
247
07d9aa13 248/*
249 * Open an SSH connection to user@host and execute cmd.
250 */
251static void do_cmd(char *host, char *user, char *cmd)
252{
c51a56e2 253 char *err, *realhost;
254
255 if (host == NULL || host[0] == '\0')
256 bump("Empty host name");
257
258 /* Try to load settings for this host */
259 do_defaults(host);
260 if (cfg.host[0] == '\0') {
261 /* No settings for this host; use defaults */
262 strncpy(cfg.host, host, sizeof(cfg.host)-1);
263 cfg.host[sizeof(cfg.host)-1] = '\0';
264 cfg.port = 22;
265 }
266
267 /* Set username */
268 if (user != NULL && user[0] != '\0') {
269 strncpy(cfg.username, user, sizeof(cfg.username)-1);
270 cfg.username[sizeof(cfg.username)-1] = '\0';
c51a56e2 271 } else if (cfg.username[0] == '\0') {
272 bump("Empty user name");
273 }
274
275 if (cfg.protocol != PROT_SSH)
276 cfg.port = 22;
277
ed89e8a5 278 if (portnumber)
279 cfg.port = portnumber;
280
fb09bf1c 281 err = ssh_scp_init(cfg.host, cfg.port, cmd, &realhost);
c51a56e2 282 if (err != NULL)
283 bump("ssh_init: %s", err);
284 if (verbose && realhost != NULL)
cc87246d 285 tell_user(stderr, "Connected to %s\n", realhost);
c51a56e2 286
287 connection_open = 1;
07d9aa13 288}
289
07d9aa13 290/*
291 * Update statistic information about current file.
292 */
293static void print_stats(char *name, unsigned long size, unsigned long done,
c51a56e2 294 time_t start, time_t now)
07d9aa13 295{
c51a56e2 296 float ratebs;
297 unsigned long eta;
298 char etastr[10];
299 int pct;
300
cc87246d 301 /* GUI Adaptation - Sept 2000 */
302 if (gui_mode)
303 gui_update_stats(name, size, ((done *100) / size), now-start);
304 else {
305 if (now > start)
306 ratebs = (float) done / (now - start);
307 else
308 ratebs = (float) done;
c51a56e2 309
cc87246d 310 if (ratebs < 1.0)
311 eta = size - done;
312 else
313 eta = (unsigned long) ((size - done) / ratebs);
314 sprintf(etastr, "%02ld:%02ld:%02ld",
315 eta / 3600, (eta % 3600) / 60, eta % 60);
c51a56e2 316
cc87246d 317 pct = (int) (100.0 * (float) done / size);
c51a56e2 318
cc87246d 319 printf("\r%-25.25s | %10ld kB | %5.1f kB/s | ETA: %8s | %3d%%",
320 name, done / 1024, ratebs / 1024.0,
321 etastr, pct);
c51a56e2 322
cc87246d 323 if (done == size)
324 printf("\n");
325 }
07d9aa13 326}
327
07d9aa13 328/*
329 * Find a colon in str and return a pointer to the colon.
39ddf0ff 330 * This is used to separate hostname from filename.
07d9aa13 331 */
332static char * colon(char *str)
333{
c51a56e2 334 /* We ignore a leading colon, since the hostname cannot be
335 empty. We also ignore a colon as second character because
336 of filenames like f:myfile.txt. */
337 if (str[0] == '\0' ||
338 str[0] == ':' ||
339 str[1] == ':')
340 return (NULL);
341 while (*str != '\0' &&
342 *str != ':' &&
343 *str != '/' &&
344 *str != '\\')
345 str++;
346 if (*str == ':')
347 return (str);
348 else
349 return (NULL);
07d9aa13 350}
351
07d9aa13 352/*
353 * Wait for a response from the other side.
354 * Return 0 if ok, -1 if error.
355 */
356static int response(void)
357{
c51a56e2 358 char ch, resp, rbuf[2048];
359 int p;
360
fb09bf1c 361 if (ssh_scp_recv(&resp, 1) <= 0)
c51a56e2 362 bump("Lost connection");
363
364 p = 0;
365 switch (resp) {
366 case 0: /* ok */
367 return (0);
368 default:
369 rbuf[p++] = resp;
370 /* fallthrough */
371 case 1: /* error */
372 case 2: /* fatal error */
373 do {
fb09bf1c 374 if (ssh_scp_recv(&ch, 1) <= 0)
c51a56e2 375 bump("Protocol error: Lost connection");
376 rbuf[p++] = ch;
377 } while (p < sizeof(rbuf) && ch != '\n');
378 rbuf[p-1] = '\0';
379 if (resp == 1)
cc87246d 380 tell_user(stderr, "%s\n", rbuf);
c51a56e2 381 else
382 bump("%s", rbuf);
383 errs++;
384 return (-1);
385 }
07d9aa13 386}
387
07d9aa13 388/*
389 * Send an error message to the other side and to the screen.
390 * Increment error counter.
391 */
392static void run_err(const char *fmt, ...)
393{
c51a56e2 394 char str[2048];
395 va_list ap;
396 va_start(ap, fmt);
397 errs++;
398 strcpy(str, "\01scp: ");
399 vsprintf(str+strlen(str), fmt, ap);
400 strcat(str, "\n");
fb09bf1c 401 ssh_scp_send(str, strlen(str));
cc87246d 402 tell_user(stderr, "%s",str);
c51a56e2 403 va_end(ap);
07d9aa13 404}
405
07d9aa13 406/*
407 * Execute the source part of the SCP protocol.
408 */
409static void source(char *src)
410{
c51a56e2 411 char buf[2048];
412 unsigned long size;
413 char *last;
414 HANDLE f;
415 DWORD attr;
416 unsigned long i;
417 unsigned long stat_bytes;
418 time_t stat_starttime, stat_lasttime;
419
420 attr = GetFileAttributes(src);
996c8c3b 421 if (attr == (DWORD)-1) {
c51a56e2 422 run_err("%s: No such file or directory", src);
423 return;
424 }
425
426 if ((attr & FILE_ATTRIBUTE_DIRECTORY) != 0) {
7f1f80de 427 if (recursive) {
428 /*
429 * Avoid . and .. directories.
430 */
431 char *p;
432 p = strrchr(src, '/');
433 if (!p)
434 p = strrchr(src, '\\');
435 if (!p)
436 p = src;
437 else
438 p++;
439 if (!strcmp(p, ".") || !strcmp(p, ".."))
440 /* skip . and .. */;
441 else
442 rsource(src);
443 } else {
c51a56e2 444 run_err("%s: not a regular file", src);
7f1f80de 445 }
c51a56e2 446 return;
447 }
448
449 if ((last = strrchr(src, '/')) == NULL)
450 last = src;
451 else
452 last++;
453 if (strrchr(last, '\\') != NULL)
454 last = strrchr(last, '\\') + 1;
455 if (last == src && strchr(src, ':') != NULL)
456 last = strchr(src, ':') + 1;
457
458 f = CreateFile(src, GENERIC_READ, FILE_SHARE_READ, NULL,
459 OPEN_EXISTING, 0, 0);
460 if (f == INVALID_HANDLE_VALUE) {
486543a1 461 run_err("%s: Cannot open file", src);
c51a56e2 462 return;
463 }
464
465 if (preserve) {
466 FILETIME actime, wrtime;
467 unsigned long mtime, atime;
468 GetFileTime(f, NULL, &actime, &wrtime);
469 TIME_WIN_TO_POSIX(actime, atime);
470 TIME_WIN_TO_POSIX(wrtime, mtime);
471 sprintf(buf, "T%lu 0 %lu 0\n", mtime, atime);
fb09bf1c 472 ssh_scp_send(buf, strlen(buf));
07d9aa13 473 if (response())
c51a56e2 474 return;
475 }
476
477 size = GetFileSize(f, NULL);
478 sprintf(buf, "C0644 %lu %s\n", size, last);
479 if (verbose)
cc87246d 480 tell_user(stderr, "Sending file modes: %s", buf);
fb09bf1c 481 ssh_scp_send(buf, strlen(buf));
c51a56e2 482 if (response())
483 return;
484
485 if (statistics) {
486 stat_bytes = 0;
487 stat_starttime = time(NULL);
488 stat_lasttime = 0;
489 }
490
491 for (i = 0; i < size; i += 4096) {
492 char transbuf[4096];
493 DWORD j, k = 4096;
494 if (i + k > size) k = size - i;
495 if (! ReadFile(f, transbuf, k, &j, NULL) || j != k) {
496 if (statistics) printf("\n");
497 bump("%s: Read error", src);
07d9aa13 498 }
fb09bf1c 499 ssh_scp_send(transbuf, k);
c51a56e2 500 if (statistics) {
501 stat_bytes += k;
502 if (time(NULL) != stat_lasttime ||
503 i + k == size) {
504 stat_lasttime = time(NULL);
505 print_stats(last, size, stat_bytes,
506 stat_starttime, stat_lasttime);
507 }
07d9aa13 508 }
c51a56e2 509 }
510 CloseHandle(f);
07d9aa13 511
fb09bf1c 512 ssh_scp_send("", 1);
c51a56e2 513 (void) response();
07d9aa13 514}
515
07d9aa13 516/*
517 * Recursively send the contents of a directory.
518 */
519static void rsource(char *src)
520{
c51a56e2 521 char buf[2048];
522 char *last;
523 HANDLE dir;
524 WIN32_FIND_DATA fdat;
525 int ok;
526
527 if ((last = strrchr(src, '/')) == NULL)
528 last = src;
529 else
530 last++;
531 if (strrchr(last, '\\') != NULL)
532 last = strrchr(last, '\\') + 1;
533 if (last == src && strchr(src, ':') != NULL)
534 last = strchr(src, ':') + 1;
535
536 /* maybe send filetime */
537
538 sprintf(buf, "D0755 0 %s\n", last);
539 if (verbose)
cc87246d 540 tell_user(stderr, "Entering directory: %s", buf);
fb09bf1c 541 ssh_scp_send(buf, strlen(buf));
c51a56e2 542 if (response())
543 return;
544
545 sprintf(buf, "%s/*", src);
546 dir = FindFirstFile(buf, &fdat);
547 ok = (dir != INVALID_HANDLE_VALUE);
548 while (ok) {
549 if (strcmp(fdat.cFileName, ".") == 0 ||
550 strcmp(fdat.cFileName, "..") == 0) {
551 } else if (strlen(src) + 1 + strlen(fdat.cFileName) >=
552 sizeof(buf)) {
553 run_err("%s/%s: Name too long", src, fdat.cFileName);
554 } else {
555 sprintf(buf, "%s/%s", src, fdat.cFileName);
556 source(buf);
07d9aa13 557 }
c51a56e2 558 ok = FindNextFile(dir, &fdat);
559 }
560 FindClose(dir);
07d9aa13 561
c51a56e2 562 sprintf(buf, "E\n");
fb09bf1c 563 ssh_scp_send(buf, strlen(buf));
c51a56e2 564 (void) response();
07d9aa13 565}
566
07d9aa13 567/*
568 * Execute the sink part of the SCP protocol.
569 */
570static void sink(char *targ)
571{
c51a56e2 572 char buf[2048];
573 char namebuf[2048];
574 char ch;
575 int targisdir = 0;
996c8c3b 576 int settime;
c51a56e2 577 int exists;
578 DWORD attr;
579 HANDLE f;
580 unsigned long mtime, atime;
581 unsigned int mode;
582 unsigned long size, i;
583 int wrerror = 0;
584 unsigned long stat_bytes;
585 time_t stat_starttime, stat_lasttime;
586 char *stat_name;
587
588 attr = GetFileAttributes(targ);
996c8c3b 589 if (attr != (DWORD)-1 && (attr & FILE_ATTRIBUTE_DIRECTORY) != 0)
c51a56e2 590 targisdir = 1;
591
592 if (targetshouldbedirectory && !targisdir)
593 bump("%s: Not a directory", targ);
594
fb09bf1c 595 ssh_scp_send("", 1);
c51a56e2 596 while (1) {
597 settime = 0;
598 gottime:
fb09bf1c 599 if (ssh_scp_recv(&ch, 1) <= 0)
c51a56e2 600 return;
601 if (ch == '\n')
602 bump("Protocol error: Unexpected newline");
603 i = 0;
604 buf[i++] = ch;
605 do {
fb09bf1c 606 if (ssh_scp_recv(&ch, 1) <= 0)
c51a56e2 607 bump("Lost connection");
608 buf[i++] = ch;
609 } while (i < sizeof(buf) && ch != '\n');
610 buf[i-1] = '\0';
611 switch (buf[0]) {
612 case '\01': /* error */
cc87246d 613 tell_user(stderr, "%s\n", buf+1);
c51a56e2 614 errs++;
615 continue;
616 case '\02': /* fatal error */
617 bump("%s", buf+1);
618 case 'E':
fb09bf1c 619 ssh_scp_send("", 1);
c51a56e2 620 return;
621 case 'T':
1d470ad2 622 if (sscanf(buf, "T%ld %*d %ld %*d",
c51a56e2 623 &mtime, &atime) == 2) {
624 settime = 1;
fb09bf1c 625 ssh_scp_send("", 1);
c51a56e2 626 goto gottime;
627 }
628 bump("Protocol error: Illegal time format");
629 case 'C':
630 case 'D':
631 break;
632 default:
633 bump("Protocol error: Expected control record");
634 }
07d9aa13 635
1d470ad2 636 if (sscanf(buf+1, "%u %lu %[^\n]", &mode, &size, namebuf) != 3)
c51a56e2 637 bump("Protocol error: Illegal file descriptor format");
638 if (targisdir) {
639 char t[2048];
640 strcpy(t, targ);
641 if (targ[0] != '\0')
642 strcat(t, "/");
643 strcat(t, namebuf);
644 strcpy(namebuf, t);
645 } else {
646 strcpy(namebuf, targ);
647 }
648 attr = GetFileAttributes(namebuf);
996c8c3b 649 exists = (attr != (DWORD)-1);
c51a56e2 650
651 if (buf[0] == 'D') {
652 if (exists && (attr & FILE_ATTRIBUTE_DIRECTORY) == 0) {
653 run_err("%s: Not a directory", namebuf);
654 continue;
655 }
656 if (!exists) {
657 if (! CreateDirectory(namebuf, NULL)) {
658 run_err("%s: Cannot create directory",
659 namebuf);
660 continue;
661 }
662 }
663 sink(namebuf);
664 /* can we set the timestamp for directories ? */
665 continue;
666 }
07d9aa13 667
c51a56e2 668 f = CreateFile(namebuf, GENERIC_WRITE, 0, NULL,
669 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
670 if (f == INVALID_HANDLE_VALUE) {
671 run_err("%s: Cannot create file", namebuf);
672 continue;
673 }
07d9aa13 674
fb09bf1c 675 ssh_scp_send("", 1);
07d9aa13 676
c51a56e2 677 if (statistics) {
678 stat_bytes = 0;
679 stat_starttime = time(NULL);
680 stat_lasttime = 0;
681 if ((stat_name = strrchr(namebuf, '/')) == NULL)
682 stat_name = namebuf;
683 else
684 stat_name++;
685 if (strrchr(stat_name, '\\') != NULL)
686 stat_name = strrchr(stat_name, '\\') + 1;
687 }
07d9aa13 688
c51a56e2 689 for (i = 0; i < size; i += 4096) {
690 char transbuf[4096];
996c8c3b 691 DWORD j, k = 4096;
c51a56e2 692 if (i + k > size) k = size - i;
fb09bf1c 693 if (ssh_scp_recv(transbuf, k) == 0)
c51a56e2 694 bump("Lost connection");
695 if (wrerror) continue;
696 if (! WriteFile(f, transbuf, k, &j, NULL) || j != k) {
697 wrerror = 1;
698 if (statistics)
699 printf("\r%-25.25s | %50s\n",
700 stat_name,
701 "Write error.. waiting for end of file");
702 continue;
703 }
704 if (statistics) {
705 stat_bytes += k;
706 if (time(NULL) > stat_lasttime ||
707 i + k == size) {
708 stat_lasttime = time(NULL);
709 print_stats(stat_name, size, stat_bytes,
710 stat_starttime, stat_lasttime);
07d9aa13 711 }
c51a56e2 712 }
713 }
714 (void) response();
07d9aa13 715
c51a56e2 716 if (settime) {
717 FILETIME actime, wrtime;
718 TIME_POSIX_TO_WIN(atime, actime);
719 TIME_POSIX_TO_WIN(mtime, wrtime);
720 SetFileTime(f, NULL, &actime, &wrtime);
07d9aa13 721 }
07d9aa13 722
c51a56e2 723 CloseHandle(f);
724 if (wrerror) {
725 run_err("%s: Write error", namebuf);
726 continue;
727 }
fb09bf1c 728 ssh_scp_send("", 1);
c51a56e2 729 }
730}
07d9aa13 731
732/*
733 * We will copy local files to a remote server.
734 */
735static void toremote(int argc, char *argv[])
736{
c51a56e2 737 char *src, *targ, *host, *user;
738 char *cmd;
739 int i;
740
741 targ = argv[argc-1];
742
39ddf0ff 743 /* Separate host from filename */
c51a56e2 744 host = targ;
745 targ = colon(targ);
746 if (targ == NULL)
747 bump("targ == NULL in toremote()");
748 *targ++ = '\0';
749 if (*targ == '\0')
750 targ = ".";
751 /* Substitute "." for emtpy target */
752
39ddf0ff 753 /* Separate host and username */
c51a56e2 754 user = host;
755 host = strrchr(host, '@');
756 if (host == NULL) {
757 host = user;
758 user = NULL;
759 } else {
760 *host++ = '\0';
761 if (*user == '\0')
762 user = NULL;
763 }
764
765 if (argc == 2) {
766 /* Find out if the source filespec covers multiple files
767 if so, we should set the targetshouldbedirectory flag */
768 HANDLE fh;
769 WIN32_FIND_DATA fdat;
770 if (colon(argv[0]) != NULL)
771 bump("%s: Remote to remote not supported", argv[0]);
772 fh = FindFirstFile(argv[0], &fdat);
773 if (fh == INVALID_HANDLE_VALUE)
774 bump("%s: No such file or directory\n", argv[0]);
775 if (FindNextFile(fh, &fdat))
776 targetshouldbedirectory = 1;
777 FindClose(fh);
778 }
779
780 cmd = smalloc(strlen(targ) + 100);
781 sprintf(cmd, "scp%s%s%s%s -t %s",
782 verbose ? " -v" : "",
783 recursive ? " -r" : "",
784 preserve ? " -p" : "",
785 targetshouldbedirectory ? " -d" : "",
786 targ);
787 do_cmd(host, user, cmd);
788 sfree(cmd);
789
790 (void) response();
791
792 for (i = 0; i < argc - 1; i++) {
793 HANDLE dir;
794 WIN32_FIND_DATA fdat;
795 src = argv[i];
796 if (colon(src) != NULL) {
cc87246d 797 tell_user(stderr, "%s: Remote to remote not supported\n", src);
c51a56e2 798 errs++;
799 continue;
07d9aa13 800 }
c51a56e2 801 dir = FindFirstFile(src, &fdat);
802 if (dir == INVALID_HANDLE_VALUE) {
803 run_err("%s: No such file or directory", src);
804 continue;
07d9aa13 805 }
c51a56e2 806 do {
807 char *last;
808 char namebuf[2048];
809 if (strlen(src) + strlen(fdat.cFileName) >=
810 sizeof(namebuf)) {
cc87246d 811 tell_user(stderr, "%s: Name too long", src);
c51a56e2 812 continue;
813 }
814 strcpy(namebuf, src);
815 if ((last = strrchr(namebuf, '/')) == NULL)
816 last = namebuf;
817 else
818 last++;
819 if (strrchr(last, '\\') != NULL)
820 last = strrchr(last, '\\') + 1;
821 if (last == namebuf && strrchr(namebuf, ':') != NULL)
822 last = strchr(namebuf, ':') + 1;
823 strcpy(last, fdat.cFileName);
824 source(namebuf);
825 } while (FindNextFile(dir, &fdat));
826 FindClose(dir);
827 }
07d9aa13 828}
829
07d9aa13 830/*
831 * We will copy files from a remote server to the local machine.
832 */
833static void tolocal(int argc, char *argv[])
834{
c51a56e2 835 char *src, *targ, *host, *user;
836 char *cmd;
837
838 if (argc != 2)
839 bump("More than one remote source not supported");
840
841 src = argv[0];
842 targ = argv[1];
843
39ddf0ff 844 /* Separate host from filename */
c51a56e2 845 host = src;
846 src = colon(src);
847 if (src == NULL)
848 bump("Local to local copy not supported");
849 *src++ = '\0';
850 if (*src == '\0')
851 src = ".";
852 /* Substitute "." for empty filename */
853
39ddf0ff 854 /* Separate username and hostname */
c51a56e2 855 user = host;
856 host = strrchr(host, '@');
857 if (host == NULL) {
858 host = user;
859 user = NULL;
860 } else {
861 *host++ = '\0';
862 if (*user == '\0')
863 user = NULL;
864 }
865
866 cmd = smalloc(strlen(src) + 100);
867 sprintf(cmd, "scp%s%s%s%s -f %s",
868 verbose ? " -v" : "",
869 recursive ? " -r" : "",
870 preserve ? " -p" : "",
871 targetshouldbedirectory ? " -d" : "",
872 src);
873 do_cmd(host, user, cmd);
874 sfree(cmd);
875
876 sink(targ);
07d9aa13 877}
878
07d9aa13 879/*
39ddf0ff 880 * We will issue a list command to get a remote directory.
881 */
882static void get_dir_list(int argc, char *argv[])
883{
884 char *src, *host, *user;
885 char *cmd, *p, *q;
886 char c;
887
888 src = argv[0];
889
890 /* Separate host from filename */
891 host = src;
892 src = colon(src);
893 if (src == NULL)
894 bump("Local to local copy not supported");
895 *src++ = '\0';
896 if (*src == '\0')
897 src = ".";
898 /* Substitute "." for empty filename */
899
900 /* Separate username and hostname */
901 user = host;
902 host = strrchr(host, '@');
903 if (host == NULL) {
904 host = user;
905 user = NULL;
906 } else {
907 *host++ = '\0';
908 if (*user == '\0')
909 user = NULL;
910 }
911
912 cmd = smalloc(4*strlen(src) + 100);
913 strcpy(cmd, "ls -la '");
914 p = cmd + strlen(cmd);
915 for (q = src; *q; q++) {
916 if (*q == '\'') {
917 *p++ = '\''; *p++ = '\\'; *p++ = '\''; *p++ = '\'';
918 } else {
919 *p++ = *q;
920 }
921 }
922 *p++ = '\'';
923 *p = '\0';
cc87246d 924
39ddf0ff 925 do_cmd(host, user, cmd);
926 sfree(cmd);
927
fb09bf1c 928 while (ssh_scp_recv(&c, 1) > 0)
cc87246d 929 tell_char(stdout, c);
39ddf0ff 930}
931
932/*
07d9aa13 933 * Initialize the Win$ock driver.
934 */
996c8c3b 935static void init_winsock(void)
07d9aa13 936{
c51a56e2 937 WORD winsock_ver;
938 WSADATA wsadata;
939
940 winsock_ver = MAKEWORD(1, 1);
941 if (WSAStartup(winsock_ver, &wsadata))
942 bump("Unable to initialise WinSock");
943 if (LOBYTE(wsadata.wVersion) != 1 ||
944 HIBYTE(wsadata.wVersion) != 1)
945 bump("WinSock version is incompatible with 1.1");
07d9aa13 946}
947
07d9aa13 948/*
949 * Short description of parameters.
950 */
996c8c3b 951static void usage(void)
07d9aa13 952{
c51a56e2 953 printf("PuTTY Secure Copy client\n");
954 printf("%s\n", ver);
a3e55ea1 955 printf("Usage: pscp [options] [user@]host:source target\n");
956 printf(" pscp [options] source [source...] [user@]host:target\n");
957 printf(" pscp [options] -ls user@host:filespec\n");
b8a19193 958 printf("Options:\n");
959 printf(" -p preserve file attributes\n");
960 printf(" -q quiet, don't show statistics\n");
961 printf(" -r copy directories recursively\n");
962 printf(" -v show verbose messages\n");
963 printf(" -P port connect to specified port\n");
964 printf(" -pw passw login with specified password\n");
cc87246d 965 /* GUI Adaptation - Sept 2000 */
966 printf(" -gui hWnd GUI mode with the windows handle for receiving messages\n");
c51a56e2 967 exit(1);
07d9aa13 968}
969
07d9aa13 970/*
971 * Main program (no, really?)
972 */
973int main(int argc, char *argv[])
974{
c51a56e2 975 int i;
39ddf0ff 976 int list = 0;
c51a56e2 977
fb09bf1c 978 default_protocol = PROT_TELNET;
979
67779be7 980 flags = FLAG_STDERR;
fb09bf1c 981 ssh_get_password = &get_password;
c51a56e2 982 init_winsock();
983
984 for (i = 1; i < argc; i++) {
985 if (argv[i][0] != '-')
986 break;
987 if (strcmp(argv[i], "-v") == 0)
4017be6d 988 verbose = 1, flags |= FLAG_VERBOSE;
c51a56e2 989 else if (strcmp(argv[i], "-r") == 0)
990 recursive = 1;
991 else if (strcmp(argv[i], "-p") == 0)
992 preserve = 1;
993 else if (strcmp(argv[i], "-q") == 0)
994 statistics = 0;
995 else if (strcmp(argv[i], "-h") == 0 ||
996 strcmp(argv[i], "-?") == 0)
997 usage();
b8a19193 998 else if (strcmp(argv[i], "-P") == 0 && i+1 < argc)
999 portnumber = atoi(argv[++i]);
1000 else if (strcmp(argv[i], "-pw") == 0 && i+1 < argc)
1001 password = argv[++i];
cc87246d 1002 else if (strcmp(argv[i], "-gui") == 0 && i+1 < argc) {
1003 gui_hwnd = argv[++i];
1004 gui_mode = 1;
1005 } else if (strcmp(argv[i], "-ls") == 0)
1006 list = 1;
c51a56e2 1007 else if (strcmp(argv[i], "--") == 0)
1008 { i++; break; }
07d9aa13 1009 else
c51a56e2 1010 usage();
1011 }
1012 argc -= i;
1013 argv += i;
1014
39ddf0ff 1015 if (list) {
1016 if (argc != 1)
1017 usage();
1018 get_dir_list(argc, argv);
c51a56e2 1019
39ddf0ff 1020 } else {
1021
1022 if (argc < 2)
1023 usage();
1024 if (argc > 2)
1025 targetshouldbedirectory = 1;
1026
1027 if (colon(argv[argc-1]) != NULL)
1028 toremote(argc, argv);
1029 else
1030 tolocal(argc, argv);
1031 }
c51a56e2 1032
1033 if (connection_open) {
1034 char ch;
fb09bf1c 1035 ssh_scp_send_eof();
1036 ssh_scp_recv(&ch, 1);
c51a56e2 1037 }
1038 WSACleanup();
1039 random_save_seed();
07d9aa13 1040
cc87246d 1041 /* GUI Adaptation - August 2000 */
1042 if (gui_mode) {
1043 unsigned int msg_id = WM_RET_ERR_CNT;
1044 if (list) msg_id = WM_LS_RET_ERR_CNT;
1045 while (!PostMessage( (HWND)atoi(gui_hwnd), msg_id, (WPARAM)errs, 0/*lParam*/ ) )
1046 SleepEx(1000,TRUE);
1047 }
c51a56e2 1048 return (errs == 0 ? 0 : 1);
07d9aa13 1049}
1050
1051/* end */