2 * scp - secure remote copy. This is basically patched BSD rcp which
3 * uses ssh to do the data transfer (instead of using rcmd).
5 * NOTE: This version should NOT be suid root. (This uses ssh to
6 * do the transfer and ssh has the necessary privileges.)
8 * 1995 Timo Rinne <tri@iki.fi>, Tatu Ylonen <ylo@cs.hut.fi>
10 * As far as I am concerned, the code I have written for this software
11 * can be used freely for any purpose. Any derived versions of this
12 * software must be clearly marked as such, and if the derived work is
13 * incompatible with the protocol description in the RFC file, it must be
14 * called by a name other than "ssh" or "Secure Shell".
17 * Copyright (c) 1999 Theo de Raadt. All rights reserved.
18 * Copyright (c) 1999 Aaron Campbell. All rights reserved.
20 * Redistribution and use in source and binary forms, with or without
21 * modification, are permitted provided that the following conditions
23 * 1. Redistributions of source code must retain the above copyright
24 * notice, this list of conditions and the following disclaimer.
25 * 2. Redistributions in binary form must reproduce the above copyright
26 * notice, this list of conditions and the following disclaimer in the
27 * documentation and/or other materials provided with the distribution.
29 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
30 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
31 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
32 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
33 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
34 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
38 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44 * Copyright (c) 1983, 1990, 1992, 1993, 1995
45 * The Regents of the University of California. All rights reserved.
47 * Redistribution and use in source and binary forms, with or without
48 * modification, are permitted provided that the following conditions
50 * 1. Redistributions of source code must retain the above copyright
51 * notice, this list of conditions and the following disclaimer.
52 * 2. Redistributions in binary form must reproduce the above copyright
53 * notice, this list of conditions and the following disclaimer in the
54 * documentation and/or other materials provided with the distribution.
55 * 3. Neither the name of the University nor the names of its contributors
56 * may be used to endorse or promote products derived from this software
57 * without specific prior written permission.
59 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
60 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
61 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
62 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
63 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
64 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
65 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
66 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
67 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
68 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
74 /*RCSID("$OpenBSD: scp.c,v 1.130 2006/01/31 10:35:43 djm Exp $");*/
79 #include "progressmeter.h"
83 /* Struct for addargs */
89 /* Name of current file being transferred. */
92 /* This is set to non-zero to enable verbose mode. */
95 /* This is set to zero if the progressmeter is not desired. */
98 /* This is the program to execute for the secured connection. ("ssh" or -S) */
99 char *ssh_program
= _PATH_SSH_PROGRAM
;
101 /* This is used to store the pid of ssh_program */
102 pid_t do_cmd_pid
= -1;
107 if (do_cmd_pid
> 1) {
108 kill(do_cmd_pid
, signo
? signo
: SIGTERM
);
109 waitpid(do_cmd_pid
, NULL
, 0);
118 do_local_cmd(arglist
*a
)
125 fatal("do_local_cmd: no arguments");
128 fprintf(stderr
, "Executing:");
129 for (i
= 0; i
< a
->num
; i
++)
130 fprintf(stderr
, " %s", a
->list
[i
]);
131 fprintf(stderr
, "\n");
137 #endif /* __uClinux__ */
139 fatal("do_local_cmd: fork: %s", strerror(errno
));
142 execvp(a
->list
[0], a
->list
);
148 #endif /* __uClinux__ */
152 signal(SIGTERM
, killchild
);
153 signal(SIGINT
, killchild
);
154 signal(SIGHUP
, killchild
);
156 while (waitpid(pid
, &status
, 0) == -1)
158 fatal("do_local_cmd: waitpid: %s", strerror(errno
));
162 if (!WIFEXITED(status
) || WEXITSTATUS(status
) != 0)
169 * This function executes the given command as the specified user on the
170 * given host. This returns < 0 if execution fails, and >= 0 otherwise. This
171 * assigns the input and output file descriptors on success.
175 do_cmd(char *host
, char *remuser
, char *cmd
, int *fdin
, int *fdout
, int argc
)
177 int pin
[2], pout
[2], reserved
[2];
181 "Executing: program %s host %s, user %s, command %s\n",
183 remuser
? remuser
: "(unspecified)", cmd
);
186 * Reserve two descriptors so that the real pipes won't get
187 * descriptors 0 and 1 because that will screw up dup2 below.
191 /* Create a socket pair for communicating with ssh. */
193 fatal("pipe: %s", strerror(errno
));
195 fatal("pipe: %s", strerror(errno
));
197 /* Free the reserved descriptors. */
201 /* uClinux needs to build the args here before vforking,
202 otherwise we do it later on. */
204 replacearg(&args
, 0, "%s", ssh_program
);
206 addargs(&args
, "-l%s", remuser
);
207 addargs(&args
, "%s", host
);
208 addargs(&args
, "%s", cmd
);
209 #endif /* __uClinux__ */
211 /* Fork a child to execute the command on the remote host using ssh. */
213 do_cmd_pid
= vfork();
216 #endif /* __uClinux__ */
218 if (do_cmd_pid
== 0) {
228 replacearg(&args
, 0, "%s", ssh_program
);
230 addargs(&args
, "-l%s", remuser
);
231 addargs(&args
, "%s", host
);
232 addargs(&args
, "%s", cmd
);
233 #endif /* __uClinux__ */
235 execvp(ssh_program
, args
.list
);
241 #endif /* __uClinux__ */
242 } else if (do_cmd_pid
== -1) {
243 fatal("fork: %s", strerror(errno
));
248 /* clean up command */
250 xfree(args
.list
[args
.num
-1]);
251 args
.list
[args
.num
-1]=NULL
;
254 xfree(args
.list
[args
.num
-1]);
255 args
.list
[args
.num
-1]=NULL
;
258 if (remuser
!= NULL
) {
259 xfree(args
.list
[args
.num
-1]);
260 args
.list
[args
.num
-1]=NULL
;
263 #endif /* __uClinux__ */
265 /* Parent. Close the other side, and return the local side. */
270 signal(SIGTERM
, killchild
);
271 signal(SIGINT
, killchild
);
272 signal(SIGHUP
, killchild
);
281 BUF
*allocbuf(BUF
*, int, int);
285 void run_err(const char *,...);
286 void verifydir(char *);
290 int errs
, remin
, remout
;
291 int pflag
, iamremote
, iamrecursive
, targetshouldbedirectory
;
294 char cmd
[CMDNEEDS
]; /* must hold "rcp -r -p -d\0" */
297 void rsource(char *, struct stat
*);
298 void sink(int, char *[]);
299 void source(int, char *[]);
300 void tolocal(int, char *[]);
301 void toremote(char *, int, char *[]);
304 #if defined(DBMULTI_scp) || !defined(DROPBEAR_MULTI)
305 #if defined(DBMULTI_scp) && defined(DROPBEAR_MULTI)
306 int scp_main(int argc
, char **argv
)
309 main(int argc
, char **argv
)
312 int ch
, fflag
, tflag
, status
;
318 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
321 memset(&args
, '\0', sizeof(args
));
323 addargs(&args
, "%s", ssh_program
);
326 while ((ch
= getopt(argc
, argv
, "dfl:prtvBCc:i:P:q1246S:o:F:")) != -1)
328 /* User-visible flags. */
334 addargs(&args
, "-%c", ch
);
340 addargs(&args
, "-%c%s", ch
, optarg
);
343 addargs(&args
, "-p%s", optarg
);
346 addargs(&args
, "-oBatchmode yes");
349 speed
= strtod(optarg
, &endp
);
350 if (speed
<= 0 || *endp
!= '\0')
352 limit_rate
= speed
* 1024;
361 ssh_program
= xstrdup(optarg
);
364 addargs(&args
, "-v");
367 #ifdef PROGRESS_METER
369 addargs(&args
, "-q");
374 /* Server options. */
376 targetshouldbedirectory
= 1;
378 case 'f': /* "from" */
386 setmode(0, O_BINARY
);
395 if ((pwd
= getpwuid(userid
= getuid())) == NULL
)
396 fatal("unknown user %u", (u_int
) userid
);
398 if (!isatty(STDERR_FILENO
))
401 remin
= STDIN_FILENO
;
402 remout
= STDOUT_FILENO
;
405 /* Follow "protocol", send data. */
418 targetshouldbedirectory
= 1;
422 /* Command to be executed on remote system using "ssh". */
423 (void) snprintf(cmd
, sizeof cmd
, "scp%s%s%s%s",
424 verbose_mode
? " -v" : "",
425 iamrecursive
? " -r" : "", pflag
? " -p" : "",
426 targetshouldbedirectory
? " -d" : "");
428 (void) signal(SIGPIPE
, lostconn
);
430 if ((targ
= colon(argv
[argc
- 1]))) /* Dest is remote host. */
431 toremote(targ
, argc
, argv
);
433 if (targetshouldbedirectory
)
434 verifydir(argv
[argc
- 1]);
435 tolocal(argc
, argv
); /* Dest is local host. */
438 * Finally check the exit status of the ssh process, if one was forked
439 * and no error has occured yet
441 if (do_cmd_pid
!= -1 && errs
== 0) {
445 (void) close(remout
);
446 if (waitpid(do_cmd_pid
, &status
, 0) == -1)
449 if (!WIFEXITED(status
) || WEXITSTATUS(status
) != 0)
455 #endif /* DBMULTI_scp stuff */
458 toremote(char *targ
, int argc
, char **argv
)
461 char *bp
, *host
, *src
, *suser
, *thost
, *tuser
, *arg
;
464 memset(&alist
, '\0', sizeof(alist
));
471 arg
= xstrdup(argv
[argc
- 1]);
472 if ((thost
= strrchr(arg
, '@'))) {
483 if (tuser
!= NULL
&& !okname(tuser
)) {
488 for (i
= 0; i
< argc
- 1; i
++) {
489 src
= colon(argv
[i
]);
490 if (src
) { /* remote to remote */
492 addargs(&alist
, "%s", ssh_program
);
494 addargs(&alist
, "-v");
495 addargs(&alist
, "-x");
496 addargs(&alist
, "-oClearAllForwardings yes");
497 addargs(&alist
, "-n");
502 host
= strrchr(argv
[i
], '@');
506 host
= cleanhostname(host
);
509 suser
= pwd
->pw_name
;
510 else if (!okname(suser
))
512 addargs(&alist
, "-l");
513 addargs(&alist
, "%s", suser
);
515 host
= cleanhostname(argv
[i
]);
517 addargs(&alist
, "%s", host
);
518 addargs(&alist
, "%s", cmd
);
519 addargs(&alist
, "%s", src
);
520 addargs(&alist
, "%s%s%s:%s",
521 tuser
? tuser
: "", tuser
? "@" : "",
523 if (do_local_cmd(&alist
) != 0)
525 } else { /* local to remote */
527 len
= strlen(targ
) + CMDNEEDS
+ 20;
529 (void) snprintf(bp
, len
, "%s -t %s", cmd
, targ
);
530 host
= cleanhostname(thost
);
531 if (do_cmd(host
, tuser
, bp
, &remin
,
544 tolocal(int argc
, char **argv
)
547 char *bp
, *host
, *src
, *suser
;
550 memset(&alist
, '\0', sizeof(alist
));
553 for (i
= 0; i
< argc
- 1; i
++) {
554 if (!(src
= colon(argv
[i
]))) { /* Local to local. */
556 addargs(&alist
, "%s", _PATH_CP
);
558 addargs(&alist
, "-r");
560 addargs(&alist
, "-p");
561 addargs(&alist
, "%s", argv
[i
]);
562 addargs(&alist
, "%s", argv
[argc
-1]);
563 if (do_local_cmd(&alist
))
570 if ((host
= strrchr(argv
[i
], '@')) == NULL
) {
577 suser
= pwd
->pw_name
;
579 host
= cleanhostname(host
);
580 len
= strlen(src
) + CMDNEEDS
+ 20;
582 (void) snprintf(bp
, len
, "%s -f %s", cmd
, src
);
583 if (do_cmd(host
, suser
, bp
, &remin
, &remout
, argc
) < 0) {
589 sink(1, argv
+ argc
- 1);
596 source(int argc
, char **argv
)
601 off_t i
, amt
, statbytes
;
603 int fd
= -1, haderr
, indx
;
604 char *last
, *name
, buf
[2048];
607 for (indx
= 0; indx
< argc
; ++indx
) {
611 while (len
> 1 && name
[len
-1] == '/')
613 if (strchr(name
, '\n') != NULL
) {
614 run_err("%s: skipping, filename contains a newline",
618 if ((fd
= open(name
, O_RDONLY
, 0)) < 0)
620 if (fstat(fd
, &stb
) < 0) {
621 syserr
: run_err("%s: %s", name
, strerror(errno
));
624 switch (stb
.st_mode
& S_IFMT
) {
634 run_err("%s: not a regular file", name
);
637 if ((last
= strrchr(name
, '/')) == NULL
)
644 * Make it compatible with possible future
645 * versions expecting microseconds.
647 (void) snprintf(buf
, sizeof buf
, "T%lu 0 %lu 0\n",
648 (u_long
) stb
.st_mtime
,
649 (u_long
) stb
.st_atime
);
650 (void) atomicio(vwrite
, remout
, buf
, strlen(buf
));
654 #define FILEMODEMASK (S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO)
655 snprintf(buf
, sizeof buf
, "C%04o %lld %s\n",
656 (u_int
) (stb
.st_mode
& FILEMODEMASK
),
657 (long long)stb
.st_size
, last
);
659 fprintf(stderr
, "Sending file modes: %s", buf
);
661 (void) atomicio(vwrite
, remout
, buf
, strlen(buf
));
664 if ((bp
= allocbuf(&buffer
, fd
, 2048)) == NULL
) {
665 next
: if (fd
!= -1) {
673 start_progress_meter(curfile
, stb
.st_size
, &statbytes
);
675 /* Keep writing after an error so that we stay sync'd up. */
676 for (haderr
= i
= 0; i
< stb
.st_size
; i
+= bp
->cnt
) {
678 if (i
+ amt
> stb
.st_size
)
679 amt
= stb
.st_size
- i
;
681 result
= atomicio(read
, fd
, bp
->buf
, amt
);
686 (void) atomicio(vwrite
, remout
, bp
->buf
, amt
);
688 result
= atomicio(vwrite
, remout
, bp
->buf
, amt
);
696 #ifdef PROGRESS_METER
698 stop_progress_meter();
702 if (close(fd
) < 0 && !haderr
)
707 (void) atomicio(vwrite
, remout
, "", 1);
709 run_err("%s: %s", name
, strerror(haderr
));
715 rsource(char *name
, struct stat
*statp
)
719 char *last
, *vect
[1], path
[1100];
721 if (!(dirp
= opendir(name
))) {
722 run_err("%s: %s", name
, strerror(errno
));
725 last
= strrchr(name
, '/');
731 (void) snprintf(path
, sizeof(path
), "T%lu 0 %lu 0\n",
732 (u_long
) statp
->st_mtime
,
733 (u_long
) statp
->st_atime
);
734 (void) atomicio(vwrite
, remout
, path
, strlen(path
));
735 if (response() < 0) {
740 (void) snprintf(path
, sizeof path
, "D%04o %d %.1024s\n",
741 (u_int
) (statp
->st_mode
& FILEMODEMASK
), 0, last
);
743 fprintf(stderr
, "Entering directory: %s", path
);
744 (void) atomicio(vwrite
, remout
, path
, strlen(path
));
745 if (response() < 0) {
749 while ((dp
= readdir(dirp
)) != NULL
) {
752 if (!strcmp(dp
->d_name
, ".") || !strcmp(dp
->d_name
, ".."))
754 if (strlen(name
) + 1 + strlen(dp
->d_name
) >= sizeof(path
) - 1) {
755 run_err("%s/%s: name too long", name
, dp
->d_name
);
758 (void) snprintf(path
, sizeof path
, "%s/%s", name
, dp
->d_name
);
762 (void) closedir(dirp
);
763 (void) atomicio(vwrite
, remout
, "E\n", 2);
770 static struct timeval bwstart
, bwend
;
771 static int lamt
, thresh
= 16384;
773 struct timespec ts
, rm
;
775 if (!timerisset(&bwstart
)) {
776 gettimeofday(&bwstart
, NULL
);
784 gettimeofday(&bwend
, NULL
);
785 timersub(&bwend
, &bwstart
, &bwend
);
786 if (!timerisset(&bwend
))
790 waitlen
= (double)1000000L * lamt
/ limit_rate
;
792 bwstart
.tv_sec
= waitlen
/ 1000000L;
793 bwstart
.tv_usec
= waitlen
% 1000000L;
795 if (timercmp(&bwstart
, &bwend
, >)) {
796 timersub(&bwstart
, &bwend
, &bwend
);
798 /* Adjust the wait time */
803 } else if (bwend
.tv_usec
< 100) {
809 TIMEVAL_TO_TIMESPEC(&bwend
, &ts
);
810 while (nanosleep(&ts
, &rm
) == -1) {
818 gettimeofday(&bwstart
, NULL
);
822 sink(int argc
, char **argv
)
832 int amt
, exists
, first
, mask
, mode
, ofd
, omode
;
833 off_t size
, statbytes
;
834 int setimes
, targisdir
, wrerrno
= 0;
835 char ch
, *cp
, *np
, *targ
, *why
, *vect
[1], buf
[2048];
836 struct timeval tv
[2];
840 #define SCREWUP(str) { why = str; goto screwup; }
842 setimes
= targisdir
= 0;
847 run_err("ambiguous target");
851 if (targetshouldbedirectory
)
854 (void) atomicio(vwrite
, remout
, "", 1);
855 if (stat(targ
, &stb
) == 0 && S_ISDIR(stb
.st_mode
))
857 for (first
= 1;; first
= 0) {
859 if (atomicio(read
, remin
, cp
, 1) != 1)
862 SCREWUP("unexpected <newline>");
864 if (atomicio(read
, remin
, &ch
, sizeof(ch
)) != sizeof(ch
))
865 SCREWUP("lost connection");
867 } while (cp
< &buf
[sizeof(buf
) - 1] && ch
!= '\n');
870 fprintf(stderr
, "Sink: %s", buf
);
872 if (buf
[0] == '\01' || buf
[0] == '\02') {
874 (void) atomicio(vwrite
, STDERR_FILENO
,
875 buf
+ 1, strlen(buf
+ 1));
882 (void) atomicio(vwrite
, remout
, "", 1);
892 mtime
.tv_sec
= strtol(cp
, &cp
, 10);
893 if (!cp
|| *cp
++ != ' ')
894 SCREWUP("mtime.sec not delimited");
895 mtime
.tv_usec
= strtol(cp
, &cp
, 10);
896 if (!cp
|| *cp
++ != ' ')
897 SCREWUP("mtime.usec not delimited");
898 atime
.tv_sec
= strtol(cp
, &cp
, 10);
899 if (!cp
|| *cp
++ != ' ')
900 SCREWUP("atime.sec not delimited");
901 atime
.tv_usec
= strtol(cp
, &cp
, 10);
902 if (!cp
|| *cp
++ != '\0')
903 SCREWUP("atime.usec not delimited");
904 (void) atomicio(vwrite
, remout
, "", 1);
907 if (*cp
!= 'C' && *cp
!= 'D') {
909 * Check for the case "rcp remote:foo\* local:bar".
910 * In this case, the line "No match." can be returned
911 * by the shell before the rcp command on the remote is
912 * executed so the ^Aerror_message convention isn't
919 SCREWUP("expected control record");
922 for (++cp
; cp
< buf
+ 5; cp
++) {
923 if (*cp
< '0' || *cp
> '7')
925 mode
= (mode
<< 3) | (*cp
- '0');
928 SCREWUP("mode not delimited");
930 for (size
= 0; isdigit(*cp
);)
931 size
= size
* 10 + (*cp
++ - '0');
933 SCREWUP("size not delimited");
934 if ((strchr(cp
, '/') != NULL
) || (strcmp(cp
, "..") == 0)) {
935 run_err("error: unexpected filename: %s", cp
);
939 static char *namebuf
;
940 static size_t cursize
;
943 need
= strlen(targ
) + strlen(cp
) + 250;
944 if (need
> cursize
) {
947 namebuf
= xmalloc(need
);
950 (void) snprintf(namebuf
, need
, "%s%s%s", targ
,
951 strcmp(targ
, "/") ? "/" : "", cp
);
956 exists
= stat(np
, &stb
) == 0;
958 int mod_flag
= pflag
;
960 SCREWUP("received directory without -r");
962 if (!S_ISDIR(stb
.st_mode
)) {
967 (void) chmod(np
, mode
);
969 /* Handle copying from a read-only
972 if (mkdir(np
, mode
| S_IRWXU
) < 0)
975 vect
[0] = xstrdup(np
);
979 if (utimes(vect
[0], tv
) < 0)
980 run_err("%s: set times: %s",
981 vect
[0], strerror(errno
));
984 (void) chmod(vect
[0], mode
);
991 if ((ofd
= open(np
, O_WRONLY
|O_CREAT
, mode
)) < 0) {
992 bad
: run_err("%s: %s", np
, strerror(errno
));
995 (void) atomicio(vwrite
, remout
, "", 1);
996 if ((bp
= allocbuf(&buffer
, ofd
, 4096)) == NULL
) {
1004 #ifdef PROGRESS_METER
1006 start_progress_meter(curfile
, size
, &statbytes
);
1008 for (count
= i
= 0; i
< size
; i
+= 4096) {
1014 j
= atomicio(read
, remin
, cp
, amt
);
1016 run_err("%s", j
? strerror(errno
) :
1017 "dropped connection");
1028 if (count
== bp
->cnt
) {
1029 /* Keep reading so we stay sync'd up. */
1031 if (atomicio(vwrite
, ofd
, bp
->buf
,
1041 #ifdef PROGRESS_METER
1043 stop_progress_meter();
1045 if (count
!= 0 && wrerr
== NO
&&
1046 atomicio(vwrite
, ofd
, bp
->buf
, count
) != count
) {
1050 if (wrerr
== NO
&& ftruncate(ofd
, size
) != 0) {
1051 run_err("%s: truncate: %s", np
, strerror(errno
));
1055 if (exists
|| omode
!= mode
)
1057 if (fchmod(ofd
, omode
)) {
1058 #else /* HAVE_FCHMOD */
1059 if (chmod(np
, omode
)) {
1060 #endif /* HAVE_FCHMOD */
1061 run_err("%s: set mode: %s",
1062 np
, strerror(errno
));
1066 if (!exists
&& omode
!= mode
)
1068 if (fchmod(ofd
, omode
& ~mask
)) {
1069 #else /* HAVE_FCHMOD */
1070 if (chmod(np
, omode
& ~mask
)) {
1071 #endif /* HAVE_FCHMOD */
1072 run_err("%s: set mode: %s",
1073 np
, strerror(errno
));
1077 if (close(ofd
) == -1) {
1082 if (setimes
&& wrerr
== NO
) {
1084 if (utimes(np
, tv
) < 0) {
1085 run_err("%s: set times: %s",
1086 np
, strerror(errno
));
1092 run_err("%s: %s", np
, strerror(wrerrno
));
1095 (void) atomicio(vwrite
, remout
, "", 1);
1102 run_err("protocol error: %s", why
);
1109 char ch
, *cp
, resp
, rbuf
[2048];
1111 if (atomicio(read
, remin
, &resp
, sizeof(resp
)) != sizeof(resp
))
1121 case 1: /* error, followed by error msg */
1122 case 2: /* fatal error, "" */
1124 if (atomicio(read
, remin
, &ch
, sizeof(ch
)) != sizeof(ch
))
1127 } while (cp
< &rbuf
[sizeof(rbuf
) - 1] && ch
!= '\n');
1130 (void) atomicio(vwrite
, STDERR_FILENO
, rbuf
, cp
- rbuf
);
1142 (void) fprintf(stderr
,
1143 "usage: scp [-1246BCpqrv] [-c cipher] [-F ssh_config] [-i identity_file]\n"
1144 " [-l limit] [-o ssh_option] [-P port] [-S program]\n"
1145 " [[user@]host1:]file1 [...] [[user@]host2:]file2\n");
1150 run_err(const char *fmt
,...)
1156 if (fp
== NULL
&& !(fp
= fdopen(remout
, "w")))
1158 (void) fprintf(fp
, "%c", 0x01);
1159 (void) fprintf(fp
, "scp: ");
1161 (void) vfprintf(fp
, fmt
, ap
);
1163 (void) fprintf(fp
, "\n");
1168 vfprintf(stderr
, fmt
, ap
);
1170 fprintf(stderr
, "\n");
1179 if (!stat(cp
, &stb
)) {
1180 if (S_ISDIR(stb
.st_mode
))
1184 run_err("%s: %s", cp
, strerror(errno
));
1199 if (!isalpha(c
) && !isdigit(c
)) {
1214 bad
: fprintf(stderr
, "%s: invalid user name\n", cp0
);
1219 allocbuf(BUF
*bp
, int fd
, int blksize
)
1222 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
1225 if (fstat(fd
, &stb
) < 0) {
1226 run_err("fstat: %s", strerror(errno
));
1229 size
= roundup(stb
.st_blksize
, blksize
);
1232 #else /* HAVE_STRUCT_STAT_ST_BLKSIZE */
1234 #endif /* HAVE_STRUCT_STAT_ST_BLKSIZE */
1235 if (bp
->cnt
>= size
)
1237 if (bp
->buf
== NULL
)
1238 bp
->buf
= xmalloc(size
);
1240 bp
->buf
= xrealloc(bp
->buf
, size
);
1241 memset(bp
->buf
, 0, size
);
1250 write(STDERR_FILENO
, "lost connection\n", 16);