Oops. Dirsep in Makefile.cyg should be / not \.
[u/mdw/putty] / scp.c
CommitLineData
07d9aa13 1/*
a673e210 2 * scp.c - Scp (Secure Copy) client for PuTTY.
3 * Joris van Rantwijk, Simon Tatham
07d9aa13 4 *
a673e210 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 * (SGT, 2001-09-10: Joris van Rantwijk assures me that although
9 * this file as originally submitted was inspired by, and
10 * _structurally_ based on, ssh-1.2.26's scp.c, there wasn't any
11 * actual code duplicated, so the above comment shouldn't give rise
12 * to licensing issues.)
07d9aa13 13 */
14
07d9aa13 15#include <windows.h>
4d331a77 16#ifndef AUTO_WINSOCK
17#ifdef WINSOCK_TWO
18#include <winsock2.h>
19#else
07d9aa13 20#include <winsock.h>
4d331a77 21#endif
22#endif
07d9aa13 23#include <stdlib.h>
24#include <stdio.h>
25#include <string.h>
fd5e5847 26#include <limits.h>
07d9aa13 27#include <time.h>
feb7fdfe 28#include <assert.h>
07d9aa13 29
30#define PUTTY_DO_GLOBALS
31#include "putty.h"
fd5e5847 32#include "ssh.h"
33#include "sftp.h"
8c3cd914 34#include "winstuff.h"
a9422f39 35#include "storage.h"
07d9aa13 36
07d9aa13 37#define TIME_POSIX_TO_WIN(t, ft) (*(LONGLONG*)&(ft) = \
c51a56e2 38 ((LONGLONG) (t) + (LONGLONG) 11644473600) * (LONGLONG) 10000000)
07d9aa13 39#define TIME_WIN_TO_POSIX(ft, t) ((t) = (unsigned long) \
c51a56e2 40 ((*(LONGLONG*)&(ft)) / (LONGLONG) 10000000 - (LONGLONG) 11644473600))
07d9aa13 41
cc87246d 42/* GUI Adaptation - Sept 2000 */
037fb113 43
44/* This is just a base value from which the main message numbers are
45 * derived. */
cc87246d 46#define WM_APP_BASE 0x8000
037fb113 47
48/* These two pass a single character value in wParam. They represent
49 * the visible output from PSCP. */
cc87246d 50#define WM_STD_OUT_CHAR ( WM_APP_BASE+400 )
51#define WM_STD_ERR_CHAR ( WM_APP_BASE+401 )
037fb113 52
53/* These pass a transfer status update. WM_STATS_CHAR passes a single
54 * character in wParam, and is called repeatedly to pass the name of
55 * the file, terminated with "\n". WM_STATS_SIZE passes the size of
56 * the file being transferred in wParam. WM_STATS_ELAPSED is called
57 * to pass the elapsed time (in seconds) in wParam, and
58 * WM_STATS_PERCENT passes the percentage of the transfer which is
59 * complete, also in wParam. */
cc87246d 60#define WM_STATS_CHAR ( WM_APP_BASE+402 )
61#define WM_STATS_SIZE ( WM_APP_BASE+403 )
62#define WM_STATS_PERCENT ( WM_APP_BASE+404 )
63#define WM_STATS_ELAPSED ( WM_APP_BASE+405 )
037fb113 64
65/* These are used at the end of a run to pass an error code in
66 * wParam: zero means success, nonzero means failure. WM_RET_ERR_CNT
67 * is used after a copy, and WM_LS_RET_ERR_CNT is used after a file
68 * list operation. */
cc87246d 69#define WM_RET_ERR_CNT ( WM_APP_BASE+406 )
70#define WM_LS_RET_ERR_CNT ( WM_APP_BASE+407 )
037fb113 71
72/* More transfer status update messages. WM_STATS_DONE passes the
73 * number of bytes sent so far in wParam. WM_STATS_ETA passes the
74 * estimated time to completion (in seconds). WM_STATS_RATEBS passes
75 * the average transfer rate (in bytes per second). */
d524be1c 76#define WM_STATS_DONE ( WM_APP_BASE+408 )
77#define WM_STATS_ETA ( WM_APP_BASE+409 )
78#define WM_STATS_RATEBS ( WM_APP_BASE+410 )
cc87246d 79
2bc6a386 80static int list = 0;
fb09bf1c 81static int verbose = 0;
07d9aa13 82static int recursive = 0;
83static int preserve = 0;
84static int targetshouldbedirectory = 0;
85static int statistics = 1;
b1daf518 86static int prev_stats_len = 0;
cd1f39ab 87static int scp_unsafe_mode = 0;
07d9aa13 88static int errs = 0;
cc87246d 89/* GUI Adaptation - Sept 2000 */
90#define NAME_STR_MAX 2048
32874aea 91static char statname[NAME_STR_MAX + 1];
cc87246d 92static unsigned long statsize = 0;
d524be1c 93static unsigned long statdone = 0;
94static unsigned long stateta = 0;
95static unsigned long statratebs = 0;
cc87246d 96static int statperct = 0;
90a14a09 97static unsigned long statelapsed = 0;
cc87246d 98static int gui_mode = 0;
99static char *gui_hwnd = NULL;
fd5e5847 100static int using_sftp = 0;
07d9aa13 101
102static void source(char *src);
103static void rsource(char *src);
ca2d5943 104static void sink(char *targ, char *src);
cc87246d 105/* GUI Adaptation - Sept 2000 */
32874aea 106static void tell_char(FILE * stream, char c);
107static void tell_str(FILE * stream, char *str);
108static void tell_user(FILE * stream, char *fmt, ...);
90a14a09 109static void gui_update_stats(char *name, unsigned long size,
d524be1c 110 int percentage, unsigned long elapsed,
111 unsigned long done, unsigned long eta,
112 unsigned long ratebs);
07d9aa13 113
5471d09a 114/*
115 * The maximum amount of queued data we accept before we stop and
116 * wait for the server to process some.
117 */
118#define MAX_SCP_BUFSIZE 16384
119
760e88b2 120void ldisc_send(char *buf, int len, int interactive)
32874aea 121{
feb7fdfe 122 /*
123 * This is only here because of the calls to ldisc_send(NULL,
124 * 0) in ssh.c. Nothing in PSCP actually needs to use the ldisc
125 * as an ldisc. So if we get called with any real data, I want
126 * to know about it.
127 */
128 assert(len == 0);
129}
130
cc87246d 131/* GUI Adaptation - Sept 2000 */
75cab814 132static void send_msg(HWND h, UINT message, WPARAM wParam)
cc87246d 133{
32874aea 134 while (!PostMessage(h, message, wParam, 0))
135 SleepEx(1000, TRUE);
cc87246d 136}
137
32874aea 138static void tell_char(FILE * stream, char c)
cc87246d 139{
140 if (!gui_mode)
141 fputc(c, stream);
32874aea 142 else {
cc87246d 143 unsigned int msg_id = WM_STD_OUT_CHAR;
32874aea 144 if (stream == stderr)
145 msg_id = WM_STD_ERR_CHAR;
146 send_msg((HWND) atoi(gui_hwnd), msg_id, (WPARAM) c);
cc87246d 147 }
148}
149
32874aea 150static void tell_str(FILE * stream, char *str)
cc87246d 151{
152 unsigned int i;
153
32874aea 154 for (i = 0; i < strlen(str); ++i)
cc87246d 155 tell_char(stream, str[i]);
156}
157
32874aea 158static void tell_user(FILE * stream, char *fmt, ...)
cc87246d 159{
32874aea 160 char str[0x100]; /* Make the size big enough */
cc87246d 161 va_list ap;
162 va_start(ap, fmt);
163 vsprintf(str, fmt, ap);
164 va_end(ap);
165 strcat(str, "\n");
166 tell_str(stream, str);
167}
168
32874aea 169static void gui_update_stats(char *name, unsigned long size,
d524be1c 170 int percentage, unsigned long elapsed,
171 unsigned long done, unsigned long eta,
172 unsigned long ratebs)
cc87246d 173{
174 unsigned int i;
175
32874aea 176 if (strcmp(name, statname) != 0) {
177 for (i = 0; i < strlen(name); ++i)
178 send_msg((HWND) atoi(gui_hwnd), WM_STATS_CHAR,
179 (WPARAM) name[i]);
180 send_msg((HWND) atoi(gui_hwnd), WM_STATS_CHAR, (WPARAM) '\n');
181 strcpy(statname, name);
cc87246d 182 }
32874aea 183 if (statsize != size) {
184 send_msg((HWND) atoi(gui_hwnd), WM_STATS_SIZE, (WPARAM) size);
cc87246d 185 statsize = size;
186 }
d524be1c 187 if (statdone != done) {
188 send_msg((HWND) atoi(gui_hwnd), WM_STATS_DONE, (WPARAM) done);
189 statdone = done;
190 }
191 if (stateta != eta) {
192 send_msg((HWND) atoi(gui_hwnd), WM_STATS_ETA, (WPARAM) eta);
193 stateta = eta;
194 }
195 if (statratebs != ratebs) {
196 send_msg((HWND) atoi(gui_hwnd), WM_STATS_RATEBS, (WPARAM) ratebs);
197 statratebs = ratebs;
198 }
32874aea 199 if (statelapsed != elapsed) {
200 send_msg((HWND) atoi(gui_hwnd), WM_STATS_ELAPSED,
201 (WPARAM) elapsed);
cc87246d 202 statelapsed = elapsed;
203 }
32874aea 204 if (statperct != percentage) {
205 send_msg((HWND) atoi(gui_hwnd), WM_STATS_PERCENT,
206 (WPARAM) percentage);
cc87246d 207 statperct = percentage;
208 }
209}
210
fb09bf1c 211/*
07d9aa13 212 * Print an error message and perform a fatal exit.
213 */
214void fatalbox(char *fmt, ...)
215{
32874aea 216 char str[0x100]; /* Make the size big enough */
c51a56e2 217 va_list ap;
218 va_start(ap, fmt);
120e4b40 219 strcpy(str, "Fatal: ");
32874aea 220 vsprintf(str + strlen(str), fmt, ap);
c51a56e2 221 va_end(ap);
cc87246d 222 strcat(str, "\n");
223 tell_str(stderr, str);
2bc6a386 224 errs++;
225
226 if (gui_mode) {
227 unsigned int msg_id = WM_RET_ERR_CNT;
228 if (list)
229 msg_id = WM_LS_RET_ERR_CNT;
230 while (!PostMessage
231 ((HWND) atoi(gui_hwnd), msg_id, (WPARAM) errs,
232 0 /*lParam */ ))SleepEx(1000, TRUE);
233 }
cc87246d 234
93b581bd 235 cleanup_exit(1);
07d9aa13 236}
8d5de777 237void connection_fatal(char *fmt, ...)
238{
32874aea 239 char str[0x100]; /* Make the size big enough */
8d5de777 240 va_list ap;
241 va_start(ap, fmt);
120e4b40 242 strcpy(str, "Fatal: ");
32874aea 243 vsprintf(str + strlen(str), fmt, ap);
8d5de777 244 va_end(ap);
245 strcat(str, "\n");
246 tell_str(stderr, str);
2bc6a386 247 errs++;
248
249 if (gui_mode) {
250 unsigned int msg_id = WM_RET_ERR_CNT;
251 if (list)
252 msg_id = WM_LS_RET_ERR_CNT;
253 while (!PostMessage
254 ((HWND) atoi(gui_hwnd), msg_id, (WPARAM) errs,
255 0 /*lParam */ ))SleepEx(1000, TRUE);
256 }
8d5de777 257
93b581bd 258 cleanup_exit(1);
8d5de777 259}
07d9aa13 260
07d9aa13 261/*
8df7a775 262 * Be told what socket we're supposed to be using.
263 */
264static SOCKET scp_ssh_socket;
32874aea 265char *do_select(SOCKET skt, int startup)
266{
8df7a775 267 if (startup)
268 scp_ssh_socket = skt;
269 else
270 scp_ssh_socket = INVALID_SOCKET;
271 return NULL;
272}
273extern int select_result(WPARAM, LPARAM);
274
275/*
3bdaf79d 276 * Receive a block of data from the SSH link. Block until all data
277 * is available.
278 *
279 * To do this, we repeatedly call the SSH protocol module, with our
fe50e814 280 * own trap in from_backend() to catch the data that comes back. We
281 * do this until we have enough data.
3bdaf79d 282 */
8df7a775 283
32874aea 284static unsigned char *outptr; /* where to put the data */
285static unsigned outlen; /* how much data required */
3bdaf79d 286static unsigned char *pending = NULL; /* any spare data */
32874aea 287static unsigned pendlen = 0, pendsize = 0; /* length and phys. size of buffer */
5471d09a 288int from_backend(int is_stderr, char *data, int datalen)
32874aea 289{
290 unsigned char *p = (unsigned char *) data;
291 unsigned len = (unsigned) datalen;
fe50e814 292
2b0c045b 293 assert(len > 0);
294
3bdaf79d 295 /*
fe50e814 296 * stderr data is just spouted to local stderr and otherwise
297 * ignored.
3bdaf79d 298 */
fe50e814 299 if (is_stderr) {
300 fwrite(data, 1, len, stderr);
5471d09a 301 return 0;
fe50e814 302 }
3bdaf79d 303
3bdaf79d 304 /*
305 * If this is before the real session begins, just return.
306 */
307 if (!outptr)
5471d09a 308 return 0;
3bdaf79d 309
310 if (outlen > 0) {
32874aea 311 unsigned used = outlen;
312 if (used > len)
313 used = len;
314 memcpy(outptr, p, used);
315 outptr += used;
316 outlen -= used;
317 p += used;
318 len -= used;
3bdaf79d 319 }
320
321 if (len > 0) {
32874aea 322 if (pendsize < pendlen + len) {
323 pendsize = pendlen + len + 4096;
324 pending = (pending ? srealloc(pending, pendsize) :
325 smalloc(pendsize));
326 if (!pending)
327 fatalbox("Out of memory");
328 }
329 memcpy(pending + pendlen, p, len);
330 pendlen += len;
3bdaf79d 331 }
5471d09a 332
333 return 0;
334}
335static int scp_process_network_event(void)
336{
337 fd_set readfds;
338
339 FD_ZERO(&readfds);
340 FD_SET(scp_ssh_socket, &readfds);
341 if (select(1, &readfds, NULL, NULL, NULL) < 0)
342 return 0; /* doom */
343 select_result((WPARAM) scp_ssh_socket, (LPARAM) FD_READ);
344 return 1;
3bdaf79d 345}
32874aea 346static int ssh_scp_recv(unsigned char *buf, int len)
347{
3bdaf79d 348 outptr = buf;
349 outlen = len;
350
351 /*
352 * See if the pending-input block contains some of what we
353 * need.
354 */
355 if (pendlen > 0) {
32874aea 356 unsigned pendused = pendlen;
357 if (pendused > outlen)
358 pendused = outlen;
3bdaf79d 359 memcpy(outptr, pending, pendused);
32874aea 360 memmove(pending, pending + pendused, pendlen - pendused);
3bdaf79d 361 outptr += pendused;
362 outlen -= pendused;
32874aea 363 pendlen -= pendused;
364 if (pendlen == 0) {
365 pendsize = 0;
366 sfree(pending);
367 pending = NULL;
368 }
369 if (outlen == 0)
370 return len;
3bdaf79d 371 }
372
373 while (outlen > 0) {
5471d09a 374 if (!scp_process_network_event())
32874aea 375 return 0; /* doom */
3bdaf79d 376 }
377
378 return len;
379}
380
381/*
382 * Loop through the ssh connection and authentication process.
383 */
32874aea 384static void ssh_scp_init(void)
385{
8df7a775 386 if (scp_ssh_socket == INVALID_SOCKET)
3bdaf79d 387 return;
388 while (!back->sendok()) {
32874aea 389 fd_set readfds;
390 FD_ZERO(&readfds);
391 FD_SET(scp_ssh_socket, &readfds);
392 if (select(1, &readfds, NULL, NULL, NULL) < 0)
393 return; /* doom */
394 select_result((WPARAM) scp_ssh_socket, (LPARAM) FD_READ);
3bdaf79d 395 }
fd5e5847 396 using_sftp = !ssh_fallback_cmd;
3bdaf79d 397}
398
399/*
07d9aa13 400 * Print an error message and exit after closing the SSH link.
401 */
402static void bump(char *fmt, ...)
403{
32874aea 404 char str[0x100]; /* Make the size big enough */
c51a56e2 405 va_list ap;
406 va_start(ap, fmt);
120e4b40 407 strcpy(str, "Fatal: ");
32874aea 408 vsprintf(str + strlen(str), fmt, ap);
c51a56e2 409 va_end(ap);
cc87246d 410 strcat(str, "\n");
411 tell_str(stderr, str);
2bc6a386 412 errs++;
cc87246d 413
eba78553 414 if (back != NULL && back->socket() != NULL) {
c51a56e2 415 char ch;
3bdaf79d 416 back->special(TS_EOF);
fb09bf1c 417 ssh_scp_recv(&ch, 1);
c51a56e2 418 }
2bc6a386 419
420 if (gui_mode) {
421 unsigned int msg_id = WM_RET_ERR_CNT;
422 if (list)
423 msg_id = WM_LS_RET_ERR_CNT;
424 while (!PostMessage
425 ((HWND) atoi(gui_hwnd), msg_id, (WPARAM) errs,
426 0 /*lParam */ ))SleepEx(1000, TRUE);
427 }
428
93b581bd 429 cleanup_exit(1);
07d9aa13 430}
431
07d9aa13 432/*
433 * Open an SSH connection to user@host and execute cmd.
434 */
435static void do_cmd(char *host, char *user, char *cmd)
436{
c51a56e2 437 char *err, *realhost;
f5e6a5c6 438 DWORD namelen;
c51a56e2 439
440 if (host == NULL || host[0] == '\0')
441 bump("Empty host name");
442
443 /* Try to load settings for this host */
a9422f39 444 do_defaults(host, &cfg);
c51a56e2 445 if (cfg.host[0] == '\0') {
446 /* No settings for this host; use defaults */
32874aea 447 do_defaults(NULL, &cfg);
448 strncpy(cfg.host, host, sizeof(cfg.host) - 1);
449 cfg.host[sizeof(cfg.host) - 1] = '\0';
4db4f6a6 450 }
451
452 /*
453 * Force use of SSH. (If they got the protocol wrong we assume the
454 * port is useless too.)
455 */
456 if (cfg.protocol != PROT_SSH) {
457 cfg.protocol = PROT_SSH;
458 cfg.port = 22;
c51a56e2 459 }
460
449925a6 461 /*
c0a81592 462 * Enact command-line overrides.
463 */
464 cmdline_run_saved();
465
466 /*
449925a6 467 * Trim leading whitespace off the hostname if it's there.
468 */
469 {
470 int space = strspn(cfg.host, " \t");
471 memmove(cfg.host, cfg.host+space, 1+strlen(cfg.host)-space);
472 }
473
474 /* See if host is of the form user@host */
475 if (cfg.host[0] != '\0') {
476 char *atsign = strchr(cfg.host, '@');
477 /* Make sure we're not overflowing the user field */
478 if (atsign) {
479 if (atsign - cfg.host < sizeof cfg.username) {
480 strncpy(cfg.username, cfg.host, atsign - cfg.host);
481 cfg.username[atsign - cfg.host] = '\0';
482 }
483 memmove(cfg.host, atsign + 1, 1 + strlen(atsign + 1));
484 }
485 }
486
487 /*
488 * Trim a colon suffix off the hostname if it's there.
489 */
490 cfg.host[strcspn(cfg.host, ":")] = '\0';
491
c51a56e2 492 /* Set username */
493 if (user != NULL && user[0] != '\0') {
32874aea 494 strncpy(cfg.username, user, sizeof(cfg.username) - 1);
495 cfg.username[sizeof(cfg.username) - 1] = '\0';
c51a56e2 496 } else if (cfg.username[0] == '\0') {
f5e6a5c6 497 namelen = 0;
498 if (GetUserName(user, &namelen) == FALSE)
499 bump("Empty user name");
93558b21 500 user = smalloc(namelen * sizeof(char));
f5e6a5c6 501 GetUserName(user, &namelen);
32874aea 502 if (verbose)
503 tell_user(stderr, "Guessing user name: %s", user);
504 strncpy(cfg.username, user, sizeof(cfg.username) - 1);
505 cfg.username[sizeof(cfg.username) - 1] = '\0';
f5e6a5c6 506 free(user);
c51a56e2 507 }
508
fd5e5847 509 /*
d27b4a18 510 * Disable scary things which shouldn't be enabled for simple
511 * things like SCP and SFTP: agent forwarding, port forwarding,
512 * X forwarding.
513 */
514 cfg.x11_forward = 0;
515 cfg.agentfwd = 0;
516 cfg.portfwd[0] = cfg.portfwd[1] = '\0';
517
518 /*
fd5e5847 519 * Attempt to start the SFTP subsystem as a first choice,
520 * falling back to the provided scp command if that fails.
521 */
522 strcpy(cfg.remote_cmd, "sftp");
523 cfg.ssh_subsys = TRUE;
524 cfg.remote_cmd_ptr2 = cmd;
525 cfg.ssh_subsys2 = FALSE;
3bdaf79d 526 cfg.nopty = TRUE;
527
528 back = &ssh_backend;
529
2184a5d9 530 err = back->init(cfg.host, cfg.port, &realhost, 0);
c51a56e2 531 if (err != NULL)
532 bump("ssh_init: %s", err);
3bdaf79d 533 ssh_scp_init();
c51a56e2 534 if (verbose && realhost != NULL)
cc87246d 535 tell_user(stderr, "Connected to %s\n", realhost);
6e1ebb76 536 sfree(realhost);
07d9aa13 537}
538
07d9aa13 539/*
540 * Update statistic information about current file.
541 */
542static void print_stats(char *name, unsigned long size, unsigned long done,
32874aea 543 time_t start, time_t now)
07d9aa13 544{
c51a56e2 545 float ratebs;
546 unsigned long eta;
547 char etastr[10];
548 int pct;
b1daf518 549 int len;
d524be1c 550 int elap;
c51a56e2 551
d524be1c 552 elap = (unsigned long) difftime(now, start);
c51a56e2 553
d524be1c 554 if (now > start)
555 ratebs = (float) done / elap;
556 else
557 ratebs = (float) done;
558
559 if (ratebs < 1.0)
560 eta = size - done;
561 else
562 eta = (unsigned long) ((size - done) / ratebs);
563 sprintf(etastr, "%02ld:%02ld:%02ld",
564 eta / 3600, (eta % 3600) / 60, eta % 60);
c51a56e2 565
d524be1c 566 pct = (int) (100 * (done * 1.0 / size));
c51a56e2 567
d524be1c 568 if (gui_mode)
569 /* GUI Adaptation - Sept 2000 */
570 gui_update_stats(name, size, pct, elap, done, eta,
571 (unsigned long) ratebs);
572 else {
b1daf518 573 len = printf("\r%-25.25s | %10ld kB | %5.1f kB/s | ETA: %8s | %3d%%",
574 name, done / 1024, ratebs / 1024.0, etastr, pct);
575 if (len < prev_stats_len)
576 printf("%*s", prev_stats_len - len, "");
577 prev_stats_len = len;
c51a56e2 578
cc87246d 579 if (done == size)
580 printf("\n");
581 }
07d9aa13 582}
583
07d9aa13 584/*
585 * Find a colon in str and return a pointer to the colon.
39ddf0ff 586 * This is used to separate hostname from filename.
07d9aa13 587 */
32874aea 588static char *colon(char *str)
07d9aa13 589{
c51a56e2 590 /* We ignore a leading colon, since the hostname cannot be
32874aea 591 empty. We also ignore a colon as second character because
592 of filenames like f:myfile.txt. */
593 if (str[0] == '\0' || str[0] == ':' || str[1] == ':')
c51a56e2 594 return (NULL);
32874aea 595 while (*str != '\0' && *str != ':' && *str != '/' && *str != '\\')
c51a56e2 596 str++;
597 if (*str == ':')
598 return (str);
599 else
600 return (NULL);
07d9aa13 601}
602
07d9aa13 603/*
03f64569 604 * Return a pointer to the portion of str that comes after the last
b3dcd9b2 605 * slash (or backslash or colon, if `local' is TRUE).
03f64569 606 */
4eb24e3a 607static char *stripslashes(char *str, int local)
03f64569 608{
609 char *p;
610
b3dcd9b2 611 if (local) {
612 p = strchr(str, ':');
613 if (p) str = p+1;
614 }
615
03f64569 616 p = strrchr(str, '/');
617 if (p) str = p+1;
618
4eb24e3a 619 if (local) {
620 p = strrchr(str, '\\');
621 if (p) str = p+1;
622 }
03f64569 623
624 return str;
625}
626
627/*
fd5e5847 628 * Determine whether a string is entirely composed of dots.
629 */
630static int is_dots(char *str)
631{
632 return str[strspn(str, ".")] == '\0';
633}
634
635/*
07d9aa13 636 * Wait for a response from the other side.
637 * Return 0 if ok, -1 if error.
638 */
639static int response(void)
640{
c51a56e2 641 char ch, resp, rbuf[2048];
642 int p;
643
fb09bf1c 644 if (ssh_scp_recv(&resp, 1) <= 0)
c51a56e2 645 bump("Lost connection");
646
647 p = 0;
648 switch (resp) {
32874aea 649 case 0: /* ok */
c51a56e2 650 return (0);
651 default:
652 rbuf[p++] = resp;
653 /* fallthrough */
32874aea 654 case 1: /* error */
655 case 2: /* fatal error */
c51a56e2 656 do {
fb09bf1c 657 if (ssh_scp_recv(&ch, 1) <= 0)
c51a56e2 658 bump("Protocol error: Lost connection");
659 rbuf[p++] = ch;
660 } while (p < sizeof(rbuf) && ch != '\n');
32874aea 661 rbuf[p - 1] = '\0';
c51a56e2 662 if (resp == 1)
cc87246d 663 tell_user(stderr, "%s\n", rbuf);
c51a56e2 664 else
665 bump("%s", rbuf);
666 errs++;
667 return (-1);
668 }
07d9aa13 669}
670
fd5e5847 671int sftp_recvdata(char *buf, int len)
672{
673 return ssh_scp_recv(buf, len);
674}
675int sftp_senddata(char *buf, int len)
676{
677 back->send((unsigned char *) buf, len);
678 return 1;
679}
680
681/* ----------------------------------------------------------------------
682 * sftp-based replacement for the hacky `pscp -ls'.
683 */
684static int sftp_ls_compare(const void *av, const void *bv)
685{
686 const struct fxp_name *a = (const struct fxp_name *) av;
687 const struct fxp_name *b = (const struct fxp_name *) bv;
688 return strcmp(a->filename, b->filename);
689}
690void scp_sftp_listdir(char *dirname)
691{
692 struct fxp_handle *dirh;
693 struct fxp_names *names;
694 struct fxp_name *ournames;
695 int nnames, namesize;
fd5e5847 696 int i;
697
9acdecb3 698 if (!fxp_init()) {
699 tell_user(stderr, "unable to initialise SFTP: %s", fxp_error());
700 errs++;
701 return;
702 }
703
fd5e5847 704 printf("Listing directory %s\n", dirname);
705
706 dirh = fxp_opendir(dirname);
707 if (dirh == NULL) {
cdcbdf3b 708 printf("Unable to open %s: %s\n", dirname, fxp_error());
fd5e5847 709 } else {
710 nnames = namesize = 0;
711 ournames = NULL;
712
713 while (1) {
714
715 names = fxp_readdir(dirh);
716 if (names == NULL) {
717 if (fxp_error_type() == SSH_FX_EOF)
718 break;
cdcbdf3b 719 printf("Reading directory %s: %s\n", dirname, fxp_error());
fd5e5847 720 break;
721 }
722 if (names->nnames == 0) {
723 fxp_free_names(names);
724 break;
725 }
726
727 if (nnames + names->nnames >= namesize) {
728 namesize += names->nnames + 128;
729 ournames =
730 srealloc(ournames, namesize * sizeof(*ournames));
731 }
732
733 for (i = 0; i < names->nnames; i++)
734 ournames[nnames++] = names->names[i];
735
736 names->nnames = 0; /* prevent free_names */
737 fxp_free_names(names);
738 }
739 fxp_close(dirh);
740
741 /*
742 * Now we have our filenames. Sort them by actual file
743 * name, and then output the longname parts.
744 */
745 qsort(ournames, nnames, sizeof(*ournames), sftp_ls_compare);
746
747 /*
748 * And print them.
749 */
750 for (i = 0; i < nnames; i++)
751 printf("%s\n", ournames[i].longname);
752 }
753}
754
120e4b40 755/* ----------------------------------------------------------------------
756 * Helper routines that contain the actual SCP protocol elements,
fd5e5847 757 * implemented both as SCP1 and SFTP.
120e4b40 758 */
759
fd5e5847 760static struct scp_sftp_dirstack {
761 struct scp_sftp_dirstack *next;
762 struct fxp_name *names;
763 int namepos, namelen;
764 char *dirpath;
4eb24e3a 765 char *wildcard;
825ec8ee 766 int matched_something; /* wildcard match set was non-empty */
fd5e5847 767} *scp_sftp_dirstack_head;
768static char *scp_sftp_remotepath, *scp_sftp_currentname;
4eb24e3a 769static char *scp_sftp_wildcard;
fd5e5847 770static int scp_sftp_targetisdir, scp_sftp_donethistarget;
771static int scp_sftp_preserve, scp_sftp_recursive;
772static unsigned long scp_sftp_mtime, scp_sftp_atime;
773static int scp_has_times;
774static struct fxp_handle *scp_sftp_filehandle;
775static uint64 scp_sftp_fileoffset;
776
777void scp_source_setup(char *target, int shouldbedir)
778{
779 if (using_sftp) {
780 /*
781 * Find out whether the target filespec is in fact a
782 * directory.
783 */
784 struct fxp_attrs attrs;
785
02105c79 786 if (!fxp_init()) {
787 tell_user(stderr, "unable to initialise SFTP: %s", fxp_error());
788 errs++;
789 return 1;
790 }
791
fd5e5847 792 if (!fxp_stat(target, &attrs) ||
793 !(attrs.flags & SSH_FILEXFER_ATTR_PERMISSIONS))
794 scp_sftp_targetisdir = 0;
795 else
796 scp_sftp_targetisdir = (attrs.permissions & 0040000) != 0;
797
798 if (shouldbedir && !scp_sftp_targetisdir) {
799 bump("pscp: remote filespec %s: not a directory\n", target);
800 }
801
802 scp_sftp_remotepath = dupstr(target);
803
804 scp_has_times = 0;
805 } else {
806 (void) response();
807 }
808}
809
120e4b40 810int scp_send_errmsg(char *str)
811{
fd5e5847 812 if (using_sftp) {
813 /* do nothing; we never need to send our errors to the server */
814 } else {
815 back->send("\001", 1); /* scp protocol error prefix */
816 back->send(str, strlen(str));
817 }
120e4b40 818 return 0; /* can't fail */
819}
820
821int scp_send_filetimes(unsigned long mtime, unsigned long atime)
822{
fd5e5847 823 if (using_sftp) {
824 scp_sftp_mtime = mtime;
825 scp_sftp_atime = atime;
826 scp_has_times = 1;
827 return 0;
828 } else {
829 char buf[80];
830 sprintf(buf, "T%lu 0 %lu 0\n", mtime, atime);
831 back->send(buf, strlen(buf));
832 return response();
833 }
120e4b40 834}
835
836int scp_send_filename(char *name, unsigned long size, int modes)
837{
fd5e5847 838 if (using_sftp) {
839 char *fullname;
840 if (scp_sftp_targetisdir) {
841 fullname = dupcat(scp_sftp_remotepath, "/", name, NULL);
842 } else {
843 fullname = dupstr(scp_sftp_remotepath);
844 }
845 scp_sftp_filehandle =
846 fxp_open(fullname, SSH_FXF_WRITE | SSH_FXF_CREAT | SSH_FXF_TRUNC);
847 if (!scp_sftp_filehandle) {
848 tell_user(stderr, "pscp: unable to open %s: %s",
849 fullname, fxp_error());
850 errs++;
851 return 1;
852 }
853 scp_sftp_fileoffset = uint64_make(0, 0);
854 sfree(fullname);
855 return 0;
856 } else {
857 char buf[40];
858 sprintf(buf, "C%04o %lu ", modes, size);
859 back->send(buf, strlen(buf));
860 back->send(name, strlen(name));
861 back->send("\n", 1);
862 return response();
863 }
120e4b40 864}
865
866int scp_send_filedata(char *data, int len)
867{
fd5e5847 868 if (using_sftp) {
869 if (!scp_sftp_filehandle) {
870 return 1;
871 }
872 if (!fxp_write(scp_sftp_filehandle, data, scp_sftp_fileoffset, len)) {
873 tell_user(stderr, "error while writing: %s\n", fxp_error());
874 errs++;
875 return 1;
876 }
877 scp_sftp_fileoffset = uint64_add32(scp_sftp_fileoffset, len);
878 return 0;
879 } else {
880 int bufsize = back->send(data, len);
120e4b40 881
fd5e5847 882 /*
883 * If the network transfer is backing up - that is, the
884 * remote site is not accepting data as fast as we can
885 * produce it - then we must loop on network events until
886 * we have space in the buffer again.
887 */
888 while (bufsize > MAX_SCP_BUFSIZE) {
889 if (!scp_process_network_event())
890 return 1;
891 bufsize = back->sendbuffer();
892 }
893
894 return 0;
895 }
896}
897
898int scp_send_finish(void)
899{
900 if (using_sftp) {
901 struct fxp_attrs attrs;
902 if (!scp_sftp_filehandle) {
120e4b40 903 return 1;
fd5e5847 904 }
905 if (scp_has_times) {
906 attrs.flags = SSH_FILEXFER_ATTR_ACMODTIME;
907 attrs.atime = scp_sftp_atime;
908 attrs.mtime = scp_sftp_mtime;
909 if (!fxp_fsetstat(scp_sftp_filehandle, attrs)) {
910 tell_user(stderr, "unable to set file times: %s\n", fxp_error());
911 errs++;
912 }
913 }
914 fxp_close(scp_sftp_filehandle);
915 scp_has_times = 0;
916 return 0;
917 } else {
918 back->send("", 1);
919 return response();
120e4b40 920 }
fd5e5847 921}
120e4b40 922
fd5e5847 923char *scp_save_remotepath(void)
924{
925 if (using_sftp)
926 return scp_sftp_remotepath;
927 else
928 return NULL;
120e4b40 929}
930
fd5e5847 931void scp_restore_remotepath(char *data)
120e4b40 932{
fd5e5847 933 if (using_sftp)
934 scp_sftp_remotepath = data;
120e4b40 935}
936
937int scp_send_dirname(char *name, int modes)
938{
fd5e5847 939 if (using_sftp) {
940 char *fullname;
941 char const *err;
942 struct fxp_attrs attrs;
943 if (scp_sftp_targetisdir) {
944 fullname = dupcat(scp_sftp_remotepath, "/", name, NULL);
945 } else {
946 fullname = dupstr(scp_sftp_remotepath);
947 }
948
949 /*
950 * We don't worry about whether we managed to create the
951 * directory, because if it exists already it's OK just to
952 * use it. Instead, we will stat it afterwards, and if it
953 * exists and is a directory we will assume we were either
954 * successful or it didn't matter.
955 */
956 if (!fxp_mkdir(fullname))
957 err = fxp_error();
958 else
959 err = "server reported no error";
960 if (!fxp_stat(fullname, &attrs) ||
961 !(attrs.flags & SSH_FILEXFER_ATTR_PERMISSIONS) ||
962 !(attrs.permissions & 0040000)) {
963 tell_user(stderr, "unable to create directory %s: %s",
964 fullname, err);
965 errs++;
966 return 1;
967 }
968
969 scp_sftp_remotepath = fullname;
970
971 return 0;
972 } else {
973 char buf[40];
974 sprintf(buf, "D%04o 0 ", modes);
975 back->send(buf, strlen(buf));
976 back->send(name, strlen(name));
977 back->send("\n", 1);
978 return response();
979 }
120e4b40 980}
981
982int scp_send_enddir(void)
983{
fd5e5847 984 if (using_sftp) {
985 sfree(scp_sftp_remotepath);
986 return 0;
987 } else {
988 back->send("E\n", 2);
989 return response();
990 }
991}
992
993/*
994 * Yes, I know; I have an scp_sink_setup _and_ an scp_sink_init.
995 * That's bad. The difference is that scp_sink_setup is called once
996 * right at the start, whereas scp_sink_init is called to
997 * initialise every level of recursion in the protocol.
998 */
4eb24e3a 999int scp_sink_setup(char *source, int preserve, int recursive)
fd5e5847 1000{
1001 if (using_sftp) {
4eb24e3a 1002 char *newsource;
02105c79 1003
1004 if (!fxp_init()) {
1005 tell_user(stderr, "unable to initialise SFTP: %s", fxp_error());
1006 errs++;
1007 return 1;
1008 }
4eb24e3a 1009 /*
1010 * It's possible that the source string we've been given
1011 * contains a wildcard. If so, we must split the directory
1012 * away from the wildcard itself (throwing an error if any
1013 * wildcardness comes before the final slash) and arrange
1014 * things so that a dirstack entry will be set up.
1015 */
1016 newsource = smalloc(1+strlen(source));
1017 if (!wc_unescape(newsource, source)) {
1018 /* Yes, here we go; it's a wildcard. Bah. */
1019 char *dupsource, *lastpart, *dirpart, *wildcard;
1020 dupsource = dupstr(source);
1021 lastpart = stripslashes(dupsource, 0);
1022 wildcard = dupstr(lastpart);
1023 *lastpart = '\0';
1024 if (*dupsource && dupsource[1]) {
1025 /*
1026 * The remains of dupsource are at least two
1027 * characters long, meaning the pathname wasn't
1028 * empty or just `/'. Hence, we remove the trailing
1029 * slash.
1030 */
1031 lastpart[-1] = '\0';
6b18a524 1032 } else if (!*dupsource) {
1033 /*
1034 * The remains of dupsource are _empty_ - the whole
1035 * pathname was a wildcard. Hence we need to
1036 * replace it with ".".
1037 */
1038 sfree(dupsource);
1039 dupsource = dupstr(".");
4eb24e3a 1040 }
1041
1042 /*
1043 * Now we have separated our string into dupsource (the
1044 * directory part) and wildcard. Both of these will
1045 * need freeing at some point. Next step is to remove
1046 * wildcard escapes from the directory part, throwing
1047 * an error if it contains a real wildcard.
1048 */
1049 dirpart = smalloc(1+strlen(dupsource));
1050 if (!wc_unescape(dirpart, dupsource)) {
1051 tell_user(stderr, "%s: multiple-level wildcards unsupported",
1052 source);
1053 errs++;
1054 sfree(dirpart);
1055 sfree(wildcard);
1056 sfree(dupsource);
1057 return 1;
1058 }
1059
1060 /*
1061 * Now we have dirpart (unescaped, ie a valid remote
1062 * path), and wildcard (a wildcard). This will be
1063 * sufficient to arrange a dirstack entry.
1064 */
1065 scp_sftp_remotepath = dirpart;
1066 scp_sftp_wildcard = wildcard;
1067 sfree(dupsource);
1068 } else {
1069 scp_sftp_remotepath = newsource;
1070 scp_sftp_wildcard = NULL;
1071 }
fd5e5847 1072 scp_sftp_preserve = preserve;
1073 scp_sftp_recursive = recursive;
1074 scp_sftp_donethistarget = 0;
1075 scp_sftp_dirstack_head = NULL;
1076 }
4eb24e3a 1077 return 0;
120e4b40 1078}
1079
1080int scp_sink_init(void)
1081{
fd5e5847 1082 if (!using_sftp) {
1083 back->send("", 1);
1084 }
120e4b40 1085 return 0;
1086}
1087
1088#define SCP_SINK_FILE 1
1089#define SCP_SINK_DIR 2
1090#define SCP_SINK_ENDDIR 3
4eb24e3a 1091#define SCP_SINK_RETRY 4 /* not an action; just try again */
120e4b40 1092struct scp_sink_action {
1093 int action; /* FILE, DIR, ENDDIR */
1094 char *buf; /* will need freeing after use */
1095 char *name; /* filename or dirname (not ENDDIR) */
1096 int mode; /* access mode (not ENDDIR) */
1097 unsigned long size; /* file size (not ENDDIR) */
1098 int settime; /* 1 if atime and mtime are filled */
1099 unsigned long atime, mtime; /* access times for the file */
1100};
1101
1102int scp_get_sink_action(struct scp_sink_action *act)
1103{
fd5e5847 1104 if (using_sftp) {
1105 char *fname;
1106 int must_free_fname;
1107 struct fxp_attrs attrs;
1108 int ret;
1109
1110 if (!scp_sftp_dirstack_head) {
1111 if (!scp_sftp_donethistarget) {
1112 /*
1113 * Simple case: we are only dealing with one file.
1114 */
1115 fname = scp_sftp_remotepath;
1116 must_free_fname = 0;
1117 scp_sftp_donethistarget = 1;
1118 } else {
1119 /*
1120 * Even simpler case: one file _which we've done_.
1121 * Return 1 (finished).
1122 */
1123 return 1;
1124 }
1125 } else {
1126 /*
1127 * We're now in the middle of stepping through a list
1128 * of names returned from fxp_readdir(); so let's carry
1129 * on.
1130 */
1131 struct scp_sftp_dirstack *head = scp_sftp_dirstack_head;
1132 while (head->namepos < head->namelen &&
4eb24e3a 1133 (is_dots(head->names[head->namepos].filename) ||
1134 (head->wildcard &&
1135 !wc_match(head->wildcard,
1136 head->names[head->namepos].filename))))
fd5e5847 1137 head->namepos++; /* skip . and .. */
1138 if (head->namepos < head->namelen) {
825ec8ee 1139 head->matched_something = 1;
fd5e5847 1140 fname = dupcat(head->dirpath, "/",
1141 head->names[head->namepos++].filename,
1142 NULL);
1143 must_free_fname = 1;
1144 } else {
1145 /*
1146 * We've come to the end of the list; pop it off
4eb24e3a 1147 * the stack and return an ENDDIR action (or RETRY
1148 * if this was a wildcard match).
fd5e5847 1149 */
4eb24e3a 1150 if (head->wildcard) {
1151 act->action = SCP_SINK_RETRY;
825ec8ee 1152 if (!head->matched_something) {
1153 tell_user(stderr, "pscp: wildcard '%s' matched "
1154 "no files", head->wildcard);
1155 errs++;
1156 }
4eb24e3a 1157 sfree(head->wildcard);
825ec8ee 1158
4eb24e3a 1159 } else {
1160 act->action = SCP_SINK_ENDDIR;
1161 }
1162
fd5e5847 1163 sfree(head->dirpath);
1164 sfree(head->names);
1165 scp_sftp_dirstack_head = head->next;
1166 sfree(head);
1167
fd5e5847 1168 return 0;
1169 }
1170 }
cd1f39ab 1171
fd5e5847 1172 /*
1173 * Now we have a filename. Stat it, and see if it's a file
1174 * or a directory.
1175 */
1176 ret = fxp_stat(fname, &attrs);
1177 if (!ret || !(attrs.flags & SSH_FILEXFER_ATTR_PERMISSIONS)) {
1178 tell_user(stderr, "unable to identify %s: %s", fname,
1179 ret ? "file type not supplied" : fxp_error());
1180 errs++;
120e4b40 1181 return 1;
fd5e5847 1182 }
1183
1184 if (attrs.permissions & 0040000) {
1185 struct scp_sftp_dirstack *newitem;
1186 struct fxp_handle *dirhandle;
1187 int nnames, namesize;
1188 struct fxp_name *ournames;
1189 struct fxp_names *names;
1190
1191 /*
37dfb97a 1192 * It's a directory. If we're not in recursive mode,
1193 * this merits a complaint (which is fatal if the name
1194 * was specified directly, but not if it was matched by
1195 * a wildcard).
1196 *
1197 * We skip this complaint completely if
1198 * scp_sftp_wildcard is set, because that's an
1199 * indication that we're not actually supposed to
1200 * _recursively_ transfer the dir, just scan it for
1201 * things matching the wildcard.
fd5e5847 1202 */
4eb24e3a 1203 if (!scp_sftp_recursive && !scp_sftp_wildcard) {
fd5e5847 1204 tell_user(stderr, "pscp: %s: is a directory", fname);
1205 errs++;
1206 if (must_free_fname) sfree(fname);
37dfb97a 1207 if (scp_sftp_dirstack_head) {
1208 act->action = SCP_SINK_RETRY;
1209 return 0;
1210 } else {
1211 return 1;
1212 }
120e4b40 1213 }
fd5e5847 1214
1215 /*
1216 * Otherwise, the fun begins. We must fxp_opendir() the
1217 * directory, slurp the filenames into memory, return
4eb24e3a 1218 * SCP_SINK_DIR (unless this is a wildcard match), and
1219 * set targetisdir. The next time we're called, we will
1220 * run through the list of filenames one by one,
1221 * matching them against a wildcard if present.
fd5e5847 1222 *
1223 * If targetisdir is _already_ set (meaning we're
1224 * already in the middle of going through another such
1225 * list), we must push the other (target,namelist) pair
1226 * on a stack.
1227 */
1228 dirhandle = fxp_opendir(fname);
1229 if (!dirhandle) {
1230 tell_user(stderr, "scp: unable to open directory %s: %s",
1231 fname, fxp_error());
1232 if (must_free_fname) sfree(fname);
1233 errs++;
1234 return 1;
1235 }
1236 nnames = namesize = 0;
1237 ournames = NULL;
1238 while (1) {
1239 int i;
1240
1241 names = fxp_readdir(dirhandle);
1242 if (names == NULL) {
1243 if (fxp_error_type() == SSH_FX_EOF)
1244 break;
1245 tell_user(stderr, "scp: reading directory %s: %s\n",
1246 fname, fxp_error());
1247 if (must_free_fname) sfree(fname);
1248 sfree(ournames);
1249 errs++;
1250 return 1;
1251 }
1252 if (names->nnames == 0) {
1253 fxp_free_names(names);
1254 break;
1255 }
1256 if (nnames + names->nnames >= namesize) {
1257 namesize += names->nnames + 128;
1258 ournames =
1259 srealloc(ournames, namesize * sizeof(*ournames));
1260 }
1261 for (i = 0; i < names->nnames; i++)
1262 ournames[nnames++] = names->names[i];
1263 names->nnames = 0; /* prevent free_names */
1264 fxp_free_names(names);
1265 }
1266 fxp_close(dirhandle);
1267
1268 newitem = smalloc(sizeof(struct scp_sftp_dirstack));
1269 newitem->next = scp_sftp_dirstack_head;
1270 newitem->names = ournames;
1271 newitem->namepos = 0;
1272 newitem->namelen = nnames;
1273 if (must_free_fname)
1274 newitem->dirpath = fname;
1275 else
1276 newitem->dirpath = dupstr(fname);
4eb24e3a 1277 if (scp_sftp_wildcard) {
1278 newitem->wildcard = scp_sftp_wildcard;
825ec8ee 1279 newitem->matched_something = 0;
4eb24e3a 1280 scp_sftp_wildcard = NULL;
1281 } else {
1282 newitem->wildcard = NULL;
1283 }
fd5e5847 1284 scp_sftp_dirstack_head = newitem;
1285
4eb24e3a 1286 if (newitem->wildcard) {
1287 act->action = SCP_SINK_RETRY;
1288 } else {
1289 act->action = SCP_SINK_DIR;
1290 act->buf = dupstr(stripslashes(fname, 0));
1291 act->name = act->buf;
1292 act->size = 0; /* duhh, it's a directory */
1293 act->mode = 07777 & attrs.permissions;
1294 if (scp_sftp_preserve &&
1295 (attrs.flags & SSH_FILEXFER_ATTR_ACMODTIME)) {
1296 act->atime = attrs.atime;
1297 act->mtime = attrs.mtime;
1298 act->settime = 1;
1299 } else
1300 act->settime = 0;
1301 }
120e4b40 1302 return 0;
fd5e5847 1303
1304 } else {
1305 /*
1306 * It's a file. Return SCP_SINK_FILE.
1307 */
1308 act->action = SCP_SINK_FILE;
4eb24e3a 1309 act->buf = dupstr(stripslashes(fname, 0));
fd5e5847 1310 act->name = act->buf;
1311 if (attrs.flags & SSH_FILEXFER_ATTR_SIZE) {
1312 if (uint64_compare(attrs.size,
1313 uint64_make(0, ULONG_MAX)) > 0) {
1314 act->size = ULONG_MAX; /* *boggle* */
1315 } else
1316 act->size = attrs.size.lo;
1317 } else
1318 act->size = ULONG_MAX; /* no idea */
1319 act->mode = 07777 & attrs.permissions;
1320 if (scp_sftp_preserve &&
1321 (attrs.flags & SSH_FILEXFER_ATTR_ACMODTIME)) {
1322 act->atime = attrs.atime;
1323 act->mtime = attrs.mtime;
120e4b40 1324 act->settime = 1;
fd5e5847 1325 } else
1326 act->settime = 0;
1327 if (must_free_fname)
1328 scp_sftp_currentname = fname;
1329 else
1330 scp_sftp_currentname = dupstr(fname);
1331 return 0;
1332 }
1333
1334 } else {
1335 int done = 0;
1336 int i, bufsize;
1337 int action;
1338 char ch;
1339
1340 act->settime = 0;
1341 act->buf = NULL;
1342 bufsize = 0;
1343
1344 while (!done) {
1345 if (ssh_scp_recv(&ch, 1) <= 0)
1346 return 1;
1347 if (ch == '\n')
1348 bump("Protocol error: Unexpected newline");
1349 i = 0;
1350 action = ch;
1351 do {
1352 if (ssh_scp_recv(&ch, 1) <= 0)
1353 bump("Lost connection");
1354 if (i >= bufsize) {
1355 bufsize = i + 128;
1356 act->buf = srealloc(act->buf, bufsize);
1357 }
1358 act->buf[i++] = ch;
1359 } while (ch != '\n');
1360 act->buf[i - 1] = '\0';
1361 switch (action) {
1362 case '\01': /* error */
1363 tell_user(stderr, "%s\n", act->buf);
1364 errs++;
1365 continue; /* go round again */
1366 case '\02': /* fatal error */
1367 bump("%s", act->buf);
1368 case 'E':
120e4b40 1369 back->send("", 1);
fd5e5847 1370 act->action = SCP_SINK_ENDDIR;
1371 return 0;
1372 case 'T':
1373 if (sscanf(act->buf, "%ld %*d %ld %*d",
1374 &act->mtime, &act->atime) == 2) {
1375 act->settime = 1;
1376 back->send("", 1);
1377 continue; /* go round again */
1378 }
1379 bump("Protocol error: Illegal time format");
1380 case 'C':
1381 case 'D':
1382 act->action = (action == 'C' ? SCP_SINK_FILE : SCP_SINK_DIR);
1383 break;
1384 default:
1385 bump("Protocol error: Expected control record");
120e4b40 1386 }
fd5e5847 1387 /*
1388 * We will go round this loop only once, unless we hit
1389 * `continue' above.
1390 */
1391 done = 1;
120e4b40 1392 }
fd5e5847 1393
120e4b40 1394 /*
fd5e5847 1395 * If we get here, we must have seen SCP_SINK_FILE or
1396 * SCP_SINK_DIR.
120e4b40 1397 */
fd5e5847 1398 if (sscanf(act->buf, "%o %lu %n", &act->mode, &act->size, &i) != 2)
1399 bump("Protocol error: Illegal file descriptor format");
1400 act->name = act->buf + i;
1401 return 0;
120e4b40 1402 }
120e4b40 1403}
1404
1405int scp_accept_filexfer(void)
1406{
fd5e5847 1407 if (using_sftp) {
1408 scp_sftp_filehandle =
1409 fxp_open(scp_sftp_currentname, SSH_FXF_READ);
1410 if (!scp_sftp_filehandle) {
1411 tell_user(stderr, "pscp: unable to open %s: %s",
1412 scp_sftp_currentname, fxp_error());
1413 errs++;
1414 return 1;
1415 }
1416 scp_sftp_fileoffset = uint64_make(0, 0);
1417 sfree(scp_sftp_currentname);
1418 return 0;
1419 } else {
1420 back->send("", 1);
1421 return 0; /* can't fail */
1422 }
120e4b40 1423}
1424
1425int scp_recv_filedata(char *data, int len)
1426{
fd5e5847 1427 if (using_sftp) {
1428 int actuallen = fxp_read(scp_sftp_filehandle, data,
1429 scp_sftp_fileoffset, len);
1430 if (actuallen == -1 && fxp_error_type() != SSH_FX_EOF) {
1431 tell_user(stderr, "pscp: error while reading: %s", fxp_error());
1432 errs++;
1433 return -1;
1434 }
1435 if (actuallen < 0)
1436 actuallen = 0;
1437
1438 scp_sftp_fileoffset = uint64_add32(scp_sftp_fileoffset, actuallen);
1439
1440 return actuallen;
1441 } else {
1442 return ssh_scp_recv(data, len);
1443 }
120e4b40 1444}
1445
1446int scp_finish_filerecv(void)
1447{
fd5e5847 1448 if (using_sftp) {
1449 fxp_close(scp_sftp_filehandle);
1450 return 0;
1451 } else {
1452 back->send("", 1);
1453 return response();
1454 }
120e4b40 1455}
1456
1457/* ----------------------------------------------------------------------
07d9aa13 1458 * Send an error message to the other side and to the screen.
1459 * Increment error counter.
1460 */
1461static void run_err(const char *fmt, ...)
1462{
c51a56e2 1463 char str[2048];
1464 va_list ap;
1465 va_start(ap, fmt);
1466 errs++;
9520eba8 1467 strcpy(str, "scp: ");
32874aea 1468 vsprintf(str + strlen(str), fmt, ap);
c51a56e2 1469 strcat(str, "\n");
120e4b40 1470 scp_send_errmsg(str);
32874aea 1471 tell_user(stderr, "%s", str);
c51a56e2 1472 va_end(ap);
07d9aa13 1473}
1474
07d9aa13 1475/*
1476 * Execute the source part of the SCP protocol.
1477 */
1478static void source(char *src)
1479{
c51a56e2 1480 unsigned long size;
1481 char *last;
1482 HANDLE f;
1483 DWORD attr;
1484 unsigned long i;
1485 unsigned long stat_bytes;
1486 time_t stat_starttime, stat_lasttime;
1487
1488 attr = GetFileAttributes(src);
32874aea 1489 if (attr == (DWORD) - 1) {
c51a56e2 1490 run_err("%s: No such file or directory", src);
1491 return;
1492 }
1493
1494 if ((attr & FILE_ATTRIBUTE_DIRECTORY) != 0) {
7f1f80de 1495 if (recursive) {
32874aea 1496 /*
1497 * Avoid . and .. directories.
1498 */
1499 char *p;
1500 p = strrchr(src, '/');
1501 if (!p)
1502 p = strrchr(src, '\\');
1503 if (!p)
1504 p = src;
1505 else
1506 p++;
1507 if (!strcmp(p, ".") || !strcmp(p, ".."))
1508 /* skip . and .. */ ;
1509 else
1510 rsource(src);
1511 } else {
c51a56e2 1512 run_err("%s: not a regular file", src);
32874aea 1513 }
c51a56e2 1514 return;
1515 }
1516
1517 if ((last = strrchr(src, '/')) == NULL)
1518 last = src;
1519 else
1520 last++;
1521 if (strrchr(last, '\\') != NULL)
1522 last = strrchr(last, '\\') + 1;
1523 if (last == src && strchr(src, ':') != NULL)
1524 last = strchr(src, ':') + 1;
1525
1526 f = CreateFile(src, GENERIC_READ, FILE_SHARE_READ, NULL,
1527 OPEN_EXISTING, 0, 0);
1528 if (f == INVALID_HANDLE_VALUE) {
486543a1 1529 run_err("%s: Cannot open file", src);
c51a56e2 1530 return;
1531 }
1532
1533 if (preserve) {
1534 FILETIME actime, wrtime;
1535 unsigned long mtime, atime;
1536 GetFileTime(f, NULL, &actime, &wrtime);
1537 TIME_WIN_TO_POSIX(actime, atime);
1538 TIME_WIN_TO_POSIX(wrtime, mtime);
120e4b40 1539 if (scp_send_filetimes(mtime, atime))
c51a56e2 1540 return;
1541 }
1542
1543 size = GetFileSize(f, NULL);
c51a56e2 1544 if (verbose)
120e4b40 1545 tell_user(stderr, "Sending file %s, size=%lu", last, size);
1546 if (scp_send_filename(last, size, 0644))
c51a56e2 1547 return;
1548
2d466ffd 1549 stat_bytes = 0;
1550 stat_starttime = time(NULL);
1551 stat_lasttime = 0;
c51a56e2 1552
1553 for (i = 0; i < size; i += 4096) {
1554 char transbuf[4096];
1555 DWORD j, k = 4096;
5471d09a 1556
32874aea 1557 if (i + k > size)
1558 k = size - i;
1559 if (!ReadFile(f, transbuf, k, &j, NULL) || j != k) {
1560 if (statistics)
1561 printf("\n");
c51a56e2 1562 bump("%s: Read error", src);
07d9aa13 1563 }
120e4b40 1564 if (scp_send_filedata(transbuf, k))
1565 bump("%s: Network error occurred", src);
1566
c51a56e2 1567 if (statistics) {
1568 stat_bytes += k;
32874aea 1569 if (time(NULL) != stat_lasttime || i + k == size) {
c51a56e2 1570 stat_lasttime = time(NULL);
1571 print_stats(last, size, stat_bytes,
1572 stat_starttime, stat_lasttime);
1573 }
07d9aa13 1574 }
5471d09a 1575
c51a56e2 1576 }
1577 CloseHandle(f);
07d9aa13 1578
120e4b40 1579 (void) scp_send_finish();
07d9aa13 1580}
1581
07d9aa13 1582/*
1583 * Recursively send the contents of a directory.
1584 */
1585static void rsource(char *src)
1586{
03f64569 1587 char *last, *findfile;
fd5e5847 1588 char *save_target;
c51a56e2 1589 HANDLE dir;
1590 WIN32_FIND_DATA fdat;
1591 int ok;
1592
1593 if ((last = strrchr(src, '/')) == NULL)
1594 last = src;
1595 else
1596 last++;
1597 if (strrchr(last, '\\') != NULL)
1598 last = strrchr(last, '\\') + 1;
1599 if (last == src && strchr(src, ':') != NULL)
1600 last = strchr(src, ':') + 1;
1601
1602 /* maybe send filetime */
1603
fd5e5847 1604 save_target = scp_save_remotepath();
1605
c51a56e2 1606 if (verbose)
120e4b40 1607 tell_user(stderr, "Entering directory: %s", last);
1608 if (scp_send_dirname(last, 0755))
c51a56e2 1609 return;
1610
03f64569 1611 findfile = dupcat(src, "/*", NULL);
1612 dir = FindFirstFile(findfile, &fdat);
c51a56e2 1613 ok = (dir != INVALID_HANDLE_VALUE);
1614 while (ok) {
1615 if (strcmp(fdat.cFileName, ".") == 0 ||
1616 strcmp(fdat.cFileName, "..") == 0) {
03f64569 1617 /* ignore . and .. */
c51a56e2 1618 } else {
fd5e5847 1619 char *foundfile = dupcat(src, "/", fdat.cFileName, NULL);
03f64569 1620 source(foundfile);
1621 sfree(foundfile);
07d9aa13 1622 }
c51a56e2 1623 ok = FindNextFile(dir, &fdat);
1624 }
1625 FindClose(dir);
03f64569 1626 sfree(findfile);
07d9aa13 1627
120e4b40 1628 (void) scp_send_enddir();
fd5e5847 1629
1630 scp_restore_remotepath(save_target);
07d9aa13 1631}
1632
07d9aa13 1633/*
03f64569 1634 * Execute the sink part of the SCP protocol.
07d9aa13 1635 */
ca2d5943 1636static void sink(char *targ, char *src)
07d9aa13 1637{
03f64569 1638 char *destfname;
c51a56e2 1639 int targisdir = 0;
c51a56e2 1640 int exists;
1641 DWORD attr;
1642 HANDLE f;
120e4b40 1643 unsigned long received;
c51a56e2 1644 int wrerror = 0;
1645 unsigned long stat_bytes;
1646 time_t stat_starttime, stat_lasttime;
1647 char *stat_name;
1648
1649 attr = GetFileAttributes(targ);
32874aea 1650 if (attr != (DWORD) - 1 && (attr & FILE_ATTRIBUTE_DIRECTORY) != 0)
c51a56e2 1651 targisdir = 1;
1652
1653 if (targetshouldbedirectory && !targisdir)
1654 bump("%s: Not a directory", targ);
1655
120e4b40 1656 scp_sink_init();
c51a56e2 1657 while (1) {
120e4b40 1658 struct scp_sink_action act;
1659 if (scp_get_sink_action(&act))
c51a56e2 1660 return;
07d9aa13 1661
120e4b40 1662 if (act.action == SCP_SINK_ENDDIR)
1663 return;
03f64569 1664
4eb24e3a 1665 if (act.action == SCP_SINK_RETRY)
1666 continue;
1667
c51a56e2 1668 if (targisdir) {
03f64569 1669 /*
1670 * Prevent the remote side from maliciously writing to
1671 * files outside the target area by sending a filename
1672 * containing `../'. In fact, it shouldn't be sending
b3dcd9b2 1673 * filenames with any slashes or colons in at all; so
1674 * we'll find the last slash, backslash or colon in the
1675 * filename and use only the part after that. (And
1676 * warn!)
03f64569 1677 *
1678 * In addition, we also ensure here that if we're
1679 * copying a single file and the target is a directory
1680 * (common usage: `pscp host:filename .') the remote
1681 * can't send us a _different_ file name. We can
1682 * distinguish this case because `src' will be non-NULL
1683 * and the last component of that will fail to match
1684 * (the last component of) the name sent.
4eeae4a3 1685 *
cd1f39ab 1686 * Well, not always; if `src' is a wildcard, we do
4eeae4a3 1687 * expect to get back filenames that don't correspond
cd1f39ab 1688 * exactly to it. Ideally in this case, we would like
1689 * to ensure that the returned filename actually
1690 * matches the wildcard pattern - but one of SCP's
1691 * protocol infelicities is that wildcard matching is
1692 * done at the server end _by the server's rules_ and
1693 * so in general this is infeasible. Hence, we only
1694 * accept filenames that don't correspond to `src' if
1695 * unsafe mode is enabled or we are using SFTP (which
1696 * resolves remote wildcards on the client side and can
1697 * be trusted).
03f64569 1698 */
1699 char *striptarget, *stripsrc;
1700
4eb24e3a 1701 striptarget = stripslashes(act.name, 1);
03f64569 1702 if (striptarget != act.name) {
1703 tell_user(stderr, "warning: remote host sent a compound"
b3dcd9b2 1704 " pathname '%s'", act.name);
1705 tell_user(stderr, " renaming local file to '%s'",
1706 striptarget);
03f64569 1707 }
1708
1709 /*
1710 * Also check to see if the target filename is '.' or
1711 * '..', or indeed '...' and so on because Windows
1712 * appears to interpret those like '..'.
1713 */
fd5e5847 1714 if (is_dots(striptarget)) {
03f64569 1715 bump("security violation: remote host attempted to write to"
1716 " a '.' or '..' path!");
1717 }
1718
1719 if (src) {
4eb24e3a 1720 stripsrc = stripslashes(src, 1);
cd1f39ab 1721 if (strcmp(striptarget, stripsrc) &&
1722 !using_sftp && !scp_unsafe_mode) {
1723 tell_user(stderr, "warning: remote host tried to write "
1724 "to a file called '%s'", striptarget);
1725 tell_user(stderr, " when we requested a file "
1726 "called '%s'.", stripsrc);
1727 tell_user(stderr, " If this is a wildcard, "
1728 "consider upgrading to SSH 2 or using");
1729 tell_user(stderr, " the '-unsafe' option. Renaming"
1730 " of this file has been disallowed.");
4eeae4a3 1731 /* Override the name the server provided with our own. */
1732 striptarget = stripsrc;
03f64569 1733 }
03f64569 1734 }
1735
c51a56e2 1736 if (targ[0] != '\0')
03f64569 1737 destfname = dupcat(targ, "\\", striptarget, NULL);
1738 else
1739 destfname = dupstr(striptarget);
c51a56e2 1740 } else {
03f64569 1741 /*
1742 * In this branch of the if, the target area is a
1743 * single file with an explicitly specified name in any
1744 * case, so there's no danger.
1745 */
1746 destfname = dupstr(targ);
c51a56e2 1747 }
03f64569 1748 attr = GetFileAttributes(destfname);
32874aea 1749 exists = (attr != (DWORD) - 1);
c51a56e2 1750
120e4b40 1751 if (act.action == SCP_SINK_DIR) {
c51a56e2 1752 if (exists && (attr & FILE_ATTRIBUTE_DIRECTORY) == 0) {
03f64569 1753 run_err("%s: Not a directory", destfname);
c51a56e2 1754 continue;
1755 }
1756 if (!exists) {
03f64569 1757 if (!CreateDirectory(destfname, NULL)) {
1758 run_err("%s: Cannot create directory", destfname);
c51a56e2 1759 continue;
1760 }
1761 }
03f64569 1762 sink(destfname, NULL);
c51a56e2 1763 /* can we set the timestamp for directories ? */
1764 continue;
1765 }
07d9aa13 1766
03f64569 1767 f = CreateFile(destfname, GENERIC_WRITE, 0, NULL,
c51a56e2 1768 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
1769 if (f == INVALID_HANDLE_VALUE) {
03f64569 1770 run_err("%s: Cannot create file", destfname);
c51a56e2 1771 continue;
1772 }
07d9aa13 1773
120e4b40 1774 if (scp_accept_filexfer())
1775 return;
07d9aa13 1776
2d466ffd 1777 stat_bytes = 0;
1778 stat_starttime = time(NULL);
1779 stat_lasttime = 0;
4eb24e3a 1780 stat_name = stripslashes(destfname, 1);
07d9aa13 1781
120e4b40 1782 received = 0;
1783 while (received < act.size) {
c51a56e2 1784 char transbuf[4096];
120e4b40 1785 DWORD blksize, read, written;
1786 blksize = 4096;
1787 if (blksize > act.size - received)
1788 blksize = act.size - received;
1789 read = scp_recv_filedata(transbuf, blksize);
1790 if (read <= 0)
c51a56e2 1791 bump("Lost connection");
32874aea 1792 if (wrerror)
1793 continue;
120e4b40 1794 if (!WriteFile(f, transbuf, read, &written, NULL) ||
1795 written != read) {
c51a56e2 1796 wrerror = 1;
120e4b40 1797 /* FIXME: in sftp we can actually abort the transfer */
c51a56e2 1798 if (statistics)
1799 printf("\r%-25.25s | %50s\n",
1800 stat_name,
1801 "Write error.. waiting for end of file");
1802 continue;
1803 }
1804 if (statistics) {
120e4b40 1805 stat_bytes += read;
1806 if (time(NULL) > stat_lasttime ||
1807 received + read == act.size) {
c51a56e2 1808 stat_lasttime = time(NULL);
120e4b40 1809 print_stats(stat_name, act.size, stat_bytes,
c51a56e2 1810 stat_starttime, stat_lasttime);
07d9aa13 1811 }
c51a56e2 1812 }
120e4b40 1813 received += read;
c51a56e2 1814 }
120e4b40 1815 if (act.settime) {
c51a56e2 1816 FILETIME actime, wrtime;
120e4b40 1817 TIME_POSIX_TO_WIN(act.atime, actime);
1818 TIME_POSIX_TO_WIN(act.mtime, wrtime);
c51a56e2 1819 SetFileTime(f, NULL, &actime, &wrtime);
07d9aa13 1820 }
07d9aa13 1821
c51a56e2 1822 CloseHandle(f);
1823 if (wrerror) {
03f64569 1824 run_err("%s: Write error", destfname);
c51a56e2 1825 continue;
1826 }
120e4b40 1827 (void) scp_finish_filerecv();
03f64569 1828 sfree(destfname);
d4aa8594 1829 sfree(act.buf);
c51a56e2 1830 }
1831}
07d9aa13 1832
1833/*
120e4b40 1834 * We will copy local files to a remote server.
07d9aa13 1835 */
1836static void toremote(int argc, char *argv[])
1837{
c51a56e2 1838 char *src, *targ, *host, *user;
1839 char *cmd;
1840 int i;
1841
32874aea 1842 targ = argv[argc - 1];
c51a56e2 1843
39ddf0ff 1844 /* Separate host from filename */
c51a56e2 1845 host = targ;
1846 targ = colon(targ);
1847 if (targ == NULL)
1848 bump("targ == NULL in toremote()");
1849 *targ++ = '\0';
1850 if (*targ == '\0')
1851 targ = ".";
1852 /* Substitute "." for emtpy target */
1853
39ddf0ff 1854 /* Separate host and username */
c51a56e2 1855 user = host;
1856 host = strrchr(host, '@');
1857 if (host == NULL) {
1858 host = user;
1859 user = NULL;
1860 } else {
1861 *host++ = '\0';
1862 if (*user == '\0')
1863 user = NULL;
1864 }
1865
1866 if (argc == 2) {
1867 /* Find out if the source filespec covers multiple files
32874aea 1868 if so, we should set the targetshouldbedirectory flag */
c51a56e2 1869 HANDLE fh;
1870 WIN32_FIND_DATA fdat;
1871 if (colon(argv[0]) != NULL)
1872 bump("%s: Remote to remote not supported", argv[0]);
1873 fh = FindFirstFile(argv[0], &fdat);
1874 if (fh == INVALID_HANDLE_VALUE)
1875 bump("%s: No such file or directory\n", argv[0]);
1876 if (FindNextFile(fh, &fdat))
1877 targetshouldbedirectory = 1;
1878 FindClose(fh);
1879 }
1880
1881 cmd = smalloc(strlen(targ) + 100);
1882 sprintf(cmd, "scp%s%s%s%s -t %s",
1883 verbose ? " -v" : "",
1884 recursive ? " -r" : "",
1885 preserve ? " -p" : "",
32874aea 1886 targetshouldbedirectory ? " -d" : "", targ);
c51a56e2 1887 do_cmd(host, user, cmd);
1888 sfree(cmd);
1889
fd5e5847 1890 scp_source_setup(targ, targetshouldbedirectory);
c51a56e2 1891
1892 for (i = 0; i < argc - 1; i++) {
03f64569 1893 char *srcpath, *last;
c51a56e2 1894 HANDLE dir;
1895 WIN32_FIND_DATA fdat;
1896 src = argv[i];
1897 if (colon(src) != NULL) {
cc87246d 1898 tell_user(stderr, "%s: Remote to remote not supported\n", src);
c51a56e2 1899 errs++;
1900 continue;
07d9aa13 1901 }
03f64569 1902
1903 /*
1904 * Trim off the last pathname component of `src', to
1905 * provide the base pathname which will be prepended to
1906 * filenames returned from Find{First,Next}File.
1907 */
1908 srcpath = dupstr(src);
4eb24e3a 1909 last = stripslashes(srcpath, 1);
03f64569 1910 *last = '\0';
03f64569 1911
c51a56e2 1912 dir = FindFirstFile(src, &fdat);
1913 if (dir == INVALID_HANDLE_VALUE) {
1914 run_err("%s: No such file or directory", src);
1915 continue;
07d9aa13 1916 }
c51a56e2 1917 do {
03f64569 1918 char *filename;
7f266ffb 1919 /*
1920 * Ensure that . and .. are never matched by wildcards,
1921 * but only by deliberate action.
1922 */
1923 if (!strcmp(fdat.cFileName, ".") ||
1924 !strcmp(fdat.cFileName, "..")) {
1925 /*
1926 * Find*File has returned a special dir. We require
1927 * that _either_ `src' ends in a backslash followed
1928 * by that string, _or_ `src' is precisely that
1929 * string.
1930 */
1931 int len = strlen(src), dlen = strlen(fdat.cFileName);
1932 if (len == dlen && !strcmp(src, fdat.cFileName)) {
32874aea 1933 /* ok */ ;
1934 } else if (len > dlen + 1 && src[len - dlen - 1] == '\\' &&
1935 !strcmp(src + len - dlen, fdat.cFileName)) {
1936 /* ok */ ;
7f266ffb 1937 } else
1938 continue; /* ignore this one */
1939 }
03f64569 1940 filename = dupcat(srcpath, fdat.cFileName, NULL);
1941 source(filename);
1942 sfree(filename);
c51a56e2 1943 } while (FindNextFile(dir, &fdat));
1944 FindClose(dir);
03f64569 1945 sfree(srcpath);
c51a56e2 1946 }
07d9aa13 1947}
1948
07d9aa13 1949/*
1950 * We will copy files from a remote server to the local machine.
1951 */
1952static void tolocal(int argc, char *argv[])
1953{
c51a56e2 1954 char *src, *targ, *host, *user;
1955 char *cmd;
1956
1957 if (argc != 2)
1958 bump("More than one remote source not supported");
1959
1960 src = argv[0];
1961 targ = argv[1];
1962
39ddf0ff 1963 /* Separate host from filename */
c51a56e2 1964 host = src;
1965 src = colon(src);
1966 if (src == NULL)
1967 bump("Local to local copy not supported");
1968 *src++ = '\0';
1969 if (*src == '\0')
1970 src = ".";
1971 /* Substitute "." for empty filename */
1972
39ddf0ff 1973 /* Separate username and hostname */
c51a56e2 1974 user = host;
1975 host = strrchr(host, '@');
1976 if (host == NULL) {
1977 host = user;
1978 user = NULL;
1979 } else {
1980 *host++ = '\0';
1981 if (*user == '\0')
1982 user = NULL;
1983 }
1984
1985 cmd = smalloc(strlen(src) + 100);
1986 sprintf(cmd, "scp%s%s%s%s -f %s",
1987 verbose ? " -v" : "",
1988 recursive ? " -r" : "",
1989 preserve ? " -p" : "",
32874aea 1990 targetshouldbedirectory ? " -d" : "", src);
c51a56e2 1991 do_cmd(host, user, cmd);
1992 sfree(cmd);
1993
4eb24e3a 1994 if (scp_sink_setup(src, preserve, recursive))
1995 return;
fd5e5847 1996
ca2d5943 1997 sink(targ, src);
07d9aa13 1998}
1999
07d9aa13 2000/*
39ddf0ff 2001 * We will issue a list command to get a remote directory.
2002 */
2003static void get_dir_list(int argc, char *argv[])
2004{
2005 char *src, *host, *user;
2006 char *cmd, *p, *q;
2007 char c;
2008
2009 src = argv[0];
2010
2011 /* Separate host from filename */
2012 host = src;
2013 src = colon(src);
2014 if (src == NULL)
2015 bump("Local to local copy not supported");
2016 *src++ = '\0';
2017 if (*src == '\0')
2018 src = ".";
2019 /* Substitute "." for empty filename */
2020
2021 /* Separate username and hostname */
2022 user = host;
2023 host = strrchr(host, '@');
2024 if (host == NULL) {
2025 host = user;
2026 user = NULL;
2027 } else {
2028 *host++ = '\0';
2029 if (*user == '\0')
2030 user = NULL;
2031 }
2032
32874aea 2033 cmd = smalloc(4 * strlen(src) + 100);
39ddf0ff 2034 strcpy(cmd, "ls -la '");
2035 p = cmd + strlen(cmd);
2036 for (q = src; *q; q++) {
2037 if (*q == '\'') {
32874aea 2038 *p++ = '\'';
2039 *p++ = '\\';
2040 *p++ = '\'';
2041 *p++ = '\'';
39ddf0ff 2042 } else {
2043 *p++ = *q;
2044 }
2045 }
2046 *p++ = '\'';
2047 *p = '\0';
cc87246d 2048
39ddf0ff 2049 do_cmd(host, user, cmd);
2050 sfree(cmd);
2051
fd5e5847 2052 if (using_sftp) {
2053 scp_sftp_listdir(src);
2054 } else {
2055 while (ssh_scp_recv(&c, 1) > 0)
2056 tell_char(stdout, c);
2057 }
39ddf0ff 2058}
2059
2060/*
07d9aa13 2061 * Initialize the Win$ock driver.
2062 */
996c8c3b 2063static void init_winsock(void)
07d9aa13 2064{
c51a56e2 2065 WORD winsock_ver;
2066 WSADATA wsadata;
2067
2068 winsock_ver = MAKEWORD(1, 1);
2069 if (WSAStartup(winsock_ver, &wsadata))
2070 bump("Unable to initialise WinSock");
32874aea 2071 if (LOBYTE(wsadata.wVersion) != 1 || HIBYTE(wsadata.wVersion) != 1)
c51a56e2 2072 bump("WinSock version is incompatible with 1.1");
07d9aa13 2073}
2074
07d9aa13 2075/*
2076 * Short description of parameters.
2077 */
996c8c3b 2078static void usage(void)
07d9aa13 2079{
c51a56e2 2080 printf("PuTTY Secure Copy client\n");
2081 printf("%s\n", ver);
a3e55ea1 2082 printf("Usage: pscp [options] [user@]host:source target\n");
32874aea 2083 printf
2084 (" pscp [options] source [source...] [user@]host:target\n");
a3e55ea1 2085 printf(" pscp [options] -ls user@host:filespec\n");
b8a19193 2086 printf("Options:\n");
2087 printf(" -p preserve file attributes\n");
2088 printf(" -q quiet, don't show statistics\n");
2089 printf(" -r copy directories recursively\n");
2090 printf(" -v show verbose messages\n");
e2a197cf 2091 printf(" -load sessname Load settings from saved session\n");
b8a19193 2092 printf(" -P port connect to specified port\n");
e2a197cf 2093 printf(" -l user connect with specified username\n");
b8a19193 2094 printf(" -pw passw login with specified password\n");
e2a197cf 2095 printf(" -1 -2 force use of particular SSH protocol version\n");
2096 printf(" -C enable compression\n");
2097 printf(" -i key private key file for authentication\n");
2098 printf(" -batch disable all interactive prompts\n");
cd1f39ab 2099 printf(" -unsafe allow server-side wildcards (DANGEROUS)\n");
ee8b0370 2100#if 0
2101 /*
2102 * -gui is an internal option, used by GUI front ends to get
2103 * pscp to pass progress reports back to them. It's not an
2104 * ordinary user-accessible option, so it shouldn't be part of
2105 * the command-line help. The only people who need to know
2106 * about it are programmers, and they can read the source.
2107 */
32874aea 2108 printf
2109 (" -gui hWnd GUI mode with the windows handle for receiving messages\n");
ee8b0370 2110#endif
93b581bd 2111 cleanup_exit(1);
07d9aa13 2112}
2113
c0a81592 2114void cmdline_error(char *p, ...)
2115{
2116 va_list ap;
2117 fprintf(stderr, "pscp: ");
2118 va_start(ap, p);
2119 vfprintf(stderr, p, ap);
2120 va_end(ap);
2121 fputc('\n', stderr);
2122 exit(1);
2123}
2124
07d9aa13 2125/*
2126 * Main program (no, really?)
2127 */
2128int main(int argc, char *argv[])
2129{
c51a56e2 2130 int i;
2131
fb09bf1c 2132 default_protocol = PROT_TELNET;
2133
67779be7 2134 flags = FLAG_STDERR;
c0a81592 2135 cmdline_tooltype = TOOLTYPE_FILETRANSFER;
ff2ae367 2136 ssh_get_line = &console_get_line;
c51a56e2 2137 init_winsock();
8df7a775 2138 sk_init();
c51a56e2 2139
2140 for (i = 1; i < argc; i++) {
c0a81592 2141 int ret;
c51a56e2 2142 if (argv[i][0] != '-')
2143 break;
c0a81592 2144 ret = cmdline_process_param(argv[i], i+1<argc?argv[i+1]:NULL, 1);
2145 if (ret == -2) {
2146 cmdline_error("option \"%s\" requires an argument", argv[i]);
2147 } else if (ret == 2) {
2148 i++; /* skip next argument */
2149 } else if (ret == 1) {
2150 /* We have our own verbosity in addition to `flags'. */
2151 if (flags & FLAG_VERBOSE)
2152 verbose = 1;
2153 } else if (strcmp(argv[i], "-r") == 0) {
c51a56e2 2154 recursive = 1;
c0a81592 2155 } else if (strcmp(argv[i], "-p") == 0) {
c51a56e2 2156 preserve = 1;
c0a81592 2157 } else if (strcmp(argv[i], "-q") == 0) {
c51a56e2 2158 statistics = 0;
c0a81592 2159 } else if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "-?") == 0) {
c51a56e2 2160 usage();
c0a81592 2161 } else if (strcmp(argv[i], "-gui") == 0 && i + 1 < argc) {
cc87246d 2162 gui_hwnd = argv[++i];
2163 gui_mode = 1;
ff2ae367 2164 console_batch_mode = TRUE;
c0a81592 2165 } else if (strcmp(argv[i], "-ls") == 0) {
32874aea 2166 list = 1;
c0a81592 2167 } else if (strcmp(argv[i], "-batch") == 0) {
2168 console_batch_mode = 1;
2169 } else if (strcmp(argv[i], "-unsafe") == 0) {
cd1f39ab 2170 scp_unsafe_mode = 1;
c0a81592 2171 } else if (strcmp(argv[i], "--") == 0) {
32874aea 2172 i++;
2173 break;
2174 } else
c51a56e2 2175 usage();
2176 }
2177 argc -= i;
2178 argv += i;
eba78553 2179 back = NULL;
c51a56e2 2180
39ddf0ff 2181 if (list) {
2182 if (argc != 1)
2183 usage();
2184 get_dir_list(argc, argv);
c51a56e2 2185
39ddf0ff 2186 } else {
2187
2188 if (argc < 2)
2189 usage();
2190 if (argc > 2)
2191 targetshouldbedirectory = 1;
2192
32874aea 2193 if (colon(argv[argc - 1]) != NULL)
39ddf0ff 2194 toremote(argc, argv);
2195 else
2196 tolocal(argc, argv);
2197 }
c51a56e2 2198
eba78553 2199 if (back != NULL && back->socket() != NULL) {
c51a56e2 2200 char ch;
3bdaf79d 2201 back->special(TS_EOF);
fb09bf1c 2202 ssh_scp_recv(&ch, 1);
c51a56e2 2203 }
2204 WSACleanup();
2205 random_save_seed();
07d9aa13 2206
cc87246d 2207 /* GUI Adaptation - August 2000 */
2208 if (gui_mode) {
2209 unsigned int msg_id = WM_RET_ERR_CNT;
32874aea 2210 if (list)
2211 msg_id = WM_LS_RET_ERR_CNT;
2212 while (!PostMessage
2213 ((HWND) atoi(gui_hwnd), msg_id, (WPARAM) errs,
2214 0 /*lParam */ ))SleepEx(1000, TRUE);
cc87246d 2215 }
c51a56e2 2216 return (errs == 0 ? 0 : 1);
07d9aa13 2217}
2218
2219/* end */