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

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.66    ! djm        28: RCSID("$OpenBSD: sftp-int.c,v 1.65 2003/11/21 11:57:03 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;
                    600:
                    601:                /* Count entries for sort and find longest filename */
                    602:                for (n = 0; d[n] != NULL; n++)
                    603:                        m = MAX(m, strlen(d[n]->filename));
                    604:
1.65      djm       605:                if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) != -1)
1.60      mouring   606:                        width = ws.ws_col;
                    607:
                    608:                columns = width / (m + 2);
1.62      fgsch     609:                columns = MAX(columns, 1);
1.60      mouring   610:                colspace = width / columns;
                    611:        }
1.48      djm       612:
                    613:        qsort(d, n, sizeof(*d), sdirent_comp);
                    614:
                    615:        for (n = 0; d[n] != NULL; n++) {
                    616:                char *tmp, *fname;
1.50      deraadt   617:
1.48      djm       618:                tmp = path_append(path, d[n]->filename);
                    619:                fname = path_strip(tmp, strip_path);
                    620:                xfree(tmp);
                    621:
1.60      mouring   622:                if (lflag & LONG_VIEW) {
1.48      djm       623:                        char *lname;
                    624:                        struct stat sb;
                    625:
                    626:                        memset(&sb, 0, sizeof(sb));
                    627:                        attrib_to_stat(&d[n]->a, &sb);
                    628:                        lname = ls_file(fname, &sb, 1);
                    629:                        printf("%s\n", lname);
                    630:                        xfree(lname);
                    631:                } else {
1.60      mouring   632:                        printf("%-*s", colspace, fname);
                    633:                        if (c >= columns) {
                    634:                                printf("\n");
                    635:                                c = 1;
                    636:                        } else
                    637:                                c++;
1.48      djm       638:                }
1.50      deraadt   639:
1.48      djm       640:                xfree(fname);
                    641:        }
                    642:
1.60      mouring   643:        if (!(lflag & LONG_VIEW) && (c != 1))
                    644:                printf("\n");
                    645:
1.48      djm       646:        free_sftp_dirents(d);
                    647:        return (0);
                    648: }
                    649:
                    650: /* sftp ls.1 replacement which handles path globs */
                    651: static int
1.50      deraadt   652: do_globbed_ls(struct sftp_conn *conn, char *path, char *strip_path,
1.48      djm       653:     int lflag)
                    654: {
                    655:        glob_t g;
1.60      mouring   656:        int i, c = 1, colspace = 0, columns = 1;
1.48      djm       657:        Attrib *a;
                    658:
                    659:        memset(&g, 0, sizeof(g));
                    660:
1.50      deraadt   661:        if (remote_glob(conn, path, GLOB_MARK|GLOB_NOCHECK|GLOB_BRACE,
1.48      djm       662:            NULL, &g)) {
                    663:                error("Can't ls: \"%s\" not found", path);
                    664:                return (-1);
                    665:        }
                    666:
                    667:        /*
1.50      deraadt   668:         * If the glob returns a single match, which is the same as the
1.48      djm       669:         * input glob, and it is a directory, then just list its contents
                    670:         */
1.50      deraadt   671:        if (g.gl_pathc == 1 &&
1.48      djm       672:            strncmp(path, g.gl_pathv[0], strlen(g.gl_pathv[0]) - 1) == 0) {
                    673:                if ((a = do_lstat(conn, path, 1)) == NULL) {
                    674:                        globfree(&g);
1.65      djm       675:                        return (-1);
1.48      djm       676:                }
1.50      deraadt   677:                if ((a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) &&
1.48      djm       678:                    S_ISDIR(a->perm)) {
                    679:                        globfree(&g);
                    680:                        return (do_ls_dir(conn, path, strip_path, lflag));
                    681:                }
                    682:        }
                    683:
1.60      mouring   684:        if (!(lflag & SHORT_VIEW)) {
                    685:                int m = 0, width = 80;
1.65      djm       686:                struct winsize ws;
1.60      mouring   687:
                    688:                /* Count entries for sort and find longest filename */
1.65      djm       689:                for (i = 0; g.gl_pathv[i]; i++)
1.60      mouring   690:                        m = MAX(m, strlen(g.gl_pathv[i]));
                    691:
                    692:                if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) != -1)
                    693:                        width = ws.ws_col;
                    694:
                    695:                columns = width / (m + 2);
1.62      fgsch     696:                columns = MAX(columns, 1);
1.60      mouring   697:                colspace = width / columns;
                    698:        }
                    699:
1.48      djm       700:        for (i = 0; g.gl_pathv[i]; i++) {
1.60      mouring   701:                char *fname;
1.48      djm       702:
                    703:                fname = path_strip(g.gl_pathv[i], strip_path);
                    704:
1.60      mouring   705:                if (lflag & LONG_VIEW) {
                    706:                        char *lname;
                    707:                        struct stat sb;
                    708:
1.48      djm       709:                        /*
                    710:                         * XXX: this is slow - 1 roundtrip per path
1.50      deraadt   711:                         * A solution to this is to fork glob() and
                    712:                         * build a sftp specific version which keeps the
1.48      djm       713:                         * attribs (which currently get thrown away)
                    714:                         * that the server returns as well as the filenames.
                    715:                         */
                    716:                        memset(&sb, 0, sizeof(sb));
                    717:                        a = do_lstat(conn, g.gl_pathv[i], 1);
                    718:                        if (a != NULL)
                    719:                                attrib_to_stat(a, &sb);
                    720:                        lname = ls_file(fname, &sb, 1);
                    721:                        printf("%s\n", lname);
                    722:                        xfree(lname);
                    723:                } else {
1.60      mouring   724:                        printf("%-*s", colspace, fname);
                    725:                        if (c >= columns) {
                    726:                                printf("\n");
                    727:                                c = 1;
                    728:                        } else
                    729:                                c++;
1.48      djm       730:                }
                    731:                xfree(fname);
                    732:        }
1.60      mouring   733:
                    734:        if (!(lflag & LONG_VIEW) && (c != 1))
                    735:                printf("\n");
1.48      djm       736:
                    737:        if (g.gl_pathc)
                    738:                globfree(&g);
                    739:
                    740:        return (0);
                    741: }
                    742:
                    743: static int
1.51      djm       744: parse_args(const char **cpp, int *pflag, int *lflag, int *iflag,
1.48      djm       745:     unsigned long *n_arg, char **path1, char **path2)
1.1       djm       746: {
                    747:        const char *cmd, *cp = *cpp;
1.21      stevesk   748:        char *cp2;
1.4       markus    749:        int base = 0;
1.21      stevesk   750:        long l;
1.1       djm       751:        int i, cmdnum;
                    752:
                    753:        /* Skip leading whitespace */
                    754:        cp = cp + strspn(cp, WHITESPACE);
                    755:
1.51      djm       756:        /* Ignore blank lines and lines which begin with comment '#' char */
                    757:        if (*cp == '\0' || *cp == '#')
                    758:                return (0);
1.1       djm       759:
1.51      djm       760:        /* Check for leading '-' (disable error processing) */
                    761:        *iflag = 0;
                    762:        if (*cp == '-') {
                    763:                *iflag = 1;
                    764:                cp++;
                    765:        }
1.65      djm       766:
1.1       djm       767:        /* Figure out which command we have */
1.41      deraadt   768:        for (i = 0; cmds[i].c; i++) {
1.1       djm       769:                int cmdlen = strlen(cmds[i].c);
                    770:
                    771:                /* Check for command followed by whitespace */
                    772:                if (!strncasecmp(cp, cmds[i].c, cmdlen) &&
                    773:                    strchr(WHITESPACE, cp[cmdlen])) {
                    774:                        cp += cmdlen;
                    775:                        cp = cp + strspn(cp, WHITESPACE);
                    776:                        break;
                    777:                }
                    778:        }
                    779:        cmdnum = cmds[i].n;
                    780:        cmd = cmds[i].c;
                    781:
                    782:        /* Special case */
                    783:        if (*cp == '!') {
                    784:                cp++;
                    785:                cmdnum = I_SHELL;
                    786:        } else if (cmdnum == -1) {
                    787:                error("Invalid command.");
1.51      djm       788:                return (-1);
1.1       djm       789:        }
                    790:
                    791:        /* Get arguments and parse flags */
1.48      djm       792:        *lflag = *pflag = *n_arg = 0;
1.1       djm       793:        *path1 = *path2 = NULL;
                    794:        switch (cmdnum) {
                    795:        case I_GET:
                    796:        case I_PUT:
                    797:                if (parse_getput_flags(&cp, pflag))
                    798:                        return(-1);
                    799:                /* Get first pathname (mandatory) */
                    800:                if (get_pathname(&cp, path1))
                    801:                        return(-1);
                    802:                if (*path1 == NULL) {
                    803:                        error("You must specify at least one path after a "
                    804:                            "%s command.", cmd);
                    805:                        return(-1);
                    806:                }
                    807:                /* Try to get second pathname (optional) */
                    808:                if (get_pathname(&cp, path2))
                    809:                        return(-1);
                    810:                break;
                    811:        case I_RENAME:
1.26      djm       812:        case I_SYMLINK:
1.1       djm       813:                if (get_pathname(&cp, path1))
                    814:                        return(-1);
                    815:                if (get_pathname(&cp, path2))
                    816:                        return(-1);
                    817:                if (!*path1 || !*path2) {
                    818:                        error("You must specify two paths after a %s "
                    819:                            "command.", cmd);
                    820:                        return(-1);
                    821:                }
                    822:                break;
                    823:        case I_RM:
                    824:        case I_MKDIR:
                    825:        case I_RMDIR:
                    826:        case I_CHDIR:
                    827:        case I_LCHDIR:
                    828:        case I_LMKDIR:
                    829:                /* Get pathname (mandatory) */
                    830:                if (get_pathname(&cp, path1))
                    831:                        return(-1);
                    832:                if (*path1 == NULL) {
1.3       stevesk   833:                        error("You must specify a path after a %s command.",
1.1       djm       834:                            cmd);
                    835:                        return(-1);
                    836:                }
                    837:                break;
                    838:        case I_LS:
1.48      djm       839:                if (parse_ls_flags(&cp, lflag))
                    840:                        return(-1);
1.1       djm       841:                /* Path is optional */
                    842:                if (get_pathname(&cp, path1))
                    843:                        return(-1);
                    844:                break;
                    845:        case I_LLS:
                    846:        case I_SHELL:
                    847:                /* Uses the rest of the line */
                    848:                break;
                    849:        case I_LUMASK:
1.21      stevesk   850:                base = 8;
1.1       djm       851:        case I_CHMOD:
1.4       markus    852:                base = 8;
1.1       djm       853:        case I_CHOWN:
                    854:        case I_CHGRP:
                    855:                /* Get numeric arg (mandatory) */
1.21      stevesk   856:                l = strtol(cp, &cp2, base);
                    857:                if (cp2 == cp || ((l == LONG_MIN || l == LONG_MAX) &&
                    858:                    errno == ERANGE) || l < 0) {
1.1       djm       859:                        error("You must supply a numeric argument "
                    860:                            "to the %s command.", cmd);
                    861:                        return(-1);
                    862:                }
1.21      stevesk   863:                cp = cp2;
                    864:                *n_arg = l;
                    865:                if (cmdnum == I_LUMASK && strchr(WHITESPACE, *cp))
                    866:                        break;
                    867:                if (cmdnum == I_LUMASK || !strchr(WHITESPACE, *cp)) {
1.1       djm       868:                        error("You must supply a numeric argument "
                    869:                            "to the %s command.", cmd);
                    870:                        return(-1);
                    871:                }
                    872:                cp += strspn(cp, WHITESPACE);
                    873:
                    874:                /* Get pathname (mandatory) */
                    875:                if (get_pathname(&cp, path1))
                    876:                        return(-1);
                    877:                if (*path1 == NULL) {
1.3       stevesk   878:                        error("You must specify a path after a %s command.",
1.1       djm       879:                            cmd);
                    880:                        return(-1);
                    881:                }
                    882:                break;
                    883:        case I_QUIT:
                    884:        case I_PWD:
                    885:        case I_LPWD:
                    886:        case I_HELP:
1.28      markus    887:        case I_VERSION:
1.52      fgsch     888:        case I_PROGRESS:
1.1       djm       889:                break;
                    890:        default:
                    891:                fatal("Command not implemented");
                    892:        }
                    893:
                    894:        *cpp = cp;
                    895:        return(cmdnum);
                    896: }
                    897:
1.37      itojun    898: static int
1.51      djm       899: parse_dispatch_command(struct sftp_conn *conn, const char *cmd, char **pwd,
                    900:     int err_abort)
1.1       djm       901: {
1.8       provos    902:        char *path1, *path2, *tmp;
1.51      djm       903:        int pflag, lflag, iflag, cmdnum, i;
1.1       djm       904:        unsigned long n_arg;
                    905:        Attrib a, *aa;
1.23      deraadt   906:        char path_buf[MAXPATHLEN];
1.25      deraadt   907:        int err = 0;
1.27      djm       908:        glob_t g;
1.1       djm       909:
                    910:        path1 = path2 = NULL;
1.51      djm       911:        cmdnum = parse_args(&cmd, &pflag, &lflag, &iflag, &n_arg,
1.48      djm       912:            &path1, &path2);
1.1       djm       913:
1.51      djm       914:        if (iflag != 0)
                    915:                err_abort = 0;
                    916:
1.29      djm       917:        memset(&g, 0, sizeof(g));
                    918:
1.1       djm       919:        /* Perform command */
                    920:        switch (cmdnum) {
1.51      djm       921:        case 0:
                    922:                /* Blank line */
                    923:                break;
1.1       djm       924:        case -1:
1.51      djm       925:                /* Unrecognized command */
                    926:                err = -1;
1.1       djm       927:                break;
                    928:        case I_GET:
1.44      djm       929:                err = process_get(conn, path1, path2, *pwd, pflag);
1.1       djm       930:                break;
                    931:        case I_PUT:
1.44      djm       932:                err = process_put(conn, path1, path2, *pwd, pflag);
1.33      markus    933:                break;
                    934:        case I_RENAME:
1.1       djm       935:                path1 = make_absolute(path1, *pwd);
                    936:                path2 = make_absolute(path2, *pwd);
1.44      djm       937:                err = do_rename(conn, path1, path2);
1.1       djm       938:                break;
1.26      djm       939:        case I_SYMLINK:
1.44      djm       940:                path2 = make_absolute(path2, *pwd);
                    941:                err = do_symlink(conn, path1, path2);
1.26      djm       942:                break;
1.1       djm       943:        case I_RM:
                    944:                path1 = make_absolute(path1, *pwd);
1.44      djm       945:                remote_glob(conn, path1, GLOB_NOCHECK, NULL, &g);
1.41      deraadt   946:                for (i = 0; g.gl_pathv[i]; i++) {
1.27      djm       947:                        printf("Removing %s\n", g.gl_pathv[i]);
1.51      djm       948:                        err = do_rm(conn, g.gl_pathv[i]);
                    949:                        if (err != 0 && err_abort)
                    950:                                break;
1.27      djm       951:                }
1.1       djm       952:                break;
                    953:        case I_MKDIR:
                    954:                path1 = make_absolute(path1, *pwd);
                    955:                attrib_clear(&a);
                    956:                a.flags |= SSH2_FILEXFER_ATTR_PERMISSIONS;
                    957:                a.perm = 0777;
1.44      djm       958:                err = do_mkdir(conn, path1, &a);
1.1       djm       959:                break;
                    960:        case I_RMDIR:
                    961:                path1 = make_absolute(path1, *pwd);
1.44      djm       962:                err = do_rmdir(conn, path1);
1.1       djm       963:                break;
                    964:        case I_CHDIR:
                    965:                path1 = make_absolute(path1, *pwd);
1.44      djm       966:                if ((tmp = do_realpath(conn, path1)) == NULL) {
1.25      deraadt   967:                        err = 1;
1.11      markus    968:                        break;
1.25      deraadt   969:                }
1.44      djm       970:                if ((aa = do_stat(conn, tmp, 0)) == NULL) {
1.11      markus    971:                        xfree(tmp);
1.25      deraadt   972:                        err = 1;
1.11      markus    973:                        break;
                    974:                }
1.9       djm       975:                if (!(aa->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)) {
                    976:                        error("Can't change directory: Can't check target");
                    977:                        xfree(tmp);
1.25      deraadt   978:                        err = 1;
1.9       djm       979:                        break;
                    980:                }
                    981:                if (!S_ISDIR(aa->perm)) {
                    982:                        error("Can't change directory: \"%s\" is not "
                    983:                            "a directory", tmp);
                    984:                        xfree(tmp);
1.25      deraadt   985:                        err = 1;
1.9       djm       986:                        break;
                    987:                }
1.11      markus    988:                xfree(*pwd);
                    989:                *pwd = tmp;
1.1       djm       990:                break;
                    991:        case I_LS:
1.12      djm       992:                if (!path1) {
1.48      djm       993:                        do_globbed_ls(conn, *pwd, *pwd, lflag);
1.12      djm       994:                        break;
                    995:                }
1.50      deraadt   996:
1.48      djm       997:                /* Strip pwd off beginning of non-absolute paths */
                    998:                tmp = NULL;
                    999:                if (*path1 != '/')
                   1000:                        tmp = *pwd;
                   1001:
1.1       djm      1002:                path1 = make_absolute(path1, *pwd);
1.51      djm      1003:                err = do_globbed_ls(conn, path1, tmp, lflag);
1.1       djm      1004:                break;
                   1005:        case I_LCHDIR:
1.25      deraadt  1006:                if (chdir(path1) == -1) {
1.1       djm      1007:                        error("Couldn't change local directory to "
                   1008:                            "\"%s\": %s", path1, strerror(errno));
1.25      deraadt  1009:                        err = 1;
                   1010:                }
1.1       djm      1011:                break;
                   1012:        case I_LMKDIR:
1.25      deraadt  1013:                if (mkdir(path1, 0777) == -1) {
1.17      stevesk  1014:                        error("Couldn't create local directory "
1.1       djm      1015:                            "\"%s\": %s", path1, strerror(errno));
1.25      deraadt  1016:                        err = 1;
                   1017:                }
1.1       djm      1018:                break;
                   1019:        case I_LLS:
                   1020:                local_do_ls(cmd);
                   1021:                break;
                   1022:        case I_SHELL:
                   1023:                local_do_shell(cmd);
                   1024:                break;
                   1025:        case I_LUMASK:
                   1026:                umask(n_arg);
1.21      stevesk  1027:                printf("Local umask: %03lo\n", n_arg);
1.1       djm      1028:                break;
                   1029:        case I_CHMOD:
                   1030:                path1 = make_absolute(path1, *pwd);
                   1031:                attrib_clear(&a);
                   1032:                a.flags |= SSH2_FILEXFER_ATTR_PERMISSIONS;
                   1033:                a.perm = n_arg;
1.44      djm      1034:                remote_glob(conn, path1, GLOB_NOCHECK, NULL, &g);
1.41      deraadt  1035:                for (i = 0; g.gl_pathv[i]; i++) {
1.27      djm      1036:                        printf("Changing mode on %s\n", g.gl_pathv[i]);
1.51      djm      1037:                        err = do_setstat(conn, g.gl_pathv[i], &a);
                   1038:                        if (err != 0 && err_abort)
                   1039:                                break;
1.27      djm      1040:                }
1.5       stevesk  1041:                break;
1.1       djm      1042:        case I_CHOWN:
1.51      djm      1043:        case I_CHGRP:
1.1       djm      1044:                path1 = make_absolute(path1, *pwd);
1.44      djm      1045:                remote_glob(conn, path1, GLOB_NOCHECK, NULL, &g);
1.41      deraadt  1046:                for (i = 0; g.gl_pathv[i]; i++) {
1.51      djm      1047:                        if (!(aa = do_stat(conn, g.gl_pathv[i], 0))) {
                   1048:                                if (err != 0 && err_abort)
                   1049:                                        break;
                   1050:                                else
                   1051:                                        continue;
                   1052:                        }
1.27      djm      1053:                        if (!(aa->flags & SSH2_FILEXFER_ATTR_UIDGID)) {
                   1054:                                error("Can't get current ownership of "
                   1055:                                    "remote file \"%s\"", g.gl_pathv[i]);
1.51      djm      1056:                                if (err != 0 && err_abort)
                   1057:                                        break;
                   1058:                                else
                   1059:                                        continue;
1.27      djm      1060:                        }
                   1061:                        aa->flags &= SSH2_FILEXFER_ATTR_UIDGID;
1.51      djm      1062:                        if (cmdnum == I_CHOWN) {
                   1063:                                printf("Changing owner on %s\n", g.gl_pathv[i]);
                   1064:                                aa->uid = n_arg;
                   1065:                        } else {
                   1066:                                printf("Changing group on %s\n", g.gl_pathv[i]);
                   1067:                                aa->gid = n_arg;
1.27      djm      1068:                        }
1.51      djm      1069:                        err = do_setstat(conn, g.gl_pathv[i], aa);
                   1070:                        if (err != 0 && err_abort)
                   1071:                                break;
1.1       djm      1072:                }
                   1073:                break;
                   1074:        case I_PWD:
                   1075:                printf("Remote working directory: %s\n", *pwd);
                   1076:                break;
                   1077:        case I_LPWD:
1.51      djm      1078:                if (!getcwd(path_buf, sizeof(path_buf))) {
                   1079:                        error("Couldn't get local cwd: %s", strerror(errno));
                   1080:                        err = -1;
                   1081:                        break;
                   1082:                }
                   1083:                printf("Local working directory: %s\n", path_buf);
1.1       djm      1084:                break;
                   1085:        case I_QUIT:
1.51      djm      1086:                /* Processed below */
                   1087:                break;
1.1       djm      1088:        case I_HELP:
                   1089:                help();
1.28      markus   1090:                break;
                   1091:        case I_VERSION:
1.47      deraadt  1092:                printf("SFTP protocol version %u\n", sftp_proto_version(conn));
1.52      fgsch    1093:                break;
                   1094:        case I_PROGRESS:
                   1095:                showprogress = !showprogress;
                   1096:                if (showprogress)
                   1097:                        printf("Progress meter enabled\n");
                   1098:                else
                   1099:                        printf("Progress meter disabled\n");
1.1       djm      1100:                break;
                   1101:        default:
                   1102:                fatal("%d is not implemented", cmdnum);
                   1103:        }
                   1104:
1.29      djm      1105:        if (g.gl_pathc)
                   1106:                globfree(&g);
1.1       djm      1107:        if (path1)
                   1108:                xfree(path1);
                   1109:        if (path2)
                   1110:                xfree(path2);
1.25      deraadt  1111:
1.51      djm      1112:        /* If an unignored error occurs in batch mode we should abort. */
                   1113:        if (err_abort && err != 0)
                   1114:                return (-1);
                   1115:        else if (cmdnum == I_QUIT)
                   1116:                return (1);
1.25      deraadt  1117:
1.51      djm      1118:        return (0);
1.1       djm      1119: }
                   1120:
1.51      djm      1121: int
1.35      mouring  1122: interactive_loop(int fd_in, int fd_out, char *file1, char *file2)
1.1       djm      1123: {
                   1124:        char *pwd;
1.35      mouring  1125:        char *dir = NULL;
1.1       djm      1126:        char cmd[2048];
1.44      djm      1127:        struct sftp_conn *conn;
1.51      djm      1128:        int err;
1.26      djm      1129:
1.44      djm      1130:        conn = do_init(fd_in, fd_out, copy_buffer_len, num_requests);
                   1131:        if (conn == NULL)
1.26      djm      1132:                fatal("Couldn't initialise connection to server");
1.1       djm      1133:
1.44      djm      1134:        pwd = do_realpath(conn, ".");
1.1       djm      1135:        if (pwd == NULL)
                   1136:                fatal("Need cwd");
                   1137:
1.35      mouring  1138:        if (file1 != NULL) {
                   1139:                dir = xstrdup(file1);
                   1140:                dir = make_absolute(dir, pwd);
                   1141:
1.44      djm      1142:                if (remote_is_dir(conn, dir) && file2 == NULL) {
1.35      mouring  1143:                        printf("Changing to: %s\n", dir);
                   1144:                        snprintf(cmd, sizeof cmd, "cd \"%s\"", dir);
1.51      djm      1145:                        if (parse_dispatch_command(conn, cmd, &pwd, 1) != 0)
                   1146:                                return (-1);
1.35      mouring  1147:                } else {
                   1148:                        if (file2 == NULL)
                   1149:                                snprintf(cmd, sizeof cmd, "get %s", dir);
                   1150:                        else
                   1151:                                snprintf(cmd, sizeof cmd, "get %s %s", dir,
                   1152:                                    file2);
                   1153:
1.51      djm      1154:                        err = parse_dispatch_command(conn, cmd, &pwd, 1);
1.45      mpech    1155:                        xfree(dir);
1.57      markus   1156:                        xfree(pwd);
1.51      djm      1157:                        return (err);
1.35      mouring  1158:                }
1.45      mpech    1159:                xfree(dir);
1.35      mouring  1160:        }
1.51      djm      1161:
1.14      stevesk  1162:        setvbuf(stdout, NULL, _IOLBF, 0);
1.25      deraadt  1163:        setvbuf(infile, NULL, _IOLBF, 0);
1.1       djm      1164:
1.51      djm      1165:        err = 0;
1.41      deraadt  1166:        for (;;) {
1.1       djm      1167:                char *cp;
                   1168:
                   1169:                printf("sftp> ");
                   1170:
                   1171:                /* XXX: use libedit */
1.25      deraadt  1172:                if (fgets(cmd, sizeof(cmd), infile) == NULL) {
1.1       djm      1173:                        printf("\n");
                   1174:                        break;
1.66    ! djm      1175:                }
        !          1176:
        !          1177:                if (batchmode) /* Echo command */
1.25      deraadt  1178:                        printf("%s", cmd);
                   1179:
1.1       djm      1180:                cp = strrchr(cmd, '\n');
                   1181:                if (cp)
                   1182:                        *cp = '\0';
1.25      deraadt  1183:
1.66    ! djm      1184:                err = parse_dispatch_command(conn, cmd, &pwd, batchmode);
1.51      djm      1185:                if (err != 0)
1.1       djm      1186:                        break;
                   1187:        }
                   1188:        xfree(pwd);
1.51      djm      1189:
                   1190:        /* err == 1 signifies normal "quit" exit */
                   1191:        return (err >= 0 ? 0 : -1);
1.1       djm      1192: }
1.51      djm      1193: