telnet.c 14 KB

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