1 /* $OpenBSD: scp.c,v 1.148 2006/07/17 01:31:09 stevesk 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 #ifdef HAVE_SYS_STAT_H
78 # include <sys/stat.h>
93 #include "pathnames.h"
96 #include "progressmeter.h"
98 extern char *__progname
;
100 int do_cmd(char *host
, char *remuser
, char *cmd
, int *fdin
, int *fdout
);
104 /* Struct for addargs */
107 /* Bandwidth limit */
108 off_t limit_rate
= 0;
110 /* Name of current file being transferred. */
113 /* This is set to non-zero to enable verbose mode. */
114 int verbose_mode
= 0;
116 /* This is set to zero if the progressmeter is not desired. */
117 int showprogress
= 1;
119 /* This is the program to execute for the secured connection. ("ssh" or -S) */
120 char *ssh_program
= _PATH_SSH_PROGRAM
;
122 /* This is used to store the pid of ssh_program */
123 pid_t do_cmd_pid
= -1;
128 if (do_cmd_pid
> 1) {
129 kill(do_cmd_pid
, signo
? signo
: SIGTERM
);
130 waitpid(do_cmd_pid
, NULL
, 0);
139 do_local_cmd(arglist
*a
)
146 fatal("do_local_cmd: no arguments");
149 fprintf(stderr
, "Executing:");
150 for (i
= 0; i
< a
->num
; i
++)
151 fprintf(stderr
, " %s", a
->list
[i
]);
152 fprintf(stderr
, "\n");
154 if ((pid
= fork()) == -1)
155 fatal("do_local_cmd: fork: %s", strerror(errno
));
158 execvp(a
->list
[0], a
->list
);
164 signal(SIGTERM
, killchild
);
165 signal(SIGINT
, killchild
);
166 signal(SIGHUP
, killchild
);
168 while (waitpid(pid
, &status
, 0) == -1)
170 fatal("do_local_cmd: waitpid: %s", strerror(errno
));
174 if (!WIFEXITED(status
) || WEXITSTATUS(status
) != 0)
181 * This function executes the given command as the specified user on the
182 * given host. This returns < 0 if execution fails, and >= 0 otherwise. This
183 * assigns the input and output file descriptors on success.
187 do_cmd(char *host
, char *remuser
, char *cmd
, int *fdin
, int *fdout
)
189 int pin
[2], pout
[2], reserved
[2];
193 "Executing: program %s host %s, user %s, command %s\n",
195 remuser
? remuser
: "(unspecified)", cmd
);
198 * Reserve two descriptors so that the real pipes won't get
199 * descriptors 0 and 1 because that will screw up dup2 below.
201 if (pipe(reserved
) < 0)
202 fatal("pipe: %s", strerror(errno
));
204 /* Create a socket pair for communicating with ssh. */
206 fatal("pipe: %s", strerror(errno
));
208 fatal("pipe: %s", strerror(errno
));
210 /* Free the reserved descriptors. */
214 /* Fork a child to execute the command on the remote host using ssh. */
216 if (do_cmd_pid
== 0) {
225 replacearg(&args
, 0, "%s", ssh_program
);
227 addargs(&args
, "-l%s", remuser
);
228 addargs(&args
, "%s", host
);
229 addargs(&args
, "%s", cmd
);
231 execvp(ssh_program
, args
.list
);
234 } else if (do_cmd_pid
== -1) {
235 fatal("fork: %s", strerror(errno
));
237 /* Parent. Close the other side, and return the local side. */
242 signal(SIGTERM
, killchild
);
243 signal(SIGINT
, killchild
);
244 signal(SIGHUP
, killchild
);
253 BUF
*allocbuf(BUF
*, int, int);
256 void run_err(const char *,...);
257 void verifydir(char *);
261 int errs
, remin
, remout
;
262 int pflag
, iamremote
, iamrecursive
, targetshouldbedirectory
;
265 char cmd
[CMDNEEDS
]; /* must hold "rcp -r -p -d\0" */
268 void rsource(char *, struct stat
*);
269 void sink(int, char *[]);
270 void source(int, char *[]);
271 void tolocal(int, char *[]);
272 void toremote(char *, int, char *[]);
276 main(int argc
, char **argv
)
278 int ch
, fflag
, tflag
, status
, n
;
280 char *targ
, *endp
, **newargv
;
284 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
287 /* Copy argv, because we modify it */
288 newargv
= xcalloc(MAX(argc
+ 1, 1), sizeof(*newargv
));
289 for (n
= 0; n
< argc
; n
++)
290 newargv
[n
] = xstrdup(argv
[n
]);
293 __progname
= ssh_get_progname(argv
[0]);
295 memset(&args
, '\0', sizeof(args
));
297 addargs(&args
, "%s", ssh_program
);
298 addargs(&args
, "-x");
299 addargs(&args
, "-oForwardAgent no");
300 addargs(&args
, "-oPermitLocalCommand no");
301 addargs(&args
, "-oClearAllForwardings yes");
304 while ((ch
= getopt(argc
, argv
, "dfl:prtvBCc:i:P:q1246S:o:F:")) != -1)
306 /* User-visible flags. */
312 addargs(&args
, "-%c", ch
);
318 addargs(&args
, "-%c%s", ch
, optarg
);
321 addargs(&args
, "-p%s", optarg
);
324 addargs(&args
, "-oBatchmode yes");
327 speed
= strtod(optarg
, &endp
);
328 if (speed
<= 0 || *endp
!= '\0')
330 limit_rate
= speed
* 1024;
339 ssh_program
= xstrdup(optarg
);
342 addargs(&args
, "-v");
346 addargs(&args
, "-q");
350 /* Server options. */
352 targetshouldbedirectory
= 1;
354 case 'f': /* "from" */
362 setmode(0, O_BINARY
);
371 if ((pwd
= getpwuid(userid
= getuid())) == NULL
)
372 fatal("unknown user %u", (u_int
) userid
);
374 if (!isatty(STDERR_FILENO
))
377 remin
= STDIN_FILENO
;
378 remout
= STDOUT_FILENO
;
381 /* Follow "protocol", send data. */
394 targetshouldbedirectory
= 1;
398 /* Command to be executed on remote system using "ssh". */
399 (void) snprintf(cmd
, sizeof cmd
, "scp%s%s%s%s",
400 verbose_mode
? " -v" : "",
401 iamrecursive
? " -r" : "", pflag
? " -p" : "",
402 targetshouldbedirectory
? " -d" : "");
404 (void) signal(SIGPIPE
, lostconn
);
406 if ((targ
= colon(argv
[argc
- 1]))) /* Dest is remote host. */
407 toremote(targ
, argc
, argv
);
409 if (targetshouldbedirectory
)
410 verifydir(argv
[argc
- 1]);
411 tolocal(argc
, argv
); /* Dest is local host. */
414 * Finally check the exit status of the ssh process, if one was forked
415 * and no error has occured yet
417 if (do_cmd_pid
!= -1 && errs
== 0) {
421 (void) close(remout
);
422 if (waitpid(do_cmd_pid
, &status
, 0) == -1)
425 if (!WIFEXITED(status
) || WEXITSTATUS(status
) != 0)
433 toremote(char *targ
, int argc
, char **argv
)
435 char *bp
, *host
, *src
, *suser
, *thost
, *tuser
, *arg
;
439 memset(&alist
, '\0', sizeof(alist
));
446 arg
= xstrdup(argv
[argc
- 1]);
447 if ((thost
= strrchr(arg
, '@'))) {
458 if (tuser
!= NULL
&& !okname(tuser
)) {
463 for (i
= 0; i
< argc
- 1; i
++) {
464 src
= colon(argv
[i
]);
465 if (src
) { /* remote to remote */
467 addargs(&alist
, "%s", ssh_program
);
469 addargs(&alist
, "-v");
470 addargs(&alist
, "-x");
471 addargs(&alist
, "-oClearAllForwardings yes");
472 addargs(&alist
, "-n");
477 host
= strrchr(argv
[i
], '@');
481 host
= cleanhostname(host
);
484 suser
= pwd
->pw_name
;
485 else if (!okname(suser
))
487 addargs(&alist
, "-l");
488 addargs(&alist
, "%s", suser
);
490 host
= cleanhostname(argv
[i
]);
492 addargs(&alist
, "%s", host
);
493 addargs(&alist
, "%s", cmd
);
494 addargs(&alist
, "%s", src
);
495 addargs(&alist
, "%s%s%s:%s",
496 tuser
? tuser
: "", tuser
? "@" : "",
498 if (do_local_cmd(&alist
) != 0)
500 } else { /* local to remote */
502 xasprintf(&bp
, "%s -t %s", cmd
, targ
);
503 host
= cleanhostname(thost
);
504 if (do_cmd(host
, tuser
, bp
, &remin
,
518 tolocal(int argc
, char **argv
)
520 char *bp
, *host
, *src
, *suser
;
524 memset(&alist
, '\0', sizeof(alist
));
527 for (i
= 0; i
< argc
- 1; i
++) {
528 if (!(src
= colon(argv
[i
]))) { /* Local to local. */
530 addargs(&alist
, "%s", _PATH_CP
);
532 addargs(&alist
, "-r");
534 addargs(&alist
, "-p");
535 addargs(&alist
, "%s", argv
[i
]);
536 addargs(&alist
, "%s", argv
[argc
-1]);
537 if (do_local_cmd(&alist
))
544 if ((host
= strrchr(argv
[i
], '@')) == NULL
) {
551 suser
= pwd
->pw_name
;
553 host
= cleanhostname(host
);
554 xasprintf(&bp
, "%s -f %s", cmd
, src
);
555 if (do_cmd(host
, suser
, bp
, &remin
, &remout
) < 0) {
561 sink(1, argv
+ argc
- 1);
568 source(int argc
, char **argv
)
573 off_t i
, amt
, statbytes
;
575 int fd
= -1, haderr
, indx
;
576 char *last
, *name
, buf
[2048];
579 for (indx
= 0; indx
< argc
; ++indx
) {
583 while (len
> 1 && name
[len
-1] == '/')
585 if (strchr(name
, '\n') != NULL
) {
586 run_err("%s: skipping, filename contains a newline",
590 if ((fd
= open(name
, O_RDONLY
, 0)) < 0)
592 if (fstat(fd
, &stb
) < 0) {
593 syserr
: run_err("%s: %s", name
, strerror(errno
));
596 switch (stb
.st_mode
& S_IFMT
) {
606 run_err("%s: not a regular file", name
);
609 if ((last
= strrchr(name
, '/')) == NULL
)
616 * Make it compatible with possible future
617 * versions expecting microseconds.
619 (void) snprintf(buf
, sizeof buf
, "T%lu 0 %lu 0\n",
620 (u_long
) stb
.st_mtime
,
621 (u_long
) stb
.st_atime
);
622 (void) atomicio(vwrite
, remout
, buf
, strlen(buf
));
626 #define FILEMODEMASK (S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO)
627 snprintf(buf
, sizeof buf
, "C%04o %lld %s\n",
628 (u_int
) (stb
.st_mode
& FILEMODEMASK
),
629 (long long)stb
.st_size
, last
);
631 fprintf(stderr
, "Sending file modes: %s", buf
);
633 (void) atomicio(vwrite
, remout
, buf
, strlen(buf
));
636 if ((bp
= allocbuf(&buffer
, fd
, 2048)) == NULL
) {
637 next
: if (fd
!= -1) {
644 start_progress_meter(curfile
, stb
.st_size
, &statbytes
);
645 /* Keep writing after an error so that we stay sync'd up. */
646 for (haderr
= i
= 0; i
< stb
.st_size
; i
+= bp
->cnt
) {
648 if (i
+ amt
> stb
.st_size
)
649 amt
= stb
.st_size
- i
;
651 result
= atomicio(read
, fd
, bp
->buf
, amt
);
656 (void) atomicio(vwrite
, remout
, bp
->buf
, amt
);
658 result
= atomicio(vwrite
, remout
, bp
->buf
, amt
);
667 stop_progress_meter();
670 if (close(fd
) < 0 && !haderr
)
675 (void) atomicio(vwrite
, remout
, "", 1);
677 run_err("%s: %s", name
, strerror(haderr
));
683 rsource(char *name
, struct stat
*statp
)
687 char *last
, *vect
[1], path
[1100];
689 if (!(dirp
= opendir(name
))) {
690 run_err("%s: %s", name
, strerror(errno
));
693 last
= strrchr(name
, '/');
699 (void) snprintf(path
, sizeof(path
), "T%lu 0 %lu 0\n",
700 (u_long
) statp
->st_mtime
,
701 (u_long
) statp
->st_atime
);
702 (void) atomicio(vwrite
, remout
, path
, strlen(path
));
703 if (response() < 0) {
708 (void) snprintf(path
, sizeof path
, "D%04o %d %.1024s\n",
709 (u_int
) (statp
->st_mode
& FILEMODEMASK
), 0, last
);
711 fprintf(stderr
, "Entering directory: %s", path
);
712 (void) atomicio(vwrite
, remout
, path
, strlen(path
));
713 if (response() < 0) {
717 while ((dp
= readdir(dirp
)) != NULL
) {
720 if (!strcmp(dp
->d_name
, ".") || !strcmp(dp
->d_name
, ".."))
722 if (strlen(name
) + 1 + strlen(dp
->d_name
) >= sizeof(path
) - 1) {
723 run_err("%s/%s: name too long", name
, dp
->d_name
);
726 (void) snprintf(path
, sizeof path
, "%s/%s", name
, dp
->d_name
);
730 (void) closedir(dirp
);
731 (void) atomicio(vwrite
, remout
, "E\n", 2);
738 static struct timeval bwstart
, bwend
;
739 static int lamt
, thresh
= 16384;
741 struct timespec ts
, rm
;
743 if (!timerisset(&bwstart
)) {
744 gettimeofday(&bwstart
, NULL
);
752 gettimeofday(&bwend
, NULL
);
753 timersub(&bwend
, &bwstart
, &bwend
);
754 if (!timerisset(&bwend
))
758 waitlen
= (double)1000000L * lamt
/ limit_rate
;
760 bwstart
.tv_sec
= waitlen
/ 1000000L;
761 bwstart
.tv_usec
= waitlen
% 1000000L;
763 if (timercmp(&bwstart
, &bwend
, >)) {
764 timersub(&bwstart
, &bwend
, &bwend
);
766 /* Adjust the wait time */
771 } else if (bwend
.tv_usec
< 100) {
777 TIMEVAL_TO_TIMESPEC(&bwend
, &ts
);
778 while (nanosleep(&ts
, &rm
) == -1) {
786 gettimeofday(&bwstart
, NULL
);
790 sink(int argc
, char **argv
)
800 int amt
, exists
, first
, ofd
;
801 mode_t mode
, omode
, mask
;
802 off_t size
, statbytes
;
803 int setimes
, targisdir
, wrerrno
= 0;
804 char ch
, *cp
, *np
, *targ
, *why
, *vect
[1], buf
[2048];
805 struct timeval tv
[2];
809 #define SCREWUP(str) { why = str; goto screwup; }
811 setimes
= targisdir
= 0;
816 run_err("ambiguous target");
820 if (targetshouldbedirectory
)
823 (void) atomicio(vwrite
, remout
, "", 1);
824 if (stat(targ
, &stb
) == 0 && S_ISDIR(stb
.st_mode
))
826 for (first
= 1;; first
= 0) {
828 if (atomicio(read
, remin
, cp
, 1) != 1)
831 SCREWUP("unexpected <newline>");
833 if (atomicio(read
, remin
, &ch
, sizeof(ch
)) != sizeof(ch
))
834 SCREWUP("lost connection");
836 } while (cp
< &buf
[sizeof(buf
) - 1] && ch
!= '\n');
839 fprintf(stderr
, "Sink: %s", buf
);
841 if (buf
[0] == '\01' || buf
[0] == '\02') {
843 (void) atomicio(vwrite
, STDERR_FILENO
,
844 buf
+ 1, strlen(buf
+ 1));
851 (void) atomicio(vwrite
, remout
, "", 1);
861 mtime
.tv_sec
= strtol(cp
, &cp
, 10);
862 if (!cp
|| *cp
++ != ' ')
863 SCREWUP("mtime.sec not delimited");
864 mtime
.tv_usec
= strtol(cp
, &cp
, 10);
865 if (!cp
|| *cp
++ != ' ')
866 SCREWUP("mtime.usec not delimited");
867 atime
.tv_sec
= strtol(cp
, &cp
, 10);
868 if (!cp
|| *cp
++ != ' ')
869 SCREWUP("atime.sec not delimited");
870 atime
.tv_usec
= strtol(cp
, &cp
, 10);
871 if (!cp
|| *cp
++ != '\0')
872 SCREWUP("atime.usec not delimited");
873 (void) atomicio(vwrite
, remout
, "", 1);
876 if (*cp
!= 'C' && *cp
!= 'D') {
878 * Check for the case "rcp remote:foo\* local:bar".
879 * In this case, the line "No match." can be returned
880 * by the shell before the rcp command on the remote is
881 * executed so the ^Aerror_message convention isn't
888 SCREWUP("expected control record");
891 for (++cp
; cp
< buf
+ 5; cp
++) {
892 if (*cp
< '0' || *cp
> '7')
894 mode
= (mode
<< 3) | (*cp
- '0');
897 SCREWUP("mode not delimited");
899 for (size
= 0; isdigit(*cp
);)
900 size
= size
* 10 + (*cp
++ - '0');
902 SCREWUP("size not delimited");
903 if ((strchr(cp
, '/') != NULL
) || (strcmp(cp
, "..") == 0)) {
904 run_err("error: unexpected filename: %s", cp
);
908 static char *namebuf
;
909 static size_t cursize
;
912 need
= strlen(targ
) + strlen(cp
) + 250;
913 if (need
> cursize
) {
916 namebuf
= xmalloc(need
);
919 (void) snprintf(namebuf
, need
, "%s%s%s", targ
,
920 strcmp(targ
, "/") ? "/" : "", cp
);
925 exists
= stat(np
, &stb
) == 0;
927 int mod_flag
= pflag
;
929 SCREWUP("received directory without -r");
931 if (!S_ISDIR(stb
.st_mode
)) {
936 (void) chmod(np
, mode
);
938 /* Handle copying from a read-only
941 if (mkdir(np
, mode
| S_IRWXU
) < 0)
944 vect
[0] = xstrdup(np
);
948 if (utimes(vect
[0], tv
) < 0)
949 run_err("%s: set times: %s",
950 vect
[0], strerror(errno
));
953 (void) chmod(vect
[0], mode
);
960 if ((ofd
= open(np
, O_WRONLY
|O_CREAT
, mode
)) < 0) {
961 bad
: run_err("%s: %s", np
, strerror(errno
));
964 (void) atomicio(vwrite
, remout
, "", 1);
965 if ((bp
= allocbuf(&buffer
, ofd
, 4096)) == NULL
) {
974 start_progress_meter(curfile
, size
, &statbytes
);
975 for (count
= i
= 0; i
< size
; i
+= 4096) {
981 j
= atomicio(read
, remin
, cp
, amt
);
983 run_err("%s", j
? strerror(errno
) :
984 "dropped connection");
995 if (count
== bp
->cnt
) {
996 /* Keep reading so we stay sync'd up. */
998 if (atomicio(vwrite
, ofd
, bp
->buf
,
1009 stop_progress_meter();
1010 if (count
!= 0 && wrerr
== NO
&&
1011 atomicio(vwrite
, ofd
, bp
->buf
, count
) != count
) {
1015 if (wrerr
== NO
&& ftruncate(ofd
, size
) != 0) {
1016 run_err("%s: truncate: %s", np
, strerror(errno
));
1020 if (exists
|| omode
!= mode
)
1022 if (fchmod(ofd
, omode
)) {
1023 #else /* HAVE_FCHMOD */
1024 if (chmod(np
, omode
)) {
1025 #endif /* HAVE_FCHMOD */
1026 run_err("%s: set mode: %s",
1027 np
, strerror(errno
));
1031 if (!exists
&& omode
!= mode
)
1033 if (fchmod(ofd
, omode
& ~mask
)) {
1034 #else /* HAVE_FCHMOD */
1035 if (chmod(np
, omode
& ~mask
)) {
1036 #endif /* HAVE_FCHMOD */
1037 run_err("%s: set mode: %s",
1038 np
, strerror(errno
));
1042 if (close(ofd
) == -1) {
1047 if (setimes
&& wrerr
== NO
) {
1049 if (utimes(np
, tv
) < 0) {
1050 run_err("%s: set times: %s",
1051 np
, strerror(errno
));
1057 run_err("%s: %s", np
, strerror(wrerrno
));
1060 (void) atomicio(vwrite
, remout
, "", 1);
1067 run_err("protocol error: %s", why
);
1074 char ch
, *cp
, resp
, rbuf
[2048];
1076 if (atomicio(read
, remin
, &resp
, sizeof(resp
)) != sizeof(resp
))
1086 case 1: /* error, followed by error msg */
1087 case 2: /* fatal error, "" */
1089 if (atomicio(read
, remin
, &ch
, sizeof(ch
)) != sizeof(ch
))
1092 } while (cp
< &rbuf
[sizeof(rbuf
) - 1] && ch
!= '\n');
1095 (void) atomicio(vwrite
, STDERR_FILENO
, rbuf
, cp
- rbuf
);
1107 (void) fprintf(stderr
,
1108 "usage: scp [-1246BCpqrv] [-c cipher] [-F ssh_config] [-i identity_file]\n"
1109 " [-l limit] [-o ssh_option] [-P port] [-S program]\n"
1110 " [[user@]host1:]file1 [...] [[user@]host2:]file2\n");
1115 run_err(const char *fmt
,...)
1121 if (fp
!= NULL
|| (remout
!= -1 && (fp
= fdopen(remout
, "w")))) {
1122 (void) fprintf(fp
, "%c", 0x01);
1123 (void) fprintf(fp
, "scp: ");
1125 (void) vfprintf(fp
, fmt
, ap
);
1127 (void) fprintf(fp
, "\n");
1133 vfprintf(stderr
, fmt
, ap
);
1135 fprintf(stderr
, "\n");
1144 if (!stat(cp
, &stb
)) {
1145 if (S_ISDIR(stb
.st_mode
))
1149 run_err("%s: %s", cp
, strerror(errno
));
1164 if (!isalpha(c
) && !isdigit(c
)) {
1179 bad
: fprintf(stderr
, "%s: invalid user name\n", cp0
);
1184 allocbuf(BUF
*bp
, int fd
, int blksize
)
1187 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
1190 if (fstat(fd
, &stb
) < 0) {
1191 run_err("fstat: %s", strerror(errno
));
1194 size
= roundup(stb
.st_blksize
, blksize
);
1197 #else /* HAVE_STRUCT_STAT_ST_BLKSIZE */
1199 #endif /* HAVE_STRUCT_STAT_ST_BLKSIZE */
1200 if (bp
->cnt
>= size
)
1202 if (bp
->buf
== NULL
)
1203 bp
->buf
= xmalloc(size
);
1205 bp
->buf
= xrealloc(bp
->buf
, 1, size
);
1206 memset(bp
->buf
, 0, size
);
1215 write(STDERR_FILENO
, "lost connection\n", 16);