[BACK]Return to sftp.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / ssh

Annotation of src/usr.bin/ssh/sftp.c, Revision 1.209

1.209   ! djm         1: /* $OpenBSD: sftp.c,v 1.208 2021/04/03 06:18:41 djm Exp $ */
1.1       djm         2: /*
1.42      djm         3:  * Copyright (c) 2001-2004 Damien Miller <djm@openbsd.org>
1.1       djm         4:  *
1.42      djm         5:  * Permission to use, copy, modify, and distribute this software for any
                      6:  * purpose with or without fee is hereby granted, provided that the above
                      7:  * copyright notice and this permission notice appear in all copies.
1.1       djm         8:  *
1.42      djm         9:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                     10:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     11:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     12:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     13:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     14:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
                     15:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1.1       djm        16:  */
                     17:
1.91      deraadt    18: #include <sys/types.h>
1.72      stevesk    19: #include <sys/ioctl.h>
1.73      stevesk    20: #include <sys/wait.h>
1.75      stevesk    21: #include <sys/stat.h>
1.83      stevesk    22: #include <sys/socket.h>
1.100     djm        23: #include <sys/statvfs.h>
1.44      djm        24:
1.97      djm        25: #include <ctype.h>
1.85      stevesk    26: #include <errno.h>
1.44      djm        27: #include <glob.h>
1.57      djm        28: #include <histedit.h>
1.71      stevesk    29: #include <paths.h>
1.111     djm        30: #include <libgen.h>
1.146     dtucker    31: #include <locale.h>
1.74      stevesk    32: #include <signal.h>
1.174     schwarze   33: #include <stdarg.h>
1.89      stevesk    34: #include <stdlib.h>
1.90      stevesk    35: #include <stdio.h>
1.87      stevesk    36: #include <string.h>
1.86      stevesk    37: #include <unistd.h>
1.170     deraadt    38: #include <limits.h>
1.100     djm        39: #include <util.h>
1.1       djm        40:
                     41: #include "xmalloc.h"
                     42: #include "log.h"
                     43: #include "pathnames.h"
1.16      mouring    44: #include "misc.h"
1.174     schwarze   45: #include "utf8.h"
1.1       djm        46:
                     47: #include "sftp.h"
1.169     djm        48: #include "ssherr.h"
                     49: #include "sshbuf.h"
1.1       djm        50: #include "sftp-common.h"
                     51: #include "sftp-client.h"
1.43      djm        52:
1.44      djm        53: /* File to read commands from */
                     54: FILE* infile;
1.15      mouring    55:
1.44      djm        56: /* Are we in batchfile mode? */
1.39      djm        57: int batchmode = 0;
1.44      djm        58:
                     59: /* PID of ssh transport process */
1.185     bluhm      60: static volatile pid_t sshpid = -1;
1.7       markus     61:
1.209   ! djm        62: /* Suppress diagnostic messages */
1.143     djm        63: int quiet = 0;
                     64:
1.44      djm        65: /* This is set to 0 if the progressmeter is not desired. */
1.45      djm        66: int showprogress = 1;
1.44      djm        67:
1.111     djm        68: /* When this option is set, we always recursively download/upload directories */
                     69: int global_rflag = 0;
                     70:
1.159     logan      71: /* When this option is set, we resume download or upload if possible */
1.148     djm        72: int global_aflag = 0;
                     73:
1.111     djm        74: /* When this option is set, the file transfers will always preserve times */
                     75: int global_pflag = 0;
                     76:
1.156     djm        77: /* When this option is set, transfers will have fsync() called on each file */
                     78: int global_fflag = 0;
                     79:
1.46      djm        80: /* SIGINT received during command processing */
                     81: volatile sig_atomic_t interrupted = 0;
                     82:
1.52      djm        83: /* I wish qsort() took a separate ctx for the comparison function...*/
                     84: int sort_flag;
1.180     djm        85: glob_t *sort_glob;
1.52      djm        86:
1.116     djm        87: /* Context used for commandline completion */
                     88: struct complete_ctx {
                     89:        struct sftp_conn *conn;
                     90:        char **remote_pathp;
                     91: };
                     92:
1.44      djm        93: int remote_glob(struct sftp_conn *, const char *, int,
                     94:     int (*)(const char *, int), glob_t *); /* proto for sftp-glob.c */
                     95:
                     96: /* Separators for interactive commands */
                     97: #define WHITESPACE " \t\r\n"
                     98:
1.52      djm        99: /* ls flags */
1.119     djm       100: #define LS_LONG_VIEW   0x0001  /* Full view ala ls -l */
                    101: #define LS_SHORT_VIEW  0x0002  /* Single row view ala ls -1 */
                    102: #define LS_NUMERIC_VIEW        0x0004  /* Long view with numeric uid/gid */
                    103: #define LS_NAME_SORT   0x0008  /* Sort by name (default) */
                    104: #define LS_TIME_SORT   0x0010  /* Sort by mtime */
                    105: #define LS_SIZE_SORT   0x0020  /* Sort by file size */
                    106: #define LS_REVERSE_SORT        0x0040  /* Reverse sort order */
                    107: #define LS_SHOW_ALL    0x0080  /* Don't skip filenames starting with '.' */
                    108: #define LS_SI_UNITS    0x0100  /* Display sizes as K, M, G, etc. */
1.52      djm       109:
1.119     djm       110: #define VIEW_FLAGS     (LS_LONG_VIEW|LS_SHORT_VIEW|LS_NUMERIC_VIEW|LS_SI_UNITS)
1.53      djm       111: #define SORT_FLAGS     (LS_NAME_SORT|LS_TIME_SORT|LS_SIZE_SORT)
1.44      djm       112:
                    113: /* Commands for interactive mode */
1.149     djm       114: enum sftp_command {
                    115:        I_CHDIR = 1,
                    116:        I_CHGRP,
                    117:        I_CHMOD,
                    118:        I_CHOWN,
                    119:        I_DF,
                    120:        I_GET,
                    121:        I_HELP,
                    122:        I_LCHDIR,
                    123:        I_LINK,
                    124:        I_LLS,
                    125:        I_LMKDIR,
                    126:        I_LPWD,
                    127:        I_LS,
                    128:        I_LUMASK,
                    129:        I_MKDIR,
                    130:        I_PUT,
                    131:        I_PWD,
                    132:        I_QUIT,
1.160     logan     133:        I_REGET,
1.149     djm       134:        I_RENAME,
1.160     logan     135:        I_REPUT,
1.149     djm       136:        I_RM,
                    137:        I_RMDIR,
                    138:        I_SHELL,
                    139:        I_SYMLINK,
                    140:        I_VERSION,
                    141:        I_PROGRESS,
                    142: };
1.44      djm       143:
                    144: struct CMD {
                    145:        const char *c;
                    146:        const int n;
1.116     djm       147:        const int t;
1.44      djm       148: };
                    149:
1.116     djm       150: /* Type of completion */
                    151: #define NOARGS 0
                    152: #define REMOTE 1
                    153: #define LOCAL  2
                    154:
1.44      djm       155: static const struct CMD cmds[] = {
1.116     djm       156:        { "bye",        I_QUIT,         NOARGS  },
                    157:        { "cd",         I_CHDIR,        REMOTE  },
                    158:        { "chdir",      I_CHDIR,        REMOTE  },
                    159:        { "chgrp",      I_CHGRP,        REMOTE  },
                    160:        { "chmod",      I_CHMOD,        REMOTE  },
                    161:        { "chown",      I_CHOWN,        REMOTE  },
                    162:        { "df",         I_DF,           REMOTE  },
                    163:        { "dir",        I_LS,           REMOTE  },
                    164:        { "exit",       I_QUIT,         NOARGS  },
                    165:        { "get",        I_GET,          REMOTE  },
                    166:        { "help",       I_HELP,         NOARGS  },
                    167:        { "lcd",        I_LCHDIR,       LOCAL   },
                    168:        { "lchdir",     I_LCHDIR,       LOCAL   },
                    169:        { "lls",        I_LLS,          LOCAL   },
                    170:        { "lmkdir",     I_LMKDIR,       LOCAL   },
1.132     djm       171:        { "ln",         I_LINK,         REMOTE  },
1.116     djm       172:        { "lpwd",       I_LPWD,         LOCAL   },
                    173:        { "ls",         I_LS,           REMOTE  },
                    174:        { "lumask",     I_LUMASK,       NOARGS  },
                    175:        { "mkdir",      I_MKDIR,        REMOTE  },
1.124     dtucker   176:        { "mget",       I_GET,          REMOTE  },
                    177:        { "mput",       I_PUT,          LOCAL   },
1.116     djm       178:        { "progress",   I_PROGRESS,     NOARGS  },
                    179:        { "put",        I_PUT,          LOCAL   },
                    180:        { "pwd",        I_PWD,          REMOTE  },
                    181:        { "quit",       I_QUIT,         NOARGS  },
1.148     djm       182:        { "reget",      I_REGET,        REMOTE  },
1.116     djm       183:        { "rename",     I_RENAME,       REMOTE  },
1.167     djm       184:        { "reput",      I_REPUT,        LOCAL   },
1.116     djm       185:        { "rm",         I_RM,           REMOTE  },
                    186:        { "rmdir",      I_RMDIR,        REMOTE  },
                    187:        { "symlink",    I_SYMLINK,      REMOTE  },
                    188:        { "version",    I_VERSION,      NOARGS  },
                    189:        { "!",          I_SHELL,        NOARGS  },
                    190:        { "?",          I_HELP,         NOARGS  },
                    191:        { NULL,         -1,             -1      }
1.44      djm       192: };
                    193:
1.96      stevesk   194: /* ARGSUSED */
1.44      djm       195: static void
1.46      djm       196: killchild(int signo)
                    197: {
1.196     djm       198:        pid_t pid;
                    199:
                    200:        pid = sshpid;
                    201:        if (pid > 1) {
                    202:                kill(pid, SIGTERM);
                    203:                waitpid(pid, NULL, 0);
1.61      dtucker   204:        }
1.46      djm       205:
                    206:        _exit(1);
                    207: }
                    208:
1.96      stevesk   209: /* ARGSUSED */
1.46      djm       210: static void
1.177     millert   211: suspchild(int signo)
                    212: {
                    213:        if (sshpid > 1) {
                    214:                kill(sshpid, signo);
                    215:                while (waitpid(sshpid, NULL, WUNTRACED) == -1 && errno == EINTR)
                    216:                        continue;
                    217:        }
                    218:        kill(getpid(), SIGSTOP);
                    219: }
                    220:
                    221: /* ARGSUSED */
                    222: static void
1.46      djm       223: cmd_interrupt(int signo)
                    224: {
                    225:        const char msg[] = "\rInterrupt  \n";
1.59      djm       226:        int olderrno = errno;
1.46      djm       227:
1.144     dtucker   228:        (void)write(STDERR_FILENO, msg, sizeof(msg) - 1);
1.46      djm       229:        interrupted = 1;
1.59      djm       230:        errno = olderrno;
1.46      djm       231: }
                    232:
1.184     djm       233: /*ARGSUSED*/
                    234: static void
                    235: sigchld_handler(int sig)
                    236: {
                    237:        int save_errno = errno;
                    238:        pid_t pid;
                    239:        const char msg[] = "\rConnection closed.  \n";
                    240:
                    241:        /* Report if ssh transport process dies. */
                    242:        while ((pid = waitpid(sshpid, NULL, WNOHANG)) == -1 && errno == EINTR)
                    243:                continue;
1.185     bluhm     244:        if (pid == sshpid) {
1.184     djm       245:                (void)write(STDERR_FILENO, msg, sizeof(msg) - 1);
1.185     bluhm     246:                sshpid = -1;
                    247:        }
1.184     djm       248:
                    249:        errno = save_errno;
                    250: }
                    251:
1.46      djm       252: static void
1.44      djm       253: help(void)
                    254: {
1.106     sobrado   255:        printf("Available commands:\n"
                    256:            "bye                                Quit sftp\n"
                    257:            "cd path                            Change remote directory to 'path'\n"
1.189     djm       258:            "chgrp [-h] grp path                Change group of file 'path' to 'grp'\n"
                    259:            "chmod [-h] mode path               Change permissions of file 'path' to 'mode'\n"
                    260:            "chown [-h] own path                Change owner of file 'path' to 'own'\n"
1.106     sobrado   261:            "df [-hi] [path]                    Display statistics for current directory or\n"
                    262:            "                                   filesystem containing 'path'\n"
                    263:            "exit                               Quit sftp\n"
1.193     jmc       264:            "get [-afpR] remote [local]         Download file\n"
1.106     sobrado   265:            "help                               Display this help text\n"
                    266:            "lcd path                           Change local directory to 'path'\n"
                    267:            "lls [ls-options [path]]            Display local directory listing\n"
                    268:            "lmkdir path                        Create local directory\n"
1.132     djm       269:            "ln [-s] oldpath newpath            Link remote file (-s for symlink)\n"
1.106     sobrado   270:            "lpwd                               Print local working directory\n"
1.121     jmc       271:            "ls [-1afhlnrSt] [path]             Display remote directory listing\n"
1.106     sobrado   272:            "lumask umask                       Set local umask to 'umask'\n"
                    273:            "mkdir path                         Create remote directory\n"
                    274:            "progress                           Toggle display of progress meter\n"
1.193     jmc       275:            "put [-afpR] local [remote]         Upload file\n"
1.106     sobrado   276:            "pwd                                Display remote working directory\n"
                    277:            "quit                               Quit sftp\n"
1.193     jmc       278:            "reget [-fpR] remote [local]        Resume download file\n"
1.106     sobrado   279:            "rename oldpath newpath             Rename remote file\n"
1.193     jmc       280:            "reput [-fpR] local [remote]        Resume upload file\n"
1.106     sobrado   281:            "rm path                            Delete remote file\n"
                    282:            "rmdir path                         Remove remote directory\n"
                    283:            "symlink oldpath newpath            Symlink remote file\n"
                    284:            "version                            Show SFTP version\n"
                    285:            "!command                           Execute 'command' in local shell\n"
                    286:            "!                                  Escape to local shell\n"
                    287:            "?                                  Synonym for help\n");
1.44      djm       288: }
                    289:
                    290: static void
                    291: local_do_shell(const char *args)
                    292: {
                    293:        int status;
                    294:        char *shell;
                    295:        pid_t pid;
                    296:
                    297:        if (!*args)
                    298:                args = NULL;
                    299:
1.130     djm       300:        if ((shell = getenv("SHELL")) == NULL || *shell == '\0')
1.44      djm       301:                shell = _PATH_BSHELL;
                    302:
                    303:        if ((pid = fork()) == -1)
                    304:                fatal("Couldn't fork: %s", strerror(errno));
                    305:
                    306:        if (pid == 0) {
                    307:                /* XXX: child has pipe fds to ssh subproc open - issue? */
                    308:                if (args) {
                    309:                        debug3("Executing %s -c \"%s\"", shell, args);
                    310:                        execl(shell, shell, "-c", args, (char *)NULL);
                    311:                } else {
                    312:                        debug3("Executing %s", shell);
                    313:                        execl(shell, shell, (char *)NULL);
                    314:                }
                    315:                fprintf(stderr, "Couldn't execute \"%s\": %s\n", shell,
                    316:                    strerror(errno));
                    317:                _exit(1);
                    318:        }
                    319:        while (waitpid(pid, &status, 0) == -1)
                    320:                if (errno != EINTR)
                    321:                        fatal("Couldn't wait for child: %s", strerror(errno));
                    322:        if (!WIFEXITED(status))
1.78      djm       323:                error("Shell exited abnormally");
1.44      djm       324:        else if (WEXITSTATUS(status))
                    325:                error("Shell exited with status %d", WEXITSTATUS(status));
                    326: }
                    327:
                    328: static void
                    329: local_do_ls(const char *args)
                    330: {
                    331:        if (!args || !*args)
                    332:                local_do_shell(_PATH_LS);
                    333:        else {
                    334:                int len = strlen(_PATH_LS " ") + strlen(args) + 1;
                    335:                char *buf = xmalloc(len);
                    336:
                    337:                /* XXX: quoting - rip quoting code from ftp? */
                    338:                snprintf(buf, len, _PATH_LS " %s", args);
                    339:                local_do_shell(buf);
1.145     djm       340:                free(buf);
1.44      djm       341:        }
                    342: }
                    343:
                    344: /* Strip one path (usually the pwd) from the start of another */
                    345: static char *
1.175     djm       346: path_strip(const char *path, const char *strip)
1.44      djm       347: {
                    348:        size_t len;
                    349:
                    350:        if (strip == NULL)
                    351:                return (xstrdup(path));
                    352:
                    353:        len = strlen(strip);
1.59      djm       354:        if (strncmp(path, strip, len) == 0) {
1.44      djm       355:                if (strip[len - 1] != '/' && path[len] == '/')
                    356:                        len++;
                    357:                return (xstrdup(path + len));
                    358:        }
                    359:
                    360:        return (xstrdup(path));
                    361: }
                    362:
                    363: static int
1.148     djm       364: parse_getput_flags(const char *cmd, char **argv, int argc,
1.156     djm       365:     int *aflag, int *fflag, int *pflag, int *rflag)
1.44      djm       366: {
1.102     martynas  367:        extern int opterr, optind, optopt, optreset;
1.97      djm       368:        int ch;
1.44      djm       369:
1.97      djm       370:        optind = optreset = 1;
                    371:        opterr = 0;
                    372:
1.156     djm       373:        *aflag = *fflag = *rflag = *pflag = 0;
                    374:        while ((ch = getopt(argc, argv, "afPpRr")) != -1) {
1.97      djm       375:                switch (ch) {
1.148     djm       376:                case 'a':
                    377:                        *aflag = 1;
                    378:                        break;
1.156     djm       379:                case 'f':
                    380:                        *fflag = 1;
                    381:                        break;
1.44      djm       382:                case 'p':
                    383:                case 'P':
                    384:                        *pflag = 1;
                    385:                        break;
1.111     djm       386:                case 'r':
                    387:                case 'R':
                    388:                        *rflag = 1;
                    389:                        break;
1.44      djm       390:                default:
1.102     martynas  391:                        error("%s: Invalid flag -%c", cmd, optopt);
1.97      djm       392:                        return -1;
1.44      djm       393:                }
                    394:        }
                    395:
1.97      djm       396:        return optind;
1.44      djm       397: }
                    398:
                    399: static int
1.132     djm       400: parse_link_flags(const char *cmd, char **argv, int argc, int *sflag)
                    401: {
                    402:        extern int opterr, optind, optopt, optreset;
                    403:        int ch;
                    404:
                    405:        optind = optreset = 1;
                    406:        opterr = 0;
                    407:
                    408:        *sflag = 0;
                    409:        while ((ch = getopt(argc, argv, "s")) != -1) {
                    410:                switch (ch) {
                    411:                case 's':
                    412:                        *sflag = 1;
                    413:                        break;
                    414:                default:
                    415:                        error("%s: Invalid flag -%c", cmd, optopt);
                    416:                        return -1;
                    417:                }
                    418:        }
                    419:
                    420:        return optind;
                    421: }
                    422:
                    423: static int
1.152     djm       424: parse_rename_flags(const char *cmd, char **argv, int argc, int *lflag)
                    425: {
                    426:        extern int opterr, optind, optopt, optreset;
                    427:        int ch;
                    428:
                    429:        optind = optreset = 1;
                    430:        opterr = 0;
                    431:
                    432:        *lflag = 0;
                    433:        while ((ch = getopt(argc, argv, "l")) != -1) {
                    434:                switch (ch) {
                    435:                case 'l':
                    436:                        *lflag = 1;
                    437:                        break;
                    438:                default:
                    439:                        error("%s: Invalid flag -%c", cmd, optopt);
                    440:                        return -1;
                    441:                }
                    442:        }
                    443:
                    444:        return optind;
                    445: }
                    446:
                    447: static int
1.97      djm       448: parse_ls_flags(char **argv, int argc, int *lflag)
1.44      djm       449: {
1.102     martynas  450:        extern int opterr, optind, optopt, optreset;
1.97      djm       451:        int ch;
                    452:
                    453:        optind = optreset = 1;
                    454:        opterr = 0;
1.44      djm       455:
1.53      djm       456:        *lflag = LS_NAME_SORT;
1.119     djm       457:        while ((ch = getopt(argc, argv, "1Safhlnrt")) != -1) {
1.97      djm       458:                switch (ch) {
                    459:                case '1':
                    460:                        *lflag &= ~VIEW_FLAGS;
                    461:                        *lflag |= LS_SHORT_VIEW;
                    462:                        break;
                    463:                case 'S':
                    464:                        *lflag &= ~SORT_FLAGS;
                    465:                        *lflag |= LS_SIZE_SORT;
                    466:                        break;
                    467:                case 'a':
                    468:                        *lflag |= LS_SHOW_ALL;
                    469:                        break;
                    470:                case 'f':
                    471:                        *lflag &= ~SORT_FLAGS;
                    472:                        break;
1.119     djm       473:                case 'h':
                    474:                        *lflag |= LS_SI_UNITS;
                    475:                        break;
1.97      djm       476:                case 'l':
1.119     djm       477:                        *lflag &= ~LS_SHORT_VIEW;
1.97      djm       478:                        *lflag |= LS_LONG_VIEW;
                    479:                        break;
                    480:                case 'n':
1.119     djm       481:                        *lflag &= ~LS_SHORT_VIEW;
1.97      djm       482:                        *lflag |= LS_NUMERIC_VIEW|LS_LONG_VIEW;
                    483:                        break;
                    484:                case 'r':
                    485:                        *lflag |= LS_REVERSE_SORT;
                    486:                        break;
                    487:                case 't':
                    488:                        *lflag &= ~SORT_FLAGS;
                    489:                        *lflag |= LS_TIME_SORT;
                    490:                        break;
                    491:                default:
1.102     martynas  492:                        error("ls: Invalid flag -%c", optopt);
1.97      djm       493:                        return -1;
1.44      djm       494:                }
                    495:        }
                    496:
1.97      djm       497:        return optind;
1.44      djm       498: }
                    499:
                    500: static int
1.100     djm       501: parse_df_flags(const char *cmd, char **argv, int argc, int *hflag, int *iflag)
                    502: {
1.102     martynas  503:        extern int opterr, optind, optopt, optreset;
1.100     djm       504:        int ch;
                    505:
                    506:        optind = optreset = 1;
                    507:        opterr = 0;
                    508:
                    509:        *hflag = *iflag = 0;
                    510:        while ((ch = getopt(argc, argv, "hi")) != -1) {
                    511:                switch (ch) {
                    512:                case 'h':
                    513:                        *hflag = 1;
                    514:                        break;
                    515:                case 'i':
                    516:                        *iflag = 1;
                    517:                        break;
                    518:                default:
1.102     martynas  519:                        error("%s: Invalid flag -%c", cmd, optopt);
1.100     djm       520:                        return -1;
                    521:                }
                    522:        }
                    523:
                    524:        return optind;
                    525: }
                    526:
                    527: static int
1.189     djm       528: parse_ch_flags(const char *cmd, char **argv, int argc, int *hflag)
                    529: {
                    530:        extern int opterr, optind, optopt, optreset;
                    531:        int ch;
                    532:
                    533:        optind = optreset = 1;
                    534:        opterr = 0;
                    535:
                    536:        *hflag = 0;
                    537:        while ((ch = getopt(argc, argv, "h")) != -1) {
                    538:                switch (ch) {
                    539:                case 'h':
                    540:                        *hflag = 1;
                    541:                        break;
                    542:                default:
                    543:                        error("%s: Invalid flag -%c", cmd, optopt);
                    544:                        return -1;
                    545:                }
                    546:        }
                    547:
                    548:        return optind;
                    549: }
                    550:
                    551: static int
1.153     djm       552: parse_no_flags(const char *cmd, char **argv, int argc)
                    553: {
                    554:        extern int opterr, optind, optopt, optreset;
                    555:        int ch;
                    556:
                    557:        optind = optreset = 1;
                    558:        opterr = 0;
                    559:
                    560:        while ((ch = getopt(argc, argv, "")) != -1) {
                    561:                switch (ch) {
                    562:                default:
                    563:                        error("%s: Invalid flag -%c", cmd, optopt);
                    564:                        return -1;
                    565:                }
                    566:        }
                    567:
                    568:        return optind;
                    569: }
                    570:
                    571: static int
1.175     djm       572: process_get(struct sftp_conn *conn, const char *src, const char *dst,
                    573:     const char *pwd, int pflag, int rflag, int resume, int fflag)
1.44      djm       574: {
                    575:        char *abs_src = NULL;
                    576:        char *abs_dst = NULL;
                    577:        glob_t g;
1.111     djm       578:        char *filename, *tmp=NULL;
1.164     djm       579:        int i, r, err = 0;
1.44      djm       580:
                    581:        abs_src = xstrdup(src);
                    582:        abs_src = make_absolute(abs_src, pwd);
1.111     djm       583:        memset(&g, 0, sizeof(g));
1.44      djm       584:
                    585:        debug3("Looking up %s", abs_src);
1.164     djm       586:        if ((r = remote_glob(conn, abs_src, GLOB_MARK, NULL, &g)) != 0) {
                    587:                if (r == GLOB_NOSPACE) {
                    588:                        error("Too many matches for \"%s\".", abs_src);
                    589:                } else {
                    590:                        error("File \"%s\" not found.", abs_src);
                    591:                }
1.44      djm       592:                err = -1;
                    593:                goto out;
                    594:        }
                    595:
1.111     djm       596:        /*
                    597:         * If multiple matches then dst must be a directory or
                    598:         * unspecified.
                    599:         */
1.205     djm       600:        if (g.gl_matchc > 1 && dst != NULL && !local_is_dir(dst)) {
1.111     djm       601:                error("Multiple source paths, but destination "
                    602:                    "\"%s\" is not a directory", dst);
1.44      djm       603:                err = -1;
                    604:                goto out;
                    605:        }
                    606:
1.46      djm       607:        for (i = 0; g.gl_pathv[i] && !interrupted; i++) {
1.111     djm       608:                tmp = xstrdup(g.gl_pathv[i]);
                    609:                if ((filename = basename(tmp)) == NULL) {
                    610:                        error("basename %s: %s", tmp, strerror(errno));
1.145     djm       611:                        free(tmp);
1.44      djm       612:                        err = -1;
                    613:                        goto out;
                    614:                }
                    615:
                    616:                if (g.gl_matchc == 1 && dst) {
1.205     djm       617:                        if (local_is_dir(dst)) {
1.111     djm       618:                                abs_dst = path_append(dst, filename);
                    619:                        } else {
1.44      djm       620:                                abs_dst = xstrdup(dst);
1.111     djm       621:                        }
1.44      djm       622:                } else if (dst) {
1.111     djm       623:                        abs_dst = path_append(dst, filename);
                    624:                } else {
                    625:                        abs_dst = xstrdup(filename);
                    626:                }
1.145     djm       627:                free(tmp);
1.44      djm       628:
1.148     djm       629:                resume |= global_aflag;
                    630:                if (!quiet && resume)
1.174     schwarze  631:                        mprintf("Resuming %s to %s\n",
                    632:                            g.gl_pathv[i], abs_dst);
1.148     djm       633:                else if (!quiet && !resume)
1.174     schwarze  634:                        mprintf("Fetching %s to %s\n",
                    635:                            g.gl_pathv[i], abs_dst);
1.205     djm       636:                if (globpath_is_dir(g.gl_pathv[i]) && (rflag || global_rflag)) {
1.148     djm       637:                        if (download_dir(conn, g.gl_pathv[i], abs_dst, NULL,
1.156     djm       638:                            pflag || global_pflag, 1, resume,
                    639:                            fflag || global_fflag) == -1)
1.111     djm       640:                                err = -1;
                    641:                } else {
                    642:                        if (do_download(conn, g.gl_pathv[i], abs_dst, NULL,
1.156     djm       643:                            pflag || global_pflag, resume,
                    644:                            fflag || global_fflag) == -1)
1.111     djm       645:                                err = -1;
                    646:                }
1.145     djm       647:                free(abs_dst);
1.44      djm       648:                abs_dst = NULL;
                    649:        }
                    650:
                    651: out:
1.145     djm       652:        free(abs_src);
1.44      djm       653:        globfree(&g);
                    654:        return(err);
                    655: }
                    656:
                    657: static int
1.175     djm       658: process_put(struct sftp_conn *conn, const char *src, const char *dst,
                    659:     const char *pwd, int pflag, int rflag, int resume, int fflag)
1.44      djm       660: {
                    661:        char *tmp_dst = NULL;
                    662:        char *abs_dst = NULL;
1.111     djm       663:        char *tmp = NULL, *filename = NULL;
1.44      djm       664:        glob_t g;
                    665:        int err = 0;
1.111     djm       666:        int i, dst_is_dir = 1;
1.99      djm       667:        struct stat sb;
1.44      djm       668:
                    669:        if (dst) {
                    670:                tmp_dst = xstrdup(dst);
                    671:                tmp_dst = make_absolute(tmp_dst, pwd);
                    672:        }
                    673:
                    674:        memset(&g, 0, sizeof(g));
                    675:        debug3("Looking up %s", src);
1.111     djm       676:        if (glob(src, GLOB_NOCHECK | GLOB_MARK, NULL, &g)) {
1.44      djm       677:                error("File \"%s\" not found.", src);
                    678:                err = -1;
                    679:                goto out;
                    680:        }
                    681:
1.111     djm       682:        /* If we aren't fetching to pwd then stash this status for later */
                    683:        if (tmp_dst != NULL)
                    684:                dst_is_dir = remote_is_dir(conn, tmp_dst);
                    685:
1.44      djm       686:        /* If multiple matches, dst may be directory or unspecified */
1.111     djm       687:        if (g.gl_matchc > 1 && tmp_dst && !dst_is_dir) {
                    688:                error("Multiple paths match, but destination "
                    689:                    "\"%s\" is not a directory", tmp_dst);
1.44      djm       690:                err = -1;
                    691:                goto out;
                    692:        }
                    693:
1.46      djm       694:        for (i = 0; g.gl_pathv[i] && !interrupted; i++) {
1.99      djm       695:                if (stat(g.gl_pathv[i], &sb) == -1) {
                    696:                        err = -1;
                    697:                        error("stat %s: %s", g.gl_pathv[i], strerror(errno));
                    698:                        continue;
                    699:                }
1.149     djm       700:
1.111     djm       701:                tmp = xstrdup(g.gl_pathv[i]);
                    702:                if ((filename = basename(tmp)) == NULL) {
                    703:                        error("basename %s: %s", tmp, strerror(errno));
1.145     djm       704:                        free(tmp);
1.44      djm       705:                        err = -1;
                    706:                        goto out;
                    707:                }
                    708:
                    709:                if (g.gl_matchc == 1 && tmp_dst) {
                    710:                        /* If directory specified, append filename */
1.111     djm       711:                        if (dst_is_dir)
                    712:                                abs_dst = path_append(tmp_dst, filename);
                    713:                        else
1.44      djm       714:                                abs_dst = xstrdup(tmp_dst);
                    715:                } else if (tmp_dst) {
1.111     djm       716:                        abs_dst = path_append(tmp_dst, filename);
                    717:                } else {
                    718:                        abs_dst = make_absolute(xstrdup(filename), pwd);
                    719:                }
1.145     djm       720:                free(tmp);
1.44      djm       721:
1.208     djm       722:                resume |= global_aflag;
1.159     logan     723:                if (!quiet && resume)
1.174     schwarze  724:                        mprintf("Resuming upload of %s to %s\n",
                    725:                            g.gl_pathv[i], abs_dst);
1.159     logan     726:                else if (!quiet && !resume)
1.174     schwarze  727:                        mprintf("Uploading %s to %s\n",
                    728:                            g.gl_pathv[i], abs_dst);
1.205     djm       729:                if (globpath_is_dir(g.gl_pathv[i]) && (rflag || global_rflag)) {
1.111     djm       730:                        if (upload_dir(conn, g.gl_pathv[i], abs_dst,
1.159     logan     731:                            pflag || global_pflag, 1, resume,
1.156     djm       732:                            fflag || global_fflag) == -1)
1.111     djm       733:                                err = -1;
                    734:                } else {
                    735:                        if (do_upload(conn, g.gl_pathv[i], abs_dst,
1.159     logan     736:                            pflag || global_pflag, resume,
1.156     djm       737:                            fflag || global_fflag) == -1)
1.111     djm       738:                                err = -1;
                    739:                }
1.44      djm       740:        }
                    741:
                    742: out:
1.145     djm       743:        free(abs_dst);
                    744:        free(tmp_dst);
1.44      djm       745:        globfree(&g);
                    746:        return(err);
                    747: }
                    748:
                    749: static int
                    750: sdirent_comp(const void *aa, const void *bb)
                    751: {
                    752:        SFTP_DIRENT *a = *(SFTP_DIRENT **)aa;
                    753:        SFTP_DIRENT *b = *(SFTP_DIRENT **)bb;
1.53      djm       754:        int rmul = sort_flag & LS_REVERSE_SORT ? -1 : 1;
1.44      djm       755:
1.52      djm       756: #define NCMP(a,b) (a == b ? 0 : (a < b ? 1 : -1))
1.53      djm       757:        if (sort_flag & LS_NAME_SORT)
1.52      djm       758:                return (rmul * strcmp(a->filename, b->filename));
1.53      djm       759:        else if (sort_flag & LS_TIME_SORT)
1.52      djm       760:                return (rmul * NCMP(a->a.mtime, b->a.mtime));
1.53      djm       761:        else if (sort_flag & LS_SIZE_SORT)
1.52      djm       762:                return (rmul * NCMP(a->a.size, b->a.size));
                    763:
                    764:        fatal("Unknown ls sort type");
1.44      djm       765: }
                    766:
                    767: /* sftp ls.1 replacement for directories */
                    768: static int
1.175     djm       769: do_ls_dir(struct sftp_conn *conn, const char *path,
                    770:     const char *strip_path, int lflag)
1.44      djm       771: {
1.64      djm       772:        int n;
                    773:        u_int c = 1, colspace = 0, columns = 1;
1.44      djm       774:        SFTP_DIRENT **d;
                    775:
                    776:        if ((n = do_readdir(conn, path, &d)) != 0)
                    777:                return (n);
                    778:
1.53      djm       779:        if (!(lflag & LS_SHORT_VIEW)) {
1.64      djm       780:                u_int m = 0, width = 80;
1.44      djm       781:                struct winsize ws;
                    782:                char *tmp;
                    783:
                    784:                /* Count entries for sort and find longest filename */
1.54      djm       785:                for (n = 0; d[n] != NULL; n++) {
                    786:                        if (d[n]->filename[0] != '.' || (lflag & LS_SHOW_ALL))
1.176     deraadt   787:                                m = MAXIMUM(m, strlen(d[n]->filename));
1.54      djm       788:                }
1.44      djm       789:
                    790:                /* Add any subpath that also needs to be counted */
                    791:                tmp = path_strip(path, strip_path);
                    792:                m += strlen(tmp);
1.145     djm       793:                free(tmp);
1.44      djm       794:
                    795:                if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) != -1)
                    796:                        width = ws.ws_col;
                    797:
                    798:                columns = width / (m + 2);
1.176     deraadt   799:                columns = MAXIMUM(columns, 1);
1.44      djm       800:                colspace = width / columns;
1.176     deraadt   801:                colspace = MINIMUM(colspace, width);
1.44      djm       802:        }
                    803:
1.52      djm       804:        if (lflag & SORT_FLAGS) {
1.68      dtucker   805:                for (n = 0; d[n] != NULL; n++)
                    806:                        ;       /* count entries */
1.53      djm       807:                sort_flag = lflag & (SORT_FLAGS|LS_REVERSE_SORT);
1.52      djm       808:                qsort(d, n, sizeof(*d), sdirent_comp);
                    809:        }
1.44      djm       810:
1.46      djm       811:        for (n = 0; d[n] != NULL && !interrupted; n++) {
1.44      djm       812:                char *tmp, *fname;
1.54      djm       813:
                    814:                if (d[n]->filename[0] == '.' && !(lflag & LS_SHOW_ALL))
                    815:                        continue;
1.44      djm       816:
                    817:                tmp = path_append(path, d[n]->filename);
                    818:                fname = path_strip(tmp, strip_path);
1.145     djm       819:                free(tmp);
1.44      djm       820:
1.53      djm       821:                if (lflag & LS_LONG_VIEW) {
1.119     djm       822:                        if (lflag & (LS_NUMERIC_VIEW|LS_SI_UNITS)) {
1.50      djm       823:                                char *lname;
                    824:                                struct stat sb;
                    825:
                    826:                                memset(&sb, 0, sizeof(sb));
                    827:                                attrib_to_stat(&d[n]->a, &sb);
1.119     djm       828:                                lname = ls_file(fname, &sb, 1,
                    829:                                    (lflag & LS_SI_UNITS));
1.174     schwarze  830:                                mprintf("%s\n", lname);
1.145     djm       831:                                free(lname);
1.50      djm       832:                        } else
1.174     schwarze  833:                                mprintf("%s\n", d[n]->longname);
1.44      djm       834:                } else {
1.174     schwarze  835:                        mprintf("%-*s", colspace, fname);
1.44      djm       836:                        if (c >= columns) {
                    837:                                printf("\n");
                    838:                                c = 1;
                    839:                        } else
                    840:                                c++;
                    841:                }
                    842:
1.145     djm       843:                free(fname);
1.44      djm       844:        }
                    845:
1.53      djm       846:        if (!(lflag & LS_LONG_VIEW) && (c != 1))
1.44      djm       847:                printf("\n");
                    848:
                    849:        free_sftp_dirents(d);
                    850:        return (0);
                    851: }
                    852:
1.180     djm       853: static int
                    854: sglob_comp(const void *aa, const void *bb)
                    855: {
                    856:        u_int a = *(const u_int *)aa;
                    857:        u_int b = *(const u_int *)bb;
                    858:        const char *ap = sort_glob->gl_pathv[a];
                    859:        const char *bp = sort_glob->gl_pathv[b];
                    860:        const struct stat *as = sort_glob->gl_statv[a];
                    861:        const struct stat *bs = sort_glob->gl_statv[b];
                    862:        int rmul = sort_flag & LS_REVERSE_SORT ? -1 : 1;
                    863:
                    864: #define NCMP(a,b) (a == b ? 0 : (a < b ? 1 : -1))
                    865:        if (sort_flag & LS_NAME_SORT)
                    866:                return (rmul * strcmp(ap, bp));
1.206     djm       867:        else if (sort_flag & LS_TIME_SORT) {
                    868:                if (timespeccmp(&as->st_mtim, &bs->st_mtim, ==))
                    869:                        return 0;
                    870:                return timespeccmp(&as->st_mtim, &bs->st_mtim, <) ?
                    871:                    rmul : -rmul;
                    872:        } else if (sort_flag & LS_SIZE_SORT)
1.180     djm       873:                return (rmul * NCMP(as->st_size, bs->st_size));
                    874:
                    875:        fatal("Unknown ls sort type");
                    876: }
                    877:
1.44      djm       878: /* sftp ls.1 replacement which handles path globs */
                    879: static int
1.175     djm       880: do_globbed_ls(struct sftp_conn *conn, const char *path,
                    881:     const char *strip_path, int lflag)
1.44      djm       882: {
1.129     djm       883:        char *fname, *lname;
1.44      djm       884:        glob_t g;
1.164     djm       885:        int err, r;
1.129     djm       886:        struct winsize ws;
1.180     djm       887:        u_int i, j, nentries, *indices = NULL, c = 1;
                    888:        u_int colspace = 0, columns = 1, m = 0, width = 80;
1.44      djm       889:
                    890:        memset(&g, 0, sizeof(g));
                    891:
1.164     djm       892:        if ((r = remote_glob(conn, path,
1.133     djm       893:            GLOB_MARK|GLOB_NOCHECK|GLOB_BRACE|GLOB_KEEPSTAT|GLOB_NOSORT,
1.164     djm       894:            NULL, &g)) != 0 ||
1.128     djm       895:            (g.gl_pathc && !g.gl_matchc)) {
1.60      fgsch     896:                if (g.gl_pathc)
                    897:                        globfree(&g);
1.164     djm       898:                if (r == GLOB_NOSPACE) {
                    899:                        error("Can't ls: Too many matches for \"%s\"", path);
                    900:                } else {
                    901:                        error("Can't ls: \"%s\" not found", path);
                    902:                }
1.128     djm       903:                return -1;
1.44      djm       904:        }
                    905:
1.46      djm       906:        if (interrupted)
                    907:                goto out;
                    908:
1.44      djm       909:        /*
1.60      fgsch     910:         * If the glob returns a single match and it is a directory,
                    911:         * then just list its contents.
1.44      djm       912:         */
1.128     djm       913:        if (g.gl_matchc == 1 && g.gl_statv[0] != NULL &&
                    914:            S_ISDIR(g.gl_statv[0]->st_mode)) {
                    915:                err = do_ls_dir(conn, g.gl_pathv[0], strip_path, lflag);
                    916:                globfree(&g);
                    917:                return err;
1.44      djm       918:        }
                    919:
1.129     djm       920:        if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) != -1)
                    921:                width = ws.ws_col;
                    922:
1.53      djm       923:        if (!(lflag & LS_SHORT_VIEW)) {
1.44      djm       924:                /* Count entries for sort and find longest filename */
                    925:                for (i = 0; g.gl_pathv[i]; i++)
1.176     deraadt   926:                        m = MAXIMUM(m, strlen(g.gl_pathv[i]));
1.44      djm       927:
                    928:                columns = width / (m + 2);
1.176     deraadt   929:                columns = MAXIMUM(columns, 1);
1.44      djm       930:                colspace = width / columns;
                    931:        }
                    932:
1.180     djm       933:        /*
                    934:         * Sorting: rather than mess with the contents of glob_t, prepare
                    935:         * an array of indices into it and sort that. For the usual
                    936:         * unsorted case, the indices are just the identity 1=1, 2=2, etc.
                    937:         */
                    938:        for (nentries = 0; g.gl_pathv[nentries] != NULL; nentries++)
                    939:                ;       /* count entries */
                    940:        indices = calloc(nentries, sizeof(*indices));
                    941:        for (i = 0; i < nentries; i++)
                    942:                indices[i] = i;
                    943:
                    944:        if (lflag & SORT_FLAGS) {
                    945:                sort_glob = &g;
                    946:                sort_flag = lflag & (SORT_FLAGS|LS_REVERSE_SORT);
                    947:                qsort(indices, nentries, sizeof(*indices), sglob_comp);
                    948:                sort_glob = NULL;
                    949:        }
                    950:
                    951:        for (j = 0; j < nentries && !interrupted; j++) {
                    952:                i = indices[j];
1.44      djm       953:                fname = path_strip(g.gl_pathv[i], strip_path);
1.53      djm       954:                if (lflag & LS_LONG_VIEW) {
1.128     djm       955:                        if (g.gl_statv[i] == NULL) {
                    956:                                error("no stat information for %s", fname);
                    957:                                continue;
                    958:                        }
                    959:                        lname = ls_file(fname, g.gl_statv[i], 1,
                    960:                            (lflag & LS_SI_UNITS));
1.174     schwarze  961:                        mprintf("%s\n", lname);
1.145     djm       962:                        free(lname);
1.44      djm       963:                } else {
1.174     schwarze  964:                        mprintf("%-*s", colspace, fname);
1.44      djm       965:                        if (c >= columns) {
                    966:                                printf("\n");
                    967:                                c = 1;
                    968:                        } else
                    969:                                c++;
                    970:                }
1.145     djm       971:                free(fname);
1.44      djm       972:        }
                    973:
1.53      djm       974:        if (!(lflag & LS_LONG_VIEW) && (c != 1))
1.44      djm       975:                printf("\n");
                    976:
1.46      djm       977:  out:
1.44      djm       978:        if (g.gl_pathc)
                    979:                globfree(&g);
1.180     djm       980:        free(indices);
1.44      djm       981:
1.128     djm       982:        return 0;
1.44      djm       983: }
                    984:
1.100     djm       985: static int
1.175     djm       986: do_df(struct sftp_conn *conn, const char *path, int hflag, int iflag)
1.100     djm       987: {
1.101     dtucker   988:        struct sftp_statvfs st;
1.178     djm       989:        char s_used[FMT_SCALED_STRSIZE], s_avail[FMT_SCALED_STRSIZE];
                    990:        char s_root[FMT_SCALED_STRSIZE], s_total[FMT_SCALED_STRSIZE];
                    991:        char s_icapacity[16], s_dcapacity[16];
1.100     djm       992:
                    993:        if (do_statvfs(conn, path, &st, 1) == -1)
                    994:                return -1;
1.178     djm       995:        if (st.f_files == 0)
                    996:                strlcpy(s_icapacity, "ERR", sizeof(s_icapacity));
                    997:        else {
                    998:                snprintf(s_icapacity, sizeof(s_icapacity), "%3llu%%",
                    999:                    (unsigned long long)(100 * (st.f_files - st.f_ffree) /
                   1000:                    st.f_files));
                   1001:        }
                   1002:        if (st.f_blocks == 0)
                   1003:                strlcpy(s_dcapacity, "ERR", sizeof(s_dcapacity));
                   1004:        else {
                   1005:                snprintf(s_dcapacity, sizeof(s_dcapacity), "%3llu%%",
                   1006:                    (unsigned long long)(100 * (st.f_blocks - st.f_bfree) /
                   1007:                    st.f_blocks));
                   1008:        }
1.100     djm      1009:        if (iflag) {
                   1010:                printf("     Inodes        Used       Avail      "
                   1011:                    "(root)    %%Capacity\n");
1.178     djm      1012:                printf("%11llu %11llu %11llu %11llu         %s\n",
1.100     djm      1013:                    (unsigned long long)st.f_files,
                   1014:                    (unsigned long long)(st.f_files - st.f_ffree),
                   1015:                    (unsigned long long)st.f_favail,
1.178     djm      1016:                    (unsigned long long)st.f_ffree, s_icapacity);
1.100     djm      1017:        } else if (hflag) {
                   1018:                strlcpy(s_used, "error", sizeof(s_used));
                   1019:                strlcpy(s_avail, "error", sizeof(s_avail));
                   1020:                strlcpy(s_root, "error", sizeof(s_root));
                   1021:                strlcpy(s_total, "error", sizeof(s_total));
                   1022:                fmt_scaled((st.f_blocks - st.f_bfree) * st.f_frsize, s_used);
                   1023:                fmt_scaled(st.f_bavail * st.f_frsize, s_avail);
                   1024:                fmt_scaled(st.f_bfree * st.f_frsize, s_root);
                   1025:                fmt_scaled(st.f_blocks * st.f_frsize, s_total);
                   1026:                printf("    Size     Used    Avail   (root)    %%Capacity\n");
1.178     djm      1027:                printf("%7sB %7sB %7sB %7sB         %s\n",
                   1028:                    s_total, s_used, s_avail, s_root, s_dcapacity);
1.100     djm      1029:        } else {
                   1030:                printf("        Size         Used        Avail       "
                   1031:                    "(root)    %%Capacity\n");
1.178     djm      1032:                printf("%12llu %12llu %12llu %12llu         %s\n",
1.100     djm      1033:                    (unsigned long long)(st.f_frsize * st.f_blocks / 1024),
                   1034:                    (unsigned long long)(st.f_frsize *
                   1035:                    (st.f_blocks - st.f_bfree) / 1024),
                   1036:                    (unsigned long long)(st.f_frsize * st.f_bavail / 1024),
                   1037:                    (unsigned long long)(st.f_frsize * st.f_bfree / 1024),
1.178     djm      1038:                    s_dcapacity);
1.100     djm      1039:        }
                   1040:        return 0;
                   1041: }
                   1042:
1.97      djm      1043: /*
                   1044:  * Undo escaping of glob sequences in place. Used to undo extra escaping
                   1045:  * applied in makeargv() when the string is destined for a function that
                   1046:  * does not glob it.
                   1047:  */
                   1048: static void
                   1049: undo_glob_escape(char *s)
                   1050: {
                   1051:        size_t i, j;
                   1052:
                   1053:        for (i = j = 0;;) {
                   1054:                if (s[i] == '\0') {
                   1055:                        s[j] = '\0';
                   1056:                        return;
                   1057:                }
                   1058:                if (s[i] != '\\') {
                   1059:                        s[j++] = s[i++];
                   1060:                        continue;
                   1061:                }
                   1062:                /* s[i] == '\\' */
                   1063:                ++i;
                   1064:                switch (s[i]) {
                   1065:                case '?':
                   1066:                case '[':
                   1067:                case '*':
                   1068:                case '\\':
                   1069:                        s[j++] = s[i++];
                   1070:                        break;
                   1071:                case '\0':
                   1072:                        s[j++] = '\\';
                   1073:                        s[j] = '\0';
                   1074:                        return;
                   1075:                default:
                   1076:                        s[j++] = '\\';
                   1077:                        s[j++] = s[i++];
                   1078:                        break;
                   1079:                }
                   1080:        }
                   1081: }
                   1082:
                   1083: /*
                   1084:  * Split a string into an argument vector using sh(1)-style quoting,
                   1085:  * comment and escaping rules, but with some tweaks to handle glob(3)
                   1086:  * wildcards.
1.116     djm      1087:  * The "sloppy" flag allows for recovery from missing terminating quote, for
                   1088:  * use in parsing incomplete commandlines during tab autocompletion.
                   1089:  *
1.97      djm      1090:  * Returns NULL on error or a NULL-terminated array of arguments.
1.116     djm      1091:  *
                   1092:  * If "lastquote" is not NULL, the quoting character used for the last
                   1093:  * argument is placed in *lastquote ("\0", "'" or "\"").
1.149     djm      1094:  *
1.116     djm      1095:  * If "terminated" is not NULL, *terminated will be set to 1 when the
                   1096:  * last argument's quote has been properly terminated or 0 otherwise.
                   1097:  * This parameter is only of use if "sloppy" is set.
1.97      djm      1098:  */
1.204     djm      1099: #define MAXARGS                128
1.97      djm      1100: #define MAXARGLEN      8192
                   1101: static char **
1.116     djm      1102: makeargv(const char *arg, int *argcp, int sloppy, char *lastquote,
                   1103:     u_int *terminated)
1.97      djm      1104: {
                   1105:        int argc, quot;
                   1106:        size_t i, j;
                   1107:        static char argvs[MAXARGLEN];
                   1108:        static char *argv[MAXARGS + 1];
                   1109:        enum { MA_START, MA_SQUOTE, MA_DQUOTE, MA_UNQUOTED } state, q;
                   1110:
                   1111:        *argcp = argc = 0;
                   1112:        if (strlen(arg) > sizeof(argvs) - 1) {
                   1113:  args_too_longs:
                   1114:                error("string too long");
                   1115:                return NULL;
                   1116:        }
1.116     djm      1117:        if (terminated != NULL)
                   1118:                *terminated = 1;
                   1119:        if (lastquote != NULL)
                   1120:                *lastquote = '\0';
1.97      djm      1121:        state = MA_START;
                   1122:        i = j = 0;
                   1123:        for (;;) {
1.141     markus   1124:                if ((size_t)argc >= sizeof(argv) / sizeof(*argv)){
1.138     dtucker  1125:                        error("Too many arguments.");
                   1126:                        return NULL;
                   1127:                }
1.158     deraadt  1128:                if (isspace((unsigned char)arg[i])) {
1.97      djm      1129:                        if (state == MA_UNQUOTED) {
                   1130:                                /* Terminate current argument */
                   1131:                                argvs[j++] = '\0';
                   1132:                                argc++;
                   1133:                                state = MA_START;
                   1134:                        } else if (state != MA_START)
                   1135:                                argvs[j++] = arg[i];
                   1136:                } else if (arg[i] == '"' || arg[i] == '\'') {
                   1137:                        q = arg[i] == '"' ? MA_DQUOTE : MA_SQUOTE;
                   1138:                        if (state == MA_START) {
                   1139:                                argv[argc] = argvs + j;
                   1140:                                state = q;
1.116     djm      1141:                                if (lastquote != NULL)
                   1142:                                        *lastquote = arg[i];
1.149     djm      1143:                        } else if (state == MA_UNQUOTED)
1.97      djm      1144:                                state = q;
                   1145:                        else if (state == q)
                   1146:                                state = MA_UNQUOTED;
                   1147:                        else
                   1148:                                argvs[j++] = arg[i];
                   1149:                } else if (arg[i] == '\\') {
                   1150:                        if (state == MA_SQUOTE || state == MA_DQUOTE) {
                   1151:                                quot = state == MA_SQUOTE ? '\'' : '"';
                   1152:                                /* Unescape quote we are in */
                   1153:                                /* XXX support \n and friends? */
                   1154:                                if (arg[i + 1] == quot) {
                   1155:                                        i++;
                   1156:                                        argvs[j++] = arg[i];
                   1157:                                } else if (arg[i + 1] == '?' ||
                   1158:                                    arg[i + 1] == '[' || arg[i + 1] == '*') {
                   1159:                                        /*
                   1160:                                         * Special case for sftp: append
                   1161:                                         * double-escaped glob sequence -
                   1162:                                         * glob will undo one level of
                   1163:                                         * escaping. NB. string can grow here.
                   1164:                                         */
                   1165:                                        if (j >= sizeof(argvs) - 5)
                   1166:                                                goto args_too_longs;
                   1167:                                        argvs[j++] = '\\';
                   1168:                                        argvs[j++] = arg[i++];
                   1169:                                        argvs[j++] = '\\';
                   1170:                                        argvs[j++] = arg[i];
                   1171:                                } else {
                   1172:                                        argvs[j++] = arg[i++];
                   1173:                                        argvs[j++] = arg[i];
                   1174:                                }
                   1175:                        } else {
                   1176:                                if (state == MA_START) {
                   1177:                                        argv[argc] = argvs + j;
                   1178:                                        state = MA_UNQUOTED;
1.116     djm      1179:                                        if (lastquote != NULL)
                   1180:                                                *lastquote = '\0';
1.97      djm      1181:                                }
                   1182:                                if (arg[i + 1] == '?' || arg[i + 1] == '[' ||
                   1183:                                    arg[i + 1] == '*' || arg[i + 1] == '\\') {
                   1184:                                        /*
                   1185:                                         * Special case for sftp: append
                   1186:                                         * escaped glob sequence -
                   1187:                                         * glob will undo one level of
                   1188:                                         * escaping.
                   1189:                                         */
                   1190:                                        argvs[j++] = arg[i++];
                   1191:                                        argvs[j++] = arg[i];
                   1192:                                } else {
                   1193:                                        /* Unescape everything */
                   1194:                                        /* XXX support \n and friends? */
                   1195:                                        i++;
                   1196:                                        argvs[j++] = arg[i];
                   1197:                                }
                   1198:                        }
                   1199:                } else if (arg[i] == '#') {
                   1200:                        if (state == MA_SQUOTE || state == MA_DQUOTE)
                   1201:                                argvs[j++] = arg[i];
                   1202:                        else
                   1203:                                goto string_done;
                   1204:                } else if (arg[i] == '\0') {
                   1205:                        if (state == MA_SQUOTE || state == MA_DQUOTE) {
1.116     djm      1206:                                if (sloppy) {
                   1207:                                        state = MA_UNQUOTED;
                   1208:                                        if (terminated != NULL)
                   1209:                                                *terminated = 0;
                   1210:                                        goto string_done;
                   1211:                                }
1.97      djm      1212:                                error("Unterminated quoted argument");
                   1213:                                return NULL;
                   1214:                        }
                   1215:  string_done:
                   1216:                        if (state == MA_UNQUOTED) {
                   1217:                                argvs[j++] = '\0';
                   1218:                                argc++;
                   1219:                        }
                   1220:                        break;
                   1221:                } else {
                   1222:                        if (state == MA_START) {
                   1223:                                argv[argc] = argvs + j;
                   1224:                                state = MA_UNQUOTED;
1.116     djm      1225:                                if (lastquote != NULL)
                   1226:                                        *lastquote = '\0';
1.97      djm      1227:                        }
                   1228:                        if ((state == MA_SQUOTE || state == MA_DQUOTE) &&
                   1229:                            (arg[i] == '?' || arg[i] == '[' || arg[i] == '*')) {
                   1230:                                /*
                   1231:                                 * Special case for sftp: escape quoted
                   1232:                                 * glob(3) wildcards. NB. string can grow
                   1233:                                 * here.
                   1234:                                 */
                   1235:                                if (j >= sizeof(argvs) - 3)
                   1236:                                        goto args_too_longs;
                   1237:                                argvs[j++] = '\\';
                   1238:                                argvs[j++] = arg[i];
                   1239:                        } else
                   1240:                                argvs[j++] = arg[i];
                   1241:                }
                   1242:                i++;
                   1243:        }
                   1244:        *argcp = argc;
                   1245:        return argv;
                   1246: }
                   1247:
1.44      djm      1248: static int
1.187     djm      1249: parse_args(const char **cpp, int *ignore_errors, int *disable_echo, int *aflag,
1.173     djm      1250:          int *fflag, int *hflag, int *iflag, int *lflag, int *pflag,
1.159     logan    1251:          int *rflag, int *sflag,
1.156     djm      1252:     unsigned long *n_arg, char **path1, char **path2)
1.44      djm      1253: {
                   1254:        const char *cmd, *cp = *cpp;
1.97      djm      1255:        char *cp2, **argv;
1.44      djm      1256:        int base = 0;
1.202     dtucker  1257:        long long ll;
1.182     djm      1258:        int path1_mandatory = 0, i, cmdnum, optidx, argc;
1.44      djm      1259:
                   1260:        /* Skip leading whitespace */
                   1261:        cp = cp + strspn(cp, WHITESPACE);
                   1262:
1.187     djm      1263:        /*
                   1264:         * Check for leading '-' (disable error processing) and '@' (suppress
                   1265:         * command echo)
                   1266:         */
1.156     djm      1267:        *ignore_errors = 0;
1.187     djm      1268:        *disable_echo = 0;
                   1269:        for (;*cp != '\0'; cp++) {
                   1270:                if (*cp == '-') {
                   1271:                        *ignore_errors = 1;
                   1272:                } else if (*cp == '@') {
                   1273:                        *disable_echo = 1;
                   1274:                } else {
                   1275:                        /* all other characters terminate prefix processing */
                   1276:                        break;
                   1277:                }
1.44      djm      1278:        }
1.187     djm      1279:        cp = cp + strspn(cp, WHITESPACE);
1.118     dtucker  1280:
                   1281:        /* Ignore blank lines and lines which begin with comment '#' char */
                   1282:        if (*cp == '\0' || *cp == '#')
                   1283:                return (0);
1.44      djm      1284:
1.116     djm      1285:        if ((argv = makeargv(cp, &argc, 0, NULL, NULL)) == NULL)
1.97      djm      1286:                return -1;
                   1287:
1.44      djm      1288:        /* Figure out which command we have */
1.97      djm      1289:        for (i = 0; cmds[i].c != NULL; i++) {
1.142     djm      1290:                if (argv[0] != NULL && strcasecmp(cmds[i].c, argv[0]) == 0)
1.44      djm      1291:                        break;
                   1292:        }
                   1293:        cmdnum = cmds[i].n;
                   1294:        cmd = cmds[i].c;
                   1295:
                   1296:        /* Special case */
                   1297:        if (*cp == '!') {
                   1298:                cp++;
                   1299:                cmdnum = I_SHELL;
                   1300:        } else if (cmdnum == -1) {
                   1301:                error("Invalid command.");
1.97      djm      1302:                return -1;
1.44      djm      1303:        }
                   1304:
                   1305:        /* Get arguments and parse flags */
1.156     djm      1306:        *aflag = *fflag = *hflag = *iflag = *lflag = *pflag = 0;
                   1307:        *rflag = *sflag = 0;
1.44      djm      1308:        *path1 = *path2 = NULL;
1.97      djm      1309:        optidx = 1;
1.44      djm      1310:        switch (cmdnum) {
                   1311:        case I_GET:
1.148     djm      1312:        case I_REGET:
1.159     logan    1313:        case I_REPUT:
1.44      djm      1314:        case I_PUT:
1.132     djm      1315:                if ((optidx = parse_getput_flags(cmd, argv, argc,
1.156     djm      1316:                    aflag, fflag, pflag, rflag)) == -1)
1.97      djm      1317:                        return -1;
1.44      djm      1318:                /* Get first pathname (mandatory) */
1.97      djm      1319:                if (argc - optidx < 1) {
1.44      djm      1320:                        error("You must specify at least one path after a "
                   1321:                            "%s command.", cmd);
1.97      djm      1322:                        return -1;
                   1323:                }
                   1324:                *path1 = xstrdup(argv[optidx]);
                   1325:                /* Get second pathname (optional) */
                   1326:                if (argc - optidx > 1) {
                   1327:                        *path2 = xstrdup(argv[optidx + 1]);
                   1328:                        /* Destination is not globbed */
                   1329:                        undo_glob_escape(*path2);
1.44      djm      1330:                }
                   1331:                break;
1.132     djm      1332:        case I_LINK:
                   1333:                if ((optidx = parse_link_flags(cmd, argv, argc, sflag)) == -1)
                   1334:                        return -1;
1.152     djm      1335:                goto parse_two_paths;
                   1336:        case I_RENAME:
                   1337:                if ((optidx = parse_rename_flags(cmd, argv, argc, lflag)) == -1)
                   1338:                        return -1;
                   1339:                goto parse_two_paths;
1.132     djm      1340:        case I_SYMLINK:
1.153     djm      1341:                if ((optidx = parse_no_flags(cmd, argv, argc)) == -1)
                   1342:                        return -1;
1.152     djm      1343:  parse_two_paths:
1.97      djm      1344:                if (argc - optidx < 2) {
1.44      djm      1345:                        error("You must specify two paths after a %s "
                   1346:                            "command.", cmd);
1.97      djm      1347:                        return -1;
1.44      djm      1348:                }
1.97      djm      1349:                *path1 = xstrdup(argv[optidx]);
                   1350:                *path2 = xstrdup(argv[optidx + 1]);
                   1351:                /* Paths are not globbed */
                   1352:                undo_glob_escape(*path1);
                   1353:                undo_glob_escape(*path2);
1.44      djm      1354:                break;
                   1355:        case I_RM:
                   1356:        case I_MKDIR:
                   1357:        case I_RMDIR:
1.182     djm      1358:        case I_LMKDIR:
                   1359:                path1_mandatory = 1;
                   1360:                /* FALLTHROUGH */
1.44      djm      1361:        case I_CHDIR:
                   1362:        case I_LCHDIR:
1.153     djm      1363:                if ((optidx = parse_no_flags(cmd, argv, argc)) == -1)
                   1364:                        return -1;
1.44      djm      1365:                /* Get pathname (mandatory) */
1.97      djm      1366:                if (argc - optidx < 1) {
1.182     djm      1367:                        if (!path1_mandatory)
                   1368:                                break; /* return a NULL path1 */
1.44      djm      1369:                        error("You must specify a path after a %s command.",
                   1370:                            cmd);
1.97      djm      1371:                        return -1;
1.44      djm      1372:                }
1.97      djm      1373:                *path1 = xstrdup(argv[optidx]);
                   1374:                /* Only "rm" globs */
                   1375:                if (cmdnum != I_RM)
                   1376:                        undo_glob_escape(*path1);
1.44      djm      1377:                break;
1.100     djm      1378:        case I_DF:
                   1379:                if ((optidx = parse_df_flags(cmd, argv, argc, hflag,
                   1380:                    iflag)) == -1)
                   1381:                        return -1;
                   1382:                /* Default to current directory if no path specified */
                   1383:                if (argc - optidx < 1)
                   1384:                        *path1 = NULL;
                   1385:                else {
                   1386:                        *path1 = xstrdup(argv[optidx]);
                   1387:                        undo_glob_escape(*path1);
                   1388:                }
                   1389:                break;
1.44      djm      1390:        case I_LS:
1.97      djm      1391:                if ((optidx = parse_ls_flags(argv, argc, lflag)) == -1)
1.44      djm      1392:                        return(-1);
                   1393:                /* Path is optional */
1.97      djm      1394:                if (argc - optidx > 0)
                   1395:                        *path1 = xstrdup(argv[optidx]);
1.44      djm      1396:                break;
                   1397:        case I_LLS:
1.98      djm      1398:                /* Skip ls command and following whitespace */
                   1399:                cp = cp + strlen(cmd) + strspn(cp, WHITESPACE);
1.44      djm      1400:        case I_SHELL:
                   1401:                /* Uses the rest of the line */
                   1402:                break;
                   1403:        case I_LUMASK:
                   1404:        case I_CHMOD:
                   1405:                base = 8;
1.186     dtucker  1406:                /* FALLTHROUGH */
1.44      djm      1407:        case I_CHOWN:
                   1408:        case I_CHGRP:
1.189     djm      1409:                if ((optidx = parse_ch_flags(cmd, argv, argc, hflag)) == -1)
1.153     djm      1410:                        return -1;
1.44      djm      1411:                /* Get numeric arg (mandatory) */
1.97      djm      1412:                if (argc - optidx < 1)
                   1413:                        goto need_num_arg;
1.93      ray      1414:                errno = 0;
1.202     dtucker  1415:                ll = strtoll(argv[optidx], &cp2, base);
1.97      djm      1416:                if (cp2 == argv[optidx] || *cp2 != '\0' ||
1.202     dtucker  1417:                    ((ll == LLONG_MIN || ll == LLONG_MAX) && errno == ERANGE) ||
                   1418:                    ll < 0 || ll > UINT32_MAX) {
1.97      djm      1419:  need_num_arg:
1.44      djm      1420:                        error("You must supply a numeric argument "
                   1421:                            "to the %s command.", cmd);
1.97      djm      1422:                        return -1;
1.44      djm      1423:                }
1.202     dtucker  1424:                *n_arg = ll;
1.97      djm      1425:                if (cmdnum == I_LUMASK)
1.44      djm      1426:                        break;
                   1427:                /* Get pathname (mandatory) */
1.97      djm      1428:                if (argc - optidx < 2) {
1.44      djm      1429:                        error("You must specify a path after a %s command.",
                   1430:                            cmd);
1.97      djm      1431:                        return -1;
1.44      djm      1432:                }
1.97      djm      1433:                *path1 = xstrdup(argv[optidx + 1]);
1.44      djm      1434:                break;
                   1435:        case I_QUIT:
                   1436:        case I_PWD:
                   1437:        case I_LPWD:
                   1438:        case I_HELP:
                   1439:        case I_VERSION:
                   1440:        case I_PROGRESS:
1.153     djm      1441:                if ((optidx = parse_no_flags(cmd, argv, argc)) == -1)
                   1442:                        return -1;
1.44      djm      1443:                break;
                   1444:        default:
                   1445:                fatal("Command not implemented");
                   1446:        }
                   1447:
                   1448:        *cpp = cp;
                   1449:        return(cmdnum);
                   1450: }
                   1451:
                   1452: static int
                   1453: parse_dispatch_command(struct sftp_conn *conn, const char *cmd, char **pwd,
1.187     djm      1454:     const char *startdir, int err_abort, int echo_command)
1.44      djm      1455: {
1.187     djm      1456:        const char *ocmd = cmd;
1.44      djm      1457:        char *path1, *path2, *tmp;
1.187     djm      1458:        int ignore_errors = 0, disable_echo = 1;
                   1459:        int aflag = 0, fflag = 0, hflag = 0, iflag = 0;
1.156     djm      1460:        int lflag = 0, pflag = 0, rflag = 0, sflag = 0;
1.132     djm      1461:        int cmdnum, i;
1.107     dtucker  1462:        unsigned long n_arg = 0;
1.44      djm      1463:        Attrib a, *aa;
1.170     deraadt  1464:        char path_buf[PATH_MAX];
1.44      djm      1465:        int err = 0;
                   1466:        glob_t g;
                   1467:
                   1468:        path1 = path2 = NULL;
1.187     djm      1469:        cmdnum = parse_args(&cmd, &ignore_errors, &disable_echo, &aflag, &fflag,
                   1470:            &hflag, &iflag, &lflag, &pflag, &rflag, &sflag, &n_arg,
                   1471:            &path1, &path2);
1.156     djm      1472:        if (ignore_errors != 0)
1.44      djm      1473:                err_abort = 0;
                   1474:
1.187     djm      1475:        if (echo_command && !disable_echo)
                   1476:                mprintf("sftp> %s\n", ocmd);
                   1477:
1.44      djm      1478:        memset(&g, 0, sizeof(g));
                   1479:
                   1480:        /* Perform command */
                   1481:        switch (cmdnum) {
                   1482:        case 0:
                   1483:                /* Blank line */
                   1484:                break;
                   1485:        case -1:
                   1486:                /* Unrecognized command */
                   1487:                err = -1;
                   1488:                break;
1.148     djm      1489:        case I_REGET:
                   1490:                aflag = 1;
                   1491:                /* FALLTHROUGH */
1.44      djm      1492:        case I_GET:
1.148     djm      1493:                err = process_get(conn, path1, path2, *pwd, pflag,
1.156     djm      1494:                    rflag, aflag, fflag);
1.44      djm      1495:                break;
1.159     logan    1496:        case I_REPUT:
                   1497:                aflag = 1;
                   1498:                /* FALLTHROUGH */
1.44      djm      1499:        case I_PUT:
1.156     djm      1500:                err = process_put(conn, path1, path2, *pwd, pflag,
1.159     logan    1501:                    rflag, aflag, fflag);
1.44      djm      1502:                break;
                   1503:        case I_RENAME:
                   1504:                path1 = make_absolute(path1, *pwd);
                   1505:                path2 = make_absolute(path2, *pwd);
1.152     djm      1506:                err = do_rename(conn, path1, path2, lflag);
1.44      djm      1507:                break;
                   1508:        case I_SYMLINK:
1.132     djm      1509:                sflag = 1;
1.186     dtucker  1510:                /* FALLTHROUGH */
1.132     djm      1511:        case I_LINK:
1.151     djm      1512:                if (!sflag)
                   1513:                        path1 = make_absolute(path1, *pwd);
1.44      djm      1514:                path2 = make_absolute(path2, *pwd);
1.132     djm      1515:                err = (sflag ? do_symlink : do_hardlink)(conn, path1, path2);
1.44      djm      1516:                break;
                   1517:        case I_RM:
                   1518:                path1 = make_absolute(path1, *pwd);
                   1519:                remote_glob(conn, path1, GLOB_NOCHECK, NULL, &g);
1.46      djm      1520:                for (i = 0; g.gl_pathv[i] && !interrupted; i++) {
1.143     djm      1521:                        if (!quiet)
1.174     schwarze 1522:                                mprintf("Removing %s\n", g.gl_pathv[i]);
1.44      djm      1523:                        err = do_rm(conn, g.gl_pathv[i]);
                   1524:                        if (err != 0 && err_abort)
                   1525:                                break;
                   1526:                }
                   1527:                break;
                   1528:        case I_MKDIR:
                   1529:                path1 = make_absolute(path1, *pwd);
                   1530:                attrib_clear(&a);
                   1531:                a.flags |= SSH2_FILEXFER_ATTR_PERMISSIONS;
                   1532:                a.perm = 0777;
1.111     djm      1533:                err = do_mkdir(conn, path1, &a, 1);
1.44      djm      1534:                break;
                   1535:        case I_RMDIR:
                   1536:                path1 = make_absolute(path1, *pwd);
                   1537:                err = do_rmdir(conn, path1);
                   1538:                break;
                   1539:        case I_CHDIR:
1.182     djm      1540:                if (path1 == NULL || *path1 == '\0')
                   1541:                        path1 = xstrdup(startdir);
1.44      djm      1542:                path1 = make_absolute(path1, *pwd);
                   1543:                if ((tmp = do_realpath(conn, path1)) == NULL) {
                   1544:                        err = 1;
                   1545:                        break;
                   1546:                }
                   1547:                if ((aa = do_stat(conn, tmp, 0)) == NULL) {
1.145     djm      1548:                        free(tmp);
1.44      djm      1549:                        err = 1;
                   1550:                        break;
                   1551:                }
                   1552:                if (!(aa->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)) {
                   1553:                        error("Can't change directory: Can't check target");
1.145     djm      1554:                        free(tmp);
1.44      djm      1555:                        err = 1;
                   1556:                        break;
                   1557:                }
                   1558:                if (!S_ISDIR(aa->perm)) {
                   1559:                        error("Can't change directory: \"%s\" is not "
                   1560:                            "a directory", tmp);
1.145     djm      1561:                        free(tmp);
1.44      djm      1562:                        err = 1;
                   1563:                        break;
                   1564:                }
1.145     djm      1565:                free(*pwd);
1.44      djm      1566:                *pwd = tmp;
                   1567:                break;
                   1568:        case I_LS:
                   1569:                if (!path1) {
1.125     djm      1570:                        do_ls_dir(conn, *pwd, *pwd, lflag);
1.44      djm      1571:                        break;
                   1572:                }
                   1573:
                   1574:                /* Strip pwd off beginning of non-absolute paths */
                   1575:                tmp = NULL;
1.188     djm      1576:                if (!path_absolute(path1))
1.44      djm      1577:                        tmp = *pwd;
                   1578:
                   1579:                path1 = make_absolute(path1, *pwd);
                   1580:                err = do_globbed_ls(conn, path1, tmp, lflag);
1.100     djm      1581:                break;
                   1582:        case I_DF:
                   1583:                /* Default to current directory if no path specified */
                   1584:                if (path1 == NULL)
                   1585:                        path1 = xstrdup(*pwd);
                   1586:                path1 = make_absolute(path1, *pwd);
                   1587:                err = do_df(conn, path1, hflag, iflag);
1.44      djm      1588:                break;
                   1589:        case I_LCHDIR:
1.182     djm      1590:                if (path1 == NULL || *path1 == '\0')
                   1591:                        path1 = xstrdup("~");
1.166     deraadt  1592:                tmp = tilde_expand_filename(path1, getuid());
1.165     djm      1593:                free(path1);
                   1594:                path1 = tmp;
1.44      djm      1595:                if (chdir(path1) == -1) {
                   1596:                        error("Couldn't change local directory to "
                   1597:                            "\"%s\": %s", path1, strerror(errno));
                   1598:                        err = 1;
                   1599:                }
                   1600:                break;
                   1601:        case I_LMKDIR:
                   1602:                if (mkdir(path1, 0777) == -1) {
                   1603:                        error("Couldn't create local directory "
                   1604:                            "\"%s\": %s", path1, strerror(errno));
                   1605:                        err = 1;
                   1606:                }
                   1607:                break;
                   1608:        case I_LLS:
                   1609:                local_do_ls(cmd);
                   1610:                break;
                   1611:        case I_SHELL:
                   1612:                local_do_shell(cmd);
                   1613:                break;
                   1614:        case I_LUMASK:
                   1615:                umask(n_arg);
                   1616:                printf("Local umask: %03lo\n", n_arg);
                   1617:                break;
                   1618:        case I_CHMOD:
                   1619:                path1 = make_absolute(path1, *pwd);
                   1620:                attrib_clear(&a);
                   1621:                a.flags |= SSH2_FILEXFER_ATTR_PERMISSIONS;
                   1622:                a.perm = n_arg;
                   1623:                remote_glob(conn, path1, GLOB_NOCHECK, NULL, &g);
1.46      djm      1624:                for (i = 0; g.gl_pathv[i] && !interrupted; i++) {
1.143     djm      1625:                        if (!quiet)
1.174     schwarze 1626:                                mprintf("Changing mode on %s\n",
                   1627:                                    g.gl_pathv[i]);
1.189     djm      1628:                        err = (hflag ? do_lsetstat : do_setstat)(conn,
                   1629:                            g.gl_pathv[i], &a);
1.44      djm      1630:                        if (err != 0 && err_abort)
                   1631:                                break;
                   1632:                }
                   1633:                break;
                   1634:        case I_CHOWN:
                   1635:        case I_CHGRP:
                   1636:                path1 = make_absolute(path1, *pwd);
                   1637:                remote_glob(conn, path1, GLOB_NOCHECK, NULL, &g);
1.46      djm      1638:                for (i = 0; g.gl_pathv[i] && !interrupted; i++) {
1.189     djm      1639:                        if (!(aa = (hflag ? do_lstat : do_stat)(conn,
                   1640:                            g.gl_pathv[i], 0))) {
1.104     djm      1641:                                if (err_abort) {
                   1642:                                        err = -1;
1.44      djm      1643:                                        break;
1.104     djm      1644:                                } else
1.44      djm      1645:                                        continue;
                   1646:                        }
                   1647:                        if (!(aa->flags & SSH2_FILEXFER_ATTR_UIDGID)) {
                   1648:                                error("Can't get current ownership of "
                   1649:                                    "remote file \"%s\"", g.gl_pathv[i]);
1.104     djm      1650:                                if (err_abort) {
                   1651:                                        err = -1;
1.44      djm      1652:                                        break;
1.104     djm      1653:                                } else
1.44      djm      1654:                                        continue;
                   1655:                        }
                   1656:                        aa->flags &= SSH2_FILEXFER_ATTR_UIDGID;
                   1657:                        if (cmdnum == I_CHOWN) {
1.143     djm      1658:                                if (!quiet)
1.174     schwarze 1659:                                        mprintf("Changing owner on %s\n",
1.143     djm      1660:                                            g.gl_pathv[i]);
1.44      djm      1661:                                aa->uid = n_arg;
                   1662:                        } else {
1.143     djm      1663:                                if (!quiet)
1.174     schwarze 1664:                                        mprintf("Changing group on %s\n",
1.143     djm      1665:                                            g.gl_pathv[i]);
1.44      djm      1666:                                aa->gid = n_arg;
                   1667:                        }
1.189     djm      1668:                        err = (hflag ? do_lsetstat : do_setstat)(conn,
                   1669:                            g.gl_pathv[i], aa);
1.44      djm      1670:                        if (err != 0 && err_abort)
                   1671:                                break;
                   1672:                }
                   1673:                break;
                   1674:        case I_PWD:
1.174     schwarze 1675:                mprintf("Remote working directory: %s\n", *pwd);
1.44      djm      1676:                break;
                   1677:        case I_LPWD:
                   1678:                if (!getcwd(path_buf, sizeof(path_buf))) {
                   1679:                        error("Couldn't get local cwd: %s", strerror(errno));
                   1680:                        err = -1;
                   1681:                        break;
                   1682:                }
1.174     schwarze 1683:                mprintf("Local working directory: %s\n", path_buf);
1.44      djm      1684:                break;
                   1685:        case I_QUIT:
                   1686:                /* Processed below */
                   1687:                break;
                   1688:        case I_HELP:
                   1689:                help();
                   1690:                break;
                   1691:        case I_VERSION:
                   1692:                printf("SFTP protocol version %u\n", sftp_proto_version(conn));
                   1693:                break;
                   1694:        case I_PROGRESS:
                   1695:                showprogress = !showprogress;
                   1696:                if (showprogress)
                   1697:                        printf("Progress meter enabled\n");
                   1698:                else
                   1699:                        printf("Progress meter disabled\n");
                   1700:                break;
                   1701:        default:
                   1702:                fatal("%d is not implemented", cmdnum);
                   1703:        }
                   1704:
                   1705:        if (g.gl_pathc)
                   1706:                globfree(&g);
1.145     djm      1707:        free(path1);
                   1708:        free(path2);
1.44      djm      1709:
                   1710:        /* If an unignored error occurs in batch mode we should abort. */
                   1711:        if (err_abort && err != 0)
                   1712:                return (-1);
                   1713:        else if (cmdnum == I_QUIT)
                   1714:                return (1);
                   1715:
                   1716:        return (0);
                   1717: }
                   1718:
1.57      djm      1719: static char *
                   1720: prompt(EditLine *el)
                   1721: {
                   1722:        return ("sftp> ");
                   1723: }
                   1724:
1.116     djm      1725: /* Display entries in 'list' after skipping the first 'len' chars */
                   1726: static void
                   1727: complete_display(char **list, u_int len)
                   1728: {
                   1729:        u_int y, m = 0, width = 80, columns = 1, colspace = 0, llen;
                   1730:        struct winsize ws;
                   1731:        char *tmp;
                   1732:
                   1733:        /* Count entries for sort and find longest */
1.149     djm      1734:        for (y = 0; list[y]; y++)
1.176     deraadt  1735:                m = MAXIMUM(m, strlen(list[y]));
1.116     djm      1736:
                   1737:        if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) != -1)
                   1738:                width = ws.ws_col;
                   1739:
                   1740:        m = m > len ? m - len : 0;
                   1741:        columns = width / (m + 2);
1.176     deraadt  1742:        columns = MAXIMUM(columns, 1);
1.116     djm      1743:        colspace = width / columns;
1.176     deraadt  1744:        colspace = MINIMUM(colspace, width);
1.116     djm      1745:
                   1746:        printf("\n");
                   1747:        m = 1;
                   1748:        for (y = 0; list[y]; y++) {
                   1749:                llen = strlen(list[y]);
                   1750:                tmp = llen > len ? list[y] + len : "";
1.174     schwarze 1751:                mprintf("%-*s", colspace, tmp);
1.116     djm      1752:                if (m >= columns) {
                   1753:                        printf("\n");
                   1754:                        m = 1;
                   1755:                } else
                   1756:                        m++;
                   1757:        }
                   1758:        printf("\n");
                   1759: }
                   1760:
                   1761: /*
                   1762:  * Given a "list" of words that begin with a common prefix of "word",
                   1763:  * attempt to find an autocompletion to extends "word" by the next
                   1764:  * characters common to all entries in "list".
                   1765:  */
                   1766: static char *
                   1767: complete_ambiguous(const char *word, char **list, size_t count)
                   1768: {
                   1769:        if (word == NULL)
                   1770:                return NULL;
                   1771:
                   1772:        if (count > 0) {
                   1773:                u_int y, matchlen = strlen(list[0]);
                   1774:
                   1775:                /* Find length of common stem */
                   1776:                for (y = 1; list[y]; y++) {
                   1777:                        u_int x;
                   1778:
1.149     djm      1779:                        for (x = 0; x < matchlen; x++)
                   1780:                                if (list[0][x] != list[y][x])
1.116     djm      1781:                                        break;
                   1782:
                   1783:                        matchlen = x;
                   1784:                }
                   1785:
                   1786:                if (matchlen > strlen(word)) {
                   1787:                        char *tmp = xstrdup(list[0]);
                   1788:
1.117     dtucker  1789:                        tmp[matchlen] = '\0';
1.116     djm      1790:                        return tmp;
                   1791:                }
1.149     djm      1792:        }
1.116     djm      1793:
                   1794:        return xstrdup(word);
                   1795: }
                   1796:
                   1797: /* Autocomplete a sftp command */
                   1798: static int
                   1799: complete_cmd_parse(EditLine *el, char *cmd, int lastarg, char quote,
                   1800:     int terminated)
                   1801: {
                   1802:        u_int y, count = 0, cmdlen, tmplen;
                   1803:        char *tmp, **list, argterm[3];
                   1804:        const LineInfo *lf;
                   1805:
                   1806:        list = xcalloc((sizeof(cmds) / sizeof(*cmds)) + 1, sizeof(char *));
                   1807:
                   1808:        /* No command specified: display all available commands */
                   1809:        if (cmd == NULL) {
                   1810:                for (y = 0; cmds[y].c; y++)
                   1811:                        list[count++] = xstrdup(cmds[y].c);
1.149     djm      1812:
1.116     djm      1813:                list[count] = NULL;
                   1814:                complete_display(list, 0);
                   1815:
1.149     djm      1816:                for (y = 0; list[y] != NULL; y++)
                   1817:                        free(list[y]);
1.145     djm      1818:                free(list);
1.116     djm      1819:                return count;
                   1820:        }
                   1821:
                   1822:        /* Prepare subset of commands that start with "cmd" */
                   1823:        cmdlen = strlen(cmd);
                   1824:        for (y = 0; cmds[y].c; y++)  {
1.149     djm      1825:                if (!strncasecmp(cmd, cmds[y].c, cmdlen))
1.116     djm      1826:                        list[count++] = xstrdup(cmds[y].c);
                   1827:        }
                   1828:        list[count] = NULL;
                   1829:
1.134     oga      1830:        if (count == 0) {
1.145     djm      1831:                free(list);
1.116     djm      1832:                return 0;
1.134     oga      1833:        }
1.116     djm      1834:
1.183     djm      1835:        /* Complete ambiguous command */
1.116     djm      1836:        tmp = complete_ambiguous(cmd, list, count);
                   1837:        if (count > 1)
                   1838:                complete_display(list, 0);
                   1839:
1.149     djm      1840:        for (y = 0; list[y]; y++)
                   1841:                free(list[y]);
1.145     djm      1842:        free(list);
1.116     djm      1843:
                   1844:        if (tmp != NULL) {
                   1845:                tmplen = strlen(tmp);
                   1846:                cmdlen = strlen(cmd);
                   1847:                /* If cmd may be extended then do so */
                   1848:                if (tmplen > cmdlen)
                   1849:                        if (el_insertstr(el, tmp + cmdlen) == -1)
                   1850:                                fatal("el_insertstr failed.");
                   1851:                lf = el_line(el);
                   1852:                /* Terminate argument cleanly */
                   1853:                if (count == 1) {
                   1854:                        y = 0;
                   1855:                        if (!terminated)
                   1856:                                argterm[y++] = quote;
                   1857:                        if (lastarg || *(lf->cursor) != ' ')
                   1858:                                argterm[y++] = ' ';
                   1859:                        argterm[y] = '\0';
                   1860:                        if (y > 0 && el_insertstr(el, argterm) == -1)
                   1861:                                fatal("el_insertstr failed.");
                   1862:                }
1.145     djm      1863:                free(tmp);
1.116     djm      1864:        }
                   1865:
                   1866:        return count;
                   1867: }
                   1868:
                   1869: /*
                   1870:  * Determine whether a particular sftp command's arguments (if any)
                   1871:  * represent local or remote files.
                   1872:  */
                   1873: static int
                   1874: complete_is_remote(char *cmd) {
                   1875:        int i;
                   1876:
                   1877:        if (cmd == NULL)
                   1878:                return -1;
                   1879:
                   1880:        for (i = 0; cmds[i].c; i++) {
1.149     djm      1881:                if (!strncasecmp(cmd, cmds[i].c, strlen(cmds[i].c)))
1.116     djm      1882:                        return cmds[i].t;
                   1883:        }
                   1884:
                   1885:        return -1;
                   1886: }
                   1887:
                   1888: /* Autocomplete a filename "file" */
                   1889: static int
                   1890: complete_match(EditLine *el, struct sftp_conn *conn, char *remote_path,
                   1891:     char *file, int remote, int lastarg, char quote, int terminated)
                   1892: {
                   1893:        glob_t g;
1.146     dtucker  1894:        char *tmp, *tmp2, ins[8];
1.140     dtucker  1895:        u_int i, hadglob, pwdlen, len, tmplen, filelen, cesc, isesc, isabs;
1.146     dtucker  1896:        int clen;
1.116     djm      1897:        const LineInfo *lf;
1.149     djm      1898:
1.116     djm      1899:        /* Glob from "file" location */
                   1900:        if (file == NULL)
                   1901:                tmp = xstrdup("*");
                   1902:        else
                   1903:                xasprintf(&tmp, "%s*", file);
                   1904:
1.139     dtucker  1905:        /* Check if the path is absolute. */
1.188     djm      1906:        isabs = path_absolute(tmp);
1.139     dtucker  1907:
1.116     djm      1908:        memset(&g, 0, sizeof(g));
                   1909:        if (remote != LOCAL) {
                   1910:                tmp = make_absolute(tmp, remote_path);
                   1911:                remote_glob(conn, tmp, GLOB_DOOFFS|GLOB_MARK, NULL, &g);
1.149     djm      1912:        } else
1.116     djm      1913:                glob(tmp, GLOB_DOOFFS|GLOB_MARK, NULL, &g);
1.149     djm      1914:
1.116     djm      1915:        /* Determine length of pwd so we can trim completion display */
                   1916:        for (hadglob = tmplen = pwdlen = 0; tmp[tmplen] != 0; tmplen++) {
                   1917:                /* Terminate counting on first unescaped glob metacharacter */
                   1918:                if (tmp[tmplen] == '*' || tmp[tmplen] == '?') {
                   1919:                        if (tmp[tmplen] != '*' || tmp[tmplen + 1] != '\0')
                   1920:                                hadglob = 1;
                   1921:                        break;
                   1922:                }
                   1923:                if (tmp[tmplen] == '\\' && tmp[tmplen + 1] != '\0')
                   1924:                        tmplen++;
                   1925:                if (tmp[tmplen] == '/')
                   1926:                        pwdlen = tmplen + 1;    /* track last seen '/' */
                   1927:        }
1.145     djm      1928:        free(tmp);
1.161     dtucker  1929:        tmp = NULL;
1.116     djm      1930:
1.149     djm      1931:        if (g.gl_matchc == 0)
1.116     djm      1932:                goto out;
                   1933:
                   1934:        if (g.gl_matchc > 1)
                   1935:                complete_display(g.gl_pathv, pwdlen);
                   1936:
                   1937:        /* Don't try to extend globs */
                   1938:        if (file == NULL || hadglob)
                   1939:                goto out;
                   1940:
                   1941:        tmp2 = complete_ambiguous(file, g.gl_pathv, g.gl_matchc);
1.139     dtucker  1942:        tmp = path_strip(tmp2, isabs ? NULL : remote_path);
1.145     djm      1943:        free(tmp2);
1.116     djm      1944:
                   1945:        if (tmp == NULL)
                   1946:                goto out;
                   1947:
                   1948:        tmplen = strlen(tmp);
                   1949:        filelen = strlen(file);
                   1950:
1.140     dtucker  1951:        /* Count the number of escaped characters in the input string. */
                   1952:        cesc = isesc = 0;
                   1953:        for (i = 0; i < filelen; i++) {
                   1954:                if (!isesc && file[i] == '\\' && i + 1 < filelen){
                   1955:                        isesc = 1;
                   1956:                        cesc++;
                   1957:                } else
                   1958:                        isesc = 0;
                   1959:        }
                   1960:
                   1961:        if (tmplen > (filelen - cesc)) {
                   1962:                tmp2 = tmp + filelen - cesc;
1.149     djm      1963:                len = strlen(tmp2);
1.116     djm      1964:                /* quote argument on way out */
1.146     dtucker  1965:                for (i = 0; i < len; i += clen) {
                   1966:                        if ((clen = mblen(tmp2 + i, len - i)) < 0 ||
                   1967:                            (size_t)clen > sizeof(ins) - 2)
                   1968:                                fatal("invalid multibyte character");
1.116     djm      1969:                        ins[0] = '\\';
1.146     dtucker  1970:                        memcpy(ins + 1, tmp2 + i, clen);
                   1971:                        ins[clen + 1] = '\0';
1.116     djm      1972:                        switch (tmp2[i]) {
                   1973:                        case '\'':
                   1974:                        case '"':
                   1975:                        case '\\':
                   1976:                        case '\t':
1.131     sthen    1977:                        case '[':
1.116     djm      1978:                        case ' ':
1.140     dtucker  1979:                        case '#':
                   1980:                        case '*':
1.116     djm      1981:                                if (quote == '\0' || tmp2[i] == quote) {
                   1982:                                        if (el_insertstr(el, ins) == -1)
                   1983:                                                fatal("el_insertstr "
                   1984:                                                    "failed.");
                   1985:                                        break;
                   1986:                                }
                   1987:                                /* FALLTHROUGH */
                   1988:                        default:
                   1989:                                if (el_insertstr(el, ins + 1) == -1)
                   1990:                                        fatal("el_insertstr failed.");
                   1991:                                break;
                   1992:                        }
                   1993:                }
                   1994:        }
                   1995:
                   1996:        lf = el_line(el);
                   1997:        if (g.gl_matchc == 1) {
                   1998:                i = 0;
1.162     dtucker  1999:                if (!terminated && quote != '\0')
1.116     djm      2000:                        ins[i++] = quote;
1.120     djm      2001:                if (*(lf->cursor - 1) != '/' &&
                   2002:                    (lastarg || *(lf->cursor) != ' '))
1.116     djm      2003:                        ins[i++] = ' ';
                   2004:                ins[i] = '\0';
                   2005:                if (i > 0 && el_insertstr(el, ins) == -1)
                   2006:                        fatal("el_insertstr failed.");
                   2007:        }
1.145     djm      2008:        free(tmp);
1.116     djm      2009:
                   2010:  out:
                   2011:        globfree(&g);
                   2012:        return g.gl_matchc;
                   2013: }
                   2014:
                   2015: /* tab-completion hook function, called via libedit */
                   2016: static unsigned char
                   2017: complete(EditLine *el, int ch)
                   2018: {
1.149     djm      2019:        char **argv, *line, quote;
1.147     djm      2020:        int argc, carg;
                   2021:        u_int cursor, len, terminated, ret = CC_ERROR;
1.116     djm      2022:        const LineInfo *lf;
                   2023:        struct complete_ctx *complete_ctx;
                   2024:
                   2025:        lf = el_line(el);
                   2026:        if (el_get(el, EL_CLIENTDATA, (void**)&complete_ctx) != 0)
1.203     djm      2027:                fatal_f("el_get failed");
1.116     djm      2028:
                   2029:        /* Figure out which argument the cursor points to */
                   2030:        cursor = lf->cursor - lf->buffer;
1.171     deraadt  2031:        line = xmalloc(cursor + 1);
1.116     djm      2032:        memcpy(line, lf->buffer, cursor);
                   2033:        line[cursor] = '\0';
                   2034:        argv = makeargv(line, &carg, 1, &quote, &terminated);
1.145     djm      2035:        free(line);
1.116     djm      2036:
                   2037:        /* Get all the arguments on the line */
                   2038:        len = lf->lastchar - lf->buffer;
1.171     deraadt  2039:        line = xmalloc(len + 1);
1.116     djm      2040:        memcpy(line, lf->buffer, len);
                   2041:        line[len] = '\0';
                   2042:        argv = makeargv(line, &argc, 1, NULL, NULL);
                   2043:
                   2044:        /* Ensure cursor is at EOL or a argument boundary */
                   2045:        if (line[cursor] != ' ' && line[cursor] != '\0' &&
                   2046:            line[cursor] != '\n') {
1.145     djm      2047:                free(line);
1.116     djm      2048:                return ret;
                   2049:        }
                   2050:
                   2051:        if (carg == 0) {
                   2052:                /* Show all available commands */
                   2053:                complete_cmd_parse(el, NULL, argc == carg, '\0', 1);
                   2054:                ret = CC_REDISPLAY;
                   2055:        } else if (carg == 1 && cursor > 0 && line[cursor - 1] != ' ')  {
                   2056:                /* Handle the command parsing */
                   2057:                if (complete_cmd_parse(el, argv[0], argc == carg,
1.149     djm      2058:                    quote, terminated) != 0)
1.116     djm      2059:                        ret = CC_REDISPLAY;
                   2060:        } else if (carg >= 1) {
                   2061:                /* Handle file parsing */
                   2062:                int remote = complete_is_remote(argv[0]);
                   2063:                char *filematch = NULL;
                   2064:
                   2065:                if (carg > 1 && line[cursor-1] != ' ')
                   2066:                        filematch = argv[carg - 1];
                   2067:
                   2068:                if (remote != 0 &&
                   2069:                    complete_match(el, complete_ctx->conn,
                   2070:                    *complete_ctx->remote_pathp, filematch,
1.149     djm      2071:                    remote, carg == argc, quote, terminated) != 0)
1.116     djm      2072:                        ret = CC_REDISPLAY;
                   2073:        }
                   2074:
1.149     djm      2075:        free(line);
1.116     djm      2076:        return ret;
                   2077: }
                   2078:
1.182     djm      2079: static int
1.112     djm      2080: interactive_loop(struct sftp_conn *conn, char *file1, char *file2)
1.44      djm      2081: {
1.116     djm      2082:        char *remote_path;
1.182     djm      2083:        char *dir = NULL, *startdir = NULL;
1.44      djm      2084:        char cmd[2048];
1.66      jaredy   2085:        int err, interactive;
1.57      djm      2086:        EditLine *el = NULL;
                   2087:        History *hl = NULL;
                   2088:        HistEvent hev;
                   2089:        extern char *__progname;
1.116     djm      2090:        struct complete_ctx complete_ctx;
1.57      djm      2091:
                   2092:        if (!batchmode && isatty(STDIN_FILENO)) {
                   2093:                if ((el = el_init(__progname, stdin, stdout, stderr)) == NULL)
                   2094:                        fatal("Couldn't initialise editline");
                   2095:                if ((hl = history_init()) == NULL)
                   2096:                        fatal("Couldn't initialise editline history");
                   2097:                history(hl, &hev, H_SETSIZE, 100);
                   2098:                el_set(el, EL_HIST, history, hl);
                   2099:
                   2100:                el_set(el, EL_PROMPT, prompt);
                   2101:                el_set(el, EL_EDITOR, "emacs");
                   2102:                el_set(el, EL_TERMINAL, NULL);
                   2103:                el_set(el, EL_SIGNAL, 1);
                   2104:                el_source(el, NULL);
1.116     djm      2105:
                   2106:                /* Tab Completion */
1.149     djm      2107:                el_set(el, EL_ADDFN, "ftp-complete",
1.131     sthen    2108:                    "Context sensitive argument completion", complete);
1.116     djm      2109:                complete_ctx.conn = conn;
                   2110:                complete_ctx.remote_pathp = &remote_path;
                   2111:                el_set(el, EL_CLIENTDATA, (void*)&complete_ctx);
                   2112:                el_set(el, EL_BIND, "^I", "ftp-complete", NULL);
1.154     djm      2113:                /* enable ctrl-left-arrow and ctrl-right-arrow */
                   2114:                el_set(el, EL_BIND, "\\e[1;5C", "em-next-word", NULL);
1.194     tb       2115:                el_set(el, EL_BIND, "\\e\\e[C", "em-next-word", NULL);
1.154     djm      2116:                el_set(el, EL_BIND, "\\e[1;5D", "ed-prev-word", NULL);
                   2117:                el_set(el, EL_BIND, "\\e\\e[D", "ed-prev-word", NULL);
1.155     djm      2118:                /* make ^w match ksh behaviour */
                   2119:                el_set(el, EL_BIND, "^w", "ed-delete-prev-word", NULL);
1.57      djm      2120:        }
1.44      djm      2121:
1.116     djm      2122:        remote_path = do_realpath(conn, ".");
                   2123:        if (remote_path == NULL)
1.44      djm      2124:                fatal("Need cwd");
1.182     djm      2125:        startdir = xstrdup(remote_path);
1.44      djm      2126:
                   2127:        if (file1 != NULL) {
                   2128:                dir = xstrdup(file1);
1.116     djm      2129:                dir = make_absolute(dir, remote_path);
1.44      djm      2130:
                   2131:                if (remote_is_dir(conn, dir) && file2 == NULL) {
1.143     djm      2132:                        if (!quiet)
1.174     schwarze 2133:                                mprintf("Changing to: %s\n", dir);
1.44      djm      2134:                        snprintf(cmd, sizeof cmd, "cd \"%s\"", dir);
1.116     djm      2135:                        if (parse_dispatch_command(conn, cmd,
1.187     djm      2136:                            &remote_path, startdir, 1, 0) != 0) {
1.145     djm      2137:                                free(dir);
1.182     djm      2138:                                free(startdir);
1.145     djm      2139:                                free(remote_path);
                   2140:                                free(conn);
1.44      djm      2141:                                return (-1);
1.58      markus   2142:                        }
1.44      djm      2143:                } else {
1.137     djm      2144:                        /* XXX this is wrong wrt quoting */
1.148     djm      2145:                        snprintf(cmd, sizeof cmd, "get%s %s%s%s",
                   2146:                            global_aflag ? " -a" : "", dir,
                   2147:                            file2 == NULL ? "" : " ",
                   2148:                            file2 == NULL ? "" : file2);
1.116     djm      2149:                        err = parse_dispatch_command(conn, cmd,
1.187     djm      2150:                            &remote_path, startdir, 1, 0);
1.145     djm      2151:                        free(dir);
1.182     djm      2152:                        free(startdir);
1.145     djm      2153:                        free(remote_path);
                   2154:                        free(conn);
1.44      djm      2155:                        return (err);
                   2156:                }
1.145     djm      2157:                free(dir);
1.44      djm      2158:        }
                   2159:
1.168     millert  2160:        setvbuf(stdout, NULL, _IOLBF, 0);
                   2161:        setvbuf(infile, NULL, _IOLBF, 0);
1.44      djm      2162:
1.66      jaredy   2163:        interactive = !batchmode && isatty(STDIN_FILENO);
1.44      djm      2164:        err = 0;
                   2165:        for (;;) {
1.57      djm      2166:                const char *line;
                   2167:                int count = 0;
1.44      djm      2168:
1.197     dtucker  2169:                ssh_signal(SIGINT, SIG_IGN);
1.46      djm      2170:
1.57      djm      2171:                if (el == NULL) {
1.66      jaredy   2172:                        if (interactive)
                   2173:                                printf("sftp> ");
1.57      djm      2174:                        if (fgets(cmd, sizeof(cmd), infile) == NULL) {
1.66      jaredy   2175:                                if (interactive)
                   2176:                                        printf("\n");
1.57      djm      2177:                                break;
                   2178:                        }
                   2179:                } else {
1.116     djm      2180:                        if ((line = el_gets(el, &count)) == NULL ||
                   2181:                            count <= 0) {
1.66      jaredy   2182:                                printf("\n");
1.57      djm      2183:                                break;
1.66      jaredy   2184:                        }
1.57      djm      2185:                        history(hl, &hev, H_ENTER, line);
                   2186:                        if (strlcpy(cmd, line, sizeof(cmd)) >= sizeof(cmd)) {
                   2187:                                fprintf(stderr, "Error: input line too long\n");
                   2188:                                continue;
                   2189:                        }
1.44      djm      2190:                }
                   2191:
1.187     djm      2192:                cmd[strcspn(cmd, "\n")] = '\0';
1.44      djm      2193:
1.46      djm      2194:                /* Handle user interrupts gracefully during commands */
                   2195:                interrupted = 0;
1.197     dtucker  2196:                ssh_signal(SIGINT, cmd_interrupt);
1.46      djm      2197:
1.116     djm      2198:                err = parse_dispatch_command(conn, cmd, &remote_path,
1.187     djm      2199:                    startdir, batchmode, !interactive && el == NULL);
1.44      djm      2200:                if (err != 0)
                   2201:                        break;
                   2202:        }
1.197     dtucker  2203:        ssh_signal(SIGCHLD, SIG_DFL);
1.145     djm      2204:        free(remote_path);
1.182     djm      2205:        free(startdir);
1.145     djm      2206:        free(conn);
1.66      jaredy   2207:
                   2208:        if (el != NULL)
                   2209:                el_end(el);
1.44      djm      2210:
                   2211:        /* err == 1 signifies normal "quit" exit */
                   2212:        return (err >= 0 ? 0 : -1);
                   2213: }
1.34      fgsch    2214:
1.18      itojun   2215: static void
1.36      djm      2216: connect_to_server(char *path, char **args, int *in, int *out)
1.1       djm      2217: {
                   2218:        int c_in, c_out;
1.30      deraadt  2219:
1.1       djm      2220:        int inout[2];
1.30      deraadt  2221:
1.1       djm      2222:        if (socketpair(AF_UNIX, SOCK_STREAM, 0, inout) == -1)
                   2223:                fatal("socketpair: %s", strerror(errno));
                   2224:        *in = *out = inout[0];
                   2225:        c_in = c_out = inout[1];
                   2226:
1.36      djm      2227:        if ((sshpid = fork()) == -1)
1.1       djm      2228:                fatal("fork: %s", strerror(errno));
1.36      djm      2229:        else if (sshpid == 0) {
1.1       djm      2230:                if ((dup2(c_in, STDIN_FILENO) == -1) ||
                   2231:                    (dup2(c_out, STDOUT_FILENO) == -1)) {
                   2232:                        fprintf(stderr, "dup2: %s\n", strerror(errno));
1.47      djm      2233:                        _exit(1);
1.1       djm      2234:                }
                   2235:                close(*in);
                   2236:                close(*out);
                   2237:                close(c_in);
                   2238:                close(c_out);
1.46      djm      2239:
                   2240:                /*
                   2241:                 * The underlying ssh is in the same process group, so we must
1.56      deraadt  2242:                 * ignore SIGINT if we want to gracefully abort commands,
                   2243:                 * otherwise the signal will make it to the ssh process and
1.122     guenther 2244:                 * kill it too.  Contrawise, since sftp sends SIGTERMs to the
                   2245:                 * underlying ssh, it must *not* ignore that signal.
1.46      djm      2246:                 */
1.197     dtucker  2247:                ssh_signal(SIGINT, SIG_IGN);
                   2248:                ssh_signal(SIGTERM, SIG_DFL);
1.49      dtucker  2249:                execvp(path, args);
1.23      djm      2250:                fprintf(stderr, "exec: %s: %s\n", path, strerror(errno));
1.47      djm      2251:                _exit(1);
1.1       djm      2252:        }
                   2253:
1.197     dtucker  2254:        ssh_signal(SIGTERM, killchild);
                   2255:        ssh_signal(SIGINT, killchild);
                   2256:        ssh_signal(SIGHUP, killchild);
                   2257:        ssh_signal(SIGTSTP, suspchild);
                   2258:        ssh_signal(SIGTTIN, suspchild);
                   2259:        ssh_signal(SIGTTOU, suspchild);
                   2260:        ssh_signal(SIGCHLD, sigchld_handler);
1.1       djm      2261:        close(c_in);
                   2262:        close(c_out);
                   2263: }
                   2264:
1.18      itojun   2265: static void
1.1       djm      2266: usage(void)
                   2267: {
1.25      mpech    2268:        extern char *__progname;
1.27      markus   2269:
1.19      stevesk  2270:        fprintf(stderr,
1.201     djm      2271:            "usage: %s [-46AaCfNpqrv] [-B buffer_size] [-b batchfile] [-c cipher]\n"
1.190     tb       2272:            "          [-D sftp_server_path] [-F ssh_config] [-i identity_file]\n"
                   2273:            "          [-J destination] [-l limit] [-o ssh_option] [-P port]\n"
                   2274:            "          [-R num_requests] [-S program] [-s subsystem | sftp_server]\n"
                   2275:            "          destination\n",
1.181     millert  2276:            __progname);
1.1       djm      2277:        exit(1);
                   2278: }
                   2279:
1.2       stevesk  2280: int
1.1       djm      2281: main(int argc, char **argv)
                   2282: {
1.199     djm      2283:        int in, out, ch, err, tmp, port = -1, noisy = 0;
1.181     millert  2284:        char *host = NULL, *user, *cp, *file2 = NULL;
1.198     dtucker  2285:        int debug_level = 0;
1.17      mouring  2286:        char *file1 = NULL, *sftp_server = NULL;
1.23      djm      2287:        char *ssh_program = _PATH_SSH_PROGRAM, *sftp_direct = NULL;
1.126     djm      2288:        const char *errstr;
1.17      mouring  2289:        LogLevel ll = SYSLOG_LEVEL_INFO;
                   2290:        arglist args;
1.3       djm      2291:        extern int optind;
                   2292:        extern char *optarg;
1.112     djm      2293:        struct sftp_conn *conn;
1.207     djm      2294:        size_t copy_buffer_len = 0;
                   2295:        size_t num_requests = 0;
1.126     djm      2296:        long long limit_kbps = 0;
1.67      djm      2297:
                   2298:        /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
                   2299:        sanitise_stdfd();
1.146     dtucker  2300:        setlocale(LC_CTYPE, "");
1.1       djm      2301:
1.70      djm      2302:        memset(&args, '\0', sizeof(args));
1.17      mouring  2303:        args.list = NULL;
1.80      djm      2304:        addargs(&args, "%s", ssh_program);
1.17      mouring  2305:        addargs(&args, "-oForwardX11 no");
1.69      reyk     2306:        addargs(&args, "-oPermitLocalCommand no");
1.21      stevesk  2307:        addargs(&args, "-oClearAllForwardings yes");
1.40      djm      2308:
1.17      mouring  2309:        ll = SYSLOG_LEVEL_INFO;
1.40      djm      2310:        infile = stdin;
1.3       djm      2311:
1.109     djm      2312:        while ((ch = getopt(argc, argv,
1.201     djm      2313:            "1246AafhNpqrvCc:D:i:l:o:s:S:b:B:F:J:P:R:")) != -1) {
1.3       djm      2314:                switch (ch) {
1.108     djm      2315:                /* Passed through to ssh(1) */
1.201     djm      2316:                case 'A':
1.108     djm      2317:                case '4':
                   2318:                case '6':
1.3       djm      2319:                case 'C':
1.108     djm      2320:                        addargs(&args, "-%c", ch);
                   2321:                        break;
                   2322:                /* Passed through to ssh(1) with argument */
                   2323:                case 'F':
1.190     tb       2324:                case 'J':
1.108     djm      2325:                case 'c':
                   2326:                case 'i':
                   2327:                case 'o':
1.113     halex    2328:                        addargs(&args, "-%c", ch);
                   2329:                        addargs(&args, "%s", optarg);
1.108     djm      2330:                        break;
                   2331:                case 'q':
1.143     djm      2332:                        ll = SYSLOG_LEVEL_ERROR;
                   2333:                        quiet = 1;
1.108     djm      2334:                        showprogress = 0;
                   2335:                        addargs(&args, "-%c", ch);
1.3       djm      2336:                        break;
1.109     djm      2337:                case 'P':
1.181     millert  2338:                        port = a2port(optarg);
                   2339:                        if (port <= 0)
                   2340:                                fatal("Bad port \"%s\"\n", optarg);
1.109     djm      2341:                        break;
1.3       djm      2342:                case 'v':
1.17      mouring  2343:                        if (debug_level < 3) {
                   2344:                                addargs(&args, "-v");
                   2345:                                ll = SYSLOG_LEVEL_DEBUG1 + debug_level;
                   2346:                        }
                   2347:                        debug_level++;
1.3       djm      2348:                        break;
1.7       markus   2349:                case '1':
1.198     dtucker  2350:                        fatal("SSH protocol v.1 is no longer supported");
1.7       markus   2351:                        break;
1.108     djm      2352:                case '2':
1.198     dtucker  2353:                        /* accept silently */
1.148     djm      2354:                        break;
                   2355:                case 'a':
                   2356:                        global_aflag = 1;
1.7       markus   2357:                        break;
1.108     djm      2358:                case 'B':
                   2359:                        copy_buffer_len = strtol(optarg, &cp, 10);
                   2360:                        if (copy_buffer_len == 0 || *cp != '\0')
                   2361:                                fatal("Invalid buffer size \"%s\"", optarg);
1.3       djm      2362:                        break;
1.10      deraadt  2363:                case 'b':
1.39      djm      2364:                        if (batchmode)
                   2365:                                fatal("Batch file already specified.");
                   2366:
                   2367:                        /* Allow "-" as stdin */
1.56      deraadt  2368:                        if (strcmp(optarg, "-") != 0 &&
1.65      djm      2369:                            (infile = fopen(optarg, "r")) == NULL)
1.39      djm      2370:                                fatal("%s (%s).", strerror(errno), optarg);
1.34      fgsch    2371:                        showprogress = 0;
1.143     djm      2372:                        quiet = batchmode = 1;
1.62      djm      2373:                        addargs(&args, "-obatchmode yes");
1.156     djm      2374:                        break;
                   2375:                case 'f':
                   2376:                        global_fflag = 1;
1.10      deraadt  2377:                        break;
1.199     djm      2378:                case 'N':
                   2379:                        noisy = 1; /* Used to clear quiet mode after getopt */
                   2380:                        break;
1.111     djm      2381:                case 'p':
                   2382:                        global_pflag = 1;
                   2383:                        break;
1.109     djm      2384:                case 'D':
1.23      djm      2385:                        sftp_direct = optarg;
1.111     djm      2386:                        break;
1.126     djm      2387:                case 'l':
                   2388:                        limit_kbps = strtonum(optarg, 1, 100 * 1024 * 1024,
                   2389:                            &errstr);
                   2390:                        if (errstr != NULL)
                   2391:                                usage();
                   2392:                        limit_kbps *= 1024; /* kbps */
                   2393:                        break;
1.111     djm      2394:                case 'r':
                   2395:                        global_rflag = 1;
1.24      djm      2396:                        break;
1.26      djm      2397:                case 'R':
                   2398:                        num_requests = strtol(optarg, &cp, 10);
                   2399:                        if (num_requests == 0 || *cp != '\0')
1.27      markus   2400:                                fatal("Invalid number of requests \"%s\"",
1.26      djm      2401:                                    optarg);
1.108     djm      2402:                        break;
                   2403:                case 's':
                   2404:                        sftp_server = optarg;
                   2405:                        break;
                   2406:                case 'S':
                   2407:                        ssh_program = optarg;
                   2408:                        replacearg(&args, 0, "%s", ssh_program);
1.23      djm      2409:                        break;
1.3       djm      2410:                case 'h':
                   2411:                default:
1.1       djm      2412:                        usage();
                   2413:                }
                   2414:        }
1.201     djm      2415:
                   2416:        /* Do this last because we want the user to be able to override it */
                   2417:        addargs(&args, "-oForwardAgent no");
1.45      djm      2418:
                   2419:        if (!isatty(STDERR_FILENO))
                   2420:                showprogress = 0;
1.199     djm      2421:
                   2422:        if (noisy)
                   2423:                quiet = 0;
1.1       djm      2424:
1.29      markus   2425:        log_init(argv[0], ll, SYSLOG_FACILITY_USER, 1);
                   2426:
1.23      djm      2427:        if (sftp_direct == NULL) {
                   2428:                if (optind == argc || argc > (optind + 2))
                   2429:                        usage();
1.181     millert  2430:                argv += optind;
1.23      djm      2431:
1.181     millert  2432:                switch (parse_uri("sftp", *argv, &user, &host, &tmp, &file1)) {
                   2433:                case -1:
                   2434:                        usage();
                   2435:                        break;
                   2436:                case 0:
                   2437:                        if (tmp != -1)
                   2438:                                port = tmp;
                   2439:                        break;
                   2440:                default:
1.192     dtucker  2441:                        /* Try with user, host and path. */
1.181     millert  2442:                        if (parse_user_host_path(*argv, &user, &host,
1.192     dtucker  2443:                            &file1) == 0)
                   2444:                                break;
                   2445:                        /* Try with user and host. */
                   2446:                        if (parse_user_host_port(*argv, &user, &host, NULL)
                   2447:                            == 0)
                   2448:                                break;
                   2449:                        /* Treat as a plain hostname. */
                   2450:                        host = xstrdup(*argv);
                   2451:                        host = cleanhostname(host);
1.181     millert  2452:                        break;
1.23      djm      2453:                }
1.181     millert  2454:                file2 = *(argv + 1);
1.3       djm      2455:
1.23      djm      2456:                if (!*host) {
                   2457:                        fprintf(stderr, "Missing hostname\n");
1.1       djm      2458:                        usage();
                   2459:                }
                   2460:
1.181     millert  2461:                if (port != -1)
                   2462:                        addargs(&args, "-oPort %d", port);
                   2463:                if (user != NULL) {
                   2464:                        addargs(&args, "-l");
                   2465:                        addargs(&args, "%s", user);
                   2466:                }
1.23      djm      2467:
                   2468:                /* no subsystem if the server-spec contains a '/' */
                   2469:                if (sftp_server == NULL || strchr(sftp_server, '/') == NULL)
                   2470:                        addargs(&args, "-s");
                   2471:
1.115     guenther 2472:                addargs(&args, "--");
1.23      djm      2473:                addargs(&args, "%s", host);
1.27      markus   2474:                addargs(&args, "%s", (sftp_server != NULL ?
1.23      djm      2475:                    sftp_server : "sftp"));
                   2476:
1.36      djm      2477:                connect_to_server(ssh_program, args.list, &in, &out);
1.23      djm      2478:        } else {
                   2479:                args.list = NULL;
                   2480:                addargs(&args, "sftp-server");
                   2481:
1.36      djm      2482:                connect_to_server(sftp_direct, args.list, &in, &out);
1.1       djm      2483:        }
1.70      djm      2484:        freeargs(&args);
1.1       djm      2485:
1.126     djm      2486:        conn = do_init(in, out, copy_buffer_len, num_requests, limit_kbps);
1.112     djm      2487:        if (conn == NULL)
                   2488:                fatal("Couldn't initialise connection to server");
                   2489:
1.143     djm      2490:        if (!quiet) {
1.112     djm      2491:                if (sftp_direct == NULL)
                   2492:                        fprintf(stderr, "Connected to %s.\n", host);
                   2493:                else
                   2494:                        fprintf(stderr, "Attached to %s.\n", sftp_direct);
                   2495:        }
                   2496:
                   2497:        err = interactive_loop(conn, file1, file2);
1.1       djm      2498:
                   2499:        close(in);
                   2500:        close(out);
1.39      djm      2501:        if (batchmode)
1.10      deraadt  2502:                fclose(infile);
1.1       djm      2503:
1.185     bluhm    2504:        while (waitpid(sshpid, NULL, 0) == -1 && sshpid > 1)
1.28      markus   2505:                if (errno != EINTR)
                   2506:                        fatal("Couldn't wait for ssh process: %s",
                   2507:                            strerror(errno));
1.1       djm      2508:
1.33      djm      2509:        exit(err == 0 ? 0 : 1);
1.1       djm      2510: }