xref: /netbsd-src/usr.bin/telnet/main.c (revision bcc8ec9959e7b01e313d813067bfb43a3ad70551)
1 /*	$NetBSD: main.c,v 1.14 2001/01/03 12:41:00 mjl Exp $	*/
2 
3 /*
4  * Copyright (c) 1988, 1990, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by the University of
18  *	California, Berkeley and its contributors.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #include <sys/cdefs.h>
37 #ifndef lint
38 __COPYRIGHT("@(#) Copyright (c) 1988, 1990, 1993\n\
39 	The Regents of the University of California.  All rights reserved.\n");
40 #endif /* not lint */
41 
42 #ifndef lint
43 #if 0
44 static char sccsid[] = "@(#)main.c	8.3 (Berkeley) 5/30/95";
45 #else
46 __RCSID("$NetBSD: main.c,v 1.14 2001/01/03 12:41:00 mjl Exp $");
47 #endif
48 #endif /* not lint */
49 
50 #include <sys/types.h>
51 
52 #include <unistd.h>
53 
54 #include "ring.h"
55 #include "externs.h"
56 #include "defines.h"
57 #ifdef AUTHENTICATION
58 #include <libtelnet/auth.h>
59 #endif
60 #ifdef ENCRYPTION
61 #include <libtelnet/encrypt.h>
62 #endif
63 
64 /* These values need to be the same as defined in libtelnet/kerberos5.c */
65 /* Either define them in both places, or put in some common header file. */
66 #define OPTS_FORWARD_CREDS	0x00000002
67 #define OPTS_FORWARDABLE_CREDS	0x00000001
68 
69 #if 0
70 #define FORWARD
71 #endif
72 
73 #if defined(IPSEC) && defined(IPSEC_POLICY_IPSEC)
74 char *ipsec_policy_in = NULL;
75 char *ipsec_policy_out = NULL;
76 #endif
77 
78 int main P((int, char *[]));
79 
80 /*
81  * Initialize variables.
82  */
83     void
84 tninit()
85 {
86     init_terminal();
87 
88     init_network();
89 
90     init_telnet();
91 
92     init_sys();
93 
94 #if defined(TN3270)
95     init_3270();
96 #endif
97 }
98 
99 	void
100 usage()
101 {
102 	fprintf(stderr, "Usage: %s %s%s%s%s\n",
103 	    prompt,
104 #ifdef	AUTHENTICATION
105 	    "[-8] [-E] [-K] [-L] [-N] [-S tos] [-X atype] [-a] [-c] [-d]",
106 	    "\n\t[-e char] [-k realm] [-l user] [-f/-F] [-n tracefile] ",
107 #else
108 	    "[-8] [-E] [-L] [-N] [-S tos] [-a] [-c] [-d] [-e char] [-l user]",
109 	    "\n\t[-n tracefile]",
110 #endif
111 #if defined(TN3270) && defined(unix)
112 # ifdef AUTHENTICATION
113 	    "[-noasynch] [-noasynctty]\n\t[-noasyncnet] [-r] [-t transcom] ",
114 # else
115 	    "[-noasynch] [-noasynctty] [-noasyncnet] [-r]\n\t[-t transcom]",
116 # endif
117 #else
118 	    "[-r] ",
119 #endif
120 #ifdef	ENCRYPTION
121 	    "[-x] "
122 #endif
123 #if defined(IPSEC) && defined(IPSEC_POLICY_IPSEC)
124 	    "[-P policy] [host-name [port]]"
125 #else
126 	    "[host-name [port]]"
127 #endif
128 	);
129 	exit(1);
130 }
131 
132 /*
133  * main.  Parse arguments, invoke the protocol or command parser.
134  */
135 
136 
137 int
138 main(argc, argv)
139 	int argc;
140 	char *argv[];
141 {
142 	extern char *optarg;
143 	extern int optind;
144 	int ch;
145 	char *user;
146 #ifdef	FORWARD
147 	extern int forward_flags;
148 #endif	/* FORWARD */
149 
150 	tninit();		/* Clear out things */
151 #if	defined(CRAY) && !defined(__STDC__)
152 	_setlist_init();	/* Work around compiler bug */
153 #endif
154 
155 	TerminalSaveState();
156 
157 	if ((prompt = strrchr(argv[0], '/')) != NULL)
158 		++prompt;
159 	else
160 		prompt = argv[0];
161 
162 	user = NULL;
163 
164 	rlogin = (strncmp(prompt, "rlog", 4) == 0) ? '~' : _POSIX_VDISABLE;
165 	autologin = -1;
166 
167 #if defined(IPSEC) && defined(IPSEC_POLICY_IPSEC)
168 #define IPSECOPT	"P:"
169 #else
170 #define IPSECOPT
171 #endif
172 	while ((ch = getopt(argc, argv, "8EKLNS:X:acde:fFk:l:n:rt:x"
173 			IPSECOPT)) != -1) {
174 #undef IPSECOPT
175 		switch(ch) {
176 		case '8':
177 			eight = 3;	/* binary output and input */
178 			break;
179 		case 'E':
180 			rlogin = escape = _POSIX_VDISABLE;
181 			break;
182 		case 'K':
183 #ifdef	AUTHENTICATION
184 			autologin = 0;
185 #endif
186 			break;
187 		case 'L':
188 			eight |= 2;	/* binary output only */
189 			break;
190 		case 'N':
191 			doaddrlookup = 0;
192 			break;
193 		case 'S':
194 		    {
195 #ifdef	HAS_GETTOS
196 			extern int tos;
197 
198 			if ((tos = parsetos(optarg, "tcp")) < 0)
199 				fprintf(stderr, "%s%s%s%s\n",
200 					prompt, ": Bad TOS argument '",
201 					optarg,
202 					"; will try to use default TOS");
203 #else
204 			fprintf(stderr,
205 			   "%s: Warning: -S ignored, no parsetos() support.\n",
206 								prompt);
207 #endif
208 		    }
209 			break;
210 		case 'X':
211 #ifdef	AUTHENTICATION
212 			auth_disable_name(optarg);
213 #endif
214 			break;
215 		case 'a':
216 			autologin = 1;
217 			break;
218 		case 'c':
219 			skiprc = 1;
220 			break;
221 		case 'd':
222 			debug = 1;
223 			break;
224 		case 'e':
225 			set_escape_char(optarg);
226 			break;
227 		case 'f':
228 #if defined(AUTHENTICATION) && defined(KRB5) && defined(FORWARD)
229 			if (forward_flags & OPTS_FORWARD_CREDS) {
230 			    fprintf(stderr,
231 				    "%s: Only one of -f and -F allowed.\n",
232 				    prompt);
233 			    usage();
234 			}
235 			forward_flags |= OPTS_FORWARD_CREDS;
236 #else
237 			fprintf(stderr,
238 			 "%s: Warning: -f ignored, no Kerberos V5 support.\n",
239 				prompt);
240 #endif
241 			break;
242 		case 'F':
243 #if defined(AUTHENTICATION) && defined(KRB5) && defined(FORWARD)
244 			if (forward_flags & OPTS_FORWARD_CREDS) {
245 			    fprintf(stderr,
246 				    "%s: Only one of -f and -F allowed.\n",
247 				    prompt);
248 			    usage();
249 			}
250 			forward_flags |= OPTS_FORWARD_CREDS;
251 			forward_flags |= OPTS_FORWARDABLE_CREDS;
252 #else
253 			fprintf(stderr,
254 			 "%s: Warning: -F ignored, no Kerberos V5 support.\n",
255 				prompt);
256 #endif
257 			break;
258 		case 'k':
259 #if defined(AUTHENTICATION) && defined(KRB4)
260 		    {
261 			extern char *dest_realm, dst_realm_buf[], dst_realm_sz;
262 			dest_realm = dst_realm_buf;
263 			(void)strncpy(dest_realm, optarg, dst_realm_sz);
264 		    }
265 #else
266 			fprintf(stderr,
267 			   "%s: Warning: -k ignored, no Kerberos V4 support.\n",
268 								prompt);
269 #endif
270 			break;
271 		case 'l':
272 			autologin = 1;
273 			user = optarg;
274 			break;
275 		case 'n':
276 #if defined(TN3270) && defined(unix)
277 			/* distinguish between "-n oasynch" and "-noasynch" */
278 			if (argv[optind - 1][0] == '-' && argv[optind - 1][1]
279 			    == 'n' && argv[optind - 1][2] == 'o') {
280 				if (!strcmp(optarg, "oasynch")) {
281 					noasynchtty = 1;
282 					noasynchnet = 1;
283 				} else if (!strcmp(optarg, "oasynchtty"))
284 					noasynchtty = 1;
285 				else if (!strcmp(optarg, "oasynchnet"))
286 					noasynchnet = 1;
287 			} else
288 #endif	/* defined(TN3270) && defined(unix) */
289 				SetNetTrace(optarg);
290 			break;
291 		case 'r':
292 			rlogin = '~';
293 			break;
294 		case 't':
295 #if defined(TN3270) && defined(unix)
296 			(void)strlcpy(tline, optarg, sizeof(tline));
297 			transcom = tline;
298 #else
299 			fprintf(stderr,
300 			   "%s: Warning: -t ignored, no TN3270 support.\n",
301 								prompt);
302 #endif
303 			break;
304 		case 'x':
305 #ifdef	ENCRYPTION
306 			encrypt_auto(1);
307 			decrypt_auto(1);
308 #else	/* ENCRYPTION */
309 			fprintf(stderr,
310 			    "%s: Warning: -x ignored, no ENCRYPT support.\n",
311 								prompt);
312 #endif	/* ENCRYPTION */
313 			break;
314 #if defined(IPSEC) && defined(IPSEC_POLICY_IPSEC)
315 		case 'P':
316 			if (!strncmp("in", optarg, 2))
317 				ipsec_policy_in = strdup(optarg);
318 			else if (!strncmp("out", optarg, 3))
319 				ipsec_policy_out = strdup(optarg);
320 			else
321 				usage();
322 			break;
323 #endif
324 		case '?':
325 		default:
326 			usage();
327 			/* NOTREACHED */
328 		}
329 	}
330 	if (autologin == -1)
331 		autologin = (rlogin == _POSIX_VDISABLE) ? 0 : 1;
332 
333 	argc -= optind;
334 	argv += optind;
335 
336 	if (argc) {
337 		char *args[7], **argp = args;
338 #ifdef __GNUC__
339 		(void) &argp;	/* avoid longjmp clobbering */
340 #endif
341 
342 		if (argc > 2)
343 			usage();
344 		*argp++ = prompt;
345 		if (user) {
346 			*argp++ = "-l";
347 			*argp++ = user;
348 		}
349 		*argp++ = argv[0];		/* host */
350 		if (argc > 1)
351 			*argp++ = argv[1];	/* port */
352 		*argp = 0;
353 
354 		if (setjmp(toplevel) != 0)
355 			Exit(0);
356 		if (tn(argp - args, args) == 1)
357 			return (0);
358 		else
359 			return (1);
360 	}
361 	(void)setjmp(toplevel);
362 	for (;;) {
363 #ifdef TN3270
364 		if (shell_active)
365 			shell_continue();
366 		else
367 #endif
368 			command(1, 0, 0);
369 	}
370 }
371