1 /* $OpenBSD: scp.c,v 1.162 2008/01/01 09:06:39 dtucker 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>
81 # include <sys/poll.h>
82 #ifdef HAVE_SYS_TIME_H
83 # include <sys/time.h>
100 #if defined(HAVE_STRNVIS) && defined(HAVE_VIS_H)
105 #include "atomicio.h"
106 #include "pathnames.h"
109 #include "progressmeter.h"
111 extern char *__progname;
113 #define COPY_BUFLEN 16384
115 int do_cmd(char *host, char *remuser, char *cmd, int *fdin, int *fdout);
119 /* Struct for addargs */
122 /* Bandwidth limit */
123 off_t limit_rate = 0;
125 /* Name of current file being transferred. */
128 /* This is set to non-zero to enable verbose mode. */
129 int verbose_mode = 0;
131 /* This is set to zero if the progressmeter is not desired. */
132 int showprogress = 1;
134 /* This is the program to execute for the secured connection. ("ssh" or -S) */
135 char *ssh_program = _PATH_SSH_PROGRAM;
137 /* This is used to store the pid of ssh_program */
138 pid_t do_cmd_pid = -1;
143 if (do_cmd_pid > 1) {
144 kill(do_cmd_pid, signo ? signo : SIGTERM);
145 waitpid(do_cmd_pid, NULL, 0);
154 do_local_cmd(arglist *a)
161 fatal("do_local_cmd: no arguments");
164 fprintf(stderr, "Executing:");
165 for (i = 0; i < a->num; i++)
166 fprintf(stderr, " %s", a->list[i]);
167 fprintf(stderr, "\n");
169 if ((pid = fork()) == -1)
170 fatal("do_local_cmd: fork: %s", strerror(errno));
173 execvp(a->list[0], a->list);
179 signal(SIGTERM, killchild);
180 signal(SIGINT, killchild);
181 signal(SIGHUP, killchild);
183 while (waitpid(pid, &status, 0) == -1)
185 fatal("do_local_cmd: waitpid: %s", strerror(errno));
189 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
196 * This function executes the given command as the specified user on the
197 * given host. This returns < 0 if execution fails, and >= 0 otherwise. This
198 * assigns the input and output file descriptors on success.
202 do_cmd(char *host, char *remuser, char *cmd, int *fdin, int *fdout)
204 int pin[2], pout[2], reserved[2];
208 "Executing: program %s host %s, user %s, command %s\n",
210 remuser ? remuser : "(unspecified)", cmd);
213 * Reserve two descriptors so that the real pipes won't get
214 * descriptors 0 and 1 because that will screw up dup2 below.
216 if (pipe(reserved) < 0)
217 fatal("pipe: %s", strerror(errno));
219 /* Create a socket pair for communicating with ssh. */
221 fatal("pipe: %s", strerror(errno));
223 fatal("pipe: %s", strerror(errno));
225 /* Free the reserved descriptors. */
229 /* Fork a child to execute the command on the remote host using ssh. */
231 if (do_cmd_pid == 0) {
240 replacearg(&args, 0, "%s", ssh_program);
242 addargs(&args, "-l%s", remuser);
243 addargs(&args, "%s", host);
244 addargs(&args, "%s", cmd);
246 execvp(ssh_program, args.list);
249 } else if (do_cmd_pid == -1) {
250 fatal("fork: %s", strerror(errno));
252 /* Parent. Close the other side, and return the local side. */
257 signal(SIGTERM, killchild);
258 signal(SIGINT, killchild);
259 signal(SIGHUP, killchild);
268 BUF *allocbuf(BUF *, int, int);
271 void run_err(const char *,...);
272 void verifydir(char *);
276 int errs, remin, remout;
277 int pflag, iamremote, iamrecursive, targetshouldbedirectory;
280 char cmd[CMDNEEDS]; /* must hold "rcp -r -p -d\0" */
283 void rsource(char *, struct stat *);
284 void sink(int, char *[]);
285 void source(int, char *[]);
286 void tolocal(int, char *[]);
287 void toremote(char *, int, char *[]);
288 size_t scpio(ssize_t (*)(int, void *, size_t), int, void *, size_t, off_t *);
292 main(int argc, char **argv)
294 int ch, fflag, tflag, status, n;
296 char *targ, *endp, **newargv;
300 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
303 /* Copy argv, because we modify it */
304 newargv = xcalloc(MAX(argc + 1, 1), sizeof(*newargv));
305 for (n = 0; n < argc; n++)
306 newargv[n] = xstrdup(argv[n]);
309 __progname = ssh_get_progname(argv[0]);
311 memset(&args, '\0', sizeof(args));
313 addargs(&args, "%s", ssh_program);
314 addargs(&args, "-x");
315 addargs(&args, "-oForwardAgent no");
316 addargs(&args, "-oPermitLocalCommand no");
317 addargs(&args, "-oClearAllForwardings yes");
320 while ((ch = getopt(argc, argv, "dfl:prtvBCc:i:P:q1246S:o:F:")) != -1)
322 /* User-visible flags. */
328 addargs(&args, "-%c", ch);
334 addargs(&args, "-%c%s", ch, optarg);
337 addargs(&args, "-p%s", optarg);
340 addargs(&args, "-oBatchmode yes");
343 speed = strtod(optarg, &endp);
344 if (speed <= 0 || *endp != '\0')
346 limit_rate = speed * 1024;
355 ssh_program = xstrdup(optarg);
358 addargs(&args, "-v");
362 addargs(&args, "-q");
366 /* Server options. */
368 targetshouldbedirectory = 1;
370 case 'f': /* "from" */
378 setmode(0, O_BINARY);
387 if ((pwd = getpwuid(userid = getuid())) == NULL)
388 fatal("unknown user %u", (u_int) userid);
390 if (!isatty(STDOUT_FILENO))
393 remin = STDIN_FILENO;
394 remout = STDOUT_FILENO;
397 /* Follow "protocol", send data. */
410 targetshouldbedirectory = 1;
414 /* Command to be executed on remote system using "ssh". */
415 (void) snprintf(cmd, sizeof cmd, "scp%s%s%s%s",
416 verbose_mode ? " -v" : "",
417 iamrecursive ? " -r" : "", pflag ? " -p" : "",
418 targetshouldbedirectory ? " -d" : "");
420 (void) signal(SIGPIPE, lostconn);
422 if ((targ = colon(argv[argc - 1]))) /* Dest is remote host. */
423 toremote(targ, argc, argv);
425 if (targetshouldbedirectory)
426 verifydir(argv[argc - 1]);
427 tolocal(argc, argv); /* Dest is local host. */
430 * Finally check the exit status of the ssh process, if one was forked
431 * and no error has occured yet
433 if (do_cmd_pid != -1 && errs == 0) {
437 (void) close(remout);
438 if (waitpid(do_cmd_pid, &status, 0) == -1)
441 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
449 * atomicio-like wrapper that also applies bandwidth limits and updates
450 * the progressmeter counter.
453 scpio(ssize_t (*f)(int, void *, size_t), int fd, void *_p, size_t l, off_t *c)
455 u_char *p = (u_char *)_p;
461 pfd.events = f == read ? POLLIN : POLLOUT;
462 for (offset = 0; offset < l;) {
463 r = f(fd, p + offset, l - offset);
471 if (errno == EAGAIN) {
472 (void)poll(&pfd, 1, -1); /* Ignore errors */
486 toremote(char *targ, int argc, char **argv)
488 char *bp, *host, *src, *suser, *thost, *tuser, *arg;
492 memset(&alist, '\0', sizeof(alist));
499 arg = xstrdup(argv[argc - 1]);
500 if ((thost = strrchr(arg, '@'))) {
511 if (tuser != NULL && !okname(tuser)) {
516 for (i = 0; i < argc - 1; i++) {
517 src = colon(argv[i]);
518 if (src) { /* remote to remote */
520 addargs(&alist, "%s", ssh_program);
522 addargs(&alist, "-v");
523 addargs(&alist, "-x");
524 addargs(&alist, "-oClearAllForwardings yes");
525 addargs(&alist, "-n");
530 host = strrchr(argv[i], '@');
534 host = cleanhostname(host);
537 suser = pwd->pw_name;
538 else if (!okname(suser))
540 addargs(&alist, "-l");
541 addargs(&alist, "%s", suser);
543 host = cleanhostname(argv[i]);
545 addargs(&alist, "%s", host);
546 addargs(&alist, "%s", cmd);
547 addargs(&alist, "%s", src);
548 addargs(&alist, "%s%s%s:%s",
549 tuser ? tuser : "", tuser ? "@" : "",
551 if (do_local_cmd(&alist) != 0)
553 } else { /* local to remote */
555 xasprintf(&bp, "%s -t %s", cmd, targ);
556 host = cleanhostname(thost);
557 if (do_cmd(host, tuser, bp, &remin,
571 tolocal(int argc, char **argv)
573 char *bp, *host, *src, *suser;
577 memset(&alist, '\0', sizeof(alist));
580 for (i = 0; i < argc - 1; i++) {
581 if (!(src = colon(argv[i]))) { /* Local to local. */
583 addargs(&alist, "%s", _PATH_CP);
585 addargs(&alist, "-r");
587 addargs(&alist, "-p");
588 addargs(&alist, "%s", argv[i]);
589 addargs(&alist, "%s", argv[argc-1]);
590 if (do_local_cmd(&alist))
597 if ((host = strrchr(argv[i], '@')) == NULL) {
604 suser = pwd->pw_name;
606 host = cleanhostname(host);
607 xasprintf(&bp, "%s -f %s", cmd, src);
608 if (do_cmd(host, suser, bp, &remin, &remout) < 0) {
614 sink(1, argv + argc - 1);
621 source(int argc, char **argv)
626 off_t i, amt, statbytes;
627 int fd = -1, haderr, indx;
628 char *last, *name, buf[2048], encname[MAXPATHLEN];
631 for (indx = 0; indx < argc; ++indx) {
635 while (len > 1 && name[len-1] == '/')
637 if ((fd = open(name, O_RDONLY|O_NONBLOCK, 0)) < 0)
639 if (strchr(name, '\n') != NULL) {
640 strnvis(encname, name, sizeof(encname), VIS_NL);
643 if (fstat(fd, &stb) < 0) {
644 syserr: run_err("%s: %s", name, strerror(errno));
648 switch (stb.st_mode & S_IFMT) {
658 run_err("%s: not a regular file", name);
661 if ((last = strrchr(name, '/')) == NULL)
668 * Make it compatible with possible future
669 * versions expecting microseconds.
671 (void) snprintf(buf, sizeof buf, "T%lu 0 %lu 0\n",
672 (u_long) (stb.st_mtime < 0 ? 0 : stb.st_mtime),
673 (u_long) (stb.st_atime < 0 ? 0 : stb.st_atime));
675 fprintf(stderr, "File mtime %ld atime %ld\n",
676 (long)stb.st_mtime, (long)stb.st_atime);
677 fprintf(stderr, "Sending file timestamps: %s",
680 (void) atomicio(vwrite, remout, buf, strlen(buf));
684 #define FILEMODEMASK (S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO)
685 snprintf(buf, sizeof buf, "C%04o %lld %s\n",
686 (u_int) (stb.st_mode & FILEMODEMASK),
687 (long long)stb.st_size, last);
689 fprintf(stderr, "Sending file modes: %s", buf);
691 (void) atomicio(vwrite, remout, buf, strlen(buf));
694 if ((bp = allocbuf(&buffer, fd, COPY_BUFLEN)) == NULL) {
695 next: if (fd != -1) {
702 start_progress_meter(curfile, stb.st_size, &statbytes);
703 set_nonblock(remout);
704 for (haderr = i = 0; i < stb.st_size; i += bp->cnt) {
706 if (i + amt > stb.st_size)
707 amt = stb.st_size - i;
709 if (atomicio(read, fd, bp->buf, amt) != amt)
712 /* Keep writing after error to retain sync */
714 (void)atomicio(vwrite, remout, bp->buf, amt);
717 if (scpio(vwrite, remout, bp->buf, amt,
721 unset_nonblock(remout);
723 stop_progress_meter();
726 if (close(fd) < 0 && !haderr)
731 (void) atomicio(vwrite, remout, "", 1);
733 run_err("%s: %s", name, strerror(haderr));
739 rsource(char *name, struct stat *statp)
743 char *last, *vect[1], path[1100];
745 if (!(dirp = opendir(name))) {
746 run_err("%s: %s", name, strerror(errno));
749 last = strrchr(name, '/');
755 (void) snprintf(path, sizeof(path), "T%lu 0 %lu 0\n",
756 (u_long) statp->st_mtime,
757 (u_long) statp->st_atime);
758 (void) atomicio(vwrite, remout, path, strlen(path));
759 if (response() < 0) {
764 (void) snprintf(path, sizeof path, "D%04o %d %.1024s\n",
765 (u_int) (statp->st_mode & FILEMODEMASK), 0, last);
767 fprintf(stderr, "Entering directory: %s", path);
768 (void) atomicio(vwrite, remout, path, strlen(path));
769 if (response() < 0) {
773 while ((dp = readdir(dirp)) != NULL) {
776 if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
778 if (strlen(name) + 1 + strlen(dp->d_name) >= sizeof(path) - 1) {
779 run_err("%s/%s: name too long", name, dp->d_name);
782 (void) snprintf(path, sizeof path, "%s/%s", name, dp->d_name);
786 (void) closedir(dirp);
787 (void) atomicio(vwrite, remout, "E\n", 2);
794 static struct timeval bwstart, bwend;
795 static int lamt, thresh = 16384;
797 struct timespec ts, rm;
799 if (!timerisset(&bwstart)) {
800 gettimeofday(&bwstart, NULL);
808 gettimeofday(&bwend, NULL);
809 timersub(&bwend, &bwstart, &bwend);
810 if (!timerisset(&bwend))
814 waitlen = (double)1000000L * lamt / limit_rate;
816 bwstart.tv_sec = waitlen / 1000000L;
817 bwstart.tv_usec = waitlen % 1000000L;
819 if (timercmp(&bwstart, &bwend, >)) {
820 timersub(&bwstart, &bwend, &bwend);
822 /* Adjust the wait time */
827 } else if (bwend.tv_usec < 10000) {
829 if (thresh > COPY_BUFLEN * 4)
830 thresh = COPY_BUFLEN * 4;
833 TIMEVAL_TO_TIMESPEC(&bwend, &ts);
834 while (nanosleep(&ts, &rm) == -1) {
842 gettimeofday(&bwstart, NULL);
846 sink(int argc, char **argv)
856 int amt, exists, first, ofd;
857 mode_t mode, omode, mask;
858 off_t size, statbytes;
859 int setimes, targisdir, wrerrno = 0;
860 char ch, *cp, *np, *targ, *why, *vect[1], buf[2048];
861 struct timeval tv[2];
865 #define SCREWUP(str) { why = str; goto screwup; }
867 setimes = targisdir = 0;
872 run_err("ambiguous target");
876 if (targetshouldbedirectory)
879 (void) atomicio(vwrite, remout, "", 1);
880 if (stat(targ, &stb) == 0 && S_ISDIR(stb.st_mode))
882 for (first = 1;; first = 0) {
884 if (atomicio(read, remin, cp, 1) != 1)
887 SCREWUP("unexpected <newline>");
889 if (atomicio(read, remin, &ch, sizeof(ch)) != sizeof(ch))
890 SCREWUP("lost connection");
892 } while (cp < &buf[sizeof(buf) - 1] && ch != '\n');
895 fprintf(stderr, "Sink: %s", buf);
897 if (buf[0] == '\01' || buf[0] == '\02') {
899 (void) atomicio(vwrite, STDERR_FILENO,
900 buf + 1, strlen(buf + 1));
907 (void) atomicio(vwrite, remout, "", 1);
917 mtime.tv_sec = strtol(cp, &cp, 10);
918 if (!cp || *cp++ != ' ')
919 SCREWUP("mtime.sec not delimited");
920 mtime.tv_usec = strtol(cp, &cp, 10);
921 if (!cp || *cp++ != ' ')
922 SCREWUP("mtime.usec not delimited");
923 atime.tv_sec = strtol(cp, &cp, 10);
924 if (!cp || *cp++ != ' ')
925 SCREWUP("atime.sec not delimited");
926 atime.tv_usec = strtol(cp, &cp, 10);
927 if (!cp || *cp++ != '\0')
928 SCREWUP("atime.usec not delimited");
929 (void) atomicio(vwrite, remout, "", 1);
932 if (*cp != 'C' && *cp != 'D') {
934 * Check for the case "rcp remote:foo\* local:bar".
935 * In this case, the line "No match." can be returned
936 * by the shell before the rcp command on the remote is
937 * executed so the ^Aerror_message convention isn't
944 SCREWUP("expected control record");
947 for (++cp; cp < buf + 5; cp++) {
948 if (*cp < '0' || *cp > '7')
950 mode = (mode << 3) | (*cp - '0');
953 SCREWUP("mode not delimited");
955 for (size = 0; isdigit(*cp);)
956 size = size * 10 + (*cp++ - '0');
958 SCREWUP("size not delimited");
959 if ((strchr(cp, '/') != NULL) || (strcmp(cp, "..") == 0)) {
960 run_err("error: unexpected filename: %s", cp);
964 static char *namebuf;
965 static size_t cursize;
968 need = strlen(targ) + strlen(cp) + 250;
969 if (need > cursize) {
972 namebuf = xmalloc(need);
975 (void) snprintf(namebuf, need, "%s%s%s", targ,
976 strcmp(targ, "/") ? "/" : "", cp);
981 exists = stat(np, &stb) == 0;
983 int mod_flag = pflag;
985 SCREWUP("received directory without -r");
987 if (!S_ISDIR(stb.st_mode)) {
992 (void) chmod(np, mode);
994 /* Handle copying from a read-only
997 if (mkdir(np, mode | S_IRWXU) < 0)
1000 vect[0] = xstrdup(np);
1004 if (utimes(vect[0], tv) < 0)
1005 run_err("%s: set times: %s",
1006 vect[0], strerror(errno));
1009 (void) chmod(vect[0], mode);
1016 if ((ofd = open(np, O_WRONLY|O_CREAT, mode)) < 0) {
1017 bad: run_err("%s: %s", np, strerror(errno));
1020 (void) atomicio(vwrite, remout, "", 1);
1021 if ((bp = allocbuf(&buffer, ofd, COPY_BUFLEN)) == NULL) {
1030 start_progress_meter(curfile, size, &statbytes);
1031 set_nonblock(remin);
1032 for (count = i = 0; i < size; i += bp->cnt) {
1038 j = scpio(read, remin, cp, amt, &statbytes);
1040 run_err("%s", j != EPIPE ?
1042 "dropped connection");
1049 if (count == bp->cnt) {
1050 /* Keep reading so we stay sync'd up. */
1052 if (atomicio(vwrite, ofd, bp->buf,
1062 unset_nonblock(remin);
1064 stop_progress_meter();
1065 if (count != 0 && wrerr == NO &&
1066 atomicio(vwrite, ofd, bp->buf, count) != count) {
1070 if (wrerr == NO && (!exists || S_ISREG(stb.st_mode)) &&
1071 ftruncate(ofd, size) != 0) {
1072 run_err("%s: truncate: %s", np, strerror(errno));
1076 if (exists || omode != mode)
1078 if (fchmod(ofd, omode)) {
1079 #else /* HAVE_FCHMOD */
1080 if (chmod(np, omode)) {
1081 #endif /* HAVE_FCHMOD */
1082 run_err("%s: set mode: %s",
1083 np, strerror(errno));
1087 if (!exists && omode != mode)
1089 if (fchmod(ofd, omode & ~mask)) {
1090 #else /* HAVE_FCHMOD */
1091 if (chmod(np, omode & ~mask)) {
1092 #endif /* HAVE_FCHMOD */
1093 run_err("%s: set mode: %s",
1094 np, strerror(errno));
1098 if (close(ofd) == -1) {
1103 if (setimes && wrerr == NO) {
1105 if (utimes(np, tv) < 0) {
1106 run_err("%s: set times: %s",
1107 np, strerror(errno));
1113 run_err("%s: %s", np, strerror(wrerrno));
1116 (void) atomicio(vwrite, remout, "", 1);
1123 run_err("protocol error: %s", why);
1130 char ch, *cp, resp, rbuf[2048];
1132 if (atomicio(read, remin, &resp, sizeof(resp)) != sizeof(resp))
1142 case 1: /* error, followed by error msg */
1143 case 2: /* fatal error, "" */
1145 if (atomicio(read, remin, &ch, sizeof(ch)) != sizeof(ch))
1148 } while (cp < &rbuf[sizeof(rbuf) - 1] && ch != '\n');
1151 (void) atomicio(vwrite, STDERR_FILENO, rbuf, cp - rbuf);
1163 (void) fprintf(stderr,
1164 "usage: scp [-1246BCpqrv] [-c cipher] [-F ssh_config] [-i identity_file]\n"
1165 " [-l limit] [-o ssh_option] [-P port] [-S program]\n"
1166 " [[user@]host1:]file1 ... [[user@]host2:]file2\n");
1171 run_err(const char *fmt,...)
1177 if (fp != NULL || (remout != -1 && (fp = fdopen(remout, "w")))) {
1178 (void) fprintf(fp, "%c", 0x01);
1179 (void) fprintf(fp, "scp: ");
1181 (void) vfprintf(fp, fmt, ap);
1183 (void) fprintf(fp, "\n");
1189 vfprintf(stderr, fmt, ap);
1191 fprintf(stderr, "\n");
1200 if (!stat(cp, &stb)) {
1201 if (S_ISDIR(stb.st_mode))
1205 run_err("%s: %s", cp, strerror(errno));
1220 if (!isalpha(c) && !isdigit(c)) {
1235 bad: fprintf(stderr, "%s: invalid user name\n", cp0);
1240 allocbuf(BUF *bp, int fd, int blksize)
1243 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
1246 if (fstat(fd, &stb) < 0) {
1247 run_err("fstat: %s", strerror(errno));
1250 size = roundup(stb.st_blksize, blksize);
1253 #else /* HAVE_STRUCT_STAT_ST_BLKSIZE */
1255 #endif /* HAVE_STRUCT_STAT_ST_BLKSIZE */
1256 if (bp->cnt >= size)
1258 if (bp->buf == NULL)
1259 bp->buf = xmalloc(size);
1261 bp->buf = xrealloc(bp->buf, 1, size);
1262 memset(bp->buf, 0, size);
1271 write(STDERR_FILENO, "lost connection\n", 16);