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

1.12    ! hin         1: /*     $OpenBSD: main.c,v 1.11 2001/05/25 10:24:25 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);
                    143:           EncryptVerbose(1);
                    144:         }
                    145: #endif
                    146:
                    147:     krb5_free_context(context);
                    148: }
                    149: #endif
                    150:
1.1       deraadt   151: /*
                    152:  * main.  Parse arguments, invoke the protocol or command parser.
                    153:  */
                    154:
1.7       art       155:        int
1.1       deraadt   156: main(argc, argv)
                    157:        int argc;
                    158:        char *argv[];
                    159: {
                    160:        extern char *optarg;
                    161:        extern int optind;
                    162:        int ch;
1.5       art       163:        char *user, *alias;
1.1       deraadt   164: #ifdef FORWARD
                    165:        extern int forward_flags;
                    166: #endif /* FORWARD */
                    167:
1.11      hin       168: #ifdef KRB5
                    169:        krb5_init();
                    170: #endif
                    171:
1.1       deraadt   172:        tninit();               /* Clear out things */
                    173:
                    174:        TerminalSaveState();
                    175:
1.5       art       176:        if ((prompt = strrchr(argv[0], '/')))
1.1       deraadt   177:                ++prompt;
                    178:        else
                    179:                prompt = argv[0];
                    180:
1.3       niklas    181:        user = alias = NULL;
1.1       deraadt   182:
                    183:        rlogin = (strncmp(prompt, "rlog", 4) == 0) ? '~' : _POSIX_VDISABLE;
1.5       art       184:
                    185:        /*
                    186:         * if AUTHENTICATION and ENCRYPTION is set autologin will be
                    187:         * set to true after the getopt switch; unless the -K option is
                    188:         * passed
                    189:         */
1.1       deraadt   190:        autologin = -1;
                    191:
1.5       art       192:        while ((ch = getopt(argc, argv, "78DEKLS:X:ab:cde:fFk:l:n:rt:x")) != -1) {
1.1       deraadt   193:                switch(ch) {
                    194:                case '8':
                    195:                        eight = 3;      /* binary output and input */
                    196:                        break;
1.5       art       197:                case '7':
                    198:                        eight = 0;
                    199:                        break;
                    200:                case 'D': {
                    201:                        /* sometimes we don't want a mangled display */
                    202:                        char *p;
                    203:                        if((p = getenv("DISPLAY")))
                    204:                                env_define("DISPLAY", (unsigned char*)p);
                    205:                        break;
                    206:                }
                    207:
1.1       deraadt   208:                case 'E':
                    209:                        rlogin = escape = _POSIX_VDISABLE;
                    210:                        break;
                    211:                case 'K':
                    212: #ifdef AUTHENTICATION
                    213:                        autologin = 0;
                    214: #endif
                    215:                        break;
                    216:                case 'L':
                    217:                        eight |= 2;     /* binary output only */
                    218:                        break;
                    219:                case 'S':
                    220:                    {
                    221: #ifdef HAS_GETTOS
                    222:                        extern int tos;
                    223:
                    224:                        if ((tos = parsetos(optarg, "tcp")) < 0)
                    225:                                fprintf(stderr, "%s%s%s%s\n",
                    226:                                        prompt, ": Bad TOS argument '",
                    227:                                        optarg,
                    228:                                        "; will try to use default TOS");
                    229: #else
                    230:                        fprintf(stderr,
                    231:                           "%s: Warning: -S ignored, no parsetos() support.\n",
                    232:                                                                prompt);
                    233: #endif
                    234:                    }
                    235:                        break;
                    236:                case 'X':
                    237: #ifdef AUTHENTICATION
                    238:                        auth_disable_name(optarg);
                    239: #endif
                    240:                        break;
                    241:                case 'a':
                    242:                        autologin = 1;
                    243:                        break;
                    244:                case 'c':
                    245:                        skiprc = 1;
                    246:                        break;
                    247:                case 'd':
                    248:                        debug = 1;
                    249:                        break;
                    250:                case 'e':
                    251:                        set_escape_char(optarg);
                    252:                        break;
                    253:                case 'f':
                    254: #if defined(AUTHENTICATION) && defined(KRB5) && defined(FORWARD)
1.11      hin       255:                        if ((forward_flags & OPTS_FORWARD_CREDS) &&
                    256:                            !default_forward) {
1.2       niklas    257:                            fprintf(stderr,
1.1       deraadt   258:                                    "%s: Only one of -f and -F allowed.\n",
                    259:                                    prompt);
                    260:                            usage();
                    261:                        }
                    262:                        forward_flags |= OPTS_FORWARD_CREDS;
                    263: #else
                    264:                        fprintf(stderr,
1.2       niklas    265:                         "%s: Warning: -f ignored, no Kerberos V5 support.\n",
1.1       deraadt   266:                                prompt);
                    267: #endif
                    268:                        break;
                    269:                case 'F':
                    270: #if defined(AUTHENTICATION) && defined(KRB5) && defined(FORWARD)
1.11      hin       271:                        if ((forward_flags & OPTS_FORWARD_CREDS) &&
                    272:                            !default_forward) {
1.2       niklas    273:                            fprintf(stderr,
1.1       deraadt   274:                                    "%s: Only one of -f and -F allowed.\n",
                    275:                                    prompt);
                    276:                            usage();
                    277:                        }
                    278:                        forward_flags |= OPTS_FORWARD_CREDS;
                    279:                        forward_flags |= OPTS_FORWARDABLE_CREDS;
                    280: #else
                    281:                        fprintf(stderr,
1.2       niklas    282:                         "%s: Warning: -F ignored, no Kerberos V5 support.\n",
1.1       deraadt   283:                                prompt);
                    284: #endif
                    285:                        break;
                    286:                case 'k':
                    287: #if defined(AUTHENTICATION) && defined(KRB4)
                    288:                    {
1.5       art       289:                        extern char *dest_realm, dst_realm_buf[];
                    290:                        extern int dst_realm_sz;
1.1       deraadt   291:                        dest_realm = dst_realm_buf;
                    292:                        (void)strncpy(dest_realm, optarg, dst_realm_sz);
                    293:                    }
                    294: #else
                    295:                        fprintf(stderr,
                    296:                           "%s: Warning: -k ignored, no Kerberos V4 support.\n",
                    297:                                                                prompt);
                    298: #endif
                    299:                        break;
                    300:                case 'l':
1.6       art       301:                        autologin = -1;
1.1       deraadt   302:                        user = optarg;
                    303:                        break;
1.3       niklas    304:                case 'b':
                    305:                        alias = optarg;
                    306:                        break;
1.1       deraadt   307:                case 'n':
                    308: #if defined(TN3270) && defined(unix)
                    309:                        /* distinguish between "-n oasynch" and "-noasynch" */
                    310:                        if (argv[optind - 1][0] == '-' && argv[optind - 1][1]
                    311:                            == 'n' && argv[optind - 1][2] == 'o') {
                    312:                                if (!strcmp(optarg, "oasynch")) {
                    313:                                        noasynchtty = 1;
                    314:                                        noasynchnet = 1;
                    315:                                } else if (!strcmp(optarg, "oasynchtty"))
                    316:                                        noasynchtty = 1;
                    317:                                else if (!strcmp(optarg, "oasynchnet"))
                    318:                                        noasynchnet = 1;
                    319:                        } else
                    320: #endif /* defined(TN3270) && defined(unix) */
                    321:                                SetNetTrace(optarg);
                    322:                        break;
                    323:                case 'r':
                    324:                        rlogin = '~';
                    325:                        break;
                    326:                case 't':
                    327: #if defined(TN3270) && defined(unix)
1.10      aaron     328:                        (void)strlcpy(tline, optarg, sizeof tline);
1.1       deraadt   329:                        transcom = tline;
                    330: #else
                    331:                        fprintf(stderr,
                    332:                           "%s: Warning: -t ignored, no TN3270 support.\n",
                    333:                                                                prompt);
                    334: #endif
                    335:                        break;
                    336:                case 'x':
1.5       art       337: #ifdef ENCRYPTION
                    338:                        encrypt_auto(1);
                    339:                        decrypt_auto(1);
                    340:                        EncryptVerbose(1);
                    341: #else
1.1       deraadt   342:                        fprintf(stderr,
                    343:                            "%s: Warning: -x ignored, no ENCRYPT support.\n",
                    344:                                                                prompt);
1.5       art       345: #endif
1.1       deraadt   346:                        break;
                    347:                case '?':
                    348:                default:
                    349:                        usage();
                    350:                        /* NOTREACHED */
                    351:                }
                    352:        }
1.5       art       353:
1.11      hin       354:        if (autologin == -1) {
1.6       art       355: #if defined(AUTHENTICATION)
1.12    ! hin       356:            if(check_krb4_tickets() || check_krb5_tickets())
1.11      hin       357:                autologin = 1;
1.5       art       358: #endif
                    359: #if defined(ENCRYPTION)
1.11      hin       360:                encrypt_auto(1);
                    361:                decrypt_auto(1);
1.5       art       362: #endif
                    363:        }
1.11      hin       364:
1.1       deraadt   365:        if (autologin == -1)
                    366:                autologin = (rlogin == _POSIX_VDISABLE) ? 0 : 1;
                    367:
                    368:        argc -= optind;
                    369:        argv += optind;
                    370:
                    371:        if (argc) {
                    372:                char *args[7], **argp = args;
                    373:
                    374:                if (argc > 2)
                    375:                        usage();
                    376:                *argp++ = prompt;
                    377:                if (user) {
                    378:                        *argp++ = "-l";
                    379:                        *argp++ = user;
1.3       niklas    380:                }
                    381:                if (alias) {
                    382:                        *argp++ = "-b";
                    383:                        *argp++ = alias;
1.1       deraadt   384:                }
                    385:                *argp++ = argv[0];              /* host */
                    386:                if (argc > 1)
                    387:                        *argp++ = argv[1];      /* port */
                    388:                *argp = 0;
                    389:
                    390:                if (setjmp(toplevel) != 0)
                    391:                        Exit(0);
                    392:                if (tn(argp - args, args) == 1)
                    393:                        return (0);
                    394:                else
                    395:                        return (1);
                    396:        }
                    397:        (void)setjmp(toplevel);
                    398:        for (;;) {
                    399: #ifdef TN3270
                    400:                if (shell_active)
                    401:                        shell_continue();
                    402:                else
                    403: #endif
                    404:                        command(1, 0, 0);
                    405:        }
1.7       art       406:        return 0;
1.1       deraadt   407: }