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 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 * Revision 3.1 1994/04/17 11:31:54 too
28 * Modified 2000/06/13 for inclusion into BusyBox by Erik Andersen <andersen@codepoet.org>
29 * Modified 2001/05/07 to add ability to pass TTYPE to remote host by Jim McQuillan
31 * Modified 2004/02/11 to add ability to pass the USER variable to remote host
32 * by Fernando Silveira <swrh@gmx.net>
43 #include <arpa/telnet.h>
44 #include <sys/types.h>
45 #include <sys/socket.h>
46 #include <netinet/in.h>
50 static const int DOTRACE = 1;
54 #include <arpa/inet.h> /* for inet_ntoa()... */
55 #define TRACE(x, y) do { if (x) printf y; } while (0)
67 #define DATABUFSIZE 128
68 #define IACBUFSIZE 128
70 static const int CHM_TRY = 0;
71 static const int CHM_ON = 1;
72 static const int CHM_OFF = 2;
74 static const int UF_ECHO = 0x01;
75 static const int UF_SGA = 0x02;
85 #define WriteCS(fd, str) write(fd, str, sizeof str -1)
87 typedef unsigned char byte;
89 /* use globals to reduce size ??? */ /* test this hypothesis later */
90 static struct Globalvars {
91 int netfd; /* console fd:s are 0 and 1 (and 2) */
92 /* same buffer used both for network and console read/write */
93 char buf[DATABUFSIZE]; /* allocating so static size is smaller */
94 byte telstate; /* telnet negotiation state from network input */
95 byte telwish; /* DO, DONT, WILL, WONT */
99 /* buffer to handle telnet negotiations */
100 char iacbuf[IACBUFSIZE];
101 short iaclen; /* could even use byte */
102 struct termios termios_def;
103 struct termios termios_raw;
106 #define xUSE_GLOBALVAR_PTR /* xUSE... -> don't use :D (makes smaller code) */
108 #ifdef USE_GLOBALVAR_PTR
109 struct Globalvars * Gptr;
113 static inline void iacflush(void)
115 write(G.netfd, G.iacbuf, G.iaclen);
119 /* Function prototypes */
120 static void rawmode(void);
121 static void cookmode(void);
122 static void do_linemode(void);
123 static void will_charmode(void);
124 static void telopt(byte c);
125 static int subneg(byte c);
130 #ifdef CONFIG_FEATURE_TELNET_TTYPE
134 #ifdef CONFIG_FEATURE_TELNET_AUTOLOGIN
135 static const char *autologin;
138 #ifdef CONFIG_FEATURE_AUTOWIDTH
139 static int win_width, win_height;
142 static void doexit(int ev)
148 static void conescape(void)
152 if (G.gotsig) /* came from line mode... go raw */
155 WriteCS(1, "\r\nConsole escape. Commands are:\r\n\n"
156 " l go to line mode\r\n"
157 " c go to character mode\r\n"
158 " z suspend telnet\r\n"
159 " e exit telnet\r\n");
161 if (read(0, &b, 1) <= 0)
189 WriteCS(1, "continuing...\r\n");
198 static void handlenetoutput(int len)
200 /* here we could do smart tricks how to handle 0xFF:s in output
201 * stream like writing twice every sequence of FF:s (thus doing
202 * many write()s. But I think interactive telnet application does
203 * not need to be 100% 8-bit clean, so changing every 0xff:s to
206 * 2002-mar-21, Przemyslaw Czerpak (druzus@polbox.com)
208 * first - I cannot use programs like sz/rz
209 * second - the 0x0D is sent as one character and if the next
210 * char is 0x0A then it's eaten by a server side.
211 * third - whay doy you have to make 'many write()s'?
212 * I don't understand.
213 * So I implemented it. It's realy useful for me. I hope that
214 * others people will find it interesting to.
219 byte outbuf[4*DATABUFSIZE];
221 for (i = len, j = 0; i > 0; i--, p++)
235 write(G.netfd, outbuf, j);
239 static void handlenetinput(int len)
244 for (i = 0; i < len; i++)
248 if (G.telstate == 0) /* most of the time state == 0 */
267 if (c == IAC) /* IAC IAC -> 0xFF */
277 G.telstate = TS_SUB1;
287 G.telstate = TS_0; /* DATA MARK must be added later */
290 case TS_OPT: /* WILL, WONT, DO, DONT */
294 case TS_SUB1: /* Subnegotiation */
295 case TS_SUB2: /* Subnegotiation */
303 if (G.iaclen) iacflush();
304 if (G.telstate == TS_0) G.telstate = 0;
310 write(1, G.buf, len);
314 /* ******************************* */
316 static inline void putiac(int c)
318 G.iacbuf[G.iaclen++] = c;
322 static void putiac2(byte wwdd, byte c)
324 if (G.iaclen + 3 > IACBUFSIZE)
333 static void putiac1(byte c)
335 if (G.iaclen + 2 > IACBUFSIZE)
343 #ifdef CONFIG_FEATURE_TELNET_TTYPE
344 static void putiac_subopt(byte c, char *str)
346 int len = strlen(str) + 6; // ( 2 + 1 + 1 + strlen + 2 )
348 if (G.iaclen + len > IACBUFSIZE)
364 #ifdef CONFIG_FEATURE_TELNET_AUTOLOGIN
365 static void putiac_subopt_autologin(void)
367 int len = strlen(autologin) + 6; // (2 + 1 + 1 + strlen + 2)
370 if (G.iaclen + len > IACBUFSIZE)
375 putiac(TELOPT_NEW_ENVIRON);
382 putiac(NEW_ENV_VALUE);
385 putiac(*autologin++);
392 #ifdef CONFIG_FEATURE_AUTOWIDTH
393 static void putiac_naws(byte c, int x, int y)
395 if (G.iaclen + 9 > IACBUFSIZE)
402 putiac((x >> 8) & 0xff);
404 putiac((y >> 8) & 0xff);
412 /* void putiacstring (subneg strings) */
414 /* ******************************* */
416 static char const escapecharis[] = "\r\nEscape character is ";
418 static void setConMode(void)
420 if (G.telflags & UF_ECHO)
422 if (G.charmode == CHM_TRY) {
424 printf("\r\nEntering character mode%s'^]'.\r\n", escapecharis);
430 if (G.charmode != CHM_OFF) {
431 G.charmode = CHM_OFF;
432 printf("\r\nEntering line mode%s'^C'.\r\n", escapecharis);
438 /* ******************************* */
440 static void will_charmode(void)
442 G.charmode = CHM_TRY;
443 G.telflags |= (UF_ECHO | UF_SGA);
446 putiac2(DO, TELOPT_ECHO);
447 putiac2(DO, TELOPT_SGA);
451 static void do_linemode(void)
453 G.charmode = CHM_TRY;
454 G.telflags &= ~(UF_ECHO | UF_SGA);
457 putiac2(DONT, TELOPT_ECHO);
458 putiac2(DONT, TELOPT_SGA);
462 /* ******************************* */
464 static inline void to_notsup(char c)
466 if (G.telwish == WILL) putiac2(DONT, c);
467 else if (G.telwish == DO) putiac2(WONT, c);
470 static inline void to_echo(void)
472 /* if server requests ECHO, don't agree */
473 if (G.telwish == DO) { putiac2(WONT, TELOPT_ECHO); return; }
474 else if (G.telwish == DONT) return;
476 if (G.telflags & UF_ECHO)
478 if (G.telwish == WILL)
482 if (G.telwish == WONT)
485 if (G.charmode != CHM_OFF)
486 G.telflags ^= UF_ECHO;
488 if (G.telflags & UF_ECHO)
489 putiac2(DO, TELOPT_ECHO);
491 putiac2(DONT, TELOPT_ECHO);
494 WriteCS(1, "\r\n"); /* sudden modec */
497 static inline void to_sga(void)
499 /* daemon always sends will/wont, client do/dont */
501 if (G.telflags & UF_SGA)
503 if (G.telwish == WILL)
507 if (G.telwish == WONT)
510 if ((G.telflags ^= UF_SGA) & UF_SGA) /* toggle */
511 putiac2(DO, TELOPT_SGA);
513 putiac2(DONT, TELOPT_SGA);
518 #ifdef CONFIG_FEATURE_TELNET_TTYPE
519 static inline void to_ttype(void)
521 /* Tell server we will (or won't) do TTYPE */
524 putiac2(WILL, TELOPT_TTYPE);
526 putiac2(WONT, TELOPT_TTYPE);
532 #ifdef CONFIG_FEATURE_TELNET_AUTOLOGIN
533 static inline void to_new_environ(void)
535 /* Tell server we will (or will not) do AUTOLOGIN */
538 putiac2(WILL, TELOPT_NEW_ENVIRON);
540 putiac2(WONT, TELOPT_NEW_ENVIRON);
546 #ifdef CONFIG_FEATURE_AUTOWIDTH
547 static inline void to_naws(void)
549 /* Tell server we will do NAWS */
550 putiac2(WILL, TELOPT_NAWS);
555 static void telopt(byte c)
559 case TELOPT_ECHO: to_echo(); break;
560 case TELOPT_SGA: to_sga(); break;
561 #ifdef CONFIG_FEATURE_TELNET_TTYPE
562 case TELOPT_TTYPE: to_ttype();break;
564 #ifdef CONFIG_FEATURE_TELNET_AUTOLOGIN
565 case TELOPT_NEW_ENVIRON: to_new_environ(); break;
567 #ifdef CONFIG_FEATURE_AUTOWIDTH
568 case TELOPT_NAWS: to_naws();
569 putiac_naws(c, win_width, win_height);
572 default: to_notsup(c);
578 /* ******************************* */
580 /* subnegotiation -- ignore all (except TTYPE,NAWS) */
582 static int subneg(byte c)
588 G.telstate = TS_SUB2;
589 #ifdef CONFIG_FEATURE_TELNET_TTYPE
591 if (c == TELOPT_TTYPE)
592 putiac_subopt(TELOPT_TTYPE,ttype);
594 #ifdef CONFIG_FEATURE_TELNET_AUTOLOGIN
596 if (c == TELOPT_NEW_ENVIRON)
597 putiac_subopt_autologin();
603 G.telstate = TS_SUB1;
609 /* ******************************* */
611 static void fgotsig(int sig)
617 static void rawmode(void)
619 tcsetattr(0, TCSADRAIN, &G.termios_raw);
622 static void cookmode(void)
624 tcsetattr(0, TCSADRAIN, &G.termios_def);
627 extern int telnet_main(int argc, char** argv)
630 struct sockaddr_in s_in;
632 struct pollfd ufds[2];
638 #ifdef CONFIG_FEATURE_TELNET_AUTOLOGIN
642 #ifdef CONFIG_FEATURE_AUTOWIDTH
643 get_terminal_width_height(0, &win_width, &win_height);
646 #ifdef CONFIG_FEATURE_TELNET_TTYPE
647 ttype = getenv("TERM");
650 memset(&G, 0, sizeof G);
652 if (tcgetattr(0, &G.termios_def) < 0)
655 G.termios_raw = G.termios_def;
656 cfmakeraw(&G.termios_raw);
661 #ifdef CONFIG_FEATURE_TELNET_AUTOLOGIN
663 while ((opt = getopt(argc, argv, "al:")) != EOF) {
669 autologin = getenv("USER");
677 bb_lookup_host(&s_in, argv[optind++]);
678 s_in.sin_port = bb_lookup_port((optind < argc) ? argv[optind++] :
679 "telnet", "tcp", 23);
685 bb_lookup_host(&s_in, argv[1]);
686 s_in.sin_port = bb_lookup_port((argc == 3) ? argv[2] : "telnet", "tcp", 23);
689 G.netfd = xconnect(&s_in);
691 setsockopt(G.netfd, SOL_SOCKET, SO_KEEPALIVE, &one, sizeof one);
693 signal(SIGINT, fgotsig);
696 ufds[0].fd = 0; ufds[1].fd = G.netfd;
697 ufds[0].events = ufds[1].events = POLLIN;
701 FD_SET(G.netfd, &readfds);
708 fd_set rfds = readfds;
710 switch (select(maxfd, &rfds, NULL, NULL, NULL))
712 switch (poll(ufds, 2, -1))
718 /* error, ignore and/or log something, bay go to loop */
727 if (ufds[0].revents) /* well, should check POLLIN, but ... */
729 if (FD_ISSET(0, &rfds))
732 len = read(0, G.buf, DATABUFSIZE);
737 TRACE(0, ("Read con: %d\n", len));
739 handlenetoutput(len);
743 if (ufds[1].revents) /* well, should check POLLIN, but ... */
745 if (FD_ISSET(G.netfd, &rfds))
748 len = read(G.netfd, G.buf, DATABUFSIZE);
752 WriteCS(1, "Connection closed by foreign host.\r\n");
755 TRACE(0, ("Read netfd (%d): %d\n", G.netfd, len));
765 c-file-style: "linux"