2 * Copyright (c) 1988, 1990, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * @(#)commands.c 8.4 (Berkeley) 5/30/95
34 * $FreeBSD: src/crypto/telnet/telnet/commands.c,v 1.12.2.7 2003/04/23 07:16:32 ru Exp $
35 * $DragonFly: src/crypto/telnet/telnet/commands.c,v 1.3 2008/09/04 11:54:24 hasso Exp $
38 #include <sys/param.h>
41 #include <sys/socket.h>
42 #include <netinet/in.h>
55 #include <arpa/telnet.h>
56 #include <arpa/inet.h>
68 #include <libtelnet/auth.h>
71 #include <libtelnet/encrypt.h>
74 #include <netinet/in_systm.h>
75 #include <netinet/ip.h>
76 #include <netinet/ip6.h>
78 #ifndef MAXHOSTNAMELEN
79 #define MAXHOSTNAMELEN 256
82 typedef int (*intrtn_t
)(int, char **);
85 extern int auth_togdebug(int);
88 extern int EncryptAutoEnc(int);
89 extern int EncryptAutoDec(int);
90 extern int EncryptDebug(int);
91 extern int EncryptVerbose(int);
92 #endif /* ENCRYPTION */
93 #if defined(IPPROTO_IP) && defined(IP_TOS)
95 #endif /* defined(IPPROTO_IP) && defined(IP_TOS) */
98 static char _hostname
[MAXHOSTNAMELEN
];
100 static int help(int, char **);
101 static int call(intrtn_t
, ...);
102 static void cmdrc(char *, char *);
104 static int switch_af(struct addrinfo
**);
106 static int togglehelp(void);
107 static int send_tncmd(void (*)(int, int), const char *, char *);
108 static int setmod(int);
109 static int clearmode(int);
110 static int modehelp(void);
111 static int sourceroute(struct addrinfo
*, char *, char **, int *, int *, int *);
114 const char *name
; /* command name */
115 const char *help
; /* help string (NULL for no help) */
116 int (*handler
)(int, char **); /* routine which executes command */
117 int needconnect
; /* Do we need to be connected to execute? */
120 static char line
[256];
121 static char saveline
[256];
123 static char *margv
[20];
126 #include <sys/wait.h>
127 #define PATH_OPIEKEY "/usr/bin/opiekey"
129 opie_calc(int argc
, char *argv
[])
134 printf("%s sequence challenge\n", argv
[0]);
140 execv(PATH_OPIEKEY
, argv
);
146 (void) wait(&status
);
147 if (WIFEXITED(status
))
148 return (WEXITSTATUS(status
));
162 if (*cp
== '!') { /* Special case shell escape */
163 strcpy(saveline
, line
); /* save for shell command */
164 *argp
++ = strdup("!"); /* No room in string to get this */
176 for (cp2
= cp
; c
!= '\0'; c
= *++cp
) {
184 if ((c
= *++cp
) == '\0')
186 } else if (c
== '"') {
189 } else if (c
== '\'') {
192 } else if (isspace(c
))
206 * Make a character string into a number.
208 * Todo: 1. Could take random integers (12, 0x12, 012, 0b1).
221 c
= b
| 0x40; /* DEL */
234 * Construct a control character sequence
235 * for a special character.
242 * The only way I could get the Sun 3.5 compiler
244 * if ((unsigned int)c >= 0x80)
245 * was to assign "c" to an unsigned int variable...
248 unsigned int uic
= (unsigned int)c
;
252 if (c
== (cc_t
)_POSIX_VDISABLE
) {
257 buf
[1] = ((c
>>6)&07) + '0';
258 buf
[2] = ((c
>>3)&07) + '0';
259 buf
[3] = (c
&07) + '0';
261 } else if (uic
>= 0x20) {
273 * The following are data structures and routines for
274 * the "send" command.
279 const char *name
; /* How user refers to it (case independent) */
280 const char *help
; /* Help information (0 ==> no help) */
281 int needconnect
; /* Need to be connected */
282 int narg
; /* Number of arguments */
283 int (*handler
)(char *, ...); /* Routine to perform (for special ops) */
284 int nbyte
; /* Number of bytes to send this command */
285 int what
; /* Character to be sent (<0 ==> special) */
293 send_dontcmd(char *),
294 send_willcmd(char *),
295 send_wontcmd(char *);
297 static struct sendlist Sendlist
[] = {
298 { "ao", "Send Telnet Abort output", 1, 0, NULL
, 2, AO
},
299 { "ayt", "Send Telnet 'Are You There'", 1, 0, NULL
, 2, AYT
},
300 { "brk", "Send Telnet Break", 1, 0, NULL
, 2, BREAK
},
301 { "break", NULL
, 1, 0, NULL
, 2, BREAK
},
302 { "ec", "Send Telnet Erase Character", 1, 0, NULL
, 2, EC
},
303 { "el", "Send Telnet Erase Line", 1, 0, NULL
, 2, EL
},
304 { "escape", "Send current escape character",1, 0, (int (*)(char *, ...))send_esc
, 1, 0 },
305 { "ga", "Send Telnet 'Go Ahead' sequence", 1, 0, NULL
, 2, GA
},
306 { "ip", "Send Telnet Interrupt Process",1, 0, NULL
, 2, IP
},
307 { "intp", NULL
, 1, 0, NULL
, 2, IP
},
308 { "interrupt", NULL
, 1, 0, NULL
, 2, IP
},
309 { "intr", NULL
, 1, 0, NULL
, 2, IP
},
310 { "nop", "Send Telnet 'No operation'", 1, 0, NULL
, 2, NOP
},
311 { "eor", "Send Telnet 'End of Record'", 1, 0, NULL
, 2, EOR
},
312 { "abort", "Send Telnet 'Abort Process'", 1, 0, NULL
, 2, ABORT
},
313 { "susp", "Send Telnet 'Suspend Process'",1, 0, NULL
, 2, SUSP
},
314 { "eof", "Send Telnet End of File Character", 1, 0, NULL
, 2, xEOF
},
315 { "synch", "Perform Telnet 'Synch operation'", 1, 0, (int (*)(char *, ...))dosynch
, 2, 0 },
316 { "getstatus", "Send request for STATUS", 1, 0, (int (*)(char *, ...))get_status
, 6, 0 },
317 { "?", "Display send options", 0, 0, (int (*)(char *, ...))send_help
, 0, 0 },
318 { "help", NULL
, 0, 0, (int (*)(char *, ...))send_help
, 0, 0 },
319 { "do", NULL
, 0, 1, (int (*)(char *, ...))send_docmd
, 3, 0 },
320 { "dont", NULL
, 0, 1, (int (*)(char *, ...))send_dontcmd
, 3, 0 },
321 { "will", NULL
, 0, 1, (int (*)(char *, ...))send_willcmd
, 3, 0 },
322 { "wont", NULL
, 0, 1, (int (*)(char *, ...))send_wontcmd
, 3, 0 },
323 { NULL
, NULL
, 0, 0, NULL
, 0, 0 }
326 #define GETSEND(name) ((struct sendlist *) genget(name, (char **) Sendlist, \
327 sizeof(struct sendlist)))
330 sendcmd(int argc
, char *argv
[])
332 int count
; /* how many bytes we are going to need to send */
334 struct sendlist
*s
; /* pointer to current command */
339 printf("need at least one argument for 'send' command\n");
340 printf("'send ?' for help\n");
344 * First, validate all the send arguments.
345 * In addition, we see how much space we are going to need, and
346 * whether or not we will be doing a "SYNCH" operation (which
347 * flushes the network queue).
350 for (i
= 1; i
< argc
; i
++) {
351 s
= GETSEND(argv
[i
]);
353 printf("Unknown send argument '%s'\n'send ?' for help.\n",
356 } else if (Ambiguous((void *)s
)) {
357 printf("Ambiguous send argument '%s'\n'send ?' for help.\n",
361 if (i
+ s
->narg
>= argc
) {
363 "Need %d argument%s to 'send %s' command. 'send %s ?' for help.\n",
364 s
->narg
, s
->narg
== 1 ? "" : "s", s
->name
, s
->name
);
368 if ((void *)s
->handler
== (void *)send_help
) {
374 needconnect
+= s
->needconnect
;
376 if (!connected
&& needconnect
) {
377 printf("?Need to be connected first.\n");
378 printf("'send ?' for help\n");
381 /* Now, do we have enough room? */
382 if (NETROOM() < count
) {
383 printf("There is not enough room in the buffer TO the network\n");
384 printf("to process your request. Nothing will be done.\n");
385 printf("('send synch' will throw away most data in the network\n");
386 printf("buffer, if this might help.)\n");
389 /* OK, they are all OK, now go through again and actually send */
391 for (i
= 1; i
< argc
; i
++) {
392 if ((s
= GETSEND(argv
[i
])) == 0) {
393 fprintf(stderr
, "Telnet 'send' error - argument disappeared!\n");
399 success
+= (*s
->handler
)((s
->narg
> 0) ? argv
[i
+1] : 0,
400 (s
->narg
> 1) ? argv
[i
+2] : 0);
403 NET2ADD(IAC
, s
->what
);
404 printoption("SENT", IAC
, s
->what
);
407 return (count
== success
);
418 send_docmd(char *name
)
420 return(send_tncmd(send_do
, "do", name
));
424 send_dontcmd(char *name
)
426 return(send_tncmd(send_dont
, "dont", name
));
430 send_willcmd(char *name
)
432 return(send_tncmd(send_will
, "will", name
));
436 send_wontcmd(char *name
)
438 return(send_tncmd(send_wont
, "wont", name
));
442 send_tncmd(void (*func
)(int, int), const char *cmd
, char *name
)
445 extern char *telopts
[];
448 if (isprefix(name
, "help") || isprefix(name
, "?")) {
451 printf("Usage: send %s <value|option>\n", cmd
);
452 printf("\"value\" must be from 0 to 255\n");
453 printf("Valid options are:\n\t");
456 for (cpp
= telopts
; *cpp
; cpp
++) {
457 len
= strlen(*cpp
) + 3;
458 if (col
+ len
> 65) {
462 printf(" \"%s\"", *cpp
);
468 cpp
= (char **)genget(name
, telopts
, sizeof(char *));
469 if (Ambiguous(cpp
)) {
470 fprintf(stderr
,"'%s': ambiguous argument ('send %s ?' for help).\n",
479 while (*cp
>= '0' && *cp
<= '9') {
485 fprintf(stderr
, "'%s': unknown argument ('send %s ?' for help).\n",
488 } else if (val
< 0 || val
> 255) {
489 fprintf(stderr
, "'%s': bad value ('send %s ?' for help).\n",
495 printf("?Need to be connected first.\n");
505 struct sendlist
*s
; /* pointer to current command */
506 for (s
= Sendlist
; s
->name
; s
++) {
508 printf("%-15s %s\n", s
->name
, s
->help
);
514 * The following are the routines and data structures referred
515 * to by the arguments to the "toggle" command.
530 (SetSockOpt(net
, SOL_SOCKET
, SO_DEBUG
, debug
)) < 0) {
531 perror("setsockopt (SO_DEBUG)");
535 if (net
> 0 && SetSockOpt(net
, SOL_SOCKET
, SO_DEBUG
, 1) < 0)
536 perror("setsockopt (SO_DEBUG)");
538 printf("Cannot turn off socket debugging\n");
548 printf("Will send carriage returns as telnet <CR><LF>.\n");
550 printf("Will send carriage returns as telnet <CR><NUL>.\n");
560 donebinarytoggle
= 1;
565 if (my_want_state_is_will(TELOPT_BINARY
) &&
566 my_want_state_is_do(TELOPT_BINARY
)) {
568 } else if (my_want_state_is_wont(TELOPT_BINARY
) &&
569 my_want_state_is_dont(TELOPT_BINARY
)) {
572 val
= binmode
? 0 : 1;
576 if (my_want_state_is_will(TELOPT_BINARY
) &&
577 my_want_state_is_do(TELOPT_BINARY
)) {
578 printf("Already operating in binary mode with remote host.\n");
580 printf("Negotiating binary mode with remote host.\n");
584 if (my_want_state_is_wont(TELOPT_BINARY
) &&
585 my_want_state_is_dont(TELOPT_BINARY
)) {
586 printf("Already in network ascii mode with remote host.\n");
588 printf("Negotiating network ascii mode with remote host.\n");
598 donebinarytoggle
= 1;
601 val
= my_want_state_is_do(TELOPT_BINARY
) ? 0 : 1;
604 if (my_want_state_is_do(TELOPT_BINARY
)) {
605 printf("Already receiving in binary mode.\n");
607 printf("Negotiating binary mode on input.\n");
611 if (my_want_state_is_dont(TELOPT_BINARY
)) {
612 printf("Already receiving in network ascii mode.\n");
614 printf("Negotiating network ascii mode on input.\n");
624 donebinarytoggle
= 1;
627 val
= my_want_state_is_will(TELOPT_BINARY
) ? 0 : 1;
630 if (my_want_state_is_will(TELOPT_BINARY
)) {
631 printf("Already transmitting in binary mode.\n");
633 printf("Negotiating binary mode on output.\n");
637 if (my_want_state_is_wont(TELOPT_BINARY
)) {
638 printf("Already transmitting in network ascii mode.\n");
640 printf("Negotiating network ascii mode on output.\n");
648 const char *name
; /* name of toggle */
649 const char *help
; /* help message */
650 int (*handler
)(int); /* routine to do actual setting */
652 const char *actionexplanation
;
655 static struct togglelist Togglelist
[] = {
657 "flushing of output when sending interrupt characters",
660 "flush output when sending interrupt characters" },
662 "automatic sending of interrupt characters in urgent mode",
665 "send interrupt characters in urgent mode" },
666 #ifdef AUTHENTICATION
668 "automatic sending of login and/or authentication info",
671 "send login name and/or authentication information" },
673 "Toggle authentication debugging",
676 "print authentication debugging information" },
680 "automatic encryption of data stream",
683 "automatically encrypt output" },
685 "automatic decryption of data stream",
688 "automatically decrypt input" },
690 "Toggle verbose encryption output",
693 "print verbose encryption output" },
695 "Toggle encryption debugging",
698 "print encryption debugging information" },
699 #endif /* ENCRYPTION */
701 "don't read ~/.telnetrc file",
704 "skip reading of ~/.telnetrc file" },
706 "sending and receiving of binary data",
711 "receiving of binary data",
716 "sending of binary data",
721 "sending carriage returns as telnet <CR><LF>",
722 (int (*)(int))togcrlf
,
726 "mapping of received carriage returns",
729 "map carriage return on output" },
731 "local recognition of certain control characters",
732 (int (*)(int))lclchars
,
734 "recognize certain control characters" },
735 { " ", "", NULL
, NULL
, NULL
}, /* empty line */
738 (int (*)(int))togdebug
,
740 "turn on socket level debugging" },
742 "printing of hexadecimal network data (debugging)",
745 "print hexadecimal representation of network traffic" },
747 "output of \"netdata\" to user readable format (debugging)",
750 "print user readable output for \"netdata\"" },
752 "viewing of options processing (debugging)",
755 "show option processing" },
757 "(debugging) toggle printing of hexadecimal terminal data",
760 "print hexadecimal representation of terminal traffic" },
763 (int (*)(int))togglehelp
,
766 { NULL
, NULL
, NULL
, NULL
, NULL
},
769 (int (*)(int))togglehelp
,
772 { NULL
, NULL
, NULL
, NULL
, NULL
}
778 struct togglelist
*c
;
780 for (c
= Togglelist
; c
->name
; c
++) {
783 printf("%-15s toggle %s\n", c
->name
, c
->help
);
789 printf("%-15s %s\n", "?", "display help information");
794 settogglehelp(int set
)
796 struct togglelist
*c
;
798 for (c
= Togglelist
; c
->name
; c
++) {
801 printf("%-15s %s %s\n", c
->name
, set
? "enable" : "disable",
809 #define GETTOGGLE(name) (struct togglelist *) \
810 genget(name, (char **) Togglelist, sizeof(struct togglelist))
813 toggle(int argc
, char *argv
[])
817 struct togglelist
*c
;
821 "Need an argument to 'toggle' command. 'toggle ?' for help.\n");
829 if (Ambiguous((void *)c
)) {
830 fprintf(stderr
, "'%s': ambiguous argument ('toggle ?' for help).\n",
834 fprintf(stderr
, "'%s': unknown argument ('toggle ?' for help).\n",
839 *c
->variable
= !*c
->variable
; /* invert it */
840 if (c
->actionexplanation
) {
841 printf("%s %s.\n", *c
->variable
? "Will" : "Won't",
842 c
->actionexplanation
);
846 retval
&= (*c
->handler
)(-1);
854 * The following perform the "set" command.
858 struct termio new_tc
= { 0, 0, 0, 0, {}, 0, 0 };
862 const char *name
; /* name */
863 const char *help
; /* help information */
864 void (*handler
)(char *);
865 cc_t
*charp
; /* where it is located at */
868 static struct setlist Setlist
[] = {
869 #ifdef KLUDGELINEMODE
870 { "echo", "character to toggle local echoing on/off", NULL
, &echoc
},
872 { "escape", "character to escape back to telnet command mode", NULL
, &escape
},
873 { "rlogin", "rlogin escape character", 0, &rlogin
},
874 { "tracefile", "file to write trace information to", SetNetTrace
, (cc_t
*)NetTraceFile
},
875 { " ", "", NULL
, NULL
},
876 { " ", "The following need 'localchars' to be toggled true", NULL
, NULL
},
877 { "flushoutput", "character to cause an Abort Output", NULL
, termFlushCharp
},
878 { "interrupt", "character to cause an Interrupt Process", NULL
, termIntCharp
},
879 { "quit", "character to cause an Abort process", NULL
, termQuitCharp
},
880 { "eof", "character to cause an EOF ", NULL
, termEofCharp
},
881 { " ", "", NULL
, NULL
},
882 { " ", "The following are for local editing in linemode", NULL
, NULL
},
883 { "erase", "character to use to erase a character", NULL
, termEraseCharp
},
884 { "kill", "character to use to erase a line", NULL
, termKillCharp
},
885 { "lnext", "character to use for literal next", NULL
, termLiteralNextCharp
},
886 { "susp", "character to cause a Suspend Process", NULL
, termSuspCharp
},
887 { "reprint", "character to use for line reprint", NULL
, termRprntCharp
},
888 { "worderase", "character to use to erase a word", NULL
, termWerasCharp
},
889 { "start", "character to use for XON", NULL
, termStartCharp
},
890 { "stop", "character to use for XOFF", NULL
, termStopCharp
},
891 { "forw1", "alternate end of line character", NULL
, termForw1Charp
},
892 { "forw2", "alternate end of line character", NULL
, termForw2Charp
},
893 { "ayt", "alternate AYT character", NULL
, termAytCharp
},
894 { NULL
, NULL
, NULL
, NULL
}
897 static struct setlist
*
900 return (struct setlist
*)
901 genget(name
, (char **) Setlist
, sizeof(struct setlist
));
905 set_escape_char(char *s
)
907 if (rlogin
!= _POSIX_VDISABLE
) {
908 rlogin
= (s
&& *s
) ? special(s
) : _POSIX_VDISABLE
;
909 printf("Telnet rlogin escape character is '%s'.\n",
912 escape
= (s
&& *s
) ? special(s
) : _POSIX_VDISABLE
;
913 printf("Telnet escape character is '%s'.\n", control(escape
));
918 setcmd(int argc
, char *argv
[])
922 struct togglelist
*c
;
924 if (argc
< 2 || argc
> 3) {
925 printf("Format is 'set Name Value'\n'set ?' for help.\n");
928 if ((argc
== 2) && (isprefix(argv
[1], "?") || isprefix(argv
[1], "help"))) {
929 for (ct
= Setlist
; ct
->name
; ct
++)
930 printf("%-15s %s\n", ct
->name
, ct
->help
);
933 printf("%-15s %s\n", "?", "display help information");
937 ct
= getset(argv
[1]);
939 c
= GETTOGGLE(argv
[1]);
941 fprintf(stderr
, "'%s': unknown argument ('set ?' for help).\n",
944 } else if (Ambiguous((void *)c
)) {
945 fprintf(stderr
, "'%s': ambiguous argument ('set ?' for help).\n",
950 if ((argc
== 2) || (strcmp("on", argv
[2]) == 0))
952 else if (strcmp("off", argv
[2]) == 0)
955 printf("Format is 'set togglename [on|off]'\n'set ?' for help.\n");
958 if (c
->actionexplanation
) {
959 printf("%s %s.\n", *c
->variable
? "Will" : "Won't",
960 c
->actionexplanation
);
965 } else if (argc
!= 3) {
966 printf("Format is 'set Name Value'\n'set ?' for help.\n");
968 } else if (Ambiguous((void *)ct
)) {
969 fprintf(stderr
, "'%s': ambiguous argument ('set ?' for help).\n",
972 } else if (ct
->handler
) {
973 (*ct
->handler
)(argv
[2]);
974 printf("%s set to \"%s\".\n", ct
->name
, (char *)ct
->charp
);
976 if (strcmp("off", argv
[2])) {
977 value
= special(argv
[2]);
979 value
= _POSIX_VDISABLE
;
981 *(ct
->charp
) = (cc_t
)value
;
982 printf("%s character is '%s'.\n", ct
->name
, control(*(ct
->charp
)));
989 unsetcmd(int argc
, char *argv
[])
992 struct togglelist
*c
;
997 "Need an argument to 'unset' command. 'unset ?' for help.\n");
1000 if (isprefix(argv
[1], "?") || isprefix(argv
[1], "help")) {
1001 for (ct
= Setlist
; ct
->name
; ct
++)
1002 printf("%-15s %s\n", ct
->name
, ct
->help
);
1005 printf("%-15s %s\n", "?", "display help information");
1015 c
= GETTOGGLE(name
);
1017 fprintf(stderr
, "'%s': unknown argument ('unset ?' for help).\n",
1020 } else if (Ambiguous((void *)c
)) {
1021 fprintf(stderr
, "'%s': ambiguous argument ('unset ?' for help).\n",
1027 if (c
->actionexplanation
) {
1028 printf("%s %s.\n", *c
->variable
? "Will" : "Won't",
1029 c
->actionexplanation
);
1034 } else if (Ambiguous((void *)ct
)) {
1035 fprintf(stderr
, "'%s': ambiguous argument ('unset ?' for help).\n",
1038 } else if (ct
->handler
) {
1040 printf("%s reset to \"%s\".\n", ct
->name
, (char *)ct
->charp
);
1042 *(ct
->charp
) = _POSIX_VDISABLE
;
1043 printf("%s character is '%s'.\n", ct
->name
, control(*(ct
->charp
)));
1050 * The following are the data structures and routines for the
1053 #ifdef KLUDGELINEMODE
1054 extern int kludgelinemode
;
1060 send_wont(TELOPT_LINEMODE
, 1);
1061 send_dont(TELOPT_SGA
, 1);
1062 send_dont(TELOPT_ECHO
, 1);
1070 #ifdef KLUDGELINEMODE
1072 send_dont(TELOPT_SGA
, 1);
1074 send_will(TELOPT_LINEMODE
, 1);
1075 send_dont(TELOPT_ECHO
, 1);
1082 #ifdef KLUDGELINEMODE
1084 send_do(TELOPT_SGA
, 1);
1087 send_wont(TELOPT_LINEMODE
, 1);
1088 send_do(TELOPT_ECHO
, 1);
1093 dolmmode(int bit
, int on
)
1096 extern int linemode
;
1098 if (my_want_state_is_wont(TELOPT_LINEMODE
)) {
1099 printf("?Need to have LINEMODE option enabled first.\n");
1100 printf("'mode ?' for help.\n");
1105 c
= (linemode
| bit
);
1107 c
= (linemode
& ~bit
);
1115 return dolmmode(bit
, 1);
1121 return dolmmode(bit
, 0);
1125 const char *name
; /* command name */
1126 const char *help
; /* help string */
1127 int (*handler
)(int);/* routine which executes command */
1128 int needconnect
; /* Do we need to be connected to execute? */
1132 static struct modelist ModeList
[] = {
1133 { "character", "Disable LINEMODE option", (int (*)(int))docharmode
, 1, 0 },
1134 #ifdef KLUDGELINEMODE
1135 { "", "(or disable obsolete line-by-line mode)", NULL
, 0, 0 },
1137 { "line", "Enable LINEMODE option", (int (*)(int))dolinemode
, 1, 0 },
1138 #ifdef KLUDGELINEMODE
1139 { "", "(or enable obsolete line-by-line mode)", NULL
, 0, 0 },
1141 { "", "", NULL
, 0, 0 },
1142 { "", "These require the LINEMODE option to be enabled", NULL
, 0, 0 },
1143 { "isig", "Enable signal trapping", setmod
, 1, MODE_TRAPSIG
},
1144 { "+isig", 0, setmod
, 1, MODE_TRAPSIG
},
1145 { "-isig", "Disable signal trapping", clearmode
, 1, MODE_TRAPSIG
},
1146 { "edit", "Enable character editing", setmod
, 1, MODE_EDIT
},
1147 { "+edit", 0, setmod
, 1, MODE_EDIT
},
1148 { "-edit", "Disable character editing", clearmode
, 1, MODE_EDIT
},
1149 { "softtabs", "Enable tab expansion", setmod
, 1, MODE_SOFT_TAB
},
1150 { "+softtabs", 0, setmod
, 1, MODE_SOFT_TAB
},
1151 { "-softtabs", "Disable character editing", clearmode
, 1, MODE_SOFT_TAB
},
1152 { "litecho", "Enable literal character echo", setmod
, 1, MODE_LIT_ECHO
},
1153 { "+litecho", 0, setmod
, 1, MODE_LIT_ECHO
},
1154 { "-litecho", "Disable literal character echo", clearmode
, 1, MODE_LIT_ECHO
},
1155 { "help", 0, (int (*)(int))modehelp
, 0, 0 },
1156 #ifdef KLUDGELINEMODE
1157 { "kludgeline", 0, (int (*)(int))dokludgemode
, 1, 0 },
1159 { "", "", NULL
, 0, 0 },
1160 { "?", "Print help information", (int (*)(int))modehelp
, 0, 0 },
1161 { NULL
, NULL
, NULL
, 0, 0 },
1168 struct modelist
*mt
;
1170 printf("format is: 'mode Mode', where 'Mode' is one of:\n\n");
1171 for (mt
= ModeList
; mt
->name
; mt
++) {
1174 printf("%-15s %s\n", mt
->name
, mt
->help
);
1182 #define GETMODECMD(name) (struct modelist *) \
1183 genget(name, (char **) ModeList, sizeof(struct modelist))
1186 modecmd(int argc
, char *argv
[])
1188 struct modelist
*mt
;
1191 printf("'mode' command requires an argument\n");
1192 printf("'mode ?' for help.\n");
1193 } else if ((mt
= GETMODECMD(argv
[1])) == 0) {
1194 fprintf(stderr
, "Unknown mode '%s' ('mode ?' for help).\n", argv
[1]);
1195 } else if (Ambiguous((void *)mt
)) {
1196 fprintf(stderr
, "Ambiguous mode '%s' ('mode ?' for help).\n", argv
[1]);
1197 } else if (mt
->needconnect
&& !connected
) {
1198 printf("?Need to be connected first.\n");
1199 printf("'mode ?' for help.\n");
1200 } else if (mt
->handler
) {
1201 return (*mt
->handler
)(mt
->arg1
);
1207 * The following data structures and routines implement the
1208 * "display" command.
1212 display(int argc
, char *argv
[])
1214 struct togglelist
*tl
;
1217 #define dotog(tl) if (tl->variable && tl->actionexplanation) { \
1218 if (*tl->variable) { \
1223 printf(" %s.\n", tl->actionexplanation); \
1226 #define doset(sl) if (sl->name && *sl->name != ' ') { \
1227 if (sl->handler == 0) \
1228 printf("%-15s [%s]\n", sl->name, control(*sl->charp)); \
1230 printf("%-15s \"%s\"\n", sl->name, (char *)sl->charp); \
1234 for (tl
= Togglelist
; tl
->name
; tl
++) {
1238 for (sl
= Setlist
; sl
->name
; sl
++) {
1244 for (i
= 1; i
< argc
; i
++) {
1245 sl
= getset(argv
[i
]);
1246 tl
= GETTOGGLE(argv
[i
]);
1247 if (Ambiguous((void *)sl
) || Ambiguous((void *)tl
)) {
1248 printf("?Ambiguous argument '%s'.\n", argv
[i
]);
1250 } else if (!sl
&& !tl
) {
1251 printf("?Unknown argument '%s'.\n", argv
[i
]);
1263 /*@*/optionstatus();
1266 #endif /* ENCRYPTION */
1273 * The following are the data structures, and many of the routines,
1274 * relating to command processing.
1278 * Set the escape character.
1281 setescape(int argc
, char *argv
[])
1287 "Deprecated usage - please use 'set escape%s%s' in the future.\n",
1288 (argc
> 2)? " ":"", (argc
> 2)? argv
[1]: "");
1292 printf("new escape character: ");
1293 (void) fgets(buf
, sizeof(buf
), stdin
);
1298 (void) fflush(stdout
);
1306 printf("Deprecated usage - please use 'toggle crmod' in the future.\n");
1307 printf("%s map carriage return on output.\n", crmod
? "Will" : "Won't");
1308 (void) fflush(stdout
);
1318 long oldrows
, oldcols
, newrows
, newcols
, err_
;
1320 err_
= (TerminalWindowSize(&oldrows
, &oldcols
) == 0) ? 1 : 0;
1321 (void) kill(0, SIGTSTP
);
1323 * If we didn't get the window size before the SUSPEND, but we
1324 * can get them now (?), then send the NAWS to make sure that
1325 * we are set up for the right window size.
1327 if (TerminalWindowSize(&newrows
, &newcols
) && connected
&&
1328 (err_
|| ((oldrows
!= newrows
) || (oldcols
!= newcols
)))) {
1332 /* reget parameters in case they were changed */
1333 TerminalSaveState();
1336 printf("Suspend is not supported. Try the '!' command instead\n");
1342 shell(int argc
, char *argv
[] __unused
)
1344 long oldrows
, oldcols
, newrows
, newcols
, err_
;
1348 err_
= (TerminalWindowSize(&oldrows
, &oldcols
) == 0) ? 1 : 0;
1351 perror("Fork failed\n");
1357 * Fire up the shell in the child.
1359 const char *shellp
, *shellname
;
1361 shellp
= getenv("SHELL");
1364 if ((shellname
= strrchr(shellp
, '/')) == 0)
1369 execl(shellp
, shellname
, "-c", &saveline
[1], NULL
);
1371 execl(shellp
, shellname
, NULL
);
1376 (void)wait(NULL
); /* Wait for the shell to complete */
1378 if (TerminalWindowSize(&newrows
, &newcols
) && connected
&&
1379 (err_
|| ((oldrows
!= newrows
) || (oldcols
!= newcols
)))) {
1388 bye(int argc
, char *argv
[])
1390 extern int resettermname
;
1393 (void) shutdown(net
, SHUT_RDWR
);
1394 printf("Connection closed.\n");
1395 (void) NetClose(net
);
1398 #ifdef AUTHENTICATION
1400 auth_encrypt_connect(connected
);
1406 if ((argc
!= 2) || (strcmp(argv
[1], "fromquit") != 0)) {
1407 longjmp(toplevel
, 1);
1410 return 1; /* Keep lint, etc., happy */
1416 (void) call(bye
, "bye", "fromquit", 0);
1423 send_do(TELOPT_LOGOUT
, 1);
1436 void (*handler
)(int);
1440 static void slc_help(void);
1442 struct slclist SlcList
[] = {
1443 { "export", "Use local special character definitions",
1444 (void (*)(int))slc_mode_export
, 0 },
1445 { "import", "Use remote special character definitions",
1446 slc_mode_import
, 1 },
1447 { "check", "Verify remote special character definitions",
1448 slc_mode_import
, 0 },
1449 { "help", NULL
, (void (*)(int))slc_help
, 0 },
1450 { "?", "Print help information", (void (*)(int))slc_help
, 0 },
1451 { NULL
, NULL
, NULL
, 0 },
1459 for (c
= SlcList
; c
->name
; c
++) {
1462 printf("%-15s %s\n", c
->name
, c
->help
);
1469 static struct slclist
*
1472 return (struct slclist
*)
1473 genget(name
, (char **) SlcList
, sizeof(struct slclist
));
1477 slccmd(int argc
, char *argv
[])
1483 "Need an argument to 'slc' command. 'slc ?' for help.\n");
1486 c
= getslc(argv
[1]);
1488 fprintf(stderr
, "'%s': unknown argument ('slc ?' for help).\n",
1492 if (Ambiguous((void *)c
)) {
1493 fprintf(stderr
, "'%s': ambiguous argument ('slc ?' for help).\n",
1497 (*c
->handler
)(c
->arg
);
1503 * The ENVIRON command.
1509 void (*handler
)(unsigned char *, unsigned char *);
1513 extern struct env_lst
*
1514 env_define(const unsigned char *, unsigned char *);
1516 env_undefine(unsigned char *),
1517 env_export(const unsigned char *),
1518 env_unexport(const unsigned char *),
1519 env_send(unsigned char *),
1520 #if defined(OLD_ENVIRON) && defined(ENV_HACK)
1521 env_varval(unsigned char *),
1527 struct envlist EnvList
[] = {
1528 { "define", "Define an environment variable",
1529 (void (*)(unsigned char *, unsigned char *))env_define
, 2 },
1530 { "undefine", "Undefine an environment variable",
1531 (void (*)(unsigned char *, unsigned char *))env_undefine
, 1 },
1532 { "export", "Mark an environment variable for automatic export",
1533 (void (*)(unsigned char *, unsigned char *))env_export
, 1 },
1534 { "unexport", "Don't mark an environment variable for automatic export",
1535 (void (*)(unsigned char *, unsigned char *))env_unexport
, 1 },
1536 { "send", "Send an environment variable", (void (*)(unsigned char *, unsigned char *))env_send
, 1 },
1537 { "list", "List the current environment variables",
1538 (void (*)(unsigned char *, unsigned char *))env_list
, 0 },
1539 #if defined(OLD_ENVIRON) && defined(ENV_HACK)
1540 { "varval", "Reverse VAR and VALUE (auto, right, wrong, status)",
1541 (void (*)(unsigned char *, unsigned char *))env_varval
, 1 },
1543 { "help", NULL
, (void (*)(unsigned char *, unsigned char *))env_help
, 0 },
1544 { "?", "Print help information", (void (*)(unsigned char *, unsigned char *))env_help
, 0 },
1545 { NULL
, NULL
, NULL
, 0 },
1553 for (c
= EnvList
; c
->name
; c
++) {
1556 printf("%-15s %s\n", c
->name
, c
->help
);
1563 static struct envlist
*
1564 getenvcmd(char *name
)
1566 return (struct envlist
*)
1567 genget(name
, (char **) EnvList
, sizeof(struct envlist
));
1571 env_cmd(int argc
, char *argv
[])
1577 "Need an argument to 'environ' command. 'environ ?' for help.\n");
1580 c
= getenvcmd(argv
[1]);
1582 fprintf(stderr
, "'%s': unknown argument ('environ ?' for help).\n",
1586 if (Ambiguous((void *)c
)) {
1587 fprintf(stderr
, "'%s': ambiguous argument ('environ ?' for help).\n",
1591 if (c
->narg
+ 2 != argc
) {
1593 "Need %s%d argument%s to 'environ %s' command. 'environ ?' for help.\n",
1594 c
->narg
< argc
+ 2 ? "only " : "",
1595 c
->narg
, c
->narg
== 1 ? "" : "s", c
->name
);
1598 (*c
->handler
)(argv
[2], argv
[3]);
1603 struct env_lst
*next
; /* pointer to next structure */
1604 struct env_lst
*prev
; /* pointer to previous structure */
1605 unsigned char *var
; /* pointer to variable name */
1606 unsigned char *value
; /* pointer to variable value */
1607 int export
; /* 1 -> export with default list of variables */
1608 int welldefined
; /* A well defined variable */
1611 struct env_lst envlisthead
;
1613 static struct env_lst
*
1614 env_find(const unsigned char *var
)
1618 for (ep
= envlisthead
.next
; ep
; ep
= ep
->next
) {
1619 if (strcmp(ep
->var
, var
) == 0)
1628 extern char **environ
;
1632 for (epp
= environ
; *epp
; epp
++) {
1633 if ((cp
= strchr(*epp
, '='))) {
1635 ep
= env_define((unsigned char *)*epp
,
1636 (unsigned char *)cp
+1);
1642 * Special case for DISPLAY variable. If it is ":0.0" or
1643 * "unix:0.0", we have to get rid of "unix" and insert our
1646 if ((ep
= env_find("DISPLAY"))
1647 && ((*ep
->value
== ':')
1648 || (strncmp((char *)ep
->value
, "unix:", 5) == 0))) {
1650 char *cp2
= strchr((char *)ep
->value
, ':');
1652 gethostname(hbuf
, 256);
1654 cp
= (char *)malloc(strlen(hbuf
) + strlen(cp2
) + 1);
1655 sprintf((char *)cp
, "%s%s", hbuf
, cp2
);
1657 ep
->value
= (unsigned char *)cp
;
1660 * If USER is not defined, but LOGNAME is, then add
1661 * USER with the value from LOGNAME. By default, we
1662 * don't export the USER variable.
1664 if ((env_find("USER") == NULL
) && (ep
= env_find("LOGNAME"))) {
1665 env_define("USER", ep
->value
);
1666 env_unexport("USER");
1668 env_export("DISPLAY");
1669 env_export("PRINTER");
1673 env_define(const unsigned char *var
, unsigned char *value
)
1677 if ((ep
= env_find(var
))) {
1683 ep
= (struct env_lst
*)malloc(sizeof(struct env_lst
));
1684 ep
->next
= envlisthead
.next
;
1685 envlisthead
.next
= ep
;
1686 ep
->prev
= &envlisthead
;
1688 ep
->next
->prev
= ep
;
1690 ep
->welldefined
= opt_welldefined(var
);
1692 ep
->var
= strdup(var
);
1693 ep
->value
= strdup(value
);
1698 env_undefine(unsigned char *var
)
1702 if ((ep
= env_find(var
))) {
1703 ep
->prev
->next
= ep
->next
;
1705 ep
->next
->prev
= ep
->prev
;
1715 env_export(const unsigned char *var
)
1719 if ((ep
= env_find(var
)))
1724 env_unexport(const unsigned char *var
)
1728 if ((ep
= env_find(var
)))
1733 env_send(unsigned char *var
)
1737 if (my_state_is_wont(TELOPT_NEW_ENVIRON
)
1739 && my_state_is_wont(TELOPT_OLD_ENVIRON
)
1743 "Cannot send '%s': Telnet ENVIRON option not enabled\n",
1749 fprintf(stderr
, "Cannot send '%s': variable not defined\n",
1753 env_opt_start_info();
1754 env_opt_add(ep
->var
);
1763 for (ep
= envlisthead
.next
; ep
; ep
= ep
->next
) {
1764 printf("%c %-20s %s\n", ep
->export
? '*' : ' ',
1765 ep
->var
, ep
->value
);
1770 env_default(int init
, int welldefined
)
1772 static struct env_lst
*nep
= NULL
;
1779 while ((nep
= nep
->next
)) {
1780 if (nep
->export
&& (nep
->welldefined
== welldefined
))
1788 env_getvalue(const unsigned char *var
)
1792 if ((ep
= env_find(var
)))
1797 #if defined(OLD_ENVIRON) && defined(ENV_HACK)
1799 env_varval(unsigned char *what
)
1801 extern int old_env_var
, old_env_value
, env_auto
;
1802 int len
= strlen((char *)what
);
1807 if (strncasecmp((char *)what
, "status", len
) == 0) {
1809 printf("%s%s", "VAR and VALUE are/will be ",
1810 "determined automatically\n");
1811 if (old_env_var
== OLD_ENV_VAR
)
1812 printf("VAR and VALUE set to correct definitions\n");
1814 printf("VAR and VALUE definitions are reversed\n");
1815 } else if (strncasecmp((char *)what
, "auto", len
) == 0) {
1817 old_env_var
= OLD_ENV_VALUE
;
1818 old_env_value
= OLD_ENV_VAR
;
1819 } else if (strncasecmp((char *)what
, "right", len
) == 0) {
1821 old_env_var
= OLD_ENV_VAR
;
1822 old_env_value
= OLD_ENV_VALUE
;
1823 } else if (strncasecmp((char *)what
, "wrong", len
) == 0) {
1825 old_env_var
= OLD_ENV_VALUE
;
1826 old_env_value
= OLD_ENV_VAR
;
1829 printf("Unknown \"varval\" command. (\"auto\", \"right\", \"wrong\", \"status\")\n");
1834 #ifdef AUTHENTICATION
1836 * The AUTHENTICATE command.
1842 int (*handler
)(char *);
1847 auth_enable(char *),
1848 auth_disable(char *),
1853 struct authlist AuthList
[] = {
1854 { "status", "Display current status of authentication information",
1855 (int (*)(char *))auth_status
, 0 },
1856 { "disable", "Disable an authentication type ('auth disable ?' for more)",
1858 { "enable", "Enable an authentication type ('auth enable ?' for more)",
1860 { "help", NULL
, (int (*)(char *))auth_help
, 0 },
1861 { "?", "Print help information", (int (*)(char *))auth_help
, 0 },
1862 { NULL
, NULL
, NULL
, 0 },
1870 for (c
= AuthList
; c
->name
; c
++) {
1873 printf("%-15s %s\n", c
->name
, c
->help
);
1882 auth_cmd(int argc
, char *argv
[])
1888 "Need an argument to 'auth' command. 'auth ?' for help.\n");
1892 c
= (struct authlist
*)
1893 genget(argv
[1], (char **) AuthList
, sizeof(struct authlist
));
1895 fprintf(stderr
, "'%s': unknown argument ('auth ?' for help).\n",
1899 if (Ambiguous((void *)c
)) {
1900 fprintf(stderr
, "'%s': ambiguous argument ('auth ?' for help).\n",
1904 if (c
->narg
+ 2 != argc
) {
1906 "Need %s%d argument%s to 'auth %s' command. 'auth ?' for help.\n",
1907 c
->narg
< argc
+ 2 ? "only " : "",
1908 c
->narg
, c
->narg
== 1 ? "" : "s", c
->name
);
1911 return((*c
->handler
)(argv
[2]));
1917 * The ENCRYPT command.
1920 struct encryptlist
{
1923 int (*handler
)(char *, char *);
1930 EncryptEnable(char *, char *),
1931 EncryptDisable(char *, char *),
1932 EncryptType(char *, char *),
1933 EncryptStart(char *),
1934 EncryptStartInput(void),
1935 EncryptStartOutput(void),
1936 EncryptStop(char *),
1937 EncryptStopInput(void),
1938 EncryptStopOutput(void),
1939 EncryptStatus(void);
1943 struct encryptlist EncryptList
[] = {
1944 { "enable", "Enable encryption. ('encrypt enable ?' for more)",
1945 EncryptEnable
, 1, 1, 2 },
1946 { "disable", "Disable encryption. ('encrypt enable ?' for more)",
1947 EncryptDisable
, 0, 1, 2 },
1948 { "type", "Set encryption type. ('encrypt type ?' for more)",
1949 EncryptType
, 0, 1, 1 },
1950 { "start", "Start encryption. ('encrypt start ?' for more)",
1951 (int (*)(char *, char *))EncryptStart
, 1, 0, 1 },
1952 { "stop", "Stop encryption. ('encrypt stop ?' for more)",
1953 (int (*)(char *, char *))EncryptStop
, 1, 0, 1 },
1954 { "input", "Start encrypting the input stream",
1955 (int (*)(char *, char *))EncryptStartInput
, 1, 0, 0 },
1956 { "-input", "Stop encrypting the input stream",
1957 (int (*)(char *, char *))EncryptStopInput
, 1, 0, 0 },
1958 { "output", "Start encrypting the output stream",
1959 (int (*)(char *, char *))EncryptStartOutput
, 1, 0, 0 },
1960 { "-output", "Stop encrypting the output stream",
1961 (int (*)(char *, char *))EncryptStopOutput
, 1, 0, 0 },
1963 { "status", "Display current status of authentication information",
1964 (int (*)(char *, char *))EncryptStatus
, 0, 0, 0 },
1965 { "help", NULL
, (int (*)(char *, char *))EncryptHelp
, 0, 0, 0 },
1966 { "?", "Print help information", (int (*)(char *, char *))EncryptHelp
, 0, 0, 0 },
1967 { NULL
, NULL
, NULL
, 0, 0, 0 },
1973 struct encryptlist
*c
;
1975 for (c
= EncryptList
; c
->name
; c
++) {
1978 printf("%-15s %s\n", c
->name
, c
->help
);
1987 encrypt_cmd(int argc
, char *argv
[])
1989 struct encryptlist
*c
;
1993 "Need an argument to 'encrypt' command. 'encrypt ?' for help.\n");
1997 c
= (struct encryptlist
*)
1998 genget(argv
[1], (char **) EncryptList
, sizeof(struct encryptlist
));
2000 fprintf(stderr
, "'%s': unknown argument ('encrypt ?' for help).\n",
2004 if (Ambiguous((void *)c
)) {
2005 fprintf(stderr
, "'%s': ambiguous argument ('encrypt ?' for help).\n",
2010 if (argc
< c
->minarg
|| argc
> c
->maxarg
) {
2011 if (c
->minarg
== c
->maxarg
) {
2012 fprintf(stderr
, "Need %s%d argument%s ",
2013 c
->minarg
< argc
? "only " : "", c
->minarg
,
2014 c
->minarg
== 1 ? "" : "s");
2016 fprintf(stderr
, "Need %s%d-%d arguments ",
2017 c
->maxarg
< argc
? "only " : "", c
->minarg
, c
->maxarg
);
2019 fprintf(stderr
, "to 'encrypt %s' command. 'encrypt ?' for help.\n",
2023 if (c
->needconnect
&& !connected
) {
2024 if (!(argc
&& (isprefix(argv
[2], "help") || isprefix(argv
[2], "?")))) {
2025 printf("?Need to be connected first.\n");
2029 return ((*c
->handler
)(argc
> 0 ? argv
[2] : 0,
2030 argc
> 1 ? argv
[3] : 0));
2032 #endif /* ENCRYPTION */
2035 * Print status about the connection.
2039 status(int argc
, char *argv
[])
2042 printf("Connected to %s.\n", hostname
);
2043 if ((argc
< 2) || strcmp(argv
[1], "notmuch")) {
2044 int mode
= getconnmode();
2046 if (my_want_state_is_will(TELOPT_LINEMODE
)) {
2047 printf("Operating with LINEMODE option\n");
2048 printf("%s line editing\n", (mode
&MODE_EDIT
) ? "Local" : "No");
2049 printf("%s catching of signals\n",
2050 (mode
&MODE_TRAPSIG
) ? "Local" : "No");
2052 #ifdef KLUDGELINEMODE
2053 } else if (kludgelinemode
&& my_want_state_is_dont(TELOPT_SGA
)) {
2054 printf("Operating in obsolete linemode\n");
2057 printf("Operating in single character mode\n");
2059 printf("Catching signals locally\n");
2061 printf("%s character echo\n", (mode
&MODE_ECHO
) ? "Local" : "Remote");
2062 if (my_want_state_is_will(TELOPT_LFLOW
))
2063 printf("%s flow control\n", (mode
&MODE_FLOW
) ? "Local" : "No");
2066 #endif /* ENCRYPTION */
2069 printf("No connection.\n");
2071 printf("Escape character is '%s'.\n", control(escape
));
2072 (void) fflush(stdout
);
2078 * Function that gets called when SIGINFO is received.
2083 (void) call(status
, "status", "notmuch", 0);
2088 sockaddr_ntop(struct sockaddr
*sa
)
2091 static char addrbuf
[INET6_ADDRSTRLEN
];
2093 switch (sa
->sa_family
) {
2095 addr
= &((struct sockaddr_in
*)sa
)->sin_addr
;
2098 addr
= &((struct sockaddr_un
*)sa
)->sun_path
;
2102 addr
= &((struct sockaddr_in6
*)sa
)->sin6_addr
;
2108 inet_ntop(sa
->sa_family
, addr
, addrbuf
, sizeof(addrbuf
));
2112 #if defined(IPSEC) && defined(IPSEC_POLICY_IPSEC)
2114 setpolicy(int lnet
, struct addrinfo
*res
, char *policy
)
2123 buf
= ipsec_set_policy(policy
, strlen(policy
));
2125 printf("%s\n", ipsec_strerror());
2128 level
= res
->ai_family
== AF_INET
? IPPROTO_IP
: IPPROTO_IPV6
;
2129 optname
= res
->ai_family
== AF_INET
? IP_IPSEC_POLICY
: IPV6_IPSEC_POLICY
;
2130 if (setsockopt(lnet
, level
, optname
, buf
, ipsec_get_policylen(buf
)) < 0){
2131 perror("setsockopt");
2142 * When an Address Family related error happend, check if retry with
2143 * another AF is possible or not.
2144 * Return 1, if retry with another af is OK. Else, return 0.
2147 switch_af(struct addrinfo
**aip
)
2150 struct addrinfo
*ai
;
2153 nextaf
= (ai
->ai_family
== AF_INET
) ? AF_INET6
: AF_INET
;
2156 while (ai
!= NULL
&& ai
->ai_family
!= nextaf
);
2166 tn(int argc
, char *argv
[])
2171 int srcroute
= 0, result
;
2172 char *cmd
, *hostp
= 0, *portp
= 0, *user
= 0;
2173 char *src_addr
= NULL
;
2174 struct addrinfo hints
, *res
, *res0
= NULL
, *src_res
, *src_res0
= NULL
;
2175 int error
= 0, af_error
= 0;
2178 printf("?Already connected to %s\n", hostname
);
2183 (void) strcpy(line
, "open ");
2185 (void) fgets(&line
[strlen(line
)], sizeof(line
) - strlen(line
), stdin
);
2193 if (strcmp(*argv
, "help") == 0 || isprefix(*argv
, "?"))
2195 if (strcmp(*argv
, "-l") == 0) {
2203 if (strcmp(*argv
, "-a") == 0) {
2208 if (strcmp(*argv
, "-s") == 0) {
2227 printf("usage: %s [-l user] [-a] [-s src_addr] host-name [port]\n", cmd
);
2234 if (src_addr
!= NULL
) {
2235 memset(&hints
, 0, sizeof(hints
));
2236 hints
.ai_flags
= AI_NUMERICHOST
;
2237 hints
.ai_family
= family
;
2238 hints
.ai_socktype
= SOCK_STREAM
;
2239 error
= getaddrinfo(src_addr
, 0, &hints
, &src_res
);
2240 if (error
== EAI_NODATA
) {
2242 error
= getaddrinfo(src_addr
, 0, &hints
, &src_res
);
2245 fprintf(stderr
, "%s: %s\n", src_addr
, gai_strerror(error
));
2246 if (error
== EAI_SYSTEM
)
2247 fprintf(stderr
, "%s: %s\n", src_addr
, strerror(errno
));
2253 if (hostp
[0] == '/') {
2254 struct sockaddr_un su
;
2256 if (strlen(hostp
) >= sizeof(su
.sun_path
)) {
2257 fprintf(stderr
, "hostname too long for unix domain socket: %s",
2261 memset(&su
, 0, sizeof su
);
2262 su
.sun_family
= AF_UNIX
;
2263 strncpy(su
.sun_path
, hostp
, sizeof su
.sun_path
);
2264 printf("Trying %s...\n", hostp
);
2265 net
= socket(PF_UNIX
, SOCK_STREAM
, 0);
2270 if (connect(net
, (struct sockaddr
*)&su
, sizeof su
) == -1) {
2271 perror(su
.sun_path
);
2272 (void) NetClose(net
);
2276 } else if (hostp
[0] == '@' || hostp
[0] == '!') {
2279 family
== AF_INET6
||
2281 (hostname
= strrchr(hostp
, ':')) == NULL
)
2282 hostname
= strrchr(hostp
, '@');
2283 if (hostname
== NULL
) {
2293 portp
= strdup("telnet");
2294 } else if (*portp
== '-') {
2300 memset(&hints
, 0, sizeof(hints
));
2301 hints
.ai_flags
= AI_NUMERICHOST
;
2302 hints
.ai_family
= family
;
2303 hints
.ai_socktype
= SOCK_STREAM
;
2304 error
= getaddrinfo(hostname
, portp
, &hints
, &res
);
2306 hints
.ai_flags
= AI_CANONNAME
;
2307 error
= getaddrinfo(hostname
, portp
, &hints
, &res
);
2310 fprintf(stderr
, "%s: %s\n", hostname
, gai_strerror(error
));
2311 if (error
== EAI_SYSTEM
)
2312 fprintf(stderr
, "%s: %s\n", hostname
, strerror(errno
));
2316 if (hints
.ai_flags
== AI_NUMERICHOST
) {
2317 /* hostname has numeric */
2321 gni_err
= getnameinfo(res
->ai_addr
, res
->ai_addr
->sa_len
,
2322 _hostname
, sizeof(_hostname
) - 1, NULL
, 0,
2325 (void) strncpy(_hostname
, hostp
, sizeof(_hostname
) - 1);
2326 _hostname
[sizeof(_hostname
)-1] = '\0';
2327 hostname
= _hostname
;
2329 /* hostname has FQDN */
2331 (void) strncpy(_hostname
, hostname
, sizeof(_hostname
) - 1);
2332 else if (res
->ai_canonname
!= NULL
)
2333 strcpy(_hostname
, res
->ai_canonname
);
2335 (void) strncpy(_hostname
, hostp
, sizeof(_hostname
) - 1);
2336 _hostname
[sizeof(_hostname
)-1] = '\0';
2337 hostname
= _hostname
;
2343 if (srcroute
!= 0) {
2344 static char hostbuf
[BUFSIZ
];
2346 if (af_error
== 0) { /* save intermediate hostnames for retry */
2347 strncpy(hostbuf
, hostp
, BUFSIZ
- 1);
2348 hostbuf
[BUFSIZ
- 1] = '\0';
2352 result
= sourceroute(res
, hostp
, &srp
, &srlen
, &proto
, &opt
);
2355 if (family
== AF_UNSPEC
&& af_error
== 0 &&
2356 switch_af(&res
) == 1) {
2363 } else if (result
== -1) {
2364 printf("Bad source route option: %s\n", hostp
);
2370 printf("Trying %s...\n", sockaddr_ntop(res
->ai_addr
));
2371 net
= socket(res
->ai_family
, res
->ai_socktype
, res
->ai_protocol
);
2375 if (family
== AF_UNSPEC
&& af_error
== 0 &&
2376 switch_af(&res
) == 1) {
2381 perror("telnet: socket");
2384 if (srp
&& setsockopt(net
, proto
, opt
, (char *)srp
, srlen
) < 0)
2385 perror("setsockopt (source route)");
2386 #if defined(IPPROTO_IP) && defined(IP_TOS)
2387 if (res
->ai_family
== PF_INET
) {
2388 # if defined(HAS_GETTOS)
2390 if (tos
< 0 && (tp
= gettosbyname("telnet", "tcp")))
2394 tos
= IPTOS_LOWDELAY
;
2396 && (setsockopt(net
, IPPROTO_IP
, IP_TOS
,
2397 (char *)&tos
, sizeof(int)) < 0)
2398 && (errno
!= ENOPROTOOPT
))
2399 perror("telnet: setsockopt (IP_TOS) (ignored)");
2401 #endif /* defined(IPPROTO_IP) && defined(IP_TOS) */
2403 if (debug
&& SetSockOpt(net
, SOL_SOCKET
, SO_DEBUG
, 1) < 0) {
2404 perror("setsockopt (SO_DEBUG)");
2407 if (src_addr
!= NULL
) {
2408 for (src_res
= src_res0
; src_res
!= 0; src_res
= src_res
->ai_next
)
2409 if (src_res
->ai_family
== res
->ai_family
)
2411 if (src_res
== NULL
)
2413 if (bind(net
, src_res
->ai_addr
, src_res
->ai_addrlen
) == -1) {
2415 if (family
== AF_UNSPEC
&& af_error
== 0 &&
2416 switch_af(&res
) == 1) {
2418 (void) NetClose(net
);
2423 (void) NetClose(net
);
2427 #if defined(IPSEC) && defined(IPSEC_POLICY_IPSEC)
2428 if (setpolicy(net
, res
, ipsec_policy_in
) < 0) {
2429 (void) NetClose(net
);
2432 if (setpolicy(net
, res
, ipsec_policy_out
) < 0) {
2433 (void) NetClose(net
);
2438 if (connect(net
, res
->ai_addr
, res
->ai_addrlen
) < 0) {
2439 struct addrinfo
*next
;
2441 next
= res
->ai_next
;
2442 /* If already an af failed, only try same af. */
2444 while (next
!= NULL
&& next
->ai_family
!= res
->ai_family
)
2445 next
= next
->ai_next
;
2446 warn("connect to address %s", sockaddr_ntop(res
->ai_addr
));
2449 (void) NetClose(net
);
2452 warnx("Unable to connect to remote host");
2453 (void) NetClose(net
);
2457 #ifdef AUTHENTICATION
2459 auth_encrypt_connect(connected
);
2462 } while (connected
== 0);
2464 if (src_res0
!= NULL
)
2465 freeaddrinfo(src_res0
);
2466 cmdrc(hostp
, hostname
);
2468 if (autologin
&& user
== NULL
) {
2471 user
= getenv("USER");
2473 ((pw
= getpwnam(user
)) && pw
->pw_uid
!= getuid())) {
2474 if ((pw
= getpwuid(getuid())))
2481 env_define("USER", user
);
2484 (void) call(status
, "status", "notmuch", 0);
2485 if (setjmp(peerdied
) == 0)
2487 (void) NetClose(net
);
2488 ExitString("Connection closed by foreign host.\n",1);
2493 if (src_res0
!= NULL
)
2494 freeaddrinfo(src_res0
);
2498 #define HELPINDENT (sizeof ("connect"))
2501 openhelp
[] = "connect to a site",
2502 closehelp
[] = "close current connection",
2503 logouthelp
[] = "forcibly logout remote user and close the connection",
2504 quithelp
[] = "exit telnet",
2505 statushelp
[] = "print status information",
2506 helphelp
[] = "print help information",
2507 sendhelp
[] = "transmit special characters ('send ?' for more)",
2508 sethelp
[] = "set operating parameters ('set ?' for more)",
2509 unsethelp
[] = "unset operating parameters ('unset ?' for more)",
2510 togglestring
[] ="toggle operating parameters ('toggle ?' for more)",
2511 slchelp
[] = "change state of special characters ('slc ?' for more)",
2512 displayhelp
[] = "display operating parameters",
2513 #ifdef AUTHENTICATION
2514 authhelp
[] = "turn on (off) authentication ('auth ?' for more)",
2517 encrypthelp
[] = "turn on (off) encryption ('encrypt ?' for more)",
2518 #endif /* ENCRYPTION */
2519 zhelp
[] = "suspend telnet",
2521 opiehelp
[] = "compute response to OPIE challenge",
2523 shellhelp
[] = "invoke a subshell",
2524 envhelp
[] = "change environment variables ('environ ?' for more)",
2525 modestring
[] = "try to enter line or character mode ('mode ?' for more)";
2527 static Command cmdtab
[] = {
2528 { "close", closehelp
, bye
, 1 },
2529 { "logout", logouthelp
, (int (*)(int, char **))logout
, 1 },
2530 { "display", displayhelp
, display
, 0 },
2531 { "mode", modestring
, modecmd
, 0 },
2532 { "telnet", openhelp
, tn
, 0 },
2533 { "open", openhelp
, tn
, 0 },
2534 { "quit", quithelp
, (int (*)(int, char **))quit
, 0 },
2535 { "send", sendhelp
, sendcmd
, 0 },
2536 { "set", sethelp
, setcmd
, 0 },
2537 { "unset", unsethelp
, unsetcmd
, 0 },
2538 { "status", statushelp
, status
, 0 },
2539 { "toggle", togglestring
, toggle
, 0 },
2540 { "slc", slchelp
, slccmd
, 0 },
2541 #ifdef AUTHENTICATION
2542 { "auth", authhelp
, auth_cmd
, 0 },
2545 { "encrypt", encrypthelp
, encrypt_cmd
, 0 },
2546 #endif /* ENCRYPTION */
2547 { "z", zhelp
, (int (*)(int, char **))suspend
, 0 },
2548 { "!", shellhelp
, shell
, 1 },
2549 { "environ", envhelp
, env_cmd
, 0 },
2550 { "?", helphelp
, help
, 0 },
2552 { "opie", opiehelp
, opie_calc
, 0 },
2554 { NULL
, NULL
, NULL
, 0 }
2557 static char crmodhelp
[] = "deprecated command -- use 'toggle crmod' instead";
2558 static char escapehelp
[] = "deprecated command -- use 'set escape' instead";
2560 static Command cmdtab2
[] = {
2561 { "help", 0, help
, 0 },
2562 { "escape", escapehelp
, setescape
, 0 },
2563 { "crmod", crmodhelp
, (int (*)(int, char **))togcrmod
, 0 },
2564 { NULL
, NULL
, NULL
, 0 }
2569 * Call routine with argc, argv set from args (terminated by 0).
2573 call(intrtn_t routine
, ...)
2579 va_start(ap
, routine
);
2580 while ((args
[argno
++] = va_arg(ap
, char *)) != 0);
2582 return (*routine
)(argno
-1, args
);
2591 if ((cm
= (Command
*) genget(name
, (char **) cmdtab
, sizeof(Command
))))
2593 return (Command
*) genget(name
, (char **) cmdtab2
, sizeof(Command
));
2597 command(int top
, const char *tbuf
, int cnt
)
2605 (void) signal(SIGINT
, SIG_DFL
);
2606 (void) signal(SIGQUIT
, SIG_DFL
);
2609 if (rlogin
== _POSIX_VDISABLE
)
2610 printf("%s> ", prompt
);
2614 while (cnt
> 0 && (*cp
++ = *tbuf
++) != '\n')
2617 if (cp
== line
|| *--cp
!= '\n' || cp
== line
)
2620 if (rlogin
== _POSIX_VDISABLE
)
2621 printf("%s\n", line
);
2624 if (rlogin
!= _POSIX_VDISABLE
)
2625 printf("%s> ", prompt
);
2626 if (fgets(line
, sizeof(line
), stdin
) == NULL
) {
2627 if (feof(stdin
) || ferror(stdin
)) {
2637 if (margv
[0] == 0) {
2640 c
= getcmd(margv
[0]);
2641 if (Ambiguous((void *)c
)) {
2642 printf("?Ambiguous command\n");
2646 printf("?Invalid command\n");
2649 if (c
->needconnect
&& !connected
) {
2650 printf("?Need to be connected first.\n");
2653 if ((*c
->handler
)(margc
, margv
)) {
2659 longjmp(toplevel
, 1);
2670 help(int argc
, char *argv
[])
2675 printf("Commands may be abbreviated. Commands are:\n\n");
2676 for (c
= cmdtab
; c
->name
; c
++)
2678 printf("%-*s\t%s\n", (int)HELPINDENT
, c
->name
,
2683 else while (--argc
> 0) {
2687 if (Ambiguous((void *)c
))
2688 printf("?Ambiguous help command %s\n", arg
);
2690 printf("?Invalid help command %s\n", arg
);
2692 printf("%s\n", c
->help
);
2697 static char *rcname
= 0;
2698 static char rcbuf
[128];
2701 cmdrc(char *m1
, char *m2
)
2706 int l1
= strlen(m1
);
2707 int l2
= strlen(m2
);
2708 char m1save
[MAXHOSTNAMELEN
];
2713 strlcpy(m1save
, m1
, sizeof(m1save
));
2717 rcname
= getenv("HOME");
2718 if (rcname
&& (strlen(rcname
) + 10) < sizeof(rcbuf
))
2719 strcpy(rcbuf
, rcname
);
2722 strcat(rcbuf
, "/.telnetrc");
2726 if ((rcfile
= fopen(rcname
, "r")) == 0) {
2731 if (fgets(line
, sizeof(line
), rcfile
) == NULL
)
2738 if (!isspace(line
[0]))
2741 if (gotmachine
== 0) {
2742 if (isspace(line
[0]))
2744 if (strncasecmp(line
, m1
, l1
) == 0)
2745 strncpy(line
, &line
[l1
], sizeof(line
) - l1
);
2746 else if (strncasecmp(line
, m2
, l2
) == 0)
2747 strncpy(line
, &line
[l2
], sizeof(line
) - l2
);
2748 else if (strncasecmp(line
, "DEFAULT", 7) == 0)
2749 strncpy(line
, &line
[7], sizeof(line
) - 7);
2752 if (line
[0] != ' ' && line
[0] != '\t' && line
[0] != '\n')
2759 c
= getcmd(margv
[0]);
2760 if (Ambiguous((void *)c
)) {
2761 printf("?Ambiguous command: %s\n", margv
[0]);
2765 printf("?Invalid command: %s\n", margv
[0]);
2769 * This should never happen...
2771 if (c
->needconnect
&& !connected
) {
2772 printf("?Need to be connected first for %s.\n", margv
[0]);
2775 (*c
->handler
)(margc
, margv
);
2781 * Source route is handed in as
2782 * [!]@hop1@hop2...[@|:]dst
2783 * If the leading ! is present, it is a
2784 * strict source route, otherwise it is
2785 * assmed to be a loose source route.
2787 * We fill in the source route option as
2788 * hop1,hop2,hop3...dest
2789 * and return a pointer to hop1, which will
2790 * be the address to connect() to.
2794 * res: ponter to addrinfo structure which contains sockaddr to
2795 * the host to connect to.
2797 * arg: pointer to route list to decipher
2799 * cpp: If *cpp is not equal to NULL, this is a
2800 * pointer to a pointer to a character array
2801 * that should be filled in with the option.
2803 * lenp: pointer to an integer that contains the
2804 * length of *cpp if *cpp != NULL.
2806 * protop: pointer to an integer that should be filled in with
2807 * appropriate protocol for setsockopt, as socket
2810 * optp: pointer to an integer that should be filled in with
2811 * appropriate option for setsockopt, as socket protocol
2816 * If the return value is 1, then all operations are
2817 * successful. If the
2818 * return value is -1, there was a syntax error in the
2819 * option, either unknown characters, or too many hosts.
2820 * If the return value is 0, one of the hostnames in the
2821 * path is unknown, and *cpp is set to point to the bad
2824 * *cpp: If *cpp was equal to NULL, it will be filled
2825 * in with a pointer to our static area that has
2826 * the option filled in. This will be 32bit aligned.
2828 * *lenp: This will be filled in with how long the option
2829 * pointed to by *cpp is.
2831 * *protop: This will be filled in with appropriate protocol for
2832 * setsockopt, as socket protocol family.
2834 * *optp: This will be filled in with appropriate option for
2835 * setsockopt, as socket protocol family.
2838 sourceroute(struct addrinfo
*ai
, char *arg
, char **cpp
, int *lenp
, int *protop
, int *optp
)
2840 static char buf
[1024 + ALIGNBYTES
]; /*XXX*/
2841 char *cp
, *cp2
, *lsrp
, *ep
;
2842 struct sockaddr_in
*_sin
;
2844 struct sockaddr_in6
*sin6
;
2845 struct cmsghdr
*cmsg
;
2846 struct ip6_rthdr
*rth
;
2848 struct addrinfo hints
, *res
;
2853 * Verify the arguments, and make sure we have
2854 * at least 7 bytes for the option.
2856 if (cpp
== NULL
|| lenp
== NULL
)
2859 switch (res
->ai_family
) {
2866 if (*lenp
< (int)CMSG_SPACE(sizeof(struct ip6_rthdr
) +
2867 sizeof(struct in6_addr
)))
2874 * Decide whether we have a buffer passed to us,
2875 * or if we need to use our own static buffer.
2881 *cpp
= lsrp
= (char *)ALIGN(buf
);
2888 if (ai
->ai_family
== AF_INET6
) {
2890 * RFC3542 has obsoleted IPV6_PKTOPTIONS socket option.
2892 #ifdef COMPAT_RFC1883 /* XXX */
2896 *protop
= IPPROTO_IPV6
;
2897 *optp
= IPV6_PKTOPTIONS
;
2900 #endif /* COMPAT_RFC1883 */
2905 * Next, decide whether we have a loose source
2906 * route or a strict source route, and fill in
2907 * the begining of the option.
2911 *lsrp
++ = IPOPT_SSRR
;
2913 *lsrp
++ = IPOPT_LSRR
;
2918 lsrp
++; /* skip over length, we'll fill it in later */
2920 *protop
= IPPROTO_IP
;
2925 memset(&hints
, 0, sizeof(hints
));
2926 hints
.ai_family
= ai
->ai_family
;
2927 hints
.ai_socktype
= SOCK_STREAM
;
2931 ai
->ai_family
!= AF_INET6
&&
2935 else for (cp2
= cp
; (c
= *cp2
); cp2
++) {
2940 } else if (c
== '@') {
2944 ai
->ai_family
!= AF_INET6
&&
2955 hints
.ai_flags
= AI_NUMERICHOST
;
2956 error
= getaddrinfo(cp
, NULL
, &hints
, &res
);
2957 if (error
== EAI_NODATA
) {
2959 error
= getaddrinfo(cp
, NULL
, &hints
, &res
);
2962 fprintf(stderr
, "%s: %s\n", cp
, gai_strerror(error
));
2963 if (error
== EAI_SYSTEM
)
2964 fprintf(stderr
, "%s: %s\n", cp
,
2970 if (res
->ai_family
== AF_INET6
) {
2975 _sin
= (struct sockaddr_in
*)res
->ai_addr
;
2976 memcpy(lsrp
, (char *)&_sin
->sin_addr
, 4);
2984 * Check to make sure there is space for next address
2987 #ifdef COMPAT_RFC1883 /* XXX */
2988 if (res
->ai_family
== AF_INET6
) {
2989 if (((char *)CMSG_DATA(cmsg
) +
2990 sizeof(struct ip6_rthdr
)) > ep
)
2993 #endif /* COMPAT_RFC1883 */
3000 if (res
->ai_family
== AF_INET6
) {
3001 #ifdef COMPAT_RFC1883 /* XXX */
3003 #endif /* COMPAT_RFC1883 */
3007 if ((*(*cpp
+IPOPT_OLEN
) = lsrp
- *cpp
) <= 7) {
3012 *lsrp
++ = IPOPT_NOP
; /* 32 bit word align it */
3013 *lenp
= lsrp
- *cpp
;