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 /* Use RFC 2292 constants in <netinet6/in6.h> */
43 #define __APPLE_USE_RFC_2292
45 #include <sys/param.h>
48 #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 #include <libiosexec.h>
87 #ifndef MAXHOSTNAMELEN
88 #define MAXHOSTNAMELEN 256
91 typedef int (*intrtn_t
)(int, char **);
94 extern int auth_togdebug(int);
97 extern int EncryptAutoEnc(int);
98 extern int EncryptAutoDec(int);
99 extern int EncryptDebug(int);
100 extern int EncryptVerbose(int);
101 #endif /* ENCRYPTION */
102 #if defined(IPPROTO_IP) && defined(IP_TOS)
104 #endif /* defined(IPPROTO_IP) && defined(IP_TOS) */
107 static char _hostname
[MAXHOSTNAMELEN
];
109 static int help(int, char **);
110 static int call(intrtn_t
, ...);
111 static void cmdrc(char *, char *);
113 static int switch_af(struct addrinfo
**);
115 static int togglehelp(void);
116 static int send_tncmd(void (*)(int, int), const char *, char *);
117 static int setmod(int);
118 static int clearmode(int);
119 static int modehelp(void);
121 static int sourceroute(struct addrinfo
*, char *, char **, int *, int *, int *);
125 const char *name
; /* command name */
126 const char *help
; /* help string (NULL for no help) */
127 int (*handler
)(int, char **); /* routine which executes command */
128 int needconnect
; /* Do we need to be connected to execute? */
131 static char line
[256];
132 static char saveline
[256];
134 static char *margv
[20];
137 #include <sys/wait.h>
138 #define PATH_OPIEKEY "/usr/bin/opiekey"
140 opie_calc(int argc
, char *argv
[])
145 printf("%s sequence challenge\n", argv
[0]);
151 execv(PATH_OPIEKEY
, argv
);
157 (void) wait(&status
);
158 if (WIFEXITED(status
))
159 return (WEXITSTATUS(status
));
173 if (*cp
== '!') { /* Special case shell escape */
174 strcpy(saveline
, line
); /* save for shell command */
175 *argp
++ = strdup("!"); /* No room in string to get this */
187 for (cp2
= cp
; c
!= '\0'; c
= *++cp
) {
195 if ((c
= *++cp
) == '\0')
197 } else if (c
== '"') {
200 } else if (c
== '\'') {
203 } else if (isspace(c
))
217 * Make a character string into a number.
219 * Todo: 1. Could take random integers (12, 0x12, 012, 0b1).
232 c
= b
| 0x40; /* DEL */
245 * Construct a control character sequence
246 * for a special character.
253 * The only way I could get the Sun 3.5 compiler
255 * if ((unsigned int)c >= 0x80)
256 * was to assign "c" to an unsigned int variable...
259 unsigned int uic
= (unsigned int)c
;
263 if (c
== (cc_t
)_POSIX_VDISABLE
) {
268 buf
[1] = ((c
>>6)&07) + '0';
269 buf
[2] = ((c
>>3)&07) + '0';
270 buf
[3] = (c
&07) + '0';
272 } else if (uic
>= 0x20) {
284 * The following are data structures and routines for
285 * the "send" command.
290 const char *name
; /* How user refers to it (case independent) */
291 const char *help
; /* Help information (0 ==> no help) */
292 int needconnect
; /* Need to be connected */
293 int narg
; /* Number of arguments */
294 int (*handler
)(char *, ...); /* Routine to perform (for special ops) */
295 int nbyte
; /* Number of bytes to send this command */
296 int what
; /* Character to be sent (<0 ==> special) */
304 send_dontcmd(char *),
305 send_willcmd(char *),
306 send_wontcmd(char *);
308 static struct sendlist Sendlist
[] = {
309 { "ao", "Send Telnet Abort output", 1, 0, NULL
, 2, AO
},
310 { "ayt", "Send Telnet 'Are You There'", 1, 0, NULL
, 2, AYT
},
311 { "brk", "Send Telnet Break", 1, 0, NULL
, 2, BREAK
},
312 { "break", NULL
, 1, 0, NULL
, 2, BREAK
},
313 { "ec", "Send Telnet Erase Character", 1, 0, NULL
, 2, EC
},
314 { "el", "Send Telnet Erase Line", 1, 0, NULL
, 2, EL
},
315 { "escape", "Send current escape character",1, 0, (int (*)(char *, ...))send_esc
, 1, 0 },
316 { "ga", "Send Telnet 'Go Ahead' sequence", 1, 0, NULL
, 2, GA
},
317 { "ip", "Send Telnet Interrupt Process",1, 0, NULL
, 2, IP
},
318 { "intp", NULL
, 1, 0, NULL
, 2, IP
},
319 { "interrupt", NULL
, 1, 0, NULL
, 2, IP
},
320 { "intr", NULL
, 1, 0, NULL
, 2, IP
},
321 { "nop", "Send Telnet 'No operation'", 1, 0, NULL
, 2, NOP
},
322 { "eor", "Send Telnet 'End of Record'", 1, 0, NULL
, 2, EOR
},
323 { "abort", "Send Telnet 'Abort Process'", 1, 0, NULL
, 2, ABORT
},
324 { "susp", "Send Telnet 'Suspend Process'",1, 0, NULL
, 2, SUSP
},
325 { "eof", "Send Telnet End of File Character", 1, 0, NULL
, 2, xEOF
},
326 { "synch", "Perform Telnet 'Synch operation'", 1, 0, (int (*)(char *, ...))dosynch
, 2, 0 },
327 { "getstatus", "Send request for STATUS", 1, 0, (int (*)(char *, ...))get_status
, 6, 0 },
328 { "?", "Display send options", 0, 0, (int (*)(char *, ...))send_help
, 0, 0 },
329 { "help", NULL
, 0, 0, (int (*)(char *, ...))send_help
, 0, 0 },
330 { "do", NULL
, 0, 1, (int (*)(char *, ...))send_docmd
, 3, 0 },
331 { "dont", NULL
, 0, 1, (int (*)(char *, ...))send_dontcmd
, 3, 0 },
332 { "will", NULL
, 0, 1, (int (*)(char *, ...))send_willcmd
, 3, 0 },
333 { "wont", NULL
, 0, 1, (int (*)(char *, ...))send_wontcmd
, 3, 0 },
334 { NULL
, NULL
, 0, 0, NULL
, 0, 0 }
337 #define GETSEND(name) ((struct sendlist *) genget(name, (char **) Sendlist, \
338 sizeof(struct sendlist)))
341 sendcmd(int argc
, char *argv
[])
343 int count
; /* how many bytes we are going to need to send */
345 struct sendlist
*s
; /* pointer to current command */
350 printf("need at least one argument for 'send' command\n");
351 printf("'send ?' for help\n");
355 * First, validate all the send arguments.
356 * In addition, we see how much space we are going to need, and
357 * whether or not we will be doing a "SYNCH" operation (which
358 * flushes the network queue).
361 for (i
= 1; i
< argc
; i
++) {
362 s
= GETSEND(argv
[i
]);
364 printf("Unknown send argument '%s'\n'send ?' for help.\n",
367 } else if (Ambiguous((void *)s
)) {
368 printf("Ambiguous send argument '%s'\n'send ?' for help.\n",
372 if (i
+ s
->narg
>= argc
) {
374 "Need %d argument%s to 'send %s' command. 'send %s ?' for help.\n",
375 s
->narg
, s
->narg
== 1 ? "" : "s", s
->name
, s
->name
);
379 if ((void *)s
->handler
== (void *)send_help
) {
385 needconnect
+= s
->needconnect
;
387 if (!connected
&& needconnect
) {
388 printf("?Need to be connected first.\n");
389 printf("'send ?' for help\n");
392 /* Now, do we have enough room? */
393 if (NETROOM() < count
) {
394 printf("There is not enough room in the buffer TO the network\n");
395 printf("to process your request. Nothing will be done.\n");
396 printf("('send synch' will throw away most data in the network\n");
397 printf("buffer, if this might help.)\n");
400 /* OK, they are all OK, now go through again and actually send */
402 for (i
= 1; i
< argc
; i
++) {
403 if ((s
= GETSEND(argv
[i
])) == 0) {
404 fprintf(stderr
, "Telnet 'send' error - argument disappeared!\n");
410 success
+= (*s
->handler
)((s
->narg
> 0) ? argv
[i
+1] : 0,
411 (s
->narg
> 1) ? argv
[i
+2] : 0);
414 NET2ADD(IAC
, s
->what
);
415 printoption("SENT", IAC
, s
->what
);
418 return (count
== success
);
429 send_docmd(char *name
)
431 return(send_tncmd(send_do
, "do", name
));
438 return(send_tncmd(send_dont
, "dont", name
));
442 send_willcmd(char *name
)
444 return(send_tncmd(send_will
, "will", name
));
448 send_wontcmd(char *name
)
450 return(send_tncmd(send_wont
, "wont", name
));
454 send_tncmd(void (*func
)(int, int), const char *cmd
, char *name
)
457 extern char *telopts
[];
460 if (isprefix(name
, "help") || isprefix(name
, "?")) {
463 printf("usage: send %s <value|option>\n", cmd
);
464 printf("\"value\" must be from 0 to 255\n");
465 printf("Valid options are:\n\t");
468 for (cpp
= telopts
; *cpp
; cpp
++) {
469 len
= strlen(*cpp
) + 3;
470 if (col
+ len
> 65) {
474 printf(" \"%s\"", *cpp
);
480 cpp
= (char **)genget(name
, telopts
, sizeof(char *));
481 if (Ambiguous(cpp
)) {
482 fprintf(stderr
,"'%s': ambiguous argument ('send %s ?' for help).\n",
491 while (*cp
>= '0' && *cp
<= '9') {
497 fprintf(stderr
, "'%s': unknown argument ('send %s ?' for help).\n",
500 } else if (val
< 0 || val
> 255) {
501 fprintf(stderr
, "'%s': bad value ('send %s ?' for help).\n",
507 printf("?Need to be connected first.\n");
517 struct sendlist
*s
; /* pointer to current command */
518 for (s
= Sendlist
; s
->name
; s
++) {
520 printf("%-15s %s\n", s
->name
, s
->help
);
526 * The following are the routines and data structures referred
527 * to by the arguments to the "toggle" command.
542 (SetSockOpt(net
, SOL_SOCKET
, SO_DEBUG
, telnet_debug
)) < 0) {
543 perror("setsockopt (SO_DEBUG)");
547 if (net
> 0 && SetSockOpt(net
, SOL_SOCKET
, SO_DEBUG
, 1) < 0)
548 perror("setsockopt (SO_DEBUG)");
550 printf("Cannot turn off socket debugging\n");
560 printf("Will send carriage returns as telnet <CR><LF>.\n");
562 printf("Will send carriage returns as telnet <CR><NUL>.\n");
572 donebinarytoggle
= 1;
577 if (my_want_state_is_will(TELOPT_BINARY
) &&
578 my_want_state_is_do(TELOPT_BINARY
)) {
580 } else if (my_want_state_is_wont(TELOPT_BINARY
) &&
581 my_want_state_is_dont(TELOPT_BINARY
)) {
584 val
= binmode
? 0 : 1;
588 if (my_want_state_is_will(TELOPT_BINARY
) &&
589 my_want_state_is_do(TELOPT_BINARY
)) {
590 printf("Already operating in binary mode with remote host.\n");
592 printf("Negotiating binary mode with remote host.\n");
596 if (my_want_state_is_wont(TELOPT_BINARY
) &&
597 my_want_state_is_dont(TELOPT_BINARY
)) {
598 printf("Already in network ascii mode with remote host.\n");
600 printf("Negotiating network ascii mode with remote host.\n");
610 donebinarytoggle
= 1;
613 val
= my_want_state_is_do(TELOPT_BINARY
) ? 0 : 1;
616 if (my_want_state_is_do(TELOPT_BINARY
)) {
617 printf("Already receiving in binary mode.\n");
619 printf("Negotiating binary mode on input.\n");
623 if (my_want_state_is_dont(TELOPT_BINARY
)) {
624 printf("Already receiving in network ascii mode.\n");
626 printf("Negotiating network ascii mode on input.\n");
636 donebinarytoggle
= 1;
639 val
= my_want_state_is_will(TELOPT_BINARY
) ? 0 : 1;
642 if (my_want_state_is_will(TELOPT_BINARY
)) {
643 printf("Already transmitting in binary mode.\n");
645 printf("Negotiating binary mode on output.\n");
649 if (my_want_state_is_wont(TELOPT_BINARY
)) {
650 printf("Already transmitting in network ascii mode.\n");
652 printf("Negotiating network ascii mode on output.\n");
660 const char *name
; /* name of toggle */
661 const char *help
; /* help message */
662 int (*handler
)(int); /* routine to do actual setting */
664 const char *actionexplanation
;
667 static struct togglelist Togglelist
[] = {
669 "flushing of output when sending interrupt characters",
672 "flush output when sending interrupt characters" },
674 "automatic sending of interrupt characters in urgent mode",
677 "send interrupt characters in urgent mode" },
678 #ifdef AUTHENTICATION
680 "automatic sending of login and/or authentication info",
683 "send login name and/or authentication information" },
685 "Toggle authentication debugging",
688 "print authentication debugging information" },
692 "automatic encryption of data stream",
695 "automatically encrypt output" },
697 "automatic decryption of data stream",
700 "automatically decrypt input" },
702 "Toggle verbose encryption output",
705 "print verbose encryption output" },
707 "Toggle encryption debugging",
710 "print encryption debugging information" },
711 #endif /* ENCRYPTION */
713 "don't read ~/.telnetrc file",
716 "skip reading of ~/.telnetrc file" },
718 "sending and receiving of binary data",
723 "receiving of binary data",
728 "sending of binary data",
733 "sending carriage returns as telnet <CR><LF>",
734 (int (*)(int))togcrlf
,
738 "mapping of received carriage returns",
741 "map carriage return on output" },
743 "local recognition of certain control characters",
744 (int (*)(int))lclchars
,
746 "recognize certain control characters" },
747 { " ", "", NULL
, NULL
, NULL
}, /* empty line */
750 (int (*)(int))togdebug
,
752 "turn on socket level debugging" },
754 "printing of hexadecimal network data (debugging)",
757 "print hexadecimal representation of network traffic" },
759 "output of \"netdata\" to user readable format (debugging)",
762 "print user readable output for \"netdata\"" },
764 "viewing of options processing (debugging)",
767 "show option processing" },
769 "(debugging) toggle printing of hexadecimal terminal data",
772 "print hexadecimal representation of terminal traffic" },
775 (int (*)(int))togglehelp
,
778 { NULL
, NULL
, NULL
, NULL
, NULL
},
781 (int (*)(int))togglehelp
,
784 { NULL
, NULL
, NULL
, NULL
, NULL
}
790 struct togglelist
*c
;
792 for (c
= Togglelist
; c
->name
; c
++) {
795 printf("%-15s toggle %s\n", c
->name
, c
->help
);
801 printf("%-15s %s\n", "?", "display help information");
806 settogglehelp(int set
)
808 struct togglelist
*c
;
810 for (c
= Togglelist
; c
->name
; c
++) {
813 printf("%-15s %s %s\n", c
->name
, set
? "enable" : "disable",
821 #define GETTOGGLE(name) (struct togglelist *) \
822 genget(name, (char **) Togglelist, sizeof(struct togglelist))
825 toggle(int argc
, char *argv
[])
829 struct togglelist
*c
;
833 "Need an argument to 'toggle' command. 'toggle ?' for help.\n");
841 if (Ambiguous((void *)c
)) {
842 fprintf(stderr
, "'%s': ambiguous argument ('toggle ?' for help).\n",
846 fprintf(stderr
, "'%s': unknown argument ('toggle ?' for help).\n",
851 *c
->variable
= !*c
->variable
; /* invert it */
852 if (c
->actionexplanation
) {
853 printf("%s %s.\n", *c
->variable
? "Will" : "Won't",
854 c
->actionexplanation
);
858 retval
&= (*c
->handler
)(-1);
866 * The following perform the "set" command.
870 struct termio new_tc
= { 0, 0, 0, 0, {}, 0, 0 };
874 const char *name
; /* name */
875 const char *help
; /* help information */
876 void (*handler
)(char *);
877 cc_t
*charp
; /* where it is located at */
880 static struct setlist Setlist
[] = {
881 #ifdef KLUDGELINEMODE
882 { "echo", "character to toggle local echoing on/off", NULL
, &echoc
},
884 { "escape", "character to escape back to telnet command mode", NULL
, &escape
},
885 { "rlogin", "rlogin escape character", 0, &rlogin
},
886 { "tracefile", "file to write trace information to", SetNetTrace
, (cc_t
*)NetTraceFile
},
887 { " ", "", NULL
, NULL
},
888 { " ", "The following need 'localchars' to be toggled true", NULL
, NULL
},
889 { "flushoutput", "character to cause an Abort Output", NULL
, termFlushCharp
},
890 { "interrupt", "character to cause an Interrupt Process", NULL
, termIntCharp
},
891 { "quit", "character to cause an Abort process", NULL
, termQuitCharp
},
892 { "eof", "character to cause an EOF ", NULL
, termEofCharp
},
893 { " ", "", NULL
, NULL
},
894 { " ", "The following are for local editing in linemode", NULL
, NULL
},
895 { "erase", "character to use to erase a character", NULL
, termEraseCharp
},
896 { "kill", "character to use to erase a line", NULL
, termKillCharp
},
897 { "lnext", "character to use for literal next", NULL
, termLiteralNextCharp
},
898 { "susp", "character to cause a Suspend Process", NULL
, termSuspCharp
},
899 { "reprint", "character to use for line reprint", NULL
, termRprntCharp
},
900 { "worderase", "character to use to erase a word", NULL
, termWerasCharp
},
901 { "start", "character to use for XON", NULL
, termStartCharp
},
902 { "stop", "character to use for XOFF", NULL
, termStopCharp
},
903 { "forw1", "alternate end of line character", NULL
, termForw1Charp
},
904 { "forw2", "alternate end of line character", NULL
, termForw2Charp
},
905 { "ayt", "alternate AYT character", NULL
, termAytCharp
},
906 { NULL
, NULL
, NULL
, NULL
}
909 static struct setlist
*
912 return (struct setlist
*)
913 genget(name
, (char **) Setlist
, sizeof(struct setlist
));
917 set_escape_char(char *s
)
919 if (rlogin
!= _POSIX_VDISABLE
) {
920 rlogin
= (s
&& *s
) ? special(s
) : _POSIX_VDISABLE
;
921 printf("Telnet rlogin escape character is '%s'.\n",
924 escape
= (s
&& *s
) ? special(s
) : _POSIX_VDISABLE
;
925 printf("Telnet escape character is '%s'.\n", control(escape
));
930 setcmd(int argc
, char *argv
[])
934 struct togglelist
*c
;
936 if (argc
< 2 || argc
> 3) {
937 printf("Format is 'set Name Value'\n'set ?' for help.\n");
940 if ((argc
== 2) && (isprefix(argv
[1], "?") || isprefix(argv
[1], "help"))) {
941 for (ct
= Setlist
; ct
->name
; ct
++)
942 printf("%-15s %s\n", ct
->name
, ct
->help
);
945 printf("%-15s %s\n", "?", "display help information");
949 ct
= getset(argv
[1]);
951 c
= GETTOGGLE(argv
[1]);
953 fprintf(stderr
, "'%s': unknown argument ('set ?' for help).\n",
956 } else if (Ambiguous((void *)c
)) {
957 fprintf(stderr
, "'%s': ambiguous argument ('set ?' for help).\n",
962 if ((argc
== 2) || (strcmp("on", argv
[2]) == 0))
964 else if (strcmp("off", argv
[2]) == 0)
967 printf("Format is 'set togglename [on|off]'\n'set ?' for help.\n");
970 if (c
->actionexplanation
) {
971 printf("%s %s.\n", *c
->variable
? "Will" : "Won't",
972 c
->actionexplanation
);
977 } else if (argc
!= 3) {
978 printf("Format is 'set Name Value'\n'set ?' for help.\n");
980 } else if (Ambiguous((void *)ct
)) {
981 fprintf(stderr
, "'%s': ambiguous argument ('set ?' for help).\n",
984 } else if (ct
->handler
) {
985 (*ct
->handler
)(argv
[2]);
986 printf("%s set to \"%s\".\n", ct
->name
, (char *)ct
->charp
);
988 if (strcmp("off", argv
[2])) {
989 value
= special(argv
[2]);
991 value
= _POSIX_VDISABLE
;
993 *(ct
->charp
) = (cc_t
)value
;
994 printf("%s character is '%s'.\n", ct
->name
, control(*(ct
->charp
)));
1001 unsetcmd(int argc
, char *argv
[])
1004 struct togglelist
*c
;
1009 "Need an argument to 'unset' command. 'unset ?' for help.\n");
1012 if (isprefix(argv
[1], "?") || isprefix(argv
[1], "help")) {
1013 for (ct
= Setlist
; ct
->name
; ct
++)
1014 printf("%-15s %s\n", ct
->name
, ct
->help
);
1017 printf("%-15s %s\n", "?", "display help information");
1027 c
= GETTOGGLE(name
);
1029 fprintf(stderr
, "'%s': unknown argument ('unset ?' for help).\n",
1032 } else if (Ambiguous((void *)c
)) {
1033 fprintf(stderr
, "'%s': ambiguous argument ('unset ?' for help).\n",
1039 if (c
->actionexplanation
) {
1040 printf("%s %s.\n", *c
->variable
? "Will" : "Won't",
1041 c
->actionexplanation
);
1046 } else if (Ambiguous((void *)ct
)) {
1047 fprintf(stderr
, "'%s': ambiguous argument ('unset ?' for help).\n",
1050 } else if (ct
->handler
) {
1052 printf("%s reset to \"%s\".\n", ct
->name
, (char *)ct
->charp
);
1054 *(ct
->charp
) = _POSIX_VDISABLE
;
1055 printf("%s character is '%s'.\n", ct
->name
, control(*(ct
->charp
)));
1062 * The following are the data structures and routines for the
1065 #ifdef KLUDGELINEMODE
1066 extern int kludgelinemode
;
1072 send_wont(TELOPT_LINEMODE
, 1);
1073 send_dont(TELOPT_SGA
, 1);
1074 send_dont(TELOPT_ECHO
, 1);
1082 #ifdef KLUDGELINEMODE
1084 send_dont(TELOPT_SGA
, 1);
1086 send_will(TELOPT_LINEMODE
, 1);
1087 send_dont(TELOPT_ECHO
, 1);
1094 #ifdef KLUDGELINEMODE
1096 send_do(TELOPT_SGA
, 1);
1099 send_wont(TELOPT_LINEMODE
, 1);
1100 send_do(TELOPT_ECHO
, 1);
1105 dolmmode(int bit
, int on
)
1108 extern int linemode
;
1110 if (my_want_state_is_wont(TELOPT_LINEMODE
)) {
1111 printf("?Need to have LINEMODE option enabled first.\n");
1112 printf("'mode ?' for help.\n");
1117 c
= (linemode
| bit
);
1119 c
= (linemode
& ~bit
);
1127 return dolmmode(bit
, 1);
1133 return dolmmode(bit
, 0);
1137 const char *name
; /* command name */
1138 const char *help
; /* help string */
1139 int (*handler
)(int);/* routine which executes command */
1140 int needconnect
; /* Do we need to be connected to execute? */
1144 static struct modelist ModeList
[] = {
1145 { "character", "Disable LINEMODE option", (int (*)(int))docharmode
, 1, 0 },
1146 #ifdef KLUDGELINEMODE
1147 { "", "(or disable obsolete line-by-line mode)", NULL
, 0, 0 },
1149 { "line", "Enable LINEMODE option", (int (*)(int))dolinemode
, 1, 0 },
1150 #ifdef KLUDGELINEMODE
1151 { "", "(or enable obsolete line-by-line mode)", NULL
, 0, 0 },
1153 { "", "", NULL
, 0, 0 },
1154 { "", "These require the LINEMODE option to be enabled", NULL
, 0, 0 },
1155 { "isig", "Enable signal trapping", setmod
, 1, MODE_TRAPSIG
},
1156 { "+isig", 0, setmod
, 1, MODE_TRAPSIG
},
1157 { "-isig", "Disable signal trapping", clearmode
, 1, MODE_TRAPSIG
},
1158 { "edit", "Enable character editing", setmod
, 1, MODE_EDIT
},
1159 { "+edit", 0, setmod
, 1, MODE_EDIT
},
1160 { "-edit", "Disable character editing", clearmode
, 1, MODE_EDIT
},
1161 { "softtabs", "Enable tab expansion", setmod
, 1, MODE_SOFT_TAB
},
1162 { "+softtabs", 0, setmod
, 1, MODE_SOFT_TAB
},
1163 { "-softtabs", "Disable character editing", clearmode
, 1, MODE_SOFT_TAB
},
1164 { "litecho", "Enable literal character echo", setmod
, 1, MODE_LIT_ECHO
},
1165 { "+litecho", 0, setmod
, 1, MODE_LIT_ECHO
},
1166 { "-litecho", "Disable literal character echo", clearmode
, 1, MODE_LIT_ECHO
},
1167 { "help", 0, (int (*)(int))modehelp
, 0, 0 },
1168 #ifdef KLUDGELINEMODE
1169 { "kludgeline", 0, (int (*)(int))dokludgemode
, 1, 0 },
1171 { "", "", NULL
, 0, 0 },
1172 { "?", "Print help information", (int (*)(int))modehelp
, 0, 0 },
1173 { NULL
, NULL
, NULL
, 0, 0 },
1180 struct modelist
*mt
;
1182 printf("format is: 'mode Mode', where 'Mode' is one of:\n\n");
1183 for (mt
= ModeList
; mt
->name
; mt
++) {
1186 printf("%-15s %s\n", mt
->name
, mt
->help
);
1194 #define GETMODECMD(name) (struct modelist *) \
1195 genget(name, (char **) ModeList, sizeof(struct modelist))
1198 modecmd(int argc
, char *argv
[])
1200 struct modelist
*mt
;
1203 printf("'mode' command requires an argument\n");
1204 printf("'mode ?' for help.\n");
1205 } else if ((mt
= GETMODECMD(argv
[1])) == 0) {
1206 fprintf(stderr
, "Unknown mode '%s' ('mode ?' for help).\n", argv
[1]);
1207 } else if (Ambiguous((void *)mt
)) {
1208 fprintf(stderr
, "Ambiguous mode '%s' ('mode ?' for help).\n", argv
[1]);
1209 } else if (mt
->needconnect
&& !connected
) {
1210 printf("?Need to be connected first.\n");
1211 printf("'mode ?' for help.\n");
1212 } else if (mt
->handler
) {
1213 return (*mt
->handler
)(mt
->arg1
);
1219 * The following data structures and routines implement the
1220 * "display" command.
1224 display(int argc
, char *argv
[])
1226 struct togglelist
*tl
;
1229 #define dotog(tl) if (tl->variable && tl->actionexplanation) { \
1230 if (*tl->variable) { \
1235 printf(" %s.\n", tl->actionexplanation); \
1238 #define doset(sl) if (sl->name && *sl->name != ' ') { \
1239 if (sl->handler == 0) \
1240 printf("%-15s [%s]\n", sl->name, control(*sl->charp)); \
1242 printf("%-15s \"%s\"\n", sl->name, (char *)sl->charp); \
1246 for (tl
= Togglelist
; tl
->name
; tl
++) {
1250 for (sl
= Setlist
; sl
->name
; sl
++) {
1256 for (i
= 1; i
< argc
; i
++) {
1257 sl
= getset(argv
[i
]);
1258 tl
= GETTOGGLE(argv
[i
]);
1259 if (Ambiguous((void *)sl
) || Ambiguous((void *)tl
)) {
1260 printf("?Ambiguous argument '%s'.\n", argv
[i
]);
1262 } else if (!sl
&& !tl
) {
1263 printf("?Unknown argument '%s'.\n", argv
[i
]);
1275 /*@*/optionstatus();
1278 #endif /* ENCRYPTION */
1285 * The following are the data structures, and many of the routines,
1286 * relating to command processing.
1290 * Set the escape character.
1293 setescape(int argc
, char *argv
[])
1299 "Deprecated usage - please use 'set escape%s%s' in the future.\n",
1300 (argc
> 2)? " ":"", (argc
> 2)? argv
[1]: "");
1304 printf("new escape character: ");
1305 (void) fgets(buf
, sizeof(buf
), stdin
);
1310 (void) fflush(stdout
);
1318 printf("Deprecated usage - please use 'toggle crmod' in the future.\n");
1319 printf("%s map carriage return on output.\n", crmod
? "Will" : "Won't");
1320 (void) fflush(stdout
);
1330 long oldrows
, oldcols
, newrows
, newcols
, err_
;
1332 err_
= (TerminalWindowSize(&oldrows
, &oldcols
) == 0) ? 1 : 0;
1333 (void) kill(0, SIGTSTP
);
1335 * If we didn't get the window size before the SUSPEND, but we
1336 * can get them now (?), then send the NAWS to make sure that
1337 * we are set up for the right window size.
1339 if (TerminalWindowSize(&newrows
, &newcols
) && connected
&&
1340 (err_
|| ((oldrows
!= newrows
) || (oldcols
!= newcols
)))) {
1344 /* reget parameters in case they were changed */
1345 TerminalSaveState();
1348 printf("Suspend is not supported. Try the '!' command instead\n");
1354 shell(int argc
, char *argv
[] __unused
)
1356 long oldrows
, oldcols
, newrows
, newcols
, err_
;
1360 err_
= (TerminalWindowSize(&oldrows
, &oldcols
) == 0) ? 1 : 0;
1363 perror("Fork failed\n");
1369 * Fire up the shell in the child.
1371 const char *shellp
, *shellname
;
1373 shellp
= getenv("SHELL");
1376 if ((shellname
= strrchr(shellp
, '/')) == 0)
1381 execl(shellp
, shellname
, "-c", &saveline
[1], (char *)0);
1383 execl(shellp
, shellname
, (char *)0);
1388 (void)wait((int *)0); /* Wait for the shell to complete */
1390 if (TerminalWindowSize(&newrows
, &newcols
) && connected
&&
1391 (err_
|| ((oldrows
!= newrows
) || (oldcols
!= newcols
)))) {
1400 bye(int argc
, char *argv
[])
1402 extern int resettermname
;
1405 (void) shutdown(net
, 2);
1406 printf("Connection closed.\n");
1407 (void) NetClose(net
);
1410 #ifdef AUTHENTICATION
1412 auth_encrypt_connect(connected
);
1418 if ((argc
!= 2) || (strcmp(argv
[1], "fromquit") != 0)) {
1419 longjmp(toplevel
, 1);
1422 return 1; /* Keep lint, etc., happy */
1428 (void) call(bye
, "bye", "fromquit", 0);
1435 send_do(TELOPT_LOGOUT
, 1);
1448 void (*handler
)(int);
1452 static void slc_help(void);
1454 struct slclist SlcList
[] = {
1455 { "export", "Use local special character definitions",
1456 (void (*)(int))slc_mode_export
, 0 },
1457 { "import", "Use remote special character definitions",
1458 slc_mode_import
, 1 },
1459 { "check", "Verify remote special character definitions",
1460 slc_mode_import
, 0 },
1461 { "help", NULL
, (void (*)(int))slc_help
, 0 },
1462 { "?", "Print help information", (void (*)(int))slc_help
, 0 },
1463 { NULL
, NULL
, NULL
, 0 },
1471 for (c
= SlcList
; c
->name
; c
++) {
1474 printf("%-15s %s\n", c
->name
, c
->help
);
1481 static struct slclist
*
1484 return (struct slclist
*)
1485 genget(name
, (char **) SlcList
, sizeof(struct slclist
));
1489 slccmd(int argc
, char *argv
[])
1495 "Need an argument to 'slc' command. 'slc ?' for help.\n");
1498 c
= getslc(argv
[1]);
1500 fprintf(stderr
, "'%s': unknown argument ('slc ?' for help).\n",
1504 if (Ambiguous((void *)c
)) {
1505 fprintf(stderr
, "'%s': ambiguous argument ('slc ?' for help).\n",
1509 (*c
->handler
)(c
->arg
);
1515 * The ENVIRON command.
1521 void (*handler
)(unsigned char *, unsigned char *);
1525 extern struct env_lst
*
1526 env_define(const unsigned char *, unsigned char *);
1528 env_undefine(unsigned char *),
1529 env_export(const unsigned char *),
1530 env_unexport(const unsigned char *),
1531 env_send(unsigned char *),
1532 #if defined(OLD_ENVIRON) && defined(ENV_HACK)
1533 env_varval(unsigned char *),
1539 struct envlist EnvList
[] = {
1540 { "define", "Define an environment variable",
1541 (void (*)(unsigned char *, unsigned char *))env_define
, 2 },
1542 { "undefine", "Undefine an environment variable",
1543 (void (*)(unsigned char *, unsigned char *))env_undefine
, 1 },
1544 { "export", "Mark an environment variable for automatic export",
1545 (void (*)(unsigned char *, unsigned char *))env_export
, 1 },
1546 { "unexport", "Don't mark an environment variable for automatic export",
1547 (void (*)(unsigned char *, unsigned char *))env_unexport
, 1 },
1548 { "send", "Send an environment variable", (void (*)(unsigned char *, unsigned char *))env_send
, 1 },
1549 { "list", "List the current environment variables",
1550 (void (*)(unsigned char *, unsigned char *))env_list
, 0 },
1551 #if defined(OLD_ENVIRON) && defined(ENV_HACK)
1552 { "varval", "Reverse VAR and VALUE (auto, right, wrong, status)",
1553 (void (*)(unsigned char *, unsigned char *))env_varval
, 1 },
1555 { "help", NULL
, (void (*)(unsigned char *, unsigned char *))env_help
, 0 },
1556 { "?", "Print help information", (void (*)(unsigned char *, unsigned char *))env_help
, 0 },
1557 { NULL
, NULL
, NULL
, 0 },
1565 for (c
= EnvList
; c
->name
; c
++) {
1568 printf("%-15s %s\n", c
->name
, c
->help
);
1575 static struct envlist
*
1576 getenvcmd(char *name
)
1578 return (struct envlist
*)
1579 genget(name
, (char **) EnvList
, sizeof(struct envlist
));
1583 env_cmd(int argc
, char *argv
[])
1589 "Need an argument to 'environ' command. 'environ ?' for help.\n");
1592 c
= getenvcmd(argv
[1]);
1594 fprintf(stderr
, "'%s': unknown argument ('environ ?' for help).\n",
1598 if (Ambiguous((void *)c
)) {
1599 fprintf(stderr
, "'%s': ambiguous argument ('environ ?' for help).\n",
1603 if (c
->narg
+ 2 != argc
&& strcasecmp(argv
[1],"define")==0 && c
->narg
+ 1 != argc
) {
1605 "Need %s%d argument%s to 'environ %s' command. 'environ ?' for help.\n",
1606 c
->narg
< argc
+ 2 ? "only " : "",
1607 c
->narg
, c
->narg
== 1 ? "" : "s", c
->name
);
1610 (*c
->handler
)((unsigned char *)argv
[2], (unsigned char *)argv
[3]);
1615 struct env_lst
*next
; /* pointer to next structure */
1616 struct env_lst
*prev
; /* pointer to previous structure */
1617 unsigned char *var
; /* pointer to variable name */
1618 unsigned char *value
; /* pointer to variable value */
1619 int export
; /* 1 -> export with default list of variables */
1620 int welldefined
; /* A well defined variable */
1623 struct env_lst envlisthead
;
1625 static struct env_lst
*
1626 env_find(const unsigned char *var
)
1630 for (ep
= envlisthead
.next
; ep
; ep
= ep
->next
) {
1631 if (strcmp((const char *)ep
->var
, (const char *)var
) == 0)
1644 const char *safe_vars
[]=
1645 {"USER", "PRINTER", "DISPLAY", "TERM", "COLUMNS", "LINES"};
1647 for(i
=0;i
<sizeof(safe_vars
)/sizeof(const char *);i
++) {
1648 if((ev
=getenv(safe_vars
[i
]))) {
1649 ep
=env_define((unsigned char *)safe_vars
[i
],(unsigned char *)ev
);
1655 * Special case for DISPLAY variable. If it is ":0.0" or
1656 * "unix:0.0", we have to get rid of "unix" and insert our
1659 if ((ep
= env_find((const unsigned char *)"DISPLAY
"))
1660 && ((*ep->value == ':')
1661 || (strncmp((char *)ep->value, "unix
:", 5) == 0))) {
1663 char *cp, *cp2 = strchr((char *)ep->value, ':');
1665 gethostname(hbuf, 256);
1667 cp = (char *)malloc(strlen(hbuf) + strlen(cp2) + 1);
1668 sprintf((char *)cp, "%s%s
", hbuf, cp2);
1670 ep->value = (unsigned char *)cp;
1673 * If USER is not defined, but LOGNAME is, then add
1674 * USER with the value from LOGNAME. By default, we
1675 * don't export the USER variable.
1677 if ((env_find((const unsigned char *)"USER
") == NULL) && (ep = env_find((const unsigned char *)"LOGNAME
"))) {
1678 env_define((const unsigned char *)"USER
", ep->value);
1679 env_unexport((const unsigned char *)"USER
");
1681 env_export((const unsigned char *)"DISPLAY
");
1682 env_export((const unsigned char *)"PRINTER
");
1686 env_define(const unsigned char *var, unsigned char *value)
1691 if ((ep = env_find(var))) {
1697 ep = (struct env_lst *)malloc(sizeof(struct env_lst));
1698 ep->next = envlisthead.next;
1699 envlisthead.next = ep;
1700 ep->prev = &envlisthead;
1702 ep->next->prev = ep;
1705 ep->welldefined = opt_welldefined((const char *)var);
1707 ep->var = (unsigned char *)strdup((const char *)var);
1710 ep->value = (unsigned char *)strdup((const char *)value);
1711 else if((ev=getenv((const char *)var)))
1712 ep->value = (unsigned char *)strdup(ev);
1713 else ep->value = (unsigned char *)strdup("");
1718 env_undefine(unsigned char *var)
1722 if ((ep = env_find(var))) {
1723 ep->prev->next = ep->next;
1725 ep->next->prev = ep->prev;
1735 env_export(const unsigned char *var)
1739 if ((ep = env_find(var)))
1744 env_unexport(const unsigned char *var)
1748 if ((ep = env_find(var)))
1753 env_send(unsigned char *var)
1757 if (my_state_is_wont(TELOPT_NEW_ENVIRON)
1759 && my_state_is_wont(TELOPT_OLD_ENVIRON)
1763 "Cannot send
'%s': Telnet ENVIRON option
not enabled
\n",
1769 fprintf(stderr, "Cannot send
'%s': variable
not defined
\n",
1773 env_opt_start_info();
1774 env_opt_add(ep->var);
1783 for (ep = envlisthead.next; ep; ep = ep->next) {
1784 printf("%c
%-20s %s
\n", ep->export ? '*' : ' ',
1785 ep->var, ep->value);
1790 env_default(int init, int welldefined)
1792 static struct env_lst *nep = NULL;
1799 while ((nep = nep->next)) {
1800 if (nep->export && (nep->welldefined == welldefined))
1808 env_getvalue(const unsigned char *var)
1812 if ((ep = env_find(var)))
1817 #if defined(OLD_ENVIRON) && defined(ENV_HACK)
1819 env_varval(unsigned char *what)
1821 extern int old_env_var, old_env_value, env_auto;
1822 int len = strlen((char *)what);
1827 if (strncasecmp((char *)what, "status
", len) == 0) {
1829 printf("%s%s
", "VAR
and VALUE are
/will be
",
1830 "determined automatically
\n");
1831 if (old_env_var == OLD_ENV_VAR)
1832 printf("VAR
and VALUE set to correct definitions
\n");
1834 printf("VAR
and VALUE definitions are reversed
\n");
1835 } else if (strncasecmp((char *)what, "auto", len) == 0) {
1837 old_env_var = OLD_ENV_VALUE;
1838 old_env_value = OLD_ENV_VAR;
1839 } else if (strncasecmp((char *)what, "right
", len) == 0) {
1841 old_env_var = OLD_ENV_VAR;
1842 old_env_value = OLD_ENV_VALUE;
1843 } else if (strncasecmp((char *)what, "wrong
", len) == 0) {
1845 old_env_var = OLD_ENV_VALUE;
1846 old_env_value = OLD_ENV_VAR;
1849 printf("Unknown
\"varval
\" command
. (\"auto\", \"right
\", \"wrong
\", \"status
\")\n");
1854 #ifdef AUTHENTICATION
1856 * The AUTHENTICATE command.
1862 int (*handler)(char *);
1867 auth_enable(char *),
1868 auth_disable(char *),
1873 struct authlist AuthList[] = {
1874 { "status
", "Display current status of authentication information
",
1875 (int (*)(char *))auth_status, 0 },
1876 { "disable
", "Disable an authentication
type ('auth disable ?' for more)"
,
1878 { "enable", "Enable an authentication type ('auth enable ?' for more)",
1880 { "help", NULL
, (int (*)(char *))auth_help
, 0 },
1881 { "?", "Print help information", (int (*)(char *))auth_help
, 0 },
1882 { NULL
, NULL
, NULL
, 0 },
1890 for (c
= AuthList
; c
->name
; c
++) {
1893 printf("%-15s %s\n", c
->name
, c
->help
);
1902 auth_cmd(int argc
, char *argv
[])
1908 "Need an argument to 'auth' command. 'auth ?' for help.\n");
1912 c
= (struct authlist
*)
1913 genget(argv
[1], (char **) AuthList
, sizeof(struct authlist
));
1915 fprintf(stderr
, "'%s': unknown argument ('auth ?' for help).\n",
1919 if (Ambiguous((void *)c
)) {
1920 fprintf(stderr
, "'%s': ambiguous argument ('auth ?' for help).\n",
1924 if (c
->narg
+ 2 != argc
) {
1926 "Need %s%d argument%s to 'auth %s' command. 'auth ?' for help.\n",
1927 c
->narg
< argc
+ 2 ? "only " : "",
1928 c
->narg
, c
->narg
== 1 ? "" : "s", c
->name
);
1931 return((*c
->handler
)(argv
[2]));
1937 * The ENCRYPT command.
1940 struct encryptlist
{
1943 int (*handler
)(char *, char *);
1950 EncryptEnable(char *, char *),
1951 EncryptDisable(char *, char *),
1952 EncryptType(char *, char *),
1953 EncryptStart(char *),
1954 EncryptStartInput(void),
1955 EncryptStartOutput(void),
1956 EncryptStop(char *),
1957 EncryptStopInput(void),
1958 EncryptStopOutput(void),
1959 EncryptStatus(void);
1963 struct encryptlist EncryptList
[] = {
1964 { "enable", "Enable encryption. ('encrypt enable ?' for more)",
1965 EncryptEnable
, 1, 1, 2 },
1966 { "disable", "Disable encryption. ('encrypt enable ?' for more)",
1967 EncryptDisable
, 0, 1, 2 },
1968 { "type", "Set encryption type. ('encrypt type ?' for more)",
1969 EncryptType
, 0, 1, 1 },
1970 { "start", "Start encryption. ('encrypt start ?' for more)",
1971 (int (*)(char *, char *))EncryptStart
, 1, 0, 1 },
1972 { "stop", "Stop encryption. ('encrypt stop ?' for more)",
1973 (int (*)(char *, char *))EncryptStop
, 1, 0, 1 },
1974 { "input", "Start encrypting the input stream",
1975 (int (*)(char *, char *))EncryptStartInput
, 1, 0, 0 },
1976 { "-input", "Stop encrypting the input stream",
1977 (int (*)(char *, char *))EncryptStopInput
, 1, 0, 0 },
1978 { "output", "Start encrypting the output stream",
1979 (int (*)(char *, char *))EncryptStartOutput
, 1, 0, 0 },
1980 { "-output", "Stop encrypting the output stream",
1981 (int (*)(char *, char *))EncryptStopOutput
, 1, 0, 0 },
1983 { "status", "Display current status of authentication information",
1984 (int (*)(char *, char *))EncryptStatus
, 0, 0, 0 },
1985 { "help", NULL
, (int (*)(char *, char *))EncryptHelp
, 0, 0, 0 },
1986 { "?", "Print help information", (int (*)(char *, char *))EncryptHelp
, 0, 0, 0 },
1987 { NULL
, NULL
, NULL
, 0, 0, 0 },
1993 struct encryptlist
*c
;
1995 for (c
= EncryptList
; c
->name
; c
++) {
1998 printf("%-15s %s\n", c
->name
, c
->help
);
2007 encrypt_cmd(int argc
, char *argv
[])
2009 struct encryptlist
*c
;
2013 "Need an argument to 'encrypt' command. 'encrypt ?' for help.\n");
2017 c
= (struct encryptlist
*)
2018 genget(argv
[1], (char **) EncryptList
, sizeof(struct encryptlist
));
2020 fprintf(stderr
, "'%s': unknown argument ('encrypt ?' for help).\n",
2024 if (Ambiguous((void *)c
)) {
2025 fprintf(stderr
, "'%s': ambiguous argument ('encrypt ?' for help).\n",
2030 if (argc
< c
->minarg
|| argc
> c
->maxarg
) {
2031 if (c
->minarg
== c
->maxarg
) {
2032 fprintf(stderr
, "Need %s%d argument%s ",
2033 c
->minarg
< argc
? "only " : "", c
->minarg
,
2034 c
->minarg
== 1 ? "" : "s");
2036 fprintf(stderr
, "Need %s%d-%d arguments ",
2037 c
->maxarg
< argc
? "only " : "", c
->minarg
, c
->maxarg
);
2039 fprintf(stderr
, "to 'encrypt %s' command. 'encrypt ?' for help.\n",
2043 if (c
->needconnect
&& !connected
) {
2044 if (!(argc
&& (isprefix(argv
[2], "help") || isprefix(argv
[2], "?")))) {
2045 printf("?Need to be connected first.\n");
2049 return ((*c
->handler
)(argc
> 0 ? argv
[2] : 0,
2050 argc
> 1 ? argv
[3] : 0));
2052 #endif /* ENCRYPTION */
2055 * Print status about the connection.
2059 status(int argc
, char *argv
[])
2062 printf("Connected to %s.\n", hostname
);
2063 if ((argc
< 2) || strcmp(argv
[1], "notmuch")) {
2064 int mode
= getconnmode();
2066 if (my_want_state_is_will(TELOPT_LINEMODE
)) {
2067 printf("Operating with LINEMODE option\n");
2068 printf("%s line editing\n", (mode
&MODE_EDIT
) ? "Local" : "No");
2069 printf("%s catching of signals\n",
2070 (mode
&MODE_TRAPSIG
) ? "Local" : "No");
2072 #ifdef KLUDGELINEMODE
2073 } else if (kludgelinemode
&& my_want_state_is_dont(TELOPT_SGA
)) {
2074 printf("Operating in obsolete linemode\n");
2077 printf("Operating in single character mode\n");
2079 printf("Catching signals locally\n");
2081 printf("%s character echo\n", (mode
&MODE_ECHO
) ? "Local" : "Remote");
2082 if (my_want_state_is_will(TELOPT_LFLOW
))
2083 printf("%s flow control\n", (mode
&MODE_FLOW
) ? "Local" : "No");
2086 #endif /* ENCRYPTION */
2089 printf("No connection.\n");
2091 printf("Escape character is '%s'.\n", control(escape
));
2092 (void) fflush(stdout
);
2098 * Function that gets called when SIGINFO is received.
2103 (void) call(status
, "status", "notmuch", 0);
2108 sockaddr_ntop(struct sockaddr
*sa
)
2111 static char addrbuf
[INET6_ADDRSTRLEN
];
2113 switch (sa
->sa_family
) {
2115 addr
= &((struct sockaddr_in
*)sa
)->sin_addr
;
2118 addr
= &((struct sockaddr_un
*)sa
)->sun_path
;
2122 addr
= &((struct sockaddr_in6
*)sa
)->sin6_addr
;
2128 inet_ntop(sa
->sa_family
, addr
, addrbuf
, sizeof(addrbuf
));
2132 #if defined(IPSEC) && defined(IPSEC_POLICY_IPSEC)
2134 setpolicy(int lnet
, struct addrinfo
*res
, char *policy
)
2143 buf
= ipsec_set_policy(policy
, strlen(policy
));
2145 printf("%s\n", ipsec_strerror());
2148 level
= res
->ai_family
== AF_INET
? IPPROTO_IP
: IPPROTO_IPV6
;
2149 optname
= res
->ai_family
== AF_INET
? IP_IPSEC_POLICY
: IPV6_IPSEC_POLICY
;
2150 if (setsockopt(lnet
, level
, optname
, buf
, ipsec_get_policylen(buf
)) < 0){
2151 perror("setsockopt");
2162 * When an Address Family related error happend, check if retry with
2163 * another AF is possible or not.
2164 * Return 1, if retry with another af is OK. Else, return 0.
2167 switch_af(struct addrinfo
**aip
)
2170 struct addrinfo
*ai
;
2173 nextaf
= (ai
->ai_family
== AF_INET
) ? AF_INET6
: AF_INET
;
2176 while (ai
!= NULL
&& ai
->ai_family
!= nextaf
);
2186 tn(int argc
, char *argv
[])
2189 int proto
= 0, opt
= 0;
2192 int srcroute
= 0, result
;
2196 char *cmd
, *hostp
= 0, *portp
= 0, *user
= 0;
2197 char *src_addr
= NULL
;
2198 struct addrinfo hints
, *res
, *res0
= NULL
, *src_res
, *src_res0
= NULL
;
2199 int error
= 0, af_error
= 0;
2202 printf("?Already connected to %s\n", hostname
);
2207 (void) strcpy(line
, "open ");
2209 (void) fgets(&line
[strlen(line
)], sizeof(line
) - strlen(line
), stdin
);
2217 if (strcmp(*argv
, "help") == 0 || isprefix(*argv
, "?"))
2219 if (strcmp(*argv
, "-l") == 0) {
2227 if (strcmp(*argv
, "-a") == 0) {
2232 if (strcmp(*argv
, "-s") == 0) {
2251 printf("usage: %s [-l user] [-a] [-s src_addr] host-name [port]\n", cmd
);
2258 if (src_addr
!= NULL
) {
2259 memset(&hints
, 0, sizeof(hints
));
2260 hints
.ai_flags
= AI_NUMERICHOST
;
2261 hints
.ai_family
= family
;
2262 hints
.ai_socktype
= SOCK_STREAM
;
2263 error
= getaddrinfo(src_addr
, 0, &hints
, &src_res
);
2265 if ((error
== EAI_NODATA
) || (error
== EAI_NONAME
))
2267 if (error
== EAI_NONAME
)
2271 error
= getaddrinfo(src_addr
, 0, &hints
, &src_res
);
2274 fprintf(stderr
, "%s: %s\n", src_addr
, gai_strerror(error
));
2275 if (error
== EAI_SYSTEM
)
2276 fprintf(stderr
, "%s: %s\n", src_addr
, strerror(errno
));
2282 if (hostp
[0] == '/') {
2283 struct sockaddr_un su
;
2285 if (strlen(hostp
) >= sizeof(su
.sun_path
)) {
2286 fprintf(stderr
, "hostname too long for unix domain socket: %s",
2290 memset(&su
, 0, sizeof su
);
2291 su
.sun_family
= AF_UNIX
;
2292 strncpy(su
.sun_path
, hostp
, sizeof su
.sun_path
);
2293 printf("Trying %s...\n", hostp
);
2294 net
= socket(PF_UNIX
, SOCK_STREAM
, 0);
2299 if (connect(net
, (struct sockaddr
*)&su
, sizeof su
) == -1) {
2300 perror(su
.sun_path
);
2301 (void) NetClose(net
);
2305 } else if (hostp
[0] == '@' || hostp
[0] == '!') {
2308 family
== AF_INET6
||
2310 (hostname
= strrchr(hostp
, ':')) == NULL
)
2311 hostname
= strrchr(hostp
, '@');
2312 if (hostname
== NULL
) {
2322 portp
= strdup("telnet");
2323 } else if (*portp
== '-') {
2326 } else if (*portp
== '+') {
2335 * 5760578: Attempt to convert service name to a
2336 * numeric port before calling getaddrinfo().
2338 struct servent
*srv
= getservbyname(portp
, NULL
);
2340 asprintf(&portp
, "%d", ntohs(srv
->s_port
));
2343 #endif /* __APPLE__ */
2345 memset(&hints
, 0, sizeof(hints
));
2346 hints
.ai_flags
= AI_NUMERICHOST
;
2347 hints
.ai_family
= family
;
2348 hints
.ai_socktype
= SOCK_STREAM
;
2349 error
= getaddrinfo(hostname
, portp
, &hints
, &res
);
2351 hints
.ai_flags
= AI_CANONNAME
;
2352 error
= getaddrinfo(hostname
, portp
, &hints
, &res
);
2355 fprintf(stderr
, "%s: %s\n", hostname
, gai_strerror(error
));
2356 if (error
== EAI_SYSTEM
)
2357 fprintf(stderr
, "%s: %s\n", hostname
, strerror(errno
));
2361 if (hints
.ai_flags
== AI_NUMERICHOST
) {
2362 /* hostname has numeric */
2366 gni_err
= getnameinfo(res
->ai_addr
, res
->ai_addr
->sa_len
,
2367 _hostname
, sizeof(_hostname
) - 1, NULL
, 0,
2370 (void) strncpy(_hostname
, hostp
, sizeof(_hostname
) - 1);
2371 _hostname
[sizeof(_hostname
)-1] = '\0';
2372 hostname
= _hostname
;
2374 /* hostname has FQDN */
2376 (void) strncpy(_hostname
, hostname
, sizeof(_hostname
) - 1);
2377 else if (res
->ai_canonname
!= NULL
)
2378 strncpy(_hostname
, res
->ai_canonname
, sizeof(_hostname
) - 1);
2380 (void) strncpy(_hostname
, hostp
, sizeof(_hostname
) - 1);
2381 _hostname
[sizeof(_hostname
)-1] = '\0';
2382 hostname
= _hostname
;
2389 if (srcroute
!= 0) {
2390 static char hostbuf
[BUFSIZ
];
2392 if (af_error
== 0) { /* save intermediate hostnames for retry */
2393 strncpy(hostbuf
, hostp
, BUFSIZ
- 1);
2394 hostbuf
[BUFSIZ
- 1] = '\0';
2398 result
= sourceroute(res
, hostp
, &srp
, &srlen
, &proto
, &opt
);
2401 if (family
== AF_UNSPEC
&& af_error
== 0 &&
2402 switch_af(&res
) == 1) {
2409 } else if (result
== -1) {
2410 printf("Bad source route option: %s\n", hostp
);
2417 printf("Trying %s...\n", sockaddr_ntop(res
->ai_addr
));
2418 net
= socket(res
->ai_family
, res
->ai_socktype
, res
->ai_protocol
);
2422 if (family
== AF_UNSPEC
&& af_error
== 0 &&
2423 switch_af(&res
) == 1) {
2428 perror("telnet: socket");
2431 if (srp
&& setsockopt(net
, proto
, opt
, (char *)srp
, srlen
) < 0)
2432 perror("setsockopt (source route)");
2433 #if defined(IPPROTO_IP) && defined(IP_TOS)
2434 if (res
->ai_family
== PF_INET
) {
2435 # if defined(HAS_GETTOS)
2437 if (tos
< 0 && (tp
= gettosbyname("telnet", "tcp")))
2441 tos
= IPTOS_LOWDELAY
;
2443 && (setsockopt(net
, IPPROTO_IP
, IP_TOS
,
2444 (char *)&tos
, sizeof(int)) < 0)
2445 && (errno
!= ENOPROTOOPT
))
2446 perror("telnet: setsockopt (IP_TOS) (ignored)");
2448 #endif /* defined(IPPROTO_IP) && defined(IP_TOS) */
2450 if (telnet_debug
&& SetSockOpt(net
, SOL_SOCKET
, SO_DEBUG
, 1) < 0) {
2451 perror("setsockopt (SO_DEBUG)");
2454 if (src_addr
!= NULL
) {
2455 for (src_res
= src_res0
; src_res
!= 0; src_res
= src_res
->ai_next
)
2456 if (src_res
->ai_family
== res
->ai_family
)
2458 if (src_res
== NULL
)
2460 if (bind(net
, src_res
->ai_addr
, src_res
->ai_addrlen
) == -1) {
2462 if (family
== AF_UNSPEC
&& af_error
== 0 &&
2463 switch_af(&res
) == 1) {
2465 (void) NetClose(net
);
2470 (void) NetClose(net
);
2474 #if defined(IPSEC) && defined(IPSEC_POLICY_IPSEC)
2475 if (setpolicy(net
, res
, ipsec_policy_in
) < 0) {
2476 (void) NetClose(net
);
2479 if (setpolicy(net
, res
, ipsec_policy_out
) < 0) {
2480 (void) NetClose(net
);
2485 if (connect(net
, res
->ai_addr
, res
->ai_addrlen
) < 0) {
2486 struct addrinfo
*next
;
2488 next
= res
->ai_next
;
2489 /* If already an af failed, only try same af. */
2491 while (next
!= NULL
&& next
->ai_family
!= res
->ai_family
)
2492 next
= next
->ai_next
;
2493 warn("connect to address %s", sockaddr_ntop(res
->ai_addr
));
2496 (void) NetClose(net
);
2499 warnx("Unable to connect to remote host");
2500 (void) NetClose(net
);
2504 #ifdef AUTHENTICATION
2506 auth_encrypt_connect(connected
);
2509 } while (connected
== 0);
2511 if (src_res0
!= NULL
)
2512 freeaddrinfo(src_res0
);
2513 cmdrc(hostp
, hostname
);
2516 if (autologin
&& user
== NULL
) {
2519 user
= getenv("USER");
2521 ((pw
= getpwnam(user
)) && pw
->pw_uid
!= getuid())) {
2522 if ((pw
= getpwuid(getuid())))
2529 env_define((unsigned char*)"USER
", (unsigned char*)user);
2530 env_export((unsigned char*)"USER
");
2532 (void) call(status, "status
", "notmuch
", 0);
2533 if (setjmp(peerdied) == 0)
2535 (void) NetClose(net);
2536 ExitString("Connection closed by foreign host
.\n",1);
2541 if (src_res0 != NULL)
2542 freeaddrinfo(src_res0);
2546 #define HELPINDENT (sizeof ("connect
"))
2549 openhelp[] = "connect to a site
",
2550 closehelp[] = "close current connection
",
2551 logouthelp[] = "forcibly logout remote user
and close the connection
",
2552 quithelp[] = "exit telnet
",
2553 statushelp[] = "print status information
",
2554 helphelp[] = "print help information
",
2555 sendhelp[] = "transmit special
characters ('send ?' for more)"
,
2556 sethelp
[] = "set operating parameters ('set ?' for more)",
2557 unsethelp
[] = "unset operating parameters ('unset ?' for more)",
2558 togglestring
[] ="toggle operating parameters ('toggle ?' for more)",
2559 slchelp
[] = "change state of special charaters ('slc ?' for more)",
2560 displayhelp
[] = "display operating parameters",
2561 #ifdef AUTHENTICATION
2562 authhelp
[] = "turn on (off) authentication ('auth ?' for more)",
2565 encrypthelp
[] = "turn on (off) encryption ('encrypt ?' for more)",
2566 #endif /* ENCRYPTION */
2567 zhelp
[] = "suspend telnet",
2569 opiehelp
[] = "compute response to OPIE challenge",
2571 shellhelp
[] = "invoke a subshell",
2572 envhelp
[] = "change environment variables ('environ ?' for more)",
2573 modestring
[] = "try to enter line or character mode ('mode ?' for more)";
2575 static Command cmdtab
[] = {
2576 { "close", closehelp
, bye
, 1 },
2577 { "logout", logouthelp
, (int (*)(int, char **))logout
, 1 },
2578 { "display", displayhelp
, display
, 0 },
2579 { "mode", modestring
, modecmd
, 0 },
2580 { "telnet", openhelp
, tn
, 0 },
2581 { "open", openhelp
, tn
, 0 },
2582 { "quit", quithelp
, (int (*)(int, char **))quit
, 0 },
2583 { "send", sendhelp
, sendcmd
, 0 },
2584 { "set", sethelp
, setcmd
, 0 },
2585 { "unset", unsethelp
, unsetcmd
, 0 },
2586 { "status", statushelp
, status
, 0 },
2587 { "toggle", togglestring
, toggle
, 0 },
2588 { "slc", slchelp
, slccmd
, 0 },
2589 #ifdef AUTHENTICATION
2590 { "auth", authhelp
, auth_cmd
, 0 },
2593 { "encrypt", encrypthelp
, encrypt_cmd
, 0 },
2594 #endif /* ENCRYPTION */
2595 { "z", zhelp
, (int (*)(int, char **))suspend
, 0 },
2596 { "!", shellhelp
, shell
, 1 },
2597 { "environ", envhelp
, env_cmd
, 0 },
2598 { "?", helphelp
, help
, 0 },
2600 { "opie", opiehelp
, opie_calc
, 0 },
2602 { NULL
, NULL
, NULL
, 0 }
2605 static char crmodhelp
[] = "deprecated command -- use 'toggle crmod' instead";
2606 static char escapehelp
[] = "deprecated command -- use 'set escape' instead";
2608 static Command cmdtab2
[] = {
2609 { "help", 0, help
, 0 },
2610 { "escape", escapehelp
, setescape
, 0 },
2611 { "crmod", crmodhelp
, (int (*)(int, char **))togcrmod
, 0 },
2612 { NULL
, NULL
, NULL
, 0 }
2617 * Call routine with argc, argv set from args (terminated by 0).
2621 call(intrtn_t routine
, ...)
2627 va_start(ap
, routine
);
2628 while ((args
[argno
++] = va_arg(ap
, char *)) != 0);
2630 return (*routine
)(argno
-1, args
);
2639 if ((cm
= (Command
*) genget(name
, (char **) cmdtab
, sizeof(Command
))))
2641 return (Command
*) genget(name
, (char **) cmdtab2
, sizeof(Command
));
2645 command(int top
, const char *tbuf
, int cnt
)
2653 (void) signal(SIGINT
, SIG_DFL
);
2654 (void) signal(SIGQUIT
, SIG_DFL
);
2657 if (rlogin
== _POSIX_VDISABLE
)
2658 printf("%s> ", prompt
);
2662 while (cnt
> 0 && (*cp
++ = *tbuf
++) != '\n')
2665 if (cp
== line
|| *--cp
!= '\n' || cp
== line
)
2668 if (rlogin
== _POSIX_VDISABLE
)
2669 printf("%s\n", line
);
2672 if (rlogin
!= _POSIX_VDISABLE
)
2673 printf("%s> ", prompt
);
2674 if (fgets(line
, sizeof(line
), stdin
) == NULL
) {
2675 if (feof(stdin
) || ferror(stdin
)) {
2685 if (margv
[0] == 0) {
2688 c
= getcmd(margv
[0]);
2689 if (Ambiguous((void *)c
)) {
2690 printf("?Ambiguous command\n");
2694 printf("?Invalid command\n");
2697 if (c
->needconnect
&& !connected
) {
2698 printf("?Need to be connected first.\n");
2701 if ((*c
->handler
)(margc
, margv
)) {
2707 longjmp(toplevel
, 1);
2718 help(int argc
, char *argv
[])
2723 printf("Commands may be abbreviated. Commands are:\n\n");
2724 for (c
= cmdtab
; c
->name
; c
++)
2726 printf("%-*s\t%s\n", (int)HELPINDENT
, c
->name
,
2731 else while (--argc
> 0) {
2735 if (Ambiguous((void *)c
))
2736 printf("?Ambiguous help command %s\n", arg
);
2737 else if (c
== (Command
*)0)
2738 printf("?Invalid help command %s\n", arg
);
2740 printf("%s\n", c
->help
);
2745 static char *rcname
= 0;
2746 static char rcbuf
[128];
2749 cmdrc(char *m1
, char *m2
)
2754 int l1
= strlen(m1
);
2755 int l2
= strlen(m2
);
2756 char m1save
[MAXHOSTNAMELEN
];
2761 strlcpy(m1save
, m1
, sizeof(m1save
));
2765 rcname
= getenv("HOME");
2766 if (rcname
&& (strlen(rcname
) + 10) < sizeof(rcbuf
))
2767 strcpy(rcbuf
, rcname
);
2770 strcat(rcbuf
, "/.telnetrc");
2774 if ((rcfile
= fopen(rcname
, "r")) == 0) {
2779 if (fgets(line
, sizeof(line
), rcfile
) == NULL
)
2786 if (!isspace(line
[0]))
2789 if (gotmachine
== 0) {
2790 if (isspace(line
[0]))
2792 if (strncasecmp(line
, m1
, l1
) == 0)
2793 strncpy(line
, &line
[l1
], sizeof(line
) - l1
);
2794 else if (strncasecmp(line
, m2
, l2
) == 0)
2795 strncpy(line
, &line
[l2
], sizeof(line
) - l2
);
2796 else if (strncasecmp(line
, "DEFAULT", 7) == 0)
2797 strncpy(line
, &line
[7], sizeof(line
) - 7);
2800 if (line
[0] != ' ' && line
[0] != '\t' && line
[0] != '\n')
2807 c
= getcmd(margv
[0]);
2808 if (Ambiguous((void *)c
)) {
2809 printf("?Ambiguous command: %s\n", margv
[0]);
2813 printf("?Invalid command: %s\n", margv
[0]);
2817 * This should never happen...
2819 if (c
->needconnect
&& !connected
) {
2820 printf("?Need to be connected first for %s.\n", margv
[0]);
2823 (*c
->handler
)(margc
, margv
);
2830 * Source route is handed in as
2831 * [!]@hop1@hop2...[@|:]dst
2832 * If the leading ! is present, it is a
2833 * strict source route, otherwise it is
2834 * assmed to be a loose source route.
2836 * We fill in the source route option as
2837 * hop1,hop2,hop3...dest
2838 * and return a pointer to hop1, which will
2839 * be the address to connect() to.
2843 * res: ponter to addrinfo structure which contains sockaddr to
2844 * the host to connect to.
2846 * arg: pointer to route list to decipher
2848 * cpp: If *cpp is not equal to NULL, this is a
2849 * pointer to a pointer to a character array
2850 * that should be filled in with the option.
2852 * lenp: pointer to an integer that contains the
2853 * length of *cpp if *cpp != NULL.
2855 * protop: pointer to an integer that should be filled in with
2856 * appropriate protocol for setsockopt, as socket
2859 * optp: pointer to an integer that should be filled in with
2860 * appropriate option for setsockopt, as socket protocol
2865 * If the return value is 1, then all operations are
2866 * successful. If the
2867 * return value is -1, there was a syntax error in the
2868 * option, either unknown characters, or too many hosts.
2869 * If the return value is 0, one of the hostnames in the
2870 * path is unknown, and *cpp is set to point to the bad
2873 * *cpp: If *cpp was equal to NULL, it will be filled
2874 * in with a pointer to our static area that has
2875 * the option filled in. This will be 32bit aligned.
2877 * *lenp: This will be filled in with how long the option
2878 * pointed to by *cpp is.
2880 * *protop: This will be filled in with appropriate protocol for
2881 * setsockopt, as socket protocol family.
2883 * *optp: This will be filled in with appropriate option for
2884 * setsockopt, as socket protocol family.
2887 sourceroute(struct addrinfo
*ai
, char *arg
, char **cpp
, int *lenp
, int *protop
, int *optp
)
2889 static char buf
[1024 + ALIGNBYTES
]; /*XXX*/
2890 char *cp
, *cp2
, *lsrp
, *ep
;
2891 struct sockaddr_in
*_sin
;
2893 struct sockaddr_in6
*sin6
;
2894 struct cmsghdr
*cmsg
= NULL
;
2896 struct addrinfo hints
, *res
;
2901 * Verify the arguments, and make sure we have
2902 * at least 7 bytes for the option.
2904 if (cpp
== NULL
|| lenp
== NULL
)
2907 switch (ai
->ai_family
) {
2914 if (*lenp
< (int)CMSG_SPACE(sizeof(struct ip6_rthdr
) +
2915 sizeof(struct in6_addr
)))
2922 * Decide whether we have a buffer passed to us,
2923 * or if we need to use our own static buffer.
2929 *cpp
= lsrp
= (char *)ALIGN(buf
);
2936 if (ai
->ai_family
== AF_INET6
) {
2937 cmsg
= inet6_rthdr_init(*cpp
, IPV6_RTHDR_TYPE_0
);
2940 *protop
= IPPROTO_IPV6
;
2941 *optp
= IPV6_PKTOPTIONS
;
2946 * Next, decide whether we have a loose source
2947 * route or a strict source route, and fill in
2948 * the begining of the option.
2952 *lsrp
++ = IPOPT_SSRR
;
2954 *lsrp
++ = IPOPT_LSRR
;
2959 lsrp
++; /* skip over length, we'll fill it in later */
2961 *protop
= IPPROTO_IP
;
2966 memset(&hints
, 0, sizeof(hints
));
2967 hints
.ai_family
= ai
->ai_family
;
2968 hints
.ai_socktype
= SOCK_STREAM
;
2972 ai
->ai_family
!= AF_INET6
&&
2976 else for (cp2
= cp
; (c
= *cp2
); cp2
++) {
2981 } else if (c
== '@') {
2985 ai
->ai_family
!= AF_INET6
&&
2996 hints
.ai_flags
= AI_NUMERICHOST
;
2997 error
= getaddrinfo(cp
, NULL
, &hints
, &res
);
2999 if ((error
== EAI_NODATA
) || (error
== EAI_NONAME
))
3001 if (error
== EAI_NONAME
)
3005 error
= getaddrinfo(cp
, NULL
, &hints
, &res
);
3008 fprintf(stderr
, "%s: %s\n", cp
, gai_strerror(error
));
3009 if (error
== EAI_SYSTEM
)
3010 fprintf(stderr
, "%s: %s\n", cp
,
3016 if (res
->ai_family
== AF_INET6
) {
3017 sin6
= (struct sockaddr_in6
*)res
->ai_addr
;
3018 inet6_rthdr_add(cmsg
, &sin6
->sin6_addr
,
3023 _sin
= (struct sockaddr_in
*)res
->ai_addr
;
3024 memcpy(lsrp
, (char *)&_sin
->sin_addr
, 4);
3032 * Check to make sure there is space for next address
3035 if (res
->ai_family
== AF_INET6
) {
3036 if (((char *)CMSG_DATA(cmsg
) +
3037 sizeof(struct ip6_rthdr
) +
3038 ((inet6_rthdr_segments(cmsg
) + 1) *
3039 sizeof(struct in6_addr
))) > ep
)
3048 if (res
->ai_family
== AF_INET6
) {
3049 inet6_rthdr_lasthop(cmsg
, IPV6_RTHDR_LOOSE
);
3050 *lenp
= cmsg
->cmsg_len
;
3054 if ((*(*cpp
+IPOPT_OLEN
) = lsrp
- *cpp
) <= 7) {
3059 *lsrp
++ = IPOPT_NOP
; /* 32 bit word align it */
3060 *lenp
= lsrp
- *cpp
;