xref: /netbsd-src/usr.bin/telnet/main.c (revision 2a399c6883d870daece976daec6ffa7bb7f934ce)
1 /*	$NetBSD: main.c,v 1.6 1997/11/01 06:51:46 lukem 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. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by the University of
18  *	California, Berkeley and its contributors.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #ifndef lint
37 static char copyright[] =
38 "@(#) Copyright (c) 1988, 1990, 1993\n\
39 	The Regents of the University of California.  All rights reserved.\n";
40 #endif /* not lint */
41 
42 #ifndef lint
43 #if 0
44 static char sccsid[] = "@(#)main.c	8.3 (Berkeley) 5/30/95";
45 #else
46 static char rcsid[] = "$NetBSD: main.c,v 1.6 1997/11/01 06:51:46 lukem Exp $";
47 #endif
48 #endif /* not lint */
49 
50 #include <sys/types.h>
51 
52 #include "ring.h"
53 #include "externs.h"
54 #include "defines.h"
55 
56 /* These values need to be the same as defined in libtelnet/kerberos5.c */
57 /* Either define them in both places, or put in some common header file. */
58 #define OPTS_FORWARD_CREDS	0x00000002
59 #define OPTS_FORWARDABLE_CREDS	0x00000001
60 
61 #if 0
62 #define FORWARD
63 #endif
64 
65 /*
66  * Initialize variables.
67  */
68     void
69 tninit()
70 {
71     init_terminal();
72 
73     init_network();
74 
75     init_telnet();
76 
77     init_sys();
78 
79 #if defined(TN3270)
80     init_3270();
81 #endif
82 }
83 
84 	void
85 usage()
86 {
87 	fprintf(stderr, "Usage: %s %s%s%s%s\n",
88 	    prompt,
89 #ifdef	AUTHENTICATION
90 	    "[-8] [-E] [-K] [-L] [-S tos] [-X atype] [-a] [-c] [-d] [-e char]",
91 	    "\n\t[-k realm] [-l user] [-f/-F] [-n tracefile] ",
92 #else
93 	    "[-8] [-E] [-L] [-S tos] [-a] [-c] [-d] [-e char] [-l user]",
94 	    "\n\t[-n tracefile]",
95 #endif
96 #if defined(TN3270) && defined(unix)
97 # ifdef AUTHENTICATION
98 	    "[-noasynch] [-noasynctty]\n\t[-noasyncnet] [-r] [-t transcom] ",
99 # else
100 	    "[-noasynch] [-noasynctty] [-noasyncnet] [-r]\n\t[-t transcom]",
101 # endif
102 #else
103 	    "[-r] ",
104 #endif
105 	    "[host-name [port]]"
106 	);
107 	exit(1);
108 }
109 
110 /*
111  * main.  Parse arguments, invoke the protocol or command parser.
112  */
113 
114 
115 main(argc, argv)
116 	int argc;
117 	char *argv[];
118 {
119 	extern char *optarg;
120 	extern int optind;
121 	int ch;
122 	char *user, *strrchr();
123 #ifdef	FORWARD
124 	extern int forward_flags;
125 #endif	/* FORWARD */
126 
127 	tninit();		/* Clear out things */
128 #if	defined(CRAY) && !defined(__STDC__)
129 	_setlist_init();	/* Work around compiler bug */
130 #endif
131 
132 	TerminalSaveState();
133 
134 	if (prompt = strrchr(argv[0], '/'))
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 	while ((ch = getopt(argc, argv, "8EKLS:X:acde:fFk:l:n:rt:x")) != -1) {
145 		switch(ch) {
146 		case '8':
147 			eight = 3;	/* binary output and input */
148 			break;
149 		case 'E':
150 			rlogin = escape = _POSIX_VDISABLE;
151 			break;
152 		case 'K':
153 #ifdef	AUTHENTICATION
154 			autologin = 0;
155 #endif
156 			break;
157 		case 'L':
158 			eight |= 2;	/* binary output only */
159 			break;
160 		case 'S':
161 		    {
162 #ifdef	HAS_GETTOS
163 			extern int tos;
164 
165 			if ((tos = parsetos(optarg, "tcp")) < 0)
166 				fprintf(stderr, "%s%s%s%s\n",
167 					prompt, ": Bad TOS argument '",
168 					optarg,
169 					"; will try to use default TOS");
170 #else
171 			fprintf(stderr,
172 			   "%s: Warning: -S ignored, no parsetos() support.\n",
173 								prompt);
174 #endif
175 		    }
176 			break;
177 		case 'X':
178 #ifdef	AUTHENTICATION
179 			auth_disable_name(optarg);
180 #endif
181 			break;
182 		case 'a':
183 			autologin = 1;
184 			break;
185 		case 'c':
186 			skiprc = 1;
187 			break;
188 		case 'd':
189 			debug = 1;
190 			break;
191 		case 'e':
192 			set_escape_char(optarg);
193 			break;
194 		case 'f':
195 #if defined(AUTHENTICATION) && defined(KRB5) && defined(FORWARD)
196 			if (forward_flags & OPTS_FORWARD_CREDS) {
197 			    fprintf(stderr,
198 				    "%s: Only one of -f and -F allowed.\n",
199 				    prompt);
200 			    usage();
201 			}
202 			forward_flags |= OPTS_FORWARD_CREDS;
203 #else
204 			fprintf(stderr,
205 			 "%s: Warning: -f ignored, no Kerberos V5 support.\n",
206 				prompt);
207 #endif
208 			break;
209 		case 'F':
210 #if defined(AUTHENTICATION) && defined(KRB5) && defined(FORWARD)
211 			if (forward_flags & OPTS_FORWARD_CREDS) {
212 			    fprintf(stderr,
213 				    "%s: Only one of -f and -F allowed.\n",
214 				    prompt);
215 			    usage();
216 			}
217 			forward_flags |= OPTS_FORWARD_CREDS;
218 			forward_flags |= OPTS_FORWARDABLE_CREDS;
219 #else
220 			fprintf(stderr,
221 			 "%s: Warning: -F ignored, no Kerberos V5 support.\n",
222 				prompt);
223 #endif
224 			break;
225 		case 'k':
226 #if defined(AUTHENTICATION) && defined(KRB4)
227 		    {
228 			extern char *dest_realm, dst_realm_buf[], dst_realm_sz;
229 			dest_realm = dst_realm_buf;
230 			(void)strncpy(dest_realm, optarg, dst_realm_sz);
231 		    }
232 #else
233 			fprintf(stderr,
234 			   "%s: Warning: -k ignored, no Kerberos V4 support.\n",
235 								prompt);
236 #endif
237 			break;
238 		case 'l':
239 			autologin = 1;
240 			user = optarg;
241 			break;
242 		case 'n':
243 #if defined(TN3270) && defined(unix)
244 			/* distinguish between "-n oasynch" and "-noasynch" */
245 			if (argv[optind - 1][0] == '-' && argv[optind - 1][1]
246 			    == 'n' && argv[optind - 1][2] == 'o') {
247 				if (!strcmp(optarg, "oasynch")) {
248 					noasynchtty = 1;
249 					noasynchnet = 1;
250 				} else if (!strcmp(optarg, "oasynchtty"))
251 					noasynchtty = 1;
252 				else if (!strcmp(optarg, "oasynchnet"))
253 					noasynchnet = 1;
254 			} else
255 #endif	/* defined(TN3270) && defined(unix) */
256 				SetNetTrace(optarg);
257 			break;
258 		case 'r':
259 			rlogin = '~';
260 			break;
261 		case 't':
262 #if defined(TN3270) && defined(unix)
263 			transcom = tline;
264 			(void)strcpy(transcom, optarg);
265 #else
266 			fprintf(stderr,
267 			   "%s: Warning: -t ignored, no TN3270 support.\n",
268 								prompt);
269 #endif
270 			break;
271 		case 'x':
272 			fprintf(stderr,
273 			    "%s: Warning: -x ignored, no ENCRYPT support.\n",
274 								prompt);
275 			break;
276 		case '?':
277 		default:
278 			usage();
279 			/* NOTREACHED */
280 		}
281 	}
282 	if (autologin == -1)
283 		autologin = (rlogin == _POSIX_VDISABLE) ? 0 : 1;
284 
285 	argc -= optind;
286 	argv += optind;
287 
288 	if (argc) {
289 		char *args[7], **argp = args;
290 
291 		if (argc > 2)
292 			usage();
293 		*argp++ = prompt;
294 		if (user) {
295 			*argp++ = "-l";
296 			*argp++ = user;
297 		}
298 		*argp++ = argv[0];		/* host */
299 		if (argc > 1)
300 			*argp++ = argv[1];	/* port */
301 		*argp = 0;
302 
303 		if (setjmp(toplevel) != 0)
304 			Exit(0);
305 		if (tn(argp - args, args) == 1)
306 			return (0);
307 		else
308 			return (1);
309 	}
310 	(void)setjmp(toplevel);
311 	for (;;) {
312 #ifdef TN3270
313 		if (shell_active)
314 			shell_continue();
315 		else
316 #endif
317 			command(1, 0, 0);
318 	}
319 }
320