telnet.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695
  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 GPLv2 or later, see file LICENSE in this source tree.
  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. //config:config TELNET
  23. //config: bool "telnet (8.8 kb)"
  24. //config: default y
  25. //config: help
  26. //config: Telnet is an interface to the TELNET protocol, but is also commonly
  27. //config: used to test other simple protocols.
  28. //config:
  29. //config:config FEATURE_TELNET_TTYPE
  30. //config: bool "Pass TERM type to remote host"
  31. //config: default y
  32. //config: depends on TELNET
  33. //config: help
  34. //config: Setting this option will forward the TERM environment variable to the
  35. //config: remote host you are connecting to. This is useful to make sure that
  36. //config: things like ANSI colors and other control sequences behave.
  37. //config:
  38. //config:config FEATURE_TELNET_AUTOLOGIN
  39. //config: bool "Pass USER type to remote host"
  40. //config: default y
  41. //config: depends on TELNET
  42. //config: help
  43. //config: Setting this option will forward the USER environment variable to the
  44. //config: remote host you are connecting to. This is useful when you need to
  45. //config: log into a machine without telling the username (autologin). This
  46. //config: option enables '-a' and '-l USER' options.
  47. //config:
  48. //config:config FEATURE_TELNET_WIDTH
  49. //config: bool "Enable window size autodetection"
  50. //config: default y
  51. //config: depends on TELNET
  52. //applet:IF_TELNET(APPLET(telnet, BB_DIR_USR_BIN, BB_SUID_DROP))
  53. //kbuild:lib-$(CONFIG_TELNET) += telnet.o
  54. //usage:#if ENABLE_FEATURE_TELNET_AUTOLOGIN
  55. //usage:#define telnet_trivial_usage
  56. //usage: "[-a] [-l USER] HOST [PORT]"
  57. //usage:#define telnet_full_usage "\n\n"
  58. //usage: "Connect to telnet server\n"
  59. //usage: "\n -a Automatic login with $USER variable"
  60. //usage: "\n -l USER Automatic login as USER"
  61. //usage:
  62. //usage:#else
  63. //usage:#define telnet_trivial_usage
  64. //usage: "HOST [PORT]"
  65. //usage:#define telnet_full_usage "\n\n"
  66. //usage: "Connect to telnet server"
  67. //usage:#endif
  68. #include <arpa/telnet.h>
  69. #include <netinet/in.h>
  70. #include "libbb.h"
  71. #include "common_bufsiz.h"
  72. #ifdef __BIONIC__
  73. /* should be in arpa/telnet.h */
  74. # define IAC 255 /* interpret as command: */
  75. # define DONT 254 /* you are not to use option */
  76. # define DO 253 /* please, you use option */
  77. # define WONT 252 /* I won't use option */
  78. # define WILL 251 /* I will use option */
  79. # define SB 250 /* interpret as subnegotiation */
  80. # define SE 240 /* end sub negotiation */
  81. # define TELOPT_ECHO 1 /* echo */
  82. # define TELOPT_SGA 3 /* suppress go ahead */
  83. # define TELOPT_TTYPE 24 /* terminal type */
  84. # define TELOPT_NAWS 31 /* window size */
  85. #endif
  86. enum {
  87. DATABUFSIZE = 128,
  88. IACBUFSIZE = 128,
  89. CHM_TRY = 0,
  90. CHM_ON = 1,
  91. CHM_OFF = 2,
  92. UF_ECHO = 0x01,
  93. UF_SGA = 0x02,
  94. TS_NORMAL = 0,
  95. TS_COPY = 1,
  96. TS_IAC = 2,
  97. TS_OPT = 3,
  98. TS_SUB1 = 4,
  99. TS_SUB2 = 5,
  100. TS_CR = 6,
  101. };
  102. typedef unsigned char byte;
  103. enum { netfd = 3 };
  104. struct globals {
  105. int iaclen; /* could even use byte, but it's a loss on x86 */
  106. byte telstate; /* telnet negotiation state from network input */
  107. byte telwish; /* DO, DONT, WILL, WONT */
  108. byte charmode;
  109. byte telflags;
  110. byte do_termios;
  111. #if ENABLE_FEATURE_TELNET_TTYPE
  112. char *ttype;
  113. #endif
  114. #if ENABLE_FEATURE_TELNET_AUTOLOGIN
  115. const char *autologin;
  116. #endif
  117. #if ENABLE_FEATURE_TELNET_WIDTH
  118. unsigned win_width, win_height;
  119. #endif
  120. /* same buffer used both for network and console read/write */
  121. char buf[DATABUFSIZE];
  122. /* buffer to handle telnet negotiations */
  123. char iacbuf[IACBUFSIZE];
  124. struct termios termios_def;
  125. struct termios termios_raw;
  126. } FIX_ALIASING;
  127. #define G (*(struct globals*)bb_common_bufsiz1)
  128. #define INIT_G() do { \
  129. setup_common_bufsiz(); \
  130. BUILD_BUG_ON(sizeof(G) > COMMON_BUFSIZE); \
  131. } while (0)
  132. static void rawmode(void);
  133. static void cookmode(void);
  134. static void do_linemode(void);
  135. static void will_charmode(void);
  136. static void telopt(byte c);
  137. static void subneg(byte c);
  138. static void iac_flush(void)
  139. {
  140. full_write(netfd, G.iacbuf, G.iaclen);
  141. G.iaclen = 0;
  142. }
  143. static void doexit(int ev) NORETURN;
  144. static void doexit(int ev)
  145. {
  146. cookmode();
  147. exit(ev);
  148. }
  149. static void con_escape(void)
  150. {
  151. char b;
  152. if (bb_got_signal) /* came from line mode... go raw */
  153. rawmode();
  154. full_write1_str("\r\nConsole escape. Commands are:\r\n\n"
  155. " l go to line mode\r\n"
  156. " c go to character mode\r\n"
  157. " z suspend telnet\r\n"
  158. " e exit telnet\r\n");
  159. if (read(STDIN_FILENO, &b, 1) <= 0)
  160. doexit(EXIT_FAILURE);
  161. switch (b) {
  162. case 'l':
  163. if (!bb_got_signal) {
  164. do_linemode();
  165. goto ret;
  166. }
  167. break;
  168. case 'c':
  169. if (bb_got_signal) {
  170. will_charmode();
  171. goto ret;
  172. }
  173. break;
  174. case 'z':
  175. cookmode();
  176. kill(0, SIGTSTP);
  177. rawmode();
  178. break;
  179. case 'e':
  180. doexit(EXIT_SUCCESS);
  181. }
  182. full_write1_str("continuing...\r\n");
  183. if (bb_got_signal)
  184. cookmode();
  185. ret:
  186. bb_got_signal = 0;
  187. }
  188. static void handle_net_output(int len)
  189. {
  190. byte outbuf[2 * DATABUFSIZE];
  191. byte *dst = outbuf;
  192. byte *src = (byte*)G.buf;
  193. byte *end = src + len;
  194. while (src < end) {
  195. byte c = *src++;
  196. if (c == 0x1d) {
  197. con_escape();
  198. return;
  199. }
  200. *dst = c;
  201. if (c == IAC)
  202. *++dst = c; /* IAC -> IAC IAC */
  203. else
  204. if (c == '\r' || c == '\n') {
  205. /* Enter key sends '\r' in raw mode and '\n' in cooked one.
  206. *
  207. * See RFC 1123 3.3.1 Telnet End-of-Line Convention.
  208. * Using CR LF instead of other allowed possibilities
  209. * like CR NUL - easier to talk to HTTP/SMTP servers.
  210. */
  211. *dst = '\r'; /* Enter -> CR LF */
  212. *++dst = '\n';
  213. }
  214. dst++;
  215. }
  216. if (dst - outbuf != 0)
  217. full_write(netfd, outbuf, dst - outbuf);
  218. }
  219. static void handle_net_input(int len)
  220. {
  221. int i;
  222. int cstart = 0;
  223. for (i = 0; i < len; i++) {
  224. byte c = G.buf[i];
  225. if (G.telstate == TS_NORMAL) { /* most typical state */
  226. if (c == IAC) {
  227. cstart = i;
  228. G.telstate = TS_IAC;
  229. }
  230. else if (c == '\r') {
  231. cstart = i + 1;
  232. G.telstate = TS_CR;
  233. }
  234. /* No IACs were seen so far, no need to copy
  235. * bytes within G.buf: */
  236. continue;
  237. }
  238. switch (G.telstate) {
  239. case TS_CR:
  240. /* Prev char was CR. If cur one is NUL, ignore it.
  241. * See RFC 1123 section 3.3.1 for discussion of telnet EOL handling.
  242. */
  243. G.telstate = TS_COPY;
  244. if (c == '\0')
  245. break;
  246. /* else: fall through - need to handle CR IAC ... properly */
  247. case TS_COPY: /* Prev char was ordinary */
  248. /* Similar to NORMAL, but in TS_COPY we need to copy bytes */
  249. if (c == IAC)
  250. G.telstate = TS_IAC;
  251. else
  252. G.buf[cstart++] = c;
  253. if (c == '\r')
  254. G.telstate = TS_CR;
  255. break;
  256. case TS_IAC: /* Prev char was IAC */
  257. if (c == IAC) { /* IAC IAC -> one IAC */
  258. G.buf[cstart++] = c;
  259. G.telstate = TS_COPY;
  260. break;
  261. }
  262. /* else */
  263. switch (c) {
  264. case SB:
  265. G.telstate = TS_SUB1;
  266. break;
  267. case DO:
  268. case DONT:
  269. case WILL:
  270. case WONT:
  271. G.telwish = c;
  272. G.telstate = TS_OPT;
  273. break;
  274. /* DATA MARK must be added later */
  275. default:
  276. G.telstate = TS_COPY;
  277. }
  278. break;
  279. case TS_OPT: /* Prev chars were IAC WILL/WONT/DO/DONT */
  280. telopt(c);
  281. G.telstate = TS_COPY;
  282. break;
  283. case TS_SUB1: /* Subnegotiation */
  284. case TS_SUB2: /* Subnegotiation */
  285. subneg(c); /* can change G.telstate */
  286. break;
  287. }
  288. }
  289. if (G.telstate != TS_NORMAL) {
  290. /* We had some IACs, or CR */
  291. if (G.iaclen)
  292. iac_flush();
  293. if (G.telstate == TS_COPY) /* we aren't in the middle of IAC */
  294. G.telstate = TS_NORMAL;
  295. len = cstart;
  296. }
  297. if (len)
  298. full_write(STDOUT_FILENO, G.buf, len);
  299. }
  300. static void put_iac(int c)
  301. {
  302. G.iacbuf[G.iaclen++] = c;
  303. }
  304. static void put_iac2_merged(unsigned wwdd_and_c)
  305. {
  306. if (G.iaclen + 3 > IACBUFSIZE)
  307. iac_flush();
  308. put_iac(IAC);
  309. put_iac(wwdd_and_c >> 8);
  310. put_iac(wwdd_and_c & 0xff);
  311. }
  312. #define put_iac2(wwdd,c) put_iac2_merged(((wwdd)<<8) + (c))
  313. #if ENABLE_FEATURE_TELNET_TTYPE
  314. static void put_iac_subopt(byte c, char *str)
  315. {
  316. int len = strlen(str) + 6; // ( 2 + 1 + 1 + strlen + 2 )
  317. if (G.iaclen + len > IACBUFSIZE)
  318. iac_flush();
  319. put_iac(IAC);
  320. put_iac(SB);
  321. put_iac(c);
  322. put_iac(0);
  323. while (*str)
  324. put_iac(*str++);
  325. put_iac(IAC);
  326. put_iac(SE);
  327. }
  328. #endif
  329. #if ENABLE_FEATURE_TELNET_AUTOLOGIN
  330. static void put_iac_subopt_autologin(void)
  331. {
  332. int len = strlen(G.autologin) + 6; // (2 + 1 + 1 + strlen + 2)
  333. const char *p = "USER";
  334. if (G.iaclen + len > IACBUFSIZE)
  335. iac_flush();
  336. put_iac(IAC);
  337. put_iac(SB);
  338. put_iac(TELOPT_NEW_ENVIRON);
  339. put_iac(TELQUAL_IS);
  340. put_iac(NEW_ENV_VAR);
  341. while (*p)
  342. put_iac(*p++);
  343. put_iac(NEW_ENV_VALUE);
  344. p = G.autologin;
  345. while (*p)
  346. put_iac(*p++);
  347. put_iac(IAC);
  348. put_iac(SE);
  349. }
  350. #endif
  351. #if ENABLE_FEATURE_TELNET_WIDTH
  352. static void put_iac_naws(byte c, int x, int y)
  353. {
  354. if (G.iaclen + 9 > IACBUFSIZE)
  355. iac_flush();
  356. put_iac(IAC);
  357. put_iac(SB);
  358. put_iac(c);
  359. /* "... & 0xff" implicitly done below */
  360. put_iac(x >> 8);
  361. put_iac(x);
  362. put_iac(y >> 8);
  363. put_iac(y);
  364. put_iac(IAC);
  365. put_iac(SE);
  366. }
  367. #endif
  368. static void setConMode(void)
  369. {
  370. if (G.telflags & UF_ECHO) {
  371. if (G.charmode == CHM_TRY) {
  372. G.charmode = CHM_ON;
  373. printf("\r\nEntering %s mode"
  374. "\r\nEscape character is '^%c'.\r\n", "character", ']');
  375. rawmode();
  376. }
  377. } else {
  378. if (G.charmode != CHM_OFF) {
  379. G.charmode = CHM_OFF;
  380. printf("\r\nEntering %s mode"
  381. "\r\nEscape character is '^%c'.\r\n", "line", 'C');
  382. cookmode();
  383. }
  384. }
  385. }
  386. static void will_charmode(void)
  387. {
  388. G.charmode = CHM_TRY;
  389. G.telflags |= (UF_ECHO | UF_SGA);
  390. setConMode();
  391. put_iac2(DO, TELOPT_ECHO);
  392. put_iac2(DO, TELOPT_SGA);
  393. iac_flush();
  394. }
  395. static void do_linemode(void)
  396. {
  397. G.charmode = CHM_TRY;
  398. G.telflags &= ~(UF_ECHO | UF_SGA);
  399. setConMode();
  400. put_iac2(DONT, TELOPT_ECHO);
  401. put_iac2(DONT, TELOPT_SGA);
  402. iac_flush();
  403. }
  404. static void to_notsup(char c)
  405. {
  406. if (G.telwish == WILL)
  407. put_iac2(DONT, c);
  408. else if (G.telwish == DO)
  409. put_iac2(WONT, c);
  410. }
  411. static void to_echo(void)
  412. {
  413. /* if server requests ECHO, don't agree */
  414. if (G.telwish == DO) {
  415. put_iac2(WONT, TELOPT_ECHO);
  416. return;
  417. }
  418. if (G.telwish == DONT)
  419. return;
  420. if (G.telflags & UF_ECHO) {
  421. if (G.telwish == WILL)
  422. return;
  423. } else if (G.telwish == WONT)
  424. return;
  425. if (G.charmode != CHM_OFF)
  426. G.telflags ^= UF_ECHO;
  427. if (G.telflags & UF_ECHO)
  428. put_iac2(DO, TELOPT_ECHO);
  429. else
  430. put_iac2(DONT, TELOPT_ECHO);
  431. setConMode();
  432. full_write1_str("\r\n"); /* sudden modec */
  433. }
  434. static void to_sga(void)
  435. {
  436. /* daemon always sends will/wont, client do/dont */
  437. if (G.telflags & UF_SGA) {
  438. if (G.telwish == WILL)
  439. return;
  440. } else if (G.telwish == WONT)
  441. return;
  442. G.telflags ^= UF_SGA; /* toggle */
  443. if (G.telflags & UF_SGA)
  444. put_iac2(DO, TELOPT_SGA);
  445. else
  446. put_iac2(DONT, TELOPT_SGA);
  447. }
  448. #if ENABLE_FEATURE_TELNET_TTYPE
  449. static void to_ttype(void)
  450. {
  451. /* Tell server we will (or won't) do TTYPE */
  452. if (G.ttype)
  453. put_iac2(WILL, TELOPT_TTYPE);
  454. else
  455. put_iac2(WONT, TELOPT_TTYPE);
  456. }
  457. #endif
  458. #if ENABLE_FEATURE_TELNET_AUTOLOGIN
  459. static void to_new_environ(void)
  460. {
  461. /* Tell server we will (or will not) do AUTOLOGIN */
  462. if (G.autologin)
  463. put_iac2(WILL, TELOPT_NEW_ENVIRON);
  464. else
  465. put_iac2(WONT, TELOPT_NEW_ENVIRON);
  466. }
  467. #endif
  468. #if ENABLE_FEATURE_TELNET_WIDTH
  469. static void to_naws(void)
  470. {
  471. /* Tell server we will do NAWS */
  472. put_iac2(WILL, TELOPT_NAWS);
  473. }
  474. #endif
  475. static void telopt(byte c)
  476. {
  477. switch (c) {
  478. case TELOPT_ECHO:
  479. to_echo(); break;
  480. case TELOPT_SGA:
  481. to_sga(); break;
  482. #if ENABLE_FEATURE_TELNET_TTYPE
  483. case TELOPT_TTYPE:
  484. to_ttype(); break;
  485. #endif
  486. #if ENABLE_FEATURE_TELNET_AUTOLOGIN
  487. case TELOPT_NEW_ENVIRON:
  488. to_new_environ(); break;
  489. #endif
  490. #if ENABLE_FEATURE_TELNET_WIDTH
  491. case TELOPT_NAWS:
  492. to_naws();
  493. put_iac_naws(c, G.win_width, G.win_height);
  494. break;
  495. #endif
  496. default:
  497. to_notsup(c);
  498. break;
  499. }
  500. }
  501. /* subnegotiation -- ignore all (except TTYPE,NAWS) */
  502. static void subneg(byte c)
  503. {
  504. switch (G.telstate) {
  505. case TS_SUB1:
  506. if (c == IAC)
  507. G.telstate = TS_SUB2;
  508. #if ENABLE_FEATURE_TELNET_TTYPE
  509. else
  510. if (c == TELOPT_TTYPE && G.ttype)
  511. put_iac_subopt(TELOPT_TTYPE, G.ttype);
  512. #endif
  513. #if ENABLE_FEATURE_TELNET_AUTOLOGIN
  514. else
  515. if (c == TELOPT_NEW_ENVIRON && G.autologin)
  516. put_iac_subopt_autologin();
  517. #endif
  518. break;
  519. case TS_SUB2:
  520. if (c == SE) {
  521. G.telstate = TS_COPY;
  522. return;
  523. }
  524. G.telstate = TS_SUB1;
  525. break;
  526. }
  527. }
  528. static void rawmode(void)
  529. {
  530. if (G.do_termios)
  531. tcsetattr(0, TCSADRAIN, &G.termios_raw);
  532. }
  533. static void cookmode(void)
  534. {
  535. if (G.do_termios)
  536. tcsetattr(0, TCSADRAIN, &G.termios_def);
  537. }
  538. int telnet_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  539. int telnet_main(int argc UNUSED_PARAM, char **argv)
  540. {
  541. char *host;
  542. int port;
  543. int len;
  544. struct pollfd ufds[2];
  545. INIT_G();
  546. #if ENABLE_FEATURE_TELNET_TTYPE
  547. G.ttype = getenv("TERM");
  548. #endif
  549. if (tcgetattr(0, &G.termios_def) >= 0) {
  550. G.do_termios = 1;
  551. G.termios_raw = G.termios_def;
  552. cfmakeraw(&G.termios_raw);
  553. }
  554. #if ENABLE_FEATURE_TELNET_AUTOLOGIN
  555. if (1 == getopt32(argv, "al:", &G.autologin)) {
  556. /* Only -a without -l USER picks $USER from envvar */
  557. G.autologin = getenv("USER");
  558. }
  559. argv += optind;
  560. #else
  561. argv++;
  562. #endif
  563. if (!*argv)
  564. bb_show_usage();
  565. host = *argv++;
  566. port = *argv ? bb_lookup_port(*argv++, "tcp", 23)
  567. : bb_lookup_std_port("telnet", "tcp", 23);
  568. if (*argv) /* extra params?? */
  569. bb_show_usage();
  570. xmove_fd(create_and_connect_stream_or_die(host, port), netfd);
  571. setsockopt_keepalive(netfd);
  572. #if ENABLE_FEATURE_TELNET_WIDTH
  573. get_terminal_width_height(0, &G.win_width, &G.win_height);
  574. //TODO: support dynamic resize?
  575. #endif
  576. signal(SIGINT, record_signo);
  577. ufds[0].fd = STDIN_FILENO;
  578. ufds[0].events = POLLIN;
  579. ufds[1].fd = netfd;
  580. ufds[1].events = POLLIN;
  581. while (1) {
  582. if (poll(ufds, 2, -1) < 0) {
  583. /* error, ignore and/or log something, bay go to loop */
  584. if (bb_got_signal)
  585. con_escape();
  586. else
  587. sleep(1);
  588. continue;
  589. }
  590. // FIXME: reads can block. Need full bidirectional buffering.
  591. if (ufds[0].revents) {
  592. len = safe_read(STDIN_FILENO, G.buf, DATABUFSIZE);
  593. if (len <= 0)
  594. doexit(EXIT_SUCCESS);
  595. handle_net_output(len);
  596. }
  597. if (ufds[1].revents) {
  598. len = safe_read(netfd, G.buf, DATABUFSIZE);
  599. if (len <= 0) {
  600. full_write1_str("Connection closed by foreign host\r\n");
  601. doexit(EXIT_FAILURE);
  602. }
  603. handle_net_input(len);
  604. }
  605. } /* while (1) */
  606. }