xref: /freebsd/crypto/heimdal/appl/telnet/telnet/main.c (revision 23f282aa31e9b6fceacd449020e936e98d6f2298)
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 static char *copyright[] = {
35     "@(#) Copyright (c) 1988, 1990, 1993\n"
36     "\tThe Regents of the University of California.  All rights reserved.\n",
37     (char*)copyright
38 };
39 
40 #include "telnet_locl.h"
41 RCSID("$Id: main.c,v 1.30 1999/11/13 06:30:11 assar Exp $");
42 
43 /* These values need to be the same as defined in libtelnet/kerberos5.c */
44 /* Either define them in both places, or put in some common header file. */
45 #define OPTS_FORWARD_CREDS	0x00000002
46 #define OPTS_FORWARDABLE_CREDS	0x00000001
47 
48 #if KRB5
49 #define FORWARD
50 #endif
51 
52 /*
53  * Initialize variables.
54  */
55 void
56 tninit(void)
57 {
58     init_terminal();
59 
60     init_network();
61 
62     init_telnet();
63 
64     init_sys();
65 }
66 
67 void
68 usage(void)
69 {
70   fprintf(stderr, "Usage: %s %s%s%s%s\n", prompt,
71 #ifdef	AUTHENTICATION
72 	  "[-8] [-E] [-K] [-L] [-G] [-S tos] [-X atype] [-a] [-c] [-d] [-e char]",
73 	  "\n\t[-k realm] [-l user] [-f/-F] [-n tracefile] ",
74 #else
75 	  "[-8] [-E] [-L] [-S tos] [-a] [-c] [-d] [-e char] [-l user]",
76 	  "\n\t[-n tracefile]",
77 #endif
78 	  "[-r] ",
79 #ifdef	ENCRYPTION
80 	  "[-x] [host-name [port]]"
81 #else
82 	  "[host-name [port]]"
83 #endif
84     );
85   exit(1);
86 }
87 
88 /*
89  * main.  Parse arguments, invoke the protocol or command parser.
90  */
91 
92 
93 #ifdef	FORWARD
94 extern int forward_flags;
95 static int default_forward=0;
96 #endif	/* FORWARD */
97 
98 #ifdef KRB5
99 /* XXX ugly hack to setup dns-proxy stuff */
100 #define Authenticator asn1_Authenticator
101 #include <krb5.h>
102 static void
103 krb5_init(void)
104 {
105     krb5_context context;
106     krb5_init_context(&context);
107 
108 #if defined(AUTHENTICATION) && defined(KRB5) && defined(FORWARD)
109     if (krb5_config_get_bool (context, NULL,
110          "libdefaults", "forward", NULL)) {
111            forward_flags |= OPTS_FORWARD_CREDS;
112            default_forward=1;
113     }
114     if (krb5_config_get_bool (context, NULL,
115          "libdefaults", "forwardable", NULL)) {
116            forward_flags |= OPTS_FORWARDABLE_CREDS;
117            default_forward=1;
118     }
119 #endif
120 #ifdef  ENCRYPTION
121     if (krb5_config_get_bool (context, NULL,
122         "libdefaults", "encrypt", NULL)) {
123           encrypt_auto(1);
124           decrypt_auto(1);
125           EncryptVerbose(1);
126         }
127 #endif
128 
129     krb5_free_context(context);
130 }
131 #endif
132 
133 int
134 main(int argc, char **argv)
135 {
136 	int ch;
137 	char *user;
138 
139 #ifdef KRB5
140 	krb5_init();
141 #endif
142 
143 	tninit();		/* Clear out things */
144 
145 	TerminalSaveState();
146 
147 	if ((prompt = strrchr(argv[0], '/')))
148 		++prompt;
149 	else
150 		prompt = argv[0];
151 
152 	user = NULL;
153 
154 	rlogin = (strncmp(prompt, "rlog", 4) == 0) ? '~' : _POSIX_VDISABLE;
155 
156 	/*
157 	 * if AUTHENTICATION and ENCRYPTION is set autologin will be
158 	 * se to true after the getopt switch; unless the -K option is
159 	 * passed
160 	 */
161 	autologin = -1;
162 
163 	while((ch = getopt(argc, argv,
164 			   "78DEKLS:X:abcde:fFk:l:n:rxG")) != -1) {
165 		switch(ch) {
166 		case '8':
167 			eight = 3;	/* binary output and input */
168 			break;
169 		case '7':
170 			eight = 0;
171 			break;
172 		case 'b':
173 		    binary = 3;
174 		    break;
175 		case 'D': {
176 		    /* sometimes we don't want a mangled display */
177 		    char *p;
178 		    if((p = getenv("DISPLAY")))
179 			env_define("DISPLAY", (unsigned char*)p);
180 		    break;
181 		}
182 		case 'E':
183 			rlogin = escape = _POSIX_VDISABLE;
184 			break;
185 		case 'K':
186 #ifdef	AUTHENTICATION
187 			autologin = 0;
188 #endif
189 			break;
190 		case 'L':
191 			eight |= 2;	/* binary output only */
192 			break;
193 		case 'S':
194 		    {
195 #ifdef	HAVE_PARSETOS
196 			extern int tos;
197 
198 			if ((tos = parsetos(optarg, "tcp")) < 0)
199 				fprintf(stderr, "%s%s%s%s\n",
200 					prompt, ": Bad TOS argument '",
201 					optarg,
202 					"; will try to use default TOS");
203 #else
204 			fprintf(stderr,
205 			   "%s: Warning: -S ignored, no parsetos() support.\n",
206 								prompt);
207 #endif
208 		    }
209 			break;
210 		case 'X':
211 #ifdef	AUTHENTICATION
212 			auth_disable_name(optarg);
213 #endif
214 			break;
215 		case 'a':
216 			autologin = 1;
217 			break;
218 		case 'c':
219 			skiprc = 1;
220 			break;
221 		case 'd':
222 			debug = 1;
223 			break;
224 		case 'e':
225 			set_escape_char(optarg);
226 			break;
227 		case 'f':
228 #if defined(AUTHENTICATION) && defined(KRB5) && defined(FORWARD)
229 			if ((forward_flags & OPTS_FORWARD_CREDS) &&
230 			    !default_forward) {
231 			    fprintf(stderr,
232 				    "%s: Only one of -f and -F allowed.\n",
233 				    prompt);
234 			    usage();
235 			}
236 			forward_flags |= OPTS_FORWARD_CREDS;
237 #else
238 			fprintf(stderr,
239 			 "%s: Warning: -f ignored, no Kerberos V5 support.\n",
240 				prompt);
241 #endif
242 			break;
243 		case 'F':
244 #if defined(AUTHENTICATION) && defined(KRB5) && defined(FORWARD)
245 			if ((forward_flags & OPTS_FORWARD_CREDS) &&
246 			    !default_forward) {
247 			    fprintf(stderr,
248 				    "%s: Only one of -f and -F allowed.\n",
249 				    prompt);
250 			    usage();
251 			}
252 			forward_flags |= OPTS_FORWARD_CREDS;
253 			forward_flags |= OPTS_FORWARDABLE_CREDS;
254 #else
255 			fprintf(stderr,
256 			 "%s: Warning: -F ignored, no Kerberos V5 support.\n",
257 				prompt);
258 #endif
259 			break;
260 		case 'k':
261 #if defined(AUTHENTICATION) && defined(KRB4)
262 		    {
263 			extern char *dest_realm, dst_realm_buf[];
264 			extern int dst_realm_sz;
265 			dest_realm = dst_realm_buf;
266 			strlcpy(dest_realm, optarg, dst_realm_sz);
267 		    }
268 #else
269 			fprintf(stderr,
270 			   "%s: Warning: -k ignored, no Kerberos V4 support.\n",
271 								prompt);
272 #endif
273 			break;
274 		case 'l':
275 		  if(autologin == 0){
276 		    fprintf(stderr, "%s: Warning: -K ignored\n", prompt);
277 		    autologin = -1;
278 		  }
279 			user = optarg;
280 			break;
281 		case 'n':
282 				SetNetTrace(optarg);
283 			break;
284 		case 'r':
285 			rlogin = '~';
286 			break;
287 		case 'x':
288 #ifdef	ENCRYPTION
289 			encrypt_auto(1);
290 			decrypt_auto(1);
291 			EncryptVerbose(1);
292 #else
293 			fprintf(stderr,
294 			    "%s: Warning: -x ignored, no ENCRYPT support.\n",
295 								prompt);
296 #endif
297 			break;
298 		case 'G':
299 #if defined(AUTHENTICATION) && defined(KRB5) && defined(FORWARD)
300                         forward_flags ^= OPTS_FORWARD_CREDS;
301                         forward_flags ^= OPTS_FORWARDABLE_CREDS;
302 #else
303                         fprintf(stderr,
304                          "%s: Warning: -G ignored, no Kerberos V5 support.\n",
305                                 prompt);
306 #endif
307                         break;
308 
309 		case '?':
310 		default:
311 			usage();
312 			/* NOTREACHED */
313 		}
314 	}
315 
316 	if (autologin == -1) {		/* esc@magic.fi; force  */
317 #if defined(AUTHENTICATION)
318 		autologin = 1;
319 #endif
320 #if defined(ENCRYPTION)
321 		encrypt_auto(1);
322 		decrypt_auto(1);
323 #endif
324 	}
325 
326 	if (autologin == -1)
327 		autologin = (rlogin == _POSIX_VDISABLE) ? 0 : 1;
328 
329 	argc -= optind;
330 	argv += optind;
331 
332 	if (argc) {
333 		char *args[7], **argp = args;
334 
335 		if (argc > 2)
336 			usage();
337 		*argp++ = prompt;
338 		if (user) {
339 			*argp++ = "-l";
340 			*argp++ = user;
341 		}
342 		*argp++ = argv[0];		/* host */
343 		if (argc > 1)
344 			*argp++ = argv[1];	/* port */
345 		*argp = 0;
346 
347 		if (setjmp(toplevel) != 0)
348 			Exit(0);
349 		if (tn(argp - args, args) == 1)
350 			return (0);
351 		else
352 			return (1);
353 	}
354 	setjmp(toplevel);
355 	for (;;) {
356 			command(1, 0, 0);
357 	}
358 }
359