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

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