telnet.c 13 KB

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