telnetd.c 16 KB

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