telnetd.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674
  1. /* $Id: telnetd.c,v 1.13 2004/09/14 17:24:58 bug1 Exp $
  2. *
  3. * Simple telnet server
  4. * Bjorn Wesen, Axis Communications AB (bjornw@axis.com)
  5. *
  6. * This file is distributed under the Gnu Public License (GPL),
  7. * please see the file LICENSE for further information.
  8. *
  9. * ---------------------------------------------------------------------------
  10. * (C) Copyright 2000, Axis Communications AB, LUND, SWEDEN
  11. ****************************************************************************
  12. *
  13. * The telnetd manpage says it all:
  14. *
  15. * Telnetd operates by allocating a pseudo-terminal device (see pty(4)) for
  16. * a client, then creating a login process which has the slave side of the
  17. * pseudo-terminal as stdin, stdout, and stderr. Telnetd manipulates the
  18. * master side of the pseudo-terminal, implementing the telnet protocol and
  19. * passing characters between the remote client and the login process.
  20. *
  21. * Vladimir Oleynik <dzo@simtreas.ru> 2001
  22. * Set process group corrections, initial busybox port
  23. */
  24. /*#define DEBUG 1 */
  25. #include <sys/time.h>
  26. #include <sys/socket.h>
  27. #include <sys/wait.h>
  28. #include <sys/ioctl.h>
  29. #include <string.h>
  30. #include <stdlib.h>
  31. #include <unistd.h>
  32. #include <errno.h>
  33. #include <netinet/in.h>
  34. #include <fcntl.h>
  35. #include <stdio.h>
  36. #include <signal.h>
  37. #include <termios.h>
  38. #ifdef DEBUG
  39. #define TELCMDS
  40. #define TELOPTS
  41. #endif
  42. #include <arpa/telnet.h>
  43. #include <ctype.h>
  44. #include <sys/syslog.h>
  45. #include "busybox.h"
  46. #define BUFSIZE 4000
  47. #ifdef CONFIG_FEATURE_IPV6
  48. #define SOCKET_TYPE AF_INET6
  49. typedef struct sockaddr_in6 sockaddr_type;
  50. #else
  51. #define SOCKET_TYPE AF_INET
  52. typedef struct sockaddr_in sockaddr_type;
  53. #endif
  54. #ifdef CONFIG_LOGIN
  55. static const char *loginpath = "/bin/login";
  56. #else
  57. static const char *loginpath;
  58. #endif
  59. static const char *issuefile = "/etc/issue.net";
  60. /* shell name and arguments */
  61. static const char *argv_init[] = {NULL, NULL};
  62. /* structure that describes a session */
  63. struct tsession {
  64. #ifdef CONFIG_FEATURE_TELNETD_INETD
  65. int sockfd_read, sockfd_write, ptyfd;
  66. #else /* CONFIG_FEATURE_TELNETD_INETD */
  67. struct tsession *next;
  68. int sockfd, ptyfd;
  69. #endif /* CONFIG_FEATURE_TELNETD_INETD */
  70. int shell_pid;
  71. /* two circular buffers */
  72. char *buf1, *buf2;
  73. int rdidx1, wridx1, size1;
  74. int rdidx2, wridx2, size2;
  75. };
  76. /*
  77. This is how the buffers are used. The arrows indicate the movement
  78. of data.
  79. +-------+ wridx1++ +------+ rdidx1++ +----------+
  80. | | <-------------- | buf1 | <-------------- | |
  81. | | size1-- +------+ size1++ | |
  82. | pty | | socket |
  83. | | rdidx2++ +------+ wridx2++ | |
  84. | | --------------> | buf2 | --------------> | |
  85. +-------+ size2++ +------+ size2-- +----------+
  86. Each session has got two buffers.
  87. */
  88. static int maxfd;
  89. static struct tsession *sessions;
  90. /*
  91. Remove all IAC's from the buffer pointed to by bf (recieved IACs are ignored
  92. and must be removed so as to not be interpreted by the terminal). Make an
  93. uninterrupted string of characters fit for the terminal. Do this by packing
  94. all characters meant for the terminal sequentially towards the end of bf.
  95. Return a pointer to the beginning of the characters meant for the terminal.
  96. and make *num_totty the number of characters that should be sent to
  97. the terminal.
  98. Note - If an IAC (3 byte quantity) starts before (bf + len) but extends
  99. past (bf + len) then that IAC will be left unprocessed and *processed will be
  100. less than len.
  101. FIXME - if we mean to send 0xFF to the terminal then it will be escaped,
  102. what is the escape character? We aren't handling that situation here.
  103. CR-LF ->'s CR mapping is also done here, for convenience
  104. */
  105. static char *
  106. remove_iacs(struct tsession *ts, int *pnum_totty) {
  107. unsigned char *ptr0 = ts->buf1 + ts->wridx1;
  108. unsigned char *ptr = ptr0;
  109. unsigned char *totty = ptr;
  110. unsigned char *end = ptr + MIN(BUFSIZE - ts->wridx1, ts->size1);
  111. int processed;
  112. int num_totty;
  113. while (ptr < end) {
  114. if (*ptr != IAC) {
  115. int c = *ptr;
  116. *totty++ = *ptr++;
  117. /* We now map \r\n ==> \r for pragmatic reasons.
  118. * Many client implementations send \r\n when
  119. * the user hits the CarriageReturn key.
  120. */
  121. if (c == '\r' && (*ptr == '\n' || *ptr == 0) && ptr < end)
  122. ptr++;
  123. }
  124. else {
  125. /*
  126. * TELOPT_NAWS support!
  127. */
  128. if ((ptr+2) >= end) {
  129. /* only the beginning of the IAC is in the
  130. buffer we were asked to process, we can't
  131. process this char. */
  132. break;
  133. }
  134. /*
  135. * IAC -> SB -> TELOPT_NAWS -> 4-byte -> IAC -> SE
  136. */
  137. else if (ptr[1] == SB && ptr[2] == TELOPT_NAWS) {
  138. struct winsize ws;
  139. if ((ptr+8) >= end)
  140. break; /* incomplete, can't process */
  141. ws.ws_col = (ptr[3] << 8) | ptr[4];
  142. ws.ws_row = (ptr[5] << 8) | ptr[6];
  143. (void) ioctl(ts->ptyfd, TIOCSWINSZ, (char *)&ws);
  144. ptr += 9;
  145. }
  146. else {
  147. /* skip 3-byte IAC non-SB cmd */
  148. #ifdef DEBUG
  149. fprintf(stderr, "Ignoring IAC %s,%s\n",
  150. TELCMD(*(ptr+1)), TELOPT(*(ptr+2)));
  151. #endif
  152. ptr += 3;
  153. }
  154. }
  155. }
  156. processed = ptr - ptr0;
  157. num_totty = totty - ptr0;
  158. /* the difference between processed and num_to tty
  159. is all the iacs we removed from the stream.
  160. Adjust buf1 accordingly. */
  161. ts->wridx1 += processed - num_totty;
  162. ts->size1 -= processed - num_totty;
  163. *pnum_totty = num_totty;
  164. /* move the chars meant for the terminal towards the end of the
  165. buffer. */
  166. return memmove(ptr - num_totty, ptr0, num_totty);
  167. }
  168. static int
  169. getpty(char *line)
  170. {
  171. int p;
  172. #ifdef CONFIG_FEATURE_DEVPTS
  173. p = open("/dev/ptmx", 2);
  174. if (p > 0) {
  175. grantpt(p);
  176. unlockpt(p);
  177. strcpy(line, ptsname(p));
  178. return(p);
  179. }
  180. #else
  181. struct stat stb;
  182. int i;
  183. int j;
  184. strcpy(line, "/dev/ptyXX");
  185. for (i = 0; i < 16; i++) {
  186. line[8] = "pqrstuvwxyzabcde"[i];
  187. line[9] = '0';
  188. if (stat(line, &stb) < 0) {
  189. continue;
  190. }
  191. for (j = 0; j < 16; j++) {
  192. line[9] = j < 10 ? j + '0' : j - 10 + 'a';
  193. if ((p = open(line, O_RDWR | O_NOCTTY)) >= 0) {
  194. line[5] = 't';
  195. return p;
  196. }
  197. }
  198. }
  199. #endif /* CONFIG_FEATURE_DEVPTS */
  200. return -1;
  201. }
  202. static void
  203. send_iac(struct tsession *ts, unsigned char command, int option)
  204. {
  205. /* We rely on that there is space in the buffer for now. */
  206. char *b = ts->buf2 + ts->rdidx2;
  207. *b++ = IAC;
  208. *b++ = command;
  209. *b++ = option;
  210. ts->rdidx2 += 3;
  211. ts->size2 += 3;
  212. }
  213. static struct tsession *
  214. #ifdef CONFIG_FEATURE_TELNETD_INETD
  215. make_new_session(void)
  216. #else /* CONFIG_FEATURE_TELNETD_INETD */
  217. make_new_session(int sockfd)
  218. #endif /* CONFIG_FEATURE_TELNETD_INETD */
  219. {
  220. struct termios termbuf;
  221. int pty, pid;
  222. char tty_name[32];
  223. struct tsession *ts = malloc(sizeof(struct tsession) + BUFSIZE * 2);
  224. ts->buf1 = (char *)(&ts[1]);
  225. ts->buf2 = ts->buf1 + BUFSIZE;
  226. #ifdef CONFIG_FEATURE_TELNETD_INETD
  227. ts->sockfd_read = 0;
  228. ts->sockfd_write = 1;
  229. #else /* CONFIG_FEATURE_TELNETD_INETD */
  230. ts->sockfd = sockfd;
  231. #endif /* CONFIG_FEATURE_TELNETD_INETD */
  232. ts->rdidx1 = ts->wridx1 = ts->size1 = 0;
  233. ts->rdidx2 = ts->wridx2 = ts->size2 = 0;
  234. /* Got a new connection, set up a tty and spawn a shell. */
  235. pty = getpty(tty_name);
  236. if (pty < 0) {
  237. syslog(LOG_ERR, "All network ports in use!");
  238. return 0;
  239. }
  240. if (pty > maxfd)
  241. maxfd = pty;
  242. ts->ptyfd = pty;
  243. /* Make the telnet client understand we will echo characters so it
  244. * should not do it locally. We don't tell the client to run linemode,
  245. * because we want to handle line editing and tab completion and other
  246. * stuff that requires char-by-char support.
  247. */
  248. send_iac(ts, DO, TELOPT_ECHO);
  249. send_iac(ts, DO, TELOPT_NAWS);
  250. send_iac(ts, DO, TELOPT_LFLOW);
  251. send_iac(ts, WILL, TELOPT_ECHO);
  252. send_iac(ts, WILL, TELOPT_SGA);
  253. if ((pid = fork()) < 0) {
  254. syslog(LOG_ERR, "Can`t forking");
  255. }
  256. if (pid == 0) {
  257. /* In child, open the child's side of the tty. */
  258. int i;
  259. for(i = 0; i <= maxfd; i++)
  260. close(i);
  261. /* make new process group */
  262. setsid();
  263. if (open(tty_name, O_RDWR /*| O_NOCTTY*/) < 0) {
  264. syslog(LOG_ERR, "Could not open tty");
  265. exit(1);
  266. }
  267. dup(0);
  268. dup(0);
  269. tcsetpgrp(0, getpid());
  270. /* The pseudo-terminal allocated to the client is configured to operate in
  271. * cooked mode, and with XTABS CRMOD enabled (see tty(4)).
  272. */
  273. tcgetattr(0, &termbuf);
  274. termbuf.c_lflag |= ECHO; /* if we use readline we dont want this */
  275. termbuf.c_oflag |= ONLCR|XTABS;
  276. termbuf.c_iflag |= ICRNL;
  277. termbuf.c_iflag &= ~IXOFF;
  278. /*termbuf.c_lflag &= ~ICANON;*/
  279. tcsetattr(0, TCSANOW, &termbuf);
  280. print_login_issue(issuefile, NULL);
  281. /* exec shell, with correct argv and env */
  282. execv(loginpath, (char *const *)argv_init);
  283. /* NOT REACHED */
  284. syslog(LOG_ERR, "execv error");
  285. exit(1);
  286. }
  287. ts->shell_pid = pid;
  288. return ts;
  289. }
  290. #ifndef CONFIG_FEATURE_TELNETD_INETD
  291. static void
  292. free_session(struct tsession *ts)
  293. {
  294. struct tsession *t = sessions;
  295. /* Unlink this telnet session from the session list. */
  296. if(t == ts)
  297. sessions = ts->next;
  298. else {
  299. while(t->next != ts)
  300. t = t->next;
  301. t->next = ts->next;
  302. }
  303. kill(ts->shell_pid, SIGKILL);
  304. wait4(ts->shell_pid, NULL, 0, NULL);
  305. close(ts->ptyfd);
  306. close(ts->sockfd);
  307. if(ts->ptyfd == maxfd || ts->sockfd == maxfd)
  308. maxfd--;
  309. if(ts->ptyfd == maxfd || ts->sockfd == maxfd)
  310. maxfd--;
  311. free(ts);
  312. }
  313. #endif /* CONFIG_FEATURE_TELNETD_INETD */
  314. int
  315. telnetd_main(int argc, char **argv)
  316. {
  317. #ifndef CONFIG_FEATURE_TELNETD_INETD
  318. sockaddr_type sa;
  319. int master_fd;
  320. #endif /* CONFIG_FEATURE_TELNETD_INETD */
  321. fd_set rdfdset, wrfdset;
  322. int selret;
  323. #ifndef CONFIG_FEATURE_TELNETD_INETD
  324. int on = 1;
  325. int portnbr = 23;
  326. #endif /* CONFIG_FEATURE_TELNETD_INETD */
  327. int c;
  328. static const char options[] =
  329. #ifdef CONFIG_FEATURE_TELNETD_INETD
  330. "f:l:";
  331. #else /* CONFIG_EATURE_TELNETD_INETD */
  332. "f:l:p:";
  333. #endif /* CONFIG_FEATURE_TELNETD_INETD */
  334. int maxlen, w, r;
  335. #ifndef CONFIG_LOGIN
  336. loginpath = DEFAULT_SHELL;
  337. #endif
  338. for (;;) {
  339. c = getopt( argc, argv, options);
  340. if (c == EOF) break;
  341. switch (c) {
  342. case 'f':
  343. issuefile = optarg;
  344. break;
  345. case 'l':
  346. loginpath = optarg;
  347. break;
  348. #ifndef CONFIG_FEATURE_TELNETD_INETD
  349. case 'p':
  350. portnbr = atoi(optarg);
  351. break;
  352. #endif /* CONFIG_FEATURE_TELNETD_INETD */
  353. default:
  354. bb_show_usage();
  355. }
  356. }
  357. if (access(loginpath, X_OK) < 0) {
  358. bb_error_msg_and_die ("'%s' unavailable.", loginpath);
  359. }
  360. argv_init[0] = loginpath;
  361. openlog(bb_applet_name, 0, LOG_USER);
  362. #ifdef CONFIG_FEATURE_TELNETD_INETD
  363. maxfd = 1;
  364. sessions = make_new_session();
  365. #else /* CONFIG_EATURE_TELNETD_INETD */
  366. sessions = 0;
  367. /* Grab a TCP socket. */
  368. master_fd = socket(SOCKET_TYPE, SOCK_STREAM, 0);
  369. if (master_fd < 0) {
  370. bb_perror_msg_and_die("socket");
  371. }
  372. (void)setsockopt(master_fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
  373. /* Set it to listen to specified port. */
  374. memset((void *)&sa, 0, sizeof(sa));
  375. #ifdef CONFIG_FEATURE_IPV6
  376. sa.sin6_family = AF_INET6;
  377. sa.sin6_port = htons(portnbr);
  378. #else
  379. sa.sin_family = AF_INET;
  380. sa.sin_port = htons(portnbr);
  381. #endif
  382. if (bind(master_fd, (struct sockaddr *) &sa, sizeof(sa)) < 0) {
  383. bb_perror_msg_and_die("bind");
  384. }
  385. if (listen(master_fd, 1) < 0) {
  386. bb_perror_msg_and_die("listen");
  387. }
  388. if (daemon(0, 0) < 0)
  389. bb_perror_msg_and_die("daemon");
  390. maxfd = master_fd;
  391. #endif /* CONFIG_FEATURE_TELNETD_INETD */
  392. do {
  393. struct tsession *ts;
  394. FD_ZERO(&rdfdset);
  395. FD_ZERO(&wrfdset);
  396. /* select on the master socket, all telnet sockets and their
  397. * ptys if there is room in their respective session buffers.
  398. */
  399. #ifndef CONFIG_FEATURE_TELNETD_INETD
  400. FD_SET(master_fd, &rdfdset);
  401. #endif /* CONFIG_FEATURE_TELNETD_INETD */
  402. ts = sessions;
  403. #ifndef CONFIG_FEATURE_TELNETD_INETD
  404. while (ts) {
  405. #endif /* CONFIG_FEATURE_TELNETD_INETD */
  406. /* buf1 is used from socket to pty
  407. * buf2 is used from pty to socket
  408. */
  409. if (ts->size1 > 0) {
  410. FD_SET(ts->ptyfd, &wrfdset); /* can write to pty */
  411. }
  412. if (ts->size1 < BUFSIZE) {
  413. #ifdef CONFIG_FEATURE_TELNETD_INETD
  414. FD_SET(ts->sockfd_read, &rdfdset); /* can read from socket */
  415. #else /* CONFIG_FEATURE_TELNETD_INETD */
  416. FD_SET(ts->sockfd, &rdfdset); /* can read from socket */
  417. #endif /* CONFIG_FEATURE_TELNETD_INETD */
  418. }
  419. if (ts->size2 > 0) {
  420. #ifdef CONFIG_FEATURE_TELNETD_INETD
  421. FD_SET(ts->sockfd_write, &wrfdset); /* can write to socket */
  422. #else /* CONFIG_FEATURE_TELNETD_INETD */
  423. FD_SET(ts->sockfd, &wrfdset); /* can write to socket */
  424. #endif /* CONFIG_FEATURE_TELNETD_INETD */
  425. }
  426. if (ts->size2 < BUFSIZE) {
  427. FD_SET(ts->ptyfd, &rdfdset); /* can read from pty */
  428. }
  429. #ifndef CONFIG_FEATURE_TELNETD_INETD
  430. ts = ts->next;
  431. }
  432. #endif /* CONFIG_FEATURE_TELNETD_INETD */
  433. selret = select(maxfd + 1, &rdfdset, &wrfdset, 0, 0);
  434. if (!selret)
  435. break;
  436. #ifndef CONFIG_FEATURE_TELNETD_INETD
  437. /* First check for and accept new sessions. */
  438. if (FD_ISSET(master_fd, &rdfdset)) {
  439. int fd, salen;
  440. salen = sizeof(sa);
  441. if ((fd = accept(master_fd, (struct sockaddr *)&sa,
  442. &salen)) < 0) {
  443. continue;
  444. } else {
  445. /* Create a new session and link it into
  446. our active list. */
  447. struct tsession *new_ts = make_new_session(fd);
  448. if (new_ts) {
  449. new_ts->next = sessions;
  450. sessions = new_ts;
  451. if (fd > maxfd)
  452. maxfd = fd;
  453. } else {
  454. close(fd);
  455. }
  456. }
  457. }
  458. /* Then check for data tunneling. */
  459. ts = sessions;
  460. while (ts) { /* For all sessions... */
  461. #endif /* CONFIG_FEATURE_TELNETD_INETD */
  462. #ifndef CONFIG_FEATURE_TELNETD_INETD
  463. struct tsession *next = ts->next; /* in case we free ts. */
  464. #endif /* CONFIG_FEATURE_TELNETD_INETD */
  465. if (ts->size1 && FD_ISSET(ts->ptyfd, &wrfdset)) {
  466. int num_totty;
  467. char *ptr;
  468. /* Write to pty from buffer 1. */
  469. ptr = remove_iacs(ts, &num_totty);
  470. w = write(ts->ptyfd, ptr, num_totty);
  471. if (w < 0) {
  472. #ifdef CONFIG_FEATURE_TELNETD_INETD
  473. exit(0);
  474. #else /* CONFIG_FEATURE_TELNETD_INETD */
  475. free_session(ts);
  476. ts = next;
  477. continue;
  478. #endif /* CONFIG_FEATURE_TELNETD_INETD */
  479. }
  480. ts->wridx1 += w;
  481. ts->size1 -= w;
  482. if (ts->wridx1 == BUFSIZE)
  483. ts->wridx1 = 0;
  484. }
  485. #ifdef CONFIG_FEATURE_TELNETD_INETD
  486. if (ts->size2 && FD_ISSET(ts->sockfd_write, &wrfdset)) {
  487. #else /* CONFIG_FEATURE_TELNETD_INETD */
  488. if (ts->size2 && FD_ISSET(ts->sockfd, &wrfdset)) {
  489. #endif /* CONFIG_FEATURE_TELNETD_INETD */
  490. /* Write to socket from buffer 2. */
  491. maxlen = MIN(BUFSIZE - ts->wridx2, ts->size2);
  492. #ifdef CONFIG_FEATURE_TELNETD_INETD
  493. w = write(ts->sockfd_write, ts->buf2 + ts->wridx2, maxlen);
  494. if (w < 0)
  495. exit(0);
  496. #else /* CONFIG_FEATURE_TELNETD_INETD */
  497. w = write(ts->sockfd, ts->buf2 + ts->wridx2, maxlen);
  498. if (w < 0) {
  499. free_session(ts);
  500. ts = next;
  501. continue;
  502. }
  503. #endif /* CONFIG_FEATURE_TELNETD_INETD */
  504. ts->wridx2 += w;
  505. ts->size2 -= w;
  506. if (ts->wridx2 == BUFSIZE)
  507. ts->wridx2 = 0;
  508. }
  509. #ifdef CONFIG_FEATURE_TELNETD_INETD
  510. if (ts->size1 < BUFSIZE && FD_ISSET(ts->sockfd_read, &rdfdset)) {
  511. #else /* CONFIG_FEATURE_TELNETD_INETD */
  512. if (ts->size1 < BUFSIZE && FD_ISSET(ts->sockfd, &rdfdset)) {
  513. #endif /* CONFIG_FEATURE_TELNETD_INETD */
  514. /* Read from socket to buffer 1. */
  515. maxlen = MIN(BUFSIZE - ts->rdidx1,
  516. BUFSIZE - ts->size1);
  517. #ifdef CONFIG_FEATURE_TELNETD_INETD
  518. r = read(ts->sockfd_read, ts->buf1 + ts->rdidx1, maxlen);
  519. if (!r || (r < 0 && errno != EINTR))
  520. exit(0);
  521. #else /* CONFIG_FEATURE_TELNETD_INETD */
  522. r = read(ts->sockfd, ts->buf1 + ts->rdidx1, maxlen);
  523. if (!r || (r < 0 && errno != EINTR)) {
  524. free_session(ts);
  525. ts = next;
  526. continue;
  527. }
  528. #endif /* CONFIG_FEATURE_TELNETD_INETD */
  529. if(!*(ts->buf1 + ts->rdidx1 + r - 1)) {
  530. r--;
  531. if(!r)
  532. continue;
  533. }
  534. ts->rdidx1 += r;
  535. ts->size1 += r;
  536. if (ts->rdidx1 == BUFSIZE)
  537. ts->rdidx1 = 0;
  538. }
  539. if (ts->size2 < BUFSIZE && FD_ISSET(ts->ptyfd, &rdfdset)) {
  540. /* Read from pty to buffer 2. */
  541. maxlen = MIN(BUFSIZE - ts->rdidx2,
  542. BUFSIZE - ts->size2);
  543. r = read(ts->ptyfd, ts->buf2 + ts->rdidx2, maxlen);
  544. if (!r || (r < 0 && errno != EINTR)) {
  545. #ifdef CONFIG_FEATURE_TELNETD_INETD
  546. exit(0);
  547. #else /* CONFIG_FEATURE_TELNETD_INETD */
  548. free_session(ts);
  549. ts = next;
  550. continue;
  551. #endif /* CONFIG_FEATURE_TELNETD_INETD */
  552. }
  553. ts->rdidx2 += r;
  554. ts->size2 += r;
  555. if (ts->rdidx2 == BUFSIZE)
  556. ts->rdidx2 = 0;
  557. }
  558. if (ts->size1 == 0) {
  559. ts->rdidx1 = 0;
  560. ts->wridx1 = 0;
  561. }
  562. if (ts->size2 == 0) {
  563. ts->rdidx2 = 0;
  564. ts->wridx2 = 0;
  565. }
  566. #ifndef CONFIG_FEATURE_TELNETD_INETD
  567. ts = next;
  568. }
  569. #endif /* CONFIG_FEATURE_TELNETD_INETD */
  570. } while (1);
  571. return 0;
  572. }