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

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