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>
38 #include <arpa/inet.h> /* for inet_ntoa()... */
39 #define TRACE(x, y) do { if (x) printf y; } while (0)
44 #define DATABUFSIZE 128
45 #define IACBUFSIZE 128
62 #define WriteCS(fd, str) write(fd, str, sizeof str -1)
64 typedef unsigned char byte;
66 /* use globals to reduce size ??? */ /* test this hypothesis later */
67 static struct Globalvars {
68 int netfd; /* console fd:s are 0 and 1 (and 2) */
69 /* same buffer used both for network and console read/write */
70 char buf[DATABUFSIZE]; /* allocating so static size is smaller */
71 byte telstate; /* telnet negotiation state from network input */
72 byte telwish; /* DO, DONT, WILL, WONT */
77 /* buffer to handle telnet negotiations */
78 char iacbuf[IACBUFSIZE];
79 short iaclen; /* could even use byte */
80 struct termios termios_def;
81 struct termios termios_raw;
84 #define xUSE_GLOBALVAR_PTR /* xUSE... -> don't use :D (makes smaller code) */
86 #ifdef USE_GLOBALVAR_PTR
87 struct Globalvars * Gptr;
91 static void iacflush(void)
93 write(G.netfd, G.iacbuf, G.iaclen);
97 /* Function prototypes */
98 static void rawmode(void);
99 static void cookmode(void);
100 static void do_linemode(void);
101 static void will_charmode(void);
102 static void telopt(byte c);
103 static int subneg(byte c);
108 #ifdef CONFIG_FEATURE_TELNET_TTYPE
112 #ifdef CONFIG_FEATURE_TELNET_AUTOLOGIN
113 static const char *autologin;
116 #ifdef CONFIG_FEATURE_AUTOWIDTH
117 static int win_width, win_height;
120 static void doexit(int ev)
126 static void conescape(void)
130 if (G.gotsig) /* came from line mode... go raw */
133 WriteCS(1, "\r\nConsole escape. Commands are:\r\n\n"
134 " l go to line mode\r\n"
135 " c go to character mode\r\n"
136 " z suspend telnet\r\n"
137 " e exit telnet\r\n");
139 if (read(0, &b, 1) <= 0)
167 WriteCS(1, "continuing...\r\n");
176 static void handlenetoutput(int len)
178 /* here we could do smart tricks how to handle 0xFF:s in output
179 * stream like writing twice every sequence of FF:s (thus doing
180 * many write()s. But I think interactive telnet application does
181 * not need to be 100% 8-bit clean, so changing every 0xff:s to
184 * 2002-mar-21, Przemyslaw Czerpak (druzus@polbox.com)
186 * first - I cannot use programs like sz/rz
187 * second - the 0x0D is sent as one character and if the next
188 * char is 0x0A then it's eaten by a server side.
189 * third - whay doy you have to make 'many write()s'?
190 * I don't understand.
191 * So I implemented it. It's realy useful for me. I hope that
192 * others people will find it interesting to.
196 byte * p = (byte*)G.buf;
197 byte outbuf[4*DATABUFSIZE];
199 for (i = len, j = 0; i > 0; i--, p++)
213 write(G.netfd, outbuf, j);
217 static void handlenetinput(int len)
222 for (i = 0; i < len; i++)
226 if (G.telstate == 0) /* most of the time state == 0 */
245 if (c == IAC) /* IAC IAC -> 0xFF */
255 G.telstate = TS_SUB1;
265 G.telstate = TS_0; /* DATA MARK must be added later */
268 case TS_OPT: /* WILL, WONT, DO, DONT */
272 case TS_SUB1: /* Subnegotiation */
273 case TS_SUB2: /* Subnegotiation */
281 if (G.iaclen) iacflush();
282 if (G.telstate == TS_0) G.telstate = 0;
288 write(1, G.buf, len);
292 /* ******************************* */
294 static void putiac(int c)
296 G.iacbuf[G.iaclen++] = c;
300 static void putiac2(byte wwdd, byte c)
302 if (G.iaclen + 3 > IACBUFSIZE)
310 #ifdef CONFIG_FEATURE_TELNET_TTYPE
311 static void putiac_subopt(byte c, char *str)
313 int len = strlen(str) + 6; // ( 2 + 1 + 1 + strlen + 2 )
315 if (G.iaclen + len > IACBUFSIZE)
331 #ifdef CONFIG_FEATURE_TELNET_AUTOLOGIN
332 static void putiac_subopt_autologin(void)
334 int len = strlen(autologin) + 6; // (2 + 1 + 1 + strlen + 2)
337 if (G.iaclen + len > IACBUFSIZE)
342 putiac(TELOPT_NEW_ENVIRON);
349 putiac(NEW_ENV_VALUE);
352 putiac(*autologin++);
359 #ifdef CONFIG_FEATURE_AUTOWIDTH
360 static void putiac_naws(byte c, int x, int y)
362 if (G.iaclen + 9 > IACBUFSIZE)
369 putiac((x >> 8) & 0xff);
371 putiac((y >> 8) & 0xff);
379 /* void putiacstring (subneg strings) */
381 /* ******************************* */
383 static char const escapecharis[] = "\r\nEscape character is ";
385 static void setConMode(void)
387 if (G.telflags & UF_ECHO)
389 if (G.charmode == CHM_TRY) {
391 printf("\r\nEntering character mode%s'^]'.\r\n", escapecharis);
397 if (G.charmode != CHM_OFF) {
398 G.charmode = CHM_OFF;
399 printf("\r\nEntering line mode%s'^C'.\r\n", escapecharis);
405 /* ******************************* */
407 static void will_charmode(void)
409 G.charmode = CHM_TRY;
410 G.telflags |= (UF_ECHO | UF_SGA);
413 putiac2(DO, TELOPT_ECHO);
414 putiac2(DO, TELOPT_SGA);
418 static void do_linemode(void)
420 G.charmode = CHM_TRY;
421 G.telflags &= ~(UF_ECHO | UF_SGA);
424 putiac2(DONT, TELOPT_ECHO);
425 putiac2(DONT, TELOPT_SGA);
429 /* ******************************* */
431 static void to_notsup(char c)
433 if (G.telwish == WILL) putiac2(DONT, c);
434 else if (G.telwish == DO) putiac2(WONT, c);
437 static void to_echo(void)
439 /* if server requests ECHO, don't agree */
440 if (G.telwish == DO) { putiac2(WONT, TELOPT_ECHO); return; }
441 else if (G.telwish == DONT) return;
443 if (G.telflags & UF_ECHO)
445 if (G.telwish == WILL)
449 if (G.telwish == WONT)
452 if (G.charmode != CHM_OFF)
453 G.telflags ^= UF_ECHO;
455 if (G.telflags & UF_ECHO)
456 putiac2(DO, TELOPT_ECHO);
458 putiac2(DONT, TELOPT_ECHO);
461 WriteCS(1, "\r\n"); /* sudden modec */
464 static void to_sga(void)
466 /* daemon always sends will/wont, client do/dont */
468 if (G.telflags & UF_SGA)
470 if (G.telwish == WILL)
474 if (G.telwish == WONT)
477 if ((G.telflags ^= UF_SGA) & UF_SGA) /* toggle */
478 putiac2(DO, TELOPT_SGA);
480 putiac2(DONT, TELOPT_SGA);
485 #ifdef CONFIG_FEATURE_TELNET_TTYPE
486 static void to_ttype(void)
488 /* Tell server we will (or won't) do TTYPE */
491 putiac2(WILL, TELOPT_TTYPE);
493 putiac2(WONT, TELOPT_TTYPE);
499 #ifdef CONFIG_FEATURE_TELNET_AUTOLOGIN
500 static void to_new_environ(void)
502 /* Tell server we will (or will not) do AUTOLOGIN */
505 putiac2(WILL, TELOPT_NEW_ENVIRON);
507 putiac2(WONT, TELOPT_NEW_ENVIRON);
513 #ifdef CONFIG_FEATURE_AUTOWIDTH
514 static void to_naws(void)
516 /* Tell server we will do NAWS */
517 putiac2(WILL, TELOPT_NAWS);
522 static void telopt(byte c)
526 case TELOPT_ECHO: to_echo(); break;
527 case TELOPT_SGA: to_sga(); break;
528 #ifdef CONFIG_FEATURE_TELNET_TTYPE
529 case TELOPT_TTYPE: to_ttype();break;
531 #ifdef CONFIG_FEATURE_TELNET_AUTOLOGIN
532 case TELOPT_NEW_ENVIRON: to_new_environ(); break;
534 #ifdef CONFIG_FEATURE_AUTOWIDTH
535 case TELOPT_NAWS: to_naws();
536 putiac_naws(c, win_width, win_height);
539 default: to_notsup(c);
545 /* ******************************* */
547 /* subnegotiation -- ignore all (except TTYPE,NAWS) */
549 static int subneg(byte c)
555 G.telstate = TS_SUB2;
556 #ifdef CONFIG_FEATURE_TELNET_TTYPE
558 if (c == TELOPT_TTYPE)
559 putiac_subopt(TELOPT_TTYPE,ttype);
561 #ifdef CONFIG_FEATURE_TELNET_AUTOLOGIN
563 if (c == TELOPT_NEW_ENVIRON)
564 putiac_subopt_autologin();
570 G.telstate = TS_SUB1;
576 /* ******************************* */
578 static void fgotsig(int sig)
584 static void rawmode(void)
586 if (G.do_termios) tcsetattr(0, TCSADRAIN, &G.termios_raw);
589 static void cookmode(void)
591 if (G.do_termios) tcsetattr(0, TCSADRAIN, &G.termios_def);
594 int telnet_main(int argc, char** argv)
597 struct sockaddr_in s_in;
599 struct pollfd ufds[2];
605 #ifdef CONFIG_FEATURE_AUTOWIDTH
606 get_terminal_width_height(0, &win_width, &win_height);
609 #ifdef CONFIG_FEATURE_TELNET_TTYPE
610 ttype = getenv("TERM");
613 memset(&G, 0, sizeof G);
615 if (tcgetattr(0, &G.termios_def) >= 0) {
618 G.termios_raw = G.termios_def;
619 cfmakeraw(&G.termios_raw);
625 #ifdef CONFIG_FEATURE_TELNET_AUTOLOGIN
626 if (1 & getopt32(argc, argv, "al:", &autologin))
627 autologin = getenv("USER");
630 bb_lookup_host(&s_in, argv[optind++]);
631 s_in.sin_port = bb_lookup_port((optind < argc) ? argv[optind++] :
632 "telnet", "tcp", 23);
638 bb_lookup_host(&s_in, argv[1]);
639 s_in.sin_port = bb_lookup_port((argc == 3) ? argv[2] : "telnet", "tcp", 23);
642 G.netfd = xconnect_tcp_v4(&s_in);
644 setsockopt(G.netfd, SOL_SOCKET, SO_KEEPALIVE, &one, sizeof one);
646 signal(SIGINT, fgotsig);
649 ufds[0].fd = 0; ufds[1].fd = G.netfd;
650 ufds[0].events = ufds[1].events = POLLIN;
654 FD_SET(G.netfd, &readfds);
661 fd_set rfds = readfds;
663 switch (select(maxfd, &rfds, NULL, NULL, NULL))
665 switch (poll(ufds, 2, -1))
671 /* error, ignore and/or log something, bay go to loop */
680 if (ufds[0].revents) /* well, should check POLLIN, but ... */
682 if (FD_ISSET(0, &rfds))
685 len = read(0, G.buf, DATABUFSIZE);
690 TRACE(0, ("Read con: %d\n", len));
692 handlenetoutput(len);
696 if (ufds[1].revents) /* well, should check POLLIN, but ... */
698 if (FD_ISSET(G.netfd, &rfds))
701 len = read(G.netfd, G.buf, DATABUFSIZE);
705 WriteCS(1, "Connection closed by foreign host.\r\n");
708 TRACE(0, ("Read netfd (%d): %d\n", G.netfd, len));