1 /* vi: set sw=4 ts=4: */
3 * telnet implementation for busybox
5 * Author: Tomi Ollila <too@iki.fi>
6 * Copyright (C) 1994-2000 by Tomi Ollila
8 * Created: Thu Apr 7 13:29:41 1994 too
9 * Last modified: Fri Jun 9 14:34:24 2000 too
11 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
14 * Revision 3.1 1994/04/17 11:31:54 too
16 * Modified 2000/06/13 for inclusion into BusyBox by Erik Andersen <andersen@codepoet.org>
17 * Modified 2001/05/07 to add ability to pass TTYPE to remote host by Jim McQuillan
19 * Modified 2004/02/11 to add ability to pass the USER variable to remote host
20 * by Fernando Silveira <swrh@gmx.net>
22 //config:config TELNET
23 //config: bool "telnet (8.7 kb)"
26 //config: Telnet is an interface to the TELNET protocol, but is also commonly
27 //config: used to test other simple protocols.
29 //config:config FEATURE_TELNET_TTYPE
30 //config: bool "Pass TERM type to remote host"
32 //config: depends on TELNET
34 //config: Setting this option will forward the TERM environment variable to the
35 //config: remote host you are connecting to. This is useful to make sure that
36 //config: things like ANSI colors and other control sequences behave.
38 //config:config FEATURE_TELNET_AUTOLOGIN
39 //config: bool "Pass USER type to remote host"
41 //config: depends on TELNET
43 //config: Setting this option will forward the USER environment variable to the
44 //config: remote host you are connecting to. This is useful when you need to
45 //config: log into a machine without telling the username (autologin). This
46 //config: option enables '-a' and '-l USER' options.
48 //config:config FEATURE_TELNET_WIDTH
49 //config: bool "Enable window size autodetection"
51 //config: depends on TELNET
53 //applet:IF_TELNET(APPLET(telnet, BB_DIR_USR_BIN, BB_SUID_DROP))
55 //kbuild:lib-$(CONFIG_TELNET) += telnet.o
57 //usage:#if ENABLE_FEATURE_TELNET_AUTOLOGIN
58 //usage:#define telnet_trivial_usage
59 //usage: "[-a] [-l USER] HOST [PORT]"
60 //usage:#define telnet_full_usage "\n\n"
61 //usage: "Connect to telnet server\n"
62 //usage: "\n -a Automatic login with $USER variable"
63 //usage: "\n -l USER Automatic login as USER"
66 //usage:#define telnet_trivial_usage
67 //usage: "HOST [PORT]"
68 //usage:#define telnet_full_usage "\n\n"
69 //usage: "Connect to telnet server"
72 #include <arpa/telnet.h>
73 #include <netinet/in.h>
75 #include "common_bufsiz.h"
78 /* should be in arpa/telnet.h */
79 # define IAC 255 /* interpret as command: */
80 # define DONT 254 /* you are not to use option */
81 # define DO 253 /* please, you use option */
82 # define WONT 252 /* I won't use option */
83 # define WILL 251 /* I will use option */
84 # define SB 250 /* interpret as subnegotiation */
85 # define SE 240 /* end sub negotiation */
86 # define TELOPT_ECHO 1 /* echo */
87 # define TELOPT_SGA 3 /* suppress go ahead */
88 # define TELOPT_TTYPE 24 /* terminal type */
89 # define TELOPT_NAWS 31 /* window size */
112 typedef unsigned char byte;
117 int iaclen; /* could even use byte, but it's a loss on x86 */
118 byte telstate; /* telnet negotiation state from network input */
119 byte telwish; /* DO, DONT, WILL, WONT */
123 #if ENABLE_FEATURE_TELNET_TTYPE
126 #if ENABLE_FEATURE_TELNET_AUTOLOGIN
127 const char *autologin;
129 #if ENABLE_FEATURE_TELNET_WIDTH
130 unsigned win_width, win_height;
132 /* same buffer used both for network and console read/write */
133 char buf[DATABUFSIZE];
134 /* buffer to handle telnet negotiations */
135 char iacbuf[IACBUFSIZE];
136 struct termios termios_def;
137 struct termios termios_raw;
139 #define G (*(struct globals*)bb_common_bufsiz1)
140 #define INIT_G() do { \
141 setup_common_bufsiz(); \
142 BUILD_BUG_ON(sizeof(G) > COMMON_BUFSIZE); \
146 static void rawmode(void);
147 static void cookmode(void);
148 static void do_linemode(void);
149 static void will_charmode(void);
150 static void telopt(byte c);
151 static void subneg(byte c);
153 static void iac_flush(void)
155 full_write(netfd, G.iacbuf, G.iaclen);
159 static void doexit(int ev) NORETURN;
160 static void doexit(int ev)
166 static void con_escape(void)
170 if (bb_got_signal) /* came from line mode... go raw */
173 full_write1_str("\r\nConsole escape. Commands are:\r\n\n"
174 " l go to line mode\r\n"
175 " c go to character mode\r\n"
176 " z suspend telnet\r\n"
177 " e exit telnet\r\n");
179 if (read(STDIN_FILENO, &b, 1) <= 0)
180 doexit(EXIT_FAILURE);
184 if (!bb_got_signal) {
201 doexit(EXIT_SUCCESS);
204 full_write1_str("continuing...\r\n");
212 static void handle_net_output(int len)
214 byte outbuf[2 * DATABUFSIZE];
216 byte *src = (byte*)G.buf;
217 byte *end = src + len;
227 *++dst = c; /* IAC -> IAC IAC */
229 if (c == '\r' || c == '\n') {
230 /* Enter key sends '\r' in raw mode and '\n' in cooked one.
232 * See RFC 1123 3.3.1 Telnet End-of-Line Convention.
233 * Using CR LF instead of other allowed possibilities
234 * like CR NUL - easier to talk to HTTP/SMTP servers.
236 *dst = '\r'; /* Enter -> CR LF */
241 if (dst - outbuf != 0)
242 full_write(netfd, outbuf, dst - outbuf);
245 static void handle_net_input(int len)
250 for (i = 0; i < len; i++) {
253 if (G.telstate == TS_NORMAL) { /* most typical state */
258 else if (c == '\r') {
262 /* No IACs were seen so far, no need to copy
263 * bytes within G.buf: */
267 switch (G.telstate) {
269 /* Prev char was CR. If cur one is NUL, ignore it.
270 * See RFC 1123 section 3.3.1 for discussion of telnet EOL handling.
272 G.telstate = TS_COPY;
275 /* else: fall through - need to handle CR IAC ... properly */
277 case TS_COPY: /* Prev char was ordinary */
278 /* Similar to NORMAL, but in TS_COPY we need to copy bytes */
287 case TS_IAC: /* Prev char was IAC */
288 if (c == IAC) { /* IAC IAC -> one IAC */
290 G.telstate = TS_COPY;
296 G.telstate = TS_SUB1;
305 /* DATA MARK must be added later */
307 G.telstate = TS_COPY;
311 case TS_OPT: /* Prev chars were IAC WILL/WONT/DO/DONT */
313 G.telstate = TS_COPY;
316 case TS_SUB1: /* Subnegotiation */
317 case TS_SUB2: /* Subnegotiation */
318 subneg(c); /* can change G.telstate */
323 if (G.telstate != TS_NORMAL) {
324 /* We had some IACs, or CR */
327 if (G.telstate == TS_COPY) /* we aren't in the middle of IAC */
328 G.telstate = TS_NORMAL;
333 full_write(STDOUT_FILENO, G.buf, len);
336 static void put_iac(int c)
338 G.iacbuf[G.iaclen++] = c;
341 static void put_iac2_merged(unsigned wwdd_and_c)
343 if (G.iaclen + 3 > IACBUFSIZE)
347 put_iac(wwdd_and_c >> 8);
348 put_iac(wwdd_and_c & 0xff);
350 #define put_iac2(wwdd,c) put_iac2_merged(((wwdd)<<8) + (c))
352 #if ENABLE_FEATURE_TELNET_TTYPE
353 static void put_iac_subopt(byte c, char *str)
355 int len = strlen(str) + 6; // ( 2 + 1 + 1 + strlen + 2 )
357 if (G.iaclen + len > IACBUFSIZE)
373 #if ENABLE_FEATURE_TELNET_AUTOLOGIN
374 static void put_iac_subopt_autologin(void)
376 int len = strlen(G.autologin) + 6; // (2 + 1 + 1 + strlen + 2)
377 const char *p = "USER";
379 if (G.iaclen + len > IACBUFSIZE)
384 put_iac(TELOPT_NEW_ENVIRON);
386 put_iac(NEW_ENV_VAR);
391 put_iac(NEW_ENV_VALUE);
402 #if ENABLE_FEATURE_TELNET_WIDTH
403 static void put_iac_naws(byte c, int x, int y)
405 if (G.iaclen + 9 > IACBUFSIZE)
412 /* "... & 0xff" implicitly done below */
423 static void setConMode(void)
425 if (G.telflags & UF_ECHO) {
426 if (G.charmode == CHM_TRY) {
428 printf("\r\nEntering %s mode"
429 "\r\nEscape character is '^%c'.\r\n", "character", ']');
433 if (G.charmode != CHM_OFF) {
434 G.charmode = CHM_OFF;
435 printf("\r\nEntering %s mode"
436 "\r\nEscape character is '^%c'.\r\n", "line", 'C');
442 static void will_charmode(void)
444 G.charmode = CHM_TRY;
445 G.telflags |= (UF_ECHO | UF_SGA);
448 put_iac2(DO, TELOPT_ECHO);
449 put_iac2(DO, TELOPT_SGA);
453 static void do_linemode(void)
455 G.charmode = CHM_TRY;
456 G.telflags &= ~(UF_ECHO | UF_SGA);
459 put_iac2(DONT, TELOPT_ECHO);
460 put_iac2(DONT, TELOPT_SGA);
464 static void to_notsup(char c)
466 if (G.telwish == WILL)
468 else if (G.telwish == DO)
472 static void to_echo(void)
474 /* if server requests ECHO, don't agree */
475 if (G.telwish == DO) {
476 put_iac2(WONT, TELOPT_ECHO);
479 if (G.telwish == DONT)
482 if (G.telflags & UF_ECHO) {
483 if (G.telwish == WILL)
485 } else if (G.telwish == WONT)
488 if (G.charmode != CHM_OFF)
489 G.telflags ^= UF_ECHO;
491 if (G.telflags & UF_ECHO)
492 put_iac2(DO, TELOPT_ECHO);
494 put_iac2(DONT, TELOPT_ECHO);
497 full_write1_str("\r\n"); /* sudden modec */
500 static void to_sga(void)
502 /* daemon always sends will/wont, client do/dont */
504 if (G.telflags & UF_SGA) {
505 if (G.telwish == WILL)
507 } else if (G.telwish == WONT)
510 G.telflags ^= UF_SGA; /* toggle */
511 if (G.telflags & UF_SGA)
512 put_iac2(DO, TELOPT_SGA);
514 put_iac2(DONT, TELOPT_SGA);
517 #if ENABLE_FEATURE_TELNET_TTYPE
518 static void to_ttype(void)
520 /* Tell server we will (or won't) do TTYPE */
522 put_iac2(WILL, TELOPT_TTYPE);
524 put_iac2(WONT, TELOPT_TTYPE);
528 #if ENABLE_FEATURE_TELNET_AUTOLOGIN
529 static void to_new_environ(void)
531 /* Tell server we will (or will not) do AUTOLOGIN */
533 put_iac2(WILL, TELOPT_NEW_ENVIRON);
535 put_iac2(WONT, TELOPT_NEW_ENVIRON);
539 #if ENABLE_FEATURE_TELNET_WIDTH
540 static void to_naws(void)
542 /* Tell server we will do NAWS */
543 put_iac2(WILL, TELOPT_NAWS);
547 static void telopt(byte c)
554 #if ENABLE_FEATURE_TELNET_TTYPE
558 #if ENABLE_FEATURE_TELNET_AUTOLOGIN
559 case TELOPT_NEW_ENVIRON:
560 to_new_environ(); break;
562 #if ENABLE_FEATURE_TELNET_WIDTH
565 put_iac_naws(c, G.win_width, G.win_height);
574 /* subnegotiation -- ignore all (except TTYPE,NAWS) */
575 static void subneg(byte c)
577 switch (G.telstate) {
580 G.telstate = TS_SUB2;
581 #if ENABLE_FEATURE_TELNET_TTYPE
583 if (c == TELOPT_TTYPE && G.ttype)
584 put_iac_subopt(TELOPT_TTYPE, G.ttype);
586 #if ENABLE_FEATURE_TELNET_AUTOLOGIN
588 if (c == TELOPT_NEW_ENVIRON && G.autologin)
589 put_iac_subopt_autologin();
594 G.telstate = TS_COPY;
597 G.telstate = TS_SUB1;
602 static void rawmode(void)
605 tcsetattr(0, TCSADRAIN, &G.termios_raw);
608 static void cookmode(void)
611 tcsetattr(0, TCSADRAIN, &G.termios_def);
614 int telnet_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
615 int telnet_main(int argc UNUSED_PARAM, char **argv)
620 struct pollfd ufds[2];
624 #if ENABLE_FEATURE_TELNET_TTYPE
625 G.ttype = getenv("TERM");
628 if (tcgetattr(0, &G.termios_def) >= 0) {
630 G.termios_raw = G.termios_def;
631 cfmakeraw(&G.termios_raw);
634 #if ENABLE_FEATURE_TELNET_AUTOLOGIN
635 if (1 == getopt32(argv, "al:", &G.autologin)) {
636 /* Only -a without -l USER picks $USER from envvar */
637 G.autologin = getenv("USER");
646 port = *argv ? bb_lookup_port(*argv++, "tcp", 23)
647 : bb_lookup_std_port("telnet", "tcp", 23);
648 if (*argv) /* extra params?? */
651 xmove_fd(create_and_connect_stream_or_die(host, port), netfd);
653 setsockopt_keepalive(netfd);
655 #if ENABLE_FEATURE_TELNET_WIDTH
656 get_terminal_width_height(0, &G.win_width, &G.win_height);
657 //TODO: support dynamic resize?
660 signal(SIGINT, record_signo);
662 ufds[0].fd = STDIN_FILENO;
663 ufds[0].events = POLLIN;
665 ufds[1].events = POLLIN;
668 if (poll(ufds, 2, -1) < 0) {
669 /* error, ignore and/or log something, bay go to loop */
677 // FIXME: reads can block. Need full bidirectional buffering.
679 if (ufds[0].revents) {
680 len = safe_read(STDIN_FILENO, G.buf, DATABUFSIZE);
682 doexit(EXIT_SUCCESS);
683 handle_net_output(len);
686 if (ufds[1].revents) {
687 len = safe_read(netfd, G.buf, DATABUFSIZE);
689 full_write1_str("Connection closed by foreign host\r\n");
690 doexit(EXIT_FAILURE);
692 handle_net_input(len);