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>
59 #include <arpa/telnet.h>
60 #include <arpa/inet.h>
72 #include <libtelnet/auth.h>
75 #include <libtelnet/encrypt.h>
78 #include <netinet/in_systm.h>
79 #include <netinet/ip.h>
80 #include <netinet/ip6.h>
82 #ifndef MAXHOSTNAMELEN
83 #define MAXHOSTNAMELEN 256
86 typedef int (*intrtn_t)(int, char **);
89 extern int auth_togdebug(int);
92 extern int EncryptAutoEnc(int);
93 extern int EncryptAutoDec(int);
94 extern int EncryptDebug(int);
95 extern int EncryptVerbose(int);
96 #endif /* ENCRYPTION */
97 #if defined(IPPROTO_IP) && defined(IP_TOS)
99 #endif /* defined(IPPROTO_IP) && defined(IP_TOS) */
102 static char _hostname[MAXHOSTNAMELEN];
104 static int help(int, char **);
105 static int call(intrtn_t, ...);
106 static void cmdrc(char *, char *);
108 static int switch_af(struct addrinfo **);
110 static int togglehelp(void);
111 static int send_tncmd(void (*)(int, int), const char *, char *);
112 static int setmod(int);
113 static int clearmode(int);
114 static int modehelp(void);
115 static int sourceroute(struct addrinfo *, char *, char **, int *, int *, int *);
118 const char *name; /* command name */
119 const char *help; /* help string (NULL for no help) */
120 int (*handler)(int, char **); /* routine which executes command */
121 int needconnect; /* Do we need to be connected to execute? */
124 static char line[256];
125 static char saveline[256];
127 static char *margv[20];
130 #include <sys/wait.h>
131 #define PATH_OPIEKEY "/usr/bin/opiekey"
133 opie_calc(int argc, char *argv[])
138 printf("%s sequence challenge\n", argv[0]);
144 execv(PATH_OPIEKEY, argv);
150 (void) wait(&status);
151 if (WIFEXITED(status))
152 return (WEXITSTATUS(status));
166 if (*cp == '!') { /* Special case shell escape */
167 strcpy(saveline, line); /* save for shell command */
168 *argp++ = strdup("!"); /* No room in string to get this */
180 for (cp2 = cp; c != '\0'; c = *++cp) {
188 if ((c = *++cp) == '\0')
190 } else if (c == '"') {
193 } else if (c == '\'') {
196 } else if (isspace(c))
210 * Make a character string into a number.
212 * Todo: 1. Could take random integers (12, 0x12, 012, 0b1).
225 c = b | 0x40; /* DEL */
238 * Construct a control character sequence
239 * for a special character.
246 * The only way I could get the Sun 3.5 compiler
248 * if ((unsigned int)c >= 0x80)
249 * was to assign "c" to an unsigned int variable...
252 unsigned int uic = (unsigned int)c;
256 if (c == (cc_t)_POSIX_VDISABLE) {
261 buf[1] = ((c>>6)&07) + '0';
262 buf[2] = ((c>>3)&07) + '0';
263 buf[3] = (c&07) + '0';
265 } else if (uic >= 0x20) {
277 * The following are data structures and routines for
278 * the "send" command.
283 const char *name; /* How user refers to it (case independent) */
284 const char *help; /* Help information (0 ==> no help) */
285 int needconnect; /* Need to be connected */
286 int narg; /* Number of arguments */
287 int (*handler)(char *, ...); /* Routine to perform (for special ops) */
288 int nbyte; /* Number of bytes to send this command */
289 int what; /* Character to be sent (<0 ==> special) */
297 send_dontcmd(char *),
298 send_willcmd(char *),
299 send_wontcmd(char *);
301 static struct sendlist Sendlist[] = {
302 { "ao", "Send Telnet Abort output", 1, 0, NULL, 2, AO },
303 { "ayt", "Send Telnet 'Are You There'", 1, 0, NULL, 2, AYT },
304 { "brk", "Send Telnet Break", 1, 0, NULL, 2, BREAK },
305 { "break", NULL, 1, 0, NULL, 2, BREAK },
306 { "ec", "Send Telnet Erase Character", 1, 0, NULL, 2, EC },
307 { "el", "Send Telnet Erase Line", 1, 0, NULL, 2, EL },
308 { "escape", "Send current escape character",1, 0, (int (*)(char *, ...))send_esc, 1, 0 },
309 { "ga", "Send Telnet 'Go Ahead' sequence", 1, 0, NULL, 2, GA },
310 { "ip", "Send Telnet Interrupt Process",1, 0, NULL, 2, IP },
311 { "intp", NULL, 1, 0, NULL, 2, IP },
312 { "interrupt", NULL, 1, 0, NULL, 2, IP },
313 { "intr", NULL, 1, 0, NULL, 2, IP },
314 { "nop", "Send Telnet 'No operation'", 1, 0, NULL, 2, NOP },
315 { "eor", "Send Telnet 'End of Record'", 1, 0, NULL, 2, EOR },
316 { "abort", "Send Telnet 'Abort Process'", 1, 0, NULL, 2, ABORT },
317 { "susp", "Send Telnet 'Suspend Process'",1, 0, NULL, 2, SUSP },
318 { "eof", "Send Telnet End of File Character", 1, 0, NULL, 2, xEOF },
319 { "synch", "Perform Telnet 'Synch operation'", 1, 0, (int (*)(char *, ...))dosynch, 2, 0 },
320 { "getstatus", "Send request for STATUS", 1, 0, (int (*)(char *, ...))get_status, 6, 0 },
321 { "?", "Display send options", 0, 0, (int (*)(char *, ...))send_help, 0, 0 },
322 { "help", NULL, 0, 0, (int (*)(char *, ...))send_help, 0, 0 },
323 { "do", NULL, 0, 1, (int (*)(char *, ...))send_docmd, 3, 0 },
324 { "dont", NULL, 0, 1, (int (*)(char *, ...))send_dontcmd, 3, 0 },
325 { "will", NULL, 0, 1, (int (*)(char *, ...))send_willcmd, 3, 0 },
326 { "wont", NULL, 0, 1, (int (*)(char *, ...))send_wontcmd, 3, 0 },
327 { NULL, NULL, 0, 0, NULL, 0, 0 }
330 #define GETSEND(name) ((struct sendlist *) genget(name, (char **) Sendlist, \
331 sizeof(struct sendlist)))
334 sendcmd(int argc, char *argv[])
336 int count; /* how many bytes we are going to need to send */
338 struct sendlist *s; /* pointer to current command */
343 printf("need at least one argument for 'send' command\n");
344 printf("'send ?' for help\n");
348 * First, validate all the send arguments.
349 * In addition, we see how much space we are going to need, and
350 * whether or not we will be doing a "SYNCH" operation (which
351 * flushes the network queue).
354 for (i = 1; i < argc; i++) {
355 s = GETSEND(argv[i]);
357 printf("Unknown send argument '%s'\n'send ?' for help.\n",
360 } else if (Ambiguous((void *)s)) {
361 printf("Ambiguous send argument '%s'\n'send ?' for help.\n",
365 if (i + s->narg >= argc) {
367 "Need %d argument%s to 'send %s' command. 'send %s ?' for help.\n",
368 s->narg, s->narg == 1 ? "" : "s", s->name, s->name);
372 if ((void *)s->handler == (void *)send_help) {
378 needconnect += s->needconnect;
380 if (!connected && needconnect) {
381 printf("?Need to be connected first.\n");
382 printf("'send ?' for help\n");
385 /* Now, do we have enough room? */
386 if (NETROOM() < count) {
387 printf("There is not enough room in the buffer TO the network\n");
388 printf("to process your request. Nothing will be done.\n");
389 printf("('send synch' will throw away most data in the network\n");
390 printf("buffer, if this might help.)\n");
393 /* OK, they are all OK, now go through again and actually send */
395 for (i = 1; i < argc; i++) {
396 if ((s = GETSEND(argv[i])) == 0) {
397 fprintf(stderr, "Telnet 'send' error - argument disappeared!\n");
403 success += (*s->handler)((s->narg > 0) ? argv[i+1] : 0,
404 (s->narg > 1) ? argv[i+2] : 0);
407 NET2ADD(IAC, s->what);
408 printoption("SENT", IAC, s->what);
411 return (count == success);
422 send_docmd(char *name)
424 return(send_tncmd(send_do, "do", name));
431 return(send_tncmd(send_dont, "dont", name));
435 send_willcmd(char *name)
437 return(send_tncmd(send_will, "will", name));
441 send_wontcmd(char *name)
443 return(send_tncmd(send_wont, "wont", name));
447 send_tncmd(void (*func)(int, int), const char *cmd, char *name)
450 extern char *telopts[];
453 if (isprefix(name, "help") || isprefix(name, "?")) {
456 printf("usage: send %s <value|option>\n", cmd);
457 printf("\"value\" must be from 0 to 255\n");
458 printf("Valid options are:\n\t");
461 for (cpp = telopts; *cpp; cpp++) {
462 len = strlen(*cpp) + 3;
463 if (col + len > 65) {
467 printf(" \"%s\"", *cpp);
473 cpp = (char **)genget(name, telopts, sizeof(char *));
474 if (Ambiguous(cpp)) {
475 fprintf(stderr,"'%s': ambiguous argument ('send %s ?' for help).\n",
484 while (*cp >= '0' && *cp <= '9') {
490 fprintf(stderr, "'%s': unknown argument ('send %s ?' for help).\n",
493 } else if (val < 0 || val > 255) {
494 fprintf(stderr, "'%s': bad value ('send %s ?' for help).\n",
500 printf("?Need to be connected first.\n");
510 struct sendlist *s; /* pointer to current command */
511 for (s = Sendlist; s->name; s++) {
513 printf("%-15s %s\n", s->name, s->help);
519 * The following are the routines and data structures referred
520 * to by the arguments to the "toggle" command.
535 (SetSockOpt(net, SOL_SOCKET, SO_DEBUG, telnet_debug)) < 0) {
536 perror("setsockopt (SO_DEBUG)");
540 if (net > 0 && SetSockOpt(net, SOL_SOCKET, SO_DEBUG, 1) < 0)
541 perror("setsockopt (SO_DEBUG)");
543 printf("Cannot turn off socket debugging\n");
553 printf("Will send carriage returns as telnet <CR><LF>.\n");
555 printf("Will send carriage returns as telnet <CR><NUL>.\n");
565 donebinarytoggle = 1;
570 if (my_want_state_is_will(TELOPT_BINARY) &&
571 my_want_state_is_do(TELOPT_BINARY)) {
573 } else if (my_want_state_is_wont(TELOPT_BINARY) &&
574 my_want_state_is_dont(TELOPT_BINARY)) {
577 val = binmode ? 0 : 1;
581 if (my_want_state_is_will(TELOPT_BINARY) &&
582 my_want_state_is_do(TELOPT_BINARY)) {
583 printf("Already operating in binary mode with remote host.\n");
585 printf("Negotiating binary mode with remote host.\n");
589 if (my_want_state_is_wont(TELOPT_BINARY) &&
590 my_want_state_is_dont(TELOPT_BINARY)) {
591 printf("Already in network ascii mode with remote host.\n");
593 printf("Negotiating network ascii mode with remote host.\n");
603 donebinarytoggle = 1;
606 val = my_want_state_is_do(TELOPT_BINARY) ? 0 : 1;
609 if (my_want_state_is_do(TELOPT_BINARY)) {
610 printf("Already receiving in binary mode.\n");
612 printf("Negotiating binary mode on input.\n");
616 if (my_want_state_is_dont(TELOPT_BINARY)) {
617 printf("Already receiving in network ascii mode.\n");
619 printf("Negotiating network ascii mode on input.\n");
629 donebinarytoggle = 1;
632 val = my_want_state_is_will(TELOPT_BINARY) ? 0 : 1;
635 if (my_want_state_is_will(TELOPT_BINARY)) {
636 printf("Already transmitting in binary mode.\n");
638 printf("Negotiating binary mode on output.\n");
642 if (my_want_state_is_wont(TELOPT_BINARY)) {
643 printf("Already transmitting in network ascii mode.\n");
645 printf("Negotiating network ascii mode on output.\n");
653 const char *name; /* name of toggle */
654 const char *help; /* help message */
655 int (*handler)(int); /* routine to do actual setting */
657 const char *actionexplanation;
660 static struct togglelist Togglelist[] = {
662 "flushing of output when sending interrupt characters",
665 "flush output when sending interrupt characters" },
667 "automatic sending of interrupt characters in urgent mode",
670 "send interrupt characters in urgent mode" },
671 #ifdef AUTHENTICATION
673 "automatic sending of login and/or authentication info",
676 "send login name and/or authentication information" },
678 "Toggle authentication debugging",
681 "print authentication debugging information" },
685 "automatic encryption of data stream",
688 "automatically encrypt output" },
690 "automatic decryption of data stream",
693 "automatically decrypt input" },
695 "Toggle verbose encryption output",
698 "print verbose encryption output" },
700 "Toggle encryption debugging",
703 "print encryption debugging information" },
704 #endif /* ENCRYPTION */
706 "don't read ~/.telnetrc file",
709 "skip reading of ~/.telnetrc file" },
711 "sending and receiving of binary data",
716 "receiving of binary data",
721 "sending of binary data",
726 "sending carriage returns as telnet <CR><LF>",
727 (int (*)(int))togcrlf,
731 "mapping of received carriage returns",
734 "map carriage return on output" },
736 "local recognition of certain control characters",
737 (int (*)(int))lclchars,
739 "recognize certain control characters" },
740 { " ", "", NULL, NULL, NULL }, /* empty line */
743 (int (*)(int))togdebug,
745 "turn on socket level debugging" },
747 "printing of hexadecimal network data (debugging)",
750 "print hexadecimal representation of network traffic" },
752 "output of \"netdata\" to user readable format (debugging)",
755 "print user readable output for \"netdata\"" },
757 "viewing of options processing (debugging)",
760 "show option processing" },
762 "(debugging) toggle printing of hexadecimal terminal data",
765 "print hexadecimal representation of terminal traffic" },
768 (int (*)(int))togglehelp,
771 { NULL, NULL, NULL, NULL, NULL },
774 (int (*)(int))togglehelp,
777 { NULL, NULL, NULL, NULL, NULL }
783 struct togglelist *c;
785 for (c = Togglelist; c->name; c++) {
788 printf("%-15s toggle %s\n", c->name, c->help);
794 printf("%-15s %s\n", "?", "display help information");
799 settogglehelp(int set)
801 struct togglelist *c;
803 for (c = Togglelist; c->name; c++) {
806 printf("%-15s %s %s\n", c->name, set ? "enable" : "disable",
814 #define GETTOGGLE(name) (struct togglelist *) \
815 genget(name, (char **) Togglelist, sizeof(struct togglelist))
818 toggle(int argc, char *argv[])
822 struct togglelist *c;
826 "Need an argument to 'toggle' command. 'toggle ?' for help.\n");
834 if (Ambiguous((void *)c)) {
835 fprintf(stderr, "'%s': ambiguous argument ('toggle ?' for help).\n",
839 fprintf(stderr, "'%s': unknown argument ('toggle ?' for help).\n",
844 *c->variable = !*c->variable; /* invert it */
845 if (c->actionexplanation) {
846 printf("%s %s.\n", *c->variable? "Will" : "Won't",
847 c->actionexplanation);
851 retval &= (*c->handler)(-1);
859 * The following perform the "set" command.
863 struct termio new_tc = { 0, 0, 0, 0, {}, 0, 0 };
867 const char *name; /* name */
868 const char *help; /* help information */
869 void (*handler)(char *);
870 cc_t *charp; /* where it is located at */
873 static struct setlist Setlist[] = {
874 #ifdef KLUDGELINEMODE
875 { "echo", "character to toggle local echoing on/off", NULL, &echoc },
877 { "escape", "character to escape back to telnet command mode", NULL, &escape },
878 { "rlogin", "rlogin escape character", 0, &rlogin },
879 { "tracefile", "file to write trace information to", SetNetTrace, (cc_t *)NetTraceFile},
880 { " ", "", NULL, NULL },
881 { " ", "The following need 'localchars' to be toggled true", NULL, NULL },
882 { "flushoutput", "character to cause an Abort Output", NULL, termFlushCharp },
883 { "interrupt", "character to cause an Interrupt Process", NULL, termIntCharp },
884 { "quit", "character to cause an Abort process", NULL, termQuitCharp },
885 { "eof", "character to cause an EOF ", NULL, termEofCharp },
886 { " ", "", NULL, NULL },
887 { " ", "The following are for local editing in linemode", NULL, NULL },
888 { "erase", "character to use to erase a character", NULL, termEraseCharp },
889 { "kill", "character to use to erase a line", NULL, termKillCharp },
890 { "lnext", "character to use for literal next", NULL, termLiteralNextCharp },
891 { "susp", "character to cause a Suspend Process", NULL, termSuspCharp },
892 { "reprint", "character to use for line reprint", NULL, termRprntCharp },
893 { "worderase", "character to use to erase a word", NULL, termWerasCharp },
894 { "start", "character to use for XON", NULL, termStartCharp },
895 { "stop", "character to use for XOFF", NULL, termStopCharp },
896 { "forw1", "alternate end of line character", NULL, termForw1Charp },
897 { "forw2", "alternate end of line character", NULL, termForw2Charp },
898 { "ayt", "alternate AYT character", NULL, termAytCharp },
899 { "baudrate", "set remote baud rate", DoBaudRate, ComPortBaudRate },
900 { NULL, NULL, NULL, NULL }
903 static struct setlist *
906 return (struct setlist *)
907 genget(name, (char **) Setlist, sizeof(struct setlist));
911 set_escape_char(char *s)
913 if (rlogin != _POSIX_VDISABLE) {
914 rlogin = (s && *s) ? special(s) : _POSIX_VDISABLE;
915 printf("Telnet rlogin escape character is '%s'.\n",
918 escape = (s && *s) ? special(s) : _POSIX_VDISABLE;
919 printf("Telnet escape character is '%s'.\n", control(escape));
924 setcmd(int argc, char *argv[])
928 struct togglelist *c;
930 if (argc < 2 || argc > 3) {
931 printf("Format is 'set Name Value'\n'set ?' for help.\n");
934 if ((argc == 2) && (isprefix(argv[1], "?") || isprefix(argv[1], "help"))) {
935 for (ct = Setlist; ct->name; ct++)
936 printf("%-15s %s\n", ct->name, ct->help);
939 printf("%-15s %s\n", "?", "display help information");
943 ct = getset(argv[1]);
945 c = GETTOGGLE(argv[1]);
947 fprintf(stderr, "'%s': unknown argument ('set ?' for help).\n",
950 } else if (Ambiguous((void *)c)) {
951 fprintf(stderr, "'%s': ambiguous argument ('set ?' for help).\n",
956 if ((argc == 2) || (strcmp("on", argv[2]) == 0))
958 else if (strcmp("off", argv[2]) == 0)
961 printf("Format is 'set togglename [on|off]'\n'set ?' for help.\n");
964 if (c->actionexplanation) {
965 printf("%s %s.\n", *c->variable? "Will" : "Won't",
966 c->actionexplanation);
971 } else if (argc != 3) {
972 printf("Format is 'set Name Value'\n'set ?' for help.\n");
974 } else if (Ambiguous((void *)ct)) {
975 fprintf(stderr, "'%s': ambiguous argument ('set ?' for help).\n",
978 } else if (ct->handler) {
979 (*ct->handler)(argv[2]);
980 printf("%s set to \"%s\".\n", ct->name, (char *)ct->charp);
982 if (strcmp("off", argv[2])) {
983 value = special(argv[2]);
985 value = _POSIX_VDISABLE;
987 *(ct->charp) = (cc_t)value;
988 printf("%s character is '%s'.\n", ct->name, control(*(ct->charp)));
995 unsetcmd(int argc, char *argv[])
998 struct togglelist *c;
1003 "Need an argument to 'unset' command. 'unset ?' for help.\n");
1006 if (isprefix(argv[1], "?") || isprefix(argv[1], "help")) {
1007 for (ct = Setlist; ct->name; ct++)
1008 printf("%-15s %s\n", ct->name, ct->help);
1011 printf("%-15s %s\n", "?", "display help information");
1021 c = GETTOGGLE(name);
1023 fprintf(stderr, "'%s': unknown argument ('unset ?' for help).\n",
1026 } else if (Ambiguous((void *)c)) {
1027 fprintf(stderr, "'%s': ambiguous argument ('unset ?' for help).\n",
1033 if (c->actionexplanation) {
1034 printf("%s %s.\n", *c->variable? "Will" : "Won't",
1035 c->actionexplanation);
1040 } else if (Ambiguous((void *)ct)) {
1041 fprintf(stderr, "'%s': ambiguous argument ('unset ?' for help).\n",
1044 } else if (ct->handler) {
1046 printf("%s reset to \"%s\".\n", ct->name, (char *)ct->charp);
1048 *(ct->charp) = _POSIX_VDISABLE;
1049 printf("%s character is '%s'.\n", ct->name, control(*(ct->charp)));
1056 * The following are the data structures and routines for the
1059 #ifdef KLUDGELINEMODE
1060 extern int kludgelinemode;
1066 send_wont(TELOPT_LINEMODE, 1);
1067 send_dont(TELOPT_SGA, 1);
1068 send_dont(TELOPT_ECHO, 1);
1076 #ifdef KLUDGELINEMODE
1078 send_dont(TELOPT_SGA, 1);
1080 send_will(TELOPT_LINEMODE, 1);
1081 send_dont(TELOPT_ECHO, 1);
1088 #ifdef KLUDGELINEMODE
1090 send_do(TELOPT_SGA, 1);
1093 send_wont(TELOPT_LINEMODE, 1);
1094 send_do(TELOPT_ECHO, 1);
1099 dolmmode(int bit, int on)
1102 extern int linemode;
1104 if (my_want_state_is_wont(TELOPT_LINEMODE)) {
1105 printf("?Need to have LINEMODE option enabled first.\n");
1106 printf("'mode ?' for help.\n");
1111 c = (linemode | bit);
1113 c = (linemode & ~bit);
1121 return dolmmode(bit, 1);
1127 return dolmmode(bit, 0);
1131 const char *name; /* command name */
1132 const char *help; /* help string */
1133 int (*handler)(int);/* routine which executes command */
1134 int needconnect; /* Do we need to be connected to execute? */
1138 static struct modelist ModeList[] = {
1139 { "character", "Disable LINEMODE option", (int (*)(int))docharmode, 1, 0 },
1140 #ifdef KLUDGELINEMODE
1141 { "", "(or disable obsolete line-by-line mode)", NULL, 0, 0 },
1143 { "line", "Enable LINEMODE option", (int (*)(int))dolinemode, 1, 0 },
1144 #ifdef KLUDGELINEMODE
1145 { "", "(or enable obsolete line-by-line mode)", NULL, 0, 0 },
1147 { "", "", NULL, 0, 0 },
1148 { "", "These require the LINEMODE option to be enabled", NULL, 0, 0 },
1149 { "isig", "Enable signal trapping", setmod, 1, MODE_TRAPSIG },
1150 { "+isig", 0, setmod, 1, MODE_TRAPSIG },
1151 { "-isig", "Disable signal trapping", clearmode, 1, MODE_TRAPSIG },
1152 { "edit", "Enable character editing", setmod, 1, MODE_EDIT },
1153 { "+edit", 0, setmod, 1, MODE_EDIT },
1154 { "-edit", "Disable character editing", clearmode, 1, MODE_EDIT },
1155 { "softtabs", "Enable tab expansion", setmod, 1, MODE_SOFT_TAB },
1156 { "+softtabs", 0, setmod, 1, MODE_SOFT_TAB },
1157 { "-softtabs", "Disable character editing", clearmode, 1, MODE_SOFT_TAB },
1158 { "litecho", "Enable literal character echo", setmod, 1, MODE_LIT_ECHO },
1159 { "+litecho", 0, setmod, 1, MODE_LIT_ECHO },
1160 { "-litecho", "Disable literal character echo", clearmode, 1, MODE_LIT_ECHO },
1161 { "help", 0, (int (*)(int))modehelp, 0, 0 },
1162 #ifdef KLUDGELINEMODE
1163 { "kludgeline", 0, (int (*)(int))dokludgemode, 1, 0 },
1165 { "", "", NULL, 0, 0 },
1166 { "?", "Print help information", (int (*)(int))modehelp, 0, 0 },
1167 { NULL, NULL, NULL, 0, 0 },
1174 struct modelist *mt;
1176 printf("format is: 'mode Mode', where 'Mode' is one of:\n\n");
1177 for (mt = ModeList; mt->name; mt++) {
1180 printf("%-15s %s\n", mt->name, mt->help);
1188 #define GETMODECMD(name) (struct modelist *) \
1189 genget(name, (char **) ModeList, sizeof(struct modelist))
1192 modecmd(int argc, char *argv[])
1194 struct modelist *mt;
1197 printf("'mode' command requires an argument\n");
1198 printf("'mode ?' for help.\n");
1199 } else if ((mt = GETMODECMD(argv[1])) == 0) {
1200 fprintf(stderr, "Unknown mode '%s' ('mode ?' for help).\n", argv[1]);
1201 } else if (Ambiguous((void *)mt)) {
1202 fprintf(stderr, "Ambiguous mode '%s' ('mode ?' for help).\n", argv[1]);
1203 } else if (mt->needconnect && !connected) {
1204 printf("?Need to be connected first.\n");
1205 printf("'mode ?' for help.\n");
1206 } else if (mt->handler) {
1207 return (*mt->handler)(mt->arg1);
1213 * The following data structures and routines implement the
1214 * "display" command.
1218 display(int argc, char *argv[])
1220 struct togglelist *tl;
1223 #define dotog(tl) if (tl->variable && tl->actionexplanation) { \
1224 if (*tl->variable) { \
1229 printf(" %s.\n", tl->actionexplanation); \
1232 #define doset(sl) if (sl->name && *sl->name != ' ') { \
1233 if (sl->handler == 0) \
1234 printf("%-15s [%s]\n", sl->name, control(*sl->charp)); \
1236 printf("%-15s \"%s\"\n", sl->name, (char *)sl->charp); \
1240 for (tl = Togglelist; tl->name; tl++) {
1244 for (sl = Setlist; sl->name; sl++) {
1250 for (i = 1; i < argc; i++) {
1251 sl = getset(argv[i]);
1252 tl = GETTOGGLE(argv[i]);
1253 if (Ambiguous((void *)sl) || Ambiguous((void *)tl)) {
1254 printf("?Ambiguous argument '%s'.\n", argv[i]);
1256 } else if (!sl && !tl) {
1257 printf("?Unknown argument '%s'.\n", argv[i]);
1269 /*@*/optionstatus();
1272 #endif /* ENCRYPTION */
1279 * The following are the data structures, and many of the routines,
1280 * relating to command processing.
1284 * Set the escape character.
1287 setescape(int argc, char *argv[])
1293 "Deprecated usage - please use 'set escape%s%s' in the future.\n",
1294 (argc > 2)? " ":"", (argc > 2)? argv[1]: "");
1298 printf("new escape character: ");
1299 (void) fgets(buf, sizeof(buf), stdin);
1304 (void) fflush(stdout);
1312 printf("Deprecated usage - please use 'toggle crmod' in the future.\n");
1313 printf("%s map carriage return on output.\n", crmod ? "Will" : "Won't");
1314 (void) fflush(stdout);
1324 long oldrows, oldcols, newrows, newcols, err_;
1326 err_ = (TerminalWindowSize(&oldrows, &oldcols) == 0) ? 1 : 0;
1327 (void) kill(0, SIGTSTP);
1329 * If we didn't get the window size before the SUSPEND, but we
1330 * can get them now (?), then send the NAWS to make sure that
1331 * we are set up for the right window size.
1333 if (TerminalWindowSize(&newrows, &newcols) && connected &&
1334 (err_ || ((oldrows != newrows) || (oldcols != newcols)))) {
1338 /* reget parameters in case they were changed */
1339 TerminalSaveState();
1342 printf("Suspend is not supported. Try the '!' command instead\n");
1348 shell(int argc, char *argv[] __unused)
1350 long oldrows, oldcols, newrows, newcols, err_;
1354 err_ = (TerminalWindowSize(&oldrows, &oldcols) == 0) ? 1 : 0;
1357 perror("Fork failed\n");
1363 * Fire up the shell in the child.
1365 const char *shellp, *shellname;
1367 shellp = getenv("SHELL");
1370 if ((shellname = strrchr(shellp, '/')) == 0)
1375 execl(shellp, shellname, "-c", &saveline[1], (char *)0);
1377 execl(shellp, shellname, (char *)0);
1382 (void)wait((int *)0); /* Wait for the shell to complete */
1384 if (TerminalWindowSize(&newrows, &newcols) && connected &&
1385 (err_ || ((oldrows != newrows) || (oldcols != newcols)))) {
1394 bye(int argc, char *argv[])
1396 extern int resettermname;
1399 (void) shutdown(net, 2);
1400 printf("Connection closed.\n");
1401 (void) NetClose(net);
1404 #ifdef AUTHENTICATION
1406 auth_encrypt_connect(connected);
1412 if ((argc != 2) || (strcmp(argv[1], "fromquit") != 0)) {
1413 longjmp(toplevel, 1);
1416 return 1; /* Keep lint, etc., happy */
1422 (void) call(bye, "bye", "fromquit", 0);
1429 send_do(TELOPT_LOGOUT, 1);
1442 void (*handler)(int);
1446 static void slc_help(void);
1448 struct slclist SlcList[] = {
1449 { "export", "Use local special character definitions",
1450 (void (*)(int))slc_mode_export, 0 },
1451 { "import", "Use remote special character definitions",
1452 slc_mode_import, 1 },
1453 { "check", "Verify remote special character definitions",
1454 slc_mode_import, 0 },
1455 { "help", NULL, (void (*)(int))slc_help, 0 },
1456 { "?", "Print help information", (void (*)(int))slc_help, 0 },
1457 { NULL, NULL, NULL, 0 },
1465 for (c = SlcList; c->name; c++) {
1468 printf("%-15s %s\n", c->name, c->help);
1475 static struct slclist *
1478 return (struct slclist *)
1479 genget(name, (char **) SlcList, sizeof(struct slclist));
1483 slccmd(int argc, char *argv[])
1489 "Need an argument to 'slc' command. 'slc ?' for help.\n");
1492 c = getslc(argv[1]);
1494 fprintf(stderr, "'%s': unknown argument ('slc ?' for help).\n",
1498 if (Ambiguous((void *)c)) {
1499 fprintf(stderr, "'%s': ambiguous argument ('slc ?' for help).\n",
1503 (*c->handler)(c->arg);
1509 * The ENVIRON command.
1515 void (*handler)(unsigned char *, unsigned char *);
1519 extern struct env_lst *
1520 env_define(const unsigned char *, unsigned char *);
1522 env_undefine(unsigned char *),
1523 env_export(const unsigned char *),
1524 env_unexport(const unsigned char *),
1525 env_send(unsigned char *),
1526 #if defined(OLD_ENVIRON) && defined(ENV_HACK)
1527 env_varval(unsigned char *),
1533 struct envlist EnvList[] = {
1534 { "define", "Define an environment variable",
1535 (void (*)(unsigned char *, unsigned char *))env_define, 2 },
1536 { "undefine", "Undefine an environment variable",
1537 (void (*)(unsigned char *, unsigned char *))env_undefine, 1 },
1538 { "export", "Mark an environment variable for automatic export",
1539 (void (*)(unsigned char *, unsigned char *))env_export, 1 },
1540 { "unexport", "Don't mark an environment variable for automatic export",
1541 (void (*)(unsigned char *, unsigned char *))env_unexport, 1 },
1542 { "send", "Send an environment variable", (void (*)(unsigned char *, unsigned char *))env_send, 1 },
1543 { "list", "List the current environment variables",
1544 (void (*)(unsigned char *, unsigned char *))env_list, 0 },
1545 #if defined(OLD_ENVIRON) && defined(ENV_HACK)
1546 { "varval", "Reverse VAR and VALUE (auto, right, wrong, status)",
1547 (void (*)(unsigned char *, unsigned char *))env_varval, 1 },
1549 { "help", NULL, (void (*)(unsigned char *, unsigned char *))env_help, 0 },
1550 { "?", "Print help information", (void (*)(unsigned char *, unsigned char *))env_help, 0 },
1551 { NULL, NULL, NULL, 0 },
1559 for (c = EnvList; c->name; c++) {
1562 printf("%-15s %s\n", c->name, c->help);
1569 static struct envlist *
1570 getenvcmd(char *name)
1572 return (struct envlist *)
1573 genget(name, (char **) EnvList, sizeof(struct envlist));
1577 env_cmd(int argc, char *argv[])
1583 "Need an argument to 'environ' command. 'environ ?' for help.\n");
1586 c = getenvcmd(argv[1]);
1588 fprintf(stderr, "'%s': unknown argument ('environ ?' for help).\n",
1592 if (Ambiguous((void *)c)) {
1593 fprintf(stderr, "'%s': ambiguous argument ('environ ?' for help).\n",
1597 if (c->narg + 2 != argc) {
1599 "Need %s%d argument%s to 'environ %s' command. 'environ ?' for help.\n",
1600 c->narg < argc + 2 ? "only " : "",
1601 c->narg, c->narg == 1 ? "" : "s", c->name);
1604 (*c->handler)(argv[2], argv[3]);
1609 struct env_lst *next; /* pointer to next structure */
1610 struct env_lst *prev; /* pointer to previous structure */
1611 unsigned char *var; /* pointer to variable name */
1612 unsigned char *value; /* pointer to variable value */
1613 int export; /* 1 -> export with default list of variables */
1614 int welldefined; /* A well defined variable */
1617 struct env_lst envlisthead;
1619 static struct env_lst *
1620 env_find(const unsigned char *var)
1624 for (ep = envlisthead.next; ep; ep = ep->next) {
1625 if (strcmp(ep->var, var) == 0)
1634 extern char **environ;
1638 for (epp = environ; *epp; epp++) {
1639 if ((cp = strchr(*epp, '='))) {
1641 ep = env_define((unsigned char *)*epp,
1642 (unsigned char *)cp+1);
1648 * Special case for DISPLAY variable. If it is ":0.0" or
1649 * "unix:0.0", we have to get rid of "unix" and insert our
1652 if ((ep = env_find("DISPLAY"))
1653 && ((*ep->value == ':')
1654 || (strncmp((char *)ep->value, "unix:", 5) == 0))) {
1656 char *cp2 = strchr((char *)ep->value, ':');
1658 gethostname(hbuf, 256);
1660 cp = (char *)malloc(strlen(hbuf) + strlen(cp2) + 1);
1661 sprintf((char *)cp, "%s%s", hbuf, cp2);
1663 ep->value = (unsigned char *)cp;
1666 * If USER is not defined, but LOGNAME is, then add
1667 * USER with the value from LOGNAME. By default, we
1668 * don't export the USER variable.
1670 if ((env_find("USER") == NULL) && (ep = env_find("LOGNAME"))) {
1671 env_define("USER", ep->value);
1672 env_unexport("USER");
1674 env_export("DISPLAY");
1675 env_export("PRINTER");
1679 env_define(const unsigned char *var, unsigned char *value)
1683 if ((ep = env_find(var))) {
1689 ep = (struct env_lst *)malloc(sizeof(struct env_lst));
1690 ep->next = envlisthead.next;
1691 envlisthead.next = ep;
1692 ep->prev = &envlisthead;
1694 ep->next->prev = ep;
1696 ep->welldefined = opt_welldefined(var);
1698 ep->var = strdup(var);
1699 ep->value = strdup(value);
1704 env_undefine(unsigned char *var)
1708 if ((ep = env_find(var))) {
1709 ep->prev->next = ep->next;
1711 ep->next->prev = ep->prev;
1721 env_export(const unsigned char *var)
1725 if ((ep = env_find(var)))
1730 env_unexport(const unsigned char *var)
1734 if ((ep = env_find(var)))
1739 env_send(unsigned char *var)
1743 if (my_state_is_wont(TELOPT_NEW_ENVIRON)
1745 && my_state_is_wont(TELOPT_OLD_ENVIRON)
1749 "Cannot send '%s': Telnet ENVIRON option not enabled\n",
1755 fprintf(stderr, "Cannot send '%s': variable not defined\n",
1759 env_opt_start_info();
1760 env_opt_add(ep->var);
1769 for (ep = envlisthead.next; ep; ep = ep->next) {
1770 printf("%c %-20s %s\n", ep->export ? '*' : ' ',
1771 ep->var, ep->value);
1776 env_default(int init, int welldefined)
1778 static struct env_lst *nep = NULL;
1785 while ((nep = nep->next)) {
1786 if (nep->export && (nep->welldefined == welldefined))
1794 env_getvalue(const unsigned char *var)
1798 if ((ep = env_find(var)))
1803 #if defined(OLD_ENVIRON) && defined(ENV_HACK)
1805 env_varval(unsigned char *what)
1807 extern int old_env_var, old_env_value, env_auto;
1808 int len = strlen((char *)what);
1813 if (strncasecmp((char *)what, "status", len) == 0) {
1815 printf("%s%s", "VAR and VALUE are/will be ",
1816 "determined automatically\n");
1817 if (old_env_var == OLD_ENV_VAR)
1818 printf("VAR and VALUE set to correct definitions\n");
1820 printf("VAR and VALUE definitions are reversed\n");
1821 } else if (strncasecmp((char *)what, "auto", len) == 0) {
1823 old_env_var = OLD_ENV_VALUE;
1824 old_env_value = OLD_ENV_VAR;
1825 } else if (strncasecmp((char *)what, "right", len) == 0) {
1827 old_env_var = OLD_ENV_VAR;
1828 old_env_value = OLD_ENV_VALUE;
1829 } else if (strncasecmp((char *)what, "wrong", len) == 0) {
1831 old_env_var = OLD_ENV_VALUE;
1832 old_env_value = OLD_ENV_VAR;
1835 printf("Unknown \"varval\" command. (\"auto\", \"right\", \"wrong\", \"status\")\n");
1840 #ifdef AUTHENTICATION
1842 * The AUTHENTICATE command.
1848 int (*handler)(char *);
1853 auth_enable(char *),
1854 auth_disable(char *),
1859 struct authlist AuthList[] = {
1860 { "status", "Display current status of authentication information",
1861 (int (*)(char *))auth_status, 0 },
1862 { "disable", "Disable an authentication type ('auth disable ?' for more)",
1864 { "enable", "Enable an authentication type ('auth enable ?' for more)",
1866 { "help", NULL, (int (*)(char *))auth_help, 0 },
1867 { "?", "Print help information", (int (*)(char *))auth_help, 0 },
1868 { NULL, NULL, NULL, 0 },
1876 for (c = AuthList; c->name; c++) {
1879 printf("%-15s %s\n", c->name, c->help);
1888 auth_cmd(int argc, char *argv[])
1894 "Need an argument to 'auth' command. 'auth ?' for help.\n");
1898 c = (struct authlist *)
1899 genget(argv[1], (char **) AuthList, sizeof(struct authlist));
1901 fprintf(stderr, "'%s': unknown argument ('auth ?' for help).\n",
1905 if (Ambiguous((void *)c)) {
1906 fprintf(stderr, "'%s': ambiguous argument ('auth ?' for help).\n",
1910 if (c->narg + 2 != argc) {
1912 "Need %s%d argument%s to 'auth %s' command. 'auth ?' for help.\n",
1913 c->narg < argc + 2 ? "only " : "",
1914 c->narg, c->narg == 1 ? "" : "s", c->name);
1917 return((*c->handler)(argv[2]));
1923 * The ENCRYPT command.
1926 struct encryptlist {
1929 int (*handler)(char *, char *);
1936 EncryptEnable(char *, char *),
1937 EncryptDisable(char *, char *),
1938 EncryptType(char *, char *),
1939 EncryptStart(char *),
1940 EncryptStartInput(void),
1941 EncryptStartOutput(void),
1942 EncryptStop(char *),
1943 EncryptStopInput(void),
1944 EncryptStopOutput(void),
1945 EncryptStatus(void);
1949 struct encryptlist EncryptList[] = {
1950 { "enable", "Enable encryption. ('encrypt enable ?' for more)",
1951 EncryptEnable, 1, 1, 2 },
1952 { "disable", "Disable encryption. ('encrypt enable ?' for more)",
1953 EncryptDisable, 0, 1, 2 },
1954 { "type", "Set encryption type. ('encrypt type ?' for more)",
1955 EncryptType, 0, 1, 1 },
1956 { "start", "Start encryption. ('encrypt start ?' for more)",
1957 (int (*)(char *, char *))EncryptStart, 1, 0, 1 },
1958 { "stop", "Stop encryption. ('encrypt stop ?' for more)",
1959 (int (*)(char *, char *))EncryptStop, 1, 0, 1 },
1960 { "input", "Start encrypting the input stream",
1961 (int (*)(char *, char *))EncryptStartInput, 1, 0, 0 },
1962 { "-input", "Stop encrypting the input stream",
1963 (int (*)(char *, char *))EncryptStopInput, 1, 0, 0 },
1964 { "output", "Start encrypting the output stream",
1965 (int (*)(char *, char *))EncryptStartOutput, 1, 0, 0 },
1966 { "-output", "Stop encrypting the output stream",
1967 (int (*)(char *, char *))EncryptStopOutput, 1, 0, 0 },
1969 { "status", "Display current status of authentication information",
1970 (int (*)(char *, char *))EncryptStatus, 0, 0, 0 },
1971 { "help", NULL, (int (*)(char *, char *))EncryptHelp, 0, 0, 0 },
1972 { "?", "Print help information", (int (*)(char *, char *))EncryptHelp, 0, 0, 0 },
1973 { NULL, NULL, NULL, 0, 0, 0 },
1979 struct encryptlist *c;
1981 for (c = EncryptList; c->name; c++) {
1984 printf("%-15s %s\n", c->name, c->help);
1993 encrypt_cmd(int argc, char *argv[])
1995 struct encryptlist *c;
1999 "Need an argument to 'encrypt' command. 'encrypt ?' for help.\n");
2003 c = (struct encryptlist *)
2004 genget(argv[1], (char **) EncryptList, sizeof(struct encryptlist));
2006 fprintf(stderr, "'%s': unknown argument ('encrypt ?' for help).\n",
2010 if (Ambiguous((void *)c)) {
2011 fprintf(stderr, "'%s': ambiguous argument ('encrypt ?' for help).\n",
2016 if (argc < c->minarg || argc > c->maxarg) {
2017 if (c->minarg == c->maxarg) {
2018 fprintf(stderr, "Need %s%d argument%s ",
2019 c->minarg < argc ? "only " : "", c->minarg,
2020 c->minarg == 1 ? "" : "s");
2022 fprintf(stderr, "Need %s%d-%d arguments ",
2023 c->maxarg < argc ? "only " : "", c->minarg, c->maxarg);
2025 fprintf(stderr, "to 'encrypt %s' command. 'encrypt ?' for help.\n",
2029 if (c->needconnect && !connected) {
2030 if (!(argc && (isprefix(argv[2], "help") || isprefix(argv[2], "?")))) {
2031 printf("?Need to be connected first.\n");
2035 return ((*c->handler)(argc > 0 ? argv[2] : 0,
2036 argc > 1 ? argv[3] : 0));
2038 #endif /* ENCRYPTION */
2041 * Print status about the connection.
2045 status(int argc, char *argv[])
2048 printf("Connected to %s.\n", hostname);
2049 if ((argc < 2) || strcmp(argv[1], "notmuch")) {
2050 int mode = getconnmode();
2052 if (my_want_state_is_will(TELOPT_LINEMODE)) {
2053 printf("Operating with LINEMODE option\n");
2054 printf("%s line editing\n", (mode&MODE_EDIT) ? "Local" : "No");
2055 printf("%s catching of signals\n",
2056 (mode&MODE_TRAPSIG) ? "Local" : "No");
2058 #ifdef KLUDGELINEMODE
2059 } else if (kludgelinemode && my_want_state_is_dont(TELOPT_SGA)) {
2060 printf("Operating in obsolete linemode\n");
2063 printf("Operating in single character mode\n");
2065 printf("Catching signals locally\n");
2067 printf("%s character echo\n", (mode&MODE_ECHO) ? "Local" : "Remote");
2068 if (my_want_state_is_will(TELOPT_LFLOW))
2069 printf("%s flow control\n", (mode&MODE_FLOW) ? "Local" : "No");
2072 #endif /* ENCRYPTION */
2075 printf("No connection.\n");
2077 printf("Escape character is '%s'.\n", control(escape));
2078 (void) fflush(stdout);
2084 * Function that gets called when SIGINFO is received.
2089 (void) call(status, "status", "notmuch", 0);
2094 sockaddr_ntop(struct sockaddr *sa)
2097 static char addrbuf[INET6_ADDRSTRLEN];
2099 switch (sa->sa_family) {
2101 addr = &((struct sockaddr_in *)sa)->sin_addr;
2104 addr = &((struct sockaddr_un *)sa)->sun_path;
2108 addr = &((struct sockaddr_in6 *)sa)->sin6_addr;
2114 inet_ntop(sa->sa_family, addr, addrbuf, sizeof(addrbuf));
2118 #if defined(IPSEC) && defined(IPSEC_POLICY_IPSEC)
2120 setpolicy(int lnet, struct addrinfo *res, char *policy)
2129 buf = ipsec_set_policy(policy, strlen(policy));
2131 printf("%s\n", ipsec_strerror());
2134 level = res->ai_family == AF_INET ? IPPROTO_IP : IPPROTO_IPV6;
2135 optname = res->ai_family == AF_INET ? IP_IPSEC_POLICY : IPV6_IPSEC_POLICY;
2136 if (setsockopt(lnet, level, optname, buf, ipsec_get_policylen(buf)) < 0){
2137 perror("setsockopt");
2148 * When an Address Family related error happend, check if retry with
2149 * another AF is possible or not.
2150 * Return 1, if retry with another af is OK. Else, return 0.
2153 switch_af(struct addrinfo **aip)
2156 struct addrinfo *ai;
2159 nextaf = (ai->ai_family == AF_INET) ? AF_INET6 : AF_INET;
2162 while (ai != NULL && ai->ai_family != nextaf);
2172 tn(int argc, char *argv[])
2177 int srcroute = 0, result;
2178 char *cmd, *hostp = 0, *portp = 0, *user = 0;
2179 char *src_addr = NULL;
2180 struct addrinfo hints, *res, *res0 = NULL, *src_res, *src_res0 = NULL;
2181 int error = 0, af_error = 0;
2184 printf("?Already connected to %s\n", hostname);
2189 (void) strcpy(line, "open ");
2191 (void) fgets(&line[strlen(line)], sizeof(line) - strlen(line), stdin);
2199 if (strcmp(*argv, "help") == 0 || isprefix(*argv, "?"))
2201 if (strcmp(*argv, "-l") == 0) {
2209 if (strcmp(*argv, "-a") == 0) {
2214 if (strcmp(*argv, "-s") == 0) {
2233 printf("usage: %s [-l user] [-a] [-s src_addr] host-name [port]\n", cmd);
2240 if (src_addr != NULL) {
2241 memset(&hints, 0, sizeof(hints));
2242 hints.ai_family = family;
2243 hints.ai_socktype = SOCK_STREAM;
2244 error = getaddrinfo(src_addr, 0, &hints, &src_res);
2245 if (error == EAI_NONAME) {
2247 error = getaddrinfo(src_addr, 0, &hints, &src_res);
2250 fprintf(stderr, "%s: %s\n", src_addr, gai_strerror(error));
2251 if (error == EAI_SYSTEM)
2252 fprintf(stderr, "%s: %s\n", src_addr, strerror(errno));
2258 if (hostp[0] == '/') {
2259 struct sockaddr_un su;
2261 if (strlen(hostp) >= sizeof(su.sun_path)) {
2262 fprintf(stderr, "hostname too long for unix domain socket: %s",
2267 memset(&su, 0, sizeof su);
2268 su.sun_family = AF_UNIX;
2269 strncpy(su.sun_path, hostp, sizeof su.sun_path);
2270 printf("Trying %s...\n", hostp);
2271 net = socket(PF_UNIX, SOCK_STREAM, 0);
2276 if (connect(net, (struct sockaddr *)&su, sizeof su) == -1) {
2277 perror(su.sun_path);
2278 (void) NetClose(net);
2282 } else if (hostp[0] == '@' || hostp[0] == '!') {
2285 family == AF_INET6 ||
2287 (hostname = strrchr(hostp, ':')) == NULL)
2288 hostname = strrchr(hostp, '@');
2289 if (hostname == NULL) {
2299 portp = strdup("telnet");
2300 } else if (*portp == '-') {
2303 } else if (*portp == '+') {
2309 memset(&hints, 0, sizeof(hints));
2310 hints.ai_flags = AI_NUMERICHOST;
2311 hints.ai_family = family;
2312 hints.ai_socktype = SOCK_STREAM;
2313 error = getaddrinfo(hostname, portp, &hints, &res);
2315 hints.ai_flags = AI_CANONNAME;
2316 error = getaddrinfo(hostname, portp, &hints, &res);
2319 fprintf(stderr, "%s: %s\n", hostname, gai_strerror(error));
2320 if (error == EAI_SYSTEM)
2321 fprintf(stderr, "%s: %s\n", hostname, strerror(errno));
2325 if (hints.ai_flags == AI_NUMERICHOST) {
2326 /* hostname has numeric */
2330 gni_err = getnameinfo(res->ai_addr, res->ai_addr->sa_len,
2331 _hostname, sizeof(_hostname) - 1, NULL, 0,
2334 (void) strncpy(_hostname, hostp, sizeof(_hostname) - 1);
2335 _hostname[sizeof(_hostname)-1] = '\0';
2336 hostname = _hostname;
2338 /* hostname has FQDN */
2340 (void) strncpy(_hostname, hostname, sizeof(_hostname) - 1);
2341 else if (res->ai_canonname != NULL)
2342 strcpy(_hostname, res->ai_canonname);
2344 (void) strncpy(_hostname, hostp, sizeof(_hostname) - 1);
2345 _hostname[sizeof(_hostname)-1] = '\0';
2346 hostname = _hostname;
2352 if (srcroute != 0) {
2353 static char hostbuf[BUFSIZ];
2355 if (af_error == 0) { /* save intermediate hostnames for retry */
2356 strncpy(hostbuf, hostp, BUFSIZ - 1);
2357 hostbuf[BUFSIZ - 1] = '\0';
2361 result = sourceroute(res, hostp, &srp, &srlen, &proto, &opt);
2364 if (family == AF_UNSPEC && af_error == 0 &&
2365 switch_af(&res) == 1) {
2372 } else if (result == -1) {
2373 printf("Bad source route option: %s\n", hostp);
2379 printf("Trying %s...\n", sockaddr_ntop(res->ai_addr));
2380 net = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
2384 if (family == AF_UNSPEC && af_error == 0 &&
2385 switch_af(&res) == 1) {
2390 perror("telnet: socket");
2393 if (srp && setsockopt(net, proto, opt, (char *)srp, srlen) < 0)
2394 perror("setsockopt (source route)");
2395 #if defined(IPPROTO_IP) && defined(IP_TOS)
2396 if (res->ai_family == PF_INET) {
2397 # if defined(HAS_GETTOS)
2399 if (tos < 0 && (tp = gettosbyname("telnet", "tcp")))
2403 tos = IPTOS_LOWDELAY;
2405 && (setsockopt(net, IPPROTO_IP, IP_TOS,
2406 (char *)&tos, sizeof(int)) < 0)
2407 && (errno != ENOPROTOOPT))
2408 perror("telnet: setsockopt (IP_TOS) (ignored)");
2410 #endif /* defined(IPPROTO_IP) && defined(IP_TOS) */
2412 if (telnet_debug && SetSockOpt(net, SOL_SOCKET, SO_DEBUG, 1) < 0) {
2413 perror("setsockopt (SO_DEBUG)");
2416 if (src_addr != NULL) {
2417 for (src_res = src_res0; src_res != 0; src_res = src_res->ai_next)
2418 if (src_res->ai_family == res->ai_family)
2420 if (src_res == NULL)
2422 if (bind(net, src_res->ai_addr, src_res->ai_addrlen) == -1) {
2424 if (family == AF_UNSPEC && af_error == 0 &&
2425 switch_af(&res) == 1) {
2427 (void) NetClose(net);
2432 (void) NetClose(net);
2436 #if defined(IPSEC) && defined(IPSEC_POLICY_IPSEC)
2437 if (setpolicy(net, res, ipsec_policy_in) < 0) {
2438 (void) NetClose(net);
2441 if (setpolicy(net, res, ipsec_policy_out) < 0) {
2442 (void) NetClose(net);
2447 if (connect(net, res->ai_addr, res->ai_addrlen) < 0) {
2448 struct addrinfo *next;
2450 next = res->ai_next;
2451 /* If already an af failed, only try same af. */
2453 while (next != NULL && next->ai_family != res->ai_family)
2454 next = next->ai_next;
2455 warn("connect to address %s", sockaddr_ntop(res->ai_addr));
2458 (void) NetClose(net);
2461 warnx("Unable to connect to remote host");
2462 (void) NetClose(net);
2466 #ifdef AUTHENTICATION
2468 auth_encrypt_connect(connected);
2471 } while (connected == 0);
2473 if (src_res0 != NULL)
2474 freeaddrinfo(src_res0);
2475 cmdrc(hostp, hostname);
2478 if (autologin && user == NULL) {
2481 user = getenv("USER");
2483 ((pw = getpwnam(user)) && pw->pw_uid != getuid())) {
2484 if ((pw = getpwuid(getuid())))
2491 env_define("USER", user);
2494 (void) call(status, "status", "notmuch", 0);
2496 (void) NetClose(net);
2497 ExitString("Connection closed by foreign host.\n",1);
2502 if (src_res0 != NULL)
2503 freeaddrinfo(src_res0);
2507 #define HELPINDENT (sizeof ("connect"))
2510 openhelp[] = "connect to a site",
2511 closehelp[] = "close current connection",
2512 logouthelp[] = "forcibly logout remote user and close the connection",
2513 quithelp[] = "exit telnet",
2514 statushelp[] = "print status information",
2515 helphelp[] = "print help information",
2516 sendhelp[] = "transmit special characters ('send ?' for more)",
2517 sethelp[] = "set operating parameters ('set ?' for more)",
2518 unsethelp[] = "unset operating parameters ('unset ?' for more)",
2519 togglestring[] ="toggle operating parameters ('toggle ?' for more)",
2520 slchelp[] = "change state of special charaters ('slc ?' for more)",
2521 displayhelp[] = "display operating parameters",
2522 #ifdef AUTHENTICATION
2523 authhelp[] = "turn on (off) authentication ('auth ?' for more)",
2526 encrypthelp[] = "turn on (off) encryption ('encrypt ?' for more)",
2527 #endif /* ENCRYPTION */
2528 zhelp[] = "suspend telnet",
2530 opiehelp[] = "compute response to OPIE challenge",
2532 shellhelp[] = "invoke a subshell",
2533 envhelp[] = "change environment variables ('environ ?' for more)",
2534 modestring[] = "try to enter line or character mode ('mode ?' for more)";
2536 static Command cmdtab[] = {
2537 { "close", closehelp, bye, 1 },
2538 { "logout", logouthelp, (int (*)(int, char **))logout, 1 },
2539 { "display", displayhelp, display, 0 },
2540 { "mode", modestring, modecmd, 0 },
2541 { "telnet", openhelp, tn, 0 },
2542 { "open", openhelp, tn, 0 },
2543 { "quit", quithelp, (int (*)(int, char **))quit, 0 },
2544 { "send", sendhelp, sendcmd, 0 },
2545 { "set", sethelp, setcmd, 0 },
2546 { "unset", unsethelp, unsetcmd, 0 },
2547 { "status", statushelp, status, 0 },
2548 { "toggle", togglestring, toggle, 0 },
2549 { "slc", slchelp, slccmd, 0 },
2550 #ifdef AUTHENTICATION
2551 { "auth", authhelp, auth_cmd, 0 },
2554 { "encrypt", encrypthelp, encrypt_cmd, 0 },
2555 #endif /* ENCRYPTION */
2556 { "z", zhelp, (int (*)(int, char **))suspend, 0 },
2557 { "!", shellhelp, shell, 1 },
2558 { "environ", envhelp, env_cmd, 0 },
2559 { "?", helphelp, help, 0 },
2561 { "opie", opiehelp, opie_calc, 0 },
2563 { NULL, NULL, NULL, 0 }
2566 static char crmodhelp[] = "deprecated command -- use 'toggle crmod' instead";
2567 static char escapehelp[] = "deprecated command -- use 'set escape' instead";
2569 static Command cmdtab2[] = {
2570 { "help", 0, help, 0 },
2571 { "escape", escapehelp, setescape, 0 },
2572 { "crmod", crmodhelp, (int (*)(int, char **))togcrmod, 0 },
2573 { NULL, NULL, NULL, 0 }
2578 * Call routine with argc, argv set from args (terminated by 0).
2582 call(intrtn_t routine, ...)
2588 va_start(ap, routine);
2589 while ((args[argno++] = va_arg(ap, char *)) != 0);
2591 return (*routine)(argno-1, args);
2600 if ((cm = (Command *) genget(name, (char **) cmdtab, sizeof(Command))))
2602 return (Command *) genget(name, (char **) cmdtab2, sizeof(Command));
2606 command(int top, const char *tbuf, int cnt)
2614 (void) signal(SIGINT, SIG_DFL);
2615 (void) signal(SIGQUIT, SIG_DFL);
2618 if (rlogin == _POSIX_VDISABLE)
2619 printf("%s> ", prompt);
2623 while (cnt > 0 && (*cp++ = *tbuf++) != '\n')
2626 if (cp == line || *--cp != '\n' || cp == line)
2629 if (rlogin == _POSIX_VDISABLE)
2630 printf("%s\n", line);
2633 if (rlogin != _POSIX_VDISABLE)
2634 printf("%s> ", prompt);
2635 if (fgets(line, sizeof(line), stdin) == NULL) {
2636 if (feof(stdin) || ferror(stdin)) {
2646 if (margv[0] == 0) {
2649 c = getcmd(margv[0]);
2650 if (Ambiguous((void *)c)) {
2651 printf("?Ambiguous command\n");
2655 printf("?Invalid command\n");
2658 if (c->needconnect && !connected) {
2659 printf("?Need to be connected first.\n");
2662 if ((*c->handler)(margc, margv)) {
2668 longjmp(toplevel, 1);
2679 help(int argc, char *argv[])
2684 printf("Commands may be abbreviated. Commands are:\n\n");
2685 for (c = cmdtab; c->name; c++)
2687 printf("%-*s\t%s\n", (int)HELPINDENT, c->name,
2692 else while (--argc > 0) {
2696 if (Ambiguous((void *)c))
2697 printf("?Ambiguous help command %s\n", arg);
2698 else if (c == (Command *)0)
2699 printf("?Invalid help command %s\n", arg);
2701 printf("%s\n", c->help);
2706 static char *rcname = 0;
2707 static char rcbuf[128];
2710 cmdrc(char *m1, char *m2)
2715 int l1 = strlen(m1);
2716 int l2 = strlen(m2);
2717 char m1save[MAXHOSTNAMELEN];
2722 strlcpy(m1save, m1, sizeof(m1save));
2726 rcname = getenv("HOME");
2727 if (rcname && (strlen(rcname) + 10) < sizeof(rcbuf))
2728 strcpy(rcbuf, rcname);
2731 strcat(rcbuf, "/.telnetrc");
2735 if ((rcfile = fopen(rcname, "r")) == 0) {
2740 if (fgets(line, sizeof(line), rcfile) == NULL)
2747 if (!isspace(line[0]))
2750 if (gotmachine == 0) {
2751 if (isspace(line[0]))
2753 if (strncasecmp(line, m1, l1) == 0)
2754 strncpy(line, &line[l1], sizeof(line) - l1);
2755 else if (strncasecmp(line, m2, l2) == 0)
2756 strncpy(line, &line[l2], sizeof(line) - l2);
2757 else if (strncasecmp(line, "DEFAULT", 7) == 0)
2758 strncpy(line, &line[7], sizeof(line) - 7);
2761 if (line[0] != ' ' && line[0] != '\t' && line[0] != '\n')
2768 c = getcmd(margv[0]);
2769 if (Ambiguous((void *)c)) {
2770 printf("?Ambiguous command: %s\n", margv[0]);
2774 printf("?Invalid command: %s\n", margv[0]);
2778 * This should never happen...
2780 if (c->needconnect && !connected) {
2781 printf("?Need to be connected first for %s.\n", margv[0]);
2784 (*c->handler)(margc, margv);
2790 * Source route is handed in as
2791 * [!]@hop1@hop2...[@|:]dst
2792 * If the leading ! is present, it is a
2793 * strict source route, otherwise it is
2794 * assmed to be a loose source route.
2796 * We fill in the source route option as
2797 * hop1,hop2,hop3...dest
2798 * and return a pointer to hop1, which will
2799 * be the address to connect() to.
2803 * res: ponter to addrinfo structure which contains sockaddr to
2804 * the host to connect to.
2806 * arg: pointer to route list to decipher
2808 * cpp: If *cpp is not equal to NULL, this is a
2809 * pointer to a pointer to a character array
2810 * that should be filled in with the option.
2812 * lenp: pointer to an integer that contains the
2813 * length of *cpp if *cpp != NULL.
2815 * protop: pointer to an integer that should be filled in with
2816 * appropriate protocol for setsockopt, as socket
2819 * optp: pointer to an integer that should be filled in with
2820 * appropriate option for setsockopt, as socket protocol
2825 * If the return value is 1, then all operations are
2826 * successful. If the
2827 * return value is -1, there was a syntax error in the
2828 * option, either unknown characters, or too many hosts.
2829 * If the return value is 0, one of the hostnames in the
2830 * path is unknown, and *cpp is set to point to the bad
2833 * *cpp: If *cpp was equal to NULL, it will be filled
2834 * in with a pointer to our static area that has
2835 * the option filled in. This will be 32bit aligned.
2837 * *lenp: This will be filled in with how long the option
2838 * pointed to by *cpp is.
2840 * *protop: This will be filled in with appropriate protocol for
2841 * setsockopt, as socket protocol family.
2843 * *optp: This will be filled in with appropriate option for
2844 * setsockopt, as socket protocol family.
2847 sourceroute(struct addrinfo *ai, char *arg, char **cpp, int *lenp, int *protop, int *optp)
2849 static char buf[1024 + ALIGNBYTES]; /*XXX*/
2850 char *cp, *cp2, *lsrp, *ep;
2851 struct sockaddr_in *_sin;
2853 struct sockaddr_in6 *sin6;
2854 struct ip6_rthdr *rth;
2856 struct addrinfo hints, *res;
2861 * Verify the arguments, and make sure we have
2862 * at least 7 bytes for the option.
2864 if (cpp == NULL || lenp == NULL)
2867 switch (res->ai_family) {
2874 if (*lenp < (int)CMSG_SPACE(sizeof(struct ip6_rthdr) +
2875 sizeof(struct in6_addr)))
2882 * Decide whether we have a buffer passed to us,
2883 * or if we need to use our own static buffer.
2889 *cpp = lsrp = (char *)ALIGN(buf);
2896 if (ai->ai_family == AF_INET6) {
2897 if ((rth = inet6_rth_init((void *)*cpp, sizeof(buf),
2898 IPV6_RTHDR_TYPE_0, 0)) == NULL)
2902 *protop = IPPROTO_IPV6;
2908 * Next, decide whether we have a loose source
2909 * route or a strict source route, and fill in
2910 * the begining of the option.
2914 *lsrp++ = IPOPT_SSRR;
2916 *lsrp++ = IPOPT_LSRR;
2921 lsrp++; /* skip over length, we'll fill it in later */
2923 *protop = IPPROTO_IP;
2928 memset(&hints, 0, sizeof(hints));
2929 hints.ai_family = ai->ai_family;
2930 hints.ai_socktype = SOCK_STREAM;
2934 ai->ai_family != AF_INET6 &&
2938 else for (cp2 = cp; (c = *cp2); cp2++) {
2943 } else if (c == '@') {
2947 ai->ai_family != AF_INET6 &&
2958 hints.ai_flags = AI_NUMERICHOST;
2959 error = getaddrinfo(cp, NULL, &hints, &res);
2960 if (error == EAI_NONAME) {
2962 error = getaddrinfo(cp, NULL, &hints, &res);
2965 fprintf(stderr, "%s: %s\n", cp, gai_strerror(error));
2966 if (error == EAI_SYSTEM)
2967 fprintf(stderr, "%s: %s\n", cp,
2973 if (res->ai_family == AF_INET6) {
2974 sin6 = (struct sockaddr_in6 *)res->ai_addr;
2975 if (inet6_rth_add((void *)rth, &sin6->sin6_addr) == -1)
2980 _sin = (struct sockaddr_in *)res->ai_addr;
2981 memcpy(lsrp, (char *)&_sin->sin_addr, 4);
2989 * Check to make sure there is space for next address
2996 if (res->ai_family == AF_INET6) {
2997 rth->ip6r_len = rth->ip6r_segleft * 2;
2998 *lenp = (rth->ip6r_len + 1) << 3;
3002 if ((*(*cpp+IPOPT_OLEN) = lsrp - *cpp) <= 7) {
3007 *lsrp++ = IPOPT_NOP; /* 32 bit word align it */
3008 *lenp = lsrp - *cpp;