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