2 * Copyright (c) 1988, 1990, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * @(#)commands.c 8.4 (Berkeley) 5/30/95
34 * $FreeBSD: src/crypto/telnet/telnet/commands.c,v 1.12.2.7 2003/04/23 07:16:32 ru Exp $
35 * $DragonFly: src/crypto/telnet/telnet/commands.c,v 1.2 2003/06/17 04:24:37 dillon Exp $
38 #include <sys/param.h>
41 #include <sys/socket.h>
42 #include <netinet/in.h>
55 #include <arpa/telnet.h>
56 #include <arpa/inet.h>
68 #include <libtelnet/auth.h>
71 #include <libtelnet/encrypt.h>
74 #include <netinet/in_systm.h>
75 #include <netinet/ip.h>
76 #include <netinet/ip6.h>
78 #ifndef MAXHOSTNAMELEN
79 #define MAXHOSTNAMELEN 256
82 typedef int (*intrtn_t)(int, char **);
85 extern int auth_togdebug(int);
88 extern int EncryptAutoEnc(int);
89 extern int EncryptAutoDec(int);
90 extern int EncryptDebug(int);
91 extern int EncryptVerbose(int);
92 #endif /* ENCRYPTION */
93 #if defined(IPPROTO_IP) && defined(IP_TOS)
95 #endif /* defined(IPPROTO_IP) && defined(IP_TOS) */
98 static char _hostname[MAXHOSTNAMELEN];
100 static int help(int, char **);
101 static int call(intrtn_t, ...);
102 static void cmdrc(char *, char *);
104 static int switch_af(struct addrinfo **);
106 static int togglehelp(void);
107 static int send_tncmd(void (*)(int, int), const char *, char *);
108 static int setmod(int);
109 static int clearmode(int);
110 static int modehelp(void);
111 static int sourceroute(struct addrinfo *, char *, char **, int *, int *, int *);
114 const char *name; /* command name */
115 const char *help; /* help string (NULL for no help) */
116 int (*handler)(int, char **); /* routine which executes command */
117 int needconnect; /* Do we need to be connected to execute? */
120 static char line[256];
121 static char saveline[256];
123 static char *margv[20];
126 #include <sys/wait.h>
127 #define PATH_OPIEKEY "/usr/bin/opiekey"
129 opie_calc(int argc, char *argv[])
134 printf("%s sequence challenge\n", argv[0]);
140 execv(PATH_OPIEKEY, argv);
146 (void) wait(&status);
147 if (WIFEXITED(status))
148 return (WEXITSTATUS(status));
162 if (*cp == '!') { /* Special case shell escape */
163 strcpy(saveline, line); /* save for shell command */
164 *argp++ = strdup("!"); /* No room in string to get this */
176 for (cp2 = cp; c != '\0'; c = *++cp) {
184 if ((c = *++cp) == '\0')
186 } else if (c == '"') {
189 } else if (c == '\'') {
192 } else if (isspace(c))
206 * Make a character string into a number.
208 * Todo: 1. Could take random integers (12, 0x12, 012, 0b1).
221 c = b | 0x40; /* DEL */
234 * Construct a control character sequence
235 * for a special character.
242 * The only way I could get the Sun 3.5 compiler
244 * if ((unsigned int)c >= 0x80)
245 * was to assign "c" to an unsigned int variable...
248 unsigned int uic = (unsigned int)c;
252 if (c == (cc_t)_POSIX_VDISABLE) {
257 buf[1] = ((c>>6)&07) + '0';
258 buf[2] = ((c>>3)&07) + '0';
259 buf[3] = (c&07) + '0';
261 } else if (uic >= 0x20) {
273 * The following are data structures and routines for
274 * the "send" command.
279 const char *name; /* How user refers to it (case independent) */
280 const char *help; /* Help information (0 ==> no help) */
281 int needconnect; /* Need to be connected */
282 int narg; /* Number of arguments */
283 int (*handler)(char *, ...); /* Routine to perform (for special ops) */
284 int nbyte; /* Number of bytes to send this command */
285 int what; /* Character to be sent (<0 ==> special) */
293 send_dontcmd(char *),
294 send_willcmd(char *),
295 send_wontcmd(char *);
297 static struct sendlist Sendlist[] = {
298 { "ao", "Send Telnet Abort output", 1, 0, NULL, 2, AO },
299 { "ayt", "Send Telnet 'Are You There'", 1, 0, NULL, 2, AYT },
300 { "brk", "Send Telnet Break", 1, 0, NULL, 2, BREAK },
301 { "break", NULL, 1, 0, NULL, 2, BREAK },
302 { "ec", "Send Telnet Erase Character", 1, 0, NULL, 2, EC },
303 { "el", "Send Telnet Erase Line", 1, 0, NULL, 2, EL },
304 { "escape", "Send current escape character",1, 0, (int (*)(char *, ...))send_esc, 1, 0 },
305 { "ga", "Send Telnet 'Go Ahead' sequence", 1, 0, NULL, 2, GA },
306 { "ip", "Send Telnet Interrupt Process",1, 0, NULL, 2, IP },
307 { "intp", NULL, 1, 0, NULL, 2, IP },
308 { "interrupt", NULL, 1, 0, NULL, 2, IP },
309 { "intr", NULL, 1, 0, NULL, 2, IP },
310 { "nop", "Send Telnet 'No operation'", 1, 0, NULL, 2, NOP },
311 { "eor", "Send Telnet 'End of Record'", 1, 0, NULL, 2, EOR },
312 { "abort", "Send Telnet 'Abort Process'", 1, 0, NULL, 2, ABORT },
313 { "susp", "Send Telnet 'Suspend Process'",1, 0, NULL, 2, SUSP },
314 { "eof", "Send Telnet End of File Character", 1, 0, NULL, 2, xEOF },
315 { "synch", "Perform Telnet 'Synch operation'", 1, 0, (int (*)(char *, ...))dosynch, 2, 0 },
316 { "getstatus", "Send request for STATUS", 1, 0, (int (*)(char *, ...))get_status, 6, 0 },
317 { "?", "Display send options", 0, 0, (int (*)(char *, ...))send_help, 0, 0 },
318 { "help", NULL, 0, 0, (int (*)(char *, ...))send_help, 0, 0 },
319 { "do", NULL, 0, 1, (int (*)(char *, ...))send_docmd, 3, 0 },
320 { "dont", NULL, 0, 1, (int (*)(char *, ...))send_dontcmd, 3, 0 },
321 { "will", NULL, 0, 1, (int (*)(char *, ...))send_willcmd, 3, 0 },
322 { "wont", NULL, 0, 1, (int (*)(char *, ...))send_wontcmd, 3, 0 },
323 { NULL, NULL, 0, 0, NULL, 0, 0 }
326 #define GETSEND(name) ((struct sendlist *) genget(name, (char **) Sendlist, \
327 sizeof(struct sendlist)))
330 sendcmd(int argc, char *argv[])
332 int count; /* how many bytes we are going to need to send */
334 struct sendlist *s; /* pointer to current command */
339 printf("need at least one argument for 'send' command\n");
340 printf("'send ?' for help\n");
344 * First, validate all the send arguments.
345 * In addition, we see how much space we are going to need, and
346 * whether or not we will be doing a "SYNCH" operation (which
347 * flushes the network queue).
350 for (i = 1; i < argc; i++) {
351 s = GETSEND(argv[i]);
353 printf("Unknown send argument '%s'\n'send ?' for help.\n",
356 } else if (Ambiguous((void *)s)) {
357 printf("Ambiguous send argument '%s'\n'send ?' for help.\n",
361 if (i + s->narg >= argc) {
363 "Need %d argument%s to 'send %s' command. 'send %s ?' for help.\n",
364 s->narg, s->narg == 1 ? "" : "s", s->name, s->name);
368 if ((void *)s->handler == (void *)send_help) {
374 needconnect += s->needconnect;
376 if (!connected && needconnect) {
377 printf("?Need to be connected first.\n");
378 printf("'send ?' for help\n");
381 /* Now, do we have enough room? */
382 if (NETROOM() < count) {
383 printf("There is not enough room in the buffer TO the network\n");
384 printf("to process your request. Nothing will be done.\n");
385 printf("('send synch' will throw away most data in the network\n");
386 printf("buffer, if this might help.)\n");
389 /* OK, they are all OK, now go through again and actually send */
391 for (i = 1; i < argc; i++) {
392 if ((s = GETSEND(argv[i])) == 0) {
393 fprintf(stderr, "Telnet 'send' error - argument disappeared!\n");
399 success += (*s->handler)((s->narg > 0) ? argv[i+1] : 0,
400 (s->narg > 1) ? argv[i+2] : 0);
403 NET2ADD(IAC, s->what);
404 printoption("SENT", IAC, s->what);
407 return (count == success);
418 send_docmd(char *name)
420 return(send_tncmd(send_do, "do", name));
427 return(send_tncmd(send_dont, "dont", name));
431 send_willcmd(char *name)
433 return(send_tncmd(send_will, "will", name));
437 send_wontcmd(char *name)
439 return(send_tncmd(send_wont, "wont", name));
443 send_tncmd(void (*func)(int, int), const char *cmd, char *name)
446 extern char *telopts[];
449 if (isprefix(name, "help") || isprefix(name, "?")) {
452 printf("Usage: send %s <value|option>\n", cmd);
453 printf("\"value\" must be from 0 to 255\n");
454 printf("Valid options are:\n\t");
457 for (cpp = telopts; *cpp; cpp++) {
458 len = strlen(*cpp) + 3;
459 if (col + len > 65) {
463 printf(" \"%s\"", *cpp);
469 cpp = (char **)genget(name, telopts, sizeof(char *));
470 if (Ambiguous(cpp)) {
471 fprintf(stderr,"'%s': ambiguous argument ('send %s ?' for help).\n",
480 while (*cp >= '0' && *cp <= '9') {
486 fprintf(stderr, "'%s': unknown argument ('send %s ?' for help).\n",
489 } else if (val < 0 || val > 255) {
490 fprintf(stderr, "'%s': bad value ('send %s ?' for help).\n",
496 printf("?Need to be connected first.\n");
506 struct sendlist *s; /* pointer to current command */
507 for (s = Sendlist; s->name; s++) {
509 printf("%-15s %s\n", s->name, s->help);
515 * The following are the routines and data structures referred
516 * to by the arguments to the "toggle" command.
531 (SetSockOpt(net, SOL_SOCKET, SO_DEBUG, debug)) < 0) {
532 perror("setsockopt (SO_DEBUG)");
536 if (net > 0 && SetSockOpt(net, SOL_SOCKET, SO_DEBUG, 1) < 0)
537 perror("setsockopt (SO_DEBUG)");
539 printf("Cannot turn off socket debugging\n");
549 printf("Will send carriage returns as telnet <CR><LF>.\n");
551 printf("Will send carriage returns as telnet <CR><NUL>.\n");
561 donebinarytoggle = 1;
566 if (my_want_state_is_will(TELOPT_BINARY) &&
567 my_want_state_is_do(TELOPT_BINARY)) {
569 } else if (my_want_state_is_wont(TELOPT_BINARY) &&
570 my_want_state_is_dont(TELOPT_BINARY)) {
573 val = binmode ? 0 : 1;
577 if (my_want_state_is_will(TELOPT_BINARY) &&
578 my_want_state_is_do(TELOPT_BINARY)) {
579 printf("Already operating in binary mode with remote host.\n");
581 printf("Negotiating binary mode with remote host.\n");
585 if (my_want_state_is_wont(TELOPT_BINARY) &&
586 my_want_state_is_dont(TELOPT_BINARY)) {
587 printf("Already in network ascii mode with remote host.\n");
589 printf("Negotiating network ascii mode with remote host.\n");
599 donebinarytoggle = 1;
602 val = my_want_state_is_do(TELOPT_BINARY) ? 0 : 1;
605 if (my_want_state_is_do(TELOPT_BINARY)) {
606 printf("Already receiving in binary mode.\n");
608 printf("Negotiating binary mode on input.\n");
612 if (my_want_state_is_dont(TELOPT_BINARY)) {
613 printf("Already receiving in network ascii mode.\n");
615 printf("Negotiating network ascii mode on input.\n");
625 donebinarytoggle = 1;
628 val = my_want_state_is_will(TELOPT_BINARY) ? 0 : 1;
631 if (my_want_state_is_will(TELOPT_BINARY)) {
632 printf("Already transmitting in binary mode.\n");
634 printf("Negotiating binary mode on output.\n");
638 if (my_want_state_is_wont(TELOPT_BINARY)) {
639 printf("Already transmitting in network ascii mode.\n");
641 printf("Negotiating network ascii mode on output.\n");
649 const char *name; /* name of toggle */
650 const char *help; /* help message */
651 int (*handler)(int); /* routine to do actual setting */
653 const char *actionexplanation;
656 static struct togglelist Togglelist[] = {
658 "flushing of output when sending interrupt characters",
661 "flush output when sending interrupt characters" },
663 "automatic sending of interrupt characters in urgent mode",
666 "send interrupt characters in urgent mode" },
667 #ifdef AUTHENTICATION
669 "automatic sending of login and/or authentication info",
672 "send login name and/or authentication information" },
674 "Toggle authentication debugging",
677 "print authentication debugging information" },
681 "automatic encryption of data stream",
684 "automatically encrypt output" },
686 "automatic decryption of data stream",
689 "automatically decrypt input" },
691 "Toggle verbose encryption output",
694 "print verbose encryption output" },
696 "Toggle encryption debugging",
699 "print encryption debugging information" },
700 #endif /* ENCRYPTION */
702 "don't read ~/.telnetrc file",
705 "skip reading of ~/.telnetrc file" },
707 "sending and receiving of binary data",
712 "receiving of binary data",
717 "sending of binary data",
722 "sending carriage returns as telnet <CR><LF>",
723 (int (*)(int))togcrlf,
727 "mapping of received carriage returns",
730 "map carriage return on output" },
732 "local recognition of certain control characters",
733 (int (*)(int))lclchars,
735 "recognize certain control characters" },
736 { " ", "", NULL, NULL, NULL }, /* empty line */
739 (int (*)(int))togdebug,
741 "turn on socket level debugging" },
743 "printing of hexadecimal network data (debugging)",
746 "print hexadecimal representation of network traffic" },
748 "output of \"netdata\" to user readable format (debugging)",
751 "print user readable output for \"netdata\"" },
753 "viewing of options processing (debugging)",
756 "show option processing" },
758 "(debugging) toggle printing of hexadecimal terminal data",
761 "print hexadecimal representation of terminal traffic" },
764 (int (*)(int))togglehelp,
767 { NULL, NULL, NULL, NULL, NULL },
770 (int (*)(int))togglehelp,
773 { NULL, NULL, NULL, NULL, NULL }
779 struct togglelist *c;
781 for (c = Togglelist; c->name; c++) {
784 printf("%-15s toggle %s\n", c->name, c->help);
790 printf("%-15s %s\n", "?", "display help information");
795 settogglehelp(int set)
797 struct togglelist *c;
799 for (c = Togglelist; c->name; c++) {
802 printf("%-15s %s %s\n", c->name, set ? "enable" : "disable",
810 #define GETTOGGLE(name) (struct togglelist *) \
811 genget(name, (char **) Togglelist, sizeof(struct togglelist))
814 toggle(int argc, char *argv[])
818 struct togglelist *c;
822 "Need an argument to 'toggle' command. 'toggle ?' for help.\n");
830 if (Ambiguous((void *)c)) {
831 fprintf(stderr, "'%s': ambiguous argument ('toggle ?' for help).\n",
835 fprintf(stderr, "'%s': unknown argument ('toggle ?' for help).\n",
840 *c->variable = !*c->variable; /* invert it */
841 if (c->actionexplanation) {
842 printf("%s %s.\n", *c->variable? "Will" : "Won't",
843 c->actionexplanation);
847 retval &= (*c->handler)(-1);
855 * The following perform the "set" command.
859 struct termio new_tc = { 0, 0, 0, 0, {}, 0, 0 };
863 const char *name; /* name */
864 const char *help; /* help information */
865 void (*handler)(char *);
866 cc_t *charp; /* where it is located at */
869 static struct setlist Setlist[] = {
870 #ifdef KLUDGELINEMODE
871 { "echo", "character to toggle local echoing on/off", NULL, &echoc },
873 { "escape", "character to escape back to telnet command mode", NULL, &escape },
874 { "rlogin", "rlogin escape character", 0, &rlogin },
875 { "tracefile", "file to write trace information to", SetNetTrace, (cc_t *)NetTraceFile},
876 { " ", "", NULL, NULL },
877 { " ", "The following need 'localchars' to be toggled true", NULL, NULL },
878 { "flushoutput", "character to cause an Abort Output", NULL, termFlushCharp },
879 { "interrupt", "character to cause an Interrupt Process", NULL, termIntCharp },
880 { "quit", "character to cause an Abort process", NULL, termQuitCharp },
881 { "eof", "character to cause an EOF ", NULL, termEofCharp },
882 { " ", "", NULL, NULL },
883 { " ", "The following are for local editing in linemode", NULL, NULL },
884 { "erase", "character to use to erase a character", NULL, termEraseCharp },
885 { "kill", "character to use to erase a line", NULL, termKillCharp },
886 { "lnext", "character to use for literal next", NULL, termLiteralNextCharp },
887 { "susp", "character to cause a Suspend Process", NULL, termSuspCharp },
888 { "reprint", "character to use for line reprint", NULL, termRprntCharp },
889 { "worderase", "character to use to erase a word", NULL, termWerasCharp },
890 { "start", "character to use for XON", NULL, termStartCharp },
891 { "stop", "character to use for XOFF", NULL, termStopCharp },
892 { "forw1", "alternate end of line character", NULL, termForw1Charp },
893 { "forw2", "alternate end of line character", NULL, termForw2Charp },
894 { "ayt", "alternate AYT character", NULL, termAytCharp },
895 { NULL, NULL, NULL, NULL }
898 static struct setlist *
901 return (struct setlist *)
902 genget(name, (char **) Setlist, sizeof(struct setlist));
906 set_escape_char(char *s)
908 if (rlogin != _POSIX_VDISABLE) {
909 rlogin = (s && *s) ? special(s) : _POSIX_VDISABLE;
910 printf("Telnet rlogin escape character is '%s'.\n",
913 escape = (s && *s) ? special(s) : _POSIX_VDISABLE;
914 printf("Telnet escape character is '%s'.\n", control(escape));
919 setcmd(int argc, char *argv[])
923 struct togglelist *c;
925 if (argc < 2 || argc > 3) {
926 printf("Format is 'set Name Value'\n'set ?' for help.\n");
929 if ((argc == 2) && (isprefix(argv[1], "?") || isprefix(argv[1], "help"))) {
930 for (ct = Setlist; ct->name; ct++)
931 printf("%-15s %s\n", ct->name, ct->help);
934 printf("%-15s %s\n", "?", "display help information");
938 ct = getset(argv[1]);
940 c = GETTOGGLE(argv[1]);
942 fprintf(stderr, "'%s': unknown argument ('set ?' for help).\n",
945 } else if (Ambiguous((void *)c)) {
946 fprintf(stderr, "'%s': ambiguous argument ('set ?' for help).\n",
951 if ((argc == 2) || (strcmp("on", argv[2]) == 0))
953 else if (strcmp("off", argv[2]) == 0)
956 printf("Format is 'set togglename [on|off]'\n'set ?' for help.\n");
959 if (c->actionexplanation) {
960 printf("%s %s.\n", *c->variable? "Will" : "Won't",
961 c->actionexplanation);
966 } else if (argc != 3) {
967 printf("Format is 'set Name Value'\n'set ?' for help.\n");
969 } else if (Ambiguous((void *)ct)) {
970 fprintf(stderr, "'%s': ambiguous argument ('set ?' for help).\n",
973 } else if (ct->handler) {
974 (*ct->handler)(argv[2]);
975 printf("%s set to \"%s\".\n", ct->name, (char *)ct->charp);
977 if (strcmp("off", argv[2])) {
978 value = special(argv[2]);
980 value = _POSIX_VDISABLE;
982 *(ct->charp) = (cc_t)value;
983 printf("%s character is '%s'.\n", ct->name, control(*(ct->charp)));
990 unsetcmd(int argc, char *argv[])
993 struct togglelist *c;
998 "Need an argument to 'unset' command. 'unset ?' for help.\n");
1001 if (isprefix(argv[1], "?") || isprefix(argv[1], "help")) {
1002 for (ct = Setlist; ct->name; ct++)
1003 printf("%-15s %s\n", ct->name, ct->help);
1006 printf("%-15s %s\n", "?", "display help information");
1016 c = GETTOGGLE(name);
1018 fprintf(stderr, "'%s': unknown argument ('unset ?' for help).\n",
1021 } else if (Ambiguous((void *)c)) {
1022 fprintf(stderr, "'%s': ambiguous argument ('unset ?' for help).\n",
1028 if (c->actionexplanation) {
1029 printf("%s %s.\n", *c->variable? "Will" : "Won't",
1030 c->actionexplanation);
1035 } else if (Ambiguous((void *)ct)) {
1036 fprintf(stderr, "'%s': ambiguous argument ('unset ?' for help).\n",
1039 } else if (ct->handler) {
1041 printf("%s reset to \"%s\".\n", ct->name, (char *)ct->charp);
1043 *(ct->charp) = _POSIX_VDISABLE;
1044 printf("%s character is '%s'.\n", ct->name, control(*(ct->charp)));
1051 * The following are the data structures and routines for the
1054 #ifdef KLUDGELINEMODE
1055 extern int kludgelinemode;
1061 send_wont(TELOPT_LINEMODE, 1);
1062 send_dont(TELOPT_SGA, 1);
1063 send_dont(TELOPT_ECHO, 1);
1071 #ifdef KLUDGELINEMODE
1073 send_dont(TELOPT_SGA, 1);
1075 send_will(TELOPT_LINEMODE, 1);
1076 send_dont(TELOPT_ECHO, 1);
1083 #ifdef KLUDGELINEMODE
1085 send_do(TELOPT_SGA, 1);
1088 send_wont(TELOPT_LINEMODE, 1);
1089 send_do(TELOPT_ECHO, 1);
1094 dolmmode(int bit, int on)
1097 extern int linemode;
1099 if (my_want_state_is_wont(TELOPT_LINEMODE)) {
1100 printf("?Need to have LINEMODE option enabled first.\n");
1101 printf("'mode ?' for help.\n");
1106 c = (linemode | bit);
1108 c = (linemode & ~bit);
1116 return dolmmode(bit, 1);
1122 return dolmmode(bit, 0);
1126 const char *name; /* command name */
1127 const char *help; /* help string */
1128 int (*handler)(int);/* routine which executes command */
1129 int needconnect; /* Do we need to be connected to execute? */
1133 static struct modelist ModeList[] = {
1134 { "character", "Disable LINEMODE option", (int (*)(int))docharmode, 1, 0 },
1135 #ifdef KLUDGELINEMODE
1136 { "", "(or disable obsolete line-by-line mode)", NULL, 0, 0 },
1138 { "line", "Enable LINEMODE option", (int (*)(int))dolinemode, 1, 0 },
1139 #ifdef KLUDGELINEMODE
1140 { "", "(or enable obsolete line-by-line mode)", NULL, 0, 0 },
1142 { "", "", NULL, 0, 0 },
1143 { "", "These require the LINEMODE option to be enabled", NULL, 0, 0 },
1144 { "isig", "Enable signal trapping", setmod, 1, MODE_TRAPSIG },
1145 { "+isig", 0, setmod, 1, MODE_TRAPSIG },
1146 { "-isig", "Disable signal trapping", clearmode, 1, MODE_TRAPSIG },
1147 { "edit", "Enable character editing", setmod, 1, MODE_EDIT },
1148 { "+edit", 0, setmod, 1, MODE_EDIT },
1149 { "-edit", "Disable character editing", clearmode, 1, MODE_EDIT },
1150 { "softtabs", "Enable tab expansion", setmod, 1, MODE_SOFT_TAB },
1151 { "+softtabs", 0, setmod, 1, MODE_SOFT_TAB },
1152 { "-softtabs", "Disable character editing", clearmode, 1, MODE_SOFT_TAB },
1153 { "litecho", "Enable literal character echo", setmod, 1, MODE_LIT_ECHO },
1154 { "+litecho", 0, setmod, 1, MODE_LIT_ECHO },
1155 { "-litecho", "Disable literal character echo", clearmode, 1, MODE_LIT_ECHO },
1156 { "help", 0, (int (*)(int))modehelp, 0, 0 },
1157 #ifdef KLUDGELINEMODE
1158 { "kludgeline", 0, (int (*)(int))dokludgemode, 1, 0 },
1160 { "", "", NULL, 0, 0 },
1161 { "?", "Print help information", (int (*)(int))modehelp, 0, 0 },
1162 { NULL, NULL, NULL, 0, 0 },
1169 struct modelist *mt;
1171 printf("format is: 'mode Mode', where 'Mode' is one of:\n\n");
1172 for (mt = ModeList; mt->name; mt++) {
1175 printf("%-15s %s\n", mt->name, mt->help);
1183 #define GETMODECMD(name) (struct modelist *) \
1184 genget(name, (char **) ModeList, sizeof(struct modelist))
1187 modecmd(int argc, char *argv[])
1189 struct modelist *mt;
1192 printf("'mode' command requires an argument\n");
1193 printf("'mode ?' for help.\n");
1194 } else if ((mt = GETMODECMD(argv[1])) == 0) {
1195 fprintf(stderr, "Unknown mode '%s' ('mode ?' for help).\n", argv[1]);
1196 } else if (Ambiguous((void *)mt)) {
1197 fprintf(stderr, "Ambiguous mode '%s' ('mode ?' for help).\n", argv[1]);
1198 } else if (mt->needconnect && !connected) {
1199 printf("?Need to be connected first.\n");
1200 printf("'mode ?' for help.\n");
1201 } else if (mt->handler) {
1202 return (*mt->handler)(mt->arg1);
1208 * The following data structures and routines implement the
1209 * "display" command.
1213 display(int argc, char *argv[])
1215 struct togglelist *tl;
1218 #define dotog(tl) if (tl->variable && tl->actionexplanation) { \
1219 if (*tl->variable) { \
1224 printf(" %s.\n", tl->actionexplanation); \
1227 #define doset(sl) if (sl->name && *sl->name != ' ') { \
1228 if (sl->handler == 0) \
1229 printf("%-15s [%s]\n", sl->name, control(*sl->charp)); \
1231 printf("%-15s \"%s\"\n", sl->name, (char *)sl->charp); \
1235 for (tl = Togglelist; tl->name; tl++) {
1239 for (sl = Setlist; sl->name; sl++) {
1245 for (i = 1; i < argc; i++) {
1246 sl = getset(argv[i]);
1247 tl = GETTOGGLE(argv[i]);
1248 if (Ambiguous((void *)sl) || Ambiguous((void *)tl)) {
1249 printf("?Ambiguous argument '%s'.\n", argv[i]);
1251 } else if (!sl && !tl) {
1252 printf("?Unknown argument '%s'.\n", argv[i]);
1264 /*@*/optionstatus();
1267 #endif /* ENCRYPTION */
1274 * The following are the data structures, and many of the routines,
1275 * relating to command processing.
1279 * Set the escape character.
1282 setescape(int argc, char *argv[])
1288 "Deprecated usage - please use 'set escape%s%s' in the future.\n",
1289 (argc > 2)? " ":"", (argc > 2)? argv[1]: "");
1293 printf("new escape character: ");
1294 (void) fgets(buf, sizeof(buf), stdin);
1299 (void) fflush(stdout);
1307 printf("Deprecated usage - please use 'toggle crmod' in the future.\n");
1308 printf("%s map carriage return on output.\n", crmod ? "Will" : "Won't");
1309 (void) fflush(stdout);
1319 long oldrows, oldcols, newrows, newcols, err_;
1321 err_ = (TerminalWindowSize(&oldrows, &oldcols) == 0) ? 1 : 0;
1322 (void) kill(0, SIGTSTP);
1324 * If we didn't get the window size before the SUSPEND, but we
1325 * can get them now (?), then send the NAWS to make sure that
1326 * we are set up for the right window size.
1328 if (TerminalWindowSize(&newrows, &newcols) && connected &&
1329 (err_ || ((oldrows != newrows) || (oldcols != newcols)))) {
1333 /* reget parameters in case they were changed */
1334 TerminalSaveState();
1337 printf("Suspend is not supported. Try the '!' command instead\n");
1343 shell(int argc, char *argv[] __unused)
1345 long oldrows, oldcols, newrows, newcols, err_;
1349 err_ = (TerminalWindowSize(&oldrows, &oldcols) == 0) ? 1 : 0;
1352 perror("Fork failed\n");
1358 * Fire up the shell in the child.
1360 const char *shellp, *shellname;
1362 shellp = getenv("SHELL");
1365 if ((shellname = strrchr(shellp, '/')) == 0)
1370 execl(shellp, shellname, "-c", &saveline[1], (char *)0);
1372 execl(shellp, shellname, (char *)0);
1377 (void)wait((int *)0); /* Wait for the shell to complete */
1379 if (TerminalWindowSize(&newrows, &newcols) && connected &&
1380 (err_ || ((oldrows != newrows) || (oldcols != newcols)))) {
1389 bye(int argc, char *argv[])
1391 extern int resettermname;
1394 (void) shutdown(net, 2);
1395 printf("Connection closed.\n");
1396 (void) NetClose(net);
1399 #ifdef AUTHENTICATION
1401 auth_encrypt_connect(connected);
1407 if ((argc != 2) || (strcmp(argv[1], "fromquit") != 0)) {
1408 longjmp(toplevel, 1);
1411 return 1; /* Keep lint, etc., happy */
1417 (void) call(bye, "bye", "fromquit", 0);
1424 send_do(TELOPT_LOGOUT, 1);
1437 void (*handler)(int);
1441 static void slc_help(void);
1443 struct slclist SlcList[] = {
1444 { "export", "Use local special character definitions",
1445 (void (*)(int))slc_mode_export, 0 },
1446 { "import", "Use remote special character definitions",
1447 slc_mode_import, 1 },
1448 { "check", "Verify remote special character definitions",
1449 slc_mode_import, 0 },
1450 { "help", NULL, (void (*)(int))slc_help, 0 },
1451 { "?", "Print help information", (void (*)(int))slc_help, 0 },
1452 { NULL, NULL, NULL, 0 },
1460 for (c = SlcList; c->name; c++) {
1463 printf("%-15s %s\n", c->name, c->help);
1470 static struct slclist *
1473 return (struct slclist *)
1474 genget(name, (char **) SlcList, sizeof(struct slclist));
1478 slccmd(int argc, char *argv[])
1484 "Need an argument to 'slc' command. 'slc ?' for help.\n");
1487 c = getslc(argv[1]);
1489 fprintf(stderr, "'%s': unknown argument ('slc ?' for help).\n",
1493 if (Ambiguous((void *)c)) {
1494 fprintf(stderr, "'%s': ambiguous argument ('slc ?' for help).\n",
1498 (*c->handler)(c->arg);
1504 * The ENVIRON command.
1510 void (*handler)(unsigned char *, unsigned char *);
1514 extern struct env_lst *
1515 env_define(const unsigned char *, unsigned char *);
1517 env_undefine(unsigned char *),
1518 env_export(const unsigned char *),
1519 env_unexport(const unsigned char *),
1520 env_send(unsigned char *),
1521 #if defined(OLD_ENVIRON) && defined(ENV_HACK)
1522 env_varval(unsigned char *),
1528 struct envlist EnvList[] = {
1529 { "define", "Define an environment variable",
1530 (void (*)(unsigned char *, unsigned char *))env_define, 2 },
1531 { "undefine", "Undefine an environment variable",
1532 (void (*)(unsigned char *, unsigned char *))env_undefine, 1 },
1533 { "export", "Mark an environment variable for automatic export",
1534 (void (*)(unsigned char *, unsigned char *))env_export, 1 },
1535 { "unexport", "Don't mark an environment variable for automatic export",
1536 (void (*)(unsigned char *, unsigned char *))env_unexport, 1 },
1537 { "send", "Send an environment variable", (void (*)(unsigned char *, unsigned char *))env_send, 1 },
1538 { "list", "List the current environment variables",
1539 (void (*)(unsigned char *, unsigned char *))env_list, 0 },
1540 #if defined(OLD_ENVIRON) && defined(ENV_HACK)
1541 { "varval", "Reverse VAR and VALUE (auto, right, wrong, status)",
1542 (void (*)(unsigned char *, unsigned char *))env_varval, 1 },
1544 { "help", NULL, (void (*)(unsigned char *, unsigned char *))env_help, 0 },
1545 { "?", "Print help information", (void (*)(unsigned char *, unsigned char *))env_help, 0 },
1546 { NULL, NULL, NULL, 0 },
1554 for (c = EnvList; c->name; c++) {
1557 printf("%-15s %s\n", c->name, c->help);
1564 static struct envlist *
1565 getenvcmd(char *name)
1567 return (struct envlist *)
1568 genget(name, (char **) EnvList, sizeof(struct envlist));
1572 env_cmd(int argc, char *argv[])
1578 "Need an argument to 'environ' command. 'environ ?' for help.\n");
1581 c = getenvcmd(argv[1]);
1583 fprintf(stderr, "'%s': unknown argument ('environ ?' for help).\n",
1587 if (Ambiguous((void *)c)) {
1588 fprintf(stderr, "'%s': ambiguous argument ('environ ?' for help).\n",
1592 if (c->narg + 2 != argc) {
1594 "Need %s%d argument%s to 'environ %s' command. 'environ ?' for help.\n",
1595 c->narg < argc + 2 ? "only " : "",
1596 c->narg, c->narg == 1 ? "" : "s", c->name);
1599 (*c->handler)(argv[2], argv[3]);
1604 struct env_lst *next; /* pointer to next structure */
1605 struct env_lst *prev; /* pointer to previous structure */
1606 unsigned char *var; /* pointer to variable name */
1607 unsigned char *value; /* pointer to variable value */
1608 int export; /* 1 -> export with default list of variables */
1609 int welldefined; /* A well defined variable */
1612 struct env_lst envlisthead;
1614 static struct env_lst *
1615 env_find(const unsigned char *var)
1619 for (ep = envlisthead.next; ep; ep = ep->next) {
1620 if (strcmp(ep->var, var) == 0)
1629 extern char **environ;
1633 for (epp = environ; *epp; epp++) {
1634 if ((cp = strchr(*epp, '='))) {
1636 ep = env_define((unsigned char *)*epp,
1637 (unsigned char *)cp+1);
1643 * Special case for DISPLAY variable. If it is ":0.0" or
1644 * "unix:0.0", we have to get rid of "unix" and insert our
1647 if ((ep = env_find("DISPLAY"))
1648 && ((*ep->value == ':')
1649 || (strncmp((char *)ep->value, "unix:", 5) == 0))) {
1651 char *cp2 = strchr((char *)ep->value, ':');
1653 gethostname(hbuf, 256);
1655 cp = (char *)malloc(strlen(hbuf) + strlen(cp2) + 1);
1656 sprintf((char *)cp, "%s%s", hbuf, cp2);
1658 ep->value = (unsigned char *)cp;
1661 * If USER is not defined, but LOGNAME is, then add
1662 * USER with the value from LOGNAME. By default, we
1663 * don't export the USER variable.
1665 if ((env_find("USER") == NULL) && (ep = env_find("LOGNAME"))) {
1666 env_define("USER", ep->value);
1667 env_unexport("USER");
1669 env_export("DISPLAY");
1670 env_export("PRINTER");
1674 env_define(const unsigned char *var, unsigned char *value)
1678 if ((ep = env_find(var))) {
1684 ep = (struct env_lst *)malloc(sizeof(struct env_lst));
1685 ep->next = envlisthead.next;
1686 envlisthead.next = ep;
1687 ep->prev = &envlisthead;
1689 ep->next->prev = ep;
1691 ep->welldefined = opt_welldefined(var);
1693 ep->var = strdup(var);
1694 ep->value = strdup(value);
1699 env_undefine(unsigned char *var)
1703 if ((ep = env_find(var))) {
1704 ep->prev->next = ep->next;
1706 ep->next->prev = ep->prev;
1716 env_export(const unsigned char *var)
1720 if ((ep = env_find(var)))
1725 env_unexport(const unsigned char *var)
1729 if ((ep = env_find(var)))
1734 env_send(unsigned char *var)
1738 if (my_state_is_wont(TELOPT_NEW_ENVIRON)
1740 && my_state_is_wont(TELOPT_OLD_ENVIRON)
1744 "Cannot send '%s': Telnet ENVIRON option not enabled\n",
1750 fprintf(stderr, "Cannot send '%s': variable not defined\n",
1754 env_opt_start_info();
1755 env_opt_add(ep->var);
1764 for (ep = envlisthead.next; ep; ep = ep->next) {
1765 printf("%c %-20s %s\n", ep->export ? '*' : ' ',
1766 ep->var, ep->value);
1771 env_default(int init, int welldefined)
1773 static struct env_lst *nep = NULL;
1780 while ((nep = nep->next)) {
1781 if (nep->export && (nep->welldefined == welldefined))
1789 env_getvalue(const unsigned char *var)
1793 if ((ep = env_find(var)))
1798 #if defined(OLD_ENVIRON) && defined(ENV_HACK)
1800 env_varval(unsigned char *what)
1802 extern int old_env_var, old_env_value, env_auto;
1803 int len = strlen((char *)what);
1808 if (strncasecmp((char *)what, "status", len) == 0) {
1810 printf("%s%s", "VAR and VALUE are/will be ",
1811 "determined automatically\n");
1812 if (old_env_var == OLD_ENV_VAR)
1813 printf("VAR and VALUE set to correct definitions\n");
1815 printf("VAR and VALUE definitions are reversed\n");
1816 } else if (strncasecmp((char *)what, "auto", len) == 0) {
1818 old_env_var = OLD_ENV_VALUE;
1819 old_env_value = OLD_ENV_VAR;
1820 } else if (strncasecmp((char *)what, "right", len) == 0) {
1822 old_env_var = OLD_ENV_VAR;
1823 old_env_value = OLD_ENV_VALUE;
1824 } else if (strncasecmp((char *)what, "wrong", len) == 0) {
1826 old_env_var = OLD_ENV_VALUE;
1827 old_env_value = OLD_ENV_VAR;
1830 printf("Unknown \"varval\" command. (\"auto\", \"right\", \"wrong\", \"status\")\n");
1835 #ifdef AUTHENTICATION
1837 * The AUTHENTICATE command.
1843 int (*handler)(char *);
1848 auth_enable(char *),
1849 auth_disable(char *),
1854 struct authlist AuthList[] = {
1855 { "status", "Display current status of authentication information",
1856 (int (*)(char *))auth_status, 0 },
1857 { "disable", "Disable an authentication type ('auth disable ?' for more)",
1859 { "enable", "Enable an authentication type ('auth enable ?' for more)",
1861 { "help", NULL, (int (*)(char *))auth_help, 0 },
1862 { "?", "Print help information", (int (*)(char *))auth_help, 0 },
1863 { NULL, NULL, NULL, 0 },
1871 for (c = AuthList; c->name; c++) {
1874 printf("%-15s %s\n", c->name, c->help);
1883 auth_cmd(int argc, char *argv[])
1889 "Need an argument to 'auth' command. 'auth ?' for help.\n");
1893 c = (struct authlist *)
1894 genget(argv[1], (char **) AuthList, sizeof(struct authlist));
1896 fprintf(stderr, "'%s': unknown argument ('auth ?' for help).\n",
1900 if (Ambiguous((void *)c)) {
1901 fprintf(stderr, "'%s': ambiguous argument ('auth ?' for help).\n",
1905 if (c->narg + 2 != argc) {
1907 "Need %s%d argument%s to 'auth %s' command. 'auth ?' for help.\n",
1908 c->narg < argc + 2 ? "only " : "",
1909 c->narg, c->narg == 1 ? "" : "s", c->name);
1912 return((*c->handler)(argv[2]));
1918 * The ENCRYPT command.
1921 struct encryptlist {
1924 int (*handler)(char *, char *);
1931 EncryptEnable(char *, char *),
1932 EncryptDisable(char *, char *),
1933 EncryptType(char *, char *),
1934 EncryptStart(char *),
1935 EncryptStartInput(void),
1936 EncryptStartOutput(void),
1937 EncryptStop(char *),
1938 EncryptStopInput(void),
1939 EncryptStopOutput(void),
1940 EncryptStatus(void);
1944 struct encryptlist EncryptList[] = {
1945 { "enable", "Enable encryption. ('encrypt enable ?' for more)",
1946 EncryptEnable, 1, 1, 2 },
1947 { "disable", "Disable encryption. ('encrypt enable ?' for more)",
1948 EncryptDisable, 0, 1, 2 },
1949 { "type", "Set encryption type. ('encrypt type ?' for more)",
1950 EncryptType, 0, 1, 1 },
1951 { "start", "Start encryption. ('encrypt start ?' for more)",
1952 (int (*)(char *, char *))EncryptStart, 1, 0, 1 },
1953 { "stop", "Stop encryption. ('encrypt stop ?' for more)",
1954 (int (*)(char *, char *))EncryptStop, 1, 0, 1 },
1955 { "input", "Start encrypting the input stream",
1956 (int (*)(char *, char *))EncryptStartInput, 1, 0, 0 },
1957 { "-input", "Stop encrypting the input stream",
1958 (int (*)(char *, char *))EncryptStopInput, 1, 0, 0 },
1959 { "output", "Start encrypting the output stream",
1960 (int (*)(char *, char *))EncryptStartOutput, 1, 0, 0 },
1961 { "-output", "Stop encrypting the output stream",
1962 (int (*)(char *, char *))EncryptStopOutput, 1, 0, 0 },
1964 { "status", "Display current status of authentication information",
1965 (int (*)(char *, char *))EncryptStatus, 0, 0, 0 },
1966 { "help", NULL, (int (*)(char *, char *))EncryptHelp, 0, 0, 0 },
1967 { "?", "Print help information", (int (*)(char *, char *))EncryptHelp, 0, 0, 0 },
1968 { NULL, NULL, NULL, 0, 0, 0 },
1974 struct encryptlist *c;
1976 for (c = EncryptList; c->name; c++) {
1979 printf("%-15s %s\n", c->name, c->help);
1988 encrypt_cmd(int argc, char *argv[])
1990 struct encryptlist *c;
1994 "Need an argument to 'encrypt' command. 'encrypt ?' for help.\n");
1998 c = (struct encryptlist *)
1999 genget(argv[1], (char **) EncryptList, sizeof(struct encryptlist));
2001 fprintf(stderr, "'%s': unknown argument ('encrypt ?' for help).\n",
2005 if (Ambiguous((void *)c)) {
2006 fprintf(stderr, "'%s': ambiguous argument ('encrypt ?' for help).\n",
2011 if (argc < c->minarg || argc > c->maxarg) {
2012 if (c->minarg == c->maxarg) {
2013 fprintf(stderr, "Need %s%d argument%s ",
2014 c->minarg < argc ? "only " : "", c->minarg,
2015 c->minarg == 1 ? "" : "s");
2017 fprintf(stderr, "Need %s%d-%d arguments ",
2018 c->maxarg < argc ? "only " : "", c->minarg, c->maxarg);
2020 fprintf(stderr, "to 'encrypt %s' command. 'encrypt ?' for help.\n",
2024 if (c->needconnect && !connected) {
2025 if (!(argc && (isprefix(argv[2], "help") || isprefix(argv[2], "?")))) {
2026 printf("?Need to be connected first.\n");
2030 return ((*c->handler)(argc > 0 ? argv[2] : 0,
2031 argc > 1 ? argv[3] : 0));
2033 #endif /* ENCRYPTION */
2036 * Print status about the connection.
2040 status(int argc, char *argv[])
2043 printf("Connected to %s.\n", hostname);
2044 if ((argc < 2) || strcmp(argv[1], "notmuch")) {
2045 int mode = getconnmode();
2047 if (my_want_state_is_will(TELOPT_LINEMODE)) {
2048 printf("Operating with LINEMODE option\n");
2049 printf("%s line editing\n", (mode&MODE_EDIT) ? "Local" : "No");
2050 printf("%s catching of signals\n",
2051 (mode&MODE_TRAPSIG) ? "Local" : "No");
2053 #ifdef KLUDGELINEMODE
2054 } else if (kludgelinemode && my_want_state_is_dont(TELOPT_SGA)) {
2055 printf("Operating in obsolete linemode\n");
2058 printf("Operating in single character mode\n");
2060 printf("Catching signals locally\n");
2062 printf("%s character echo\n", (mode&MODE_ECHO) ? "Local" : "Remote");
2063 if (my_want_state_is_will(TELOPT_LFLOW))
2064 printf("%s flow control\n", (mode&MODE_FLOW) ? "Local" : "No");
2067 #endif /* ENCRYPTION */
2070 printf("No connection.\n");
2072 printf("Escape character is '%s'.\n", control(escape));
2073 (void) fflush(stdout);
2079 * Function that gets called when SIGINFO is received.
2084 (void) call(status, "status", "notmuch", 0);
2089 sockaddr_ntop(struct sockaddr *sa)
2092 static char addrbuf[INET6_ADDRSTRLEN];
2094 switch (sa->sa_family) {
2096 addr = &((struct sockaddr_in *)sa)->sin_addr;
2099 addr = &((struct sockaddr_un *)sa)->sun_path;
2103 addr = &((struct sockaddr_in6 *)sa)->sin6_addr;
2109 inet_ntop(sa->sa_family, addr, addrbuf, sizeof(addrbuf));
2113 #if defined(IPSEC) && defined(IPSEC_POLICY_IPSEC)
2115 setpolicy(int lnet, struct addrinfo *res, char *policy)
2124 buf = ipsec_set_policy(policy, strlen(policy));
2126 printf("%s\n", ipsec_strerror());
2129 level = res->ai_family == AF_INET ? IPPROTO_IP : IPPROTO_IPV6;
2130 optname = res->ai_family == AF_INET ? IP_IPSEC_POLICY : IPV6_IPSEC_POLICY;
2131 if (setsockopt(lnet, level, optname, buf, ipsec_get_policylen(buf)) < 0){
2132 perror("setsockopt");
2143 * When an Address Family related error happend, check if retry with
2144 * another AF is possible or not.
2145 * Return 1, if retry with another af is OK. Else, return 0.
2148 switch_af(struct addrinfo **aip)
2151 struct addrinfo *ai;
2154 nextaf = (ai->ai_family == AF_INET) ? AF_INET6 : AF_INET;
2157 while (ai != NULL && ai->ai_family != nextaf);
2167 tn(int argc, char *argv[])
2172 int srcroute = 0, result;
2173 char *cmd, *hostp = 0, *portp = 0, *user = 0;
2174 char *src_addr = NULL;
2175 struct addrinfo hints, *res, *res0 = NULL, *src_res, *src_res0 = NULL;
2176 int error = 0, af_error = 0;
2179 printf("?Already connected to %s\n", hostname);
2184 (void) strcpy(line, "open ");
2186 (void) fgets(&line[strlen(line)], sizeof(line) - strlen(line), stdin);
2194 if (strcmp(*argv, "help") == 0 || isprefix(*argv, "?"))
2196 if (strcmp(*argv, "-l") == 0) {
2204 if (strcmp(*argv, "-a") == 0) {
2209 if (strcmp(*argv, "-s") == 0) {
2228 printf("usage: %s [-l user] [-a] [-s src_addr] host-name [port]\n", cmd);
2235 if (src_addr != NULL) {
2236 memset(&hints, 0, sizeof(hints));
2237 hints.ai_family = family;
2238 hints.ai_socktype = SOCK_STREAM;
2239 error = getaddrinfo(src_addr, 0, &hints, &src_res);
2240 if (error == EAI_NODATA) {
2242 error = getaddrinfo(src_addr, 0, &hints, &src_res);
2245 fprintf(stderr, "%s: %s\n", src_addr, gai_strerror(error));
2246 if (error == EAI_SYSTEM)
2247 fprintf(stderr, "%s: %s\n", src_addr, strerror(errno));
2253 if (hostp[0] == '/') {
2254 struct sockaddr_un su;
2256 if (strlen(hostp) >= sizeof(su.sun_path)) {
2257 fprintf(stderr, "hostname too long for unix domain socket: %s",
2261 memset(&su, 0, sizeof su);
2262 su.sun_family = AF_UNIX;
2263 strncpy(su.sun_path, hostp, sizeof su.sun_path);
2264 printf("Trying %s...\n", hostp);
2265 net = socket(PF_UNIX, SOCK_STREAM, 0);
2270 if (connect(net, (struct sockaddr *)&su, sizeof su) == -1) {
2271 perror(su.sun_path);
2272 (void) NetClose(net);
2276 } else if (hostp[0] == '@' || hostp[0] == '!') {
2279 family == AF_INET6 ||
2281 (hostname = strrchr(hostp, ':')) == NULL)
2282 hostname = strrchr(hostp, '@');
2283 if (hostname == NULL) {
2293 portp = strdup("telnet");
2294 } else if (*portp == '-') {
2300 memset(&hints, 0, sizeof(hints));
2301 hints.ai_flags = AI_NUMERICHOST;
2302 hints.ai_family = family;
2303 hints.ai_socktype = SOCK_STREAM;
2304 error = getaddrinfo(hostname, portp, &hints, &res);
2306 hints.ai_flags = AI_CANONNAME;
2307 error = getaddrinfo(hostname, portp, &hints, &res);
2310 fprintf(stderr, "%s: %s\n", hostname, gai_strerror(error));
2311 if (error == EAI_SYSTEM)
2312 fprintf(stderr, "%s: %s\n", hostname, strerror(errno));
2316 if (hints.ai_flags == AI_NUMERICHOST) {
2317 /* hostname has numeric */
2321 gni_err = getnameinfo(res->ai_addr, res->ai_addr->sa_len,
2322 _hostname, sizeof(_hostname) - 1, NULL, 0,
2325 (void) strncpy(_hostname, hostp, sizeof(_hostname) - 1);
2326 _hostname[sizeof(_hostname)-1] = '\0';
2327 hostname = _hostname;
2329 /* hostname has FQDN */
2331 (void) strncpy(_hostname, hostname, sizeof(_hostname) - 1);
2332 else if (res->ai_canonname != NULL)
2333 strcpy(_hostname, res->ai_canonname);
2335 (void) strncpy(_hostname, hostp, sizeof(_hostname) - 1);
2336 _hostname[sizeof(_hostname)-1] = '\0';
2337 hostname = _hostname;
2343 if (srcroute != 0) {
2344 static char hostbuf[BUFSIZ];
2346 if (af_error == 0) { /* save intermediate hostnames for retry */
2347 strncpy(hostbuf, hostp, BUFSIZ - 1);
2348 hostbuf[BUFSIZ - 1] = '\0';
2352 result = sourceroute(res, hostp, &srp, &srlen, &proto, &opt);
2355 if (family == AF_UNSPEC && af_error == 0 &&
2356 switch_af(&res) == 1) {
2363 } else if (result == -1) {
2364 printf("Bad source route option: %s\n", hostp);
2370 printf("Trying %s...\n", sockaddr_ntop(res->ai_addr));
2371 net = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
2375 if (family == AF_UNSPEC && af_error == 0 &&
2376 switch_af(&res) == 1) {
2381 perror("telnet: socket");
2384 if (srp && setsockopt(net, proto, opt, (char *)srp, srlen) < 0)
2385 perror("setsockopt (source route)");
2386 #if defined(IPPROTO_IP) && defined(IP_TOS)
2387 if (res->ai_family == PF_INET) {
2388 # if defined(HAS_GETTOS)
2390 if (tos < 0 && (tp = gettosbyname("telnet", "tcp")))
2394 tos = IPTOS_LOWDELAY;
2396 && (setsockopt(net, IPPROTO_IP, IP_TOS,
2397 (char *)&tos, sizeof(int)) < 0)
2398 && (errno != ENOPROTOOPT))
2399 perror("telnet: setsockopt (IP_TOS) (ignored)");
2401 #endif /* defined(IPPROTO_IP) && defined(IP_TOS) */
2403 if (debug && SetSockOpt(net, SOL_SOCKET, SO_DEBUG, 1) < 0) {
2404 perror("setsockopt (SO_DEBUG)");
2407 if (src_addr != NULL) {
2408 for (src_res = src_res0; src_res != 0; src_res = src_res->ai_next)
2409 if (src_res->ai_family == res->ai_family)
2411 if (src_res == NULL)
2413 if (bind(net, src_res->ai_addr, src_res->ai_addrlen) == -1) {
2415 if (family == AF_UNSPEC && af_error == 0 &&
2416 switch_af(&res) == 1) {
2418 (void) NetClose(net);
2423 (void) NetClose(net);
2427 #if defined(IPSEC) && defined(IPSEC_POLICY_IPSEC)
2428 if (setpolicy(net, res, ipsec_policy_in) < 0) {
2429 (void) NetClose(net);
2432 if (setpolicy(net, res, ipsec_policy_out) < 0) {
2433 (void) NetClose(net);
2438 if (connect(net, res->ai_addr, res->ai_addrlen) < 0) {
2439 struct addrinfo *next;
2441 next = res->ai_next;
2442 /* If already an af failed, only try same af. */
2444 while (next != NULL && next->ai_family != res->ai_family)
2445 next = next->ai_next;
2446 warn("connect to address %s", sockaddr_ntop(res->ai_addr));
2449 (void) NetClose(net);
2452 warnx("Unable to connect to remote host");
2453 (void) NetClose(net);
2457 #ifdef AUTHENTICATION
2459 auth_encrypt_connect(connected);
2462 } while (connected == 0);
2464 if (src_res0 != NULL)
2465 freeaddrinfo(src_res0);
2466 cmdrc(hostp, hostname);
2468 if (autologin && user == NULL) {
2471 user = getenv("USER");
2473 ((pw = getpwnam(user)) && pw->pw_uid != getuid())) {
2474 if ((pw = getpwuid(getuid())))
2481 env_define("USER", user);
2484 (void) call(status, "status", "notmuch", 0);
2485 if (setjmp(peerdied) == 0)
2487 (void) NetClose(net);
2488 ExitString("Connection closed by foreign host.\n",1);
2493 if (src_res0 != NULL)
2494 freeaddrinfo(src_res0);
2498 #define HELPINDENT (sizeof ("connect"))
2501 openhelp[] = "connect to a site",
2502 closehelp[] = "close current connection",
2503 logouthelp[] = "forcibly logout remote user and close the connection",
2504 quithelp[] = "exit telnet",
2505 statushelp[] = "print status information",
2506 helphelp[] = "print help information",
2507 sendhelp[] = "transmit special characters ('send ?' for more)",
2508 sethelp[] = "set operating parameters ('set ?' for more)",
2509 unsethelp[] = "unset operating parameters ('unset ?' for more)",
2510 togglestring[] ="toggle operating parameters ('toggle ?' for more)",
2511 slchelp[] = "change state of special charaters ('slc ?' for more)",
2512 displayhelp[] = "display operating parameters",
2513 #ifdef AUTHENTICATION
2514 authhelp[] = "turn on (off) authentication ('auth ?' for more)",
2517 encrypthelp[] = "turn on (off) encryption ('encrypt ?' for more)",
2518 #endif /* ENCRYPTION */
2519 zhelp[] = "suspend telnet",
2521 opiehelp[] = "compute response to OPIE challenge",
2523 shellhelp[] = "invoke a subshell",
2524 envhelp[] = "change environment variables ('environ ?' for more)",
2525 modestring[] = "try to enter line or character mode ('mode ?' for more)";
2527 static Command cmdtab[] = {
2528 { "close", closehelp, bye, 1 },
2529 { "logout", logouthelp, (int (*)(int, char **))logout, 1 },
2530 { "display", displayhelp, display, 0 },
2531 { "mode", modestring, modecmd, 0 },
2532 { "telnet", openhelp, tn, 0 },
2533 { "open", openhelp, tn, 0 },
2534 { "quit", quithelp, (int (*)(int, char **))quit, 0 },
2535 { "send", sendhelp, sendcmd, 0 },
2536 { "set", sethelp, setcmd, 0 },
2537 { "unset", unsethelp, unsetcmd, 0 },
2538 { "status", statushelp, status, 0 },
2539 { "toggle", togglestring, toggle, 0 },
2540 { "slc", slchelp, slccmd, 0 },
2541 #ifdef AUTHENTICATION
2542 { "auth", authhelp, auth_cmd, 0 },
2545 { "encrypt", encrypthelp, encrypt_cmd, 0 },
2546 #endif /* ENCRYPTION */
2547 { "z", zhelp, (int (*)(int, char **))suspend, 0 },
2548 { "!", shellhelp, shell, 1 },
2549 { "environ", envhelp, env_cmd, 0 },
2550 { "?", helphelp, help, 0 },
2552 { "opie", opiehelp, opie_calc, 0 },
2554 { NULL, NULL, NULL, 0 }
2557 static char crmodhelp[] = "deprecated command -- use 'toggle crmod' instead";
2558 static char escapehelp[] = "deprecated command -- use 'set escape' instead";
2560 static Command cmdtab2[] = {
2561 { "help", 0, help, 0 },
2562 { "escape", escapehelp, setescape, 0 },
2563 { "crmod", crmodhelp, (int (*)(int, char **))togcrmod, 0 },
2564 { NULL, NULL, NULL, 0 }
2569 * Call routine with argc, argv set from args (terminated by 0).
2573 call(intrtn_t routine, ...)
2579 va_start(ap, routine);
2580 while ((args[argno++] = va_arg(ap, char *)) != 0);
2582 return (*routine)(argno-1, args);
2591 if ((cm = (Command *) genget(name, (char **) cmdtab, sizeof(Command))))
2593 return (Command *) genget(name, (char **) cmdtab2, sizeof(Command));
2597 command(int top, const char *tbuf, int cnt)
2605 (void) signal(SIGINT, SIG_DFL);
2606 (void) signal(SIGQUIT, SIG_DFL);
2609 if (rlogin == _POSIX_VDISABLE)
2610 printf("%s> ", prompt);
2614 while (cnt > 0 && (*cp++ = *tbuf++) != '\n')
2617 if (cp == line || *--cp != '\n' || cp == line)
2620 if (rlogin == _POSIX_VDISABLE)
2621 printf("%s\n", line);
2624 if (rlogin != _POSIX_VDISABLE)
2625 printf("%s> ", prompt);
2626 if (fgets(line, sizeof(line), stdin) == NULL) {
2627 if (feof(stdin) || ferror(stdin)) {
2637 if (margv[0] == 0) {
2640 c = getcmd(margv[0]);
2641 if (Ambiguous((void *)c)) {
2642 printf("?Ambiguous command\n");
2646 printf("?Invalid command\n");
2649 if (c->needconnect && !connected) {
2650 printf("?Need to be connected first.\n");
2653 if ((*c->handler)(margc, margv)) {
2659 longjmp(toplevel, 1);
2670 help(int argc, char *argv[])
2675 printf("Commands may be abbreviated. Commands are:\n\n");
2676 for (c = cmdtab; c->name; c++)
2678 printf("%-*s\t%s\n", (int)HELPINDENT, c->name,
2683 else while (--argc > 0) {
2687 if (Ambiguous((void *)c))
2688 printf("?Ambiguous help command %s\n", arg);
2689 else if (c == (Command *)0)
2690 printf("?Invalid help command %s\n", arg);
2692 printf("%s\n", c->help);
2697 static char *rcname = 0;
2698 static char rcbuf[128];
2701 cmdrc(char *m1, char *m2)
2706 int l1 = strlen(m1);
2707 int l2 = strlen(m2);
2708 char m1save[MAXHOSTNAMELEN];
2713 strlcpy(m1save, m1, sizeof(m1save));
2717 rcname = getenv("HOME");
2718 if (rcname && (strlen(rcname) + 10) < sizeof(rcbuf))
2719 strcpy(rcbuf, rcname);
2722 strcat(rcbuf, "/.telnetrc");
2726 if ((rcfile = fopen(rcname, "r")) == 0) {
2731 if (fgets(line, sizeof(line), rcfile) == NULL)
2738 if (!isspace(line[0]))
2741 if (gotmachine == 0) {
2742 if (isspace(line[0]))
2744 if (strncasecmp(line, m1, l1) == 0)
2745 strncpy(line, &line[l1], sizeof(line) - l1);
2746 else if (strncasecmp(line, m2, l2) == 0)
2747 strncpy(line, &line[l2], sizeof(line) - l2);
2748 else if (strncasecmp(line, "DEFAULT", 7) == 0)
2749 strncpy(line, &line[7], sizeof(line) - 7);
2752 if (line[0] != ' ' && line[0] != '\t' && line[0] != '\n')
2759 c = getcmd(margv[0]);
2760 if (Ambiguous((void *)c)) {
2761 printf("?Ambiguous command: %s\n", margv[0]);
2765 printf("?Invalid command: %s\n", margv[0]);
2769 * This should never happen...
2771 if (c->needconnect && !connected) {
2772 printf("?Need to be connected first for %s.\n", margv[0]);
2775 (*c->handler)(margc, margv);
2781 * Source route is handed in as
2782 * [!]@hop1@hop2...[@|:]dst
2783 * If the leading ! is present, it is a
2784 * strict source route, otherwise it is
2785 * assmed to be a loose source route.
2787 * We fill in the source route option as
2788 * hop1,hop2,hop3...dest
2789 * and return a pointer to hop1, which will
2790 * be the address to connect() to.
2794 * res: ponter to addrinfo structure which contains sockaddr to
2795 * the host to connect to.
2797 * arg: pointer to route list to decipher
2799 * cpp: If *cpp is not equal to NULL, this is a
2800 * pointer to a pointer to a character array
2801 * that should be filled in with the option.
2803 * lenp: pointer to an integer that contains the
2804 * length of *cpp if *cpp != NULL.
2806 * protop: pointer to an integer that should be filled in with
2807 * appropriate protocol for setsockopt, as socket
2810 * optp: pointer to an integer that should be filled in with
2811 * appropriate option for setsockopt, as socket protocol
2816 * If the return value is 1, then all operations are
2817 * successful. If the
2818 * return value is -1, there was a syntax error in the
2819 * option, either unknown characters, or too many hosts.
2820 * If the return value is 0, one of the hostnames in the
2821 * path is unknown, and *cpp is set to point to the bad
2824 * *cpp: If *cpp was equal to NULL, it will be filled
2825 * in with a pointer to our static area that has
2826 * the option filled in. This will be 32bit aligned.
2828 * *lenp: This will be filled in with how long the option
2829 * pointed to by *cpp is.
2831 * *protop: This will be filled in with appropriate protocol for
2832 * setsockopt, as socket protocol family.
2834 * *optp: This will be filled in with appropriate option for
2835 * setsockopt, as socket protocol family.
2838 sourceroute(struct addrinfo *ai, char *arg, char **cpp, int *lenp, int *protop, int *optp)
2840 static char buf[1024 + ALIGNBYTES]; /*XXX*/
2841 char *cp, *cp2, *lsrp, *ep;
2842 struct sockaddr_in *_sin;
2844 struct sockaddr_in6 *sin6;
2845 struct cmsghdr *cmsg;
2847 struct addrinfo hints, *res;
2852 * Verify the arguments, and make sure we have
2853 * at least 7 bytes for the option.
2855 if (cpp == NULL || lenp == NULL)
2858 switch (res->ai_family) {
2865 if (*lenp < (int)CMSG_SPACE(sizeof(struct ip6_rthdr) +
2866 sizeof(struct in6_addr)))
2873 * Decide whether we have a buffer passed to us,
2874 * or if we need to use our own static buffer.
2880 *cpp = lsrp = (char *)ALIGN(buf);
2887 if (ai->ai_family == AF_INET6) {
2888 cmsg = inet6_rthdr_init(*cpp, IPV6_RTHDR_TYPE_0);
2891 *protop = IPPROTO_IPV6;
2892 *optp = IPV6_PKTOPTIONS;
2897 * Next, decide whether we have a loose source
2898 * route or a strict source route, and fill in
2899 * the begining of the option.
2903 *lsrp++ = IPOPT_SSRR;
2905 *lsrp++ = IPOPT_LSRR;
2910 lsrp++; /* skip over length, we'll fill it in later */
2912 *protop = IPPROTO_IP;
2917 memset(&hints, 0, sizeof(hints));
2918 hints.ai_family = ai->ai_family;
2919 hints.ai_socktype = SOCK_STREAM;
2923 ai->ai_family != AF_INET6 &&
2927 else for (cp2 = cp; (c = *cp2); cp2++) {
2932 } else if (c == '@') {
2936 ai->ai_family != AF_INET6 &&
2947 hints.ai_flags = AI_NUMERICHOST;
2948 error = getaddrinfo(cp, NULL, &hints, &res);
2949 if (error == EAI_NODATA) {
2951 error = getaddrinfo(cp, NULL, &hints, &res);
2954 fprintf(stderr, "%s: %s\n", cp, gai_strerror(error));
2955 if (error == EAI_SYSTEM)
2956 fprintf(stderr, "%s: %s\n", cp,
2962 if (res->ai_family == AF_INET6) {
2963 sin6 = (struct sockaddr_in6 *)res->ai_addr;
2964 inet6_rthdr_add(cmsg, &sin6->sin6_addr,
2969 _sin = (struct sockaddr_in *)res->ai_addr;
2970 memcpy(lsrp, (char *)&_sin->sin_addr, 4);
2978 * Check to make sure there is space for next address
2981 if (res->ai_family == AF_INET6) {
2982 if (((char *)CMSG_DATA(cmsg) +
2983 sizeof(struct ip6_rthdr) +
2984 ((inet6_rthdr_segments(cmsg) + 1) *
2985 sizeof(struct in6_addr))) > ep)
2994 if (res->ai_family == AF_INET6) {
2995 inet6_rthdr_lasthop(cmsg, IPV6_RTHDR_LOOSE);
2996 *lenp = cmsg->cmsg_len;
3000 if ((*(*cpp+IPOPT_OLEN) = lsrp - *cpp) <= 7) {
3005 *lsrp++ = IPOPT_NOP; /* 32 bit word align it */
3006 *lenp = lsrp - *cpp;