[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.91.2.1

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