Debian Codegraph @

telnet/commands.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 #ifdef HAVE_SYS_PARAM_H
51 # include <sys/param.h>
52 #endif
53 #include <sys/types.h>
54 #include <sys/wait.h>
55 #include <sys/file.h>
56
57 #include <sys/socket.h>
58 #include <netinet/in.h>
59
60 #include <fcntl.h>
61 #include <unistd.h>
62
63 #include <signal.h>
64 #include <netdb.h>
65 #include <ctype.h>
66 #include <pwd.h>
67 #include <stdarg.h>
68 #include <errno.h>
69
70 #include <stdlib.h>
71 #include <string.h>
72 #include <limits.h> /* LLONG_MAX for Solaris. */
73
74 #include <arpa/inet.h>
75 #include <arpa/telnet.h>
76
77 #if defined HAVE_IDN2_H && defined HAVE_IDN2
78 # include <idn2.h>
79 #elif defined HAVE_IDNA_H
80 # include <idna.h>
81 #endif
82
83 #include <attribute.h>
84 #include <libinetutils.h>
85
86 #include "general.h"
87
88 #include "ring.h"
89
90 #include "externs.h"
91 #include "defines.h"
92 #include "types.h"
93
94 #include "xalloc.h"
95 #include "xstrtol.h"
96 #include "xvasprintf.h"
97 #include "xalloc.h"
98
99 #ifdef ENCRYPTION
100 # include <libtelnet/encrypt.h>
101 #endif
102 #if defined AUTHENTICATION || defined ENCRYPTION
103 # include <libtelnet/misc.h>
104 #endif
105
106 #if !defined CRAY && !defined sysV88
107 # ifdef HAVE_NETINET_IN_SYSTM_H
108 # include <netinet/in_systm.h>
109 # endif
110 # if (defined vax || defined tahoe || defined hp300) && !defined ultrix
111 # include <machine/endian.h>
112 # endif/* vax */
113 #endif /* !defined(CRAY) && !defined(sysV88) */
114
115 #ifdef HAVE_NETINET_IP_H
116 # include <netinet/ip.h>
117 #endif
118
119 char *hostname = NULL;
120
121 extern int isprefix (const char *s1, const char *s2);
122 extern char **genget (const char *, char **, int);
123 extern int Ambiguous (void *);
124
125 typedef int (*intrtn_t) (int, char *[]);
126 static int call (intrtn_t, ...);
127
128 typedef struct
129 {
130 const char *name; /* command name */
131 const char *help; /* help string (NULL for no help) */
132 int (*handler) (int, char *[]); /* routine which executes command */
133 int needconnect; /* Do we need to be connected to execute? */
134 } Command;
135
136 #define TELNET_MAX_ARGS 20
137
138 static char line[256];
139 static char saveline[256];
140 static int margc;
141 static char *margv[TELNET_MAX_ARGS];
142
143 static struct env_lst *env_help (const char *, char *);
144 static void slc_help (int);
145
146 static void
147 makeargv (void)
148 {
149 char *cp, *cp2, c;
150 char **argp = margv;
151
152 margc = 0;
153 cp = line;
154 if (*cp == '!')
155 { /* Special case shell escape */
156 strcpy (saveline, line); /* save for shell command */
157 *argp++ = "!"; /* No room in string to get this */
158 margc++;
159 cp++;
160 }
161 while ((c = *cp))
162 {
163 int inquote = 0;
164 while (isspace (c))
165 c = *++cp;
166 if (c == '\0')
167 break;
168 if (margc + 1 >= TELNET_MAX_ARGS)
169 {
170 fprintf (stderr, "Ignoring excess arguments.\n");
171 break;
172 }
173 *argp++ = cp;
174 margc += 1;
175 for (cp2 = cp; c != '\0'; c = *++cp)
176 {
177 if (inquote)
178 {
179 if (c == inquote)
180 {
181 inquote = 0;
182 continue;
183 }
184 }
185 else
186 {
187 if (c == '\\')
188 {
189 if ((c = *++cp) == '\0')
190 break;
191 }
192 else if (c == '"')
193 {
194 inquote = '"';
195 continue;
196 }
197 else if (c == '\'')
198 {
199 inquote = '\'';
200 continue;
201 }
202 else if (isspace (c))
203 break;
204 }
205 *cp2++ = c;
206 }
207 *cp2 = '\0';
208 if (c == '\0')
209 break;
210 cp++;
211 }
212 *argp++ = 0;
213 }
214
215 /*
216 * Make a character string into a number.
217 *
218 * Todo: 1. Could take random integers (12, 0x12, 012, 0b1).
219 */
220
221 static int
222 special (char *s)
223 {
224 char c;
225 char b;
226
227 switch (*s)
228 {
229 case '^':
230 b = *++s;
231 if (b == '?')
232 {
233 c = b | 0x40; /* DEL */
234 }
235 else
236 {
237 c = b & 0x1f;
238 }
239 break;
240 default:
241 c = *s;
242 break;
243 }
244 return c;
245 }
246
247 /*
248 * Construct a control character sequence
249 * for a special character.
250 */
251 static const char *
252 control (cc_t c)
253 {
254 static char buf[5];
255 /*
256 * The only way I could get the Sun 3.5 compiler
257 * to shut up about
258 * if ((unsigned int)c >= 0x80)
259 * was to assign "c" to an unsigned int variable...
260 * Arggg....
261 */
262 unsigned int uic = (unsigned int) c;
263
264 if (uic == 0x7f)
265 return ("^?");
266 if (c == (cc_t) _POSIX_VDISABLE)
267 {
268 return "off";
269 }
270 if (uic >= 0x80)
271 {
272 buf[0] = '\\';
273 buf[1] = ((c >> 6) & 07) + '0';
274 buf[2] = ((c >> 3) & 07) + '0';
275 buf[3] = (c & 07) + '0';
276 buf[4] = 0;
277 }
278 else if (uic >= 0x20)
279 {
280 buf[0] = c;
281 buf[1] = 0;
282 }
283 else
284 {
285 buf[0] = '^';
286 buf[1] = '@' + c;
287 buf[2] = 0;
288 }
289 return (buf);
290 }
291
292
293
294 /*
295 * The following are data structures and routines for
296 * the "send" command.
297 *
298 */
299
300 struct sendlist
301 {
302 const char *name; /* How user refers to it (case independent) */
303 const char *help; /* Help information (0 ==> no help) */
304 int needconnect; /* Need to be connected */
305 int narg; /* Number of arguments */
306 int (*handler) (const char *); /* Routine to perform (for special ops) */
307 int nbyte; /* Number of bytes to send this command */
308 int what; /* Character to be sent (<0 ==> special) */
309 };
310
311
312 static int send_esc (const char *);
313 static int send_docmd (const char *);
314 static int send_dontcmd (const char *);
315 static int send_willcmd (const char *);
316 static int send_wontcmd (const char *);
317 static int send_help (const char *);
318
319 static struct sendlist Sendlist[] = {
320 {"ao", "Send Telnet Abort output", 1, 0, NULL, 2, AO},
321 {"ayt", "Send Telnet 'Are You There'", 1, 0, NULL, 2, AYT},
322 {"brk", "Send Telnet Break", 1, 0, NULL, 2, BREAK},
323 {"break", NULL, 1, 0, NULL, 2, BREAK},
324 {"ec", "Send Telnet Erase Character", 1, 0, NULL, 2, EC},
325 {"el", "Send Telnet Erase Line", 1, 0, NULL, 2, EL},
326 {"escape", "Send current escape character", 1, 0, send_esc, 1, 0},
327 {"ga", "Send Telnet 'Go Ahead' sequence", 1, 0, NULL, 2, GA},
328 {"ip", "Send Telnet Interrupt Process", 1, 0, NULL, 2, IP},
329 {"intp", NULL, 1, 0, NULL, 2, IP},
330 {"interrupt", NULL, 1, 0, NULL, 2, IP},
331 {"intr", NULL, 1, 0, 0, 2, IP},
332 {"nop", "Send Telnet 'No operation'", 1, 0, NULL, 2, NOP},
333 {"eor", "Send Telnet 'End of Record'", 1, 0, NULL, 2, EOR},
334 {"abort", "Send Telnet 'Abort Process'", 1, 0, NULL, 2, ABORT},
335 {"susp", "Send Telnet 'Suspend Process'", 1, 0, NULL, 2, SUSP},
336 {"eof", "Send Telnet End of File Character", 1, 0, NULL, 2, xEOF},
337 {"synch", "Perform Telnet 'Synch operation'", 1, 0, dosynch, 2, 0},
338 {"getstatus", "Send request for STATUS", 1, 0, get_status, 6, 0},
339 {"?", "Display send options", 0, 0, send_help, 0, 0},
340 {"help", NULL, 0, 0, send_help, 0, 0},
341 {"do", NULL, 0, 1, send_docmd, 3, 0},
342 {"dont", NULL, 0, 1, send_dontcmd, 3, 0},
343 {"will", NULL, 0, 1, send_willcmd, 3, 0},
344 {"wont", NULL, 0, 1, send_wontcmd, 3, 0},
345 {NULL, NULL, 0, 0, NULL, 0, 0}
346 };
347
348 #define GETSEND(name) ((struct sendlist *) genget(name, (char **) Sendlist, \
349 sizeof(struct sendlist)))
350
351 static int
352 sendcmd (int argc, char **argv)
353 {
354 int count; /* how many bytes we are going to need to send */
355 int i;
356 struct sendlist *s; /* pointer to current command */
357 int success = 0;
358 int needconnect = 0;
359
360 if (argc < 2)
361 {
362 printf ("need at least one argument for 'send' command\n");
363 printf ("'send ?' for help\n");
364 return 0;
365 }
366 /*
367 * First, validate all the send arguments.
368 * In addition, we see how much space we are going to need, and
369 * whether or not we will be doing a "SYNCH" operation (which
370 * flushes the network queue).
371 */
372 count = 0;
373 for (i = 1; i < argc; i++)
374 {
375 s = GETSEND (argv[i]);
376 if (s == 0)
377 {
378 printf ("Unknown send argument '%s'\n'send ?' for help.\n",
379 argv[i]);
380 return 0;
381 }
382 else if (Ambiguous (s))
383 {
384 printf ("Ambiguous send argument '%s'\n'send ?' for help.\n",
385 argv[i]);
386 return 0;
387 }
388 if (i + s->narg >= argc)
389 {
390 fprintf (stderr,
391 "Need %d argument%s to 'send %s' command. 'send %s ?' for help.\n",
392 s->narg, s->narg == 1 ? "" : "s", s->name, s->name);
393 return 0;
394 }
395 count += s->nbyte;
396 if (s->handler == send_help)
397 {
398 send_help (NULL);
399 return 0;
400 }
401
402 i += s->narg;
403 needconnect += s->needconnect;
404 }
405 if (!connected && needconnect)
406 {
407 printf ("?Need to be connected first.\n");
408 printf ("'send ?' for help\n");
409 return 0;
410 }
411 /* Now, do we have enough room? */
412 if (NETROOM () < count)
413 {
414 printf ("There is not enough room in the buffer TO the network\n");
415 printf ("to process your request. Nothing will be done.\n");
416 printf ("('send synch' will throw away most data in the network\n");
417 printf ("buffer, if this might help.)\n");
418 return 0;
419 }
420 /* OK, they are all OK, now go through again and actually send */
421 count = 0;
422 for (i = 1; i < argc; i++)
423 {
424 if ((s = GETSEND (argv[i])) == 0)
425 {
426 fprintf (stderr, "Telnet 'send' error - argument disappeared!\n");
427 (void) quit (0, NULL);
428 /*NOTREACHED*/}
429 if (s->handler)
430 {
431 count++;
432 success += (*s->handler) ((s->narg > 0) ? argv[i + 1] : 0);
433 i += s->narg;
434 }
435 else
436 {
437 NET2ADD (IAC, s->what);
438 printoption ("SENT", IAC, s->what);
439 }
440 }
441 return (count == success);
442 }
443
444 static int
445 send_esc (const char *name)
446 {
447 NETADD (escape);
448 return 1;
449 }
450
451 int
452 send_tncmd (void (*func) (int, int), char *cmd, const char *name)
453 {
454 char **cpp;
455 #if !HAVE_DECL_TELOPTS
456 extern char *telopts[];
457 #endif
458 intmax_t value = 0;
459
460 if (isprefix (name, "help") || isprefix (name, "?"))
461 {
462 int col, len;
463
464 printf ("Usage: send %s <value|option>\n", cmd);
465 printf ("\"value\" must be from 0 to 255\n");
466 printf ("Valid options are:\n\t");
467
468 col = 8;
469 for (cpp = telopts; *cpp; cpp++)
470 {
471 len = strlen (*cpp) + 3;
472 if (col + len > 65)
473 {
474 printf ("\n\t");
475 col = 8;
476 }
477 printf (" \"%s\"", *cpp);
478 col += len;
479 }
480 printf ("\n");
481 return 0;
482 }
483 cpp = (char **) genget (name, telopts, sizeof (char *));
484 if (Ambiguous (cpp))
485 {
486 fprintf (stderr, "'%s': ambiguous argument ('send %s ?' for help).\n",
487 name, cmd);
488 return 0;
489 }
490 if (cpp)
491 {
492 #if HAVE_DECL_TELOPTS
493 value = cpp - (char **) telopts;
494 #else /* !HAVE_DECL_TELOPTS */
495 value = cpp - telopts;
496 #endif
497 }
498 else
499 {
500 const char *cp = name;
501 enum strtol_error err = xstrtoimax (cp, NULL, 10, &value, "");
502
503 if (err == LONGINT_OVERFLOW || value < 0 || value > 255)
504 {
505 fprintf (stderr, "'%s': bad value ('send %s ?' for help).\n",
506 name, cmd);
507 return 0;
508 }
509 else if (err != LONGINT_OK)
510 {
511 fprintf (stderr, "'%s': unknown argument ('send %s ?' for help).\n",
512 name, cmd);
513 return 0;
514 }
515 }
516 if (!connected)
517 {
518 printf ("?Need to be connected first.\n");
519 return 0;
520 }
521 (*func) (value, 1);
522 return 1;
523 }
524
525 static int
526 send_docmd (const char *name)
527 {
528 return (send_tncmd (send_do, "do", name));
529 }
530
531 static int
532 send_dontcmd (const char *name)
533 {
534 return (send_tncmd (send_dont, "dont", name));
535 }
536
537 static int
538 send_willcmd (const char *name)
539 {
540 return (send_tncmd (send_will, "will", name));
541 }
542
543 static int
544 send_wontcmd (const char *name)
545 {
546 return (send_tncmd (send_wont, "wont", name));
547 }
548
549 static int
550 send_help (const char *name)
551 {
552 struct sendlist *s; /* pointer to current command */
553 for (s = Sendlist; s->name; s++)
554 {
555 if (s->help)
556 printf ("%-15s %s\n", s->name, s->help);
557 }
558 return (0);
559 }
560
561 /*
562 * The following are the routines and data structures referred
563 * to by the arguments to the "toggle" command.
564 */
565
566 static int
567 lclchars (int val)
568 {
569 donelclchars = 1;
570 return 1;
571 }
572
573 static int
574 togdebug (int val)
575 {
576 #ifndef NOT43
577 if (net > 0 && (SetSockOpt (net, SOL_SOCKET, SO_DEBUG, debug)) < 0)
578 {
579 perror ("setsockopt (SO_DEBUG)");
580 }
581 #else /* NOT43 */
582 if (debug)
583 {
584 if (net > 0 && SetSockOpt (net, SOL_SOCKET, SO_DEBUG, 0, 0) < 0)
585 perror ("setsockopt (SO_DEBUG)");
586 }
587 else
588 printf ("Cannot turn off socket debugging\n");
589 #endif /* NOT43 */
590 return 1;
591 }
592
593
594 static int
595 togcrlf (int val)
596 {
597 if (crlf)
598 {
599 printf ("Will send carriage returns as telnet <CR><LF>.\n");
600 }
601 else
602 {
603 printf ("Will send carriage returns as telnet <CR><NUL>.\n");
604 }
605 return 1;
606 }
607
608 int binmode;
609
610 static int
611 togbinary (int val)
612 {
613 donebinarytoggle = 1;
614
615 if (val >= 0)
616 {
617 binmode = val;
618 }
619 else
620 {
621 if (my_want_state_is_will (TELOPT_BINARY) &&
622 my_want_state_is_do (TELOPT_BINARY))
623 {
624 binmode = 1;
625 }
626 else if (my_want_state_is_wont (TELOPT_BINARY) &&
627 my_want_state_is_dont (TELOPT_BINARY))
628 {
629 binmode = 0;
630 }
631 val = binmode ? 0 : 1;
632 }
633
634 if (val == 1)
635 {
636 if (my_want_state_is_will (TELOPT_BINARY) &&
637 my_want_state_is_do (TELOPT_BINARY))
638 {
639 printf ("Already operating in binary mode with remote host.\n");
640 }
641 else
642 {
643 printf ("Negotiating binary mode with remote host.\n");
644 tel_enter_binary (3);
645 }
646 }
647 else
648 {
649 if (my_want_state_is_wont (TELOPT_BINARY) &&
650 my_want_state_is_dont (TELOPT_BINARY))
651 {
652 printf ("Already in network ascii mode with remote host.\n");
653 }
654 else
655 {
656 printf ("Negotiating network ascii mode with remote host.\n");
657 tel_leave_binary (3);
658 }
659 }
660 return 1;
661 }
662
663 static int
664 togrbinary (int val)
665 {
666 donebinarytoggle = 1;
667
668 if (val == -1)
669 val = my_want_state_is_do (TELOPT_BINARY) ? 0 : 1;
670
671 if (val == 1)
672 {
673 if (my_want_state_is_do (TELOPT_BINARY))
674 {
675 printf ("Already receiving in binary mode.\n");
676 }
677 else
678 {
679 printf ("Negotiating binary mode on input.\n");
680 tel_enter_binary (1);
681 }
682 }
683 else
684 {
685 if (my_want_state_is_dont (TELOPT_BINARY))
686 {
687 printf ("Already receiving in network ascii mode.\n");
688 }
689 else
690 {
691 printf ("Negotiating network ascii mode on input.\n");
692 tel_leave_binary (1);
693 }
694 }
695 return 1;
696 }
697
698 static int
699 togxbinary (int val)
700 {
701 donebinarytoggle = 1;
702
703 if (val == -1)
704 val = my_want_state_is_will (TELOPT_BINARY) ? 0 : 1;
705
706 if (val == 1)
707 {
708 if (my_want_state_is_will (TELOPT_BINARY))
709 {
710 printf ("Already transmitting in binary mode.\n");
711 }
712 else
713 {
714 printf ("Negotiating binary mode on output.\n");
715 tel_enter_binary (2);
716 }
717 }
718 else
719 {
720 if (my_want_state_is_wont (TELOPT_BINARY))
721 {
722 printf ("Already transmitting in network ascii mode.\n");
723 }
724 else
725 {
726 printf ("Negotiating network ascii mode on output.\n");
727 tel_leave_binary (2);
728 }
729 }
730 return 1;
731 }
732
733
734 static int togglehelp (int);
735 #if defined AUTHENTICATION
736 extern int auth_togdebug (int);
737 #endif
738 #ifdef ENCRYPTION
739 extern int EncryptAutoEnc (int);
740 extern int EncryptAutoDec (int);
741 extern int EncryptDebug (int);
742 extern int EncryptVerbose (int);
743 #endif /* ENCRYPTION */
744
745 struct togglelist
746 {
747 const char *name; /* name of toggle */
748 const char *help; /* help message */
749 int (*handler) (int); /* routine to do actual setting */
750 int *variable;
751 const char *actionexplanation;
752 };
753
754 static struct togglelist Togglelist[] = {
755 {"autoflush",
756 "flushing of output when sending interrupt characters",
757 NULL,
758 &autoflush,
759 "flush output when sending interrupt characters"},
760 {"autosynch",
761 "automatic sending of interrupt characters in urgent mode",
762 NULL,
763 &autosynch,
764 "send interrupt characters in urgent mode"},
765 #if defined AUTHENTICATION
766 {"autologin",
767 "automatic sending of login and/or authentication info",
768 NULL,
769 &autologin,
770 "send login name and/or authentication information"},
771 {"authdebug",
772 "Toggle authentication debugging",
773 auth_togdebug,
774 NULL,
775 "print authentication debugging information"},
776 #endif
777 #ifdef ENCRYPTION
778 {"autoencrypt",
779 "automatic encryption of data stream",
780 EncryptAutoEnc,
781 NULL,
782 "automatically encrypt output"},
783 {"autodecrypt",
784 "automatic decryption of data stream",
785 EncryptAutoDec,
786 NULL,
787 "automatically decrypt input"},
788 {"verbose_encrypt",
789 "Toggle verbose encryption output",
790 EncryptVerbose,
791 NULL,
792 "print verbose encryption output"},
793 {"encdebug",
794 "Toggle encryption debugging",
795 EncryptDebug,
796 NULL,
797 "print encryption debugging information"},
798 #endif /* ENCRYPTION */
799 {"skiprc",
800 "don't read ~/.telnetrc file",
801 NULL,
802 &skiprc,
803 "skip reading of ~/.telnetrc file"},
804 {"binary",
805 "sending and receiving of binary data",
806 togbinary,
807 NULL,
808 NULL},
809 {"inbinary",
810 "receiving of binary data",
811 togrbinary,
812 NULL,
813 NULL},
814 {"outbinary",
815 "sending of binary data",
816 togxbinary,
817 NULL,
818 NULL},
819 {"crlf",
820 "sending carriage returns as telnet <CR><LF>",
821 togcrlf,
822 &crlf,
823 NULL},
824 {"crmod",
825 "mapping of received carriage returns",
826 NULL,
827 &crmod,
828 "map carriage return on output"},
829 {"localchars",
830 "local recognition of certain control characters",
831 lclchars,
832 &localchars,
833 "recognize certain control characters"},
834 {" ", "", NULL, NULL, NULL}, /* empty line */
835 {"debug",
836 "debugging",
837 togdebug,
838 &debug,
839 "turn on socket level debugging"},
840 {"netdata",
841 "printing of hexadecimal network data (debugging)",
842 NULL,
843 &netdata,
844 "print hexadecimal representation of network traffic"},
845 {"prettydump",
846 "output of \"netdata\" to user readable format (debugging)",
847 NULL,
848 &prettydump,
849 "print user readable output for \"netdata\""},
850 {"options",
851 "viewing of options processing (debugging)",
852 NULL,
853 &showoptions,
854 "show option processing"},
855 {"termdata",
856 "(debugging) toggle printing of hexadecimal terminal data",
857 NULL,
858 &termdata,
859 "print hexadecimal representation of terminal traffic"},
860 {"?",
861 NULL,
862 togglehelp, NULL, NULL},
863 {"help",
864 NULL,
865 togglehelp, NULL, NULL},
866 {NULL, NULL, NULL, NULL, NULL}
867 };
868
869 static int
870 togglehelp (int val)
871 {
872 struct togglelist *c;
873
874 for (c = Togglelist; c->name; c++)
875 {
876 if (c->help)
877 {
878 if (*c->help)
879 printf ("%-15s toggle %s\n", c->name, c->help);
880 else
881 printf ("\n");
882 }
883 }
884 printf ("\n");
885 printf ("%-15s %s\n", "?", "display help information");
886 return 0;
887 }
888
889 static void
890 settogglehelp (int set)
891 {
892 struct togglelist *c;
893
894 for (c = Togglelist; c->name; c++)
895 {
896 if (c->help)
897 {
898 if (*c->help)
899 printf ("%-15s %s %s\n", c->name, set ? "enable" : "disable",
900 c->help);
901 else
902 printf ("\n");
903 }
904 }
905 }
906
907 #define GETTOGGLE(name) (struct togglelist *) \
908 genget(name, (char **) Togglelist, sizeof(struct togglelist))
909
910 static int
911 toggle (int argc, char *argv[])
912 {
913 int retval = 1;
914 char *name;
915 struct togglelist *c;
916
917 if (argc < 2)
918 {
919 fprintf (stderr,
920 "Need an argument to 'toggle' command. 'toggle ?' for help.\n");
921 return 0;
922 }
923 argc--;
924 argv++;
925 while (argc--)
926 {
927 name = *argv++;
928 c = GETTOGGLE (name);
929 if (Ambiguous (c))
930 {
931 fprintf (stderr,
932 "'%s': ambiguous argument ('toggle ?' for help).\n", name);
933 return 0;
934 }
935 else if (c == 0)
936 {
937 fprintf (stderr, "'%s': unknown argument ('toggle ?' for help).\n",
938 name);
939 return 0;
940 }
941 else
942 {
943 if (c->variable)
944 {
945 *c->variable = !*c->variable; /* invert it */
946 if (c->actionexplanation)
947 {
948 printf ("%s %s.\n", *c->variable ? "Will" : "Won't",
949 c->actionexplanation);
950 }
951 }
952 if (c->handler)
953 {
954 retval &= (*c->handler) (-1);
955 }
956 }
957 }
958 return retval;
959 }
960
961 /*
962 * The following perform the "set" command.
963 */
964
965 #ifdef USE_TERMIO
966 struct termio new_tc = { 0 };
967 #endif
968
969 struct setlist
970 {
971 const char *name; /* name */
972 const char *help; /* help information */
973 void (*handler) (const char *);
974 cc_t *charp; /* where it is located at */
975 };
976
977 static struct setlist Setlist[] = {
978 #ifdef KLUDGELINEMODE
979 {"echo", "character to toggle local echoing on/off", NULL, &echoc},
980 #endif
981 {"escape", "character to escape back to telnet command mode", NULL,
982 &escape},
983 {"rlogin", "rlogin escape character", NULL, &rlogin},
984 {"tracefile", "file to write trace information to", SetNetTrace,
985 (cc_t *) NetTraceFile},
986 {" ", "", NULL, NULL},
987 {" ", "The following need 'localchars' to be toggled true", NULL, NULL},
988 {"flushoutput", "character to cause an Abort Output", NULL, termFlushCharp},
989 {"interrupt", "character to cause an Interrupt Process", NULL,
990 termIntCharp},
991 {"quit", "character to cause an Abort process", NULL, termQuitCharp},
992 {"eof", "character to cause an EOF ", NULL, termEofCharp},
993 {" ", "", NULL, NULL},
994 {" ", "The following are for local editing in linemode", NULL, NULL},
995 {"erase", "character to use to erase a character", NULL, termEraseCharp},
996 {"kill", "character to use to erase a line", NULL, termKillCharp},
997 {"lnext", "character to use for literal next", NULL, termLiteralNextCharp},
998 {"susp", "character to cause a Suspend Process", NULL, termSuspCharp},
999 {"reprint", "character to use for line reprint", NULL, termRprntCharp},
1000 {"worderase", "character to use to erase a word", NULL, termWerasCharp},
1001 {"start", "character to use for XON", NULL, termStartCharp},
1002 {"stop", "character to use for XOFF", NULL, termStopCharp},
1003 {"forw1", "alternate end of line character", NULL, termForw1Charp},
1004 {"forw2", "alternate end of line character", NULL, termForw2Charp},
1005 {"ayt", "alternate AYT character", NULL, termAytCharp},
1006 {NULL, NULL, NULL, NULL}
1007 };
1008
1009 #if defined CRAY && !defined __STDC__
1010 /* Work around compiler bug in pcc 4.1.5 */
1011 void
1012 _setlist_init (void)
1013 {
1014 # ifndef KLUDGELINEMODE
1015 # define N 5
1016 # else
1017 # define N 6
1018 # endif
1019 Setlist[N + 0].charp = &termFlushChar;
1020 Setlist[N + 1].charp = &termIntChar;
1021 Setlist[N + 2].charp = &termQuitChar;
1022 Setlist[N + 3].charp = &termEofChar;
1023 Setlist[N + 6].charp = &termEraseChar;
1024 Setlist[N + 7].charp = &termKillChar;
1025 Setlist[N + 8].charp = &termLiteralNextChar;
1026 Setlist[N + 9].charp = &termSuspChar;
1027 Setlist[N + 10].charp = &termRprntChar;
1028 Setlist[N + 11].charp = &termWerasChar;
1029 Setlist[N + 12].charp = &termStartChar;
1030 Setlist[N + 13].charp = &termStopChar;
1031 Setlist[N + 14].charp = &termForw1Char;
1032 Setlist[N + 15].charp = &termForw2Char;
1033 Setlist[N + 16].charp = &termAytChar;
1034 # undef N
1035 }
1036 #endif /* defined(CRAY) && !defined(__STDC__) */
1037
1038 static struct setlist *
1039 getset (char *name)
1040 {
1041 return (struct setlist *)
1042 genget (name, (char **) Setlist, sizeof (struct setlist));
1043 }
1044
1045 void
1046 set_escape_char (char *s)
1047 {
1048 if (rlogin != _POSIX_VDISABLE)
1049 {
1050 rlogin = (s && *s) ? special (s) : _POSIX_VDISABLE;
1051 printf ("Telnet rlogin escape character is '%s'.\n", control (rlogin));
1052 }
1053 else
1054 {
1055 escape = (s && *s) ? special (s) : _POSIX_VDISABLE;
1056 printf ("Telnet escape character is '%s'.\n", control (escape));
1057 }
1058 }
1059
1060 static int
1061 setcmd (int argc, char *argv[])
1062 {
1063 int value;
1064 struct setlist *ct;
1065 struct togglelist *c;
1066
1067 if (argc < 2 || argc > 3)
1068 {
1069 printf ("Format is 'set Name Value'\n'set ?' for help.\n");
1070 return 0;
1071 }
1072 if ((argc == 2) && (isprefix (argv[1], "?") || isprefix (argv[1], "help")))
1073 {
1074 for (ct = Setlist; ct->name; ct++)
1075 printf ("%-15s %s\n", ct->name, ct->help);
1076 printf ("\n");
1077 settogglehelp (1);
1078 printf ("%-15s %s\n", "?", "display help information");
1079 return 0;
1080 }
1081
1082 ct = getset (argv[1]);
1083 if (ct == 0)
1084 {
1085 c = GETTOGGLE (argv[1]);
1086 if (c == 0)
1087 {
1088 fprintf (stderr, "'%s': unknown argument ('set ?' for help).\n",
1089 argv[1]);
1090 return 0;
1091 }
1092 else if (Ambiguous (c))
1093 {
1094 fprintf (stderr, "'%s': ambiguous argument ('set ?' for help).\n",
1095 argv[1]);
1096 return 0;
1097 }
1098 if (c->variable)
1099 {
1100 if ((argc == 2) || (strcmp ("on", argv[2]) == 0))
1101 *c->variable = 1;
1102 else if (strcmp ("off", argv[2]) == 0)
1103 *c->variable = 0;
1104 else
1105 {
1106 printf
1107 ("Format is 'set togglename [on|off]'\n'set ?' for help.\n");
1108 return 0;
1109 }
1110 if (c->actionexplanation)
1111 {
1112 printf ("%s %s.\n", *c->variable ? "Will" : "Won't",
1113 c->actionexplanation);
1114 }
1115 }
1116 if (c->handler)
1117 (*c->handler) (1);
1118 }
1119 else if (argc != 3)
1120 {
1121 printf ("Format is 'set Name Value'\n'set ?' for help.\n");
1122 return 0;
1123 }
1124 else if (Ambiguous (ct))
1125 {
1126 fprintf (stderr, "'%s': ambiguous argument ('set ?' for help).\n",
1127 argv[1]);
1128 return 0;
1129 }
1130 else if (ct->handler)
1131 {
1132 (*ct->handler) (argv[2]);
1133 printf ("%s set to \"%s\".\n", ct->name, (char *) ct->charp);
1134 }
1135 else if (!ct->charp)
1136 {
1137 fprintf (stderr, "'%s': invalid argument ('set ?' for help).\n",
1138 argv[1]);
1139 }
1140 else
1141 {
1142 if (strcmp ("off", argv[2]))
1143 {
1144 value = special (argv[2]);
1145 }
1146 else
1147 {
1148 value = _POSIX_VDISABLE;
1149 }
1150 *(ct->charp) = (cc_t) value;
1151 printf ("%s character is '%s'.\n", ct->name, control (*(ct->charp)));
1152 }
1153 slc_check ();
1154 return 1;
1155 }
1156
1157 static int
1158 unsetcmd (int argc, char *argv[])
1159 {
1160 struct setlist *ct;
1161 struct togglelist *c;
1162 char *name;
1163
1164 if (argc < 2)
1165 {
1166 fprintf (stderr,
1167 "Need an argument to 'unset' command. 'unset ?' for help.\n");
1168 return 0;
1169 }
1170 if (isprefix (argv[1], "?") || isprefix (argv[1], "help"))
1171 {
1172 for (ct = Setlist; ct->name; ct++)
1173 printf ("%-15s %s\n", ct->name, ct->help);
1174 printf ("\n");
1175 settogglehelp (0);
1176 printf ("%-15s %s\n", "?", "display help information");
1177 return 0;
1178 }
1179
1180 argc--;
1181 argv++;
1182 while (argc--)
1183 {
1184 name = *argv++;
1185 ct = getset (name);
1186 if (ct == 0)
1187 {
1188 c = GETTOGGLE (name);
1189 if (c == 0)
1190 {
1191 fprintf (stderr,
1192 "'%s': unknown argument ('unset ?' for help).\n",
1193 name);
1194 return 0;
1195 }
1196 else if (Ambiguous (c))
1197 {
1198 fprintf (stderr,
1199 "'%s': ambiguous argument ('unset ?' for help).\n",
1200 name);
1201 return 0;
1202 }
1203 if (c->variable)
1204 {
1205 *c->variable = 0;
1206 if (c->actionexplanation)
1207 {
1208 printf ("%s %s.\n", *c->variable ? "Will" : "Won't",
1209 c->actionexplanation);
1210 }
1211 }
1212 if (c->handler)
1213 (*c->handler) (0);
1214 }
1215 else if (Ambiguous (ct))
1216 {
1217 fprintf (stderr, "'%s': ambiguous argument ('unset ?' for help).\n",
1218 name);
1219 return 0;
1220 }
1221 else if (ct->handler)
1222 {
1223 (*ct->handler) (0);
1224 printf ("%s reset to \"%s\".\n", ct->name, (char *) ct->charp);
1225 }
1226 else if (!ct->charp)
1227 {
1228 fprintf (stderr, "'%s': invalid argument ('unset ?' for help).\n",
1229 name);
1230 return 0;
1231 }
1232 else
1233 {
1234 *(ct->charp) = _POSIX_VDISABLE;
1235 printf ("%s character is '%s'.\n", ct->name,
1236 control (*(ct->charp)));
1237 }
1238 }
1239 return 1;
1240 }
1241
1242 /*
1243 * The following are the data structures and routines for the
1244 * 'mode' command.
1245 */
1246 #ifdef KLUDGELINEMODE
1247 extern int kludgelinemode;
1248
1249 static int
1250 dokludgemode (int val)
1251 {
1252 kludgelinemode = 1;
1253 send_wont (TELOPT_LINEMODE, 1);
1254 send_dont (TELOPT_SGA, 1);
1255 send_dont (TELOPT_ECHO, 1);
1256 return 0;
1257 }
1258 #endif
1259
1260 static int
1261 dolinemode (int val)
1262 {
1263 #ifdef KLUDGELINEMODE
1264 if (kludgelinemode)
1265 send_dont (TELOPT_SGA, 1);
1266 #endif
1267 send_will (TELOPT_LINEMODE, 1);
1268 send_dont (TELOPT_ECHO, 1);
1269 return 1;
1270 }
1271
1272 static int
1273 docharmode (int val)
1274 {
1275 #ifdef KLUDGELINEMODE
1276 if (kludgelinemode)
1277 send_do (TELOPT_SGA, 1);
1278 else
1279 #endif
1280 send_wont (TELOPT_LINEMODE, 1);
1281 send_do (TELOPT_ECHO, 1);
1282 return 1;
1283 }
1284
1285 static int
1286 dolmmode (int bit, int on)
1287 {
1288 unsigned char c;
1289 extern int linemode;
1290
1291 if (my_want_state_is_wont (TELOPT_LINEMODE))
1292 {
1293 printf ("?Need to have LINEMODE option enabled first.\n");
1294 printf ("'mode ?' for help.\n");
1295 return 0;
1296 }
1297
1298 if (on)
1299 c = (linemode | bit);
1300 else
1301 c = (linemode & ~bit);
1302 lm_mode (&c, 1, 1);
1303 return 1;
1304 }
1305
1306 int
1307 set_mode (int bit)
1308 {
1309 return dolmmode (bit, 1);
1310 }
1311
1312 int
1313 clear_mode (int bit)
1314 {
1315 return dolmmode (bit, 0);
1316 }
1317
1318 struct modelist
1319 {
1320 const char *name; /* command name */
1321 const char *help; /* help string */
1322 int (*handler) (int); /* routine which executes command */
1323 int needconnect; /* Do we need to be connected to execute? */
1324 int arg1;
1325 };
1326
1327 extern int modehelp (int val);
1328
1329 static struct modelist ModeList[] = {
1330 {"character", "Disable LINEMODE option", docharmode, 1, 0},
1331 #ifdef KLUDGELINEMODE
1332 {"", "(or disable obsolete line-by-line mode)", NULL, 0, 0},
1333 #endif
1334 {"line", "Enable LINEMODE option", dolinemode, 1, 0},
1335 #ifdef KLUDGELINEMODE
1336 {"", "(or enable obsolete line-by-line mode)", NULL, 0, 0},
1337 #endif
1338 {"", "", NULL, 0, 0},
1339 {"", "These require the LINEMODE option to be enabled", NULL, 0, 0},
1340 {"isig", "Enable signal trapping", set_mode, 1, MODE_TRAPSIG},
1341 {"+isig", NULL, set_mode, 1, MODE_TRAPSIG},
1342 {"-isig", "Disable signal trapping", clear_mode, 1, MODE_TRAPSIG},
1343 {"edit", "Enable character editing", set_mode, 1, MODE_EDIT},
1344 {"+edit", NULL, set_mode, 1, MODE_EDIT},
1345 {"-edit", "Disable character editing", clear_mode, 1, MODE_EDIT},
1346 {"softtabs", "Enable tab expansion", set_mode, 1, MODE_SOFT_TAB},
1347 {"+softtabs", NULL, set_mode, 1, MODE_SOFT_TAB},
1348 {"-softtabs", "Disable character editing", clear_mode, 1, MODE_SOFT_TAB},
1349 {"litecho", "Enable literal character echo", set_mode, 1, MODE_LIT_ECHO},
1350 {"+litecho", NULL, set_mode, 1, MODE_LIT_ECHO},
1351 {"-litecho", "Disable literal character echo", clear_mode, 1,
1352 MODE_LIT_ECHO},
1353 {"help", NULL, modehelp, 0, 0},
1354 #ifdef KLUDGELINEMODE
1355 {"kludgeline", NULL, dokludgemode, 1, 0},
1356 #endif
1357 {"", "", NULL, 0, 0},
1358 {"?", "Print help information", modehelp, 0, 0},
1359 {NULL, NULL, NULL, 0, 0},
1360 };
1361
1362
1363 int
1364 modehelp (int val)
1365 {
1366 struct modelist *mt;
1367
1368 printf ("format is: 'mode Mode', where 'Mode' is one of:\n\n");
1369 for (mt = ModeList; mt->name; mt++)
1370 {
1371 if (mt->help)
1372 {
1373 if (*mt->help)
1374 printf ("%-15s %s\n", mt->name, mt->help);
1375 else
1376 printf ("\n");
1377 }
1378 }
1379 return 0;
1380 }
1381
1382 #define GETMODECMD(name) (struct modelist *) \
1383 genget(name, (char **) ModeList, sizeof(struct modelist))
1384
1385 static int
1386 modecmd (int argc, char *argv[])
1387 {
1388 struct modelist *mt;
1389
1390 if (argc != 2)
1391 {
1392 printf ("'mode' command requires an argument\n");
1393 printf ("'mode ?' for help.\n");
1394 }
1395 else if ((mt = GETMODECMD (argv[1])) == 0)
1396 {
1397 fprintf (stderr, "Unknown mode '%s' ('mode ?' for help).\n", argv[1]);
1398 }
1399 else if (Ambiguous (mt))
1400 {
1401 fprintf (stderr, "Ambiguous mode '%s' ('mode ?' for help).\n", argv[1]);
1402 }
1403 else if (mt->needconnect && !connected)
1404 {
1405 printf ("?Need to be connected first.\n");
1406 printf ("'mode ?' for help.\n");
1407 }
1408 else if (mt->handler)
1409 {
1410 return (*mt->handler) (mt->arg1);
1411 }
1412 return 0;
1413 }
1414
1415 /*
1416 * The following data structures and routines implement the
1417 * "display" command.
1418 */
1419
1420 static int
1421 display (int argc, char *argv[])
1422 {
1423 struct togglelist *tl;
1424 struct setlist *sl;
1425
1426 #define dotog(tl) if (tl->variable && tl->actionexplanation) { \
1427 if (*tl->variable) { \
1428 printf("will"); \
1429 } else { \
1430 printf("won't"); \
1431 } \
1432 printf(" %s.\n", tl->actionexplanation); \
1433 }
1434
1435 #define doset(sl) if (sl->name && *sl->name != ' ') { \
1436 if (sl->handler == 0) \
1437 printf("%-15s [%s]\n", sl->name, control(*sl->charp)); \
1438 else \
1439 printf("%-15s \"%s\"\n", sl->name, (char *)sl->charp); \
1440 }
1441
1442 if (argc == 1)
1443 {
1444 for (tl = Togglelist; tl->name; tl++)
1445 {
1446 dotog (tl);
1447 }
1448 printf ("\n");
1449 for (sl = Setlist; sl->name; sl++)
1450 {
1451 doset (sl);
1452 }
1453 }
1454 else
1455 {
1456 int i;
1457
1458 for (i = 1; i < argc; i++)
1459 {
1460 sl = getset (argv[i]);
1461 tl = GETTOGGLE (argv[i]);
1462 if (Ambiguous (sl) || Ambiguous (tl))
1463 {
1464 printf ("?Ambiguous argument '%s'.\n", argv[i]);
1465 return 0;
1466 }
1467 else if (!sl && !tl)
1468 {
1469 printf ("?Unknown argument '%s'.\n", argv[i]);
1470 return 0;
1471 }
1472 else
1473 {
1474 if (tl)
1475 {
1476 dotog (tl);
1477 }
1478 if (sl)
1479 {
1480 doset (sl);
1481 }
1482 }
1483 }
1484 }
1485 /*@*/ optionstatus ();
1486 #ifdef ENCRYPTION
1487 EncryptStatus ();
1488 #endif /* ENCRYPTION */
1489 return 1;
1490 #undef doset
1491 #undef dotog
1492 }
1493
1494 /*
1495 * The following are the data structures, and many of the routines,
1496 * relating to command processing.
1497 */
1498
1499 /*
1500 * Set the escape character.
1501 */
1502 static int
1503 setescape (int argc, char *argv[])
1504 {
1505 char *arg;
1506 char buf[50];
1507
1508 printf ("Deprecated usage - please use 'set escape%s%s' in the future.\n",
1509 (argc > 2) ? " " : "", (argc > 2) ? argv[1] : "");
1510 if (argc > 2)
1511 arg = argv[1];
1512 else
1513 {
1514 printf ("new escape character: ");
1515 if (fgets (buf, sizeof (buf), stdin) == NULL)
1516 {
1517 buf[0] = '\0';
1518 printf ("\n");
1519 }
1520 arg = buf;
1521 }
1522 if (arg[0] != '\0')
1523 escape = arg[0];
1524 printf ("Escape character is '%s'.\n", control (escape));
1525 fflush (stdout);
1526 return 1;
1527 }
1528
1529 static int
1530 togcrmod (int argc, char *argv[])
1531 {
1532 crmod = !crmod;
1533 printf ("Deprecated usage - please use 'toggle crmod' in the future.\n");
1534 printf ("%s map carriage return on output.\n", crmod ? "Will" : "Won't");
1535 fflush (stdout);
1536 return 1;
1537 }
1538
1539 int
1540 suspend (int argc, char *argv[])
1541 {
1542 #ifdef SIGTSTP
1543 setcommandmode ();
1544 {
1545 long oldrows, oldcols, newrows, newcols, err;
1546
1547 err = (TerminalWindowSize (&oldrows, &oldcols) == 0) ? 1 : 0;
1548 kill (0, SIGTSTP);
1549 /*
1550 * If we didn't get the window size before the SUSPEND, but we
1551 * can get them now (?), then send the NAWS to make sure that
1552 * we are set up for the right window size.
1553 */
1554 if (TerminalWindowSize (&newrows, &newcols) && connected &&
1555 (err || ((oldrows != newrows) || (oldcols != newcols))))
1556 {
1557 sendnaws ();
1558 }
1559 }
1560 /* reget parameters in case they were changed */
1561 TerminalSaveState ();
1562 setconnmode (0);
1563 #else
1564 printf ("Suspend is not supported. Try the '!' command instead\n");
1565 #endif
1566 return 1;
1567 }
1568
1569 int
1570 shell (int argc, MAYBE_UNUSED char *argv[])
1571 {
1572 long oldrows, oldcols, newrows, newcols, err;
1573
1574 setcommandmode ();
1575
1576 err = (TerminalWindowSize (&oldrows, &oldcols) == 0) ? 1 : 0;
1577 switch (vfork ())
1578 {
1579 case -1:
1580 perror ("Fork failed\n");
1581 break;
1582
1583 case 0:
1584 {
1585 /*
1586 * Fire up the shell in the child.
1587 */
1588 char *shellp, *shellname;
1589
1590 shellp = getenv ("SHELL");
1591 if (shellp == NULL)
1592 shellp = PATH_BSHELL;
1593 shellname = strrchr (shellp, '/');
1594 if (shellname == NULL)
1595 shellname = shellp;
1596 else
1597 shellname++;
1598 if (argc > 1)
1599 execl (shellp, shellname, "-c", &saveline[1], NULL);
1600 else
1601 execl (shellp, shellname, NULL);
1602 perror ("Execl");
1603 _exit (EXIT_FAILURE);
1604 }
1605 default:
1606 wait ((int *) 0); /* Wait for the shell to complete */
1607
1608 if (TerminalWindowSize (&newrows, &newcols) && connected &&
1609 (err || ((oldrows != newrows) || (oldcols != newcols))))
1610 {
1611 sendnaws ();
1612 }
1613 break;
1614 }
1615 return 1;
1616 }
1617
1618 /* int argc; Number of arguments */
1619 /* char *argv[]; arguments */
1620 static int
1621 bye (int argc, char *argv[])
1622 {
1623 extern int resettermname;
1624
1625 if (connected)
1626 {
1627 shutdown (net, 2);
1628 printf ("Connection closed.\n");
1629 NetClose (net);
1630 connected = 0;
1631 resettermname = 1;
1632 #if defined AUTHENTICATION || defined ENCRYPTION
1633 auth_encrypt_connect (connected);
1634 #endif /* defined(AUTHENTICATION) || defined(ENCRYPTION) */
1635 /* reset options */
1636 tninit ();
1637 }
1638 if ((argc != 2) || (strcmp (argv[1], "fromquit") != 0))
1639 {
1640 longjmp (toplevel, 1);
1641 }
1642 return 1;
1643 }
1644
1645 int
1646 quit (int argc, char *argv[])
1647 {
1648 call (bye, "bye", "fromquit", 0);
1649 Exit (0);
1650 return 0;
1651 }
1652
1653 int
1654 logoutcmd (int argc, char *argv[])
1655 {
1656 send_do (TELOPT_LOGOUT, 1);
1657 netflush ();
1658 return 1;
1659 }
1660
1661
1662 /*
1663 * The SLC command.
1664 */
1665
1666 struct slclist
1667 {
1668 const char *name;
1669 const char *help;
1670 void (*handler) (int);
1671 int arg;
1672 };
1673
1674 struct slclist SlcList[] = {
1675 {"export", "Use local special character definitions",
1676 slc_mode_export, 0},
1677 {"import", "Use remote special character definitions",
1678 slc_mode_import, 1},
1679 {"check", "Verify remote special character definitions",
1680 slc_mode_import, 0},
1681 {"help", NULL, slc_help, 0},
1682 {"?", "Print help information", slc_help, 0},
1683 {NULL, NULL, NULL, 0},
1684 };
1685
1686 static void
1687 slc_help (int n)
1688 {
1689 struct slclist *c;
1690
1691 for (c = SlcList; c->name; c++)
1692 {
1693 if (c->help)
1694 {
1695 if (*c->help)
1696 printf ("%-15s %s\n", c->name, c->help);
1697 else
1698 printf ("\n");
1699 }
1700 }
1701 }
1702
1703 static struct slclist *
1704 getslc (char *name)
1705 {
1706 return (struct slclist *)
1707 genget (name, (char **) SlcList, sizeof (struct slclist));
1708 }
1709
1710 static int
1711 slccmd (int argc, char **argv)
1712 {
1713 struct slclist *c;
1714
1715 if (argc != 2)
1716 {
1717 fprintf (stderr,
1718 "Need an argument to 'slc' command. 'slc ?' for help.\n");
1719 return 0;
1720 }
1721 c = getslc (argv[1]);
1722 if (c == 0)
1723 {
1724 fprintf (stderr, "'%s': unknown argument ('slc ?' for help).\n",
1725 argv[1]);
1726 return 0;
1727 }
1728 if (Ambiguous (c))
1729 {
1730 fprintf (stderr, "'%s': ambiguous argument ('slc ?' for help).\n",
1731 argv[1]);
1732 return 0;
1733 }
1734 (*c->handler) (c->arg);
1735 slcstate ();
1736 return 1;
1737 }
1738
1739 /*
1740 * The ENVIRON command.
1741 */
1742
1743 struct envlist
1744 {
1745 const char *name;
1746 const char *help;
1747 struct env_lst *(*handler) (const char *, char *);
1748 int narg;
1749 };
1750
1751 struct envlist EnvList[] = {
1752 {"define", "Define an environment variable",
1753 env_define, 2},
1754 {"undefine", "Undefine an environment variable",
1755 env_undefine, 1},
1756 {"export", "Mark an environment variable for automatic export",
1757 env_export, 1},
1758 {"unexport", "Don't mark an environment variable for automatic export",
1759 env_unexport, 1},
1760 {"send", "Send an environment variable", env_send, 1},
1761 {"list", "List the current environment variables",
1762 env_list, 0},
1763 #if defined OLD_ENVIRON && defined ENV_HACK
1764 {"varval", "Reverse VAR and VALUE (auto, right, wrong, status)",
1765 env_varval, 1},
1766 #endif
1767 {"help", NULL, env_help, 0},
1768 {"?", "Print help information", env_help, 0},
1769 {NULL, NULL, NULL, 0},
1770 };
1771
1772 static struct env_lst *
1773 env_help (const char *us1, char *us2)
1774 {
1775 struct envlist *c;
1776
1777 for (c = EnvList; c->name; c++)
1778 {
1779 if (c->help)
1780 {
1781 if (*c->help)
1782 printf ("%-15s %s\n", c->name, c->help);
1783 else
1784 printf ("\n");
1785 }
1786 }
1787 return NULL;
1788 }
1789
1790 static struct envlist *
1791 getenvcmd (char *name)
1792 {
1793 return (struct envlist *)
1794 genget (name, (char **) EnvList, sizeof (struct envlist));
1795 }
1796
1797 int
1798 env_cmd (int argc, char *argv[])
1799 {
1800 struct envlist *c;
1801
1802 if (argc < 2)
1803 {
1804 fprintf (stderr,
1805 "Need an argument to 'environ' command. 'environ ?' for help.\n");
1806 return 0;
1807 }
1808 c = getenvcmd (argv[1]);
1809 if (c == 0)
1810 {
1811 fprintf (stderr, "'%s': unknown argument ('environ ?' for help).\n",
1812 argv[1]);
1813 return 0;
1814 }
1815 if (Ambiguous (c))
1816 {
1817 fprintf (stderr, "'%s': ambiguous argument ('environ ?' for help).\n",
1818 argv[1]);
1819 return 0;
1820 }
1821 if (c->narg + 2 != argc)
1822 {
1823 fprintf (stderr,
1824 "Need %s%d argument%s to 'environ %s' command. 'environ ?' for help.\n",
1825 c->narg < argc + 2 ? "only " : "",
1826 c->narg, c->narg == 1 ? "" : "s", c->name);
1827 return 0;
1828 }
1829 (*c->handler) (argv[2], argv[3]);
1830 return 1;
1831 }
1832
1833 struct env_lst
1834 {
1835 struct env_lst *next; /* pointer to next structure */
1836 struct env_lst *prev; /* pointer to previous structure */
1837 unsigned char *var; /* pointer to variable name */
1838 unsigned char *value; /* pointer to variable value */
1839 int export; /* 1 -> export with default list of variables */
1840 int welldefined; /* A well defined variable */
1841 };
1842
1843 struct env_lst envlisthead;
1844
1845 struct env_lst *
1846 env_find (const char *var)
1847 {
1848 struct env_lst *ep;
1849
1850 for (ep = envlisthead.next; ep; ep = ep->next)
1851 {
1852 if (strcmp ((char *) ep->var, var) == 0)
1853 return (ep);
1854 }
1855 return (NULL);
1856 }
1857
1858 void
1859 env_init (void)
1860 {
1861 char **epp, *cp;
1862 struct env_lst *ep;
1863
1864 for (epp = environ; *epp; epp++)
1865 {
1866 cp = strchr (*epp, '=');
1867 if (cp)
1868 {
1869 *cp = '\0';
1870 ep = env_define (*epp, cp + 1);
1871 ep->export = 0;
1872 *cp = '=';
1873 }
1874 }
1875 /*
1876 * Special case for DISPLAY variable. If it is ":0.0" or
1877 * "unix:0.0", we have to get rid of "unix" and insert our
1878 * hostname.
1879 */
1880 if ((ep = env_find ("DISPLAY"))
1881 && ((*ep->value == ':')
1882 || (strncmp ((char *) ep->value, "unix:", 5) == 0)))
1883 {
1884 char *hostname = localhost ();
1885 char *cp2 = strchr ((char *) ep->value, ':');
1886
1887 cp = xmalloc (strlen (hostname) + strlen (cp2) + 1);
1888 sprintf (cp, "%s%s", hostname, cp2);
1889
1890 free (ep->value);
1891 ep->value = (unsigned char *) cp;
1892
1893 free (hostname);
1894 }
1895 /*
1896 * If USER is not defined, but LOGNAME is, then add
1897 * USER with the value from LOGNAME. By default, we
1898 * don't export the USER variable.
1899 */
1900 if ((env_find ("USER") == NULL) && (ep = env_find ("LOGNAME")))
1901 {
1902 env_define ("USER", (char *) ep->value);
1903 env_unexport ("USER", NULL);
1904 }
1905 env_export ("DISPLAY", NULL);
1906 env_export ("PRINTER", NULL);
1907 }
1908
1909 struct env_lst *
1910 env_define (const char *var, char *value)
1911 {
1912 struct env_lst *ep = env_find (var);
1913 if (ep)
1914 {
1915 free (ep->var);
1916 free (ep->value);
1917 }
1918 else
1919 {
1920 ep = (struct env_lst *) xmalloc (sizeof (struct env_lst));
1921 ep->next = envlisthead.next;
1922 envlisthead.next = ep;
1923 ep->prev = &envlisthead;
1924 if (ep->next)
1925 ep->next->prev = ep;
1926 }
1927 ep->welldefined = opt_welldefined ((char *) var);
1928 ep->export = 1;
1929 ep->var = (unsigned char *) strdup ((char *) var);
1930 ep->value = (unsigned char *) strdup ((char *) value);
1931 return (ep);
1932 }
1933
1934 struct env_lst *
1935 env_undefine (const char *var, char *d)
1936 {
1937 struct env_lst *ep = env_find (var);
1938 if (ep)
1939 {
1940 ep->prev->next = ep->next;
1941 if (ep->next)
1942 ep->next->prev = ep->prev;
1943 free (ep->var);
1944 free (ep->value);
1945 free (ep);
1946 }
1947 return NULL;
1948 }
1949
1950 struct env_lst *
1951 env_export (const char *var, char *d)
1952 {
1953 struct env_lst *ep = env_find (var);
1954 if (ep)
1955 ep->export = 1;
1956 return NULL;
1957 }
1958
1959 struct env_lst *
1960 env_unexport (const char *var, char *d)
1961 {
1962 struct env_lst *ep = env_find (var);
1963 if (ep)
1964 ep->export = 0;
1965 return NULL;
1966 }
1967
1968 struct env_lst *
1969 env_send (const char *var, char *d)
1970 {
1971 struct env_lst *ep;
1972
1973 if (my_state_is_wont (TELOPT_NEW_ENVIRON)
1974 #ifdef OLD_ENVIRON
1975 && my_state_is_wont (TELOPT_OLD_ENVIRON)
1976 #endif
1977 )
1978 {
1979 fprintf (stderr,
1980 "Cannot send '%s': Telnet ENVIRON option not enabled\n", var);
1981 return NULL;
1982 }
1983 ep = env_find (var);
1984 if (ep == 0)
1985 {
1986 fprintf (stderr, "Cannot send '%s': variable not defined\n", var);
1987 return NULL;
1988 }
1989 env_opt_start_info ();
1990 env_opt_add (ep->var);
1991 env_opt_end (0);
1992
1993 return NULL;
1994 }
1995
1996 struct env_lst *
1997 env_list (const char *d1, char *d2)
1998 {
1999 struct env_lst *ep;
2000
2001 for (ep = envlisthead.next; ep; ep = ep->next)
2002 {
2003 printf ("%c %-20s %s\n", ep->export ? '*' : ' ', ep->var, ep->value);
2004 }
2005 return NULL;
2006 }
2007
2008 unsigned char *
2009 env_default (int init, int welldefined)
2010 {
2011 static struct env_lst *nep = NULL;
2012
2013 if (init)
2014 {
2015 nep = &envlisthead;
2016 return NULL;
2017 }
2018 if (nep)
2019 {
2020 while ((nep = nep->next))
2021 {
2022 if (nep->export && (nep->welldefined == welldefined))
2023 return (nep->var);
2024 }
2025 }
2026 return (NULL);
2027 }
2028
2029 unsigned char *
2030 env_getvalue (const char *var, bool exported_only)
2031 {
2032 struct env_lst *ep = env_find (var);
2033 return ep && (!exported_only || ep->export) ? ep->value : NULL;
2034 }
2035
2036 #if defined OLD_ENVIRON && defined ENV_HACK
2037 void
2038 env_varval (const char *what, char *d)
2039 {
2040 extern int old_env_var, old_env_value, env_auto;
2041 int len = strlen (what);
2042
2043 if (len == 0)
2044 goto unknown;
2045
2046 if (strncasecmp ((char *) what, "status", len) == 0)
2047 {
2048 if (env_auto)
2049 printf ("%s%s", "VAR and VALUE are/will be ",
2050 "determined automatically\n");
2051 if (old_env_var == OLD_ENV_VAR)
2052 printf ("VAR and VALUE set to correct definitions\n");
2053 else
2054 printf ("VAR and VALUE definitions are reversed\n");
2055 }
2056 else if (strncasecmp ((char *) what, "auto", len) == 0)
2057 {
2058 env_auto = 1;
2059 old_env_var = OLD_ENV_VALUE;
2060 old_env_value = OLD_ENV_VAR;
2061 }
2062 else if (strncasecmp ((char *) what, "right", len) == 0)
2063 {
2064 env_auto = 0;
2065 old_env_var = OLD_ENV_VAR;
2066 old_env_value = OLD_ENV_VALUE;
2067 }
2068 else if (strncasecmp ((char *) what, "wrong", len) == 0)
2069 {
2070 env_auto = 0;
2071 old_env_var = OLD_ENV_VALUE;
2072 old_env_value = OLD_ENV_VAR;
2073 }
2074 else
2075 {
2076 unknown:
2077 printf
2078 ("Unknown \"varval\" command. (\"auto\", \"right\", \"wrong\", \"status\")\n");
2079 }
2080 }
2081 #endif
2082
2083 #if defined AUTHENTICATION
2084 /*
2085 * The AUTHENTICATE command.
2086 */
2087
2088 struct authlist
2089 {
2090 const char *name;
2091 char *help;
2092 int (*handler) (char *);
2093 int narg;
2094 };
2095
2096 extern int auth_enable (char *), auth_disable (char *), auth_status (void);
2097 static int auth_help (void);
2098
2099 struct authlist AuthList[] = {
2100 {"status", "Display current status of authentication information",
2101 (int (*)(char *)) auth_status, 0},
2102 {"disable", "Disable an authentication type ('auth disable ?' for more)",
2103 auth_disable, 1},
2104 {"enable", "Enable an authentication type ('auth enable ?' for more)",
2105 auth_enable, 1},
2106 {"help", NULL, (int (*)(char *)) auth_help, 0},
2107 {"?", "Print help information", (int (*)(char *)) auth_help, 0},
2108 {NULL, NULL, NULL, 0},
2109 };
2110
2111 static int
2112 auth_help (void)
2113 {
2114 struct authlist *c;
2115
2116 for (c = AuthList; c->name; c++)
2117 {
2118 if (c->help)
2119 {
2120 if (*c->help)
2121 printf ("%-15s %s\n", c->name, c->help);
2122 else
2123 printf ("\n");
2124 }
2125 }
2126 return 0;
2127 }
2128
2129 int
2130 auth_cmd (int argc, char *argv[])
2131 {
2132 struct authlist *c;
2133
2134 if (argc < 2)
2135 {
2136 fprintf (stderr,
2137 "Need an argument to 'auth' command. 'auth ?' for help.\n");
2138 return 0;
2139 }
2140
2141 c = (struct authlist *)
2142 genget (argv[1], (char **) AuthList, sizeof (struct authlist));
2143 if (c == NULL)
2144 {
2145 fprintf (stderr, "'%s': unknown argument ('auth ?' for help).\n",
2146 argv[1]);
2147 return 0;
2148 }
2149 if (Ambiguous (c))
2150 {
2151 fprintf (stderr, "'%s': ambiguous argument ('auth ?' for help).\n",
2152 argv[1]);
2153 return 0;
2154 }
2155 if (c->narg + 2 != argc)
2156 {
2157 fprintf (stderr,
2158 "Need %s%d argument%s to 'auth %s' command. 'auth ?' for help.\n",
2159 c->narg < argc + 2 ? "only " : "",
2160 c->narg, c->narg == 1 ? "" : "s", c->name);
2161 return 0;
2162 }
2163 return (*c->handler) (argv[2]);
2164 }
2165 #endif
2166
2167 #ifdef ENCRYPTION
2168 /*
2169 * The ENCRYPT command.
2170 */
2171
2172 struct encryptlist
2173 {
2174 const char *name;
2175 const char *help;
2176 int (*handler) (char *, char *);
2177 int needconnect;
2178 int minarg;
2179 int maxarg;
2180 };
2181
2182 extern int
2183 EncryptEnable (char *, char *),
2184 EncryptDisable (char *, char *),
2185 EncryptType (char *, char *),
2186 EncryptStart (char *),
2187 EncryptStartInput (void),
2188 EncryptStartOutput (void),
2189 EncryptStop (char *),
2190 EncryptStopInput (void), EncryptStopOutput (void), EncryptStatus (void);
2191 static int EncryptHelp (void);
2192
2193 struct encryptlist EncryptList[] = {
2194 {"enable", "Enable encryption. ('encrypt enable ?' for more)",
2195 EncryptEnable, 1, 1, 2},
2196 {"disable", "Disable encryption. ('encrypt enable ?' for more)",
2197 EncryptDisable, 0, 1, 2},
2198 {"type", "Set encryption type. ('encrypt type ?' for more)",
2199 EncryptType, 0, 1, 1},
2200 {"start", "Start encryption. ('encrypt start ?' for more)",
2201 (int (*)(char *, char *)) EncryptStart, 1, 0, 1},
2202 {"stop", "Stop encryption. ('encrypt stop ?' for more)",
2203 (int (*)(char *, char *)) EncryptStop, 1, 0, 1},
2204 {"input", "Start encrypting the input stream",
2205 (int (*)(char *, char *)) EncryptStartInput, 1, 0, 0},
2206 {"-input", "Stop encrypting the input stream",
2207 (int (*)(char *, char *)) EncryptStopInput, 1, 0, 0},
2208 {"output", "Start encrypting the output stream",
2209 (int (*)(char *, char *)) EncryptStartOutput, 1, 0, 0},
2210 {"-output", "Stop encrypting the output stream",
2211 (int (*)(char *, char *)) EncryptStopOutput, 1, 0, 0},
2212
2213 {"status", "Display current status of authentication information",
2214 (int (*)(char *, char *)) EncryptStatus, 0, 0, 0},
2215 {"help", 0, (int (*)(char *, char *)) EncryptHelp, 0, 0, 0},
2216 {"?", "Print help information", (int (*)(char *, char *)) EncryptHelp, 0, 0,
2217 0},
2218 {NULL, NULL, NULL, 0, 0, 0},
2219 };
2220
2221 static int
2222 EncryptHelp (void)
2223 {
2224 struct encryptlist *c;
2225
2226 for (c = EncryptList; c->name; c++)
2227 {
2228 if (c->help)
2229 {
2230 if (*c->help)
2231 printf ("%-15s %s\n", c->name, c->help);
2232 else
2233 printf ("\n");
2234 }
2235 }
2236 return 0;
2237 }
2238
2239 int
2240 encrypt_cmd (int argc, char *argv[])
2241 {
2242 struct encryptlist *c;
2243
2244 if (argc < 2)
2245 {
2246 fprintf (stderr,
2247 "Need an argument to 'encrypt' command. 'encrypt ?' for help.\n");
2248 return 0;
2249 }
2250
2251 c = (struct encryptlist *)
2252 genget (argv[1], (char **) EncryptList, sizeof (struct encryptlist));
2253 if (c == NULL)
2254 {
2255 fprintf (stderr, "'%s': unknown argument ('encrypt ?' for help).\n",
2256 argv[1]);
2257 return 0;
2258 }
2259 if (Ambiguous (c))
2260 {
2261 fprintf (stderr, "'%s': ambiguous argument ('encrypt ?' for help).\n",
2262 argv[1]);
2263 return 0;
2264 }
2265 argc -= 2;
2266 if (argc < c->minarg || argc > c->maxarg)
2267 {
2268 if (c->minarg == c->maxarg)
2269 {
2270 fprintf (stderr, "Need %s%d argument%s ",
2271 c->minarg < argc ? "only " : "", c->minarg,
2272 c->minarg == 1 ? "" : "s");
2273 }
2274 else
2275 {
2276 fprintf (stderr, "Need %s%d-%d arguments ",
2277 c->maxarg < argc ? "only " : "", c->minarg, c->maxarg);
2278 }
2279 fprintf (stderr, "to 'encrypt %s' command. 'encrypt ?' for help.\n",
2280 c->name);
2281 return 0;
2282 }
2283 if (c->needconnect && !connected)
2284 {
2285 if (!(argc && (isprefix (argv[2], "help") || isprefix (argv[2], "?"))))
2286 {
2287 printf ("?Need to be connected first.\n");
2288 return 0;
2289 }
2290 }
2291 return (*c->handler) (argc > 0 ? argv[2] : NULL, argc > 1 ? argv[3] : NULL);
2292 }
2293 #endif /* ENCRYPTION */
2294
2295 /*
2296 * Print status about the connection.
2297 */
2298 static int
2299 status (int argc, char *argv[])
2300 {
2301 if (connected)
2302 {
2303 printf ("Connected to %s.\n", hostname);
2304 if ((argc < 2) || strcmp (argv[1], "notmuch"))
2305 {
2306 int mode = getconnmode ();
2307
2308 if (my_want_state_is_will (TELOPT_LINEMODE))
2309 {
2310 printf ("Operating with LINEMODE option\n");
2311 printf ("%s line editing\n",
2312 (mode & MODE_EDIT) ? "Local" : "No");
2313 printf ("%s catching of signals\n",
2314 (mode & MODE_TRAPSIG) ? "Local" : "No");
2315 slcstate ();
2316 #ifdef KLUDGELINEMODE
2317 }
2318 else if (kludgelinemode && my_want_state_is_dont (TELOPT_SGA))
2319 {
2320 printf ("Operating in obsolete linemode\n");
2321 #endif
2322 }
2323 else
2324 {
2325 printf ("Operating in single character mode\n");
2326 if (localchars)
2327 printf ("Catching signals locally\n");
2328 }
2329 printf ("%s character echo\n",
2330 (mode & MODE_ECHO) ? "Local" : "Remote");
2331 if (my_want_state_is_will (TELOPT_LFLOW))
2332 printf ("%s flow control\n", (mode & MODE_FLOW) ? "Local" : "No");
2333 #ifdef ENCRYPTION
2334 encrypt_display ();
2335 #endif /* ENCRYPTION */
2336 }
2337 }
2338 else
2339 {
2340 printf ("No connection.\n");
2341 }
2342 printf ("Escape character is '%s'.\n", control (escape));
2343 fflush (stdout);
2344 return 1;
2345 }
2346
2347 #ifdef SIGINFO
2348 /*
2349 * Function that gets called when SIGINFO is received.
2350 */
2351 int
2352 ayt_status (void)
2353 {
2354 call (status, "status", "notmuch", 0);
2355 return 1; /* not used */
2356 }
2357 #endif /* SIGINFO */
2358
2359 static void cmdrc (char *m1, char *m2);
2360
2361 int
2362 tn (int argc, char *argv[])
2363 {
2364 #ifdef IPV6
2365 struct addrinfo *result, *aip, hints, *hostaddr;
2366 #else
2367 struct hostent *host = 0;
2368 struct sockaddr_in sin;
2369 struct servent *sp = 0;
2370 in_addr_t temp;
2371 #endif
2372 const int on = 1;
2373 int err;
2374 char *cmd, *hostp = 0, *portp = 0, *user = 0, *srchostp = 0;
2375 #if defined HAVE_IDN || defined HAVE_IDN2
2376 char *hosttmp = 0;
2377 #endif
2378
2379 #ifdef IPV6
2380 memset (&hints, 0, sizeof (hints));
2381 #else
2382 /* clear the socket address prior to use */
2383 memset ((char *) &sin, 0, sizeof (sin));
2384 #endif
2385
2386 if (connected)
2387 {
2388 printf ("?Already connected to %s\n", hostname);
2389 return 0;
2390 }
2391 if (argc < 2)
2392 {
2393 strcpy (line, "open ");
2394 printf ("(to) ");
2395 if (fgets (&line[strlen (line)], sizeof (line) - strlen (line), stdin))
2396 {
2397 makeargv ();
2398 argc = margc;
2399 argv = margv;
2400 }
2401 else
2402 printf ("?Name of host was not understood.\n");
2403 }
2404 cmd = *argv;
2405 --argc;
2406 ++argv;
2407 while (argc)
2408 {
2409 if (strcmp (*argv, "help") == 0 || isprefix (*argv, "?"))
2410 goto usage;
2411 if (strcmp (*argv, "-l") == 0)
2412 {
2413 --argc;
2414 ++argv;
2415 if (argc == 0)
2416 goto usage;
2417 user = *argv++;
2418 --argc;
2419 continue;
2420 }
2421 if (strcmp (*argv, "-b") == 0)
2422 {
2423 --argc;
2424 ++argv;
2425 if (argc == 0)
2426 goto usage;
2427 srchostp = *argv++;
2428 --argc;
2429 continue;
2430 }
2431 if (strcmp (*argv, "-a") == 0)
2432 {
2433 --argc;
2434 ++argv;
2435 autologin = 1;
2436 continue;
2437 }
2438 if (strcmp (*argv, "-6") == 0)
2439 {
2440 --argc;
2441 ++argv;
2442 #ifdef IPV6
2443 hints.ai_family = AF_INET6;
2444 #else
2445 puts ("IPv6 isn't supported");
2446 #endif
2447 continue;
2448 }
2449 if (strcmp (*argv, "-4") == 0)
2450 {
2451 --argc;
2452 ++argv;
2453 #ifdef IPV6
2454 hints.ai_family = AF_INET;
2455 #endif
2456 continue;
2457 }
2458 if (hostp == 0)
2459 {
2460 hostp = *argv++;
2461 --argc;
2462 continue;
2463 }
2464 if (portp == 0)
2465 {
2466 portp = *argv++;
2467 --argc;
2468 continue;
2469 }
2470 usage:
2471 printf
2472 ("usage: %s [-4] [-6] [-l user] [-b addr] [-a] host-name [port]\n",
2473 cmd);
2474 return 0;
2475 }
2476 if (hostp == 0)
2477 goto usage;
2478
2479 if (!portp)
2480 {
2481 portp = "telnet";
2482 telnetport = 1;
2483 }
2484 else
2485 {
2486 if (*portp == '-')
2487 {
2488 portp++;
2489 telnetport = 1;
2490 }
2491 else
2492 telnetport = 0;
2493 if (*portp >= '0' && *portp <= '9')
2494 {
2495 long long int val;
2496 char *endp;
2497
2498 val = strtoll (portp, &endp, 10);
2499
2500 if ((errno == ERANGE && (val == LLONG_MAX || val == LLONG_MIN))
2501 || (*endp == '\0' && (val < 1 || val > 65535)))
2502 {
2503 printf ("Port number %s is out of range.\n", portp);
2504 return 0;
2505 }
2506 else if (*endp)
2507 {
2508 printf ("Invalid port name '%s'.\n", portp);
2509 return 0;
2510 }
2511 }
2512 }
2513
2514 free (hostname);
2515 hostname = strdup (hostp);
2516 if (!hostname)
2517 {
2518 printf ("Can't allocate memory to copy hostname\n");
2519 return 0;
2520 }
2521
2522 #if defined AUTHENTICATION || defined ENCRYPTION
2523 {
2524 /* Extract instance name of server, eliminating
2525 * the Kerberos principal prefix.
2526 */
2527 char *p = strchr (hostp, '/');
2528
2529 if (p)
2530 hostp = ++p;
2531 }
2532 #endif /* AUTHENTICATION || ENCRYPTION */
2533
2534 #if defined HAVE_IDN || defined HAVE_IDN2
2535 err = idna_to_ascii_lz (hostp, &hosttmp, 0);
2536 if (err)
2537 {
2538 printf ("Server lookup failure: %s:%s, %s\n",
2539 hostp, portp, idna_strerror (err));
2540 return 0;
2541 }
2542 hostp = hosttmp;
2543 #endif /* !HAVE_IDN && !HAVE_IDN2 */
2544
2545 #ifdef IPV6
2546 hints.ai_socktype = SOCK_STREAM;
2547 # ifdef AI_IDN
2548 hints.ai_flags = AI_IDN;
2549 # endif
2550
2551 if (srchostp)
2552 {
2553 err = getaddrinfo (srchostp, "0", &hints, &hostaddr);
2554 if (err < 0)
2555 {
2556 printf ("Could not resolve %s: %s\n", srchostp, gai_strerror (err));
2557 return 0;
2558 }
2559 hints.ai_family = hostaddr->ai_family;
2560 }
2561
2562 err = getaddrinfo (hostp, portp, &hints, &result);
2563 if (err)
2564 {
2565 const char *errmsg;
2566
2567 if (err == EAI_SYSTEM)
2568 errmsg = strerror (errno);
2569 else
2570 errmsg = gai_strerror (err);
2571
2572 printf ("Server lookup failure: %s:%s, %s\n", hostp, portp, errmsg);
2573 return 0;
2574 }
2575
2576 aip = result;
2577
2578 do
2579 {
2580 char buf[256];
2581
2582 err = getnameinfo (aip->ai_addr, aip->ai_addrlen, buf, sizeof (buf),
2583 NULL, 0, NI_NUMERICHOST);
2584 if (err)
2585 {
2586 /* I don't know how thing can happen, but we just handle it. */
2587 const char *errmsg;
2588
2589 if (err == EAI_SYSTEM)
2590 errmsg = strerror (errno);
2591 else
2592 errmsg = gai_strerror (err);
2593
2594 printf ("getnameinfo error: %s\n", errmsg);
2595 return 0;
2596 }
2597
2598 printf ("Trying %s...\n", buf);
2599
2600 net = socket (aip->ai_family, SOCK_STREAM, 0);
2601 if (net < 0)
2602 {
2603 perror ("telnet: socket");
2604 return 0;
2605 }
2606
2607 if (srchostp)
2608 {
2609 err = bind (net, hostaddr->ai_addr, hostaddr->ai_addrlen);
2610 if (err < 0)
2611 {
2612 perror ("telnet: bind");
2613 return 0;
2614 }
2615 }
2616
2617 if (debug)
2618 {
2619 err = setsockopt (net, SOL_SOCKET, SO_DEBUG, &on, sizeof (on));
2620 if (err < 0)
2621 perror ("setsockopt (SO_DEBUG)");
2622 }
2623
2624 err = connect (net, (struct sockaddr *) aip->ai_addr, aip->ai_addrlen);
2625 if (err < 0)
2626 {
2627 if (aip->ai_next)
2628 {
2629 perror ("Connection failed");
2630 aip = aip->ai_next;
2631 close (net);
2632 continue;
2633 }
2634
2635 perror ("telnet: Unable to connect to remote host");
2636 return 0;
2637 }
2638
2639 connected++;
2640 # if defined AUTHENTICATION || defined ENCRYPTION
2641 auth_encrypt_connect (connected);
2642 # endif/* defined(AUTHENTICATION) || defined(ENCRYPTION) */
2643 }
2644 while (!connected);
2645
2646 if (srchostp)
2647 freeaddrinfo (hostaddr);
2648 freeaddrinfo (result);
2649 #else /* !IPV6 */
2650 temp = inet_addr (hostp);
2651 if (temp != (in_addr_t) - 1)
2652 {
2653 sin.sin_addr.s_addr = temp;
2654 sin.sin_family = AF_INET;
2655 }
2656 else
2657 {
2658 host = gethostbyname (hostp);
2659 if (host)
2660 {
2661 sin.sin_family = host->h_addrtype;
2662 memmove (&sin.sin_addr, host->h_addr_list[0], host->h_length);
2663 }
2664 else
2665 {
2666 printf ("Can't lookup hostname %s\n", hostp);
2667 return 0;
2668 }
2669 }
2670
2671 sin.sin_port = atoi (portp);
2672 if (sin.sin_port == 0)
2673 {
2674 sp = getservbyname (portp, "tcp");
2675 if (sp == 0)
2676 {
2677 printf ("tcp/%s: unknown service\n", portp);
2678 return 0;
2679 }
2680 sin.sin_port = sp->s_port;
2681 }
2682 else
2683 sin.sin_port = htons (sin.sin_port);
2684
2685 printf ("Trying %s...\n", inet_ntoa (sin.sin_addr));
2686 do
2687 {
2688 net = socket (AF_INET, SOCK_STREAM, 0);
2689 if (net < 0)
2690 {
2691 perror ("telnet: socket");
2692 return 0;
2693 }
2694 # if defined IPPROTO_IP && defined IP_TOS
2695 {
2696 # ifdef IPTOS_LOWDELAY
2697 const int tos = IPTOS_LOWDELAY;
2698 # else
2699 const int tos = 0x10;
2700 # endif
2701
2702 err = setsockopt (net, IPPROTO_IP, IP_TOS,
2703 (char *) &tos, sizeof (tos));
2704 if (err < 0 && errno != ENOPROTOOPT)
2705 perror ("telnet: setsockopt (IP_TOS) (ignored)");
2706 }
2707 # endif/* defined(IPPROTO_IP) && defined(IP_TOS) */
2708
2709 if (debug
2710 && setsockopt (net, SOL_SOCKET, SO_DEBUG, &on, sizeof (on)) < 0)
2711 perror ("setsockopt (SO_DEBUG)");
2712
2713 if (connect (net, (struct sockaddr *) &sin, sizeof (sin)) < 0)
2714 {
2715 if (host && host->h_addr_list[1])
2716 {
2717 int oerrno = errno;
2718
2719 fprintf (stderr, "telnet: connect to address %s: ",
2720 inet_ntoa (sin.sin_addr));
2721 errno = oerrno;
2722 perror ((char *) 0);
2723 host->h_addr_list++;
2724 memmove (&sin.sin_addr, host->h_addr_list[0], host->h_length);
2725 close (net);
2726 continue;
2727 }
2728 perror ("telnet: Unable to connect to remote host");
2729 return 0;
2730 }
2731 connected++;
2732 # if defined AUTHENTICATION || defined ENCRYPTION
2733 auth_encrypt_connect (connected);
2734 # endif/* defined(AUTHENTICATION) || defined(ENCRYPTION) */
2735 }
2736 while (connected == 0);
2737 #endif /* !IPV6 */
2738
2739 cmdrc (hostp, hostname);
2740 #if defined HAVE_IDN || defined HAVE_IDN2
2741 /* Last use of HOSTP, alias HOSTTMP. */
2742 free (hosttmp);
2743 #endif
2744
2745 if (autologin && user == NULL)
2746 {
2747 struct passwd *pw;
2748
2749 user = getenv ("USER");
2750 if (user == NULL || ((pw = getpwnam (user)) && pw->pw_uid != getuid ()))
2751 {
2752 if ((pw = getpwuid (getuid ())))
2753 user = pw->pw_name;
2754 else
2755 user = NULL;
2756 }
2757 }
2758 if (user)
2759 {
2760 env_define ("USER", user);
2761 env_export ("USER", NULL);
2762 }
2763 call (status, "status", "notmuch", 0);
2764 err = 0;
2765 if (setjmp (peerdied) == 0)
2766 telnet (user);
2767 else
2768 err = 1;
2769
2770 close (net);
2771 ExitString ("Connection closed by foreign host.\n", err);
2772 /* NOT REACHED */
2773 return 0;
2774 }
2775
2776 #define HELPINDENT (sizeof ("connect"))
2777
2778 static char
2779 openhelp[] = "connect to a site",
2780 closehelp[] = "close current connection",
2781 logouthelp[] = "forcibly logout remote user and close the connection",
2782 quithelp[] = "exit telnet",
2783 statushelp[] = "print status information",
2784 helphelp[] = "print help information",
2785 sendhelp[] = "transmit special characters ('send ?' for more)",
2786 sethelp[] = "set operating parameters ('set ?' for more)",
2787 unsethelp[] = "unset operating parameters ('unset ?' for more)",
2788 togglestring[] = "toggle operating parameters ('toggle ?' for more)",
2789 slchelp[] = "change state of special characters ('slc ?' for more)",
2790 displayhelp[] = "display operating parameters",
2791 #if defined AUTHENTICATION
2792 authhelp[] = "turn on (off) authentication ('auth ?' for more)",
2793 #endif
2794 #ifdef ENCRYPTION
2795 encrypthelp[] = "turn on (off) encryption ('encrypt ?' for more)",
2796 #endif
2797 /* ENCRYPTION */
2798 #if defined unix || defined __unix || defined __unix__
2799 zhelp[] = "suspend telnet",
2800 #endif
2801 /* unix || __unix || __unix__ */
2802 shellhelp[] = "invoke a subshell",
2803 envhelp[] = "change environment variables ('environ ?' for more)",
2804 modestring[] = "try to enter line or character mode ('mode ?' for more)";
2805
2806 static int help (int argc, char **argv);
2807
2808 static Command cmdtab[] = {
2809 {"close", closehelp, bye, 1},
2810 {"logout", logouthelp, logoutcmd, 1},
2811 {"display", displayhelp, display, 0},
2812 {"mode", modestring, modecmd, 0},
2813 {"open", openhelp, tn, 0},
2814 {"quit", quithelp, quit, 0},
2815 {"send", sendhelp, sendcmd, 0},
2816 {"set", sethelp, setcmd, 0},
2817 {"unset", unsethelp, unsetcmd, 0},
2818 {"status", statushelp, status, 0},
2819 {"toggle", togglestring, toggle, 0},
2820 {"slc", slchelp, slccmd, 0},
2821 #if defined AUTHENTICATION
2822 {"auth", authhelp, auth_cmd, 0},
2823 #endif
2824 #ifdef ENCRYPTION
2825 {"encrypt", encrypthelp, encrypt_cmd, 0},
2826 #endif /* ENCRYPTION */
2827 #if defined unix || defined __unix || defined __unix__
2828 {"z", zhelp, suspend, 0},
2829 #endif /* unix || __unix || __unix__ */
2830 {"!", shellhelp, shell, 0},
2831 {"environ", envhelp, env_cmd, 0},
2832 {"?", helphelp, help, 0},
2833 {NULL, NULL, NULL, 0}
2834 };
2835
2836 static char crmodhelp[] = "deprecated command -- use 'toggle crmod' instead";
2837 static char escapehelp[] = "deprecated command -- use 'set escape' instead";
2838
2839 static Command cmdtab2[] = {
2840 {"help", helphelp, help, 0},
2841 {"escape", escapehelp, setescape, 0},
2842 {"crmod", crmodhelp, togcrmod, 0},
2843 {NULL, NULL, NULL, 0}
2844 };
2845
2846
2847 /*
2848 * Call routine with argc, argv set from args (terminated by 0).
2849 */
2850
2851 static int
2852 call (intrtn_t routine, ...)
2853 {
2854 va_list ap;
2855 char *args[100];
2856 int argno = 0;
2857
2858 va_start (ap, routine);
2859 while ((args[argno++] = va_arg (ap, char *)) != 0)
2860 {
2861 ;
2862 }
2863 va_end (ap);
2864 return (*routine) (argno - 1, args);
2865 }
2866
2867
2868 static Command *
2869 getcmd (char *name)
2870 {
2871 Command *cm;
2872
2873 if ((cm = (Command *) genget (name, (char **) cmdtab, sizeof (Command))))
2874 return cm;
2875 return (Command *) genget (name, (char **) cmdtab2, sizeof (Command));
2876 }
2877
2878 void
2879 command (int top, char *tbuf, int cnt)
2880 {
2881 Command *c;
2882
2883 setcommandmode ();
2884 if (!top)
2885 {
2886 putchar ('\n');
2887 }
2888 #if defined unix || defined __unix || defined __unix__
2889 else
2890 {
2891 signal (SIGINT, SIG_DFL);
2892 signal (SIGQUIT, SIG_DFL);
2893 }
2894 #endif /* unix || __unix || __unix__ */
2895 for (;;)
2896 {
2897 if (rlogin == _POSIX_VDISABLE)
2898 printf ("%s> ", prompt);
2899 if (tbuf)
2900 {
2901 char *cp;
2902 cp = line;
2903 while (cnt > 0 && (*cp++ = *tbuf++) != '\n')
2904 cnt--;
2905 tbuf = 0;
2906 if (cp == line || *--cp != '\n' || cp == line)
2907 goto getline;
2908 *cp = '\0';
2909 if (rlogin == _POSIX_VDISABLE)
2910 printf ("%s\n", line);
2911 }
2912 else
2913 {
2914 getline:
2915 if (rlogin != _POSIX_VDISABLE)
2916 printf ("%s> ", prompt);
2917 if (fgets (line, sizeof (line), stdin) == NULL)
2918 {
2919 if (feof (stdin) || ferror (stdin))
2920 {
2921 printf ("\n");
2922 (void) quit (0, NULL);
2923 /*NOTREACHED*/}
2924 break;
2925 }
2926 }
2927 if (line[0] == 0)
2928 break;
2929 makeargv ();
2930 if (margv[0] == 0)
2931 {
2932 break;
2933 }
2934 c = getcmd (margv[0]);
2935 if (Ambiguous (c))
2936 {
2937 printf ("?Ambiguous command\n");
2938 continue;
2939 }
2940 if (c == 0)
2941 {
2942 printf ("?Invalid command\n");
2943 continue;
2944 }
2945 if (c->needconnect && !connected)
2946 {
2947 printf ("?Need to be connected first.\n");
2948 continue;
2949 }
2950 if ((*c->handler) (margc, margv))
2951 {
2952 break;
2953 }
2954 }
2955 if (!top)
2956 {
2957 if (!connected)
2958 {
2959 longjmp (toplevel, 1);
2960 }
2961 setconnmode (0);
2962 }
2963 }
2964
2965 /*
2966 * Help command.
2967 */
2968 static int
2969 help (int argc, char *argv[])
2970 {
2971 Command *c;
2972
2973 if (argc == 1)
2974 {
2975 printf ("Commands may be abbreviated. Commands are:\n\n");
2976 for (c = cmdtab; c->name; c++)
2977 if (c->help)
2978 {
2979 printf ("%-*s\t%s\n", (int) HELPINDENT, c->name, c->help);
2980 }
2981 return 0;
2982 }
2983 while (--argc > 0)
2984 {
2985 char *arg;
2986 arg = *++argv;
2987 c = getcmd (arg);
2988 if (Ambiguous (c))
2989 printf ("?Ambiguous help command %s\n", arg);
2990 else if (c == (Command *) 0)
2991 printf ("?Invalid help command %s\n", arg);
2992 else if (c->help)
2993 printf ("%s\n", c->help);
2994 }
2995 return 0;
2996 }
2997
2998 static char *rcname = 0;
2999
3000 static void
3001 cmdrc (char *m1, char *m2)
3002 {
3003 Command *c;
3004 FILE *rcfile;
3005 int gotmachine = 0;
3006 int l1 = strlen (m1);
3007 int l2 = strlen (m2);
3008
3009 if (skiprc)
3010 return;
3011
3012 if (rcname == 0)
3013 {
3014 const char *home = getenv ("HOME");
3015 if (home)
3016 rcname = xasprintf ("%s/.telnetrc", home);
3017 else
3018 rcname = xstrdup ("/.telnetrc");
3019 }
3020
3021 if ((rcfile = fopen (rcname, "r")) == 0)
3022 {
3023 return;
3024 }
3025
3026 for (;;)
3027 {
3028 if (fgets (line, sizeof (line), rcfile) == NULL)
3029 break;
3030 if (line[0] == 0)
3031 break;
3032 if (line[0] == '#')
3033 continue;
3034 if (gotmachine)
3035 {
3036 if (!isspace (line[0]))
3037 gotmachine = 0;
3038 }
3039 if (gotmachine == 0)
3040 {
3041 if (isspace (line[0]))
3042 continue;
3043 if (strncasecmp (line, m1, l1) == 0)
3044 memmove (line, &line[l1], strlen (&line[l1]) + 1);
3045 else if (strncasecmp (line, m2, l2) == 0)
3046 memmove (line, &line[l2], strlen (&line[l2]) + 1);
3047 else if (strncasecmp (line, "DEFAULT", 7) == 0)
3048 memmove (line, &line[7], strlen (&line[7]) + 1);
3049 else
3050 continue;
3051 if (line[0] != ' ' && line[0] != '\t' && line[0] != '\n')
3052 continue;
3053 gotmachine = 1;
3054 }
3055 makeargv ();
3056 if (margv[0] == 0)
3057 continue;
3058 c = getcmd (margv[0]);
3059 if (Ambiguous (c))
3060 {
3061 printf ("?Ambiguous command: %s\n", margv[0]);
3062 continue;
3063 }
3064 if (c == 0)
3065 {
3066 printf ("?Invalid command: %s\n", margv[0]);
3067 continue;
3068 }
3069 /*
3070 * This should never happen...
3071 */
3072 if (c->needconnect && !connected)
3073 {
3074 printf ("?Need to be connected first for %s.\n", margv[0]);
3075 continue;
3076 }
3077 (*c->handler) (margc, margv);
3078 }
3079 fclose (rcfile);
3080 }