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