telnet.c 12 KB

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