telnet.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671
  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. /* here we could do smart tricks how to handle 0xFF:s in output
  166. * stream like writing twice every sequence of FF:s (thus doing
  167. * many write()s. But I think interactive telnet application does
  168. * not need to be 100% 8-bit clean, so changing every 0xff:s to
  169. * 0x7f:s
  170. *
  171. * 2002-mar-21, Przemyslaw Czerpak (druzus@polbox.com)
  172. * I don't agree.
  173. * first - I cannot use programs like sz/rz
  174. * second - the 0x0D is sent as one character and if the next
  175. * char is 0x0A then it's eaten by a server side.
  176. * third - why do you have to make 'many write()s'?
  177. * I don't understand.
  178. * So I implemented it. It's really useful for me. I hope that
  179. * other people will find it interesting too.
  180. */
  181. byte outbuf[2 * DATABUFSIZE];
  182. byte *p = (byte*)G.buf;
  183. int j = 0;
  184. for (; len > 0; len--, p++) {
  185. byte c = *p;
  186. if (c == 0x1d) {
  187. con_escape();
  188. return;
  189. }
  190. outbuf[j++] = c;
  191. if (c == IAC)
  192. outbuf[j++] = c; /* IAC -> IAC IAC */
  193. else if (c == '\r')
  194. outbuf[j++] = '\0'; /* CR -> CR NUL */
  195. }
  196. if (j > 0)
  197. full_write(netfd, outbuf, j);
  198. }
  199. static void handle_net_input(int len)
  200. {
  201. int i;
  202. int cstart = 0;
  203. for (i = 0; i < len; i++) {
  204. byte c = G.buf[i];
  205. if (G.telstate == TS_NORMAL) { /* most typical state */
  206. if (c == IAC) {
  207. cstart = i;
  208. G.telstate = TS_IAC;
  209. }
  210. else if (c == '\r') {
  211. cstart = i + 1;
  212. G.telstate = TS_CR;
  213. }
  214. /* No IACs were seen so far, no need to copy
  215. * bytes within G.buf: */
  216. continue;
  217. }
  218. switch (G.telstate) {
  219. case TS_CR:
  220. /* Prev char was CR. If cur one is NUL, ignore it.
  221. * See RFC 1123 section 3.3.1 for discussion of telnet EOL handling.
  222. */
  223. G.telstate = TS_COPY;
  224. if (c == '\0')
  225. break;
  226. /* else: fall through - need to handle CR IAC ... properly */
  227. case TS_COPY: /* Prev char was ordinary */
  228. /* Similar to NORMAL, but in TS_COPY we need to copy bytes */
  229. if (c == IAC)
  230. G.telstate = TS_IAC;
  231. else
  232. G.buf[cstart++] = c;
  233. if (c == '\r')
  234. G.telstate = TS_CR;
  235. break;
  236. case TS_IAC: /* Prev char was IAC */
  237. if (c == IAC) { /* IAC IAC -> one IAC */
  238. G.buf[cstart++] = c;
  239. G.telstate = TS_COPY;
  240. break;
  241. }
  242. /* else */
  243. switch (c) {
  244. case SB:
  245. G.telstate = TS_SUB1;
  246. break;
  247. case DO:
  248. case DONT:
  249. case WILL:
  250. case WONT:
  251. G.telwish = c;
  252. G.telstate = TS_OPT;
  253. break;
  254. /* DATA MARK must be added later */
  255. default:
  256. G.telstate = TS_COPY;
  257. }
  258. break;
  259. case TS_OPT: /* Prev chars were IAC WILL/WONT/DO/DONT */
  260. telopt(c);
  261. G.telstate = TS_COPY;
  262. break;
  263. case TS_SUB1: /* Subnegotiation */
  264. case TS_SUB2: /* Subnegotiation */
  265. subneg(c); /* can change G.telstate */
  266. break;
  267. }
  268. }
  269. if (G.telstate != TS_NORMAL) {
  270. /* We had some IACs, or CR */
  271. if (G.iaclen)
  272. iac_flush();
  273. if (G.telstate == TS_COPY) /* we aren't in the middle of IAC */
  274. G.telstate = TS_NORMAL;
  275. len = cstart;
  276. }
  277. if (len)
  278. full_write(STDOUT_FILENO, G.buf, len);
  279. }
  280. static void put_iac(int c)
  281. {
  282. G.iacbuf[G.iaclen++] = c;
  283. }
  284. static void put_iac2(byte wwdd, byte c)
  285. {
  286. if (G.iaclen + 3 > IACBUFSIZE)
  287. iac_flush();
  288. put_iac(IAC);
  289. put_iac(wwdd);
  290. put_iac(c);
  291. }
  292. #if ENABLE_FEATURE_TELNET_TTYPE
  293. static void put_iac_subopt(byte c, char *str)
  294. {
  295. int len = strlen(str) + 6; // ( 2 + 1 + 1 + strlen + 2 )
  296. if (G.iaclen + len > IACBUFSIZE)
  297. iac_flush();
  298. put_iac(IAC);
  299. put_iac(SB);
  300. put_iac(c);
  301. put_iac(0);
  302. while (*str)
  303. put_iac(*str++);
  304. put_iac(IAC);
  305. put_iac(SE);
  306. }
  307. #endif
  308. #if ENABLE_FEATURE_TELNET_AUTOLOGIN
  309. static void put_iac_subopt_autologin(void)
  310. {
  311. int len = strlen(G.autologin) + 6; // (2 + 1 + 1 + strlen + 2)
  312. const char *p = "USER";
  313. if (G.iaclen + len > IACBUFSIZE)
  314. iac_flush();
  315. put_iac(IAC);
  316. put_iac(SB);
  317. put_iac(TELOPT_NEW_ENVIRON);
  318. put_iac(TELQUAL_IS);
  319. put_iac(NEW_ENV_VAR);
  320. while (*p)
  321. put_iac(*p++);
  322. put_iac(NEW_ENV_VALUE);
  323. p = G.autologin;
  324. while (*p)
  325. put_iac(*p++);
  326. put_iac(IAC);
  327. put_iac(SE);
  328. }
  329. #endif
  330. #if ENABLE_FEATURE_AUTOWIDTH
  331. static void put_iac_naws(byte c, int x, int y)
  332. {
  333. if (G.iaclen + 9 > IACBUFSIZE)
  334. iac_flush();
  335. put_iac(IAC);
  336. put_iac(SB);
  337. put_iac(c);
  338. put_iac((x >> 8) & 0xff);
  339. put_iac(x & 0xff);
  340. put_iac((y >> 8) & 0xff);
  341. put_iac(y & 0xff);
  342. put_iac(IAC);
  343. put_iac(SE);
  344. }
  345. #endif
  346. static char const escapecharis[] ALIGN1 = "\r\nEscape character is ";
  347. static void setConMode(void)
  348. {
  349. if (G.telflags & UF_ECHO) {
  350. if (G.charmode == CHM_TRY) {
  351. G.charmode = CHM_ON;
  352. printf("\r\nEntering character mode%s'^]'.\r\n", escapecharis);
  353. rawmode();
  354. }
  355. } else {
  356. if (G.charmode != CHM_OFF) {
  357. G.charmode = CHM_OFF;
  358. printf("\r\nEntering line mode%s'^C'.\r\n", escapecharis);
  359. cookmode();
  360. }
  361. }
  362. }
  363. static void will_charmode(void)
  364. {
  365. G.charmode = CHM_TRY;
  366. G.telflags |= (UF_ECHO | UF_SGA);
  367. setConMode();
  368. put_iac2(DO, TELOPT_ECHO);
  369. put_iac2(DO, TELOPT_SGA);
  370. iac_flush();
  371. }
  372. static void do_linemode(void)
  373. {
  374. G.charmode = CHM_TRY;
  375. G.telflags &= ~(UF_ECHO | UF_SGA);
  376. setConMode();
  377. put_iac2(DONT, TELOPT_ECHO);
  378. put_iac2(DONT, TELOPT_SGA);
  379. iac_flush();
  380. }
  381. static void to_notsup(char c)
  382. {
  383. if (G.telwish == WILL)
  384. put_iac2(DONT, c);
  385. else if (G.telwish == DO)
  386. put_iac2(WONT, c);
  387. }
  388. static void to_echo(void)
  389. {
  390. /* if server requests ECHO, don't agree */
  391. if (G.telwish == DO) {
  392. put_iac2(WONT, TELOPT_ECHO);
  393. return;
  394. }
  395. if (G.telwish == DONT)
  396. return;
  397. if (G.telflags & UF_ECHO) {
  398. if (G.telwish == WILL)
  399. return;
  400. } else if (G.telwish == WONT)
  401. return;
  402. if (G.charmode != CHM_OFF)
  403. G.telflags ^= UF_ECHO;
  404. if (G.telflags & UF_ECHO)
  405. put_iac2(DO, TELOPT_ECHO);
  406. else
  407. put_iac2(DONT, TELOPT_ECHO);
  408. setConMode();
  409. full_write1_str("\r\n"); /* sudden modec */
  410. }
  411. static void to_sga(void)
  412. {
  413. /* daemon always sends will/wont, client do/dont */
  414. if (G.telflags & UF_SGA) {
  415. if (G.telwish == WILL)
  416. return;
  417. } else if (G.telwish == WONT)
  418. return;
  419. G.telflags ^= UF_SGA; /* toggle */
  420. if (G.telflags & UF_SGA)
  421. put_iac2(DO, TELOPT_SGA);
  422. else
  423. put_iac2(DONT, TELOPT_SGA);
  424. }
  425. #if ENABLE_FEATURE_TELNET_TTYPE
  426. static void to_ttype(void)
  427. {
  428. /* Tell server we will (or won't) do TTYPE */
  429. if (G.ttype)
  430. put_iac2(WILL, TELOPT_TTYPE);
  431. else
  432. put_iac2(WONT, TELOPT_TTYPE);
  433. }
  434. #endif
  435. #if ENABLE_FEATURE_TELNET_AUTOLOGIN
  436. static void to_new_environ(void)
  437. {
  438. /* Tell server we will (or will not) do AUTOLOGIN */
  439. if (G.autologin)
  440. put_iac2(WILL, TELOPT_NEW_ENVIRON);
  441. else
  442. put_iac2(WONT, TELOPT_NEW_ENVIRON);
  443. }
  444. #endif
  445. #if ENABLE_FEATURE_AUTOWIDTH
  446. static void to_naws(void)
  447. {
  448. /* Tell server we will do NAWS */
  449. put_iac2(WILL, TELOPT_NAWS);
  450. }
  451. #endif
  452. static void telopt(byte c)
  453. {
  454. switch (c) {
  455. case TELOPT_ECHO:
  456. to_echo(); break;
  457. case TELOPT_SGA:
  458. to_sga(); break;
  459. #if ENABLE_FEATURE_TELNET_TTYPE
  460. case TELOPT_TTYPE:
  461. to_ttype(); break;
  462. #endif
  463. #if ENABLE_FEATURE_TELNET_AUTOLOGIN
  464. case TELOPT_NEW_ENVIRON:
  465. to_new_environ(); break;
  466. #endif
  467. #if ENABLE_FEATURE_AUTOWIDTH
  468. case TELOPT_NAWS:
  469. to_naws();
  470. put_iac_naws(c, G.win_width, G.win_height);
  471. break;
  472. #endif
  473. default:
  474. to_notsup(c);
  475. break;
  476. }
  477. }
  478. /* subnegotiation -- ignore all (except TTYPE,NAWS) */
  479. static void subneg(byte c)
  480. {
  481. switch (G.telstate) {
  482. case TS_SUB1:
  483. if (c == IAC)
  484. G.telstate = TS_SUB2;
  485. #if ENABLE_FEATURE_TELNET_TTYPE
  486. else
  487. if (c == TELOPT_TTYPE && G.ttype)
  488. put_iac_subopt(TELOPT_TTYPE, G.ttype);
  489. #endif
  490. #if ENABLE_FEATURE_TELNET_AUTOLOGIN
  491. else
  492. if (c == TELOPT_NEW_ENVIRON && G.autologin)
  493. put_iac_subopt_autologin();
  494. #endif
  495. break;
  496. case TS_SUB2:
  497. if (c == SE) {
  498. G.telstate = TS_COPY;
  499. return;
  500. }
  501. G.telstate = TS_SUB1;
  502. break;
  503. }
  504. }
  505. static void rawmode(void)
  506. {
  507. if (G.do_termios)
  508. tcsetattr(0, TCSADRAIN, &G.termios_raw);
  509. }
  510. static void cookmode(void)
  511. {
  512. if (G.do_termios)
  513. tcsetattr(0, TCSADRAIN, &G.termios_def);
  514. }
  515. int telnet_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  516. int telnet_main(int argc UNUSED_PARAM, char **argv)
  517. {
  518. char *host;
  519. int port;
  520. int len;
  521. struct pollfd ufds[2];
  522. INIT_G();
  523. #if ENABLE_FEATURE_AUTOWIDTH
  524. get_terminal_width_height(0, &G.win_width, &G.win_height);
  525. #endif
  526. #if ENABLE_FEATURE_TELNET_TTYPE
  527. G.ttype = getenv("TERM");
  528. #endif
  529. if (tcgetattr(0, &G.termios_def) >= 0) {
  530. G.do_termios = 1;
  531. G.termios_raw = G.termios_def;
  532. cfmakeraw(&G.termios_raw);
  533. }
  534. #if ENABLE_FEATURE_TELNET_AUTOLOGIN
  535. if (1 & getopt32(argv, "al:", &G.autologin))
  536. G.autologin = getenv("USER");
  537. argv += optind;
  538. #else
  539. argv++;
  540. #endif
  541. if (!*argv)
  542. bb_show_usage();
  543. host = *argv++;
  544. port = bb_lookup_port(*argv ? *argv++ : "telnet", "tcp", 23);
  545. if (*argv) /* extra params?? */
  546. bb_show_usage();
  547. xmove_fd(create_and_connect_stream_or_die(host, port), netfd);
  548. setsockopt(netfd, SOL_SOCKET, SO_KEEPALIVE, &const_int_1, sizeof(const_int_1));
  549. signal(SIGINT, record_signo);
  550. ufds[0].fd = STDIN_FILENO;
  551. ufds[0].events = POLLIN;
  552. ufds[1].fd = netfd;
  553. ufds[1].events = POLLIN;
  554. while (1) {
  555. if (poll(ufds, 2, -1) < 0) {
  556. /* error, ignore and/or log something, bay go to loop */
  557. if (bb_got_signal)
  558. con_escape();
  559. else
  560. sleep(1);
  561. continue;
  562. }
  563. // FIXME: reads can block. Need full bidirectional buffering.
  564. if (ufds[0].revents) {
  565. len = safe_read(STDIN_FILENO, G.buf, DATABUFSIZE);
  566. if (len <= 0)
  567. doexit(EXIT_SUCCESS);
  568. TRACE(0, ("Read con: %d\n", len));
  569. handle_net_output(len);
  570. }
  571. if (ufds[1].revents) {
  572. len = safe_read(netfd, G.buf, DATABUFSIZE);
  573. if (len <= 0) {
  574. full_write1_str("Connection closed by foreign host\r\n");
  575. doexit(EXIT_FAILURE);
  576. }
  577. TRACE(0, ("Read netfd (%d): %d\n", netfd, len));
  578. handle_net_input(len);
  579. }
  580. } /* while (1) */
  581. }