blob: ed92a8403eabbfcba7a3e8f3a9c3f3afed901006 [file] [log] [blame]
Eric Andersen28c70b32000-06-14 20:42:57 +00001/* vi: set sw=4 ts=4: */
Erik Andersenf7c49ef2000-02-22 17:17:45 +00002/*
Eric Andersen28c70b32000-06-14 20:42:57 +00003 * telnet implementation for busybox
Erik Andersenf7c49ef2000-02-22 17:17:45 +00004 *
Eric Andersen28c70b32000-06-14 20:42:57 +00005 * 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
Erik Andersenf7c49ef2000-02-22 17:17:45 +000010 *
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 *
Eric Andersen28c70b32000-06-14 20:42:57 +000025 * 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
29 * <andersen@lineo.com>
Erik Andersenf7c49ef2000-02-22 17:17:45 +000030 *
Erik Andersenf7c49ef2000-02-22 17:17:45 +000031 */
32
Eric Andersen3570a342000-09-25 21:45:58 +000033#include "busybox.h"
Erik Andersenf7c49ef2000-02-22 17:17:45 +000034#include <termios.h>
Eric Andersen28c70b32000-06-14 20:42:57 +000035#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>
Erik Andersenf7c49ef2000-02-22 17:17:45 +000042#include <sys/types.h>
43#include <sys/socket.h>
Eric Andersen28c70b32000-06-14 20:42:57 +000044#include <netinet/in.h>
45#include <netdb.h>
Erik Andersenf7c49ef2000-02-22 17:17:45 +000046
Eric Andersen28c70b32000-06-14 20:42:57 +000047#if 0
Mark Whitley59ab0252001-01-23 22:30:04 +000048static const int DOTRACE = 1;
Eric Andersen28c70b32000-06-14 20:42:57 +000049#endif
50
Pavel Roskin616d13b2000-07-28 19:38:27 +000051#ifdef DOTRACE
Eric Andersen28c70b32000-06-14 20:42:57 +000052#include <arpa/inet.h> /* for inet_ntoa()... */
53#define TRACE(x, y) do { if (x) printf y; } while (0)
54#else
55#define TRACE(x, y)
56#endif
57
58#if 0
59#define USE_POLL
60#include <sys/poll.h>
61#else
62#include <sys/time.h>
63#endif
64
Mark Whitley59ab0252001-01-23 22:30:04 +000065static const int DATABUFSIZE = 128;
66static const int IACBUFSIZE = 128;
Eric Andersen28c70b32000-06-14 20:42:57 +000067
Mark Whitley59ab0252001-01-23 22:30:04 +000068static const int CHM_TRY = 0;
69static const int CHM_ON = 1;
70static const int CHM_OFF = 2;
Eric Andersen28c70b32000-06-14 20:42:57 +000071
Mark Whitley59ab0252001-01-23 22:30:04 +000072static const int UF_ECHO = 0x01;
73static const int UF_SGA = 0x02;
Eric Andersen28c70b32000-06-14 20:42:57 +000074
Mark Whitley59ab0252001-01-23 22:30:04 +000075enum {
76 TS_0 = 1,
77 TS_IAC = 2,
78 TS_OPT = 3,
79 TS_SUB1 = 4,
80 TS_SUB2 = 5,
81};
Eric Andersen28c70b32000-06-14 20:42:57 +000082
83#define WriteCS(fd, str) write(fd, str, sizeof str -1)
84
85typedef unsigned char byte;
86
87/* use globals to reduce size ??? */ /* test this hypothesis later */
88struct Globalvars {
89 int netfd; /* console fd:s are 0 and 1 (and 2) */
90 /* same buffer used both for network and console read/write */
91 char * buf; /* allocating so static size is smaller */
92 short len;
93 byte telstate; /* telnet negotiation state from network input */
94 byte telwish; /* DO, DONT, WILL, WONT */
95 byte charmode;
96 byte telflags;
97 byte gotsig;
98 /* buffer to handle telnet negotiations */
99 char * iacbuf;
100 short iaclen; /* could even use byte */
101 struct termios termios_def;
102 struct termios termios_raw;
103} G;
104
105#define xUSE_GLOBALVAR_PTR /* xUSE... -> don't use :D (makes smaller code) */
106
107#ifdef USE_GLOBALVAR_PTR
108struct Globalvars * Gptr;
109#define G (*Gptr)
110#else
111struct Globalvars G;
112#endif
113
114static inline void iacflush()
115{
116 write(G.netfd, G.iacbuf, G.iaclen);
117 G.iaclen = 0;
118}
119
120/* Function prototypes */
121static int getport(char * p);
122static struct in_addr getserver(char * p);
123static int create_socket();
124static void setup_sockaddr_in(struct sockaddr_in * addr, int port);
125static int remote_connect(struct in_addr addr, int port);
126static void rawmode();
127static void cookmode();
128static void do_linemode();
129static void will_charmode();
130static void telopt(byte c);
131static int subneg(byte c);
132#if 0
133static int local_bind(int port);
134#endif
135
136/* Some globals */
137static int one = 1;
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000138
Eric Andersen28c70b32000-06-14 20:42:57 +0000139static void doexit(int ev)
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000140{
Eric Andersen28c70b32000-06-14 20:42:57 +0000141 cookmode();
142 exit(ev);
143}
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000144
Eric Andersen28c70b32000-06-14 20:42:57 +0000145static void conescape()
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000146{
Eric Andersen28c70b32000-06-14 20:42:57 +0000147 char b;
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000148
Eric Andersen28c70b32000-06-14 20:42:57 +0000149 if (G.gotsig) /* came from line mode... go raw */
150 rawmode();
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000151
Eric Andersen28c70b32000-06-14 20:42:57 +0000152 WriteCS(1, "\r\nConsole escape. Commands are:\r\n\n"
153 " l go to line mode\r\n"
154 " c go to character mode\r\n"
155 " z suspend telnet\r\n"
156 " e exit telnet\r\n");
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000157
Eric Andersen28c70b32000-06-14 20:42:57 +0000158 if (read(0, &b, 1) <= 0)
159 doexit(1);
160
161 switch (b)
162 {
163 case 'l':
164 if (!G.gotsig)
165 {
166 do_linemode();
167 goto rrturn;
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000168 }
Eric Andersen28c70b32000-06-14 20:42:57 +0000169 break;
170 case 'c':
171 if (G.gotsig)
172 {
173 will_charmode();
174 goto rrturn;
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000175 }
Eric Andersen28c70b32000-06-14 20:42:57 +0000176 break;
177 case 'z':
178 cookmode();
179 kill(0, SIGTSTP);
180 rawmode();
181 break;
182 case 'e':
183 doexit(0);
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000184 }
Eric Andersen28c70b32000-06-14 20:42:57 +0000185
186 WriteCS(1, "continuing...\r\n");
187
188 if (G.gotsig)
189 cookmode();
190
191 rrturn:
192 G.gotsig = 0;
193
194}
195static void handlenetoutput()
196{
197 /* here we could do smart tricks how to handle 0xFF:s in output
198 * stream like writing twice every sequence of FF:s (thus doing
199 * many write()s. But I think interactive telnet application does
200 * not need to be 100% 8-bit clean, so changing every 0xff:s to
201 * 0x7f:s */
202
203 int i;
204 byte * p = G.buf;
205
206 for (i = G.len; i > 0; i--, p++)
207 {
208 if (*p == 0x1d)
209 {
210 conescape();
211 return;
212 }
213 if (*p == 0xff)
214 *p = 0x7f;
215 }
216 write(G.netfd, G.buf, G.len);
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000217}
218
Eric Andersen28c70b32000-06-14 20:42:57 +0000219
220static void handlenetinput()
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000221{
Eric Andersen28c70b32000-06-14 20:42:57 +0000222 int i;
223 int cstart = 0;
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000224
Eric Andersen28c70b32000-06-14 20:42:57 +0000225 for (i = 0; i < G.len; i++)
226 {
227 byte c = G.buf[i];
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000228
Eric Andersen28c70b32000-06-14 20:42:57 +0000229 if (G.telstate == 0) /* most of the time state == 0 */
230 {
231 if (c == IAC)
232 {
233 cstart = i;
234 G.telstate = TS_IAC;
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000235 }
236 }
Eric Andersen28c70b32000-06-14 20:42:57 +0000237 else
238 switch (G.telstate)
239 {
240 case TS_0:
241 if (c == IAC)
242 G.telstate = TS_IAC;
243 else
244 G.buf[cstart++] = c;
245 break;
246
247 case TS_IAC:
248 if (c == IAC) /* IAC IAC -> 0xFF */
249 {
250 G.buf[cstart++] = c;
251 G.telstate = TS_0;
252 break;
253 }
254 /* else */
255 switch (c)
256 {
257 case SB:
258 G.telstate = TS_SUB1;
259 break;
260 case DO:
261 case DONT:
262 case WILL:
263 case WONT:
264 G.telwish = c;
265 G.telstate = TS_OPT;
266 break;
267 default:
268 G.telstate = TS_0; /* DATA MARK must be added later */
269 }
270 break;
271 case TS_OPT: /* WILL, WONT, DO, DONT */
272 telopt(c);
273 G.telstate = TS_0;
274 break;
275 case TS_SUB1: /* Subnegotiation */
276 case TS_SUB2: /* Subnegotiation */
277 if (subneg(c) == TRUE)
278 G.telstate = TS_0;
279 break;
280 }
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000281 }
Eric Andersen28c70b32000-06-14 20:42:57 +0000282 if (G.telstate)
283 {
284 if (G.iaclen) iacflush();
285 if (G.telstate == TS_0) G.telstate = 0;
286
287 G.len = cstart;
288 }
289
290 if (G.len)
291 write(1, G.buf, G.len);
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000292}
293
Eric Andersen28c70b32000-06-14 20:42:57 +0000294
295/* ******************************* */
296
297static inline void putiac(int c)
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000298{
Eric Andersen28c70b32000-06-14 20:42:57 +0000299 G.iacbuf[G.iaclen++] = c;
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000300}
301
Eric Andersen28c70b32000-06-14 20:42:57 +0000302
303static void putiac2(byte wwdd, byte c)
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000304{
Eric Andersen28c70b32000-06-14 20:42:57 +0000305 if (G.iaclen + 3 > IACBUFSIZE)
306 iacflush();
307
308 putiac(IAC);
309 putiac(wwdd);
310 putiac(c);
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000311}
312
Eric Andersen28c70b32000-06-14 20:42:57 +0000313#if 0
314static void putiac1(byte c)
315{
316 if (G.iaclen + 2 > IACBUFSIZE)
317 iacflush();
318
319 putiac(IAC);
320 putiac(c);
321}
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000322#endif
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000323
Eric Andersen28c70b32000-06-14 20:42:57 +0000324/* void putiacstring (subneg strings) */
325
326/* ******************************* */
327
328char const escapecharis[] = "\r\nEscape character is ";
329
330static void setConMode()
331{
332 if (G.telflags & UF_ECHO)
333 {
334 if (G.charmode == CHM_TRY) {
335 G.charmode = CHM_ON;
Matt Kraai12f417e2001-01-18 02:57:08 +0000336 printf("\r\nEntering character mode%s'^]'.\r\n", escapecharis);
Eric Andersen28c70b32000-06-14 20:42:57 +0000337 rawmode();
338 }
339 }
340 else
341 {
342 if (G.charmode != CHM_OFF) {
343 G.charmode = CHM_OFF;
Matt Kraai12f417e2001-01-18 02:57:08 +0000344 printf("\r\nEntering line mode%s'^C'.\r\n", escapecharis);
Eric Andersen28c70b32000-06-14 20:42:57 +0000345 cookmode();
346 }
347 }
348}
349
350/* ******************************* */
351
352static void will_charmode()
353{
354 G.charmode = CHM_TRY;
355 G.telflags |= (UF_ECHO | UF_SGA);
356 setConMode();
357
358 putiac2(DO, TELOPT_ECHO);
359 putiac2(DO, TELOPT_SGA);
360 iacflush();
361}
362
363static void do_linemode()
364{
365 G.charmode = CHM_TRY;
366 G.telflags &= ~(UF_ECHO | UF_SGA);
367 setConMode();
368
369 putiac2(DONT, TELOPT_ECHO);
370 putiac2(DONT, TELOPT_SGA);
371 iacflush();
372}
373
374/* ******************************* */
375
376static inline void to_notsup(char c)
377{
378 if (G.telwish == WILL) putiac2(DONT, c);
379 else if (G.telwish == DO) putiac2(WONT, c);
380}
381
382static inline void to_echo()
383{
384 /* if server requests ECHO, don't agree */
385 if (G.telwish == DO) { putiac2(WONT, TELOPT_ECHO); return; }
386 else if (G.telwish == DONT) return;
387
388 if (G.telflags & UF_ECHO)
389 {
390 if (G.telwish == WILL)
391 return;
392 }
393 else
394 if (G.telwish == WONT)
395 return;
396
397 if (G.charmode != CHM_OFF)
398 G.telflags ^= UF_ECHO;
399
400 if (G.telflags & UF_ECHO)
401 putiac2(DO, TELOPT_ECHO);
402 else
403 putiac2(DONT, TELOPT_ECHO);
404
405 setConMode();
406 WriteCS(1, "\r\n"); /* sudden modec */
407}
408
409static inline void to_sga()
410{
411 /* daemon always sends will/wont, client do/dont */
412
413 if (G.telflags & UF_SGA)
414 {
415 if (G.telwish == WILL)
416 return;
417 }
418 else
419 if (G.telwish == WONT)
420 return;
421
422 if ((G.telflags ^= UF_SGA) & UF_SGA) /* toggle */
423 putiac2(DO, TELOPT_SGA);
424 else
425 putiac2(DONT, TELOPT_SGA);
426
427 return;
428}
429
430static void telopt(byte c)
431{
432 switch (c)
433 {
434 case TELOPT_ECHO: to_echo(c); break;
435 case TELOPT_SGA: to_sga(c); break;
436 default: to_notsup(c); break;
437 }
438}
439
440
441/* ******************************* */
442
443/* subnegotiation -- ignore all */
444
445static int subneg(byte c)
446{
447 switch (G.telstate)
448 {
449 case TS_SUB1:
450 if (c == IAC)
451 G.telstate = TS_SUB2;
452 break;
453 case TS_SUB2:
454 if (c == SE)
455 return TRUE;
456 G.telstate = TS_SUB1;
457 /* break; */
458 }
459 return FALSE;
460}
461
462/* ******************************* */
463
464static void fgotsig(int sig)
465{
466 G.gotsig = sig;
467}
468
469
470static void rawmode()
471{
472 tcsetattr(0, TCSADRAIN, &G.termios_raw);
473}
474
475static void cookmode()
476{
477 tcsetattr(0, TCSADRAIN, &G.termios_def);
478}
479
480extern int telnet_main(int argc, char** argv)
481{
482 struct in_addr host;
483 int port;
484#ifdef USE_POLL
485 struct pollfd ufds[2];
486#else
487 fd_set readfds;
488 int maxfd;
489#endif
490
491
492 memset(&G, 0, sizeof G);
493
494 if (tcgetattr(0, &G.termios_def) < 0)
495 exit(1);
496
497 G.termios_raw = G.termios_def;
498
499 cfmakeraw(&G.termios_raw);
500
Eric Andersen67991cf2001-02-14 21:23:06 +0000501 if (argc < 2) show_usage();
Eric Andersen28c70b32000-06-14 20:42:57 +0000502 port = (argc > 2)? getport(argv[2]): 23;
503
504 G.buf = xmalloc(DATABUFSIZE);
505 G.iacbuf = xmalloc(IACBUFSIZE);
506
507 host = getserver(argv[1]);
508
509 G.netfd = remote_connect(host, port);
510
511 signal(SIGINT, fgotsig);
512
513#ifdef USE_POLL
514 ufds[0].fd = 0; ufds[1].fd = G.netfd;
515 ufds[0].events = ufds[1].events = POLLIN;
516#else
517 FD_ZERO(&readfds);
518 FD_SET(0, &readfds);
519 FD_SET(G.netfd, &readfds);
520 maxfd = G.netfd + 1;
521#endif
522
523 while (1)
524 {
525#ifndef USE_POLL
526 fd_set rfds = readfds;
527
528 switch (select(maxfd, &rfds, NULL, NULL, NULL))
529#else
530 switch (poll(ufds, 2, -1))
531#endif
532 {
533 case 0:
534 /* timeout */
535 case -1:
536 /* error, ignore and/or log something, bay go to loop */
537 if (G.gotsig)
538 conescape();
539 else
540 sleep(1);
541 break;
542 default:
543
544#ifdef USE_POLL
545 if (ufds[0].revents) /* well, should check POLLIN, but ... */
546#else
547 if (FD_ISSET(0, &rfds))
548#endif
549 {
550 G.len = read(0, G.buf, DATABUFSIZE);
551
552 if (G.len <= 0)
553 doexit(0);
554
555 TRACE(0, ("Read con: %d\n", G.len));
556
557 handlenetoutput();
558 }
559
560#ifdef USE_POLL
561 if (ufds[1].revents) /* well, should check POLLIN, but ... */
562#else
563 if (FD_ISSET(G.netfd, &rfds))
564#endif
565 {
566 G.len = read(G.netfd, G.buf, DATABUFSIZE);
567
568 if (G.len <= 0)
569 {
570 WriteCS(1, "Connection closed by foreign host.\r\n");
571 doexit(1);
572 }
573 TRACE(0, ("Read netfd (%d): %d\n", G.netfd, G.len));
574
575 handlenetinput();
576 }
577 }
578 }
579}
580
581static int getport(char * p)
582{
583 unsigned int port = atoi(p);
584
585 if ((unsigned)(port - 1 ) > 65534)
586 {
Matt Kraaidd19c692001-01-31 19:00:21 +0000587 error_msg_and_die("%s: bad port number", p);
Eric Andersen28c70b32000-06-14 20:42:57 +0000588 }
589 return port;
590}
591
592static struct in_addr getserver(char * host)
593{
594 struct in_addr addr;
595
596 struct hostent * he;
597 if ((he = gethostbyname(host)) == NULL)
598 {
Matt Kraaidd19c692001-01-31 19:00:21 +0000599 error_msg_and_die("%s: Unknown host", host);
Eric Andersen28c70b32000-06-14 20:42:57 +0000600 }
601 memcpy(&addr, he->h_addr, sizeof addr);
602
603 TRACE(1, ("addr: %s\n", inet_ntoa(addr)));
604
605 return addr;
606}
607
608static int create_socket()
609{
610 return socket(AF_INET, SOCK_STREAM, 0);
611}
612
613static void setup_sockaddr_in(struct sockaddr_in * addr, int port)
614{
615 memset(addr, 0, sizeof addr);
616 addr->sin_family = AF_INET;
617 addr->sin_port = htons(port);
618}
619
620#if 0
621static int local_bind(int port)
622{
623 struct sockaddr_in s_addr;
624 int s = create_socket();
625
626 setup_sockaddr_in(&s_addr, port);
627
628 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &one, sizeof one);
629
630 if (bind(s, &s_addr, sizeof s_addr) < 0)
631 {
632 char * e = sys_errlist[errno];
633 syserrorexit("bind");
634 exit(1);
635 }
636 listen(s, 1);
637
638 return s;
639}
640#endif
641
642static int remote_connect(struct in_addr addr, int port)
643{
644 struct sockaddr_in s_addr;
645 int s = create_socket();
646
647 setup_sockaddr_in(&s_addr, port);
648 s_addr.sin_addr = addr;
649
650 setsockopt(s, SOL_SOCKET, SO_KEEPALIVE, &one, sizeof one);
651
652 if (connect(s, (struct sockaddr *)&s_addr, sizeof s_addr) < 0)
653 {
Matt Kraai1fa1ade2000-12-18 03:57:16 +0000654 perror_msg_and_die("Unable to connect to remote host");
Eric Andersen28c70b32000-06-14 20:42:57 +0000655 }
656 return s;
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000657}
658
659/*
Eric Andersen28c70b32000-06-14 20:42:57 +0000660Local Variables:
661c-file-style: "linux"
662c-basic-offset: 4
663tab-width: 4
664End:
665*/
666