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>
31 #include <arpa/telnet.h>
32 #include <sys/types.h>
33 #include <sys/socket.h>
34 #include <netinet/in.h>
42 #include <arpa/inet.h> /* for inet_ntoa()... */
43 #define TRACE(x, y) do { if (x) printf y; } while (0)
48 #define DATABUFSIZE 128
49 #define IACBUFSIZE 128
66 #define WriteCS(fd, str) write(fd, str, sizeof str -1)
68 typedef unsigned char byte;
70 /* use globals to reduce size ??? */ /* test this hypothesis later */
71 static struct Globalvars {
72 int netfd; /* console fd:s are 0 and 1 (and 2) */
73 /* same buffer used both for network and console read/write */
74 char buf[DATABUFSIZE]; /* allocating so static size is smaller */
75 byte telstate; /* telnet negotiation state from network input */
76 byte telwish; /* DO, DONT, WILL, WONT */
81 /* buffer to handle telnet negotiations */
82 char iacbuf[IACBUFSIZE];
83 short iaclen; /* could even use byte */
84 struct termios termios_def;
85 struct termios termios_raw;
88 #define xUSE_GLOBALVAR_PTR /* xUSE... -> don't use :D (makes smaller code) */
90 #ifdef USE_GLOBALVAR_PTR
91 struct Globalvars * Gptr;
95 static inline void iacflush(void)
97 write(G.netfd, G.iacbuf, G.iaclen);
101 /* Function prototypes */
102 static void rawmode(void);
103 static void cookmode(void);
104 static void do_linemode(void);
105 static void will_charmode(void);
106 static void telopt(byte c);
107 static int subneg(byte c);
112 #ifdef CONFIG_FEATURE_TELNET_TTYPE
116 #ifdef CONFIG_FEATURE_TELNET_AUTOLOGIN
117 static const char *autologin;
120 #ifdef CONFIG_FEATURE_AUTOWIDTH
121 static int win_width, win_height;
124 static void doexit(int ev)
130 static void conescape(void)
134 if (G.gotsig) /* came from line mode... go raw */
137 WriteCS(1, "\r\nConsole escape. Commands are:\r\n\n"
138 " l go to line mode\r\n"
139 " c go to character mode\r\n"
140 " z suspend telnet\r\n"
141 " e exit telnet\r\n");
143 if (read(0, &b, 1) <= 0)
171 WriteCS(1, "continuing...\r\n");
180 static void handlenetoutput(int len)
182 /* here we could do smart tricks how to handle 0xFF:s in output
183 * stream like writing twice every sequence of FF:s (thus doing
184 * many write()s. But I think interactive telnet application does
185 * not need to be 100% 8-bit clean, so changing every 0xff:s to
188 * 2002-mar-21, Przemyslaw Czerpak (druzus@polbox.com)
190 * first - I cannot use programs like sz/rz
191 * second - the 0x0D is sent as one character and if the next
192 * char is 0x0A then it's eaten by a server side.
193 * third - whay doy you have to make 'many write()s'?
194 * I don't understand.
195 * So I implemented it. It's realy useful for me. I hope that
196 * others people will find it interesting to.
200 byte * p = (byte*)G.buf;
201 byte outbuf[4*DATABUFSIZE];
203 for (i = len, j = 0; i > 0; i--, p++)
217 write(G.netfd, outbuf, j);
221 static void handlenetinput(int len)
226 for (i = 0; i < len; i++)
230 if (G.telstate == 0) /* most of the time state == 0 */
249 if (c == IAC) /* IAC IAC -> 0xFF */
259 G.telstate = TS_SUB1;
269 G.telstate = TS_0; /* DATA MARK must be added later */
272 case TS_OPT: /* WILL, WONT, DO, DONT */
276 case TS_SUB1: /* Subnegotiation */
277 case TS_SUB2: /* Subnegotiation */
285 if (G.iaclen) iacflush();
286 if (G.telstate == TS_0) G.telstate = 0;
292 write(1, G.buf, len);
296 /* ******************************* */
298 static inline void putiac(int c)
300 G.iacbuf[G.iaclen++] = c;
304 static void putiac2(byte wwdd, byte c)
306 if (G.iaclen + 3 > IACBUFSIZE)
315 static void putiac1(byte c)
317 if (G.iaclen + 2 > IACBUFSIZE)
325 #ifdef CONFIG_FEATURE_TELNET_TTYPE
326 static void putiac_subopt(byte c, char *str)
328 int len = strlen(str) + 6; // ( 2 + 1 + 1 + strlen + 2 )
330 if (G.iaclen + len > IACBUFSIZE)
346 #ifdef CONFIG_FEATURE_TELNET_AUTOLOGIN
347 static void putiac_subopt_autologin(void)
349 int len = strlen(autologin) + 6; // (2 + 1 + 1 + strlen + 2)
352 if (G.iaclen + len > IACBUFSIZE)
357 putiac(TELOPT_NEW_ENVIRON);
364 putiac(NEW_ENV_VALUE);
367 putiac(*autologin++);
374 #ifdef CONFIG_FEATURE_AUTOWIDTH
375 static void putiac_naws(byte c, int x, int y)
377 if (G.iaclen + 9 > IACBUFSIZE)
384 putiac((x >> 8) & 0xff);
386 putiac((y >> 8) & 0xff);
394 /* void putiacstring (subneg strings) */
396 /* ******************************* */
398 static char const escapecharis[] = "\r\nEscape character is ";
400 static void setConMode(void)
402 if (G.telflags & UF_ECHO)
404 if (G.charmode == CHM_TRY) {
406 printf("\r\nEntering character mode%s'^]'.\r\n", escapecharis);
412 if (G.charmode != CHM_OFF) {
413 G.charmode = CHM_OFF;
414 printf("\r\nEntering line mode%s'^C'.\r\n", escapecharis);
420 /* ******************************* */
422 static void will_charmode(void)
424 G.charmode = CHM_TRY;
425 G.telflags |= (UF_ECHO | UF_SGA);
428 putiac2(DO, TELOPT_ECHO);
429 putiac2(DO, TELOPT_SGA);
433 static void do_linemode(void)
435 G.charmode = CHM_TRY;
436 G.telflags &= ~(UF_ECHO | UF_SGA);
439 putiac2(DONT, TELOPT_ECHO);
440 putiac2(DONT, TELOPT_SGA);
444 /* ******************************* */
446 static inline void to_notsup(char c)
448 if (G.telwish == WILL) putiac2(DONT, c);
449 else if (G.telwish == DO) putiac2(WONT, c);
452 static inline void to_echo(void)
454 /* if server requests ECHO, don't agree */
455 if (G.telwish == DO) { putiac2(WONT, TELOPT_ECHO); return; }
456 else if (G.telwish == DONT) return;
458 if (G.telflags & UF_ECHO)
460 if (G.telwish == WILL)
464 if (G.telwish == WONT)
467 if (G.charmode != CHM_OFF)
468 G.telflags ^= UF_ECHO;
470 if (G.telflags & UF_ECHO)
471 putiac2(DO, TELOPT_ECHO);
473 putiac2(DONT, TELOPT_ECHO);
476 WriteCS(1, "\r\n"); /* sudden modec */
479 static inline void to_sga(void)
481 /* daemon always sends will/wont, client do/dont */
483 if (G.telflags & UF_SGA)
485 if (G.telwish == WILL)
489 if (G.telwish == WONT)
492 if ((G.telflags ^= UF_SGA) & UF_SGA) /* toggle */
493 putiac2(DO, TELOPT_SGA);
495 putiac2(DONT, TELOPT_SGA);
500 #ifdef CONFIG_FEATURE_TELNET_TTYPE
501 static inline void to_ttype(void)
503 /* Tell server we will (or won't) do TTYPE */
506 putiac2(WILL, TELOPT_TTYPE);
508 putiac2(WONT, TELOPT_TTYPE);
514 #ifdef CONFIG_FEATURE_TELNET_AUTOLOGIN
515 static inline void to_new_environ(void)
517 /* Tell server we will (or will not) do AUTOLOGIN */
520 putiac2(WILL, TELOPT_NEW_ENVIRON);
522 putiac2(WONT, TELOPT_NEW_ENVIRON);
528 #ifdef CONFIG_FEATURE_AUTOWIDTH
529 static inline void to_naws(void)
531 /* Tell server we will do NAWS */
532 putiac2(WILL, TELOPT_NAWS);
537 static void telopt(byte c)
541 case TELOPT_ECHO: to_echo(); break;
542 case TELOPT_SGA: to_sga(); break;
543 #ifdef CONFIG_FEATURE_TELNET_TTYPE
544 case TELOPT_TTYPE: to_ttype();break;
546 #ifdef CONFIG_FEATURE_TELNET_AUTOLOGIN
547 case TELOPT_NEW_ENVIRON: to_new_environ(); break;
549 #ifdef CONFIG_FEATURE_AUTOWIDTH
550 case TELOPT_NAWS: to_naws();
551 putiac_naws(c, win_width, win_height);
554 default: to_notsup(c);
560 /* ******************************* */
562 /* subnegotiation -- ignore all (except TTYPE,NAWS) */
564 static int subneg(byte c)
570 G.telstate = TS_SUB2;
571 #ifdef CONFIG_FEATURE_TELNET_TTYPE
573 if (c == TELOPT_TTYPE)
574 putiac_subopt(TELOPT_TTYPE,ttype);
576 #ifdef CONFIG_FEATURE_TELNET_AUTOLOGIN
578 if (c == TELOPT_NEW_ENVIRON)
579 putiac_subopt_autologin();
585 G.telstate = TS_SUB1;
591 /* ******************************* */
593 static void fgotsig(int sig)
599 static void rawmode(void)
601 if (G.do_termios) tcsetattr(0, TCSADRAIN, &G.termios_raw);
604 static void cookmode(void)
606 if (G.do_termios) tcsetattr(0, TCSADRAIN, &G.termios_def);
609 int telnet_main(int argc, char** argv)
612 struct sockaddr_in s_in;
614 struct pollfd ufds[2];
620 #ifdef CONFIG_FEATURE_AUTOWIDTH
621 get_terminal_width_height(0, &win_width, &win_height);
624 #ifdef CONFIG_FEATURE_TELNET_TTYPE
625 ttype = getenv("TERM");
628 memset(&G, 0, sizeof G);
630 if (tcgetattr(0, &G.termios_def) >= 0) {
633 G.termios_raw = G.termios_def;
634 cfmakeraw(&G.termios_raw);
640 #ifdef CONFIG_FEATURE_TELNET_AUTOLOGIN
641 if (1 & bb_getopt_ulflags(argc, argv, "al:", &autologin))
642 autologin = getenv("USER");
645 bb_lookup_host(&s_in, argv[optind++]);
646 s_in.sin_port = bb_lookup_port((optind < argc) ? argv[optind++] :
647 "telnet", "tcp", 23);
653 bb_lookup_host(&s_in, argv[1]);
654 s_in.sin_port = bb_lookup_port((argc == 3) ? argv[2] : "telnet", "tcp", 23);
657 G.netfd = xconnect(&s_in);
659 setsockopt(G.netfd, SOL_SOCKET, SO_KEEPALIVE, &one, sizeof one);
661 signal(SIGINT, fgotsig);
664 ufds[0].fd = 0; ufds[1].fd = G.netfd;
665 ufds[0].events = ufds[1].events = POLLIN;
669 FD_SET(G.netfd, &readfds);
676 fd_set rfds = readfds;
678 switch (select(maxfd, &rfds, NULL, NULL, NULL))
680 switch (poll(ufds, 2, -1))
686 /* error, ignore and/or log something, bay go to loop */
695 if (ufds[0].revents) /* well, should check POLLIN, but ... */
697 if (FD_ISSET(0, &rfds))
700 len = read(0, G.buf, DATABUFSIZE);
705 TRACE(0, ("Read con: %d\n", len));
707 handlenetoutput(len);
711 if (ufds[1].revents) /* well, should check POLLIN, but ... */
713 if (FD_ISSET(G.netfd, &rfds))
716 len = read(G.netfd, G.buf, DATABUFSIZE);
720 WriteCS(1, "Connection closed by foreign host.\r\n");
723 TRACE(0, ("Read netfd (%d): %d\n", G.netfd, len));