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 */
65 #if ENABLE_FEATURE_TELNET_TTYPE
68 #if ENABLE_FEATURE_TELNET_AUTOLOGIN
69 const char *autologin;
71 #if ENABLE_FEATURE_AUTOWIDTH
72 unsigned win_width, win_height;
74 /* same buffer used both for network and console read/write */
75 char buf[DATABUFSIZE];
76 /* buffer to handle telnet negotiations */
77 char iacbuf[IACBUFSIZE];
78 struct termios termios_def;
79 struct termios termios_raw;
81 #define G (*(struct globals*)&bb_common_bufsiz1)
82 void BUG_telnet_globals_too_big(void);
83 #define INIT_G() do { \
84 if (sizeof(G) > COMMON_BUFSIZE) \
85 BUG_telnet_globals_too_big(); \
86 /* memset(&G, 0, sizeof G); - already is */ \
89 /* Function prototypes */
90 static void rawmode(void);
91 static void cookmode(void);
92 static void do_linemode(void);
93 static void will_charmode(void);
94 static void telopt(byte c);
95 static int subneg(byte c);
97 static void iacflush(void)
99 write(netfd, G.iacbuf, G.iaclen);
103 #define write_str(fd, str) write(fd, str, sizeof(str) - 1)
105 static void doexit(int ev) NORETURN;
106 static void doexit(int ev)
112 static void conescape(void)
116 if (G.gotsig) /* came from line mode... go raw */
119 write_str(1, "\r\nConsole escape. Commands are:\r\n\n"
120 " l go to line mode\r\n"
121 " c go to character mode\r\n"
122 " z suspend telnet\r\n"
123 " e exit telnet\r\n");
125 if (read(STDIN_FILENO, &b, 1) <= 0)
126 doexit(EXIT_FAILURE);
147 doexit(EXIT_SUCCESS);
150 write_str(1, "continuing...\r\n");
160 static void handlenetoutput(int len)
162 /* here we could do smart tricks how to handle 0xFF:s in output
163 * stream like writing twice every sequence of FF:s (thus doing
164 * many write()s. But I think interactive telnet application does
165 * not need to be 100% 8-bit clean, so changing every 0xff:s to
168 * 2002-mar-21, Przemyslaw Czerpak (druzus@polbox.com)
170 * first - I cannot use programs like sz/rz
171 * second - the 0x0D is sent as one character and if the next
172 * char is 0x0A then it's eaten by a server side.
173 * third - whay doy you have to make 'many write()s'?
174 * I don't understand.
175 * So I implemented it. It's realy useful for me. I hope that
176 * others people will find it interesting too.
180 byte * p = (byte*)G.buf;
181 byte outbuf[4*DATABUFSIZE];
183 for (i = len, j = 0; i > 0; i--, p++) {
195 write(netfd, outbuf, j);
198 static void handlenetinput(int len)
203 for (i = 0; i < len; i++) {
206 if (G.telstate == 0) { /* most of the time state == 0 */
212 switch (G.telstate) {
221 if (c == IAC) { /* IAC IAC -> 0xFF */
229 G.telstate = TS_SUB1;
239 G.telstate = TS_0; /* DATA MARK must be added later */
242 case TS_OPT: /* WILL, WONT, DO, DONT */
246 case TS_SUB1: /* Subnegotiation */
247 case TS_SUB2: /* Subnegotiation */
254 if (G.iaclen) iacflush();
255 if (G.telstate == TS_0) G.telstate = 0;
260 write(STDOUT_FILENO, G.buf, len);
263 static void putiac(int c)
265 G.iacbuf[G.iaclen++] = c;
268 static void putiac2(byte wwdd, byte c)
270 if (G.iaclen + 3 > IACBUFSIZE)
278 #if ENABLE_FEATURE_TELNET_TTYPE
279 static void putiac_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 putiac_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 putiac(TELOPT_NEW_ENVIRON);
317 putiac(NEW_ENV_VALUE);
320 putiac(*G.autologin++);
327 #if ENABLE_FEATURE_AUTOWIDTH
328 static void putiac_naws(byte c, int x, int y)
330 if (G.iaclen + 9 > IACBUFSIZE)
337 putiac((x >> 8) & 0xff);
339 putiac((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 putiac2(DO, TELOPT_ECHO);
373 putiac2(DO, TELOPT_SGA);
377 static void do_linemode(void)
379 G.charmode = CHM_TRY;
380 G.telflags &= ~(UF_ECHO | UF_SGA);
383 putiac2(DONT, TELOPT_ECHO);
384 putiac2(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 putiac2(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 putiac2(DO, TELOPT_ECHO);
418 putiac2(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 putiac2(DO, TELOPT_SGA);
438 putiac2(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 putiac2(WILL, TELOPT_TTYPE);
449 putiac2(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 putiac2(WILL, TELOPT_NEW_ENVIRON);
461 putiac2(WONT, TELOPT_NEW_ENVIRON);
465 #if ENABLE_FEATURE_AUTOWIDTH
466 static void to_naws(void)
468 /* Tell server we will do NAWS */
469 putiac2(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 putiac_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 putiac_subopt(TELOPT_TTYPE, G.ttype);
512 #if ENABLE_FEATURE_TELNET_AUTOLOGIN
514 if (c == TELOPT_NEW_ENVIRON)
515 putiac_subopt_autologin();
521 G.telstate = TS_SUB1;
527 static void fgotsig(int sig)
533 static void rawmode(void)
536 tcsetattr(0, TCSADRAIN, &G.termios_raw);
539 static void cookmode(void)
542 tcsetattr(0, TCSADRAIN, &G.termios_def);
545 /* poll gives smaller (-70 bytes) code */
548 int telnet_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
549 int telnet_main(int argc UNUSED_PARAM, char **argv)
555 struct pollfd ufds[2];
563 #if ENABLE_FEATURE_AUTOWIDTH
564 get_terminal_width_height(0, &G.win_width, &G.win_height);
567 #if ENABLE_FEATURE_TELNET_TTYPE
568 G.ttype = getenv("TERM");
571 if (tcgetattr(0, &G.termios_def) >= 0) {
573 G.termios_raw = G.termios_def;
574 cfmakeraw(&G.termios_raw);
577 #if ENABLE_FEATURE_TELNET_AUTOLOGIN
578 if (1 & getopt32(argv, "al:", &G.autologin))
579 G.autologin = getenv("USER");
587 port = bb_lookup_port(*argv ? *argv++ : "telnet", "tcp", 23);
588 if (*argv) /* extra params?? */
591 xmove_fd(create_and_connect_stream_or_die(host, port), netfd);
593 setsockopt(netfd, SOL_SOCKET, SO_KEEPALIVE, &const_int_1, sizeof(const_int_1));
595 signal(SIGINT, fgotsig);
598 ufds[0].fd = 0; ufds[1].fd = netfd;
599 ufds[0].events = ufds[1].events = POLLIN;
602 FD_SET(STDIN_FILENO, &readfds);
603 FD_SET(netfd, &readfds);
609 fd_set rfds = readfds;
611 switch (select(maxfd, &rfds, NULL, NULL, NULL))
613 switch (poll(ufds, 2, -1))
619 /* error, ignore and/or log something, bay go to loop */
628 if (ufds[0].revents) /* well, should check POLLIN, but ... */
630 if (FD_ISSET(STDIN_FILENO, &rfds))
633 len = read(STDIN_FILENO, G.buf, DATABUFSIZE);
635 doexit(EXIT_SUCCESS);
636 TRACE(0, ("Read con: %d\n", len));
637 handlenetoutput(len);
641 if (ufds[1].revents) /* well, should check POLLIN, but ... */
643 if (FD_ISSET(netfd, &rfds))
646 len = read(netfd, G.buf, DATABUFSIZE);
648 write_str(1, "Connection closed by foreign host\r\n");
649 doexit(EXIT_FAILURE);
651 TRACE(0, ("Read netfd (%d): %d\n", netfd, len));