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

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