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

Annotation of src/usr.bin/telnet/commands.c, Revision 1.54

1.54    ! phessler    1: /*     $OpenBSD: commands.c,v 1.53 2011/06/21 17:31:07 mikeb Exp $     */
1.4       deraadt     2: /*     $NetBSD: commands.c,v 1.14 1996/03/24 22:03:48 jtk Exp $        */
1.3       niklas      3:
1.1       deraadt     4: /*
                      5:  * Copyright (c) 1988, 1990, 1993
                      6:  *     The Regents of the University of California.  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.
1.45      millert    16:  * 3. Neither the name of the University nor the names of its contributors
1.1       deraadt    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 REGENTS 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 REGENTS 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:
1.10      art        33: #include "telnet_locl.h"
1.15      art        34: #include <err.h>
1.1       deraadt    35:
                     36: #if    defined(IPPROTO_IP) && defined(IP_TOS)
                     37: int tos = -1;
                     38: #endif /* defined(IPPROTO_IP) && defined(IP_TOS) */
                     39:
                     40: char   *hostname;
                     41:
1.10      art        42: typedef int (*intrtn_t)(int, char**);
1.40      millert    43: static int call(intrtn_t, ...);
1.1       deraadt    44:
                     45: typedef struct {
                     46:        char    *name;          /* command name */
                     47:        char    *help;          /* help string (NULL for no help) */
                     48:        int     (*handler)();   /* routine which executes command */
                     49:        int     needconnect;    /* Do we need to be connected to execute? */
                     50: } Command;
                     51:
                     52: static char line[256];
                     53: static char saveline[256];
                     54: static int margc;
                     55: static char *margv[20];
                     56:
1.6       deraadt    57: #if    defined(SKEY)
                     58: #include <sys/wait.h>
                     59: #define PATH_SKEY      "/usr/bin/skey"
                     60:     int
                     61: skey_calc(argc, argv)
                     62:        int argc;
                     63:        char **argv;
                     64: {
                     65:        int status;
                     66:
                     67:        if(argc != 3) {
1.21      deraadt    68:                printf("usage: %s sequence challenge\n", argv[0]);
1.15      art        69:                return 0;
1.6       deraadt    70:        }
                     71:
                     72:        switch(fork()) {
                     73:        case 0:
                     74:                execv(PATH_SKEY, argv);
                     75:                exit (1);
                     76:        case -1:
1.15      art        77:                err(1, "fork");
1.6       deraadt    78:                break;
                     79:        default:
                     80:                (void) wait(&status);
                     81:                if (WIFEXITED(status))
                     82:                        return (WEXITSTATUS(status));
                     83:                return (0);
                     84:        }
                     85: }
                     86: #endif
                     87:
                     88:
                     89:
1.1       deraadt    90:     static void
                     91: makeargv()
                     92: {
1.39      mpech      93:     char *cp, *cp2, c;
                     94:     char **argp = margv;
1.1       deraadt    95:
                     96:     margc = 0;
                     97:     cp = line;
                     98:     if (*cp == '!') {          /* Special case shell escape */
1.43      hin        99:        strlcpy(saveline, line, sizeof(saveline)); /* save for shell command */
1.1       deraadt   100:        *argp++ = "!";          /* No room in string to get this */
                    101:        margc++;
                    102:        cp++;
                    103:     }
1.10      art       104:     while ((c = *cp)) {
1.39      mpech     105:        int inquote = 0;
1.1       deraadt   106:        while (isspace(c))
                    107:            c = *++cp;
                    108:        if (c == '\0')
                    109:            break;
                    110:        *argp++ = cp;
                    111:        margc += 1;
                    112:        for (cp2 = cp; c != '\0'; c = *++cp) {
                    113:            if (inquote) {
                    114:                if (c == inquote) {
                    115:                    inquote = 0;
                    116:                    continue;
                    117:                }
                    118:            } else {
                    119:                if (c == '\\') {
                    120:                    if ((c = *++cp) == '\0')
                    121:                        break;
                    122:                } else if (c == '"') {
                    123:                    inquote = '"';
                    124:                    continue;
                    125:                } else if (c == '\'') {
                    126:                    inquote = '\'';
                    127:                    continue;
                    128:                } else if (isspace(c))
                    129:                    break;
                    130:            }
                    131:            *cp2++ = c;
                    132:        }
                    133:        *cp2 = '\0';
                    134:        if (c == '\0')
                    135:            break;
                    136:        cp++;
                    137:     }
                    138:     *argp++ = 0;
                    139: }
                    140:
                    141: /*
                    142:  * Make a character string into a number.
                    143:  *
                    144:  * Todo:  1.  Could take random integers (12, 0x12, 012, 0b1).
                    145:  */
                    146:
1.15      art       147:        static char
1.1       deraadt   148: special(s)
1.39      mpech     149:        char *s;
1.1       deraadt   150: {
1.39      mpech     151:        char c;
1.1       deraadt   152:        char b;
                    153:
                    154:        switch (*s) {
                    155:        case '^':
                    156:                b = *++s;
                    157:                if (b == '?') {
                    158:                    c = b | 0x40;               /* DEL */
                    159:                } else {
                    160:                    c = b & 0x1f;
                    161:                }
                    162:                break;
                    163:        default:
                    164:                c = *s;
                    165:                break;
                    166:        }
                    167:        return c;
                    168: }
                    169:
                    170: /*
                    171:  * Construct a control character sequence
                    172:  * for a special character.
                    173:  */
                    174:        static char *
                    175: control(c)
1.39      mpech     176:        cc_t c;
1.1       deraadt   177: {
                    178:        static char buf[5];
                    179:        /*
                    180:         * The only way I could get the Sun 3.5 compiler
                    181:         * to shut up about
                    182:         *      if ((unsigned int)c >= 0x80)
                    183:         * was to assign "c" to an unsigned int variable...
                    184:         * Arggg....
                    185:         */
1.39      mpech     186:        unsigned int uic = (unsigned int)c;
1.1       deraadt   187:
                    188:        if (uic == 0x7f)
                    189:                return ("^?");
                    190:        if (c == (cc_t)_POSIX_VDISABLE) {
                    191:                return "off";
                    192:        }
                    193:        if (uic >= 0x80) {
                    194:                buf[0] = '\\';
                    195:                buf[1] = ((c>>6)&07) + '0';
                    196:                buf[2] = ((c>>3)&07) + '0';
                    197:                buf[3] = (c&07) + '0';
                    198:                buf[4] = 0;
                    199:        } else if (uic >= 0x20) {
                    200:                buf[0] = c;
                    201:                buf[1] = 0;
                    202:        } else {
                    203:                buf[0] = '^';
                    204:                buf[1] = '@'+c;
                    205:                buf[2] = 0;
                    206:        }
                    207:        return (buf);
                    208: }
                    209:
                    210:
                    211:
                    212: /*
                    213:  *     The following are data structures and routines for
                    214:  *     the "send" command.
                    215:  *
                    216:  */
1.3       niklas    217:
1.1       deraadt   218: struct sendlist {
                    219:     char       *name;          /* How user refers to it (case independent) */
                    220:     char       *help;          /* Help information (0 ==> no help) */
                    221:     int                needconnect;    /* Need to be connected */
                    222:     int                narg;           /* Number of arguments */
                    223:     int                (*handler)();   /* Routine to perform (for special ops) */
                    224:     int                nbyte;          /* Number of bytes to send this command */
                    225:     int                what;           /* Character to be sent (<0 ==> special) */
                    226: };
                    227: 
                    228:
                    229: static int
1.41      millert   230:        send_esc(void),
                    231:        send_help(void),
                    232:        send_docmd(char *),
                    233:        send_dontcmd(char *),
                    234:        send_willcmd(char *),
                    235:        send_wontcmd(char *);
1.1       deraadt   236:
                    237: static struct sendlist Sendlist[] = {
                    238:     { "ao",    "Send Telnet Abort output",             1, 0, 0, 2, AO },
                    239:     { "ayt",   "Send Telnet 'Are You There'",          1, 0, 0, 2, AYT },
                    240:     { "brk",   "Send Telnet Break",                    1, 0, 0, 2, BREAK },
                    241:     { "break", 0,                                      1, 0, 0, 2, BREAK },
                    242:     { "ec",    "Send Telnet Erase Character",          1, 0, 0, 2, EC },
                    243:     { "el",    "Send Telnet Erase Line",               1, 0, 0, 2, EL },
                    244:     { "escape",        "Send current escape character",        1, 0, send_esc, 1, 0 },
                    245:     { "ga",    "Send Telnet 'Go Ahead' sequence",      1, 0, 0, 2, GA },
                    246:     { "ip",    "Send Telnet Interrupt Process",        1, 0, 0, 2, IP },
                    247:     { "intp",  0,                                      1, 0, 0, 2, IP },
                    248:     { "interrupt", 0,                                  1, 0, 0, 2, IP },
                    249:     { "intr",  0,                                      1, 0, 0, 2, IP },
                    250:     { "nop",   "Send Telnet 'No operation'",           1, 0, 0, 2, NOP },
                    251:     { "eor",   "Send Telnet 'End of Record'",          1, 0, 0, 2, EOR },
                    252:     { "abort", "Send Telnet 'Abort Process'",          1, 0, 0, 2, ABORT },
                    253:     { "susp",  "Send Telnet 'Suspend Process'",        1, 0, 0, 2, SUSP },
                    254:     { "eof",   "Send Telnet End of File Character",    1, 0, 0, 2, xEOF },
                    255:     { "synch", "Perform Telnet 'Synch operation'",     1, 0, dosynch, 2, 0 },
                    256:     { "getstatus", "Send request for STATUS",          1, 0, get_status, 6, 0 },
                    257:     { "?",     "Display send options",                 0, 0, send_help, 0, 0 },
                    258:     { "help",  0,                                      0, 0, send_help, 0, 0 },
                    259:     { "do",    0,                                      0, 1, send_docmd, 3, 0 },
                    260:     { "dont",  0,                                      0, 1, send_dontcmd, 3, 0 },
                    261:     { "will",  0,                                      0, 1, send_willcmd, 3, 0 },
                    262:     { "wont",  0,                                      0, 1, send_wontcmd, 3, 0 },
                    263:     { 0 }
                    264: };
                    265:
                    266: #define        GETSEND(name) ((struct sendlist *) genget(name, (char **) Sendlist, \
                    267:                                sizeof(struct sendlist)))
                    268:
                    269:     static int
                    270: sendcmd(argc, argv)
                    271:     int  argc;
                    272:     char **argv;
                    273: {
                    274:     int count;         /* how many bytes we are going to need to send */
                    275:     int i;
                    276:     struct sendlist *s;        /* pointer to current command */
                    277:     int success = 0;
                    278:     int needconnect = 0;
                    279:
                    280:     if (argc < 2) {
1.10      art       281:        printf("need at least one argument for 'send' command\r\n");
                    282:        printf("'send ?' for help\r\n");
1.1       deraadt   283:        return 0;
                    284:     }
                    285:     /*
                    286:      * First, validate all the send arguments.
                    287:      * In addition, we see how much space we are going to need, and
                    288:      * whether or not we will be doing a "SYNCH" operation (which
                    289:      * flushes the network queue).
                    290:      */
                    291:     count = 0;
                    292:     for (i = 1; i < argc; i++) {
                    293:        s = GETSEND(argv[i]);
                    294:        if (s == 0) {
1.10      art       295:            printf("Unknown send argument '%s'\r\n'send ?' for help.\r\n",
1.1       deraadt   296:                        argv[i]);
                    297:            return 0;
                    298:        } else if (Ambiguous(s)) {
1.10      art       299:            printf("Ambiguous send argument '%s'\r\n'send ?' for help.\r\n",
1.1       deraadt   300:                        argv[i]);
                    301:            return 0;
                    302:        }
                    303:        if (i + s->narg >= argc) {
                    304:            fprintf(stderr,
1.10      art       305:            "Need %d argument%s to 'send %s' command.  'send %s ?' for help.\r\n",
1.1       deraadt   306:                s->narg, s->narg == 1 ? "" : "s", s->name, s->name);
                    307:            return 0;
                    308:        }
                    309:        count += s->nbyte;
                    310:        if (s->handler == send_help) {
                    311:            send_help();
                    312:            return 0;
                    313:        }
                    314:
                    315:        i += s->narg;
                    316:        needconnect += s->needconnect;
                    317:     }
                    318:     if (!connected && needconnect) {
1.10      art       319:        printf("?Need to be connected first.\r\n");
                    320:        printf("'send ?' for help\r\n");
1.1       deraadt   321:        return 0;
                    322:     }
                    323:     /* Now, do we have enough room? */
                    324:     if (NETROOM() < count) {
1.10      art       325:        printf("There is not enough room in the buffer TO the network\r\n");
                    326:        printf("to process your request.  Nothing will be done.\r\n");
                    327:        printf("('send synch' will throw away most data in the network\r\n");
                    328:        printf("buffer, if this might help.)\r\n");
1.1       deraadt   329:        return 0;
                    330:     }
                    331:     /* OK, they are all OK, now go through again and actually send */
                    332:     count = 0;
                    333:     for (i = 1; i < argc; i++) {
                    334:        if ((s = GETSEND(argv[i])) == 0) {
1.10      art       335:            fprintf(stderr, "Telnet 'send' error - argument disappeared!\r\n");
1.1       deraadt   336:            (void) quit();
                    337:            /*NOTREACHED*/
                    338:        }
                    339:        if (s->handler) {
                    340:            count++;
                    341:            success += (*s->handler)((s->narg > 0) ? argv[i+1] : 0,
                    342:                                  (s->narg > 1) ? argv[i+2] : 0);
                    343:            i += s->narg;
                    344:        } else {
                    345:            NET2ADD(IAC, s->what);
                    346:            printoption("SENT", IAC, s->what);
                    347:        }
                    348:     }
                    349:     return (count == success);
                    350: }
                    351:
1.10      art       352:        static int
                    353: send_tncmd(void (*func)(), char *cmd, char *name);
                    354:
1.1       deraadt   355:     static int
                    356: send_esc()
                    357: {
                    358:     NETADD(escape);
                    359:     return 1;
                    360: }
                    361:
                    362:     static int
                    363: send_docmd(name)
                    364:     char *name;
                    365: {
                    366:     return(send_tncmd(send_do, "do", name));
                    367: }
                    368:
                    369:     static int
                    370: send_dontcmd(name)
                    371:     char *name;
                    372: {
                    373:     return(send_tncmd(send_dont, "dont", name));
                    374: }
                    375:     static int
                    376: send_willcmd(name)
                    377:     char *name;
                    378: {
                    379:     return(send_tncmd(send_will, "will", name));
                    380: }
                    381:     static int
                    382: send_wontcmd(name)
                    383:     char *name;
                    384: {
                    385:     return(send_tncmd(send_wont, "wont", name));
                    386: }
                    387:
                    388:     int
                    389: send_tncmd(func, cmd, name)
                    390:     void       (*func)();
                    391:     char       *cmd, *name;
                    392: {
                    393:     char **cpp;
                    394:     extern char *telopts[];
1.39      mpech     395:     int val = 0;
1.1       deraadt   396:
1.10      art       397:     if (isprefix(name, "help") || isprefix(name, "?")) {
1.39      mpech     398:        int col, len;
1.1       deraadt   399:
1.10      art       400:        printf("Usage: send %s <value|option>\r\n", cmd);
                    401:        printf("\"value\" must be from 0 to 255\r\n");
                    402:        printf("Valid options are:\r\n\t");
1.1       deraadt   403:
                    404:        col = 8;
                    405:        for (cpp = telopts; *cpp; cpp++) {
                    406:            len = strlen(*cpp) + 3;
                    407:            if (col + len > 65) {
1.10      art       408:                printf("\r\n\t");
1.1       deraadt   409:                col = 8;
                    410:            }
                    411:            printf(" \"%s\"", *cpp);
                    412:            col += len;
                    413:        }
1.10      art       414:        printf("\r\n");
1.1       deraadt   415:        return 0;
                    416:     }
                    417:     cpp = (char **)genget(name, telopts, sizeof(char *));
                    418:     if (Ambiguous(cpp)) {
1.10      art       419:        fprintf(stderr,"'%s': ambiguous argument ('send %s ?' for help).\r\n",
1.1       deraadt   420:                                        name, cmd);
                    421:        return 0;
                    422:     }
                    423:     if (cpp) {
                    424:        val = cpp - telopts;
                    425:     } else {
1.39      mpech     426:        char *cp = name;
1.1       deraadt   427:
                    428:        while (*cp >= '0' && *cp <= '9') {
                    429:            val *= 10;
                    430:            val += *cp - '0';
                    431:            cp++;
                    432:        }
                    433:        if (*cp != 0) {
1.10      art       434:            fprintf(stderr, "'%s': unknown argument ('send %s ?' for help).\r\n",
1.1       deraadt   435:                                        name, cmd);
                    436:            return 0;
                    437:        } else if (val < 0 || val > 255) {
1.10      art       438:            fprintf(stderr, "'%s': bad value ('send %s ?' for help).\r\n",
1.1       deraadt   439:                                        name, cmd);
                    440:            return 0;
                    441:        }
                    442:     }
                    443:     if (!connected) {
1.10      art       444:        printf("?Need to be connected first.\r\n");
1.1       deraadt   445:        return 0;
                    446:     }
                    447:     (*func)(val, 1);
                    448:     return 1;
                    449: }
                    450:
                    451:     static int
                    452: send_help()
                    453: {
                    454:     struct sendlist *s;        /* pointer to current command */
                    455:     for (s = Sendlist; s->name; s++) {
                    456:        if (s->help)
1.10      art       457:            printf("%-15s %s\r\n", s->name, s->help);
1.1       deraadt   458:     }
                    459:     return(0);
                    460: }
                    461: 
                    462: /*
                    463:  * The following are the routines and data structures referred
                    464:  * to by the arguments to the "toggle" command.
                    465:  */
                    466:
                    467:     static int
                    468: lclchars()
                    469: {
                    470:     donelclchars = 1;
                    471:     return 1;
                    472: }
                    473:
                    474:     static int
                    475: togdebug()
                    476: {
                    477: #ifndef        NOT43
                    478:     if (net > 0 &&
                    479:        (SetSockOpt(net, SOL_SOCKET, SO_DEBUG, debug)) < 0) {
                    480:            perror("setsockopt (SO_DEBUG)");
                    481:     }
                    482: #else  /* NOT43 */
                    483:     if (debug) {
1.10      art       484:        if (net > 0 && SetSockOpt(net, SOL_SOCKET, SO_DEBUG, 0, 0) < 0)
1.1       deraadt   485:            perror("setsockopt (SO_DEBUG)");
                    486:     } else
1.10      art       487:        printf("Cannot turn off socket debugging\r\n");
1.1       deraadt   488: #endif /* NOT43 */
                    489:     return 1;
                    490: }
                    491:
                    492:
                    493:     static int
                    494: togcrlf()
                    495: {
                    496:     if (crlf) {
1.10      art       497:        printf("Will send carriage returns as telnet <CR><LF>.\r\n");
1.1       deraadt   498:     } else {
1.10      art       499:        printf("Will send carriage returns as telnet <CR><NUL>.\r\n");
1.1       deraadt   500:     }
                    501:     return 1;
                    502: }
                    503:
                    504: int binmode;
                    505:
                    506:     static int
                    507: togbinary(val)
                    508:     int val;
                    509: {
                    510:     donebinarytoggle = 1;
                    511:
                    512:     if (val >= 0) {
                    513:        binmode = val;
                    514:     } else {
                    515:        if (my_want_state_is_will(TELOPT_BINARY) &&
                    516:                                my_want_state_is_do(TELOPT_BINARY)) {
                    517:            binmode = 1;
                    518:        } else if (my_want_state_is_wont(TELOPT_BINARY) &&
                    519:                                my_want_state_is_dont(TELOPT_BINARY)) {
                    520:            binmode = 0;
                    521:        }
                    522:        val = binmode ? 0 : 1;
                    523:     }
                    524:
                    525:     if (val == 1) {
                    526:        if (my_want_state_is_will(TELOPT_BINARY) &&
                    527:                                        my_want_state_is_do(TELOPT_BINARY)) {
1.10      art       528:            printf("Already operating in binary mode with remote host.\r\n");
1.1       deraadt   529:        } else {
1.10      art       530:            printf("Negotiating binary mode with remote host.\r\n");
1.1       deraadt   531:            tel_enter_binary(3);
                    532:        }
                    533:     } else {
                    534:        if (my_want_state_is_wont(TELOPT_BINARY) &&
                    535:                                        my_want_state_is_dont(TELOPT_BINARY)) {
1.10      art       536:            printf("Already in network ascii mode with remote host.\r\n");
1.1       deraadt   537:        } else {
1.10      art       538:            printf("Negotiating network ascii mode with remote host.\r\n");
1.1       deraadt   539:            tel_leave_binary(3);
                    540:        }
                    541:     }
                    542:     return 1;
                    543: }
                    544:
                    545:     static int
                    546: togrbinary(val)
                    547:     int val;
                    548: {
                    549:     donebinarytoggle = 1;
                    550:
                    551:     if (val == -1)
                    552:        val = my_want_state_is_do(TELOPT_BINARY) ? 0 : 1;
                    553:
                    554:     if (val == 1) {
                    555:        if (my_want_state_is_do(TELOPT_BINARY)) {
1.10      art       556:            printf("Already receiving in binary mode.\r\n");
1.1       deraadt   557:        } else {
1.10      art       558:            printf("Negotiating binary mode on input.\r\n");
1.1       deraadt   559:            tel_enter_binary(1);
                    560:        }
                    561:     } else {
                    562:        if (my_want_state_is_dont(TELOPT_BINARY)) {
1.10      art       563:            printf("Already receiving in network ascii mode.\r\n");
1.1       deraadt   564:        } else {
1.10      art       565:            printf("Negotiating network ascii mode on input.\r\n");
1.1       deraadt   566:            tel_leave_binary(1);
                    567:        }
                    568:     }
                    569:     return 1;
                    570: }
                    571:
                    572:     static int
                    573: togxbinary(val)
                    574:     int val;
                    575: {
                    576:     donebinarytoggle = 1;
                    577:
                    578:     if (val == -1)
                    579:        val = my_want_state_is_will(TELOPT_BINARY) ? 0 : 1;
                    580:
                    581:     if (val == 1) {
                    582:        if (my_want_state_is_will(TELOPT_BINARY)) {
1.10      art       583:            printf("Already transmitting in binary mode.\r\n");
1.1       deraadt   584:        } else {
1.10      art       585:            printf("Negotiating binary mode on output.\r\n");
1.1       deraadt   586:            tel_enter_binary(2);
                    587:        }
                    588:     } else {
                    589:        if (my_want_state_is_wont(TELOPT_BINARY)) {
1.10      art       590:            printf("Already transmitting in network ascii mode.\r\n");
1.1       deraadt   591:        } else {
1.10      art       592:            printf("Negotiating network ascii mode on output.\r\n");
1.1       deraadt   593:            tel_leave_binary(2);
                    594:        }
                    595:     }
                    596:     return 1;
                    597: }
                    598:
                    599:
1.41      millert   600: static int togglehelp(void);
1.1       deraadt   601: #if    defined(AUTHENTICATION)
1.41      millert   602: extern int auth_togdebug(int);
1.1       deraadt   603: #endif
1.10      art       604: #if    defined(ENCRYPTION)
1.41      millert   605: extern int EncryptAutoEnc(int);
                    606: extern int EncryptAutoDec(int);
                    607: extern int EncryptDebug(int);
                    608: extern int EncryptVerbose(int);
1.10      art       609: #endif
                    610:
1.1       deraadt   611:
                    612: struct togglelist {
                    613:     char       *name;          /* name of toggle */
                    614:     char       *help;          /* help message */
                    615:     int                (*handler)();   /* routine to do actual setting */
                    616:     int                *variable;
                    617:     char       *actionexplanation;
1.8       robin     618:     int                needconnect;    /* Need to be connected */
1.1       deraadt   619: };
                    620:
                    621: static struct togglelist Togglelist[] = {
                    622:     { "autoflush",
                    623:        "flushing of output when sending interrupt characters",
                    624:            0,
                    625:                &autoflush,
1.10      art       626:                    "flush output when sending interrupt characters" },
1.1       deraadt   627:     { "autosynch",
                    628:        "automatic sending of interrupt characters in urgent mode",
                    629:            0,
                    630:                &autosynch,
1.10      art       631:                    "send interrupt characters in urgent mode" },
1.1       deraadt   632: #if    defined(AUTHENTICATION)
                    633:     { "autologin",
                    634:        "automatic sending of login and/or authentication info",
                    635:            0,
                    636:                &autologin,
1.10      art       637:                    "send login name and/or authentication information" },
1.1       deraadt   638:     { "authdebug",
                    639:        "Toggle authentication debugging",
                    640:            auth_togdebug,
                    641:                0,
1.10      art       642:                     "print authentication debugging information" },
                    643: #endif
                    644: #if    defined(ENCRYPTION)
                    645:     { "autoencrypt",
                    646:        "automatic encryption of data stream",
                    647:            EncryptAutoEnc,
                    648:                0,
                    649:                    "automatically encrypt output" },
                    650:     { "autodecrypt",
                    651:        "automatic decryption of data stream",
                    652:            EncryptAutoDec,
                    653:                0,
                    654:                    "automatically decrypt input" },
                    655:     { "verbose_encrypt",
                    656:        "Toggle verbose encryption output",
                    657:            EncryptVerbose,
                    658:                0,
                    659:                    "print verbose encryption output" },
                    660:     { "encdebug",
                    661:        "Toggle encryption debugging",
                    662:            EncryptDebug,
                    663:                0,
                    664:                    "print encryption debugging information" },
1.1       deraadt   665: #endif
                    666:     { "skiprc",
                    667:        "don't read ~/.telnetrc file",
                    668:            0,
                    669:                &skiprc,
1.10      art       670:                    "skip reading of ~/.telnetrc file" },
1.1       deraadt   671:     { "binary",
                    672:        "sending and receiving of binary data",
                    673:            togbinary,
                    674:                0,
1.10      art       675:                    0 },
1.1       deraadt   676:     { "inbinary",
                    677:        "receiving of binary data",
                    678:            togrbinary,
                    679:                0,
1.10      art       680:                    0 },
1.1       deraadt   681:     { "outbinary",
                    682:        "sending of binary data",
                    683:            togxbinary,
                    684:                0,
1.10      art       685:                    0 },
1.1       deraadt   686:     { "crlf",
                    687:        "sending carriage returns as telnet <CR><LF>",
                    688:            togcrlf,
                    689:                &crlf,
1.10      art       690:                    0 },
1.1       deraadt   691:     { "crmod",
                    692:        "mapping of received carriage returns",
                    693:            0,
                    694:                &crmod,
1.10      art       695:                    "map carriage return on output" },
1.1       deraadt   696:     { "localchars",
                    697:        "local recognition of certain control characters",
                    698:            lclchars,
                    699:                &localchars,
1.10      art       700:                    "recognize certain control characters" },
1.8       robin     701:     { " ", "", 0, 0 },         /* empty line */
1.1       deraadt   702: #if    defined(unix) && defined(TN3270)
                    703:     { "apitrace",
                    704:        "(debugging) toggle tracing of API transactions",
                    705:            0,
                    706:                &apitrace,
1.8       robin     707:                    "trace API transactions", 0 },
1.1       deraadt   708:     { "cursesdata",
                    709:        "(debugging) toggle printing of hexadecimal curses data",
                    710:            0,
                    711:                &cursesdata,
1.8       robin     712:                    "print hexadecimal representation of curses data", 0 },
1.1       deraadt   713: #endif /* defined(unix) && defined(TN3270) */
                    714:     { "debug",
                    715:        "debugging",
                    716:            togdebug,
                    717:                &debug,
1.10      art       718:                    "turn on socket level debugging" },
1.1       deraadt   719:     { "netdata",
                    720:        "printing of hexadecimal network data (debugging)",
                    721:            0,
                    722:                &netdata,
1.10      art       723:                    "print hexadecimal representation of network traffic" },
1.1       deraadt   724:     { "prettydump",
                    725:        "output of \"netdata\" to user readable format (debugging)",
                    726:            0,
                    727:                &prettydump,
1.10      art       728:                    "print user readable output for \"netdata\"" },
1.1       deraadt   729:     { "options",
                    730:        "viewing of options processing (debugging)",
                    731:            0,
                    732:                &showoptions,
1.10      art       733:                    "show option processing" },
1.1       deraadt   734: #if    defined(unix)
                    735:     { "termdata",
                    736:        "(debugging) toggle printing of hexadecimal terminal data",
                    737:            0,
                    738:                &termdata,
1.10      art       739:                    "print hexadecimal representation of terminal traffic" },
1.1       deraadt   740: #endif /* defined(unix) */
                    741:     { "?",
                    742:        0,
1.10      art       743:            togglehelp },
1.1       deraadt   744:     { "help",
                    745:        0,
1.10      art       746:            togglehelp },
1.1       deraadt   747:     { 0 }
                    748: };
                    749:
                    750:     static int
                    751: togglehelp()
                    752: {
                    753:     struct togglelist *c;
                    754:
                    755:     for (c = Togglelist; c->name; c++) {
                    756:        if (c->help) {
                    757:            if (*c->help)
1.10      art       758:                printf("%-15s toggle %s\r\n", c->name, c->help);
1.1       deraadt   759:            else
1.10      art       760:                printf("\r\n");
1.1       deraadt   761:        }
                    762:     }
1.10      art       763:     printf("\r\n");
                    764:     printf("%-15s %s\r\n", "?", "display help information");
1.1       deraadt   765:     return 0;
                    766: }
                    767:
                    768:     static void
                    769: settogglehelp(set)
                    770:     int set;
                    771: {
                    772:     struct togglelist *c;
                    773:
                    774:     for (c = Togglelist; c->name; c++) {
                    775:        if (c->help) {
                    776:            if (*c->help)
1.10      art       777:                printf("%-15s %s %s\r\n", c->name, set ? "enable" : "disable",
1.1       deraadt   778:                                                c->help);
                    779:            else
1.10      art       780:                printf("\r\n");
1.1       deraadt   781:        }
                    782:     }
                    783: }
                    784:
                    785: #define        GETTOGGLE(name) (struct togglelist *) \
                    786:                genget(name, (char **) Togglelist, sizeof(struct togglelist))
                    787:
                    788:     static int
                    789: toggle(argc, argv)
                    790:     int  argc;
                    791:     char *argv[];
                    792: {
                    793:     int retval = 1;
                    794:     char *name;
                    795:     struct togglelist *c;
                    796:
                    797:     if (argc < 2) {
                    798:        fprintf(stderr,
1.10      art       799:            "Need an argument to 'toggle' command.  'toggle ?' for help.\r\n");
1.1       deraadt   800:        return 0;
                    801:     }
                    802:     argc--;
                    803:     argv++;
                    804:     while (argc--) {
                    805:        name = *argv++;
                    806:        c = GETTOGGLE(name);
                    807:        if (Ambiguous(c)) {
1.10      art       808:            fprintf(stderr, "'%s': ambiguous argument ('toggle ?' for help).\r\n",
1.1       deraadt   809:                                        name);
                    810:            return 0;
                    811:        } else if (c == 0) {
1.10      art       812:            fprintf(stderr, "'%s': unknown argument ('toggle ?' for help).\r\n",
1.1       deraadt   813:                                        name);
                    814:            return 0;
1.8       robin     815:        } else if (!connected && c->needconnect) {
1.10      art       816:            printf("?Need to be connected first.\r\n");
                    817:            printf("'send ?' for help\r\n");
1.8       robin     818:            return 0;
1.1       deraadt   819:        } else {
                    820:            if (c->variable) {
                    821:                *c->variable = !*c->variable;           /* invert it */
                    822:                if (c->actionexplanation) {
1.10      art       823:                    printf("%s %s.\r\n", *c->variable? "Will" : "Won't",
1.1       deraadt   824:                                                        c->actionexplanation);
                    825:                }
                    826:            }
                    827:            if (c->handler) {
                    828:                retval &= (*c->handler)(-1);
                    829:            }
                    830:        }
                    831:     }
                    832:     return retval;
                    833: }
                    834: 
                    835: /*
                    836:  * The following perform the "set" command.
                    837:  */
                    838:
                    839: #ifdef USE_TERMIO
1.10      art       840: struct termios new_tc = { 0 };
1.1       deraadt   841: #endif
                    842:
                    843: struct setlist {
                    844:     char *name;                                /* name */
                    845:     char *help;                                /* help information */
                    846:     void (*handler)();
                    847:     cc_t *charp;                       /* where it is located at */
                    848: };
                    849:
                    850: static struct setlist Setlist[] = {
                    851: #ifdef KLUDGELINEMODE
                    852:     { "echo",  "character to toggle local echoing on/off", 0, &echoc },
                    853: #endif
                    854:     { "escape",        "character to escape back to telnet command mode", 0, &escape },
                    855:     { "rlogin", "rlogin escape character", 0, &rlogin },
                    856:     { "tracefile", "file to write trace information to", SetNetTrace, (cc_t *)NetTraceFile},
                    857:     { " ", "" },
                    858:     { " ", "The following need 'localchars' to be toggled true", 0, 0 },
1.10      art       859:     { "flushoutput", "character to cause an Abort Output", 0, &termFlushChar },
                    860:     { "interrupt", "character to cause an Interrupt Process", 0, &termIntChar },
                    861:     { "quit",  "character to cause an Abort process", 0, &termQuitChar },
                    862:     { "eof",   "character to cause an EOF ", 0, &termEofChar },
1.1       deraadt   863:     { " ", "" },
                    864:     { " ", "The following are for local editing in linemode", 0, 0 },
1.10      art       865:     { "erase", "character to use to erase a character", 0, &termEraseChar },
                    866:     { "kill",  "character to use to erase a line", 0, &termKillChar },
                    867:     { "lnext", "character to use for literal next", 0, &termLiteralNextChar },
                    868:     { "susp",  "character to cause a Suspend Process", 0, &termSuspChar },
                    869:     { "reprint", "character to use for line reprint", 0, &termRprntChar },
                    870:     { "worderase", "character to use to erase a word", 0, &termWerasChar },
                    871:     { "start", "character to use for XON", 0, &termStartChar },
                    872:     { "stop",  "character to use for XOFF", 0, &termStopChar },
                    873:     { "forw1", "alternate end of line character", 0, &termForw1Char },
                    874:     { "forw2", "alternate end of line character", 0, &termForw2Char },
                    875:     { "ayt",   "alternate AYT character", 0, &termAytChar },
1.1       deraadt   876:     { 0 }
                    877: };
                    878:
                    879:     static struct setlist *
                    880: getset(name)
                    881:     char *name;
                    882: {
                    883:     return (struct setlist *)
                    884:                genget(name, (char **) Setlist, sizeof(struct setlist));
                    885: }
                    886:
                    887:     void
                    888: set_escape_char(s)
                    889:     char *s;
                    890: {
                    891:        if (rlogin != _POSIX_VDISABLE) {
                    892:                rlogin = (s && *s) ? special(s) : _POSIX_VDISABLE;
1.10      art       893:                printf("Telnet rlogin escape character is '%s'.\r\n",
1.1       deraadt   894:                                        control(rlogin));
                    895:        } else {
                    896:                escape = (s && *s) ? special(s) : _POSIX_VDISABLE;
1.10      art       897:                printf("Telnet escape character is '%s'.\r\n", control(escape));
1.1       deraadt   898:        }
                    899: }
                    900:
                    901:     static int
                    902: setcmd(argc, argv)
                    903:     int  argc;
                    904:     char *argv[];
                    905: {
                    906:     int value;
                    907:     struct setlist *ct;
                    908:     struct togglelist *c;
                    909:
                    910:     if (argc < 2 || argc > 3) {
1.10      art       911:        printf("Format is 'set Name Value'\r\n'set ?' for help.\r\n");
1.1       deraadt   912:        return 0;
                    913:     }
                    914:     if ((argc == 2) && (isprefix(argv[1], "?") || isprefix(argv[1], "help"))) {
                    915:        for (ct = Setlist; ct->name; ct++)
1.10      art       916:            printf("%-15s %s\r\n", ct->name, ct->help);
                    917:        printf("\r\n");
1.1       deraadt   918:        settogglehelp(1);
1.10      art       919:        printf("%-15s %s\r\n", "?", "display help information");
1.1       deraadt   920:        return 0;
                    921:     }
                    922:
                    923:     ct = getset(argv[1]);
                    924:     if (ct == 0) {
                    925:        c = GETTOGGLE(argv[1]);
                    926:        if (c == 0) {
1.10      art       927:            fprintf(stderr, "'%s': unknown argument ('set ?' for help).\r\n",
1.1       deraadt   928:                        argv[1]);
                    929:            return 0;
                    930:        } else if (Ambiguous(c)) {
1.10      art       931:            fprintf(stderr, "'%s': ambiguous argument ('set ?' for help).\r\n",
1.1       deraadt   932:                        argv[1]);
                    933:            return 0;
1.8       robin     934:        } else if (!connected && c->needconnect) {
1.10      art       935:            printf("?Need to be connected first.\r\n");
                    936:            printf("'send ?' for help\r\n");
1.8       robin     937:            return 0;
1.1       deraadt   938:        }
1.8       robin     939:
1.1       deraadt   940:        if (c->variable) {
                    941:            if ((argc == 2) || (strcmp("on", argv[2]) == 0))
                    942:                *c->variable = 1;
                    943:            else if (strcmp("off", argv[2]) == 0)
                    944:                *c->variable = 0;
                    945:            else {
1.10      art       946:                printf("Format is 'set togglename [on|off]'\r\n'set ?' for help.\r\n");
1.1       deraadt   947:                return 0;
                    948:            }
                    949:            if (c->actionexplanation) {
1.10      art       950:                printf("%s %s.\r\n", *c->variable? "Will" : "Won't",
1.1       deraadt   951:                                                        c->actionexplanation);
                    952:            }
                    953:        }
                    954:        if (c->handler)
                    955:            (*c->handler)(1);
                    956:     } else if (argc != 3) {
1.10      art       957:        printf("Format is 'set Name Value'\r\n'set ?' for help.\r\n");
1.1       deraadt   958:        return 0;
                    959:     } else if (Ambiguous(ct)) {
1.10      art       960:        fprintf(stderr, "'%s': ambiguous argument ('set ?' for help).\r\n",
1.1       deraadt   961:                        argv[1]);
                    962:        return 0;
                    963:     } else if (ct->handler) {
                    964:        (*ct->handler)(argv[2]);
1.10      art       965:        printf("%s set to \"%s\".\r\n", ct->name, (char *)ct->charp);
1.1       deraadt   966:     } else {
                    967:        if (strcmp("off", argv[2])) {
                    968:            value = special(argv[2]);
                    969:        } else {
                    970:            value = _POSIX_VDISABLE;
                    971:        }
                    972:        *(ct->charp) = (cc_t)value;
1.10      art       973:        printf("%s character is '%s'.\r\n", ct->name, control(*(ct->charp)));
1.1       deraadt   974:     }
                    975:     slc_check();
                    976:     return 1;
                    977: }
                    978:
                    979:     static int
                    980: unsetcmd(argc, argv)
                    981:     int  argc;
                    982:     char *argv[];
                    983: {
                    984:     struct setlist *ct;
                    985:     struct togglelist *c;
1.39      mpech     986:     char *name;
1.1       deraadt   987:
                    988:     if (argc < 2) {
                    989:        fprintf(stderr,
1.10      art       990:            "Need an argument to 'unset' command.  'unset ?' for help.\r\n");
1.1       deraadt   991:        return 0;
                    992:     }
                    993:     if (isprefix(argv[1], "?") || isprefix(argv[1], "help")) {
                    994:        for (ct = Setlist; ct->name; ct++)
1.10      art       995:            printf("%-15s %s\r\n", ct->name, ct->help);
                    996:        printf("\r\n");
1.1       deraadt   997:        settogglehelp(0);
1.10      art       998:        printf("%-15s %s\r\n", "?", "display help information");
1.1       deraadt   999:        return 0;
                   1000:     }
                   1001:
                   1002:     argc--;
                   1003:     argv++;
                   1004:     while (argc--) {
                   1005:        name = *argv++;
                   1006:        ct = getset(name);
                   1007:        if (ct == 0) {
                   1008:            c = GETTOGGLE(name);
                   1009:            if (c == 0) {
1.10      art      1010:                fprintf(stderr, "'%s': unknown argument ('unset ?' for help).\r\n",
1.1       deraadt  1011:                        name);
                   1012:                return 0;
                   1013:            } else if (Ambiguous(c)) {
1.10      art      1014:                fprintf(stderr, "'%s': ambiguous argument ('unset ?' for help).\r\n",
1.1       deraadt  1015:                        name);
                   1016:                return 0;
                   1017:            }
                   1018:            if (c->variable) {
                   1019:                *c->variable = 0;
                   1020:                if (c->actionexplanation) {
1.10      art      1021:                    printf("%s %s.\r\n", *c->variable? "Will" : "Won't",
1.1       deraadt  1022:                                                        c->actionexplanation);
                   1023:                }
                   1024:            }
                   1025:            if (c->handler)
                   1026:                (*c->handler)(0);
                   1027:        } else if (Ambiguous(ct)) {
1.10      art      1028:            fprintf(stderr, "'%s': ambiguous argument ('unset ?' for help).\r\n",
1.1       deraadt  1029:                        name);
                   1030:            return 0;
                   1031:        } else if (ct->handler) {
                   1032:            (*ct->handler)(0);
1.10      art      1033:            printf("%s reset to \"%s\".\r\n", ct->name, (char *)ct->charp);
1.1       deraadt  1034:        } else {
                   1035:            *(ct->charp) = _POSIX_VDISABLE;
1.10      art      1036:            printf("%s character is '%s'.\r\n", ct->name, control(*(ct->charp)));
1.1       deraadt  1037:        }
                   1038:     }
                   1039:     return 1;
                   1040: }
                   1041: 
                   1042: /*
                   1043:  * The following are the data structures and routines for the
                   1044:  * 'mode' command.
                   1045:  */
                   1046: #ifdef KLUDGELINEMODE
                   1047: extern int kludgelinemode;
                   1048:
                   1049:     static int
                   1050: dokludgemode()
                   1051: {
                   1052:     kludgelinemode = 1;
                   1053:     send_wont(TELOPT_LINEMODE, 1);
                   1054:     send_dont(TELOPT_SGA, 1);
                   1055:     send_dont(TELOPT_ECHO, 1);
1.10      art      1056:     return 1;
1.1       deraadt  1057: }
                   1058: #endif
                   1059:
                   1060:     static int
                   1061: dolinemode()
                   1062: {
                   1063: #ifdef KLUDGELINEMODE
                   1064:     if (kludgelinemode)
                   1065:        send_dont(TELOPT_SGA, 1);
                   1066: #endif
                   1067:     send_will(TELOPT_LINEMODE, 1);
                   1068:     send_dont(TELOPT_ECHO, 1);
                   1069:     return 1;
                   1070: }
                   1071:
                   1072:     static int
                   1073: docharmode()
                   1074: {
                   1075: #ifdef KLUDGELINEMODE
                   1076:     if (kludgelinemode)
                   1077:        send_do(TELOPT_SGA, 1);
                   1078:     else
                   1079: #endif
                   1080:     send_wont(TELOPT_LINEMODE, 1);
                   1081:     send_do(TELOPT_ECHO, 1);
                   1082:     return 1;
                   1083: }
                   1084:
                   1085:     static int
                   1086: dolmmode(bit, on)
                   1087:     int bit, on;
                   1088: {
                   1089:     unsigned char c;
                   1090:     extern int linemode;
                   1091:
                   1092:     if (my_want_state_is_wont(TELOPT_LINEMODE)) {
1.10      art      1093:        printf("?Need to have LINEMODE option enabled first.\r\n");
                   1094:        printf("'mode ?' for help.\r\n");
1.1       deraadt  1095:        return 0;
                   1096:     }
                   1097:
                   1098:     if (on)
                   1099:        c = (linemode | bit);
                   1100:     else
                   1101:        c = (linemode & ~bit);
                   1102:     lm_mode(&c, 1, 1);
                   1103:     return 1;
                   1104: }
                   1105:
                   1106:     int
1.10      art      1107: tn_setmode(bit)
1.1       deraadt  1108: {
                   1109:     return dolmmode(bit, 1);
                   1110: }
                   1111:
                   1112:     int
1.10      art      1113: tn_clearmode(bit)
1.1       deraadt  1114: {
                   1115:     return dolmmode(bit, 0);
                   1116: }
                   1117:
                   1118: struct modelist {
                   1119:        char    *name;          /* command name */
                   1120:        char    *help;          /* help string */
                   1121:        int     (*handler)();   /* routine which executes command */
                   1122:        int     needconnect;    /* Do we need to be connected to execute? */
                   1123:        int     arg1;
                   1124: };
                   1125:
1.40      millert  1126: static int modehelp(void);
1.1       deraadt  1127:
                   1128: static struct modelist ModeList[] = {
                   1129:     { "character", "Disable LINEMODE option",  docharmode, 1 },
                   1130: #ifdef KLUDGELINEMODE
                   1131:     { "",      "(or disable obsolete line-by-line mode)", 0 },
                   1132: #endif
                   1133:     { "line",  "Enable LINEMODE option",       dolinemode, 1 },
                   1134: #ifdef KLUDGELINEMODE
                   1135:     { "",      "(or enable obsolete line-by-line mode)", 0 },
                   1136: #endif
                   1137:     { "", "", 0 },
                   1138:     { "",      "These require the LINEMODE option to be enabled", 0 },
1.10      art      1139:     { "isig",  "Enable signal trapping",       tn_setmode, 1, MODE_TRAPSIG },
                   1140:     { "+isig", 0,                              tn_setmode, 1, MODE_TRAPSIG },
                   1141:     { "-isig", "Disable signal trapping",      tn_clearmode, 1, MODE_TRAPSIG },
                   1142:     { "edit",  "Enable character editing",     tn_setmode, 1, MODE_EDIT },
                   1143:     { "+edit", 0,                              tn_setmode, 1, MODE_EDIT },
                   1144:     { "-edit", "Disable character editing",    tn_clearmode, 1, MODE_EDIT },
                   1145:     { "softtabs", "Enable tab expansion",      tn_setmode, 1, MODE_SOFT_TAB },
                   1146:     { "+softtabs", 0,                          tn_setmode, 1, MODE_SOFT_TAB },
                   1147:     { "-softtabs", "Disable character editing",        tn_clearmode, 1, MODE_SOFT_TAB },
                   1148:     { "litecho", "Enable literal character echo", tn_setmode, 1, MODE_LIT_ECHO },
                   1149:     { "+litecho", 0,                           tn_setmode, 1, MODE_LIT_ECHO },
                   1150:     { "-litecho", "Disable literal character echo", tn_clearmode, 1, MODE_LIT_ECHO },
1.1       deraadt  1151:     { "help",  0,                              modehelp, 0 },
                   1152: #ifdef KLUDGELINEMODE
                   1153:     { "kludgeline", 0,                         dokludgemode, 1 },
                   1154: #endif
                   1155:     { "", "", 0 },
                   1156:     { "?",     "Print help information",       modehelp, 0 },
                   1157:     { 0 },
                   1158: };
                   1159:
                   1160:
1.10      art      1161:     static int
1.1       deraadt  1162: modehelp()
                   1163: {
                   1164:     struct modelist *mt;
                   1165:
1.10      art      1166:     printf("format is:  'mode Mode', where 'Mode' is one of:\r\n\r\n");
1.1       deraadt  1167:     for (mt = ModeList; mt->name; mt++) {
                   1168:        if (mt->help) {
                   1169:            if (*mt->help)
1.10      art      1170:                printf("%-15s %s\r\n", mt->name, mt->help);
1.1       deraadt  1171:            else
1.10      art      1172:                printf("\r\n");
1.1       deraadt  1173:        }
                   1174:     }
                   1175:     return 0;
                   1176: }
                   1177:
                   1178: #define        GETMODECMD(name) (struct modelist *) \
                   1179:                genget(name, (char **) ModeList, sizeof(struct modelist))
                   1180:
                   1181:     static int
                   1182: modecmd(argc, argv)
                   1183:     int  argc;
                   1184:     char *argv[];
                   1185: {
                   1186:     struct modelist *mt;
                   1187:
                   1188:     if (argc != 2) {
1.10      art      1189:        printf("'mode' command requires an argument\r\n");
                   1190:        printf("'mode ?' for help.\r\n");
1.1       deraadt  1191:     } else if ((mt = GETMODECMD(argv[1])) == 0) {
1.10      art      1192:        fprintf(stderr, "Unknown mode '%s' ('mode ?' for help).\r\n", argv[1]);
1.1       deraadt  1193:     } else if (Ambiguous(mt)) {
1.10      art      1194:        fprintf(stderr, "Ambiguous mode '%s' ('mode ?' for help).\r\n", argv[1]);
1.1       deraadt  1195:     } else if (mt->needconnect && !connected) {
1.10      art      1196:        printf("?Need to be connected first.\r\n");
                   1197:        printf("'mode ?' for help.\r\n");
1.1       deraadt  1198:     } else if (mt->handler) {
                   1199:        return (*mt->handler)(mt->arg1);
                   1200:     }
                   1201:     return 0;
                   1202: }
                   1203: 
                   1204: /*
                   1205:  * The following data structures and routines implement the
                   1206:  * "display" command.
                   1207:  */
                   1208:
                   1209:     static int
                   1210: display(argc, argv)
                   1211:     int  argc;
                   1212:     char *argv[];
                   1213: {
                   1214:     struct togglelist *tl;
                   1215:     struct setlist *sl;
                   1216:
                   1217: #define        dotog(tl)       if (tl->variable && tl->actionexplanation) { \
                   1218:                            if (*tl->variable) { \
                   1219:                                printf("will"); \
                   1220:                            } else { \
                   1221:                                printf("won't"); \
                   1222:                            } \
1.10      art      1223:                            printf(" %s.\r\n", tl->actionexplanation); \
1.1       deraadt  1224:                        }
                   1225:
                   1226: #define        doset(sl)   if (sl->name && *sl->name != ' ') { \
                   1227:                        if (sl->handler == 0) \
1.10      art      1228:                            printf("%-15s [%s]\r\n", sl->name, control(*sl->charp)); \
1.1       deraadt  1229:                        else \
1.10      art      1230:                            printf("%-15s \"%s\"\r\n", sl->name, (char *)sl->charp); \
1.1       deraadt  1231:                    }
                   1232:
                   1233:     if (argc == 1) {
                   1234:        for (tl = Togglelist; tl->name; tl++) {
                   1235:            dotog(tl);
                   1236:        }
1.10      art      1237:        printf("\r\n");
1.1       deraadt  1238:        for (sl = Setlist; sl->name; sl++) {
                   1239:            doset(sl);
                   1240:        }
                   1241:     } else {
                   1242:        int i;
                   1243:
                   1244:        for (i = 1; i < argc; i++) {
                   1245:            sl = getset(argv[i]);
                   1246:            tl = GETTOGGLE(argv[i]);
                   1247:            if (Ambiguous(sl) || Ambiguous(tl)) {
1.10      art      1248:                printf("?Ambiguous argument '%s'.\r\n", argv[i]);
1.1       deraadt  1249:                return 0;
                   1250:            } else if (!sl && !tl) {
1.10      art      1251:                printf("?Unknown argument '%s'.\r\n", argv[i]);
1.1       deraadt  1252:                return 0;
                   1253:            } else {
                   1254:                if (tl) {
                   1255:                    dotog(tl);
                   1256:                }
                   1257:                if (sl) {
                   1258:                    doset(sl);
                   1259:                }
                   1260:            }
                   1261:        }
                   1262:     }
                   1263: /*@*/optionstatus();
1.10      art      1264: #if    defined(ENCRYPTION)
                   1265:     EncryptStatus();
                   1266: #endif
1.1       deraadt  1267:     return 1;
                   1268: #undef doset
                   1269: #undef dotog
                   1270: }
1.10      art      1271:
1.1       deraadt  1272: /*
                   1273:  * The following are the data structures, and many of the routines,
                   1274:  * relating to command processing.
                   1275:  */
                   1276:
                   1277: /*
                   1278:  * Set the escape character.
                   1279:  */
                   1280:        static int
                   1281: setescape(argc, argv)
                   1282:        int argc;
                   1283:        char *argv[];
                   1284: {
1.39      mpech    1285:        char *arg;
1.1       deraadt  1286:        char buf[50];
                   1287:
                   1288:        printf(
1.10      art      1289:            "Deprecated usage - please use 'set escape%s%s' in the future.\r\n",
1.1       deraadt  1290:                                (argc > 2)? " ":"", (argc > 2)? argv[1]: "");
                   1291:        if (argc > 2)
                   1292:                arg = argv[1];
                   1293:        else {
                   1294:                printf("new escape character: ");
                   1295:                (void) fgets(buf, sizeof(buf), stdin);
                   1296:                arg = buf;
                   1297:        }
                   1298:        if (arg[0] != '\0')
                   1299:                escape = arg[0];
                   1300:        if (!In3270) {
1.10      art      1301:                printf("Escape character is '%s'.\r\n", control(escape));
1.1       deraadt  1302:        }
                   1303:        (void) fflush(stdout);
                   1304:        return 1;
                   1305: }
                   1306:
                   1307:     /*VARARGS*/
                   1308:     static int
                   1309: togcrmod()
                   1310: {
                   1311:     crmod = !crmod;
1.10      art      1312:     printf("Deprecated usage - please use 'toggle crmod' in the future.\r\n");
                   1313:     printf("%s map carriage return on output.\r\n", crmod ? "Will" : "Won't");
1.1       deraadt  1314:     (void) fflush(stdout);
                   1315:     return 1;
                   1316: }
                   1317:
                   1318:     /*VARARGS*/
1.11      deraadt  1319:     int
1.10      art      1320: telnetsuspend()
1.1       deraadt  1321: {
                   1322: #ifdef SIGTSTP
                   1323:     setcommandmode();
                   1324:     {
                   1325:        long oldrows, oldcols, newrows, newcols, err;
                   1326:
                   1327:        err = (TerminalWindowSize(&oldrows, &oldcols) == 0) ? 1 : 0;
                   1328:        (void) kill(0, SIGTSTP);
                   1329:        /*
                   1330:         * If we didn't get the window size before the SUSPEND, but we
1.3       niklas   1331:         * can get them now (?), then send the NAWS to make sure that
1.1       deraadt  1332:         * we are set up for the right window size.
                   1333:         */
                   1334:        if (TerminalWindowSize(&newrows, &newcols) && connected &&
                   1335:            (err || ((oldrows != newrows) || (oldcols != newcols)))) {
                   1336:                sendnaws();
                   1337:        }
                   1338:     }
                   1339:     /* reget parameters in case they were changed */
                   1340:     TerminalSaveState();
                   1341:     setconnmode(0);
                   1342: #else
1.10      art      1343:     printf("Suspend is not supported.  Try the '!' command instead\r\n");
1.1       deraadt  1344: #endif
                   1345:     return 1;
                   1346: }
                   1347:
                   1348: #if    !defined(TN3270)
                   1349:     /*ARGSUSED*/
                   1350:     int
                   1351: shell(argc, argv)
                   1352:     int argc;
                   1353:     char *argv[];
                   1354: {
                   1355:     long oldrows, oldcols, newrows, newcols, err;
                   1356:
                   1357:     setcommandmode();
                   1358:
                   1359:     err = (TerminalWindowSize(&oldrows, &oldcols) == 0) ? 1 : 0;
                   1360:     switch(vfork()) {
                   1361:     case -1:
1.10      art      1362:        perror("Fork failed\r\n");
1.1       deraadt  1363:        break;
                   1364:
                   1365:     case 0:
                   1366:        {
                   1367:            /*
                   1368:             * Fire up the shell in the child.
                   1369:             */
1.39      mpech    1370:            char *shellp, *shellname;
1.1       deraadt  1371:
                   1372:            shellp = getenv("SHELL");
                   1373:            if (shellp == NULL)
                   1374:                shellp = "/bin/sh";
1.3       niklas   1375:            if ((shellname = strrchr(shellp, '/')) == 0)
1.1       deraadt  1376:                shellname = shellp;
                   1377:            else
                   1378:                shellname++;
                   1379:            if (argc > 1)
1.36      deraadt  1380:                execl(shellp, shellname, "-c", &saveline[1], (char *)NULL);
1.1       deraadt  1381:            else
1.36      deraadt  1382:                execl(shellp, shellname, (char *)NULL);
1.1       deraadt  1383:            perror("Execl");
                   1384:            _exit(1);
                   1385:        }
                   1386:     default:
                   1387:            (void)wait((int *)0);       /* Wait for the shell to complete */
                   1388:
                   1389:            if (TerminalWindowSize(&newrows, &newcols) && connected &&
                   1390:                (err || ((oldrows != newrows) || (oldcols != newcols)))) {
                   1391:                    sendnaws();
                   1392:            }
                   1393:            break;
                   1394:     }
                   1395:     return 1;
                   1396: }
                   1397: #else  /* !defined(TN3270) */
                   1398: extern int shell();
                   1399: #endif /* !defined(TN3270) */
                   1400:
                   1401:     /*VARARGS*/
1.15      art      1402:     static int
1.1       deraadt  1403: bye(argc, argv)
                   1404:     int  argc;         /* Number of arguments */
                   1405:     char *argv[];      /* arguments */
                   1406: {
                   1407:     extern int resettermname;
                   1408:
                   1409:     if (connected) {
                   1410:        (void) shutdown(net, 2);
1.10      art      1411:        printf("Connection closed.\r\n");
1.1       deraadt  1412:        (void) NetClose(net);
                   1413:        connected = 0;
                   1414:        resettermname = 1;
1.10      art      1415: #if    defined(AUTHENTICATION) || defined(ENCRYPTION)
1.1       deraadt  1416:        auth_encrypt_connect(connected);
                   1417: #endif /* defined(AUTHENTICATION) */
                   1418:        /* reset options */
                   1419:        tninit();
                   1420: #if    defined(TN3270)
                   1421:        SetIn3270();            /* Get out of 3270 mode */
                   1422: #endif /* defined(TN3270) */
                   1423:     }
                   1424:     if ((argc != 2) || (strcmp(argv[1], "fromquit") != 0)) {
                   1425:        longjmp(toplevel, 1);
                   1426:        /* NOTREACHED */
                   1427:     }
1.10      art      1428:     return 0; /* NOTREACHED */
1.1       deraadt  1429: }
                   1430:
                   1431: /*VARARGS*/
1.10      art      1432:        int
1.1       deraadt  1433: quit()
                   1434: {
                   1435:        (void) call(bye, "bye", "fromquit", 0);
                   1436:        Exit(0);
1.10      art      1437:        return 0; /*NOTREACHED*/
1.1       deraadt  1438: }
                   1439:
                   1440: /*VARARGS*/
1.10      art      1441:        static int
1.1       deraadt  1442: logout()
                   1443: {
                   1444:        send_do(TELOPT_LOGOUT, 1);
                   1445:        (void) netflush();
                   1446:        return 1;
                   1447: }
                   1448:
                   1449: 
                   1450: /*
                   1451:  * The SLC command.
                   1452:  */
                   1453:
                   1454: struct slclist {
                   1455:        char    *name;
                   1456:        char    *help;
                   1457:        void    (*handler)();
                   1458:        int     arg;
                   1459: };
                   1460:
                   1461: static void slc_help();
                   1462:
                   1463: struct slclist SlcList[] = {
                   1464:     { "export",        "Use local special character definitions",
                   1465:                                                slc_mode_export,        0 },
                   1466:     { "import",        "Use remote special character definitions",
                   1467:                                                slc_mode_import,        1 },
                   1468:     { "check", "Verify remote special character definitions",
                   1469:                                                slc_mode_import,        0 },
                   1470:     { "help",  0,                              slc_help,               0 },
                   1471:     { "?",     "Print help information",       slc_help,               0 },
                   1472:     { 0 },
                   1473: };
                   1474:
                   1475:     static void
                   1476: slc_help()
                   1477: {
                   1478:     struct slclist *c;
                   1479:
                   1480:     for (c = SlcList; c->name; c++) {
                   1481:        if (c->help) {
                   1482:            if (*c->help)
1.10      art      1483:                printf("%-15s %s\r\n", c->name, c->help);
1.1       deraadt  1484:            else
1.10      art      1485:                printf("\r\n");
1.1       deraadt  1486:        }
                   1487:     }
                   1488: }
                   1489:
                   1490:     static struct slclist *
                   1491: getslc(name)
                   1492:     char *name;
                   1493: {
                   1494:     return (struct slclist *)
                   1495:                genget(name, (char **) SlcList, sizeof(struct slclist));
                   1496: }
                   1497:
1.15      art      1498:     static int
1.1       deraadt  1499: slccmd(argc, argv)
                   1500:     int  argc;
                   1501:     char *argv[];
                   1502: {
                   1503:     struct slclist *c;
                   1504:
                   1505:     if (argc != 2) {
                   1506:        fprintf(stderr,
1.10      art      1507:            "Need an argument to 'slc' command.  'slc ?' for help.\r\n");
1.1       deraadt  1508:        return 0;
                   1509:     }
                   1510:     c = getslc(argv[1]);
                   1511:     if (c == 0) {
1.10      art      1512:        fprintf(stderr, "'%s': unknown argument ('slc ?' for help).\r\n",
1.1       deraadt  1513:                                argv[1]);
1.3       niklas   1514:        return 0;
1.1       deraadt  1515:     }
                   1516:     if (Ambiguous(c)) {
1.10      art      1517:        fprintf(stderr, "'%s': ambiguous argument ('slc ?' for help).\r\n",
1.1       deraadt  1518:                                argv[1]);
1.3       niklas   1519:        return 0;
1.1       deraadt  1520:     }
                   1521:     (*c->handler)(c->arg);
                   1522:     slcstate();
                   1523:     return 1;
                   1524: }
                   1525: 
                   1526: /*
                   1527:  * The ENVIRON command.
                   1528:  */
                   1529:
                   1530: struct envlist {
                   1531:        char    *name;
                   1532:        char    *help;
                   1533:        void    (*handler)();
                   1534:        int     narg;
                   1535: };
                   1536:
1.41      millert  1537: static void    env_help(void);
1.1       deraadt  1538:
                   1539: struct envlist EnvList[] = {
                   1540:     { "define",        "Define an environment variable",
                   1541:                                                (void (*)())env_define, 2 },
                   1542:     { "undefine", "Undefine an environment variable",
                   1543:                                                env_undefine,   1 },
                   1544:     { "export",        "Mark an environment variable for automatic export",
                   1545:                                                env_export,     1 },
                   1546:     { "unexport", "Don't mark an environment variable for automatic export",
                   1547:                                                env_unexport,   1 },
                   1548:     { "send",  "Send an environment variable", env_send,       1 },
                   1549:     { "list",  "List the current environment variables",
                   1550:                                                env_list,       0 },
                   1551: #if defined(OLD_ENVIRON) && defined(ENV_HACK)
                   1552:     { "varval", "Reverse VAR and VALUE (auto, right, wrong, status)",
                   1553:                                                env_varval,    1 },
                   1554: #endif
                   1555:     { "help",  0,                              env_help,               0 },
                   1556:     { "?",     "Print help information",       env_help,               0 },
                   1557:     { 0 },
                   1558: };
                   1559:
                   1560:     static void
                   1561: env_help()
                   1562: {
                   1563:     struct envlist *c;
                   1564:
                   1565:     for (c = EnvList; c->name; c++) {
                   1566:        if (c->help) {
                   1567:            if (*c->help)
1.10      art      1568:                printf("%-15s %s\r\n", c->name, c->help);
1.1       deraadt  1569:            else
1.10      art      1570:                printf("\r\n");
1.1       deraadt  1571:        }
                   1572:     }
                   1573: }
                   1574:
                   1575:     static struct envlist *
                   1576: getenvcmd(name)
                   1577:     char *name;
                   1578: {
                   1579:     return (struct envlist *)
                   1580:                genget(name, (char **) EnvList, sizeof(struct envlist));
                   1581: }
                   1582:
1.15      art      1583:     int
1.1       deraadt  1584: env_cmd(argc, argv)
                   1585:     int  argc;
                   1586:     char *argv[];
                   1587: {
                   1588:     struct envlist *c;
                   1589:
                   1590:     if (argc < 2) {
                   1591:        fprintf(stderr,
1.10      art      1592:            "Need an argument to 'environ' command.  'environ ?' for help.\r\n");
1.1       deraadt  1593:        return 0;
                   1594:     }
                   1595:     c = getenvcmd(argv[1]);
                   1596:     if (c == 0) {
1.10      art      1597:        fprintf(stderr, "'%s': unknown argument ('environ ?' for help).\r\n",
1.1       deraadt  1598:                                argv[1]);
1.3       niklas   1599:        return 0;
1.1       deraadt  1600:     }
                   1601:     if (Ambiguous(c)) {
1.10      art      1602:        fprintf(stderr, "'%s': ambiguous argument ('environ ?' for help).\r\n",
1.1       deraadt  1603:                                argv[1]);
1.3       niklas   1604:        return 0;
1.1       deraadt  1605:     }
                   1606:     if (c->narg + 2 != argc) {
                   1607:        fprintf(stderr,
1.10      art      1608:            "Need %s%d argument%s to 'environ %s' command.  'environ ?' for help.\r\n",
1.1       deraadt  1609:                c->narg < argc + 2 ? "only " : "",
                   1610:                c->narg, c->narg == 1 ? "" : "s", c->name);
                   1611:        return 0;
                   1612:     }
                   1613:     (*c->handler)(argv[2], argv[3]);
                   1614:     return 1;
                   1615: }
                   1616:
                   1617: struct env_lst {
                   1618:        struct env_lst *next;   /* pointer to next structure */
                   1619:        struct env_lst *prev;   /* pointer to previous structure */
                   1620:        unsigned char *var;     /* pointer to variable name */
                   1621:        unsigned char *value;   /* pointer to variable value */
                   1622:        int export;             /* 1 -> export with default list of variables */
                   1623:        int welldefined;        /* A well defined variable */
                   1624: };
                   1625:
                   1626: struct env_lst envlisthead;
                   1627:
                   1628:        struct env_lst *
                   1629: env_find(var)
                   1630:        unsigned char *var;
                   1631: {
1.39      mpech    1632:        struct env_lst *ep;
1.1       deraadt  1633:
                   1634:        for (ep = envlisthead.next; ep; ep = ep->next) {
                   1635:                if (strcmp((char *)ep->var, (char *)var) == 0)
                   1636:                        return(ep);
                   1637:        }
                   1638:        return(NULL);
                   1639: }
                   1640:
                   1641:        void
                   1642: env_init()
                   1643: {
                   1644:        extern char **environ;
1.10      art      1645:        char **epp, *cp;
                   1646:        struct env_lst *ep;
1.1       deraadt  1647:
                   1648:        for (epp = environ; *epp; epp++) {
1.10      art      1649:                if ((cp = strchr(*epp, '='))) {
1.1       deraadt  1650:                        *cp = '\0';
                   1651:                        ep = env_define((unsigned char *)*epp,
                   1652:                                        (unsigned char *)cp+1);
                   1653:                        ep->export = 0;
                   1654:                        *cp = '=';
                   1655:                }
                   1656:        }
                   1657:        /*
                   1658:         * Special case for DISPLAY variable.  If it is ":0.0" or
                   1659:         * "unix:0.0", we have to get rid of "unix" and insert our
                   1660:         * hostname.
                   1661:         */
                   1662:        if ((ep = env_find("DISPLAY"))
                   1663:            && ((*ep->value == ':')
1.3       niklas   1664:                || (strncmp((char *)ep->value, "unix:", 5) == 0))) {
1.18      deraadt  1665:                char hbuf[MAXHOSTNAMELEN];
1.3       niklas   1666:                char *cp2 = strchr((char *)ep->value, ':');
1.1       deraadt  1667:
1.18      deraadt  1668:                gethostname(hbuf, sizeof hbuf);
1.10      art      1669:
                   1670:                /* If this is not the full name, try to get it via DNS */
                   1671:                if (strchr(hbuf, '.') == 0) {
                   1672:                        struct hostent *he = gethostbyname(hbuf);
                   1673:                        if (he != 0)
1.19      deraadt  1674:                                strncpy(hbuf, he->h_name, sizeof hbuf-1);
                   1675:                        hbuf[sizeof hbuf-1] = '\0';
1.10      art      1676:                }
                   1677:
1.44      pvalchev 1678:                if (asprintf (&cp, "%s%s", hbuf, cp2) == -1)
1.14      art      1679:                        err(1, "asprintf");
1.10      art      1680:
1.1       deraadt  1681:                free(ep->value);
                   1682:                ep->value = (unsigned char *)cp;
                   1683:        }
                   1684:        /*
                   1685:         * If USER is not defined, but LOGNAME is, then add
                   1686:         * USER with the value from LOGNAME.  By default, we
                   1687:         * don't export the USER variable.
                   1688:         */
                   1689:        if ((env_find("USER") == NULL) && (ep = env_find("LOGNAME"))) {
                   1690:                env_define((unsigned char *)"USER", ep->value);
                   1691:                env_unexport((unsigned char *)"USER");
                   1692:        }
                   1693:        env_export((unsigned char *)"DISPLAY");
                   1694:        env_export((unsigned char *)"PRINTER");
1.10      art      1695:        env_export((unsigned char *)"XAUTHORITY");
1.1       deraadt  1696: }
                   1697:
                   1698:        struct env_lst *
                   1699: env_define(var, value)
                   1700:        unsigned char *var, *value;
                   1701: {
1.39      mpech    1702:        struct env_lst *ep;
1.1       deraadt  1703:
1.10      art      1704:        if ((ep = env_find(var))) {
1.1       deraadt  1705:                if (ep->var)
                   1706:                        free(ep->var);
                   1707:                if (ep->value)
                   1708:                        free(ep->value);
                   1709:        } else {
1.14      art      1710:                if ((ep = malloc(sizeof(struct env_lst))) == NULL)
                   1711:                        err(1, "malloc");
1.1       deraadt  1712:                ep->next = envlisthead.next;
                   1713:                envlisthead.next = ep;
                   1714:                ep->prev = &envlisthead;
                   1715:                if (ep->next)
                   1716:                        ep->next->prev = ep;
                   1717:        }
1.10      art      1718:        ep->welldefined = opt_welldefined((char *)var);
1.1       deraadt  1719:        ep->export = 1;
1.14      art      1720:        if ((ep->var = strdup((char *)var)) == NULL)
                   1721:                err(1, "strdup");
                   1722:        if ((ep->value = strdup((char *)value)) == NULL)
                   1723:                err(1, "strdup");
1.1       deraadt  1724:        return(ep);
                   1725: }
                   1726:
                   1727:        void
                   1728: env_undefine(var)
                   1729:        unsigned char *var;
                   1730: {
1.39      mpech    1731:        struct env_lst *ep;
1.1       deraadt  1732:
1.10      art      1733:        if ((ep = env_find(var))) {
1.1       deraadt  1734:                ep->prev->next = ep->next;
                   1735:                if (ep->next)
                   1736:                        ep->next->prev = ep->prev;
                   1737:                if (ep->var)
                   1738:                        free(ep->var);
                   1739:                if (ep->value)
                   1740:                        free(ep->value);
                   1741:                free(ep);
                   1742:        }
                   1743: }
                   1744:
                   1745:        void
                   1746: env_export(var)
                   1747:        unsigned char *var;
                   1748: {
1.39      mpech    1749:        struct env_lst *ep;
1.1       deraadt  1750:
1.10      art      1751:        if ((ep = env_find(var)))
1.1       deraadt  1752:                ep->export = 1;
                   1753: }
                   1754:
                   1755:        void
                   1756: env_unexport(var)
                   1757:        unsigned char *var;
                   1758: {
1.39      mpech    1759:        struct env_lst *ep;
1.1       deraadt  1760:
1.15      art      1761:        if ((ep = env_find(var)) != NULL)
1.1       deraadt  1762:                ep->export = 0;
                   1763: }
                   1764:
                   1765:        void
                   1766: env_send(var)
                   1767:        unsigned char *var;
                   1768: {
1.39      mpech    1769:        struct env_lst *ep;
1.1       deraadt  1770:
1.3       niklas   1771:        if (my_state_is_wont(TELOPT_NEW_ENVIRON)
1.1       deraadt  1772: #ifdef OLD_ENVIRON
                   1773:            && my_state_is_wont(TELOPT_OLD_ENVIRON)
                   1774: #endif
                   1775:                ) {
                   1776:                fprintf(stderr,
1.10      art      1777:                    "Cannot send '%s': Telnet ENVIRON option not enabled\r\n",
1.1       deraadt  1778:                                                                        var);
                   1779:                return;
                   1780:        }
                   1781:        ep = env_find(var);
                   1782:        if (ep == 0) {
1.10      art      1783:                fprintf(stderr, "Cannot send '%s': variable not defined\r\n",
1.1       deraadt  1784:                                                                        var);
                   1785:                return;
                   1786:        }
                   1787:        env_opt_start_info();
                   1788:        env_opt_add(ep->var);
                   1789:        env_opt_end(0);
                   1790: }
                   1791:
                   1792:        void
                   1793: env_list()
                   1794: {
1.39      mpech    1795:        struct env_lst *ep;
1.1       deraadt  1796:
                   1797:        for (ep = envlisthead.next; ep; ep = ep->next) {
1.10      art      1798:                printf("%c %-20s %s\r\n", ep->export ? '*' : ' ',
1.1       deraadt  1799:                                        ep->var, ep->value);
                   1800:        }
                   1801: }
                   1802:
                   1803:        unsigned char *
                   1804: env_default(init, welldefined)
                   1805:        int init;
                   1806: {
                   1807:        static struct env_lst *nep = NULL;
                   1808:
                   1809:        if (init) {
                   1810:                nep = &envlisthead;
1.10      art      1811:                return NULL;
1.1       deraadt  1812:        }
                   1813:        if (nep) {
1.10      art      1814:                while ((nep = nep->next)) {
1.1       deraadt  1815:                        if (nep->export && (nep->welldefined == welldefined))
                   1816:                                return(nep->var);
                   1817:                }
                   1818:        }
                   1819:        return(NULL);
                   1820: }
                   1821:
                   1822:        unsigned char *
1.48      otto     1823: env_getvalue(var, exported_only)
1.1       deraadt  1824:        unsigned char *var;
1.48      otto     1825:        int exported_only;
1.1       deraadt  1826: {
1.39      mpech    1827:        struct env_lst *ep;
1.1       deraadt  1828:
1.48      otto     1829:        if ((ep = env_find(var)) && (!exported_only || ep->export))
1.1       deraadt  1830:                return(ep->value);
                   1831:        return(NULL);
                   1832: }
                   1833:
                   1834: #if defined(OLD_ENVIRON) && defined(ENV_HACK)
                   1835:        void
                   1836: env_varval(what)
                   1837:        unsigned char *what;
                   1838: {
                   1839:        extern int old_env_var, old_env_value, env_auto;
                   1840:        int len = strlen((char *)what);
                   1841:
                   1842:        if (len == 0)
                   1843:                goto unknown;
                   1844:
                   1845:        if (strncasecmp((char *)what, "status", len) == 0) {
                   1846:                if (env_auto)
                   1847:                        printf("%s%s", "VAR and VALUE are/will be ",
1.10      art      1848:                                        "determined automatically\r\n");
1.1       deraadt  1849:                if (old_env_var == OLD_ENV_VAR)
1.10      art      1850:                        printf("VAR and VALUE set to correct definitions\r\n");
1.1       deraadt  1851:                else
1.10      art      1852:                        printf("VAR and VALUE definitions are reversed\r\n");
1.1       deraadt  1853:        } else if (strncasecmp((char *)what, "auto", len) == 0) {
                   1854:                env_auto = 1;
                   1855:                old_env_var = OLD_ENV_VALUE;
                   1856:                old_env_value = OLD_ENV_VAR;
                   1857:        } else if (strncasecmp((char *)what, "right", len) == 0) {
                   1858:                env_auto = 0;
                   1859:                old_env_var = OLD_ENV_VAR;
                   1860:                old_env_value = OLD_ENV_VALUE;
                   1861:        } else if (strncasecmp((char *)what, "wrong", len) == 0) {
                   1862:                env_auto = 0;
                   1863:                old_env_var = OLD_ENV_VALUE;
                   1864:                old_env_value = OLD_ENV_VAR;
                   1865:        } else {
                   1866: unknown:
1.10      art      1867:                printf("Unknown \"varval\" command. (\"auto\", \"right\", \"wrong\", \"status\")\r\n");
1.1       deraadt  1868:        }
                   1869: }
                   1870: #endif
                   1871:
                   1872: #if    defined(AUTHENTICATION)
                   1873: /*
                   1874:  * The AUTHENTICATE command.
                   1875:  */
                   1876:
                   1877: struct authlist {
                   1878:        char    *name;
                   1879:        char    *help;
                   1880:        int     (*handler)();
                   1881:        int     narg;
                   1882: };
                   1883:
                   1884: static int
1.41      millert  1885:        auth_help(void);
1.1       deraadt  1886:
                   1887: struct authlist AuthList[] = {
                   1888:     { "status",        "Display current status of authentication information",
                   1889:                                                auth_status,    0 },
                   1890:     { "disable", "Disable an authentication type ('auth disable ?' for more)",
                   1891:                                                auth_disable,   1 },
                   1892:     { "enable", "Enable an authentication type ('auth enable ?' for more)",
                   1893:                                                auth_enable,    1 },
                   1894:     { "help",  0,                              auth_help,              0 },
                   1895:     { "?",     "Print help information",       auth_help,              0 },
                   1896:     { 0 },
                   1897: };
                   1898:
                   1899:     static int
                   1900: auth_help()
                   1901: {
                   1902:     struct authlist *c;
                   1903:
                   1904:     for (c = AuthList; c->name; c++) {
                   1905:        if (c->help) {
                   1906:            if (*c->help)
1.10      art      1907:                printf("%-15s %s\r\n", c->name, c->help);
1.1       deraadt  1908:            else
1.10      art      1909:                printf("\r\n");
1.1       deraadt  1910:        }
                   1911:     }
                   1912:     return 0;
                   1913: }
                   1914:
1.15      art      1915:     int
1.1       deraadt  1916: auth_cmd(argc, argv)
                   1917:     int  argc;
                   1918:     char *argv[];
                   1919: {
                   1920:     struct authlist *c;
                   1921:
1.3       niklas   1922:     if (argc < 2) {
                   1923:        fprintf(stderr,
1.10      art      1924:            "Need an argument to 'auth' command.  'auth ?' for help.\r\n");
1.3       niklas   1925:        return 0;
                   1926:     }
                   1927:
1.1       deraadt  1928:     c = (struct authlist *)
                   1929:                genget(argv[1], (char **) AuthList, sizeof(struct authlist));
                   1930:     if (c == 0) {
1.10      art      1931:        fprintf(stderr, "'%s': unknown argument ('auth ?' for help).\r\n",
1.1       deraadt  1932:                                argv[1]);
1.3       niklas   1933:        return 0;
1.1       deraadt  1934:     }
                   1935:     if (Ambiguous(c)) {
1.10      art      1936:        fprintf(stderr, "'%s': ambiguous argument ('auth ?' for help).\r\n",
1.1       deraadt  1937:                                argv[1]);
1.3       niklas   1938:        return 0;
1.1       deraadt  1939:     }
                   1940:     if (c->narg + 2 != argc) {
                   1941:        fprintf(stderr,
1.10      art      1942:            "Need %s%d argument%s to 'auth %s' command.  'auth ?' for help.\r\n",
1.1       deraadt  1943:                c->narg < argc + 2 ? "only " : "",
                   1944:                c->narg, c->narg == 1 ? "" : "s", c->name);
                   1945:        return 0;
                   1946:     }
                   1947:     return((*c->handler)(argv[2], argv[3]));
                   1948: }
                   1949: #endif
                   1950:
1.10      art      1951: #if    defined(ENCRYPTION)
                   1952: /*
                   1953:  * The ENCRYPT command.
                   1954:  */
                   1955:
                   1956: struct encryptlist {
                   1957:        char    *name;
                   1958:        char    *help;
                   1959:        int     (*handler)();
                   1960:        int     needconnect;
                   1961:        int     minarg;
                   1962:        int     maxarg;
                   1963: };
                   1964:
                   1965: static int
                   1966:        EncryptHelp (void);
                   1967:
                   1968: struct encryptlist EncryptList[] = {
                   1969:     { "enable", "Enable encryption. ('encrypt enable ?' for more)",
                   1970:                                                EncryptEnable, 1, 1, 2 },
                   1971:     { "disable", "Disable encryption. ('encrypt enable ?' for more)",
                   1972:                                                EncryptDisable, 0, 1, 2 },
1.20      art      1973:     { "type", "Set encryption type. ('encrypt type ?' for more)",
1.10      art      1974:                                                EncryptType, 0, 1, 1 },
                   1975:     { "start", "Start encryption. ('encrypt start ?' for more)",
                   1976:                                                EncryptStart, 1, 0, 1 },
                   1977:     { "stop", "Stop encryption. ('encrypt stop ?' for more)",
                   1978:                                                EncryptStop, 1, 0, 1 },
                   1979:     { "input", "Start encrypting the input stream",
                   1980:                                                EncryptStartInput, 1, 0, 0 },
                   1981:     { "-input", "Stop encrypting the input stream",
                   1982:                                                EncryptStopInput, 1, 0, 0 },
                   1983:     { "output", "Start encrypting the output stream",
                   1984:                                                EncryptStartOutput, 1, 0, 0 },
                   1985:     { "-output", "Stop encrypting the output stream",
                   1986:                                                EncryptStopOutput, 1, 0, 0 },
                   1987:
                   1988:     { "status",        "Display current status of authentication information",
                   1989:                                                EncryptStatus,  0, 0, 0 },
                   1990:     { "help",  0,                              EncryptHelp,    0, 0, 0 },
                   1991:     { "?",     "Print help information",       EncryptHelp,    0, 0, 0 },
                   1992:     { 0 },
                   1993: };
                   1994:
                   1995: static int
                   1996: EncryptHelp()
                   1997: {
                   1998:     struct encryptlist *c;
                   1999:
                   2000:     for (c = EncryptList; c->name; c++) {
                   2001:        if (c->help) {
                   2002:            if (*c->help)
                   2003:                printf("%-15s %s\r\n", c->name, c->help);
                   2004:            else
                   2005:                printf("\r\n");
                   2006:        }
                   2007:     }
                   2008:     return 0;
                   2009: }
                   2010:
                   2011: static int
                   2012: encrypt_cmd(int argc, char **argv)
                   2013: {
                   2014:     struct encryptlist *c;
1.13      art      2015:
                   2016:     if (argc < 2) {
                   2017:        fprintf(stderr, "Need at least one argument for 'encrypt' command.\n");
                   2018:        fprintf(stderr, "('encrypt ?' for help)\n");
                   2019:        return 0;
                   2020:     }
                   2021:
1.10      art      2022:     c = (struct encryptlist *)
                   2023:                genget(argv[1], (char **) EncryptList, sizeof(struct encryptlist));
                   2024:     if (c == 0) {
                   2025:         fprintf(stderr, "'%s': unknown argument ('encrypt ?' for help).\r\n",
                   2026:                                argv[1]);
                   2027:         return 0;
                   2028:     }
                   2029:     if (Ambiguous(c)) {
                   2030:         fprintf(stderr, "'%s': ambiguous argument ('encrypt ?' for help).\r\n",
                   2031:                                argv[1]);
                   2032:         return 0;
                   2033:     }
                   2034:     argc -= 2;
                   2035:     if (argc < c->minarg || argc > c->maxarg) {
                   2036:        if (c->minarg == c->maxarg) {
                   2037:            fprintf(stderr, "Need %s%d argument%s ",
                   2038:                c->minarg < argc ? "only " : "", c->minarg,
                   2039:                c->minarg == 1 ? "" : "s");
                   2040:        } else {
                   2041:            fprintf(stderr, "Need %s%d-%d arguments ",
                   2042:                c->maxarg < argc ? "only " : "", c->minarg, c->maxarg);
                   2043:        }
                   2044:        fprintf(stderr, "to 'encrypt %s' command.  'encrypt ?' for help.\r\n",
                   2045:                c->name);
                   2046:        return 0;
                   2047:     }
                   2048:     if (c->needconnect && !connected) {
                   2049:        if (!(argc && (isprefix(argv[2], "help") || isprefix(argv[2], "?")))) {
                   2050:            printf("?Need to be connected first.\r\n");
                   2051:            return 0;
                   2052:        }
                   2053:     }
                   2054:     return ((*c->handler)(argc > 0 ? argv[2] : 0,
                   2055:                        argc > 1 ? argv[3] : 0,
                   2056:                        argc > 2 ? argv[4] : 0));
                   2057: }
                   2058: #endif
1.1       deraadt  2059:
                   2060: #if    defined(unix) && defined(TN3270)
                   2061:     static void
                   2062: filestuff(fd)
                   2063:     int fd;
                   2064: {
                   2065:     int res;
                   2066:
                   2067: #ifdef F_GETOWN
                   2068:     setconnmode(0);
                   2069:     res = fcntl(fd, F_GETOWN, 0);
                   2070:     setcommandmode();
                   2071:
                   2072:     if (res == -1) {
                   2073:        perror("fcntl");
                   2074:        return;
                   2075:     }
1.10      art      2076:     printf("\tOwner is %d.\r\n", res);
1.1       deraadt  2077: #endif
                   2078:
                   2079:     setconnmode(0);
                   2080:     res = fcntl(fd, F_GETFL, 0);
                   2081:     setcommandmode();
                   2082:
                   2083:     if (res == -1) {
                   2084:        perror("fcntl");
                   2085:        return;
                   2086:     }
                   2087: #ifdef notdef
1.10      art      2088:     printf("\tFlags are 0x%x: %s\r\n", res, decodeflags(res));
1.1       deraadt  2089: #endif
                   2090: }
                   2091: #endif /* defined(unix) && defined(TN3270) */
                   2092:
                   2093: /*
                   2094:  * Print status about the connection.
                   2095:  */
                   2096:     /*ARGSUSED*/
1.15      art      2097:     static int
1.1       deraadt  2098: status(argc, argv)
                   2099:     int         argc;
                   2100:     char *argv[];
                   2101: {
                   2102:     if (connected) {
1.10      art      2103:        printf("Connected to %s.\r\n", hostname);
1.1       deraadt  2104:        if ((argc < 2) || strcmp(argv[1], "notmuch")) {
                   2105:            int mode = getconnmode();
                   2106:
                   2107:            if (my_want_state_is_will(TELOPT_LINEMODE)) {
1.10      art      2108:                printf("Operating with LINEMODE option\r\n");
                   2109:                printf("%s line editing\r\n", (mode&MODE_EDIT) ? "Local" : "No");
                   2110:                printf("%s catching of signals\r\n",
1.1       deraadt  2111:                                        (mode&MODE_TRAPSIG) ? "Local" : "No");
                   2112:                slcstate();
                   2113: #ifdef KLUDGELINEMODE
                   2114:            } else if (kludgelinemode && my_want_state_is_dont(TELOPT_SGA)) {
1.10      art      2115:                printf("Operating in obsolete linemode\r\n");
1.1       deraadt  2116: #endif
                   2117:            } else {
1.10      art      2118:                printf("Operating in single character mode\r\n");
1.1       deraadt  2119:                if (localchars)
1.10      art      2120:                    printf("Catching signals locally\r\n");
1.1       deraadt  2121:            }
1.10      art      2122:            printf("%s character echo\r\n", (mode&MODE_ECHO) ? "Local" : "Remote");
1.1       deraadt  2123:            if (my_want_state_is_will(TELOPT_LFLOW))
1.10      art      2124:                printf("%s flow control\r\n", (mode&MODE_FLOW) ? "Local" : "No");
                   2125: #if    defined(ENCRYPTION)
                   2126:            encrypt_display();
                   2127: #endif
1.1       deraadt  2128:        }
                   2129:     } else {
1.10      art      2130:        printf("No connection.\r\n");
1.1       deraadt  2131:     }
                   2132: #   if !defined(TN3270)
1.10      art      2133:     printf("Escape character is '%s'.\r\n", control(escape));
1.1       deraadt  2134:     (void) fflush(stdout);
                   2135: #   else /* !defined(TN3270) */
                   2136:     if ((!In3270) && ((argc < 2) || strcmp(argv[1], "notmuch"))) {
1.10      art      2137:        printf("Escape character is '%s'.\r\n", control(escape));
1.1       deraadt  2138:     }
                   2139: #   if defined(unix)
                   2140:     if ((argc >= 2) && !strcmp(argv[1], "everything")) {
1.10      art      2141:        printf("SIGIO received %d time%s.\r\n",
1.1       deraadt  2142:                                sigiocount, (sigiocount == 1)? "":"s");
                   2143:        if (In3270) {
1.42      mpech    2144:            printf("Process ID %ld, process group %ld.\r\n",
                   2145:                                            (long)getpid(), (long)getpgrp());
1.10      art      2146:            printf("Terminal input:\r\n");
1.1       deraadt  2147:            filestuff(tin);
1.10      art      2148:            printf("Terminal output:\r\n");
1.1       deraadt  2149:            filestuff(tout);
1.10      art      2150:            printf("Network socket:\r\n");
1.1       deraadt  2151:            filestuff(net);
                   2152:        }
                   2153:     }
                   2154:     if (In3270 && transcom) {
1.10      art      2155:        printf("Transparent mode command is '%s'.\r\n", transcom);
1.1       deraadt  2156:     }
                   2157: #   endif /* defined(unix) */
                   2158:     (void) fflush(stdout);
                   2159:     if (In3270) {
                   2160:        return 0;
                   2161:     }
                   2162: #   endif /* defined(TN3270) */
1.10      art      2163:     fflush(stdout);
1.1       deraadt  2164:     return 1;
                   2165: }
                   2166:
                   2167: #ifdef SIGINFO
                   2168: /*
                   2169:  * Function that gets called when SIGINFO is received.
                   2170:  */
1.10      art      2171: void
1.1       deraadt  2172: ayt_status()
                   2173: {
                   2174:     (void) call(status, "status", "notmuch", 0);
                   2175: }
                   2176: #endif
                   2177:
1.10      art      2178: static Command *getcmd(char *name);
                   2179:
                   2180: static void
                   2181: cmdrc(char *m1, char *m2)
                   2182: {
                   2183:     static char rcname[128];
                   2184:     Command *c;
                   2185:     FILE *rcfile;
                   2186:     int gotmachine = 0;
                   2187:     int l1 = strlen(m1);
                   2188:     int l2 = strlen(m2);
1.34      aaron    2189:     char m1save[MAXHOSTNAMELEN];
1.10      art      2190:
                   2191:     if (skiprc)
                   2192:        return;
                   2193:
1.34      aaron    2194:     strlcpy(m1save, m1, sizeof(m1save));
1.10      art      2195:     m1 = m1save;
                   2196:
                   2197:     if (rcname[0] == 0) {
                   2198:        char *home = getenv("HOME");
                   2199:
1.29      millert  2200:        if (home == NULL || *home == '\0')
                   2201:            return;
1.10      art      2202:        snprintf (rcname, sizeof(rcname), "%s/.telnetrc",
                   2203:                  home ? home : "");
                   2204:     }
                   2205:
                   2206:     if ((rcfile = fopen(rcname, "r")) == 0) {
                   2207:        return;
                   2208:     }
                   2209:
                   2210:     for (;;) {
                   2211:        if (fgets(line, sizeof(line), rcfile) == NULL)
                   2212:            break;
                   2213:        if (line[0] == 0)
                   2214:            break;
                   2215:        if (line[0] == '#')
                   2216:            continue;
                   2217:        if (gotmachine) {
                   2218:            if (!isspace(line[0]))
                   2219:                gotmachine = 0;
                   2220:        }
                   2221:        if (gotmachine == 0) {
                   2222:            if (isspace(line[0]))
                   2223:                continue;
                   2224:            if (strncasecmp(line, m1, l1) == 0)
                   2225:                strncpy(line, &line[l1], sizeof(line) - l1);
                   2226:            else if (strncasecmp(line, m2, l2) == 0)
                   2227:                strncpy(line, &line[l2], sizeof(line) - l2);
                   2228:            else if (strncasecmp(line, "DEFAULT", 7) == 0)
                   2229:                strncpy(line, &line[7], sizeof(line) - 7);
                   2230:            else
                   2231:                continue;
                   2232:            if (line[0] != ' ' && line[0] != '\t' && line[0] != '\n')
                   2233:                continue;
                   2234:            gotmachine = 1;
                   2235:        }
                   2236:        makeargv();
                   2237:        if (margv[0] == 0)
                   2238:            continue;
                   2239:        c = getcmd(margv[0]);
                   2240:        if (Ambiguous(c)) {
                   2241:            printf("?Ambiguous command: %s\r\n", margv[0]);
                   2242:            continue;
                   2243:        }
                   2244:        if (c == 0) {
                   2245:            printf("?Invalid command: %s\r\n", margv[0]);
                   2246:            continue;
                   2247:        }
                   2248:        /*
                   2249:         * This should never happen...
                   2250:         */
                   2251:        if (c->needconnect && !connected) {
                   2252:            printf("?Need to be connected first for %s.\r\n", margv[0]);
                   2253:            continue;
                   2254:        }
                   2255:        (*c->handler)(margc, margv);
                   2256:     }
                   2257:     fclose(rcfile);
                   2258: }
                   2259:
1.1       deraadt  2260:
                   2261:     int
                   2262: tn(argc, argv)
                   2263:     int argc;
                   2264:     char *argv[];
                   2265: {
1.23      itojun   2266:     struct addrinfo hints, *res, *res0;
                   2267:     int error;
1.1       deraadt  2268:     struct sockaddr_in sin;
                   2269:     unsigned long temp;
                   2270:     extern char *inet_ntoa();
                   2271: #if    defined(IP_OPTIONS) && defined(IPPROTO_IP)
1.10      art      2272:     char *srp = 0;
                   2273:     int srlen;
1.1       deraadt  2274: #endif
1.5       niklas   2275:     char *cmd, *hostp = 0, *portp = 0, *user = 0, *aliasp = 0;
1.31      itojun   2276:     int retry;
                   2277:     const int niflags = NI_NUMERICHOST;
1.1       deraadt  2278:
                   2279:     /* clear the socket address prior to use */
1.3       niklas   2280:     memset((char *)&sin, 0, sizeof(sin));
1.1       deraadt  2281:
                   2282:     if (connected) {
1.10      art      2283:        printf("?Already connected to %s\r\n", hostname);
1.1       deraadt  2284:        return 0;
                   2285:     }
                   2286:     if (argc < 2) {
1.43      hin      2287:        strlcpy(line, "open ", sizeof(line));
1.1       deraadt  2288:        printf("(to) ");
                   2289:        (void) fgets(&line[strlen(line)], sizeof(line) - strlen(line), stdin);
                   2290:        makeargv();
                   2291:        argc = margc;
                   2292:        argv = margv;
                   2293:     }
                   2294:     cmd = *argv;
                   2295:     --argc; ++argv;
                   2296:     while (argc) {
1.3       niklas   2297:        if (strcmp(*argv, "help") == 0 || isprefix(*argv, "?"))
1.1       deraadt  2298:            goto usage;
                   2299:        if (strcmp(*argv, "-l") == 0) {
                   2300:            --argc; ++argv;
                   2301:            if (argc == 0)
                   2302:                goto usage;
1.14      art      2303:            if ((user = strdup(*argv++)) == NULL)
                   2304:                err(1, "strdup");
1.1       deraadt  2305:            --argc;
                   2306:            continue;
                   2307:        }
1.5       niklas   2308:        if (strcmp(*argv, "-b") == 0) {
                   2309:            --argc; ++argv;
                   2310:            if (argc == 0)
                   2311:                goto usage;
                   2312:            aliasp = *argv++;
                   2313:            --argc;
                   2314:            continue;
                   2315:        }
1.1       deraadt  2316:        if (strcmp(*argv, "-a") == 0) {
                   2317:            --argc; ++argv;
                   2318:            autologin = 1;
                   2319:            continue;
                   2320:        }
                   2321:        if (hostp == 0) {
                   2322:            hostp = *argv++;
                   2323:            --argc;
                   2324:            continue;
                   2325:        }
                   2326:        if (portp == 0) {
                   2327:            portp = *argv++;
                   2328:            --argc;
                   2329:            continue;
                   2330:        }
                   2331:     usage:
1.10      art      2332:        printf("usage: %s [-l user] [-a] host-name [port]\r\n", cmd);
1.1       deraadt  2333:        return 0;
                   2334:     }
                   2335:     if (hostp == 0)
                   2336:        goto usage;
                   2337:
                   2338: #if    defined(IP_OPTIONS) && defined(IPPROTO_IP)
                   2339:     if (hostp[0] == '@' || hostp[0] == '!') {
                   2340:        if ((hostname = strrchr(hostp, ':')) == NULL)
                   2341:            hostname = strrchr(hostp, '@');
                   2342:        hostname++;
                   2343:        srp = 0;
                   2344:        temp = sourceroute(hostp, &srp, &srlen);
                   2345:        if (temp == 0) {
                   2346:            herror(srp);
                   2347:            return 0;
                   2348:        } else if (temp == -1) {
1.10      art      2349:            printf("Bad source route option: %s\r\n", hostp);
1.1       deraadt  2350:            return 0;
                   2351:        } else {
1.10      art      2352:            abort();
1.1       deraadt  2353:        }
1.23      itojun   2354:     } else
1.1       deraadt  2355: #endif
1.23      itojun   2356:     {
                   2357:        hostname = hostp;
                   2358:        memset(&hints, 0, sizeof(hints));
1.46      otto     2359:        hints.ai_family = family;
1.23      itojun   2360:        hints.ai_socktype = SOCK_STREAM;
                   2361:        hints.ai_flags = AI_CANONNAME;
                   2362:        if (portp == NULL) {
                   2363:            portp = "telnet";
1.38      itojun   2364:            telnetport = 1;
1.23      itojun   2365:        } else if (*portp == '-') {
                   2366:            portp++;
                   2367:            telnetport = 1;
1.38      itojun   2368:        } else
                   2369:            telnetport = 0;
1.28      millert  2370:        h_errno = 0;
1.23      itojun   2371:        error = getaddrinfo(hostp, portp, &hints, &res0);
                   2372:        if (error) {
1.28      millert  2373:            if (error == EAI_SERVICE)
                   2374:                warnx("%s: bad port", portp);
                   2375:            else
                   2376:                warnx("%s: %s", hostp, gai_strerror(error));
                   2377:            if (h_errno)
                   2378:                herror(hostp);
1.23      itojun   2379:            return 0;
                   2380:        }
1.1       deraadt  2381:     }
1.10      art      2382:
1.23      itojun   2383:     net = -1;
                   2384:     retry = 0;
                   2385:     for (res = res0; res; res = res->ai_next) {
                   2386:        if (1 /* retry */) {
1.31      itojun   2387:            char hbuf[NI_MAXHOST];
1.23      itojun   2388:
1.31      itojun   2389:            if (getnameinfo(res->ai_addr, res->ai_addrlen, hbuf, sizeof(hbuf),
                   2390:                    NULL, 0, niflags) != 0) {
1.43      hin      2391:                strlcpy(hbuf, "(invalid)", sizeof(hbuf));
1.31      itojun   2392:            }
1.23      itojun   2393:            printf("Trying %s...\r\n", hbuf);
                   2394:        }
                   2395:        net = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
                   2396:        if (net < 0)
                   2397:            continue;
1.51      claudio  2398:
1.54    ! phessler 2399:        if (setsockopt(net, SOL_SOCKET, SO_RTABLE, &rtableid,
        !          2400:            sizeof(rtableid)) == -1)
        !          2401:                perror("setsockopt (SO_RTABLE)");
1.10      art      2402:
1.5       niklas   2403:        if (aliasp) {
1.23      itojun   2404:            struct addrinfo ahints, *ares;
                   2405:
                   2406:            memset(&ahints, 0, sizeof(ahints));
1.46      otto     2407:            ahints.ai_family = family;
1.23      itojun   2408:            ahints.ai_socktype = SOCK_STREAM;
                   2409:            ahints.ai_flags = AI_PASSIVE;
                   2410:            error = getaddrinfo(aliasp, "0", &ahints, &ares);
                   2411:            if (error) {
                   2412:                warn("%s: %s", aliasp, gai_strerror(error));
                   2413:                close(net);
1.49      otto     2414:                net = -1;
1.23      itojun   2415:                continue;
1.5       niklas   2416:            }
1.32      itojun   2417:            if (bind(net, ares->ai_addr, ares->ai_addrlen) < 0) {
1.31      itojun   2418:                perror(aliasp);
                   2419:                (void) close(net);   /* dump descriptor */
1.49      otto     2420:                net = -1;
1.23      itojun   2421:                freeaddrinfo(ares);
                   2422:                continue;
1.5       niklas   2423:             }
1.23      itojun   2424:            freeaddrinfo(ares);
                   2425:        }
                   2426: #if    defined(IP_OPTIONS) && defined(IPPROTO_IP)
                   2427:        if (srp && res->ai_family == AF_INET
                   2428:         && setsockopt(net, IPPROTO_IP, IP_OPTIONS, (char *)srp, srlen) < 0)
1.1       deraadt  2429:                perror("setsockopt (IP_OPTIONS)");
                   2430: #endif
                   2431: #if    defined(IPPROTO_IP) && defined(IP_TOS)
1.23      itojun   2432:        if (res->ai_family == AF_INET) {
1.1       deraadt  2433: # if   defined(HAS_GETTOS)
                   2434:            struct tosent *tp;
                   2435:            if (tos < 0 && (tp = gettosbyname("telnet", "tcp")))
                   2436:                tos = tp->t_tos;
                   2437: # endif
                   2438:            if (tos < 0)
1.3       niklas   2439:                tos = IPTOS_LOWDELAY;   /* Low Delay bit */
1.1       deraadt  2440:            if (tos
                   2441:                && (setsockopt(net, IPPROTO_IP, IP_TOS,
1.10      art      2442:                    (void *)&tos, sizeof(int)) < 0)
1.1       deraadt  2443:                && (errno != ENOPROTOOPT))
                   2444:                    perror("telnet: setsockopt (IP_TOS) (ignored)");
                   2445:        }
                   2446: #endif /* defined(IPPROTO_IP) && defined(IP_TOS) */
                   2447:
                   2448:        if (debug && SetSockOpt(net, SOL_SOCKET, SO_DEBUG, 1) < 0) {
                   2449:                perror("setsockopt (SO_DEBUG)");
                   2450:        }
                   2451:
1.23      itojun   2452:        if (connect(net, res->ai_addr, res->ai_addrlen) < 0) {
1.31      itojun   2453:            char hbuf[NI_MAXHOST];
1.16      deraadt  2454:
1.31      itojun   2455:            if (getnameinfo(res->ai_addr, res->ai_addrlen, hbuf, sizeof(hbuf),
                   2456:                    NULL, 0, niflags) != 0) {
1.43      hin      2457:                strlcpy(hbuf, "(invalid)", sizeof(hbuf));
1.31      itojun   2458:            }
1.25      itojun   2459:            fprintf(stderr, "telnet: connect to address %s: %s\n", hbuf,
                   2460:                strerror(errno));
1.23      itojun   2461:
                   2462:            close(net);
                   2463:            net = -1;
                   2464:            retry++;
                   2465:            continue;
1.1       deraadt  2466:        }
1.23      itojun   2467:
1.1       deraadt  2468:        connected++;
1.10      art      2469: #if    defined(AUTHENTICATION) || defined(ENCRYPTION)
1.1       deraadt  2470:        auth_encrypt_connect(connected);
                   2471: #endif /* defined(AUTHENTICATION) */
1.23      itojun   2472:        break;
                   2473:     }
                   2474:     freeaddrinfo(res0);
                   2475:     if (net < 0) {
                   2476:        return 0;
                   2477:     }
1.1       deraadt  2478:     cmdrc(hostp, hostname);
                   2479:     if (autologin && user == NULL) {
                   2480:        struct passwd *pw;
                   2481:
                   2482:        user = getenv("USER");
                   2483:        if (user == NULL ||
1.15      art      2484:            ((pw = getpwnam(user)) && pw->pw_uid != getuid())) {
                   2485:                if ((pw = getpwuid(getuid())) != NULL)
1.1       deraadt  2486:                        user = pw->pw_name;
                   2487:                else
                   2488:                        user = NULL;
                   2489:        }
                   2490:     }
                   2491:     if (user) {
                   2492:        env_define((unsigned char *)"USER", (unsigned char *)user);
                   2493:        env_export((unsigned char *)"USER");
                   2494:     }
                   2495:     (void) call(status, "status", "notmuch", 0);
                   2496:     if (setjmp(peerdied) == 0)
                   2497:        telnet(user);
                   2498:     (void) NetClose(net);
1.10      art      2499:     ExitString("Connection closed by foreign host.\r\n",1);
1.1       deraadt  2500:     /*NOTREACHED*/
1.10      art      2501:     return 0;
1.1       deraadt  2502: }
                   2503:
                   2504: #define HELPINDENT (sizeof ("connect"))
                   2505:
                   2506: static char
                   2507:        openhelp[] =    "connect to a site",
                   2508:        closehelp[] =   "close current connection",
                   2509:        logouthelp[] =  "forcibly logout remote user and close the connection",
                   2510:        quithelp[] =    "exit telnet",
                   2511:        statushelp[] =  "print status information",
                   2512:        helphelp[] =    "print help information",
                   2513:        sendhelp[] =    "transmit special characters ('send ?' for more)",
                   2514:        sethelp[] =     "set operating parameters ('set ?' for more)",
                   2515:        unsethelp[] =   "unset operating parameters ('unset ?' for more)",
                   2516:        togglestring[] ="toggle operating parameters ('toggle ?' for more)",
                   2517:        slchelp[] =     "change state of special charaters ('slc ?' for more)",
                   2518:        displayhelp[] = "display operating parameters",
                   2519: #if    defined(TN3270) && defined(unix)
                   2520:        transcomhelp[] = "specify Unix command for transparent mode pipe",
                   2521: #endif /* defined(TN3270) && defined(unix) */
                   2522: #if    defined(AUTHENTICATION)
                   2523:        authhelp[] =    "turn on (off) authentication ('auth ?' for more)",
                   2524: #endif
1.10      art      2525: #if     defined(ENCRYPTION)
                   2526:         encrypthelp[] = "turn on (off) encryption ('encrypt ?' for more)",
                   2527: #endif
1.1       deraadt  2528:        zhelp[] =       "suspend telnet",
1.21      deraadt  2529: #ifdef SKEY
                   2530:        skeyhelp[] =    "compute response to s/key challenge",
                   2531: #endif
1.1       deraadt  2532:        shellhelp[] =   "invoke a subshell",
                   2533:        envhelp[] =     "change environment variables ('environ ?' for more)",
                   2534:        modestring[] = "try to enter line or character mode ('mode ?' for more)";
                   2535:
1.40      millert  2536: static int     help(int, char**);
1.1       deraadt  2537:
                   2538: static Command cmdtab[] = {
                   2539:        { "close",      closehelp,      bye,            1 },
                   2540:        { "logout",     logouthelp,     logout,         1 },
                   2541:        { "display",    displayhelp,    display,        0 },
                   2542:        { "mode",       modestring,     modecmd,        0 },
                   2543:        { "open",       openhelp,       tn,             0 },
                   2544:        { "quit",       quithelp,       quit,           0 },
                   2545:        { "send",       sendhelp,       sendcmd,        0 },
                   2546:        { "set",        sethelp,        setcmd,         0 },
                   2547:        { "unset",      unsethelp,      unsetcmd,       0 },
                   2548:        { "status",     statushelp,     status,         0 },
                   2549:        { "toggle",     togglestring,   toggle,         0 },
                   2550:        { "slc",        slchelp,        slccmd,         0 },
                   2551: #if    defined(TN3270) && defined(unix)
                   2552:        { "transcom",   transcomhelp,   settranscom,    0 },
                   2553: #endif /* defined(TN3270) && defined(unix) */
                   2554: #if    defined(AUTHENTICATION)
                   2555:        { "auth",       authhelp,       auth_cmd,       0 },
                   2556: #endif
1.10      art      2557: #if    defined(ENCRYPTION)
                   2558:        { "encrypt",    encrypthelp,    encrypt_cmd,    0 },
                   2559: #endif
                   2560:
                   2561:        { "z",          zhelp,          telnetsuspend,  0 },
1.1       deraadt  2562: #if    defined(TN3270)
                   2563:        { "!",          shellhelp,      shell,          1 },
                   2564: #else
                   2565:        { "!",          shellhelp,      shell,          0 },
                   2566: #endif
                   2567:        { "environ",    envhelp,        env_cmd,        0 },
                   2568:        { "?",          helphelp,       help,           0 },
1.6       deraadt  2569: #if    defined(SKEY)
1.21      deraadt  2570:        { "skey",       skeyhelp,       skey_calc,      0 },
1.6       deraadt  2571: #endif
1.10      art      2572:        { 0,            0,              0,              0 }
1.1       deraadt  2573: };
                   2574:
                   2575: static char    crmodhelp[] =   "deprecated command -- use 'toggle crmod' instead";
                   2576: static char    escapehelp[] =  "deprecated command -- use 'set escape' instead";
                   2577:
                   2578: static Command cmdtab2[] = {
                   2579:        { "help",       0,              help,           0 },
                   2580:        { "escape",     escapehelp,     setescape,      0 },
                   2581:        { "crmod",      crmodhelp,      togcrmod,       0 },
1.10      art      2582:        { 0,            0,              0,              0 }
1.1       deraadt  2583: };
                   2584:
                   2585:
                   2586: /*
                   2587:  * Call routine with argc, argv set from args (terminated by 0).
                   2588:  */
                   2589:
                   2590:     /*VARARGS1*/
1.10      art      2591:     static int
                   2592: call(intrtn_t routine, ...)
1.1       deraadt  2593: {
                   2594:     va_list ap;
                   2595:     char *args[100];
                   2596:     int argno = 0;
                   2597:
1.10      art      2598:     va_start(ap, routine);
                   2599:     while ((args[argno++] = va_arg(ap, char *)) != 0);
1.1       deraadt  2600:     va_end(ap);
                   2601:     return (*routine)(argno-1, args);
                   2602: }
                   2603:
                   2604:
                   2605:     static Command *
                   2606: getcmd(name)
                   2607:     char *name;
                   2608: {
                   2609:     Command *cm;
                   2610:
1.10      art      2611:     if ((cm = (Command *) genget(name, (char **) cmdtab, sizeof(Command))))
1.1       deraadt  2612:        return cm;
                   2613:     return (Command *) genget(name, (char **) cmdtab2, sizeof(Command));
                   2614: }
                   2615:
                   2616:     void
                   2617: command(top, tbuf, cnt)
                   2618:     int top;
                   2619:     char *tbuf;
                   2620:     int cnt;
                   2621: {
1.39      mpech    2622:     Command *c;
1.1       deraadt  2623:
                   2624:     setcommandmode();
                   2625:     if (!top) {
                   2626:        putchar('\n');
                   2627: #if    defined(unix)
                   2628:     } else {
                   2629:        (void) signal(SIGINT, SIG_DFL);
                   2630:        (void) signal(SIGQUIT, SIG_DFL);
                   2631: #endif /* defined(unix) */
                   2632:     }
                   2633:     for (;;) {
                   2634:        if (rlogin == _POSIX_VDISABLE)
                   2635:                printf("%s> ", prompt);
                   2636:        if (tbuf) {
1.39      mpech    2637:            char *cp;
1.1       deraadt  2638:            cp = line;
                   2639:            while (cnt > 0 && (*cp++ = *tbuf++) != '\n')
                   2640:                cnt--;
                   2641:            tbuf = 0;
                   2642:            if (cp == line || *--cp != '\n' || cp == line)
                   2643:                goto getline;
                   2644:            *cp = '\0';
                   2645:            if (rlogin == _POSIX_VDISABLE)
1.10      art      2646:                printf("%s\r\n", line);
1.1       deraadt  2647:        } else {
                   2648:        getline:
                   2649:            if (rlogin != _POSIX_VDISABLE)
                   2650:                printf("%s> ", prompt);
                   2651:            if (fgets(line, sizeof(line), stdin) == NULL) {
                   2652:                if (feof(stdin) || ferror(stdin)) {
                   2653:                    (void) quit();
                   2654:                    /*NOTREACHED*/
                   2655:                }
                   2656:                break;
                   2657:            }
                   2658:        }
                   2659:        if (line[0] == 0)
                   2660:            break;
                   2661:        makeargv();
                   2662:        if (margv[0] == 0) {
                   2663:            break;
                   2664:        }
                   2665:        c = getcmd(margv[0]);
                   2666:        if (Ambiguous(c)) {
1.10      art      2667:            printf("?Ambiguous command\r\n");
1.1       deraadt  2668:            continue;
                   2669:        }
                   2670:        if (c == 0) {
1.10      art      2671:            printf("?Invalid command\r\n");
1.1       deraadt  2672:            continue;
                   2673:        }
                   2674:        if (c->needconnect && !connected) {
1.10      art      2675:            printf("?Need to be connected first.\r\n");
1.1       deraadt  2676:            continue;
                   2677:        }
                   2678:        if ((*c->handler)(margc, margv)) {
                   2679:            break;
                   2680:        }
                   2681:     }
                   2682:     if (!top) {
                   2683:        if (!connected) {
                   2684:            longjmp(toplevel, 1);
                   2685:            /*NOTREACHED*/
                   2686:        }
                   2687: #if    defined(TN3270)
                   2688:        if (shell_active == 0) {
                   2689:            setconnmode(0);
                   2690:        }
                   2691: #else  /* defined(TN3270) */
                   2692:        setconnmode(0);
                   2693: #endif /* defined(TN3270) */
                   2694:     }
                   2695: }
                   2696: 
                   2697: /*
                   2698:  * Help command.
                   2699:  */
1.15      art      2700:        static int
1.1       deraadt  2701: help(argc, argv)
                   2702:        int argc;
                   2703:        char *argv[];
                   2704: {
1.39      mpech    2705:        Command *c;
1.1       deraadt  2706:
                   2707:        if (argc == 1) {
1.10      art      2708:                printf("Commands may be abbreviated.  Commands are:\r\n\r\n");
1.1       deraadt  2709:                for (c = cmdtab; c->name; c++)
                   2710:                        if (c->help) {
1.37      deraadt  2711:                                printf("%-*s\t%s\r\n", (int)HELPINDENT, c->name,
1.1       deraadt  2712:                                                                    c->help);
                   2713:                        }
                   2714:                return 0;
                   2715:        }
                   2716:        while (--argc > 0) {
1.39      mpech    2717:                char *arg;
1.1       deraadt  2718:                arg = *++argv;
                   2719:                c = getcmd(arg);
                   2720:                if (Ambiguous(c))
1.10      art      2721:                        printf("?Ambiguous help command %s\r\n", arg);
1.1       deraadt  2722:                else if (c == (Command *)0)
1.10      art      2723:                        printf("?Invalid help command %s\r\n", arg);
1.1       deraadt  2724:                else
1.10      art      2725:                        printf("%s\r\n", c->help);
1.1       deraadt  2726:        }
                   2727:        return 0;
                   2728: }
                   2729:
                   2730: #if    defined(IP_OPTIONS) && defined(IPPROTO_IP)
                   2731:
                   2732: /*
                   2733:  * Source route is handed in as
                   2734:  *     [!]@hop1@hop2...[@|:]dst
                   2735:  * If the leading ! is present, it is a
                   2736:  * strict source route, otherwise it is
                   2737:  * assmed to be a loose source route.
                   2738:  *
                   2739:  * We fill in the source route option as
                   2740:  *     hop1,hop2,hop3...dest
                   2741:  * and return a pointer to hop1, which will
                   2742:  * be the address to connect() to.
                   2743:  *
                   2744:  * Arguments:
                   2745:  *     arg:    pointer to route list to decipher
                   2746:  *
                   2747:  *     cpp:    If *cpp is not equal to NULL, this is a
                   2748:  *             pointer to a pointer to a character array
                   2749:  *             that should be filled in with the option.
                   2750:  *
                   2751:  *     lenp:   pointer to an integer that contains the
                   2752:  *             length of *cpp if *cpp != NULL.
                   2753:  *
                   2754:  * Return values:
                   2755:  *
                   2756:  *     Returns the address of the host to connect to.  If the
                   2757:  *     return value is -1, there was a syntax error in the
                   2758:  *     option, either unknown characters, or too many hosts.
                   2759:  *     If the return value is 0, one of the hostnames in the
                   2760:  *     path is unknown, and *cpp is set to point to the bad
                   2761:  *     hostname.
                   2762:  *
                   2763:  *     *cpp:   If *cpp was equal to NULL, it will be filled
                   2764:  *             in with a pointer to our static area that has
                   2765:  *             the option filled in.  This will be 32bit aligned.
1.3       niklas   2766:  *
1.1       deraadt  2767:  *     *lenp:  This will be filled in with how long the option
                   2768:  *             pointed to by *cpp is.
1.3       niklas   2769:  *
1.1       deraadt  2770:  */
                   2771:        unsigned long
                   2772: sourceroute(arg, cpp, lenp)
                   2773:        char    *arg;
                   2774:        char    **cpp;
                   2775:        int     *lenp;
                   2776: {
                   2777:        static char lsr[44];
                   2778: #ifdef sysV88
                   2779:        static IOPTN ipopt;
                   2780: #endif
                   2781:        char *cp, *cp2, *lsrp, *lsrep;
1.39      mpech    2782:        int tmp;
1.1       deraadt  2783:        struct in_addr sin_addr;
1.39      mpech    2784:        struct hostent *host = 0;
                   2785:        char c;
1.1       deraadt  2786:
                   2787:        /*
                   2788:         * Verify the arguments, and make sure we have
                   2789:         * at least 7 bytes for the option.
                   2790:         */
                   2791:        if (cpp == NULL || lenp == NULL)
                   2792:                return((unsigned long)-1);
                   2793:        if (*cpp != NULL && *lenp < 7)
                   2794:                return((unsigned long)-1);
                   2795:        /*
                   2796:         * Decide whether we have a buffer passed to us,
                   2797:         * or if we need to use our own static buffer.
                   2798:         */
                   2799:        if (*cpp) {
                   2800:                lsrp = *cpp;
                   2801:                lsrep = lsrp + *lenp;
                   2802:        } else {
                   2803:                *cpp = lsrp = lsr;
                   2804:                lsrep = lsrp + 44;
                   2805:        }
                   2806:
                   2807:        cp = arg;
                   2808:
                   2809:        /*
                   2810:         * Next, decide whether we have a loose source
                   2811:         * route or a strict source route, and fill in
                   2812:         * the begining of the option.
                   2813:         */
                   2814: #ifndef        sysV88
                   2815:        if (*cp == '!') {
                   2816:                cp++;
                   2817:                *lsrp++ = IPOPT_SSRR;
                   2818:        } else
                   2819:                *lsrp++ = IPOPT_LSRR;
                   2820: #else
                   2821:        if (*cp == '!') {
                   2822:                cp++;
                   2823:                ipopt.io_type = IPOPT_SSRR;
                   2824:        } else
                   2825:                ipopt.io_type = IPOPT_LSRR;
                   2826: #endif
                   2827:
                   2828:        if (*cp != '@')
                   2829:                return((unsigned long)-1);
                   2830:
                   2831: #ifndef        sysV88
                   2832:        lsrp++;         /* skip over length, we'll fill it in later */
                   2833:        *lsrp++ = 4;
                   2834: #endif
                   2835:
                   2836:        cp++;
                   2837:
                   2838:        sin_addr.s_addr = 0;
                   2839:
                   2840:        for (c = 0;;) {
                   2841:                if (c == ':')
                   2842:                        cp2 = 0;
1.10      art      2843:                else for (cp2 = cp; (c = *cp2); cp2++) {
1.1       deraadt  2844:                        if (c == ',') {
                   2845:                                *cp2++ = '\0';
                   2846:                                if (*cp2 == '@')
                   2847:                                        cp2++;
                   2848:                        } else if (c == '@') {
                   2849:                                *cp2++ = '\0';
                   2850:                        } else if (c == ':') {
                   2851:                                *cp2++ = '\0';
                   2852:                        } else
                   2853:                                continue;
                   2854:                        break;
                   2855:                }
                   2856:                if (!c)
                   2857:                        cp2 = 0;
                   2858:
1.10      art      2859:                if ((tmp = inet_addr(cp)) != -1) {
1.1       deraadt  2860:                        sin_addr.s_addr = tmp;
1.10      art      2861:                } else if ((host = gethostbyname(cp))) {
1.1       deraadt  2862: #if    defined(h_addr)
1.3       niklas   2863:                        memmove((caddr_t)&sin_addr,
1.10      art      2864:                                host->h_addr_list[0],
                   2865:                                sizeof(sin_addr));
1.1       deraadt  2866: #else
1.10      art      2867:                        memmove((caddr_t)&sin_addr, host->h_addr,
                   2868:                                sizeof(sin_addr));
1.1       deraadt  2869: #endif
                   2870:                } else {
                   2871:                        *cpp = cp;
                   2872:                        return(0);
                   2873:                }
1.3       niklas   2874:                memmove(lsrp, (char *)&sin_addr, 4);
1.1       deraadt  2875:                lsrp += 4;
                   2876:                if (cp2)
                   2877:                        cp = cp2;
                   2878:                else
                   2879:                        break;
                   2880:                /*
                   2881:                 * Check to make sure there is space for next address
                   2882:                 */
                   2883:                if (lsrp + 4 > lsrep)
                   2884:                        return((unsigned long)-1);
                   2885:        }
                   2886: #ifndef        sysV88
                   2887:        if ((*(*cpp+IPOPT_OLEN) = lsrp - *cpp) <= 7) {
                   2888:                *cpp = 0;
                   2889:                *lenp = 0;
                   2890:                return((unsigned long)-1);
                   2891:        }
                   2892:        *lsrp++ = IPOPT_NOP; /* 32 bit word align it */
                   2893:        *lenp = lsrp - *cpp;
                   2894: #else
                   2895:        ipopt.io_len = lsrp - *cpp;
                   2896:        if (ipopt.io_len <= 5) {                /* Is 3 better ? */
                   2897:                *cpp = 0;
                   2898:                *lenp = 0;
                   2899:                return((unsigned long)-1);
                   2900:        }
                   2901:        *lenp = sizeof(ipopt);
                   2902:        *cpp = (char *) &ipopt;
                   2903: #endif
                   2904:        return(sin_addr.s_addr);
                   2905: }
                   2906: #endif