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/usr.bin/telnet/commands.c,v 1.21.2.6 2002/11/30 05:35:13 eric Exp $
35 * $DragonFly: src/usr.bin/telnet/commands.c,v 1.7 2008/09/04 09:08:22 hasso 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 <netinet/in_systm.h>
69 #include <netinet/ip.h>
70 #include <netinet/ip6.h>
72 #ifndef MAXHOSTNAMELEN
73 #define MAXHOSTNAMELEN 256
76 typedef int (*intrtn_t)(int, char **);
78 #if defined(IPPROTO_IP) && defined(IP_TOS)
80 #endif /* defined(IPPROTO_IP) && defined(IP_TOS) */
83 static char _hostname[MAXHOSTNAMELEN];
85 static int help(int, char **);
86 static int call(intrtn_t, ...);
87 static void cmdrc(char *, char *);
89 static int switch_af(struct addrinfo **);
91 static int togglehelp(void);
92 static int send_tncmd(void (*)(int, int), const char *, char *);
93 static int setmod(int);
94 static int clearmode(int);
95 static int modehelp(void);
96 static int sourceroute(struct addrinfo *, char *, char **, int *, int *, int *);
99 const char *name; /* command name */
100 const char *help; /* help string (NULL for no help) */
101 int (*handler)(int, char **); /* routine which executes command */
102 int needconnect; /* Do we need to be connected to execute? */
105 static char line[256];
106 static char saveline[256];
108 static char *margv[20];
111 #include <sys/wait.h>
112 #define PATH_OPIEKEY "/usr/bin/opiekey"
114 opie_calc(int argc, char *argv[])
119 printf("%s sequence challenge\n", argv[0]);
125 execv(PATH_OPIEKEY, argv);
131 (void) wait(&status);
132 if (WIFEXITED(status))
133 return (WEXITSTATUS(status));
147 if (*cp == '!') { /* Special case shell escape */
148 strcpy(saveline, line); /* save for shell command */
149 *argp++ = strdup("!"); /* No room in string to get this */
161 for (cp2 = cp; c != '\0'; c = *++cp) {
169 if ((c = *++cp) == '\0')
171 } else if (c == '"') {
174 } else if (c == '\'') {
177 } else if (isspace(c))
191 * Make a character string into a number.
193 * Todo: 1. Could take random integers (12, 0x12, 012, 0b1).
206 c = b | 0x40; /* DEL */
219 * Construct a control character sequence
220 * for a special character.
227 * The only way I could get the Sun 3.5 compiler
229 * if ((unsigned int)c >= 0x80)
230 * was to assign "c" to an unsigned int variable...
233 unsigned int uic = (unsigned int)c;
237 if (c == (cc_t)_POSIX_VDISABLE) {
242 buf[1] = ((c>>6)&07) + '0';
243 buf[2] = ((c>>3)&07) + '0';
244 buf[3] = (c&07) + '0';
246 } else if (uic >= 0x20) {
258 * The following are data structures and routines for
259 * the "send" command.
264 const char *name; /* How user refers to it (case independent) */
265 const char *help; /* Help information (0 ==> no help) */
266 int needconnect; /* Need to be connected */
267 int narg; /* Number of arguments */
268 int (*handler)(char *, ...); /* Routine to perform (for special ops) */
269 int nbyte; /* Number of bytes to send this command */
270 int what; /* Character to be sent (<0 ==> special) */
278 send_dontcmd(char *),
279 send_willcmd(char *),
280 send_wontcmd(char *);
282 static struct sendlist Sendlist[] = {
283 { "ao", "Send Telnet Abort output", 1, 0, NULL, 2, AO },
284 { "ayt", "Send Telnet 'Are You There'", 1, 0, NULL, 2, AYT },
285 { "brk", "Send Telnet Break", 1, 0, NULL, 2, BREAK },
286 { "break", NULL, 1, 0, NULL, 2, BREAK },
287 { "ec", "Send Telnet Erase Character", 1, 0, NULL, 2, EC },
288 { "el", "Send Telnet Erase Line", 1, 0, NULL, 2, EL },
289 { "escape", "Send current escape character",1, 0, (int (*)(char *, ...))send_esc, 1, 0 },
290 { "ga", "Send Telnet 'Go Ahead' sequence", 1, 0, NULL, 2, GA },
291 { "ip", "Send Telnet Interrupt Process",1, 0, NULL, 2, IP },
292 { "intp", NULL, 1, 0, NULL, 2, IP },
293 { "interrupt", NULL, 1, 0, NULL, 2, IP },
294 { "intr", NULL, 1, 0, NULL, 2, IP },
295 { "nop", "Send Telnet 'No operation'", 1, 0, NULL, 2, NOP },
296 { "eor", "Send Telnet 'End of Record'", 1, 0, NULL, 2, EOR },
297 { "abort", "Send Telnet 'Abort Process'", 1, 0, NULL, 2, ABORT },
298 { "susp", "Send Telnet 'Suspend Process'",1, 0, NULL, 2, SUSP },
299 { "eof", "Send Telnet End of File Character", 1, 0, NULL, 2, xEOF },
300 { "synch", "Perform Telnet 'Synch operation'", 1, 0, (int (*)(char *, ...))dosynch, 2, 0 },
301 { "getstatus", "Send request for STATUS", 1, 0, (int (*)(char *, ...))get_status, 6, 0 },
302 { "?", "Display send options", 0, 0, (int (*)(char *, ...))send_help, 0, 0 },
303 { "help", NULL, 0, 0, (int (*)(char *, ...))send_help, 0, 0 },
304 { "do", NULL, 0, 1, (int (*)(char *, ...))send_docmd, 3, 0 },
305 { "dont", NULL, 0, 1, (int (*)(char *, ...))send_dontcmd, 3, 0 },
306 { "will", NULL, 0, 1, (int (*)(char *, ...))send_willcmd, 3, 0 },
307 { "wont", NULL, 0, 1, (int (*)(char *, ...))send_wontcmd, 3, 0 },
308 { NULL, NULL, 0, 0, NULL, 0, 0 }
311 #define GETSEND(name) ((struct sendlist *) genget(name, (char **) Sendlist, \
312 sizeof(struct sendlist)))
315 sendcmd(int argc, char *argv[])
317 int count; /* how many bytes we are going to need to send */
319 struct sendlist *s; /* pointer to current command */
324 printf("need at least one argument for 'send' command\n");
325 printf("'send ?' for help\n");
329 * First, validate all the send arguments.
330 * In addition, we see how much space we are going to need, and
331 * whether or not we will be doing a "SYNCH" operation (which
332 * flushes the network queue).
335 for (i = 1; i < argc; i++) {
336 s = GETSEND(argv[i]);
338 printf("Unknown send argument '%s'\n'send ?' for help.\n",
341 } else if (Ambiguous((void *)s)) {
342 printf("Ambiguous send argument '%s'\n'send ?' for help.\n",
346 if (i + s->narg >= argc) {
348 "Need %d argument%s to 'send %s' command. 'send %s ?' for help.\n",
349 s->narg, s->narg == 1 ? "" : "s", s->name, s->name);
353 if ((void *)s->handler == (void *)send_help) {
359 needconnect += s->needconnect;
361 if (!connected && needconnect) {
362 printf("?Need to be connected first.\n");
363 printf("'send ?' for help\n");
366 /* Now, do we have enough room? */
367 if (NETROOM() < count) {
368 printf("There is not enough room in the buffer TO the network\n");
369 printf("to process your request. Nothing will be done.\n");
370 printf("('send synch' will throw away most data in the network\n");
371 printf("buffer, if this might help.)\n");
374 /* OK, they are all OK, now go through again and actually send */
376 for (i = 1; i < argc; i++) {
377 if ((s = GETSEND(argv[i])) == 0) {
378 fprintf(stderr, "Telnet 'send' error - argument disappeared!\n");
384 success += (*s->handler)((s->narg > 0) ? argv[i+1] : 0,
385 (s->narg > 1) ? argv[i+2] : 0);
388 NET2ADD(IAC, s->what);
389 printoption("SENT", IAC, s->what);
392 return (count == success);
403 send_docmd(char *name)
405 return(send_tncmd(send_do, "do", name));
409 send_dontcmd(char *name)
411 return(send_tncmd(send_dont, "dont", name));
415 send_willcmd(char *name)
417 return(send_tncmd(send_will, "will", name));
421 send_wontcmd(char *name)
423 return(send_tncmd(send_wont, "wont", name));
427 send_tncmd(void (*func)(int, int), const char *cmd, char *name)
430 extern char *telopts[];
433 if (isprefix(name, "help") || isprefix(name, "?")) {
436 printf("Usage: send %s <value|option>\n", cmd);
437 printf("\"value\" must be from 0 to 255\n");
438 printf("Valid options are:\n\t");
441 for (cpp = telopts; *cpp; cpp++) {
442 len = strlen(*cpp) + 3;
443 if (col + len > 65) {
447 printf(" \"%s\"", *cpp);
453 cpp = (char **)genget(name, telopts, sizeof(char *));
454 if (Ambiguous(cpp)) {
455 fprintf(stderr,"'%s': ambiguous argument ('send %s ?' for help).\n",
464 while (*cp >= '0' && *cp <= '9') {
470 fprintf(stderr, "'%s': unknown argument ('send %s ?' for help).\n",
473 } else if (val < 0 || val > 255) {
474 fprintf(stderr, "'%s': bad value ('send %s ?' for help).\n",
480 printf("?Need to be connected first.\n");
490 struct sendlist *s; /* pointer to current command */
491 for (s = Sendlist; s->name; s++) {
493 printf("%-15s %s\n", s->name, s->help);
499 * The following are the routines and data structures referred
500 * to by the arguments to the "toggle" command.
515 (SetSockOpt(net, SOL_SOCKET, SO_DEBUG, debug)) < 0) {
516 perror("setsockopt (SO_DEBUG)");
520 if (net > 0 && SetSockOpt(net, SOL_SOCKET, SO_DEBUG, 1) < 0)
521 perror("setsockopt (SO_DEBUG)");
523 printf("Cannot turn off socket debugging\n");
533 printf("Will send carriage returns as telnet <CR><LF>.\n");
535 printf("Will send carriage returns as telnet <CR><NUL>.\n");
545 donebinarytoggle = 1;
550 if (my_want_state_is_will(TELOPT_BINARY) &&
551 my_want_state_is_do(TELOPT_BINARY)) {
553 } else if (my_want_state_is_wont(TELOPT_BINARY) &&
554 my_want_state_is_dont(TELOPT_BINARY)) {
557 val = binmode ? 0 : 1;
561 if (my_want_state_is_will(TELOPT_BINARY) &&
562 my_want_state_is_do(TELOPT_BINARY)) {
563 printf("Already operating in binary mode with remote host.\n");
565 printf("Negotiating binary mode with remote host.\n");
569 if (my_want_state_is_wont(TELOPT_BINARY) &&
570 my_want_state_is_dont(TELOPT_BINARY)) {
571 printf("Already in network ascii mode with remote host.\n");
573 printf("Negotiating network ascii mode with remote host.\n");
583 donebinarytoggle = 1;
586 val = my_want_state_is_do(TELOPT_BINARY) ? 0 : 1;
589 if (my_want_state_is_do(TELOPT_BINARY)) {
590 printf("Already receiving in binary mode.\n");
592 printf("Negotiating binary mode on input.\n");
596 if (my_want_state_is_dont(TELOPT_BINARY)) {
597 printf("Already receiving in network ascii mode.\n");
599 printf("Negotiating network ascii mode on input.\n");
609 donebinarytoggle = 1;
612 val = my_want_state_is_will(TELOPT_BINARY) ? 0 : 1;
615 if (my_want_state_is_will(TELOPT_BINARY)) {
616 printf("Already transmitting in binary mode.\n");
618 printf("Negotiating binary mode on output.\n");
622 if (my_want_state_is_wont(TELOPT_BINARY)) {
623 printf("Already transmitting in network ascii mode.\n");
625 printf("Negotiating network ascii mode on output.\n");
633 const char *name; /* name of toggle */
634 const char *help; /* help message */
635 int (*handler)(int); /* routine to do actual setting */
637 const char *actionexplanation;
640 static struct togglelist Togglelist[] = {
642 "flushing of output when sending interrupt characters",
645 "flush output when sending interrupt characters" },
647 "automatic sending of interrupt characters in urgent mode",
650 "send interrupt characters in urgent mode" },
652 "don't read ~/.telnetrc file",
655 "skip reading of ~/.telnetrc file" },
657 "sending and receiving of binary data",
662 "receiving of binary data",
667 "sending of binary data",
672 "sending carriage returns as telnet <CR><LF>",
673 (int (*)(int))togcrlf,
677 "mapping of received carriage returns",
680 "map carriage return on output" },
682 "local recognition of certain control characters",
683 (int (*)(int))lclchars,
685 "recognize certain control characters" },
686 { " ", "", NULL, NULL, NULL }, /* empty line */
689 (int (*)(int))togdebug,
691 "turn on socket level debugging" },
693 "printing of hexadecimal network data (debugging)",
696 "print hexadecimal representation of network traffic" },
698 "output of \"netdata\" to user readable format (debugging)",
701 "print user readable output for \"netdata\"" },
703 "viewing of options processing (debugging)",
706 "show option processing" },
708 "(debugging) toggle printing of hexadecimal terminal data",
711 "print hexadecimal representation of terminal traffic" },
714 (int (*)(int))togglehelp,
717 { NULL, NULL, NULL, NULL, NULL },
720 (int (*)(int))togglehelp,
723 { NULL, NULL, NULL, NULL, NULL }
729 struct togglelist *c;
731 for (c = Togglelist; c->name; c++) {
734 printf("%-15s toggle %s\n", c->name, c->help);
740 printf("%-15s %s\n", "?", "display help information");
745 settogglehelp(int set)
747 struct togglelist *c;
749 for (c = Togglelist; c->name; c++) {
752 printf("%-15s %s %s\n", c->name, set ? "enable" : "disable",
760 #define GETTOGGLE(name) (struct togglelist *) \
761 genget(name, (char **) Togglelist, sizeof(struct togglelist))
764 toggle(int argc, char *argv[])
768 struct togglelist *c;
772 "Need an argument to 'toggle' command. 'toggle ?' for help.\n");
780 if (Ambiguous((void *)c)) {
781 fprintf(stderr, "'%s': ambiguous argument ('toggle ?' for help).\n",
785 fprintf(stderr, "'%s': unknown argument ('toggle ?' for help).\n",
790 *c->variable = !*c->variable; /* invert it */
791 if (c->actionexplanation) {
792 printf("%s %s.\n", *c->variable? "Will" : "Won't",
793 c->actionexplanation);
797 retval &= (*c->handler)(-1);
805 * The following perform the "set" command.
809 struct termio new_tc = { 0, 0, 0, 0, {}, 0, 0 };
813 const char *name; /* name */
814 const char *help; /* help information */
815 void (*handler)(char *);
816 cc_t *charp; /* where it is located at */
819 static struct setlist Setlist[] = {
820 #ifdef KLUDGELINEMODE
821 { "echo", "character to toggle local echoing on/off", NULL, &echoc },
823 { "escape", "character to escape back to telnet command mode", NULL, &escape },
824 { "rlogin", "rlogin escape character", 0, &rlogin },
825 { "tracefile", "file to write trace information to", SetNetTrace, (cc_t *)NetTraceFile},
826 { " ", "", NULL, NULL },
827 { " ", "The following need 'localchars' to be toggled true", NULL, NULL },
828 { "flushoutput", "character to cause an Abort Output", NULL, termFlushCharp },
829 { "interrupt", "character to cause an Interrupt Process", NULL, termIntCharp },
830 { "quit", "character to cause an Abort process", NULL, termQuitCharp },
831 { "eof", "character to cause an EOF ", NULL, termEofCharp },
832 { " ", "", NULL, NULL },
833 { " ", "The following are for local editing in linemode", NULL, NULL },
834 { "erase", "character to use to erase a character", NULL, termEraseCharp },
835 { "kill", "character to use to erase a line", NULL, termKillCharp },
836 { "lnext", "character to use for literal next", NULL, termLiteralNextCharp },
837 { "susp", "character to cause a Suspend Process", NULL, termSuspCharp },
838 { "reprint", "character to use for line reprint", NULL, termRprntCharp },
839 { "worderase", "character to use to erase a word", NULL, termWerasCharp },
840 { "start", "character to use for XON", NULL, termStartCharp },
841 { "stop", "character to use for XOFF", NULL, termStopCharp },
842 { "forw1", "alternate end of line character", NULL, termForw1Charp },
843 { "forw2", "alternate end of line character", NULL, termForw2Charp },
844 { "ayt", "alternate AYT character", NULL, termAytCharp },
845 { NULL, NULL, NULL, NULL }
848 static struct setlist *
851 return (struct setlist *)
852 genget(name, (char **) Setlist, sizeof(struct setlist));
856 set_escape_char(char *s)
858 if (rlogin != _POSIX_VDISABLE) {
859 rlogin = (s && *s) ? special(s) : _POSIX_VDISABLE;
860 printf("Telnet rlogin escape character is '%s'.\n",
863 escape = (s && *s) ? special(s) : _POSIX_VDISABLE;
864 printf("Telnet escape character is '%s'.\n", control(escape));
869 setcmd(int argc, char *argv[])
873 struct togglelist *c;
875 if (argc < 2 || argc > 3) {
876 printf("Format is 'set Name Value'\n'set ?' for help.\n");
879 if ((argc == 2) && (isprefix(argv[1], "?") || isprefix(argv[1], "help"))) {
880 for (ct = Setlist; ct->name; ct++)
881 printf("%-15s %s\n", ct->name, ct->help);
884 printf("%-15s %s\n", "?", "display help information");
888 ct = getset(argv[1]);
890 c = GETTOGGLE(argv[1]);
892 fprintf(stderr, "'%s': unknown argument ('set ?' for help).\n",
895 } else if (Ambiguous((void *)c)) {
896 fprintf(stderr, "'%s': ambiguous argument ('set ?' for help).\n",
901 if ((argc == 2) || (strcmp("on", argv[2]) == 0))
903 else if (strcmp("off", argv[2]) == 0)
906 printf("Format is 'set togglename [on|off]'\n'set ?' for help.\n");
909 if (c->actionexplanation) {
910 printf("%s %s.\n", *c->variable? "Will" : "Won't",
911 c->actionexplanation);
916 } else if (argc != 3) {
917 printf("Format is 'set Name Value'\n'set ?' for help.\n");
919 } else if (Ambiguous((void *)ct)) {
920 fprintf(stderr, "'%s': ambiguous argument ('set ?' for help).\n",
923 } else if (ct->handler) {
924 (*ct->handler)(argv[2]);
925 printf("%s set to \"%s\".\n", ct->name, (char *)ct->charp);
927 if (strcmp("off", argv[2])) {
928 value = special(argv[2]);
930 value = _POSIX_VDISABLE;
932 *(ct->charp) = (cc_t)value;
933 printf("%s character is '%s'.\n", ct->name, control(*(ct->charp)));
940 unsetcmd(int argc, char *argv[])
943 struct togglelist *c;
948 "Need an argument to 'unset' command. 'unset ?' for help.\n");
951 if (isprefix(argv[1], "?") || isprefix(argv[1], "help")) {
952 for (ct = Setlist; ct->name; ct++)
953 printf("%-15s %s\n", ct->name, ct->help);
956 printf("%-15s %s\n", "?", "display help information");
968 fprintf(stderr, "'%s': unknown argument ('unset ?' for help).\n",
971 } else if (Ambiguous((void *)c)) {
972 fprintf(stderr, "'%s': ambiguous argument ('unset ?' for help).\n",
978 if (c->actionexplanation) {
979 printf("%s %s.\n", *c->variable? "Will" : "Won't",
980 c->actionexplanation);
985 } else if (Ambiguous((void *)ct)) {
986 fprintf(stderr, "'%s': ambiguous argument ('unset ?' for help).\n",
989 } else if (ct->handler) {
991 printf("%s reset to \"%s\".\n", ct->name, (char *)ct->charp);
993 *(ct->charp) = _POSIX_VDISABLE;
994 printf("%s character is '%s'.\n", ct->name, control(*(ct->charp)));
1001 * The following are the data structures and routines for the
1004 #ifdef KLUDGELINEMODE
1005 extern int kludgelinemode;
1011 send_wont(TELOPT_LINEMODE, 1);
1012 send_dont(TELOPT_SGA, 1);
1013 send_dont(TELOPT_ECHO, 1);
1021 #ifdef KLUDGELINEMODE
1023 send_dont(TELOPT_SGA, 1);
1025 send_will(TELOPT_LINEMODE, 1);
1026 send_dont(TELOPT_ECHO, 1);
1033 #ifdef KLUDGELINEMODE
1035 send_do(TELOPT_SGA, 1);
1038 send_wont(TELOPT_LINEMODE, 1);
1039 send_do(TELOPT_ECHO, 1);
1044 dolmmode(int bit, int on)
1047 extern int linemode;
1049 if (my_want_state_is_wont(TELOPT_LINEMODE)) {
1050 printf("?Need to have LINEMODE option enabled first.\n");
1051 printf("'mode ?' for help.\n");
1056 c = (linemode | bit);
1058 c = (linemode & ~bit);
1066 return dolmmode(bit, 1);
1072 return dolmmode(bit, 0);
1076 const char *name; /* command name */
1077 const char *help; /* help string */
1078 int (*handler)(int);/* routine which executes command */
1079 int needconnect; /* Do we need to be connected to execute? */
1083 static struct modelist ModeList[] = {
1084 { "character", "Disable LINEMODE option", (int (*)(int))docharmode, 1, 0 },
1085 #ifdef KLUDGELINEMODE
1086 { "", "(or disable obsolete line-by-line mode)", NULL, 0, 0 },
1088 { "line", "Enable LINEMODE option", (int (*)(int))dolinemode, 1, 0 },
1089 #ifdef KLUDGELINEMODE
1090 { "", "(or enable obsolete line-by-line mode)", NULL, 0, 0 },
1092 { "", "", NULL, 0, 0 },
1093 { "", "These require the LINEMODE option to be enabled", NULL, 0, 0 },
1094 { "isig", "Enable signal trapping", setmod, 1, MODE_TRAPSIG },
1095 { "+isig", 0, setmod, 1, MODE_TRAPSIG },
1096 { "-isig", "Disable signal trapping", clearmode, 1, MODE_TRAPSIG },
1097 { "edit", "Enable character editing", setmod, 1, MODE_EDIT },
1098 { "+edit", 0, setmod, 1, MODE_EDIT },
1099 { "-edit", "Disable character editing", clearmode, 1, MODE_EDIT },
1100 { "softtabs", "Enable tab expansion", setmod, 1, MODE_SOFT_TAB },
1101 { "+softtabs", 0, setmod, 1, MODE_SOFT_TAB },
1102 { "-softtabs", "Disable character editing", clearmode, 1, MODE_SOFT_TAB },
1103 { "litecho", "Enable literal character echo", setmod, 1, MODE_LIT_ECHO },
1104 { "+litecho", 0, setmod, 1, MODE_LIT_ECHO },
1105 { "-litecho", "Disable literal character echo", clearmode, 1, MODE_LIT_ECHO },
1106 { "help", 0, (int (*)(int))modehelp, 0, 0 },
1107 #ifdef KLUDGELINEMODE
1108 { "kludgeline", 0, (int (*)(int))dokludgemode, 1, 0 },
1110 { "", "", NULL, 0, 0 },
1111 { "?", "Print help information", (int (*)(int))modehelp, 0, 0 },
1112 { NULL, NULL, NULL, 0, 0 },
1119 struct modelist *mt;
1121 printf("format is: 'mode Mode', where 'Mode' is one of:\n\n");
1122 for (mt = ModeList; mt->name; mt++) {
1125 printf("%-15s %s\n", mt->name, mt->help);
1133 #define GETMODECMD(name) (struct modelist *) \
1134 genget(name, (char **) ModeList, sizeof(struct modelist))
1137 modecmd(int argc, char *argv[])
1139 struct modelist *mt;
1142 printf("'mode' command requires an argument\n");
1143 printf("'mode ?' for help.\n");
1144 } else if ((mt = GETMODECMD(argv[1])) == 0) {
1145 fprintf(stderr, "Unknown mode '%s' ('mode ?' for help).\n", argv[1]);
1146 } else if (Ambiguous((void *)mt)) {
1147 fprintf(stderr, "Ambiguous mode '%s' ('mode ?' for help).\n", argv[1]);
1148 } else if (mt->needconnect && !connected) {
1149 printf("?Need to be connected first.\n");
1150 printf("'mode ?' for help.\n");
1151 } else if (mt->handler) {
1152 return (*mt->handler)(mt->arg1);
1158 * The following data structures and routines implement the
1159 * "display" command.
1163 display(int argc, char *argv[])
1165 struct togglelist *tl;
1168 #define dotog(tl) if (tl->variable && tl->actionexplanation) { \
1169 if (*tl->variable) { \
1174 printf(" %s.\n", tl->actionexplanation); \
1177 #define doset(sl) if (sl->name && *sl->name != ' ') { \
1178 if (sl->handler == 0) \
1179 printf("%-15s [%s]\n", sl->name, control(*sl->charp)); \
1181 printf("%-15s \"%s\"\n", sl->name, (char *)sl->charp); \
1185 for (tl = Togglelist; tl->name; tl++) {
1189 for (sl = Setlist; sl->name; sl++) {
1195 for (i = 1; i < argc; i++) {
1196 sl = getset(argv[i]);
1197 tl = GETTOGGLE(argv[i]);
1198 if (Ambiguous((void *)sl) || Ambiguous((void *)tl)) {
1199 printf("?Ambiguous argument '%s'.\n", argv[i]);
1201 } else if (!sl && !tl) {
1202 printf("?Unknown argument '%s'.\n", argv[i]);
1214 /*@*/optionstatus();
1221 * The following are the data structures, and many of the routines,
1222 * relating to command processing.
1226 * Set the escape character.
1229 setescape(int argc, char *argv[])
1235 "Deprecated usage - please use 'set escape%s%s' in the future.\n",
1236 (argc > 2)? " ":"", (argc > 2)? argv[1]: "");
1240 printf("new escape character: ");
1241 (void) fgets(buf, sizeof(buf), stdin);
1246 (void) fflush(stdout);
1254 printf("Deprecated usage - please use 'toggle crmod' in the future.\n");
1255 printf("%s map carriage return on output.\n", crmod ? "Will" : "Won't");
1256 (void) fflush(stdout);
1266 long oldrows, oldcols, newrows, newcols, err_;
1268 err_ = (TerminalWindowSize(&oldrows, &oldcols) == 0) ? 1 : 0;
1269 (void) kill(0, SIGTSTP);
1271 * If we didn't get the window size before the SUSPEND, but we
1272 * can get them now (?), then send the NAWS to make sure that
1273 * we are set up for the right window size.
1275 if (TerminalWindowSize(&newrows, &newcols) && connected &&
1276 (err_ || ((oldrows != newrows) || (oldcols != newcols)))) {
1280 /* reget parameters in case they were changed */
1281 TerminalSaveState();
1284 printf("Suspend is not supported. Try the '!' command instead\n");
1290 shell(int argc, char *argv[] __unused)
1292 long oldrows, oldcols, newrows, newcols, err_;
1296 err_ = (TerminalWindowSize(&oldrows, &oldcols) == 0) ? 1 : 0;
1299 perror("Fork failed\n");
1305 * Fire up the shell in the child.
1307 const char *shellp, *shellname;
1309 shellp = getenv("SHELL");
1312 if ((shellname = strrchr(shellp, '/')) == 0)
1317 execl(shellp, shellname, "-c", &saveline[1], (char *)0);
1319 execl(shellp, shellname, (char *)0);
1324 (void)wait((int *)0); /* Wait for the shell to complete */
1326 if (TerminalWindowSize(&newrows, &newcols) && connected &&
1327 (err_ || ((oldrows != newrows) || (oldcols != newcols)))) {
1336 bye(int argc, char *argv[])
1338 extern int resettermname;
1341 (void) shutdown(net, SHUT_RDWR);
1342 printf("Connection closed.\n");
1343 (void) NetClose(net);
1349 if ((argc != 2) || (strcmp(argv[1], "fromquit") != 0)) {
1350 longjmp(toplevel, 1);
1353 return 1; /* Keep lint, etc., happy */
1359 (void) call(bye, "bye", "fromquit", 0);
1366 send_do(TELOPT_LOGOUT, 1);
1379 void (*handler)(int);
1383 static void slc_help(void);
1385 struct slclist SlcList[] = {
1386 { "export", "Use local special character definitions",
1387 (void (*)(int))slc_mode_export, 0 },
1388 { "import", "Use remote special character definitions",
1389 slc_mode_import, 1 },
1390 { "check", "Verify remote special character definitions",
1391 slc_mode_import, 0 },
1392 { "help", NULL, (void (*)(int))slc_help, 0 },
1393 { "?", "Print help information", (void (*)(int))slc_help, 0 },
1394 { NULL, NULL, NULL, 0 },
1402 for (c = SlcList; c->name; c++) {
1405 printf("%-15s %s\n", c->name, c->help);
1412 static struct slclist *
1415 return (struct slclist *)
1416 genget(name, (char **) SlcList, sizeof(struct slclist));
1420 slccmd(int argc, char *argv[])
1426 "Need an argument to 'slc' command. 'slc ?' for help.\n");
1429 c = getslc(argv[1]);
1431 fprintf(stderr, "'%s': unknown argument ('slc ?' for help).\n",
1435 if (Ambiguous((void *)c)) {
1436 fprintf(stderr, "'%s': ambiguous argument ('slc ?' for help).\n",
1440 (*c->handler)(c->arg);
1446 * The ENVIRON command.
1452 void (*handler)(unsigned char *, unsigned char *);
1456 extern struct env_lst *
1457 env_define(const unsigned char *, unsigned char *);
1459 env_undefine(unsigned char *),
1460 env_export(const unsigned char *),
1461 env_unexport(const unsigned char *),
1462 env_send(unsigned char *),
1463 #if defined(OLD_ENVIRON) && defined(ENV_HACK)
1464 env_varval(unsigned char *),
1470 struct envlist EnvList[] = {
1471 { "define", "Define an environment variable",
1472 (void (*)(unsigned char *, unsigned char *))env_define, 2 },
1473 { "undefine", "Undefine an environment variable",
1474 (void (*)(unsigned char *, unsigned char *))env_undefine, 1 },
1475 { "export", "Mark an environment variable for automatic export",
1476 (void (*)(unsigned char *, unsigned char *))env_export, 1 },
1477 { "unexport", "Don't mark an environment variable for automatic export",
1478 (void (*)(unsigned char *, unsigned char *))env_unexport, 1 },
1479 { "send", "Send an environment variable", (void (*)(unsigned char *, unsigned char *))env_send, 1 },
1480 { "list", "List the current environment variables",
1481 (void (*)(unsigned char *, unsigned char *))env_list, 0 },
1482 #if defined(OLD_ENVIRON) && defined(ENV_HACK)
1483 { "varval", "Reverse VAR and VALUE (auto, right, wrong, status)",
1484 (void (*)(unsigned char *, unsigned char *))env_varval, 1 },
1486 { "help", NULL, (void (*)(unsigned char *, unsigned char *))env_help, 0 },
1487 { "?", "Print help information", (void (*)(unsigned char *, unsigned char *))env_help, 0 },
1488 { NULL, NULL, NULL, 0 },
1496 for (c = EnvList; c->name; c++) {
1499 printf("%-15s %s\n", c->name, c->help);
1506 static struct envlist *
1507 getenvcmd(char *name)
1509 return (struct envlist *)
1510 genget(name, (char **) EnvList, sizeof(struct envlist));
1514 env_cmd(int argc, char *argv[])
1520 "Need an argument to 'environ' command. 'environ ?' for help.\n");
1523 c = getenvcmd(argv[1]);
1525 fprintf(stderr, "'%s': unknown argument ('environ ?' for help).\n",
1529 if (Ambiguous((void *)c)) {
1530 fprintf(stderr, "'%s': ambiguous argument ('environ ?' for help).\n",
1534 if (c->narg + 2 != argc) {
1536 "Need %s%d argument%s to 'environ %s' command. 'environ ?' for help.\n",
1537 c->narg < argc + 2 ? "only " : "",
1538 c->narg, c->narg == 1 ? "" : "s", c->name);
1541 (*c->handler)(argv[2], argv[3]);
1546 struct env_lst *next; /* pointer to next structure */
1547 struct env_lst *prev; /* pointer to previous structure */
1548 unsigned char *var; /* pointer to variable name */
1549 unsigned char *value; /* pointer to variable value */
1550 int export; /* 1 -> export with default list of variables */
1551 int welldefined; /* A well defined variable */
1554 struct env_lst envlisthead;
1556 static struct env_lst *
1557 env_find(const unsigned char *var)
1561 for (ep = envlisthead.next; ep; ep = ep->next) {
1562 if (strcmp(ep->var, var) == 0)
1571 extern char **environ;
1575 for (epp = environ; *epp; epp++) {
1576 if ((cp = strchr(*epp, '='))) {
1578 ep = env_define((unsigned char *)*epp,
1579 (unsigned char *)cp+1);
1585 * Special case for DISPLAY variable. If it is ":0.0" or
1586 * "unix:0.0", we have to get rid of "unix" and insert our
1589 if ((ep = env_find("DISPLAY"))
1590 && ((*ep->value == ':')
1591 || (strncmp((char *)ep->value, "unix:", 5) == 0))) {
1593 char *cp2 = strchr((char *)ep->value, ':');
1595 gethostname(hbuf, 256);
1597 cp = (char *)malloc(strlen(hbuf) + strlen(cp2) + 1);
1598 sprintf((char *)cp, "%s%s", hbuf, cp2);
1600 ep->value = (unsigned char *)cp;
1603 * If USER is not defined, but LOGNAME is, then add
1604 * USER with the value from LOGNAME. By default, we
1605 * don't export the USER variable.
1607 if ((env_find("USER") == NULL) && (ep = env_find("LOGNAME"))) {
1608 env_define("USER", ep->value);
1609 env_unexport("USER");
1611 env_export("DISPLAY");
1612 env_export("PRINTER");
1616 env_define(const unsigned char *var, unsigned char *value)
1620 if ((ep = env_find(var))) {
1626 ep = (struct env_lst *)malloc(sizeof(struct env_lst));
1627 ep->next = envlisthead.next;
1628 envlisthead.next = ep;
1629 ep->prev = &envlisthead;
1631 ep->next->prev = ep;
1633 ep->welldefined = opt_welldefined(var);
1635 ep->var = strdup(var);
1636 ep->value = strdup(value);
1641 env_undefine(unsigned char *var)
1645 if ((ep = env_find(var))) {
1646 ep->prev->next = ep->next;
1648 ep->next->prev = ep->prev;
1658 env_export(const unsigned char *var)
1662 if ((ep = env_find(var)))
1667 env_unexport(const unsigned char *var)
1671 if ((ep = env_find(var)))
1676 env_send(unsigned char *var)
1680 if (my_state_is_wont(TELOPT_NEW_ENVIRON)
1682 && my_state_is_wont(TELOPT_OLD_ENVIRON)
1686 "Cannot send '%s': Telnet ENVIRON option not enabled\n",
1692 fprintf(stderr, "Cannot send '%s': variable not defined\n",
1696 env_opt_start_info();
1697 env_opt_add(ep->var);
1706 for (ep = envlisthead.next; ep; ep = ep->next) {
1707 printf("%c %-20s %s\n", ep->export ? '*' : ' ',
1708 ep->var, ep->value);
1713 env_default(int init, int welldefined)
1715 static struct env_lst *nep = NULL;
1722 while ((nep = nep->next)) {
1723 if (nep->export && (nep->welldefined == welldefined))
1731 env_getvalue(const unsigned char *var)
1735 if ((ep = env_find(var)))
1740 #if defined(OLD_ENVIRON) && defined(ENV_HACK)
1742 env_varval(unsigned char *what)
1744 extern int old_env_var, old_env_value, env_auto;
1745 int len = strlen((char *)what);
1750 if (strncasecmp((char *)what, "status", len) == 0) {
1752 printf("%s%s", "VAR and VALUE are/will be ",
1753 "determined automatically\n");
1754 if (old_env_var == OLD_ENV_VAR)
1755 printf("VAR and VALUE set to correct definitions\n");
1757 printf("VAR and VALUE definitions are reversed\n");
1758 } else if (strncasecmp((char *)what, "auto", len) == 0) {
1760 old_env_var = OLD_ENV_VALUE;
1761 old_env_value = OLD_ENV_VAR;
1762 } else if (strncasecmp((char *)what, "right", len) == 0) {
1764 old_env_var = OLD_ENV_VAR;
1765 old_env_value = OLD_ENV_VALUE;
1766 } else if (strncasecmp((char *)what, "wrong", len) == 0) {
1768 old_env_var = OLD_ENV_VALUE;
1769 old_env_value = OLD_ENV_VAR;
1772 printf("Unknown \"varval\" command. (\"auto\", \"right\", \"wrong\", \"status\")\n");
1780 * Print status about the connection.
1784 status(int argc, char *argv[])
1787 printf("Connected to %s.\n", hostname);
1788 if ((argc < 2) || strcmp(argv[1], "notmuch")) {
1789 int mode = getconnmode();
1791 if (my_want_state_is_will(TELOPT_LINEMODE)) {
1792 printf("Operating with LINEMODE option\n");
1793 printf("%s line editing\n", (mode&MODE_EDIT) ? "Local" : "No");
1794 printf("%s catching of signals\n",
1795 (mode&MODE_TRAPSIG) ? "Local" : "No");
1797 #ifdef KLUDGELINEMODE
1798 } else if (kludgelinemode && my_want_state_is_dont(TELOPT_SGA)) {
1799 printf("Operating in obsolete linemode\n");
1802 printf("Operating in single character mode\n");
1804 printf("Catching signals locally\n");
1806 printf("%s character echo\n", (mode&MODE_ECHO) ? "Local" : "Remote");
1807 if (my_want_state_is_will(TELOPT_LFLOW))
1808 printf("%s flow control\n", (mode&MODE_FLOW) ? "Local" : "No");
1811 printf("No connection.\n");
1813 printf("Escape character is '%s'.\n", control(escape));
1814 (void) fflush(stdout);
1820 * Function that gets called when SIGINFO is received.
1825 (void) call(status, "status", "notmuch", 0);
1830 sockaddr_ntop(struct sockaddr *sa)
1833 static char addrbuf[INET6_ADDRSTRLEN];
1835 switch (sa->sa_family) {
1837 addr = &((struct sockaddr_in *)sa)->sin_addr;
1840 addr = &((struct sockaddr_un *)sa)->sun_path;
1844 addr = &((struct sockaddr_in6 *)sa)->sin6_addr;
1850 inet_ntop(sa->sa_family, addr, addrbuf, sizeof(addrbuf));
1854 #if defined(IPSEC) && defined(IPSEC_POLICY_IPSEC)
1856 setpolicy(int lnet, struct addrinfo *res, char *policy)
1865 buf = ipsec_set_policy(policy, strlen(policy));
1867 printf("%s\n", ipsec_strerror());
1870 level = res->ai_family == AF_INET ? IPPROTO_IP : IPPROTO_IPV6;
1871 optname = res->ai_family == AF_INET ? IP_IPSEC_POLICY : IPV6_IPSEC_POLICY;
1872 if (setsockopt(lnet, level, optname, buf, ipsec_get_policylen(buf)) < 0){
1873 perror("setsockopt");
1884 * When an Address Family related error happend, check if retry with
1885 * another AF is possible or not.
1886 * Return 1, if retry with another af is OK. Else, return 0.
1889 switch_af(struct addrinfo **aip)
1892 struct addrinfo *ai;
1895 nextaf = (ai->ai_family == AF_INET) ? AF_INET6 : AF_INET;
1898 while (ai != NULL && ai->ai_family != nextaf);
1908 tn(int argc, char *argv[])
1913 int srcroute = 0, result;
1914 char *cmd, *hostp = 0, *portp = 0, *user = 0;
1915 char *src_addr = NULL;
1916 struct addrinfo hints, *res, *res0 = NULL, *src_res, *src_res0 = NULL;
1917 int error = 0, af_error = 0;
1920 printf("?Already connected to %s\n", hostname);
1925 (void) strcpy(line, "open ");
1927 (void) fgets(&line[strlen(line)], sizeof(line) - strlen(line), stdin);
1935 if (strcmp(*argv, "help") == 0 || isprefix(*argv, "?"))
1937 if (strcmp(*argv, "-l") == 0) {
1945 if (strcmp(*argv, "-a") == 0) {
1950 if (strcmp(*argv, "-s") == 0) {
1969 printf("usage: %s [-l user] [-a] [-s src_addr] host-name [port]\n", cmd);
1976 if (src_addr != NULL) {
1977 memset(&hints, 0, sizeof(hints));
1978 hints.ai_flags = AI_NUMERICHOST;
1979 hints.ai_family = family;
1980 hints.ai_socktype = SOCK_STREAM;
1981 error = getaddrinfo(src_addr, 0, &hints, &src_res);
1982 if (error == EAI_NODATA) {
1984 error = getaddrinfo(src_addr, 0, &hints, &src_res);
1987 fprintf(stderr, "%s: %s\n", src_addr, gai_strerror(error));
1988 if (error == EAI_SYSTEM)
1989 fprintf(stderr, "%s: %s\n", src_addr, strerror(errno));
1995 if (hostp[0] == '/') {
1996 struct sockaddr_un su;
1998 if (strlen(hostp) >= sizeof(su.sun_path)) {
1999 fprintf(stderr, "hostname too long for unix domain socket: %s",
2003 memset(&su, 0, sizeof su);
2004 su.sun_family = AF_UNIX;
2005 strncpy(su.sun_path, hostp, sizeof su.sun_path);
2006 printf("Trying %s...\n", hostp);
2007 net = socket(PF_UNIX, SOCK_STREAM, 0);
2012 if (connect(net, (struct sockaddr *)&su, sizeof su) == -1) {
2013 perror(su.sun_path);
2014 (void) NetClose(net);
2018 } else if (hostp[0] == '@' || hostp[0] == '!') {
2021 family == AF_INET6 ||
2023 (hostname = strrchr(hostp, ':')) == NULL)
2024 hostname = strrchr(hostp, '@');
2025 if (hostname == NULL) {
2035 portp = strdup("telnet");
2036 } else if (*portp == '-') {
2042 memset(&hints, 0, sizeof(hints));
2043 hints.ai_flags = AI_NUMERICHOST;
2044 hints.ai_family = family;
2045 hints.ai_socktype = SOCK_STREAM;
2046 error = getaddrinfo(hostname, portp, &hints, &res);
2048 hints.ai_flags = AI_CANONNAME;
2049 error = getaddrinfo(hostname, portp, &hints, &res);
2052 fprintf(stderr, "%s: %s\n", hostname, gai_strerror(error));
2053 if (error == EAI_SYSTEM)
2054 fprintf(stderr, "%s: %s\n", hostname, strerror(errno));
2058 if (hints.ai_flags == AI_NUMERICHOST) {
2059 /* hostname has numeric */
2063 gni_err = getnameinfo(res->ai_addr, res->ai_addr->sa_len,
2064 _hostname, sizeof(_hostname) - 1, NULL, 0,
2067 (void) strncpy(_hostname, hostp, sizeof(_hostname) - 1);
2068 _hostname[sizeof(_hostname)-1] = '\0';
2069 hostname = _hostname;
2071 /* hostname has FQDN */
2073 (void) strncpy(_hostname, hostname, sizeof(_hostname) - 1);
2074 else if (res->ai_canonname != NULL)
2075 strcpy(_hostname, res->ai_canonname);
2077 (void) strncpy(_hostname, hostp, sizeof(_hostname) - 1);
2078 _hostname[sizeof(_hostname)-1] = '\0';
2079 hostname = _hostname;
2085 if (srcroute != 0) {
2086 static char hostbuf[BUFSIZ];
2088 if (af_error == 0) { /* save intermediate hostnames for retry */
2089 strncpy(hostbuf, hostp, BUFSIZ - 1);
2090 hostbuf[BUFSIZ - 1] = '\0';
2094 result = sourceroute(res, hostp, &srp, &srlen, &proto, &opt);
2097 if (family == AF_UNSPEC && af_error == 0 &&
2098 switch_af(&res) == 1) {
2105 } else if (result == -1) {
2106 printf("Bad source route option: %s\n", hostp);
2112 printf("Trying %s...\n", sockaddr_ntop(res->ai_addr));
2113 net = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
2117 if (family == AF_UNSPEC && af_error == 0 &&
2118 switch_af(&res) == 1) {
2123 perror("telnet: socket");
2126 if (srp && setsockopt(net, proto, opt, (char *)srp, srlen) < 0)
2127 perror("setsockopt (source route)");
2128 #if defined(IPPROTO_IP) && defined(IP_TOS)
2129 if (res->ai_family == PF_INET) {
2130 # if defined(HAS_GETTOS)
2132 if (tos < 0 && (tp = gettosbyname("telnet", "tcp")))
2136 tos = IPTOS_LOWDELAY;
2138 && (setsockopt(net, IPPROTO_IP, IP_TOS,
2139 (char *)&tos, sizeof(int)) < 0)
2140 && (errno != ENOPROTOOPT))
2141 perror("telnet: setsockopt (IP_TOS) (ignored)");
2143 #endif /* defined(IPPROTO_IP) && defined(IP_TOS) */
2145 if (debug && SetSockOpt(net, SOL_SOCKET, SO_DEBUG, 1) < 0) {
2146 perror("setsockopt (SO_DEBUG)");
2149 if (src_addr != NULL) {
2150 for (src_res = src_res0; src_res != 0; src_res = src_res->ai_next)
2151 if (src_res->ai_family == res->ai_family)
2153 if (src_res == NULL)
2155 if (bind(net, src_res->ai_addr, src_res->ai_addrlen) == -1) {
2157 if (family == AF_UNSPEC && af_error == 0 &&
2158 switch_af(&res) == 1) {
2160 (void) NetClose(net);
2165 (void) NetClose(net);
2169 #if defined(IPSEC) && defined(IPSEC_POLICY_IPSEC)
2170 if (setpolicy(net, res, ipsec_policy_in) < 0) {
2171 (void) NetClose(net);
2174 if (setpolicy(net, res, ipsec_policy_out) < 0) {
2175 (void) NetClose(net);
2180 if (connect(net, res->ai_addr, res->ai_addrlen) < 0) {
2181 struct addrinfo *next;
2183 next = res->ai_next;
2184 /* If already an af failed, only try same af. */
2186 while (next != NULL && next->ai_family != res->ai_family)
2187 next = next->ai_next;
2188 warn("connect to address %s", sockaddr_ntop(res->ai_addr));
2191 (void) NetClose(net);
2194 warnx("Unable to connect to remote host");
2195 (void) NetClose(net);
2199 } while (connected == 0);
2201 if (src_res0 != NULL)
2202 freeaddrinfo(src_res0);
2203 cmdrc(hostp, hostname);
2205 if (autologin && user == NULL) {
2208 user = getenv("USER");
2210 ((pw = getpwnam(user)) && pw->pw_uid != getuid())) {
2211 if ((pw = getpwuid(getuid())))
2218 env_define("USER", user);
2221 (void) call(status, "status", "notmuch", 0);
2222 if (setjmp(peerdied) == 0)
2224 (void) NetClose(net);
2225 ExitString("Connection closed by foreign host.\n",1);
2230 if (src_res0 != NULL)
2231 freeaddrinfo(src_res0);
2235 #define HELPINDENT (sizeof ("connect"))
2238 openhelp[] = "connect to a site",
2239 closehelp[] = "close current connection",
2240 logouthelp[] = "forcibly logout remote user and close the connection",
2241 quithelp[] = "exit telnet",
2242 statushelp[] = "print status information",
2243 helphelp[] = "print help information",
2244 sendhelp[] = "transmit special characters ('send ?' for more)",
2245 sethelp[] = "set operating parameters ('set ?' for more)",
2246 unsethelp[] = "unset operating parameters ('unset ?' for more)",
2247 togglestring[] ="toggle operating parameters ('toggle ?' for more)",
2248 slchelp[] = "change state of special characters ('slc ?' for more)",
2249 displayhelp[] = "display operating parameters",
2250 zhelp[] = "suspend telnet",
2252 opiehelp[] = "compute response to OPIE challenge",
2254 shellhelp[] = "invoke a subshell",
2255 envhelp[] = "change environment variables ('environ ?' for more)",
2256 modestring[] = "try to enter line or character mode ('mode ?' for more)";
2258 static Command cmdtab[] = {
2259 { "close", closehelp, bye, 1 },
2260 { "logout", logouthelp, (int (*)(int, char **))logout, 1 },
2261 { "display", displayhelp, display, 0 },
2262 { "mode", modestring, modecmd, 0 },
2263 { "telnet", openhelp, tn, 0 },
2264 { "open", openhelp, tn, 0 },
2265 { "quit", quithelp, (int (*)(int, char **))quit, 0 },
2266 { "send", sendhelp, sendcmd, 0 },
2267 { "set", sethelp, setcmd, 0 },
2268 { "unset", unsethelp, unsetcmd, 0 },
2269 { "status", statushelp, status, 0 },
2270 { "toggle", togglestring, toggle, 0 },
2271 { "slc", slchelp, slccmd, 0 },
2272 { "z", zhelp, (int (*)(int, char **))suspend, 0 },
2273 { "!", shellhelp, shell, 1 },
2274 { "environ", envhelp, env_cmd, 0 },
2275 { "?", helphelp, help, 0 },
2277 { "opie", opiehelp, opie_calc, 0 },
2279 { NULL, NULL, NULL, 0 }
2282 static char crmodhelp[] = "deprecated command -- use 'toggle crmod' instead";
2283 static char escapehelp[] = "deprecated command -- use 'set escape' instead";
2285 static Command cmdtab2[] = {
2286 { "help", 0, help, 0 },
2287 { "escape", escapehelp, setescape, 0 },
2288 { "crmod", crmodhelp, (int (*)(int, char **))togcrmod, 0 },
2289 { NULL, NULL, NULL, 0 }
2294 * Call routine with argc, argv set from args (terminated by 0).
2298 call(intrtn_t routine, ...)
2304 va_start(ap, routine);
2305 while ((args[argno++] = va_arg(ap, char *)) != 0);
2307 return (*routine)(argno-1, args);
2316 if ((cm = (Command *) genget(name, (char **) cmdtab, sizeof(Command))))
2318 return (Command *) genget(name, (char **) cmdtab2, sizeof(Command));
2322 command(int top, const char *tbuf, int cnt)
2330 (void) signal(SIGINT, SIG_DFL);
2331 (void) signal(SIGQUIT, SIG_DFL);
2334 if (rlogin == _POSIX_VDISABLE)
2335 printf("%s> ", prompt);
2339 while (cnt > 0 && (*cp++ = *tbuf++) != '\n')
2342 if (cp == line || *--cp != '\n' || cp == line)
2345 if (rlogin == _POSIX_VDISABLE)
2346 printf("%s\n", line);
2349 if (rlogin != _POSIX_VDISABLE)
2350 printf("%s> ", prompt);
2351 if (fgets(line, sizeof(line), stdin) == NULL) {
2352 if (feof(stdin) || ferror(stdin)) {
2362 if (margv[0] == 0) {
2365 c = getcmd(margv[0]);
2366 if (Ambiguous((void *)c)) {
2367 printf("?Ambiguous command\n");
2371 printf("?Invalid command\n");
2374 if (c->needconnect && !connected) {
2375 printf("?Need to be connected first.\n");
2378 if ((*c->handler)(margc, margv)) {
2384 longjmp(toplevel, 1);
2395 help(int argc, char *argv[])
2400 printf("Commands may be abbreviated. Commands are:\n\n");
2401 for (c = cmdtab; c->name; c++)
2403 printf("%-*s\t%s\n", (int)HELPINDENT, c->name,
2408 else while (--argc > 0) {
2412 if (Ambiguous((void *)c))
2413 printf("?Ambiguous help command %s\n", arg);
2414 else if (c == (Command *)0)
2415 printf("?Invalid help command %s\n", arg);
2417 printf("%s\n", c->help);
2422 static char *rcname = 0;
2423 static char rcbuf[128];
2426 cmdrc(char *m1, char *m2)
2431 int l1 = strlen(m1);
2432 int l2 = strlen(m2);
2433 char m1save[MAXHOSTNAMELEN];
2438 strlcpy(m1save, m1, sizeof(m1save));
2442 rcname = getenv("HOME");
2443 if (rcname && (strlen(rcname) + 10) < sizeof(rcbuf))
2444 strcpy(rcbuf, rcname);
2447 strcat(rcbuf, "/.telnetrc");
2451 if ((rcfile = fopen(rcname, "r")) == 0) {
2456 if (fgets(line, sizeof(line), rcfile) == NULL)
2463 if (!isspace(line[0]))
2466 if (gotmachine == 0) {
2467 if (isspace(line[0]))
2469 if (strncasecmp(line, m1, l1) == 0)
2470 strncpy(line, &line[l1], sizeof(line) - l1);
2471 else if (strncasecmp(line, m2, l2) == 0)
2472 strncpy(line, &line[l2], sizeof(line) - l2);
2473 else if (strncasecmp(line, "DEFAULT", 7) == 0)
2474 strncpy(line, &line[7], sizeof(line) - 7);
2477 if (line[0] != ' ' && line[0] != '\t' && line[0] != '\n')
2484 c = getcmd(margv[0]);
2485 if (Ambiguous((void *)c)) {
2486 printf("?Ambiguous command: %s\n", margv[0]);
2490 printf("?Invalid command: %s\n", margv[0]);
2494 * This should never happen...
2496 if (c->needconnect && !connected) {
2497 printf("?Need to be connected first for %s.\n", margv[0]);
2500 (*c->handler)(margc, margv);
2506 * Source route is handed in as
2507 * [!]@hop1@hop2...[@|:]dst
2508 * If the leading ! is present, it is a
2509 * strict source route, otherwise it is
2510 * assmed to be a loose source route.
2512 * We fill in the source route option as
2513 * hop1,hop2,hop3...dest
2514 * and return a pointer to hop1, which will
2515 * be the address to connect() to.
2519 * res: ponter to addrinfo structure which contains sockaddr to
2520 * the host to connect to.
2522 * arg: pointer to route list to decipher
2524 * cpp: If *cpp is not equal to NULL, this is a
2525 * pointer to a pointer to a character array
2526 * that should be filled in with the option.
2528 * lenp: pointer to an integer that contains the
2529 * length of *cpp if *cpp != NULL.
2531 * protop: pointer to an integer that should be filled in with
2532 * appropriate protocol for setsockopt, as socket
2535 * optp: pointer to an integer that should be filled in with
2536 * appropriate option for setsockopt, as socket protocol
2541 * If the return value is 1, then all operations are
2542 * successful. If the
2543 * return value is -1, there was a syntax error in the
2544 * option, either unknown characters, or too many hosts.
2545 * If the return value is 0, one of the hostnames in the
2546 * path is unknown, and *cpp is set to point to the bad
2549 * *cpp: If *cpp was equal to NULL, it will be filled
2550 * in with a pointer to our static area that has
2551 * the option filled in. This will be 32bit aligned.
2553 * *lenp: This will be filled in with how long the option
2554 * pointed to by *cpp is.
2556 * *protop: This will be filled in with appropriate protocol for
2557 * setsockopt, as socket protocol family.
2559 * *optp: This will be filled in with appropriate option for
2560 * setsockopt, as socket protocol family.
2563 sourceroute(struct addrinfo *ai, char *arg, char **cpp, int *lenp, int *protop, int *optp)
2565 static char buf[1024 + ALIGNBYTES]; /*XXX*/
2566 char *cp, *cp2, *lsrp, *ep;
2567 struct sockaddr_in *_sin;
2569 struct sockaddr_in6 *sin6;
2570 struct cmsghdr *cmsg;
2571 struct ip6_rthdr *rth;
2573 struct addrinfo hints, *res;
2578 * Verify the arguments, and make sure we have
2579 * at least 7 bytes for the option.
2581 if (cpp == NULL || lenp == NULL)
2584 switch (res->ai_family) {
2591 if (*lenp < (int)CMSG_SPACE(sizeof(struct ip6_rthdr) +
2592 sizeof(struct in6_addr)))
2599 * Decide whether we have a buffer passed to us,
2600 * or if we need to use our own static buffer.
2606 *cpp = lsrp = (char *)ALIGN(buf);
2613 if (ai->ai_family == AF_INET6) {
2615 *RFC3542 has obsoleted IPV6_PKTOPTIONS socket option.
2617 #ifdef COMPAT_RFC1883 /* XXX */
2621 *protop = IPPROTO_IPV6;
2622 *optp = IPV6_PKTOPTIONS;
2625 #endif /* COMPAT_RFC1883 */
2630 * Next, decide whether we have a loose source
2631 * route or a strict source route, and fill in
2632 * the begining of the option.
2636 *lsrp++ = IPOPT_SSRR;
2638 *lsrp++ = IPOPT_LSRR;
2643 lsrp++; /* skip over length, we'll fill it in later */
2645 *protop = IPPROTO_IP;
2650 memset(&hints, 0, sizeof(hints));
2651 hints.ai_family = ai->ai_family;
2652 hints.ai_socktype = SOCK_STREAM;
2656 ai->ai_family != AF_INET6 &&
2660 else for (cp2 = cp; (c = *cp2); cp2++) {
2665 } else if (c == '@') {
2669 ai->ai_family != AF_INET6 &&
2680 hints.ai_flags = AI_NUMERICHOST;
2681 error = getaddrinfo(cp, NULL, &hints, &res);
2682 if (error == EAI_NODATA) {
2684 error = getaddrinfo(cp, NULL, &hints, &res);
2687 fprintf(stderr, "%s: %s\n", cp, gai_strerror(error));
2688 if (error == EAI_SYSTEM)
2689 fprintf(stderr, "%s: %s\n", cp,
2695 if (res->ai_family == AF_INET6) {
2700 _sin = (struct sockaddr_in *)res->ai_addr;
2701 memcpy(lsrp, (char *)&_sin->sin_addr, 4);
2709 * Check to make sure there is space for next address
2712 #ifdef COMPAT_RFC1883 /* XXX */
2713 if (res->ai_family == AF_INET6) {
2714 if (((char *)CMSG_DATA(cmsg) +
2715 sizeof(struct ip6_rthdr)) > ep)
2718 #endif /* COMPAT_RFC1883 */
2725 if (res->ai_family == AF_INET6) {
2726 #ifdef COMPAT_RFC1883 /* XXX */
2728 #endif /* COMPAT_RFC1883 */
2732 if ((*(*cpp+IPOPT_OLEN) = lsrp - *cpp) <= 7) {
2737 *lsrp++ = IPOPT_NOP; /* 32 bit word align it */
2738 *lenp = lsrp - *cpp;