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 #define INIT_G() do { \
81 struct G_sizecheck { \
82 char G_sizecheck[sizeof(G) > COMMON_BUFSIZE ? -1 : 1]; \
86 /* Function prototypes */
87 static void rawmode(void);
88 static void cookmode(void);
89 static void do_linemode(void);
90 static void will_charmode(void);
91 static void telopt(byte c);
92 static int subneg(byte c);
94 static void iac_flush(void)
96 write(netfd, G.iacbuf, G.iaclen);
100 #define write_str(fd, str) write(fd, str, sizeof(str) - 1)
102 static void doexit(int ev) NORETURN;
103 static void doexit(int ev)
109 static void con_escape(void)
113 if (bb_got_signal) /* came from line mode... go raw */
116 write_str(1, "\r\nConsole escape. Commands are:\r\n\n"
117 " l go to line mode\r\n"
118 " c go to character mode\r\n"
119 " z suspend telnet\r\n"
120 " e exit telnet\r\n");
122 if (read(STDIN_FILENO, &b, 1) <= 0)
123 doexit(EXIT_FAILURE);
127 if (!bb_got_signal) {
144 doexit(EXIT_SUCCESS);
147 write_str(1, "continuing...\r\n");
156 static void handle_net_output(int len)
158 /* here we could do smart tricks how to handle 0xFF:s in output
159 * stream like writing twice every sequence of FF:s (thus doing
160 * many write()s. But I think interactive telnet application does
161 * not need to be 100% 8-bit clean, so changing every 0xff:s to
164 * 2002-mar-21, Przemyslaw Czerpak (druzus@polbox.com)
166 * first - I cannot use programs like sz/rz
167 * second - the 0x0D is sent as one character and if the next
168 * char is 0x0A then it's eaten by a server side.
169 * third - why do you have to make 'many write()s'?
170 * I don't understand.
171 * So I implemented it. It's really useful for me. I hope that
172 * other people will find it interesting too.
176 byte *p = (byte*)G.buf;
177 byte outbuf[4*DATABUFSIZE];
179 for (i = len, j = 0; i > 0; i--, p++) {
191 write(netfd, outbuf, j);
194 static void handle_net_input(int len)
199 for (i = 0; i < len; i++) {
202 if (G.telstate == 0) { /* most of the time state == 0 */
209 switch (G.telstate) {
218 if (c == IAC) { /* IAC IAC -> 0xFF */
226 G.telstate = TS_SUB1;
236 G.telstate = TS_0; /* DATA MARK must be added later */
239 case TS_OPT: /* WILL, WONT, DO, DONT */
243 case TS_SUB1: /* Subnegotiation */
244 case TS_SUB2: /* Subnegotiation */
253 if (G.telstate == TS_0)
259 write(STDOUT_FILENO, G.buf, len);
262 static void put_iac(int c)
264 G.iacbuf[G.iaclen++] = c;
267 static void put_iac2(byte wwdd, byte c)
269 if (G.iaclen + 3 > IACBUFSIZE)
277 #if ENABLE_FEATURE_TELNET_TTYPE
278 static void put_iac_subopt(byte c, char *str)
280 int len = strlen(str) + 6; // ( 2 + 1 + 1 + strlen + 2 )
282 if (G.iaclen + len > IACBUFSIZE)
298 #if ENABLE_FEATURE_TELNET_AUTOLOGIN
299 static void put_iac_subopt_autologin(void)
301 int len = strlen(G.autologin) + 6; // (2 + 1 + 1 + strlen + 2)
302 const char *user = "USER";
304 if (G.iaclen + len > IACBUFSIZE)
309 put_iac(TELOPT_NEW_ENVIRON);
311 put_iac(NEW_ENV_VAR);
316 put_iac(NEW_ENV_VALUE);
319 put_iac(*G.autologin++);
326 #if ENABLE_FEATURE_AUTOWIDTH
327 static void put_iac_naws(byte c, int x, int y)
329 if (G.iaclen + 9 > IACBUFSIZE)
336 put_iac((x >> 8) & 0xff);
338 put_iac((y >> 8) & 0xff);
346 static char const escapecharis[] ALIGN1 = "\r\nEscape character is ";
348 static void setConMode(void)
350 if (G.telflags & UF_ECHO) {
351 if (G.charmode == CHM_TRY) {
353 printf("\r\nEntering character mode%s'^]'.\r\n", escapecharis);
357 if (G.charmode != CHM_OFF) {
358 G.charmode = CHM_OFF;
359 printf("\r\nEntering line mode%s'^C'.\r\n", escapecharis);
365 static void will_charmode(void)
367 G.charmode = CHM_TRY;
368 G.telflags |= (UF_ECHO | UF_SGA);
371 put_iac2(DO, TELOPT_ECHO);
372 put_iac2(DO, TELOPT_SGA);
376 static void do_linemode(void)
378 G.charmode = CHM_TRY;
379 G.telflags &= ~(UF_ECHO | UF_SGA);
382 put_iac2(DONT, TELOPT_ECHO);
383 put_iac2(DONT, TELOPT_SGA);
387 static void to_notsup(char c)
389 if (G.telwish == WILL)
391 else if (G.telwish == DO)
395 static void to_echo(void)
397 /* if server requests ECHO, don't agree */
398 if (G.telwish == DO) {
399 put_iac2(WONT, TELOPT_ECHO);
402 if (G.telwish == DONT)
405 if (G.telflags & UF_ECHO) {
406 if (G.telwish == WILL)
408 } else if (G.telwish == WONT)
411 if (G.charmode != CHM_OFF)
412 G.telflags ^= UF_ECHO;
414 if (G.telflags & UF_ECHO)
415 put_iac2(DO, TELOPT_ECHO);
417 put_iac2(DONT, TELOPT_ECHO);
420 write_str(1, "\r\n"); /* sudden modec */
423 static void to_sga(void)
425 /* daemon always sends will/wont, client do/dont */
427 if (G.telflags & UF_SGA) {
428 if (G.telwish == WILL)
430 } else if (G.telwish == WONT)
433 G.telflags ^= UF_SGA; /* toggle */
434 if (G.telflags & UF_SGA)
435 put_iac2(DO, TELOPT_SGA);
437 put_iac2(DONT, TELOPT_SGA);
440 #if ENABLE_FEATURE_TELNET_TTYPE
441 static void to_ttype(void)
443 /* Tell server we will (or won't) do TTYPE */
446 put_iac2(WILL, TELOPT_TTYPE);
448 put_iac2(WONT, TELOPT_TTYPE);
452 #if ENABLE_FEATURE_TELNET_AUTOLOGIN
453 static void to_new_environ(void)
455 /* Tell server we will (or will not) do AUTOLOGIN */
458 put_iac2(WILL, TELOPT_NEW_ENVIRON);
460 put_iac2(WONT, TELOPT_NEW_ENVIRON);
464 #if ENABLE_FEATURE_AUTOWIDTH
465 static void to_naws(void)
467 /* Tell server we will do NAWS */
468 put_iac2(WILL, TELOPT_NAWS);
472 static void telopt(byte c)
479 #if ENABLE_FEATURE_TELNET_TTYPE
483 #if ENABLE_FEATURE_TELNET_AUTOLOGIN
484 case TELOPT_NEW_ENVIRON:
485 to_new_environ(); break;
487 #if ENABLE_FEATURE_AUTOWIDTH
490 put_iac_naws(c, G.win_width, G.win_height);
499 /* subnegotiation -- ignore all (except TTYPE,NAWS) */
500 static int subneg(byte c)
502 switch (G.telstate) {
505 G.telstate = TS_SUB2;
506 #if ENABLE_FEATURE_TELNET_TTYPE
508 if (c == TELOPT_TTYPE)
509 put_iac_subopt(TELOPT_TTYPE, G.ttype);
511 #if ENABLE_FEATURE_TELNET_AUTOLOGIN
513 if (c == TELOPT_NEW_ENVIRON)
514 put_iac_subopt_autologin();
520 G.telstate = TS_SUB1;
526 static void rawmode(void)
529 tcsetattr(0, TCSADRAIN, &G.termios_raw);
532 static void cookmode(void)
535 tcsetattr(0, TCSADRAIN, &G.termios_def);
538 /* poll gives smaller (-70 bytes) code */
541 int telnet_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
542 int telnet_main(int argc UNUSED_PARAM, char **argv)
548 struct pollfd ufds[2];
556 #if ENABLE_FEATURE_AUTOWIDTH
557 get_terminal_width_height(0, &G.win_width, &G.win_height);
560 #if ENABLE_FEATURE_TELNET_TTYPE
561 G.ttype = getenv("TERM");
564 if (tcgetattr(0, &G.termios_def) >= 0) {
566 G.termios_raw = G.termios_def;
567 cfmakeraw(&G.termios_raw);
570 #if ENABLE_FEATURE_TELNET_AUTOLOGIN
571 if (1 & getopt32(argv, "al:", &G.autologin))
572 G.autologin = getenv("USER");
580 port = bb_lookup_port(*argv ? *argv++ : "telnet", "tcp", 23);
581 if (*argv) /* extra params?? */
584 xmove_fd(create_and_connect_stream_or_die(host, port), netfd);
586 setsockopt(netfd, SOL_SOCKET, SO_KEEPALIVE, &const_int_1, sizeof(const_int_1));
588 signal(SIGINT, record_signo);
591 ufds[0].fd = 0; ufds[1].fd = netfd;
592 ufds[0].events = ufds[1].events = POLLIN;
595 FD_SET(STDIN_FILENO, &readfds);
596 FD_SET(netfd, &readfds);
602 fd_set rfds = readfds;
604 switch (select(maxfd, &rfds, NULL, NULL, NULL))
606 switch (poll(ufds, 2, -1))
612 /* error, ignore and/or log something, bay go to loop */
621 if (ufds[0].revents & POLLIN)
623 if (FD_ISSET(STDIN_FILENO, &rfds))
626 len = safe_read(STDIN_FILENO, G.buf, DATABUFSIZE);
628 doexit(EXIT_SUCCESS);
629 TRACE(0, ("Read con: %d\n", len));
630 handle_net_output(len);
634 if (ufds[1].revents & POLLIN)
636 if (FD_ISSET(netfd, &rfds))
639 len = safe_read(netfd, G.buf, DATABUFSIZE);
641 write_str(1, "Connection closed by foreign host\r\n");
642 doexit(EXIT_FAILURE);
644 TRACE(0, ("Read netfd (%d): %d\n", netfd, len));
645 handle_net_input(len);