2 * scp.c - Scp (Secure Copy) client for PuTTY.
3 * Joris van Rantwijk, Simon Tatham
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.
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.)
22 #define PUTTY_DO_GLOBALS
31 static int verbose
= 0;
32 static int recursive
= 0;
33 static int preserve
= 0;
34 static int targetshouldbedirectory
= 0;
35 static int statistics
= 1;
36 static int prev_stats_len
= 0;
37 static int scp_unsafe_mode
= 0;
39 static int try_scp
= 1;
40 static int try_sftp
= 1;
41 static int main_cmd_is_sftp
= 0;
42 static int fallback_cmd_is_sftp
= 0;
43 static int using_sftp
= 0;
44 static int uploading
= 0;
47 static void *backhandle
;
51 static void source(char *src
);
52 static void rsource(char *src
);
53 static void sink(char *targ
, char *src
);
55 const char *const appname
= "PSCP";
58 * The maximum amount of queued data we accept before we stop and
59 * wait for the server to process some.
61 #define MAX_SCP_BUFSIZE 16384
63 void ldisc_send(void *handle
, char *buf
, int len
, int interactive
)
66 * This is only here because of the calls to ldisc_send(NULL,
67 * 0) in ssh.c. Nothing in PSCP actually needs to use the ldisc
68 * as an ldisc. So if we get called with any real data, I want
74 static void tell_char(FILE * stream
, char c
)
79 static void tell_str(FILE * stream
, char *str
)
83 for (i
= 0; i
< strlen(str
); ++i
)
84 tell_char(stream
, str
[i
]);
87 static void tell_user(FILE * stream
, char *fmt
, ...)
92 str
= dupvprintf(fmt
, ap
);
94 str2
= dupcat(str
, "\n", NULL
);
96 tell_str(stream
, str2
);
101 * Print an error message and perform a fatal exit.
103 void fatalbox(char *fmt
, ...)
108 str
= dupvprintf(fmt
, ap
);
109 str2
= dupcat("Fatal: ", str
, "\n", NULL
);
112 tell_str(stderr
, str2
);
118 void modalfatalbox(char *fmt
, ...)
123 str
= dupvprintf(fmt
, ap
);
124 str2
= dupcat("Fatal: ", str
, "\n", NULL
);
127 tell_str(stderr
, str2
);
133 void nonfatal(char *fmt
, ...)
138 str
= dupvprintf(fmt
, ap
);
139 str2
= dupcat("Error: ", str
, "\n", NULL
);
142 tell_str(stderr
, str2
);
146 void connection_fatal(void *frontend
, char *fmt
, ...)
151 str
= dupvprintf(fmt
, ap
);
152 str2
= dupcat("Fatal: ", str
, "\n", NULL
);
155 tell_str(stderr
, str2
);
163 * In pscp, all agent requests should be synchronous, so this is a
166 void agent_schedule_callback(void (*callback
)(void *, void *, int),
167 void *callback_ctx
, void *data
, int len
)
169 assert(!"We shouldn't be here");
173 * Receive a block of data from the SSH link. Block until all data
176 * To do this, we repeatedly call the SSH protocol module, with our
177 * own trap in from_backend() to catch the data that comes back. We
178 * do this until we have enough data.
181 static unsigned char *outptr
; /* where to put the data */
182 static unsigned outlen
; /* how much data required */
183 static unsigned char *pending
= NULL
; /* any spare data */
184 static unsigned pendlen
= 0, pendsize
= 0; /* length and phys. size of buffer */
185 int from_backend(void *frontend
, int is_stderr
, const char *data
, int datalen
)
187 unsigned char *p
= (unsigned char *) data
;
188 unsigned len
= (unsigned) datalen
;
191 * stderr data is just spouted to local stderr and otherwise
196 if (fwrite(data
, 1, len
, stderr
) < len
)
201 if ((outlen
> 0) && (len
> 0)) {
202 unsigned used
= outlen
;
205 memcpy(outptr
, p
, used
);
213 if (pendsize
< pendlen
+ len
) {
214 pendsize
= pendlen
+ len
+ 4096;
215 pending
= sresize(pending
, pendsize
, unsigned char);
217 memcpy(pending
+ pendlen
, p
, len
);
223 int from_backend_untrusted(void *frontend_handle
, const char *data
, int len
)
226 * No "untrusted" output should get here (the way the code is
227 * currently, it's all diverted by FLAG_STDERR).
229 assert(!"Unexpected call to from_backend_untrusted()");
230 return 0; /* not reached */
232 int from_backend_eof(void *frontend
)
235 * We usually expect to be the party deciding when to close the
236 * connection, so if we see EOF before we sent it ourselves, we
237 * should panic. The exception is if we're using old-style scp and
238 * downloading rather than uploading.
240 if ((using_sftp
|| uploading
) && !sent_eof
) {
241 connection_fatal(frontend
,
242 "Received unexpected end-of-file from server");
246 static int ssh_scp_recv(unsigned char *buf
, int len
)
252 * See if the pending-input block contains some of what we
256 unsigned pendused
= pendlen
;
257 if (pendused
> outlen
)
259 memcpy(outptr
, pending
, pendused
);
260 memmove(pending
, pending
+ pendused
, pendlen
- pendused
);
274 if (back
->exitcode(backhandle
) >= 0 || ssh_sftp_loop_iteration() < 0)
282 * Loop through the ssh connection and authentication process.
284 static void ssh_scp_init(void)
286 while (!back
->sendok(backhandle
)) {
287 if (back
->exitcode(backhandle
) >= 0) {
291 if (ssh_sftp_loop_iteration() < 0) {
297 /* Work out which backend we ended up using. */
298 if (!ssh_fallback_cmd(backhandle
))
299 using_sftp
= main_cmd_is_sftp
;
301 using_sftp
= fallback_cmd_is_sftp
;
305 tell_user(stderr
, "Using SFTP");
307 tell_user(stderr
, "Using SCP1");
312 * Print an error message and exit after closing the SSH link.
314 static void bump(char *fmt
, ...)
319 str
= dupvprintf(fmt
, ap
);
321 str2
= dupcat(str
, "\n", NULL
);
323 tell_str(stderr
, str2
);
327 if (back
!= NULL
&& back
->connected(backhandle
)) {
329 back
->special(backhandle
, TS_EOF
);
331 ssh_scp_recv((unsigned char *) &ch
, 1);
338 * Wait for the reply to a single SFTP request. Parallels the same
339 * function in psftp.c (but isn't centralised into sftp.c because the
340 * latter module handles SFTP only and shouldn't assume that SFTP is
341 * the only thing going on by calling connection_fatal).
343 struct sftp_packet
*sftp_wait_for_reply(struct sftp_request
*req
)
345 struct sftp_packet
*pktin
;
346 struct sftp_request
*rreq
;
351 connection_fatal(NULL
, "did not receive SFTP response packet "
353 rreq
= sftp_find_request(pktin
);
355 connection_fatal(NULL
, "unable to understand SFTP response packet "
356 "from server: %s", fxp_error());
361 * Open an SSH connection to user@host and execute cmd.
363 static void do_cmd(char *host
, char *user
, char *cmd
)
369 if (host
== NULL
|| host
[0] == '\0')
370 bump("Empty host name");
373 * Remove fiddly bits of address: remove a colon suffix, and
374 * the square brackets around an IPv6 literal address.
376 if (host
[0] == '[') {
378 host
[strcspn(host
, "]")] = '\0';
380 host
[strcspn(host
, ":")] = '\0';
384 * If we haven't loaded session details already (e.g., from -load),
385 * try looking for a session called "host".
387 if (!loaded_session
) {
388 /* Try to load settings for `host' into a temporary config */
389 Conf
*conf2
= conf_new();
390 conf_set_str(conf2
, CONF_host
, "");
391 do_defaults(host
, conf2
);
392 if (conf_get_str(conf2
, CONF_host
)[0] != '\0') {
393 /* Settings present and include hostname */
394 /* Re-load data into the real config. */
395 do_defaults(host
, conf
);
397 /* Session doesn't exist or mention a hostname. */
398 /* Use `host' as a bare hostname. */
399 conf_set_str(conf
, CONF_host
, host
);
402 /* Patch in hostname `host' to session details. */
403 conf_set_str(conf
, CONF_host
, host
);
407 * Force use of SSH. (If they got the protocol wrong we assume the
408 * port is useless too.)
410 if (conf_get_int(conf
, CONF_protocol
) != PROT_SSH
) {
411 conf_set_int(conf
, CONF_protocol
, PROT_SSH
);
412 conf_set_int(conf
, CONF_port
, 22);
416 * Enact command-line overrides.
418 cmdline_run_saved(conf
);
421 * Muck about with the hostname in various ways.
424 char *hostbuf
= dupstr(conf_get_str(conf
, CONF_host
));
425 char *host
= hostbuf
;
429 * Trim leading whitespace.
431 host
+= strspn(host
, " \t");
434 * See if host is of the form user@host, and separate out
435 * the username if so.
437 if (host
[0] != '\0') {
438 char *atsign
= strrchr(host
, '@');
441 conf_set_str(conf
, CONF_username
, host
);
447 * Remove any remaining whitespace.
452 if (*q
!= ' ' && *q
!= '\t')
458 conf_set_str(conf
, CONF_host
, hostbuf
);
463 if (user
!= NULL
&& user
[0] != '\0') {
464 conf_set_str(conf
, CONF_username
, user
);
465 } else if (conf_get_str(conf
, CONF_username
)[0] == '\0') {
466 user
= get_username();
468 bump("Empty user name");
471 tell_user(stderr
, "Guessing user name: %s", user
);
472 conf_set_str(conf
, CONF_username
, user
);
478 * Disable scary things which shouldn't be enabled for simple
479 * things like SCP and SFTP: agent forwarding, port forwarding,
482 conf_set_int(conf
, CONF_x11_forward
, 0);
483 conf_set_int(conf
, CONF_agentfwd
, 0);
484 conf_set_int(conf
, CONF_ssh_simple
, TRUE
);
487 while ((key
= conf_get_str_nthstrkey(conf
, CONF_portfwd
, 0)) != NULL
)
488 conf_del_str_str(conf
, CONF_portfwd
, key
);
492 * Set up main and possibly fallback command depending on
493 * options specified by user.
494 * Attempt to start the SFTP subsystem as a first choice,
495 * falling back to the provided scp command if that fails.
497 conf_set_str(conf
, CONF_remote_cmd2
, "");
499 /* First choice is SFTP subsystem. */
500 main_cmd_is_sftp
= 1;
501 conf_set_str(conf
, CONF_remote_cmd
, "sftp");
502 conf_set_int(conf
, CONF_ssh_subsys
, TRUE
);
504 /* Fallback is to use the provided scp command. */
505 fallback_cmd_is_sftp
= 0;
506 conf_set_str(conf
, CONF_remote_cmd2
, cmd
);
507 conf_set_int(conf
, CONF_ssh_subsys2
, FALSE
);
509 /* Since we're not going to try SCP, we may as well try
510 * harder to find an SFTP server, since in the current
511 * implementation we have a spare slot. */
512 fallback_cmd_is_sftp
= 1;
513 /* see psftp.c for full explanation of this kludge */
514 conf_set_str(conf
, CONF_remote_cmd2
,
515 "test -x /usr/lib/sftp-server &&"
516 " exec /usr/lib/sftp-server\n"
517 "test -x /usr/local/lib/sftp-server &&"
518 " exec /usr/local/lib/sftp-server\n"
520 conf_set_int(conf
, CONF_ssh_subsys2
, FALSE
);
523 /* Don't try SFTP at all; just try the scp command. */
524 main_cmd_is_sftp
= 0;
525 conf_set_str(conf
, CONF_remote_cmd
, cmd
);
526 conf_set_int(conf
, CONF_ssh_subsys
, FALSE
);
528 conf_set_int(conf
, CONF_nopty
, TRUE
);
532 err
= back
->init(NULL
, &backhandle
, conf
,
533 conf_get_str(conf
, CONF_host
),
534 conf_get_int(conf
, CONF_port
),
536 conf_get_int(conf
, CONF_tcp_keepalives
));
538 bump("ssh_init: %s", err
);
539 logctx
= log_init(NULL
, conf
);
540 back
->provide_logctx(backhandle
, logctx
);
541 console_provide_logctx(logctx
);
543 if (verbose
&& realhost
!= NULL
&& errs
== 0)
544 tell_user(stderr
, "Connected to %s", realhost
);
549 * Update statistic information about current file.
551 static void print_stats(char *name
, uint64 size
, uint64 done
,
552 time_t start
, time_t now
)
563 elap
= (unsigned long) difftime(now
, start
);
566 ratebs
= (float) (uint64_to_double(done
) / elap
);
568 ratebs
= (float) uint64_to_double(done
);
571 eta
= (unsigned long) (uint64_to_double(uint64_subtract(size
, done
)));
573 eta
= (unsigned long)
574 ((uint64_to_double(uint64_subtract(size
, done
)) / ratebs
));
577 etastr
= dupprintf("%02ld:%02ld:%02ld",
578 eta
/ 3600, (eta
% 3600) / 60, eta
% 60);
580 donedbl
= uint64_to_double(done
);
581 sizedbl
= uint64_to_double(size
);
582 pct
= (int) (100 * (donedbl
* 1.0 / sizedbl
));
586 /* divide by 1024 to provide kB */
587 uint64_decimal(uint64_shift_right(done
, 10), donekb
);
588 len
= printf("\r%-25.25s | %s kB | %5.1f kB/s | ETA: %8s | %3d%%",
590 donekb
, ratebs
/ 1024.0, etastr
, pct
);
591 if (len
< prev_stats_len
)
592 printf("%*s", prev_stats_len
- len
, "");
593 prev_stats_len
= len
;
595 if (uint64_compare(done
, size
) == 0)
605 * Find a colon in str and return a pointer to the colon.
606 * This is used to separate hostname from filename.
608 static char *colon(char *str
)
610 /* We ignore a leading colon, since the hostname cannot be
611 empty. We also ignore a colon as second character because
612 of filenames like f:myfile.txt. */
613 if (str
[0] == '\0' || str
[0] == ':' ||
614 (str
[0] != '[' && str
[1] == ':'))
616 while (*str
!= '\0' && *str
!= ':' && *str
!= '/' && *str
!= '\\') {
618 /* Skip over IPv6 literal addresses
619 * (eg: 'jeroen@[2001:db8::1]:myfile.txt') */
620 char *ipv6_end
= strchr(str
, ']');
634 * Return a pointer to the portion of str that comes after the last
635 * slash (or backslash or colon, if `local' is TRUE).
637 static char *stripslashes(char *str
, int local
)
642 p
= strchr(str
, ':');
646 p
= strrchr(str
, '/');
650 p
= strrchr(str
, '\\');
658 * Determine whether a string is entirely composed of dots.
660 static int is_dots(char *str
)
662 return str
[strspn(str
, ".")] == '\0';
666 * Wait for a response from the other side.
667 * Return 0 if ok, -1 if error.
669 static int response(void)
671 char ch
, resp
, rbuf
[2048];
674 if (ssh_scp_recv((unsigned char *) &resp
, 1) <= 0)
675 bump("Lost connection");
685 case 2: /* fatal error */
687 if (ssh_scp_recv((unsigned char *) &ch
, 1) <= 0)
688 bump("Protocol error: Lost connection");
690 } while (p
< sizeof(rbuf
) && ch
!= '\n');
693 tell_user(stderr
, "%s", rbuf
);
701 int sftp_recvdata(char *buf
, int len
)
703 return ssh_scp_recv((unsigned char *) buf
, len
);
705 int sftp_senddata(char *buf
, int len
)
707 back
->send(backhandle
, buf
, len
);
711 /* ----------------------------------------------------------------------
712 * sftp-based replacement for the hacky `pscp -ls'.
714 static int sftp_ls_compare(const void *av
, const void *bv
)
716 const struct fxp_name
*a
= (const struct fxp_name
*) av
;
717 const struct fxp_name
*b
= (const struct fxp_name
*) bv
;
718 return strcmp(a
->filename
, b
->filename
);
720 void scp_sftp_listdir(char *dirname
)
722 struct fxp_handle
*dirh
;
723 struct fxp_names
*names
;
724 struct fxp_name
*ournames
;
725 struct sftp_packet
*pktin
;
726 struct sftp_request
*req
;
727 int nnames
, namesize
;
731 tell_user(stderr
, "unable to initialise SFTP: %s", fxp_error());
736 printf("Listing directory %s\n", dirname
);
738 req
= fxp_opendir_send(dirname
);
739 pktin
= sftp_wait_for_reply(req
);
740 dirh
= fxp_opendir_recv(pktin
, req
);
743 printf("Unable to open %s: %s\n", dirname
, fxp_error());
745 nnames
= namesize
= 0;
750 req
= fxp_readdir_send(dirh
);
751 pktin
= sftp_wait_for_reply(req
);
752 names
= fxp_readdir_recv(pktin
, req
);
755 if (fxp_error_type() == SSH_FX_EOF
)
757 printf("Reading directory %s: %s\n", dirname
, fxp_error());
760 if (names
->nnames
== 0) {
761 fxp_free_names(names
);
765 if (nnames
+ names
->nnames
>= namesize
) {
766 namesize
+= names
->nnames
+ 128;
767 ournames
= sresize(ournames
, namesize
, struct fxp_name
);
770 for (i
= 0; i
< names
->nnames
; i
++)
771 ournames
[nnames
++] = names
->names
[i
];
772 names
->nnames
= 0; /* prevent free_names */
773 fxp_free_names(names
);
775 req
= fxp_close_send(dirh
);
776 pktin
= sftp_wait_for_reply(req
);
777 fxp_close_recv(pktin
, req
);
780 * Now we have our filenames. Sort them by actual file
781 * name, and then output the longname parts.
784 qsort(ournames
, nnames
, sizeof(*ournames
), sftp_ls_compare
);
789 for (i
= 0; i
< nnames
; i
++)
790 printf("%s\n", ournames
[i
].longname
);
796 /* ----------------------------------------------------------------------
797 * Helper routines that contain the actual SCP protocol elements,
798 * implemented both as SCP1 and SFTP.
801 static struct scp_sftp_dirstack
{
802 struct scp_sftp_dirstack
*next
;
803 struct fxp_name
*names
;
804 int namepos
, namelen
;
807 int matched_something
; /* wildcard match set was non-empty */
808 } *scp_sftp_dirstack_head
;
809 static char *scp_sftp_remotepath
, *scp_sftp_currentname
;
810 static char *scp_sftp_wildcard
;
811 static int scp_sftp_targetisdir
, scp_sftp_donethistarget
;
812 static int scp_sftp_preserve
, scp_sftp_recursive
;
813 static unsigned long scp_sftp_mtime
, scp_sftp_atime
;
814 static int scp_has_times
;
815 static struct fxp_handle
*scp_sftp_filehandle
;
816 static struct fxp_xfer
*scp_sftp_xfer
;
817 static uint64 scp_sftp_fileoffset
;
819 int scp_source_setup(char *target
, int shouldbedir
)
823 * Find out whether the target filespec is in fact a
826 struct sftp_packet
*pktin
;
827 struct sftp_request
*req
;
828 struct fxp_attrs attrs
;
832 tell_user(stderr
, "unable to initialise SFTP: %s", fxp_error());
837 req
= fxp_stat_send(target
);
838 pktin
= sftp_wait_for_reply(req
);
839 ret
= fxp_stat_recv(pktin
, req
, &attrs
);
841 if (!ret
|| !(attrs
.flags
& SSH_FILEXFER_ATTR_PERMISSIONS
))
842 scp_sftp_targetisdir
= 0;
844 scp_sftp_targetisdir
= (attrs
.permissions
& 0040000) != 0;
846 if (shouldbedir
&& !scp_sftp_targetisdir
) {
847 bump("pscp: remote filespec %s: not a directory\n", target
);
850 scp_sftp_remotepath
= dupstr(target
);
859 int scp_send_errmsg(char *str
)
862 /* do nothing; we never need to send our errors to the server */
864 back
->send(backhandle
, "\001", 1);/* scp protocol error prefix */
865 back
->send(backhandle
, str
, strlen(str
));
867 return 0; /* can't fail */
870 int scp_send_filetimes(unsigned long mtime
, unsigned long atime
)
873 scp_sftp_mtime
= mtime
;
874 scp_sftp_atime
= atime
;
879 sprintf(buf
, "T%lu 0 %lu 0\n", mtime
, atime
);
880 back
->send(backhandle
, buf
, strlen(buf
));
885 int scp_send_filename(char *name
, uint64 size
, int permissions
)
889 struct sftp_packet
*pktin
;
890 struct sftp_request
*req
;
891 struct fxp_attrs attrs
;
893 if (scp_sftp_targetisdir
) {
894 fullname
= dupcat(scp_sftp_remotepath
, "/", name
, NULL
);
896 fullname
= dupstr(scp_sftp_remotepath
);
900 PUT_PERMISSIONS(attrs
, permissions
);
902 req
= fxp_open_send(fullname
,
903 SSH_FXF_WRITE
| SSH_FXF_CREAT
| SSH_FXF_TRUNC
,
905 pktin
= sftp_wait_for_reply(req
);
906 scp_sftp_filehandle
= fxp_open_recv(pktin
, req
);
908 if (!scp_sftp_filehandle
) {
909 tell_user(stderr
, "pscp: unable to open %s: %s",
910 fullname
, fxp_error());
915 scp_sftp_fileoffset
= uint64_make(0, 0);
916 scp_sftp_xfer
= xfer_upload_init(scp_sftp_filehandle
,
917 scp_sftp_fileoffset
);
923 uint64_decimal(size
, sizestr
);
926 sprintf(buf
, "C%04o %s ", (int)(permissions
& 07777), sizestr
);
927 back
->send(backhandle
, buf
, strlen(buf
));
928 back
->send(backhandle
, name
, strlen(name
));
929 back
->send(backhandle
, "\n", 1);
934 int scp_send_filedata(char *data
, int len
)
938 struct sftp_packet
*pktin
;
940 if (!scp_sftp_filehandle
) {
944 while (!xfer_upload_ready(scp_sftp_xfer
)) {
946 ret
= xfer_upload_gotpkt(scp_sftp_xfer
, pktin
);
948 tell_user(stderr
, "error while writing: %s", fxp_error());
949 if (ret
== INT_MIN
) /* pktin not even freed */
956 xfer_upload_data(scp_sftp_xfer
, data
, len
);
958 scp_sftp_fileoffset
= uint64_add32(scp_sftp_fileoffset
, len
);
961 int bufsize
= back
->send(backhandle
, data
, len
);
964 * If the network transfer is backing up - that is, the
965 * remote site is not accepting data as fast as we can
966 * produce it - then we must loop on network events until
967 * we have space in the buffer again.
969 while (bufsize
> MAX_SCP_BUFSIZE
) {
970 if (ssh_sftp_loop_iteration() < 0)
972 bufsize
= back
->sendbuffer(backhandle
);
979 int scp_send_finish(void)
982 struct fxp_attrs attrs
;
983 struct sftp_packet
*pktin
;
984 struct sftp_request
*req
;
987 while (!xfer_done(scp_sftp_xfer
)) {
989 ret
= xfer_upload_gotpkt(scp_sftp_xfer
, pktin
);
991 tell_user(stderr
, "error while writing: %s", fxp_error());
992 if (ret
== INT_MIN
) /* pktin not even freed */
998 xfer_cleanup(scp_sftp_xfer
);
1000 if (!scp_sftp_filehandle
) {
1003 if (scp_has_times
) {
1004 attrs
.flags
= SSH_FILEXFER_ATTR_ACMODTIME
;
1005 attrs
.atime
= scp_sftp_atime
;
1006 attrs
.mtime
= scp_sftp_mtime
;
1007 req
= fxp_fsetstat_send(scp_sftp_filehandle
, attrs
);
1008 pktin
= sftp_wait_for_reply(req
);
1009 ret
= fxp_fsetstat_recv(pktin
, req
);
1011 tell_user(stderr
, "unable to set file times: %s", fxp_error());
1015 req
= fxp_close_send(scp_sftp_filehandle
);
1016 pktin
= sftp_wait_for_reply(req
);
1017 fxp_close_recv(pktin
, req
);
1021 back
->send(backhandle
, "", 1);
1026 char *scp_save_remotepath(void)
1029 return scp_sftp_remotepath
;
1034 void scp_restore_remotepath(char *data
)
1037 scp_sftp_remotepath
= data
;
1040 int scp_send_dirname(char *name
, int modes
)
1045 struct fxp_attrs attrs
;
1046 struct sftp_packet
*pktin
;
1047 struct sftp_request
*req
;
1050 if (scp_sftp_targetisdir
) {
1051 fullname
= dupcat(scp_sftp_remotepath
, "/", name
, NULL
);
1053 fullname
= dupstr(scp_sftp_remotepath
);
1057 * We don't worry about whether we managed to create the
1058 * directory, because if it exists already it's OK just to
1059 * use it. Instead, we will stat it afterwards, and if it
1060 * exists and is a directory we will assume we were either
1061 * successful or it didn't matter.
1063 req
= fxp_mkdir_send(fullname
);
1064 pktin
= sftp_wait_for_reply(req
);
1065 ret
= fxp_mkdir_recv(pktin
, req
);
1070 err
= "server reported no error";
1072 req
= fxp_stat_send(fullname
);
1073 pktin
= sftp_wait_for_reply(req
);
1074 ret
= fxp_stat_recv(pktin
, req
, &attrs
);
1076 if (!ret
|| !(attrs
.flags
& SSH_FILEXFER_ATTR_PERMISSIONS
) ||
1077 !(attrs
.permissions
& 0040000)) {
1078 tell_user(stderr
, "unable to create directory %s: %s",
1085 scp_sftp_remotepath
= fullname
;
1090 sprintf(buf
, "D%04o 0 ", modes
);
1091 back
->send(backhandle
, buf
, strlen(buf
));
1092 back
->send(backhandle
, name
, strlen(name
));
1093 back
->send(backhandle
, "\n", 1);
1098 int scp_send_enddir(void)
1101 sfree(scp_sftp_remotepath
);
1104 back
->send(backhandle
, "E\n", 2);
1110 * Yes, I know; I have an scp_sink_setup _and_ an scp_sink_init.
1111 * That's bad. The difference is that scp_sink_setup is called once
1112 * right at the start, whereas scp_sink_init is called to
1113 * initialise every level of recursion in the protocol.
1115 int scp_sink_setup(char *source
, int preserve
, int recursive
)
1121 tell_user(stderr
, "unable to initialise SFTP: %s", fxp_error());
1126 * It's possible that the source string we've been given
1127 * contains a wildcard. If so, we must split the directory
1128 * away from the wildcard itself (throwing an error if any
1129 * wildcardness comes before the final slash) and arrange
1130 * things so that a dirstack entry will be set up.
1132 newsource
= snewn(1+strlen(source
), char);
1133 if (!wc_unescape(newsource
, source
)) {
1134 /* Yes, here we go; it's a wildcard. Bah. */
1135 char *dupsource
, *lastpart
, *dirpart
, *wildcard
;
1139 dupsource
= dupstr(source
);
1140 lastpart
= stripslashes(dupsource
, 0);
1141 wildcard
= dupstr(lastpart
);
1143 if (*dupsource
&& dupsource
[1]) {
1145 * The remains of dupsource are at least two
1146 * characters long, meaning the pathname wasn't
1147 * empty or just `/'. Hence, we remove the trailing
1150 lastpart
[-1] = '\0';
1151 } else if (!*dupsource
) {
1153 * The remains of dupsource are _empty_ - the whole
1154 * pathname was a wildcard. Hence we need to
1155 * replace it with ".".
1158 dupsource
= dupstr(".");
1162 * Now we have separated our string into dupsource (the
1163 * directory part) and wildcard. Both of these will
1164 * need freeing at some point. Next step is to remove
1165 * wildcard escapes from the directory part, throwing
1166 * an error if it contains a real wildcard.
1168 dirpart
= snewn(1+strlen(dupsource
), char);
1169 if (!wc_unescape(dirpart
, dupsource
)) {
1170 tell_user(stderr
, "%s: multiple-level wildcards unsupported",
1180 * Now we have dirpart (unescaped, ie a valid remote
1181 * path), and wildcard (a wildcard). This will be
1182 * sufficient to arrange a dirstack entry.
1184 scp_sftp_remotepath
= dirpart
;
1185 scp_sftp_wildcard
= wildcard
;
1188 scp_sftp_remotepath
= newsource
;
1189 scp_sftp_wildcard
= NULL
;
1191 scp_sftp_preserve
= preserve
;
1192 scp_sftp_recursive
= recursive
;
1193 scp_sftp_donethistarget
= 0;
1194 scp_sftp_dirstack_head
= NULL
;
1199 int scp_sink_init(void)
1202 back
->send(backhandle
, "", 1);
1207 #define SCP_SINK_FILE 1
1208 #define SCP_SINK_DIR 2
1209 #define SCP_SINK_ENDDIR 3
1210 #define SCP_SINK_RETRY 4 /* not an action; just try again */
1211 struct scp_sink_action
{
1212 int action
; /* FILE, DIR, ENDDIR */
1213 char *buf
; /* will need freeing after use */
1214 char *name
; /* filename or dirname (not ENDDIR) */
1215 long permissions
; /* access permissions (not ENDDIR) */
1216 uint64 size
; /* file size (not ENDDIR) */
1217 int settime
; /* 1 if atime and mtime are filled */
1218 unsigned long atime
, mtime
; /* access times for the file */
1221 int scp_get_sink_action(struct scp_sink_action
*act
)
1225 int must_free_fname
;
1226 struct fxp_attrs attrs
;
1227 struct sftp_packet
*pktin
;
1228 struct sftp_request
*req
;
1231 if (!scp_sftp_dirstack_head
) {
1232 if (!scp_sftp_donethistarget
) {
1234 * Simple case: we are only dealing with one file.
1236 fname
= scp_sftp_remotepath
;
1237 must_free_fname
= 0;
1238 scp_sftp_donethistarget
= 1;
1241 * Even simpler case: one file _which we've done_.
1242 * Return 1 (finished).
1248 * We're now in the middle of stepping through a list
1249 * of names returned from fxp_readdir(); so let's carry
1252 struct scp_sftp_dirstack
*head
= scp_sftp_dirstack_head
;
1253 while (head
->namepos
< head
->namelen
&&
1254 (is_dots(head
->names
[head
->namepos
].filename
) ||
1256 !wc_match(head
->wildcard
,
1257 head
->names
[head
->namepos
].filename
))))
1258 head
->namepos
++; /* skip . and .. */
1259 if (head
->namepos
< head
->namelen
) {
1260 head
->matched_something
= 1;
1261 fname
= dupcat(head
->dirpath
, "/",
1262 head
->names
[head
->namepos
++].filename
,
1264 must_free_fname
= 1;
1267 * We've come to the end of the list; pop it off
1268 * the stack and return an ENDDIR action (or RETRY
1269 * if this was a wildcard match).
1271 if (head
->wildcard
) {
1272 act
->action
= SCP_SINK_RETRY
;
1273 if (!head
->matched_something
) {
1274 tell_user(stderr
, "pscp: wildcard '%s' matched "
1275 "no files", head
->wildcard
);
1278 sfree(head
->wildcard
);
1281 act
->action
= SCP_SINK_ENDDIR
;
1284 sfree(head
->dirpath
);
1286 scp_sftp_dirstack_head
= head
->next
;
1294 * Now we have a filename. Stat it, and see if it's a file
1297 req
= fxp_stat_send(fname
);
1298 pktin
= sftp_wait_for_reply(req
);
1299 ret
= fxp_stat_recv(pktin
, req
, &attrs
);
1301 if (!ret
|| !(attrs
.flags
& SSH_FILEXFER_ATTR_PERMISSIONS
)) {
1302 tell_user(stderr
, "unable to identify %s: %s", fname
,
1303 ret ?
"file type not supplied" : fxp_error());
1304 if (must_free_fname
) sfree(fname
);
1309 if (attrs
.permissions
& 0040000) {
1310 struct scp_sftp_dirstack
*newitem
;
1311 struct fxp_handle
*dirhandle
;
1312 int nnames
, namesize
;
1313 struct fxp_name
*ournames
;
1314 struct fxp_names
*names
;
1317 * It's a directory. If we're not in recursive mode,
1318 * this merits a complaint (which is fatal if the name
1319 * was specified directly, but not if it was matched by
1322 * We skip this complaint completely if
1323 * scp_sftp_wildcard is set, because that's an
1324 * indication that we're not actually supposed to
1325 * _recursively_ transfer the dir, just scan it for
1326 * things matching the wildcard.
1328 if (!scp_sftp_recursive
&& !scp_sftp_wildcard
) {
1329 tell_user(stderr
, "pscp: %s: is a directory", fname
);
1331 if (must_free_fname
) sfree(fname
);
1332 if (scp_sftp_dirstack_head
) {
1333 act
->action
= SCP_SINK_RETRY
;
1341 * Otherwise, the fun begins. We must fxp_opendir() the
1342 * directory, slurp the filenames into memory, return
1343 * SCP_SINK_DIR (unless this is a wildcard match), and
1344 * set targetisdir. The next time we're called, we will
1345 * run through the list of filenames one by one,
1346 * matching them against a wildcard if present.
1348 * If targetisdir is _already_ set (meaning we're
1349 * already in the middle of going through another such
1350 * list), we must push the other (target,namelist) pair
1353 req
= fxp_opendir_send(fname
);
1354 pktin
= sftp_wait_for_reply(req
);
1355 dirhandle
= fxp_opendir_recv(pktin
, req
);
1358 tell_user(stderr
, "pscp: unable to open directory %s: %s",
1359 fname
, fxp_error());
1360 if (must_free_fname
) sfree(fname
);
1364 nnames
= namesize
= 0;
1369 req
= fxp_readdir_send(dirhandle
);
1370 pktin
= sftp_wait_for_reply(req
);
1371 names
= fxp_readdir_recv(pktin
, req
);
1373 if (names
== NULL
) {
1374 if (fxp_error_type() == SSH_FX_EOF
)
1376 tell_user(stderr
, "pscp: reading directory %s: %s",
1377 fname
, fxp_error());
1379 req
= fxp_close_send(dirhandle
);
1380 pktin
= sftp_wait_for_reply(req
);
1381 fxp_close_recv(pktin
, req
);
1383 if (must_free_fname
) sfree(fname
);
1388 if (names
->nnames
== 0) {
1389 fxp_free_names(names
);
1392 if (nnames
+ names
->nnames
>= namesize
) {
1393 namesize
+= names
->nnames
+ 128;
1394 ournames
= sresize(ournames
, namesize
, struct fxp_name
);
1396 for (i
= 0; i
< names
->nnames
; i
++) {
1397 if (!strcmp(names
->names
[i
].filename
, ".") ||
1398 !strcmp(names
->names
[i
].filename
, "..")) {
1400 * . and .. are normal consequences of
1401 * reading a directory, and aren't worth
1402 * complaining about.
1404 } else if (!vet_filename(names
->names
[i
].filename
)) {
1405 tell_user(stderr
, "ignoring potentially dangerous server-"
1406 "supplied filename '%s'",
1407 names
->names
[i
].filename
);
1409 ournames
[nnames
++] = names
->names
[i
];
1411 names
->nnames
= 0; /* prevent free_names */
1412 fxp_free_names(names
);
1414 req
= fxp_close_send(dirhandle
);
1415 pktin
= sftp_wait_for_reply(req
);
1416 fxp_close_recv(pktin
, req
);
1418 newitem
= snew(struct scp_sftp_dirstack
);
1419 newitem
->next
= scp_sftp_dirstack_head
;
1420 newitem
->names
= ournames
;
1421 newitem
->namepos
= 0;
1422 newitem
->namelen
= nnames
;
1423 if (must_free_fname
)
1424 newitem
->dirpath
= fname
;
1426 newitem
->dirpath
= dupstr(fname
);
1427 if (scp_sftp_wildcard
) {
1428 newitem
->wildcard
= scp_sftp_wildcard
;
1429 newitem
->matched_something
= 0;
1430 scp_sftp_wildcard
= NULL
;
1432 newitem
->wildcard
= NULL
;
1434 scp_sftp_dirstack_head
= newitem
;
1436 if (newitem
->wildcard
) {
1437 act
->action
= SCP_SINK_RETRY
;
1439 act
->action
= SCP_SINK_DIR
;
1440 act
->buf
= dupstr(stripslashes(fname
, 0));
1441 act
->name
= act
->buf
;
1442 act
->size
= uint64_make(0,0); /* duhh, it's a directory */
1443 act
->permissions
= 07777 & attrs
.permissions
;
1444 if (scp_sftp_preserve
&&
1445 (attrs
.flags
& SSH_FILEXFER_ATTR_ACMODTIME
)) {
1446 act
->atime
= attrs
.atime
;
1447 act
->mtime
= attrs
.mtime
;
1456 * It's a file. Return SCP_SINK_FILE.
1458 act
->action
= SCP_SINK_FILE
;
1459 act
->buf
= dupstr(stripslashes(fname
, 0));
1460 act
->name
= act
->buf
;
1461 if (attrs
.flags
& SSH_FILEXFER_ATTR_SIZE
) {
1462 act
->size
= attrs
.size
;
1464 act
->size
= uint64_make(ULONG_MAX
,ULONG_MAX
); /* no idea */
1465 act
->permissions
= 07777 & attrs
.permissions
;
1466 if (scp_sftp_preserve
&&
1467 (attrs
.flags
& SSH_FILEXFER_ATTR_ACMODTIME
)) {
1468 act
->atime
= attrs
.atime
;
1469 act
->mtime
= attrs
.mtime
;
1473 if (must_free_fname
)
1474 scp_sftp_currentname
= fname
;
1476 scp_sftp_currentname
= dupstr(fname
);
1491 if (ssh_scp_recv((unsigned char *) &ch
, 1) <= 0)
1494 bump("Protocol error: Unexpected newline");
1498 if (ssh_scp_recv((unsigned char *) &ch
, 1) <= 0)
1499 bump("Lost connection");
1502 act
->buf
= sresize(act
->buf
, bufsize
, char);
1505 } while (ch
!= '\n');
1506 act
->buf
[i
- 1] = '\0';
1508 case '\01': /* error */
1509 tell_user(stderr
, "%s", act
->buf
);
1511 continue; /* go round again */
1512 case '\02': /* fatal error */
1513 bump("%s", act
->buf
);
1515 back
->send(backhandle
, "", 1);
1516 act
->action
= SCP_SINK_ENDDIR
;
1519 if (sscanf(act
->buf
, "%ld %*d %ld %*d",
1520 &act
->mtime
, &act
->atime
) == 2) {
1522 back
->send(backhandle
, "", 1);
1523 continue; /* go round again */
1525 bump("Protocol error: Illegal time format");
1528 act
->action
= (action
== 'C' ? SCP_SINK_FILE
: SCP_SINK_DIR
);
1531 bump("Protocol error: Expected control record");
1534 * We will go round this loop only once, unless we hit
1541 * If we get here, we must have seen SCP_SINK_FILE or
1547 if (sscanf(act
->buf
, "%lo %s %n", &act
->permissions
,
1549 bump("Protocol error: Illegal file descriptor format");
1550 act
->size
= uint64_from_decimal(sizestr
);
1551 act
->name
= act
->buf
+ i
;
1557 int scp_accept_filexfer(void)
1560 struct sftp_packet
*pktin
;
1561 struct sftp_request
*req
;
1563 req
= fxp_open_send(scp_sftp_currentname
, SSH_FXF_READ
, NULL
);
1564 pktin
= sftp_wait_for_reply(req
);
1565 scp_sftp_filehandle
= fxp_open_recv(pktin
, req
);
1567 if (!scp_sftp_filehandle
) {
1568 tell_user(stderr
, "pscp: unable to open %s: %s",
1569 scp_sftp_currentname
, fxp_error());
1573 scp_sftp_fileoffset
= uint64_make(0, 0);
1574 scp_sftp_xfer
= xfer_download_init(scp_sftp_filehandle
,
1575 scp_sftp_fileoffset
);
1576 sfree(scp_sftp_currentname
);
1579 back
->send(backhandle
, "", 1);
1580 return 0; /* can't fail */
1584 int scp_recv_filedata(char *data
, int len
)
1587 struct sftp_packet
*pktin
;
1591 xfer_download_queue(scp_sftp_xfer
);
1592 pktin
= sftp_recv();
1593 ret
= xfer_download_gotpkt(scp_sftp_xfer
, pktin
);
1595 tell_user(stderr
, "pscp: error while reading: %s", fxp_error());
1596 if (ret
== INT_MIN
) /* pktin not even freed */
1602 if (xfer_download_data(scp_sftp_xfer
, &vbuf
, &actuallen
)) {
1604 * This assertion relies on the fact that the natural
1605 * block size used in the xfer manager is at most that
1606 * used in this module. I don't like crossing layers in
1607 * this way, but it'll do for now.
1609 assert(actuallen
<= len
);
1610 memcpy(data
, vbuf
, actuallen
);
1615 scp_sftp_fileoffset
= uint64_add32(scp_sftp_fileoffset
, actuallen
);
1619 return ssh_scp_recv((unsigned char *) data
, len
);
1623 int scp_finish_filerecv(void)
1626 struct sftp_packet
*pktin
;
1627 struct sftp_request
*req
;
1630 * Ensure that xfer_done() will work correctly, so we can
1631 * clean up any outstanding requests from the file
1634 xfer_set_error(scp_sftp_xfer
);
1635 while (!xfer_done(scp_sftp_xfer
)) {
1639 pktin
= sftp_recv();
1640 ret
= xfer_download_gotpkt(scp_sftp_xfer
, pktin
);
1642 tell_user(stderr
, "pscp: error while reading: %s", fxp_error());
1643 if (ret
== INT_MIN
) /* pktin not even freed */
1648 if (xfer_download_data(scp_sftp_xfer
, &vbuf
, &len
))
1651 xfer_cleanup(scp_sftp_xfer
);
1653 req
= fxp_close_send(scp_sftp_filehandle
);
1654 pktin
= sftp_wait_for_reply(req
);
1655 fxp_close_recv(pktin
, req
);
1658 back
->send(backhandle
, "", 1);
1663 /* ----------------------------------------------------------------------
1664 * Send an error message to the other side and to the screen.
1665 * Increment error counter.
1667 static void run_err(const char *fmt
, ...)
1673 str
= dupvprintf(fmt
, ap
);
1674 str2
= dupcat("pscp: ", str
, "\n", NULL
);
1676 scp_send_errmsg(str2
);
1677 tell_user(stderr
, "%s", str2
);
1683 * Execute the source part of the SCP protocol.
1685 static void source(char *src
)
1688 unsigned long mtime
, atime
;
1695 time_t stat_starttime
, stat_lasttime
;
1697 attr
= file_type(src
);
1698 if (attr
== FILE_TYPE_NONEXISTENT
||
1699 attr
== FILE_TYPE_WEIRD
) {
1700 run_err("%s: %s file or directory", src
,
1701 (attr
== FILE_TYPE_WEIRD ?
"Not a" : "No such"));
1705 if (attr
== FILE_TYPE_DIRECTORY
) {
1708 * Avoid . and .. directories.
1711 p
= strrchr(src
, '/');
1713 p
= strrchr(src
, '\\');
1718 if (!strcmp(p
, ".") || !strcmp(p
, ".."))
1719 /* skip . and .. */ ;
1723 run_err("%s: not a regular file", src
);
1728 if ((last
= strrchr(src
, '/')) == NULL
)
1732 if (strrchr(last
, '\\') != NULL
)
1733 last
= strrchr(last
, '\\') + 1;
1734 if (last
== src
&& strchr(src
, ':') != NULL
)
1735 last
= strchr(src
, ':') + 1;
1737 f
= open_existing_file(src
, &size
, &mtime
, &atime
, &permissions
);
1739 run_err("%s: Cannot open file", src
);
1743 if (scp_send_filetimes(mtime
, atime
)) {
1751 uint64_decimal(size
, sizestr
);
1752 tell_user(stderr
, "Sending file %s, size=%s", last
, sizestr
);
1754 if (scp_send_filename(last
, size
, permissions
)) {
1759 stat_bytes
= uint64_make(0,0);
1760 stat_starttime
= time(NULL
);
1763 for (i
= uint64_make(0,0);
1764 uint64_compare(i
,size
) < 0;
1765 i
= uint64_add32(i
,4096)) {
1766 char transbuf
[4096];
1769 if (uint64_compare(uint64_add32(i
, k
),size
) > 0) /* i + k > size */
1770 k
= (uint64_subtract(size
, i
)).lo
; /* k = size - i; */
1771 if ((j
= read_from_file(f
, transbuf
, k
)) != k
) {
1774 bump("%s: Read error", src
);
1776 if (scp_send_filedata(transbuf
, k
))
1777 bump("%s: Network error occurred", src
);
1780 stat_bytes
= uint64_add32(stat_bytes
, k
);
1781 if (time(NULL
) != stat_lasttime
||
1782 (uint64_compare(uint64_add32(i
, k
), size
) == 0)) {
1783 stat_lasttime
= time(NULL
);
1784 print_stats(last
, size
, stat_bytes
,
1785 stat_starttime
, stat_lasttime
);
1792 (void) scp_send_finish();
1796 * Recursively send the contents of a directory.
1798 static void rsource(char *src
)
1804 if ((last
= strrchr(src
, '/')) == NULL
)
1808 if (strrchr(last
, '\\') != NULL
)
1809 last
= strrchr(last
, '\\') + 1;
1810 if (last
== src
&& strchr(src
, ':') != NULL
)
1811 last
= strchr(src
, ':') + 1;
1813 /* maybe send filetime */
1815 save_target
= scp_save_remotepath();
1818 tell_user(stderr
, "Entering directory: %s", last
);
1819 if (scp_send_dirname(last
, 0755))
1822 dir
= open_directory(src
);
1825 while ((filename
= read_filename(dir
)) != NULL
) {
1826 char *foundfile
= dupcat(src
, "/", filename
, NULL
);
1832 close_directory(dir
);
1834 (void) scp_send_enddir();
1836 scp_restore_remotepath(save_target
);
1840 * Execute the sink part of the SCP protocol.
1842 static void sink(char *targ
, char *src
)
1852 time_t stat_starttime
, stat_lasttime
;
1855 attr
= file_type(targ
);
1856 if (attr
== FILE_TYPE_DIRECTORY
)
1859 if (targetshouldbedirectory
&& !targisdir
)
1860 bump("%s: Not a directory", targ
);
1864 struct scp_sink_action act
;
1865 if (scp_get_sink_action(&act
))
1868 if (act
.action
== SCP_SINK_ENDDIR
)
1871 if (act
.action
== SCP_SINK_RETRY
)
1876 * Prevent the remote side from maliciously writing to
1877 * files outside the target area by sending a filename
1878 * containing `../'. In fact, it shouldn't be sending
1879 * filenames with any slashes or colons in at all; so
1880 * we'll find the last slash, backslash or colon in the
1881 * filename and use only the part after that. (And
1884 * In addition, we also ensure here that if we're
1885 * copying a single file and the target is a directory
1886 * (common usage: `pscp host:filename .') the remote
1887 * can't send us a _different_ file name. We can
1888 * distinguish this case because `src' will be non-NULL
1889 * and the last component of that will fail to match
1890 * (the last component of) the name sent.
1892 * Well, not always; if `src' is a wildcard, we do
1893 * expect to get back filenames that don't correspond
1894 * exactly to it. Ideally in this case, we would like
1895 * to ensure that the returned filename actually
1896 * matches the wildcard pattern - but one of SCP's
1897 * protocol infelicities is that wildcard matching is
1898 * done at the server end _by the server's rules_ and
1899 * so in general this is infeasible. Hence, we only
1900 * accept filenames that don't correspond to `src' if
1901 * unsafe mode is enabled or we are using SFTP (which
1902 * resolves remote wildcards on the client side and can
1905 char *striptarget
, *stripsrc
;
1907 striptarget
= stripslashes(act
.name
, 1);
1908 if (striptarget
!= act
.name
) {
1909 tell_user(stderr
, "warning: remote host sent a compound"
1910 " pathname '%s'", act
.name
);
1911 tell_user(stderr
, " renaming local file to '%s'",
1916 * Also check to see if the target filename is '.' or
1917 * '..', or indeed '...' and so on because Windows
1918 * appears to interpret those like '..'.
1920 if (is_dots(striptarget
)) {
1921 bump("security violation: remote host attempted to write to"
1922 " a '.' or '..' path!");
1926 stripsrc
= stripslashes(src
, 1);
1927 if (strcmp(striptarget
, stripsrc
) &&
1928 !using_sftp
&& !scp_unsafe_mode
) {
1929 tell_user(stderr
, "warning: remote host tried to write "
1930 "to a file called '%s'", striptarget
);
1931 tell_user(stderr
, " when we requested a file "
1932 "called '%s'.", stripsrc
);
1933 tell_user(stderr
, " If this is a wildcard, "
1934 "consider upgrading to SSH-2 or using");
1935 tell_user(stderr
, " the '-unsafe' option. Renaming"
1936 " of this file has been disallowed.");
1937 /* Override the name the server provided with our own. */
1938 striptarget
= stripsrc
;
1942 if (targ
[0] != '\0')
1943 destfname
= dir_file_cat(targ
, striptarget
);
1945 destfname
= dupstr(striptarget
);
1948 * In this branch of the if, the target area is a
1949 * single file with an explicitly specified name in any
1950 * case, so there's no danger.
1952 destfname
= dupstr(targ
);
1954 attr
= file_type(destfname
);
1955 exists
= (attr
!= FILE_TYPE_NONEXISTENT
);
1957 if (act
.action
== SCP_SINK_DIR
) {
1958 if (exists
&& attr
!= FILE_TYPE_DIRECTORY
) {
1959 run_err("%s: Not a directory", destfname
);
1964 if (!create_directory(destfname
)) {
1965 run_err("%s: Cannot create directory", destfname
);
1970 sink(destfname
, NULL
);
1971 /* can we set the timestamp for directories ? */
1976 f
= open_new_file(destfname
, act
.permissions
);
1978 run_err("%s: Cannot create file", destfname
);
1983 if (scp_accept_filexfer()) {
1989 stat_bytes
= uint64_make(0, 0);
1990 stat_starttime
= time(NULL
);
1992 stat_name
= stripslashes(destfname
, 1);
1994 received
= uint64_make(0, 0);
1995 while (uint64_compare(received
,act
.size
) < 0) {
1996 char transbuf
[32768];
1999 blksize
= uint64_make(0, 32768);
2000 if (uint64_compare(blksize
,uint64_subtract(act
.size
,received
)) > 0)
2001 blksize
= uint64_subtract(act
.size
,received
);
2002 read
= scp_recv_filedata(transbuf
, (int)blksize
.lo
);
2004 bump("Lost connection");
2007 if (write_to_file(f
, transbuf
, read
) != (int)read
) {
2009 /* FIXME: in sftp we can actually abort the transfer */
2011 printf("\r%-25.25s | %50s\n",
2013 "Write error.. waiting for end of file");
2017 stat_bytes
= uint64_add32(stat_bytes
,read
);
2018 if (time(NULL
) > stat_lasttime
||
2019 uint64_compare(uint64_add32(received
, read
), act
.size
) == 0) {
2020 stat_lasttime
= time(NULL
);
2021 print_stats(stat_name
, act
.size
, stat_bytes
,
2022 stat_starttime
, stat_lasttime
);
2025 received
= uint64_add32(received
, read
);
2028 set_file_times(f
, act
.mtime
, act
.atime
);
2033 run_err("%s: Write error", destfname
);
2037 (void) scp_finish_filerecv();
2044 * We will copy local files to a remote server.
2046 static void toremote(int argc
, char *argv
[])
2048 char *src
, *targ
, *host
, *user
;
2054 targ
= argv
[argc
- 1];
2056 /* Separate host from filename */
2060 bump("targ == NULL in toremote()");
2064 /* Substitute "." for empty target */
2066 /* Separate host and username */
2068 host
= strrchr(host
, '@');
2079 if (colon(argv
[0]) != NULL
)
2080 bump("%s: Remote to remote not supported", argv
[0]);
2082 wc_type
= test_wildcard(argv
[0], 1);
2083 if (wc_type
== WCTYPE_NONEXISTENT
)
2084 bump("%s: No such file or directory\n", argv
[0]);
2085 else if (wc_type
== WCTYPE_WILDCARD
)
2086 targetshouldbedirectory
= 1;
2089 cmd
= dupprintf("scp%s%s%s%s -t %s",
2090 verbose ?
" -v" : "",
2091 recursive ?
" -r" : "",
2092 preserve ?
" -p" : "",
2093 targetshouldbedirectory ?
" -d" : "", targ
);
2094 do_cmd(host
, user
, cmd
);
2097 if (scp_source_setup(targ
, targetshouldbedirectory
))
2100 for (i
= 0; i
< argc
- 1; i
++) {
2102 if (colon(src
) != NULL
) {
2103 tell_user(stderr
, "%s: Remote to remote not supported\n", src
);
2108 wc_type
= test_wildcard(src
, 1);
2109 if (wc_type
== WCTYPE_NONEXISTENT
) {
2110 run_err("%s: No such file or directory", src
);
2112 } else if (wc_type
== WCTYPE_FILENAME
) {
2116 WildcardMatcher
*wc
;
2119 wc
= begin_wildcard_matching(src
);
2121 run_err("%s: No such file or directory", src
);
2125 while ((filename
= wildcard_get_filename(wc
)) != NULL
) {
2130 finish_wildcard_matching(wc
);
2136 * We will copy files from a remote server to the local machine.
2138 static void tolocal(int argc
, char *argv
[])
2140 char *src
, *targ
, *host
, *user
;
2146 bump("More than one remote source not supported");
2151 /* Separate host from filename */
2155 bump("Local to local copy not supported");
2159 /* Substitute "." for empty filename */
2161 /* Separate username and hostname */
2163 host
= strrchr(host
, '@');
2173 cmd
= dupprintf("scp%s%s%s%s -f %s",
2174 verbose ?
" -v" : "",
2175 recursive ?
" -r" : "",
2176 preserve ?
" -p" : "",
2177 targetshouldbedirectory ?
" -d" : "", src
);
2178 do_cmd(host
, user
, cmd
);
2181 if (scp_sink_setup(src
, preserve
, recursive
))
2188 * We will issue a list command to get a remote directory.
2190 static void get_dir_list(int argc
, char *argv
[])
2192 char *src
, *host
, *user
;
2198 /* Separate host from filename */
2202 bump("Local file listing not supported");
2206 /* Substitute "." for empty filename */
2208 /* Separate username and hostname */
2210 host
= strrchr(host
, '@');
2220 cmd
= snewn(4 * strlen(src
) + 100, char);
2221 strcpy(cmd
, "ls -la '");
2222 p
= cmd
+ strlen(cmd
);
2223 for (q
= src
; *q
; q
++) {
2236 do_cmd(host
, user
, cmd
);
2240 scp_sftp_listdir(src
);
2242 while (ssh_scp_recv((unsigned char *) &c
, 1) > 0)
2243 tell_char(stdout
, c
);
2248 * Short description of parameters.
2250 static void usage(void)
2252 printf("PuTTY Secure Copy client\n");
2253 printf("%s\n", ver
);
2254 printf("Usage: pscp [options] [user@]host:source target\n");
2256 (" pscp [options] source [source...] [user@]host:target\n");
2257 printf(" pscp [options] -ls [user@]host:filespec\n");
2258 printf("Options:\n");
2259 printf(" -V print version information and exit\n");
2260 printf(" -pgpfp print PGP key fingerprints and exit\n");
2261 printf(" -p preserve file attributes\n");
2262 printf(" -q quiet, don't show statistics\n");
2263 printf(" -r copy directories recursively\n");
2264 printf(" -v show verbose messages\n");
2265 printf(" -load sessname Load settings from saved session\n");
2266 printf(" -P port connect to specified port\n");
2267 printf(" -l user connect with specified username\n");
2268 printf(" -pw passw login with specified password\n");
2269 printf(" -1 -2 force use of particular SSH protocol version\n");
2270 printf(" -4 -6 force use of IPv4 or IPv6\n");
2271 printf(" -C enable compression\n");
2272 printf(" -i key private key file for authentication\n");
2273 printf(" -noagent disable use of Pageant\n");
2274 printf(" -agent enable use of Pageant\n");
2275 printf(" -batch disable all interactive prompts\n");
2276 printf(" -unsafe allow server-side wildcards (DANGEROUS)\n");
2277 printf(" -sftp force use of SFTP protocol\n");
2278 printf(" -scp force use of SCP protocol\n");
2281 * -gui is an internal option, used by GUI front ends to get
2282 * pscp to pass progress reports back to them. It's not an
2283 * ordinary user-accessible option, so it shouldn't be part of
2284 * the command-line help. The only people who need to know
2285 * about it are programmers, and they can read the source.
2288 (" -gui hWnd GUI mode with the windows handle for receiving messages\n");
2295 printf("pscp: %s\n", ver
);
2299 void cmdline_error(char *p
, ...)
2302 fprintf(stderr
, "pscp: ");
2304 vfprintf(stderr
, p
, ap
);
2306 fprintf(stderr
, "\n try typing just \"pscp\" for help\n");
2311 * Main program. (Called `psftp_main' because it gets called from
2312 * *sftp.c; bit silly, I know, but it had to be called _something_.)
2314 int psftp_main(int argc
, char *argv
[])
2318 default_protocol
= PROT_TELNET
;
2321 #ifdef FLAG_SYNCAGENT
2325 cmdline_tooltype
= TOOLTYPE_FILETRANSFER
;
2328 /* Load Default Settings before doing anything else. */
2330 do_defaults(NULL
, conf
);
2331 loaded_session
= FALSE
;
2333 for (i
= 1; i
< argc
; i
++) {
2335 if (argv
[i
][0] != '-')
2337 ret
= cmdline_process_param(argv
[i
], i
+1<argc?argv
[i
+1]:NULL
, 1, conf
);
2339 cmdline_error("option \"%s\" requires an argument", argv
[i
]);
2340 } else if (ret
== 2) {
2341 i
++; /* skip next argument */
2342 } else if (ret
== 1) {
2343 /* We have our own verbosity in addition to `flags'. */
2344 if (flags
& FLAG_VERBOSE
)
2346 } else if (strcmp(argv
[i
], "-pgpfp") == 0) {
2349 } else if (strcmp(argv
[i
], "-r") == 0) {
2351 } else if (strcmp(argv
[i
], "-p") == 0) {
2353 } else if (strcmp(argv
[i
], "-q") == 0) {
2355 } else if (strcmp(argv
[i
], "-h") == 0 ||
2356 strcmp(argv
[i
], "-?") == 0 ||
2357 strcmp(argv
[i
], "--help") == 0) {
2359 } else if (strcmp(argv
[i
], "-V") == 0 ||
2360 strcmp(argv
[i
], "--version") == 0) {
2362 } else if (strcmp(argv
[i
], "-ls") == 0) {
2364 } else if (strcmp(argv
[i
], "-batch") == 0) {
2365 console_batch_mode
= 1;
2366 } else if (strcmp(argv
[i
], "-unsafe") == 0) {
2367 scp_unsafe_mode
= 1;
2368 } else if (strcmp(argv
[i
], "-sftp") == 0) {
2369 try_scp
= 0; try_sftp
= 1;
2370 } else if (strcmp(argv
[i
], "-scp") == 0) {
2371 try_scp
= 1; try_sftp
= 0;
2372 } else if (strcmp(argv
[i
], "--") == 0) {
2376 cmdline_error("unknown option \"%s\"", argv
[i
]);
2386 get_dir_list(argc
, argv
);
2393 targetshouldbedirectory
= 1;
2395 if (colon(argv
[argc
- 1]) != NULL
)
2396 toremote(argc
, argv
);
2398 tolocal(argc
, argv
);
2401 if (back
!= NULL
&& back
->connected(backhandle
)) {
2403 back
->special(backhandle
, TS_EOF
);
2405 ssh_scp_recv((unsigned char *) &ch
, 1);
2410 console_provide_logctx(NULL
);
2411 back
->free(backhandle
);
2415 return (errs
== 0 ?
0 : 1);