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>
24 #include <arpa/telnet.h>
25 #include <netinet/in.h>
29 #define TRACE(x, y) do { if (x) printf y; } while (0)
52 typedef unsigned char byte;
57 int iaclen; /* could even use byte, but it's a loss on x86 */
58 byte telstate; /* telnet negotiation state from network input */
59 byte telwish; /* DO, DONT, WILL, WONT */
63 #if ENABLE_FEATURE_TELNET_TTYPE
66 #if ENABLE_FEATURE_TELNET_AUTOLOGIN
67 const char *autologin;
69 #if ENABLE_FEATURE_AUTOWIDTH
70 unsigned win_width, win_height;
72 /* same buffer used both for network and console read/write */
73 char buf[DATABUFSIZE];
74 /* buffer to handle telnet negotiations */
75 char iacbuf[IACBUFSIZE];
76 struct termios termios_def;
77 struct termios termios_raw;
79 #define G (*(struct globals*)&bb_common_bufsiz1)
80 void BUG_telnet_globals_too_big(void);
81 #define INIT_G() do { \
82 if (sizeof(G) > COMMON_BUFSIZE) \
83 BUG_telnet_globals_too_big(); \
84 /* memset(&G, 0, sizeof G); - already is */ \
87 /* Function prototypes */
88 static void rawmode(void);
89 static void cookmode(void);
90 static void do_linemode(void);
91 static void will_charmode(void);
92 static void telopt(byte c);
93 static int subneg(byte c);
95 static void iac_flush(void)
97 write(netfd, G.iacbuf, G.iaclen);
101 #define write_str(fd, str) write(fd, str, sizeof(str) - 1)
103 static void doexit(int ev) NORETURN;
104 static void doexit(int ev)
110 static void con_escape(void)
114 if (bb_got_signal) /* came from line mode... go raw */
117 write_str(1, "\r\nConsole escape. Commands are:\r\n\n"
118 " l go to line mode\r\n"
119 " c go to character mode\r\n"
120 " z suspend telnet\r\n"
121 " e exit telnet\r\n");
123 if (read(STDIN_FILENO, &b, 1) <= 0)
124 doexit(EXIT_FAILURE);
128 if (!bb_got_signal) {
145 doexit(EXIT_SUCCESS);
148 write_str(1, "continuing...\r\n");
157 static void handle_net_output(int len)
159 /* here we could do smart tricks how to handle 0xFF:s in output
160 * stream like writing twice every sequence of FF:s (thus doing
161 * many write()s. But I think interactive telnet application does
162 * not need to be 100% 8-bit clean, so changing every 0xff:s to
165 * 2002-mar-21, Przemyslaw Czerpak (druzus@polbox.com)
167 * first - I cannot use programs like sz/rz
168 * second - the 0x0D is sent as one character and if the next
169 * char is 0x0A then it's eaten by a server side.
170 * third - why do you have to make 'many write()s'?
171 * I don't understand.
172 * So I implemented it. It's really useful for me. I hope that
173 * other people will find it interesting too.
177 byte *p = (byte*)G.buf;
178 byte outbuf[4*DATABUFSIZE];
180 for (i = len, j = 0; i > 0; i--, p++) {
192 write(netfd, outbuf, j);
195 static void handle_net_input(int len)
200 for (i = 0; i < len; i++) {
203 if (G.telstate == 0) { /* most of the time state == 0 */
210 switch (G.telstate) {
219 if (c == IAC) { /* IAC IAC -> 0xFF */
227 G.telstate = TS_SUB1;
237 G.telstate = TS_0; /* DATA MARK must be added later */
240 case TS_OPT: /* WILL, WONT, DO, DONT */
244 case TS_SUB1: /* Subnegotiation */
245 case TS_SUB2: /* Subnegotiation */
254 if (G.telstate == TS_0)
260 write(STDOUT_FILENO, G.buf, len);
263 static void put_iac(int c)
265 G.iacbuf[G.iaclen++] = c;
268 static void put_iac2(byte wwdd, byte c)
270 if (G.iaclen + 3 > IACBUFSIZE)
278 #if ENABLE_FEATURE_TELNET_TTYPE
279 static void put_iac_subopt(byte c, char *str)
281 int len = strlen(str) + 6; // ( 2 + 1 + 1 + strlen + 2 )
283 if (G.iaclen + len > IACBUFSIZE)
299 #if ENABLE_FEATURE_TELNET_AUTOLOGIN
300 static void put_iac_subopt_autologin(void)
302 int len = strlen(G.autologin) + 6; // (2 + 1 + 1 + strlen + 2)
303 const char *user = "USER";
305 if (G.iaclen + len > IACBUFSIZE)
310 put_iac(TELOPT_NEW_ENVIRON);
312 put_iac(NEW_ENV_VAR);
317 put_iac(NEW_ENV_VALUE);
320 put_iac(*G.autologin++);
327 #if ENABLE_FEATURE_AUTOWIDTH
328 static void put_iac_naws(byte c, int x, int y)
330 if (G.iaclen + 9 > IACBUFSIZE)
337 put_iac((x >> 8) & 0xff);
339 put_iac((y >> 8) & 0xff);
347 static char const escapecharis[] ALIGN1 = "\r\nEscape character is ";
349 static void setConMode(void)
351 if (G.telflags & UF_ECHO) {
352 if (G.charmode == CHM_TRY) {
354 printf("\r\nEntering character mode%s'^]'.\r\n", escapecharis);
358 if (G.charmode != CHM_OFF) {
359 G.charmode = CHM_OFF;
360 printf("\r\nEntering line mode%s'^C'.\r\n", escapecharis);
366 static void will_charmode(void)
368 G.charmode = CHM_TRY;
369 G.telflags |= (UF_ECHO | UF_SGA);
372 put_iac2(DO, TELOPT_ECHO);
373 put_iac2(DO, TELOPT_SGA);
377 static void do_linemode(void)
379 G.charmode = CHM_TRY;
380 G.telflags &= ~(UF_ECHO | UF_SGA);
383 put_iac2(DONT, TELOPT_ECHO);
384 put_iac2(DONT, TELOPT_SGA);
388 static void to_notsup(char c)
390 if (G.telwish == WILL)
392 else if (G.telwish == DO)
396 static void to_echo(void)
398 /* if server requests ECHO, don't agree */
399 if (G.telwish == DO) {
400 put_iac2(WONT, TELOPT_ECHO);
403 if (G.telwish == DONT)
406 if (G.telflags & UF_ECHO) {
407 if (G.telwish == WILL)
409 } else if (G.telwish == WONT)
412 if (G.charmode != CHM_OFF)
413 G.telflags ^= UF_ECHO;
415 if (G.telflags & UF_ECHO)
416 put_iac2(DO, TELOPT_ECHO);
418 put_iac2(DONT, TELOPT_ECHO);
421 write_str(1, "\r\n"); /* sudden modec */
424 static void to_sga(void)
426 /* daemon always sends will/wont, client do/dont */
428 if (G.telflags & UF_SGA) {
429 if (G.telwish == WILL)
431 } else if (G.telwish == WONT)
434 G.telflags ^= UF_SGA; /* toggle */
435 if (G.telflags & UF_SGA)
436 put_iac2(DO, TELOPT_SGA);
438 put_iac2(DONT, TELOPT_SGA);
441 #if ENABLE_FEATURE_TELNET_TTYPE
442 static void to_ttype(void)
444 /* Tell server we will (or won't) do TTYPE */
447 put_iac2(WILL, TELOPT_TTYPE);
449 put_iac2(WONT, TELOPT_TTYPE);
453 #if ENABLE_FEATURE_TELNET_AUTOLOGIN
454 static void to_new_environ(void)
456 /* Tell server we will (or will not) do AUTOLOGIN */
459 put_iac2(WILL, TELOPT_NEW_ENVIRON);
461 put_iac2(WONT, TELOPT_NEW_ENVIRON);
465 #if ENABLE_FEATURE_AUTOWIDTH
466 static void to_naws(void)
468 /* Tell server we will do NAWS */
469 put_iac2(WILL, TELOPT_NAWS);
473 static void telopt(byte c)
480 #if ENABLE_FEATURE_TELNET_TTYPE
484 #if ENABLE_FEATURE_TELNET_AUTOLOGIN
485 case TELOPT_NEW_ENVIRON:
486 to_new_environ(); break;
488 #if ENABLE_FEATURE_AUTOWIDTH
491 put_iac_naws(c, G.win_width, G.win_height);
500 /* subnegotiation -- ignore all (except TTYPE,NAWS) */
501 static int subneg(byte c)
503 switch (G.telstate) {
506 G.telstate = TS_SUB2;
507 #if ENABLE_FEATURE_TELNET_TTYPE
509 if (c == TELOPT_TTYPE)
510 put_iac_subopt(TELOPT_TTYPE, G.ttype);
512 #if ENABLE_FEATURE_TELNET_AUTOLOGIN
514 if (c == TELOPT_NEW_ENVIRON)
515 put_iac_subopt_autologin();
521 G.telstate = TS_SUB1;
527 static void rawmode(void)
530 tcsetattr(0, TCSADRAIN, &G.termios_raw);
533 static void cookmode(void)
536 tcsetattr(0, TCSADRAIN, &G.termios_def);
539 /* poll gives smaller (-70 bytes) code */
542 int telnet_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
543 int telnet_main(int argc UNUSED_PARAM, char **argv)
549 struct pollfd ufds[2];
557 #if ENABLE_FEATURE_AUTOWIDTH
558 get_terminal_width_height(0, &G.win_width, &G.win_height);
561 #if ENABLE_FEATURE_TELNET_TTYPE
562 G.ttype = getenv("TERM");
565 if (tcgetattr(0, &G.termios_def) >= 0) {
567 G.termios_raw = G.termios_def;
568 cfmakeraw(&G.termios_raw);
571 #if ENABLE_FEATURE_TELNET_AUTOLOGIN
572 if (1 & getopt32(argv, "al:", &G.autologin))
573 G.autologin = getenv("USER");
581 port = bb_lookup_port(*argv ? *argv++ : "telnet", "tcp", 23);
582 if (*argv) /* extra params?? */
585 xmove_fd(create_and_connect_stream_or_die(host, port), netfd);
587 setsockopt(netfd, SOL_SOCKET, SO_KEEPALIVE, &const_int_1, sizeof(const_int_1));
589 signal(SIGINT, record_signo);
592 ufds[0].fd = 0; ufds[1].fd = netfd;
593 ufds[0].events = ufds[1].events = POLLIN;
596 FD_SET(STDIN_FILENO, &readfds);
597 FD_SET(netfd, &readfds);
603 fd_set rfds = readfds;
605 switch (select(maxfd, &rfds, NULL, NULL, NULL))
607 switch (poll(ufds, 2, -1))
613 /* error, ignore and/or log something, bay go to loop */
622 if (ufds[0].revents) /* well, should check POLLIN, but ... */
624 if (FD_ISSET(STDIN_FILENO, &rfds))
627 len = read(STDIN_FILENO, G.buf, DATABUFSIZE);
629 doexit(EXIT_SUCCESS);
630 TRACE(0, ("Read con: %d\n", len));
631 handle_net_output(len);
635 if (ufds[1].revents) /* well, should check POLLIN, but ... */
637 if (FD_ISSET(netfd, &rfds))
640 len = read(netfd, G.buf, DATABUFSIZE);
642 write_str(1, "Connection closed by foreign host\r\n");
643 doexit(EXIT_FAILURE);
645 TRACE(0, ("Read netfd (%d): %d\n", netfd, len));
646 handle_net_input(len);