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

1.193   ! millert     1: /* $OpenBSD: scp.c,v 1.192 2017/05/31 09:15:42 deraadt 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:
1.132     stevesk    74: #include <sys/types.h>
1.161     djm        75: #include <sys/poll.h>
1.132     stevesk    76: #include <sys/wait.h>
1.134     stevesk    77: #include <sys/stat.h>
1.151     stevesk    78: #include <sys/time.h>
1.155     deraadt    79: #include <sys/uio.h>
1.131     stevesk    80:
1.135     stevesk    81: #include <ctype.h>
1.131     stevesk    82: #include <dirent.h>
1.147     stevesk    83: #include <errno.h>
1.144     stevesk    84: #include <fcntl.h>
1.186     schwarze   85: #include <locale.h>
1.143     stevesk    86: #include <pwd.h>
1.133     stevesk    87: #include <signal.h>
1.146     stevesk    88: #include <stdarg.h>
1.189     millert    89: #include <stdint.h>
1.154     stevesk    90: #include <stdio.h>
1.153     stevesk    91: #include <stdlib.h>
1.150     stevesk    92: #include <string.h>
1.149     stevesk    93: #include <time.h>
1.148     stevesk    94: #include <unistd.h>
1.181     deraadt    95: #include <limits.h>
1.158     dtucker    96: #include <vis.h>
1.1       deraadt    97:
                     98: #include "xmalloc.h"
1.193   ! millert    99: #include "ssh.h"
1.51      markus    100: #include "atomicio.h"
1.50      markus    101: #include "pathnames.h"
1.51      markus    102: #include "log.h"
1.69      mouring   103: #include "misc.h"
1.97      fgsch     104: #include "progressmeter.h"
1.186     schwarze  105: #include "utf8.h"
1.1       deraadt   106:
1.161     djm       107: #define COPY_BUFLEN    16384
                    108:
1.193   ! millert   109: int do_cmd(char *host, char *remuser, int port, char *cmd, int *fdin, int *fdout);
        !           110: int do_cmd2(char *host, char *remuser, int port, char *cmd, int fdin, int fdout);
1.11      aaron     111:
1.70      mouring   112: /* Struct for addargs */
                    113: arglist args;
1.168     djm       114: arglist remote_remote_args;
1.41      markus    115:
1.99      markus    116: /* Bandwidth limit */
1.167     djm       117: long long limit_kbps = 0;
                    118: struct bwlimit bwlimit;
1.99      markus    119:
1.11      aaron     120: /* Name of current file being transferred. */
                    121: char *curfile;
1.4       aaron     122:
1.1       deraadt   123: /* This is set to non-zero to enable verbose mode. */
1.17      markus    124: int verbose_mode = 0;
1.1       deraadt   125:
1.6       aaron     126: /* This is set to zero if the progressmeter is not desired. */
                    127: int showprogress = 1;
                    128:
1.169     markus    129: /*
                    130:  * This is set to non-zero if remote-remote copy should be piped
                    131:  * through this process.
                    132:  */
                    133: int throughlocal = 0;
                    134:
1.193   ! millert   135: /* Non-standard port to use for the ssh connection or -1. */
        !           136: int sshport = -1;
        !           137:
1.34      deraadt   138: /* This is the program to execute for the secured connection. ("ssh" or -S) */
1.50      markus    139: char *ssh_program = _PATH_SSH_PROGRAM;
1.34      deraadt   140:
1.92      markus    141: /* This is used to store the pid of ssh_program */
1.105     djm       142: pid_t do_cmd_pid = -1;
                    143:
                    144: static void
                    145: killchild(int signo)
                    146: {
1.119     dtucker   147:        if (do_cmd_pid > 1) {
1.123     avsm      148:                kill(do_cmd_pid, signo ? signo : SIGTERM);
1.119     dtucker   149:                waitpid(do_cmd_pid, NULL, 0);
                    150:        }
1.105     djm       151:
1.123     avsm      152:        if (signo)
                    153:                _exit(1);
                    154:        exit(1);
1.105     djm       155: }
1.92      markus    156:
1.166     millert   157: static void
                    158: suspchild(int signo)
                    159: {
                    160:        int status;
                    161:
                    162:        if (do_cmd_pid > 1) {
                    163:                kill(do_cmd_pid, signo);
                    164:                while (waitpid(do_cmd_pid, &status, WUNTRACED) == -1 &&
                    165:                    errno == EINTR)
                    166:                        ;
                    167:                kill(getpid(), SIGSTOP);
                    168:        }
                    169: }
                    170:
1.129     djm       171: static int
                    172: do_local_cmd(arglist *a)
                    173: {
                    174:        u_int i;
                    175:        int status;
                    176:        pid_t pid;
                    177:
                    178:        if (a->num == 0)
                    179:                fatal("do_local_cmd: no arguments");
                    180:
                    181:        if (verbose_mode) {
                    182:                fprintf(stderr, "Executing:");
                    183:                for (i = 0; i < a->num; i++)
1.186     schwarze  184:                        fmprintf(stderr, " %s", a->list[i]);
1.129     djm       185:                fprintf(stderr, "\n");
                    186:        }
                    187:        if ((pid = fork()) == -1)
                    188:                fatal("do_local_cmd: fork: %s", strerror(errno));
                    189:
                    190:        if (pid == 0) {
                    191:                execvp(a->list[0], a->list);
                    192:                perror(a->list[0]);
                    193:                exit(1);
                    194:        }
                    195:
                    196:        do_cmd_pid = pid;
                    197:        signal(SIGTERM, killchild);
                    198:        signal(SIGINT, killchild);
                    199:        signal(SIGHUP, killchild);
                    200:
                    201:        while (waitpid(pid, &status, 0) == -1)
                    202:                if (errno != EINTR)
                    203:                        fatal("do_local_cmd: waitpid: %s", strerror(errno));
                    204:
                    205:        do_cmd_pid = -1;
                    206:
                    207:        if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
                    208:                return (-1);
                    209:
                    210:        return (0);
                    211: }
                    212:
1.20      markus    213: /*
                    214:  * This function executes the given command as the specified user on the
                    215:  * given host.  This returns < 0 if execution fails, and >= 0 otherwise. This
                    216:  * assigns the input and output file descriptors on success.
                    217:  */
1.1       deraadt   218:
1.27      markus    219: int
1.193   ! millert   220: do_cmd(char *host, char *remuser, int port, char *cmd, int *fdin, int *fdout)
1.1       deraadt   221: {
1.18      markus    222:        int pin[2], pout[2], reserved[2];
                    223:
                    224:        if (verbose_mode)
1.186     schwarze  225:                fmprintf(stderr,
1.78      deraadt   226:                    "Executing: program %s host %s, user %s, command %s\n",
                    227:                    ssh_program, host,
                    228:                    remuser ? remuser : "(unspecified)", cmd);
1.18      markus    229:
1.193   ! millert   230:        if (port == -1)
        !           231:                port = sshport;
        !           232:
1.20      markus    233:        /*
                    234:         * Reserve two descriptors so that the real pipes won't get
                    235:         * descriptors 0 and 1 because that will screw up dup2 below.
                    236:         */
1.142     markus    237:        if (pipe(reserved) < 0)
                    238:                fatal("pipe: %s", strerror(errno));
1.18      markus    239:
                    240:        /* Create a socket pair for communicating with ssh. */
                    241:        if (pipe(pin) < 0)
                    242:                fatal("pipe: %s", strerror(errno));
                    243:        if (pipe(pout) < 0)
                    244:                fatal("pipe: %s", strerror(errno));
                    245:
                    246:        /* Free the reserved descriptors. */
                    247:        close(reserved[0]);
                    248:        close(reserved[1]);
1.166     millert   249:
                    250:        signal(SIGTSTP, suspchild);
                    251:        signal(SIGTTIN, suspchild);
                    252:        signal(SIGTTOU, suspchild);
1.18      markus    253:
1.106     nino      254:        /* Fork a child to execute the command on the remote host using ssh. */
1.92      markus    255:        do_cmd_pid = fork();
                    256:        if (do_cmd_pid == 0) {
1.18      markus    257:                /* Child. */
                    258:                close(pin[1]);
                    259:                close(pout[0]);
                    260:                dup2(pin[0], 0);
                    261:                dup2(pout[1], 1);
                    262:                close(pin[0]);
                    263:                close(pout[1]);
                    264:
1.129     djm       265:                replacearg(&args, 0, "%s", ssh_program);
1.193   ! millert   266:                if (port != -1) {
        !           267:                        addargs(&args, "-p");
        !           268:                        addargs(&args, "%d", port);
        !           269:                }
1.165     guenther  270:                if (remuser != NULL) {
                    271:                        addargs(&args, "-l");
                    272:                        addargs(&args, "%s", remuser);
                    273:                }
                    274:                addargs(&args, "--");
1.70      mouring   275:                addargs(&args, "%s", host);
                    276:                addargs(&args, "%s", cmd);
1.18      markus    277:
1.41      markus    278:                execvp(ssh_program, args.list);
1.34      deraadt   279:                perror(ssh_program);
1.18      markus    280:                exit(1);
1.92      markus    281:        } else if (do_cmd_pid == -1) {
                    282:                fatal("fork: %s", strerror(errno));
1.18      markus    283:        }
                    284:        /* Parent.  Close the other side, and return the local side. */
                    285:        close(pin[0]);
                    286:        *fdout = pin[1];
                    287:        close(pout[1]);
                    288:        *fdin = pout[0];
1.105     djm       289:        signal(SIGTERM, killchild);
                    290:        signal(SIGINT, killchild);
                    291:        signal(SIGHUP, killchild);
1.18      markus    292:        return 0;
1.1       deraadt   293: }
                    294:
1.169     markus    295: /*
                    296:  * This functions executes a command simlar to do_cmd(), but expects the
                    297:  * input and output descriptors to be setup by a previous call to do_cmd().
                    298:  * This way the input and output of two commands can be connected.
                    299:  */
                    300: int
1.193   ! millert   301: do_cmd2(char *host, char *remuser, int port, char *cmd, int fdin, int fdout)
1.169     markus    302: {
                    303:        pid_t pid;
                    304:        int status;
                    305:
                    306:        if (verbose_mode)
1.186     schwarze  307:                fmprintf(stderr,
1.169     markus    308:                    "Executing: 2nd program %s host %s, user %s, command %s\n",
                    309:                    ssh_program, host,
                    310:                    remuser ? remuser : "(unspecified)", cmd);
                    311:
1.193   ! millert   312:        if (port == -1)
        !           313:                port = sshport;
        !           314:
1.169     markus    315:        /* Fork a child to execute the command on the remote host using ssh. */
                    316:        pid = fork();
                    317:        if (pid == 0) {
                    318:                dup2(fdin, 0);
                    319:                dup2(fdout, 1);
                    320:
                    321:                replacearg(&args, 0, "%s", ssh_program);
1.193   ! millert   322:                if (port != -1) {
        !           323:                        addargs(&args, "-p");
        !           324:                        addargs(&args, "%d", port);
        !           325:                }
1.169     markus    326:                if (remuser != NULL) {
                    327:                        addargs(&args, "-l");
                    328:                        addargs(&args, "%s", remuser);
                    329:                }
                    330:                addargs(&args, "--");
                    331:                addargs(&args, "%s", host);
                    332:                addargs(&args, "%s", cmd);
                    333:
                    334:                execvp(ssh_program, args.list);
                    335:                perror(ssh_program);
                    336:                exit(1);
                    337:        } else if (pid == -1) {
                    338:                fatal("fork: %s", strerror(errno));
                    339:        }
                    340:        while (waitpid(pid, &status, 0) == -1)
                    341:                if (errno != EINTR)
                    342:                        fatal("do_cmd2: waitpid: %s", strerror(errno));
                    343:        return 0;
                    344: }
                    345:
1.1       deraadt   346: typedef struct {
1.124     djm       347:        size_t cnt;
1.1       deraadt   348:        char *buf;
                    349: } BUF;
                    350:
1.18      markus    351: BUF *allocbuf(BUF *, int, int);
                    352: void lostconn(int);
                    353: int okname(char *);
                    354: void run_err(const char *,...);
                    355: void verifydir(char *);
1.1       deraadt   356:
                    357: struct passwd *pwd;
1.18      markus    358: uid_t userid;
1.1       deraadt   359: int errs, remin, remout;
                    360: int pflag, iamremote, iamrecursive, targetshouldbedirectory;
                    361:
                    362: #define        CMDNEEDS        64
                    363: char cmd[CMDNEEDS];            /* must hold "rcp -r -p -d\0" */
                    364:
1.18      markus    365: int response(void);
                    366: void rsource(char *, struct stat *);
                    367: void sink(int, char *[]);
                    368: void source(int, char *[]);
                    369: void tolocal(int, char *[]);
1.193   ! millert   370: void toremote(int, char *[]);
1.18      markus    371: void usage(void);
1.1       deraadt   372:
                    373: int
1.104     djm       374: main(int argc, char **argv)
1.1       deraadt   375: {
1.145     djm       376:        int ch, fflag, tflag, status, n;
1.193   ! millert   377:        char **newargv;
1.167     djm       378:        const char *errstr;
1.1       deraadt   379:        extern char *optarg;
                    380:        extern int optind;
1.126     djm       381:
                    382:        /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
                    383:        sanitise_stdfd();
1.145     djm       384:
1.186     schwarze  385:        setlocale(LC_CTYPE, "");
                    386:
1.145     djm       387:        /* Copy argv, because we modify it */
1.187     deraadt   388:        newargv = xcalloc(MAXIMUM(argc + 1, 1), sizeof(*newargv));
1.145     djm       389:        for (n = 0; n < argc; n++)
                    390:                newargv[n] = xstrdup(argv[n]);
                    391:        argv = newargv;
1.1       deraadt   392:
1.129     djm       393:        memset(&args, '\0', sizeof(args));
1.168     djm       394:        memset(&remote_remote_args, '\0', sizeof(remote_remote_args));
                    395:        args.list = remote_remote_args.list = NULL;
1.129     djm       396:        addargs(&args, "%s", ssh_program);
1.70      mouring   397:        addargs(&args, "-x");
1.168     djm       398:        addargs(&args, "-oForwardAgent=no");
                    399:        addargs(&args, "-oPermitLocalCommand=no");
                    400:        addargs(&args, "-oClearAllForwardings=yes");
1.41      markus    401:
1.1       deraadt   402:        fflag = tflag = 0;
1.169     markus    403:        while ((ch = getopt(argc, argv, "dfl:prtvBCc:i:P:q12346S:o:F:")) != -1)
1.18      markus    404:                switch (ch) {
                    405:                /* User-visible flags. */
1.100     markus    406:                case '1':
1.190     djm       407:                        fatal("SSH protocol v.1 is no longer supported");
                    408:                        break;
1.100     markus    409:                case '2':
1.190     djm       410:                        /* Ignored */
                    411:                        break;
1.23      markus    412:                case '4':
1.41      markus    413:                case '6':
                    414:                case 'C':
1.70      mouring   415:                        addargs(&args, "-%c", ch);
1.168     djm       416:                        addargs(&remote_remote_args, "-%c", ch);
1.41      markus    417:                        break;
1.169     markus    418:                case '3':
                    419:                        throughlocal = 1;
                    420:                        break;
1.41      markus    421:                case 'o':
                    422:                case 'c':
                    423:                case 'i':
1.83      stevesk   424:                case 'F':
1.168     djm       425:                        addargs(&remote_remote_args, "-%c", ch);
                    426:                        addargs(&remote_remote_args, "%s", optarg);
1.165     guenther  427:                        addargs(&args, "-%c", ch);
                    428:                        addargs(&args, "%s", optarg);
1.23      markus    429:                        break;
1.41      markus    430:                case 'P':
1.193   ! millert   431:                        sshport = a2port(optarg);
        !           432:                        if (sshport <= 0)
        !           433:                                fatal("bad port \"%s\"\n", optarg);
1.41      markus    434:                        break;
                    435:                case 'B':
1.168     djm       436:                        addargs(&remote_remote_args, "-oBatchmode=yes");
                    437:                        addargs(&args, "-oBatchmode=yes");
1.23      markus    438:                        break;
1.99      markus    439:                case 'l':
1.167     djm       440:                        limit_kbps = strtonum(optarg, 1, 100 * 1024 * 1024,
                    441:                            &errstr);
                    442:                        if (errstr != NULL)
1.99      markus    443:                                usage();
1.167     djm       444:                        limit_kbps *= 1024; /* kbps */
                    445:                        bandwidth_limit_init(&bwlimit, limit_kbps, COPY_BUFLEN);
1.99      markus    446:                        break;
1.1       deraadt   447:                case 'p':
                    448:                        pflag = 1;
                    449:                        break;
                    450:                case 'r':
                    451:                        iamrecursive = 1;
                    452:                        break;
1.34      deraadt   453:                case 'S':
1.41      markus    454:                        ssh_program = xstrdup(optarg);
                    455:                        break;
                    456:                case 'v':
1.72      markus    457:                        addargs(&args, "-v");
1.168     djm       458:                        addargs(&remote_remote_args, "-v");
1.41      markus    459:                        verbose_mode = 1;
                    460:                        break;
                    461:                case 'q':
1.111     dtucker   462:                        addargs(&args, "-q");
1.168     djm       463:                        addargs(&remote_remote_args, "-q");
1.41      markus    464:                        showprogress = 0;
1.34      deraadt   465:                        break;
                    466:
1.18      markus    467:                /* Server options. */
1.1       deraadt   468:                case 'd':
                    469:                        targetshouldbedirectory = 1;
                    470:                        break;
1.18      markus    471:                case 'f':       /* "from" */
1.1       deraadt   472:                        iamremote = 1;
                    473:                        fflag = 1;
                    474:                        break;
1.18      markus    475:                case 't':       /* "to" */
1.1       deraadt   476:                        iamremote = 1;
                    477:                        tflag = 1;
                    478:                        break;
                    479:                default:
                    480:                        usage();
                    481:                }
                    482:        argc -= optind;
                    483:        argv += optind;
                    484:
                    485:        if ((pwd = getpwuid(userid = getuid())) == NULL)
1.108     deraadt   486:                fatal("unknown user %u", (u_int) userid);
1.1       deraadt   487:
1.156     djm       488:        if (!isatty(STDOUT_FILENO))
1.7       deraadt   489:                showprogress = 0;
1.184     deraadt   490:
                    491:        if (pflag) {
                    492:                /* Cannot pledge: -p allows setuid/setgid files... */
                    493:        } else {
                    494:                if (pledge("stdio rpath wpath cpath fattr tty proc exec",
                    495:                    NULL) == -1) {
                    496:                        perror("pledge");
                    497:                        exit(1);
                    498:                }
                    499:        }
1.7       deraadt   500:
1.1       deraadt   501:        remin = STDIN_FILENO;
                    502:        remout = STDOUT_FILENO;
                    503:
1.52      stevesk   504:        if (fflag) {
1.18      markus    505:                /* Follow "protocol", send data. */
                    506:                (void) response();
1.1       deraadt   507:                source(argc, argv);
                    508:                exit(errs != 0);
                    509:        }
1.18      markus    510:        if (tflag) {
                    511:                /* Receive data. */
1.1       deraadt   512:                sink(argc, argv);
                    513:                exit(errs != 0);
                    514:        }
                    515:        if (argc < 2)
                    516:                usage();
                    517:        if (argc > 2)
                    518:                targetshouldbedirectory = 1;
                    519:
                    520:        remin = remout = -1;
1.92      markus    521:        do_cmd_pid = -1;
1.1       deraadt   522:        /* Command to be executed on remote system using "ssh". */
1.55      deraadt   523:        (void) snprintf(cmd, sizeof cmd, "scp%s%s%s%s",
                    524:            verbose_mode ? " -v" : "",
1.35      deraadt   525:            iamrecursive ? " -r" : "", pflag ? " -p" : "",
                    526:            targetshouldbedirectory ? " -d" : "");
1.1       deraadt   527:
1.18      markus    528:        (void) signal(SIGPIPE, lostconn);
1.1       deraadt   529:
1.193   ! millert   530:        if (colon(argv[argc - 1]))      /* Dest is remote host. */
        !           531:                toremote(argc, argv);
1.1       deraadt   532:        else {
                    533:                if (targetshouldbedirectory)
                    534:                        verifydir(argv[argc - 1]);
1.130     djm       535:                tolocal(argc, argv);    /* Dest is local host. */
1.92      markus    536:        }
                    537:        /*
                    538:         * Finally check the exit status of the ssh process, if one was forked
1.164     stevesk   539:         * and no error has occurred yet
1.92      markus    540:         */
                    541:        if (do_cmd_pid != -1 && errs == 0) {
                    542:                if (remin != -1)
                    543:                    (void) close(remin);
                    544:                if (remout != -1)
                    545:                    (void) close(remout);
                    546:                if (waitpid(do_cmd_pid, &status, 0) == -1)
                    547:                        errs = 1;
                    548:                else {
                    549:                        if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
                    550:                                errs = 1;
                    551:                }
1.1       deraadt   552:        }
                    553:        exit(errs != 0);
                    554: }
                    555:
1.167     djm       556: /* Callback from atomicio6 to update progress meter and limit bandwidth */
                    557: static int
                    558: scpio(void *_cnt, size_t s)
1.161     djm       559: {
1.167     djm       560:        off_t *cnt = (off_t *)_cnt;
                    561:
                    562:        *cnt += s;
                    563:        if (limit_kbps > 0)
                    564:                bandwidth_limit(&bwlimit, s);
                    565:        return 0;
1.161     djm       566: }
                    567:
1.176     guenther  568: static int
                    569: do_times(int fd, int verb, const struct stat *sb)
                    570: {
                    571:        /* strlen(2^64) == 20; strlen(10^6) == 7 */
                    572:        char buf[(20 + 7 + 2) * 2 + 2];
                    573:
                    574:        (void)snprintf(buf, sizeof(buf), "T%llu 0 %llu 0\n",
                    575:            (unsigned long long) (sb->st_mtime < 0 ? 0 : sb->st_mtime),
                    576:            (unsigned long long) (sb->st_atime < 0 ? 0 : sb->st_atime));
                    577:        if (verb) {
                    578:                fprintf(stderr, "File mtime %lld atime %lld\n",
                    579:                    (long long)sb->st_mtime, (long long)sb->st_atime);
                    580:                fprintf(stderr, "Sending file timestamps: %s", buf);
                    581:        }
                    582:        (void) atomicio(vwrite, fd, buf, strlen(buf));
                    583:        return (response());
                    584: }
                    585:
1.1       deraadt   586: void
1.193   ! millert   587: toremote(int argc, char **argv)
1.1       deraadt   588: {
1.193   ! millert   589:        char *suser = NULL, *host = NULL, *src = NULL;
        !           590:        char *bp, *tuser, *thost, *targ;
        !           591:        int sport = -1, tport = -1;
1.129     djm       592:        arglist alist;
1.193   ! millert   593:        int i, r;
1.168     djm       594:        u_int j;
1.129     djm       595:
                    596:        memset(&alist, '\0', sizeof(alist));
                    597:        alist.list = NULL;
1.1       deraadt   598:
1.193   ! millert   599:        /* Parse target */
        !           600:        r = parse_uri("scp", argv[argc - 1], &tuser, &thost, &tport, &targ);
        !           601:        if (r == -1)
        !           602:                goto out;       /* invalid URI */
        !           603:        if (r != 0) {
        !           604:                if (parse_user_host_path(argv[argc - 1], &tuser, &thost,
        !           605:                    &targ) == -1)
        !           606:                        goto out;
1.129     djm       607:        }
1.193   ! millert   608:        if (tuser != NULL && !okname(tuser))
        !           609:                goto out;
1.129     djm       610:
1.193   ! millert   611:        /* Parse source files */
1.1       deraadt   612:        for (i = 0; i < argc - 1; i++) {
1.193   ! millert   613:                free(suser);
        !           614:                free(host);
        !           615:                free(src);
        !           616:                r = parse_uri("scp", argv[i], &suser, &host, &sport, &src);
        !           617:                if (r == -1)
        !           618:                        continue;       /* invalid URI */
        !           619:                if (r != 0)
        !           620:                        parse_user_host_path(argv[i], &suser, &host, &src);
        !           621:                if (suser != NULL && !okname(suser)) {
        !           622:                        ++errs;
        !           623:                        continue;
        !           624:                }
        !           625:                if (host && throughlocal) {     /* extended remote to remote */
1.171     djm       626:                        xasprintf(&bp, "%s -f %s%s", cmd,
                    627:                            *src == '-' ? "-- " : "", src);
1.193   ! millert   628:                        if (do_cmd(host, suser, sport, bp, &remin, &remout) < 0)
1.169     markus    629:                                exit(1);
1.173     djm       630:                        free(bp);
1.171     djm       631:                        xasprintf(&bp, "%s -t %s%s", cmd,
                    632:                            *targ == '-' ? "-- " : "", targ);
1.193   ! millert   633:                        if (do_cmd2(thost, tuser, tport, bp, remin, remout) < 0)
1.169     markus    634:                                exit(1);
1.173     djm       635:                        free(bp);
1.169     markus    636:                        (void) close(remin);
                    637:                        (void) close(remout);
                    638:                        remin = remout = -1;
1.193   ! millert   639:                } else if (host) {      /* standard remote to remote */
        !           640:                        if (tport != -1 && tport != SSH_DEFAULT_PORT) {
        !           641:                                /* This would require the remote support URIs */
        !           642:                                fatal("target port not supported with two "
        !           643:                                    "remote hosts without the -3 option");
        !           644:                        }
        !           645:
1.129     djm       646:                        freeargs(&alist);
                    647:                        addargs(&alist, "%s", ssh_program);
                    648:                        addargs(&alist, "-x");
1.168     djm       649:                        addargs(&alist, "-oClearAllForwardings=yes");
1.129     djm       650:                        addargs(&alist, "-n");
1.168     djm       651:                        for (j = 0; j < remote_remote_args.num; j++) {
                    652:                                addargs(&alist, "%s",
                    653:                                    remote_remote_args.list[j]);
                    654:                        }
1.193   ! millert   655:
        !           656:                        if (sport != -1) {
        !           657:                                addargs(&alist, "-p");
        !           658:                                addargs(&alist, "%d", sport);
        !           659:                        }
        !           660:                        if (suser) {
1.129     djm       661:                                addargs(&alist, "-l");
                    662:                                addargs(&alist, "%s", suser);
1.23      markus    663:                        }
1.165     guenther  664:                        addargs(&alist, "--");
1.129     djm       665:                        addargs(&alist, "%s", host);
                    666:                        addargs(&alist, "%s", cmd);
                    667:                        addargs(&alist, "%s", src);
                    668:                        addargs(&alist, "%s%s%s:%s",
                    669:                            tuser ? tuser : "", tuser ? "@" : "",
                    670:                            thost, targ);
                    671:                        if (do_local_cmd(&alist) != 0)
1.109     markus    672:                                errs = 1;
1.18      markus    673:                } else {        /* local to remote */
1.1       deraadt   674:                        if (remin == -1) {
1.171     djm       675:                                xasprintf(&bp, "%s -t %s%s", cmd,
                    676:                                    *targ == '-' ? "-- " : "", targ);
1.193   ! millert   677:                                if (do_cmd(thost, tuser, tport, bp, &remin,
1.140     deraadt   678:                                    &remout) < 0)
1.18      markus    679:                                        exit(1);
1.1       deraadt   680:                                if (response() < 0)
                    681:                                        exit(1);
1.173     djm       682:                                free(bp);
1.1       deraadt   683:                        }
1.18      markus    684:                        source(1, argv + i);
1.1       deraadt   685:                }
                    686:        }
1.193   ! millert   687: out:
        !           688:        free(tuser);
        !           689:        free(thost);
        !           690:        free(targ);
        !           691:        free(suser);
        !           692:        free(host);
        !           693:        free(src);
1.1       deraadt   694: }
                    695:
                    696: void
1.104     djm       697: tolocal(int argc, char **argv)
1.1       deraadt   698: {
1.193   ! millert   699:        char *bp, *host = NULL, *src = NULL, *suser = NULL;
1.129     djm       700:        arglist alist;
1.193   ! millert   701:        int i, r, sport = -1;
1.129     djm       702:
                    703:        memset(&alist, '\0', sizeof(alist));
                    704:        alist.list = NULL;
1.1       deraadt   705:
                    706:        for (i = 0; i < argc - 1; i++) {
1.193   ! millert   707:                free(suser);
        !           708:                free(host);
        !           709:                free(src);
        !           710:                r = parse_uri("scp", argv[i], &suser, &host, &sport, &src);
        !           711:                if (r == -1) {
        !           712:                        ++errs;
        !           713:                        continue;
        !           714:                }
        !           715:                if (r != 0)
        !           716:                        parse_user_host_path(argv[i], &suser, &host, &src);
        !           717:                if (suser != NULL && !okname(suser)) {
        !           718:                        ++errs;
        !           719:                        continue;
        !           720:                }
        !           721:                if (!host) {    /* Local to local. */
1.129     djm       722:                        freeargs(&alist);
                    723:                        addargs(&alist, "%s", _PATH_CP);
                    724:                        if (iamrecursive)
                    725:                                addargs(&alist, "-r");
                    726:                        if (pflag)
                    727:                                addargs(&alist, "-p");
1.165     guenther  728:                        addargs(&alist, "--");
1.129     djm       729:                        addargs(&alist, "%s", argv[i]);
                    730:                        addargs(&alist, "%s", argv[argc-1]);
                    731:                        if (do_local_cmd(&alist))
1.1       deraadt   732:                                ++errs;
                    733:                        continue;
                    734:                }
1.193   ! millert   735:                /* Remote to local. */
1.171     djm       736:                xasprintf(&bp, "%s -f %s%s",
                    737:                    cmd, *src == '-' ? "-- " : "", src);
1.193   ! millert   738:                if (do_cmd(host, suser, sport, bp, &remin, &remout) < 0) {
1.173     djm       739:                        free(bp);
1.18      markus    740:                        ++errs;
                    741:                        continue;
1.1       deraadt   742:                }
1.173     djm       743:                free(bp);
1.1       deraadt   744:                sink(1, argv + argc - 1);
1.18      markus    745:                (void) close(remin);
1.1       deraadt   746:                remin = remout = -1;
                    747:        }
1.193   ! millert   748:        free(suser);
        !           749:        free(host);
        !           750:        free(src);
1.1       deraadt   751: }
                    752:
                    753: void
1.104     djm       754: source(int argc, char **argv)
1.1       deraadt   755: {
                    756:        struct stat stb;
                    757:        static BUF buffer;
                    758:        BUF *bp;
1.163     dtucker   759:        off_t i, statbytes;
1.180     djm       760:        size_t amt, nr;
1.125     dtucker   761:        int fd = -1, haderr, indx;
1.181     deraadt   762:        char *last, *name, buf[2048], encname[PATH_MAX];
1.65      deraadt   763:        int len;
1.1       deraadt   764:
                    765:        for (indx = 0; indx < argc; ++indx) {
1.18      markus    766:                name = argv[indx];
1.11      aaron     767:                statbytes = 0;
1.65      deraadt   768:                len = strlen(name);
                    769:                while (len > 1 && name[len-1] == '/')
                    770:                        name[--len] = '\0';
1.158     dtucker   771:                if ((fd = open(name, O_RDONLY|O_NONBLOCK, 0)) < 0)
                    772:                        goto syserr;
1.85      markus    773:                if (strchr(name, '\n') != NULL) {
1.158     dtucker   774:                        strnvis(encname, name, sizeof(encname), VIS_NL);
                    775:                        name = encname;
1.85      markus    776:                }
1.1       deraadt   777:                if (fstat(fd, &stb) < 0) {
                    778: syserr:                        run_err("%s: %s", name, strerror(errno));
                    779:                        goto next;
                    780:                }
1.163     dtucker   781:                if (stb.st_size < 0) {
                    782:                        run_err("%s: %s", name, "Negative file size");
                    783:                        goto next;
                    784:                }
1.157     djm       785:                unset_nonblock(fd);
1.1       deraadt   786:                switch (stb.st_mode & S_IFMT) {
                    787:                case S_IFREG:
                    788:                        break;
                    789:                case S_IFDIR:
                    790:                        if (iamrecursive) {
                    791:                                rsource(name, &stb);
                    792:                                goto next;
                    793:                        }
                    794:                        /* FALLTHROUGH */
                    795:                default:
                    796:                        run_err("%s: not a regular file", name);
                    797:                        goto next;
                    798:                }
                    799:                if ((last = strrchr(name, '/')) == NULL)
                    800:                        last = name;
                    801:                else
                    802:                        ++last;
1.11      aaron     803:                curfile = last;
1.1       deraadt   804:                if (pflag) {
1.176     guenther  805:                        if (do_times(remout, verbose_mode, &stb) < 0)
1.1       deraadt   806:                                goto next;
                    807:                }
                    808: #define        FILEMODEMASK    (S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO)
1.61      markus    809:                snprintf(buf, sizeof buf, "C%04o %lld %s\n",
1.47      markus    810:                    (u_int) (stb.st_mode & FILEMODEMASK),
1.62      markus    811:                    (long long)stb.st_size, last);
1.186     schwarze  812:                if (verbose_mode)
                    813:                        fmprintf(stderr, "Sending file modes: %s", buf);
1.107     deraadt   814:                (void) atomicio(vwrite, remout, buf, strlen(buf));
1.1       deraadt   815:                if (response() < 0)
                    816:                        goto next;
1.161     djm       817:                if ((bp = allocbuf(&buffer, fd, COPY_BUFLEN)) == NULL) {
1.127     deraadt   818: next:                  if (fd != -1) {
                    819:                                (void) close(fd);
                    820:                                fd = -1;
                    821:                        }
1.1       deraadt   822:                        continue;
                    823:                }
1.97      fgsch     824:                if (showprogress)
                    825:                        start_progress_meter(curfile, stb.st_size, &statbytes);
1.161     djm       826:                set_nonblock(remout);
1.1       deraadt   827:                for (haderr = i = 0; i < stb.st_size; i += bp->cnt) {
                    828:                        amt = bp->cnt;
1.163     dtucker   829:                        if (i + (off_t)amt > stb.st_size)
1.1       deraadt   830:                                amt = stb.st_size - i;
                    831:                        if (!haderr) {
1.180     djm       832:                                if ((nr = atomicio(read, fd,
                    833:                                    bp->buf, amt)) != amt) {
1.122     avsm      834:                                        haderr = errno;
1.180     djm       835:                                        memset(bp->buf + nr, 0, amt - nr);
                    836:                                }
1.1       deraadt   837:                        }
1.161     djm       838:                        /* Keep writing after error to retain sync */
                    839:                        if (haderr) {
                    840:                                (void)atomicio(vwrite, remout, bp->buf, amt);
1.180     djm       841:                                memset(bp->buf, 0, amt);
1.161     djm       842:                                continue;
1.1       deraadt   843:                        }
1.167     djm       844:                        if (atomicio6(vwrite, remout, bp->buf, amt, scpio,
1.161     djm       845:                            &statbytes) != amt)
                    846:                                haderr = errno;
1.1       deraadt   847:                }
1.161     djm       848:                unset_nonblock(remout);
1.4       aaron     849:
1.127     deraadt   850:                if (fd != -1) {
                    851:                        if (close(fd) < 0 && !haderr)
                    852:                                haderr = errno;
                    853:                        fd = -1;
                    854:                }
1.1       deraadt   855:                if (!haderr)
1.107     deraadt   856:                        (void) atomicio(vwrite, remout, "", 1);
1.1       deraadt   857:                else
                    858:                        run_err("%s: %s", name, strerror(haderr));
1.18      markus    859:                (void) response();
1.185     dtucker   860:                if (showprogress)
                    861:                        stop_progress_meter();
1.1       deraadt   862:        }
                    863: }
                    864:
                    865: void
1.104     djm       866: rsource(char *name, struct stat *statp)
1.1       deraadt   867: {
                    868:        DIR *dirp;
                    869:        struct dirent *dp;
1.181     deraadt   870:        char *last, *vect[1], path[PATH_MAX];
1.1       deraadt   871:
                    872:        if (!(dirp = opendir(name))) {
                    873:                run_err("%s: %s", name, strerror(errno));
                    874:                return;
                    875:        }
                    876:        last = strrchr(name, '/');
1.183     mmcc      877:        if (last == NULL)
1.1       deraadt   878:                last = name;
                    879:        else
                    880:                last++;
                    881:        if (pflag) {
1.176     guenther  882:                if (do_times(remout, verbose_mode, statp) < 0) {
1.1       deraadt   883:                        closedir(dirp);
                    884:                        return;
                    885:                }
                    886:        }
1.55      deraadt   887:        (void) snprintf(path, sizeof path, "D%04o %d %.1024s\n",
1.47      markus    888:            (u_int) (statp->st_mode & FILEMODEMASK), 0, last);
1.18      markus    889:        if (verbose_mode)
1.186     schwarze  890:                fmprintf(stderr, "Entering directory: %s", path);
1.107     deraadt   891:        (void) atomicio(vwrite, remout, path, strlen(path));
1.1       deraadt   892:        if (response() < 0) {
                    893:                closedir(dirp);
                    894:                return;
                    895:        }
1.63      stevesk   896:        while ((dp = readdir(dirp)) != NULL) {
1.1       deraadt   897:                if (dp->d_ino == 0)
                    898:                        continue;
                    899:                if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
                    900:                        continue;
                    901:                if (strlen(name) + 1 + strlen(dp->d_name) >= sizeof(path) - 1) {
                    902:                        run_err("%s/%s: name too long", name, dp->d_name);
                    903:                        continue;
                    904:                }
1.55      deraadt   905:                (void) snprintf(path, sizeof path, "%s/%s", name, dp->d_name);
1.1       deraadt   906:                vect[0] = path;
                    907:                source(1, vect);
                    908:        }
1.18      markus    909:        (void) closedir(dirp);
1.107     deraadt   910:        (void) atomicio(vwrite, remout, "E\n", 2);
1.18      markus    911:        (void) response();
1.1       deraadt   912: }
                    913:
1.189     millert   914: #define TYPE_OVERFLOW(type, val) \
                    915:        ((sizeof(type) == 4 && (val) > INT32_MAX) || \
                    916:         (sizeof(type) == 8 && (val) > INT64_MAX) || \
                    917:         (sizeof(type) != 4 && sizeof(type) != 8))
                    918:
1.1       deraadt   919: void
1.104     djm       920: sink(int argc, char **argv)
1.1       deraadt   921: {
                    922:        static BUF buffer;
                    923:        struct stat stb;
1.18      markus    924:        enum {
                    925:                YES, NO, DISPLAYED
                    926:        } wrerr;
1.1       deraadt   927:        BUF *bp;
1.122     avsm      928:        off_t i;
1.124     djm       929:        size_t j, count;
1.140     deraadt   930:        int amt, exists, first, ofd;
                    931:        mode_t mode, omode, mask;
1.97      fgsch     932:        off_t size, statbytes;
1.176     guenther  933:        unsigned long long ull;
1.36      deraadt   934:        int setimes, targisdir, wrerrno = 0;
1.186     schwarze  935:        char ch, *cp, *np, *targ, *why, *vect[1], buf[2048], visbuf[2048];
1.40      markus    936:        struct timeval tv[2];
1.1       deraadt   937:
1.66      stevesk   938: #define        atime   tv[0]
                    939: #define        mtime   tv[1]
1.118     deraadt   940: #define        SCREWUP(str)    { why = str; goto screwup; }
1.1       deraadt   941:
1.189     millert   942:        if (TYPE_OVERFLOW(time_t, 0) || TYPE_OVERFLOW(off_t, 0))
                    943:                SCREWUP("Unexpected off_t/time_t size");
                    944:
1.1       deraadt   945:        setimes = targisdir = 0;
                    946:        mask = umask(0);
                    947:        if (!pflag)
1.18      markus    948:                (void) umask(mask);
1.1       deraadt   949:        if (argc != 1) {
                    950:                run_err("ambiguous target");
                    951:                exit(1);
                    952:        }
                    953:        targ = *argv;
                    954:        if (targetshouldbedirectory)
                    955:                verifydir(targ);
1.18      markus    956:
1.107     deraadt   957:        (void) atomicio(vwrite, remout, "", 1);
1.1       deraadt   958:        if (stat(targ, &stb) == 0 && S_ISDIR(stb.st_mode))
                    959:                targisdir = 1;
                    960:        for (first = 1;; first = 0) {
                    961:                cp = buf;
1.122     avsm      962:                if (atomicio(read, remin, cp, 1) != 1)
1.1       deraadt   963:                        return;
                    964:                if (*cp++ == '\n')
                    965:                        SCREWUP("unexpected <newline>");
                    966:                do {
1.30      deraadt   967:                        if (atomicio(read, remin, &ch, sizeof(ch)) != sizeof(ch))
1.1       deraadt   968:                                SCREWUP("lost connection");
                    969:                        *cp++ = ch;
                    970:                } while (cp < &buf[sizeof(buf) - 1] && ch != '\n');
                    971:                *cp = 0;
1.114     markus    972:                if (verbose_mode)
1.186     schwarze  973:                        fmprintf(stderr, "Sink: %s", buf);
1.1       deraadt   974:
                    975:                if (buf[0] == '\01' || buf[0] == '\02') {
1.186     schwarze  976:                        if (iamremote == 0) {
                    977:                                (void) snmprintf(visbuf, sizeof(visbuf),
                    978:                                    NULL, "%s", buf + 1);
1.107     deraadt   979:                                (void) atomicio(vwrite, STDERR_FILENO,
1.186     schwarze  980:                                    visbuf, strlen(visbuf));
                    981:                        }
1.1       deraadt   982:                        if (buf[0] == '\02')
                    983:                                exit(1);
                    984:                        ++errs;
                    985:                        continue;
                    986:                }
                    987:                if (buf[0] == 'E') {
1.107     deraadt   988:                        (void) atomicio(vwrite, remout, "", 1);
1.1       deraadt   989:                        return;
                    990:                }
                    991:                if (ch == '\n')
                    992:                        *--cp = 0;
                    993:
                    994:                cp = buf;
                    995:                if (*cp == 'T') {
                    996:                        setimes++;
                    997:                        cp++;
1.176     guenther  998:                        if (!isdigit((unsigned char)*cp))
                    999:                                SCREWUP("mtime.sec not present");
                   1000:                        ull = strtoull(cp, &cp, 10);
1.66      stevesk  1001:                        if (!cp || *cp++ != ' ')
1.1       deraadt  1002:                                SCREWUP("mtime.sec not delimited");
1.189     millert  1003:                        if (TYPE_OVERFLOW(time_t, ull))
1.176     guenther 1004:                                setimes = 0;    /* out of range */
                   1005:                        mtime.tv_sec = ull;
1.66      stevesk  1006:                        mtime.tv_usec = strtol(cp, &cp, 10);
1.176     guenther 1007:                        if (!cp || *cp++ != ' ' || mtime.tv_usec < 0 ||
                   1008:                            mtime.tv_usec > 999999)
1.1       deraadt  1009:                                SCREWUP("mtime.usec not delimited");
1.176     guenther 1010:                        if (!isdigit((unsigned char)*cp))
                   1011:                                SCREWUP("atime.sec not present");
                   1012:                        ull = strtoull(cp, &cp, 10);
1.66      stevesk  1013:                        if (!cp || *cp++ != ' ')
1.1       deraadt  1014:                                SCREWUP("atime.sec not delimited");
1.189     millert  1015:                        if (TYPE_OVERFLOW(time_t, ull))
1.176     guenther 1016:                                setimes = 0;    /* out of range */
                   1017:                        atime.tv_sec = ull;
1.66      stevesk  1018:                        atime.tv_usec = strtol(cp, &cp, 10);
1.176     guenther 1019:                        if (!cp || *cp++ != '\0' || atime.tv_usec < 0 ||
                   1020:                            atime.tv_usec > 999999)
1.1       deraadt  1021:                                SCREWUP("atime.usec not delimited");
1.107     deraadt  1022:                        (void) atomicio(vwrite, remout, "", 1);
1.1       deraadt  1023:                        continue;
                   1024:                }
                   1025:                if (*cp != 'C' && *cp != 'D') {
                   1026:                        /*
                   1027:                         * Check for the case "rcp remote:foo\* local:bar".
                   1028:                         * In this case, the line "No match." can be returned
                   1029:                         * by the shell before the rcp command on the remote is
                   1030:                         * executed so the ^Aerror_message convention isn't
                   1031:                         * followed.
                   1032:                         */
                   1033:                        if (first) {
                   1034:                                run_err("%s", cp);
                   1035:                                exit(1);
                   1036:                        }
                   1037:                        SCREWUP("expected control record");
                   1038:                }
                   1039:                mode = 0;
                   1040:                for (++cp; cp < buf + 5; cp++) {
                   1041:                        if (*cp < '0' || *cp > '7')
                   1042:                                SCREWUP("bad mode");
                   1043:                        mode = (mode << 3) | (*cp - '0');
                   1044:                }
                   1045:                if (*cp++ != ' ')
                   1046:                        SCREWUP("mode not delimited");
                   1047:
1.188     millert  1048:                if (!isdigit((unsigned char)*cp))
                   1049:                        SCREWUP("size not present");
                   1050:                ull = strtoull(cp, &cp, 10);
                   1051:                if (!cp || *cp++ != ' ')
1.1       deraadt  1052:                        SCREWUP("size not delimited");
1.189     millert  1053:                if (TYPE_OVERFLOW(off_t, ull))
1.188     millert  1054:                        SCREWUP("size out of range");
                   1055:                size = (off_t)ull;
                   1056:
1.114     markus   1057:                if ((strchr(cp, '/') != NULL) || (strcmp(cp, "..") == 0)) {
                   1058:                        run_err("error: unexpected filename: %s", cp);
                   1059:                        exit(1);
                   1060:                }
1.1       deraadt  1061:                if (targisdir) {
                   1062:                        static char *namebuf;
1.124     djm      1063:                        static size_t cursize;
1.1       deraadt  1064:                        size_t need;
                   1065:
                   1066:                        need = strlen(targ) + strlen(cp) + 250;
1.55      deraadt  1067:                        if (need > cursize) {
1.173     djm      1068:                                free(namebuf);
1.18      markus   1069:                                namebuf = xmalloc(need);
1.55      deraadt  1070:                                cursize = need;
                   1071:                        }
                   1072:                        (void) snprintf(namebuf, need, "%s%s%s", targ,
1.88      mouring  1073:                            strcmp(targ, "/") ? "/" : "", cp);
1.1       deraadt  1074:                        np = namebuf;
                   1075:                } else
                   1076:                        np = targ;
1.12      aaron    1077:                curfile = cp;
1.1       deraadt  1078:                exists = stat(np, &stb) == 0;
                   1079:                if (buf[0] == 'D') {
                   1080:                        int mod_flag = pflag;
1.114     markus   1081:                        if (!iamrecursive)
                   1082:                                SCREWUP("received directory without -r");
1.1       deraadt  1083:                        if (exists) {
                   1084:                                if (!S_ISDIR(stb.st_mode)) {
                   1085:                                        errno = ENOTDIR;
                   1086:                                        goto bad;
                   1087:                                }
                   1088:                                if (pflag)
1.18      markus   1089:                                        (void) chmod(np, mode);
1.1       deraadt  1090:                        } else {
1.18      markus   1091:                                /* Handle copying from a read-only
                   1092:                                   directory */
1.1       deraadt  1093:                                mod_flag = 1;
                   1094:                                if (mkdir(np, mode | S_IRWXU) < 0)
                   1095:                                        goto bad;
                   1096:                        }
1.58      danh     1097:                        vect[0] = xstrdup(np);
1.1       deraadt  1098:                        sink(1, vect);
                   1099:                        if (setimes) {
                   1100:                                setimes = 0;
1.59      deraadt  1101:                                if (utimes(vect[0], tv) < 0)
1.18      markus   1102:                                        run_err("%s: set times: %s",
1.59      deraadt  1103:                                            vect[0], strerror(errno));
1.1       deraadt  1104:                        }
                   1105:                        if (mod_flag)
1.59      deraadt  1106:                                (void) chmod(vect[0], mode);
1.173     djm      1107:                        free(vect[0]);
1.1       deraadt  1108:                        continue;
                   1109:                }
                   1110:                omode = mode;
1.174     dtucker  1111:                mode |= S_IWUSR;
1.71      markus   1112:                if ((ofd = open(np, O_WRONLY|O_CREAT, mode)) < 0) {
1.1       deraadt  1113: bad:                   run_err("%s: %s", np, strerror(errno));
                   1114:                        continue;
                   1115:                }
1.107     deraadt  1116:                (void) atomicio(vwrite, remout, "", 1);
1.161     djm      1117:                if ((bp = allocbuf(&buffer, ofd, COPY_BUFLEN)) == NULL) {
1.18      markus   1118:                        (void) close(ofd);
1.1       deraadt  1119:                        continue;
                   1120:                }
                   1121:                cp = bp->buf;
                   1122:                wrerr = NO;
1.7       deraadt  1123:
1.12      aaron    1124:                statbytes = 0;
1.97      fgsch    1125:                if (showprogress)
                   1126:                        start_progress_meter(curfile, size, &statbytes);
1.161     djm      1127:                set_nonblock(remin);
                   1128:                for (count = i = 0; i < size; i += bp->cnt) {
                   1129:                        amt = bp->cnt;
1.1       deraadt  1130:                        if (i + amt > size)
                   1131:                                amt = size - i;
                   1132:                        count += amt;
                   1133:                        do {
1.167     djm      1134:                                j = atomicio6(read, remin, cp, amt,
                   1135:                                    scpio, &statbytes);
1.122     avsm     1136:                                if (j == 0) {
1.161     djm      1137:                                        run_err("%s", j != EPIPE ?
                   1138:                                            strerror(errno) :
1.63      stevesk  1139:                                            "dropped connection");
1.1       deraadt  1140:                                        exit(1);
                   1141:                                }
                   1142:                                amt -= j;
                   1143:                                cp += j;
                   1144:                        } while (amt > 0);
1.112     djm      1145:
1.1       deraadt  1146:                        if (count == bp->cnt) {
                   1147:                                /* Keep reading so we stay sync'd up. */
                   1148:                                if (wrerr == NO) {
1.122     avsm     1149:                                        if (atomicio(vwrite, ofd, bp->buf,
                   1150:                                            count) != count) {
1.1       deraadt  1151:                                                wrerr = YES;
1.122     avsm     1152:                                                wrerrno = errno;
1.1       deraadt  1153:                                        }
                   1154:                                }
                   1155:                                count = 0;
                   1156:                                cp = bp->buf;
                   1157:                        }
                   1158:                }
1.161     djm      1159:                unset_nonblock(remin);
1.1       deraadt  1160:                if (count != 0 && wrerr == NO &&
1.122     avsm     1161:                    atomicio(vwrite, ofd, bp->buf, count) != count) {
1.1       deraadt  1162:                        wrerr = YES;
1.122     avsm     1163:                        wrerrno = errno;
1.1       deraadt  1164:                }
1.159     djm      1165:                if (wrerr == NO && (!exists || S_ISREG(stb.st_mode)) &&
                   1166:                    ftruncate(ofd, size) != 0) {
1.1       deraadt  1167:                        run_err("%s: truncate: %s", np, strerror(errno));
                   1168:                        wrerr = DISPLAYED;
                   1169:                }
                   1170:                if (pflag) {
                   1171:                        if (exists || omode != mode)
1.116     dtucker  1172:                                if (fchmod(ofd, omode)) {
1.1       deraadt  1173:                                        run_err("%s: set mode: %s",
1.63      stevesk  1174:                                            np, strerror(errno));
1.116     dtucker  1175:                                        wrerr = DISPLAYED;
                   1176:                                }
1.1       deraadt  1177:                } else {
                   1178:                        if (!exists && omode != mode)
1.116     dtucker  1179:                                if (fchmod(ofd, omode & ~mask)) {
1.1       deraadt  1180:                                        run_err("%s: set mode: %s",
1.63      stevesk  1181:                                            np, strerror(errno));
1.116     dtucker  1182:                                        wrerr = DISPLAYED;
                   1183:                                }
1.1       deraadt  1184:                }
1.33      provos   1185:                if (close(ofd) == -1) {
                   1186:                        wrerr = YES;
                   1187:                        wrerrno = errno;
                   1188:                }
1.18      markus   1189:                (void) response();
1.185     dtucker  1190:                if (showprogress)
                   1191:                        stop_progress_meter();
1.1       deraadt  1192:                if (setimes && wrerr == NO) {
                   1193:                        setimes = 0;
1.40      markus   1194:                        if (utimes(np, tv) < 0) {
1.1       deraadt  1195:                                run_err("%s: set times: %s",
1.63      stevesk  1196:                                    np, strerror(errno));
1.1       deraadt  1197:                                wrerr = DISPLAYED;
                   1198:                        }
                   1199:                }
1.18      markus   1200:                switch (wrerr) {
1.1       deraadt  1201:                case YES:
                   1202:                        run_err("%s: %s", np, strerror(wrerrno));
                   1203:                        break;
                   1204:                case NO:
1.107     deraadt  1205:                        (void) atomicio(vwrite, remout, "", 1);
1.1       deraadt  1206:                        break;
                   1207:                case DISPLAYED:
                   1208:                        break;
                   1209:                }
                   1210:        }
                   1211: screwup:
                   1212:        run_err("protocol error: %s", why);
                   1213:        exit(1);
                   1214: }
                   1215:
                   1216: int
1.86      itojun   1217: response(void)
1.1       deraadt  1218: {
1.186     schwarze 1219:        char ch, *cp, resp, rbuf[2048], visbuf[2048];
1.1       deraadt  1220:
1.30      deraadt  1221:        if (atomicio(read, remin, &resp, sizeof(resp)) != sizeof(resp))
1.1       deraadt  1222:                lostconn(0);
                   1223:
                   1224:        cp = rbuf;
1.18      markus   1225:        switch (resp) {
                   1226:        case 0:         /* ok */
1.1       deraadt  1227:                return (0);
                   1228:        default:
                   1229:                *cp++ = resp;
                   1230:                /* FALLTHROUGH */
1.18      markus   1231:        case 1:         /* error, followed by error msg */
                   1232:        case 2:         /* fatal error, "" */
1.1       deraadt  1233:                do {
1.30      deraadt  1234:                        if (atomicio(read, remin, &ch, sizeof(ch)) != sizeof(ch))
1.1       deraadt  1235:                                lostconn(0);
                   1236:                        *cp++ = ch;
                   1237:                } while (cp < &rbuf[sizeof(rbuf) - 1] && ch != '\n');
                   1238:
1.186     schwarze 1239:                if (!iamremote) {
                   1240:                        cp[-1] = '\0';
                   1241:                        (void) snmprintf(visbuf, sizeof(visbuf),
                   1242:                            NULL, "%s\n", rbuf);
                   1243:                        (void) atomicio(vwrite, STDERR_FILENO,
                   1244:                            visbuf, strlen(visbuf));
                   1245:                }
1.1       deraadt  1246:                ++errs;
                   1247:                if (resp == 1)
                   1248:                        return (-1);
                   1249:                exit(1);
                   1250:        }
                   1251:        /* NOTREACHED */
                   1252: }
                   1253:
                   1254: void
1.86      itojun   1255: usage(void)
1.1       deraadt  1256: {
1.83      stevesk  1257:        (void) fprintf(stderr,
1.191     jmc      1258:            "usage: scp [-346BCpqrv] [-c cipher] [-F ssh_config] [-i identity_file]\n"
1.193   ! millert  1259:            "           [-l limit] [-o ssh_option] [-P port] [-S program] source ... target\n");
1.1       deraadt  1260:        exit(1);
                   1261: }
                   1262:
                   1263: void
1.18      markus   1264: run_err(const char *fmt,...)
1.1       deraadt  1265: {
                   1266:        static FILE *fp;
                   1267:        va_list ap;
                   1268:
                   1269:        ++errs;
1.136     biorn    1270:        if (fp != NULL || (remout != -1 && (fp = fdopen(remout, "w")))) {
                   1271:                (void) fprintf(fp, "%c", 0x01);
                   1272:                (void) fprintf(fp, "scp: ");
                   1273:                va_start(ap, fmt);
                   1274:                (void) vfprintf(fp, fmt, ap);
                   1275:                va_end(ap);
                   1276:                (void) fprintf(fp, "\n");
                   1277:                (void) fflush(fp);
                   1278:        }
1.18      markus   1279:
                   1280:        if (!iamremote) {
1.73      markus   1281:                va_start(ap, fmt);
1.186     schwarze 1282:                vfmprintf(stderr, fmt, ap);
1.73      markus   1283:                va_end(ap);
1.18      markus   1284:                fprintf(stderr, "\n");
                   1285:        }
1.1       deraadt  1286: }
                   1287:
                   1288: void
1.104     djm      1289: verifydir(char *cp)
1.1       deraadt  1290: {
                   1291:        struct stat stb;
                   1292:
                   1293:        if (!stat(cp, &stb)) {
                   1294:                if (S_ISDIR(stb.st_mode))
                   1295:                        return;
                   1296:                errno = ENOTDIR;
                   1297:        }
                   1298:        run_err("%s: %s", cp, strerror(errno));
1.123     avsm     1299:        killchild(0);
1.1       deraadt  1300: }
                   1301:
                   1302: int
1.104     djm      1303: okname(char *cp0)
1.1       deraadt  1304: {
                   1305:        int c;
                   1306:        char *cp;
                   1307:
                   1308:        cp = cp0;
                   1309:        do {
1.75      deraadt  1310:                c = (int)*cp;
1.1       deraadt  1311:                if (c & 0200)
                   1312:                        goto bad;
1.179     deraadt  1313:                if (!isalpha(c) && !isdigit((unsigned char)c)) {
1.101     markus   1314:                        switch (c) {
                   1315:                        case '\'':
                   1316:                        case '"':
                   1317:                        case '`':
                   1318:                        case ' ':
                   1319:                        case '#':
                   1320:                                goto bad;
                   1321:                        default:
                   1322:                                break;
                   1323:                        }
                   1324:                }
1.1       deraadt  1325:        } while (*++cp);
                   1326:        return (1);
                   1327:
1.186     schwarze 1328: bad:   fmprintf(stderr, "%s: invalid user name\n", cp0);
1.1       deraadt  1329:        return (0);
                   1330: }
                   1331:
                   1332: BUF *
1.104     djm      1333: allocbuf(BUF *bp, int fd, int blksize)
1.1       deraadt  1334: {
                   1335:        size_t size;
                   1336:        struct stat stb;
                   1337:
                   1338:        if (fstat(fd, &stb) < 0) {
                   1339:                run_err("fstat: %s", strerror(errno));
                   1340:                return (0);
                   1341:        }
1.187     deraadt  1342:        size = ROUNDUP(stb.st_blksize, blksize);
1.95      markus   1343:        if (size == 0)
1.18      markus   1344:                size = blksize;
1.1       deraadt  1345:        if (bp->cnt >= size)
                   1346:                return (bp);
1.192     deraadt  1347:        bp->buf = xrecallocarray(bp->buf, bp->cnt, size, 1);
1.1       deraadt  1348:        bp->cnt = size;
                   1349:        return (bp);
                   1350: }
                   1351:
                   1352: void
1.104     djm      1353: lostconn(int signo)
1.1       deraadt  1354: {
                   1355:        if (!iamremote)
1.172     dtucker  1356:                (void)write(STDERR_FILENO, "lost connection\n", 16);
1.74      markus   1357:        if (signo)
                   1358:                _exit(1);
                   1359:        else
                   1360:                exit(1);
1.4       aaron    1361: }