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