Debian Codegraph @

telnet/main.c

raw
1 /*
2 Copyright (C) 1995-2026 Free Software Foundation, Inc.
3
4 This file is part of GNU Inetutils.
5
6 GNU Inetutils is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or (at
9 your option) any later version.
10
11 GNU Inetutils is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see `http://www.gnu.org/licenses/'. */
18
19 /*
20 * Copyright (c) 1988, 1990, 1993
21 * The Regents of the University of California. All rights reserved.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the above copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. Neither the name of the University nor the names of its contributors
32 * may be used to endorse or promote products derived from this software
33 * without specific prior written permission.
34 *
35 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
36 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
38 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
39 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
40 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
41 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
42 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
43 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
44 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
45 * SUCH DAMAGE.
46 */
47
48 #include <config.h>
49
50 #include <sys/types.h>
51
52 #include <stdlib.h>
53 #ifdef HAVE_LOCALE_H
54 # include <locale.h>
55 #endif
56
57 #include "ring.h"
58 #include "defines.h"
59 #include "externs.h"
60
61 #include <progname.h>
62 #include <error.h>
63 #include <argp.h>
64 #include <attribute.h>
65 #include <libinetutils.h>
66
67 #include <arpa/telnet.h>
68
69 #ifdef AUTHENTICATION
70 # include <libtelnet/auth.h>
71 #endif
72 #ifdef ENCRYPTION
73 # include <libtelnet/encrypt.h>
74 #endif
75
76 /* These values need to be the same as defined in libtelnet/kerberos5.c */
77 /* Either define them in both places, or put in some common header file. */
78 #define OPTS_FORWARD_CREDS 0x00000002
79 #define OPTS_FORWARDABLE_CREDS 0x00000001
80
81 #if 0
82 # define FORWARD
83 #endif
84
85 /*
86 * Initialize variables.
87 */
88 void
89 tninit (void)
90 {
91 init_terminal ();
92
93 init_network ();
94
95 init_telnet ();
96
97 init_sys ();
98
99 }
100
101 int family = 0;
102 char *user, *srcaddr;
103 #ifdef FORWARD
104 extern int forward_flags;
105 #endif /* FORWARD */
106
107 enum
108 {
109 OPTION_NOASYNCH = 256,
110 OPTION_NOASYNCTTY,
111 OPTION_NOASYNCNET
112 };
113
114 #if defined KERBEROS || defined SHISHI
115 extern char *dest_realm;
116 #endif
117
118 static struct argp_option argp_options[] = {
119 #define GRID 10
120 {NULL, 0, NULL, 0,
121 "General options:", GRID},
122
123 {"ipv4", '4', NULL, 0,
124 "use only IPv4", GRID + 1},
125 {"ipv6", '6', NULL, 0,
126 "use only IPv6", GRID + 1},
127 /* FIXME: Called "8bit" in r* utils */
128 {"binary", '8', NULL, 0,
129 "use an 8-bit data transmission", GRID + 1},
130 {"bind", 'b', "ADDRESS", 0,
131 "bind to specific local ADDRESS", GRID + 1},
132 {"login", 'a', NULL, 0,
133 "attempt automatic login", GRID + 1},
134 {"no-rc", 'c', NULL, 0,
135 "do not read the user's .telnetrc file", GRID + 1},
136 {"debug", 'd', NULL, 0,
137 "turn on debugging", GRID + 1},
138 {"escape", 'e', "CHAR", 0,
139 "use CHAR as an escape character", GRID + 1},
140 {"no-escape", 'E', NULL, 0,
141 "use no escape character", GRID + 1},
142 {"no-login", 'K', NULL, 0,
143 "do not automatically login to the remote system", GRID + 1},
144 {"user", 'l', "USER", 0,
145 "attempt automatic login as USER", GRID + 1},
146 {"binary-output", 'L', NULL, 0, /* FIXME: Why L?? */
147 "use an 8-bit data transmission for output only", GRID + 1},
148 {"trace", 'n', "FILE", 0,
149 "record trace information into FILE", GRID + 1},
150 {"rlogin", 'r', NULL, 0,
151 "use a user-interface similar to rlogin", GRID + 1},
152 #undef GRID
153
154 #ifdef ENCRYPTION
155 # define GRID 20
156 {NULL, 0, NULL, 0,
157 "Encryption control:", GRID},
158 {"encrypt", 'x', NULL, 0,
159 "encrypt the data stream, if possible", GRID + 1},
160 # undef GRID
161 #endif
162
163 #ifdef AUTHENTICATION
164 # define GRID 30
165 {NULL, 0, NULL, 0,
166 "Authentication and Kerberos options:", GRID},
167 {"disable-auth", 'X', "ATYPE", 0,
168 "disable type ATYPE authentication", GRID + 1},
169 # if defined KERBEROS || defined SHISHI
170 {"realm", 'k', "REALM", 0,
171 "obtain tickets for the remote host in REALM "
172 "instead of the remote host's realm", GRID + 1},
173 # endif
174 # if defined KRB5 && defined FORWARD
175 {"fwd-credentials", 'f', NULL, 0,
176 "allow the local credentials to be forwarded", GRID + 1},
177 {NULL, 'F', NULL, 0,
178 "forward a forwardable copy of the local credentials "
179 "to the remote system", GRID + 1},
180 # endif
181 # undef GRID
182 #endif
183 {NULL, 0, NULL, 0, NULL, 0}
184 };
185
186 static error_t
187 parse_opt (int key, char *arg, struct argp_state *state MAYBE_UNUSED)
188 {
189 switch (key)
190 {
191 case '4':
192 family = 4;
193 break;
194
195 case '6':
196 family = 6;
197 break;
198
199 case '8':
200 eight = 3; /* binary output and input */
201 break;
202
203 case 'E':
204 rlogin = escape = _POSIX_VDISABLE;
205 break;
206
207 case 'K':
208 #ifdef AUTHENTICATION
209 autologin = 0;
210 #endif
211 break;
212
213 case 'L':
214 eight |= 2; /* binary output only */
215 break;
216
217 #ifdef AUTHENTICATION
218 case 'X':
219 auth_disable_name (arg);
220 break;
221 #endif
222
223 case 'a':
224 autologin = 1;
225 break;
226
227 case 'c':
228 skiprc = 1;
229 break;
230
231 case 'd':
232 debug = 1;
233 break;
234
235 case 'e':
236 set_escape_char (arg);
237 break;
238
239 #if defined AUTHENTICATION && defined KRB5 && defined FORWARD
240 case 'f':
241 if (forward_flags & OPTS_FORWARD_CREDS)
242 argp_error (state, "Only one of -f and -F allowed.", prompt);
243 forward_flags |= OPTS_FORWARD_CREDS;
244 break;
245
246 case 'F':
247 if (forward_flags & OPTS_FORWARD_CREDS)
248 argp_error (state, "Only one of -f and -F allowed");
249 forward_flags |= OPTS_FORWARD_CREDS;
250 forward_flags |= OPTS_FORWARDABLE_CREDS;
251 break;
252 #endif
253
254 #if defined AUTHENTICATION && \
255 ( defined KERBEROS || defined SHISHI )
256 case 'k':
257 dest_realm = arg;
258 break;
259 #endif
260
261 case 'l':
262 autologin = 1;
263 user = arg;
264 break;
265
266 case 'n':
267 SetNetTrace (arg);
268 break;
269
270 case 'r':
271 rlogin = '~';
272 break;
273
274 #ifdef ENCRYPTION
275 case 'x':
276 encrypt_auto (1);
277 decrypt_auto (1);
278 break;
279 #endif
280
281 case 'b':
282 srcaddr = arg;
283 break;
284
285 default:
286 return ARGP_ERR_UNKNOWN;
287 }
288
289 return 0;
290 }
291
292
293 const char args_doc[] = "[HOST [PORT]]";
294 const char doc[] = "Login to remote system HOST "
295 "(optionally, on service port PORT)";
296 static struct argp argp =
297 { argp_options, parse_opt, args_doc, doc, NULL, NULL, NULL };
298
299
300
301 /*
302 * main. Parse arguments, invoke the protocol or command parser.
303 */
304 int
305 main (int argc, char *argv[])
306 {
307 int index;
308
309 set_program_name (argv[0]);
310
311 #ifdef HAVE_SETLOCALE
312 setlocale (LC_ALL, "");
313 #endif
314
315 tninit (); /* Clear out things */
316 #if defined CRAY && !defined __STDC__
317 _setlist_init (); /* Work around compiler bug */
318 #endif
319
320 TerminalSaveState ();
321
322 if ((prompt = strrchr (argv[0], '/')))
323 ++prompt;
324 else
325 prompt = argv[0];
326
327 user = NULL;
328
329 rlogin = (strncmp (prompt, "rlog", 4) == 0) ? '~' : _POSIX_VDISABLE;
330 autologin = -1;
331
332 /* Parse command line */
333 iu_argp_init ("telnet", default_program_authors);
334 argp_parse (&argp, argc, argv, 0, &index, NULL);
335
336 if (autologin == -1)
337 autologin = (rlogin == _POSIX_VDISABLE) ? 0 : 1;
338
339 argc -= index;
340 argv += index;
341
342 if (argc)
343 {
344 /* The command line contains at least one argument.
345 */
346 char *args[10], **argp = args;
347
348 if (argc > 2)
349 error (EXIT_FAILURE, 0, "too many arguments");
350 *argp++ = prompt;
351 if (user)
352 {
353 *argp++ = "-l";
354 *argp++ = user;
355 }
356 if (srcaddr)
357 {
358 *argp++ = "-b";
359 *argp++ = srcaddr;
360 }
361 if (family == 4)
362 *argp++ = "-4";
363 else if (family == 6)
364 *argp++ = "-6";
365
366 *argp++ = argv[0]; /* host */
367 if (argc > 1)
368 *argp++ = argv[1]; /* port */
369 *argp = 0;
370
371 if (setjmp (toplevel) != 0)
372 Exit (0);
373 if (tn (argp - args, args) == 1) /* Returns only on error. */
374 return (0);
375 else
376 return (1);
377 /* NOT REACHED */
378 }
379
380 /* Built-in parser loop; sub-commands jump to `toplevel' mark. */
381 setjmp (toplevel);
382 for (;;)
383 command (1, 0, 0);
384 /* NOT REACHED */
385 }