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 */
93 # define TRACE(x, y) do { if (x) printf y; } while (0)
118 typedef unsigned char byte;
123 int iaclen; /* could even use byte, but it's a loss on x86 */
124 byte telstate; /* telnet negotiation state from network input */
125 byte telwish; /* DO, DONT, WILL, WONT */
129 #if ENABLE_FEATURE_TELNET_TTYPE
132 #if ENABLE_FEATURE_TELNET_AUTOLOGIN
133 const char *autologin;
135 #if ENABLE_FEATURE_TELNET_WIDTH
136 unsigned win_width, win_height;
138 /* same buffer used both for network and console read/write */
139 char buf[DATABUFSIZE];
140 /* buffer to handle telnet negotiations */
141 char iacbuf[IACBUFSIZE];
142 struct termios termios_def;
143 struct termios termios_raw;
145 #define G (*(struct globals*)bb_common_bufsiz1)
146 #define INIT_G() do { \
147 setup_common_bufsiz(); \
148 BUILD_BUG_ON(sizeof(G) > COMMON_BUFSIZE); \
152 static void rawmode(void);
153 static void cookmode(void);
154 static void do_linemode(void);
155 static void will_charmode(void);
156 static void telopt(byte c);
157 static void subneg(byte c);
159 static void iac_flush(void)
161 full_write(netfd, G.iacbuf, G.iaclen);
165 static void doexit(int ev) NORETURN;
166 static void doexit(int ev)
172 static void con_escape(void)
176 if (bb_got_signal) /* came from line mode... go raw */
179 full_write1_str("\r\nConsole escape. Commands are:\r\n\n"
180 " l go to line mode\r\n"
181 " c go to character mode\r\n"
182 " z suspend telnet\r\n"
183 " e exit telnet\r\n");
185 if (read(STDIN_FILENO, &b, 1) <= 0)
186 doexit(EXIT_FAILURE);
190 if (!bb_got_signal) {
207 doexit(EXIT_SUCCESS);
210 full_write1_str("continuing...\r\n");
218 static void handle_net_output(int len)
220 byte outbuf[2 * DATABUFSIZE];
222 byte *src = (byte*)G.buf;
223 byte *end = src + len;
233 *++dst = c; /* IAC -> IAC IAC */
235 if (c == '\r' || c == '\n') {
236 /* Enter key sends '\r' in raw mode and '\n' in cooked one.
238 * See RFC 1123 3.3.1 Telnet End-of-Line Convention.
239 * Using CR LF instead of other allowed possibilities
240 * like CR NUL - easier to talk to HTTP/SMTP servers.
242 *dst = '\r'; /* Enter -> CR LF */
247 if (dst - outbuf != 0)
248 full_write(netfd, outbuf, dst - outbuf);
251 static void handle_net_input(int len)
256 for (i = 0; i < len; i++) {
259 if (G.telstate == TS_NORMAL) { /* most typical state */
264 else if (c == '\r') {
268 /* No IACs were seen so far, no need to copy
269 * bytes within G.buf: */
273 switch (G.telstate) {
275 /* Prev char was CR. If cur one is NUL, ignore it.
276 * See RFC 1123 section 3.3.1 for discussion of telnet EOL handling.
278 G.telstate = TS_COPY;
281 /* else: fall through - need to handle CR IAC ... properly */
283 case TS_COPY: /* Prev char was ordinary */
284 /* Similar to NORMAL, but in TS_COPY we need to copy bytes */
293 case TS_IAC: /* Prev char was IAC */
294 if (c == IAC) { /* IAC IAC -> one IAC */
296 G.telstate = TS_COPY;
302 G.telstate = TS_SUB1;
311 /* DATA MARK must be added later */
313 G.telstate = TS_COPY;
317 case TS_OPT: /* Prev chars were IAC WILL/WONT/DO/DONT */
319 G.telstate = TS_COPY;
322 case TS_SUB1: /* Subnegotiation */
323 case TS_SUB2: /* Subnegotiation */
324 subneg(c); /* can change G.telstate */
329 if (G.telstate != TS_NORMAL) {
330 /* We had some IACs, or CR */
333 if (G.telstate == TS_COPY) /* we aren't in the middle of IAC */
334 G.telstate = TS_NORMAL;
339 full_write(STDOUT_FILENO, G.buf, len);
342 static void put_iac(int c)
344 G.iacbuf[G.iaclen++] = c;
347 static void put_iac2_merged(unsigned wwdd_and_c)
349 if (G.iaclen + 3 > IACBUFSIZE)
353 put_iac(wwdd_and_c >> 8);
354 put_iac(wwdd_and_c & 0xff);
356 #define put_iac2(wwdd,c) put_iac2_merged(((wwdd)<<8) + (c))
358 #if ENABLE_FEATURE_TELNET_TTYPE
359 static void put_iac_subopt(byte c, char *str)
361 int len = strlen(str) + 6; // ( 2 + 1 + 1 + strlen + 2 )
363 if (G.iaclen + len > IACBUFSIZE)
379 #if ENABLE_FEATURE_TELNET_AUTOLOGIN
380 static void put_iac_subopt_autologin(void)
382 int len = strlen(G.autologin) + 6; // (2 + 1 + 1 + strlen + 2)
383 const char *p = "USER";
385 if (G.iaclen + len > IACBUFSIZE)
390 put_iac(TELOPT_NEW_ENVIRON);
392 put_iac(NEW_ENV_VAR);
397 put_iac(NEW_ENV_VALUE);
408 #if ENABLE_FEATURE_TELNET_WIDTH
409 static void put_iac_naws(byte c, int x, int y)
411 if (G.iaclen + 9 > IACBUFSIZE)
418 /* "... & 0xff" implicitly done below */
429 static void setConMode(void)
431 if (G.telflags & UF_ECHO) {
432 if (G.charmode == CHM_TRY) {
434 printf("\r\nEntering %s mode"
435 "\r\nEscape character is '^%c'.\r\n", "character", ']');
439 if (G.charmode != CHM_OFF) {
440 G.charmode = CHM_OFF;
441 printf("\r\nEntering %s mode"
442 "\r\nEscape character is '^%c'.\r\n", "line", 'C');
448 static void will_charmode(void)
450 G.charmode = CHM_TRY;
451 G.telflags |= (UF_ECHO | UF_SGA);
454 put_iac2(DO, TELOPT_ECHO);
455 put_iac2(DO, TELOPT_SGA);
459 static void do_linemode(void)
461 G.charmode = CHM_TRY;
462 G.telflags &= ~(UF_ECHO | UF_SGA);
465 put_iac2(DONT, TELOPT_ECHO);
466 put_iac2(DONT, TELOPT_SGA);
470 static void to_notsup(char c)
472 if (G.telwish == WILL)
474 else if (G.telwish == DO)
478 static void to_echo(void)
480 /* if server requests ECHO, don't agree */
481 if (G.telwish == DO) {
482 put_iac2(WONT, TELOPT_ECHO);
485 if (G.telwish == DONT)
488 if (G.telflags & UF_ECHO) {
489 if (G.telwish == WILL)
491 } else if (G.telwish == WONT)
494 if (G.charmode != CHM_OFF)
495 G.telflags ^= UF_ECHO;
497 if (G.telflags & UF_ECHO)
498 put_iac2(DO, TELOPT_ECHO);
500 put_iac2(DONT, TELOPT_ECHO);
503 full_write1_str("\r\n"); /* sudden modec */
506 static void to_sga(void)
508 /* daemon always sends will/wont, client do/dont */
510 if (G.telflags & UF_SGA) {
511 if (G.telwish == WILL)
513 } else if (G.telwish == WONT)
516 G.telflags ^= UF_SGA; /* toggle */
517 if (G.telflags & UF_SGA)
518 put_iac2(DO, TELOPT_SGA);
520 put_iac2(DONT, TELOPT_SGA);
523 #if ENABLE_FEATURE_TELNET_TTYPE
524 static void to_ttype(void)
526 /* Tell server we will (or won't) do TTYPE */
528 put_iac2(WILL, TELOPT_TTYPE);
530 put_iac2(WONT, TELOPT_TTYPE);
534 #if ENABLE_FEATURE_TELNET_AUTOLOGIN
535 static void to_new_environ(void)
537 /* Tell server we will (or will not) do AUTOLOGIN */
539 put_iac2(WILL, TELOPT_NEW_ENVIRON);
541 put_iac2(WONT, TELOPT_NEW_ENVIRON);
545 #if ENABLE_FEATURE_TELNET_WIDTH
546 static void to_naws(void)
548 /* Tell server we will do NAWS */
549 put_iac2(WILL, TELOPT_NAWS);
553 static void telopt(byte c)
560 #if ENABLE_FEATURE_TELNET_TTYPE
564 #if ENABLE_FEATURE_TELNET_AUTOLOGIN
565 case TELOPT_NEW_ENVIRON:
566 to_new_environ(); break;
568 #if ENABLE_FEATURE_TELNET_WIDTH
571 put_iac_naws(c, G.win_width, G.win_height);
580 /* subnegotiation -- ignore all (except TTYPE,NAWS) */
581 static void subneg(byte c)
583 switch (G.telstate) {
586 G.telstate = TS_SUB2;
587 #if ENABLE_FEATURE_TELNET_TTYPE
589 if (c == TELOPT_TTYPE && G.ttype)
590 put_iac_subopt(TELOPT_TTYPE, G.ttype);
592 #if ENABLE_FEATURE_TELNET_AUTOLOGIN
594 if (c == TELOPT_NEW_ENVIRON && G.autologin)
595 put_iac_subopt_autologin();
600 G.telstate = TS_COPY;
603 G.telstate = TS_SUB1;
608 static void rawmode(void)
611 tcsetattr(0, TCSADRAIN, &G.termios_raw);
614 static void cookmode(void)
617 tcsetattr(0, TCSADRAIN, &G.termios_def);
620 int telnet_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
621 int telnet_main(int argc UNUSED_PARAM, char **argv)
626 struct pollfd ufds[2];
630 #if ENABLE_FEATURE_TELNET_WIDTH
631 get_terminal_width_height(0, &G.win_width, &G.win_height);
634 #if ENABLE_FEATURE_TELNET_TTYPE
635 G.ttype = getenv("TERM");
638 if (tcgetattr(0, &G.termios_def) >= 0) {
640 G.termios_raw = G.termios_def;
641 cfmakeraw(&G.termios_raw);
644 #if ENABLE_FEATURE_TELNET_AUTOLOGIN
645 if (1 == getopt32(argv, "al:", &G.autologin)) {
646 /* Only -a without -l USER picks $USER from envvar */
647 G.autologin = getenv("USER");
656 port = bb_lookup_port(*argv ? *argv++ : "telnet", "tcp", 23);
657 if (*argv) /* extra params?? */
660 xmove_fd(create_and_connect_stream_or_die(host, port), netfd);
662 setsockopt_keepalive(netfd);
664 signal(SIGINT, record_signo);
666 ufds[0].fd = STDIN_FILENO;
667 ufds[0].events = POLLIN;
669 ufds[1].events = POLLIN;
672 if (poll(ufds, 2, -1) < 0) {
673 /* error, ignore and/or log something, bay go to loop */
681 // FIXME: reads can block. Need full bidirectional buffering.
683 if (ufds[0].revents) {
684 len = safe_read(STDIN_FILENO, G.buf, DATABUFSIZE);
686 doexit(EXIT_SUCCESS);
687 TRACE(0, ("Read con: %d\n", len));
688 handle_net_output(len);
691 if (ufds[1].revents) {
692 len = safe_read(netfd, G.buf, DATABUFSIZE);
694 full_write1_str("Connection closed by foreign host\r\n");
695 doexit(EXIT_FAILURE);
697 TRACE(0, ("Read netfd (%d): %d\n", netfd, len));
698 handle_net_input(len);