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