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. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 static const char sccsid[] = "@(#)commands.c 8.4 (Berkeley) 5/30/95";
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
38 #include <sys/param.h>
41 #include <sys/socket.h>
42 #include <netinet/in.h>
56 #include <arpa/telnet.h>
57 #include <arpa/inet.h>
69 #include <libtelnet/auth.h>
72 #include <libtelnet/encrypt.h>
75 #include <netinet/in_systm.h>
76 #include <netinet/ip.h>
77 #include <netinet/ip6.h>
79 #ifndef MAXHOSTNAMELEN
80 #define MAXHOSTNAMELEN 256
83 typedef int (*intrtn_t)(int, char **);
86 extern int auth_togdebug(int);
89 extern int EncryptAutoEnc(int);
90 extern int EncryptAutoDec(int);
91 extern int EncryptDebug(int);
92 extern int EncryptVerbose(int);
93 #endif /* ENCRYPTION */
94 #if defined(IPPROTO_IP) && defined(IP_TOS)
96 #endif /* defined(IPPROTO_IP) && defined(IP_TOS) */
99 static char _hostname[MAXHOSTNAMELEN];
101 static int help(int, char **);
102 static int call(intrtn_t, ...);
103 static void cmdrc(char *, char *);
105 static int switch_af(struct addrinfo **);
107 static int togglehelp(void);
108 static int send_tncmd(void (*)(int, int), const char *, char *);
109 static int setmod(int);
110 static int clearmode(int);
111 static int modehelp(void);
112 static int sourceroute(struct addrinfo *, char *, unsigned char **, int *, int *, int *);
115 const char *name; /* command name */
116 const char *help; /* help string (NULL for no help) */
117 int (*handler)(int, char **); /* routine which executes command */
118 int needconnect; /* Do we need to be connected to execute? */
121 static char line[256];
122 static char saveline[256];
124 static char *margv[20];
128 #include <sys/wait.h>
129 #define PATH_OPIEKEY "/usr/bin/opiekey"
131 opie_calc(int argc, char *argv[])
136 printf("%s sequence challenge\n", argv[0]);
142 execv(PATH_OPIEKEY, argv);
148 (void) wait(&status);
149 if (WIFEXITED(status))
150 return (WEXITSTATUS(status));
164 if (*cp == '!') { /* Special case shell escape */
165 strcpy(saveline, line); /* save for shell command */
166 *argp++ = strdup("!"); /* No room in string to get this */
178 for (cp2 = cp; c != '\0'; c = *++cp) {
186 if ((c = *++cp) == '\0')
188 } else if (c == '"') {
191 } else if (c == '\'') {
194 } else if (isspace(c))
208 * Make a character string into a number.
210 * Todo: 1. Could take random integers (12, 0x12, 012, 0b1).
223 c = b | 0x40; /* DEL */
236 * Construct a control character sequence
237 * for a special character.
244 * The only way I could get the Sun 3.5 compiler
246 * if ((unsigned int)c >= 0x80)
247 * was to assign "c" to an unsigned int variable...
250 unsigned int uic = (unsigned int)c;
254 if (c == (cc_t)_POSIX_VDISABLE) {
259 buf[1] = ((c>>6)&07) + '0';
260 buf[2] = ((c>>3)&07) + '0';
261 buf[3] = (c&07) + '0';
263 } else if (uic >= 0x20) {
275 * The following are data structures and routines for
276 * the "send" command.
281 const char *name; /* How user refers to it (case independent) */
282 const char *help; /* Help information (0 ==> no help) */
283 int needconnect; /* Need to be connected */
284 int narg; /* Number of arguments */
285 int (*handler)(char *, ...); /* Routine to perform (for special ops) */
286 int nbyte; /* Number of bytes to send this command */
287 int what; /* Character to be sent (<0 ==> special) */
295 send_dontcmd(char *),
296 send_willcmd(char *),
297 send_wontcmd(char *);
299 static struct sendlist Sendlist[] = {
300 { "ao", "Send Telnet Abort output", 1, 0, NULL, 2, AO },
301 { "ayt", "Send Telnet 'Are You There'", 1, 0, NULL, 2, AYT },
302 { "brk", "Send Telnet Break", 1, 0, NULL, 2, BREAK },
303 { "break", NULL, 1, 0, NULL, 2, BREAK },
304 { "ec", "Send Telnet Erase Character", 1, 0, NULL, 2, EC },
305 { "el", "Send Telnet Erase Line", 1, 0, NULL, 2, EL },
306 { "escape", "Send current escape character",1, 0, (int (*)(char *, ...))send_esc, 1, 0 },
307 { "ga", "Send Telnet 'Go Ahead' sequence", 1, 0, NULL, 2, GA },
308 { "ip", "Send Telnet Interrupt Process",1, 0, NULL, 2, IP },
309 { "intp", NULL, 1, 0, NULL, 2, IP },
310 { "interrupt", NULL, 1, 0, NULL, 2, IP },
311 { "intr", NULL, 1, 0, NULL, 2, IP },
312 { "nop", "Send Telnet 'No operation'", 1, 0, NULL, 2, NOP },
313 { "eor", "Send Telnet 'End of Record'", 1, 0, NULL, 2, EOR },
314 { "abort", "Send Telnet 'Abort Process'", 1, 0, NULL, 2, ABORT },
315 { "susp", "Send Telnet 'Suspend Process'",1, 0, NULL, 2, SUSP },
316 { "eof", "Send Telnet End of File Character", 1, 0, NULL, 2, xEOF },
317 { "synch", "Perform Telnet 'Synch operation'", 1, 0, (int (*)(char *, ...))dosynch, 2, 0 },
318 { "getstatus", "Send request for STATUS", 1, 0, (int (*)(char *, ...))get_status, 6, 0 },
319 { "?", "Display send options", 0, 0, (int (*)(char *, ...))send_help, 0, 0 },
320 { "help", NULL, 0, 0, (int (*)(char *, ...))send_help, 0, 0 },
321 { "do", NULL, 0, 1, (int (*)(char *, ...))send_docmd, 3, 0 },
322 { "dont", NULL, 0, 1, (int (*)(char *, ...))send_dontcmd, 3, 0 },
323 { "will", NULL, 0, 1, (int (*)(char *, ...))send_willcmd, 3, 0 },
324 { "wont", NULL, 0, 1, (int (*)(char *, ...))send_wontcmd, 3, 0 },
325 { NULL, NULL, 0, 0, NULL, 0, 0 }
328 #define GETSEND(name) ((struct sendlist *) genget(name, (char **) Sendlist, \
329 sizeof(struct sendlist)))
332 sendcmd(int argc, char *argv[])
334 int count; /* how many bytes we are going to need to send */
336 struct sendlist *s; /* pointer to current command */
341 printf("need at least one argument for 'send' command\n");
342 printf("'send ?' for help\n");
346 * First, validate all the send arguments.
347 * In addition, we see how much space we are going to need, and
348 * whether or not we will be doing a "SYNCH" operation (which
349 * flushes the network queue).
352 for (i = 1; i < argc; i++) {
353 s = GETSEND(argv[i]);
355 printf("Unknown send argument '%s'\n'send ?' for help.\n",
358 } else if (Ambiguous((void *)s)) {
359 printf("Ambiguous send argument '%s'\n'send ?' for help.\n",
363 if (i + s->narg >= argc) {
365 "Need %d argument%s to 'send %s' command. 'send %s ?' for help.\n",
366 s->narg, s->narg == 1 ? "" : "s", s->name, s->name);
370 if ((void *)s->handler == (void *)send_help) {
376 needconnect += s->needconnect;
378 if (!connected && needconnect) {
379 printf("?Need to be connected first.\n");
380 printf("'send ?' for help\n");
383 /* Now, do we have enough room? */
384 if (NETROOM() < count) {
385 printf("There is not enough room in the buffer TO the network\n");
386 printf("to process your request. Nothing will be done.\n");
387 printf("('send synch' will throw away most data in the network\n");
388 printf("buffer, if this might help.)\n");
391 /* OK, they are all OK, now go through again and actually send */
393 for (i = 1; i < argc; i++) {
394 if ((s = GETSEND(argv[i])) == 0) {
395 fprintf(stderr, "Telnet 'send' error - argument disappeared!\n");
401 success += (*s->handler)((s->narg > 0) ? argv[i+1] : 0,
402 (s->narg > 1) ? argv[i+2] : 0);
405 NET2ADD(IAC, s->what);
406 printoption("SENT", IAC, s->what);
409 return (count == success);
420 send_docmd(char *name)
422 return(send_tncmd(send_do, "do", name));
426 send_dontcmd(char *name)
428 return(send_tncmd(send_dont, "dont", name));
432 send_willcmd(char *name)
434 return(send_tncmd(send_will, "will", name));
438 send_wontcmd(char *name)
440 return(send_tncmd(send_wont, "wont", name));
444 send_tncmd(void (*func)(int, int), const char *cmd, char *name)
447 extern char *telopts[];
450 if (isprefix(name, "help") || isprefix(name, "?")) {
453 printf("usage: send %s <value|option>\n", cmd);
454 printf("\"value\" must be from 0 to 255\n");
455 printf("Valid options are:\n\t");
458 for (cpp = telopts; *cpp; cpp++) {
459 len = strlen(*cpp) + 3;
460 if (col + len > 65) {
464 printf(" \"%s\"", *cpp);
470 cpp = (char **)genget(name, telopts, sizeof(char *));
471 if (Ambiguous(cpp)) {
472 fprintf(stderr,"'%s': ambiguous argument ('send %s ?' for help).\n",
481 while (*cp >= '0' && *cp <= '9') {
487 fprintf(stderr, "'%s': unknown argument ('send %s ?' for help).\n",
490 } else if (val < 0 || val > 255) {
491 fprintf(stderr, "'%s': bad value ('send %s ?' for help).\n",
497 printf("?Need to be connected first.\n");
507 struct sendlist *s; /* pointer to current command */
508 for (s = Sendlist; s->name; s++) {
510 printf("%-15s %s\n", s->name, s->help);
516 * The following are the routines and data structures referred
517 * to by the arguments to the "toggle" command.
532 (SetSockOpt(net, SOL_SOCKET, SO_DEBUG, telnet_debug)) < 0) {
533 perror("setsockopt (SO_DEBUG)");
537 if (net > 0 && SetSockOpt(net, SOL_SOCKET, SO_DEBUG, 1) < 0)
538 perror("setsockopt (SO_DEBUG)");
540 printf("Cannot turn off socket debugging\n");
550 printf("Will send carriage returns as telnet <CR><LF>.\n");
552 printf("Will send carriage returns as telnet <CR><NUL>.\n");
562 donebinarytoggle = 1;
567 if (my_want_state_is_will(TELOPT_BINARY) &&
568 my_want_state_is_do(TELOPT_BINARY)) {
570 } else if (my_want_state_is_wont(TELOPT_BINARY) &&
571 my_want_state_is_dont(TELOPT_BINARY)) {
574 val = binmode ? 0 : 1;
578 if (my_want_state_is_will(TELOPT_BINARY) &&
579 my_want_state_is_do(TELOPT_BINARY)) {
580 printf("Already operating in binary mode with remote host.\n");
582 printf("Negotiating binary mode with remote host.\n");
586 if (my_want_state_is_wont(TELOPT_BINARY) &&
587 my_want_state_is_dont(TELOPT_BINARY)) {
588 printf("Already in network ascii mode with remote host.\n");
590 printf("Negotiating network ascii mode with remote host.\n");
600 donebinarytoggle = 1;
603 val = my_want_state_is_do(TELOPT_BINARY) ? 0 : 1;
606 if (my_want_state_is_do(TELOPT_BINARY)) {
607 printf("Already receiving in binary mode.\n");
609 printf("Negotiating binary mode on input.\n");
613 if (my_want_state_is_dont(TELOPT_BINARY)) {
614 printf("Already receiving in network ascii mode.\n");
616 printf("Negotiating network ascii mode on input.\n");
626 donebinarytoggle = 1;
629 val = my_want_state_is_will(TELOPT_BINARY) ? 0 : 1;
632 if (my_want_state_is_will(TELOPT_BINARY)) {
633 printf("Already transmitting in binary mode.\n");
635 printf("Negotiating binary mode on output.\n");
639 if (my_want_state_is_wont(TELOPT_BINARY)) {
640 printf("Already transmitting in network ascii mode.\n");
642 printf("Negotiating network ascii mode on output.\n");
650 const char *name; /* name of toggle */
651 const char *help; /* help message */
652 int (*handler)(int); /* routine to do actual setting */
654 const char *actionexplanation;
657 static struct togglelist Togglelist[] = {
659 "flushing of output when sending interrupt characters",
662 "flush output when sending interrupt characters" },
664 "automatic sending of interrupt characters in urgent mode",
667 "send interrupt characters in urgent mode" },
668 #ifdef AUTHENTICATION
670 "automatic sending of login and/or authentication info",
673 "send login name and/or authentication information" },
675 "Toggle authentication debugging",
678 "print authentication debugging information" },
682 "automatic encryption of data stream",
685 "automatically encrypt output" },
687 "automatic decryption of data stream",
690 "automatically decrypt input" },
692 "Toggle verbose encryption output",
695 "print verbose encryption output" },
697 "Toggle encryption debugging",
700 "print encryption debugging information" },
701 #endif /* ENCRYPTION */
703 "don't read ~/.telnetrc file",
706 "skip reading of ~/.telnetrc file" },
708 "sending and receiving of binary data",
713 "receiving of binary data",
718 "sending of binary data",
723 "sending carriage returns as telnet <CR><LF>",
724 (int (*)(int))togcrlf,
728 "mapping of received carriage returns",
731 "map carriage return on output" },
733 "local recognition of certain control characters",
734 (int (*)(int))lclchars,
736 "recognize certain control characters" },
737 { " ", "", NULL, NULL, NULL }, /* empty line */
740 (int (*)(int))togdebug,
742 "turn on socket level debugging" },
744 "printing of hexadecimal network data (debugging)",
747 "print hexadecimal representation of network traffic" },
749 "output of \"netdata\" to user readable format (debugging)",
752 "print user readable output for \"netdata\"" },
754 "viewing of options processing (debugging)",
757 "show option processing" },
759 "(debugging) toggle printing of hexadecimal terminal data",
762 "print hexadecimal representation of terminal traffic" },
765 (int (*)(int))togglehelp,
768 { NULL, NULL, NULL, NULL, NULL },
771 (int (*)(int))togglehelp,
774 { NULL, NULL, NULL, NULL, NULL }
780 struct togglelist *c;
782 for (c = Togglelist; c->name; c++) {
785 printf("%-15s toggle %s\n", c->name, c->help);
791 printf("%-15s %s\n", "?", "display help information");
796 settogglehelp(int set)
798 struct togglelist *c;
800 for (c = Togglelist; c->name; c++) {
803 printf("%-15s %s %s\n", c->name, set ? "enable" : "disable",
811 #define GETTOGGLE(name) (struct togglelist *) \
812 genget(name, (char **) Togglelist, sizeof(struct togglelist))
815 toggle(int argc, char *argv[])
819 struct togglelist *c;
823 "Need an argument to 'toggle' command. 'toggle ?' for help.\n");
831 if (Ambiguous((void *)c)) {
832 fprintf(stderr, "'%s': ambiguous argument ('toggle ?' for help).\n",
836 fprintf(stderr, "'%s': unknown argument ('toggle ?' for help).\n",
841 *c->variable = !*c->variable; /* invert it */
842 if (c->actionexplanation) {
843 printf("%s %s.\n", *c->variable? "Will" : "Won't",
844 c->actionexplanation);
848 retval &= (*c->handler)(-1);
856 * The following perform the "set" command.
860 struct termio new_tc = { 0, 0, 0, 0, {}, 0, 0 };
864 const char *name; /* name */
865 const char *help; /* help information */
866 void (*handler)(char *);
867 cc_t *charp; /* where it is located at */
870 static struct setlist Setlist[] = {
871 #ifdef KLUDGELINEMODE
872 { "echo", "character to toggle local echoing on/off", NULL, &echoc },
874 { "escape", "character to escape back to telnet command mode", NULL, &escape },
875 { "rlogin", "rlogin escape character", 0, &rlogin },
876 { "tracefile", "file to write trace information to", SetNetTrace, (cc_t *)NetTraceFile},
877 { " ", "", NULL, NULL },
878 { " ", "The following need 'localchars' to be toggled true", NULL, NULL },
879 { "flushoutput", "character to cause an Abort Output", NULL, termFlushCharp },
880 { "interrupt", "character to cause an Interrupt Process", NULL, termIntCharp },
881 { "quit", "character to cause an Abort process", NULL, termQuitCharp },
882 { "eof", "character to cause an EOF ", NULL, termEofCharp },
883 { " ", "", NULL, NULL },
884 { " ", "The following are for local editing in linemode", NULL, NULL },
885 { "erase", "character to use to erase a character", NULL, termEraseCharp },
886 { "kill", "character to use to erase a line", NULL, termKillCharp },
887 { "lnext", "character to use for literal next", NULL, termLiteralNextCharp },
888 { "susp", "character to cause a Suspend Process", NULL, termSuspCharp },
889 { "reprint", "character to use for line reprint", NULL, termRprntCharp },
890 { "worderase", "character to use to erase a word", NULL, termWerasCharp },
891 { "start", "character to use for XON", NULL, termStartCharp },
892 { "stop", "character to use for XOFF", NULL, termStopCharp },
893 { "forw1", "alternate end of line character", NULL, termForw1Charp },
894 { "forw2", "alternate end of line character", NULL, termForw2Charp },
895 { "ayt", "alternate AYT character", NULL, termAytCharp },
896 { "baudrate", "set remote baud rate", DoBaudRate, ComPortBaudRate },
897 { NULL, NULL, NULL, NULL }
900 static struct setlist *
903 return (struct setlist *)
904 genget(name, (char **) Setlist, sizeof(struct setlist));
908 set_escape_char(char *s)
910 if (rlogin != _POSIX_VDISABLE) {
911 rlogin = (s && *s) ? special(s) : _POSIX_VDISABLE;
912 printf("Telnet rlogin escape character is '%s'.\n",
915 escape = (s && *s) ? special(s) : _POSIX_VDISABLE;
916 printf("Telnet escape character is '%s'.\n", control(escape));
921 setcmd(int argc, char *argv[])
925 struct togglelist *c;
927 if (argc < 2 || argc > 3) {
928 printf("Format is 'set Name Value'\n'set ?' for help.\n");
931 if ((argc == 2) && (isprefix(argv[1], "?") || isprefix(argv[1], "help"))) {
932 for (ct = Setlist; ct->name; ct++)
933 printf("%-15s %s\n", ct->name, ct->help);
936 printf("%-15s %s\n", "?", "display help information");
940 ct = getset(argv[1]);
941 if (ct == 0 || !(ct->name && ct->name[0] != ' ')) {
942 c = GETTOGGLE(argv[1]);
944 fprintf(stderr, "'%s': unknown argument ('set ?' for help).\n",
947 } else if (Ambiguous((void *)c)) {
948 fprintf(stderr, "'%s': ambiguous argument ('set ?' for help).\n",
953 if ((argc == 2) || (strcmp("on", argv[2]) == 0))
955 else if (strcmp("off", argv[2]) == 0)
958 printf("Format is 'set togglename [on|off]'\n'set ?' for help.\n");
961 if (c->actionexplanation) {
962 printf("%s %s.\n", *c->variable? "Will" : "Won't",
963 c->actionexplanation);
968 } else if (argc != 3) {
969 printf("Format is 'set Name Value'\n'set ?' for help.\n");
971 } else if (Ambiguous((void *)ct)) {
972 fprintf(stderr, "'%s': ambiguous argument ('set ?' for help).\n",
975 } else if (ct->handler) {
976 (*ct->handler)(argv[2]);
977 printf("%s set to \"%s\".\n", ct->name, (char *)ct->charp);
979 if (strcmp("off", argv[2])) {
980 value = special(argv[2]);
982 value = _POSIX_VDISABLE;
984 *(ct->charp) = (cc_t)value;
985 printf("%s character is '%s'.\n", ct->name, control(*(ct->charp)));
992 unsetcmd(int argc, char *argv[])
995 struct togglelist *c;
1000 "Need an argument to 'unset' command. 'unset ?' for help.\n");
1003 if (isprefix(argv[1], "?") || isprefix(argv[1], "help")) {
1004 for (ct = Setlist; ct->name; ct++)
1005 printf("%-15s %s\n", ct->name, ct->help);
1008 printf("%-15s %s\n", "?", "display help information");
1017 if (ct == 0 || !(ct->name && ct->name[0] != ' ')) {
1018 c = GETTOGGLE(name);
1020 fprintf(stderr, "'%s': unknown argument ('unset ?' for help).\n",
1023 } else if (Ambiguous((void *)c)) {
1024 fprintf(stderr, "'%s': ambiguous argument ('unset ?' for help).\n",
1030 if (c->actionexplanation) {
1031 printf("%s %s.\n", *c->variable? "Will" : "Won't",
1032 c->actionexplanation);
1037 } else if (Ambiguous((void *)ct)) {
1038 fprintf(stderr, "'%s': ambiguous argument ('unset ?' for help).\n",
1041 } else if (ct->handler) {
1043 printf("%s reset to \"%s\".\n", ct->name, (char *)ct->charp);
1045 *(ct->charp) = _POSIX_VDISABLE;
1046 printf("%s character is '%s'.\n", ct->name, control(*(ct->charp)));
1053 * The following are the data structures and routines for the
1056 #ifdef KLUDGELINEMODE
1057 extern int kludgelinemode;
1063 send_wont(TELOPT_LINEMODE, 1);
1064 send_dont(TELOPT_SGA, 1);
1065 send_dont(TELOPT_ECHO, 1);
1073 #ifdef KLUDGELINEMODE
1075 send_dont(TELOPT_SGA, 1);
1077 send_will(TELOPT_LINEMODE, 1);
1078 send_dont(TELOPT_ECHO, 1);
1085 #ifdef KLUDGELINEMODE
1087 send_do(TELOPT_SGA, 1);
1090 send_wont(TELOPT_LINEMODE, 1);
1091 send_do(TELOPT_ECHO, 1);
1096 dolmmode(int bit, int on)
1099 extern int linemode;
1101 if (my_want_state_is_wont(TELOPT_LINEMODE)) {
1102 printf("?Need to have LINEMODE option enabled first.\n");
1103 printf("'mode ?' for help.\n");
1108 c = (linemode | bit);
1110 c = (linemode & ~bit);
1118 return dolmmode(bit, 1);
1124 return dolmmode(bit, 0);
1128 const char *name; /* command name */
1129 const char *help; /* help string */
1130 int (*handler)(int);/* routine which executes command */
1131 int needconnect; /* Do we need to be connected to execute? */
1135 static struct modelist ModeList[] = {
1136 { "character", "Disable LINEMODE option", (int (*)(int))docharmode, 1, 0 },
1137 #ifdef KLUDGELINEMODE
1138 { "", "(or disable obsolete line-by-line mode)", NULL, 0, 0 },
1140 { "line", "Enable LINEMODE option", (int (*)(int))dolinemode, 1, 0 },
1141 #ifdef KLUDGELINEMODE
1142 { "", "(or enable obsolete line-by-line mode)", NULL, 0, 0 },
1144 { "", "", NULL, 0, 0 },
1145 { "", "These require the LINEMODE option to be enabled", NULL, 0, 0 },
1146 { "isig", "Enable signal trapping", setmod, 1, MODE_TRAPSIG },
1147 { "+isig", 0, setmod, 1, MODE_TRAPSIG },
1148 { "-isig", "Disable signal trapping", clearmode, 1, MODE_TRAPSIG },
1149 { "edit", "Enable character editing", setmod, 1, MODE_EDIT },
1150 { "+edit", 0, setmod, 1, MODE_EDIT },
1151 { "-edit", "Disable character editing", clearmode, 1, MODE_EDIT },
1152 { "softtabs", "Enable tab expansion", setmod, 1, MODE_SOFT_TAB },
1153 { "+softtabs", 0, setmod, 1, MODE_SOFT_TAB },
1154 { "-softtabs", "Disable character editing", clearmode, 1, MODE_SOFT_TAB },
1155 { "litecho", "Enable literal character echo", setmod, 1, MODE_LIT_ECHO },
1156 { "+litecho", 0, setmod, 1, MODE_LIT_ECHO },
1157 { "-litecho", "Disable literal character echo", clearmode, 1, MODE_LIT_ECHO },
1158 { "help", 0, (int (*)(int))modehelp, 0, 0 },
1159 #ifdef KLUDGELINEMODE
1160 { "kludgeline", 0, (int (*)(int))dokludgemode, 1, 0 },
1162 { "", "", NULL, 0, 0 },
1163 { "?", "Print help information", (int (*)(int))modehelp, 0, 0 },
1164 { NULL, NULL, NULL, 0, 0 },
1171 struct modelist *mt;
1173 printf("format is: 'mode Mode', where 'Mode' is one of:\n\n");
1174 for (mt = ModeList; mt->name; mt++) {
1177 printf("%-15s %s\n", mt->name, mt->help);
1185 #define GETMODECMD(name) (struct modelist *) \
1186 genget(name, (char **) ModeList, sizeof(struct modelist))
1189 modecmd(int argc, char *argv[])
1191 struct modelist *mt;
1194 printf("'mode' command requires an argument\n");
1195 printf("'mode ?' for help.\n");
1196 } else if ((mt = GETMODECMD(argv[1])) == 0) {
1197 fprintf(stderr, "Unknown mode '%s' ('mode ?' for help).\n", argv[1]);
1198 } else if (Ambiguous((void *)mt)) {
1199 fprintf(stderr, "Ambiguous mode '%s' ('mode ?' for help).\n", argv[1]);
1200 } else if (mt->needconnect && !connected) {
1201 printf("?Need to be connected first.\n");
1202 printf("'mode ?' for help.\n");
1203 } else if (mt->handler) {
1204 return (*mt->handler)(mt->arg1);
1210 * The following data structures and routines implement the
1211 * "display" command.
1215 display(int argc, char *argv[])
1217 struct togglelist *tl;
1220 #define dotog(tl) if (tl->variable && tl->actionexplanation) { \
1221 if (*tl->variable) { \
1226 printf(" %s.\n", tl->actionexplanation); \
1229 #define doset(sl) if (sl->name && *sl->name != ' ') { \
1230 if (sl->handler == 0) \
1231 printf("%-15s [%s]\n", sl->name, control(*sl->charp)); \
1233 printf("%-15s \"%s\"\n", sl->name, (char *)sl->charp); \
1237 for (tl = Togglelist; tl->name; tl++) {
1241 for (sl = Setlist; sl->name; sl++) {
1247 for (i = 1; i < argc; i++) {
1248 sl = getset(argv[i]);
1249 tl = GETTOGGLE(argv[i]);
1250 if (Ambiguous((void *)sl) || Ambiguous((void *)tl)) {
1251 printf("?Ambiguous argument '%s'.\n", argv[i]);
1253 } else if (!sl && !tl) {
1254 printf("?Unknown argument '%s'.\n", argv[i]);
1266 /*@*/optionstatus();
1269 #endif /* ENCRYPTION */
1276 * The following are the data structures, and many of the routines,
1277 * relating to command processing.
1281 * Set the escape character.
1284 setescape(int argc, char *argv[])
1290 "Deprecated usage - please use 'set escape%s%s' in the future.\n",
1291 (argc > 2)? " ":"", (argc > 2)? argv[1]: "");
1295 printf("new escape character: ");
1296 (void) fgets(buf, sizeof(buf), stdin);
1301 (void) fflush(stdout);
1309 printf("Deprecated usage - please use 'toggle crmod' in the future.\n");
1310 printf("%s map carriage return on output.\n", crmod ? "Will" : "Won't");
1311 (void) fflush(stdout);
1321 long oldrows, oldcols, newrows, newcols, err_;
1323 err_ = (TerminalWindowSize(&oldrows, &oldcols) == 0) ? 1 : 0;
1324 (void) kill(0, SIGTSTP);
1326 * If we didn't get the window size before the SUSPEND, but we
1327 * can get them now (?), then send the NAWS to make sure that
1328 * we are set up for the right window size.
1330 if (TerminalWindowSize(&newrows, &newcols) && connected &&
1331 (err_ || ((oldrows != newrows) || (oldcols != newcols)))) {
1335 /* reget parameters in case they were changed */
1336 TerminalSaveState();
1339 printf("Suspend is not supported. Try the '!' command instead\n");
1345 shell(int argc, char *argv[] __unused)
1347 long oldrows, oldcols, newrows, newcols, err_;
1351 err_ = (TerminalWindowSize(&oldrows, &oldcols) == 0) ? 1 : 0;
1354 perror("Fork failed\n");
1360 * Fire up the shell in the child.
1362 const char *shellp, *shellname;
1364 shellp = getenv("SHELL");
1367 if ((shellname = strrchr(shellp, '/')) == 0)
1372 execl(shellp, shellname, "-c", &saveline[1], (char *)0);
1374 execl(shellp, shellname, (char *)0);
1379 (void)wait((int *)0); /* Wait for the shell to complete */
1381 if (TerminalWindowSize(&newrows, &newcols) && connected &&
1382 (err_ || ((oldrows != newrows) || (oldcols != newcols)))) {
1391 bye(int argc, char *argv[])
1393 extern int resettermname;
1396 (void) shutdown(net, 2);
1397 printf("Connection closed.\n");
1398 (void) NetClose(net);
1401 #ifdef AUTHENTICATION
1403 auth_encrypt_connect(connected);
1409 if ((argc != 2) || (strcmp(argv[1], "fromquit") != 0)) {
1410 longjmp(toplevel, 1);
1413 return 1; /* Keep lint, etc., happy */
1419 (void) call(bye, "bye", "fromquit", 0);
1426 send_do(TELOPT_LOGOUT, 1);
1439 void (*handler)(int);
1443 static void slc_help(void);
1445 struct slclist SlcList[] = {
1446 { "export", "Use local special character definitions",
1447 (void (*)(int))slc_mode_export, 0 },
1448 { "import", "Use remote special character definitions",
1449 slc_mode_import, 1 },
1450 { "check", "Verify remote special character definitions",
1451 slc_mode_import, 0 },
1452 { "help", NULL, (void (*)(int))slc_help, 0 },
1453 { "?", "Print help information", (void (*)(int))slc_help, 0 },
1454 { NULL, NULL, NULL, 0 },
1462 for (c = SlcList; c->name; c++) {
1465 printf("%-15s %s\n", c->name, c->help);
1472 static struct slclist *
1475 return (struct slclist *)
1476 genget(name, (char **) SlcList, sizeof(struct slclist));
1480 slccmd(int argc, char *argv[])
1486 "Need an argument to 'slc' command. 'slc ?' for help.\n");
1489 c = getslc(argv[1]);
1491 fprintf(stderr, "'%s': unknown argument ('slc ?' for help).\n",
1495 if (Ambiguous((void *)c)) {
1496 fprintf(stderr, "'%s': ambiguous argument ('slc ?' for help).\n",
1500 (*c->handler)(c->arg);
1506 * The ENVIRON command.
1512 void (*handler)(unsigned char *, unsigned char *);
1516 extern struct env_lst *
1517 env_define(const unsigned char *, unsigned char *);
1519 env_undefine(unsigned char *),
1520 env_export(const unsigned char *),
1521 env_unexport(const unsigned char *),
1522 env_send(unsigned char *),
1523 #if defined(OLD_ENVIRON) && defined(ENV_HACK)
1524 env_varval(unsigned char *),
1530 struct envlist EnvList[] = {
1531 { "define", "Define an environment variable",
1532 (void (*)(unsigned char *, unsigned char *))env_define, 2 },
1533 { "undefine", "Undefine an environment variable",
1534 (void (*)(unsigned char *, unsigned char *))env_undefine, 1 },
1535 { "export", "Mark an environment variable for automatic export",
1536 (void (*)(unsigned char *, unsigned char *))env_export, 1 },
1537 { "unexport", "Don't mark an environment variable for automatic export",
1538 (void (*)(unsigned char *, unsigned char *))env_unexport, 1 },
1539 { "send", "Send an environment variable", (void (*)(unsigned char *, unsigned char *))env_send, 1 },
1540 { "list", "List the current environment variables",
1541 (void (*)(unsigned char *, unsigned char *))env_list, 0 },
1542 #if defined(OLD_ENVIRON) && defined(ENV_HACK)
1543 { "varval", "Reverse VAR and VALUE (auto, right, wrong, status)",
1544 (void (*)(unsigned char *, unsigned char *))env_varval, 1 },
1546 { "help", NULL, (void (*)(unsigned char *, unsigned char *))env_help, 0 },
1547 { "?", "Print help information", (void (*)(unsigned char *, unsigned char *))env_help, 0 },
1548 { NULL, NULL, NULL, 0 },
1556 for (c = EnvList; c->name; c++) {
1559 printf("%-15s %s\n", c->name, c->help);
1566 static struct envlist *
1567 getenvcmd(char *name)
1569 return (struct envlist *)
1570 genget(name, (char **) EnvList, sizeof(struct envlist));
1574 env_cmd(int argc, char *argv[])
1580 "Need an argument to 'environ' command. 'environ ?' for help.\n");
1583 c = getenvcmd(argv[1]);
1585 fprintf(stderr, "'%s': unknown argument ('environ ?' for help).\n",
1589 if (Ambiguous((void *)c)) {
1590 fprintf(stderr, "'%s': ambiguous argument ('environ ?' for help).\n",
1594 if (c->narg + 2 != argc) {
1596 "Need %s%d argument%s to 'environ %s' command. 'environ ?' for help.\n",
1597 c->narg < argc + 2 ? "only " : "",
1598 c->narg, c->narg == 1 ? "" : "s", c->name);
1601 (*c->handler)(argv[2], argv[3]);
1606 struct env_lst *next; /* pointer to next structure */
1607 struct env_lst *prev; /* pointer to previous structure */
1608 unsigned char *var; /* pointer to variable name */
1609 unsigned char *value; /* pointer to variable value */
1610 int export; /* 1 -> export with default list of variables */
1611 int welldefined; /* A well defined variable */
1614 struct env_lst envlisthead;
1616 static struct env_lst *
1617 env_find(const unsigned char *var)
1621 for (ep = envlisthead.next; ep; ep = ep->next) {
1622 if (strcmp(ep->var, var) == 0)
1631 extern char **environ;
1635 for (epp = environ; *epp; epp++) {
1636 if ((cp = strchr(*epp, '='))) {
1638 ep = env_define((unsigned char *)*epp,
1639 (unsigned char *)cp+1);
1645 * Special case for DISPLAY variable. If it is ":0.0" or
1646 * "unix:0.0", we have to get rid of "unix" and insert our
1649 if ((ep = env_find("DISPLAY"))
1650 && ((*ep->value == ':')
1651 || (strncmp((char *)ep->value, "unix:", 5) == 0))) {
1653 char *cp2 = strchr((char *)ep->value, ':');
1655 gethostname(hbuf, sizeof(hbuf));
1656 hbuf[sizeof(hbuf)-1] = '\0';
1657 asprintf(&cp, "%s%s", hbuf, cp2);
1660 ep->value = (unsigned char *)cp;
1663 * If USER is not defined, but LOGNAME is, then add
1664 * USER with the value from LOGNAME. By default, we
1665 * don't export the USER variable.
1667 if ((env_find("USER") == NULL) && (ep = env_find("LOGNAME"))) {
1668 env_define("USER", ep->value);
1669 env_unexport("USER");
1671 env_export("DISPLAY");
1672 env_export("PRINTER");
1676 env_define(const unsigned char *var, unsigned char *value)
1680 if ((ep = env_find(var))) {
1686 ep = (struct env_lst *)malloc(sizeof(struct env_lst));
1687 ep->next = envlisthead.next;
1688 envlisthead.next = ep;
1689 ep->prev = &envlisthead;
1691 ep->next->prev = ep;
1693 ep->welldefined = opt_welldefined(var);
1695 ep->var = strdup(var);
1696 ep->value = strdup(value);
1701 env_undefine(unsigned char *var)
1705 if ((ep = env_find(var))) {
1706 ep->prev->next = ep->next;
1708 ep->next->prev = ep->prev;
1718 env_export(const unsigned char *var)
1722 if ((ep = env_find(var)))
1727 env_unexport(const unsigned char *var)
1731 if ((ep = env_find(var)))
1736 env_send(unsigned char *var)
1740 if (my_state_is_wont(TELOPT_NEW_ENVIRON)
1742 && my_state_is_wont(TELOPT_OLD_ENVIRON)
1746 "Cannot send '%s': Telnet ENVIRON option not enabled\n",
1752 fprintf(stderr, "Cannot send '%s': variable not defined\n",
1756 env_opt_start_info();
1757 env_opt_add(ep->var);
1766 for (ep = envlisthead.next; ep; ep = ep->next) {
1767 printf("%c %-20s %s\n", ep->export ? '*' : ' ',
1768 ep->var, ep->value);
1773 env_default(int init, int welldefined)
1775 static struct env_lst *nep = NULL;
1782 while ((nep = nep->next)) {
1783 if (nep->export && (nep->welldefined == welldefined))
1791 env_getvalue(const unsigned char *var)
1795 if ((ep = env_find(var)))
1800 #if defined(OLD_ENVIRON) && defined(ENV_HACK)
1802 env_varval(unsigned char *what)
1804 extern int old_env_var, old_env_value, env_auto;
1805 int len = strlen((char *)what);
1810 if (strncasecmp((char *)what, "status", len) == 0) {
1812 printf("%s%s", "VAR and VALUE are/will be ",
1813 "determined automatically\n");
1814 if (old_env_var == OLD_ENV_VAR)
1815 printf("VAR and VALUE set to correct definitions\n");
1817 printf("VAR and VALUE definitions are reversed\n");
1818 } else if (strncasecmp((char *)what, "auto", len) == 0) {
1820 old_env_var = OLD_ENV_VALUE;
1821 old_env_value = OLD_ENV_VAR;
1822 } else if (strncasecmp((char *)what, "right", len) == 0) {
1824 old_env_var = OLD_ENV_VAR;
1825 old_env_value = OLD_ENV_VALUE;
1826 } else if (strncasecmp((char *)what, "wrong", len) == 0) {
1828 old_env_var = OLD_ENV_VALUE;
1829 old_env_value = OLD_ENV_VAR;
1832 printf("Unknown \"varval\" command. (\"auto\", \"right\", \"wrong\", \"status\")\n");
1837 #ifdef AUTHENTICATION
1839 * The AUTHENTICATE command.
1845 int (*handler)(char *);
1850 auth_enable(char *),
1851 auth_disable(char *),
1856 struct authlist AuthList[] = {
1857 { "status", "Display current status of authentication information",
1858 (int (*)(char *))auth_status, 0 },
1859 { "disable", "Disable an authentication type ('auth disable ?' for more)",
1861 { "enable", "Enable an authentication type ('auth enable ?' for more)",
1863 { "help", NULL, (int (*)(char *))auth_help, 0 },
1864 { "?", "Print help information", (int (*)(char *))auth_help, 0 },
1865 { NULL, NULL, NULL, 0 },
1873 for (c = AuthList; c->name; c++) {
1876 printf("%-15s %s\n", c->name, c->help);
1885 auth_cmd(int argc, char *argv[])
1891 "Need an argument to 'auth' command. 'auth ?' for help.\n");
1895 c = (struct authlist *)
1896 genget(argv[1], (char **) AuthList, sizeof(struct authlist));
1898 fprintf(stderr, "'%s': unknown argument ('auth ?' for help).\n",
1902 if (Ambiguous((void *)c)) {
1903 fprintf(stderr, "'%s': ambiguous argument ('auth ?' for help).\n",
1907 if (c->narg + 2 != argc) {
1909 "Need %s%d argument%s to 'auth %s' command. 'auth ?' for help.\n",
1910 c->narg < argc + 2 ? "only " : "",
1911 c->narg, c->narg == 1 ? "" : "s", c->name);
1914 return((*c->handler)(argv[2]));
1920 * The ENCRYPT command.
1923 struct encryptlist {
1926 int (*handler)(char *, char *);
1933 EncryptEnable(char *, char *),
1934 EncryptDisable(char *, char *),
1935 EncryptType(char *, char *),
1936 EncryptStart(char *),
1937 EncryptStartInput(void),
1938 EncryptStartOutput(void),
1939 EncryptStop(char *),
1940 EncryptStopInput(void),
1941 EncryptStopOutput(void),
1942 EncryptStatus(void);
1946 struct encryptlist EncryptList[] = {
1947 { "enable", "Enable encryption. ('encrypt enable ?' for more)",
1948 EncryptEnable, 1, 1, 2 },
1949 { "disable", "Disable encryption. ('encrypt enable ?' for more)",
1950 EncryptDisable, 0, 1, 2 },
1951 { "type", "Set encryption type. ('encrypt type ?' for more)",
1952 EncryptType, 0, 1, 1 },
1953 { "start", "Start encryption. ('encrypt start ?' for more)",
1954 (int (*)(char *, char *))EncryptStart, 1, 0, 1 },
1955 { "stop", "Stop encryption. ('encrypt stop ?' for more)",
1956 (int (*)(char *, char *))EncryptStop, 1, 0, 1 },
1957 { "input", "Start encrypting the input stream",
1958 (int (*)(char *, char *))EncryptStartInput, 1, 0, 0 },
1959 { "-input", "Stop encrypting the input stream",
1960 (int (*)(char *, char *))EncryptStopInput, 1, 0, 0 },
1961 { "output", "Start encrypting the output stream",
1962 (int (*)(char *, char *))EncryptStartOutput, 1, 0, 0 },
1963 { "-output", "Stop encrypting the output stream",
1964 (int (*)(char *, char *))EncryptStopOutput, 1, 0, 0 },
1966 { "status", "Display current status of authentication information",
1967 (int (*)(char *, char *))EncryptStatus, 0, 0, 0 },
1968 { "help", NULL, (int (*)(char *, char *))EncryptHelp, 0, 0, 0 },
1969 { "?", "Print help information", (int (*)(char *, char *))EncryptHelp, 0, 0, 0 },
1970 { NULL, NULL, NULL, 0, 0, 0 },
1976 struct encryptlist *c;
1978 for (c = EncryptList; c->name; c++) {
1981 printf("%-15s %s\n", c->name, c->help);
1990 encrypt_cmd(int argc, char *argv[])
1992 struct encryptlist *c;
1996 "Need an argument to 'encrypt' command. 'encrypt ?' for help.\n");
2000 c = (struct encryptlist *)
2001 genget(argv[1], (char **) EncryptList, sizeof(struct encryptlist));
2003 fprintf(stderr, "'%s': unknown argument ('encrypt ?' for help).\n",
2007 if (Ambiguous((void *)c)) {
2008 fprintf(stderr, "'%s': ambiguous argument ('encrypt ?' for help).\n",
2013 if (argc < c->minarg || argc > c->maxarg) {
2014 if (c->minarg == c->maxarg) {
2015 fprintf(stderr, "Need %s%d argument%s ",
2016 c->minarg < argc ? "only " : "", c->minarg,
2017 c->minarg == 1 ? "" : "s");
2019 fprintf(stderr, "Need %s%d-%d arguments ",
2020 c->maxarg < argc ? "only " : "", c->minarg, c->maxarg);
2022 fprintf(stderr, "to 'encrypt %s' command. 'encrypt ?' for help.\n",
2026 if (c->needconnect && !connected) {
2027 if (!(argc && (isprefix(argv[2], "help") || isprefix(argv[2], "?")))) {
2028 printf("?Need to be connected first.\n");
2032 return ((*c->handler)(argc > 0 ? argv[2] : 0,
2033 argc > 1 ? argv[3] : 0));
2035 #endif /* ENCRYPTION */
2038 * Print status about the connection.
2042 status(int argc, char *argv[])
2046 printf("Connected to %s.\n", hostname);
2047 if ((argc < 2) || strcmp(argv[1], "notmuch")) {
2048 int mode = getconnmode();
2050 if (my_want_state_is_will(TELOPT_LINEMODE)) {
2051 printf("Operating with LINEMODE option\n");
2052 printf("%s line editing\n", (mode&MODE_EDIT) ? "Local" : "No");
2053 printf("%s catching of signals\n",
2054 (mode&MODE_TRAPSIG) ? "Local" : "No");
2056 #ifdef KLUDGELINEMODE
2057 } else if (kludgelinemode && my_want_state_is_dont(TELOPT_SGA)) {
2058 printf("Operating in obsolete linemode\n");
2061 printf("Operating in single character mode\n");
2063 printf("Catching signals locally\n");
2065 printf("%s character echo\n", (mode&MODE_ECHO) ? "Local" : "Remote");
2066 if (my_want_state_is_will(TELOPT_LFLOW))
2067 printf("%s flow control\n", (mode&MODE_FLOW) ? "Local" : "No");
2070 #endif /* ENCRYPTION */
2073 printf("No connection.\n");
2076 printf("Escape character is '%s'.\n", control(escape));
2077 (void) fflush(stdout);
2083 * Function that gets called when SIGINFO is received.
2088 (void) call(status, "status", "notmuch", 0);
2093 sockaddr_ntop(struct sockaddr *sa)
2096 static char addrbuf[INET6_ADDRSTRLEN];
2098 switch (sa->sa_family) {
2100 addr = &((struct sockaddr_in *)sa)->sin_addr;
2103 addr = &((struct sockaddr_un *)sa)->sun_path;
2107 addr = &((struct sockaddr_in6 *)sa)->sin6_addr;
2113 inet_ntop(sa->sa_family, addr, addrbuf, sizeof(addrbuf));
2117 #if defined(IPSEC) && defined(IPSEC_POLICY_IPSEC)
2119 setpolicy(int lnet, struct addrinfo *res, char *policy)
2128 buf = ipsec_set_policy(policy, strlen(policy));
2130 printf("%s\n", ipsec_strerror());
2133 level = res->ai_family == AF_INET ? IPPROTO_IP : IPPROTO_IPV6;
2134 optname = res->ai_family == AF_INET ? IP_IPSEC_POLICY : IPV6_IPSEC_POLICY;
2135 if (setsockopt(lnet, level, optname, buf, ipsec_get_policylen(buf)) < 0){
2136 perror("setsockopt");
2147 * When an Address Family related error happend, check if retry with
2148 * another AF is possible or not.
2149 * Return 1, if retry with another af is OK. Else, return 0.
2152 switch_af(struct addrinfo **aip)
2155 struct addrinfo *ai;
2158 nextaf = (ai->ai_family == AF_INET) ? AF_INET6 : AF_INET;
2161 while (ai != NULL && ai->ai_family != nextaf);
2171 tn(int argc, char *argv[])
2173 unsigned char *srp = 0;
2176 int srcroute = 0, result;
2177 char *cmd, *hostp = 0, *portp = 0, *user = 0;
2178 char *src_addr = NULL;
2179 struct addrinfo hints, *res, *res0 = NULL, *src_res, *src_res0 = NULL;
2180 int error = 0, af_error = 0;
2183 printf("?Already connected to %s\n", hostname);
2188 (void) strcpy(line, "open ");
2190 (void) fgets(&line[strlen(line)], sizeof(line) - strlen(line), stdin);
2198 if (strcmp(*argv, "help") == 0 || isprefix(*argv, "?"))
2200 if (strcmp(*argv, "-l") == 0) {
2208 if (strcmp(*argv, "-a") == 0) {
2213 if (strcmp(*argv, "-s") == 0) {
2232 printf("usage: %s [-l user] [-a] [-s src_addr] host-name [port]\n", cmd);
2239 if (src_addr != NULL) {
2240 memset(&hints, 0, sizeof(hints));
2241 hints.ai_family = family;
2242 hints.ai_socktype = SOCK_STREAM;
2243 error = getaddrinfo(src_addr, 0, &hints, &src_res);
2244 if (error == EAI_NONAME) {
2246 error = getaddrinfo(src_addr, 0, &hints, &src_res);
2249 fprintf(stderr, "%s: %s\n", src_addr, gai_strerror(error));
2250 if (error == EAI_SYSTEM)
2251 fprintf(stderr, "%s: %s\n", src_addr, strerror(errno));
2257 if (hostp[0] == '/') {
2258 struct sockaddr_un su;
2260 if (strlen(hostp) >= sizeof(su.sun_path)) {
2261 fprintf(stderr, "hostname too long for unix domain socket: %s",
2266 memset(&su, 0, sizeof su);
2267 su.sun_family = AF_UNIX;
2268 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);
2380 printf("Trying %s...\n", sockaddr_ntop(res->ai_addr));
2381 net = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
2385 if (family == AF_UNSPEC && af_error == 0 &&
2386 switch_af(&res) == 1) {
2391 perror("telnet: socket");
2394 if (srp && setsockopt(net, proto, opt, (char *)srp, srlen) < 0)
2395 perror("setsockopt (source route)");
2396 #if defined(IPPROTO_IP) && defined(IP_TOS)
2397 if (res->ai_family == PF_INET) {
2398 # if defined(HAS_GETTOS)
2400 if (tos < 0 && (tp = gettosbyname("telnet", "tcp")))
2404 tos = IPTOS_LOWDELAY;
2406 && (setsockopt(net, IPPROTO_IP, IP_TOS,
2407 (char *)&tos, sizeof(int)) < 0)
2408 && (errno != ENOPROTOOPT))
2409 perror("telnet: setsockopt (IP_TOS) (ignored)");
2411 #endif /* defined(IPPROTO_IP) && defined(IP_TOS) */
2413 if (telnet_debug && SetSockOpt(net, SOL_SOCKET, SO_DEBUG, 1) < 0) {
2414 perror("setsockopt (SO_DEBUG)");
2417 if (src_addr != NULL) {
2418 for (src_res = src_res0; src_res != 0; src_res = src_res->ai_next)
2419 if (src_res->ai_family == res->ai_family)
2421 if (src_res == NULL)
2423 if (bind(net, src_res->ai_addr, src_res->ai_addrlen) == -1) {
2425 if (family == AF_UNSPEC && af_error == 0 &&
2426 switch_af(&res) == 1) {
2428 (void) NetClose(net);
2433 (void) NetClose(net);
2437 #if defined(IPSEC) && defined(IPSEC_POLICY_IPSEC)
2438 if (setpolicy(net, res, ipsec_policy_in) < 0) {
2439 (void) NetClose(net);
2442 if (setpolicy(net, res, ipsec_policy_out) < 0) {
2443 (void) NetClose(net);
2448 if (connect(net, res->ai_addr, res->ai_addrlen) < 0) {
2449 struct addrinfo *next;
2451 next = res->ai_next;
2452 /* If already an af failed, only try same af. */
2454 while (next != NULL && next->ai_family != res->ai_family)
2455 next = next->ai_next;
2456 warn("connect to address %s", sockaddr_ntop(res->ai_addr));
2459 (void) NetClose(net);
2462 warnx("Unable to connect to remote host");
2463 (void) NetClose(net);
2467 #ifdef AUTHENTICATION
2469 auth_encrypt_connect(connected);
2472 } while (connected == 0);
2474 if (src_res0 != NULL)
2475 freeaddrinfo(src_res0);
2476 cmdrc(hostp, hostname);
2479 if (autologin && user == NULL) {
2482 user = getenv("USER");
2484 ((pw = getpwnam(user)) && pw->pw_uid != getuid())) {
2485 if ((pw = getpwuid(getuid())))
2492 env_define("USER", user);
2495 (void) call(status, "status", "notmuch", 0);
2497 (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;