3
0

telnet.c 14 KB

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