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

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.28    ! markus     29: RCSID("$OpenBSD: sftp-int.c,v 1.27 2001/03/13 22:42:54 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 },
                     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 *
                    200: make_absolute(char *p, char *pwd)
                    201: {
                    202:        char buf[2048];
                    203:
                    204:        /* Derelativise */
                    205:        if (p && p[0] != '/') {
                    206:                snprintf(buf, sizeof(buf), "%s/%s", pwd, p);
                    207:                xfree(p);
                    208:                p = xstrdup(buf);
                    209:        }
                    210:
                    211:        return(p);
                    212: }
                    213:
                    214: int
                    215: parse_getput_flags(const char **cpp, int *pflag)
                    216: {
                    217:        const char *cp = *cpp;
                    218:
                    219:        /* Check for flags */
                    220:        if (cp[0] == '-' && cp[1] && strchr(WHITESPACE, cp[2])) {
1.20      djm       221:                switch (cp[1]) {
1.22      djm       222:                case 'p':
1.1       djm       223:                case 'P':
                    224:                        *pflag = 1;
                    225:                        break;
                    226:                default:
1.22      djm       227:                        error("Invalid flag -%c", cp[1]);
1.1       djm       228:                        return(-1);
                    229:                }
                    230:                cp += 2;
                    231:                *cpp = cp + strspn(cp, WHITESPACE);
                    232:        }
                    233:
                    234:        return(0);
                    235: }
                    236:
                    237: int
                    238: get_pathname(const char **cpp, char **path)
                    239: {
1.8       provos    240:        const char *cp = *cpp, *end;
                    241:        char quot;
1.1       djm       242:        int i;
                    243:
                    244:        cp += strspn(cp, WHITESPACE);
                    245:        if (!*cp) {
                    246:                *cpp = cp;
                    247:                *path = NULL;
1.8       provos    248:                return (0);
1.1       djm       249:        }
                    250:
                    251:        /* Check for quoted filenames */
                    252:        if (*cp == '\"' || *cp == '\'') {
1.8       provos    253:                quot = *cp++;
                    254:
                    255:                end = strchr(cp, quot);
                    256:                if (end == NULL) {
1.1       djm       257:                        error("Unterminated quote");
1.8       provos    258:                        goto fail;
1.1       djm       259:                }
1.8       provos    260:                if (cp == end) {
1.1       djm       261:                        error("Empty quotes");
1.8       provos    262:                        goto fail;
1.1       djm       263:                }
1.8       provos    264:                *cpp = end + 1 + strspn(end + 1, WHITESPACE);
                    265:        } else {
                    266:                /* Read to end of filename */
                    267:                end = strpbrk(cp, WHITESPACE);
                    268:                if (end == NULL)
                    269:                        end = strchr(cp, '\0');
                    270:                *cpp = end + strspn(end, WHITESPACE);
1.1       djm       271:        }
                    272:
1.8       provos    273:        i = end - cp;
1.1       djm       274:
                    275:        *path = xmalloc(i + 1);
                    276:        memcpy(*path, cp, i);
                    277:        (*path)[i] = '\0';
                    278:        return(0);
1.8       provos    279:
                    280:  fail:
                    281:        *path = NULL;
                    282:        return (-1);
1.1       djm       283: }
                    284:
                    285: int
                    286: infer_path(const char *p, char **ifp)
                    287: {
                    288:        char *cp;
                    289:
                    290:        cp = strrchr(p, '/');
                    291:        if (cp == NULL) {
                    292:                *ifp = xstrdup(p);
                    293:                return(0);
                    294:        }
                    295:
                    296:        if (!cp[1]) {
                    297:                error("Invalid path");
                    298:                return(-1);
                    299:        }
                    300:
                    301:        *ifp = xstrdup(cp + 1);
                    302:        return(0);
                    303: }
                    304:
                    305: int
                    306: parse_args(const char **cpp, int *pflag, unsigned long *n_arg,
                    307:     char **path1, char **path2)
                    308: {
                    309:        const char *cmd, *cp = *cpp;
1.21      stevesk   310:        char *cp2;
1.4       markus    311:        int base = 0;
1.21      stevesk   312:        long l;
1.1       djm       313:        int i, cmdnum;
                    314:
                    315:        /* Skip leading whitespace */
                    316:        cp = cp + strspn(cp, WHITESPACE);
                    317:
                    318:        /* Ignore blank lines */
                    319:        if (!*cp)
                    320:                return(-1);
                    321:
                    322:        /* Figure out which command we have */
                    323:        for(i = 0; cmds[i].c; i++) {
                    324:                int cmdlen = strlen(cmds[i].c);
                    325:
                    326:                /* Check for command followed by whitespace */
                    327:                if (!strncasecmp(cp, cmds[i].c, cmdlen) &&
                    328:                    strchr(WHITESPACE, cp[cmdlen])) {
                    329:                        cp += cmdlen;
                    330:                        cp = cp + strspn(cp, WHITESPACE);
                    331:                        break;
                    332:                }
                    333:        }
                    334:        cmdnum = cmds[i].n;
                    335:        cmd = cmds[i].c;
                    336:
                    337:        /* Special case */
                    338:        if (*cp == '!') {
                    339:                cp++;
                    340:                cmdnum = I_SHELL;
                    341:        } else if (cmdnum == -1) {
                    342:                error("Invalid command.");
                    343:                return(-1);
                    344:        }
                    345:
                    346:        /* Get arguments and parse flags */
                    347:        *pflag = *n_arg = 0;
                    348:        *path1 = *path2 = NULL;
                    349:        switch (cmdnum) {
                    350:        case I_GET:
                    351:        case I_PUT:
                    352:                if (parse_getput_flags(&cp, pflag))
                    353:                        return(-1);
                    354:                /* Get first pathname (mandatory) */
                    355:                if (get_pathname(&cp, path1))
                    356:                        return(-1);
                    357:                if (*path1 == NULL) {
                    358:                        error("You must specify at least one path after a "
                    359:                            "%s command.", cmd);
                    360:                        return(-1);
                    361:                }
                    362:                /* Try to get second pathname (optional) */
                    363:                if (get_pathname(&cp, path2))
                    364:                        return(-1);
                    365:                break;
                    366:        case I_RENAME:
1.26      djm       367:        case I_SYMLINK:
1.1       djm       368:                if (get_pathname(&cp, path1))
                    369:                        return(-1);
                    370:                if (get_pathname(&cp, path2))
                    371:                        return(-1);
                    372:                if (!*path1 || !*path2) {
                    373:                        error("You must specify two paths after a %s "
                    374:                            "command.", cmd);
                    375:                        return(-1);
                    376:                }
                    377:                break;
                    378:        case I_RM:
                    379:        case I_MKDIR:
                    380:        case I_RMDIR:
                    381:        case I_CHDIR:
                    382:        case I_LCHDIR:
                    383:        case I_LMKDIR:
                    384:                /* Get pathname (mandatory) */
                    385:                if (get_pathname(&cp, path1))
                    386:                        return(-1);
                    387:                if (*path1 == NULL) {
1.3       stevesk   388:                        error("You must specify a path after a %s command.",
1.1       djm       389:                            cmd);
                    390:                        return(-1);
                    391:                }
                    392:                break;
                    393:        case I_LS:
                    394:                /* Path is optional */
                    395:                if (get_pathname(&cp, path1))
                    396:                        return(-1);
                    397:                break;
                    398:        case I_LLS:
                    399:        case I_SHELL:
                    400:                /* Uses the rest of the line */
                    401:                break;
                    402:        case I_LUMASK:
1.21      stevesk   403:                base = 8;
1.1       djm       404:        case I_CHMOD:
1.4       markus    405:                base = 8;
1.1       djm       406:        case I_CHOWN:
                    407:        case I_CHGRP:
                    408:                /* Get numeric arg (mandatory) */
1.21      stevesk   409:                l = strtol(cp, &cp2, base);
                    410:                if (cp2 == cp || ((l == LONG_MIN || l == LONG_MAX) &&
                    411:                    errno == ERANGE) || l < 0) {
1.1       djm       412:                        error("You must supply a numeric argument "
                    413:                            "to the %s command.", cmd);
                    414:                        return(-1);
                    415:                }
1.21      stevesk   416:                cp = cp2;
                    417:                *n_arg = l;
                    418:                if (cmdnum == I_LUMASK && strchr(WHITESPACE, *cp))
                    419:                        break;
                    420:                if (cmdnum == I_LUMASK || !strchr(WHITESPACE, *cp)) {
1.1       djm       421:                        error("You must supply a numeric argument "
                    422:                            "to the %s command.", cmd);
                    423:                        return(-1);
                    424:                }
                    425:                cp += strspn(cp, WHITESPACE);
                    426:
                    427:                /* Get pathname (mandatory) */
                    428:                if (get_pathname(&cp, path1))
                    429:                        return(-1);
                    430:                if (*path1 == NULL) {
1.3       stevesk   431:                        error("You must specify a path after a %s command.",
1.1       djm       432:                            cmd);
                    433:                        return(-1);
                    434:                }
                    435:                break;
                    436:        case I_QUIT:
                    437:        case I_PWD:
                    438:        case I_LPWD:
                    439:        case I_HELP:
1.28    ! markus    440:        case I_VERSION:
1.1       djm       441:                break;
                    442:        default:
                    443:                fatal("Command not implemented");
                    444:        }
                    445:
                    446:        *cpp = cp;
                    447:        return(cmdnum);
                    448: }
                    449:
                    450: int
                    451: parse_dispatch_command(int in, int out, const char *cmd, char **pwd)
                    452: {
1.8       provos    453:        char *path1, *path2, *tmp;
1.27      djm       454:        int pflag, cmdnum, i;
1.1       djm       455:        unsigned long n_arg;
                    456:        Attrib a, *aa;
1.23      deraadt   457:        char path_buf[MAXPATHLEN];
1.25      deraadt   458:        int err = 0;
1.27      djm       459:        glob_t g;
1.1       djm       460:
                    461:        path1 = path2 = NULL;
                    462:        cmdnum = parse_args(&cmd, &pflag, &n_arg, &path1, &path2);
                    463:
                    464:        /* Perform command */
                    465:        switch (cmdnum) {
                    466:        case -1:
                    467:                break;
                    468:        case I_GET:
1.27      djm       469:                memset(&g, 0, sizeof(g));
                    470:                if (!remote_glob(in, out, path1, 0, NULL, &g)) {
                    471:                        if (path2) {
                    472:                                /* XXX: target should be directory */
                    473:                                error("You cannot specify a target when "
                    474:                                    "downloading multiple files");
                    475:                                err = -1;
                    476:                                break;
                    477:                        }
                    478:                        for(i = 0; g.gl_pathv[i]; i++) {
                    479:                                if (!infer_path(g.gl_pathv[i], &path2)) {
                    480:                                        printf("Fetching %s\n", g.gl_pathv[i]);
                    481:                                        if (do_download(in, out, g.gl_pathv[i],
                    482:                                            path2, pflag) == -1)
                    483:                                                err = -1;
                    484:                                        free(path2);
                    485:                                        path2 = NULL;
                    486:                                } else
                    487:                                        err = -1;
                    488:                        }
                    489:                } else {
                    490:                        if (!path2 && infer_path(path1, &path2)) {
                    491:                                err = -1;
                    492:                                break;
                    493:                        }
                    494:                        err = do_download(in, out, path1, path2, pflag);
                    495:                }
1.1       djm       496:                break;
                    497:        case I_PUT:
1.27      djm       498:                if (!glob(path1, 0, NULL, &g)) {
                    499:                        if (path2) {
                    500:                                error("You cannot specify a target when "
                    501:                                    "uploading multiple files");
                    502:                                err = -1;
                    503:                                break;
                    504:                        }
                    505:                        for(i = 0; g.gl_pathv[i]; i++) {
                    506:                                if (!infer_path(g.gl_pathv[i], &path2)) {
                    507:                                        path2 = make_absolute(path2, *pwd);
                    508:                                        printf("Uploading %s\n", g.gl_pathv[i]);
                    509:                                        if (do_upload(in, out, g.gl_pathv[i],
                    510:                                            path2, pflag) == -1)
                    511:                                                err = -1;
                    512:                                        free(path2);
                    513:                                        path2 = NULL;
                    514:                                } else
                    515:                                        err = -1;
                    516:                        }
                    517:                } else {
                    518:                        if (!path2 && infer_path(path1, &path2)) {
                    519:                                err = -1;
                    520:                                break;
                    521:                        }
                    522:                        err = do_upload(in, out, path1, path2, pflag);
                    523:                }
                    524:                break;
                    525:        case I_RENAME:
1.1       djm       526:                path1 = make_absolute(path1, *pwd);
                    527:                path2 = make_absolute(path2, *pwd);
1.25      deraadt   528:                err = do_rename(in, out, path1, path2);
1.1       djm       529:                break;
1.26      djm       530:        case I_SYMLINK:
                    531:                if (version < 3) {
                    532:                        error("The server (version %d) does not support "
                    533:                            "this operation", version);
                    534:                        err = -1;
                    535:                } else {
                    536:                        path2 = make_absolute(path2, *pwd);
                    537:                        err = do_symlink(in, out, path1, path2);
                    538:                }
                    539:                break;
1.1       djm       540:        case I_RM:
                    541:                path1 = make_absolute(path1, *pwd);
1.27      djm       542:                remote_glob(in, out, path1, GLOB_NOCHECK, NULL, &g);
                    543:                for(i = 0; g.gl_pathv[i]; i++) {
                    544:                        printf("Removing %s\n", g.gl_pathv[i]);
                    545:                        if (do_rm(in, out, g.gl_pathv[i]) == -1)
                    546:                                err = -1;
                    547:                }
1.1       djm       548:                break;
                    549:        case I_MKDIR:
                    550:                path1 = make_absolute(path1, *pwd);
                    551:                attrib_clear(&a);
                    552:                a.flags |= SSH2_FILEXFER_ATTR_PERMISSIONS;
                    553:                a.perm = 0777;
1.25      deraadt   554:                err = do_mkdir(in, out, path1, &a);
1.1       djm       555:                break;
                    556:        case I_RMDIR:
                    557:                path1 = make_absolute(path1, *pwd);
1.25      deraadt   558:                err = do_rmdir(in, out, path1);
1.1       djm       559:                break;
                    560:        case I_CHDIR:
                    561:                path1 = make_absolute(path1, *pwd);
1.25      deraadt   562:                if ((tmp = do_realpath(in, out, path1)) == NULL) {
                    563:                        err = 1;
1.11      markus    564:                        break;
1.25      deraadt   565:                }
1.11      markus    566:                if ((aa = do_stat(in, out, tmp)) == NULL) {
                    567:                        xfree(tmp);
1.25      deraadt   568:                        err = 1;
1.11      markus    569:                        break;
                    570:                }
1.9       djm       571:                if (!(aa->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)) {
                    572:                        error("Can't change directory: Can't check target");
                    573:                        xfree(tmp);
1.25      deraadt   574:                        err = 1;
1.9       djm       575:                        break;
                    576:                }
                    577:                if (!S_ISDIR(aa->perm)) {
                    578:                        error("Can't change directory: \"%s\" is not "
                    579:                            "a directory", tmp);
                    580:                        xfree(tmp);
1.25      deraadt   581:                        err = 1;
1.9       djm       582:                        break;
                    583:                }
1.11      markus    584:                xfree(*pwd);
                    585:                *pwd = tmp;
1.1       djm       586:                break;
                    587:        case I_LS:
1.12      djm       588:                if (!path1) {
                    589:                        do_ls(in, out, *pwd);
                    590:                        break;
                    591:                }
1.1       djm       592:                path1 = make_absolute(path1, *pwd);
1.12      djm       593:                if ((tmp = do_realpath(in, out, path1)) == NULL)
                    594:                        break;
                    595:                xfree(path1);
                    596:                path1 = tmp;
                    597:                if ((aa = do_stat(in, out, path1)) == NULL)
                    598:                        break;
                    599:                if ((aa->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) &&
                    600:                    !S_ISDIR(aa->perm)) {
                    601:                        error("Can't ls: \"%s\" is not a directory", path1);
                    602:                        break;
                    603:                }
                    604:                do_ls(in, out, path1);
1.1       djm       605:                break;
                    606:        case I_LCHDIR:
1.25      deraadt   607:                if (chdir(path1) == -1) {
1.1       djm       608:                        error("Couldn't change local directory to "
                    609:                            "\"%s\": %s", path1, strerror(errno));
1.25      deraadt   610:                        err = 1;
                    611:                }
1.1       djm       612:                break;
                    613:        case I_LMKDIR:
1.25      deraadt   614:                if (mkdir(path1, 0777) == -1) {
1.17      stevesk   615:                        error("Couldn't create local directory "
1.1       djm       616:                            "\"%s\": %s", path1, strerror(errno));
1.25      deraadt   617:                        err = 1;
                    618:                }
1.1       djm       619:                break;
                    620:        case I_LLS:
                    621:                local_do_ls(cmd);
                    622:                break;
                    623:        case I_SHELL:
                    624:                local_do_shell(cmd);
                    625:                break;
                    626:        case I_LUMASK:
                    627:                umask(n_arg);
1.21      stevesk   628:                printf("Local umask: %03lo\n", n_arg);
1.1       djm       629:                break;
                    630:        case I_CHMOD:
                    631:                path1 = make_absolute(path1, *pwd);
                    632:                attrib_clear(&a);
                    633:                a.flags |= SSH2_FILEXFER_ATTR_PERMISSIONS;
                    634:                a.perm = n_arg;
1.27      djm       635:                remote_glob(in, out, path1, GLOB_NOCHECK, NULL, &g);
                    636:                for(i = 0; g.gl_pathv[i]; i++) {
                    637:                        printf("Changing mode on %s\n", g.gl_pathv[i]);
                    638:                        do_setstat(in, out, g.gl_pathv[i], &a);
                    639:                }
1.5       stevesk   640:                break;
1.1       djm       641:        case I_CHOWN:
                    642:                path1 = make_absolute(path1, *pwd);
1.27      djm       643:                remote_glob(in, out, path1, GLOB_NOCHECK, NULL, &g);
                    644:                for(i = 0; g.gl_pathv[i]; i++) {
                    645:                        if (!(aa = do_stat(in, out, g.gl_pathv[i])))
                    646:                                continue;
                    647:                        if (!(aa->flags & SSH2_FILEXFER_ATTR_UIDGID)) {
                    648:                                error("Can't get current ownership of "
                    649:                                    "remote file \"%s\"", g.gl_pathv[i]);
                    650:                                continue;
                    651:                        }
                    652:                        printf("Changing owner on %s\n", g.gl_pathv[i]);
                    653:                        aa->flags &= SSH2_FILEXFER_ATTR_UIDGID;
                    654:                        aa->uid = n_arg;
                    655:                        do_setstat(in, out, g.gl_pathv[i], aa);
1.1       djm       656:                }
                    657:                break;
                    658:        case I_CHGRP:
                    659:                path1 = make_absolute(path1, *pwd);
1.27      djm       660:                remote_glob(in, out, path1, GLOB_NOCHECK, NULL, &g);
                    661:                for(i = 0; g.gl_pathv[i]; i++) {
                    662:                        if (!(aa = do_stat(in, out, g.gl_pathv[i])))
                    663:                                continue;
                    664:                        if (!(aa->flags & SSH2_FILEXFER_ATTR_UIDGID)) {
                    665:                                error("Can't get current ownership of "
                    666:                                    "remote file \"%s\"", g.gl_pathv[i]);
                    667:                                continue;
                    668:                        }
                    669:                        printf("Changing group on %s\n", g.gl_pathv[i]);
                    670:                        aa->flags &= SSH2_FILEXFER_ATTR_UIDGID;
                    671:                        aa->gid = n_arg;
                    672:                        do_setstat(in, out, g.gl_pathv[i], aa);
1.1       djm       673:                }
                    674:                break;
                    675:        case I_PWD:
                    676:                printf("Remote working directory: %s\n", *pwd);
                    677:                break;
                    678:        case I_LPWD:
                    679:                if (!getcwd(path_buf, sizeof(path_buf)))
1.24      millert   680:                        error("Couldn't get local cwd: %s",
1.1       djm       681:                            strerror(errno));
                    682:                else
                    683:                        printf("Local working directory: %s\n",
                    684:                            path_buf);
                    685:                break;
                    686:        case I_QUIT:
                    687:                return(-1);
                    688:        case I_HELP:
                    689:                help();
1.28    ! markus    690:                break;
        !           691:        case I_VERSION:
        !           692:                printf("SFTP protocol version %d\n", version);
1.1       djm       693:                break;
                    694:        default:
                    695:                fatal("%d is not implemented", cmdnum);
                    696:        }
                    697:
                    698:        if (path1)
                    699:                xfree(path1);
                    700:        if (path2)
                    701:                xfree(path2);
1.25      deraadt   702:
                    703:        /* If an error occurs in batch mode we should abort. */
                    704:        if (infile != stdin && err > 0)
                    705:                return -1;
                    706:
1.1       djm       707:        return(0);
                    708: }
                    709:
                    710: void
                    711: interactive_loop(int fd_in, int fd_out)
                    712: {
                    713:        char *pwd;
                    714:        char cmd[2048];
1.26      djm       715:
                    716:        version = do_init(fd_in, fd_out);
                    717:        if (version == -1)
                    718:                fatal("Couldn't initialise connection to server");
1.1       djm       719:
                    720:        pwd = do_realpath(fd_in, fd_out, ".");
                    721:        if (pwd == NULL)
                    722:                fatal("Need cwd");
                    723:
1.14      stevesk   724:        setvbuf(stdout, NULL, _IOLBF, 0);
1.25      deraadt   725:        setvbuf(infile, NULL, _IOLBF, 0);
1.1       djm       726:
                    727:        for(;;) {
                    728:                char *cp;
                    729:
                    730:                printf("sftp> ");
                    731:
                    732:                /* XXX: use libedit */
1.25      deraadt   733:                if (fgets(cmd, sizeof(cmd), infile) == NULL) {
1.1       djm       734:                        printf("\n");
                    735:                        break;
1.25      deraadt   736:                } else if (infile != stdin) /* Bluff typing */
                    737:                        printf("%s", cmd);
                    738:
1.1       djm       739:                cp = strrchr(cmd, '\n');
                    740:                if (cp)
                    741:                        *cp = '\0';
1.25      deraadt   742:
1.1       djm       743:                if (parse_dispatch_command(fd_in, fd_out, cmd, &pwd))
                    744:                        break;
                    745:        }
                    746:        xfree(pwd);
                    747: }