main.c revision 1.24 1 /* $NetBSD: main.c,v 1.24 2006/02/02 19:33:12 he 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. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 #ifndef lint
34 __COPYRIGHT("@(#) Copyright (c) 1988, 1990, 1993\n\
35 The Regents of the University of California. All rights reserved.\n");
36 #endif /* not lint */
37
38 #ifndef lint
39 #if 0
40 static char sccsid[] = "@(#)main.c 8.3 (Berkeley) 5/30/95";
41 #else
42 __RCSID("$NetBSD: main.c,v 1.24 2006/02/02 19:33:12 he Exp $");
43 #endif
44 #endif /* not lint */
45
46 #include <sys/types.h>
47 #include <sys/socket.h>
48
49 #include <unistd.h>
50
51 #include "ring.h"
52 #include "externs.h"
53 #include "defines.h"
54 #ifdef AUTHENTICATION
55 #include <libtelnet/auth.h>
56 #endif
57 #ifdef ENCRYPTION
58 #include <libtelnet/encrypt.h>
59 #endif
60
61 /* These values need to be the same as defined in libtelnet/kerberos5.c */
62 /* Either define them in both places, or put in some common header file. */
63 #define OPTS_FORWARD_CREDS 0x00000002
64 #define OPTS_FORWARDABLE_CREDS 0x00000001
65
66 #if defined(IPSEC) && defined(IPSEC_POLICY_IPSEC)
67 char *ipsec_policy_in = NULL;
68 char *ipsec_policy_out = NULL;
69 #endif
70
71 int family = AF_UNSPEC;
72
73 int main(int, char *[]);
74
75 /*
76 * Initialize variables.
77 */
78 void
79 tninit(void)
80 {
81 init_terminal();
82
83 init_network();
84
85 init_telnet();
86
87 init_sys();
88
89 #ifdef TN3270
90 init_3270();
91 #endif
92 }
93
94 void
95 usage()
96 {
97 fprintf(stderr, "usage: %s %s%s%s%s\n",
98 prompt,
99 #ifdef AUTHENTICATION
100 "[-4] [-6] [-8] [-E] [-K] [-L] [-N] [-S tos] [-X atype] [-a] [-c]",
101 "\n\t[-d] [-e char] [-k realm] [-l user] [-f/-F] [-n tracefile] ",
102 #else
103 "[-4] [-6] [-8] [-E] [-L] [-N] [-S tos] [-a] [-c] [-d] [-e char]",
104 "\n\t[-l user] [-n tracefile] ",
105 #endif
106 #ifdef TN3270
107 # ifdef AUTHENTICATION
108 "[-noasynch] [-noasynctty]\n\t[-noasyncnet] [-r] [-t transcom] ",
109 # else
110 "[-noasynch] [-noasynctty] [-noasyncnet] [-r]\n\t[-t transcom]",
111 # endif
112 #else
113 "[-r] ",
114 #endif
115 #ifdef ENCRYPTION
116 "[-x] "
117 #endif
118 #if defined(IPSEC) && defined(IPSEC_POLICY_IPSEC)
119 "\n\t[-P policy] [host-name [port]]"
120 #else
121 "\n\t[host-name [port]]"
122 #endif
123 );
124 exit(1);
125 }
126
127 /*
128 * main. Parse arguments, invoke the protocol or command parser.
129 */
130
131
132 int
133 main(int argc, char *argv[])
134 {
135 extern char *optarg;
136 extern int optind;
137 int ch;
138 char *user;
139 #ifdef FORWARD
140 extern int forward_flags;
141 #endif /* FORWARD */
142
143 tninit(); /* Clear out things */
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, "468EKLNS:X:acde:fFk:l:n:rt:x"
163 IPSECOPT)) != -1) {
164 #undef IPSECOPT
165 switch(ch) {
166 case '4':
167 family = AF_INET;
168 break;
169 case '6':
170 family = AF_INET6;
171 break;
172 case '8':
173 eight = 3; /* binary output and input */
174 break;
175 case 'E':
176 rlogin = escape = _POSIX_VDISABLE;
177 break;
178 case 'K':
179 #ifdef AUTHENTICATION
180 autologin = 0;
181 #endif
182 break;
183 case 'L':
184 eight |= 2; /* binary output only */
185 break;
186 case 'N':
187 doaddrlookup = 0;
188 break;
189 case 'S':
190 {
191 fprintf(stderr,
192 "%s: Warning: -S ignored, no parsetos() support.\n",
193 prompt);
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 telnet_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)strlcpy(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 if(autologin == 0) {
259 autologin = -1;
260 }
261 user = optarg;
262 break;
263 case 'n':
264 #ifdef TN3270
265 /* distinguish between "-n oasynch" and "-noasynch" */
266 if (argv[optind - 1][0] == '-' && argv[optind - 1][1]
267 == 'n' && argv[optind - 1][2] == 'o') {
268 if (!strcmp(optarg, "oasynch")) {
269 noasynchtty = 1;
270 noasynchnet = 1;
271 } else if (!strcmp(optarg, "oasynchtty"))
272 noasynchtty = 1;
273 else if (!strcmp(optarg, "oasynchnet"))
274 noasynchnet = 1;
275 } else
276 #endif /* defined(TN3270) */
277 SetNetTrace(optarg);
278 break;
279 case 'r':
280 rlogin = '~';
281 break;
282 case 't':
283 #ifdef TN3270
284 (void)strlcpy(tline, optarg, sizeof(tline));
285 transcom = tline;
286 #else
287 fprintf(stderr,
288 "%s: Warning: -t ignored, no TN3270 support.\n",
289 prompt);
290 #endif
291 break;
292 case 'x':
293 #ifdef ENCRYPTION
294 encrypt_auto(1);
295 decrypt_auto(1);
296 #else /* ENCRYPTION */
297 fprintf(stderr,
298 "%s: Warning: -x ignored, no ENCRYPT support.\n",
299 prompt);
300 #endif /* ENCRYPTION */
301 break;
302 #if defined(IPSEC) && defined(IPSEC_POLICY_IPSEC)
303 case 'P':
304 if (!strncmp("in", optarg, 2))
305 ipsec_policy_in = strdup(optarg);
306 else if (!strncmp("out", optarg, 3))
307 ipsec_policy_out = strdup(optarg);
308 else
309 usage();
310 break;
311 #endif
312 case '?':
313 default:
314 usage();
315 /* NOTREACHED */
316 }
317 }
318
319 if (autologin == -1) { /* esc (at) magic.fi; force */
320 #if defined(AUTHENTICATION)
321 autologin = 1;
322 #endif
323 #if defined(ENCRYPTION)
324 encrypt_auto(1);
325 decrypt_auto(1);
326 #endif
327 }
328
329 if (autologin == -1)
330 autologin = (rlogin == _POSIX_VDISABLE) ? 0 : 1;
331
332 argc -= optind;
333 argv += optind;
334
335 if (argc) {
336 char *args[7], **argp = args;
337 #ifdef __GNUC__
338 (void) &argp; /* avoid longjmp clobbering */
339 #endif
340
341 if (argc > 2)
342 usage();
343 *argp++ = prompt;
344 if (user) {
345 *argp++ = "-l";
346 *argp++ = user;
347 }
348 *argp++ = argv[0]; /* host */
349 if (argc > 1)
350 *argp++ = argv[1]; /* port */
351 *argp = 0;
352
353 if (setjmp(toplevel) != 0)
354 Exit(0);
355 if (tn(argp - args, args) == 1)
356 return (0);
357 else
358 return (1);
359 }
360 (void)setjmp(toplevel);
361 for (;;) {
362 #ifdef TN3270
363 if (shell_active)
364 shell_continue();
365 else
366 #endif
367 command(1, 0, 0);
368 }
369 }
370