telnet.c 12 KB

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