telnet.c 13 KB

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