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

Annotation of src/usr.bin/ftp/main.c, Revision 1.93

1.93    ! jsing       1: /*     $OpenBSD: main.c,v 1.92 2014/07/16 04:52:43 lteo Exp $  */
1.36      millert     2: /*     $NetBSD: main.c,v 1.24 1997/08/18 10:20:26 lukem Exp $  */
1.5       deraadt     3:
1.1       deraadt     4: /*
1.44      itojun      5:  * Copyright (C) 1997 and 1998 WIDE Project.
                      6:  * All rights reserved.
                      7:  *
                      8:  * Redistribution and use in source and binary forms, with or without
                      9:  * modification, are permitted provided that the following conditions
                     10:  * are met:
                     11:  * 1. Redistributions of source code must retain the above copyright
                     12:  *    notice, this list of conditions and the following disclaimer.
                     13:  * 2. Redistributions in binary form must reproduce the above copyright
                     14:  *    notice, this list of conditions and the following disclaimer in the
                     15:  *    documentation and/or other materials provided with the distribution.
                     16:  * 3. Neither the name of the project nor the names of its contributors
                     17:  *    may be used to endorse or promote products derived from this software
                     18:  *    without specific prior written permission.
                     19:  *
                     20:  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
                     21:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     22:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     23:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
                     24:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     25:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     26:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     27:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     28:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     29:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     30:  * SUCH DAMAGE.
                     31:  */
                     32:
                     33: /*
1.1       deraadt    34:  * Copyright (c) 1985, 1989, 1993, 1994
                     35:  *     The Regents of the University of California.  All rights reserved.
                     36:  *
                     37:  * Redistribution and use in source and binary forms, with or without
                     38:  * modification, are permitted provided that the following conditions
                     39:  * are met:
                     40:  * 1. Redistributions of source code must retain the above copyright
                     41:  *    notice, this list of conditions and the following disclaimer.
                     42:  * 2. Redistributions in binary form must reproduce the above copyright
                     43:  *    notice, this list of conditions and the following disclaimer in the
                     44:  *    documentation and/or other materials provided with the distribution.
1.53      millert    45:  * 3. Neither the name of the University nor the names of its contributors
1.1       deraadt    46:  *    may be used to endorse or promote products derived from this software
                     47:  *    without specific prior written permission.
                     48:  *
                     49:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     50:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     51:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     52:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     53:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     54:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     55:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     56:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     57:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     58:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     59:  * SUCH DAMAGE.
                     60:  */
                     61:
                     62: /*
                     63:  * FTP User Program -- Command Interface.
                     64:  */
                     65: #include <sys/types.h>
                     66: #include <sys/socket.h>
                     67:
1.18      millert    68: #include <ctype.h>
1.1       deraadt    69: #include <err.h>
1.86      jca        70: #include <limits.h>
1.1       deraadt    71: #include <netdb.h>
                     72: #include <pwd.h>
                     73: #include <stdio.h>
1.39      deraadt    74: #include <errno.h>
1.18      millert    75: #include <stdlib.h>
1.1       deraadt    76: #include <string.h>
                     77: #include <unistd.h>
                     78:
1.91      jsing      79: #include <ressl.h>
                     80:
                     81: #include "cmds.h"
1.1       deraadt    82: #include "ftp_var.h"
                     83:
1.86      jca        84: #ifndef SMALL
                     85: char * const ssl_verify_opts[] = {
                     86: #define SSL_CAFILE     0
                     87:        "cafile",
                     88: #define SSL_CAPATH     1
                     89:        "capath",
                     90: #define SSL_CIPHERS    2
                     91:        "ciphers",
                     92: #define SSL_DONTVERIFY 3
                     93:        "dont",
                     94: #define SSL_DOVERIFY   4
                     95:        "do",
                     96: #define SSL_VERIFYDEPTH        5
                     97:        "depth",
                     98:        NULL
                     99: };
1.91      jsing     100:
                    101: struct ressl_config *ressl_config;
1.86      jca       102: #endif /* !SMALL */
                    103:
1.49      deraadt   104: int family = PF_UNSPEC;
1.81      halex     105: int pipeout;
1.49      deraadt   106:
1.1       deraadt   107: int
1.54      deraadt   108: main(volatile int argc, char *argv[])
1.1       deraadt   109: {
1.34      millert   110:        int ch, top, rval;
1.1       deraadt   111:        struct passwd *pw = NULL;
1.44      itojun    112:        char *cp, homedir[MAXPATHLEN];
1.38      millert   113:        char *outfile = NULL;
1.62      tedu      114:        const char *errstr;
1.28      millert   115:        int dumb_terminal = 0;
1.91      jsing     116: #ifndef SMALL
                    117:        long long depth;
                    118: #endif
1.1       deraadt   119:
1.44      itojun    120:        ftpport = "ftp";
                    121:        httpport = "http";
1.61      deraadt   122: #ifndef SMALL
                    123:        httpsport = "https";
1.69      martynas  124: #endif /* !SMALL */
1.44      itojun    125:        gateport = getenv("FTPSERVERPORT");
1.51      millert   126:        if (gateport == NULL || *gateport == '\0')
1.44      itojun    127:                gateport = "ftpgate";
1.1       deraadt   128:        doglob = 1;
                    129:        interactive = 1;
                    130:        autologin = 1;
1.37      millert   131:        passivemode = 1;
                    132:        activefallback = 1;
1.17      millert   133:        preserve = 1;
                    134:        verbose = 0;
                    135:        progress = 0;
1.36      millert   136:        gatemode = 0;
1.25      millert   137: #ifndef SMALL
1.22      millert   138:        editing = 0;
1.28      millert   139:        el = NULL;
                    140:        hist = NULL;
1.63      pyr       141:        cookiefile = NULL;
1.67      martynas  142:        resume = 0;
1.82      haesbaer  143:        srcaddr = NULL;
1.77      martynas  144:        marg_sl = sl_init();
1.69      martynas  145: #endif /* !SMALL */
1.8       kstailey  146:        mark = HASHBYTES;
1.47      itojun    147: #ifdef INET6
                    148:        epsv4 = 1;
                    149: #else
                    150:        epsv4 = 0;
                    151: #endif
                    152:        epsv4bad = 0;
1.1       deraadt   153:
1.37      millert   154:        /* Set default operation mode based on FTPMODE environment variable */
1.51      millert   155:        if ((cp = getenv("FTPMODE")) != NULL && *cp != '\0') {
1.37      millert   156:                if (strcmp(cp, "passive") == 0) {
                    157:                        passivemode = 1;
                    158:                        activefallback = 0;
                    159:                } else if (strcmp(cp, "active") == 0) {
                    160:                        passivemode = 0;
                    161:                        activefallback = 0;
                    162:                } else if (strcmp(cp, "gate") == 0) {
                    163:                        gatemode = 1;
                    164:                } else if (strcmp(cp, "auto") == 0) {
                    165:                        passivemode = 1;
                    166:                        activefallback = 1;
                    167:                } else
                    168:                        warnx("unknown FTPMODE: %s.  Using defaults", cp);
                    169:        }
                    170:
                    171:        if (strcmp(__progname, "gate-ftp") == 0)
1.36      millert   172:                gatemode = 1;
                    173:        gateserver = getenv("FTPSERVER");
                    174:        if (gateserver == NULL || *gateserver == '\0')
                    175:                gateserver = GATE_SERVER;
                    176:        if (gatemode) {
                    177:                if (*gateserver == '\0') {
                    178:                        warnx(
1.37      millert   179: "Neither $FTPSERVER nor $GATE_SERVER is defined; disabling gate-ftp");
1.36      millert   180:                        gatemode = 0;
                    181:                }
                    182:        }
1.17      millert   183:
1.31      millert   184:        cp = getenv("TERM");
1.51      millert   185:        dumb_terminal = (cp == NULL || *cp == '\0' || !strcmp(cp, "dumb") ||
1.31      millert   186:            !strcmp(cp, "emacs") || !strcmp(cp, "su"));
1.17      millert   187:        fromatty = isatty(fileno(stdin));
1.22      millert   188:        if (fromatty) {
1.17      millert   189:                verbose = 1;            /* verbose if from a tty */
1.25      millert   190: #ifndef SMALL
1.28      millert   191:                if (!dumb_terminal)
1.34      millert   192:                        editing = 1;    /* editing mode on if tty is usable */
1.69      martynas  193: #endif /* !SMALL */
1.22      millert   194:        }
1.30      deraadt   195:
                    196:        ttyout = stdout;
1.33      millert   197:        if (isatty(fileno(ttyout)) && !dumb_terminal && foregroundproc())
1.34      millert   198:                progress = 1;           /* progress bar on if tty is usable */
1.32      deraadt   199:
1.63      pyr       200: #ifndef SMALL
                    201:        cookiefile = getenv("http_cookies");
1.69      martynas  202: #endif /* !SMALL */
1.88      lteo      203:        httpuseragent = NULL;
1.63      pyr       204:
1.86      jca       205:        while ((ch = getopt(argc, argv,
1.88      lteo      206:                    "46AaCc:dD:Eegik:mno:pP:r:S:s:tU:vV")) != -1) {
1.1       deraadt   207:                switch (ch) {
1.49      deraadt   208:                case '4':
                    209:                        family = PF_INET;
                    210:                        break;
                    211:                case '6':
                    212:                        family = PF_INET6;
                    213:                        break;
1.37      millert   214:                case 'A':
                    215:                        activefallback = 0;
                    216:                        passivemode = 0;
                    217:                        break;
                    218:
1.17      millert   219:                case 'a':
                    220:                        anonftp = 1;
                    221:                        break;
                    222:
1.67      martynas  223:                case 'C':
                    224: #ifndef SMALL
                    225:                        resume = 1;
1.69      martynas  226: #endif /* !SMALL */
1.67      martynas  227:                        break;
                    228:
1.63      pyr       229:                case 'c':
                    230: #ifndef SMALL
                    231:                        cookiefile = optarg;
1.69      martynas  232: #endif /* !SMALL */
1.63      pyr       233:                        break;
                    234:
1.87      deraadt   235:                case 'D':
                    236:                        action = optarg;
                    237:                        break;
1.1       deraadt   238:                case 'd':
1.70      martynas  239: #ifndef SMALL
1.1       deraadt   240:                        options |= SO_DEBUG;
                    241:                        debug++;
1.70      martynas  242: #endif /* !SMALL */
1.1       deraadt   243:                        break;
1.17      millert   244:
1.59      fgsch     245:                case 'E':
                    246:                        epsv4 = 0;
                    247:                        break;
                    248:
1.28      millert   249:                case 'e':
                    250: #ifndef SMALL
1.22      millert   251:                        editing = 0;
1.69      martynas  252: #endif /* !SMALL */
1.21      kstailey  253:                        break;
                    254:
1.1       deraadt   255:                case 'g':
                    256:                        doglob = 0;
                    257:                        break;
                    258:
                    259:                case 'i':
                    260:                        interactive = 0;
1.41      millert   261:                        break;
                    262:
1.65      espie     263:                case 'k':
                    264:                        keep_alive_timeout = strtonum(optarg, 0, INT_MAX,
                    265:                            &errstr);
                    266:                        if (errstr != NULL) {
                    267:                                warnx("keep alive amount is %s: %s", errstr,
                    268:                                        optarg);
                    269:                                usage();
                    270:                        }
                    271:                        break;
1.41      millert   272:                case 'm':
1.42      millert   273:                        progress = -1;
1.1       deraadt   274:                        break;
                    275:
                    276:                case 'n':
                    277:                        autologin = 0;
                    278:                        break;
                    279:
1.38      millert   280:                case 'o':
                    281:                        outfile = optarg;
1.81      halex     282:                        if (*outfile == '\0') {
                    283:                                pipeout = 0;
                    284:                                outfile = NULL;
                    285:                                ttyout = stdout;
                    286:                        } else {
                    287:                                pipeout = strcmp(outfile, "-") == 0;
                    288:                                ttyout = pipeout ? stderr : stdout;
                    289:                        }
1.38      millert   290:                        break;
                    291:
1.3       deraadt   292:                case 'p':
1.17      millert   293:                        passivemode = 1;
1.37      millert   294:                        activefallback = 0;
1.3       deraadt   295:                        break;
                    296:
1.17      millert   297:                case 'P':
1.44      itojun    298:                        ftpport = optarg;
1.7       mickey    299:                        break;
                    300:
1.18      millert   301:                case 'r':
1.62      tedu      302:                        retry_connect = strtonum(optarg, 0, INT_MAX, &errstr);
1.65      espie     303:                        if (errstr != NULL) {
                    304:                                warnx("retry amount is %s: %s", errstr,
1.62      tedu      305:                                        optarg);
1.65      espie     306:                                usage();
                    307:                        }
1.18      millert   308:                        break;
                    309:
1.86      jca       310:                case 'S':
                    311: #ifndef SMALL
1.91      jsing     312:                        if (ressl_config == NULL) {
                    313:                                ressl_config = ressl_config_new();
                    314:                                if (ressl_config == NULL)
                    315:                                        errx(1, "ressl config failed");
                    316:                        }
                    317:
1.86      jca       318:                        cp = optarg;
                    319:                        while (*cp) {
                    320:                                char    *str;
                    321:                                switch (getsubopt(&cp, ssl_verify_opts, &str)) {
                    322:                                case SSL_CAFILE:
                    323:                                        if (str == NULL)
                    324:                                                errx(1, "missing CA file");
1.93    ! jsing     325:                                        if (ressl_config_set_ca_file(
        !           326:                                            ressl_config, str) != 0)
        !           327:                                                errx(1, "ressl ca file failed");
1.86      jca       328:                                        break;
                    329:                                case SSL_CAPATH:
                    330:                                        if (str == NULL)
                    331:                                                errx(1, "missing CA directory"
                    332:                                                    " path");
1.93    ! jsing     333:                                        if (ressl_config_set_ca_path(
        !           334:                                            ressl_config, str) != 0)
        !           335:                                                errx(1, "ressl ca path failed");
1.86      jca       336:                                        break;
                    337:                                case SSL_CIPHERS:
                    338:                                        if (str == NULL)
                    339:                                                errx(1, "missing cipher list");
1.93    ! jsing     340:                                        if (ressl_config_set_ciphers(
        !           341:                                            ressl_config, str) != 0)
        !           342:                                                errx(1, "ressl ciphers failed");
1.86      jca       343:                                        break;
                    344:                                case SSL_DONTVERIFY:
1.91      jsing     345:                                        ressl_config_insecure_no_verify(
                    346:                                            ressl_config);
1.86      jca       347:                                        break;
                    348:                                case SSL_DOVERIFY:
1.91      jsing     349:                                        ressl_config_verify(ressl_config);
1.86      jca       350:                                        break;
                    351:                                case SSL_VERIFYDEPTH:
                    352:                                        if (str == NULL)
                    353:                                                errx(1, "missing depth");
1.91      jsing     354:                                        depth = strtonum(str, 0, INT_MAX,
                    355:                                            &errstr);
1.86      jca       356:                                        if (errstr)
                    357:                                                errx(1, "certificate "
                    358:                                                    "validation depth is %s",
                    359:                                                    errstr);
1.91      jsing     360:                                        ressl_config_set_verify_depth(
                    361:                                            ressl_config, (int)depth);
1.86      jca       362:                                        break;
                    363:                                default:
                    364:                                        errx(1, "unknown -S suboption `%s'",
                    365:                                            suboptarg ? suboptarg : "");
                    366:                                        /* NOTREACHED */
                    367:                                }
                    368:                        }
                    369: #endif
                    370:                        break;
                    371:
1.82      haesbaer  372:                case 's':
                    373: #ifndef SMALL
                    374:                        srcaddr = optarg;
                    375: #endif /* !SMALL */
                    376:                        break;
                    377:
1.1       deraadt   378:                case 't':
1.17      millert   379:                        trace = 1;
1.1       deraadt   380:                        break;
                    381:
1.89      halex     382: #ifndef SMALL
1.88      lteo      383:                case 'U':
1.89      halex     384:                        free (httpuseragent);
1.88      lteo      385:                        if (strcspn(optarg, "\r\n") != strlen(optarg))
                    386:                                errx(1, "Invalid User-Agent: %s.", optarg);
                    387:                        if (asprintf(&httpuseragent, "User-Agent: %s",
                    388:                            optarg) == -1)
                    389:                                errx(1, "Can't allocate memory for HTTP(S) "
                    390:                                    "User-Agent");
1.89      halex     391:                        break;
1.88      lteo      392: #endif /* !SMALL */
                    393:
1.1       deraadt   394:                case 'v':
1.17      millert   395:                        verbose = 1;
                    396:                        break;
                    397:
                    398:                case 'V':
                    399:                        verbose = 0;
1.1       deraadt   400:                        break;
                    401:
                    402:                default:
1.17      millert   403:                        usage();
1.1       deraadt   404:                }
                    405:        }
                    406:        argc -= optind;
                    407:        argv += optind;
1.63      pyr       408:
                    409: #ifndef SMALL
                    410:        cookie_load();
1.69      martynas  411: #endif /* !SMALL */
1.89      halex     412:        if (httpuseragent == NULL)
                    413:                httpuseragent = HTTP_USER_AGENT;
1.1       deraadt   414:
                    415:        cpend = 0;      /* no pending replies */
                    416:        proxy = 0;      /* proxy not active */
                    417:        crflag = 1;     /* strip c.r. on ascii gets */
                    418:        sendport = -1;  /* not using ports */
                    419:        /*
                    420:         * Set up the home directory in case we're globbing.
                    421:         */
                    422:        cp = getlogin();
                    423:        if (cp != NULL) {
                    424:                pw = getpwnam(cp);
                    425:        }
                    426:        if (pw == NULL)
                    427:                pw = getpwuid(getuid());
                    428:        if (pw != NULL) {
1.52      deraadt   429:                (void)strlcpy(homedir, pw->pw_dir, sizeof homedir);
1.1       deraadt   430:                home = homedir;
1.3       deraadt   431:        }
1.9       michaels  432:
1.17      millert   433:        setttywidth(0);
1.18      millert   434:        (void)signal(SIGWINCH, setttywidth);
1.34      millert   435:
1.17      millert   436:        if (argc > 0) {
1.44      itojun    437:                if (isurl(argv[0])) {
1.38      millert   438:                        rval = auto_fetch(argc, argv, outfile);
1.17      millert   439:                        if (rval >= 0)          /* -1 == connected and cd-ed */
                    440:                                exit(rval);
                    441:                } else {
1.77      martynas  442: #ifndef SMALL
1.3       deraadt   443:                        char *xargv[5];
                    444:
                    445:                        if (setjmp(toplevel))
                    446:                                exit(0);
1.20      millert   447:                        (void)signal(SIGINT, (sig_t)intr);
                    448:                        (void)signal(SIGPIPE, (sig_t)lostpeer);
1.3       deraadt   449:                        xargv[0] = __progname;
1.17      millert   450:                        xargv[1] = argv[0];
                    451:                        xargv[2] = argv[1];
                    452:                        xargv[3] = argv[2];
                    453:                        xargv[4] = NULL;
1.18      millert   454:                        do {
                    455:                                setpeer(argc+1, xargv);
                    456:                                if (!retry_connect)
                    457:                                        break;
                    458:                                if (!connected) {
                    459:                                        macnum = 0;
1.30      deraadt   460:                                        fputs("Retrying...\n", ttyout);
1.18      millert   461:                                        sleep(retry_connect);
                    462:                                }
                    463:                        } while (!connected);
1.35      mickey    464:                        retry_connect = 0; /* connected, stop hiding msgs */
1.77      martynas  465: #endif /* !SMALL */
1.3       deraadt   466:                }
1.1       deraadt   467:        }
1.28      millert   468: #ifndef SMALL
                    469:        controlediting();
1.1       deraadt   470:        top = setjmp(toplevel) == 0;
                    471:        if (top) {
1.20      millert   472:                (void)signal(SIGINT, (sig_t)intr);
                    473:                (void)signal(SIGPIPE, (sig_t)lostpeer);
1.1       deraadt   474:        }
                    475:        for (;;) {
                    476:                cmdscanner(top);
                    477:                top = 1;
                    478:        }
1.77      martynas  479: #else /* !SMALL */
                    480:        usage();
                    481: #endif /* !SMALL */
1.1       deraadt   482: }
                    483:
                    484: void
1.58      deraadt   485: intr(void)
1.1       deraadt   486: {
1.90      deraadt   487:        int save_errno = errno;
1.1       deraadt   488:
1.90      deraadt   489:        write(fileno(ttyout), "\n\r", 2);
1.17      millert   490:        alarmtimer(0);
1.90      deraadt   491:
                    492:        errno = save_errno;
1.1       deraadt   493:        longjmp(toplevel, 1);
                    494: }
                    495:
                    496: void
1.58      deraadt   497: lostpeer(void)
1.1       deraadt   498: {
1.39      deraadt   499:        int save_errno = errno;
1.1       deraadt   500:
1.17      millert   501:        alarmtimer(0);
1.1       deraadt   502:        if (connected) {
                    503:                if (cout != NULL) {
1.66      moritz    504:                        (void)shutdown(fileno(cout), SHUT_RDWR);
1.18      millert   505:                        (void)fclose(cout);
1.1       deraadt   506:                        cout = NULL;
                    507:                }
                    508:                if (data >= 0) {
1.66      moritz    509:                        (void)shutdown(data, SHUT_RDWR);
1.18      millert   510:                        (void)close(data);
1.1       deraadt   511:                        data = -1;
                    512:                }
                    513:                connected = 0;
                    514:        }
                    515:        pswitch(1);
                    516:        if (connected) {
                    517:                if (cout != NULL) {
1.66      moritz    518:                        (void)shutdown(fileno(cout), SHUT_RDWR);
1.18      millert   519:                        (void)fclose(cout);
1.1       deraadt   520:                        cout = NULL;
                    521:                }
                    522:                connected = 0;
                    523:        }
                    524:        proxflag = 0;
                    525:        pswitch(0);
1.39      deraadt   526:        errno = save_errno;
1.1       deraadt   527: }
                    528:
1.77      martynas  529: #ifndef SMALL
1.1       deraadt   530: /*
1.17      millert   531:  * Generate a prompt
                    532:  */
1.1       deraadt   533: char *
1.58      deraadt   534: prompt(void)
1.1       deraadt   535: {
1.17      millert   536:        return ("ftp> ");
1.1       deraadt   537: }
                    538:
                    539: /*
                    540:  * Command parser.
                    541:  */
                    542: void
1.58      deraadt   543: cmdscanner(int top)
1.1       deraadt   544: {
                    545:        struct cmd *c;
1.17      millert   546:        int num;
1.55      otto      547:        HistEvent hev;
1.1       deraadt   548:
1.77      martynas  549:        if (!top && !editing)
1.30      deraadt   550:                (void)putc('\n', ttyout);
1.1       deraadt   551:        for (;;) {
1.17      millert   552:                if (!editing) {
                    553:                        if (fromatty) {
1.30      deraadt   554:                                fputs(prompt(), ttyout);
                    555:                                (void)fflush(ttyout);
1.17      millert   556:                        }
                    557:                        if (fgets(line, sizeof(line), stdin) == NULL)
                    558:                                quit(0, 0);
                    559:                        num = strlen(line);
                    560:                        if (num == 0)
                    561:                                break;
                    562:                        if (line[--num] == '\n') {
                    563:                                if (num == 0)
                    564:                                        break;
                    565:                                line[num] = '\0';
                    566:                        } else if (num == sizeof(line) - 2) {
1.30      deraadt   567:                                fputs("sorry, input line too long.\n", ttyout);
1.17      millert   568:                                while ((num = getchar()) != '\n' && num != EOF)
                    569:                                        /* void */;
                    570:                                break;
                    571:                        } /* else it was a line without a newline */
                    572:                } else {
                    573:                        const char *buf;
                    574:                        cursor_pos = NULL;
                    575:
1.90      deraadt   576:                        if ((buf = el_gets(el, &num)) == NULL || num == 0) {
                    577:                                putc('\n', ttyout);
                    578:                                fflush(ttyout);
1.17      millert   579:                                quit(0, 0);
1.90      deraadt   580:                        }
1.46      fgsch     581:                        if (buf[--num] == '\n') {
1.17      millert   582:                                if (num == 0)
                    583:                                        break;
1.46      fgsch     584:                        }
                    585:                        if (num >= sizeof(line)) {
1.30      deraadt   586:                                fputs("sorry, input line too long.\n", ttyout);
1.1       deraadt   587:                                break;
1.17      millert   588:                        }
1.34      millert   589:                        memcpy(line, buf, (size_t)num);
1.17      millert   590:                        line[num] = '\0';
1.55      otto      591:                        history(hist, &hev, H_ENTER, buf);
1.17      millert   592:                }
                    593:
1.1       deraadt   594:                makeargv();
1.17      millert   595:                if (margc == 0)
1.1       deraadt   596:                        continue;
                    597:                c = getcmd(margv[0]);
                    598:                if (c == (struct cmd *)-1) {
1.30      deraadt   599:                        fputs("?Ambiguous command.\n", ttyout);
1.1       deraadt   600:                        continue;
                    601:                }
                    602:                if (c == 0) {
1.24      millert   603:                        /*
                    604:                         * Give editline(3) a shot at unknown commands.
                    605:                         * XXX - bogus commands with a colon in
                    606:                         *       them will not elicit an error.
                    607:                         */
1.45      markus    608:                        if (editing &&
1.55      otto      609:                            el_parse(el, margc, (const char **)margv) != 0)
1.30      deraadt   610:                                fputs("?Invalid command.\n", ttyout);
1.1       deraadt   611:                        continue;
                    612:                }
                    613:                if (c->c_conn && !connected) {
1.30      deraadt   614:                        fputs("Not connected.\n", ttyout);
1.1       deraadt   615:                        continue;
                    616:                }
1.17      millert   617:                confirmrest = 0;
1.1       deraadt   618:                (*c->c_handler)(margc, margv);
                    619:                if (bell && c->c_bell)
1.30      deraadt   620:                        (void)putc('\007', ttyout);
1.1       deraadt   621:                if (c->c_handler != help)
                    622:                        break;
                    623:        }
1.20      millert   624:        (void)signal(SIGINT, (sig_t)intr);
                    625:        (void)signal(SIGPIPE, (sig_t)lostpeer);
1.1       deraadt   626: }
                    627:
                    628: struct cmd *
1.58      deraadt   629: getcmd(const char *name)
1.1       deraadt   630: {
1.17      millert   631:        const char *p, *q;
1.1       deraadt   632:        struct cmd *c, *found;
                    633:        int nmatches, longest;
1.2       deraadt   634:
                    635:        if (name == NULL)
                    636:                return (0);
1.1       deraadt   637:
                    638:        longest = 0;
                    639:        nmatches = 0;
                    640:        found = 0;
1.17      millert   641:        for (c = cmdtab; (p = c->c_name) != NULL; c++) {
1.1       deraadt   642:                for (q = name; *q == *p++; q++)
                    643:                        if (*q == 0)            /* exact match? */
                    644:                                return (c);
                    645:                if (!*q) {                      /* the name was a prefix */
                    646:                        if (q - name > longest) {
                    647:                                longest = q - name;
                    648:                                nmatches = 1;
                    649:                                found = c;
                    650:                        } else if (q - name == longest)
                    651:                                nmatches++;
                    652:                }
                    653:        }
                    654:        if (nmatches > 1)
                    655:                return ((struct cmd *)-1);
                    656:        return (found);
                    657: }
                    658:
                    659: /*
                    660:  * Slice a string up into argc/argv.
                    661:  */
                    662:
                    663: int slrflag;
                    664:
                    665: void
1.58      deraadt   666: makeargv(void)
1.1       deraadt   667: {
1.17      millert   668:        char *argp;
1.1       deraadt   669:
                    670:        stringbase = line;              /* scan from first of buffer */
                    671:        argbase = argbuf;               /* store from first of buffer */
                    672:        slrflag = 0;
1.17      millert   673:        marg_sl->sl_cur = 0;            /* reset to start of marg_sl */
1.1       deraadt   674:        for (margc = 0; ; margc++) {
1.17      millert   675:                argp = slurpstring();
                    676:                sl_add(marg_sl, argp);
                    677:                if (argp == NULL)
1.1       deraadt   678:                        break;
                    679:        }
1.17      millert   680:        if (cursor_pos == line) {
                    681:                cursor_argc = 0;
                    682:                cursor_argo = 0;
                    683:        } else if (cursor_pos != NULL) {
                    684:                cursor_argc = margc;
                    685:                cursor_argo = strlen(margv[margc-1]);
                    686:        }
                    687: }
1.1       deraadt   688:
1.17      millert   689: #define INC_CHKCURSOR(x)       { (x)++ ; \
                    690:                                if (x == cursor_pos) { \
                    691:                                        cursor_argc = margc; \
                    692:                                        cursor_argo = ap-argbase; \
                    693:                                        cursor_pos = NULL; \
                    694:                                } }
1.1       deraadt   695:
                    696: /*
                    697:  * Parse string into argbuf;
                    698:  * implemented with FSM to
                    699:  * handle quoting and strings
                    700:  */
                    701: char *
1.58      deraadt   702: slurpstring(void)
1.1       deraadt   703: {
                    704:        int got_one = 0;
                    705:        char *sb = stringbase;
                    706:        char *ap = argbase;
                    707:        char *tmp = argbase;            /* will return this if token found */
                    708:
                    709:        if (*sb == '!' || *sb == '$') { /* recognize ! as a token for shell */
                    710:                switch (slrflag) {      /* and $ as token for macro invoke */
                    711:                        case 0:
                    712:                                slrflag++;
1.17      millert   713:                                INC_CHKCURSOR(stringbase);
1.1       deraadt   714:                                return ((*sb == '!') ? "!" : "$");
                    715:                                /* NOTREACHED */
                    716:                        case 1:
                    717:                                slrflag++;
                    718:                                altarg = stringbase;
                    719:                                break;
                    720:                        default:
                    721:                                break;
                    722:                }
                    723:        }
                    724:
                    725: S0:
                    726:        switch (*sb) {
                    727:
                    728:        case '\0':
                    729:                goto OUT;
                    730:
                    731:        case ' ':
                    732:        case '\t':
1.17      millert   733:                INC_CHKCURSOR(sb);
                    734:                goto S0;
1.1       deraadt   735:
                    736:        default:
                    737:                switch (slrflag) {
                    738:                        case 0:
                    739:                                slrflag++;
                    740:                                break;
                    741:                        case 1:
                    742:                                slrflag++;
                    743:                                altarg = sb;
                    744:                                break;
                    745:                        default:
                    746:                                break;
                    747:                }
                    748:                goto S1;
                    749:        }
                    750:
                    751: S1:
                    752:        switch (*sb) {
                    753:
                    754:        case ' ':
                    755:        case '\t':
                    756:        case '\0':
                    757:                goto OUT;       /* end of token */
                    758:
                    759:        case '\\':
1.17      millert   760:                INC_CHKCURSOR(sb);
                    761:                goto S2;        /* slurp next character */
1.1       deraadt   762:
                    763:        case '"':
1.17      millert   764:                INC_CHKCURSOR(sb);
                    765:                goto S3;        /* slurp quoted string */
1.1       deraadt   766:
                    767:        default:
1.17      millert   768:                *ap = *sb;      /* add character to token */
                    769:                ap++;
                    770:                INC_CHKCURSOR(sb);
1.1       deraadt   771:                got_one = 1;
                    772:                goto S1;
                    773:        }
                    774:
                    775: S2:
                    776:        switch (*sb) {
                    777:
                    778:        case '\0':
                    779:                goto OUT;
                    780:
                    781:        default:
1.17      millert   782:                *ap = *sb;
                    783:                ap++;
                    784:                INC_CHKCURSOR(sb);
1.1       deraadt   785:                got_one = 1;
                    786:                goto S1;
                    787:        }
                    788:
                    789: S3:
                    790:        switch (*sb) {
                    791:
                    792:        case '\0':
                    793:                goto OUT;
                    794:
                    795:        case '"':
1.17      millert   796:                INC_CHKCURSOR(sb);
                    797:                goto S1;
1.1       deraadt   798:
                    799:        default:
1.17      millert   800:                *ap = *sb;
                    801:                ap++;
                    802:                INC_CHKCURSOR(sb);
1.1       deraadt   803:                got_one = 1;
                    804:                goto S3;
                    805:        }
                    806:
                    807: OUT:
                    808:        if (got_one)
                    809:                *ap++ = '\0';
                    810:        argbase = ap;                   /* update storage pointer */
                    811:        stringbase = sb;                /* update scan pointer */
                    812:        if (got_one) {
                    813:                return (tmp);
                    814:        }
                    815:        switch (slrflag) {
                    816:                case 0:
                    817:                        slrflag++;
                    818:                        break;
                    819:                case 1:
                    820:                        slrflag++;
                    821:                        altarg = (char *) 0;
                    822:                        break;
                    823:                default:
                    824:                        break;
                    825:        }
                    826:        return ((char *)0);
                    827: }
                    828:
                    829: /*
                    830:  * Help command.
                    831:  * Call each command handler with argc == 0 and argv[0] == name.
                    832:  */
                    833: void
1.58      deraadt   834: help(int argc, char *argv[])
1.1       deraadt   835: {
                    836:        struct cmd *c;
                    837:
                    838:        if (argc == 1) {
1.17      millert   839:                StringList *buf;
1.1       deraadt   840:
1.17      millert   841:                buf = sl_init();
1.30      deraadt   842:                fprintf(ttyout, "%sommands may be abbreviated.  Commands are:\n\n",
1.17      millert   843:                    proxy ? "Proxy c" : "C");
                    844:                for (c = cmdtab; c < &cmdtab[NCMDS]; c++)
                    845:                        if (c->c_name && (!proxy || c->c_proxy))
                    846:                                sl_add(buf, c->c_name);
                    847:                list_vertical(buf);
                    848:                sl_free(buf, 0);
1.1       deraadt   849:                return;
                    850:        }
1.17      millert   851:
1.18      millert   852: #define HELPINDENT ((int) sizeof("disconnect"))
1.17      millert   853:
1.1       deraadt   854:        while (--argc > 0) {
                    855:                char *arg;
1.17      millert   856:
1.1       deraadt   857:                arg = *++argv;
                    858:                c = getcmd(arg);
                    859:                if (c == (struct cmd *)-1)
1.30      deraadt   860:                        fprintf(ttyout, "?Ambiguous help command %s\n", arg);
1.1       deraadt   861:                else if (c == (struct cmd *)0)
1.30      deraadt   862:                        fprintf(ttyout, "?Invalid help command %s\n", arg);
1.1       deraadt   863:                else
1.30      deraadt   864:                        fprintf(ttyout, "%-*s\t%s\n", HELPINDENT,
1.1       deraadt   865:                                c->c_name, c->c_help);
                    866:        }
1.17      millert   867: }
1.77      martynas  868: #endif /* !SMALL */
1.17      millert   869:
                    870: void
1.58      deraadt   871: usage(void)
1.17      millert   872: {
1.92      lteo      873:        fprintf(stderr, "usage: "
1.70      martynas  874: #ifndef SMALL
1.92      lteo      875:            "%1$s [-46AadEegimnptVv] [-D title] [-k seconds] [-P port] "
1.87      deraadt   876:            "[-r seconds]\n"
                    877:            "           [-s srcaddr] [host [port]]\n"
1.92      lteo      878:            "       %1$s [-C] [-o output] [-s srcaddr]\n"
                    879:            "           ftp://[user:password@]host[:port]/file[/] ...\n"
                    880:            "       %1$s [-C] [-c cookie] [-o output] [-S ssl_options] "
1.84      haesbaer  881:            "[-s srcaddr]\n"
1.88      lteo      882:            "           [-U useragent] "
1.92      lteo      883:            "http[s]://[user:password@]host[:port]/file ...\n"
                    884:            "       %1$s [-C] [-o output] [-s srcaddr] file:file ...\n"
                    885:            "       %1$s [-C] [-o output] [-s srcaddr] host:/file[/] ...\n",
1.69      martynas  886: #else /* !SMALL */
1.92      lteo      887:            "%1$s [-o output] ftp://[user:password@]host[:port]/file[/] ...\n"
                    888:            "       %1$s [-o output] http://host[:port]/file ...\n"
                    889:            "       %1$s [-o output] file:file ...\n"
                    890:            "       %1$s [-o output] host:/file[/] ...\n",
1.69      martynas  891: #endif /* !SMALL */
1.92      lteo      892:            __progname);
1.17      millert   893:        exit(1);
1.1       deraadt   894: }