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

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