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

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