1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * Copyright 2002 Sun Microsystems, Inc.  All rights reserved.
3*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
4*7c478bd9Sstevel@tonic-gate  */
5*7c478bd9Sstevel@tonic-gate 
6*7c478bd9Sstevel@tonic-gate /*
7*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1988, 1990 Regents of the University of California.
8*7c478bd9Sstevel@tonic-gate  * All rights reserved.
9*7c478bd9Sstevel@tonic-gate  *
10*7c478bd9Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
11*7c478bd9Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
12*7c478bd9Sstevel@tonic-gate  * are met:
13*7c478bd9Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
14*7c478bd9Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
15*7c478bd9Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
16*7c478bd9Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
17*7c478bd9Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
18*7c478bd9Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this software
19*7c478bd9Sstevel@tonic-gate  *    must display the following acknowledgement:
20*7c478bd9Sstevel@tonic-gate  *	This product includes software developed by the University of
21*7c478bd9Sstevel@tonic-gate  *	California, Berkeley and its contributors.
22*7c478bd9Sstevel@tonic-gate  * 4. Neither the name of the University nor the names of its contributors
23*7c478bd9Sstevel@tonic-gate  *    may be used to endorse or promote products derived from this software
24*7c478bd9Sstevel@tonic-gate  *    without specific prior written permission.
25*7c478bd9Sstevel@tonic-gate  *
26*7c478bd9Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27*7c478bd9Sstevel@tonic-gate  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28*7c478bd9Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29*7c478bd9Sstevel@tonic-gate  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30*7c478bd9Sstevel@tonic-gate  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31*7c478bd9Sstevel@tonic-gate  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32*7c478bd9Sstevel@tonic-gate  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33*7c478bd9Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34*7c478bd9Sstevel@tonic-gate  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35*7c478bd9Sstevel@tonic-gate  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36*7c478bd9Sstevel@tonic-gate  * SUCH DAMAGE.
37*7c478bd9Sstevel@tonic-gate  */
38*7c478bd9Sstevel@tonic-gate 
39*7c478bd9Sstevel@tonic-gate #ifndef lint
40*7c478bd9Sstevel@tonic-gate char copyright[] =
41*7c478bd9Sstevel@tonic-gate "@(#) Copyright (c) 1988, 1990 Regents of the University of California.\n"
42*7c478bd9Sstevel@tonic-gate " All rights reserved.\n";
43*7c478bd9Sstevel@tonic-gate #endif /* not lint */
44*7c478bd9Sstevel@tonic-gate 
45*7c478bd9Sstevel@tonic-gate #ifndef lint
46*7c478bd9Sstevel@tonic-gate static char sccsid[] = "@(#)main.c	5.5 (Berkeley) 12/18/92";
47*7c478bd9Sstevel@tonic-gate #endif /* not lint */
48*7c478bd9Sstevel@tonic-gate 
49*7c478bd9Sstevel@tonic-gate #include <string.h>
50*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
51*7c478bd9Sstevel@tonic-gate 
52*7c478bd9Sstevel@tonic-gate #include "ring.h"
53*7c478bd9Sstevel@tonic-gate #include "externs.h"
54*7c478bd9Sstevel@tonic-gate #include "defines.h"
55*7c478bd9Sstevel@tonic-gate 
56*7c478bd9Sstevel@tonic-gate /* These values need to be the same as defined in libtelnet/kerberos5.c */
57*7c478bd9Sstevel@tonic-gate /* Either define them in both places, or put in some common header file. */
58*7c478bd9Sstevel@tonic-gate #define	OPTS_FORWARD_CREDS		0x00000002
59*7c478bd9Sstevel@tonic-gate #define	OPTS_FORWARDABLE_CREDS		0x00000001
60*7c478bd9Sstevel@tonic-gate 
61*7c478bd9Sstevel@tonic-gate /*
62*7c478bd9Sstevel@tonic-gate  * This flag is incremented, if any of the
63*7c478bd9Sstevel@tonic-gate  * Kerberos command line options are used.
64*7c478bd9Sstevel@tonic-gate  */
65*7c478bd9Sstevel@tonic-gate int krb5auth_flag = 0;
66*7c478bd9Sstevel@tonic-gate 
67*7c478bd9Sstevel@tonic-gate /*
68*7c478bd9Sstevel@tonic-gate  * Initialize variables.
69*7c478bd9Sstevel@tonic-gate  */
70*7c478bd9Sstevel@tonic-gate int
tninit()71*7c478bd9Sstevel@tonic-gate tninit()
72*7c478bd9Sstevel@tonic-gate {
73*7c478bd9Sstevel@tonic-gate 	init_terminal();
74*7c478bd9Sstevel@tonic-gate 
75*7c478bd9Sstevel@tonic-gate 	init_network();
76*7c478bd9Sstevel@tonic-gate 
77*7c478bd9Sstevel@tonic-gate 	if (init_telnet() == 0)
78*7c478bd9Sstevel@tonic-gate 		return (0);
79*7c478bd9Sstevel@tonic-gate 
80*7c478bd9Sstevel@tonic-gate 	init_sys();
81*7c478bd9Sstevel@tonic-gate 
82*7c478bd9Sstevel@tonic-gate 	return (1);
83*7c478bd9Sstevel@tonic-gate }
84*7c478bd9Sstevel@tonic-gate 
85*7c478bd9Sstevel@tonic-gate #if	defined(USE_TOS)
86*7c478bd9Sstevel@tonic-gate #define	TELNET_OPTIONS	"8EKLS:X:acde:fFk:l:n:rt:x"
87*7c478bd9Sstevel@tonic-gate #else
88*7c478bd9Sstevel@tonic-gate #define	TELNET_OPTIONS	"8EKLX:acde:fFk:l:n:rt:x"
89*7c478bd9Sstevel@tonic-gate #endif	/* USE_TOS */
90*7c478bd9Sstevel@tonic-gate 
91*7c478bd9Sstevel@tonic-gate static void
usage()92*7c478bd9Sstevel@tonic-gate usage()
93*7c478bd9Sstevel@tonic-gate {
94*7c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "Usage: %s %s\n",
95*7c478bd9Sstevel@tonic-gate 		prompt,
96*7c478bd9Sstevel@tonic-gate 		" [-8] [-E] [-K] [-L] [-a] [-c] [-d] [-f/-F] [-r] [-x]"
97*7c478bd9Sstevel@tonic-gate 		"\n\t[-e char] [-k realm] [-l user] [-n tracefile] [-X atype]"
98*7c478bd9Sstevel@tonic-gate 		"\n\t[host-name [port]]");
99*7c478bd9Sstevel@tonic-gate 	exit(1);
100*7c478bd9Sstevel@tonic-gate }
101*7c478bd9Sstevel@tonic-gate 
102*7c478bd9Sstevel@tonic-gate /*
103*7c478bd9Sstevel@tonic-gate  * main.  Parse arguments, invoke the protocol or command parser.
104*7c478bd9Sstevel@tonic-gate  */
105*7c478bd9Sstevel@tonic-gate 
106*7c478bd9Sstevel@tonic-gate 
107*7c478bd9Sstevel@tonic-gate int
main(int argc,char * argv[])108*7c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
109*7c478bd9Sstevel@tonic-gate {
110*7c478bd9Sstevel@tonic-gate 	int ch;
111*7c478bd9Sstevel@tonic-gate 	char *user;
112*7c478bd9Sstevel@tonic-gate 	extern boolean_t auth_enable_encrypt;
113*7c478bd9Sstevel@tonic-gate 	extern int forward_flags;
114*7c478bd9Sstevel@tonic-gate 
115*7c478bd9Sstevel@tonic-gate 	/* Clear out things */
116*7c478bd9Sstevel@tonic-gate 	if (tninit() == 0)
117*7c478bd9Sstevel@tonic-gate 		return (EXIT_FAILURE);
118*7c478bd9Sstevel@tonic-gate 
119*7c478bd9Sstevel@tonic-gate 	if (!isatty(fileno(stdin))) {
120*7c478bd9Sstevel@tonic-gate 		setbuf(stdin, NULL);
121*7c478bd9Sstevel@tonic-gate 	}
122*7c478bd9Sstevel@tonic-gate 	if (!isatty(fileno(stdout))) {
123*7c478bd9Sstevel@tonic-gate 		setbuf(stdout, NULL);
124*7c478bd9Sstevel@tonic-gate 	}
125*7c478bd9Sstevel@tonic-gate 
126*7c478bd9Sstevel@tonic-gate 	TerminalSaveState();
127*7c478bd9Sstevel@tonic-gate 
128*7c478bd9Sstevel@tonic-gate 	if (prompt = strrchr(argv[0], '/'))
129*7c478bd9Sstevel@tonic-gate 		++prompt;
130*7c478bd9Sstevel@tonic-gate 	else
131*7c478bd9Sstevel@tonic-gate 		prompt = argv[0];
132*7c478bd9Sstevel@tonic-gate 
133*7c478bd9Sstevel@tonic-gate 	user = NULL;
134*7c478bd9Sstevel@tonic-gate 
135*7c478bd9Sstevel@tonic-gate 	rlogin = (strncmp(prompt, "rlog", 4) == 0) ? '~' : _POSIX_VDISABLE;
136*7c478bd9Sstevel@tonic-gate 	autologin = -1;
137*7c478bd9Sstevel@tonic-gate 
138*7c478bd9Sstevel@tonic-gate 	while ((ch = getopt(argc, argv, TELNET_OPTIONS)) != EOF) {
139*7c478bd9Sstevel@tonic-gate 		switch (ch) {
140*7c478bd9Sstevel@tonic-gate 		case 'K':
141*7c478bd9Sstevel@tonic-gate 			autologin_set = 1;
142*7c478bd9Sstevel@tonic-gate 			autologin = 0;
143*7c478bd9Sstevel@tonic-gate 			krb5auth_flag++;
144*7c478bd9Sstevel@tonic-gate 			break;
145*7c478bd9Sstevel@tonic-gate 		case 'X':
146*7c478bd9Sstevel@tonic-gate 			auth_disable_name(optarg);
147*7c478bd9Sstevel@tonic-gate 			krb5auth_flag++;
148*7c478bd9Sstevel@tonic-gate 			break;
149*7c478bd9Sstevel@tonic-gate 		case 'a':
150*7c478bd9Sstevel@tonic-gate 			autologin_set = 1;
151*7c478bd9Sstevel@tonic-gate 			autologin = 1;
152*7c478bd9Sstevel@tonic-gate 			krb5auth_flag++;
153*7c478bd9Sstevel@tonic-gate 			break;
154*7c478bd9Sstevel@tonic-gate 		case 'f':
155*7c478bd9Sstevel@tonic-gate 			if (forward_flags & OPTS_FORWARD_CREDS) {
156*7c478bd9Sstevel@tonic-gate 			    (void) fprintf(stderr, gettext(
157*7c478bd9Sstevel@tonic-gate 				"%s: Only one of -f "
158*7c478bd9Sstevel@tonic-gate 				"and -F allowed.\n"), prompt);
159*7c478bd9Sstevel@tonic-gate 			    usage();
160*7c478bd9Sstevel@tonic-gate 			}
161*7c478bd9Sstevel@tonic-gate 			forward_flags |= OPTS_FORWARD_CREDS;
162*7c478bd9Sstevel@tonic-gate 			forward_flag_set = 1;
163*7c478bd9Sstevel@tonic-gate 			krb5auth_flag++;
164*7c478bd9Sstevel@tonic-gate 			break;
165*7c478bd9Sstevel@tonic-gate 		case 'F':
166*7c478bd9Sstevel@tonic-gate 			if (forward_flags & OPTS_FORWARD_CREDS) {
167*7c478bd9Sstevel@tonic-gate 			    (void) fprintf(stderr, gettext(
168*7c478bd9Sstevel@tonic-gate 				"%s: Only one of -f "
169*7c478bd9Sstevel@tonic-gate 				"and -F allowed.\n"), prompt);
170*7c478bd9Sstevel@tonic-gate 			    usage();
171*7c478bd9Sstevel@tonic-gate 			}
172*7c478bd9Sstevel@tonic-gate 			forward_flags |= OPTS_FORWARD_CREDS;
173*7c478bd9Sstevel@tonic-gate 			forward_flags |= OPTS_FORWARDABLE_CREDS;
174*7c478bd9Sstevel@tonic-gate 			forwardable_flag_set = 1;
175*7c478bd9Sstevel@tonic-gate 			forward_flag = 1;
176*7c478bd9Sstevel@tonic-gate 			krb5auth_flag++;
177*7c478bd9Sstevel@tonic-gate 			break;
178*7c478bd9Sstevel@tonic-gate 		case 'k':
179*7c478bd9Sstevel@tonic-gate 			set_krb5_realm(optarg);
180*7c478bd9Sstevel@tonic-gate 			krb5auth_flag++;
181*7c478bd9Sstevel@tonic-gate 			break;
182*7c478bd9Sstevel@tonic-gate 		case 'x':
183*7c478bd9Sstevel@tonic-gate 			if (krb5_privacy_allowed()) {
184*7c478bd9Sstevel@tonic-gate 				encrypt_auto(1);
185*7c478bd9Sstevel@tonic-gate 				decrypt_auto(1);
186*7c478bd9Sstevel@tonic-gate 				wantencryption = B_TRUE;
187*7c478bd9Sstevel@tonic-gate 				autologin = 1;
188*7c478bd9Sstevel@tonic-gate 				autologin_set = 1;
189*7c478bd9Sstevel@tonic-gate 				auth_enable_encrypt = B_TRUE;
190*7c478bd9Sstevel@tonic-gate 				encrypt_flag_set = 1;
191*7c478bd9Sstevel@tonic-gate 				krb5auth_flag++;
192*7c478bd9Sstevel@tonic-gate 			} else {
193*7c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
194*7c478bd9Sstevel@tonic-gate 					"%s: Encryption not supported.\n"),
195*7c478bd9Sstevel@tonic-gate 					prompt);
196*7c478bd9Sstevel@tonic-gate 				exit(1);
197*7c478bd9Sstevel@tonic-gate 			}
198*7c478bd9Sstevel@tonic-gate 			break;
199*7c478bd9Sstevel@tonic-gate 
200*7c478bd9Sstevel@tonic-gate 		/* begin common options */
201*7c478bd9Sstevel@tonic-gate 		case '8':
202*7c478bd9Sstevel@tonic-gate 			eight = 3;	/* binary output and input */
203*7c478bd9Sstevel@tonic-gate 			break;
204*7c478bd9Sstevel@tonic-gate 		case 'E':
205*7c478bd9Sstevel@tonic-gate 			escape_valid = B_FALSE;
206*7c478bd9Sstevel@tonic-gate 			rlogin = escape = _POSIX_VDISABLE;
207*7c478bd9Sstevel@tonic-gate 			break;
208*7c478bd9Sstevel@tonic-gate 		case 'L':
209*7c478bd9Sstevel@tonic-gate 			eight |= 2;	/* binary output only */
210*7c478bd9Sstevel@tonic-gate 			break;
211*7c478bd9Sstevel@tonic-gate #if USE_TOS
212*7c478bd9Sstevel@tonic-gate 		case 'S':
213*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
214*7c478bd9Sstevel@tonic-gate 			    "%s: Warning: -S ignored, no parsetos() support.\n",
215*7c478bd9Sstevel@tonic-gate 			    prompt);
216*7c478bd9Sstevel@tonic-gate 			break;
217*7c478bd9Sstevel@tonic-gate #endif /* USE_TOS */
218*7c478bd9Sstevel@tonic-gate 		case 'c':
219*7c478bd9Sstevel@tonic-gate 			skiprc = 1;
220*7c478bd9Sstevel@tonic-gate 			break;
221*7c478bd9Sstevel@tonic-gate 		case 'd':
222*7c478bd9Sstevel@tonic-gate 			debug = 1;
223*7c478bd9Sstevel@tonic-gate 			break;
224*7c478bd9Sstevel@tonic-gate 		case 'e':
225*7c478bd9Sstevel@tonic-gate 			escape_valid = B_TRUE;
226*7c478bd9Sstevel@tonic-gate 			set_escape_char(optarg);
227*7c478bd9Sstevel@tonic-gate 			break;
228*7c478bd9Sstevel@tonic-gate 		case 'l':
229*7c478bd9Sstevel@tonic-gate 			autologin_set = 1;
230*7c478bd9Sstevel@tonic-gate 			autologin = 1;
231*7c478bd9Sstevel@tonic-gate 			user = optarg;
232*7c478bd9Sstevel@tonic-gate 			break;
233*7c478bd9Sstevel@tonic-gate 		case 'n':
234*7c478bd9Sstevel@tonic-gate 			SetNetTrace(optarg);
235*7c478bd9Sstevel@tonic-gate 			break;
236*7c478bd9Sstevel@tonic-gate 		case 'r':
237*7c478bd9Sstevel@tonic-gate 			rlogin = '~';
238*7c478bd9Sstevel@tonic-gate 			break;
239*7c478bd9Sstevel@tonic-gate 		case 't':
240*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
241*7c478bd9Sstevel@tonic-gate 			    "%s: Warning: -t ignored, no TN3270 support.\n",
242*7c478bd9Sstevel@tonic-gate 			    prompt);
243*7c478bd9Sstevel@tonic-gate 			break;
244*7c478bd9Sstevel@tonic-gate 		default:
245*7c478bd9Sstevel@tonic-gate 			usage();
246*7c478bd9Sstevel@tonic-gate 			/* NOTREACHED */
247*7c478bd9Sstevel@tonic-gate 		}
248*7c478bd9Sstevel@tonic-gate 	}
249*7c478bd9Sstevel@tonic-gate 	if (autologin == -1)
250*7c478bd9Sstevel@tonic-gate 		autologin = (rlogin == _POSIX_VDISABLE) ? 0 : 1;
251*7c478bd9Sstevel@tonic-gate 
252*7c478bd9Sstevel@tonic-gate 	argc -= optind;
253*7c478bd9Sstevel@tonic-gate 	argv += optind;
254*7c478bd9Sstevel@tonic-gate 
255*7c478bd9Sstevel@tonic-gate 	if (argc) {
256*7c478bd9Sstevel@tonic-gate 		char *args[7], **argp = args;
257*7c478bd9Sstevel@tonic-gate 
258*7c478bd9Sstevel@tonic-gate 		if (argc > 2)
259*7c478bd9Sstevel@tonic-gate 			usage();
260*7c478bd9Sstevel@tonic-gate 		*argp++ = prompt;
261*7c478bd9Sstevel@tonic-gate 		if (user) {
262*7c478bd9Sstevel@tonic-gate 			*argp++ = "-l";
263*7c478bd9Sstevel@tonic-gate 			*argp++ = user;
264*7c478bd9Sstevel@tonic-gate 		}
265*7c478bd9Sstevel@tonic-gate 		*argp++ = argv[0];		/* host */
266*7c478bd9Sstevel@tonic-gate 		if (argc > 1)
267*7c478bd9Sstevel@tonic-gate 			*argp++ = argv[1];	/* port */
268*7c478bd9Sstevel@tonic-gate 		*argp = 0;
269*7c478bd9Sstevel@tonic-gate 
270*7c478bd9Sstevel@tonic-gate 		if (setjmp(toplevel) != 0)
271*7c478bd9Sstevel@tonic-gate 			Exit(EXIT_SUCCESS);
272*7c478bd9Sstevel@tonic-gate 		if (tn(argp - args, args) == 1)
273*7c478bd9Sstevel@tonic-gate 			return (EXIT_SUCCESS);
274*7c478bd9Sstevel@tonic-gate 		else
275*7c478bd9Sstevel@tonic-gate 			return (EXIT_FAILURE);
276*7c478bd9Sstevel@tonic-gate 	}
277*7c478bd9Sstevel@tonic-gate 	(void) setjmp(toplevel);
278*7c478bd9Sstevel@tonic-gate 	for (;;) {
279*7c478bd9Sstevel@tonic-gate 		command(1, 0, 0);
280*7c478bd9Sstevel@tonic-gate 	}
281*7c478bd9Sstevel@tonic-gate }
282