2 * Copyright (c) 1988, 1990, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
34 #include "telnet_locl.h"
36 RCSID("$Id: commands.c 16224 2005-10-22 17:17:44Z lha $");
38 #if defined(IPPROTO_IP) && defined(IP_TOS)
40 #endif /* defined(IPPROTO_IP) && defined(IP_TOS) */
43 static char _hostname[MaxHostNameLen];
45 typedef int (*intrtn_t)(int, char**);
46 static int call(intrtn_t, ...);
49 char *name; /* command name */
50 char *help; /* help string (NULL for no help) */
51 int (*handler)(); /* routine which executes command */
52 int needconnect; /* Do we need to be connected to execute? */
55 static char line[256];
56 static char saveline[256];
58 static char *margv[20];
68 if (*cp == '!') { /* Special case shell escape */
69 /* save for shell command */
70 strlcpy(saveline, line, sizeof(saveline));
71 *argp++ = "!"; /* No room in string to get this */
77 while (isspace((unsigned char)c))
83 for (cp2 = cp; c != '\0'; c = *++cp) {
91 if ((c = *++cp) == '\0')
93 } else if (c == '"') {
96 } else if (c == '\'') {
99 } else if (isspace((unsigned char)c))
113 * Make a character string into a number.
115 * Todo: 1. Could take random integers (12, 0x12, 012, 0b1).
128 c = b | 0x40; /* DEL */
141 * Construct a control character sequence
142 * for a special character.
149 * The only way I could get the Sun 3.5 compiler
151 * if ((unsigned int)c >= 0x80)
152 * was to assign "c" to an unsigned int variable...
155 unsigned int uic = (unsigned int)c;
159 if (c == (cc_t)_POSIX_VDISABLE) {
164 buf[1] = ((c>>6)&07) + '0';
165 buf[2] = ((c>>3)&07) + '0';
166 buf[3] = (c&07) + '0';
168 } else if (uic >= 0x20) {
182 * The following are data structures and routines for
183 * the "send" command.
188 char *name; /* How user refers to it (case independent) */
189 char *help; /* Help information (0 ==> no help) */
190 int needconnect; /* Need to be connected */
191 int narg; /* Number of arguments */
192 int (*handler)(); /* Routine to perform (for special ops) */
193 int nbyte; /* Number of bytes to send this command */
194 int what; /* Character to be sent (<0 ==> special) */
202 send_dontcmd (char *),
203 send_willcmd (char *),
204 send_wontcmd (char *);
206 static struct sendlist Sendlist[] = {
207 { "ao", "Send Telnet Abort output", 1, 0, 0, 2, AO },
208 { "ayt", "Send Telnet 'Are You There'", 1, 0, 0, 2, AYT },
209 { "brk", "Send Telnet Break", 1, 0, 0, 2, BREAK },
210 { "break", 0, 1, 0, 0, 2, BREAK },
211 { "ec", "Send Telnet Erase Character", 1, 0, 0, 2, EC },
212 { "el", "Send Telnet Erase Line", 1, 0, 0, 2, EL },
213 { "escape", "Send current escape character", 1, 0, send_esc, 1, 0 },
214 { "ga", "Send Telnet 'Go Ahead' sequence", 1, 0, 0, 2, GA },
215 { "ip", "Send Telnet Interrupt Process", 1, 0, 0, 2, IP },
216 { "intp", 0, 1, 0, 0, 2, IP },
217 { "interrupt", 0, 1, 0, 0, 2, IP },
218 { "intr", 0, 1, 0, 0, 2, IP },
219 { "nop", "Send Telnet 'No operation'", 1, 0, 0, 2, NOP },
220 { "eor", "Send Telnet 'End of Record'", 1, 0, 0, 2, EOR },
221 { "abort", "Send Telnet 'Abort Process'", 1, 0, 0, 2, ABORT },
222 { "susp", "Send Telnet 'Suspend Process'", 1, 0, 0, 2, SUSP },
223 { "eof", "Send Telnet End of File Character", 1, 0, 0, 2, xEOF },
224 { "synch", "Perform Telnet 'Synch operation'", 1, 0, dosynch, 2, 0 },
225 { "getstatus", "Send request for STATUS", 1, 0, get_status, 6, 0 },
226 { "?", "Display send options", 0, 0, send_help, 0, 0 },
227 { "help", 0, 0, 0, send_help, 0, 0 },
228 { "do", 0, 0, 1, send_docmd, 3, 0 },
229 { "dont", 0, 0, 1, send_dontcmd, 3, 0 },
230 { "will", 0, 0, 1, send_willcmd, 3, 0 },
231 { "wont", 0, 0, 1, send_wontcmd, 3, 0 },
235 #define GETSEND(name) ((struct sendlist *) genget(name, (char **) Sendlist, \
236 sizeof(struct sendlist)))
239 sendcmd(int argc, char **argv)
241 int count; /* how many bytes we are going to need to send */
243 struct sendlist *s; /* pointer to current command */
248 printf("need at least one argument for 'send' command\r\n");
249 printf("'send ?' for help\r\n");
253 * First, validate all the send arguments.
254 * In addition, we see how much space we are going to need, and
255 * whether or not we will be doing a "SYNCH" operation (which
256 * flushes the network queue).
259 for (i = 1; i < argc; i++) {
260 s = GETSEND(argv[i]);
262 printf("Unknown send argument '%s'\r\n'send ?' for help.\r\n",
265 } else if (Ambiguous(s)) {
266 printf("Ambiguous send argument '%s'\r\n'send ?' for help.\r\n",
270 if (i + s->narg >= argc) {
272 "Need %d argument%s to 'send %s' command. 'send %s ?' for help.\r\n",
273 s->narg, s->narg == 1 ? "" : "s", s->name, s->name);
277 if (s->handler == send_help) {
283 needconnect += s->needconnect;
285 if (!connected && needconnect) {
286 printf("?Need to be connected first.\r\n");
287 printf("'send ?' for help\r\n");
290 /* Now, do we have enough room? */
291 if (NETROOM() < count) {
292 printf("There is not enough room in the buffer TO the network\r\n");
293 printf("to process your request. Nothing will be done.\r\n");
294 printf("('send synch' will throw away most data in the network\r\n");
295 printf("buffer, if this might help.)\r\n");
298 /* OK, they are all OK, now go through again and actually send */
300 for (i = 1; i < argc; i++) {
301 if ((s = GETSEND(argv[i])) == 0) {
302 fprintf(stderr, "Telnet 'send' error - argument disappeared!\r\n");
308 success += (*s->handler)((s->narg > 0) ? argv[i+1] : 0,
309 (s->narg > 1) ? argv[i+2] : 0);
312 NET2ADD(IAC, s->what);
313 printoption("SENT", IAC, s->what);
316 return (count == success);
320 send_tncmd(void (*func)(), char *cmd, char *name);
330 send_docmd(char *name)
332 return(send_tncmd(send_do, "do", name));
336 send_dontcmd(char *name)
338 return(send_tncmd(send_dont, "dont", name));
342 send_willcmd(char *name)
344 return(send_tncmd(send_will, "will", name));
348 send_wontcmd(char *name)
350 return(send_tncmd(send_wont, "wont", name));
353 extern char *telopts[]; /* XXX */
356 send_tncmd(void (*func)(), char *cmd, char *name)
361 if (isprefix(name, "help") || isprefix(name, "?")) {
364 printf("Usage: send %s <value|option>\r\n", cmd);
365 printf("\"value\" must be from 0 to 255\r\n");
366 printf("Valid options are:\r\n\t");
369 for (cpp = telopts; *cpp; cpp++) {
370 len = strlen(*cpp) + 3;
371 if (col + len > 65) {
375 printf(" \"%s\"", *cpp);
381 cpp = genget(name, telopts, sizeof(char *));
382 if (Ambiguous(cpp)) {
383 fprintf(stderr,"'%s': ambiguous argument ('send %s ?' for help).\r\n",
392 while (*cp >= '0' && *cp <= '9') {
398 fprintf(stderr, "'%s': unknown argument ('send %s ?' for help).\r\n",
401 } else if (val < 0 || val > 255) {
402 fprintf(stderr, "'%s': bad value ('send %s ?' for help).\r\n",
408 printf("?Need to be connected first.\r\n");
418 struct sendlist *s; /* pointer to current command */
419 for (s = Sendlist; s->name; s++) {
421 printf("%-15s %s\r\n", s->name, s->help);
427 * The following are the routines and data structures referred
428 * to by the arguments to the "toggle" command.
443 (SetSockOpt(net, SOL_SOCKET, SO_DEBUG, debug)) < 0) {
444 perror("setsockopt (SO_DEBUG)");
448 if (net > 0 && SetSockOpt(net, SOL_SOCKET, SO_DEBUG, 0, 0) < 0)
449 perror("setsockopt (SO_DEBUG)");
451 printf("Cannot turn off socket debugging\r\n");
456 #if defined(KRB4) && defined(HAVE_KRB_DISABLE_DEBUG)
474 printf("Will send carriage returns as telnet <CR><LF>.\r\n");
476 printf("Will send carriage returns as telnet <CR><NUL>.\r\n");
486 donebinarytoggle = 1;
491 if (my_want_state_is_will(TELOPT_BINARY) &&
492 my_want_state_is_do(TELOPT_BINARY)) {
494 } else if (my_want_state_is_wont(TELOPT_BINARY) &&
495 my_want_state_is_dont(TELOPT_BINARY)) {
498 val = binmode ? 0 : 1;
502 if (my_want_state_is_will(TELOPT_BINARY) &&
503 my_want_state_is_do(TELOPT_BINARY)) {
504 printf("Already operating in binary mode with remote host.\r\n");
506 printf("Negotiating binary mode with remote host.\r\n");
510 if (my_want_state_is_wont(TELOPT_BINARY) &&
511 my_want_state_is_dont(TELOPT_BINARY)) {
512 printf("Already in network ascii mode with remote host.\r\n");
514 printf("Negotiating network ascii mode with remote host.\r\n");
524 donebinarytoggle = 1;
527 val = my_want_state_is_do(TELOPT_BINARY) ? 0 : 1;
530 if (my_want_state_is_do(TELOPT_BINARY)) {
531 printf("Already receiving in binary mode.\r\n");
533 printf("Negotiating binary mode on input.\r\n");
537 if (my_want_state_is_dont(TELOPT_BINARY)) {
538 printf("Already receiving in network ascii mode.\r\n");
540 printf("Negotiating network ascii mode on input.\r\n");
550 donebinarytoggle = 1;
553 val = my_want_state_is_will(TELOPT_BINARY) ? 0 : 1;
556 if (my_want_state_is_will(TELOPT_BINARY)) {
557 printf("Already transmitting in binary mode.\r\n");
559 printf("Negotiating binary mode on output.\r\n");
563 if (my_want_state_is_wont(TELOPT_BINARY)) {
564 printf("Already transmitting in network ascii mode.\r\n");
566 printf("Negotiating network ascii mode on output.\r\n");
574 static int togglehelp (void);
575 #if defined(AUTHENTICATION)
576 extern int auth_togdebug (int);
578 #if defined(ENCRYPTION)
579 extern int EncryptAutoEnc (int);
580 extern int EncryptAutoDec (int);
581 extern int EncryptDebug (int);
582 extern int EncryptVerbose (int);
586 char *name; /* name of toggle */
587 char *help; /* help message */
588 int (*handler)(); /* routine to do actual setting */
590 char *actionexplanation;
593 static struct togglelist Togglelist[] = {
595 "flushing of output when sending interrupt characters",
598 "flush output when sending interrupt characters" },
600 "automatic sending of interrupt characters in urgent mode",
603 "send interrupt characters in urgent mode" },
604 #if defined(AUTHENTICATION)
606 "automatic sending of login and/or authentication info",
609 "send login name and/or authentication information" },
611 "authentication debugging",
614 "print authentication debugging information" },
616 #if defined(ENCRYPTION)
618 "automatic encryption of data stream",
621 "automatically encrypt output" },
623 "automatic decryption of data stream",
626 "automatically decrypt input" },
628 "verbose encryption output",
631 "print verbose encryption output" },
633 "encryption debugging",
636 "print encryption debugging information" },
640 "credentials forwarding",
641 kerberos5_set_forward,
643 "forward credentials" },
645 "forwardable flag of forwarded credentials",
646 kerberos5_set_forwardable,
648 "forward forwardable credentials" },
651 "don't read ~/.telnetrc file",
654 "skip reading of ~/.telnetrc file" },
656 "sending and receiving of binary data",
661 "receiving of binary data",
666 "sending of binary data",
671 "sending carriage returns as telnet <CR><LF>",
676 "mapping of received carriage returns",
679 "map carriage return on output" },
681 "local recognition of certain control characters",
684 "recognize certain control characters" },
685 { " ", "", 0 }, /* empty line */
690 "turn on socket level debugging" },
691 #if defined(KRB4) && defined(HAVE_KRB_DISABLE_DEBUG)
693 "kerberos 4 debugging",
696 "turn on kerberos 4 debugging" },
699 "printing of hexadecimal network data (debugging)",
702 "print hexadecimal representation of network traffic" },
704 "output of \"netdata\" to user readable format (debugging)",
707 "print user readable output for \"netdata\"" },
709 "viewing of options processing (debugging)",
712 "show option processing" },
714 "printing of hexadecimal terminal data (debugging)",
717 "print hexadecimal representation of terminal traffic" },
730 struct togglelist *c;
732 for (c = Togglelist; c->name; c++) {
735 printf("%-15s toggle %s\r\n", c->name, c->help);
741 printf("%-15s %s\r\n", "?", "display help information");
746 settogglehelp(int set)
748 struct togglelist *c;
750 for (c = Togglelist; c->name; c++) {
753 printf("%-15s %s %s\r\n", c->name, set ? "enable" : "disable",
761 #define GETTOGGLE(name) (struct togglelist *) \
762 genget(name, (char **) Togglelist, sizeof(struct togglelist))
765 toggle(int argc, char *argv[])
769 struct togglelist *c;
773 "Need an argument to 'toggle' command. 'toggle ?' for help.\r\n");
782 fprintf(stderr, "'%s': ambiguous argument ('toggle ?' for help).\r\n",
786 fprintf(stderr, "'%s': unknown argument ('toggle ?' for help).\r\n",
791 *c->variable = !*c->variable; /* invert it */
792 if (c->actionexplanation) {
793 printf("%s %s.\r\n", *c->variable? "Will" : "Won't",
794 c->actionexplanation);
798 retval &= (*c->handler)(-1);
806 * The following perform the "set" command.
809 struct termios new_tc = { 0 };
812 char *name; /* name */
813 char *help; /* help information */
815 cc_t *charp; /* where it is located at */
818 static struct setlist Setlist[] = {
819 #ifdef KLUDGELINEMODE
820 { "echo", "character to toggle local echoing on/off", 0, &echoc },
822 { "escape", "character to escape back to telnet command mode", 0, &escape },
823 { "rlogin", "rlogin escape character", 0, &rlogin },
824 { "tracefile", "file to write trace information to", SetNetTrace, (cc_t *)NetTraceFile},
826 { " ", "The following need 'localchars' to be toggled true", 0, 0 },
827 { "flushoutput", "character to cause an Abort Output", 0, &termFlushChar },
828 { "interrupt", "character to cause an Interrupt Process", 0, &termIntChar },
829 { "quit", "character to cause an Abort process", 0, &termQuitChar },
830 { "eof", "character to cause an EOF ", 0, &termEofChar },
832 { " ", "The following are for local editing in linemode", 0, 0 },
833 { "erase", "character to use to erase a character", 0, &termEraseChar },
834 { "kill", "character to use to erase a line", 0, &termKillChar },
835 { "lnext", "character to use for literal next", 0, &termLiteralNextChar },
836 { "susp", "character to cause a Suspend Process", 0, &termSuspChar },
837 { "reprint", "character to use for line reprint", 0, &termRprntChar },
838 { "worderase", "character to use to erase a word", 0, &termWerasChar },
839 { "start", "character to use for XON", 0, &termStartChar },
840 { "stop", "character to use for XOFF", 0, &termStopChar },
841 { "forw1", "alternate end of line character", 0, &termForw1Char },
842 { "forw2", "alternate end of line character", 0, &termForw2Char },
843 { "ayt", "alternate AYT character", 0, &termAytChar },
847 static struct setlist *
850 return (struct setlist *)
851 genget(name, (char **) Setlist, sizeof(struct setlist));
855 set_escape_char(char *s)
857 if (rlogin != _POSIX_VDISABLE) {
858 rlogin = (s && *s) ? special(s) : _POSIX_VDISABLE;
859 printf("Telnet rlogin escape character is '%s'.\r\n",
862 escape = (s && *s) ? special(s) : _POSIX_VDISABLE;
863 printf("Telnet escape character is '%s'.\r\n", control(escape));
868 setcmd(int argc, char *argv[])
872 struct togglelist *c;
874 if (argc < 2 || argc > 3) {
875 printf("Format is 'set Name Value'\r\n'set ?' for help.\r\n");
878 if ((argc == 2) && (isprefix(argv[1], "?") || isprefix(argv[1], "help"))) {
879 for (ct = Setlist; ct->name; ct++)
880 printf("%-15s %s\r\n", ct->name, ct->help);
883 printf("%-15s %s\r\n", "?", "display help information");
887 ct = getset(argv[1]);
889 c = GETTOGGLE(argv[1]);
891 fprintf(stderr, "'%s': unknown argument ('set ?' for help).\r\n",
894 } else if (Ambiguous(c)) {
895 fprintf(stderr, "'%s': ambiguous argument ('set ?' for help).\r\n",
900 if ((argc == 2) || (strcmp("on", argv[2]) == 0))
902 else if (strcmp("off", argv[2]) == 0)
905 printf("Format is 'set togglename [on|off]'\r\n'set ?' for help.\r\n");
908 if (c->actionexplanation) {
909 printf("%s %s.\r\n", *c->variable? "Will" : "Won't",
910 c->actionexplanation);
915 } else if (argc != 3) {
916 printf("Format is 'set Name Value'\r\n'set ?' for help.\r\n");
918 } else if (Ambiguous(ct)) {
919 fprintf(stderr, "'%s': ambiguous argument ('set ?' for help).\r\n",
922 } else if (ct->handler) {
923 (*ct->handler)(argv[2]);
924 printf("%s set to \"%s\".\r\n", ct->name, (char *)ct->charp);
926 if (strcmp("off", argv[2])) {
927 value = special(argv[2]);
929 value = _POSIX_VDISABLE;
931 *(ct->charp) = (cc_t)value;
932 printf("%s character is '%s'.\r\n", ct->name, control(*(ct->charp)));
939 unsetcmd(int argc, char *argv[])
942 struct togglelist *c;
947 "Need an argument to 'unset' command. 'unset ?' for help.\r\n");
950 if (isprefix(argv[1], "?") || isprefix(argv[1], "help")) {
951 for (ct = Setlist; ct->name; ct++)
952 printf("%-15s %s\r\n", ct->name, ct->help);
955 printf("%-15s %s\r\n", "?", "display help information");
967 fprintf(stderr, "'%s': unknown argument ('unset ?' for help).\r\n",
970 } else if (Ambiguous(c)) {
971 fprintf(stderr, "'%s': ambiguous argument ('unset ?' for help).\r\n",
977 if (c->actionexplanation) {
978 printf("%s %s.\r\n", *c->variable? "Will" : "Won't",
979 c->actionexplanation);
984 } else if (Ambiguous(ct)) {
985 fprintf(stderr, "'%s': ambiguous argument ('unset ?' for help).\r\n",
988 } else if (ct->handler) {
990 printf("%s reset to \"%s\".\r\n", ct->name, (char *)ct->charp);
992 *(ct->charp) = _POSIX_VDISABLE;
993 printf("%s character is '%s'.\r\n", ct->name, control(*(ct->charp)));
1000 * The following are the data structures and routines for the
1003 #ifdef KLUDGELINEMODE
1009 send_wont(TELOPT_LINEMODE, 1);
1010 send_dont(TELOPT_SGA, 1);
1011 send_dont(TELOPT_ECHO, 1);
1019 #ifdef KLUDGELINEMODE
1021 send_dont(TELOPT_SGA, 1);
1023 send_will(TELOPT_LINEMODE, 1);
1024 send_dont(TELOPT_ECHO, 1);
1031 #ifdef KLUDGELINEMODE
1033 send_do(TELOPT_SGA, 1);
1036 send_wont(TELOPT_LINEMODE, 1);
1037 send_do(TELOPT_ECHO, 1);
1042 dolmmode(int bit, int on)
1046 if (my_want_state_is_wont(TELOPT_LINEMODE)) {
1047 printf("?Need to have LINEMODE option enabled first.\r\n");
1048 printf("'mode ?' for help.\r\n");
1053 c = (linemode | bit);
1055 c = (linemode & ~bit);
1063 return dolmmode(bit, 1);
1067 tn_clearmode(int bit)
1069 return dolmmode(bit, 0);
1073 char *name; /* command name */
1074 char *help; /* help string */
1075 int (*handler)(); /* routine which executes command */
1076 int needconnect; /* Do we need to be connected to execute? */
1080 static int modehelp(void);
1082 static struct modelist ModeList[] = {
1083 { "character", "Disable LINEMODE option", docharmode, 1 },
1084 #ifdef KLUDGELINEMODE
1085 { "", "(or disable obsolete line-by-line mode)", 0 },
1087 { "line", "Enable LINEMODE option", dolinemode, 1 },
1088 #ifdef KLUDGELINEMODE
1089 { "", "(or enable obsolete line-by-line mode)", 0 },
1092 { "", "These require the LINEMODE option to be enabled", 0 },
1093 { "isig", "Enable signal trapping", tn_setmode, 1, MODE_TRAPSIG },
1094 { "+isig", 0, tn_setmode, 1, MODE_TRAPSIG },
1095 { "-isig", "Disable signal trapping", tn_clearmode, 1, MODE_TRAPSIG },
1096 { "edit", "Enable character editing", tn_setmode, 1, MODE_EDIT },
1097 { "+edit", 0, tn_setmode, 1, MODE_EDIT },
1098 { "-edit", "Disable character editing", tn_clearmode, 1, MODE_EDIT },
1099 { "softtabs", "Enable tab expansion", tn_setmode, 1, MODE_SOFT_TAB },
1100 { "+softtabs", 0, tn_setmode, 1, MODE_SOFT_TAB },
1101 { "-softtabs", "Disable tab expansion", tn_clearmode, 1, MODE_SOFT_TAB },
1102 { "litecho", "Enable literal character echo", tn_setmode, 1, MODE_LIT_ECHO },
1103 { "+litecho", 0, tn_setmode, 1, MODE_LIT_ECHO },
1104 { "-litecho", "Disable literal character echo", tn_clearmode, 1, MODE_LIT_ECHO },
1105 { "help", 0, modehelp, 0 },
1106 #ifdef KLUDGELINEMODE
1107 { "kludgeline", 0, dokludgemode, 1 },
1110 { "?", "Print help information", modehelp, 0 },
1118 struct modelist *mt;
1120 printf("format is: 'mode Mode', where 'Mode' is one of:\r\n\r\n");
1121 for (mt = ModeList; mt->name; mt++) {
1124 printf("%-15s %s\r\n", mt->name, mt->help);
1132 #define GETMODECMD(name) (struct modelist *) \
1133 genget(name, (char **) ModeList, sizeof(struct modelist))
1136 modecmd(int argc, char **argv)
1138 struct modelist *mt;
1141 printf("'mode' command requires an argument\r\n");
1142 printf("'mode ?' for help.\r\n");
1143 } else if ((mt = GETMODECMD(argv[1])) == 0) {
1144 fprintf(stderr, "Unknown mode '%s' ('mode ?' for help).\r\n", argv[1]);
1145 } else if (Ambiguous(mt)) {
1146 fprintf(stderr, "Ambiguous mode '%s' ('mode ?' for help).\r\n", argv[1]);
1147 } else if (mt->needconnect && !connected) {
1148 printf("?Need to be connected first.\r\n");
1149 printf("'mode ?' for help.\r\n");
1150 } else if (mt->handler) {
1151 return (*mt->handler)(mt->arg1);
1157 * The following data structures and routines implement the
1158 * "display" command.
1162 display(int argc, char *argv[])
1164 struct togglelist *tl;
1167 #define dotog(tl) if (tl->variable && tl->actionexplanation) { \
1168 if (*tl->variable) { \
1173 printf(" %s.\r\n", tl->actionexplanation); \
1176 #define doset(sl) if (sl->name && *sl->name != ' ') { \
1177 if (sl->handler == 0) \
1178 printf("%-15s [%s]\r\n", sl->name, control(*sl->charp)); \
1180 printf("%-15s \"%s\"\r\n", sl->name, (char *)sl->charp); \
1184 for (tl = Togglelist; tl->name; tl++) {
1188 for (sl = Setlist; sl->name; sl++) {
1194 for (i = 1; i < argc; i++) {
1195 sl = getset(argv[i]);
1196 tl = GETTOGGLE(argv[i]);
1197 if (Ambiguous(sl) || Ambiguous(tl)) {
1198 printf("?Ambiguous argument '%s'.\r\n", argv[i]);
1200 } else if (!sl && !tl) {
1201 printf("?Unknown argument '%s'.\r\n", argv[i]);
1213 /*@*/optionstatus();
1214 #if defined(ENCRYPTION)
1223 * The following are the data structures, and many of the routines,
1224 * relating to command processing.
1228 * Set the escape character.
1231 setescape(int argc, char *argv[])
1237 "Deprecated usage - please use 'set escape%s%s' in the future.\r\n",
1238 (argc > 2)? " ":"", (argc > 2)? argv[1]: "");
1242 printf("new escape character: ");
1243 fgets(buf, sizeof(buf), stdin);
1248 printf("Escape character is '%s'.\r\n", control(escape));
1258 printf("Deprecated usage - please use 'toggle crmod' in the future.\r\n");
1259 printf("%s map carriage return on output.\r\n", crmod ? "Will" : "Won't");
1270 long oldrows, oldcols, newrows, newcols, err;
1272 err = (TerminalWindowSize(&oldrows, &oldcols) == 0) ? 1 : 0;
1275 * If we didn't get the window size before the SUSPEND, but we
1276 * can get them now (?), then send the NAWS to make sure that
1277 * we are set up for the right window size.
1279 if (TerminalWindowSize(&newrows, &newcols) && connected &&
1280 (err || ((oldrows != newrows) || (oldcols != newcols)))) {
1284 /* reget parameters in case they were changed */
1285 TerminalSaveState();
1288 printf("Suspend is not supported. Try the '!' command instead\r\n");
1294 shell(int argc, char **argv)
1296 long oldrows, oldcols, newrows, newcols, err;
1300 err = (TerminalWindowSize(&oldrows, &oldcols) == 0) ? 1 : 0;
1303 perror("Fork failed\r\n");
1309 * Fire up the shell in the child.
1311 char *shellp, *shellname;
1313 shellp = getenv("SHELL");
1316 if ((shellname = strrchr(shellp, '/')) == 0)
1321 execl(shellp, shellname, "-c", &saveline[1], NULL);
1323 execl(shellp, shellname, NULL);
1328 wait((int *)0); /* Wait for the shell to complete */
1330 if (TerminalWindowSize(&newrows, &newcols) && connected &&
1331 (err || ((oldrows != newrows) || (oldcols != newcols)))) {
1340 bye(int argc, char **argv)
1344 printf("Connection closed.\r\n");
1348 #if defined(AUTHENTICATION) || defined(ENCRYPTION)
1349 auth_encrypt_connect(connected);
1354 if ((argc != 2) || (strcmp(argv[1], "fromquit") != 0))
1355 longjmp(toplevel, 1);
1356 return 0; /* NOTREACHED */
1362 call(bye, "bye", "fromquit", 0);
1364 return 0; /*NOTREACHED*/
1370 send_do(TELOPT_LOGOUT, 1);
1387 static void slc_help(void);
1389 struct slclist SlcList[] = {
1390 { "export", "Use local special character definitions",
1391 slc_mode_export, 0 },
1392 { "import", "Use remote special character definitions",
1393 slc_mode_import, 1 },
1394 { "check", "Verify remote special character definitions",
1395 slc_mode_import, 0 },
1396 { "help", 0, slc_help, 0 },
1397 { "?", "Print help information", slc_help, 0 },
1406 for (c = SlcList; c->name; c++) {
1409 printf("%-15s %s\r\n", c->name, c->help);
1416 static struct slclist *
1419 return (struct slclist *)
1420 genget(name, (char **) SlcList, sizeof(struct slclist));
1424 slccmd(int argc, char **argv)
1430 "Need an argument to 'slc' command. 'slc ?' for help.\r\n");
1433 c = getslc(argv[1]);
1435 fprintf(stderr, "'%s': unknown argument ('slc ?' for help).\r\n",
1440 fprintf(stderr, "'%s': ambiguous argument ('slc ?' for help).\r\n",
1444 (*c->handler)(c->arg);
1450 * The ENVIRON command.
1460 static void env_help (void);
1462 struct envlist EnvList[] = {
1463 { "define", "Define an environment variable",
1464 (void (*)())env_define, 2 },
1465 { "undefine", "Undefine an environment variable",
1467 { "export", "Mark an environment variable for automatic export",
1469 { "unexport", "Don't mark an environment variable for automatic export",
1471 { "send", "Send an environment variable", env_send, 1 },
1472 { "list", "List the current environment variables",
1474 { "help", 0, env_help, 0 },
1475 { "?", "Print help information", env_help, 0 },
1484 for (c = EnvList; c->name; c++) {
1487 printf("%-15s %s\r\n", c->name, c->help);
1494 static struct envlist *
1495 getenvcmd(char *name)
1497 return (struct envlist *)
1498 genget(name, (char **) EnvList, sizeof(struct envlist));
1502 env_cmd(int argc, char **argv)
1508 "Need an argument to 'environ' command. 'environ ?' for help.\r\n");
1511 c = getenvcmd(argv[1]);
1513 fprintf(stderr, "'%s': unknown argument ('environ ?' for help).\r\n",
1518 fprintf(stderr, "'%s': ambiguous argument ('environ ?' for help).\r\n",
1522 if (c->narg + 2 != argc) {
1524 "Need %s%d argument%s to 'environ %s' command. 'environ ?' for help.\r\n",
1525 c->narg < argc + 2 ? "only " : "",
1526 c->narg, c->narg == 1 ? "" : "s", c->name);
1529 (*c->handler)(argv[2], argv[3]);
1534 struct env_lst *next; /* pointer to next structure */
1535 struct env_lst *prev; /* pointer to previous structure */
1536 unsigned char *var; /* pointer to variable name */
1537 unsigned char *value; /* pointer to variable value */
1538 int export; /* 1 -> export with default list of variables */
1539 int welldefined; /* A well defined variable */
1542 struct env_lst envlisthead;
1545 env_find(unsigned char *var)
1549 for (ep = envlisthead.next; ep; ep = ep->next) {
1550 if (strcmp((char *)ep->var, (char *)var) == 0)
1557 #define environ _environ
1566 for (epp = environ; *epp; epp++) {
1567 if ((cp = strchr(*epp, '='))) {
1569 ep = env_define((unsigned char *)*epp,
1570 (unsigned char *)cp+1);
1576 * Special case for DISPLAY variable. If it is ":0.0" or
1577 * "unix:0.0", we have to get rid of "unix" and insert our
1580 if ((ep = env_find((unsigned char*)"DISPLAY"))
1581 && (*ep->value == ':'
1582 || strncmp((char *)ep->value, "unix:", 5) == 0)) {
1584 char *cp2 = strchr((char *)ep->value, ':');
1587 /* XXX - should be k_gethostname? */
1588 gethostname(hbuf, 256);
1591 /* If this is not the full name, try to get it via DNS */
1592 if (strchr(hbuf, '.') == 0) {
1593 struct addrinfo hints, *ai, *a;
1595 memset (&hints, 0, sizeof(hints));
1596 hints.ai_flags = AI_CANONNAME;
1598 error = getaddrinfo (hbuf, NULL, &hints, &ai);
1600 for (a = ai; a != NULL; a = a->ai_next)
1601 if (a->ai_canonname != NULL) {
1611 error = asprintf (&cp, "%s%s", hbuf, cp2);
1614 ep->value = (unsigned char *)cp;
1618 * If USER is not defined, but LOGNAME is, then add
1619 * USER with the value from LOGNAME. By default, we
1620 * don't export the USER variable.
1622 if ((env_find((unsigned char*)"USER") == NULL) &&
1623 (ep = env_find((unsigned char*)"LOGNAME"))) {
1624 env_define((unsigned char *)"USER", ep->value);
1625 env_unexport((unsigned char *)"USER");
1627 env_export((unsigned char *)"DISPLAY");
1628 env_export((unsigned char *)"PRINTER");
1629 env_export((unsigned char *)"XAUTHORITY");
1633 env_define(unsigned char *var, unsigned char *value)
1637 if ((ep = env_find(var))) {
1643 ep = (struct env_lst *)malloc(sizeof(struct env_lst));
1644 ep->next = envlisthead.next;
1645 envlisthead.next = ep;
1646 ep->prev = &envlisthead;
1648 ep->next->prev = ep;
1650 ep->welldefined = opt_welldefined((char *)var);
1652 ep->var = (unsigned char *)strdup((char *)var);
1653 ep->value = (unsigned char *)strdup((char *)value);
1658 env_undefine(unsigned char *var)
1662 if ((ep = env_find(var))) {
1663 ep->prev->next = ep->next;
1665 ep->next->prev = ep->prev;
1675 env_export(unsigned char *var)
1679 if ((ep = env_find(var)))
1684 env_unexport(unsigned char *var)
1688 if ((ep = env_find(var)))
1693 env_send(unsigned char *var)
1697 if (my_state_is_wont(TELOPT_NEW_ENVIRON)
1699 && my_state_is_wont(TELOPT_OLD_ENVIRON)
1703 "Cannot send '%s': Telnet ENVIRON option not enabled\r\n",
1709 fprintf(stderr, "Cannot send '%s': variable not defined\r\n",
1713 env_opt_start_info();
1714 env_opt_add(ep->var);
1723 for (ep = envlisthead.next; ep; ep = ep->next) {
1724 printf("%c %-20s %s\r\n", ep->export ? '*' : ' ',
1725 ep->var, ep->value);
1730 env_default(int init, int welldefined)
1732 static struct env_lst *nep = NULL;
1739 while ((nep = nep->next)) {
1740 if (nep->export && (nep->welldefined == welldefined))
1748 env_getvalue(unsigned char *var)
1752 if ((ep = env_find(var)))
1758 #if defined(AUTHENTICATION)
1760 * The AUTHENTICATE command.
1773 struct authlist AuthList[] = {
1774 { "status", "Display current status of authentication information",
1776 { "disable", "Disable an authentication type ('auth disable ?' for more)",
1778 { "enable", "Enable an authentication type ('auth enable ?' for more)",
1780 { "help", 0, auth_help, 0 },
1781 { "?", "Print help information", auth_help, 0 },
1790 for (c = AuthList; c->name; c++) {
1793 printf("%-15s %s\r\n", c->name, c->help);
1802 auth_cmd(int argc, char **argv)
1808 "Need an argument to 'auth' command. 'auth ?' for help.\r\n");
1812 c = (struct authlist *)
1813 genget(argv[1], (char **) AuthList, sizeof(struct authlist));
1815 fprintf(stderr, "'%s': unknown argument ('auth ?' for help).\r\n",
1820 fprintf(stderr, "'%s': ambiguous argument ('auth ?' for help).\r\n",
1824 if (c->narg + 2 != argc) {
1826 "Need %s%d argument%s to 'auth %s' command. 'auth ?' for help.\r\n",
1827 c->narg < argc + 2 ? "only " : "",
1828 c->narg, c->narg == 1 ? "" : "s", c->name);
1831 return((*c->handler)(argv[2], argv[3]));
1836 #if defined(ENCRYPTION)
1838 * The ENCRYPT command.
1841 struct encryptlist {
1853 struct encryptlist EncryptList[] = {
1854 { "enable", "Enable encryption. ('encrypt enable ?' for more)",
1855 EncryptEnable, 1, 1, 2 },
1856 { "disable", "Disable encryption. ('encrypt enable ?' for more)",
1857 EncryptDisable, 0, 1, 2 },
1858 { "type", "Set encryptiong type. ('encrypt type ?' for more)",
1859 EncryptType, 0, 1, 1 },
1860 { "start", "Start encryption. ('encrypt start ?' for more)",
1861 EncryptStart, 1, 0, 1 },
1862 { "stop", "Stop encryption. ('encrypt stop ?' for more)",
1863 EncryptStop, 1, 0, 1 },
1864 { "input", "Start encrypting the input stream",
1865 EncryptStartInput, 1, 0, 0 },
1866 { "-input", "Stop encrypting the input stream",
1867 EncryptStopInput, 1, 0, 0 },
1868 { "output", "Start encrypting the output stream",
1869 EncryptStartOutput, 1, 0, 0 },
1870 { "-output", "Stop encrypting the output stream",
1871 EncryptStopOutput, 1, 0, 0 },
1873 { "status", "Display current status of authentication information",
1874 EncryptStatus, 0, 0, 0 },
1875 { "help", 0, EncryptHelp, 0, 0, 0 },
1876 { "?", "Print help information", EncryptHelp, 0, 0, 0 },
1883 struct encryptlist *c;
1885 for (c = EncryptList; c->name; c++) {
1888 printf("%-15s %s\r\n", c->name, c->help);
1897 encrypt_cmd(int argc, char **argv)
1899 struct encryptlist *c;
1901 c = (struct encryptlist *)
1902 genget(argv[1], (char **) EncryptList, sizeof(struct encryptlist));
1904 fprintf(stderr, "'%s': unknown argument ('encrypt ?' for help).\r\n",
1909 fprintf(stderr, "'%s': ambiguous argument ('encrypt ?' for help).\r\n",
1914 if (argc < c->minarg || argc > c->maxarg) {
1915 if (c->minarg == c->maxarg) {
1916 fprintf(stderr, "Need %s%d argument%s ",
1917 c->minarg < argc ? "only " : "", c->minarg,
1918 c->minarg == 1 ? "" : "s");
1920 fprintf(stderr, "Need %s%d-%d arguments ",
1921 c->maxarg < argc ? "only " : "", c->minarg, c->maxarg);
1923 fprintf(stderr, "to 'encrypt %s' command. 'encrypt ?' for help.\r\n",
1927 if (c->needconnect && !connected) {
1928 if (!(argc && (isprefix(argv[2], "help") || isprefix(argv[2], "?")))) {
1929 printf("?Need to be connected first.\r\n");
1933 return ((*c->handler)(argc > 0 ? argv[2] : 0,
1934 argc > 1 ? argv[3] : 0,
1935 argc > 2 ? argv[4] : 0));
1941 * Print status about the connection.
1945 status(int argc, char **argv)
1948 printf("Connected to %s.\r\n", hostname);
1949 if ((argc < 2) || strcmp(argv[1], "notmuch")) {
1950 int mode = getconnmode();
1952 if (my_want_state_is_will(TELOPT_LINEMODE)) {
1953 printf("Operating with LINEMODE option\r\n");
1954 printf("%s line editing\r\n", (mode&MODE_EDIT) ? "Local" : "No");
1955 printf("%s catching of signals\r\n",
1956 (mode&MODE_TRAPSIG) ? "Local" : "No");
1958 #ifdef KLUDGELINEMODE
1959 } else if (kludgelinemode && my_want_state_is_dont(TELOPT_SGA)) {
1960 printf("Operating in obsolete linemode\r\n");
1963 printf("Operating in single character mode\r\n");
1965 printf("Catching signals locally\r\n");
1967 printf("%s character echo\r\n", (mode&MODE_ECHO) ? "Local" : "Remote");
1968 if (my_want_state_is_will(TELOPT_LFLOW))
1969 printf("%s flow control\r\n", (mode&MODE_FLOW) ? "Local" : "No");
1970 #if defined(ENCRYPTION)
1975 printf("No connection.\r\n");
1977 printf("Escape character is '%s'.\r\n", control(escape));
1984 * Function that gets called when SIGINFO is received.
1987 ayt_status(int ignore)
1989 call(status, "status", "notmuch", 0);
1993 static Command *getcmd(char *name);
1996 cmdrc(char *m1, char *m2)
1998 static char rcname[128];
2002 int l1 = strlen(m1);
2003 int l2 = strlen(m2);
2009 strlcpy(m1save, m1, sizeof(m1save));
2012 if (rcname[0] == 0) {
2013 char *home = getenv("HOME");
2015 snprintf (rcname, sizeof(rcname), "%s/.telnetrc",
2019 if ((rcfile = fopen(rcname, "r")) == 0) {
2024 if (fgets(line, sizeof(line), rcfile) == NULL)
2031 if (!isspace((unsigned char)line[0]))
2034 if (gotmachine == 0) {
2035 if (isspace((unsigned char)line[0]))
2037 if (strncasecmp(line, m1, l1) == 0)
2038 strncpy(line, &line[l1], sizeof(line) - l1);
2039 else if (strncasecmp(line, m2, l2) == 0)
2040 strncpy(line, &line[l2], sizeof(line) - l2);
2041 else if (strncasecmp(line, "DEFAULT", 7) == 0)
2042 strncpy(line, &line[7], sizeof(line) - 7);
2045 if (line[0] != ' ' && line[0] != '\t' && line[0] != '\n')
2052 c = getcmd(margv[0]);
2054 printf("?Ambiguous command: %s\r\n", margv[0]);
2058 printf("?Invalid command: %s\r\n", margv[0]);
2062 * This should never happen...
2064 if (c->needconnect && !connected) {
2065 printf("?Need to be connected first for %s.\r\n", margv[0]);
2068 (*c->handler)(margc, margv);
2074 tn(int argc, char **argv)
2076 struct servent *sp = 0;
2077 char *cmd, *hostp = 0, *portp = 0;
2081 /* clear the socket address prior to use */
2084 printf("?Already connected to %s\r\n", hostname);
2088 strlcpy(line, "open ", sizeof(line));
2090 fgets(&line[strlen(line)], sizeof(line) - strlen(line), stdin);
2098 if (strcmp(*argv, "help") == 0 || isprefix(*argv, "?"))
2100 if (strcmp(*argv, "-l") == 0) {
2104 user = strdup(*argv++);
2108 if (strcmp(*argv, "-a") == 0) {
2124 printf("usage: %s [-l user] [-a] host-name [port]\r\n", cmd);
2130 strlcpy (_hostname, hostp, sizeof(_hostname));
2132 if (hostp[0] == '@' || hostp[0] == '!') {
2135 for (p = hostp + 1; *p; p++) {
2136 if (*p == ',' || *p == '@')
2139 if (hostname == NULL) {
2140 fprintf(stderr, "%s: bad source route specification\n", hostp);
2148 if (*portp == '-') {
2155 sp = roken_getservbyname(portp, "tcp");
2159 printf("%s: bad port number\r\n", portp);
2167 sp = roken_getservbyname("telnet", "tcp");
2169 fprintf(stderr, "telnet: tcp/telnet: unknown service\r\n");
2178 struct addrinfo *ai, *a, hints;
2180 char portstr[NI_MAXSERV];
2182 memset (&hints, 0, sizeof(hints));
2183 hints.ai_socktype = SOCK_STREAM;
2184 hints.ai_protocol = IPPROTO_TCP;
2185 hints.ai_flags = AI_CANONNAME;
2187 snprintf (portstr, sizeof(portstr), "%u", ntohs(port));
2189 error = getaddrinfo (hostname, portstr, &hints, &ai);
2191 fprintf (stderr, "%s: %s\r\n", hostname, gai_strerror (error));
2195 for (a = ai; a != NULL && connected == 0; a = a->ai_next) {
2198 if (a->ai_canonname != NULL)
2199 strlcpy (_hostname, a->ai_canonname, sizeof(_hostname));
2201 if (getnameinfo (a->ai_addr, a->ai_addrlen,
2202 addrstr, sizeof(addrstr),
2203 NULL, 0, NI_NUMERICHOST) != 0)
2204 strlcpy (addrstr, "unknown address", sizeof(addrstr));
2206 printf("Trying %s...\r\n", addrstr);
2208 net = socket (a->ai_family, a->ai_socktype, a->ai_protocol);
2214 #if defined(IP_OPTIONS) && defined(IPPROTO_IP) && defined(HAVE_SETSOCKOPT)
2215 if (hostp[0] == '@' || hostp[0] == '!') {
2220 if ((srlen = sourceroute(a, hostp, &srp, &proto, &opt)) < 0) {
2221 (void) NetClose(net);
2225 if (srp && setsockopt(net, proto, opt, srp, srlen) < 0)
2226 perror("setsockopt (source route)");
2230 #if defined(IPPROTO_IP) && defined(IP_TOS)
2231 if (a->ai_family == AF_INET) {
2232 # if defined(HAVE_GETTOSBYNAME)
2234 if (tos < 0 && (tp = gettosbyname("telnet", "tcp")))
2238 tos = 020; /* Low Delay bit */
2240 && (setsockopt(net, IPPROTO_IP, IP_TOS,
2241 (void *)&tos, sizeof(int)) < 0)
2242 && (errno != ENOPROTOOPT))
2243 perror("telnet: setsockopt (IP_TOS) (ignored)");
2245 #endif /* defined(IPPROTO_IP) && defined(IP_TOS) */
2246 if (debug && SetSockOpt(net, SOL_SOCKET, SO_DEBUG, 1) < 0) {
2247 perror("setsockopt (SO_DEBUG)");
2250 if (connect (net, a->ai_addr, a->ai_addrlen) < 0) {
2251 fprintf (stderr, "telnet: connect to address %s: %s\n",
2252 addrstr, strerror(errno));
2254 if (a->ai_next != NULL) {
2262 #if defined(AUTHENTICATION) || defined(ENCRYPTION)
2263 auth_encrypt_connect(connected);
2270 cmdrc(hostp, hostname);
2271 set_forward_options();
2272 if (autologin && user == NULL)
2273 user = (char *)get_default_username ();
2275 env_define((unsigned char *)"USER", (unsigned char *)user);
2276 env_export((unsigned char *)"USER");
2278 call(status, "status", "notmuch", 0);
2279 if (setjmp(peerdied) == 0)
2280 my_telnet((char *)user);
2282 ExitString("Connection closed by foreign host.\r\n",1);
2287 #define HELPINDENT ((int)sizeof ("connect"))
2290 openhelp[] = "connect to a site",
2291 closehelp[] = "close current connection",
2292 logouthelp[] = "forcibly logout remote user and close the connection",
2293 quithelp[] = "exit telnet",
2294 statushelp[] = "print status information",
2295 helphelp[] = "print help information",
2296 sendhelp[] = "transmit special characters ('send ?' for more)",
2297 sethelp[] = "set operating parameters ('set ?' for more)",
2298 unsethelp[] = "unset operating parameters ('unset ?' for more)",
2299 togglestring[] ="toggle operating parameters ('toggle ?' for more)",
2300 slchelp[] = "change state of special charaters ('slc ?' for more)",
2301 displayhelp[] = "display operating parameters",
2302 #if defined(AUTHENTICATION)
2303 authhelp[] = "turn on (off) authentication ('auth ?' for more)",
2305 #if defined(ENCRYPTION)
2306 encrypthelp[] = "turn on (off) encryption ('encrypt ?' for more)",
2308 zhelp[] = "suspend telnet",
2309 shellhelp[] = "invoke a subshell",
2310 envhelp[] = "change environment variables ('environ ?' for more)",
2311 modestring[] = "try to enter line or character mode ('mode ?' for more)";
2313 static int help(int argc, char **argv);
2315 static Command cmdtab[] = {
2316 { "close", closehelp, bye, 1 },
2317 { "logout", logouthelp, logout, 1 },
2318 { "display", displayhelp, display, 0 },
2319 { "mode", modestring, modecmd, 0 },
2320 { "open", openhelp, tn, 0 },
2321 { "quit", quithelp, quit, 0 },
2322 { "send", sendhelp, sendcmd, 0 },
2323 { "set", sethelp, setcmd, 0 },
2324 { "unset", unsethelp, unsetcmd, 0 },
2325 { "status", statushelp, status, 0 },
2326 { "toggle", togglestring, toggle, 0 },
2327 { "slc", slchelp, slccmd, 0 },
2328 #if defined(AUTHENTICATION)
2329 { "auth", authhelp, auth_cmd, 0 },
2331 #if defined(ENCRYPTION)
2332 { "encrypt", encrypthelp, encrypt_cmd, 0 },
2334 { "z", zhelp, telnetsuspend, 0 },
2335 { "!", shellhelp, shell, 0 },
2336 { "environ", envhelp, env_cmd, 0 },
2337 { "?", helphelp, help, 0 },
2341 static char crmodhelp[] = "deprecated command -- use 'toggle crmod' instead";
2342 static char escapehelp[] = "deprecated command -- use 'set escape' instead";
2344 static Command cmdtab2[] = {
2345 { "help", 0, help, 0 },
2346 { "escape", escapehelp, setescape, 0 },
2347 { "crmod", crmodhelp, togcrmod, 0 },
2353 * Call routine with argc, argv set from args (terminated by 0).
2357 call(intrtn_t routine, ...)
2363 va_start(ap, routine);
2364 while ((args[argno++] = va_arg(ap, char *)) != 0);
2366 return (*routine)(argno-1, args);
2375 if ((cm = (Command *) genget(name, (char **) cmdtab, sizeof(Command))))
2377 return (Command *) genget(name, (char **) cmdtab2, sizeof(Command));
2381 command(int top, char *tbuf, int cnt)
2389 signal(SIGINT, SIG_DFL);
2390 signal(SIGQUIT, SIG_DFL);
2393 if (rlogin == _POSIX_VDISABLE)
2394 printf("%s> ", prompt);
2398 while (cnt > 0 && (*cp++ = *tbuf++) != '\n')
2401 if (cp == line || *--cp != '\n' || cp == line)
2404 if (rlogin == _POSIX_VDISABLE)
2405 printf("%s\r\n", line);
2408 if (rlogin != _POSIX_VDISABLE)
2409 printf("%s> ", prompt);
2410 if (fgets(line, sizeof(line), stdin) == NULL) {
2411 if (feof(stdin) || ferror(stdin)) {
2421 if (margv[0] == 0) {
2424 c = getcmd(margv[0]);
2426 printf("?Ambiguous command\r\n");
2430 printf("?Invalid command\r\n");
2433 if (c->needconnect && !connected) {
2434 printf("?Need to be connected first.\r\n");
2437 if ((*c->handler)(margc, margv)) {
2443 longjmp(toplevel, 1);
2454 help(int argc, char **argv)
2459 printf("Commands may be abbreviated. Commands are:\r\n\r\n");
2460 for (c = cmdtab; c->name; c++)
2462 printf("%-*s\t%s\r\n", HELPINDENT, c->name,
2467 while (--argc > 0) {
2472 printf("?Ambiguous help command %s\r\n", arg);
2473 else if (c == (Command *)0)
2474 printf("?Invalid help command %s\r\n", arg);
2476 printf("%s\r\n", c->help);
2482 #if defined(IP_OPTIONS) && defined(IPPROTO_IP)
2485 * Source route is handed in as
2486 * [!]@hop1@hop2...@dst
2488 * If the leading ! is present, it is a strict source route, otherwise it is
2489 * assmed to be a loose source route. Note that leading ! is effective
2490 * only for IPv4 case.
2492 * We fill in the source route option as
2493 * hop1,hop2,hop3...dest
2494 * and return a pointer to hop1, which will
2495 * be the address to connect() to.
2498 * ai: The address (by struct addrinfo) for the final destination.
2500 * arg: Pointer to route list to decipher
2502 * cpp: Pointer to a pointer, so that sourceroute() can return
2503 * the address of result buffer (statically alloc'ed).
2506 * Pointer to an integer. The pointed variable
2507 * lenp: pointer to an integer that contains the
2508 * length of *cpp if *cpp != NULL.
2512 * Returns the length of the option pointed to by *cpp. If the
2513 * return value is -1, there was a syntax error in the
2514 * option, either arg contained unknown characters or too many hosts,
2515 * or hostname cannot be resolved.
2517 * The caller needs to pass return value (len), *cpp, *protop and *optp
2520 * *cpp: Points to the result buffer. The region is statically
2521 * allocated by the function.
2524 * protocol # to be passed to setsockopt(2).
2526 * *optp: option # to be passed to setsockopt(2).
2530 sourceroute(struct addrinfo *ai,
2536 char *cp, *cp2, *lsrp = NULL, *lsrep = NULL;
2537 struct addrinfo hints, *res;
2539 struct sockaddr_in *sin;
2541 static char lsr[44];
2543 struct cmsghdr *cmsg = NULL;
2544 struct sockaddr_in6 *sin6;
2545 static char rhbuf[1024];
2549 * Verify the arguments.
2557 switch (ai->ai_family) {
2560 lsrep = lsrp + sizeof(lsr);
2563 * Next, decide whether we have a loose source
2564 * route or a strict source route, and fill in
2565 * the begining of the option.
2569 *lsrp++ = IPOPT_SSRR;
2571 *lsrp++ = IPOPT_LSRR;
2574 lsrp++; /* skip over length, we'll fill it in later */
2577 *protop = IPPROTO_IP;
2582 /* this needs to be updated for rfc2292bis */
2583 #ifdef IPV6_PKTOPTIONS
2584 cmsg = inet6_rthdr_init(rhbuf, IPV6_RTHDR_TYPE_0);
2588 *protop = IPPROTO_IPV6;
2589 *optp = IPV6_PKTOPTIONS;
2599 memset(&hints, 0, sizeof(hints));
2600 hints.ai_family = ai->ai_family;
2601 hints.ai_socktype = SOCK_STREAM;
2606 else for (cp2 = cp; (c = *cp2) != '\0'; cp2++) {
2611 } else if (c == '@') {
2614 #if 0 /*colon conflicts with IPv6 address*/
2615 else if (c == ':') {
2626 error = getaddrinfo(cp, NULL, &hints, &res);
2628 fprintf(stderr, "%s: %s\n", cp, gai_strerror(error));
2631 if (ai->ai_family != res->ai_family) {
2635 if (ai->ai_family == AF_INET) {
2637 * Check to make sure there is space for address
2639 if (lsrp + 4 > lsrep) {
2643 sin = (struct sockaddr_in *)res->ai_addr;
2644 memcpy(lsrp, &sin->sin_addr, sizeof(struct in_addr));
2645 lsrp += sizeof(struct in_addr);
2648 else if (ai->ai_family == AF_INET6) {
2649 sin6 = (struct sockaddr_in6 *)res->ai_addr;
2650 inet6_rthdr_add(cmsg, &sin6->sin6_addr,
2664 if (ai->ai_family == AF_INET) {
2665 /* record the last hop */
2666 if (lsrp + 4 > lsrep)
2668 sin = (struct sockaddr_in *)ai->ai_addr;
2669 memcpy(lsrp, &sin->sin_addr, sizeof(struct in_addr));
2670 lsrp += sizeof(struct in_addr);
2672 lsr[IPOPT_OLEN] = lsrp - lsr;
2673 if (lsr[IPOPT_OLEN] <= 7 || lsr[IPOPT_OLEN] > 40)
2675 *lsrp++ = IPOPT_NOP; /*32bit word align*/
2679 ipopt.io_len = lsrp - lsr;
2680 if (ipopt.io_len <= 5) /*is 3 better?*/
2682 *cpp = (char 8)&ipopt;
2686 else if (ai->ai_family == AF_INET6) {
2687 inet6_rthdr_lasthop(cmsg, IPV6_RTHDR_LOOSE);
2688 len = cmsg->cmsg_len;