1 /* $OpenBSD: scp.c,v 1.170 2010/12/09 14:13:33 jmc Exp $ */
3 * scp - secure remote copy. This is basically patched BSD rcp which
4 * uses ssh to do the data transfer (instead of using rcmd).
6 * NOTE: This version should NOT be suid root. (This uses ssh to
7 * do the transfer and ssh has the necessary privileges.)
9 * 1995 Timo Rinne <tri@iki.fi>, Tatu Ylonen <ylo@cs.hut.fi>
11 * As far as I am concerned, the code I have written for this software
12 * can be used freely for any purpose. Any derived versions of this
13 * software must be clearly marked as such, and if the derived work is
14 * incompatible with the protocol description in the RFC file, it must be
15 * called by a name other than "ssh" or "Secure Shell".
18 * Copyright (c) 1999 Theo de Raadt. All rights reserved.
19 * Copyright (c) 1999 Aaron Campbell. All rights reserved.
21 * Redistribution and use in source and binary forms, with or without
22 * modification, are permitted provided that the following conditions
24 * 1. Redistributions of source code must retain the above copyright
25 * notice, this list of conditions and the following disclaimer.
26 * 2. Redistributions in binary form must reproduce the above copyright
27 * notice, this list of conditions and the following disclaimer in the
28 * documentation and/or other materials provided with the distribution.
30 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
31 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
33 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
34 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
35 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
36 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
39 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45 * Copyright (c) 1983, 1990, 1992, 1993, 1995
46 * The Regents of the University of California. All rights reserved.
48 * Redistribution and use in source and binary forms, with or without
49 * modification, are permitted provided that the following conditions
51 * 1. Redistributions of source code must retain the above copyright
52 * notice, this list of conditions and the following disclaimer.
53 * 2. Redistributions in binary form must reproduce the above copyright
54 * notice, this list of conditions and the following disclaimer in the
55 * documentation and/or other materials provided with the distribution.
56 * 3. Neither the name of the University nor the names of its contributors
57 * may be used to endorse or promote products derived from this software
58 * without specific prior written permission.
60 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
61 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
62 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
63 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
64 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
65 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
66 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
67 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
68 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
69 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
76 #include <sys/types.h>
77 #include <sys/param.h>
78 #ifdef HAVE_SYS_STAT_H
79 # include <sys/stat.h>
84 # ifdef HAVE_SYS_POLL_H
85 # include <sys/poll.h>
88 #ifdef HAVE_SYS_TIME_H
89 # include <sys/time.h>
106 #if defined(HAVE_STRNVIS) && defined(HAVE_VIS_H)
111 #include "atomicio.h"
112 #include "pathnames.h"
115 #include "progressmeter.h"
117 extern char *__progname
;
119 #define COPY_BUFLEN 16384
121 int do_cmd(char *host
, char *remuser
, char *cmd
, int *fdin
, int *fdout
);
122 int do_cmd2(char *host
, char *remuser
, char *cmd
, int fdin
, int fdout
);
124 /* Struct for addargs */
126 arglist remote_remote_args
;
128 /* Bandwidth limit */
129 long long limit_kbps
= 0;
130 struct bwlimit bwlimit
;
132 /* Name of current file being transferred. */
135 /* This is set to non-zero to enable verbose mode. */
136 int verbose_mode
= 0;
138 /* This is set to zero if the progressmeter is not desired. */
139 int showprogress
= 1;
142 * This is set to non-zero if remote-remote copy should be piped
143 * through this process.
145 int throughlocal
= 0;
147 /* This is the program to execute for the secured connection. ("ssh" or -S) */
148 char *ssh_program
= _PATH_SSH_PROGRAM
;
150 /* This is used to store the pid of ssh_program */
151 pid_t do_cmd_pid
= -1;
156 if (do_cmd_pid
> 1) {
157 kill(do_cmd_pid
, signo
? signo
: SIGTERM
);
158 waitpid(do_cmd_pid
, NULL
, 0);
171 if (do_cmd_pid
> 1) {
172 kill(do_cmd_pid
, signo
);
173 while (waitpid(do_cmd_pid
, &status
, WUNTRACED
) == -1 &&
176 kill(getpid(), SIGSTOP
);
181 do_local_cmd(arglist
*a
)
188 fatal("do_local_cmd: no arguments");
191 fprintf(stderr
, "Executing:");
192 for (i
= 0; i
< a
->num
; i
++)
193 fprintf(stderr
, " %s", a
->list
[i
]);
194 fprintf(stderr
, "\n");
196 if ((pid
= fork()) == -1)
197 fatal("do_local_cmd: fork: %s", strerror(errno
));
200 execvp(a
->list
[0], a
->list
);
206 signal(SIGTERM
, killchild
);
207 signal(SIGINT
, killchild
);
208 signal(SIGHUP
, killchild
);
210 while (waitpid(pid
, &status
, 0) == -1)
212 fatal("do_local_cmd: waitpid: %s", strerror(errno
));
216 if (!WIFEXITED(status
) || WEXITSTATUS(status
) != 0)
223 * This function executes the given command as the specified user on the
224 * given host. This returns < 0 if execution fails, and >= 0 otherwise. This
225 * assigns the input and output file descriptors on success.
229 do_cmd(char *host
, char *remuser
, char *cmd
, int *fdin
, int *fdout
)
231 int pin
[2], pout
[2], reserved
[2];
235 "Executing: program %s host %s, user %s, command %s\n",
237 remuser
? remuser
: "(unspecified)", cmd
);
240 * Reserve two descriptors so that the real pipes won't get
241 * descriptors 0 and 1 because that will screw up dup2 below.
243 if (pipe(reserved
) < 0)
244 fatal("pipe: %s", strerror(errno
));
246 /* Create a socket pair for communicating with ssh. */
248 fatal("pipe: %s", strerror(errno
));
250 fatal("pipe: %s", strerror(errno
));
252 /* Free the reserved descriptors. */
256 signal(SIGTSTP
, suspchild
);
257 signal(SIGTTIN
, suspchild
);
258 signal(SIGTTOU
, suspchild
);
260 /* Fork a child to execute the command on the remote host using ssh. */
262 if (do_cmd_pid
== 0) {
271 replacearg(&args
, 0, "%s", ssh_program
);
272 if (remuser
!= NULL
) {
273 addargs(&args
, "-l");
274 addargs(&args
, "%s", remuser
);
276 addargs(&args
, "--");
277 addargs(&args
, "%s", host
);
278 addargs(&args
, "%s", cmd
);
280 execvp(ssh_program
, args
.list
);
283 } else if (do_cmd_pid
== -1) {
284 fatal("fork: %s", strerror(errno
));
286 /* Parent. Close the other side, and return the local side. */
291 signal(SIGTERM
, killchild
);
292 signal(SIGINT
, killchild
);
293 signal(SIGHUP
, killchild
);
298 * This functions executes a command simlar to do_cmd(), but expects the
299 * input and output descriptors to be setup by a previous call to do_cmd().
300 * This way the input and output of two commands can be connected.
303 do_cmd2(char *host
, char *remuser
, char *cmd
, int fdin
, int fdout
)
310 "Executing: 2nd program %s host %s, user %s, command %s\n",
312 remuser
? remuser
: "(unspecified)", cmd
);
314 /* Fork a child to execute the command on the remote host using ssh. */
320 replacearg(&args
, 0, "%s", ssh_program
);
321 if (remuser
!= NULL
) {
322 addargs(&args
, "-l");
323 addargs(&args
, "%s", remuser
);
325 addargs(&args
, "--");
326 addargs(&args
, "%s", host
);
327 addargs(&args
, "%s", cmd
);
329 execvp(ssh_program
, args
.list
);
332 } else if (pid
== -1) {
333 fatal("fork: %s", strerror(errno
));
335 while (waitpid(pid
, &status
, 0) == -1)
337 fatal("do_cmd2: waitpid: %s", strerror(errno
));
346 BUF
*allocbuf(BUF
*, int, int);
349 void run_err(const char *,...);
350 void verifydir(char *);
354 int errs
, remin
, remout
;
355 int pflag
, iamremote
, iamrecursive
, targetshouldbedirectory
;
358 char cmd
[CMDNEEDS
]; /* must hold "rcp -r -p -d\0" */
361 void rsource(char *, struct stat
*);
362 void sink(int, char *[]);
363 void source(int, char *[]);
364 void tolocal(int, char *[]);
365 void toremote(char *, int, char *[]);
369 main(int argc
, char **argv
)
371 int ch
, fflag
, tflag
, status
, n
;
372 char *targ
, **newargv
;
377 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
380 /* Copy argv, because we modify it */
381 newargv
= xcalloc(MAX(argc
+ 1, 1), sizeof(*newargv
));
382 for (n
= 0; n
< argc
; n
++)
383 newargv
[n
] = xstrdup(argv
[n
]);
386 __progname
= ssh_get_progname(argv
[0]);
388 memset(&args
, '\0', sizeof(args
));
389 memset(&remote_remote_args
, '\0', sizeof(remote_remote_args
));
390 args
.list
= remote_remote_args
.list
= NULL
;
391 addargs(&args
, "%s", ssh_program
);
392 addargs(&args
, "-x");
393 addargs(&args
, "-oForwardAgent=no");
394 addargs(&args
, "-oPermitLocalCommand=no");
395 addargs(&args
, "-oClearAllForwardings=yes");
398 while ((ch
= getopt(argc
, argv
, "dfl:prtvBCc:i:P:q12346S:o:F:")) != -1)
400 /* User-visible flags. */
406 addargs(&args
, "-%c", ch
);
407 addargs(&remote_remote_args
, "-%c", ch
);
416 addargs(&remote_remote_args
, "-%c", ch
);
417 addargs(&remote_remote_args
, "%s", optarg
);
418 addargs(&args
, "-%c", ch
);
419 addargs(&args
, "%s", optarg
);
422 addargs(&remote_remote_args
, "-p");
423 addargs(&remote_remote_args
, "%s", optarg
);
424 addargs(&args
, "-p");
425 addargs(&args
, "%s", optarg
);
428 addargs(&remote_remote_args
, "-oBatchmode=yes");
429 addargs(&args
, "-oBatchmode=yes");
432 limit_kbps
= strtonum(optarg
, 1, 100 * 1024 * 1024,
436 limit_kbps
*= 1024; /* kbps */
437 bandwidth_limit_init(&bwlimit
, limit_kbps
, COPY_BUFLEN
);
446 ssh_program
= xstrdup(optarg
);
449 addargs(&args
, "-v");
450 addargs(&remote_remote_args
, "-v");
454 addargs(&args
, "-q");
455 addargs(&remote_remote_args
, "-q");
459 /* Server options. */
461 targetshouldbedirectory
= 1;
463 case 'f': /* "from" */
471 setmode(0, O_BINARY
);
480 if ((pwd
= getpwuid(userid
= getuid())) == NULL
)
481 fatal("unknown user %u", (u_int
) userid
);
483 if (!isatty(STDOUT_FILENO
))
486 remin
= STDIN_FILENO
;
487 remout
= STDOUT_FILENO
;
490 /* Follow "protocol", send data. */
503 targetshouldbedirectory
= 1;
507 /* Command to be executed on remote system using "ssh". */
508 (void) snprintf(cmd
, sizeof cmd
, "scp%s%s%s%s",
509 verbose_mode
? " -v" : "",
510 iamrecursive
? " -r" : "", pflag
? " -p" : "",
511 targetshouldbedirectory
? " -d" : "");
513 (void) signal(SIGPIPE
, lostconn
);
515 if ((targ
= colon(argv
[argc
- 1]))) /* Dest is remote host. */
516 toremote(targ
, argc
, argv
);
518 if (targetshouldbedirectory
)
519 verifydir(argv
[argc
- 1]);
520 tolocal(argc
, argv
); /* Dest is local host. */
523 * Finally check the exit status of the ssh process, if one was forked
524 * and no error has occurred yet
526 if (do_cmd_pid
!= -1 && errs
== 0) {
530 (void) close(remout
);
531 if (waitpid(do_cmd_pid
, &status
, 0) == -1)
534 if (!WIFEXITED(status
) || WEXITSTATUS(status
) != 0)
541 /* Callback from atomicio6 to update progress meter and limit bandwidth */
543 scpio(void *_cnt
, size_t s
)
545 off_t
*cnt
= (off_t
*)_cnt
;
549 bandwidth_limit(&bwlimit
, s
);
554 toremote(char *targ
, int argc
, char **argv
)
556 char *bp
, *host
, *src
, *suser
, *thost
, *tuser
, *arg
;
561 memset(&alist
, '\0', sizeof(alist
));
568 arg
= xstrdup(argv
[argc
- 1]);
569 if ((thost
= strrchr(arg
, '@'))) {
580 if (tuser
!= NULL
&& !okname(tuser
)) {
585 for (i
= 0; i
< argc
- 1; i
++) {
586 src
= colon(argv
[i
]);
587 if (src
&& throughlocal
) { /* extended remote to remote */
591 host
= strrchr(argv
[i
], '@');
594 host
= cleanhostname(host
);
597 suser
= pwd
->pw_name
;
598 else if (!okname(suser
))
601 host
= cleanhostname(argv
[i
]);
604 xasprintf(&bp
, "%s -f -- %s", cmd
, src
);
605 if (do_cmd(host
, suser
, bp
, &remin
, &remout
) < 0)
608 host
= cleanhostname(thost
);
609 xasprintf(&bp
, "%s -t -- %s", cmd
, targ
);
610 if (do_cmd2(host
, tuser
, bp
, remin
, remout
) < 0)
614 (void) close(remout
);
616 } else if (src
) { /* standard remote to remote */
618 addargs(&alist
, "%s", ssh_program
);
619 addargs(&alist
, "-x");
620 addargs(&alist
, "-oClearAllForwardings=yes");
621 addargs(&alist
, "-n");
622 for (j
= 0; j
< remote_remote_args
.num
; j
++) {
623 addargs(&alist
, "%s",
624 remote_remote_args
.list
[j
]);
629 host
= strrchr(argv
[i
], '@');
633 host
= cleanhostname(host
);
636 suser
= pwd
->pw_name
;
637 else if (!okname(suser
))
639 addargs(&alist
, "-l");
640 addargs(&alist
, "%s", suser
);
642 host
= cleanhostname(argv
[i
]);
644 addargs(&alist
, "--");
645 addargs(&alist
, "%s", host
);
646 addargs(&alist
, "%s", cmd
);
647 addargs(&alist
, "%s", src
);
648 addargs(&alist
, "%s%s%s:%s",
649 tuser
? tuser
: "", tuser
? "@" : "",
651 if (do_local_cmd(&alist
) != 0)
653 } else { /* local to remote */
655 xasprintf(&bp
, "%s -t -- %s", cmd
, targ
);
656 host
= cleanhostname(thost
);
657 if (do_cmd(host
, tuser
, bp
, &remin
,
671 tolocal(int argc
, char **argv
)
673 char *bp
, *host
, *src
, *suser
;
677 memset(&alist
, '\0', sizeof(alist
));
680 for (i
= 0; i
< argc
- 1; i
++) {
681 if (!(src
= colon(argv
[i
]))) { /* Local to local. */
683 addargs(&alist
, "%s", _PATH_CP
);
685 addargs(&alist
, "-r");
687 addargs(&alist
, "-p");
688 addargs(&alist
, "--");
689 addargs(&alist
, "%s", argv
[i
]);
690 addargs(&alist
, "%s", argv
[argc
-1]);
691 if (do_local_cmd(&alist
))
698 if ((host
= strrchr(argv
[i
], '@')) == NULL
) {
705 suser
= pwd
->pw_name
;
707 host
= cleanhostname(host
);
708 xasprintf(&bp
, "%s -f -- %s", cmd
, src
);
709 if (do_cmd(host
, suser
, bp
, &remin
, &remout
) < 0) {
715 sink(1, argv
+ argc
- 1);
722 source(int argc
, char **argv
)
729 int fd
= -1, haderr
, indx
;
730 char *last
, *name
, buf
[2048], encname
[MAXPATHLEN
];
733 for (indx
= 0; indx
< argc
; ++indx
) {
737 while (len
> 1 && name
[len
-1] == '/')
739 if ((fd
= open(name
, O_RDONLY
|O_NONBLOCK
, 0)) < 0)
741 if (strchr(name
, '\n') != NULL
) {
742 strnvis(encname
, name
, sizeof(encname
), VIS_NL
);
745 if (fstat(fd
, &stb
) < 0) {
746 syserr
: run_err("%s: %s", name
, strerror(errno
));
749 if (stb
.st_size
< 0) {
750 run_err("%s: %s", name
, "Negative file size");
754 switch (stb
.st_mode
& S_IFMT
) {
764 run_err("%s: not a regular file", name
);
767 if ((last
= strrchr(name
, '/')) == NULL
)
774 * Make it compatible with possible future
775 * versions expecting microseconds.
777 (void) snprintf(buf
, sizeof buf
, "T%lu 0 %lu 0\n",
778 (u_long
) (stb
.st_mtime
< 0 ? 0 : stb
.st_mtime
),
779 (u_long
) (stb
.st_atime
< 0 ? 0 : stb
.st_atime
));
781 fprintf(stderr
, "File mtime %ld atime %ld\n",
782 (long)stb
.st_mtime
, (long)stb
.st_atime
);
783 fprintf(stderr
, "Sending file timestamps: %s",
786 (void) atomicio(vwrite
, remout
, buf
, strlen(buf
));
790 #define FILEMODEMASK (S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO)
791 snprintf(buf
, sizeof buf
, "C%04o %lld %s\n",
792 (u_int
) (stb
.st_mode
& FILEMODEMASK
),
793 (long long)stb
.st_size
, last
);
795 fprintf(stderr
, "Sending file modes: %s", buf
);
797 (void) atomicio(vwrite
, remout
, buf
, strlen(buf
));
800 if ((bp
= allocbuf(&buffer
, fd
, COPY_BUFLEN
)) == NULL
) {
801 next
: if (fd
!= -1) {
808 start_progress_meter(curfile
, stb
.st_size
, &statbytes
);
809 set_nonblock(remout
);
810 for (haderr
= i
= 0; i
< stb
.st_size
; i
+= bp
->cnt
) {
812 if (i
+ (off_t
)amt
> stb
.st_size
)
813 amt
= stb
.st_size
- i
;
815 if (atomicio(read
, fd
, bp
->buf
, amt
) != amt
)
818 /* Keep writing after error to retain sync */
820 (void)atomicio(vwrite
, remout
, bp
->buf
, amt
);
823 if (atomicio6(vwrite
, remout
, bp
->buf
, amt
, scpio
,
827 unset_nonblock(remout
);
829 stop_progress_meter();
832 if (close(fd
) < 0 && !haderr
)
837 (void) atomicio(vwrite
, remout
, "", 1);
839 run_err("%s: %s", name
, strerror(haderr
));
845 rsource(char *name
, struct stat
*statp
)
849 char *last
, *vect
[1], path
[1100];
851 if (!(dirp
= opendir(name
))) {
852 run_err("%s: %s", name
, strerror(errno
));
855 last
= strrchr(name
, '/');
861 (void) snprintf(path
, sizeof(path
), "T%lu 0 %lu 0\n",
862 (u_long
) statp
->st_mtime
,
863 (u_long
) statp
->st_atime
);
864 (void) atomicio(vwrite
, remout
, path
, strlen(path
));
865 if (response() < 0) {
870 (void) snprintf(path
, sizeof path
, "D%04o %d %.1024s\n",
871 (u_int
) (statp
->st_mode
& FILEMODEMASK
), 0, last
);
873 fprintf(stderr
, "Entering directory: %s", path
);
874 (void) atomicio(vwrite
, remout
, path
, strlen(path
));
875 if (response() < 0) {
879 while ((dp
= readdir(dirp
)) != NULL
) {
882 if (!strcmp(dp
->d_name
, ".") || !strcmp(dp
->d_name
, ".."))
884 if (strlen(name
) + 1 + strlen(dp
->d_name
) >= sizeof(path
) - 1) {
885 run_err("%s/%s: name too long", name
, dp
->d_name
);
888 (void) snprintf(path
, sizeof path
, "%s/%s", name
, dp
->d_name
);
892 (void) closedir(dirp
);
893 (void) atomicio(vwrite
, remout
, "E\n", 2);
898 sink(int argc
, char **argv
)
908 int amt
, exists
, first
, ofd
;
909 mode_t mode
, omode
, mask
;
910 off_t size
, statbytes
;
911 int setimes
, targisdir
, wrerrno
= 0;
912 char ch
, *cp
, *np
, *targ
, *why
, *vect
[1], buf
[2048];
913 struct timeval tv
[2];
917 #define SCREWUP(str) { why = str; goto screwup; }
919 setimes
= targisdir
= 0;
924 run_err("ambiguous target");
928 if (targetshouldbedirectory
)
931 (void) atomicio(vwrite
, remout
, "", 1);
932 if (stat(targ
, &stb
) == 0 && S_ISDIR(stb
.st_mode
))
934 for (first
= 1;; first
= 0) {
936 if (atomicio(read
, remin
, cp
, 1) != 1)
939 SCREWUP("unexpected <newline>");
941 if (atomicio(read
, remin
, &ch
, sizeof(ch
)) != sizeof(ch
))
942 SCREWUP("lost connection");
944 } while (cp
< &buf
[sizeof(buf
) - 1] && ch
!= '\n');
947 fprintf(stderr
, "Sink: %s", buf
);
949 if (buf
[0] == '\01' || buf
[0] == '\02') {
951 (void) atomicio(vwrite
, STDERR_FILENO
,
952 buf
+ 1, strlen(buf
+ 1));
959 (void) atomicio(vwrite
, remout
, "", 1);
969 mtime
.tv_sec
= strtol(cp
, &cp
, 10);
970 if (!cp
|| *cp
++ != ' ')
971 SCREWUP("mtime.sec not delimited");
972 mtime
.tv_usec
= strtol(cp
, &cp
, 10);
973 if (!cp
|| *cp
++ != ' ')
974 SCREWUP("mtime.usec not delimited");
975 atime
.tv_sec
= strtol(cp
, &cp
, 10);
976 if (!cp
|| *cp
++ != ' ')
977 SCREWUP("atime.sec not delimited");
978 atime
.tv_usec
= strtol(cp
, &cp
, 10);
979 if (!cp
|| *cp
++ != '\0')
980 SCREWUP("atime.usec not delimited");
981 (void) atomicio(vwrite
, remout
, "", 1);
984 if (*cp
!= 'C' && *cp
!= 'D') {
986 * Check for the case "rcp remote:foo\* local:bar".
987 * In this case, the line "No match." can be returned
988 * by the shell before the rcp command on the remote is
989 * executed so the ^Aerror_message convention isn't
996 SCREWUP("expected control record");
999 for (++cp
; cp
< buf
+ 5; cp
++) {
1000 if (*cp
< '0' || *cp
> '7')
1001 SCREWUP("bad mode");
1002 mode
= (mode
<< 3) | (*cp
- '0');
1005 SCREWUP("mode not delimited");
1007 for (size
= 0; isdigit(*cp
);)
1008 size
= size
* 10 + (*cp
++ - '0');
1010 SCREWUP("size not delimited");
1011 if ((strchr(cp
, '/') != NULL
) || (strcmp(cp
, "..") == 0)) {
1012 run_err("error: unexpected filename: %s", cp
);
1016 static char *namebuf
;
1017 static size_t cursize
;
1020 need
= strlen(targ
) + strlen(cp
) + 250;
1021 if (need
> cursize
) {
1024 namebuf
= xmalloc(need
);
1027 (void) snprintf(namebuf
, need
, "%s%s%s", targ
,
1028 strcmp(targ
, "/") ? "/" : "", cp
);
1033 exists
= stat(np
, &stb
) == 0;
1034 if (buf
[0] == 'D') {
1035 int mod_flag
= pflag
;
1037 SCREWUP("received directory without -r");
1039 if (!S_ISDIR(stb
.st_mode
)) {
1044 (void) chmod(np
, mode
);
1046 /* Handle copying from a read-only
1049 if (mkdir(np
, mode
| S_IRWXU
) < 0)
1052 vect
[0] = xstrdup(np
);
1056 if (utimes(vect
[0], tv
) < 0)
1057 run_err("%s: set times: %s",
1058 vect
[0], strerror(errno
));
1061 (void) chmod(vect
[0], mode
);
1068 if ((ofd
= open(np
, O_WRONLY
|O_CREAT
, mode
)) < 0) {
1069 bad
: run_err("%s: %s", np
, strerror(errno
));
1072 (void) atomicio(vwrite
, remout
, "", 1);
1073 if ((bp
= allocbuf(&buffer
, ofd
, COPY_BUFLEN
)) == NULL
) {
1082 start_progress_meter(curfile
, size
, &statbytes
);
1083 set_nonblock(remin
);
1084 for (count
= i
= 0; i
< size
; i
+= bp
->cnt
) {
1090 j
= atomicio6(read
, remin
, cp
, amt
,
1093 run_err("%s", j
!= EPIPE
?
1095 "dropped connection");
1102 if (count
== bp
->cnt
) {
1103 /* Keep reading so we stay sync'd up. */
1105 if (atomicio(vwrite
, ofd
, bp
->buf
,
1115 unset_nonblock(remin
);
1117 stop_progress_meter();
1118 if (count
!= 0 && wrerr
== NO
&&
1119 atomicio(vwrite
, ofd
, bp
->buf
, count
) != count
) {
1123 if (wrerr
== NO
&& (!exists
|| S_ISREG(stb
.st_mode
)) &&
1124 ftruncate(ofd
, size
) != 0) {
1125 run_err("%s: truncate: %s", np
, strerror(errno
));
1129 if (exists
|| omode
!= mode
)
1131 if (fchmod(ofd
, omode
)) {
1132 #else /* HAVE_FCHMOD */
1133 if (chmod(np
, omode
)) {
1134 #endif /* HAVE_FCHMOD */
1135 run_err("%s: set mode: %s",
1136 np
, strerror(errno
));
1140 if (!exists
&& omode
!= mode
)
1142 if (fchmod(ofd
, omode
& ~mask
)) {
1143 #else /* HAVE_FCHMOD */
1144 if (chmod(np
, omode
& ~mask
)) {
1145 #endif /* HAVE_FCHMOD */
1146 run_err("%s: set mode: %s",
1147 np
, strerror(errno
));
1151 if (close(ofd
) == -1) {
1156 if (setimes
&& wrerr
== NO
) {
1158 if (utimes(np
, tv
) < 0) {
1159 run_err("%s: set times: %s",
1160 np
, strerror(errno
));
1166 run_err("%s: %s", np
, strerror(wrerrno
));
1169 (void) atomicio(vwrite
, remout
, "", 1);
1176 run_err("protocol error: %s", why
);
1183 char ch
, *cp
, resp
, rbuf
[2048];
1185 if (atomicio(read
, remin
, &resp
, sizeof(resp
)) != sizeof(resp
))
1195 case 1: /* error, followed by error msg */
1196 case 2: /* fatal error, "" */
1198 if (atomicio(read
, remin
, &ch
, sizeof(ch
)) != sizeof(ch
))
1201 } while (cp
< &rbuf
[sizeof(rbuf
) - 1] && ch
!= '\n');
1204 (void) atomicio(vwrite
, STDERR_FILENO
, rbuf
, cp
- rbuf
);
1216 (void) fprintf(stderr
,
1217 "usage: scp [-12346BCpqrv] [-c cipher] [-F ssh_config] [-i identity_file]\n"
1218 " [-l limit] [-o ssh_option] [-P port] [-S program]\n"
1219 " [[user@]host1:]file1 ... [[user@]host2:]file2\n");
1224 run_err(const char *fmt
,...)
1230 if (fp
!= NULL
|| (remout
!= -1 && (fp
= fdopen(remout
, "w")))) {
1231 (void) fprintf(fp
, "%c", 0x01);
1232 (void) fprintf(fp
, "scp: ");
1234 (void) vfprintf(fp
, fmt
, ap
);
1236 (void) fprintf(fp
, "\n");
1242 vfprintf(stderr
, fmt
, ap
);
1244 fprintf(stderr
, "\n");
1253 if (!stat(cp
, &stb
)) {
1254 if (S_ISDIR(stb
.st_mode
))
1258 run_err("%s: %s", cp
, strerror(errno
));
1273 if (!isalpha(c
) && !isdigit(c
)) {
1288 bad
: fprintf(stderr
, "%s: invalid user name\n", cp0
);
1293 allocbuf(BUF
*bp
, int fd
, int blksize
)
1296 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
1299 if (fstat(fd
, &stb
) < 0) {
1300 run_err("fstat: %s", strerror(errno
));
1303 size
= roundup(stb
.st_blksize
, blksize
);
1306 #else /* HAVE_STRUCT_STAT_ST_BLKSIZE */
1308 #endif /* HAVE_STRUCT_STAT_ST_BLKSIZE */
1309 if (bp
->cnt
>= size
)
1311 if (bp
->buf
== NULL
)
1312 bp
->buf
= xmalloc(size
);
1314 bp
->buf
= xrealloc(bp
->buf
, 1, size
);
1315 memset(bp
->buf
, 0, size
);
1324 write(STDERR_FILENO
, "lost connection\n", 16);