xref: /haiku/src/bin/network/telnet/main.c (revision b671e9bbdbd10268a042b4f4cc4317ccd03d105e)
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 
34 #if 0
35 #ifndef lint
36 static const char sccsid[] = "@(#)main.c	8.3 (Berkeley) 5/30/95";
37 #endif
38 #endif
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD: src/contrib/telnet/telnet/main.c,v 1.20 2005/01/09 10:24:45 maxim Exp $");
41 
42 #include <sys/param.h>
43 #include <sys/socket.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <unistd.h>
47 
48 #include "ring.h"
49 #include "externs.h"
50 #include "defines.h"
51 
52 #ifdef	AUTHENTICATION
53 #include <libtelnet/auth.h>
54 #endif
55 #ifdef	ENCRYPTION
56 #include <libtelnet/encrypt.h>
57 #endif
58 
59 /* These values need to be the same as defined in libtelnet/kerberos5.c */
60 /* Either define them in both places, or put in some common header file. */
61 #define OPTS_FORWARD_CREDS	0x00000002
62 #define OPTS_FORWARDABLE_CREDS	0x00000001
63 
64 #if defined(IPSEC) && defined(IPSEC_POLICY_IPSEC)
65 char *ipsec_policy_in = NULL;
66 char *ipsec_policy_out = NULL;
67 #endif
68 
69 extern int tos;
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 static void
89 usage(void)
90 {
91 	fprintf(stderr, "usage: %s %s%s%s%s\n",
92 	    prompt,
93 #ifdef	AUTHENTICATION
94 	    "[-4] [-6] [-8] [-E] [-K] [-L] [-N] [-S tos] [-X atype] [-c] [-d]",
95 	    "\n\t[-e char] [-k realm] [-l user] [-f/-F] [-n tracefile] ",
96 #else
97 	    "[-4] [-6] [-8] [-E] [-L] [-N] [-S tos] [-c] [-d]",
98 	    "\n\t[-e char] [-l user] [-n tracefile] ",
99 #endif
100 	    "[-r] [-s src_addr] [-u] ",
101 #if defined(IPSEC) && defined(IPSEC_POLICY_IPSEC)
102 	    "[-P policy] "
103 #endif
104 #ifdef	ENCRYPTION
105 	    "[-y] [host-name [port]]"
106 #else	/* ENCRYPTION */
107 	    "[host-name [port]]"
108 #endif	/* ENCRYPTION */
109 	);
110 	exit(1);
111 }
112 
113 /*
114  * main.  Parse arguments, invoke the protocol or command parser.
115  */
116 
117 int
118 main(int argc, char *argv[])
119 {
120 	int ch;
121 #if (!defined(__BEOS__) && !defined(__HAIKU__))
122 	char *ep;
123 	u_long ultmp;
124 #endif
125 	char *user;
126 	char *src_addr = NULL;
127 #ifdef	FORWARD
128 	extern int forward_flags;
129 #endif	/* FORWARD */
130 
131 	tninit();		/* Clear out things */
132 
133 	TerminalSaveState();
134 
135 	if ((prompt = strrchr(argv[0], '/')))
136 		++prompt;
137 	else
138 		prompt = argv[0];
139 
140 	user = NULL;
141 
142 	rlogin = (strncmp(prompt, "rlog", 4) == 0) ? '~' : _POSIX_VDISABLE;
143 #ifdef AUTHENTICATION
144 	autologin = 1;
145 #else
146 	autologin = -1;
147 #endif
148 
149 #ifdef	ENCRYPTION
150 	encrypt_auto(1);
151 	decrypt_auto(1);
152 #endif
153 
154 #if defined(IPSEC) && defined(IPSEC_POLICY_IPSEC)
155 #define IPSECOPT	"P:"
156 #else
157 #define IPSECOPT
158 #endif
159 	while ((ch = getopt(argc, argv,
160 			    "468EKLNS:X:acde:fFk:l:n:rs:uxy" IPSECOPT)) != -1)
161 #undef IPSECOPT
162 	{
163 		switch(ch) {
164 		case '4':
165 			family = AF_INET;
166 			break;
167 #ifdef INET6
168 		case '6':
169 			family = AF_INET6;
170 			break;
171 #endif
172 		case '8':
173 			eight = 3;	/* binary output and input */
174 			break;
175 		case 'E':
176 			rlogin = escape = _POSIX_VDISABLE;
177 			break;
178 		case 'K':
179 #ifdef	AUTHENTICATION
180 			autologin = 0;
181 #endif
182 			break;
183 		case 'L':
184 			eight |= 2;	/* binary output only */
185 			break;
186 		case 'N':
187 			doaddrlookup = 0;
188 			break;
189 		case 'S':
190 #if (defined(__BEOS__) || defined(__HAIKU__))
191 			fprintf(stderr, "-S option is not supported\n");
192 #else
193 # ifdef	HAS_GETTOS
194 
195 			if ((tos = parsetos(optarg, "tcp")) < 0)
196 				fprintf(stderr, "%s%s%s%s\n",
197 					prompt, ": Bad TOS argument '",
198 					optarg,
199 					"; will try to use default TOS");
200 # else
201 # define	MAXTOS	255
202 			ultmp = strtoul(optarg, &ep, 0);
203 			if (*ep || ep == optarg || ultmp > MAXTOS)
204 				fprintf(stderr, "%s%s%s%s\n",
205 					prompt, ": Bad TOS argument '",
206 					optarg,
207 					"; will try to use default TOS");
208 			else
209 				tos = ultmp;
210 # endif
211 #endif	/* __BEOS__ */
212 			break;
213 		case 'X':
214 #ifdef	AUTHENTICATION
215 			auth_disable_name(optarg);
216 #endif
217 			break;
218 		case 'a':
219 #ifdef	AUTHENTICATION
220 			/* It's the default now, so ignore */
221 #else
222 			autologin = 1;
223 #endif
224 			break;
225 		case 'c':
226 			skiprc = 1;
227 			break;
228 		case 'd':
229 			telnet_debug = 1;
230 			break;
231 		case 'e':
232 			set_escape_char(optarg);
233 			break;
234 		case 'f':
235 #ifdef	AUTHENTICATION
236 #if defined(KRB5) && defined(FORWARD)
237 			if (forward_flags & OPTS_FORWARD_CREDS) {
238 			    fprintf(stderr,
239 				    "%s: Only one of -f and -F allowed.\n",
240 				    prompt);
241 			    usage();
242 			}
243 			forward_flags |= OPTS_FORWARD_CREDS;
244 #else
245 			fprintf(stderr,
246 			 "%s: Warning: -f ignored, no Kerberos V5 support.\n",
247 				prompt);
248 #endif
249 #else
250 			fprintf(stderr,
251 			 "%s: Warning: -f ignored, no Kerberos V5 support.\n",
252 				prompt);
253 #endif
254 			break;
255 		case 'F':
256 #ifdef	AUTHENTICATION
257 #if defined(KRB5) && defined(FORWARD)
258 			if (forward_flags & OPTS_FORWARD_CREDS) {
259 			    fprintf(stderr,
260 				    "%s: Only one of -f and -F allowed.\n",
261 				    prompt);
262 			    usage();
263 			}
264 			forward_flags |= OPTS_FORWARD_CREDS;
265 			forward_flags |= OPTS_FORWARDABLE_CREDS;
266 #else
267 			fprintf(stderr,
268 			 "%s: Warning: -F ignored, no Kerberos V5 support.\n",
269 				prompt);
270 #endif
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 #ifdef	AUTHENTICATION
279 #if defined(KRB4)
280 		    {
281 			extern char *dest_realm, dst_realm_buf[], dst_realm_sz;
282 			dest_realm = dst_realm_buf;
283 			(void)strncpy(dest_realm, optarg, dst_realm_sz);
284 		    }
285 #else
286 			fprintf(stderr,
287 			   "%s: Warning: -k ignored, no Kerberos V4 support.\n",
288 								prompt);
289 #endif
290 #else
291 			fprintf(stderr,
292 			   "%s: Warning: -k ignored, no Kerberos V4 support.\n",
293 								prompt);
294 #endif
295 			break;
296 		case 'l':
297 #ifdef	AUTHENTICATION
298 			/* This is the default now, so ignore it */
299 #else
300 			autologin = 1;
301 #endif
302 			user = optarg;
303 			break;
304 		case 'n':
305 				SetNetTrace(optarg);
306 			break;
307 		case 'r':
308 			rlogin = '~';
309 			break;
310 		case 's':
311 			src_addr = optarg;
312 			break;
313 		case 'u':
314 			family = AF_UNIX;
315 			break;
316 		case 'x':
317 #ifndef	ENCRYPTION
318 			fprintf(stderr,
319 			    "%s: Warning: -x ignored, no ENCRYPT support.\n",
320 								prompt);
321 #endif	/* ENCRYPTION */
322 			break;
323 		case 'y':
324 #ifdef	ENCRYPTION
325 			encrypt_auto(0);
326 			decrypt_auto(0);
327 #else
328 			fprintf(stderr,
329 			    "%s: Warning: -y ignored, no ENCRYPT support.\n",
330 								prompt);
331 #endif	/* ENCRYPTION */
332 			break;
333 #if defined(IPSEC) && defined(IPSEC_POLICY_IPSEC)
334 		case 'P':
335 			if (!strncmp("in", optarg, 2))
336 				ipsec_policy_in = strdup(optarg);
337 			else if (!strncmp("out", optarg, 3))
338 				ipsec_policy_out = strdup(optarg);
339 			else
340 				usage();
341 			break;
342 #endif
343 		case '?':
344 		default:
345 			usage();
346 			/* NOTREACHED */
347 		}
348 	}
349 	if (autologin == -1)
350 		autologin = (rlogin == _POSIX_VDISABLE) ? 0 : 1;
351 
352 	argc -= optind;
353 	argv += optind;
354 
355 	if (argc) {
356 		char *args[9], **argp = args;
357 
358 		if (argc > 2)
359 			usage();
360 		*argp++ = prompt;
361 		if (user) {
362 			*argp++ = strdup("-l");
363 			*argp++ = user;
364 		}
365 		if (src_addr) {
366 			*argp++ = strdup("-s");
367 			*argp++ = src_addr;
368 		}
369 		*argp++ = argv[0];		/* host */
370 		if (argc > 1)
371 			*argp++ = argv[1];	/* port */
372 		*argp = 0;
373 
374 		if (setjmp(toplevel) != 0)
375 			Exit(0);
376 		if (tn(argp - args, args) == 1)
377 			return (0);
378 		else
379 			return (1);
380 	}
381 	(void)setjmp(toplevel);
382 	for (;;) {
383 			command(1, 0, 0);
384 	}
385 	return 0;
386 }
387