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));
429 return(send_tncmd(send_dont, "dont", name));
433 send_willcmd(char *name)
435 return(send_tncmd(send_will, "will", name));
439 send_wontcmd(char *name)
441 return(send_tncmd(send_wont, "wont", name));
445 send_tncmd(void (*func)(int, int), const char *cmd, char *name)
448 extern char *telopts[];
451 if (isprefix(name, "help") || isprefix(name, "?")) {
454 printf("usage: send %s <value|option>\n", cmd);
455 printf("\"value\" must be from 0 to 255\n");
456 printf("Valid options are:\n\t");
459 for (cpp = telopts; *cpp; cpp++) {
460 len = strlen(*cpp) + 3;
461 if (col + len > 65) {
465 printf(" \"%s\"", *cpp);
471 cpp = (char **)genget(name, telopts, sizeof(char *));
472 if (Ambiguous(cpp)) {
473 fprintf(stderr,"'%s': ambiguous argument ('send %s ?' for help).\n",
482 while (*cp >= '0' && *cp <= '9') {
488 fprintf(stderr, "'%s': unknown argument ('send %s ?' for help).\n",
491 } else if (val < 0 || val > 255) {
492 fprintf(stderr, "'%s': bad value ('send %s ?' for help).\n",
498 printf("?Need to be connected first.\n");
508 struct sendlist *s; /* pointer to current command */
509 for (s = Sendlist; s->name; s++) {
511 printf("%-15s %s\n", s->name, s->help);
517 * The following are the routines and data structures referred
518 * to by the arguments to the "toggle" command.
533 (SetSockOpt(net, SOL_SOCKET, SO_DEBUG, telnet_debug)) < 0) {
534 perror("setsockopt (SO_DEBUG)");
538 if (net > 0 && SetSockOpt(net, SOL_SOCKET, SO_DEBUG, 1) < 0)
539 perror("setsockopt (SO_DEBUG)");
541 printf("Cannot turn off socket debugging\n");
551 printf("Will send carriage returns as telnet <CR><LF>.\n");
553 printf("Will send carriage returns as telnet <CR><NUL>.\n");
563 donebinarytoggle = 1;
568 if (my_want_state_is_will(TELOPT_BINARY) &&
569 my_want_state_is_do(TELOPT_BINARY)) {
571 } else if (my_want_state_is_wont(TELOPT_BINARY) &&
572 my_want_state_is_dont(TELOPT_BINARY)) {
575 val = binmode ? 0 : 1;
579 if (my_want_state_is_will(TELOPT_BINARY) &&
580 my_want_state_is_do(TELOPT_BINARY)) {
581 printf("Already operating in binary mode with remote host.\n");
583 printf("Negotiating binary mode with remote host.\n");
587 if (my_want_state_is_wont(TELOPT_BINARY) &&
588 my_want_state_is_dont(TELOPT_BINARY)) {
589 printf("Already in network ascii mode with remote host.\n");
591 printf("Negotiating network ascii mode with remote host.\n");
601 donebinarytoggle = 1;
604 val = my_want_state_is_do(TELOPT_BINARY) ? 0 : 1;
607 if (my_want_state_is_do(TELOPT_BINARY)) {
608 printf("Already receiving in binary mode.\n");
610 printf("Negotiating binary mode on input.\n");
614 if (my_want_state_is_dont(TELOPT_BINARY)) {
615 printf("Already receiving in network ascii mode.\n");
617 printf("Negotiating network ascii mode on input.\n");
627 donebinarytoggle = 1;
630 val = my_want_state_is_will(TELOPT_BINARY) ? 0 : 1;
633 if (my_want_state_is_will(TELOPT_BINARY)) {
634 printf("Already transmitting in binary mode.\n");
636 printf("Negotiating binary mode on output.\n");
640 if (my_want_state_is_wont(TELOPT_BINARY)) {
641 printf("Already transmitting in network ascii mode.\n");
643 printf("Negotiating network ascii mode on output.\n");
651 const char *name; /* name of toggle */
652 const char *help; /* help message */
653 int (*handler)(int); /* routine to do actual setting */
655 const char *actionexplanation;
658 static struct togglelist Togglelist[] = {
660 "flushing of output when sending interrupt characters",
663 "flush output when sending interrupt characters" },
665 "automatic sending of interrupt characters in urgent mode",
668 "send interrupt characters in urgent mode" },
669 #ifdef AUTHENTICATION
671 "automatic sending of login and/or authentication info",
674 "send login name and/or authentication information" },
676 "Toggle authentication debugging",
679 "print authentication debugging information" },
683 "automatic encryption of data stream",
686 "automatically encrypt output" },
688 "automatic decryption of data stream",
691 "automatically decrypt input" },
693 "Toggle verbose encryption output",
696 "print verbose encryption output" },
698 "Toggle encryption debugging",
701 "print encryption debugging information" },
702 #endif /* ENCRYPTION */
704 "don't read ~/.telnetrc file",
707 "skip reading of ~/.telnetrc file" },
709 "sending and receiving of binary data",
714 "receiving of binary data",
719 "sending of binary data",
724 "sending carriage returns as telnet <CR><LF>",
725 (int (*)(int))togcrlf,
729 "mapping of received carriage returns",
732 "map carriage return on output" },
734 "local recognition of certain control characters",
735 (int (*)(int))lclchars,
737 "recognize certain control characters" },
738 { " ", "", NULL, NULL, NULL }, /* empty line */
741 (int (*)(int))togdebug,
743 "turn on socket level debugging" },
745 "printing of hexadecimal network data (debugging)",
748 "print hexadecimal representation of network traffic" },
750 "output of \"netdata\" to user readable format (debugging)",
753 "print user readable output for \"netdata\"" },
755 "viewing of options processing (debugging)",
758 "show option processing" },
760 "(debugging) toggle printing of hexadecimal terminal data",
763 "print hexadecimal representation of terminal traffic" },
766 (int (*)(int))togglehelp,
769 { NULL, NULL, NULL, NULL, NULL },
772 (int (*)(int))togglehelp,
775 { NULL, NULL, NULL, NULL, NULL }
781 struct togglelist *c;
783 for (c = Togglelist; c->name; c++) {
786 printf("%-15s toggle %s\n", c->name, c->help);
792 printf("%-15s %s\n", "?", "display help information");
797 settogglehelp(int set)
799 struct togglelist *c;
801 for (c = Togglelist; c->name; c++) {
804 printf("%-15s %s %s\n", c->name, set ? "enable" : "disable",
812 #define GETTOGGLE(name) (struct togglelist *) \
813 genget(name, (char **) Togglelist, sizeof(struct togglelist))
816 toggle(int argc, char *argv[])
820 struct togglelist *c;
824 "Need an argument to 'toggle' command. 'toggle ?' for help.\n");
832 if (Ambiguous((void *)c)) {
833 fprintf(stderr, "'%s': ambiguous argument ('toggle ?' for help).\n",
837 fprintf(stderr, "'%s': unknown argument ('toggle ?' for help).\n",
842 *c->variable = !*c->variable; /* invert it */
843 if (c->actionexplanation) {
844 printf("%s %s.\n", *c->variable? "Will" : "Won't",
845 c->actionexplanation);
849 retval &= (*c->handler)(-1);
857 * The following perform the "set" command.
861 struct termio new_tc = { 0, 0, 0, 0, {}, 0, 0 };
865 const char *name; /* name */
866 const char *help; /* help information */
867 void (*handler)(char *);
868 cc_t *charp; /* where it is located at */
871 static struct setlist Setlist[] = {
872 #ifdef KLUDGELINEMODE
873 { "echo", "character to toggle local echoing on/off", NULL, &echoc },
875 { "escape", "character to escape back to telnet command mode", NULL, &escape },
876 { "rlogin", "rlogin escape character", 0, &rlogin },
877 { "tracefile", "file to write trace information to", SetNetTrace, (cc_t *)NetTraceFile},
878 { " ", "", NULL, NULL },
879 { " ", "The following need 'localchars' to be toggled true", NULL, NULL },
880 { "flushoutput", "character to cause an Abort Output", NULL, termFlushCharp },
881 { "interrupt", "character to cause an Interrupt Process", NULL, termIntCharp },
882 { "quit", "character to cause an Abort process", NULL, termQuitCharp },
883 { "eof", "character to cause an EOF ", NULL, termEofCharp },
884 { " ", "", NULL, NULL },
885 { " ", "The following are for local editing in linemode", NULL, NULL },
886 { "erase", "character to use to erase a character", NULL, termEraseCharp },
887 { "kill", "character to use to erase a line", NULL, termKillCharp },
888 { "lnext", "character to use for literal next", NULL, termLiteralNextCharp },
889 { "susp", "character to cause a Suspend Process", NULL, termSuspCharp },
890 { "reprint", "character to use for line reprint", NULL, termRprntCharp },
891 { "worderase", "character to use to erase a word", NULL, termWerasCharp },
892 { "start", "character to use for XON", NULL, termStartCharp },
893 { "stop", "character to use for XOFF", NULL, termStopCharp },
894 { "forw1", "alternate end of line character", NULL, termForw1Charp },
895 { "forw2", "alternate end of line character", NULL, termForw2Charp },
896 { "ayt", "alternate AYT character", NULL, termAytCharp },
897 { "baudrate", "set remote baud rate", DoBaudRate, ComPortBaudRate },
898 { NULL, NULL, NULL, NULL }
901 static struct setlist *
904 return (struct setlist *)
905 genget(name, (char **) Setlist, sizeof(struct setlist));
909 set_escape_char(char *s)
911 if (rlogin != _POSIX_VDISABLE) {
912 rlogin = (s && *s) ? special(s) : _POSIX_VDISABLE;
913 printf("Telnet rlogin escape character is '%s'.\n",
916 escape = (s && *s) ? special(s) : _POSIX_VDISABLE;
917 printf("Telnet escape character is '%s'.\n", control(escape));
922 setcmd(int argc, char *argv[])
926 struct togglelist *c;
928 if (argc < 2 || argc > 3) {
929 printf("Format is 'set Name Value'\n'set ?' for help.\n");
932 if ((argc == 2) && (isprefix(argv[1], "?") || isprefix(argv[1], "help"))) {
933 for (ct = Setlist; ct->name; ct++)
934 printf("%-15s %s\n", ct->name, ct->help);
937 printf("%-15s %s\n", "?", "display help information");
941 ct = getset(argv[1]);
942 if (ct == 0 || !(ct->name && ct->name[0] != ' ')) {
943 c = GETTOGGLE(argv[1]);
945 fprintf(stderr, "'%s': unknown argument ('set ?' for help).\n",
948 } else if (Ambiguous((void *)c)) {
949 fprintf(stderr, "'%s': ambiguous argument ('set ?' for help).\n",
954 if ((argc == 2) || (strcmp("on", argv[2]) == 0))
956 else if (strcmp("off", argv[2]) == 0)
959 printf("Format is 'set togglename [on|off]'\n'set ?' for help.\n");
962 if (c->actionexplanation) {
963 printf("%s %s.\n", *c->variable? "Will" : "Won't",
964 c->actionexplanation);
969 } else if (argc != 3) {
970 printf("Format is 'set Name Value'\n'set ?' for help.\n");
972 } else if (Ambiguous((void *)ct)) {
973 fprintf(stderr, "'%s': ambiguous argument ('set ?' for help).\n",
976 } else if (ct->handler) {
977 (*ct->handler)(argv[2]);
978 printf("%s set to \"%s\".\n", ct->name, (char *)ct->charp);
980 if (strcmp("off", argv[2])) {
981 value = special(argv[2]);
983 value = _POSIX_VDISABLE;
985 *(ct->charp) = (cc_t)value;
986 printf("%s character is '%s'.\n", ct->name, control(*(ct->charp)));
993 unsetcmd(int argc, char *argv[])
996 struct togglelist *c;
1001 "Need an argument to 'unset' command. 'unset ?' for help.\n");
1004 if (isprefix(argv[1], "?") || isprefix(argv[1], "help")) {
1005 for (ct = Setlist; ct->name; ct++)
1006 printf("%-15s %s\n", ct->name, ct->help);
1009 printf("%-15s %s\n", "?", "display help information");
1018 if (ct == 0 || !(ct->name && ct->name[0] != ' ')) {
1019 c = GETTOGGLE(name);
1021 fprintf(stderr, "'%s': unknown argument ('unset ?' for help).\n",
1024 } else if (Ambiguous((void *)c)) {
1025 fprintf(stderr, "'%s': ambiguous argument ('unset ?' for help).\n",
1031 if (c->actionexplanation) {
1032 printf("%s %s.\n", *c->variable? "Will" : "Won't",
1033 c->actionexplanation);
1038 } else if (Ambiguous((void *)ct)) {
1039 fprintf(stderr, "'%s': ambiguous argument ('unset ?' for help).\n",
1042 } else if (ct->handler) {
1044 printf("%s reset to \"%s\".\n", ct->name, (char *)ct->charp);
1046 *(ct->charp) = _POSIX_VDISABLE;
1047 printf("%s character is '%s'.\n", ct->name, control(*(ct->charp)));
1054 * The following are the data structures and routines for the
1057 #ifdef KLUDGELINEMODE
1058 extern int kludgelinemode;
1064 send_wont(TELOPT_LINEMODE, 1);
1065 send_dont(TELOPT_SGA, 1);
1066 send_dont(TELOPT_ECHO, 1);
1074 #ifdef KLUDGELINEMODE
1076 send_dont(TELOPT_SGA, 1);
1078 send_will(TELOPT_LINEMODE, 1);
1079 send_dont(TELOPT_ECHO, 1);
1086 #ifdef KLUDGELINEMODE
1088 send_do(TELOPT_SGA, 1);
1091 send_wont(TELOPT_LINEMODE, 1);
1092 send_do(TELOPT_ECHO, 1);
1097 dolmmode(int bit, int on)
1100 extern int linemode;
1102 if (my_want_state_is_wont(TELOPT_LINEMODE)) {
1103 printf("?Need to have LINEMODE option enabled first.\n");
1104 printf("'mode ?' for help.\n");
1109 c = (linemode | bit);
1111 c = (linemode & ~bit);
1119 return dolmmode(bit, 1);
1125 return dolmmode(bit, 0);
1129 const char *name; /* command name */
1130 const char *help; /* help string */
1131 int (*handler)(int);/* routine which executes command */
1132 int needconnect; /* Do we need to be connected to execute? */
1136 static struct modelist ModeList[] = {
1137 { "character", "Disable LINEMODE option", (int (*)(int))docharmode, 1, 0 },
1138 #ifdef KLUDGELINEMODE
1139 { "", "(or disable obsolete line-by-line mode)", NULL, 0, 0 },
1141 { "line", "Enable LINEMODE option", (int (*)(int))dolinemode, 1, 0 },
1142 #ifdef KLUDGELINEMODE
1143 { "", "(or enable obsolete line-by-line mode)", NULL, 0, 0 },
1145 { "", "", NULL, 0, 0 },
1146 { "", "These require the LINEMODE option to be enabled", NULL, 0, 0 },
1147 { "isig", "Enable signal trapping", setmod, 1, MODE_TRAPSIG },
1148 { "+isig", 0, setmod, 1, MODE_TRAPSIG },
1149 { "-isig", "Disable signal trapping", clearmode, 1, MODE_TRAPSIG },
1150 { "edit", "Enable character editing", setmod, 1, MODE_EDIT },
1151 { "+edit", 0, setmod, 1, MODE_EDIT },
1152 { "-edit", "Disable character editing", clearmode, 1, MODE_EDIT },
1153 { "softtabs", "Enable tab expansion", setmod, 1, MODE_SOFT_TAB },
1154 { "+softtabs", 0, setmod, 1, MODE_SOFT_TAB },
1155 { "-softtabs", "Disable character editing", clearmode, 1, MODE_SOFT_TAB },
1156 { "litecho", "Enable literal character echo", setmod, 1, MODE_LIT_ECHO },
1157 { "+litecho", 0, setmod, 1, MODE_LIT_ECHO },
1158 { "-litecho", "Disable literal character echo", clearmode, 1, MODE_LIT_ECHO },
1159 { "help", 0, (int (*)(int))modehelp, 0, 0 },
1160 #ifdef KLUDGELINEMODE
1161 { "kludgeline", 0, (int (*)(int))dokludgemode, 1, 0 },
1163 { "", "", NULL, 0, 0 },
1164 { "?", "Print help information", (int (*)(int))modehelp, 0, 0 },
1165 { NULL, NULL, NULL, 0, 0 },
1172 struct modelist *mt;
1174 printf("format is: 'mode Mode', where 'Mode' is one of:\n\n");
1175 for (mt = ModeList; mt->name; mt++) {
1178 printf("%-15s %s\n", mt->name, mt->help);
1186 #define GETMODECMD(name) (struct modelist *) \
1187 genget(name, (char **) ModeList, sizeof(struct modelist))
1190 modecmd(int argc, char *argv[])
1192 struct modelist *mt;
1195 printf("'mode' command requires an argument\n");
1196 printf("'mode ?' for help.\n");
1197 } else if ((mt = GETMODECMD(argv[1])) == 0) {
1198 fprintf(stderr, "Unknown mode '%s' ('mode ?' for help).\n", argv[1]);
1199 } else if (Ambiguous((void *)mt)) {
1200 fprintf(stderr, "Ambiguous mode '%s' ('mode ?' for help).\n", argv[1]);
1201 } else if (mt->needconnect && !connected) {
1202 printf("?Need to be connected first.\n");
1203 printf("'mode ?' for help.\n");
1204 } else if (mt->handler) {
1205 return (*mt->handler)(mt->arg1);
1211 * The following data structures and routines implement the
1212 * "display" command.
1216 display(int argc, char *argv[])
1218 struct togglelist *tl;
1221 #define dotog(tl) if (tl->variable && tl->actionexplanation) { \
1222 if (*tl->variable) { \
1227 printf(" %s.\n", tl->actionexplanation); \
1230 #define doset(sl) if (sl->name && *sl->name != ' ') { \
1231 if (sl->handler == 0) \
1232 printf("%-15s [%s]\n", sl->name, control(*sl->charp)); \
1234 printf("%-15s \"%s\"\n", sl->name, (char *)sl->charp); \
1238 for (tl = Togglelist; tl->name; tl++) {
1242 for (sl = Setlist; sl->name; sl++) {
1248 for (i = 1; i < argc; i++) {
1249 sl = getset(argv[i]);
1250 tl = GETTOGGLE(argv[i]);
1251 if (Ambiguous((void *)sl) || Ambiguous((void *)tl)) {
1252 printf("?Ambiguous argument '%s'.\n", argv[i]);
1254 } else if (!sl && !tl) {
1255 printf("?Unknown argument '%s'.\n", argv[i]);
1267 /*@*/optionstatus();
1270 #endif /* ENCRYPTION */
1277 * The following are the data structures, and many of the routines,
1278 * relating to command processing.
1282 * Set the escape character.
1285 setescape(int argc, char *argv[])
1291 "Deprecated usage - please use 'set escape%s%s' in the future.\n",
1292 (argc > 2)? " ":"", (argc > 2)? argv[1]: "");
1296 printf("new escape character: ");
1297 (void) fgets(buf, sizeof(buf), stdin);
1302 (void) fflush(stdout);
1310 printf("Deprecated usage - please use 'toggle crmod' in the future.\n");
1311 printf("%s map carriage return on output.\n", crmod ? "Will" : "Won't");
1312 (void) fflush(stdout);
1322 long oldrows, oldcols, newrows, newcols, err_;
1324 err_ = (TerminalWindowSize(&oldrows, &oldcols) == 0) ? 1 : 0;
1325 (void) kill(0, SIGTSTP);
1327 * If we didn't get the window size before the SUSPEND, but we
1328 * can get them now (?), then send the NAWS to make sure that
1329 * we are set up for the right window size.
1331 if (TerminalWindowSize(&newrows, &newcols) && connected &&
1332 (err_ || ((oldrows != newrows) || (oldcols != newcols)))) {
1336 /* reget parameters in case they were changed */
1337 TerminalSaveState();
1340 printf("Suspend is not supported. Try the '!' command instead\n");
1346 shell(int argc, char *argv[] __unused)
1348 long oldrows, oldcols, newrows, newcols, err_;
1352 err_ = (TerminalWindowSize(&oldrows, &oldcols) == 0) ? 1 : 0;
1355 perror("Fork failed\n");
1361 * Fire up the shell in the child.
1363 const char *shellp, *shellname;
1365 shellp = getenv("SHELL");
1368 if ((shellname = strrchr(shellp, '/')) == 0)
1373 execl(shellp, shellname, "-c", &saveline[1], (char *)0);
1375 execl(shellp, shellname, (char *)0);
1380 (void)wait((int *)0); /* Wait for the shell to complete */
1382 if (TerminalWindowSize(&newrows, &newcols) && connected &&
1383 (err_ || ((oldrows != newrows) || (oldcols != newcols)))) {
1392 bye(int argc, char *argv[])
1394 extern int resettermname;
1397 (void) shutdown(net, 2);
1398 printf("Connection closed.\n");
1399 (void) NetClose(net);
1402 #ifdef AUTHENTICATION
1404 auth_encrypt_connect(connected);
1410 if ((argc != 2) || (strcmp(argv[1], "fromquit") != 0)) {
1411 longjmp(toplevel, 1);
1414 return 1; /* Keep lint, etc., happy */
1420 (void) call(bye, "bye", "fromquit", 0);
1427 send_do(TELOPT_LOGOUT, 1);
1440 void (*handler)(int);
1444 static void slc_help(void);
1446 struct slclist SlcList[] = {
1447 { "export", "Use local special character definitions",
1448 (void (*)(int))slc_mode_export, 0 },
1449 { "import", "Use remote special character definitions",
1450 slc_mode_import, 1 },
1451 { "check", "Verify remote special character definitions",
1452 slc_mode_import, 0 },
1453 { "help", NULL, (void (*)(int))slc_help, 0 },
1454 { "?", "Print help information", (void (*)(int))slc_help, 0 },
1455 { NULL, NULL, NULL, 0 },
1463 for (c = SlcList; c->name; c++) {
1466 printf("%-15s %s\n", c->name, c->help);
1473 static struct slclist *
1476 return (struct slclist *)
1477 genget(name, (char **) SlcList, sizeof(struct slclist));
1481 slccmd(int argc, char *argv[])
1487 "Need an argument to 'slc' command. 'slc ?' for help.\n");
1490 c = getslc(argv[1]);
1492 fprintf(stderr, "'%s': unknown argument ('slc ?' for help).\n",
1496 if (Ambiguous((void *)c)) {
1497 fprintf(stderr, "'%s': ambiguous argument ('slc ?' for help).\n",
1501 (*c->handler)(c->arg);
1507 * The ENVIRON command.
1513 void (*handler)(unsigned char *, unsigned char *);
1517 extern struct env_lst *
1518 env_define(const unsigned char *, unsigned char *);
1520 env_undefine(unsigned char *),
1521 env_export(const unsigned char *),
1522 env_unexport(const unsigned char *),
1523 env_send(unsigned char *),
1524 #if defined(OLD_ENVIRON) && defined(ENV_HACK)
1525 env_varval(unsigned char *),
1531 struct envlist EnvList[] = {
1532 { "define", "Define an environment variable",
1533 (void (*)(unsigned char *, unsigned char *))env_define, 2 },
1534 { "undefine", "Undefine an environment variable",
1535 (void (*)(unsigned char *, unsigned char *))env_undefine, 1 },
1536 { "export", "Mark an environment variable for automatic export",
1537 (void (*)(unsigned char *, unsigned char *))env_export, 1 },
1538 { "unexport", "Don't mark an environment variable for automatic export",
1539 (void (*)(unsigned char *, unsigned char *))env_unexport, 1 },
1540 { "send", "Send an environment variable", (void (*)(unsigned char *, unsigned char *))env_send, 1 },
1541 { "list", "List the current environment variables",
1542 (void (*)(unsigned char *, unsigned char *))env_list, 0 },
1543 #if defined(OLD_ENVIRON) && defined(ENV_HACK)
1544 { "varval", "Reverse VAR and VALUE (auto, right, wrong, status)",
1545 (void (*)(unsigned char *, unsigned char *))env_varval, 1 },
1547 { "help", NULL, (void (*)(unsigned char *, unsigned char *))env_help, 0 },
1548 { "?", "Print help information", (void (*)(unsigned char *, unsigned char *))env_help, 0 },
1549 { NULL, NULL, NULL, 0 },
1557 for (c = EnvList; c->name; c++) {
1560 printf("%-15s %s\n", c->name, c->help);
1567 static struct envlist *
1568 getenvcmd(char *name)
1570 return (struct envlist *)
1571 genget(name, (char **) EnvList, sizeof(struct envlist));
1575 env_cmd(int argc, char *argv[])
1581 "Need an argument to 'environ' command. 'environ ?' for help.\n");
1584 c = getenvcmd(argv[1]);
1586 fprintf(stderr, "'%s': unknown argument ('environ ?' for help).\n",
1590 if (Ambiguous((void *)c)) {
1591 fprintf(stderr, "'%s': ambiguous argument ('environ ?' for help).\n",
1595 if (c->narg + 2 != argc) {
1597 "Need %s%d argument%s to 'environ %s' command. 'environ ?' for help.\n",
1598 c->narg < argc + 2 ? "only " : "",
1599 c->narg, c->narg == 1 ? "" : "s", c->name);
1602 (*c->handler)(argv[2], argv[3]);
1607 struct env_lst *next; /* pointer to next structure */
1608 struct env_lst *prev; /* pointer to previous structure */
1609 unsigned char *var; /* pointer to variable name */
1610 unsigned char *value; /* pointer to variable value */
1611 int export; /* 1 -> export with default list of variables */
1612 int welldefined; /* A well defined variable */
1615 struct env_lst envlisthead;
1617 static struct env_lst *
1618 env_find(const unsigned char *var)
1622 for (ep = envlisthead.next; ep; ep = ep->next) {
1623 if (strcmp(ep->var, var) == 0)
1632 extern char **environ;
1636 for (epp = environ; *epp; epp++) {
1637 if ((cp = strchr(*epp, '='))) {
1639 ep = env_define((unsigned char *)*epp,
1640 (unsigned char *)cp+1);
1646 * Special case for DISPLAY variable. If it is ":0.0" or
1647 * "unix:0.0", we have to get rid of "unix" and insert our
1650 if ((ep = env_find("DISPLAY"))
1651 && ((*ep->value == ':')
1652 || (strncmp((char *)ep->value, "unix:", 5) == 0))) {
1654 char *cp2 = strchr((char *)ep->value, ':');
1656 gethostname(hbuf, sizeof(hbuf));
1657 hbuf[sizeof(hbuf)-1] = '\0';
1658 asprintf(&cp, "%s%s", hbuf, cp2);
1661 ep->value = (unsigned char *)cp;
1664 * If USER is not defined, but LOGNAME is, then add
1665 * USER with the value from LOGNAME. By default, we
1666 * don't export the USER variable.
1668 if ((env_find("USER") == NULL) && (ep = env_find("LOGNAME"))) {
1669 env_define("USER", ep->value);
1670 env_unexport("USER");
1672 env_export("DISPLAY");
1673 env_export("PRINTER");
1677 env_define(const unsigned char *var, unsigned char *value)
1681 if ((ep = env_find(var))) {
1687 ep = (struct env_lst *)malloc(sizeof(struct env_lst));
1688 ep->next = envlisthead.next;
1689 envlisthead.next = ep;
1690 ep->prev = &envlisthead;
1692 ep->next->prev = ep;
1694 ep->welldefined = opt_welldefined(var);
1696 ep->var = strdup(var);
1697 ep->value = strdup(value);
1702 env_undefine(unsigned char *var)
1706 if ((ep = env_find(var))) {
1707 ep->prev->next = ep->next;
1709 ep->next->prev = ep->prev;
1719 env_export(const unsigned char *var)
1723 if ((ep = env_find(var)))
1728 env_unexport(const unsigned char *var)
1732 if ((ep = env_find(var)))
1737 env_send(unsigned char *var)
1741 if (my_state_is_wont(TELOPT_NEW_ENVIRON)
1743 && my_state_is_wont(TELOPT_OLD_ENVIRON)
1747 "Cannot send '%s': Telnet ENVIRON option not enabled\n",
1753 fprintf(stderr, "Cannot send '%s': variable not defined\n",
1757 env_opt_start_info();
1758 env_opt_add(ep->var);
1767 for (ep = envlisthead.next; ep; ep = ep->next) {
1768 printf("%c %-20s %s\n", ep->export ? '*' : ' ',
1769 ep->var, ep->value);
1774 env_default(int init, int welldefined)
1776 static struct env_lst *nep = NULL;
1783 while ((nep = nep->next)) {
1784 if (nep->export && (nep->welldefined == welldefined))
1792 env_getvalue(const unsigned char *var)
1796 if ((ep = env_find(var)))
1801 #if defined(OLD_ENVIRON) && defined(ENV_HACK)
1803 env_varval(unsigned char *what)
1805 extern int old_env_var, old_env_value, env_auto;
1806 int len = strlen((char *)what);
1811 if (strncasecmp((char *)what, "status", len) == 0) {
1813 printf("%s%s", "VAR and VALUE are/will be ",
1814 "determined automatically\n");
1815 if (old_env_var == OLD_ENV_VAR)
1816 printf("VAR and VALUE set to correct definitions\n");
1818 printf("VAR and VALUE definitions are reversed\n");
1819 } else if (strncasecmp((char *)what, "auto", len) == 0) {
1821 old_env_var = OLD_ENV_VALUE;
1822 old_env_value = OLD_ENV_VAR;
1823 } else if (strncasecmp((char *)what, "right", len) == 0) {
1825 old_env_var = OLD_ENV_VAR;
1826 old_env_value = OLD_ENV_VALUE;
1827 } else if (strncasecmp((char *)what, "wrong", len) == 0) {
1829 old_env_var = OLD_ENV_VALUE;
1830 old_env_value = OLD_ENV_VAR;
1833 printf("Unknown \"varval\" command. (\"auto\", \"right\", \"wrong\", \"status\")\n");
1838 #ifdef AUTHENTICATION
1840 * The AUTHENTICATE command.
1846 int (*handler)(char *);
1851 auth_enable(char *),
1852 auth_disable(char *),
1857 struct authlist AuthList[] = {
1858 { "status", "Display current status of authentication information",
1859 (int (*)(char *))auth_status, 0 },
1860 { "disable", "Disable an authentication type ('auth disable ?' for more)",
1862 { "enable", "Enable an authentication type ('auth enable ?' for more)",
1864 { "help", NULL, (int (*)(char *))auth_help, 0 },
1865 { "?", "Print help information", (int (*)(char *))auth_help, 0 },
1866 { NULL, NULL, NULL, 0 },
1874 for (c = AuthList; c->name; c++) {
1877 printf("%-15s %s\n", c->name, c->help);
1886 auth_cmd(int argc, char *argv[])
1892 "Need an argument to 'auth' command. 'auth ?' for help.\n");
1896 c = (struct authlist *)
1897 genget(argv[1], (char **) AuthList, sizeof(struct authlist));
1899 fprintf(stderr, "'%s': unknown argument ('auth ?' for help).\n",
1903 if (Ambiguous((void *)c)) {
1904 fprintf(stderr, "'%s': ambiguous argument ('auth ?' for help).\n",
1908 if (c->narg + 2 != argc) {
1910 "Need %s%d argument%s to 'auth %s' command. 'auth ?' for help.\n",
1911 c->narg < argc + 2 ? "only " : "",
1912 c->narg, c->narg == 1 ? "" : "s", c->name);
1915 return((*c->handler)(argv[2]));
1921 * The ENCRYPT command.
1924 struct encryptlist {
1927 int (*handler)(char *, char *);
1934 EncryptEnable(char *, char *),
1935 EncryptDisable(char *, char *),
1936 EncryptType(char *, char *),
1937 EncryptStart(char *),
1938 EncryptStartInput(void),
1939 EncryptStartOutput(void),
1940 EncryptStop(char *),
1941 EncryptStopInput(void),
1942 EncryptStopOutput(void),
1943 EncryptStatus(void);
1947 struct encryptlist EncryptList[] = {
1948 { "enable", "Enable encryption. ('encrypt enable ?' for more)",
1949 EncryptEnable, 1, 1, 2 },
1950 { "disable", "Disable encryption. ('encrypt enable ?' for more)",
1951 EncryptDisable, 0, 1, 2 },
1952 { "type", "Set encryption type. ('encrypt type ?' for more)",
1953 EncryptType, 0, 1, 1 },
1954 { "start", "Start encryption. ('encrypt start ?' for more)",
1955 (int (*)(char *, char *))EncryptStart, 1, 0, 1 },
1956 { "stop", "Stop encryption. ('encrypt stop ?' for more)",
1957 (int (*)(char *, char *))EncryptStop, 1, 0, 1 },
1958 { "input", "Start encrypting the input stream",
1959 (int (*)(char *, char *))EncryptStartInput, 1, 0, 0 },
1960 { "-input", "Stop encrypting the input stream",
1961 (int (*)(char *, char *))EncryptStopInput, 1, 0, 0 },
1962 { "output", "Start encrypting the output stream",
1963 (int (*)(char *, char *))EncryptStartOutput, 1, 0, 0 },
1964 { "-output", "Stop encrypting the output stream",
1965 (int (*)(char *, char *))EncryptStopOutput, 1, 0, 0 },
1967 { "status", "Display current status of authentication information",
1968 (int (*)(char *, char *))EncryptStatus, 0, 0, 0 },
1969 { "help", NULL, (int (*)(char *, char *))EncryptHelp, 0, 0, 0 },
1970 { "?", "Print help information", (int (*)(char *, char *))EncryptHelp, 0, 0, 0 },
1971 { NULL, NULL, NULL, 0, 0, 0 },
1977 struct encryptlist *c;
1979 for (c = EncryptList; c->name; c++) {
1982 printf("%-15s %s\n", c->name, c->help);
1991 encrypt_cmd(int argc, char *argv[])
1993 struct encryptlist *c;
1997 "Need an argument to 'encrypt' command. 'encrypt ?' for help.\n");
2001 c = (struct encryptlist *)
2002 genget(argv[1], (char **) EncryptList, sizeof(struct encryptlist));
2004 fprintf(stderr, "'%s': unknown argument ('encrypt ?' for help).\n",
2008 if (Ambiguous((void *)c)) {
2009 fprintf(stderr, "'%s': ambiguous argument ('encrypt ?' for help).\n",
2014 if (argc < c->minarg || argc > c->maxarg) {
2015 if (c->minarg == c->maxarg) {
2016 fprintf(stderr, "Need %s%d argument%s ",
2017 c->minarg < argc ? "only " : "", c->minarg,
2018 c->minarg == 1 ? "" : "s");
2020 fprintf(stderr, "Need %s%d-%d arguments ",
2021 c->maxarg < argc ? "only " : "", c->minarg, c->maxarg);
2023 fprintf(stderr, "to 'encrypt %s' command. 'encrypt ?' for help.\n",
2027 if (c->needconnect && !connected) {
2028 if (!(argc && (isprefix(argv[2], "help") || isprefix(argv[2], "?")))) {
2029 printf("?Need to be connected first.\n");
2033 return ((*c->handler)(argc > 0 ? argv[2] : 0,
2034 argc > 1 ? argv[3] : 0));
2036 #endif /* ENCRYPTION */
2039 * Print status about the connection.
2043 status(int argc, char *argv[])
2047 printf("Connected to %s.\n", hostname);
2048 if ((argc < 2) || strcmp(argv[1], "notmuch")) {
2049 int mode = getconnmode();
2051 if (my_want_state_is_will(TELOPT_LINEMODE)) {
2052 printf("Operating with LINEMODE option\n");
2053 printf("%s line editing\n", (mode&MODE_EDIT) ? "Local" : "No");
2054 printf("%s catching of signals\n",
2055 (mode&MODE_TRAPSIG) ? "Local" : "No");
2057 #ifdef KLUDGELINEMODE
2058 } else if (kludgelinemode && my_want_state_is_dont(TELOPT_SGA)) {
2059 printf("Operating in obsolete linemode\n");
2062 printf("Operating in single character mode\n");
2064 printf("Catching signals locally\n");
2066 printf("%s character echo\n", (mode&MODE_ECHO) ? "Local" : "Remote");
2067 if (my_want_state_is_will(TELOPT_LFLOW))
2068 printf("%s flow control\n", (mode&MODE_FLOW) ? "Local" : "No");
2071 #endif /* ENCRYPTION */
2074 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[])
2174 unsigned char *srp = 0;
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);
2271 printf("Trying %s...\n", hostp);
2272 net = socket(PF_UNIX, SOCK_STREAM, 0);
2277 if (connect(net, (struct sockaddr *)&su, sizeof su) == -1) {
2278 perror(su.sun_path);
2279 (void) NetClose(net);
2283 } else if (hostp[0] == '@' || hostp[0] == '!') {
2286 family == AF_INET6 ||
2288 (hostname = strrchr(hostp, ':')) == NULL)
2289 hostname = strrchr(hostp, '@');
2290 if (hostname == NULL) {
2300 portp = strdup("telnet");
2301 } else if (*portp == '-') {
2304 } else if (*portp == '+') {
2310 memset(&hints, 0, sizeof(hints));
2311 hints.ai_flags = AI_NUMERICHOST;
2312 hints.ai_family = family;
2313 hints.ai_socktype = SOCK_STREAM;
2314 error = getaddrinfo(hostname, portp, &hints, &res);
2316 hints.ai_flags = AI_CANONNAME;
2317 error = getaddrinfo(hostname, portp, &hints, &res);
2320 fprintf(stderr, "%s: %s\n", hostname, gai_strerror(error));
2321 if (error == EAI_SYSTEM)
2322 fprintf(stderr, "%s: %s\n", hostname, strerror(errno));
2326 if (hints.ai_flags == AI_NUMERICHOST) {
2327 /* hostname has numeric */
2331 gni_err = getnameinfo(res->ai_addr, res->ai_addr->sa_len,
2332 _hostname, sizeof(_hostname) - 1, NULL, 0,
2335 (void) strncpy(_hostname, hostp, sizeof(_hostname) - 1);
2336 _hostname[sizeof(_hostname)-1] = '\0';
2337 hostname = _hostname;
2339 /* hostname has FQDN */
2341 (void) strncpy(_hostname, hostname, sizeof(_hostname) - 1);
2342 else if (res->ai_canonname != NULL)
2343 strcpy(_hostname, res->ai_canonname);
2345 (void) strncpy(_hostname, hostp, sizeof(_hostname) - 1);
2346 _hostname[sizeof(_hostname)-1] = '\0';
2347 hostname = _hostname;
2353 if (srcroute != 0) {
2354 static char hostbuf[BUFSIZ];
2356 if (af_error == 0) { /* save intermediate hostnames for retry */
2357 strncpy(hostbuf, hostp, BUFSIZ - 1);
2358 hostbuf[BUFSIZ - 1] = '\0';
2362 result = sourceroute(res, hostp, &srp, &srlen, &proto, &opt);
2365 if (family == AF_UNSPEC && af_error == 0 &&
2366 switch_af(&res) == 1) {
2373 } else if (result == -1) {
2374 printf("Bad source route option: %s\n", hostp);
2381 printf("Trying %s...\n", sockaddr_ntop(res->ai_addr));
2382 net = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
2386 if (family == AF_UNSPEC && af_error == 0 &&
2387 switch_af(&res) == 1) {
2392 perror("telnet: socket");
2395 if (srp && setsockopt(net, proto, opt, (char *)srp, srlen) < 0)
2396 perror("setsockopt (source route)");
2397 #if defined(IPPROTO_IP) && defined(IP_TOS)
2398 if (res->ai_family == PF_INET) {
2399 # if defined(HAS_GETTOS)
2401 if (tos < 0 && (tp = gettosbyname("telnet", "tcp")))
2405 tos = IPTOS_LOWDELAY;
2407 && (setsockopt(net, IPPROTO_IP, IP_TOS,
2408 (char *)&tos, sizeof(int)) < 0)
2409 && (errno != ENOPROTOOPT))
2410 perror("telnet: setsockopt (IP_TOS) (ignored)");
2412 #endif /* defined(IPPROTO_IP) && defined(IP_TOS) */
2414 if (telnet_debug && SetSockOpt(net, SOL_SOCKET, SO_DEBUG, 1) < 0) {
2415 perror("setsockopt (SO_DEBUG)");
2418 if (src_addr != NULL) {
2419 for (src_res = src_res0; src_res != 0; src_res = src_res->ai_next)
2420 if (src_res->ai_family == res->ai_family)
2422 if (src_res == NULL)
2424 if (bind(net, src_res->ai_addr, src_res->ai_addrlen) == -1) {
2426 if (family == AF_UNSPEC && af_error == 0 &&
2427 switch_af(&res) == 1) {
2429 (void) NetClose(net);
2434 (void) NetClose(net);
2438 #if defined(IPSEC) && defined(IPSEC_POLICY_IPSEC)
2439 if (setpolicy(net, res, ipsec_policy_in) < 0) {
2440 (void) NetClose(net);
2443 if (setpolicy(net, res, ipsec_policy_out) < 0) {
2444 (void) NetClose(net);
2449 if (connect(net, res->ai_addr, res->ai_addrlen) < 0) {
2450 struct addrinfo *next;
2452 next = res->ai_next;
2453 /* If already an af failed, only try same af. */
2455 while (next != NULL && next->ai_family != res->ai_family)
2456 next = next->ai_next;
2457 warn("connect to address %s", sockaddr_ntop(res->ai_addr));
2460 (void) NetClose(net);
2463 warnx("Unable to connect to remote host");
2464 (void) NetClose(net);
2468 #ifdef AUTHENTICATION
2470 auth_encrypt_connect(connected);
2473 } while (connected == 0);
2475 if (src_res0 != NULL)
2476 freeaddrinfo(src_res0);
2477 cmdrc(hostp, hostname);
2480 if (autologin && user == NULL) {
2483 user = getenv("USER");
2485 ((pw = getpwnam(user)) && pw->pw_uid != getuid())) {
2486 if ((pw = getpwuid(getuid())))
2493 env_define("USER", user);
2496 (void) call(status, "status", "notmuch", 0);
2498 (void) NetClose(net);
2502 ExitString("Connection closed by foreign host.\n",1);
2507 if (src_res0 != NULL)
2508 freeaddrinfo(src_res0);
2512 #define HELPINDENT (sizeof ("connect"))
2515 openhelp[] = "connect to a site",
2516 closehelp[] = "close current connection",
2517 logouthelp[] = "forcibly logout remote user and close the connection",
2518 quithelp[] = "exit telnet",
2519 statushelp[] = "print status information",
2520 helphelp[] = "print help information",
2521 sendhelp[] = "transmit special characters ('send ?' for more)",
2522 sethelp[] = "set operating parameters ('set ?' for more)",
2523 unsethelp[] = "unset operating parameters ('unset ?' for more)",
2524 togglestring[] ="toggle operating parameters ('toggle ?' for more)",
2525 slchelp[] = "change state of special charaters ('slc ?' for more)",
2526 displayhelp[] = "display operating parameters",
2527 #ifdef AUTHENTICATION
2528 authhelp[] = "turn on (off) authentication ('auth ?' for more)",
2531 encrypthelp[] = "turn on (off) encryption ('encrypt ?' for more)",
2532 #endif /* ENCRYPTION */
2533 zhelp[] = "suspend telnet",
2535 opiehelp[] = "compute response to OPIE challenge",
2537 shellhelp[] = "invoke a subshell",
2538 envhelp[] = "change environment variables ('environ ?' for more)",
2539 modestring[] = "try to enter line or character mode ('mode ?' for more)";
2541 static Command cmdtab[] = {
2542 { "close", closehelp, bye, 1 },
2543 { "logout", logouthelp, (int (*)(int, char **))logout, 1 },
2544 { "display", displayhelp, display, 0 },
2545 { "mode", modestring, modecmd, 0 },
2546 { "telnet", openhelp, tn, 0 },
2547 { "open", openhelp, tn, 0 },
2548 { "quit", quithelp, (int (*)(int, char **))quit, 0 },
2549 { "send", sendhelp, sendcmd, 0 },
2550 { "set", sethelp, setcmd, 0 },
2551 { "unset", unsethelp, unsetcmd, 0 },
2552 { "status", statushelp, status, 0 },
2553 { "toggle", togglestring, toggle, 0 },
2554 { "slc", slchelp, slccmd, 0 },
2555 #ifdef AUTHENTICATION
2556 { "auth", authhelp, auth_cmd, 0 },
2559 { "encrypt", encrypthelp, encrypt_cmd, 0 },
2560 #endif /* ENCRYPTION */
2561 { "z", zhelp, (int (*)(int, char **))suspend, 0 },
2562 { "!", shellhelp, shell, 1 },
2563 { "environ", envhelp, env_cmd, 0 },
2564 { "?", helphelp, help, 0 },
2566 { "opie", opiehelp, opie_calc, 0 },
2568 { NULL, NULL, NULL, 0 }
2571 static char crmodhelp[] = "deprecated command -- use 'toggle crmod' instead";
2572 static char escapehelp[] = "deprecated command -- use 'set escape' instead";
2574 static Command cmdtab2[] = {
2575 { "help", 0, help, 0 },
2576 { "escape", escapehelp, setescape, 0 },
2577 { "crmod", crmodhelp, (int (*)(int, char **))togcrmod, 0 },
2578 { NULL, NULL, NULL, 0 }
2583 * Call routine with argc, argv set from args (terminated by 0).
2587 call(intrtn_t routine, ...)
2593 va_start(ap, routine);
2594 while ((args[argno++] = va_arg(ap, char *)) != 0);
2596 return (*routine)(argno-1, args);
2605 if ((cm = (Command *) genget(name, (char **) cmdtab, sizeof(Command))))
2607 return (Command *) genget(name, (char **) cmdtab2, sizeof(Command));
2611 command(int top, const char *tbuf, int cnt)
2619 (void) signal(SIGINT, SIG_DFL);
2620 (void) signal(SIGQUIT, SIG_DFL);
2623 if (rlogin == _POSIX_VDISABLE)
2624 printf("%s> ", prompt);
2628 while (cnt > 0 && (*cp++ = *tbuf++) != '\n')
2631 if (cp == line || *--cp != '\n' || cp == line)
2634 if (rlogin == _POSIX_VDISABLE)
2635 printf("%s\n", line);
2638 if (rlogin != _POSIX_VDISABLE)
2639 printf("%s> ", prompt);
2640 if (fgets(line, sizeof(line), stdin) == NULL) {
2641 if (feof(stdin) || ferror(stdin)) {
2651 if (margv[0] == 0) {
2654 c = getcmd(margv[0]);
2655 if (Ambiguous((void *)c)) {
2656 printf("?Ambiguous command\n");
2660 printf("?Invalid command\n");
2663 if (c->needconnect && !connected) {
2664 printf("?Need to be connected first.\n");
2667 if ((*c->handler)(margc, margv)) {
2673 longjmp(toplevel, 1);
2684 help(int argc, char *argv[])
2689 printf("Commands may be abbreviated. Commands are:\n\n");
2690 for (c = cmdtab; c->name; c++)
2692 printf("%-*s\t%s\n", (int)HELPINDENT, c->name,
2697 else while (--argc > 0) {
2701 if (Ambiguous((void *)c))
2702 printf("?Ambiguous help command %s\n", arg);
2703 else if (c == (Command *)0)
2704 printf("?Invalid help command %s\n", arg);
2706 printf("%s\n", c->help);
2711 static char *rcname = 0;
2712 static char rcbuf[128];
2715 cmdrc(char *m1, char *m2)
2720 int l1 = strlen(m1);
2721 int l2 = strlen(m2);
2722 char m1save[MAXHOSTNAMELEN];
2727 strlcpy(m1save, m1, sizeof(m1save));
2731 rcname = getenv("HOME");
2732 if (rcname && (strlen(rcname) + 10) < sizeof(rcbuf))
2733 strcpy(rcbuf, rcname);
2736 strcat(rcbuf, "/.telnetrc");
2740 if ((rcfile = fopen(rcname, "r")) == 0) {
2745 if (fgets(line, sizeof(line), rcfile) == NULL)
2752 if (!isspace(line[0]))
2755 if (gotmachine == 0) {
2756 if (isspace(line[0]))
2758 if (strncasecmp(line, m1, l1) == 0)
2759 strncpy(line, &line[l1], sizeof(line) - l1);
2760 else if (strncasecmp(line, m2, l2) == 0)
2761 strncpy(line, &line[l2], sizeof(line) - l2);
2762 else if (strncasecmp(line, "DEFAULT", 7) == 0)
2763 strncpy(line, &line[7], sizeof(line) - 7);
2766 if (line[0] != ' ' && line[0] != '\t' && line[0] != '\n')
2773 c = getcmd(margv[0]);
2774 if (Ambiguous((void *)c)) {
2775 printf("?Ambiguous command: %s\n", margv[0]);
2779 printf("?Invalid command: %s\n", margv[0]);
2783 * This should never happen...
2785 if (c->needconnect && !connected) {
2786 printf("?Need to be connected first for %s.\n", margv[0]);
2789 (*c->handler)(margc, margv);
2795 * Source route is handed in as
2796 * [!]@hop1@hop2...[@|:]dst
2797 * If the leading ! is present, it is a
2798 * strict source route, otherwise it is
2799 * assmed to be a loose source route.
2801 * We fill in the source route option as
2802 * hop1,hop2,hop3...dest
2803 * and return a pointer to hop1, which will
2804 * be the address to connect() to.
2808 * res: ponter to addrinfo structure which contains sockaddr to
2809 * the host to connect to.
2811 * arg: pointer to route list to decipher
2813 * cpp: If *cpp is not equal to NULL, this is a
2814 * pointer to a pointer to a character array
2815 * that should be filled in with the option.
2817 * lenp: pointer to an integer that contains the
2818 * length of *cpp if *cpp != NULL.
2820 * protop: pointer to an integer that should be filled in with
2821 * appropriate protocol for setsockopt, as socket
2824 * optp: pointer to an integer that should be filled in with
2825 * appropriate option for setsockopt, as socket protocol
2830 * If the return value is 1, then all operations are
2831 * successful. If the
2832 * return value is -1, there was a syntax error in the
2833 * option, either unknown characters, or too many hosts.
2834 * If the return value is 0, one of the hostnames in the
2835 * path is unknown, and *cpp is set to point to the bad
2838 * *cpp: If *cpp was equal to NULL, it will be filled
2839 * in with a pointer to our static area that has
2840 * the option filled in. This will be 32bit aligned.
2842 * *lenp: This will be filled in with how long the option
2843 * pointed to by *cpp is.
2845 * *protop: This will be filled in with appropriate protocol for
2846 * setsockopt, as socket protocol family.
2848 * *optp: This will be filled in with appropriate option for
2849 * setsockopt, as socket protocol family.
2852 sourceroute(struct addrinfo *ai, char *arg, unsigned char **cpp, int *lenp, int *protop, int *optp)
2854 static char buf[1024 + ALIGNBYTES]; /*XXX*/
2855 unsigned char *cp, *cp2, *lsrp, *ep;
2856 struct sockaddr_in *_sin;
2858 struct sockaddr_in6 *sin6;
2859 struct ip6_rthdr *rth;
2861 struct addrinfo hints, *res;
2866 * Verify the arguments, and make sure we have
2867 * at least 7 bytes for the option.
2869 if (cpp == NULL || lenp == NULL)
2872 switch (res->ai_family) {
2879 if (*lenp < (int)CMSG_SPACE(sizeof(struct ip6_rthdr) +
2880 sizeof(struct in6_addr)))
2887 * Decide whether we have a buffer passed to us,
2888 * or if we need to use our own static buffer.
2894 *cpp = lsrp = (char *)ALIGN(buf);
2901 if (ai->ai_family == AF_INET6) {
2902 if ((rth = inet6_rth_init((void *)*cpp, sizeof(buf),
2903 IPV6_RTHDR_TYPE_0, 0)) == NULL)
2907 *protop = IPPROTO_IPV6;
2913 * Next, decide whether we have a loose source
2914 * route or a strict source route, and fill in
2915 * the begining of the option.
2919 *lsrp++ = IPOPT_SSRR;
2921 *lsrp++ = IPOPT_LSRR;
2926 lsrp++; /* skip over length, we'll fill it in later */
2928 *protop = IPPROTO_IP;
2933 memset(&hints, 0, sizeof(hints));
2934 hints.ai_family = ai->ai_family;
2935 hints.ai_socktype = SOCK_STREAM;
2939 ai->ai_family != AF_INET6 &&
2943 else for (cp2 = cp; (c = *cp2); cp2++) {
2948 } else if (c == '@') {
2952 ai->ai_family != AF_INET6 &&
2963 hints.ai_flags = AI_NUMERICHOST;
2964 error = getaddrinfo(cp, NULL, &hints, &res);
2965 if (error == EAI_NONAME) {
2967 error = getaddrinfo(cp, NULL, &hints, &res);
2970 fprintf(stderr, "%s: %s\n", cp, gai_strerror(error));
2971 if (error == EAI_SYSTEM)
2972 fprintf(stderr, "%s: %s\n", cp,
2978 if (res->ai_family == AF_INET6) {
2979 sin6 = (struct sockaddr_in6 *)res->ai_addr;
2980 if (inet6_rth_add((void *)rth, &sin6->sin6_addr) == -1)
2985 _sin = (struct sockaddr_in *)res->ai_addr;
2986 memcpy(lsrp, (char *)&_sin->sin_addr, 4);
2994 * Check to make sure there is space for next address
3001 if (res->ai_family == AF_INET6) {
3002 rth->ip6r_len = rth->ip6r_segleft * 2;
3003 *lenp = (rth->ip6r_len + 1) << 3;
3007 if ((*(*cpp+IPOPT_OLEN) = lsrp - *cpp) <= 7) {
3012 *lsrp++ = IPOPT_NOP; /* 32 bit word align it */
3013 *lenp = lsrp - *cpp;