2 * Copyright (c) 1988, 1990, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#)commands.c 8.4 (Berkeley) 5/30/95
30 * $FreeBSD: src/crypto/telnet/telnet/commands.c,v 1.12.2.7 2003/04/23 07:16:32 ru Exp $
33 #include <sys/param.h>
36 #include <sys/socket.h>
37 #include <netinet/in.h>
50 #include <arpa/telnet.h>
51 #include <arpa/inet.h>
63 #include <libtelnet/auth.h>
66 #include <libtelnet/encrypt.h>
69 #include <netinet/in_systm.h>
70 #include <netinet/ip.h>
71 #include <netinet/ip6.h>
73 #ifndef MAXHOSTNAMELEN
74 #define MAXHOSTNAMELEN 256
77 typedef int (*intrtn_t
)(int, char **);
80 extern int auth_togdebug(int);
83 extern int EncryptAutoEnc(int);
84 extern int EncryptAutoDec(int);
85 extern int EncryptDebug(int);
86 extern int EncryptVerbose(int);
87 #endif /* ENCRYPTION */
88 #if defined(IPPROTO_IP) && defined(IP_TOS)
90 #endif /* defined(IPPROTO_IP) && defined(IP_TOS) */
93 static char _hostname
[MAXHOSTNAMELEN
];
95 static int help(int, char **);
96 static int call(intrtn_t
, ...);
97 static void cmdrc(const char *, const char *);
99 static int switch_af(struct addrinfo
**);
101 static int togglehelp(void);
102 static int send_tncmd(void (*)(int, int), const char *, char *);
103 static int setmod(int);
104 static int clearmode(int);
105 static int modehelp(void);
106 static int sourceroute(struct addrinfo
*, char *, char **, int *, int *, int *);
109 const char *name
; /* command name */
110 const char *help
; /* help string (NULL for no help) */
111 int (*handler
)(int, char **); /* routine which executes command */
112 int needconnect
; /* Do we need to be connected to execute? */
115 static char line
[256];
116 static char saveline
[256];
118 static char *margv
[20];
121 #include <sys/wait.h>
122 #define PATH_OPIEKEY "/usr/bin/opiekey"
124 opie_calc(int argc
, char *argv
[])
129 printf("%s sequence challenge\n", argv
[0]);
135 execv(PATH_OPIEKEY
, argv
);
141 (void) wait(&status
);
142 if (WIFEXITED(status
))
143 return (WEXITSTATUS(status
));
157 if (*cp
== '!') { /* Special case shell escape */
158 strcpy(saveline
, line
); /* save for shell command */
159 *argp
++ = strdup("!"); /* No room in string to get this */
171 for (cp2
= cp
; c
!= '\0'; c
= *++cp
) {
179 if ((c
= *++cp
) == '\0')
181 } else if (c
== '"') {
184 } else if (c
== '\'') {
187 } else if (isspace(c
))
201 * Make a character string into a number.
203 * Todo: 1. Could take random integers (12, 0x12, 012, 0b1).
216 c
= b
| 0x40; /* DEL */
229 * Construct a control character sequence
230 * for a special character.
237 * The only way I could get the Sun 3.5 compiler
239 * if ((unsigned int)c >= 0x80)
240 * was to assign "c" to an unsigned int variable...
243 unsigned int uic
= (unsigned int)c
;
247 if (c
== (cc_t
)_POSIX_VDISABLE
) {
252 buf
[1] = ((c
>>6)&07) + '0';
253 buf
[2] = ((c
>>3)&07) + '0';
254 buf
[3] = (c
&07) + '0';
256 } else if (uic
>= 0x20) {
268 * The following are data structures and routines for
269 * the "send" command.
274 const char *name
; /* How user refers to it (case independent) */
275 const char *help
; /* Help information (0 ==> no help) */
276 int needconnect
; /* Need to be connected */
277 int narg
; /* Number of arguments */
278 int (*handler
)(char *, ...); /* Routine to perform (for special ops) */
279 int nbyte
; /* Number of bytes to send this command */
280 int what
; /* Character to be sent (<0 ==> special) */
288 send_dontcmd(char *),
289 send_willcmd(char *),
290 send_wontcmd(char *);
292 static struct sendlist Sendlist
[] = {
293 { "ao", "Send Telnet Abort output", 1, 0, NULL
, 2, AO
},
294 { "ayt", "Send Telnet 'Are You There'", 1, 0, NULL
, 2, AYT
},
295 { "brk", "Send Telnet Break", 1, 0, NULL
, 2, BREAK
},
296 { "break", NULL
, 1, 0, NULL
, 2, BREAK
},
297 { "ec", "Send Telnet Erase Character", 1, 0, NULL
, 2, EC
},
298 { "el", "Send Telnet Erase Line", 1, 0, NULL
, 2, EL
},
299 { "escape", "Send current escape character",1, 0, (int (*)(char *, ...))send_esc
, 1, 0 },
300 { "ga", "Send Telnet 'Go Ahead' sequence", 1, 0, NULL
, 2, GA
},
301 { "ip", "Send Telnet Interrupt Process",1, 0, NULL
, 2, IP
},
302 { "intp", NULL
, 1, 0, NULL
, 2, IP
},
303 { "interrupt", NULL
, 1, 0, NULL
, 2, IP
},
304 { "intr", NULL
, 1, 0, NULL
, 2, IP
},
305 { "nop", "Send Telnet 'No operation'", 1, 0, NULL
, 2, NOP
},
306 { "eor", "Send Telnet 'End of Record'", 1, 0, NULL
, 2, EOR
},
307 { "abort", "Send Telnet 'Abort Process'", 1, 0, NULL
, 2, ABORT
},
308 { "susp", "Send Telnet 'Suspend Process'",1, 0, NULL
, 2, SUSP
},
309 { "eof", "Send Telnet End of File Character", 1, 0, NULL
, 2, xEOF
},
310 { "synch", "Perform Telnet 'Synch operation'", 1, 0, (int (*)(char *, ...))dosynch
, 2, 0 },
311 { "getstatus", "Send request for STATUS", 1, 0, (int (*)(char *, ...))get_status
, 6, 0 },
312 { "?", "Display send options", 0, 0, (int (*)(char *, ...))send_help
, 0, 0 },
313 { "help", NULL
, 0, 0, (int (*)(char *, ...))send_help
, 0, 0 },
314 { "do", NULL
, 0, 1, (int (*)(char *, ...))send_docmd
, 3, 0 },
315 { "dont", NULL
, 0, 1, (int (*)(char *, ...))send_dontcmd
, 3, 0 },
316 { "will", NULL
, 0, 1, (int (*)(char *, ...))send_willcmd
, 3, 0 },
317 { "wont", NULL
, 0, 1, (int (*)(char *, ...))send_wontcmd
, 3, 0 },
318 { NULL
, NULL
, 0, 0, NULL
, 0, 0 }
321 #define GETSEND(name) ((struct sendlist *) genget(name, (char **) Sendlist, \
322 sizeof(struct sendlist)))
325 sendcmd(int argc
, char *argv
[])
327 int count
; /* how many bytes we are going to need to send */
329 struct sendlist
*s
; /* pointer to current command */
334 printf("need at least one argument for 'send' command\n");
335 printf("'send ?' for help\n");
339 * First, validate all the send arguments.
340 * In addition, we see how much space we are going to need, and
341 * whether or not we will be doing a "SYNCH" operation (which
342 * flushes the network queue).
345 for (i
= 1; i
< argc
; i
++) {
346 s
= GETSEND(argv
[i
]);
348 printf("Unknown send argument '%s'\n'send ?' for help.\n",
351 } else if (Ambiguous((void *)s
)) {
352 printf("Ambiguous send argument '%s'\n'send ?' for help.\n",
356 if (i
+ s
->narg
>= argc
) {
358 "Need %d argument%s to 'send %s' command. 'send %s ?' for help.\n",
359 s
->narg
, s
->narg
== 1 ? "" : "s", s
->name
, s
->name
);
363 if ((void *)s
->handler
== (void *)send_help
) {
369 needconnect
+= s
->needconnect
;
371 if (!connected
&& needconnect
) {
372 printf("?Need to be connected first.\n");
373 printf("'send ?' for help\n");
376 /* Now, do we have enough room? */
377 if (NETROOM() < count
) {
378 printf("There is not enough room in the buffer TO the network\n");
379 printf("to process your request. Nothing will be done.\n");
380 printf("('send synch' will throw away most data in the network\n");
381 printf("buffer, if this might help.)\n");
384 /* OK, they are all OK, now go through again and actually send */
386 for (i
= 1; i
< argc
; i
++) {
387 if ((s
= GETSEND(argv
[i
])) == NULL
) {
388 fprintf(stderr
, "Telnet 'send' error - argument disappeared!\n");
394 success
+= (*s
->handler
)((s
->narg
> 0) ? argv
[i
+1] : 0,
395 (s
->narg
> 1) ? argv
[i
+2] : 0);
398 NET2ADD(IAC
, s
->what
);
399 printoption("SENT", IAC
, s
->what
);
402 return (count
== success
);
413 send_docmd(char *name
)
415 return(send_tncmd(send_do
, "do", name
));
419 send_dontcmd(char *name
)
421 return(send_tncmd(send_dont
, "dont", name
));
425 send_willcmd(char *name
)
427 return(send_tncmd(send_will
, "will", name
));
431 send_wontcmd(char *name
)
433 return(send_tncmd(send_wont
, "wont", name
));
437 send_tncmd(void (*func
)(int, int), const char *cmd
, char *name
)
440 extern char *telopts
[];
443 if (isprefix(name
, "help") || isprefix(name
, "?")) {
446 printf("Usage: send %s <value|option>\n", cmd
);
447 printf("\"value\" must be from 0 to 255\n");
448 printf("Valid options are:\n\t");
451 for (cpp
= telopts
; *cpp
; cpp
++) {
452 len
= strlen(*cpp
) + 3;
453 if (col
+ len
> 65) {
457 printf(" \"%s\"", *cpp
);
463 cpp
= (char **)genget(name
, telopts
, sizeof(char *));
464 if (Ambiguous(cpp
)) {
465 fprintf(stderr
,"'%s': ambiguous argument ('send %s ?' for help).\n",
474 while (*cp
>= '0' && *cp
<= '9') {
480 fprintf(stderr
, "'%s': unknown argument ('send %s ?' for help).\n",
483 } else if (val
< 0 || val
> 255) {
484 fprintf(stderr
, "'%s': bad value ('send %s ?' for help).\n",
490 printf("?Need to be connected first.\n");
500 struct sendlist
*s
; /* pointer to current command */
501 for (s
= Sendlist
; s
->name
; s
++) {
503 printf("%-15s %s\n", s
->name
, s
->help
);
509 * The following are the routines and data structures referred
510 * to by the arguments to the "toggle" command.
525 (SetSockOpt(net
, SOL_SOCKET
, SO_DEBUG
, telnet_debug
)) < 0) {
526 perror("setsockopt (SO_DEBUG)");
530 if (net
> 0 && SetSockOpt(net
, SOL_SOCKET
, SO_DEBUG
, 1) < 0)
531 perror("setsockopt (SO_DEBUG)");
533 printf("Cannot turn off socket debugging\n");
543 printf("Will send carriage returns as telnet <CR><LF>.\n");
545 printf("Will send carriage returns as telnet <CR><NUL>.\n");
555 donebinarytoggle
= 1;
560 if (my_want_state_is_will(TELOPT_BINARY
) &&
561 my_want_state_is_do(TELOPT_BINARY
)) {
563 } else if (my_want_state_is_wont(TELOPT_BINARY
) &&
564 my_want_state_is_dont(TELOPT_BINARY
)) {
567 val
= binmode
? 0 : 1;
571 if (my_want_state_is_will(TELOPT_BINARY
) &&
572 my_want_state_is_do(TELOPT_BINARY
)) {
573 printf("Already operating in binary mode with remote host.\n");
575 printf("Negotiating binary mode with remote host.\n");
579 if (my_want_state_is_wont(TELOPT_BINARY
) &&
580 my_want_state_is_dont(TELOPT_BINARY
)) {
581 printf("Already in network ascii mode with remote host.\n");
583 printf("Negotiating network ascii mode with remote host.\n");
593 donebinarytoggle
= 1;
596 val
= my_want_state_is_do(TELOPT_BINARY
) ? 0 : 1;
599 if (my_want_state_is_do(TELOPT_BINARY
)) {
600 printf("Already receiving in binary mode.\n");
602 printf("Negotiating binary mode on input.\n");
606 if (my_want_state_is_dont(TELOPT_BINARY
)) {
607 printf("Already receiving in network ascii mode.\n");
609 printf("Negotiating network ascii mode on input.\n");
619 donebinarytoggle
= 1;
622 val
= my_want_state_is_will(TELOPT_BINARY
) ? 0 : 1;
625 if (my_want_state_is_will(TELOPT_BINARY
)) {
626 printf("Already transmitting in binary mode.\n");
628 printf("Negotiating binary mode on output.\n");
632 if (my_want_state_is_wont(TELOPT_BINARY
)) {
633 printf("Already transmitting in network ascii mode.\n");
635 printf("Negotiating network ascii mode on output.\n");
643 const char *name
; /* name of toggle */
644 const char *help
; /* help message */
645 int (*handler
)(int); /* routine to do actual setting */
647 const char *actionexplanation
;
650 static struct togglelist Togglelist
[] = {
652 "flushing of output when sending interrupt characters",
655 "flush output when sending interrupt characters" },
657 "automatic sending of interrupt characters in urgent mode",
660 "send interrupt characters in urgent mode" },
661 #ifdef AUTHENTICATION
663 "automatic sending of login and/or authentication info",
666 "send login name and/or authentication information" },
668 "Toggle authentication debugging",
671 "print authentication debugging information" },
675 "automatic encryption of data stream",
678 "automatically encrypt output" },
680 "automatic decryption of data stream",
683 "automatically decrypt input" },
685 "Toggle verbose encryption output",
688 "print verbose encryption output" },
690 "Toggle encryption debugging",
693 "print encryption debugging information" },
694 #endif /* ENCRYPTION */
696 "don't read ~/.telnetrc file",
699 "skip reading of ~/.telnetrc file" },
701 "sending and receiving of binary data",
706 "receiving of binary data",
711 "sending of binary data",
716 "sending carriage returns as telnet <CR><LF>",
717 (int (*)(int))togcrlf
,
721 "mapping of received carriage returns",
724 "map carriage return on output" },
726 "local recognition of certain control characters",
727 (int (*)(int))lclchars
,
729 "recognize certain control characters" },
730 { " ", "", NULL
, NULL
, NULL
}, /* empty line */
733 (int (*)(int))togdebug
,
735 "turn on socket level debugging" },
737 "printing of hexadecimal network data (debugging)",
740 "print hexadecimal representation of network traffic" },
742 "output of \"netdata\" to user readable format (debugging)",
745 "print user readable output for \"netdata\"" },
747 "viewing of options processing (debugging)",
750 "show option processing" },
752 "(debugging) toggle printing of hexadecimal terminal data",
755 "print hexadecimal representation of terminal traffic" },
758 (int (*)(int))togglehelp
,
761 { NULL
, NULL
, NULL
, NULL
, NULL
},
764 (int (*)(int))togglehelp
,
767 { NULL
, NULL
, NULL
, NULL
, NULL
}
773 struct togglelist
*c
;
775 for (c
= Togglelist
; c
->name
; c
++) {
778 printf("%-15s toggle %s\n", c
->name
, c
->help
);
784 printf("%-15s %s\n", "?", "display help information");
789 settogglehelp(int set
)
791 struct togglelist
*c
;
793 for (c
= Togglelist
; c
->name
; c
++) {
796 printf("%-15s %s %s\n", c
->name
, set
? "enable" : "disable",
804 #define GETTOGGLE(name) (struct togglelist *) \
805 genget(name, (char **) Togglelist, sizeof(struct togglelist))
808 toggle(int argc
, char *argv
[])
812 struct togglelist
*c
;
816 "Need an argument to 'toggle' command. 'toggle ?' for help.\n");
824 if (Ambiguous((void *)c
)) {
825 fprintf(stderr
, "'%s': ambiguous argument ('toggle ?' for help).\n",
828 } else if (c
== NULL
) {
829 fprintf(stderr
, "'%s': unknown argument ('toggle ?' for help).\n",
834 *c
->variable
= !*c
->variable
; /* invert it */
835 if (c
->actionexplanation
) {
836 printf("%s %s.\n", *c
->variable
? "Will" : "Won't",
837 c
->actionexplanation
);
841 retval
&= (*c
->handler
)(-1);
849 * The following perform the "set" command.
853 struct termio new_tc
= { 0, 0, 0, 0, {}, 0, 0 };
857 const char *name
; /* name */
858 const char *help
; /* help information */
859 void (*handler
)(char *);
860 cc_t
*charp
; /* where it is located at */
863 static struct setlist Setlist
[] = {
864 #ifdef KLUDGELINEMODE
865 { "echo", "character to toggle local echoing on/off", NULL
, &echoc
},
867 { "escape", "character to escape back to telnet command mode", NULL
, &escape
},
868 { "rlogin", "rlogin escape character", 0, &rlogin
},
869 { "tracefile", "file to write trace information to", SetNetTrace
, (cc_t
*)NetTraceFile
},
870 { " ", "", NULL
, NULL
},
871 { " ", "The following need 'localchars' to be toggled true", NULL
, NULL
},
872 { "flushoutput", "character to cause an Abort Output", NULL
, termFlushCharp
},
873 { "interrupt", "character to cause an Interrupt Process", NULL
, termIntCharp
},
874 { "quit", "character to cause an Abort process", NULL
, termQuitCharp
},
875 { "eof", "character to cause an EOF ", NULL
, termEofCharp
},
876 { " ", "", NULL
, NULL
},
877 { " ", "The following are for local editing in linemode", NULL
, NULL
},
878 { "erase", "character to use to erase a character", NULL
, termEraseCharp
},
879 { "kill", "character to use to erase a line", NULL
, termKillCharp
},
880 { "lnext", "character to use for literal next", NULL
, termLiteralNextCharp
},
881 { "susp", "character to cause a Suspend Process", NULL
, termSuspCharp
},
882 { "reprint", "character to use for line reprint", NULL
, termRprntCharp
},
883 { "worderase", "character to use to erase a word", NULL
, termWerasCharp
},
884 { "start", "character to use for XON", NULL
, termStartCharp
},
885 { "stop", "character to use for XOFF", NULL
, termStopCharp
},
886 { "forw1", "alternate end of line character", NULL
, termForw1Charp
},
887 { "forw2", "alternate end of line character", NULL
, termForw2Charp
},
888 { "ayt", "alternate AYT character", NULL
, termAytCharp
},
889 { NULL
, NULL
, NULL
, NULL
}
892 static struct setlist
*
895 return (struct setlist
*)
896 genget(name
, (char **) Setlist
, sizeof(struct setlist
));
900 set_escape_char(char *s
)
902 if (rlogin
!= _POSIX_VDISABLE
) {
903 rlogin
= (s
&& *s
) ? special(s
) : _POSIX_VDISABLE
;
904 printf("Telnet rlogin escape character is '%s'.\n",
907 escape
= (s
&& *s
) ? special(s
) : _POSIX_VDISABLE
;
908 printf("Telnet escape character is '%s'.\n", control(escape
));
913 setcmd(int argc
, char *argv
[])
917 struct togglelist
*c
;
919 if (argc
< 2 || argc
> 3) {
920 printf("Format is 'set Name Value'\n'set ?' for help.\n");
923 if ((argc
== 2) && (isprefix(argv
[1], "?") || isprefix(argv
[1], "help"))) {
924 for (ct
= Setlist
; ct
->name
; ct
++)
925 printf("%-15s %s\n", ct
->name
, ct
->help
);
928 printf("%-15s %s\n", "?", "display help information");
932 ct
= getset(argv
[1]);
934 c
= GETTOGGLE(argv
[1]);
936 fprintf(stderr
, "'%s': unknown argument ('set ?' for help).\n",
939 } else if (Ambiguous((void *)c
)) {
940 fprintf(stderr
, "'%s': ambiguous argument ('set ?' for help).\n",
945 if ((argc
== 2) || (strcmp("on", argv
[2]) == 0))
947 else if (strcmp("off", argv
[2]) == 0)
950 printf("Format is 'set togglename [on|off]'\n'set ?' for help.\n");
953 if (c
->actionexplanation
) {
954 printf("%s %s.\n", *c
->variable
? "Will" : "Won't",
955 c
->actionexplanation
);
960 } else if (argc
!= 3) {
961 printf("Format is 'set Name Value'\n'set ?' for help.\n");
963 } else if (Ambiguous((void *)ct
)) {
964 fprintf(stderr
, "'%s': ambiguous argument ('set ?' for help).\n",
967 } else if (ct
->handler
) {
968 (*ct
->handler
)(argv
[2]);
969 printf("%s set to \"%s\".\n", ct
->name
, (char *)ct
->charp
);
971 if (strcmp("off", argv
[2])) {
972 value
= special(argv
[2]);
974 value
= _POSIX_VDISABLE
;
976 *(ct
->charp
) = (cc_t
)value
;
977 printf("%s character is '%s'.\n", ct
->name
, control(*(ct
->charp
)));
984 unsetcmd(int argc
, char *argv
[])
987 struct togglelist
*c
;
992 "Need an argument to 'unset' command. 'unset ?' for help.\n");
995 if (isprefix(argv
[1], "?") || isprefix(argv
[1], "help")) {
996 for (ct
= Setlist
; ct
->name
; ct
++)
997 printf("%-15s %s\n", ct
->name
, ct
->help
);
1000 printf("%-15s %s\n", "?", "display help information");
1010 c
= GETTOGGLE(name
);
1012 fprintf(stderr
, "'%s': unknown argument ('unset ?' for help).\n",
1015 } else if (Ambiguous((void *)c
)) {
1016 fprintf(stderr
, "'%s': ambiguous argument ('unset ?' for help).\n",
1022 if (c
->actionexplanation
) {
1023 printf("%s %s.\n", *c
->variable
? "Will" : "Won't",
1024 c
->actionexplanation
);
1029 } else if (Ambiguous((void *)ct
)) {
1030 fprintf(stderr
, "'%s': ambiguous argument ('unset ?' for help).\n",
1033 } else if (ct
->handler
) {
1035 printf("%s reset to \"%s\".\n", ct
->name
, (char *)ct
->charp
);
1037 *(ct
->charp
) = _POSIX_VDISABLE
;
1038 printf("%s character is '%s'.\n", ct
->name
, control(*(ct
->charp
)));
1045 * The following are the data structures and routines for the
1048 #ifdef KLUDGELINEMODE
1049 extern int kludgelinemode
;
1055 send_wont(TELOPT_LINEMODE
, 1);
1056 send_dont(TELOPT_SGA
, 1);
1057 send_dont(TELOPT_ECHO
, 1);
1065 #ifdef KLUDGELINEMODE
1067 send_dont(TELOPT_SGA
, 1);
1069 send_will(TELOPT_LINEMODE
, 1);
1070 send_dont(TELOPT_ECHO
, 1);
1077 #ifdef KLUDGELINEMODE
1079 send_do(TELOPT_SGA
, 1);
1082 send_wont(TELOPT_LINEMODE
, 1);
1083 send_do(TELOPT_ECHO
, 1);
1088 dolmmode(int bit
, int on
)
1091 extern int linemode
;
1093 if (my_want_state_is_wont(TELOPT_LINEMODE
)) {
1094 printf("?Need to have LINEMODE option enabled first.\n");
1095 printf("'mode ?' for help.\n");
1100 c
= (linemode
| bit
);
1102 c
= (linemode
& ~bit
);
1110 return dolmmode(bit
, 1);
1116 return dolmmode(bit
, 0);
1120 const char *name
; /* command name */
1121 const char *help
; /* help string */
1122 int (*handler
)(int);/* routine which executes command */
1123 int needconnect
; /* Do we need to be connected to execute? */
1127 static struct modelist ModeList
[] = {
1128 { "character", "Disable LINEMODE option", (int (*)(int))docharmode
, 1, 0 },
1129 #ifdef KLUDGELINEMODE
1130 { "", "(or disable obsolete line-by-line mode)", NULL
, 0, 0 },
1132 { "line", "Enable LINEMODE option", (int (*)(int))dolinemode
, 1, 0 },
1133 #ifdef KLUDGELINEMODE
1134 { "", "(or enable obsolete line-by-line mode)", NULL
, 0, 0 },
1136 { "", "", NULL
, 0, 0 },
1137 { "", "These require the LINEMODE option to be enabled", NULL
, 0, 0 },
1138 { "isig", "Enable signal trapping", setmod
, 1, MODE_TRAPSIG
},
1139 { "+isig", 0, setmod
, 1, MODE_TRAPSIG
},
1140 { "-isig", "Disable signal trapping", clearmode
, 1, MODE_TRAPSIG
},
1141 { "edit", "Enable character editing", setmod
, 1, MODE_EDIT
},
1142 { "+edit", 0, setmod
, 1, MODE_EDIT
},
1143 { "-edit", "Disable character editing", clearmode
, 1, MODE_EDIT
},
1144 { "softtabs", "Enable tab expansion", setmod
, 1, MODE_SOFT_TAB
},
1145 { "+softtabs", 0, setmod
, 1, MODE_SOFT_TAB
},
1146 { "-softtabs", "Disable character editing", clearmode
, 1, MODE_SOFT_TAB
},
1147 { "litecho", "Enable literal character echo", setmod
, 1, MODE_LIT_ECHO
},
1148 { "+litecho", 0, setmod
, 1, MODE_LIT_ECHO
},
1149 { "-litecho", "Disable literal character echo", clearmode
, 1, MODE_LIT_ECHO
},
1150 { "help", 0, (int (*)(int))modehelp
, 0, 0 },
1151 #ifdef KLUDGELINEMODE
1152 { "kludgeline", 0, (int (*)(int))dokludgemode
, 1, 0 },
1154 { "", "", NULL
, 0, 0 },
1155 { "?", "Print help information", (int (*)(int))modehelp
, 0, 0 },
1156 { NULL
, NULL
, NULL
, 0, 0 },
1163 struct modelist
*mt
;
1165 printf("format is: 'mode Mode', where 'Mode' is one of:\n\n");
1166 for (mt
= ModeList
; mt
->name
; mt
++) {
1169 printf("%-15s %s\n", mt
->name
, mt
->help
);
1177 #define GETMODECMD(name) (struct modelist *) \
1178 genget(name, (char **) ModeList, sizeof(struct modelist))
1181 modecmd(int argc
, char *argv
[])
1183 struct modelist
*mt
;
1186 printf("'mode' command requires an argument\n");
1187 printf("'mode ?' for help.\n");
1188 } else if ((mt
= GETMODECMD(argv
[1])) == NULL
) {
1189 fprintf(stderr
, "Unknown mode '%s' ('mode ?' for help).\n", argv
[1]);
1190 } else if (Ambiguous((void *)mt
)) {
1191 fprintf(stderr
, "Ambiguous mode '%s' ('mode ?' for help).\n", argv
[1]);
1192 } else if (mt
->needconnect
&& !connected
) {
1193 printf("?Need to be connected first.\n");
1194 printf("'mode ?' for help.\n");
1195 } else if (mt
->handler
) {
1196 return (*mt
->handler
)(mt
->arg1
);
1202 * The following data structures and routines implement the
1203 * "display" command.
1207 display(int argc
, char *argv
[])
1209 struct togglelist
*tl
;
1212 #define dotog(tl) if (tl->variable && tl->actionexplanation) { \
1213 if (*tl->variable) { \
1218 printf(" %s.\n", tl->actionexplanation); \
1221 #define doset(sl) if (sl->name && *sl->name != ' ') { \
1222 if (sl->handler == 0) \
1223 printf("%-15s [%s]\n", sl->name, control(*sl->charp)); \
1225 printf("%-15s \"%s\"\n", sl->name, (char *)sl->charp); \
1229 for (tl
= Togglelist
; tl
->name
; tl
++) {
1233 for (sl
= Setlist
; sl
->name
; sl
++) {
1239 for (i
= 1; i
< argc
; i
++) {
1240 sl
= getset(argv
[i
]);
1241 tl
= GETTOGGLE(argv
[i
]);
1242 if (Ambiguous((void *)sl
) || Ambiguous((void *)tl
)) {
1243 printf("?Ambiguous argument '%s'.\n", argv
[i
]);
1245 } else if (!sl
&& !tl
) {
1246 printf("?Unknown argument '%s'.\n", argv
[i
]);
1258 /*@*/optionstatus();
1261 #endif /* ENCRYPTION */
1268 * The following are the data structures, and many of the routines,
1269 * relating to command processing.
1273 * Set the escape character.
1276 setescape(int argc
, char *argv
[])
1282 "Deprecated usage - please use 'set escape%s%s' in the future.\n",
1283 (argc
> 2)? " ":"", (argc
> 2)? argv
[1]: "");
1287 printf("new escape character: ");
1288 (void) fgets(buf
, sizeof(buf
), stdin
);
1293 (void) fflush(stdout
);
1301 printf("Deprecated usage - please use 'toggle crmod' in the future.\n");
1302 printf("%s map carriage return on output.\n", crmod
? "Will" : "Won't");
1303 (void) fflush(stdout
);
1313 long oldrows
, oldcols
, newrows
, newcols
, err_
;
1315 err_
= (TerminalWindowSize(&oldrows
, &oldcols
) == 0) ? 1 : 0;
1316 (void) kill(0, SIGTSTP
);
1318 * If we didn't get the window size before the SUSPEND, but we
1319 * can get them now (?), then send the NAWS to make sure that
1320 * we are set up for the right window size.
1322 if (TerminalWindowSize(&newrows
, &newcols
) && connected
&&
1323 (err_
|| ((oldrows
!= newrows
) || (oldcols
!= newcols
)))) {
1327 /* reget parameters in case they were changed */
1328 TerminalSaveState();
1331 printf("Suspend is not supported. Try the '!' command instead\n");
1337 shell(int argc
, char *argv
[] __unused
)
1339 long oldrows
, oldcols
, newrows
, newcols
, err_
;
1343 err_
= (TerminalWindowSize(&oldrows
, &oldcols
) == 0) ? 1 : 0;
1346 perror("Fork failed\n");
1352 * Fire up the shell in the child.
1354 const char *shellp
, *shellname
;
1356 shellp
= getenv("SHELL");
1359 if ((shellname
= strrchr(shellp
, '/')) == NULL
)
1364 execl(shellp
, shellname
, "-c", &saveline
[1], NULL
);
1366 execl(shellp
, shellname
, NULL
);
1371 (void)wait(NULL
); /* Wait for the shell to complete */
1373 if (TerminalWindowSize(&newrows
, &newcols
) && connected
&&
1374 (err_
|| ((oldrows
!= newrows
) || (oldcols
!= newcols
)))) {
1383 bye(int argc
, char *argv
[])
1385 extern int resettermname
;
1388 (void) shutdown(net
, SHUT_RDWR
);
1389 printf("Connection closed.\n");
1390 (void) NetClose(net
);
1393 #ifdef AUTHENTICATION
1395 auth_encrypt_connect(connected
);
1401 if ((argc
!= 2) || (strcmp(argv
[1], "fromquit") != 0)) {
1402 longjmp(toplevel
, 1);
1405 return 1; /* Keep lint, etc., happy */
1411 (void) call(bye
, "bye", "fromquit", 0);
1418 send_do(TELOPT_LOGOUT
, 1);
1431 void (*handler
)(int);
1435 static void slc_help(void);
1437 struct slclist SlcList
[] = {
1438 { "export", "Use local special character definitions",
1439 (void (*)(int))slc_mode_export
, 0 },
1440 { "import", "Use remote special character definitions",
1441 slc_mode_import
, 1 },
1442 { "check", "Verify remote special character definitions",
1443 slc_mode_import
, 0 },
1444 { "help", NULL
, (void (*)(int))slc_help
, 0 },
1445 { "?", "Print help information", (void (*)(int))slc_help
, 0 },
1446 { NULL
, NULL
, NULL
, 0 },
1454 for (c
= SlcList
; c
->name
; c
++) {
1457 printf("%-15s %s\n", c
->name
, c
->help
);
1464 static struct slclist
*
1467 return (struct slclist
*)
1468 genget(name
, (char **) SlcList
, sizeof(struct slclist
));
1472 slccmd(int argc
, char *argv
[])
1478 "Need an argument to 'slc' command. 'slc ?' for help.\n");
1481 c
= getslc(argv
[1]);
1483 fprintf(stderr
, "'%s': unknown argument ('slc ?' for help).\n",
1487 if (Ambiguous((void *)c
)) {
1488 fprintf(stderr
, "'%s': ambiguous argument ('slc ?' for help).\n",
1492 (*c
->handler
)(c
->arg
);
1498 * The ENVIRON command.
1504 void (*handler
)(unsigned char *, unsigned char *);
1508 extern struct env_lst
*
1509 env_define(const unsigned char *, unsigned char *);
1511 env_undefine(const unsigned char *),
1512 env_export(const unsigned char *),
1513 env_unexport(const unsigned char *),
1514 env_send(const unsigned char *),
1515 #if defined(OLD_ENVIRON) && defined(ENV_HACK)
1516 env_varval(const unsigned char *),
1522 struct envlist EnvList
[] = {
1523 { "define", "Define an environment variable",
1524 (void (*)(unsigned char *, unsigned char *))env_define
, 2 },
1525 { "undefine", "Undefine an environment variable",
1526 (void (*)(unsigned char *, unsigned char *))env_undefine
, 1 },
1527 { "export", "Mark an environment variable for automatic export",
1528 (void (*)(unsigned char *, unsigned char *))env_export
, 1 },
1529 { "unexport", "Don't mark an environment variable for automatic export",
1530 (void (*)(unsigned char *, unsigned char *))env_unexport
, 1 },
1531 { "send", "Send an environment variable", (void (*)(unsigned char *, unsigned char *))env_send
, 1 },
1532 { "list", "List the current environment variables",
1533 (void (*)(unsigned char *, unsigned char *))env_list
, 0 },
1534 #if defined(OLD_ENVIRON) && defined(ENV_HACK)
1535 { "varval", "Reverse VAR and VALUE (auto, right, wrong, status)",
1536 (void (*)(unsigned char *, unsigned char *))env_varval
, 1 },
1538 { "help", NULL
, (void (*)(unsigned char *, unsigned char *))env_help
, 0 },
1539 { "?", "Print help information", (void (*)(unsigned char *, unsigned char *))env_help
, 0 },
1540 { NULL
, NULL
, NULL
, 0 },
1548 for (c
= EnvList
; c
->name
; c
++) {
1551 printf("%-15s %s\n", c
->name
, c
->help
);
1558 static struct envlist
*
1559 getenvcmd(char *name
)
1561 return (struct envlist
*)
1562 genget(name
, (char **) EnvList
, sizeof(struct envlist
));
1566 env_cmd(int argc
, char *argv
[])
1572 "Need an argument to 'environ' command. 'environ ?' for help.\n");
1575 c
= getenvcmd(argv
[1]);
1577 fprintf(stderr
, "'%s': unknown argument ('environ ?' for help).\n",
1581 if (Ambiguous((void *)c
)) {
1582 fprintf(stderr
, "'%s': ambiguous argument ('environ ?' for help).\n",
1586 if (c
->narg
+ 2 != argc
) {
1588 "Need %s%d argument%s to 'environ %s' command. 'environ ?' for help.\n",
1589 c
->narg
< argc
+ 2 ? "only " : "",
1590 c
->narg
, c
->narg
== 1 ? "" : "s", c
->name
);
1593 (*c
->handler
)(argv
[2], argv
[3]);
1598 struct env_lst
*next
; /* pointer to next structure */
1599 struct env_lst
*prev
; /* pointer to previous structure */
1600 unsigned char *var
; /* pointer to variable name */
1601 unsigned char *value
; /* pointer to variable value */
1602 int export
; /* 1 -> export with default list of variables */
1603 int welldefined
; /* A well defined variable */
1606 struct env_lst envlisthead
;
1608 static struct env_lst
*
1609 env_find(const unsigned char *var
)
1613 for (ep
= envlisthead
.next
; ep
; ep
= ep
->next
) {
1614 if (strcmp(ep
->var
, var
) == 0)
1623 extern char **environ
;
1627 for (epp
= environ
; *epp
; epp
++) {
1628 if ((cp
= strchr(*epp
, '='))) {
1630 ep
= env_define((unsigned char *)*epp
,
1631 (unsigned char *)cp
+1);
1637 * Special case for DISPLAY variable. If it is ":0.0" or
1638 * "unix:0.0", we have to get rid of "unix" and insert our
1641 if ((ep
= env_find("DISPLAY"))
1642 && ((*ep
->value
== ':')
1643 || (strncmp((char *)ep
->value
, "unix:", 5) == 0))) {
1645 char *cp2
= strchr((char *)ep
->value
, ':');
1647 gethostname(hbuf
, 256);
1649 cp
= (char *)malloc(strlen(hbuf
) + strlen(cp2
) + 1);
1650 sprintf((char *)cp
, "%s%s", hbuf
, cp2
);
1652 ep
->value
= (unsigned char *)cp
;
1655 * If USER is not defined, but LOGNAME is, then add
1656 * USER with the value from LOGNAME. By default, we
1657 * don't export the USER variable.
1659 if ((env_find("USER") == NULL
) && (ep
= env_find("LOGNAME"))) {
1660 env_define("USER", ep
->value
);
1661 env_unexport("USER");
1663 env_export("DISPLAY");
1664 env_export("PRINTER");
1668 env_define(const unsigned char *var
, unsigned char *value
)
1672 if ((ep
= env_find(var
))) {
1678 ep
= (struct env_lst
*)malloc(sizeof(struct env_lst
));
1679 ep
->next
= envlisthead
.next
;
1680 envlisthead
.next
= ep
;
1681 ep
->prev
= &envlisthead
;
1683 ep
->next
->prev
= ep
;
1685 ep
->welldefined
= opt_welldefined(var
);
1687 ep
->var
= strdup(var
);
1688 ep
->value
= strdup(value
);
1693 env_undefine(const unsigned char *var
)
1697 if ((ep
= env_find(var
))) {
1698 ep
->prev
->next
= ep
->next
;
1700 ep
->next
->prev
= ep
->prev
;
1710 env_export(const unsigned char *var
)
1714 if ((ep
= env_find(var
)))
1719 env_unexport(const unsigned char *var
)
1723 if ((ep
= env_find(var
)))
1728 env_send(const unsigned char *var
)
1732 if (my_state_is_wont(TELOPT_NEW_ENVIRON
)
1734 && my_state_is_wont(TELOPT_OLD_ENVIRON
)
1738 "Cannot send '%s': Telnet ENVIRON option not enabled\n",
1744 fprintf(stderr
, "Cannot send '%s': variable not defined\n",
1748 env_opt_start_info();
1749 env_opt_add(ep
->var
);
1758 for (ep
= envlisthead
.next
; ep
; ep
= ep
->next
) {
1759 printf("%c %-20s %s\n", ep
->export
? '*' : ' ',
1760 ep
->var
, ep
->value
);
1765 env_default(int init
, int welldefined
)
1767 static struct env_lst
*nep
= NULL
;
1774 while ((nep
= nep
->next
)) {
1775 if (nep
->export
&& (nep
->welldefined
== welldefined
))
1783 env_getvalue(const unsigned char *var
)
1787 if ((ep
= env_find(var
)))
1792 #if defined(OLD_ENVIRON) && defined(ENV_HACK)
1794 env_varval(const unsigned char *what
)
1796 extern int old_env_var
, old_env_value
, env_auto
;
1797 int len
= strlen((char *)what
);
1802 if (strncasecmp((char *)what
, "status", len
) == 0) {
1804 printf("%s%s", "VAR and VALUE are/will be ",
1805 "determined automatically\n");
1806 if (old_env_var
== OLD_ENV_VAR
)
1807 printf("VAR and VALUE set to correct definitions\n");
1809 printf("VAR and VALUE definitions are reversed\n");
1810 } else if (strncasecmp((char *)what
, "auto", len
) == 0) {
1812 old_env_var
= OLD_ENV_VALUE
;
1813 old_env_value
= OLD_ENV_VAR
;
1814 } else if (strncasecmp((char *)what
, "right", len
) == 0) {
1816 old_env_var
= OLD_ENV_VAR
;
1817 old_env_value
= OLD_ENV_VALUE
;
1818 } else if (strncasecmp((char *)what
, "wrong", len
) == 0) {
1820 old_env_var
= OLD_ENV_VALUE
;
1821 old_env_value
= OLD_ENV_VAR
;
1824 printf("Unknown \"varval\" command. (\"auto\", \"right\", \"wrong\", \"status\")\n");
1829 #ifdef AUTHENTICATION
1831 * The AUTHENTICATE command.
1837 int (*handler
)(char *);
1842 auth_enable(char *),
1843 auth_disable(char *),
1848 struct authlist AuthList
[] = {
1849 { "status", "Display current status of authentication information",
1850 (int (*)(char *))auth_status
, 0 },
1851 { "disable", "Disable an authentication type ('auth disable ?' for more)",
1853 { "enable", "Enable an authentication type ('auth enable ?' for more)",
1855 { "help", NULL
, (int (*)(char *))auth_help
, 0 },
1856 { "?", "Print help information", (int (*)(char *))auth_help
, 0 },
1857 { NULL
, NULL
, NULL
, 0 },
1865 for (c
= AuthList
; c
->name
; c
++) {
1868 printf("%-15s %s\n", c
->name
, c
->help
);
1877 auth_cmd(int argc
, char *argv
[])
1883 "Need an argument to 'auth' command. 'auth ?' for help.\n");
1887 c
= (struct authlist
*)
1888 genget(argv
[1], (char **) AuthList
, sizeof(struct authlist
));
1890 fprintf(stderr
, "'%s': unknown argument ('auth ?' for help).\n",
1894 if (Ambiguous((void *)c
)) {
1895 fprintf(stderr
, "'%s': ambiguous argument ('auth ?' for help).\n",
1899 if (c
->narg
+ 2 != argc
) {
1901 "Need %s%d argument%s to 'auth %s' command. 'auth ?' for help.\n",
1902 c
->narg
< argc
+ 2 ? "only " : "",
1903 c
->narg
, c
->narg
== 1 ? "" : "s", c
->name
);
1906 return((*c
->handler
)(argv
[2]));
1912 * The ENCRYPT command.
1915 struct encryptlist
{
1918 int (*handler
)(char *, char *);
1925 EncryptEnable(char *, char *),
1926 EncryptDisable(char *, char *),
1927 EncryptType(char *, char *),
1928 EncryptStart(char *),
1929 EncryptStartInput(void),
1930 EncryptStartOutput(void),
1931 EncryptStop(char *),
1932 EncryptStopInput(void),
1933 EncryptStopOutput(void),
1934 EncryptStatus(void);
1938 struct encryptlist EncryptList
[] = {
1939 { "enable", "Enable encryption. ('encrypt enable ?' for more)",
1940 EncryptEnable
, 1, 1, 2 },
1941 { "disable", "Disable encryption. ('encrypt enable ?' for more)",
1942 EncryptDisable
, 0, 1, 2 },
1943 { "type", "Set encryption type. ('encrypt type ?' for more)",
1944 EncryptType
, 0, 1, 1 },
1945 { "start", "Start encryption. ('encrypt start ?' for more)",
1946 (int (*)(char *, char *))EncryptStart
, 1, 0, 1 },
1947 { "stop", "Stop encryption. ('encrypt stop ?' for more)",
1948 (int (*)(char *, char *))EncryptStop
, 1, 0, 1 },
1949 { "input", "Start encrypting the input stream",
1950 (int (*)(char *, char *))EncryptStartInput
, 1, 0, 0 },
1951 { "-input", "Stop encrypting the input stream",
1952 (int (*)(char *, char *))EncryptStopInput
, 1, 0, 0 },
1953 { "output", "Start encrypting the output stream",
1954 (int (*)(char *, char *))EncryptStartOutput
, 1, 0, 0 },
1955 { "-output", "Stop encrypting the output stream",
1956 (int (*)(char *, char *))EncryptStopOutput
, 1, 0, 0 },
1958 { "status", "Display current status of authentication information",
1959 (int (*)(char *, char *))EncryptStatus
, 0, 0, 0 },
1960 { "help", NULL
, (int (*)(char *, char *))EncryptHelp
, 0, 0, 0 },
1961 { "?", "Print help information", (int (*)(char *, char *))EncryptHelp
, 0, 0, 0 },
1962 { NULL
, NULL
, NULL
, 0, 0, 0 },
1968 struct encryptlist
*c
;
1970 for (c
= EncryptList
; c
->name
; c
++) {
1973 printf("%-15s %s\n", c
->name
, c
->help
);
1982 encrypt_cmd(int argc
, char *argv
[])
1984 struct encryptlist
*c
;
1988 "Need an argument to 'encrypt' command. 'encrypt ?' for help.\n");
1992 c
= (struct encryptlist
*)
1993 genget(argv
[1], (char **) EncryptList
, sizeof(struct encryptlist
));
1995 fprintf(stderr
, "'%s': unknown argument ('encrypt ?' for help).\n",
1999 if (Ambiguous((void *)c
)) {
2000 fprintf(stderr
, "'%s': ambiguous argument ('encrypt ?' for help).\n",
2005 if (argc
< c
->minarg
|| argc
> c
->maxarg
) {
2006 if (c
->minarg
== c
->maxarg
) {
2007 fprintf(stderr
, "Need %s%d argument%s ",
2008 c
->minarg
< argc
? "only " : "", c
->minarg
,
2009 c
->minarg
== 1 ? "" : "s");
2011 fprintf(stderr
, "Need %s%d-%d arguments ",
2012 c
->maxarg
< argc
? "only " : "", c
->minarg
, c
->maxarg
);
2014 fprintf(stderr
, "to 'encrypt %s' command. 'encrypt ?' for help.\n",
2018 if (c
->needconnect
&& !connected
) {
2019 if (!(argc
&& (isprefix(argv
[2], "help") || isprefix(argv
[2], "?")))) {
2020 printf("?Need to be connected first.\n");
2024 return ((*c
->handler
)(argc
> 0 ? argv
[2] : 0,
2025 argc
> 1 ? argv
[3] : 0));
2027 #endif /* ENCRYPTION */
2030 * Print status about the connection.
2034 status(int argc
, char *argv
[])
2037 printf("Connected to %s.\n", hostname
);
2038 if ((argc
< 2) || strcmp(argv
[1], "notmuch")) {
2039 int mode
= getconnmode();
2041 if (my_want_state_is_will(TELOPT_LINEMODE
)) {
2042 printf("Operating with LINEMODE option\n");
2043 printf("%s line editing\n", (mode
&MODE_EDIT
) ? "Local" : "No");
2044 printf("%s catching of signals\n",
2045 (mode
&MODE_TRAPSIG
) ? "Local" : "No");
2047 #ifdef KLUDGELINEMODE
2048 } else if (kludgelinemode
&& my_want_state_is_dont(TELOPT_SGA
)) {
2049 printf("Operating in obsolete linemode\n");
2052 printf("Operating in single character mode\n");
2054 printf("Catching signals locally\n");
2056 printf("%s character echo\n", (mode
&MODE_ECHO
) ? "Local" : "Remote");
2057 if (my_want_state_is_will(TELOPT_LFLOW
))
2058 printf("%s flow control\n", (mode
&MODE_FLOW
) ? "Local" : "No");
2061 #endif /* ENCRYPTION */
2064 printf("No connection.\n");
2066 printf("Escape character is '%s'.\n", control(escape
));
2067 (void) fflush(stdout
);
2073 * Function that gets called when SIGINFO is received.
2078 (void) call(status
, "status", "notmuch", 0);
2083 sockaddr_ntop(struct sockaddr
*sa
)
2086 static char addrbuf
[INET6_ADDRSTRLEN
];
2088 switch (sa
->sa_family
) {
2090 addr
= &((struct sockaddr_in
*)sa
)->sin_addr
;
2093 addr
= &((struct sockaddr_un
*)sa
)->sun_path
;
2097 addr
= &((struct sockaddr_in6
*)sa
)->sin6_addr
;
2103 inet_ntop(sa
->sa_family
, addr
, addrbuf
, sizeof(addrbuf
));
2109 * When an Address Family related error happend, check if retry with
2110 * another AF is possible or not.
2111 * Return 1, if retry with another af is OK. Else, return 0.
2114 switch_af(struct addrinfo
**aip
)
2117 struct addrinfo
*ai
;
2120 nextaf
= (ai
->ai_family
== AF_INET
) ? AF_INET6
: AF_INET
;
2123 while (ai
!= NULL
&& ai
->ai_family
!= nextaf
);
2133 tn(int argc
, char *argv
[])
2138 int srcroute
= 0, result
;
2139 char *cmd
, *hostp
= NULL
, *portp
= NULL
, *user
= NULL
;
2140 char *src_addr
= NULL
;
2141 struct addrinfo hints
, *res
, *res0
= NULL
, *src_res
, *src_res0
= NULL
;
2142 int error
= 0, af_error
= 0;
2145 printf("?Already connected to %s\n", hostname
);
2150 (void) strlcpy(line
, "open ", sizeof(line
));
2152 (void) fgets(&line
[strlen(line
)], sizeof(line
) - strlen(line
), stdin
);
2160 if (strcmp(*argv
, "help") == 0 || isprefix(*argv
, "?"))
2162 if (strcmp(*argv
, "-l") == 0) {
2170 if (strcmp(*argv
, "-a") == 0) {
2175 if (strcmp(*argv
, "-s") == 0) {
2183 if (hostp
== NULL
) {
2188 if (portp
== NULL
) {
2194 printf("usage: %s [-l user] [-a] [-s src_addr] host-name [port]\n", cmd
);
2201 if (src_addr
!= NULL
) {
2202 memset(&hints
, 0, sizeof(hints
));
2203 hints
.ai_flags
= AI_NUMERICHOST
;
2204 hints
.ai_family
= family
;
2205 hints
.ai_socktype
= SOCK_STREAM
;
2206 error
= getaddrinfo(src_addr
, 0, &hints
, &src_res
);
2207 if (error
== EAI_NODATA
) {
2209 error
= getaddrinfo(src_addr
, 0, &hints
, &src_res
);
2212 fprintf(stderr
, "%s: %s\n", src_addr
, gai_strerror(error
));
2213 if (error
== EAI_SYSTEM
)
2214 fprintf(stderr
, "%s: %s\n", src_addr
, strerror(errno
));
2220 if (hostp
[0] == '/') {
2221 struct sockaddr_un su
;
2223 if (strlen(hostp
) >= sizeof(su
.sun_path
)) {
2224 fprintf(stderr
, "hostname too long for unix domain socket: %s",
2228 memset(&su
, 0, sizeof su
);
2229 su
.sun_family
= AF_UNIX
;
2230 strncpy(su
.sun_path
, hostp
, sizeof su
.sun_path
);
2231 printf("Trying %s...\n", hostp
);
2232 net
= socket(PF_UNIX
, SOCK_STREAM
, 0);
2237 if (connect(net
, (struct sockaddr
*)&su
, sizeof su
) == -1) {
2238 perror(su
.sun_path
);
2239 (void) NetClose(net
);
2243 } else if (hostp
[0] == '@' || hostp
[0] == '!') {
2246 family
== AF_INET6
||
2248 (hostname
= strrchr(hostp
, ':')) == NULL
)
2249 hostname
= strrchr(hostp
, '@');
2250 if (hostname
== NULL
) {
2260 portp
= strdup("telnet");
2261 } else if (*portp
== '-') {
2267 memset(&hints
, 0, sizeof(hints
));
2268 hints
.ai_flags
= AI_NUMERICHOST
;
2269 hints
.ai_family
= family
;
2270 hints
.ai_socktype
= SOCK_STREAM
;
2271 error
= getaddrinfo(hostname
, portp
, &hints
, &res
);
2273 hints
.ai_flags
= AI_CANONNAME
;
2274 error
= getaddrinfo(hostname
, portp
, &hints
, &res
);
2277 fprintf(stderr
, "%s: %s\n", hostname
, gai_strerror(error
));
2278 if (error
== EAI_SYSTEM
)
2279 fprintf(stderr
, "%s: %s\n", hostname
, strerror(errno
));
2283 if (hints
.ai_flags
== AI_NUMERICHOST
) {
2284 /* hostname has numeric */
2288 gni_err
= getnameinfo(res
->ai_addr
, res
->ai_addr
->sa_len
,
2289 _hostname
, sizeof(_hostname
) - 1, NULL
, 0,
2292 (void) strncpy(_hostname
, hostp
, sizeof(_hostname
) - 1);
2293 _hostname
[sizeof(_hostname
)-1] = '\0';
2294 hostname
= _hostname
;
2296 /* hostname has FQDN */
2298 (void) strncpy(_hostname
, hostname
, sizeof(_hostname
) - 1);
2299 else if (res
->ai_canonname
!= NULL
)
2300 strcpy(_hostname
, res
->ai_canonname
);
2302 (void) strncpy(_hostname
, hostp
, sizeof(_hostname
) - 1);
2303 _hostname
[sizeof(_hostname
)-1] = '\0';
2304 hostname
= _hostname
;
2310 if (srcroute
!= 0) {
2311 static char hostbuf
[BUFSIZ
];
2313 if (af_error
== 0) { /* save intermediate hostnames for retry */
2314 strncpy(hostbuf
, hostp
, BUFSIZ
- 1);
2315 hostbuf
[BUFSIZ
- 1] = '\0';
2319 result
= sourceroute(res
, hostp
, &srp
, &srlen
, &proto
, &opt
);
2322 if (family
== AF_UNSPEC
&& af_error
== 0 &&
2323 switch_af(&res
) == 1) {
2330 } else if (result
== -1) {
2331 printf("Bad source route option: %s\n", hostp
);
2337 printf("Trying %s...\n", sockaddr_ntop(res
->ai_addr
));
2338 net
= socket(res
->ai_family
, res
->ai_socktype
, res
->ai_protocol
);
2342 if (family
== AF_UNSPEC
&& af_error
== 0 &&
2343 switch_af(&res
) == 1) {
2348 perror("telnet: socket");
2351 if (srp
&& setsockopt(net
, proto
, opt
, (char *)srp
, srlen
) < 0)
2352 perror("setsockopt (source route)");
2353 #if defined(IPPROTO_IP) && defined(IP_TOS)
2354 if (res
->ai_family
== PF_INET
) {
2355 # if defined(HAS_GETTOS)
2357 if (tos
< 0 && (tp
= gettosbyname("telnet", "tcp")))
2361 tos
= IPTOS_LOWDELAY
;
2363 && (setsockopt(net
, IPPROTO_IP
, IP_TOS
,
2364 (char *)&tos
, sizeof(int)) < 0)
2365 && (errno
!= ENOPROTOOPT
))
2366 perror("telnet: setsockopt (IP_TOS) (ignored)");
2368 #endif /* defined(IPPROTO_IP) && defined(IP_TOS) */
2370 if (telnet_debug
&& SetSockOpt(net
, SOL_SOCKET
, SO_DEBUG
, 1) < 0) {
2371 perror("setsockopt (SO_DEBUG)");
2374 if (src_addr
!= NULL
) {
2375 for (src_res
= src_res0
; src_res
!= NULL
; src_res
= src_res
->ai_next
)
2376 if (src_res
->ai_family
== res
->ai_family
)
2378 if (src_res
== NULL
)
2380 if (bind(net
, src_res
->ai_addr
, src_res
->ai_addrlen
) == -1) {
2382 if (family
== AF_UNSPEC
&& af_error
== 0 &&
2383 switch_af(&res
) == 1) {
2385 (void) NetClose(net
);
2390 (void) NetClose(net
);
2395 if (connect(net
, res
->ai_addr
, res
->ai_addrlen
) < 0) {
2396 struct addrinfo
*next
;
2398 next
= res
->ai_next
;
2399 /* If already an af failed, only try same af. */
2401 while (next
!= NULL
&& next
->ai_family
!= res
->ai_family
)
2402 next
= next
->ai_next
;
2403 warn("connect to address %s", sockaddr_ntop(res
->ai_addr
));
2406 (void) NetClose(net
);
2409 warnx("Unable to connect to remote host");
2410 (void) NetClose(net
);
2414 #ifdef AUTHENTICATION
2416 auth_encrypt_connect(connected
);
2419 } while (connected
== 0);
2421 if (src_res0
!= NULL
)
2422 freeaddrinfo(src_res0
);
2423 cmdrc(hostp
, hostname
);
2425 if (autologin
&& user
== NULL
) {
2428 user
= getenv("USER");
2430 ((pw
= getpwnam(user
)) && pw
->pw_uid
!= getuid())) {
2431 if ((pw
= getpwuid(getuid())))
2438 env_define("USER", user
);
2441 (void) call(status
, "status", "notmuch", 0);
2442 if (setjmp(peerdied
) == 0)
2444 (void) NetClose(net
);
2445 ExitString("Connection closed by foreign host.\n",1);
2450 if (src_res0
!= NULL
)
2451 freeaddrinfo(src_res0
);
2455 #define HELPINDENT (sizeof ("connect"))
2458 openhelp
[] = "connect to a site",
2459 closehelp
[] = "close current connection",
2460 logouthelp
[] = "forcibly logout remote user and close the connection",
2461 quithelp
[] = "exit telnet",
2462 statushelp
[] = "print status information",
2463 helphelp
[] = "print help information",
2464 sendhelp
[] = "transmit special characters ('send ?' for more)",
2465 sethelp
[] = "set operating parameters ('set ?' for more)",
2466 unsethelp
[] = "unset operating parameters ('unset ?' for more)",
2467 togglestring
[] ="toggle operating parameters ('toggle ?' for more)",
2468 slchelp
[] = "change state of special characters ('slc ?' for more)",
2469 displayhelp
[] = "display operating parameters",
2470 #ifdef AUTHENTICATION
2471 authhelp
[] = "turn on (off) authentication ('auth ?' for more)",
2474 encrypthelp
[] = "turn on (off) encryption ('encrypt ?' for more)",
2475 #endif /* ENCRYPTION */
2476 zhelp
[] = "suspend telnet",
2478 opiehelp
[] = "compute response to OPIE challenge",
2480 shellhelp
[] = "invoke a subshell",
2481 envhelp
[] = "change environment variables ('environ ?' for more)",
2482 modestring
[] = "try to enter line or character mode ('mode ?' for more)";
2484 static Command cmdtab
[] = {
2485 { "close", closehelp
, bye
, 1 },
2486 { "logout", logouthelp
, (int (*)(int, char **))logout
, 1 },
2487 { "display", displayhelp
, display
, 0 },
2488 { "mode", modestring
, modecmd
, 0 },
2489 { "telnet", openhelp
, tn
, 0 },
2490 { "open", openhelp
, tn
, 0 },
2491 { "quit", quithelp
, (int (*)(int, char **))quit
, 0 },
2492 { "send", sendhelp
, sendcmd
, 0 },
2493 { "set", sethelp
, setcmd
, 0 },
2494 { "unset", unsethelp
, unsetcmd
, 0 },
2495 { "status", statushelp
, status
, 0 },
2496 { "toggle", togglestring
, toggle
, 0 },
2497 { "slc", slchelp
, slccmd
, 0 },
2498 #ifdef AUTHENTICATION
2499 { "auth", authhelp
, auth_cmd
, 0 },
2502 { "encrypt", encrypthelp
, encrypt_cmd
, 0 },
2503 #endif /* ENCRYPTION */
2504 { "z", zhelp
, (int (*)(int, char **))suspend
, 0 },
2505 { "!", shellhelp
, shell
, 1 },
2506 { "environ", envhelp
, env_cmd
, 0 },
2507 { "?", helphelp
, help
, 0 },
2509 { "opie", opiehelp
, opie_calc
, 0 },
2511 { NULL
, NULL
, NULL
, 0 }
2514 static char crmodhelp
[] = "deprecated command -- use 'toggle crmod' instead";
2515 static char escapehelp
[] = "deprecated command -- use 'set escape' instead";
2517 static Command cmdtab2
[] = {
2518 { "help", 0, help
, 0 },
2519 { "escape", escapehelp
, setescape
, 0 },
2520 { "crmod", crmodhelp
, (int (*)(int, char **))togcrmod
, 0 },
2521 { NULL
, NULL
, NULL
, 0 }
2526 * Call routine with argc, argv set from args (terminated by 0).
2530 call(intrtn_t routine
, ...)
2536 va_start(ap
, routine
);
2537 while ((args
[argno
++] = va_arg(ap
, char *)) != NULL
);
2539 return (*routine
)(argno
-1, args
);
2548 if ((cm
= (Command
*) genget(name
, (char **) cmdtab
, sizeof(Command
))))
2550 return (Command
*) genget(name
, (char **) cmdtab2
, sizeof(Command
));
2554 command(int top
, const char *tbuf
, int cnt
)
2562 (void) signal(SIGINT
, SIG_DFL
);
2563 (void) signal(SIGQUIT
, SIG_DFL
);
2566 if (rlogin
== _POSIX_VDISABLE
)
2567 printf("%s> ", prompt
);
2571 while (cnt
> 0 && (*cp
++ = *tbuf
++) != '\n')
2574 if (cp
== line
|| *--cp
!= '\n' || cp
== line
)
2577 if (rlogin
== _POSIX_VDISABLE
)
2578 printf("%s\n", line
);
2581 if (rlogin
!= _POSIX_VDISABLE
)
2582 printf("%s> ", prompt
);
2583 if (fgets(line
, sizeof(line
), stdin
) == NULL
) {
2584 if (feof(stdin
) || ferror(stdin
)) {
2594 if (margv
[0] == NULL
) {
2597 c
= getcmd(margv
[0]);
2598 if (Ambiguous((void *)c
)) {
2599 printf("?Ambiguous command\n");
2603 printf("?Invalid command\n");
2606 if (c
->needconnect
&& !connected
) {
2607 printf("?Need to be connected first.\n");
2610 if ((*c
->handler
)(margc
, margv
)) {
2616 longjmp(toplevel
, 1);
2627 help(int argc
, char *argv
[])
2632 printf("Commands may be abbreviated. Commands are:\n\n");
2633 for (c
= cmdtab
; c
->name
; c
++)
2635 printf("%-*s\t%s\n", (int)HELPINDENT
, c
->name
,
2640 else while (--argc
> 0) {
2644 if (Ambiguous((void *)c
))
2645 printf("?Ambiguous help command %s\n", arg
);
2647 printf("?Invalid help command %s\n", arg
);
2649 printf("%s\n", c
->help
);
2654 static char *rcname
= NULL
;
2655 static char rcbuf
[128];
2658 cmdrc(const char *m1
, const char *m2
)
2663 int l1
= strlen(m1
);
2664 int l2
= strlen(m2
);
2665 char m1save
[MAXHOSTNAMELEN
+ 1];
2666 char temp
[sizeof(line
)];
2671 strlcpy(m1save
, m1
, sizeof(m1save
));
2674 if (rcname
== NULL
) {
2675 rcname
= getenv("HOME");
2676 if (rcname
&& (strlen(rcname
) + 10) < sizeof(rcbuf
))
2677 strcpy(rcbuf
, rcname
);
2680 strcat(rcbuf
, "/.telnetrc");
2684 if ((rcfile
= fopen(rcname
, "r")) == NULL
) {
2689 if (fgets(line
, sizeof(line
), rcfile
) == NULL
)
2696 if (!isspace(line
[0]))
2699 if (gotmachine
== 0) {
2700 if (isspace(line
[0]))
2702 if (strncasecmp(line
, m1
, l1
) == 0) {
2703 strncpy(temp
, &line
[l1
], sizeof(line
) - l1
);
2704 strncpy(line
, temp
, sizeof(line
) - l1
);
2705 } else if (strncasecmp(line
, m2
, l2
) == 0) {
2706 strncpy(temp
, &line
[l2
], sizeof(line
) - l2
);
2707 strncpy(line
, temp
, sizeof(line
) - l2
);
2708 } else if (strncasecmp(line
, "DEFAULT", 7) == 0) {
2709 strncpy(temp
, &line
[7], sizeof(line
) - 7);
2710 strncpy(line
, temp
, sizeof(line
) - 7);
2713 if (line
[0] != ' ' && line
[0] != '\t' && line
[0] != '\n')
2718 if (margv
[0] == NULL
)
2720 c
= getcmd(margv
[0]);
2721 if (Ambiguous((void *)c
)) {
2722 printf("?Ambiguous command: %s\n", margv
[0]);
2726 printf("?Invalid command: %s\n", margv
[0]);
2730 * This should never happen...
2732 if (c
->needconnect
&& !connected
) {
2733 printf("?Need to be connected first for %s.\n", margv
[0]);
2736 (*c
->handler
)(margc
, margv
);
2742 * Source route is handed in as
2743 * [!]@hop1@hop2...[@|:]dst
2744 * If the leading ! is present, it is a
2745 * strict source route, otherwise it is
2746 * assmed to be a loose source route.
2748 * We fill in the source route option as
2749 * hop1,hop2,hop3...dest
2750 * and return a pointer to hop1, which will
2751 * be the address to connect() to.
2755 * res: ponter to addrinfo structure which contains sockaddr to
2756 * the host to connect to.
2758 * arg: pointer to route list to decipher
2760 * cpp: If *cpp is not equal to NULL, this is a
2761 * pointer to a pointer to a character array
2762 * that should be filled in with the option.
2764 * lenp: pointer to an integer that contains the
2765 * length of *cpp if *cpp != NULL.
2767 * protop: pointer to an integer that should be filled in with
2768 * appropriate protocol for setsockopt, as socket
2771 * optp: pointer to an integer that should be filled in with
2772 * appropriate option for setsockopt, as socket protocol
2777 * If the return value is 1, then all operations are
2778 * successful. If the
2779 * return value is -1, there was a syntax error in the
2780 * option, either unknown characters, or too many hosts.
2781 * If the return value is 0, one of the hostnames in the
2782 * path is unknown, and *cpp is set to point to the bad
2785 * *cpp: If *cpp was equal to NULL, it will be filled
2786 * in with a pointer to our static area that has
2787 * the option filled in. This will be 32bit aligned.
2789 * *lenp: This will be filled in with how long the option
2790 * pointed to by *cpp is.
2792 * *protop: This will be filled in with appropriate protocol for
2793 * setsockopt, as socket protocol family.
2795 * *optp: This will be filled in with appropriate option for
2796 * setsockopt, as socket protocol family.
2799 sourceroute(struct addrinfo
*ai
, char *arg
, char **cpp
, int *lenp
, int *protop
, int *optp
)
2801 static char buf
[1024 + ALIGNBYTES
]; /*XXX*/
2802 char *cp
, *cp2
, *lsrp
, *ep
;
2803 struct sockaddr_in
*_sin
;
2805 #ifdef COMPAT_RFC1883 /* XXX */
2806 struct cmsghdr
*cmsg
;
2809 struct addrinfo hints
, *res
;
2814 * Verify the arguments, and make sure we have
2815 * at least 7 bytes for the option.
2817 if (cpp
== NULL
|| lenp
== NULL
)
2820 switch (res
->ai_family
) {
2827 if (*lenp
< (int)CMSG_SPACE(sizeof(struct ip6_rthdr
) +
2828 sizeof(struct in6_addr
)))
2835 * Decide whether we have a buffer passed to us,
2836 * or if we need to use our own static buffer.
2842 *cpp
= lsrp
= (char *)ALIGN(buf
);
2849 if (ai
->ai_family
== AF_INET6
) {
2851 * RFC3542 has obsoleted IPV6_PKTOPTIONS socket option.
2853 #ifdef COMPAT_RFC1883 /* XXX */
2857 *protop
= IPPROTO_IPV6
;
2858 *optp
= IPV6_PKTOPTIONS
;
2861 #endif /* COMPAT_RFC1883 */
2866 * Next, decide whether we have a loose source
2867 * route or a strict source route, and fill in
2868 * the begining of the option.
2872 *lsrp
++ = IPOPT_SSRR
;
2874 *lsrp
++ = IPOPT_LSRR
;
2879 lsrp
++; /* skip over length, we'll fill it in later */
2881 *protop
= IPPROTO_IP
;
2886 memset(&hints
, 0, sizeof(hints
));
2887 hints
.ai_family
= ai
->ai_family
;
2888 hints
.ai_socktype
= SOCK_STREAM
;
2892 ai
->ai_family
!= AF_INET6
&&
2896 else for (cp2
= cp
; (c
= *cp2
); cp2
++) {
2901 } else if (c
== '@') {
2905 ai
->ai_family
!= AF_INET6
&&
2916 hints
.ai_flags
= AI_NUMERICHOST
;
2917 error
= getaddrinfo(cp
, NULL
, &hints
, &res
);
2918 if (error
== EAI_NODATA
) {
2920 error
= getaddrinfo(cp
, NULL
, &hints
, &res
);
2923 fprintf(stderr
, "%s: %s\n", cp
, gai_strerror(error
));
2924 if (error
== EAI_SYSTEM
)
2925 fprintf(stderr
, "%s: %s\n", cp
,
2931 if (res
->ai_family
== AF_INET6
) {
2936 _sin
= (struct sockaddr_in
*)res
->ai_addr
;
2937 memcpy(lsrp
, (char *)&_sin
->sin_addr
, 4);
2945 * Check to make sure there is space for next address
2948 #ifdef COMPAT_RFC1883 /* XXX */
2949 if (res
->ai_family
== AF_INET6
) {
2950 if (((char *)CMSG_DATA(cmsg
) +
2951 sizeof(struct ip6_rthdr
)) > ep
)
2954 #endif /* COMPAT_RFC1883 */
2961 if (res
->ai_family
== AF_INET6
) {
2962 #ifdef COMPAT_RFC1883 /* XXX */
2964 #endif /* COMPAT_RFC1883 */
2968 if ((*(*cpp
+IPOPT_OLEN
) = lsrp
- *cpp
) <= 7) {
2973 *lsrp
++ = IPOPT_NOP
; /* 32 bit word align it */
2974 *lenp
= lsrp
- *cpp
;