xref: /netbsd/usr.bin/telnet/main.c (revision bf9ec67e)
1 /*	$NetBSD: main.c,v 1.15 2001/03/04 01:51:05 assar 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.15 2001/03/04 01:51:05 assar 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 			if(autologin == 0) {
273 				autologin = -1;
274 			}
275 			user = optarg;
276 			break;
277 		case 'n':
278 #if defined(TN3270) && defined(unix)
279 			/* distinguish between "-n oasynch" and "-noasynch" */
280 			if (argv[optind - 1][0] == '-' && argv[optind - 1][1]
281 			    == 'n' && argv[optind - 1][2] == 'o') {
282 				if (!strcmp(optarg, "oasynch")) {
283 					noasynchtty = 1;
284 					noasynchnet = 1;
285 				} else if (!strcmp(optarg, "oasynchtty"))
286 					noasynchtty = 1;
287 				else if (!strcmp(optarg, "oasynchnet"))
288 					noasynchnet = 1;
289 			} else
290 #endif	/* defined(TN3270) && defined(unix) */
291 				SetNetTrace(optarg);
292 			break;
293 		case 'r':
294 			rlogin = '~';
295 			break;
296 		case 't':
297 #if defined(TN3270) && defined(unix)
298 			(void)strlcpy(tline, optarg, sizeof(tline));
299 			transcom = tline;
300 #else
301 			fprintf(stderr,
302 			   "%s: Warning: -t ignored, no TN3270 support.\n",
303 								prompt);
304 #endif
305 			break;
306 		case 'x':
307 #ifdef	ENCRYPTION
308 			encrypt_auto(1);
309 			decrypt_auto(1);
310 #else	/* ENCRYPTION */
311 			fprintf(stderr,
312 			    "%s: Warning: -x ignored, no ENCRYPT support.\n",
313 								prompt);
314 #endif	/* ENCRYPTION */
315 			break;
316 #if defined(IPSEC) && defined(IPSEC_POLICY_IPSEC)
317 		case 'P':
318 			if (!strncmp("in", optarg, 2))
319 				ipsec_policy_in = strdup(optarg);
320 			else if (!strncmp("out", optarg, 3))
321 				ipsec_policy_out = strdup(optarg);
322 			else
323 				usage();
324 			break;
325 #endif
326 		case '?':
327 		default:
328 			usage();
329 			/* NOTREACHED */
330 		}
331 	}
332 
333 	if (autologin == -1) {		/* esc@magic.fi; force  */
334 #if defined(AUTHENTICATION)
335 		autologin = 1;
336 #endif
337 #if defined(ENCRYPTION)
338 		encrypt_auto(1);
339 		decrypt_auto(1);
340 #endif
341 	}
342 
343 	if (autologin == -1)
344 		autologin = (rlogin == _POSIX_VDISABLE) ? 0 : 1;
345 
346 	argc -= optind;
347 	argv += optind;
348 
349 	if (argc) {
350 		char *args[7], **argp = args;
351 #ifdef __GNUC__
352 		(void) &argp;	/* avoid longjmp clobbering */
353 #endif
354 
355 		if (argc > 2)
356 			usage();
357 		*argp++ = prompt;
358 		if (user) {
359 			*argp++ = "-l";
360 			*argp++ = user;
361 		}
362 		*argp++ = argv[0];		/* host */
363 		if (argc > 1)
364 			*argp++ = argv[1];	/* port */
365 		*argp = 0;
366 
367 		if (setjmp(toplevel) != 0)
368 			Exit(0);
369 		if (tn(argp - args, args) == 1)
370 			return (0);
371 		else
372 			return (1);
373 	}
374 	(void)setjmp(toplevel);
375 	for (;;) {
376 #ifdef TN3270
377 		if (shell_active)
378 			shell_continue();
379 		else
380 #endif
381 			command(1, 0, 0);
382 	}
383 }
384