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

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