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
36 static const char sccsid[] = "@(#)commands.c 8.4 (Berkeley) 5/30/95";
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
42 #include <sys/param.h>
45 #include <sys/socket.h>
46 #include <netinet/in.h>
60 #include <arpa/telnet.h>
61 #include <arpa/inet.h>
73 #include <libtelnet/auth.h>
76 #include <libtelnet/encrypt.h>
79 #include <netinet/in_systm.h>
80 #include <netinet/ip.h>
81 #include <netinet/ip6.h>
83 #ifndef MAXHOSTNAMELEN
84 #define MAXHOSTNAMELEN 256
87 typedef int (*intrtn_t)(int, char **);
90 extern int auth_togdebug(int);
93 extern int EncryptAutoEnc(int);
94 extern int EncryptAutoDec(int);
95 extern int EncryptDebug(int);
96 extern int EncryptVerbose(int);
97 #endif /* ENCRYPTION */
98 #if defined(IPPROTO_IP) && defined(IP_TOS)
100 #endif /* defined(IPPROTO_IP) && defined(IP_TOS) */
103 static char _hostname[MAXHOSTNAMELEN];
105 static int help(int, char **);
106 static int call(intrtn_t, ...);
107 static void cmdrc(char *, char *);
109 static int switch_af(struct addrinfo **);
111 static int togglehelp(void);
112 static int send_tncmd(void (*)(int, int), const char *, char *);
113 static int setmod(int);
114 static int clearmode(int);
115 static int modehelp(void);
116 static int sourceroute(struct addrinfo *, char *, unsigned char **, int *, int *, int *);
119 const char *name; /* command name */
120 const char *help; /* help string (NULL for no help) */
121 int (*handler)(int, char **); /* routine which executes command */
122 int needconnect; /* Do we need to be connected to execute? */
125 static char line[256];
126 static char saveline[256];
128 static char *margv[20];
131 #include <sys/wait.h>
132 #define PATH_OPIEKEY "/usr/bin/opiekey"
134 opie_calc(int argc, char *argv[])
139 printf("%s sequence challenge\n", argv[0]);
145 execv(PATH_OPIEKEY, argv);
151 (void) wait(&status);
152 if (WIFEXITED(status))
153 return (WEXITSTATUS(status));
167 if (*cp == '!') { /* Special case shell escape */
168 strcpy(saveline, line); /* save for shell command */
169 *argp++ = strdup("!"); /* No room in string to get this */
181 for (cp2 = cp; c != '\0'; c = *++cp) {
189 if ((c = *++cp) == '\0')
191 } else if (c == '"') {
194 } else if (c == '\'') {
197 } else if (isspace(c))
211 * Make a character string into a number.
213 * Todo: 1. Could take random integers (12, 0x12, 012, 0b1).
226 c = b | 0x40; /* DEL */
239 * Construct a control character sequence
240 * for a special character.
247 * The only way I could get the Sun 3.5 compiler
249 * if ((unsigned int)c >= 0x80)
250 * was to assign "c" to an unsigned int variable...
253 unsigned int uic = (unsigned int)c;
257 if (c == (cc_t)_POSIX_VDISABLE) {
262 buf[1] = ((c>>6)&07) + '0';
263 buf[2] = ((c>>3)&07) + '0';
264 buf[3] = (c&07) + '0';
266 } else if (uic >= 0x20) {
278 * The following are data structures and routines for
279 * the "send" command.
284 const char *name; /* How user refers to it (case independent) */
285 const char *help; /* Help information (0 ==> no help) */
286 int needconnect; /* Need to be connected */
287 int narg; /* Number of arguments */
288 int (*handler)(char *, ...); /* Routine to perform (for special ops) */
289 int nbyte; /* Number of bytes to send this command */
290 int what; /* Character to be sent (<0 ==> special) */
298 send_dontcmd(char *),
299 send_willcmd(char *),
300 send_wontcmd(char *);
302 static struct sendlist Sendlist[] = {
303 { "ao", "Send Telnet Abort output", 1, 0, NULL, 2, AO },
304 { "ayt", "Send Telnet 'Are You There'", 1, 0, NULL, 2, AYT },
305 { "brk", "Send Telnet Break", 1, 0, NULL, 2, BREAK },
306 { "break", NULL, 1, 0, NULL, 2, BREAK },
307 { "ec", "Send Telnet Erase Character", 1, 0, NULL, 2, EC },
308 { "el", "Send Telnet Erase Line", 1, 0, NULL, 2, EL },
309 { "escape", "Send current escape character",1, 0, (int (*)(char *, ...))send_esc, 1, 0 },
310 { "ga", "Send Telnet 'Go Ahead' sequence", 1, 0, NULL, 2, GA },
311 { "ip", "Send Telnet Interrupt Process",1, 0, NULL, 2, IP },
312 { "intp", NULL, 1, 0, NULL, 2, IP },
313 { "interrupt", NULL, 1, 0, NULL, 2, IP },
314 { "intr", NULL, 1, 0, NULL, 2, IP },
315 { "nop", "Send Telnet 'No operation'", 1, 0, NULL, 2, NOP },
316 { "eor", "Send Telnet 'End of Record'", 1, 0, NULL, 2, EOR },
317 { "abort", "Send Telnet 'Abort Process'", 1, 0, NULL, 2, ABORT },
318 { "susp", "Send Telnet 'Suspend Process'",1, 0, NULL, 2, SUSP },
319 { "eof", "Send Telnet End of File Character", 1, 0, NULL, 2, xEOF },
320 { "synch", "Perform Telnet 'Synch operation'", 1, 0, (int (*)(char *, ...))dosynch, 2, 0 },
321 { "getstatus", "Send request for STATUS", 1, 0, (int (*)(char *, ...))get_status, 6, 0 },
322 { "?", "Display send options", 0, 0, (int (*)(char *, ...))send_help, 0, 0 },
323 { "help", NULL, 0, 0, (int (*)(char *, ...))send_help, 0, 0 },
324 { "do", NULL, 0, 1, (int (*)(char *, ...))send_docmd, 3, 0 },
325 { "dont", NULL, 0, 1, (int (*)(char *, ...))send_dontcmd, 3, 0 },
326 { "will", NULL, 0, 1, (int (*)(char *, ...))send_willcmd, 3, 0 },
327 { "wont", NULL, 0, 1, (int (*)(char *, ...))send_wontcmd, 3, 0 },
328 { NULL, NULL, 0, 0, NULL, 0, 0 }
331 #define GETSEND(name) ((struct sendlist *) genget(name, (char **) Sendlist, \
332 sizeof(struct sendlist)))
335 sendcmd(int argc, char *argv[])
337 int count; /* how many bytes we are going to need to send */
339 struct sendlist *s; /* pointer to current command */
344 printf("need at least one argument for 'send' command\n");
345 printf("'send ?' for help\n");
349 * First, validate all the send arguments.
350 * In addition, we see how much space we are going to need, and
351 * whether or not we will be doing a "SYNCH" operation (which
352 * flushes the network queue).
355 for (i = 1; i < argc; i++) {
356 s = GETSEND(argv[i]);
358 printf("Unknown send argument '%s'\n'send ?' for help.\n",
361 } else if (Ambiguous((void *)s)) {
362 printf("Ambiguous send argument '%s'\n'send ?' for help.\n",
366 if (i + s->narg >= argc) {
368 "Need %d argument%s to 'send %s' command. 'send %s ?' for help.\n",
369 s->narg, s->narg == 1 ? "" : "s", s->name, s->name);
373 if ((void *)s->handler == (void *)send_help) {
379 needconnect += s->needconnect;
381 if (!connected && needconnect) {
382 printf("?Need to be connected first.\n");
383 printf("'send ?' for help\n");
386 /* Now, do we have enough room? */
387 if (NETROOM() < count) {
388 printf("There is not enough room in the buffer TO the network\n");
389 printf("to process your request. Nothing will be done.\n");
390 printf("('send synch' will throw away most data in the network\n");
391 printf("buffer, if this might help.)\n");
394 /* OK, they are all OK, now go through again and actually send */
396 for (i = 1; i < argc; i++) {
397 if ((s = GETSEND(argv[i])) == 0) {
398 fprintf(stderr, "Telnet 'send' error - argument disappeared!\n");
404 success += (*s->handler)((s->narg > 0) ? argv[i+1] : 0,
405 (s->narg > 1) ? argv[i+2] : 0);
408 NET2ADD(IAC, s->what);
409 printoption("SENT", IAC, s->what);
412 return (count == success);
423 send_docmd(char *name)
425 return(send_tncmd(send_do, "do", name));
432 return(send_tncmd(send_dont, "dont", name));
436 send_willcmd(char *name)
438 return(send_tncmd(send_will, "will", name));
442 send_wontcmd(char *name)
444 return(send_tncmd(send_wont, "wont", name));
448 send_tncmd(void (*func)(int, int), const char *cmd, char *name)
451 extern char *telopts[];
454 if (isprefix(name, "help") || isprefix(name, "?")) {
457 printf("usage: send %s <value|option>\n", cmd);
458 printf("\"value\" must be from 0 to 255\n");
459 printf("Valid options are:\n\t");
462 for (cpp = telopts; *cpp; cpp++) {
463 len = strlen(*cpp) + 3;
464 if (col + len > 65) {
468 printf(" \"%s\"", *cpp);
474 cpp = (char **)genget(name, telopts, sizeof(char *));
475 if (Ambiguous(cpp)) {
476 fprintf(stderr,"'%s': ambiguous argument ('send %s ?' for help).\n",
485 while (*cp >= '0' && *cp <= '9') {
491 fprintf(stderr, "'%s': unknown argument ('send %s ?' for help).\n",
494 } else if (val < 0 || val > 255) {
495 fprintf(stderr, "'%s': bad value ('send %s ?' for help).\n",
501 printf("?Need to be connected first.\n");
511 struct sendlist *s; /* pointer to current command */
512 for (s = Sendlist; s->name; s++) {
514 printf("%-15s %s\n", s->name, s->help);
520 * The following are the routines and data structures referred
521 * to by the arguments to the "toggle" command.
536 (SetSockOpt(net, SOL_SOCKET, SO_DEBUG, telnet_debug)) < 0) {
537 perror("setsockopt (SO_DEBUG)");
541 if (net > 0 && SetSockOpt(net, SOL_SOCKET, SO_DEBUG, 1) < 0)
542 perror("setsockopt (SO_DEBUG)");
544 printf("Cannot turn off socket debugging\n");
554 printf("Will send carriage returns as telnet <CR><LF>.\n");
556 printf("Will send carriage returns as telnet <CR><NUL>.\n");
566 donebinarytoggle = 1;
571 if (my_want_state_is_will(TELOPT_BINARY) &&
572 my_want_state_is_do(TELOPT_BINARY)) {
574 } else if (my_want_state_is_wont(TELOPT_BINARY) &&
575 my_want_state_is_dont(TELOPT_BINARY)) {
578 val = binmode ? 0 : 1;
582 if (my_want_state_is_will(TELOPT_BINARY) &&
583 my_want_state_is_do(TELOPT_BINARY)) {
584 printf("Already operating in binary mode with remote host.\n");
586 printf("Negotiating binary mode with remote host.\n");
590 if (my_want_state_is_wont(TELOPT_BINARY) &&
591 my_want_state_is_dont(TELOPT_BINARY)) {
592 printf("Already in network ascii mode with remote host.\n");
594 printf("Negotiating network ascii mode with remote host.\n");
604 donebinarytoggle = 1;
607 val = my_want_state_is_do(TELOPT_BINARY) ? 0 : 1;
610 if (my_want_state_is_do(TELOPT_BINARY)) {
611 printf("Already receiving in binary mode.\n");
613 printf("Negotiating binary mode on input.\n");
617 if (my_want_state_is_dont(TELOPT_BINARY)) {
618 printf("Already receiving in network ascii mode.\n");
620 printf("Negotiating network ascii mode on input.\n");
630 donebinarytoggle = 1;
633 val = my_want_state_is_will(TELOPT_BINARY) ? 0 : 1;
636 if (my_want_state_is_will(TELOPT_BINARY)) {
637 printf("Already transmitting in binary mode.\n");
639 printf("Negotiating binary mode on output.\n");
643 if (my_want_state_is_wont(TELOPT_BINARY)) {
644 printf("Already transmitting in network ascii mode.\n");
646 printf("Negotiating network ascii mode on output.\n");
654 const char *name; /* name of toggle */
655 const char *help; /* help message */
656 int (*handler)(int); /* routine to do actual setting */
658 const char *actionexplanation;
661 static struct togglelist Togglelist[] = {
663 "flushing of output when sending interrupt characters",
666 "flush output when sending interrupt characters" },
668 "automatic sending of interrupt characters in urgent mode",
671 "send interrupt characters in urgent mode" },
672 #ifdef AUTHENTICATION
674 "automatic sending of login and/or authentication info",
677 "send login name and/or authentication information" },
679 "Toggle authentication debugging",
682 "print authentication debugging information" },
686 "automatic encryption of data stream",
689 "automatically encrypt output" },
691 "automatic decryption of data stream",
694 "automatically decrypt input" },
696 "Toggle verbose encryption output",
699 "print verbose encryption output" },
701 "Toggle encryption debugging",
704 "print encryption debugging information" },
705 #endif /* ENCRYPTION */
707 "don't read ~/.telnetrc file",
710 "skip reading of ~/.telnetrc file" },
712 "sending and receiving of binary data",
717 "receiving of binary data",
722 "sending of binary data",
727 "sending carriage returns as telnet <CR><LF>",
728 (int (*)(int))togcrlf,
732 "mapping of received carriage returns",
735 "map carriage return on output" },
737 "local recognition of certain control characters",
738 (int (*)(int))lclchars,
740 "recognize certain control characters" },
741 { " ", "", NULL, NULL, NULL }, /* empty line */
744 (int (*)(int))togdebug,
746 "turn on socket level debugging" },
748 "printing of hexadecimal network data (debugging)",
751 "print hexadecimal representation of network traffic" },
753 "output of \"netdata\" to user readable format (debugging)",
756 "print user readable output for \"netdata\"" },
758 "viewing of options processing (debugging)",
761 "show option processing" },
763 "(debugging) toggle printing of hexadecimal terminal data",
766 "print hexadecimal representation of terminal traffic" },
769 (int (*)(int))togglehelp,
772 { NULL, NULL, NULL, NULL, NULL },
775 (int (*)(int))togglehelp,
778 { NULL, NULL, NULL, NULL, NULL }
784 struct togglelist *c;
786 for (c = Togglelist; c->name; c++) {
789 printf("%-15s toggle %s\n", c->name, c->help);
795 printf("%-15s %s\n", "?", "display help information");
800 settogglehelp(int set)
802 struct togglelist *c;
804 for (c = Togglelist; c->name; c++) {
807 printf("%-15s %s %s\n", c->name, set ? "enable" : "disable",
815 #define GETTOGGLE(name) (struct togglelist *) \
816 genget(name, (char **) Togglelist, sizeof(struct togglelist))
819 toggle(int argc, char *argv[])
823 struct togglelist *c;
827 "Need an argument to 'toggle' command. 'toggle ?' for help.\n");
835 if (Ambiguous((void *)c)) {
836 fprintf(stderr, "'%s': ambiguous argument ('toggle ?' for help).\n",
840 fprintf(stderr, "'%s': unknown argument ('toggle ?' for help).\n",
845 *c->variable = !*c->variable; /* invert it */
846 if (c->actionexplanation) {
847 printf("%s %s.\n", *c->variable? "Will" : "Won't",
848 c->actionexplanation);
852 retval &= (*c->handler)(-1);
860 * The following perform the "set" command.
864 struct termio new_tc = { 0, 0, 0, 0, {}, 0, 0 };
868 const char *name; /* name */
869 const char *help; /* help information */
870 void (*handler)(char *);
871 cc_t *charp; /* where it is located at */
874 static struct setlist Setlist[] = {
875 #ifdef KLUDGELINEMODE
876 { "echo", "character to toggle local echoing on/off", NULL, &echoc },
878 { "escape", "character to escape back to telnet command mode", NULL, &escape },
879 { "rlogin", "rlogin escape character", 0, &rlogin },
880 { "tracefile", "file to write trace information to", SetNetTrace, (cc_t *)NetTraceFile},
881 { " ", "", NULL, NULL },
882 { " ", "The following need 'localchars' to be toggled true", NULL, NULL },
883 { "flushoutput", "character to cause an Abort Output", NULL, termFlushCharp },
884 { "interrupt", "character to cause an Interrupt Process", NULL, termIntCharp },
885 { "quit", "character to cause an Abort process", NULL, termQuitCharp },
886 { "eof", "character to cause an EOF ", NULL, termEofCharp },
887 { " ", "", NULL, NULL },
888 { " ", "The following are for local editing in linemode", NULL, NULL },
889 { "erase", "character to use to erase a character", NULL, termEraseCharp },
890 { "kill", "character to use to erase a line", NULL, termKillCharp },
891 { "lnext", "character to use for literal next", NULL, termLiteralNextCharp },
892 { "susp", "character to cause a Suspend Process", NULL, termSuspCharp },
893 { "reprint", "character to use for line reprint", NULL, termRprntCharp },
894 { "worderase", "character to use to erase a word", NULL, termWerasCharp },
895 { "start", "character to use for XON", NULL, termStartCharp },
896 { "stop", "character to use for XOFF", NULL, termStopCharp },
897 { "forw1", "alternate end of line character", NULL, termForw1Charp },
898 { "forw2", "alternate end of line character", NULL, termForw2Charp },
899 { "ayt", "alternate AYT character", NULL, termAytCharp },
900 { "baudrate", "set remote baud rate", DoBaudRate, ComPortBaudRate },
901 { NULL, NULL, NULL, NULL }
904 static struct setlist *
907 return (struct setlist *)
908 genget(name, (char **) Setlist, sizeof(struct setlist));
912 set_escape_char(char *s)
914 if (rlogin != _POSIX_VDISABLE) {
915 rlogin = (s && *s) ? special(s) : _POSIX_VDISABLE;
916 printf("Telnet rlogin escape character is '%s'.\n",
919 escape = (s && *s) ? special(s) : _POSIX_VDISABLE;
920 printf("Telnet escape character is '%s'.\n", control(escape));
925 setcmd(int argc, char *argv[])
929 struct togglelist *c;
931 if (argc < 2 || argc > 3) {
932 printf("Format is 'set Name Value'\n'set ?' for help.\n");
935 if ((argc == 2) && (isprefix(argv[1], "?") || isprefix(argv[1], "help"))) {
936 for (ct = Setlist; ct->name; ct++)
937 printf("%-15s %s\n", ct->name, ct->help);
940 printf("%-15s %s\n", "?", "display help information");
944 ct = getset(argv[1]);
946 c = GETTOGGLE(argv[1]);
948 fprintf(stderr, "'%s': unknown argument ('set ?' for help).\n",
951 } else if (Ambiguous((void *)c)) {
952 fprintf(stderr, "'%s': ambiguous argument ('set ?' for help).\n",
957 if ((argc == 2) || (strcmp("on", argv[2]) == 0))
959 else if (strcmp("off", argv[2]) == 0)
962 printf("Format is 'set togglename [on|off]'\n'set ?' for help.\n");
965 if (c->actionexplanation) {
966 printf("%s %s.\n", *c->variable? "Will" : "Won't",
967 c->actionexplanation);
972 } else if (argc != 3) {
973 printf("Format is 'set Name Value'\n'set ?' for help.\n");
975 } else if (Ambiguous((void *)ct)) {
976 fprintf(stderr, "'%s': ambiguous argument ('set ?' for help).\n",
979 } else if (ct->handler) {
980 (*ct->handler)(argv[2]);
981 printf("%s set to \"%s\".\n", ct->name, (char *)ct->charp);
983 if (strcmp("off", argv[2])) {
984 value = special(argv[2]);
986 value = _POSIX_VDISABLE;
988 *(ct->charp) = (cc_t)value;
989 printf("%s character is '%s'.\n", ct->name, control(*(ct->charp)));
996 unsetcmd(int argc, char *argv[])
999 struct togglelist *c;
1004 "Need an argument to 'unset' command. 'unset ?' for help.\n");
1007 if (isprefix(argv[1], "?") || isprefix(argv[1], "help")) {
1008 for (ct = Setlist; ct->name; ct++)
1009 printf("%-15s %s\n", ct->name, ct->help);
1012 printf("%-15s %s\n", "?", "display help information");
1022 c = GETTOGGLE(name);
1024 fprintf(stderr, "'%s': unknown argument ('unset ?' for help).\n",
1027 } else if (Ambiguous((void *)c)) {
1028 fprintf(stderr, "'%s': ambiguous argument ('unset ?' for help).\n",
1034 if (c->actionexplanation) {
1035 printf("%s %s.\n", *c->variable? "Will" : "Won't",
1036 c->actionexplanation);
1041 } else if (Ambiguous((void *)ct)) {
1042 fprintf(stderr, "'%s': ambiguous argument ('unset ?' for help).\n",
1045 } else if (ct->handler) {
1047 printf("%s reset to \"%s\".\n", ct->name, (char *)ct->charp);
1049 *(ct->charp) = _POSIX_VDISABLE;
1050 printf("%s character is '%s'.\n", ct->name, control(*(ct->charp)));
1057 * The following are the data structures and routines for the
1060 #ifdef KLUDGELINEMODE
1061 extern int kludgelinemode;
1067 send_wont(TELOPT_LINEMODE, 1);
1068 send_dont(TELOPT_SGA, 1);
1069 send_dont(TELOPT_ECHO, 1);
1077 #ifdef KLUDGELINEMODE
1079 send_dont(TELOPT_SGA, 1);
1081 send_will(TELOPT_LINEMODE, 1);
1082 send_dont(TELOPT_ECHO, 1);
1089 #ifdef KLUDGELINEMODE
1091 send_do(TELOPT_SGA, 1);
1094 send_wont(TELOPT_LINEMODE, 1);
1095 send_do(TELOPT_ECHO, 1);
1100 dolmmode(int bit, int on)
1103 extern int linemode;
1105 if (my_want_state_is_wont(TELOPT_LINEMODE)) {
1106 printf("?Need to have LINEMODE option enabled first.\n");
1107 printf("'mode ?' for help.\n");
1112 c = (linemode | bit);
1114 c = (linemode & ~bit);
1122 return dolmmode(bit, 1);
1128 return dolmmode(bit, 0);
1132 const char *name; /* command name */
1133 const char *help; /* help string */
1134 int (*handler)(int);/* routine which executes command */
1135 int needconnect; /* Do we need to be connected to execute? */
1139 static struct modelist ModeList[] = {
1140 { "character", "Disable LINEMODE option", (int (*)(int))docharmode, 1, 0 },
1141 #ifdef KLUDGELINEMODE
1142 { "", "(or disable obsolete line-by-line mode)", NULL, 0, 0 },
1144 { "line", "Enable LINEMODE option", (int (*)(int))dolinemode, 1, 0 },
1145 #ifdef KLUDGELINEMODE
1146 { "", "(or enable obsolete line-by-line mode)", NULL, 0, 0 },
1148 { "", "", NULL, 0, 0 },
1149 { "", "These require the LINEMODE option to be enabled", NULL, 0, 0 },
1150 { "isig", "Enable signal trapping", setmod, 1, MODE_TRAPSIG },
1151 { "+isig", 0, setmod, 1, MODE_TRAPSIG },
1152 { "-isig", "Disable signal trapping", clearmode, 1, MODE_TRAPSIG },
1153 { "edit", "Enable character editing", setmod, 1, MODE_EDIT },
1154 { "+edit", 0, setmod, 1, MODE_EDIT },
1155 { "-edit", "Disable character editing", clearmode, 1, MODE_EDIT },
1156 { "softtabs", "Enable tab expansion", setmod, 1, MODE_SOFT_TAB },
1157 { "+softtabs", 0, setmod, 1, MODE_SOFT_TAB },
1158 { "-softtabs", "Disable character editing", clearmode, 1, MODE_SOFT_TAB },
1159 { "litecho", "Enable literal character echo", setmod, 1, MODE_LIT_ECHO },
1160 { "+litecho", 0, setmod, 1, MODE_LIT_ECHO },
1161 { "-litecho", "Disable literal character echo", clearmode, 1, MODE_LIT_ECHO },
1162 { "help", 0, (int (*)(int))modehelp, 0, 0 },
1163 #ifdef KLUDGELINEMODE
1164 { "kludgeline", 0, (int (*)(int))dokludgemode, 1, 0 },
1166 { "", "", NULL, 0, 0 },
1167 { "?", "Print help information", (int (*)(int))modehelp, 0, 0 },
1168 { NULL, NULL, NULL, 0, 0 },
1175 struct modelist *mt;
1177 printf("format is: 'mode Mode', where 'Mode' is one of:\n\n");
1178 for (mt = ModeList; mt->name; mt++) {
1181 printf("%-15s %s\n", mt->name, mt->help);
1189 #define GETMODECMD(name) (struct modelist *) \
1190 genget(name, (char **) ModeList, sizeof(struct modelist))
1193 modecmd(int argc, char *argv[])
1195 struct modelist *mt;
1198 printf("'mode' command requires an argument\n");
1199 printf("'mode ?' for help.\n");
1200 } else if ((mt = GETMODECMD(argv[1])) == 0) {
1201 fprintf(stderr, "Unknown mode '%s' ('mode ?' for help).\n", argv[1]);
1202 } else if (Ambiguous((void *)mt)) {
1203 fprintf(stderr, "Ambiguous mode '%s' ('mode ?' for help).\n", argv[1]);
1204 } else if (mt->needconnect && !connected) {
1205 printf("?Need to be connected first.\n");
1206 printf("'mode ?' for help.\n");
1207 } else if (mt->handler) {
1208 return (*mt->handler)(mt->arg1);
1214 * The following data structures and routines implement the
1215 * "display" command.
1219 display(int argc, char *argv[])
1221 struct togglelist *tl;
1224 #define dotog(tl) if (tl->variable && tl->actionexplanation) { \
1225 if (*tl->variable) { \
1230 printf(" %s.\n", tl->actionexplanation); \
1233 #define doset(sl) if (sl->name && *sl->name != ' ') { \
1234 if (sl->handler == 0) \
1235 printf("%-15s [%s]\n", sl->name, control(*sl->charp)); \
1237 printf("%-15s \"%s\"\n", sl->name, (char *)sl->charp); \
1241 for (tl = Togglelist; tl->name; tl++) {
1245 for (sl = Setlist; sl->name; sl++) {
1251 for (i = 1; i < argc; i++) {
1252 sl = getset(argv[i]);
1253 tl = GETTOGGLE(argv[i]);
1254 if (Ambiguous((void *)sl) || Ambiguous((void *)tl)) {
1255 printf("?Ambiguous argument '%s'.\n", argv[i]);
1257 } else if (!sl && !tl) {
1258 printf("?Unknown argument '%s'.\n", argv[i]);
1270 /*@*/optionstatus();
1273 #endif /* ENCRYPTION */
1280 * The following are the data structures, and many of the routines,
1281 * relating to command processing.
1285 * Set the escape character.
1288 setescape(int argc, char *argv[])
1294 "Deprecated usage - please use 'set escape%s%s' in the future.\n",
1295 (argc > 2)? " ":"", (argc > 2)? argv[1]: "");
1299 printf("new escape character: ");
1300 (void) fgets(buf, sizeof(buf), stdin);
1305 (void) fflush(stdout);
1313 printf("Deprecated usage - please use 'toggle crmod' in the future.\n");
1314 printf("%s map carriage return on output.\n", crmod ? "Will" : "Won't");
1315 (void) fflush(stdout);
1325 long oldrows, oldcols, newrows, newcols, err_;
1327 err_ = (TerminalWindowSize(&oldrows, &oldcols) == 0) ? 1 : 0;
1328 (void) kill(0, SIGTSTP);
1330 * If we didn't get the window size before the SUSPEND, but we
1331 * can get them now (?), then send the NAWS to make sure that
1332 * we are set up for the right window size.
1334 if (TerminalWindowSize(&newrows, &newcols) && connected &&
1335 (err_ || ((oldrows != newrows) || (oldcols != newcols)))) {
1339 /* reget parameters in case they were changed */
1340 TerminalSaveState();
1343 printf("Suspend is not supported. Try the '!' command instead\n");
1349 shell(int argc, char *argv[] __unused)
1351 long oldrows, oldcols, newrows, newcols, err_;
1355 err_ = (TerminalWindowSize(&oldrows, &oldcols) == 0) ? 1 : 0;
1358 perror("Fork failed\n");
1364 * Fire up the shell in the child.
1366 const char *shellp, *shellname;
1368 shellp = getenv("SHELL");
1371 if ((shellname = strrchr(shellp, '/')) == 0)
1376 execl(shellp, shellname, "-c", &saveline[1], (char *)0);
1378 execl(shellp, shellname, (char *)0);
1383 (void)wait((int *)0); /* Wait for the shell to complete */
1385 if (TerminalWindowSize(&newrows, &newcols) && connected &&
1386 (err_ || ((oldrows != newrows) || (oldcols != newcols)))) {
1395 bye(int argc, char *argv[])
1397 extern int resettermname;
1400 (void) shutdown(net, 2);
1401 printf("Connection closed.\n");
1402 (void) NetClose(net);
1405 #ifdef AUTHENTICATION
1407 auth_encrypt_connect(connected);
1413 if ((argc != 2) || (strcmp(argv[1], "fromquit") != 0)) {
1414 longjmp(toplevel, 1);
1417 return 1; /* Keep lint, etc., happy */
1423 (void) call(bye, "bye", "fromquit", 0);
1430 send_do(TELOPT_LOGOUT, 1);
1443 void (*handler)(int);
1447 static void slc_help(void);
1449 struct slclist SlcList[] = {
1450 { "export", "Use local special character definitions",
1451 (void (*)(int))slc_mode_export, 0 },
1452 { "import", "Use remote special character definitions",
1453 slc_mode_import, 1 },
1454 { "check", "Verify remote special character definitions",
1455 slc_mode_import, 0 },
1456 { "help", NULL, (void (*)(int))slc_help, 0 },
1457 { "?", "Print help information", (void (*)(int))slc_help, 0 },
1458 { NULL, NULL, NULL, 0 },
1466 for (c = SlcList; c->name; c++) {
1469 printf("%-15s %s\n", c->name, c->help);
1476 static struct slclist *
1479 return (struct slclist *)
1480 genget(name, (char **) SlcList, sizeof(struct slclist));
1484 slccmd(int argc, char *argv[])
1490 "Need an argument to 'slc' command. 'slc ?' for help.\n");
1493 c = getslc(argv[1]);
1495 fprintf(stderr, "'%s': unknown argument ('slc ?' for help).\n",
1499 if (Ambiguous((void *)c)) {
1500 fprintf(stderr, "'%s': ambiguous argument ('slc ?' for help).\n",
1504 (*c->handler)(c->arg);
1510 * The ENVIRON command.
1516 void (*handler)(unsigned char *, unsigned char *);
1520 extern struct env_lst *
1521 env_define(const unsigned char *, unsigned char *);
1523 env_undefine(unsigned char *),
1524 env_export(const unsigned char *),
1525 env_unexport(const unsigned char *),
1526 env_send(unsigned char *),
1527 #if defined(OLD_ENVIRON) && defined(ENV_HACK)
1528 env_varval(unsigned char *),
1534 struct envlist EnvList[] = {
1535 { "define", "Define an environment variable",
1536 (void (*)(unsigned char *, unsigned char *))env_define, 2 },
1537 { "undefine", "Undefine an environment variable",
1538 (void (*)(unsigned char *, unsigned char *))env_undefine, 1 },
1539 { "export", "Mark an environment variable for automatic export",
1540 (void (*)(unsigned char *, unsigned char *))env_export, 1 },
1541 { "unexport", "Don't mark an environment variable for automatic export",
1542 (void (*)(unsigned char *, unsigned char *))env_unexport, 1 },
1543 { "send", "Send an environment variable", (void (*)(unsigned char *, unsigned char *))env_send, 1 },
1544 { "list", "List the current environment variables",
1545 (void (*)(unsigned char *, unsigned char *))env_list, 0 },
1546 #if defined(OLD_ENVIRON) && defined(ENV_HACK)
1547 { "varval", "Reverse VAR and VALUE (auto, right, wrong, status)",
1548 (void (*)(unsigned char *, unsigned char *))env_varval, 1 },
1550 { "help", NULL, (void (*)(unsigned char *, unsigned char *))env_help, 0 },
1551 { "?", "Print help information", (void (*)(unsigned char *, unsigned char *))env_help, 0 },
1552 { NULL, NULL, NULL, 0 },
1560 for (c = EnvList; c->name; c++) {
1563 printf("%-15s %s\n", c->name, c->help);
1570 static struct envlist *
1571 getenvcmd(char *name)
1573 return (struct envlist *)
1574 genget(name, (char **) EnvList, sizeof(struct envlist));
1578 env_cmd(int argc, char *argv[])
1584 "Need an argument to 'environ' command. 'environ ?' for help.\n");
1587 c = getenvcmd(argv[1]);
1589 fprintf(stderr, "'%s': unknown argument ('environ ?' for help).\n",
1593 if (Ambiguous((void *)c)) {
1594 fprintf(stderr, "'%s': ambiguous argument ('environ ?' for help).\n",
1598 if (c->narg + 2 != argc) {
1600 "Need %s%d argument%s to 'environ %s' command. 'environ ?' for help.\n",
1601 c->narg < argc + 2 ? "only " : "",
1602 c->narg, c->narg == 1 ? "" : "s", c->name);
1605 (*c->handler)(argv[2], argv[3]);
1610 struct env_lst *next; /* pointer to next structure */
1611 struct env_lst *prev; /* pointer to previous structure */
1612 unsigned char *var; /* pointer to variable name */
1613 unsigned char *value; /* pointer to variable value */
1614 int export; /* 1 -> export with default list of variables */
1615 int welldefined; /* A well defined variable */
1618 struct env_lst envlisthead;
1620 static struct env_lst *
1621 env_find(const unsigned char *var)
1625 for (ep = envlisthead.next; ep; ep = ep->next) {
1626 if (strcmp(ep->var, var) == 0)
1635 extern char **environ;
1639 for (epp = environ; *epp; epp++) {
1640 if ((cp = strchr(*epp, '='))) {
1642 ep = env_define((unsigned char *)*epp,
1643 (unsigned char *)cp+1);
1649 * Special case for DISPLAY variable. If it is ":0.0" or
1650 * "unix:0.0", we have to get rid of "unix" and insert our
1653 if ((ep = env_find("DISPLAY"))
1654 && ((*ep->value == ':')
1655 || (strncmp((char *)ep->value, "unix:", 5) == 0))) {
1657 char *cp2 = strchr((char *)ep->value, ':');
1660 gethostname(hbuf, sizeof(hbuf));
1661 hbuf[sizeof(hbuf)-1] = '\0';
1662 buflen = strlen(hbuf) + strlen(cp2) + 1;
1663 cp = (char *)malloc(sizeof(char)*buflen);
1665 snprintf((char *)cp, buflen, "%s%s", hbuf, cp2);
1667 ep->value = (unsigned char *)cp;
1670 * If USER is not defined, but LOGNAME is, then add
1671 * USER with the value from LOGNAME. By default, we
1672 * don't export the USER variable.
1674 if ((env_find("USER") == NULL) && (ep = env_find("LOGNAME"))) {
1675 env_define("USER", ep->value);
1676 env_unexport("USER");
1678 env_export("DISPLAY");
1679 env_export("PRINTER");
1683 env_define(const unsigned char *var, unsigned char *value)
1687 if ((ep = env_find(var))) {
1693 ep = (struct env_lst *)malloc(sizeof(struct env_lst));
1694 ep->next = envlisthead.next;
1695 envlisthead.next = ep;
1696 ep->prev = &envlisthead;
1698 ep->next->prev = ep;
1700 ep->welldefined = opt_welldefined(var);
1702 ep->var = strdup(var);
1703 ep->value = strdup(value);
1708 env_undefine(unsigned char *var)
1712 if ((ep = env_find(var))) {
1713 ep->prev->next = ep->next;
1715 ep->next->prev = ep->prev;
1725 env_export(const unsigned char *var)
1729 if ((ep = env_find(var)))
1734 env_unexport(const unsigned char *var)
1738 if ((ep = env_find(var)))
1743 env_send(unsigned char *var)
1747 if (my_state_is_wont(TELOPT_NEW_ENVIRON)
1749 && my_state_is_wont(TELOPT_OLD_ENVIRON)
1753 "Cannot send '%s': Telnet ENVIRON option not enabled\n",
1759 fprintf(stderr, "Cannot send '%s': variable not defined\n",
1763 env_opt_start_info();
1764 env_opt_add(ep->var);
1773 for (ep = envlisthead.next; ep; ep = ep->next) {
1774 printf("%c %-20s %s\n", ep->export ? '*' : ' ',
1775 ep->var, ep->value);
1780 env_default(int init, int welldefined)
1782 static struct env_lst *nep = NULL;
1789 while ((nep = nep->next)) {
1790 if (nep->export && (nep->welldefined == welldefined))
1798 env_getvalue(const unsigned char *var)
1802 if ((ep = env_find(var)))
1807 #if defined(OLD_ENVIRON) && defined(ENV_HACK)
1809 env_varval(unsigned char *what)
1811 extern int old_env_var, old_env_value, env_auto;
1812 int len = strlen((char *)what);
1817 if (strncasecmp((char *)what, "status", len) == 0) {
1819 printf("%s%s", "VAR and VALUE are/will be ",
1820 "determined automatically\n");
1821 if (old_env_var == OLD_ENV_VAR)
1822 printf("VAR and VALUE set to correct definitions\n");
1824 printf("VAR and VALUE definitions are reversed\n");
1825 } else if (strncasecmp((char *)what, "auto", len) == 0) {
1827 old_env_var = OLD_ENV_VALUE;
1828 old_env_value = OLD_ENV_VAR;
1829 } else if (strncasecmp((char *)what, "right", len) == 0) {
1831 old_env_var = OLD_ENV_VAR;
1832 old_env_value = OLD_ENV_VALUE;
1833 } else if (strncasecmp((char *)what, "wrong", len) == 0) {
1835 old_env_var = OLD_ENV_VALUE;
1836 old_env_value = OLD_ENV_VAR;
1839 printf("Unknown \"varval\" command. (\"auto\", \"right\", \"wrong\", \"status\")\n");
1844 #ifdef AUTHENTICATION
1846 * The AUTHENTICATE command.
1852 int (*handler)(char *);
1857 auth_enable(char *),
1858 auth_disable(char *),
1863 struct authlist AuthList[] = {
1864 { "status", "Display current status of authentication information",
1865 (int (*)(char *))auth_status, 0 },
1866 { "disable", "Disable an authentication type ('auth disable ?' for more)",
1868 { "enable", "Enable an authentication type ('auth enable ?' for more)",
1870 { "help", NULL, (int (*)(char *))auth_help, 0 },
1871 { "?", "Print help information", (int (*)(char *))auth_help, 0 },
1872 { NULL, NULL, NULL, 0 },
1880 for (c = AuthList; c->name; c++) {
1883 printf("%-15s %s\n", c->name, c->help);
1892 auth_cmd(int argc, char *argv[])
1898 "Need an argument to 'auth' command. 'auth ?' for help.\n");
1902 c = (struct authlist *)
1903 genget(argv[1], (char **) AuthList, sizeof(struct authlist));
1905 fprintf(stderr, "'%s': unknown argument ('auth ?' for help).\n",
1909 if (Ambiguous((void *)c)) {
1910 fprintf(stderr, "'%s': ambiguous argument ('auth ?' for help).\n",
1914 if (c->narg + 2 != argc) {
1916 "Need %s%d argument%s to 'auth %s' command. 'auth ?' for help.\n",
1917 c->narg < argc + 2 ? "only " : "",
1918 c->narg, c->narg == 1 ? "" : "s", c->name);
1921 return((*c->handler)(argv[2]));
1927 * The ENCRYPT command.
1930 struct encryptlist {
1933 int (*handler)(char *, char *);
1940 EncryptEnable(char *, char *),
1941 EncryptDisable(char *, char *),
1942 EncryptType(char *, char *),
1943 EncryptStart(char *),
1944 EncryptStartInput(void),
1945 EncryptStartOutput(void),
1946 EncryptStop(char *),
1947 EncryptStopInput(void),
1948 EncryptStopOutput(void),
1949 EncryptStatus(void);
1953 struct encryptlist EncryptList[] = {
1954 { "enable", "Enable encryption. ('encrypt enable ?' for more)",
1955 EncryptEnable, 1, 1, 2 },
1956 { "disable", "Disable encryption. ('encrypt enable ?' for more)",
1957 EncryptDisable, 0, 1, 2 },
1958 { "type", "Set encryption type. ('encrypt type ?' for more)",
1959 EncryptType, 0, 1, 1 },
1960 { "start", "Start encryption. ('encrypt start ?' for more)",
1961 (int (*)(char *, char *))EncryptStart, 1, 0, 1 },
1962 { "stop", "Stop encryption. ('encrypt stop ?' for more)",
1963 (int (*)(char *, char *))EncryptStop, 1, 0, 1 },
1964 { "input", "Start encrypting the input stream",
1965 (int (*)(char *, char *))EncryptStartInput, 1, 0, 0 },
1966 { "-input", "Stop encrypting the input stream",
1967 (int (*)(char *, char *))EncryptStopInput, 1, 0, 0 },
1968 { "output", "Start encrypting the output stream",
1969 (int (*)(char *, char *))EncryptStartOutput, 1, 0, 0 },
1970 { "-output", "Stop encrypting the output stream",
1971 (int (*)(char *, char *))EncryptStopOutput, 1, 0, 0 },
1973 { "status", "Display current status of authentication information",
1974 (int (*)(char *, char *))EncryptStatus, 0, 0, 0 },
1975 { "help", NULL, (int (*)(char *, char *))EncryptHelp, 0, 0, 0 },
1976 { "?", "Print help information", (int (*)(char *, char *))EncryptHelp, 0, 0, 0 },
1977 { NULL, NULL, NULL, 0, 0, 0 },
1983 struct encryptlist *c;
1985 for (c = EncryptList; c->name; c++) {
1988 printf("%-15s %s\n", c->name, c->help);
1997 encrypt_cmd(int argc, char *argv[])
1999 struct encryptlist *c;
2003 "Need an argument to 'encrypt' command. 'encrypt ?' for help.\n");
2007 c = (struct encryptlist *)
2008 genget(argv[1], (char **) EncryptList, sizeof(struct encryptlist));
2010 fprintf(stderr, "'%s': unknown argument ('encrypt ?' for help).\n",
2014 if (Ambiguous((void *)c)) {
2015 fprintf(stderr, "'%s': ambiguous argument ('encrypt ?' for help).\n",
2020 if (argc < c->minarg || argc > c->maxarg) {
2021 if (c->minarg == c->maxarg) {
2022 fprintf(stderr, "Need %s%d argument%s ",
2023 c->minarg < argc ? "only " : "", c->minarg,
2024 c->minarg == 1 ? "" : "s");
2026 fprintf(stderr, "Need %s%d-%d arguments ",
2027 c->maxarg < argc ? "only " : "", c->minarg, c->maxarg);
2029 fprintf(stderr, "to 'encrypt %s' command. 'encrypt ?' for help.\n",
2033 if (c->needconnect && !connected) {
2034 if (!(argc && (isprefix(argv[2], "help") || isprefix(argv[2], "?")))) {
2035 printf("?Need to be connected first.\n");
2039 return ((*c->handler)(argc > 0 ? argv[2] : 0,
2040 argc > 1 ? argv[3] : 0));
2042 #endif /* ENCRYPTION */
2045 * Print status about the connection.
2049 status(int argc, char *argv[])
2052 printf("Connected to %s.\n", hostname);
2053 if ((argc < 2) || strcmp(argv[1], "notmuch")) {
2054 int mode = getconnmode();
2056 if (my_want_state_is_will(TELOPT_LINEMODE)) {
2057 printf("Operating with LINEMODE option\n");
2058 printf("%s line editing\n", (mode&MODE_EDIT) ? "Local" : "No");
2059 printf("%s catching of signals\n",
2060 (mode&MODE_TRAPSIG) ? "Local" : "No");
2062 #ifdef KLUDGELINEMODE
2063 } else if (kludgelinemode && my_want_state_is_dont(TELOPT_SGA)) {
2064 printf("Operating in obsolete linemode\n");
2067 printf("Operating in single character mode\n");
2069 printf("Catching signals locally\n");
2071 printf("%s character echo\n", (mode&MODE_ECHO) ? "Local" : "Remote");
2072 if (my_want_state_is_will(TELOPT_LFLOW))
2073 printf("%s flow control\n", (mode&MODE_FLOW) ? "Local" : "No");
2076 #endif /* ENCRYPTION */
2079 printf("No connection.\n");
2081 printf("Escape character is '%s'.\n", control(escape));
2082 (void) fflush(stdout);
2088 * Function that gets called when SIGINFO is received.
2093 (void) call(status, "status", "notmuch", 0);
2098 sockaddr_ntop(struct sockaddr *sa)
2101 static char addrbuf[INET6_ADDRSTRLEN];
2103 switch (sa->sa_family) {
2105 addr = &((struct sockaddr_in *)sa)->sin_addr;
2108 addr = &((struct sockaddr_un *)sa)->sun_path;
2112 addr = &((struct sockaddr_in6 *)sa)->sin6_addr;
2118 inet_ntop(sa->sa_family, addr, addrbuf, sizeof(addrbuf));
2122 #if defined(IPSEC) && defined(IPSEC_POLICY_IPSEC)
2124 setpolicy(int lnet, struct addrinfo *res, char *policy)
2133 buf = ipsec_set_policy(policy, strlen(policy));
2135 printf("%s\n", ipsec_strerror());
2138 level = res->ai_family == AF_INET ? IPPROTO_IP : IPPROTO_IPV6;
2139 optname = res->ai_family == AF_INET ? IP_IPSEC_POLICY : IPV6_IPSEC_POLICY;
2140 if (setsockopt(lnet, level, optname, buf, ipsec_get_policylen(buf)) < 0){
2141 perror("setsockopt");
2152 * When an Address Family related error happend, check if retry with
2153 * another AF is possible or not.
2154 * Return 1, if retry with another af is OK. Else, return 0.
2157 switch_af(struct addrinfo **aip)
2160 struct addrinfo *ai;
2163 nextaf = (ai->ai_family == AF_INET) ? AF_INET6 : AF_INET;
2166 while (ai != NULL && ai->ai_family != nextaf);
2176 tn(int argc, char *argv[])
2178 unsigned char *srp = 0;
2181 int srcroute = 0, result;
2182 char *cmd, *hostp = 0, *portp = 0, *user = 0;
2183 char *src_addr = NULL;
2184 struct addrinfo hints, *res, *res0 = NULL, *src_res, *src_res0 = NULL;
2185 int error = 0, af_error = 0;
2188 printf("?Already connected to %s\n", hostname);
2193 (void) strcpy(line, "open ");
2195 (void) fgets(&line[strlen(line)], sizeof(line) - strlen(line), stdin);
2203 if (strcmp(*argv, "help") == 0 || isprefix(*argv, "?"))
2205 if (strcmp(*argv, "-l") == 0) {
2213 if (strcmp(*argv, "-a") == 0) {
2218 if (strcmp(*argv, "-s") == 0) {
2237 printf("usage: %s [-l user] [-a] [-s src_addr] host-name [port]\n", cmd);
2244 if (src_addr != NULL) {
2245 memset(&hints, 0, sizeof(hints));
2246 hints.ai_family = family;
2247 hints.ai_socktype = SOCK_STREAM;
2248 error = getaddrinfo(src_addr, 0, &hints, &src_res);
2249 if (error == EAI_NONAME) {
2251 error = getaddrinfo(src_addr, 0, &hints, &src_res);
2254 fprintf(stderr, "%s: %s\n", src_addr, gai_strerror(error));
2255 if (error == EAI_SYSTEM)
2256 fprintf(stderr, "%s: %s\n", src_addr, strerror(errno));
2262 if (hostp[0] == '/') {
2263 struct sockaddr_un su;
2265 if (strlen(hostp) >= sizeof(su.sun_path)) {
2266 fprintf(stderr, "hostname too long for unix domain socket: %s",
2271 memset(&su, 0, sizeof su);
2272 su.sun_family = AF_UNIX;
2273 strncpy(su.sun_path, hostp, sizeof su.sun_path);
2274 printf("Trying %s...\n", hostp);
2275 net = socket(PF_UNIX, SOCK_STREAM, 0);
2280 if (connect(net, (struct sockaddr *)&su, sizeof su) == -1) {
2281 perror(su.sun_path);
2282 (void) NetClose(net);
2286 } else if (hostp[0] == '@' || hostp[0] == '!') {
2289 family == AF_INET6 ||
2291 (hostname = strrchr(hostp, ':')) == NULL)
2292 hostname = strrchr(hostp, '@');
2293 if (hostname == NULL) {
2303 portp = strdup("telnet");
2304 } else if (*portp == '-') {
2307 } else if (*portp == '+') {
2313 memset(&hints, 0, sizeof(hints));
2314 hints.ai_flags = AI_NUMERICHOST;
2315 hints.ai_family = family;
2316 hints.ai_socktype = SOCK_STREAM;
2317 error = getaddrinfo(hostname, portp, &hints, &res);
2319 hints.ai_flags = AI_CANONNAME;
2320 error = getaddrinfo(hostname, portp, &hints, &res);
2323 fprintf(stderr, "%s: %s\n", hostname, gai_strerror(error));
2324 if (error == EAI_SYSTEM)
2325 fprintf(stderr, "%s: %s\n", hostname, strerror(errno));
2329 if (hints.ai_flags == AI_NUMERICHOST) {
2330 /* hostname has numeric */
2334 gni_err = getnameinfo(res->ai_addr, res->ai_addr->sa_len,
2335 _hostname, sizeof(_hostname) - 1, NULL, 0,
2338 (void) strncpy(_hostname, hostp, sizeof(_hostname) - 1);
2339 _hostname[sizeof(_hostname)-1] = '\0';
2340 hostname = _hostname;
2342 /* hostname has FQDN */
2344 (void) strncpy(_hostname, hostname, sizeof(_hostname) - 1);
2345 else if (res->ai_canonname != NULL)
2346 strcpy(_hostname, res->ai_canonname);
2348 (void) strncpy(_hostname, hostp, sizeof(_hostname) - 1);
2349 _hostname[sizeof(_hostname)-1] = '\0';
2350 hostname = _hostname;
2356 if (srcroute != 0) {
2357 static char hostbuf[BUFSIZ];
2359 if (af_error == 0) { /* save intermediate hostnames for retry */
2360 strncpy(hostbuf, hostp, BUFSIZ - 1);
2361 hostbuf[BUFSIZ - 1] = '\0';
2365 result = sourceroute(res, hostp, &srp, &srlen, &proto, &opt);
2368 if (family == AF_UNSPEC && af_error == 0 &&
2369 switch_af(&res) == 1) {
2376 } else if (result == -1) {
2377 printf("Bad source route option: %s\n", hostp);
2383 printf("Trying %s...\n", sockaddr_ntop(res->ai_addr));
2384 net = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
2388 if (family == AF_UNSPEC && af_error == 0 &&
2389 switch_af(&res) == 1) {
2394 perror("telnet: socket");
2397 if (srp && setsockopt(net, proto, opt, (char *)srp, srlen) < 0)
2398 perror("setsockopt (source route)");
2399 #if defined(IPPROTO_IP) && defined(IP_TOS)
2400 if (res->ai_family == PF_INET) {
2401 # if defined(HAS_GETTOS)
2403 if (tos < 0 && (tp = gettosbyname("telnet", "tcp")))
2407 tos = IPTOS_LOWDELAY;
2409 && (setsockopt(net, IPPROTO_IP, IP_TOS,
2410 (char *)&tos, sizeof(int)) < 0)
2411 && (errno != ENOPROTOOPT))
2412 perror("telnet: setsockopt (IP_TOS) (ignored)");
2414 #endif /* defined(IPPROTO_IP) && defined(IP_TOS) */
2416 if (telnet_debug && SetSockOpt(net, SOL_SOCKET, SO_DEBUG, 1) < 0) {
2417 perror("setsockopt (SO_DEBUG)");
2420 if (src_addr != NULL) {
2421 for (src_res = src_res0; src_res != 0; src_res = src_res->ai_next)
2422 if (src_res->ai_family == res->ai_family)
2424 if (src_res == NULL)
2426 if (bind(net, src_res->ai_addr, src_res->ai_addrlen) == -1) {
2428 if (family == AF_UNSPEC && af_error == 0 &&
2429 switch_af(&res) == 1) {
2431 (void) NetClose(net);
2436 (void) NetClose(net);
2440 #if defined(IPSEC) && defined(IPSEC_POLICY_IPSEC)
2441 if (setpolicy(net, res, ipsec_policy_in) < 0) {
2442 (void) NetClose(net);
2445 if (setpolicy(net, res, ipsec_policy_out) < 0) {
2446 (void) NetClose(net);
2451 if (connect(net, res->ai_addr, res->ai_addrlen) < 0) {
2452 struct addrinfo *next;
2454 next = res->ai_next;
2455 /* If already an af failed, only try same af. */
2457 while (next != NULL && next->ai_family != res->ai_family)
2458 next = next->ai_next;
2459 warn("connect to address %s", sockaddr_ntop(res->ai_addr));
2462 (void) NetClose(net);
2465 warnx("Unable to connect to remote host");
2466 (void) NetClose(net);
2470 #ifdef AUTHENTICATION
2472 auth_encrypt_connect(connected);
2475 } while (connected == 0);
2477 if (src_res0 != NULL)
2478 freeaddrinfo(src_res0);
2479 cmdrc(hostp, hostname);
2482 if (autologin && user == NULL) {
2485 user = getenv("USER");
2487 ((pw = getpwnam(user)) && pw->pw_uid != getuid())) {
2488 if ((pw = getpwuid(getuid())))
2495 env_define("USER", user);
2498 (void) call(status, "status", "notmuch", 0);
2500 (void) NetClose(net);
2501 ExitString("Connection closed by foreign host.\n",1);
2506 if (src_res0 != NULL)
2507 freeaddrinfo(src_res0);
2511 #define HELPINDENT (sizeof ("connect"))
2514 openhelp[] = "connect to a site",
2515 closehelp[] = "close current connection",
2516 logouthelp[] = "forcibly logout remote user and close the connection",
2517 quithelp[] = "exit telnet",
2518 statushelp[] = "print status information",
2519 helphelp[] = "print help information",
2520 sendhelp[] = "transmit special characters ('send ?' for more)",
2521 sethelp[] = "set operating parameters ('set ?' for more)",
2522 unsethelp[] = "unset operating parameters ('unset ?' for more)",
2523 togglestring[] ="toggle operating parameters ('toggle ?' for more)",
2524 slchelp[] = "change state of special charaters ('slc ?' for more)",
2525 displayhelp[] = "display operating parameters",
2526 #ifdef AUTHENTICATION
2527 authhelp[] = "turn on (off) authentication ('auth ?' for more)",
2530 encrypthelp[] = "turn on (off) encryption ('encrypt ?' for more)",
2531 #endif /* ENCRYPTION */
2532 zhelp[] = "suspend telnet",
2534 opiehelp[] = "compute response to OPIE challenge",
2536 shellhelp[] = "invoke a subshell",
2537 envhelp[] = "change environment variables ('environ ?' for more)",
2538 modestring[] = "try to enter line or character mode ('mode ?' for more)";
2540 static Command cmdtab[] = {
2541 { "close", closehelp, bye, 1 },
2542 { "logout", logouthelp, (int (*)(int, char **))logout, 1 },
2543 { "display", displayhelp, display, 0 },
2544 { "mode", modestring, modecmd, 0 },
2545 { "telnet", openhelp, tn, 0 },
2546 { "open", openhelp, tn, 0 },
2547 { "quit", quithelp, (int (*)(int, char **))quit, 0 },
2548 { "send", sendhelp, sendcmd, 0 },
2549 { "set", sethelp, setcmd, 0 },
2550 { "unset", unsethelp, unsetcmd, 0 },
2551 { "status", statushelp, status, 0 },
2552 { "toggle", togglestring, toggle, 0 },
2553 { "slc", slchelp, slccmd, 0 },
2554 #ifdef AUTHENTICATION
2555 { "auth", authhelp, auth_cmd, 0 },
2558 { "encrypt", encrypthelp, encrypt_cmd, 0 },
2559 #endif /* ENCRYPTION */
2560 { "z", zhelp, (int (*)(int, char **))suspend, 0 },
2561 { "!", shellhelp, shell, 1 },
2562 { "environ", envhelp, env_cmd, 0 },
2563 { "?", helphelp, help, 0 },
2565 { "opie", opiehelp, opie_calc, 0 },
2567 { NULL, NULL, NULL, 0 }
2570 static char crmodhelp[] = "deprecated command -- use 'toggle crmod' instead";
2571 static char escapehelp[] = "deprecated command -- use 'set escape' instead";
2573 static Command cmdtab2[] = {
2574 { "help", 0, help, 0 },
2575 { "escape", escapehelp, setescape, 0 },
2576 { "crmod", crmodhelp, (int (*)(int, char **))togcrmod, 0 },
2577 { NULL, NULL, NULL, 0 }
2582 * Call routine with argc, argv set from args (terminated by 0).
2586 call(intrtn_t routine, ...)
2592 va_start(ap, routine);
2593 while ((args[argno++] = va_arg(ap, char *)) != 0);
2595 return (*routine)(argno-1, args);
2604 if ((cm = (Command *) genget(name, (char **) cmdtab, sizeof(Command))))
2606 return (Command *) genget(name, (char **) cmdtab2, sizeof(Command));
2610 command(int top, const char *tbuf, int cnt)
2618 (void) signal(SIGINT, SIG_DFL);
2619 (void) signal(SIGQUIT, SIG_DFL);
2622 if (rlogin == _POSIX_VDISABLE)
2623 printf("%s> ", prompt);
2627 while (cnt > 0 && (*cp++ = *tbuf++) != '\n')
2630 if (cp == line || *--cp != '\n' || cp == line)
2633 if (rlogin == _POSIX_VDISABLE)
2634 printf("%s\n", line);
2637 if (rlogin != _POSIX_VDISABLE)
2638 printf("%s> ", prompt);
2639 if (fgets(line, sizeof(line), stdin) == NULL) {
2640 if (feof(stdin) || ferror(stdin)) {
2650 if (margv[0] == 0) {
2653 c = getcmd(margv[0]);
2654 if (Ambiguous((void *)c)) {
2655 printf("?Ambiguous command\n");
2659 printf("?Invalid command\n");
2662 if (c->needconnect && !connected) {
2663 printf("?Need to be connected first.\n");
2666 if ((*c->handler)(margc, margv)) {
2672 longjmp(toplevel, 1);
2683 help(int argc, char *argv[])
2688 printf("Commands may be abbreviated. Commands are:\n\n");
2689 for (c = cmdtab; c->name; c++)
2691 printf("%-*s\t%s\n", (int)HELPINDENT, c->name,
2696 else while (--argc > 0) {
2700 if (Ambiguous((void *)c))
2701 printf("?Ambiguous help command %s\n", arg);
2702 else if (c == (Command *)0)
2703 printf("?Invalid help command %s\n", arg);
2705 printf("%s\n", c->help);
2710 static char *rcname = 0;
2711 static char rcbuf[128];
2714 cmdrc(char *m1, char *m2)
2719 int l1 = strlen(m1);
2720 int l2 = strlen(m2);
2721 char m1save[MAXHOSTNAMELEN];
2726 strlcpy(m1save, m1, sizeof(m1save));
2730 rcname = getenv("HOME");
2731 if (rcname && (strlen(rcname) + 10) < sizeof(rcbuf))
2732 strcpy(rcbuf, rcname);
2735 strcat(rcbuf, "/.telnetrc");
2739 if ((rcfile = fopen(rcname, "r")) == 0) {
2744 if (fgets(line, sizeof(line), rcfile) == NULL)
2751 if (!isspace(line[0]))
2754 if (gotmachine == 0) {
2755 if (isspace(line[0]))
2757 if (strncasecmp(line, m1, l1) == 0)
2758 strncpy(line, &line[l1], sizeof(line) - l1);
2759 else if (strncasecmp(line, m2, l2) == 0)
2760 strncpy(line, &line[l2], sizeof(line) - l2);
2761 else if (strncasecmp(line, "DEFAULT", 7) == 0)
2762 strncpy(line, &line[7], sizeof(line) - 7);
2765 if (line[0] != ' ' && line[0] != '\t' && line[0] != '\n')
2772 c = getcmd(margv[0]);
2773 if (Ambiguous((void *)c)) {
2774 printf("?Ambiguous command: %s\n", margv[0]);
2778 printf("?Invalid command: %s\n", margv[0]);
2782 * This should never happen...
2784 if (c->needconnect && !connected) {
2785 printf("?Need to be connected first for %s.\n", margv[0]);
2788 (*c->handler)(margc, margv);
2794 * Source route is handed in as
2795 * [!]@hop1@hop2...[@|:]dst
2796 * If the leading ! is present, it is a
2797 * strict source route, otherwise it is
2798 * assmed to be a loose source route.
2800 * We fill in the source route option as
2801 * hop1,hop2,hop3...dest
2802 * and return a pointer to hop1, which will
2803 * be the address to connect() to.
2807 * res: ponter to addrinfo structure which contains sockaddr to
2808 * the host to connect to.
2810 * arg: pointer to route list to decipher
2812 * cpp: If *cpp is not equal to NULL, this is a
2813 * pointer to a pointer to a character array
2814 * that should be filled in with the option.
2816 * lenp: pointer to an integer that contains the
2817 * length of *cpp if *cpp != NULL.
2819 * protop: pointer to an integer that should be filled in with
2820 * appropriate protocol for setsockopt, as socket
2823 * optp: pointer to an integer that should be filled in with
2824 * appropriate option for setsockopt, as socket protocol
2829 * If the return value is 1, then all operations are
2830 * successful. If the
2831 * return value is -1, there was a syntax error in the
2832 * option, either unknown characters, or too many hosts.
2833 * If the return value is 0, one of the hostnames in the
2834 * path is unknown, and *cpp is set to point to the bad
2837 * *cpp: If *cpp was equal to NULL, it will be filled
2838 * in with a pointer to our static area that has
2839 * the option filled in. This will be 32bit aligned.
2841 * *lenp: This will be filled in with how long the option
2842 * pointed to by *cpp is.
2844 * *protop: This will be filled in with appropriate protocol for
2845 * setsockopt, as socket protocol family.
2847 * *optp: This will be filled in with appropriate option for
2848 * setsockopt, as socket protocol family.
2851 sourceroute(struct addrinfo *ai, char *arg, unsigned char **cpp, int *lenp, int *protop, int *optp)
2853 static char buf[1024 + ALIGNBYTES]; /*XXX*/
2854 unsigned char *cp, *cp2, *lsrp, *ep;
2855 struct sockaddr_in *_sin;
2857 struct sockaddr_in6 *sin6;
2858 struct ip6_rthdr *rth;
2860 struct addrinfo hints, *res;
2865 * Verify the arguments, and make sure we have
2866 * at least 7 bytes for the option.
2868 if (cpp == NULL || lenp == NULL)
2871 switch (res->ai_family) {
2878 if (*lenp < (int)CMSG_SPACE(sizeof(struct ip6_rthdr) +
2879 sizeof(struct in6_addr)))
2886 * Decide whether we have a buffer passed to us,
2887 * or if we need to use our own static buffer.
2893 *cpp = lsrp = (char *)ALIGN(buf);
2900 if (ai->ai_family == AF_INET6) {
2901 if ((rth = inet6_rth_init((void *)*cpp, sizeof(buf),
2902 IPV6_RTHDR_TYPE_0, 0)) == NULL)
2906 *protop = IPPROTO_IPV6;
2912 * Next, decide whether we have a loose source
2913 * route or a strict source route, and fill in
2914 * the begining of the option.
2918 *lsrp++ = IPOPT_SSRR;
2920 *lsrp++ = IPOPT_LSRR;
2925 lsrp++; /* skip over length, we'll fill it in later */
2927 *protop = IPPROTO_IP;
2932 memset(&hints, 0, sizeof(hints));
2933 hints.ai_family = ai->ai_family;
2934 hints.ai_socktype = SOCK_STREAM;
2938 ai->ai_family != AF_INET6 &&
2942 else for (cp2 = cp; (c = *cp2); cp2++) {
2947 } else if (c == '@') {
2951 ai->ai_family != AF_INET6 &&
2962 hints.ai_flags = AI_NUMERICHOST;
2963 error = getaddrinfo(cp, NULL, &hints, &res);
2964 if (error == EAI_NONAME) {
2966 error = getaddrinfo(cp, NULL, &hints, &res);
2969 fprintf(stderr, "%s: %s\n", cp, gai_strerror(error));
2970 if (error == EAI_SYSTEM)
2971 fprintf(stderr, "%s: %s\n", cp,
2977 if (res->ai_family == AF_INET6) {
2978 sin6 = (struct sockaddr_in6 *)res->ai_addr;
2979 if (inet6_rth_add((void *)rth, &sin6->sin6_addr) == -1)
2984 _sin = (struct sockaddr_in *)res->ai_addr;
2985 memcpy(lsrp, (char *)&_sin->sin_addr, 4);
2993 * Check to make sure there is space for next address
3000 if (res->ai_family == AF_INET6) {
3001 rth->ip6r_len = rth->ip6r_segleft * 2;
3002 *lenp = (rth->ip6r_len + 1) << 3;
3006 if ((*(*cpp+IPOPT_OLEN) = lsrp - *cpp) <= 7) {
3011 *lsrp++ = IPOPT_NOP; /* 32 bit word align it */
3012 *lenp = lsrp - *cpp;