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