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

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