OSDN Git Service

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