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

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