2 * scp.c - Scp (Secure Copy) client for PuTTY.
\r
3 * Joris van Rantwijk, Simon Tatham
\r
5 * This is mainly based on ssh-1.2.26/scp.c by Timo Rinne & Tatu Ylonen.
\r
6 * They, in turn, used stuff from BSD rcp.
\r
8 * (SGT, 2001-09-10: Joris van Rantwijk assures me that although
\r
9 * this file as originally submitted was inspired by, and
\r
10 * _structurally_ based on, ssh-1.2.26's scp.c, there wasn't any
\r
11 * actual code duplicated, so the above comment shouldn't give rise
\r
12 * to licensing issues.)
\r
22 #define PUTTY_DO_GLOBALS
\r
27 #include "storage.h"
\r
30 static int list = 0;
\r
31 static int verbose = 0;
\r
32 static int recursive = 0;
\r
33 static int preserve = 0;
\r
34 static int targetshouldbedirectory = 0;
\r
35 static int statistics = 1;
\r
36 static int prev_stats_len = 0;
\r
37 static int scp_unsafe_mode = 0;
\r
38 static int errs = 0;
\r
39 static int try_scp = 1;
\r
40 static int try_sftp = 1;
\r
41 static int main_cmd_is_sftp = 0;
\r
42 static int fallback_cmd_is_sftp = 0;
\r
43 static int using_sftp = 0;
\r
45 static Backend *back;
\r
46 static void *backhandle;
\r
49 static void source(char *src);
\r
50 static void rsource(char *src);
\r
51 static void sink(char *targ, char *src);
\r
53 const char *const appname = "PSCP";
\r
56 * The maximum amount of queued data we accept before we stop and
\r
57 * wait for the server to process some.
\r
59 #define MAX_SCP_BUFSIZE 16384
\r
61 void ldisc_send(void *handle, char *buf, int len, int interactive)
\r
64 * This is only here because of the calls to ldisc_send(NULL,
\r
65 * 0) in ssh.c. Nothing in PSCP actually needs to use the ldisc
\r
66 * as an ldisc. So if we get called with any real data, I want
\r
72 static void tell_char(FILE * stream, char c)
\r
77 static void tell_str(FILE * stream, char *str)
\r
81 for (i = 0; i < strlen(str); ++i)
\r
82 tell_char(stream, str[i]);
\r
85 static void tell_user(FILE * stream, char *fmt, ...)
\r
90 str = dupvprintf(fmt, ap);
\r
92 str2 = dupcat(str, "\n", NULL);
\r
94 tell_str(stream, str2);
\r
99 * Print an error message and perform a fatal exit.
\r
101 void fatalbox(char *fmt, ...)
\r
106 str = dupvprintf(fmt, ap);
\r
107 str2 = dupcat("Fatal: ", str, "\n", NULL);
\r
110 tell_str(stderr, str2);
\r
116 void modalfatalbox(char *fmt, ...)
\r
121 str = dupvprintf(fmt, ap);
\r
122 str2 = dupcat("Fatal: ", str, "\n", NULL);
\r
125 tell_str(stderr, str2);
\r
131 void connection_fatal(void *frontend, char *fmt, ...)
\r
136 str = dupvprintf(fmt, ap);
\r
137 str2 = dupcat("Fatal: ", str, "\n", NULL);
\r
140 tell_str(stderr, str2);
\r
148 * In pscp, all agent requests should be synchronous, so this is a
\r
149 * never-called stub.
\r
151 void agent_schedule_callback(void (*callback)(void *, void *, int),
\r
152 void *callback_ctx, void *data, int len)
\r
154 assert(!"We shouldn't be here");
\r
158 * Receive a block of data from the SSH link. Block until all data
\r
161 * To do this, we repeatedly call the SSH protocol module, with our
\r
162 * own trap in from_backend() to catch the data that comes back. We
\r
163 * do this until we have enough data.
\r
166 static unsigned char *outptr; /* where to put the data */
\r
167 static unsigned outlen; /* how much data required */
\r
168 static unsigned char *pending = NULL; /* any spare data */
\r
169 static unsigned pendlen = 0, pendsize = 0; /* length and phys. size of buffer */
\r
170 int from_backend(void *frontend, int is_stderr, const char *data, int datalen)
\r
172 unsigned char *p = (unsigned char *) data;
\r
173 unsigned len = (unsigned) datalen;
\r
176 * stderr data is just spouted to local stderr and otherwise
\r
181 if (fwrite(data, 1, len, stderr) < len)
\r
186 if ((outlen > 0) && (len > 0)) {
\r
187 unsigned used = outlen;
\r
190 memcpy(outptr, p, used);
\r
198 if (pendsize < pendlen + len) {
\r
199 pendsize = pendlen + len + 4096;
\r
200 pending = sresize(pending, pendsize, unsigned char);
\r
202 memcpy(pending + pendlen, p, len);
\r
208 int from_backend_untrusted(void *frontend_handle, const char *data, int len)
\r
211 * No "untrusted" output should get here (the way the code is
\r
212 * currently, it's all diverted by FLAG_STDERR).
\r
214 assert(!"Unexpected call to from_backend_untrusted()");
\r
215 return 0; /* not reached */
\r
217 static int ssh_scp_recv(unsigned char *buf, int len)
\r
223 * See if the pending-input block contains some of what we
\r
227 unsigned pendused = pendlen;
\r
228 if (pendused > outlen)
\r
230 memcpy(outptr, pending, pendused);
\r
231 memmove(pending, pending + pendused, pendlen - pendused);
\r
232 outptr += pendused;
\r
233 outlen -= pendused;
\r
234 pendlen -= pendused;
\r
235 if (pendlen == 0) {
\r
244 while (outlen > 0) {
\r
245 if (back->exitcode(backhandle) >= 0 || ssh_sftp_loop_iteration() < 0)
\r
246 return 0; /* doom */
\r
253 * Loop through the ssh connection and authentication process.
\r
255 static void ssh_scp_init(void)
\r
257 while (!back->sendok(backhandle)) {
\r
258 if (back->exitcode(backhandle) >= 0) {
\r
262 if (ssh_sftp_loop_iteration() < 0) {
\r
268 /* Work out which backend we ended up using. */
\r
269 if (!ssh_fallback_cmd(backhandle))
\r
270 using_sftp = main_cmd_is_sftp;
\r
272 using_sftp = fallback_cmd_is_sftp;
\r
276 tell_user(stderr, "Using SFTP");
\r
278 tell_user(stderr, "Using SCP1");
\r
283 * Print an error message and exit after closing the SSH link.
\r
285 static void bump(char *fmt, ...)
\r
290 str = dupvprintf(fmt, ap);
\r
292 str2 = dupcat(str, "\n", NULL);
\r
294 tell_str(stderr, str2);
\r
298 if (back != NULL && back->connected(backhandle)) {
\r
300 back->special(backhandle, TS_EOF);
\r
301 ssh_scp_recv((unsigned char *) &ch, 1);
\r
308 * Open an SSH connection to user@host and execute cmd.
\r
310 static void do_cmd(char *host, char *user, char *cmd)
\r
316 if (host == NULL || host[0] == '\0')
\r
317 bump("Empty host name");
\r
320 * Remove fiddly bits of address: remove a colon suffix, and
\r
321 * the square brackets around an IPv6 literal address.
\r
323 if (host[0] == '[') {
\r
325 host[strcspn(host, "]")] = '\0';
\r
327 host[strcspn(host, ":")] = '\0';
\r
331 * If we haven't loaded session details already (e.g., from -load),
\r
332 * try looking for a session called "host".
\r
334 if (!loaded_session) {
\r
335 /* Try to load settings for `host' into a temporary config */
\r
337 cfg2.host[0] = '\0';
\r
338 do_defaults(host, &cfg2);
\r
339 if (cfg2.host[0] != '\0') {
\r
340 /* Settings present and include hostname */
\r
341 /* Re-load data into the real config. */
\r
342 do_defaults(host, &cfg);
\r
344 /* Session doesn't exist or mention a hostname. */
\r
345 /* Use `host' as a bare hostname. */
\r
346 strncpy(cfg.host, host, sizeof(cfg.host) - 1);
\r
347 cfg.host[sizeof(cfg.host) - 1] = '\0';
\r
350 /* Patch in hostname `host' to session details. */
\r
351 strncpy(cfg.host, host, sizeof(cfg.host) - 1);
\r
352 cfg.host[sizeof(cfg.host) - 1] = '\0';
\r
356 * Force use of SSH. (If they got the protocol wrong we assume the
\r
357 * port is useless too.)
\r
359 if (cfg.protocol != PROT_SSH) {
\r
360 cfg.protocol = PROT_SSH;
\r
365 * Enact command-line overrides.
\r
367 cmdline_run_saved(&cfg);
\r
370 * Trim leading whitespace off the hostname if it's there.
\r
373 int space = strspn(cfg.host, " \t");
\r
374 memmove(cfg.host, cfg.host+space, 1+strlen(cfg.host)-space);
\r
377 /* See if host is of the form user@host */
\r
378 if (cfg.host[0] != '\0') {
\r
379 char *atsign = strrchr(cfg.host, '@');
\r
380 /* Make sure we're not overflowing the user field */
\r
382 if (atsign - cfg.host < sizeof cfg.username) {
\r
383 strncpy(cfg.username, cfg.host, atsign - cfg.host);
\r
384 cfg.username[atsign - cfg.host] = '\0';
\r
386 memmove(cfg.host, atsign + 1, 1 + strlen(atsign + 1));
\r
391 * Remove any remaining whitespace from the hostname.
\r
394 int p1 = 0, p2 = 0;
\r
395 while (cfg.host[p2] != '\0') {
\r
396 if (cfg.host[p2] != ' ' && cfg.host[p2] != '\t') {
\r
397 cfg.host[p1] = cfg.host[p2];
\r
402 cfg.host[p1] = '\0';
\r
406 if (user != NULL && user[0] != '\0') {
\r
407 strncpy(cfg.username, user, sizeof(cfg.username) - 1);
\r
408 cfg.username[sizeof(cfg.username) - 1] = '\0';
\r
409 } else if (cfg.username[0] == '\0') {
\r
410 user = get_username();
\r
412 bump("Empty user name");
\r
415 tell_user(stderr, "Guessing user name: %s", user);
\r
416 strncpy(cfg.username, user, sizeof(cfg.username) - 1);
\r
417 cfg.username[sizeof(cfg.username) - 1] = '\0';
\r
423 * Disable scary things which shouldn't be enabled for simple
\r
424 * things like SCP and SFTP: agent forwarding, port forwarding,
\r
427 cfg.x11_forward = 0;
\r
429 cfg.portfwd[0] = cfg.portfwd[1] = '\0';
\r
430 cfg.ssh_simple = TRUE;
\r
433 * Set up main and possibly fallback command depending on
\r
434 * options specified by user.
\r
435 * Attempt to start the SFTP subsystem as a first choice,
\r
436 * falling back to the provided scp command if that fails.
\r
438 cfg.remote_cmd_ptr2 = NULL;
\r
440 /* First choice is SFTP subsystem. */
\r
441 main_cmd_is_sftp = 1;
\r
442 strcpy(cfg.remote_cmd, "sftp");
\r
443 cfg.ssh_subsys = TRUE;
\r
445 /* Fallback is to use the provided scp command. */
\r
446 fallback_cmd_is_sftp = 0;
\r
447 cfg.remote_cmd_ptr2 = cmd;
\r
448 cfg.ssh_subsys2 = FALSE;
\r
450 /* Since we're not going to try SCP, we may as well try
\r
451 * harder to find an SFTP server, since in the current
\r
452 * implementation we have a spare slot. */
\r
453 fallback_cmd_is_sftp = 1;
\r
454 /* see psftp.c for full explanation of this kludge */
\r
455 cfg.remote_cmd_ptr2 =
\r
456 "test -x /usr/lib/sftp-server && exec /usr/lib/sftp-server\n"
\r
457 "test -x /usr/local/lib/sftp-server && exec /usr/local/lib/sftp-server\n"
\r
458 "exec sftp-server";
\r
459 cfg.ssh_subsys2 = FALSE;
\r
462 /* Don't try SFTP at all; just try the scp command. */
\r
463 main_cmd_is_sftp = 0;
\r
464 cfg.remote_cmd_ptr = cmd;
\r
465 cfg.ssh_subsys = FALSE;
\r
469 back = &ssh_backend;
\r
471 err = back->init(NULL, &backhandle, &cfg, cfg.host, cfg.port, &realhost,
\r
472 0, cfg.tcp_keepalives);
\r
474 bump("ssh_init: %s", err);
\r
475 logctx = log_init(NULL, &cfg);
\r
476 back->provide_logctx(backhandle, logctx);
\r
477 console_provide_logctx(logctx);
\r
479 if (verbose && realhost != NULL && errs == 0)
\r
480 tell_user(stderr, "Connected to %s\n", realhost);
\r
485 * Update statistic information about current file.
\r
487 static void print_stats(char *name, uint64 size, uint64 done,
\r
488 time_t start, time_t now)
\r
499 elap = (unsigned long) difftime(now, start);
\r
502 ratebs = (float) (uint64_to_double(done) / elap);
\r
504 ratebs = (float) uint64_to_double(done);
\r
507 eta = (unsigned long) (uint64_to_double(uint64_subtract(size, done)));
\r
509 eta = (unsigned long)
\r
510 ((uint64_to_double(uint64_subtract(size, done)) / ratebs));
\r
513 etastr = dupprintf("%02ld:%02ld:%02ld",
\r
514 eta / 3600, (eta % 3600) / 60, eta % 60);
\r
516 donedbl = uint64_to_double(done);
\r
517 sizedbl = uint64_to_double(size);
\r
518 pct = (int) (100 * (donedbl * 1.0 / sizedbl));
\r
522 /* divide by 1024 to provide kB */
\r
523 uint64_decimal(uint64_shift_right(done, 10), donekb);
\r
524 len = printf("\r%-25.25s | %s kB | %5.1f kB/s | ETA: %8s | %3d%%",
\r
526 donekb, ratebs / 1024.0, etastr, pct);
\r
527 if (len < prev_stats_len)
\r
528 printf("%*s", prev_stats_len - len, "");
\r
529 prev_stats_len = len;
\r
531 if (uint64_compare(done, size) == 0)
\r
541 * Find a colon in str and return a pointer to the colon.
\r
542 * This is used to separate hostname from filename.
\r
544 static char *colon(char *str)
\r
546 /* We ignore a leading colon, since the hostname cannot be
\r
547 empty. We also ignore a colon as second character because
\r
548 of filenames like f:myfile.txt. */
\r
549 if (str[0] == '\0' || str[0] == ':' ||
\r
550 (str[0] != '[' && str[1] == ':'))
\r
552 while (*str != '\0' && *str != ':' && *str != '/' && *str != '\\') {
\r
554 /* Skip over IPv6 literal addresses
\r
555 * (eg: 'jeroen@[2001:db8::1]:myfile.txt') */
\r
556 char *ipv6_end = strchr(str, ']');
\r
570 * Return a pointer to the portion of str that comes after the last
\r
571 * slash (or backslash or colon, if `local' is TRUE).
\r
573 static char *stripslashes(char *str, int local)
\r
578 p = strchr(str, ':');
\r
582 p = strrchr(str, '/');
\r
586 p = strrchr(str, '\\');
\r
594 * Determine whether a string is entirely composed of dots.
\r
596 static int is_dots(char *str)
\r
598 return str[strspn(str, ".")] == '\0';
\r
602 * Wait for a response from the other side.
\r
603 * Return 0 if ok, -1 if error.
\r
605 static int response(void)
\r
607 char ch, resp, rbuf[2048];
\r
610 if (ssh_scp_recv((unsigned char *) &resp, 1) <= 0)
\r
611 bump("Lost connection");
\r
620 case 1: /* error */
\r
621 case 2: /* fatal error */
\r
623 if (ssh_scp_recv((unsigned char *) &ch, 1) <= 0)
\r
624 bump("Protocol error: Lost connection");
\r
626 } while (p < sizeof(rbuf) && ch != '\n');
\r
627 rbuf[p - 1] = '\0';
\r
629 tell_user(stderr, "%s\n", rbuf);
\r
637 int sftp_recvdata(char *buf, int len)
\r
639 return ssh_scp_recv((unsigned char *) buf, len);
\r
641 int sftp_senddata(char *buf, int len)
\r
643 back->send(backhandle, buf, len);
\r
647 /* ----------------------------------------------------------------------
\r
648 * sftp-based replacement for the hacky `pscp -ls'.
\r
650 static int sftp_ls_compare(const void *av, const void *bv)
\r
652 const struct fxp_name *a = (const struct fxp_name *) av;
\r
653 const struct fxp_name *b = (const struct fxp_name *) bv;
\r
654 return strcmp(a->filename, b->filename);
\r
656 void scp_sftp_listdir(char *dirname)
\r
658 struct fxp_handle *dirh;
\r
659 struct fxp_names *names;
\r
660 struct fxp_name *ournames;
\r
661 struct sftp_packet *pktin;
\r
662 struct sftp_request *req, *rreq;
\r
663 int nnames, namesize;
\r
667 tell_user(stderr, "unable to initialise SFTP: %s", fxp_error());
\r
672 printf("Listing directory %s\n", dirname);
\r
674 sftp_register(req = fxp_opendir_send(dirname));
\r
675 rreq = sftp_find_request(pktin = sftp_recv());
\r
676 assert(rreq == req);
\r
677 dirh = fxp_opendir_recv(pktin, rreq);
\r
679 if (dirh == NULL) {
\r
680 printf("Unable to open %s: %s\n", dirname, fxp_error());
\r
682 nnames = namesize = 0;
\r
687 sftp_register(req = fxp_readdir_send(dirh));
\r
688 rreq = sftp_find_request(pktin = sftp_recv());
\r
689 assert(rreq == req);
\r
690 names = fxp_readdir_recv(pktin, rreq);
\r
692 if (names == NULL) {
\r
693 if (fxp_error_type() == SSH_FX_EOF)
\r
695 printf("Reading directory %s: %s\n", dirname, fxp_error());
\r
698 if (names->nnames == 0) {
\r
699 fxp_free_names(names);
\r
703 if (nnames + names->nnames >= namesize) {
\r
704 namesize += names->nnames + 128;
\r
705 ournames = sresize(ournames, namesize, struct fxp_name);
\r
708 for (i = 0; i < names->nnames; i++)
\r
709 ournames[nnames++] = names->names[i];
\r
710 names->nnames = 0; /* prevent free_names */
\r
711 fxp_free_names(names);
\r
713 sftp_register(req = fxp_close_send(dirh));
\r
714 rreq = sftp_find_request(pktin = sftp_recv());
\r
715 assert(rreq == req);
\r
716 fxp_close_recv(pktin, rreq);
\r
719 * Now we have our filenames. Sort them by actual file
\r
720 * name, and then output the longname parts.
\r
722 qsort(ournames, nnames, sizeof(*ournames), sftp_ls_compare);
\r
727 for (i = 0; i < nnames; i++)
\r
728 printf("%s\n", ournames[i].longname);
\r
732 /* ----------------------------------------------------------------------
\r
733 * Helper routines that contain the actual SCP protocol elements,
\r
734 * implemented both as SCP1 and SFTP.
\r
737 static struct scp_sftp_dirstack {
\r
738 struct scp_sftp_dirstack *next;
\r
739 struct fxp_name *names;
\r
740 int namepos, namelen;
\r
743 int matched_something; /* wildcard match set was non-empty */
\r
744 } *scp_sftp_dirstack_head;
\r
745 static char *scp_sftp_remotepath, *scp_sftp_currentname;
\r
746 static char *scp_sftp_wildcard;
\r
747 static int scp_sftp_targetisdir, scp_sftp_donethistarget;
\r
748 static int scp_sftp_preserve, scp_sftp_recursive;
\r
749 static unsigned long scp_sftp_mtime, scp_sftp_atime;
\r
750 static int scp_has_times;
\r
751 static struct fxp_handle *scp_sftp_filehandle;
\r
752 static struct fxp_xfer *scp_sftp_xfer;
\r
753 static uint64 scp_sftp_fileoffset;
\r
755 int scp_source_setup(char *target, int shouldbedir)
\r
759 * Find out whether the target filespec is in fact a
\r
762 struct sftp_packet *pktin;
\r
763 struct sftp_request *req, *rreq;
\r
764 struct fxp_attrs attrs;
\r
768 tell_user(stderr, "unable to initialise SFTP: %s", fxp_error());
\r
773 sftp_register(req = fxp_stat_send(target));
\r
774 rreq = sftp_find_request(pktin = sftp_recv());
\r
775 assert(rreq == req);
\r
776 ret = fxp_stat_recv(pktin, rreq, &attrs);
\r
778 if (!ret || !(attrs.flags & SSH_FILEXFER_ATTR_PERMISSIONS))
\r
779 scp_sftp_targetisdir = 0;
\r
781 scp_sftp_targetisdir = (attrs.permissions & 0040000) != 0;
\r
783 if (shouldbedir && !scp_sftp_targetisdir) {
\r
784 bump("pscp: remote filespec %s: not a directory\n", target);
\r
787 scp_sftp_remotepath = dupstr(target);
\r
796 int scp_send_errmsg(char *str)
\r
799 /* do nothing; we never need to send our errors to the server */
\r
801 back->send(backhandle, "\001", 1);/* scp protocol error prefix */
\r
802 back->send(backhandle, str, strlen(str));
\r
804 return 0; /* can't fail */
\r
807 int scp_send_filetimes(unsigned long mtime, unsigned long atime)
\r
810 scp_sftp_mtime = mtime;
\r
811 scp_sftp_atime = atime;
\r
816 sprintf(buf, "T%lu 0 %lu 0\n", mtime, atime);
\r
817 back->send(backhandle, buf, strlen(buf));
\r
822 int scp_send_filename(char *name, uint64 size, int modes)
\r
826 struct sftp_packet *pktin;
\r
827 struct sftp_request *req, *rreq;
\r
829 if (scp_sftp_targetisdir) {
\r
830 fullname = dupcat(scp_sftp_remotepath, "/", name, NULL);
\r
832 fullname = dupstr(scp_sftp_remotepath);
\r
835 sftp_register(req = fxp_open_send(fullname, SSH_FXF_WRITE |
\r
836 SSH_FXF_CREAT | SSH_FXF_TRUNC));
\r
837 rreq = sftp_find_request(pktin = sftp_recv());
\r
838 assert(rreq == req);
\r
839 scp_sftp_filehandle = fxp_open_recv(pktin, rreq);
\r
841 if (!scp_sftp_filehandle) {
\r
842 tell_user(stderr, "pscp: unable to open %s: %s",
\r
843 fullname, fxp_error());
\r
847 scp_sftp_fileoffset = uint64_make(0, 0);
\r
848 scp_sftp_xfer = xfer_upload_init(scp_sftp_filehandle,
\r
849 scp_sftp_fileoffset);
\r
855 uint64_decimal(size, sizestr);
\r
856 sprintf(buf, "C%04o %s ", modes, sizestr);
\r
857 back->send(backhandle, buf, strlen(buf));
\r
858 back->send(backhandle, name, strlen(name));
\r
859 back->send(backhandle, "\n", 1);
\r
864 int scp_send_filedata(char *data, int len)
\r
868 struct sftp_packet *pktin;
\r
870 if (!scp_sftp_filehandle) {
\r
874 while (!xfer_upload_ready(scp_sftp_xfer)) {
\r
875 pktin = sftp_recv();
\r
876 ret = xfer_upload_gotpkt(scp_sftp_xfer, pktin);
\r
878 tell_user(stderr, "error while writing: %s\n", fxp_error());
\r
884 xfer_upload_data(scp_sftp_xfer, data, len);
\r
886 scp_sftp_fileoffset = uint64_add32(scp_sftp_fileoffset, len);
\r
889 int bufsize = back->send(backhandle, data, len);
\r
892 * If the network transfer is backing up - that is, the
\r
893 * remote site is not accepting data as fast as we can
\r
894 * produce it - then we must loop on network events until
\r
895 * we have space in the buffer again.
\r
897 while (bufsize > MAX_SCP_BUFSIZE) {
\r
898 if (ssh_sftp_loop_iteration() < 0)
\r
900 bufsize = back->sendbuffer(backhandle);
\r
907 int scp_send_finish(void)
\r
910 struct fxp_attrs attrs;
\r
911 struct sftp_packet *pktin;
\r
912 struct sftp_request *req, *rreq;
\r
915 while (!xfer_done(scp_sftp_xfer)) {
\r
916 pktin = sftp_recv();
\r
917 xfer_upload_gotpkt(scp_sftp_xfer, pktin);
\r
919 xfer_cleanup(scp_sftp_xfer);
\r
921 if (!scp_sftp_filehandle) {
\r
924 if (scp_has_times) {
\r
925 attrs.flags = SSH_FILEXFER_ATTR_ACMODTIME;
\r
926 attrs.atime = scp_sftp_atime;
\r
927 attrs.mtime = scp_sftp_mtime;
\r
928 sftp_register(req = fxp_fsetstat_send(scp_sftp_filehandle, attrs));
\r
929 rreq = sftp_find_request(pktin = sftp_recv());
\r
930 assert(rreq == req);
\r
931 ret = fxp_fsetstat_recv(pktin, rreq);
\r
933 tell_user(stderr, "unable to set file times: %s\n", fxp_error());
\r
937 sftp_register(req = fxp_close_send(scp_sftp_filehandle));
\r
938 rreq = sftp_find_request(pktin = sftp_recv());
\r
939 assert(rreq == req);
\r
940 fxp_close_recv(pktin, rreq);
\r
944 back->send(backhandle, "", 1);
\r
949 char *scp_save_remotepath(void)
\r
952 return scp_sftp_remotepath;
\r
957 void scp_restore_remotepath(char *data)
\r
960 scp_sftp_remotepath = data;
\r
963 int scp_send_dirname(char *name, int modes)
\r
968 struct fxp_attrs attrs;
\r
969 struct sftp_packet *pktin;
\r
970 struct sftp_request *req, *rreq;
\r
973 if (scp_sftp_targetisdir) {
\r
974 fullname = dupcat(scp_sftp_remotepath, "/", name, NULL);
\r
976 fullname = dupstr(scp_sftp_remotepath);
\r
980 * We don't worry about whether we managed to create the
\r
981 * directory, because if it exists already it's OK just to
\r
982 * use it. Instead, we will stat it afterwards, and if it
\r
983 * exists and is a directory we will assume we were either
\r
984 * successful or it didn't matter.
\r
986 sftp_register(req = fxp_mkdir_send(fullname));
\r
987 rreq = sftp_find_request(pktin = sftp_recv());
\r
988 assert(rreq == req);
\r
989 ret = fxp_mkdir_recv(pktin, rreq);
\r
994 err = "server reported no error";
\r
996 sftp_register(req = fxp_stat_send(fullname));
\r
997 rreq = sftp_find_request(pktin = sftp_recv());
\r
998 assert(rreq == req);
\r
999 ret = fxp_stat_recv(pktin, rreq, &attrs);
\r
1001 if (!ret || !(attrs.flags & SSH_FILEXFER_ATTR_PERMISSIONS) ||
\r
1002 !(attrs.permissions & 0040000)) {
\r
1003 tell_user(stderr, "unable to create directory %s: %s",
\r
1009 scp_sftp_remotepath = fullname;
\r
1014 sprintf(buf, "D%04o 0 ", modes);
\r
1015 back->send(backhandle, buf, strlen(buf));
\r
1016 back->send(backhandle, name, strlen(name));
\r
1017 back->send(backhandle, "\n", 1);
\r
1018 return response();
\r
1022 int scp_send_enddir(void)
\r
1025 sfree(scp_sftp_remotepath);
\r
1028 back->send(backhandle, "E\n", 2);
\r
1029 return response();
\r
1034 * Yes, I know; I have an scp_sink_setup _and_ an scp_sink_init.
\r
1035 * That's bad. The difference is that scp_sink_setup is called once
\r
1036 * right at the start, whereas scp_sink_init is called to
\r
1037 * initialise every level of recursion in the protocol.
\r
1039 int scp_sink_setup(char *source, int preserve, int recursive)
\r
1044 if (!fxp_init()) {
\r
1045 tell_user(stderr, "unable to initialise SFTP: %s", fxp_error());
\r
1050 * It's possible that the source string we've been given
\r
1051 * contains a wildcard. If so, we must split the directory
\r
1052 * away from the wildcard itself (throwing an error if any
\r
1053 * wildcardness comes before the final slash) and arrange
\r
1054 * things so that a dirstack entry will be set up.
\r
1056 newsource = snewn(1+strlen(source), char);
\r
1057 if (!wc_unescape(newsource, source)) {
\r
1058 /* Yes, here we go; it's a wildcard. Bah. */
\r
1059 char *dupsource, *lastpart, *dirpart, *wildcard;
\r
1060 dupsource = dupstr(source);
\r
1061 lastpart = stripslashes(dupsource, 0);
\r
1062 wildcard = dupstr(lastpart);
\r
1064 if (*dupsource && dupsource[1]) {
\r
1066 * The remains of dupsource are at least two
\r
1067 * characters long, meaning the pathname wasn't
\r
1068 * empty or just `/'. Hence, we remove the trailing
\r
1071 lastpart[-1] = '\0';
\r
1072 } else if (!*dupsource) {
\r
1074 * The remains of dupsource are _empty_ - the whole
\r
1075 * pathname was a wildcard. Hence we need to
\r
1076 * replace it with ".".
\r
1079 dupsource = dupstr(".");
\r
1083 * Now we have separated our string into dupsource (the
\r
1084 * directory part) and wildcard. Both of these will
\r
1085 * need freeing at some point. Next step is to remove
\r
1086 * wildcard escapes from the directory part, throwing
\r
1087 * an error if it contains a real wildcard.
\r
1089 dirpart = snewn(1+strlen(dupsource), char);
\r
1090 if (!wc_unescape(dirpart, dupsource)) {
\r
1091 tell_user(stderr, "%s: multiple-level wildcards unsupported",
\r
1101 * Now we have dirpart (unescaped, ie a valid remote
\r
1102 * path), and wildcard (a wildcard). This will be
\r
1103 * sufficient to arrange a dirstack entry.
\r
1105 scp_sftp_remotepath = dirpart;
\r
1106 scp_sftp_wildcard = wildcard;
\r
1109 scp_sftp_remotepath = newsource;
\r
1110 scp_sftp_wildcard = NULL;
\r
1112 scp_sftp_preserve = preserve;
\r
1113 scp_sftp_recursive = recursive;
\r
1114 scp_sftp_donethistarget = 0;
\r
1115 scp_sftp_dirstack_head = NULL;
\r
1120 int scp_sink_init(void)
\r
1122 if (!using_sftp) {
\r
1123 back->send(backhandle, "", 1);
\r
1128 #define SCP_SINK_FILE 1
\r
1129 #define SCP_SINK_DIR 2
\r
1130 #define SCP_SINK_ENDDIR 3
\r
1131 #define SCP_SINK_RETRY 4 /* not an action; just try again */
\r
1132 struct scp_sink_action {
\r
1133 int action; /* FILE, DIR, ENDDIR */
\r
1134 char *buf; /* will need freeing after use */
\r
1135 char *name; /* filename or dirname (not ENDDIR) */
\r
1136 int mode; /* access mode (not ENDDIR) */
\r
1137 uint64 size; /* file size (not ENDDIR) */
\r
1138 int settime; /* 1 if atime and mtime are filled */
\r
1139 unsigned long atime, mtime; /* access times for the file */
\r
1142 int scp_get_sink_action(struct scp_sink_action *act)
\r
1146 int must_free_fname;
\r
1147 struct fxp_attrs attrs;
\r
1148 struct sftp_packet *pktin;
\r
1149 struct sftp_request *req, *rreq;
\r
1152 if (!scp_sftp_dirstack_head) {
\r
1153 if (!scp_sftp_donethistarget) {
\r
1155 * Simple case: we are only dealing with one file.
\r
1157 fname = scp_sftp_remotepath;
\r
1158 must_free_fname = 0;
\r
1159 scp_sftp_donethistarget = 1;
\r
1162 * Even simpler case: one file _which we've done_.
\r
1163 * Return 1 (finished).
\r
1169 * We're now in the middle of stepping through a list
\r
1170 * of names returned from fxp_readdir(); so let's carry
\r
1173 struct scp_sftp_dirstack *head = scp_sftp_dirstack_head;
\r
1174 while (head->namepos < head->namelen &&
\r
1175 (is_dots(head->names[head->namepos].filename) ||
\r
1176 (head->wildcard &&
\r
1177 !wc_match(head->wildcard,
\r
1178 head->names[head->namepos].filename))))
\r
1179 head->namepos++; /* skip . and .. */
\r
1180 if (head->namepos < head->namelen) {
\r
1181 head->matched_something = 1;
\r
1182 fname = dupcat(head->dirpath, "/",
\r
1183 head->names[head->namepos++].filename,
\r
1185 must_free_fname = 1;
\r
1188 * We've come to the end of the list; pop it off
\r
1189 * the stack and return an ENDDIR action (or RETRY
\r
1190 * if this was a wildcard match).
\r
1192 if (head->wildcard) {
\r
1193 act->action = SCP_SINK_RETRY;
\r
1194 if (!head->matched_something) {
\r
1195 tell_user(stderr, "pscp: wildcard '%s' matched "
\r
1196 "no files", head->wildcard);
\r
1199 sfree(head->wildcard);
\r
1202 act->action = SCP_SINK_ENDDIR;
\r
1205 sfree(head->dirpath);
\r
1206 sfree(head->names);
\r
1207 scp_sftp_dirstack_head = head->next;
\r
1215 * Now we have a filename. Stat it, and see if it's a file
\r
1218 sftp_register(req = fxp_stat_send(fname));
\r
1219 rreq = sftp_find_request(pktin = sftp_recv());
\r
1220 assert(rreq == req);
\r
1221 ret = fxp_stat_recv(pktin, rreq, &attrs);
\r
1223 if (!ret || !(attrs.flags & SSH_FILEXFER_ATTR_PERMISSIONS)) {
\r
1224 tell_user(stderr, "unable to identify %s: %s", fname,
\r
1225 ret ? "file type not supplied" : fxp_error());
\r
1230 if (attrs.permissions & 0040000) {
\r
1231 struct scp_sftp_dirstack *newitem;
\r
1232 struct fxp_handle *dirhandle;
\r
1233 int nnames, namesize;
\r
1234 struct fxp_name *ournames;
\r
1235 struct fxp_names *names;
\r
1238 * It's a directory. If we're not in recursive mode,
\r
1239 * this merits a complaint (which is fatal if the name
\r
1240 * was specified directly, but not if it was matched by
\r
1243 * We skip this complaint completely if
\r
1244 * scp_sftp_wildcard is set, because that's an
\r
1245 * indication that we're not actually supposed to
\r
1246 * _recursively_ transfer the dir, just scan it for
\r
1247 * things matching the wildcard.
\r
1249 if (!scp_sftp_recursive && !scp_sftp_wildcard) {
\r
1250 tell_user(stderr, "pscp: %s: is a directory", fname);
\r
1252 if (must_free_fname) sfree(fname);
\r
1253 if (scp_sftp_dirstack_head) {
\r
1254 act->action = SCP_SINK_RETRY;
\r
1262 * Otherwise, the fun begins. We must fxp_opendir() the
\r
1263 * directory, slurp the filenames into memory, return
\r
1264 * SCP_SINK_DIR (unless this is a wildcard match), and
\r
1265 * set targetisdir. The next time we're called, we will
\r
1266 * run through the list of filenames one by one,
\r
1267 * matching them against a wildcard if present.
\r
1269 * If targetisdir is _already_ set (meaning we're
\r
1270 * already in the middle of going through another such
\r
1271 * list), we must push the other (target,namelist) pair
\r
1274 sftp_register(req = fxp_opendir_send(fname));
\r
1275 rreq = sftp_find_request(pktin = sftp_recv());
\r
1276 assert(rreq == req);
\r
1277 dirhandle = fxp_opendir_recv(pktin, rreq);
\r
1280 tell_user(stderr, "scp: unable to open directory %s: %s",
\r
1281 fname, fxp_error());
\r
1282 if (must_free_fname) sfree(fname);
\r
1286 nnames = namesize = 0;
\r
1291 sftp_register(req = fxp_readdir_send(dirhandle));
\r
1292 rreq = sftp_find_request(pktin = sftp_recv());
\r
1293 assert(rreq == req);
\r
1294 names = fxp_readdir_recv(pktin, rreq);
\r
1296 if (names == NULL) {
\r
1297 if (fxp_error_type() == SSH_FX_EOF)
\r
1299 tell_user(stderr, "scp: reading directory %s: %s\n",
\r
1300 fname, fxp_error());
\r
1301 if (must_free_fname) sfree(fname);
\r
1306 if (names->nnames == 0) {
\r
1307 fxp_free_names(names);
\r
1310 if (nnames + names->nnames >= namesize) {
\r
1311 namesize += names->nnames + 128;
\r
1312 ournames = sresize(ournames, namesize, struct fxp_name);
\r
1314 for (i = 0; i < names->nnames; i++) {
\r
1315 if (!strcmp(names->names[i].filename, ".") ||
\r
1316 !strcmp(names->names[i].filename, "..")) {
\r
1318 * . and .. are normal consequences of
\r
1319 * reading a directory, and aren't worth
\r
1320 * complaining about.
\r
1322 } else if (!vet_filename(names->names[i].filename)) {
\r
1323 tell_user(stderr, "ignoring potentially dangerous server-"
\r
1324 "supplied filename '%s'\n",
\r
1325 names->names[i].filename);
\r
1327 ournames[nnames++] = names->names[i];
\r
1329 names->nnames = 0; /* prevent free_names */
\r
1330 fxp_free_names(names);
\r
1332 sftp_register(req = fxp_close_send(dirhandle));
\r
1333 rreq = sftp_find_request(pktin = sftp_recv());
\r
1334 assert(rreq == req);
\r
1335 fxp_close_recv(pktin, rreq);
\r
1337 newitem = snew(struct scp_sftp_dirstack);
\r
1338 newitem->next = scp_sftp_dirstack_head;
\r
1339 newitem->names = ournames;
\r
1340 newitem->namepos = 0;
\r
1341 newitem->namelen = nnames;
\r
1342 if (must_free_fname)
\r
1343 newitem->dirpath = fname;
\r
1345 newitem->dirpath = dupstr(fname);
\r
1346 if (scp_sftp_wildcard) {
\r
1347 newitem->wildcard = scp_sftp_wildcard;
\r
1348 newitem->matched_something = 0;
\r
1349 scp_sftp_wildcard = NULL;
\r
1351 newitem->wildcard = NULL;
\r
1353 scp_sftp_dirstack_head = newitem;
\r
1355 if (newitem->wildcard) {
\r
1356 act->action = SCP_SINK_RETRY;
\r
1358 act->action = SCP_SINK_DIR;
\r
1359 act->buf = dupstr(stripslashes(fname, 0));
\r
1360 act->name = act->buf;
\r
1361 act->size = uint64_make(0,0); /* duhh, it's a directory */
\r
1362 act->mode = 07777 & attrs.permissions;
\r
1363 if (scp_sftp_preserve &&
\r
1364 (attrs.flags & SSH_FILEXFER_ATTR_ACMODTIME)) {
\r
1365 act->atime = attrs.atime;
\r
1366 act->mtime = attrs.mtime;
\r
1375 * It's a file. Return SCP_SINK_FILE.
\r
1377 act->action = SCP_SINK_FILE;
\r
1378 act->buf = dupstr(stripslashes(fname, 0));
\r
1379 act->name = act->buf;
\r
1380 if (attrs.flags & SSH_FILEXFER_ATTR_SIZE) {
\r
1381 act->size = attrs.size;
\r
1383 act->size = uint64_make(ULONG_MAX,ULONG_MAX); /* no idea */
\r
1384 act->mode = 07777 & attrs.permissions;
\r
1385 if (scp_sftp_preserve &&
\r
1386 (attrs.flags & SSH_FILEXFER_ATTR_ACMODTIME)) {
\r
1387 act->atime = attrs.atime;
\r
1388 act->mtime = attrs.mtime;
\r
1392 if (must_free_fname)
\r
1393 scp_sftp_currentname = fname;
\r
1395 scp_sftp_currentname = dupstr(fname);
\r
1410 if (ssh_scp_recv((unsigned char *) &ch, 1) <= 0)
\r
1413 bump("Protocol error: Unexpected newline");
\r
1417 if (ssh_scp_recv((unsigned char *) &ch, 1) <= 0)
\r
1418 bump("Lost connection");
\r
1419 if (i >= bufsize) {
\r
1420 bufsize = i + 128;
\r
1421 act->buf = sresize(act->buf, bufsize, char);
\r
1423 act->buf[i++] = ch;
\r
1424 } while (ch != '\n');
\r
1425 act->buf[i - 1] = '\0';
\r
1427 case '\01': /* error */
\r
1428 tell_user(stderr, "%s\n", act->buf);
\r
1430 continue; /* go round again */
\r
1431 case '\02': /* fatal error */
\r
1432 bump("%s", act->buf);
\r
1434 back->send(backhandle, "", 1);
\r
1435 act->action = SCP_SINK_ENDDIR;
\r
1438 if (sscanf(act->buf, "%ld %*d %ld %*d",
\r
1439 &act->mtime, &act->atime) == 2) {
\r
1441 back->send(backhandle, "", 1);
\r
1442 continue; /* go round again */
\r
1444 bump("Protocol error: Illegal time format");
\r
1447 act->action = (action == 'C' ? SCP_SINK_FILE : SCP_SINK_DIR);
\r
1450 bump("Protocol error: Expected control record");
\r
1453 * We will go round this loop only once, unless we hit
\r
1454 * `continue' above.
\r
1460 * If we get here, we must have seen SCP_SINK_FILE or
\r
1466 if (sscanf(act->buf, "%o %s %n", &act->mode, sizestr, &i) != 2)
\r
1467 bump("Protocol error: Illegal file descriptor format");
\r
1468 act->size = uint64_from_decimal(sizestr);
\r
1469 act->name = act->buf + i;
\r
1475 int scp_accept_filexfer(void)
\r
1478 struct sftp_packet *pktin;
\r
1479 struct sftp_request *req, *rreq;
\r
1481 sftp_register(req = fxp_open_send(scp_sftp_currentname, SSH_FXF_READ));
\r
1482 rreq = sftp_find_request(pktin = sftp_recv());
\r
1483 assert(rreq == req);
\r
1484 scp_sftp_filehandle = fxp_open_recv(pktin, rreq);
\r
1486 if (!scp_sftp_filehandle) {
\r
1487 tell_user(stderr, "pscp: unable to open %s: %s",
\r
1488 scp_sftp_currentname, fxp_error());
\r
1492 scp_sftp_fileoffset = uint64_make(0, 0);
\r
1493 scp_sftp_xfer = xfer_download_init(scp_sftp_filehandle,
\r
1494 scp_sftp_fileoffset);
\r
1495 sfree(scp_sftp_currentname);
\r
1498 back->send(backhandle, "", 1);
\r
1499 return 0; /* can't fail */
\r
1503 int scp_recv_filedata(char *data, int len)
\r
1506 struct sftp_packet *pktin;
\r
1507 int ret, actuallen;
\r
1510 xfer_download_queue(scp_sftp_xfer);
\r
1511 pktin = sftp_recv();
\r
1512 ret = xfer_download_gotpkt(scp_sftp_xfer, pktin);
\r
1515 tell_user(stderr, "pscp: error while reading: %s", fxp_error());
\r
1520 if (xfer_download_data(scp_sftp_xfer, &vbuf, &actuallen)) {
\r
1522 * This assertion relies on the fact that the natural
\r
1523 * block size used in the xfer manager is at most that
\r
1524 * used in this module. I don't like crossing layers in
\r
1525 * this way, but it'll do for now.
\r
1527 assert(actuallen <= len);
\r
1528 memcpy(data, vbuf, actuallen);
\r
1533 scp_sftp_fileoffset = uint64_add32(scp_sftp_fileoffset, actuallen);
\r
1537 return ssh_scp_recv((unsigned char *) data, len);
\r
1541 int scp_finish_filerecv(void)
\r
1544 struct sftp_packet *pktin;
\r
1545 struct sftp_request *req, *rreq;
\r
1548 * Ensure that xfer_done() will work correctly, so we can
\r
1549 * clean up any outstanding requests from the file
\r
1552 xfer_set_error(scp_sftp_xfer);
\r
1553 while (!xfer_done(scp_sftp_xfer)) {
\r
1557 pktin = sftp_recv();
\r
1558 xfer_download_gotpkt(scp_sftp_xfer, pktin);
\r
1559 if (xfer_download_data(scp_sftp_xfer, &vbuf, &len))
\r
1562 xfer_cleanup(scp_sftp_xfer);
\r
1564 sftp_register(req = fxp_close_send(scp_sftp_filehandle));
\r
1565 rreq = sftp_find_request(pktin = sftp_recv());
\r
1566 assert(rreq == req);
\r
1567 fxp_close_recv(pktin, rreq);
\r
1570 back->send(backhandle, "", 1);
\r
1571 return response();
\r
1575 /* ----------------------------------------------------------------------
\r
1576 * Send an error message to the other side and to the screen.
\r
1577 * Increment error counter.
\r
1579 static void run_err(const char *fmt, ...)
\r
1583 va_start(ap, fmt);
\r
1585 str = dupvprintf(fmt, ap);
\r
1586 str2 = dupcat("scp: ", str, "\n", NULL);
\r
1588 scp_send_errmsg(str2);
\r
1589 tell_user(stderr, "%s", str2);
\r
1595 * Execute the source part of the SCP protocol.
\r
1597 static void source(char *src)
\r
1600 unsigned long mtime, atime;
\r
1605 uint64 stat_bytes;
\r
1606 time_t stat_starttime, stat_lasttime;
\r
1608 attr = file_type(src);
\r
1609 if (attr == FILE_TYPE_NONEXISTENT ||
\r
1610 attr == FILE_TYPE_WEIRD) {
\r
1611 run_err("%s: %s file or directory", src,
\r
1612 (attr == FILE_TYPE_WEIRD ? "Not a" : "No such"));
\r
1616 if (attr == FILE_TYPE_DIRECTORY) {
\r
1619 * Avoid . and .. directories.
\r
1622 p = strrchr(src, '/');
\r
1624 p = strrchr(src, '\\');
\r
1629 if (!strcmp(p, ".") || !strcmp(p, ".."))
\r
1630 /* skip . and .. */ ;
\r
1634 run_err("%s: not a regular file", src);
\r
1639 if ((last = strrchr(src, '/')) == NULL)
\r
1643 if (strrchr(last, '\\') != NULL)
\r
1644 last = strrchr(last, '\\') + 1;
\r
1645 if (last == src && strchr(src, ':') != NULL)
\r
1646 last = strchr(src, ':') + 1;
\r
1648 f = open_existing_file(src, &size, &mtime, &atime);
\r
1650 run_err("%s: Cannot open file", src);
\r
1654 if (scp_send_filetimes(mtime, atime))
\r
1660 uint64_decimal(size, sizestr);
\r
1661 tell_user(stderr, "Sending file %s, size=%s", last, sizestr);
\r
1663 if (scp_send_filename(last, size, 0644))
\r
1666 stat_bytes = uint64_make(0,0);
\r
1667 stat_starttime = time(NULL);
\r
1668 stat_lasttime = 0;
\r
1670 for (i = uint64_make(0,0);
\r
1671 uint64_compare(i,size) < 0;
\r
1672 i = uint64_add32(i,4096)) {
\r
1673 char transbuf[4096];
\r
1676 if (uint64_compare(uint64_add32(i, k),size) > 0) /* i + k > size */
\r
1677 k = (uint64_subtract(size, i)).lo; /* k = size - i; */
\r
1678 if ((j = read_from_file(f, transbuf, k)) != k) {
\r
1681 bump("%s: Read error", src);
\r
1683 if (scp_send_filedata(transbuf, k))
\r
1684 bump("%s: Network error occurred", src);
\r
1687 stat_bytes = uint64_add32(stat_bytes, k);
\r
1688 if (time(NULL) != stat_lasttime ||
\r
1689 (uint64_compare(uint64_add32(i, k), size) == 0)) {
\r
1690 stat_lasttime = time(NULL);
\r
1691 print_stats(last, size, stat_bytes,
\r
1692 stat_starttime, stat_lasttime);
\r
1699 (void) scp_send_finish();
\r
1703 * Recursively send the contents of a directory.
\r
1705 static void rsource(char *src)
\r
1708 char *save_target;
\r
1711 if ((last = strrchr(src, '/')) == NULL)
\r
1715 if (strrchr(last, '\\') != NULL)
\r
1716 last = strrchr(last, '\\') + 1;
\r
1717 if (last == src && strchr(src, ':') != NULL)
\r
1718 last = strchr(src, ':') + 1;
\r
1720 /* maybe send filetime */
\r
1722 save_target = scp_save_remotepath();
\r
1725 tell_user(stderr, "Entering directory: %s", last);
\r
1726 if (scp_send_dirname(last, 0755))
\r
1729 dir = open_directory(src);
\r
1730 if (dir != NULL) {
\r
1732 while ((filename = read_filename(dir)) != NULL) {
\r
1733 char *foundfile = dupcat(src, "/", filename, NULL);
\r
1734 source(foundfile);
\r
1739 close_directory(dir);
\r
1741 (void) scp_send_enddir();
\r
1743 scp_restore_remotepath(save_target);
\r
1747 * Execute the sink part of the SCP protocol.
\r
1749 static void sink(char *targ, char *src)
\r
1752 int targisdir = 0;
\r
1758 uint64 stat_bytes;
\r
1759 time_t stat_starttime, stat_lasttime;
\r
1762 attr = file_type(targ);
\r
1763 if (attr == FILE_TYPE_DIRECTORY)
\r
1766 if (targetshouldbedirectory && !targisdir)
\r
1767 bump("%s: Not a directory", targ);
\r
1771 struct scp_sink_action act;
\r
1772 if (scp_get_sink_action(&act))
\r
1775 if (act.action == SCP_SINK_ENDDIR)
\r
1778 if (act.action == SCP_SINK_RETRY)
\r
1783 * Prevent the remote side from maliciously writing to
\r
1784 * files outside the target area by sending a filename
\r
1785 * containing `../'. In fact, it shouldn't be sending
\r
1786 * filenames with any slashes or colons in at all; so
\r
1787 * we'll find the last slash, backslash or colon in the
\r
1788 * filename and use only the part after that. (And
\r
1791 * In addition, we also ensure here that if we're
\r
1792 * copying a single file and the target is a directory
\r
1793 * (common usage: `pscp host:filename .') the remote
\r
1794 * can't send us a _different_ file name. We can
\r
1795 * distinguish this case because `src' will be non-NULL
\r
1796 * and the last component of that will fail to match
\r
1797 * (the last component of) the name sent.
\r
1799 * Well, not always; if `src' is a wildcard, we do
\r
1800 * expect to get back filenames that don't correspond
\r
1801 * exactly to it. Ideally in this case, we would like
\r
1802 * to ensure that the returned filename actually
\r
1803 * matches the wildcard pattern - but one of SCP's
\r
1804 * protocol infelicities is that wildcard matching is
\r
1805 * done at the server end _by the server's rules_ and
\r
1806 * so in general this is infeasible. Hence, we only
\r
1807 * accept filenames that don't correspond to `src' if
\r
1808 * unsafe mode is enabled or we are using SFTP (which
\r
1809 * resolves remote wildcards on the client side and can
\r
1812 char *striptarget, *stripsrc;
\r
1814 striptarget = stripslashes(act.name, 1);
\r
1815 if (striptarget != act.name) {
\r
1816 tell_user(stderr, "warning: remote host sent a compound"
\r
1817 " pathname '%s'", act.name);
\r
1818 tell_user(stderr, " renaming local file to '%s'",
\r
1823 * Also check to see if the target filename is '.' or
\r
1824 * '..', or indeed '...' and so on because Windows
\r
1825 * appears to interpret those like '..'.
\r
1827 if (is_dots(striptarget)) {
\r
1828 bump("security violation: remote host attempted to write to"
\r
1829 " a '.' or '..' path!");
\r
1833 stripsrc = stripslashes(src, 1);
\r
1834 if (strcmp(striptarget, stripsrc) &&
\r
1835 !using_sftp && !scp_unsafe_mode) {
\r
1836 tell_user(stderr, "warning: remote host tried to write "
\r
1837 "to a file called '%s'", striptarget);
\r
1838 tell_user(stderr, " when we requested a file "
\r
1839 "called '%s'.", stripsrc);
\r
1840 tell_user(stderr, " If this is a wildcard, "
\r
1841 "consider upgrading to SSH-2 or using");
\r
1842 tell_user(stderr, " the '-unsafe' option. Renaming"
\r
1843 " of this file has been disallowed.");
\r
1844 /* Override the name the server provided with our own. */
\r
1845 striptarget = stripsrc;
\r
1849 if (targ[0] != '\0')
\r
1850 destfname = dir_file_cat(targ, striptarget);
\r
1852 destfname = dupstr(striptarget);
\r
1855 * In this branch of the if, the target area is a
\r
1856 * single file with an explicitly specified name in any
\r
1857 * case, so there's no danger.
\r
1859 destfname = dupstr(targ);
\r
1861 attr = file_type(destfname);
\r
1862 exists = (attr != FILE_TYPE_NONEXISTENT);
\r
1864 if (act.action == SCP_SINK_DIR) {
\r
1865 if (exists && attr != FILE_TYPE_DIRECTORY) {
\r
1866 run_err("%s: Not a directory", destfname);
\r
1870 if (!create_directory(destfname)) {
\r
1871 run_err("%s: Cannot create directory", destfname);
\r
1875 sink(destfname, NULL);
\r
1876 /* can we set the timestamp for directories ? */
\r
1880 f = open_new_file(destfname);
\r
1882 run_err("%s: Cannot create file", destfname);
\r
1886 if (scp_accept_filexfer())
\r
1889 stat_bytes = uint64_make(0, 0);
\r
1890 stat_starttime = time(NULL);
\r
1891 stat_lasttime = 0;
\r
1892 stat_name = stripslashes(destfname, 1);
\r
1894 received = uint64_make(0, 0);
\r
1895 while (uint64_compare(received,act.size) < 0) {
\r
1896 char transbuf[32768];
\r
1899 blksize = uint64_make(0, 32768);
\r
1900 if (uint64_compare(blksize,uint64_subtract(act.size,received)) > 0)
\r
1901 blksize = uint64_subtract(act.size,received);
\r
1902 read = scp_recv_filedata(transbuf, (int)blksize.lo);
\r
1904 bump("Lost connection");
\r
1907 if (write_to_file(f, transbuf, read) != (int)read) {
\r
1909 /* FIXME: in sftp we can actually abort the transfer */
\r
1911 printf("\r%-25.25s | %50s\n",
\r
1913 "Write error.. waiting for end of file");
\r
1917 stat_bytes = uint64_add32(stat_bytes,read);
\r
1918 if (time(NULL) > stat_lasttime ||
\r
1919 uint64_compare(uint64_add32(received, read), act.size) == 0) {
\r
1920 stat_lasttime = time(NULL);
\r
1921 print_stats(stat_name, act.size, stat_bytes,
\r
1922 stat_starttime, stat_lasttime);
\r
1925 received = uint64_add32(received, read);
\r
1927 if (act.settime) {
\r
1928 set_file_times(f, act.mtime, act.atime);
\r
1933 run_err("%s: Write error", destfname);
\r
1936 (void) scp_finish_filerecv();
\r
1943 * We will copy local files to a remote server.
\r
1945 static void toremote(int argc, char *argv[])
\r
1947 char *src, *targ, *host, *user;
\r
1951 targ = argv[argc - 1];
\r
1953 /* Separate host from filename */
\r
1955 targ = colon(targ);
\r
1957 bump("targ == NULL in toremote()");
\r
1959 if (*targ == '\0')
\r
1961 /* Substitute "." for empty target */
\r
1963 /* Separate host and username */
\r
1965 host = strrchr(host, '@');
\r
1966 if (host == NULL) {
\r
1971 if (*user == '\0')
\r
1976 if (colon(argv[0]) != NULL)
\r
1977 bump("%s: Remote to remote not supported", argv[0]);
\r
1979 wc_type = test_wildcard(argv[0], 1);
\r
1980 if (wc_type == WCTYPE_NONEXISTENT)
\r
1981 bump("%s: No such file or directory\n", argv[0]);
\r
1982 else if (wc_type == WCTYPE_WILDCARD)
\r
1983 targetshouldbedirectory = 1;
\r
1986 cmd = dupprintf("scp%s%s%s%s -t %s",
\r
1987 verbose ? " -v" : "",
\r
1988 recursive ? " -r" : "",
\r
1989 preserve ? " -p" : "",
\r
1990 targetshouldbedirectory ? " -d" : "", targ);
\r
1991 do_cmd(host, user, cmd);
\r
1994 if (scp_source_setup(targ, targetshouldbedirectory))
\r
1997 for (i = 0; i < argc - 1; i++) {
\r
1999 if (colon(src) != NULL) {
\r
2000 tell_user(stderr, "%s: Remote to remote not supported\n", src);
\r
2005 wc_type = test_wildcard(src, 1);
\r
2006 if (wc_type == WCTYPE_NONEXISTENT) {
\r
2007 run_err("%s: No such file or directory", src);
\r
2009 } else if (wc_type == WCTYPE_FILENAME) {
\r
2013 WildcardMatcher *wc;
\r
2016 wc = begin_wildcard_matching(src);
\r
2018 run_err("%s: No such file or directory", src);
\r
2022 while ((filename = wildcard_get_filename(wc)) != NULL) {
\r
2027 finish_wildcard_matching(wc);
\r
2033 * We will copy files from a remote server to the local machine.
\r
2035 static void tolocal(int argc, char *argv[])
\r
2037 char *src, *targ, *host, *user;
\r
2041 bump("More than one remote source not supported");
\r
2046 /* Separate host from filename */
\r
2050 bump("Local to local copy not supported");
\r
2054 /* Substitute "." for empty filename */
\r
2056 /* Separate username and hostname */
\r
2058 host = strrchr(host, '@');
\r
2059 if (host == NULL) {
\r
2064 if (*user == '\0')
\r
2068 cmd = dupprintf("scp%s%s%s%s -f %s",
\r
2069 verbose ? " -v" : "",
\r
2070 recursive ? " -r" : "",
\r
2071 preserve ? " -p" : "",
\r
2072 targetshouldbedirectory ? " -d" : "", src);
\r
2073 do_cmd(host, user, cmd);
\r
2076 if (scp_sink_setup(src, preserve, recursive))
\r
2083 * We will issue a list command to get a remote directory.
\r
2085 static void get_dir_list(int argc, char *argv[])
\r
2087 char *src, *host, *user;
\r
2088 char *cmd, *p, *q;
\r
2093 /* Separate host from filename */
\r
2097 bump("Local file listing not supported");
\r
2101 /* Substitute "." for empty filename */
\r
2103 /* Separate username and hostname */
\r
2105 host = strrchr(host, '@');
\r
2106 if (host == NULL) {
\r
2111 if (*user == '\0')
\r
2115 cmd = snewn(4 * strlen(src) + 100, char);
\r
2116 strcpy(cmd, "ls -la '");
\r
2117 p = cmd + strlen(cmd);
\r
2118 for (q = src; *q; q++) {
\r
2131 do_cmd(host, user, cmd);
\r
2135 scp_sftp_listdir(src);
\r
2137 while (ssh_scp_recv((unsigned char *) &c, 1) > 0)
\r
2138 tell_char(stdout, c);
\r
2143 * Short description of parameters.
\r
2145 static void usage(void)
\r
2147 printf("PuTTY Secure Copy client\n");
\r
2148 printf("%s\n", ver);
\r
2149 printf("Usage: pscp [options] [user@]host:source target\n");
\r
2151 (" pscp [options] source [source...] [user@]host:target\n");
\r
2152 printf(" pscp [options] -ls [user@]host:filespec\n");
\r
2153 printf("Options:\n");
\r
2154 printf(" -V print version information and exit\n");
\r
2155 printf(" -pgpfp print PGP key fingerprints and exit\n");
\r
2156 printf(" -p preserve file attributes\n");
\r
2157 printf(" -q quiet, don't show statistics\n");
\r
2158 printf(" -r copy directories recursively\n");
\r
2159 printf(" -v show verbose messages\n");
\r
2160 printf(" -load sessname Load settings from saved session\n");
\r
2161 printf(" -P port connect to specified port\n");
\r
2162 printf(" -l user connect with specified username\n");
\r
2163 printf(" -pw passw login with specified password\n");
\r
2164 printf(" -1 -2 force use of particular SSH protocol version\n");
\r
2165 printf(" -4 -6 force use of IPv4 or IPv6\n");
\r
2166 printf(" -C enable compression\n");
\r
2167 printf(" -i key private key file for authentication\n");
\r
2168 printf(" -noagent disable use of Pageant\n");
\r
2169 printf(" -agent enable use of Pageant\n");
\r
2170 printf(" -batch disable all interactive prompts\n");
\r
2171 printf(" -unsafe allow server-side wildcards (DANGEROUS)\n");
\r
2172 printf(" -sftp force use of SFTP protocol\n");
\r
2173 printf(" -scp force use of SCP protocol\n");
\r
2176 * -gui is an internal option, used by GUI front ends to get
\r
2177 * pscp to pass progress reports back to them. It's not an
\r
2178 * ordinary user-accessible option, so it shouldn't be part of
\r
2179 * the command-line help. The only people who need to know
\r
2180 * about it are programmers, and they can read the source.
\r
2183 (" -gui hWnd GUI mode with the windows handle for receiving messages\n");
\r
2188 void version(void)
\r
2190 printf("pscp: %s\n", ver);
\r
2194 void cmdline_error(char *p, ...)
\r
2197 fprintf(stderr, "pscp: ");
\r
2199 vfprintf(stderr, p, ap);
\r
2201 fprintf(stderr, "\n try typing just \"pscp\" for help\n");
\r
2206 * Main program. (Called `psftp_main' because it gets called from
\r
2207 * *sftp.c; bit silly, I know, but it had to be called _something_.)
\r
2209 int psftp_main(int argc, char *argv[])
\r
2213 default_protocol = PROT_TELNET;
\r
2215 flags = FLAG_STDERR
\r
2216 #ifdef FLAG_SYNCAGENT
\r
2220 cmdline_tooltype = TOOLTYPE_FILETRANSFER;
\r
2223 /* Load Default Settings before doing anything else. */
\r
2224 do_defaults(NULL, &cfg);
\r
2225 loaded_session = FALSE;
\r
2227 for (i = 1; i < argc; i++) {
\r
2229 if (argv[i][0] != '-')
\r
2231 ret = cmdline_process_param(argv[i], i+1<argc?argv[i+1]:NULL, 1, &cfg);
\r
2233 cmdline_error("option \"%s\" requires an argument", argv[i]);
\r
2234 } else if (ret == 2) {
\r
2235 i++; /* skip next argument */
\r
2236 } else if (ret == 1) {
\r
2237 /* We have our own verbosity in addition to `flags'. */
\r
2238 if (flags & FLAG_VERBOSE)
\r
2240 } else if (strcmp(argv[i], "-pgpfp") == 0) {
\r
2241 pgp_fingerprints();
\r
2243 } else if (strcmp(argv[i], "-r") == 0) {
\r
2245 } else if (strcmp(argv[i], "-p") == 0) {
\r
2247 } else if (strcmp(argv[i], "-q") == 0) {
\r
2249 } else if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "-?") == 0) {
\r
2251 } else if (strcmp(argv[i], "-V") == 0) {
\r
2253 } else if (strcmp(argv[i], "-ls") == 0) {
\r
2255 } else if (strcmp(argv[i], "-batch") == 0) {
\r
2256 console_batch_mode = 1;
\r
2257 } else if (strcmp(argv[i], "-unsafe") == 0) {
\r
2258 scp_unsafe_mode = 1;
\r
2259 } else if (strcmp(argv[i], "-sftp") == 0) {
\r
2260 try_scp = 0; try_sftp = 1;
\r
2261 } else if (strcmp(argv[i], "-scp") == 0) {
\r
2262 try_scp = 1; try_sftp = 0;
\r
2263 } else if (strcmp(argv[i], "--") == 0) {
\r
2267 cmdline_error("unknown option \"%s\"", argv[i]);
\r
2277 get_dir_list(argc, argv);
\r
2284 targetshouldbedirectory = 1;
\r
2286 if (colon(argv[argc - 1]) != NULL)
\r
2287 toremote(argc, argv);
\r
2289 tolocal(argc, argv);
\r
2292 if (back != NULL && back->connected(backhandle)) {
\r
2294 back->special(backhandle, TS_EOF);
\r
2295 ssh_scp_recv((unsigned char *) &ch, 1);
\r
2297 random_save_seed();
\r
2299 cmdline_cleanup();
\r
2300 console_provide_logctx(NULL);
\r
2301 back->free(backhandle);
\r
2302 backhandle = NULL;
\r
2305 return (errs == 0 ? 0 : 1);
\r