1 /* $OpenBSD: scp.c,v 1.151 2006/07/25 02:59:21 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>
80 #ifdef HAVE_SYS_TIME_H
81 # include <sys/time.h>
98 #include "pathnames.h"
101 #include "progressmeter.h"
103 extern char *__progname;
105 int do_cmd(char *host, char *remuser, char *cmd, int *fdin, int *fdout);
109 /* Struct for addargs */
112 /* Bandwidth limit */
113 off_t limit_rate = 0;
115 /* Name of current file being transferred. */
118 /* This is set to non-zero to enable verbose mode. */
119 int verbose_mode = 0;
121 /* This is set to zero if the progressmeter is not desired. */
122 int showprogress = 1;
124 /* This is the program to execute for the secured connection. ("ssh" or -S) */
125 char *ssh_program = _PATH_SSH_PROGRAM;
127 /* This is used to store the pid of ssh_program */
128 pid_t do_cmd_pid = -1;
133 if (do_cmd_pid > 1) {
134 kill(do_cmd_pid, signo ? signo : SIGTERM);
135 waitpid(do_cmd_pid, NULL, 0);
144 do_local_cmd(arglist *a)
151 fatal("do_local_cmd: no arguments");
154 fprintf(stderr, "Executing:");
155 for (i = 0; i < a->num; i++)
156 fprintf(stderr, " %s", a->list[i]);
157 fprintf(stderr, "\n");
159 if ((pid = fork()) == -1)
160 fatal("do_local_cmd: fork: %s", strerror(errno));
163 execvp(a->list[0], a->list);
169 signal(SIGTERM, killchild);
170 signal(SIGINT, killchild);
171 signal(SIGHUP, killchild);
173 while (waitpid(pid, &status, 0) == -1)
175 fatal("do_local_cmd: waitpid: %s", strerror(errno));
179 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
186 * This function executes the given command as the specified user on the
187 * given host. This returns < 0 if execution fails, and >= 0 otherwise. This
188 * assigns the input and output file descriptors on success.
192 do_cmd(char *host, char *remuser, char *cmd, int *fdin, int *fdout)
194 int pin[2], pout[2], reserved[2];
198 "Executing: program %s host %s, user %s, command %s\n",
200 remuser ? remuser : "(unspecified)", cmd);
203 * Reserve two descriptors so that the real pipes won't get
204 * descriptors 0 and 1 because that will screw up dup2 below.
206 if (pipe(reserved) < 0)
207 fatal("pipe: %s", strerror(errno));
209 /* Create a socket pair for communicating with ssh. */
211 fatal("pipe: %s", strerror(errno));
213 fatal("pipe: %s", strerror(errno));
215 /* Free the reserved descriptors. */
219 /* Fork a child to execute the command on the remote host using ssh. */
221 if (do_cmd_pid == 0) {
230 replacearg(&args, 0, "%s", ssh_program);
232 addargs(&args, "-l%s", remuser);
233 addargs(&args, "%s", host);
234 addargs(&args, "%s", cmd);
236 execvp(ssh_program, args.list);
239 } else if (do_cmd_pid == -1) {
240 fatal("fork: %s", strerror(errno));
242 /* Parent. Close the other side, and return the local side. */
247 signal(SIGTERM, killchild);
248 signal(SIGINT, killchild);
249 signal(SIGHUP, killchild);
258 BUF *allocbuf(BUF *, int, int);
261 void run_err(const char *,...);
262 void verifydir(char *);
266 int errs, remin, remout;
267 int pflag, iamremote, iamrecursive, targetshouldbedirectory;
270 char cmd[CMDNEEDS]; /* must hold "rcp -r -p -d\0" */
273 void rsource(char *, struct stat *);
274 void sink(int, char *[]);
275 void source(int, char *[]);
276 void tolocal(int, char *[]);
277 void toremote(char *, int, char *[]);
281 main(int argc, char **argv)
283 int ch, fflag, tflag, status, n;
285 char *targ, *endp, **newargv;
289 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
292 /* Copy argv, because we modify it */
293 newargv = xcalloc(MAX(argc + 1, 1), sizeof(*newargv));
294 for (n = 0; n < argc; n++)
295 newargv[n] = xstrdup(argv[n]);
298 __progname = ssh_get_progname(argv[0]);
300 memset(&args, '\0', sizeof(args));
302 addargs(&args, "%s", ssh_program);
303 addargs(&args, "-x");
304 addargs(&args, "-oForwardAgent no");
305 addargs(&args, "-oPermitLocalCommand no");
306 addargs(&args, "-oClearAllForwardings yes");
309 while ((ch = getopt(argc, argv, "dfl:prtvBCc:i:P:q1246S:o:F:")) != -1)
311 /* User-visible flags. */
317 addargs(&args, "-%c", ch);
323 addargs(&args, "-%c%s", ch, optarg);
326 addargs(&args, "-p%s", optarg);
329 addargs(&args, "-oBatchmode yes");
332 speed = strtod(optarg, &endp);
333 if (speed <= 0 || *endp != '\0')
335 limit_rate = speed * 1024;
344 ssh_program = xstrdup(optarg);
347 addargs(&args, "-v");
351 addargs(&args, "-q");
355 /* Server options. */
357 targetshouldbedirectory = 1;
359 case 'f': /* "from" */
367 setmode(0, O_BINARY);
376 if ((pwd = getpwuid(userid = getuid())) == NULL)
377 fatal("unknown user %u", (u_int) userid);
379 if (!isatty(STDERR_FILENO))
382 remin = STDIN_FILENO;
383 remout = STDOUT_FILENO;
386 /* Follow "protocol", send data. */
399 targetshouldbedirectory = 1;
403 /* Command to be executed on remote system using "ssh". */
404 (void) snprintf(cmd, sizeof cmd, "scp%s%s%s%s",
405 verbose_mode ? " -v" : "",
406 iamrecursive ? " -r" : "", pflag ? " -p" : "",
407 targetshouldbedirectory ? " -d" : "");
409 (void) signal(SIGPIPE, lostconn);
411 if ((targ = colon(argv[argc - 1]))) /* Dest is remote host. */
412 toremote(targ, argc, argv);
414 if (targetshouldbedirectory)
415 verifydir(argv[argc - 1]);
416 tolocal(argc, argv); /* Dest is local host. */
419 * Finally check the exit status of the ssh process, if one was forked
420 * and no error has occured yet
422 if (do_cmd_pid != -1 && errs == 0) {
426 (void) close(remout);
427 if (waitpid(do_cmd_pid, &status, 0) == -1)
430 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
438 toremote(char *targ, int argc, char **argv)
440 char *bp, *host, *src, *suser, *thost, *tuser, *arg;
444 memset(&alist, '\0', sizeof(alist));
451 arg = xstrdup(argv[argc - 1]);
452 if ((thost = strrchr(arg, '@'))) {
463 if (tuser != NULL && !okname(tuser)) {
468 for (i = 0; i < argc - 1; i++) {
469 src = colon(argv[i]);
470 if (src) { /* remote to remote */
472 addargs(&alist, "%s", ssh_program);
474 addargs(&alist, "-v");
475 addargs(&alist, "-x");
476 addargs(&alist, "-oClearAllForwardings yes");
477 addargs(&alist, "-n");
482 host = strrchr(argv[i], '@');
486 host = cleanhostname(host);
489 suser = pwd->pw_name;
490 else if (!okname(suser))
492 addargs(&alist, "-l");
493 addargs(&alist, "%s", suser);
495 host = cleanhostname(argv[i]);
497 addargs(&alist, "%s", host);
498 addargs(&alist, "%s", cmd);
499 addargs(&alist, "%s", src);
500 addargs(&alist, "%s%s%s:%s",
501 tuser ? tuser : "", tuser ? "@" : "",
503 if (do_local_cmd(&alist) != 0)
505 } else { /* local to remote */
507 xasprintf(&bp, "%s -t %s", cmd, targ);
508 host = cleanhostname(thost);
509 if (do_cmd(host, tuser, bp, &remin,
523 tolocal(int argc, char **argv)
525 char *bp, *host, *src, *suser;
529 memset(&alist, '\0', sizeof(alist));
532 for (i = 0; i < argc - 1; i++) {
533 if (!(src = colon(argv[i]))) { /* Local to local. */
535 addargs(&alist, "%s", _PATH_CP);
537 addargs(&alist, "-r");
539 addargs(&alist, "-p");
540 addargs(&alist, "%s", argv[i]);
541 addargs(&alist, "%s", argv[argc-1]);
542 if (do_local_cmd(&alist))
549 if ((host = strrchr(argv[i], '@')) == NULL) {
556 suser = pwd->pw_name;
558 host = cleanhostname(host);
559 xasprintf(&bp, "%s -f %s", cmd, src);
560 if (do_cmd(host, suser, bp, &remin, &remout) < 0) {
566 sink(1, argv + argc - 1);
573 source(int argc, char **argv)
578 off_t i, amt, statbytes;
580 int fd = -1, haderr, indx;
581 char *last, *name, buf[2048];
584 for (indx = 0; indx < argc; ++indx) {
588 while (len > 1 && name[len-1] == '/')
590 if (strchr(name, '\n') != NULL) {
591 run_err("%s: skipping, filename contains a newline",
595 if ((fd = open(name, O_RDONLY, 0)) < 0)
597 if (fstat(fd, &stb) < 0) {
598 syserr: run_err("%s: %s", name, strerror(errno));
601 switch (stb.st_mode & S_IFMT) {
611 run_err("%s: not a regular file", name);
614 if ((last = strrchr(name, '/')) == NULL)
621 * Make it compatible with possible future
622 * versions expecting microseconds.
624 (void) snprintf(buf, sizeof buf, "T%lu 0 %lu 0\n",
625 (u_long) stb.st_mtime,
626 (u_long) stb.st_atime);
627 (void) atomicio(vwrite, remout, buf, strlen(buf));
631 #define FILEMODEMASK (S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO)
632 snprintf(buf, sizeof buf, "C%04o %lld %s\n",
633 (u_int) (stb.st_mode & FILEMODEMASK),
634 (long long)stb.st_size, last);
636 fprintf(stderr, "Sending file modes: %s", buf);
638 (void) atomicio(vwrite, remout, buf, strlen(buf));
641 if ((bp = allocbuf(&buffer, fd, 2048)) == NULL) {
642 next: if (fd != -1) {
649 start_progress_meter(curfile, stb.st_size, &statbytes);
650 /* Keep writing after an error so that we stay sync'd up. */
651 for (haderr = i = 0; i < stb.st_size; i += bp->cnt) {
653 if (i + amt > stb.st_size)
654 amt = stb.st_size - i;
656 result = atomicio(read, fd, bp->buf, amt);
661 (void) atomicio(vwrite, remout, bp->buf, amt);
663 result = atomicio(vwrite, remout, bp->buf, amt);
672 stop_progress_meter();
675 if (close(fd) < 0 && !haderr)
680 (void) atomicio(vwrite, remout, "", 1);
682 run_err("%s: %s", name, strerror(haderr));
688 rsource(char *name, struct stat *statp)
692 char *last, *vect[1], path[1100];
694 if (!(dirp = opendir(name))) {
695 run_err("%s: %s", name, strerror(errno));
698 last = strrchr(name, '/');
704 (void) snprintf(path, sizeof(path), "T%lu 0 %lu 0\n",
705 (u_long) statp->st_mtime,
706 (u_long) statp->st_atime);
707 (void) atomicio(vwrite, remout, path, strlen(path));
708 if (response() < 0) {
713 (void) snprintf(path, sizeof path, "D%04o %d %.1024s\n",
714 (u_int) (statp->st_mode & FILEMODEMASK), 0, last);
716 fprintf(stderr, "Entering directory: %s", path);
717 (void) atomicio(vwrite, remout, path, strlen(path));
718 if (response() < 0) {
722 while ((dp = readdir(dirp)) != NULL) {
725 if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
727 if (strlen(name) + 1 + strlen(dp->d_name) >= sizeof(path) - 1) {
728 run_err("%s/%s: name too long", name, dp->d_name);
731 (void) snprintf(path, sizeof path, "%s/%s", name, dp->d_name);
735 (void) closedir(dirp);
736 (void) atomicio(vwrite, remout, "E\n", 2);
743 static struct timeval bwstart, bwend;
744 static int lamt, thresh = 16384;
746 struct timespec ts, rm;
748 if (!timerisset(&bwstart)) {
749 gettimeofday(&bwstart, NULL);
757 gettimeofday(&bwend, NULL);
758 timersub(&bwend, &bwstart, &bwend);
759 if (!timerisset(&bwend))
763 waitlen = (double)1000000L * lamt / limit_rate;
765 bwstart.tv_sec = waitlen / 1000000L;
766 bwstart.tv_usec = waitlen % 1000000L;
768 if (timercmp(&bwstart, &bwend, >)) {
769 timersub(&bwstart, &bwend, &bwend);
771 /* Adjust the wait time */
776 } else if (bwend.tv_usec < 100) {
782 TIMEVAL_TO_TIMESPEC(&bwend, &ts);
783 while (nanosleep(&ts, &rm) == -1) {
791 gettimeofday(&bwstart, NULL);
795 sink(int argc, char **argv)
805 int amt, exists, first, ofd;
806 mode_t mode, omode, mask;
807 off_t size, statbytes;
808 int setimes, targisdir, wrerrno = 0;
809 char ch, *cp, *np, *targ, *why, *vect[1], buf[2048];
810 struct timeval tv[2];
814 #define SCREWUP(str) { why = str; goto screwup; }
816 setimes = targisdir = 0;
821 run_err("ambiguous target");
825 if (targetshouldbedirectory)
828 (void) atomicio(vwrite, remout, "", 1);
829 if (stat(targ, &stb) == 0 && S_ISDIR(stb.st_mode))
831 for (first = 1;; first = 0) {
833 if (atomicio(read, remin, cp, 1) != 1)
836 SCREWUP("unexpected <newline>");
838 if (atomicio(read, remin, &ch, sizeof(ch)) != sizeof(ch))
839 SCREWUP("lost connection");
841 } while (cp < &buf[sizeof(buf) - 1] && ch != '\n');
844 fprintf(stderr, "Sink: %s", buf);
846 if (buf[0] == '\01' || buf[0] == '\02') {
848 (void) atomicio(vwrite, STDERR_FILENO,
849 buf + 1, strlen(buf + 1));
856 (void) atomicio(vwrite, remout, "", 1);
866 mtime.tv_sec = strtol(cp, &cp, 10);
867 if (!cp || *cp++ != ' ')
868 SCREWUP("mtime.sec not delimited");
869 mtime.tv_usec = strtol(cp, &cp, 10);
870 if (!cp || *cp++ != ' ')
871 SCREWUP("mtime.usec not delimited");
872 atime.tv_sec = strtol(cp, &cp, 10);
873 if (!cp || *cp++ != ' ')
874 SCREWUP("atime.sec not delimited");
875 atime.tv_usec = strtol(cp, &cp, 10);
876 if (!cp || *cp++ != '\0')
877 SCREWUP("atime.usec not delimited");
878 (void) atomicio(vwrite, remout, "", 1);
881 if (*cp != 'C' && *cp != 'D') {
883 * Check for the case "rcp remote:foo\* local:bar".
884 * In this case, the line "No match." can be returned
885 * by the shell before the rcp command on the remote is
886 * executed so the ^Aerror_message convention isn't
893 SCREWUP("expected control record");
896 for (++cp; cp < buf + 5; cp++) {
897 if (*cp < '0' || *cp > '7')
899 mode = (mode << 3) | (*cp - '0');
902 SCREWUP("mode not delimited");
904 for (size = 0; isdigit(*cp);)
905 size = size * 10 + (*cp++ - '0');
907 SCREWUP("size not delimited");
908 if ((strchr(cp, '/') != NULL) || (strcmp(cp, "..") == 0)) {
909 run_err("error: unexpected filename: %s", cp);
913 static char *namebuf;
914 static size_t cursize;
917 need = strlen(targ) + strlen(cp) + 250;
918 if (need > cursize) {
921 namebuf = xmalloc(need);
924 (void) snprintf(namebuf, need, "%s%s%s", targ,
925 strcmp(targ, "/") ? "/" : "", cp);
930 exists = stat(np, &stb) == 0;
932 int mod_flag = pflag;
934 SCREWUP("received directory without -r");
936 if (!S_ISDIR(stb.st_mode)) {
941 (void) chmod(np, mode);
943 /* Handle copying from a read-only
946 if (mkdir(np, mode | S_IRWXU) < 0)
949 vect[0] = xstrdup(np);
953 if (utimes(vect[0], tv) < 0)
954 run_err("%s: set times: %s",
955 vect[0], strerror(errno));
958 (void) chmod(vect[0], mode);
965 if ((ofd = open(np, O_WRONLY|O_CREAT, mode)) < 0) {
966 bad: run_err("%s: %s", np, strerror(errno));
969 (void) atomicio(vwrite, remout, "", 1);
970 if ((bp = allocbuf(&buffer, ofd, 4096)) == NULL) {
979 start_progress_meter(curfile, size, &statbytes);
980 for (count = i = 0; i < size; i += 4096) {
986 j = atomicio(read, remin, cp, amt);
988 run_err("%s", j ? strerror(errno) :
989 "dropped connection");
1000 if (count == bp->cnt) {
1001 /* Keep reading so we stay sync'd up. */
1003 if (atomicio(vwrite, ofd, bp->buf,
1014 stop_progress_meter();
1015 if (count != 0 && wrerr == NO &&
1016 atomicio(vwrite, ofd, bp->buf, count) != count) {
1020 if (wrerr == NO && ftruncate(ofd, size) != 0) {
1021 run_err("%s: truncate: %s", np, strerror(errno));
1025 if (exists || omode != mode)
1027 if (fchmod(ofd, omode)) {
1028 #else /* HAVE_FCHMOD */
1029 if (chmod(np, omode)) {
1030 #endif /* HAVE_FCHMOD */
1031 run_err("%s: set mode: %s",
1032 np, strerror(errno));
1036 if (!exists && omode != mode)
1038 if (fchmod(ofd, omode & ~mask)) {
1039 #else /* HAVE_FCHMOD */
1040 if (chmod(np, omode & ~mask)) {
1041 #endif /* HAVE_FCHMOD */
1042 run_err("%s: set mode: %s",
1043 np, strerror(errno));
1047 if (close(ofd) == -1) {
1052 if (setimes && wrerr == NO) {
1054 if (utimes(np, tv) < 0) {
1055 run_err("%s: set times: %s",
1056 np, strerror(errno));
1062 run_err("%s: %s", np, strerror(wrerrno));
1065 (void) atomicio(vwrite, remout, "", 1);
1072 run_err("protocol error: %s", why);
1079 char ch, *cp, resp, rbuf[2048];
1081 if (atomicio(read, remin, &resp, sizeof(resp)) != sizeof(resp))
1091 case 1: /* error, followed by error msg */
1092 case 2: /* fatal error, "" */
1094 if (atomicio(read, remin, &ch, sizeof(ch)) != sizeof(ch))
1097 } while (cp < &rbuf[sizeof(rbuf) - 1] && ch != '\n');
1100 (void) atomicio(vwrite, STDERR_FILENO, rbuf, cp - rbuf);
1112 (void) fprintf(stderr,
1113 "usage: scp [-1246BCpqrv] [-c cipher] [-F ssh_config] [-i identity_file]\n"
1114 " [-l limit] [-o ssh_option] [-P port] [-S program]\n"
1115 " [[user@]host1:]file1 [...] [[user@]host2:]file2\n");
1120 run_err(const char *fmt,...)
1126 if (fp != NULL || (remout != -1 && (fp = fdopen(remout, "w")))) {
1127 (void) fprintf(fp, "%c", 0x01);
1128 (void) fprintf(fp, "scp: ");
1130 (void) vfprintf(fp, fmt, ap);
1132 (void) fprintf(fp, "\n");
1138 vfprintf(stderr, fmt, ap);
1140 fprintf(stderr, "\n");
1149 if (!stat(cp, &stb)) {
1150 if (S_ISDIR(stb.st_mode))
1154 run_err("%s: %s", cp, strerror(errno));
1169 if (!isalpha(c) && !isdigit(c)) {
1184 bad: fprintf(stderr, "%s: invalid user name\n", cp0);
1189 allocbuf(BUF *bp, int fd, int blksize)
1192 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
1195 if (fstat(fd, &stb) < 0) {
1196 run_err("fstat: %s", strerror(errno));
1199 size = roundup(stb.st_blksize, blksize);
1202 #else /* HAVE_STRUCT_STAT_ST_BLKSIZE */
1204 #endif /* HAVE_STRUCT_STAT_ST_BLKSIZE */
1205 if (bp->cnt >= size)
1207 if (bp->buf == NULL)
1208 bp->buf = xmalloc(size);
1210 bp->buf = xrealloc(bp->buf, 1, size);
1211 memset(bp->buf, 0, size);
1220 write(STDERR_FILENO, "lost connection\n", 16);