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 $
37 #include <sys/param.h>
40 #include <sys/socket.h>
41 #include <netinet/in.h>
54 #include <arpa/telnet.h>
55 #include <arpa/inet.h>
67 #include <libtelnet/auth.h>
70 #include <libtelnet/encrypt.h>
73 #include <netinet/in_systm.h>
74 #include <netinet/ip.h>
75 #include <netinet/ip6.h>
77 #ifndef MAXHOSTNAMELEN
78 #define MAXHOSTNAMELEN 256
81 typedef int (*intrtn_t)(int, char **);
84 extern int auth_togdebug(int);
87 extern int EncryptAutoEnc(int);
88 extern int EncryptAutoDec(int);
89 extern int EncryptDebug(int);
90 extern int EncryptVerbose(int);
91 #endif /* ENCRYPTION */
92 #if defined(IPPROTO_IP) && defined(IP_TOS)
94 #endif /* defined(IPPROTO_IP) && defined(IP_TOS) */
97 static char _hostname[MAXHOSTNAMELEN];
99 static int help(int, char **);
100 static int call(intrtn_t, ...);
101 static void cmdrc(char *, char *);
103 static int switch_af(struct addrinfo **);
105 static int togglehelp(void);
106 static int send_tncmd(void (*)(int, int), const char *, char *);
107 static int setmod(int);
108 static int clearmode(int);
109 static int modehelp(void);
110 static int sourceroute(struct addrinfo *, char *, char **, int *, int *, int *);
113 const char *name; /* command name */
114 const char *help; /* help string (NULL for no help) */
115 int (*handler)(int, char **); /* routine which executes command */
116 int needconnect; /* Do we need to be connected to execute? */
119 static char line[256];
120 static char saveline[256];
122 static char *margv[20];
125 #include <sys/wait.h>
126 #define PATH_OPIEKEY "/usr/bin/opiekey"
128 opie_calc(int argc, char *argv[])
133 printf("%s sequence challenge\n", argv[0]);
139 execv(PATH_OPIEKEY, argv);
145 (void) wait(&status);
146 if (WIFEXITED(status))
147 return (WEXITSTATUS(status));
161 if (*cp == '!') { /* Special case shell escape */
162 strcpy(saveline, line); /* save for shell command */
163 *argp++ = strdup("!"); /* No room in string to get this */
175 for (cp2 = cp; c != '\0'; c = *++cp) {
183 if ((c = *++cp) == '\0')
185 } else if (c == '"') {
188 } else if (c == '\'') {
191 } else if (isspace(c))
205 * Make a character string into a number.
207 * Todo: 1. Could take random integers (12, 0x12, 012, 0b1).
220 c = b | 0x40; /* DEL */
233 * Construct a control character sequence
234 * for a special character.
241 * The only way I could get the Sun 3.5 compiler
243 * if ((unsigned int)c >= 0x80)
244 * was to assign "c" to an unsigned int variable...
247 unsigned int uic = (unsigned int)c;
251 if (c == (cc_t)_POSIX_VDISABLE) {
256 buf[1] = ((c>>6)&07) + '0';
257 buf[2] = ((c>>3)&07) + '0';
258 buf[3] = (c&07) + '0';
260 } else if (uic >= 0x20) {
272 * The following are data structures and routines for
273 * the "send" command.
278 const char *name; /* How user refers to it (case independent) */
279 const char *help; /* Help information (0 ==> no help) */
280 int needconnect; /* Need to be connected */
281 int narg; /* Number of arguments */
282 int (*handler)(char *, ...); /* Routine to perform (for special ops) */
283 int nbyte; /* Number of bytes to send this command */
284 int what; /* Character to be sent (<0 ==> special) */
292 send_dontcmd(char *),
293 send_willcmd(char *),
294 send_wontcmd(char *);
296 static struct sendlist Sendlist[] = {
297 { "ao", "Send Telnet Abort output", 1, 0, NULL, 2, AO },
298 { "ayt", "Send Telnet 'Are You There'", 1, 0, NULL, 2, AYT },
299 { "brk", "Send Telnet Break", 1, 0, NULL, 2, BREAK },
300 { "break", NULL, 1, 0, NULL, 2, BREAK },
301 { "ec", "Send Telnet Erase Character", 1, 0, NULL, 2, EC },
302 { "el", "Send Telnet Erase Line", 1, 0, NULL, 2, EL },
303 { "escape", "Send current escape character",1, 0, (int (*)(char *, ...))send_esc, 1, 0 },
304 { "ga", "Send Telnet 'Go Ahead' sequence", 1, 0, NULL, 2, GA },
305 { "ip", "Send Telnet Interrupt Process",1, 0, NULL, 2, IP },
306 { "intp", NULL, 1, 0, NULL, 2, IP },
307 { "interrupt", NULL, 1, 0, NULL, 2, IP },
308 { "intr", NULL, 1, 0, NULL, 2, IP },
309 { "nop", "Send Telnet 'No operation'", 1, 0, NULL, 2, NOP },
310 { "eor", "Send Telnet 'End of Record'", 1, 0, NULL, 2, EOR },
311 { "abort", "Send Telnet 'Abort Process'", 1, 0, NULL, 2, ABORT },
312 { "susp", "Send Telnet 'Suspend Process'",1, 0, NULL, 2, SUSP },
313 { "eof", "Send Telnet End of File Character", 1, 0, NULL, 2, xEOF },
314 { "synch", "Perform Telnet 'Synch operation'", 1, 0, (int (*)(char *, ...))dosynch, 2, 0 },
315 { "getstatus", "Send request for STATUS", 1, 0, (int (*)(char *, ...))get_status, 6, 0 },
316 { "?", "Display send options", 0, 0, (int (*)(char *, ...))send_help, 0, 0 },
317 { "help", NULL, 0, 0, (int (*)(char *, ...))send_help, 0, 0 },
318 { "do", NULL, 0, 1, (int (*)(char *, ...))send_docmd, 3, 0 },
319 { "dont", NULL, 0, 1, (int (*)(char *, ...))send_dontcmd, 3, 0 },
320 { "will", NULL, 0, 1, (int (*)(char *, ...))send_willcmd, 3, 0 },
321 { "wont", NULL, 0, 1, (int (*)(char *, ...))send_wontcmd, 3, 0 },
322 { NULL, NULL, 0, 0, NULL, 0, 0 }
325 #define GETSEND(name) ((struct sendlist *) genget(name, (char **) Sendlist, \
326 sizeof(struct sendlist)))
329 sendcmd(int argc, char *argv[])
331 int count; /* how many bytes we are going to need to send */
333 struct sendlist *s; /* pointer to current command */
338 printf("need at least one argument for 'send' command\n");
339 printf("'send ?' for help\n");
343 * First, validate all the send arguments.
344 * In addition, we see how much space we are going to need, and
345 * whether or not we will be doing a "SYNCH" operation (which
346 * flushes the network queue).
349 for (i = 1; i < argc; i++) {
350 s = GETSEND(argv[i]);
352 printf("Unknown send argument '%s'\n'send ?' for help.\n",
355 } else if (Ambiguous((void *)s)) {
356 printf("Ambiguous send argument '%s'\n'send ?' for help.\n",
360 if (i + s->narg >= argc) {
362 "Need %d argument%s to 'send %s' command. 'send %s ?' for help.\n",
363 s->narg, s->narg == 1 ? "" : "s", s->name, s->name);
367 if ((void *)s->handler == (void *)send_help) {
373 needconnect += s->needconnect;
375 if (!connected && needconnect) {
376 printf("?Need to be connected first.\n");
377 printf("'send ?' for help\n");
380 /* Now, do we have enough room? */
381 if (NETROOM() < count) {
382 printf("There is not enough room in the buffer TO the network\n");
383 printf("to process your request. Nothing will be done.\n");
384 printf("('send synch' will throw away most data in the network\n");
385 printf("buffer, if this might help.)\n");
388 /* OK, they are all OK, now go through again and actually send */
390 for (i = 1; i < argc; i++) {
391 if ((s = GETSEND(argv[i])) == NULL) {
392 fprintf(stderr, "Telnet 'send' error - argument disappeared!\n");
398 success += (*s->handler)((s->narg > 0) ? argv[i+1] : 0,
399 (s->narg > 1) ? argv[i+2] : 0);
402 NET2ADD(IAC, s->what);
403 printoption("SENT", IAC, s->what);
406 return (count == success);
417 send_docmd(char *name)
419 return(send_tncmd(send_do, "do", name));
423 send_dontcmd(char *name)
425 return(send_tncmd(send_dont, "dont", name));
429 send_willcmd(char *name)
431 return(send_tncmd(send_will, "will", name));
435 send_wontcmd(char *name)
437 return(send_tncmd(send_wont, "wont", name));
441 send_tncmd(void (*func)(int, int), const char *cmd, char *name)
444 extern char *telopts[];
447 if (isprefix(name, "help") || isprefix(name, "?")) {
450 printf("Usage: send %s <value|option>\n", cmd);
451 printf("\"value\" must be from 0 to 255\n");
452 printf("Valid options are:\n\t");
455 for (cpp = telopts; *cpp; cpp++) {
456 len = strlen(*cpp) + 3;
457 if (col + len > 65) {
461 printf(" \"%s\"", *cpp);
467 cpp = (char **)genget(name, telopts, sizeof(char *));
468 if (Ambiguous(cpp)) {
469 fprintf(stderr,"'%s': ambiguous argument ('send %s ?' for help).\n",
478 while (*cp >= '0' && *cp <= '9') {
484 fprintf(stderr, "'%s': unknown argument ('send %s ?' for help).\n",
487 } else if (val < 0 || val > 255) {
488 fprintf(stderr, "'%s': bad value ('send %s ?' for help).\n",
494 printf("?Need to be connected first.\n");
504 struct sendlist *s; /* pointer to current command */
505 for (s = Sendlist; s->name; s++) {
507 printf("%-15s %s\n", s->name, s->help);
513 * The following are the routines and data structures referred
514 * to by the arguments to the "toggle" command.
529 (SetSockOpt(net, SOL_SOCKET, SO_DEBUG, debug)) < 0) {
530 perror("setsockopt (SO_DEBUG)");
534 if (net > 0 && SetSockOpt(net, SOL_SOCKET, SO_DEBUG, 1) < 0)
535 perror("setsockopt (SO_DEBUG)");
537 printf("Cannot turn off socket debugging\n");
547 printf("Will send carriage returns as telnet <CR><LF>.\n");
549 printf("Will send carriage returns as telnet <CR><NUL>.\n");
559 donebinarytoggle = 1;
564 if (my_want_state_is_will(TELOPT_BINARY) &&
565 my_want_state_is_do(TELOPT_BINARY)) {
567 } else if (my_want_state_is_wont(TELOPT_BINARY) &&
568 my_want_state_is_dont(TELOPT_BINARY)) {
571 val = binmode ? 0 : 1;
575 if (my_want_state_is_will(TELOPT_BINARY) &&
576 my_want_state_is_do(TELOPT_BINARY)) {
577 printf("Already operating in binary mode with remote host.\n");
579 printf("Negotiating binary mode with remote host.\n");
583 if (my_want_state_is_wont(TELOPT_BINARY) &&
584 my_want_state_is_dont(TELOPT_BINARY)) {
585 printf("Already in network ascii mode with remote host.\n");
587 printf("Negotiating network ascii mode with remote host.\n");
597 donebinarytoggle = 1;
600 val = my_want_state_is_do(TELOPT_BINARY) ? 0 : 1;
603 if (my_want_state_is_do(TELOPT_BINARY)) {
604 printf("Already receiving in binary mode.\n");
606 printf("Negotiating binary mode on input.\n");
610 if (my_want_state_is_dont(TELOPT_BINARY)) {
611 printf("Already receiving in network ascii mode.\n");
613 printf("Negotiating network ascii mode on input.\n");
623 donebinarytoggle = 1;
626 val = my_want_state_is_will(TELOPT_BINARY) ? 0 : 1;
629 if (my_want_state_is_will(TELOPT_BINARY)) {
630 printf("Already transmitting in binary mode.\n");
632 printf("Negotiating binary mode on output.\n");
636 if (my_want_state_is_wont(TELOPT_BINARY)) {
637 printf("Already transmitting in network ascii mode.\n");
639 printf("Negotiating network ascii mode on output.\n");
647 const char *name; /* name of toggle */
648 const char *help; /* help message */
649 int (*handler)(int); /* routine to do actual setting */
651 const char *actionexplanation;
654 static struct togglelist Togglelist[] = {
656 "flushing of output when sending interrupt characters",
659 "flush output when sending interrupt characters" },
661 "automatic sending of interrupt characters in urgent mode",
664 "send interrupt characters in urgent mode" },
665 #ifdef AUTHENTICATION
667 "automatic sending of login and/or authentication info",
670 "send login name and/or authentication information" },
672 "Toggle authentication debugging",
675 "print authentication debugging information" },
679 "automatic encryption of data stream",
682 "automatically encrypt output" },
684 "automatic decryption of data stream",
687 "automatically decrypt input" },
689 "Toggle verbose encryption output",
692 "print verbose encryption output" },
694 "Toggle encryption debugging",
697 "print encryption debugging information" },
698 #endif /* ENCRYPTION */
700 "don't read ~/.telnetrc file",
703 "skip reading of ~/.telnetrc file" },
705 "sending and receiving of binary data",
710 "receiving of binary data",
715 "sending of binary data",
720 "sending carriage returns as telnet <CR><LF>",
721 (int (*)(int))togcrlf,
725 "mapping of received carriage returns",
728 "map carriage return on output" },
730 "local recognition of certain control characters",
731 (int (*)(int))lclchars,
733 "recognize certain control characters" },
734 { " ", "", NULL, NULL, NULL }, /* empty line */
737 (int (*)(int))togdebug,
739 "turn on socket level debugging" },
741 "printing of hexadecimal network data (debugging)",
744 "print hexadecimal representation of network traffic" },
746 "output of \"netdata\" to user readable format (debugging)",
749 "print user readable output for \"netdata\"" },
751 "viewing of options processing (debugging)",
754 "show option processing" },
756 "(debugging) toggle printing of hexadecimal terminal data",
759 "print hexadecimal representation of terminal traffic" },
762 (int (*)(int))togglehelp,
765 { NULL, NULL, NULL, NULL, NULL },
768 (int (*)(int))togglehelp,
771 { NULL, NULL, NULL, NULL, NULL }
777 struct togglelist *c;
779 for (c = Togglelist; c->name; c++) {
782 printf("%-15s toggle %s\n", c->name, c->help);
788 printf("%-15s %s\n", "?", "display help information");
793 settogglehelp(int set)
795 struct togglelist *c;
797 for (c = Togglelist; c->name; c++) {
800 printf("%-15s %s %s\n", c->name, set ? "enable" : "disable",
808 #define GETTOGGLE(name) (struct togglelist *) \
809 genget(name, (char **) Togglelist, sizeof(struct togglelist))
812 toggle(int argc, char *argv[])
816 struct togglelist *c;
820 "Need an argument to 'toggle' command. 'toggle ?' for help.\n");
828 if (Ambiguous((void *)c)) {
829 fprintf(stderr, "'%s': ambiguous argument ('toggle ?' for help).\n",
832 } else if (c == NULL) {
833 fprintf(stderr, "'%s': unknown argument ('toggle ?' for help).\n",
838 *c->variable = !*c->variable; /* invert it */
839 if (c->actionexplanation) {
840 printf("%s %s.\n", *c->variable? "Will" : "Won't",
841 c->actionexplanation);
845 retval &= (*c->handler)(-1);
853 * The following perform the "set" command.
857 struct termio new_tc = { 0, 0, 0, 0, {}, 0, 0 };
861 const char *name; /* name */
862 const char *help; /* help information */
863 void (*handler)(char *);
864 cc_t *charp; /* where it is located at */
867 static struct setlist Setlist[] = {
868 #ifdef KLUDGELINEMODE
869 { "echo", "character to toggle local echoing on/off", NULL, &echoc },
871 { "escape", "character to escape back to telnet command mode", NULL, &escape },
872 { "rlogin", "rlogin escape character", 0, &rlogin },
873 { "tracefile", "file to write trace information to", SetNetTrace, (cc_t *)NetTraceFile},
874 { " ", "", NULL, NULL },
875 { " ", "The following need 'localchars' to be toggled true", NULL, NULL },
876 { "flushoutput", "character to cause an Abort Output", NULL, termFlushCharp },
877 { "interrupt", "character to cause an Interrupt Process", NULL, termIntCharp },
878 { "quit", "character to cause an Abort process", NULL, termQuitCharp },
879 { "eof", "character to cause an EOF ", NULL, termEofCharp },
880 { " ", "", NULL, NULL },
881 { " ", "The following are for local editing in linemode", NULL, NULL },
882 { "erase", "character to use to erase a character", NULL, termEraseCharp },
883 { "kill", "character to use to erase a line", NULL, termKillCharp },
884 { "lnext", "character to use for literal next", NULL, termLiteralNextCharp },
885 { "susp", "character to cause a Suspend Process", NULL, termSuspCharp },
886 { "reprint", "character to use for line reprint", NULL, termRprntCharp },
887 { "worderase", "character to use to erase a word", NULL, termWerasCharp },
888 { "start", "character to use for XON", NULL, termStartCharp },
889 { "stop", "character to use for XOFF", NULL, termStopCharp },
890 { "forw1", "alternate end of line character", NULL, termForw1Charp },
891 { "forw2", "alternate end of line character", NULL, termForw2Charp },
892 { "ayt", "alternate AYT character", NULL, termAytCharp },
893 { NULL, NULL, NULL, NULL }
896 static struct setlist *
899 return (struct setlist *)
900 genget(name, (char **) Setlist, sizeof(struct setlist));
904 set_escape_char(char *s)
906 if (rlogin != _POSIX_VDISABLE) {
907 rlogin = (s && *s) ? special(s) : _POSIX_VDISABLE;
908 printf("Telnet rlogin escape character is '%s'.\n",
911 escape = (s && *s) ? special(s) : _POSIX_VDISABLE;
912 printf("Telnet escape character is '%s'.\n", control(escape));
917 setcmd(int argc, char *argv[])
921 struct togglelist *c;
923 if (argc < 2 || argc > 3) {
924 printf("Format is 'set Name Value'\n'set ?' for help.\n");
927 if ((argc == 2) && (isprefix(argv[1], "?") || isprefix(argv[1], "help"))) {
928 for (ct = Setlist; ct->name; ct++)
929 printf("%-15s %s\n", ct->name, ct->help);
932 printf("%-15s %s\n", "?", "display help information");
936 ct = getset(argv[1]);
938 c = GETTOGGLE(argv[1]);
940 fprintf(stderr, "'%s': unknown argument ('set ?' for help).\n",
943 } else if (Ambiguous((void *)c)) {
944 fprintf(stderr, "'%s': ambiguous argument ('set ?' for help).\n",
949 if ((argc == 2) || (strcmp("on", argv[2]) == 0))
951 else if (strcmp("off", argv[2]) == 0)
954 printf("Format is 'set togglename [on|off]'\n'set ?' for help.\n");
957 if (c->actionexplanation) {
958 printf("%s %s.\n", *c->variable? "Will" : "Won't",
959 c->actionexplanation);
964 } else if (argc != 3) {
965 printf("Format is 'set Name Value'\n'set ?' for help.\n");
967 } else if (Ambiguous((void *)ct)) {
968 fprintf(stderr, "'%s': ambiguous argument ('set ?' for help).\n",
971 } else if (ct->handler) {
972 (*ct->handler)(argv[2]);
973 printf("%s set to \"%s\".\n", ct->name, (char *)ct->charp);
975 if (strcmp("off", argv[2])) {
976 value = special(argv[2]);
978 value = _POSIX_VDISABLE;
980 *(ct->charp) = (cc_t)value;
981 printf("%s character is '%s'.\n", ct->name, control(*(ct->charp)));
988 unsetcmd(int argc, char *argv[])
991 struct togglelist *c;
996 "Need an argument to 'unset' command. 'unset ?' for help.\n");
999 if (isprefix(argv[1], "?") || isprefix(argv[1], "help")) {
1000 for (ct = Setlist; ct->name; ct++)
1001 printf("%-15s %s\n", ct->name, ct->help);
1004 printf("%-15s %s\n", "?", "display help information");
1014 c = GETTOGGLE(name);
1016 fprintf(stderr, "'%s': unknown argument ('unset ?' for help).\n",
1019 } else if (Ambiguous((void *)c)) {
1020 fprintf(stderr, "'%s': ambiguous argument ('unset ?' for help).\n",
1026 if (c->actionexplanation) {
1027 printf("%s %s.\n", *c->variable? "Will" : "Won't",
1028 c->actionexplanation);
1033 } else if (Ambiguous((void *)ct)) {
1034 fprintf(stderr, "'%s': ambiguous argument ('unset ?' for help).\n",
1037 } else if (ct->handler) {
1039 printf("%s reset to \"%s\".\n", ct->name, (char *)ct->charp);
1041 *(ct->charp) = _POSIX_VDISABLE;
1042 printf("%s character is '%s'.\n", ct->name, control(*(ct->charp)));
1049 * The following are the data structures and routines for the
1052 #ifdef KLUDGELINEMODE
1053 extern int kludgelinemode;
1059 send_wont(TELOPT_LINEMODE, 1);
1060 send_dont(TELOPT_SGA, 1);
1061 send_dont(TELOPT_ECHO, 1);
1069 #ifdef KLUDGELINEMODE
1071 send_dont(TELOPT_SGA, 1);
1073 send_will(TELOPT_LINEMODE, 1);
1074 send_dont(TELOPT_ECHO, 1);
1081 #ifdef KLUDGELINEMODE
1083 send_do(TELOPT_SGA, 1);
1086 send_wont(TELOPT_LINEMODE, 1);
1087 send_do(TELOPT_ECHO, 1);
1092 dolmmode(int bit, int on)
1095 extern int linemode;
1097 if (my_want_state_is_wont(TELOPT_LINEMODE)) {
1098 printf("?Need to have LINEMODE option enabled first.\n");
1099 printf("'mode ?' for help.\n");
1104 c = (linemode | bit);
1106 c = (linemode & ~bit);
1114 return dolmmode(bit, 1);
1120 return dolmmode(bit, 0);
1124 const char *name; /* command name */
1125 const char *help; /* help string */
1126 int (*handler)(int);/* routine which executes command */
1127 int needconnect; /* Do we need to be connected to execute? */
1131 static struct modelist ModeList[] = {
1132 { "character", "Disable LINEMODE option", (int (*)(int))docharmode, 1, 0 },
1133 #ifdef KLUDGELINEMODE
1134 { "", "(or disable obsolete line-by-line mode)", NULL, 0, 0 },
1136 { "line", "Enable LINEMODE option", (int (*)(int))dolinemode, 1, 0 },
1137 #ifdef KLUDGELINEMODE
1138 { "", "(or enable obsolete line-by-line mode)", NULL, 0, 0 },
1140 { "", "", NULL, 0, 0 },
1141 { "", "These require the LINEMODE option to be enabled", NULL, 0, 0 },
1142 { "isig", "Enable signal trapping", setmod, 1, MODE_TRAPSIG },
1143 { "+isig", 0, setmod, 1, MODE_TRAPSIG },
1144 { "-isig", "Disable signal trapping", clearmode, 1, MODE_TRAPSIG },
1145 { "edit", "Enable character editing", setmod, 1, MODE_EDIT },
1146 { "+edit", 0, setmod, 1, MODE_EDIT },
1147 { "-edit", "Disable character editing", clearmode, 1, MODE_EDIT },
1148 { "softtabs", "Enable tab expansion", setmod, 1, MODE_SOFT_TAB },
1149 { "+softtabs", 0, setmod, 1, MODE_SOFT_TAB },
1150 { "-softtabs", "Disable character editing", clearmode, 1, MODE_SOFT_TAB },
1151 { "litecho", "Enable literal character echo", setmod, 1, MODE_LIT_ECHO },
1152 { "+litecho", 0, setmod, 1, MODE_LIT_ECHO },
1153 { "-litecho", "Disable literal character echo", clearmode, 1, MODE_LIT_ECHO },
1154 { "help", 0, (int (*)(int))modehelp, 0, 0 },
1155 #ifdef KLUDGELINEMODE
1156 { "kludgeline", 0, (int (*)(int))dokludgemode, 1, 0 },
1158 { "", "", NULL, 0, 0 },
1159 { "?", "Print help information", (int (*)(int))modehelp, 0, 0 },
1160 { NULL, NULL, NULL, 0, 0 },
1167 struct modelist *mt;
1169 printf("format is: 'mode Mode', where 'Mode' is one of:\n\n");
1170 for (mt = ModeList; mt->name; mt++) {
1173 printf("%-15s %s\n", mt->name, mt->help);
1181 #define GETMODECMD(name) (struct modelist *) \
1182 genget(name, (char **) ModeList, sizeof(struct modelist))
1185 modecmd(int argc, char *argv[])
1187 struct modelist *mt;
1190 printf("'mode' command requires an argument\n");
1191 printf("'mode ?' for help.\n");
1192 } else if ((mt = GETMODECMD(argv[1])) == NULL) {
1193 fprintf(stderr, "Unknown mode '%s' ('mode ?' for help).\n", argv[1]);
1194 } else if (Ambiguous((void *)mt)) {
1195 fprintf(stderr, "Ambiguous mode '%s' ('mode ?' for help).\n", argv[1]);
1196 } else if (mt->needconnect && !connected) {
1197 printf("?Need to be connected first.\n");
1198 printf("'mode ?' for help.\n");
1199 } else if (mt->handler) {
1200 return (*mt->handler)(mt->arg1);
1206 * The following data structures and routines implement the
1207 * "display" command.
1211 display(int argc, char *argv[])
1213 struct togglelist *tl;
1216 #define dotog(tl) if (tl->variable && tl->actionexplanation) { \
1217 if (*tl->variable) { \
1222 printf(" %s.\n", tl->actionexplanation); \
1225 #define doset(sl) if (sl->name && *sl->name != ' ') { \
1226 if (sl->handler == 0) \
1227 printf("%-15s [%s]\n", sl->name, control(*sl->charp)); \
1229 printf("%-15s \"%s\"\n", sl->name, (char *)sl->charp); \
1233 for (tl = Togglelist; tl->name; tl++) {
1237 for (sl = Setlist; sl->name; sl++) {
1243 for (i = 1; i < argc; i++) {
1244 sl = getset(argv[i]);
1245 tl = GETTOGGLE(argv[i]);
1246 if (Ambiguous((void *)sl) || Ambiguous((void *)tl)) {
1247 printf("?Ambiguous argument '%s'.\n", argv[i]);
1249 } else if (!sl && !tl) {
1250 printf("?Unknown argument '%s'.\n", argv[i]);
1262 /*@*/optionstatus();
1265 #endif /* ENCRYPTION */
1272 * The following are the data structures, and many of the routines,
1273 * relating to command processing.
1277 * Set the escape character.
1280 setescape(int argc, char *argv[])
1286 "Deprecated usage - please use 'set escape%s%s' in the future.\n",
1287 (argc > 2)? " ":"", (argc > 2)? argv[1]: "");
1291 printf("new escape character: ");
1292 (void) fgets(buf, sizeof(buf), stdin);
1297 (void) fflush(stdout);
1305 printf("Deprecated usage - please use 'toggle crmod' in the future.\n");
1306 printf("%s map carriage return on output.\n", crmod ? "Will" : "Won't");
1307 (void) fflush(stdout);
1317 long oldrows, oldcols, newrows, newcols, err_;
1319 err_ = (TerminalWindowSize(&oldrows, &oldcols) == 0) ? 1 : 0;
1320 (void) kill(0, SIGTSTP);
1322 * If we didn't get the window size before the SUSPEND, but we
1323 * can get them now (?), then send the NAWS to make sure that
1324 * we are set up for the right window size.
1326 if (TerminalWindowSize(&newrows, &newcols) && connected &&
1327 (err_ || ((oldrows != newrows) || (oldcols != newcols)))) {
1331 /* reget parameters in case they were changed */
1332 TerminalSaveState();
1335 printf("Suspend is not supported. Try the '!' command instead\n");
1341 shell(int argc, char *argv[] __unused)
1343 long oldrows, oldcols, newrows, newcols, err_;
1347 err_ = (TerminalWindowSize(&oldrows, &oldcols) == 0) ? 1 : 0;
1350 perror("Fork failed\n");
1356 * Fire up the shell in the child.
1358 const char *shellp, *shellname;
1360 shellp = getenv("SHELL");
1363 if ((shellname = strrchr(shellp, '/')) == NULL)
1368 execl(shellp, shellname, "-c", &saveline[1], NULL);
1370 execl(shellp, shellname, NULL);
1375 (void)wait(NULL); /* Wait for the shell to complete */
1377 if (TerminalWindowSize(&newrows, &newcols) && connected &&
1378 (err_ || ((oldrows != newrows) || (oldcols != newcols)))) {
1387 bye(int argc, char *argv[])
1389 extern int resettermname;
1392 (void) shutdown(net, SHUT_RDWR);
1393 printf("Connection closed.\n");
1394 (void) NetClose(net);
1397 #ifdef AUTHENTICATION
1399 auth_encrypt_connect(connected);
1405 if ((argc != 2) || (strcmp(argv[1], "fromquit") != 0)) {
1406 longjmp(toplevel, 1);
1409 return 1; /* Keep lint, etc., happy */
1415 (void) call(bye, "bye", "fromquit", 0);
1422 send_do(TELOPT_LOGOUT, 1);
1435 void (*handler)(int);
1439 static void slc_help(void);
1441 struct slclist SlcList[] = {
1442 { "export", "Use local special character definitions",
1443 (void (*)(int))slc_mode_export, 0 },
1444 { "import", "Use remote special character definitions",
1445 slc_mode_import, 1 },
1446 { "check", "Verify remote special character definitions",
1447 slc_mode_import, 0 },
1448 { "help", NULL, (void (*)(int))slc_help, 0 },
1449 { "?", "Print help information", (void (*)(int))slc_help, 0 },
1450 { NULL, NULL, NULL, 0 },
1458 for (c = SlcList; c->name; c++) {
1461 printf("%-15s %s\n", c->name, c->help);
1468 static struct slclist *
1471 return (struct slclist *)
1472 genget(name, (char **) SlcList, sizeof(struct slclist));
1476 slccmd(int argc, char *argv[])
1482 "Need an argument to 'slc' command. 'slc ?' for help.\n");
1485 c = getslc(argv[1]);
1487 fprintf(stderr, "'%s': unknown argument ('slc ?' for help).\n",
1491 if (Ambiguous((void *)c)) {
1492 fprintf(stderr, "'%s': ambiguous argument ('slc ?' for help).\n",
1496 (*c->handler)(c->arg);
1502 * The ENVIRON command.
1508 void (*handler)(unsigned char *, unsigned char *);
1512 extern struct env_lst *
1513 env_define(const unsigned char *, unsigned char *);
1515 env_undefine(unsigned char *),
1516 env_export(const unsigned char *),
1517 env_unexport(const unsigned char *),
1518 env_send(unsigned char *),
1519 #if defined(OLD_ENVIRON) && defined(ENV_HACK)
1520 env_varval(unsigned char *),
1526 struct envlist EnvList[] = {
1527 { "define", "Define an environment variable",
1528 (void (*)(unsigned char *, unsigned char *))env_define, 2 },
1529 { "undefine", "Undefine an environment variable",
1530 (void (*)(unsigned char *, unsigned char *))env_undefine, 1 },
1531 { "export", "Mark an environment variable for automatic export",
1532 (void (*)(unsigned char *, unsigned char *))env_export, 1 },
1533 { "unexport", "Don't mark an environment variable for automatic export",
1534 (void (*)(unsigned char *, unsigned char *))env_unexport, 1 },
1535 { "send", "Send an environment variable", (void (*)(unsigned char *, unsigned char *))env_send, 1 },
1536 { "list", "List the current environment variables",
1537 (void (*)(unsigned char *, unsigned char *))env_list, 0 },
1538 #if defined(OLD_ENVIRON) && defined(ENV_HACK)
1539 { "varval", "Reverse VAR and VALUE (auto, right, wrong, status)",
1540 (void (*)(unsigned char *, unsigned char *))env_varval, 1 },
1542 { "help", NULL, (void (*)(unsigned char *, unsigned char *))env_help, 0 },
1543 { "?", "Print help information", (void (*)(unsigned char *, unsigned char *))env_help, 0 },
1544 { NULL, NULL, NULL, 0 },
1552 for (c = EnvList; c->name; c++) {
1555 printf("%-15s %s\n", c->name, c->help);
1562 static struct envlist *
1563 getenvcmd(char *name)
1565 return (struct envlist *)
1566 genget(name, (char **) EnvList, sizeof(struct envlist));
1570 env_cmd(int argc, char *argv[])
1576 "Need an argument to 'environ' command. 'environ ?' for help.\n");
1579 c = getenvcmd(argv[1]);
1581 fprintf(stderr, "'%s': unknown argument ('environ ?' for help).\n",
1585 if (Ambiguous((void *)c)) {
1586 fprintf(stderr, "'%s': ambiguous argument ('environ ?' for help).\n",
1590 if (c->narg + 2 != argc) {
1592 "Need %s%d argument%s to 'environ %s' command. 'environ ?' for help.\n",
1593 c->narg < argc + 2 ? "only " : "",
1594 c->narg, c->narg == 1 ? "" : "s", c->name);
1597 (*c->handler)(argv[2], argv[3]);
1602 struct env_lst *next; /* pointer to next structure */
1603 struct env_lst *prev; /* pointer to previous structure */
1604 unsigned char *var; /* pointer to variable name */
1605 unsigned char *value; /* pointer to variable value */
1606 int export; /* 1 -> export with default list of variables */
1607 int welldefined; /* A well defined variable */
1610 struct env_lst envlisthead;
1612 static struct env_lst *
1613 env_find(const unsigned char *var)
1617 for (ep = envlisthead.next; ep; ep = ep->next) {
1618 if (strcmp(ep->var, var) == 0)
1627 extern char **environ;
1631 for (epp = environ; *epp; epp++) {
1632 if ((cp = strchr(*epp, '='))) {
1634 ep = env_define((unsigned char *)*epp,
1635 (unsigned char *)cp+1);
1641 * Special case for DISPLAY variable. If it is ":0.0" or
1642 * "unix:0.0", we have to get rid of "unix" and insert our
1645 if ((ep = env_find("DISPLAY"))
1646 && ((*ep->value == ':')
1647 || (strncmp((char *)ep->value, "unix:", 5) == 0))) {
1649 char *cp2 = strchr((char *)ep->value, ':');
1651 gethostname(hbuf, 256);
1653 cp = (char *)malloc(strlen(hbuf) + strlen(cp2) + 1);
1654 sprintf((char *)cp, "%s%s", hbuf, cp2);
1656 ep->value = (unsigned char *)cp;
1659 * If USER is not defined, but LOGNAME is, then add
1660 * USER with the value from LOGNAME. By default, we
1661 * don't export the USER variable.
1663 if ((env_find("USER") == NULL) && (ep = env_find("LOGNAME"))) {
1664 env_define("USER", ep->value);
1665 env_unexport("USER");
1667 env_export("DISPLAY");
1668 env_export("PRINTER");
1672 env_define(const unsigned char *var, unsigned char *value)
1676 if ((ep = env_find(var))) {
1682 ep = (struct env_lst *)malloc(sizeof(struct env_lst));
1683 ep->next = envlisthead.next;
1684 envlisthead.next = ep;
1685 ep->prev = &envlisthead;
1687 ep->next->prev = ep;
1689 ep->welldefined = opt_welldefined(var);
1691 ep->var = strdup(var);
1692 ep->value = strdup(value);
1697 env_undefine(unsigned char *var)
1701 if ((ep = env_find(var))) {
1702 ep->prev->next = ep->next;
1704 ep->next->prev = ep->prev;
1714 env_export(const unsigned char *var)
1718 if ((ep = env_find(var)))
1723 env_unexport(const unsigned char *var)
1727 if ((ep = env_find(var)))
1732 env_send(unsigned char *var)
1736 if (my_state_is_wont(TELOPT_NEW_ENVIRON)
1738 && my_state_is_wont(TELOPT_OLD_ENVIRON)
1742 "Cannot send '%s': Telnet ENVIRON option not enabled\n",
1748 fprintf(stderr, "Cannot send '%s': variable not defined\n",
1752 env_opt_start_info();
1753 env_opt_add(ep->var);
1762 for (ep = envlisthead.next; ep; ep = ep->next) {
1763 printf("%c %-20s %s\n", ep->export ? '*' : ' ',
1764 ep->var, ep->value);
1769 env_default(int init, int welldefined)
1771 static struct env_lst *nep = NULL;
1778 while ((nep = nep->next)) {
1779 if (nep->export && (nep->welldefined == welldefined))
1787 env_getvalue(const unsigned char *var)
1791 if ((ep = env_find(var)))
1796 #if defined(OLD_ENVIRON) && defined(ENV_HACK)
1798 env_varval(unsigned char *what)
1800 extern int old_env_var, old_env_value, env_auto;
1801 int len = strlen((char *)what);
1806 if (strncasecmp((char *)what, "status", len) == 0) {
1808 printf("%s%s", "VAR and VALUE are/will be ",
1809 "determined automatically\n");
1810 if (old_env_var == OLD_ENV_VAR)
1811 printf("VAR and VALUE set to correct definitions\n");
1813 printf("VAR and VALUE definitions are reversed\n");
1814 } else if (strncasecmp((char *)what, "auto", len) == 0) {
1816 old_env_var = OLD_ENV_VALUE;
1817 old_env_value = OLD_ENV_VAR;
1818 } else if (strncasecmp((char *)what, "right", len) == 0) {
1820 old_env_var = OLD_ENV_VAR;
1821 old_env_value = OLD_ENV_VALUE;
1822 } else if (strncasecmp((char *)what, "wrong", len) == 0) {
1824 old_env_var = OLD_ENV_VALUE;
1825 old_env_value = OLD_ENV_VAR;
1828 printf("Unknown \"varval\" command. (\"auto\", \"right\", \"wrong\", \"status\")\n");
1833 #ifdef AUTHENTICATION
1835 * The AUTHENTICATE command.
1841 int (*handler)(char *);
1846 auth_enable(char *),
1847 auth_disable(char *),
1852 struct authlist AuthList[] = {
1853 { "status", "Display current status of authentication information",
1854 (int (*)(char *))auth_status, 0 },
1855 { "disable", "Disable an authentication type ('auth disable ?' for more)",
1857 { "enable", "Enable an authentication type ('auth enable ?' for more)",
1859 { "help", NULL, (int (*)(char *))auth_help, 0 },
1860 { "?", "Print help information", (int (*)(char *))auth_help, 0 },
1861 { NULL, NULL, NULL, 0 },
1869 for (c = AuthList; c->name; c++) {
1872 printf("%-15s %s\n", c->name, c->help);
1881 auth_cmd(int argc, char *argv[])
1887 "Need an argument to 'auth' command. 'auth ?' for help.\n");
1891 c = (struct authlist *)
1892 genget(argv[1], (char **) AuthList, sizeof(struct authlist));
1894 fprintf(stderr, "'%s': unknown argument ('auth ?' for help).\n",
1898 if (Ambiguous((void *)c)) {
1899 fprintf(stderr, "'%s': ambiguous argument ('auth ?' for help).\n",
1903 if (c->narg + 2 != argc) {
1905 "Need %s%d argument%s to 'auth %s' command. 'auth ?' for help.\n",
1906 c->narg < argc + 2 ? "only " : "",
1907 c->narg, c->narg == 1 ? "" : "s", c->name);
1910 return((*c->handler)(argv[2]));
1916 * The ENCRYPT command.
1919 struct encryptlist {
1922 int (*handler)(char *, char *);
1929 EncryptEnable(char *, char *),
1930 EncryptDisable(char *, char *),
1931 EncryptType(char *, char *),
1932 EncryptStart(char *),
1933 EncryptStartInput(void),
1934 EncryptStartOutput(void),
1935 EncryptStop(char *),
1936 EncryptStopInput(void),
1937 EncryptStopOutput(void),
1938 EncryptStatus(void);
1942 struct encryptlist EncryptList[] = {
1943 { "enable", "Enable encryption. ('encrypt enable ?' for more)",
1944 EncryptEnable, 1, 1, 2 },
1945 { "disable", "Disable encryption. ('encrypt enable ?' for more)",
1946 EncryptDisable, 0, 1, 2 },
1947 { "type", "Set encryption type. ('encrypt type ?' for more)",
1948 EncryptType, 0, 1, 1 },
1949 { "start", "Start encryption. ('encrypt start ?' for more)",
1950 (int (*)(char *, char *))EncryptStart, 1, 0, 1 },
1951 { "stop", "Stop encryption. ('encrypt stop ?' for more)",
1952 (int (*)(char *, char *))EncryptStop, 1, 0, 1 },
1953 { "input", "Start encrypting the input stream",
1954 (int (*)(char *, char *))EncryptStartInput, 1, 0, 0 },
1955 { "-input", "Stop encrypting the input stream",
1956 (int (*)(char *, char *))EncryptStopInput, 1, 0, 0 },
1957 { "output", "Start encrypting the output stream",
1958 (int (*)(char *, char *))EncryptStartOutput, 1, 0, 0 },
1959 { "-output", "Stop encrypting the output stream",
1960 (int (*)(char *, char *))EncryptStopOutput, 1, 0, 0 },
1962 { "status", "Display current status of authentication information",
1963 (int (*)(char *, char *))EncryptStatus, 0, 0, 0 },
1964 { "help", NULL, (int (*)(char *, char *))EncryptHelp, 0, 0, 0 },
1965 { "?", "Print help information", (int (*)(char *, char *))EncryptHelp, 0, 0, 0 },
1966 { NULL, NULL, NULL, 0, 0, 0 },
1972 struct encryptlist *c;
1974 for (c = EncryptList; c->name; c++) {
1977 printf("%-15s %s\n", c->name, c->help);
1986 encrypt_cmd(int argc, char *argv[])
1988 struct encryptlist *c;
1992 "Need an argument to 'encrypt' command. 'encrypt ?' for help.\n");
1996 c = (struct encryptlist *)
1997 genget(argv[1], (char **) EncryptList, sizeof(struct encryptlist));
1999 fprintf(stderr, "'%s': unknown argument ('encrypt ?' for help).\n",
2003 if (Ambiguous((void *)c)) {
2004 fprintf(stderr, "'%s': ambiguous argument ('encrypt ?' for help).\n",
2009 if (argc < c->minarg || argc > c->maxarg) {
2010 if (c->minarg == c->maxarg) {
2011 fprintf(stderr, "Need %s%d argument%s ",
2012 c->minarg < argc ? "only " : "", c->minarg,
2013 c->minarg == 1 ? "" : "s");
2015 fprintf(stderr, "Need %s%d-%d arguments ",
2016 c->maxarg < argc ? "only " : "", c->minarg, c->maxarg);
2018 fprintf(stderr, "to 'encrypt %s' command. 'encrypt ?' for help.\n",
2022 if (c->needconnect && !connected) {
2023 if (!(argc && (isprefix(argv[2], "help") || isprefix(argv[2], "?")))) {
2024 printf("?Need to be connected first.\n");
2028 return ((*c->handler)(argc > 0 ? argv[2] : 0,
2029 argc > 1 ? argv[3] : 0));
2031 #endif /* ENCRYPTION */
2034 * Print status about the connection.
2038 status(int argc, char *argv[])
2041 printf("Connected to %s.\n", hostname);
2042 if ((argc < 2) || strcmp(argv[1], "notmuch")) {
2043 int mode = getconnmode();
2045 if (my_want_state_is_will(TELOPT_LINEMODE)) {
2046 printf("Operating with LINEMODE option\n");
2047 printf("%s line editing\n", (mode&MODE_EDIT) ? "Local" : "No");
2048 printf("%s catching of signals\n",
2049 (mode&MODE_TRAPSIG) ? "Local" : "No");
2051 #ifdef KLUDGELINEMODE
2052 } else if (kludgelinemode && my_want_state_is_dont(TELOPT_SGA)) {
2053 printf("Operating in obsolete linemode\n");
2056 printf("Operating in single character mode\n");
2058 printf("Catching signals locally\n");
2060 printf("%s character echo\n", (mode&MODE_ECHO) ? "Local" : "Remote");
2061 if (my_want_state_is_will(TELOPT_LFLOW))
2062 printf("%s flow control\n", (mode&MODE_FLOW) ? "Local" : "No");
2065 #endif /* ENCRYPTION */
2068 printf("No connection.\n");
2070 printf("Escape character is '%s'.\n", control(escape));
2071 (void) fflush(stdout);
2077 * Function that gets called when SIGINFO is received.
2082 (void) call(status, "status", "notmuch", 0);
2087 sockaddr_ntop(struct sockaddr *sa)
2090 static char addrbuf[INET6_ADDRSTRLEN];
2092 switch (sa->sa_family) {
2094 addr = &((struct sockaddr_in *)sa)->sin_addr;
2097 addr = &((struct sockaddr_un *)sa)->sun_path;
2101 addr = &((struct sockaddr_in6 *)sa)->sin6_addr;
2107 inet_ntop(sa->sa_family, addr, addrbuf, sizeof(addrbuf));
2111 #if defined(IPSEC) && defined(IPSEC_POLICY_IPSEC)
2113 setpolicy(int lnet, struct addrinfo *res, char *policy)
2122 buf = ipsec_set_policy(policy, strlen(policy));
2124 printf("%s\n", ipsec_strerror());
2127 level = res->ai_family == AF_INET ? IPPROTO_IP : IPPROTO_IPV6;
2128 optname = res->ai_family == AF_INET ? IP_IPSEC_POLICY : IPV6_IPSEC_POLICY;
2129 if (setsockopt(lnet, level, optname, buf, ipsec_get_policylen(buf)) < 0){
2130 perror("setsockopt");
2141 * When an Address Family related error happend, check if retry with
2142 * another AF is possible or not.
2143 * Return 1, if retry with another af is OK. Else, return 0.
2146 switch_af(struct addrinfo **aip)
2149 struct addrinfo *ai;
2152 nextaf = (ai->ai_family == AF_INET) ? AF_INET6 : AF_INET;
2155 while (ai != NULL && ai->ai_family != nextaf);
2165 tn(int argc, char *argv[])
2170 int srcroute = 0, result;
2171 char *cmd, *hostp = NULL, *portp = NULL, *user = NULL;
2172 char *src_addr = NULL;
2173 struct addrinfo hints, *res, *res0 = NULL, *src_res, *src_res0 = NULL;
2174 int error = 0, af_error = 0;
2177 printf("?Already connected to %s\n", hostname);
2182 (void) strcpy(line, "open ");
2184 (void) fgets(&line[strlen(line)], sizeof(line) - strlen(line), stdin);
2192 if (strcmp(*argv, "help") == 0 || isprefix(*argv, "?"))
2194 if (strcmp(*argv, "-l") == 0) {
2202 if (strcmp(*argv, "-a") == 0) {
2207 if (strcmp(*argv, "-s") == 0) {
2215 if (hostp == NULL) {
2220 if (portp == NULL) {
2226 printf("usage: %s [-l user] [-a] [-s src_addr] host-name [port]\n", cmd);
2233 if (src_addr != NULL) {
2234 memset(&hints, 0, sizeof(hints));
2235 hints.ai_flags = AI_NUMERICHOST;
2236 hints.ai_family = family;
2237 hints.ai_socktype = SOCK_STREAM;
2238 error = getaddrinfo(src_addr, 0, &hints, &src_res);
2239 if (error == EAI_NODATA) {
2241 error = getaddrinfo(src_addr, 0, &hints, &src_res);
2244 fprintf(stderr, "%s: %s\n", src_addr, gai_strerror(error));
2245 if (error == EAI_SYSTEM)
2246 fprintf(stderr, "%s: %s\n", src_addr, strerror(errno));
2252 if (hostp[0] == '/') {
2253 struct sockaddr_un su;
2255 if (strlen(hostp) >= sizeof(su.sun_path)) {
2256 fprintf(stderr, "hostname too long for unix domain socket: %s",
2260 memset(&su, 0, sizeof su);
2261 su.sun_family = AF_UNIX;
2262 strncpy(su.sun_path, hostp, sizeof su.sun_path);
2263 printf("Trying %s...\n", hostp);
2264 net = socket(PF_UNIX, SOCK_STREAM, 0);
2269 if (connect(net, (struct sockaddr *)&su, sizeof su) == -1) {
2270 perror(su.sun_path);
2271 (void) NetClose(net);
2275 } else if (hostp[0] == '@' || hostp[0] == '!') {
2278 family == AF_INET6 ||
2280 (hostname = strrchr(hostp, ':')) == NULL)
2281 hostname = strrchr(hostp, '@');
2282 if (hostname == NULL) {
2292 portp = strdup("telnet");
2293 } else if (*portp == '-') {
2299 memset(&hints, 0, sizeof(hints));
2300 hints.ai_flags = AI_NUMERICHOST;
2301 hints.ai_family = family;
2302 hints.ai_socktype = SOCK_STREAM;
2303 error = getaddrinfo(hostname, portp, &hints, &res);
2305 hints.ai_flags = AI_CANONNAME;
2306 error = getaddrinfo(hostname, portp, &hints, &res);
2309 fprintf(stderr, "%s: %s\n", hostname, gai_strerror(error));
2310 if (error == EAI_SYSTEM)
2311 fprintf(stderr, "%s: %s\n", hostname, strerror(errno));
2315 if (hints.ai_flags == AI_NUMERICHOST) {
2316 /* hostname has numeric */
2320 gni_err = getnameinfo(res->ai_addr, res->ai_addr->sa_len,
2321 _hostname, sizeof(_hostname) - 1, NULL, 0,
2324 (void) strncpy(_hostname, hostp, sizeof(_hostname) - 1);
2325 _hostname[sizeof(_hostname)-1] = '\0';
2326 hostname = _hostname;
2328 /* hostname has FQDN */
2330 (void) strncpy(_hostname, hostname, sizeof(_hostname) - 1);
2331 else if (res->ai_canonname != NULL)
2332 strcpy(_hostname, res->ai_canonname);
2334 (void) strncpy(_hostname, hostp, sizeof(_hostname) - 1);
2335 _hostname[sizeof(_hostname)-1] = '\0';
2336 hostname = _hostname;
2342 if (srcroute != 0) {
2343 static char hostbuf[BUFSIZ];
2345 if (af_error == 0) { /* save intermediate hostnames for retry */
2346 strncpy(hostbuf, hostp, BUFSIZ - 1);
2347 hostbuf[BUFSIZ - 1] = '\0';
2351 result = sourceroute(res, hostp, &srp, &srlen, &proto, &opt);
2354 if (family == AF_UNSPEC && af_error == 0 &&
2355 switch_af(&res) == 1) {
2362 } else if (result == -1) {
2363 printf("Bad source route option: %s\n", hostp);
2369 printf("Trying %s...\n", sockaddr_ntop(res->ai_addr));
2370 net = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
2374 if (family == AF_UNSPEC && af_error == 0 &&
2375 switch_af(&res) == 1) {
2380 perror("telnet: socket");
2383 if (srp && setsockopt(net, proto, opt, (char *)srp, srlen) < 0)
2384 perror("setsockopt (source route)");
2385 #if defined(IPPROTO_IP) && defined(IP_TOS)
2386 if (res->ai_family == PF_INET) {
2387 # if defined(HAS_GETTOS)
2389 if (tos < 0 && (tp = gettosbyname("telnet", "tcp")))
2393 tos = IPTOS_LOWDELAY;
2395 && (setsockopt(net, IPPROTO_IP, IP_TOS,
2396 (char *)&tos, sizeof(int)) < 0)
2397 && (errno != ENOPROTOOPT))
2398 perror("telnet: setsockopt (IP_TOS) (ignored)");
2400 #endif /* defined(IPPROTO_IP) && defined(IP_TOS) */
2402 if (debug && SetSockOpt(net, SOL_SOCKET, SO_DEBUG, 1) < 0) {
2403 perror("setsockopt (SO_DEBUG)");
2406 if (src_addr != NULL) {
2407 for (src_res = src_res0; src_res != NULL; src_res = src_res->ai_next)
2408 if (src_res->ai_family == res->ai_family)
2410 if (src_res == NULL)
2412 if (bind(net, src_res->ai_addr, src_res->ai_addrlen) == -1) {
2414 if (family == AF_UNSPEC && af_error == 0 &&
2415 switch_af(&res) == 1) {
2417 (void) NetClose(net);
2422 (void) NetClose(net);
2426 #if defined(IPSEC) && defined(IPSEC_POLICY_IPSEC)
2427 if (setpolicy(net, res, ipsec_policy_in) < 0) {
2428 (void) NetClose(net);
2431 if (setpolicy(net, res, ipsec_policy_out) < 0) {
2432 (void) NetClose(net);
2437 if (connect(net, res->ai_addr, res->ai_addrlen) < 0) {
2438 struct addrinfo *next;
2440 next = res->ai_next;
2441 /* If already an af failed, only try same af. */
2443 while (next != NULL && next->ai_family != res->ai_family)
2444 next = next->ai_next;
2445 warn("connect to address %s", sockaddr_ntop(res->ai_addr));
2448 (void) NetClose(net);
2451 warnx("Unable to connect to remote host");
2452 (void) NetClose(net);
2456 #ifdef AUTHENTICATION
2458 auth_encrypt_connect(connected);
2461 } while (connected == 0);
2463 if (src_res0 != NULL)
2464 freeaddrinfo(src_res0);
2465 cmdrc(hostp, hostname);
2467 if (autologin && user == NULL) {
2470 user = getenv("USER");
2472 ((pw = getpwnam(user)) && pw->pw_uid != getuid())) {
2473 if ((pw = getpwuid(getuid())))
2480 env_define("USER", user);
2483 (void) call(status, "status", "notmuch", 0);
2484 if (setjmp(peerdied) == 0)
2486 (void) NetClose(net);
2487 ExitString("Connection closed by foreign host.\n",1);
2492 if (src_res0 != NULL)
2493 freeaddrinfo(src_res0);
2497 #define HELPINDENT (sizeof ("connect"))
2500 openhelp[] = "connect to a site",
2501 closehelp[] = "close current connection",
2502 logouthelp[] = "forcibly logout remote user and close the connection",
2503 quithelp[] = "exit telnet",
2504 statushelp[] = "print status information",
2505 helphelp[] = "print help information",
2506 sendhelp[] = "transmit special characters ('send ?' for more)",
2507 sethelp[] = "set operating parameters ('set ?' for more)",
2508 unsethelp[] = "unset operating parameters ('unset ?' for more)",
2509 togglestring[] ="toggle operating parameters ('toggle ?' for more)",
2510 slchelp[] = "change state of special characters ('slc ?' for more)",
2511 displayhelp[] = "display operating parameters",
2512 #ifdef AUTHENTICATION
2513 authhelp[] = "turn on (off) authentication ('auth ?' for more)",
2516 encrypthelp[] = "turn on (off) encryption ('encrypt ?' for more)",
2517 #endif /* ENCRYPTION */
2518 zhelp[] = "suspend telnet",
2520 opiehelp[] = "compute response to OPIE challenge",
2522 shellhelp[] = "invoke a subshell",
2523 envhelp[] = "change environment variables ('environ ?' for more)",
2524 modestring[] = "try to enter line or character mode ('mode ?' for more)";
2526 static Command cmdtab[] = {
2527 { "close", closehelp, bye, 1 },
2528 { "logout", logouthelp, (int (*)(int, char **))logout, 1 },
2529 { "display", displayhelp, display, 0 },
2530 { "mode", modestring, modecmd, 0 },
2531 { "telnet", openhelp, tn, 0 },
2532 { "open", openhelp, tn, 0 },
2533 { "quit", quithelp, (int (*)(int, char **))quit, 0 },
2534 { "send", sendhelp, sendcmd, 0 },
2535 { "set", sethelp, setcmd, 0 },
2536 { "unset", unsethelp, unsetcmd, 0 },
2537 { "status", statushelp, status, 0 },
2538 { "toggle", togglestring, toggle, 0 },
2539 { "slc", slchelp, slccmd, 0 },
2540 #ifdef AUTHENTICATION
2541 { "auth", authhelp, auth_cmd, 0 },
2544 { "encrypt", encrypthelp, encrypt_cmd, 0 },
2545 #endif /* ENCRYPTION */
2546 { "z", zhelp, (int (*)(int, char **))suspend, 0 },
2547 { "!", shellhelp, shell, 1 },
2548 { "environ", envhelp, env_cmd, 0 },
2549 { "?", helphelp, help, 0 },
2551 { "opie", opiehelp, opie_calc, 0 },
2553 { NULL, NULL, NULL, 0 }
2556 static char crmodhelp[] = "deprecated command -- use 'toggle crmod' instead";
2557 static char escapehelp[] = "deprecated command -- use 'set escape' instead";
2559 static Command cmdtab2[] = {
2560 { "help", 0, help, 0 },
2561 { "escape", escapehelp, setescape, 0 },
2562 { "crmod", crmodhelp, (int (*)(int, char **))togcrmod, 0 },
2563 { NULL, NULL, NULL, 0 }
2568 * Call routine with argc, argv set from args (terminated by 0).
2572 call(intrtn_t routine, ...)
2578 va_start(ap, routine);
2579 while ((args[argno++] = va_arg(ap, char *)) != NULL);
2581 return (*routine)(argno-1, args);
2590 if ((cm = (Command *) genget(name, (char **) cmdtab, sizeof(Command))))
2592 return (Command *) genget(name, (char **) cmdtab2, sizeof(Command));
2596 command(int top, const char *tbuf, int cnt)
2604 (void) signal(SIGINT, SIG_DFL);
2605 (void) signal(SIGQUIT, SIG_DFL);
2608 if (rlogin == _POSIX_VDISABLE)
2609 printf("%s> ", prompt);
2613 while (cnt > 0 && (*cp++ = *tbuf++) != '\n')
2616 if (cp == line || *--cp != '\n' || cp == line)
2619 if (rlogin == _POSIX_VDISABLE)
2620 printf("%s\n", line);
2623 if (rlogin != _POSIX_VDISABLE)
2624 printf("%s> ", prompt);
2625 if (fgets(line, sizeof(line), stdin) == NULL) {
2626 if (feof(stdin) || ferror(stdin)) {
2636 if (margv[0] == NULL) {
2639 c = getcmd(margv[0]);
2640 if (Ambiguous((void *)c)) {
2641 printf("?Ambiguous command\n");
2645 printf("?Invalid command\n");
2648 if (c->needconnect && !connected) {
2649 printf("?Need to be connected first.\n");
2652 if ((*c->handler)(margc, margv)) {
2658 longjmp(toplevel, 1);
2669 help(int argc, char *argv[])
2674 printf("Commands may be abbreviated. Commands are:\n\n");
2675 for (c = cmdtab; c->name; c++)
2677 printf("%-*s\t%s\n", (int)HELPINDENT, c->name,
2682 else while (--argc > 0) {
2686 if (Ambiguous((void *)c))
2687 printf("?Ambiguous help command %s\n", arg);
2689 printf("?Invalid help command %s\n", arg);
2691 printf("%s\n", c->help);
2696 static char *rcname = NULL;
2697 static char rcbuf[128];
2700 cmdrc(char *m1, char *m2)
2705 int l1 = strlen(m1);
2706 int l2 = strlen(m2);
2707 char m1save[MAXHOSTNAMELEN];
2712 strlcpy(m1save, m1, sizeof(m1save));
2715 if (rcname == NULL) {
2716 rcname = getenv("HOME");
2717 if (rcname && (strlen(rcname) + 10) < sizeof(rcbuf))
2718 strcpy(rcbuf, rcname);
2721 strcat(rcbuf, "/.telnetrc");
2725 if ((rcfile = fopen(rcname, "r")) == NULL) {
2730 if (fgets(line, sizeof(line), rcfile) == NULL)
2737 if (!isspace(line[0]))
2740 if (gotmachine == 0) {
2741 if (isspace(line[0]))
2743 if (strncasecmp(line, m1, l1) == 0)
2744 strncpy(line, &line[l1], sizeof(line) - l1);
2745 else if (strncasecmp(line, m2, l2) == 0)
2746 strncpy(line, &line[l2], sizeof(line) - l2);
2747 else if (strncasecmp(line, "DEFAULT", 7) == 0)
2748 strncpy(line, &line[7], sizeof(line) - 7);
2751 if (line[0] != ' ' && line[0] != '\t' && line[0] != '\n')
2756 if (margv[0] == NULL)
2758 c = getcmd(margv[0]);
2759 if (Ambiguous((void *)c)) {
2760 printf("?Ambiguous command: %s\n", margv[0]);
2764 printf("?Invalid command: %s\n", margv[0]);
2768 * This should never happen...
2770 if (c->needconnect && !connected) {
2771 printf("?Need to be connected first for %s.\n", margv[0]);
2774 (*c->handler)(margc, margv);
2780 * Source route is handed in as
2781 * [!]@hop1@hop2...[@|:]dst
2782 * If the leading ! is present, it is a
2783 * strict source route, otherwise it is
2784 * assmed to be a loose source route.
2786 * We fill in the source route option as
2787 * hop1,hop2,hop3...dest
2788 * and return a pointer to hop1, which will
2789 * be the address to connect() to.
2793 * res: ponter to addrinfo structure which contains sockaddr to
2794 * the host to connect to.
2796 * arg: pointer to route list to decipher
2798 * cpp: If *cpp is not equal to NULL, this is a
2799 * pointer to a pointer to a character array
2800 * that should be filled in with the option.
2802 * lenp: pointer to an integer that contains the
2803 * length of *cpp if *cpp != NULL.
2805 * protop: pointer to an integer that should be filled in with
2806 * appropriate protocol for setsockopt, as socket
2809 * optp: pointer to an integer that should be filled in with
2810 * appropriate option for setsockopt, as socket protocol
2815 * If the return value is 1, then all operations are
2816 * successful. If the
2817 * return value is -1, there was a syntax error in the
2818 * option, either unknown characters, or too many hosts.
2819 * If the return value is 0, one of the hostnames in the
2820 * path is unknown, and *cpp is set to point to the bad
2823 * *cpp: If *cpp was equal to NULL, it will be filled
2824 * in with a pointer to our static area that has
2825 * the option filled in. This will be 32bit aligned.
2827 * *lenp: This will be filled in with how long the option
2828 * pointed to by *cpp is.
2830 * *protop: This will be filled in with appropriate protocol for
2831 * setsockopt, as socket protocol family.
2833 * *optp: This will be filled in with appropriate option for
2834 * setsockopt, as socket protocol family.
2837 sourceroute(struct addrinfo *ai, char *arg, char **cpp, int *lenp, int *protop, int *optp)
2839 static char buf[1024 + ALIGNBYTES]; /*XXX*/
2840 char *cp, *cp2, *lsrp, *ep;
2841 struct sockaddr_in *_sin;
2843 struct sockaddr_in6 *sin6;
2844 struct cmsghdr *cmsg;
2845 struct ip6_rthdr *rth;
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) {
2889 * RFC3542 has obsoleted IPV6_PKTOPTIONS socket option.
2891 #ifdef COMPAT_RFC1883 /* XXX */
2895 *protop = IPPROTO_IPV6;
2896 *optp = IPV6_PKTOPTIONS;
2899 #endif /* COMPAT_RFC1883 */
2904 * Next, decide whether we have a loose source
2905 * route or a strict source route, and fill in
2906 * the begining of the option.
2910 *lsrp++ = IPOPT_SSRR;
2912 *lsrp++ = IPOPT_LSRR;
2917 lsrp++; /* skip over length, we'll fill it in later */
2919 *protop = IPPROTO_IP;
2924 memset(&hints, 0, sizeof(hints));
2925 hints.ai_family = ai->ai_family;
2926 hints.ai_socktype = SOCK_STREAM;
2930 ai->ai_family != AF_INET6 &&
2934 else for (cp2 = cp; (c = *cp2); cp2++) {
2939 } else if (c == '@') {
2943 ai->ai_family != AF_INET6 &&
2954 hints.ai_flags = AI_NUMERICHOST;
2955 error = getaddrinfo(cp, NULL, &hints, &res);
2956 if (error == EAI_NODATA) {
2958 error = getaddrinfo(cp, NULL, &hints, &res);
2961 fprintf(stderr, "%s: %s\n", cp, gai_strerror(error));
2962 if (error == EAI_SYSTEM)
2963 fprintf(stderr, "%s: %s\n", cp,
2969 if (res->ai_family == AF_INET6) {
2974 _sin = (struct sockaddr_in *)res->ai_addr;
2975 memcpy(lsrp, (char *)&_sin->sin_addr, 4);
2983 * Check to make sure there is space for next address
2986 #ifdef COMPAT_RFC1883 /* XXX */
2987 if (res->ai_family == AF_INET6) {
2988 if (((char *)CMSG_DATA(cmsg) +
2989 sizeof(struct ip6_rthdr)) > ep)
2992 #endif /* COMPAT_RFC1883 */
2999 if (res->ai_family == AF_INET6) {
3000 #ifdef COMPAT_RFC1883 /* XXX */
3002 #endif /* COMPAT_RFC1883 */
3006 if ((*(*cpp+IPOPT_OLEN) = lsrp - *cpp) <= 7) {
3011 *lsrp++ = IPOPT_NOP; /* 32 bit word align it */
3012 *lenp = lsrp - *cpp;