telnet.c 13 KB

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