1 /* $NetBSD: scp.c,v 1.2 2009/06/07 22:38:47 christos Exp $ */
2 /* $OpenBSD: scp.c,v 1.164 2008/10/10 04:55:16 stevesk Exp $ */
4 * scp - secure remote copy. This is basically patched BSD rcp which
5 * uses ssh to do the data transfer (instead of using rcmd).
7 * NOTE: This version should NOT be suid root. (This uses ssh to
8 * do the transfer and ssh has the necessary privileges.)
10 * 1995 Timo Rinne <tri@iki.fi>, Tatu Ylonen <ylo@cs.hut.fi>
12 * As far as I am concerned, the code I have written for this software
13 * can be used freely for any purpose. Any derived versions of this
14 * software must be clearly marked as such, and if the derived work is
15 * incompatible with the protocol description in the RFC file, it must be
16 * called by a name other than "ssh" or "Secure Shell".
19 * Copyright (c) 1999 Theo de Raadt. All rights reserved.
20 * Copyright (c) 1999 Aaron Campbell. All rights reserved.
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
25 * 1. Redistributions of source code must retain the above copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
31 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
32 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
33 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
34 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
35 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
37 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
38 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
39 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
40 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
46 * Copyright (c) 1983, 1990, 1992, 1993, 1995
47 * The Regents of the University of California. All rights reserved.
49 * Redistribution and use in source and binary forms, with or without
50 * modification, are permitted provided that the following conditions
52 * 1. Redistributions of source code must retain the above copyright
53 * notice, this list of conditions and the following disclaimer.
54 * 2. Redistributions in binary form must reproduce the above copyright
55 * notice, this list of conditions and the following disclaimer in the
56 * documentation and/or other materials provided with the distribution.
57 * 3. Neither the name of the University nor the names of its contributors
58 * may be used to endorse or promote products derived from this software
59 * without specific prior written permission.
61 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
62 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
63 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
64 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
65 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
66 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
67 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
68 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
69 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
70 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
76 __RCSID("$NetBSD: scp.c,v 1.2 2009/06/07 22:38:47 christos Exp $");
77 #include <sys/param.h>
78 #include <sys/types.h>
100 #include "atomicio.h"
101 #include "pathnames.h"
104 #include "progressmeter.h"
106 #define COPY_BUFLEN 16384
108 int do_cmd(char *host
, char *remuser
, char *cmd
, int *fdin
, int *fdout
);
112 /* Struct for addargs */
115 /* Bandwidth limit */
116 off_t limit_rate
= 0;
118 /* Name of current file being transferred. */
121 /* This is set to non-zero to enable verbose mode. */
122 int verbose_mode
= 0;
124 /* This is set to zero if the progressmeter is not desired. */
125 int showprogress
= 1;
127 /* This is the program to execute for the secured connection. ("ssh" or -S) */
129 char *ssh_program
= RESCUEDIR
"/ssh";
131 char *ssh_program
= _PATH_SSH_PROGRAM
;
134 /* This is used to store the pid of ssh_program */
135 pid_t do_cmd_pid
= -1;
140 if (do_cmd_pid
> 1) {
141 kill(do_cmd_pid
, signo
? signo
: SIGTERM
);
142 waitpid(do_cmd_pid
, NULL
, 0);
151 do_local_cmd(arglist
*a
)
158 fatal("do_local_cmd: no arguments");
161 fprintf(stderr
, "Executing:");
162 for (i
= 0; i
< a
->num
; i
++)
163 fprintf(stderr
, " %s", a
->list
[i
]);
164 fprintf(stderr
, "\n");
166 if ((pid
= fork()) == -1)
167 fatal("do_local_cmd: fork: %s", strerror(errno
));
170 execvp(a
->list
[0], a
->list
);
176 signal(SIGTERM
, killchild
);
177 signal(SIGINT
, killchild
);
178 signal(SIGHUP
, killchild
);
180 while (waitpid(pid
, &status
, 0) == -1)
182 fatal("do_local_cmd: waitpid: %s", strerror(errno
));
186 if (!WIFEXITED(status
) || WEXITSTATUS(status
) != 0)
193 * This function executes the given command as the specified user on the
194 * given host. This returns < 0 if execution fails, and >= 0 otherwise. This
195 * assigns the input and output file descriptors on success.
199 do_cmd(char *host
, char *remuser
, char *cmd
, int *fdin
, int *fdout
)
201 int pin
[2], pout
[2], reserved
[2];
205 "Executing: program %s host %s, user %s, command %s\n",
207 remuser
? remuser
: "(unspecified)", cmd
);
210 * Reserve two descriptors so that the real pipes won't get
211 * descriptors 0 and 1 because that will screw up dup2 below.
213 if (pipe(reserved
) < 0)
214 fatal("pipe: %s", strerror(errno
));
216 /* Create a socket pair for communicating with ssh. */
218 fatal("pipe: %s", strerror(errno
));
220 fatal("pipe: %s", strerror(errno
));
222 /* Free the reserved descriptors. */
226 /* Fork a child to execute the command on the remote host using ssh. */
228 if (do_cmd_pid
== 0) {
237 replacearg(&args
, 0, "%s", ssh_program
);
239 addargs(&args
, "-l%s", remuser
);
240 addargs(&args
, "%s", host
);
241 addargs(&args
, "%s", cmd
);
243 execvp(ssh_program
, args
.list
);
246 } else if (do_cmd_pid
== -1) {
247 fatal("fork: %s", strerror(errno
));
249 /* Parent. Close the other side, and return the local side. */
254 signal(SIGTERM
, killchild
);
255 signal(SIGINT
, killchild
);
256 signal(SIGHUP
, killchild
);
265 BUF
*allocbuf(BUF
*, int, int);
268 void run_err(const char *,...);
269 void verifydir(char *);
273 int errs
, remin
, remout
;
274 int pflag
, iamremote
, iamrecursive
, targetshouldbedirectory
;
277 char cmd
[CMDNEEDS
]; /* must hold "rcp -r -p -d\0" */
280 void rsource(char *, struct stat
*);
281 void sink(int, char *[]);
282 void source(int, char *[]);
283 void tolocal(int, char *[]);
284 void toremote(char *, int, char *[]);
288 main(int argc
, char **argv
)
290 int ch
, fflag
, tflag
, status
, n
;
292 char *targ
, *endp
, **newargv
;
296 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
299 /* Copy argv, because we modify it */
300 newargv
= xcalloc(MAX(argc
+ 1, 1), sizeof(*newargv
));
301 for (n
= 0; n
< argc
; n
++)
302 newargv
[n
] = xstrdup(argv
[n
]);
305 memset(&args
, '\0', sizeof(args
));
307 addargs(&args
, "%s", ssh_program
);
308 addargs(&args
, "-x");
309 addargs(&args
, "-oForwardAgent no");
310 addargs(&args
, "-oPermitLocalCommand no");
311 addargs(&args
, "-oClearAllForwardings yes");
314 while ((ch
= getopt(argc
, argv
, "dfl:prtvBCc:i:P:q1246S:o:F:")) != -1)
316 /* User-visible flags. */
322 addargs(&args
, "-%c", ch
);
328 addargs(&args
, "-%c%s", ch
, optarg
);
331 addargs(&args
, "-p%s", optarg
);
334 addargs(&args
, "-oBatchmode yes");
337 speed
= strtod(optarg
, &endp
);
338 if (speed
<= 0 || *endp
!= '\0')
340 limit_rate
= speed
* 1024;
349 ssh_program
= xstrdup(optarg
);
352 addargs(&args
, "-v");
356 addargs(&args
, "-q");
360 /* Server options. */
362 targetshouldbedirectory
= 1;
364 case 'f': /* "from" */
378 if ((pwd
= getpwuid(userid
= getuid())) == NULL
)
379 fatal("unknown user %u", (u_int
) userid
);
381 if (!isatty(STDOUT_FILENO
))
384 remin
= STDIN_FILENO
;
385 remout
= STDOUT_FILENO
;
388 /* Follow "protocol", send data. */
401 targetshouldbedirectory
= 1;
405 /* Command to be executed on remote system using "ssh". */
406 (void) snprintf(cmd
, sizeof cmd
, "scp%s%s%s%s",
407 verbose_mode
? " -v" : "",
408 iamrecursive
? " -r" : "", pflag
? " -p" : "",
409 targetshouldbedirectory
? " -d" : "");
411 (void) signal(SIGPIPE
, lostconn
);
413 if ((targ
= colon(argv
[argc
- 1]))) /* Dest is remote host. */
414 toremote(targ
, argc
, argv
);
416 if (targetshouldbedirectory
)
417 verifydir(argv
[argc
- 1]);
418 tolocal(argc
, argv
); /* Dest is local host. */
421 * Finally check the exit status of the ssh process, if one was forked
422 * and no error has occurred yet
424 if (do_cmd_pid
!= -1 && errs
== 0) {
428 (void) close(remout
);
429 if (waitpid(do_cmd_pid
, &status
, 0) == -1)
432 if (!WIFEXITED(status
) || WEXITSTATUS(status
) != 0)
440 * atomicio-like wrapper that also applies bandwidth limits and updates
441 * the progressmeter counter.
444 scpio(ssize_t (*f
)(int, void *, size_t), int fd
, void *_p
, size_t l
, off_t
*c
)
446 u_char
*p
= (u_char
*)_p
;
452 pfd
.events
= f
== read
? POLLIN
: POLLOUT
;
453 for (offset
= 0; offset
< l
;) {
454 r
= f(fd
, p
+ offset
, l
- offset
);
462 if (errno
== EAGAIN
) {
463 (void)poll(&pfd
, 1, -1); /* Ignore errors */
477 toremote(char *targ
, int argc
, char **argv
)
479 char *bp
, *host
, *src
, *suser
, *thost
, *tuser
, *arg
;
483 memset(&alist
, '\0', sizeof(alist
));
490 arg
= xstrdup(argv
[argc
- 1]);
491 if ((thost
= strrchr(arg
, '@'))) {
502 if (tuser
!= NULL
&& !okname(tuser
)) {
507 for (i
= 0; i
< argc
- 1; i
++) {
508 src
= colon(argv
[i
]);
509 if (src
) { /* remote to remote */
511 addargs(&alist
, "%s", ssh_program
);
513 addargs(&alist
, "-v");
514 addargs(&alist
, "-x");
515 addargs(&alist
, "-oClearAllForwardings yes");
516 addargs(&alist
, "-n");
521 host
= strrchr(argv
[i
], '@');
525 host
= cleanhostname(host
);
528 suser
= pwd
->pw_name
;
529 else if (!okname(suser
))
531 addargs(&alist
, "-l");
532 addargs(&alist
, "%s", suser
);
534 host
= cleanhostname(argv
[i
]);
536 addargs(&alist
, "%s", host
);
537 addargs(&alist
, "%s", cmd
);
538 addargs(&alist
, "%s", src
);
539 addargs(&alist
, "%s%s%s:%s",
540 tuser
? tuser
: "", tuser
? "@" : "",
542 if (do_local_cmd(&alist
) != 0)
544 } else { /* local to remote */
546 xasprintf(&bp
, "%s -t %s", cmd
, targ
);
547 host
= cleanhostname(thost
);
548 if (do_cmd(host
, tuser
, bp
, &remin
,
562 tolocal(int argc
, char **argv
)
564 char *bp
, *host
, *src
, *suser
;
568 memset(&alist
, '\0', sizeof(alist
));
571 for (i
= 0; i
< argc
- 1; i
++) {
572 if (!(src
= colon(argv
[i
]))) { /* Local to local. */
574 addargs(&alist
, "%s", _PATH_CP
);
576 addargs(&alist
, "-r");
578 addargs(&alist
, "-p");
579 addargs(&alist
, "%s", argv
[i
]);
580 addargs(&alist
, "%s", argv
[argc
-1]);
581 if (do_local_cmd(&alist
))
588 if ((host
= strrchr(argv
[i
], '@')) == NULL
) {
595 suser
= pwd
->pw_name
;
597 host
= cleanhostname(host
);
598 xasprintf(&bp
, "%s -f %s", cmd
, src
);
599 if (do_cmd(host
, suser
, bp
, &remin
, &remout
) < 0) {
605 sink(1, argv
+ argc
- 1);
612 source(int argc
, char **argv
)
619 int fd
= -1, haderr
, indx
;
620 char *last
, *name
, buf
[16384], encname
[MAXPATHLEN
];
623 for (indx
= 0; indx
< argc
; ++indx
) {
628 while (len
> 1 && name
[len
-1] == '/')
630 if ((fd
= open(name
, O_RDONLY
|O_NONBLOCK
, 0)) < 0)
632 if (strchr(name
, '\n') != NULL
) {
633 strvisx(encname
, name
, len
, VIS_NL
);
636 if (fstat(fd
, &stb
) < 0) {
637 syserr
: run_err("%s: %s", name
, strerror(errno
));
640 if (stb
.st_size
< 0) {
641 run_err("%s: %s", name
, "Negative file size");
645 switch (stb
.st_mode
& S_IFMT
) {
655 run_err("%s: not a regular file", name
);
658 if ((last
= strrchr(name
, '/')) == NULL
)
665 * Make it compatible with possible future
666 * versions expecting microseconds.
668 (void) snprintf(buf
, sizeof buf
, "T%lu 0 %lu 0\n",
669 (u_long
) (stb
.st_mtime
< 0 ? 0 : stb
.st_mtime
),
670 (u_long
) (stb
.st_atime
< 0 ? 0 : stb
.st_atime
));
672 fprintf(stderr
, "File mtime %ld atime %ld\n",
673 (long)stb
.st_mtime
, (long)stb
.st_atime
);
674 fprintf(stderr
, "Sending file timestamps: %s",
677 (void) atomicio(vwrite
, remout
, buf
, strlen(buf
));
681 #define FILEMODEMASK (S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO)
682 snprintf(buf
, sizeof buf
, "C%04o %lld %s\n",
683 (u_int
) (stb
.st_mode
& FILEMODEMASK
),
684 (long long)stb
.st_size
, last
);
686 fprintf(stderr
, "Sending file modes: %s", buf
);
688 (void) atomicio(vwrite
, remout
, buf
, strlen(buf
));
691 if ((bp
= allocbuf(&buffer
, fd
, COPY_BUFLEN
)) == NULL
) {
692 next
: if (fd
!= -1) {
699 start_progress_meter(curfile
, stb
.st_size
, &statbytes
);
700 set_nonblock(remout
);
701 for (haderr
= i
= 0; i
< stb
.st_size
; i
+= bp
->cnt
) {
703 if (i
+ (off_t
)amt
> stb
.st_size
)
704 amt
= stb
.st_size
- i
;
706 if (atomicio(read
, fd
, bp
->buf
, amt
) != amt
)
709 /* Keep writing after error to retain sync */
711 (void)atomicio(vwrite
, remout
, bp
->buf
, amt
);
714 if (scpio(vwrite
, remout
, bp
->buf
, amt
,
718 unset_nonblock(remout
);
720 stop_progress_meter();
723 if (close(fd
) < 0 && !haderr
)
728 (void) atomicio(vwrite
, remout
, "", 1);
730 run_err("%s: %s", name
, strerror(haderr
));
736 rsource(char *name
, struct stat
*statp
)
740 char *last
, *vect
[1], path
[1100];
742 if (!(dirp
= opendir(name
))) {
743 run_err("%s: %s", name
, strerror(errno
));
746 last
= strrchr(name
, '/');
752 (void) snprintf(path
, sizeof(path
), "T%lu 0 %lu 0\n",
753 (u_long
) statp
->st_mtime
,
754 (u_long
) statp
->st_atime
);
755 (void) atomicio(vwrite
, remout
, path
, strlen(path
));
756 if (response() < 0) {
761 (void) snprintf(path
, sizeof path
, "D%04o %d %.1024s\n",
762 (u_int
) (statp
->st_mode
& FILEMODEMASK
), 0, last
);
764 fprintf(stderr
, "Entering directory: %s", path
);
765 (void) atomicio(vwrite
, remout
, path
, strlen(path
));
766 if (response() < 0) {
770 while ((dp
= readdir(dirp
)) != NULL
) {
773 if (!strcmp(dp
->d_name
, ".") || !strcmp(dp
->d_name
, ".."))
775 if (strlen(name
) + 1 + strlen(dp
->d_name
) >= sizeof(path
) - 1) {
776 run_err("%s/%s: name too long", name
, dp
->d_name
);
779 (void) snprintf(path
, sizeof path
, "%s/%s", name
, dp
->d_name
);
783 (void) closedir(dirp
);
784 (void) atomicio(vwrite
, remout
, "E\n", 2);
791 static struct timeval bwstart
, bwend
;
792 static int lamt
, thresh
= 16384;
794 struct timespec ts
, rm
;
796 if (!timerisset(&bwstart
)) {
797 gettimeofday(&bwstart
, NULL
);
805 gettimeofday(&bwend
, NULL
);
806 timersub(&bwend
, &bwstart
, &bwend
);
807 if (!timerisset(&bwend
))
811 waitlen
= (double)1000000L * lamt
/ limit_rate
;
813 bwstart
.tv_sec
= waitlen
/ 1000000L;
814 bwstart
.tv_usec
= waitlen
% 1000000L;
816 if (timercmp(&bwstart
, &bwend
, >)) {
817 timersub(&bwstart
, &bwend
, &bwend
);
819 /* Adjust the wait time */
824 } else if (bwend
.tv_usec
< 10000) {
826 if (thresh
> COPY_BUFLEN
* 4)
827 thresh
= COPY_BUFLEN
* 4;
830 TIMEVAL_TO_TIMESPEC(&bwend
, &ts
);
831 while (nanosleep(&ts
, &rm
) == -1) {
839 gettimeofday(&bwstart
, NULL
);
843 sink(int argc
, char **argv
)
853 int amt
, exists
, first
, ofd
;
854 mode_t mode
, omode
, mask
;
855 off_t size
, statbytes
;
856 int setimes
, targisdir
, wrerrno
= 0;
857 char ch
, *cp
, *np
, *targ
, *why
, *vect
[1], buf
[16384];
858 struct timeval tv
[2];
862 #define SCREWUP(str) { why = str; goto screwup; }
864 setimes
= targisdir
= 0;
869 run_err("ambiguous target");
873 if (targetshouldbedirectory
)
876 (void) atomicio(vwrite
, remout
, "", 1);
877 if (stat(targ
, &stb
) == 0 && S_ISDIR(stb
.st_mode
))
879 for (first
= 1;; first
= 0) {
881 if (atomicio(read
, remin
, cp
, 1) != 1)
884 SCREWUP("unexpected <newline>");
886 if (atomicio(read
, remin
, &ch
, sizeof(ch
)) != sizeof(ch
))
887 SCREWUP("lost connection");
889 } while (cp
< &buf
[sizeof(buf
) - 1] && ch
!= '\n');
892 fprintf(stderr
, "Sink: %s", buf
);
894 if (buf
[0] == '\01' || buf
[0] == '\02') {
896 (void) atomicio(vwrite
, STDERR_FILENO
,
897 buf
+ 1, strlen(buf
+ 1));
904 (void) atomicio(vwrite
, remout
, "", 1);
914 mtime
.tv_sec
= strtol(cp
, &cp
, 10);
915 if (!cp
|| *cp
++ != ' ')
916 SCREWUP("mtime.sec not delimited");
917 mtime
.tv_usec
= strtol(cp
, &cp
, 10);
918 if (!cp
|| *cp
++ != ' ')
919 SCREWUP("mtime.usec not delimited");
920 atime
.tv_sec
= strtol(cp
, &cp
, 10);
921 if (!cp
|| *cp
++ != ' ')
922 SCREWUP("atime.sec not delimited");
923 atime
.tv_usec
= strtol(cp
, &cp
, 10);
924 if (!cp
|| *cp
++ != '\0')
925 SCREWUP("atime.usec not delimited");
926 (void) atomicio(vwrite
, remout
, "", 1);
929 if (*cp
!= 'C' && *cp
!= 'D') {
931 * Check for the case "rcp remote:foo\* local:bar".
932 * In this case, the line "No match." can be returned
933 * by the shell before the rcp command on the remote is
934 * executed so the ^Aerror_message convention isn't
941 SCREWUP("expected control record");
944 for (++cp
; cp
< buf
+ 5; cp
++) {
945 if (*cp
< '0' || *cp
> '7')
947 mode
= (mode
<< 3) | (*cp
- '0');
950 SCREWUP("mode not delimited");
952 for (size
= 0; isdigit((unsigned char)*cp
);)
953 size
= size
* 10 + (*cp
++ - '0');
955 SCREWUP("size not delimited");
956 if ((strchr(cp
, '/') != NULL
) || (strcmp(cp
, "..") == 0)) {
957 run_err("error: unexpected filename: %s", cp
);
961 static char *namebuf
;
962 static size_t cursize
;
965 need
= strlen(targ
) + strlen(cp
) + 250;
966 if (need
> cursize
) {
969 namebuf
= xmalloc(need
);
972 (void) snprintf(namebuf
, need
, "%s%s%s", targ
,
973 strcmp(targ
, "/") ? "/" : "", cp
);
978 exists
= stat(np
, &stb
) == 0;
980 int mod_flag
= pflag
;
982 SCREWUP("received directory without -r");
984 if (!S_ISDIR(stb
.st_mode
)) {
989 (void) chmod(np
, mode
);
991 /* Handle copying from a read-only
994 if (mkdir(np
, mode
| S_IRWXU
) < 0)
997 vect
[0] = xstrdup(np
);
1001 if (utimes(vect
[0], tv
) < 0)
1002 run_err("%s: set times: %s",
1003 vect
[0], strerror(errno
));
1006 (void) chmod(vect
[0], mode
);
1013 if ((ofd
= open(np
, O_WRONLY
|O_CREAT
, mode
)) < 0) {
1014 bad
: run_err("%s: %s", np
, strerror(errno
));
1017 (void) atomicio(vwrite
, remout
, "", 1);
1018 if ((bp
= allocbuf(&buffer
, ofd
, COPY_BUFLEN
)) == NULL
) {
1027 start_progress_meter(curfile
, size
, &statbytes
);
1028 set_nonblock(remin
);
1029 for (count
= i
= 0; i
< size
; i
+= bp
->cnt
) {
1035 j
= scpio(read
, remin
, cp
, amt
, &statbytes
);
1037 run_err("%s", j
!= EPIPE
?
1039 "dropped connection");
1046 if (count
== bp
->cnt
) {
1047 /* Keep reading so we stay sync'd up. */
1049 if (atomicio(vwrite
, ofd
, bp
->buf
,
1059 unset_nonblock(remin
);
1061 stop_progress_meter();
1062 if (count
!= 0 && wrerr
== NO
&&
1063 atomicio(vwrite
, ofd
, bp
->buf
, count
) != count
) {
1067 if (wrerr
== NO
&& (!exists
|| S_ISREG(stb
.st_mode
)) &&
1068 ftruncate(ofd
, size
) != 0) {
1069 run_err("%s: truncate: %s", np
, strerror(errno
));
1073 if (exists
|| omode
!= mode
)
1074 if (fchmod(ofd
, omode
)) {
1075 run_err("%s: set mode: %s",
1076 np
, strerror(errno
));
1080 if (!exists
&& omode
!= mode
)
1081 if (fchmod(ofd
, omode
& ~mask
)) {
1082 run_err("%s: set mode: %s",
1083 np
, strerror(errno
));
1087 if (close(ofd
) == -1) {
1092 if (setimes
&& wrerr
== NO
) {
1094 if (utimes(np
, tv
) < 0) {
1095 run_err("%s: set times: %s",
1096 np
, strerror(errno
));
1102 run_err("%s: %s", np
, strerror(wrerrno
));
1105 (void) atomicio(vwrite
, remout
, "", 1);
1112 run_err("protocol error: %s", why
);
1119 char ch
, *cp
, resp
, rbuf
[2048];
1121 if (atomicio(read
, remin
, &resp
, sizeof(resp
)) != sizeof(resp
))
1131 case 1: /* error, followed by error msg */
1132 case 2: /* fatal error, "" */
1134 if (atomicio(read
, remin
, &ch
, sizeof(ch
)) != sizeof(ch
))
1137 } while (cp
< &rbuf
[sizeof(rbuf
) - 1] && ch
!= '\n');
1140 (void) atomicio(vwrite
, STDERR_FILENO
, rbuf
, cp
- rbuf
);
1152 (void) fprintf(stderr
,
1153 "usage: scp [-1246BCpqrv] [-c cipher] [-F ssh_config] [-i identity_file]\n"
1154 " [-l limit] [-o ssh_option] [-P port] [-S program]\n"
1155 " [[user@]host1:]file1 ... [[user@]host2:]file2\n");
1160 run_err(const char *fmt
,...)
1166 if (fp
!= NULL
|| (remout
!= -1 && (fp
= fdopen(remout
, "w")))) {
1167 (void) fprintf(fp
, "%c", 0x01);
1168 (void) fprintf(fp
, "scp: ");
1170 (void) vfprintf(fp
, fmt
, ap
);
1172 (void) fprintf(fp
, "\n");
1178 vfprintf(stderr
, fmt
, ap
);
1180 fprintf(stderr
, "\n");
1189 if (!stat(cp
, &stb
)) {
1190 if (S_ISDIR(stb
.st_mode
))
1194 run_err("%s: %s", cp
, strerror(errno
));
1209 if (!isalpha(c
) && !isdigit(c
)) {
1224 bad
: fprintf(stderr
, "%s: invalid user name\n", cp0
);
1229 allocbuf(BUF
*bp
, int fd
, int blksize
)
1234 if (fstat(fd
, &stb
) < 0) {
1235 run_err("fstat: %s", strerror(errno
));
1238 size
= roundup(stb
.st_blksize
, blksize
);
1241 if (bp
->cnt
>= size
)
1243 if (bp
->buf
== NULL
)
1244 bp
->buf
= xmalloc(size
);
1246 bp
->buf
= xrealloc(bp
->buf
, 1, size
);
1247 memset(bp
->buf
, 0, size
);
1256 write(STDERR_FILENO
, "lost connection\n", 16);