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

1.80    ! jca         1: /*     $OpenBSD: commands.c,v 1.79 2015/11/13 17:04:48 deraadt Exp $   */
1.4       deraadt     2: /*     $NetBSD: commands.c,v 1.14 1996/03/24 22:03:48 jtk Exp $        */
1.3       niklas      3:
1.1       deraadt     4: /*
                      5:  * Copyright (c) 1988, 1990, 1993
                      6:  *     The Regents of the University of California.  All rights reserved.
                      7:  *
                      8:  * Redistribution and use in source and binary forms, with or without
                      9:  * modification, are permitted provided that the following conditions
                     10:  * are met:
                     11:  * 1. Redistributions of source code must retain the above copyright
                     12:  *    notice, this list of conditions and the following disclaimer.
                     13:  * 2. Redistributions in binary form must reproduce the above copyright
                     14:  *    notice, this list of conditions and the following disclaimer in the
                     15:  *    documentation and/or other materials provided with the distribution.
1.45      millert    16:  * 3. Neither the name of the University nor the names of its contributors
1.1       deraadt    17:  *    may be used to endorse or promote products derived from this software
                     18:  *    without specific prior written permission.
                     19:  *
                     20:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     21:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     22:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     23:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     24:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     25:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     26:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     27:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     28:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     29:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     30:  * SUCH DAMAGE.
                     31:  */
                     32:
1.10      art        33: #include "telnet_locl.h"
1.59      guenther   34:
1.62      guenther   35: #include <sys/socket.h>
1.59      guenther   36: #include <netinet/in.h>
                     37: #include <netinet/ip.h>
                     38: #include <arpa/inet.h>
1.62      guenther   39: #include <arpa/telnet.h>
1.59      guenther   40:
1.60      guenther   41: #include <ctype.h>
1.15      art        42: #include <err.h>
1.62      guenther   43: #include <errno.h>
1.61      guenther   44: #include <netdb.h>
1.59      guenther   45: #include <pwd.h>
                     46: #include <stdarg.h>
1.62      guenther   47: #include <stdlib.h>
                     48: #include <string.h>
1.60      guenther   49: #include <unistd.h>
1.71      deraadt    50: #include <limits.h>
1.1       deraadt    51:
                     52: char   *hostname;
                     53:
                     54: typedef struct {
                     55:        char    *name;          /* command name */
                     56:        char    *help;          /* help string (NULL for no help) */
1.63      guenther   57:        int     (*handler)(int, char **);/* routine which executes command */
1.1       deraadt    58:        int     needconnect;    /* Do we need to be connected to execute? */
                     59: } Command;
                     60:
                     61: static char line[256];
                     62: static int margc;
                     63: static char *margv[20];
                     64:
1.69      jsg        65: static void
                     66: makeargv(void)
1.1       deraadt    67: {
1.39      mpech      68:     char *cp, *cp2, c;
                     69:     char **argp = margv;
1.1       deraadt    70:
                     71:     margc = 0;
                     72:     cp = line;
1.10      art        73:     while ((c = *cp)) {
1.39      mpech      74:        int inquote = 0;
1.60      guenther   75:        while (isspace((unsigned char)c))
1.1       deraadt    76:            c = *++cp;
                     77:        if (c == '\0')
                     78:            break;
                     79:        *argp++ = cp;
                     80:        margc += 1;
                     81:        for (cp2 = cp; c != '\0'; c = *++cp) {
                     82:            if (inquote) {
                     83:                if (c == inquote) {
                     84:                    inquote = 0;
                     85:                    continue;
                     86:                }
                     87:            } else {
                     88:                if (c == '\\') {
                     89:                    if ((c = *++cp) == '\0')
                     90:                        break;
                     91:                } else if (c == '"') {
                     92:                    inquote = '"';
                     93:                    continue;
                     94:                } else if (c == '\'') {
                     95:                    inquote = '\'';
                     96:                    continue;
1.60      guenther   97:                } else if (isspace((unsigned char)c))
1.1       deraadt    98:                    break;
                     99:            }
                    100:            *cp2++ = c;
                    101:        }
                    102:        *cp2 = '\0';
                    103:        if (c == '\0')
                    104:            break;
                    105:        cp++;
                    106:     }
                    107:     *argp++ = 0;
                    108: }
                    109:
                    110: /*
                    111:  * Make a character string into a number.
                    112:  *
                    113:  * Todo:  1.  Could take random integers (12, 0x12, 012, 0b1).
                    114:  */
                    115:
1.69      jsg       116: static char
                    117: special(char *s)
1.1       deraadt   118: {
1.39      mpech     119:        char c;
1.1       deraadt   120:        char b;
                    121:
                    122:        switch (*s) {
                    123:        case '^':
                    124:                b = *++s;
                    125:                if (b == '?') {
                    126:                    c = b | 0x40;               /* DEL */
                    127:                } else {
                    128:                    c = b & 0x1f;
                    129:                }
                    130:                break;
                    131:        default:
                    132:                c = *s;
                    133:                break;
                    134:        }
                    135:        return c;
                    136: }
                    137:
                    138: /*
                    139:  * Construct a control character sequence
                    140:  * for a special character.
                    141:  */
1.69      jsg       142: static char *
                    143: control(cc_t c)
1.1       deraadt   144: {
                    145:        static char buf[5];
                    146:        /*
                    147:         * The only way I could get the Sun 3.5 compiler
                    148:         * to shut up about
                    149:         *      if ((unsigned int)c >= 0x80)
                    150:         * was to assign "c" to an unsigned int variable...
                    151:         * Arggg....
                    152:         */
1.39      mpech     153:        unsigned int uic = (unsigned int)c;
1.1       deraadt   154:
                    155:        if (uic == 0x7f)
                    156:                return ("^?");
                    157:        if (c == (cc_t)_POSIX_VDISABLE) {
                    158:                return "off";
                    159:        }
                    160:        if (uic >= 0x80) {
                    161:                buf[0] = '\\';
                    162:                buf[1] = ((c>>6)&07) + '0';
                    163:                buf[2] = ((c>>3)&07) + '0';
                    164:                buf[3] = (c&07) + '0';
                    165:                buf[4] = 0;
                    166:        } else if (uic >= 0x20) {
                    167:                buf[0] = c;
                    168:                buf[1] = 0;
                    169:        } else {
                    170:                buf[0] = '^';
                    171:                buf[1] = '@'+c;
                    172:                buf[2] = 0;
                    173:        }
                    174:        return (buf);
                    175: }
                    176:
                    177: /*
                    178:  *     The following are data structures and routines for
                    179:  *     the "send" command.
                    180:  *
                    181:  */
1.3       niklas    182:
1.1       deraadt   183: struct sendlist {
                    184:     char       *name;          /* How user refers to it (case independent) */
                    185:     char       *help;          /* Help information (0 ==> no help) */
                    186:     int                needconnect;    /* Need to be connected */
                    187:     int                narg;           /* Number of arguments */
                    188:     int                (*handler)();   /* Routine to perform (for special ops) */
                    189:     int                nbyte;          /* Number of bytes to send this command */
                    190:     int                what;           /* Character to be sent (<0 ==> special) */
                    191: };
                    192: 
                    193:
                    194: static int
1.41      millert   195:        send_esc(void),
                    196:        send_help(void),
                    197:        send_docmd(char *),
                    198:        send_dontcmd(char *),
                    199:        send_willcmd(char *),
                    200:        send_wontcmd(char *);
1.1       deraadt   201:
                    202: static struct sendlist Sendlist[] = {
                    203:     { "ao",    "Send Telnet Abort output",             1, 0, 0, 2, AO },
                    204:     { "ayt",   "Send Telnet 'Are You There'",          1, 0, 0, 2, AYT },
                    205:     { "brk",   "Send Telnet Break",                    1, 0, 0, 2, BREAK },
                    206:     { "break", 0,                                      1, 0, 0, 2, BREAK },
                    207:     { "ec",    "Send Telnet Erase Character",          1, 0, 0, 2, EC },
                    208:     { "el",    "Send Telnet Erase Line",               1, 0, 0, 2, EL },
                    209:     { "escape",        "Send current escape character",        1, 0, send_esc, 1, 0 },
                    210:     { "ga",    "Send Telnet 'Go Ahead' sequence",      1, 0, 0, 2, GA },
                    211:     { "ip",    "Send Telnet Interrupt Process",        1, 0, 0, 2, IP },
                    212:     { "intp",  0,                                      1, 0, 0, 2, IP },
                    213:     { "interrupt", 0,                                  1, 0, 0, 2, IP },
                    214:     { "intr",  0,                                      1, 0, 0, 2, IP },
                    215:     { "nop",   "Send Telnet 'No operation'",           1, 0, 0, 2, NOP },
                    216:     { "eor",   "Send Telnet 'End of Record'",          1, 0, 0, 2, EOR },
                    217:     { "abort", "Send Telnet 'Abort Process'",          1, 0, 0, 2, ABORT },
                    218:     { "susp",  "Send Telnet 'Suspend Process'",        1, 0, 0, 2, SUSP },
                    219:     { "eof",   "Send Telnet End of File Character",    1, 0, 0, 2, xEOF },
                    220:     { "synch", "Perform Telnet 'Synch operation'",     1, 0, dosynch, 2, 0 },
                    221:     { "getstatus", "Send request for STATUS",          1, 0, get_status, 6, 0 },
                    222:     { "?",     "Display send options",                 0, 0, send_help, 0, 0 },
                    223:     { "help",  0,                                      0, 0, send_help, 0, 0 },
                    224:     { "do",    0,                                      0, 1, send_docmd, 3, 0 },
                    225:     { "dont",  0,                                      0, 1, send_dontcmd, 3, 0 },
                    226:     { "will",  0,                                      0, 1, send_willcmd, 3, 0 },
                    227:     { "wont",  0,                                      0, 1, send_wontcmd, 3, 0 },
                    228:     { 0 }
                    229: };
                    230:
                    231: #define        GETSEND(name) ((struct sendlist *) genget(name, (char **) Sendlist, \
                    232:                                sizeof(struct sendlist)))
                    233:
1.69      jsg       234: static int
                    235: sendcmd(int argc, char **argv)
1.1       deraadt   236: {
                    237:     int count;         /* how many bytes we are going to need to send */
                    238:     int i;
                    239:     struct sendlist *s;        /* pointer to current command */
                    240:     int success = 0;
                    241:     int needconnect = 0;
                    242:
                    243:     if (argc < 2) {
1.10      art       244:        printf("need at least one argument for 'send' command\r\n");
                    245:        printf("'send ?' for help\r\n");
1.1       deraadt   246:        return 0;
                    247:     }
                    248:     /*
                    249:      * First, validate all the send arguments.
                    250:      * In addition, we see how much space we are going to need, and
                    251:      * whether or not we will be doing a "SYNCH" operation (which
                    252:      * flushes the network queue).
                    253:      */
                    254:     count = 0;
                    255:     for (i = 1; i < argc; i++) {
                    256:        s = GETSEND(argv[i]);
                    257:        if (s == 0) {
1.10      art       258:            printf("Unknown send argument '%s'\r\n'send ?' for help.\r\n",
1.1       deraadt   259:                        argv[i]);
                    260:            return 0;
                    261:        } else if (Ambiguous(s)) {
1.10      art       262:            printf("Ambiguous send argument '%s'\r\n'send ?' for help.\r\n",
1.1       deraadt   263:                        argv[i]);
                    264:            return 0;
                    265:        }
                    266:        if (i + s->narg >= argc) {
                    267:            fprintf(stderr,
1.10      art       268:            "Need %d argument%s to 'send %s' command.  'send %s ?' for help.\r\n",
1.1       deraadt   269:                s->narg, s->narg == 1 ? "" : "s", s->name, s->name);
                    270:            return 0;
                    271:        }
                    272:        count += s->nbyte;
                    273:        if (s->handler == send_help) {
                    274:            send_help();
                    275:            return 0;
                    276:        }
                    277:
                    278:        i += s->narg;
                    279:        needconnect += s->needconnect;
                    280:     }
                    281:     if (!connected && needconnect) {
1.10      art       282:        printf("?Need to be connected first.\r\n");
                    283:        printf("'send ?' for help\r\n");
1.1       deraadt   284:        return 0;
                    285:     }
                    286:     /* Now, do we have enough room? */
                    287:     if (NETROOM() < count) {
1.10      art       288:        printf("There is not enough room in the buffer TO the network\r\n");
                    289:        printf("to process your request.  Nothing will be done.\r\n");
                    290:        printf("('send synch' will throw away most data in the network\r\n");
                    291:        printf("buffer, if this might help.)\r\n");
1.1       deraadt   292:        return 0;
                    293:     }
                    294:     /* OK, they are all OK, now go through again and actually send */
                    295:     count = 0;
                    296:     for (i = 1; i < argc; i++) {
                    297:        if ((s = GETSEND(argv[i])) == 0) {
1.10      art       298:            fprintf(stderr, "Telnet 'send' error - argument disappeared!\r\n");
1.63      guenther  299:            quit();
1.1       deraadt   300:        }
                    301:        if (s->handler) {
                    302:            count++;
                    303:            success += (*s->handler)((s->narg > 0) ? argv[i+1] : 0,
                    304:                                  (s->narg > 1) ? argv[i+2] : 0);
                    305:            i += s->narg;
                    306:        } else {
                    307:            NET2ADD(IAC, s->what);
                    308:            printoption("SENT", IAC, s->what);
                    309:        }
                    310:     }
                    311:     return (count == success);
                    312: }
                    313:
1.69      jsg       314: static int send_tncmd(void (*func)(int, int), char *cmd, char *name);
1.10      art       315:
1.69      jsg       316: static int
                    317: send_esc(void)
1.1       deraadt   318: {
                    319:     NETADD(escape);
                    320:     return 1;
                    321: }
                    322:
1.69      jsg       323: static int
                    324: send_docmd(char *name)
1.1       deraadt   325: {
                    326:     return(send_tncmd(send_do, "do", name));
                    327: }
                    328:
1.69      jsg       329: static int
                    330: send_dontcmd(char *name)
1.1       deraadt   331: {
                    332:     return(send_tncmd(send_dont, "dont", name));
                    333: }
1.69      jsg       334:
                    335: static int
                    336: send_willcmd(char *name)
1.1       deraadt   337: {
                    338:     return(send_tncmd(send_will, "will", name));
                    339: }
1.69      jsg       340:
                    341: static int
                    342: send_wontcmd(char *name)
1.1       deraadt   343: {
                    344:     return(send_tncmd(send_wont, "wont", name));
                    345: }
                    346:
1.69      jsg       347: int
                    348: send_tncmd(void (*func)(int, int), char *cmd, char *name)
1.1       deraadt   349: {
                    350:     char **cpp;
                    351:     extern char *telopts[];
1.39      mpech     352:     int val = 0;
1.1       deraadt   353:
1.10      art       354:     if (isprefix(name, "help") || isprefix(name, "?")) {
1.39      mpech     355:        int col, len;
1.1       deraadt   356:
1.10      art       357:        printf("Usage: send %s <value|option>\r\n", cmd);
                    358:        printf("\"value\" must be from 0 to 255\r\n");
                    359:        printf("Valid options are:\r\n\t");
1.1       deraadt   360:
                    361:        col = 8;
                    362:        for (cpp = telopts; *cpp; cpp++) {
                    363:            len = strlen(*cpp) + 3;
                    364:            if (col + len > 65) {
1.10      art       365:                printf("\r\n\t");
1.1       deraadt   366:                col = 8;
                    367:            }
                    368:            printf(" \"%s\"", *cpp);
                    369:            col += len;
                    370:        }
1.10      art       371:        printf("\r\n");
1.1       deraadt   372:        return 0;
                    373:     }
                    374:     cpp = (char **)genget(name, telopts, sizeof(char *));
                    375:     if (Ambiguous(cpp)) {
1.10      art       376:        fprintf(stderr,"'%s': ambiguous argument ('send %s ?' for help).\r\n",
1.1       deraadt   377:                                        name, cmd);
                    378:        return 0;
                    379:     }
                    380:     if (cpp) {
                    381:        val = cpp - telopts;
                    382:     } else {
1.39      mpech     383:        char *cp = name;
1.1       deraadt   384:
                    385:        while (*cp >= '0' && *cp <= '9') {
                    386:            val *= 10;
                    387:            val += *cp - '0';
                    388:            cp++;
                    389:        }
                    390:        if (*cp != 0) {
1.10      art       391:            fprintf(stderr, "'%s': unknown argument ('send %s ?' for help).\r\n",
1.1       deraadt   392:                                        name, cmd);
                    393:            return 0;
                    394:        } else if (val < 0 || val > 255) {
1.10      art       395:            fprintf(stderr, "'%s': bad value ('send %s ?' for help).\r\n",
1.1       deraadt   396:                                        name, cmd);
                    397:            return 0;
                    398:        }
                    399:     }
                    400:     if (!connected) {
1.10      art       401:        printf("?Need to be connected first.\r\n");
1.1       deraadt   402:        return 0;
                    403:     }
                    404:     (*func)(val, 1);
                    405:     return 1;
                    406: }
                    407:
1.69      jsg       408: static int
                    409: send_help(void)
1.1       deraadt   410: {
                    411:     struct sendlist *s;        /* pointer to current command */
                    412:     for (s = Sendlist; s->name; s++) {
                    413:        if (s->help)
1.10      art       414:            printf("%-15s %s\r\n", s->name, s->help);
1.1       deraadt   415:     }
                    416:     return(0);
                    417: }
                    418: 
                    419: /*
                    420:  * The following are the routines and data structures referred
                    421:  * to by the arguments to the "toggle" command.
                    422:  */
                    423:
1.69      jsg       424: static int
                    425: lclchars(int unused)
1.1       deraadt   426: {
                    427:     donelclchars = 1;
                    428:     return 1;
                    429: }
                    430:
1.69      jsg       431: static int
                    432: togcrlf(int unused)
1.1       deraadt   433: {
                    434:     if (crlf) {
1.10      art       435:        printf("Will send carriage returns as telnet <CR><LF>.\r\n");
1.1       deraadt   436:     } else {
1.10      art       437:        printf("Will send carriage returns as telnet <CR><NUL>.\r\n");
1.1       deraadt   438:     }
                    439:     return 1;
                    440: }
                    441:
                    442: int binmode;
                    443:
1.69      jsg       444: static int
                    445: togbinary(int val)
1.1       deraadt   446: {
                    447:     donebinarytoggle = 1;
                    448:
                    449:     if (val >= 0) {
                    450:        binmode = val;
                    451:     } else {
                    452:        if (my_want_state_is_will(TELOPT_BINARY) &&
                    453:                                my_want_state_is_do(TELOPT_BINARY)) {
                    454:            binmode = 1;
                    455:        } else if (my_want_state_is_wont(TELOPT_BINARY) &&
                    456:                                my_want_state_is_dont(TELOPT_BINARY)) {
                    457:            binmode = 0;
                    458:        }
                    459:        val = binmode ? 0 : 1;
                    460:     }
                    461:
                    462:     if (val == 1) {
                    463:        if (my_want_state_is_will(TELOPT_BINARY) &&
                    464:                                        my_want_state_is_do(TELOPT_BINARY)) {
1.10      art       465:            printf("Already operating in binary mode with remote host.\r\n");
1.1       deraadt   466:        } else {
1.10      art       467:            printf("Negotiating binary mode with remote host.\r\n");
1.1       deraadt   468:            tel_enter_binary(3);
                    469:        }
                    470:     } else {
                    471:        if (my_want_state_is_wont(TELOPT_BINARY) &&
                    472:                                        my_want_state_is_dont(TELOPT_BINARY)) {
1.10      art       473:            printf("Already in network ascii mode with remote host.\r\n");
1.1       deraadt   474:        } else {
1.10      art       475:            printf("Negotiating network ascii mode with remote host.\r\n");
1.1       deraadt   476:            tel_leave_binary(3);
                    477:        }
                    478:     }
                    479:     return 1;
                    480: }
                    481:
1.69      jsg       482: static int
                    483: togrbinary(int val)
1.1       deraadt   484: {
                    485:     donebinarytoggle = 1;
                    486:
                    487:     if (val == -1)
                    488:        val = my_want_state_is_do(TELOPT_BINARY) ? 0 : 1;
                    489:
                    490:     if (val == 1) {
                    491:        if (my_want_state_is_do(TELOPT_BINARY)) {
1.10      art       492:            printf("Already receiving in binary mode.\r\n");
1.1       deraadt   493:        } else {
1.10      art       494:            printf("Negotiating binary mode on input.\r\n");
1.1       deraadt   495:            tel_enter_binary(1);
                    496:        }
                    497:     } else {
                    498:        if (my_want_state_is_dont(TELOPT_BINARY)) {
1.10      art       499:            printf("Already receiving in network ascii mode.\r\n");
1.1       deraadt   500:        } else {
1.10      art       501:            printf("Negotiating network ascii mode on input.\r\n");
1.1       deraadt   502:            tel_leave_binary(1);
                    503:        }
                    504:     }
                    505:     return 1;
                    506: }
                    507:
1.69      jsg       508: static int
                    509: togxbinary(int val)
1.1       deraadt   510: {
                    511:     donebinarytoggle = 1;
                    512:
                    513:     if (val == -1)
                    514:        val = my_want_state_is_will(TELOPT_BINARY) ? 0 : 1;
                    515:
                    516:     if (val == 1) {
                    517:        if (my_want_state_is_will(TELOPT_BINARY)) {
1.10      art       518:            printf("Already transmitting in binary mode.\r\n");
1.1       deraadt   519:        } else {
1.10      art       520:            printf("Negotiating binary mode on output.\r\n");
1.1       deraadt   521:            tel_enter_binary(2);
                    522:        }
                    523:     } else {
                    524:        if (my_want_state_is_wont(TELOPT_BINARY)) {
1.10      art       525:            printf("Already transmitting in network ascii mode.\r\n");
1.1       deraadt   526:        } else {
1.10      art       527:            printf("Negotiating network ascii mode on output.\r\n");
1.1       deraadt   528:            tel_leave_binary(2);
                    529:        }
                    530:     }
                    531:     return 1;
                    532: }
                    533:
                    534:
1.63      guenther  535: static int togglehelp(int);
1.1       deraadt   536:
                    537: struct togglelist {
1.63      guenther  538:     char       *name;                  /* name of toggle */
                    539:     char       *help;                  /* help message */
                    540:     int                (*handler)(int);        /* routine to do actual setting */
1.1       deraadt   541:     int                *variable;
                    542:     char       *actionexplanation;
1.8       robin     543:     int                needconnect;    /* Need to be connected */
1.1       deraadt   544: };
                    545:
                    546: static struct togglelist Togglelist[] = {
                    547:     { "autoflush",
                    548:        "flushing of output when sending interrupt characters",
                    549:            0,
                    550:                &autoflush,
1.10      art       551:                    "flush output when sending interrupt characters" },
1.1       deraadt   552:     { "autosynch",
                    553:        "automatic sending of interrupt characters in urgent mode",
                    554:            0,
                    555:                &autosynch,
1.10      art       556:                    "send interrupt characters in urgent mode" },
1.1       deraadt   557:     { "autologin",
1.57      guenther  558:        "automatic sending of login name",
1.1       deraadt   559:            0,
                    560:                &autologin,
1.57      guenther  561:                    "send login name" },
1.1       deraadt   562:     { "skiprc",
                    563:        "don't read ~/.telnetrc file",
                    564:            0,
                    565:                &skiprc,
1.10      art       566:                    "skip reading of ~/.telnetrc file" },
1.1       deraadt   567:     { "binary",
                    568:        "sending and receiving of binary data",
                    569:            togbinary,
                    570:                0,
1.10      art       571:                    0 },
1.1       deraadt   572:     { "inbinary",
                    573:        "receiving of binary data",
                    574:            togrbinary,
                    575:                0,
1.10      art       576:                    0 },
1.1       deraadt   577:     { "outbinary",
                    578:        "sending of binary data",
                    579:            togxbinary,
                    580:                0,
1.10      art       581:                    0 },
1.1       deraadt   582:     { "crlf",
                    583:        "sending carriage returns as telnet <CR><LF>",
                    584:            togcrlf,
                    585:                &crlf,
1.10      art       586:                    0 },
1.1       deraadt   587:     { "crmod",
                    588:        "mapping of received carriage returns",
                    589:            0,
                    590:                &crmod,
1.10      art       591:                    "map carriage return on output" },
1.1       deraadt   592:     { "localchars",
                    593:        "local recognition of certain control characters",
                    594:            lclchars,
                    595:                &localchars,
1.10      art       596:                    "recognize certain control characters" },
1.8       robin     597:     { " ", "", 0, 0 },         /* empty line */
1.1       deraadt   598:     { "netdata",
                    599:        "printing of hexadecimal network data (debugging)",
                    600:            0,
                    601:                &netdata,
1.10      art       602:                    "print hexadecimal representation of network traffic" },
1.1       deraadt   603:     { "prettydump",
                    604:        "output of \"netdata\" to user readable format (debugging)",
                    605:            0,
                    606:                &prettydump,
1.10      art       607:                    "print user readable output for \"netdata\"" },
1.1       deraadt   608:     { "options",
                    609:        "viewing of options processing (debugging)",
                    610:            0,
                    611:                &showoptions,
1.10      art       612:                    "show option processing" },
1.1       deraadt   613:     { "termdata",
                    614:        "(debugging) toggle printing of hexadecimal terminal data",
                    615:            0,
                    616:                &termdata,
1.10      art       617:                    "print hexadecimal representation of terminal traffic" },
1.1       deraadt   618:     { "?",
                    619:        0,
1.10      art       620:            togglehelp },
1.1       deraadt   621:     { "help",
                    622:        0,
1.10      art       623:            togglehelp },
1.1       deraadt   624:     { 0 }
                    625: };
                    626:
1.69      jsg       627: static int
1.63      guenther  628: togglehelp(int unused)
1.1       deraadt   629: {
                    630:     struct togglelist *c;
                    631:
                    632:     for (c = Togglelist; c->name; c++) {
                    633:        if (c->help) {
                    634:            if (*c->help)
1.10      art       635:                printf("%-15s toggle %s\r\n", c->name, c->help);
1.1       deraadt   636:            else
1.10      art       637:                printf("\r\n");
1.1       deraadt   638:        }
                    639:     }
1.10      art       640:     printf("\r\n");
                    641:     printf("%-15s %s\r\n", "?", "display help information");
1.1       deraadt   642:     return 0;
                    643: }
                    644:
1.69      jsg       645: static void
                    646: settogglehelp(int set)
1.1       deraadt   647: {
                    648:     struct togglelist *c;
                    649:
                    650:     for (c = Togglelist; c->name; c++) {
                    651:        if (c->help) {
                    652:            if (*c->help)
1.10      art       653:                printf("%-15s %s %s\r\n", c->name, set ? "enable" : "disable",
1.1       deraadt   654:                                                c->help);
                    655:            else
1.10      art       656:                printf("\r\n");
1.1       deraadt   657:        }
                    658:     }
                    659: }
                    660:
                    661: #define        GETTOGGLE(name) (struct togglelist *) \
                    662:                genget(name, (char **) Togglelist, sizeof(struct togglelist))
                    663:
1.69      jsg       664: static int
                    665: toggle(int argc, char *argv[])
1.1       deraadt   666: {
                    667:     int retval = 1;
                    668:     char *name;
                    669:     struct togglelist *c;
                    670:
                    671:     if (argc < 2) {
                    672:        fprintf(stderr,
1.10      art       673:            "Need an argument to 'toggle' command.  'toggle ?' for help.\r\n");
1.1       deraadt   674:        return 0;
                    675:     }
                    676:     argc--;
                    677:     argv++;
                    678:     while (argc--) {
                    679:        name = *argv++;
                    680:        c = GETTOGGLE(name);
                    681:        if (Ambiguous(c)) {
1.10      art       682:            fprintf(stderr, "'%s': ambiguous argument ('toggle ?' for help).\r\n",
1.1       deraadt   683:                                        name);
                    684:            return 0;
                    685:        } else if (c == 0) {
1.10      art       686:            fprintf(stderr, "'%s': unknown argument ('toggle ?' for help).\r\n",
1.1       deraadt   687:                                        name);
                    688:            return 0;
1.8       robin     689:        } else if (!connected && c->needconnect) {
1.10      art       690:            printf("?Need to be connected first.\r\n");
                    691:            printf("'send ?' for help\r\n");
1.8       robin     692:            return 0;
1.1       deraadt   693:        } else {
                    694:            if (c->variable) {
                    695:                *c->variable = !*c->variable;           /* invert it */
                    696:                if (c->actionexplanation) {
1.10      art       697:                    printf("%s %s.\r\n", *c->variable? "Will" : "Won't",
1.1       deraadt   698:                                                        c->actionexplanation);
                    699:                }
                    700:            }
                    701:            if (c->handler) {
                    702:                retval &= (*c->handler)(-1);
                    703:            }
                    704:        }
                    705:     }
                    706:     return retval;
                    707: }
                    708: 
                    709: /*
                    710:  * The following perform the "set" command.
                    711:  */
                    712:
1.10      art       713: struct termios new_tc = { 0 };
1.1       deraadt   714:
                    715: struct setlist {
                    716:     char *name;                                /* name */
                    717:     char *help;                                /* help information */
1.70      guenther  718:     void (*handler)(const char *);
1.1       deraadt   719:     cc_t *charp;                       /* where it is located at */
                    720: };
                    721:
                    722: static struct setlist Setlist[] = {
                    723: #ifdef KLUDGELINEMODE
                    724:     { "echo",  "character to toggle local echoing on/off", 0, &echoc },
                    725: #endif
                    726:     { "escape",        "character to escape back to telnet command mode", 0, &escape },
                    727:     { "rlogin", "rlogin escape character", 0, &rlogin },
                    728:     { " ", "" },
                    729:     { " ", "The following need 'localchars' to be toggled true", 0, 0 },
1.10      art       730:     { "flushoutput", "character to cause an Abort Output", 0, &termFlushChar },
                    731:     { "interrupt", "character to cause an Interrupt Process", 0, &termIntChar },
                    732:     { "quit",  "character to cause an Abort process", 0, &termQuitChar },
                    733:     { "eof",   "character to cause an EOF ", 0, &termEofChar },
1.1       deraadt   734:     { " ", "" },
                    735:     { " ", "The following are for local editing in linemode", 0, 0 },
1.10      art       736:     { "erase", "character to use to erase a character", 0, &termEraseChar },
                    737:     { "kill",  "character to use to erase a line", 0, &termKillChar },
                    738:     { "lnext", "character to use for literal next", 0, &termLiteralNextChar },
                    739:     { "susp",  "character to cause a Suspend Process", 0, &termSuspChar },
                    740:     { "reprint", "character to use for line reprint", 0, &termRprntChar },
                    741:     { "worderase", "character to use to erase a word", 0, &termWerasChar },
                    742:     { "start", "character to use for XON", 0, &termStartChar },
                    743:     { "stop",  "character to use for XOFF", 0, &termStopChar },
                    744:     { "forw1", "alternate end of line character", 0, &termForw1Char },
                    745:     { "forw2", "alternate end of line character", 0, &termForw2Char },
                    746:     { "ayt",   "alternate AYT character", 0, &termAytChar },
1.1       deraadt   747:     { 0 }
                    748: };
                    749:
1.69      jsg       750: static struct setlist *
                    751: getset(char *name)
1.1       deraadt   752: {
                    753:     return (struct setlist *)
                    754:                genget(name, (char **) Setlist, sizeof(struct setlist));
                    755: }
                    756:
1.69      jsg       757: void
                    758: set_escape_char(char *s)
1.1       deraadt   759: {
                    760:        if (rlogin != _POSIX_VDISABLE) {
                    761:                rlogin = (s && *s) ? special(s) : _POSIX_VDISABLE;
1.10      art       762:                printf("Telnet rlogin escape character is '%s'.\r\n",
1.1       deraadt   763:                                        control(rlogin));
                    764:        } else {
                    765:                escape = (s && *s) ? special(s) : _POSIX_VDISABLE;
1.10      art       766:                printf("Telnet escape character is '%s'.\r\n", control(escape));
1.1       deraadt   767:        }
                    768: }
                    769:
1.69      jsg       770: static int
                    771: setcmd(int argc, char *argv[])
1.1       deraadt   772: {
                    773:     int value;
                    774:     struct setlist *ct;
                    775:     struct togglelist *c;
                    776:
                    777:     if (argc < 2 || argc > 3) {
1.10      art       778:        printf("Format is 'set Name Value'\r\n'set ?' for help.\r\n");
1.1       deraadt   779:        return 0;
                    780:     }
                    781:     if ((argc == 2) && (isprefix(argv[1], "?") || isprefix(argv[1], "help"))) {
                    782:        for (ct = Setlist; ct->name; ct++)
1.10      art       783:            printf("%-15s %s\r\n", ct->name, ct->help);
                    784:        printf("\r\n");
1.1       deraadt   785:        settogglehelp(1);
1.10      art       786:        printf("%-15s %s\r\n", "?", "display help information");
1.1       deraadt   787:        return 0;
                    788:     }
                    789:
                    790:     ct = getset(argv[1]);
                    791:     if (ct == 0) {
                    792:        c = GETTOGGLE(argv[1]);
                    793:        if (c == 0) {
1.10      art       794:            fprintf(stderr, "'%s': unknown argument ('set ?' for help).\r\n",
1.1       deraadt   795:                        argv[1]);
                    796:            return 0;
                    797:        } else if (Ambiguous(c)) {
1.10      art       798:            fprintf(stderr, "'%s': ambiguous argument ('set ?' for help).\r\n",
1.1       deraadt   799:                        argv[1]);
                    800:            return 0;
1.8       robin     801:        } else if (!connected && c->needconnect) {
1.10      art       802:            printf("?Need to be connected first.\r\n");
                    803:            printf("'send ?' for help\r\n");
1.8       robin     804:            return 0;
1.1       deraadt   805:        }
1.8       robin     806:
1.1       deraadt   807:        if (c->variable) {
                    808:            if ((argc == 2) || (strcmp("on", argv[2]) == 0))
                    809:                *c->variable = 1;
                    810:            else if (strcmp("off", argv[2]) == 0)
                    811:                *c->variable = 0;
                    812:            else {
1.10      art       813:                printf("Format is 'set togglename [on|off]'\r\n'set ?' for help.\r\n");
1.1       deraadt   814:                return 0;
                    815:            }
                    816:            if (c->actionexplanation) {
1.10      art       817:                printf("%s %s.\r\n", *c->variable? "Will" : "Won't",
1.1       deraadt   818:                                                        c->actionexplanation);
                    819:            }
                    820:        }
                    821:        if (c->handler)
                    822:            (*c->handler)(1);
                    823:     } else if (argc != 3) {
1.10      art       824:        printf("Format is 'set Name Value'\r\n'set ?' for help.\r\n");
1.1       deraadt   825:        return 0;
                    826:     } else if (Ambiguous(ct)) {
1.10      art       827:        fprintf(stderr, "'%s': ambiguous argument ('set ?' for help).\r\n",
1.1       deraadt   828:                        argv[1]);
                    829:        return 0;
                    830:     } else if (ct->handler) {
                    831:        (*ct->handler)(argv[2]);
1.10      art       832:        printf("%s set to \"%s\".\r\n", ct->name, (char *)ct->charp);
1.1       deraadt   833:     } else {
                    834:        if (strcmp("off", argv[2])) {
                    835:            value = special(argv[2]);
                    836:        } else {
                    837:            value = _POSIX_VDISABLE;
                    838:        }
                    839:        *(ct->charp) = (cc_t)value;
1.10      art       840:        printf("%s character is '%s'.\r\n", ct->name, control(*(ct->charp)));
1.1       deraadt   841:     }
                    842:     slc_check();
                    843:     return 1;
                    844: }
                    845:
1.69      jsg       846: static int
                    847: unsetcmd(int argc, char *argv[])
1.1       deraadt   848: {
                    849:     struct setlist *ct;
                    850:     struct togglelist *c;
1.39      mpech     851:     char *name;
1.1       deraadt   852:
                    853:     if (argc < 2) {
                    854:        fprintf(stderr,
1.10      art       855:            "Need an argument to 'unset' command.  'unset ?' for help.\r\n");
1.1       deraadt   856:        return 0;
                    857:     }
                    858:     if (isprefix(argv[1], "?") || isprefix(argv[1], "help")) {
                    859:        for (ct = Setlist; ct->name; ct++)
1.10      art       860:            printf("%-15s %s\r\n", ct->name, ct->help);
                    861:        printf("\r\n");
1.1       deraadt   862:        settogglehelp(0);
1.10      art       863:        printf("%-15s %s\r\n", "?", "display help information");
1.1       deraadt   864:        return 0;
                    865:     }
                    866:
                    867:     argc--;
                    868:     argv++;
                    869:     while (argc--) {
                    870:        name = *argv++;
                    871:        ct = getset(name);
                    872:        if (ct == 0) {
                    873:            c = GETTOGGLE(name);
                    874:            if (c == 0) {
1.10      art       875:                fprintf(stderr, "'%s': unknown argument ('unset ?' for help).\r\n",
1.1       deraadt   876:                        name);
                    877:                return 0;
                    878:            } else if (Ambiguous(c)) {
1.10      art       879:                fprintf(stderr, "'%s': ambiguous argument ('unset ?' for help).\r\n",
1.1       deraadt   880:                        name);
                    881:                return 0;
                    882:            }
                    883:            if (c->variable) {
                    884:                *c->variable = 0;
                    885:                if (c->actionexplanation) {
1.10      art       886:                    printf("%s %s.\r\n", *c->variable? "Will" : "Won't",
1.1       deraadt   887:                                                        c->actionexplanation);
                    888:                }
                    889:            }
                    890:            if (c->handler)
                    891:                (*c->handler)(0);
                    892:        } else if (Ambiguous(ct)) {
1.10      art       893:            fprintf(stderr, "'%s': ambiguous argument ('unset ?' for help).\r\n",
1.1       deraadt   894:                        name);
                    895:            return 0;
                    896:        } else if (ct->handler) {
1.63      guenther  897:            (*ct->handler)(NULL);
1.10      art       898:            printf("%s reset to \"%s\".\r\n", ct->name, (char *)ct->charp);
1.1       deraadt   899:        } else {
                    900:            *(ct->charp) = _POSIX_VDISABLE;
1.10      art       901:            printf("%s character is '%s'.\r\n", ct->name, control(*(ct->charp)));
1.1       deraadt   902:        }
                    903:     }
                    904:     return 1;
                    905: }
                    906: 
                    907: /*
                    908:  * The following are the data structures and routines for the
                    909:  * 'mode' command.
                    910:  */
                    911: #ifdef KLUDGELINEMODE
1.69      jsg       912: static int
                    913: dokludgemode(int unused)
1.1       deraadt   914: {
                    915:     kludgelinemode = 1;
                    916:     send_wont(TELOPT_LINEMODE, 1);
                    917:     send_dont(TELOPT_SGA, 1);
                    918:     send_dont(TELOPT_ECHO, 1);
1.10      art       919:     return 1;
1.1       deraadt   920: }
                    921: #endif
                    922:
1.69      jsg       923: static int
                    924: dolinemode(int unused)
1.1       deraadt   925: {
                    926: #ifdef KLUDGELINEMODE
                    927:     if (kludgelinemode)
                    928:        send_dont(TELOPT_SGA, 1);
                    929: #endif
                    930:     send_will(TELOPT_LINEMODE, 1);
                    931:     send_dont(TELOPT_ECHO, 1);
                    932:     return 1;
                    933: }
                    934:
1.69      jsg       935: static int
                    936: docharmode(int unused)
1.1       deraadt   937: {
                    938: #ifdef KLUDGELINEMODE
                    939:     if (kludgelinemode)
                    940:        send_do(TELOPT_SGA, 1);
                    941:     else
                    942: #endif
                    943:     send_wont(TELOPT_LINEMODE, 1);
                    944:     send_do(TELOPT_ECHO, 1);
                    945:     return 1;
                    946: }
                    947:
1.69      jsg       948: static int
                    949: dolmmode(int bit, int on)
1.1       deraadt   950: {
                    951:     unsigned char c;
                    952:
                    953:     if (my_want_state_is_wont(TELOPT_LINEMODE)) {
1.10      art       954:        printf("?Need to have LINEMODE option enabled first.\r\n");
                    955:        printf("'mode ?' for help.\r\n");
1.1       deraadt   956:        return 0;
                    957:     }
                    958:
                    959:     if (on)
                    960:        c = (linemode | bit);
                    961:     else
                    962:        c = (linemode & ~bit);
                    963:     lm_mode(&c, 1, 1);
                    964:     return 1;
                    965: }
                    966:
1.69      jsg       967: int
                    968: tn_setmode(int bit)
1.1       deraadt   969: {
                    970:     return dolmmode(bit, 1);
                    971: }
                    972:
1.69      jsg       973: int
                    974: tn_clearmode(int bit)
1.1       deraadt   975: {
                    976:     return dolmmode(bit, 0);
                    977: }
                    978:
                    979: struct modelist {
                    980:        char    *name;          /* command name */
                    981:        char    *help;          /* help string */
1.63      guenther  982:        int     (*handler)(int);/* routine which executes command */
1.1       deraadt   983:        int     needconnect;    /* Do we need to be connected to execute? */
                    984:        int     arg1;
                    985: };
                    986:
1.63      guenther  987: static int modehelp(int);
1.1       deraadt   988:
                    989: static struct modelist ModeList[] = {
                    990:     { "character", "Disable LINEMODE option",  docharmode, 1 },
                    991: #ifdef KLUDGELINEMODE
                    992:     { "",      "(or disable obsolete line-by-line mode)", 0 },
                    993: #endif
                    994:     { "line",  "Enable LINEMODE option",       dolinemode, 1 },
                    995: #ifdef KLUDGELINEMODE
                    996:     { "",      "(or enable obsolete line-by-line mode)", 0 },
                    997: #endif
                    998:     { "", "", 0 },
                    999:     { "",      "These require the LINEMODE option to be enabled", 0 },
1.10      art      1000:     { "isig",  "Enable signal trapping",       tn_setmode, 1, MODE_TRAPSIG },
                   1001:     { "+isig", 0,                              tn_setmode, 1, MODE_TRAPSIG },
                   1002:     { "-isig", "Disable signal trapping",      tn_clearmode, 1, MODE_TRAPSIG },
                   1003:     { "edit",  "Enable character editing",     tn_setmode, 1, MODE_EDIT },
                   1004:     { "+edit", 0,                              tn_setmode, 1, MODE_EDIT },
                   1005:     { "-edit", "Disable character editing",    tn_clearmode, 1, MODE_EDIT },
                   1006:     { "softtabs", "Enable tab expansion",      tn_setmode, 1, MODE_SOFT_TAB },
                   1007:     { "+softtabs", 0,                          tn_setmode, 1, MODE_SOFT_TAB },
                   1008:     { "-softtabs", "Disable character editing",        tn_clearmode, 1, MODE_SOFT_TAB },
                   1009:     { "litecho", "Enable literal character echo", tn_setmode, 1, MODE_LIT_ECHO },
                   1010:     { "+litecho", 0,                           tn_setmode, 1, MODE_LIT_ECHO },
                   1011:     { "-litecho", "Disable literal character echo", tn_clearmode, 1, MODE_LIT_ECHO },
1.1       deraadt  1012:     { "help",  0,                              modehelp, 0 },
                   1013: #ifdef KLUDGELINEMODE
                   1014:     { "kludgeline", 0,                         dokludgemode, 1 },
                   1015: #endif
                   1016:     { "", "", 0 },
                   1017:     { "?",     "Print help information",       modehelp, 0 },
                   1018:     { 0 },
                   1019: };
                   1020:
1.69      jsg      1021: static int
1.63      guenther 1022: modehelp(int unused)
1.1       deraadt  1023: {
                   1024:     struct modelist *mt;
                   1025:
1.10      art      1026:     printf("format is:  'mode Mode', where 'Mode' is one of:\r\n\r\n");
1.1       deraadt  1027:     for (mt = ModeList; mt->name; mt++) {
                   1028:        if (mt->help) {
                   1029:            if (*mt->help)
1.10      art      1030:                printf("%-15s %s\r\n", mt->name, mt->help);
1.1       deraadt  1031:            else
1.10      art      1032:                printf("\r\n");
1.1       deraadt  1033:        }
                   1034:     }
                   1035:     return 0;
                   1036: }
                   1037:
                   1038: #define        GETMODECMD(name) (struct modelist *) \
                   1039:                genget(name, (char **) ModeList, sizeof(struct modelist))
                   1040:
1.69      jsg      1041: static int
                   1042: modecmd(int argc, char *argv[])
1.1       deraadt  1043: {
                   1044:     struct modelist *mt;
                   1045:
                   1046:     if (argc != 2) {
1.10      art      1047:        printf("'mode' command requires an argument\r\n");
                   1048:        printf("'mode ?' for help.\r\n");
1.1       deraadt  1049:     } else if ((mt = GETMODECMD(argv[1])) == 0) {
1.10      art      1050:        fprintf(stderr, "Unknown mode '%s' ('mode ?' for help).\r\n", argv[1]);
1.1       deraadt  1051:     } else if (Ambiguous(mt)) {
1.10      art      1052:        fprintf(stderr, "Ambiguous mode '%s' ('mode ?' for help).\r\n", argv[1]);
1.1       deraadt  1053:     } else if (mt->needconnect && !connected) {
1.10      art      1054:        printf("?Need to be connected first.\r\n");
                   1055:        printf("'mode ?' for help.\r\n");
1.1       deraadt  1056:     } else if (mt->handler) {
                   1057:        return (*mt->handler)(mt->arg1);
                   1058:     }
                   1059:     return 0;
                   1060: }
                   1061: 
                   1062: /*
                   1063:  * The following data structures and routines implement the
                   1064:  * "display" command.
                   1065:  */
                   1066:
1.69      jsg      1067: static int
                   1068: display(int argc, char *argv[])
1.1       deraadt  1069: {
                   1070:     struct togglelist *tl;
                   1071:     struct setlist *sl;
                   1072:
                   1073: #define        dotog(tl)       if (tl->variable && tl->actionexplanation) { \
                   1074:                            if (*tl->variable) { \
                   1075:                                printf("will"); \
                   1076:                            } else { \
                   1077:                                printf("won't"); \
                   1078:                            } \
1.10      art      1079:                            printf(" %s.\r\n", tl->actionexplanation); \
1.1       deraadt  1080:                        }
                   1081:
                   1082: #define        doset(sl)   if (sl->name && *sl->name != ' ') { \
                   1083:                        if (sl->handler == 0) \
1.10      art      1084:                            printf("%-15s [%s]\r\n", sl->name, control(*sl->charp)); \
1.1       deraadt  1085:                        else \
1.10      art      1086:                            printf("%-15s \"%s\"\r\n", sl->name, (char *)sl->charp); \
1.1       deraadt  1087:                    }
                   1088:
                   1089:     if (argc == 1) {
                   1090:        for (tl = Togglelist; tl->name; tl++) {
                   1091:            dotog(tl);
                   1092:        }
1.10      art      1093:        printf("\r\n");
1.1       deraadt  1094:        for (sl = Setlist; sl->name; sl++) {
                   1095:            doset(sl);
                   1096:        }
                   1097:     } else {
                   1098:        int i;
                   1099:
                   1100:        for (i = 1; i < argc; i++) {
                   1101:            sl = getset(argv[i]);
                   1102:            tl = GETTOGGLE(argv[i]);
                   1103:            if (Ambiguous(sl) || Ambiguous(tl)) {
1.10      art      1104:                printf("?Ambiguous argument '%s'.\r\n", argv[i]);
1.1       deraadt  1105:                return 0;
                   1106:            } else if (!sl && !tl) {
1.10      art      1107:                printf("?Unknown argument '%s'.\r\n", argv[i]);
1.1       deraadt  1108:                return 0;
                   1109:            } else {
                   1110:                if (tl) {
                   1111:                    dotog(tl);
                   1112:                }
                   1113:                if (sl) {
                   1114:                    doset(sl);
                   1115:                }
                   1116:            }
                   1117:        }
                   1118:     }
                   1119: /*@*/optionstatus();
                   1120:     return 1;
                   1121: #undef doset
                   1122: #undef dotog
                   1123: }
1.10      art      1124:
1.1       deraadt  1125: /*
                   1126:  * The following are the data structures, and many of the routines,
                   1127:  * relating to command processing.
                   1128:  */
                   1129:
                   1130: /*
                   1131:  * Set the escape character.
                   1132:  */
1.69      jsg      1133: static int
                   1134: setescape(int argc, char *argv[])
1.1       deraadt  1135: {
1.39      mpech    1136:        char *arg;
1.1       deraadt  1137:        char buf[50];
                   1138:
                   1139:        printf(
1.10      art      1140:            "Deprecated usage - please use 'set escape%s%s' in the future.\r\n",
1.1       deraadt  1141:                                (argc > 2)? " ":"", (argc > 2)? argv[1]: "");
                   1142:        if (argc > 2)
                   1143:                arg = argv[1];
                   1144:        else {
                   1145:                printf("new escape character: ");
                   1146:                (void) fgets(buf, sizeof(buf), stdin);
                   1147:                arg = buf;
                   1148:        }
                   1149:        if (arg[0] != '\0')
                   1150:                escape = arg[0];
1.56      guenther 1151:        printf("Escape character is '%s'.\r\n", control(escape));
1.1       deraadt  1152:        (void) fflush(stdout);
                   1153:        return 1;
                   1154: }
                   1155:
1.69      jsg      1156: static int
                   1157: togcrmod(int unused1, char *unused2[])
1.1       deraadt  1158: {
                   1159:     crmod = !crmod;
1.10      art      1160:     printf("Deprecated usage - please use 'toggle crmod' in the future.\r\n");
                   1161:     printf("%s map carriage return on output.\r\n", crmod ? "Will" : "Won't");
1.1       deraadt  1162:     (void) fflush(stdout);
                   1163:     return 1;
                   1164: }
                   1165:
1.69      jsg      1166: int
                   1167: telnetsuspend(int unused1, char *unused2[])
1.1       deraadt  1168: {
                   1169:     setcommandmode();
                   1170:     {
                   1171:        long oldrows, oldcols, newrows, newcols, err;
                   1172:
                   1173:        err = (TerminalWindowSize(&oldrows, &oldcols) == 0) ? 1 : 0;
                   1174:        (void) kill(0, SIGTSTP);
                   1175:        /*
                   1176:         * If we didn't get the window size before the SUSPEND, but we
1.3       niklas   1177:         * can get them now (?), then send the NAWS to make sure that
1.1       deraadt  1178:         * we are set up for the right window size.
                   1179:         */
                   1180:        if (TerminalWindowSize(&newrows, &newcols) && connected &&
                   1181:            (err || ((oldrows != newrows) || (oldcols != newcols)))) {
                   1182:                sendnaws();
                   1183:        }
                   1184:     }
                   1185:     /* reget parameters in case they were changed */
                   1186:     TerminalSaveState();
                   1187:     setconnmode(0);
                   1188:     return 1;
                   1189: }
                   1190:
1.68      guenther 1191: static void
                   1192: close_connection(void)
                   1193: {
                   1194:        if (connected) {
1.80    ! jca      1195:                (void) shutdown(net, SHUT_RDWR);
1.68      guenther 1196:                printf("Connection closed.\r\n");
                   1197:                (void)close(net);
                   1198:                connected = 0;
                   1199:                resettermname = 1;
                   1200:                /* reset options */
                   1201:                tninit();
                   1202:        }
                   1203: }
                   1204:
                   1205: static int
1.69      jsg      1206: bye(int argc, char *argv[])
1.1       deraadt  1207: {
1.68      guenther 1208:        close_connection();
1.1       deraadt  1209:        longjmp(toplevel, 1);
                   1210: }
                   1211:
1.63      guenther 1212: void
1.59      guenther 1213: quit(void)
1.1       deraadt  1214: {
1.68      guenther 1215:        close_connection();
1.1       deraadt  1216:        Exit(0);
                   1217: }
                   1218:
1.63      guenther 1219: static int
                   1220: quitcmd(int unused1, char *unused2[])
                   1221: {
                   1222:        quit();
                   1223: }
                   1224:
1.69      jsg      1225: static int
                   1226: logout(int unused1, char *unused2[])
1.1       deraadt  1227: {
                   1228:        send_do(TELOPT_LOGOUT, 1);
                   1229:        (void) netflush();
                   1230:        return 1;
                   1231: }
                   1232:
                   1233: 
                   1234: /*
                   1235:  * The SLC command.
                   1236:  */
                   1237:
                   1238: struct slclist {
                   1239:        char    *name;
                   1240:        char    *help;
1.63      guenther 1241:        void    (*handler)(int);
1.1       deraadt  1242:        int     arg;
                   1243: };
                   1244:
1.63      guenther 1245: static void slc_help(int);
1.1       deraadt  1246:
                   1247: struct slclist SlcList[] = {
                   1248:     { "export",        "Use local special character definitions",
                   1249:                                                slc_mode_export,        0 },
                   1250:     { "import",        "Use remote special character definitions",
                   1251:                                                slc_mode_import,        1 },
                   1252:     { "check", "Verify remote special character definitions",
                   1253:                                                slc_mode_import,        0 },
                   1254:     { "help",  0,                              slc_help,               0 },
                   1255:     { "?",     "Print help information",       slc_help,               0 },
                   1256:     { 0 },
                   1257: };
                   1258:
1.69      jsg      1259: static void
1.63      guenther 1260: slc_help(int unused)
1.1       deraadt  1261: {
                   1262:     struct slclist *c;
                   1263:
                   1264:     for (c = SlcList; c->name; c++) {
                   1265:        if (c->help) {
                   1266:            if (*c->help)
1.10      art      1267:                printf("%-15s %s\r\n", c->name, c->help);
1.1       deraadt  1268:            else
1.10      art      1269:                printf("\r\n");
1.1       deraadt  1270:        }
                   1271:     }
                   1272: }
                   1273:
1.69      jsg      1274: static struct slclist *
                   1275: getslc(char *name)
1.1       deraadt  1276: {
                   1277:     return (struct slclist *)
                   1278:                genget(name, (char **) SlcList, sizeof(struct slclist));
                   1279: }
                   1280:
1.69      jsg      1281: static int
                   1282: slccmd(int argc, char *argv[])
1.1       deraadt  1283: {
                   1284:     struct slclist *c;
                   1285:
                   1286:     if (argc != 2) {
                   1287:        fprintf(stderr,
1.10      art      1288:            "Need an argument to 'slc' command.  'slc ?' for help.\r\n");
1.1       deraadt  1289:        return 0;
                   1290:     }
                   1291:     c = getslc(argv[1]);
                   1292:     if (c == 0) {
1.10      art      1293:        fprintf(stderr, "'%s': unknown argument ('slc ?' for help).\r\n",
1.1       deraadt  1294:                                argv[1]);
1.3       niklas   1295:        return 0;
1.1       deraadt  1296:     }
                   1297:     if (Ambiguous(c)) {
1.10      art      1298:        fprintf(stderr, "'%s': ambiguous argument ('slc ?' for help).\r\n",
1.1       deraadt  1299:                                argv[1]);
1.3       niklas   1300:        return 0;
1.1       deraadt  1301:     }
                   1302:     (*c->handler)(c->arg);
                   1303:     slcstate();
                   1304:     return 1;
                   1305: }
                   1306: 
                   1307: /*
                   1308:  * The ENVIRON command.
                   1309:  */
                   1310:
                   1311: struct envlist {
                   1312:        char    *name;
                   1313:        char    *help;
                   1314:        void    (*handler)();
                   1315:        int     narg;
                   1316: };
                   1317:
1.41      millert  1318: static void    env_help(void);
1.70      guenther 1319: static void    env_undefine(const char *);
                   1320: static void    env_export(const char *);
                   1321: static void    env_unexport(const char *);
                   1322: static void    env_send(const char *);
1.67      guenther 1323: static void    env_list(void);
1.70      guenther 1324: static struct env_lst *env_find(const char *var);
1.1       deraadt  1325:
                   1326: struct envlist EnvList[] = {
                   1327:     { "define",        "Define an environment variable",
                   1328:                                                (void (*)())env_define, 2 },
                   1329:     { "undefine", "Undefine an environment variable",
                   1330:                                                env_undefine,   1 },
                   1331:     { "export",        "Mark an environment variable for automatic export",
                   1332:                                                env_export,     1 },
                   1333:     { "unexport", "Don't mark an environment variable for automatic export",
                   1334:                                                env_unexport,   1 },
                   1335:     { "send",  "Send an environment variable", env_send,       1 },
                   1336:     { "list",  "List the current environment variables",
                   1337:                                                env_list,       0 },
                   1338:     { "help",  0,                              env_help,               0 },
                   1339:     { "?",     "Print help information",       env_help,               0 },
                   1340:     { 0 },
                   1341: };
                   1342:
1.67      guenther 1343: static void
1.69      jsg      1344: env_help(void)
1.1       deraadt  1345: {
                   1346:     struct envlist *c;
                   1347:
                   1348:     for (c = EnvList; c->name; c++) {
                   1349:        if (c->help) {
                   1350:            if (*c->help)
1.10      art      1351:                printf("%-15s %s\r\n", c->name, c->help);
1.1       deraadt  1352:            else
1.10      art      1353:                printf("\r\n");
1.1       deraadt  1354:        }
                   1355:     }
                   1356: }
                   1357:
1.67      guenther 1358: static struct envlist *
1.69      jsg      1359: getenvcmd(char *name)
1.1       deraadt  1360: {
                   1361:     return (struct envlist *)
                   1362:                genget(name, (char **) EnvList, sizeof(struct envlist));
                   1363: }
                   1364:
1.67      guenther 1365: static int
1.69      jsg      1366: env_cmd(int argc, char *argv[])
1.1       deraadt  1367: {
                   1368:     struct envlist *c;
                   1369:
                   1370:     if (argc < 2) {
                   1371:        fprintf(stderr,
1.10      art      1372:            "Need an argument to 'environ' command.  'environ ?' for help.\r\n");
1.1       deraadt  1373:        return 0;
                   1374:     }
                   1375:     c = getenvcmd(argv[1]);
                   1376:     if (c == 0) {
1.10      art      1377:        fprintf(stderr, "'%s': unknown argument ('environ ?' for help).\r\n",
1.1       deraadt  1378:                                argv[1]);
1.3       niklas   1379:        return 0;
1.1       deraadt  1380:     }
                   1381:     if (Ambiguous(c)) {
1.10      art      1382:        fprintf(stderr, "'%s': ambiguous argument ('environ ?' for help).\r\n",
1.1       deraadt  1383:                                argv[1]);
1.3       niklas   1384:        return 0;
1.1       deraadt  1385:     }
                   1386:     if (c->narg + 2 != argc) {
                   1387:        fprintf(stderr,
1.10      art      1388:            "Need %s%d argument%s to 'environ %s' command.  'environ ?' for help.\r\n",
1.1       deraadt  1389:                c->narg < argc + 2 ? "only " : "",
                   1390:                c->narg, c->narg == 1 ? "" : "s", c->name);
                   1391:        return 0;
                   1392:     }
                   1393:     (*c->handler)(argv[2], argv[3]);
                   1394:     return 1;
                   1395: }
                   1396:
                   1397: struct env_lst {
                   1398:        struct env_lst *next;   /* pointer to next structure */
                   1399:        struct env_lst *prev;   /* pointer to previous structure */
1.70      guenther 1400:        char *var;              /* pointer to variable name */
                   1401:        char *value;            /* pointer to variable value */
1.1       deraadt  1402:        int export;             /* 1 -> export with default list of variables */
                   1403:        int welldefined;        /* A well defined variable */
                   1404: };
                   1405:
                   1406: struct env_lst envlisthead;
                   1407:
1.67      guenther 1408: static struct env_lst *
1.70      guenther 1409: env_find(const char *var)
1.1       deraadt  1410: {
1.39      mpech    1411:        struct env_lst *ep;
1.1       deraadt  1412:
                   1413:        for (ep = envlisthead.next; ep; ep = ep->next) {
1.70      guenther 1414:                if (strcmp(ep->var, var) == 0)
1.1       deraadt  1415:                        return(ep);
                   1416:        }
                   1417:        return(NULL);
                   1418: }
                   1419:
1.69      jsg      1420: void
                   1421: env_init(void)
1.1       deraadt  1422: {
                   1423:        extern char **environ;
1.10      art      1424:        char **epp, *cp;
                   1425:        struct env_lst *ep;
1.1       deraadt  1426:
                   1427:        for (epp = environ; *epp; epp++) {
1.10      art      1428:                if ((cp = strchr(*epp, '='))) {
1.1       deraadt  1429:                        *cp = '\0';
1.70      guenther 1430:                        ep = env_define(*epp, cp+1);
1.1       deraadt  1431:                        ep->export = 0;
                   1432:                        *cp = '=';
                   1433:                }
                   1434:        }
                   1435:        /*
                   1436:         * Special case for DISPLAY variable.  If it is ":0.0" or
                   1437:         * "unix:0.0", we have to get rid of "unix" and insert our
                   1438:         * hostname.
                   1439:         */
                   1440:        if ((ep = env_find("DISPLAY"))
                   1441:            && ((*ep->value == ':')
1.70      guenther 1442:                || (strncmp(ep->value, "unix:", 5) == 0))) {
1.71      deraadt  1443:                char hbuf[HOST_NAME_MAX+1];
1.70      guenther 1444:                char *cp2 = strchr(ep->value, ':');
1.1       deraadt  1445:
1.18      deraadt  1446:                gethostname(hbuf, sizeof hbuf);
1.10      art      1447:
                   1448:                /* If this is not the full name, try to get it via DNS */
                   1449:                if (strchr(hbuf, '.') == 0) {
                   1450:                        struct hostent *he = gethostbyname(hbuf);
                   1451:                        if (he != 0)
1.19      deraadt  1452:                                strncpy(hbuf, he->h_name, sizeof hbuf-1);
                   1453:                        hbuf[sizeof hbuf-1] = '\0';
1.10      art      1454:                }
                   1455:
1.44      pvalchev 1456:                if (asprintf (&cp, "%s%s", hbuf, cp2) == -1)
1.14      art      1457:                        err(1, "asprintf");
1.10      art      1458:
1.1       deraadt  1459:                free(ep->value);
1.70      guenther 1460:                ep->value = cp;
1.1       deraadt  1461:        }
                   1462:        /*
                   1463:         * If USER is not defined, but LOGNAME is, then add
                   1464:         * USER with the value from LOGNAME.  By default, we
                   1465:         * don't export the USER variable.
                   1466:         */
                   1467:        if ((env_find("USER") == NULL) && (ep = env_find("LOGNAME"))) {
1.70      guenther 1468:                env_define("USER", ep->value);
                   1469:                env_unexport("USER");
1.1       deraadt  1470:        }
1.70      guenther 1471:        env_export("DISPLAY");
                   1472:        env_export("PRINTER");
                   1473:        env_export("XAUTHORITY");
1.1       deraadt  1474: }
                   1475:
1.69      jsg      1476: struct env_lst *
1.70      guenther 1477: env_define(const char *var, const char *value)
1.1       deraadt  1478: {
1.39      mpech    1479:        struct env_lst *ep;
1.1       deraadt  1480:
1.10      art      1481:        if ((ep = env_find(var))) {
1.1       deraadt  1482:                if (ep->var)
                   1483:                        free(ep->var);
                   1484:                if (ep->value)
                   1485:                        free(ep->value);
                   1486:        } else {
1.14      art      1487:                if ((ep = malloc(sizeof(struct env_lst))) == NULL)
                   1488:                        err(1, "malloc");
1.1       deraadt  1489:                ep->next = envlisthead.next;
                   1490:                envlisthead.next = ep;
                   1491:                ep->prev = &envlisthead;
                   1492:                if (ep->next)
                   1493:                        ep->next->prev = ep;
                   1494:        }
1.70      guenther 1495:        ep->welldefined = opt_welldefined(var);
1.1       deraadt  1496:        ep->export = 1;
1.70      guenther 1497:        if ((ep->var = strdup(var)) == NULL)
1.14      art      1498:                err(1, "strdup");
1.70      guenther 1499:        if ((ep->value = strdup(value)) == NULL)
1.14      art      1500:                err(1, "strdup");
1.1       deraadt  1501:        return(ep);
                   1502: }
                   1503:
1.67      guenther 1504: static void
1.70      guenther 1505: env_undefine(const char *var)
1.1       deraadt  1506: {
1.39      mpech    1507:        struct env_lst *ep;
1.1       deraadt  1508:
1.10      art      1509:        if ((ep = env_find(var))) {
1.1       deraadt  1510:                ep->prev->next = ep->next;
                   1511:                if (ep->next)
                   1512:                        ep->next->prev = ep->prev;
                   1513:                if (ep->var)
                   1514:                        free(ep->var);
                   1515:                if (ep->value)
                   1516:                        free(ep->value);
                   1517:                free(ep);
                   1518:        }
                   1519: }
                   1520:
1.67      guenther 1521: static void
1.70      guenther 1522: env_export(const char *var)
1.1       deraadt  1523: {
1.39      mpech    1524:        struct env_lst *ep;
1.1       deraadt  1525:
1.10      art      1526:        if ((ep = env_find(var)))
1.1       deraadt  1527:                ep->export = 1;
                   1528: }
                   1529:
1.67      guenther 1530: static void
1.70      guenther 1531: env_unexport(const char *var)
1.1       deraadt  1532: {
1.39      mpech    1533:        struct env_lst *ep;
1.1       deraadt  1534:
1.15      art      1535:        if ((ep = env_find(var)) != NULL)
1.1       deraadt  1536:                ep->export = 0;
                   1537: }
                   1538:
1.67      guenther 1539: static void
1.70      guenther 1540: env_send(const char *var)
1.1       deraadt  1541: {
1.39      mpech    1542:        struct env_lst *ep;
1.1       deraadt  1543:
1.3       niklas   1544:        if (my_state_is_wont(TELOPT_NEW_ENVIRON)
1.1       deraadt  1545:                ) {
                   1546:                fprintf(stderr,
1.10      art      1547:                    "Cannot send '%s': Telnet ENVIRON option not enabled\r\n",
1.1       deraadt  1548:                                                                        var);
                   1549:                return;
                   1550:        }
                   1551:        ep = env_find(var);
                   1552:        if (ep == 0) {
1.10      art      1553:                fprintf(stderr, "Cannot send '%s': variable not defined\r\n",
1.1       deraadt  1554:                                                                        var);
                   1555:                return;
                   1556:        }
                   1557:        env_opt_start_info();
                   1558:        env_opt_add(ep->var);
                   1559:        env_opt_end(0);
                   1560: }
                   1561:
1.67      guenther 1562: static void
1.69      jsg      1563: env_list(void)
1.1       deraadt  1564: {
1.39      mpech    1565:        struct env_lst *ep;
1.1       deraadt  1566:
                   1567:        for (ep = envlisthead.next; ep; ep = ep->next) {
1.10      art      1568:                printf("%c %-20s %s\r\n", ep->export ? '*' : ' ',
1.1       deraadt  1569:                                        ep->var, ep->value);
                   1570:        }
                   1571: }
                   1572:
1.70      guenther 1573: char *
1.69      jsg      1574: env_default(int init, int welldefined)
1.1       deraadt  1575: {
                   1576:        static struct env_lst *nep = NULL;
                   1577:
                   1578:        if (init) {
                   1579:                nep = &envlisthead;
1.10      art      1580:                return NULL;
1.1       deraadt  1581:        }
                   1582:        if (nep) {
1.10      art      1583:                while ((nep = nep->next)) {
1.1       deraadt  1584:                        if (nep->export && (nep->welldefined == welldefined))
                   1585:                                return(nep->var);
                   1586:                }
                   1587:        }
                   1588:        return(NULL);
                   1589: }
                   1590:
1.70      guenther 1591: char *
                   1592: env_getvalue(const char *var, int exported_only)
1.1       deraadt  1593: {
1.39      mpech    1594:        struct env_lst *ep;
1.1       deraadt  1595:
1.48      otto     1596:        if ((ep = env_find(var)) && (!exported_only || ep->export))
1.1       deraadt  1597:                return(ep->value);
                   1598:        return(NULL);
                   1599: }
                   1600:
1.68      guenther 1601: static void
                   1602: connection_status(int local_only)
                   1603: {
                   1604:        if (!connected)
                   1605:                printf("No connection.\r\n");
                   1606:        else {
                   1607:                printf("Connected to %s.\r\n", hostname);
                   1608:                if (!local_only) {
                   1609:                        int mode = getconnmode();
                   1610:
                   1611:                        printf("Operating ");
                   1612:                        if (my_want_state_is_will(TELOPT_LINEMODE)) {
                   1613:                                printf("with LINEMODE option\r\n"
                   1614:                                    "%s line editing\r\n"
                   1615:                                    "%s catching of signals\r\n",
                   1616:                                    (mode & MODE_EDIT) ? "Local" : "No",
                   1617:                                    (mode & MODE_TRAPSIG) ? "Local" : "No");
                   1618:                                slcstate();
                   1619: #ifdef KLUDGELINEMODE
                   1620:                        } else if (kludgelinemode &&
                   1621:                            my_want_state_is_dont(TELOPT_SGA)) {
                   1622:                                printf("in obsolete linemode\r\n");
                   1623: #endif
                   1624:                        } else {
                   1625:                                printf("in single character mode\r\n");
                   1626:                                if (localchars)
                   1627:                                        printf("Catching signals locally\r\n");
                   1628:                        }
                   1629:
                   1630:                        printf("%s character echo\r\n",
                   1631:                            (mode & MODE_ECHO) ? "Local" : "Remote");
                   1632:                        if (my_want_state_is_will(TELOPT_LFLOW))
                   1633:                                printf("%s flow control\r\n",
                   1634:                                    (mode & MODE_FLOW) ? "Local" : "No");
                   1635:                }
                   1636:        }
                   1637:        printf("Escape character is '%s'.\r\n", control(escape));
                   1638:        (void) fflush(stdout);
                   1639: }
                   1640:
1.1       deraadt  1641: /*
                   1642:  * Print status about the connection.
                   1643:  */
1.68      guenther 1644: static int
                   1645: status(int argc, char *argv[])
1.1       deraadt  1646: {
1.68      guenther 1647:        connection_status(0);
                   1648:        return 1;
1.1       deraadt  1649: }
                   1650:
                   1651: #ifdef SIGINFO
                   1652: /*
                   1653:  * Function that gets called when SIGINFO is received.
                   1654:  */
1.10      art      1655: void
1.68      guenther 1656: ayt_status(int sig)
1.1       deraadt  1657: {
1.68      guenther 1658:        connection_status(1);
1.1       deraadt  1659: }
                   1660: #endif
                   1661:
1.10      art      1662: static Command *getcmd(char *name);
                   1663:
                   1664: static void
                   1665: cmdrc(char *m1, char *m2)
                   1666: {
                   1667:     static char rcname[128];
                   1668:     Command *c;
                   1669:     FILE *rcfile;
                   1670:     int gotmachine = 0;
                   1671:     int l1 = strlen(m1);
                   1672:     int l2 = strlen(m2);
1.71      deraadt  1673:     char m1save[HOST_NAME_MAX+1];
1.10      art      1674:
                   1675:     if (skiprc)
                   1676:        return;
                   1677:
1.34      aaron    1678:     strlcpy(m1save, m1, sizeof(m1save));
1.10      art      1679:     m1 = m1save;
                   1680:
                   1681:     if (rcname[0] == 0) {
                   1682:        char *home = getenv("HOME");
                   1683:
1.29      millert  1684:        if (home == NULL || *home == '\0')
                   1685:            return;
1.10      art      1686:        snprintf (rcname, sizeof(rcname), "%s/.telnetrc",
                   1687:                  home ? home : "");
                   1688:     }
                   1689:
                   1690:     if ((rcfile = fopen(rcname, "r")) == 0) {
                   1691:        return;
                   1692:     }
                   1693:
                   1694:     for (;;) {
                   1695:        if (fgets(line, sizeof(line), rcfile) == NULL)
                   1696:            break;
                   1697:        if (line[0] == 0)
                   1698:            break;
                   1699:        if (line[0] == '#')
                   1700:            continue;
                   1701:        if (gotmachine) {
1.60      guenther 1702:            if (!isspace((unsigned char)line[0]))
1.10      art      1703:                gotmachine = 0;
                   1704:        }
                   1705:        if (gotmachine == 0) {
1.60      guenther 1706:            if (isspace((unsigned char)line[0]))
1.10      art      1707:                continue;
                   1708:            if (strncasecmp(line, m1, l1) == 0)
                   1709:                strncpy(line, &line[l1], sizeof(line) - l1);
                   1710:            else if (strncasecmp(line, m2, l2) == 0)
                   1711:                strncpy(line, &line[l2], sizeof(line) - l2);
                   1712:            else if (strncasecmp(line, "DEFAULT", 7) == 0)
                   1713:                strncpy(line, &line[7], sizeof(line) - 7);
                   1714:            else
                   1715:                continue;
                   1716:            if (line[0] != ' ' && line[0] != '\t' && line[0] != '\n')
                   1717:                continue;
                   1718:            gotmachine = 1;
                   1719:        }
                   1720:        makeargv();
                   1721:        if (margv[0] == 0)
                   1722:            continue;
                   1723:        c = getcmd(margv[0]);
                   1724:        if (Ambiguous(c)) {
                   1725:            printf("?Ambiguous command: %s\r\n", margv[0]);
                   1726:            continue;
                   1727:        }
                   1728:        if (c == 0) {
                   1729:            printf("?Invalid command: %s\r\n", margv[0]);
                   1730:            continue;
                   1731:        }
                   1732:        /*
                   1733:         * This should never happen...
                   1734:         */
                   1735:        if (c->needconnect && !connected) {
                   1736:            printf("?Need to be connected first for %s.\r\n", margv[0]);
                   1737:            continue;
                   1738:        }
                   1739:        (*c->handler)(margc, margv);
                   1740:     }
                   1741:     fclose(rcfile);
                   1742: }
                   1743:
1.69      jsg      1744: int
                   1745: tn(int argc, char *argv[])
1.1       deraadt  1746: {
1.23      itojun   1747:     struct addrinfo hints, *res, *res0;
1.5       niklas   1748:     char *cmd, *hostp = 0, *portp = 0, *user = 0, *aliasp = 0;
1.73      jca      1749:     int error, retry;
1.74      jca      1750:     const int niflags = NI_NUMERICHOST, tos = IPTOS_LOWDELAY;
1.1       deraadt  1751:
                   1752:     if (connected) {
1.10      art      1753:        printf("?Already connected to %s\r\n", hostname);
1.1       deraadt  1754:        return 0;
                   1755:     }
                   1756:     if (argc < 2) {
1.43      hin      1757:        strlcpy(line, "open ", sizeof(line));
1.1       deraadt  1758:        printf("(to) ");
                   1759:        (void) fgets(&line[strlen(line)], sizeof(line) - strlen(line), stdin);
                   1760:        makeargv();
                   1761:        argc = margc;
                   1762:        argv = margv;
                   1763:     }
                   1764:     cmd = *argv;
                   1765:     --argc; ++argv;
                   1766:     while (argc) {
1.3       niklas   1767:        if (strcmp(*argv, "help") == 0 || isprefix(*argv, "?"))
1.1       deraadt  1768:            goto usage;
                   1769:        if (strcmp(*argv, "-l") == 0) {
                   1770:            --argc; ++argv;
                   1771:            if (argc == 0)
                   1772:                goto usage;
1.14      art      1773:            if ((user = strdup(*argv++)) == NULL)
                   1774:                err(1, "strdup");
1.1       deraadt  1775:            --argc;
                   1776:            continue;
                   1777:        }
1.5       niklas   1778:        if (strcmp(*argv, "-b") == 0) {
                   1779:            --argc; ++argv;
                   1780:            if (argc == 0)
                   1781:                goto usage;
                   1782:            aliasp = *argv++;
                   1783:            --argc;
                   1784:            continue;
                   1785:        }
1.1       deraadt  1786:        if (strcmp(*argv, "-a") == 0) {
                   1787:            --argc; ++argv;
                   1788:            autologin = 1;
                   1789:            continue;
                   1790:        }
                   1791:        if (hostp == 0) {
                   1792:            hostp = *argv++;
                   1793:            --argc;
                   1794:            continue;
                   1795:        }
                   1796:        if (portp == 0) {
                   1797:            portp = *argv++;
                   1798:            --argc;
                   1799:            continue;
                   1800:        }
                   1801:     usage:
1.56      guenther 1802:        printf("usage: %s [-a] [-b hostalias] [-l user] host-name [port]\r\n", cmd);
1.1       deraadt  1803:        return 0;
                   1804:     }
                   1805:     if (hostp == 0)
                   1806:        goto usage;
                   1807:
1.72      jca      1808:     hostname = hostp;
                   1809:     memset(&hints, 0, sizeof(hints));
                   1810:     hints.ai_family = family;
                   1811:     hints.ai_socktype = SOCK_STREAM;
                   1812:     hints.ai_flags = AI_CANONNAME;
                   1813:     if (portp == NULL) {
                   1814:         portp = "telnet";
                   1815:         telnetport = 1;
                   1816:     } else if (*portp == '-') {
                   1817:         portp++;
                   1818:         telnetport = 1;
1.23      itojun   1819:     } else
1.72      jca      1820:         telnetport = 0;
                   1821:     error = getaddrinfo(hostp, portp, &hints, &res0);
                   1822:     if (error) {
                   1823:         if (error == EAI_SERVICE)
                   1824:             warnx("%s: bad port", portp);
                   1825:         else
                   1826:             warnx("%s: %s", hostp, gai_strerror(error));
                   1827:         return 0;
1.1       deraadt  1828:     }
1.10      art      1829:
1.23      itojun   1830:     net = -1;
                   1831:     retry = 0;
                   1832:     for (res = res0; res; res = res->ai_next) {
                   1833:        if (1 /* retry */) {
1.31      itojun   1834:            char hbuf[NI_MAXHOST];
1.23      itojun   1835:
1.31      itojun   1836:            if (getnameinfo(res->ai_addr, res->ai_addrlen, hbuf, sizeof(hbuf),
                   1837:                    NULL, 0, niflags) != 0) {
1.43      hin      1838:                strlcpy(hbuf, "(invalid)", sizeof(hbuf));
1.31      itojun   1839:            }
1.23      itojun   1840:            printf("Trying %s...\r\n", hbuf);
                   1841:        }
                   1842:        net = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
                   1843:        if (net < 0)
                   1844:            continue;
1.10      art      1845:
1.5       niklas   1846:        if (aliasp) {
1.23      itojun   1847:            struct addrinfo ahints, *ares;
                   1848:
                   1849:            memset(&ahints, 0, sizeof(ahints));
1.46      otto     1850:            ahints.ai_family = family;
1.23      itojun   1851:            ahints.ai_socktype = SOCK_STREAM;
                   1852:            ahints.ai_flags = AI_PASSIVE;
                   1853:            error = getaddrinfo(aliasp, "0", &ahints, &ares);
                   1854:            if (error) {
                   1855:                warn("%s: %s", aliasp, gai_strerror(error));
                   1856:                close(net);
1.49      otto     1857:                net = -1;
1.23      itojun   1858:                continue;
1.5       niklas   1859:            }
1.32      itojun   1860:            if (bind(net, ares->ai_addr, ares->ai_addrlen) < 0) {
1.31      itojun   1861:                perror(aliasp);
                   1862:                (void) close(net);   /* dump descriptor */
1.49      otto     1863:                net = -1;
1.23      itojun   1864:                freeaddrinfo(ares);
                   1865:                continue;
1.5       niklas   1866:             }
1.23      itojun   1867:            freeaddrinfo(ares);
                   1868:        }
1.74      jca      1869:
                   1870:        switch (res->ai_family) {
                   1871:        case AF_INET:
                   1872:                if (setsockopt(net, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)) < 0
                   1873:                    && errno != ENOPROTOOPT)
                   1874:                        perror("telnet: setsockopt (IP_TOS) (ignored)");
                   1875:                break;
                   1876:        case AF_INET6:
                   1877:                if (setsockopt(net, IPPROTO_IPV6, IPV6_TCLASS, &tos,
                   1878:                    sizeof(tos)) < 0 && errno != ENOPROTOOPT)
                   1879:                        perror("telnet: setsockopt (IPV6_TCLASS) (ignored)");
                   1880:                break;
1.1       deraadt  1881:        }
                   1882:
1.23      itojun   1883:        if (connect(net, res->ai_addr, res->ai_addrlen) < 0) {
1.31      itojun   1884:            char hbuf[NI_MAXHOST];
1.16      deraadt  1885:
1.31      itojun   1886:            if (getnameinfo(res->ai_addr, res->ai_addrlen, hbuf, sizeof(hbuf),
                   1887:                    NULL, 0, niflags) != 0) {
1.43      hin      1888:                strlcpy(hbuf, "(invalid)", sizeof(hbuf));
1.31      itojun   1889:            }
1.25      itojun   1890:            fprintf(stderr, "telnet: connect to address %s: %s\n", hbuf,
                   1891:                strerror(errno));
1.23      itojun   1892:
                   1893:            close(net);
                   1894:            net = -1;
                   1895:            retry++;
                   1896:            continue;
1.1       deraadt  1897:        }
1.23      itojun   1898:
1.1       deraadt  1899:        connected++;
1.23      itojun   1900:        break;
                   1901:     }
                   1902:     freeaddrinfo(res0);
                   1903:     if (net < 0) {
                   1904:        return 0;
                   1905:     }
1.1       deraadt  1906:     cmdrc(hostp, hostname);
                   1907:     if (autologin && user == NULL) {
                   1908:        struct passwd *pw;
                   1909:
1.58      guenther 1910:        user = getlogin();
1.1       deraadt  1911:        if (user == NULL ||
1.58      guenther 1912:            (pw = getpwnam(user)) == NULL || pw->pw_uid != getuid()) {
1.15      art      1913:                if ((pw = getpwuid(getuid())) != NULL)
1.1       deraadt  1914:                        user = pw->pw_name;
                   1915:                else
                   1916:                        user = NULL;
                   1917:        }
                   1918:     }
                   1919:     if (user) {
1.70      guenther 1920:        env_define("USER", user);
                   1921:        env_export("USER");
1.1       deraadt  1922:     }
1.68      guenther 1923:     connection_status(1);
1.1       deraadt  1924:     if (setjmp(peerdied) == 0)
                   1925:        telnet(user);
1.67      guenther 1926:     (void)close(net);
1.10      art      1927:     ExitString("Connection closed by foreign host.\r\n",1);
1.1       deraadt  1928: }
                   1929:
                   1930: #define HELPINDENT (sizeof ("connect"))
                   1931:
                   1932: static char
                   1933:        openhelp[] =    "connect to a site",
                   1934:        closehelp[] =   "close current connection",
                   1935:        logouthelp[] =  "forcibly logout remote user and close the connection",
                   1936:        quithelp[] =    "exit telnet",
                   1937:        statushelp[] =  "print status information",
                   1938:        helphelp[] =    "print help information",
                   1939:        sendhelp[] =    "transmit special characters ('send ?' for more)",
                   1940:        sethelp[] =     "set operating parameters ('set ?' for more)",
                   1941:        unsethelp[] =   "unset operating parameters ('unset ?' for more)",
                   1942:        togglestring[] ="toggle operating parameters ('toggle ?' for more)",
                   1943:        slchelp[] =     "change state of special charaters ('slc ?' for more)",
                   1944:        displayhelp[] = "display operating parameters",
                   1945:        zhelp[] =       "suspend telnet",
                   1946:        envhelp[] =     "change environment variables ('environ ?' for more)",
                   1947:        modestring[] = "try to enter line or character mode ('mode ?' for more)";
                   1948:
1.40      millert  1949: static int     help(int, char**);
1.1       deraadt  1950:
                   1951: static Command cmdtab[] = {
                   1952:        { "close",      closehelp,      bye,            1 },
                   1953:        { "logout",     logouthelp,     logout,         1 },
                   1954:        { "display",    displayhelp,    display,        0 },
                   1955:        { "mode",       modestring,     modecmd,        0 },
                   1956:        { "open",       openhelp,       tn,             0 },
1.63      guenther 1957:        { "quit",       quithelp,       quitcmd,        0 },
1.1       deraadt  1958:        { "send",       sendhelp,       sendcmd,        0 },
                   1959:        { "set",        sethelp,        setcmd,         0 },
                   1960:        { "unset",      unsethelp,      unsetcmd,       0 },
                   1961:        { "status",     statushelp,     status,         0 },
                   1962:        { "toggle",     togglestring,   toggle,         0 },
                   1963:        { "slc",        slchelp,        slccmd,         0 },
1.10      art      1964:
                   1965:        { "z",          zhelp,          telnetsuspend,  0 },
1.1       deraadt  1966:        { "environ",    envhelp,        env_cmd,        0 },
                   1967:        { "?",          helphelp,       help,           0 },
1.10      art      1968:        { 0,            0,              0,              0 }
1.1       deraadt  1969: };
                   1970:
                   1971: static char    crmodhelp[] =   "deprecated command -- use 'toggle crmod' instead";
                   1972: static char    escapehelp[] =  "deprecated command -- use 'set escape' instead";
                   1973:
                   1974: static Command cmdtab2[] = {
                   1975:        { "help",       0,              help,           0 },
                   1976:        { "escape",     escapehelp,     setescape,      0 },
                   1977:        { "crmod",      crmodhelp,      togcrmod,       0 },
1.10      art      1978:        { 0,            0,              0,              0 }
1.1       deraadt  1979: };
                   1980:
                   1981:
1.69      jsg      1982: static Command *
                   1983: getcmd(char *name)
1.1       deraadt  1984: {
                   1985:     Command *cm;
                   1986:
1.10      art      1987:     if ((cm = (Command *) genget(name, (char **) cmdtab, sizeof(Command))))
1.1       deraadt  1988:        return cm;
                   1989:     return (Command *) genget(name, (char **) cmdtab2, sizeof(Command));
                   1990: }
                   1991:
1.69      jsg      1992: void
                   1993: command(int top, char *tbuf, int cnt)
1.1       deraadt  1994: {
1.39      mpech    1995:     Command *c;
1.1       deraadt  1996:
                   1997:     setcommandmode();
                   1998:     if (!top) {
                   1999:        putchar('\n');
                   2000:     } else {
                   2001:        (void) signal(SIGINT, SIG_DFL);
                   2002:        (void) signal(SIGQUIT, SIG_DFL);
                   2003:     }
                   2004:     for (;;) {
                   2005:        if (rlogin == _POSIX_VDISABLE)
                   2006:                printf("%s> ", prompt);
                   2007:        if (tbuf) {
1.39      mpech    2008:            char *cp;
1.1       deraadt  2009:            cp = line;
                   2010:            while (cnt > 0 && (*cp++ = *tbuf++) != '\n')
                   2011:                cnt--;
                   2012:            tbuf = 0;
                   2013:            if (cp == line || *--cp != '\n' || cp == line)
                   2014:                goto getline;
                   2015:            *cp = '\0';
                   2016:            if (rlogin == _POSIX_VDISABLE)
1.10      art      2017:                printf("%s\r\n", line);
1.1       deraadt  2018:        } else {
                   2019:        getline:
                   2020:            if (rlogin != _POSIX_VDISABLE)
                   2021:                printf("%s> ", prompt);
                   2022:            if (fgets(line, sizeof(line), stdin) == NULL) {
1.66      guenther 2023:                if (feof(stdin) || ferror(stdin))
1.63      guenther 2024:                    quit();
1.1       deraadt  2025:                break;
                   2026:            }
                   2027:        }
                   2028:        if (line[0] == 0)
                   2029:            break;
                   2030:        makeargv();
                   2031:        if (margv[0] == 0) {
                   2032:            break;
                   2033:        }
                   2034:        c = getcmd(margv[0]);
                   2035:        if (Ambiguous(c)) {
1.10      art      2036:            printf("?Ambiguous command\r\n");
1.1       deraadt  2037:            continue;
                   2038:        }
                   2039:        if (c == 0) {
1.10      art      2040:            printf("?Invalid command\r\n");
1.1       deraadt  2041:            continue;
                   2042:        }
                   2043:        if (c->needconnect && !connected) {
1.10      art      2044:            printf("?Need to be connected first.\r\n");
1.1       deraadt  2045:            continue;
                   2046:        }
                   2047:        if ((*c->handler)(margc, margv)) {
                   2048:            break;
                   2049:        }
                   2050:     }
                   2051:     if (!top) {
1.66      guenther 2052:        if (!connected)
1.1       deraadt  2053:            longjmp(toplevel, 1);
                   2054:        setconnmode(0);
                   2055:     }
                   2056: }
                   2057: 
                   2058: /*
                   2059:  * Help command.
                   2060:  */
1.69      jsg      2061: static int
                   2062: help(int argc, char *argv[])
1.1       deraadt  2063: {
1.39      mpech    2064:        Command *c;
1.1       deraadt  2065:
                   2066:        if (argc == 1) {
1.10      art      2067:                printf("Commands may be abbreviated.  Commands are:\r\n\r\n");
1.1       deraadt  2068:                for (c = cmdtab; c->name; c++)
                   2069:                        if (c->help) {
1.37      deraadt  2070:                                printf("%-*s\t%s\r\n", (int)HELPINDENT, c->name,
1.1       deraadt  2071:                                                                    c->help);
                   2072:                        }
                   2073:                return 0;
                   2074:        }
                   2075:        while (--argc > 0) {
1.39      mpech    2076:                char *arg;
1.1       deraadt  2077:                arg = *++argv;
                   2078:                c = getcmd(arg);
                   2079:                if (Ambiguous(c))
1.10      art      2080:                        printf("?Ambiguous help command %s\r\n", arg);
1.1       deraadt  2081:                else if (c == (Command *)0)
1.10      art      2082:                        printf("?Invalid help command %s\r\n", arg);
1.1       deraadt  2083:                else
1.10      art      2084:                        printf("%s\r\n", c->help);
1.1       deraadt  2085:        }
                   2086:        return 0;
                   2087: }