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 <sys/cdefs.h>
36 __FBSDID("$FreeBSD: src/usr.bin/telnet/commands.c,v 1.21.2.6 2002/11/30 05:35:13 eric Exp $");
39 static const char sccsid[] = "@(#)commands.c 8.4 (Berkeley) 5/30/95";
42 #include <sys/param.h>
45 #include <sys/socket.h>
46 #include <netinet/in.h>
59 #include <arpa/telnet.h>
60 #include <arpa/inet.h>
72 #include <netinet/in_systm.h>
73 #include <netinet/ip.h>
74 #include <netinet/ip6.h>
76 #ifndef MAXHOSTNAMELEN
77 #define MAXHOSTNAMELEN 256
80 typedef int (*intrtn_t)(int, char **);
82 #if defined(IPPROTO_IP) && defined(IP_TOS)
84 #endif /* defined(IPPROTO_IP) && defined(IP_TOS) */
87 static char _hostname[MAXHOSTNAMELEN];
89 static int help(int, char **);
90 static int call(intrtn_t, ...);
91 static void cmdrc(char *, char *);
93 static int switch_af(struct addrinfo **);
95 static int togglehelp(void);
96 static int send_tncmd(void (*)(int, int), const char *, char *);
97 static int setmod(int);
98 static int clearmode(int);
99 static int modehelp(void);
100 static int sourceroute(struct addrinfo *, char *, char **, int *, int *, int *);
103 const char *name; /* command name */
104 const char *help; /* help string (NULL for no help) */
105 int (*handler)(int, char **); /* routine which executes command */
106 int needconnect; /* Do we need to be connected to execute? */
109 static char line[256];
110 static char saveline[256];
112 static char *margv[20];
115 #include <sys/wait.h>
116 #define PATH_OPIEKEY "/usr/bin/opiekey"
118 opie_calc(int argc, char *argv[])
123 printf("%s sequence challenge\n", argv[0]);
129 execv(PATH_OPIEKEY, argv);
135 (void) wait(&status);
136 if (WIFEXITED(status))
137 return (WEXITSTATUS(status));
151 if (*cp == '!') { /* Special case shell escape */
152 strcpy(saveline, line); /* save for shell command */
153 *argp++ = strdup("!"); /* No room in string to get this */
165 for (cp2 = cp; c != '\0'; c = *++cp) {
173 if ((c = *++cp) == '\0')
175 } else if (c == '"') {
178 } else if (c == '\'') {
181 } else if (isspace(c))
195 * Make a character string into a number.
197 * Todo: 1. Could take random integers (12, 0x12, 012, 0b1).
210 c = b | 0x40; /* DEL */
223 * Construct a control character sequence
224 * for a special character.
231 * The only way I could get the Sun 3.5 compiler
233 * if ((unsigned int)c >= 0x80)
234 * was to assign "c" to an unsigned int variable...
237 unsigned int uic = (unsigned int)c;
241 if (c == (cc_t)_POSIX_VDISABLE) {
246 buf[1] = ((c>>6)&07) + '0';
247 buf[2] = ((c>>3)&07) + '0';
248 buf[3] = (c&07) + '0';
250 } else if (uic >= 0x20) {
262 * The following are data structures and routines for
263 * the "send" command.
268 const char *name; /* How user refers to it (case independent) */
269 const char *help; /* Help information (0 ==> no help) */
270 int needconnect; /* Need to be connected */
271 int narg; /* Number of arguments */
272 int (*handler)(char *, ...); /* Routine to perform (for special ops) */
273 int nbyte; /* Number of bytes to send this command */
274 int what; /* Character to be sent (<0 ==> special) */
282 send_dontcmd(char *),
283 send_willcmd(char *),
284 send_wontcmd(char *);
286 static struct sendlist Sendlist[] = {
287 { "ao", "Send Telnet Abort output", 1, 0, NULL, 2, AO },
288 { "ayt", "Send Telnet 'Are You There'", 1, 0, NULL, 2, AYT },
289 { "brk", "Send Telnet Break", 1, 0, NULL, 2, BREAK },
290 { "break", NULL, 1, 0, NULL, 2, BREAK },
291 { "ec", "Send Telnet Erase Character", 1, 0, NULL, 2, EC },
292 { "el", "Send Telnet Erase Line", 1, 0, NULL, 2, EL },
293 { "escape", "Send current escape character",1, 0, (int (*)(char *, ...))send_esc, 1, 0 },
294 { "ga", "Send Telnet 'Go Ahead' sequence", 1, 0, NULL, 2, GA },
295 { "ip", "Send Telnet Interrupt Process",1, 0, NULL, 2, IP },
296 { "intp", NULL, 1, 0, NULL, 2, IP },
297 { "interrupt", NULL, 1, 0, NULL, 2, IP },
298 { "intr", NULL, 1, 0, NULL, 2, IP },
299 { "nop", "Send Telnet 'No operation'", 1, 0, NULL, 2, NOP },
300 { "eor", "Send Telnet 'End of Record'", 1, 0, NULL, 2, EOR },
301 { "abort", "Send Telnet 'Abort Process'", 1, 0, NULL, 2, ABORT },
302 { "susp", "Send Telnet 'Suspend Process'",1, 0, NULL, 2, SUSP },
303 { "eof", "Send Telnet End of File Character", 1, 0, NULL, 2, xEOF },
304 { "synch", "Perform Telnet 'Synch operation'", 1, 0, (int (*)(char *, ...))dosynch, 2, 0 },
305 { "getstatus", "Send request for STATUS", 1, 0, (int (*)(char *, ...))get_status, 6, 0 },
306 { "?", "Display send options", 0, 0, (int (*)(char *, ...))send_help, 0, 0 },
307 { "help", NULL, 0, 0, (int (*)(char *, ...))send_help, 0, 0 },
308 { "do", NULL, 0, 1, (int (*)(char *, ...))send_docmd, 3, 0 },
309 { "dont", NULL, 0, 1, (int (*)(char *, ...))send_dontcmd, 3, 0 },
310 { "will", NULL, 0, 1, (int (*)(char *, ...))send_willcmd, 3, 0 },
311 { "wont", NULL, 0, 1, (int (*)(char *, ...))send_wontcmd, 3, 0 },
312 { NULL, NULL, 0, 0, NULL, 0, 0 }
315 #define GETSEND(name) ((struct sendlist *) genget(name, (char **) Sendlist, \
316 sizeof(struct sendlist)))
319 sendcmd(int argc, char *argv[])
321 int count; /* how many bytes we are going to need to send */
323 struct sendlist *s; /* pointer to current command */
328 printf("need at least one argument for 'send' command\n");
329 printf("'send ?' for help\n");
333 * First, validate all the send arguments.
334 * In addition, we see how much space we are going to need, and
335 * whether or not we will be doing a "SYNCH" operation (which
336 * flushes the network queue).
339 for (i = 1; i < argc; i++) {
340 s = GETSEND(argv[i]);
342 printf("Unknown send argument '%s'\n'send ?' for help.\n",
345 } else if (Ambiguous((void *)s)) {
346 printf("Ambiguous send argument '%s'\n'send ?' for help.\n",
350 if (i + s->narg >= argc) {
352 "Need %d argument%s to 'send %s' command. 'send %s ?' for help.\n",
353 s->narg, s->narg == 1 ? "" : "s", s->name, s->name);
357 if ((void *)s->handler == (void *)send_help) {
363 needconnect += s->needconnect;
365 if (!connected && needconnect) {
366 printf("?Need to be connected first.\n");
367 printf("'send ?' for help\n");
370 /* Now, do we have enough room? */
371 if (NETROOM() < count) {
372 printf("There is not enough room in the buffer TO the network\n");
373 printf("to process your request. Nothing will be done.\n");
374 printf("('send synch' will throw away most data in the network\n");
375 printf("buffer, if this might help.)\n");
378 /* OK, they are all OK, now go through again and actually send */
380 for (i = 1; i < argc; i++) {
381 if ((s = GETSEND(argv[i])) == 0) {
382 fprintf(stderr, "Telnet 'send' error - argument disappeared!\n");
388 success += (*s->handler)((s->narg > 0) ? argv[i+1] : 0,
389 (s->narg > 1) ? argv[i+2] : 0);
392 NET2ADD(IAC, s->what);
393 printoption("SENT", IAC, s->what);
396 return (count == success);
407 send_docmd(char *name)
409 return(send_tncmd(send_do, "do", name));
416 return(send_tncmd(send_dont, "dont", name));
420 send_willcmd(char *name)
422 return(send_tncmd(send_will, "will", name));
426 send_wontcmd(char *name)
428 return(send_tncmd(send_wont, "wont", name));
432 send_tncmd(void (*func)(int, int), const char *cmd, char *name)
435 extern char *telopts[];
438 if (isprefix(name, "help") || isprefix(name, "?")) {
441 printf("Usage: send %s <value|option>\n", cmd);
442 printf("\"value\" must be from 0 to 255\n");
443 printf("Valid options are:\n\t");
446 for (cpp = telopts; *cpp; cpp++) {
447 len = strlen(*cpp) + 3;
448 if (col + len > 65) {
452 printf(" \"%s\"", *cpp);
458 cpp = (char **)genget(name, telopts, sizeof(char *));
459 if (Ambiguous(cpp)) {
460 fprintf(stderr,"'%s': ambiguous argument ('send %s ?' for help).\n",
469 while (*cp >= '0' && *cp <= '9') {
475 fprintf(stderr, "'%s': unknown argument ('send %s ?' for help).\n",
478 } else if (val < 0 || val > 255) {
479 fprintf(stderr, "'%s': bad value ('send %s ?' for help).\n",
485 printf("?Need to be connected first.\n");
495 struct sendlist *s; /* pointer to current command */
496 for (s = Sendlist; s->name; s++) {
498 printf("%-15s %s\n", s->name, s->help);
504 * The following are the routines and data structures referred
505 * to by the arguments to the "toggle" command.
520 (SetSockOpt(net, SOL_SOCKET, SO_DEBUG, debug)) < 0) {
521 perror("setsockopt (SO_DEBUG)");
525 if (net > 0 && SetSockOpt(net, SOL_SOCKET, SO_DEBUG, 1) < 0)
526 perror("setsockopt (SO_DEBUG)");
528 printf("Cannot turn off socket debugging\n");
538 printf("Will send carriage returns as telnet <CR><LF>.\n");
540 printf("Will send carriage returns as telnet <CR><NUL>.\n");
550 donebinarytoggle = 1;
555 if (my_want_state_is_will(TELOPT_BINARY) &&
556 my_want_state_is_do(TELOPT_BINARY)) {
558 } else if (my_want_state_is_wont(TELOPT_BINARY) &&
559 my_want_state_is_dont(TELOPT_BINARY)) {
562 val = binmode ? 0 : 1;
566 if (my_want_state_is_will(TELOPT_BINARY) &&
567 my_want_state_is_do(TELOPT_BINARY)) {
568 printf("Already operating in binary mode with remote host.\n");
570 printf("Negotiating binary mode with remote host.\n");
574 if (my_want_state_is_wont(TELOPT_BINARY) &&
575 my_want_state_is_dont(TELOPT_BINARY)) {
576 printf("Already in network ascii mode with remote host.\n");
578 printf("Negotiating network ascii mode with remote host.\n");
588 donebinarytoggle = 1;
591 val = my_want_state_is_do(TELOPT_BINARY) ? 0 : 1;
594 if (my_want_state_is_do(TELOPT_BINARY)) {
595 printf("Already receiving in binary mode.\n");
597 printf("Negotiating binary mode on input.\n");
601 if (my_want_state_is_dont(TELOPT_BINARY)) {
602 printf("Already receiving in network ascii mode.\n");
604 printf("Negotiating network ascii mode on input.\n");
614 donebinarytoggle = 1;
617 val = my_want_state_is_will(TELOPT_BINARY) ? 0 : 1;
620 if (my_want_state_is_will(TELOPT_BINARY)) {
621 printf("Already transmitting in binary mode.\n");
623 printf("Negotiating binary mode on output.\n");
627 if (my_want_state_is_wont(TELOPT_BINARY)) {
628 printf("Already transmitting in network ascii mode.\n");
630 printf("Negotiating network ascii mode on output.\n");
638 const char *name; /* name of toggle */
639 const char *help; /* help message */
640 int (*handler)(int); /* routine to do actual setting */
642 const char *actionexplanation;
645 static struct togglelist Togglelist[] = {
647 "flushing of output when sending interrupt characters",
650 "flush output when sending interrupt characters" },
652 "automatic sending of interrupt characters in urgent mode",
655 "send interrupt characters in urgent mode" },
657 "don't read ~/.telnetrc file",
660 "skip reading of ~/.telnetrc file" },
662 "sending and receiving of binary data",
667 "receiving of binary data",
672 "sending of binary data",
677 "sending carriage returns as telnet <CR><LF>",
678 (int (*)(int))togcrlf,
682 "mapping of received carriage returns",
685 "map carriage return on output" },
687 "local recognition of certain control characters",
688 (int (*)(int))lclchars,
690 "recognize certain control characters" },
691 { " ", "", NULL, NULL, NULL }, /* empty line */
694 (int (*)(int))togdebug,
696 "turn on socket level debugging" },
698 "printing of hexadecimal network data (debugging)",
701 "print hexadecimal representation of network traffic" },
703 "output of \"netdata\" to user readable format (debugging)",
706 "print user readable output for \"netdata\"" },
708 "viewing of options processing (debugging)",
711 "show option processing" },
713 "(debugging) toggle printing of hexadecimal terminal data",
716 "print hexadecimal representation of terminal traffic" },
719 (int (*)(int))togglehelp,
722 { NULL, NULL, NULL, NULL, NULL },
725 (int (*)(int))togglehelp,
728 { NULL, NULL, NULL, NULL, NULL }
734 struct togglelist *c;
736 for (c = Togglelist; c->name; c++) {
739 printf("%-15s toggle %s\n", c->name, c->help);
745 printf("%-15s %s\n", "?", "display help information");
750 settogglehelp(int set)
752 struct togglelist *c;
754 for (c = Togglelist; c->name; c++) {
757 printf("%-15s %s %s\n", c->name, set ? "enable" : "disable",
765 #define GETTOGGLE(name) (struct togglelist *) \
766 genget(name, (char **) Togglelist, sizeof(struct togglelist))
769 toggle(int argc, char *argv[])
773 struct togglelist *c;
777 "Need an argument to 'toggle' command. 'toggle ?' for help.\n");
785 if (Ambiguous((void *)c)) {
786 fprintf(stderr, "'%s': ambiguous argument ('toggle ?' for help).\n",
790 fprintf(stderr, "'%s': unknown argument ('toggle ?' for help).\n",
795 *c->variable = !*c->variable; /* invert it */
796 if (c->actionexplanation) {
797 printf("%s %s.\n", *c->variable? "Will" : "Won't",
798 c->actionexplanation);
802 retval &= (*c->handler)(-1);
810 * The following perform the "set" command.
814 struct termio new_tc = { 0, 0, 0, 0, {}, 0, 0 };
818 const char *name; /* name */
819 const char *help; /* help information */
820 void (*handler)(char *);
821 cc_t *charp; /* where it is located at */
824 static struct setlist Setlist[] = {
825 #ifdef KLUDGELINEMODE
826 { "echo", "character to toggle local echoing on/off", NULL, &echoc },
828 { "escape", "character to escape back to telnet command mode", NULL, &escape },
829 { "rlogin", "rlogin escape character", 0, &rlogin },
830 { "tracefile", "file to write trace information to", SetNetTrace, (cc_t *)NetTraceFile},
831 { " ", "", NULL, NULL },
832 { " ", "The following need 'localchars' to be toggled true", NULL, NULL },
833 { "flushoutput", "character to cause an Abort Output", NULL, termFlushCharp },
834 { "interrupt", "character to cause an Interrupt Process", NULL, termIntCharp },
835 { "quit", "character to cause an Abort process", NULL, termQuitCharp },
836 { "eof", "character to cause an EOF ", NULL, termEofCharp },
837 { " ", "", NULL, NULL },
838 { " ", "The following are for local editing in linemode", NULL, NULL },
839 { "erase", "character to use to erase a character", NULL, termEraseCharp },
840 { "kill", "character to use to erase a line", NULL, termKillCharp },
841 { "lnext", "character to use for literal next", NULL, termLiteralNextCharp },
842 { "susp", "character to cause a Suspend Process", NULL, termSuspCharp },
843 { "reprint", "character to use for line reprint", NULL, termRprntCharp },
844 { "worderase", "character to use to erase a word", NULL, termWerasCharp },
845 { "start", "character to use for XON", NULL, termStartCharp },
846 { "stop", "character to use for XOFF", NULL, termStopCharp },
847 { "forw1", "alternate end of line character", NULL, termForw1Charp },
848 { "forw2", "alternate end of line character", NULL, termForw2Charp },
849 { "ayt", "alternate AYT character", NULL, termAytCharp },
850 { NULL, NULL, NULL, NULL }
853 static struct setlist *
856 return (struct setlist *)
857 genget(name, (char **) Setlist, sizeof(struct setlist));
861 set_escape_char(char *s)
863 if (rlogin != _POSIX_VDISABLE) {
864 rlogin = (s && *s) ? special(s) : _POSIX_VDISABLE;
865 printf("Telnet rlogin escape character is '%s'.\n",
868 escape = (s && *s) ? special(s) : _POSIX_VDISABLE;
869 printf("Telnet escape character is '%s'.\n", control(escape));
874 setcmd(int argc, char *argv[])
878 struct togglelist *c;
880 if (argc < 2 || argc > 3) {
881 printf("Format is 'set Name Value'\n'set ?' for help.\n");
884 if ((argc == 2) && (isprefix(argv[1], "?") || isprefix(argv[1], "help"))) {
885 for (ct = Setlist; ct->name; ct++)
886 printf("%-15s %s\n", ct->name, ct->help);
889 printf("%-15s %s\n", "?", "display help information");
893 ct = getset(argv[1]);
895 c = GETTOGGLE(argv[1]);
897 fprintf(stderr, "'%s': unknown argument ('set ?' for help).\n",
900 } else if (Ambiguous((void *)c)) {
901 fprintf(stderr, "'%s': ambiguous argument ('set ?' for help).\n",
906 if ((argc == 2) || (strcmp("on", argv[2]) == 0))
908 else if (strcmp("off", argv[2]) == 0)
911 printf("Format is 'set togglename [on|off]'\n'set ?' for help.\n");
914 if (c->actionexplanation) {
915 printf("%s %s.\n", *c->variable? "Will" : "Won't",
916 c->actionexplanation);
921 } else if (argc != 3) {
922 printf("Format is 'set Name Value'\n'set ?' for help.\n");
924 } else if (Ambiguous((void *)ct)) {
925 fprintf(stderr, "'%s': ambiguous argument ('set ?' for help).\n",
928 } else if (ct->handler) {
929 (*ct->handler)(argv[2]);
930 printf("%s set to \"%s\".\n", ct->name, (char *)ct->charp);
932 if (strcmp("off", argv[2])) {
933 value = special(argv[2]);
935 value = _POSIX_VDISABLE;
937 *(ct->charp) = (cc_t)value;
938 printf("%s character is '%s'.\n", ct->name, control(*(ct->charp)));
945 unsetcmd(int argc, char *argv[])
948 struct togglelist *c;
953 "Need an argument to 'unset' command. 'unset ?' for help.\n");
956 if (isprefix(argv[1], "?") || isprefix(argv[1], "help")) {
957 for (ct = Setlist; ct->name; ct++)
958 printf("%-15s %s\n", ct->name, ct->help);
961 printf("%-15s %s\n", "?", "display help information");
973 fprintf(stderr, "'%s': unknown argument ('unset ?' for help).\n",
976 } else if (Ambiguous((void *)c)) {
977 fprintf(stderr, "'%s': ambiguous argument ('unset ?' for help).\n",
983 if (c->actionexplanation) {
984 printf("%s %s.\n", *c->variable? "Will" : "Won't",
985 c->actionexplanation);
990 } else if (Ambiguous((void *)ct)) {
991 fprintf(stderr, "'%s': ambiguous argument ('unset ?' for help).\n",
994 } else if (ct->handler) {
996 printf("%s reset to \"%s\".\n", ct->name, (char *)ct->charp);
998 *(ct->charp) = _POSIX_VDISABLE;
999 printf("%s character is '%s'.\n", ct->name, control(*(ct->charp)));
1006 * The following are the data structures and routines for the
1009 #ifdef KLUDGELINEMODE
1010 extern int kludgelinemode;
1016 send_wont(TELOPT_LINEMODE, 1);
1017 send_dont(TELOPT_SGA, 1);
1018 send_dont(TELOPT_ECHO, 1);
1026 #ifdef KLUDGELINEMODE
1028 send_dont(TELOPT_SGA, 1);
1030 send_will(TELOPT_LINEMODE, 1);
1031 send_dont(TELOPT_ECHO, 1);
1038 #ifdef KLUDGELINEMODE
1040 send_do(TELOPT_SGA, 1);
1043 send_wont(TELOPT_LINEMODE, 1);
1044 send_do(TELOPT_ECHO, 1);
1049 dolmmode(int bit, int on)
1052 extern int linemode;
1054 if (my_want_state_is_wont(TELOPT_LINEMODE)) {
1055 printf("?Need to have LINEMODE option enabled first.\n");
1056 printf("'mode ?' for help.\n");
1061 c = (linemode | bit);
1063 c = (linemode & ~bit);
1071 return dolmmode(bit, 1);
1077 return dolmmode(bit, 0);
1081 const char *name; /* command name */
1082 const char *help; /* help string */
1083 int (*handler)(int);/* routine which executes command */
1084 int needconnect; /* Do we need to be connected to execute? */
1088 static struct modelist ModeList[] = {
1089 { "character", "Disable LINEMODE option", (int (*)(int))docharmode, 1, 0 },
1090 #ifdef KLUDGELINEMODE
1091 { "", "(or disable obsolete line-by-line mode)", NULL, 0, 0 },
1093 { "line", "Enable LINEMODE option", (int (*)(int))dolinemode, 1, 0 },
1094 #ifdef KLUDGELINEMODE
1095 { "", "(or enable obsolete line-by-line mode)", NULL, 0, 0 },
1097 { "", "", NULL, 0, 0 },
1098 { "", "These require the LINEMODE option to be enabled", NULL, 0, 0 },
1099 { "isig", "Enable signal trapping", setmod, 1, MODE_TRAPSIG },
1100 { "+isig", 0, setmod, 1, MODE_TRAPSIG },
1101 { "-isig", "Disable signal trapping", clearmode, 1, MODE_TRAPSIG },
1102 { "edit", "Enable character editing", setmod, 1, MODE_EDIT },
1103 { "+edit", 0, setmod, 1, MODE_EDIT },
1104 { "-edit", "Disable character editing", clearmode, 1, MODE_EDIT },
1105 { "softtabs", "Enable tab expansion", setmod, 1, MODE_SOFT_TAB },
1106 { "+softtabs", 0, setmod, 1, MODE_SOFT_TAB },
1107 { "-softtabs", "Disable character editing", clearmode, 1, MODE_SOFT_TAB },
1108 { "litecho", "Enable literal character echo", setmod, 1, MODE_LIT_ECHO },
1109 { "+litecho", 0, setmod, 1, MODE_LIT_ECHO },
1110 { "-litecho", "Disable literal character echo", clearmode, 1, MODE_LIT_ECHO },
1111 { "help", 0, (int (*)(int))modehelp, 0, 0 },
1112 #ifdef KLUDGELINEMODE
1113 { "kludgeline", 0, (int (*)(int))dokludgemode, 1, 0 },
1115 { "", "", NULL, 0, 0 },
1116 { "?", "Print help information", (int (*)(int))modehelp, 0, 0 },
1117 { NULL, NULL, NULL, 0, 0 },
1124 struct modelist *mt;
1126 printf("format is: 'mode Mode', where 'Mode' is one of:\n\n");
1127 for (mt = ModeList; mt->name; mt++) {
1130 printf("%-15s %s\n", mt->name, mt->help);
1138 #define GETMODECMD(name) (struct modelist *) \
1139 genget(name, (char **) ModeList, sizeof(struct modelist))
1142 modecmd(int argc, char *argv[])
1144 struct modelist *mt;
1147 printf("'mode' command requires an argument\n");
1148 printf("'mode ?' for help.\n");
1149 } else if ((mt = GETMODECMD(argv[1])) == 0) {
1150 fprintf(stderr, "Unknown mode '%s' ('mode ?' for help).\n", argv[1]);
1151 } else if (Ambiguous((void *)mt)) {
1152 fprintf(stderr, "Ambiguous mode '%s' ('mode ?' for help).\n", argv[1]);
1153 } else if (mt->needconnect && !connected) {
1154 printf("?Need to be connected first.\n");
1155 printf("'mode ?' for help.\n");
1156 } else if (mt->handler) {
1157 return (*mt->handler)(mt->arg1);
1163 * The following data structures and routines implement the
1164 * "display" command.
1168 display(int argc, char *argv[])
1170 struct togglelist *tl;
1173 #define dotog(tl) if (tl->variable && tl->actionexplanation) { \
1174 if (*tl->variable) { \
1179 printf(" %s.\n", tl->actionexplanation); \
1182 #define doset(sl) if (sl->name && *sl->name != ' ') { \
1183 if (sl->handler == 0) \
1184 printf("%-15s [%s]\n", sl->name, control(*sl->charp)); \
1186 printf("%-15s \"%s\"\n", sl->name, (char *)sl->charp); \
1190 for (tl = Togglelist; tl->name; tl++) {
1194 for (sl = Setlist; sl->name; sl++) {
1200 for (i = 1; i < argc; i++) {
1201 sl = getset(argv[i]);
1202 tl = GETTOGGLE(argv[i]);
1203 if (Ambiguous((void *)sl) || Ambiguous((void *)tl)) {
1204 printf("?Ambiguous argument '%s'.\n", argv[i]);
1206 } else if (!sl && !tl) {
1207 printf("?Unknown argument '%s'.\n", argv[i]);
1219 /*@*/optionstatus();
1226 * The following are the data structures, and many of the routines,
1227 * relating to command processing.
1231 * Set the escape character.
1234 setescape(int argc, char *argv[])
1240 "Deprecated usage - please use 'set escape%s%s' in the future.\n",
1241 (argc > 2)? " ":"", (argc > 2)? argv[1]: "");
1245 printf("new escape character: ");
1246 (void) fgets(buf, sizeof(buf), stdin);
1251 (void) fflush(stdout);
1259 printf("Deprecated usage - please use 'toggle crmod' in the future.\n");
1260 printf("%s map carriage return on output.\n", crmod ? "Will" : "Won't");
1261 (void) fflush(stdout);
1271 long oldrows, oldcols, newrows, newcols, err_;
1273 err_ = (TerminalWindowSize(&oldrows, &oldcols) == 0) ? 1 : 0;
1274 (void) kill(0, SIGTSTP);
1276 * If we didn't get the window size before the SUSPEND, but we
1277 * can get them now (?), then send the NAWS to make sure that
1278 * we are set up for the right window size.
1280 if (TerminalWindowSize(&newrows, &newcols) && connected &&
1281 (err_ || ((oldrows != newrows) || (oldcols != newcols)))) {
1285 /* reget parameters in case they were changed */
1286 TerminalSaveState();
1289 printf("Suspend is not supported. Try the '!' command instead\n");
1295 shell(int argc, char *argv[] __unused)
1297 long oldrows, oldcols, newrows, newcols, err_;
1301 err_ = (TerminalWindowSize(&oldrows, &oldcols) == 0) ? 1 : 0;
1304 perror("Fork failed\n");
1310 * Fire up the shell in the child.
1312 const char *shellp, *shellname;
1314 shellp = getenv("SHELL");
1317 if ((shellname = strrchr(shellp, '/')) == 0)
1322 execl(shellp, shellname, "-c", &saveline[1], (char *)0);
1324 execl(shellp, shellname, (char *)0);
1329 (void)wait((int *)0); /* Wait for the shell to complete */
1331 if (TerminalWindowSize(&newrows, &newcols) && connected &&
1332 (err_ || ((oldrows != newrows) || (oldcols != newcols)))) {
1341 bye(int argc, char *argv[])
1343 extern int resettermname;
1346 (void) shutdown(net, 2);
1347 printf("Connection closed.\n");
1348 (void) NetClose(net);
1354 if ((argc != 2) || (strcmp(argv[1], "fromquit") != 0)) {
1355 longjmp(toplevel, 1);
1358 return 1; /* Keep lint, etc., happy */
1364 (void) call(bye, "bye", "fromquit", 0);
1371 send_do(TELOPT_LOGOUT, 1);
1384 void (*handler)(int);
1388 static void slc_help(void);
1390 struct slclist SlcList[] = {
1391 { "export", "Use local special character definitions",
1392 (void (*)(int))slc_mode_export, 0 },
1393 { "import", "Use remote special character definitions",
1394 slc_mode_import, 1 },
1395 { "check", "Verify remote special character definitions",
1396 slc_mode_import, 0 },
1397 { "help", NULL, (void (*)(int))slc_help, 0 },
1398 { "?", "Print help information", (void (*)(int))slc_help, 0 },
1399 { NULL, NULL, NULL, 0 },
1407 for (c = SlcList; c->name; c++) {
1410 printf("%-15s %s\n", c->name, c->help);
1417 static struct slclist *
1420 return (struct slclist *)
1421 genget(name, (char **) SlcList, sizeof(struct slclist));
1425 slccmd(int argc, char *argv[])
1431 "Need an argument to 'slc' command. 'slc ?' for help.\n");
1434 c = getslc(argv[1]);
1436 fprintf(stderr, "'%s': unknown argument ('slc ?' for help).\n",
1440 if (Ambiguous((void *)c)) {
1441 fprintf(stderr, "'%s': ambiguous argument ('slc ?' for help).\n",
1445 (*c->handler)(c->arg);
1451 * The ENVIRON command.
1457 void (*handler)(unsigned char *, unsigned char *);
1461 extern struct env_lst *
1462 env_define(const unsigned char *, unsigned char *);
1464 env_undefine(unsigned char *),
1465 env_export(const unsigned char *),
1466 env_unexport(const unsigned char *),
1467 env_send(unsigned char *),
1468 #if defined(OLD_ENVIRON) && defined(ENV_HACK)
1469 env_varval(unsigned char *),
1475 struct envlist EnvList[] = {
1476 { "define", "Define an environment variable",
1477 (void (*)(unsigned char *, unsigned char *))env_define, 2 },
1478 { "undefine", "Undefine an environment variable",
1479 (void (*)(unsigned char *, unsigned char *))env_undefine, 1 },
1480 { "export", "Mark an environment variable for automatic export",
1481 (void (*)(unsigned char *, unsigned char *))env_export, 1 },
1482 { "unexport", "Don't mark an environment variable for automatic export",
1483 (void (*)(unsigned char *, unsigned char *))env_unexport, 1 },
1484 { "send", "Send an environment variable", (void (*)(unsigned char *, unsigned char *))env_send, 1 },
1485 { "list", "List the current environment variables",
1486 (void (*)(unsigned char *, unsigned char *))env_list, 0 },
1487 #if defined(OLD_ENVIRON) && defined(ENV_HACK)
1488 { "varval", "Reverse VAR and VALUE (auto, right, wrong, status)",
1489 (void (*)(unsigned char *, unsigned char *))env_varval, 1 },
1491 { "help", NULL, (void (*)(unsigned char *, unsigned char *))env_help, 0 },
1492 { "?", "Print help information", (void (*)(unsigned char *, unsigned char *))env_help, 0 },
1493 { NULL, NULL, NULL, 0 },
1501 for (c = EnvList; c->name; c++) {
1504 printf("%-15s %s\n", c->name, c->help);
1511 static struct envlist *
1512 getenvcmd(char *name)
1514 return (struct envlist *)
1515 genget(name, (char **) EnvList, sizeof(struct envlist));
1519 env_cmd(int argc, char *argv[])
1525 "Need an argument to 'environ' command. 'environ ?' for help.\n");
1528 c = getenvcmd(argv[1]);
1530 fprintf(stderr, "'%s': unknown argument ('environ ?' for help).\n",
1534 if (Ambiguous((void *)c)) {
1535 fprintf(stderr, "'%s': ambiguous argument ('environ ?' for help).\n",
1539 if (c->narg + 2 != argc) {
1541 "Need %s%d argument%s to 'environ %s' command. 'environ ?' for help.\n",
1542 c->narg < argc + 2 ? "only " : "",
1543 c->narg, c->narg == 1 ? "" : "s", c->name);
1546 (*c->handler)(argv[2], argv[3]);
1551 struct env_lst *next; /* pointer to next structure */
1552 struct env_lst *prev; /* pointer to previous structure */
1553 unsigned char *var; /* pointer to variable name */
1554 unsigned char *value; /* pointer to variable value */
1555 int export; /* 1 -> export with default list of variables */
1556 int welldefined; /* A well defined variable */
1559 struct env_lst envlisthead;
1561 static struct env_lst *
1562 env_find(const unsigned char *var)
1566 for (ep = envlisthead.next; ep; ep = ep->next) {
1567 if (strcmp(ep->var, var) == 0)
1576 extern char **environ;
1580 for (epp = environ; *epp; epp++) {
1581 if ((cp = strchr(*epp, '='))) {
1583 ep = env_define((unsigned char *)*epp,
1584 (unsigned char *)cp+1);
1590 * Special case for DISPLAY variable. If it is ":0.0" or
1591 * "unix:0.0", we have to get rid of "unix" and insert our
1594 if ((ep = env_find("DISPLAY"))
1595 && ((*ep->value == ':')
1596 || (strncmp((char *)ep->value, "unix:", 5) == 0))) {
1598 char *cp2 = strchr((char *)ep->value, ':');
1600 gethostname(hbuf, 256);
1602 cp = (char *)malloc(strlen(hbuf) + strlen(cp2) + 1);
1603 sprintf((char *)cp, "%s%s", hbuf, cp2);
1605 ep->value = (unsigned char *)cp;
1608 * If USER is not defined, but LOGNAME is, then add
1609 * USER with the value from LOGNAME. By default, we
1610 * don't export the USER variable.
1612 if ((env_find("USER") == NULL) && (ep = env_find("LOGNAME"))) {
1613 env_define("USER", ep->value);
1614 env_unexport("USER");
1616 env_export("DISPLAY");
1617 env_export("PRINTER");
1621 env_define(const unsigned char *var, unsigned char *value)
1625 if ((ep = env_find(var))) {
1631 ep = (struct env_lst *)malloc(sizeof(struct env_lst));
1632 ep->next = envlisthead.next;
1633 envlisthead.next = ep;
1634 ep->prev = &envlisthead;
1636 ep->next->prev = ep;
1638 ep->welldefined = opt_welldefined(var);
1640 ep->var = strdup(var);
1641 ep->value = strdup(value);
1646 env_undefine(unsigned char *var)
1650 if ((ep = env_find(var))) {
1651 ep->prev->next = ep->next;
1653 ep->next->prev = ep->prev;
1663 env_export(const unsigned char *var)
1667 if ((ep = env_find(var)))
1672 env_unexport(const unsigned char *var)
1676 if ((ep = env_find(var)))
1681 env_send(unsigned char *var)
1685 if (my_state_is_wont(TELOPT_NEW_ENVIRON)
1687 && my_state_is_wont(TELOPT_OLD_ENVIRON)
1691 "Cannot send '%s': Telnet ENVIRON option not enabled\n",
1697 fprintf(stderr, "Cannot send '%s': variable not defined\n",
1701 env_opt_start_info();
1702 env_opt_add(ep->var);
1711 for (ep = envlisthead.next; ep; ep = ep->next) {
1712 printf("%c %-20s %s\n", ep->export ? '*' : ' ',
1713 ep->var, ep->value);
1718 env_default(int init, int welldefined)
1720 static struct env_lst *nep = NULL;
1727 while ((nep = nep->next)) {
1728 if (nep->export && (nep->welldefined == welldefined))
1736 env_getvalue(const unsigned char *var)
1740 if ((ep = env_find(var)))
1745 #if defined(OLD_ENVIRON) && defined(ENV_HACK)
1747 env_varval(unsigned char *what)
1749 extern int old_env_var, old_env_value, env_auto;
1750 int len = strlen((char *)what);
1755 if (strncasecmp((char *)what, "status", len) == 0) {
1757 printf("%s%s", "VAR and VALUE are/will be ",
1758 "determined automatically\n");
1759 if (old_env_var == OLD_ENV_VAR)
1760 printf("VAR and VALUE set to correct definitions\n");
1762 printf("VAR and VALUE definitions are reversed\n");
1763 } else if (strncasecmp((char *)what, "auto", len) == 0) {
1765 old_env_var = OLD_ENV_VALUE;
1766 old_env_value = OLD_ENV_VAR;
1767 } else if (strncasecmp((char *)what, "right", len) == 0) {
1769 old_env_var = OLD_ENV_VAR;
1770 old_env_value = OLD_ENV_VALUE;
1771 } else if (strncasecmp((char *)what, "wrong", len) == 0) {
1773 old_env_var = OLD_ENV_VALUE;
1774 old_env_value = OLD_ENV_VAR;
1777 printf("Unknown \"varval\" command. (\"auto\", \"right\", \"wrong\", \"status\")\n");
1785 * Print status about the connection.
1789 status(int argc, char *argv[])
1792 printf("Connected to %s.\n", hostname);
1793 if ((argc < 2) || strcmp(argv[1], "notmuch")) {
1794 int mode = getconnmode();
1796 if (my_want_state_is_will(TELOPT_LINEMODE)) {
1797 printf("Operating with LINEMODE option\n");
1798 printf("%s line editing\n", (mode&MODE_EDIT) ? "Local" : "No");
1799 printf("%s catching of signals\n",
1800 (mode&MODE_TRAPSIG) ? "Local" : "No");
1802 #ifdef KLUDGELINEMODE
1803 } else if (kludgelinemode && my_want_state_is_dont(TELOPT_SGA)) {
1804 printf("Operating in obsolete linemode\n");
1807 printf("Operating in single character mode\n");
1809 printf("Catching signals locally\n");
1811 printf("%s character echo\n", (mode&MODE_ECHO) ? "Local" : "Remote");
1812 if (my_want_state_is_will(TELOPT_LFLOW))
1813 printf("%s flow control\n", (mode&MODE_FLOW) ? "Local" : "No");
1816 printf("No connection.\n");
1818 printf("Escape character is '%s'.\n", control(escape));
1819 (void) fflush(stdout);
1825 * Function that gets called when SIGINFO is received.
1830 (void) call(status, "status", "notmuch", 0);
1835 sockaddr_ntop(struct sockaddr *sa)
1838 static char addrbuf[INET6_ADDRSTRLEN];
1840 switch (sa->sa_family) {
1842 addr = &((struct sockaddr_in *)sa)->sin_addr;
1845 addr = &((struct sockaddr_un *)sa)->sun_path;
1849 addr = &((struct sockaddr_in6 *)sa)->sin6_addr;
1855 inet_ntop(sa->sa_family, addr, addrbuf, sizeof(addrbuf));
1859 #if defined(IPSEC) && defined(IPSEC_POLICY_IPSEC)
1861 setpolicy(int lnet, struct addrinfo *res, char *policy)
1870 buf = ipsec_set_policy(policy, strlen(policy));
1872 printf("%s\n", ipsec_strerror());
1875 level = res->ai_family == AF_INET ? IPPROTO_IP : IPPROTO_IPV6;
1876 optname = res->ai_family == AF_INET ? IP_IPSEC_POLICY : IPV6_IPSEC_POLICY;
1877 if (setsockopt(lnet, level, optname, buf, ipsec_get_policylen(buf)) < 0){
1878 perror("setsockopt");
1889 * When an Address Family related error happend, check if retry with
1890 * another AF is possible or not.
1891 * Return 1, if retry with another af is OK. Else, return 0.
1894 switch_af(struct addrinfo **aip)
1897 struct addrinfo *ai;
1900 nextaf = (ai->ai_family == AF_INET) ? AF_INET6 : AF_INET;
1903 while (ai != NULL && ai->ai_family != nextaf);
1913 tn(int argc, char *argv[])
1918 int srcroute = 0, result;
1919 char *cmd, *hostp = 0, *portp = 0, *user = 0;
1920 char *src_addr = NULL;
1921 struct addrinfo hints, *res, *res0 = NULL, *src_res, *src_res0 = NULL;
1922 int error = 0, af_error = 0;
1925 printf("?Already connected to %s\n", hostname);
1930 (void) strcpy(line, "open ");
1932 (void) fgets(&line[strlen(line)], sizeof(line) - strlen(line), stdin);
1940 if (strcmp(*argv, "help") == 0 || isprefix(*argv, "?"))
1942 if (strcmp(*argv, "-l") == 0) {
1950 if (strcmp(*argv, "-a") == 0) {
1955 if (strcmp(*argv, "-s") == 0) {
1974 printf("usage: %s [-l user] [-a] [-s src_addr] host-name [port]\n", cmd);
1981 if (src_addr != NULL) {
1982 memset(&hints, 0, sizeof(hints));
1983 hints.ai_flags = AI_NUMERICHOST;
1984 hints.ai_family = family;
1985 hints.ai_socktype = SOCK_STREAM;
1986 error = getaddrinfo(src_addr, 0, &hints, &src_res);
1987 if (error == EAI_NODATA) {
1989 error = getaddrinfo(src_addr, 0, &hints, &src_res);
1992 fprintf(stderr, "%s: %s\n", src_addr, gai_strerror(error));
1993 if (error == EAI_SYSTEM)
1994 fprintf(stderr, "%s: %s\n", src_addr, strerror(errno));
2000 if (hostp[0] == '/') {
2001 struct sockaddr_un su;
2003 if (strlen(hostp) >= sizeof(su.sun_path)) {
2004 fprintf(stderr, "hostname too long for unix domain socket: %s",
2008 memset(&su, 0, sizeof su);
2009 su.sun_family = AF_UNIX;
2010 strncpy(su.sun_path, hostp, sizeof su.sun_path);
2011 printf("Trying %s...\n", hostp);
2012 net = socket(PF_UNIX, SOCK_STREAM, 0);
2017 if (connect(net, (struct sockaddr *)&su, sizeof su) == -1) {
2018 perror(su.sun_path);
2019 (void) NetClose(net);
2023 } else if (hostp[0] == '@' || hostp[0] == '!') {
2026 family == AF_INET6 ||
2028 (hostname = strrchr(hostp, ':')) == NULL)
2029 hostname = strrchr(hostp, '@');
2030 if (hostname == NULL) {
2040 portp = strdup("telnet");
2041 } else if (*portp == '-') {
2047 memset(&hints, 0, sizeof(hints));
2048 hints.ai_flags = AI_NUMERICHOST;
2049 hints.ai_family = family;
2050 hints.ai_socktype = SOCK_STREAM;
2051 error = getaddrinfo(hostname, portp, &hints, &res);
2053 hints.ai_flags = AI_CANONNAME;
2054 error = getaddrinfo(hostname, portp, &hints, &res);
2057 fprintf(stderr, "%s: %s\n", hostname, gai_strerror(error));
2058 if (error == EAI_SYSTEM)
2059 fprintf(stderr, "%s: %s\n", hostname, strerror(errno));
2063 if (hints.ai_flags == AI_NUMERICHOST) {
2064 /* hostname has numeric */
2068 gni_err = getnameinfo(res->ai_addr, res->ai_addr->sa_len,
2069 _hostname, sizeof(_hostname) - 1, NULL, 0,
2072 (void) strncpy(_hostname, hostp, sizeof(_hostname) - 1);
2073 _hostname[sizeof(_hostname)-1] = '\0';
2074 hostname = _hostname;
2076 /* hostname has FQDN */
2078 (void) strncpy(_hostname, hostname, sizeof(_hostname) - 1);
2079 else if (res->ai_canonname != NULL)
2080 strcpy(_hostname, res->ai_canonname);
2082 (void) strncpy(_hostname, hostp, sizeof(_hostname) - 1);
2083 _hostname[sizeof(_hostname)-1] = '\0';
2084 hostname = _hostname;
2090 if (srcroute != 0) {
2091 static char hostbuf[BUFSIZ];
2093 if (af_error == 0) { /* save intermediate hostnames for retry */
2094 strncpy(hostbuf, hostp, BUFSIZ - 1);
2095 hostbuf[BUFSIZ - 1] = '\0';
2099 result = sourceroute(res, hostp, &srp, &srlen, &proto, &opt);
2102 if (family == AF_UNSPEC && af_error == 0 &&
2103 switch_af(&res) == 1) {
2110 } else if (result == -1) {
2111 printf("Bad source route option: %s\n", hostp);
2117 printf("Trying %s...\n", sockaddr_ntop(res->ai_addr));
2118 net = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
2122 if (family == AF_UNSPEC && af_error == 0 &&
2123 switch_af(&res) == 1) {
2128 perror("telnet: socket");
2131 if (srp && setsockopt(net, proto, opt, (char *)srp, srlen) < 0)
2132 perror("setsockopt (source route)");
2133 #if defined(IPPROTO_IP) && defined(IP_TOS)
2134 if (res->ai_family == PF_INET) {
2135 # if defined(HAS_GETTOS)
2137 if (tos < 0 && (tp = gettosbyname("telnet", "tcp")))
2141 tos = IPTOS_LOWDELAY;
2143 && (setsockopt(net, IPPROTO_IP, IP_TOS,
2144 (char *)&tos, sizeof(int)) < 0)
2145 && (errno != ENOPROTOOPT))
2146 perror("telnet: setsockopt (IP_TOS) (ignored)");
2148 #endif /* defined(IPPROTO_IP) && defined(IP_TOS) */
2150 if (debug && SetSockOpt(net, SOL_SOCKET, SO_DEBUG, 1) < 0) {
2151 perror("setsockopt (SO_DEBUG)");
2154 if (src_addr != NULL) {
2155 for (src_res = src_res0; src_res != 0; src_res = src_res->ai_next)
2156 if (src_res->ai_family == res->ai_family)
2158 if (src_res == NULL)
2160 if (bind(net, src_res->ai_addr, src_res->ai_addrlen) == -1) {
2162 if (family == AF_UNSPEC && af_error == 0 &&
2163 switch_af(&res) == 1) {
2165 (void) NetClose(net);
2170 (void) NetClose(net);
2174 #if defined(IPSEC) && defined(IPSEC_POLICY_IPSEC)
2175 if (setpolicy(net, res, ipsec_policy_in) < 0) {
2176 (void) NetClose(net);
2179 if (setpolicy(net, res, ipsec_policy_out) < 0) {
2180 (void) NetClose(net);
2185 if (connect(net, res->ai_addr, res->ai_addrlen) < 0) {
2186 struct addrinfo *next;
2188 next = res->ai_next;
2189 /* If already an af failed, only try same af. */
2191 while (next != NULL && next->ai_family != res->ai_family)
2192 next = next->ai_next;
2193 warn("connect to address %s", sockaddr_ntop(res->ai_addr));
2196 (void) NetClose(net);
2199 warnx("Unable to connect to remote host");
2200 (void) NetClose(net);
2204 } while (connected == 0);
2206 if (src_res0 != NULL)
2207 freeaddrinfo(src_res0);
2208 cmdrc(hostp, hostname);
2210 if (autologin && user == NULL) {
2213 user = getenv("USER");
2215 ((pw = getpwnam(user)) && pw->pw_uid != getuid())) {
2216 if ((pw = getpwuid(getuid())))
2223 env_define("USER", user);
2226 (void) call(status, "status", "notmuch", 0);
2227 if (setjmp(peerdied) == 0)
2229 (void) NetClose(net);
2230 ExitString("Connection closed by foreign host.\n",1);
2235 if (src_res0 != NULL)
2236 freeaddrinfo(src_res0);
2240 #define HELPINDENT (sizeof ("connect"))
2243 openhelp[] = "connect to a site",
2244 closehelp[] = "close current connection",
2245 logouthelp[] = "forcibly logout remote user and close the connection",
2246 quithelp[] = "exit telnet",
2247 statushelp[] = "print status information",
2248 helphelp[] = "print help information",
2249 sendhelp[] = "transmit special characters ('send ?' for more)",
2250 sethelp[] = "set operating parameters ('set ?' for more)",
2251 unsethelp[] = "unset operating parameters ('unset ?' for more)",
2252 togglestring[] ="toggle operating parameters ('toggle ?' for more)",
2253 slchelp[] = "change state of special charaters ('slc ?' for more)",
2254 displayhelp[] = "display operating parameters",
2255 zhelp[] = "suspend telnet",
2257 opiehelp[] = "compute response to OPIE challenge",
2259 shellhelp[] = "invoke a subshell",
2260 envhelp[] = "change environment variables ('environ ?' for more)",
2261 modestring[] = "try to enter line or character mode ('mode ?' for more)";
2263 static Command cmdtab[] = {
2264 { "close", closehelp, bye, 1 },
2265 { "logout", logouthelp, (int (*)(int, char **))logout, 1 },
2266 { "display", displayhelp, display, 0 },
2267 { "mode", modestring, modecmd, 0 },
2268 { "telnet", openhelp, tn, 0 },
2269 { "open", openhelp, tn, 0 },
2270 { "quit", quithelp, (int (*)(int, char **))quit, 0 },
2271 { "send", sendhelp, sendcmd, 0 },
2272 { "set", sethelp, setcmd, 0 },
2273 { "unset", unsethelp, unsetcmd, 0 },
2274 { "status", statushelp, status, 0 },
2275 { "toggle", togglestring, toggle, 0 },
2276 { "slc", slchelp, slccmd, 0 },
2277 { "z", zhelp, (int (*)(int, char **))suspend, 0 },
2278 { "!", shellhelp, shell, 1 },
2279 { "environ", envhelp, env_cmd, 0 },
2280 { "?", helphelp, help, 0 },
2282 { "opie", opiehelp, opie_calc, 0 },
2284 { NULL, NULL, NULL, 0 }
2287 static char crmodhelp[] = "deprecated command -- use 'toggle crmod' instead";
2288 static char escapehelp[] = "deprecated command -- use 'set escape' instead";
2290 static Command cmdtab2[] = {
2291 { "help", 0, help, 0 },
2292 { "escape", escapehelp, setescape, 0 },
2293 { "crmod", crmodhelp, (int (*)(int, char **))togcrmod, 0 },
2294 { NULL, NULL, NULL, 0 }
2299 * Call routine with argc, argv set from args (terminated by 0).
2303 call(intrtn_t routine, ...)
2309 va_start(ap, routine);
2310 while ((args[argno++] = va_arg(ap, char *)) != 0);
2312 return (*routine)(argno-1, args);
2321 if ((cm = (Command *) genget(name, (char **) cmdtab, sizeof(Command))))
2323 return (Command *) genget(name, (char **) cmdtab2, sizeof(Command));
2327 command(int top, const char *tbuf, int cnt)
2335 (void) signal(SIGINT, SIG_DFL);
2336 (void) signal(SIGQUIT, SIG_DFL);
2339 if (rlogin == _POSIX_VDISABLE)
2340 printf("%s> ", prompt);
2344 while (cnt > 0 && (*cp++ = *tbuf++) != '\n')
2347 if (cp == line || *--cp != '\n' || cp == line)
2350 if (rlogin == _POSIX_VDISABLE)
2351 printf("%s\n", line);
2354 if (rlogin != _POSIX_VDISABLE)
2355 printf("%s> ", prompt);
2356 if (fgets(line, sizeof(line), stdin) == NULL) {
2357 if (feof(stdin) || ferror(stdin)) {
2367 if (margv[0] == 0) {
2370 c = getcmd(margv[0]);
2371 if (Ambiguous((void *)c)) {
2372 printf("?Ambiguous command\n");
2376 printf("?Invalid command\n");
2379 if (c->needconnect && !connected) {
2380 printf("?Need to be connected first.\n");
2383 if ((*c->handler)(margc, margv)) {
2389 longjmp(toplevel, 1);
2400 help(int argc, char *argv[])
2405 printf("Commands may be abbreviated. Commands are:\n\n");
2406 for (c = cmdtab; c->name; c++)
2408 printf("%-*s\t%s\n", (int)HELPINDENT, c->name,
2413 else while (--argc > 0) {
2417 if (Ambiguous((void *)c))
2418 printf("?Ambiguous help command %s\n", arg);
2419 else if (c == (Command *)0)
2420 printf("?Invalid help command %s\n", arg);
2422 printf("%s\n", c->help);
2427 static char *rcname = 0;
2428 static char rcbuf[128];
2431 cmdrc(char *m1, char *m2)
2436 int l1 = strlen(m1);
2437 int l2 = strlen(m2);
2438 char m1save[MAXHOSTNAMELEN];
2443 strlcpy(m1save, m1, sizeof(m1save));
2447 rcname = getenv("HOME");
2448 if (rcname && (strlen(rcname) + 10) < sizeof(rcbuf))
2449 strcpy(rcbuf, rcname);
2452 strcat(rcbuf, "/.telnetrc");
2456 if ((rcfile = fopen(rcname, "r")) == 0) {
2461 if (fgets(line, sizeof(line), rcfile) == NULL)
2468 if (!isspace(line[0]))
2471 if (gotmachine == 0) {
2472 if (isspace(line[0]))
2474 if (strncasecmp(line, m1, l1) == 0)
2475 strncpy(line, &line[l1], sizeof(line) - l1);
2476 else if (strncasecmp(line, m2, l2) == 0)
2477 strncpy(line, &line[l2], sizeof(line) - l2);
2478 else if (strncasecmp(line, "DEFAULT", 7) == 0)
2479 strncpy(line, &line[7], sizeof(line) - 7);
2482 if (line[0] != ' ' && line[0] != '\t' && line[0] != '\n')
2489 c = getcmd(margv[0]);
2490 if (Ambiguous((void *)c)) {
2491 printf("?Ambiguous command: %s\n", margv[0]);
2495 printf("?Invalid command: %s\n", margv[0]);
2499 * This should never happen...
2501 if (c->needconnect && !connected) {
2502 printf("?Need to be connected first for %s.\n", margv[0]);
2505 (*c->handler)(margc, margv);
2511 * Source route is handed in as
2512 * [!]@hop1@hop2...[@|:]dst
2513 * If the leading ! is present, it is a
2514 * strict source route, otherwise it is
2515 * assmed to be a loose source route.
2517 * We fill in the source route option as
2518 * hop1,hop2,hop3...dest
2519 * and return a pointer to hop1, which will
2520 * be the address to connect() to.
2524 * res: ponter to addrinfo structure which contains sockaddr to
2525 * the host to connect to.
2527 * arg: pointer to route list to decipher
2529 * cpp: If *cpp is not equal to NULL, this is a
2530 * pointer to a pointer to a character array
2531 * that should be filled in with the option.
2533 * lenp: pointer to an integer that contains the
2534 * length of *cpp if *cpp != NULL.
2536 * protop: pointer to an integer that should be filled in with
2537 * appropriate protocol for setsockopt, as socket
2540 * optp: pointer to an integer that should be filled in with
2541 * appropriate option for setsockopt, as socket protocol
2546 * If the return value is 1, then all operations are
2547 * successful. If the
2548 * return value is -1, there was a syntax error in the
2549 * option, either unknown characters, or too many hosts.
2550 * If the return value is 0, one of the hostnames in the
2551 * path is unknown, and *cpp is set to point to the bad
2554 * *cpp: If *cpp was equal to NULL, it will be filled
2555 * in with a pointer to our static area that has
2556 * the option filled in. This will be 32bit aligned.
2558 * *lenp: This will be filled in with how long the option
2559 * pointed to by *cpp is.
2561 * *protop: This will be filled in with appropriate protocol for
2562 * setsockopt, as socket protocol family.
2564 * *optp: This will be filled in with appropriate option for
2565 * setsockopt, as socket protocol family.
2568 sourceroute(struct addrinfo *ai, char *arg, char **cpp, int *lenp, int *protop, int *optp)
2570 static char buf[1024 + ALIGNBYTES]; /*XXX*/
2571 char *cp, *cp2, *lsrp, *ep;
2572 struct sockaddr_in *_sin;
2574 struct sockaddr_in6 *sin6;
2575 struct cmsghdr *cmsg;
2577 struct addrinfo hints, *res;
2582 * Verify the arguments, and make sure we have
2583 * at least 7 bytes for the option.
2585 if (cpp == NULL || lenp == NULL)
2588 switch (res->ai_family) {
2595 if (*lenp < (int)CMSG_SPACE(sizeof(struct ip6_rthdr) +
2596 sizeof(struct in6_addr)))
2603 * Decide whether we have a buffer passed to us,
2604 * or if we need to use our own static buffer.
2610 *cpp = lsrp = (char *)ALIGN(buf);
2617 if (ai->ai_family == AF_INET6) {
2618 cmsg = inet6_rthdr_init(*cpp, IPV6_RTHDR_TYPE_0);
2621 *protop = IPPROTO_IPV6;
2622 *optp = IPV6_PKTOPTIONS;
2627 * Next, decide whether we have a loose source
2628 * route or a strict source route, and fill in
2629 * the begining of the option.
2633 *lsrp++ = IPOPT_SSRR;
2635 *lsrp++ = IPOPT_LSRR;
2640 lsrp++; /* skip over length, we'll fill it in later */
2642 *protop = IPPROTO_IP;
2647 memset(&hints, 0, sizeof(hints));
2648 hints.ai_family = ai->ai_family;
2649 hints.ai_socktype = SOCK_STREAM;
2653 ai->ai_family != AF_INET6 &&
2657 else for (cp2 = cp; (c = *cp2); cp2++) {
2662 } else if (c == '@') {
2666 ai->ai_family != AF_INET6 &&
2677 hints.ai_flags = AI_NUMERICHOST;
2678 error = getaddrinfo(cp, NULL, &hints, &res);
2679 if (error == EAI_NODATA) {
2681 error = getaddrinfo(cp, NULL, &hints, &res);
2684 fprintf(stderr, "%s: %s\n", cp, gai_strerror(error));
2685 if (error == EAI_SYSTEM)
2686 fprintf(stderr, "%s: %s\n", cp,
2692 if (res->ai_family == AF_INET6) {
2693 sin6 = (struct sockaddr_in6 *)res->ai_addr;
2694 inet6_rthdr_add(cmsg, &sin6->sin6_addr,
2699 _sin = (struct sockaddr_in *)res->ai_addr;
2700 memcpy(lsrp, (char *)&_sin->sin_addr, 4);
2708 * Check to make sure there is space for next address
2711 if (res->ai_family == AF_INET6) {
2712 if (((char *)CMSG_DATA(cmsg) +
2713 sizeof(struct ip6_rthdr) +
2714 ((inet6_rthdr_segments(cmsg) + 1) *
2715 sizeof(struct in6_addr))) > ep)
2724 if (res->ai_family == AF_INET6) {
2725 inet6_rthdr_lasthop(cmsg, IPV6_RTHDR_LOOSE);
2726 *lenp = cmsg->cmsg_len;
2730 if ((*(*cpp+IPOPT_OLEN) = lsrp - *cpp) <= 7) {
2735 *lsrp++ = IPOPT_NOP; /* 32 bit word align it */
2736 *lenp = lsrp - *cpp;