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

1.14    ! art         1: /*     $OpenBSD: commands.c,v 1.13 1998/04/07 20:01:06 art 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);
1.14    ! art      1683:                if (cp == NULL)
        !          1684:                        err(1, "asprintf");
1.10      art      1685:
1.1       deraadt  1686:                free(ep->value);
                   1687:                ep->value = (unsigned char *)cp;
                   1688:        }
                   1689:        /*
                   1690:         * If USER is not defined, but LOGNAME is, then add
                   1691:         * USER with the value from LOGNAME.  By default, we
                   1692:         * don't export the USER variable.
                   1693:         */
                   1694:        if ((env_find("USER") == NULL) && (ep = env_find("LOGNAME"))) {
                   1695:                env_define((unsigned char *)"USER", ep->value);
                   1696:                env_unexport((unsigned char *)"USER");
                   1697:        }
                   1698:        env_export((unsigned char *)"DISPLAY");
                   1699:        env_export((unsigned char *)"PRINTER");
1.10      art      1700:        env_export((unsigned char *)"XAUTHORITY");
1.1       deraadt  1701: }
                   1702:
                   1703:        struct env_lst *
                   1704: env_define(var, value)
                   1705:        unsigned char *var, *value;
                   1706: {
                   1707:        register struct env_lst *ep;
                   1708:
1.10      art      1709:        if ((ep = env_find(var))) {
1.1       deraadt  1710:                if (ep->var)
                   1711:                        free(ep->var);
                   1712:                if (ep->value)
                   1713:                        free(ep->value);
                   1714:        } else {
1.14    ! art      1715:                if ((ep = malloc(sizeof(struct env_lst))) == NULL)
        !          1716:                        err(1, "malloc");
1.1       deraadt  1717:                ep->next = envlisthead.next;
                   1718:                envlisthead.next = ep;
                   1719:                ep->prev = &envlisthead;
                   1720:                if (ep->next)
                   1721:                        ep->next->prev = ep;
                   1722:        }
1.10      art      1723:        ep->welldefined = opt_welldefined((char *)var);
1.1       deraadt  1724:        ep->export = 1;
1.14    ! art      1725:        if ((ep->var = strdup((char *)var)) == NULL)
        !          1726:                err(1, "strdup");
        !          1727:        if ((ep->value = strdup((char *)value)) == NULL)
        !          1728:                err(1, "strdup");
1.1       deraadt  1729:        return(ep);
                   1730: }
                   1731:
                   1732:        void
                   1733: env_undefine(var)
                   1734:        unsigned char *var;
                   1735: {
                   1736:        register struct env_lst *ep;
                   1737:
1.10      art      1738:        if ((ep = env_find(var))) {
1.1       deraadt  1739:                ep->prev->next = ep->next;
                   1740:                if (ep->next)
                   1741:                        ep->next->prev = ep->prev;
                   1742:                if (ep->var)
                   1743:                        free(ep->var);
                   1744:                if (ep->value)
                   1745:                        free(ep->value);
                   1746:                free(ep);
                   1747:        }
                   1748: }
                   1749:
                   1750:        void
                   1751: env_export(var)
                   1752:        unsigned char *var;
                   1753: {
                   1754:        register struct env_lst *ep;
                   1755:
1.10      art      1756:        if ((ep = env_find(var)))
1.1       deraadt  1757:                ep->export = 1;
                   1758: }
                   1759:
                   1760:        void
                   1761: env_unexport(var)
                   1762:        unsigned char *var;
                   1763: {
                   1764:        register struct env_lst *ep;
                   1765:
                   1766:        if (ep = env_find(var))
                   1767:                ep->export = 0;
                   1768: }
                   1769:
                   1770:        void
                   1771: env_send(var)
                   1772:        unsigned char *var;
                   1773: {
                   1774:        register struct env_lst *ep;
                   1775:
1.3       niklas   1776:        if (my_state_is_wont(TELOPT_NEW_ENVIRON)
1.1       deraadt  1777: #ifdef OLD_ENVIRON
                   1778:            && my_state_is_wont(TELOPT_OLD_ENVIRON)
                   1779: #endif
                   1780:                ) {
                   1781:                fprintf(stderr,
1.10      art      1782:                    "Cannot send '%s': Telnet ENVIRON option not enabled\r\n",
1.1       deraadt  1783:                                                                        var);
                   1784:                return;
                   1785:        }
                   1786:        ep = env_find(var);
                   1787:        if (ep == 0) {
1.10      art      1788:                fprintf(stderr, "Cannot send '%s': variable not defined\r\n",
1.1       deraadt  1789:                                                                        var);
                   1790:                return;
                   1791:        }
                   1792:        env_opt_start_info();
                   1793:        env_opt_add(ep->var);
                   1794:        env_opt_end(0);
                   1795: }
                   1796:
                   1797:        void
                   1798: env_list()
                   1799: {
                   1800:        register struct env_lst *ep;
                   1801:
                   1802:        for (ep = envlisthead.next; ep; ep = ep->next) {
1.10      art      1803:                printf("%c %-20s %s\r\n", ep->export ? '*' : ' ',
1.1       deraadt  1804:                                        ep->var, ep->value);
                   1805:        }
                   1806: }
                   1807:
                   1808:        unsigned char *
                   1809: env_default(init, welldefined)
                   1810:        int init;
                   1811: {
                   1812:        static struct env_lst *nep = NULL;
                   1813:
                   1814:        if (init) {
                   1815:                nep = &envlisthead;
1.10      art      1816:                return NULL;
1.1       deraadt  1817:        }
                   1818:        if (nep) {
1.10      art      1819:                while ((nep = nep->next)) {
1.1       deraadt  1820:                        if (nep->export && (nep->welldefined == welldefined))
                   1821:                                return(nep->var);
                   1822:                }
                   1823:        }
                   1824:        return(NULL);
                   1825: }
                   1826:
                   1827:        unsigned char *
                   1828: env_getvalue(var)
                   1829:        unsigned char *var;
                   1830: {
                   1831:        register struct env_lst *ep;
                   1832:
1.10      art      1833:        if ((ep = env_find(var)))
1.1       deraadt  1834:                return(ep->value);
                   1835:        return(NULL);
                   1836: }
                   1837:
                   1838: #if defined(OLD_ENVIRON) && defined(ENV_HACK)
                   1839:        void
                   1840: env_varval(what)
                   1841:        unsigned char *what;
                   1842: {
                   1843:        extern int old_env_var, old_env_value, env_auto;
                   1844:        int len = strlen((char *)what);
                   1845:
                   1846:        if (len == 0)
                   1847:                goto unknown;
                   1848:
                   1849:        if (strncasecmp((char *)what, "status", len) == 0) {
                   1850:                if (env_auto)
                   1851:                        printf("%s%s", "VAR and VALUE are/will be ",
1.10      art      1852:                                        "determined automatically\r\n");
1.1       deraadt  1853:                if (old_env_var == OLD_ENV_VAR)
1.10      art      1854:                        printf("VAR and VALUE set to correct definitions\r\n");
1.1       deraadt  1855:                else
1.10      art      1856:                        printf("VAR and VALUE definitions are reversed\r\n");
1.1       deraadt  1857:        } else if (strncasecmp((char *)what, "auto", len) == 0) {
                   1858:                env_auto = 1;
                   1859:                old_env_var = OLD_ENV_VALUE;
                   1860:                old_env_value = OLD_ENV_VAR;
                   1861:        } else if (strncasecmp((char *)what, "right", len) == 0) {
                   1862:                env_auto = 0;
                   1863:                old_env_var = OLD_ENV_VAR;
                   1864:                old_env_value = OLD_ENV_VALUE;
                   1865:        } else if (strncasecmp((char *)what, "wrong", len) == 0) {
                   1866:                env_auto = 0;
                   1867:                old_env_var = OLD_ENV_VALUE;
                   1868:                old_env_value = OLD_ENV_VAR;
                   1869:        } else {
                   1870: unknown:
1.10      art      1871:                printf("Unknown \"varval\" command. (\"auto\", \"right\", \"wrong\", \"status\")\r\n");
1.1       deraadt  1872:        }
                   1873: }
                   1874: #endif
                   1875:
                   1876: #if    defined(AUTHENTICATION)
                   1877: /*
                   1878:  * The AUTHENTICATE command.
                   1879:  */
                   1880:
                   1881: struct authlist {
                   1882:        char    *name;
                   1883:        char    *help;
                   1884:        int     (*handler)();
                   1885:        int     narg;
                   1886: };
                   1887:
                   1888: static int
                   1889:        auth_help P((void));
                   1890:
                   1891: struct authlist AuthList[] = {
                   1892:     { "status",        "Display current status of authentication information",
                   1893:                                                auth_status,    0 },
                   1894:     { "disable", "Disable an authentication type ('auth disable ?' for more)",
                   1895:                                                auth_disable,   1 },
                   1896:     { "enable", "Enable an authentication type ('auth enable ?' for more)",
                   1897:                                                auth_enable,    1 },
                   1898:     { "help",  0,                              auth_help,              0 },
                   1899:     { "?",     "Print help information",       auth_help,              0 },
                   1900:     { 0 },
                   1901: };
                   1902:
                   1903:     static int
                   1904: auth_help()
                   1905: {
                   1906:     struct authlist *c;
                   1907:
                   1908:     for (c = AuthList; c->name; c++) {
                   1909:        if (c->help) {
                   1910:            if (*c->help)
1.10      art      1911:                printf("%-15s %s\r\n", c->name, c->help);
1.1       deraadt  1912:            else
1.10      art      1913:                printf("\r\n");
1.1       deraadt  1914:        }
                   1915:     }
                   1916:     return 0;
                   1917: }
                   1918:
                   1919: auth_cmd(argc, argv)
                   1920:     int  argc;
                   1921:     char *argv[];
                   1922: {
                   1923:     struct authlist *c;
                   1924:
1.3       niklas   1925:     if (argc < 2) {
                   1926:        fprintf(stderr,
1.10      art      1927:            "Need an argument to 'auth' command.  'auth ?' for help.\r\n");
1.3       niklas   1928:        return 0;
                   1929:     }
                   1930:
1.1       deraadt  1931:     c = (struct authlist *)
                   1932:                genget(argv[1], (char **) AuthList, sizeof(struct authlist));
                   1933:     if (c == 0) {
1.10      art      1934:        fprintf(stderr, "'%s': unknown argument ('auth ?' for help).\r\n",
1.1       deraadt  1935:                                argv[1]);
1.3       niklas   1936:        return 0;
1.1       deraadt  1937:     }
                   1938:     if (Ambiguous(c)) {
1.10      art      1939:        fprintf(stderr, "'%s': ambiguous argument ('auth ?' for help).\r\n",
1.1       deraadt  1940:                                argv[1]);
1.3       niklas   1941:        return 0;
1.1       deraadt  1942:     }
                   1943:     if (c->narg + 2 != argc) {
                   1944:        fprintf(stderr,
1.10      art      1945:            "Need %s%d argument%s to 'auth %s' command.  'auth ?' for help.\r\n",
1.1       deraadt  1946:                c->narg < argc + 2 ? "only " : "",
                   1947:                c->narg, c->narg == 1 ? "" : "s", c->name);
                   1948:        return 0;
                   1949:     }
                   1950:     return((*c->handler)(argv[2], argv[3]));
                   1951: }
                   1952: #endif
                   1953:
1.10      art      1954: #if    defined(ENCRYPTION)
                   1955: /*
                   1956:  * The ENCRYPT command.
                   1957:  */
                   1958:
                   1959: struct encryptlist {
                   1960:        char    *name;
                   1961:        char    *help;
                   1962:        int     (*handler)();
                   1963:        int     needconnect;
                   1964:        int     minarg;
                   1965:        int     maxarg;
                   1966: };
                   1967:
                   1968: static int
                   1969:        EncryptHelp (void);
                   1970:
                   1971: struct encryptlist EncryptList[] = {
                   1972:     { "enable", "Enable encryption. ('encrypt enable ?' for more)",
                   1973:                                                EncryptEnable, 1, 1, 2 },
                   1974:     { "disable", "Disable encryption. ('encrypt enable ?' for more)",
                   1975:                                                EncryptDisable, 0, 1, 2 },
                   1976:     { "type", "Set encryptiong type. ('encrypt type ?' for more)",
                   1977:                                                EncryptType, 0, 1, 1 },
                   1978:     { "start", "Start encryption. ('encrypt start ?' for more)",
                   1979:                                                EncryptStart, 1, 0, 1 },
                   1980:     { "stop", "Stop encryption. ('encrypt stop ?' for more)",
                   1981:                                                EncryptStop, 1, 0, 1 },
                   1982:     { "input", "Start encrypting the input stream",
                   1983:                                                EncryptStartInput, 1, 0, 0 },
                   1984:     { "-input", "Stop encrypting the input stream",
                   1985:                                                EncryptStopInput, 1, 0, 0 },
                   1986:     { "output", "Start encrypting the output stream",
                   1987:                                                EncryptStartOutput, 1, 0, 0 },
                   1988:     { "-output", "Stop encrypting the output stream",
                   1989:                                                EncryptStopOutput, 1, 0, 0 },
                   1990:
                   1991:     { "status",        "Display current status of authentication information",
                   1992:                                                EncryptStatus,  0, 0, 0 },
                   1993:     { "help",  0,                              EncryptHelp,    0, 0, 0 },
                   1994:     { "?",     "Print help information",       EncryptHelp,    0, 0, 0 },
                   1995:     { 0 },
                   1996: };
                   1997:
                   1998: static int
                   1999: EncryptHelp()
                   2000: {
                   2001:     struct encryptlist *c;
                   2002:
                   2003:     for (c = EncryptList; c->name; c++) {
                   2004:        if (c->help) {
                   2005:            if (*c->help)
                   2006:                printf("%-15s %s\r\n", c->name, c->help);
                   2007:            else
                   2008:                printf("\r\n");
                   2009:        }
                   2010:     }
                   2011:     return 0;
                   2012: }
                   2013:
                   2014: static int
                   2015: encrypt_cmd(int argc, char **argv)
                   2016: {
                   2017:     struct encryptlist *c;
1.13      art      2018:
                   2019:     if (argc < 2) {
                   2020:        fprintf(stderr, "Need at least one argument for 'encrypt' command.\n");
                   2021:        fprintf(stderr, "('encrypt ?' for help)\n");
                   2022:        return 0;
                   2023:     }
                   2024:
1.10      art      2025:     c = (struct encryptlist *)
                   2026:                genget(argv[1], (char **) EncryptList, sizeof(struct encryptlist));
                   2027:     if (c == 0) {
                   2028:         fprintf(stderr, "'%s': unknown argument ('encrypt ?' for help).\r\n",
                   2029:                                argv[1]);
                   2030:         return 0;
                   2031:     }
                   2032:     if (Ambiguous(c)) {
                   2033:         fprintf(stderr, "'%s': ambiguous argument ('encrypt ?' for help).\r\n",
                   2034:                                argv[1]);
                   2035:         return 0;
                   2036:     }
                   2037:     argc -= 2;
                   2038:     if (argc < c->minarg || argc > c->maxarg) {
                   2039:        if (c->minarg == c->maxarg) {
                   2040:            fprintf(stderr, "Need %s%d argument%s ",
                   2041:                c->minarg < argc ? "only " : "", c->minarg,
                   2042:                c->minarg == 1 ? "" : "s");
                   2043:        } else {
                   2044:            fprintf(stderr, "Need %s%d-%d arguments ",
                   2045:                c->maxarg < argc ? "only " : "", c->minarg, c->maxarg);
                   2046:        }
                   2047:        fprintf(stderr, "to 'encrypt %s' command.  'encrypt ?' for help.\r\n",
                   2048:                c->name);
                   2049:        return 0;
                   2050:     }
                   2051:     if (c->needconnect && !connected) {
                   2052:        if (!(argc && (isprefix(argv[2], "help") || isprefix(argv[2], "?")))) {
                   2053:            printf("?Need to be connected first.\r\n");
                   2054:            return 0;
                   2055:        }
                   2056:     }
                   2057:     return ((*c->handler)(argc > 0 ? argv[2] : 0,
                   2058:                        argc > 1 ? argv[3] : 0,
                   2059:                        argc > 2 ? argv[4] : 0));
                   2060: }
                   2061: #endif
1.1       deraadt  2062:
                   2063: #if    defined(unix) && defined(TN3270)
                   2064:     static void
                   2065: filestuff(fd)
                   2066:     int fd;
                   2067: {
                   2068:     int res;
                   2069:
                   2070: #ifdef F_GETOWN
                   2071:     setconnmode(0);
                   2072:     res = fcntl(fd, F_GETOWN, 0);
                   2073:     setcommandmode();
                   2074:
                   2075:     if (res == -1) {
                   2076:        perror("fcntl");
                   2077:        return;
                   2078:     }
1.10      art      2079:     printf("\tOwner is %d.\r\n", res);
1.1       deraadt  2080: #endif
                   2081:
                   2082:     setconnmode(0);
                   2083:     res = fcntl(fd, F_GETFL, 0);
                   2084:     setcommandmode();
                   2085:
                   2086:     if (res == -1) {
                   2087:        perror("fcntl");
                   2088:        return;
                   2089:     }
                   2090: #ifdef notdef
1.10      art      2091:     printf("\tFlags are 0x%x: %s\r\n", res, decodeflags(res));
1.1       deraadt  2092: #endif
                   2093: }
                   2094: #endif /* defined(unix) && defined(TN3270) */
                   2095:
                   2096: /*
                   2097:  * Print status about the connection.
                   2098:  */
                   2099:     /*ARGSUSED*/
                   2100:     static
                   2101: status(argc, argv)
                   2102:     int         argc;
                   2103:     char *argv[];
                   2104: {
                   2105:     if (connected) {
1.10      art      2106:        printf("Connected to %s.\r\n", hostname);
1.1       deraadt  2107:        if ((argc < 2) || strcmp(argv[1], "notmuch")) {
                   2108:            int mode = getconnmode();
                   2109:
                   2110:            if (my_want_state_is_will(TELOPT_LINEMODE)) {
1.10      art      2111:                printf("Operating with LINEMODE option\r\n");
                   2112:                printf("%s line editing\r\n", (mode&MODE_EDIT) ? "Local" : "No");
                   2113:                printf("%s catching of signals\r\n",
1.1       deraadt  2114:                                        (mode&MODE_TRAPSIG) ? "Local" : "No");
                   2115:                slcstate();
                   2116: #ifdef KLUDGELINEMODE
                   2117:            } else if (kludgelinemode && my_want_state_is_dont(TELOPT_SGA)) {
1.10      art      2118:                printf("Operating in obsolete linemode\r\n");
1.1       deraadt  2119: #endif
                   2120:            } else {
1.10      art      2121:                printf("Operating in single character mode\r\n");
1.1       deraadt  2122:                if (localchars)
1.10      art      2123:                    printf("Catching signals locally\r\n");
1.1       deraadt  2124:            }
1.10      art      2125:            printf("%s character echo\r\n", (mode&MODE_ECHO) ? "Local" : "Remote");
1.1       deraadt  2126:            if (my_want_state_is_will(TELOPT_LFLOW))
1.10      art      2127:                printf("%s flow control\r\n", (mode&MODE_FLOW) ? "Local" : "No");
                   2128: #if    defined(ENCRYPTION)
                   2129:            encrypt_display();
                   2130: #endif
1.1       deraadt  2131:        }
                   2132:     } else {
1.10      art      2133:        printf("No connection.\r\n");
1.1       deraadt  2134:     }
                   2135: #   if !defined(TN3270)
1.10      art      2136:     printf("Escape character is '%s'.\r\n", control(escape));
1.1       deraadt  2137:     (void) fflush(stdout);
                   2138: #   else /* !defined(TN3270) */
                   2139:     if ((!In3270) && ((argc < 2) || strcmp(argv[1], "notmuch"))) {
1.10      art      2140:        printf("Escape character is '%s'.\r\n", control(escape));
1.1       deraadt  2141:     }
                   2142: #   if defined(unix)
                   2143:     if ((argc >= 2) && !strcmp(argv[1], "everything")) {
1.10      art      2144:        printf("SIGIO received %d time%s.\r\n",
1.1       deraadt  2145:                                sigiocount, (sigiocount == 1)? "":"s");
                   2146:        if (In3270) {
1.10      art      2147:            printf("Process ID %d, process group %d.\r\n",
1.11      deraadt  2148:                                            getpid(), getpgrp());
1.10      art      2149:            printf("Terminal input:\r\n");
1.1       deraadt  2150:            filestuff(tin);
1.10      art      2151:            printf("Terminal output:\r\n");
1.1       deraadt  2152:            filestuff(tout);
1.10      art      2153:            printf("Network socket:\r\n");
1.1       deraadt  2154:            filestuff(net);
                   2155:        }
                   2156:     }
                   2157:     if (In3270 && transcom) {
1.10      art      2158:        printf("Transparent mode command is '%s'.\r\n", transcom);
1.1       deraadt  2159:     }
                   2160: #   endif /* defined(unix) */
                   2161:     (void) fflush(stdout);
                   2162:     if (In3270) {
                   2163:        return 0;
                   2164:     }
                   2165: #   endif /* defined(TN3270) */
1.10      art      2166:     fflush(stdout);
1.1       deraadt  2167:     return 1;
                   2168: }
                   2169:
                   2170: #ifdef SIGINFO
                   2171: /*
                   2172:  * Function that gets called when SIGINFO is received.
                   2173:  */
1.10      art      2174: void
1.1       deraadt  2175: ayt_status()
                   2176: {
                   2177:     (void) call(status, "status", "notmuch", 0);
                   2178: }
                   2179: #endif
                   2180:
1.10      art      2181: static Command *getcmd(char *name);
                   2182:
                   2183: static void
                   2184: cmdrc(char *m1, char *m2)
                   2185: {
                   2186:     static char rcname[128];
                   2187:     Command *c;
                   2188:     FILE *rcfile;
                   2189:     int gotmachine = 0;
                   2190:     int l1 = strlen(m1);
                   2191:     int l2 = strlen(m2);
                   2192:     char m1save[64];
                   2193:
                   2194:     if (skiprc)
                   2195:        return;
                   2196:
                   2197:     strcpy(m1save, m1);
                   2198:     m1 = m1save;
                   2199:
                   2200:     if (rcname[0] == 0) {
                   2201:        char *home = getenv("HOME");
                   2202:
                   2203:        snprintf (rcname, sizeof(rcname), "%s/.telnetrc",
                   2204:                  home ? home : "");
                   2205:     }
                   2206:
                   2207:     if ((rcfile = fopen(rcname, "r")) == 0) {
                   2208:        return;
                   2209:     }
                   2210:
                   2211:     for (;;) {
                   2212:        if (fgets(line, sizeof(line), rcfile) == NULL)
                   2213:            break;
                   2214:        if (line[0] == 0)
                   2215:            break;
                   2216:        if (line[0] == '#')
                   2217:            continue;
                   2218:        if (gotmachine) {
                   2219:            if (!isspace(line[0]))
                   2220:                gotmachine = 0;
                   2221:        }
                   2222:        if (gotmachine == 0) {
                   2223:            if (isspace(line[0]))
                   2224:                continue;
                   2225:            if (strncasecmp(line, m1, l1) == 0)
                   2226:                strncpy(line, &line[l1], sizeof(line) - l1);
                   2227:            else if (strncasecmp(line, m2, l2) == 0)
                   2228:                strncpy(line, &line[l2], sizeof(line) - l2);
                   2229:            else if (strncasecmp(line, "DEFAULT", 7) == 0)
                   2230:                strncpy(line, &line[7], sizeof(line) - 7);
                   2231:            else
                   2232:                continue;
                   2233:            if (line[0] != ' ' && line[0] != '\t' && line[0] != '\n')
                   2234:                continue;
                   2235:            gotmachine = 1;
                   2236:        }
                   2237:        makeargv();
                   2238:        if (margv[0] == 0)
                   2239:            continue;
                   2240:        c = getcmd(margv[0]);
                   2241:        if (Ambiguous(c)) {
                   2242:            printf("?Ambiguous command: %s\r\n", margv[0]);
                   2243:            continue;
                   2244:        }
                   2245:        if (c == 0) {
                   2246:            printf("?Invalid command: %s\r\n", margv[0]);
                   2247:            continue;
                   2248:        }
                   2249:        /*
                   2250:         * This should never happen...
                   2251:         */
                   2252:        if (c->needconnect && !connected) {
                   2253:            printf("?Need to be connected first for %s.\r\n", margv[0]);
                   2254:            continue;
                   2255:        }
                   2256:        (*c->handler)(margc, margv);
                   2257:     }
                   2258:     fclose(rcfile);
                   2259: }
                   2260:
1.1       deraadt  2261:
                   2262:     int
                   2263: tn(argc, argv)
                   2264:     int argc;
                   2265:     char *argv[];
                   2266: {
1.5       niklas   2267:     register struct hostent *host = 0, *alias = 0;
1.10      art      2268: #if defined(AF_INET6)
                   2269:     struct sockaddr_in6 sin6;
                   2270: #endif
1.1       deraadt  2271:     struct sockaddr_in sin;
1.5       niklas   2272:     struct sockaddr_in ladr;
1.10      art      2273:     struct sockaddr *sa;
                   2274:     int sa_size;
1.1       deraadt  2275:     struct servent *sp = 0;
                   2276:     unsigned long temp;
                   2277:     extern char *inet_ntoa();
                   2278: #if    defined(IP_OPTIONS) && defined(IPPROTO_IP)
1.10      art      2279:     char *srp = 0;
                   2280:     int srlen;
1.1       deraadt  2281: #endif
1.5       niklas   2282:     char *cmd, *hostp = 0, *portp = 0, *user = 0, *aliasp = 0;
1.10      art      2283:     int family, port;
1.1       deraadt  2284:
                   2285:     /* clear the socket address prior to use */
1.3       niklas   2286:     memset((char *)&sin, 0, sizeof(sin));
1.1       deraadt  2287:
                   2288:     if (connected) {
1.10      art      2289:        printf("?Already connected to %s\r\n", hostname);
1.9       tholo    2290:        seteuid(getuid());
1.1       deraadt  2291:        setuid(getuid());
                   2292:        return 0;
                   2293:     }
                   2294:     if (argc < 2) {
                   2295:        (void) strcpy(line, "open ");
                   2296:        printf("(to) ");
                   2297:        (void) fgets(&line[strlen(line)], sizeof(line) - strlen(line), stdin);
                   2298:        makeargv();
                   2299:        argc = margc;
                   2300:        argv = margv;
                   2301:     }
                   2302:     cmd = *argv;
                   2303:     --argc; ++argv;
                   2304:     while (argc) {
1.3       niklas   2305:        if (strcmp(*argv, "help") == 0 || isprefix(*argv, "?"))
1.1       deraadt  2306:            goto usage;
                   2307:        if (strcmp(*argv, "-l") == 0) {
                   2308:            --argc; ++argv;
                   2309:            if (argc == 0)
                   2310:                goto usage;
1.14    ! art      2311:            if ((user = strdup(*argv++)) == NULL)
        !          2312:                err(1, "strdup");
1.1       deraadt  2313:            --argc;
                   2314:            continue;
                   2315:        }
1.5       niklas   2316:        if (strcmp(*argv, "-b") == 0) {
                   2317:            --argc; ++argv;
                   2318:            if (argc == 0)
                   2319:                goto usage;
                   2320:            aliasp = *argv++;
                   2321:            --argc;
                   2322:            continue;
                   2323:        }
1.1       deraadt  2324:        if (strcmp(*argv, "-a") == 0) {
                   2325:            --argc; ++argv;
                   2326:            autologin = 1;
                   2327:            continue;
                   2328:        }
                   2329:        if (hostp == 0) {
                   2330:            hostp = *argv++;
                   2331:            --argc;
                   2332:            continue;
                   2333:        }
                   2334:        if (portp == 0) {
                   2335:            portp = *argv++;
                   2336:            --argc;
                   2337:            continue;
                   2338:        }
                   2339:     usage:
1.10      art      2340:        printf("usage: %s [-l user] [-a] host-name [port]\r\n", cmd);
1.9       tholo    2341:        seteuid(getuid());
1.1       deraadt  2342:        setuid(getuid());
                   2343:        return 0;
                   2344:     }
                   2345:     if (hostp == 0)
                   2346:        goto usage;
                   2347:
                   2348: #if    defined(IP_OPTIONS) && defined(IPPROTO_IP)
                   2349:     if (hostp[0] == '@' || hostp[0] == '!') {
                   2350:        if ((hostname = strrchr(hostp, ':')) == NULL)
                   2351:            hostname = strrchr(hostp, '@');
                   2352:        hostname++;
                   2353:        srp = 0;
                   2354:        temp = sourceroute(hostp, &srp, &srlen);
                   2355:        if (temp == 0) {
                   2356:            herror(srp);
1.9       tholo    2357:            seteuid(getuid());
1.1       deraadt  2358:            setuid(getuid());
                   2359:            return 0;
                   2360:        } else if (temp == -1) {
1.10      art      2361:            printf("Bad source route option: %s\r\n", hostp);
1.9       tholo    2362:            seteuid(getuid());
1.1       deraadt  2363:            setuid(getuid());
                   2364:            return 0;
                   2365:        } else {
1.10      art      2366:            abort();
1.1       deraadt  2367:        }
                   2368:     } else {
                   2369: #endif
1.10      art      2370:        memset (&sin, 0, sizeof(sin));
                   2371: #if defined(HAVE_INET_PTON) && defined(AF_INET6)
                   2372:        memset (&sin6, 0, sizeof(sin6));
                   2373:
                   2374:        if(inet_pton(AF_INET6, hostp, &sin6.sin6_addr)) {
                   2375:            sin6.sin6_family = family = AF_INET6;
1.12      art      2376:            sa = (struct sockaddr *)&sin6;
                   2377:            sa_size = sizeof(sin6);
1.10      art      2378:            strcpy(_hostname, hostp);
                   2379:            hostname =_hostname;
                   2380:        } else
                   2381: #endif
                   2382:            if(inet_aton(hostp, &sin.sin_addr)){
                   2383:                sin.sin_family = family = AF_INET;
1.12      art      2384:                sa = (struct sockaddr *)&sin;
                   2385:                sa_size = sizeof(sin);
1.10      art      2386:                strcpy(_hostname, hostp);
1.1       deraadt  2387:                hostname = _hostname;
                   2388:            } else {
1.10      art      2389: #ifdef HAVE_GETHOSTBYNAME2
                   2390:                host = gethostbyname2(hostp, AF_INET6);
                   2391:                if(host == NULL)
                   2392:                    host = gethostbyname2(hostp, AF_INET);
                   2393: #else
                   2394:                host = gethostbyname(hostp);
                   2395: #endif
                   2396:                if (host) {
                   2397:                    strncpy(_hostname, host->h_name, sizeof(_hostname));
                   2398:                    family = host->h_addrtype;
                   2399:
                   2400:                    switch(family) {
                   2401:                    case AF_INET:
                   2402:                        memset(&sin, 0, sizeof(sin));
                   2403:                        sa_size = sizeof(sin);
                   2404:                        sa = (struct sockaddr *)&sin;
                   2405:                        sin.sin_family = family;
                   2406:                        sin.sin_addr   = *((struct in_addr *)(*host->h_addr_list));
                   2407:                        break;
                   2408: #if defined(AF_INET6) && defined(HAVE_STRUCT_SOCKADDR_IN6)
                   2409:                    case AF_INET6:
                   2410:                        memset(&sin6, 0, sizeof(sin6));
                   2411:                        sa_size = sizeof(sin6);
                   2412:                        sa = (struct sockaddr *)&sin6;
                   2413:                        sin6.sin6_family = family;
                   2414:                        sin6.sin6_addr   = *((struct in6_addr *)(*host->h_addr_list));
                   2415:                        break;
                   2416: #endif
                   2417:                    default:
                   2418:                        fprintf(stderr, "Bad address family: %d\n", family);
                   2419:                        return 0;
                   2420:                    }
                   2421:
                   2422:                    _hostname[sizeof(_hostname)-1] = '\0';
                   2423:                    hostname = _hostname;
                   2424:                } else {
                   2425:                    herror(hostp);
                   2426:                    seteuid(getuid());
                   2427:                    setuid(getuid());
                   2428:                    fprintf (stderr, "%s: %s\r\n", hostp ? hostp : "",
                   2429:                             "unknown error");
                   2430:                    return 0;
                   2431:                }
1.1       deraadt  2432:            }
                   2433: #if    defined(IP_OPTIONS) && defined(IPPROTO_IP)
                   2434:     }
                   2435: #endif
                   2436:     if (portp) {
                   2437:        if (*portp == '-') {
                   2438:            portp++;
                   2439:            telnetport = 1;
                   2440:        } else
                   2441:            telnetport = 0;
1.10      art      2442:        port = atoi(portp);
                   2443:        if (port == 0) {
1.1       deraadt  2444:            sp = getservbyname(portp, "tcp");
                   2445:            if (sp)
1.10      art      2446:                port = sp->s_port;
1.1       deraadt  2447:            else {
1.10      art      2448:                printf("%s: bad port number\r\n", portp);
1.9       tholo    2449:                seteuid(getuid());
1.3       niklas   2450:                setuid(getuid());
1.1       deraadt  2451:                return 0;
                   2452:            }
                   2453:        } else {
1.10      art      2454:            port = htons(port);
1.1       deraadt  2455:        }
                   2456:     } else {
                   2457:        if (sp == 0) {
                   2458:            sp = getservbyname("telnet", "tcp");
                   2459:            if (sp == 0) {
1.10      art      2460:                fprintf(stderr, "telnet: tcp/telnet: unknown service\r\n");
1.9       tholo    2461:                seteuid(getuid());
1.3       niklas   2462:                setuid(getuid());
1.1       deraadt  2463:                return 0;
                   2464:            }
1.10      art      2465:            port = sp->s_port;
1.1       deraadt  2466:        }
                   2467:        telnetport = 1;
                   2468:     }
1.10      art      2469:     switch(family) {
                   2470:     case AF_INET:
                   2471:        sin.sin_port = port;
                   2472:        printf("Trying %s...\r\n", inet_ntoa(sin.sin_addr));
                   2473:        break;
                   2474: #if defined(AF_INET6) && defined(HAVE_STRUCT_SOCKADDR_IN6)
                   2475:     case AF_INET6: {
                   2476: #ifndef INET6_ADDRSTRLEN
                   2477: #define INET6_ADDRSTRLEN 46
                   2478: #endif
                   2479:
                   2480:        char buf[INET6_ADDRSTRLEN];
                   2481:
                   2482:        sin6.sin6_port = port;
                   2483: #ifdef HAVE_INET_NTOP
                   2484:        printf("Trying %s...\r\n", inet_ntop(AF_INET6,
                   2485:                                             &sin6.sin6_addr,
                   2486:                                             buf,
                   2487:                                             sizeof(buf)));
                   2488: #endif
                   2489:        break;
                   2490:     }
                   2491: #endif
                   2492:     default:
                   2493:        abort();
                   2494:     }
                   2495:
1.1       deraadt  2496:     do {
1.10      art      2497:        net = socket(family, SOCK_STREAM, 0);
1.9       tholo    2498:        seteuid(getuid());
1.1       deraadt  2499:        setuid(getuid());
                   2500:        if (net < 0) {
                   2501:            perror("telnet: socket");
                   2502:            return 0;
                   2503:        }
1.5       niklas   2504:        if (aliasp) {
                   2505:            memset ((caddr_t)&ladr, 0, sizeof (ladr));
                   2506:            temp = inet_addr(aliasp);
                   2507:            if (temp != INADDR_NONE) {
                   2508:                ladr.sin_addr.s_addr = temp;
                   2509:                ladr.sin_family = AF_INET;
                   2510:                alias = gethostbyaddr((char *)&temp, sizeof(temp), AF_INET);
                   2511:            } else {
                   2512:                alias = gethostbyname(aliasp);
                   2513:                if (alias) {
                   2514:                    ladr.sin_family = alias->h_addrtype;
                   2515: #if    defined(h_addr)         /* In 4.3, this is a #define */
                   2516:                    memmove((caddr_t)&ladr.sin_addr,
                   2517:                        alias->h_addr_list[0], alias->h_length);
                   2518: #else  /* defined(h_addr) */
                   2519:                    memmove((caddr_t)&ladr.sin_addr, alias->h_addr,
                   2520:                        alias->h_length);
                   2521: #endif /* defined(h_addr) */
                   2522:                } else {
                   2523:                    herror(aliasp);
                   2524:                    return 0;
                   2525:                }
                   2526:            }
                   2527:             ladr.sin_port = htons(0);
                   2528:
                   2529:             if (bind (net, (struct sockaddr *)&ladr, sizeof(ladr)) < 0) {
                   2530:                 perror(aliasp);;
                   2531:                 (void) close(net);   /* dump descriptor */
                   2532:                return 0;
                   2533:             }
                   2534:         }
                   2535:  #if   defined(IP_OPTIONS) && defined(IPPROTO_IP)
1.1       deraadt  2536:        if (srp && setsockopt(net, IPPROTO_IP, IP_OPTIONS, (char *)srp, srlen) < 0)
                   2537:                perror("setsockopt (IP_OPTIONS)");
                   2538: #endif
                   2539: #if    defined(IPPROTO_IP) && defined(IP_TOS)
                   2540:        {
                   2541: # if   defined(HAS_GETTOS)
                   2542:            struct tosent *tp;
                   2543:            if (tos < 0 && (tp = gettosbyname("telnet", "tcp")))
                   2544:                tos = tp->t_tos;
                   2545: # endif
                   2546:            if (tos < 0)
1.3       niklas   2547:                tos = IPTOS_LOWDELAY;   /* Low Delay bit */
1.1       deraadt  2548:            if (tos
                   2549:                && (setsockopt(net, IPPROTO_IP, IP_TOS,
1.10      art      2550:                    (void *)&tos, sizeof(int)) < 0)
1.1       deraadt  2551:                && (errno != ENOPROTOOPT))
                   2552:                    perror("telnet: setsockopt (IP_TOS) (ignored)");
                   2553:        }
                   2554: #endif /* defined(IPPROTO_IP) && defined(IP_TOS) */
                   2555:
                   2556:        if (debug && SetSockOpt(net, SOL_SOCKET, SO_DEBUG, 1) < 0) {
                   2557:                perror("setsockopt (SO_DEBUG)");
                   2558:        }
                   2559:
1.10      art      2560:        if (connect(net, sa, sa_size) < 0) {
1.1       deraadt  2561:            if (host && host->h_addr_list[1]) {
                   2562:                int oerrno = errno;
                   2563:
1.10      art      2564:                switch(family) {
                   2565:                case AF_INET :
                   2566:                    fprintf(stderr, "telnet: connect to address %s: ",
                   2567:                            inet_ntoa(sin.sin_addr));
                   2568:                    sin.sin_addr = *((struct in_addr *)(*++host->h_addr_list));
                   2569:                    break;
                   2570: #if defined(AF_INET6) && defined(HAVE_STRUCT_SOCKADDR_IN6)
                   2571:                case AF_INET6: {
                   2572:                    char buf[INET6_ADDRSTRLEN];
                   2573:
                   2574:                    fprintf(stderr, "telnet: connect to address %s: ",
                   2575:                            inet_ntop(AF_INET6, &sin6.sin6_addr, buf,
                   2576:                                      sizeof(buf)));
                   2577:                    sin6.sin6_addr = *((struct in6_addr *)(*++host->h_addr_list));
                   2578:                    break;
                   2579:                }
                   2580: #endif
                   2581:                default:
                   2582:                    abort();
                   2583:                }
                   2584:
1.1       deraadt  2585:                errno = oerrno;
1.10      art      2586:                perror(NULL);
1.1       deraadt  2587:                host->h_addr_list++;
1.3       niklas   2588:                memmove((caddr_t)&sin.sin_addr,
1.1       deraadt  2589:                        host->h_addr_list[0], host->h_length);
                   2590:                (void) NetClose(net);
                   2591:                continue;
                   2592:            }
                   2593:            perror("telnet: Unable to connect to remote host");
                   2594:            return 0;
                   2595:        }
                   2596:        connected++;
1.10      art      2597: #if    defined(AUTHENTICATION) || defined(ENCRYPTION)
1.1       deraadt  2598:        auth_encrypt_connect(connected);
                   2599: #endif /* defined(AUTHENTICATION) */
                   2600:     } while (connected == 0);
                   2601:     cmdrc(hostp, hostname);
                   2602:     if (autologin && user == NULL) {
                   2603:        struct passwd *pw;
                   2604:
                   2605:        user = getenv("USER");
                   2606:        if (user == NULL ||
                   2607:            (pw = getpwnam(user)) && pw->pw_uid != getuid()) {
                   2608:                if (pw = getpwuid(getuid()))
                   2609:                        user = pw->pw_name;
                   2610:                else
                   2611:                        user = NULL;
                   2612:        }
                   2613:     }
                   2614:     if (user) {
                   2615:        env_define((unsigned char *)"USER", (unsigned char *)user);
                   2616:        env_export((unsigned char *)"USER");
                   2617:     }
                   2618:     (void) call(status, "status", "notmuch", 0);
                   2619:     if (setjmp(peerdied) == 0)
                   2620:        telnet(user);
                   2621:     (void) NetClose(net);
1.10      art      2622:     ExitString("Connection closed by foreign host.\r\n",1);
1.1       deraadt  2623:     /*NOTREACHED*/
1.10      art      2624:     return 0;
1.1       deraadt  2625: }
                   2626:
                   2627: #define HELPINDENT (sizeof ("connect"))
                   2628:
                   2629: static char
                   2630:        openhelp[] =    "connect to a site",
                   2631:        closehelp[] =   "close current connection",
                   2632:        logouthelp[] =  "forcibly logout remote user and close the connection",
                   2633:        quithelp[] =    "exit telnet",
                   2634:        statushelp[] =  "print status information",
                   2635:        helphelp[] =    "print help information",
                   2636:        sendhelp[] =    "transmit special characters ('send ?' for more)",
                   2637:        sethelp[] =     "set operating parameters ('set ?' for more)",
                   2638:        unsethelp[] =   "unset operating parameters ('unset ?' for more)",
                   2639:        togglestring[] ="toggle operating parameters ('toggle ?' for more)",
                   2640:        slchelp[] =     "change state of special charaters ('slc ?' for more)",
                   2641:        displayhelp[] = "display operating parameters",
                   2642: #if    defined(TN3270) && defined(unix)
                   2643:        transcomhelp[] = "specify Unix command for transparent mode pipe",
                   2644: #endif /* defined(TN3270) && defined(unix) */
                   2645: #if    defined(AUTHENTICATION)
                   2646:        authhelp[] =    "turn on (off) authentication ('auth ?' for more)",
                   2647: #endif
1.10      art      2648: #if     defined(ENCRYPTION)
                   2649:         encrypthelp[] = "turn on (off) encryption ('encrypt ?' for more)",
                   2650: #endif
1.1       deraadt  2651:        zhelp[] =       "suspend telnet",
                   2652:        shellhelp[] =   "invoke a subshell",
                   2653:        envhelp[] =     "change environment variables ('environ ?' for more)",
                   2654:        modestring[] = "try to enter line or character mode ('mode ?' for more)";
                   2655:
1.10      art      2656: static int     help __P((int, char**));
1.1       deraadt  2657:
                   2658: static Command cmdtab[] = {
                   2659:        { "close",      closehelp,      bye,            1 },
                   2660:        { "logout",     logouthelp,     logout,         1 },
                   2661:        { "display",    displayhelp,    display,        0 },
                   2662:        { "mode",       modestring,     modecmd,        0 },
                   2663:        { "open",       openhelp,       tn,             0 },
                   2664:        { "quit",       quithelp,       quit,           0 },
                   2665:        { "send",       sendhelp,       sendcmd,        0 },
                   2666:        { "set",        sethelp,        setcmd,         0 },
                   2667:        { "unset",      unsethelp,      unsetcmd,       0 },
                   2668:        { "status",     statushelp,     status,         0 },
                   2669:        { "toggle",     togglestring,   toggle,         0 },
                   2670:        { "slc",        slchelp,        slccmd,         0 },
                   2671: #if    defined(TN3270) && defined(unix)
                   2672:        { "transcom",   transcomhelp,   settranscom,    0 },
                   2673: #endif /* defined(TN3270) && defined(unix) */
                   2674: #if    defined(AUTHENTICATION)
                   2675:        { "auth",       authhelp,       auth_cmd,       0 },
                   2676: #endif
1.10      art      2677: #if    defined(ENCRYPTION)
                   2678:        { "encrypt",    encrypthelp,    encrypt_cmd,    0 },
                   2679: #endif
                   2680:
                   2681:        { "z",          zhelp,          telnetsuspend,  0 },
1.1       deraadt  2682: #if    defined(TN3270)
                   2683:        { "!",          shellhelp,      shell,          1 },
                   2684: #else
                   2685:        { "!",          shellhelp,      shell,          0 },
                   2686: #endif
                   2687:        { "environ",    envhelp,        env_cmd,        0 },
                   2688:        { "?",          helphelp,       help,           0 },
1.6       deraadt  2689: #if    defined(SKEY)
                   2690:        { "skey",       NULL,           skey_calc,      0 },
                   2691: #endif
1.10      art      2692:        { 0,            0,              0,              0 }
1.1       deraadt  2693: };
                   2694:
                   2695: static char    crmodhelp[] =   "deprecated command -- use 'toggle crmod' instead";
                   2696: static char    escapehelp[] =  "deprecated command -- use 'set escape' instead";
                   2697:
                   2698: static Command cmdtab2[] = {
                   2699:        { "help",       0,              help,           0 },
                   2700:        { "escape",     escapehelp,     setescape,      0 },
                   2701:        { "crmod",      crmodhelp,      togcrmod,       0 },
1.10      art      2702:        { 0,            0,              0,              0 }
1.1       deraadt  2703: };
                   2704:
                   2705:
                   2706: /*
                   2707:  * Call routine with argc, argv set from args (terminated by 0).
                   2708:  */
                   2709:
                   2710:     /*VARARGS1*/
1.10      art      2711:     static int
                   2712: call(intrtn_t routine, ...)
1.1       deraadt  2713: {
                   2714:     va_list ap;
                   2715:     char *args[100];
                   2716:     int argno = 0;
                   2717:
1.10      art      2718:     va_start(ap, routine);
                   2719:     while ((args[argno++] = va_arg(ap, char *)) != 0);
1.1       deraadt  2720:     va_end(ap);
                   2721:     return (*routine)(argno-1, args);
                   2722: }
                   2723:
                   2724:
                   2725:     static Command *
                   2726: getcmd(name)
                   2727:     char *name;
                   2728: {
                   2729:     Command *cm;
                   2730:
1.10      art      2731:     if ((cm = (Command *) genget(name, (char **) cmdtab, sizeof(Command))))
1.1       deraadt  2732:        return cm;
                   2733:     return (Command *) genget(name, (char **) cmdtab2, sizeof(Command));
                   2734: }
                   2735:
                   2736:     void
                   2737: command(top, tbuf, cnt)
                   2738:     int top;
                   2739:     char *tbuf;
                   2740:     int cnt;
                   2741: {
                   2742:     register Command *c;
                   2743:
                   2744:     setcommandmode();
                   2745:     if (!top) {
                   2746:        putchar('\n');
                   2747: #if    defined(unix)
                   2748:     } else {
                   2749:        (void) signal(SIGINT, SIG_DFL);
                   2750:        (void) signal(SIGQUIT, SIG_DFL);
                   2751: #endif /* defined(unix) */
                   2752:     }
                   2753:     for (;;) {
                   2754:        if (rlogin == _POSIX_VDISABLE)
                   2755:                printf("%s> ", prompt);
                   2756:        if (tbuf) {
                   2757:            register char *cp;
                   2758:            cp = line;
                   2759:            while (cnt > 0 && (*cp++ = *tbuf++) != '\n')
                   2760:                cnt--;
                   2761:            tbuf = 0;
                   2762:            if (cp == line || *--cp != '\n' || cp == line)
                   2763:                goto getline;
                   2764:            *cp = '\0';
                   2765:            if (rlogin == _POSIX_VDISABLE)
1.10      art      2766:                printf("%s\r\n", line);
1.1       deraadt  2767:        } else {
                   2768:        getline:
                   2769:            if (rlogin != _POSIX_VDISABLE)
                   2770:                printf("%s> ", prompt);
                   2771:            if (fgets(line, sizeof(line), stdin) == NULL) {
                   2772:                if (feof(stdin) || ferror(stdin)) {
                   2773:                    (void) quit();
                   2774:                    /*NOTREACHED*/
                   2775:                }
                   2776:                break;
                   2777:            }
                   2778:        }
                   2779:        if (line[0] == 0)
                   2780:            break;
                   2781:        makeargv();
                   2782:        if (margv[0] == 0) {
                   2783:            break;
                   2784:        }
                   2785:        c = getcmd(margv[0]);
                   2786:        if (Ambiguous(c)) {
1.10      art      2787:            printf("?Ambiguous command\r\n");
1.1       deraadt  2788:            continue;
                   2789:        }
                   2790:        if (c == 0) {
1.10      art      2791:            printf("?Invalid command\r\n");
1.1       deraadt  2792:            continue;
                   2793:        }
                   2794:        if (c->needconnect && !connected) {
1.10      art      2795:            printf("?Need to be connected first.\r\n");
1.1       deraadt  2796:            continue;
                   2797:        }
                   2798:        if ((*c->handler)(margc, margv)) {
                   2799:            break;
                   2800:        }
                   2801:     }
                   2802:     if (!top) {
                   2803:        if (!connected) {
                   2804:            longjmp(toplevel, 1);
                   2805:            /*NOTREACHED*/
                   2806:        }
                   2807: #if    defined(TN3270)
                   2808:        if (shell_active == 0) {
                   2809:            setconnmode(0);
                   2810:        }
                   2811: #else  /* defined(TN3270) */
                   2812:        setconnmode(0);
                   2813: #endif /* defined(TN3270) */
                   2814:     }
                   2815: }
                   2816: 
                   2817: /*
                   2818:  * Help command.
                   2819:  */
                   2820:        static
                   2821: help(argc, argv)
                   2822:        int argc;
                   2823:        char *argv[];
                   2824: {
                   2825:        register Command *c;
                   2826:
                   2827:        if (argc == 1) {
1.10      art      2828:                printf("Commands may be abbreviated.  Commands are:\r\n\r\n");
1.1       deraadt  2829:                for (c = cmdtab; c->name; c++)
                   2830:                        if (c->help) {
1.10      art      2831:                                printf("%-*s\t%s\r\n", HELPINDENT, c->name,
1.1       deraadt  2832:                                                                    c->help);
                   2833:                        }
                   2834:                return 0;
                   2835:        }
                   2836:        while (--argc > 0) {
                   2837:                register char *arg;
                   2838:                arg = *++argv;
                   2839:                c = getcmd(arg);
                   2840:                if (Ambiguous(c))
1.10      art      2841:                        printf("?Ambiguous help command %s\r\n", arg);
1.1       deraadt  2842:                else if (c == (Command *)0)
1.10      art      2843:                        printf("?Invalid help command %s\r\n", arg);
1.1       deraadt  2844:                else
1.10      art      2845:                        printf("%s\r\n", c->help);
1.1       deraadt  2846:        }
                   2847:        return 0;
                   2848: }
                   2849:
                   2850: static char *rcname = 0;
                   2851: static char rcbuf[128];
                   2852:
                   2853: #if    defined(IP_OPTIONS) && defined(IPPROTO_IP)
                   2854:
                   2855: /*
                   2856:  * Source route is handed in as
                   2857:  *     [!]@hop1@hop2...[@|:]dst
                   2858:  * If the leading ! is present, it is a
                   2859:  * strict source route, otherwise it is
                   2860:  * assmed to be a loose source route.
                   2861:  *
                   2862:  * We fill in the source route option as
                   2863:  *     hop1,hop2,hop3...dest
                   2864:  * and return a pointer to hop1, which will
                   2865:  * be the address to connect() to.
                   2866:  *
                   2867:  * Arguments:
                   2868:  *     arg:    pointer to route list to decipher
                   2869:  *
                   2870:  *     cpp:    If *cpp is not equal to NULL, this is a
                   2871:  *             pointer to a pointer to a character array
                   2872:  *             that should be filled in with the option.
                   2873:  *
                   2874:  *     lenp:   pointer to an integer that contains the
                   2875:  *             length of *cpp if *cpp != NULL.
                   2876:  *
                   2877:  * Return values:
                   2878:  *
                   2879:  *     Returns the address of the host to connect to.  If the
                   2880:  *     return value is -1, there was a syntax error in the
                   2881:  *     option, either unknown characters, or too many hosts.
                   2882:  *     If the return value is 0, one of the hostnames in the
                   2883:  *     path is unknown, and *cpp is set to point to the bad
                   2884:  *     hostname.
                   2885:  *
                   2886:  *     *cpp:   If *cpp was equal to NULL, it will be filled
                   2887:  *             in with a pointer to our static area that has
                   2888:  *             the option filled in.  This will be 32bit aligned.
1.3       niklas   2889:  *
1.1       deraadt  2890:  *     *lenp:  This will be filled in with how long the option
                   2891:  *             pointed to by *cpp is.
1.3       niklas   2892:  *
1.1       deraadt  2893:  */
                   2894:        unsigned long
                   2895: sourceroute(arg, cpp, lenp)
                   2896:        char    *arg;
                   2897:        char    **cpp;
                   2898:        int     *lenp;
                   2899: {
                   2900:        static char lsr[44];
                   2901: #ifdef sysV88
                   2902:        static IOPTN ipopt;
                   2903: #endif
                   2904:        char *cp, *cp2, *lsrp, *lsrep;
                   2905:        register int tmp;
                   2906:        struct in_addr sin_addr;
                   2907:        register struct hostent *host = 0;
                   2908:        register char c;
                   2909:
                   2910:        /*
                   2911:         * Verify the arguments, and make sure we have
                   2912:         * at least 7 bytes for the option.
                   2913:         */
                   2914:        if (cpp == NULL || lenp == NULL)
                   2915:                return((unsigned long)-1);
                   2916:        if (*cpp != NULL && *lenp < 7)
                   2917:                return((unsigned long)-1);
                   2918:        /*
                   2919:         * Decide whether we have a buffer passed to us,
                   2920:         * or if we need to use our own static buffer.
                   2921:         */
                   2922:        if (*cpp) {
                   2923:                lsrp = *cpp;
                   2924:                lsrep = lsrp + *lenp;
                   2925:        } else {
                   2926:                *cpp = lsrp = lsr;
                   2927:                lsrep = lsrp + 44;
                   2928:        }
                   2929:
                   2930:        cp = arg;
                   2931:
                   2932:        /*
                   2933:         * Next, decide whether we have a loose source
                   2934:         * route or a strict source route, and fill in
                   2935:         * the begining of the option.
                   2936:         */
                   2937: #ifndef        sysV88
                   2938:        if (*cp == '!') {
                   2939:                cp++;
                   2940:                *lsrp++ = IPOPT_SSRR;
                   2941:        } else
                   2942:                *lsrp++ = IPOPT_LSRR;
                   2943: #else
                   2944:        if (*cp == '!') {
                   2945:                cp++;
                   2946:                ipopt.io_type = IPOPT_SSRR;
                   2947:        } else
                   2948:                ipopt.io_type = IPOPT_LSRR;
                   2949: #endif
                   2950:
                   2951:        if (*cp != '@')
                   2952:                return((unsigned long)-1);
                   2953:
                   2954: #ifndef        sysV88
                   2955:        lsrp++;         /* skip over length, we'll fill it in later */
                   2956:        *lsrp++ = 4;
                   2957: #endif
                   2958:
                   2959:        cp++;
                   2960:
                   2961:        sin_addr.s_addr = 0;
                   2962:
                   2963:        for (c = 0;;) {
                   2964:                if (c == ':')
                   2965:                        cp2 = 0;
1.10      art      2966:                else for (cp2 = cp; (c = *cp2); cp2++) {
1.1       deraadt  2967:                        if (c == ',') {
                   2968:                                *cp2++ = '\0';
                   2969:                                if (*cp2 == '@')
                   2970:                                        cp2++;
                   2971:                        } else if (c == '@') {
                   2972:                                *cp2++ = '\0';
                   2973:                        } else if (c == ':') {
                   2974:                                *cp2++ = '\0';
                   2975:                        } else
                   2976:                                continue;
                   2977:                        break;
                   2978:                }
                   2979:                if (!c)
                   2980:                        cp2 = 0;
                   2981:
1.10      art      2982:                if ((tmp = inet_addr(cp)) != -1) {
1.1       deraadt  2983:                        sin_addr.s_addr = tmp;
1.10      art      2984:                } else if ((host = gethostbyname(cp))) {
1.1       deraadt  2985: #if    defined(h_addr)
1.3       niklas   2986:                        memmove((caddr_t)&sin_addr,
1.10      art      2987:                                host->h_addr_list[0],
                   2988:                                sizeof(sin_addr));
1.1       deraadt  2989: #else
1.10      art      2990:                        memmove((caddr_t)&sin_addr, host->h_addr,
                   2991:                                sizeof(sin_addr));
1.1       deraadt  2992: #endif
                   2993:                } else {
                   2994:                        *cpp = cp;
                   2995:                        return(0);
                   2996:                }
1.3       niklas   2997:                memmove(lsrp, (char *)&sin_addr, 4);
1.1       deraadt  2998:                lsrp += 4;
                   2999:                if (cp2)
                   3000:                        cp = cp2;
                   3001:                else
                   3002:                        break;
                   3003:                /*
                   3004:                 * Check to make sure there is space for next address
                   3005:                 */
                   3006:                if (lsrp + 4 > lsrep)
                   3007:                        return((unsigned long)-1);
                   3008:        }
                   3009: #ifndef        sysV88
                   3010:        if ((*(*cpp+IPOPT_OLEN) = lsrp - *cpp) <= 7) {
                   3011:                *cpp = 0;
                   3012:                *lenp = 0;
                   3013:                return((unsigned long)-1);
                   3014:        }
                   3015:        *lsrp++ = IPOPT_NOP; /* 32 bit word align it */
                   3016:        *lenp = lsrp - *cpp;
                   3017: #else
                   3018:        ipopt.io_len = lsrp - *cpp;
                   3019:        if (ipopt.io_len <= 5) {                /* Is 3 better ? */
                   3020:                *cpp = 0;
                   3021:                *lenp = 0;
                   3022:                return((unsigned long)-1);
                   3023:        }
                   3024:        *lenp = sizeof(ipopt);
                   3025:        *cpp = (char *) &ipopt;
                   3026: #endif
                   3027:        return(sin_addr.s_addr);
                   3028: }
                   3029: #endif