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 #include <arpa/telnet.h>
25 #include <netinet/in.h>
29 #define TRACE(x, y) do { if (x) printf y; } while (0)
54 typedef unsigned char byte
;
59 int iaclen
; /* could even use byte, but it's a loss on x86 */
60 byte telstate
; /* telnet negotiation state from network input */
61 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 #define INIT_G() do { \
83 struct G_sizecheck { \
84 char G_sizecheck[sizeof(G) > COMMON_BUFSIZE ? -1 : 1]; \
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 void 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");
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.
175 byte outbuf
[2 * DATABUFSIZE
];
176 byte
*p
= (byte
*)G
.buf
;
179 for (; len
> 0; len
--, p
++) {
187 outbuf
[j
++] = c
; /* IAC -> IAC IAC */
189 outbuf
[j
++] = '\0'; /* CR -> CR NUL */
192 full_write(netfd
, outbuf
, j
);
195 static void handle_net_input(int len
)
200 for (i
= 0; i
< len
; i
++) {
203 if (G
.telstate
== TS_NORMAL
) { /* most typical state */
208 else if (c
== '\r') {
212 /* No IACs were seen so far, no need to copy
213 * bytes within G.buf: */
217 switch (G
.telstate
) {
219 /* Prev char was CR. If cur one is NUL, ignore it.
220 * See RFC 1123 section 3.3.1 for discussion of telnet EOL handling.
222 G
.telstate
= TS_COPY
;
225 /* else: fall through - need to handle CR IAC ... properly */
227 case TS_COPY
: /* Prev char was ordinary */
228 /* Similar to NORMAL, but in TS_COPY we need to copy bytes */
237 case TS_IAC
: /* Prev char was IAC */
238 if (c
== IAC
) { /* IAC IAC -> one IAC */
240 G
.telstate
= TS_COPY
;
246 G
.telstate
= TS_SUB1
;
255 /* DATA MARK must be added later */
257 G
.telstate
= TS_COPY
;
261 case TS_OPT
: /* Prev chars were IAC WILL/WONT/DO/DONT */
263 G
.telstate
= TS_COPY
;
266 case TS_SUB1
: /* Subnegotiation */
267 case TS_SUB2
: /* Subnegotiation */
268 subneg(c
); /* can change G.telstate */
273 if (G
.telstate
!= TS_NORMAL
) {
274 /* We had some IACs, or CR */
277 if (G
.telstate
== TS_COPY
) /* we aren't in the middle of IAC */
278 G
.telstate
= TS_NORMAL
;
283 full_write(STDOUT_FILENO
, G
.buf
, len
);
286 static void put_iac(int c
)
288 G
.iacbuf
[G
.iaclen
++] = c
;
291 static void put_iac2(byte wwdd
, byte c
)
293 if (G
.iaclen
+ 3 > IACBUFSIZE
)
301 #if ENABLE_FEATURE_TELNET_TTYPE
302 static void put_iac_subopt(byte c
, char *str
)
304 int len
= strlen(str
) + 6; // ( 2 + 1 + 1 + strlen + 2 )
306 if (G
.iaclen
+ len
> IACBUFSIZE
)
322 #if ENABLE_FEATURE_TELNET_AUTOLOGIN
323 static void put_iac_subopt_autologin(void)
325 int len
= strlen(G
.autologin
) + 6; // (2 + 1 + 1 + strlen + 2)
326 const char *p
= "USER";
328 if (G
.iaclen
+ len
> IACBUFSIZE
)
333 put_iac(TELOPT_NEW_ENVIRON
);
335 put_iac(NEW_ENV_VAR
);
340 put_iac(NEW_ENV_VALUE
);
351 #if ENABLE_FEATURE_AUTOWIDTH
352 static void put_iac_naws(byte c
, int x
, int y
)
354 if (G
.iaclen
+ 9 > IACBUFSIZE
)
361 put_iac((x
>> 8) & 0xff);
363 put_iac((y
>> 8) & 0xff);
371 static char const escapecharis
[] ALIGN1
= "\r\nEscape character is ";
373 static void setConMode(void)
375 if (G
.telflags
& UF_ECHO
) {
376 if (G
.charmode
== CHM_TRY
) {
378 printf("\r\nEntering character mode%s'^]'.\r\n", escapecharis
);
382 if (G
.charmode
!= CHM_OFF
) {
383 G
.charmode
= CHM_OFF
;
384 printf("\r\nEntering line mode%s'^C'.\r\n", escapecharis
);
390 static void will_charmode(void)
392 G
.charmode
= CHM_TRY
;
393 G
.telflags
|= (UF_ECHO
| UF_SGA
);
396 put_iac2(DO
, TELOPT_ECHO
);
397 put_iac2(DO
, TELOPT_SGA
);
401 static void do_linemode(void)
403 G
.charmode
= CHM_TRY
;
404 G
.telflags
&= ~(UF_ECHO
| UF_SGA
);
407 put_iac2(DONT
, TELOPT_ECHO
);
408 put_iac2(DONT
, TELOPT_SGA
);
412 static void to_notsup(char c
)
414 if (G
.telwish
== WILL
)
416 else if (G
.telwish
== DO
)
420 static void to_echo(void)
422 /* if server requests ECHO, don't agree */
423 if (G
.telwish
== DO
) {
424 put_iac2(WONT
, TELOPT_ECHO
);
427 if (G
.telwish
== DONT
)
430 if (G
.telflags
& UF_ECHO
) {
431 if (G
.telwish
== WILL
)
433 } else if (G
.telwish
== WONT
)
436 if (G
.charmode
!= CHM_OFF
)
437 G
.telflags
^= UF_ECHO
;
439 if (G
.telflags
& UF_ECHO
)
440 put_iac2(DO
, TELOPT_ECHO
);
442 put_iac2(DONT
, TELOPT_ECHO
);
445 full_write1_str("\r\n"); /* sudden modec */
448 static void to_sga(void)
450 /* daemon always sends will/wont, client do/dont */
452 if (G
.telflags
& UF_SGA
) {
453 if (G
.telwish
== WILL
)
455 } else if (G
.telwish
== WONT
)
458 G
.telflags
^= UF_SGA
; /* toggle */
459 if (G
.telflags
& UF_SGA
)
460 put_iac2(DO
, TELOPT_SGA
);
462 put_iac2(DONT
, TELOPT_SGA
);
465 #if ENABLE_FEATURE_TELNET_TTYPE
466 static void to_ttype(void)
468 /* Tell server we will (or won't) do TTYPE */
470 put_iac2(WILL
, TELOPT_TTYPE
);
472 put_iac2(WONT
, TELOPT_TTYPE
);
476 #if ENABLE_FEATURE_TELNET_AUTOLOGIN
477 static void to_new_environ(void)
479 /* Tell server we will (or will not) do AUTOLOGIN */
481 put_iac2(WILL
, TELOPT_NEW_ENVIRON
);
483 put_iac2(WONT
, TELOPT_NEW_ENVIRON
);
487 #if ENABLE_FEATURE_AUTOWIDTH
488 static void to_naws(void)
490 /* Tell server we will do NAWS */
491 put_iac2(WILL
, TELOPT_NAWS
);
495 static void telopt(byte c
)
502 #if ENABLE_FEATURE_TELNET_TTYPE
506 #if ENABLE_FEATURE_TELNET_AUTOLOGIN
507 case TELOPT_NEW_ENVIRON
:
508 to_new_environ(); break;
510 #if ENABLE_FEATURE_AUTOWIDTH
513 put_iac_naws(c
, G
.win_width
, G
.win_height
);
522 /* subnegotiation -- ignore all (except TTYPE,NAWS) */
523 static void subneg(byte c
)
525 switch (G
.telstate
) {
528 G
.telstate
= TS_SUB2
;
529 #if ENABLE_FEATURE_TELNET_TTYPE
531 if (c
== TELOPT_TTYPE
&& G
.ttype
)
532 put_iac_subopt(TELOPT_TTYPE
, G
.ttype
);
534 #if ENABLE_FEATURE_TELNET_AUTOLOGIN
536 if (c
== TELOPT_NEW_ENVIRON
&& G
.autologin
)
537 put_iac_subopt_autologin();
542 G
.telstate
= TS_COPY
;
545 G
.telstate
= TS_SUB1
;
550 static void rawmode(void)
553 tcsetattr(0, TCSADRAIN
, &G
.termios_raw
);
556 static void cookmode(void)
559 tcsetattr(0, TCSADRAIN
, &G
.termios_def
);
562 int telnet_main(int argc
, char **argv
) MAIN_EXTERNALLY_VISIBLE
;
563 int telnet_main(int argc UNUSED_PARAM
, char **argv
)
568 struct pollfd ufds
[2];
572 #if ENABLE_FEATURE_AUTOWIDTH
573 get_terminal_width_height(0, &G
.win_width
, &G
.win_height
);
576 #if ENABLE_FEATURE_TELNET_TTYPE
577 G
.ttype
= getenv("TERM");
580 if (tcgetattr(0, &G
.termios_def
) >= 0) {
582 G
.termios_raw
= G
.termios_def
;
583 cfmakeraw(&G
.termios_raw
);
586 #if ENABLE_FEATURE_TELNET_AUTOLOGIN
587 if (1 & getopt32(argv
, "al:", &G
.autologin
))
588 G
.autologin
= getenv("USER");
596 port
= bb_lookup_port(*argv
? *argv
++ : "telnet", "tcp", 23);
597 if (*argv
) /* extra params?? */
600 xmove_fd(create_and_connect_stream_or_die(host
, port
), netfd
);
602 setsockopt(netfd
, SOL_SOCKET
, SO_KEEPALIVE
, &const_int_1
, sizeof(const_int_1
));
604 signal(SIGINT
, record_signo
);
606 ufds
[0].fd
= STDIN_FILENO
;
607 ufds
[0].events
= POLLIN
;
609 ufds
[1].events
= POLLIN
;
612 if (poll(ufds
, 2, -1) < 0) {
613 /* error, ignore and/or log something, bay go to loop */
621 // FIXME: reads can block. Need full bidirectional buffering.
623 if (ufds
[0].revents
) {
624 len
= safe_read(STDIN_FILENO
, G
.buf
, DATABUFSIZE
);
626 doexit(EXIT_SUCCESS
);
627 TRACE(0, ("Read con: %d\n", len
));
628 handle_net_output(len
);
631 if (ufds
[1].revents
) {
632 len
= safe_read(netfd
, G
.buf
, DATABUFSIZE
);
634 full_write1_str("Connection closed by foreign host\r\n");
635 doexit(EXIT_FAILURE
);
637 TRACE(0, ("Read netfd (%d): %d\n", netfd
, len
));
638 handle_net_input(len
);