No empty .Rs/.Re
[netbsd-mini2440.git] / usr.bin / telnet / main.c
blobc09ba55e2dc645730b5a5bd5f1f41633b6a1681f
1 /* $NetBSD: main.c,v 1.26 2006/12/18 14:18:40 christos Exp $ */
3 /*
4 * Copyright (c) 1988, 1990, 1993
5 * The Regents of the University of California. All rights reserved.
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.
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.
32 #include <sys/cdefs.h>
33 #ifndef lint
34 __COPYRIGHT("@(#) Copyright (c) 1988, 1990, 1993\
35 The Regents of the University of California. All rights reserved.");
36 #endif /* not lint */
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.26 2006/12/18 14:18:40 christos Exp $");
43 #endif
44 #endif /* not lint */
46 #include <sys/types.h>
47 #include <sys/socket.h>
49 #include <unistd.h>
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
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
66 #if defined(IPSEC) && defined(IPSEC_POLICY_IPSEC)
67 char *ipsec_policy_in = NULL;
68 char *ipsec_policy_out = NULL;
69 #endif
71 int family = AF_UNSPEC;
73 int main(int, char *[]);
76 * Initialize variables.
78 void
79 tninit(void)
81 init_terminal();
83 init_network();
85 init_telnet();
87 init_sys();
89 #ifdef TN3270
90 init_3270();
91 #endif
94 void
95 usage()
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
124 exit(1);
128 * main. Parse arguments, invoke the protocol or command parser.
133 main(int argc, char *argv[])
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 */
143 tninit(); /* Clear out things */
145 TerminalSaveState();
147 if ((prompt = strrchr(argv[0], '/')) != NULL)
148 ++prompt;
149 else
150 prompt = argv[0];
152 user = NULL;
154 rlogin = (strncmp(prompt, "rlog", 4) == 0) ? '~' : _POSIX_VDISABLE;
155 autologin = -1;
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':
191 fprintf(stderr,
192 "%s: Warning: -S ignored, no parsetos() support.\n",
193 prompt);
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();
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();
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 fprintf(stderr,
246 "%s: Warning: -k ignored, no Kerberos V4 support.\n",
247 prompt);
248 break;
249 case 'l':
250 if(autologin == 0) {
251 autologin = -1;
253 user = optarg;
254 break;
255 case 'n':
256 #ifdef TN3270
257 /* distinguish between "-n oasynch" and "-noasynch" */
258 if (argv[optind - 1][0] == '-' && argv[optind - 1][1]
259 == 'n' && argv[optind - 1][2] == 'o') {
260 if (!strcmp(optarg, "oasynch")) {
261 noasynchtty = 1;
262 noasynchnet = 1;
263 } else if (!strcmp(optarg, "oasynchtty"))
264 noasynchtty = 1;
265 else if (!strcmp(optarg, "oasynchnet"))
266 noasynchnet = 1;
267 } else
268 #endif /* defined(TN3270) */
269 SetNetTrace(optarg);
270 break;
271 case 'r':
272 rlogin = '~';
273 break;
274 case 't':
275 #ifdef TN3270
276 (void)strlcpy(tline, optarg, sizeof(tline));
277 transcom = tline;
278 #else
279 fprintf(stderr,
280 "%s: Warning: -t ignored, no TN3270 support.\n",
281 prompt);
282 #endif
283 break;
284 case 'x':
285 #ifdef ENCRYPTION
286 encrypt_auto(1);
287 decrypt_auto(1);
288 #else /* ENCRYPTION */
289 fprintf(stderr,
290 "%s: Warning: -x ignored, no ENCRYPT support.\n",
291 prompt);
292 #endif /* ENCRYPTION */
293 break;
294 #if defined(IPSEC) && defined(IPSEC_POLICY_IPSEC)
295 case 'P':
296 if (!strncmp("in", optarg, 2))
297 ipsec_policy_in = strdup(optarg);
298 else if (!strncmp("out", optarg, 3))
299 ipsec_policy_out = strdup(optarg);
300 else
301 usage();
302 break;
303 #endif
304 case '?':
305 default:
306 usage();
307 /* NOTREACHED */
311 if (autologin == -1) { /* esc@magic.fi; force */
312 #if defined(AUTHENTICATION)
313 autologin = 1;
314 #endif
315 #if defined(ENCRYPTION)
316 encrypt_auto(1);
317 decrypt_auto(1);
318 #endif
321 if (autologin == -1)
322 autologin = (rlogin == _POSIX_VDISABLE) ? 0 : 1;
324 argc -= optind;
325 argv += optind;
327 if (argc) {
328 char *args[7];
329 char ** volatile argp; /* avoid longjmp clobbering */
331 argp = args;
332 if (argc > 2)
333 usage();
334 *argp++ = prompt;
335 if (user) {
336 *argp++ = "-l";
337 *argp++ = user;
339 *argp++ = argv[0]; /* host */
340 if (argc > 1)
341 *argp++ = argv[1]; /* port */
342 *argp = 0;
344 if (setjmp(toplevel) != 0)
345 Exit(0);
346 if (tn(argp - args, args) == 1)
347 return (0);
348 else
349 return (1);
351 (void)setjmp(toplevel);
352 for (;;) {
353 #ifdef TN3270
354 if (shell_active)
355 shell_continue();
356 else
357 #endif
358 command(1, 0, 0);