[BACK]Return to sftp-int.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / ssh

Annotation of src/usr.bin/ssh/sftp-int.c, Revision 1.35

1.1       djm         1: /*
                      2:  * Copyright (c) 2001 Damien Miller.  All rights reserved.
                      3:  *
                      4:  * Redistribution and use in source and binary forms, with or without
                      5:  * modification, are permitted provided that the following conditions
                      6:  * are met:
                      7:  * 1. Redistributions of source code must retain the above copyright
                      8:  *    notice, this list of conditions and the following disclaimer.
                      9:  * 2. Redistributions in binary form must reproduce the above copyright
                     10:  *    notice, this list of conditions and the following disclaimer in the
                     11:  *    documentation and/or other materials provided with the distribution.
                     12:  *
                     13:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
                     14:  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
                     15:  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
                     16:  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
                     17:  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
                     18:  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
                     19:  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
                     20:  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
                     21:  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
                     22:  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
                     23:  */
                     24:
1.12      djm        25: /* XXX: globbed ls */
1.1       djm        26: /* XXX: recursive operations */
                     27:
                     28: #include "includes.h"
1.35    ! mouring    29: RCSID("$OpenBSD: sftp-int.c,v 1.34 2001/04/11 07:06:22 djm Exp $");
1.27      djm        30:
                     31: #include <glob.h>
1.1       djm        32:
                     33: #include "buffer.h"
                     34: #include "xmalloc.h"
                     35: #include "log.h"
                     36: #include "pathnames.h"
                     37:
                     38: #include "sftp.h"
                     39: #include "sftp-common.h"
1.27      djm        40: #include "sftp-glob.h"
1.1       djm        41: #include "sftp-client.h"
                     42: #include "sftp-int.h"
                     43:
1.26      djm        44: /* File to read commands from */
                     45: extern FILE *infile;
                     46:
                     47: /* Version of server we are speaking to */
                     48: int version;
1.25      deraadt    49:
1.1       djm        50: /* Seperators for interactive commands */
                     51: #define WHITESPACE " \t\r\n"
                     52:
                     53: /* Commands for interactive mode */
                     54: #define I_CHDIR                1
                     55: #define I_CHGRP                2
                     56: #define I_CHMOD                3
                     57: #define I_CHOWN                4
                     58: #define I_GET          5
                     59: #define I_HELP         6
                     60: #define I_LCHDIR       7
                     61: #define I_LLS          8
                     62: #define I_LMKDIR       9
                     63: #define I_LPWD         10
                     64: #define I_LS           11
                     65: #define I_LUMASK       12
                     66: #define I_MKDIR                13
                     67: #define I_PUT          14
                     68: #define I_PWD          15
                     69: #define I_QUIT         16
                     70: #define I_RENAME       17
                     71: #define I_RM           18
                     72: #define I_RMDIR                19
                     73: #define I_SHELL                20
1.26      djm        74: #define I_SYMLINK      21
1.28      markus     75: #define I_VERSION      22
1.1       djm        76:
                     77: struct CMD {
1.6       deraadt    78:        const char *c;
1.1       djm        79:        const int n;
                     80: };
                     81:
                     82: const struct CMD cmds[] = {
1.15      stevesk    83:        { "cd",         I_CHDIR },
                     84:        { "chdir",      I_CHDIR },
                     85:        { "chgrp",      I_CHGRP },
                     86:        { "chmod",      I_CHMOD },
                     87:        { "chown",      I_CHOWN },
                     88:        { "dir",        I_LS },
                     89:        { "exit",       I_QUIT },
                     90:        { "get",        I_GET },
1.34      djm        91:        { "mget",       I_GET },
1.15      stevesk    92:        { "help",       I_HELP },
                     93:        { "lcd",        I_LCHDIR },
                     94:        { "lchdir",     I_LCHDIR },
                     95:        { "lls",        I_LLS },
                     96:        { "lmkdir",     I_LMKDIR },
1.26      djm        97:        { "ln",         I_SYMLINK },
1.15      stevesk    98:        { "lpwd",       I_LPWD },
                     99:        { "ls",         I_LS },
                    100:        { "lumask",     I_LUMASK },
                    101:        { "mkdir",      I_MKDIR },
                    102:        { "put",        I_PUT },
1.34      djm       103:        { "mput",       I_PUT },
1.15      stevesk   104:        { "pwd",        I_PWD },
                    105:        { "quit",       I_QUIT },
                    106:        { "rename",     I_RENAME },
                    107:        { "rm",         I_RM },
                    108:        { "rmdir",      I_RMDIR },
1.26      djm       109:        { "symlink",    I_SYMLINK },
1.28      markus    110:        { "version",    I_VERSION },
1.6       deraadt   111:        { "!",          I_SHELL },
1.7       deraadt   112:        { "?",          I_HELP },
1.6       deraadt   113:        { NULL,                 -1}
1.1       djm       114: };
                    115:
                    116: void
                    117: help(void)
                    118: {
                    119:        printf("Available commands:\n");
1.13      stevesk   120:        printf("cd path                       Change remote directory to 'path'\n");
                    121:        printf("lcd path                      Change local directory to 'path'\n");
                    122:        printf("chgrp grp path                Change group of file 'path' to 'grp'\n");
                    123:        printf("chmod mode path               Change permissions of file 'path' to 'mode'\n");
                    124:        printf("chown own path                Change owner of file 'path' to 'own'\n");
                    125:        printf("help                          Display this help text\n");
                    126:        printf("get remote-path [local-path]  Download file\n");
                    127:        printf("lls [ls-options [path]]       Display local directory listing\n");
1.26      djm       128:        printf("ln oldpath newpath            Symlink remote file\n");
1.13      stevesk   129:        printf("lmkdir path                   Create local directory\n");
                    130:        printf("lpwd                          Print local working directory\n");
                    131:        printf("ls [path]                     Display remote directory listing\n");
                    132:        printf("lumask umask                  Set local umask to 'umask'\n");
                    133:        printf("mkdir path                    Create remote directory\n");
                    134:        printf("put local-path [remote-path]  Upload file\n");
                    135:        printf("pwd                           Display remote working directory\n");
                    136:        printf("exit                          Quit sftp\n");
                    137:        printf("quit                          Quit sftp\n");
                    138:        printf("rename oldpath newpath        Rename remote file\n");
                    139:        printf("rmdir path                    Remove remote directory\n");
                    140:        printf("rm path                       Delete remote file\n");
1.26      djm       141:        printf("symlink oldpath newpath       Symlink remote file\n");
1.28      markus    142:        printf("version                       Show SFTP version\n");
1.1       djm       143:        printf("!command                      Execute 'command' in local shell\n");
                    144:        printf("!                             Escape to local shell\n");
1.13      stevesk   145:        printf("?                             Synonym for help\n");
1.1       djm       146: }
                    147:
                    148: void
                    149: local_do_shell(const char *args)
                    150: {
                    151:        int ret, status;
                    152:        char *shell;
                    153:        pid_t pid;
1.3       stevesk   154:
1.1       djm       155:        if (!*args)
                    156:                args = NULL;
1.3       stevesk   157:
1.1       djm       158:        if ((shell = getenv("SHELL")) == NULL)
                    159:                shell = _PATH_BSHELL;
                    160:
                    161:        if ((pid = fork()) == -1)
                    162:                fatal("Couldn't fork: %s", strerror(errno));
                    163:
                    164:        if (pid == 0) {
                    165:                /* XXX: child has pipe fds to ssh subproc open - issue? */
                    166:                if (args) {
                    167:                        debug3("Executing %s -c \"%s\"", shell, args);
                    168:                        ret = execl(shell, shell, "-c", args, NULL);
                    169:                } else {
                    170:                        debug3("Executing %s", shell);
                    171:                        ret = execl(shell, shell, NULL);
                    172:                }
1.3       stevesk   173:                fprintf(stderr, "Couldn't execute \"%s\": %s\n", shell,
1.1       djm       174:                    strerror(errno));
                    175:                _exit(1);
                    176:        }
                    177:        if (waitpid(pid, &status, 0) == -1)
                    178:                fatal("Couldn't wait for child: %s", strerror(errno));
                    179:        if (!WIFEXITED(status))
                    180:                error("Shell exited abormally");
                    181:        else if (WEXITSTATUS(status))
                    182:                error("Shell exited with status %d", WEXITSTATUS(status));
                    183: }
                    184:
1.3       stevesk   185: void
1.1       djm       186: local_do_ls(const char *args)
                    187: {
                    188:        if (!args || !*args)
1.18      stevesk   189:                local_do_shell(_PATH_LS);
1.1       djm       190:        else {
1.18      stevesk   191:                int len = strlen(_PATH_LS " ") + strlen(args) + 1;
1.16      deraadt   192:                char *buf = xmalloc(len);
1.1       djm       193:
                    194:                /* XXX: quoting - rip quoting code from ftp? */
1.18      stevesk   195:                snprintf(buf, len, _PATH_LS " %s", args);
1.1       djm       196:                local_do_shell(buf);
1.16      deraadt   197:                xfree(buf);
1.1       djm       198:        }
                    199: }
                    200:
                    201: char *
1.29      djm       202: path_append(char *p1, char *p2)
                    203: {
                    204:        char *ret;
1.31      markus    205:        int len = strlen(p1) + strlen(p2) + 2;
1.29      djm       206:
1.31      markus    207:        ret = xmalloc(len);
                    208:        strlcpy(ret, p1, len);
                    209:        strlcat(ret, "/", len);
                    210:        strlcat(ret, p2, len);
1.29      djm       211:
                    212:        return(ret);
                    213: }
                    214:
                    215: char *
1.1       djm       216: make_absolute(char *p, char *pwd)
                    217: {
1.29      djm       218:        char *abs;
1.1       djm       219:
                    220:        /* Derelativise */
                    221:        if (p && p[0] != '/') {
1.29      djm       222:                abs = path_append(pwd, p);
1.1       djm       223:                xfree(p);
1.29      djm       224:                return(abs);
                    225:        } else
                    226:                return(p);
                    227: }
                    228:
                    229: int
                    230: infer_path(const char *p, char **ifp)
                    231: {
                    232:        char *cp;
                    233:
                    234:        cp = strrchr(p, '/');
                    235:        if (cp == NULL) {
                    236:                *ifp = xstrdup(p);
                    237:                return(0);
1.1       djm       238:        }
                    239:
1.29      djm       240:        if (!cp[1]) {
                    241:                error("Invalid path");
                    242:                return(-1);
                    243:        }
                    244:
                    245:        *ifp = xstrdup(cp + 1);
                    246:        return(0);
1.1       djm       247: }
                    248:
                    249: int
                    250: parse_getput_flags(const char **cpp, int *pflag)
                    251: {
                    252:        const char *cp = *cpp;
                    253:
                    254:        /* Check for flags */
                    255:        if (cp[0] == '-' && cp[1] && strchr(WHITESPACE, cp[2])) {
1.20      djm       256:                switch (cp[1]) {
1.22      djm       257:                case 'p':
1.1       djm       258:                case 'P':
                    259:                        *pflag = 1;
                    260:                        break;
                    261:                default:
1.22      djm       262:                        error("Invalid flag -%c", cp[1]);
1.1       djm       263:                        return(-1);
                    264:                }
                    265:                cp += 2;
                    266:                *cpp = cp + strspn(cp, WHITESPACE);
                    267:        }
                    268:
                    269:        return(0);
                    270: }
                    271:
                    272: int
                    273: get_pathname(const char **cpp, char **path)
                    274: {
1.8       provos    275:        const char *cp = *cpp, *end;
                    276:        char quot;
1.1       djm       277:        int i;
                    278:
                    279:        cp += strspn(cp, WHITESPACE);
                    280:        if (!*cp) {
                    281:                *cpp = cp;
                    282:                *path = NULL;
1.8       provos    283:                return (0);
1.1       djm       284:        }
                    285:
                    286:        /* Check for quoted filenames */
                    287:        if (*cp == '\"' || *cp == '\'') {
1.8       provos    288:                quot = *cp++;
1.30      markus    289:
1.8       provos    290:                end = strchr(cp, quot);
                    291:                if (end == NULL) {
1.1       djm       292:                        error("Unterminated quote");
1.8       provos    293:                        goto fail;
1.1       djm       294:                }
1.8       provos    295:                if (cp == end) {
1.1       djm       296:                        error("Empty quotes");
1.8       provos    297:                        goto fail;
1.1       djm       298:                }
1.8       provos    299:                *cpp = end + 1 + strspn(end + 1, WHITESPACE);
                    300:        } else {
                    301:                /* Read to end of filename */
                    302:                end = strpbrk(cp, WHITESPACE);
                    303:                if (end == NULL)
                    304:                        end = strchr(cp, '\0');
                    305:                *cpp = end + strspn(end, WHITESPACE);
1.1       djm       306:        }
                    307:
1.8       provos    308:        i = end - cp;
1.1       djm       309:
                    310:        *path = xmalloc(i + 1);
                    311:        memcpy(*path, cp, i);
                    312:        (*path)[i] = '\0';
                    313:        return(0);
1.8       provos    314:
                    315:  fail:
                    316:        *path = NULL;
                    317:        return (-1);
1.1       djm       318: }
                    319:
                    320: int
1.29      djm       321: is_dir(char *path)
                    322: {
                    323:        struct stat sb;
                    324:
                    325:        /* XXX: report errors? */
                    326:        if (stat(path, &sb) == -1)
                    327:                return(0);
                    328:
                    329:        return(sb.st_mode & S_IFDIR);
                    330: }
                    331:
                    332: int
                    333: remote_is_dir(int in, int out, char *path)
1.1       djm       334: {
1.29      djm       335:        Attrib *a;
1.1       djm       336:
1.29      djm       337:        /* XXX: report errors? */
                    338:        if ((a = do_stat(in, out, path, 1)) == NULL)
                    339:                return(0);
                    340:        if (!(a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS))
1.1       djm       341:                return(0);
1.29      djm       342:        return(a->perm & S_IFDIR);
                    343: }
                    344:
                    345: int
                    346: process_get(int in, int out, char *src, char *dst, char *pwd, int pflag)
                    347: {
                    348:        char *abs_src = NULL;
                    349:        char *abs_dst = NULL;
                    350:        char *tmp;
                    351:        glob_t g;
                    352:        int err = 0;
                    353:        int i;
1.30      markus    354:
1.29      djm       355:        abs_src = xstrdup(src);
                    356:        abs_src = make_absolute(abs_src, pwd);
                    357:
1.30      markus    358:        memset(&g, 0, sizeof(g));
1.29      djm       359:        debug3("Looking up %s", abs_src);
                    360:        if (remote_glob(in, out, abs_src, 0, NULL, &g)) {
                    361:                error("File \"%s\" not found.", abs_src);
                    362:                err = -1;
                    363:                goto out;
                    364:        }
                    365:
                    366:        /* Only one match, dst may be file, directory or unspecified */
                    367:        if (g.gl_pathv[0] && g.gl_matchc == 1) {
                    368:                if (dst) {
                    369:                        /* If directory specified, append filename */
                    370:                        if (is_dir(dst)) {
                    371:                                if (infer_path(g.gl_pathv[0], &tmp)) {
                    372:                                        err = 1;
                    373:                                        goto out;
                    374:                                }
                    375:                                abs_dst = path_append(dst, tmp);
                    376:                                xfree(tmp);
                    377:                        } else
                    378:                                abs_dst = xstrdup(dst);
                    379:                } else if (infer_path(g.gl_pathv[0], &abs_dst)) {
                    380:                        err = -1;
                    381:                        goto out;
                    382:                }
                    383:                printf("Fetching %s to %s\n", g.gl_pathv[0], abs_dst);
                    384:                err = do_download(in, out, g.gl_pathv[0], abs_dst, pflag);
                    385:                goto out;
                    386:        }
                    387:
                    388:        /* Multiple matches, dst may be directory or unspecified */
                    389:        if (dst && !is_dir(dst)) {
1.30      markus    390:                error("Multiple files match, but \"%s\" is not a directory",
1.29      djm       391:                    dst);
                    392:                err = -1;
                    393:                goto out;
                    394:        }
1.30      markus    395:
1.29      djm       396:        for(i = 0; g.gl_pathv[i]; i++) {
                    397:                if (infer_path(g.gl_pathv[i], &tmp)) {
                    398:                        err = -1;
                    399:                        goto out;
                    400:                }
                    401:                if (dst) {
                    402:                        abs_dst = path_append(dst, tmp);
                    403:                        xfree(tmp);
                    404:                } else
                    405:                        abs_dst = tmp;
                    406:
                    407:                printf("Fetching %s to %s\n", g.gl_pathv[i], abs_dst);
                    408:                if (do_download(in, out, g.gl_pathv[i], abs_dst, pflag) == -1)
                    409:                        err = -1;
                    410:                xfree(abs_dst);
                    411:                abs_dst = NULL;
1.1       djm       412:        }
                    413:
1.29      djm       414: out:
                    415:        xfree(abs_src);
                    416:        if (abs_dst)
                    417:                xfree(abs_dst);
                    418:        globfree(&g);
                    419:        return(err);
                    420: }
                    421:
                    422: int
                    423: process_put(int in, int out, char *src, char *dst, char *pwd, int pflag)
                    424: {
                    425:        char *tmp_dst = NULL;
                    426:        char *abs_dst = NULL;
                    427:        char *tmp;
                    428:        glob_t g;
                    429:        int err = 0;
                    430:        int i;
                    431:
                    432:        if (dst) {
                    433:                tmp_dst = xstrdup(dst);
                    434:                tmp_dst = make_absolute(tmp_dst, pwd);
                    435:        }
                    436:
1.30      markus    437:        memset(&g, 0, sizeof(g));
1.29      djm       438:        debug3("Looking up %s", src);
                    439:        if (glob(src, 0, NULL, &g)) {
                    440:                error("File \"%s\" not found.", src);
                    441:                err = -1;
                    442:                goto out;
                    443:        }
                    444:
                    445:        /* Only one match, dst may be file, directory or unspecified */
                    446:        if (g.gl_pathv[0] && g.gl_matchc == 1) {
                    447:                if (tmp_dst) {
                    448:                        /* If directory specified, append filename */
                    449:                        if (remote_is_dir(in, out, tmp_dst)) {
                    450:                                if (infer_path(g.gl_pathv[0], &tmp)) {
                    451:                                        err = 1;
                    452:                                        goto out;
                    453:                                }
                    454:                                abs_dst = path_append(tmp_dst, tmp);
                    455:                                xfree(tmp);
                    456:                        } else
                    457:                                abs_dst = xstrdup(tmp_dst);
1.32      markus    458:                } else {
                    459:                        if (infer_path(g.gl_pathv[0], &abs_dst)) {
                    460:                                err = -1;
                    461:                                goto out;
                    462:                        }
                    463:                        abs_dst = make_absolute(abs_dst, pwd);
1.29      djm       464:                }
                    465:                printf("Uploading %s to %s\n", g.gl_pathv[0], abs_dst);
                    466:                err = do_upload(in, out, g.gl_pathv[0], abs_dst, pflag);
                    467:                goto out;
                    468:        }
                    469:
                    470:        /* Multiple matches, dst may be directory or unspecified */
                    471:        if (tmp_dst && !remote_is_dir(in, out, tmp_dst)) {
1.30      markus    472:                error("Multiple files match, but \"%s\" is not a directory",
1.29      djm       473:                    tmp_dst);
                    474:                err = -1;
                    475:                goto out;
                    476:        }
                    477:
                    478:        for(i = 0; g.gl_pathv[i]; i++) {
                    479:                if (infer_path(g.gl_pathv[i], &tmp)) {
                    480:                        err = -1;
                    481:                        goto out;
                    482:                }
                    483:                if (tmp_dst) {
                    484:                        abs_dst = path_append(tmp_dst, tmp);
                    485:                        xfree(tmp);
                    486:                } else
                    487:                        abs_dst = make_absolute(tmp, pwd);
                    488:
                    489:                printf("Uploading %s to %s\n", g.gl_pathv[i], abs_dst);
                    490:                if (do_upload(in, out, g.gl_pathv[i], abs_dst, pflag) == -1)
                    491:                        err = -1;
1.1       djm       492:        }
                    493:
1.29      djm       494: out:
                    495:        if (abs_dst)
                    496:                xfree(abs_dst);
                    497:        if (tmp_dst)
                    498:                xfree(tmp_dst);
                    499:        return(err);
1.1       djm       500: }
                    501:
                    502: int
                    503: parse_args(const char **cpp, int *pflag, unsigned long *n_arg,
                    504:     char **path1, char **path2)
                    505: {
                    506:        const char *cmd, *cp = *cpp;
1.21      stevesk   507:        char *cp2;
1.4       markus    508:        int base = 0;
1.21      stevesk   509:        long l;
1.1       djm       510:        int i, cmdnum;
                    511:
                    512:        /* Skip leading whitespace */
                    513:        cp = cp + strspn(cp, WHITESPACE);
                    514:
                    515:        /* Ignore blank lines */
                    516:        if (!*cp)
                    517:                return(-1);
                    518:
                    519:        /* Figure out which command we have */
                    520:        for(i = 0; cmds[i].c; i++) {
                    521:                int cmdlen = strlen(cmds[i].c);
                    522:
                    523:                /* Check for command followed by whitespace */
                    524:                if (!strncasecmp(cp, cmds[i].c, cmdlen) &&
                    525:                    strchr(WHITESPACE, cp[cmdlen])) {
                    526:                        cp += cmdlen;
                    527:                        cp = cp + strspn(cp, WHITESPACE);
                    528:                        break;
                    529:                }
                    530:        }
                    531:        cmdnum = cmds[i].n;
                    532:        cmd = cmds[i].c;
                    533:
                    534:        /* Special case */
                    535:        if (*cp == '!') {
                    536:                cp++;
                    537:                cmdnum = I_SHELL;
                    538:        } else if (cmdnum == -1) {
                    539:                error("Invalid command.");
                    540:                return(-1);
                    541:        }
                    542:
                    543:        /* Get arguments and parse flags */
                    544:        *pflag = *n_arg = 0;
                    545:        *path1 = *path2 = NULL;
                    546:        switch (cmdnum) {
                    547:        case I_GET:
                    548:        case I_PUT:
                    549:                if (parse_getput_flags(&cp, pflag))
                    550:                        return(-1);
                    551:                /* Get first pathname (mandatory) */
                    552:                if (get_pathname(&cp, path1))
                    553:                        return(-1);
                    554:                if (*path1 == NULL) {
                    555:                        error("You must specify at least one path after a "
                    556:                            "%s command.", cmd);
                    557:                        return(-1);
                    558:                }
                    559:                /* Try to get second pathname (optional) */
                    560:                if (get_pathname(&cp, path2))
                    561:                        return(-1);
                    562:                break;
                    563:        case I_RENAME:
1.26      djm       564:        case I_SYMLINK:
1.1       djm       565:                if (get_pathname(&cp, path1))
                    566:                        return(-1);
                    567:                if (get_pathname(&cp, path2))
                    568:                        return(-1);
                    569:                if (!*path1 || !*path2) {
                    570:                        error("You must specify two paths after a %s "
                    571:                            "command.", cmd);
                    572:                        return(-1);
                    573:                }
                    574:                break;
                    575:        case I_RM:
                    576:        case I_MKDIR:
                    577:        case I_RMDIR:
                    578:        case I_CHDIR:
                    579:        case I_LCHDIR:
                    580:        case I_LMKDIR:
                    581:                /* Get pathname (mandatory) */
                    582:                if (get_pathname(&cp, path1))
                    583:                        return(-1);
                    584:                if (*path1 == NULL) {
1.3       stevesk   585:                        error("You must specify a path after a %s command.",
1.1       djm       586:                            cmd);
                    587:                        return(-1);
                    588:                }
                    589:                break;
                    590:        case I_LS:
                    591:                /* Path is optional */
                    592:                if (get_pathname(&cp, path1))
                    593:                        return(-1);
                    594:                break;
                    595:        case I_LLS:
                    596:        case I_SHELL:
                    597:                /* Uses the rest of the line */
                    598:                break;
                    599:        case I_LUMASK:
1.21      stevesk   600:                base = 8;
1.1       djm       601:        case I_CHMOD:
1.4       markus    602:                base = 8;
1.1       djm       603:        case I_CHOWN:
                    604:        case I_CHGRP:
                    605:                /* Get numeric arg (mandatory) */
1.21      stevesk   606:                l = strtol(cp, &cp2, base);
                    607:                if (cp2 == cp || ((l == LONG_MIN || l == LONG_MAX) &&
                    608:                    errno == ERANGE) || l < 0) {
1.1       djm       609:                        error("You must supply a numeric argument "
                    610:                            "to the %s command.", cmd);
                    611:                        return(-1);
                    612:                }
1.21      stevesk   613:                cp = cp2;
                    614:                *n_arg = l;
                    615:                if (cmdnum == I_LUMASK && strchr(WHITESPACE, *cp))
                    616:                        break;
                    617:                if (cmdnum == I_LUMASK || !strchr(WHITESPACE, *cp)) {
1.1       djm       618:                        error("You must supply a numeric argument "
                    619:                            "to the %s command.", cmd);
                    620:                        return(-1);
                    621:                }
                    622:                cp += strspn(cp, WHITESPACE);
                    623:
                    624:                /* Get pathname (mandatory) */
                    625:                if (get_pathname(&cp, path1))
                    626:                        return(-1);
                    627:                if (*path1 == NULL) {
1.3       stevesk   628:                        error("You must specify a path after a %s command.",
1.1       djm       629:                            cmd);
                    630:                        return(-1);
                    631:                }
                    632:                break;
                    633:        case I_QUIT:
                    634:        case I_PWD:
                    635:        case I_LPWD:
                    636:        case I_HELP:
1.28      markus    637:        case I_VERSION:
1.1       djm       638:                break;
                    639:        default:
                    640:                fatal("Command not implemented");
                    641:        }
                    642:
                    643:        *cpp = cp;
                    644:        return(cmdnum);
                    645: }
                    646:
                    647: int
                    648: parse_dispatch_command(int in, int out, const char *cmd, char **pwd)
                    649: {
1.8       provos    650:        char *path1, *path2, *tmp;
1.27      djm       651:        int pflag, cmdnum, i;
1.1       djm       652:        unsigned long n_arg;
                    653:        Attrib a, *aa;
1.23      deraadt   654:        char path_buf[MAXPATHLEN];
1.25      deraadt   655:        int err = 0;
1.27      djm       656:        glob_t g;
1.1       djm       657:
                    658:        path1 = path2 = NULL;
                    659:        cmdnum = parse_args(&cmd, &pflag, &n_arg, &path1, &path2);
                    660:
1.29      djm       661:        memset(&g, 0, sizeof(g));
                    662:
1.1       djm       663:        /* Perform command */
                    664:        switch (cmdnum) {
                    665:        case -1:
                    666:                break;
                    667:        case I_GET:
1.29      djm       668:                err = process_get(in, out, path1, path2, *pwd, pflag);
1.1       djm       669:                break;
                    670:        case I_PUT:
1.29      djm       671:                err = process_put(in, out, path1, path2, *pwd, pflag);
1.33      markus    672:                break;
                    673:        case I_RENAME:
1.1       djm       674:                path1 = make_absolute(path1, *pwd);
                    675:                path2 = make_absolute(path2, *pwd);
1.25      deraadt   676:                err = do_rename(in, out, path1, path2);
1.1       djm       677:                break;
1.26      djm       678:        case I_SYMLINK:
                    679:                if (version < 3) {
                    680:                        error("The server (version %d) does not support "
                    681:                            "this operation", version);
                    682:                        err = -1;
                    683:                } else {
                    684:                        path2 = make_absolute(path2, *pwd);
                    685:                        err = do_symlink(in, out, path1, path2);
                    686:                }
                    687:                break;
1.1       djm       688:        case I_RM:
                    689:                path1 = make_absolute(path1, *pwd);
1.27      djm       690:                remote_glob(in, out, path1, GLOB_NOCHECK, NULL, &g);
                    691:                for(i = 0; g.gl_pathv[i]; i++) {
                    692:                        printf("Removing %s\n", g.gl_pathv[i]);
                    693:                        if (do_rm(in, out, g.gl_pathv[i]) == -1)
                    694:                                err = -1;
                    695:                }
1.1       djm       696:                break;
                    697:        case I_MKDIR:
                    698:                path1 = make_absolute(path1, *pwd);
                    699:                attrib_clear(&a);
                    700:                a.flags |= SSH2_FILEXFER_ATTR_PERMISSIONS;
                    701:                a.perm = 0777;
1.25      deraadt   702:                err = do_mkdir(in, out, path1, &a);
1.1       djm       703:                break;
                    704:        case I_RMDIR:
                    705:                path1 = make_absolute(path1, *pwd);
1.25      deraadt   706:                err = do_rmdir(in, out, path1);
1.1       djm       707:                break;
                    708:        case I_CHDIR:
                    709:                path1 = make_absolute(path1, *pwd);
1.25      deraadt   710:                if ((tmp = do_realpath(in, out, path1)) == NULL) {
                    711:                        err = 1;
1.11      markus    712:                        break;
1.25      deraadt   713:                }
1.29      djm       714:                if ((aa = do_stat(in, out, tmp, 0)) == NULL) {
1.11      markus    715:                        xfree(tmp);
1.25      deraadt   716:                        err = 1;
1.11      markus    717:                        break;
                    718:                }
1.9       djm       719:                if (!(aa->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)) {
                    720:                        error("Can't change directory: Can't check target");
                    721:                        xfree(tmp);
1.25      deraadt   722:                        err = 1;
1.9       djm       723:                        break;
                    724:                }
                    725:                if (!S_ISDIR(aa->perm)) {
                    726:                        error("Can't change directory: \"%s\" is not "
                    727:                            "a directory", tmp);
                    728:                        xfree(tmp);
1.25      deraadt   729:                        err = 1;
1.9       djm       730:                        break;
                    731:                }
1.11      markus    732:                xfree(*pwd);
                    733:                *pwd = tmp;
1.1       djm       734:                break;
                    735:        case I_LS:
1.12      djm       736:                if (!path1) {
                    737:                        do_ls(in, out, *pwd);
                    738:                        break;
                    739:                }
1.1       djm       740:                path1 = make_absolute(path1, *pwd);
1.12      djm       741:                if ((tmp = do_realpath(in, out, path1)) == NULL)
                    742:                        break;
                    743:                xfree(path1);
                    744:                path1 = tmp;
1.29      djm       745:                if ((aa = do_stat(in, out, path1, 0)) == NULL)
1.12      djm       746:                        break;
1.30      markus    747:                if ((aa->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) &&
1.12      djm       748:                    !S_ISDIR(aa->perm)) {
                    749:                        error("Can't ls: \"%s\" is not a directory", path1);
                    750:                        break;
                    751:                }
                    752:                do_ls(in, out, path1);
1.1       djm       753:                break;
                    754:        case I_LCHDIR:
1.25      deraadt   755:                if (chdir(path1) == -1) {
1.1       djm       756:                        error("Couldn't change local directory to "
                    757:                            "\"%s\": %s", path1, strerror(errno));
1.25      deraadt   758:                        err = 1;
                    759:                }
1.1       djm       760:                break;
                    761:        case I_LMKDIR:
1.25      deraadt   762:                if (mkdir(path1, 0777) == -1) {
1.17      stevesk   763:                        error("Couldn't create local directory "
1.1       djm       764:                            "\"%s\": %s", path1, strerror(errno));
1.25      deraadt   765:                        err = 1;
                    766:                }
1.1       djm       767:                break;
                    768:        case I_LLS:
                    769:                local_do_ls(cmd);
                    770:                break;
                    771:        case I_SHELL:
                    772:                local_do_shell(cmd);
                    773:                break;
                    774:        case I_LUMASK:
                    775:                umask(n_arg);
1.21      stevesk   776:                printf("Local umask: %03lo\n", n_arg);
1.1       djm       777:                break;
                    778:        case I_CHMOD:
                    779:                path1 = make_absolute(path1, *pwd);
                    780:                attrib_clear(&a);
                    781:                a.flags |= SSH2_FILEXFER_ATTR_PERMISSIONS;
                    782:                a.perm = n_arg;
1.27      djm       783:                remote_glob(in, out, path1, GLOB_NOCHECK, NULL, &g);
                    784:                for(i = 0; g.gl_pathv[i]; i++) {
                    785:                        printf("Changing mode on %s\n", g.gl_pathv[i]);
                    786:                        do_setstat(in, out, g.gl_pathv[i], &a);
                    787:                }
1.5       stevesk   788:                break;
1.1       djm       789:        case I_CHOWN:
                    790:                path1 = make_absolute(path1, *pwd);
1.27      djm       791:                remote_glob(in, out, path1, GLOB_NOCHECK, NULL, &g);
                    792:                for(i = 0; g.gl_pathv[i]; i++) {
1.29      djm       793:                        if (!(aa = do_stat(in, out, g.gl_pathv[i], 0)))
1.27      djm       794:                                continue;
                    795:                        if (!(aa->flags & SSH2_FILEXFER_ATTR_UIDGID)) {
                    796:                                error("Can't get current ownership of "
                    797:                                    "remote file \"%s\"", g.gl_pathv[i]);
                    798:                                continue;
                    799:                        }
                    800:                        printf("Changing owner on %s\n", g.gl_pathv[i]);
                    801:                        aa->flags &= SSH2_FILEXFER_ATTR_UIDGID;
                    802:                        aa->uid = n_arg;
                    803:                        do_setstat(in, out, g.gl_pathv[i], aa);
1.1       djm       804:                }
                    805:                break;
                    806:        case I_CHGRP:
                    807:                path1 = make_absolute(path1, *pwd);
1.27      djm       808:                remote_glob(in, out, path1, GLOB_NOCHECK, NULL, &g);
                    809:                for(i = 0; g.gl_pathv[i]; i++) {
1.29      djm       810:                        if (!(aa = do_stat(in, out, g.gl_pathv[i], 0)))
1.27      djm       811:                                continue;
                    812:                        if (!(aa->flags & SSH2_FILEXFER_ATTR_UIDGID)) {
                    813:                                error("Can't get current ownership of "
                    814:                                    "remote file \"%s\"", g.gl_pathv[i]);
                    815:                                continue;
                    816:                        }
                    817:                        printf("Changing group on %s\n", g.gl_pathv[i]);
                    818:                        aa->flags &= SSH2_FILEXFER_ATTR_UIDGID;
                    819:                        aa->gid = n_arg;
                    820:                        do_setstat(in, out, g.gl_pathv[i], aa);
1.1       djm       821:                }
                    822:                break;
                    823:        case I_PWD:
                    824:                printf("Remote working directory: %s\n", *pwd);
                    825:                break;
                    826:        case I_LPWD:
                    827:                if (!getcwd(path_buf, sizeof(path_buf)))
1.24      millert   828:                        error("Couldn't get local cwd: %s",
1.1       djm       829:                            strerror(errno));
                    830:                else
                    831:                        printf("Local working directory: %s\n",
                    832:                            path_buf);
                    833:                break;
                    834:        case I_QUIT:
                    835:                return(-1);
                    836:        case I_HELP:
                    837:                help();
1.28      markus    838:                break;
                    839:        case I_VERSION:
                    840:                printf("SFTP protocol version %d\n", version);
1.1       djm       841:                break;
                    842:        default:
                    843:                fatal("%d is not implemented", cmdnum);
                    844:        }
                    845:
1.29      djm       846:        if (g.gl_pathc)
                    847:                globfree(&g);
1.1       djm       848:        if (path1)
                    849:                xfree(path1);
                    850:        if (path2)
                    851:                xfree(path2);
1.25      deraadt   852:
                    853:        /* If an error occurs in batch mode we should abort. */
                    854:        if (infile != stdin && err > 0)
                    855:                return -1;
                    856:
1.1       djm       857:        return(0);
                    858: }
                    859:
                    860: void
1.35    ! mouring   861: interactive_loop(int fd_in, int fd_out, char *file1, char *file2)
1.1       djm       862: {
                    863:        char *pwd;
1.35    ! mouring   864:        char *dir = NULL;
1.1       djm       865:        char cmd[2048];
1.26      djm       866:
                    867:        version = do_init(fd_in, fd_out);
                    868:        if (version == -1)
                    869:                fatal("Couldn't initialise connection to server");
1.1       djm       870:
                    871:        pwd = do_realpath(fd_in, fd_out, ".");
                    872:        if (pwd == NULL)
                    873:                fatal("Need cwd");
                    874:
1.35    ! mouring   875:        if (file1 != NULL) {
        !           876:                dir = xstrdup(file1);
        !           877:                dir = make_absolute(dir, pwd);
        !           878:
        !           879:                if (remote_is_dir(fd_in, fd_out, dir) && file2 == NULL) {
        !           880:                        printf("Changing to: %s\n", dir);
        !           881:                        snprintf(cmd, sizeof cmd, "cd \"%s\"", dir);
        !           882:                        parse_dispatch_command(fd_in, fd_out, cmd, &pwd);
        !           883:                } else {
        !           884:                        if (file2 == NULL)
        !           885:                                snprintf(cmd, sizeof cmd, "get %s", dir);
        !           886:                        else
        !           887:                                snprintf(cmd, sizeof cmd, "get %s %s", dir,
        !           888:                                    file2);
        !           889:
        !           890:                        parse_dispatch_command(fd_in, fd_out, cmd, &pwd);
        !           891:                        return;
        !           892:                }
        !           893:        }
1.14      stevesk   894:        setvbuf(stdout, NULL, _IOLBF, 0);
1.25      deraadt   895:        setvbuf(infile, NULL, _IOLBF, 0);
1.1       djm       896:
                    897:        for(;;) {
                    898:                char *cp;
                    899:
                    900:                printf("sftp> ");
                    901:
                    902:                /* XXX: use libedit */
1.25      deraadt   903:                if (fgets(cmd, sizeof(cmd), infile) == NULL) {
1.1       djm       904:                        printf("\n");
                    905:                        break;
1.25      deraadt   906:                } else if (infile != stdin) /* Bluff typing */
                    907:                        printf("%s", cmd);
                    908:
1.1       djm       909:                cp = strrchr(cmd, '\n');
                    910:                if (cp)
                    911:                        *cp = '\0';
1.25      deraadt   912:
1.1       djm       913:                if (parse_dispatch_command(fd_in, fd_out, cmd, &pwd))
                    914:                        break;
                    915:        }
                    916:        xfree(pwd);
                    917: }