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

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