Email addr fix
[oweals/busybox.git] / networking / telnet.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * telnet implementation for busybox
4  *
5  * Author: Tomi Ollila <too@iki.fi>
6  * Copyright (C) 1994-2000 by Tomi Ollila
7  *
8  * Created: Thu Apr  7 13:29:41 1994 too
9  * Last modified: Fri Jun  9 14:34:24 2000 too
10  *
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.
15  *
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.
20  *
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
24  *
25  * HISTORY
26  * Revision 3.1  1994/04/17  11:31:54  too
27  * initial revision
28  * Modified 2000/06/13 for inclusion into BusyBox by Erik Andersen <andersee@debian.org>
29  * Modified 2001/05/07 to add ability to pass TTYPE to remote host by Jim McQuillan
30  * <jam@ltsp.org>
31  *
32  */
33
34 #include <termios.h>
35 #include <unistd.h>
36 #include <errno.h>
37 #include <stdlib.h>
38 #include <stdarg.h>
39 #include <string.h>
40 #include <signal.h>
41 #include <arpa/telnet.h>
42 #include <sys/types.h>
43 #include <sys/socket.h>
44 #include <netinet/in.h>
45 #include <netdb.h>
46 #include "busybox.h"
47
48 #ifdef CONFIG_FEATURE_AUTOWIDTH
49 #   include <sys/ioctl.h>
50 #endif
51
52 #if 0
53 static const int DOTRACE = 1;
54 #endif
55
56 #ifdef DOTRACE
57 #include <arpa/inet.h> /* for inet_ntoa()... */
58 #define TRACE(x, y) do { if (x) printf y; } while (0)
59 #else
60 #define TRACE(x, y) 
61 #endif
62
63 #if 0
64 #define USE_POLL
65 #include <sys/poll.h>
66 #else
67 #include <sys/time.h>
68 #endif
69
70 #define DATABUFSIZE  128
71 #define IACBUFSIZE   128
72
73 static const int CHM_TRY = 0;
74 static const int CHM_ON = 1;
75 static const int CHM_OFF = 2;
76
77 static const int UF_ECHO = 0x01;
78 static const int UF_SGA = 0x02;
79
80 enum {
81         TS_0 = 1,
82         TS_IAC = 2,
83         TS_OPT = 3,
84         TS_SUB1 = 4,
85         TS_SUB2 = 5,
86 };
87
88 #define WriteCS(fd, str) write(fd, str, sizeof str -1)
89
90 typedef unsigned char byte;
91
92 /* use globals to reduce size ??? */ /* test this hypothesis later */
93 static struct Globalvars {
94         int             netfd; /* console fd:s are 0 and 1 (and 2) */
95     /* same buffer used both for network and console read/write */
96         char    buf[DATABUFSIZE]; /* allocating so static size is smaller */
97         byte    telstate; /* telnet negotiation state from network input */
98         byte    telwish;  /* DO, DONT, WILL, WONT */
99         byte    charmode;
100         byte    telflags;
101         byte    gotsig;
102         /* buffer to handle telnet negotiations */
103         char    iacbuf[IACBUFSIZE];
104         short   iaclen; /* could even use byte */
105         struct termios termios_def;     
106         struct termios termios_raw;     
107 } G;
108
109 #define xUSE_GLOBALVAR_PTR /* xUSE... -> don't use :D (makes smaller code) */
110
111 #ifdef USE_GLOBALVAR_PTR
112 struct Globalvars * Gptr;
113 #define G (*Gptr)
114 #else
115 static struct Globalvars G;
116 #endif
117
118 static inline void iacflush(void)
119 {
120         write(G.netfd, G.iacbuf, G.iaclen);
121         G.iaclen = 0;
122 }
123
124 /* Function prototypes */
125 static int getport(char * p);
126 static struct in_addr getserver(char * p);
127 static void setup_sockaddr_in(struct sockaddr_in * addr, int port);
128 static int remote_connect(struct in_addr addr, int port);
129 static void rawmode(void);
130 static void cookmode(void);
131 static void do_linemode(void);
132 static void will_charmode(void);
133 static void telopt(byte c);
134 static int subneg(byte c);
135 #if 0
136 static int local_bind(int port);
137 #endif
138
139 /* Some globals */
140 static int one = 1;
141
142 #ifdef CONFIG_FEATURE_TELNET_TTYPE
143 static char *ttype;
144 #endif
145
146 #ifdef CONFIG_FEATURE_AUTOWIDTH
147 static int win_width, win_height;
148 #endif
149
150 static void doexit(int ev)
151 {
152         cookmode();
153         exit(ev);
154 }       
155
156 static void conescape(void)
157 {
158         char b;
159
160         if (G.gotsig)   /* came from line  mode... go raw */
161                 rawmode();
162
163         WriteCS(1, "\r\nConsole escape. Commands are:\r\n\n"
164                         " l     go to line mode\r\n"
165                         " c     go to character mode\r\n"
166                         " z     suspend telnet\r\n"
167                         " e     exit telnet\r\n");
168
169         if (read(0, &b, 1) <= 0)
170                 doexit(1);
171
172         switch (b)
173         {
174         case 'l':
175                 if (!G.gotsig)
176                 {
177                         do_linemode();
178                         goto rrturn;
179                 }
180                 break;
181         case 'c':
182                 if (G.gotsig)
183                 {
184                         will_charmode();
185                         goto rrturn;
186                 }
187                 break;
188         case 'z':
189                 cookmode();
190                 kill(0, SIGTSTP);
191                 rawmode();
192                 break;
193         case 'e':
194                 doexit(0);
195         }
196
197         WriteCS(1, "continuing...\r\n");
198
199         if (G.gotsig)
200                 cookmode();
201         
202  rrturn:
203         G.gotsig = 0;
204         
205 }
206 static void handlenetoutput(int len)
207 {
208         /*      here we could do smart tricks how to handle 0xFF:s in output
209          *      stream  like writing twice every sequence of FF:s (thus doing
210          *      many write()s. But I think interactive telnet application does
211          *      not need to be 100% 8-bit clean, so changing every 0xff:s to
212          *      0x7f:s
213          *
214          *      2002-mar-21, Przemyslaw Czerpak (druzus@polbox.com)
215          *      I don't agree.
216          *      first - I cannot use programs like sz/rz
217          *      second - the 0x0D is sent as one character and if the next
218          *               char is 0x0A then it's eaten by a server side.
219          *      third - whay doy you have to make 'many write()s'?
220          *              I don't understand.
221          *      So I implemented it. It's realy useful for me. I hope that
222          *      others people will find it interesting to.
223          */
224
225         int i, j;
226         byte * p = G.buf;
227         byte outbuf[4*DATABUFSIZE];
228
229         for (i = len, j = 0; i > 0; i--, p++)
230         {
231                 if (*p == 0x1d)
232                 {
233                         conescape();
234                         return;
235                 }
236                 outbuf[j++] = *p;
237                 if (*p == 0xff)
238                     outbuf[j++] = 0xff;
239                 else if (*p == 0x0d)
240                     outbuf[j++] = 0x00;
241         }
242         if (j > 0 )
243             write(G.netfd, outbuf, j);
244 }
245
246
247 static void handlenetinput(int len)
248 {
249         int i;
250         int cstart = 0;
251
252         for (i = 0; i < len; i++)
253         {
254                 byte c = G.buf[i];
255
256                 if (G.telstate == 0) /* most of the time state == 0 */
257                 {
258                         if (c == IAC)
259                         {
260                                 cstart = i;
261                                 G.telstate = TS_IAC;
262                         }
263                 }
264                 else
265                         switch (G.telstate)
266                          {
267                          case TS_0:
268                                  if (c == IAC)
269                                          G.telstate = TS_IAC;
270                                  else
271                                          G.buf[cstart++] = c;
272                                  break;
273
274                          case TS_IAC:
275                                  if (c == IAC) /* IAC IAC -> 0xFF */
276                                  {
277                                          G.buf[cstart++] = c;
278                                          G.telstate = TS_0;
279                                          break;
280                                  }
281                                  /* else */
282                                  switch (c)
283                                  {
284                                  case SB:
285                                          G.telstate = TS_SUB1;
286                                          break;
287                                  case DO:
288                                  case DONT:
289                                  case WILL:
290                                  case WONT:
291                                          G.telwish =  c;
292                                          G.telstate = TS_OPT;
293                                          break;
294                                  default:
295                                          G.telstate = TS_0;     /* DATA MARK must be added later */
296                                  }
297                                  break;
298                          case TS_OPT: /* WILL, WONT, DO, DONT */
299                                  telopt(c);
300                                  G.telstate = TS_0;
301                                  break;
302                          case TS_SUB1: /* Subnegotiation */
303                          case TS_SUB2: /* Subnegotiation */
304                                  if (subneg(c))
305                                          G.telstate = TS_0;
306                                  break;
307                          }
308         }
309         if (G.telstate)
310         {
311                 if (G.iaclen)                   iacflush();
312                 if (G.telstate == TS_0) G.telstate = 0;
313
314                 len = cstart;
315         }
316
317         if (len)
318                 write(1, G.buf, len);
319 }
320
321
322 /* ******************************* */
323
324 static inline void putiac(int c)
325 {
326         G.iacbuf[G.iaclen++] = c;
327 }
328
329
330 static void putiac2(byte wwdd, byte c)
331 {
332         if (G.iaclen + 3 > IACBUFSIZE)
333                 iacflush();
334
335         putiac(IAC);
336         putiac(wwdd);
337         putiac(c);
338 }
339
340 #if 0
341 static void putiac1(byte c)
342 {
343         if (G.iaclen + 2 > IACBUFSIZE)
344                 iacflush();
345
346         putiac(IAC);
347         putiac(c);
348 }
349 #endif
350
351 #ifdef CONFIG_FEATURE_TELNET_TTYPE
352 static void putiac_subopt(byte c, char *str)
353 {
354         int     len = strlen(str) + 6;   // ( 2 + 1 + 1 + strlen + 2 )
355
356         if (G.iaclen + len > IACBUFSIZE)
357                 iacflush();
358
359         putiac(IAC);
360         putiac(SB);
361         putiac(c);
362         putiac(0);
363
364         while(*str)
365                 putiac(*str++);
366
367         putiac(IAC);
368         putiac(SE);
369 }
370 #endif
371
372 #ifdef CONFIG_FEATURE_AUTOWIDTH
373 static void putiac_naws(byte c, int x, int y)
374 {
375         if (G.iaclen + 9 > IACBUFSIZE)
376                 iacflush();
377
378         putiac(IAC);
379         putiac(SB);
380         putiac(c);
381
382         putiac((x >> 8) & 0xff);
383         putiac(x & 0xff);
384         putiac((y >> 8) & 0xff);
385         putiac(y & 0xff);
386
387         putiac(IAC);
388         putiac(SE);
389 }
390 #endif
391
392 /* void putiacstring (subneg strings) */
393
394 /* ******************************* */
395
396 static char const escapecharis[] = "\r\nEscape character is ";
397
398 static void setConMode(void)
399 {
400         if (G.telflags & UF_ECHO)
401         {
402                 if (G.charmode == CHM_TRY) {
403                         G.charmode = CHM_ON;
404                         printf("\r\nEntering character mode%s'^]'.\r\n", escapecharis);
405                         rawmode();
406                 }
407         }
408         else
409         {
410                 if (G.charmode != CHM_OFF) {
411                         G.charmode = CHM_OFF;
412                         printf("\r\nEntering line mode%s'^C'.\r\n", escapecharis);
413                         cookmode();
414                 }
415         }
416 }
417
418 /* ******************************* */
419
420 static void will_charmode(void)
421 {
422         G.charmode = CHM_TRY;
423         G.telflags |= (UF_ECHO | UF_SGA);
424         setConMode();
425   
426         putiac2(DO, TELOPT_ECHO);
427         putiac2(DO, TELOPT_SGA);
428         iacflush();
429 }
430
431 static void do_linemode(void)
432 {
433         G.charmode = CHM_TRY;
434         G.telflags &= ~(UF_ECHO | UF_SGA);
435         setConMode();
436
437         putiac2(DONT, TELOPT_ECHO);
438         putiac2(DONT, TELOPT_SGA);
439         iacflush();
440 }
441
442 /* ******************************* */
443
444 static inline void to_notsup(char c)
445 {
446         if      (G.telwish == WILL)     putiac2(DONT, c);
447         else if (G.telwish == DO)       putiac2(WONT, c);
448 }
449
450 static inline void to_echo(void)
451 {
452         /* if server requests ECHO, don't agree */
453         if      (G.telwish == DO) {     putiac2(WONT, TELOPT_ECHO);     return; }
454         else if (G.telwish == DONT)     return;
455   
456         if (G.telflags & UF_ECHO)
457         {
458                 if (G.telwish == WILL)
459                         return;
460         }
461         else
462                 if (G.telwish == WONT)
463                         return;
464
465         if (G.charmode != CHM_OFF)
466                 G.telflags ^= UF_ECHO;
467
468         if (G.telflags & UF_ECHO)
469                 putiac2(DO, TELOPT_ECHO);
470         else
471                 putiac2(DONT, TELOPT_ECHO);
472
473         setConMode();
474         WriteCS(1, "\r\n");  /* sudden modec */
475 }
476
477 static inline void to_sga(void)
478 {
479         /* daemon always sends will/wont, client do/dont */
480
481         if (G.telflags & UF_SGA)
482         {
483                 if (G.telwish == WILL)
484                         return;
485         }
486         else
487                 if (G.telwish == WONT)
488                         return;
489   
490         if ((G.telflags ^= UF_SGA) & UF_SGA) /* toggle */
491                 putiac2(DO, TELOPT_SGA);
492         else
493                 putiac2(DONT, TELOPT_SGA);
494
495         return;
496 }
497
498 #ifdef CONFIG_FEATURE_TELNET_TTYPE
499 static inline void to_ttype(void)
500 {
501         /* Tell server we will (or won't) do TTYPE */
502
503         if(ttype)
504                 putiac2(WILL, TELOPT_TTYPE);
505         else
506                 putiac2(WONT, TELOPT_TTYPE);
507
508         return;
509 }
510 #endif
511
512 #ifdef CONFIG_FEATURE_AUTOWIDTH
513 static inline void to_naws(void)
514
515         /* Tell server we will do NAWS */
516         putiac2(WILL, TELOPT_NAWS);
517         return;
518 }         
519 #endif
520
521 static void telopt(byte c)
522 {
523         switch (c)
524         {
525                 case TELOPT_ECHO:               to_echo();      break;
526                 case TELOPT_SGA:                to_sga();       break;
527 #ifdef CONFIG_FEATURE_TELNET_TTYPE
528                 case TELOPT_TTYPE:              to_ttype();break;
529 #endif
530 #ifdef CONFIG_FEATURE_AUTOWIDTH
531                 case TELOPT_NAWS:               to_naws();
532                                                                 putiac_naws(c, win_width, win_height);
533                                                                 break;
534 #endif
535                 default:                                to_notsup(c);
536                                                                 break;
537         }
538 }
539
540
541 /* ******************************* */
542
543 /* subnegotiation -- ignore all (except TTYPE,NAWS) */
544
545 static int subneg(byte c)
546 {
547         switch (G.telstate)
548         {
549         case TS_SUB1:
550                 if (c == IAC)
551                         G.telstate = TS_SUB2;
552 #ifdef CONFIG_FEATURE_TELNET_TTYPE
553                 else
554                 if (c == TELOPT_TTYPE)
555                         putiac_subopt(TELOPT_TTYPE,ttype);
556 #endif
557                 break;
558         case TS_SUB2:
559                 if (c == SE)
560                         return TRUE;
561                 G.telstate = TS_SUB1;
562                 /* break; */
563         }
564         return FALSE;
565 }
566
567 /* ******************************* */
568
569 static void fgotsig(int sig)
570 {
571         G.gotsig = sig;
572 }
573
574
575 static void rawmode(void)
576 {
577         tcsetattr(0, TCSADRAIN, &G.termios_raw);
578 }       
579
580 static void cookmode(void)
581 {
582         tcsetattr(0, TCSADRAIN, &G.termios_def);
583 }
584
585 extern int telnet_main(int argc, char** argv)
586 {
587         struct in_addr host;
588         int port;
589         int len;
590 #ifdef USE_POLL
591         struct pollfd ufds[2];
592 #else   
593         fd_set readfds;
594         int maxfd;
595 #endif  
596
597 #ifdef CONFIG_FEATURE_AUTOWIDTH
598     struct winsize winp;
599     if( ioctl(0, TIOCGWINSZ, &winp) == 0 ) {
600         win_width  = winp.ws_col;
601         win_height = winp.ws_row;
602     }
603 #endif
604
605 #ifdef CONFIG_FEATURE_TELNET_TTYPE
606     ttype = getenv("TERM");
607 #endif
608
609         memset(&G, 0, sizeof G);
610
611         if (tcgetattr(0, &G.termios_def) < 0)
612                 exit(1);
613         
614         G.termios_raw = G.termios_def;
615         cfmakeraw(&G.termios_raw);
616         
617         if (argc < 2)   show_usage();
618         port = (argc > 2)? getport(argv[2]): 23;
619         
620         host = getserver(argv[1]);
621
622         G.netfd = remote_connect(host, port);
623
624         signal(SIGINT, fgotsig);
625
626 #ifdef USE_POLL
627         ufds[0].fd = 0; ufds[1].fd = G.netfd;
628         ufds[0].events = ufds[1].events = POLLIN;
629 #else   
630         FD_ZERO(&readfds);
631         FD_SET(0, &readfds);
632         FD_SET(G.netfd, &readfds);
633         maxfd = G.netfd + 1;
634 #endif
635         
636         while (1)
637         {
638 #ifndef USE_POLL
639                 fd_set rfds = readfds;
640                 
641                 switch (select(maxfd, &rfds, NULL, NULL, NULL))
642 #else
643                 switch (poll(ufds, 2, -1))
644 #endif                  
645                 {
646                 case 0:
647                         /* timeout */
648                 case -1:
649                         /* error, ignore and/or log something, bay go to loop */
650                         if (G.gotsig)
651                                 conescape();
652                         else
653                                 sleep(1);
654                         break;
655                 default:
656
657 #ifdef USE_POLL
658                         if (ufds[0].revents) /* well, should check POLLIN, but ... */
659 #else                           
660                         if (FD_ISSET(0, &rfds))
661 #endif                          
662                         {
663                                 len = read(0, G.buf, DATABUFSIZE);
664
665                                 if (len <= 0)
666                                         doexit(0);
667
668                                 TRACE(0, ("Read con: %d\n", len));
669                                 
670                                 handlenetoutput(len);
671                         }
672
673 #ifdef USE_POLL
674                         if (ufds[1].revents) /* well, should check POLLIN, but ... */
675 #else                           
676                         if (FD_ISSET(G.netfd, &rfds))
677 #endif                          
678                         {
679                                 len = read(G.netfd, G.buf, DATABUFSIZE);
680
681                                 if (len <= 0)
682                                 {
683                                         WriteCS(1, "Connection closed by foreign host.\r\n");
684                                         doexit(1);
685                                 }
686                                 TRACE(0, ("Read netfd (%d): %d\n", G.netfd, len));
687
688                                 handlenetinput(len);
689                         }
690                 }
691         }
692 }
693
694 static int getport(char * p)
695 {
696         unsigned int port = atoi(p);
697
698         if ((unsigned)(port - 1 ) > 65534)
699         {
700                 error_msg_and_die("%s: bad port number", p);
701         }
702         return port;
703 }
704
705 static struct in_addr getserver(char * host)
706 {
707         struct in_addr addr;
708
709         struct hostent * he;
710         he = xgethostbyname(host);
711         memcpy(&addr, he->h_addr, sizeof addr);
712
713         TRACE(1, ("addr: %s\n", inet_ntoa(addr)));
714
715         return addr;
716 }
717
718 static int create_socket(void)
719 {
720         return socket(AF_INET, SOCK_STREAM, 0);
721 }
722
723 static void setup_sockaddr_in(struct sockaddr_in * addr, int port)
724 {
725         memset(addr, 0, sizeof(struct sockaddr_in));
726         addr->sin_family = AF_INET;
727         addr->sin_port = htons(port);
728 }
729   
730 #if 0
731 static int local_bind(int port)
732 {
733         struct sockaddr_in s_addr;
734         int s = create_socket();
735   
736         setup_sockaddr_in(&s_addr, port);
737   
738         setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &one, sizeof one);
739   
740         if (bind(s, &s_addr, sizeof s_addr) < 0)
741         {
742                 char * e = sys_errlist[errno];
743                 syserrorexit("bind");
744                 exit(1);
745         }
746         listen(s, 1);
747         
748         return s;
749 }
750 #endif
751
752 static int remote_connect(struct in_addr addr, int port)
753 {
754         struct sockaddr_in s_addr;
755         int s = create_socket();
756
757         setup_sockaddr_in(&s_addr, port);
758         s_addr.sin_addr = addr;
759
760         setsockopt(s, SOL_SOCKET, SO_KEEPALIVE, &one, sizeof one);
761
762         if (connect(s, (struct sockaddr *)&s_addr, sizeof s_addr) < 0)
763         {
764                 perror_msg_and_die("Unable to connect to remote host");
765         }
766         return s;
767 }
768
769 /*
770 Local Variables:
771 c-file-style: "linux"
772 c-basic-offset: 4
773 tab-width: 4
774 End:
775 */