xref: /netbsd-src/usr.bin/telnet/main.c (revision 4472dbe5e3bd91ef2540bada7a7ca7384627ff9b)
1 /*	$NetBSD: main.c,v 1.12 2000/01/31 14:25:43 itojun 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.12 2000/01/31 14:25:43 itojun 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 
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 #if 0
64 #define FORWARD
65 #endif
66 
67 #if defined(IPSEC) && defined(IPSEC_POLICY_IPSEC)
68 char *ipsec_policy_in = NULL;
69 char *ipsec_policy_out = NULL;
70 #endif
71 
72 int main P((int, char *[]));
73 
74 /*
75  * Initialize variables.
76  */
77     void
78 tninit()
79 {
80     init_terminal();
81 
82     init_network();
83 
84     init_telnet();
85 
86     init_sys();
87 
88 #if defined(TN3270)
89     init_3270();
90 #endif
91 }
92 
93 	void
94 usage()
95 {
96 	fprintf(stderr, "Usage: %s %s%s%s%s\n",
97 	    prompt,
98 #ifdef	AUTHENTICATION
99 	    "[-8] [-E] [-K] [-L] [-N] [-S tos] [-X atype] [-a] [-c] [-d]",
100 	    "\n\t[-e char] [-k realm] [-l user] [-f/-F] [-n tracefile] ",
101 #else
102 	    "[-8] [-E] [-L] [-N] [-S tos] [-a] [-c] [-d] [-e char] [-l user]",
103 	    "\n\t[-n tracefile]",
104 #endif
105 #if defined(TN3270) && defined(unix)
106 # ifdef AUTHENTICATION
107 	    "[-noasynch] [-noasynctty]\n\t[-noasyncnet] [-r] [-t transcom] ",
108 # else
109 	    "[-noasynch] [-noasynctty] [-noasyncnet] [-r]\n\t[-t transcom]",
110 # endif
111 #else
112 	    "[-r] ",
113 #endif
114 #if defined(IPSEC) && defined(IPSEC_POLICY_IPSEC)
115 	    "[-P policy] [host-name [port]]"
116 #else
117 	    "[host-name [port]]"
118 #endif
119 	);
120 	exit(1);
121 }
122 
123 /*
124  * main.  Parse arguments, invoke the protocol or command parser.
125  */
126 
127 
128 int
129 main(argc, argv)
130 	int argc;
131 	char *argv[];
132 {
133 	extern char *optarg;
134 	extern int optind;
135 	int ch;
136 	char *user;
137 #ifdef	FORWARD
138 	extern int forward_flags;
139 #endif	/* FORWARD */
140 
141 	tninit();		/* Clear out things */
142 #if	defined(CRAY) && !defined(__STDC__)
143 	_setlist_init();	/* Work around compiler bug */
144 #endif
145 
146 	TerminalSaveState();
147 
148 	if ((prompt = strrchr(argv[0], '/')) != NULL)
149 		++prompt;
150 	else
151 		prompt = argv[0];
152 
153 	user = NULL;
154 
155 	rlogin = (strncmp(prompt, "rlog", 4) == 0) ? '~' : _POSIX_VDISABLE;
156 	autologin = -1;
157 
158 #if defined(IPSEC) && defined(IPSEC_POLICY_IPSEC)
159 #define IPSECOPT	"P:"
160 #else
161 #define IPSECOPT
162 #endif
163 	while ((ch = getopt(argc, argv, "8EKLNS:X:acde:fFk:l:n:rt:x"
164 			IPSECOPT)) != -1) {
165 #undef IPSECOPT
166 		switch(ch) {
167 		case '8':
168 			eight = 3;	/* binary output and input */
169 			break;
170 		case 'E':
171 			rlogin = escape = _POSIX_VDISABLE;
172 			break;
173 		case 'K':
174 #ifdef	AUTHENTICATION
175 			autologin = 0;
176 #endif
177 			break;
178 		case 'L':
179 			eight |= 2;	/* binary output only */
180 			break;
181 		case 'N':
182 			doaddrlookup = 0;
183 			break;
184 		case 'S':
185 		    {
186 #ifdef	HAS_GETTOS
187 			extern int tos;
188 
189 			if ((tos = parsetos(optarg, "tcp")) < 0)
190 				fprintf(stderr, "%s%s%s%s\n",
191 					prompt, ": Bad TOS argument '",
192 					optarg,
193 					"; will try to use default TOS");
194 #else
195 			fprintf(stderr,
196 			   "%s: Warning: -S ignored, no parsetos() support.\n",
197 								prompt);
198 #endif
199 		    }
200 			break;
201 		case 'X':
202 #ifdef	AUTHENTICATION
203 			auth_disable_name(optarg);
204 #endif
205 			break;
206 		case 'a':
207 			autologin = 1;
208 			break;
209 		case 'c':
210 			skiprc = 1;
211 			break;
212 		case 'd':
213 			debug = 1;
214 			break;
215 		case 'e':
216 			set_escape_char(optarg);
217 			break;
218 		case 'f':
219 #if defined(AUTHENTICATION) && defined(KRB5) && defined(FORWARD)
220 			if (forward_flags & OPTS_FORWARD_CREDS) {
221 			    fprintf(stderr,
222 				    "%s: Only one of -f and -F allowed.\n",
223 				    prompt);
224 			    usage();
225 			}
226 			forward_flags |= OPTS_FORWARD_CREDS;
227 #else
228 			fprintf(stderr,
229 			 "%s: Warning: -f ignored, no Kerberos V5 support.\n",
230 				prompt);
231 #endif
232 			break;
233 		case 'F':
234 #if defined(AUTHENTICATION) && defined(KRB5) && defined(FORWARD)
235 			if (forward_flags & OPTS_FORWARD_CREDS) {
236 			    fprintf(stderr,
237 				    "%s: Only one of -f and -F allowed.\n",
238 				    prompt);
239 			    usage();
240 			}
241 			forward_flags |= OPTS_FORWARD_CREDS;
242 			forward_flags |= OPTS_FORWARDABLE_CREDS;
243 #else
244 			fprintf(stderr,
245 			 "%s: Warning: -F ignored, no Kerberos V5 support.\n",
246 				prompt);
247 #endif
248 			break;
249 		case 'k':
250 #if defined(AUTHENTICATION) && defined(KRB4)
251 		    {
252 			extern char *dest_realm, dst_realm_buf[], dst_realm_sz;
253 			dest_realm = dst_realm_buf;
254 			(void)strncpy(dest_realm, optarg, dst_realm_sz);
255 		    }
256 #else
257 			fprintf(stderr,
258 			   "%s: Warning: -k ignored, no Kerberos V4 support.\n",
259 								prompt);
260 #endif
261 			break;
262 		case 'l':
263 			autologin = 1;
264 			user = optarg;
265 			break;
266 		case 'n':
267 #if defined(TN3270) && defined(unix)
268 			/* distinguish between "-n oasynch" and "-noasynch" */
269 			if (argv[optind - 1][0] == '-' && argv[optind - 1][1]
270 			    == 'n' && argv[optind - 1][2] == 'o') {
271 				if (!strcmp(optarg, "oasynch")) {
272 					noasynchtty = 1;
273 					noasynchnet = 1;
274 				} else if (!strcmp(optarg, "oasynchtty"))
275 					noasynchtty = 1;
276 				else if (!strcmp(optarg, "oasynchnet"))
277 					noasynchnet = 1;
278 			} else
279 #endif	/* defined(TN3270) && defined(unix) */
280 				SetNetTrace(optarg);
281 			break;
282 		case 'r':
283 			rlogin = '~';
284 			break;
285 		case 't':
286 #if defined(TN3270) && defined(unix)
287 			transcom = tline;
288 			(void)strcpy(transcom, optarg);
289 #else
290 			fprintf(stderr,
291 			   "%s: Warning: -t ignored, no TN3270 support.\n",
292 								prompt);
293 #endif
294 			break;
295 		case 'x':
296 			fprintf(stderr,
297 			    "%s: Warning: -x ignored, no ENCRYPT support.\n",
298 								prompt);
299 			break;
300 #if defined(IPSEC) && defined(IPSEC_POLICY_IPSEC)
301 		case 'P':
302 			if (!strncmp("in", optarg, 2))
303 				ipsec_policy_in = strdup(optarg);
304 			else if (!strncmp("out", optarg, 3))
305 				ipsec_policy_out = strdup(optarg);
306 			else
307 				usage();
308 			break;
309 #endif
310 		case '?':
311 		default:
312 			usage();
313 			/* NOTREACHED */
314 		}
315 	}
316 	if (autologin == -1)
317 		autologin = (rlogin == _POSIX_VDISABLE) ? 0 : 1;
318 
319 	argc -= optind;
320 	argv += optind;
321 
322 	if (argc) {
323 		char *args[7], **argp = args;
324 #ifdef __GNUC__
325 		(void) &argp;	/* avoid longjmp clobbering */
326 #endif
327 
328 		if (argc > 2)
329 			usage();
330 		*argp++ = prompt;
331 		if (user) {
332 			*argp++ = "-l";
333 			*argp++ = user;
334 		}
335 		*argp++ = argv[0];		/* host */
336 		if (argc > 1)
337 			*argp++ = argv[1];	/* port */
338 		*argp = 0;
339 
340 		if (setjmp(toplevel) != 0)
341 			Exit(0);
342 		if (tn(argp - args, args) == 1)
343 			return (0);
344 		else
345 			return (1);
346 	}
347 	(void)setjmp(toplevel);
348 	for (;;) {
349 #ifdef TN3270
350 		if (shell_active)
351 			shell_continue();
352 		else
353 #endif
354 			command(1, 0, 0);
355 	}
356 }
357