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