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 */
100 /* buffer to handle telnet negotiations */
101 char iacbuf[IACBUFSIZE];
102 short iaclen; /* could even use byte */
103 struct termios termios_def;
104 struct termios termios_raw;
107 #define xUSE_GLOBALVAR_PTR /* xUSE... -> don't use :D (makes smaller code) */
109 #ifdef USE_GLOBALVAR_PTR
110 struct Globalvars * Gptr;
114 static inline void iacflush(void)
116 write(G.netfd, G.iacbuf, G.iaclen);
120 /* Function prototypes */
121 static void rawmode(void);
122 static void cookmode(void);
123 static void do_linemode(void);
124 static void will_charmode(void);
125 static void telopt(byte c);
126 static int subneg(byte c);
131 #ifdef CONFIG_FEATURE_TELNET_TTYPE
135 #ifdef CONFIG_FEATURE_TELNET_AUTOLOGIN
136 static const char *autologin;
139 #ifdef CONFIG_FEATURE_AUTOWIDTH
140 static int win_width, win_height;
143 static void doexit(int ev)
149 static void conescape(void)
153 if (G.gotsig) /* came from line mode... go raw */
156 WriteCS(1, "\r\nConsole escape. Commands are:\r\n\n"
157 " l go to line mode\r\n"
158 " c go to character mode\r\n"
159 " z suspend telnet\r\n"
160 " e exit telnet\r\n");
162 if (read(0, &b, 1) <= 0)
190 WriteCS(1, "continuing...\r\n");
199 static void handlenetoutput(int len)
201 /* here we could do smart tricks how to handle 0xFF:s in output
202 * stream like writing twice every sequence of FF:s (thus doing
203 * many write()s. But I think interactive telnet application does
204 * not need to be 100% 8-bit clean, so changing every 0xff:s to
207 * 2002-mar-21, Przemyslaw Czerpak (druzus@polbox.com)
209 * first - I cannot use programs like sz/rz
210 * second - the 0x0D is sent as one character and if the next
211 * char is 0x0A then it's eaten by a server side.
212 * third - whay doy you have to make 'many write()s'?
213 * I don't understand.
214 * So I implemented it. It's realy useful for me. I hope that
215 * others people will find it interesting to.
220 byte outbuf[4*DATABUFSIZE];
222 for (i = len, j = 0; i > 0; i--, p++)
236 write(G.netfd, outbuf, j);
240 static void handlenetinput(int len)
245 for (i = 0; i < len; i++)
249 if (G.telstate == 0) /* most of the time state == 0 */
268 if (c == IAC) /* IAC IAC -> 0xFF */
278 G.telstate = TS_SUB1;
288 G.telstate = TS_0; /* DATA MARK must be added later */
291 case TS_OPT: /* WILL, WONT, DO, DONT */
295 case TS_SUB1: /* Subnegotiation */
296 case TS_SUB2: /* Subnegotiation */
304 if (G.iaclen) iacflush();
305 if (G.telstate == TS_0) G.telstate = 0;
311 write(1, G.buf, len);
315 /* ******************************* */
317 static inline void putiac(int c)
319 G.iacbuf[G.iaclen++] = c;
323 static void putiac2(byte wwdd, byte c)
325 if (G.iaclen + 3 > IACBUFSIZE)
334 static void putiac1(byte c)
336 if (G.iaclen + 2 > IACBUFSIZE)
344 #ifdef CONFIG_FEATURE_TELNET_TTYPE
345 static void putiac_subopt(byte c, char *str)
347 int len = strlen(str) + 6; // ( 2 + 1 + 1 + strlen + 2 )
349 if (G.iaclen + len > IACBUFSIZE)
365 #ifdef CONFIG_FEATURE_TELNET_AUTOLOGIN
366 static void putiac_subopt_autologin(void)
368 int len = strlen(autologin) + 6; // (2 + 1 + 1 + strlen + 2)
371 if (G.iaclen + len > IACBUFSIZE)
376 putiac(TELOPT_NEW_ENVIRON);
383 putiac(NEW_ENV_VALUE);
386 putiac(*autologin++);
393 #ifdef CONFIG_FEATURE_AUTOWIDTH
394 static void putiac_naws(byte c, int x, int y)
396 if (G.iaclen + 9 > IACBUFSIZE)
403 putiac((x >> 8) & 0xff);
405 putiac((y >> 8) & 0xff);
413 /* void putiacstring (subneg strings) */
415 /* ******************************* */
417 static char const escapecharis[] = "\r\nEscape character is ";
419 static void setConMode(void)
421 if (G.telflags & UF_ECHO)
423 if (G.charmode == CHM_TRY) {
425 printf("\r\nEntering character mode%s'^]'.\r\n", escapecharis);
431 if (G.charmode != CHM_OFF) {
432 G.charmode = CHM_OFF;
433 printf("\r\nEntering line mode%s'^C'.\r\n", escapecharis);
439 /* ******************************* */
441 static void will_charmode(void)
443 G.charmode = CHM_TRY;
444 G.telflags |= (UF_ECHO | UF_SGA);
447 putiac2(DO, TELOPT_ECHO);
448 putiac2(DO, TELOPT_SGA);
452 static void do_linemode(void)
454 G.charmode = CHM_TRY;
455 G.telflags &= ~(UF_ECHO | UF_SGA);
458 putiac2(DONT, TELOPT_ECHO);
459 putiac2(DONT, TELOPT_SGA);
463 /* ******************************* */
465 static inline void to_notsup(char c)
467 if (G.telwish == WILL) putiac2(DONT, c);
468 else if (G.telwish == DO) putiac2(WONT, c);
471 static inline void to_echo(void)
473 /* if server requests ECHO, don't agree */
474 if (G.telwish == DO) { putiac2(WONT, TELOPT_ECHO); return; }
475 else if (G.telwish == DONT) return;
477 if (G.telflags & UF_ECHO)
479 if (G.telwish == WILL)
483 if (G.telwish == WONT)
486 if (G.charmode != CHM_OFF)
487 G.telflags ^= UF_ECHO;
489 if (G.telflags & UF_ECHO)
490 putiac2(DO, TELOPT_ECHO);
492 putiac2(DONT, TELOPT_ECHO);
495 WriteCS(1, "\r\n"); /* sudden modec */
498 static inline void to_sga(void)
500 /* daemon always sends will/wont, client do/dont */
502 if (G.telflags & UF_SGA)
504 if (G.telwish == WILL)
508 if (G.telwish == WONT)
511 if ((G.telflags ^= UF_SGA) & UF_SGA) /* toggle */
512 putiac2(DO, TELOPT_SGA);
514 putiac2(DONT, TELOPT_SGA);
519 #ifdef CONFIG_FEATURE_TELNET_TTYPE
520 static inline void to_ttype(void)
522 /* Tell server we will (or won't) do TTYPE */
525 putiac2(WILL, TELOPT_TTYPE);
527 putiac2(WONT, TELOPT_TTYPE);
533 #ifdef CONFIG_FEATURE_TELNET_AUTOLOGIN
534 static inline void to_new_environ(void)
536 /* Tell server we will (or will not) do AUTOLOGIN */
539 putiac2(WILL, TELOPT_NEW_ENVIRON);
541 putiac2(WONT, TELOPT_NEW_ENVIRON);
547 #ifdef CONFIG_FEATURE_AUTOWIDTH
548 static inline void to_naws(void)
550 /* Tell server we will do NAWS */
551 putiac2(WILL, TELOPT_NAWS);
556 static void telopt(byte c)
560 case TELOPT_ECHO: to_echo(); break;
561 case TELOPT_SGA: to_sga(); break;
562 #ifdef CONFIG_FEATURE_TELNET_TTYPE
563 case TELOPT_TTYPE: to_ttype();break;
565 #ifdef CONFIG_FEATURE_TELNET_AUTOLOGIN
566 case TELOPT_NEW_ENVIRON: to_new_environ(); break;
568 #ifdef CONFIG_FEATURE_AUTOWIDTH
569 case TELOPT_NAWS: to_naws();
570 putiac_naws(c, win_width, win_height);
573 default: to_notsup(c);
579 /* ******************************* */
581 /* subnegotiation -- ignore all (except TTYPE,NAWS) */
583 static int subneg(byte c)
589 G.telstate = TS_SUB2;
590 #ifdef CONFIG_FEATURE_TELNET_TTYPE
592 if (c == TELOPT_TTYPE)
593 putiac_subopt(TELOPT_TTYPE,ttype);
595 #ifdef CONFIG_FEATURE_TELNET_AUTOLOGIN
597 if (c == TELOPT_NEW_ENVIRON)
598 putiac_subopt_autologin();
604 G.telstate = TS_SUB1;
610 /* ******************************* */
612 static void fgotsig(int sig)
618 static void rawmode(void)
620 if (G.do_termios) tcsetattr(0, TCSADRAIN, &G.termios_raw);
623 static void cookmode(void)
625 if (G.do_termios) tcsetattr(0, TCSADRAIN, &G.termios_def);
628 extern int telnet_main(int argc, char** argv)
631 struct sockaddr_in s_in;
633 struct pollfd ufds[2];
639 #ifdef CONFIG_FEATURE_TELNET_AUTOLOGIN
643 #ifdef CONFIG_FEATURE_AUTOWIDTH
644 get_terminal_width_height(0, &win_width, &win_height);
647 #ifdef CONFIG_FEATURE_TELNET_TTYPE
648 ttype = getenv("TERM");
651 memset(&G, 0, sizeof G);
653 if (tcgetattr(0, &G.termios_def) >= 0) {
656 G.termios_raw = G.termios_def;
657 cfmakeraw(&G.termios_raw);
663 #ifdef CONFIG_FEATURE_TELNET_AUTOLOGIN
665 while ((opt = getopt(argc, argv, "al:")) != EOF) {
671 autologin = getenv("USER");
679 bb_lookup_host(&s_in, argv[optind++]);
680 s_in.sin_port = bb_lookup_port((optind < argc) ? argv[optind++] :
681 "telnet", "tcp", 23);
687 bb_lookup_host(&s_in, argv[1]);
688 s_in.sin_port = bb_lookup_port((argc == 3) ? argv[2] : "telnet", "tcp", 23);
691 G.netfd = xconnect(&s_in);
693 setsockopt(G.netfd, SOL_SOCKET, SO_KEEPALIVE, &one, sizeof one);
695 signal(SIGINT, fgotsig);
698 ufds[0].fd = 0; ufds[1].fd = G.netfd;
699 ufds[0].events = ufds[1].events = POLLIN;
703 FD_SET(G.netfd, &readfds);
710 fd_set rfds = readfds;
712 switch (select(maxfd, &rfds, NULL, NULL, NULL))
714 switch (poll(ufds, 2, -1))
720 /* error, ignore and/or log something, bay go to loop */
729 if (ufds[0].revents) /* well, should check POLLIN, but ... */
731 if (FD_ISSET(0, &rfds))
734 len = read(0, G.buf, DATABUFSIZE);
739 TRACE(0, ("Read con: %d\n", len));
741 handlenetoutput(len);
745 if (ufds[1].revents) /* well, should check POLLIN, but ... */
747 if (FD_ISSET(G.netfd, &rfds))
750 len = read(G.netfd, G.buf, DATABUFSIZE);
754 WriteCS(1, "Connection closed by foreign host.\r\n");
757 TRACE(0, ("Read netfd (%d): %d\n", G.netfd, len));
767 c-file-style: "linux"