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");
139 fatal("do_local_cmd: fork: %s", strerror(errno
));
142 execvp(a
->list
[0], a
->list
);
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 arg_setup(char *host
, char *remuser
, char *cmd
)
177 replacearg(&args
, 0, "%s", ssh_program
);
179 addargs(&args
, "-l%s", remuser
);
180 addargs(&args
, "%s", host
);
181 addargs(&args
, "%s", cmd
);
185 do_cmd(char *host
, char *remuser
, char *cmd
, int *fdin
, int *fdout
, int argc
)
187 int pin
[2], pout
[2], reserved
[2];
191 "Executing: program %s host %s, user %s, command %s\n",
193 remuser
? remuser
: "(unspecified)", cmd
);
196 * Reserve two descriptors so that the real pipes won't get
197 * descriptors 0 and 1 because that will screw up dup2 below.
201 /* Create a socket pair for communicating with ssh. */
203 fatal("pipe: %s", strerror(errno
));
205 fatal("pipe: %s", strerror(errno
));
207 /* Free the reserved descriptors. */
211 /* uClinux needs to build the args here before vforking,
212 otherwise we do it later on. */
214 arg_setup(host
, remuser
, cmd
);
217 /* Fork a child to execute the command on the remote host using ssh. */
219 do_cmd_pid
= vfork();
224 if (do_cmd_pid
== 0) {
234 arg_setup(host
, remuser
, cmd
);
237 execvp(ssh_program
, args
.list
);
244 } else if (do_cmd_pid
== -1) {
245 fatal("fork: %s", strerror(errno
));
249 /* clean up command */
251 xfree(args
.list
[args
.num
-1]);
252 args
.list
[args
.num
-1]=NULL
;
255 xfree(args
.list
[args
.num
-1]);
256 args
.list
[args
.num
-1]=NULL
;
259 if (remuser
!= NULL
) {
260 xfree(args
.list
[args
.num
-1]);
261 args
.list
[args
.num
-1]=NULL
;
266 /* Parent. Close the other side, and return the local side. */
271 signal(SIGTERM
, killchild
);
272 signal(SIGINT
, killchild
);
273 signal(SIGHUP
, killchild
);
282 BUF
*allocbuf(BUF
*, int, int);
286 void run_err(const char *,...);
287 void verifydir(char *);
291 int errs
, remin
, remout
;
292 int pflag
, iamremote
, iamrecursive
, targetshouldbedirectory
;
295 char cmd
[CMDNEEDS
]; /* must hold "rcp -r -p -d\0" */
298 void rsource(char *, struct stat
*);
299 void sink(int, char *[]);
300 void source(int, char *[]);
301 void tolocal(int, char *[]);
302 void toremote(char *, int, char *[]);
305 #if defined(DBMULTI_scp) || !defined(DROPBEAR_MULTI)
306 #if defined(DBMULTI_scp) && defined(DROPBEAR_MULTI)
307 int scp_main(int argc
, char **argv
)
310 main(int argc
, char **argv
)
313 int ch
, fflag
, tflag
, status
;
319 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
322 memset(&args
, '\0', sizeof(args
));
324 addargs(&args
, "%s", ssh_program
);
327 while ((ch
= getopt(argc
, argv
, "dfl:prtvBCc:i:P:q1246S:o:F:")) != -1)
329 /* User-visible flags. */
335 addargs(&args
, "-%c", ch
);
341 addargs(&args
, "-%c%s", ch
, optarg
);
344 addargs(&args
, "-p%s", optarg
);
347 fprintf(stderr
, "Note: -B option is disabled in this version of scp");
350 speed
= strtod(optarg
, &endp
);
351 if (speed
<= 0 || *endp
!= '\0')
353 limit_rate
= speed
* 1024;
362 ssh_program
= xstrdup(optarg
);
365 addargs(&args
, "-v");
369 #ifdef PROGRESS_METER
370 addargs(&args
, "-q");
375 /* Server options. */
377 targetshouldbedirectory
= 1;
379 case 'f': /* "from" */
387 setmode(0, O_BINARY
);
396 if ((pwd
= getpwuid(userid
= getuid())) == NULL
)
397 fatal("unknown user %u", (u_int
) userid
);
399 if (!isatty(STDERR_FILENO
))
402 remin
= STDIN_FILENO
;
403 remout
= STDOUT_FILENO
;
406 /* Follow "protocol", send data. */
419 targetshouldbedirectory
= 1;
423 /* Command to be executed on remote system using "ssh". */
424 (void) snprintf(cmd
, sizeof cmd
, "scp%s%s%s%s",
425 verbose_mode
? " -v" : "",
426 iamrecursive
? " -r" : "", pflag
? " -p" : "",
427 targetshouldbedirectory
? " -d" : "");
429 (void) signal(SIGPIPE
, lostconn
);
431 if ((targ
= colon(argv
[argc
- 1]))) /* Dest is remote host. */
432 toremote(targ
, argc
, argv
);
434 if (targetshouldbedirectory
)
435 verifydir(argv
[argc
- 1]);
436 tolocal(argc
, argv
); /* Dest is local host. */
439 * Finally check the exit status of the ssh process, if one was forked
440 * and no error has occured yet
442 if (do_cmd_pid
!= -1 && errs
== 0) {
446 (void) close(remout
);
447 if (waitpid(do_cmd_pid
, &status
, 0) == -1)
450 if (!WIFEXITED(status
) || WEXITSTATUS(status
) != 0)
456 #endif /* DBMULTI_scp stuff */
459 toremote(char *targ
, int argc
, char **argv
)
462 char *bp
, *host
, *src
, *suser
, *thost
, *tuser
, *arg
;
465 memset(&alist
, '\0', sizeof(alist
));
472 arg
= xstrdup(argv
[argc
- 1]);
473 if ((thost
= strrchr(arg
, '@'))) {
484 if (tuser
!= NULL
&& !okname(tuser
)) {
489 for (i
= 0; i
< argc
- 1; i
++) {
490 src
= colon(argv
[i
]);
491 if (src
) { /* remote to remote */
493 addargs(&alist
, "%s", ssh_program
);
495 addargs(&alist
, "-v");
497 /* Disabled since dbclient won't understand them
498 and scp works fine without them. */
499 addargs(&alist
, "-x");
500 addargs(&alist
, "-oClearAllForwardings yes");
501 addargs(&alist
, "-n");
507 host
= strrchr(argv
[i
], '@');
511 host
= cleanhostname(host
);
514 suser
= pwd
->pw_name
;
515 else if (!okname(suser
))
517 addargs(&alist
, "-l");
518 addargs(&alist
, "%s", suser
);
520 host
= cleanhostname(argv
[i
]);
522 addargs(&alist
, "%s", host
);
523 addargs(&alist
, "%s", cmd
);
524 addargs(&alist
, "%s", src
);
525 addargs(&alist
, "%s%s%s:%s",
526 tuser
? tuser
: "", tuser
? "@" : "",
528 if (do_local_cmd(&alist
) != 0)
530 } else { /* local to remote */
532 len
= strlen(targ
) + CMDNEEDS
+ 20;
534 (void) snprintf(bp
, len
, "%s -t %s", cmd
, targ
);
535 host
= cleanhostname(thost
);
536 if (do_cmd(host
, tuser
, bp
, &remin
,
549 tolocal(int argc
, char **argv
)
552 char *bp
, *host
, *src
, *suser
;
555 memset(&alist
, '\0', sizeof(alist
));
558 for (i
= 0; i
< argc
- 1; i
++) {
559 if (!(src
= colon(argv
[i
]))) { /* Local to local. */
561 addargs(&alist
, "%s", _PATH_CP
);
563 addargs(&alist
, "-r");
565 addargs(&alist
, "-p");
566 addargs(&alist
, "%s", argv
[i
]);
567 addargs(&alist
, "%s", argv
[argc
-1]);
568 if (do_local_cmd(&alist
))
575 if ((host
= strrchr(argv
[i
], '@')) == NULL
) {
582 suser
= pwd
->pw_name
;
584 host
= cleanhostname(host
);
585 len
= strlen(src
) + CMDNEEDS
+ 20;
587 (void) snprintf(bp
, len
, "%s -f %s", cmd
, src
);
588 if (do_cmd(host
, suser
, bp
, &remin
, &remout
, argc
) < 0) {
594 sink(1, argv
+ argc
- 1);
601 source(int argc
, char **argv
)
606 off_t i
, amt
, statbytes
;
608 int fd
= -1, haderr
, indx
;
609 char *last
, *name
, buf
[2048];
612 for (indx
= 0; indx
< argc
; ++indx
) {
616 while (len
> 1 && name
[len
-1] == '/')
618 if (strchr(name
, '\n') != NULL
) {
619 run_err("%s: skipping, filename contains a newline",
623 if ((fd
= open(name
, O_RDONLY
, 0)) < 0)
625 if (fstat(fd
, &stb
) < 0) {
626 syserr
: run_err("%s: %s", name
, strerror(errno
));
629 switch (stb
.st_mode
& S_IFMT
) {
639 run_err("%s: not a regular file", name
);
642 if ((last
= strrchr(name
, '/')) == NULL
)
649 * Make it compatible with possible future
650 * versions expecting microseconds.
652 (void) snprintf(buf
, sizeof buf
, "T%lu 0 %lu 0\n",
653 (u_long
) stb
.st_mtime
,
654 (u_long
) stb
.st_atime
);
655 (void) atomicio(vwrite
, remout
, buf
, strlen(buf
));
659 #define FILEMODEMASK (S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO)
660 snprintf(buf
, sizeof buf
, "C%04o %lld %s\n",
661 (u_int
) (stb
.st_mode
& FILEMODEMASK
),
662 (long long)stb
.st_size
, last
);
664 fprintf(stderr
, "Sending file modes: %s", buf
);
666 (void) atomicio(vwrite
, remout
, buf
, strlen(buf
));
669 if ((bp
= allocbuf(&buffer
, fd
, 2048)) == NULL
) {
670 next
: if (fd
!= -1) {
678 start_progress_meter(curfile
, stb
.st_size
, &statbytes
);
680 /* Keep writing after an error so that we stay sync'd up. */
681 for (haderr
= i
= 0; i
< stb
.st_size
; i
+= bp
->cnt
) {
683 if (i
+ amt
> stb
.st_size
)
684 amt
= stb
.st_size
- i
;
686 result
= atomicio(read
, fd
, bp
->buf
, amt
);
691 (void) atomicio(vwrite
, remout
, bp
->buf
, amt
);
693 result
= atomicio(vwrite
, remout
, bp
->buf
, amt
);
701 #ifdef PROGRESS_METER
703 stop_progress_meter();
707 if (close(fd
) < 0 && !haderr
)
712 (void) atomicio(vwrite
, remout
, "", 1);
714 run_err("%s: %s", name
, strerror(haderr
));
720 rsource(char *name
, struct stat
*statp
)
724 char *last
, *vect
[1], path
[1100];
726 if (!(dirp
= opendir(name
))) {
727 run_err("%s: %s", name
, strerror(errno
));
730 last
= strrchr(name
, '/');
736 (void) snprintf(path
, sizeof(path
), "T%lu 0 %lu 0\n",
737 (u_long
) statp
->st_mtime
,
738 (u_long
) statp
->st_atime
);
739 (void) atomicio(vwrite
, remout
, path
, strlen(path
));
740 if (response() < 0) {
745 (void) snprintf(path
, sizeof path
, "D%04o %d %.1024s\n",
746 (u_int
) (statp
->st_mode
& FILEMODEMASK
), 0, last
);
748 fprintf(stderr
, "Entering directory: %s", path
);
749 (void) atomicio(vwrite
, remout
, path
, strlen(path
));
750 if (response() < 0) {
754 while ((dp
= readdir(dirp
)) != NULL
) {
757 if (!strcmp(dp
->d_name
, ".") || !strcmp(dp
->d_name
, ".."))
759 if (strlen(name
) + 1 + strlen(dp
->d_name
) >= sizeof(path
) - 1) {
760 run_err("%s/%s: name too long", name
, dp
->d_name
);
763 (void) snprintf(path
, sizeof path
, "%s/%s", name
, dp
->d_name
);
767 (void) closedir(dirp
);
768 (void) atomicio(vwrite
, remout
, "E\n", 2);
775 static struct timeval bwstart
, bwend
;
776 static int lamt
, thresh
= 16384;
778 struct timespec ts
, rm
;
780 if (!timerisset(&bwstart
)) {
781 gettimeofday(&bwstart
, NULL
);
789 gettimeofday(&bwend
, NULL
);
790 timersub(&bwend
, &bwstart
, &bwend
);
791 if (!timerisset(&bwend
))
795 waitlen
= (double)1000000L * lamt
/ limit_rate
;
797 bwstart
.tv_sec
= waitlen
/ 1000000L;
798 bwstart
.tv_usec
= waitlen
% 1000000L;
800 if (timercmp(&bwstart
, &bwend
, >)) {
801 timersub(&bwstart
, &bwend
, &bwend
);
803 /* Adjust the wait time */
808 } else if (bwend
.tv_usec
< 100) {
814 TIMEVAL_TO_TIMESPEC(&bwend
, &ts
);
815 while (nanosleep(&ts
, &rm
) == -1) {
823 gettimeofday(&bwstart
, NULL
);
827 sink(int argc
, char **argv
)
837 int amt
, exists
, first
, mask
, mode
, ofd
, omode
;
838 off_t size
, statbytes
;
839 int setimes
, targisdir
, wrerrno
= 0;
840 char ch
, *cp
, *np
, *targ
, *why
, *vect
[1], buf
[2048];
841 struct timeval tv
[2];
845 #define SCREWUP(str) { why = str; goto screwup; }
847 setimes
= targisdir
= 0;
852 run_err("ambiguous target");
856 if (targetshouldbedirectory
)
859 (void) atomicio(vwrite
, remout
, "", 1);
860 if (stat(targ
, &stb
) == 0 && S_ISDIR(stb
.st_mode
))
862 for (first
= 1;; first
= 0) {
864 if (atomicio(read
, remin
, cp
, 1) != 1)
867 SCREWUP("unexpected <newline>");
869 if (atomicio(read
, remin
, &ch
, sizeof(ch
)) != sizeof(ch
))
870 SCREWUP("lost connection");
872 } while (cp
< &buf
[sizeof(buf
) - 1] && ch
!= '\n');
875 fprintf(stderr
, "Sink: %s", buf
);
877 if (buf
[0] == '\01' || buf
[0] == '\02') {
879 (void) atomicio(vwrite
, STDERR_FILENO
,
880 buf
+ 1, strlen(buf
+ 1));
887 (void) atomicio(vwrite
, remout
, "", 1);
897 mtime
.tv_sec
= strtol(cp
, &cp
, 10);
898 if (!cp
|| *cp
++ != ' ')
899 SCREWUP("mtime.sec not delimited");
900 mtime
.tv_usec
= strtol(cp
, &cp
, 10);
901 if (!cp
|| *cp
++ != ' ')
902 SCREWUP("mtime.usec not delimited");
903 atime
.tv_sec
= strtol(cp
, &cp
, 10);
904 if (!cp
|| *cp
++ != ' ')
905 SCREWUP("atime.sec not delimited");
906 atime
.tv_usec
= strtol(cp
, &cp
, 10);
907 if (!cp
|| *cp
++ != '\0')
908 SCREWUP("atime.usec not delimited");
909 (void) atomicio(vwrite
, remout
, "", 1);
912 if (*cp
!= 'C' && *cp
!= 'D') {
914 * Check for the case "rcp remote:foo\* local:bar".
915 * In this case, the line "No match." can be returned
916 * by the shell before the rcp command on the remote is
917 * executed so the ^Aerror_message convention isn't
924 SCREWUP("expected control record");
927 for (++cp
; cp
< buf
+ 5; cp
++) {
928 if (*cp
< '0' || *cp
> '7')
930 mode
= (mode
<< 3) | (*cp
- '0');
933 SCREWUP("mode not delimited");
935 for (size
= 0; isdigit(*cp
);)
936 size
= size
* 10 + (*cp
++ - '0');
938 SCREWUP("size not delimited");
939 if ((strchr(cp
, '/') != NULL
) || (strcmp(cp
, "..") == 0)) {
940 run_err("error: unexpected filename: %s", cp
);
944 static char *namebuf
;
945 static size_t cursize
;
948 need
= strlen(targ
) + strlen(cp
) + 250;
949 if (need
> cursize
) {
952 namebuf
= xmalloc(need
);
955 (void) snprintf(namebuf
, need
, "%s%s%s", targ
,
956 strcmp(targ
, "/") ? "/" : "", cp
);
961 exists
= stat(np
, &stb
) == 0;
963 int mod_flag
= pflag
;
965 SCREWUP("received directory without -r");
967 if (!S_ISDIR(stb
.st_mode
)) {
972 (void) chmod(np
, mode
);
974 /* Handle copying from a read-only
977 if (mkdir(np
, mode
| S_IRWXU
) < 0)
980 vect
[0] = xstrdup(np
);
984 if (utimes(vect
[0], tv
) < 0)
985 run_err("%s: set times: %s",
986 vect
[0], strerror(errno
));
989 (void) chmod(vect
[0], mode
);
996 if ((ofd
= open(np
, O_WRONLY
|O_CREAT
, mode
)) < 0) {
997 bad
: run_err("%s: %s", np
, strerror(errno
));
1000 (void) atomicio(vwrite
, remout
, "", 1);
1001 if ((bp
= allocbuf(&buffer
, ofd
, 4096)) == NULL
) {
1009 #ifdef PROGRESS_METER
1011 start_progress_meter(curfile
, size
, &statbytes
);
1013 for (count
= i
= 0; i
< size
; i
+= 4096) {
1019 j
= atomicio(read
, remin
, cp
, amt
);
1021 run_err("%s", j
? strerror(errno
) :
1022 "dropped connection");
1033 if (count
== bp
->cnt
) {
1034 /* Keep reading so we stay sync'd up. */
1036 if (atomicio(vwrite
, ofd
, bp
->buf
,
1046 #ifdef PROGRESS_METER
1048 stop_progress_meter();
1050 if (count
!= 0 && wrerr
== NO
&&
1051 atomicio(vwrite
, ofd
, bp
->buf
, count
) != count
) {
1055 if (wrerr
== NO
&& ftruncate(ofd
, size
) != 0) {
1056 run_err("%s: truncate: %s", np
, strerror(errno
));
1060 if (exists
|| omode
!= mode
)
1062 if (fchmod(ofd
, omode
)) {
1063 #else /* HAVE_FCHMOD */
1064 if (chmod(np
, omode
)) {
1065 #endif /* HAVE_FCHMOD */
1066 run_err("%s: set mode: %s",
1067 np
, strerror(errno
));
1071 if (!exists
&& omode
!= mode
)
1073 if (fchmod(ofd
, omode
& ~mask
)) {
1074 #else /* HAVE_FCHMOD */
1075 if (chmod(np
, omode
& ~mask
)) {
1076 #endif /* HAVE_FCHMOD */
1077 run_err("%s: set mode: %s",
1078 np
, strerror(errno
));
1082 if (close(ofd
) == -1) {
1087 if (setimes
&& wrerr
== NO
) {
1089 if (utimes(np
, tv
) < 0) {
1090 run_err("%s: set times: %s",
1091 np
, strerror(errno
));
1097 run_err("%s: %s", np
, strerror(wrerrno
));
1100 (void) atomicio(vwrite
, remout
, "", 1);
1107 run_err("protocol error: %s", why
);
1114 char ch
, *cp
, resp
, rbuf
[2048];
1116 if (atomicio(read
, remin
, &resp
, sizeof(resp
)) != sizeof(resp
))
1126 case 1: /* error, followed by error msg */
1127 case 2: /* fatal error, "" */
1129 if (atomicio(read
, remin
, &ch
, sizeof(ch
)) != sizeof(ch
))
1132 } while (cp
< &rbuf
[sizeof(rbuf
) - 1] && ch
!= '\n');
1135 (void) atomicio(vwrite
, STDERR_FILENO
, rbuf
, cp
- rbuf
);
1147 (void) fprintf(stderr
,
1148 "usage: scp [-1246BCpqrv] [-c cipher] [-F ssh_config] [-i identity_file]\n"
1149 " [-l limit] [-o ssh_option] [-P port] [-S program]\n"
1150 " [[user@]host1:]file1 [...] [[user@]host2:]file2\n");
1155 run_err(const char *fmt
,...)
1161 if (fp
== NULL
&& !(fp
= fdopen(remout
, "w")))
1163 (void) fprintf(fp
, "%c", 0x01);
1164 (void) fprintf(fp
, "scp: ");
1166 (void) vfprintf(fp
, fmt
, ap
);
1168 (void) fprintf(fp
, "\n");
1173 vfprintf(stderr
, fmt
, ap
);
1175 fprintf(stderr
, "\n");
1184 if (!stat(cp
, &stb
)) {
1185 if (S_ISDIR(stb
.st_mode
))
1189 run_err("%s: %s", cp
, strerror(errno
));
1204 if (!isalpha(c
) && !isdigit(c
)) {
1219 bad
: fprintf(stderr
, "%s: invalid user name\n", cp0
);
1224 allocbuf(BUF
*bp
, int fd
, int blksize
)
1227 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
1230 if (fstat(fd
, &stb
) < 0) {
1231 run_err("fstat: %s", strerror(errno
));
1234 size
= roundup(stb
.st_blksize
, blksize
);
1237 #else /* HAVE_STRUCT_STAT_ST_BLKSIZE */
1239 #endif /* HAVE_STRUCT_STAT_ST_BLKSIZE */
1240 if (bp
->cnt
>= size
)
1242 if (bp
->buf
== NULL
)
1243 bp
->buf
= xmalloc(size
);
1245 bp
->buf
= xrealloc(bp
->buf
, size
);
1246 memset(bp
->buf
, 0, size
);
1255 write(STDERR_FILENO
, "lost connection\n", 16);