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 the GPL v2 or later, see the file LICENSE in this tarball.
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>
25 #include <arpa/telnet.h>
26 #include <netinet/in.h>
30 #define TRACE(x, y) do { if (x) printf y; } while (0)
53 typedef unsigned char byte;
58 int iaclen; /* could even use byte, but it's a loss on x86 */
59 byte telstate; /* telnet negotiation state from network input */
60 byte telwish; /* DO, DONT, WILL, WONT */
64 #if ENABLE_FEATURE_TELNET_TTYPE
67 #if ENABLE_FEATURE_TELNET_AUTOLOGIN
68 const char *autologin;
70 #if ENABLE_FEATURE_AUTOWIDTH
71 unsigned win_width, win_height;
73 /* same buffer used both for network and console read/write */
74 char buf[DATABUFSIZE];
75 /* buffer to handle telnet negotiations */
76 char iacbuf[IACBUFSIZE];
77 struct termios termios_def;
78 struct termios termios_raw;
80 #define G (*(struct globals*)&bb_common_bufsiz1)
81 void BUG_telnet_globals_too_big(void);
82 #define INIT_G() do { \
83 if (sizeof(G) > COMMON_BUFSIZE) \
84 BUG_telnet_globals_too_big(); \
85 /* memset(&G, 0, sizeof G); - already is */ \
88 /* Function prototypes */
89 static void rawmode(void);
90 static void cookmode(void);
91 static void do_linemode(void);
92 static void will_charmode(void);
93 static void telopt(byte c);
94 static int subneg(byte c);
96 static void iac_flush(void)
98 write(netfd, G.iacbuf, G.iaclen);
102 #define write_str(fd, str) write(fd, str, sizeof(str) - 1)
104 static void doexit(int ev) NORETURN;
105 static void doexit(int ev)
111 static void con_escape(void)
115 if (bb_got_signal) /* came from line mode... go raw */
118 write_str(1, "\r\nConsole escape. Commands are:\r\n\n"
119 " l go to line mode\r\n"
120 " c go to character mode\r\n"
121 " z suspend telnet\r\n"
122 " e exit telnet\r\n");
124 if (read(STDIN_FILENO, &b, 1) <= 0)
125 doexit(EXIT_FAILURE);
129 if (!bb_got_signal) {
146 doexit(EXIT_SUCCESS);
149 write_str(1, "continuing...\r\n");
158 static void handle_net_output(int len)
160 /* here we could do smart tricks how to handle 0xFF:s in output
161 * stream like writing twice every sequence of FF:s (thus doing
162 * many write()s. But I think interactive telnet application does
163 * not need to be 100% 8-bit clean, so changing every 0xff:s to
166 * 2002-mar-21, Przemyslaw Czerpak (druzus@polbox.com)
168 * first - I cannot use programs like sz/rz
169 * second - the 0x0D is sent as one character and if the next
170 * char is 0x0A then it's eaten by a server side.
171 * third - why do you have to make 'many write()s'?
172 * I don't understand.
173 * So I implemented it. It's really useful for me. I hope that
174 * other people will find it interesting too.
178 byte *p = (byte*)G.buf;
179 byte outbuf[4*DATABUFSIZE];
181 for (i = len, j = 0; i > 0; i--, p++) {
193 write(netfd, outbuf, j);
196 static void handle_net_input(int len)
201 for (i = 0; i < len; i++) {
204 if (G.telstate == 0) { /* most of the time state == 0 */
211 switch (G.telstate) {
220 if (c == IAC) { /* IAC IAC -> 0xFF */
228 G.telstate = TS_SUB1;
238 G.telstate = TS_0; /* DATA MARK must be added later */
241 case TS_OPT: /* WILL, WONT, DO, DONT */
245 case TS_SUB1: /* Subnegotiation */
246 case TS_SUB2: /* Subnegotiation */
255 if (G.telstate == TS_0)
261 write(STDOUT_FILENO, G.buf, len);
264 static void put_iac(int c)
266 G.iacbuf[G.iaclen++] = c;
269 static void put_iac2(byte wwdd, byte c)
271 if (G.iaclen + 3 > IACBUFSIZE)
279 #if ENABLE_FEATURE_TELNET_TTYPE
280 static void put_iac_subopt(byte c, char *str)
282 int len = strlen(str) + 6; // ( 2 + 1 + 1 + strlen + 2 )
284 if (G.iaclen + len > IACBUFSIZE)
300 #if ENABLE_FEATURE_TELNET_AUTOLOGIN
301 static void put_iac_subopt_autologin(void)
303 int len = strlen(G.autologin) + 6; // (2 + 1 + 1 + strlen + 2)
304 const char *user = "USER";
306 if (G.iaclen + len > IACBUFSIZE)
311 put_iac(TELOPT_NEW_ENVIRON);
313 put_iac(NEW_ENV_VAR);
318 put_iac(NEW_ENV_VALUE);
321 put_iac(*G.autologin++);
328 #if ENABLE_FEATURE_AUTOWIDTH
329 static void put_iac_naws(byte c, int x, int y)
331 if (G.iaclen + 9 > IACBUFSIZE)
338 put_iac((x >> 8) & 0xff);
340 put_iac((y >> 8) & 0xff);
348 static char const escapecharis[] ALIGN1 = "\r\nEscape character is ";
350 static void setConMode(void)
352 if (G.telflags & UF_ECHO) {
353 if (G.charmode == CHM_TRY) {
355 printf("\r\nEntering character mode%s'^]'.\r\n", escapecharis);
359 if (G.charmode != CHM_OFF) {
360 G.charmode = CHM_OFF;
361 printf("\r\nEntering line mode%s'^C'.\r\n", escapecharis);
367 static void will_charmode(void)
369 G.charmode = CHM_TRY;
370 G.telflags |= (UF_ECHO | UF_SGA);
373 put_iac2(DO, TELOPT_ECHO);
374 put_iac2(DO, TELOPT_SGA);
378 static void do_linemode(void)
380 G.charmode = CHM_TRY;
381 G.telflags &= ~(UF_ECHO | UF_SGA);
384 put_iac2(DONT, TELOPT_ECHO);
385 put_iac2(DONT, TELOPT_SGA);
389 static void to_notsup(char c)
391 if (G.telwish == WILL)
393 else if (G.telwish == DO)
397 static void to_echo(void)
399 /* if server requests ECHO, don't agree */
400 if (G.telwish == DO) {
401 put_iac2(WONT, TELOPT_ECHO);
404 if (G.telwish == DONT)
407 if (G.telflags & UF_ECHO) {
408 if (G.telwish == WILL)
410 } else if (G.telwish == WONT)
413 if (G.charmode != CHM_OFF)
414 G.telflags ^= UF_ECHO;
416 if (G.telflags & UF_ECHO)
417 put_iac2(DO, TELOPT_ECHO);
419 put_iac2(DONT, TELOPT_ECHO);
422 write_str(1, "\r\n"); /* sudden modec */
425 static void to_sga(void)
427 /* daemon always sends will/wont, client do/dont */
429 if (G.telflags & UF_SGA) {
430 if (G.telwish == WILL)
432 } else if (G.telwish == WONT)
435 G.telflags ^= UF_SGA; /* toggle */
436 if (G.telflags & UF_SGA)
437 put_iac2(DO, TELOPT_SGA);
439 put_iac2(DONT, TELOPT_SGA);
442 #if ENABLE_FEATURE_TELNET_TTYPE
443 static void to_ttype(void)
445 /* Tell server we will (or won't) do TTYPE */
448 put_iac2(WILL, TELOPT_TTYPE);
450 put_iac2(WONT, TELOPT_TTYPE);
454 #if ENABLE_FEATURE_TELNET_AUTOLOGIN
455 static void to_new_environ(void)
457 /* Tell server we will (or will not) do AUTOLOGIN */
460 put_iac2(WILL, TELOPT_NEW_ENVIRON);
462 put_iac2(WONT, TELOPT_NEW_ENVIRON);
466 #if ENABLE_FEATURE_AUTOWIDTH
467 static void to_naws(void)
469 /* Tell server we will do NAWS */
470 put_iac2(WILL, TELOPT_NAWS);
474 static void telopt(byte c)
481 #if ENABLE_FEATURE_TELNET_TTYPE
485 #if ENABLE_FEATURE_TELNET_AUTOLOGIN
486 case TELOPT_NEW_ENVIRON:
487 to_new_environ(); break;
489 #if ENABLE_FEATURE_AUTOWIDTH
492 put_iac_naws(c, G.win_width, G.win_height);
501 /* subnegotiation -- ignore all (except TTYPE,NAWS) */
502 static int subneg(byte c)
504 switch (G.telstate) {
507 G.telstate = TS_SUB2;
508 #if ENABLE_FEATURE_TELNET_TTYPE
510 if (c == TELOPT_TTYPE)
511 put_iac_subopt(TELOPT_TTYPE, G.ttype);
513 #if ENABLE_FEATURE_TELNET_AUTOLOGIN
515 if (c == TELOPT_NEW_ENVIRON)
516 put_iac_subopt_autologin();
522 G.telstate = TS_SUB1;
528 static void rawmode(void)
531 tcsetattr(0, TCSADRAIN, &G.termios_raw);
534 static void cookmode(void)
537 tcsetattr(0, TCSADRAIN, &G.termios_def);
540 /* poll gives smaller (-70 bytes) code */
543 int telnet_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
544 int telnet_main(int argc UNUSED_PARAM, char **argv)
550 struct pollfd ufds[2];
558 #if ENABLE_FEATURE_AUTOWIDTH
559 get_terminal_width_height(0, &G.win_width, &G.win_height);
562 #if ENABLE_FEATURE_TELNET_TTYPE
563 G.ttype = getenv("TERM");
566 if (tcgetattr(0, &G.termios_def) >= 0) {
568 G.termios_raw = G.termios_def;
569 cfmakeraw(&G.termios_raw);
572 #if ENABLE_FEATURE_TELNET_AUTOLOGIN
573 if (1 & getopt32(argv, "al:", &G.autologin))
574 G.autologin = getenv("USER");
582 port = bb_lookup_port(*argv ? *argv++ : "telnet", "tcp", 23);
583 if (*argv) /* extra params?? */
586 xmove_fd(create_and_connect_stream_or_die(host, port), netfd);
588 setsockopt(netfd, SOL_SOCKET, SO_KEEPALIVE, &const_int_1, sizeof(const_int_1));
590 signal(SIGINT, record_signo);
593 ufds[0].fd = 0; ufds[1].fd = netfd;
594 ufds[0].events = ufds[1].events = POLLIN;
597 FD_SET(STDIN_FILENO, &readfds);
598 FD_SET(netfd, &readfds);
604 fd_set rfds = readfds;
606 switch (select(maxfd, &rfds, NULL, NULL, NULL))
608 switch (poll(ufds, 2, -1))
614 /* error, ignore and/or log something, bay go to loop */
623 if (ufds[0].revents) /* well, should check POLLIN, but ... */
625 if (FD_ISSET(STDIN_FILENO, &rfds))
628 len = read(STDIN_FILENO, G.buf, DATABUFSIZE);
630 doexit(EXIT_SUCCESS);
631 TRACE(0, ("Read con: %d\n", len));
632 handle_net_output(len);
636 if (ufds[1].revents) /* well, should check POLLIN, but ... */
638 if (FD_ISSET(netfd, &rfds))
641 len = read(netfd, G.buf, DATABUFSIZE);
643 write_str(1, "Connection closed by foreign host\r\n");
644 doexit(EXIT_FAILURE);
646 TRACE(0, ("Read netfd (%d): %d\n", netfd, len));
647 handle_net_input(len);