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