1 /* $OpenBSD: scp.c,v 1.167 2010/09/22 22:58:51 djm 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
);
123 /* Struct for addargs */
126 /* Bandwidth limit */
127 long long limit_kbps
= 0;
128 struct bwlimit bwlimit
;
130 /* Name of current file being transferred. */
133 /* This is set to non-zero to enable verbose mode. */
134 int verbose_mode
= 0;
136 /* This is set to zero if the progressmeter is not desired. */
137 int showprogress
= 1;
139 /* This is the program to execute for the secured connection. ("ssh" or -S) */
140 char *ssh_program
= _PATH_SSH_PROGRAM
;
142 /* This is used to store the pid of ssh_program */
143 pid_t do_cmd_pid
= -1;
148 if (do_cmd_pid
> 1) {
149 kill(do_cmd_pid
, signo
? signo
: SIGTERM
);
150 waitpid(do_cmd_pid
, NULL
, 0);
163 if (do_cmd_pid
> 1) {
164 kill(do_cmd_pid
, signo
);
165 while (waitpid(do_cmd_pid
, &status
, WUNTRACED
) == -1 &&
168 kill(getpid(), SIGSTOP
);
173 do_local_cmd(arglist
*a
)
180 fatal("do_local_cmd: no arguments");
183 fprintf(stderr
, "Executing:");
184 for (i
= 0; i
< a
->num
; i
++)
185 fprintf(stderr
, " %s", a
->list
[i
]);
186 fprintf(stderr
, "\n");
188 if ((pid
= fork()) == -1)
189 fatal("do_local_cmd: fork: %s", strerror(errno
));
192 execvp(a
->list
[0], a
->list
);
198 signal(SIGTERM
, killchild
);
199 signal(SIGINT
, killchild
);
200 signal(SIGHUP
, killchild
);
202 while (waitpid(pid
, &status
, 0) == -1)
204 fatal("do_local_cmd: waitpid: %s", strerror(errno
));
208 if (!WIFEXITED(status
) || WEXITSTATUS(status
) != 0)
215 * This function executes the given command as the specified user on the
216 * given host. This returns < 0 if execution fails, and >= 0 otherwise. This
217 * assigns the input and output file descriptors on success.
221 do_cmd(char *host
, char *remuser
, char *cmd
, int *fdin
, int *fdout
)
223 int pin
[2], pout
[2], reserved
[2];
227 "Executing: program %s host %s, user %s, command %s\n",
229 remuser
? remuser
: "(unspecified)", cmd
);
232 * Reserve two descriptors so that the real pipes won't get
233 * descriptors 0 and 1 because that will screw up dup2 below.
235 if (pipe(reserved
) < 0)
236 fatal("pipe: %s", strerror(errno
));
238 /* Create a socket pair for communicating with ssh. */
240 fatal("pipe: %s", strerror(errno
));
242 fatal("pipe: %s", strerror(errno
));
244 /* Free the reserved descriptors. */
248 signal(SIGTSTP
, suspchild
);
249 signal(SIGTTIN
, suspchild
);
250 signal(SIGTTOU
, suspchild
);
252 /* Fork a child to execute the command on the remote host using ssh. */
254 if (do_cmd_pid
== 0) {
263 replacearg(&args
, 0, "%s", ssh_program
);
264 if (remuser
!= NULL
) {
265 addargs(&args
, "-l");
266 addargs(&args
, "%s", remuser
);
268 addargs(&args
, "--");
269 addargs(&args
, "%s", host
);
270 addargs(&args
, "%s", cmd
);
272 execvp(ssh_program
, args
.list
);
275 } else if (do_cmd_pid
== -1) {
276 fatal("fork: %s", strerror(errno
));
278 /* Parent. Close the other side, and return the local side. */
283 signal(SIGTERM
, killchild
);
284 signal(SIGINT
, killchild
);
285 signal(SIGHUP
, killchild
);
294 BUF
*allocbuf(BUF
*, int, int);
297 void run_err(const char *,...);
298 void verifydir(char *);
302 int errs
, remin
, remout
;
303 int pflag
, iamremote
, iamrecursive
, targetshouldbedirectory
;
306 char cmd
[CMDNEEDS
]; /* must hold "rcp -r -p -d\0" */
309 void rsource(char *, struct stat
*);
310 void sink(int, char *[]);
311 void source(int, char *[]);
312 void tolocal(int, char *[]);
313 void toremote(char *, int, char *[]);
317 main(int argc
, char **argv
)
319 int ch
, fflag
, tflag
, status
, n
;
320 char *targ
, **newargv
;
325 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
328 /* Copy argv, because we modify it */
329 newargv
= xcalloc(MAX(argc
+ 1, 1), sizeof(*newargv
));
330 for (n
= 0; n
< argc
; n
++)
331 newargv
[n
] = xstrdup(argv
[n
]);
334 __progname
= ssh_get_progname(argv
[0]);
336 memset(&args
, '\0', sizeof(args
));
338 addargs(&args
, "%s", ssh_program
);
339 addargs(&args
, "-x");
340 addargs(&args
, "-oForwardAgent no");
341 addargs(&args
, "-oPermitLocalCommand no");
342 addargs(&args
, "-oClearAllForwardings yes");
345 while ((ch
= getopt(argc
, argv
, "dfl:prtvBCc:i:P:q1246S:o:F:")) != -1)
347 /* User-visible flags. */
353 addargs(&args
, "-%c", ch
);
359 addargs(&args
, "-%c", ch
);
360 addargs(&args
, "%s", optarg
);
363 addargs(&args
, "-p");
364 addargs(&args
, "%s", optarg
);
367 addargs(&args
, "-oBatchmode yes");
370 limit_kbps
= strtonum(optarg
, 1, 100 * 1024 * 1024,
374 limit_kbps
*= 1024; /* kbps */
375 bandwidth_limit_init(&bwlimit
, limit_kbps
, COPY_BUFLEN
);
384 ssh_program
= xstrdup(optarg
);
387 addargs(&args
, "-v");
391 addargs(&args
, "-q");
395 /* Server options. */
397 targetshouldbedirectory
= 1;
399 case 'f': /* "from" */
407 setmode(0, O_BINARY
);
416 if ((pwd
= getpwuid(userid
= getuid())) == NULL
)
417 fatal("unknown user %u", (u_int
) userid
);
419 if (!isatty(STDOUT_FILENO
))
422 remin
= STDIN_FILENO
;
423 remout
= STDOUT_FILENO
;
426 /* Follow "protocol", send data. */
439 targetshouldbedirectory
= 1;
443 /* Command to be executed on remote system using "ssh". */
444 (void) snprintf(cmd
, sizeof cmd
, "scp%s%s%s%s",
445 verbose_mode
? " -v" : "",
446 iamrecursive
? " -r" : "", pflag
? " -p" : "",
447 targetshouldbedirectory
? " -d" : "");
449 (void) signal(SIGPIPE
, lostconn
);
451 if ((targ
= colon(argv
[argc
- 1]))) /* Dest is remote host. */
452 toremote(targ
, argc
, argv
);
454 if (targetshouldbedirectory
)
455 verifydir(argv
[argc
- 1]);
456 tolocal(argc
, argv
); /* Dest is local host. */
459 * Finally check the exit status of the ssh process, if one was forked
460 * and no error has occurred yet
462 if (do_cmd_pid
!= -1 && errs
== 0) {
466 (void) close(remout
);
467 if (waitpid(do_cmd_pid
, &status
, 0) == -1)
470 if (!WIFEXITED(status
) || WEXITSTATUS(status
) != 0)
477 /* Callback from atomicio6 to update progress meter and limit bandwidth */
479 scpio(void *_cnt
, size_t s
)
481 off_t
*cnt
= (off_t
*)_cnt
;
485 bandwidth_limit(&bwlimit
, s
);
490 toremote(char *targ
, int argc
, char **argv
)
492 char *bp
, *host
, *src
, *suser
, *thost
, *tuser
, *arg
;
496 memset(&alist
, '\0', sizeof(alist
));
503 arg
= xstrdup(argv
[argc
- 1]);
504 if ((thost
= strrchr(arg
, '@'))) {
515 if (tuser
!= NULL
&& !okname(tuser
)) {
520 for (i
= 0; i
< argc
- 1; i
++) {
521 src
= colon(argv
[i
]);
522 if (src
) { /* remote to remote */
524 addargs(&alist
, "%s", ssh_program
);
526 addargs(&alist
, "-v");
527 addargs(&alist
, "-x");
528 addargs(&alist
, "-oClearAllForwardings yes");
529 addargs(&alist
, "-n");
534 host
= strrchr(argv
[i
], '@');
538 host
= cleanhostname(host
);
541 suser
= pwd
->pw_name
;
542 else if (!okname(suser
))
544 addargs(&alist
, "-l");
545 addargs(&alist
, "%s", suser
);
547 host
= cleanhostname(argv
[i
]);
549 addargs(&alist
, "--");
550 addargs(&alist
, "%s", host
);
551 addargs(&alist
, "%s", cmd
);
552 addargs(&alist
, "%s", src
);
553 addargs(&alist
, "%s%s%s:%s",
554 tuser
? tuser
: "", tuser
? "@" : "",
556 if (do_local_cmd(&alist
) != 0)
558 } else { /* local to remote */
560 xasprintf(&bp
, "%s -t -- %s", cmd
, targ
);
561 host
= cleanhostname(thost
);
562 if (do_cmd(host
, tuser
, bp
, &remin
,
576 tolocal(int argc
, char **argv
)
578 char *bp
, *host
, *src
, *suser
;
582 memset(&alist
, '\0', sizeof(alist
));
585 for (i
= 0; i
< argc
- 1; i
++) {
586 if (!(src
= colon(argv
[i
]))) { /* Local to local. */
588 addargs(&alist
, "%s", _PATH_CP
);
590 addargs(&alist
, "-r");
592 addargs(&alist
, "-p");
593 addargs(&alist
, "--");
594 addargs(&alist
, "%s", argv
[i
]);
595 addargs(&alist
, "%s", argv
[argc
-1]);
596 if (do_local_cmd(&alist
))
603 if ((host
= strrchr(argv
[i
], '@')) == NULL
) {
610 suser
= pwd
->pw_name
;
612 host
= cleanhostname(host
);
613 xasprintf(&bp
, "%s -f -- %s", cmd
, src
);
614 if (do_cmd(host
, suser
, bp
, &remin
, &remout
) < 0) {
620 sink(1, argv
+ argc
- 1);
627 source(int argc
, char **argv
)
634 int fd
= -1, haderr
, indx
;
635 char *last
, *name
, buf
[2048], encname
[MAXPATHLEN
];
638 for (indx
= 0; indx
< argc
; ++indx
) {
642 while (len
> 1 && name
[len
-1] == '/')
644 if ((fd
= open(name
, O_RDONLY
|O_NONBLOCK
, 0)) < 0)
646 if (strchr(name
, '\n') != NULL
) {
647 strnvis(encname
, name
, sizeof(encname
), VIS_NL
);
650 if (fstat(fd
, &stb
) < 0) {
651 syserr
: run_err("%s: %s", name
, strerror(errno
));
654 if (stb
.st_size
< 0) {
655 run_err("%s: %s", name
, "Negative file size");
659 switch (stb
.st_mode
& S_IFMT
) {
669 run_err("%s: not a regular file", name
);
672 if ((last
= strrchr(name
, '/')) == NULL
)
679 * Make it compatible with possible future
680 * versions expecting microseconds.
682 (void) snprintf(buf
, sizeof buf
, "T%lu 0 %lu 0\n",
683 (u_long
) (stb
.st_mtime
< 0 ? 0 : stb
.st_mtime
),
684 (u_long
) (stb
.st_atime
< 0 ? 0 : stb
.st_atime
));
686 fprintf(stderr
, "File mtime %ld atime %ld\n",
687 (long)stb
.st_mtime
, (long)stb
.st_atime
);
688 fprintf(stderr
, "Sending file timestamps: %s",
691 (void) atomicio(vwrite
, remout
, buf
, strlen(buf
));
695 #define FILEMODEMASK (S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO)
696 snprintf(buf
, sizeof buf
, "C%04o %lld %s\n",
697 (u_int
) (stb
.st_mode
& FILEMODEMASK
),
698 (long long)stb
.st_size
, last
);
700 fprintf(stderr
, "Sending file modes: %s", buf
);
702 (void) atomicio(vwrite
, remout
, buf
, strlen(buf
));
705 if ((bp
= allocbuf(&buffer
, fd
, COPY_BUFLEN
)) == NULL
) {
706 next
: if (fd
!= -1) {
713 start_progress_meter(curfile
, stb
.st_size
, &statbytes
);
714 set_nonblock(remout
);
715 for (haderr
= i
= 0; i
< stb
.st_size
; i
+= bp
->cnt
) {
717 if (i
+ (off_t
)amt
> stb
.st_size
)
718 amt
= stb
.st_size
- i
;
720 if (atomicio(read
, fd
, bp
->buf
, amt
) != amt
)
723 /* Keep writing after error to retain sync */
725 (void)atomicio(vwrite
, remout
, bp
->buf
, amt
);
728 if (atomicio6(vwrite
, remout
, bp
->buf
, amt
, scpio
,
732 unset_nonblock(remout
);
734 stop_progress_meter();
737 if (close(fd
) < 0 && !haderr
)
742 (void) atomicio(vwrite
, remout
, "", 1);
744 run_err("%s: %s", name
, strerror(haderr
));
750 rsource(char *name
, struct stat
*statp
)
754 char *last
, *vect
[1], path
[1100];
756 if (!(dirp
= opendir(name
))) {
757 run_err("%s: %s", name
, strerror(errno
));
760 last
= strrchr(name
, '/');
766 (void) snprintf(path
, sizeof(path
), "T%lu 0 %lu 0\n",
767 (u_long
) statp
->st_mtime
,
768 (u_long
) statp
->st_atime
);
769 (void) atomicio(vwrite
, remout
, path
, strlen(path
));
770 if (response() < 0) {
775 (void) snprintf(path
, sizeof path
, "D%04o %d %.1024s\n",
776 (u_int
) (statp
->st_mode
& FILEMODEMASK
), 0, last
);
778 fprintf(stderr
, "Entering directory: %s", path
);
779 (void) atomicio(vwrite
, remout
, path
, strlen(path
));
780 if (response() < 0) {
784 while ((dp
= readdir(dirp
)) != NULL
) {
787 if (!strcmp(dp
->d_name
, ".") || !strcmp(dp
->d_name
, ".."))
789 if (strlen(name
) + 1 + strlen(dp
->d_name
) >= sizeof(path
) - 1) {
790 run_err("%s/%s: name too long", name
, dp
->d_name
);
793 (void) snprintf(path
, sizeof path
, "%s/%s", name
, dp
->d_name
);
797 (void) closedir(dirp
);
798 (void) atomicio(vwrite
, remout
, "E\n", 2);
803 sink(int argc
, char **argv
)
813 int amt
, exists
, first
, ofd
;
814 mode_t mode
, omode
, mask
;
815 off_t size
, statbytes
;
816 int setimes
, targisdir
, wrerrno
= 0;
817 char ch
, *cp
, *np
, *targ
, *why
, *vect
[1], buf
[2048];
818 struct timeval tv
[2];
822 #define SCREWUP(str) { why = str; goto screwup; }
824 setimes
= targisdir
= 0;
829 run_err("ambiguous target");
833 if (targetshouldbedirectory
)
836 (void) atomicio(vwrite
, remout
, "", 1);
837 if (stat(targ
, &stb
) == 0 && S_ISDIR(stb
.st_mode
))
839 for (first
= 1;; first
= 0) {
841 if (atomicio(read
, remin
, cp
, 1) != 1)
844 SCREWUP("unexpected <newline>");
846 if (atomicio(read
, remin
, &ch
, sizeof(ch
)) != sizeof(ch
))
847 SCREWUP("lost connection");
849 } while (cp
< &buf
[sizeof(buf
) - 1] && ch
!= '\n');
852 fprintf(stderr
, "Sink: %s", buf
);
854 if (buf
[0] == '\01' || buf
[0] == '\02') {
856 (void) atomicio(vwrite
, STDERR_FILENO
,
857 buf
+ 1, strlen(buf
+ 1));
864 (void) atomicio(vwrite
, remout
, "", 1);
874 mtime
.tv_sec
= strtol(cp
, &cp
, 10);
875 if (!cp
|| *cp
++ != ' ')
876 SCREWUP("mtime.sec not delimited");
877 mtime
.tv_usec
= strtol(cp
, &cp
, 10);
878 if (!cp
|| *cp
++ != ' ')
879 SCREWUP("mtime.usec not delimited");
880 atime
.tv_sec
= strtol(cp
, &cp
, 10);
881 if (!cp
|| *cp
++ != ' ')
882 SCREWUP("atime.sec not delimited");
883 atime
.tv_usec
= strtol(cp
, &cp
, 10);
884 if (!cp
|| *cp
++ != '\0')
885 SCREWUP("atime.usec not delimited");
886 (void) atomicio(vwrite
, remout
, "", 1);
889 if (*cp
!= 'C' && *cp
!= 'D') {
891 * Check for the case "rcp remote:foo\* local:bar".
892 * In this case, the line "No match." can be returned
893 * by the shell before the rcp command on the remote is
894 * executed so the ^Aerror_message convention isn't
901 SCREWUP("expected control record");
904 for (++cp
; cp
< buf
+ 5; cp
++) {
905 if (*cp
< '0' || *cp
> '7')
907 mode
= (mode
<< 3) | (*cp
- '0');
910 SCREWUP("mode not delimited");
912 for (size
= 0; isdigit(*cp
);)
913 size
= size
* 10 + (*cp
++ - '0');
915 SCREWUP("size not delimited");
916 if ((strchr(cp
, '/') != NULL
) || (strcmp(cp
, "..") == 0)) {
917 run_err("error: unexpected filename: %s", cp
);
921 static char *namebuf
;
922 static size_t cursize
;
925 need
= strlen(targ
) + strlen(cp
) + 250;
926 if (need
> cursize
) {
929 namebuf
= xmalloc(need
);
932 (void) snprintf(namebuf
, need
, "%s%s%s", targ
,
933 strcmp(targ
, "/") ? "/" : "", cp
);
938 exists
= stat(np
, &stb
) == 0;
940 int mod_flag
= pflag
;
942 SCREWUP("received directory without -r");
944 if (!S_ISDIR(stb
.st_mode
)) {
949 (void) chmod(np
, mode
);
951 /* Handle copying from a read-only
954 if (mkdir(np
, mode
| S_IRWXU
) < 0)
957 vect
[0] = xstrdup(np
);
961 if (utimes(vect
[0], tv
) < 0)
962 run_err("%s: set times: %s",
963 vect
[0], strerror(errno
));
966 (void) chmod(vect
[0], mode
);
973 if ((ofd
= open(np
, O_WRONLY
|O_CREAT
, mode
)) < 0) {
974 bad
: run_err("%s: %s", np
, strerror(errno
));
977 (void) atomicio(vwrite
, remout
, "", 1);
978 if ((bp
= allocbuf(&buffer
, ofd
, COPY_BUFLEN
)) == NULL
) {
987 start_progress_meter(curfile
, size
, &statbytes
);
989 for (count
= i
= 0; i
< size
; i
+= bp
->cnt
) {
995 j
= atomicio6(read
, remin
, cp
, amt
,
998 run_err("%s", j
!= EPIPE
?
1000 "dropped connection");
1007 if (count
== bp
->cnt
) {
1008 /* Keep reading so we stay sync'd up. */
1010 if (atomicio(vwrite
, ofd
, bp
->buf
,
1020 unset_nonblock(remin
);
1022 stop_progress_meter();
1023 if (count
!= 0 && wrerr
== NO
&&
1024 atomicio(vwrite
, ofd
, bp
->buf
, count
) != count
) {
1028 if (wrerr
== NO
&& (!exists
|| S_ISREG(stb
.st_mode
)) &&
1029 ftruncate(ofd
, size
) != 0) {
1030 run_err("%s: truncate: %s", np
, strerror(errno
));
1034 if (exists
|| omode
!= mode
)
1036 if (fchmod(ofd
, omode
)) {
1037 #else /* HAVE_FCHMOD */
1038 if (chmod(np
, omode
)) {
1039 #endif /* HAVE_FCHMOD */
1040 run_err("%s: set mode: %s",
1041 np
, strerror(errno
));
1045 if (!exists
&& omode
!= mode
)
1047 if (fchmod(ofd
, omode
& ~mask
)) {
1048 #else /* HAVE_FCHMOD */
1049 if (chmod(np
, omode
& ~mask
)) {
1050 #endif /* HAVE_FCHMOD */
1051 run_err("%s: set mode: %s",
1052 np
, strerror(errno
));
1056 if (close(ofd
) == -1) {
1061 if (setimes
&& wrerr
== NO
) {
1063 if (utimes(np
, tv
) < 0) {
1064 run_err("%s: set times: %s",
1065 np
, strerror(errno
));
1071 run_err("%s: %s", np
, strerror(wrerrno
));
1074 (void) atomicio(vwrite
, remout
, "", 1);
1081 run_err("protocol error: %s", why
);
1088 char ch
, *cp
, resp
, rbuf
[2048];
1090 if (atomicio(read
, remin
, &resp
, sizeof(resp
)) != sizeof(resp
))
1100 case 1: /* error, followed by error msg */
1101 case 2: /* fatal error, "" */
1103 if (atomicio(read
, remin
, &ch
, sizeof(ch
)) != sizeof(ch
))
1106 } while (cp
< &rbuf
[sizeof(rbuf
) - 1] && ch
!= '\n');
1109 (void) atomicio(vwrite
, STDERR_FILENO
, rbuf
, cp
- rbuf
);
1121 (void) fprintf(stderr
,
1122 "usage: scp [-1246BCpqrv] [-c cipher] [-F ssh_config] [-i identity_file]\n"
1123 " [-l limit] [-o ssh_option] [-P port] [-S program]\n"
1124 " [[user@]host1:]file1 ... [[user@]host2:]file2\n");
1129 run_err(const char *fmt
,...)
1135 if (fp
!= NULL
|| (remout
!= -1 && (fp
= fdopen(remout
, "w")))) {
1136 (void) fprintf(fp
, "%c", 0x01);
1137 (void) fprintf(fp
, "scp: ");
1139 (void) vfprintf(fp
, fmt
, ap
);
1141 (void) fprintf(fp
, "\n");
1147 vfprintf(stderr
, fmt
, ap
);
1149 fprintf(stderr
, "\n");
1158 if (!stat(cp
, &stb
)) {
1159 if (S_ISDIR(stb
.st_mode
))
1163 run_err("%s: %s", cp
, strerror(errno
));
1178 if (!isalpha(c
) && !isdigit(c
)) {
1193 bad
: fprintf(stderr
, "%s: invalid user name\n", cp0
);
1198 allocbuf(BUF
*bp
, int fd
, int blksize
)
1201 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
1204 if (fstat(fd
, &stb
) < 0) {
1205 run_err("fstat: %s", strerror(errno
));
1208 size
= roundup(stb
.st_blksize
, blksize
);
1211 #else /* HAVE_STRUCT_STAT_ST_BLKSIZE */
1213 #endif /* HAVE_STRUCT_STAT_ST_BLKSIZE */
1214 if (bp
->cnt
>= size
)
1216 if (bp
->buf
== NULL
)
1217 bp
->buf
= xmalloc(size
);
1219 bp
->buf
= xrealloc(bp
->buf
, 1, size
);
1220 memset(bp
->buf
, 0, size
);
1229 write(STDERR_FILENO
, "lost connection\n", 16);