Added a fourth application: plink, a command line connection utility
[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
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
20 #define TIME_POSIX_TO_WIN(t, ft) (*(LONGLONG*)&(ft) = \
21 ((LONGLONG) (t) + (LONGLONG) 11644473600) * (LONGLONG) 10000000)
22 #define TIME_WIN_TO_POSIX(ft, t) ((t) = (unsigned long) \
23 ((*(LONGLONG*)&(ft)) / (LONGLONG) 10000000 - (LONGLONG) 11644473600))
24
25 static int verbose = 0;
26 static int recursive = 0;
27 static int preserve = 0;
28 static int targetshouldbedirectory = 0;
29 static int statistics = 1;
30 static int portnumber = 0;
31 static char *password = NULL;
32 static int errs = 0;
33 static int connection_open = 0;
34
35 static void source(char *src);
36 static void rsource(char *src);
37 static void sink(char *targ);
38
39 /*
40 * This function is needed to link with ssh.c, but it never gets called.
41 */
42 void term_out(void)
43 {
44 abort();
45 }
46
47 /*
48 * Print an error message and perform a fatal exit.
49 */
50 void fatalbox(char *fmt, ...)
51 {
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);
59 }
60
61 /*
62 * Print an error message and exit after closing the SSH link.
63 */
64 static void bump(char *fmt, ...)
65 {
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;
74 ssh_scp_send_eof();
75 ssh_scp_recv(&ch, 1);
76 }
77 exit(1);
78 }
79
80 static int get_password(const char *prompt, char *str, int maxlen)
81 {
82 HANDLE hin, hout;
83 DWORD savemode, i;
84
85 if (password) {
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 }
96 }
97
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");
102
103 GetConsoleMode(hin, &savemode);
104 SetConsoleMode(hin, (savemode & (~ENABLE_ECHO_INPUT)) |
105 ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT);
106
107 WriteFile(hout, prompt, strlen(prompt), &i, NULL);
108 ReadFile(hin, str, maxlen-1, &i, NULL);
109
110 SetConsoleMode(hin, savemode);
111
112 if ((int)i > maxlen) i = maxlen-1; else i = i - 2;
113 str[i] = '\0';
114
115 WriteFile(hout, "\r\n", 2, &i, NULL);
116
117 return 1;
118 }
119
120 /*
121 * Open an SSH connection to user@host and execute cmd.
122 */
123 static void do_cmd(char *host, char *user, char *cmd)
124 {
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';
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
150 if (portnumber)
151 cfg.port = portnumber;
152
153 err = ssh_scp_init(cfg.host, cfg.port, cmd, &realhost);
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;
160 }
161
162 /*
163 * Update statistic information about current file.
164 */
165 static void print_stats(char *name, unsigned long size, unsigned long done,
166 time_t start, time_t now)
167 {
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);
182 sprintf(etastr, "%02ld:%02ld:%02ld",
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");
193 }
194
195 /*
196 * Find a colon in str and return a pointer to the colon.
197 * This is used to separate hostname from filename.
198 */
199 static char * colon(char *str)
200 {
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);
217 }
218
219 /*
220 * Wait for a response from the other side.
221 * Return 0 if ok, -1 if error.
222 */
223 static int response(void)
224 {
225 char ch, resp, rbuf[2048];
226 int p;
227
228 if (ssh_scp_recv(&resp, 1) <= 0)
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 {
241 if (ssh_scp_recv(&ch, 1) <= 0)
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 }
253 }
254
255 /*
256 * Send an error message to the other side and to the screen.
257 * Increment error counter.
258 */
259 static void run_err(const char *fmt, ...)
260 {
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");
268 ssh_scp_send(str, strlen(str));
269 vfprintf(stderr, fmt, ap);
270 fprintf(stderr, "\n");
271 va_end(ap);
272 }
273
274 /*
275 * Execute the source part of the SCP protocol.
276 */
277 static void source(char *src)
278 {
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);
289 if (attr == (DWORD)-1) {
290 run_err("%s: No such file or directory", src);
291 return;
292 }
293
294 if ((attr & FILE_ATTRIBUTE_DIRECTORY) != 0) {
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 {
312 run_err("%s: not a regular file", src);
313 }
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) {
329 run_err("%s: Cannot open file", src);
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);
340 ssh_scp_send(buf, strlen(buf));
341 if (response())
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);
349 ssh_scp_send(buf, strlen(buf));
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);
366 }
367 ssh_scp_send(transbuf, k);
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 }
376 }
377 }
378 CloseHandle(f);
379
380 ssh_scp_send("", 1);
381 (void) response();
382 }
383
384 /*
385 * Recursively send the contents of a directory.
386 */
387 static void rsource(char *src)
388 {
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);
409 ssh_scp_send(buf, strlen(buf));
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);
425 }
426 ok = FindNextFile(dir, &fdat);
427 }
428 FindClose(dir);
429
430 sprintf(buf, "E\n");
431 ssh_scp_send(buf, strlen(buf));
432 (void) response();
433 }
434
435 /*
436 * Execute the sink part of the SCP protocol.
437 */
438 static void sink(char *targ)
439 {
440 char buf[2048];
441 char namebuf[2048];
442 char ch;
443 int targisdir = 0;
444 int settime;
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);
457 if (attr != (DWORD)-1 && (attr & FILE_ATTRIBUTE_DIRECTORY) != 0)
458 targisdir = 1;
459
460 if (targetshouldbedirectory && !targisdir)
461 bump("%s: Not a directory", targ);
462
463 ssh_scp_send("", 1);
464 while (1) {
465 settime = 0;
466 gottime:
467 if (ssh_scp_recv(&ch, 1) <= 0)
468 return;
469 if (ch == '\n')
470 bump("Protocol error: Unexpected newline");
471 i = 0;
472 buf[i++] = ch;
473 do {
474 if (ssh_scp_recv(&ch, 1) <= 0)
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':
487 ssh_scp_send("", 1);
488 return;
489 case 'T':
490 if (sscanf(buf, "T%ld %*d %ld %*d",
491 &mtime, &atime) == 2) {
492 settime = 1;
493 ssh_scp_send("", 1);
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 }
503
504 if (sscanf(buf+1, "%u %lu %[^\n]", &mode, &size, namebuf) != 3)
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);
517 exists = (attr != (DWORD)-1);
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 }
535
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 }
542
543 ssh_scp_send("", 1);
544
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 }
556
557 for (i = 0; i < size; i += 4096) {
558 char transbuf[4096];
559 DWORD j, k = 4096;
560 if (i + k > size) k = size - i;
561 if (ssh_scp_recv(transbuf, k) == 0)
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);
579 }
580 }
581 }
582 (void) response();
583
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);
589 }
590
591 CloseHandle(f);
592 if (wrerror) {
593 run_err("%s: Write error", namebuf);
594 continue;
595 }
596 ssh_scp_send("", 1);
597 }
598 }
599
600 /*
601 * We will copy local files to a remote server.
602 */
603 static void toremote(int argc, char *argv[])
604 {
605 char *src, *targ, *host, *user;
606 char *cmd;
607 int i;
608
609 targ = argv[argc-1];
610
611 /* Separate host from filename */
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
621 /* Separate host and username */
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;
669 }
670 dir = FindFirstFile(src, &fdat);
671 if (dir == INVALID_HANDLE_VALUE) {
672 run_err("%s: No such file or directory", src);
673 continue;
674 }
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 }
697 }
698
699 /*
700 * We will copy files from a remote server to the local machine.
701 */
702 static void tolocal(int argc, char *argv[])
703 {
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
713 /* Separate host from filename */
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
723 /* Separate username and hostname */
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);
746 }
747
748 /*
749 * We will issue a list command to get a remote directory.
750 */
751 static 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
797 while (ssh_scp_recv(&c, 1) > 0)
798 fputc(c, stdout); /* thank heavens for buffered I/O */
799 }
800
801 /*
802 * Initialize the Win$ock driver.
803 */
804 static void init_winsock(void)
805 {
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");
815 }
816
817 /*
818 * Short description of parameters.
819 */
820 static void usage(void)
821 {
822 printf("PuTTY Secure Copy client\n");
823 printf("%s\n", ver);
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");
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");
834 exit(1);
835 }
836
837 /*
838 * Main program (no, really?)
839 */
840 int main(int argc, char *argv[])
841 {
842 int i;
843 int list = 0;
844
845 default_protocol = PROT_TELNET;
846
847 flags = 0;
848 ssh_get_password = &get_password;
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)
855 verbose = 1, flags |= FLAG_VERBOSE;
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();
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];
869 else if (strcmp(argv[i], "-ls") == 0)
870 list = 1;
871 else if (strcmp(argv[i], "--") == 0)
872 { i++; break; }
873 else
874 usage();
875 }
876 argc -= i;
877 argv += i;
878
879 if (list) {
880 if (argc != 1)
881 usage();
882 get_dir_list(argc, argv);
883
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 }
896
897 if (connection_open) {
898 char ch;
899 ssh_scp_send_eof();
900 ssh_scp_recv(&ch, 1);
901 }
902 WSACleanup();
903 random_save_seed();
904
905 return (errs == 0 ? 0 : 1);
906 }
907
908 /* end */
909