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
30 static int verbose
= 0;
31 static int recursive
= 0;
32 static int preserve
= 0;
33 static int targetshouldbedirectory
= 0;
34 static int statistics
= 1;
35 static int prev_stats_len
= 0;
36 static int scp_unsafe_mode
= 0;
38 static int gui_mode
= 0;
39 static int using_sftp
= 0;
42 static void *backhandle
;
45 static void source(char *src
);
46 static void rsource(char *src
);
47 static void sink(char *targ
, char *src
);
50 * The maximum amount of queued data we accept before we stop and
51 * wait for the server to process some.
53 #define MAX_SCP_BUFSIZE 16384
55 void ldisc_send(void *handle
, char *buf
, int len
, int interactive
)
58 * This is only here because of the calls to ldisc_send(NULL,
59 * 0) in ssh.c. Nothing in PSCP actually needs to use the ldisc
60 * as an ldisc. So if we get called with any real data, I want
66 static void tell_char(FILE * stream
, char c
)
71 gui_send_char(stream
== stderr
, c
);
74 static void tell_str(FILE * stream
, char *str
)
78 for (i
= 0; i
< strlen(str
); ++i
)
79 tell_char(stream
, str
[i
]);
82 static void tell_user(FILE * stream
, char *fmt
, ...)
87 str
= dupvprintf(fmt
, ap
);
89 str2
= dupcat(str
, "\n", NULL
);
91 tell_str(stream
, str2
);
96 * Print an error message and perform a fatal exit.
98 void fatalbox(char *fmt
, ...)
103 str
= dupvprintf(fmt
, ap
);
104 str2
= dupcat("Fatal: ", str
, "\n", NULL
);
107 tell_str(stderr
, str2
);
112 gui_send_errcount(list
, errs
);
116 void modalfatalbox(char *fmt
, ...)
121 str
= dupvprintf(fmt
, ap
);
122 str2
= dupcat("Fatal: ", str
, "\n", NULL
);
125 tell_str(stderr
, str2
);
130 gui_send_errcount(list
, errs
);
134 void connection_fatal(void *frontend
, char *fmt
, ...)
139 str
= dupvprintf(fmt
, ap
);
140 str2
= dupcat("Fatal: ", str
, "\n", NULL
);
143 tell_str(stderr
, str2
);
148 gui_send_errcount(list
, errs
);
154 * In pscp, all agent requests should be synchronous, so this is a
157 void agent_schedule_callback(void (*callback
)(void *, void *, int),
158 void *callback_ctx
, void *data
, int len
)
160 assert(!"We shouldn't be here");
164 * Receive a block of data from the SSH link. Block until all data
167 * To do this, we repeatedly call the SSH protocol module, with our
168 * own trap in from_backend() to catch the data that comes back. We
169 * do this until we have enough data.
172 static unsigned char *outptr
; /* where to put the data */
173 static unsigned outlen
; /* how much data required */
174 static unsigned char *pending
= NULL
; /* any spare data */
175 static unsigned pendlen
= 0, pendsize
= 0; /* length and phys. size of buffer */
176 int from_backend(void *frontend
, int is_stderr
, const char *data
, int datalen
)
178 unsigned char *p
= (unsigned char *) data
;
179 unsigned len
= (unsigned) datalen
;
182 * stderr data is just spouted to local stderr and otherwise
187 fwrite(data
, 1, len
, stderr
);
192 * If this is before the real session begins, just return.
197 if ((outlen
> 0) && (len
> 0)) {
198 unsigned used
= outlen
;
201 memcpy(outptr
, p
, used
);
209 if (pendsize
< pendlen
+ len
) {
210 pendsize
= pendlen
+ len
+ 4096;
211 pending
= sresize(pending
, pendsize
, unsigned char);
213 fatalbox("Out of memory");
215 memcpy(pending
+ pendlen
, p
, len
);
221 static int ssh_scp_recv(unsigned char *buf
, int len
)
227 * See if the pending-input block contains some of what we
231 unsigned pendused
= pendlen
;
232 if (pendused
> outlen
)
234 memcpy(outptr
, pending
, pendused
);
235 memmove(pending
, pending
+ pendused
, pendlen
- pendused
);
249 if (ssh_sftp_loop_iteration() < 0)
257 * Loop through the ssh connection and authentication process.
259 static void ssh_scp_init(void)
261 while (!back
->sendok(backhandle
)) {
262 if (ssh_sftp_loop_iteration() < 0)
265 using_sftp
= !ssh_fallback_cmd(backhandle
);
268 tell_user(stderr
, "Using SFTP");
270 tell_user(stderr
, "Using SCP1");
275 * Print an error message and exit after closing the SSH link.
277 static void bump(char *fmt
, ...)
282 str
= dupvprintf(fmt
, ap
);
284 str2
= dupcat(str
, "\n", NULL
);
286 tell_str(stderr
, str2
);
290 if (back
!= NULL
&& back
->socket(backhandle
) != NULL
) {
292 back
->special(backhandle
, TS_EOF
);
293 ssh_scp_recv(&ch
, 1);
297 gui_send_errcount(list
, errs
);
303 * Open an SSH connection to user@host and execute cmd.
305 static void do_cmd(char *host
, char *user
, char *cmd
)
311 if (host
== NULL
|| host
[0] == '\0')
312 bump("Empty host name");
314 /* Try to load settings for this host */
315 do_defaults(host
, &cfg
);
316 if (cfg
.host
[0] == '\0') {
317 /* No settings for this host; use defaults */
318 do_defaults(NULL
, &cfg
);
319 strncpy(cfg
.host
, host
, sizeof(cfg
.host
) - 1);
320 cfg
.host
[sizeof(cfg
.host
) - 1] = '\0';
324 * Force use of SSH. (If they got the protocol wrong we assume the
325 * port is useless too.)
327 if (cfg
.protocol
!= PROT_SSH
) {
328 cfg
.protocol
= PROT_SSH
;
333 * Enact command-line overrides.
335 cmdline_run_saved(&cfg
);
338 * Trim leading whitespace off the hostname if it's there.
341 int space
= strspn(cfg
.host
, " \t");
342 memmove(cfg
.host
, cfg
.host
+space
, 1+strlen(cfg
.host
)-space
);
345 /* See if host is of the form user@host */
346 if (cfg
.host
[0] != '\0') {
347 char *atsign
= strchr(cfg
.host
, '@');
348 /* Make sure we're not overflowing the user field */
350 if (atsign
- cfg
.host
< sizeof cfg
.username
) {
351 strncpy(cfg
.username
, cfg
.host
, atsign
- cfg
.host
);
352 cfg
.username
[atsign
- cfg
.host
] = '\0';
354 memmove(cfg
.host
, atsign
+ 1, 1 + strlen(atsign
+ 1));
359 * Trim a colon suffix off the hostname if it's there.
361 cfg
.host
[strcspn(cfg
.host
, ":")] = '\0';
364 * Remove any remaining whitespace from the hostname.
368 while (cfg
.host
[p2
] != '\0') {
369 if (cfg
.host
[p2
] != ' ' && cfg
.host
[p2
] != '\t') {
370 cfg
.host
[p1
] = cfg
.host
[p2
];
379 if (user
!= NULL
&& user
[0] != '\0') {
380 strncpy(cfg
.username
, user
, sizeof(cfg
.username
) - 1);
381 cfg
.username
[sizeof(cfg
.username
) - 1] = '\0';
382 } else if (cfg
.username
[0] == '\0') {
383 user
= get_username();
385 bump("Empty user name");
388 tell_user(stderr
, "Guessing user name: %s", user
);
389 strncpy(cfg
.username
, user
, sizeof(cfg
.username
) - 1);
390 cfg
.username
[sizeof(cfg
.username
) - 1] = '\0';
396 * Disable scary things which shouldn't be enabled for simple
397 * things like SCP and SFTP: agent forwarding, port forwarding,
402 cfg
.portfwd
[0] = cfg
.portfwd
[1] = '\0';
405 * Attempt to start the SFTP subsystem as a first choice,
406 * falling back to the provided scp command if that fails.
408 strcpy(cfg
.remote_cmd
, "sftp");
409 cfg
.ssh_subsys
= TRUE
;
410 cfg
.remote_cmd_ptr2
= cmd
;
411 cfg
.ssh_subsys2
= FALSE
;
416 err
= back
->init(NULL
, &backhandle
, &cfg
, cfg
.host
, cfg
.port
, &realhost
,0);
418 bump("ssh_init: %s", err
);
419 logctx
= log_init(NULL
, &cfg
);
420 back
->provide_logctx(backhandle
, logctx
);
421 console_provide_logctx(logctx
);
423 if (verbose
&& realhost
!= NULL
)
424 tell_user(stderr
, "Connected to %s\n", realhost
);
429 * Update statistic information about current file.
431 static void print_stats(char *name
, unsigned long size
, unsigned long done
,
432 time_t start
, time_t now
)
441 elap
= (unsigned long) difftime(now
, start
);
444 ratebs
= (float) done
/ elap
;
446 ratebs
= (float) done
;
451 eta
= (unsigned long) ((size
- done
) / ratebs
);
452 sprintf(etastr
, "%02ld:%02ld:%02ld",
453 eta
/ 3600, (eta
% 3600) / 60, eta
% 60);
455 pct
= (int) (100 * (done
* 1.0 / size
));
458 gui_update_stats(name
, size
, pct
, elap
, done
, eta
,
459 (unsigned long) ratebs
);
461 len
= printf("\r%-25.25s | %10ld kB | %5.1f kB/s | ETA: %8s | %3d%%",
462 name
, done
/ 1024, ratebs
/ 1024.0, etastr
, pct
);
463 if (len
< prev_stats_len
)
464 printf("%*s", prev_stats_len
- len
, "");
465 prev_stats_len
= len
;
475 * Find a colon in str and return a pointer to the colon.
476 * This is used to separate hostname from filename.
478 static char *colon(char *str
)
480 /* We ignore a leading colon, since the hostname cannot be
481 empty. We also ignore a colon as second character because
482 of filenames like f:myfile.txt. */
483 if (str
[0] == '\0' || str
[0] == ':' || str
[1] == ':')
485 while (*str
!= '\0' && *str
!= ':' && *str
!= '/' && *str
!= '\\')
494 * Return a pointer to the portion of str that comes after the last
495 * slash (or backslash or colon, if `local' is TRUE).
497 static char *stripslashes(char *str
, int local
)
502 p
= strchr(str
, ':');
506 p
= strrchr(str
, '/');
510 p
= strrchr(str
, '\\');
518 * Determine whether a string is entirely composed of dots.
520 static int is_dots(char *str
)
522 return str
[strspn(str
, ".")] == '\0';
526 * Wait for a response from the other side.
527 * Return 0 if ok, -1 if error.
529 static int response(void)
531 char ch
, resp
, rbuf
[2048];
534 if (ssh_scp_recv(&resp
, 1) <= 0)
535 bump("Lost connection");
545 case 2: /* fatal error */
547 if (ssh_scp_recv(&ch
, 1) <= 0)
548 bump("Protocol error: Lost connection");
550 } while (p
< sizeof(rbuf
) && ch
!= '\n');
553 tell_user(stderr
, "%s\n", rbuf
);
561 int sftp_recvdata(char *buf
, int len
)
563 return ssh_scp_recv(buf
, len
);
565 int sftp_senddata(char *buf
, int len
)
567 back
->send(backhandle
, (unsigned char *) buf
, len
);
571 /* ----------------------------------------------------------------------
572 * sftp-based replacement for the hacky `pscp -ls'.
574 static int sftp_ls_compare(const void *av
, const void *bv
)
576 const struct fxp_name
*a
= (const struct fxp_name
*) av
;
577 const struct fxp_name
*b
= (const struct fxp_name
*) bv
;
578 return strcmp(a
->filename
, b
->filename
);
580 void scp_sftp_listdir(char *dirname
)
582 struct fxp_handle
*dirh
;
583 struct fxp_names
*names
;
584 struct fxp_name
*ournames
;
585 struct sftp_packet
*pktin
;
586 struct sftp_request
*req
, *rreq
;
587 int nnames
, namesize
;
591 tell_user(stderr
, "unable to initialise SFTP: %s", fxp_error());
596 printf("Listing directory %s\n", dirname
);
598 sftp_register(req
= fxp_opendir_send(dirname
));
599 rreq
= sftp_find_request(pktin
= sftp_recv());
601 dirh
= fxp_opendir_recv(pktin
, rreq
);
604 printf("Unable to open %s: %s\n", dirname
, fxp_error());
606 nnames
= namesize
= 0;
611 sftp_register(req
= fxp_readdir_send(dirh
));
612 rreq
= sftp_find_request(pktin
= sftp_recv());
614 names
= fxp_readdir_recv(pktin
, rreq
);
617 if (fxp_error_type() == SSH_FX_EOF
)
619 printf("Reading directory %s: %s\n", dirname
, fxp_error());
622 if (names
->nnames
== 0) {
623 fxp_free_names(names
);
627 if (nnames
+ names
->nnames
>= namesize
) {
628 namesize
+= names
->nnames
+ 128;
629 ournames
= sresize(ournames
, namesize
, struct fxp_name
);
632 for (i
= 0; i
< names
->nnames
; i
++)
633 ournames
[nnames
++] = names
->names
[i
];
635 names
->nnames
= 0; /* prevent free_names */
636 fxp_free_names(names
);
638 sftp_register(req
= fxp_close_send(dirh
));
639 rreq
= sftp_find_request(pktin
= sftp_recv());
641 fxp_close_recv(pktin
, rreq
);
644 * Now we have our filenames. Sort them by actual file
645 * name, and then output the longname parts.
647 qsort(ournames
, nnames
, sizeof(*ournames
), sftp_ls_compare
);
652 for (i
= 0; i
< nnames
; i
++)
653 printf("%s\n", ournames
[i
].longname
);
657 /* ----------------------------------------------------------------------
658 * Helper routines that contain the actual SCP protocol elements,
659 * implemented both as SCP1 and SFTP.
662 static struct scp_sftp_dirstack
{
663 struct scp_sftp_dirstack
*next
;
664 struct fxp_name
*names
;
665 int namepos
, namelen
;
668 int matched_something
; /* wildcard match set was non-empty */
669 } *scp_sftp_dirstack_head
;
670 static char *scp_sftp_remotepath
, *scp_sftp_currentname
;
671 static char *scp_sftp_wildcard
;
672 static int scp_sftp_targetisdir
, scp_sftp_donethistarget
;
673 static int scp_sftp_preserve
, scp_sftp_recursive
;
674 static unsigned long scp_sftp_mtime
, scp_sftp_atime
;
675 static int scp_has_times
;
676 static struct fxp_handle
*scp_sftp_filehandle
;
677 static struct fxp_xfer
*scp_sftp_xfer
;
678 static uint64 scp_sftp_fileoffset
;
680 void scp_source_setup(char *target
, int shouldbedir
)
684 * Find out whether the target filespec is in fact a
687 struct sftp_packet
*pktin
;
688 struct sftp_request
*req
, *rreq
;
689 struct fxp_attrs attrs
;
693 tell_user(stderr
, "unable to initialise SFTP: %s", fxp_error());
698 sftp_register(req
= fxp_stat_send(target
));
699 rreq
= sftp_find_request(pktin
= sftp_recv());
701 ret
= fxp_stat_recv(pktin
, rreq
, &attrs
);
703 if (!ret
|| !(attrs
.flags
& SSH_FILEXFER_ATTR_PERMISSIONS
))
704 scp_sftp_targetisdir
= 0;
706 scp_sftp_targetisdir
= (attrs
.permissions
& 0040000) != 0;
708 if (shouldbedir
&& !scp_sftp_targetisdir
) {
709 bump("pscp: remote filespec %s: not a directory\n", target
);
712 scp_sftp_remotepath
= dupstr(target
);
720 int scp_send_errmsg(char *str
)
723 /* do nothing; we never need to send our errors to the server */
725 back
->send(backhandle
, "\001", 1);/* scp protocol error prefix */
726 back
->send(backhandle
, str
, strlen(str
));
728 return 0; /* can't fail */
731 int scp_send_filetimes(unsigned long mtime
, unsigned long atime
)
734 scp_sftp_mtime
= mtime
;
735 scp_sftp_atime
= atime
;
740 sprintf(buf
, "T%lu 0 %lu 0\n", mtime
, atime
);
741 back
->send(backhandle
, buf
, strlen(buf
));
746 int scp_send_filename(char *name
, unsigned long size
, int modes
)
750 struct sftp_packet
*pktin
;
751 struct sftp_request
*req
, *rreq
;
753 if (scp_sftp_targetisdir
) {
754 fullname
= dupcat(scp_sftp_remotepath
, "/", name
, NULL
);
756 fullname
= dupstr(scp_sftp_remotepath
);
759 sftp_register(req
= fxp_open_send(fullname
, SSH_FXF_WRITE
|
760 SSH_FXF_CREAT
| SSH_FXF_TRUNC
));
761 rreq
= sftp_find_request(pktin
= sftp_recv());
763 scp_sftp_filehandle
= fxp_open_recv(pktin
, rreq
);
765 if (!scp_sftp_filehandle
) {
766 tell_user(stderr
, "pscp: unable to open %s: %s",
767 fullname
, fxp_error());
771 scp_sftp_fileoffset
= uint64_make(0, 0);
772 scp_sftp_xfer
= xfer_upload_init(scp_sftp_filehandle
,
773 scp_sftp_fileoffset
);
778 sprintf(buf
, "C%04o %lu ", modes
, size
);
779 back
->send(backhandle
, buf
, strlen(buf
));
780 back
->send(backhandle
, name
, strlen(name
));
781 back
->send(backhandle
, "\n", 1);
786 int scp_send_filedata(char *data
, int len
)
790 struct sftp_packet
*pktin
;
792 if (!scp_sftp_filehandle
) {
796 while (!xfer_upload_ready(scp_sftp_xfer
)) {
798 ret
= xfer_upload_gotpkt(scp_sftp_xfer
, pktin
);
800 tell_user(stderr
, "error while writing: %s\n", fxp_error());
806 xfer_upload_data(scp_sftp_xfer
, data
, len
);
808 scp_sftp_fileoffset
= uint64_add32(scp_sftp_fileoffset
, len
);
811 int bufsize
= back
->send(backhandle
, data
, len
);
814 * If the network transfer is backing up - that is, the
815 * remote site is not accepting data as fast as we can
816 * produce it - then we must loop on network events until
817 * we have space in the buffer again.
819 while (bufsize
> MAX_SCP_BUFSIZE
) {
820 if (ssh_sftp_loop_iteration() < 0)
822 bufsize
= back
->sendbuffer(backhandle
);
829 int scp_send_finish(void)
832 struct fxp_attrs attrs
;
833 struct sftp_packet
*pktin
;
834 struct sftp_request
*req
, *rreq
;
837 while (!xfer_done(scp_sftp_xfer
)) {
839 xfer_upload_gotpkt(scp_sftp_xfer
, pktin
);
841 xfer_cleanup(scp_sftp_xfer
);
843 if (!scp_sftp_filehandle
) {
847 attrs
.flags
= SSH_FILEXFER_ATTR_ACMODTIME
;
848 attrs
.atime
= scp_sftp_atime
;
849 attrs
.mtime
= scp_sftp_mtime
;
850 sftp_register(req
= fxp_fsetstat_send(scp_sftp_filehandle
, attrs
));
851 rreq
= sftp_find_request(pktin
= sftp_recv());
853 ret
= fxp_fsetstat_recv(pktin
, rreq
);
855 tell_user(stderr
, "unable to set file times: %s\n", fxp_error());
859 sftp_register(req
= fxp_close_send(scp_sftp_filehandle
));
860 rreq
= sftp_find_request(pktin
= sftp_recv());
862 fxp_close_recv(pktin
, rreq
);
866 back
->send(backhandle
, "", 1);
871 char *scp_save_remotepath(void)
874 return scp_sftp_remotepath
;
879 void scp_restore_remotepath(char *data
)
882 scp_sftp_remotepath
= data
;
885 int scp_send_dirname(char *name
, int modes
)
890 struct fxp_attrs attrs
;
891 struct sftp_packet
*pktin
;
892 struct sftp_request
*req
, *rreq
;
895 if (scp_sftp_targetisdir
) {
896 fullname
= dupcat(scp_sftp_remotepath
, "/", name
, NULL
);
898 fullname
= dupstr(scp_sftp_remotepath
);
902 * We don't worry about whether we managed to create the
903 * directory, because if it exists already it's OK just to
904 * use it. Instead, we will stat it afterwards, and if it
905 * exists and is a directory we will assume we were either
906 * successful or it didn't matter.
908 sftp_register(req
= fxp_mkdir_send(fullname
));
909 rreq
= sftp_find_request(pktin
= sftp_recv());
911 ret
= fxp_mkdir_recv(pktin
, rreq
);
916 err
= "server reported no error";
918 sftp_register(req
= fxp_stat_send(fullname
));
919 rreq
= sftp_find_request(pktin
= sftp_recv());
921 ret
= fxp_stat_recv(pktin
, rreq
, &attrs
);
923 if (!ret
|| !(attrs
.flags
& SSH_FILEXFER_ATTR_PERMISSIONS
) ||
924 !(attrs
.permissions
& 0040000)) {
925 tell_user(stderr
, "unable to create directory %s: %s",
931 scp_sftp_remotepath
= fullname
;
936 sprintf(buf
, "D%04o 0 ", modes
);
937 back
->send(backhandle
, buf
, strlen(buf
));
938 back
->send(backhandle
, name
, strlen(name
));
939 back
->send(backhandle
, "\n", 1);
944 int scp_send_enddir(void)
947 sfree(scp_sftp_remotepath
);
950 back
->send(backhandle
, "E\n", 2);
956 * Yes, I know; I have an scp_sink_setup _and_ an scp_sink_init.
957 * That's bad. The difference is that scp_sink_setup is called once
958 * right at the start, whereas scp_sink_init is called to
959 * initialise every level of recursion in the protocol.
961 int scp_sink_setup(char *source
, int preserve
, int recursive
)
967 tell_user(stderr
, "unable to initialise SFTP: %s", fxp_error());
972 * It's possible that the source string we've been given
973 * contains a wildcard. If so, we must split the directory
974 * away from the wildcard itself (throwing an error if any
975 * wildcardness comes before the final slash) and arrange
976 * things so that a dirstack entry will be set up.
978 newsource
= snewn(1+strlen(source
), char);
979 if (!wc_unescape(newsource
, source
)) {
980 /* Yes, here we go; it's a wildcard. Bah. */
981 char *dupsource
, *lastpart
, *dirpart
, *wildcard
;
982 dupsource
= dupstr(source
);
983 lastpart
= stripslashes(dupsource
, 0);
984 wildcard
= dupstr(lastpart
);
986 if (*dupsource
&& dupsource
[1]) {
988 * The remains of dupsource are at least two
989 * characters long, meaning the pathname wasn't
990 * empty or just `/'. Hence, we remove the trailing
994 } else if (!*dupsource
) {
996 * The remains of dupsource are _empty_ - the whole
997 * pathname was a wildcard. Hence we need to
998 * replace it with ".".
1001 dupsource
= dupstr(".");
1005 * Now we have separated our string into dupsource (the
1006 * directory part) and wildcard. Both of these will
1007 * need freeing at some point. Next step is to remove
1008 * wildcard escapes from the directory part, throwing
1009 * an error if it contains a real wildcard.
1011 dirpart
= snewn(1+strlen(dupsource
), char);
1012 if (!wc_unescape(dirpart
, dupsource
)) {
1013 tell_user(stderr
, "%s: multiple-level wildcards unsupported",
1023 * Now we have dirpart (unescaped, ie a valid remote
1024 * path), and wildcard (a wildcard). This will be
1025 * sufficient to arrange a dirstack entry.
1027 scp_sftp_remotepath
= dirpart
;
1028 scp_sftp_wildcard
= wildcard
;
1031 scp_sftp_remotepath
= newsource
;
1032 scp_sftp_wildcard
= NULL
;
1034 scp_sftp_preserve
= preserve
;
1035 scp_sftp_recursive
= recursive
;
1036 scp_sftp_donethistarget
= 0;
1037 scp_sftp_dirstack_head
= NULL
;
1042 int scp_sink_init(void)
1045 back
->send(backhandle
, "", 1);
1050 #define SCP_SINK_FILE 1
1051 #define SCP_SINK_DIR 2
1052 #define SCP_SINK_ENDDIR 3
1053 #define SCP_SINK_RETRY 4 /* not an action; just try again */
1054 struct scp_sink_action
{
1055 int action
; /* FILE, DIR, ENDDIR */
1056 char *buf
; /* will need freeing after use */
1057 char *name
; /* filename or dirname (not ENDDIR) */
1058 int mode
; /* access mode (not ENDDIR) */
1059 unsigned long size
; /* file size (not ENDDIR) */
1060 int settime
; /* 1 if atime and mtime are filled */
1061 unsigned long atime
, mtime
; /* access times for the file */
1064 int scp_get_sink_action(struct scp_sink_action
*act
)
1068 int must_free_fname
;
1069 struct fxp_attrs attrs
;
1070 struct sftp_packet
*pktin
;
1071 struct sftp_request
*req
, *rreq
;
1074 if (!scp_sftp_dirstack_head
) {
1075 if (!scp_sftp_donethistarget
) {
1077 * Simple case: we are only dealing with one file.
1079 fname
= scp_sftp_remotepath
;
1080 must_free_fname
= 0;
1081 scp_sftp_donethistarget
= 1;
1084 * Even simpler case: one file _which we've done_.
1085 * Return 1 (finished).
1091 * We're now in the middle of stepping through a list
1092 * of names returned from fxp_readdir(); so let's carry
1095 struct scp_sftp_dirstack
*head
= scp_sftp_dirstack_head
;
1096 while (head
->namepos
< head
->namelen
&&
1097 (is_dots(head
->names
[head
->namepos
].filename
) ||
1099 !wc_match(head
->wildcard
,
1100 head
->names
[head
->namepos
].filename
))))
1101 head
->namepos
++; /* skip . and .. */
1102 if (head
->namepos
< head
->namelen
) {
1103 head
->matched_something
= 1;
1104 fname
= dupcat(head
->dirpath
, "/",
1105 head
->names
[head
->namepos
++].filename
,
1107 must_free_fname
= 1;
1110 * We've come to the end of the list; pop it off
1111 * the stack and return an ENDDIR action (or RETRY
1112 * if this was a wildcard match).
1114 if (head
->wildcard
) {
1115 act
->action
= SCP_SINK_RETRY
;
1116 if (!head
->matched_something
) {
1117 tell_user(stderr
, "pscp: wildcard '%s' matched "
1118 "no files", head
->wildcard
);
1121 sfree(head
->wildcard
);
1124 act
->action
= SCP_SINK_ENDDIR
;
1127 sfree(head
->dirpath
);
1129 scp_sftp_dirstack_head
= head
->next
;
1137 * Now we have a filename. Stat it, and see if it's a file
1140 sftp_register(req
= fxp_stat_send(fname
));
1141 rreq
= sftp_find_request(pktin
= sftp_recv());
1142 assert(rreq
== req
);
1143 ret
= fxp_stat_recv(pktin
, rreq
, &attrs
);
1145 if (!ret
|| !(attrs
.flags
& SSH_FILEXFER_ATTR_PERMISSIONS
)) {
1146 tell_user(stderr
, "unable to identify %s: %s", fname
,
1147 ret ?
"file type not supplied" : fxp_error());
1152 if (attrs
.permissions
& 0040000) {
1153 struct scp_sftp_dirstack
*newitem
;
1154 struct fxp_handle
*dirhandle
;
1155 int nnames
, namesize
;
1156 struct fxp_name
*ournames
;
1157 struct fxp_names
*names
;
1160 * It's a directory. If we're not in recursive mode,
1161 * this merits a complaint (which is fatal if the name
1162 * was specified directly, but not if it was matched by
1165 * We skip this complaint completely if
1166 * scp_sftp_wildcard is set, because that's an
1167 * indication that we're not actually supposed to
1168 * _recursively_ transfer the dir, just scan it for
1169 * things matching the wildcard.
1171 if (!scp_sftp_recursive
&& !scp_sftp_wildcard
) {
1172 tell_user(stderr
, "pscp: %s: is a directory", fname
);
1174 if (must_free_fname
) sfree(fname
);
1175 if (scp_sftp_dirstack_head
) {
1176 act
->action
= SCP_SINK_RETRY
;
1184 * Otherwise, the fun begins. We must fxp_opendir() the
1185 * directory, slurp the filenames into memory, return
1186 * SCP_SINK_DIR (unless this is a wildcard match), and
1187 * set targetisdir. The next time we're called, we will
1188 * run through the list of filenames one by one,
1189 * matching them against a wildcard if present.
1191 * If targetisdir is _already_ set (meaning we're
1192 * already in the middle of going through another such
1193 * list), we must push the other (target,namelist) pair
1196 sftp_register(req
= fxp_opendir_send(fname
));
1197 rreq
= sftp_find_request(pktin
= sftp_recv());
1198 assert(rreq
== req
);
1199 dirhandle
= fxp_opendir_recv(pktin
, rreq
);
1202 tell_user(stderr
, "scp: unable to open directory %s: %s",
1203 fname
, fxp_error());
1204 if (must_free_fname
) sfree(fname
);
1208 nnames
= namesize
= 0;
1213 sftp_register(req
= fxp_readdir_send(dirhandle
));
1214 rreq
= sftp_find_request(pktin
= sftp_recv());
1215 assert(rreq
== req
);
1216 names
= fxp_readdir_recv(pktin
, rreq
);
1218 if (names
== NULL
) {
1219 if (fxp_error_type() == SSH_FX_EOF
)
1221 tell_user(stderr
, "scp: reading directory %s: %s\n",
1222 fname
, fxp_error());
1223 if (must_free_fname
) sfree(fname
);
1228 if (names
->nnames
== 0) {
1229 fxp_free_names(names
);
1232 if (nnames
+ names
->nnames
>= namesize
) {
1233 namesize
+= names
->nnames
+ 128;
1234 ournames
= sresize(ournames
, namesize
, struct fxp_name
);
1236 for (i
= 0; i
< names
->nnames
; i
++)
1237 ournames
[nnames
++] = names
->names
[i
];
1238 names
->nnames
= 0; /* prevent free_names */
1239 fxp_free_names(names
);
1241 sftp_register(req
= fxp_close_send(dirhandle
));
1242 rreq
= sftp_find_request(pktin
= sftp_recv());
1243 assert(rreq
== req
);
1244 fxp_close_recv(pktin
, rreq
);
1246 newitem
= snew(struct scp_sftp_dirstack
);
1247 newitem
->next
= scp_sftp_dirstack_head
;
1248 newitem
->names
= ournames
;
1249 newitem
->namepos
= 0;
1250 newitem
->namelen
= nnames
;
1251 if (must_free_fname
)
1252 newitem
->dirpath
= fname
;
1254 newitem
->dirpath
= dupstr(fname
);
1255 if (scp_sftp_wildcard
) {
1256 newitem
->wildcard
= scp_sftp_wildcard
;
1257 newitem
->matched_something
= 0;
1258 scp_sftp_wildcard
= NULL
;
1260 newitem
->wildcard
= NULL
;
1262 scp_sftp_dirstack_head
= newitem
;
1264 if (newitem
->wildcard
) {
1265 act
->action
= SCP_SINK_RETRY
;
1267 act
->action
= SCP_SINK_DIR
;
1268 act
->buf
= dupstr(stripslashes(fname
, 0));
1269 act
->name
= act
->buf
;
1270 act
->size
= 0; /* duhh, it's a directory */
1271 act
->mode
= 07777 & attrs
.permissions
;
1272 if (scp_sftp_preserve
&&
1273 (attrs
.flags
& SSH_FILEXFER_ATTR_ACMODTIME
)) {
1274 act
->atime
= attrs
.atime
;
1275 act
->mtime
= attrs
.mtime
;
1284 * It's a file. Return SCP_SINK_FILE.
1286 act
->action
= SCP_SINK_FILE
;
1287 act
->buf
= dupstr(stripslashes(fname
, 0));
1288 act
->name
= act
->buf
;
1289 if (attrs
.flags
& SSH_FILEXFER_ATTR_SIZE
) {
1290 if (uint64_compare(attrs
.size
,
1291 uint64_make(0, ULONG_MAX
)) > 0) {
1292 act
->size
= ULONG_MAX
; /* *boggle* */
1294 act
->size
= attrs
.size
.lo
;
1296 act
->size
= ULONG_MAX
; /* no idea */
1297 act
->mode
= 07777 & attrs
.permissions
;
1298 if (scp_sftp_preserve
&&
1299 (attrs
.flags
& SSH_FILEXFER_ATTR_ACMODTIME
)) {
1300 act
->atime
= attrs
.atime
;
1301 act
->mtime
= attrs
.mtime
;
1305 if (must_free_fname
)
1306 scp_sftp_currentname
= fname
;
1308 scp_sftp_currentname
= dupstr(fname
);
1323 if (ssh_scp_recv(&ch
, 1) <= 0)
1326 bump("Protocol error: Unexpected newline");
1330 if (ssh_scp_recv(&ch
, 1) <= 0)
1331 bump("Lost connection");
1334 act
->buf
= sresize(act
->buf
, bufsize
, char);
1337 } while (ch
!= '\n');
1338 act
->buf
[i
- 1] = '\0';
1340 case '\01': /* error */
1341 tell_user(stderr
, "%s\n", act
->buf
);
1343 continue; /* go round again */
1344 case '\02': /* fatal error */
1345 bump("%s", act
->buf
);
1347 back
->send(backhandle
, "", 1);
1348 act
->action
= SCP_SINK_ENDDIR
;
1351 if (sscanf(act
->buf
, "%ld %*d %ld %*d",
1352 &act
->mtime
, &act
->atime
) == 2) {
1354 back
->send(backhandle
, "", 1);
1355 continue; /* go round again */
1357 bump("Protocol error: Illegal time format");
1360 act
->action
= (action
== 'C' ? SCP_SINK_FILE
: SCP_SINK_DIR
);
1363 bump("Protocol error: Expected control record");
1366 * We will go round this loop only once, unless we hit
1373 * If we get here, we must have seen SCP_SINK_FILE or
1376 if (sscanf(act
->buf
, "%o %lu %n", &act
->mode
, &act
->size
, &i
) != 2)
1377 bump("Protocol error: Illegal file descriptor format");
1378 act
->name
= act
->buf
+ i
;
1383 int scp_accept_filexfer(void)
1386 struct sftp_packet
*pktin
;
1387 struct sftp_request
*req
, *rreq
;
1389 sftp_register(req
= fxp_open_send(scp_sftp_currentname
, SSH_FXF_READ
));
1390 rreq
= sftp_find_request(pktin
= sftp_recv());
1391 assert(rreq
== req
);
1392 scp_sftp_filehandle
= fxp_open_recv(pktin
, rreq
);
1394 if (!scp_sftp_filehandle
) {
1395 tell_user(stderr
, "pscp: unable to open %s: %s",
1396 scp_sftp_currentname
, fxp_error());
1400 scp_sftp_fileoffset
= uint64_make(0, 0);
1401 scp_sftp_xfer
= xfer_download_init(scp_sftp_filehandle
,
1402 scp_sftp_fileoffset
);
1403 sfree(scp_sftp_currentname
);
1406 back
->send(backhandle
, "", 1);
1407 return 0; /* can't fail */
1411 int scp_recv_filedata(char *data
, int len
)
1414 struct sftp_packet
*pktin
;
1418 xfer_download_queue(scp_sftp_xfer
);
1419 pktin
= sftp_recv();
1420 ret
= xfer_download_gotpkt(scp_sftp_xfer
, pktin
);
1423 tell_user(stderr
, "pscp: error while reading: %s", fxp_error());
1428 if (xfer_download_data(scp_sftp_xfer
, &vbuf
, &actuallen
)) {
1430 * This assertion relies on the fact that the natural
1431 * block size used in the xfer manager is at most that
1432 * used in this module. I don't like crossing layers in
1433 * this way, but it'll do for now.
1435 assert(actuallen
<= len
);
1436 memcpy(data
, vbuf
, actuallen
);
1441 scp_sftp_fileoffset
= uint64_add32(scp_sftp_fileoffset
, actuallen
);
1445 return ssh_scp_recv(data
, len
);
1449 int scp_finish_filerecv(void)
1452 struct sftp_packet
*pktin
;
1453 struct sftp_request
*req
, *rreq
;
1456 * Ensure that xfer_done() will work correctly, so we can
1457 * clean up any outstanding requests from the file
1460 xfer_set_error(scp_sftp_xfer
);
1461 while (!xfer_done(scp_sftp_xfer
)) {
1465 pktin
= sftp_recv();
1466 xfer_download_gotpkt(scp_sftp_xfer
, pktin
);
1467 if (xfer_download_data(scp_sftp_xfer
, &vbuf
, &len
))
1470 xfer_cleanup(scp_sftp_xfer
);
1472 sftp_register(req
= fxp_close_send(scp_sftp_filehandle
));
1473 rreq
= sftp_find_request(pktin
= sftp_recv());
1474 assert(rreq
== req
);
1475 fxp_close_recv(pktin
, rreq
);
1478 back
->send(backhandle
, "", 1);
1483 /* ----------------------------------------------------------------------
1484 * Send an error message to the other side and to the screen.
1485 * Increment error counter.
1487 static void run_err(const char *fmt
, ...)
1493 str
= dupvprintf(fmt
, ap
);
1494 str2
= dupcat("scp: ", str
, "\n", NULL
);
1496 scp_send_errmsg(str2
);
1497 tell_user(stderr
, "%s", str2
);
1503 * Execute the source part of the SCP protocol.
1505 static void source(char *src
)
1508 unsigned long mtime
, atime
;
1513 unsigned long stat_bytes
;
1514 time_t stat_starttime
, stat_lasttime
;
1516 attr
= file_type(src
);
1517 if (attr
== FILE_TYPE_NONEXISTENT
||
1518 attr
== FILE_TYPE_WEIRD
) {
1519 run_err("%s: %s file or directory", src
,
1520 (attr
== FILE_TYPE_WEIRD ?
"Not a" : "No such"));
1524 if (attr
== FILE_TYPE_DIRECTORY
) {
1527 * Avoid . and .. directories.
1530 p
= strrchr(src
, '/');
1532 p
= strrchr(src
, '\\');
1537 if (!strcmp(p
, ".") || !strcmp(p
, ".."))
1538 /* skip . and .. */ ;
1542 run_err("%s: not a regular file", src
);
1547 if ((last
= strrchr(src
, '/')) == NULL
)
1551 if (strrchr(last
, '\\') != NULL
)
1552 last
= strrchr(last
, '\\') + 1;
1553 if (last
== src
&& strchr(src
, ':') != NULL
)
1554 last
= strchr(src
, ':') + 1;
1556 f
= open_existing_file(src
, &size
, &mtime
, &atime
);
1558 run_err("%s: Cannot open file", src
);
1562 if (scp_send_filetimes(mtime
, atime
))
1567 tell_user(stderr
, "Sending file %s, size=%lu", last
, size
);
1568 if (scp_send_filename(last
, size
, 0644))
1572 stat_starttime
= time(NULL
);
1575 for (i
= 0; i
< size
; i
+= 4096) {
1576 char transbuf
[4096];
1581 if ((j
= read_from_file(f
, transbuf
, k
)) != k
) {
1584 bump("%s: Read error", src
);
1586 if (scp_send_filedata(transbuf
, k
))
1587 bump("%s: Network error occurred", src
);
1591 if (time(NULL
) != stat_lasttime
|| i
+ k
== size
) {
1592 stat_lasttime
= time(NULL
);
1593 print_stats(last
, size
, stat_bytes
,
1594 stat_starttime
, stat_lasttime
);
1601 (void) scp_send_finish();
1605 * Recursively send the contents of a directory.
1607 static void rsource(char *src
)
1613 if ((last
= strrchr(src
, '/')) == NULL
)
1617 if (strrchr(last
, '\\') != NULL
)
1618 last
= strrchr(last
, '\\') + 1;
1619 if (last
== src
&& strchr(src
, ':') != NULL
)
1620 last
= strchr(src
, ':') + 1;
1622 /* maybe send filetime */
1624 save_target
= scp_save_remotepath();
1627 tell_user(stderr
, "Entering directory: %s", last
);
1628 if (scp_send_dirname(last
, 0755))
1631 dir
= open_directory(src
);
1634 while ((filename
= read_filename(dir
)) != NULL
) {
1635 char *foundfile
= dupcat(src
, "/", filename
, NULL
);
1641 close_directory(dir
);
1643 (void) scp_send_enddir();
1645 scp_restore_remotepath(save_target
);
1649 * Execute the sink part of the SCP protocol.
1651 static void sink(char *targ
, char *src
)
1658 unsigned long received
;
1660 unsigned long stat_bytes
;
1661 time_t stat_starttime
, stat_lasttime
;
1664 attr
= file_type(targ
);
1665 if (attr
== FILE_TYPE_DIRECTORY
)
1668 if (targetshouldbedirectory
&& !targisdir
)
1669 bump("%s: Not a directory", targ
);
1673 struct scp_sink_action act
;
1674 if (scp_get_sink_action(&act
))
1677 if (act
.action
== SCP_SINK_ENDDIR
)
1680 if (act
.action
== SCP_SINK_RETRY
)
1685 * Prevent the remote side from maliciously writing to
1686 * files outside the target area by sending a filename
1687 * containing `../'. In fact, it shouldn't be sending
1688 * filenames with any slashes or colons in at all; so
1689 * we'll find the last slash, backslash or colon in the
1690 * filename and use only the part after that. (And
1693 * In addition, we also ensure here that if we're
1694 * copying a single file and the target is a directory
1695 * (common usage: `pscp host:filename .') the remote
1696 * can't send us a _different_ file name. We can
1697 * distinguish this case because `src' will be non-NULL
1698 * and the last component of that will fail to match
1699 * (the last component of) the name sent.
1701 * Well, not always; if `src' is a wildcard, we do
1702 * expect to get back filenames that don't correspond
1703 * exactly to it. Ideally in this case, we would like
1704 * to ensure that the returned filename actually
1705 * matches the wildcard pattern - but one of SCP's
1706 * protocol infelicities is that wildcard matching is
1707 * done at the server end _by the server's rules_ and
1708 * so in general this is infeasible. Hence, we only
1709 * accept filenames that don't correspond to `src' if
1710 * unsafe mode is enabled or we are using SFTP (which
1711 * resolves remote wildcards on the client side and can
1714 char *striptarget
, *stripsrc
;
1716 striptarget
= stripslashes(act
.name
, 1);
1717 if (striptarget
!= act
.name
) {
1718 tell_user(stderr
, "warning: remote host sent a compound"
1719 " pathname '%s'", act
.name
);
1720 tell_user(stderr
, " renaming local file to '%s'",
1725 * Also check to see if the target filename is '.' or
1726 * '..', or indeed '...' and so on because Windows
1727 * appears to interpret those like '..'.
1729 if (is_dots(striptarget
)) {
1730 bump("security violation: remote host attempted to write to"
1731 " a '.' or '..' path!");
1735 stripsrc
= stripslashes(src
, 1);
1736 if (strcmp(striptarget
, stripsrc
) &&
1737 !using_sftp
&& !scp_unsafe_mode
) {
1738 tell_user(stderr
, "warning: remote host tried to write "
1739 "to a file called '%s'", striptarget
);
1740 tell_user(stderr
, " when we requested a file "
1741 "called '%s'.", stripsrc
);
1742 tell_user(stderr
, " If this is a wildcard, "
1743 "consider upgrading to SSH 2 or using");
1744 tell_user(stderr
, " the '-unsafe' option. Renaming"
1745 " of this file has been disallowed.");
1746 /* Override the name the server provided with our own. */
1747 striptarget
= stripsrc
;
1751 if (targ
[0] != '\0')
1752 destfname
= dir_file_cat(targ
, striptarget
);
1754 destfname
= dupstr(striptarget
);
1757 * In this branch of the if, the target area is a
1758 * single file with an explicitly specified name in any
1759 * case, so there's no danger.
1761 destfname
= dupstr(targ
);
1763 attr
= file_type(destfname
);
1764 exists
= (attr
!= FILE_TYPE_NONEXISTENT
);
1766 if (act
.action
== SCP_SINK_DIR
) {
1767 if (exists
&& attr
!= FILE_TYPE_DIRECTORY
) {
1768 run_err("%s: Not a directory", destfname
);
1772 if (!create_directory(destfname
)) {
1773 run_err("%s: Cannot create directory", destfname
);
1777 sink(destfname
, NULL
);
1778 /* can we set the timestamp for directories ? */
1782 f
= open_new_file(destfname
);
1784 run_err("%s: Cannot create file", destfname
);
1788 if (scp_accept_filexfer())
1792 stat_starttime
= time(NULL
);
1794 stat_name
= stripslashes(destfname
, 1);
1797 while (received
< act
.size
) {
1798 char transbuf
[4096];
1801 if (blksize
> (int)(act
.size
- received
))
1802 blksize
= act
.size
- received
;
1803 read
= scp_recv_filedata(transbuf
, blksize
);
1805 bump("Lost connection");
1808 if (write_to_file(f
, transbuf
, read
) != (int)read
) {
1810 /* FIXME: in sftp we can actually abort the transfer */
1812 printf("\r%-25.25s | %50s\n",
1814 "Write error.. waiting for end of file");
1819 if (time(NULL
) > stat_lasttime
||
1820 received
+ read
== act
.size
) {
1821 stat_lasttime
= time(NULL
);
1822 print_stats(stat_name
, act
.size
, stat_bytes
,
1823 stat_starttime
, stat_lasttime
);
1829 set_file_times(f
, act
.mtime
, act
.atime
);
1834 run_err("%s: Write error", destfname
);
1837 (void) scp_finish_filerecv();
1844 * We will copy local files to a remote server.
1846 static void toremote(int argc
, char *argv
[])
1848 char *src
, *targ
, *host
, *user
;
1852 targ
= argv
[argc
- 1];
1854 /* Separate host from filename */
1858 bump("targ == NULL in toremote()");
1862 /* Substitute "." for emtpy target */
1864 /* Separate host and username */
1866 host
= strrchr(host
, '@');
1877 if (colon(argv
[0]) != NULL
)
1878 bump("%s: Remote to remote not supported", argv
[0]);
1880 wc_type
= test_wildcard(argv
[0], 1);
1881 if (wc_type
== WCTYPE_NONEXISTENT
)
1882 bump("%s: No such file or directory\n", argv
[0]);
1883 else if (wc_type
== WCTYPE_WILDCARD
)
1884 targetshouldbedirectory
= 1;
1887 cmd
= dupprintf("scp%s%s%s%s -t %s",
1888 verbose ?
" -v" : "",
1889 recursive ?
" -r" : "",
1890 preserve ?
" -p" : "",
1891 targetshouldbedirectory ?
" -d" : "", targ
);
1892 do_cmd(host
, user
, cmd
);
1895 scp_source_setup(targ
, targetshouldbedirectory
);
1897 for (i
= 0; i
< argc
- 1; i
++) {
1899 if (colon(src
) != NULL
) {
1900 tell_user(stderr
, "%s: Remote to remote not supported\n", src
);
1905 wc_type
= test_wildcard(src
, 1);
1906 if (wc_type
== WCTYPE_NONEXISTENT
) {
1907 run_err("%s: No such file or directory", src
);
1909 } else if (wc_type
== WCTYPE_FILENAME
) {
1913 WildcardMatcher
*wc
;
1916 wc
= begin_wildcard_matching(src
);
1918 run_err("%s: No such file or directory", src
);
1922 while ((filename
= wildcard_get_filename(wc
)) != NULL
) {
1927 finish_wildcard_matching(wc
);
1933 * We will copy files from a remote server to the local machine.
1935 static void tolocal(int argc
, char *argv
[])
1937 char *src
, *targ
, *host
, *user
;
1941 bump("More than one remote source not supported");
1946 /* Separate host from filename */
1950 bump("Local to local copy not supported");
1954 /* Substitute "." for empty filename */
1956 /* Separate username and hostname */
1958 host
= strrchr(host
, '@');
1968 cmd
= dupprintf("scp%s%s%s%s -f %s",
1969 verbose ?
" -v" : "",
1970 recursive ?
" -r" : "",
1971 preserve ?
" -p" : "",
1972 targetshouldbedirectory ?
" -d" : "", src
);
1973 do_cmd(host
, user
, cmd
);
1976 if (scp_sink_setup(src
, preserve
, recursive
))
1983 * We will issue a list command to get a remote directory.
1985 static void get_dir_list(int argc
, char *argv
[])
1987 char *src
, *host
, *user
;
1993 /* Separate host from filename */
1997 bump("Local to local copy not supported");
2001 /* Substitute "." for empty filename */
2003 /* Separate username and hostname */
2005 host
= strrchr(host
, '@');
2015 cmd
= snewn(4 * strlen(src
) + 100, char);
2016 strcpy(cmd
, "ls -la '");
2017 p
= cmd
+ strlen(cmd
);
2018 for (q
= src
; *q
; q
++) {
2031 do_cmd(host
, user
, cmd
);
2035 scp_sftp_listdir(src
);
2037 while (ssh_scp_recv(&c
, 1) > 0)
2038 tell_char(stdout
, c
);
2043 * Short description of parameters.
2045 static void usage(void)
2047 printf("PuTTY Secure Copy client\n");
2048 printf("%s\n", ver
);
2049 printf("Usage: pscp [options] [user@]host:source target\n");
2051 (" pscp [options] source [source...] [user@]host:target\n");
2052 printf(" pscp [options] -ls user@host:filespec\n");
2053 printf("Options:\n");
2054 printf(" -p preserve file attributes\n");
2055 printf(" -q quiet, don't show statistics\n");
2056 printf(" -r copy directories recursively\n");
2057 printf(" -v show verbose messages\n");
2058 printf(" -load sessname Load settings from saved session\n");
2059 printf(" -P port connect to specified port\n");
2060 printf(" -l user connect with specified username\n");
2061 printf(" -pw passw login with specified password\n");
2062 printf(" -1 -2 force use of particular SSH protocol version\n");
2063 printf(" -C enable compression\n");
2064 printf(" -i key private key file for authentication\n");
2065 printf(" -batch disable all interactive prompts\n");
2066 printf(" -unsafe allow server-side wildcards (DANGEROUS)\n");
2069 * -gui is an internal option, used by GUI front ends to get
2070 * pscp to pass progress reports back to them. It's not an
2071 * ordinary user-accessible option, so it shouldn't be part of
2072 * the command-line help. The only people who need to know
2073 * about it are programmers, and they can read the source.
2076 (" -gui hWnd GUI mode with the windows handle for receiving messages\n");
2081 void cmdline_error(char *p
, ...)
2084 fprintf(stderr
, "pscp: ");
2086 vfprintf(stderr
, p
, ap
);
2088 fprintf(stderr
, "\n try typing just \"pscp\" for help\n");
2093 * Main program. (Called `psftp_main' because it gets called from
2094 * *sftp.c; bit silly, I know, but it had to be called _something_.)
2096 int psftp_main(int argc
, char *argv
[])
2100 default_protocol
= PROT_TELNET
;
2103 #ifdef FLAG_SYNCAGENT
2107 cmdline_tooltype
= TOOLTYPE_FILETRANSFER
;
2108 ssh_get_line
= &console_get_line
;
2111 for (i
= 1; i
< argc
; i
++) {
2113 if (argv
[i
][0] != '-')
2115 ret
= cmdline_process_param(argv
[i
], i
+1<argc?argv
[i
+1]:NULL
, 1, &cfg
);
2117 cmdline_error("option \"%s\" requires an argument", argv
[i
]);
2118 } else if (ret
== 2) {
2119 i
++; /* skip next argument */
2120 } else if (ret
== 1) {
2121 /* We have our own verbosity in addition to `flags'. */
2122 if (flags
& FLAG_VERBOSE
)
2124 } else if (strcmp(argv
[i
], "-r") == 0) {
2126 } else if (strcmp(argv
[i
], "-p") == 0) {
2128 } else if (strcmp(argv
[i
], "-q") == 0) {
2130 } else if (strcmp(argv
[i
], "-h") == 0 || strcmp(argv
[i
], "-?") == 0) {
2132 } else if (strcmp(argv
[i
], "-gui") == 0 && i
+ 1 < argc
) {
2133 gui_enable(argv
[++i
]);
2135 console_batch_mode
= TRUE
;
2136 } else if (strcmp(argv
[i
], "-ls") == 0) {
2138 } else if (strcmp(argv
[i
], "-batch") == 0) {
2139 console_batch_mode
= 1;
2140 } else if (strcmp(argv
[i
], "-unsafe") == 0) {
2141 scp_unsafe_mode
= 1;
2142 } else if (strcmp(argv
[i
], "--") == 0) {
2146 cmdline_error("unknown option \"%s\"", argv
[i
]);
2156 get_dir_list(argc
, argv
);
2163 targetshouldbedirectory
= 1;
2165 if (colon(argv
[argc
- 1]) != NULL
)
2166 toremote(argc
, argv
);
2168 tolocal(argc
, argv
);
2171 if (back
!= NULL
&& back
->socket(backhandle
) != NULL
) {
2173 back
->special(backhandle
, TS_EOF
);
2174 ssh_scp_recv(&ch
, 1);
2179 gui_send_errcount(list
, errs
);
2182 console_provide_logctx(NULL
);
2183 back
->free(backhandle
);
2187 return (errs
== 0 ?
0 : 1);