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