xref: /freebsd/contrib/telnet/telnet/main.c (revision 4cf49a43559ed9fdad601bdcccd2c55963008675)
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 #ifndef lint
35 static const char copyright[] =
36 "@(#) Copyright (c) 1988, 1990, 1993\n\
37 	The Regents of the University of California.  All rights reserved.\n";
38 #endif /* not lint */
39 
40 #ifndef lint
41 static const char sccsid[] = "@(#)main.c	8.3 (Berkeley) 5/30/95";
42 #endif /* not lint */
43 
44 #include <sys/types.h>
45 #include <stdlib.h>
46 
47 #include "ring.h"
48 #include "externs.h"
49 #include "defines.h"
50 
51 #if	defined(AUTHENTICATION)
52 #include <libtelnet/auth.h>
53 #endif
54 #if	defined(ENCRYPTION)
55 #include <libtelnet/encrypt.h>
56 #endif
57 
58 /* These values need to be the same as defined in libtelnet/kerberos5.c */
59 /* Either define them in both places, or put in some common header file. */
60 #define OPTS_FORWARD_CREDS	0x00000002
61 #define OPTS_FORWARDABLE_CREDS	0x00000001
62 
63 #if 0
64 #define FORWARD
65 #endif
66 
67 void init_terminal(void);
68 void init_network(void);
69 void init_telnet(void);
70 void init_sys(void);
71 void init_3270(void);
72 
73 /*
74  * Initialize variables.
75  */
76     void
77 tninit()
78 {
79     init_terminal();
80 
81     init_network();
82 
83     init_telnet();
84 
85     init_sys();
86 
87 #if defined(TN3270)
88     init_3270();
89 #endif
90 }
91 
92 	void
93 usage()
94 {
95 	fprintf(stderr, "Usage: %s %s%s%s%s\n",
96 	    prompt,
97 #ifdef	AUTHENTICATION
98 	    "[-8] [-E] [-K] [-L] [-N] [-S tos] [-X atype] [-a] [-c] [-d]",
99 	    "\n\t[-e char] [-k realm] [-l user] [-f/-F] [-n tracefile] ",
100 #else
101 	    "[-8] [-E] [-L] [-N] [-S tos] [-a] [-c] [-d] [-e char] [-l user]",
102 	    "\n\t[-n tracefile] ",
103 #endif
104 #if defined(TN3270) && defined(unix)
105 # ifdef AUTHENTICATION
106 	    "[-noasynch] [-noasynctty]\n\t"
107 	    "[-noasyncnet] [-r] [-s src_addr] [-t transcom] ",
108 # else
109 	    "[-noasynch] [-noasynctty] [-noasyncnet] [-r]\n\t"
110 	    "[-s src_addr] [-t transcom]",
111 # endif
112 #else
113 	    "[-r] [-s src_addr] ",
114 #endif
115 #ifdef	ENCRYPTION
116 	    "[-x] [host-name [port]]"
117 #else	/* ENCRYPTION */
118 	    "[host-name [port]]"
119 #endif	/* ENCRYPTION */
120 	);
121 	exit(1);
122 }
123 
124 /*
125  * main.  Parse arguments, invoke the protocol or command parser.
126  */
127 
128 	int
129 main(argc, argv)
130 	int argc;
131 	char *argv[];
132 {
133 	extern char *optarg;
134 	extern int optind;
135 	int ch;
136 	char *user, *strrchr();
137 	char *src_addr = NULL;
138 #ifdef	FORWARD
139 	extern int forward_flags;
140 #endif	/* FORWARD */
141 
142 	tninit();		/* Clear out things */
143 #if	defined(CRAY) && !defined(__STDC__)
144 	_setlist_init();	/* Work around compiler bug */
145 #endif
146 
147 	TerminalSaveState();
148 
149 	if ((prompt = strrchr(argv[0], '/')))
150 		++prompt;
151 	else
152 		prompt = argv[0];
153 
154 	user = NULL;
155 
156 	rlogin = (strncmp(prompt, "rlog", 4) == 0) ? '~' : _POSIX_VDISABLE;
157 	autologin = -1;
158 
159 	while ((ch = getopt(argc, argv, "8EKLNS:X:acde:fFk:l:n:rs:t:x")) != EOF) {
160 		switch(ch) {
161 		case '8':
162 			eight = 3;	/* binary output and input */
163 			break;
164 		case 'E':
165 			rlogin = escape = _POSIX_VDISABLE;
166 			break;
167 		case 'K':
168 #ifdef	AUTHENTICATION
169 			autologin = 0;
170 #endif
171 			break;
172 		case 'L':
173 			eight |= 2;	/* binary output only */
174 			break;
175 		case 'N':
176 			doaddrlookup = 0;
177 			break;
178 		case 'S':
179 		    {
180 #ifdef	HAS_GETTOS
181 			extern int tos;
182 
183 			if ((tos = parsetos(optarg, "tcp")) < 0)
184 				fprintf(stderr, "%s%s%s%s\n",
185 					prompt, ": Bad TOS argument '",
186 					optarg,
187 					"; will try to use default TOS");
188 #else
189 			fprintf(stderr,
190 			   "%s: Warning: -S ignored, no parsetos() support.\n",
191 								prompt);
192 #endif
193 		    }
194 			break;
195 		case 'X':
196 #ifdef	AUTHENTICATION
197 			auth_disable_name(optarg);
198 #endif
199 			break;
200 		case 'a':
201 			autologin = 1;
202 			break;
203 		case 'c':
204 			skiprc = 1;
205 			break;
206 		case 'd':
207 			debug = 1;
208 			break;
209 		case 'e':
210 			set_escape_char(optarg);
211 			break;
212 		case 'f':
213 #if defined(AUTHENTICATION) && defined(KRB5) && defined(FORWARD)
214 			if (forward_flags & OPTS_FORWARD_CREDS) {
215 			    fprintf(stderr,
216 				    "%s: Only one of -f and -F allowed.\n",
217 				    prompt);
218 			    usage();
219 			}
220 			forward_flags |= OPTS_FORWARD_CREDS;
221 #else
222 			fprintf(stderr,
223 			 "%s: Warning: -f ignored, no Kerberos V5 support.\n",
224 				prompt);
225 #endif
226 			break;
227 		case 'F':
228 #if defined(AUTHENTICATION) && defined(KRB5) && defined(FORWARD)
229 			if (forward_flags & OPTS_FORWARD_CREDS) {
230 			    fprintf(stderr,
231 				    "%s: Only one of -f and -F allowed.\n",
232 				    prompt);
233 			    usage();
234 			}
235 			forward_flags |= OPTS_FORWARD_CREDS;
236 			forward_flags |= OPTS_FORWARDABLE_CREDS;
237 #else
238 			fprintf(stderr,
239 			 "%s: Warning: -F ignored, no Kerberos V5 support.\n",
240 				prompt);
241 #endif
242 			break;
243 		case 'k':
244 #if defined(AUTHENTICATION) && defined(KRB4)
245 		    {
246 			extern char *dest_realm, dst_realm_buf[], dst_realm_sz;
247 			dest_realm = dst_realm_buf;
248 			(void)strncpy(dest_realm, optarg, dst_realm_sz);
249 		    }
250 #else
251 			fprintf(stderr,
252 			   "%s: Warning: -k ignored, no Kerberos V4 support.\n",
253 								prompt);
254 #endif
255 			break;
256 		case 'l':
257 			autologin = 1;
258 			user = optarg;
259 			break;
260 		case 'n':
261 #if defined(TN3270) && defined(unix)
262 			/* distinguish between "-n oasynch" and "-noasynch" */
263 			if (argv[optind - 1][0] == '-' && argv[optind - 1][1]
264 			    == 'n' && argv[optind - 1][2] == 'o') {
265 				if (!strcmp(optarg, "oasynch")) {
266 					noasynchtty = 1;
267 					noasynchnet = 1;
268 				} else if (!strcmp(optarg, "oasynchtty"))
269 					noasynchtty = 1;
270 				else if (!strcmp(optarg, "oasynchnet"))
271 					noasynchnet = 1;
272 			} else
273 #endif	/* defined(TN3270) && defined(unix) */
274 				SetNetTrace(optarg);
275 			break;
276 		case 'r':
277 			rlogin = '~';
278 			break;
279 		case 's':
280 			src_addr = optarg;
281 			break;
282 		case 't':
283 #if defined(TN3270) && defined(unix)
284 			transcom = tline;
285 			(void)strcpy(transcom, optarg);
286 #else
287 			fprintf(stderr,
288 			   "%s: Warning: -t ignored, no TN3270 support.\n",
289 								prompt);
290 #endif
291 			break;
292 		case 'x':
293 #ifdef	ENCRYPTION
294 			encrypt_auto(1);
295 			decrypt_auto(1);
296 #else	/* ENCRYPTION */
297 			fprintf(stderr,
298 			    "%s: Warning: -x ignored, no ENCRYPT support.\n",
299 								prompt);
300 #endif	/* ENCRYPTION */
301 			break;
302 		case '?':
303 		default:
304 			usage();
305 			/* NOTREACHED */
306 		}
307 	}
308 	if (autologin == -1)
309 		autologin = (rlogin == _POSIX_VDISABLE) ? 0 : 1;
310 
311 	argc -= optind;
312 	argv += optind;
313 
314 	if (argc) {
315 		char *args[9], **argp = args;
316 
317 		if (argc > 2)
318 			usage();
319 		*argp++ = prompt;
320 		if (user) {
321 			*argp++ = "-l";
322 			*argp++ = user;
323 		}
324 		if (src_addr) {
325 			*argp++ = "-s";
326 			*argp++ = src_addr;
327 		}
328 		*argp++ = argv[0];		/* host */
329 		if (argc > 1)
330 			*argp++ = argv[1];	/* port */
331 		*argp = 0;
332 
333 		if (setjmp(toplevel) != 0)
334 			Exit(0);
335 		if (tn(argp - args, args) == 1)
336 			return (0);
337 		else
338 			return (1);
339 	}
340 	(void)setjmp(toplevel);
341 	for (;;) {
342 #ifdef TN3270
343 		if (shell_active)
344 			shell_continue();
345 		else
346 #endif
347 			command(1, 0, 0);
348 	}
349 	return 0;
350 }
351