xref: /netbsd-src/usr.bin/telnet/main.c (revision 181254a7b1bdde6873432bffef2d2decc4b5c22f)
1 /*	$NetBSD: main.c,v 1.32 2018/12/14 23:40:17 christos 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. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 #ifndef lint
34 __COPYRIGHT("@(#) Copyright (c) 1988, 1990, 1993\
35  The Regents of the University of California.  All rights reserved.");
36 #endif /* not lint */
37 
38 #ifndef lint
39 #if 0
40 static char sccsid[] = "@(#)main.c	8.3 (Berkeley) 5/30/95";
41 #else
42 __RCSID("$NetBSD: main.c,v 1.32 2018/12/14 23:40:17 christos Exp $");
43 #endif
44 #endif /* not lint */
45 
46 #include <sys/types.h>
47 #include <sys/socket.h>
48 
49 #include <unistd.h>
50 
51 #include "ring.h"
52 #include "externs.h"
53 #include "defines.h"
54 #ifdef AUTHENTICATION
55 #include <libtelnet/auth.h>
56 #endif
57 #ifdef 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 defined(IPSEC) && defined(IPSEC_POLICY_IPSEC)
67 char *ipsec_policy_in = NULL;
68 char *ipsec_policy_out = NULL;
69 #endif
70 
71 int family = AF_UNSPEC;
72 
73 /*
74  * Initialize variables.
75  */
76 void
77 tninit(void)
78 {
79     init_terminal();
80 
81     init_network();
82 
83     init_telnet();
84 
85     init_sys();
86 
87 }
88 
89 	void
90 usage(void)
91 {
92 	fprintf(stderr, "usage: %s %s%s%s%s\n",
93 	    prompt,
94 #ifdef	AUTHENTICATION
95 	    "[-4] [-6] [-8] [-E] [-K] [-L] [-N] [-S tos] [-X atype] [-a] [-c]",
96 	    "\n\t[-d] [-e char] [-k realm] [-l user] [-f/-F] [-n tracefile] ",
97 #else
98 	    "[-4] [-6] [-8] [-E] [-L] [-N] [-S tos] [-a] [-c] [-d] [-e char]",
99 	    "\n\t[-l user] [-n tracefile] ",
100 #endif
101 	    "[-r] ",
102 #ifdef	ENCRYPTION
103 	    "[-x] "
104 #endif
105 #if defined(IPSEC) && defined(IPSEC_POLICY_IPSEC)
106 	    "\n\t[-P policy] [host-name [port]]"
107 #else
108 	    "\n\t[host-name [port]]"
109 #endif
110 	);
111 	exit(1);
112 }
113 
114 /*
115  * main.  Parse arguments, invoke the protocol or command parser.
116  */
117 
118 
119 int
120 main(int argc, char *argv[])
121 {
122 	extern char *optarg;
123 	extern int optind;
124 	int ch;
125 	char *user;
126 #ifdef	FORWARD
127 	extern int forward_flags;
128 #endif	/* FORWARD */
129 
130 	tninit();		/* Clear out things */
131 
132 	TerminalSaveState();
133 
134 	if ((prompt = strrchr(argv[0], '/')) != NULL)
135 		++prompt;
136 	else
137 		prompt = argv[0];
138 
139 	user = NULL;
140 
141 	rlogin = (strncmp(prompt, "rlog", 4) == 0) ? '~' : _POSIX_VDISABLE;
142 	autologin = -1;
143 
144 #if defined(IPSEC) && defined(IPSEC_POLICY_IPSEC)
145 #define IPSECOPT	"P:"
146 #else
147 #define IPSECOPT
148 #endif
149 	while ((ch = getopt(argc, argv, "468EKLNS:X:acde:fFk:l:n:rt:x"
150 			IPSECOPT)) != -1) {
151 #undef IPSECOPT
152 		switch(ch) {
153 		case '4':
154 			family = AF_INET;
155 			break;
156 		case '6':
157 			family = AF_INET6;
158 			break;
159 		case '8':
160 			eight = 3;	/* binary output and input */
161 			break;
162 		case 'E':
163 			rlogin = escape = _POSIX_VDISABLE;
164 			break;
165 		case 'K':
166 #ifdef	AUTHENTICATION
167 			autologin = 0;
168 #endif
169 			break;
170 		case 'L':
171 			eight |= 2;	/* binary output only */
172 			break;
173 		case 'N':
174 			doaddrlookup = 0;
175 			break;
176 		case 'S':
177 		    {
178 			fprintf(stderr,
179 			   "%s: Warning: -S ignored, no parsetos() support.\n",
180 								prompt);
181 		    }
182 			break;
183 		case 'X':
184 #ifdef	AUTHENTICATION
185 			auth_disable_name(optarg);
186 #endif
187 			break;
188 		case 'a':
189 			autologin = 1;
190 			break;
191 		case 'c':
192 			skiprc = 1;
193 			break;
194 		case 'd':
195 			telnet_debug = 1;
196 			break;
197 		case 'e':
198 			set_escape_char(optarg);
199 			break;
200 		case 'f':
201 #if defined(AUTHENTICATION) && defined(KRB5) && defined(FORWARD)
202 			if (forward_flags & OPTS_FORWARD_CREDS) {
203 			    fprintf(stderr,
204 				    "%s: Only one of -f and -F allowed.\n",
205 				    prompt);
206 			    usage();
207 			}
208 			forward_flags |= OPTS_FORWARD_CREDS;
209 #else
210 			fprintf(stderr,
211 			 "%s: Warning: -f ignored, no Kerberos V5 support.\n",
212 				prompt);
213 #endif
214 			break;
215 		case 'F':
216 #if defined(AUTHENTICATION) && defined(KRB5) && defined(FORWARD)
217 			if (forward_flags & OPTS_FORWARD_CREDS) {
218 			    fprintf(stderr,
219 				    "%s: Only one of -f and -F allowed.\n",
220 				    prompt);
221 			    usage();
222 			}
223 			forward_flags |= OPTS_FORWARD_CREDS;
224 			forward_flags |= OPTS_FORWARDABLE_CREDS;
225 #else
226 			fprintf(stderr,
227 			 "%s: Warning: -F ignored, no Kerberos V5 support.\n",
228 				prompt);
229 #endif
230 			break;
231 		case 'k':
232 			fprintf(stderr,
233 			   "%s: Warning: -k ignored, no Kerberos V4 support.\n",
234 								prompt);
235 			break;
236 		case 'l':
237 			if(autologin == 0) {
238 				autologin = -1;
239 			}
240 			user = optarg;
241 			break;
242 		case 'n':
243 				SetNetTrace(optarg);
244 			break;
245 		case 'r':
246 			rlogin = '~';
247 			break;
248 		case 't':
249 			fprintf(stderr,
250 			   "%s: Warning: -t ignored, no TN3270 support.\n",
251 								prompt);
252 			break;
253 		case 'x':
254 #ifdef	ENCRYPTION
255 			encrypt_auto(1);
256 			decrypt_auto(1);
257 #else	/* ENCRYPTION */
258 			fprintf(stderr,
259 			    "%s: Warning: -x ignored, no ENCRYPT support.\n",
260 								prompt);
261 #endif	/* ENCRYPTION */
262 			break;
263 #if defined(IPSEC) && defined(IPSEC_POLICY_IPSEC)
264 		case 'P':
265 			if (!strncmp("in", optarg, 2))
266 				ipsec_policy_in = strdup(optarg);
267 			else if (!strncmp("out", optarg, 3))
268 				ipsec_policy_out = strdup(optarg);
269 			else
270 				usage();
271 			break;
272 #endif
273 		case '?':
274 		default:
275 			usage();
276 			/* NOTREACHED */
277 		}
278 	}
279 
280 	if (autologin == -1) {		/* esc@magic.fi; force  */
281 #if defined(AUTHENTICATION)
282 		autologin = 1;
283 #endif
284 #if defined(ENCRYPTION)
285 		encrypt_auto(1);
286 		decrypt_auto(1);
287 #endif
288 	}
289 
290 	if (autologin == -1)
291 		autologin = (rlogin == _POSIX_VDISABLE) ? 0 : 1;
292 
293 	argc -= optind;
294 	argv += optind;
295 
296 	if (argc) {
297 		static char ml[] = "-l";
298 		char *args[7];
299 		char ** volatile argp;	/* avoid longjmp clobbering */
300 
301 		argp = args;
302 		if (argc > 2)
303 			usage();
304 		*argp++ = prompt;
305 		if (user) {
306 			*argp++ = ml;
307 			*argp++ = user;
308 		}
309 		*argp++ = argv[0];		/* host */
310 		if (argc > 1)
311 			*argp++ = argv[1];	/* port */
312 		*argp = 0;
313 
314 		if (setjmp(toplevel) != 0)
315 			Exit(0);
316 		if (tn((int)(argp - args), args) == 1)
317 			return (0);
318 		else
319 			return (1);
320 	}
321 	(void)setjmp(toplevel);
322 	for (;;) {
323 			command(1, 0, 0);
324 	}
325 }
326