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 #include <arpa/inet.h> /* for inet_ntoa()... */
31 #define TRACE(x, y) do { if (x) printf y; } while (0)
36 #define DATABUFSIZE 128
37 #define IACBUFSIZE 128
54 #define WriteCS(fd, str) write(fd, str, sizeof str -1)
56 typedef unsigned char byte;
58 /* use globals to reduce size ??? */ /* test this hypothesis later */
59 static struct Globalvars {
60 int netfd; /* console fd:s are 0 and 1 (and 2) */
61 /* same buffer used both for network and console read/write */
62 char buf[DATABUFSIZE]; /* allocating so static size is smaller */
63 byte telstate; /* telnet negotiation state from network input */
64 byte telwish; /* DO, DONT, WILL, WONT */
69 /* buffer to handle telnet negotiations */
70 char iacbuf[IACBUFSIZE];
71 short iaclen; /* could even use byte */
72 struct termios termios_def;
73 struct termios termios_raw;
76 #define xUSE_GLOBALVAR_PTR /* xUSE... -> don't use :D (makes smaller code) */
78 #ifdef USE_GLOBALVAR_PTR
79 struct Globalvars * Gptr;
83 static void iacflush(void)
85 write(G.netfd, G.iacbuf, G.iaclen);
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);
98 #ifdef CONFIG_FEATURE_TELNET_TTYPE
102 #ifdef CONFIG_FEATURE_TELNET_AUTOLOGIN
103 static const char *autologin;
106 #ifdef CONFIG_FEATURE_AUTOWIDTH
107 static int win_width, win_height;
110 static void doexit(int ev)
116 static void conescape(void)
120 if (G.gotsig) /* came from line mode... go raw */
123 WriteCS(1, "\r\nConsole escape. Commands are:\r\n\n"
124 " l go to line mode\r\n"
125 " c go to character mode\r\n"
126 " z suspend telnet\r\n"
127 " e exit telnet\r\n");
129 if (read(0, &b, 1) <= 0)
157 WriteCS(1, "continuing...\r\n");
166 static void handlenetoutput(int len)
168 /* here we could do smart tricks how to handle 0xFF:s in output
169 * stream like writing twice every sequence of FF:s (thus doing
170 * many write()s. But I think interactive telnet application does
171 * not need to be 100% 8-bit clean, so changing every 0xff:s to
174 * 2002-mar-21, Przemyslaw Czerpak (druzus@polbox.com)
176 * first - I cannot use programs like sz/rz
177 * second - the 0x0D is sent as one character and if the next
178 * char is 0x0A then it's eaten by a server side.
179 * third - whay doy you have to make 'many write()s'?
180 * I don't understand.
181 * So I implemented it. It's realy useful for me. I hope that
182 * others people will find it interesting to.
186 byte * p = (byte*)G.buf;
187 byte outbuf[4*DATABUFSIZE];
189 for (i = len, j = 0; i > 0; i--, p++)
203 write(G.netfd, outbuf, j);
207 static void handlenetinput(int len)
212 for (i = 0; i < len; i++)
216 if (G.telstate == 0) /* most of the time state == 0 */
235 if (c == IAC) /* IAC IAC -> 0xFF */
245 G.telstate = TS_SUB1;
255 G.telstate = TS_0; /* DATA MARK must be added later */
258 case TS_OPT: /* WILL, WONT, DO, DONT */
262 case TS_SUB1: /* Subnegotiation */
263 case TS_SUB2: /* Subnegotiation */
271 if (G.iaclen) iacflush();
272 if (G.telstate == TS_0) G.telstate = 0;
278 write(1, G.buf, len);
282 /* ******************************* */
284 static void putiac(int c)
286 G.iacbuf[G.iaclen++] = c;
290 static void putiac2(byte wwdd, byte c)
292 if (G.iaclen + 3 > IACBUFSIZE)
300 #ifdef CONFIG_FEATURE_TELNET_TTYPE
301 static void putiac_subopt(byte c, char *str)
303 int len = strlen(str) + 6; // ( 2 + 1 + 1 + strlen + 2 )
305 if (G.iaclen + len > IACBUFSIZE)
321 #ifdef CONFIG_FEATURE_TELNET_AUTOLOGIN
322 static void putiac_subopt_autologin(void)
324 int len = strlen(autologin) + 6; // (2 + 1 + 1 + strlen + 2)
327 if (G.iaclen + len > IACBUFSIZE)
332 putiac(TELOPT_NEW_ENVIRON);
339 putiac(NEW_ENV_VALUE);
342 putiac(*autologin++);
349 #ifdef CONFIG_FEATURE_AUTOWIDTH
350 static void putiac_naws(byte c, int x, int y)
352 if (G.iaclen + 9 > IACBUFSIZE)
359 putiac((x >> 8) & 0xff);
361 putiac((y >> 8) & 0xff);
369 /* void putiacstring (subneg strings) */
371 /* ******************************* */
373 static char const escapecharis[] = "\r\nEscape character is ";
375 static void setConMode(void)
377 if (G.telflags & UF_ECHO)
379 if (G.charmode == CHM_TRY) {
381 printf("\r\nEntering character mode%s'^]'.\r\n", escapecharis);
387 if (G.charmode != CHM_OFF) {
388 G.charmode = CHM_OFF;
389 printf("\r\nEntering line mode%s'^C'.\r\n", escapecharis);
395 /* ******************************* */
397 static void will_charmode(void)
399 G.charmode = CHM_TRY;
400 G.telflags |= (UF_ECHO | UF_SGA);
403 putiac2(DO, TELOPT_ECHO);
404 putiac2(DO, TELOPT_SGA);
408 static void do_linemode(void)
410 G.charmode = CHM_TRY;
411 G.telflags &= ~(UF_ECHO | UF_SGA);
414 putiac2(DONT, TELOPT_ECHO);
415 putiac2(DONT, TELOPT_SGA);
419 /* ******************************* */
421 static void to_notsup(char c)
423 if (G.telwish == WILL) putiac2(DONT, c);
424 else if (G.telwish == DO) putiac2(WONT, c);
427 static void to_echo(void)
429 /* if server requests ECHO, don't agree */
430 if (G.telwish == DO) { putiac2(WONT, TELOPT_ECHO); return; }
431 else if (G.telwish == DONT) return;
433 if (G.telflags & UF_ECHO)
435 if (G.telwish == WILL)
439 if (G.telwish == WONT)
442 if (G.charmode != CHM_OFF)
443 G.telflags ^= UF_ECHO;
445 if (G.telflags & UF_ECHO)
446 putiac2(DO, TELOPT_ECHO);
448 putiac2(DONT, TELOPT_ECHO);
451 WriteCS(1, "\r\n"); /* sudden modec */
454 static void to_sga(void)
456 /* daemon always sends will/wont, client do/dont */
458 if (G.telflags & UF_SGA)
460 if (G.telwish == WILL)
464 if (G.telwish == WONT)
467 if ((G.telflags ^= UF_SGA) & UF_SGA) /* toggle */
468 putiac2(DO, TELOPT_SGA);
470 putiac2(DONT, TELOPT_SGA);
475 #ifdef CONFIG_FEATURE_TELNET_TTYPE
476 static void to_ttype(void)
478 /* Tell server we will (or won't) do TTYPE */
481 putiac2(WILL, TELOPT_TTYPE);
483 putiac2(WONT, TELOPT_TTYPE);
489 #ifdef CONFIG_FEATURE_TELNET_AUTOLOGIN
490 static void to_new_environ(void)
492 /* Tell server we will (or will not) do AUTOLOGIN */
495 putiac2(WILL, TELOPT_NEW_ENVIRON);
497 putiac2(WONT, TELOPT_NEW_ENVIRON);
503 #ifdef CONFIG_FEATURE_AUTOWIDTH
504 static void to_naws(void)
506 /* Tell server we will do NAWS */
507 putiac2(WILL, TELOPT_NAWS);
512 static void telopt(byte c)
516 case TELOPT_ECHO: to_echo(); break;
517 case TELOPT_SGA: to_sga(); break;
518 #ifdef CONFIG_FEATURE_TELNET_TTYPE
519 case TELOPT_TTYPE: to_ttype();break;
521 #ifdef CONFIG_FEATURE_TELNET_AUTOLOGIN
522 case TELOPT_NEW_ENVIRON: to_new_environ(); break;
524 #ifdef CONFIG_FEATURE_AUTOWIDTH
525 case TELOPT_NAWS: to_naws();
526 putiac_naws(c, win_width, win_height);
529 default: to_notsup(c);
535 /* ******************************* */
537 /* subnegotiation -- ignore all (except TTYPE,NAWS) */
539 static int subneg(byte c)
545 G.telstate = TS_SUB2;
546 #ifdef CONFIG_FEATURE_TELNET_TTYPE
548 if (c == TELOPT_TTYPE)
549 putiac_subopt(TELOPT_TTYPE,ttype);
551 #ifdef CONFIG_FEATURE_TELNET_AUTOLOGIN
553 if (c == TELOPT_NEW_ENVIRON)
554 putiac_subopt_autologin();
560 G.telstate = TS_SUB1;
566 /* ******************************* */
568 static void fgotsig(int sig)
574 static void rawmode(void)
576 if (G.do_termios) tcsetattr(0, TCSADRAIN, &G.termios_raw);
579 static void cookmode(void)
581 if (G.do_termios) tcsetattr(0, TCSADRAIN, &G.termios_def);
584 int telnet_main(int argc, char** argv)
590 struct pollfd ufds[2];
596 #ifdef CONFIG_FEATURE_AUTOWIDTH
597 get_terminal_width_height(0, &win_width, &win_height);
600 #ifdef CONFIG_FEATURE_TELNET_TTYPE
601 ttype = getenv("TERM");
604 /* memset(&G, 0, sizeof G); - already is */
606 if (tcgetattr(0, &G.termios_def) >= 0) {
608 G.termios_raw = G.termios_def;
609 cfmakeraw(&G.termios_raw);
615 #ifdef CONFIG_FEATURE_TELNET_AUTOLOGIN
616 if (1 & getopt32(argc, argv, "al:", &autologin))
617 autologin = getenv("USER");
625 port = bb_lookup_port(*argv ? *argv++ : "telnet", "tcp", 23);
626 if (*argv) /* extra params?? */
629 G.netfd = create_and_connect_stream_or_die(host, port);
631 setsockopt(G.netfd, SOL_SOCKET, SO_KEEPALIVE, &const_int_1, sizeof(const_int_1));
633 signal(SIGINT, fgotsig);
636 ufds[0].fd = 0; ufds[1].fd = G.netfd;
637 ufds[0].events = ufds[1].events = POLLIN;
641 FD_SET(G.netfd, &readfds);
647 fd_set rfds = readfds;
649 switch (select(maxfd, &rfds, NULL, NULL, NULL))
651 switch (poll(ufds, 2, -1))
657 /* error, ignore and/or log something, bay go to loop */
666 if (ufds[0].revents) /* well, should check POLLIN, but ... */
668 if (FD_ISSET(0, &rfds))
671 len = read(0, G.buf, DATABUFSIZE);
676 TRACE(0, ("Read con: %d\n", len));
678 handlenetoutput(len);
682 if (ufds[1].revents) /* well, should check POLLIN, but ... */
684 if (FD_ISSET(G.netfd, &rfds))
687 len = read(G.netfd, G.buf, DATABUFSIZE);
690 WriteCS(1, "Connection closed by foreign host.\r\n");
693 TRACE(0, ("Read netfd (%d): %d\n", G.netfd, len));