Initial import from FreeBSD RELENG_4:
[dragonfly.git] / usr.bin / telnet / main.c
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
34 #include <sys/cdefs.h>
35
36 __FBSDID("$FreeBSD: src/usr.bin/telnet/main.c,v 1.10.2.5 2002/04/13 11:07:13 markm Exp $");
37
38 #ifndef lint
39 static const char sccsid[] = "@(#)main.c        8.3 (Berkeley) 5/30/95";
40 #endif
41
42 #include <sys/types.h>
43 #include <sys/socket.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <unistd.h>
47
48 #include "ring.h"
49 #include "externs.h"
50 #include "defines.h"
51
52
53 /* These values need to be the same as defined in libtelnet/kerberos5.c */
54 /* Either define them in both places, or put in some common header file. */
55 #define OPTS_FORWARD_CREDS      0x00000002
56 #define OPTS_FORWARDABLE_CREDS  0x00000001
57
58 #if 0
59 #define FORWARD
60 #endif
61
62 #if defined(IPSEC) && defined(IPSEC_POLICY_IPSEC)
63 char *ipsec_policy_in = NULL;
64 char *ipsec_policy_out = NULL;
65 #endif
66
67 int family = AF_UNSPEC;
68
69 /*
70  * Initialize variables.
71  */
72 void
73 tninit(void)
74 {
75     init_terminal();
76
77     init_network();
78
79     init_telnet();
80
81     init_sys();
82 }
83
84 static void
85 usage(void)
86 {
87         fprintf(stderr, "Usage: %s %s%s%s%s\n",
88             prompt,
89             "[-4] [-6] [-8] [-E] [-L] [-N] [-S tos] [-c] [-d]",
90             "\n\t[-e char] [-l user] [-n tracefile] ",
91             "[-r] [-s src_addr] [-u] ",
92 #if defined(IPSEC) && defined(IPSEC_POLICY_IPSEC)
93             "[-P policy] "
94 #endif
95             "[host-name [port]]"
96         );
97         exit(1);
98 }
99
100 /*
101  * main.  Parse arguments, invoke the protocol or command parser.
102  */
103
104 int
105 main(int argc, char *argv[])
106 {
107         int ch;
108         char *user;
109         char *src_addr = NULL;
110 #ifdef  FORWARD
111         extern int forward_flags;
112 #endif  /* FORWARD */
113
114         tninit();               /* Clear out things */
115
116         TerminalSaveState();
117
118         if ((prompt = strrchr(argv[0], '/')))
119                 ++prompt;
120         else
121                 prompt = argv[0];
122
123         user = NULL;
124
125         rlogin = (strncmp(prompt, "rlog", 4) == 0) ? '~' : _POSIX_VDISABLE;
126         autologin = -1;
127
128
129 #if defined(IPSEC) && defined(IPSEC_POLICY_IPSEC)
130 #define IPSECOPT        "P:"
131 #else
132 #define IPSECOPT
133 #endif
134         while ((ch = getopt(argc, argv,
135                             "468EKLNS:X:acde:fFk:l:n:rs:t:uxy" IPSECOPT)) != -1)
136 #undef IPSECOPT
137         {
138                 switch(ch) {
139                 case '4':
140                         family = AF_INET;
141                         break;
142 #ifdef INET6
143                 case '6':
144                         family = AF_INET6;
145                         break;
146 #endif
147                 case '8':
148                         eight = 3;      /* binary output and input */
149                         break;
150                 case 'E':
151                         rlogin = escape = _POSIX_VDISABLE;
152                         break;
153                 case 'K':
154                         break;
155                 case 'L':
156                         eight |= 2;     /* binary output only */
157                         break;
158                 case 'N':
159                         doaddrlookup = 0;
160                         break;
161                 case 'S':
162                     {
163 #ifdef  HAS_GETTOS
164                         extern int tos;
165
166                         if ((tos = parsetos(optarg, "tcp")) < 0)
167                                 fprintf(stderr, "%s%s%s%s\n",
168                                         prompt, ": Bad TOS argument '",
169                                         optarg,
170                                         "; will try to use default TOS");
171 #else
172                         fprintf(stderr,
173                            "%s: Warning: -S ignored, no parsetos() support.\n",
174                                                                 prompt);
175 #endif
176                     }
177                         break;
178                 case 'X':
179                         break;
180                 case 'a':
181                         autologin = 1;
182                         break;
183                 case 'c':
184                         skiprc = 1;
185                         break;
186                 case 'd':
187                         debug = 1;
188                         break;
189                 case 'e':
190                         set_escape_char(optarg);
191                         break;
192                 case 'f':
193                         fprintf(stderr,
194                          "%s: Warning: -f ignored, no Kerberos V5 support.\n",
195                                 prompt);
196                         break;
197                 case 'F':
198                         fprintf(stderr,
199                          "%s: Warning: -F ignored, no Kerberos V5 support.\n",
200                                 prompt);
201                         break;
202                 case 'k':
203                         fprintf(stderr,
204                            "%s: Warning: -k ignored, no Kerberos V4 support.\n",
205                                                                 prompt);
206                         break;
207                 case 'l':
208                         autologin = 1;
209                         user = optarg;
210                         break;
211                 case 'n':
212                                 SetNetTrace(optarg);
213                         break;
214                 case 'r':
215                         rlogin = '~';
216                         break;
217                 case 's':
218                         src_addr = optarg;
219                         break;
220                 case 'u':
221                         family = AF_UNIX;
222                         break;
223                 case 'x':
224                         fprintf(stderr,
225                             "%s: Warning: -x ignored, no ENCRYPT support.\n",
226                                                                 prompt);
227                         break;
228                 case 'y':
229                         fprintf(stderr,
230                             "%s: Warning: -y ignored, no ENCRYPT support.\n",
231                                                                 prompt);
232                         break;
233 #if defined(IPSEC) && defined(IPSEC_POLICY_IPSEC)
234                 case 'P':
235                         if (!strncmp("in", optarg, 2))
236                                 ipsec_policy_in = strdup(optarg);
237                         else if (!strncmp("out", optarg, 3))
238                                 ipsec_policy_out = strdup(optarg);
239                         else
240                                 usage();
241                         break;
242 #endif
243                 case '?':
244                 default:
245                         usage();
246                         /* NOTREACHED */
247                 }
248         }
249         if (autologin == -1)
250                 autologin = (rlogin == _POSIX_VDISABLE) ? 0 : 1;
251
252         argc -= optind;
253         argv += optind;
254
255         if (argc) {
256                 char *args[9], **argp = args;
257
258                 if (argc > 2)
259                         usage();
260                 *argp++ = prompt;
261                 if (user) {
262                         *argp++ = strdup("-l");
263                         *argp++ = user;
264                 }
265                 if (src_addr) {
266                         *argp++ = strdup("-s");
267                         *argp++ = src_addr;
268                 }
269                 *argp++ = argv[0];              /* host */
270                 if (argc > 1)
271                         *argp++ = argv[1];      /* port */
272                 *argp = 0;
273
274                 if (setjmp(toplevel) != 0)
275                         Exit(0);
276                 if (tn(argp - args, args) == 1)
277                         return (0);
278                 else
279                         return (1);
280         }
281         (void)setjmp(toplevel);
282         for (;;) {
283                         command(1, 0, 0);
284         }
285         return 0;
286 }