Annotation of src/usr.bin/telnet/main.c, Revision 1.18
1.18 ! sobrado 1: /* $OpenBSD: main.c,v 1.17 2009/06/05 00:20:46 claudio 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.
1.14 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:
33: #ifndef lint
1.2 niklas 34: static char copyright[] =
35: "@(#) Copyright (c) 1988, 1990, 1993\n\
36: The Regents of the University of California. All rights reserved.\n";
1.1 deraadt 37: #endif /* not lint */
38:
1.5 art 39: #include "telnet_locl.h"
1.1 deraadt 40:
41: /* These values need to be the same as defined in libtelnet/kerberos5.c */
42: /* Either define them in both places, or put in some common header file. */
1.2 niklas 43: #define OPTS_FORWARD_CREDS 0x00000002
44: #define OPTS_FORWARDABLE_CREDS 0x00000001
1.1 deraadt 45:
1.11 hin 46: #ifdef KRB5
1.1 deraadt 47: #define FORWARD
1.11 hin 48: /* XXX ugly hack to setup dns-proxy stuff */
49: #define Authenticator asn1_Authenticator
50: #include <kerberosV/krb5.h>
1.1 deraadt 51: #endif
52:
1.6 art 53: #ifdef KRB4
54: #include <kerberosIV/krb.h>
55: #endif
56:
1.11 hin 57: #ifdef FORWARD
58: int forward_flags;
59: static int default_forward=0;
60: #endif
61:
1.15 otto 62: int family = AF_UNSPEC;
1.17 claudio 63: u_int rdomain;
1.15 otto 64:
1.1 deraadt 65: /*
66: * Initialize variables.
67: */
68: void
69: tninit()
70: {
71: init_terminal();
72:
73: init_network();
1.2 niklas 74:
1.1 deraadt 75: init_telnet();
76:
77: init_sys();
78:
79: #if defined(TN3270)
80: init_3270();
81: #endif
82: }
83:
84: void
85: usage()
86: {
1.16 jmc 87: extern char *__progname;
88:
89: (void)fprintf(stderr,
90: #if defined(TN3270)
91: "usage: %s [-d] [-n filename] [-t commandname] [sysname [port]]\n",
1.1 deraadt 92: # else
1.16 jmc 93: "usage: %s [-468acdEFfKLrx] [-b hostalias] [-e escapechar] "
94: "[-k realm]\n"
1.18 ! sobrado 95: "\t[-l user] [-n tracefile] [-V rdomain] [-X authtype] "
1.17 claudio 96: "[host [port]]\n",
1.1 deraadt 97: #endif
1.16 jmc 98: __progname);
99:
1.1 deraadt 100: exit(1);
101: }
102:
1.11 hin 103:
104: #ifdef KRB5
105: static void
106: krb5_init(void)
107: {
108: krb5_context context;
109: krb5_error_code ret;
110:
111: ret = krb5_init_context(&context);
112: if (ret)
113: return;
114:
115: #if defined(AUTHENTICATION) && defined(KRB5) && defined(FORWARD)
116: if (krb5_config_get_bool (context, NULL,
117: "libdefaults", "forward", NULL)) {
118: forward_flags |= OPTS_FORWARD_CREDS;
119: default_forward=1;
120: }
121: if (krb5_config_get_bool (context, NULL,
122: "libdefaults", "forwardable", NULL)) {
123: forward_flags |= OPTS_FORWARDABLE_CREDS;
124: default_forward=1;
125: }
126: #endif
127: #ifdef ENCRYPTION
128: if (krb5_config_get_bool (context, NULL,
129: "libdefaults", "encrypt", NULL)) {
130: encrypt_auto(1);
131: decrypt_auto(1);
1.13 hin 132: wantencryption = 1;
1.11 hin 133: EncryptVerbose(1);
134: }
135: #endif
136:
137: krb5_free_context(context);
138: }
139: #endif
140:
1.1 deraadt 141: /*
142: * main. Parse arguments, invoke the protocol or command parser.
143: */
144:
1.7 art 145: int
1.1 deraadt 146: main(argc, argv)
147: int argc;
148: char *argv[];
149: {
150: extern char *optarg;
151: extern int optind;
152: int ch;
1.5 art 153: char *user, *alias;
1.17 claudio 154: const char *errstr;
1.1 deraadt 155: #ifdef FORWARD
156: extern int forward_flags;
157: #endif /* FORWARD */
158:
1.11 hin 159: #ifdef KRB5
160: krb5_init();
161: #endif
162:
1.1 deraadt 163: tninit(); /* Clear out things */
164:
165: TerminalSaveState();
166:
1.5 art 167: if ((prompt = strrchr(argv[0], '/')))
1.1 deraadt 168: ++prompt;
169: else
170: prompt = argv[0];
171:
1.3 niklas 172: user = alias = NULL;
1.1 deraadt 173:
174: rlogin = (strncmp(prompt, "rlog", 4) == 0) ? '~' : _POSIX_VDISABLE;
1.5 art 175:
176: /*
177: * if AUTHENTICATION and ENCRYPTION is set autologin will be
178: * set to true after the getopt switch; unless the -K option is
179: * passed
180: */
1.1 deraadt 181: autologin = -1;
182:
1.17 claudio 183: while ((ch = getopt(argc, argv, "4678DEKLS:X:ab:cde:fFk:l:n:rt:V:x"))
1.15 otto 184: != -1) {
1.1 deraadt 185: switch(ch) {
1.15 otto 186: case '4':
187: family = AF_INET;
188: break;
189: case '6':
190: family = AF_INET6;
191: break;
1.1 deraadt 192: case '8':
193: eight = 3; /* binary output and input */
194: break;
1.5 art 195: case '7':
196: eight = 0;
197: break;
198: case 'D': {
199: /* sometimes we don't want a mangled display */
200: char *p;
201: if((p = getenv("DISPLAY")))
202: env_define("DISPLAY", (unsigned char*)p);
203: break;
204: }
205:
1.1 deraadt 206: case 'E':
207: rlogin = escape = _POSIX_VDISABLE;
208: break;
209: case 'K':
210: #ifdef AUTHENTICATION
211: autologin = 0;
212: #endif
213: break;
214: case 'L':
215: eight |= 2; /* binary output only */
216: break;
217: case 'S':
218: {
219: #ifdef HAS_GETTOS
220: extern int tos;
221:
222: if ((tos = parsetos(optarg, "tcp")) < 0)
223: fprintf(stderr, "%s%s%s%s\n",
224: prompt, ": Bad TOS argument '",
225: optarg,
226: "; will try to use default TOS");
227: #else
228: fprintf(stderr,
229: "%s: Warning: -S ignored, no parsetos() support.\n",
230: prompt);
231: #endif
232: }
233: break;
234: case 'X':
235: #ifdef AUTHENTICATION
236: auth_disable_name(optarg);
237: #endif
238: break;
239: case 'a':
240: autologin = 1;
241: break;
242: case 'c':
243: skiprc = 1;
244: break;
245: case 'd':
246: debug = 1;
247: break;
248: case 'e':
249: set_escape_char(optarg);
250: break;
251: case 'f':
252: #if defined(AUTHENTICATION) && defined(KRB5) && defined(FORWARD)
1.11 hin 253: if ((forward_flags & OPTS_FORWARD_CREDS) &&
254: !default_forward) {
1.2 niklas 255: fprintf(stderr,
1.1 deraadt 256: "%s: Only one of -f and -F allowed.\n",
257: prompt);
258: usage();
259: }
260: forward_flags |= OPTS_FORWARD_CREDS;
261: #else
262: fprintf(stderr,
1.2 niklas 263: "%s: Warning: -f ignored, no Kerberos V5 support.\n",
1.1 deraadt 264: prompt);
265: #endif
266: break;
267: case 'F':
268: #if defined(AUTHENTICATION) && defined(KRB5) && defined(FORWARD)
1.11 hin 269: if ((forward_flags & OPTS_FORWARD_CREDS) &&
270: !default_forward) {
1.2 niklas 271: fprintf(stderr,
1.1 deraadt 272: "%s: Only one of -f and -F allowed.\n",
273: prompt);
274: usage();
275: }
276: forward_flags |= OPTS_FORWARD_CREDS;
277: forward_flags |= OPTS_FORWARDABLE_CREDS;
278: #else
279: fprintf(stderr,
1.2 niklas 280: "%s: Warning: -F ignored, no Kerberos V5 support.\n",
1.1 deraadt 281: prompt);
282: #endif
283: break;
284: case 'k':
285: #if defined(AUTHENTICATION) && defined(KRB4)
286: {
1.5 art 287: extern char *dest_realm, dst_realm_buf[];
288: extern int dst_realm_sz;
1.1 deraadt 289: dest_realm = dst_realm_buf;
290: (void)strncpy(dest_realm, optarg, dst_realm_sz);
291: }
292: #else
293: fprintf(stderr,
294: "%s: Warning: -k ignored, no Kerberos V4 support.\n",
295: prompt);
296: #endif
297: break;
298: case 'l':
1.6 art 299: autologin = -1;
1.1 deraadt 300: user = optarg;
301: break;
1.3 niklas 302: case 'b':
303: alias = optarg;
304: break;
1.1 deraadt 305: case 'n':
306: #if defined(TN3270) && defined(unix)
307: /* distinguish between "-n oasynch" and "-noasynch" */
308: if (argv[optind - 1][0] == '-' && argv[optind - 1][1]
309: == 'n' && argv[optind - 1][2] == 'o') {
310: if (!strcmp(optarg, "oasynch")) {
311: noasynchtty = 1;
312: noasynchnet = 1;
313: } else if (!strcmp(optarg, "oasynchtty"))
314: noasynchtty = 1;
315: else if (!strcmp(optarg, "oasynchnet"))
316: noasynchnet = 1;
317: } else
318: #endif /* defined(TN3270) && defined(unix) */
319: SetNetTrace(optarg);
320: break;
321: case 'r':
322: rlogin = '~';
323: break;
324: case 't':
325: #if defined(TN3270) && defined(unix)
1.10 aaron 326: (void)strlcpy(tline, optarg, sizeof tline);
1.1 deraadt 327: transcom = tline;
328: #else
329: fprintf(stderr,
330: "%s: Warning: -t ignored, no TN3270 support.\n",
331: prompt);
332: #endif
1.17 claudio 333: break;
334: case 'V':
335: rdomain = (unsigned int)strtonum(optarg, 0,
336: RT_TABLEID_MAX, &errstr);
337: if (errstr) {
338: fprintf(stderr,
339: "%s: Warning: -R ignored, rdomain %s: %s\n",
340: prompt, errstr, optarg);
341: }
1.1 deraadt 342: break;
343: case 'x':
1.5 art 344: #ifdef ENCRYPTION
345: encrypt_auto(1);
346: decrypt_auto(1);
1.13 hin 347: wantencryption = 1;
1.5 art 348: EncryptVerbose(1);
349: #else
1.1 deraadt 350: fprintf(stderr,
351: "%s: Warning: -x ignored, no ENCRYPT support.\n",
352: prompt);
1.5 art 353: #endif
1.1 deraadt 354: break;
355: case '?':
356: default:
357: usage();
358: /* NOTREACHED */
359: }
360: }
1.5 art 361:
1.11 hin 362: if (autologin == -1) {
1.6 art 363: #if defined(AUTHENTICATION)
1.13 hin 364: if(check_krb4_tickets() || check_krb5_tickets())
365: autologin = 1;
1.5 art 366: #endif
367: #if defined(ENCRYPTION)
1.11 hin 368: encrypt_auto(1);
369: decrypt_auto(1);
1.5 art 370: #endif
371: }
1.13 hin 372:
1.1 deraadt 373: if (autologin == -1)
374: autologin = (rlogin == _POSIX_VDISABLE) ? 0 : 1;
375:
376: argc -= optind;
377: argv += optind;
378:
379: if (argc) {
380: char *args[7], **argp = args;
381:
382: if (argc > 2)
383: usage();
384: *argp++ = prompt;
385: if (user) {
386: *argp++ = "-l";
387: *argp++ = user;
1.3 niklas 388: }
389: if (alias) {
390: *argp++ = "-b";
391: *argp++ = alias;
1.1 deraadt 392: }
393: *argp++ = argv[0]; /* host */
394: if (argc > 1)
395: *argp++ = argv[1]; /* port */
396: *argp = 0;
397:
398: if (setjmp(toplevel) != 0)
399: Exit(0);
400: if (tn(argp - args, args) == 1)
401: return (0);
402: else
403: return (1);
404: }
405: (void)setjmp(toplevel);
406: for (;;) {
407: #ifdef TN3270
408: if (shell_active)
409: shell_continue();
410: else
411: #endif
412: command(1, 0, 0);
413: }
1.7 art 414: return 0;
1.1 deraadt 415: }