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