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
33 * @(#)commands.c 8.4 (Berkeley) 5/30/95
34 * $FreeBSD: src/usr.bin/telnet/commands.c,v 1.21.2.6 2002/11/30 05:35:13 eric Exp $
35 * $DragonFly: src/usr.bin/telnet/commands.c,v 1.2 2003/06/17 04:29:32 dillon Exp $
38 #include <sys/param.h>
41 #include <sys/socket.h>
42 #include <netinet/in.h>
55 #include <arpa/telnet.h>
56 #include <arpa/inet.h>
68 #include <netinet/in_systm.h>
69 #include <netinet/ip.h>
70 #include <netinet/ip6.h>
72 #ifndef MAXHOSTNAMELEN
73 #define MAXHOSTNAMELEN 256
76 typedef int (*intrtn_t)(int, char **);
78 #if defined(IPPROTO_IP) && defined(IP_TOS)
80 #endif /* defined(IPPROTO_IP) && defined(IP_TOS) */
83 static char _hostname[MAXHOSTNAMELEN];
85 static int help(int, char **);
86 static int call(intrtn_t, ...);
87 static void cmdrc(char *, char *);
89 static int switch_af(struct addrinfo **);
91 static int togglehelp(void);
92 static int send_tncmd(void (*)(int, int), const char *, char *);
93 static int setmod(int);
94 static int clearmode(int);
95 static int modehelp(void);
96 static int sourceroute(struct addrinfo *, char *, char **, int *, int *, int *);
99 const char *name; /* command name */
100 const char *help; /* help string (NULL for no help) */
101 int (*handler)(int, char **); /* routine which executes command */
102 int needconnect; /* Do we need to be connected to execute? */
105 static char line[256];
106 static char saveline[256];
108 static char *margv[20];
111 #include <sys/wait.h>
112 #define PATH_OPIEKEY "/usr/bin/opiekey"
114 opie_calc(int argc, char *argv[])
119 printf("%s sequence challenge\n", argv[0]);
125 execv(PATH_OPIEKEY, argv);
131 (void) wait(&status);
132 if (WIFEXITED(status))
133 return (WEXITSTATUS(status));
147 if (*cp == '!') { /* Special case shell escape */
148 strcpy(saveline, line); /* save for shell command */
149 *argp++ = strdup("!"); /* No room in string to get this */
161 for (cp2 = cp; c != '\0'; c = *++cp) {
169 if ((c = *++cp) == '\0')
171 } else if (c == '"') {
174 } else if (c == '\'') {
177 } else if (isspace(c))
191 * Make a character string into a number.
193 * Todo: 1. Could take random integers (12, 0x12, 012, 0b1).
206 c = b | 0x40; /* DEL */
219 * Construct a control character sequence
220 * for a special character.
227 * The only way I could get the Sun 3.5 compiler
229 * if ((unsigned int)c >= 0x80)
230 * was to assign "c" to an unsigned int variable...
233 unsigned int uic = (unsigned int)c;
237 if (c == (cc_t)_POSIX_VDISABLE) {
242 buf[1] = ((c>>6)&07) + '0';
243 buf[2] = ((c>>3)&07) + '0';
244 buf[3] = (c&07) + '0';
246 } else if (uic >= 0x20) {
258 * The following are data structures and routines for
259 * the "send" command.
264 const char *name; /* How user refers to it (case independent) */
265 const char *help; /* Help information (0 ==> no help) */
266 int needconnect; /* Need to be connected */
267 int narg; /* Number of arguments */
268 int (*handler)(char *, ...); /* Routine to perform (for special ops) */
269 int nbyte; /* Number of bytes to send this command */
270 int what; /* Character to be sent (<0 ==> special) */
278 send_dontcmd(char *),
279 send_willcmd(char *),
280 send_wontcmd(char *);
282 static struct sendlist Sendlist[] = {
283 { "ao", "Send Telnet Abort output", 1, 0, NULL, 2, AO },
284 { "ayt", "Send Telnet 'Are You There'", 1, 0, NULL, 2, AYT },
285 { "brk", "Send Telnet Break", 1, 0, NULL, 2, BREAK },
286 { "break", NULL, 1, 0, NULL, 2, BREAK },
287 { "ec", "Send Telnet Erase Character", 1, 0, NULL, 2, EC },
288 { "el", "Send Telnet Erase Line", 1, 0, NULL, 2, EL },
289 { "escape", "Send current escape character",1, 0, (int (*)(char *, ...))send_esc, 1, 0 },
290 { "ga", "Send Telnet 'Go Ahead' sequence", 1, 0, NULL, 2, GA },
291 { "ip", "Send Telnet Interrupt Process",1, 0, NULL, 2, IP },
292 { "intp", NULL, 1, 0, NULL, 2, IP },
293 { "interrupt", NULL, 1, 0, NULL, 2, IP },
294 { "intr", NULL, 1, 0, NULL, 2, IP },
295 { "nop", "Send Telnet 'No operation'", 1, 0, NULL, 2, NOP },
296 { "eor", "Send Telnet 'End of Record'", 1, 0, NULL, 2, EOR },
297 { "abort", "Send Telnet 'Abort Process'", 1, 0, NULL, 2, ABORT },
298 { "susp", "Send Telnet 'Suspend Process'",1, 0, NULL, 2, SUSP },
299 { "eof", "Send Telnet End of File Character", 1, 0, NULL, 2, xEOF },
300 { "synch", "Perform Telnet 'Synch operation'", 1, 0, (int (*)(char *, ...))dosynch, 2, 0 },
301 { "getstatus", "Send request for STATUS", 1, 0, (int (*)(char *, ...))get_status, 6, 0 },
302 { "?", "Display send options", 0, 0, (int (*)(char *, ...))send_help, 0, 0 },
303 { "help", NULL, 0, 0, (int (*)(char *, ...))send_help, 0, 0 },
304 { "do", NULL, 0, 1, (int (*)(char *, ...))send_docmd, 3, 0 },
305 { "dont", NULL, 0, 1, (int (*)(char *, ...))send_dontcmd, 3, 0 },
306 { "will", NULL, 0, 1, (int (*)(char *, ...))send_willcmd, 3, 0 },
307 { "wont", NULL, 0, 1, (int (*)(char *, ...))send_wontcmd, 3, 0 },
308 { NULL, NULL, 0, 0, NULL, 0, 0 }
311 #define GETSEND(name) ((struct sendlist *) genget(name, (char **) Sendlist, \
312 sizeof(struct sendlist)))
315 sendcmd(int argc, char *argv[])
317 int count; /* how many bytes we are going to need to send */
319 struct sendlist *s; /* pointer to current command */
324 printf("need at least one argument for 'send' command\n");
325 printf("'send ?' for help\n");
329 * First, validate all the send arguments.
330 * In addition, we see how much space we are going to need, and
331 * whether or not we will be doing a "SYNCH" operation (which
332 * flushes the network queue).
335 for (i = 1; i < argc; i++) {
336 s = GETSEND(argv[i]);
338 printf("Unknown send argument '%s'\n'send ?' for help.\n",
341 } else if (Ambiguous((void *)s)) {
342 printf("Ambiguous send argument '%s'\n'send ?' for help.\n",
346 if (i + s->narg >= argc) {
348 "Need %d argument%s to 'send %s' command. 'send %s ?' for help.\n",
349 s->narg, s->narg == 1 ? "" : "s", s->name, s->name);
353 if ((void *)s->handler == (void *)send_help) {
359 needconnect += s->needconnect;
361 if (!connected && needconnect) {
362 printf("?Need to be connected first.\n");
363 printf("'send ?' for help\n");
366 /* Now, do we have enough room? */
367 if (NETROOM() < count) {
368 printf("There is not enough room in the buffer TO the network\n");
369 printf("to process your request. Nothing will be done.\n");
370 printf("('send synch' will throw away most data in the network\n");
371 printf("buffer, if this might help.)\n");
374 /* OK, they are all OK, now go through again and actually send */
376 for (i = 1; i < argc; i++) {
377 if ((s = GETSEND(argv[i])) == 0) {
378 fprintf(stderr, "Telnet 'send' error - argument disappeared!\n");
384 success += (*s->handler)((s->narg > 0) ? argv[i+1] : 0,
385 (s->narg > 1) ? argv[i+2] : 0);
388 NET2ADD(IAC, s->what);
389 printoption("SENT", IAC, s->what);
392 return (count == success);
403 send_docmd(char *name)
405 return(send_tncmd(send_do, "do", name));
412 return(send_tncmd(send_dont, "dont", name));
416 send_willcmd(char *name)
418 return(send_tncmd(send_will, "will", name));
422 send_wontcmd(char *name)
424 return(send_tncmd(send_wont, "wont", name));
428 send_tncmd(void (*func)(int, int), const char *cmd, char *name)
431 extern char *telopts[];
434 if (isprefix(name, "help") || isprefix(name, "?")) {
437 printf("Usage: send %s <value|option>\n", cmd);
438 printf("\"value\" must be from 0 to 255\n");
439 printf("Valid options are:\n\t");
442 for (cpp = telopts; *cpp; cpp++) {
443 len = strlen(*cpp) + 3;
444 if (col + len > 65) {
448 printf(" \"%s\"", *cpp);
454 cpp = (char **)genget(name, telopts, sizeof(char *));
455 if (Ambiguous(cpp)) {
456 fprintf(stderr,"'%s': ambiguous argument ('send %s ?' for help).\n",
465 while (*cp >= '0' && *cp <= '9') {
471 fprintf(stderr, "'%s': unknown argument ('send %s ?' for help).\n",
474 } else if (val < 0 || val > 255) {
475 fprintf(stderr, "'%s': bad value ('send %s ?' for help).\n",
481 printf("?Need to be connected first.\n");
491 struct sendlist *s; /* pointer to current command */
492 for (s = Sendlist; s->name; s++) {
494 printf("%-15s %s\n", s->name, s->help);
500 * The following are the routines and data structures referred
501 * to by the arguments to the "toggle" command.
516 (SetSockOpt(net, SOL_SOCKET, SO_DEBUG, debug)) < 0) {
517 perror("setsockopt (SO_DEBUG)");
521 if (net > 0 && SetSockOpt(net, SOL_SOCKET, SO_DEBUG, 1) < 0)
522 perror("setsockopt (SO_DEBUG)");
524 printf("Cannot turn off socket debugging\n");
534 printf("Will send carriage returns as telnet <CR><LF>.\n");
536 printf("Will send carriage returns as telnet <CR><NUL>.\n");
546 donebinarytoggle = 1;
551 if (my_want_state_is_will(TELOPT_BINARY) &&
552 my_want_state_is_do(TELOPT_BINARY)) {
554 } else if (my_want_state_is_wont(TELOPT_BINARY) &&
555 my_want_state_is_dont(TELOPT_BINARY)) {
558 val = binmode ? 0 : 1;
562 if (my_want_state_is_will(TELOPT_BINARY) &&
563 my_want_state_is_do(TELOPT_BINARY)) {
564 printf("Already operating in binary mode with remote host.\n");
566 printf("Negotiating binary mode with remote host.\n");
570 if (my_want_state_is_wont(TELOPT_BINARY) &&
571 my_want_state_is_dont(TELOPT_BINARY)) {
572 printf("Already in network ascii mode with remote host.\n");
574 printf("Negotiating network ascii mode with remote host.\n");
584 donebinarytoggle = 1;
587 val = my_want_state_is_do(TELOPT_BINARY) ? 0 : 1;
590 if (my_want_state_is_do(TELOPT_BINARY)) {
591 printf("Already receiving in binary mode.\n");
593 printf("Negotiating binary mode on input.\n");
597 if (my_want_state_is_dont(TELOPT_BINARY)) {
598 printf("Already receiving in network ascii mode.\n");
600 printf("Negotiating network ascii mode on input.\n");
610 donebinarytoggle = 1;
613 val = my_want_state_is_will(TELOPT_BINARY) ? 0 : 1;
616 if (my_want_state_is_will(TELOPT_BINARY)) {
617 printf("Already transmitting in binary mode.\n");
619 printf("Negotiating binary mode on output.\n");
623 if (my_want_state_is_wont(TELOPT_BINARY)) {
624 printf("Already transmitting in network ascii mode.\n");
626 printf("Negotiating network ascii mode on output.\n");
634 const char *name; /* name of toggle */
635 const char *help; /* help message */
636 int (*handler)(int); /* routine to do actual setting */
638 const char *actionexplanation;
641 static struct togglelist Togglelist[] = {
643 "flushing of output when sending interrupt characters",
646 "flush output when sending interrupt characters" },
648 "automatic sending of interrupt characters in urgent mode",
651 "send interrupt characters in urgent mode" },
653 "don't read ~/.telnetrc file",
656 "skip reading of ~/.telnetrc file" },
658 "sending and receiving of binary data",
663 "receiving of binary data",
668 "sending of binary data",
673 "sending carriage returns as telnet <CR><LF>",
674 (int (*)(int))togcrlf,
678 "mapping of received carriage returns",
681 "map carriage return on output" },
683 "local recognition of certain control characters",
684 (int (*)(int))lclchars,
686 "recognize certain control characters" },
687 { " ", "", NULL, NULL, NULL }, /* empty line */
690 (int (*)(int))togdebug,
692 "turn on socket level debugging" },
694 "printing of hexadecimal network data (debugging)",
697 "print hexadecimal representation of network traffic" },
699 "output of \"netdata\" to user readable format (debugging)",
702 "print user readable output for \"netdata\"" },
704 "viewing of options processing (debugging)",
707 "show option processing" },
709 "(debugging) toggle printing of hexadecimal terminal data",
712 "print hexadecimal representation of terminal traffic" },
715 (int (*)(int))togglehelp,
718 { NULL, NULL, NULL, NULL, NULL },
721 (int (*)(int))togglehelp,
724 { NULL, NULL, NULL, NULL, NULL }
730 struct togglelist *c;
732 for (c = Togglelist; c->name; c++) {
735 printf("%-15s toggle %s\n", c->name, c->help);
741 printf("%-15s %s\n", "?", "display help information");
746 settogglehelp(int set)
748 struct togglelist *c;
750 for (c = Togglelist; c->name; c++) {
753 printf("%-15s %s %s\n", c->name, set ? "enable" : "disable",
761 #define GETTOGGLE(name) (struct togglelist *) \
762 genget(name, (char **) Togglelist, sizeof(struct togglelist))
765 toggle(int argc, char *argv[])
769 struct togglelist *c;
773 "Need an argument to 'toggle' command. 'toggle ?' for help.\n");
781 if (Ambiguous((void *)c)) {
782 fprintf(stderr, "'%s': ambiguous argument ('toggle ?' for help).\n",
786 fprintf(stderr, "'%s': unknown argument ('toggle ?' for help).\n",
791 *c->variable = !*c->variable; /* invert it */
792 if (c->actionexplanation) {
793 printf("%s %s.\n", *c->variable? "Will" : "Won't",
794 c->actionexplanation);
798 retval &= (*c->handler)(-1);
806 * The following perform the "set" command.
810 struct termio new_tc = { 0, 0, 0, 0, {}, 0, 0 };
814 const char *name; /* name */
815 const char *help; /* help information */
816 void (*handler)(char *);
817 cc_t *charp; /* where it is located at */
820 static struct setlist Setlist[] = {
821 #ifdef KLUDGELINEMODE
822 { "echo", "character to toggle local echoing on/off", NULL, &echoc },
824 { "escape", "character to escape back to telnet command mode", NULL, &escape },
825 { "rlogin", "rlogin escape character", 0, &rlogin },
826 { "tracefile", "file to write trace information to", SetNetTrace, (cc_t *)NetTraceFile},
827 { " ", "", NULL, NULL },
828 { " ", "The following need 'localchars' to be toggled true", NULL, NULL },
829 { "flushoutput", "character to cause an Abort Output", NULL, termFlushCharp },
830 { "interrupt", "character to cause an Interrupt Process", NULL, termIntCharp },
831 { "quit", "character to cause an Abort process", NULL, termQuitCharp },
832 { "eof", "character to cause an EOF ", NULL, termEofCharp },
833 { " ", "", NULL, NULL },
834 { " ", "The following are for local editing in linemode", NULL, NULL },
835 { "erase", "character to use to erase a character", NULL, termEraseCharp },
836 { "kill", "character to use to erase a line", NULL, termKillCharp },
837 { "lnext", "character to use for literal next", NULL, termLiteralNextCharp },
838 { "susp", "character to cause a Suspend Process", NULL, termSuspCharp },
839 { "reprint", "character to use for line reprint", NULL, termRprntCharp },
840 { "worderase", "character to use to erase a word", NULL, termWerasCharp },
841 { "start", "character to use for XON", NULL, termStartCharp },
842 { "stop", "character to use for XOFF", NULL, termStopCharp },
843 { "forw1", "alternate end of line character", NULL, termForw1Charp },
844 { "forw2", "alternate end of line character", NULL, termForw2Charp },
845 { "ayt", "alternate AYT character", NULL, termAytCharp },
846 { NULL, NULL, NULL, NULL }
849 static struct setlist *
852 return (struct setlist *)
853 genget(name, (char **) Setlist, sizeof(struct setlist));
857 set_escape_char(char *s)
859 if (rlogin != _POSIX_VDISABLE) {
860 rlogin = (s && *s) ? special(s) : _POSIX_VDISABLE;
861 printf("Telnet rlogin escape character is '%s'.\n",
864 escape = (s && *s) ? special(s) : _POSIX_VDISABLE;
865 printf("Telnet escape character is '%s'.\n", control(escape));
870 setcmd(int argc, char *argv[])
874 struct togglelist *c;
876 if (argc < 2 || argc > 3) {
877 printf("Format is 'set Name Value'\n'set ?' for help.\n");
880 if ((argc == 2) && (isprefix(argv[1], "?") || isprefix(argv[1], "help"))) {
881 for (ct = Setlist; ct->name; ct++)
882 printf("%-15s %s\n", ct->name, ct->help);
885 printf("%-15s %s\n", "?", "display help information");
889 ct = getset(argv[1]);
891 c = GETTOGGLE(argv[1]);
893 fprintf(stderr, "'%s': unknown argument ('set ?' for help).\n",
896 } else if (Ambiguous((void *)c)) {
897 fprintf(stderr, "'%s': ambiguous argument ('set ?' for help).\n",
902 if ((argc == 2) || (strcmp("on", argv[2]) == 0))
904 else if (strcmp("off", argv[2]) == 0)
907 printf("Format is 'set togglename [on|off]'\n'set ?' for help.\n");
910 if (c->actionexplanation) {
911 printf("%s %s.\n", *c->variable? "Will" : "Won't",
912 c->actionexplanation);
917 } else if (argc != 3) {
918 printf("Format is 'set Name Value'\n'set ?' for help.\n");
920 } else if (Ambiguous((void *)ct)) {
921 fprintf(stderr, "'%s': ambiguous argument ('set ?' for help).\n",
924 } else if (ct->handler) {
925 (*ct->handler)(argv[2]);
926 printf("%s set to \"%s\".\n", ct->name, (char *)ct->charp);
928 if (strcmp("off", argv[2])) {
929 value = special(argv[2]);
931 value = _POSIX_VDISABLE;
933 *(ct->charp) = (cc_t)value;
934 printf("%s character is '%s'.\n", ct->name, control(*(ct->charp)));
941 unsetcmd(int argc, char *argv[])
944 struct togglelist *c;
949 "Need an argument to 'unset' command. 'unset ?' for help.\n");
952 if (isprefix(argv[1], "?") || isprefix(argv[1], "help")) {
953 for (ct = Setlist; ct->name; ct++)
954 printf("%-15s %s\n", ct->name, ct->help);
957 printf("%-15s %s\n", "?", "display help information");
969 fprintf(stderr, "'%s': unknown argument ('unset ?' for help).\n",
972 } else if (Ambiguous((void *)c)) {
973 fprintf(stderr, "'%s': ambiguous argument ('unset ?' for help).\n",
979 if (c->actionexplanation) {
980 printf("%s %s.\n", *c->variable? "Will" : "Won't",
981 c->actionexplanation);
986 } else if (Ambiguous((void *)ct)) {
987 fprintf(stderr, "'%s': ambiguous argument ('unset ?' for help).\n",
990 } else if (ct->handler) {
992 printf("%s reset to \"%s\".\n", ct->name, (char *)ct->charp);
994 *(ct->charp) = _POSIX_VDISABLE;
995 printf("%s character is '%s'.\n", ct->name, control(*(ct->charp)));
1002 * The following are the data structures and routines for the
1005 #ifdef KLUDGELINEMODE
1006 extern int kludgelinemode;
1012 send_wont(TELOPT_LINEMODE, 1);
1013 send_dont(TELOPT_SGA, 1);
1014 send_dont(TELOPT_ECHO, 1);
1022 #ifdef KLUDGELINEMODE
1024 send_dont(TELOPT_SGA, 1);
1026 send_will(TELOPT_LINEMODE, 1);
1027 send_dont(TELOPT_ECHO, 1);
1034 #ifdef KLUDGELINEMODE
1036 send_do(TELOPT_SGA, 1);
1039 send_wont(TELOPT_LINEMODE, 1);
1040 send_do(TELOPT_ECHO, 1);
1045 dolmmode(int bit, int on)
1048 extern int linemode;
1050 if (my_want_state_is_wont(TELOPT_LINEMODE)) {
1051 printf("?Need to have LINEMODE option enabled first.\n");
1052 printf("'mode ?' for help.\n");
1057 c = (linemode | bit);
1059 c = (linemode & ~bit);
1067 return dolmmode(bit, 1);
1073 return dolmmode(bit, 0);
1077 const char *name; /* command name */
1078 const char *help; /* help string */
1079 int (*handler)(int);/* routine which executes command */
1080 int needconnect; /* Do we need to be connected to execute? */
1084 static struct modelist ModeList[] = {
1085 { "character", "Disable LINEMODE option", (int (*)(int))docharmode, 1, 0 },
1086 #ifdef KLUDGELINEMODE
1087 { "", "(or disable obsolete line-by-line mode)", NULL, 0, 0 },
1089 { "line", "Enable LINEMODE option", (int (*)(int))dolinemode, 1, 0 },
1090 #ifdef KLUDGELINEMODE
1091 { "", "(or enable obsolete line-by-line mode)", NULL, 0, 0 },
1093 { "", "", NULL, 0, 0 },
1094 { "", "These require the LINEMODE option to be enabled", NULL, 0, 0 },
1095 { "isig", "Enable signal trapping", setmod, 1, MODE_TRAPSIG },
1096 { "+isig", 0, setmod, 1, MODE_TRAPSIG },
1097 { "-isig", "Disable signal trapping", clearmode, 1, MODE_TRAPSIG },
1098 { "edit", "Enable character editing", setmod, 1, MODE_EDIT },
1099 { "+edit", 0, setmod, 1, MODE_EDIT },
1100 { "-edit", "Disable character editing", clearmode, 1, MODE_EDIT },
1101 { "softtabs", "Enable tab expansion", setmod, 1, MODE_SOFT_TAB },
1102 { "+softtabs", 0, setmod, 1, MODE_SOFT_TAB },
1103 { "-softtabs", "Disable character editing", clearmode, 1, MODE_SOFT_TAB },
1104 { "litecho", "Enable literal character echo", setmod, 1, MODE_LIT_ECHO },
1105 { "+litecho", 0, setmod, 1, MODE_LIT_ECHO },
1106 { "-litecho", "Disable literal character echo", clearmode, 1, MODE_LIT_ECHO },
1107 { "help", 0, (int (*)(int))modehelp, 0, 0 },
1108 #ifdef KLUDGELINEMODE
1109 { "kludgeline", 0, (int (*)(int))dokludgemode, 1, 0 },
1111 { "", "", NULL, 0, 0 },
1112 { "?", "Print help information", (int (*)(int))modehelp, 0, 0 },
1113 { NULL, NULL, NULL, 0, 0 },
1120 struct modelist *mt;
1122 printf("format is: 'mode Mode', where 'Mode' is one of:\n\n");
1123 for (mt = ModeList; mt->name; mt++) {
1126 printf("%-15s %s\n", mt->name, mt->help);
1134 #define GETMODECMD(name) (struct modelist *) \
1135 genget(name, (char **) ModeList, sizeof(struct modelist))
1138 modecmd(int argc, char *argv[])
1140 struct modelist *mt;
1143 printf("'mode' command requires an argument\n");
1144 printf("'mode ?' for help.\n");
1145 } else if ((mt = GETMODECMD(argv[1])) == 0) {
1146 fprintf(stderr, "Unknown mode '%s' ('mode ?' for help).\n", argv[1]);
1147 } else if (Ambiguous((void *)mt)) {
1148 fprintf(stderr, "Ambiguous mode '%s' ('mode ?' for help).\n", argv[1]);
1149 } else if (mt->needconnect && !connected) {
1150 printf("?Need to be connected first.\n");
1151 printf("'mode ?' for help.\n");
1152 } else if (mt->handler) {
1153 return (*mt->handler)(mt->arg1);
1159 * The following data structures and routines implement the
1160 * "display" command.
1164 display(int argc, char *argv[])
1166 struct togglelist *tl;
1169 #define dotog(tl) if (tl->variable && tl->actionexplanation) { \
1170 if (*tl->variable) { \
1175 printf(" %s.\n", tl->actionexplanation); \
1178 #define doset(sl) if (sl->name && *sl->name != ' ') { \
1179 if (sl->handler == 0) \
1180 printf("%-15s [%s]\n", sl->name, control(*sl->charp)); \
1182 printf("%-15s \"%s\"\n", sl->name, (char *)sl->charp); \
1186 for (tl = Togglelist; tl->name; tl++) {
1190 for (sl = Setlist; sl->name; sl++) {
1196 for (i = 1; i < argc; i++) {
1197 sl = getset(argv[i]);
1198 tl = GETTOGGLE(argv[i]);
1199 if (Ambiguous((void *)sl) || Ambiguous((void *)tl)) {
1200 printf("?Ambiguous argument '%s'.\n", argv[i]);
1202 } else if (!sl && !tl) {
1203 printf("?Unknown argument '%s'.\n", argv[i]);
1215 /*@*/optionstatus();
1222 * The following are the data structures, and many of the routines,
1223 * relating to command processing.
1227 * Set the escape character.
1230 setescape(int argc, char *argv[])
1236 "Deprecated usage - please use 'set escape%s%s' in the future.\n",
1237 (argc > 2)? " ":"", (argc > 2)? argv[1]: "");
1241 printf("new escape character: ");
1242 (void) fgets(buf, sizeof(buf), stdin);
1247 (void) fflush(stdout);
1255 printf("Deprecated usage - please use 'toggle crmod' in the future.\n");
1256 printf("%s map carriage return on output.\n", crmod ? "Will" : "Won't");
1257 (void) fflush(stdout);
1267 long oldrows, oldcols, newrows, newcols, err_;
1269 err_ = (TerminalWindowSize(&oldrows, &oldcols) == 0) ? 1 : 0;
1270 (void) kill(0, SIGTSTP);
1272 * If we didn't get the window size before the SUSPEND, but we
1273 * can get them now (?), then send the NAWS to make sure that
1274 * we are set up for the right window size.
1276 if (TerminalWindowSize(&newrows, &newcols) && connected &&
1277 (err_ || ((oldrows != newrows) || (oldcols != newcols)))) {
1281 /* reget parameters in case they were changed */
1282 TerminalSaveState();
1285 printf("Suspend is not supported. Try the '!' command instead\n");
1291 shell(int argc, char *argv[] __unused)
1293 long oldrows, oldcols, newrows, newcols, err_;
1297 err_ = (TerminalWindowSize(&oldrows, &oldcols) == 0) ? 1 : 0;
1300 perror("Fork failed\n");
1306 * Fire up the shell in the child.
1308 const char *shellp, *shellname;
1310 shellp = getenv("SHELL");
1313 if ((shellname = strrchr(shellp, '/')) == 0)
1318 execl(shellp, shellname, "-c", &saveline[1], (char *)0);
1320 execl(shellp, shellname, (char *)0);
1325 (void)wait((int *)0); /* Wait for the shell to complete */
1327 if (TerminalWindowSize(&newrows, &newcols) && connected &&
1328 (err_ || ((oldrows != newrows) || (oldcols != newcols)))) {
1337 bye(int argc, char *argv[])
1339 extern int resettermname;
1342 (void) shutdown(net, 2);
1343 printf("Connection closed.\n");
1344 (void) NetClose(net);
1350 if ((argc != 2) || (strcmp(argv[1], "fromquit") != 0)) {
1351 longjmp(toplevel, 1);
1354 return 1; /* Keep lint, etc., happy */
1360 (void) call(bye, "bye", "fromquit", 0);
1367 send_do(TELOPT_LOGOUT, 1);
1380 void (*handler)(int);
1384 static void slc_help(void);
1386 struct slclist SlcList[] = {
1387 { "export", "Use local special character definitions",
1388 (void (*)(int))slc_mode_export, 0 },
1389 { "import", "Use remote special character definitions",
1390 slc_mode_import, 1 },
1391 { "check", "Verify remote special character definitions",
1392 slc_mode_import, 0 },
1393 { "help", NULL, (void (*)(int))slc_help, 0 },
1394 { "?", "Print help information", (void (*)(int))slc_help, 0 },
1395 { NULL, NULL, NULL, 0 },
1403 for (c = SlcList; c->name; c++) {
1406 printf("%-15s %s\n", c->name, c->help);
1413 static struct slclist *
1416 return (struct slclist *)
1417 genget(name, (char **) SlcList, sizeof(struct slclist));
1421 slccmd(int argc, char *argv[])
1427 "Need an argument to 'slc' command. 'slc ?' for help.\n");
1430 c = getslc(argv[1]);
1432 fprintf(stderr, "'%s': unknown argument ('slc ?' for help).\n",
1436 if (Ambiguous((void *)c)) {
1437 fprintf(stderr, "'%s': ambiguous argument ('slc ?' for help).\n",
1441 (*c->handler)(c->arg);
1447 * The ENVIRON command.
1453 void (*handler)(unsigned char *, unsigned char *);
1457 extern struct env_lst *
1458 env_define(const unsigned char *, unsigned char *);
1460 env_undefine(unsigned char *),
1461 env_export(const unsigned char *),
1462 env_unexport(const unsigned char *),
1463 env_send(unsigned char *),
1464 #if defined(OLD_ENVIRON) && defined(ENV_HACK)
1465 env_varval(unsigned char *),
1471 struct envlist EnvList[] = {
1472 { "define", "Define an environment variable",
1473 (void (*)(unsigned char *, unsigned char *))env_define, 2 },
1474 { "undefine", "Undefine an environment variable",
1475 (void (*)(unsigned char *, unsigned char *))env_undefine, 1 },
1476 { "export", "Mark an environment variable for automatic export",
1477 (void (*)(unsigned char *, unsigned char *))env_export, 1 },
1478 { "unexport", "Don't mark an environment variable for automatic export",
1479 (void (*)(unsigned char *, unsigned char *))env_unexport, 1 },
1480 { "send", "Send an environment variable", (void (*)(unsigned char *, unsigned char *))env_send, 1 },
1481 { "list", "List the current environment variables",
1482 (void (*)(unsigned char *, unsigned char *))env_list, 0 },
1483 #if defined(OLD_ENVIRON) && defined(ENV_HACK)
1484 { "varval", "Reverse VAR and VALUE (auto, right, wrong, status)",
1485 (void (*)(unsigned char *, unsigned char *))env_varval, 1 },
1487 { "help", NULL, (void (*)(unsigned char *, unsigned char *))env_help, 0 },
1488 { "?", "Print help information", (void (*)(unsigned char *, unsigned char *))env_help, 0 },
1489 { NULL, NULL, NULL, 0 },
1497 for (c = EnvList; c->name; c++) {
1500 printf("%-15s %s\n", c->name, c->help);
1507 static struct envlist *
1508 getenvcmd(char *name)
1510 return (struct envlist *)
1511 genget(name, (char **) EnvList, sizeof(struct envlist));
1515 env_cmd(int argc, char *argv[])
1521 "Need an argument to 'environ' command. 'environ ?' for help.\n");
1524 c = getenvcmd(argv[1]);
1526 fprintf(stderr, "'%s': unknown argument ('environ ?' for help).\n",
1530 if (Ambiguous((void *)c)) {
1531 fprintf(stderr, "'%s': ambiguous argument ('environ ?' for help).\n",
1535 if (c->narg + 2 != argc) {
1537 "Need %s%d argument%s to 'environ %s' command. 'environ ?' for help.\n",
1538 c->narg < argc + 2 ? "only " : "",
1539 c->narg, c->narg == 1 ? "" : "s", c->name);
1542 (*c->handler)(argv[2], argv[3]);
1547 struct env_lst *next; /* pointer to next structure */
1548 struct env_lst *prev; /* pointer to previous structure */
1549 unsigned char *var; /* pointer to variable name */
1550 unsigned char *value; /* pointer to variable value */
1551 int export; /* 1 -> export with default list of variables */
1552 int welldefined; /* A well defined variable */
1555 struct env_lst envlisthead;
1557 static struct env_lst *
1558 env_find(const unsigned char *var)
1562 for (ep = envlisthead.next; ep; ep = ep->next) {
1563 if (strcmp(ep->var, var) == 0)
1572 extern char **environ;
1576 for (epp = environ; *epp; epp++) {
1577 if ((cp = strchr(*epp, '='))) {
1579 ep = env_define((unsigned char *)*epp,
1580 (unsigned char *)cp+1);
1586 * Special case for DISPLAY variable. If it is ":0.0" or
1587 * "unix:0.0", we have to get rid of "unix" and insert our
1590 if ((ep = env_find("DISPLAY"))
1591 && ((*ep->value == ':')
1592 || (strncmp((char *)ep->value, "unix:", 5) == 0))) {
1594 char *cp2 = strchr((char *)ep->value, ':');
1596 gethostname(hbuf, 256);
1598 cp = (char *)malloc(strlen(hbuf) + strlen(cp2) + 1);
1599 sprintf((char *)cp, "%s%s", hbuf, cp2);
1601 ep->value = (unsigned char *)cp;
1604 * If USER is not defined, but LOGNAME is, then add
1605 * USER with the value from LOGNAME. By default, we
1606 * don't export the USER variable.
1608 if ((env_find("USER") == NULL) && (ep = env_find("LOGNAME"))) {
1609 env_define("USER", ep->value);
1610 env_unexport("USER");
1612 env_export("DISPLAY");
1613 env_export("PRINTER");
1617 env_define(const unsigned char *var, unsigned char *value)
1621 if ((ep = env_find(var))) {
1627 ep = (struct env_lst *)malloc(sizeof(struct env_lst));
1628 ep->next = envlisthead.next;
1629 envlisthead.next = ep;
1630 ep->prev = &envlisthead;
1632 ep->next->prev = ep;
1634 ep->welldefined = opt_welldefined(var);
1636 ep->var = strdup(var);
1637 ep->value = strdup(value);
1642 env_undefine(unsigned char *var)
1646 if ((ep = env_find(var))) {
1647 ep->prev->next = ep->next;
1649 ep->next->prev = ep->prev;
1659 env_export(const unsigned char *var)
1663 if ((ep = env_find(var)))
1668 env_unexport(const unsigned char *var)
1672 if ((ep = env_find(var)))
1677 env_send(unsigned char *var)
1681 if (my_state_is_wont(TELOPT_NEW_ENVIRON)
1683 && my_state_is_wont(TELOPT_OLD_ENVIRON)
1687 "Cannot send '%s': Telnet ENVIRON option not enabled\n",
1693 fprintf(stderr, "Cannot send '%s': variable not defined\n",
1697 env_opt_start_info();
1698 env_opt_add(ep->var);
1707 for (ep = envlisthead.next; ep; ep = ep->next) {
1708 printf("%c %-20s %s\n", ep->export ? '*' : ' ',
1709 ep->var, ep->value);
1714 env_default(int init, int welldefined)
1716 static struct env_lst *nep = NULL;
1723 while ((nep = nep->next)) {
1724 if (nep->export && (nep->welldefined == welldefined))
1732 env_getvalue(const unsigned char *var)
1736 if ((ep = env_find(var)))
1741 #if defined(OLD_ENVIRON) && defined(ENV_HACK)
1743 env_varval(unsigned char *what)
1745 extern int old_env_var, old_env_value, env_auto;
1746 int len = strlen((char *)what);
1751 if (strncasecmp((char *)what, "status", len) == 0) {
1753 printf("%s%s", "VAR and VALUE are/will be ",
1754 "determined automatically\n");
1755 if (old_env_var == OLD_ENV_VAR)
1756 printf("VAR and VALUE set to correct definitions\n");
1758 printf("VAR and VALUE definitions are reversed\n");
1759 } else if (strncasecmp((char *)what, "auto", len) == 0) {
1761 old_env_var = OLD_ENV_VALUE;
1762 old_env_value = OLD_ENV_VAR;
1763 } else if (strncasecmp((char *)what, "right", len) == 0) {
1765 old_env_var = OLD_ENV_VAR;
1766 old_env_value = OLD_ENV_VALUE;
1767 } else if (strncasecmp((char *)what, "wrong", len) == 0) {
1769 old_env_var = OLD_ENV_VALUE;
1770 old_env_value = OLD_ENV_VAR;
1773 printf("Unknown \"varval\" command. (\"auto\", \"right\", \"wrong\", \"status\")\n");
1781 * Print status about the connection.
1785 status(int argc, char *argv[])
1788 printf("Connected to %s.\n", hostname);
1789 if ((argc < 2) || strcmp(argv[1], "notmuch")) {
1790 int mode = getconnmode();
1792 if (my_want_state_is_will(TELOPT_LINEMODE)) {
1793 printf("Operating with LINEMODE option\n");
1794 printf("%s line editing\n", (mode&MODE_EDIT) ? "Local" : "No");
1795 printf("%s catching of signals\n",
1796 (mode&MODE_TRAPSIG) ? "Local" : "No");
1798 #ifdef KLUDGELINEMODE
1799 } else if (kludgelinemode && my_want_state_is_dont(TELOPT_SGA)) {
1800 printf("Operating in obsolete linemode\n");
1803 printf("Operating in single character mode\n");
1805 printf("Catching signals locally\n");
1807 printf("%s character echo\n", (mode&MODE_ECHO) ? "Local" : "Remote");
1808 if (my_want_state_is_will(TELOPT_LFLOW))
1809 printf("%s flow control\n", (mode&MODE_FLOW) ? "Local" : "No");
1812 printf("No connection.\n");
1814 printf("Escape character is '%s'.\n", control(escape));
1815 (void) fflush(stdout);
1821 * Function that gets called when SIGINFO is received.
1826 (void) call(status, "status", "notmuch", 0);
1831 sockaddr_ntop(struct sockaddr *sa)
1834 static char addrbuf[INET6_ADDRSTRLEN];
1836 switch (sa->sa_family) {
1838 addr = &((struct sockaddr_in *)sa)->sin_addr;
1841 addr = &((struct sockaddr_un *)sa)->sun_path;
1845 addr = &((struct sockaddr_in6 *)sa)->sin6_addr;
1851 inet_ntop(sa->sa_family, addr, addrbuf, sizeof(addrbuf));
1855 #if defined(IPSEC) && defined(IPSEC_POLICY_IPSEC)
1857 setpolicy(int lnet, struct addrinfo *res, char *policy)
1866 buf = ipsec_set_policy(policy, strlen(policy));
1868 printf("%s\n", ipsec_strerror());
1871 level = res->ai_family == AF_INET ? IPPROTO_IP : IPPROTO_IPV6;
1872 optname = res->ai_family == AF_INET ? IP_IPSEC_POLICY : IPV6_IPSEC_POLICY;
1873 if (setsockopt(lnet, level, optname, buf, ipsec_get_policylen(buf)) < 0){
1874 perror("setsockopt");
1885 * When an Address Family related error happend, check if retry with
1886 * another AF is possible or not.
1887 * Return 1, if retry with another af is OK. Else, return 0.
1890 switch_af(struct addrinfo **aip)
1893 struct addrinfo *ai;
1896 nextaf = (ai->ai_family == AF_INET) ? AF_INET6 : AF_INET;
1899 while (ai != NULL && ai->ai_family != nextaf);
1909 tn(int argc, char *argv[])
1914 int srcroute = 0, result;
1915 char *cmd, *hostp = 0, *portp = 0, *user = 0;
1916 char *src_addr = NULL;
1917 struct addrinfo hints, *res, *res0 = NULL, *src_res, *src_res0 = NULL;
1918 int error = 0, af_error = 0;
1921 printf("?Already connected to %s\n", hostname);
1926 (void) strcpy(line, "open ");
1928 (void) fgets(&line[strlen(line)], sizeof(line) - strlen(line), stdin);
1936 if (strcmp(*argv, "help") == 0 || isprefix(*argv, "?"))
1938 if (strcmp(*argv, "-l") == 0) {
1946 if (strcmp(*argv, "-a") == 0) {
1951 if (strcmp(*argv, "-s") == 0) {
1970 printf("usage: %s [-l user] [-a] [-s src_addr] host-name [port]\n", cmd);
1977 if (src_addr != NULL) {
1978 memset(&hints, 0, sizeof(hints));
1979 hints.ai_flags = AI_NUMERICHOST;
1980 hints.ai_family = family;
1981 hints.ai_socktype = SOCK_STREAM;
1982 error = getaddrinfo(src_addr, 0, &hints, &src_res);
1983 if (error == EAI_NODATA) {
1985 error = getaddrinfo(src_addr, 0, &hints, &src_res);
1988 fprintf(stderr, "%s: %s\n", src_addr, gai_strerror(error));
1989 if (error == EAI_SYSTEM)
1990 fprintf(stderr, "%s: %s\n", src_addr, strerror(errno));
1996 if (hostp[0] == '/') {
1997 struct sockaddr_un su;
1999 if (strlen(hostp) >= sizeof(su.sun_path)) {
2000 fprintf(stderr, "hostname too long for unix domain socket: %s",
2004 memset(&su, 0, sizeof su);
2005 su.sun_family = AF_UNIX;
2006 strncpy(su.sun_path, hostp, sizeof su.sun_path);
2007 printf("Trying %s...\n", hostp);
2008 net = socket(PF_UNIX, SOCK_STREAM, 0);
2013 if (connect(net, (struct sockaddr *)&su, sizeof su) == -1) {
2014 perror(su.sun_path);
2015 (void) NetClose(net);
2019 } else if (hostp[0] == '@' || hostp[0] == '!') {
2022 family == AF_INET6 ||
2024 (hostname = strrchr(hostp, ':')) == NULL)
2025 hostname = strrchr(hostp, '@');
2026 if (hostname == NULL) {
2036 portp = strdup("telnet");
2037 } else if (*portp == '-') {
2043 memset(&hints, 0, sizeof(hints));
2044 hints.ai_flags = AI_NUMERICHOST;
2045 hints.ai_family = family;
2046 hints.ai_socktype = SOCK_STREAM;
2047 error = getaddrinfo(hostname, portp, &hints, &res);
2049 hints.ai_flags = AI_CANONNAME;
2050 error = getaddrinfo(hostname, portp, &hints, &res);
2053 fprintf(stderr, "%s: %s\n", hostname, gai_strerror(error));
2054 if (error == EAI_SYSTEM)
2055 fprintf(stderr, "%s: %s\n", hostname, strerror(errno));
2059 if (hints.ai_flags == AI_NUMERICHOST) {
2060 /* hostname has numeric */
2064 gni_err = getnameinfo(res->ai_addr, res->ai_addr->sa_len,
2065 _hostname, sizeof(_hostname) - 1, NULL, 0,
2068 (void) strncpy(_hostname, hostp, sizeof(_hostname) - 1);
2069 _hostname[sizeof(_hostname)-1] = '\0';
2070 hostname = _hostname;
2072 /* hostname has FQDN */
2074 (void) strncpy(_hostname, hostname, sizeof(_hostname) - 1);
2075 else if (res->ai_canonname != NULL)
2076 strcpy(_hostname, res->ai_canonname);
2078 (void) strncpy(_hostname, hostp, sizeof(_hostname) - 1);
2079 _hostname[sizeof(_hostname)-1] = '\0';
2080 hostname = _hostname;
2086 if (srcroute != 0) {
2087 static char hostbuf[BUFSIZ];
2089 if (af_error == 0) { /* save intermediate hostnames for retry */
2090 strncpy(hostbuf, hostp, BUFSIZ - 1);
2091 hostbuf[BUFSIZ - 1] = '\0';
2095 result = sourceroute(res, hostp, &srp, &srlen, &proto, &opt);
2098 if (family == AF_UNSPEC && af_error == 0 &&
2099 switch_af(&res) == 1) {
2106 } else if (result == -1) {
2107 printf("Bad source route option: %s\n", hostp);
2113 printf("Trying %s...\n", sockaddr_ntop(res->ai_addr));
2114 net = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
2118 if (family == AF_UNSPEC && af_error == 0 &&
2119 switch_af(&res) == 1) {
2124 perror("telnet: socket");
2127 if (srp && setsockopt(net, proto, opt, (char *)srp, srlen) < 0)
2128 perror("setsockopt (source route)");
2129 #if defined(IPPROTO_IP) && defined(IP_TOS)
2130 if (res->ai_family == PF_INET) {
2131 # if defined(HAS_GETTOS)
2133 if (tos < 0 && (tp = gettosbyname("telnet", "tcp")))
2137 tos = IPTOS_LOWDELAY;
2139 && (setsockopt(net, IPPROTO_IP, IP_TOS,
2140 (char *)&tos, sizeof(int)) < 0)
2141 && (errno != ENOPROTOOPT))
2142 perror("telnet: setsockopt (IP_TOS) (ignored)");
2144 #endif /* defined(IPPROTO_IP) && defined(IP_TOS) */
2146 if (debug && SetSockOpt(net, SOL_SOCKET, SO_DEBUG, 1) < 0) {
2147 perror("setsockopt (SO_DEBUG)");
2150 if (src_addr != NULL) {
2151 for (src_res = src_res0; src_res != 0; src_res = src_res->ai_next)
2152 if (src_res->ai_family == res->ai_family)
2154 if (src_res == NULL)
2156 if (bind(net, src_res->ai_addr, src_res->ai_addrlen) == -1) {
2158 if (family == AF_UNSPEC && af_error == 0 &&
2159 switch_af(&res) == 1) {
2161 (void) NetClose(net);
2166 (void) NetClose(net);
2170 #if defined(IPSEC) && defined(IPSEC_POLICY_IPSEC)
2171 if (setpolicy(net, res, ipsec_policy_in) < 0) {
2172 (void) NetClose(net);
2175 if (setpolicy(net, res, ipsec_policy_out) < 0) {
2176 (void) NetClose(net);
2181 if (connect(net, res->ai_addr, res->ai_addrlen) < 0) {
2182 struct addrinfo *next;
2184 next = res->ai_next;
2185 /* If already an af failed, only try same af. */
2187 while (next != NULL && next->ai_family != res->ai_family)
2188 next = next->ai_next;
2189 warn("connect to address %s", sockaddr_ntop(res->ai_addr));
2192 (void) NetClose(net);
2195 warnx("Unable to connect to remote host");
2196 (void) NetClose(net);
2200 } while (connected == 0);
2202 if (src_res0 != NULL)
2203 freeaddrinfo(src_res0);
2204 cmdrc(hostp, hostname);
2206 if (autologin && user == NULL) {
2209 user = getenv("USER");
2211 ((pw = getpwnam(user)) && pw->pw_uid != getuid())) {
2212 if ((pw = getpwuid(getuid())))
2219 env_define("USER", user);
2222 (void) call(status, "status", "notmuch", 0);
2223 if (setjmp(peerdied) == 0)
2225 (void) NetClose(net);
2226 ExitString("Connection closed by foreign host.\n",1);
2231 if (src_res0 != NULL)
2232 freeaddrinfo(src_res0);
2236 #define HELPINDENT (sizeof ("connect"))
2239 openhelp[] = "connect to a site",
2240 closehelp[] = "close current connection",
2241 logouthelp[] = "forcibly logout remote user and close the connection",
2242 quithelp[] = "exit telnet",
2243 statushelp[] = "print status information",
2244 helphelp[] = "print help information",
2245 sendhelp[] = "transmit special characters ('send ?' for more)",
2246 sethelp[] = "set operating parameters ('set ?' for more)",
2247 unsethelp[] = "unset operating parameters ('unset ?' for more)",
2248 togglestring[] ="toggle operating parameters ('toggle ?' for more)",
2249 slchelp[] = "change state of special charaters ('slc ?' for more)",
2250 displayhelp[] = "display operating parameters",
2251 zhelp[] = "suspend telnet",
2253 opiehelp[] = "compute response to OPIE challenge",
2255 shellhelp[] = "invoke a subshell",
2256 envhelp[] = "change environment variables ('environ ?' for more)",
2257 modestring[] = "try to enter line or character mode ('mode ?' for more)";
2259 static Command cmdtab[] = {
2260 { "close", closehelp, bye, 1 },
2261 { "logout", logouthelp, (int (*)(int, char **))logout, 1 },
2262 { "display", displayhelp, display, 0 },
2263 { "mode", modestring, modecmd, 0 },
2264 { "telnet", openhelp, tn, 0 },
2265 { "open", openhelp, tn, 0 },
2266 { "quit", quithelp, (int (*)(int, char **))quit, 0 },
2267 { "send", sendhelp, sendcmd, 0 },
2268 { "set", sethelp, setcmd, 0 },
2269 { "unset", unsethelp, unsetcmd, 0 },
2270 { "status", statushelp, status, 0 },
2271 { "toggle", togglestring, toggle, 0 },
2272 { "slc", slchelp, slccmd, 0 },
2273 { "z", zhelp, (int (*)(int, char **))suspend, 0 },
2274 { "!", shellhelp, shell, 1 },
2275 { "environ", envhelp, env_cmd, 0 },
2276 { "?", helphelp, help, 0 },
2278 { "opie", opiehelp, opie_calc, 0 },
2280 { NULL, NULL, NULL, 0 }
2283 static char crmodhelp[] = "deprecated command -- use 'toggle crmod' instead";
2284 static char escapehelp[] = "deprecated command -- use 'set escape' instead";
2286 static Command cmdtab2[] = {
2287 { "help", 0, help, 0 },
2288 { "escape", escapehelp, setescape, 0 },
2289 { "crmod", crmodhelp, (int (*)(int, char **))togcrmod, 0 },
2290 { NULL, NULL, NULL, 0 }
2295 * Call routine with argc, argv set from args (terminated by 0).
2299 call(intrtn_t routine, ...)
2305 va_start(ap, routine);
2306 while ((args[argno++] = va_arg(ap, char *)) != 0);
2308 return (*routine)(argno-1, args);
2317 if ((cm = (Command *) genget(name, (char **) cmdtab, sizeof(Command))))
2319 return (Command *) genget(name, (char **) cmdtab2, sizeof(Command));
2323 command(int top, const char *tbuf, int cnt)
2331 (void) signal(SIGINT, SIG_DFL);
2332 (void) signal(SIGQUIT, SIG_DFL);
2335 if (rlogin == _POSIX_VDISABLE)
2336 printf("%s> ", prompt);
2340 while (cnt > 0 && (*cp++ = *tbuf++) != '\n')
2343 if (cp == line || *--cp != '\n' || cp == line)
2346 if (rlogin == _POSIX_VDISABLE)
2347 printf("%s\n", line);
2350 if (rlogin != _POSIX_VDISABLE)
2351 printf("%s> ", prompt);
2352 if (fgets(line, sizeof(line), stdin) == NULL) {
2353 if (feof(stdin) || ferror(stdin)) {
2363 if (margv[0] == 0) {
2366 c = getcmd(margv[0]);
2367 if (Ambiguous((void *)c)) {
2368 printf("?Ambiguous command\n");
2372 printf("?Invalid command\n");
2375 if (c->needconnect && !connected) {
2376 printf("?Need to be connected first.\n");
2379 if ((*c->handler)(margc, margv)) {
2385 longjmp(toplevel, 1);
2396 help(int argc, char *argv[])
2401 printf("Commands may be abbreviated. Commands are:\n\n");
2402 for (c = cmdtab; c->name; c++)
2404 printf("%-*s\t%s\n", (int)HELPINDENT, c->name,
2409 else while (--argc > 0) {
2413 if (Ambiguous((void *)c))
2414 printf("?Ambiguous help command %s\n", arg);
2415 else if (c == (Command *)0)
2416 printf("?Invalid help command %s\n", arg);
2418 printf("%s\n", c->help);
2423 static char *rcname = 0;
2424 static char rcbuf[128];
2427 cmdrc(char *m1, char *m2)
2432 int l1 = strlen(m1);
2433 int l2 = strlen(m2);
2434 char m1save[MAXHOSTNAMELEN];
2439 strlcpy(m1save, m1, sizeof(m1save));
2443 rcname = getenv("HOME");
2444 if (rcname && (strlen(rcname) + 10) < sizeof(rcbuf))
2445 strcpy(rcbuf, rcname);
2448 strcat(rcbuf, "/.telnetrc");
2452 if ((rcfile = fopen(rcname, "r")) == 0) {
2457 if (fgets(line, sizeof(line), rcfile) == NULL)
2464 if (!isspace(line[0]))
2467 if (gotmachine == 0) {
2468 if (isspace(line[0]))
2470 if (strncasecmp(line, m1, l1) == 0)
2471 strncpy(line, &line[l1], sizeof(line) - l1);
2472 else if (strncasecmp(line, m2, l2) == 0)
2473 strncpy(line, &line[l2], sizeof(line) - l2);
2474 else if (strncasecmp(line, "DEFAULT", 7) == 0)
2475 strncpy(line, &line[7], sizeof(line) - 7);
2478 if (line[0] != ' ' && line[0] != '\t' && line[0] != '\n')
2485 c = getcmd(margv[0]);
2486 if (Ambiguous((void *)c)) {
2487 printf("?Ambiguous command: %s\n", margv[0]);
2491 printf("?Invalid command: %s\n", margv[0]);
2495 * This should never happen...
2497 if (c->needconnect && !connected) {
2498 printf("?Need to be connected first for %s.\n", margv[0]);
2501 (*c->handler)(margc, margv);
2507 * Source route is handed in as
2508 * [!]@hop1@hop2...[@|:]dst
2509 * If the leading ! is present, it is a
2510 * strict source route, otherwise it is
2511 * assmed to be a loose source route.
2513 * We fill in the source route option as
2514 * hop1,hop2,hop3...dest
2515 * and return a pointer to hop1, which will
2516 * be the address to connect() to.
2520 * res: ponter to addrinfo structure which contains sockaddr to
2521 * the host to connect to.
2523 * arg: pointer to route list to decipher
2525 * cpp: If *cpp is not equal to NULL, this is a
2526 * pointer to a pointer to a character array
2527 * that should be filled in with the option.
2529 * lenp: pointer to an integer that contains the
2530 * length of *cpp if *cpp != NULL.
2532 * protop: pointer to an integer that should be filled in with
2533 * appropriate protocol for setsockopt, as socket
2536 * optp: pointer to an integer that should be filled in with
2537 * appropriate option for setsockopt, as socket protocol
2542 * If the return value is 1, then all operations are
2543 * successful. If the
2544 * return value is -1, there was a syntax error in the
2545 * option, either unknown characters, or too many hosts.
2546 * If the return value is 0, one of the hostnames in the
2547 * path is unknown, and *cpp is set to point to the bad
2550 * *cpp: If *cpp was equal to NULL, it will be filled
2551 * in with a pointer to our static area that has
2552 * the option filled in. This will be 32bit aligned.
2554 * *lenp: This will be filled in with how long the option
2555 * pointed to by *cpp is.
2557 * *protop: This will be filled in with appropriate protocol for
2558 * setsockopt, as socket protocol family.
2560 * *optp: This will be filled in with appropriate option for
2561 * setsockopt, as socket protocol family.
2564 sourceroute(struct addrinfo *ai, char *arg, char **cpp, int *lenp, int *protop, int *optp)
2566 static char buf[1024 + ALIGNBYTES]; /*XXX*/
2567 char *cp, *cp2, *lsrp, *ep;
2568 struct sockaddr_in *_sin;
2570 struct sockaddr_in6 *sin6;
2571 struct cmsghdr *cmsg;
2573 struct addrinfo hints, *res;
2578 * Verify the arguments, and make sure we have
2579 * at least 7 bytes for the option.
2581 if (cpp == NULL || lenp == NULL)
2584 switch (res->ai_family) {
2591 if (*lenp < (int)CMSG_SPACE(sizeof(struct ip6_rthdr) +
2592 sizeof(struct in6_addr)))
2599 * Decide whether we have a buffer passed to us,
2600 * or if we need to use our own static buffer.
2606 *cpp = lsrp = (char *)ALIGN(buf);
2613 if (ai->ai_family == AF_INET6) {
2614 cmsg = inet6_rthdr_init(*cpp, IPV6_RTHDR_TYPE_0);
2617 *protop = IPPROTO_IPV6;
2618 *optp = IPV6_PKTOPTIONS;
2623 * Next, decide whether we have a loose source
2624 * route or a strict source route, and fill in
2625 * the begining of the option.
2629 *lsrp++ = IPOPT_SSRR;
2631 *lsrp++ = IPOPT_LSRR;
2636 lsrp++; /* skip over length, we'll fill it in later */
2638 *protop = IPPROTO_IP;
2643 memset(&hints, 0, sizeof(hints));
2644 hints.ai_family = ai->ai_family;
2645 hints.ai_socktype = SOCK_STREAM;
2649 ai->ai_family != AF_INET6 &&
2653 else for (cp2 = cp; (c = *cp2); cp2++) {
2658 } else if (c == '@') {
2662 ai->ai_family != AF_INET6 &&
2673 hints.ai_flags = AI_NUMERICHOST;
2674 error = getaddrinfo(cp, NULL, &hints, &res);
2675 if (error == EAI_NODATA) {
2677 error = getaddrinfo(cp, NULL, &hints, &res);
2680 fprintf(stderr, "%s: %s\n", cp, gai_strerror(error));
2681 if (error == EAI_SYSTEM)
2682 fprintf(stderr, "%s: %s\n", cp,
2688 if (res->ai_family == AF_INET6) {
2689 sin6 = (struct sockaddr_in6 *)res->ai_addr;
2690 inet6_rthdr_add(cmsg, &sin6->sin6_addr,
2695 _sin = (struct sockaddr_in *)res->ai_addr;
2696 memcpy(lsrp, (char *)&_sin->sin_addr, 4);
2704 * Check to make sure there is space for next address
2707 if (res->ai_family == AF_INET6) {
2708 if (((char *)CMSG_DATA(cmsg) +
2709 sizeof(struct ip6_rthdr) +
2710 ((inet6_rthdr_segments(cmsg) + 1) *
2711 sizeof(struct in6_addr))) > ep)
2720 if (res->ai_family == AF_INET6) {
2721 inet6_rthdr_lasthop(cmsg, IPV6_RTHDR_LOOSE);
2722 *lenp = cmsg->cmsg_len;
2726 if ((*(*cpp+IPOPT_OLEN) = lsrp - *cpp) <= 7) {
2731 *lsrp++ = IPOPT_NOP; /* 32 bit word align it */
2732 *lenp = lsrp - *cpp;