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 fprintf(stderr
, "Note: -B option is disabled in this version of scp");
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");
496 // Disabled since dbclient won't understand them
497 // and scp works fine without them.
498 addargs(&alist
, "-x");
499 addargs(&alist
, "-oClearAllForwardings yes");
500 addargs(&alist
, "-n");
506 host
= strrchr(argv
[i
], '@');
510 host
= cleanhostname(host
);
513 suser
= pwd
->pw_name
;
514 else if (!okname(suser
))
516 addargs(&alist
, "-l");
517 addargs(&alist
, "%s", suser
);
519 host
= cleanhostname(argv
[i
]);
521 addargs(&alist
, "%s", host
);
522 addargs(&alist
, "%s", cmd
);
523 addargs(&alist
, "%s", src
);
524 addargs(&alist
, "%s%s%s:%s",
525 tuser
? tuser
: "", tuser
? "@" : "",
527 if (do_local_cmd(&alist
) != 0)
529 } else { /* local to remote */
531 len
= strlen(targ
) + CMDNEEDS
+ 20;
533 (void) snprintf(bp
, len
, "%s -t %s", cmd
, targ
);
534 host
= cleanhostname(thost
);
535 if (do_cmd(host
, tuser
, bp
, &remin
,
548 tolocal(int argc
, char **argv
)
551 char *bp
, *host
, *src
, *suser
;
554 memset(&alist
, '\0', sizeof(alist
));
557 for (i
= 0; i
< argc
- 1; i
++) {
558 if (!(src
= colon(argv
[i
]))) { /* Local to local. */
560 addargs(&alist
, "%s", _PATH_CP
);
562 addargs(&alist
, "-r");
564 addargs(&alist
, "-p");
565 addargs(&alist
, "%s", argv
[i
]);
566 addargs(&alist
, "%s", argv
[argc
-1]);
567 if (do_local_cmd(&alist
))
574 if ((host
= strrchr(argv
[i
], '@')) == NULL
) {
581 suser
= pwd
->pw_name
;
583 host
= cleanhostname(host
);
584 len
= strlen(src
) + CMDNEEDS
+ 20;
586 (void) snprintf(bp
, len
, "%s -f %s", cmd
, src
);
587 if (do_cmd(host
, suser
, bp
, &remin
, &remout
, argc
) < 0) {
593 sink(1, argv
+ argc
- 1);
600 source(int argc
, char **argv
)
605 off_t i
, amt
, statbytes
;
607 int fd
= -1, haderr
, indx
;
608 char *last
, *name
, buf
[2048];
611 for (indx
= 0; indx
< argc
; ++indx
) {
615 while (len
> 1 && name
[len
-1] == '/')
617 if (strchr(name
, '\n') != NULL
) {
618 run_err("%s: skipping, filename contains a newline",
622 if ((fd
= open(name
, O_RDONLY
, 0)) < 0)
624 if (fstat(fd
, &stb
) < 0) {
625 syserr
: run_err("%s: %s", name
, strerror(errno
));
628 switch (stb
.st_mode
& S_IFMT
) {
638 run_err("%s: not a regular file", name
);
641 if ((last
= strrchr(name
, '/')) == NULL
)
648 * Make it compatible with possible future
649 * versions expecting microseconds.
651 (void) snprintf(buf
, sizeof buf
, "T%lu 0 %lu 0\n",
652 (u_long
) stb
.st_mtime
,
653 (u_long
) stb
.st_atime
);
654 (void) atomicio(vwrite
, remout
, buf
, strlen(buf
));
658 #define FILEMODEMASK (S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO)
659 snprintf(buf
, sizeof buf
, "C%04o %lld %s\n",
660 (u_int
) (stb
.st_mode
& FILEMODEMASK
),
661 (long long)stb
.st_size
, last
);
663 fprintf(stderr
, "Sending file modes: %s", buf
);
665 (void) atomicio(vwrite
, remout
, buf
, strlen(buf
));
668 if ((bp
= allocbuf(&buffer
, fd
, 2048)) == NULL
) {
669 next
: if (fd
!= -1) {
677 start_progress_meter(curfile
, stb
.st_size
, &statbytes
);
679 /* Keep writing after an error so that we stay sync'd up. */
680 for (haderr
= i
= 0; i
< stb
.st_size
; i
+= bp
->cnt
) {
682 if (i
+ amt
> stb
.st_size
)
683 amt
= stb
.st_size
- i
;
685 result
= atomicio(read
, fd
, bp
->buf
, amt
);
690 (void) atomicio(vwrite
, remout
, bp
->buf
, amt
);
692 result
= atomicio(vwrite
, remout
, bp
->buf
, amt
);
700 #ifdef PROGRESS_METER
702 stop_progress_meter();
706 if (close(fd
) < 0 && !haderr
)
711 (void) atomicio(vwrite
, remout
, "", 1);
713 run_err("%s: %s", name
, strerror(haderr
));
719 rsource(char *name
, struct stat
*statp
)
723 char *last
, *vect
[1], path
[1100];
725 if (!(dirp
= opendir(name
))) {
726 run_err("%s: %s", name
, strerror(errno
));
729 last
= strrchr(name
, '/');
735 (void) snprintf(path
, sizeof(path
), "T%lu 0 %lu 0\n",
736 (u_long
) statp
->st_mtime
,
737 (u_long
) statp
->st_atime
);
738 (void) atomicio(vwrite
, remout
, path
, strlen(path
));
739 if (response() < 0) {
744 (void) snprintf(path
, sizeof path
, "D%04o %d %.1024s\n",
745 (u_int
) (statp
->st_mode
& FILEMODEMASK
), 0, last
);
747 fprintf(stderr
, "Entering directory: %s", path
);
748 (void) atomicio(vwrite
, remout
, path
, strlen(path
));
749 if (response() < 0) {
753 while ((dp
= readdir(dirp
)) != NULL
) {
756 if (!strcmp(dp
->d_name
, ".") || !strcmp(dp
->d_name
, ".."))
758 if (strlen(name
) + 1 + strlen(dp
->d_name
) >= sizeof(path
) - 1) {
759 run_err("%s/%s: name too long", name
, dp
->d_name
);
762 (void) snprintf(path
, sizeof path
, "%s/%s", name
, dp
->d_name
);
766 (void) closedir(dirp
);
767 (void) atomicio(vwrite
, remout
, "E\n", 2);
774 static struct timeval bwstart
, bwend
;
775 static int lamt
, thresh
= 16384;
777 struct timespec ts
, rm
;
779 if (!timerisset(&bwstart
)) {
780 gettimeofday(&bwstart
, NULL
);
788 gettimeofday(&bwend
, NULL
);
789 timersub(&bwend
, &bwstart
, &bwend
);
790 if (!timerisset(&bwend
))
794 waitlen
= (double)1000000L * lamt
/ limit_rate
;
796 bwstart
.tv_sec
= waitlen
/ 1000000L;
797 bwstart
.tv_usec
= waitlen
% 1000000L;
799 if (timercmp(&bwstart
, &bwend
, >)) {
800 timersub(&bwstart
, &bwend
, &bwend
);
802 /* Adjust the wait time */
807 } else if (bwend
.tv_usec
< 100) {
813 TIMEVAL_TO_TIMESPEC(&bwend
, &ts
);
814 while (nanosleep(&ts
, &rm
) == -1) {
822 gettimeofday(&bwstart
, NULL
);
826 sink(int argc
, char **argv
)
836 int amt
, exists
, first
, mask
, mode
, ofd
, omode
;
837 off_t size
, statbytes
;
838 int setimes
, targisdir
, wrerrno
= 0;
839 char ch
, *cp
, *np
, *targ
, *why
, *vect
[1], buf
[2048];
840 struct timeval tv
[2];
844 #define SCREWUP(str) { why = str; goto screwup; }
846 setimes
= targisdir
= 0;
851 run_err("ambiguous target");
855 if (targetshouldbedirectory
)
858 (void) atomicio(vwrite
, remout
, "", 1);
859 if (stat(targ
, &stb
) == 0 && S_ISDIR(stb
.st_mode
))
861 for (first
= 1;; first
= 0) {
863 if (atomicio(read
, remin
, cp
, 1) != 1)
866 SCREWUP("unexpected <newline>");
868 if (atomicio(read
, remin
, &ch
, sizeof(ch
)) != sizeof(ch
))
869 SCREWUP("lost connection");
871 } while (cp
< &buf
[sizeof(buf
) - 1] && ch
!= '\n');
874 fprintf(stderr
, "Sink: %s", buf
);
876 if (buf
[0] == '\01' || buf
[0] == '\02') {
878 (void) atomicio(vwrite
, STDERR_FILENO
,
879 buf
+ 1, strlen(buf
+ 1));
886 (void) atomicio(vwrite
, remout
, "", 1);
896 mtime
.tv_sec
= strtol(cp
, &cp
, 10);
897 if (!cp
|| *cp
++ != ' ')
898 SCREWUP("mtime.sec not delimited");
899 mtime
.tv_usec
= strtol(cp
, &cp
, 10);
900 if (!cp
|| *cp
++ != ' ')
901 SCREWUP("mtime.usec not delimited");
902 atime
.tv_sec
= strtol(cp
, &cp
, 10);
903 if (!cp
|| *cp
++ != ' ')
904 SCREWUP("atime.sec not delimited");
905 atime
.tv_usec
= strtol(cp
, &cp
, 10);
906 if (!cp
|| *cp
++ != '\0')
907 SCREWUP("atime.usec not delimited");
908 (void) atomicio(vwrite
, remout
, "", 1);
911 if (*cp
!= 'C' && *cp
!= 'D') {
913 * Check for the case "rcp remote:foo\* local:bar".
914 * In this case, the line "No match." can be returned
915 * by the shell before the rcp command on the remote is
916 * executed so the ^Aerror_message convention isn't
923 SCREWUP("expected control record");
926 for (++cp
; cp
< buf
+ 5; cp
++) {
927 if (*cp
< '0' || *cp
> '7')
929 mode
= (mode
<< 3) | (*cp
- '0');
932 SCREWUP("mode not delimited");
934 for (size
= 0; isdigit(*cp
);)
935 size
= size
* 10 + (*cp
++ - '0');
937 SCREWUP("size not delimited");
938 if ((strchr(cp
, '/') != NULL
) || (strcmp(cp
, "..") == 0)) {
939 run_err("error: unexpected filename: %s", cp
);
943 static char *namebuf
;
944 static size_t cursize
;
947 need
= strlen(targ
) + strlen(cp
) + 250;
948 if (need
> cursize
) {
951 namebuf
= xmalloc(need
);
954 (void) snprintf(namebuf
, need
, "%s%s%s", targ
,
955 strcmp(targ
, "/") ? "/" : "", cp
);
960 exists
= stat(np
, &stb
) == 0;
962 int mod_flag
= pflag
;
964 SCREWUP("received directory without -r");
966 if (!S_ISDIR(stb
.st_mode
)) {
971 (void) chmod(np
, mode
);
973 /* Handle copying from a read-only
976 if (mkdir(np
, mode
| S_IRWXU
) < 0)
979 vect
[0] = xstrdup(np
);
983 if (utimes(vect
[0], tv
) < 0)
984 run_err("%s: set times: %s",
985 vect
[0], strerror(errno
));
988 (void) chmod(vect
[0], mode
);
995 if ((ofd
= open(np
, O_WRONLY
|O_CREAT
, mode
)) < 0) {
996 bad
: run_err("%s: %s", np
, strerror(errno
));
999 (void) atomicio(vwrite
, remout
, "", 1);
1000 if ((bp
= allocbuf(&buffer
, ofd
, 4096)) == NULL
) {
1008 #ifdef PROGRESS_METER
1010 start_progress_meter(curfile
, size
, &statbytes
);
1012 for (count
= i
= 0; i
< size
; i
+= 4096) {
1018 j
= atomicio(read
, remin
, cp
, amt
);
1020 run_err("%s", j
? strerror(errno
) :
1021 "dropped connection");
1032 if (count
== bp
->cnt
) {
1033 /* Keep reading so we stay sync'd up. */
1035 if (atomicio(vwrite
, ofd
, bp
->buf
,
1045 #ifdef PROGRESS_METER
1047 stop_progress_meter();
1049 if (count
!= 0 && wrerr
== NO
&&
1050 atomicio(vwrite
, ofd
, bp
->buf
, count
) != count
) {
1054 if (wrerr
== NO
&& ftruncate(ofd
, size
) != 0) {
1055 run_err("%s: truncate: %s", np
, strerror(errno
));
1059 if (exists
|| omode
!= mode
)
1061 if (fchmod(ofd
, omode
)) {
1062 #else /* HAVE_FCHMOD */
1063 if (chmod(np
, omode
)) {
1064 #endif /* HAVE_FCHMOD */
1065 run_err("%s: set mode: %s",
1066 np
, strerror(errno
));
1070 if (!exists
&& omode
!= mode
)
1072 if (fchmod(ofd
, omode
& ~mask
)) {
1073 #else /* HAVE_FCHMOD */
1074 if (chmod(np
, omode
& ~mask
)) {
1075 #endif /* HAVE_FCHMOD */
1076 run_err("%s: set mode: %s",
1077 np
, strerror(errno
));
1081 if (close(ofd
) == -1) {
1086 if (setimes
&& wrerr
== NO
) {
1088 if (utimes(np
, tv
) < 0) {
1089 run_err("%s: set times: %s",
1090 np
, strerror(errno
));
1096 run_err("%s: %s", np
, strerror(wrerrno
));
1099 (void) atomicio(vwrite
, remout
, "", 1);
1106 run_err("protocol error: %s", why
);
1113 char ch
, *cp
, resp
, rbuf
[2048];
1115 if (atomicio(read
, remin
, &resp
, sizeof(resp
)) != sizeof(resp
))
1125 case 1: /* error, followed by error msg */
1126 case 2: /* fatal error, "" */
1128 if (atomicio(read
, remin
, &ch
, sizeof(ch
)) != sizeof(ch
))
1131 } while (cp
< &rbuf
[sizeof(rbuf
) - 1] && ch
!= '\n');
1134 (void) atomicio(vwrite
, STDERR_FILENO
, rbuf
, cp
- rbuf
);
1146 (void) fprintf(stderr
,
1147 "usage: scp [-1246BCpqrv] [-c cipher] [-F ssh_config] [-i identity_file]\n"
1148 " [-l limit] [-o ssh_option] [-P port] [-S program]\n"
1149 " [[user@]host1:]file1 [...] [[user@]host2:]file2\n");
1154 run_err(const char *fmt
,...)
1160 if (fp
== NULL
&& !(fp
= fdopen(remout
, "w")))
1162 (void) fprintf(fp
, "%c", 0x01);
1163 (void) fprintf(fp
, "scp: ");
1165 (void) vfprintf(fp
, fmt
, ap
);
1167 (void) fprintf(fp
, "\n");
1172 vfprintf(stderr
, fmt
, ap
);
1174 fprintf(stderr
, "\n");
1183 if (!stat(cp
, &stb
)) {
1184 if (S_ISDIR(stb
.st_mode
))
1188 run_err("%s: %s", cp
, strerror(errno
));
1203 if (!isalpha(c
) && !isdigit(c
)) {
1218 bad
: fprintf(stderr
, "%s: invalid user name\n", cp0
);
1223 allocbuf(BUF
*bp
, int fd
, int blksize
)
1226 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
1229 if (fstat(fd
, &stb
) < 0) {
1230 run_err("fstat: %s", strerror(errno
));
1233 size
= roundup(stb
.st_blksize
, blksize
);
1236 #else /* HAVE_STRUCT_STAT_ST_BLKSIZE */
1238 #endif /* HAVE_STRUCT_STAT_ST_BLKSIZE */
1239 if (bp
->cnt
>= size
)
1241 if (bp
->buf
== NULL
)
1242 bp
->buf
= xmalloc(size
);
1244 bp
->buf
= xrealloc(bp
->buf
, size
);
1245 memset(bp
->buf
, 0, size
);
1254 write(STDERR_FILENO
, "lost connection\n", 16);