Annotation of src/usr.bin/telnet/main.c, Revision 1.8
1.8 ! deraadt 1: /* $OpenBSD: main.c,v 1.7 1998/05/15 03:16:38 art 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.5 art 50: #if KRB5
1.1 deraadt 51: #define FORWARD
52: #endif
53:
1.6 art 54: #ifdef KRB4
55: #include <kerberosIV/krb.h>
56: #endif
57:
1.1 deraadt 58: /*
59: * Initialize variables.
60: */
61: void
62: tninit()
63: {
64: init_terminal();
65:
66: init_network();
1.2 niklas 67:
1.1 deraadt 68: init_telnet();
69:
70: init_sys();
71:
72: #if defined(TN3270)
73: init_3270();
74: #endif
75: }
76:
77: void
78: usage()
79: {
80: fprintf(stderr, "Usage: %s %s%s%s%s\n",
81: prompt,
82: #ifdef AUTHENTICATION
1.2 niklas 83: "[-8] [-E] [-K] [-L] [-S tos] [-X atype] [-a] [-c] [-d] [-e char]",
1.3 niklas 84: "\n\t[-k realm] [-l user] [-f/-F] [-n tracefile] [-b hostalias ]",
1.1 deraadt 85: #else
1.2 niklas 86: "[-8] [-E] [-L] [-S tos] [-a] [-c] [-d] [-e char] [-l user]",
1.3 niklas 87: "\n\t[-n tracefile] [-b hostalias ]",
1.1 deraadt 88: #endif
89: #if defined(TN3270) && defined(unix)
90: # ifdef AUTHENTICATION
1.2 niklas 91: "[-noasynch] [-noasynctty]\n\t[-noasyncnet] [-r] [-t transcom] ",
1.1 deraadt 92: # else
1.2 niklas 93: "[-noasynch] [-noasynctty] [-noasyncnet] [-r]\n\t[-t transcom]",
1.1 deraadt 94: # endif
95: #else
96: "[-r] ",
97: #endif
1.5 art 98: #ifdef ENCRYPTION
99: "[-x] [host-name [port]]"
100: #else
101:
1.1 deraadt 102: "[host-name [port]]"
1.5 art 103: #endif
1.1 deraadt 104: );
105: exit(1);
106: }
107:
108: /*
109: * main. Parse arguments, invoke the protocol or command parser.
110: */
111:
1.7 art 112: int
1.1 deraadt 113: main(argc, argv)
114: int argc;
115: char *argv[];
116: {
117: extern char *optarg;
118: extern int optind;
119: int ch;
1.5 art 120: char *user, *alias;
1.1 deraadt 121: #ifdef FORWARD
122: extern int forward_flags;
123: #endif /* FORWARD */
124:
125: tninit(); /* Clear out things */
126:
127: TerminalSaveState();
128:
1.5 art 129: if ((prompt = strrchr(argv[0], '/')))
1.1 deraadt 130: ++prompt;
131: else
132: prompt = argv[0];
133:
1.3 niklas 134: user = alias = NULL;
1.1 deraadt 135:
136: rlogin = (strncmp(prompt, "rlog", 4) == 0) ? '~' : _POSIX_VDISABLE;
1.5 art 137:
138: /*
139: * if AUTHENTICATION and ENCRYPTION is set autologin will be
140: * set to true after the getopt switch; unless the -K option is
141: * passed
142: */
1.1 deraadt 143: autologin = -1;
144:
1.5 art 145: while ((ch = getopt(argc, argv, "78DEKLS:X:ab:cde:fFk:l:n:rt:x")) != -1) {
1.1 deraadt 146: switch(ch) {
147: case '8':
148: eight = 3; /* binary output and input */
149: break;
1.5 art 150: case '7':
151: eight = 0;
152: break;
153: case 'D': {
154: /* sometimes we don't want a mangled display */
155: char *p;
156: if((p = getenv("DISPLAY")))
157: env_define("DISPLAY", (unsigned char*)p);
158: break;
159: }
160:
1.1 deraadt 161: case 'E':
162: rlogin = escape = _POSIX_VDISABLE;
163: break;
164: case 'K':
165: #ifdef AUTHENTICATION
166: autologin = 0;
167: #endif
168: break;
169: case 'L':
170: eight |= 2; /* binary output only */
171: break;
172: case 'S':
173: {
174: #ifdef HAS_GETTOS
175: extern int tos;
176:
177: if ((tos = parsetos(optarg, "tcp")) < 0)
178: fprintf(stderr, "%s%s%s%s\n",
179: prompt, ": Bad TOS argument '",
180: optarg,
181: "; will try to use default TOS");
182: #else
183: fprintf(stderr,
184: "%s: Warning: -S ignored, no parsetos() support.\n",
185: prompt);
186: #endif
187: }
188: break;
189: case 'X':
190: #ifdef AUTHENTICATION
191: auth_disable_name(optarg);
192: #endif
193: break;
194: case 'a':
195: autologin = 1;
196: break;
197: case 'c':
198: skiprc = 1;
199: break;
200: case 'd':
201: debug = 1;
202: break;
203: case 'e':
204: set_escape_char(optarg);
205: break;
206: case 'f':
207: #if defined(AUTHENTICATION) && defined(KRB5) && defined(FORWARD)
208: if (forward_flags & OPTS_FORWARD_CREDS) {
1.2 niklas 209: fprintf(stderr,
1.1 deraadt 210: "%s: Only one of -f and -F allowed.\n",
211: prompt);
212: usage();
213: }
214: forward_flags |= OPTS_FORWARD_CREDS;
215: #else
216: fprintf(stderr,
1.2 niklas 217: "%s: Warning: -f ignored, no Kerberos V5 support.\n",
1.1 deraadt 218: prompt);
219: #endif
220: break;
221: case 'F':
222: #if defined(AUTHENTICATION) && defined(KRB5) && defined(FORWARD)
223: if (forward_flags & OPTS_FORWARD_CREDS) {
1.2 niklas 224: fprintf(stderr,
1.1 deraadt 225: "%s: Only one of -f and -F allowed.\n",
226: prompt);
227: usage();
228: }
229: forward_flags |= OPTS_FORWARD_CREDS;
230: forward_flags |= OPTS_FORWARDABLE_CREDS;
231: #else
232: fprintf(stderr,
1.2 niklas 233: "%s: Warning: -F ignored, no Kerberos V5 support.\n",
1.1 deraadt 234: prompt);
235: #endif
236: break;
237: case 'k':
238: #if defined(AUTHENTICATION) && defined(KRB4)
239: {
1.5 art 240: extern char *dest_realm, dst_realm_buf[];
241: extern int dst_realm_sz;
1.1 deraadt 242: dest_realm = dst_realm_buf;
243: (void)strncpy(dest_realm, optarg, dst_realm_sz);
244: }
245: #else
246: fprintf(stderr,
247: "%s: Warning: -k ignored, no Kerberos V4 support.\n",
248: prompt);
249: #endif
250: break;
251: case 'l':
1.6 art 252: autologin = -1;
1.1 deraadt 253: user = optarg;
254: break;
1.3 niklas 255: case 'b':
256: alias = optarg;
257: break;
1.1 deraadt 258: case 'n':
259: #if defined(TN3270) && defined(unix)
260: /* distinguish between "-n oasynch" and "-noasynch" */
261: if (argv[optind - 1][0] == '-' && argv[optind - 1][1]
262: == 'n' && argv[optind - 1][2] == 'o') {
263: if (!strcmp(optarg, "oasynch")) {
264: noasynchtty = 1;
265: noasynchnet = 1;
266: } else if (!strcmp(optarg, "oasynchtty"))
267: noasynchtty = 1;
268: else if (!strcmp(optarg, "oasynchnet"))
269: noasynchnet = 1;
270: } else
271: #endif /* defined(TN3270) && defined(unix) */
272: SetNetTrace(optarg);
273: break;
274: case 'r':
275: rlogin = '~';
276: break;
277: case 't':
278: #if defined(TN3270) && defined(unix)
279: transcom = tline;
1.8 ! deraadt 280: (void)strlcpy(transcom, optarg, sizeof transcom);
1.1 deraadt 281: #else
282: fprintf(stderr,
283: "%s: Warning: -t ignored, no TN3270 support.\n",
284: prompt);
285: #endif
286: break;
287: case 'x':
1.5 art 288: #ifdef ENCRYPTION
289: encrypt_auto(1);
290: decrypt_auto(1);
291: EncryptVerbose(1);
292: #else
1.1 deraadt 293: fprintf(stderr,
294: "%s: Warning: -x ignored, no ENCRYPT support.\n",
295: prompt);
1.5 art 296: #endif
1.1 deraadt 297: break;
298: case '?':
299: default:
300: usage();
301: /* NOTREACHED */
302: }
303: }
1.5 art 304:
1.6 art 305: #ifdef KRB4
306: {
307: char realm[REALM_SZ];
308:
309: if (krb_get_lrealm(realm, 0) != KSUCCESS) {
310: #if defined(AUTHENTICATION)
311: auth_disable_name("KERBEROS_V4");
312: #endif
313: } else if (autologin == -1) {
1.5 art 314: #if defined(AUTHENTICATION)
1.6 art 315: autologin = 1;
1.5 art 316: #endif
317: #if defined(ENCRYPTION)
1.6 art 318: encrypt_auto(1);
319: decrypt_auto(1);
1.5 art 320: #endif
1.6 art 321: }
1.5 art 322: }
1.6 art 323: #endif /* KRB4 */
1.5 art 324:
1.1 deraadt 325: if (autologin == -1)
326: autologin = (rlogin == _POSIX_VDISABLE) ? 0 : 1;
327:
328: argc -= optind;
329: argv += optind;
330:
331: if (argc) {
332: char *args[7], **argp = args;
333:
334: if (argc > 2)
335: usage();
336: *argp++ = prompt;
337: if (user) {
338: *argp++ = "-l";
339: *argp++ = user;
1.3 niklas 340: }
341: if (alias) {
342: *argp++ = "-b";
343: *argp++ = alias;
1.1 deraadt 344: }
345: *argp++ = argv[0]; /* host */
346: if (argc > 1)
347: *argp++ = argv[1]; /* port */
348: *argp = 0;
349:
350: if (setjmp(toplevel) != 0)
351: Exit(0);
352: if (tn(argp - args, args) == 1)
353: return (0);
354: else
355: return (1);
356: }
357: (void)setjmp(toplevel);
358: for (;;) {
359: #ifdef TN3270
360: if (shell_active)
361: shell_continue();
362: else
363: #endif
364: command(1, 0, 0);
365: }
1.7 art 366: return 0;
1.1 deraadt 367: }