telnet.c 12 KB

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