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