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;
57 int netfd; /* console fd:s are 0 and 1 (and 2) */
58 short iaclen; /* could even use byte */
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 int 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;
82 #define G (*(struct globals*)bb_common_bufsiz1)
85 /* Function prototypes */
86 static void rawmode(void);
87 static void cookmode(void);
88 static void do_linemode(void);
89 static void will_charmode(void);
90 static void telopt(byte c);
91 static int subneg(byte c);
93 static void iacflush(void)
95 write(G.netfd, G.iacbuf, G.iaclen);
99 #define write_str(fd, str) write(fd, str, sizeof(str) - 1)
101 static void doexit(int ev)
107 static void conescape(void)
111 if (G.gotsig) /* came from line mode... go raw */
114 write_str(1, "\r\nConsole escape. Commands are:\r\n\n"
115 " l go to line mode\r\n"
116 " c go to character mode\r\n"
117 " z suspend telnet\r\n"
118 " e exit telnet\r\n");
120 if (read(0, &b, 1) <= 0)
145 write_str(1, "continuing...\r\n");
155 static void handlenetoutput(int len)
157 /* here we could do smart tricks how to handle 0xFF:s in output
158 * stream like writing twice every sequence of FF:s (thus doing
159 * many write()s. But I think interactive telnet application does
160 * not need to be 100% 8-bit clean, so changing every 0xff:s to
163 * 2002-mar-21, Przemyslaw Czerpak (druzus@polbox.com)
165 * first - I cannot use programs like sz/rz
166 * second - the 0x0D is sent as one character and if the next
167 * char is 0x0A then it's eaten by a server side.
168 * third - whay doy you have to make 'many write()s'?
169 * I don't understand.
170 * So I implemented it. It's realy useful for me. I hope that
171 * others people will find it interesting to.
175 byte * p = (byte*)G.buf;
176 byte outbuf[4*DATABUFSIZE];
178 for (i = len, j = 0; i > 0; i--, p++)
192 write(G.netfd, outbuf, j);
195 static void handlenetinput(int len)
200 for (i = 0; i < len; i++)
204 if (G.telstate == 0) /* most of the time state == 0 */
223 if (c == IAC) /* IAC IAC -> 0xFF */
233 G.telstate = TS_SUB1;
243 G.telstate = TS_0; /* DATA MARK must be added later */
246 case TS_OPT: /* WILL, WONT, DO, DONT */
250 case TS_SUB1: /* Subnegotiation */
251 case TS_SUB2: /* Subnegotiation */
259 if (G.iaclen) iacflush();
260 if (G.telstate == TS_0) G.telstate = 0;
266 write(1, G.buf, len);
269 static void putiac(int c)
271 G.iacbuf[G.iaclen++] = c;
274 static void putiac2(byte wwdd, byte c)
276 if (G.iaclen + 3 > IACBUFSIZE)
284 #if ENABLE_FEATURE_TELNET_TTYPE
285 static void putiac_subopt(byte c, char *str)
287 int len = strlen(str) + 6; // ( 2 + 1 + 1 + strlen + 2 )
289 if (G.iaclen + len > IACBUFSIZE)
305 #if ENABLE_FEATURE_TELNET_AUTOLOGIN
306 static void putiac_subopt_autologin(void)
308 int len = strlen(G.autologin) + 6; // (2 + 1 + 1 + strlen + 2)
309 const char *user = "USER";
311 if (G.iaclen + len > IACBUFSIZE)
316 putiac(TELOPT_NEW_ENVIRON);
323 putiac(NEW_ENV_VALUE);
326 putiac(*G.autologin++);
333 #if ENABLE_FEATURE_AUTOWIDTH
334 static void putiac_naws(byte c, int x, int y)
336 if (G.iaclen + 9 > IACBUFSIZE)
343 putiac((x >> 8) & 0xff);
345 putiac((y >> 8) & 0xff);
353 static char const escapecharis[] = "\r\nEscape character is ";
355 static void setConMode(void)
357 if (G.telflags & UF_ECHO) {
358 if (G.charmode == CHM_TRY) {
360 printf("\r\nEntering character mode%s'^]'.\r\n", escapecharis);
364 if (G.charmode != CHM_OFF) {
365 G.charmode = CHM_OFF;
366 printf("\r\nEntering line mode%s'^C'.\r\n", escapecharis);
372 static void will_charmode(void)
374 G.charmode = CHM_TRY;
375 G.telflags |= (UF_ECHO | UF_SGA);
378 putiac2(DO, TELOPT_ECHO);
379 putiac2(DO, TELOPT_SGA);
383 static void do_linemode(void)
385 G.charmode = CHM_TRY;
386 G.telflags &= ~(UF_ECHO | UF_SGA);
389 putiac2(DONT, TELOPT_ECHO);
390 putiac2(DONT, TELOPT_SGA);
394 static void to_notsup(char c)
396 if (G.telwish == WILL)
398 else if (G.telwish == DO)
402 static void to_echo(void)
404 /* if server requests ECHO, don't agree */
405 if (G.telwish == DO) {
406 putiac2(WONT, TELOPT_ECHO);
409 if (G.telwish == DONT)
412 if (G.telflags & UF_ECHO) {
413 if (G.telwish == WILL)
415 } else if (G.telwish == WONT)
418 if (G.charmode != CHM_OFF)
419 G.telflags ^= UF_ECHO;
421 if (G.telflags & UF_ECHO)
422 putiac2(DO, TELOPT_ECHO);
424 putiac2(DONT, TELOPT_ECHO);
427 write_str(1, "\r\n"); /* sudden modec */
430 static void to_sga(void)
432 /* daemon always sends will/wont, client do/dont */
434 if (G.telflags & UF_SGA) {
435 if (G.telwish == WILL)
437 } else if (G.telwish == WONT)
440 if ((G.telflags ^= UF_SGA) & UF_SGA) /* toggle */
441 putiac2(DO, TELOPT_SGA);
443 putiac2(DONT, TELOPT_SGA);
446 #if ENABLE_FEATURE_TELNET_TTYPE
447 static void to_ttype(void)
449 /* Tell server we will (or won't) do TTYPE */
452 putiac2(WILL, TELOPT_TTYPE);
454 putiac2(WONT, TELOPT_TTYPE);
458 #if ENABLE_FEATURE_TELNET_AUTOLOGIN
459 static void to_new_environ(void)
461 /* Tell server we will (or will not) do AUTOLOGIN */
464 putiac2(WILL, TELOPT_NEW_ENVIRON);
466 putiac2(WONT, TELOPT_NEW_ENVIRON);
470 #if ENABLE_FEATURE_AUTOWIDTH
471 static void to_naws(void)
473 /* Tell server we will do NAWS */
474 putiac2(WILL, TELOPT_NAWS);
478 static void telopt(byte c)
485 #if ENABLE_FEATURE_TELNET_TTYPE
489 #if ENABLE_FEATURE_TELNET_AUTOLOGIN
490 case TELOPT_NEW_ENVIRON:
491 to_new_environ(); break;
493 #if ENABLE_FEATURE_AUTOWIDTH
496 putiac_naws(c, G.win_width, G.win_height);
505 /* subnegotiation -- ignore all (except TTYPE,NAWS) */
506 static int subneg(byte c)
508 switch (G.telstate) {
511 G.telstate = TS_SUB2;
512 #if ENABLE_FEATURE_TELNET_TTYPE
514 if (c == TELOPT_TTYPE)
515 putiac_subopt(TELOPT_TTYPE, G.ttype);
517 #if ENABLE_FEATURE_TELNET_AUTOLOGIN
519 if (c == TELOPT_NEW_ENVIRON)
520 putiac_subopt_autologin();
526 G.telstate = TS_SUB1;
532 static void fgotsig(int sig)
538 static void rawmode(void)
541 tcsetattr(0, TCSADRAIN, &G.termios_raw);
544 static void cookmode(void)
547 tcsetattr(0, TCSADRAIN, &G.termios_def);
550 void BUG_telnet_globals_too_big(void);
552 int telnet_main(int argc, char** argv);
553 int telnet_main(int argc, char** argv)
559 struct pollfd ufds[2];
565 if (sizeof(G) > sizeof(bb_common_bufsiz1))
566 BUG_telnet_globals_too_big();
567 /* memset(&G, 0, sizeof G); - already is */
569 #if ENABLE_FEATURE_AUTOWIDTH
570 get_terminal_width_height(0, &G.win_width, &G.win_height);
573 #if ENABLE_FEATURE_TELNET_TTYPE
574 G.ttype = getenv("TERM");
577 if (tcgetattr(0, &G.termios_def) >= 0) {
579 G.termios_raw = G.termios_def;
580 cfmakeraw(&G.termios_raw);
586 #if ENABLE_FEATURE_TELNET_AUTOLOGIN
587 if (1 & getopt32(argc, 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 G.netfd = create_and_connect_stream_or_die(host, port);
602 setsockopt(G.netfd, SOL_SOCKET, SO_KEEPALIVE, &const_int_1, sizeof(const_int_1));
604 signal(SIGINT, fgotsig);
607 ufds[0].fd = 0; ufds[1].fd = G.netfd;
608 ufds[0].events = ufds[1].events = POLLIN;
612 FD_SET(G.netfd, &readfds);
618 fd_set rfds = readfds;
620 switch (select(maxfd, &rfds, NULL, NULL, NULL))
622 switch (poll(ufds, 2, -1))
628 /* error, ignore and/or log something, bay go to loop */
637 if (ufds[0].revents) /* well, should check POLLIN, but ... */
639 if (FD_ISSET(0, &rfds))
642 len = read(0, G.buf, DATABUFSIZE);
645 TRACE(0, ("Read con: %d\n", len));
646 handlenetoutput(len);
650 if (ufds[1].revents) /* well, should check POLLIN, but ... */
652 if (FD_ISSET(G.netfd, &rfds))
655 len = read(G.netfd, G.buf, DATABUFSIZE);
657 write_str(1, "Connection closed by foreign host\r\n");
660 TRACE(0, ("Read netfd (%d): %d\n", G.netfd, len));