xref: /freebsd/contrib/telnet/telnet/main.c (revision 5521ff5a4d1929056e7ffc982fac3341ca54df7c)
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]",
112 	    "\n\t[-e char] [-l user] [-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] [-u] ",
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 '4':
188 			family = AF_INET;
189 			break;
190 #ifdef INET6
191 		case '6':
192 			family = AF_INET6;
193 			break;
194 #endif
195 		case '8':
196 			eight = 3;	/* binary output and input */
197 			break;
198 		case 'E':
199 			rlogin = escape = _POSIX_VDISABLE;
200 			break;
201 		case 'K':
202 #ifdef	AUTHENTICATION
203 			autologin = 0;
204 #endif
205 			break;
206 		case 'L':
207 			eight |= 2;	/* binary output only */
208 			break;
209 		case 'N':
210 			doaddrlookup = 0;
211 			break;
212 		case 'S':
213 		    {
214 #ifdef	HAS_GETTOS
215 			extern int tos;
216 
217 			if ((tos = parsetos(optarg, "tcp")) < 0)
218 				fprintf(stderr, "%s%s%s%s\n",
219 					prompt, ": Bad TOS argument '",
220 					optarg,
221 					"; will try to use default TOS");
222 #else
223 			fprintf(stderr,
224 			   "%s: Warning: -S ignored, no parsetos() support.\n",
225 								prompt);
226 #endif
227 		    }
228 			break;
229 		case 'X':
230 #ifdef	AUTHENTICATION
231 			auth_disable_name(optarg);
232 #endif
233 			break;
234 		case 'a':
235 			/* It's the default now, so ignore */
236 			break;
237 		case 'c':
238 			skiprc = 1;
239 			break;
240 		case 'd':
241 			debug = 1;
242 			break;
243 		case 'e':
244 			set_escape_char(optarg);
245 			break;
246 		case 'f':
247 #if defined(AUTHENTICATION) && defined(KRB5) && defined(FORWARD)
248 			if (forward_flags & OPTS_FORWARD_CREDS) {
249 			    fprintf(stderr,
250 				    "%s: Only one of -f and -F allowed.\n",
251 				    prompt);
252 			    usage();
253 			}
254 			forward_flags |= OPTS_FORWARD_CREDS;
255 #else
256 			fprintf(stderr,
257 			 "%s: Warning: -f ignored, no Kerberos V5 support.\n",
258 				prompt);
259 #endif
260 			break;
261 		case 'F':
262 #if defined(AUTHENTICATION) && defined(KRB5) && defined(FORWARD)
263 			if (forward_flags & OPTS_FORWARD_CREDS) {
264 			    fprintf(stderr,
265 				    "%s: Only one of -f and -F allowed.\n",
266 				    prompt);
267 			    usage();
268 			}
269 			forward_flags |= OPTS_FORWARD_CREDS;
270 			forward_flags |= OPTS_FORWARDABLE_CREDS;
271 #else
272 			fprintf(stderr,
273 			 "%s: Warning: -F ignored, no Kerberos V5 support.\n",
274 				prompt);
275 #endif
276 			break;
277 		case 'k':
278 #if defined(AUTHENTICATION) && defined(KRB4)
279 		    {
280 			extern char *dest_realm, dst_realm_buf[], dst_realm_sz;
281 			dest_realm = dst_realm_buf;
282 			(void)strncpy(dest_realm, optarg, dst_realm_sz);
283 		    }
284 #else
285 			fprintf(stderr,
286 			   "%s: Warning: -k ignored, no Kerberos V4 support.\n",
287 								prompt);
288 #endif
289 			break;
290 		case 'l':
291 			user = optarg;
292 			break;
293 		case 'n':
294 #if defined(TN3270) && defined(unix)
295 			/* distinguish between "-n oasynch" and "-noasynch" */
296 			if (argv[optind - 1][0] == '-' && argv[optind - 1][1]
297 			    == 'n' && argv[optind - 1][2] == 'o') {
298 				if (!strcmp(optarg, "oasynch")) {
299 					noasynchtty = 1;
300 					noasynchnet = 1;
301 				} else if (!strcmp(optarg, "oasynchtty"))
302 					noasynchtty = 1;
303 				else if (!strcmp(optarg, "oasynchnet"))
304 					noasynchnet = 1;
305 			} else
306 #endif	/* defined(TN3270) && defined(unix) */
307 				SetNetTrace(optarg);
308 			break;
309 		case 'r':
310 			rlogin = '~';
311 			break;
312 		case 's':
313 			src_addr = optarg;
314 			break;
315 		case 't':
316 #if defined(TN3270) && defined(unix)
317 			transcom = tline;
318 			(void)strcpy(transcom, optarg);
319 #else
320 			fprintf(stderr,
321 			   "%s: Warning: -t ignored, no TN3270 support.\n",
322 								prompt);
323 #endif
324 			break;
325 		case 'u':
326 			family = AF_UNIX;
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