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>
24 //usage:#if ENABLE_FEATURE_TELNET_AUTOLOGIN
25 //usage:#define telnet_trivial_usage
26 //usage: "[-a] [-l USER] HOST [PORT]"
27 //usage:#define telnet_full_usage "\n\n"
28 //usage: "Connect to telnet server\n"
29 //usage: "\n -a Automatic login with $USER variable"
30 //usage: "\n -l USER Automatic login as USER"
33 //usage:#define telnet_trivial_usage
34 //usage: "HOST [PORT]"
35 //usage:#define telnet_full_usage "\n\n"
36 //usage: "Connect to telnet server"
39 #include <arpa/telnet.h>
40 #include <netinet/in.h>
44 #define TRACE(x, y) do { if (x) printf y; } while (0)
69 typedef unsigned char byte;
74 int iaclen; /* could even use byte, but it's a loss on x86 */
75 byte telstate; /* telnet negotiation state from network input */
76 byte telwish; /* DO, DONT, WILL, WONT */
80 #if ENABLE_FEATURE_TELNET_TTYPE
83 #if ENABLE_FEATURE_TELNET_AUTOLOGIN
84 const char *autologin;
86 #if ENABLE_FEATURE_AUTOWIDTH
87 unsigned win_width, win_height;
89 /* same buffer used both for network and console read/write */
90 char buf[DATABUFSIZE];
91 /* buffer to handle telnet negotiations */
92 char iacbuf[IACBUFSIZE];
93 struct termios termios_def;
94 struct termios termios_raw;
96 #define G (*(struct globals*)&bb_common_bufsiz1)
97 #define INIT_G() do { \
98 struct G_sizecheck { \
99 char G_sizecheck[sizeof(G) > COMMON_BUFSIZE ? -1 : 1]; \
104 static void rawmode(void);
105 static void cookmode(void);
106 static void do_linemode(void);
107 static void will_charmode(void);
108 static void telopt(byte c);
109 static void subneg(byte c);
111 static void iac_flush(void)
113 write(netfd, G.iacbuf, G.iaclen);
117 #define write_str(fd, str) write(fd, str, sizeof(str) - 1)
119 static void doexit(int ev) NORETURN;
120 static void doexit(int ev)
126 static void con_escape(void)
130 if (bb_got_signal) /* came from line mode... go raw */
133 write_str(1, "\r\nConsole escape. Commands are:\r\n\n"
134 " l go to line mode\r\n"
135 " c go to character mode\r\n"
136 " z suspend telnet\r\n"
137 " e exit telnet\r\n");
139 if (read(STDIN_FILENO, &b, 1) <= 0)
140 doexit(EXIT_FAILURE);
144 if (!bb_got_signal) {
161 doexit(EXIT_SUCCESS);
164 write_str(1, "continuing...\r\n");
172 static void handle_net_output(int len)
174 /* here we could do smart tricks how to handle 0xFF:s in output
175 * stream like writing twice every sequence of FF:s (thus doing
176 * many write()s. But I think interactive telnet application does
177 * not need to be 100% 8-bit clean, so changing every 0xff:s to
180 * 2002-mar-21, Przemyslaw Czerpak (druzus@polbox.com)
182 * first - I cannot use programs like sz/rz
183 * second - the 0x0D is sent as one character and if the next
184 * char is 0x0A then it's eaten by a server side.
185 * third - why do you have to make 'many write()s'?
186 * I don't understand.
187 * So I implemented it. It's really useful for me. I hope that
188 * other people will find it interesting too.
190 byte outbuf[2 * DATABUFSIZE];
191 byte *p = (byte*)G.buf;
194 for (; len > 0; len--, p++) {
202 outbuf[j++] = c; /* IAC -> IAC IAC */
204 outbuf[j++] = '\0'; /* CR -> CR NUL */
207 full_write(netfd, outbuf, j);
210 static void handle_net_input(int len)
215 for (i = 0; i < len; i++) {
218 if (G.telstate == TS_NORMAL) { /* most typical state */
223 else if (c == '\r') {
227 /* No IACs were seen so far, no need to copy
228 * bytes within G.buf: */
232 switch (G.telstate) {
234 /* Prev char was CR. If cur one is NUL, ignore it.
235 * See RFC 1123 section 3.3.1 for discussion of telnet EOL handling.
237 G.telstate = TS_COPY;
240 /* else: fall through - need to handle CR IAC ... properly */
242 case TS_COPY: /* Prev char was ordinary */
243 /* Similar to NORMAL, but in TS_COPY we need to copy bytes */
252 case TS_IAC: /* Prev char was IAC */
253 if (c == IAC) { /* IAC IAC -> one IAC */
255 G.telstate = TS_COPY;
261 G.telstate = TS_SUB1;
270 /* DATA MARK must be added later */
272 G.telstate = TS_COPY;
276 case TS_OPT: /* Prev chars were IAC WILL/WONT/DO/DONT */
278 G.telstate = TS_COPY;
281 case TS_SUB1: /* Subnegotiation */
282 case TS_SUB2: /* Subnegotiation */
283 subneg(c); /* can change G.telstate */
288 if (G.telstate != TS_NORMAL) {
289 /* We had some IACs, or CR */
292 if (G.telstate == TS_COPY) /* we aren't in the middle of IAC */
293 G.telstate = TS_NORMAL;
298 full_write(STDOUT_FILENO, G.buf, len);
301 static void put_iac(int c)
303 G.iacbuf[G.iaclen++] = c;
306 static void put_iac2(byte wwdd, byte c)
308 if (G.iaclen + 3 > IACBUFSIZE)
316 #if ENABLE_FEATURE_TELNET_TTYPE
317 static void put_iac_subopt(byte c, char *str)
319 int len = strlen(str) + 6; // ( 2 + 1 + 1 + strlen + 2 )
321 if (G.iaclen + len > IACBUFSIZE)
337 #if ENABLE_FEATURE_TELNET_AUTOLOGIN
338 static void put_iac_subopt_autologin(void)
340 int len = strlen(G.autologin) + 6; // (2 + 1 + 1 + strlen + 2)
341 const char *p = "USER";
343 if (G.iaclen + len > IACBUFSIZE)
348 put_iac(TELOPT_NEW_ENVIRON);
350 put_iac(NEW_ENV_VAR);
355 put_iac(NEW_ENV_VALUE);
366 #if ENABLE_FEATURE_AUTOWIDTH
367 static void put_iac_naws(byte c, int x, int y)
369 if (G.iaclen + 9 > IACBUFSIZE)
376 put_iac((x >> 8) & 0xff);
378 put_iac((y >> 8) & 0xff);
386 static char const escapecharis[] ALIGN1 = "\r\nEscape character is ";
388 static void setConMode(void)
390 if (G.telflags & UF_ECHO) {
391 if (G.charmode == CHM_TRY) {
393 printf("\r\nEntering character mode%s'^]'.\r\n", escapecharis);
397 if (G.charmode != CHM_OFF) {
398 G.charmode = CHM_OFF;
399 printf("\r\nEntering line mode%s'^C'.\r\n", escapecharis);
405 static void will_charmode(void)
407 G.charmode = CHM_TRY;
408 G.telflags |= (UF_ECHO | UF_SGA);
411 put_iac2(DO, TELOPT_ECHO);
412 put_iac2(DO, TELOPT_SGA);
416 static void do_linemode(void)
418 G.charmode = CHM_TRY;
419 G.telflags &= ~(UF_ECHO | UF_SGA);
422 put_iac2(DONT, TELOPT_ECHO);
423 put_iac2(DONT, TELOPT_SGA);
427 static void to_notsup(char c)
429 if (G.telwish == WILL)
431 else if (G.telwish == DO)
435 static void to_echo(void)
437 /* if server requests ECHO, don't agree */
438 if (G.telwish == DO) {
439 put_iac2(WONT, TELOPT_ECHO);
442 if (G.telwish == DONT)
445 if (G.telflags & UF_ECHO) {
446 if (G.telwish == WILL)
448 } else if (G.telwish == WONT)
451 if (G.charmode != CHM_OFF)
452 G.telflags ^= UF_ECHO;
454 if (G.telflags & UF_ECHO)
455 put_iac2(DO, TELOPT_ECHO);
457 put_iac2(DONT, TELOPT_ECHO);
460 full_write1_str("\r\n"); /* sudden modec */
463 static void to_sga(void)
465 /* daemon always sends will/wont, client do/dont */
467 if (G.telflags & UF_SGA) {
468 if (G.telwish == WILL)
470 } else if (G.telwish == WONT)
473 G.telflags ^= UF_SGA; /* toggle */
474 if (G.telflags & UF_SGA)
475 put_iac2(DO, TELOPT_SGA);
477 put_iac2(DONT, TELOPT_SGA);
480 #if ENABLE_FEATURE_TELNET_TTYPE
481 static void to_ttype(void)
483 /* Tell server we will (or won't) do TTYPE */
485 put_iac2(WILL, TELOPT_TTYPE);
487 put_iac2(WONT, TELOPT_TTYPE);
491 #if ENABLE_FEATURE_TELNET_AUTOLOGIN
492 static void to_new_environ(void)
494 /* Tell server we will (or will not) do AUTOLOGIN */
496 put_iac2(WILL, TELOPT_NEW_ENVIRON);
498 put_iac2(WONT, TELOPT_NEW_ENVIRON);
502 #if ENABLE_FEATURE_AUTOWIDTH
503 static void to_naws(void)
505 /* Tell server we will do NAWS */
506 put_iac2(WILL, TELOPT_NAWS);
510 static void telopt(byte c)
517 #if ENABLE_FEATURE_TELNET_TTYPE
521 #if ENABLE_FEATURE_TELNET_AUTOLOGIN
522 case TELOPT_NEW_ENVIRON:
523 to_new_environ(); break;
525 #if ENABLE_FEATURE_AUTOWIDTH
528 put_iac_naws(c, G.win_width, G.win_height);
537 /* subnegotiation -- ignore all (except TTYPE,NAWS) */
538 static void subneg(byte c)
540 switch (G.telstate) {
543 G.telstate = TS_SUB2;
544 #if ENABLE_FEATURE_TELNET_TTYPE
546 if (c == TELOPT_TTYPE && G.ttype)
547 put_iac_subopt(TELOPT_TTYPE, G.ttype);
549 #if ENABLE_FEATURE_TELNET_AUTOLOGIN
551 if (c == TELOPT_NEW_ENVIRON && G.autologin)
552 put_iac_subopt_autologin();
557 G.telstate = TS_COPY;
560 G.telstate = TS_SUB1;
565 static void rawmode(void)
568 tcsetattr(0, TCSADRAIN, &G.termios_raw);
571 static void cookmode(void)
574 tcsetattr(0, TCSADRAIN, &G.termios_def);
577 int telnet_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
578 int telnet_main(int argc UNUSED_PARAM, char **argv)
583 struct pollfd ufds[2];
587 #if ENABLE_FEATURE_AUTOWIDTH
588 get_terminal_width_height(0, &G.win_width, &G.win_height);
591 #if ENABLE_FEATURE_TELNET_TTYPE
592 G.ttype = getenv("TERM");
595 if (tcgetattr(0, &G.termios_def) >= 0) {
597 G.termios_raw = G.termios_def;
598 cfmakeraw(&G.termios_raw);
601 #if ENABLE_FEATURE_TELNET_AUTOLOGIN
602 if (1 & getopt32(argv, "al:", &G.autologin))
603 G.autologin = getenv("USER");
611 port = bb_lookup_port(*argv ? *argv++ : "telnet", "tcp", 23);
612 if (*argv) /* extra params?? */
615 xmove_fd(create_and_connect_stream_or_die(host, port), netfd);
617 setsockopt(netfd, SOL_SOCKET, SO_KEEPALIVE, &const_int_1, sizeof(const_int_1));
619 signal(SIGINT, record_signo);
621 ufds[0].fd = STDIN_FILENO;
622 ufds[0].events = POLLIN;
624 ufds[1].events = POLLIN;
627 if (poll(ufds, 2, -1) < 0) {
628 /* error, ignore and/or log something, bay go to loop */
636 // FIXME: reads can block. Need full bidirectional buffering.
638 if (ufds[0].revents) {
639 len = safe_read(STDIN_FILENO, G.buf, DATABUFSIZE);
641 doexit(EXIT_SUCCESS);
642 TRACE(0, ("Read con: %d\n", len));
643 handle_net_output(len);
646 if (ufds[1].revents) {
647 len = safe_read(netfd, G.buf, DATABUFSIZE);
649 full_write1_str("Connection closed by foreign host\r\n");
650 doexit(EXIT_FAILURE);
652 TRACE(0, ("Read netfd (%d): %d\n", netfd, len));
653 handle_net_input(len);