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"
30 //usage: "\n -a Automatic login with $USER variable"
31 //usage: "\n -l USER Automatic login as USER"
34 //usage:#define telnet_trivial_usage
35 //usage: "HOST [PORT]"
36 //usage:#define telnet_full_usage "\n\n"
37 //usage: "Connect to telnet server"
40 #include <arpa/telnet.h>
41 #include <netinet/in.h>
45 #define TRACE(x, y) do { if (x) printf y; } while (0)
70 typedef unsigned char byte;
75 int iaclen; /* could even use byte, but it's a loss on x86 */
76 byte telstate; /* telnet negotiation state from network input */
77 byte telwish; /* DO, DONT, WILL, WONT */
81 #if ENABLE_FEATURE_TELNET_TTYPE
84 #if ENABLE_FEATURE_TELNET_AUTOLOGIN
85 const char *autologin;
87 #if ENABLE_FEATURE_AUTOWIDTH
88 unsigned win_width, win_height;
90 /* same buffer used both for network and console read/write */
91 char buf[DATABUFSIZE];
92 /* buffer to handle telnet negotiations */
93 char iacbuf[IACBUFSIZE];
94 struct termios termios_def;
95 struct termios termios_raw;
97 #define G (*(struct globals*)&bb_common_bufsiz1)
98 #define INIT_G() do { \
99 struct G_sizecheck { \
100 char G_sizecheck[sizeof(G) > COMMON_BUFSIZE ? -1 : 1]; \
105 static void rawmode(void);
106 static void cookmode(void);
107 static void do_linemode(void);
108 static void will_charmode(void);
109 static void telopt(byte c);
110 static void subneg(byte c);
112 static void iac_flush(void)
114 write(netfd, G.iacbuf, G.iaclen);
118 #define write_str(fd, str) write(fd, str, sizeof(str) - 1)
120 static void doexit(int ev) NORETURN;
121 static void doexit(int ev)
127 static void con_escape(void)
131 if (bb_got_signal) /* came from line mode... go raw */
134 write_str(1, "\r\nConsole escape. Commands are:\r\n\n"
135 " l go to line mode\r\n"
136 " c go to character mode\r\n"
137 " z suspend telnet\r\n"
138 " e exit telnet\r\n");
140 if (read(STDIN_FILENO, &b, 1) <= 0)
141 doexit(EXIT_FAILURE);
145 if (!bb_got_signal) {
162 doexit(EXIT_SUCCESS);
165 write_str(1, "continuing...\r\n");
173 static void handle_net_output(int len)
175 /* here we could do smart tricks how to handle 0xFF:s in output
176 * stream like writing twice every sequence of FF:s (thus doing
177 * many write()s. But I think interactive telnet application does
178 * not need to be 100% 8-bit clean, so changing every 0xff:s to
181 * 2002-mar-21, Przemyslaw Czerpak (druzus@polbox.com)
183 * first - I cannot use programs like sz/rz
184 * second - the 0x0D is sent as one character and if the next
185 * char is 0x0A then it's eaten by a server side.
186 * third - why do you have to make 'many write()s'?
187 * I don't understand.
188 * So I implemented it. It's really useful for me. I hope that
189 * other people will find it interesting too.
191 byte outbuf[2 * DATABUFSIZE];
192 byte *p = (byte*)G.buf;
195 for (; len > 0; len--, p++) {
203 outbuf[j++] = c; /* IAC -> IAC IAC */
205 outbuf[j++] = '\0'; /* CR -> CR NUL */
208 full_write(netfd, outbuf, j);
211 static void handle_net_input(int len)
216 for (i = 0; i < len; i++) {
219 if (G.telstate == TS_NORMAL) { /* most typical state */
224 else if (c == '\r') {
228 /* No IACs were seen so far, no need to copy
229 * bytes within G.buf: */
233 switch (G.telstate) {
235 /* Prev char was CR. If cur one is NUL, ignore it.
236 * See RFC 1123 section 3.3.1 for discussion of telnet EOL handling.
238 G.telstate = TS_COPY;
241 /* else: fall through - need to handle CR IAC ... properly */
243 case TS_COPY: /* Prev char was ordinary */
244 /* Similar to NORMAL, but in TS_COPY we need to copy bytes */
253 case TS_IAC: /* Prev char was IAC */
254 if (c == IAC) { /* IAC IAC -> one IAC */
256 G.telstate = TS_COPY;
262 G.telstate = TS_SUB1;
271 /* DATA MARK must be added later */
273 G.telstate = TS_COPY;
277 case TS_OPT: /* Prev chars were IAC WILL/WONT/DO/DONT */
279 G.telstate = TS_COPY;
282 case TS_SUB1: /* Subnegotiation */
283 case TS_SUB2: /* Subnegotiation */
284 subneg(c); /* can change G.telstate */
289 if (G.telstate != TS_NORMAL) {
290 /* We had some IACs, or CR */
293 if (G.telstate == TS_COPY) /* we aren't in the middle of IAC */
294 G.telstate = TS_NORMAL;
299 full_write(STDOUT_FILENO, G.buf, len);
302 static void put_iac(int c)
304 G.iacbuf[G.iaclen++] = c;
307 static void put_iac2(byte wwdd, byte c)
309 if (G.iaclen + 3 > IACBUFSIZE)
317 #if ENABLE_FEATURE_TELNET_TTYPE
318 static void put_iac_subopt(byte c, char *str)
320 int len = strlen(str) + 6; // ( 2 + 1 + 1 + strlen + 2 )
322 if (G.iaclen + len > IACBUFSIZE)
338 #if ENABLE_FEATURE_TELNET_AUTOLOGIN
339 static void put_iac_subopt_autologin(void)
341 int len = strlen(G.autologin) + 6; // (2 + 1 + 1 + strlen + 2)
342 const char *p = "USER";
344 if (G.iaclen + len > IACBUFSIZE)
349 put_iac(TELOPT_NEW_ENVIRON);
351 put_iac(NEW_ENV_VAR);
356 put_iac(NEW_ENV_VALUE);
367 #if ENABLE_FEATURE_AUTOWIDTH
368 static void put_iac_naws(byte c, int x, int y)
370 if (G.iaclen + 9 > IACBUFSIZE)
377 put_iac((x >> 8) & 0xff);
379 put_iac((y >> 8) & 0xff);
387 static char const escapecharis[] ALIGN1 = "\r\nEscape character is ";
389 static void setConMode(void)
391 if (G.telflags & UF_ECHO) {
392 if (G.charmode == CHM_TRY) {
394 printf("\r\nEntering character mode%s'^]'.\r\n", escapecharis);
398 if (G.charmode != CHM_OFF) {
399 G.charmode = CHM_OFF;
400 printf("\r\nEntering line mode%s'^C'.\r\n", escapecharis);
406 static void will_charmode(void)
408 G.charmode = CHM_TRY;
409 G.telflags |= (UF_ECHO | UF_SGA);
412 put_iac2(DO, TELOPT_ECHO);
413 put_iac2(DO, TELOPT_SGA);
417 static void do_linemode(void)
419 G.charmode = CHM_TRY;
420 G.telflags &= ~(UF_ECHO | UF_SGA);
423 put_iac2(DONT, TELOPT_ECHO);
424 put_iac2(DONT, TELOPT_SGA);
428 static void to_notsup(char c)
430 if (G.telwish == WILL)
432 else if (G.telwish == DO)
436 static void to_echo(void)
438 /* if server requests ECHO, don't agree */
439 if (G.telwish == DO) {
440 put_iac2(WONT, TELOPT_ECHO);
443 if (G.telwish == DONT)
446 if (G.telflags & UF_ECHO) {
447 if (G.telwish == WILL)
449 } else if (G.telwish == WONT)
452 if (G.charmode != CHM_OFF)
453 G.telflags ^= UF_ECHO;
455 if (G.telflags & UF_ECHO)
456 put_iac2(DO, TELOPT_ECHO);
458 put_iac2(DONT, TELOPT_ECHO);
461 full_write1_str("\r\n"); /* sudden modec */
464 static void to_sga(void)
466 /* daemon always sends will/wont, client do/dont */
468 if (G.telflags & UF_SGA) {
469 if (G.telwish == WILL)
471 } else if (G.telwish == WONT)
474 G.telflags ^= UF_SGA; /* toggle */
475 if (G.telflags & UF_SGA)
476 put_iac2(DO, TELOPT_SGA);
478 put_iac2(DONT, TELOPT_SGA);
481 #if ENABLE_FEATURE_TELNET_TTYPE
482 static void to_ttype(void)
484 /* Tell server we will (or won't) do TTYPE */
486 put_iac2(WILL, TELOPT_TTYPE);
488 put_iac2(WONT, TELOPT_TTYPE);
492 #if ENABLE_FEATURE_TELNET_AUTOLOGIN
493 static void to_new_environ(void)
495 /* Tell server we will (or will not) do AUTOLOGIN */
497 put_iac2(WILL, TELOPT_NEW_ENVIRON);
499 put_iac2(WONT, TELOPT_NEW_ENVIRON);
503 #if ENABLE_FEATURE_AUTOWIDTH
504 static void to_naws(void)
506 /* Tell server we will do NAWS */
507 put_iac2(WILL, TELOPT_NAWS);
511 static void telopt(byte c)
518 #if ENABLE_FEATURE_TELNET_TTYPE
522 #if ENABLE_FEATURE_TELNET_AUTOLOGIN
523 case TELOPT_NEW_ENVIRON:
524 to_new_environ(); break;
526 #if ENABLE_FEATURE_AUTOWIDTH
529 put_iac_naws(c, G.win_width, G.win_height);
538 /* subnegotiation -- ignore all (except TTYPE,NAWS) */
539 static void subneg(byte c)
541 switch (G.telstate) {
544 G.telstate = TS_SUB2;
545 #if ENABLE_FEATURE_TELNET_TTYPE
547 if (c == TELOPT_TTYPE && G.ttype)
548 put_iac_subopt(TELOPT_TTYPE, G.ttype);
550 #if ENABLE_FEATURE_TELNET_AUTOLOGIN
552 if (c == TELOPT_NEW_ENVIRON && G.autologin)
553 put_iac_subopt_autologin();
558 G.telstate = TS_COPY;
561 G.telstate = TS_SUB1;
566 static void rawmode(void)
569 tcsetattr(0, TCSADRAIN, &G.termios_raw);
572 static void cookmode(void)
575 tcsetattr(0, TCSADRAIN, &G.termios_def);
578 int telnet_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
579 int telnet_main(int argc UNUSED_PARAM, char **argv)
584 struct pollfd ufds[2];
588 #if ENABLE_FEATURE_AUTOWIDTH
589 get_terminal_width_height(0, &G.win_width, &G.win_height);
592 #if ENABLE_FEATURE_TELNET_TTYPE
593 G.ttype = getenv("TERM");
596 if (tcgetattr(0, &G.termios_def) >= 0) {
598 G.termios_raw = G.termios_def;
599 cfmakeraw(&G.termios_raw);
602 #if ENABLE_FEATURE_TELNET_AUTOLOGIN
603 if (1 & getopt32(argv, "al:", &G.autologin))
604 G.autologin = getenv("USER");
612 port = bb_lookup_port(*argv ? *argv++ : "telnet", "tcp", 23);
613 if (*argv) /* extra params?? */
616 xmove_fd(create_and_connect_stream_or_die(host, port), netfd);
618 setsockopt(netfd, SOL_SOCKET, SO_KEEPALIVE, &const_int_1, sizeof(const_int_1));
620 signal(SIGINT, record_signo);
622 ufds[0].fd = STDIN_FILENO;
623 ufds[0].events = POLLIN;
625 ufds[1].events = POLLIN;
628 if (poll(ufds, 2, -1) < 0) {
629 /* error, ignore and/or log something, bay go to loop */
637 // FIXME: reads can block. Need full bidirectional buffering.
639 if (ufds[0].revents) {
640 len = safe_read(STDIN_FILENO, G.buf, DATABUFSIZE);
642 doexit(EXIT_SUCCESS);
643 TRACE(0, ("Read con: %d\n", len));
644 handle_net_output(len);
647 if (ufds[1].revents) {
648 len = safe_read(netfd, G.buf, DATABUFSIZE);
650 full_write1_str("Connection closed by foreign host\r\n");
651 doexit(EXIT_FAILURE);
653 TRACE(0, ("Read netfd (%d): %d\n", netfd, len));
654 handle_net_input(len);