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

1.1       djm         1: /*
1.68    ! djm         2:  * Copyright (c) 2001-2004 Damien Miller <djm@openbsd.org>
1.1       djm         3:  *
1.68    ! djm         4:  * Permission to use, copy, modify, and distribute this software for any
        !             5:  * purpose with or without fee is hereby granted, provided that the above
        !             6:  * copyright notice and this permission notice appear in all copies.
1.1       djm         7:  *
1.68    ! djm         8:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
        !             9:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
        !            10:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
        !            11:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
        !            12:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
        !            13:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
        !            14:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1.1       djm        15:  */
                     16:
                     17: /* XXX: recursive operations */
                     18:
                     19: #include "includes.h"
1.68    ! djm        20: RCSID("$OpenBSD: sftp-int.c,v 1.67 2004/01/23 17:57:48 mouring Exp $");
1.27      djm        21:
                     22: #include <glob.h>
1.1       djm        23:
                     24: #include "buffer.h"
                     25: #include "xmalloc.h"
                     26: #include "log.h"
                     27: #include "pathnames.h"
                     28:
                     29: #include "sftp.h"
                     30: #include "sftp-common.h"
1.27      djm        31: #include "sftp-glob.h"
1.1       djm        32: #include "sftp-client.h"
                     33: #include "sftp-int.h"
                     34:
1.26      djm        35: /* File to read commands from */
                     36: extern FILE *infile;
                     37:
1.66      djm        38: /* Are we in batchfile mode? */
                     39: extern int batchmode;
                     40:
1.42      djm        41: /* Size of buffer used when copying files */
                     42: extern size_t copy_buffer_len;
                     43:
1.43      djm        44: /* Number of concurrent outstanding requests */
                     45: extern int num_requests;
                     46:
1.52      fgsch      47: /* This is set to 0 if the progressmeter is not desired. */
                     48: int showprogress = 1;
                     49:
1.64      jmc        50: /* Separators for interactive commands */
1.1       djm        51: #define WHITESPACE " \t\r\n"
                     52:
1.60      mouring    53: /* Define what type of ls view (0 - multi-column) */
                     54: #define LONG_VIEW 1            /* Full view ala ls -l */
                     55: #define SHORT_VIEW 2           /* Single row view ala ls -1 */
                     56:
1.1       djm        57: /* Commands for interactive mode */
                     58: #define I_CHDIR                1
                     59: #define I_CHGRP                2
                     60: #define I_CHMOD                3
                     61: #define I_CHOWN                4
                     62: #define I_GET          5
                     63: #define I_HELP         6
                     64: #define I_LCHDIR       7
                     65: #define I_LLS          8
                     66: #define I_LMKDIR       9
                     67: #define I_LPWD         10
                     68: #define I_LS           11
                     69: #define I_LUMASK       12
                     70: #define I_MKDIR                13
                     71: #define I_PUT          14
                     72: #define I_PWD          15
                     73: #define I_QUIT         16
                     74: #define I_RENAME       17
                     75: #define I_RM           18
                     76: #define I_RMDIR                19
                     77: #define I_SHELL                20
1.26      djm        78: #define I_SYMLINK      21
1.28      markus     79: #define I_VERSION      22
1.52      fgsch      80: #define I_PROGRESS     23
1.1       djm        81:
                     82: struct CMD {
1.6       deraadt    83:        const char *c;
1.1       djm        84:        const int n;
                     85: };
                     86:
1.54      djm        87: static const struct CMD cmds[] = {
1.40      markus     88:        { "bye",        I_QUIT },
1.15      stevesk    89:        { "cd",         I_CHDIR },
                     90:        { "chdir",      I_CHDIR },
                     91:        { "chgrp",      I_CHGRP },
                     92:        { "chmod",      I_CHMOD },
                     93:        { "chown",      I_CHOWN },
                     94:        { "dir",        I_LS },
                     95:        { "exit",       I_QUIT },
                     96:        { "get",        I_GET },
1.34      djm        97:        { "mget",       I_GET },
1.15      stevesk    98:        { "help",       I_HELP },
                     99:        { "lcd",        I_LCHDIR },
                    100:        { "lchdir",     I_LCHDIR },
                    101:        { "lls",        I_LLS },
                    102:        { "lmkdir",     I_LMKDIR },
1.26      djm       103:        { "ln",         I_SYMLINK },
1.15      stevesk   104:        { "lpwd",       I_LPWD },
                    105:        { "ls",         I_LS },
                    106:        { "lumask",     I_LUMASK },
                    107:        { "mkdir",      I_MKDIR },
1.52      fgsch     108:        { "progress",   I_PROGRESS },
1.15      stevesk   109:        { "put",        I_PUT },
1.34      djm       110:        { "mput",       I_PUT },
1.15      stevesk   111:        { "pwd",        I_PWD },
                    112:        { "quit",       I_QUIT },
                    113:        { "rename",     I_RENAME },
                    114:        { "rm",         I_RM },
                    115:        { "rmdir",      I_RMDIR },
1.26      djm       116:        { "symlink",    I_SYMLINK },
1.28      markus    117:        { "version",    I_VERSION },
1.6       deraadt   118:        { "!",          I_SHELL },
1.7       deraadt   119:        { "?",          I_HELP },
1.6       deraadt   120:        { NULL,                 -1}
1.1       djm       121: };
                    122:
1.37      itojun    123: static void
1.1       djm       124: help(void)
                    125: {
                    126:        printf("Available commands:\n");
1.13      stevesk   127:        printf("cd path                       Change remote directory to 'path'\n");
                    128:        printf("lcd path                      Change local directory to 'path'\n");
                    129:        printf("chgrp grp path                Change group of file 'path' to 'grp'\n");
                    130:        printf("chmod mode path               Change permissions of file 'path' to 'mode'\n");
                    131:        printf("chown own path                Change owner of file 'path' to 'own'\n");
                    132:        printf("help                          Display this help text\n");
                    133:        printf("get remote-path [local-path]  Download file\n");
                    134:        printf("lls [ls-options [path]]       Display local directory listing\n");
1.26      djm       135:        printf("ln oldpath newpath            Symlink remote file\n");
1.13      stevesk   136:        printf("lmkdir path                   Create local directory\n");
                    137:        printf("lpwd                          Print local working directory\n");
                    138:        printf("ls [path]                     Display remote directory listing\n");
                    139:        printf("lumask umask                  Set local umask to 'umask'\n");
                    140:        printf("mkdir path                    Create remote directory\n");
1.53      fgsch     141:        printf("progress                      Toggle display of progress meter\n");
1.13      stevesk   142:        printf("put local-path [remote-path]  Upload file\n");
                    143:        printf("pwd                           Display remote working directory\n");
                    144:        printf("exit                          Quit sftp\n");
                    145:        printf("quit                          Quit sftp\n");
                    146:        printf("rename oldpath newpath        Rename remote file\n");
                    147:        printf("rmdir path                    Remove remote directory\n");
                    148:        printf("rm path                       Delete remote file\n");
1.26      djm       149:        printf("symlink oldpath newpath       Symlink remote file\n");
1.28      markus    150:        printf("version                       Show SFTP version\n");
1.1       djm       151:        printf("!command                      Execute 'command' in local shell\n");
                    152:        printf("!                             Escape to local shell\n");
1.13      stevesk   153:        printf("?                             Synonym for help\n");
1.1       djm       154: }
                    155:
1.37      itojun    156: static void
1.1       djm       157: local_do_shell(const char *args)
                    158: {
1.36      markus    159:        int status;
1.1       djm       160:        char *shell;
                    161:        pid_t pid;
1.3       stevesk   162:
1.1       djm       163:        if (!*args)
                    164:                args = NULL;
1.3       stevesk   165:
1.1       djm       166:        if ((shell = getenv("SHELL")) == NULL)
                    167:                shell = _PATH_BSHELL;
                    168:
                    169:        if ((pid = fork()) == -1)
                    170:                fatal("Couldn't fork: %s", strerror(errno));
                    171:
                    172:        if (pid == 0) {
                    173:                /* XXX: child has pipe fds to ssh subproc open - issue? */
                    174:                if (args) {
                    175:                        debug3("Executing %s -c \"%s\"", shell, args);
1.38      deraadt   176:                        execl(shell, shell, "-c", args, (char *)NULL);
1.1       djm       177:                } else {
                    178:                        debug3("Executing %s", shell);
1.38      deraadt   179:                        execl(shell, shell, (char *)NULL);
1.1       djm       180:                }
1.3       stevesk   181:                fprintf(stderr, "Couldn't execute \"%s\": %s\n", shell,
1.1       djm       182:                    strerror(errno));
                    183:                _exit(1);
                    184:        }
1.46      markus    185:        while (waitpid(pid, &status, 0) == -1)
                    186:                if (errno != EINTR)
                    187:                        fatal("Couldn't wait for child: %s", strerror(errno));
1.1       djm       188:        if (!WIFEXITED(status))
                    189:                error("Shell exited abormally");
                    190:        else if (WEXITSTATUS(status))
                    191:                error("Shell exited with status %d", WEXITSTATUS(status));
                    192: }
                    193:
1.37      itojun    194: static void
1.1       djm       195: local_do_ls(const char *args)
                    196: {
                    197:        if (!args || !*args)
1.18      stevesk   198:                local_do_shell(_PATH_LS);
1.1       djm       199:        else {
1.18      stevesk   200:                int len = strlen(_PATH_LS " ") + strlen(args) + 1;
1.16      deraadt   201:                char *buf = xmalloc(len);
1.1       djm       202:
                    203:                /* XXX: quoting - rip quoting code from ftp? */
1.18      stevesk   204:                snprintf(buf, len, _PATH_LS " %s", args);
1.1       djm       205:                local_do_shell(buf);
1.16      deraadt   206:                xfree(buf);
1.1       djm       207:        }
                    208: }
                    209:
1.48      djm       210: /* Strip one path (usually the pwd) from the start of another */
                    211: static char *
                    212: path_strip(char *path, char *strip)
                    213: {
                    214:        size_t len;
                    215:
                    216:        if (strip == NULL)
                    217:                return (xstrdup(path));
                    218:
                    219:        len = strlen(strip);
                    220:        if (strip != NULL && strncmp(path, strip, len) == 0) {
                    221:                if (strip[len - 1] != '/' && path[len] == '/')
                    222:                        len++;
                    223:                return (xstrdup(path + len));
                    224:        }
                    225:
                    226:        return (xstrdup(path));
                    227: }
                    228:
1.37      itojun    229: static char *
1.29      djm       230: path_append(char *p1, char *p2)
                    231: {
                    232:        char *ret;
1.31      markus    233:        int len = strlen(p1) + strlen(p2) + 2;
1.29      djm       234:
1.31      markus    235:        ret = xmalloc(len);
                    236:        strlcpy(ret, p1, len);
1.48      djm       237:        if (p1[strlen(p1) - 1] != '/')
1.39      jakob     238:                strlcat(ret, "/", len);
1.31      markus    239:        strlcat(ret, p2, len);
1.29      djm       240:
                    241:        return(ret);
                    242: }
                    243:
1.37      itojun    244: static char *
1.1       djm       245: make_absolute(char *p, char *pwd)
                    246: {
1.29      djm       247:        char *abs;
1.1       djm       248:
                    249:        /* Derelativise */
                    250:        if (p && p[0] != '/') {
1.29      djm       251:                abs = path_append(pwd, p);
1.1       djm       252:                xfree(p);
1.29      djm       253:                return(abs);
                    254:        } else
                    255:                return(p);
                    256: }
                    257:
1.37      itojun    258: static int
1.29      djm       259: infer_path(const char *p, char **ifp)
                    260: {
                    261:        char *cp;
                    262:
                    263:        cp = strrchr(p, '/');
                    264:        if (cp == NULL) {
                    265:                *ifp = xstrdup(p);
                    266:                return(0);
1.1       djm       267:        }
                    268:
1.29      djm       269:        if (!cp[1]) {
                    270:                error("Invalid path");
                    271:                return(-1);
                    272:        }
                    273:
                    274:        *ifp = xstrdup(cp + 1);
                    275:        return(0);
1.1       djm       276: }
                    277:
1.37      itojun    278: static int
1.1       djm       279: parse_getput_flags(const char **cpp, int *pflag)
                    280: {
                    281:        const char *cp = *cpp;
                    282:
                    283:        /* Check for flags */
                    284:        if (cp[0] == '-' && cp[1] && strchr(WHITESPACE, cp[2])) {
1.20      djm       285:                switch (cp[1]) {
1.22      djm       286:                case 'p':
1.1       djm       287:                case 'P':
                    288:                        *pflag = 1;
                    289:                        break;
                    290:                default:
1.22      djm       291:                        error("Invalid flag -%c", cp[1]);
1.1       djm       292:                        return(-1);
                    293:                }
                    294:                cp += 2;
                    295:                *cpp = cp + strspn(cp, WHITESPACE);
                    296:        }
                    297:
                    298:        return(0);
                    299: }
                    300:
1.37      itojun    301: static int
1.48      djm       302: parse_ls_flags(const char **cpp, int *lflag)
                    303: {
                    304:        const char *cp = *cpp;
                    305:
                    306:        /* Check for flags */
                    307:        if (cp++[0] == '-') {
                    308:                for(; strchr(WHITESPACE, *cp) == NULL; cp++) {
                    309:                        switch (*cp) {
                    310:                        case 'l':
1.60      mouring   311:                                *lflag = LONG_VIEW;
                    312:                                break;
                    313:                        case '1':
                    314:                                *lflag = SHORT_VIEW;
1.48      djm       315:                                break;
                    316:                        default:
                    317:                                error("Invalid flag -%c", *cp);
                    318:                                return(-1);
                    319:                        }
                    320:                }
                    321:                *cpp = cp + strspn(cp, WHITESPACE);
                    322:        }
                    323:
                    324:        return(0);
                    325: }
                    326:
                    327: static int
1.1       djm       328: get_pathname(const char **cpp, char **path)
                    329: {
1.8       provos    330:        const char *cp = *cpp, *end;
                    331:        char quot;
1.61      djm       332:        int i, j;
1.1       djm       333:
                    334:        cp += strspn(cp, WHITESPACE);
                    335:        if (!*cp) {
                    336:                *cpp = cp;
                    337:                *path = NULL;
1.8       provos    338:                return (0);
1.1       djm       339:        }
                    340:
1.61      djm       341:        *path = xmalloc(strlen(cp) + 1);
                    342:
1.1       djm       343:        /* Check for quoted filenames */
                    344:        if (*cp == '\"' || *cp == '\'') {
1.8       provos    345:                quot = *cp++;
1.30      markus    346:
1.61      djm       347:                /* Search for terminating quote, unescape some chars */
                    348:                for (i = j = 0; i <= strlen(cp); i++) {
                    349:                        if (cp[i] == quot) {    /* Found quote */
1.63      djm       350:                                i++;
1.61      djm       351:                                (*path)[j] = '\0';
                    352:                                break;
                    353:                        }
                    354:                        if (cp[i] == '\0') {    /* End of string */
                    355:                                error("Unterminated quote");
                    356:                                goto fail;
                    357:                        }
                    358:                        if (cp[i] == '\\') {    /* Escaped characters */
                    359:                                i++;
1.65      djm       360:                                if (cp[i] != '\'' && cp[i] != '\"' &&
1.61      djm       361:                                    cp[i] != '\\') {
                    362:                                        error("Bad escaped character '\%c'",
                    363:                                            cp[i]);
                    364:                                        goto fail;
                    365:                                }
                    366:                        }
                    367:                        (*path)[j++] = cp[i];
1.1       djm       368:                }
1.61      djm       369:
                    370:                if (j == 0) {
1.1       djm       371:                        error("Empty quotes");
1.8       provos    372:                        goto fail;
1.1       djm       373:                }
1.61      djm       374:                *cpp = cp + i + strspn(cp + i, WHITESPACE);
1.8       provos    375:        } else {
                    376:                /* Read to end of filename */
                    377:                end = strpbrk(cp, WHITESPACE);
                    378:                if (end == NULL)
                    379:                        end = strchr(cp, '\0');
                    380:                *cpp = end + strspn(end, WHITESPACE);
1.61      djm       381:
                    382:                memcpy(*path, cp, end - cp);
                    383:                (*path)[end - cp] = '\0';
1.1       djm       384:        }
1.61      djm       385:        return (0);
1.8       provos    386:
                    387:  fail:
1.65      djm       388:        xfree(*path);
                    389:        *path = NULL;
1.8       provos    390:        return (-1);
1.1       djm       391: }
                    392:
1.37      itojun    393: static int
1.29      djm       394: is_dir(char *path)
                    395: {
                    396:        struct stat sb;
                    397:
                    398:        /* XXX: report errors? */
                    399:        if (stat(path, &sb) == -1)
                    400:                return(0);
                    401:
                    402:        return(sb.st_mode & S_IFDIR);
                    403: }
                    404:
1.37      itojun    405: static int
1.55      djm       406: is_reg(char *path)
                    407: {
                    408:        struct stat sb;
                    409:
                    410:        if (stat(path, &sb) == -1)
                    411:                fatal("stat %s: %s", path, strerror(errno));
                    412:
                    413:        return(S_ISREG(sb.st_mode));
                    414: }
                    415:
                    416: static int
1.44      djm       417: remote_is_dir(struct sftp_conn *conn, char *path)
1.1       djm       418: {
1.29      djm       419:        Attrib *a;
1.1       djm       420:
1.29      djm       421:        /* XXX: report errors? */
1.44      djm       422:        if ((a = do_stat(conn, path, 1)) == NULL)
1.29      djm       423:                return(0);
                    424:        if (!(a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS))
1.1       djm       425:                return(0);
1.29      djm       426:        return(a->perm & S_IFDIR);
                    427: }
                    428:
1.37      itojun    429: static int
1.44      djm       430: process_get(struct sftp_conn *conn, char *src, char *dst, char *pwd, int pflag)
1.29      djm       431: {
                    432:        char *abs_src = NULL;
                    433:        char *abs_dst = NULL;
                    434:        char *tmp;
                    435:        glob_t g;
                    436:        int err = 0;
                    437:        int i;
1.30      markus    438:
1.29      djm       439:        abs_src = xstrdup(src);
                    440:        abs_src = make_absolute(abs_src, pwd);
                    441:
1.30      markus    442:        memset(&g, 0, sizeof(g));
1.29      djm       443:        debug3("Looking up %s", abs_src);
1.44      djm       444:        if (remote_glob(conn, abs_src, 0, NULL, &g)) {
1.29      djm       445:                error("File \"%s\" not found.", abs_src);
                    446:                err = -1;
                    447:                goto out;
                    448:        }
                    449:
1.59      mouring   450:        /* If multiple matches, dst must be a directory or unspecified */
                    451:        if (g.gl_matchc > 1 && dst && !is_dir(dst)) {
1.30      markus    452:                error("Multiple files match, but \"%s\" is not a directory",
1.29      djm       453:                    dst);
                    454:                err = -1;
                    455:                goto out;
                    456:        }
1.30      markus    457:
1.41      deraadt   458:        for (i = 0; g.gl_pathv[i]; i++) {
1.29      djm       459:                if (infer_path(g.gl_pathv[i], &tmp)) {
                    460:                        err = -1;
                    461:                        goto out;
                    462:                }
1.59      mouring   463:
                    464:                if (g.gl_matchc == 1 && dst) {
                    465:                        /* If directory specified, append filename */
                    466:                        if (is_dir(dst)) {
                    467:                                if (infer_path(g.gl_pathv[0], &tmp)) {
                    468:                                        err = 1;
                    469:                                        goto out;
                    470:                                }
                    471:                                abs_dst = path_append(dst, tmp);
                    472:                                xfree(tmp);
                    473:                        } else
                    474:                                abs_dst = xstrdup(dst);
                    475:                } else if (dst) {
1.29      djm       476:                        abs_dst = path_append(dst, tmp);
                    477:                        xfree(tmp);
                    478:                } else
                    479:                        abs_dst = tmp;
                    480:
                    481:                printf("Fetching %s to %s\n", g.gl_pathv[i], abs_dst);
1.44      djm       482:                if (do_download(conn, g.gl_pathv[i], abs_dst, pflag) == -1)
1.29      djm       483:                        err = -1;
                    484:                xfree(abs_dst);
                    485:                abs_dst = NULL;
1.1       djm       486:        }
                    487:
1.29      djm       488: out:
                    489:        xfree(abs_src);
                    490:        if (abs_dst)
                    491:                xfree(abs_dst);
                    492:        globfree(&g);
                    493:        return(err);
                    494: }
                    495:
1.37      itojun    496: static int
1.44      djm       497: process_put(struct sftp_conn *conn, char *src, char *dst, char *pwd, int pflag)
1.29      djm       498: {
                    499:        char *tmp_dst = NULL;
                    500:        char *abs_dst = NULL;
                    501:        char *tmp;
                    502:        glob_t g;
                    503:        int err = 0;
                    504:        int i;
                    505:
                    506:        if (dst) {
                    507:                tmp_dst = xstrdup(dst);
                    508:                tmp_dst = make_absolute(tmp_dst, pwd);
                    509:        }
                    510:
1.30      markus    511:        memset(&g, 0, sizeof(g));
1.29      djm       512:        debug3("Looking up %s", src);
                    513:        if (glob(src, 0, NULL, &g)) {
                    514:                error("File \"%s\" not found.", src);
                    515:                err = -1;
                    516:                goto out;
                    517:        }
                    518:
1.59      mouring   519:        /* If multiple matches, dst may be directory or unspecified */
                    520:        if (g.gl_matchc > 1 && tmp_dst && !remote_is_dir(conn, tmp_dst)) {
1.30      markus    521:                error("Multiple files match, but \"%s\" is not a directory",
1.29      djm       522:                    tmp_dst);
                    523:                err = -1;
                    524:                goto out;
                    525:        }
                    526:
1.41      deraadt   527:        for (i = 0; g.gl_pathv[i]; i++) {
1.55      djm       528:                if (!is_reg(g.gl_pathv[i])) {
1.65      djm       529:                        error("skipping non-regular file %s",
1.55      djm       530:                            g.gl_pathv[i]);
                    531:                        continue;
                    532:                }
1.29      djm       533:                if (infer_path(g.gl_pathv[i], &tmp)) {
                    534:                        err = -1;
                    535:                        goto out;
                    536:                }
1.59      mouring   537:
                    538:                if (g.gl_matchc == 1 && tmp_dst) {
                    539:                        /* If directory specified, append filename */
                    540:                        if (remote_is_dir(conn, tmp_dst)) {
                    541:                                if (infer_path(g.gl_pathv[0], &tmp)) {
                    542:                                        err = 1;
                    543:                                        goto out;
                    544:                                }
                    545:                                abs_dst = path_append(tmp_dst, tmp);
                    546:                                xfree(tmp);
                    547:                        } else
                    548:                                abs_dst = xstrdup(tmp_dst);
                    549:
                    550:                } else if (tmp_dst) {
1.29      djm       551:                        abs_dst = path_append(tmp_dst, tmp);
                    552:                        xfree(tmp);
                    553:                } else
                    554:                        abs_dst = make_absolute(tmp, pwd);
                    555:
                    556:                printf("Uploading %s to %s\n", g.gl_pathv[i], abs_dst);
1.44      djm       557:                if (do_upload(conn, g.gl_pathv[i], abs_dst, pflag) == -1)
1.29      djm       558:                        err = -1;
1.1       djm       559:        }
                    560:
1.29      djm       561: out:
                    562:        if (abs_dst)
                    563:                xfree(abs_dst);
                    564:        if (tmp_dst)
                    565:                xfree(tmp_dst);
1.58      mouring   566:        globfree(&g);
1.29      djm       567:        return(err);
1.1       djm       568: }
                    569:
1.37      itojun    570: static int
1.48      djm       571: sdirent_comp(const void *aa, const void *bb)
                    572: {
                    573:        SFTP_DIRENT *a = *(SFTP_DIRENT **)aa;
                    574:        SFTP_DIRENT *b = *(SFTP_DIRENT **)bb;
                    575:
1.50      deraadt   576:        return (strcmp(a->filename, b->filename));
1.48      djm       577: }
                    578:
                    579: /* sftp ls.1 replacement for directories */
                    580: static int
                    581: do_ls_dir(struct sftp_conn *conn, char *path, char *strip_path, int lflag)
                    582: {
1.60      mouring   583:        int n, c = 1, colspace = 0, columns = 1;
1.48      djm       584:        SFTP_DIRENT **d;
                    585:
                    586:        if ((n = do_readdir(conn, path, &d)) != 0)
                    587:                return (n);
                    588:
1.60      mouring   589:        if (!(lflag & SHORT_VIEW)) {
                    590:                int m = 0, width = 80;
                    591:                struct winsize ws;
1.67      mouring   592:                char *tmp;
1.60      mouring   593:
                    594:                /* Count entries for sort and find longest filename */
                    595:                for (n = 0; d[n] != NULL; n++)
                    596:                        m = MAX(m, strlen(d[n]->filename));
                    597:
1.67      mouring   598:                /* Add any subpath that also needs to be counted */
                    599:                tmp = path_strip(path, strip_path);
                    600:                m += strlen(tmp);
                    601:                xfree(tmp);
                    602:
1.65      djm       603:                if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) != -1)
1.60      mouring   604:                        width = ws.ws_col;
                    605:
                    606:                columns = width / (m + 2);
1.62      fgsch     607:                columns = MAX(columns, 1);
1.60      mouring   608:                colspace = width / columns;
1.67      mouring   609:                colspace = MIN(colspace, width);
1.60      mouring   610:        }
1.48      djm       611:
                    612:        qsort(d, n, sizeof(*d), sdirent_comp);
                    613:
                    614:        for (n = 0; d[n] != NULL; n++) {
                    615:                char *tmp, *fname;
1.50      deraadt   616:
1.48      djm       617:                tmp = path_append(path, d[n]->filename);
                    618:                fname = path_strip(tmp, strip_path);
                    619:                xfree(tmp);
                    620:
1.60      mouring   621:                if (lflag & LONG_VIEW) {
1.48      djm       622:                        char *lname;
                    623:                        struct stat sb;
                    624:
                    625:                        memset(&sb, 0, sizeof(sb));
                    626:                        attrib_to_stat(&d[n]->a, &sb);
                    627:                        lname = ls_file(fname, &sb, 1);
                    628:                        printf("%s\n", lname);
                    629:                        xfree(lname);
                    630:                } else {
1.60      mouring   631:                        printf("%-*s", colspace, fname);
                    632:                        if (c >= columns) {
                    633:                                printf("\n");
                    634:                                c = 1;
                    635:                        } else
                    636:                                c++;
1.48      djm       637:                }
1.50      deraadt   638:
1.48      djm       639:                xfree(fname);
                    640:        }
                    641:
1.60      mouring   642:        if (!(lflag & LONG_VIEW) && (c != 1))
                    643:                printf("\n");
                    644:
1.48      djm       645:        free_sftp_dirents(d);
                    646:        return (0);
                    647: }
                    648:
                    649: /* sftp ls.1 replacement which handles path globs */
                    650: static int
1.50      deraadt   651: do_globbed_ls(struct sftp_conn *conn, char *path, char *strip_path,
1.48      djm       652:     int lflag)
                    653: {
                    654:        glob_t g;
1.60      mouring   655:        int i, c = 1, colspace = 0, columns = 1;
1.48      djm       656:        Attrib *a;
                    657:
                    658:        memset(&g, 0, sizeof(g));
                    659:
1.50      deraadt   660:        if (remote_glob(conn, path, GLOB_MARK|GLOB_NOCHECK|GLOB_BRACE,
1.48      djm       661:            NULL, &g)) {
                    662:                error("Can't ls: \"%s\" not found", path);
                    663:                return (-1);
                    664:        }
                    665:
                    666:        /*
1.50      deraadt   667:         * If the glob returns a single match, which is the same as the
1.48      djm       668:         * input glob, and it is a directory, then just list its contents
                    669:         */
1.50      deraadt   670:        if (g.gl_pathc == 1 &&
1.48      djm       671:            strncmp(path, g.gl_pathv[0], strlen(g.gl_pathv[0]) - 1) == 0) {
                    672:                if ((a = do_lstat(conn, path, 1)) == NULL) {
                    673:                        globfree(&g);
1.65      djm       674:                        return (-1);
1.48      djm       675:                }
1.50      deraadt   676:                if ((a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) &&
1.48      djm       677:                    S_ISDIR(a->perm)) {
                    678:                        globfree(&g);
                    679:                        return (do_ls_dir(conn, path, strip_path, lflag));
                    680:                }
                    681:        }
                    682:
1.60      mouring   683:        if (!(lflag & SHORT_VIEW)) {
                    684:                int m = 0, width = 80;
1.65      djm       685:                struct winsize ws;
1.60      mouring   686:
                    687:                /* Count entries for sort and find longest filename */
1.65      djm       688:                for (i = 0; g.gl_pathv[i]; i++)
1.60      mouring   689:                        m = MAX(m, strlen(g.gl_pathv[i]));
                    690:
                    691:                if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) != -1)
                    692:                        width = ws.ws_col;
                    693:
                    694:                columns = width / (m + 2);
1.62      fgsch     695:                columns = MAX(columns, 1);
1.60      mouring   696:                colspace = width / columns;
                    697:        }
                    698:
1.48      djm       699:        for (i = 0; g.gl_pathv[i]; i++) {
1.60      mouring   700:                char *fname;
1.48      djm       701:
                    702:                fname = path_strip(g.gl_pathv[i], strip_path);
                    703:
1.60      mouring   704:                if (lflag & LONG_VIEW) {
                    705:                        char *lname;
                    706:                        struct stat sb;
                    707:
1.48      djm       708:                        /*
                    709:                         * XXX: this is slow - 1 roundtrip per path
1.50      deraadt   710:                         * A solution to this is to fork glob() and
                    711:                         * build a sftp specific version which keeps the
1.48      djm       712:                         * attribs (which currently get thrown away)
                    713:                         * that the server returns as well as the filenames.
                    714:                         */
                    715:                        memset(&sb, 0, sizeof(sb));
                    716:                        a = do_lstat(conn, g.gl_pathv[i], 1);
                    717:                        if (a != NULL)
                    718:                                attrib_to_stat(a, &sb);
                    719:                        lname = ls_file(fname, &sb, 1);
                    720:                        printf("%s\n", lname);
                    721:                        xfree(lname);
                    722:                } else {
1.60      mouring   723:                        printf("%-*s", colspace, fname);
                    724:                        if (c >= columns) {
                    725:                                printf("\n");
                    726:                                c = 1;
                    727:                        } else
                    728:                                c++;
1.48      djm       729:                }
                    730:                xfree(fname);
                    731:        }
1.60      mouring   732:
                    733:        if (!(lflag & LONG_VIEW) && (c != 1))
                    734:                printf("\n");
1.48      djm       735:
                    736:        if (g.gl_pathc)
                    737:                globfree(&g);
                    738:
                    739:        return (0);
                    740: }
                    741:
                    742: static int
1.51      djm       743: parse_args(const char **cpp, int *pflag, int *lflag, int *iflag,
1.48      djm       744:     unsigned long *n_arg, char **path1, char **path2)
1.1       djm       745: {
                    746:        const char *cmd, *cp = *cpp;
1.21      stevesk   747:        char *cp2;
1.4       markus    748:        int base = 0;
1.21      stevesk   749:        long l;
1.1       djm       750:        int i, cmdnum;
                    751:
                    752:        /* Skip leading whitespace */
                    753:        cp = cp + strspn(cp, WHITESPACE);
                    754:
1.51      djm       755:        /* Ignore blank lines and lines which begin with comment '#' char */
                    756:        if (*cp == '\0' || *cp == '#')
                    757:                return (0);
1.1       djm       758:
1.51      djm       759:        /* Check for leading '-' (disable error processing) */
                    760:        *iflag = 0;
                    761:        if (*cp == '-') {
                    762:                *iflag = 1;
                    763:                cp++;
                    764:        }
1.65      djm       765:
1.1       djm       766:        /* Figure out which command we have */
1.41      deraadt   767:        for (i = 0; cmds[i].c; i++) {
1.1       djm       768:                int cmdlen = strlen(cmds[i].c);
                    769:
                    770:                /* Check for command followed by whitespace */
                    771:                if (!strncasecmp(cp, cmds[i].c, cmdlen) &&
                    772:                    strchr(WHITESPACE, cp[cmdlen])) {
                    773:                        cp += cmdlen;
                    774:                        cp = cp + strspn(cp, WHITESPACE);
                    775:                        break;
                    776:                }
                    777:        }
                    778:        cmdnum = cmds[i].n;
                    779:        cmd = cmds[i].c;
                    780:
                    781:        /* Special case */
                    782:        if (*cp == '!') {
                    783:                cp++;
                    784:                cmdnum = I_SHELL;
                    785:        } else if (cmdnum == -1) {
                    786:                error("Invalid command.");
1.51      djm       787:                return (-1);
1.1       djm       788:        }
                    789:
                    790:        /* Get arguments and parse flags */
1.48      djm       791:        *lflag = *pflag = *n_arg = 0;
1.1       djm       792:        *path1 = *path2 = NULL;
                    793:        switch (cmdnum) {
                    794:        case I_GET:
                    795:        case I_PUT:
                    796:                if (parse_getput_flags(&cp, pflag))
                    797:                        return(-1);
                    798:                /* Get first pathname (mandatory) */
                    799:                if (get_pathname(&cp, path1))
                    800:                        return(-1);
                    801:                if (*path1 == NULL) {
                    802:                        error("You must specify at least one path after a "
                    803:                            "%s command.", cmd);
                    804:                        return(-1);
                    805:                }
                    806:                /* Try to get second pathname (optional) */
                    807:                if (get_pathname(&cp, path2))
                    808:                        return(-1);
                    809:                break;
                    810:        case I_RENAME:
1.26      djm       811:        case I_SYMLINK:
1.1       djm       812:                if (get_pathname(&cp, path1))
                    813:                        return(-1);
                    814:                if (get_pathname(&cp, path2))
                    815:                        return(-1);
                    816:                if (!*path1 || !*path2) {
                    817:                        error("You must specify two paths after a %s "
                    818:                            "command.", cmd);
                    819:                        return(-1);
                    820:                }
                    821:                break;
                    822:        case I_RM:
                    823:        case I_MKDIR:
                    824:        case I_RMDIR:
                    825:        case I_CHDIR:
                    826:        case I_LCHDIR:
                    827:        case I_LMKDIR:
                    828:                /* Get pathname (mandatory) */
                    829:                if (get_pathname(&cp, path1))
                    830:                        return(-1);
                    831:                if (*path1 == NULL) {
1.3       stevesk   832:                        error("You must specify a path after a %s command.",
1.1       djm       833:                            cmd);
                    834:                        return(-1);
                    835:                }
                    836:                break;
                    837:        case I_LS:
1.48      djm       838:                if (parse_ls_flags(&cp, lflag))
                    839:                        return(-1);
1.1       djm       840:                /* Path is optional */
                    841:                if (get_pathname(&cp, path1))
                    842:                        return(-1);
                    843:                break;
                    844:        case I_LLS:
                    845:        case I_SHELL:
                    846:                /* Uses the rest of the line */
                    847:                break;
                    848:        case I_LUMASK:
1.21      stevesk   849:                base = 8;
1.1       djm       850:        case I_CHMOD:
1.4       markus    851:                base = 8;
1.1       djm       852:        case I_CHOWN:
                    853:        case I_CHGRP:
                    854:                /* Get numeric arg (mandatory) */
1.21      stevesk   855:                l = strtol(cp, &cp2, base);
                    856:                if (cp2 == cp || ((l == LONG_MIN || l == LONG_MAX) &&
                    857:                    errno == ERANGE) || l < 0) {
1.1       djm       858:                        error("You must supply a numeric argument "
                    859:                            "to the %s command.", cmd);
                    860:                        return(-1);
                    861:                }
1.21      stevesk   862:                cp = cp2;
                    863:                *n_arg = l;
                    864:                if (cmdnum == I_LUMASK && strchr(WHITESPACE, *cp))
                    865:                        break;
                    866:                if (cmdnum == I_LUMASK || !strchr(WHITESPACE, *cp)) {
1.1       djm       867:                        error("You must supply a numeric argument "
                    868:                            "to the %s command.", cmd);
                    869:                        return(-1);
                    870:                }
                    871:                cp += strspn(cp, WHITESPACE);
                    872:
                    873:                /* Get pathname (mandatory) */
                    874:                if (get_pathname(&cp, path1))
                    875:                        return(-1);
                    876:                if (*path1 == NULL) {
1.3       stevesk   877:                        error("You must specify a path after a %s command.",
1.1       djm       878:                            cmd);
                    879:                        return(-1);
                    880:                }
                    881:                break;
                    882:        case I_QUIT:
                    883:        case I_PWD:
                    884:        case I_LPWD:
                    885:        case I_HELP:
1.28      markus    886:        case I_VERSION:
1.52      fgsch     887:        case I_PROGRESS:
1.1       djm       888:                break;
                    889:        default:
                    890:                fatal("Command not implemented");
                    891:        }
                    892:
                    893:        *cpp = cp;
                    894:        return(cmdnum);
                    895: }
                    896:
1.37      itojun    897: static int
1.51      djm       898: parse_dispatch_command(struct sftp_conn *conn, const char *cmd, char **pwd,
                    899:     int err_abort)
1.1       djm       900: {
1.8       provos    901:        char *path1, *path2, *tmp;
1.51      djm       902:        int pflag, lflag, iflag, cmdnum, i;
1.1       djm       903:        unsigned long n_arg;
                    904:        Attrib a, *aa;
1.23      deraadt   905:        char path_buf[MAXPATHLEN];
1.25      deraadt   906:        int err = 0;
1.27      djm       907:        glob_t g;
1.1       djm       908:
                    909:        path1 = path2 = NULL;
1.51      djm       910:        cmdnum = parse_args(&cmd, &pflag, &lflag, &iflag, &n_arg,
1.48      djm       911:            &path1, &path2);
1.1       djm       912:
1.51      djm       913:        if (iflag != 0)
                    914:                err_abort = 0;
                    915:
1.29      djm       916:        memset(&g, 0, sizeof(g));
                    917:
1.1       djm       918:        /* Perform command */
                    919:        switch (cmdnum) {
1.51      djm       920:        case 0:
                    921:                /* Blank line */
                    922:                break;
1.1       djm       923:        case -1:
1.51      djm       924:                /* Unrecognized command */
                    925:                err = -1;
1.1       djm       926:                break;
                    927:        case I_GET:
1.44      djm       928:                err = process_get(conn, path1, path2, *pwd, pflag);
1.1       djm       929:                break;
                    930:        case I_PUT:
1.44      djm       931:                err = process_put(conn, path1, path2, *pwd, pflag);
1.33      markus    932:                break;
                    933:        case I_RENAME:
1.1       djm       934:                path1 = make_absolute(path1, *pwd);
                    935:                path2 = make_absolute(path2, *pwd);
1.44      djm       936:                err = do_rename(conn, path1, path2);
1.1       djm       937:                break;
1.26      djm       938:        case I_SYMLINK:
1.44      djm       939:                path2 = make_absolute(path2, *pwd);
                    940:                err = do_symlink(conn, path1, path2);
1.26      djm       941:                break;
1.1       djm       942:        case I_RM:
                    943:                path1 = make_absolute(path1, *pwd);
1.44      djm       944:                remote_glob(conn, path1, GLOB_NOCHECK, NULL, &g);
1.41      deraadt   945:                for (i = 0; g.gl_pathv[i]; i++) {
1.27      djm       946:                        printf("Removing %s\n", g.gl_pathv[i]);
1.51      djm       947:                        err = do_rm(conn, g.gl_pathv[i]);
                    948:                        if (err != 0 && err_abort)
                    949:                                break;
1.27      djm       950:                }
1.1       djm       951:                break;
                    952:        case I_MKDIR:
                    953:                path1 = make_absolute(path1, *pwd);
                    954:                attrib_clear(&a);
                    955:                a.flags |= SSH2_FILEXFER_ATTR_PERMISSIONS;
                    956:                a.perm = 0777;
1.44      djm       957:                err = do_mkdir(conn, path1, &a);
1.1       djm       958:                break;
                    959:        case I_RMDIR:
                    960:                path1 = make_absolute(path1, *pwd);
1.44      djm       961:                err = do_rmdir(conn, path1);
1.1       djm       962:                break;
                    963:        case I_CHDIR:
                    964:                path1 = make_absolute(path1, *pwd);
1.44      djm       965:                if ((tmp = do_realpath(conn, path1)) == NULL) {
1.25      deraadt   966:                        err = 1;
1.11      markus    967:                        break;
1.25      deraadt   968:                }
1.44      djm       969:                if ((aa = do_stat(conn, tmp, 0)) == NULL) {
1.11      markus    970:                        xfree(tmp);
1.25      deraadt   971:                        err = 1;
1.11      markus    972:                        break;
                    973:                }
1.9       djm       974:                if (!(aa->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)) {
                    975:                        error("Can't change directory: Can't check target");
                    976:                        xfree(tmp);
1.25      deraadt   977:                        err = 1;
1.9       djm       978:                        break;
                    979:                }
                    980:                if (!S_ISDIR(aa->perm)) {
                    981:                        error("Can't change directory: \"%s\" is not "
                    982:                            "a directory", tmp);
                    983:                        xfree(tmp);
1.25      deraadt   984:                        err = 1;
1.9       djm       985:                        break;
                    986:                }
1.11      markus    987:                xfree(*pwd);
                    988:                *pwd = tmp;
1.1       djm       989:                break;
                    990:        case I_LS:
1.12      djm       991:                if (!path1) {
1.48      djm       992:                        do_globbed_ls(conn, *pwd, *pwd, lflag);
1.12      djm       993:                        break;
                    994:                }
1.50      deraadt   995:
1.48      djm       996:                /* Strip pwd off beginning of non-absolute paths */
                    997:                tmp = NULL;
                    998:                if (*path1 != '/')
                    999:                        tmp = *pwd;
                   1000:
1.1       djm      1001:                path1 = make_absolute(path1, *pwd);
1.51      djm      1002:                err = do_globbed_ls(conn, path1, tmp, lflag);
1.1       djm      1003:                break;
                   1004:        case I_LCHDIR:
1.25      deraadt  1005:                if (chdir(path1) == -1) {
1.1       djm      1006:                        error("Couldn't change local directory to "
                   1007:                            "\"%s\": %s", path1, strerror(errno));
1.25      deraadt  1008:                        err = 1;
                   1009:                }
1.1       djm      1010:                break;
                   1011:        case I_LMKDIR:
1.25      deraadt  1012:                if (mkdir(path1, 0777) == -1) {
1.17      stevesk  1013:                        error("Couldn't create local directory "
1.1       djm      1014:                            "\"%s\": %s", path1, strerror(errno));
1.25      deraadt  1015:                        err = 1;
                   1016:                }
1.1       djm      1017:                break;
                   1018:        case I_LLS:
                   1019:                local_do_ls(cmd);
                   1020:                break;
                   1021:        case I_SHELL:
                   1022:                local_do_shell(cmd);
                   1023:                break;
                   1024:        case I_LUMASK:
                   1025:                umask(n_arg);
1.21      stevesk  1026:                printf("Local umask: %03lo\n", n_arg);
1.1       djm      1027:                break;
                   1028:        case I_CHMOD:
                   1029:                path1 = make_absolute(path1, *pwd);
                   1030:                attrib_clear(&a);
                   1031:                a.flags |= SSH2_FILEXFER_ATTR_PERMISSIONS;
                   1032:                a.perm = n_arg;
1.44      djm      1033:                remote_glob(conn, path1, GLOB_NOCHECK, NULL, &g);
1.41      deraadt  1034:                for (i = 0; g.gl_pathv[i]; i++) {
1.27      djm      1035:                        printf("Changing mode on %s\n", g.gl_pathv[i]);
1.51      djm      1036:                        err = do_setstat(conn, g.gl_pathv[i], &a);
                   1037:                        if (err != 0 && err_abort)
                   1038:                                break;
1.27      djm      1039:                }
1.5       stevesk  1040:                break;
1.1       djm      1041:        case I_CHOWN:
1.51      djm      1042:        case I_CHGRP:
1.1       djm      1043:                path1 = make_absolute(path1, *pwd);
1.44      djm      1044:                remote_glob(conn, path1, GLOB_NOCHECK, NULL, &g);
1.41      deraadt  1045:                for (i = 0; g.gl_pathv[i]; i++) {
1.51      djm      1046:                        if (!(aa = do_stat(conn, g.gl_pathv[i], 0))) {
                   1047:                                if (err != 0 && err_abort)
                   1048:                                        break;
                   1049:                                else
                   1050:                                        continue;
                   1051:                        }
1.27      djm      1052:                        if (!(aa->flags & SSH2_FILEXFER_ATTR_UIDGID)) {
                   1053:                                error("Can't get current ownership of "
                   1054:                                    "remote file \"%s\"", g.gl_pathv[i]);
1.51      djm      1055:                                if (err != 0 && err_abort)
                   1056:                                        break;
                   1057:                                else
                   1058:                                        continue;
1.27      djm      1059:                        }
                   1060:                        aa->flags &= SSH2_FILEXFER_ATTR_UIDGID;
1.51      djm      1061:                        if (cmdnum == I_CHOWN) {
                   1062:                                printf("Changing owner on %s\n", g.gl_pathv[i]);
                   1063:                                aa->uid = n_arg;
                   1064:                        } else {
                   1065:                                printf("Changing group on %s\n", g.gl_pathv[i]);
                   1066:                                aa->gid = n_arg;
1.27      djm      1067:                        }
1.51      djm      1068:                        err = do_setstat(conn, g.gl_pathv[i], aa);
                   1069:                        if (err != 0 && err_abort)
                   1070:                                break;
1.1       djm      1071:                }
                   1072:                break;
                   1073:        case I_PWD:
                   1074:                printf("Remote working directory: %s\n", *pwd);
                   1075:                break;
                   1076:        case I_LPWD:
1.51      djm      1077:                if (!getcwd(path_buf, sizeof(path_buf))) {
                   1078:                        error("Couldn't get local cwd: %s", strerror(errno));
                   1079:                        err = -1;
                   1080:                        break;
                   1081:                }
                   1082:                printf("Local working directory: %s\n", path_buf);
1.1       djm      1083:                break;
                   1084:        case I_QUIT:
1.51      djm      1085:                /* Processed below */
                   1086:                break;
1.1       djm      1087:        case I_HELP:
                   1088:                help();
1.28      markus   1089:                break;
                   1090:        case I_VERSION:
1.47      deraadt  1091:                printf("SFTP protocol version %u\n", sftp_proto_version(conn));
1.52      fgsch    1092:                break;
                   1093:        case I_PROGRESS:
                   1094:                showprogress = !showprogress;
                   1095:                if (showprogress)
                   1096:                        printf("Progress meter enabled\n");
                   1097:                else
                   1098:                        printf("Progress meter disabled\n");
1.1       djm      1099:                break;
                   1100:        default:
                   1101:                fatal("%d is not implemented", cmdnum);
                   1102:        }
                   1103:
1.29      djm      1104:        if (g.gl_pathc)
                   1105:                globfree(&g);
1.1       djm      1106:        if (path1)
                   1107:                xfree(path1);
                   1108:        if (path2)
                   1109:                xfree(path2);
1.25      deraadt  1110:
1.51      djm      1111:        /* If an unignored error occurs in batch mode we should abort. */
                   1112:        if (err_abort && err != 0)
                   1113:                return (-1);
                   1114:        else if (cmdnum == I_QUIT)
                   1115:                return (1);
1.25      deraadt  1116:
1.51      djm      1117:        return (0);
1.1       djm      1118: }
                   1119:
1.51      djm      1120: int
1.35      mouring  1121: interactive_loop(int fd_in, int fd_out, char *file1, char *file2)
1.1       djm      1122: {
                   1123:        char *pwd;
1.35      mouring  1124:        char *dir = NULL;
1.1       djm      1125:        char cmd[2048];
1.44      djm      1126:        struct sftp_conn *conn;
1.51      djm      1127:        int err;
1.26      djm      1128:
1.44      djm      1129:        conn = do_init(fd_in, fd_out, copy_buffer_len, num_requests);
                   1130:        if (conn == NULL)
1.26      djm      1131:                fatal("Couldn't initialise connection to server");
1.1       djm      1132:
1.44      djm      1133:        pwd = do_realpath(conn, ".");
1.1       djm      1134:        if (pwd == NULL)
                   1135:                fatal("Need cwd");
                   1136:
1.35      mouring  1137:        if (file1 != NULL) {
                   1138:                dir = xstrdup(file1);
                   1139:                dir = make_absolute(dir, pwd);
                   1140:
1.44      djm      1141:                if (remote_is_dir(conn, dir) && file2 == NULL) {
1.35      mouring  1142:                        printf("Changing to: %s\n", dir);
                   1143:                        snprintf(cmd, sizeof cmd, "cd \"%s\"", dir);
1.51      djm      1144:                        if (parse_dispatch_command(conn, cmd, &pwd, 1) != 0)
                   1145:                                return (-1);
1.35      mouring  1146:                } else {
                   1147:                        if (file2 == NULL)
                   1148:                                snprintf(cmd, sizeof cmd, "get %s", dir);
                   1149:                        else
                   1150:                                snprintf(cmd, sizeof cmd, "get %s %s", dir,
                   1151:                                    file2);
                   1152:
1.51      djm      1153:                        err = parse_dispatch_command(conn, cmd, &pwd, 1);
1.45      mpech    1154:                        xfree(dir);
1.57      markus   1155:                        xfree(pwd);
1.51      djm      1156:                        return (err);
1.35      mouring  1157:                }
1.45      mpech    1158:                xfree(dir);
1.35      mouring  1159:        }
1.51      djm      1160:
1.14      stevesk  1161:        setvbuf(stdout, NULL, _IOLBF, 0);
1.25      deraadt  1162:        setvbuf(infile, NULL, _IOLBF, 0);
1.1       djm      1163:
1.51      djm      1164:        err = 0;
1.41      deraadt  1165:        for (;;) {
1.1       djm      1166:                char *cp;
                   1167:
                   1168:                printf("sftp> ");
                   1169:
                   1170:                /* XXX: use libedit */
1.25      deraadt  1171:                if (fgets(cmd, sizeof(cmd), infile) == NULL) {
1.1       djm      1172:                        printf("\n");
                   1173:                        break;
1.66      djm      1174:                }
                   1175:
                   1176:                if (batchmode) /* Echo command */
1.25      deraadt  1177:                        printf("%s", cmd);
                   1178:
1.1       djm      1179:                cp = strrchr(cmd, '\n');
                   1180:                if (cp)
                   1181:                        *cp = '\0';
1.25      deraadt  1182:
1.66      djm      1183:                err = parse_dispatch_command(conn, cmd, &pwd, batchmode);
1.51      djm      1184:                if (err != 0)
1.1       djm      1185:                        break;
                   1186:        }
                   1187:        xfree(pwd);
1.51      djm      1188:
                   1189:        /* err == 1 signifies normal "quit" exit */
                   1190:        return (err >= 0 ? 0 : -1);
1.1       djm      1191: }
1.51      djm      1192: