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

1.144   ! kn          1: /*     $OpenBSD: main.c,v 1.143 2023/11/30 14:52:00 kn Exp $   */
1.129     florian     2: /*     $NetBSD: main.c,v 1.24 1997/08/18 10:20:26 lukem Exp $  */
1.122     jasper      3:
1.1       deraadt     4: /*
1.129     florian     5:  * Copyright (C) 1997 and 1998 WIDE Project.
                      6:  * All rights reserved.
1.118     krw         7:  *
1.129     florian     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.
1.118     krw        19:  *
1.129     florian    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.
1.44      itojun     31:  */
                     32:
1.129     florian    33: /*
                     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.
                     45:  * 3. Neither the name of the University nor the names of its contributors
                     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:  */
1.1       deraadt    65: #include <sys/types.h>
                     66: #include <sys/socket.h>
                     67:
1.129     florian    68: #include <ctype.h>
1.1       deraadt    69: #include <err.h>
1.120     jsing      70: #include <fcntl.h>
1.129     florian    71: #include <netdb.h>
                     72: #include <pwd.h>
1.1       deraadt    73: #include <stdio.h>
1.129     florian    74: #include <errno.h>
1.18      millert    75: #include <stdlib.h>
1.1       deraadt    76: #include <string.h>
                     77: #include <unistd.h>
                     78:
1.129     florian    79: #include <tls.h>
1.91      jsing      80:
1.129     florian    81: #include "cmds.h"
                     82: #include "ftp_var.h"
1.132     jca        83:
                     84: int    trace;
                     85: int    hash;
                     86: int    mark;
                     87: int    sendport;
                     88: int    verbose;
                     89: int    connected;
                     90: int    fromatty;
                     91: int    interactive;
                     92: #ifndef SMALL
                     93: int    confirmrest;
                     94: int    debug;
                     95: int    bell;
                     96: char   *altarg;
                     97: #endif /* !SMALL */
                     98: int    doglob;
                     99: int    autologin;
                    100: int    proxy;
                    101: int    proxflag;
                    102: int    gatemode;
                    103: char   *gateserver;
                    104: int    sunique;
                    105: int    runique;
                    106: int    mcase;
                    107: int    ntflag;
                    108: int    mapflag;
                    109: int    preserve;
                    110: int    progress;
                    111: int    code;
                    112: int    crflag;
                    113: char   pasv[BUFSIZ];
                    114: int    passivemode;
                    115: int    activefallback;
                    116: char   ntin[17];
                    117: char   ntout[17];
                    118: char   mapin[PATH_MAX];
                    119: char   mapout[PATH_MAX];
                    120: char   typename[32];
                    121: int    type;
                    122: int    curtype;
                    123: char   structname[32];
                    124: int    stru;
                    125: char   formname[32];
                    126: int    form;
                    127: char   modename[32];
                    128: int    mode;
                    129: char   bytename[32];
                    130: int    bytesize;
                    131: int    anonftp;
                    132: int    dirchange;
                    133: unsigned int retry_connect;
                    134: int    ttywidth;
                    135: int    epsv4;
                    136: int    epsv4bad;
                    137:
                    138: #ifndef SMALL
                    139: int      editing;
                    140: EditLine *el;
                    141: History  *hist;
                    142: char    *cursor_pos;
                    143: size_t   cursor_argc;
                    144: size_t   cursor_argo;
                    145: int      resume;
                    146: char    *srcaddr;
1.136     robert    147: int      timestamp;
1.132     jca       148: #endif /* !SMALL */
                    149:
                    150: char    *cookiefile;
                    151:
                    152: off_t  bytes;
                    153: off_t  filesize;
                    154: char   *direction;
                    155:
                    156: char   *hostname;
                    157: int    unix_server;
                    158: int    unix_proxy;
                    159:
                    160: char *ftpport;
                    161: char *httpport;
                    162: #ifndef NOSSL
                    163: char *httpsport;
                    164: #endif /* !SMALL */
                    165: char *httpuseragent;
                    166: char *gateport;
                    167:
                    168: jmp_buf        toplevel;
                    169:
                    170: #ifndef SMALL
                    171: char   line[FTPBUFLEN];
                    172: char   *argbase;
                    173: char   *stringbase;
                    174: char   argbuf[FTPBUFLEN];
                    175: StringList *marg_sl;
                    176: int    margc;
                    177: int    options;
                    178: #endif /* !SMALL */
                    179:
                    180: int    cpend;
                    181: int    mflag;
                    182:
                    183: #ifndef SMALL
                    184: int macnum;
                    185: struct macel macros[16];
                    186: char macbuf[4096];
                    187: #endif /* !SMALL */
                    188:
                    189: FILE   *ttyout;
1.129     florian   190:
                    191: int connect_timeout;
                    192:
1.136     robert    193: #ifndef SMALL
                    194: /* enable using server timestamps by default */
                    195: int server_timestamps = 1;
                    196: #endif
                    197:
1.129     florian   198: #ifndef NOSSL
                    199: char * const ssl_verify_opts[] = {
                    200: #define SSL_CAFILE             0
                    201:        "cafile",
                    202: #define SSL_CAPATH             1
                    203:        "capath",
                    204: #define SSL_CIPHERS            2
                    205:        "ciphers",
                    206: #define SSL_DONTVERIFY         3
                    207:        "dont",
                    208: #define SSL_DOVERIFY           4
                    209:        "do",
                    210: #define SSL_VERIFYDEPTH                5
                    211:        "depth",
                    212: #define SSL_MUSTSTAPLE         6
                    213:        "muststaple",
                    214: #define SSL_NOVERIFYTIME       7
                    215:        "noverifytime",
                    216: #define SSL_SESSION            8
                    217:        "session",
1.133     tb        218: #define SSL_PROTOCOLS          9
                    219:        "protocols",
1.129     florian   220:        NULL
                    221: };
                    222:
                    223: struct tls_config *tls_config;
                    224: int tls_session_fd = -1;
                    225:
                    226: static void
                    227: process_ssl_options(char *cp)
                    228: {
                    229:        const char *errstr;
                    230:        char *str;
1.134     tb        231:        int depth;
1.133     tb        232:        uint32_t protocols;
1.129     florian   233:
                    234:        while (*cp) {
                    235:                switch (getsubopt(&cp, ssl_verify_opts, &str)) {
                    236:                case SSL_CAFILE:
                    237:                        if (str == NULL)
                    238:                                errx(1, "missing CA file");
                    239:                        if (tls_config_set_ca_file(tls_config, str) != 0)
                    240:                                errx(1, "tls ca file failed: %s",
                    241:                                    tls_config_error(tls_config));
                    242:                        break;
                    243:                case SSL_CAPATH:
                    244:                        if (str == NULL)
                    245:                                errx(1, "missing CA directory path");
                    246:                        if (tls_config_set_ca_path(tls_config, str) != 0)
                    247:                                errx(1, "tls ca path failed: %s",
                    248:                                    tls_config_error(tls_config));
                    249:                        break;
                    250:                case SSL_CIPHERS:
                    251:                        if (str == NULL)
                    252:                                errx(1, "missing cipher list");
                    253:                        if (tls_config_set_ciphers(tls_config, str) != 0)
                    254:                                errx(1, "tls ciphers failed: %s",
                    255:                                    tls_config_error(tls_config));
                    256:                        break;
                    257:                case SSL_DONTVERIFY:
                    258:                        tls_config_insecure_noverifycert(tls_config);
                    259:                        tls_config_insecure_noverifyname(tls_config);
                    260:                        break;
                    261:                case SSL_DOVERIFY:
                    262:                        tls_config_verify(tls_config);
                    263:                        break;
                    264:                case SSL_VERIFYDEPTH:
                    265:                        if (str == NULL)
                    266:                                errx(1, "missing depth");
                    267:                        depth = strtonum(str, 0, INT_MAX, &errstr);
                    268:                        if (errstr)
                    269:                                errx(1, "certificate validation depth is %s",
                    270:                                    errstr);
1.134     tb        271:                        tls_config_set_verify_depth(tls_config, depth);
1.129     florian   272:                        break;
                    273:                case SSL_MUSTSTAPLE:
                    274:                        tls_config_ocsp_require_stapling(tls_config);
                    275:                        break;
                    276:                case SSL_NOVERIFYTIME:
                    277:                        tls_config_insecure_noverifytime(tls_config);
                    278:                        break;
                    279:                case SSL_SESSION:
                    280:                        if (str == NULL)
                    281:                                errx(1, "missing session file");
                    282:                        if ((tls_session_fd = open(str, O_RDWR|O_CREAT,
                    283:                            0600)) == -1)
                    284:                                err(1, "failed to open or create session file "
                    285:                                    "'%s'", str);
                    286:                        if (tls_config_set_session_fd(tls_config,
                    287:                            tls_session_fd) == -1)
                    288:                                errx(1, "failed to set session: %s",
                    289:                                    tls_config_error(tls_config));
1.133     tb        290:                        break;
                    291:                case SSL_PROTOCOLS:
                    292:                        if (str == NULL)
                    293:                                errx(1, "missing protocol name");
                    294:                        if (tls_config_parse_protocols(&protocols, str) != 0)
                    295:                                errx(1, "failed to parse TLS protocols");
                    296:                        if (tls_config_set_protocols(tls_config, protocols) != 0)
1.135     tb        297:                                errx(1, "failed to set TLS protocols: %s",
                    298:                                    tls_config_error(tls_config));
1.129     florian   299:                        break;
                    300:                default:
                    301:                        errx(1, "unknown -S suboption `%s'",
                    302:                            suboptarg ? suboptarg : "");
                    303:                        /* NOTREACHED */
                    304:                }
                    305:        }
                    306: }
                    307: #endif /* !NOSSL */
                    308:
                    309: int family = PF_UNSPEC;
                    310: int pipeout;
1.49      deraadt   311:
1.1       deraadt   312: int
1.129     florian   313: main(volatile int argc, char *argv[])
1.1       deraadt   314: {
1.129     florian   315:        int ch, rval;
                    316: #ifndef SMALL
                    317:        int top;
                    318: #endif
                    319:        struct passwd *pw = NULL;
                    320:        char *cp, homedir[PATH_MAX];
                    321:        char *outfile = NULL;
                    322:        const char *errstr;
                    323:        int dumb_terminal = 0;
                    324:
                    325:        ftpport = "ftp";
                    326:        httpport = "http";
                    327: #ifndef NOSSL
                    328:        httpsport = "https";
                    329: #endif /* !NOSSL */
                    330:        gateport = getenv("FTPSERVERPORT");
                    331:        if (gateport == NULL || *gateport == '\0')
                    332:                gateport = "ftpgate";
                    333:        doglob = 1;
                    334:        interactive = 1;
                    335:        autologin = 1;
                    336:        passivemode = 1;
                    337:        activefallback = 1;
                    338:        preserve = 1;
                    339:        verbose = 0;
                    340:        progress = 0;
                    341:        gatemode = 0;
                    342: #ifndef NOSSL
                    343:        cookiefile = NULL;
                    344: #endif /* NOSSL */
                    345: #ifndef SMALL
                    346:        editing = 0;
                    347:        el = NULL;
                    348:        hist = NULL;
                    349:        resume = 0;
1.136     robert    350:        timestamp = 0;
1.129     florian   351:        srcaddr = NULL;
                    352:        marg_sl = sl_init();
                    353: #endif /* !SMALL */
                    354:        mark = HASHBYTES;
                    355:        epsv4 = 1;
                    356:        epsv4bad = 0;
                    357:
                    358:        /* Set default operation mode based on FTPMODE environment variable */
                    359:        if ((cp = getenv("FTPMODE")) != NULL && *cp != '\0') {
                    360:                if (strcmp(cp, "passive") == 0) {
                    361:                        passivemode = 1;
                    362:                        activefallback = 0;
                    363:                } else if (strcmp(cp, "active") == 0) {
                    364:                        passivemode = 0;
                    365:                        activefallback = 0;
                    366:                } else if (strcmp(cp, "gate") == 0) {
                    367:                        gatemode = 1;
                    368:                } else if (strcmp(cp, "auto") == 0) {
                    369:                        passivemode = 1;
                    370:                        activefallback = 1;
                    371:                } else
                    372:                        warnx("unknown FTPMODE: %s.  Using defaults", cp);
                    373:        }
                    374:
                    375:        if (strcmp(__progname, "gate-ftp") == 0)
                    376:                gatemode = 1;
                    377:        gateserver = getenv("FTPSERVER");
                    378:        if (gateserver == NULL)
                    379:                gateserver = "";
                    380:        if (gatemode) {
                    381:                if (*gateserver == '\0') {
                    382:                        warnx(
                    383: "Neither $FTPSERVER nor $GATE_SERVER is defined; disabling gate-ftp");
                    384:                        gatemode = 0;
                    385:                }
                    386:        }
                    387:
                    388:        cp = getenv("TERM");
                    389:        dumb_terminal = (cp == NULL || *cp == '\0' || !strcmp(cp, "dumb") ||
                    390:            !strcmp(cp, "emacs") || !strcmp(cp, "su"));
                    391:        fromatty = isatty(fileno(stdin));
                    392:        if (fromatty) {
                    393:                verbose = 1;            /* verbose if from a tty */
                    394: #ifndef SMALL
                    395:                if (!dumb_terminal)
                    396:                        editing = 1;    /* editing mode on if tty is usable */
                    397: #endif /* !SMALL */
                    398:        }
                    399:
                    400:        ttyout = stdout;
                    401:        if (isatty(fileno(ttyout)) && !dumb_terminal && foregroundproc())
                    402:                progress = 1;           /* progress bar on if tty is usable */
                    403:
                    404: #ifndef NOSSL
                    405:        cookiefile = getenv("http_cookies");
                    406:        if (tls_config == NULL) {
                    407:                tls_config = tls_config_new();
                    408:                if (tls_config == NULL)
                    409:                        errx(1, "tls config failed");
                    410:                if (tls_config_set_protocols(tls_config,
                    411:                    TLS_PROTOCOLS_ALL) != 0)
                    412:                        errx(1, "tls set protocols failed: %s",
                    413:                            tls_config_error(tls_config));
                    414:                if (tls_config_set_ciphers(tls_config, "legacy") != 0)
                    415:                        errx(1, "tls set ciphers failed: %s",
                    416:                            tls_config_error(tls_config));
                    417:        }
                    418: #endif /* !NOSSL */
                    419:
                    420:        httpuseragent = NULL;
                    421:
1.86      jca       422:        while ((ch = getopt(argc, argv,
1.136     robert    423:                    "46AaCc:dD:EeN:gik:Mmno:pP:r:S:s:TtU:uvVw:")) != -1) {
1.1       deraadt   424:                switch (ch) {
1.49      deraadt   425:                case '4':
1.129     florian   426:                        family = PF_INET;
1.49      deraadt   427:                        break;
                    428:                case '6':
1.129     florian   429:                        family = PF_INET6;
1.49      deraadt   430:                        break;
1.37      millert   431:                case 'A':
1.129     florian   432:                        activefallback = 0;
                    433:                        passivemode = 0;
                    434:                        break;
                    435:
1.130     deraadt   436:                case 'N':
                    437:                        setprogname(optarg);
                    438:                        break;
1.129     florian   439:                case 'a':
                    440:                        anonftp = 1;
1.37      millert   441:                        break;
1.129     florian   442:
1.67      martynas  443:                case 'C':
1.129     florian   444: #ifndef SMALL
1.67      martynas  445:                        resume = 1;
1.129     florian   446: #endif /* !SMALL */
1.63      pyr       447:                        break;
1.129     florian   448:
                    449:                case 'c':
                    450: #ifndef SMALL
                    451:                        cookiefile = optarg;
                    452: #endif /* !SMALL */
                    453:                        break;
                    454:
1.87      deraadt   455:                case 'D':
1.129     florian   456:                        action = optarg;
                    457:                        break;
                    458:                case 'd':
                    459: #ifndef SMALL
                    460:                        options |= SO_DEBUG;
                    461:                        debug++;
                    462: #endif /* !SMALL */
                    463:                        break;
                    464:
                    465:                case 'E':
                    466:                        epsv4 = 0;
                    467:                        break;
                    468:
                    469:                case 'e':
                    470: #ifndef SMALL
                    471:                        editing = 0;
                    472: #endif /* !SMALL */
                    473:                        break;
                    474:
                    475:                case 'g':
                    476:                        doglob = 0;
                    477:                        break;
                    478:
                    479:                case 'i':
                    480:                        interactive = 0;
1.87      deraadt   481:                        break;
1.129     florian   482:
                    483:                case 'k':
                    484:                        keep_alive_timeout = strtonum(optarg, 0, INT_MAX,
                    485:                            &errstr);
                    486:                        if (errstr != NULL) {
                    487:                                warnx("keep alive amount is %s: %s", errstr,
                    488:                                        optarg);
                    489:                                usage();
                    490:                        }
1.1       deraadt   491:                        break;
1.121     kmos      492:                case 'M':
1.129     florian   493:                        progress = 0;
1.59      fgsch     494:                        break;
1.121     kmos      495:                case 'm':
1.129     florian   496:                        progress = -1;
1.21      kstailey  497:                        break;
1.129     florian   498:
                    499:                case 'n':
                    500:                        autologin = 0;
                    501:                        break;
                    502:
                    503:                case 'o':
                    504:                        outfile = optarg;
1.144   ! kn        505:                        pipeout = strcmp(outfile, "-") == 0;
        !           506:                        ttyout = pipeout ? stderr : stdout;
1.129     florian   507:                        break;
                    508:
                    509:                case 'p':
                    510:                        passivemode = 1;
                    511:                        activefallback = 0;
                    512:                        break;
                    513:
                    514:                case 'P':
                    515:                        ftpport = optarg;
                    516:                        break;
                    517:
                    518:                case 'r':
                    519:                        retry_connect = strtonum(optarg, 0, INT_MAX, &errstr);
                    520:                        if (errstr != NULL) {
                    521:                                warnx("retry amount is %s: %s", errstr,
                    522:                                        optarg);
                    523:                                usage();
                    524:                        }
                    525:                        break;
                    526:
1.121     kmos      527:                case 'S':
1.129     florian   528: #ifndef NOSSL
                    529:                        process_ssl_options(optarg);
                    530: #endif /* !NOSSL */
                    531:                        break;
                    532:
                    533:                case 's':
                    534: #ifndef SMALL
                    535:                        srcaddr = optarg;
                    536: #endif /* !SMALL */
                    537:                        break;
                    538:
1.136     robert    539: #ifndef SMALL
                    540:                case 'T':
                    541:                        timestamp = 1;
                    542:                        break;
                    543: #endif /* !SMALL */
1.129     florian   544:                case 't':
                    545:                        trace = 1;
1.1       deraadt   546:                        break;
1.129     florian   547:
                    548: #ifndef SMALL
1.121     kmos      549:                case 'U':
1.129     florian   550:                        free (httpuseragent);
                    551:                        if (strcspn(optarg, "\r\n") != strlen(optarg))
                    552:                                errx(1, "Invalid User-Agent: %s.", optarg);
                    553:                        if (asprintf(&httpuseragent, "User-Agent: %s",
                    554:                            optarg) == -1)
                    555:                                errx(1, "Can't allocate memory for HTTP(S) "
                    556:                                    "User-Agent");
1.136     robert    557:                        break;
                    558:                case 'u':
                    559:                        server_timestamps = 0;
1.129     florian   560:                        break;
                    561: #endif /* !SMALL */
                    562:
                    563:                case 'v':
                    564:                        verbose = 1;
1.41      millert   565:                        break;
1.129     florian   566:
1.121     kmos      567:                case 'V':
                    568:                        verbose = 0;
1.65      espie     569:                        break;
1.129     florian   570:
1.121     kmos      571:                case 'w':
1.129     florian   572:                        connect_timeout = strtonum(optarg, 0, 200, &errstr);
                    573:                        if (errstr)
                    574:                                errx(1, "-w: %s", errstr);
1.116     deraadt   575:                        break;
1.1       deraadt   576:                default:
1.17      millert   577:                        usage();
1.1       deraadt   578:                }
                    579:        }
                    580:        argc -= optind;
                    581:        argv += optind;
1.63      pyr       582:
1.129     florian   583: #ifndef NOSSL
                    584:        cookie_load();
                    585: #endif /* !NOSSL */
                    586:        if (httpuseragent == NULL)
                    587:                httpuseragent = HTTP_USER_AGENT;
                    588:
                    589:        cpend = 0;      /* no pending replies */
                    590:        proxy = 0;      /* proxy not active */
                    591:        crflag = 1;     /* strip c.r. on ascii gets */
                    592:        sendport = -1;  /* not using ports */
                    593:        /*
                    594:         * Set up the home directory in case we're globbing.
                    595:         */
                    596:        cp = getlogin();
                    597:        if (cp != NULL) {
                    598:                pw = getpwnam(cp);
                    599:        }
                    600:        if (pw == NULL)
                    601:                pw = getpwuid(getuid());
                    602:        if (pw != NULL) {
                    603:                (void)strlcpy(homedir, pw->pw_dir, sizeof homedir);
                    604:                home = homedir;
                    605:        }
                    606:
                    607:        setttywidth(0);
                    608:        (void)signal(SIGWINCH, setttywidth);
1.9       michaels  609:
1.129     florian   610:        if (argc > 0) {
                    611:                if (isurl(argv[0])) {
                    612:                        if (pipeout) {
1.143     kn        613:                                if (pledge("stdio rpath dns tty inet",
1.129     florian   614:                                    NULL) == -1)
                    615:                                        err(1, "pledge");
                    616:                        } else {
1.103     doug      617: #ifndef SMALL
1.139     kn        618:                            if (!resume) {
1.129     florian   619:                                if (pledge("stdio rpath wpath cpath dns tty inet proc exec fattr",
                    620:                                    NULL) == -1)
                    621:                                        err(1, "pledge");
1.139     kn        622:                            } else
                    623: #endif /* !SMALL */
1.129     florian   624:                                if (pledge("stdio rpath wpath cpath dns tty inet fattr",
                    625:                                    NULL) == -1)
                    626:                                        err(1, "pledge");
                    627:                        }
1.103     doug      628:
1.129     florian   629:                        rval = auto_fetch(argc, argv, outfile);
1.140     kn        630:                        if (rval >= 0 || pipeout) /* -1 == connected and cd-ed */
1.129     florian   631:                                exit(rval);
                    632:                } else {
                    633: #ifndef SMALL
                    634:                        char *xargv[5];
1.3       deraadt   635:
1.129     florian   636:                        if (setjmp(toplevel))
                    637:                                exit(0);
                    638:                        (void)signal(SIGINT, (sig_t)intr);
                    639:                        (void)signal(SIGPIPE, (sig_t)lostpeer);
                    640:                        xargv[0] = __progname;
                    641:                        xargv[1] = argv[0];
                    642:                        xargv[2] = argv[1];
                    643:                        xargv[3] = argv[2];
                    644:                        xargv[4] = NULL;
                    645:                        do {
                    646:                                setpeer(argc+1, xargv);
                    647:                                if (!retry_connect)
                    648:                                        break;
                    649:                                if (!connected) {
                    650:                                        macnum = 0;
                    651:                                        fputs("Retrying...\n", ttyout);
                    652:                                        sleep(retry_connect);
                    653:                                }
                    654:                        } while (!connected);
                    655:                        retry_connect = 0; /* connected, stop hiding msgs */
                    656: #endif /* !SMALL */
1.3       deraadt   657:                }
1.1       deraadt   658:        }
1.129     florian   659: #ifndef SMALL
                    660:        controlediting();
                    661:        top = setjmp(toplevel) == 0;
                    662:        if (top) {
                    663:                (void)signal(SIGINT, (sig_t)intr);
                    664:                (void)signal(SIGPIPE, (sig_t)lostpeer);
                    665:        }
                    666:        for (;;) {
                    667:                cmdscanner(top);
                    668:                top = 1;
                    669:        }
                    670: #else /* !SMALL */
                    671:        usage();
                    672: #endif /* !SMALL */
                    673: }
                    674:
                    675: void
                    676: intr(void)
                    677: {
                    678:        int save_errno = errno;
                    679:
                    680:        write(fileno(ttyout), "\n\r", 2);
                    681:        alarmtimer(0);
1.121     kmos      682:
1.129     florian   683:        errno = save_errno;
                    684:        longjmp(toplevel, 1);
1.1       deraadt   685: }
                    686:
1.129     florian   687: void
                    688: lostpeer(void)
1.1       deraadt   689: {
1.129     florian   690:        int save_errno = errno;
1.121     kmos      691:
1.129     florian   692:        alarmtimer(0);
                    693:        if (connected) {
                    694:                if (cout != NULL) {
                    695:                        (void)shutdown(fileno(cout), SHUT_RDWR);
                    696:                        (void)fclose(cout);
                    697:                        cout = NULL;
                    698:                }
                    699:                if (data >= 0) {
                    700:                        (void)shutdown(data, SHUT_RDWR);
                    701:                        (void)close(data);
                    702:                        data = -1;
                    703:                }
                    704:                connected = 0;
                    705:        }
                    706:        pswitch(1);
                    707:        if (connected) {
                    708:                if (cout != NULL) {
                    709:                        (void)shutdown(fileno(cout), SHUT_RDWR);
                    710:                        (void)fclose(cout);
                    711:                        cout = NULL;
                    712:                }
                    713:                connected = 0;
1.121     kmos      714:        }
1.129     florian   715:        proxflag = 0;
                    716:        pswitch(0);
                    717:        errno = save_errno;
                    718: }
1.90      deraadt   719:
1.129     florian   720: #ifndef SMALL
                    721: /*
                    722:  * Generate a prompt
                    723:  */
                    724: char *
                    725: prompt(void)
                    726: {
                    727:        return ("ftp> ");
1.1       deraadt   728: }
                    729:
1.129     florian   730: /*
                    731:  * Command parser.
                    732:  */
                    733: void
                    734: cmdscanner(int top)
1.1       deraadt   735: {
1.129     florian   736:        struct cmd *c;
                    737:        int num;
                    738:        HistEvent hev;
1.1       deraadt   739:
1.129     florian   740:        if (!top && !editing)
                    741:                (void)putc('\n', ttyout);
                    742:        for (;;) {
                    743:                if (!editing) {
                    744:                        if (fromatty) {
                    745:                                fputs(prompt(), ttyout);
                    746:                                (void)fflush(ttyout);
                    747:                        }
                    748:                        if (fgets(line, sizeof(line), stdin) == NULL)
                    749:                                quit(0, 0);
                    750:                        num = strlen(line);
                    751:                        if (num == 0)
                    752:                                break;
                    753:                        if (line[--num] == '\n') {
                    754:                                if (num == 0)
                    755:                                        break;
                    756:                                line[num] = '\0';
                    757:                        } else if (num == sizeof(line) - 2) {
                    758:                                fputs("sorry, input line too long.\n", ttyout);
                    759:                                while ((num = getchar()) != '\n' && num != EOF)
                    760:                                        /* void */;
                    761:                                break;
                    762:                        } /* else it was a line without a newline */
                    763:                } else {
                    764:                        const char *buf;
                    765:                        cursor_pos = NULL;
                    766:
                    767:                        if ((buf = el_gets(el, &num)) == NULL || num == 0) {
                    768:                                putc('\n', ttyout);
                    769:                                fflush(ttyout);
                    770:                                quit(0, 0);
                    771:                        }
                    772:                        if (buf[--num] == '\n') {
                    773:                                if (num == 0)
                    774:                                        break;
                    775:                        }
                    776:                        if (num >= sizeof(line)) {
                    777:                                fputs("sorry, input line too long.\n", ttyout);
                    778:                                break;
                    779:                        }
                    780:                        memcpy(line, buf, (size_t)num);
                    781:                        line[num] = '\0';
                    782:                        history(hist, &hev, H_ENTER, buf);
                    783:                }
                    784:
                    785:                makeargv();
                    786:                if (margc == 0)
                    787:                        continue;
                    788:                c = getcmd(margv[0]);
                    789:                if (c == (struct cmd *)-1) {
                    790:                        fputs("?Ambiguous command.\n", ttyout);
                    791:                        continue;
                    792:                }
                    793:                if (c == 0) {
                    794:                        /*
                    795:                         * Give editline(3) a shot at unknown commands.
                    796:                         * XXX - bogus commands with a colon in
                    797:                         *       them will not elicit an error.
                    798:                         */
                    799:                        if (editing &&
                    800:                            el_parse(el, margc, (const char **)margv) != 0)
                    801:                                fputs("?Invalid command.\n", ttyout);
                    802:                        continue;
                    803:                }
                    804:                if (c->c_conn && !connected) {
                    805:                        fputs("Not connected.\n", ttyout);
                    806:                        continue;
                    807:                }
                    808:                confirmrest = 0;
                    809:                (*c->c_handler)(margc, margv);
                    810:                if (bell && c->c_bell)
                    811:                        (void)putc('\007', ttyout);
                    812:                if (c->c_handler != help)
                    813:                        break;
                    814:        }
                    815:        (void)signal(SIGINT, (sig_t)intr);
                    816:        (void)signal(SIGPIPE, (sig_t)lostpeer);
                    817: }
1.1       deraadt   818:
1.129     florian   819: struct cmd *
                    820: getcmd(const char *name)
                    821: {
                    822:        const char *p, *q;
                    823:        struct cmd *c, *found;
                    824:        int nmatches, longest;
                    825:
                    826:        if (name == NULL)
                    827:                return (0);
                    828:
                    829:        longest = 0;
                    830:        nmatches = 0;
                    831:        found = 0;
                    832:        for (c = cmdtab; (p = c->c_name) != NULL; c++) {
                    833:                for (q = name; *q == *p++; q++)
                    834:                        if (*q == 0)            /* exact match? */
                    835:                                return (c);
                    836:                if (!*q) {                      /* the name was a prefix */
                    837:                        if (q - name > longest) {
                    838:                                longest = q - name;
                    839:                                nmatches = 1;
                    840:                                found = c;
                    841:                        } else if (q - name == longest)
                    842:                                nmatches++;
                    843:                }
                    844:        }
                    845:        if (nmatches > 1)
                    846:                return ((struct cmd *)-1);
                    847:        return (found);
1.1       deraadt   848: }
                    849:
1.129     florian   850: /*
                    851:  * Slice a string up into argc/argv.
                    852:  */
                    853:
                    854: int slrflag;
                    855:
                    856: void
                    857: makeargv(void)
1.1       deraadt   858: {
1.129     florian   859:        char *argp;
1.1       deraadt   860:
1.129     florian   861:        stringbase = line;              /* scan from first of buffer */
                    862:        argbase = argbuf;               /* store from first of buffer */
                    863:        slrflag = 0;
                    864:        marg_sl->sl_cur = 0;            /* reset to start of marg_sl */
                    865:        for (margc = 0; ; margc++) {
                    866:                argp = slurpstring();
                    867:                sl_add(marg_sl, argp);
                    868:                if (argp == NULL)
1.121     kmos      869:                        break;
1.129     florian   870:        }
                    871:        if (cursor_pos == line) {
                    872:                cursor_argc = 0;
                    873:                cursor_argo = 0;
                    874:        } else if (cursor_pos != NULL) {
                    875:                cursor_argc = margc;
                    876:                cursor_argo = strlen(margv[margc-1]);
                    877:        }
                    878: }
1.121     kmos      879:
1.129     florian   880: #define INC_CHKCURSOR(x)       { (x)++ ; \
                    881:                                if (x == cursor_pos) { \
                    882:                                        cursor_argc = margc; \
                    883:                                        cursor_argo = ap-argbase; \
                    884:                                        cursor_pos = NULL; \
                    885:                                } }
1.121     kmos      886:
1.129     florian   887: /*
                    888:  * Parse string into argbuf;
                    889:  * implemented with FSM to
                    890:  * handle quoting and strings
                    891:  */
                    892: char *
                    893: slurpstring(void)
                    894: {
                    895:        int got_one = 0;
                    896:        char *sb = stringbase;
                    897:        char *ap = argbase;
                    898:        char *tmp = argbase;            /* will return this if token found */
                    899:
                    900:        if (*sb == '!' || *sb == '$') { /* recognize ! as a token for shell */
                    901:                switch (slrflag) {      /* and $ as token for macro invoke */
                    902:                        case 0:
                    903:                                slrflag++;
                    904:                                INC_CHKCURSOR(stringbase);
                    905:                                return ((*sb == '!') ? "!" : "$");
                    906:                                /* NOTREACHED */
                    907:                        case 1:
                    908:                                slrflag++;
                    909:                                altarg = stringbase;
                    910:                                break;
                    911:                        default:
                    912:                                break;
1.17      millert   913:                }
1.1       deraadt   914:        }
1.121     kmos      915:
1.129     florian   916: S0:
                    917:        switch (*sb) {
1.121     kmos      918:
1.129     florian   919:        case '\0':
                    920:                goto OUT;
1.121     kmos      921:
1.129     florian   922:        case ' ':
                    923:        case '\t':
                    924:                INC_CHKCURSOR(sb);
                    925:                goto S0;
1.1       deraadt   926:
1.129     florian   927:        default:
                    928:                switch (slrflag) {
                    929:                        case 0:
                    930:                                slrflag++;
                    931:                                break;
                    932:                        case 1:
                    933:                                slrflag++;
                    934:                                altarg = sb;
                    935:                                break;
                    936:                        default:
                    937:                                break;
                    938:                }
                    939:                goto S1;
                    940:        }
1.1       deraadt   941:
1.129     florian   942: S1:
                    943:        switch (*sb) {
1.1       deraadt   944:
1.129     florian   945:        case ' ':
                    946:        case '\t':
                    947:        case '\0':
                    948:                goto OUT;       /* end of token */
                    949:
                    950:        case '\\':
                    951:                INC_CHKCURSOR(sb);
                    952:                goto S2;        /* slurp next character */
                    953:
                    954:        case '"':
                    955:                INC_CHKCURSOR(sb);
                    956:                goto S3;        /* slurp quoted string */
1.1       deraadt   957:
1.129     florian   958:        default:
                    959:                *ap = *sb;      /* add character to token */
                    960:                ap++;
                    961:                INC_CHKCURSOR(sb);
                    962:                got_one = 1;
                    963:                goto S1;
1.1       deraadt   964:        }
                    965:
1.129     florian   966: S2:
                    967:        switch (*sb) {
1.1       deraadt   968:
1.129     florian   969:        case '\0':
                    970:                goto OUT;
1.1       deraadt   971:
                    972:        default:
1.129     florian   973:                *ap = *sb;
                    974:                ap++;
                    975:                INC_CHKCURSOR(sb);
                    976:                got_one = 1;
                    977:                goto S1;
1.1       deraadt   978:        }
                    979:
1.129     florian   980: S3:
                    981:        switch (*sb) {
                    982:
                    983:        case '\0':
                    984:                goto OUT;
                    985:
                    986:        case '"':
                    987:                INC_CHKCURSOR(sb);
                    988:                goto S1;
                    989:
                    990:        default:
                    991:                *ap = *sb;
                    992:                ap++;
                    993:                INC_CHKCURSOR(sb);
                    994:                got_one = 1;
                    995:                goto S3;
                    996:        }
1.1       deraadt   997:
1.129     florian   998: OUT:
                    999:        if (got_one)
                   1000:                *ap++ = '\0';
                   1001:        argbase = ap;                   /* update storage pointer */
                   1002:        stringbase = sb;                /* update scan pointer */
                   1003:        if (got_one) {
                   1004:                return (tmp);
                   1005:        }
                   1006:        switch (slrflag) {
                   1007:                case 0:
                   1008:                        slrflag++;
                   1009:                        break;
                   1010:                case 1:
                   1011:                        slrflag++;
                   1012:                        altarg = (char *) 0;
                   1013:                        break;
                   1014:                default:
                   1015:                        break;
                   1016:        }
                   1017:        return (NULL);
1.1       deraadt  1018: }
                   1019:
1.129     florian  1020: /*
                   1021:  * Help command.
                   1022:  * Call each command handler with argc == 0 and argv[0] == name.
                   1023:  */
                   1024: void
                   1025: help(int argc, char *argv[])
1.1       deraadt  1026: {
1.129     florian  1027:        struct cmd *c;
1.1       deraadt  1028:
1.129     florian  1029:        if (argc == 1) {
                   1030:                StringList *buf;
1.1       deraadt  1031:
1.129     florian  1032:                buf = sl_init();
                   1033:                fprintf(ttyout, "%sommands may be abbreviated.  Commands are:\n\n",
                   1034:                    proxy ? "Proxy c" : "C");
                   1035:                for (c = cmdtab; c < &cmdtab[NCMDS]; c++)
                   1036:                        if (c->c_name && (!proxy || c->c_proxy))
                   1037:                                sl_add(buf, c->c_name);
                   1038:                list_vertical(buf);
                   1039:                sl_free(buf, 0);
                   1040:                return;
                   1041:        }
1.17      millert  1042:
1.129     florian  1043: #define HELPINDENT ((int) sizeof("disconnect"))
1.17      millert  1044:
1.129     florian  1045:        while (--argc > 0) {
                   1046:                char *arg;
1.17      millert  1047:
1.129     florian  1048:                arg = *++argv;
                   1049:                c = getcmd(arg);
                   1050:                if (c == (struct cmd *)-1)
                   1051:                        fprintf(ttyout, "?Ambiguous help command %s\n", arg);
                   1052:                else if (c == NULL)
                   1053:                        fprintf(ttyout, "?Invalid help command %s\n", arg);
                   1054:                else
                   1055:                        fprintf(ttyout, "%-*s\t%s\n", HELPINDENT,
                   1056:                                c->c_name, c->c_help);
                   1057:        }
1.17      millert  1058: }
1.129     florian  1059: #endif /* !SMALL */
1.17      millert  1060:
1.129     florian  1061: __dead void
1.58      deraadt  1062: usage(void)
1.17      millert  1063: {
1.129     florian  1064:        fprintf(stderr, "usage: "
                   1065: #ifndef SMALL
                   1066:            "ftp [-46AadEegiMmnptVv] [-D title] [-k seconds] [-P port] "
                   1067:            "[-r seconds]\n"
1.131     deraadt  1068:            "           [-s sourceaddr] [host [port]]\n"
                   1069:            "       ftp [-C] [-N name] [-o output] [-s sourceaddr]\n"
1.129     florian  1070:            "           ftp://[user:password@]host[:port]/file[/] ...\n"
1.137     jmc      1071:            "       ftp [-CTu] [-c cookie] [-N name] [-o output] [-S ssl_options] "
1.131     deraadt  1072:            "[-s sourceaddr]\n"
1.129     florian  1073:            "           [-U useragent] [-w seconds] "
                   1074:            "http[s]://[user:password@]host[:port]/file ...\n"
1.131     deraadt  1075:            "       ftp [-C] [-N name] [-o output] [-s sourceaddr] file:file ...\n"
                   1076:            "       ftp [-C] [-N name] [-o output] [-s sourceaddr] host:/file[/] ...\n"
1.129     florian  1077: #else /* !SMALL */
1.130     deraadt  1078:            "ftp [-N name] [-o output] "
1.129     florian  1079:            "ftp://[user:password@]host[:port]/file[/] ...\n"
                   1080: #ifndef NOSSL
1.130     deraadt  1081:            "       ftp [-N name] [-o output] [-S ssl_options] [-w seconds] "
1.129     florian  1082:            "http[s]://[user:password@]host[:port]/file ...\n"
                   1083: #else
1.130     deraadt  1084:            "       ftp [-N name] [-o output] [-w seconds] http://host[:port]/file ...\n"
1.129     florian  1085: #endif /* NOSSL */
1.130     deraadt  1086:            "       ftp [-N name] [-o output] file:file ...\n"
                   1087:            "       ftp [-N name] [-o output] host:/file[/] ...\n"
1.129     florian  1088: #endif /* !SMALL */
                   1089:            );
1.17      millert  1090:        exit(1);
1.1       deraadt  1091: }