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

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