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