1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72#include <arpa/telnet.h>
73#include <netinet/in.h>
74#include "libbb.h"
75#include "common_bufsiz.h"
76
77#ifdef __BIONIC__
78
79# define IAC 255
80# define DONT 254
81# define DO 253
82# define WONT 252
83# define WILL 251
84# define SB 250
85# define SE 240
86# define TELOPT_ECHO 1
87# define TELOPT_SGA 3
88# define TELOPT_TTYPE 24
89# define TELOPT_NAWS 31
90#endif
91
92enum {
93 DATABUFSIZE = 128,
94 IACBUFSIZE = 128,
95
96 CHM_TRY = 0,
97 CHM_ON = 1,
98 CHM_OFF = 2,
99
100 UF_ECHO = 0x01,
101 UF_SGA = 0x02,
102
103 TS_NORMAL = 0,
104 TS_COPY = 1,
105 TS_IAC = 2,
106 TS_OPT = 3,
107 TS_SUB1 = 4,
108 TS_SUB2 = 5,
109 TS_CR = 6,
110};
111
112typedef unsigned char byte;
113
114enum { netfd = 3 };
115
116struct globals {
117 int iaclen;
118 byte telstate;
119 byte telwish;
120 byte charmode;
121 byte telflags;
122 byte do_termios;
123#if ENABLE_FEATURE_TELNET_TTYPE
124 char *ttype;
125#endif
126#if ENABLE_FEATURE_TELNET_AUTOLOGIN
127 const char *autologin;
128#endif
129#if ENABLE_FEATURE_TELNET_WIDTH
130 unsigned win_width, win_height;
131#endif
132
133 char buf[DATABUFSIZE];
134
135 char iacbuf[IACBUFSIZE];
136 struct termios termios_def;
137 struct termios termios_raw;
138} FIX_ALIASING;
139#define G (*(struct globals*)bb_common_bufsiz1)
140#define INIT_G() do { \
141 setup_common_bufsiz(); \
142 BUILD_BUG_ON(sizeof(G) > COMMON_BUFSIZE); \
143} while (0)
144
145
146static void rawmode(void);
147static void cookmode(void);
148static void do_linemode(void);
149static void will_charmode(void);
150static void telopt(byte c);
151static void subneg(byte c);
152
153static void iac_flush(void)
154{
155 if (G.iaclen != 0) {
156 full_write(netfd, G.iacbuf, G.iaclen);
157 G.iaclen = 0;
158 }
159}
160
161static void doexit(int ev) NORETURN;
162static void doexit(int ev)
163{
164 cookmode();
165 exit(ev);
166}
167
168static void con_escape(void)
169{
170 char b;
171
172 if (bb_got_signal)
173 rawmode();
174
175 full_write1_str("\r\nConsole escape. Commands are:\r\n\n"
176 " l go to line mode\r\n"
177 " c go to character mode\r\n"
178 " z suspend telnet\r\n"
179 " e exit telnet\r\n");
180
181 if (read(STDIN_FILENO, &b, 1) <= 0)
182 doexit(EXIT_FAILURE);
183
184 switch (b) {
185 case 'l':
186 if (!bb_got_signal) {
187 do_linemode();
188 goto ret;
189 }
190 break;
191 case 'c':
192 if (bb_got_signal) {
193 will_charmode();
194 goto ret;
195 }
196 break;
197 case 'z':
198 cookmode();
199 kill(0, SIGTSTP);
200 rawmode();
201 break;
202 case 'e':
203 doexit(EXIT_SUCCESS);
204 }
205
206 full_write1_str("continuing...\r\n");
207
208 if (bb_got_signal)
209 cookmode();
210 ret:
211 bb_got_signal = 0;
212}
213
214static void handle_net_output(int len)
215{
216 byte outbuf[2 * DATABUFSIZE];
217 byte *dst = outbuf;
218 byte *src = (byte*)G.buf;
219 byte *end = src + len;
220
221 while (src < end) {
222 byte c = *src++;
223 if (c == 0x1d) {
224 con_escape();
225 return;
226 }
227 *dst = c;
228 if (c == IAC)
229 *++dst = c;
230 else
231 if (c == '\r' || c == '\n') {
232
233
234
235
236
237
238 *dst = '\r';
239 *++dst = '\n';
240 }
241#if 0
242
243
244 if (c == 0x08 || c == 0x7f) {
245 *dst = IAC;
246 *++dst = EC;
247 }
248 if (c == 0x03) {
249 *dst = IAC;
250 *++dst = IP;
251 }
252#endif
253 dst++;
254 }
255 if (dst - outbuf != 0)
256 full_write(netfd, outbuf, dst - outbuf);
257}
258
259static void handle_net_input(int len)
260{
261 byte c;
262 int i;
263 int cstart = 0;
264
265 i = 0;
266
267 if (G.telstate == TS_NORMAL) {
268 while (i < len) {
269 c = G.buf[i];
270 i++;
271 if (c == IAC)
272 goto got_IAC;
273 if (c != '\r')
274 continue;
275 G.telstate = TS_CR;
276 cstart = i;
277 goto got_special;
278 }
279 full_write(STDOUT_FILENO, G.buf, len);
280 return;
281 got_IAC:
282 G.telstate = TS_IAC;
283 cstart = i - 1;
284 got_special: ;
285 }
286
287 for (; i < len; i++) {
288 c = G.buf[i];
289
290 switch (G.telstate) {
291 case TS_CR:
292
293
294
295 G.telstate = TS_COPY;
296 if (c == '\0')
297 break;
298
299
300 case TS_COPY:
301
302 if (c == IAC)
303 G.telstate = TS_IAC;
304 else {
305 G.buf[cstart++] = c;
306 if (c == '\r')
307 G.telstate = TS_CR;
308 }
309 break;
310
311 case TS_IAC:
312 switch (c) {
313 case IAC:
314 G.buf[cstart++] = c;
315 G.telstate = TS_COPY;
316 break;
317 case SB:
318 G.telstate = TS_SUB1;
319 break;
320 case DO:
321 case DONT:
322 case WILL:
323 case WONT:
324 G.telwish = c;
325 G.telstate = TS_OPT;
326 break;
327
328 default:
329 G.telstate = TS_COPY;
330 }
331 break;
332
333 case TS_OPT:
334 telopt(c);
335 G.telstate = TS_COPY;
336 break;
337
338 case TS_SUB1:
339 case TS_SUB2:
340 subneg(c);
341 break;
342 }
343 }
344
345
346 iac_flush();
347 if (G.telstate == TS_COPY)
348 G.telstate = TS_NORMAL;
349 if (cstart != 0)
350 full_write(STDOUT_FILENO, G.buf, cstart);
351}
352
353static void put_iac(int c)
354{
355 int iaclen = G.iaclen;
356 if (iaclen >= IACBUFSIZE) {
357 iac_flush();
358 iaclen = 0;
359 }
360 G.iacbuf[iaclen] = c;
361 G.iaclen = iaclen + 1;
362}
363
364static void put_iac2_msb_lsb(unsigned x_y)
365{
366 put_iac(x_y >> 8);
367 put_iac(x_y);
368}
369#define put_iac2_x_y(x,y) put_iac2_msb_lsb(((x)<<8) + (y))
370
371#if ENABLE_FEATURE_TELNET_WIDTH \
372 || ENABLE_FEATURE_TELNET_TTYPE \
373 || ENABLE_FEATURE_TELNET_AUTOLOGIN
374static void put_iac4_msb_lsb(unsigned x_y_z_t)
375{
376 put_iac2_msb_lsb(x_y_z_t >> 16);
377 put_iac2_msb_lsb(x_y_z_t);
378}
379#define put_iac4_x_y_z_t(x,y,z,t) put_iac4_msb_lsb(((x)<<24) + ((y)<<16) + ((z)<<8) + (t))
380#endif
381
382static void put_iac3_IAC_x_y_merged(unsigned wwdd_and_c)
383{
384 put_iac(IAC);
385 put_iac2_msb_lsb(wwdd_and_c);
386}
387#define put_iac3_IAC_x_y(wwdd,c) put_iac3_IAC_x_y_merged(((wwdd)<<8) + (c))
388
389#if ENABLE_FEATURE_TELNET_TTYPE
390static void put_iac_subopt(byte c, char *str)
391{
392 put_iac4_x_y_z_t(IAC, SB, c, 0);
393
394 while (*str)
395 put_iac(*str++);
396
397 put_iac2_x_y(IAC, SE);
398}
399#endif
400
401#if ENABLE_FEATURE_TELNET_AUTOLOGIN
402static void put_iac_subopt_autologin(void)
403{
404 const char *p;
405
406 put_iac4_x_y_z_t(IAC, SB, TELOPT_NEW_ENVIRON, TELQUAL_IS);
407 put_iac4_x_y_z_t(NEW_ENV_VAR, 'U', 'S', 'E');
408 put_iac2_x_y('R', NEW_ENV_VALUE);
409
410 p = G.autologin;
411 while (*p)
412 put_iac(*p++);
413
414 put_iac2_x_y(IAC, SE);
415}
416#endif
417
418#if ENABLE_FEATURE_TELNET_WIDTH
419static void put_iac_naws(byte c, int x, int y)
420{
421 put_iac3_IAC_x_y(SB, c);
422
423 put_iac4_msb_lsb((x << 16) + y);
424
425 put_iac2_x_y(IAC, SE);
426}
427#endif
428
429static void setConMode(void)
430{
431 if (G.telflags & UF_ECHO) {
432 if (G.charmode == CHM_TRY) {
433 G.charmode = CHM_ON;
434 printf("\r\nEntering %s mode"
435 "\r\nEscape character is '^%c'.\r\n", "character", ']');
436 rawmode();
437 }
438 } else {
439 if (G.charmode != CHM_OFF) {
440 G.charmode = CHM_OFF;
441 printf("\r\nEntering %s mode"
442 "\r\nEscape character is '^%c'.\r\n", "line", 'C');
443 cookmode();
444 }
445 }
446}
447
448static void will_charmode(void)
449{
450 G.charmode = CHM_TRY;
451 G.telflags |= (UF_ECHO | UF_SGA);
452 setConMode();
453
454 put_iac3_IAC_x_y(DO, TELOPT_ECHO);
455 put_iac3_IAC_x_y(DO, TELOPT_SGA);
456 iac_flush();
457}
458
459static void do_linemode(void)
460{
461 G.charmode = CHM_TRY;
462 G.telflags &= ~(UF_ECHO | UF_SGA);
463 setConMode();
464
465 put_iac3_IAC_x_y(DONT, TELOPT_ECHO);
466 put_iac3_IAC_x_y(DONT, TELOPT_SGA);
467 iac_flush();
468}
469
470static void to_notsup(char c)
471{
472 if (G.telwish == WILL)
473 put_iac3_IAC_x_y(DONT, c);
474 else if (G.telwish == DO)
475 put_iac3_IAC_x_y(WONT, c);
476}
477
478static void to_echo(void)
479{
480
481 if (G.telwish == DO) {
482 put_iac3_IAC_x_y(WONT, TELOPT_ECHO);
483 return;
484 }
485 if (G.telwish == DONT)
486 return;
487
488 if (G.telflags & UF_ECHO) {
489 if (G.telwish == WILL)
490 return;
491 } else if (G.telwish == WONT)
492 return;
493
494 if (G.charmode != CHM_OFF)
495 G.telflags ^= UF_ECHO;
496
497 if (G.telflags & UF_ECHO)
498 put_iac3_IAC_x_y(DO, TELOPT_ECHO);
499 else
500 put_iac3_IAC_x_y(DONT, TELOPT_ECHO);
501
502 setConMode();
503 full_write1_str("\r\n");
504}
505
506static void to_sga(void)
507{
508
509
510 if (G.telflags & UF_SGA) {
511 if (G.telwish == WILL)
512 return;
513 } else if (G.telwish == WONT)
514 return;
515
516 G.telflags ^= UF_SGA;
517 if (G.telflags & UF_SGA)
518 put_iac3_IAC_x_y(DO, TELOPT_SGA);
519 else
520 put_iac3_IAC_x_y(DONT, TELOPT_SGA);
521}
522
523#if ENABLE_FEATURE_TELNET_TTYPE
524static void to_ttype(void)
525{
526
527 if (G.ttype)
528 put_iac3_IAC_x_y(WILL, TELOPT_TTYPE);
529 else
530 put_iac3_IAC_x_y(WONT, TELOPT_TTYPE);
531}
532#endif
533
534#if ENABLE_FEATURE_TELNET_AUTOLOGIN
535static void to_new_environ(void)
536{
537
538 if (G.autologin)
539 put_iac3_IAC_x_y(WILL, TELOPT_NEW_ENVIRON);
540 else
541 put_iac3_IAC_x_y(WONT, TELOPT_NEW_ENVIRON);
542}
543#endif
544
545#if ENABLE_FEATURE_TELNET_WIDTH
546static void to_naws(void)
547{
548
549 put_iac3_IAC_x_y(WILL, TELOPT_NAWS);
550}
551#endif
552
553static void telopt(byte c)
554{
555 switch (c) {
556 case TELOPT_ECHO:
557 to_echo(); break;
558 case TELOPT_SGA:
559 to_sga(); break;
560#if ENABLE_FEATURE_TELNET_TTYPE
561 case TELOPT_TTYPE:
562 to_ttype(); break;
563#endif
564#if ENABLE_FEATURE_TELNET_AUTOLOGIN
565 case TELOPT_NEW_ENVIRON:
566 to_new_environ(); break;
567#endif
568#if ENABLE_FEATURE_TELNET_WIDTH
569 case TELOPT_NAWS:
570 to_naws();
571 put_iac_naws(c, G.win_width, G.win_height);
572 break;
573#endif
574 default:
575 to_notsup(c);
576 break;
577 }
578}
579
580
581static void subneg(byte c)
582{
583 switch (G.telstate) {
584 case TS_SUB1:
585 if (c == IAC)
586 G.telstate = TS_SUB2;
587#if ENABLE_FEATURE_TELNET_TTYPE
588 else
589 if (c == TELOPT_TTYPE && G.ttype)
590 put_iac_subopt(TELOPT_TTYPE, G.ttype);
591#endif
592#if ENABLE_FEATURE_TELNET_AUTOLOGIN
593 else
594 if (c == TELOPT_NEW_ENVIRON && G.autologin)
595 put_iac_subopt_autologin();
596#endif
597 break;
598 case TS_SUB2:
599 if (c == SE) {
600 G.telstate = TS_COPY;
601 return;
602 }
603 G.telstate = TS_SUB1;
604 break;
605 }
606}
607
608static void rawmode(void)
609{
610 if (G.do_termios)
611 tcsetattr(0, TCSADRAIN, &G.termios_raw);
612}
613
614static void cookmode(void)
615{
616 if (G.do_termios)
617 tcsetattr(0, TCSADRAIN, &G.termios_def);
618}
619
620int telnet_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
621int telnet_main(int argc UNUSED_PARAM, char **argv)
622{
623 char *host;
624 int port;
625 int len;
626 struct pollfd ufds[2];
627
628 INIT_G();
629
630#if ENABLE_FEATURE_TELNET_TTYPE
631 G.ttype = getenv("TERM");
632#endif
633
634 if (tcgetattr(0, &G.termios_def) >= 0) {
635 G.do_termios = 1;
636 G.termios_raw = G.termios_def;
637 cfmakeraw(&G.termios_raw);
638 }
639
640#if ENABLE_FEATURE_TELNET_AUTOLOGIN
641 if (1 == getopt32(argv, "al:", &G.autologin)) {
642
643 G.autologin = getenv("USER");
644 }
645 argv += optind;
646#else
647 argv++;
648#endif
649 if (!*argv)
650 bb_show_usage();
651 host = *argv++;
652 port = *argv ? bb_lookup_port(*argv++, "tcp", 23)
653 : bb_lookup_std_port("telnet", "tcp", 23);
654 if (*argv)
655 bb_show_usage();
656
657 xmove_fd(create_and_connect_stream_or_die(host, port), netfd);
658 printf("Connected to %s\n", host);
659
660 setsockopt_keepalive(netfd);
661
662#if ENABLE_FEATURE_TELNET_WIDTH
663 get_terminal_width_height(0, &G.win_width, &G.win_height);
664
665#endif
666
667 signal(SIGINT, record_signo);
668
669 ufds[0].fd = STDIN_FILENO;
670 ufds[0].events = POLLIN;
671 ufds[1].fd = netfd;
672 ufds[1].events = POLLIN;
673
674 while (1) {
675 if (poll(ufds, 2, -1) < 0) {
676
677 if (bb_got_signal)
678 con_escape();
679 else
680 sleep1();
681 continue;
682 }
683
684
685
686 if (ufds[0].revents) {
687 len = safe_read(STDIN_FILENO, G.buf, DATABUFSIZE);
688 if (len <= 0)
689 doexit(EXIT_SUCCESS);
690 handle_net_output(len);
691 }
692
693 if (ufds[1].revents) {
694 len = safe_read(netfd, G.buf, DATABUFSIZE);
695 if (len <= 0) {
696 full_write1_str("Connection closed by foreign host\r\n");
697 doexit(EXIT_FAILURE);
698 }
699 handle_net_input(len);
700 }
701 }
702}
703