main.c revision 1.19 1 1.19 itojun /* $NetBSD: main.c,v 1.19 2003/07/12 13:33:08 itojun Exp $ */
2 1.5 thorpej
3 1.1 cgd /*
4 1.4 jtk * Copyright (c) 1988, 1990, 1993
5 1.4 jtk * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.1 cgd * 3. All advertising materials mentioning features or use of this software
16 1.1 cgd * must display the following acknowledgement:
17 1.1 cgd * This product includes software developed by the University of
18 1.1 cgd * California, Berkeley and its contributors.
19 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
20 1.1 cgd * may be used to endorse or promote products derived from this software
21 1.1 cgd * without specific prior written permission.
22 1.1 cgd *
23 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1 cgd * SUCH DAMAGE.
34 1.1 cgd */
35 1.1 cgd
36 1.7 christos #include <sys/cdefs.h>
37 1.1 cgd #ifndef lint
38 1.7 christos __COPYRIGHT("@(#) Copyright (c) 1988, 1990, 1993\n\
39 1.7 christos The Regents of the University of California. All rights reserved.\n");
40 1.1 cgd #endif /* not lint */
41 1.1 cgd
42 1.1 cgd #ifndef lint
43 1.5 thorpej #if 0
44 1.5 thorpej static char sccsid[] = "@(#)main.c 8.3 (Berkeley) 5/30/95";
45 1.5 thorpej #else
46 1.19 itojun __RCSID("$NetBSD: main.c,v 1.19 2003/07/12 13:33:08 itojun Exp $");
47 1.5 thorpej #endif
48 1.1 cgd #endif /* not lint */
49 1.1 cgd
50 1.1 cgd #include <sys/types.h>
51 1.17 kanaoka #include <sys/socket.h>
52 1.8 enami
53 1.8 enami #include <unistd.h>
54 1.1 cgd
55 1.1 cgd #include "ring.h"
56 1.1 cgd #include "externs.h"
57 1.1 cgd #include "defines.h"
58 1.13 thorpej #ifdef AUTHENTICATION
59 1.13 thorpej #include <libtelnet/auth.h>
60 1.13 thorpej #endif
61 1.13 thorpej #ifdef ENCRYPTION
62 1.13 thorpej #include <libtelnet/encrypt.h>
63 1.13 thorpej #endif
64 1.1 cgd
65 1.3 cgd /* These values need to be the same as defined in libtelnet/kerberos5.c */
66 1.3 cgd /* Either define them in both places, or put in some common header file. */
67 1.4 jtk #define OPTS_FORWARD_CREDS 0x00000002
68 1.4 jtk #define OPTS_FORWARDABLE_CREDS 0x00000001
69 1.3 cgd
70 1.3 cgd #if 0
71 1.3 cgd #define FORWARD
72 1.3 cgd #endif
73 1.3 cgd
74 1.9 itojun #if defined(IPSEC) && defined(IPSEC_POLICY_IPSEC)
75 1.12 itojun char *ipsec_policy_in = NULL;
76 1.12 itojun char *ipsec_policy_out = NULL;
77 1.9 itojun #endif
78 1.9 itojun
79 1.17 kanaoka int family = AF_UNSPEC;
80 1.17 kanaoka
81 1.16 wiz int main(int, char *[]);
82 1.7 christos
83 1.1 cgd /*
84 1.1 cgd * Initialize variables.
85 1.1 cgd */
86 1.18 christos void
87 1.18 christos tninit(void)
88 1.1 cgd {
89 1.1 cgd init_terminal();
90 1.1 cgd
91 1.1 cgd init_network();
92 1.4 jtk
93 1.1 cgd init_telnet();
94 1.1 cgd
95 1.1 cgd init_sys();
96 1.1 cgd
97 1.1 cgd #if defined(TN3270)
98 1.1 cgd init_3270();
99 1.1 cgd #endif
100 1.1 cgd }
101 1.1 cgd
102 1.1 cgd void
103 1.1 cgd usage()
104 1.1 cgd {
105 1.1 cgd fprintf(stderr, "Usage: %s %s%s%s%s\n",
106 1.1 cgd prompt,
107 1.3 cgd #ifdef AUTHENTICATION
108 1.17 kanaoka "[-4] [-6] [-8] [-E] [-K] [-L] [-N] [-S tos] [-X atype] [-a] [-c] [-d]",
109 1.11 abs "\n\t[-e char] [-k realm] [-l user] [-f/-F] [-n tracefile] ",
110 1.1 cgd #else
111 1.17 kanaoka "[-4] [-6] [-8] [-E] [-L] [-N] [-S tos] [-a] [-c] [-d] [-e char] [-l user]",
112 1.4 jtk "\n\t[-n tracefile]",
113 1.1 cgd #endif
114 1.1 cgd #if defined(TN3270) && defined(unix)
115 1.3 cgd # ifdef AUTHENTICATION
116 1.4 jtk "[-noasynch] [-noasynctty]\n\t[-noasyncnet] [-r] [-t transcom] ",
117 1.1 cgd # else
118 1.4 jtk "[-noasynch] [-noasynctty] [-noasyncnet] [-r]\n\t[-t transcom]",
119 1.1 cgd # endif
120 1.1 cgd #else
121 1.1 cgd "[-r] ",
122 1.1 cgd #endif
123 1.13 thorpej #ifdef ENCRYPTION
124 1.13 thorpej "[-x] "
125 1.13 thorpej #endif
126 1.9 itojun #if defined(IPSEC) && defined(IPSEC_POLICY_IPSEC)
127 1.12 itojun "[-P policy] [host-name [port]]"
128 1.12 itojun #else
129 1.12 itojun "[host-name [port]]"
130 1.10 thorpej #endif
131 1.1 cgd );
132 1.1 cgd exit(1);
133 1.1 cgd }
134 1.1 cgd
135 1.1 cgd /*
136 1.1 cgd * main. Parse arguments, invoke the protocol or command parser.
137 1.1 cgd */
138 1.1 cgd
139 1.1 cgd
140 1.7 christos int
141 1.18 christos main(int argc, char *argv[])
142 1.1 cgd {
143 1.1 cgd extern char *optarg;
144 1.1 cgd extern int optind;
145 1.1 cgd int ch;
146 1.7 christos char *user;
147 1.3 cgd #ifdef FORWARD
148 1.3 cgd extern int forward_flags;
149 1.3 cgd #endif /* FORWARD */
150 1.1 cgd
151 1.1 cgd tninit(); /* Clear out things */
152 1.1 cgd
153 1.1 cgd TerminalSaveState();
154 1.1 cgd
155 1.7 christos if ((prompt = strrchr(argv[0], '/')) != NULL)
156 1.1 cgd ++prompt;
157 1.1 cgd else
158 1.1 cgd prompt = argv[0];
159 1.1 cgd
160 1.1 cgd user = NULL;
161 1.1 cgd
162 1.1 cgd rlogin = (strncmp(prompt, "rlog", 4) == 0) ? '~' : _POSIX_VDISABLE;
163 1.1 cgd autologin = -1;
164 1.1 cgd
165 1.9 itojun #if defined(IPSEC) && defined(IPSEC_POLICY_IPSEC)
166 1.9 itojun #define IPSECOPT "P:"
167 1.9 itojun #else
168 1.9 itojun #define IPSECOPT
169 1.9 itojun #endif
170 1.17 kanaoka while ((ch = getopt(argc, argv, "468EKLNS:X:acde:fFk:l:n:rt:x"
171 1.9 itojun IPSECOPT)) != -1) {
172 1.9 itojun #undef IPSECOPT
173 1.1 cgd switch(ch) {
174 1.17 kanaoka case '4':
175 1.17 kanaoka family = AF_INET;
176 1.17 kanaoka break;
177 1.17 kanaoka case '6':
178 1.17 kanaoka family = AF_INET6;
179 1.17 kanaoka break;
180 1.1 cgd case '8':
181 1.1 cgd eight = 3; /* binary output and input */
182 1.1 cgd break;
183 1.1 cgd case 'E':
184 1.1 cgd rlogin = escape = _POSIX_VDISABLE;
185 1.1 cgd break;
186 1.1 cgd case 'K':
187 1.3 cgd #ifdef AUTHENTICATION
188 1.1 cgd autologin = 0;
189 1.1 cgd #endif
190 1.1 cgd break;
191 1.1 cgd case 'L':
192 1.1 cgd eight |= 2; /* binary output only */
193 1.11 abs break;
194 1.11 abs case 'N':
195 1.11 abs doaddrlookup = 0;
196 1.1 cgd break;
197 1.1 cgd case 'S':
198 1.1 cgd {
199 1.1 cgd #ifdef HAS_GETTOS
200 1.1 cgd extern int tos;
201 1.1 cgd
202 1.1 cgd if ((tos = parsetos(optarg, "tcp")) < 0)
203 1.1 cgd fprintf(stderr, "%s%s%s%s\n",
204 1.1 cgd prompt, ": Bad TOS argument '",
205 1.1 cgd optarg,
206 1.1 cgd "; will try to use default TOS");
207 1.1 cgd #else
208 1.1 cgd fprintf(stderr,
209 1.1 cgd "%s: Warning: -S ignored, no parsetos() support.\n",
210 1.1 cgd prompt);
211 1.1 cgd #endif
212 1.1 cgd }
213 1.1 cgd break;
214 1.1 cgd case 'X':
215 1.3 cgd #ifdef AUTHENTICATION
216 1.1 cgd auth_disable_name(optarg);
217 1.1 cgd #endif
218 1.1 cgd break;
219 1.1 cgd case 'a':
220 1.1 cgd autologin = 1;
221 1.1 cgd break;
222 1.1 cgd case 'c':
223 1.1 cgd skiprc = 1;
224 1.1 cgd break;
225 1.1 cgd case 'd':
226 1.1 cgd debug = 1;
227 1.1 cgd break;
228 1.1 cgd case 'e':
229 1.1 cgd set_escape_char(optarg);
230 1.1 cgd break;
231 1.3 cgd case 'f':
232 1.3 cgd #if defined(AUTHENTICATION) && defined(KRB5) && defined(FORWARD)
233 1.3 cgd if (forward_flags & OPTS_FORWARD_CREDS) {
234 1.4 jtk fprintf(stderr,
235 1.3 cgd "%s: Only one of -f and -F allowed.\n",
236 1.3 cgd prompt);
237 1.3 cgd usage();
238 1.3 cgd }
239 1.3 cgd forward_flags |= OPTS_FORWARD_CREDS;
240 1.3 cgd #else
241 1.3 cgd fprintf(stderr,
242 1.4 jtk "%s: Warning: -f ignored, no Kerberos V5 support.\n",
243 1.3 cgd prompt);
244 1.3 cgd #endif
245 1.3 cgd break;
246 1.3 cgd case 'F':
247 1.3 cgd #if defined(AUTHENTICATION) && defined(KRB5) && defined(FORWARD)
248 1.3 cgd if (forward_flags & OPTS_FORWARD_CREDS) {
249 1.4 jtk fprintf(stderr,
250 1.3 cgd "%s: Only one of -f and -F allowed.\n",
251 1.3 cgd prompt);
252 1.3 cgd usage();
253 1.3 cgd }
254 1.3 cgd forward_flags |= OPTS_FORWARD_CREDS;
255 1.3 cgd forward_flags |= OPTS_FORWARDABLE_CREDS;
256 1.3 cgd #else
257 1.3 cgd fprintf(stderr,
258 1.4 jtk "%s: Warning: -F ignored, no Kerberos V5 support.\n",
259 1.3 cgd prompt);
260 1.3 cgd #endif
261 1.3 cgd break;
262 1.1 cgd case 'k':
263 1.3 cgd #if defined(AUTHENTICATION) && defined(KRB4)
264 1.1 cgd {
265 1.1 cgd extern char *dest_realm, dst_realm_buf[], dst_realm_sz;
266 1.1 cgd dest_realm = dst_realm_buf;
267 1.19 itojun (void)strlcpy(dest_realm, optarg, dst_realm_sz);
268 1.1 cgd }
269 1.1 cgd #else
270 1.1 cgd fprintf(stderr,
271 1.1 cgd "%s: Warning: -k ignored, no Kerberos V4 support.\n",
272 1.1 cgd prompt);
273 1.1 cgd #endif
274 1.1 cgd break;
275 1.1 cgd case 'l':
276 1.15 assar if(autologin == 0) {
277 1.15 assar autologin = -1;
278 1.15 assar }
279 1.1 cgd user = optarg;
280 1.1 cgd break;
281 1.1 cgd case 'n':
282 1.1 cgd #if defined(TN3270) && defined(unix)
283 1.1 cgd /* distinguish between "-n oasynch" and "-noasynch" */
284 1.1 cgd if (argv[optind - 1][0] == '-' && argv[optind - 1][1]
285 1.1 cgd == 'n' && argv[optind - 1][2] == 'o') {
286 1.1 cgd if (!strcmp(optarg, "oasynch")) {
287 1.1 cgd noasynchtty = 1;
288 1.1 cgd noasynchnet = 1;
289 1.1 cgd } else if (!strcmp(optarg, "oasynchtty"))
290 1.1 cgd noasynchtty = 1;
291 1.1 cgd else if (!strcmp(optarg, "oasynchnet"))
292 1.1 cgd noasynchnet = 1;
293 1.1 cgd } else
294 1.1 cgd #endif /* defined(TN3270) && defined(unix) */
295 1.1 cgd SetNetTrace(optarg);
296 1.1 cgd break;
297 1.1 cgd case 'r':
298 1.1 cgd rlogin = '~';
299 1.1 cgd break;
300 1.1 cgd case 't':
301 1.1 cgd #if defined(TN3270) && defined(unix)
302 1.14 mjl (void)strlcpy(tline, optarg, sizeof(tline));
303 1.1 cgd transcom = tline;
304 1.1 cgd #else
305 1.1 cgd fprintf(stderr,
306 1.1 cgd "%s: Warning: -t ignored, no TN3270 support.\n",
307 1.1 cgd prompt);
308 1.1 cgd #endif
309 1.1 cgd break;
310 1.1 cgd case 'x':
311 1.13 thorpej #ifdef ENCRYPTION
312 1.13 thorpej encrypt_auto(1);
313 1.13 thorpej decrypt_auto(1);
314 1.13 thorpej #else /* ENCRYPTION */
315 1.1 cgd fprintf(stderr,
316 1.1 cgd "%s: Warning: -x ignored, no ENCRYPT support.\n",
317 1.1 cgd prompt);
318 1.13 thorpej #endif /* ENCRYPTION */
319 1.1 cgd break;
320 1.9 itojun #if defined(IPSEC) && defined(IPSEC_POLICY_IPSEC)
321 1.9 itojun case 'P':
322 1.12 itojun if (!strncmp("in", optarg, 2))
323 1.12 itojun ipsec_policy_in = strdup(optarg);
324 1.12 itojun else if (!strncmp("out", optarg, 3))
325 1.12 itojun ipsec_policy_out = strdup(optarg);
326 1.12 itojun else
327 1.12 itojun usage();
328 1.9 itojun break;
329 1.9 itojun #endif
330 1.1 cgd case '?':
331 1.1 cgd default:
332 1.1 cgd usage();
333 1.1 cgd /* NOTREACHED */
334 1.1 cgd }
335 1.1 cgd }
336 1.15 assar
337 1.15 assar if (autologin == -1) { /* esc (at) magic.fi; force */
338 1.15 assar #if defined(AUTHENTICATION)
339 1.15 assar autologin = 1;
340 1.15 assar #endif
341 1.15 assar #if defined(ENCRYPTION)
342 1.15 assar encrypt_auto(1);
343 1.15 assar decrypt_auto(1);
344 1.15 assar #endif
345 1.15 assar }
346 1.15 assar
347 1.1 cgd if (autologin == -1)
348 1.1 cgd autologin = (rlogin == _POSIX_VDISABLE) ? 0 : 1;
349 1.1 cgd
350 1.1 cgd argc -= optind;
351 1.1 cgd argv += optind;
352 1.1 cgd
353 1.1 cgd if (argc) {
354 1.1 cgd char *args[7], **argp = args;
355 1.7 christos #ifdef __GNUC__
356 1.7 christos (void) &argp; /* avoid longjmp clobbering */
357 1.7 christos #endif
358 1.1 cgd
359 1.1 cgd if (argc > 2)
360 1.1 cgd usage();
361 1.1 cgd *argp++ = prompt;
362 1.1 cgd if (user) {
363 1.1 cgd *argp++ = "-l";
364 1.1 cgd *argp++ = user;
365 1.1 cgd }
366 1.1 cgd *argp++ = argv[0]; /* host */
367 1.1 cgd if (argc > 1)
368 1.1 cgd *argp++ = argv[1]; /* port */
369 1.1 cgd *argp = 0;
370 1.1 cgd
371 1.1 cgd if (setjmp(toplevel) != 0)
372 1.1 cgd Exit(0);
373 1.1 cgd if (tn(argp - args, args) == 1)
374 1.1 cgd return (0);
375 1.1 cgd else
376 1.1 cgd return (1);
377 1.1 cgd }
378 1.1 cgd (void)setjmp(toplevel);
379 1.1 cgd for (;;) {
380 1.1 cgd #ifdef TN3270
381 1.1 cgd if (shell_active)
382 1.1 cgd shell_continue();
383 1.1 cgd else
384 1.1 cgd #endif
385 1.1 cgd command(1, 0, 0);
386 1.1 cgd }
387 1.1 cgd }
388