telnet.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665
  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. full_write(netfd, G.iacbuf, G.iaclen);
  115. G.iaclen = 0;
  116. }
  117. static void doexit(int ev) NORETURN;
  118. static void doexit(int ev)
  119. {
  120. cookmode();
  121. exit(ev);
  122. }
  123. static void con_escape(void)
  124. {
  125. char b;
  126. if (bb_got_signal) /* came from line mode... go raw */
  127. rawmode();
  128. full_write1_str("\r\nConsole escape. Commands are:\r\n\n"
  129. " l go to line mode\r\n"
  130. " c go to character mode\r\n"
  131. " z suspend telnet\r\n"
  132. " e exit telnet\r\n");
  133. if (read(STDIN_FILENO, &b, 1) <= 0)
  134. doexit(EXIT_FAILURE);
  135. switch (b) {
  136. case 'l':
  137. if (!bb_got_signal) {
  138. do_linemode();
  139. goto ret;
  140. }
  141. break;
  142. case 'c':
  143. if (bb_got_signal) {
  144. will_charmode();
  145. goto ret;
  146. }
  147. break;
  148. case 'z':
  149. cookmode();
  150. kill(0, SIGTSTP);
  151. rawmode();
  152. break;
  153. case 'e':
  154. doexit(EXIT_SUCCESS);
  155. }
  156. full_write1_str("continuing...\r\n");
  157. if (bb_got_signal)
  158. cookmode();
  159. ret:
  160. bb_got_signal = 0;
  161. }
  162. static void handle_net_output(int len)
  163. {
  164. byte outbuf[2 * DATABUFSIZE];
  165. byte *dst = outbuf;
  166. byte *src = (byte*)G.buf;
  167. byte *end = src + len;
  168. while (src < end) {
  169. byte c = *src++;
  170. if (c == 0x1d) {
  171. con_escape();
  172. return;
  173. }
  174. *dst = c;
  175. if (c == IAC)
  176. *++dst = c; /* IAC -> IAC IAC */
  177. else
  178. if (c == '\r' || c == '\n') {
  179. /* Enter key sends '\r' in raw mode and '\n' in cooked one.
  180. *
  181. * See RFC 1123 3.3.1 Telnet End-of-Line Convention.
  182. * Using CR LF instead of other allowed possibilities
  183. * like CR NUL - easier to talk to HTTP/SMTP servers.
  184. */
  185. *dst = '\r'; /* Enter -> CR LF */
  186. *++dst = '\n';
  187. }
  188. dst++;
  189. }
  190. if (dst - outbuf != 0)
  191. full_write(netfd, outbuf, dst - outbuf);
  192. }
  193. static void handle_net_input(int len)
  194. {
  195. int i;
  196. int cstart = 0;
  197. for (i = 0; i < len; i++) {
  198. byte c = G.buf[i];
  199. if (G.telstate == TS_NORMAL) { /* most typical state */
  200. if (c == IAC) {
  201. cstart = i;
  202. G.telstate = TS_IAC;
  203. }
  204. else if (c == '\r') {
  205. cstart = i + 1;
  206. G.telstate = TS_CR;
  207. }
  208. /* No IACs were seen so far, no need to copy
  209. * bytes within G.buf: */
  210. continue;
  211. }
  212. switch (G.telstate) {
  213. case TS_CR:
  214. /* Prev char was CR. If cur one is NUL, ignore it.
  215. * See RFC 1123 section 3.3.1 for discussion of telnet EOL handling.
  216. */
  217. G.telstate = TS_COPY;
  218. if (c == '\0')
  219. break;
  220. /* else: fall through - need to handle CR IAC ... properly */
  221. case TS_COPY: /* Prev char was ordinary */
  222. /* Similar to NORMAL, but in TS_COPY we need to copy bytes */
  223. if (c == IAC)
  224. G.telstate = TS_IAC;
  225. else
  226. G.buf[cstart++] = c;
  227. if (c == '\r')
  228. G.telstate = TS_CR;
  229. break;
  230. case TS_IAC: /* Prev char was IAC */
  231. if (c == IAC) { /* IAC IAC -> one IAC */
  232. G.buf[cstart++] = c;
  233. G.telstate = TS_COPY;
  234. break;
  235. }
  236. /* else */
  237. switch (c) {
  238. case SB:
  239. G.telstate = TS_SUB1;
  240. break;
  241. case DO:
  242. case DONT:
  243. case WILL:
  244. case WONT:
  245. G.telwish = c;
  246. G.telstate = TS_OPT;
  247. break;
  248. /* DATA MARK must be added later */
  249. default:
  250. G.telstate = TS_COPY;
  251. }
  252. break;
  253. case TS_OPT: /* Prev chars were IAC WILL/WONT/DO/DONT */
  254. telopt(c);
  255. G.telstate = TS_COPY;
  256. break;
  257. case TS_SUB1: /* Subnegotiation */
  258. case TS_SUB2: /* Subnegotiation */
  259. subneg(c); /* can change G.telstate */
  260. break;
  261. }
  262. }
  263. if (G.telstate != TS_NORMAL) {
  264. /* We had some IACs, or CR */
  265. if (G.iaclen)
  266. iac_flush();
  267. if (G.telstate == TS_COPY) /* we aren't in the middle of IAC */
  268. G.telstate = TS_NORMAL;
  269. len = cstart;
  270. }
  271. if (len)
  272. full_write(STDOUT_FILENO, G.buf, len);
  273. }
  274. static void put_iac(int c)
  275. {
  276. G.iacbuf[G.iaclen++] = c;
  277. }
  278. static void put_iac2(byte wwdd, byte c)
  279. {
  280. if (G.iaclen + 3 > IACBUFSIZE)
  281. iac_flush();
  282. put_iac(IAC);
  283. put_iac(wwdd);
  284. put_iac(c);
  285. }
  286. #if ENABLE_FEATURE_TELNET_TTYPE
  287. static void put_iac_subopt(byte c, char *str)
  288. {
  289. int len = strlen(str) + 6; // ( 2 + 1 + 1 + strlen + 2 )
  290. if (G.iaclen + len > IACBUFSIZE)
  291. iac_flush();
  292. put_iac(IAC);
  293. put_iac(SB);
  294. put_iac(c);
  295. put_iac(0);
  296. while (*str)
  297. put_iac(*str++);
  298. put_iac(IAC);
  299. put_iac(SE);
  300. }
  301. #endif
  302. #if ENABLE_FEATURE_TELNET_AUTOLOGIN
  303. static void put_iac_subopt_autologin(void)
  304. {
  305. int len = strlen(G.autologin) + 6; // (2 + 1 + 1 + strlen + 2)
  306. const char *p = "USER";
  307. if (G.iaclen + len > IACBUFSIZE)
  308. iac_flush();
  309. put_iac(IAC);
  310. put_iac(SB);
  311. put_iac(TELOPT_NEW_ENVIRON);
  312. put_iac(TELQUAL_IS);
  313. put_iac(NEW_ENV_VAR);
  314. while (*p)
  315. put_iac(*p++);
  316. put_iac(NEW_ENV_VALUE);
  317. p = G.autologin;
  318. while (*p)
  319. put_iac(*p++);
  320. put_iac(IAC);
  321. put_iac(SE);
  322. }
  323. #endif
  324. #if ENABLE_FEATURE_AUTOWIDTH
  325. static void put_iac_naws(byte c, int x, int y)
  326. {
  327. if (G.iaclen + 9 > IACBUFSIZE)
  328. iac_flush();
  329. put_iac(IAC);
  330. put_iac(SB);
  331. put_iac(c);
  332. /* "... & 0xff" implicitly done below */
  333. put_iac(x >> 8);
  334. put_iac(x);
  335. put_iac(y >> 8);
  336. put_iac(y);
  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. }