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
36 static const char sccsid
[] = "@(#)commands.c 8.4 (Berkeley) 5/30/95";
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD: src/contrib/telnet/telnet/commands.c,v 1.35 2005/02/28 12:46:52 tobez Exp $");
42 #include <sys/param.h>
43 #if (!defined(__BEOS__) && !defined(__HAIKU__))
44 # include <sys/file.h>
46 #include <sys/socket.h>
49 #include <netinet/in.h>
62 #include <arpa/telnet.h>
63 #include <arpa/inet.h>
75 #include <libtelnet/auth.h>
78 #include <libtelnet/encrypt.h>
81 //#include <netinet/in_systm.h>
82 #include <netinet/ip.h>
83 //#include <netinet/ip6.h>
85 #ifndef MAXHOSTNAMELEN
86 #define MAXHOSTNAMELEN 256
89 typedef int (*intrtn_t
)(int, char **);
92 extern int auth_togdebug(int);
95 extern int EncryptAutoEnc(int);
96 extern int EncryptAutoDec(int);
97 extern int EncryptDebug(int);
98 extern int EncryptVerbose(int);
99 #endif /* ENCRYPTION */
100 #if defined(IPPROTO_IP) && defined(IP_TOS)
102 #endif /* defined(IPPROTO_IP) && defined(IP_TOS) */
105 static char _hostname
[MAXHOSTNAMELEN
];
107 static int help(int, char **);
108 static int call(intrtn_t
, ...);
109 static void cmdrc(char *, char *);
111 static int switch_af(struct addrinfo
**);
113 static int togglehelp(void);
114 static int send_tncmd(void (*)(int, int), const char *, char *);
115 static int setmod(int);
116 static int clearmode(int);
117 static int modehelp(void);
118 static int sourceroute(struct addrinfo
*, char *, char **, int *, int *, int *);
121 const char *name
; /* command name */
122 const char *help
; /* help string (NULL for no help) */
123 int (*handler
)(int, char **); /* routine which executes command */
124 int needconnect
; /* Do we need to be connected to execute? */
127 static char line
[256];
128 static char saveline
[256];
130 static char *margv
[20];
133 #include <sys/wait.h>
134 #define PATH_OPIEKEY "/usr/bin/opiekey"
136 opie_calc(int argc
, char *argv
[])
141 printf("%s sequence challenge\n", argv
[0]);
147 execv(PATH_OPIEKEY
, argv
);
153 (void) wait(&status
);
154 if (WIFEXITED(status
))
155 return (WEXITSTATUS(status
));
169 if (*cp
== '!') { /* Special case shell escape */
170 strcpy(saveline
, line
); /* save for shell command */
171 *argp
++ = strdup("!"); /* No room in string to get this */
183 for (cp2
= cp
; c
!= '\0'; c
= *++cp
) {
191 if ((c
= *++cp
) == '\0')
193 } else if (c
== '"') {
196 } else if (c
== '\'') {
199 } else if (isspace(c
))
213 * Make a character string into a number.
215 * Todo: 1. Could take random integers (12, 0x12, 012, 0b1).
228 c
= b
| 0x40; /* DEL */
241 * Construct a control character sequence
242 * for a special character.
249 * The only way I could get the Sun 3.5 compiler
251 * if ((unsigned int)c >= 0x80)
252 * was to assign "c" to an unsigned int variable...
255 unsigned int uic
= (unsigned int)c
;
259 if (c
== (cc_t
)_POSIX_VDISABLE
) {
264 buf
[1] = ((c
>>6)&07) + '0';
265 buf
[2] = ((c
>>3)&07) + '0';
266 buf
[3] = (c
&07) + '0';
268 } else if (uic
>= 0x20) {
280 * The following are data structures and routines for
281 * the "send" command.
286 const char *name
; /* How user refers to it (case independent) */
287 const char *help
; /* Help information (0 ==> no help) */
288 int needconnect
; /* Need to be connected */
289 int narg
; /* Number of arguments */
290 int (*handler
)(char *, ...); /* Routine to perform (for special ops) */
291 int nbyte
; /* Number of bytes to send this command */
292 int what
; /* Character to be sent (<0 ==> special) */
300 send_dontcmd(char *),
301 send_willcmd(char *),
302 send_wontcmd(char *);
304 static struct sendlist Sendlist
[] = {
305 { "ao", "Send Telnet Abort output", 1, 0, NULL
, 2, AO
},
306 { "ayt", "Send Telnet 'Are You There'", 1, 0, NULL
, 2, AYT
},
307 { "brk", "Send Telnet Break", 1, 0, NULL
, 2, BREAK
},
308 { "break", NULL
, 1, 0, NULL
, 2, BREAK
},
309 { "ec", "Send Telnet Erase Character", 1, 0, NULL
, 2, EC
},
310 { "el", "Send Telnet Erase Line", 1, 0, NULL
, 2, EL
},
311 { "escape", "Send current escape character",1, 0, (int (*)(char *, ...))send_esc
, 1, 0 },
312 { "ga", "Send Telnet 'Go Ahead' sequence", 1, 0, NULL
, 2, GA
},
313 { "ip", "Send Telnet Interrupt Process",1, 0, NULL
, 2, IP
},
314 { "intp", NULL
, 1, 0, NULL
, 2, IP
},
315 { "interrupt", NULL
, 1, 0, NULL
, 2, IP
},
316 { "intr", NULL
, 1, 0, NULL
, 2, IP
},
317 { "nop", "Send Telnet 'No operation'", 1, 0, NULL
, 2, NOP
},
318 { "eor", "Send Telnet 'End of Record'", 1, 0, NULL
, 2, EOR
},
319 { "abort", "Send Telnet 'Abort Process'", 1, 0, NULL
, 2, ABORT
},
320 { "susp", "Send Telnet 'Suspend Process'",1, 0, NULL
, 2, SUSP
},
321 { "eof", "Send Telnet End of File Character", 1, 0, NULL
, 2, xEOF
},
322 { "synch", "Perform Telnet 'Synch operation'", 1, 0, (int (*)(char *, ...))dosynch
, 2, 0 },
323 { "getstatus", "Send request for STATUS", 1, 0, (int (*)(char *, ...))get_status
, 6, 0 },
324 { "?", "Display send options", 0, 0, (int (*)(char *, ...))send_help
, 0, 0 },
325 { "help", NULL
, 0, 0, (int (*)(char *, ...))send_help
, 0, 0 },
326 { "do", NULL
, 0, 1, (int (*)(char *, ...))send_docmd
, 3, 0 },
327 { "dont", NULL
, 0, 1, (int (*)(char *, ...))send_dontcmd
, 3, 0 },
328 { "will", NULL
, 0, 1, (int (*)(char *, ...))send_willcmd
, 3, 0 },
329 { "wont", NULL
, 0, 1, (int (*)(char *, ...))send_wontcmd
, 3, 0 },
330 { NULL
, NULL
, 0, 0, NULL
, 0, 0 }
333 #define GETSEND(name) ((struct sendlist *) genget(name, (char **) Sendlist, \
334 sizeof(struct sendlist)))
337 sendcmd(int argc
, char *argv
[])
339 int count
; /* how many bytes we are going to need to send */
341 struct sendlist
*s
; /* pointer to current command */
346 printf("need at least one argument for 'send' command\n");
347 printf("'send ?' for help\n");
351 * First, validate all the send arguments.
352 * In addition, we see how much space we are going to need, and
353 * whether or not we will be doing a "SYNCH" operation (which
354 * flushes the network queue).
357 for (i
= 1; i
< argc
; i
++) {
358 s
= GETSEND(argv
[i
]);
360 printf("Unknown send argument '%s'\n'send ?' for help.\n",
363 } else if (Ambiguous((void *)s
)) {
364 printf("Ambiguous send argument '%s'\n'send ?' for help.\n",
368 if (i
+ s
->narg
>= argc
) {
370 "Need %d argument%s to 'send %s' command. 'send %s ?' for help.\n",
371 s
->narg
, s
->narg
== 1 ? "" : "s", s
->name
, s
->name
);
375 if ((void *)s
->handler
== (void *)send_help
) {
381 needconnect
+= s
->needconnect
;
383 if (!connected
&& needconnect
) {
384 printf("?Need to be connected first.\n");
385 printf("'send ?' for help\n");
388 /* Now, do we have enough room? */
389 if (NETROOM() < count
) {
390 printf("There is not enough room in the buffer TO the network\n");
391 printf("to process your request. Nothing will be done.\n");
392 printf("('send synch' will throw away most data in the network\n");
393 printf("buffer, if this might help.)\n");
396 /* OK, they are all OK, now go through again and actually send */
398 for (i
= 1; i
< argc
; i
++) {
399 if ((s
= GETSEND(argv
[i
])) == 0) {
400 fprintf(stderr
, "Telnet 'send' error - argument disappeared!\n");
406 success
+= (*s
->handler
)((s
->narg
> 0) ? argv
[i
+1] : 0,
407 (s
->narg
> 1) ? argv
[i
+2] : 0);
410 NET2ADD(IAC
, s
->what
);
411 printoption("SENT", IAC
, s
->what
);
414 return (count
== success
);
425 send_docmd(char *name
)
427 return(send_tncmd(send_do
, "do", name
));
434 return(send_tncmd(send_dont
, "dont", name
));
438 send_willcmd(char *name
)
440 return(send_tncmd(send_will
, "will", name
));
444 send_wontcmd(char *name
)
446 return(send_tncmd(send_wont
, "wont", name
));
450 send_tncmd(void (*func
)(int, int), const char *cmd
, char *name
)
453 extern char *telopts
[];
456 if (isprefix(name
, "help") || isprefix(name
, "?")) {
459 printf("usage: send %s <value|option>\n", cmd
);
460 printf("\"value\" must be from 0 to 255\n");
461 printf("Valid options are:\n\t");
464 for (cpp
= telopts
; *cpp
; cpp
++) {
465 len
= strlen(*cpp
) + 3;
466 if (col
+ len
> 65) {
470 printf(" \"%s\"", *cpp
);
476 cpp
= (char **)genget(name
, telopts
, sizeof(char *));
477 if (Ambiguous(cpp
)) {
478 fprintf(stderr
,"'%s': ambiguous argument ('send %s ?' for help).\n",
487 while (*cp
>= '0' && *cp
<= '9') {
493 fprintf(stderr
, "'%s': unknown argument ('send %s ?' for help).\n",
496 } else if (val
< 0 || val
> 255) {
497 fprintf(stderr
, "'%s': bad value ('send %s ?' for help).\n",
503 printf("?Need to be connected first.\n");
513 struct sendlist
*s
; /* pointer to current command */
514 for (s
= Sendlist
; s
->name
; s
++) {
516 printf("%-15s %s\n", s
->name
, s
->help
);
522 * The following are the routines and data structures referred
523 * to by the arguments to the "toggle" command.
538 (SetSockOpt(net
, SOL_SOCKET
, SO_DEBUG
, telnet_debug
)) < 0) {
539 perror("setsockopt (SO_DEBUG)");
543 if (net
> 0 && SetSockOpt(net
, SOL_SOCKET
, SO_DEBUG
, 1) < 0)
544 perror("setsockopt (SO_DEBUG)");
546 printf("Cannot turn off socket debugging\n");
556 printf("Will send carriage returns as telnet <CR><LF>.\n");
558 printf("Will send carriage returns as telnet <CR><NUL>.\n");
568 donebinarytoggle
= 1;
573 if (my_want_state_is_will(TELOPT_BINARY
) &&
574 my_want_state_is_do(TELOPT_BINARY
)) {
576 } else if (my_want_state_is_wont(TELOPT_BINARY
) &&
577 my_want_state_is_dont(TELOPT_BINARY
)) {
580 val
= binmode
? 0 : 1;
584 if (my_want_state_is_will(TELOPT_BINARY
) &&
585 my_want_state_is_do(TELOPT_BINARY
)) {
586 printf("Already operating in binary mode with remote host.\n");
588 printf("Negotiating binary mode with remote host.\n");
592 if (my_want_state_is_wont(TELOPT_BINARY
) &&
593 my_want_state_is_dont(TELOPT_BINARY
)) {
594 printf("Already in network ascii mode with remote host.\n");
596 printf("Negotiating network ascii mode with remote host.\n");
606 donebinarytoggle
= 1;
609 val
= my_want_state_is_do(TELOPT_BINARY
) ? 0 : 1;
612 if (my_want_state_is_do(TELOPT_BINARY
)) {
613 printf("Already receiving in binary mode.\n");
615 printf("Negotiating binary mode on input.\n");
619 if (my_want_state_is_dont(TELOPT_BINARY
)) {
620 printf("Already receiving in network ascii mode.\n");
622 printf("Negotiating network ascii mode on input.\n");
632 donebinarytoggle
= 1;
635 val
= my_want_state_is_will(TELOPT_BINARY
) ? 0 : 1;
638 if (my_want_state_is_will(TELOPT_BINARY
)) {
639 printf("Already transmitting in binary mode.\n");
641 printf("Negotiating binary mode on output.\n");
645 if (my_want_state_is_wont(TELOPT_BINARY
)) {
646 printf("Already transmitting in network ascii mode.\n");
648 printf("Negotiating network ascii mode on output.\n");
656 const char *name
; /* name of toggle */
657 const char *help
; /* help message */
658 int (*handler
)(int); /* routine to do actual setting */
660 const char *actionexplanation
;
663 static struct togglelist Togglelist
[] = {
665 "flushing of output when sending interrupt characters",
668 "flush output when sending interrupt characters" },
670 "automatic sending of interrupt characters in urgent mode",
673 "send interrupt characters in urgent mode" },
674 #ifdef AUTHENTICATION
676 "automatic sending of login and/or authentication info",
679 "send login name and/or authentication information" },
681 "Toggle authentication debugging",
684 "print authentication debugging information" },
688 "automatic encryption of data stream",
691 "automatically encrypt output" },
693 "automatic decryption of data stream",
696 "automatically decrypt input" },
698 "Toggle verbose encryption output",
701 "print verbose encryption output" },
703 "Toggle encryption debugging",
706 "print encryption debugging information" },
707 #endif /* ENCRYPTION */
709 "don't read ~/.telnetrc file",
712 "skip reading of ~/.telnetrc file" },
714 "sending and receiving of binary data",
719 "receiving of binary data",
724 "sending of binary data",
729 "sending carriage returns as telnet <CR><LF>",
730 (int (*)(int))togcrlf
,
734 "mapping of received carriage returns",
737 "map carriage return on output" },
739 "local recognition of certain control characters",
740 (int (*)(int))lclchars
,
742 "recognize certain control characters" },
743 { " ", "", NULL
, NULL
, NULL
}, /* empty line */
746 (int (*)(int))togdebug
,
748 "turn on socket level debugging" },
750 "printing of hexadecimal network data (debugging)",
753 "print hexadecimal representation of network traffic" },
755 "output of \"netdata\" to user readable format (debugging)",
758 "print user readable output for \"netdata\"" },
760 "viewing of options processing (debugging)",
763 "show option processing" },
765 "(debugging) toggle printing of hexadecimal terminal data",
768 "print hexadecimal representation of terminal traffic" },
771 (int (*)(int))togglehelp
,
774 { NULL
, NULL
, NULL
, NULL
, NULL
},
777 (int (*)(int))togglehelp
,
780 { NULL
, NULL
, NULL
, NULL
, NULL
}
786 struct togglelist
*c
;
788 for (c
= Togglelist
; c
->name
; c
++) {
791 printf("%-15s toggle %s\n", c
->name
, c
->help
);
797 printf("%-15s %s\n", "?", "display help information");
802 settogglehelp(int set
)
804 struct togglelist
*c
;
806 for (c
= Togglelist
; c
->name
; c
++) {
809 printf("%-15s %s %s\n", c
->name
, set
? "enable" : "disable",
817 #define GETTOGGLE(name) (struct togglelist *) \
818 genget(name, (char **) Togglelist, sizeof(struct togglelist))
821 toggle(int argc
, char *argv
[])
825 struct togglelist
*c
;
829 "Need an argument to 'toggle' command. 'toggle ?' for help.\n");
837 if (Ambiguous((void *)c
)) {
838 fprintf(stderr
, "'%s': ambiguous argument ('toggle ?' for help).\n",
842 fprintf(stderr
, "'%s': unknown argument ('toggle ?' for help).\n",
847 *c
->variable
= !*c
->variable
; /* invert it */
848 if (c
->actionexplanation
) {
849 printf("%s %s.\n", *c
->variable
? "Will" : "Won't",
850 c
->actionexplanation
);
854 retval
&= (*c
->handler
)(-1);
862 * The following perform the "set" command.
866 struct termio new_tc
;
870 const char *name
; /* name */
871 const char *help
; /* help information */
872 void (*handler
)(char *);
873 cc_t
*charp
; /* where it is located at */
876 static struct setlist Setlist
[] = {
877 #ifdef KLUDGELINEMODE
878 { "echo", "character to toggle local echoing on/off", NULL
, &echoc
},
880 { "escape", "character to escape back to telnet command mode", NULL
, &escape
},
881 { "rlogin", "rlogin escape character", 0, &rlogin
},
882 { "tracefile", "file to write trace information to", SetNetTrace
, (cc_t
*)NetTraceFile
},
883 { " ", "", NULL
, NULL
},
884 { " ", "The following need 'localchars' to be toggled true", NULL
, NULL
},
885 { "flushoutput", "character to cause an Abort Output", NULL
, termFlushCharp
},
886 { "interrupt", "character to cause an Interrupt Process", NULL
, termIntCharp
},
887 { "quit", "character to cause an Abort process", NULL
, termQuitCharp
},
888 { "eof", "character to cause an EOF ", NULL
, termEofCharp
},
889 { " ", "", NULL
, NULL
},
890 { " ", "The following are for local editing in linemode", NULL
, NULL
},
891 { "erase", "character to use to erase a character", NULL
, termEraseCharp
},
892 { "kill", "character to use to erase a line", NULL
, termKillCharp
},
893 { "lnext", "character to use for literal next", NULL
, termLiteralNextCharp
},
894 { "susp", "character to cause a Suspend Process", NULL
, termSuspCharp
},
895 { "reprint", "character to use for line reprint", NULL
, termRprntCharp
},
896 { "worderase", "character to use to erase a word", NULL
, termWerasCharp
},
897 { "start", "character to use for XON", NULL
, termStartCharp
},
898 { "stop", "character to use for XOFF", NULL
, termStopCharp
},
899 { "forw1", "alternate end of line character", NULL
, termForw1Charp
},
900 { "forw2", "alternate end of line character", NULL
, termForw2Charp
},
901 { "ayt", "alternate AYT character", NULL
, termAytCharp
},
902 { NULL
, NULL
, NULL
, NULL
}
905 static struct setlist
*
908 return (struct setlist
*)
909 genget(name
, (char **) Setlist
, sizeof(struct setlist
));
913 set_escape_char(char *s
)
915 if (rlogin
!= _POSIX_VDISABLE
) {
916 rlogin
= (s
&& *s
) ? special(s
) : _POSIX_VDISABLE
;
917 printf("Telnet rlogin escape character is '%s'.\n",
920 escape
= (s
&& *s
) ? special(s
) : _POSIX_VDISABLE
;
921 printf("Telnet escape character is '%s'.\n", control(escape
));
926 setcmd(int argc
, char *argv
[])
930 struct togglelist
*c
;
932 if (argc
< 2 || argc
> 3) {
933 printf("Format is 'set Name Value'\n'set ?' for help.\n");
936 if ((argc
== 2) && (isprefix(argv
[1], "?") || isprefix(argv
[1], "help"))) {
937 for (ct
= Setlist
; ct
->name
; ct
++)
938 printf("%-15s %s\n", ct
->name
, ct
->help
);
941 printf("%-15s %s\n", "?", "display help information");
945 ct
= getset(argv
[1]);
947 c
= GETTOGGLE(argv
[1]);
949 fprintf(stderr
, "'%s': unknown argument ('set ?' for help).\n",
952 } else if (Ambiguous((void *)c
)) {
953 fprintf(stderr
, "'%s': ambiguous argument ('set ?' for help).\n",
958 if ((argc
== 2) || (strcmp("on", argv
[2]) == 0))
960 else if (strcmp("off", argv
[2]) == 0)
963 printf("Format is 'set togglename [on|off]'\n'set ?' for help.\n");
966 if (c
->actionexplanation
) {
967 printf("%s %s.\n", *c
->variable
? "Will" : "Won't",
968 c
->actionexplanation
);
973 } else if (argc
!= 3) {
974 printf("Format is 'set Name Value'\n'set ?' for help.\n");
976 } else if (Ambiguous((void *)ct
)) {
977 fprintf(stderr
, "'%s': ambiguous argument ('set ?' for help).\n",
980 } else if (ct
->handler
) {
981 (*ct
->handler
)(argv
[2]);
982 printf("%s set to \"%s\".\n", ct
->name
, (char *)ct
->charp
);
984 if (strcmp("off", argv
[2])) {
985 value
= special(argv
[2]);
987 value
= _POSIX_VDISABLE
;
989 *(ct
->charp
) = (cc_t
)value
;
990 printf("%s character is '%s'.\n", ct
->name
, control(*(ct
->charp
)));
997 unsetcmd(int argc
, char *argv
[])
1000 struct togglelist
*c
;
1005 "Need an argument to 'unset' command. 'unset ?' for help.\n");
1008 if (isprefix(argv
[1], "?") || isprefix(argv
[1], "help")) {
1009 for (ct
= Setlist
; ct
->name
; ct
++)
1010 printf("%-15s %s\n", ct
->name
, ct
->help
);
1013 printf("%-15s %s\n", "?", "display help information");
1023 c
= GETTOGGLE(name
);
1025 fprintf(stderr
, "'%s': unknown argument ('unset ?' for help).\n",
1028 } else if (Ambiguous((void *)c
)) {
1029 fprintf(stderr
, "'%s': ambiguous argument ('unset ?' for help).\n",
1035 if (c
->actionexplanation
) {
1036 printf("%s %s.\n", *c
->variable
? "Will" : "Won't",
1037 c
->actionexplanation
);
1042 } else if (Ambiguous((void *)ct
)) {
1043 fprintf(stderr
, "'%s': ambiguous argument ('unset ?' for help).\n",
1046 } else if (ct
->handler
) {
1048 printf("%s reset to \"%s\".\n", ct
->name
, (char *)ct
->charp
);
1050 *(ct
->charp
) = _POSIX_VDISABLE
;
1051 printf("%s character is '%s'.\n", ct
->name
, control(*(ct
->charp
)));
1058 * The following are the data structures and routines for the
1061 #ifdef KLUDGELINEMODE
1062 extern int kludgelinemode
;
1068 send_wont(TELOPT_LINEMODE
, 1);
1069 send_dont(TELOPT_SGA
, 1);
1070 send_dont(TELOPT_ECHO
, 1);
1078 #ifdef KLUDGELINEMODE
1080 send_dont(TELOPT_SGA
, 1);
1082 send_will(TELOPT_LINEMODE
, 1);
1083 send_dont(TELOPT_ECHO
, 1);
1090 #ifdef KLUDGELINEMODE
1092 send_do(TELOPT_SGA
, 1);
1095 send_wont(TELOPT_LINEMODE
, 1);
1096 send_do(TELOPT_ECHO
, 1);
1101 dolmmode(int bit
, int on
)
1104 extern int linemode
;
1106 if (my_want_state_is_wont(TELOPT_LINEMODE
)) {
1107 printf("?Need to have LINEMODE option enabled first.\n");
1108 printf("'mode ?' for help.\n");
1113 c
= (linemode
| bit
);
1115 c
= (linemode
& ~bit
);
1123 return dolmmode(bit
, 1);
1129 return dolmmode(bit
, 0);
1133 const char *name
; /* command name */
1134 const char *help
; /* help string */
1135 int (*handler
)(int);/* routine which executes command */
1136 int needconnect
; /* Do we need to be connected to execute? */
1140 static struct modelist ModeList
[] = {
1141 { "character", "Disable LINEMODE option", (int (*)(int))docharmode
, 1, 0 },
1142 #ifdef KLUDGELINEMODE
1143 { "", "(or disable obsolete line-by-line mode)", NULL
, 0, 0 },
1145 { "line", "Enable LINEMODE option", (int (*)(int))dolinemode
, 1, 0 },
1146 #ifdef KLUDGELINEMODE
1147 { "", "(or enable obsolete line-by-line mode)", NULL
, 0, 0 },
1149 { "", "", NULL
, 0, 0 },
1150 { "", "These require the LINEMODE option to be enabled", NULL
, 0, 0 },
1151 { "isig", "Enable signal trapping", setmod
, 1, MODE_TRAPSIG
},
1152 { "+isig", 0, setmod
, 1, MODE_TRAPSIG
},
1153 { "-isig", "Disable signal trapping", clearmode
, 1, MODE_TRAPSIG
},
1154 { "edit", "Enable character editing", setmod
, 1, MODE_EDIT
},
1155 { "+edit", 0, setmod
, 1, MODE_EDIT
},
1156 { "-edit", "Disable character editing", clearmode
, 1, MODE_EDIT
},
1157 { "softtabs", "Enable tab expansion", setmod
, 1, MODE_SOFT_TAB
},
1158 { "+softtabs", 0, setmod
, 1, MODE_SOFT_TAB
},
1159 { "-softtabs", "Disable character editing", clearmode
, 1, MODE_SOFT_TAB
},
1160 { "litecho", "Enable literal character echo", setmod
, 1, MODE_LIT_ECHO
},
1161 { "+litecho", 0, setmod
, 1, MODE_LIT_ECHO
},
1162 { "-litecho", "Disable literal character echo", clearmode
, 1, MODE_LIT_ECHO
},
1163 { "help", 0, (int (*)(int))modehelp
, 0, 0 },
1164 #ifdef KLUDGELINEMODE
1165 { "kludgeline", 0, (int (*)(int))dokludgemode
, 1, 0 },
1167 { "", "", NULL
, 0, 0 },
1168 { "?", "Print help information", (int (*)(int))modehelp
, 0, 0 },
1169 { NULL
, NULL
, NULL
, 0, 0 },
1176 struct modelist
*mt
;
1178 printf("format is: 'mode Mode', where 'Mode' is one of:\n\n");
1179 for (mt
= ModeList
; mt
->name
; mt
++) {
1182 printf("%-15s %s\n", mt
->name
, mt
->help
);
1190 #define GETMODECMD(name) (struct modelist *) \
1191 genget(name, (char **) ModeList, sizeof(struct modelist))
1194 modecmd(int argc
, char *argv
[])
1196 struct modelist
*mt
;
1199 printf("'mode' command requires an argument\n");
1200 printf("'mode ?' for help.\n");
1201 } else if ((mt
= GETMODECMD(argv
[1])) == 0) {
1202 fprintf(stderr
, "Unknown mode '%s' ('mode ?' for help).\n", argv
[1]);
1203 } else if (Ambiguous((void *)mt
)) {
1204 fprintf(stderr
, "Ambiguous mode '%s' ('mode ?' for help).\n", argv
[1]);
1205 } else if (mt
->needconnect
&& !connected
) {
1206 printf("?Need to be connected first.\n");
1207 printf("'mode ?' for help.\n");
1208 } else if (mt
->handler
) {
1209 return (*mt
->handler
)(mt
->arg1
);
1215 * The following data structures and routines implement the
1216 * "display" command.
1220 display(int argc
, char *argv
[])
1222 struct togglelist
*tl
;
1225 #define dotog(tl) if (tl->variable && tl->actionexplanation) { \
1226 if (*tl->variable) { \
1231 printf(" %s.\n", tl->actionexplanation); \
1234 #define doset(sl) if (sl->name && *sl->name != ' ') { \
1235 if (sl->handler == 0) \
1236 printf("%-15s [%s]\n", sl->name, control(*sl->charp)); \
1238 printf("%-15s \"%s\"\n", sl->name, (char *)sl->charp); \
1242 for (tl
= Togglelist
; tl
->name
; tl
++) {
1246 for (sl
= Setlist
; sl
->name
; sl
++) {
1252 for (i
= 1; i
< argc
; i
++) {
1253 sl
= getset(argv
[i
]);
1254 tl
= GETTOGGLE(argv
[i
]);
1255 if (Ambiguous((void *)sl
) || Ambiguous((void *)tl
)) {
1256 printf("?Ambiguous argument '%s'.\n", argv
[i
]);
1258 } else if (!sl
&& !tl
) {
1259 printf("?Unknown argument '%s'.\n", argv
[i
]);
1271 /*@*/optionstatus();
1274 #endif /* ENCRYPTION */
1281 * The following are the data structures, and many of the routines,
1282 * relating to command processing.
1286 * Set the escape character.
1289 setescape(int argc
, char *argv
[])
1295 "Deprecated usage - please use 'set escape%s%s' in the future.\n",
1296 (argc
> 2)? " ":"", (argc
> 2)? argv
[1]: "");
1300 printf("new escape character: ");
1301 (void) fgets(buf
, sizeof(buf
), stdin
);
1306 (void) fflush(stdout
);
1314 printf("Deprecated usage - please use 'toggle crmod' in the future.\n");
1315 printf("%s map carriage return on output.\n", crmod
? "Will" : "Won't");
1316 (void) fflush(stdout
);
1326 long oldrows
, oldcols
, newrows
, newcols
, err_
;
1328 err_
= (TerminalWindowSize(&oldrows
, &oldcols
) == 0) ? 1 : 0;
1329 (void) kill(0, SIGTSTP
);
1331 * If we didn't get the window size before the SUSPEND, but we
1332 * can get them now (?), then send the NAWS to make sure that
1333 * we are set up for the right window size.
1335 if (TerminalWindowSize(&newrows
, &newcols
) && connected
&&
1336 (err_
|| ((oldrows
!= newrows
) || (oldcols
!= newcols
)))) {
1340 /* reget parameters in case they were changed */
1341 TerminalSaveState();
1344 printf("Suspend is not supported. Try the '!' command instead\n");
1350 shell(int argc
, char *argv
[] __unused
)
1352 long oldrows
, oldcols
, newrows
, newcols
, err_
;
1356 err_
= (TerminalWindowSize(&oldrows
, &oldcols
) == 0) ? 1 : 0;
1359 perror("Fork failed\n");
1365 * Fire up the shell in the child.
1367 const char *shellp
, *shellname
;
1369 shellp
= getenv("SHELL");
1372 if ((shellname
= strrchr(shellp
, '/')) == 0)
1377 execl(shellp
, shellname
, "-c", &saveline
[1], (char *)0);
1379 execl(shellp
, shellname
, (char *)0);
1384 (void)wait((int *)0); /* Wait for the shell to complete */
1386 if (TerminalWindowSize(&newrows
, &newcols
) && connected
&&
1387 (err_
|| ((oldrows
!= newrows
) || (oldcols
!= newcols
)))) {
1396 bye(int argc
, char *argv
[])
1398 extern int resettermname
;
1401 (void) shutdown(net
, 2);
1402 printf("Connection closed.\n");
1403 (void) NetClose(net
);
1406 #ifdef AUTHENTICATION
1408 auth_encrypt_connect(connected
);
1414 if ((argc
!= 2) || (strcmp(argv
[1], "fromquit") != 0)) {
1415 longjmp(toplevel
, 1);
1418 return 1; /* Keep lint, etc., happy */
1424 (void) call(bye
, "bye", "fromquit", 0);
1431 send_do(TELOPT_LOGOUT
, 1);
1444 void (*handler
)(int);
1448 static void slc_help(void);
1450 struct slclist SlcList
[] = {
1451 { "export", "Use local special character definitions",
1452 (void (*)(int))slc_mode_export
, 0 },
1453 { "import", "Use remote special character definitions",
1454 slc_mode_import
, 1 },
1455 { "check", "Verify remote special character definitions",
1456 slc_mode_import
, 0 },
1457 { "help", NULL
, (void (*)(int))slc_help
, 0 },
1458 { "?", "Print help information", (void (*)(int))slc_help
, 0 },
1459 { NULL
, NULL
, NULL
, 0 },
1467 for (c
= SlcList
; c
->name
; c
++) {
1470 printf("%-15s %s\n", c
->name
, c
->help
);
1477 static struct slclist
*
1480 return (struct slclist
*)
1481 genget(name
, (char **) SlcList
, sizeof(struct slclist
));
1485 slccmd(int argc
, char *argv
[])
1491 "Need an argument to 'slc' command. 'slc ?' for help.\n");
1494 c
= getslc(argv
[1]);
1496 fprintf(stderr
, "'%s': unknown argument ('slc ?' for help).\n",
1500 if (Ambiguous((void *)c
)) {
1501 fprintf(stderr
, "'%s': ambiguous argument ('slc ?' for help).\n",
1505 (*c
->handler
)(c
->arg
);
1511 * The ENVIRON command.
1517 void (*handler
)(unsigned char *, unsigned char *);
1521 extern struct env_lst
*
1522 env_define(const unsigned char *, unsigned char *);
1524 env_undefine(unsigned char *),
1525 env_export(const unsigned char *),
1526 env_unexport(const unsigned char *),
1527 env_send(unsigned char *),
1528 #if defined(OLD_ENVIRON) && defined(ENV_HACK)
1529 env_varval(unsigned char *),
1535 struct envlist EnvList
[] = {
1536 { "define", "Define an environment variable",
1537 (void (*)(unsigned char *, unsigned char *))env_define
, 2 },
1538 { "undefine", "Undefine an environment variable",
1539 (void (*)(unsigned char *, unsigned char *))env_undefine
, 1 },
1540 { "export", "Mark an environment variable for automatic export",
1541 (void (*)(unsigned char *, unsigned char *))env_export
, 1 },
1542 { "unexport", "Don't mark an environment variable for automatic export",
1543 (void (*)(unsigned char *, unsigned char *))env_unexport
, 1 },
1544 { "send", "Send an environment variable", (void (*)(unsigned char *, unsigned char *))env_send
, 1 },
1545 { "list", "List the current environment variables",
1546 (void (*)(unsigned char *, unsigned char *))env_list
, 0 },
1547 #if defined(OLD_ENVIRON) && defined(ENV_HACK)
1548 { "varval", "Reverse VAR and VALUE (auto, right, wrong, status)",
1549 (void (*)(unsigned char *, unsigned char *))env_varval
, 1 },
1551 { "help", NULL
, (void (*)(unsigned char *, unsigned char *))env_help
, 0 },
1552 { "?", "Print help information", (void (*)(unsigned char *, unsigned char *))env_help
, 0 },
1553 { NULL
, NULL
, NULL
, 0 },
1561 for (c
= EnvList
; c
->name
; c
++) {
1564 printf("%-15s %s\n", c
->name
, c
->help
);
1571 static struct envlist
*
1572 getenvcmd(char *name
)
1574 return (struct envlist
*)
1575 genget(name
, (char **) EnvList
, sizeof(struct envlist
));
1579 env_cmd(int argc
, char *argv
[])
1585 "Need an argument to 'environ' command. 'environ ?' for help.\n");
1588 c
= getenvcmd(argv
[1]);
1590 fprintf(stderr
, "'%s': unknown argument ('environ ?' for help).\n",
1594 if (Ambiguous((void *)c
)) {
1595 fprintf(stderr
, "'%s': ambiguous argument ('environ ?' for help).\n",
1599 if (c
->narg
+ 2 != argc
) {
1601 "Need %s%d argument%s to 'environ %s' command. 'environ ?' for help.\n",
1602 c
->narg
< argc
+ 2 ? "only " : "",
1603 c
->narg
, c
->narg
== 1 ? "" : "s", c
->name
);
1606 (*c
->handler
)(argv
[2], argv
[3]);
1611 struct env_lst
*next
; /* pointer to next structure */
1612 struct env_lst
*prev
; /* pointer to previous structure */
1613 unsigned char *var
; /* pointer to variable name */
1614 unsigned char *value
; /* pointer to variable value */
1615 int export
; /* 1 -> export with default list of variables */
1616 int welldefined
; /* A well defined variable */
1619 struct env_lst envlisthead
;
1621 static struct env_lst
*
1622 env_find(const unsigned char *var
)
1626 for (ep
= envlisthead
.next
; ep
; ep
= ep
->next
) {
1627 if (strcmp(ep
->var
, var
) == 0)
1636 extern char **environ
;
1640 for (epp
= environ
; *epp
; epp
++) {
1641 if ((cp
= strchr(*epp
, '='))) {
1643 ep
= env_define((unsigned char *)*epp
,
1644 (unsigned char *)cp
+1);
1650 * Special case for DISPLAY variable. If it is ":0.0" or
1651 * "unix:0.0", we have to get rid of "unix" and insert our
1654 if ((ep
= env_find("DISPLAY"))
1655 && ((*ep
->value
== ':')
1656 || (strncmp((char *)ep
->value
, "unix:", 5) == 0))) {
1658 char *cp2
= strchr((char *)ep
->value
, ':');
1660 gethostname(hbuf
, 256);
1662 cp
= (char *)malloc(strlen(hbuf
) + strlen(cp2
) + 1);
1663 sprintf((char *)cp
, "%s%s", hbuf
, cp2
);
1665 ep
->value
= (unsigned char *)cp
;
1668 * If USER is not defined, but LOGNAME is, then add
1669 * USER with the value from LOGNAME. By default, we
1670 * don't export the USER variable.
1672 if ((env_find("USER") == NULL
) && (ep
= env_find("LOGNAME"))) {
1673 env_define("USER", ep
->value
);
1674 env_unexport("USER");
1676 env_export("DISPLAY");
1677 env_export("PRINTER");
1681 env_define(const unsigned char *var
, unsigned char *value
)
1685 if ((ep
= env_find(var
))) {
1691 ep
= (struct env_lst
*)malloc(sizeof(struct env_lst
));
1692 ep
->next
= envlisthead
.next
;
1693 envlisthead
.next
= ep
;
1694 ep
->prev
= &envlisthead
;
1696 ep
->next
->prev
= ep
;
1698 ep
->welldefined
= opt_welldefined(var
);
1700 ep
->var
= strdup(var
);
1701 ep
->value
= strdup(value
);
1706 env_undefine(unsigned char *var
)
1710 if ((ep
= env_find(var
))) {
1711 ep
->prev
->next
= ep
->next
;
1713 ep
->next
->prev
= ep
->prev
;
1723 env_export(const unsigned char *var
)
1727 if ((ep
= env_find(var
)))
1732 env_unexport(const unsigned char *var
)
1736 if ((ep
= env_find(var
)))
1741 env_send(unsigned char *var
)
1745 if (my_state_is_wont(TELOPT_NEW_ENVIRON
)
1747 && my_state_is_wont(TELOPT_OLD_ENVIRON
)
1751 "Cannot send '%s': Telnet ENVIRON option not enabled\n",
1757 fprintf(stderr
, "Cannot send '%s': variable not defined\n",
1761 env_opt_start_info();
1762 env_opt_add(ep
->var
);
1771 for (ep
= envlisthead
.next
; ep
; ep
= ep
->next
) {
1772 printf("%c %-20s %s\n", ep
->export
? '*' : ' ',
1773 ep
->var
, ep
->value
);
1778 env_default(int init
, int welldefined
)
1780 static struct env_lst
*nep
= NULL
;
1787 while ((nep
= nep
->next
)) {
1788 if (nep
->export
&& (nep
->welldefined
== welldefined
))
1796 env_getvalue(const unsigned char *var
)
1800 if ((ep
= env_find(var
)))
1805 #if defined(OLD_ENVIRON) && defined(ENV_HACK)
1807 env_varval(unsigned char *what
)
1809 extern int old_env_var
, old_env_value
, env_auto
;
1810 int len
= strlen((char *)what
);
1815 if (strncasecmp((char *)what
, "status", len
) == 0) {
1817 printf("%s%s", "VAR and VALUE are/will be ",
1818 "determined automatically\n");
1819 if (old_env_var
== OLD_ENV_VAR
)
1820 printf("VAR and VALUE set to correct definitions\n");
1822 printf("VAR and VALUE definitions are reversed\n");
1823 } else if (strncasecmp((char *)what
, "auto", len
) == 0) {
1825 old_env_var
= OLD_ENV_VALUE
;
1826 old_env_value
= OLD_ENV_VAR
;
1827 } else if (strncasecmp((char *)what
, "right", len
) == 0) {
1829 old_env_var
= OLD_ENV_VAR
;
1830 old_env_value
= OLD_ENV_VALUE
;
1831 } else if (strncasecmp((char *)what
, "wrong", len
) == 0) {
1833 old_env_var
= OLD_ENV_VALUE
;
1834 old_env_value
= OLD_ENV_VAR
;
1837 printf("Unknown \"varval\" command. (\"auto\", \"right\", \"wrong\", \"status\")\n");
1842 #ifdef AUTHENTICATION
1844 * The AUTHENTICATE command.
1850 int (*handler
)(char *);
1855 auth_enable(char *),
1856 auth_disable(char *),
1861 struct authlist AuthList
[] = {
1862 { "status", "Display current status of authentication information",
1863 (int (*)(char *))auth_status
, 0 },
1864 { "disable", "Disable an authentication type ('auth disable ?' for more)",
1866 { "enable", "Enable an authentication type ('auth enable ?' for more)",
1868 { "help", NULL
, (int (*)(char *))auth_help
, 0 },
1869 { "?", "Print help information", (int (*)(char *))auth_help
, 0 },
1870 { NULL
, NULL
, NULL
, 0 },
1878 for (c
= AuthList
; c
->name
; c
++) {
1881 printf("%-15s %s\n", c
->name
, c
->help
);
1890 auth_cmd(int argc
, char *argv
[])
1896 "Need an argument to 'auth' command. 'auth ?' for help.\n");
1900 c
= (struct authlist
*)
1901 genget(argv
[1], (char **) AuthList
, sizeof(struct authlist
));
1903 fprintf(stderr
, "'%s': unknown argument ('auth ?' for help).\n",
1907 if (Ambiguous((void *)c
)) {
1908 fprintf(stderr
, "'%s': ambiguous argument ('auth ?' for help).\n",
1912 if (c
->narg
+ 2 != argc
) {
1914 "Need %s%d argument%s to 'auth %s' command. 'auth ?' for help.\n",
1915 c
->narg
< argc
+ 2 ? "only " : "",
1916 c
->narg
, c
->narg
== 1 ? "" : "s", c
->name
);
1919 return((*c
->handler
)(argv
[2]));
1925 * The ENCRYPT command.
1928 struct encryptlist
{
1931 int (*handler
)(char *, char *);
1938 EncryptEnable(char *, char *),
1939 EncryptDisable(char *, char *),
1940 EncryptType(char *, char *),
1941 EncryptStart(char *),
1942 EncryptStartInput(void),
1943 EncryptStartOutput(void),
1944 EncryptStop(char *),
1945 EncryptStopInput(void),
1946 EncryptStopOutput(void),
1947 EncryptStatus(void);
1951 struct encryptlist EncryptList
[] = {
1952 { "enable", "Enable encryption. ('encrypt enable ?' for more)",
1953 EncryptEnable
, 1, 1, 2 },
1954 { "disable", "Disable encryption. ('encrypt enable ?' for more)",
1955 EncryptDisable
, 0, 1, 2 },
1956 { "type", "Set encryption type. ('encrypt type ?' for more)",
1957 EncryptType
, 0, 1, 1 },
1958 { "start", "Start encryption. ('encrypt start ?' for more)",
1959 (int (*)(char *, char *))EncryptStart
, 1, 0, 1 },
1960 { "stop", "Stop encryption. ('encrypt stop ?' for more)",
1961 (int (*)(char *, char *))EncryptStop
, 1, 0, 1 },
1962 { "input", "Start encrypting the input stream",
1963 (int (*)(char *, char *))EncryptStartInput
, 1, 0, 0 },
1964 { "-input", "Stop encrypting the input stream",
1965 (int (*)(char *, char *))EncryptStopInput
, 1, 0, 0 },
1966 { "output", "Start encrypting the output stream",
1967 (int (*)(char *, char *))EncryptStartOutput
, 1, 0, 0 },
1968 { "-output", "Stop encrypting the output stream",
1969 (int (*)(char *, char *))EncryptStopOutput
, 1, 0, 0 },
1971 { "status", "Display current status of authentication information",
1972 (int (*)(char *, char *))EncryptStatus
, 0, 0, 0 },
1973 { "help", NULL
, (int (*)(char *, char *))EncryptHelp
, 0, 0, 0 },
1974 { "?", "Print help information", (int (*)(char *, char *))EncryptHelp
, 0, 0, 0 },
1975 { NULL
, NULL
, NULL
, 0, 0, 0 },
1981 struct encryptlist
*c
;
1983 for (c
= EncryptList
; c
->name
; c
++) {
1986 printf("%-15s %s\n", c
->name
, c
->help
);
1995 encrypt_cmd(int argc
, char *argv
[])
1997 struct encryptlist
*c
;
2001 "Need an argument to 'encrypt' command. 'encrypt ?' for help.\n");
2005 c
= (struct encryptlist
*)
2006 genget(argv
[1], (char **) EncryptList
, sizeof(struct encryptlist
));
2008 fprintf(stderr
, "'%s': unknown argument ('encrypt ?' for help).\n",
2012 if (Ambiguous((void *)c
)) {
2013 fprintf(stderr
, "'%s': ambiguous argument ('encrypt ?' for help).\n",
2018 if (argc
< c
->minarg
|| argc
> c
->maxarg
) {
2019 if (c
->minarg
== c
->maxarg
) {
2020 fprintf(stderr
, "Need %s%d argument%s ",
2021 c
->minarg
< argc
? "only " : "", c
->minarg
,
2022 c
->minarg
== 1 ? "" : "s");
2024 fprintf(stderr
, "Need %s%d-%d arguments ",
2025 c
->maxarg
< argc
? "only " : "", c
->minarg
, c
->maxarg
);
2027 fprintf(stderr
, "to 'encrypt %s' command. 'encrypt ?' for help.\n",
2031 if (c
->needconnect
&& !connected
) {
2032 if (!(argc
&& (isprefix(argv
[2], "help") || isprefix(argv
[2], "?")))) {
2033 printf("?Need to be connected first.\n");
2037 return ((*c
->handler
)(argc
> 0 ? argv
[2] : 0,
2038 argc
> 1 ? argv
[3] : 0));
2040 #endif /* ENCRYPTION */
2043 * Print status about the connection.
2047 status(int argc
, char *argv
[])
2050 printf("Connected to %s.\n", hostname
);
2051 if ((argc
< 2) || strcmp(argv
[1], "notmuch")) {
2052 int mode
= getconnmode();
2054 if (my_want_state_is_will(TELOPT_LINEMODE
)) {
2055 printf("Operating with LINEMODE option\n");
2056 printf("%s line editing\n", (mode
&MODE_EDIT
) ? "Local" : "No");
2057 printf("%s catching of signals\n",
2058 (mode
&MODE_TRAPSIG
) ? "Local" : "No");
2060 #ifdef KLUDGELINEMODE
2061 } else if (kludgelinemode
&& my_want_state_is_dont(TELOPT_SGA
)) {
2062 printf("Operating in obsolete linemode\n");
2065 printf("Operating in single character mode\n");
2067 printf("Catching signals locally\n");
2069 printf("%s character echo\n", (mode
&MODE_ECHO
) ? "Local" : "Remote");
2070 if (my_want_state_is_will(TELOPT_LFLOW
))
2071 printf("%s flow control\n", (mode
&MODE_FLOW
) ? "Local" : "No");
2074 #endif /* ENCRYPTION */
2077 printf("No connection.\n");
2079 printf("Escape character is '%s'.\n", control(escape
));
2080 (void) fflush(stdout
);
2086 * Function that gets called when SIGINFO is received.
2091 (void) call(status
, "status", "notmuch", 0);
2096 sockaddr_ntop(struct sockaddr
*sa
)
2099 #ifndef INET6_ADDRSTRLEN
2100 # define INET6_ADDRSTRLEN 256
2102 static char addrbuf
[INET6_ADDRSTRLEN
];
2104 switch (sa
->sa_family
) {
2106 addr
= &((struct sockaddr_in
*)sa
)->sin_addr
;
2109 addr
= &((struct sockaddr_un
*)sa
)->sun_path
;
2113 addr
= &((struct sockaddr_in6
*)sa
)->sin6_addr
;
2119 inet_ntop(sa
->sa_family
, addr
, addrbuf
, sizeof(addrbuf
));
2123 #if defined(IPSEC) && defined(IPSEC_POLICY_IPSEC)
2125 setpolicy(int lnet
, struct addrinfo
*res
, char *policy
)
2134 buf
= ipsec_set_policy(policy
, strlen(policy
));
2136 printf("%s\n", ipsec_strerror());
2139 level
= res
->ai_family
== AF_INET
? IPPROTO_IP
: IPPROTO_IPV6
;
2140 optname
= res
->ai_family
== AF_INET
? IP_IPSEC_POLICY
: IPV6_IPSEC_POLICY
;
2141 if (setsockopt(lnet
, level
, optname
, buf
, ipsec_get_policylen(buf
)) < 0){
2142 perror("setsockopt");
2153 * When an Address Family related error happend, check if retry with
2154 * another AF is possible or not.
2155 * Return 1, if retry with another af is OK. Else, return 0.
2158 switch_af(struct addrinfo
**aip
)
2161 struct addrinfo
*ai
;
2164 nextaf
= (ai
->ai_family
== AF_INET
) ? AF_INET6
: AF_INET
;
2167 while (ai
!= NULL
&& ai
->ai_family
!= nextaf
);
2177 tn(int argc
, char *argv
[])
2182 int srcroute
= 0, result
;
2183 char *cmd
, *hostp
= 0, *portp
= 0, *user
= 0;
2184 char *src_addr
= NULL
;
2185 struct addrinfo hints
, *res
, *res0
= NULL
, *src_res
, *src_res0
= NULL
;
2186 int error
= 0, af_error
= 0;
2189 printf("?Already connected to %s\n", hostname
);
2194 (void) strcpy(line
, "open ");
2196 (void) fgets(&line
[strlen(line
)], sizeof(line
) - strlen(line
), stdin
);
2204 if (strcmp(*argv
, "help") == 0 || isprefix(*argv
, "?"))
2206 if (strcmp(*argv
, "-l") == 0) {
2214 if (strcmp(*argv
, "-a") == 0) {
2219 if (strcmp(*argv
, "-s") == 0) {
2238 printf("usage: %s [-l user] [-a] [-s src_addr] host-name [port]\n", cmd
);
2245 if (src_addr
!= NULL
) {
2246 memset(&hints
, 0, sizeof(hints
));
2247 hints
.ai_family
= family
;
2248 hints
.ai_socktype
= SOCK_STREAM
;
2249 error
= getaddrinfo(src_addr
, 0, &hints
, &src_res
);
2250 if (error
== EAI_NONAME
) {
2252 error
= getaddrinfo(src_addr
, 0, &hints
, &src_res
);
2255 fprintf(stderr
, "%s: %s\n", src_addr
, gai_strerror(error
));
2256 if (error
== EAI_SYSTEM
)
2257 fprintf(stderr
, "%s: %s\n", src_addr
, strerror(errno
));
2263 if (hostp
[0] == '/') {
2264 struct sockaddr_un su
;
2266 if (strlen(hostp
) >= sizeof(su
.sun_path
)) {
2267 fprintf(stderr
, "hostname too long for unix domain socket: %s",
2272 memset(&su
, 0, sizeof su
);
2273 su
.sun_family
= AF_UNIX
;
2274 strlcpy(su
.sun_path
, hostp
, sizeof su
.sun_path
);
2275 printf("Trying %s...\n", hostp
);
2276 net
= socket(AF_UNIX
, SOCK_STREAM
, 0);
2281 if (connect(net
, (struct sockaddr
*)&su
, sizeof su
) == -1) {
2282 perror(su
.sun_path
);
2283 (void) NetClose(net
);
2287 } else if (hostp
[0] == '@' || hostp
[0] == '!') {
2290 family
== AF_INET6
||
2292 (hostname
= strrchr(hostp
, ':')) == NULL
)
2293 hostname
= strrchr(hostp
, '@');
2294 if (hostname
== NULL
) {
2304 portp
= strdup("telnet");
2305 } else if (*portp
== '-') {
2308 } else if (*portp
== '+') {
2314 memset(&hints
, 0, sizeof(hints
));
2315 hints
.ai_flags
= AI_NUMERICHOST
;
2316 hints
.ai_family
= family
;
2317 hints
.ai_socktype
= SOCK_STREAM
;
2318 error
= getaddrinfo(hostname
, portp
, &hints
, &res
);
2320 hints
.ai_flags
= AI_CANONNAME
;
2321 error
= getaddrinfo(hostname
, portp
, &hints
, &res
);
2324 fprintf(stderr
, "%s: %s\n", hostname
, gai_strerror(error
));
2325 if (error
== EAI_SYSTEM
)
2326 fprintf(stderr
, "%s: %s\n", hostname
, strerror(errno
));
2330 if (hints
.ai_flags
== AI_NUMERICHOST
) {
2331 /* hostname has numeric */
2335 gni_err
= getnameinfo(res
->ai_addr
, res
->ai_addr
->sa_len
,
2336 _hostname
, sizeof(_hostname
) - 1, NULL
, 0,
2339 (void) strncpy(_hostname
, hostp
, sizeof(_hostname
) - 1);
2340 _hostname
[sizeof(_hostname
)-1] = '\0';
2341 hostname
= _hostname
;
2343 /* hostname has FQDN */
2345 (void) strncpy(_hostname
, hostname
, sizeof(_hostname
) - 1);
2346 else if (res
->ai_canonname
!= NULL
)
2347 strcpy(_hostname
, res
->ai_canonname
);
2349 (void) strncpy(_hostname
, hostp
, sizeof(_hostname
) - 1);
2350 _hostname
[sizeof(_hostname
)-1] = '\0';
2351 hostname
= _hostname
;
2357 if (srcroute
!= 0) {
2358 static char hostbuf
[BUFSIZ
];
2360 if (af_error
== 0) { /* save intermediate hostnames for retry */
2361 strncpy(hostbuf
, hostp
, BUFSIZ
- 1);
2362 hostbuf
[BUFSIZ
- 1] = '\0';
2366 result
= sourceroute(res
, hostp
, &srp
, &srlen
, &proto
, &opt
);
2369 if (family
== AF_UNSPEC
&& af_error
== 0 &&
2370 switch_af(&res
) == 1) {
2377 } else if (result
== -1) {
2378 printf("Bad source route option: %s\n", hostp
);
2384 printf("Trying %s...\n", sockaddr_ntop(res
->ai_addr
));
2385 net
= socket(res
->ai_family
, res
->ai_socktype
, res
->ai_protocol
);
2389 if (family
== AF_UNSPEC
&& af_error
== 0 &&
2390 switch_af(&res
) == 1) {
2395 perror("telnet: socket");
2398 if (srp
&& setsockopt(net
, proto
, opt
, (char *)srp
, srlen
) < 0)
2399 perror("setsockopt (source route)");
2400 #if defined(IPPROTO_IP) && defined(IP_TOS)
2401 if (res
->ai_family
== PF_INET
) {
2402 # if defined(HAS_GETTOS)
2404 if (tos
< 0 && (tp
= gettosbyname("telnet", "tcp")))
2408 tos
= IPTOS_LOWDELAY
;
2410 && (setsockopt(net
, IPPROTO_IP
, IP_TOS
,
2411 (char *)&tos
, sizeof(int)) < 0)
2412 && (errno
!= ENOPROTOOPT
))
2413 perror("telnet: setsockopt (IP_TOS) (ignored)");
2415 #endif /* defined(IPPROTO_IP) && defined(IP_TOS) */
2417 if (telnet_debug
&& SetSockOpt(net
, SOL_SOCKET
, SO_DEBUG
, 1) < 0) {
2418 perror("setsockopt (SO_DEBUG)");
2421 if (src_addr
!= NULL
) {
2422 for (src_res
= src_res0
; src_res
!= 0; src_res
= src_res
->ai_next
)
2423 if (src_res
->ai_family
== res
->ai_family
)
2425 if (src_res
== NULL
)
2427 if (bind(net
, src_res
->ai_addr
, src_res
->ai_addrlen
) == -1) {
2429 if (family
== AF_UNSPEC
&& af_error
== 0 &&
2430 switch_af(&res
) == 1) {
2432 (void) NetClose(net
);
2437 (void) NetClose(net
);
2441 #if defined(IPSEC) && defined(IPSEC_POLICY_IPSEC)
2442 if (setpolicy(net
, res
, ipsec_policy_in
) < 0) {
2443 (void) NetClose(net
);
2446 if (setpolicy(net
, res
, ipsec_policy_out
) < 0) {
2447 (void) NetClose(net
);
2452 if (connect(net
, res
->ai_addr
, res
->ai_addrlen
) < 0) {
2453 struct addrinfo
*next
;
2455 next
= res
->ai_next
;
2456 /* If already an af failed, only try same af. */
2458 while (next
!= NULL
&& next
->ai_family
!= res
->ai_family
)
2459 next
= next
->ai_next
;
2460 warn("connect to address %s", sockaddr_ntop(res
->ai_addr
));
2463 (void) NetClose(net
);
2466 warnx("Unable to connect to remote host");
2467 (void) NetClose(net
);
2471 #ifdef AUTHENTICATION
2473 auth_encrypt_connect(connected
);
2476 } while (connected
== 0);
2478 if (src_res0
!= NULL
)
2479 freeaddrinfo(src_res0
);
2480 cmdrc(hostp
, hostname
);
2483 if (autologin
&& user
== NULL
) {
2486 user
= getenv("USER");
2488 ((pw
= getpwnam(user
)) && pw
->pw_uid
!= getuid())) {
2489 if ((pw
= getpwuid(getuid())))
2496 env_define("USER", user
);
2499 (void) call(status
, "status", "notmuch", 0);
2500 if (setjmp(peerdied
) == 0)
2502 (void) NetClose(net
);
2503 ExitString("Connection closed by foreign host.\n",1);
2508 if (src_res0
!= NULL
)
2509 freeaddrinfo(src_res0
);
2513 #define HELPINDENT (sizeof ("connect"))
2516 openhelp
[] = "connect to a site",
2517 closehelp
[] = "close current connection",
2518 logouthelp
[] = "forcibly logout remote user and close the connection",
2519 quithelp
[] = "exit telnet",
2520 statushelp
[] = "print status information",
2521 helphelp
[] = "print help information",
2522 sendhelp
[] = "transmit special characters ('send ?' for more)",
2523 sethelp
[] = "set operating parameters ('set ?' for more)",
2524 unsethelp
[] = "unset operating parameters ('unset ?' for more)",
2525 togglestring
[] ="toggle operating parameters ('toggle ?' for more)",
2526 slchelp
[] = "change state of special charaters ('slc ?' for more)",
2527 displayhelp
[] = "display operating parameters",
2528 #ifdef AUTHENTICATION
2529 authhelp
[] = "turn on (off) authentication ('auth ?' for more)",
2532 encrypthelp
[] = "turn on (off) encryption ('encrypt ?' for more)",
2533 #endif /* ENCRYPTION */
2534 zhelp
[] = "suspend telnet",
2536 opiehelp
[] = "compute response to OPIE challenge",
2538 shellhelp
[] = "invoke a subshell",
2539 envhelp
[] = "change environment variables ('environ ?' for more)",
2540 modestring
[] = "try to enter line or character mode ('mode ?' for more)";
2542 static Command cmdtab
[] = {
2543 { "close", closehelp
, bye
, 1 },
2544 { "logout", logouthelp
, (int (*)(int, char **))logout
, 1 },
2545 { "display", displayhelp
, display
, 0 },
2546 { "mode", modestring
, modecmd
, 0 },
2547 { "telnet", openhelp
, tn
, 0 },
2548 { "open", openhelp
, tn
, 0 },
2549 { "quit", quithelp
, (int (*)(int, char **))quit
, 0 },
2550 { "send", sendhelp
, sendcmd
, 0 },
2551 { "set", sethelp
, setcmd
, 0 },
2552 { "unset", unsethelp
, unsetcmd
, 0 },
2553 { "status", statushelp
, status
, 0 },
2554 { "toggle", togglestring
, toggle
, 0 },
2555 { "slc", slchelp
, slccmd
, 0 },
2556 #ifdef AUTHENTICATION
2557 { "auth", authhelp
, auth_cmd
, 0 },
2560 { "encrypt", encrypthelp
, encrypt_cmd
, 0 },
2561 #endif /* ENCRYPTION */
2562 { "z", zhelp
, (int (*)(int, char **))suspend
, 0 },
2563 { "!", shellhelp
, shell
, 1 },
2564 { "environ", envhelp
, env_cmd
, 0 },
2565 { "?", helphelp
, help
, 0 },
2567 { "opie", opiehelp
, opie_calc
, 0 },
2569 { NULL
, NULL
, NULL
, 0 }
2572 static char crmodhelp
[] = "deprecated command -- use 'toggle crmod' instead";
2573 static char escapehelp
[] = "deprecated command -- use 'set escape' instead";
2575 static Command cmdtab2
[] = {
2576 { "help", 0, help
, 0 },
2577 { "escape", escapehelp
, setescape
, 0 },
2578 { "crmod", crmodhelp
, (int (*)(int, char **))togcrmod
, 0 },
2579 { NULL
, NULL
, NULL
, 0 }
2584 * Call routine with argc, argv set from args (terminated by 0).
2588 call(intrtn_t routine
, ...)
2594 va_start(ap
, routine
);
2595 while ((args
[argno
++] = va_arg(ap
, char *)) != 0);
2597 return (*routine
)(argno
-1, args
);
2606 if ((cm
= (Command
*) genget(name
, (char **) cmdtab
, sizeof(Command
))))
2608 return (Command
*) genget(name
, (char **) cmdtab2
, sizeof(Command
));
2612 command(int top
, const char *tbuf
, int cnt
)
2620 (void) signal(SIGINT
, SIG_DFL
);
2621 (void) signal(SIGQUIT
, SIG_DFL
);
2624 if (rlogin
== _POSIX_VDISABLE
)
2625 printf("%s> ", prompt
);
2629 while (cnt
> 0 && (*cp
++ = *tbuf
++) != '\n')
2632 if (cp
== line
|| *--cp
!= '\n' || cp
== line
)
2635 if (rlogin
== _POSIX_VDISABLE
)
2636 printf("%s\n", line
);
2639 if (rlogin
!= _POSIX_VDISABLE
)
2640 printf("%s> ", prompt
);
2641 if (fgets(line
, sizeof(line
), stdin
) == NULL
) {
2642 if (feof(stdin
) || ferror(stdin
)) {
2652 if (margv
[0] == 0) {
2655 c
= getcmd(margv
[0]);
2656 if (Ambiguous((void *)c
)) {
2657 printf("?Ambiguous command\n");
2661 printf("?Invalid command\n");
2664 if (c
->needconnect
&& !connected
) {
2665 printf("?Need to be connected first.\n");
2668 if ((*c
->handler
)(margc
, margv
)) {
2674 longjmp(toplevel
, 1);
2685 help(int argc
, char *argv
[])
2690 printf("Commands may be abbreviated. Commands are:\n\n");
2691 for (c
= cmdtab
; c
->name
; c
++)
2693 printf("%-*s\t%s\n", (int)HELPINDENT
, c
->name
,
2698 else while (--argc
> 0) {
2702 if (Ambiguous((void *)c
))
2703 printf("?Ambiguous help command %s\n", arg
);
2704 else if (c
== (Command
*)0)
2705 printf("?Invalid help command %s\n", arg
);
2707 printf("%s\n", c
->help
);
2712 static char *rcname
= 0;
2713 static char rcbuf
[128];
2716 cmdrc(char *m1
, char *m2
)
2721 int l1
= strlen(m1
);
2722 int l2
= strlen(m2
);
2723 char m1save
[MAXHOSTNAMELEN
];
2728 strlcpy(m1save
, m1
, sizeof(m1save
));
2732 rcname
= getenv("HOME");
2733 if (rcname
&& (strlen(rcname
) + 10) < sizeof(rcbuf
))
2734 strcpy(rcbuf
, rcname
);
2737 strcat(rcbuf
, "/.telnetrc");
2741 if ((rcfile
= fopen(rcname
, "r")) == 0) {
2746 if (fgets(line
, sizeof(line
), rcfile
) == NULL
)
2753 if (!isspace(line
[0]))
2756 if (gotmachine
== 0) {
2757 if (isspace(line
[0]))
2759 if (strncasecmp(line
, m1
, l1
) == 0)
2760 strncpy(line
, &line
[l1
], sizeof(line
) - l1
);
2761 else if (strncasecmp(line
, m2
, l2
) == 0)
2762 strncpy(line
, &line
[l2
], sizeof(line
) - l2
);
2763 else if (strncasecmp(line
, "DEFAULT", 7) == 0)
2764 strncpy(line
, &line
[7], sizeof(line
) - 7);
2767 if (line
[0] != ' ' && line
[0] != '\t' && line
[0] != '\n')
2774 c
= getcmd(margv
[0]);
2775 if (Ambiguous((void *)c
)) {
2776 printf("?Ambiguous command: %s\n", margv
[0]);
2780 printf("?Invalid command: %s\n", margv
[0]);
2784 * This should never happen...
2786 if (c
->needconnect
&& !connected
) {
2787 printf("?Need to be connected first for %s.\n", margv
[0]);
2790 (*c
->handler
)(margc
, margv
);
2796 * Source route is handed in as
2797 * [!]@hop1@hop2...[@|:]dst
2798 * If the leading ! is present, it is a
2799 * strict source route, otherwise it is
2800 * assmed to be a loose source route.
2802 * We fill in the source route option as
2803 * hop1,hop2,hop3...dest
2804 * and return a pointer to hop1, which will
2805 * be the address to connect() to.
2809 * res: ponter to addrinfo structure which contains sockaddr to
2810 * the host to connect to.
2812 * arg: pointer to route list to decipher
2814 * cpp: If *cpp is not equal to NULL, this is a
2815 * pointer to a pointer to a character array
2816 * that should be filled in with the option.
2818 * lenp: pointer to an integer that contains the
2819 * length of *cpp if *cpp != NULL.
2821 * protop: pointer to an integer that should be filled in with
2822 * appropriate protocol for setsockopt, as socket
2825 * optp: pointer to an integer that should be filled in with
2826 * appropriate option for setsockopt, as socket protocol
2831 * If the return value is 1, then all operations are
2832 * successful. If the
2833 * return value is -1, there was a syntax error in the
2834 * option, either unknown characters, or too many hosts.
2835 * If the return value is 0, one of the hostnames in the
2836 * path is unknown, and *cpp is set to point to the bad
2839 * *cpp: If *cpp was equal to NULL, it will be filled
2840 * in with a pointer to our static area that has
2841 * the option filled in. This will be 32bit aligned.
2843 * *lenp: This will be filled in with how long the option
2844 * pointed to by *cpp is.
2846 * *protop: This will be filled in with appropriate protocol for
2847 * setsockopt, as socket protocol family.
2849 * *optp: This will be filled in with appropriate option for
2850 * setsockopt, as socket protocol family.
2853 sourceroute(struct addrinfo
*ai
, char *arg
, char **cpp
, int *lenp
, int *protop
, int *optp
)
2855 static char buf
[1024 + ALIGNBYTES
]; /*XXX*/
2856 char *cp
, *cp2
, *lsrp
, *ep
;
2857 struct sockaddr_in
*_sin
;
2859 struct sockaddr_in6
*sin6
;
2860 struct ip6_rthdr
*rth
;
2862 struct addrinfo hints
, *res
;
2867 * Verify the arguments, and make sure we have
2868 * at least 7 bytes for the option.
2870 if (cpp
== NULL
|| lenp
== NULL
)
2873 switch (res
->ai_family
) {
2880 if (*lenp
< (int)CMSG_SPACE(sizeof(struct ip6_rthdr
) +
2881 sizeof(struct in6_addr
)))
2888 * Decide whether we have a buffer passed to us,
2889 * or if we need to use our own static buffer.
2895 *cpp
= lsrp
= (char *)ALIGN(buf
);
2902 if (ai
->ai_family
== AF_INET6
) {
2903 if ((rth
= inet6_rth_init((void *)*cpp
, sizeof(buf
),
2904 IPV6_RTHDR_TYPE_0
, 0)) == NULL
)
2908 *protop
= IPPROTO_IPV6
;
2914 * Next, decide whether we have a loose source
2915 * route or a strict source route, and fill in
2916 * the begining of the option.
2920 *lsrp
++ = IPOPT_SSRR
;
2922 *lsrp
++ = IPOPT_LSRR
;
2927 lsrp
++; /* skip over length, we'll fill it in later */
2929 *protop
= IPPROTO_IP
;
2934 memset(&hints
, 0, sizeof(hints
));
2935 hints
.ai_family
= ai
->ai_family
;
2936 hints
.ai_socktype
= SOCK_STREAM
;
2940 ai
->ai_family
!= AF_INET6
&&
2944 else for (cp2
= cp
; (c
= *cp2
); cp2
++) {
2949 } else if (c
== '@') {
2953 ai
->ai_family
!= AF_INET6
&&
2964 hints
.ai_flags
= AI_NUMERICHOST
;
2965 error
= getaddrinfo(cp
, NULL
, &hints
, &res
);
2966 if (error
== EAI_NONAME
) {
2968 error
= getaddrinfo(cp
, NULL
, &hints
, &res
);
2971 fprintf(stderr
, "%s: %s\n", cp
, gai_strerror(error
));
2972 if (error
== EAI_SYSTEM
)
2973 fprintf(stderr
, "%s: %s\n", cp
,
2979 if (res
->ai_family
== AF_INET6
) {
2980 sin6
= (struct sockaddr_in6
*)res
->ai_addr
;
2981 if (inet6_rth_add((void *)rth
, &sin6
->sin6_addr
) == -1)
2986 _sin
= (struct sockaddr_in
*)res
->ai_addr
;
2987 memcpy(lsrp
, (char *)&_sin
->sin_addr
, 4);
2995 * Check to make sure there is space for next address
3002 if (res
->ai_family
== AF_INET6
) {
3003 rth
->ip6r_len
= rth
->ip6r_segleft
* 2;
3004 *lenp
= (rth
->ip6r_len
+ 1) << 3;
3008 if ((*(*cpp
+IPOPT_OLEN
) = lsrp
- *cpp
) <= 7) {
3013 *lsrp
++ = IPOPT_NOP
; /* 32 bit word align it */
3014 *lenp
= lsrp
- *cpp
;