2 * Copyright (c) 1988, 1990, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#)commands.c 8.4 (Berkeley) 5/30/95
30 * $FreeBSD: src/crypto/telnet/telnet/commands.c,v 1.12.2.7 2003/04/23 07:16:32 ru Exp $
33 #include <sys/param.h>
36 #include <sys/socket.h>
38 #include <netinet/in.h>
52 #include <arpa/telnet.h>
53 #include <arpa/inet.h>
65 #include <libtelnet/auth.h>
68 #include <libtelnet/encrypt.h>
71 #include <netinet/in_systm.h>
72 #include <netinet/ip.h>
73 #include <netinet/ip6.h>
75 #ifndef MAXHOSTNAMELEN
76 #define MAXHOSTNAMELEN 256
79 typedef int (*intrtn_t
)(int, char **);
82 extern int auth_togdebug(int);
85 extern int EncryptAutoEnc(int);
86 extern int EncryptAutoDec(int);
87 extern int EncryptDebug(int);
88 extern int EncryptVerbose(int);
89 #endif /* ENCRYPTION */
90 #if defined(IPPROTO_IP) && defined(IP_TOS)
92 #endif /* defined(IPPROTO_IP) && defined(IP_TOS) */
95 static char _hostname
[MAXHOSTNAMELEN
];
97 static int help(int, char **);
98 static int call(intrtn_t
, ...);
99 static void cmdrc(const char *, const char *);
101 static int switch_af(struct addrinfo
**);
103 static int togglehelp(void);
104 static int send_tncmd(void (*)(int, int), const char *, char *);
105 static int setmod(int);
106 static int clearmode(int);
107 static int modehelp(void);
108 static int sourceroute(struct addrinfo
*, char *, char **, int *, int *, int *);
111 const char *name
; /* command name */
112 const char *help
; /* help string (NULL for no help) */
113 int (*handler
)(int, char **); /* routine which executes command */
114 int needconnect
; /* Do we need to be connected to execute? */
117 static char line
[256];
118 static char saveline
[256];
120 static char *margv
[20];
130 if (*cp
== '!') { /* Special case shell escape */
131 strcpy(saveline
, line
); /* save for shell command */
132 *argp
++ = strdup("!"); /* No room in string to get this */
144 for (cp2
= cp
; c
!= '\0'; c
= *++cp
) {
152 if ((c
= *++cp
) == '\0')
154 } else if (c
== '"') {
157 } else if (c
== '\'') {
160 } else if (isspace(c
))
174 * Make a character string into a number.
176 * Todo: 1. Could take random integers (12, 0x12, 012, 0b1).
189 c
= b
| 0x40; /* DEL */
202 * Construct a control character sequence
203 * for a special character.
210 * The only way I could get the Sun 3.5 compiler
212 * if ((unsigned int)c >= 0x80)
213 * was to assign "c" to an unsigned int variable...
216 unsigned int uic
= (unsigned int)c
;
220 if (c
== (cc_t
)_POSIX_VDISABLE
) {
225 buf
[1] = ((c
>>6)&07) + '0';
226 buf
[2] = ((c
>>3)&07) + '0';
227 buf
[3] = (c
&07) + '0';
229 } else if (uic
>= 0x20) {
241 * The following are data structures and routines for
242 * the "send" command.
247 const char *name
; /* How user refers to it (case independent) */
248 const char *help
; /* Help information (0 ==> no help) */
249 int needconnect
; /* Need to be connected */
250 int narg
; /* Number of arguments */
251 int (*handler
)(char *, ...); /* Routine to perform (for special ops) */
252 int nbyte
; /* Number of bytes to send this command */
253 int what
; /* Character to be sent (<0 ==> special) */
261 send_dontcmd(char *),
262 send_willcmd(char *),
263 send_wontcmd(char *);
265 static struct sendlist Sendlist
[] = {
266 { "ao", "Send Telnet Abort output", 1, 0, NULL
, 2, AO
},
267 { "ayt", "Send Telnet 'Are You There'", 1, 0, NULL
, 2, AYT
},
268 { "brk", "Send Telnet Break", 1, 0, NULL
, 2, BREAK
},
269 { "break", NULL
, 1, 0, NULL
, 2, BREAK
},
270 { "ec", "Send Telnet Erase Character", 1, 0, NULL
, 2, EC
},
271 { "el", "Send Telnet Erase Line", 1, 0, NULL
, 2, EL
},
272 { "escape", "Send current escape character",1, 0, (int (*)(char *, ...))send_esc
, 1, 0 },
273 { "ga", "Send Telnet 'Go Ahead' sequence", 1, 0, NULL
, 2, GA
},
274 { "ip", "Send Telnet Interrupt Process",1, 0, NULL
, 2, IP
},
275 { "intp", NULL
, 1, 0, NULL
, 2, IP
},
276 { "interrupt", NULL
, 1, 0, NULL
, 2, IP
},
277 { "intr", NULL
, 1, 0, NULL
, 2, IP
},
278 { "nop", "Send Telnet 'No operation'", 1, 0, NULL
, 2, NOP
},
279 { "eor", "Send Telnet 'End of Record'", 1, 0, NULL
, 2, EOR
},
280 { "abort", "Send Telnet 'Abort Process'", 1, 0, NULL
, 2, ABORT
},
281 { "susp", "Send Telnet 'Suspend Process'",1, 0, NULL
, 2, SUSP
},
282 { "eof", "Send Telnet End of File Character", 1, 0, NULL
, 2, xEOF
},
283 { "synch", "Perform Telnet 'Synch operation'", 1, 0, (int (*)(char *, ...))dosynch
, 2, 0 },
284 { "getstatus", "Send request for STATUS", 1, 0, (int (*)(char *, ...))get_status
, 6, 0 },
285 { "?", "Display send options", 0, 0, (int (*)(char *, ...))send_help
, 0, 0 },
286 { "help", NULL
, 0, 0, (int (*)(char *, ...))send_help
, 0, 0 },
287 { "do", NULL
, 0, 1, (int (*)(char *, ...))send_docmd
, 3, 0 },
288 { "dont", NULL
, 0, 1, (int (*)(char *, ...))send_dontcmd
, 3, 0 },
289 { "will", NULL
, 0, 1, (int (*)(char *, ...))send_willcmd
, 3, 0 },
290 { "wont", NULL
, 0, 1, (int (*)(char *, ...))send_wontcmd
, 3, 0 },
291 { NULL
, NULL
, 0, 0, NULL
, 0, 0 }
294 #define GETSEND(name) ((struct sendlist *) genget(name, (char **) Sendlist, \
295 sizeof(struct sendlist)))
298 sendcmd(int argc
, char *argv
[])
300 int count
; /* how many bytes we are going to need to send */
302 struct sendlist
*s
; /* pointer to current command */
307 printf("need at least one argument for 'send' command\n");
308 printf("'send ?' for help\n");
312 * First, validate all the send arguments.
313 * In addition, we see how much space we are going to need, and
314 * whether or not we will be doing a "SYNCH" operation (which
315 * flushes the network queue).
318 for (i
= 1; i
< argc
; i
++) {
319 s
= GETSEND(argv
[i
]);
321 printf("Unknown send argument '%s'\n'send ?' for help.\n",
324 } else if (Ambiguous((void *)s
)) {
325 printf("Ambiguous send argument '%s'\n'send ?' for help.\n",
329 if (i
+ s
->narg
>= argc
) {
331 "Need %d argument%s to 'send %s' command. 'send %s ?' for help.\n",
332 s
->narg
, s
->narg
== 1 ? "" : "s", s
->name
, s
->name
);
336 if ((void *)s
->handler
== (void *)send_help
) {
342 needconnect
+= s
->needconnect
;
344 if (!connected
&& needconnect
) {
345 printf("?Need to be connected first.\n");
346 printf("'send ?' for help\n");
349 /* Now, do we have enough room? */
350 if (NETROOM() < count
) {
351 printf("There is not enough room in the buffer TO the network\n");
352 printf("to process your request. Nothing will be done.\n");
353 printf("('send synch' will throw away most data in the network\n");
354 printf("buffer, if this might help.)\n");
357 /* OK, they are all OK, now go through again and actually send */
359 for (i
= 1; i
< argc
; i
++) {
360 if ((s
= GETSEND(argv
[i
])) == NULL
) {
361 fprintf(stderr
, "Telnet 'send' error - argument disappeared!\n");
367 success
+= (*s
->handler
)((s
->narg
> 0) ? argv
[i
+1] : 0,
368 (s
->narg
> 1) ? argv
[i
+2] : 0);
371 NET2ADD(IAC
, s
->what
);
372 printoption("SENT", IAC
, s
->what
);
375 return (count
== success
);
386 send_docmd(char *name
)
388 return(send_tncmd(send_do
, "do", name
));
392 send_dontcmd(char *name
)
394 return(send_tncmd(send_dont
, "dont", name
));
398 send_willcmd(char *name
)
400 return(send_tncmd(send_will
, "will", name
));
404 send_wontcmd(char *name
)
406 return(send_tncmd(send_wont
, "wont", name
));
410 send_tncmd(void (*func
)(int, int), const char *cmd
, char *name
)
413 extern char *telopts
[];
416 if (isprefix(name
, "help") || isprefix(name
, "?")) {
419 printf("Usage: send %s <value|option>\n", cmd
);
420 printf("\"value\" must be from 0 to 255\n");
421 printf("Valid options are:\n\t");
424 for (cpp
= telopts
; *cpp
; cpp
++) {
425 len
= strlen(*cpp
) + 3;
426 if (col
+ len
> 65) {
430 printf(" \"%s\"", *cpp
);
436 cpp
= (char **)genget(name
, telopts
, sizeof(char *));
437 if (Ambiguous(cpp
)) {
438 fprintf(stderr
,"'%s': ambiguous argument ('send %s ?' for help).\n",
447 while (*cp
>= '0' && *cp
<= '9') {
453 fprintf(stderr
, "'%s': unknown argument ('send %s ?' for help).\n",
456 } else if (val
< 0 || val
> 255) {
457 fprintf(stderr
, "'%s': bad value ('send %s ?' for help).\n",
463 printf("?Need to be connected first.\n");
473 struct sendlist
*s
; /* pointer to current command */
474 for (s
= Sendlist
; s
->name
; s
++) {
476 printf("%-15s %s\n", s
->name
, s
->help
);
482 * The following are the routines and data structures referred
483 * to by the arguments to the "toggle" command.
498 (SetSockOpt(net
, SOL_SOCKET
, SO_DEBUG
, telnet_debug
)) < 0) {
499 perror("setsockopt (SO_DEBUG)");
503 if (net
> 0 && SetSockOpt(net
, SOL_SOCKET
, SO_DEBUG
, 1) < 0)
504 perror("setsockopt (SO_DEBUG)");
506 printf("Cannot turn off socket debugging\n");
516 printf("Will send carriage returns as telnet <CR><LF>.\n");
518 printf("Will send carriage returns as telnet <CR><NUL>.\n");
528 donebinarytoggle
= 1;
533 if (my_want_state_is_will(TELOPT_BINARY
) &&
534 my_want_state_is_do(TELOPT_BINARY
)) {
536 } else if (my_want_state_is_wont(TELOPT_BINARY
) &&
537 my_want_state_is_dont(TELOPT_BINARY
)) {
540 val
= binmode
? 0 : 1;
544 if (my_want_state_is_will(TELOPT_BINARY
) &&
545 my_want_state_is_do(TELOPT_BINARY
)) {
546 printf("Already operating in binary mode with remote host.\n");
548 printf("Negotiating binary mode with remote host.\n");
552 if (my_want_state_is_wont(TELOPT_BINARY
) &&
553 my_want_state_is_dont(TELOPT_BINARY
)) {
554 printf("Already in network ascii mode with remote host.\n");
556 printf("Negotiating network ascii mode with remote host.\n");
566 donebinarytoggle
= 1;
569 val
= my_want_state_is_do(TELOPT_BINARY
) ? 0 : 1;
572 if (my_want_state_is_do(TELOPT_BINARY
)) {
573 printf("Already receiving in binary mode.\n");
575 printf("Negotiating binary mode on input.\n");
579 if (my_want_state_is_dont(TELOPT_BINARY
)) {
580 printf("Already receiving in network ascii mode.\n");
582 printf("Negotiating network ascii mode on input.\n");
592 donebinarytoggle
= 1;
595 val
= my_want_state_is_will(TELOPT_BINARY
) ? 0 : 1;
598 if (my_want_state_is_will(TELOPT_BINARY
)) {
599 printf("Already transmitting in binary mode.\n");
601 printf("Negotiating binary mode on output.\n");
605 if (my_want_state_is_wont(TELOPT_BINARY
)) {
606 printf("Already transmitting in network ascii mode.\n");
608 printf("Negotiating network ascii mode on output.\n");
616 const char *name
; /* name of toggle */
617 const char *help
; /* help message */
618 int (*handler
)(int); /* routine to do actual setting */
620 const char *actionexplanation
;
623 static struct togglelist Togglelist
[] = {
625 "flushing of output when sending interrupt characters",
628 "flush output when sending interrupt characters" },
630 "automatic sending of interrupt characters in urgent mode",
633 "send interrupt characters in urgent mode" },
634 #ifdef AUTHENTICATION
636 "automatic sending of login and/or authentication info",
639 "send login name and/or authentication information" },
641 "Toggle authentication debugging",
644 "print authentication debugging information" },
648 "automatic encryption of data stream",
651 "automatically encrypt output" },
653 "automatic decryption of data stream",
656 "automatically decrypt input" },
658 "Toggle verbose encryption output",
661 "print verbose encryption output" },
663 "Toggle encryption debugging",
666 "print encryption debugging information" },
667 #endif /* ENCRYPTION */
669 "don't read ~/.telnetrc file",
672 "skip reading of ~/.telnetrc file" },
674 "sending and receiving of binary data",
679 "receiving of binary data",
684 "sending of binary data",
689 "sending carriage returns as telnet <CR><LF>",
690 (int (*)(int))togcrlf
,
694 "mapping of received carriage returns",
697 "map carriage return on output" },
699 "local recognition of certain control characters",
700 (int (*)(int))lclchars
,
702 "recognize certain control characters" },
703 { " ", "", NULL
, NULL
, NULL
}, /* empty line */
706 (int (*)(int))togdebug
,
708 "turn on socket level debugging" },
710 "printing of hexadecimal network data (debugging)",
713 "print hexadecimal representation of network traffic" },
715 "output of \"netdata\" to user readable format (debugging)",
718 "print user readable output for \"netdata\"" },
720 "viewing of options processing (debugging)",
723 "show option processing" },
725 "(debugging) toggle printing of hexadecimal terminal data",
728 "print hexadecimal representation of terminal traffic" },
731 (int (*)(int))togglehelp
,
734 { NULL
, NULL
, NULL
, NULL
, NULL
},
737 (int (*)(int))togglehelp
,
740 { NULL
, NULL
, NULL
, NULL
, NULL
}
746 struct togglelist
*c
;
748 for (c
= Togglelist
; c
->name
; c
++) {
751 printf("%-15s toggle %s\n", c
->name
, c
->help
);
757 printf("%-15s %s\n", "?", "display help information");
762 settogglehelp(int set
)
764 struct togglelist
*c
;
766 for (c
= Togglelist
; c
->name
; c
++) {
769 printf("%-15s %s %s\n", c
->name
, set
? "enable" : "disable",
777 #define GETTOGGLE(name) (struct togglelist *) \
778 genget(name, (char **) Togglelist, sizeof(struct togglelist))
781 toggle(int argc
, char *argv
[])
785 struct togglelist
*c
;
789 "Need an argument to 'toggle' command. 'toggle ?' for help.\n");
797 if (Ambiguous((void *)c
)) {
798 fprintf(stderr
, "'%s': ambiguous argument ('toggle ?' for help).\n",
801 } else if (c
== NULL
) {
802 fprintf(stderr
, "'%s': unknown argument ('toggle ?' for help).\n",
807 *c
->variable
= !*c
->variable
; /* invert it */
808 if (c
->actionexplanation
) {
809 printf("%s %s.\n", *c
->variable
? "Will" : "Won't",
810 c
->actionexplanation
);
814 retval
&= (*c
->handler
)(-1);
822 * The following perform the "set" command.
826 struct termio new_tc
= { 0, 0, 0, 0, {}, 0, 0 };
830 const char *name
; /* name */
831 const char *help
; /* help information */
832 void (*handler
)(char *);
833 cc_t
*charp
; /* where it is located at */
836 static struct setlist Setlist
[] = {
837 #ifdef KLUDGELINEMODE
838 { "echo", "character to toggle local echoing on/off", NULL
, &echoc
},
840 { "escape", "character to escape back to telnet command mode", NULL
, &escape
},
841 { "rlogin", "rlogin escape character", 0, &rlogin
},
842 { "tracefile", "file to write trace information to", SetNetTrace
, (cc_t
*)NetTraceFile
},
843 { " ", "", NULL
, NULL
},
844 { " ", "The following need 'localchars' to be toggled true", NULL
, NULL
},
845 { "flushoutput", "character to cause an Abort Output", NULL
, termFlushCharp
},
846 { "interrupt", "character to cause an Interrupt Process", NULL
, termIntCharp
},
847 { "quit", "character to cause an Abort process", NULL
, termQuitCharp
},
848 { "eof", "character to cause an EOF ", NULL
, termEofCharp
},
849 { " ", "", NULL
, NULL
},
850 { " ", "The following are for local editing in linemode", NULL
, NULL
},
851 { "erase", "character to use to erase a character", NULL
, termEraseCharp
},
852 { "kill", "character to use to erase a line", NULL
, termKillCharp
},
853 { "lnext", "character to use for literal next", NULL
, termLiteralNextCharp
},
854 { "susp", "character to cause a Suspend Process", NULL
, termSuspCharp
},
855 { "reprint", "character to use for line reprint", NULL
, termRprntCharp
},
856 { "worderase", "character to use to erase a word", NULL
, termWerasCharp
},
857 { "start", "character to use for XON", NULL
, termStartCharp
},
858 { "stop", "character to use for XOFF", NULL
, termStopCharp
},
859 { "forw1", "alternate end of line character", NULL
, termForw1Charp
},
860 { "forw2", "alternate end of line character", NULL
, termForw2Charp
},
861 { "ayt", "alternate AYT character", NULL
, termAytCharp
},
862 { NULL
, NULL
, NULL
, NULL
}
865 static struct setlist
*
868 return (struct setlist
*)
869 genget(name
, (char **) Setlist
, sizeof(struct setlist
));
873 set_escape_char(char *s
)
875 if (rlogin
!= _POSIX_VDISABLE
) {
876 rlogin
= (s
&& *s
) ? special(s
) : _POSIX_VDISABLE
;
877 printf("Telnet rlogin escape character is '%s'.\n",
880 escape
= (s
&& *s
) ? special(s
) : _POSIX_VDISABLE
;
881 printf("Telnet escape character is '%s'.\n", control(escape
));
886 setcmd(int argc
, char *argv
[])
890 struct togglelist
*c
;
892 if (argc
< 2 || argc
> 3) {
893 printf("Format is 'set Name Value'\n'set ?' for help.\n");
896 if ((argc
== 2) && (isprefix(argv
[1], "?") || isprefix(argv
[1], "help"))) {
897 for (ct
= Setlist
; ct
->name
; ct
++)
898 printf("%-15s %s\n", ct
->name
, ct
->help
);
901 printf("%-15s %s\n", "?", "display help information");
905 ct
= getset(argv
[1]);
907 c
= GETTOGGLE(argv
[1]);
909 fprintf(stderr
, "'%s': unknown argument ('set ?' for help).\n",
912 } else if (Ambiguous((void *)c
)) {
913 fprintf(stderr
, "'%s': ambiguous argument ('set ?' for help).\n",
918 if ((argc
== 2) || (strcmp("on", argv
[2]) == 0))
920 else if (strcmp("off", argv
[2]) == 0)
923 printf("Format is 'set togglename [on|off]'\n'set ?' for help.\n");
926 if (c
->actionexplanation
) {
927 printf("%s %s.\n", *c
->variable
? "Will" : "Won't",
928 c
->actionexplanation
);
933 } else if (argc
!= 3) {
934 printf("Format is 'set Name Value'\n'set ?' for help.\n");
936 } else if (Ambiguous((void *)ct
)) {
937 fprintf(stderr
, "'%s': ambiguous argument ('set ?' for help).\n",
940 } else if (ct
->handler
) {
941 (*ct
->handler
)(argv
[2]);
942 printf("%s set to \"%s\".\n", ct
->name
, (char *)ct
->charp
);
944 if (strcmp("off", argv
[2])) {
945 value
= special(argv
[2]);
947 value
= _POSIX_VDISABLE
;
949 *(ct
->charp
) = (cc_t
)value
;
950 printf("%s character is '%s'.\n", ct
->name
, control(*(ct
->charp
)));
957 unsetcmd(int argc
, char *argv
[])
960 struct togglelist
*c
;
965 "Need an argument to 'unset' command. 'unset ?' for help.\n");
968 if (isprefix(argv
[1], "?") || isprefix(argv
[1], "help")) {
969 for (ct
= Setlist
; ct
->name
; ct
++)
970 printf("%-15s %s\n", ct
->name
, ct
->help
);
973 printf("%-15s %s\n", "?", "display help information");
985 fprintf(stderr
, "'%s': unknown argument ('unset ?' for help).\n",
988 } else if (Ambiguous((void *)c
)) {
989 fprintf(stderr
, "'%s': ambiguous argument ('unset ?' for help).\n",
995 if (c
->actionexplanation
) {
996 printf("%s %s.\n", *c
->variable
? "Will" : "Won't",
997 c
->actionexplanation
);
1002 } else if (Ambiguous((void *)ct
)) {
1003 fprintf(stderr
, "'%s': ambiguous argument ('unset ?' for help).\n",
1006 } else if (ct
->handler
) {
1008 printf("%s reset to \"%s\".\n", ct
->name
, (char *)ct
->charp
);
1010 *(ct
->charp
) = _POSIX_VDISABLE
;
1011 printf("%s character is '%s'.\n", ct
->name
, control(*(ct
->charp
)));
1018 * The following are the data structures and routines for the
1021 #ifdef KLUDGELINEMODE
1022 extern int kludgelinemode
;
1028 send_wont(TELOPT_LINEMODE
, 1);
1029 send_dont(TELOPT_SGA
, 1);
1030 send_dont(TELOPT_ECHO
, 1);
1038 #ifdef KLUDGELINEMODE
1040 send_dont(TELOPT_SGA
, 1);
1042 send_will(TELOPT_LINEMODE
, 1);
1043 send_dont(TELOPT_ECHO
, 1);
1050 #ifdef KLUDGELINEMODE
1052 send_do(TELOPT_SGA
, 1);
1055 send_wont(TELOPT_LINEMODE
, 1);
1056 send_do(TELOPT_ECHO
, 1);
1061 dolmmode(int bit
, int on
)
1064 extern int linemode
;
1066 if (my_want_state_is_wont(TELOPT_LINEMODE
)) {
1067 printf("?Need to have LINEMODE option enabled first.\n");
1068 printf("'mode ?' for help.\n");
1073 c
= (linemode
| bit
);
1075 c
= (linemode
& ~bit
);
1083 return dolmmode(bit
, 1);
1089 return dolmmode(bit
, 0);
1093 const char *name
; /* command name */
1094 const char *help
; /* help string */
1095 int (*handler
)(int);/* routine which executes command */
1096 int needconnect
; /* Do we need to be connected to execute? */
1100 static struct modelist ModeList
[] = {
1101 { "character", "Disable LINEMODE option", (int (*)(int))docharmode
, 1, 0 },
1102 #ifdef KLUDGELINEMODE
1103 { "", "(or disable obsolete line-by-line mode)", NULL
, 0, 0 },
1105 { "line", "Enable LINEMODE option", (int (*)(int))dolinemode
, 1, 0 },
1106 #ifdef KLUDGELINEMODE
1107 { "", "(or enable obsolete line-by-line mode)", NULL
, 0, 0 },
1109 { "", "", NULL
, 0, 0 },
1110 { "", "These require the LINEMODE option to be enabled", NULL
, 0, 0 },
1111 { "isig", "Enable signal trapping", setmod
, 1, MODE_TRAPSIG
},
1112 { "+isig", 0, setmod
, 1, MODE_TRAPSIG
},
1113 { "-isig", "Disable signal trapping", clearmode
, 1, MODE_TRAPSIG
},
1114 { "edit", "Enable character editing", setmod
, 1, MODE_EDIT
},
1115 { "+edit", 0, setmod
, 1, MODE_EDIT
},
1116 { "-edit", "Disable character editing", clearmode
, 1, MODE_EDIT
},
1117 { "softtabs", "Enable tab expansion", setmod
, 1, MODE_SOFT_TAB
},
1118 { "+softtabs", 0, setmod
, 1, MODE_SOFT_TAB
},
1119 { "-softtabs", "Disable character editing", clearmode
, 1, MODE_SOFT_TAB
},
1120 { "litecho", "Enable literal character echo", setmod
, 1, MODE_LIT_ECHO
},
1121 { "+litecho", 0, setmod
, 1, MODE_LIT_ECHO
},
1122 { "-litecho", "Disable literal character echo", clearmode
, 1, MODE_LIT_ECHO
},
1123 { "help", 0, (int (*)(int))modehelp
, 0, 0 },
1124 #ifdef KLUDGELINEMODE
1125 { "kludgeline", 0, (int (*)(int))dokludgemode
, 1, 0 },
1127 { "", "", NULL
, 0, 0 },
1128 { "?", "Print help information", (int (*)(int))modehelp
, 0, 0 },
1129 { NULL
, NULL
, NULL
, 0, 0 },
1136 struct modelist
*mt
;
1138 printf("format is: 'mode Mode', where 'Mode' is one of:\n\n");
1139 for (mt
= ModeList
; mt
->name
; mt
++) {
1142 printf("%-15s %s\n", mt
->name
, mt
->help
);
1150 #define GETMODECMD(name) (struct modelist *) \
1151 genget(name, (char **) ModeList, sizeof(struct modelist))
1154 modecmd(int argc
, char *argv
[])
1156 struct modelist
*mt
;
1159 printf("'mode' command requires an argument\n");
1160 printf("'mode ?' for help.\n");
1161 } else if ((mt
= GETMODECMD(argv
[1])) == NULL
) {
1162 fprintf(stderr
, "Unknown mode '%s' ('mode ?' for help).\n", argv
[1]);
1163 } else if (Ambiguous((void *)mt
)) {
1164 fprintf(stderr
, "Ambiguous mode '%s' ('mode ?' for help).\n", argv
[1]);
1165 } else if (mt
->needconnect
&& !connected
) {
1166 printf("?Need to be connected first.\n");
1167 printf("'mode ?' for help.\n");
1168 } else if (mt
->handler
) {
1169 return (*mt
->handler
)(mt
->arg1
);
1175 * The following data structures and routines implement the
1176 * "display" command.
1180 display(int argc
, char *argv
[])
1182 struct togglelist
*tl
;
1185 #define dotog(tl) if (tl->variable && tl->actionexplanation) { \
1186 if (*tl->variable) { \
1191 printf(" %s.\n", tl->actionexplanation); \
1194 #define doset(sl) if (sl->name && *sl->name != ' ') { \
1195 if (sl->handler == 0) \
1196 printf("%-15s [%s]\n", sl->name, control(*sl->charp)); \
1198 printf("%-15s \"%s\"\n", sl->name, (char *)sl->charp); \
1202 for (tl
= Togglelist
; tl
->name
; tl
++) {
1206 for (sl
= Setlist
; sl
->name
; sl
++) {
1212 for (i
= 1; i
< argc
; i
++) {
1213 sl
= getset(argv
[i
]);
1214 tl
= GETTOGGLE(argv
[i
]);
1215 if (Ambiguous((void *)sl
) || Ambiguous((void *)tl
)) {
1216 printf("?Ambiguous argument '%s'.\n", argv
[i
]);
1218 } else if (!sl
&& !tl
) {
1219 printf("?Unknown argument '%s'.\n", argv
[i
]);
1231 /*@*/optionstatus();
1234 #endif /* ENCRYPTION */
1241 * The following are the data structures, and many of the routines,
1242 * relating to command processing.
1246 * Set the escape character.
1249 setescape(int argc
, char *argv
[])
1255 "Deprecated usage - please use 'set escape%s%s' in the future.\n",
1256 (argc
> 2)? " ":"", (argc
> 2)? argv
[1]: "");
1260 printf("new escape character: ");
1261 (void) fgets(buf
, sizeof(buf
), stdin
);
1266 (void) fflush(stdout
);
1274 printf("Deprecated usage - please use 'toggle crmod' in the future.\n");
1275 printf("%s map carriage return on output.\n", crmod
? "Will" : "Won't");
1276 (void) fflush(stdout
);
1286 long oldrows
, oldcols
, newrows
, newcols
, err_
;
1288 err_
= (TerminalWindowSize(&oldrows
, &oldcols
) == 0) ? 1 : 0;
1289 (void) kill(0, SIGTSTP
);
1291 * If we didn't get the window size before the SUSPEND, but we
1292 * can get them now (?), then send the NAWS to make sure that
1293 * we are set up for the right window size.
1295 if (TerminalWindowSize(&newrows
, &newcols
) && connected
&&
1296 (err_
|| ((oldrows
!= newrows
) || (oldcols
!= newcols
)))) {
1300 /* reget parameters in case they were changed */
1301 TerminalSaveState();
1304 printf("Suspend is not supported. Try the '!' command instead\n");
1310 shell(int argc
, char *argv
[] __unused
)
1312 long oldrows
, oldcols
, newrows
, newcols
, err_
;
1316 err_
= (TerminalWindowSize(&oldrows
, &oldcols
) == 0) ? 1 : 0;
1319 perror("Fork failed\n");
1325 * Fire up the shell in the child.
1327 const char *shellp
, *shellname
;
1329 shellp
= getenv("SHELL");
1332 if ((shellname
= strrchr(shellp
, '/')) == NULL
)
1337 execl(shellp
, shellname
, "-c", &saveline
[1], NULL
);
1339 execl(shellp
, shellname
, NULL
);
1344 (void)wait(NULL
); /* Wait for the shell to complete */
1346 if (TerminalWindowSize(&newrows
, &newcols
) && connected
&&
1347 (err_
|| ((oldrows
!= newrows
) || (oldcols
!= newcols
)))) {
1356 bye(int argc
, char *argv
[])
1358 extern int resettermname
;
1361 (void) shutdown(net
, SHUT_RDWR
);
1362 printf("Connection closed.\n");
1363 (void) NetClose(net
);
1366 #ifdef AUTHENTICATION
1368 auth_encrypt_connect(connected
);
1374 if ((argc
!= 2) || (strcmp(argv
[1], "fromquit") != 0)) {
1375 longjmp(toplevel
, 1);
1378 return 1; /* Keep lint, etc., happy */
1384 (void) call(bye
, "bye", "fromquit", 0);
1391 send_do(TELOPT_LOGOUT
, 1);
1404 void (*handler
)(int);
1408 static void slc_help(void);
1410 struct slclist SlcList
[] = {
1411 { "export", "Use local special character definitions",
1412 (void (*)(int))slc_mode_export
, 0 },
1413 { "import", "Use remote special character definitions",
1414 slc_mode_import
, 1 },
1415 { "check", "Verify remote special character definitions",
1416 slc_mode_import
, 0 },
1417 { "help", NULL
, (void (*)(int))slc_help
, 0 },
1418 { "?", "Print help information", (void (*)(int))slc_help
, 0 },
1419 { NULL
, NULL
, NULL
, 0 },
1427 for (c
= SlcList
; c
->name
; c
++) {
1430 printf("%-15s %s\n", c
->name
, c
->help
);
1437 static struct slclist
*
1440 return (struct slclist
*)
1441 genget(name
, (char **) SlcList
, sizeof(struct slclist
));
1445 slccmd(int argc
, char *argv
[])
1451 "Need an argument to 'slc' command. 'slc ?' for help.\n");
1454 c
= getslc(argv
[1]);
1456 fprintf(stderr
, "'%s': unknown argument ('slc ?' for help).\n",
1460 if (Ambiguous((void *)c
)) {
1461 fprintf(stderr
, "'%s': ambiguous argument ('slc ?' for help).\n",
1465 (*c
->handler
)(c
->arg
);
1471 * The ENVIRON command.
1477 void (*handler
)(unsigned char *, unsigned char *);
1481 extern struct env_lst
*
1482 env_define(const unsigned char *, unsigned char *);
1484 env_undefine(const unsigned char *),
1485 env_export(const unsigned char *),
1486 env_unexport(const unsigned char *),
1487 env_send(const unsigned char *),
1488 #if defined(OLD_ENVIRON) && defined(ENV_HACK)
1489 env_varval(const unsigned char *),
1495 struct envlist EnvList
[] = {
1496 { "define", "Define an environment variable",
1497 (void (*)(unsigned char *, unsigned char *))env_define
, 2 },
1498 { "undefine", "Undefine an environment variable",
1499 (void (*)(unsigned char *, unsigned char *))env_undefine
, 1 },
1500 { "export", "Mark an environment variable for automatic export",
1501 (void (*)(unsigned char *, unsigned char *))env_export
, 1 },
1502 { "unexport", "Don't mark an environment variable for automatic export",
1503 (void (*)(unsigned char *, unsigned char *))env_unexport
, 1 },
1504 { "send", "Send an environment variable", (void (*)(unsigned char *, unsigned char *))env_send
, 1 },
1505 { "list", "List the current environment variables",
1506 (void (*)(unsigned char *, unsigned char *))env_list
, 0 },
1507 #if defined(OLD_ENVIRON) && defined(ENV_HACK)
1508 { "varval", "Reverse VAR and VALUE (auto, right, wrong, status)",
1509 (void (*)(unsigned char *, unsigned char *))env_varval
, 1 },
1511 { "help", NULL
, (void (*)(unsigned char *, unsigned char *))env_help
, 0 },
1512 { "?", "Print help information", (void (*)(unsigned char *, unsigned char *))env_help
, 0 },
1513 { NULL
, NULL
, NULL
, 0 },
1521 for (c
= EnvList
; c
->name
; c
++) {
1524 printf("%-15s %s\n", c
->name
, c
->help
);
1531 static struct envlist
*
1532 getenvcmd(char *name
)
1534 return (struct envlist
*)
1535 genget(name
, (char **) EnvList
, sizeof(struct envlist
));
1539 env_cmd(int argc
, char *argv
[])
1545 "Need an argument to 'environ' command. 'environ ?' for help.\n");
1548 c
= getenvcmd(argv
[1]);
1550 fprintf(stderr
, "'%s': unknown argument ('environ ?' for help).\n",
1554 if (Ambiguous((void *)c
)) {
1555 fprintf(stderr
, "'%s': ambiguous argument ('environ ?' for help).\n",
1559 if (c
->narg
+ 2 != argc
) {
1561 "Need %s%d argument%s to 'environ %s' command. 'environ ?' for help.\n",
1562 c
->narg
< argc
+ 2 ? "only " : "",
1563 c
->narg
, c
->narg
== 1 ? "" : "s", c
->name
);
1566 (*c
->handler
)(argv
[2], argv
[3]);
1571 struct env_lst
*next
; /* pointer to next structure */
1572 struct env_lst
*prev
; /* pointer to previous structure */
1573 unsigned char *var
; /* pointer to variable name */
1574 unsigned char *value
; /* pointer to variable value */
1575 int export
; /* 1 -> export with default list of variables */
1576 int welldefined
; /* A well defined variable */
1579 struct env_lst envlisthead
;
1581 static struct env_lst
*
1582 env_find(const unsigned char *var
)
1586 for (ep
= envlisthead
.next
; ep
; ep
= ep
->next
) {
1587 if (strcmp(ep
->var
, var
) == 0)
1596 extern char **environ
;
1600 for (epp
= environ
; *epp
; epp
++) {
1601 if ((cp
= strchr(*epp
, '='))) {
1603 ep
= env_define((unsigned char *)*epp
,
1604 (unsigned char *)cp
+1);
1610 * Special case for DISPLAY variable. If it is ":0.0" or
1611 * "unix:0.0", we have to get rid of "unix" and insert our
1614 if ((ep
= env_find("DISPLAY"))
1615 && ((*ep
->value
== ':')
1616 || (strncmp((char *)ep
->value
, "unix:", 5) == 0))) {
1618 char *cp2
= strchr((char *)ep
->value
, ':');
1621 gethostname(hbuf
, sizeof(hbuf
));
1622 hbuf
[sizeof(hbuf
)-1] = '\0';
1623 buflen
= strlen(hbuf
) + strlen(cp2
) + 1;
1624 cp
= (char *)malloc(sizeof(char)*buflen
);
1626 snprintf((char *)cp
, buflen
, "%s%s", hbuf
, cp2
);
1628 ep
->value
= (unsigned char *)cp
;
1631 * If USER is not defined, but LOGNAME is, then add
1632 * USER with the value from LOGNAME. By default, we
1633 * don't export the USER variable.
1635 if ((env_find("USER") == NULL
) && (ep
= env_find("LOGNAME"))) {
1636 env_define("USER", ep
->value
);
1637 env_unexport("USER");
1639 env_export("DISPLAY");
1640 env_export("PRINTER");
1644 env_define(const unsigned char *var
, unsigned char *value
)
1648 if ((ep
= env_find(var
))) {
1654 ep
= (struct env_lst
*)malloc(sizeof(struct env_lst
));
1655 ep
->next
= envlisthead
.next
;
1656 envlisthead
.next
= ep
;
1657 ep
->prev
= &envlisthead
;
1659 ep
->next
->prev
= ep
;
1661 ep
->welldefined
= opt_welldefined(var
);
1663 ep
->var
= strdup(var
);
1664 ep
->value
= strdup(value
);
1669 env_undefine(const unsigned char *var
)
1673 if ((ep
= env_find(var
))) {
1674 ep
->prev
->next
= ep
->next
;
1676 ep
->next
->prev
= ep
->prev
;
1686 env_export(const unsigned char *var
)
1690 if ((ep
= env_find(var
)))
1695 env_unexport(const unsigned char *var
)
1699 if ((ep
= env_find(var
)))
1704 env_send(const unsigned char *var
)
1708 if (my_state_is_wont(TELOPT_NEW_ENVIRON
)
1710 && my_state_is_wont(TELOPT_OLD_ENVIRON
)
1714 "Cannot send '%s': Telnet ENVIRON option not enabled\n",
1720 fprintf(stderr
, "Cannot send '%s': variable not defined\n",
1724 env_opt_start_info();
1725 env_opt_add(ep
->var
);
1734 for (ep
= envlisthead
.next
; ep
; ep
= ep
->next
) {
1735 printf("%c %-20s %s\n", ep
->export
? '*' : ' ',
1736 ep
->var
, ep
->value
);
1741 env_default(int init
, int welldefined
)
1743 static struct env_lst
*nep
= NULL
;
1750 while ((nep
= nep
->next
)) {
1751 if (nep
->export
&& (nep
->welldefined
== welldefined
))
1759 env_getvalue(const unsigned char *var
)
1763 if ((ep
= env_find(var
)))
1768 #if defined(OLD_ENVIRON) && defined(ENV_HACK)
1770 env_varval(const unsigned char *what
)
1772 extern int old_env_var
, old_env_value
, env_auto
;
1773 int len
= strlen((char *)what
);
1778 if (strncasecmp((char *)what
, "status", len
) == 0) {
1780 printf("%s%s", "VAR and VALUE are/will be ",
1781 "determined automatically\n");
1782 if (old_env_var
== OLD_ENV_VAR
)
1783 printf("VAR and VALUE set to correct definitions\n");
1785 printf("VAR and VALUE definitions are reversed\n");
1786 } else if (strncasecmp((char *)what
, "auto", len
) == 0) {
1788 old_env_var
= OLD_ENV_VALUE
;
1789 old_env_value
= OLD_ENV_VAR
;
1790 } else if (strncasecmp((char *)what
, "right", len
) == 0) {
1792 old_env_var
= OLD_ENV_VAR
;
1793 old_env_value
= OLD_ENV_VALUE
;
1794 } else if (strncasecmp((char *)what
, "wrong", len
) == 0) {
1796 old_env_var
= OLD_ENV_VALUE
;
1797 old_env_value
= OLD_ENV_VAR
;
1800 printf("Unknown \"varval\" command. (\"auto\", \"right\", \"wrong\", \"status\")\n");
1805 #ifdef AUTHENTICATION
1807 * The AUTHENTICATE command.
1813 int (*handler
)(char *);
1818 auth_enable(char *),
1819 auth_disable(char *),
1824 struct authlist AuthList
[] = {
1825 { "status", "Display current status of authentication information",
1826 (int (*)(char *))auth_status
, 0 },
1827 { "disable", "Disable an authentication type ('auth disable ?' for more)",
1829 { "enable", "Enable an authentication type ('auth enable ?' for more)",
1831 { "help", NULL
, (int (*)(char *))auth_help
, 0 },
1832 { "?", "Print help information", (int (*)(char *))auth_help
, 0 },
1833 { NULL
, NULL
, NULL
, 0 },
1841 for (c
= AuthList
; c
->name
; c
++) {
1844 printf("%-15s %s\n", c
->name
, c
->help
);
1853 auth_cmd(int argc
, char *argv
[])
1859 "Need an argument to 'auth' command. 'auth ?' for help.\n");
1863 c
= (struct authlist
*)
1864 genget(argv
[1], (char **) AuthList
, sizeof(struct authlist
));
1866 fprintf(stderr
, "'%s': unknown argument ('auth ?' for help).\n",
1870 if (Ambiguous((void *)c
)) {
1871 fprintf(stderr
, "'%s': ambiguous argument ('auth ?' for help).\n",
1875 if (c
->narg
+ 2 != argc
) {
1877 "Need %s%d argument%s to 'auth %s' command. 'auth ?' for help.\n",
1878 c
->narg
< argc
+ 2 ? "only " : "",
1879 c
->narg
, c
->narg
== 1 ? "" : "s", c
->name
);
1882 return((*c
->handler
)(argv
[2]));
1888 * The ENCRYPT command.
1891 struct encryptlist
{
1894 int (*handler
)(char *, char *);
1901 EncryptEnable(char *, char *),
1902 EncryptDisable(char *, char *),
1903 EncryptType(char *, char *),
1904 EncryptStart(char *),
1905 EncryptStartInput(void),
1906 EncryptStartOutput(void),
1907 EncryptStop(char *),
1908 EncryptStopInput(void),
1909 EncryptStopOutput(void),
1910 EncryptStatus(void);
1914 struct encryptlist EncryptList
[] = {
1915 { "enable", "Enable encryption. ('encrypt enable ?' for more)",
1916 EncryptEnable
, 1, 1, 2 },
1917 { "disable", "Disable encryption. ('encrypt enable ?' for more)",
1918 EncryptDisable
, 0, 1, 2 },
1919 { "type", "Set encryption type. ('encrypt type ?' for more)",
1920 EncryptType
, 0, 1, 1 },
1921 { "start", "Start encryption. ('encrypt start ?' for more)",
1922 (int (*)(char *, char *))EncryptStart
, 1, 0, 1 },
1923 { "stop", "Stop encryption. ('encrypt stop ?' for more)",
1924 (int (*)(char *, char *))EncryptStop
, 1, 0, 1 },
1925 { "input", "Start encrypting the input stream",
1926 (int (*)(char *, char *))EncryptStartInput
, 1, 0, 0 },
1927 { "-input", "Stop encrypting the input stream",
1928 (int (*)(char *, char *))EncryptStopInput
, 1, 0, 0 },
1929 { "output", "Start encrypting the output stream",
1930 (int (*)(char *, char *))EncryptStartOutput
, 1, 0, 0 },
1931 { "-output", "Stop encrypting the output stream",
1932 (int (*)(char *, char *))EncryptStopOutput
, 1, 0, 0 },
1934 { "status", "Display current status of authentication information",
1935 (int (*)(char *, char *))EncryptStatus
, 0, 0, 0 },
1936 { "help", NULL
, (int (*)(char *, char *))EncryptHelp
, 0, 0, 0 },
1937 { "?", "Print help information", (int (*)(char *, char *))EncryptHelp
, 0, 0, 0 },
1938 { NULL
, NULL
, NULL
, 0, 0, 0 },
1944 struct encryptlist
*c
;
1946 for (c
= EncryptList
; c
->name
; c
++) {
1949 printf("%-15s %s\n", c
->name
, c
->help
);
1958 encrypt_cmd(int argc
, char *argv
[])
1960 struct encryptlist
*c
;
1964 "Need an argument to 'encrypt' command. 'encrypt ?' for help.\n");
1968 c
= (struct encryptlist
*)
1969 genget(argv
[1], (char **) EncryptList
, sizeof(struct encryptlist
));
1971 fprintf(stderr
, "'%s': unknown argument ('encrypt ?' for help).\n",
1975 if (Ambiguous((void *)c
)) {
1976 fprintf(stderr
, "'%s': ambiguous argument ('encrypt ?' for help).\n",
1981 if (argc
< c
->minarg
|| argc
> c
->maxarg
) {
1982 if (c
->minarg
== c
->maxarg
) {
1983 fprintf(stderr
, "Need %s%d argument%s ",
1984 c
->minarg
< argc
? "only " : "", c
->minarg
,
1985 c
->minarg
== 1 ? "" : "s");
1987 fprintf(stderr
, "Need %s%d-%d arguments ",
1988 c
->maxarg
< argc
? "only " : "", c
->minarg
, c
->maxarg
);
1990 fprintf(stderr
, "to 'encrypt %s' command. 'encrypt ?' for help.\n",
1994 if (c
->needconnect
&& !connected
) {
1995 if (!(argc
&& (isprefix(argv
[2], "help") || isprefix(argv
[2], "?")))) {
1996 printf("?Need to be connected first.\n");
2000 return ((*c
->handler
)(argc
> 0 ? argv
[2] : 0,
2001 argc
> 1 ? argv
[3] : 0));
2003 #endif /* ENCRYPTION */
2006 * Print status about the connection.
2010 status(int argc
, char *argv
[])
2013 printf("Connected to %s.\n", hostname
);
2014 if ((argc
< 2) || strcmp(argv
[1], "notmuch")) {
2015 int mode
= getconnmode();
2017 if (my_want_state_is_will(TELOPT_LINEMODE
)) {
2018 printf("Operating with LINEMODE option\n");
2019 printf("%s line editing\n", (mode
&MODE_EDIT
) ? "Local" : "No");
2020 printf("%s catching of signals\n",
2021 (mode
&MODE_TRAPSIG
) ? "Local" : "No");
2023 #ifdef KLUDGELINEMODE
2024 } else if (kludgelinemode
&& my_want_state_is_dont(TELOPT_SGA
)) {
2025 printf("Operating in obsolete linemode\n");
2028 printf("Operating in single character mode\n");
2030 printf("Catching signals locally\n");
2032 printf("%s character echo\n", (mode
&MODE_ECHO
) ? "Local" : "Remote");
2033 if (my_want_state_is_will(TELOPT_LFLOW
))
2034 printf("%s flow control\n", (mode
&MODE_FLOW
) ? "Local" : "No");
2037 #endif /* ENCRYPTION */
2040 printf("No connection.\n");
2042 printf("Escape character is '%s'.\n", control(escape
));
2043 (void) fflush(stdout
);
2049 * Function that gets called when SIGINFO is received.
2054 (void) call(status
, "status", "notmuch", 0);
2059 sockaddr_ntop(struct sockaddr
*sa
)
2062 static char addrbuf
[INET6_ADDRSTRLEN
];
2064 switch (sa
->sa_family
) {
2066 addr
= &((struct sockaddr_in
*)sa
)->sin_addr
;
2069 addr
= &((struct sockaddr_un
*)sa
)->sun_path
;
2073 addr
= &((struct sockaddr_in6
*)sa
)->sin6_addr
;
2079 inet_ntop(sa
->sa_family
, addr
, addrbuf
, sizeof(addrbuf
));
2085 * When an Address Family related error happend, check if retry with
2086 * another AF is possible or not.
2087 * Return 1, if retry with another af is OK. Else, return 0.
2090 switch_af(struct addrinfo
**aip
)
2093 struct addrinfo
*ai
;
2096 nextaf
= (ai
->ai_family
== AF_INET
) ? AF_INET6
: AF_INET
;
2099 while (ai
!= NULL
&& ai
->ai_family
!= nextaf
);
2109 tn(int argc
, char *argv
[])
2114 int srcroute
= 0, result
;
2115 char *cmd
, *hostp
= NULL
, *portp
= NULL
, *user
= NULL
;
2116 char *src_addr
= NULL
;
2117 struct addrinfo hints
, *res
, *res0
= NULL
, *src_res
, *src_res0
= NULL
;
2118 int error
= 0, af_error
= 0;
2121 printf("?Already connected to %s\n", hostname
);
2126 (void) strlcpy(line
, "open ", sizeof(line
));
2128 (void) fgets(&line
[strlen(line
)], sizeof(line
) - strlen(line
), stdin
);
2136 if (strcmp(*argv
, "help") == 0 || isprefix(*argv
, "?"))
2138 if (strcmp(*argv
, "-l") == 0) {
2146 if (strcmp(*argv
, "-a") == 0) {
2151 if (strcmp(*argv
, "-s") == 0) {
2159 if (hostp
== NULL
) {
2164 if (portp
== NULL
) {
2170 printf("usage: %s [-l user] [-a] [-s src_addr] host-name [port]\n", cmd
);
2177 if (src_addr
!= NULL
) {
2178 memset(&hints
, 0, sizeof(hints
));
2179 hints
.ai_flags
= AI_NUMERICHOST
;
2180 hints
.ai_family
= family
;
2181 hints
.ai_socktype
= SOCK_STREAM
;
2182 error
= getaddrinfo(src_addr
, 0, &hints
, &src_res
);
2183 if (error
== EAI_NODATA
) {
2185 error
= getaddrinfo(src_addr
, 0, &hints
, &src_res
);
2188 fprintf(stderr
, "%s: %s\n", src_addr
, gai_strerror(error
));
2189 if (error
== EAI_SYSTEM
)
2190 fprintf(stderr
, "%s: %s\n", src_addr
, strerror(errno
));
2196 if (hostp
[0] == '/') {
2197 struct sockaddr_un su
;
2199 if (strlen(hostp
) >= sizeof(su
.sun_path
)) {
2200 fprintf(stderr
, "hostname too long for unix domain socket: %s",
2204 memset(&su
, 0, sizeof su
);
2205 su
.sun_family
= AF_UNIX
;
2206 strncpy(su
.sun_path
, hostp
, sizeof su
.sun_path
);
2207 printf("Trying %s...\n", hostp
);
2208 net
= socket(PF_UNIX
, SOCK_STREAM
, 0);
2213 if (connect(net
, (struct sockaddr
*)&su
, sizeof su
) == -1) {
2214 perror(su
.sun_path
);
2215 (void) NetClose(net
);
2219 } else if (hostp
[0] == '@' || hostp
[0] == '!') {
2222 family
== AF_INET6
||
2224 (hostname
= strrchr(hostp
, ':')) == NULL
)
2225 hostname
= strrchr(hostp
, '@');
2226 if (hostname
== NULL
) {
2236 portp
= strdup("telnet");
2237 } else if (*portp
== '-') {
2243 memset(&hints
, 0, sizeof(hints
));
2244 hints
.ai_flags
= AI_NUMERICHOST
;
2245 hints
.ai_family
= family
;
2246 hints
.ai_socktype
= SOCK_STREAM
;
2247 error
= getaddrinfo(hostname
, portp
, &hints
, &res
);
2249 hints
.ai_flags
= AI_CANONNAME
;
2250 error
= getaddrinfo(hostname
, portp
, &hints
, &res
);
2253 fprintf(stderr
, "%s: %s\n", hostname
, gai_strerror(error
));
2254 if (error
== EAI_SYSTEM
)
2255 fprintf(stderr
, "%s: %s\n", hostname
, strerror(errno
));
2259 if (hints
.ai_flags
== AI_NUMERICHOST
) {
2260 /* hostname has numeric */
2264 gni_err
= getnameinfo(res
->ai_addr
, res
->ai_addr
->sa_len
,
2265 _hostname
, sizeof(_hostname
) - 1, NULL
, 0,
2268 (void) strncpy(_hostname
, hostp
, sizeof(_hostname
) - 1);
2269 _hostname
[sizeof(_hostname
)-1] = '\0';
2270 hostname
= _hostname
;
2272 /* hostname has FQDN */
2274 (void) strncpy(_hostname
, hostname
, sizeof(_hostname
) - 1);
2275 else if (res
->ai_canonname
!= NULL
)
2276 strcpy(_hostname
, res
->ai_canonname
);
2278 (void) strncpy(_hostname
, hostp
, sizeof(_hostname
) - 1);
2279 _hostname
[sizeof(_hostname
)-1] = '\0';
2280 hostname
= _hostname
;
2286 if (srcroute
!= 0) {
2287 static char hostbuf
[BUFSIZ
];
2289 if (af_error
== 0) { /* save intermediate hostnames for retry */
2290 strncpy(hostbuf
, hostp
, BUFSIZ
- 1);
2291 hostbuf
[BUFSIZ
- 1] = '\0';
2295 result
= sourceroute(res
, hostp
, &srp
, &srlen
, &proto
, &opt
);
2298 if (family
== AF_UNSPEC
&& af_error
== 0 &&
2299 switch_af(&res
) == 1) {
2306 } else if (result
== -1) {
2307 printf("Bad source route option: %s\n", hostp
);
2313 printf("Trying %s...\n", sockaddr_ntop(res
->ai_addr
));
2314 net
= socket(res
->ai_family
, res
->ai_socktype
, res
->ai_protocol
);
2318 if (family
== AF_UNSPEC
&& af_error
== 0 &&
2319 switch_af(&res
) == 1) {
2324 perror("telnet: socket");
2327 if (srp
&& setsockopt(net
, proto
, opt
, (char *)srp
, srlen
) < 0)
2328 perror("setsockopt (source route)");
2329 #if defined(IPPROTO_IP) && defined(IP_TOS)
2330 if (res
->ai_family
== PF_INET
) {
2331 # if defined(HAS_GETTOS)
2333 if (tos
< 0 && (tp
= gettosbyname("telnet", "tcp")))
2337 tos
= IPTOS_LOWDELAY
;
2339 && (setsockopt(net
, IPPROTO_IP
, IP_TOS
,
2340 (char *)&tos
, sizeof(int)) < 0)
2341 && (errno
!= ENOPROTOOPT
))
2342 perror("telnet: setsockopt (IP_TOS) (ignored)");
2344 #endif /* defined(IPPROTO_IP) && defined(IP_TOS) */
2346 if (telnet_debug
&& SetSockOpt(net
, SOL_SOCKET
, SO_DEBUG
, 1) < 0) {
2347 perror("setsockopt (SO_DEBUG)");
2350 if (src_addr
!= NULL
) {
2351 for (src_res
= src_res0
; src_res
!= NULL
; src_res
= src_res
->ai_next
)
2352 if (src_res
->ai_family
== res
->ai_family
)
2354 if (src_res
== NULL
)
2356 if (bind(net
, src_res
->ai_addr
, src_res
->ai_addrlen
) == -1) {
2358 if (family
== AF_UNSPEC
&& af_error
== 0 &&
2359 switch_af(&res
) == 1) {
2361 (void) NetClose(net
);
2366 (void) NetClose(net
);
2371 if (connect(net
, res
->ai_addr
, res
->ai_addrlen
) < 0) {
2372 struct addrinfo
*next
;
2374 next
= res
->ai_next
;
2375 /* If already an af failed, only try same af. */
2377 while (next
!= NULL
&& next
->ai_family
!= res
->ai_family
)
2378 next
= next
->ai_next
;
2379 warn("connect to address %s", sockaddr_ntop(res
->ai_addr
));
2382 (void) NetClose(net
);
2385 warnx("Unable to connect to remote host");
2386 (void) NetClose(net
);
2390 #ifdef AUTHENTICATION
2392 auth_encrypt_connect(connected
);
2395 } while (connected
== 0);
2397 if (src_res0
!= NULL
)
2398 freeaddrinfo(src_res0
);
2399 cmdrc(hostp
, hostname
);
2401 if (autologin
&& user
== NULL
) {
2404 user
= getenv("USER");
2406 ((pw
= getpwnam(user
)) && pw
->pw_uid
!= getuid())) {
2407 if ((pw
= getpwuid(getuid())))
2414 env_define("USER", user
);
2417 (void) call(status
, "status", "notmuch", 0);
2418 if (setjmp(peerdied
) == 0)
2420 (void) NetClose(net
);
2421 ExitString("Connection closed by foreign host.\n",1);
2426 if (src_res0
!= NULL
)
2427 freeaddrinfo(src_res0
);
2431 #define HELPINDENT (sizeof ("connect"))
2434 openhelp
[] = "connect to a site",
2435 closehelp
[] = "close current connection",
2436 logouthelp
[] = "forcibly logout remote user and close the connection",
2437 quithelp
[] = "exit telnet",
2438 statushelp
[] = "print status information",
2439 helphelp
[] = "print help information",
2440 sendhelp
[] = "transmit special characters ('send ?' for more)",
2441 sethelp
[] = "set operating parameters ('set ?' for more)",
2442 unsethelp
[] = "unset operating parameters ('unset ?' for more)",
2443 togglestring
[] ="toggle operating parameters ('toggle ?' for more)",
2444 slchelp
[] = "change state of special characters ('slc ?' for more)",
2445 displayhelp
[] = "display operating parameters",
2446 #ifdef AUTHENTICATION
2447 authhelp
[] = "turn on (off) authentication ('auth ?' for more)",
2450 encrypthelp
[] = "turn on (off) encryption ('encrypt ?' for more)",
2451 #endif /* ENCRYPTION */
2452 zhelp
[] = "suspend telnet",
2453 shellhelp
[] = "invoke a subshell",
2454 envhelp
[] = "change environment variables ('environ ?' for more)",
2455 modestring
[] = "try to enter line or character mode ('mode ?' for more)";
2457 static Command cmdtab
[] = {
2458 { "close", closehelp
, bye
, 1 },
2459 { "logout", logouthelp
, (int (*)(int, char **))logout
, 1 },
2460 { "display", displayhelp
, display
, 0 },
2461 { "mode", modestring
, modecmd
, 0 },
2462 { "telnet", openhelp
, tn
, 0 },
2463 { "open", openhelp
, tn
, 0 },
2464 { "quit", quithelp
, (int (*)(int, char **))quit
, 0 },
2465 { "send", sendhelp
, sendcmd
, 0 },
2466 { "set", sethelp
, setcmd
, 0 },
2467 { "unset", unsethelp
, unsetcmd
, 0 },
2468 { "status", statushelp
, status
, 0 },
2469 { "toggle", togglestring
, toggle
, 0 },
2470 { "slc", slchelp
, slccmd
, 0 },
2471 #ifdef AUTHENTICATION
2472 { "auth", authhelp
, auth_cmd
, 0 },
2475 { "encrypt", encrypthelp
, encrypt_cmd
, 0 },
2476 #endif /* ENCRYPTION */
2477 { "z", zhelp
, (int (*)(int, char **))suspend
, 0 },
2478 { "!", shellhelp
, shell
, 1 },
2479 { "environ", envhelp
, env_cmd
, 0 },
2480 { "?", helphelp
, help
, 0 },
2481 { NULL
, NULL
, NULL
, 0 }
2484 static char crmodhelp
[] = "deprecated command -- use 'toggle crmod' instead";
2485 static char escapehelp
[] = "deprecated command -- use 'set escape' instead";
2487 static Command cmdtab2
[] = {
2488 { "help", 0, help
, 0 },
2489 { "escape", escapehelp
, setescape
, 0 },
2490 { "crmod", crmodhelp
, (int (*)(int, char **))togcrmod
, 0 },
2491 { NULL
, NULL
, NULL
, 0 }
2496 * Call routine with argc, argv set from args (terminated by 0).
2500 call(intrtn_t routine
, ...)
2506 va_start(ap
, routine
);
2507 while ((args
[argno
++] = va_arg(ap
, char *)) != NULL
);
2509 return (*routine
)(argno
-1, args
);
2518 if ((cm
= (Command
*) genget(name
, (char **) cmdtab
, sizeof(Command
))))
2520 return (Command
*) genget(name
, (char **) cmdtab2
, sizeof(Command
));
2524 command(int top
, const char *tbuf
, int cnt
)
2532 (void) signal(SIGINT
, SIG_DFL
);
2533 (void) signal(SIGQUIT
, SIG_DFL
);
2536 if (rlogin
== _POSIX_VDISABLE
)
2537 printf("%s> ", prompt
);
2541 while (cnt
> 0 && (*cp
++ = *tbuf
++) != '\n')
2544 if (cp
== line
|| *--cp
!= '\n' || cp
== line
)
2547 if (rlogin
== _POSIX_VDISABLE
)
2548 printf("%s\n", line
);
2551 if (rlogin
!= _POSIX_VDISABLE
)
2552 printf("%s> ", prompt
);
2553 if (fgets(line
, sizeof(line
), stdin
) == NULL
) {
2554 if (feof(stdin
) || ferror(stdin
)) {
2564 if (margv
[0] == NULL
) {
2567 c
= getcmd(margv
[0]);
2568 if (Ambiguous((void *)c
)) {
2569 printf("?Ambiguous command\n");
2573 printf("?Invalid command\n");
2576 if (c
->needconnect
&& !connected
) {
2577 printf("?Need to be connected first.\n");
2580 if ((*c
->handler
)(margc
, margv
)) {
2586 longjmp(toplevel
, 1);
2597 help(int argc
, char *argv
[])
2602 printf("Commands may be abbreviated. Commands are:\n\n");
2603 for (c
= cmdtab
; c
->name
; c
++)
2605 printf("%-*s\t%s\n", (int)HELPINDENT
, c
->name
,
2610 else while (--argc
> 0) {
2614 if (Ambiguous((void *)c
))
2615 printf("?Ambiguous help command %s\n", arg
);
2617 printf("?Invalid help command %s\n", arg
);
2619 printf("%s\n", c
->help
);
2624 static char *rcname
= NULL
;
2625 static char rcbuf
[128];
2628 cmdrc(const char *m1
, const char *m2
)
2633 int l1
= strlen(m1
);
2634 int l2
= strlen(m2
);
2635 char m1save
[MAXHOSTNAMELEN
+ 1];
2636 char temp
[sizeof(line
)];
2641 strlcpy(m1save
, m1
, sizeof(m1save
));
2644 if (rcname
== NULL
) {
2645 rcname
= getenv("HOME");
2646 if (rcname
&& (strlen(rcname
) + 10) < sizeof(rcbuf
))
2647 strcpy(rcbuf
, rcname
);
2650 strcat(rcbuf
, "/.telnetrc");
2654 if ((rcfile
= fopen(rcname
, "r")) == NULL
) {
2659 if (fgets(line
, sizeof(line
), rcfile
) == NULL
)
2666 if (!isspace(line
[0]))
2669 if (gotmachine
== 0) {
2670 if (isspace(line
[0]))
2672 if (strncasecmp(line
, m1
, l1
) == 0) {
2673 strncpy(temp
, &line
[l1
], sizeof(line
) - l1
);
2674 strncpy(line
, temp
, sizeof(line
) - l1
);
2675 } else if (strncasecmp(line
, m2
, l2
) == 0) {
2676 strncpy(temp
, &line
[l2
], sizeof(line
) - l2
);
2677 strncpy(line
, temp
, sizeof(line
) - l2
);
2678 } else if (strncasecmp(line
, "DEFAULT", 7) == 0) {
2679 strncpy(temp
, &line
[7], sizeof(line
) - 7);
2680 strncpy(line
, temp
, sizeof(line
) - 7);
2683 if (line
[0] != ' ' && line
[0] != '\t' && line
[0] != '\n')
2688 if (margv
[0] == NULL
)
2690 c
= getcmd(margv
[0]);
2691 if (Ambiguous((void *)c
)) {
2692 printf("?Ambiguous command: %s\n", margv
[0]);
2696 printf("?Invalid command: %s\n", margv
[0]);
2700 * This should never happen...
2702 if (c
->needconnect
&& !connected
) {
2703 printf("?Need to be connected first for %s.\n", margv
[0]);
2706 (*c
->handler
)(margc
, margv
);
2712 * Source route is handed in as
2713 * [!]@hop1@hop2...[@|:]dst
2714 * If the leading ! is present, it is a
2715 * strict source route, otherwise it is
2716 * assmed to be a loose source route.
2718 * We fill in the source route option as
2719 * hop1,hop2,hop3...dest
2720 * and return a pointer to hop1, which will
2721 * be the address to connect() to.
2725 * res: ponter to addrinfo structure which contains sockaddr to
2726 * the host to connect to.
2728 * arg: pointer to route list to decipher
2730 * cpp: If *cpp is not equal to NULL, this is a
2731 * pointer to a pointer to a character array
2732 * that should be filled in with the option.
2734 * lenp: pointer to an integer that contains the
2735 * length of *cpp if *cpp != NULL.
2737 * protop: pointer to an integer that should be filled in with
2738 * appropriate protocol for setsockopt, as socket
2741 * optp: pointer to an integer that should be filled in with
2742 * appropriate option for setsockopt, as socket protocol
2747 * If the return value is 1, then all operations are
2748 * successful. If the
2749 * return value is -1, there was a syntax error in the
2750 * option, either unknown characters, or too many hosts.
2751 * If the return value is 0, one of the hostnames in the
2752 * path is unknown, and *cpp is set to point to the bad
2755 * *cpp: If *cpp was equal to NULL, it will be filled
2756 * in with a pointer to our static area that has
2757 * the option filled in. This will be 32bit aligned.
2759 * *lenp: This will be filled in with how long the option
2760 * pointed to by *cpp is.
2762 * *protop: This will be filled in with appropriate protocol for
2763 * setsockopt, as socket protocol family.
2765 * *optp: This will be filled in with appropriate option for
2766 * setsockopt, as socket protocol family.
2769 sourceroute(struct addrinfo
*ai
, char *arg
, char **cpp
, int *lenp
, int *protop
, int *optp
)
2771 static char buf
[1024 + ALIGNBYTES
]; /*XXX*/
2772 char *cp
, *cp2
, *lsrp
, *ep
;
2773 struct sockaddr_in
*_sin
;
2775 #ifdef COMPAT_RFC1883 /* XXX */
2776 struct cmsghdr
*cmsg
;
2779 struct addrinfo hints
, *res
;
2784 * Verify the arguments, and make sure we have
2785 * at least 7 bytes for the option.
2787 if (cpp
== NULL
|| lenp
== NULL
)
2790 switch (res
->ai_family
) {
2797 if (*lenp
< (int)CMSG_SPACE(sizeof(struct ip6_rthdr
) +
2798 sizeof(struct in6_addr
)))
2805 * Decide whether we have a buffer passed to us,
2806 * or if we need to use our own static buffer.
2812 *cpp
= lsrp
= (char *)ALIGN(buf
);
2819 if (ai
->ai_family
== AF_INET6
) {
2821 * RFC3542 has obsoleted IPV6_PKTOPTIONS socket option.
2823 #ifdef COMPAT_RFC1883 /* XXX */
2827 *protop
= IPPROTO_IPV6
;
2828 *optp
= IPV6_PKTOPTIONS
;
2831 #endif /* COMPAT_RFC1883 */
2836 * Next, decide whether we have a loose source
2837 * route or a strict source route, and fill in
2838 * the begining of the option.
2842 *lsrp
++ = IPOPT_SSRR
;
2844 *lsrp
++ = IPOPT_LSRR
;
2849 lsrp
++; /* skip over length, we'll fill it in later */
2851 *protop
= IPPROTO_IP
;
2856 memset(&hints
, 0, sizeof(hints
));
2857 hints
.ai_family
= ai
->ai_family
;
2858 hints
.ai_socktype
= SOCK_STREAM
;
2862 ai
->ai_family
!= AF_INET6
&&
2866 else for (cp2
= cp
; (c
= *cp2
); cp2
++) {
2871 } else if (c
== '@') {
2875 ai
->ai_family
!= AF_INET6
&&
2886 hints
.ai_flags
= AI_NUMERICHOST
;
2887 error
= getaddrinfo(cp
, NULL
, &hints
, &res
);
2888 if (error
== EAI_NODATA
) {
2890 error
= getaddrinfo(cp
, NULL
, &hints
, &res
);
2893 fprintf(stderr
, "%s: %s\n", cp
, gai_strerror(error
));
2894 if (error
== EAI_SYSTEM
)
2895 fprintf(stderr
, "%s: %s\n", cp
,
2901 if (res
->ai_family
== AF_INET6
) {
2906 _sin
= (struct sockaddr_in
*)res
->ai_addr
;
2907 memcpy(lsrp
, (char *)&_sin
->sin_addr
, 4);
2915 * Check to make sure there is space for next address
2918 #ifdef COMPAT_RFC1883 /* XXX */
2919 if (res
->ai_family
== AF_INET6
) {
2920 if (((char *)CMSG_DATA(cmsg
) +
2921 sizeof(struct ip6_rthdr
)) > ep
)
2924 #endif /* COMPAT_RFC1883 */
2931 if (res
->ai_family
== AF_INET6
) {
2932 #ifdef COMPAT_RFC1883 /* XXX */
2934 #endif /* COMPAT_RFC1883 */
2938 if ((*(*cpp
+IPOPT_OLEN
) = lsrp
- *cpp
) <= 7) {
2943 *lsrp
++ = IPOPT_NOP
; /* 32 bit word align it */
2944 *lenp
= lsrp
- *cpp
;