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
;
473 * Find a colon in str and return a pointer to the colon.
474 * This is used to separate hostname from filename.
476 static char *colon(char *str
)
478 /* We ignore a leading colon, since the hostname cannot be
479 empty. We also ignore a colon as second character because
480 of filenames like f:myfile.txt. */
481 if (str
[0] == '\0' || str
[0] == ':' || str
[1] == ':')
483 while (*str
!= '\0' && *str
!= ':' && *str
!= '/' && *str
!= '\\')
492 * Return a pointer to the portion of str that comes after the last
493 * slash (or backslash or colon, if `local' is TRUE).
495 static char *stripslashes(char *str
, int local
)
500 p
= strchr(str
, ':');
504 p
= strrchr(str
, '/');
508 p
= strrchr(str
, '\\');
516 * Determine whether a string is entirely composed of dots.
518 static int is_dots(char *str
)
520 return str
[strspn(str
, ".")] == '\0';
524 * Wait for a response from the other side.
525 * Return 0 if ok, -1 if error.
527 static int response(void)
529 char ch
, resp
, rbuf
[2048];
532 if (ssh_scp_recv(&resp
, 1) <= 0)
533 bump("Lost connection");
543 case 2: /* fatal error */
545 if (ssh_scp_recv(&ch
, 1) <= 0)
546 bump("Protocol error: Lost connection");
548 } while (p
< sizeof(rbuf
) && ch
!= '\n');
551 tell_user(stderr
, "%s\n", rbuf
);
559 int sftp_recvdata(char *buf
, int len
)
561 return ssh_scp_recv(buf
, len
);
563 int sftp_senddata(char *buf
, int len
)
565 back
->send(backhandle
, (unsigned char *) buf
, len
);
569 /* ----------------------------------------------------------------------
570 * sftp-based replacement for the hacky `pscp -ls'.
572 static int sftp_ls_compare(const void *av
, const void *bv
)
574 const struct fxp_name
*a
= (const struct fxp_name
*) av
;
575 const struct fxp_name
*b
= (const struct fxp_name
*) bv
;
576 return strcmp(a
->filename
, b
->filename
);
578 void scp_sftp_listdir(char *dirname
)
580 struct fxp_handle
*dirh
;
581 struct fxp_names
*names
;
582 struct fxp_name
*ournames
;
583 struct sftp_packet
*pktin
;
584 struct sftp_request
*req
, *rreq
;
585 int nnames
, namesize
;
589 tell_user(stderr
, "unable to initialise SFTP: %s", fxp_error());
594 printf("Listing directory %s\n", dirname
);
596 sftp_register(req
= fxp_opendir_send(dirname
));
597 rreq
= sftp_find_request(pktin
= sftp_recv());
599 dirh
= fxp_opendir_recv(pktin
, rreq
);
602 printf("Unable to open %s: %s\n", dirname
, fxp_error());
604 nnames
= namesize
= 0;
609 sftp_register(req
= fxp_readdir_send(dirh
));
610 rreq
= sftp_find_request(pktin
= sftp_recv());
612 names
= fxp_readdir_recv(pktin
, rreq
);
615 if (fxp_error_type() == SSH_FX_EOF
)
617 printf("Reading directory %s: %s\n", dirname
, fxp_error());
620 if (names
->nnames
== 0) {
621 fxp_free_names(names
);
625 if (nnames
+ names
->nnames
>= namesize
) {
626 namesize
+= names
->nnames
+ 128;
627 ournames
= sresize(ournames
, namesize
, struct fxp_name
);
630 for (i
= 0; i
< names
->nnames
; i
++)
631 ournames
[nnames
++] = names
->names
[i
];
633 names
->nnames
= 0; /* prevent free_names */
634 fxp_free_names(names
);
636 sftp_register(req
= fxp_close_send(dirh
));
637 rreq
= sftp_find_request(pktin
= sftp_recv());
639 fxp_close_recv(pktin
, rreq
);
642 * Now we have our filenames. Sort them by actual file
643 * name, and then output the longname parts.
645 qsort(ournames
, nnames
, sizeof(*ournames
), sftp_ls_compare
);
650 for (i
= 0; i
< nnames
; i
++)
651 printf("%s\n", ournames
[i
].longname
);
655 /* ----------------------------------------------------------------------
656 * Helper routines that contain the actual SCP protocol elements,
657 * implemented both as SCP1 and SFTP.
660 static struct scp_sftp_dirstack
{
661 struct scp_sftp_dirstack
*next
;
662 struct fxp_name
*names
;
663 int namepos
, namelen
;
666 int matched_something
; /* wildcard match set was non-empty */
667 } *scp_sftp_dirstack_head
;
668 static char *scp_sftp_remotepath
, *scp_sftp_currentname
;
669 static char *scp_sftp_wildcard
;
670 static int scp_sftp_targetisdir
, scp_sftp_donethistarget
;
671 static int scp_sftp_preserve
, scp_sftp_recursive
;
672 static unsigned long scp_sftp_mtime
, scp_sftp_atime
;
673 static int scp_has_times
;
674 static struct fxp_handle
*scp_sftp_filehandle
;
675 static struct fxp_xfer
*scp_sftp_xfer
;
676 static uint64 scp_sftp_fileoffset
;
678 void scp_source_setup(char *target
, int shouldbedir
)
682 * Find out whether the target filespec is in fact a
685 struct sftp_packet
*pktin
;
686 struct sftp_request
*req
, *rreq
;
687 struct fxp_attrs attrs
;
691 tell_user(stderr
, "unable to initialise SFTP: %s", fxp_error());
696 sftp_register(req
= fxp_stat_send(target
));
697 rreq
= sftp_find_request(pktin
= sftp_recv());
699 ret
= fxp_stat_recv(pktin
, rreq
, &attrs
);
701 if (!ret
|| !(attrs
.flags
& SSH_FILEXFER_ATTR_PERMISSIONS
))
702 scp_sftp_targetisdir
= 0;
704 scp_sftp_targetisdir
= (attrs
.permissions
& 0040000) != 0;
706 if (shouldbedir
&& !scp_sftp_targetisdir
) {
707 bump("pscp: remote filespec %s: not a directory\n", target
);
710 scp_sftp_remotepath
= dupstr(target
);
718 int scp_send_errmsg(char *str
)
721 /* do nothing; we never need to send our errors to the server */
723 back
->send(backhandle
, "\001", 1);/* scp protocol error prefix */
724 back
->send(backhandle
, str
, strlen(str
));
726 return 0; /* can't fail */
729 int scp_send_filetimes(unsigned long mtime
, unsigned long atime
)
732 scp_sftp_mtime
= mtime
;
733 scp_sftp_atime
= atime
;
738 sprintf(buf
, "T%lu 0 %lu 0\n", mtime
, atime
);
739 back
->send(backhandle
, buf
, strlen(buf
));
744 int scp_send_filename(char *name
, unsigned long size
, int modes
)
748 struct sftp_packet
*pktin
;
749 struct sftp_request
*req
, *rreq
;
751 if (scp_sftp_targetisdir
) {
752 fullname
= dupcat(scp_sftp_remotepath
, "/", name
, NULL
);
754 fullname
= dupstr(scp_sftp_remotepath
);
757 sftp_register(req
= fxp_open_send(fullname
, SSH_FXF_WRITE
|
758 SSH_FXF_CREAT
| SSH_FXF_TRUNC
));
759 rreq
= sftp_find_request(pktin
= sftp_recv());
761 scp_sftp_filehandle
= fxp_open_recv(pktin
, rreq
);
763 if (!scp_sftp_filehandle
) {
764 tell_user(stderr
, "pscp: unable to open %s: %s",
765 fullname
, fxp_error());
769 scp_sftp_fileoffset
= uint64_make(0, 0);
770 scp_sftp_xfer
= xfer_upload_init(scp_sftp_filehandle
,
771 scp_sftp_fileoffset
);
776 sprintf(buf
, "C%04o %lu ", modes
, size
);
777 back
->send(backhandle
, buf
, strlen(buf
));
778 back
->send(backhandle
, name
, strlen(name
));
779 back
->send(backhandle
, "\n", 1);
784 int scp_send_filedata(char *data
, int len
)
788 struct sftp_packet
*pktin
;
790 if (!scp_sftp_filehandle
) {
794 while (!xfer_upload_ready(scp_sftp_xfer
)) {
796 ret
= xfer_upload_gotpkt(scp_sftp_xfer
, pktin
);
798 tell_user(stderr
, "error while writing: %s\n", fxp_error());
804 xfer_upload_data(scp_sftp_xfer
, data
, len
);
806 scp_sftp_fileoffset
= uint64_add32(scp_sftp_fileoffset
, len
);
809 int bufsize
= back
->send(backhandle
, data
, len
);
812 * If the network transfer is backing up - that is, the
813 * remote site is not accepting data as fast as we can
814 * produce it - then we must loop on network events until
815 * we have space in the buffer again.
817 while (bufsize
> MAX_SCP_BUFSIZE
) {
818 if (ssh_sftp_loop_iteration() < 0)
820 bufsize
= back
->sendbuffer(backhandle
);
827 int scp_send_finish(void)
830 struct fxp_attrs attrs
;
831 struct sftp_packet
*pktin
;
832 struct sftp_request
*req
, *rreq
;
835 while (!xfer_done(scp_sftp_xfer
)) {
837 xfer_upload_gotpkt(scp_sftp_xfer
, pktin
);
839 xfer_cleanup(scp_sftp_xfer
);
841 if (!scp_sftp_filehandle
) {
845 attrs
.flags
= SSH_FILEXFER_ATTR_ACMODTIME
;
846 attrs
.atime
= scp_sftp_atime
;
847 attrs
.mtime
= scp_sftp_mtime
;
848 sftp_register(req
= fxp_fsetstat_send(scp_sftp_filehandle
, attrs
));
849 rreq
= sftp_find_request(pktin
= sftp_recv());
851 ret
= fxp_fsetstat_recv(pktin
, rreq
);
853 tell_user(stderr
, "unable to set file times: %s\n", fxp_error());
857 sftp_register(req
= fxp_close_send(scp_sftp_filehandle
));
858 rreq
= sftp_find_request(pktin
= sftp_recv());
860 fxp_close_recv(pktin
, rreq
);
864 back
->send(backhandle
, "", 1);
869 char *scp_save_remotepath(void)
872 return scp_sftp_remotepath
;
877 void scp_restore_remotepath(char *data
)
880 scp_sftp_remotepath
= data
;
883 int scp_send_dirname(char *name
, int modes
)
888 struct fxp_attrs attrs
;
889 struct sftp_packet
*pktin
;
890 struct sftp_request
*req
, *rreq
;
893 if (scp_sftp_targetisdir
) {
894 fullname
= dupcat(scp_sftp_remotepath
, "/", name
, NULL
);
896 fullname
= dupstr(scp_sftp_remotepath
);
900 * We don't worry about whether we managed to create the
901 * directory, because if it exists already it's OK just to
902 * use it. Instead, we will stat it afterwards, and if it
903 * exists and is a directory we will assume we were either
904 * successful or it didn't matter.
906 sftp_register(req
= fxp_mkdir_send(fullname
));
907 rreq
= sftp_find_request(pktin
= sftp_recv());
909 ret
= fxp_mkdir_recv(pktin
, rreq
);
914 err
= "server reported no error";
916 sftp_register(req
= fxp_stat_send(fullname
));
917 rreq
= sftp_find_request(pktin
= sftp_recv());
919 ret
= fxp_stat_recv(pktin
, rreq
, &attrs
);
921 if (!ret
|| !(attrs
.flags
& SSH_FILEXFER_ATTR_PERMISSIONS
) ||
922 !(attrs
.permissions
& 0040000)) {
923 tell_user(stderr
, "unable to create directory %s: %s",
929 scp_sftp_remotepath
= fullname
;
934 sprintf(buf
, "D%04o 0 ", modes
);
935 back
->send(backhandle
, buf
, strlen(buf
));
936 back
->send(backhandle
, name
, strlen(name
));
937 back
->send(backhandle
, "\n", 1);
942 int scp_send_enddir(void)
945 sfree(scp_sftp_remotepath
);
948 back
->send(backhandle
, "E\n", 2);
954 * Yes, I know; I have an scp_sink_setup _and_ an scp_sink_init.
955 * That's bad. The difference is that scp_sink_setup is called once
956 * right at the start, whereas scp_sink_init is called to
957 * initialise every level of recursion in the protocol.
959 int scp_sink_setup(char *source
, int preserve
, int recursive
)
965 tell_user(stderr
, "unable to initialise SFTP: %s", fxp_error());
970 * It's possible that the source string we've been given
971 * contains a wildcard. If so, we must split the directory
972 * away from the wildcard itself (throwing an error if any
973 * wildcardness comes before the final slash) and arrange
974 * things so that a dirstack entry will be set up.
976 newsource
= snewn(1+strlen(source
), char);
977 if (!wc_unescape(newsource
, source
)) {
978 /* Yes, here we go; it's a wildcard. Bah. */
979 char *dupsource
, *lastpart
, *dirpart
, *wildcard
;
980 dupsource
= dupstr(source
);
981 lastpart
= stripslashes(dupsource
, 0);
982 wildcard
= dupstr(lastpart
);
984 if (*dupsource
&& dupsource
[1]) {
986 * The remains of dupsource are at least two
987 * characters long, meaning the pathname wasn't
988 * empty or just `/'. Hence, we remove the trailing
992 } else if (!*dupsource
) {
994 * The remains of dupsource are _empty_ - the whole
995 * pathname was a wildcard. Hence we need to
996 * replace it with ".".
999 dupsource
= dupstr(".");
1003 * Now we have separated our string into dupsource (the
1004 * directory part) and wildcard. Both of these will
1005 * need freeing at some point. Next step is to remove
1006 * wildcard escapes from the directory part, throwing
1007 * an error if it contains a real wildcard.
1009 dirpart
= snewn(1+strlen(dupsource
), char);
1010 if (!wc_unescape(dirpart
, dupsource
)) {
1011 tell_user(stderr
, "%s: multiple-level wildcards unsupported",
1021 * Now we have dirpart (unescaped, ie a valid remote
1022 * path), and wildcard (a wildcard). This will be
1023 * sufficient to arrange a dirstack entry.
1025 scp_sftp_remotepath
= dirpart
;
1026 scp_sftp_wildcard
= wildcard
;
1029 scp_sftp_remotepath
= newsource
;
1030 scp_sftp_wildcard
= NULL
;
1032 scp_sftp_preserve
= preserve
;
1033 scp_sftp_recursive
= recursive
;
1034 scp_sftp_donethistarget
= 0;
1035 scp_sftp_dirstack_head
= NULL
;
1040 int scp_sink_init(void)
1043 back
->send(backhandle
, "", 1);
1048 #define SCP_SINK_FILE 1
1049 #define SCP_SINK_DIR 2
1050 #define SCP_SINK_ENDDIR 3
1051 #define SCP_SINK_RETRY 4 /* not an action; just try again */
1052 struct scp_sink_action
{
1053 int action
; /* FILE, DIR, ENDDIR */
1054 char *buf
; /* will need freeing after use */
1055 char *name
; /* filename or dirname (not ENDDIR) */
1056 int mode
; /* access mode (not ENDDIR) */
1057 unsigned long size
; /* file size (not ENDDIR) */
1058 int settime
; /* 1 if atime and mtime are filled */
1059 unsigned long atime
, mtime
; /* access times for the file */
1062 int scp_get_sink_action(struct scp_sink_action
*act
)
1066 int must_free_fname
;
1067 struct fxp_attrs attrs
;
1068 struct sftp_packet
*pktin
;
1069 struct sftp_request
*req
, *rreq
;
1072 if (!scp_sftp_dirstack_head
) {
1073 if (!scp_sftp_donethistarget
) {
1075 * Simple case: we are only dealing with one file.
1077 fname
= scp_sftp_remotepath
;
1078 must_free_fname
= 0;
1079 scp_sftp_donethistarget
= 1;
1082 * Even simpler case: one file _which we've done_.
1083 * Return 1 (finished).
1089 * We're now in the middle of stepping through a list
1090 * of names returned from fxp_readdir(); so let's carry
1093 struct scp_sftp_dirstack
*head
= scp_sftp_dirstack_head
;
1094 while (head
->namepos
< head
->namelen
&&
1095 (is_dots(head
->names
[head
->namepos
].filename
) ||
1097 !wc_match(head
->wildcard
,
1098 head
->names
[head
->namepos
].filename
))))
1099 head
->namepos
++; /* skip . and .. */
1100 if (head
->namepos
< head
->namelen
) {
1101 head
->matched_something
= 1;
1102 fname
= dupcat(head
->dirpath
, "/",
1103 head
->names
[head
->namepos
++].filename
,
1105 must_free_fname
= 1;
1108 * We've come to the end of the list; pop it off
1109 * the stack and return an ENDDIR action (or RETRY
1110 * if this was a wildcard match).
1112 if (head
->wildcard
) {
1113 act
->action
= SCP_SINK_RETRY
;
1114 if (!head
->matched_something
) {
1115 tell_user(stderr
, "pscp: wildcard '%s' matched "
1116 "no files", head
->wildcard
);
1119 sfree(head
->wildcard
);
1122 act
->action
= SCP_SINK_ENDDIR
;
1125 sfree(head
->dirpath
);
1127 scp_sftp_dirstack_head
= head
->next
;
1135 * Now we have a filename. Stat it, and see if it's a file
1138 sftp_register(req
= fxp_stat_send(fname
));
1139 rreq
= sftp_find_request(pktin
= sftp_recv());
1140 assert(rreq
== req
);
1141 ret
= fxp_stat_recv(pktin
, rreq
, &attrs
);
1143 if (!ret
|| !(attrs
.flags
& SSH_FILEXFER_ATTR_PERMISSIONS
)) {
1144 tell_user(stderr
, "unable to identify %s: %s", fname
,
1145 ret ?
"file type not supplied" : fxp_error());
1150 if (attrs
.permissions
& 0040000) {
1151 struct scp_sftp_dirstack
*newitem
;
1152 struct fxp_handle
*dirhandle
;
1153 int nnames
, namesize
;
1154 struct fxp_name
*ournames
;
1155 struct fxp_names
*names
;
1158 * It's a directory. If we're not in recursive mode,
1159 * this merits a complaint (which is fatal if the name
1160 * was specified directly, but not if it was matched by
1163 * We skip this complaint completely if
1164 * scp_sftp_wildcard is set, because that's an
1165 * indication that we're not actually supposed to
1166 * _recursively_ transfer the dir, just scan it for
1167 * things matching the wildcard.
1169 if (!scp_sftp_recursive
&& !scp_sftp_wildcard
) {
1170 tell_user(stderr
, "pscp: %s: is a directory", fname
);
1172 if (must_free_fname
) sfree(fname
);
1173 if (scp_sftp_dirstack_head
) {
1174 act
->action
= SCP_SINK_RETRY
;
1182 * Otherwise, the fun begins. We must fxp_opendir() the
1183 * directory, slurp the filenames into memory, return
1184 * SCP_SINK_DIR (unless this is a wildcard match), and
1185 * set targetisdir. The next time we're called, we will
1186 * run through the list of filenames one by one,
1187 * matching them against a wildcard if present.
1189 * If targetisdir is _already_ set (meaning we're
1190 * already in the middle of going through another such
1191 * list), we must push the other (target,namelist) pair
1194 sftp_register(req
= fxp_opendir_send(fname
));
1195 rreq
= sftp_find_request(pktin
= sftp_recv());
1196 assert(rreq
== req
);
1197 dirhandle
= fxp_opendir_recv(pktin
, rreq
);
1200 tell_user(stderr
, "scp: unable to open directory %s: %s",
1201 fname
, fxp_error());
1202 if (must_free_fname
) sfree(fname
);
1206 nnames
= namesize
= 0;
1211 sftp_register(req
= fxp_readdir_send(dirhandle
));
1212 rreq
= sftp_find_request(pktin
= sftp_recv());
1213 assert(rreq
== req
);
1214 names
= fxp_readdir_recv(pktin
, rreq
);
1216 if (names
== NULL
) {
1217 if (fxp_error_type() == SSH_FX_EOF
)
1219 tell_user(stderr
, "scp: reading directory %s: %s\n",
1220 fname
, fxp_error());
1221 if (must_free_fname
) sfree(fname
);
1226 if (names
->nnames
== 0) {
1227 fxp_free_names(names
);
1230 if (nnames
+ names
->nnames
>= namesize
) {
1231 namesize
+= names
->nnames
+ 128;
1232 ournames
= sresize(ournames
, namesize
, struct fxp_name
);
1234 for (i
= 0; i
< names
->nnames
; i
++)
1235 ournames
[nnames
++] = names
->names
[i
];
1236 names
->nnames
= 0; /* prevent free_names */
1237 fxp_free_names(names
);
1239 sftp_register(req
= fxp_close_send(dirhandle
));
1240 rreq
= sftp_find_request(pktin
= sftp_recv());
1241 assert(rreq
== req
);
1242 fxp_close_recv(pktin
, rreq
);
1244 newitem
= snew(struct scp_sftp_dirstack
);
1245 newitem
->next
= scp_sftp_dirstack_head
;
1246 newitem
->names
= ournames
;
1247 newitem
->namepos
= 0;
1248 newitem
->namelen
= nnames
;
1249 if (must_free_fname
)
1250 newitem
->dirpath
= fname
;
1252 newitem
->dirpath
= dupstr(fname
);
1253 if (scp_sftp_wildcard
) {
1254 newitem
->wildcard
= scp_sftp_wildcard
;
1255 newitem
->matched_something
= 0;
1256 scp_sftp_wildcard
= NULL
;
1258 newitem
->wildcard
= NULL
;
1260 scp_sftp_dirstack_head
= newitem
;
1262 if (newitem
->wildcard
) {
1263 act
->action
= SCP_SINK_RETRY
;
1265 act
->action
= SCP_SINK_DIR
;
1266 act
->buf
= dupstr(stripslashes(fname
, 0));
1267 act
->name
= act
->buf
;
1268 act
->size
= 0; /* duhh, it's a directory */
1269 act
->mode
= 07777 & attrs
.permissions
;
1270 if (scp_sftp_preserve
&&
1271 (attrs
.flags
& SSH_FILEXFER_ATTR_ACMODTIME
)) {
1272 act
->atime
= attrs
.atime
;
1273 act
->mtime
= attrs
.mtime
;
1282 * It's a file. Return SCP_SINK_FILE.
1284 act
->action
= SCP_SINK_FILE
;
1285 act
->buf
= dupstr(stripslashes(fname
, 0));
1286 act
->name
= act
->buf
;
1287 if (attrs
.flags
& SSH_FILEXFER_ATTR_SIZE
) {
1288 if (uint64_compare(attrs
.size
,
1289 uint64_make(0, ULONG_MAX
)) > 0) {
1290 act
->size
= ULONG_MAX
; /* *boggle* */
1292 act
->size
= attrs
.size
.lo
;
1294 act
->size
= ULONG_MAX
; /* no idea */
1295 act
->mode
= 07777 & attrs
.permissions
;
1296 if (scp_sftp_preserve
&&
1297 (attrs
.flags
& SSH_FILEXFER_ATTR_ACMODTIME
)) {
1298 act
->atime
= attrs
.atime
;
1299 act
->mtime
= attrs
.mtime
;
1303 if (must_free_fname
)
1304 scp_sftp_currentname
= fname
;
1306 scp_sftp_currentname
= dupstr(fname
);
1321 if (ssh_scp_recv(&ch
, 1) <= 0)
1324 bump("Protocol error: Unexpected newline");
1328 if (ssh_scp_recv(&ch
, 1) <= 0)
1329 bump("Lost connection");
1332 act
->buf
= sresize(act
->buf
, bufsize
, char);
1335 } while (ch
!= '\n');
1336 act
->buf
[i
- 1] = '\0';
1338 case '\01': /* error */
1339 tell_user(stderr
, "%s\n", act
->buf
);
1341 continue; /* go round again */
1342 case '\02': /* fatal error */
1343 bump("%s", act
->buf
);
1345 back
->send(backhandle
, "", 1);
1346 act
->action
= SCP_SINK_ENDDIR
;
1349 if (sscanf(act
->buf
, "%ld %*d %ld %*d",
1350 &act
->mtime
, &act
->atime
) == 2) {
1352 back
->send(backhandle
, "", 1);
1353 continue; /* go round again */
1355 bump("Protocol error: Illegal time format");
1358 act
->action
= (action
== 'C' ? SCP_SINK_FILE
: SCP_SINK_DIR
);
1361 bump("Protocol error: Expected control record");
1364 * We will go round this loop only once, unless we hit
1371 * If we get here, we must have seen SCP_SINK_FILE or
1374 if (sscanf(act
->buf
, "%o %lu %n", &act
->mode
, &act
->size
, &i
) != 2)
1375 bump("Protocol error: Illegal file descriptor format");
1376 act
->name
= act
->buf
+ i
;
1381 int scp_accept_filexfer(void)
1384 struct sftp_packet
*pktin
;
1385 struct sftp_request
*req
, *rreq
;
1387 sftp_register(req
= fxp_open_send(scp_sftp_currentname
, SSH_FXF_READ
));
1388 rreq
= sftp_find_request(pktin
= sftp_recv());
1389 assert(rreq
== req
);
1390 scp_sftp_filehandle
= fxp_open_recv(pktin
, rreq
);
1392 if (!scp_sftp_filehandle
) {
1393 tell_user(stderr
, "pscp: unable to open %s: %s",
1394 scp_sftp_currentname
, fxp_error());
1398 scp_sftp_fileoffset
= uint64_make(0, 0);
1399 scp_sftp_xfer
= xfer_download_init(scp_sftp_filehandle
,
1400 scp_sftp_fileoffset
);
1401 sfree(scp_sftp_currentname
);
1404 back
->send(backhandle
, "", 1);
1405 return 0; /* can't fail */
1409 int scp_recv_filedata(char *data
, int len
)
1412 struct sftp_packet
*pktin
;
1416 xfer_download_queue(scp_sftp_xfer
);
1417 pktin
= sftp_recv();
1418 ret
= xfer_download_gotpkt(scp_sftp_xfer
, pktin
);
1421 tell_user(stderr
, "pscp: error while reading: %s", fxp_error());
1426 if (xfer_download_data(scp_sftp_xfer
, &vbuf
, &actuallen
)) {
1428 * This assertion relies on the fact that the natural
1429 * block size used in the xfer manager is at most that
1430 * used in this module. I don't like crossing layers in
1431 * this way, but it'll do for now.
1433 assert(actuallen
<= len
);
1434 memcpy(data
, vbuf
, actuallen
);
1439 scp_sftp_fileoffset
= uint64_add32(scp_sftp_fileoffset
, actuallen
);
1443 return ssh_scp_recv(data
, len
);
1447 int scp_finish_filerecv(void)
1450 struct sftp_packet
*pktin
;
1451 struct sftp_request
*req
, *rreq
;
1454 * Ensure that xfer_done() will work correctly, so we can
1455 * clean up any outstanding requests from the file
1458 xfer_set_error(scp_sftp_xfer
);
1459 while (!xfer_done(scp_sftp_xfer
)) {
1463 pktin
= sftp_recv();
1464 xfer_download_gotpkt(scp_sftp_xfer
, pktin
);
1465 if (xfer_download_data(scp_sftp_xfer
, &vbuf
, &len
))
1468 xfer_cleanup(scp_sftp_xfer
);
1470 sftp_register(req
= fxp_close_send(scp_sftp_filehandle
));
1471 rreq
= sftp_find_request(pktin
= sftp_recv());
1472 assert(rreq
== req
);
1473 fxp_close_recv(pktin
, rreq
);
1476 back
->send(backhandle
, "", 1);
1481 /* ----------------------------------------------------------------------
1482 * Send an error message to the other side and to the screen.
1483 * Increment error counter.
1485 static void run_err(const char *fmt
, ...)
1491 str
= dupvprintf(fmt
, ap
);
1492 str2
= dupcat("scp: ", str
, "\n", NULL
);
1494 scp_send_errmsg(str2
);
1495 tell_user(stderr
, "%s", str2
);
1501 * Execute the source part of the SCP protocol.
1503 static void source(char *src
)
1506 unsigned long mtime
, atime
;
1511 unsigned long stat_bytes
;
1512 time_t stat_starttime
, stat_lasttime
;
1514 attr
= file_type(src
);
1515 if (attr
== FILE_TYPE_NONEXISTENT
||
1516 attr
== FILE_TYPE_WEIRD
) {
1517 run_err("%s: %s file or directory", src
,
1518 (attr
== FILE_TYPE_WEIRD ?
"Not a" : "No such"));
1522 if (attr
== FILE_TYPE_DIRECTORY
) {
1525 * Avoid . and .. directories.
1528 p
= strrchr(src
, '/');
1530 p
= strrchr(src
, '\\');
1535 if (!strcmp(p
, ".") || !strcmp(p
, ".."))
1536 /* skip . and .. */ ;
1540 run_err("%s: not a regular file", src
);
1545 if ((last
= strrchr(src
, '/')) == NULL
)
1549 if (strrchr(last
, '\\') != NULL
)
1550 last
= strrchr(last
, '\\') + 1;
1551 if (last
== src
&& strchr(src
, ':') != NULL
)
1552 last
= strchr(src
, ':') + 1;
1554 f
= open_existing_file(src
, &size
, &mtime
, &atime
);
1556 run_err("%s: Cannot open file", src
);
1560 if (scp_send_filetimes(mtime
, atime
))
1565 tell_user(stderr
, "Sending file %s, size=%lu", last
, size
);
1566 if (scp_send_filename(last
, size
, 0644))
1570 stat_starttime
= time(NULL
);
1573 for (i
= 0; i
< size
; i
+= 4096) {
1574 char transbuf
[4096];
1579 if ((j
= read_from_file(f
, transbuf
, k
)) != k
) {
1582 bump("%s: Read error", src
);
1584 if (scp_send_filedata(transbuf
, k
))
1585 bump("%s: Network error occurred", src
);
1589 if (time(NULL
) != stat_lasttime
|| i
+ k
== size
) {
1590 stat_lasttime
= time(NULL
);
1591 print_stats(last
, size
, stat_bytes
,
1592 stat_starttime
, stat_lasttime
);
1599 (void) scp_send_finish();
1603 * Recursively send the contents of a directory.
1605 static void rsource(char *src
)
1611 if ((last
= strrchr(src
, '/')) == NULL
)
1615 if (strrchr(last
, '\\') != NULL
)
1616 last
= strrchr(last
, '\\') + 1;
1617 if (last
== src
&& strchr(src
, ':') != NULL
)
1618 last
= strchr(src
, ':') + 1;
1620 /* maybe send filetime */
1622 save_target
= scp_save_remotepath();
1625 tell_user(stderr
, "Entering directory: %s", last
);
1626 if (scp_send_dirname(last
, 0755))
1629 dir
= open_directory(src
);
1632 while ((filename
= read_filename(dir
)) != NULL
) {
1633 char *foundfile
= dupcat(src
, "/", filename
, NULL
);
1639 close_directory(dir
);
1641 (void) scp_send_enddir();
1643 scp_restore_remotepath(save_target
);
1647 * Execute the sink part of the SCP protocol.
1649 static void sink(char *targ
, char *src
)
1656 unsigned long received
;
1658 unsigned long stat_bytes
;
1659 time_t stat_starttime
, stat_lasttime
;
1662 attr
= file_type(targ
);
1663 if (attr
== FILE_TYPE_DIRECTORY
)
1666 if (targetshouldbedirectory
&& !targisdir
)
1667 bump("%s: Not a directory", targ
);
1671 struct scp_sink_action act
;
1672 if (scp_get_sink_action(&act
))
1675 if (act
.action
== SCP_SINK_ENDDIR
)
1678 if (act
.action
== SCP_SINK_RETRY
)
1683 * Prevent the remote side from maliciously writing to
1684 * files outside the target area by sending a filename
1685 * containing `../'. In fact, it shouldn't be sending
1686 * filenames with any slashes or colons in at all; so
1687 * we'll find the last slash, backslash or colon in the
1688 * filename and use only the part after that. (And
1691 * In addition, we also ensure here that if we're
1692 * copying a single file and the target is a directory
1693 * (common usage: `pscp host:filename .') the remote
1694 * can't send us a _different_ file name. We can
1695 * distinguish this case because `src' will be non-NULL
1696 * and the last component of that will fail to match
1697 * (the last component of) the name sent.
1699 * Well, not always; if `src' is a wildcard, we do
1700 * expect to get back filenames that don't correspond
1701 * exactly to it. Ideally in this case, we would like
1702 * to ensure that the returned filename actually
1703 * matches the wildcard pattern - but one of SCP's
1704 * protocol infelicities is that wildcard matching is
1705 * done at the server end _by the server's rules_ and
1706 * so in general this is infeasible. Hence, we only
1707 * accept filenames that don't correspond to `src' if
1708 * unsafe mode is enabled or we are using SFTP (which
1709 * resolves remote wildcards on the client side and can
1712 char *striptarget
, *stripsrc
;
1714 striptarget
= stripslashes(act
.name
, 1);
1715 if (striptarget
!= act
.name
) {
1716 tell_user(stderr
, "warning: remote host sent a compound"
1717 " pathname '%s'", act
.name
);
1718 tell_user(stderr
, " renaming local file to '%s'",
1723 * Also check to see if the target filename is '.' or
1724 * '..', or indeed '...' and so on because Windows
1725 * appears to interpret those like '..'.
1727 if (is_dots(striptarget
)) {
1728 bump("security violation: remote host attempted to write to"
1729 " a '.' or '..' path!");
1733 stripsrc
= stripslashes(src
, 1);
1734 if (strcmp(striptarget
, stripsrc
) &&
1735 !using_sftp
&& !scp_unsafe_mode
) {
1736 tell_user(stderr
, "warning: remote host tried to write "
1737 "to a file called '%s'", striptarget
);
1738 tell_user(stderr
, " when we requested a file "
1739 "called '%s'.", stripsrc
);
1740 tell_user(stderr
, " If this is a wildcard, "
1741 "consider upgrading to SSH 2 or using");
1742 tell_user(stderr
, " the '-unsafe' option. Renaming"
1743 " of this file has been disallowed.");
1744 /* Override the name the server provided with our own. */
1745 striptarget
= stripsrc
;
1749 if (targ
[0] != '\0')
1750 destfname
= dir_file_cat(targ
, striptarget
);
1752 destfname
= dupstr(striptarget
);
1755 * In this branch of the if, the target area is a
1756 * single file with an explicitly specified name in any
1757 * case, so there's no danger.
1759 destfname
= dupstr(targ
);
1761 attr
= file_type(destfname
);
1762 exists
= (attr
!= FILE_TYPE_NONEXISTENT
);
1764 if (act
.action
== SCP_SINK_DIR
) {
1765 if (exists
&& attr
!= FILE_TYPE_DIRECTORY
) {
1766 run_err("%s: Not a directory", destfname
);
1770 if (!create_directory(destfname
)) {
1771 run_err("%s: Cannot create directory", destfname
);
1775 sink(destfname
, NULL
);
1776 /* can we set the timestamp for directories ? */
1780 f
= open_new_file(destfname
);
1782 run_err("%s: Cannot create file", destfname
);
1786 if (scp_accept_filexfer())
1790 stat_starttime
= time(NULL
);
1792 stat_name
= stripslashes(destfname
, 1);
1795 while (received
< act
.size
) {
1796 char transbuf
[4096];
1799 if (blksize
> (int)(act
.size
- received
))
1800 blksize
= act
.size
- received
;
1801 read
= scp_recv_filedata(transbuf
, blksize
);
1803 bump("Lost connection");
1806 if (write_to_file(f
, transbuf
, read
) != (int)read
) {
1808 /* FIXME: in sftp we can actually abort the transfer */
1810 printf("\r%-25.25s | %50s\n",
1812 "Write error.. waiting for end of file");
1817 if (time(NULL
) > stat_lasttime
||
1818 received
+ read
== act
.size
) {
1819 stat_lasttime
= time(NULL
);
1820 print_stats(stat_name
, act
.size
, stat_bytes
,
1821 stat_starttime
, stat_lasttime
);
1827 set_file_times(f
, act
.mtime
, act
.atime
);
1832 run_err("%s: Write error", destfname
);
1835 (void) scp_finish_filerecv();
1842 * We will copy local files to a remote server.
1844 static void toremote(int argc
, char *argv
[])
1846 char *src
, *targ
, *host
, *user
;
1850 targ
= argv
[argc
- 1];
1852 /* Separate host from filename */
1856 bump("targ == NULL in toremote()");
1860 /* Substitute "." for emtpy target */
1862 /* Separate host and username */
1864 host
= strrchr(host
, '@');
1875 if (colon(argv
[0]) != NULL
)
1876 bump("%s: Remote to remote not supported", argv
[0]);
1878 wc_type
= test_wildcard(argv
[0], 1);
1879 if (wc_type
== WCTYPE_NONEXISTENT
)
1880 bump("%s: No such file or directory\n", argv
[0]);
1881 else if (wc_type
== WCTYPE_WILDCARD
)
1882 targetshouldbedirectory
= 1;
1885 cmd
= dupprintf("scp%s%s%s%s -t %s",
1886 verbose ?
" -v" : "",
1887 recursive ?
" -r" : "",
1888 preserve ?
" -p" : "",
1889 targetshouldbedirectory ?
" -d" : "", targ
);
1890 do_cmd(host
, user
, cmd
);
1893 scp_source_setup(targ
, targetshouldbedirectory
);
1895 for (i
= 0; i
< argc
- 1; i
++) {
1897 if (colon(src
) != NULL
) {
1898 tell_user(stderr
, "%s: Remote to remote not supported\n", src
);
1903 wc_type
= test_wildcard(src
, 1);
1904 if (wc_type
== WCTYPE_NONEXISTENT
) {
1905 run_err("%s: No such file or directory", src
);
1907 } else if (wc_type
== WCTYPE_FILENAME
) {
1911 WildcardMatcher
*wc
;
1914 wc
= begin_wildcard_matching(src
);
1916 run_err("%s: No such file or directory", src
);
1920 while ((filename
= wildcard_get_filename(wc
)) != NULL
) {
1925 finish_wildcard_matching(wc
);
1931 * We will copy files from a remote server to the local machine.
1933 static void tolocal(int argc
, char *argv
[])
1935 char *src
, *targ
, *host
, *user
;
1939 bump("More than one remote source not supported");
1944 /* Separate host from filename */
1948 bump("Local to local copy not supported");
1952 /* Substitute "." for empty filename */
1954 /* Separate username and hostname */
1956 host
= strrchr(host
, '@');
1966 cmd
= dupprintf("scp%s%s%s%s -f %s",
1967 verbose ?
" -v" : "",
1968 recursive ?
" -r" : "",
1969 preserve ?
" -p" : "",
1970 targetshouldbedirectory ?
" -d" : "", src
);
1971 do_cmd(host
, user
, cmd
);
1974 if (scp_sink_setup(src
, preserve
, recursive
))
1981 * We will issue a list command to get a remote directory.
1983 static void get_dir_list(int argc
, char *argv
[])
1985 char *src
, *host
, *user
;
1991 /* Separate host from filename */
1995 bump("Local to local copy not supported");
1999 /* Substitute "." for empty filename */
2001 /* Separate username and hostname */
2003 host
= strrchr(host
, '@');
2013 cmd
= snewn(4 * strlen(src
) + 100, char);
2014 strcpy(cmd
, "ls -la '");
2015 p
= cmd
+ strlen(cmd
);
2016 for (q
= src
; *q
; q
++) {
2029 do_cmd(host
, user
, cmd
);
2033 scp_sftp_listdir(src
);
2035 while (ssh_scp_recv(&c
, 1) > 0)
2036 tell_char(stdout
, c
);
2041 * Short description of parameters.
2043 static void usage(void)
2045 printf("PuTTY Secure Copy client\n");
2046 printf("%s\n", ver
);
2047 printf("Usage: pscp [options] [user@]host:source target\n");
2049 (" pscp [options] source [source...] [user@]host:target\n");
2050 printf(" pscp [options] -ls user@host:filespec\n");
2051 printf("Options:\n");
2052 printf(" -p preserve file attributes\n");
2053 printf(" -q quiet, don't show statistics\n");
2054 printf(" -r copy directories recursively\n");
2055 printf(" -v show verbose messages\n");
2056 printf(" -load sessname Load settings from saved session\n");
2057 printf(" -P port connect to specified port\n");
2058 printf(" -l user connect with specified username\n");
2059 printf(" -pw passw login with specified password\n");
2060 printf(" -1 -2 force use of particular SSH protocol version\n");
2061 printf(" -C enable compression\n");
2062 printf(" -i key private key file for authentication\n");
2063 printf(" -batch disable all interactive prompts\n");
2064 printf(" -unsafe allow server-side wildcards (DANGEROUS)\n");
2067 * -gui is an internal option, used by GUI front ends to get
2068 * pscp to pass progress reports back to them. It's not an
2069 * ordinary user-accessible option, so it shouldn't be part of
2070 * the command-line help. The only people who need to know
2071 * about it are programmers, and they can read the source.
2074 (" -gui hWnd GUI mode with the windows handle for receiving messages\n");
2079 void cmdline_error(char *p
, ...)
2082 fprintf(stderr
, "pscp: ");
2084 vfprintf(stderr
, p
, ap
);
2086 fprintf(stderr
, "\n try typing just \"pscp\" for help\n");
2091 * Main program. (Called `psftp_main' because it gets called from
2092 * *sftp.c; bit silly, I know, but it had to be called _something_.)
2094 int psftp_main(int argc
, char *argv
[])
2098 default_protocol
= PROT_TELNET
;
2101 #ifdef FLAG_SYNCAGENT
2105 cmdline_tooltype
= TOOLTYPE_FILETRANSFER
;
2106 ssh_get_line
= &console_get_line
;
2109 for (i
= 1; i
< argc
; i
++) {
2111 if (argv
[i
][0] != '-')
2113 ret
= cmdline_process_param(argv
[i
], i
+1<argc?argv
[i
+1]:NULL
, 1, &cfg
);
2115 cmdline_error("option \"%s\" requires an argument", argv
[i
]);
2116 } else if (ret
== 2) {
2117 i
++; /* skip next argument */
2118 } else if (ret
== 1) {
2119 /* We have our own verbosity in addition to `flags'. */
2120 if (flags
& FLAG_VERBOSE
)
2122 } else if (strcmp(argv
[i
], "-r") == 0) {
2124 } else if (strcmp(argv
[i
], "-p") == 0) {
2126 } else if (strcmp(argv
[i
], "-q") == 0) {
2128 } else if (strcmp(argv
[i
], "-h") == 0 || strcmp(argv
[i
], "-?") == 0) {
2130 } else if (strcmp(argv
[i
], "-gui") == 0 && i
+ 1 < argc
) {
2131 gui_enable(argv
[++i
]);
2133 console_batch_mode
= TRUE
;
2134 } else if (strcmp(argv
[i
], "-ls") == 0) {
2136 } else if (strcmp(argv
[i
], "-batch") == 0) {
2137 console_batch_mode
= 1;
2138 } else if (strcmp(argv
[i
], "-unsafe") == 0) {
2139 scp_unsafe_mode
= 1;
2140 } else if (strcmp(argv
[i
], "--") == 0) {
2144 cmdline_error("unknown option \"%s\"", argv
[i
]);
2154 get_dir_list(argc
, argv
);
2161 targetshouldbedirectory
= 1;
2163 if (colon(argv
[argc
- 1]) != NULL
)
2164 toremote(argc
, argv
);
2166 tolocal(argc
, argv
);
2169 if (back
!= NULL
&& back
->socket(backhandle
) != NULL
) {
2171 back
->special(backhandle
, TS_EOF
);
2172 ssh_scp_recv(&ch
, 1);
2177 gui_send_errcount(list
, errs
);
2179 return (errs
== 0 ?
0 : 1);