]>
git.saurik.com Git - apple/network_cmds.git/blob - telnet.tproj/commands.c
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
34 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD: src/crypto/telnet/telnet/commands.c,v 1.12.2.5 2002/04/13 10:59:08 markm Exp $");
41 #define __unused __attribute__((__unused__))
45 static const char sccsid
[] = "@(#)commands.c 8.4 (Berkeley) 5/30/95";
48 #include <sys/param.h>
51 #include <sys/socket.h>
52 #include <netinet/in.h>
65 #include <arpa/telnet.h>
66 #include <arpa/inet.h>
78 #include <libtelnet/auth.h>
81 #include <libtelnet/encrypt.h>
84 #include <netinet/in_systm.h>
85 #include <netinet/ip.h>
86 #include <netinet/ip6.h>
88 #ifndef MAXHOSTNAMELEN
89 #define MAXHOSTNAMELEN 256
92 typedef int (*intrtn_t
)(int, char **);
95 extern int auth_togdebug(int);
98 extern int EncryptAutoEnc(int);
99 extern int EncryptAutoDec(int);
100 extern int EncryptDebug(int);
101 extern int EncryptVerbose(int);
102 #endif /* ENCRYPTION */
103 #if defined(IPPROTO_IP) && defined(IP_TOS)
105 #endif /* defined(IPPROTO_IP) && defined(IP_TOS) */
108 static char _hostname
[MAXHOSTNAMELEN
];
110 static int help(int, char **);
111 static int call(intrtn_t
, ...);
112 static void cmdrc(char *, char *);
114 static int switch_af(struct addrinfo
**);
116 static int togglehelp(void);
117 static int send_tncmd(void (*)(int, int), const char *, char *);
118 static int setmod(int);
119 static int clearmode(int);
120 static int modehelp(void);
121 static int sourceroute(struct addrinfo
*, char *, char **, int *, int *, int *);
124 const char *name
; /* command name */
125 const char *help
; /* help string (NULL for no help) */
126 int (*handler
)(int, char **); /* routine which executes command */
127 int needconnect
; /* Do we need to be connected to execute? */
130 static char line
[256];
131 static char saveline
[256];
133 static char *margv
[20];
136 #include <sys/wait.h>
137 #define PATH_OPIEKEY "/usr/bin/opiekey"
139 opie_calc(int argc
, char *argv
[])
144 printf("%s sequence challenge\n", argv
[0]);
150 execv(PATH_OPIEKEY
, argv
);
156 (void) wait(&status
);
157 if (WIFEXITED(status
))
158 return (WEXITSTATUS(status
));
172 if (*cp
== '!') { /* Special case shell escape */
173 strcpy(saveline
, line
); /* save for shell command */
174 *argp
++ = strdup("!"); /* No room in string to get this */
186 for (cp2
= cp
; c
!= '\0'; c
= *++cp
) {
194 if ((c
= *++cp
) == '\0')
196 } else if (c
== '"') {
199 } else if (c
== '\'') {
202 } else if (isspace(c
))
216 * Make a character string into a number.
218 * Todo: 1. Could take random integers (12, 0x12, 012, 0b1).
231 c
= b
| 0x40; /* DEL */
244 * Construct a control character sequence
245 * for a special character.
252 * The only way I could get the Sun 3.5 compiler
254 * if ((unsigned int)c >= 0x80)
255 * was to assign "c" to an unsigned int variable...
258 unsigned int uic
= (unsigned int)c
;
262 if (c
== (cc_t
)_POSIX_VDISABLE
) {
267 buf
[1] = ((c
>>6)&07) + '0';
268 buf
[2] = ((c
>>3)&07) + '0';
269 buf
[3] = (c
&07) + '0';
271 } else if (uic
>= 0x20) {
283 * The following are data structures and routines for
284 * the "send" command.
289 const char *name
; /* How user refers to it (case independent) */
290 const char *help
; /* Help information (0 ==> no help) */
291 int needconnect
; /* Need to be connected */
292 int narg
; /* Number of arguments */
293 int (*handler
)(char *, ...); /* Routine to perform (for special ops) */
294 int nbyte
; /* Number of bytes to send this command */
295 int what
; /* Character to be sent (<0 ==> special) */
303 send_dontcmd(char *),
304 send_willcmd(char *),
305 send_wontcmd(char *);
307 static struct sendlist Sendlist
[] = {
308 { "ao", "Send Telnet Abort output", 1, 0, NULL
, 2, AO
},
309 { "ayt", "Send Telnet 'Are You There'", 1, 0, NULL
, 2, AYT
},
310 { "brk", "Send Telnet Break", 1, 0, NULL
, 2, BREAK
},
311 { "break", NULL
, 1, 0, NULL
, 2, BREAK
},
312 { "ec", "Send Telnet Erase Character", 1, 0, NULL
, 2, EC
},
313 { "el", "Send Telnet Erase Line", 1, 0, NULL
, 2, EL
},
314 { "escape", "Send current escape character",1, 0, (int (*)(char *, ...))send_esc
, 1, 0 },
315 { "ga", "Send Telnet 'Go Ahead' sequence", 1, 0, NULL
, 2, GA
},
316 { "ip", "Send Telnet Interrupt Process",1, 0, NULL
, 2, IP
},
317 { "intp", NULL
, 1, 0, NULL
, 2, IP
},
318 { "interrupt", NULL
, 1, 0, NULL
, 2, IP
},
319 { "intr", NULL
, 1, 0, NULL
, 2, IP
},
320 { "nop", "Send Telnet 'No operation'", 1, 0, NULL
, 2, NOP
},
321 { "eor", "Send Telnet 'End of Record'", 1, 0, NULL
, 2, EOR
},
322 { "abort", "Send Telnet 'Abort Process'", 1, 0, NULL
, 2, ABORT
},
323 { "susp", "Send Telnet 'Suspend Process'",1, 0, NULL
, 2, SUSP
},
324 { "eof", "Send Telnet End of File Character", 1, 0, NULL
, 2, xEOF
},
325 { "synch", "Perform Telnet 'Synch operation'", 1, 0, (int (*)(char *, ...))dosynch
, 2, 0 },
326 { "getstatus", "Send request for STATUS", 1, 0, (int (*)(char *, ...))get_status
, 6, 0 },
327 { "?", "Display send options", 0, 0, (int (*)(char *, ...))send_help
, 0, 0 },
328 { "help", NULL
, 0, 0, (int (*)(char *, ...))send_help
, 0, 0 },
329 { "do", NULL
, 0, 1, (int (*)(char *, ...))send_docmd
, 3, 0 },
330 { "dont", NULL
, 0, 1, (int (*)(char *, ...))send_dontcmd
, 3, 0 },
331 { "will", NULL
, 0, 1, (int (*)(char *, ...))send_willcmd
, 3, 0 },
332 { "wont", NULL
, 0, 1, (int (*)(char *, ...))send_wontcmd
, 3, 0 },
333 { NULL
, NULL
, 0, 0, NULL
, 0, 0 }
336 #define GETSEND(name) ((struct sendlist *) genget(name, (char **) Sendlist, \
337 sizeof(struct sendlist)))
340 sendcmd(int argc
, char *argv
[])
342 int count
; /* how many bytes we are going to need to send */
344 struct sendlist
*s
; /* pointer to current command */
349 printf("need at least one argument for 'send' command\n");
350 printf("'send ?' for help\n");
354 * First, validate all the send arguments.
355 * In addition, we see how much space we are going to need, and
356 * whether or not we will be doing a "SYNCH" operation (which
357 * flushes the network queue).
360 for (i
= 1; i
< argc
; i
++) {
361 s
= GETSEND(argv
[i
]);
363 printf("Unknown send argument '%s'\n'send ?' for help.\n",
366 } else if (Ambiguous((void *)s
)) {
367 printf("Ambiguous send argument '%s'\n'send ?' for help.\n",
371 if (i
+ s
->narg
>= argc
) {
373 "Need %d argument%s to 'send %s' command. 'send %s ?' for help.\n",
374 s
->narg
, s
->narg
== 1 ? "" : "s", s
->name
, s
->name
);
378 if ((void *)s
->handler
== (void *)send_help
) {
384 needconnect
+= s
->needconnect
;
386 if (!connected
&& needconnect
) {
387 printf("?Need to be connected first.\n");
388 printf("'send ?' for help\n");
391 /* Now, do we have enough room? */
392 if (NETROOM() < count
) {
393 printf("There is not enough room in the buffer TO the network\n");
394 printf("to process your request. Nothing will be done.\n");
395 printf("('send synch' will throw away most data in the network\n");
396 printf("buffer, if this might help.)\n");
399 /* OK, they are all OK, now go through again and actually send */
401 for (i
= 1; i
< argc
; i
++) {
402 if ((s
= GETSEND(argv
[i
])) == 0) {
403 fprintf(stderr
, "Telnet 'send' error - argument disappeared!\n");
409 success
+= (*s
->handler
)((s
->narg
> 0) ? argv
[i
+1] : 0,
410 (s
->narg
> 1) ? argv
[i
+2] : 0);
413 NET2ADD(IAC
, s
->what
);
414 printoption("SENT", IAC
, s
->what
);
417 return (count
== success
);
428 send_docmd(char *name
)
430 return(send_tncmd(send_do
, "do", name
));
437 return(send_tncmd(send_dont
, "dont", name
));
441 send_willcmd(char *name
)
443 return(send_tncmd(send_will
, "will", name
));
447 send_wontcmd(char *name
)
449 return(send_tncmd(send_wont
, "wont", name
));
453 send_tncmd(void (*func
)(int, int), const char *cmd
, char *name
)
456 extern char *telopts
[];
459 if (isprefix(name
, "help") || isprefix(name
, "?")) {
462 printf("Usage: send %s <value|option>\n", cmd
);
463 printf("\"value\" must be from 0 to 255\n");
464 printf("Valid options are:\n\t");
467 for (cpp
= telopts
; *cpp
; cpp
++) {
468 len
= strlen(*cpp
) + 3;
469 if (col
+ len
> 65) {
473 printf(" \"%s\"", *cpp
);
479 cpp
= (char **)genget(name
, telopts
, sizeof(char *));
480 if (Ambiguous(cpp
)) {
481 fprintf(stderr
,"'%s': ambiguous argument ('send %s ?' for help).\n",
490 while (*cp
>= '0' && *cp
<= '9') {
496 fprintf(stderr
, "'%s': unknown argument ('send %s ?' for help).\n",
499 } else if (val
< 0 || val
> 255) {
500 fprintf(stderr
, "'%s': bad value ('send %s ?' for help).\n",
506 printf("?Need to be connected first.\n");
516 struct sendlist
*s
; /* pointer to current command */
517 for (s
= Sendlist
; s
->name
; s
++) {
519 printf("%-15s %s\n", s
->name
, s
->help
);
525 * The following are the routines and data structures referred
526 * to by the arguments to the "toggle" command.
541 (SetSockOpt(net
, SOL_SOCKET
, SO_DEBUG
, debug
)) < 0) {
542 perror("setsockopt (SO_DEBUG)");
546 if (net
> 0 && SetSockOpt(net
, SOL_SOCKET
, SO_DEBUG
, 1) < 0)
547 perror("setsockopt (SO_DEBUG)");
549 printf("Cannot turn off socket debugging\n");
559 printf("Will send carriage returns as telnet <CR><LF>.\n");
561 printf("Will send carriage returns as telnet <CR><NUL>.\n");
571 donebinarytoggle
= 1;
576 if (my_want_state_is_will(TELOPT_BINARY
) &&
577 my_want_state_is_do(TELOPT_BINARY
)) {
579 } else if (my_want_state_is_wont(TELOPT_BINARY
) &&
580 my_want_state_is_dont(TELOPT_BINARY
)) {
583 val
= binmode
? 0 : 1;
587 if (my_want_state_is_will(TELOPT_BINARY
) &&
588 my_want_state_is_do(TELOPT_BINARY
)) {
589 printf("Already operating in binary mode with remote host.\n");
591 printf("Negotiating binary mode with remote host.\n");
595 if (my_want_state_is_wont(TELOPT_BINARY
) &&
596 my_want_state_is_dont(TELOPT_BINARY
)) {
597 printf("Already in network ascii mode with remote host.\n");
599 printf("Negotiating network ascii mode with remote host.\n");
609 donebinarytoggle
= 1;
612 val
= my_want_state_is_do(TELOPT_BINARY
) ? 0 : 1;
615 if (my_want_state_is_do(TELOPT_BINARY
)) {
616 printf("Already receiving in binary mode.\n");
618 printf("Negotiating binary mode on input.\n");
622 if (my_want_state_is_dont(TELOPT_BINARY
)) {
623 printf("Already receiving in network ascii mode.\n");
625 printf("Negotiating network ascii mode on input.\n");
635 donebinarytoggle
= 1;
638 val
= my_want_state_is_will(TELOPT_BINARY
) ? 0 : 1;
641 if (my_want_state_is_will(TELOPT_BINARY
)) {
642 printf("Already transmitting in binary mode.\n");
644 printf("Negotiating binary mode on output.\n");
648 if (my_want_state_is_wont(TELOPT_BINARY
)) {
649 printf("Already transmitting in network ascii mode.\n");
651 printf("Negotiating network ascii mode on output.\n");
659 const char *name
; /* name of toggle */
660 const char *help
; /* help message */
661 int (*handler
)(int); /* routine to do actual setting */
663 const char *actionexplanation
;
666 static struct togglelist Togglelist
[] = {
668 "flushing of output when sending interrupt characters",
671 "flush output when sending interrupt characters" },
673 "automatic sending of interrupt characters in urgent mode",
676 "send interrupt characters in urgent mode" },
677 #ifdef AUTHENTICATION
679 "automatic sending of login and/or authentication info",
682 "send login name and/or authentication information" },
684 "Toggle authentication debugging",
687 "print authentication debugging information" },
691 "automatic encryption of data stream",
694 "automatically encrypt output" },
696 "automatic decryption of data stream",
699 "automatically decrypt input" },
701 "Toggle verbose encryption output",
704 "print verbose encryption output" },
706 "Toggle encryption debugging",
709 "print encryption debugging information" },
710 #endif /* ENCRYPTION */
712 "don't read ~/.telnetrc file",
715 "skip reading of ~/.telnetrc file" },
717 "sending and receiving of binary data",
722 "receiving of binary data",
727 "sending of binary data",
732 "sending carriage returns as telnet <CR><LF>",
733 (int (*)(int))togcrlf
,
737 "mapping of received carriage returns",
740 "map carriage return on output" },
742 "local recognition of certain control characters",
743 (int (*)(int))lclchars
,
745 "recognize certain control characters" },
746 { " ", "", NULL
, NULL
, NULL
}, /* empty line */
749 (int (*)(int))togdebug
,
751 "turn on socket level debugging" },
753 "printing of hexadecimal network data (debugging)",
756 "print hexadecimal representation of network traffic" },
758 "output of \"netdata\" to user readable format (debugging)",
761 "print user readable output for \"netdata\"" },
763 "viewing of options processing (debugging)",
766 "show option processing" },
768 "(debugging) toggle printing of hexadecimal terminal data",
771 "print hexadecimal representation of terminal traffic" },
774 (int (*)(int))togglehelp
,
777 { NULL
, NULL
, NULL
, NULL
, NULL
},
780 (int (*)(int))togglehelp
,
783 { NULL
, NULL
, NULL
, NULL
, NULL
}
789 struct togglelist
*c
;
791 for (c
= Togglelist
; c
->name
; c
++) {
794 printf("%-15s toggle %s\n", c
->name
, c
->help
);
800 printf("%-15s %s\n", "?", "display help information");
805 settogglehelp(int set
)
807 struct togglelist
*c
;
809 for (c
= Togglelist
; c
->name
; c
++) {
812 printf("%-15s %s %s\n", c
->name
, set
? "enable" : "disable",
820 #define GETTOGGLE(name) (struct togglelist *) \
821 genget(name, (char **) Togglelist, sizeof(struct togglelist))
824 toggle(int argc
, char *argv
[])
828 struct togglelist
*c
;
832 "Need an argument to 'toggle' command. 'toggle ?' for help.\n");
840 if (Ambiguous((void *)c
)) {
841 fprintf(stderr
, "'%s': ambiguous argument ('toggle ?' for help).\n",
845 fprintf(stderr
, "'%s': unknown argument ('toggle ?' for help).\n",
850 *c
->variable
= !*c
->variable
; /* invert it */
851 if (c
->actionexplanation
) {
852 printf("%s %s.\n", *c
->variable
? "Will" : "Won't",
853 c
->actionexplanation
);
857 retval
&= (*c
->handler
)(-1);
865 * The following perform the "set" command.
869 struct termio new_tc
= { 0, 0, 0, 0, {}, 0, 0 };
873 const char *name
; /* name */
874 const char *help
; /* help information */
875 void (*handler
)(char *);
876 cc_t
*charp
; /* where it is located at */
879 static struct setlist Setlist
[] = {
880 #ifdef KLUDGELINEMODE
881 { "echo", "character to toggle local echoing on/off", NULL
, &echoc
},
883 { "escape", "character to escape back to telnet command mode", NULL
, &escape
},
884 { "rlogin", "rlogin escape character", 0, &rlogin
},
885 { "tracefile", "file to write trace information to", SetNetTrace
, (cc_t
*)NetTraceFile
},
886 { " ", "", NULL
, NULL
},
887 { " ", "The following need 'localchars' to be toggled true", NULL
, NULL
},
888 { "flushoutput", "character to cause an Abort Output", NULL
, termFlushCharp
},
889 { "interrupt", "character to cause an Interrupt Process", NULL
, termIntCharp
},
890 { "quit", "character to cause an Abort process", NULL
, termQuitCharp
},
891 { "eof", "character to cause an EOF ", NULL
, termEofCharp
},
892 { " ", "", NULL
, NULL
},
893 { " ", "The following are for local editing in linemode", NULL
, NULL
},
894 { "erase", "character to use to erase a character", NULL
, termEraseCharp
},
895 { "kill", "character to use to erase a line", NULL
, termKillCharp
},
896 { "lnext", "character to use for literal next", NULL
, termLiteralNextCharp
},
897 { "susp", "character to cause a Suspend Process", NULL
, termSuspCharp
},
898 { "reprint", "character to use for line reprint", NULL
, termRprntCharp
},
899 { "worderase", "character to use to erase a word", NULL
, termWerasCharp
},
900 { "start", "character to use for XON", NULL
, termStartCharp
},
901 { "stop", "character to use for XOFF", NULL
, termStopCharp
},
902 { "forw1", "alternate end of line character", NULL
, termForw1Charp
},
903 { "forw2", "alternate end of line character", NULL
, termForw2Charp
},
904 { "ayt", "alternate AYT character", NULL
, termAytCharp
},
905 { NULL
, NULL
, NULL
, NULL
}
908 static struct setlist
*
911 return (struct setlist
*)
912 genget(name
, (char **) Setlist
, sizeof(struct setlist
));
916 set_escape_char(char *s
)
918 if (rlogin
!= _POSIX_VDISABLE
) {
919 rlogin
= (s
&& *s
) ? special(s
) : _POSIX_VDISABLE
;
920 printf("Telnet rlogin escape character is '%s'.\n",
923 escape
= (s
&& *s
) ? special(s
) : _POSIX_VDISABLE
;
924 printf("Telnet escape character is '%s'.\n", control(escape
));
929 setcmd(int argc
, char *argv
[])
933 struct togglelist
*c
;
935 if (argc
< 2 || argc
> 3) {
936 printf("Format is 'set Name Value'\n'set ?' for help.\n");
939 if ((argc
== 2) && (isprefix(argv
[1], "?") || isprefix(argv
[1], "help"))) {
940 for (ct
= Setlist
; ct
->name
; ct
++)
941 printf("%-15s %s\n", ct
->name
, ct
->help
);
944 printf("%-15s %s\n", "?", "display help information");
948 ct
= getset(argv
[1]);
950 c
= GETTOGGLE(argv
[1]);
952 fprintf(stderr
, "'%s': unknown argument ('set ?' for help).\n",
955 } else if (Ambiguous((void *)c
)) {
956 fprintf(stderr
, "'%s': ambiguous argument ('set ?' for help).\n",
961 if ((argc
== 2) || (strcmp("on", argv
[2]) == 0))
963 else if (strcmp("off", argv
[2]) == 0)
966 printf("Format is 'set togglename [on|off]'\n'set ?' for help.\n");
969 if (c
->actionexplanation
) {
970 printf("%s %s.\n", *c
->variable
? "Will" : "Won't",
971 c
->actionexplanation
);
976 } else if (argc
!= 3) {
977 printf("Format is 'set Name Value'\n'set ?' for help.\n");
979 } else if (Ambiguous((void *)ct
)) {
980 fprintf(stderr
, "'%s': ambiguous argument ('set ?' for help).\n",
983 } else if (ct
->handler
) {
984 (*ct
->handler
)(argv
[2]);
985 printf("%s set to \"%s\".\n", ct
->name
, (char *)ct
->charp
);
987 if (strcmp("off", argv
[2])) {
988 value
= special(argv
[2]);
990 value
= _POSIX_VDISABLE
;
992 *(ct
->charp
) = (cc_t
)value
;
993 printf("%s character is '%s'.\n", ct
->name
, control(*(ct
->charp
)));
1000 unsetcmd(int argc
, char *argv
[])
1003 struct togglelist
*c
;
1008 "Need an argument to 'unset' command. 'unset ?' for help.\n");
1011 if (isprefix(argv
[1], "?") || isprefix(argv
[1], "help")) {
1012 for (ct
= Setlist
; ct
->name
; ct
++)
1013 printf("%-15s %s\n", ct
->name
, ct
->help
);
1016 printf("%-15s %s\n", "?", "display help information");
1026 c
= GETTOGGLE(name
);
1028 fprintf(stderr
, "'%s': unknown argument ('unset ?' for help).\n",
1031 } else if (Ambiguous((void *)c
)) {
1032 fprintf(stderr
, "'%s': ambiguous argument ('unset ?' for help).\n",
1038 if (c
->actionexplanation
) {
1039 printf("%s %s.\n", *c
->variable
? "Will" : "Won't",
1040 c
->actionexplanation
);
1045 } else if (Ambiguous((void *)ct
)) {
1046 fprintf(stderr
, "'%s': ambiguous argument ('unset ?' for help).\n",
1049 } else if (ct
->handler
) {
1051 printf("%s reset to \"%s\".\n", ct
->name
, (char *)ct
->charp
);
1053 *(ct
->charp
) = _POSIX_VDISABLE
;
1054 printf("%s character is '%s'.\n", ct
->name
, control(*(ct
->charp
)));
1061 * The following are the data structures and routines for the
1064 #ifdef KLUDGELINEMODE
1065 extern int kludgelinemode
;
1071 send_wont(TELOPT_LINEMODE
, 1);
1072 send_dont(TELOPT_SGA
, 1);
1073 send_dont(TELOPT_ECHO
, 1);
1081 #ifdef KLUDGELINEMODE
1083 send_dont(TELOPT_SGA
, 1);
1085 send_will(TELOPT_LINEMODE
, 1);
1086 send_dont(TELOPT_ECHO
, 1);
1093 #ifdef KLUDGELINEMODE
1095 send_do(TELOPT_SGA
, 1);
1098 send_wont(TELOPT_LINEMODE
, 1);
1099 send_do(TELOPT_ECHO
, 1);
1104 dolmmode(int bit
, int on
)
1107 extern int linemode
;
1109 if (my_want_state_is_wont(TELOPT_LINEMODE
)) {
1110 printf("?Need to have LINEMODE option enabled first.\n");
1111 printf("'mode ?' for help.\n");
1116 c
= (linemode
| bit
);
1118 c
= (linemode
& ~bit
);
1126 return dolmmode(bit
, 1);
1132 return dolmmode(bit
, 0);
1136 const char *name
; /* command name */
1137 const char *help
; /* help string */
1138 int (*handler
)(int);/* routine which executes command */
1139 int needconnect
; /* Do we need to be connected to execute? */
1143 static struct modelist ModeList
[] = {
1144 { "character", "Disable LINEMODE option", (int (*)(int))docharmode
, 1, 0 },
1145 #ifdef KLUDGELINEMODE
1146 { "", "(or disable obsolete line-by-line mode)", NULL
, 0, 0 },
1148 { "line", "Enable LINEMODE option", (int (*)(int))dolinemode
, 1, 0 },
1149 #ifdef KLUDGELINEMODE
1150 { "", "(or enable obsolete line-by-line mode)", NULL
, 0, 0 },
1152 { "", "", NULL
, 0, 0 },
1153 { "", "These require the LINEMODE option to be enabled", NULL
, 0, 0 },
1154 { "isig", "Enable signal trapping", setmod
, 1, MODE_TRAPSIG
},
1155 { "+isig", 0, setmod
, 1, MODE_TRAPSIG
},
1156 { "-isig", "Disable signal trapping", clearmode
, 1, MODE_TRAPSIG
},
1157 { "edit", "Enable character editing", setmod
, 1, MODE_EDIT
},
1158 { "+edit", 0, setmod
, 1, MODE_EDIT
},
1159 { "-edit", "Disable character editing", clearmode
, 1, MODE_EDIT
},
1160 { "softtabs", "Enable tab expansion", setmod
, 1, MODE_SOFT_TAB
},
1161 { "+softtabs", 0, setmod
, 1, MODE_SOFT_TAB
},
1162 { "-softtabs", "Disable character editing", clearmode
, 1, MODE_SOFT_TAB
},
1163 { "litecho", "Enable literal character echo", setmod
, 1, MODE_LIT_ECHO
},
1164 { "+litecho", 0, setmod
, 1, MODE_LIT_ECHO
},
1165 { "-litecho", "Disable literal character echo", clearmode
, 1, MODE_LIT_ECHO
},
1166 { "help", 0, (int (*)(int))modehelp
, 0, 0 },
1167 #ifdef KLUDGELINEMODE
1168 { "kludgeline", 0, (int (*)(int))dokludgemode
, 1, 0 },
1170 { "", "", NULL
, 0, 0 },
1171 { "?", "Print help information", (int (*)(int))modehelp
, 0, 0 },
1172 { NULL
, NULL
, NULL
, 0, 0 },
1179 struct modelist
*mt
;
1181 printf("format is: 'mode Mode', where 'Mode' is one of:\n\n");
1182 for (mt
= ModeList
; mt
->name
; mt
++) {
1185 printf("%-15s %s\n", mt
->name
, mt
->help
);
1193 #define GETMODECMD(name) (struct modelist *) \
1194 genget(name, (char **) ModeList, sizeof(struct modelist))
1197 modecmd(int argc
, char *argv
[])
1199 struct modelist
*mt
;
1202 printf("'mode' command requires an argument\n");
1203 printf("'mode ?' for help.\n");
1204 } else if ((mt
= GETMODECMD(argv
[1])) == 0) {
1205 fprintf(stderr
, "Unknown mode '%s' ('mode ?' for help).\n", argv
[1]);
1206 } else if (Ambiguous((void *)mt
)) {
1207 fprintf(stderr
, "Ambiguous mode '%s' ('mode ?' for help).\n", argv
[1]);
1208 } else if (mt
->needconnect
&& !connected
) {
1209 printf("?Need to be connected first.\n");
1210 printf("'mode ?' for help.\n");
1211 } else if (mt
->handler
) {
1212 return (*mt
->handler
)(mt
->arg1
);
1218 * The following data structures and routines implement the
1219 * "display" command.
1223 display(int argc
, char *argv
[])
1225 struct togglelist
*tl
;
1228 #define dotog(tl) if (tl->variable && tl->actionexplanation) { \
1229 if (*tl->variable) { \
1234 printf(" %s.\n", tl->actionexplanation); \
1237 #define doset(sl) if (sl->name && *sl->name != ' ') { \
1238 if (sl->handler == 0) \
1239 printf("%-15s [%s]\n", sl->name, control(*sl->charp)); \
1241 printf("%-15s \"%s\"\n", sl->name, (char *)sl->charp); \
1245 for (tl
= Togglelist
; tl
->name
; tl
++) {
1249 for (sl
= Setlist
; sl
->name
; sl
++) {
1255 for (i
= 1; i
< argc
; i
++) {
1256 sl
= getset(argv
[i
]);
1257 tl
= GETTOGGLE(argv
[i
]);
1258 if (Ambiguous((void *)sl
) || Ambiguous((void *)tl
)) {
1259 printf("?Ambiguous argument '%s'.\n", argv
[i
]);
1261 } else if (!sl
&& !tl
) {
1262 printf("?Unknown argument '%s'.\n", argv
[i
]);
1274 /*@*/optionstatus();
1277 #endif /* ENCRYPTION */
1284 * The following are the data structures, and many of the routines,
1285 * relating to command processing.
1289 * Set the escape character.
1292 setescape(int argc
, char *argv
[])
1298 "Deprecated usage - please use 'set escape%s%s' in the future.\n",
1299 (argc
> 2)? " ":"", (argc
> 2)? argv
[1]: "");
1303 printf("new escape character: ");
1304 (void) fgets(buf
, sizeof(buf
), stdin
);
1309 (void) fflush(stdout
);
1317 printf("Deprecated usage - please use 'toggle crmod' in the future.\n");
1318 printf("%s map carriage return on output.\n", crmod
? "Will" : "Won't");
1319 (void) fflush(stdout
);
1329 long oldrows
, oldcols
, newrows
, newcols
, err_
;
1331 err_
= (TerminalWindowSize(&oldrows
, &oldcols
) == 0) ? 1 : 0;
1332 (void) kill(0, SIGTSTP
);
1334 * If we didn't get the window size before the SUSPEND, but we
1335 * can get them now (?), then send the NAWS to make sure that
1336 * we are set up for the right window size.
1338 if (TerminalWindowSize(&newrows
, &newcols
) && connected
&&
1339 (err_
|| ((oldrows
!= newrows
) || (oldcols
!= newcols
)))) {
1343 /* reget parameters in case they were changed */
1344 TerminalSaveState();
1347 printf("Suspend is not supported. Try the '!' command instead\n");
1353 shell(int argc
, char *argv
[] __unused
)
1355 long oldrows
, oldcols
, newrows
, newcols
, err_
;
1359 err_
= (TerminalWindowSize(&oldrows
, &oldcols
) == 0) ? 1 : 0;
1362 perror("Fork failed\n");
1368 * Fire up the shell in the child.
1370 const char *shellp
, *shellname
;
1372 shellp
= getenv("SHELL");
1375 if ((shellname
= strrchr(shellp
, '/')) == 0)
1380 execl(shellp
, shellname
, "-c", &saveline
[1], (char *)0);
1382 execl(shellp
, shellname
, (char *)0);
1387 (void)wait((int *)0); /* Wait for the shell to complete */
1389 if (TerminalWindowSize(&newrows
, &newcols
) && connected
&&
1390 (err_
|| ((oldrows
!= newrows
) || (oldcols
!= newcols
)))) {
1399 bye(int argc
, char *argv
[])
1401 extern int resettermname
;
1404 (void) shutdown(net
, 2);
1405 printf("Connection closed.\n");
1406 (void) NetClose(net
);
1409 #ifdef AUTHENTICATION
1411 auth_encrypt_connect(connected
);
1417 if ((argc
!= 2) || (strcmp(argv
[1], "fromquit") != 0)) {
1418 longjmp(toplevel
, 1);
1421 return 1; /* Keep lint, etc., happy */
1427 (void) call(bye
, "bye", "fromquit", 0);
1434 send_do(TELOPT_LOGOUT
, 1);
1447 void (*handler
)(int);
1451 static void slc_help(void);
1453 struct slclist SlcList
[] = {
1454 { "export", "Use local special character definitions",
1455 (void (*)(int))slc_mode_export
, 0 },
1456 { "import", "Use remote special character definitions",
1457 slc_mode_import
, 1 },
1458 { "check", "Verify remote special character definitions",
1459 slc_mode_import
, 0 },
1460 { "help", NULL
, (void (*)(int))slc_help
, 0 },
1461 { "?", "Print help information", (void (*)(int))slc_help
, 0 },
1462 { NULL
, NULL
, NULL
, 0 },
1470 for (c
= SlcList
; c
->name
; c
++) {
1473 printf("%-15s %s\n", c
->name
, c
->help
);
1480 static struct slclist
*
1483 return (struct slclist
*)
1484 genget(name
, (char **) SlcList
, sizeof(struct slclist
));
1488 slccmd(int argc
, char *argv
[])
1494 "Need an argument to 'slc' command. 'slc ?' for help.\n");
1497 c
= getslc(argv
[1]);
1499 fprintf(stderr
, "'%s': unknown argument ('slc ?' for help).\n",
1503 if (Ambiguous((void *)c
)) {
1504 fprintf(stderr
, "'%s': ambiguous argument ('slc ?' for help).\n",
1508 (*c
->handler
)(c
->arg
);
1514 * The ENVIRON command.
1520 void (*handler
)(unsigned char *, unsigned char *);
1524 extern struct env_lst
*
1525 env_define(const unsigned char *, unsigned char *);
1527 env_undefine(unsigned char *),
1528 env_export(const unsigned char *),
1529 env_unexport(const unsigned char *),
1530 env_send(unsigned char *),
1531 #if defined(OLD_ENVIRON) && defined(ENV_HACK)
1532 env_varval(unsigned char *),
1538 struct envlist EnvList
[] = {
1539 { "define", "Define an environment variable",
1540 (void (*)(unsigned char *, unsigned char *))env_define
, 2 },
1541 { "undefine", "Undefine an environment variable",
1542 (void (*)(unsigned char *, unsigned char *))env_undefine
, 1 },
1543 { "export", "Mark an environment variable for automatic export",
1544 (void (*)(unsigned char *, unsigned char *))env_export
, 1 },
1545 { "unexport", "Don't mark an environment variable for automatic export",
1546 (void (*)(unsigned char *, unsigned char *))env_unexport
, 1 },
1547 { "send", "Send an environment variable", (void (*)(unsigned char *, unsigned char *))env_send
, 1 },
1548 { "list", "List the current environment variables",
1549 (void (*)(unsigned char *, unsigned char *))env_list
, 0 },
1550 #if defined(OLD_ENVIRON) && defined(ENV_HACK)
1551 { "varval", "Reverse VAR and VALUE (auto, right, wrong, status)",
1552 (void (*)(unsigned char *, unsigned char *))env_varval
, 1 },
1554 { "help", NULL
, (void (*)(unsigned char *, unsigned char *))env_help
, 0 },
1555 { "?", "Print help information", (void (*)(unsigned char *, unsigned char *))env_help
, 0 },
1556 { NULL
, NULL
, NULL
, 0 },
1564 for (c
= EnvList
; c
->name
; c
++) {
1567 printf("%-15s %s\n", c
->name
, c
->help
);
1574 static struct envlist
*
1575 getenvcmd(char *name
)
1577 return (struct envlist
*)
1578 genget(name
, (char **) EnvList
, sizeof(struct envlist
));
1582 env_cmd(int argc
, char *argv
[])
1588 "Need an argument to 'environ' command. 'environ ?' for help.\n");
1591 c
= getenvcmd(argv
[1]);
1593 fprintf(stderr
, "'%s': unknown argument ('environ ?' for help).\n",
1597 if (Ambiguous((void *)c
)) {
1598 fprintf(stderr
, "'%s': ambiguous argument ('environ ?' for help).\n",
1602 if (c
->narg
+ 2 != argc
&& strcasecmp(argv
[1],"define")==0 && c
->narg
+ 1 != argc
) {
1604 "Need %s%d argument%s to 'environ %s' command. 'environ ?' for help.\n",
1605 c
->narg
< argc
+ 2 ? "only " : "",
1606 c
->narg
, c
->narg
== 1 ? "" : "s", c
->name
);
1609 (*c
->handler
)(argv
[2], argv
[3]);
1614 struct env_lst
*next
; /* pointer to next structure */
1615 struct env_lst
*prev
; /* pointer to previous structure */
1616 unsigned char *var
; /* pointer to variable name */
1617 unsigned char *value
; /* pointer to variable value */
1618 int export
; /* 1 -> export with default list of variables */
1619 int welldefined
; /* A well defined variable */
1622 struct env_lst envlisthead
;
1624 static struct env_lst
*
1625 env_find(const unsigned char *var
)
1629 for (ep
= envlisthead
.next
; ep
; ep
= ep
->next
) {
1630 if (strcmp(ep
->var
, var
) == 0)
1643 const char *safe_vars
[]=
1644 {"USER", "PRINTER", "DISPLAY", "TERM", "COLUMNS", "LINES"};
1646 for(i
=0;i
<sizeof(safe_vars
)/sizeof(const char *);i
++) {
1647 if(ev
=getenv(safe_vars
[i
])) {
1648 ep
=env_define((unsigned char *)safe_vars
[i
],(unsigned char *)ev
);
1654 * Special case for DISPLAY variable. If it is ":0.0" or
1655 * "unix:0.0", we have to get rid of "unix" and insert our
1658 if ((ep
= env_find("DISPLAY"))
1659 && ((*ep
->value
== ':')
1660 || (strncmp((char *)ep
->value
, "unix:", 5) == 0))) {
1662 char *cp
, *cp2
= strchr((char *)ep
->value
, ':');
1664 gethostname(hbuf
, 256);
1666 cp
= (char *)malloc(strlen(hbuf
) + strlen(cp2
) + 1);
1667 sprintf((char *)cp
, "%s%s", hbuf
, cp2
);
1669 ep
->value
= (unsigned char *)cp
;
1672 * If USER is not defined, but LOGNAME is, then add
1673 * USER with the value from LOGNAME. By default, we
1674 * don't export the USER variable.
1676 if ((env_find("USER") == NULL
) && (ep
= env_find("LOGNAME"))) {
1677 env_define("USER", ep
->value
);
1678 env_unexport("USER");
1680 env_export("DISPLAY");
1681 env_export("PRINTER");
1685 env_define(const unsigned char *var
, unsigned char *value
)
1690 if ((ep
= env_find(var
))) {
1696 ep
= (struct env_lst
*)malloc(sizeof(struct env_lst
));
1697 ep
->next
= envlisthead
.next
;
1698 envlisthead
.next
= ep
;
1699 ep
->prev
= &envlisthead
;
1701 ep
->next
->prev
= ep
;
1704 ep
->welldefined
= opt_welldefined((const char *)var
);
1706 ep
->var
= (unsigned char *)strdup((const char *)var
);
1709 ep
->value
= (unsigned char *)strdup((const char *)value
);
1710 else if(ev
=getenv((const char *)var
))
1711 ep
->value
= (unsigned char *)strdup(ev
);
1712 else ep
->value
= (unsigned char *)strdup("");
1717 env_undefine(unsigned char *var
)
1721 if ((ep
= env_find(var
))) {
1722 ep
->prev
->next
= ep
->next
;
1724 ep
->next
->prev
= ep
->prev
;
1734 env_export(const unsigned char *var
)
1738 if ((ep
= env_find(var
)))
1743 env_unexport(const unsigned char *var
)
1747 if ((ep
= env_find(var
)))
1752 env_send(unsigned char *var
)
1756 if (my_state_is_wont(TELOPT_NEW_ENVIRON
)
1758 && my_state_is_wont(TELOPT_OLD_ENVIRON
)
1762 "Cannot send '%s': Telnet ENVIRON option not enabled\n",
1768 fprintf(stderr
, "Cannot send '%s': variable not defined\n",
1772 env_opt_start_info();
1773 env_opt_add(ep
->var
);
1782 for (ep
= envlisthead
.next
; ep
; ep
= ep
->next
) {
1783 printf("%c %-20s %s\n", ep
->export
? '*' : ' ',
1784 ep
->var
, ep
->value
);
1789 env_default(int init
, int welldefined
)
1791 static struct env_lst
*nep
= NULL
;
1798 while ((nep
= nep
->next
)) {
1799 if (nep
->export
&& (nep
->welldefined
== welldefined
))
1807 env_getvalue(const unsigned char *var
)
1811 if ((ep
= env_find(var
)))
1816 #if defined(OLD_ENVIRON) && defined(ENV_HACK)
1818 env_varval(unsigned char *what
)
1820 extern int old_env_var
, old_env_value
, env_auto
;
1821 int len
= strlen((char *)what
);
1826 if (strncasecmp((char *)what
, "status", len
) == 0) {
1828 printf("%s%s", "VAR and VALUE are/will be ",
1829 "determined automatically\n");
1830 if (old_env_var
== OLD_ENV_VAR
)
1831 printf("VAR and VALUE set to correct definitions\n");
1833 printf("VAR and VALUE definitions are reversed\n");
1834 } else if (strncasecmp((char *)what
, "auto", len
) == 0) {
1836 old_env_var
= OLD_ENV_VALUE
;
1837 old_env_value
= OLD_ENV_VAR
;
1838 } else if (strncasecmp((char *)what
, "right", len
) == 0) {
1840 old_env_var
= OLD_ENV_VAR
;
1841 old_env_value
= OLD_ENV_VALUE
;
1842 } else if (strncasecmp((char *)what
, "wrong", len
) == 0) {
1844 old_env_var
= OLD_ENV_VALUE
;
1845 old_env_value
= OLD_ENV_VAR
;
1848 printf("Unknown \"varval\" command. (\"auto\", \"right\", \"wrong\", \"status\")\n");
1853 #ifdef AUTHENTICATION
1855 * The AUTHENTICATE command.
1861 int (*handler
)(char *);
1866 auth_enable(char *),
1867 auth_disable(char *),
1872 struct authlist AuthList
[] = {
1873 { "status", "Display current status of authentication information",
1874 (int (*)(char *))auth_status
, 0 },
1875 { "disable", "Disable an authentication type ('auth disable ?' for more)",
1877 { "enable", "Enable an authentication type ('auth enable ?' for more)",
1879 { "help", NULL
, (int (*)(char *))auth_help
, 0 },
1880 { "?", "Print help information", (int (*)(char *))auth_help
, 0 },
1881 { NULL
, NULL
, NULL
, 0 },
1889 for (c
= AuthList
; c
->name
; c
++) {
1892 printf("%-15s %s\n", c
->name
, c
->help
);
1901 auth_cmd(int argc
, char *argv
[])
1907 "Need an argument to 'auth' command. 'auth ?' for help.\n");
1911 c
= (struct authlist
*)
1912 genget(argv
[1], (char **) AuthList
, sizeof(struct authlist
));
1914 fprintf(stderr
, "'%s': unknown argument ('auth ?' for help).\n",
1918 if (Ambiguous((void *)c
)) {
1919 fprintf(stderr
, "'%s': ambiguous argument ('auth ?' for help).\n",
1923 if (c
->narg
+ 2 != argc
) {
1925 "Need %s%d argument%s to 'auth %s' command. 'auth ?' for help.\n",
1926 c
->narg
< argc
+ 2 ? "only " : "",
1927 c
->narg
, c
->narg
== 1 ? "" : "s", c
->name
);
1930 return((*c
->handler
)(argv
[2]));
1936 * The ENCRYPT command.
1939 struct encryptlist
{
1942 int (*handler
)(char *, char *);
1949 EncryptEnable(char *, char *),
1950 EncryptDisable(char *, char *),
1951 EncryptType(char *, char *),
1952 EncryptStart(char *),
1953 EncryptStartInput(void),
1954 EncryptStartOutput(void),
1955 EncryptStop(char *),
1956 EncryptStopInput(void),
1957 EncryptStopOutput(void),
1958 EncryptStatus(void);
1962 struct encryptlist EncryptList
[] = {
1963 { "enable", "Enable encryption. ('encrypt enable ?' for more)",
1964 EncryptEnable
, 1, 1, 2 },
1965 { "disable", "Disable encryption. ('encrypt enable ?' for more)",
1966 EncryptDisable
, 0, 1, 2 },
1967 { "type", "Set encryption type. ('encrypt type ?' for more)",
1968 EncryptType
, 0, 1, 1 },
1969 { "start", "Start encryption. ('encrypt start ?' for more)",
1970 (int (*)(char *, char *))EncryptStart
, 1, 0, 1 },
1971 { "stop", "Stop encryption. ('encrypt stop ?' for more)",
1972 (int (*)(char *, char *))EncryptStop
, 1, 0, 1 },
1973 { "input", "Start encrypting the input stream",
1974 (int (*)(char *, char *))EncryptStartInput
, 1, 0, 0 },
1975 { "-input", "Stop encrypting the input stream",
1976 (int (*)(char *, char *))EncryptStopInput
, 1, 0, 0 },
1977 { "output", "Start encrypting the output stream",
1978 (int (*)(char *, char *))EncryptStartOutput
, 1, 0, 0 },
1979 { "-output", "Stop encrypting the output stream",
1980 (int (*)(char *, char *))EncryptStopOutput
, 1, 0, 0 },
1982 { "status", "Display current status of authentication information",
1983 (int (*)(char *, char *))EncryptStatus
, 0, 0, 0 },
1984 { "help", NULL
, (int (*)(char *, char *))EncryptHelp
, 0, 0, 0 },
1985 { "?", "Print help information", (int (*)(char *, char *))EncryptHelp
, 0, 0, 0 },
1986 { NULL
, NULL
, NULL
, 0, 0, 0 },
1992 struct encryptlist
*c
;
1994 for (c
= EncryptList
; c
->name
; c
++) {
1997 printf("%-15s %s\n", c
->name
, c
->help
);
2006 encrypt_cmd(int argc
, char *argv
[])
2008 struct encryptlist
*c
;
2012 "Need an argument to 'encrypt' command. 'encrypt ?' for help.\n");
2016 c
= (struct encryptlist
*)
2017 genget(argv
[1], (char **) EncryptList
, sizeof(struct encryptlist
));
2019 fprintf(stderr
, "'%s': unknown argument ('encrypt ?' for help).\n",
2023 if (Ambiguous((void *)c
)) {
2024 fprintf(stderr
, "'%s': ambiguous argument ('encrypt ?' for help).\n",
2029 if (argc
< c
->minarg
|| argc
> c
->maxarg
) {
2030 if (c
->minarg
== c
->maxarg
) {
2031 fprintf(stderr
, "Need %s%d argument%s ",
2032 c
->minarg
< argc
? "only " : "", c
->minarg
,
2033 c
->minarg
== 1 ? "" : "s");
2035 fprintf(stderr
, "Need %s%d-%d arguments ",
2036 c
->maxarg
< argc
? "only " : "", c
->minarg
, c
->maxarg
);
2038 fprintf(stderr
, "to 'encrypt %s' command. 'encrypt ?' for help.\n",
2042 if (c
->needconnect
&& !connected
) {
2043 if (!(argc
&& (isprefix(argv
[2], "help") || isprefix(argv
[2], "?")))) {
2044 printf("?Need to be connected first.\n");
2048 return ((*c
->handler
)(argc
> 0 ? argv
[2] : 0,
2049 argc
> 1 ? argv
[3] : 0));
2051 #endif /* ENCRYPTION */
2054 * Print status about the connection.
2058 status(int argc
, char *argv
[])
2061 printf("Connected to %s.\n", hostname
);
2062 if ((argc
< 2) || strcmp(argv
[1], "notmuch")) {
2063 int mode
= getconnmode();
2065 if (my_want_state_is_will(TELOPT_LINEMODE
)) {
2066 printf("Operating with LINEMODE option\n");
2067 printf("%s line editing\n", (mode
&MODE_EDIT
) ? "Local" : "No");
2068 printf("%s catching of signals\n",
2069 (mode
&MODE_TRAPSIG
) ? "Local" : "No");
2071 #ifdef KLUDGELINEMODE
2072 } else if (kludgelinemode
&& my_want_state_is_dont(TELOPT_SGA
)) {
2073 printf("Operating in obsolete linemode\n");
2076 printf("Operating in single character mode\n");
2078 printf("Catching signals locally\n");
2080 printf("%s character echo\n", (mode
&MODE_ECHO
) ? "Local" : "Remote");
2081 if (my_want_state_is_will(TELOPT_LFLOW
))
2082 printf("%s flow control\n", (mode
&MODE_FLOW
) ? "Local" : "No");
2085 #endif /* ENCRYPTION */
2088 printf("No connection.\n");
2090 printf("Escape character is '%s'.\n", control(escape
));
2091 (void) fflush(stdout
);
2097 * Function that gets called when SIGINFO is received.
2102 (void) call(status
, "status", "notmuch", 0);
2107 sockaddr_ntop(struct sockaddr
*sa
)
2110 static char addrbuf
[INET6_ADDRSTRLEN
];
2112 switch (sa
->sa_family
) {
2114 addr
= &((struct sockaddr_in
*)sa
)->sin_addr
;
2117 addr
= &((struct sockaddr_un
*)sa
)->sun_path
;
2121 addr
= &((struct sockaddr_in6
*)sa
)->sin6_addr
;
2127 inet_ntop(sa
->sa_family
, addr
, addrbuf
, sizeof(addrbuf
));
2131 #if defined(IPSEC) && defined(IPSEC_POLICY_IPSEC)
2133 setpolicy(int lnet
, struct addrinfo
*res
, char *policy
)
2142 buf
= ipsec_set_policy(policy
, strlen(policy
));
2144 printf("%s\n", ipsec_strerror());
2147 level
= res
->ai_family
== AF_INET
? IPPROTO_IP
: IPPROTO_IPV6
;
2148 optname
= res
->ai_family
== AF_INET
? IP_IPSEC_POLICY
: IPV6_IPSEC_POLICY
;
2149 if (setsockopt(lnet
, level
, optname
, buf
, ipsec_get_policylen(buf
)) < 0){
2150 perror("setsockopt");
2161 * When an Address Family related error happend, check if retry with
2162 * another AF is possible or not.
2163 * Return 1, if retry with another af is OK. Else, return 0.
2166 switch_af(struct addrinfo
**aip
)
2169 struct addrinfo
*ai
;
2172 nextaf
= (ai
->ai_family
== AF_INET
) ? AF_INET6
: AF_INET
;
2175 while (ai
!= NULL
&& ai
->ai_family
!= nextaf
);
2185 tn(int argc
, char *argv
[])
2190 int srcroute
= 0, result
;
2191 char *cmd
, *hostp
= 0, *portp
= 0, *user
= 0;
2192 char *src_addr
= NULL
;
2193 struct addrinfo hints
, *res
, *res0
= NULL
, *src_res
, *src_res0
= NULL
;
2194 int error
= 0, af_error
= 0;
2197 printf("?Already connected to %s\n", hostname
);
2202 (void) strcpy(line
, "open ");
2204 (void) fgets(&line
[strlen(line
)], sizeof(line
) - strlen(line
), stdin
);
2212 if (strcmp(*argv
, "help") == 0 || isprefix(*argv
, "?"))
2214 if (strcmp(*argv
, "-l") == 0) {
2222 if (strcmp(*argv
, "-a") == 0) {
2227 if (strcmp(*argv
, "-s") == 0) {
2246 printf("usage: %s [-l user] [-a] [-s src_addr] host-name [port]\n", cmd
);
2253 if (src_addr
!= NULL
) {
2254 memset(&hints
, 0, sizeof(hints
));
2255 hints
.ai_flags
= AI_NUMERICHOST
;
2256 hints
.ai_family
= family
;
2257 hints
.ai_socktype
= SOCK_STREAM
;
2258 error
= getaddrinfo(src_addr
, 0, &hints
, &src_res
);
2259 if (error
== EAI_NODATA
) {
2261 error
= getaddrinfo(src_addr
, 0, &hints
, &src_res
);
2264 fprintf(stderr
, "%s: %s\n", src_addr
, gai_strerror(error
));
2265 if (error
== EAI_SYSTEM
)
2266 fprintf(stderr
, "%s: %s\n", src_addr
, strerror(errno
));
2272 if (hostp
[0] == '/') {
2273 struct sockaddr_un su
;
2275 if (strlen(hostp
) >= sizeof(su
.sun_path
)) {
2276 fprintf(stderr
, "hostname too long for unix domain socket: %s",
2280 memset(&su
, 0, sizeof su
);
2281 su
.sun_family
= AF_UNIX
;
2282 strncpy(su
.sun_path
, hostp
, sizeof su
.sun_path
);
2283 printf("Trying %s...\n", hostp
);
2284 net
= socket(PF_UNIX
, SOCK_STREAM
, 0);
2289 if (connect(net
, (struct sockaddr
*)&su
, sizeof su
) == -1) {
2290 perror(su
.sun_path
);
2291 (void) NetClose(net
);
2295 } else if (hostp
[0] == '@' || hostp
[0] == '!') {
2298 family
== AF_INET6
||
2300 (hostname
= strrchr(hostp
, ':')) == NULL
)
2301 hostname
= strrchr(hostp
, '@');
2302 if (hostname
== NULL
) {
2312 portp
= strdup("telnet");
2313 } else if (*portp
== '-') {
2319 memset(&hints
, 0, sizeof(hints
));
2320 hints
.ai_flags
= AI_NUMERICHOST
;
2321 hints
.ai_family
= family
;
2322 hints
.ai_socktype
= SOCK_STREAM
;
2323 error
= getaddrinfo(hostname
, portp
, &hints
, &res
);
2325 hints
.ai_flags
= AI_CANONNAME
;
2326 error
= getaddrinfo(hostname
, portp
, &hints
, &res
);
2329 fprintf(stderr
, "%s: %s\n", hostname
, gai_strerror(error
));
2330 if (error
== EAI_SYSTEM
)
2331 fprintf(stderr
, "%s: %s\n", hostname
, strerror(errno
));
2335 if (hints
.ai_flags
== AI_NUMERICHOST
) {
2336 /* hostname has numeric */
2340 gni_err
= getnameinfo(res
->ai_addr
, res
->ai_addr
->sa_len
,
2341 _hostname
, sizeof(_hostname
) - 1, NULL
, 0,
2344 (void) strncpy(_hostname
, hostp
, sizeof(_hostname
) - 1);
2345 _hostname
[sizeof(_hostname
)-1] = '\0';
2346 hostname
= _hostname
;
2348 /* hostname has FQDN */
2350 (void) strncpy(_hostname
, hostname
, sizeof(_hostname
) - 1);
2351 else if (res
->ai_canonname
!= NULL
)
2352 strcpy(_hostname
, res
->ai_canonname
);
2354 (void) strncpy(_hostname
, hostp
, sizeof(_hostname
) - 1);
2355 _hostname
[sizeof(_hostname
)-1] = '\0';
2356 hostname
= _hostname
;
2362 if (srcroute
!= 0) {
2363 static char hostbuf
[BUFSIZ
];
2365 if (af_error
== 0) { /* save intermediate hostnames for retry */
2366 strncpy(hostbuf
, hostp
, BUFSIZ
- 1);
2367 hostbuf
[BUFSIZ
- 1] = '\0';
2371 result
= sourceroute(res
, hostp
, &srp
, &srlen
, &proto
, &opt
);
2374 if (family
== AF_UNSPEC
&& af_error
== 0 &&
2375 switch_af(&res
) == 1) {
2382 } else if (result
== -1) {
2383 printf("Bad source route option: %s\n", hostp
);
2389 printf("Trying %s...\n", sockaddr_ntop(res
->ai_addr
));
2390 net
= socket(res
->ai_family
, res
->ai_socktype
, res
->ai_protocol
);
2394 if (family
== AF_UNSPEC
&& af_error
== 0 &&
2395 switch_af(&res
) == 1) {
2400 perror("telnet: socket");
2403 if (srp
&& setsockopt(net
, proto
, opt
, (char *)srp
, srlen
) < 0)
2404 perror("setsockopt (source route)");
2405 #if defined(IPPROTO_IP) && defined(IP_TOS)
2406 if (res
->ai_family
== PF_INET
) {
2407 # if defined(HAS_GETTOS)
2409 if (tos
< 0 && (tp
= gettosbyname("telnet", "tcp")))
2413 tos
= IPTOS_LOWDELAY
;
2415 && (setsockopt(net
, IPPROTO_IP
, IP_TOS
,
2416 (char *)&tos
, sizeof(int)) < 0)
2417 && (errno
!= ENOPROTOOPT
))
2418 perror("telnet: setsockopt (IP_TOS) (ignored)");
2420 #endif /* defined(IPPROTO_IP) && defined(IP_TOS) */
2422 if (debug
&& SetSockOpt(net
, SOL_SOCKET
, SO_DEBUG
, 1) < 0) {
2423 perror("setsockopt (SO_DEBUG)");
2426 if (src_addr
!= NULL
) {
2427 for (src_res
= src_res0
; src_res
!= 0; src_res
= src_res
->ai_next
)
2428 if (src_res
->ai_family
== res
->ai_family
)
2430 if (src_res
== NULL
)
2432 if (bind(net
, src_res
->ai_addr
, src_res
->ai_addrlen
) == -1) {
2434 if (family
== AF_UNSPEC
&& af_error
== 0 &&
2435 switch_af(&res
) == 1) {
2437 (void) NetClose(net
);
2442 (void) NetClose(net
);
2446 #if defined(IPSEC) && defined(IPSEC_POLICY_IPSEC)
2447 if (setpolicy(net
, res
, ipsec_policy_in
) < 0) {
2448 (void) NetClose(net
);
2451 if (setpolicy(net
, res
, ipsec_policy_out
) < 0) {
2452 (void) NetClose(net
);
2457 if (connect(net
, res
->ai_addr
, res
->ai_addrlen
) < 0) {
2458 struct addrinfo
*next
;
2460 next
= res
->ai_next
;
2461 /* If already an af failed, only try same af. */
2463 while (next
!= NULL
&& next
->ai_family
!= res
->ai_family
)
2464 next
= next
->ai_next
;
2465 warn("connect to address %s", sockaddr_ntop(res
->ai_addr
));
2468 (void) NetClose(net
);
2471 warnx("Unable to connect to remote host");
2472 (void) NetClose(net
);
2476 #ifdef AUTHENTICATION
2478 auth_encrypt_connect(connected
);
2481 } while (connected
== 0);
2483 if (src_res0
!= NULL
)
2484 freeaddrinfo(src_res0
);
2485 cmdrc(hostp
, hostname
);
2487 if (autologin
&& user
== NULL
) {
2490 user
= getenv("USER");
2492 ((pw
= getpwnam(user
)) && pw
->pw_uid
!= getuid())) {
2493 if ((pw
= getpwuid(getuid())))
2500 env_define("USER", user
);
2503 (void) call(status
, "status", "notmuch", 0);
2504 if (setjmp(peerdied
) == 0)
2506 (void) NetClose(net
);
2507 ExitString("Connection closed by foreign host.\n",1);
2512 if (src_res0
!= NULL
)
2513 freeaddrinfo(src_res0
);
2517 #define HELPINDENT (sizeof ("connect"))
2520 openhelp
[] = "connect to a site",
2521 closehelp
[] = "close current connection",
2522 logouthelp
[] = "forcibly logout remote user and close the connection",
2523 quithelp
[] = "exit telnet",
2524 statushelp
[] = "print status information",
2525 helphelp
[] = "print help information",
2526 sendhelp
[] = "transmit special characters ('send ?' for more)",
2527 sethelp
[] = "set operating parameters ('set ?' for more)",
2528 unsethelp
[] = "unset operating parameters ('unset ?' for more)",
2529 togglestring
[] ="toggle operating parameters ('toggle ?' for more)",
2530 slchelp
[] = "change state of special charaters ('slc ?' for more)",
2531 displayhelp
[] = "display operating parameters",
2532 #ifdef AUTHENTICATION
2533 authhelp
[] = "turn on (off) authentication ('auth ?' for more)",
2536 encrypthelp
[] = "turn on (off) encryption ('encrypt ?' for more)",
2537 #endif /* ENCRYPTION */
2538 zhelp
[] = "suspend telnet",
2540 opiehelp
[] = "compute response to OPIE challenge",
2542 shellhelp
[] = "invoke a subshell",
2543 envhelp
[] = "change environment variables ('environ ?' for more)",
2544 modestring
[] = "try to enter line or character mode ('mode ?' for more)";
2546 static Command cmdtab
[] = {
2547 { "close", closehelp
, bye
, 1 },
2548 { "logout", logouthelp
, (int (*)(int, char **))logout
, 1 },
2549 { "display", displayhelp
, display
, 0 },
2550 { "mode", modestring
, modecmd
, 0 },
2551 { "telnet", openhelp
, tn
, 0 },
2552 { "open", openhelp
, tn
, 0 },
2553 { "quit", quithelp
, (int (*)(int, char **))quit
, 0 },
2554 { "send", sendhelp
, sendcmd
, 0 },
2555 { "set", sethelp
, setcmd
, 0 },
2556 { "unset", unsethelp
, unsetcmd
, 0 },
2557 { "status", statushelp
, status
, 0 },
2558 { "toggle", togglestring
, toggle
, 0 },
2559 { "slc", slchelp
, slccmd
, 0 },
2560 #ifdef AUTHENTICATION
2561 { "auth", authhelp
, auth_cmd
, 0 },
2564 { "encrypt", encrypthelp
, encrypt_cmd
, 0 },
2565 #endif /* ENCRYPTION */
2566 { "z", zhelp
, (int (*)(int, char **))suspend
, 0 },
2567 { "!", shellhelp
, shell
, 1 },
2568 { "environ", envhelp
, env_cmd
, 0 },
2569 { "?", helphelp
, help
, 0 },
2571 { "opie", opiehelp
, opie_calc
, 0 },
2573 { NULL
, NULL
, NULL
, 0 }
2576 static char crmodhelp
[] = "deprecated command -- use 'toggle crmod' instead";
2577 static char escapehelp
[] = "deprecated command -- use 'set escape' instead";
2579 static Command cmdtab2
[] = {
2580 { "help", 0, help
, 0 },
2581 { "escape", escapehelp
, setescape
, 0 },
2582 { "crmod", crmodhelp
, (int (*)(int, char **))togcrmod
, 0 },
2583 { NULL
, NULL
, NULL
, 0 }
2588 * Call routine with argc, argv set from args (terminated by 0).
2592 call(intrtn_t routine
, ...)
2598 va_start(ap
, routine
);
2599 while ((args
[argno
++] = va_arg(ap
, char *)) != 0);
2601 return (*routine
)(argno
-1, args
);
2610 if ((cm
= (Command
*) genget(name
, (char **) cmdtab
, sizeof(Command
))))
2612 return (Command
*) genget(name
, (char **) cmdtab2
, sizeof(Command
));
2616 command(int top
, const char *tbuf
, int cnt
)
2624 (void) signal(SIGINT
, SIG_DFL
);
2625 (void) signal(SIGQUIT
, SIG_DFL
);
2628 if (rlogin
== _POSIX_VDISABLE
)
2629 printf("%s> ", prompt
);
2633 while (cnt
> 0 && (*cp
++ = *tbuf
++) != '\n')
2636 if (cp
== line
|| *--cp
!= '\n' || cp
== line
)
2639 if (rlogin
== _POSIX_VDISABLE
)
2640 printf("%s\n", line
);
2643 if (rlogin
!= _POSIX_VDISABLE
)
2644 printf("%s> ", prompt
);
2645 if (fgets(line
, sizeof(line
), stdin
) == NULL
) {
2646 if (feof(stdin
) || ferror(stdin
)) {
2656 if (margv
[0] == 0) {
2659 c
= getcmd(margv
[0]);
2660 if (Ambiguous((void *)c
)) {
2661 printf("?Ambiguous command\n");
2665 printf("?Invalid command\n");
2668 if (c
->needconnect
&& !connected
) {
2669 printf("?Need to be connected first.\n");
2672 if ((*c
->handler
)(margc
, margv
)) {
2678 longjmp(toplevel
, 1);
2689 help(int argc
, char *argv
[])
2694 printf("Commands may be abbreviated. Commands are:\n\n");
2695 for (c
= cmdtab
; c
->name
; c
++)
2697 printf("%-*s\t%s\n", (int)HELPINDENT
, c
->name
,
2702 else while (--argc
> 0) {
2706 if (Ambiguous((void *)c
))
2707 printf("?Ambiguous help command %s\n", arg
);
2708 else if (c
== (Command
*)0)
2709 printf("?Invalid help command %s\n", arg
);
2711 printf("%s\n", c
->help
);
2716 static char *rcname
= 0;
2717 static char rcbuf
[128];
2720 cmdrc(char *m1
, char *m2
)
2725 int l1
= strlen(m1
);
2726 int l2
= strlen(m2
);
2727 char m1save
[MAXHOSTNAMELEN
];
2732 strlcpy(m1save
, m1
, sizeof(m1save
));
2736 rcname
= getenv("HOME");
2737 if (rcname
&& (strlen(rcname
) + 10) < sizeof(rcbuf
))
2738 strcpy(rcbuf
, rcname
);
2741 strcat(rcbuf
, "/.telnetrc");
2745 if ((rcfile
= fopen(rcname
, "r")) == 0) {
2750 if (fgets(line
, sizeof(line
), rcfile
) == NULL
)
2757 if (!isspace(line
[0]))
2760 if (gotmachine
== 0) {
2761 if (isspace(line
[0]))
2763 if (strncasecmp(line
, m1
, l1
) == 0)
2764 strncpy(line
, &line
[l1
], sizeof(line
) - l1
);
2765 else if (strncasecmp(line
, m2
, l2
) == 0)
2766 strncpy(line
, &line
[l2
], sizeof(line
) - l2
);
2767 else if (strncasecmp(line
, "DEFAULT", 7) == 0)
2768 strncpy(line
, &line
[7], sizeof(line
) - 7);
2771 if (line
[0] != ' ' && line
[0] != '\t' && line
[0] != '\n')
2778 c
= getcmd(margv
[0]);
2779 if (Ambiguous((void *)c
)) {
2780 printf("?Ambiguous command: %s\n", margv
[0]);
2784 printf("?Invalid command: %s\n", margv
[0]);
2788 * This should never happen...
2790 if (c
->needconnect
&& !connected
) {
2791 printf("?Need to be connected first for %s.\n", margv
[0]);
2794 (*c
->handler
)(margc
, margv
);
2800 * Source route is handed in as
2801 * [!]@hop1@hop2...[@|:]dst
2802 * If the leading ! is present, it is a
2803 * strict source route, otherwise it is
2804 * assmed to be a loose source route.
2806 * We fill in the source route option as
2807 * hop1,hop2,hop3...dest
2808 * and return a pointer to hop1, which will
2809 * be the address to connect() to.
2813 * res: ponter to addrinfo structure which contains sockaddr to
2814 * the host to connect to.
2816 * arg: pointer to route list to decipher
2818 * cpp: If *cpp is not equal to NULL, this is a
2819 * pointer to a pointer to a character array
2820 * that should be filled in with the option.
2822 * lenp: pointer to an integer that contains the
2823 * length of *cpp if *cpp != NULL.
2825 * protop: pointer to an integer that should be filled in with
2826 * appropriate protocol for setsockopt, as socket
2829 * optp: pointer to an integer that should be filled in with
2830 * appropriate option for setsockopt, as socket protocol
2835 * If the return value is 1, then all operations are
2836 * successful. If the
2837 * return value is -1, there was a syntax error in the
2838 * option, either unknown characters, or too many hosts.
2839 * If the return value is 0, one of the hostnames in the
2840 * path is unknown, and *cpp is set to point to the bad
2843 * *cpp: If *cpp was equal to NULL, it will be filled
2844 * in with a pointer to our static area that has
2845 * the option filled in. This will be 32bit aligned.
2847 * *lenp: This will be filled in with how long the option
2848 * pointed to by *cpp is.
2850 * *protop: This will be filled in with appropriate protocol for
2851 * setsockopt, as socket protocol family.
2853 * *optp: This will be filled in with appropriate option for
2854 * setsockopt, as socket protocol family.
2857 sourceroute(struct addrinfo
*ai
, char *arg
, char **cpp
, int *lenp
, int *protop
, int *optp
)
2859 static char buf
[1024 + ALIGNBYTES
]; /*XXX*/
2860 char *cp
, *cp2
, *lsrp
, *ep
;
2861 struct sockaddr_in
*_sin
;
2863 struct sockaddr_in6
*sin6
;
2864 struct cmsghdr
*cmsg
;
2866 struct addrinfo hints
, *res
;
2871 * Verify the arguments, and make sure we have
2872 * at least 7 bytes for the option.
2874 if (cpp
== NULL
|| lenp
== NULL
)
2877 switch (res
->ai_family
) {
2884 if (*lenp
< (int)CMSG_SPACE(sizeof(struct ip6_rthdr
) +
2885 sizeof(struct in6_addr
)))
2892 * Decide whether we have a buffer passed to us,
2893 * or if we need to use our own static buffer.
2899 *cpp
= lsrp
= (char *)ALIGN(buf
);
2906 if (ai
->ai_family
== AF_INET6
) {
2907 cmsg
= inet6_rthdr_init(*cpp
, IPV6_RTHDR_TYPE_0
);
2910 *protop
= IPPROTO_IPV6
;
2911 *optp
= IPV6_PKTOPTIONS
;
2916 * Next, decide whether we have a loose source
2917 * route or a strict source route, and fill in
2918 * the begining of the option.
2922 *lsrp
++ = IPOPT_SSRR
;
2924 *lsrp
++ = IPOPT_LSRR
;
2929 lsrp
++; /* skip over length, we'll fill it in later */
2931 *protop
= IPPROTO_IP
;
2936 memset(&hints
, 0, sizeof(hints
));
2937 hints
.ai_family
= ai
->ai_family
;
2938 hints
.ai_socktype
= SOCK_STREAM
;
2942 ai
->ai_family
!= AF_INET6
&&
2946 else for (cp2
= cp
; (c
= *cp2
); cp2
++) {
2951 } else if (c
== '@') {
2955 ai
->ai_family
!= AF_INET6
&&
2966 hints
.ai_flags
= AI_NUMERICHOST
;
2967 error
= getaddrinfo(cp
, NULL
, &hints
, &res
);
2968 if (error
== EAI_NODATA
) {
2970 error
= getaddrinfo(cp
, NULL
, &hints
, &res
);
2973 fprintf(stderr
, "%s: %s\n", cp
, gai_strerror(error
));
2974 if (error
== EAI_SYSTEM
)
2975 fprintf(stderr
, "%s: %s\n", cp
,
2981 if (res
->ai_family
== AF_INET6
) {
2982 sin6
= (struct sockaddr_in6
*)res
->ai_addr
;
2983 inet6_rthdr_add(cmsg
, &sin6
->sin6_addr
,
2988 _sin
= (struct sockaddr_in
*)res
->ai_addr
;
2989 memcpy(lsrp
, (char *)&_sin
->sin_addr
, 4);
2997 * Check to make sure there is space for next address
3000 if (res
->ai_family
== AF_INET6
) {
3001 if (((char *)CMSG_DATA(cmsg
) +
3002 sizeof(struct ip6_rthdr
) +
3003 ((inet6_rthdr_segments(cmsg
) + 1) *
3004 sizeof(struct in6_addr
))) > ep
)
3013 if (res
->ai_family
== AF_INET6
) {
3014 inet6_rthdr_lasthop(cmsg
, IPV6_RTHDR_LOOSE
);
3015 *lenp
= cmsg
->cmsg_len
;
3019 if ((*(*cpp
+IPOPT_OLEN
) = lsrp
- *cpp
) <= 7) {
3024 *lsrp
++ = IPOPT_NOP
; /* 32 bit word align it */
3025 *lenp
= lsrp
- *cpp
;