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