1 /* $OpenBSD: scp.c,v 1.160 2007/08/06 19:16:06 sobrado 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 = "/home/djm/bin/ssh";
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,
673 (u_long) stb.st_atime);
674 (void) atomicio(vwrite, remout, buf, strlen(buf));
678 #define FILEMODEMASK (S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO)
679 snprintf(buf, sizeof buf, "C%04o %lld %s\n",
680 (u_int) (stb.st_mode & FILEMODEMASK),
681 (long long)stb.st_size, last);
683 fprintf(stderr, "Sending file modes: %s", buf);
685 (void) atomicio(vwrite, remout, buf, strlen(buf));
688 if ((bp = allocbuf(&buffer, fd, COPY_BUFLEN)) == NULL) {
689 next: if (fd != -1) {
696 start_progress_meter(curfile, stb.st_size, &statbytes);
697 set_nonblock(remout);
698 for (haderr = i = 0; i < stb.st_size; i += bp->cnt) {
700 if (i + amt > stb.st_size)
701 amt = stb.st_size - i;
703 if (atomicio(read, fd, bp->buf, amt) != amt)
706 /* Keep writing after error to retain sync */
708 (void)atomicio(vwrite, remout, bp->buf, amt);
711 if (scpio(vwrite, remout, bp->buf, amt,
715 unset_nonblock(remout);
717 stop_progress_meter();
720 if (close(fd) < 0 && !haderr)
725 (void) atomicio(vwrite, remout, "", 1);
727 run_err("%s: %s", name, strerror(haderr));
733 rsource(char *name, struct stat *statp)
737 char *last, *vect[1], path[1100];
739 if (!(dirp = opendir(name))) {
740 run_err("%s: %s", name, strerror(errno));
743 last = strrchr(name, '/');
749 (void) snprintf(path, sizeof(path), "T%lu 0 %lu 0\n",
750 (u_long) statp->st_mtime,
751 (u_long) statp->st_atime);
752 (void) atomicio(vwrite, remout, path, strlen(path));
753 if (response() < 0) {
758 (void) snprintf(path, sizeof path, "D%04o %d %.1024s\n",
759 (u_int) (statp->st_mode & FILEMODEMASK), 0, last);
761 fprintf(stderr, "Entering directory: %s", path);
762 (void) atomicio(vwrite, remout, path, strlen(path));
763 if (response() < 0) {
767 while ((dp = readdir(dirp)) != NULL) {
770 if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
772 if (strlen(name) + 1 + strlen(dp->d_name) >= sizeof(path) - 1) {
773 run_err("%s/%s: name too long", name, dp->d_name);
776 (void) snprintf(path, sizeof path, "%s/%s", name, dp->d_name);
780 (void) closedir(dirp);
781 (void) atomicio(vwrite, remout, "E\n", 2);
788 static struct timeval bwstart, bwend;
789 static int lamt, thresh = 16384;
791 struct timespec ts, rm;
793 if (!timerisset(&bwstart)) {
794 gettimeofday(&bwstart, NULL);
802 gettimeofday(&bwend, NULL);
803 timersub(&bwend, &bwstart, &bwend);
804 if (!timerisset(&bwend))
808 waitlen = (double)1000000L * lamt / limit_rate;
810 bwstart.tv_sec = waitlen / 1000000L;
811 bwstart.tv_usec = waitlen % 1000000L;
813 if (timercmp(&bwstart, &bwend, >)) {
814 timersub(&bwstart, &bwend, &bwend);
816 /* Adjust the wait time */
821 } else if (bwend.tv_usec < 10000) {
823 if (thresh > COPY_BUFLEN * 4)
824 thresh = COPY_BUFLEN * 4;
827 TIMEVAL_TO_TIMESPEC(&bwend, &ts);
828 while (nanosleep(&ts, &rm) == -1) {
836 gettimeofday(&bwstart, NULL);
840 sink(int argc, char **argv)
850 int amt, exists, first, ofd;
851 mode_t mode, omode, mask;
852 off_t size, statbytes;
853 int setimes, targisdir, wrerrno = 0;
854 char ch, *cp, *np, *targ, *why, *vect[1], buf[2048];
855 struct timeval tv[2];
859 #define SCREWUP(str) { why = str; goto screwup; }
861 setimes = targisdir = 0;
866 run_err("ambiguous target");
870 if (targetshouldbedirectory)
873 (void) atomicio(vwrite, remout, "", 1);
874 if (stat(targ, &stb) == 0 && S_ISDIR(stb.st_mode))
876 for (first = 1;; first = 0) {
878 if (atomicio(read, remin, cp, 1) != 1)
881 SCREWUP("unexpected <newline>");
883 if (atomicio(read, remin, &ch, sizeof(ch)) != sizeof(ch))
884 SCREWUP("lost connection");
886 } while (cp < &buf[sizeof(buf) - 1] && ch != '\n');
889 fprintf(stderr, "Sink: %s", buf);
891 if (buf[0] == '\01' || buf[0] == '\02') {
893 (void) atomicio(vwrite, STDERR_FILENO,
894 buf + 1, strlen(buf + 1));
901 (void) atomicio(vwrite, remout, "", 1);
911 mtime.tv_sec = strtol(cp, &cp, 10);
912 if (!cp || *cp++ != ' ')
913 SCREWUP("mtime.sec not delimited");
914 mtime.tv_usec = strtol(cp, &cp, 10);
915 if (!cp || *cp++ != ' ')
916 SCREWUP("mtime.usec not delimited");
917 atime.tv_sec = strtol(cp, &cp, 10);
918 if (!cp || *cp++ != ' ')
919 SCREWUP("atime.sec not delimited");
920 atime.tv_usec = strtol(cp, &cp, 10);
921 if (!cp || *cp++ != '\0')
922 SCREWUP("atime.usec not delimited");
923 (void) atomicio(vwrite, remout, "", 1);
926 if (*cp != 'C' && *cp != 'D') {
928 * Check for the case "rcp remote:foo\* local:bar".
929 * In this case, the line "No match." can be returned
930 * by the shell before the rcp command on the remote is
931 * executed so the ^Aerror_message convention isn't
938 SCREWUP("expected control record");
941 for (++cp; cp < buf + 5; cp++) {
942 if (*cp < '0' || *cp > '7')
944 mode = (mode << 3) | (*cp - '0');
947 SCREWUP("mode not delimited");
949 for (size = 0; isdigit(*cp);)
950 size = size * 10 + (*cp++ - '0');
952 SCREWUP("size not delimited");
953 if ((strchr(cp, '/') != NULL) || (strcmp(cp, "..") == 0)) {
954 run_err("error: unexpected filename: %s", cp);
958 static char *namebuf;
959 static size_t cursize;
962 need = strlen(targ) + strlen(cp) + 250;
963 if (need > cursize) {
966 namebuf = xmalloc(need);
969 (void) snprintf(namebuf, need, "%s%s%s", targ,
970 strcmp(targ, "/") ? "/" : "", cp);
975 exists = stat(np, &stb) == 0;
977 int mod_flag = pflag;
979 SCREWUP("received directory without -r");
981 if (!S_ISDIR(stb.st_mode)) {
986 (void) chmod(np, mode);
988 /* Handle copying from a read-only
991 if (mkdir(np, mode | S_IRWXU) < 0)
994 vect[0] = xstrdup(np);
998 if (utimes(vect[0], tv) < 0)
999 run_err("%s: set times: %s",
1000 vect[0], strerror(errno));
1003 (void) chmod(vect[0], mode);
1010 if ((ofd = open(np, O_WRONLY|O_CREAT, mode)) < 0) {
1011 bad: run_err("%s: %s", np, strerror(errno));
1014 (void) atomicio(vwrite, remout, "", 1);
1015 if ((bp = allocbuf(&buffer, ofd, COPY_BUFLEN)) == NULL) {
1024 start_progress_meter(curfile, size, &statbytes);
1025 set_nonblock(remin);
1026 for (count = i = 0; i < size; i += bp->cnt) {
1032 j = scpio(read, remin, cp, amt, &statbytes);
1034 run_err("%s", j != EPIPE ?
1036 "dropped connection");
1043 if (count == bp->cnt) {
1044 /* Keep reading so we stay sync'd up. */
1046 if (atomicio(vwrite, ofd, bp->buf,
1056 unset_nonblock(remin);
1058 stop_progress_meter();
1059 if (count != 0 && wrerr == NO &&
1060 atomicio(vwrite, ofd, bp->buf, count) != count) {
1064 if (wrerr == NO && (!exists || S_ISREG(stb.st_mode)) &&
1065 ftruncate(ofd, size) != 0) {
1066 run_err("%s: truncate: %s", np, strerror(errno));
1070 if (exists || omode != mode)
1072 if (fchmod(ofd, omode)) {
1073 #else /* HAVE_FCHMOD */
1074 if (chmod(np, omode)) {
1075 #endif /* HAVE_FCHMOD */
1076 run_err("%s: set mode: %s",
1077 np, strerror(errno));
1081 if (!exists && omode != mode)
1083 if (fchmod(ofd, omode & ~mask)) {
1084 #else /* HAVE_FCHMOD */
1085 if (chmod(np, omode & ~mask)) {
1086 #endif /* HAVE_FCHMOD */
1087 run_err("%s: set mode: %s",
1088 np, strerror(errno));
1092 if (close(ofd) == -1) {
1097 if (setimes && wrerr == NO) {
1099 if (utimes(np, tv) < 0) {
1100 run_err("%s: set times: %s",
1101 np, strerror(errno));
1107 run_err("%s: %s", np, strerror(wrerrno));
1110 (void) atomicio(vwrite, remout, "", 1);
1117 run_err("protocol error: %s", why);
1124 char ch, *cp, resp, rbuf[2048];
1126 if (atomicio(read, remin, &resp, sizeof(resp)) != sizeof(resp))
1136 case 1: /* error, followed by error msg */
1137 case 2: /* fatal error, "" */
1139 if (atomicio(read, remin, &ch, sizeof(ch)) != sizeof(ch))
1142 } while (cp < &rbuf[sizeof(rbuf) - 1] && ch != '\n');
1145 (void) atomicio(vwrite, STDERR_FILENO, rbuf, cp - rbuf);
1157 (void) fprintf(stderr,
1158 "usage: scp [-1246BCpqrv] [-c cipher] [-F ssh_config] [-i identity_file]\n"
1159 " [-l limit] [-o ssh_option] [-P port] [-S program]\n"
1160 " [[user@]host1:]file1 ... [[user@]host2:]file2\n");
1165 run_err(const char *fmt,...)
1171 if (fp != NULL || (remout != -1 && (fp = fdopen(remout, "w")))) {
1172 (void) fprintf(fp, "%c", 0x01);
1173 (void) fprintf(fp, "scp: ");
1175 (void) vfprintf(fp, fmt, ap);
1177 (void) fprintf(fp, "\n");
1183 vfprintf(stderr, fmt, ap);
1185 fprintf(stderr, "\n");
1194 if (!stat(cp, &stb)) {
1195 if (S_ISDIR(stb.st_mode))
1199 run_err("%s: %s", cp, strerror(errno));
1214 if (!isalpha(c) && !isdigit(c)) {
1229 bad: fprintf(stderr, "%s: invalid user name\n", cp0);
1234 allocbuf(BUF *bp, int fd, int blksize)
1237 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
1240 if (fstat(fd, &stb) < 0) {
1241 run_err("fstat: %s", strerror(errno));
1244 size = roundup(stb.st_blksize, blksize);
1247 #else /* HAVE_STRUCT_STAT_ST_BLKSIZE */
1249 #endif /* HAVE_STRUCT_STAT_ST_BLKSIZE */
1250 if (bp->cnt >= size)
1252 if (bp->buf == NULL)
1253 bp->buf = xmalloc(size);
1255 bp->buf = xrealloc(bp->buf, 1, size);
1256 memset(bp->buf, 0, size);
1265 write(STDERR_FILENO, "lost connection\n", 16);