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.8 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)
156 full_write(netfd, G.iacbuf, G.iaclen);
161 static void doexit(int ev) NORETURN;
162 static void doexit(int ev)
168 static void con_escape(void)
172 if (bb_got_signal) /* came from line mode... go raw */
175 full_write1_str("\r\nConsole escape. Commands are:\r\n\n"
176 " l go to line mode\r\n"
177 " c go to character mode\r\n"
178 " z suspend telnet\r\n"
179 " e exit telnet\r\n");
181 if (read(STDIN_FILENO, &b, 1) <= 0)
182 doexit(EXIT_FAILURE);
186 if (!bb_got_signal) {
203 doexit(EXIT_SUCCESS);
206 full_write1_str("continuing...\r\n");
214 static void handle_net_output(int len)
216 byte outbuf[2 * DATABUFSIZE];
218 byte *src = (byte*)G.buf;
219 byte *end = src + len;
229 *++dst = c; /* IAC -> IAC IAC */
231 if (c == '\r' || c == '\n') {
232 /* Enter key sends '\r' in raw mode and '\n' in cooked one.
234 * See RFC 1123 3.3.1 Telnet End-of-Line Convention.
235 * Using CR LF instead of other allowed possibilities
236 * like CR NUL - easier to talk to HTTP/SMTP servers.
238 *dst = '\r'; /* Enter -> CR LF */
242 /* putty's "special commands" mode does this: */
243 /* Korenix 3005 switch needs at least the backspace tweak */
244 if (c == 0x08 || c == 0x7f) { /* ctrl+h || backspace */
248 if (c == 0x03) { /* ctrl+c */
255 if (dst - outbuf != 0)
256 full_write(netfd, outbuf, dst - outbuf);
259 static void handle_net_input(int len)
266 //bb_error_msg("[%u,'%.*s']", G.telstate, len, G.buf);
267 if (G.telstate == TS_NORMAL) { /* most typical state */
271 if (c == IAC) /* unlikely */
273 if (c != '\r') /* likely */
279 full_write(STDOUT_FILENO, G.buf, len);
287 for (; i < len; i++) {
290 switch (G.telstate) {
292 /* Prev char was CR. If cur one is NUL, ignore it.
293 * See RFC 1123 section 3.3.1 for discussion of telnet EOL handling.
295 G.telstate = TS_COPY;
298 /* else: fall through - need to handle CR IAC ... properly */
300 case TS_COPY: /* Prev char was ordinary */
301 /* Similar to NORMAL, but in TS_COPY we need to copy bytes */
311 case TS_IAC: /* Prev char was IAC */
313 case IAC: /* IAC IAC -> one IAC */
315 G.telstate = TS_COPY;
318 G.telstate = TS_SUB1;
327 /* DATA MARK must be added later */
329 G.telstate = TS_COPY;
333 case TS_OPT: /* Prev chars were IAC WILL/WONT/DO/DONT */
335 G.telstate = TS_COPY;
338 case TS_SUB1: /* Subnegotiation */
339 case TS_SUB2: /* Subnegotiation */
340 subneg(c); /* can change G.telstate */
345 /* We had some IACs, or CR */
347 if (G.telstate == TS_COPY) /* we aren't in the middle of IAC */
348 G.telstate = TS_NORMAL;
350 full_write(STDOUT_FILENO, G.buf, cstart);
353 static void put_iac(int c)
355 int iaclen = G.iaclen;
356 if (iaclen >= IACBUFSIZE) {
360 G.iacbuf[iaclen] = c; /* "... & 0xff" is implicit */
361 G.iaclen = iaclen + 1;
364 static void put_iac2_msb_lsb(unsigned x_y)
366 put_iac(x_y >> 8); /* "... & 0xff" is implicit */
367 put_iac(x_y); /* "... & 0xff" is implicit */
369 #define put_iac2_x_y(x,y) put_iac2_msb_lsb(((x)<<8) + (y))
371 static void put_iac4_msb_lsb(unsigned x_y_z_t)
373 put_iac2_msb_lsb(x_y_z_t >> 16);
374 put_iac2_msb_lsb(x_y_z_t); /* "... & 0xffff" is implicit */
376 #define put_iac4_x_y_z_t(x,y,z,t) put_iac4_msb_lsb(((x)<<24) + ((y)<<16) + ((z)<<8) + (t))
378 static void put_iac3_IAC_x_y_merged(unsigned wwdd_and_c)
381 put_iac2_msb_lsb(wwdd_and_c);
383 #define put_iac3_IAC_x_y(wwdd,c) put_iac3_IAC_x_y_merged(((wwdd)<<8) + (c))
385 #if ENABLE_FEATURE_TELNET_TTYPE
386 static void put_iac_subopt(byte c, char *str)
388 put_iac4_x_y_z_t(IAC, SB, c, 0);
393 put_iac2_x_y(IAC, SE);
397 #if ENABLE_FEATURE_TELNET_AUTOLOGIN
398 static void put_iac_subopt_autologin(void)
402 put_iac4_x_y_z_t(IAC, SB, TELOPT_NEW_ENVIRON, TELQUAL_IS);
403 put_iac4_x_y_z_t(NEW_ENV_VAR, 'U', 'S', 'E'); /* "USER" */
404 put_iac2_x_y('R', NEW_ENV_VALUE);
410 put_iac2_x_y(IAC, SE);
414 #if ENABLE_FEATURE_TELNET_WIDTH
415 static void put_iac_naws(byte c, int x, int y)
417 put_iac3_IAC_x_y(SB, c);
419 put_iac4_msb_lsb((x << 16) + y);
421 put_iac2_x_y(IAC, SE);
425 static void setConMode(void)
427 if (G.telflags & UF_ECHO) {
428 if (G.charmode == CHM_TRY) {
430 printf("\r\nEntering %s mode"
431 "\r\nEscape character is '^%c'.\r\n", "character", ']');
435 if (G.charmode != CHM_OFF) {
436 G.charmode = CHM_OFF;
437 printf("\r\nEntering %s mode"
438 "\r\nEscape character is '^%c'.\r\n", "line", 'C');
444 static void will_charmode(void)
446 G.charmode = CHM_TRY;
447 G.telflags |= (UF_ECHO | UF_SGA);
450 put_iac3_IAC_x_y(DO, TELOPT_ECHO);
451 put_iac3_IAC_x_y(DO, TELOPT_SGA);
455 static void do_linemode(void)
457 G.charmode = CHM_TRY;
458 G.telflags &= ~(UF_ECHO | UF_SGA);
461 put_iac3_IAC_x_y(DONT, TELOPT_ECHO);
462 put_iac3_IAC_x_y(DONT, TELOPT_SGA);
466 static void to_notsup(char c)
468 if (G.telwish == WILL)
469 put_iac3_IAC_x_y(DONT, c);
470 else if (G.telwish == DO)
471 put_iac3_IAC_x_y(WONT, c);
474 static void to_echo(void)
476 /* if server requests ECHO, don't agree */
477 if (G.telwish == DO) {
478 put_iac3_IAC_x_y(WONT, TELOPT_ECHO);
481 if (G.telwish == DONT)
484 if (G.telflags & UF_ECHO) {
485 if (G.telwish == WILL)
487 } else if (G.telwish == WONT)
490 if (G.charmode != CHM_OFF)
491 G.telflags ^= UF_ECHO;
493 if (G.telflags & UF_ECHO)
494 put_iac3_IAC_x_y(DO, TELOPT_ECHO);
496 put_iac3_IAC_x_y(DONT, TELOPT_ECHO);
499 full_write1_str("\r\n"); /* sudden modec */
502 static void to_sga(void)
504 /* daemon always sends will/wont, client do/dont */
506 if (G.telflags & UF_SGA) {
507 if (G.telwish == WILL)
509 } else if (G.telwish == WONT)
512 G.telflags ^= UF_SGA; /* toggle */
513 if (G.telflags & UF_SGA)
514 put_iac3_IAC_x_y(DO, TELOPT_SGA);
516 put_iac3_IAC_x_y(DONT, TELOPT_SGA);
519 #if ENABLE_FEATURE_TELNET_TTYPE
520 static void to_ttype(void)
522 /* Tell server we will (or won't) do TTYPE */
524 put_iac3_IAC_x_y(WILL, TELOPT_TTYPE);
526 put_iac3_IAC_x_y(WONT, TELOPT_TTYPE);
530 #if ENABLE_FEATURE_TELNET_AUTOLOGIN
531 static void to_new_environ(void)
533 /* Tell server we will (or will not) do AUTOLOGIN */
535 put_iac3_IAC_x_y(WILL, TELOPT_NEW_ENVIRON);
537 put_iac3_IAC_x_y(WONT, TELOPT_NEW_ENVIRON);
541 #if ENABLE_FEATURE_TELNET_WIDTH
542 static void to_naws(void)
544 /* Tell server we will do NAWS */
545 put_iac3_IAC_x_y(WILL, TELOPT_NAWS);
549 static void telopt(byte c)
556 #if ENABLE_FEATURE_TELNET_TTYPE
560 #if ENABLE_FEATURE_TELNET_AUTOLOGIN
561 case TELOPT_NEW_ENVIRON:
562 to_new_environ(); break;
564 #if ENABLE_FEATURE_TELNET_WIDTH
567 put_iac_naws(c, G.win_width, G.win_height);
576 /* subnegotiation -- ignore all (except TTYPE,NAWS) */
577 static void subneg(byte c)
579 switch (G.telstate) {
582 G.telstate = TS_SUB2;
583 #if ENABLE_FEATURE_TELNET_TTYPE
585 if (c == TELOPT_TTYPE && G.ttype)
586 put_iac_subopt(TELOPT_TTYPE, G.ttype);
588 #if ENABLE_FEATURE_TELNET_AUTOLOGIN
590 if (c == TELOPT_NEW_ENVIRON && G.autologin)
591 put_iac_subopt_autologin();
596 G.telstate = TS_COPY;
599 G.telstate = TS_SUB1;
604 static void rawmode(void)
607 tcsetattr(0, TCSADRAIN, &G.termios_raw);
610 static void cookmode(void)
613 tcsetattr(0, TCSADRAIN, &G.termios_def);
616 int telnet_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
617 int telnet_main(int argc UNUSED_PARAM, char **argv)
622 struct pollfd ufds[2];
626 #if ENABLE_FEATURE_TELNET_TTYPE
627 G.ttype = getenv("TERM");
630 if (tcgetattr(0, &G.termios_def) >= 0) {
632 G.termios_raw = G.termios_def;
633 cfmakeraw(&G.termios_raw);
636 #if ENABLE_FEATURE_TELNET_AUTOLOGIN
637 if (1 == getopt32(argv, "al:", &G.autologin)) {
638 /* Only -a without -l USER picks $USER from envvar */
639 G.autologin = getenv("USER");
648 port = *argv ? bb_lookup_port(*argv++, "tcp", 23)
649 : bb_lookup_std_port("telnet", "tcp", 23);
650 if (*argv) /* extra params?? */
653 xmove_fd(create_and_connect_stream_or_die(host, port), netfd);
654 printf("Connected to %s\n", host);
656 setsockopt_keepalive(netfd);
658 #if ENABLE_FEATURE_TELNET_WIDTH
659 get_terminal_width_height(0, &G.win_width, &G.win_height);
660 //TODO: support dynamic resize?
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 handle_net_output(len);
689 if (ufds[1].revents) {
690 len = safe_read(netfd, G.buf, DATABUFSIZE);
692 full_write1_str("Connection closed by foreign host\r\n");
693 doexit(EXIT_FAILURE);
695 handle_net_input(len);