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

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