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"
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");
460 printf("Will send carriage returns as telnet <CR><LF>.\r\n");
462 printf("Will send carriage returns as telnet <CR><NUL>.\r\n");
472 donebinarytoggle = 1;
477 if (my_want_state_is_will(TELOPT_BINARY) &&
478 my_want_state_is_do(TELOPT_BINARY)) {
480 } else if (my_want_state_is_wont(TELOPT_BINARY) &&
481 my_want_state_is_dont(TELOPT_BINARY)) {
484 val = binmode ? 0 : 1;
488 if (my_want_state_is_will(TELOPT_BINARY) &&
489 my_want_state_is_do(TELOPT_BINARY)) {
490 printf("Already operating in binary mode with remote host.\r\n");
492 printf("Negotiating binary mode with remote host.\r\n");
496 if (my_want_state_is_wont(TELOPT_BINARY) &&
497 my_want_state_is_dont(TELOPT_BINARY)) {
498 printf("Already in network ascii mode with remote host.\r\n");
500 printf("Negotiating network ascii mode with remote host.\r\n");
510 donebinarytoggle = 1;
513 val = my_want_state_is_do(TELOPT_BINARY) ? 0 : 1;
516 if (my_want_state_is_do(TELOPT_BINARY)) {
517 printf("Already receiving in binary mode.\r\n");
519 printf("Negotiating binary mode on input.\r\n");
523 if (my_want_state_is_dont(TELOPT_BINARY)) {
524 printf("Already receiving in network ascii mode.\r\n");
526 printf("Negotiating network ascii mode on input.\r\n");
536 donebinarytoggle = 1;
539 val = my_want_state_is_will(TELOPT_BINARY) ? 0 : 1;
542 if (my_want_state_is_will(TELOPT_BINARY)) {
543 printf("Already transmitting in binary mode.\r\n");
545 printf("Negotiating binary mode on output.\r\n");
549 if (my_want_state_is_wont(TELOPT_BINARY)) {
550 printf("Already transmitting in network ascii mode.\r\n");
552 printf("Negotiating network ascii mode on output.\r\n");
560 static int togglehelp (void);
561 #if defined(AUTHENTICATION)
562 extern int auth_togdebug (int);
564 #if defined(ENCRYPTION)
565 extern int EncryptAutoEnc (int);
566 extern int EncryptAutoDec (int);
567 extern int EncryptDebug (int);
568 extern int EncryptVerbose (int);
572 char *name; /* name of toggle */
573 char *help; /* help message */
574 int (*handler)(); /* routine to do actual setting */
576 char *actionexplanation;
579 static struct togglelist Togglelist[] = {
581 "flushing of output when sending interrupt characters",
584 "flush output when sending interrupt characters" },
586 "automatic sending of interrupt characters in urgent mode",
589 "send interrupt characters in urgent mode" },
590 #if defined(AUTHENTICATION)
592 "automatic sending of login and/or authentication info",
595 "send login name and/or authentication information" },
597 "authentication debugging",
600 "print authentication debugging information" },
602 #if defined(ENCRYPTION)
604 "automatic encryption of data stream",
607 "automatically encrypt output" },
609 "automatic decryption of data stream",
612 "automatically decrypt input" },
614 "verbose encryption output",
617 "print verbose encryption output" },
619 "encryption debugging",
622 "print encryption debugging information" },
626 "credentials forwarding",
627 kerberos5_set_forward,
629 "forward credentials" },
631 "forwardable flag of forwarded credentials",
632 kerberos5_set_forwardable,
634 "forward forwardable credentials" },
637 "don't read ~/.telnetrc file",
640 "skip reading of ~/.telnetrc file" },
642 "sending and receiving of binary data",
647 "receiving of binary data",
652 "sending of binary data",
657 "sending carriage returns as telnet <CR><LF>",
662 "mapping of received carriage returns",
665 "map carriage return on output" },
667 "local recognition of certain control characters",
670 "recognize certain control characters" },
671 { " ", "", 0 }, /* empty line */
676 "turn on socket level debugging" },
678 "printing of hexadecimal network data (debugging)",
681 "print hexadecimal representation of network traffic" },
683 "output of \"netdata\" to user readable format (debugging)",
686 "print user readable output for \"netdata\"" },
688 "viewing of options processing (debugging)",
691 "show option processing" },
693 "printing of hexadecimal terminal data (debugging)",
696 "print hexadecimal representation of terminal traffic" },
709 struct togglelist *c;
711 for (c = Togglelist; c->name; c++) {
714 printf("%-15s toggle %s\r\n", c->name, c->help);
720 printf("%-15s %s\r\n", "?", "display help information");
725 settogglehelp(int set)
727 struct togglelist *c;
729 for (c = Togglelist; c->name; c++) {
732 printf("%-15s %s %s\r\n", c->name, set ? "enable" : "disable",
740 #define GETTOGGLE(name) (struct togglelist *) \
741 genget(name, (char **) Togglelist, sizeof(struct togglelist))
744 toggle(int argc, char *argv[])
748 struct togglelist *c;
752 "Need an argument to 'toggle' command. 'toggle ?' for help.\r\n");
761 fprintf(stderr, "'%s': ambiguous argument ('toggle ?' for help).\r\n",
765 fprintf(stderr, "'%s': unknown argument ('toggle ?' for help).\r\n",
770 *c->variable = !*c->variable; /* invert it */
771 if (c->actionexplanation) {
772 printf("%s %s.\r\n", *c->variable? "Will" : "Won't",
773 c->actionexplanation);
777 retval &= (*c->handler)(-1);
785 * The following perform the "set" command.
788 struct termios new_tc = { 0 };
791 char *name; /* name */
792 char *help; /* help information */
794 cc_t *charp; /* where it is located at */
797 static struct setlist Setlist[] = {
798 #ifdef KLUDGELINEMODE
799 { "echo", "character to toggle local echoing on/off", 0, &echoc },
801 { "escape", "character to escape back to telnet command mode", 0, &escape },
802 { "rlogin", "rlogin escape character", 0, &rlogin },
803 { "tracefile", "file to write trace information to", SetNetTrace, (cc_t *)NetTraceFile},
805 { " ", "The following need 'localchars' to be toggled true", 0, 0 },
806 { "flushoutput", "character to cause an Abort Output", 0, &termFlushChar },
807 { "interrupt", "character to cause an Interrupt Process", 0, &termIntChar },
808 { "quit", "character to cause an Abort process", 0, &termQuitChar },
809 { "eof", "character to cause an EOF ", 0, &termEofChar },
811 { " ", "The following are for local editing in linemode", 0, 0 },
812 { "erase", "character to use to erase a character", 0, &termEraseChar },
813 { "kill", "character to use to erase a line", 0, &termKillChar },
814 { "lnext", "character to use for literal next", 0, &termLiteralNextChar },
815 { "susp", "character to cause a Suspend Process", 0, &termSuspChar },
816 { "reprint", "character to use for line reprint", 0, &termRprntChar },
817 { "worderase", "character to use to erase a word", 0, &termWerasChar },
818 { "start", "character to use for XON", 0, &termStartChar },
819 { "stop", "character to use for XOFF", 0, &termStopChar },
820 { "forw1", "alternate end of line character", 0, &termForw1Char },
821 { "forw2", "alternate end of line character", 0, &termForw2Char },
822 { "ayt", "alternate AYT character", 0, &termAytChar },
826 static struct setlist *
829 return (struct setlist *)
830 genget(name, (char **) Setlist, sizeof(struct setlist));
834 set_escape_char(char *s)
836 if (rlogin != _POSIX_VDISABLE) {
837 rlogin = (s && *s) ? special(s) : _POSIX_VDISABLE;
838 printf("Telnet rlogin escape character is '%s'.\r\n",
841 escape = (s && *s) ? special(s) : _POSIX_VDISABLE;
842 printf("Telnet escape character is '%s'.\r\n", control(escape));
847 setcmd(int argc, char *argv[])
851 struct togglelist *c;
853 if (argc < 2 || argc > 3) {
854 printf("Format is 'set Name Value'\r\n'set ?' for help.\r\n");
857 if ((argc == 2) && (isprefix(argv[1], "?") || isprefix(argv[1], "help"))) {
858 for (ct = Setlist; ct->name; ct++)
859 printf("%-15s %s\r\n", ct->name, ct->help);
862 printf("%-15s %s\r\n", "?", "display help information");
866 ct = getset(argv[1]);
868 c = GETTOGGLE(argv[1]);
870 fprintf(stderr, "'%s': unknown argument ('set ?' for help).\r\n",
873 } else if (Ambiguous(c)) {
874 fprintf(stderr, "'%s': ambiguous argument ('set ?' for help).\r\n",
879 if ((argc == 2) || (strcmp("on", argv[2]) == 0))
881 else if (strcmp("off", argv[2]) == 0)
884 printf("Format is 'set togglename [on|off]'\r\n'set ?' for help.\r\n");
887 if (c->actionexplanation) {
888 printf("%s %s.\r\n", *c->variable? "Will" : "Won't",
889 c->actionexplanation);
894 } else if (argc != 3) {
895 printf("Format is 'set Name Value'\r\n'set ?' for help.\r\n");
897 } else if (Ambiguous(ct)) {
898 fprintf(stderr, "'%s': ambiguous argument ('set ?' for help).\r\n",
901 } else if (ct->handler) {
902 (*ct->handler)(argv[2]);
903 printf("%s set to \"%s\".\r\n", ct->name, (char *)ct->charp);
905 if (strcmp("off", argv[2])) {
906 value = special(argv[2]);
908 value = _POSIX_VDISABLE;
910 *(ct->charp) = (cc_t)value;
911 printf("%s character is '%s'.\r\n", ct->name, control(*(ct->charp)));
918 unsetcmd(int argc, char *argv[])
921 struct togglelist *c;
926 "Need an argument to 'unset' command. 'unset ?' for help.\r\n");
929 if (isprefix(argv[1], "?") || isprefix(argv[1], "help")) {
930 for (ct = Setlist; ct->name; ct++)
931 printf("%-15s %s\r\n", ct->name, ct->help);
934 printf("%-15s %s\r\n", "?", "display help information");
946 fprintf(stderr, "'%s': unknown argument ('unset ?' for help).\r\n",
949 } else if (Ambiguous(c)) {
950 fprintf(stderr, "'%s': ambiguous argument ('unset ?' for help).\r\n",
956 if (c->actionexplanation) {
957 printf("%s %s.\r\n", *c->variable? "Will" : "Won't",
958 c->actionexplanation);
963 } else if (Ambiguous(ct)) {
964 fprintf(stderr, "'%s': ambiguous argument ('unset ?' for help).\r\n",
967 } else if (ct->handler) {
969 printf("%s reset to \"%s\".\r\n", ct->name, (char *)ct->charp);
971 *(ct->charp) = _POSIX_VDISABLE;
972 printf("%s character is '%s'.\r\n", ct->name, control(*(ct->charp)));
979 * The following are the data structures and routines for the
982 #ifdef KLUDGELINEMODE
988 send_wont(TELOPT_LINEMODE, 1);
989 send_dont(TELOPT_SGA, 1);
990 send_dont(TELOPT_ECHO, 1);
998 #ifdef KLUDGELINEMODE
1000 send_dont(TELOPT_SGA, 1);
1002 send_will(TELOPT_LINEMODE, 1);
1003 send_dont(TELOPT_ECHO, 1);
1010 #ifdef KLUDGELINEMODE
1012 send_do(TELOPT_SGA, 1);
1015 send_wont(TELOPT_LINEMODE, 1);
1016 send_do(TELOPT_ECHO, 1);
1021 dolmmode(int bit, int on)
1025 if (my_want_state_is_wont(TELOPT_LINEMODE)) {
1026 printf("?Need to have LINEMODE option enabled first.\r\n");
1027 printf("'mode ?' for help.\r\n");
1032 c = (linemode | bit);
1034 c = (linemode & ~bit);
1042 return dolmmode(bit, 1);
1046 tn_clearmode(int bit)
1048 return dolmmode(bit, 0);
1052 char *name; /* command name */
1053 char *help; /* help string */
1054 int (*handler)(); /* routine which executes command */
1055 int needconnect; /* Do we need to be connected to execute? */
1059 static int modehelp(void);
1061 static struct modelist ModeList[] = {
1062 { "character", "Disable LINEMODE option", docharmode, 1 },
1063 #ifdef KLUDGELINEMODE
1064 { "", "(or disable obsolete line-by-line mode)", 0 },
1066 { "line", "Enable LINEMODE option", dolinemode, 1 },
1067 #ifdef KLUDGELINEMODE
1068 { "", "(or enable obsolete line-by-line mode)", 0 },
1071 { "", "These require the LINEMODE option to be enabled", 0 },
1072 { "isig", "Enable signal trapping", tn_setmode, 1, MODE_TRAPSIG },
1073 { "+isig", 0, tn_setmode, 1, MODE_TRAPSIG },
1074 { "-isig", "Disable signal trapping", tn_clearmode, 1, MODE_TRAPSIG },
1075 { "edit", "Enable character editing", tn_setmode, 1, MODE_EDIT },
1076 { "+edit", 0, tn_setmode, 1, MODE_EDIT },
1077 { "-edit", "Disable character editing", tn_clearmode, 1, MODE_EDIT },
1078 { "softtabs", "Enable tab expansion", tn_setmode, 1, MODE_SOFT_TAB },
1079 { "+softtabs", 0, tn_setmode, 1, MODE_SOFT_TAB },
1080 { "-softtabs", "Disable tab expansion", tn_clearmode, 1, MODE_SOFT_TAB },
1081 { "litecho", "Enable literal character echo", tn_setmode, 1, MODE_LIT_ECHO },
1082 { "+litecho", 0, tn_setmode, 1, MODE_LIT_ECHO },
1083 { "-litecho", "Disable literal character echo", tn_clearmode, 1, MODE_LIT_ECHO },
1084 { "help", 0, modehelp, 0 },
1085 #ifdef KLUDGELINEMODE
1086 { "kludgeline", 0, dokludgemode, 1 },
1089 { "?", "Print help information", modehelp, 0 },
1097 struct modelist *mt;
1099 printf("format is: 'mode Mode', where 'Mode' is one of:\r\n\r\n");
1100 for (mt = ModeList; mt->name; mt++) {
1103 printf("%-15s %s\r\n", mt->name, mt->help);
1111 #define GETMODECMD(name) (struct modelist *) \
1112 genget(name, (char **) ModeList, sizeof(struct modelist))
1115 modecmd(int argc, char **argv)
1117 struct modelist *mt;
1120 printf("'mode' command requires an argument\r\n");
1121 printf("'mode ?' for help.\r\n");
1122 } else if ((mt = GETMODECMD(argv[1])) == 0) {
1123 fprintf(stderr, "Unknown mode '%s' ('mode ?' for help).\r\n", argv[1]);
1124 } else if (Ambiguous(mt)) {
1125 fprintf(stderr, "Ambiguous mode '%s' ('mode ?' for help).\r\n", argv[1]);
1126 } else if (mt->needconnect && !connected) {
1127 printf("?Need to be connected first.\r\n");
1128 printf("'mode ?' for help.\r\n");
1129 } else if (mt->handler) {
1130 return (*mt->handler)(mt->arg1);
1136 * The following data structures and routines implement the
1137 * "display" command.
1141 display(int argc, char *argv[])
1143 struct togglelist *tl;
1146 #define dotog(tl) if (tl->variable && tl->actionexplanation) { \
1147 if (*tl->variable) { \
1152 printf(" %s.\r\n", tl->actionexplanation); \
1155 #define doset(sl) if (sl->name && *sl->name != ' ') { \
1156 if (sl->handler == 0) \
1157 printf("%-15s [%s]\r\n", sl->name, control(*sl->charp)); \
1159 printf("%-15s \"%s\"\r\n", sl->name, (char *)sl->charp); \
1163 for (tl = Togglelist; tl->name; tl++) {
1167 for (sl = Setlist; sl->name; sl++) {
1173 for (i = 1; i < argc; i++) {
1174 sl = getset(argv[i]);
1175 tl = GETTOGGLE(argv[i]);
1176 if (Ambiguous(sl) || Ambiguous(tl)) {
1177 printf("?Ambiguous argument '%s'.\r\n", argv[i]);
1179 } else if (!sl && !tl) {
1180 printf("?Unknown argument '%s'.\r\n", argv[i]);
1192 /*@*/optionstatus();
1193 #if defined(ENCRYPTION)
1202 * The following are the data structures, and many of the routines,
1203 * relating to command processing.
1207 * Set the escape character.
1210 setescape(int argc, char *argv[])
1216 "Deprecated usage - please use 'set escape%s%s' in the future.\r\n",
1217 (argc > 2)? " ":"", (argc > 2)? argv[1]: "");
1221 printf("new escape character: ");
1222 fgets(buf, sizeof(buf), stdin);
1227 printf("Escape character is '%s'.\r\n", control(escape));
1237 printf("Deprecated usage - please use 'toggle crmod' in the future.\r\n");
1238 printf("%s map carriage return on output.\r\n", crmod ? "Will" : "Won't");
1249 long oldrows, oldcols, newrows, newcols, err;
1251 err = (TerminalWindowSize(&oldrows, &oldcols) == 0) ? 1 : 0;
1254 * If we didn't get the window size before the SUSPEND, but we
1255 * can get them now (?), then send the NAWS to make sure that
1256 * we are set up for the right window size.
1258 if (TerminalWindowSize(&newrows, &newcols) && connected &&
1259 (err || ((oldrows != newrows) || (oldcols != newcols)))) {
1263 /* reget parameters in case they were changed */
1264 TerminalSaveState();
1267 printf("Suspend is not supported. Try the '!' command instead\r\n");
1273 shell(int argc, char **argv)
1275 long oldrows, oldcols, newrows, newcols, err;
1279 err = (TerminalWindowSize(&oldrows, &oldcols) == 0) ? 1 : 0;
1282 perror("Fork failed\r\n");
1288 * Fire up the shell in the child.
1290 char *shellp, *shellname;
1292 shellp = getenv("SHELL");
1295 if ((shellname = strrchr(shellp, '/')) == 0)
1300 execl(shellp, shellname, "-c", &saveline[1], NULL);
1302 execl(shellp, shellname, NULL);
1307 wait((int *)0); /* Wait for the shell to complete */
1309 if (TerminalWindowSize(&newrows, &newcols) && connected &&
1310 (err || ((oldrows != newrows) || (oldcols != newcols)))) {
1319 bye(int argc, char **argv)
1323 printf("Connection closed.\r\n");
1327 #if defined(AUTHENTICATION) || defined(ENCRYPTION)
1328 auth_encrypt_connect(connected);
1333 if ((argc != 2) || (strcmp(argv[1], "fromquit") != 0))
1334 longjmp(toplevel, 1);
1335 return 0; /* NOTREACHED */
1341 call(bye, "bye", "fromquit", 0);
1343 return 0; /*NOTREACHED*/
1349 send_do(TELOPT_LOGOUT, 1);
1366 static void slc_help(void);
1368 struct slclist SlcList[] = {
1369 { "export", "Use local special character definitions",
1370 slc_mode_export, 0 },
1371 { "import", "Use remote special character definitions",
1372 slc_mode_import, 1 },
1373 { "check", "Verify remote special character definitions",
1374 slc_mode_import, 0 },
1375 { "help", 0, slc_help, 0 },
1376 { "?", "Print help information", slc_help, 0 },
1385 for (c = SlcList; c->name; c++) {
1388 printf("%-15s %s\r\n", c->name, c->help);
1395 static struct slclist *
1398 return (struct slclist *)
1399 genget(name, (char **) SlcList, sizeof(struct slclist));
1403 slccmd(int argc, char **argv)
1409 "Need an argument to 'slc' command. 'slc ?' for help.\r\n");
1412 c = getslc(argv[1]);
1414 fprintf(stderr, "'%s': unknown argument ('slc ?' for help).\r\n",
1419 fprintf(stderr, "'%s': ambiguous argument ('slc ?' for help).\r\n",
1423 (*c->handler)(c->arg);
1429 * The ENVIRON command.
1439 static void env_help (void);
1441 struct envlist EnvList[] = {
1442 { "define", "Define an environment variable",
1443 (void (*)())env_define, 2 },
1444 { "undefine", "Undefine an environment variable",
1446 { "export", "Mark an environment variable for automatic export",
1448 { "unexport", "Don't mark an environment variable for automatic export",
1450 { "send", "Send an environment variable", env_send, 1 },
1451 { "list", "List the current environment variables",
1453 { "help", 0, env_help, 0 },
1454 { "?", "Print help information", env_help, 0 },
1463 for (c = EnvList; c->name; c++) {
1466 printf("%-15s %s\r\n", c->name, c->help);
1473 static struct envlist *
1474 getenvcmd(char *name)
1476 return (struct envlist *)
1477 genget(name, (char **) EnvList, sizeof(struct envlist));
1481 env_cmd(int argc, char **argv)
1487 "Need an argument to 'environ' command. 'environ ?' for help.\r\n");
1490 c = getenvcmd(argv[1]);
1492 fprintf(stderr, "'%s': unknown argument ('environ ?' for help).\r\n",
1497 fprintf(stderr, "'%s': ambiguous argument ('environ ?' for help).\r\n",
1501 if (c->narg + 2 != argc) {
1503 "Need %s%d argument%s to 'environ %s' command. 'environ ?' for help.\r\n",
1504 c->narg < argc + 2 ? "only " : "",
1505 c->narg, c->narg == 1 ? "" : "s", c->name);
1508 (*c->handler)(argv[2], argv[3]);
1513 struct env_lst *next; /* pointer to next structure */
1514 struct env_lst *prev; /* pointer to previous structure */
1515 unsigned char *var; /* pointer to variable name */
1516 unsigned char *value; /* pointer to variable value */
1517 int export; /* 1 -> export with default list of variables */
1518 int welldefined; /* A well defined variable */
1521 struct env_lst envlisthead;
1524 env_find(unsigned char *var)
1528 for (ep = envlisthead.next; ep; ep = ep->next) {
1529 if (strcmp((char *)ep->var, (char *)var) == 0)
1535 #if !HAVE_DECL_ENVIRON
1536 extern char **environ;
1545 for (epp = environ; *epp; epp++) {
1546 if ((cp = strchr(*epp, '='))) {
1548 ep = env_define((unsigned char *)*epp,
1549 (unsigned char *)cp+1);
1555 * Special case for DISPLAY variable. If it is ":0.0" or
1556 * "unix:0.0", we have to get rid of "unix" and insert our
1559 if ((ep = env_find((unsigned char*)"DISPLAY"))
1560 && (*ep->value == ':'
1561 || strncmp((char *)ep->value, "unix:", 5) == 0)) {
1563 char *cp2 = strchr((char *)ep->value, ':');
1566 /* XXX - should be k_gethostname? */
1567 gethostname(hbuf, 256);
1570 /* If this is not the full name, try to get it via DNS */
1571 if (strchr(hbuf, '.') == 0) {
1572 struct addrinfo hints, *ai, *a;
1574 memset (&hints, 0, sizeof(hints));
1575 hints.ai_flags = AI_CANONNAME;
1577 error = getaddrinfo (hbuf, NULL, &hints, &ai);
1579 for (a = ai; a != NULL; a = a->ai_next)
1580 if (a->ai_canonname != NULL) {
1590 error = asprintf (&cp, "%s%s", hbuf, cp2);
1593 ep->value = (unsigned char *)cp;
1597 * If USER is not defined, but LOGNAME is, then add
1598 * USER with the value from LOGNAME. By default, we
1599 * don't export the USER variable.
1601 if ((env_find((unsigned char*)"USER") == NULL) &&
1602 (ep = env_find((unsigned char*)"LOGNAME"))) {
1603 env_define((unsigned char *)"USER", ep->value);
1604 env_unexport((unsigned char *)"USER");
1606 env_export((unsigned char *)"DISPLAY");
1607 env_export((unsigned char *)"PRINTER");
1608 env_export((unsigned char *)"XAUTHORITY");
1612 env_define(unsigned char *var, unsigned char *value)
1616 if ((ep = env_find(var))) {
1622 ep = (struct env_lst *)malloc(sizeof(struct env_lst));
1623 ep->next = envlisthead.next;
1624 envlisthead.next = ep;
1625 ep->prev = &envlisthead;
1627 ep->next->prev = ep;
1629 ep->welldefined = opt_welldefined((char *)var);
1631 ep->var = (unsigned char *)strdup((char *)var);
1632 ep->value = (unsigned char *)strdup((char *)value);
1637 env_undefine(unsigned char *var)
1641 if ((ep = env_find(var))) {
1642 ep->prev->next = ep->next;
1644 ep->next->prev = ep->prev;
1654 env_export(unsigned char *var)
1658 if ((ep = env_find(var)))
1663 env_unexport(unsigned char *var)
1667 if ((ep = env_find(var)))
1672 env_send(unsigned char *var)
1676 if (my_state_is_wont(TELOPT_NEW_ENVIRON)
1678 && my_state_is_wont(TELOPT_OLD_ENVIRON)
1682 "Cannot send '%s': Telnet ENVIRON option not enabled\r\n",
1688 fprintf(stderr, "Cannot send '%s': variable not defined\r\n",
1692 env_opt_start_info();
1693 env_opt_add(ep->var);
1702 for (ep = envlisthead.next; ep; ep = ep->next) {
1703 printf("%c %-20s %s\r\n", ep->export ? '*' : ' ',
1704 ep->var, ep->value);
1709 env_default(int init, int welldefined)
1711 static struct env_lst *nep = NULL;
1718 while ((nep = nep->next)) {
1719 if (nep->export && (nep->welldefined == welldefined))
1727 env_getvalue(unsigned char *var)
1731 if ((ep = env_find(var)))
1737 #if defined(AUTHENTICATION)
1739 * The AUTHENTICATE command.
1752 struct authlist AuthList[] = {
1753 { "status", "Display current status of authentication information",
1755 { "disable", "Disable an authentication type ('auth disable ?' for more)",
1757 { "enable", "Enable an authentication type ('auth enable ?' for more)",
1759 { "help", 0, auth_help, 0 },
1760 { "?", "Print help information", auth_help, 0 },
1769 for (c = AuthList; c->name; c++) {
1772 printf("%-15s %s\r\n", c->name, c->help);
1781 auth_cmd(int argc, char **argv)
1787 "Need an argument to 'auth' command. 'auth ?' for help.\r\n");
1791 c = (struct authlist *)
1792 genget(argv[1], (char **) AuthList, sizeof(struct authlist));
1794 fprintf(stderr, "'%s': unknown argument ('auth ?' for help).\r\n",
1799 fprintf(stderr, "'%s': ambiguous argument ('auth ?' for help).\r\n",
1803 if (c->narg + 2 != argc) {
1805 "Need %s%d argument%s to 'auth %s' command. 'auth ?' for help.\r\n",
1806 c->narg < argc + 2 ? "only " : "",
1807 c->narg, c->narg == 1 ? "" : "s", c->name);
1810 return((*c->handler)(argv[2], argv[3]));
1815 #if defined(ENCRYPTION)
1817 * The ENCRYPT command.
1820 struct encryptlist {
1832 struct encryptlist EncryptList[] = {
1833 { "enable", "Enable encryption. ('encrypt enable ?' for more)",
1834 EncryptEnable, 1, 1, 2 },
1835 { "disable", "Disable encryption. ('encrypt enable ?' for more)",
1836 EncryptDisable, 0, 1, 2 },
1837 { "type", "Set encryptiong type. ('encrypt type ?' for more)",
1838 EncryptType, 0, 1, 1 },
1839 { "start", "Start encryption. ('encrypt start ?' for more)",
1840 EncryptStart, 1, 0, 1 },
1841 { "stop", "Stop encryption. ('encrypt stop ?' for more)",
1842 EncryptStop, 1, 0, 1 },
1843 { "input", "Start encrypting the input stream",
1844 EncryptStartInput, 1, 0, 0 },
1845 { "-input", "Stop encrypting the input stream",
1846 EncryptStopInput, 1, 0, 0 },
1847 { "output", "Start encrypting the output stream",
1848 EncryptStartOutput, 1, 0, 0 },
1849 { "-output", "Stop encrypting the output stream",
1850 EncryptStopOutput, 1, 0, 0 },
1852 { "status", "Display current status of authentication information",
1853 EncryptStatus, 0, 0, 0 },
1854 { "help", 0, EncryptHelp, 0, 0, 0 },
1855 { "?", "Print help information", EncryptHelp, 0, 0, 0 },
1862 struct encryptlist *c;
1864 for (c = EncryptList; c->name; c++) {
1867 printf("%-15s %s\r\n", c->name, c->help);
1876 encrypt_cmd(int argc, char **argv)
1878 struct encryptlist *c;
1880 c = (struct encryptlist *)
1881 genget(argv[1], (char **) EncryptList, sizeof(struct encryptlist));
1883 fprintf(stderr, "'%s': unknown argument ('encrypt ?' for help).\r\n",
1888 fprintf(stderr, "'%s': ambiguous argument ('encrypt ?' for help).\r\n",
1893 if (argc < c->minarg || argc > c->maxarg) {
1894 if (c->minarg == c->maxarg) {
1895 fprintf(stderr, "Need %s%d argument%s ",
1896 c->minarg < argc ? "only " : "", c->minarg,
1897 c->minarg == 1 ? "" : "s");
1899 fprintf(stderr, "Need %s%d-%d arguments ",
1900 c->maxarg < argc ? "only " : "", c->minarg, c->maxarg);
1902 fprintf(stderr, "to 'encrypt %s' command. 'encrypt ?' for help.\r\n",
1906 if (c->needconnect && !connected) {
1907 if (!(argc && (isprefix(argv[2], "help") || isprefix(argv[2], "?")))) {
1908 printf("?Need to be connected first.\r\n");
1912 return ((*c->handler)(argc > 0 ? argv[2] : 0,
1913 argc > 1 ? argv[3] : 0,
1914 argc > 2 ? argv[4] : 0));
1920 * Print status about the connection.
1924 status(int argc, char **argv)
1927 printf("Connected to %s.\r\n", hostname);
1928 if ((argc < 2) || strcmp(argv[1], "notmuch")) {
1929 int mode = getconnmode();
1931 if (my_want_state_is_will(TELOPT_LINEMODE)) {
1932 printf("Operating with LINEMODE option\r\n");
1933 printf("%s line editing\r\n", (mode&MODE_EDIT) ? "Local" : "No");
1934 printf("%s catching of signals\r\n",
1935 (mode&MODE_TRAPSIG) ? "Local" : "No");
1937 #ifdef KLUDGELINEMODE
1938 } else if (kludgelinemode && my_want_state_is_dont(TELOPT_SGA)) {
1939 printf("Operating in obsolete linemode\r\n");
1942 printf("Operating in single character mode\r\n");
1944 printf("Catching signals locally\r\n");
1946 printf("%s character echo\r\n", (mode&MODE_ECHO) ? "Local" : "Remote");
1947 if (my_want_state_is_will(TELOPT_LFLOW))
1948 printf("%s flow control\r\n", (mode&MODE_FLOW) ? "Local" : "No");
1949 #if defined(ENCRYPTION)
1954 printf("No connection.\r\n");
1956 printf("Escape character is '%s'.\r\n", control(escape));
1963 * Function that gets called when SIGINFO is received.
1966 ayt_status(int ignore)
1968 call(status, "status", "notmuch", 0);
1972 static Command *getcmd(char *name);
1975 cmdrc(char *m1, char *m2)
1977 static char rcname[128];
1981 int l1 = strlen(m1);
1982 int l2 = strlen(m2);
1988 strlcpy(m1save, m1, sizeof(m1save));
1991 if (rcname[0] == 0) {
1992 char *home = getenv("HOME");
1994 snprintf (rcname, sizeof(rcname), "%s/.telnetrc",
1998 if ((rcfile = fopen(rcname, "r")) == 0) {
2003 if (fgets(line, sizeof(line), rcfile) == NULL)
2010 if (!isspace((unsigned char)line[0]))
2013 if (gotmachine == 0) {
2014 if (isspace((unsigned char)line[0]))
2016 if (strncasecmp(line, m1, l1) == 0)
2017 strncpy(line, &line[l1], sizeof(line) - l1);
2018 else if (strncasecmp(line, m2, l2) == 0)
2019 strncpy(line, &line[l2], sizeof(line) - l2);
2020 else if (strncasecmp(line, "DEFAULT", 7) == 0)
2021 strncpy(line, &line[7], sizeof(line) - 7);
2024 if (line[0] != ' ' && line[0] != '\t' && line[0] != '\n')
2031 c = getcmd(margv[0]);
2033 printf("?Ambiguous command: %s\r\n", margv[0]);
2037 printf("?Invalid command: %s\r\n", margv[0]);
2041 * This should never happen...
2043 if (c->needconnect && !connected) {
2044 printf("?Need to be connected first for %s.\r\n", margv[0]);
2047 (*c->handler)(margc, margv);
2053 tn(int argc, char **argv)
2055 struct servent *sp = 0;
2056 char *cmd, *hostp = 0, *portp = 0;
2060 /* clear the socket address prior to use */
2063 printf("?Already connected to %s\r\n", hostname);
2067 strlcpy(line, "open ", sizeof(line));
2069 fgets(&line[strlen(line)], sizeof(line) - strlen(line), stdin);
2077 if (strcmp(*argv, "help") == 0 || isprefix(*argv, "?"))
2079 if (strcmp(*argv, "-l") == 0) {
2083 user = strdup(*argv++);
2087 if (strcmp(*argv, "-a") == 0) {
2103 printf("usage: %s [-l user] [-a] host-name [port]\r\n", cmd);
2109 strlcpy (_hostname, hostp, sizeof(_hostname));
2111 if (hostp[0] == '@' || hostp[0] == '!') {
2114 for (p = hostp + 1; *p; p++) {
2115 if (*p == ',' || *p == '@')
2118 if (hostname == NULL) {
2119 fprintf(stderr, "%s: bad source route specification\n", hostp);
2127 if (*portp == '-') {
2134 sp = roken_getservbyname(portp, "tcp");
2138 printf("%s: bad port number\r\n", portp);
2146 sp = roken_getservbyname("telnet", "tcp");
2148 fprintf(stderr, "telnet: tcp/telnet: unknown service\r\n");
2157 struct addrinfo *ai, *a, hints;
2159 char portstr[NI_MAXSERV];
2161 memset (&hints, 0, sizeof(hints));
2162 hints.ai_socktype = SOCK_STREAM;
2163 hints.ai_protocol = IPPROTO_TCP;
2164 hints.ai_flags = AI_CANONNAME;
2166 snprintf (portstr, sizeof(portstr), "%u", ntohs(port));
2168 error = getaddrinfo (hostname, portstr, &hints, &ai);
2170 fprintf (stderr, "%s: %s\r\n", hostname, gai_strerror (error));
2174 for (a = ai; a != NULL && connected == 0; a = a->ai_next) {
2177 if (a->ai_canonname != NULL)
2178 strlcpy (_hostname, a->ai_canonname, sizeof(_hostname));
2180 if (getnameinfo (a->ai_addr, a->ai_addrlen,
2181 addrstr, sizeof(addrstr),
2182 NULL, 0, NI_NUMERICHOST) != 0)
2183 strlcpy (addrstr, "unknown address", sizeof(addrstr));
2185 printf("Trying %s...\r\n", addrstr);
2187 net = socket (a->ai_family, a->ai_socktype, a->ai_protocol);
2193 #if defined(IP_OPTIONS) && defined(IPPROTO_IP) && defined(HAVE_SETSOCKOPT)
2194 if (hostp[0] == '@' || hostp[0] == '!') {
2199 if ((srlen = sourceroute(a, hostp, &srp, &proto, &opt)) < 0) {
2200 (void) NetClose(net);
2204 if (srp && setsockopt(net, proto, opt, srp, srlen) < 0)
2205 perror("setsockopt (source route)");
2209 #if defined(IPPROTO_IP) && defined(IP_TOS)
2210 if (a->ai_family == AF_INET) {
2211 # if defined(HAVE_GETTOSBYNAME)
2213 if (tos < 0 && (tp = gettosbyname("telnet", "tcp")))
2217 tos = 020; /* Low Delay bit */
2219 && (setsockopt(net, IPPROTO_IP, IP_TOS,
2220 (void *)&tos, sizeof(int)) < 0)
2221 && (errno != ENOPROTOOPT))
2222 perror("telnet: setsockopt (IP_TOS) (ignored)");
2224 #endif /* defined(IPPROTO_IP) && defined(IP_TOS) */
2225 if (debug && SetSockOpt(net, SOL_SOCKET, SO_DEBUG, 1) < 0) {
2226 perror("setsockopt (SO_DEBUG)");
2229 if (connect (net, a->ai_addr, a->ai_addrlen) < 0) {
2230 fprintf (stderr, "telnet: connect to address %s: %s\n",
2231 addrstr, strerror(errno));
2233 if (a->ai_next != NULL) {
2241 #if defined(AUTHENTICATION) || defined(ENCRYPTION)
2242 auth_encrypt_connect(connected);
2249 cmdrc(hostp, hostname);
2250 set_forward_options();
2251 if (autologin && user == NULL)
2252 user = (char *)get_default_username ();
2254 env_define((unsigned char *)"USER", (unsigned char *)user);
2255 env_export((unsigned char *)"USER");
2257 call(status, "status", "notmuch", 0);
2258 if (setjmp(peerdied) == 0)
2259 my_telnet((char *)user);
2261 ExitString("Connection closed by foreign host.\r\n",1);
2266 #define HELPINDENT ((int)sizeof ("connect"))
2269 openhelp[] = "connect to a site",
2270 closehelp[] = "close current connection",
2271 logouthelp[] = "forcibly logout remote user and close the connection",
2272 quithelp[] = "exit telnet",
2273 statushelp[] = "print status information",
2274 helphelp[] = "print help information",
2275 sendhelp[] = "transmit special characters ('send ?' for more)",
2276 sethelp[] = "set operating parameters ('set ?' for more)",
2277 unsethelp[] = "unset operating parameters ('unset ?' for more)",
2278 togglestring[] ="toggle operating parameters ('toggle ?' for more)",
2279 slchelp[] = "change state of special charaters ('slc ?' for more)",
2280 displayhelp[] = "display operating parameters",
2281 #if defined(AUTHENTICATION)
2282 authhelp[] = "turn on (off) authentication ('auth ?' for more)",
2284 #if defined(ENCRYPTION)
2285 encrypthelp[] = "turn on (off) encryption ('encrypt ?' for more)",
2287 zhelp[] = "suspend telnet",
2288 shellhelp[] = "invoke a subshell",
2289 envhelp[] = "change environment variables ('environ ?' for more)",
2290 modestring[] = "try to enter line or character mode ('mode ?' for more)";
2292 static int help(int argc, char **argv);
2294 static Command cmdtab[] = {
2295 { "close", closehelp, bye, 1 },
2296 { "logout", logouthelp, logout, 1 },
2297 { "display", displayhelp, display, 0 },
2298 { "mode", modestring, modecmd, 0 },
2299 { "open", openhelp, tn, 0 },
2300 { "quit", quithelp, quit, 0 },
2301 { "send", sendhelp, sendcmd, 0 },
2302 { "set", sethelp, setcmd, 0 },
2303 { "unset", unsethelp, unsetcmd, 0 },
2304 { "status", statushelp, status, 0 },
2305 { "toggle", togglestring, toggle, 0 },
2306 { "slc", slchelp, slccmd, 0 },
2307 #if defined(AUTHENTICATION)
2308 { "auth", authhelp, auth_cmd, 0 },
2310 #if defined(ENCRYPTION)
2311 { "encrypt", encrypthelp, encrypt_cmd, 0 },
2313 { "z", zhelp, telnetsuspend, 0 },
2314 { "!", shellhelp, shell, 0 },
2315 { "environ", envhelp, env_cmd, 0 },
2316 { "?", helphelp, help, 0 },
2320 static char crmodhelp[] = "deprecated command -- use 'toggle crmod' instead";
2321 static char escapehelp[] = "deprecated command -- use 'set escape' instead";
2323 static Command cmdtab2[] = {
2324 { "help", 0, help, 0 },
2325 { "escape", escapehelp, setescape, 0 },
2326 { "crmod", crmodhelp, togcrmod, 0 },
2332 * Call routine with argc, argv set from args (terminated by 0).
2336 call(intrtn_t routine, ...)
2342 va_start(ap, routine);
2343 while ((args[argno++] = va_arg(ap, char *)) != 0);
2345 return (*routine)(argno-1, args);
2354 if ((cm = (Command *) genget(name, (char **) cmdtab, sizeof(Command))))
2356 return (Command *) genget(name, (char **) cmdtab2, sizeof(Command));
2360 command(int top, char *tbuf, int cnt)
2368 signal(SIGINT, SIG_DFL);
2369 signal(SIGQUIT, SIG_DFL);
2372 if (rlogin == _POSIX_VDISABLE)
2373 printf("%s> ", prompt);
2377 while (cnt > 0 && (*cp++ = *tbuf++) != '\n')
2380 if (cp == line || *--cp != '\n' || cp == line)
2383 if (rlogin == _POSIX_VDISABLE)
2384 printf("%s\r\n", line);
2387 if (rlogin != _POSIX_VDISABLE)
2388 printf("%s> ", prompt);
2389 if (fgets(line, sizeof(line), stdin) == NULL) {
2390 if (feof(stdin) || ferror(stdin)) {
2400 if (margv[0] == 0) {
2403 c = getcmd(margv[0]);
2405 printf("?Ambiguous command\r\n");
2409 printf("?Invalid command\r\n");
2412 if (c->needconnect && !connected) {
2413 printf("?Need to be connected first.\r\n");
2416 if ((*c->handler)(margc, margv)) {
2422 longjmp(toplevel, 1);
2433 help(int argc, char **argv)
2438 printf("Commands may be abbreviated. Commands are:\r\n\r\n");
2439 for (c = cmdtab; c->name; c++)
2441 printf("%-*s\t%s\r\n", HELPINDENT, c->name,
2446 while (--argc > 0) {
2451 printf("?Ambiguous help command %s\r\n", arg);
2452 else if (c == (Command *)0)
2453 printf("?Invalid help command %s\r\n", arg);
2455 printf("%s\r\n", c->help);
2461 #if defined(IP_OPTIONS) && defined(IPPROTO_IP)
2464 * Source route is handed in as
2465 * [!]@hop1@hop2...@dst
2467 * If the leading ! is present, it is a strict source route, otherwise it is
2468 * assmed to be a loose source route. Note that leading ! is effective
2469 * only for IPv4 case.
2471 * We fill in the source route option as
2472 * hop1,hop2,hop3...dest
2473 * and return a pointer to hop1, which will
2474 * be the address to connect() to.
2477 * ai: The address (by struct addrinfo) for the final destination.
2479 * arg: Pointer to route list to decipher
2481 * cpp: Pointer to a pointer, so that sourceroute() can return
2482 * the address of result buffer (statically alloc'ed).
2485 * Pointer to an integer. The pointed variable
2486 * lenp: pointer to an integer that contains the
2487 * length of *cpp if *cpp != NULL.
2491 * Returns the length of the option pointed to by *cpp. If the
2492 * return value is -1, there was a syntax error in the
2493 * option, either arg contained unknown characters or too many hosts,
2494 * or hostname cannot be resolved.
2496 * The caller needs to pass return value (len), *cpp, *protop and *optp
2499 * *cpp: Points to the result buffer. The region is statically
2500 * allocated by the function.
2503 * protocol # to be passed to setsockopt(2).
2505 * *optp: option # to be passed to setsockopt(2).
2509 sourceroute(struct addrinfo *ai,
2515 char *cp, *cp2, *lsrp = NULL, *lsrep = NULL;
2516 struct addrinfo hints, *res;
2518 struct sockaddr_in *sin;
2520 static char lsr[44];
2522 struct cmsghdr *cmsg = NULL;
2523 struct sockaddr_in6 *sin6;
2524 static char rhbuf[1024];
2528 * Verify the arguments.
2536 switch (ai->ai_family) {
2539 lsrep = lsrp + sizeof(lsr);
2542 * Next, decide whether we have a loose source
2543 * route or a strict source route, and fill in
2544 * the begining of the option.
2548 *lsrp++ = IPOPT_SSRR;
2550 *lsrp++ = IPOPT_LSRR;
2553 lsrp++; /* skip over length, we'll fill it in later */
2556 *protop = IPPROTO_IP;
2561 /* this needs to be updated for rfc2292bis */
2562 #ifdef IPV6_PKTOPTIONS
2563 cmsg = inet6_rthdr_init(rhbuf, IPV6_RTHDR_TYPE_0);
2567 *protop = IPPROTO_IPV6;
2568 *optp = IPV6_PKTOPTIONS;
2578 memset(&hints, 0, sizeof(hints));
2579 hints.ai_family = ai->ai_family;
2580 hints.ai_socktype = SOCK_STREAM;
2585 else for (cp2 = cp; (c = *cp2) != '\0'; cp2++) {
2590 } else if (c == '@') {
2593 #if 0 /*colon conflicts with IPv6 address*/
2594 else if (c == ':') {
2605 error = getaddrinfo(cp, NULL, &hints, &res);
2607 fprintf(stderr, "%s: %s\n", cp, gai_strerror(error));
2610 if (ai->ai_family != res->ai_family) {
2614 if (ai->ai_family == AF_INET) {
2616 * Check to make sure there is space for address
2618 if (lsrp + 4 > lsrep) {
2622 sin = (struct sockaddr_in *)res->ai_addr;
2623 memcpy(lsrp, &sin->sin_addr, sizeof(struct in_addr));
2624 lsrp += sizeof(struct in_addr);
2627 else if (ai->ai_family == AF_INET6) {
2628 sin6 = (struct sockaddr_in6 *)res->ai_addr;
2629 inet6_rthdr_add(cmsg, &sin6->sin6_addr,
2643 if (ai->ai_family == AF_INET) {
2644 /* record the last hop */
2645 if (lsrp + 4 > lsrep)
2647 sin = (struct sockaddr_in *)ai->ai_addr;
2648 memcpy(lsrp, &sin->sin_addr, sizeof(struct in_addr));
2649 lsrp += sizeof(struct in_addr);
2651 lsr[IPOPT_OLEN] = lsrp - lsr;
2652 if (lsr[IPOPT_OLEN] <= 7 || lsr[IPOPT_OLEN] > 40)
2654 *lsrp++ = IPOPT_NOP; /*32bit word align*/
2658 ipopt.io_len = lsrp - lsr;
2659 if (ipopt.io_len <= 5) /*is 3 better?*/
2661 *cpp = (char 8)&ipopt;
2665 else if (ai->ai_family == AF_INET6) {
2666 inet6_rthdr_lasthop(cmsg, IPV6_RTHDR_LOOSE);
2667 len = cmsg->cmsg_len;