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>
23 //config:config TELNET
24 //config: bool "telnet (8.7 kb)"
27 //config: Telnet is an interface to the TELNET protocol, but is also commonly
28 //config: used to test other simple protocols.
30 //config:config FEATURE_TELNET_TTYPE
31 //config: bool "Pass TERM type to remote host"
33 //config: depends on TELNET
35 //config: Setting this option will forward the TERM environment variable to the
36 //config: remote host you are connecting to. This is useful to make sure that
37 //config: things like ANSI colors and other control sequences behave.
39 //config:config FEATURE_TELNET_AUTOLOGIN
40 //config: bool "Pass USER type to remote host"
42 //config: depends on TELNET
44 //config: Setting this option will forward the USER environment variable to the
45 //config: remote host you are connecting to. This is useful when you need to
46 //config: log into a machine without telling the username (autologin). This
47 //config: option enables '-a' and '-l USER' arguments.
49 //config:config FEATURE_TELNET_WIDTH
50 //config: bool "Enable window size autodetection"
52 //config: depends on TELNET
54 //applet:IF_TELNET(APPLET(telnet, BB_DIR_USR_BIN, BB_SUID_DROP))
56 //kbuild:lib-$(CONFIG_TELNET) += telnet.o
58 //usage:#if ENABLE_FEATURE_TELNET_AUTOLOGIN
59 //usage:#define telnet_trivial_usage
60 //usage: "[-a] [-l USER] HOST [PORT]"
61 //usage:#define telnet_full_usage "\n\n"
62 //usage: "Connect to telnet server\n"
63 //usage: "\n -a Automatic login with $USER variable"
64 //usage: "\n -l USER Automatic login as USER"
67 //usage:#define telnet_trivial_usage
68 //usage: "HOST [PORT]"
69 //usage:#define telnet_full_usage "\n\n"
70 //usage: "Connect to telnet server"
73 #include <arpa/telnet.h>
74 #include <netinet/in.h>
76 #include "common_bufsiz.h"
79 /* should be in arpa/telnet.h */
80 # define IAC 255 /* interpret as command: */
81 # define DONT 254 /* you are not to use option */
82 # define DO 253 /* please, you use option */
83 # define WONT 252 /* I won't use option */
84 # define WILL 251 /* I will use option */
85 # define SB 250 /* interpret as subnegotiation */
86 # define SE 240 /* end sub negotiation */
87 # define TELOPT_ECHO 1 /* echo */
88 # define TELOPT_SGA 3 /* suppress go ahead */
89 # define TELOPT_TTYPE 24 /* terminal type */
90 # define TELOPT_NAWS 31 /* window size */
94 # define TRACE(x, y) do { if (x) printf y; } while (0)
119 typedef unsigned char byte;
124 int iaclen; /* could even use byte, but it's a loss on x86 */
125 byte telstate; /* telnet negotiation state from network input */
126 byte telwish; /* DO, DONT, WILL, WONT */
130 #if ENABLE_FEATURE_TELNET_TTYPE
133 #if ENABLE_FEATURE_TELNET_AUTOLOGIN
134 const char *autologin;
136 #if ENABLE_FEATURE_TELNET_WIDTH
137 unsigned win_width, win_height;
139 /* same buffer used both for network and console read/write */
140 char buf[DATABUFSIZE];
141 /* buffer to handle telnet negotiations */
142 char iacbuf[IACBUFSIZE];
143 struct termios termios_def;
144 struct termios termios_raw;
146 #define G (*(struct globals*)bb_common_bufsiz1)
147 #define INIT_G() do { \
148 setup_common_bufsiz(); \
149 BUILD_BUG_ON(sizeof(G) > COMMON_BUFSIZE); \
153 static void rawmode(void);
154 static void cookmode(void);
155 static void do_linemode(void);
156 static void will_charmode(void);
157 static void telopt(byte c);
158 static void subneg(byte c);
160 static void iac_flush(void)
162 full_write(netfd, G.iacbuf, G.iaclen);
166 static void doexit(int ev) NORETURN;
167 static void doexit(int ev)
173 static void con_escape(void)
177 if (bb_got_signal) /* came from line mode... go raw */
180 full_write1_str("\r\nConsole escape. Commands are:\r\n\n"
181 " l go to line mode\r\n"
182 " c go to character mode\r\n"
183 " z suspend telnet\r\n"
184 " e exit telnet\r\n");
186 if (read(STDIN_FILENO, &b, 1) <= 0)
187 doexit(EXIT_FAILURE);
191 if (!bb_got_signal) {
208 doexit(EXIT_SUCCESS);
211 full_write1_str("continuing...\r\n");
219 static void handle_net_output(int len)
221 byte outbuf[2 * DATABUFSIZE];
223 byte *src = (byte*)G.buf;
224 byte *end = src + len;
234 *++dst = c; /* IAC -> IAC IAC */
236 if (c == '\r' || c == '\n') {
237 /* Enter key sends '\r' in raw mode and '\n' in cooked one.
239 * See RFC 1123 3.3.1 Telnet End-of-Line Convention.
240 * Using CR LF instead of other allowed possibilities
241 * like CR NUL - easier to talk to HTTP/SMTP servers.
243 *dst = '\r'; /* Enter -> CR LF */
248 if (dst - outbuf != 0)
249 full_write(netfd, outbuf, dst - outbuf);
252 static void handle_net_input(int len)
257 for (i = 0; i < len; i++) {
260 if (G.telstate == TS_NORMAL) { /* most typical state */
265 else if (c == '\r') {
269 /* No IACs were seen so far, no need to copy
270 * bytes within G.buf: */
274 switch (G.telstate) {
276 /* Prev char was CR. If cur one is NUL, ignore it.
277 * See RFC 1123 section 3.3.1 for discussion of telnet EOL handling.
279 G.telstate = TS_COPY;
282 /* else: fall through - need to handle CR IAC ... properly */
284 case TS_COPY: /* Prev char was ordinary */
285 /* Similar to NORMAL, but in TS_COPY we need to copy bytes */
294 case TS_IAC: /* Prev char was IAC */
295 if (c == IAC) { /* IAC IAC -> one IAC */
297 G.telstate = TS_COPY;
303 G.telstate = TS_SUB1;
312 /* DATA MARK must be added later */
314 G.telstate = TS_COPY;
318 case TS_OPT: /* Prev chars were IAC WILL/WONT/DO/DONT */
320 G.telstate = TS_COPY;
323 case TS_SUB1: /* Subnegotiation */
324 case TS_SUB2: /* Subnegotiation */
325 subneg(c); /* can change G.telstate */
330 if (G.telstate != TS_NORMAL) {
331 /* We had some IACs, or CR */
334 if (G.telstate == TS_COPY) /* we aren't in the middle of IAC */
335 G.telstate = TS_NORMAL;
340 full_write(STDOUT_FILENO, G.buf, len);
343 static void put_iac(int c)
345 G.iacbuf[G.iaclen++] = c;
348 static void put_iac2_merged(unsigned wwdd_and_c)
350 if (G.iaclen + 3 > IACBUFSIZE)
354 put_iac(wwdd_and_c >> 8);
355 put_iac(wwdd_and_c & 0xff);
357 #define put_iac2(wwdd,c) put_iac2_merged(((wwdd)<<8) + (c))
359 #if ENABLE_FEATURE_TELNET_TTYPE
360 static void put_iac_subopt(byte c, char *str)
362 int len = strlen(str) + 6; // ( 2 + 1 + 1 + strlen + 2 )
364 if (G.iaclen + len > IACBUFSIZE)
380 #if ENABLE_FEATURE_TELNET_AUTOLOGIN
381 static void put_iac_subopt_autologin(void)
383 int len = strlen(G.autologin) + 6; // (2 + 1 + 1 + strlen + 2)
384 const char *p = "USER";
386 if (G.iaclen + len > IACBUFSIZE)
391 put_iac(TELOPT_NEW_ENVIRON);
393 put_iac(NEW_ENV_VAR);
398 put_iac(NEW_ENV_VALUE);
409 #if ENABLE_FEATURE_TELNET_WIDTH
410 static void put_iac_naws(byte c, int x, int y)
412 if (G.iaclen + 9 > IACBUFSIZE)
419 /* "... & 0xff" implicitly done below */
430 static void setConMode(void)
432 if (G.telflags & UF_ECHO) {
433 if (G.charmode == CHM_TRY) {
435 printf("\r\nEntering %s mode"
436 "\r\nEscape character is '^%c'.\r\n", "character", ']');
440 if (G.charmode != CHM_OFF) {
441 G.charmode = CHM_OFF;
442 printf("\r\nEntering %s mode"
443 "\r\nEscape character is '^%c'.\r\n", "line", 'C');
449 static void will_charmode(void)
451 G.charmode = CHM_TRY;
452 G.telflags |= (UF_ECHO | UF_SGA);
455 put_iac2(DO, TELOPT_ECHO);
456 put_iac2(DO, TELOPT_SGA);
460 static void do_linemode(void)
462 G.charmode = CHM_TRY;
463 G.telflags &= ~(UF_ECHO | UF_SGA);
466 put_iac2(DONT, TELOPT_ECHO);
467 put_iac2(DONT, TELOPT_SGA);
471 static void to_notsup(char c)
473 if (G.telwish == WILL)
475 else if (G.telwish == DO)
479 static void to_echo(void)
481 /* if server requests ECHO, don't agree */
482 if (G.telwish == DO) {
483 put_iac2(WONT, TELOPT_ECHO);
486 if (G.telwish == DONT)
489 if (G.telflags & UF_ECHO) {
490 if (G.telwish == WILL)
492 } else if (G.telwish == WONT)
495 if (G.charmode != CHM_OFF)
496 G.telflags ^= UF_ECHO;
498 if (G.telflags & UF_ECHO)
499 put_iac2(DO, TELOPT_ECHO);
501 put_iac2(DONT, TELOPT_ECHO);
504 full_write1_str("\r\n"); /* sudden modec */
507 static void to_sga(void)
509 /* daemon always sends will/wont, client do/dont */
511 if (G.telflags & UF_SGA) {
512 if (G.telwish == WILL)
514 } else if (G.telwish == WONT)
517 G.telflags ^= UF_SGA; /* toggle */
518 if (G.telflags & UF_SGA)
519 put_iac2(DO, TELOPT_SGA);
521 put_iac2(DONT, TELOPT_SGA);
524 #if ENABLE_FEATURE_TELNET_TTYPE
525 static void to_ttype(void)
527 /* Tell server we will (or won't) do TTYPE */
529 put_iac2(WILL, TELOPT_TTYPE);
531 put_iac2(WONT, TELOPT_TTYPE);
535 #if ENABLE_FEATURE_TELNET_AUTOLOGIN
536 static void to_new_environ(void)
538 /* Tell server we will (or will not) do AUTOLOGIN */
540 put_iac2(WILL, TELOPT_NEW_ENVIRON);
542 put_iac2(WONT, TELOPT_NEW_ENVIRON);
546 #if ENABLE_FEATURE_TELNET_WIDTH
547 static void to_naws(void)
549 /* Tell server we will do NAWS */
550 put_iac2(WILL, TELOPT_NAWS);
554 static void telopt(byte c)
561 #if ENABLE_FEATURE_TELNET_TTYPE
565 #if ENABLE_FEATURE_TELNET_AUTOLOGIN
566 case TELOPT_NEW_ENVIRON:
567 to_new_environ(); break;
569 #if ENABLE_FEATURE_TELNET_WIDTH
572 put_iac_naws(c, G.win_width, G.win_height);
581 /* subnegotiation -- ignore all (except TTYPE,NAWS) */
582 static void subneg(byte c)
584 switch (G.telstate) {
587 G.telstate = TS_SUB2;
588 #if ENABLE_FEATURE_TELNET_TTYPE
590 if (c == TELOPT_TTYPE && G.ttype)
591 put_iac_subopt(TELOPT_TTYPE, G.ttype);
593 #if ENABLE_FEATURE_TELNET_AUTOLOGIN
595 if (c == TELOPT_NEW_ENVIRON && G.autologin)
596 put_iac_subopt_autologin();
601 G.telstate = TS_COPY;
604 G.telstate = TS_SUB1;
609 static void rawmode(void)
612 tcsetattr(0, TCSADRAIN, &G.termios_raw);
615 static void cookmode(void)
618 tcsetattr(0, TCSADRAIN, &G.termios_def);
621 int telnet_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
622 int telnet_main(int argc UNUSED_PARAM, char **argv)
627 struct pollfd ufds[2];
631 #if ENABLE_FEATURE_TELNET_WIDTH
632 get_terminal_width_height(0, &G.win_width, &G.win_height);
635 #if ENABLE_FEATURE_TELNET_TTYPE
636 G.ttype = getenv("TERM");
639 if (tcgetattr(0, &G.termios_def) >= 0) {
641 G.termios_raw = G.termios_def;
642 cfmakeraw(&G.termios_raw);
645 #if ENABLE_FEATURE_TELNET_AUTOLOGIN
646 if (1 & getopt32(argv, "al:", &G.autologin))
647 G.autologin = getenv("USER");
655 port = bb_lookup_port(*argv ? *argv++ : "telnet", "tcp", 23);
656 if (*argv) /* extra params?? */
659 xmove_fd(create_and_connect_stream_or_die(host, port), netfd);
661 setsockopt_keepalive(netfd);
663 signal(SIGINT, record_signo);
665 ufds[0].fd = STDIN_FILENO;
666 ufds[0].events = POLLIN;
668 ufds[1].events = POLLIN;
671 if (poll(ufds, 2, -1) < 0) {
672 /* error, ignore and/or log something, bay go to loop */
680 // FIXME: reads can block. Need full bidirectional buffering.
682 if (ufds[0].revents) {
683 len = safe_read(STDIN_FILENO, G.buf, DATABUFSIZE);
685 doexit(EXIT_SUCCESS);
686 TRACE(0, ("Read con: %d\n", len));
687 handle_net_output(len);
690 if (ufds[1].revents) {
691 len = safe_read(netfd, G.buf, DATABUFSIZE);
693 full_write1_str("Connection closed by foreign host\r\n");
694 doexit(EXIT_FAILURE);
696 TRACE(0, ("Read netfd (%d): %d\n", netfd, len));
697 handle_net_input(len);