1 /* $OpenBSD: scp.c,v 1.168 2010/11/26 05:52:49 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 */
125 arglist remote_remote_args
;
127 /* Bandwidth limit */
128 long long limit_kbps
= 0;
129 struct bwlimit bwlimit
;
131 /* Name of current file being transferred. */
134 /* This is set to non-zero to enable verbose mode. */
135 int verbose_mode
= 0;
137 /* This is set to zero if the progressmeter is not desired. */
138 int showprogress
= 1;
140 /* This is the program to execute for the secured connection. ("ssh" or -S) */
141 char *ssh_program
= _PATH_SSH_PROGRAM
;
143 /* This is used to store the pid of ssh_program */
144 pid_t do_cmd_pid
= -1;
149 if (do_cmd_pid
> 1) {
150 kill(do_cmd_pid
, signo
? signo
: SIGTERM
);
151 waitpid(do_cmd_pid
, NULL
, 0);
164 if (do_cmd_pid
> 1) {
165 kill(do_cmd_pid
, signo
);
166 while (waitpid(do_cmd_pid
, &status
, WUNTRACED
) == -1 &&
169 kill(getpid(), SIGSTOP
);
174 do_local_cmd(arglist
*a
)
181 fatal("do_local_cmd: no arguments");
184 fprintf(stderr
, "Executing:");
185 for (i
= 0; i
< a
->num
; i
++)
186 fprintf(stderr
, " %s", a
->list
[i
]);
187 fprintf(stderr
, "\n");
189 if ((pid
= fork()) == -1)
190 fatal("do_local_cmd: fork: %s", strerror(errno
));
193 execvp(a
->list
[0], a
->list
);
199 signal(SIGTERM
, killchild
);
200 signal(SIGINT
, killchild
);
201 signal(SIGHUP
, killchild
);
203 while (waitpid(pid
, &status
, 0) == -1)
205 fatal("do_local_cmd: waitpid: %s", strerror(errno
));
209 if (!WIFEXITED(status
) || WEXITSTATUS(status
) != 0)
216 * This function executes the given command as the specified user on the
217 * given host. This returns < 0 if execution fails, and >= 0 otherwise. This
218 * assigns the input and output file descriptors on success.
222 do_cmd(char *host
, char *remuser
, char *cmd
, int *fdin
, int *fdout
)
224 int pin
[2], pout
[2], reserved
[2];
228 "Executing: program %s host %s, user %s, command %s\n",
230 remuser
? remuser
: "(unspecified)", cmd
);
233 * Reserve two descriptors so that the real pipes won't get
234 * descriptors 0 and 1 because that will screw up dup2 below.
236 if (pipe(reserved
) < 0)
237 fatal("pipe: %s", strerror(errno
));
239 /* Create a socket pair for communicating with ssh. */
241 fatal("pipe: %s", strerror(errno
));
243 fatal("pipe: %s", strerror(errno
));
245 /* Free the reserved descriptors. */
249 signal(SIGTSTP
, suspchild
);
250 signal(SIGTTIN
, suspchild
);
251 signal(SIGTTOU
, suspchild
);
253 /* Fork a child to execute the command on the remote host using ssh. */
255 if (do_cmd_pid
== 0) {
264 replacearg(&args
, 0, "%s", ssh_program
);
265 if (remuser
!= NULL
) {
266 addargs(&args
, "-l");
267 addargs(&args
, "%s", remuser
);
269 addargs(&args
, "--");
270 addargs(&args
, "%s", host
);
271 addargs(&args
, "%s", cmd
);
273 execvp(ssh_program
, args
.list
);
276 } else if (do_cmd_pid
== -1) {
277 fatal("fork: %s", strerror(errno
));
279 /* Parent. Close the other side, and return the local side. */
284 signal(SIGTERM
, killchild
);
285 signal(SIGINT
, killchild
);
286 signal(SIGHUP
, killchild
);
295 BUF
*allocbuf(BUF
*, int, int);
298 void run_err(const char *,...);
299 void verifydir(char *);
303 int errs
, remin
, remout
;
304 int pflag
, iamremote
, iamrecursive
, targetshouldbedirectory
;
307 char cmd
[CMDNEEDS
]; /* must hold "rcp -r -p -d\0" */
310 void rsource(char *, struct stat
*);
311 void sink(int, char *[]);
312 void source(int, char *[]);
313 void tolocal(int, char *[]);
314 void toremote(char *, int, char *[]);
318 main(int argc
, char **argv
)
320 int ch
, fflag
, tflag
, status
, n
;
321 char *targ
, **newargv
;
326 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
329 /* Copy argv, because we modify it */
330 newargv
= xcalloc(MAX(argc
+ 1, 1), sizeof(*newargv
));
331 for (n
= 0; n
< argc
; n
++)
332 newargv
[n
] = xstrdup(argv
[n
]);
335 __progname
= ssh_get_progname(argv
[0]);
337 memset(&args
, '\0', sizeof(args
));
338 memset(&remote_remote_args
, '\0', sizeof(remote_remote_args
));
339 args
.list
= remote_remote_args
.list
= NULL
;
340 addargs(&args
, "%s", ssh_program
);
341 addargs(&args
, "-x");
342 addargs(&args
, "-oForwardAgent=no");
343 addargs(&args
, "-oPermitLocalCommand=no");
344 addargs(&args
, "-oClearAllForwardings=yes");
347 while ((ch
= getopt(argc
, argv
, "dfl:prtvBCc:i:P:q1246S:o:F:")) != -1)
349 /* User-visible flags. */
355 addargs(&args
, "-%c", ch
);
356 addargs(&remote_remote_args
, "-%c", ch
);
362 addargs(&remote_remote_args
, "-%c", ch
);
363 addargs(&remote_remote_args
, "%s", optarg
);
364 addargs(&args
, "-%c", ch
);
365 addargs(&args
, "%s", optarg
);
368 addargs(&remote_remote_args
, "-p");
369 addargs(&remote_remote_args
, "%s", optarg
);
370 addargs(&args
, "-p");
371 addargs(&args
, "%s", optarg
);
374 addargs(&remote_remote_args
, "-oBatchmode=yes");
375 addargs(&args
, "-oBatchmode=yes");
378 limit_kbps
= strtonum(optarg
, 1, 100 * 1024 * 1024,
382 limit_kbps
*= 1024; /* kbps */
383 bandwidth_limit_init(&bwlimit
, limit_kbps
, COPY_BUFLEN
);
392 ssh_program
= xstrdup(optarg
);
395 addargs(&args
, "-v");
396 addargs(&remote_remote_args
, "-v");
400 addargs(&args
, "-q");
401 addargs(&remote_remote_args
, "-q");
405 /* Server options. */
407 targetshouldbedirectory
= 1;
409 case 'f': /* "from" */
417 setmode(0, O_BINARY
);
426 if ((pwd
= getpwuid(userid
= getuid())) == NULL
)
427 fatal("unknown user %u", (u_int
) userid
);
429 if (!isatty(STDOUT_FILENO
))
432 remin
= STDIN_FILENO
;
433 remout
= STDOUT_FILENO
;
436 /* Follow "protocol", send data. */
449 targetshouldbedirectory
= 1;
453 /* Command to be executed on remote system using "ssh". */
454 (void) snprintf(cmd
, sizeof cmd
, "scp%s%s%s%s",
455 verbose_mode
? " -v" : "",
456 iamrecursive
? " -r" : "", pflag
? " -p" : "",
457 targetshouldbedirectory
? " -d" : "");
459 (void) signal(SIGPIPE
, lostconn
);
461 if ((targ
= colon(argv
[argc
- 1]))) /* Dest is remote host. */
462 toremote(targ
, argc
, argv
);
464 if (targetshouldbedirectory
)
465 verifydir(argv
[argc
- 1]);
466 tolocal(argc
, argv
); /* Dest is local host. */
469 * Finally check the exit status of the ssh process, if one was forked
470 * and no error has occurred yet
472 if (do_cmd_pid
!= -1 && errs
== 0) {
476 (void) close(remout
);
477 if (waitpid(do_cmd_pid
, &status
, 0) == -1)
480 if (!WIFEXITED(status
) || WEXITSTATUS(status
) != 0)
487 /* Callback from atomicio6 to update progress meter and limit bandwidth */
489 scpio(void *_cnt
, size_t s
)
491 off_t
*cnt
= (off_t
*)_cnt
;
495 bandwidth_limit(&bwlimit
, s
);
500 toremote(char *targ
, int argc
, char **argv
)
502 char *bp
, *host
, *src
, *suser
, *thost
, *tuser
, *arg
;
507 memset(&alist
, '\0', sizeof(alist
));
514 arg
= xstrdup(argv
[argc
- 1]);
515 if ((thost
= strrchr(arg
, '@'))) {
526 if (tuser
!= NULL
&& !okname(tuser
)) {
531 for (i
= 0; i
< argc
- 1; i
++) {
532 src
= colon(argv
[i
]);
533 if (src
) { /* remote to remote */
535 addargs(&alist
, "%s", ssh_program
);
536 addargs(&alist
, "-x");
537 addargs(&alist
, "-oClearAllForwardings=yes");
538 addargs(&alist
, "-n");
539 for (j
= 0; j
< remote_remote_args
.num
; j
++) {
540 addargs(&alist
, "%s",
541 remote_remote_args
.list
[j
]);
546 host
= strrchr(argv
[i
], '@');
550 host
= cleanhostname(host
);
553 suser
= pwd
->pw_name
;
554 else if (!okname(suser
))
556 addargs(&alist
, "-l");
557 addargs(&alist
, "%s", suser
);
559 host
= cleanhostname(argv
[i
]);
561 addargs(&alist
, "--");
562 addargs(&alist
, "%s", host
);
563 addargs(&alist
, "%s", cmd
);
564 addargs(&alist
, "%s", src
);
565 addargs(&alist
, "%s%s%s:%s",
566 tuser
? tuser
: "", tuser
? "@" : "",
568 if (do_local_cmd(&alist
) != 0)
570 } else { /* local to remote */
572 xasprintf(&bp
, "%s -t -- %s", cmd
, targ
);
573 host
= cleanhostname(thost
);
574 if (do_cmd(host
, tuser
, bp
, &remin
,
588 tolocal(int argc
, char **argv
)
590 char *bp
, *host
, *src
, *suser
;
594 memset(&alist
, '\0', sizeof(alist
));
597 for (i
= 0; i
< argc
- 1; i
++) {
598 if (!(src
= colon(argv
[i
]))) { /* Local to local. */
600 addargs(&alist
, "%s", _PATH_CP
);
602 addargs(&alist
, "-r");
604 addargs(&alist
, "-p");
605 addargs(&alist
, "--");
606 addargs(&alist
, "%s", argv
[i
]);
607 addargs(&alist
, "%s", argv
[argc
-1]);
608 if (do_local_cmd(&alist
))
615 if ((host
= strrchr(argv
[i
], '@')) == NULL
) {
622 suser
= pwd
->pw_name
;
624 host
= cleanhostname(host
);
625 xasprintf(&bp
, "%s -f -- %s", cmd
, src
);
626 if (do_cmd(host
, suser
, bp
, &remin
, &remout
) < 0) {
632 sink(1, argv
+ argc
- 1);
639 source(int argc
, char **argv
)
646 int fd
= -1, haderr
, indx
;
647 char *last
, *name
, buf
[2048], encname
[MAXPATHLEN
];
650 for (indx
= 0; indx
< argc
; ++indx
) {
654 while (len
> 1 && name
[len
-1] == '/')
656 if ((fd
= open(name
, O_RDONLY
|O_NONBLOCK
, 0)) < 0)
658 if (strchr(name
, '\n') != NULL
) {
659 strnvis(encname
, name
, sizeof(encname
), VIS_NL
);
662 if (fstat(fd
, &stb
) < 0) {
663 syserr
: run_err("%s: %s", name
, strerror(errno
));
666 if (stb
.st_size
< 0) {
667 run_err("%s: %s", name
, "Negative file size");
671 switch (stb
.st_mode
& S_IFMT
) {
681 run_err("%s: not a regular file", name
);
684 if ((last
= strrchr(name
, '/')) == NULL
)
691 * Make it compatible with possible future
692 * versions expecting microseconds.
694 (void) snprintf(buf
, sizeof buf
, "T%lu 0 %lu 0\n",
695 (u_long
) (stb
.st_mtime
< 0 ? 0 : stb
.st_mtime
),
696 (u_long
) (stb
.st_atime
< 0 ? 0 : stb
.st_atime
));
698 fprintf(stderr
, "File mtime %ld atime %ld\n",
699 (long)stb
.st_mtime
, (long)stb
.st_atime
);
700 fprintf(stderr
, "Sending file timestamps: %s",
703 (void) atomicio(vwrite
, remout
, buf
, strlen(buf
));
707 #define FILEMODEMASK (S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO)
708 snprintf(buf
, sizeof buf
, "C%04o %lld %s\n",
709 (u_int
) (stb
.st_mode
& FILEMODEMASK
),
710 (long long)stb
.st_size
, last
);
712 fprintf(stderr
, "Sending file modes: %s", buf
);
714 (void) atomicio(vwrite
, remout
, buf
, strlen(buf
));
717 if ((bp
= allocbuf(&buffer
, fd
, COPY_BUFLEN
)) == NULL
) {
718 next
: if (fd
!= -1) {
725 start_progress_meter(curfile
, stb
.st_size
, &statbytes
);
726 set_nonblock(remout
);
727 for (haderr
= i
= 0; i
< stb
.st_size
; i
+= bp
->cnt
) {
729 if (i
+ (off_t
)amt
> stb
.st_size
)
730 amt
= stb
.st_size
- i
;
732 if (atomicio(read
, fd
, bp
->buf
, amt
) != amt
)
735 /* Keep writing after error to retain sync */
737 (void)atomicio(vwrite
, remout
, bp
->buf
, amt
);
740 if (atomicio6(vwrite
, remout
, bp
->buf
, amt
, scpio
,
744 unset_nonblock(remout
);
746 stop_progress_meter();
749 if (close(fd
) < 0 && !haderr
)
754 (void) atomicio(vwrite
, remout
, "", 1);
756 run_err("%s: %s", name
, strerror(haderr
));
762 rsource(char *name
, struct stat
*statp
)
766 char *last
, *vect
[1], path
[1100];
768 if (!(dirp
= opendir(name
))) {
769 run_err("%s: %s", name
, strerror(errno
));
772 last
= strrchr(name
, '/');
778 (void) snprintf(path
, sizeof(path
), "T%lu 0 %lu 0\n",
779 (u_long
) statp
->st_mtime
,
780 (u_long
) statp
->st_atime
);
781 (void) atomicio(vwrite
, remout
, path
, strlen(path
));
782 if (response() < 0) {
787 (void) snprintf(path
, sizeof path
, "D%04o %d %.1024s\n",
788 (u_int
) (statp
->st_mode
& FILEMODEMASK
), 0, last
);
790 fprintf(stderr
, "Entering directory: %s", path
);
791 (void) atomicio(vwrite
, remout
, path
, strlen(path
));
792 if (response() < 0) {
796 while ((dp
= readdir(dirp
)) != NULL
) {
799 if (!strcmp(dp
->d_name
, ".") || !strcmp(dp
->d_name
, ".."))
801 if (strlen(name
) + 1 + strlen(dp
->d_name
) >= sizeof(path
) - 1) {
802 run_err("%s/%s: name too long", name
, dp
->d_name
);
805 (void) snprintf(path
, sizeof path
, "%s/%s", name
, dp
->d_name
);
809 (void) closedir(dirp
);
810 (void) atomicio(vwrite
, remout
, "E\n", 2);
815 sink(int argc
, char **argv
)
825 int amt
, exists
, first
, ofd
;
826 mode_t mode
, omode
, mask
;
827 off_t size
, statbytes
;
828 int setimes
, targisdir
, wrerrno
= 0;
829 char ch
, *cp
, *np
, *targ
, *why
, *vect
[1], buf
[2048];
830 struct timeval tv
[2];
834 #define SCREWUP(str) { why = str; goto screwup; }
836 setimes
= targisdir
= 0;
841 run_err("ambiguous target");
845 if (targetshouldbedirectory
)
848 (void) atomicio(vwrite
, remout
, "", 1);
849 if (stat(targ
, &stb
) == 0 && S_ISDIR(stb
.st_mode
))
851 for (first
= 1;; first
= 0) {
853 if (atomicio(read
, remin
, cp
, 1) != 1)
856 SCREWUP("unexpected <newline>");
858 if (atomicio(read
, remin
, &ch
, sizeof(ch
)) != sizeof(ch
))
859 SCREWUP("lost connection");
861 } while (cp
< &buf
[sizeof(buf
) - 1] && ch
!= '\n');
864 fprintf(stderr
, "Sink: %s", buf
);
866 if (buf
[0] == '\01' || buf
[0] == '\02') {
868 (void) atomicio(vwrite
, STDERR_FILENO
,
869 buf
+ 1, strlen(buf
+ 1));
876 (void) atomicio(vwrite
, remout
, "", 1);
886 mtime
.tv_sec
= strtol(cp
, &cp
, 10);
887 if (!cp
|| *cp
++ != ' ')
888 SCREWUP("mtime.sec not delimited");
889 mtime
.tv_usec
= strtol(cp
, &cp
, 10);
890 if (!cp
|| *cp
++ != ' ')
891 SCREWUP("mtime.usec not delimited");
892 atime
.tv_sec
= strtol(cp
, &cp
, 10);
893 if (!cp
|| *cp
++ != ' ')
894 SCREWUP("atime.sec not delimited");
895 atime
.tv_usec
= strtol(cp
, &cp
, 10);
896 if (!cp
|| *cp
++ != '\0')
897 SCREWUP("atime.usec not delimited");
898 (void) atomicio(vwrite
, remout
, "", 1);
901 if (*cp
!= 'C' && *cp
!= 'D') {
903 * Check for the case "rcp remote:foo\* local:bar".
904 * In this case, the line "No match." can be returned
905 * by the shell before the rcp command on the remote is
906 * executed so the ^Aerror_message convention isn't
913 SCREWUP("expected control record");
916 for (++cp
; cp
< buf
+ 5; cp
++) {
917 if (*cp
< '0' || *cp
> '7')
919 mode
= (mode
<< 3) | (*cp
- '0');
922 SCREWUP("mode not delimited");
924 for (size
= 0; isdigit(*cp
);)
925 size
= size
* 10 + (*cp
++ - '0');
927 SCREWUP("size not delimited");
928 if ((strchr(cp
, '/') != NULL
) || (strcmp(cp
, "..") == 0)) {
929 run_err("error: unexpected filename: %s", cp
);
933 static char *namebuf
;
934 static size_t cursize
;
937 need
= strlen(targ
) + strlen(cp
) + 250;
938 if (need
> cursize
) {
941 namebuf
= xmalloc(need
);
944 (void) snprintf(namebuf
, need
, "%s%s%s", targ
,
945 strcmp(targ
, "/") ? "/" : "", cp
);
950 exists
= stat(np
, &stb
) == 0;
952 int mod_flag
= pflag
;
954 SCREWUP("received directory without -r");
956 if (!S_ISDIR(stb
.st_mode
)) {
961 (void) chmod(np
, mode
);
963 /* Handle copying from a read-only
966 if (mkdir(np
, mode
| S_IRWXU
) < 0)
969 vect
[0] = xstrdup(np
);
973 if (utimes(vect
[0], tv
) < 0)
974 run_err("%s: set times: %s",
975 vect
[0], strerror(errno
));
978 (void) chmod(vect
[0], mode
);
985 if ((ofd
= open(np
, O_WRONLY
|O_CREAT
, mode
)) < 0) {
986 bad
: run_err("%s: %s", np
, strerror(errno
));
989 (void) atomicio(vwrite
, remout
, "", 1);
990 if ((bp
= allocbuf(&buffer
, ofd
, COPY_BUFLEN
)) == NULL
) {
999 start_progress_meter(curfile
, size
, &statbytes
);
1000 set_nonblock(remin
);
1001 for (count
= i
= 0; i
< size
; i
+= bp
->cnt
) {
1007 j
= atomicio6(read
, remin
, cp
, amt
,
1010 run_err("%s", j
!= EPIPE
?
1012 "dropped connection");
1019 if (count
== bp
->cnt
) {
1020 /* Keep reading so we stay sync'd up. */
1022 if (atomicio(vwrite
, ofd
, bp
->buf
,
1032 unset_nonblock(remin
);
1034 stop_progress_meter();
1035 if (count
!= 0 && wrerr
== NO
&&
1036 atomicio(vwrite
, ofd
, bp
->buf
, count
) != count
) {
1040 if (wrerr
== NO
&& (!exists
|| S_ISREG(stb
.st_mode
)) &&
1041 ftruncate(ofd
, size
) != 0) {
1042 run_err("%s: truncate: %s", np
, strerror(errno
));
1046 if (exists
|| omode
!= mode
)
1048 if (fchmod(ofd
, omode
)) {
1049 #else /* HAVE_FCHMOD */
1050 if (chmod(np
, omode
)) {
1051 #endif /* HAVE_FCHMOD */
1052 run_err("%s: set mode: %s",
1053 np
, strerror(errno
));
1057 if (!exists
&& omode
!= mode
)
1059 if (fchmod(ofd
, omode
& ~mask
)) {
1060 #else /* HAVE_FCHMOD */
1061 if (chmod(np
, omode
& ~mask
)) {
1062 #endif /* HAVE_FCHMOD */
1063 run_err("%s: set mode: %s",
1064 np
, strerror(errno
));
1068 if (close(ofd
) == -1) {
1073 if (setimes
&& wrerr
== NO
) {
1075 if (utimes(np
, tv
) < 0) {
1076 run_err("%s: set times: %s",
1077 np
, strerror(errno
));
1083 run_err("%s: %s", np
, strerror(wrerrno
));
1086 (void) atomicio(vwrite
, remout
, "", 1);
1093 run_err("protocol error: %s", why
);
1100 char ch
, *cp
, resp
, rbuf
[2048];
1102 if (atomicio(read
, remin
, &resp
, sizeof(resp
)) != sizeof(resp
))
1112 case 1: /* error, followed by error msg */
1113 case 2: /* fatal error, "" */
1115 if (atomicio(read
, remin
, &ch
, sizeof(ch
)) != sizeof(ch
))
1118 } while (cp
< &rbuf
[sizeof(rbuf
) - 1] && ch
!= '\n');
1121 (void) atomicio(vwrite
, STDERR_FILENO
, rbuf
, cp
- rbuf
);
1133 (void) fprintf(stderr
,
1134 "usage: scp [-1246BCpqrv] [-c cipher] [-F ssh_config] [-i identity_file]\n"
1135 " [-l limit] [-o ssh_option] [-P port] [-S program]\n"
1136 " [[user@]host1:]file1 ... [[user@]host2:]file2\n");
1141 run_err(const char *fmt
,...)
1147 if (fp
!= NULL
|| (remout
!= -1 && (fp
= fdopen(remout
, "w")))) {
1148 (void) fprintf(fp
, "%c", 0x01);
1149 (void) fprintf(fp
, "scp: ");
1151 (void) vfprintf(fp
, fmt
, ap
);
1153 (void) fprintf(fp
, "\n");
1159 vfprintf(stderr
, fmt
, ap
);
1161 fprintf(stderr
, "\n");
1170 if (!stat(cp
, &stb
)) {
1171 if (S_ISDIR(stb
.st_mode
))
1175 run_err("%s: %s", cp
, strerror(errno
));
1190 if (!isalpha(c
) && !isdigit(c
)) {
1205 bad
: fprintf(stderr
, "%s: invalid user name\n", cp0
);
1210 allocbuf(BUF
*bp
, int fd
, int blksize
)
1213 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
1216 if (fstat(fd
, &stb
) < 0) {
1217 run_err("fstat: %s", strerror(errno
));
1220 size
= roundup(stb
.st_blksize
, blksize
);
1223 #else /* HAVE_STRUCT_STAT_ST_BLKSIZE */
1225 #endif /* HAVE_STRUCT_STAT_ST_BLKSIZE */
1226 if (bp
->cnt
>= size
)
1228 if (bp
->buf
== NULL
)
1229 bp
->buf
= xmalloc(size
);
1231 bp
->buf
= xrealloc(bp
->buf
, 1, size
);
1232 memset(bp
->buf
, 0, size
);
1241 write(STDERR_FILENO
, "lost connection\n", 16);