[BACK]Return to scp.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / ssh

Annotation of src/usr.bin/ssh/scp.c, Revision 1.103

1.1       deraadt     1: /*
1.38      deraadt     2:  * scp - secure remote copy.  This is basically patched BSD rcp which
                      3:  * uses ssh to do the data transfer (instead of using rcmd).
1.27      markus      4:  *
1.38      deraadt     5:  * NOTE: This version should NOT be suid root.  (This uses ssh to
                      6:  * do the transfer and ssh has the necessary privileges.)
1.27      markus      7:  *
1.38      deraadt     8:  * 1995 Timo Rinne <tri@iki.fi>, Tatu Ylonen <ylo@cs.hut.fi>
                      9:  *
                     10:  * As far as I am concerned, the code I have written for this software
                     11:  * can be used freely for any purpose.  Any derived versions of this
                     12:  * software must be clearly marked as such, and if the derived work is
                     13:  * incompatible with the protocol description in the RFC file, it must be
                     14:  * called by a name other than "ssh" or "Secure Shell".
1.39      markus     15:  */
1.38      deraadt    16: /*
1.60      deraadt    17:  * Copyright (c) 1999 Theo de Raadt.  All rights reserved.
                     18:  * Copyright (c) 1999 Aaron Campbell.  All rights reserved.
1.27      markus     19:  *
1.38      deraadt    20:  * Redistribution and use in source and binary forms, with or without
                     21:  * modification, are permitted provided that the following conditions
                     22:  * are met:
                     23:  * 1. Redistributions of source code must retain the above copyright
                     24:  *    notice, this list of conditions and the following disclaimer.
                     25:  * 2. Redistributions in binary form must reproduce the above copyright
                     26:  *    notice, this list of conditions and the following disclaimer in the
                     27:  *    documentation and/or other materials provided with the distribution.
1.27      markus     28:  *
1.38      deraadt    29:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
                     30:  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
                     31:  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
                     32:  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
                     33:  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
                     34:  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
                     35:  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
                     36:  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
                     37:  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
                     38:  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
                     39:  */
1.1       deraadt    40:
                     41: /*
1.35      deraadt    42:  * Parts from:
                     43:  *
1.1       deraadt    44:  * Copyright (c) 1983, 1990, 1992, 1993, 1995
                     45:  *     The Regents of the University of California.  All rights reserved.
                     46:  *
                     47:  * Redistribution and use in source and binary forms, with or without
                     48:  * modification, are permitted provided that the following conditions
                     49:  * are met:
                     50:  * 1. Redistributions of source code must retain the above copyright
                     51:  *    notice, this list of conditions and the following disclaimer.
                     52:  * 2. Redistributions in binary form must reproduce the above copyright
                     53:  *    notice, this list of conditions and the following disclaimer in the
                     54:  *    documentation and/or other materials provided with the distribution.
1.103   ! millert    55:  * 3. Neither the name of the University nor the names of its contributors
1.1       deraadt    56:  *    may be used to endorse or promote products derived from this software
                     57:  *    without specific prior written permission.
                     58:  *
                     59:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     60:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     61:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     62:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     63:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     64:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     65:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     66:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     67:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     68:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     69:  * SUCH DAMAGE.
                     70:  *
                     71:  */
                     72:
                     73: #include "includes.h"
1.103   ! millert    74: RCSID("$OpenBSD: scp.c,v 1.102 2003/03/05 22:33:43 markus Exp $");
1.1       deraadt    75:
                     76: #include "xmalloc.h"
1.51      markus     77: #include "atomicio.h"
1.50      markus     78: #include "pathnames.h"
1.51      markus     79: #include "log.h"
1.69      mouring    80: #include "misc.h"
1.97      fgsch      81: #include "progressmeter.h"
1.1       deraadt    82:
1.35      deraadt    83: int do_cmd(char *host, char *remuser, char *cmd, int *fdin, int *fdout, int argc);
1.11      aaron      84:
1.99      markus     85: void bwlimit(int);
                     86:
1.70      mouring    87: /* Struct for addargs */
                     88: arglist args;
1.41      markus     89:
1.99      markus     90: /* Bandwidth limit */
                     91: off_t limit = 0;
                     92:
1.11      aaron      93: /* Name of current file being transferred. */
                     94: char *curfile;
1.4       aaron      95:
1.1       deraadt    96: /* This is set to non-zero to enable verbose mode. */
1.17      markus     97: int verbose_mode = 0;
1.1       deraadt    98:
1.6       aaron      99: /* This is set to zero if the progressmeter is not desired. */
                    100: int showprogress = 1;
                    101:
1.34      deraadt   102: /* This is the program to execute for the secured connection. ("ssh" or -S) */
1.50      markus    103: char *ssh_program = _PATH_SSH_PROGRAM;
1.34      deraadt   104:
1.92      markus    105: /* This is used to store the pid of ssh_program */
                    106: pid_t do_cmd_pid;
                    107:
1.20      markus    108: /*
                    109:  * This function executes the given command as the specified user on the
                    110:  * given host.  This returns < 0 if execution fails, and >= 0 otherwise. This
                    111:  * assigns the input and output file descriptors on success.
                    112:  */
1.1       deraadt   113:
1.27      markus    114: int
1.35      deraadt   115: do_cmd(char *host, char *remuser, char *cmd, int *fdin, int *fdout, int argc)
1.1       deraadt   116: {
1.18      markus    117:        int pin[2], pout[2], reserved[2];
                    118:
                    119:        if (verbose_mode)
1.78      deraadt   120:                fprintf(stderr,
                    121:                    "Executing: program %s host %s, user %s, command %s\n",
                    122:                    ssh_program, host,
                    123:                    remuser ? remuser : "(unspecified)", cmd);
1.18      markus    124:
1.20      markus    125:        /*
                    126:         * Reserve two descriptors so that the real pipes won't get
                    127:         * descriptors 0 and 1 because that will screw up dup2 below.
                    128:         */
1.18      markus    129:        pipe(reserved);
                    130:
                    131:        /* Create a socket pair for communicating with ssh. */
                    132:        if (pipe(pin) < 0)
                    133:                fatal("pipe: %s", strerror(errno));
                    134:        if (pipe(pout) < 0)
                    135:                fatal("pipe: %s", strerror(errno));
                    136:
                    137:        /* Free the reserved descriptors. */
                    138:        close(reserved[0]);
                    139:        close(reserved[1]);
                    140:
                    141:        /* For a child to execute the command on the remote host using ssh. */
1.92      markus    142:        do_cmd_pid = fork();
                    143:        if (do_cmd_pid == 0) {
1.18      markus    144:                /* Child. */
                    145:                close(pin[1]);
                    146:                close(pout[0]);
                    147:                dup2(pin[0], 0);
                    148:                dup2(pout[1], 1);
                    149:                close(pin[0]);
                    150:                close(pout[1]);
                    151:
1.41      markus    152:                args.list[0] = ssh_program;
                    153:                if (remuser != NULL)
1.70      mouring   154:                        addargs(&args, "-l%s", remuser);
                    155:                addargs(&args, "%s", host);
                    156:                addargs(&args, "%s", cmd);
1.18      markus    157:
1.41      markus    158:                execvp(ssh_program, args.list);
1.34      deraadt   159:                perror(ssh_program);
1.18      markus    160:                exit(1);
1.92      markus    161:        } else if (do_cmd_pid == -1) {
                    162:                fatal("fork: %s", strerror(errno));
1.18      markus    163:        }
                    164:        /* Parent.  Close the other side, and return the local side. */
                    165:        close(pin[0]);
                    166:        *fdout = pin[1];
                    167:        close(pout[1]);
                    168:        *fdin = pout[0];
                    169:        return 0;
1.1       deraadt   170: }
                    171:
                    172: typedef struct {
                    173:        int cnt;
                    174:        char *buf;
                    175: } BUF;
                    176:
1.18      markus    177: BUF *allocbuf(BUF *, int, int);
                    178: void lostconn(int);
                    179: void nospace(void);
                    180: int okname(char *);
                    181: void run_err(const char *,...);
                    182: void verifydir(char *);
1.1       deraadt   183:
                    184: struct passwd *pwd;
1.18      markus    185: uid_t userid;
1.1       deraadt   186: int errs, remin, remout;
                    187: int pflag, iamremote, iamrecursive, targetshouldbedirectory;
                    188:
                    189: #define        CMDNEEDS        64
                    190: char cmd[CMDNEEDS];            /* must hold "rcp -r -p -d\0" */
                    191:
1.18      markus    192: int response(void);
                    193: void rsource(char *, struct stat *);
                    194: void sink(int, char *[]);
                    195: void source(int, char *[]);
                    196: void tolocal(int, char *[]);
                    197: void toremote(char *, int, char *[]);
                    198: void usage(void);
1.1       deraadt   199:
                    200: int
                    201: main(argc, argv)
                    202:        int argc;
                    203:        char *argv[];
                    204: {
1.92      markus    205:        int ch, fflag, tflag, status;
1.99      markus    206:        double speed;
                    207:        char *targ, *endp;
1.1       deraadt   208:        extern char *optarg;
                    209:        extern int optind;
                    210:
1.41      markus    211:        args.list = NULL;
1.91      deraadt   212:        addargs(&args, "ssh");          /* overwritten with ssh_program */
1.70      mouring   213:        addargs(&args, "-x");
1.82      stevesk   214:        addargs(&args, "-oForwardAgent no");
1.84      stevesk   215:        addargs(&args, "-oClearAllForwardings yes");
1.41      markus    216:
1.1       deraadt   217:        fflag = tflag = 0;
1.100     markus    218:        while ((ch = getopt(argc, argv, "dfl:prtvBCc:i:P:q1246S:o:F:")) != -1)
1.18      markus    219:                switch (ch) {
                    220:                /* User-visible flags. */
1.100     markus    221:                case '1':
                    222:                case '2':
1.23      markus    223:                case '4':
1.41      markus    224:                case '6':
                    225:                case 'C':
1.70      mouring   226:                        addargs(&args, "-%c", ch);
1.41      markus    227:                        break;
                    228:                case 'o':
                    229:                case 'c':
                    230:                case 'i':
1.83      stevesk   231:                case 'F':
1.70      mouring   232:                        addargs(&args, "-%c%s", ch, optarg);
1.23      markus    233:                        break;
1.41      markus    234:                case 'P':
1.70      mouring   235:                        addargs(&args, "-p%s", optarg);
1.41      markus    236:                        break;
                    237:                case 'B':
1.70      mouring   238:                        addargs(&args, "-oBatchmode yes");
1.23      markus    239:                        break;
1.99      markus    240:                case 'l':
                    241:                        speed = strtod(optarg, &endp);
                    242:                        if (speed <= 0 || *endp != '\0')
                    243:                                usage();
                    244:                        limit = speed * 1024;
                    245:                        break;
1.1       deraadt   246:                case 'p':
                    247:                        pflag = 1;
                    248:                        break;
                    249:                case 'r':
                    250:                        iamrecursive = 1;
                    251:                        break;
1.34      deraadt   252:                case 'S':
1.41      markus    253:                        ssh_program = xstrdup(optarg);
                    254:                        break;
                    255:                case 'v':
1.72      markus    256:                        addargs(&args, "-v");
1.41      markus    257:                        verbose_mode = 1;
                    258:                        break;
                    259:                case 'q':
                    260:                        showprogress = 0;
1.34      deraadt   261:                        break;
                    262:
1.18      markus    263:                /* Server options. */
1.1       deraadt   264:                case 'd':
                    265:                        targetshouldbedirectory = 1;
                    266:                        break;
1.18      markus    267:                case 'f':       /* "from" */
1.1       deraadt   268:                        iamremote = 1;
                    269:                        fflag = 1;
                    270:                        break;
1.18      markus    271:                case 't':       /* "to" */
1.1       deraadt   272:                        iamremote = 1;
                    273:                        tflag = 1;
                    274:                        break;
                    275:                default:
                    276:                        usage();
                    277:                }
                    278:        argc -= optind;
                    279:        argv += optind;
                    280:
                    281:        if ((pwd = getpwuid(userid = getuid())) == NULL)
1.18      markus    282:                fatal("unknown user %d", (int) userid);
1.1       deraadt   283:
1.18      markus    284:        if (!isatty(STDERR_FILENO))
1.7       deraadt   285:                showprogress = 0;
                    286:
1.1       deraadt   287:        remin = STDIN_FILENO;
                    288:        remout = STDOUT_FILENO;
                    289:
1.52      stevesk   290:        if (fflag) {
1.18      markus    291:                /* Follow "protocol", send data. */
                    292:                (void) response();
1.1       deraadt   293:                source(argc, argv);
                    294:                exit(errs != 0);
                    295:        }
1.18      markus    296:        if (tflag) {
                    297:                /* Receive data. */
1.1       deraadt   298:                sink(argc, argv);
                    299:                exit(errs != 0);
                    300:        }
                    301:        if (argc < 2)
                    302:                usage();
                    303:        if (argc > 2)
                    304:                targetshouldbedirectory = 1;
                    305:
                    306:        remin = remout = -1;
1.92      markus    307:        do_cmd_pid = -1;
1.1       deraadt   308:        /* Command to be executed on remote system using "ssh". */
1.55      deraadt   309:        (void) snprintf(cmd, sizeof cmd, "scp%s%s%s%s",
                    310:            verbose_mode ? " -v" : "",
1.35      deraadt   311:            iamrecursive ? " -r" : "", pflag ? " -p" : "",
                    312:            targetshouldbedirectory ? " -d" : "");
1.1       deraadt   313:
1.18      markus    314:        (void) signal(SIGPIPE, lostconn);
1.1       deraadt   315:
                    316:        if ((targ = colon(argv[argc - 1])))     /* Dest is remote host. */
                    317:                toremote(targ, argc, argv);
                    318:        else {
1.18      markus    319:                tolocal(argc, argv);    /* Dest is local host. */
1.1       deraadt   320:                if (targetshouldbedirectory)
                    321:                        verifydir(argv[argc - 1]);
1.92      markus    322:        }
                    323:        /*
                    324:         * Finally check the exit status of the ssh process, if one was forked
                    325:         * and no error has occured yet
                    326:         */
                    327:        if (do_cmd_pid != -1 && errs == 0) {
                    328:                if (remin != -1)
                    329:                    (void) close(remin);
                    330:                if (remout != -1)
                    331:                    (void) close(remout);
                    332:                if (waitpid(do_cmd_pid, &status, 0) == -1)
                    333:                        errs = 1;
                    334:                else {
                    335:                        if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
                    336:                                errs = 1;
                    337:                }
1.1       deraadt   338:        }
                    339:        exit(errs != 0);
                    340: }
                    341:
                    342: void
                    343: toremote(targ, argc, argv)
                    344:        char *targ, *argv[];
                    345:        int argc;
                    346: {
                    347:        int i, len;
                    348:        char *bp, *host, *src, *suser, *thost, *tuser;
                    349:
                    350:        *targ++ = 0;
                    351:        if (*targ == 0)
                    352:                targ = ".";
                    353:
1.94      markus    354:        if ((thost = strrchr(argv[argc - 1], '@'))) {
1.1       deraadt   355:                /* user@host */
                    356:                *thost++ = 0;
                    357:                tuser = argv[argc - 1];
                    358:                if (*tuser == '\0')
                    359:                        tuser = NULL;
                    360:        } else {
                    361:                thost = argv[argc - 1];
                    362:                tuser = NULL;
                    363:        }
                    364:
                    365:        for (i = 0; i < argc - 1; i++) {
                    366:                src = colon(argv[i]);
1.18      markus    367:                if (src) {      /* remote to remote */
1.84      stevesk   368:                        static char *ssh_options =
1.89      markus    369:                            "-x -o'ClearAllForwardings yes'";
1.1       deraadt   370:                        *src++ = 0;
                    371:                        if (*src == 0)
                    372:                                src = ".";
1.94      markus    373:                        host = strrchr(argv[i], '@');
1.34      deraadt   374:                        len = strlen(ssh_program) + strlen(argv[i]) +
1.35      deraadt   375:                            strlen(src) + (tuser ? strlen(tuser) : 0) +
1.84      stevesk   376:                            strlen(thost) + strlen(targ) +
                    377:                            strlen(ssh_options) + CMDNEEDS + 20;
1.18      markus    378:                        bp = xmalloc(len);
1.1       deraadt   379:                        if (host) {
                    380:                                *host++ = 0;
1.23      markus    381:                                host = cleanhostname(host);
1.1       deraadt   382:                                suser = argv[i];
                    383:                                if (*suser == '\0')
                    384:                                        suser = pwd->pw_name;
1.102     markus    385:                                else if (!okname(suser)) {
                    386:                                        xfree(bp);
1.1       deraadt   387:                                        continue;
1.102     markus    388:                                }
                    389:                                if (tuser && !okname(tuser)) {
                    390:                                        xfree(bp);
1.101     markus    391:                                        continue;
1.102     markus    392:                                }
1.55      deraadt   393:                                snprintf(bp, len,
1.84      stevesk   394:                                    "%s%s %s -n "
1.55      deraadt   395:                                    "-l %s %s %s %s '%s%s%s:%s'",
1.44      deraadt   396:                                    ssh_program, verbose_mode ? " -v" : "",
1.84      stevesk   397:                                    ssh_options, suser, host, cmd, src,
1.44      deraadt   398:                                    tuser ? tuser : "", tuser ? "@" : "",
                    399:                                    thost, targ);
1.23      markus    400:                        } else {
                    401:                                host = cleanhostname(argv[i]);
1.55      deraadt   402:                                snprintf(bp, len,
1.84      stevesk   403:                                    "exec %s%s %s -n %s "
1.55      deraadt   404:                                    "%s %s '%s%s%s:%s'",
1.44      deraadt   405:                                    ssh_program, verbose_mode ? " -v" : "",
1.84      stevesk   406:                                    ssh_options, host, cmd, src,
1.44      deraadt   407:                                    tuser ? tuser : "", tuser ? "@" : "",
                    408:                                    thost, targ);
1.23      markus    409:                        }
1.18      markus    410:                        if (verbose_mode)
                    411:                                fprintf(stderr, "Executing: %s\n", bp);
                    412:                        (void) system(bp);
                    413:                        (void) xfree(bp);
                    414:                } else {        /* local to remote */
1.1       deraadt   415:                        if (remin == -1) {
                    416:                                len = strlen(targ) + CMDNEEDS + 20;
1.18      markus    417:                                bp = xmalloc(len);
1.55      deraadt   418:                                (void) snprintf(bp, len, "%s -t %s", cmd, targ);
1.23      markus    419:                                host = cleanhostname(thost);
1.35      deraadt   420:                                if (do_cmd(host, tuser, bp, &remin,
                    421:                                    &remout, argc) < 0)
1.18      markus    422:                                        exit(1);
1.1       deraadt   423:                                if (response() < 0)
                    424:                                        exit(1);
1.18      markus    425:                                (void) xfree(bp);
1.1       deraadt   426:                        }
1.18      markus    427:                        source(1, argv + i);
1.1       deraadt   428:                }
                    429:        }
                    430: }
                    431:
                    432: void
                    433: tolocal(argc, argv)
                    434:        int argc;
                    435:        char *argv[];
                    436: {
                    437:        int i, len;
                    438:        char *bp, *host, *src, *suser;
                    439:
                    440:        for (i = 0; i < argc - 1; i++) {
1.18      markus    441:                if (!(src = colon(argv[i]))) {  /* Local to local. */
1.1       deraadt   442:                        len = strlen(_PATH_CP) + strlen(argv[i]) +
1.35      deraadt   443:                            strlen(argv[argc - 1]) + 20;
1.1       deraadt   444:                        bp = xmalloc(len);
1.55      deraadt   445:                        (void) snprintf(bp, len, "exec %s%s%s %s %s", _PATH_CP,
1.35      deraadt   446:                            iamrecursive ? " -r" : "", pflag ? " -p" : "",
                    447:                            argv[i], argv[argc - 1]);
1.18      markus    448:                        if (verbose_mode)
                    449:                                fprintf(stderr, "Executing: %s\n", bp);
1.1       deraadt   450:                        if (system(bp))
                    451:                                ++errs;
1.18      markus    452:                        (void) xfree(bp);
1.1       deraadt   453:                        continue;
                    454:                }
                    455:                *src++ = 0;
                    456:                if (*src == 0)
                    457:                        src = ".";
1.94      markus    458:                if ((host = strrchr(argv[i], '@')) == NULL) {
1.1       deraadt   459:                        host = argv[i];
                    460:                        suser = NULL;
                    461:                } else {
                    462:                        *host++ = 0;
                    463:                        suser = argv[i];
                    464:                        if (*suser == '\0')
                    465:                                suser = pwd->pw_name;
                    466:                }
1.23      markus    467:                host = cleanhostname(host);
1.1       deraadt   468:                len = strlen(src) + CMDNEEDS + 20;
1.18      markus    469:                bp = xmalloc(len);
1.55      deraadt   470:                (void) snprintf(bp, len, "%s -f %s", cmd, src);
1.35      deraadt   471:                if (do_cmd(host, suser, bp, &remin, &remout, argc) < 0) {
1.18      markus    472:                        (void) xfree(bp);
                    473:                        ++errs;
                    474:                        continue;
1.1       deraadt   475:                }
1.18      markus    476:                xfree(bp);
1.1       deraadt   477:                sink(1, argv + argc - 1);
1.18      markus    478:                (void) close(remin);
1.1       deraadt   479:                remin = remout = -1;
                    480:        }
                    481: }
                    482:
                    483: void
                    484: source(argc, argv)
                    485:        int argc;
                    486:        char *argv[];
                    487: {
                    488:        struct stat stb;
                    489:        static BUF buffer;
                    490:        BUF *bp;
1.97      fgsch     491:        off_t i, amt, result, statbytes;
1.68      markus    492:        int fd, haderr, indx;
1.1       deraadt   493:        char *last, *name, buf[2048];
1.65      deraadt   494:        int len;
1.1       deraadt   495:
                    496:        for (indx = 0; indx < argc; ++indx) {
1.18      markus    497:                name = argv[indx];
1.11      aaron     498:                statbytes = 0;
1.65      deraadt   499:                len = strlen(name);
                    500:                while (len > 1 && name[len-1] == '/')
                    501:                        name[--len] = '\0';
1.85      markus    502:                if (strchr(name, '\n') != NULL) {
                    503:                        run_err("%s: skipping, filename contains a newline",
                    504:                            name);
                    505:                        goto next;
                    506:                }
1.1       deraadt   507:                if ((fd = open(name, O_RDONLY, 0)) < 0)
                    508:                        goto syserr;
                    509:                if (fstat(fd, &stb) < 0) {
                    510: syserr:                        run_err("%s: %s", name, strerror(errno));
                    511:                        goto next;
                    512:                }
                    513:                switch (stb.st_mode & S_IFMT) {
                    514:                case S_IFREG:
                    515:                        break;
                    516:                case S_IFDIR:
                    517:                        if (iamrecursive) {
                    518:                                rsource(name, &stb);
                    519:                                goto next;
                    520:                        }
                    521:                        /* FALLTHROUGH */
                    522:                default:
                    523:                        run_err("%s: not a regular file", name);
                    524:                        goto next;
                    525:                }
                    526:                if ((last = strrchr(name, '/')) == NULL)
                    527:                        last = name;
                    528:                else
                    529:                        ++last;
1.11      aaron     530:                curfile = last;
1.1       deraadt   531:                if (pflag) {
                    532:                        /*
                    533:                         * Make it compatible with possible future
                    534:                         * versions expecting microseconds.
                    535:                         */
1.55      deraadt   536:                        (void) snprintf(buf, sizeof buf, "T%lu 0 %lu 0\n",
1.47      markus    537:                            (u_long) stb.st_mtime,
                    538:                            (u_long) stb.st_atime);
1.29      deraadt   539:                        (void) atomicio(write, remout, buf, strlen(buf));
1.1       deraadt   540:                        if (response() < 0)
                    541:                                goto next;
                    542:                }
                    543: #define        FILEMODEMASK    (S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO)
1.61      markus    544:                snprintf(buf, sizeof buf, "C%04o %lld %s\n",
1.47      markus    545:                    (u_int) (stb.st_mode & FILEMODEMASK),
1.62      markus    546:                    (long long)stb.st_size, last);
1.18      markus    547:                if (verbose_mode) {
                    548:                        fprintf(stderr, "Sending file modes: %s", buf);
                    549:                }
1.29      deraadt   550:                (void) atomicio(write, remout, buf, strlen(buf));
1.1       deraadt   551:                if (response() < 0)
                    552:                        goto next;
                    553:                if ((bp = allocbuf(&buffer, fd, 2048)) == NULL) {
1.18      markus    554: next:                  (void) close(fd);
1.1       deraadt   555:                        continue;
                    556:                }
1.97      fgsch     557:                if (showprogress)
                    558:                        start_progress_meter(curfile, stb.st_size, &statbytes);
1.1       deraadt   559:                /* Keep writing after an error so that we stay sync'd up. */
                    560:                for (haderr = i = 0; i < stb.st_size; i += bp->cnt) {
                    561:                        amt = bp->cnt;
                    562:                        if (i + amt > stb.st_size)
                    563:                                amt = stb.st_size - i;
                    564:                        if (!haderr) {
1.30      deraadt   565:                                result = atomicio(read, fd, bp->buf, amt);
1.1       deraadt   566:                                if (result != amt)
                    567:                                        haderr = result >= 0 ? EIO : errno;
                    568:                        }
                    569:                        if (haderr)
1.29      deraadt   570:                                (void) atomicio(write, remout, bp->buf, amt);
1.1       deraadt   571:                        else {
1.29      deraadt   572:                                result = atomicio(write, remout, bp->buf, amt);
1.1       deraadt   573:                                if (result != amt)
                    574:                                        haderr = result >= 0 ? EIO : errno;
1.4       aaron     575:                                statbytes += result;
1.1       deraadt   576:                        }
1.99      markus    577:                        if (limit)
                    578:                                bwlimit(amt);
1.1       deraadt   579:                }
1.18      markus    580:                if (showprogress)
1.97      fgsch     581:                        stop_progress_meter();
1.4       aaron     582:
1.1       deraadt   583:                if (close(fd) < 0 && !haderr)
                    584:                        haderr = errno;
                    585:                if (!haderr)
1.29      deraadt   586:                        (void) atomicio(write, remout, "", 1);
1.1       deraadt   587:                else
                    588:                        run_err("%s: %s", name, strerror(haderr));
1.18      markus    589:                (void) response();
1.1       deraadt   590:        }
                    591: }
                    592:
                    593: void
                    594: rsource(name, statp)
                    595:        char *name;
                    596:        struct stat *statp;
                    597: {
                    598:        DIR *dirp;
                    599:        struct dirent *dp;
                    600:        char *last, *vect[1], path[1100];
                    601:
                    602:        if (!(dirp = opendir(name))) {
                    603:                run_err("%s: %s", name, strerror(errno));
                    604:                return;
                    605:        }
                    606:        last = strrchr(name, '/');
                    607:        if (last == 0)
                    608:                last = name;
                    609:        else
                    610:                last++;
                    611:        if (pflag) {
1.55      deraadt   612:                (void) snprintf(path, sizeof(path), "T%lu 0 %lu 0\n",
1.47      markus    613:                    (u_long) statp->st_mtime,
                    614:                    (u_long) statp->st_atime);
1.29      deraadt   615:                (void) atomicio(write, remout, path, strlen(path));
1.1       deraadt   616:                if (response() < 0) {
                    617:                        closedir(dirp);
                    618:                        return;
                    619:                }
                    620:        }
1.55      deraadt   621:        (void) snprintf(path, sizeof path, "D%04o %d %.1024s\n",
1.47      markus    622:            (u_int) (statp->st_mode & FILEMODEMASK), 0, last);
1.18      markus    623:        if (verbose_mode)
                    624:                fprintf(stderr, "Entering directory: %s", path);
1.29      deraadt   625:        (void) atomicio(write, remout, path, strlen(path));
1.1       deraadt   626:        if (response() < 0) {
                    627:                closedir(dirp);
                    628:                return;
                    629:        }
1.63      stevesk   630:        while ((dp = readdir(dirp)) != NULL) {
1.1       deraadt   631:                if (dp->d_ino == 0)
                    632:                        continue;
                    633:                if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
                    634:                        continue;
                    635:                if (strlen(name) + 1 + strlen(dp->d_name) >= sizeof(path) - 1) {
                    636:                        run_err("%s/%s: name too long", name, dp->d_name);
                    637:                        continue;
                    638:                }
1.55      deraadt   639:                (void) snprintf(path, sizeof path, "%s/%s", name, dp->d_name);
1.1       deraadt   640:                vect[0] = path;
                    641:                source(1, vect);
                    642:        }
1.18      markus    643:        (void) closedir(dirp);
1.29      deraadt   644:        (void) atomicio(write, remout, "E\n", 2);
1.18      markus    645:        (void) response();
1.1       deraadt   646: }
                    647:
                    648: void
1.99      markus    649: bwlimit(int amount)
                    650: {
                    651:        static struct timeval bwstart, bwend;
                    652:        static int lamt, thresh = 16384;
                    653:        u_int64_t wait;
                    654:        struct timespec ts, rm;
                    655:
                    656:        if (!timerisset(&bwstart)) {
                    657:                gettimeofday(&bwstart, NULL);
                    658:                return;
                    659:        }
                    660:
                    661:        lamt += amount;
                    662:        if (lamt < thresh)
                    663:                return;
                    664:
                    665:        gettimeofday(&bwend, NULL);
                    666:        timersub(&bwend, &bwstart, &bwend);
                    667:        if (!timerisset(&bwend))
                    668:                return;
                    669:
                    670:        lamt *= 8;
                    671:        wait = (double)1000000L * lamt / limit;
                    672:
                    673:        bwstart.tv_sec = wait / 1000000L;
                    674:        bwstart.tv_usec = wait % 1000000L;
                    675:
                    676:        if (timercmp(&bwstart, &bwend, >)) {
                    677:                timersub(&bwstart, &bwend, &bwend);
                    678:
                    679:                /* Adjust the wait time */
                    680:                if (bwend.tv_sec) {
                    681:                        thresh /= 2;
                    682:                        if (thresh < 2048)
                    683:                                thresh = 2048;
                    684:                } else if (bwend.tv_usec < 100) {
                    685:                        thresh *= 2;
                    686:                        if (thresh > 32768)
                    687:                                thresh = 32768;
                    688:                }
                    689:
                    690:                TIMEVAL_TO_TIMESPEC(&bwend, &ts);
                    691:                while (nanosleep(&ts, &rm) == -1) {
                    692:                        if (errno != EINTR)
                    693:                                break;
                    694:                        ts = rm;
                    695:                }
                    696:        }
                    697:
                    698:        lamt = 0;
                    699:        gettimeofday(&bwstart, NULL);
                    700: }
                    701:
                    702: void
1.1       deraadt   703: sink(argc, argv)
                    704:        int argc;
                    705:        char *argv[];
                    706: {
                    707:        static BUF buffer;
                    708:        struct stat stb;
1.18      markus    709:        enum {
                    710:                YES, NO, DISPLAYED
                    711:        } wrerr;
1.1       deraadt   712:        BUF *bp;
                    713:        off_t i, j;
                    714:        int amt, count, exists, first, mask, mode, ofd, omode;
1.97      fgsch     715:        off_t size, statbytes;
1.36      deraadt   716:        int setimes, targisdir, wrerrno = 0;
1.1       deraadt   717:        char ch, *cp, *np, *targ, *why, *vect[1], buf[2048];
1.40      markus    718:        struct timeval tv[2];
1.1       deraadt   719:
1.66      stevesk   720: #define        atime   tv[0]
                    721: #define        mtime   tv[1]
1.75      deraadt   722: #define        SCREWUP(str)    do { why = str; goto screwup; } while (0)
1.1       deraadt   723:
                    724:        setimes = targisdir = 0;
                    725:        mask = umask(0);
                    726:        if (!pflag)
1.18      markus    727:                (void) umask(mask);
1.1       deraadt   728:        if (argc != 1) {
                    729:                run_err("ambiguous target");
                    730:                exit(1);
                    731:        }
                    732:        targ = *argv;
                    733:        if (targetshouldbedirectory)
                    734:                verifydir(targ);
1.18      markus    735:
1.29      deraadt   736:        (void) atomicio(write, remout, "", 1);
1.1       deraadt   737:        if (stat(targ, &stb) == 0 && S_ISDIR(stb.st_mode))
                    738:                targisdir = 1;
                    739:        for (first = 1;; first = 0) {
                    740:                cp = buf;
1.30      deraadt   741:                if (atomicio(read, remin, cp, 1) <= 0)
1.1       deraadt   742:                        return;
                    743:                if (*cp++ == '\n')
                    744:                        SCREWUP("unexpected <newline>");
                    745:                do {
1.30      deraadt   746:                        if (atomicio(read, remin, &ch, sizeof(ch)) != sizeof(ch))
1.1       deraadt   747:                                SCREWUP("lost connection");
                    748:                        *cp++ = ch;
                    749:                } while (cp < &buf[sizeof(buf) - 1] && ch != '\n');
                    750:                *cp = 0;
                    751:
                    752:                if (buf[0] == '\01' || buf[0] == '\02') {
                    753:                        if (iamremote == 0)
1.29      deraadt   754:                                (void) atomicio(write, STDERR_FILENO,
1.44      deraadt   755:                                    buf + 1, strlen(buf + 1));
1.1       deraadt   756:                        if (buf[0] == '\02')
                    757:                                exit(1);
                    758:                        ++errs;
                    759:                        continue;
                    760:                }
                    761:                if (buf[0] == 'E') {
1.29      deraadt   762:                        (void) atomicio(write, remout, "", 1);
1.1       deraadt   763:                        return;
                    764:                }
                    765:                if (ch == '\n')
                    766:                        *--cp = 0;
                    767:
                    768:                cp = buf;
                    769:                if (*cp == 'T') {
                    770:                        setimes++;
                    771:                        cp++;
1.66      stevesk   772:                        mtime.tv_sec = strtol(cp, &cp, 10);
                    773:                        if (!cp || *cp++ != ' ')
1.1       deraadt   774:                                SCREWUP("mtime.sec not delimited");
1.66      stevesk   775:                        mtime.tv_usec = strtol(cp, &cp, 10);
                    776:                        if (!cp || *cp++ != ' ')
1.1       deraadt   777:                                SCREWUP("mtime.usec not delimited");
1.66      stevesk   778:                        atime.tv_sec = strtol(cp, &cp, 10);
                    779:                        if (!cp || *cp++ != ' ')
1.1       deraadt   780:                                SCREWUP("atime.sec not delimited");
1.66      stevesk   781:                        atime.tv_usec = strtol(cp, &cp, 10);
                    782:                        if (!cp || *cp++ != '\0')
1.1       deraadt   783:                                SCREWUP("atime.usec not delimited");
1.29      deraadt   784:                        (void) atomicio(write, remout, "", 1);
1.1       deraadt   785:                        continue;
                    786:                }
                    787:                if (*cp != 'C' && *cp != 'D') {
                    788:                        /*
                    789:                         * Check for the case "rcp remote:foo\* local:bar".
                    790:                         * In this case, the line "No match." can be returned
                    791:                         * by the shell before the rcp command on the remote is
                    792:                         * executed so the ^Aerror_message convention isn't
                    793:                         * followed.
                    794:                         */
                    795:                        if (first) {
                    796:                                run_err("%s", cp);
                    797:                                exit(1);
                    798:                        }
                    799:                        SCREWUP("expected control record");
                    800:                }
                    801:                mode = 0;
                    802:                for (++cp; cp < buf + 5; cp++) {
                    803:                        if (*cp < '0' || *cp > '7')
                    804:                                SCREWUP("bad mode");
                    805:                        mode = (mode << 3) | (*cp - '0');
                    806:                }
                    807:                if (*cp++ != ' ')
                    808:                        SCREWUP("mode not delimited");
                    809:
1.63      stevesk   810:                for (size = 0; isdigit(*cp);)
1.1       deraadt   811:                        size = size * 10 + (*cp++ - '0');
                    812:                if (*cp++ != ' ')
                    813:                        SCREWUP("size not delimited");
                    814:                if (targisdir) {
                    815:                        static char *namebuf;
                    816:                        static int cursize;
                    817:                        size_t need;
                    818:
                    819:                        need = strlen(targ) + strlen(cp) + 250;
1.55      deraadt   820:                        if (need > cursize) {
1.58      danh      821:                                if (namebuf)
                    822:                                        xfree(namebuf);
1.18      markus    823:                                namebuf = xmalloc(need);
1.55      deraadt   824:                                cursize = need;
                    825:                        }
                    826:                        (void) snprintf(namebuf, need, "%s%s%s", targ,
1.88      mouring   827:                            strcmp(targ, "/") ? "/" : "", cp);
1.1       deraadt   828:                        np = namebuf;
                    829:                } else
                    830:                        np = targ;
1.12      aaron     831:                curfile = cp;
1.1       deraadt   832:                exists = stat(np, &stb) == 0;
                    833:                if (buf[0] == 'D') {
                    834:                        int mod_flag = pflag;
                    835:                        if (exists) {
                    836:                                if (!S_ISDIR(stb.st_mode)) {
                    837:                                        errno = ENOTDIR;
                    838:                                        goto bad;
                    839:                                }
                    840:                                if (pflag)
1.18      markus    841:                                        (void) chmod(np, mode);
1.1       deraadt   842:                        } else {
1.18      markus    843:                                /* Handle copying from a read-only
                    844:                                   directory */
1.1       deraadt   845:                                mod_flag = 1;
                    846:                                if (mkdir(np, mode | S_IRWXU) < 0)
                    847:                                        goto bad;
                    848:                        }
1.58      danh      849:                        vect[0] = xstrdup(np);
1.1       deraadt   850:                        sink(1, vect);
                    851:                        if (setimes) {
                    852:                                setimes = 0;
1.59      deraadt   853:                                if (utimes(vect[0], tv) < 0)
1.18      markus    854:                                        run_err("%s: set times: %s",
1.59      deraadt   855:                                            vect[0], strerror(errno));
1.1       deraadt   856:                        }
                    857:                        if (mod_flag)
1.59      deraadt   858:                                (void) chmod(vect[0], mode);
                    859:                        if (vect[0])
                    860:                                xfree(vect[0]);
1.1       deraadt   861:                        continue;
                    862:                }
                    863:                omode = mode;
                    864:                mode |= S_IWRITE;
1.71      markus    865:                if ((ofd = open(np, O_WRONLY|O_CREAT, mode)) < 0) {
1.1       deraadt   866: bad:                   run_err("%s: %s", np, strerror(errno));
                    867:                        continue;
                    868:                }
1.29      deraadt   869:                (void) atomicio(write, remout, "", 1);
1.1       deraadt   870:                if ((bp = allocbuf(&buffer, ofd, 4096)) == NULL) {
1.18      markus    871:                        (void) close(ofd);
1.1       deraadt   872:                        continue;
                    873:                }
                    874:                cp = bp->buf;
                    875:                wrerr = NO;
1.7       deraadt   876:
1.12      aaron     877:                statbytes = 0;
1.97      fgsch     878:                if (showprogress)
                    879:                        start_progress_meter(curfile, size, &statbytes);
1.1       deraadt   880:                for (count = i = 0; i < size; i += 4096) {
                    881:                        amt = 4096;
                    882:                        if (i + amt > size)
                    883:                                amt = size - i;
                    884:                        count += amt;
                    885:                        do {
1.43      markus    886:                                j = read(remin, cp, amt);
1.78      deraadt   887:                                if (j == -1 && (errno == EINTR ||
                    888:                                    errno == EAGAIN)) {
1.43      markus    889:                                        continue;
                    890:                                } else if (j <= 0) {
1.1       deraadt   891:                                        run_err("%s", j ? strerror(errno) :
1.63      stevesk   892:                                            "dropped connection");
1.1       deraadt   893:                                        exit(1);
                    894:                                }
                    895:                                amt -= j;
                    896:                                cp += j;
1.18      markus    897:                                statbytes += j;
1.1       deraadt   898:                        } while (amt > 0);
1.99      markus    899:
                    900:                        if (limit)
                    901:                                bwlimit(4096);
                    902:
1.1       deraadt   903:                        if (count == bp->cnt) {
                    904:                                /* Keep reading so we stay sync'd up. */
                    905:                                if (wrerr == NO) {
1.30      deraadt   906:                                        j = atomicio(write, ofd, bp->buf, count);
1.1       deraadt   907:                                        if (j != count) {
                    908:                                                wrerr = YES;
1.18      markus    909:                                                wrerrno = j >= 0 ? EIO : errno;
1.1       deraadt   910:                                        }
                    911:                                }
                    912:                                count = 0;
                    913:                                cp = bp->buf;
                    914:                        }
                    915:                }
1.7       deraadt   916:                if (showprogress)
1.97      fgsch     917:                        stop_progress_meter();
1.1       deraadt   918:                if (count != 0 && wrerr == NO &&
1.30      deraadt   919:                    (j = atomicio(write, ofd, bp->buf, count)) != count) {
1.1       deraadt   920:                        wrerr = YES;
1.18      markus    921:                        wrerrno = j >= 0 ? EIO : errno;
1.1       deraadt   922:                }
1.98      djm       923:                if (wrerr == NO && ftruncate(ofd, size) != 0) {
1.1       deraadt   924:                        run_err("%s: truncate: %s", np, strerror(errno));
                    925:                        wrerr = DISPLAYED;
                    926:                }
                    927:                if (pflag) {
                    928:                        if (exists || omode != mode)
                    929:                                if (fchmod(ofd, omode))
                    930:                                        run_err("%s: set mode: %s",
1.63      stevesk   931:                                            np, strerror(errno));
1.1       deraadt   932:                } else {
                    933:                        if (!exists && omode != mode)
                    934:                                if (fchmod(ofd, omode & ~mask))
                    935:                                        run_err("%s: set mode: %s",
1.63      stevesk   936:                                            np, strerror(errno));
1.1       deraadt   937:                }
1.33      provos    938:                if (close(ofd) == -1) {
                    939:                        wrerr = YES;
                    940:                        wrerrno = errno;
                    941:                }
1.18      markus    942:                (void) response();
1.1       deraadt   943:                if (setimes && wrerr == NO) {
                    944:                        setimes = 0;
1.40      markus    945:                        if (utimes(np, tv) < 0) {
1.1       deraadt   946:                                run_err("%s: set times: %s",
1.63      stevesk   947:                                    np, strerror(errno));
1.1       deraadt   948:                                wrerr = DISPLAYED;
                    949:                        }
                    950:                }
1.18      markus    951:                switch (wrerr) {
1.1       deraadt   952:                case YES:
                    953:                        run_err("%s: %s", np, strerror(wrerrno));
                    954:                        break;
                    955:                case NO:
1.29      deraadt   956:                        (void) atomicio(write, remout, "", 1);
1.1       deraadt   957:                        break;
                    958:                case DISPLAYED:
                    959:                        break;
                    960:                }
                    961:        }
                    962: screwup:
                    963:        run_err("protocol error: %s", why);
                    964:        exit(1);
                    965: }
                    966:
                    967: int
1.86      itojun    968: response(void)
1.1       deraadt   969: {
                    970:        char ch, *cp, resp, rbuf[2048];
                    971:
1.30      deraadt   972:        if (atomicio(read, remin, &resp, sizeof(resp)) != sizeof(resp))
1.1       deraadt   973:                lostconn(0);
                    974:
                    975:        cp = rbuf;
1.18      markus    976:        switch (resp) {
                    977:        case 0:         /* ok */
1.1       deraadt   978:                return (0);
                    979:        default:
                    980:                *cp++ = resp;
                    981:                /* FALLTHROUGH */
1.18      markus    982:        case 1:         /* error, followed by error msg */
                    983:        case 2:         /* fatal error, "" */
1.1       deraadt   984:                do {
1.30      deraadt   985:                        if (atomicio(read, remin, &ch, sizeof(ch)) != sizeof(ch))
1.1       deraadt   986:                                lostconn(0);
                    987:                        *cp++ = ch;
                    988:                } while (cp < &rbuf[sizeof(rbuf) - 1] && ch != '\n');
                    989:
                    990:                if (!iamremote)
1.29      deraadt   991:                        (void) atomicio(write, STDERR_FILENO, rbuf, cp - rbuf);
1.1       deraadt   992:                ++errs;
                    993:                if (resp == 1)
                    994:                        return (-1);
                    995:                exit(1);
                    996:        }
                    997:        /* NOTREACHED */
                    998: }
                    999:
                   1000: void
1.86      itojun   1001: usage(void)
1.1       deraadt  1002: {
1.83      stevesk  1003:        (void) fprintf(stderr,
1.100     markus   1004:            "usage: scp [-pqrvBC1246] [-F config] [-S program] [-P port]\n"
1.99      markus   1005:            "           [-c cipher] [-i identity] [-l limit] [-o option]\n"
1.90      deraadt  1006:            "           [[user@]host1:]file1 [...] [[user@]host2:]file2\n");
1.1       deraadt  1007:        exit(1);
                   1008: }
                   1009:
                   1010: void
1.18      markus   1011: run_err(const char *fmt,...)
1.1       deraadt  1012: {
                   1013:        static FILE *fp;
                   1014:        va_list ap;
                   1015:
                   1016:        ++errs;
                   1017:        if (fp == NULL && !(fp = fdopen(remout, "w")))
                   1018:                return;
1.18      markus   1019:        (void) fprintf(fp, "%c", 0x01);
                   1020:        (void) fprintf(fp, "scp: ");
1.73      markus   1021:        va_start(ap, fmt);
1.18      markus   1022:        (void) vfprintf(fp, fmt, ap);
1.73      markus   1023:        va_end(ap);
1.18      markus   1024:        (void) fprintf(fp, "\n");
                   1025:        (void) fflush(fp);
                   1026:
                   1027:        if (!iamremote) {
1.73      markus   1028:                va_start(ap, fmt);
1.18      markus   1029:                vfprintf(stderr, fmt, ap);
1.73      markus   1030:                va_end(ap);
1.18      markus   1031:                fprintf(stderr, "\n");
                   1032:        }
1.1       deraadt  1033: }
                   1034:
                   1035: void
                   1036: verifydir(cp)
                   1037:        char *cp;
                   1038: {
                   1039:        struct stat stb;
                   1040:
                   1041:        if (!stat(cp, &stb)) {
                   1042:                if (S_ISDIR(stb.st_mode))
                   1043:                        return;
                   1044:                errno = ENOTDIR;
                   1045:        }
                   1046:        run_err("%s: %s", cp, strerror(errno));
                   1047:        exit(1);
                   1048: }
                   1049:
                   1050: int
                   1051: okname(cp0)
                   1052:        char *cp0;
                   1053: {
                   1054:        int c;
                   1055:        char *cp;
                   1056:
                   1057:        cp = cp0;
                   1058:        do {
1.75      deraadt  1059:                c = (int)*cp;
1.1       deraadt  1060:                if (c & 0200)
                   1061:                        goto bad;
1.101     markus   1062:                if (!isalpha(c) && !isdigit(c)) {
                   1063:                        switch (c) {
                   1064:                        case '\'':
                   1065:                        case '"':
                   1066:                        case '`':
                   1067:                        case ' ':
                   1068:                        case '#':
                   1069:                                goto bad;
                   1070:                        default:
                   1071:                                break;
                   1072:                        }
                   1073:                }
1.1       deraadt  1074:        } while (*++cp);
                   1075:        return (1);
                   1076:
1.25      markus   1077: bad:   fprintf(stderr, "%s: invalid user name\n", cp0);
1.1       deraadt  1078:        return (0);
                   1079: }
                   1080:
                   1081: BUF *
                   1082: allocbuf(bp, fd, blksize)
                   1083:        BUF *bp;
                   1084:        int fd, blksize;
                   1085: {
                   1086:        size_t size;
                   1087:        struct stat stb;
                   1088:
                   1089:        if (fstat(fd, &stb) < 0) {
                   1090:                run_err("fstat: %s", strerror(errno));
                   1091:                return (0);
                   1092:        }
1.95      markus   1093:        size = roundup(stb.st_blksize, blksize);
                   1094:        if (size == 0)
1.18      markus   1095:                size = blksize;
1.1       deraadt  1096:        if (bp->cnt >= size)
                   1097:                return (bp);
1.18      markus   1098:        if (bp->buf == NULL)
                   1099:                bp->buf = xmalloc(size);
                   1100:        else
                   1101:                bp->buf = xrealloc(bp->buf, size);
1.81      markus   1102:        memset(bp->buf, 0, size);
1.1       deraadt  1103:        bp->cnt = size;
                   1104:        return (bp);
                   1105: }
                   1106:
                   1107: void
                   1108: lostconn(signo)
                   1109:        int signo;
                   1110: {
                   1111:        if (!iamremote)
1.74      markus   1112:                write(STDERR_FILENO, "lost connection\n", 16);
                   1113:        if (signo)
                   1114:                _exit(1);
                   1115:        else
                   1116:                exit(1);
1.4       aaron    1117: }