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

1.1       djm         1: /*
1.44      djm         2:  * Copyright (c) 2001,2002 Damien Miller.  All rights reserved.
1.1       djm         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.46.2.1! jason      29: RCSID("$OpenBSD: sftp-int.c,v 1.47 2002/06/23 09:30:14 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.43      djm        50: /* Number of concurrent outstanding requests */
                     51: extern int num_requests;
                     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:        }
1.46      markus    181:        while (waitpid(pid, &status, 0) == -1)
                    182:                if (errno != EINTR)
                    183:                        fatal("Couldn't wait for child: %s", strerror(errno));
1.1       djm       184:        if (!WIFEXITED(status))
                    185:                error("Shell exited abormally");
                    186:        else if (WEXITSTATUS(status))
                    187:                error("Shell exited with status %d", WEXITSTATUS(status));
                    188: }
                    189:
1.37      itojun    190: static void
1.1       djm       191: local_do_ls(const char *args)
                    192: {
                    193:        if (!args || !*args)
1.18      stevesk   194:                local_do_shell(_PATH_LS);
1.1       djm       195:        else {
1.18      stevesk   196:                int len = strlen(_PATH_LS " ") + strlen(args) + 1;
1.16      deraadt   197:                char *buf = xmalloc(len);
1.1       djm       198:
                    199:                /* XXX: quoting - rip quoting code from ftp? */
1.18      stevesk   200:                snprintf(buf, len, _PATH_LS " %s", args);
1.1       djm       201:                local_do_shell(buf);
1.16      deraadt   202:                xfree(buf);
1.1       djm       203:        }
                    204: }
                    205:
1.37      itojun    206: static char *
1.29      djm       207: path_append(char *p1, char *p2)
                    208: {
                    209:        char *ret;
1.31      markus    210:        int len = strlen(p1) + strlen(p2) + 2;
1.29      djm       211:
1.31      markus    212:        ret = xmalloc(len);
                    213:        strlcpy(ret, p1, len);
1.41      deraadt   214:        if (strcmp(p1, "/") != 0)
1.39      jakob     215:                strlcat(ret, "/", len);
1.31      markus    216:        strlcat(ret, p2, len);
1.29      djm       217:
                    218:        return(ret);
                    219: }
                    220:
1.37      itojun    221: static char *
1.1       djm       222: make_absolute(char *p, char *pwd)
                    223: {
1.29      djm       224:        char *abs;
1.1       djm       225:
                    226:        /* Derelativise */
                    227:        if (p && p[0] != '/') {
1.29      djm       228:                abs = path_append(pwd, p);
1.1       djm       229:                xfree(p);
1.29      djm       230:                return(abs);
                    231:        } else
                    232:                return(p);
                    233: }
                    234:
1.37      itojun    235: static int
1.29      djm       236: infer_path(const char *p, char **ifp)
                    237: {
                    238:        char *cp;
                    239:
                    240:        cp = strrchr(p, '/');
                    241:        if (cp == NULL) {
                    242:                *ifp = xstrdup(p);
                    243:                return(0);
1.1       djm       244:        }
                    245:
1.29      djm       246:        if (!cp[1]) {
                    247:                error("Invalid path");
                    248:                return(-1);
                    249:        }
                    250:
                    251:        *ifp = xstrdup(cp + 1);
                    252:        return(0);
1.1       djm       253: }
                    254:
1.37      itojun    255: static int
1.1       djm       256: parse_getput_flags(const char **cpp, int *pflag)
                    257: {
                    258:        const char *cp = *cpp;
                    259:
                    260:        /* Check for flags */
                    261:        if (cp[0] == '-' && cp[1] && strchr(WHITESPACE, cp[2])) {
1.20      djm       262:                switch (cp[1]) {
1.22      djm       263:                case 'p':
1.1       djm       264:                case 'P':
                    265:                        *pflag = 1;
                    266:                        break;
                    267:                default:
1.22      djm       268:                        error("Invalid flag -%c", cp[1]);
1.1       djm       269:                        return(-1);
                    270:                }
                    271:                cp += 2;
                    272:                *cpp = cp + strspn(cp, WHITESPACE);
                    273:        }
                    274:
                    275:        return(0);
                    276: }
                    277:
1.37      itojun    278: static int
1.1       djm       279: get_pathname(const char **cpp, char **path)
                    280: {
1.8       provos    281:        const char *cp = *cpp, *end;
                    282:        char quot;
1.1       djm       283:        int i;
                    284:
                    285:        cp += strspn(cp, WHITESPACE);
                    286:        if (!*cp) {
                    287:                *cpp = cp;
                    288:                *path = NULL;
1.8       provos    289:                return (0);
1.1       djm       290:        }
                    291:
                    292:        /* Check for quoted filenames */
                    293:        if (*cp == '\"' || *cp == '\'') {
1.8       provos    294:                quot = *cp++;
1.30      markus    295:
1.8       provos    296:                end = strchr(cp, quot);
                    297:                if (end == NULL) {
1.1       djm       298:                        error("Unterminated quote");
1.8       provos    299:                        goto fail;
1.1       djm       300:                }
1.8       provos    301:                if (cp == end) {
1.1       djm       302:                        error("Empty quotes");
1.8       provos    303:                        goto fail;
1.1       djm       304:                }
1.8       provos    305:                *cpp = end + 1 + strspn(end + 1, WHITESPACE);
                    306:        } else {
                    307:                /* Read to end of filename */
                    308:                end = strpbrk(cp, WHITESPACE);
                    309:                if (end == NULL)
                    310:                        end = strchr(cp, '\0');
                    311:                *cpp = end + strspn(end, WHITESPACE);
1.1       djm       312:        }
                    313:
1.8       provos    314:        i = end - cp;
1.1       djm       315:
                    316:        *path = xmalloc(i + 1);
                    317:        memcpy(*path, cp, i);
                    318:        (*path)[i] = '\0';
                    319:        return(0);
1.8       provos    320:
                    321:  fail:
                    322:        *path = NULL;
                    323:        return (-1);
1.1       djm       324: }
                    325:
1.37      itojun    326: static int
1.29      djm       327: is_dir(char *path)
                    328: {
                    329:        struct stat sb;
                    330:
                    331:        /* XXX: report errors? */
                    332:        if (stat(path, &sb) == -1)
                    333:                return(0);
                    334:
                    335:        return(sb.st_mode & S_IFDIR);
                    336: }
                    337:
1.37      itojun    338: static int
1.44      djm       339: remote_is_dir(struct sftp_conn *conn, char *path)
1.1       djm       340: {
1.29      djm       341:        Attrib *a;
1.1       djm       342:
1.29      djm       343:        /* XXX: report errors? */
1.44      djm       344:        if ((a = do_stat(conn, path, 1)) == NULL)
1.29      djm       345:                return(0);
                    346:        if (!(a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS))
1.1       djm       347:                return(0);
1.29      djm       348:        return(a->perm & S_IFDIR);
                    349: }
                    350:
1.37      itojun    351: static int
1.44      djm       352: process_get(struct sftp_conn *conn, char *src, char *dst, char *pwd, int pflag)
1.29      djm       353: {
                    354:        char *abs_src = NULL;
                    355:        char *abs_dst = NULL;
                    356:        char *tmp;
                    357:        glob_t g;
                    358:        int err = 0;
                    359:        int i;
1.30      markus    360:
1.29      djm       361:        abs_src = xstrdup(src);
                    362:        abs_src = make_absolute(abs_src, pwd);
                    363:
1.30      markus    364:        memset(&g, 0, sizeof(g));
1.29      djm       365:        debug3("Looking up %s", abs_src);
1.44      djm       366:        if (remote_glob(conn, abs_src, 0, NULL, &g)) {
1.29      djm       367:                error("File \"%s\" not found.", abs_src);
                    368:                err = -1;
                    369:                goto out;
                    370:        }
                    371:
                    372:        /* Only one match, dst may be file, directory or unspecified */
                    373:        if (g.gl_pathv[0] && g.gl_matchc == 1) {
                    374:                if (dst) {
                    375:                        /* If directory specified, append filename */
                    376:                        if (is_dir(dst)) {
                    377:                                if (infer_path(g.gl_pathv[0], &tmp)) {
                    378:                                        err = 1;
                    379:                                        goto out;
                    380:                                }
                    381:                                abs_dst = path_append(dst, tmp);
                    382:                                xfree(tmp);
                    383:                        } else
                    384:                                abs_dst = xstrdup(dst);
                    385:                } else if (infer_path(g.gl_pathv[0], &abs_dst)) {
                    386:                        err = -1;
                    387:                        goto out;
                    388:                }
                    389:                printf("Fetching %s to %s\n", g.gl_pathv[0], abs_dst);
1.44      djm       390:                err = do_download(conn, g.gl_pathv[0], abs_dst, pflag);
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.44      djm       414:                if (do_download(conn, g.gl_pathv[i], abs_dst, pflag) == -1)
1.29      djm       415:                        err = -1;
                    416:                xfree(abs_dst);
                    417:                abs_dst = NULL;
1.1       djm       418:        }
                    419:
1.29      djm       420: out:
                    421:        xfree(abs_src);
                    422:        if (abs_dst)
                    423:                xfree(abs_dst);
                    424:        globfree(&g);
                    425:        return(err);
                    426: }
                    427:
1.37      itojun    428: static int
1.44      djm       429: process_put(struct sftp_conn *conn, char *src, char *dst, char *pwd, int pflag)
1.29      djm       430: {
                    431:        char *tmp_dst = NULL;
                    432:        char *abs_dst = NULL;
                    433:        char *tmp;
                    434:        glob_t g;
                    435:        int err = 0;
                    436:        int i;
                    437:
                    438:        if (dst) {
                    439:                tmp_dst = xstrdup(dst);
                    440:                tmp_dst = make_absolute(tmp_dst, pwd);
                    441:        }
                    442:
1.30      markus    443:        memset(&g, 0, sizeof(g));
1.29      djm       444:        debug3("Looking up %s", src);
                    445:        if (glob(src, 0, NULL, &g)) {
                    446:                error("File \"%s\" not found.", src);
                    447:                err = -1;
                    448:                goto out;
                    449:        }
                    450:
                    451:        /* Only one match, dst may be file, directory or unspecified */
                    452:        if (g.gl_pathv[0] && g.gl_matchc == 1) {
                    453:                if (tmp_dst) {
                    454:                        /* If directory specified, append filename */
1.44      djm       455:                        if (remote_is_dir(conn, tmp_dst)) {
1.29      djm       456:                                if (infer_path(g.gl_pathv[0], &tmp)) {
                    457:                                        err = 1;
                    458:                                        goto out;
                    459:                                }
                    460:                                abs_dst = path_append(tmp_dst, tmp);
                    461:                                xfree(tmp);
                    462:                        } else
                    463:                                abs_dst = xstrdup(tmp_dst);
1.32      markus    464:                } else {
                    465:                        if (infer_path(g.gl_pathv[0], &abs_dst)) {
                    466:                                err = -1;
                    467:                                goto out;
                    468:                        }
                    469:                        abs_dst = make_absolute(abs_dst, pwd);
1.29      djm       470:                }
                    471:                printf("Uploading %s to %s\n", g.gl_pathv[0], abs_dst);
1.44      djm       472:                err = do_upload(conn, g.gl_pathv[0], abs_dst, pflag);
1.29      djm       473:                goto out;
                    474:        }
                    475:
                    476:        /* Multiple matches, dst may be directory or unspecified */
1.44      djm       477:        if (tmp_dst && !remote_is_dir(conn, tmp_dst)) {
1.30      markus    478:                error("Multiple files match, but \"%s\" is not a directory",
1.29      djm       479:                    tmp_dst);
                    480:                err = -1;
                    481:                goto out;
                    482:        }
                    483:
1.41      deraadt   484:        for (i = 0; g.gl_pathv[i]; i++) {
1.29      djm       485:                if (infer_path(g.gl_pathv[i], &tmp)) {
                    486:                        err = -1;
                    487:                        goto out;
                    488:                }
                    489:                if (tmp_dst) {
                    490:                        abs_dst = path_append(tmp_dst, tmp);
                    491:                        xfree(tmp);
                    492:                } else
                    493:                        abs_dst = make_absolute(tmp, pwd);
                    494:
                    495:                printf("Uploading %s to %s\n", g.gl_pathv[i], abs_dst);
1.44      djm       496:                if (do_upload(conn, g.gl_pathv[i], abs_dst, pflag) == -1)
1.29      djm       497:                        err = -1;
1.1       djm       498:        }
                    499:
1.29      djm       500: out:
                    501:        if (abs_dst)
                    502:                xfree(abs_dst);
                    503:        if (tmp_dst)
                    504:                xfree(tmp_dst);
                    505:        return(err);
1.1       djm       506: }
                    507:
1.37      itojun    508: static int
1.1       djm       509: parse_args(const char **cpp, int *pflag, unsigned long *n_arg,
                    510:     char **path1, char **path2)
                    511: {
                    512:        const char *cmd, *cp = *cpp;
1.21      stevesk   513:        char *cp2;
1.4       markus    514:        int base = 0;
1.21      stevesk   515:        long l;
1.1       djm       516:        int i, cmdnum;
                    517:
                    518:        /* Skip leading whitespace */
                    519:        cp = cp + strspn(cp, WHITESPACE);
                    520:
                    521:        /* Ignore blank lines */
                    522:        if (!*cp)
                    523:                return(-1);
                    524:
                    525:        /* Figure out which command we have */
1.41      deraadt   526:        for (i = 0; cmds[i].c; i++) {
1.1       djm       527:                int cmdlen = strlen(cmds[i].c);
                    528:
                    529:                /* Check for command followed by whitespace */
                    530:                if (!strncasecmp(cp, cmds[i].c, cmdlen) &&
                    531:                    strchr(WHITESPACE, cp[cmdlen])) {
                    532:                        cp += cmdlen;
                    533:                        cp = cp + strspn(cp, WHITESPACE);
                    534:                        break;
                    535:                }
                    536:        }
                    537:        cmdnum = cmds[i].n;
                    538:        cmd = cmds[i].c;
                    539:
                    540:        /* Special case */
                    541:        if (*cp == '!') {
                    542:                cp++;
                    543:                cmdnum = I_SHELL;
                    544:        } else if (cmdnum == -1) {
                    545:                error("Invalid command.");
                    546:                return(-1);
                    547:        }
                    548:
                    549:        /* Get arguments and parse flags */
                    550:        *pflag = *n_arg = 0;
                    551:        *path1 = *path2 = NULL;
                    552:        switch (cmdnum) {
                    553:        case I_GET:
                    554:        case I_PUT:
                    555:                if (parse_getput_flags(&cp, pflag))
                    556:                        return(-1);
                    557:                /* Get first pathname (mandatory) */
                    558:                if (get_pathname(&cp, path1))
                    559:                        return(-1);
                    560:                if (*path1 == NULL) {
                    561:                        error("You must specify at least one path after a "
                    562:                            "%s command.", cmd);
                    563:                        return(-1);
                    564:                }
                    565:                /* Try to get second pathname (optional) */
                    566:                if (get_pathname(&cp, path2))
                    567:                        return(-1);
                    568:                break;
                    569:        case I_RENAME:
1.26      djm       570:        case I_SYMLINK:
1.1       djm       571:                if (get_pathname(&cp, path1))
                    572:                        return(-1);
                    573:                if (get_pathname(&cp, path2))
                    574:                        return(-1);
                    575:                if (!*path1 || !*path2) {
                    576:                        error("You must specify two paths after a %s "
                    577:                            "command.", cmd);
                    578:                        return(-1);
                    579:                }
                    580:                break;
                    581:        case I_RM:
                    582:        case I_MKDIR:
                    583:        case I_RMDIR:
                    584:        case I_CHDIR:
                    585:        case I_LCHDIR:
                    586:        case I_LMKDIR:
                    587:                /* Get pathname (mandatory) */
                    588:                if (get_pathname(&cp, path1))
                    589:                        return(-1);
                    590:                if (*path1 == NULL) {
1.3       stevesk   591:                        error("You must specify a path after a %s command.",
1.1       djm       592:                            cmd);
                    593:                        return(-1);
                    594:                }
                    595:                break;
                    596:        case I_LS:
                    597:                /* Path is optional */
                    598:                if (get_pathname(&cp, path1))
                    599:                        return(-1);
                    600:                break;
                    601:        case I_LLS:
                    602:        case I_SHELL:
                    603:                /* Uses the rest of the line */
                    604:                break;
                    605:        case I_LUMASK:
1.21      stevesk   606:                base = 8;
1.1       djm       607:        case I_CHMOD:
1.4       markus    608:                base = 8;
1.1       djm       609:        case I_CHOWN:
                    610:        case I_CHGRP:
                    611:                /* Get numeric arg (mandatory) */
1.21      stevesk   612:                l = strtol(cp, &cp2, base);
                    613:                if (cp2 == cp || ((l == LONG_MIN || l == LONG_MAX) &&
                    614:                    errno == ERANGE) || l < 0) {
1.1       djm       615:                        error("You must supply a numeric argument "
                    616:                            "to the %s command.", cmd);
                    617:                        return(-1);
                    618:                }
1.21      stevesk   619:                cp = cp2;
                    620:                *n_arg = l;
                    621:                if (cmdnum == I_LUMASK && strchr(WHITESPACE, *cp))
                    622:                        break;
                    623:                if (cmdnum == I_LUMASK || !strchr(WHITESPACE, *cp)) {
1.1       djm       624:                        error("You must supply a numeric argument "
                    625:                            "to the %s command.", cmd);
                    626:                        return(-1);
                    627:                }
                    628:                cp += strspn(cp, WHITESPACE);
                    629:
                    630:                /* Get pathname (mandatory) */
                    631:                if (get_pathname(&cp, path1))
                    632:                        return(-1);
                    633:                if (*path1 == NULL) {
1.3       stevesk   634:                        error("You must specify a path after a %s command.",
1.1       djm       635:                            cmd);
                    636:                        return(-1);
                    637:                }
                    638:                break;
                    639:        case I_QUIT:
                    640:        case I_PWD:
                    641:        case I_LPWD:
                    642:        case I_HELP:
1.28      markus    643:        case I_VERSION:
1.1       djm       644:                break;
                    645:        default:
                    646:                fatal("Command not implemented");
                    647:        }
                    648:
                    649:        *cpp = cp;
                    650:        return(cmdnum);
                    651: }
                    652:
1.37      itojun    653: static int
1.44      djm       654: parse_dispatch_command(struct sftp_conn *conn, const char *cmd, char **pwd)
1.1       djm       655: {
1.8       provos    656:        char *path1, *path2, *tmp;
1.27      djm       657:        int pflag, cmdnum, i;
1.1       djm       658:        unsigned long n_arg;
                    659:        Attrib a, *aa;
1.23      deraadt   660:        char path_buf[MAXPATHLEN];
1.25      deraadt   661:        int err = 0;
1.27      djm       662:        glob_t g;
1.1       djm       663:
                    664:        path1 = path2 = NULL;
                    665:        cmdnum = parse_args(&cmd, &pflag, &n_arg, &path1, &path2);
                    666:
1.29      djm       667:        memset(&g, 0, sizeof(g));
                    668:
1.1       djm       669:        /* Perform command */
                    670:        switch (cmdnum) {
                    671:        case -1:
                    672:                break;
                    673:        case I_GET:
1.44      djm       674:                err = process_get(conn, path1, path2, *pwd, pflag);
1.1       djm       675:                break;
                    676:        case I_PUT:
1.44      djm       677:                err = process_put(conn, path1, path2, *pwd, pflag);
1.33      markus    678:                break;
                    679:        case I_RENAME:
1.1       djm       680:                path1 = make_absolute(path1, *pwd);
                    681:                path2 = make_absolute(path2, *pwd);
1.44      djm       682:                err = do_rename(conn, path1, path2);
1.1       djm       683:                break;
1.26      djm       684:        case I_SYMLINK:
1.44      djm       685:                path2 = make_absolute(path2, *pwd);
                    686:                err = do_symlink(conn, path1, path2);
1.26      djm       687:                break;
1.1       djm       688:        case I_RM:
                    689:                path1 = make_absolute(path1, *pwd);
1.44      djm       690:                remote_glob(conn, path1, GLOB_NOCHECK, NULL, &g);
1.41      deraadt   691:                for (i = 0; g.gl_pathv[i]; i++) {
1.27      djm       692:                        printf("Removing %s\n", g.gl_pathv[i]);
1.44      djm       693:                        if (do_rm(conn, g.gl_pathv[i]) == -1)
1.27      djm       694:                                err = -1;
                    695:                }
1.1       djm       696:                break;
                    697:        case I_MKDIR:
                    698:                path1 = make_absolute(path1, *pwd);
                    699:                attrib_clear(&a);
                    700:                a.flags |= SSH2_FILEXFER_ATTR_PERMISSIONS;
                    701:                a.perm = 0777;
1.44      djm       702:                err = do_mkdir(conn, path1, &a);
1.1       djm       703:                break;
                    704:        case I_RMDIR:
                    705:                path1 = make_absolute(path1, *pwd);
1.44      djm       706:                err = do_rmdir(conn, path1);
1.1       djm       707:                break;
                    708:        case I_CHDIR:
                    709:                path1 = make_absolute(path1, *pwd);
1.44      djm       710:                if ((tmp = do_realpath(conn, path1)) == NULL) {
1.25      deraadt   711:                        err = 1;
1.11      markus    712:                        break;
1.25      deraadt   713:                }
1.44      djm       714:                if ((aa = do_stat(conn, tmp, 0)) == NULL) {
1.11      markus    715:                        xfree(tmp);
1.25      deraadt   716:                        err = 1;
1.11      markus    717:                        break;
                    718:                }
1.9       djm       719:                if (!(aa->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)) {
                    720:                        error("Can't change directory: Can't check target");
                    721:                        xfree(tmp);
1.25      deraadt   722:                        err = 1;
1.9       djm       723:                        break;
                    724:                }
                    725:                if (!S_ISDIR(aa->perm)) {
                    726:                        error("Can't change directory: \"%s\" is not "
                    727:                            "a directory", tmp);
                    728:                        xfree(tmp);
1.25      deraadt   729:                        err = 1;
1.9       djm       730:                        break;
                    731:                }
1.11      markus    732:                xfree(*pwd);
                    733:                *pwd = tmp;
1.1       djm       734:                break;
                    735:        case I_LS:
1.12      djm       736:                if (!path1) {
1.44      djm       737:                        do_ls(conn, *pwd);
1.12      djm       738:                        break;
                    739:                }
1.1       djm       740:                path1 = make_absolute(path1, *pwd);
1.44      djm       741:                if ((tmp = do_realpath(conn, path1)) == NULL)
1.12      djm       742:                        break;
                    743:                xfree(path1);
                    744:                path1 = tmp;
1.44      djm       745:                if ((aa = do_stat(conn, path1, 0)) == NULL)
1.12      djm       746:                        break;
1.30      markus    747:                if ((aa->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) &&
1.12      djm       748:                    !S_ISDIR(aa->perm)) {
                    749:                        error("Can't ls: \"%s\" is not a directory", path1);
                    750:                        break;
                    751:                }
1.44      djm       752:                do_ls(conn, path1);
1.1       djm       753:                break;
                    754:        case I_LCHDIR:
1.25      deraadt   755:                if (chdir(path1) == -1) {
1.1       djm       756:                        error("Couldn't change local directory to "
                    757:                            "\"%s\": %s", path1, strerror(errno));
1.25      deraadt   758:                        err = 1;
                    759:                }
1.1       djm       760:                break;
                    761:        case I_LMKDIR:
1.25      deraadt   762:                if (mkdir(path1, 0777) == -1) {
1.17      stevesk   763:                        error("Couldn't create local directory "
1.1       djm       764:                            "\"%s\": %s", path1, strerror(errno));
1.25      deraadt   765:                        err = 1;
                    766:                }
1.1       djm       767:                break;
                    768:        case I_LLS:
                    769:                local_do_ls(cmd);
                    770:                break;
                    771:        case I_SHELL:
                    772:                local_do_shell(cmd);
                    773:                break;
                    774:        case I_LUMASK:
                    775:                umask(n_arg);
1.21      stevesk   776:                printf("Local umask: %03lo\n", n_arg);
1.1       djm       777:                break;
                    778:        case I_CHMOD:
                    779:                path1 = make_absolute(path1, *pwd);
                    780:                attrib_clear(&a);
                    781:                a.flags |= SSH2_FILEXFER_ATTR_PERMISSIONS;
                    782:                a.perm = n_arg;
1.44      djm       783:                remote_glob(conn, path1, GLOB_NOCHECK, NULL, &g);
1.41      deraadt   784:                for (i = 0; g.gl_pathv[i]; i++) {
1.27      djm       785:                        printf("Changing mode on %s\n", g.gl_pathv[i]);
1.44      djm       786:                        do_setstat(conn, g.gl_pathv[i], &a);
1.27      djm       787:                }
1.5       stevesk   788:                break;
1.1       djm       789:        case I_CHOWN:
                    790:                path1 = make_absolute(path1, *pwd);
1.44      djm       791:                remote_glob(conn, path1, GLOB_NOCHECK, NULL, &g);
1.41      deraadt   792:                for (i = 0; g.gl_pathv[i]; i++) {
1.44      djm       793:                        if (!(aa = do_stat(conn, g.gl_pathv[i], 0)))
1.27      djm       794:                                continue;
                    795:                        if (!(aa->flags & SSH2_FILEXFER_ATTR_UIDGID)) {
                    796:                                error("Can't get current ownership of "
                    797:                                    "remote file \"%s\"", g.gl_pathv[i]);
                    798:                                continue;
                    799:                        }
                    800:                        printf("Changing owner on %s\n", g.gl_pathv[i]);
                    801:                        aa->flags &= SSH2_FILEXFER_ATTR_UIDGID;
                    802:                        aa->uid = n_arg;
1.44      djm       803:                        do_setstat(conn, g.gl_pathv[i], aa);
1.1       djm       804:                }
                    805:                break;
                    806:        case I_CHGRP:
                    807:                path1 = make_absolute(path1, *pwd);
1.44      djm       808:                remote_glob(conn, path1, GLOB_NOCHECK, NULL, &g);
1.41      deraadt   809:                for (i = 0; g.gl_pathv[i]; i++) {
1.44      djm       810:                        if (!(aa = do_stat(conn, g.gl_pathv[i], 0)))
1.27      djm       811:                                continue;
                    812:                        if (!(aa->flags & SSH2_FILEXFER_ATTR_UIDGID)) {
                    813:                                error("Can't get current ownership of "
                    814:                                    "remote file \"%s\"", g.gl_pathv[i]);
                    815:                                continue;
                    816:                        }
                    817:                        printf("Changing group on %s\n", g.gl_pathv[i]);
                    818:                        aa->flags &= SSH2_FILEXFER_ATTR_UIDGID;
                    819:                        aa->gid = n_arg;
1.44      djm       820:                        do_setstat(conn, g.gl_pathv[i], aa);
1.1       djm       821:                }
                    822:                break;
                    823:        case I_PWD:
                    824:                printf("Remote working directory: %s\n", *pwd);
                    825:                break;
                    826:        case I_LPWD:
                    827:                if (!getcwd(path_buf, sizeof(path_buf)))
1.24      millert   828:                        error("Couldn't get local cwd: %s",
1.1       djm       829:                            strerror(errno));
                    830:                else
                    831:                        printf("Local working directory: %s\n",
                    832:                            path_buf);
                    833:                break;
                    834:        case I_QUIT:
                    835:                return(-1);
                    836:        case I_HELP:
                    837:                help();
1.28      markus    838:                break;
                    839:        case I_VERSION:
1.46.2.1! jason     840:                printf("SFTP protocol version %u\n", sftp_proto_version(conn));
1.1       djm       841:                break;
                    842:        default:
                    843:                fatal("%d is not implemented", cmdnum);
                    844:        }
                    845:
1.29      djm       846:        if (g.gl_pathc)
                    847:                globfree(&g);
1.1       djm       848:        if (path1)
                    849:                xfree(path1);
                    850:        if (path2)
                    851:                xfree(path2);
1.25      deraadt   852:
                    853:        /* If an error occurs in batch mode we should abort. */
                    854:        if (infile != stdin && err > 0)
                    855:                return -1;
                    856:
1.1       djm       857:        return(0);
                    858: }
                    859:
                    860: void
1.35      mouring   861: interactive_loop(int fd_in, int fd_out, char *file1, char *file2)
1.1       djm       862: {
                    863:        char *pwd;
1.35      mouring   864:        char *dir = NULL;
1.1       djm       865:        char cmd[2048];
1.44      djm       866:        struct sftp_conn *conn;
1.26      djm       867:
1.44      djm       868:        conn = do_init(fd_in, fd_out, copy_buffer_len, num_requests);
                    869:        if (conn == NULL)
1.26      djm       870:                fatal("Couldn't initialise connection to server");
1.1       djm       871:
1.44      djm       872:        pwd = do_realpath(conn, ".");
1.1       djm       873:        if (pwd == NULL)
                    874:                fatal("Need cwd");
                    875:
1.35      mouring   876:        if (file1 != NULL) {
                    877:                dir = xstrdup(file1);
                    878:                dir = make_absolute(dir, pwd);
                    879:
1.44      djm       880:                if (remote_is_dir(conn, dir) && file2 == NULL) {
1.35      mouring   881:                        printf("Changing to: %s\n", dir);
                    882:                        snprintf(cmd, sizeof cmd, "cd \"%s\"", dir);
1.44      djm       883:                        parse_dispatch_command(conn, cmd, &pwd);
1.35      mouring   884:                } else {
                    885:                        if (file2 == NULL)
                    886:                                snprintf(cmd, sizeof cmd, "get %s", dir);
                    887:                        else
                    888:                                snprintf(cmd, sizeof cmd, "get %s %s", dir,
                    889:                                    file2);
                    890:
1.44      djm       891:                        parse_dispatch_command(conn, cmd, &pwd);
1.45      mpech     892:                        xfree(dir);
1.35      mouring   893:                        return;
                    894:                }
1.45      mpech     895:                xfree(dir);
1.35      mouring   896:        }
1.14      stevesk   897:        setvbuf(stdout, NULL, _IOLBF, 0);
1.25      deraadt   898:        setvbuf(infile, NULL, _IOLBF, 0);
1.1       djm       899:
1.41      deraadt   900:        for (;;) {
1.1       djm       901:                char *cp;
                    902:
                    903:                printf("sftp> ");
                    904:
                    905:                /* XXX: use libedit */
1.25      deraadt   906:                if (fgets(cmd, sizeof(cmd), infile) == NULL) {
1.1       djm       907:                        printf("\n");
                    908:                        break;
1.25      deraadt   909:                } else if (infile != stdin) /* Bluff typing */
                    910:                        printf("%s", cmd);
                    911:
1.1       djm       912:                cp = strrchr(cmd, '\n');
                    913:                if (cp)
                    914:                        *cp = '\0';
1.25      deraadt   915:
1.44      djm       916:                if (parse_dispatch_command(conn, cmd, &pwd))
1.1       djm       917:                        break;
                    918:        }
                    919:        xfree(pwd);
                    920: }