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