[BACK]Return to main.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / telnet

Annotation of src/usr.bin/telnet/main.c, Revision 1.13

1.13    ! hin         1: /*     $OpenBSD: main.c,v 1.12 2001/11/03 00:07:53 hin Exp $   */
1.2       niklas      2: /*     $NetBSD: main.c,v 1.5 1996/02/28 21:04:05 thorpej Exp $ */
                      3:
1.1       deraadt     4: /*
1.2       niklas      5:  * Copyright (c) 1988, 1990, 1993
                      6:  *     The Regents of the University of California.  All rights reserved.
1.1       deraadt     7:  *
                      8:  * Redistribution and use in source and binary forms, with or without
                      9:  * modification, are permitted provided that the following conditions
                     10:  * are met:
                     11:  * 1. Redistributions of source code must retain the above copyright
                     12:  *    notice, this list of conditions and the following disclaimer.
                     13:  * 2. Redistributions in binary form must reproduce the above copyright
                     14:  *    notice, this list of conditions and the following disclaimer in the
                     15:  *    documentation and/or other materials provided with the distribution.
                     16:  * 3. All advertising materials mentioning features or use of this software
                     17:  *    must display the following acknowledgement:
                     18:  *     This product includes software developed by the University of
                     19:  *     California, Berkeley and its contributors.
                     20:  * 4. Neither the name of the University nor the names of its contributors
                     21:  *    may be used to endorse or promote products derived from this software
                     22:  *    without specific prior written permission.
                     23:  *
                     24:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     25:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     26:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     27:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     28:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     29:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     30:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     31:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     32:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     33:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     34:  * SUCH DAMAGE.
                     35:  */
                     36:
                     37: #ifndef lint
1.2       niklas     38: static char copyright[] =
                     39: "@(#) Copyright (c) 1988, 1990, 1993\n\
                     40:        The Regents of the University of California.  All rights reserved.\n";
1.1       deraadt    41: #endif /* not lint */
                     42:
1.5       art        43: #include "telnet_locl.h"
1.1       deraadt    44:
                     45: /* These values need to be the same as defined in libtelnet/kerberos5.c */
                     46: /* Either define them in both places, or put in some common header file. */
1.2       niklas     47: #define OPTS_FORWARD_CREDS     0x00000002
                     48: #define OPTS_FORWARDABLE_CREDS 0x00000001
1.1       deraadt    49:
1.11      hin        50: #ifdef KRB5
1.1       deraadt    51: #define FORWARD
1.11      hin        52: /* XXX ugly hack to setup dns-proxy stuff */
                     53: #define Authenticator asn1_Authenticator
                     54: #include <kerberosV/krb5.h>
1.1       deraadt    55: #endif
                     56:
1.6       art        57: #ifdef KRB4
                     58: #include <kerberosIV/krb.h>
                     59: #endif
                     60:
1.11      hin        61: #ifdef FORWARD
                     62: int forward_flags;
                     63: static int default_forward=0;
                     64: #endif
                     65:
1.1       deraadt    66: /*
                     67:  * Initialize variables.
                     68:  */
                     69:     void
                     70: tninit()
                     71: {
                     72:     init_terminal();
                     73:
                     74:     init_network();
1.2       niklas     75:
1.1       deraadt    76:     init_telnet();
                     77:
                     78:     init_sys();
                     79:
                     80: #if defined(TN3270)
                     81:     init_3270();
                     82: #endif
                     83: }
                     84:
                     85:        void
                     86: usage()
                     87: {
                     88:        fprintf(stderr, "Usage: %s %s%s%s%s\n",
                     89:            prompt,
                     90: #ifdef AUTHENTICATION
1.2       niklas     91:            "[-8] [-E] [-K] [-L] [-S tos] [-X atype] [-a] [-c] [-d] [-e char]",
1.9       deraadt    92:            "\n\t[-k realm] [-l user] [-f/-F] [-n tracefile] [-b hostalias ] ",
1.1       deraadt    93: #else
1.2       niklas     94:            "[-8] [-E] [-L] [-S tos] [-a] [-c] [-d] [-e char] [-l user]",
1.9       deraadt    95:            "\n\t[-n tracefile] [-b hostalias ] ",
1.1       deraadt    96: #endif
                     97: #if defined(TN3270) && defined(unix)
                     98: # ifdef AUTHENTICATION
1.2       niklas     99:            "[-noasynch] [-noasynctty]\n\t[-noasyncnet] [-r] [-t transcom] ",
1.1       deraadt   100: # else
1.2       niklas    101:            "[-noasynch] [-noasynctty] [-noasyncnet] [-r]\n\t[-t transcom]",
1.1       deraadt   102: # endif
                    103: #else
                    104:            "[-r] ",
                    105: #endif
1.9       deraadt   106:            "\n\t"
1.5       art       107: #ifdef ENCRYPTION
1.9       deraadt   108:            "[-x] "
1.5       art       109: #endif
1.9       deraadt   110:            "[host-name [port]]");
1.1       deraadt   111:        exit(1);
                    112: }
                    113:
1.11      hin       114:
                    115: #ifdef KRB5
                    116: static void
                    117: krb5_init(void)
                    118: {
                    119:     krb5_context context;
                    120:     krb5_error_code ret;
                    121:
                    122:     ret = krb5_init_context(&context);
                    123:     if (ret)
                    124:        return;
                    125:
                    126: #if defined(AUTHENTICATION) && defined(KRB5) && defined(FORWARD)
                    127:     if (krb5_config_get_bool (context, NULL,
                    128:          "libdefaults", "forward", NULL)) {
                    129:            forward_flags |= OPTS_FORWARD_CREDS;
                    130:            default_forward=1;
                    131:     }
                    132:     if (krb5_config_get_bool (context, NULL,
                    133:          "libdefaults", "forwardable", NULL)) {
                    134:            forward_flags |= OPTS_FORWARDABLE_CREDS;
                    135:            default_forward=1;
                    136:     }
                    137: #endif
                    138: #ifdef  ENCRYPTION
                    139:     if (krb5_config_get_bool (context, NULL,
                    140:         "libdefaults", "encrypt", NULL)) {
                    141:           encrypt_auto(1);
                    142:           decrypt_auto(1);
1.13    ! hin       143:          wantencryption = 1;
1.11      hin       144:           EncryptVerbose(1);
                    145:         }
                    146: #endif
                    147:
                    148:     krb5_free_context(context);
                    149: }
                    150: #endif
                    151:
1.1       deraadt   152: /*
                    153:  * main.  Parse arguments, invoke the protocol or command parser.
                    154:  */
                    155:
1.7       art       156:        int
1.1       deraadt   157: main(argc, argv)
                    158:        int argc;
                    159:        char *argv[];
                    160: {
                    161:        extern char *optarg;
                    162:        extern int optind;
                    163:        int ch;
1.5       art       164:        char *user, *alias;
1.1       deraadt   165: #ifdef FORWARD
                    166:        extern int forward_flags;
                    167: #endif /* FORWARD */
                    168:
1.11      hin       169: #ifdef KRB5
                    170:        krb5_init();
                    171: #endif
                    172:
1.1       deraadt   173:        tninit();               /* Clear out things */
                    174:
                    175:        TerminalSaveState();
                    176:
1.5       art       177:        if ((prompt = strrchr(argv[0], '/')))
1.1       deraadt   178:                ++prompt;
                    179:        else
                    180:                prompt = argv[0];
                    181:
1.3       niklas    182:        user = alias = NULL;
1.1       deraadt   183:
                    184:        rlogin = (strncmp(prompt, "rlog", 4) == 0) ? '~' : _POSIX_VDISABLE;
1.5       art       185:
                    186:        /*
                    187:         * if AUTHENTICATION and ENCRYPTION is set autologin will be
                    188:         * set to true after the getopt switch; unless the -K option is
                    189:         * passed
                    190:         */
1.1       deraadt   191:        autologin = -1;
                    192:
1.5       art       193:        while ((ch = getopt(argc, argv, "78DEKLS:X:ab:cde:fFk:l:n:rt:x")) != -1) {
1.1       deraadt   194:                switch(ch) {
                    195:                case '8':
                    196:                        eight = 3;      /* binary output and input */
                    197:                        break;
1.5       art       198:                case '7':
                    199:                        eight = 0;
                    200:                        break;
                    201:                case 'D': {
                    202:                        /* sometimes we don't want a mangled display */
                    203:                        char *p;
                    204:                        if((p = getenv("DISPLAY")))
                    205:                                env_define("DISPLAY", (unsigned char*)p);
                    206:                        break;
                    207:                }
                    208:
1.1       deraadt   209:                case 'E':
                    210:                        rlogin = escape = _POSIX_VDISABLE;
                    211:                        break;
                    212:                case 'K':
                    213: #ifdef AUTHENTICATION
                    214:                        autologin = 0;
                    215: #endif
                    216:                        break;
                    217:                case 'L':
                    218:                        eight |= 2;     /* binary output only */
                    219:                        break;
                    220:                case 'S':
                    221:                    {
                    222: #ifdef HAS_GETTOS
                    223:                        extern int tos;
                    224:
                    225:                        if ((tos = parsetos(optarg, "tcp")) < 0)
                    226:                                fprintf(stderr, "%s%s%s%s\n",
                    227:                                        prompt, ": Bad TOS argument '",
                    228:                                        optarg,
                    229:                                        "; will try to use default TOS");
                    230: #else
                    231:                        fprintf(stderr,
                    232:                           "%s: Warning: -S ignored, no parsetos() support.\n",
                    233:                                                                prompt);
                    234: #endif
                    235:                    }
                    236:                        break;
                    237:                case 'X':
                    238: #ifdef AUTHENTICATION
                    239:                        auth_disable_name(optarg);
                    240: #endif
                    241:                        break;
                    242:                case 'a':
                    243:                        autologin = 1;
                    244:                        break;
                    245:                case 'c':
                    246:                        skiprc = 1;
                    247:                        break;
                    248:                case 'd':
                    249:                        debug = 1;
                    250:                        break;
                    251:                case 'e':
                    252:                        set_escape_char(optarg);
                    253:                        break;
                    254:                case 'f':
                    255: #if defined(AUTHENTICATION) && defined(KRB5) && defined(FORWARD)
1.11      hin       256:                        if ((forward_flags & OPTS_FORWARD_CREDS) &&
                    257:                            !default_forward) {
1.2       niklas    258:                            fprintf(stderr,
1.1       deraadt   259:                                    "%s: Only one of -f and -F allowed.\n",
                    260:                                    prompt);
                    261:                            usage();
                    262:                        }
                    263:                        forward_flags |= OPTS_FORWARD_CREDS;
                    264: #else
                    265:                        fprintf(stderr,
1.2       niklas    266:                         "%s: Warning: -f ignored, no Kerberos V5 support.\n",
1.1       deraadt   267:                                prompt);
                    268: #endif
                    269:                        break;
                    270:                case 'F':
                    271: #if defined(AUTHENTICATION) && defined(KRB5) && defined(FORWARD)
1.11      hin       272:                        if ((forward_flags & OPTS_FORWARD_CREDS) &&
                    273:                            !default_forward) {
1.2       niklas    274:                            fprintf(stderr,
1.1       deraadt   275:                                    "%s: Only one of -f and -F allowed.\n",
                    276:                                    prompt);
                    277:                            usage();
                    278:                        }
                    279:                        forward_flags |= OPTS_FORWARD_CREDS;
                    280:                        forward_flags |= OPTS_FORWARDABLE_CREDS;
                    281: #else
                    282:                        fprintf(stderr,
1.2       niklas    283:                         "%s: Warning: -F ignored, no Kerberos V5 support.\n",
1.1       deraadt   284:                                prompt);
                    285: #endif
                    286:                        break;
                    287:                case 'k':
                    288: #if defined(AUTHENTICATION) && defined(KRB4)
                    289:                    {
1.5       art       290:                        extern char *dest_realm, dst_realm_buf[];
                    291:                        extern int dst_realm_sz;
1.1       deraadt   292:                        dest_realm = dst_realm_buf;
                    293:                        (void)strncpy(dest_realm, optarg, dst_realm_sz);
                    294:                    }
                    295: #else
                    296:                        fprintf(stderr,
                    297:                           "%s: Warning: -k ignored, no Kerberos V4 support.\n",
                    298:                                                                prompt);
                    299: #endif
                    300:                        break;
                    301:                case 'l':
1.6       art       302:                        autologin = -1;
1.1       deraadt   303:                        user = optarg;
                    304:                        break;
1.3       niklas    305:                case 'b':
                    306:                        alias = optarg;
                    307:                        break;
1.1       deraadt   308:                case 'n':
                    309: #if defined(TN3270) && defined(unix)
                    310:                        /* distinguish between "-n oasynch" and "-noasynch" */
                    311:                        if (argv[optind - 1][0] == '-' && argv[optind - 1][1]
                    312:                            == 'n' && argv[optind - 1][2] == 'o') {
                    313:                                if (!strcmp(optarg, "oasynch")) {
                    314:                                        noasynchtty = 1;
                    315:                                        noasynchnet = 1;
                    316:                                } else if (!strcmp(optarg, "oasynchtty"))
                    317:                                        noasynchtty = 1;
                    318:                                else if (!strcmp(optarg, "oasynchnet"))
                    319:                                        noasynchnet = 1;
                    320:                        } else
                    321: #endif /* defined(TN3270) && defined(unix) */
                    322:                                SetNetTrace(optarg);
                    323:                        break;
                    324:                case 'r':
                    325:                        rlogin = '~';
                    326:                        break;
                    327:                case 't':
                    328: #if defined(TN3270) && defined(unix)
1.10      aaron     329:                        (void)strlcpy(tline, optarg, sizeof tline);
1.1       deraadt   330:                        transcom = tline;
                    331: #else
                    332:                        fprintf(stderr,
                    333:                           "%s: Warning: -t ignored, no TN3270 support.\n",
                    334:                                                                prompt);
                    335: #endif
                    336:                        break;
                    337:                case 'x':
1.5       art       338: #ifdef ENCRYPTION
                    339:                        encrypt_auto(1);
                    340:                        decrypt_auto(1);
1.13    ! hin       341:                        wantencryption = 1;
1.5       art       342:                        EncryptVerbose(1);
                    343: #else
1.1       deraadt   344:                        fprintf(stderr,
                    345:                            "%s: Warning: -x ignored, no ENCRYPT support.\n",
                    346:                                                                prompt);
1.5       art       347: #endif
1.1       deraadt   348:                        break;
                    349:                case '?':
                    350:                default:
                    351:                        usage();
                    352:                        /* NOTREACHED */
                    353:                }
                    354:        }
1.5       art       355:
1.11      hin       356:        if (autologin == -1) {
1.6       art       357: #if defined(AUTHENTICATION)
1.13    ! hin       358:                if(check_krb4_tickets() || check_krb5_tickets())
        !           359:                        autologin = 1;
1.5       art       360: #endif
                    361: #if defined(ENCRYPTION)
1.11      hin       362:                encrypt_auto(1);
                    363:                decrypt_auto(1);
1.5       art       364: #endif
                    365:        }
1.13    ! hin       366:
1.1       deraadt   367:        if (autologin == -1)
                    368:                autologin = (rlogin == _POSIX_VDISABLE) ? 0 : 1;
                    369:
                    370:        argc -= optind;
                    371:        argv += optind;
                    372:
                    373:        if (argc) {
                    374:                char *args[7], **argp = args;
                    375:
                    376:                if (argc > 2)
                    377:                        usage();
                    378:                *argp++ = prompt;
                    379:                if (user) {
                    380:                        *argp++ = "-l";
                    381:                        *argp++ = user;
1.3       niklas    382:                }
                    383:                if (alias) {
                    384:                        *argp++ = "-b";
                    385:                        *argp++ = alias;
1.1       deraadt   386:                }
                    387:                *argp++ = argv[0];              /* host */
                    388:                if (argc > 1)
                    389:                        *argp++ = argv[1];      /* port */
                    390:                *argp = 0;
                    391:
                    392:                if (setjmp(toplevel) != 0)
                    393:                        Exit(0);
                    394:                if (tn(argp - args, args) == 1)
                    395:                        return (0);
                    396:                else
                    397:                        return (1);
                    398:        }
                    399:        (void)setjmp(toplevel);
                    400:        for (;;) {
                    401: #ifdef TN3270
                    402:                if (shell_active)
                    403:                        shell_continue();
                    404:                else
                    405: #endif
                    406:                        command(1, 0, 0);
                    407:        }
1.7       art       408:        return 0;
1.1       deraadt   409: }