main.c revision 1.9 1 /* $NetBSD: main.c,v 1.9 1999/07/02 15:09:10 itojun 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 #include <sys/cdefs.h>
37 #ifndef lint
38 __COPYRIGHT("@(#) 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 __RCSID("$NetBSD: main.c,v 1.9 1999/07/02 15:09:10 itojun Exp $");
47 #endif
48 #endif /* not lint */
49
50 #include <sys/types.h>
51
52 #include <unistd.h>
53
54 #include "ring.h"
55 #include "externs.h"
56 #include "defines.h"
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 #if defined(IPSEC) && defined(IPSEC_POLICY_IPSEC)
68 char *ipsec_policy = NULL;
69 #endif
70
71 int main P((int, char *[]));
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] [-S tos] [-X atype] [-a] [-c] [-d] [-e char]",
99 "\n\t[-k realm] [-l user] [-f/-F] [-n tracefile] ",
100 #else
101 "[-8] [-E] [-L] [-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[-noasyncnet] [-r] [-t transcom] ",
107 # else
108 "[-noasynch] [-noasynctty] [-noasyncnet] [-r]\n\t[-t transcom]",
109 # endif
110 #else
111 "[-r] ",
112 #endif
113 #if defined(IPSEC) && defined(IPSEC_POLICY_IPSEC)
114 "[-P policy] [host-name [port]]"
115 #else
116 "[host-name [port]]"
117 #endif
118 );
119 exit(1);
120 }
121
122 /*
123 * main. Parse arguments, invoke the protocol or command parser.
124 */
125
126
127 int
128 main(argc, argv)
129 int argc;
130 char *argv[];
131 {
132 extern char *optarg;
133 extern int optind;
134 int ch;
135 char *user;
136 #ifdef FORWARD
137 extern int forward_flags;
138 #endif /* FORWARD */
139
140 tninit(); /* Clear out things */
141 #if defined(CRAY) && !defined(__STDC__)
142 _setlist_init(); /* Work around compiler bug */
143 #endif
144
145 TerminalSaveState();
146
147 if ((prompt = strrchr(argv[0], '/')) != NULL)
148 ++prompt;
149 else
150 prompt = argv[0];
151
152 user = NULL;
153
154 rlogin = (strncmp(prompt, "rlog", 4) == 0) ? '~' : _POSIX_VDISABLE;
155 autologin = -1;
156
157 #if defined(IPSEC) && defined(IPSEC_POLICY_IPSEC)
158 #define IPSECOPT "P:"
159 #else
160 #define IPSECOPT
161 #endif
162 while ((ch = getopt(argc, argv, "8EKLS:X:acde:fFk:l:n:rt:x"
163 IPSECOPT)) != -1) {
164 #undef IPSECOPT
165 switch(ch) {
166 case '8':
167 eight = 3; /* binary output and input */
168 break;
169 case 'E':
170 rlogin = escape = _POSIX_VDISABLE;
171 break;
172 case 'K':
173 #ifdef AUTHENTICATION
174 autologin = 0;
175 #endif
176 break;
177 case 'L':
178 eight |= 2; /* binary output only */
179 break;
180 case 'S':
181 {
182 #ifdef HAS_GETTOS
183 extern int tos;
184
185 if ((tos = parsetos(optarg, "tcp")) < 0)
186 fprintf(stderr, "%s%s%s%s\n",
187 prompt, ": Bad TOS argument '",
188 optarg,
189 "; will try to use default TOS");
190 #else
191 fprintf(stderr,
192 "%s: Warning: -S ignored, no parsetos() support.\n",
193 prompt);
194 #endif
195 }
196 break;
197 case 'X':
198 #ifdef AUTHENTICATION
199 auth_disable_name(optarg);
200 #endif
201 break;
202 case 'a':
203 autologin = 1;
204 break;
205 case 'c':
206 skiprc = 1;
207 break;
208 case 'd':
209 debug = 1;
210 break;
211 case 'e':
212 set_escape_char(optarg);
213 break;
214 case 'f':
215 #if defined(AUTHENTICATION) && defined(KRB5) && defined(FORWARD)
216 if (forward_flags & OPTS_FORWARD_CREDS) {
217 fprintf(stderr,
218 "%s: Only one of -f and -F allowed.\n",
219 prompt);
220 usage();
221 }
222 forward_flags |= OPTS_FORWARD_CREDS;
223 #else
224 fprintf(stderr,
225 "%s: Warning: -f ignored, no Kerberos V5 support.\n",
226 prompt);
227 #endif
228 break;
229 case 'F':
230 #if defined(AUTHENTICATION) && defined(KRB5) && defined(FORWARD)
231 if (forward_flags & OPTS_FORWARD_CREDS) {
232 fprintf(stderr,
233 "%s: Only one of -f and -F allowed.\n",
234 prompt);
235 usage();
236 }
237 forward_flags |= OPTS_FORWARD_CREDS;
238 forward_flags |= OPTS_FORWARDABLE_CREDS;
239 #else
240 fprintf(stderr,
241 "%s: Warning: -F ignored, no Kerberos V5 support.\n",
242 prompt);
243 #endif
244 break;
245 case 'k':
246 #if defined(AUTHENTICATION) && defined(KRB4)
247 {
248 extern char *dest_realm, dst_realm_buf[], dst_realm_sz;
249 dest_realm = dst_realm_buf;
250 (void)strncpy(dest_realm, optarg, dst_realm_sz);
251 }
252 #else
253 fprintf(stderr,
254 "%s: Warning: -k ignored, no Kerberos V4 support.\n",
255 prompt);
256 #endif
257 break;
258 case 'l':
259 autologin = 1;
260 user = optarg;
261 break;
262 case 'n':
263 #if defined(TN3270) && defined(unix)
264 /* distinguish between "-n oasynch" and "-noasynch" */
265 if (argv[optind - 1][0] == '-' && argv[optind - 1][1]
266 == 'n' && argv[optind - 1][2] == 'o') {
267 if (!strcmp(optarg, "oasynch")) {
268 noasynchtty = 1;
269 noasynchnet = 1;
270 } else if (!strcmp(optarg, "oasynchtty"))
271 noasynchtty = 1;
272 else if (!strcmp(optarg, "oasynchnet"))
273 noasynchnet = 1;
274 } else
275 #endif /* defined(TN3270) && defined(unix) */
276 SetNetTrace(optarg);
277 break;
278 case 'r':
279 rlogin = '~';
280 break;
281 case 't':
282 #if defined(TN3270) && defined(unix)
283 transcom = tline;
284 (void)strcpy(transcom, optarg);
285 #else
286 fprintf(stderr,
287 "%s: Warning: -t ignored, no TN3270 support.\n",
288 prompt);
289 #endif
290 break;
291 case 'x':
292 fprintf(stderr,
293 "%s: Warning: -x ignored, no ENCRYPT support.\n",
294 prompt);
295 break;
296 #if defined(IPSEC) && defined(IPSEC_POLICY_IPSEC)
297 case 'P':
298 ipsec_policy = strdup(optarg);
299 break;
300 #endif
301 case '?':
302 default:
303 usage();
304 /* NOTREACHED */
305 }
306 }
307 if (autologin == -1)
308 autologin = (rlogin == _POSIX_VDISABLE) ? 0 : 1;
309
310 argc -= optind;
311 argv += optind;
312
313 if (argc) {
314 char *args[7], **argp = args;
315 #ifdef __GNUC__
316 (void) &argp; /* avoid longjmp clobbering */
317 #endif
318
319 if (argc > 2)
320 usage();
321 *argp++ = prompt;
322 if (user) {
323 *argp++ = "-l";
324 *argp++ = user;
325 }
326 *argp++ = argv[0]; /* host */
327 if (argc > 1)
328 *argp++ = argv[1]; /* port */
329 *argp = 0;
330
331 if (setjmp(toplevel) != 0)
332 Exit(0);
333 if (tn(argp - args, args) == 1)
334 return (0);
335 else
336 return (1);
337 }
338 (void)setjmp(toplevel);
339 for (;;) {
340 #ifdef TN3270
341 if (shell_active)
342 shell_continue();
343 else
344 #endif
345 command(1, 0, 0);
346 }
347 }
348