telnet.c 14 KB

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