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

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.127   ! deraadt    74: RCSID("$OpenBSD: scp.c,v 1.126 2005/09/13 23:40:07 djm 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.127   ! deraadt   569: next:                  if (fd != -1) {
        !           570:                                (void) close(fd);
        !           571:                                fd = -1;
        !           572:                        }
1.1       deraadt   573:                        continue;
                    574:                }
1.97      fgsch     575:                if (showprogress)
                    576:                        start_progress_meter(curfile, stb.st_size, &statbytes);
1.1       deraadt   577:                /* Keep writing after an error so that we stay sync'd up. */
                    578:                for (haderr = i = 0; i < stb.st_size; i += bp->cnt) {
                    579:                        amt = bp->cnt;
                    580:                        if (i + amt > stb.st_size)
                    581:                                amt = stb.st_size - i;
                    582:                        if (!haderr) {
1.30      deraadt   583:                                result = atomicio(read, fd, bp->buf, amt);
1.1       deraadt   584:                                if (result != amt)
1.122     avsm      585:                                        haderr = errno;
1.1       deraadt   586:                        }
                    587:                        if (haderr)
1.107     deraadt   588:                                (void) atomicio(vwrite, remout, bp->buf, amt);
1.1       deraadt   589:                        else {
1.107     deraadt   590:                                result = atomicio(vwrite, remout, bp->buf, amt);
1.1       deraadt   591:                                if (result != amt)
1.122     avsm      592:                                        haderr = errno;
1.4       aaron     593:                                statbytes += result;
1.1       deraadt   594:                        }
1.113     djm       595:                        if (limit_rate)
1.99      markus    596:                                bwlimit(amt);
1.1       deraadt   597:                }
1.18      markus    598:                if (showprogress)
1.97      fgsch     599:                        stop_progress_meter();
1.4       aaron     600:
1.127   ! deraadt   601:                if (fd != -1) {
        !           602:                        if (close(fd) < 0 && !haderr)
        !           603:                                haderr = errno;
        !           604:                        fd = -1;
        !           605:                }
1.1       deraadt   606:                if (!haderr)
1.107     deraadt   607:                        (void) atomicio(vwrite, remout, "", 1);
1.1       deraadt   608:                else
                    609:                        run_err("%s: %s", name, strerror(haderr));
1.18      markus    610:                (void) response();
1.1       deraadt   611:        }
                    612: }
                    613:
                    614: void
1.104     djm       615: rsource(char *name, struct stat *statp)
1.1       deraadt   616: {
                    617:        DIR *dirp;
                    618:        struct dirent *dp;
                    619:        char *last, *vect[1], path[1100];
                    620:
                    621:        if (!(dirp = opendir(name))) {
                    622:                run_err("%s: %s", name, strerror(errno));
                    623:                return;
                    624:        }
                    625:        last = strrchr(name, '/');
                    626:        if (last == 0)
                    627:                last = name;
                    628:        else
                    629:                last++;
                    630:        if (pflag) {
1.55      deraadt   631:                (void) snprintf(path, sizeof(path), "T%lu 0 %lu 0\n",
1.47      markus    632:                    (u_long) statp->st_mtime,
                    633:                    (u_long) statp->st_atime);
1.107     deraadt   634:                (void) atomicio(vwrite, remout, path, strlen(path));
1.1       deraadt   635:                if (response() < 0) {
                    636:                        closedir(dirp);
                    637:                        return;
                    638:                }
                    639:        }
1.55      deraadt   640:        (void) snprintf(path, sizeof path, "D%04o %d %.1024s\n",
1.47      markus    641:            (u_int) (statp->st_mode & FILEMODEMASK), 0, last);
1.18      markus    642:        if (verbose_mode)
                    643:                fprintf(stderr, "Entering directory: %s", path);
1.107     deraadt   644:        (void) atomicio(vwrite, remout, path, strlen(path));
1.1       deraadt   645:        if (response() < 0) {
                    646:                closedir(dirp);
                    647:                return;
                    648:        }
1.63      stevesk   649:        while ((dp = readdir(dirp)) != NULL) {
1.1       deraadt   650:                if (dp->d_ino == 0)
                    651:                        continue;
                    652:                if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
                    653:                        continue;
                    654:                if (strlen(name) + 1 + strlen(dp->d_name) >= sizeof(path) - 1) {
                    655:                        run_err("%s/%s: name too long", name, dp->d_name);
                    656:                        continue;
                    657:                }
1.55      deraadt   658:                (void) snprintf(path, sizeof path, "%s/%s", name, dp->d_name);
1.1       deraadt   659:                vect[0] = path;
                    660:                source(1, vect);
                    661:        }
1.18      markus    662:        (void) closedir(dirp);
1.107     deraadt   663:        (void) atomicio(vwrite, remout, "E\n", 2);
1.18      markus    664:        (void) response();
1.1       deraadt   665: }
                    666:
                    667: void
1.99      markus    668: bwlimit(int amount)
                    669: {
                    670:        static struct timeval bwstart, bwend;
                    671:        static int lamt, thresh = 16384;
1.115     avsm      672:        u_int64_t waitlen;
1.99      markus    673:        struct timespec ts, rm;
                    674:
                    675:        if (!timerisset(&bwstart)) {
                    676:                gettimeofday(&bwstart, NULL);
                    677:                return;
                    678:        }
                    679:
                    680:        lamt += amount;
                    681:        if (lamt < thresh)
                    682:                return;
                    683:
                    684:        gettimeofday(&bwend, NULL);
                    685:        timersub(&bwend, &bwstart, &bwend);
                    686:        if (!timerisset(&bwend))
                    687:                return;
                    688:
                    689:        lamt *= 8;
1.115     avsm      690:        waitlen = (double)1000000L * lamt / limit_rate;
1.99      markus    691:
1.115     avsm      692:        bwstart.tv_sec = waitlen / 1000000L;
                    693:        bwstart.tv_usec = waitlen % 1000000L;
1.99      markus    694:
                    695:        if (timercmp(&bwstart, &bwend, >)) {
                    696:                timersub(&bwstart, &bwend, &bwend);
                    697:
                    698:                /* Adjust the wait time */
                    699:                if (bwend.tv_sec) {
                    700:                        thresh /= 2;
                    701:                        if (thresh < 2048)
                    702:                                thresh = 2048;
                    703:                } else if (bwend.tv_usec < 100) {
                    704:                        thresh *= 2;
                    705:                        if (thresh > 32768)
                    706:                                thresh = 32768;
                    707:                }
                    708:
                    709:                TIMEVAL_TO_TIMESPEC(&bwend, &ts);
                    710:                while (nanosleep(&ts, &rm) == -1) {
                    711:                        if (errno != EINTR)
                    712:                                break;
                    713:                        ts = rm;
                    714:                }
                    715:        }
                    716:
                    717:        lamt = 0;
                    718:        gettimeofday(&bwstart, NULL);
                    719: }
                    720:
                    721: void
1.104     djm       722: sink(int argc, char **argv)
1.1       deraadt   723: {
                    724:        static BUF buffer;
                    725:        struct stat stb;
1.18      markus    726:        enum {
                    727:                YES, NO, DISPLAYED
                    728:        } wrerr;
1.1       deraadt   729:        BUF *bp;
1.122     avsm      730:        off_t i;
1.124     djm       731:        size_t j, count;
                    732:        int amt, exists, first, mask, mode, ofd, omode;
1.97      fgsch     733:        off_t size, statbytes;
1.36      deraadt   734:        int setimes, targisdir, wrerrno = 0;
1.1       deraadt   735:        char ch, *cp, *np, *targ, *why, *vect[1], buf[2048];
1.40      markus    736:        struct timeval tv[2];
1.1       deraadt   737:
1.66      stevesk   738: #define        atime   tv[0]
                    739: #define        mtime   tv[1]
1.118     deraadt   740: #define        SCREWUP(str)    { why = str; goto screwup; }
1.1       deraadt   741:
                    742:        setimes = targisdir = 0;
                    743:        mask = umask(0);
                    744:        if (!pflag)
1.18      markus    745:                (void) umask(mask);
1.1       deraadt   746:        if (argc != 1) {
                    747:                run_err("ambiguous target");
                    748:                exit(1);
                    749:        }
                    750:        targ = *argv;
                    751:        if (targetshouldbedirectory)
                    752:                verifydir(targ);
1.18      markus    753:
1.107     deraadt   754:        (void) atomicio(vwrite, remout, "", 1);
1.1       deraadt   755:        if (stat(targ, &stb) == 0 && S_ISDIR(stb.st_mode))
                    756:                targisdir = 1;
                    757:        for (first = 1;; first = 0) {
                    758:                cp = buf;
1.122     avsm      759:                if (atomicio(read, remin, cp, 1) != 1)
1.1       deraadt   760:                        return;
                    761:                if (*cp++ == '\n')
                    762:                        SCREWUP("unexpected <newline>");
                    763:                do {
1.30      deraadt   764:                        if (atomicio(read, remin, &ch, sizeof(ch)) != sizeof(ch))
1.1       deraadt   765:                                SCREWUP("lost connection");
                    766:                        *cp++ = ch;
                    767:                } while (cp < &buf[sizeof(buf) - 1] && ch != '\n');
                    768:                *cp = 0;
1.114     markus    769:                if (verbose_mode)
                    770:                        fprintf(stderr, "Sink: %s", buf);
1.1       deraadt   771:
                    772:                if (buf[0] == '\01' || buf[0] == '\02') {
                    773:                        if (iamremote == 0)
1.107     deraadt   774:                                (void) atomicio(vwrite, STDERR_FILENO,
1.44      deraadt   775:                                    buf + 1, strlen(buf + 1));
1.1       deraadt   776:                        if (buf[0] == '\02')
                    777:                                exit(1);
                    778:                        ++errs;
                    779:                        continue;
                    780:                }
                    781:                if (buf[0] == 'E') {
1.107     deraadt   782:                        (void) atomicio(vwrite, remout, "", 1);
1.1       deraadt   783:                        return;
                    784:                }
                    785:                if (ch == '\n')
                    786:                        *--cp = 0;
                    787:
                    788:                cp = buf;
                    789:                if (*cp == 'T') {
                    790:                        setimes++;
                    791:                        cp++;
1.66      stevesk   792:                        mtime.tv_sec = strtol(cp, &cp, 10);
                    793:                        if (!cp || *cp++ != ' ')
1.1       deraadt   794:                                SCREWUP("mtime.sec not delimited");
1.66      stevesk   795:                        mtime.tv_usec = strtol(cp, &cp, 10);
                    796:                        if (!cp || *cp++ != ' ')
1.1       deraadt   797:                                SCREWUP("mtime.usec not delimited");
1.66      stevesk   798:                        atime.tv_sec = strtol(cp, &cp, 10);
                    799:                        if (!cp || *cp++ != ' ')
1.1       deraadt   800:                                SCREWUP("atime.sec not delimited");
1.66      stevesk   801:                        atime.tv_usec = strtol(cp, &cp, 10);
                    802:                        if (!cp || *cp++ != '\0')
1.1       deraadt   803:                                SCREWUP("atime.usec not delimited");
1.107     deraadt   804:                        (void) atomicio(vwrite, remout, "", 1);
1.1       deraadt   805:                        continue;
                    806:                }
                    807:                if (*cp != 'C' && *cp != 'D') {
                    808:                        /*
                    809:                         * Check for the case "rcp remote:foo\* local:bar".
                    810:                         * In this case, the line "No match." can be returned
                    811:                         * by the shell before the rcp command on the remote is
                    812:                         * executed so the ^Aerror_message convention isn't
                    813:                         * followed.
                    814:                         */
                    815:                        if (first) {
                    816:                                run_err("%s", cp);
                    817:                                exit(1);
                    818:                        }
                    819:                        SCREWUP("expected control record");
                    820:                }
                    821:                mode = 0;
                    822:                for (++cp; cp < buf + 5; cp++) {
                    823:                        if (*cp < '0' || *cp > '7')
                    824:                                SCREWUP("bad mode");
                    825:                        mode = (mode << 3) | (*cp - '0');
                    826:                }
                    827:                if (*cp++ != ' ')
                    828:                        SCREWUP("mode not delimited");
                    829:
1.63      stevesk   830:                for (size = 0; isdigit(*cp);)
1.1       deraadt   831:                        size = size * 10 + (*cp++ - '0');
                    832:                if (*cp++ != ' ')
                    833:                        SCREWUP("size not delimited");
1.114     markus    834:                if ((strchr(cp, '/') != NULL) || (strcmp(cp, "..") == 0)) {
                    835:                        run_err("error: unexpected filename: %s", cp);
                    836:                        exit(1);
                    837:                }
1.1       deraadt   838:                if (targisdir) {
                    839:                        static char *namebuf;
1.124     djm       840:                        static size_t cursize;
1.1       deraadt   841:                        size_t need;
                    842:
                    843:                        need = strlen(targ) + strlen(cp) + 250;
1.55      deraadt   844:                        if (need > cursize) {
1.58      danh      845:                                if (namebuf)
                    846:                                        xfree(namebuf);
1.18      markus    847:                                namebuf = xmalloc(need);
1.55      deraadt   848:                                cursize = need;
                    849:                        }
                    850:                        (void) snprintf(namebuf, need, "%s%s%s", targ,
1.88      mouring   851:                            strcmp(targ, "/") ? "/" : "", cp);
1.1       deraadt   852:                        np = namebuf;
                    853:                } else
                    854:                        np = targ;
1.12      aaron     855:                curfile = cp;
1.1       deraadt   856:                exists = stat(np, &stb) == 0;
                    857:                if (buf[0] == 'D') {
                    858:                        int mod_flag = pflag;
1.114     markus    859:                        if (!iamrecursive)
                    860:                                SCREWUP("received directory without -r");
1.1       deraadt   861:                        if (exists) {
                    862:                                if (!S_ISDIR(stb.st_mode)) {
                    863:                                        errno = ENOTDIR;
                    864:                                        goto bad;
                    865:                                }
                    866:                                if (pflag)
1.18      markus    867:                                        (void) chmod(np, mode);
1.1       deraadt   868:                        } else {
1.18      markus    869:                                /* Handle copying from a read-only
                    870:                                   directory */
1.1       deraadt   871:                                mod_flag = 1;
                    872:                                if (mkdir(np, mode | S_IRWXU) < 0)
                    873:                                        goto bad;
                    874:                        }
1.58      danh      875:                        vect[0] = xstrdup(np);
1.1       deraadt   876:                        sink(1, vect);
                    877:                        if (setimes) {
                    878:                                setimes = 0;
1.59      deraadt   879:                                if (utimes(vect[0], tv) < 0)
1.18      markus    880:                                        run_err("%s: set times: %s",
1.59      deraadt   881:                                            vect[0], strerror(errno));
1.1       deraadt   882:                        }
                    883:                        if (mod_flag)
1.59      deraadt   884:                                (void) chmod(vect[0], mode);
                    885:                        if (vect[0])
                    886:                                xfree(vect[0]);
1.1       deraadt   887:                        continue;
                    888:                }
                    889:                omode = mode;
                    890:                mode |= S_IWRITE;
1.71      markus    891:                if ((ofd = open(np, O_WRONLY|O_CREAT, mode)) < 0) {
1.1       deraadt   892: bad:                   run_err("%s: %s", np, strerror(errno));
                    893:                        continue;
                    894:                }
1.107     deraadt   895:                (void) atomicio(vwrite, remout, "", 1);
1.1       deraadt   896:                if ((bp = allocbuf(&buffer, ofd, 4096)) == NULL) {
1.18      markus    897:                        (void) close(ofd);
1.1       deraadt   898:                        continue;
                    899:                }
                    900:                cp = bp->buf;
                    901:                wrerr = NO;
1.7       deraadt   902:
1.12      aaron     903:                statbytes = 0;
1.97      fgsch     904:                if (showprogress)
                    905:                        start_progress_meter(curfile, size, &statbytes);
1.1       deraadt   906:                for (count = i = 0; i < size; i += 4096) {
                    907:                        amt = 4096;
                    908:                        if (i + amt > size)
                    909:                                amt = size - i;
                    910:                        count += amt;
                    911:                        do {
1.117     avsm      912:                                j = atomicio(read, remin, cp, amt);
1.122     avsm      913:                                if (j == 0) {
1.1       deraadt   914:                                        run_err("%s", j ? strerror(errno) :
1.63      stevesk   915:                                            "dropped connection");
1.1       deraadt   916:                                        exit(1);
                    917:                                }
                    918:                                amt -= j;
                    919:                                cp += j;
1.18      markus    920:                                statbytes += j;
1.1       deraadt   921:                        } while (amt > 0);
1.112     djm       922:
1.113     djm       923:                        if (limit_rate)
1.99      markus    924:                                bwlimit(4096);
                    925:
1.1       deraadt   926:                        if (count == bp->cnt) {
                    927:                                /* Keep reading so we stay sync'd up. */
                    928:                                if (wrerr == NO) {
1.122     avsm      929:                                        if (atomicio(vwrite, ofd, bp->buf,
                    930:                                            count) != count) {
1.1       deraadt   931:                                                wrerr = YES;
1.122     avsm      932:                                                wrerrno = errno;
1.1       deraadt   933:                                        }
                    934:                                }
                    935:                                count = 0;
                    936:                                cp = bp->buf;
                    937:                        }
                    938:                }
1.7       deraadt   939:                if (showprogress)
1.97      fgsch     940:                        stop_progress_meter();
1.1       deraadt   941:                if (count != 0 && wrerr == NO &&
1.122     avsm      942:                    atomicio(vwrite, ofd, bp->buf, count) != count) {
1.1       deraadt   943:                        wrerr = YES;
1.122     avsm      944:                        wrerrno = errno;
1.1       deraadt   945:                }
1.98      djm       946:                if (wrerr == NO && ftruncate(ofd, size) != 0) {
1.1       deraadt   947:                        run_err("%s: truncate: %s", np, strerror(errno));
                    948:                        wrerr = DISPLAYED;
                    949:                }
                    950:                if (pflag) {
                    951:                        if (exists || omode != mode)
1.116     dtucker   952:                                if (fchmod(ofd, omode)) {
1.1       deraadt   953:                                        run_err("%s: set mode: %s",
1.63      stevesk   954:                                            np, strerror(errno));
1.116     dtucker   955:                                        wrerr = DISPLAYED;
                    956:                                }
1.1       deraadt   957:                } else {
                    958:                        if (!exists && omode != mode)
1.116     dtucker   959:                                if (fchmod(ofd, omode & ~mask)) {
1.1       deraadt   960:                                        run_err("%s: set mode: %s",
1.63      stevesk   961:                                            np, strerror(errno));
1.116     dtucker   962:                                        wrerr = DISPLAYED;
                    963:                                }
1.1       deraadt   964:                }
1.33      provos    965:                if (close(ofd) == -1) {
                    966:                        wrerr = YES;
                    967:                        wrerrno = errno;
                    968:                }
1.18      markus    969:                (void) response();
1.1       deraadt   970:                if (setimes && wrerr == NO) {
                    971:                        setimes = 0;
1.40      markus    972:                        if (utimes(np, tv) < 0) {
1.1       deraadt   973:                                run_err("%s: set times: %s",
1.63      stevesk   974:                                    np, strerror(errno));
1.1       deraadt   975:                                wrerr = DISPLAYED;
                    976:                        }
                    977:                }
1.18      markus    978:                switch (wrerr) {
1.1       deraadt   979:                case YES:
                    980:                        run_err("%s: %s", np, strerror(wrerrno));
                    981:                        break;
                    982:                case NO:
1.107     deraadt   983:                        (void) atomicio(vwrite, remout, "", 1);
1.1       deraadt   984:                        break;
                    985:                case DISPLAYED:
                    986:                        break;
                    987:                }
                    988:        }
                    989: screwup:
                    990:        run_err("protocol error: %s", why);
                    991:        exit(1);
                    992: }
                    993:
                    994: int
1.86      itojun    995: response(void)
1.1       deraadt   996: {
                    997:        char ch, *cp, resp, rbuf[2048];
                    998:
1.30      deraadt   999:        if (atomicio(read, remin, &resp, sizeof(resp)) != sizeof(resp))
1.1       deraadt  1000:                lostconn(0);
                   1001:
                   1002:        cp = rbuf;
1.18      markus   1003:        switch (resp) {
                   1004:        case 0:         /* ok */
1.1       deraadt  1005:                return (0);
                   1006:        default:
                   1007:                *cp++ = resp;
                   1008:                /* FALLTHROUGH */
1.18      markus   1009:        case 1:         /* error, followed by error msg */
                   1010:        case 2:         /* fatal error, "" */
1.1       deraadt  1011:                do {
1.30      deraadt  1012:                        if (atomicio(read, remin, &ch, sizeof(ch)) != sizeof(ch))
1.1       deraadt  1013:                                lostconn(0);
                   1014:                        *cp++ = ch;
                   1015:                } while (cp < &rbuf[sizeof(rbuf) - 1] && ch != '\n');
                   1016:
                   1017:                if (!iamremote)
1.107     deraadt  1018:                        (void) atomicio(vwrite, STDERR_FILENO, rbuf, cp - rbuf);
1.1       deraadt  1019:                ++errs;
                   1020:                if (resp == 1)
                   1021:                        return (-1);
                   1022:                exit(1);
                   1023:        }
                   1024:        /* NOTREACHED */
                   1025: }
                   1026:
                   1027: void
1.86      itojun   1028: usage(void)
1.1       deraadt  1029: {
1.83      stevesk  1030:        (void) fprintf(stderr,
1.110     jmc      1031:            "usage: scp [-1246BCpqrv] [-c cipher] [-F ssh_config] [-i identity_file]\n"
                   1032:            "           [-l limit] [-o ssh_option] [-P port] [-S program]\n"
1.90      deraadt  1033:            "           [[user@]host1:]file1 [...] [[user@]host2:]file2\n");
1.1       deraadt  1034:        exit(1);
                   1035: }
                   1036:
                   1037: void
1.18      markus   1038: run_err(const char *fmt,...)
1.1       deraadt  1039: {
                   1040:        static FILE *fp;
                   1041:        va_list ap;
                   1042:
                   1043:        ++errs;
                   1044:        if (fp == NULL && !(fp = fdopen(remout, "w")))
                   1045:                return;
1.18      markus   1046:        (void) fprintf(fp, "%c", 0x01);
                   1047:        (void) fprintf(fp, "scp: ");
1.73      markus   1048:        va_start(ap, fmt);
1.18      markus   1049:        (void) vfprintf(fp, fmt, ap);
1.73      markus   1050:        va_end(ap);
1.18      markus   1051:        (void) fprintf(fp, "\n");
                   1052:        (void) fflush(fp);
                   1053:
                   1054:        if (!iamremote) {
1.73      markus   1055:                va_start(ap, fmt);
1.18      markus   1056:                vfprintf(stderr, fmt, ap);
1.73      markus   1057:                va_end(ap);
1.18      markus   1058:                fprintf(stderr, "\n");
                   1059:        }
1.1       deraadt  1060: }
                   1061:
                   1062: void
1.104     djm      1063: verifydir(char *cp)
1.1       deraadt  1064: {
                   1065:        struct stat stb;
                   1066:
                   1067:        if (!stat(cp, &stb)) {
                   1068:                if (S_ISDIR(stb.st_mode))
                   1069:                        return;
                   1070:                errno = ENOTDIR;
                   1071:        }
                   1072:        run_err("%s: %s", cp, strerror(errno));
1.123     avsm     1073:        killchild(0);
1.1       deraadt  1074: }
                   1075:
                   1076: int
1.104     djm      1077: okname(char *cp0)
1.1       deraadt  1078: {
                   1079:        int c;
                   1080:        char *cp;
                   1081:
                   1082:        cp = cp0;
                   1083:        do {
1.75      deraadt  1084:                c = (int)*cp;
1.1       deraadt  1085:                if (c & 0200)
                   1086:                        goto bad;
1.101     markus   1087:                if (!isalpha(c) && !isdigit(c)) {
                   1088:                        switch (c) {
                   1089:                        case '\'':
                   1090:                        case '"':
                   1091:                        case '`':
                   1092:                        case ' ':
                   1093:                        case '#':
                   1094:                                goto bad;
                   1095:                        default:
                   1096:                                break;
                   1097:                        }
                   1098:                }
1.1       deraadt  1099:        } while (*++cp);
                   1100:        return (1);
                   1101:
1.25      markus   1102: bad:   fprintf(stderr, "%s: invalid user name\n", cp0);
1.1       deraadt  1103:        return (0);
                   1104: }
                   1105:
                   1106: BUF *
1.104     djm      1107: allocbuf(BUF *bp, int fd, int blksize)
1.1       deraadt  1108: {
                   1109:        size_t size;
                   1110:        struct stat stb;
                   1111:
                   1112:        if (fstat(fd, &stb) < 0) {
                   1113:                run_err("fstat: %s", strerror(errno));
                   1114:                return (0);
                   1115:        }
1.95      markus   1116:        size = roundup(stb.st_blksize, blksize);
                   1117:        if (size == 0)
1.18      markus   1118:                size = blksize;
1.1       deraadt  1119:        if (bp->cnt >= size)
                   1120:                return (bp);
1.18      markus   1121:        if (bp->buf == NULL)
                   1122:                bp->buf = xmalloc(size);
                   1123:        else
                   1124:                bp->buf = xrealloc(bp->buf, size);
1.81      markus   1125:        memset(bp->buf, 0, size);
1.1       deraadt  1126:        bp->cnt = size;
                   1127:        return (bp);
                   1128: }
                   1129:
                   1130: void
1.104     djm      1131: lostconn(int signo)
1.1       deraadt  1132: {
                   1133:        if (!iamremote)
1.74      markus   1134:                write(STDERR_FILENO, "lost connection\n", 16);
                   1135:        if (signo)
                   1136:                _exit(1);
                   1137:        else
                   1138:                exit(1);
1.4       aaron    1139: }