1 /* vi: set sw=4 ts=4: */
4 * Bjorn Wesen, Axis Communications AB (bjornw@axis.com)
6 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
8 * ---------------------------------------------------------------------------
9 * (C) Copyright 2000, Axis Communications AB, LUND, SWEDEN
10 ****************************************************************************
12 * The telnetd manpage says it all:
14 * Telnetd operates by allocating a pseudo-terminal device (see pty(4)) for
15 * a client, then creating a login process which has the slave side of the
16 * pseudo-terminal as stdin, stdout, and stderr. Telnetd manipulates the
17 * master side of the pseudo-terminal, implementing the telnet protocol and
18 * passing characters between the remote client and the login process.
20 * Vladimir Oleynik <dzo@simtreas.ru> 2001
21 * Set process group corrections, initial busybox port
23 //config:config TELNETD
24 //config: bool "telnetd (12 kb)"
26 //config: select FEATURE_SYSLOG
28 //config: A daemon for the TELNET protocol, allowing you to log onto the host
29 //config: running the daemon. Please keep in mind that the TELNET protocol
30 //config: sends passwords in plain text. If you can't afford the space for an
31 //config: SSH daemon and you trust your network, you may say 'y' here. As a
32 //config: more secure alternative, you should seriously consider installing the
33 //config: very small Dropbear SSH daemon instead:
34 //config: http://matt.ucc.asn.au/dropbear/dropbear.html
36 //config: Note that for busybox telnetd to work you need several things:
37 //config: First of all, your kernel needs:
38 //config: CONFIG_UNIX98_PTYS=y
40 //config: Next, you need a /dev/pts directory on your root filesystem:
42 //config: $ ls -ld /dev/pts
43 //config: drwxr-xr-x 2 root root 0 Sep 23 13:21 /dev/pts/
45 //config: Next you need the pseudo terminal master multiplexer /dev/ptmx:
47 //config: $ ls -la /dev/ptmx
48 //config: crw-rw-rw- 1 root tty 5, 2 Sep 23 13:55 /dev/ptmx
50 //config: Any /dev/ttyp[0-9]* files you may have can be removed.
51 //config: Next, you need to mount the devpts filesystem on /dev/pts using:
53 //config: mount -t devpts devpts /dev/pts
55 //config: You need to be sure that busybox has LOGIN and
56 //config: FEATURE_SUID enabled. And finally, you should make
57 //config: certain that busybox has been installed setuid root:
59 //config: chown root.root /bin/busybox
60 //config: chmod 4755 /bin/busybox
62 //config: with all that done, telnetd _should_ work....
64 //config:config FEATURE_TELNETD_STANDALONE
65 //config: bool "Support standalone telnetd (not inetd only)"
67 //config: depends on TELNETD
69 //config: Selecting this will make telnetd able to run standalone.
71 //config:config FEATURE_TELNETD_INETD_WAIT
72 //config: bool "Support -w SEC option (inetd wait mode)"
74 //config: depends on FEATURE_TELNETD_STANDALONE
76 //config: This option allows you to run telnetd in "inet wait" mode.
77 //config: Example inetd.conf line (note "wait", not usual "nowait"):
79 //config: telnet stream tcp wait root /bin/telnetd telnetd -w10
81 //config: In this example, inetd passes _listening_ socket_ as fd 0
82 //config: to telnetd when connection appears.
83 //config: telnetd will wait for connections until all existing
84 //config: connections are closed, and no new connections
85 //config: appear during 10 seconds. Then it exits, and inetd continues
86 //config: to listen for new connections.
88 //config: This option is rarely used. "tcp nowait" is much more usual
89 //config: way of running tcp services, including telnetd.
90 //config: You most probably want to say N here.
92 //applet:IF_TELNETD(APPLET(telnetd, BB_DIR_USR_SBIN, BB_SUID_DROP))
94 //kbuild:lib-$(CONFIG_TELNETD) += telnetd.o
96 //usage:#define telnetd_trivial_usage
98 //usage:#define telnetd_full_usage "\n\n"
99 //usage: "Handle incoming telnet connections"
100 //usage: IF_NOT_FEATURE_TELNETD_STANDALONE(" via inetd") "\n"
101 //usage: "\n -l LOGIN Exec LOGIN on connect"
102 //usage: "\n -f ISSUE_FILE Display ISSUE_FILE instead of /etc/issue"
103 //usage: "\n -K Close connection as soon as login exits"
104 //usage: "\n (normally wait until all programs close slave pty)"
105 //usage: IF_FEATURE_TELNETD_STANDALONE(
106 //usage: "\n -p PORT Port to listen on"
107 //usage: "\n -b ADDR[:PORT] Address to bind to"
108 //usage: "\n -F Run in foreground"
109 //usage: "\n -i Inetd mode"
110 //usage: IF_FEATURE_TELNETD_INETD_WAIT(
111 //usage: "\n -w SEC Inetd 'wait' mode, linger time SEC"
112 //usage: "\n -S Log to syslog (implied by -i or without -F and -w)"
119 #include "common_bufsiz.h"
126 #include <arpa/telnet.h>
130 struct tsession *next;
135 smallint buffered_IAC_for_pty;
137 /* two circular buffers */
138 /*char *buf1, *buf2;*/
139 /*#define TS_BUF1(ts) ts->buf1*/
140 /*#define TS_BUF2(ts) TS_BUF2(ts)*/
141 #define TS_BUF1(ts) ((unsigned char*)(ts + 1))
142 #define TS_BUF2(ts) (((unsigned char*)(ts + 1)) + BUFSIZE)
143 int rdidx1, wridx1, size1;
144 int rdidx2, wridx2, size2;
147 /* Two buffers are directly after tsession in malloced memory.
148 * Make whole thing fit in 4k */
149 enum { BUFSIZE = (4 * 1024 - sizeof(struct tsession)) / 2 };
154 struct tsession *sessions;
155 const char *loginpath;
156 const char *issuefile;
159 #define G (*(struct globals*)bb_common_bufsiz1)
160 #define INIT_G() do { \
161 setup_common_bufsiz(); \
162 G.loginpath = "/bin/login"; \
163 G.issuefile = "/etc/issue.net"; \
167 /* Write some buf1 data to pty, processing IACs.
168 * Update wridx1 and size1. Return < 0 on error.
169 * Buggy if IAC is present but incomplete: skips them.
172 safe_write_to_pty_decode_iac(struct tsession *ts)
177 unsigned char *found;
179 buf = TS_BUF1(ts) + ts->wridx1;
180 wr = MIN(BUFSIZE - ts->wridx1, ts->size1);
181 /* wr is at least 1 here */
183 if (ts->buffered_IAC_for_pty) {
184 /* Last time we stopped on a "dangling" IAC byte.
185 * We removed it from the buffer back then.
186 * Now pretend it's still there, and jump to IAC processing.
188 ts->buffered_IAC_for_pty = 0;
191 buf--; /* Yes, this can point before the buffer. It's ok */
196 found = memchr(buf, IAC, wr);
198 /* There is a "prefix" of non-IAC chars.
199 * Write only them, and return.
204 /* We map \r\n ==> \r for pragmatic reasons:
205 * many client implementations send \r\n when
206 * the user hits the CarriageReturn key.
207 * See RFC 1123 3.3.1 Telnet End-of-Line Convention.
210 found = memchr(buf, '\r', wr);
212 rc = found - buf + 1;
213 rc = safe_write(ts->ptyfd, buf, rc);
216 if (rc < wr /* don't look past available data */
217 && buf[rc-1] == '\r' /* need this: imagine that write was _short_ */
218 && (buf[rc] == '\n' || buf[rc] == '\0')
222 goto update_and_return;
225 /* buf starts with IAC char. Process that sequence.
226 * Example: we get this from our own (bbox) telnet client:
227 * read(5, "\377\374\1""\377\373\37""\377\372\37\0\262\0@\377\360""\377\375\1""\377\375\3"):
228 * IAC WONT ECHO, IAC WILL NAWS, IAC SB NAWS <cols> <rows> IAC SE, IAC DO SGA
229 * Another example (telnet-0.17 from old-netkit):
230 * read(4, "\377\375\3""\377\373\30""\377\373\37""\377\373 ""\377\373!""\377\373\"""\377\373'"
231 * "\377\375\5""\377\373#""\377\374\1""\377\372\37\0\257\0I\377\360""\377\375\1"):
232 * IAC DO SGA, IAC WILL TTYPE, IAC WILL NAWS, IAC WILL TSPEED, IAC WILL LFLOW, IAC WILL LINEMODE, IAC WILL NEW_ENVIRON,
233 * IAC DO STATUS, IAC WILL XDISPLOC, IAC WONT ECHO, IAC SB NAWS <cols> <rows> IAC SE, IAC DO ECHO
236 /* Only the single IAC byte is in the buffer, eat it
237 * and set a flag "process the rest of the sequence
238 * next time we are here".
240 //bb_error_msg("dangling IAC!");
241 ts->buffered_IAC_for_pty = 1;
243 goto update_and_return;
247 /* 2-byte commands (240..250 and 255):
248 * IAC IAC (255) Literal 255. Supported.
249 * IAC SE (240) End of subnegotiation. Treated as NOP.
250 * IAC NOP (241) NOP. Supported.
251 * IAC BRK (243) Break. Like serial line break. TODO via tcsendbreak()?
252 * IAC AYT (246) Are you there. Send back evidence that AYT was seen. TODO (send NOP back)?
253 * These don't look useful:
254 * IAC DM (242) Data mark. What is this?
255 * IAC IP (244) Suspend, interrupt or abort the process. (Ancient cousin of ^C).
256 * IAC AO (245) Abort output. "You can continue running, but do not send me the output".
257 * IAC EC (247) Erase character. The receiver should delete the last received char.
258 * IAC EL (248) Erase line. The receiver should delete everything up tp last newline.
259 * IAC GA (249) Go ahead. For half-duplex lines: "now you talk".
260 * Implemented only as part of NAWS:
261 * IAC SB (250) Subnegotiation of an option follows.
264 /* Literal 255 (emacs M-DEL) */
265 //bb_error_msg("255!");
266 rc = safe_write(ts->ptyfd, &buf[1], 1);
268 * If we went through buffered_IAC_for_pty==1 path,
269 * bailing out on error like below messes up the buffer.
270 * EAGAIN is highly unlikely here, other errors will be
271 * repeated on next write, let's just skip error check.
278 goto update_and_return;
280 if (buf[1] >= 240 && buf[1] <= 249) {
281 /* NOP (241). Ignore (putty keepalive, etc) */
282 /* All other 2-byte commands also treated as NOPs here */
284 goto update_and_return;
288 /* BUG: only 2 bytes of the IAC is in the buffer, we just eat them.
289 * This is not a practical problem since >2 byte IACs are seen only
290 * in initial negotiation, when buffer is empty
293 goto update_and_return;
297 if (buf[2] == TELOPT_NAWS) {
298 /* IAC SB, TELOPT_NAWS, 4-byte, IAC SE */
301 /* BUG: incomplete, can't process */
303 goto update_and_return;
305 memset(&ws, 0, sizeof(ws)); /* pixel sizes are set to 0 */
306 ws.ws_col = (buf[3] << 8) | buf[4];
307 ws.ws_row = (buf[5] << 8) | buf[6];
308 ioctl(ts->ptyfd, TIOCSWINSZ, (char *)&ws);
310 /* trailing IAC SE will be eaten separately, as 2-byte NOP */
311 goto update_and_return;
313 /* else: other subnegs not supported yet */
316 /* Assume it is a 3-byte WILL/WONT/DO/DONT 251..254 command and skip it */
318 fprintf(stderr, "Ignoring IAC %s,%s\n",
319 TELCMD(buf[1]), TELOPT(buf[2]));
325 if (ts->wridx1 >= BUFSIZE) /* actually == BUFSIZE */
329 * Hack. We cannot process IACs which wrap around buffer's end.
330 * Since properly fixing it requires writing bigger code,
331 * we rely instead on this code making it virtually impossible
332 * to have wrapped IAC (people don't type at 2k/second).
333 * It also allows for bigger reads in common case.
335 if (ts->size1 == 0) { /* very typical */
336 //bb_error_msg("zero size1");
342 if (wr != 0 && wr < ts->rdidx1) {
343 /* Buffer is not wrapped yet.
344 * We can easily move it to the beginning.
346 //bb_error_msg("moved %d", wr);
347 memmove(TS_BUF1(ts), TS_BUF1(ts) + wr, ts->size1);
355 * Converting single IAC into double on output
357 static size_t safe_write_double_iac(int fd, const char *buf, size_t count)
360 size_t wr, rc, total;
366 if (*buf == (char)IAC) {
367 static const char IACIAC[] ALIGN1 = { IAC, IAC };
368 rc = safe_write(fd, IACIAC, 2);
369 /* BUG: if partial write was only 1 byte long, we end up emitting just one IAC */
377 /* count != 0, *buf != IAC */
378 IACptr = memchr(buf, IAC, count);
382 rc = safe_write(fd, buf, wr);
389 /* here: rc - result of last short write */
390 if ((ssize_t)rc < 0) { /* error? */
398 /* Must match getopt32 string */
400 OPT_WATCHCHILD = (1 << 2), /* -K */
401 OPT_INETD = (1 << 3) * ENABLE_FEATURE_TELNETD_STANDALONE, /* -i */
402 OPT_PORT = (1 << 4) * ENABLE_FEATURE_TELNETD_STANDALONE, /* -p PORT */
403 OPT_FOREGROUND = (1 << 6) * ENABLE_FEATURE_TELNETD_STANDALONE, /* -F */
404 OPT_SYSLOG = (1 << 7) * ENABLE_FEATURE_TELNETD_INETD_WAIT, /* -S */
405 OPT_WAIT = (1 << 8) * ENABLE_FEATURE_TELNETD_INETD_WAIT, /* -w SEC */
408 static struct tsession *
410 IF_FEATURE_TELNETD_STANDALONE(int sock)
411 IF_NOT_FEATURE_TELNETD_STANDALONE(void)
413 #if !ENABLE_FEATURE_TELNETD_STANDALONE
416 const char *login_argv[2];
417 struct termios termbuf;
419 char tty_name[GETPTY_BUFSIZE];
420 struct tsession *ts = xzalloc(sizeof(struct tsession) + BUFSIZE * 2);
422 /*ts->buf1 = (char *)(ts + 1);*/
423 /*ts->buf2 = ts->buf1 + BUFSIZE;*/
425 /* Got a new connection, set up a tty */
426 fd = xgetpty(tty_name);
431 close_on_exec_on(fd);
433 /* SO_KEEPALIVE by popular demand */
434 setsockopt_keepalive(sock);
435 #if ENABLE_FEATURE_TELNETD_STANDALONE
436 ts->sockfd_read = sock;
438 if (sock == 0) { /* We are called with fd 0 - we are in inetd mode */
439 sock++; /* so use fd 1 for output */
442 ts->sockfd_write = sock;
446 /* ts->sockfd_read = 0; - done by xzalloc */
447 ts->sockfd_write = 1;
452 /* Make the telnet client understand we will echo characters so it
453 * should not do it locally. We don't tell the client to run linemode,
454 * because we want to handle line editing and tab completion and other
455 * stuff that requires char-by-char support. */
457 static const char iacs_to_send[] ALIGN1 = {
458 IAC, DO, TELOPT_ECHO,
459 IAC, DO, TELOPT_NAWS,
460 /* This requires telnetd.ctrlSQ.patch (incomplete) */
461 /*IAC, DO, TELOPT_LFLOW,*/
462 IAC, WILL, TELOPT_ECHO,
463 IAC, WILL, TELOPT_SGA
465 /* This confuses safe_write_double_iac(), it will try to duplicate
467 //memcpy(TS_BUF2(ts), iacs_to_send, sizeof(iacs_to_send));
468 //ts->rdidx2 = sizeof(iacs_to_send);
469 //ts->size2 = sizeof(iacs_to_send);
470 /* So just stuff it into TCP stream! (no error check...) */
471 #if ENABLE_FEATURE_TELNETD_STANDALONE
472 safe_write(sock, iacs_to_send, sizeof(iacs_to_send));
474 safe_write(1, iacs_to_send, sizeof(iacs_to_send));
476 /*ts->rdidx2 = 0; - xzalloc did it */
481 pid = vfork(); /* NOMMU-friendly */
485 /* sock will be closed by caller */
486 bb_perror_msg("vfork");
496 /* Careful - we are after vfork! */
498 /* Restore default signal handling ASAP */
499 bb_signals((1 << SIGCHLD) + (1 << SIGPIPE), SIG_DFL);
503 if (ENABLE_FEATURE_UTMP) {
504 len_and_sockaddr *lsa = get_peer_lsa(sock);
505 char *hostname = NULL;
507 hostname = xmalloc_sockaddr2dotted(&lsa->u.sa);
510 write_new_utmp(pid, LOGIN_PROCESS, tty_name, /*username:*/ "LOGIN", hostname);
514 /* Make new session and process group */
517 /* Open the child's side of the tty */
518 /* NB: setsid() disconnects from any previous ctty's. Therefore
519 * we must open child's side of the tty AFTER setsid! */
521 xopen(tty_name, O_RDWR); /* becomes our ctty */
524 tcsetpgrp(0, pid); /* switch this tty's process group to us */
526 /* The pseudo-terminal allocated to the client is configured to operate
527 * in cooked mode, and with XTABS CRMOD enabled (see tty(4)) */
528 tcgetattr(0, &termbuf);
529 termbuf.c_lflag |= ECHO; /* if we use readline we dont want this */
530 termbuf.c_oflag |= ONLCR | XTABS;
531 termbuf.c_iflag |= ICRNL;
532 termbuf.c_iflag &= ~IXOFF;
533 /*termbuf.c_lflag &= ~ICANON;*/
534 tcsetattr_stdin_TCSANOW(&termbuf);
536 /* Uses FILE-based I/O to stdout, but does fflush_all(),
537 * so should be safe with vfork.
538 * I fear, though, that some users will have ridiculously big
539 * issue files, and they may block writing to fd 1,
540 * (parent is supposed to read it, but parent waits
541 * for vforked child to exec!) */
542 print_login_issue(G.issuefile, tty_name);
544 /* Exec shell / login / whatever */
545 login_argv[0] = G.loginpath;
546 login_argv[1] = NULL;
547 /* exec busybox applet (if PREFER_APPLETS=y), if that fails,
548 * exec external program.
549 * NB: sock is either 0 or has CLOEXEC set on it.
550 * fd has CLOEXEC set on it too. These two fds will be closed here.
552 BB_EXECVP(G.loginpath, (char **)login_argv);
553 /* _exit is safer with vfork, and we shouldn't send message
554 * to remote clients anyway */
555 _exit(EXIT_FAILURE); /*bb_perror_msg_and_die("execv %s", G.loginpath);*/
558 #if ENABLE_FEATURE_TELNETD_STANDALONE
561 free_session(struct tsession *ts)
565 if (option_mask32 & OPT_INETD)
568 /* Unlink this telnet session from the session list */
571 G.sessions = ts->next;
573 while (t->next != ts)
579 /* It was said that "normal" telnetd just closes ptyfd,
580 * doesn't send SIGKILL. When we close ptyfd,
581 * kernel sends SIGHUP to processes having slave side opened. */
582 kill(ts->shell_pid, SIGKILL);
583 waitpid(ts->shell_pid, NULL, 0);
586 close(ts->sockfd_read);
587 /* We do not need to close(ts->sockfd_write), it's the same
588 * as sockfd_read unless we are in inetd mode. But in inetd mode
589 * we do not reach this */
592 /* Scan all sessions and find new maxfd */
596 if (G.maxfd < ts->ptyfd)
598 if (G.maxfd < ts->sockfd_read)
599 G.maxfd = ts->sockfd_read;
601 /* Again, sockfd_write == sockfd_read here */
602 if (G.maxfd < ts->sockfd_write)
603 G.maxfd = ts->sockfd_write;
609 #else /* !FEATURE_TELNETD_STANDALONE */
611 /* Used in main() only, thus "return 0" actually is exit(EXIT_SUCCESS). */
612 #define free_session(ts) return 0
616 static void handle_sigchld(int sig UNUSED_PARAM)
620 int save_errno = errno;
622 /* Looping: more than one child may have exited */
624 pid = wait_any_nohang(NULL);
629 if (ts->shell_pid == pid) {
631 update_utmp_DEAD_PROCESS(pid);
641 int telnetd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
642 int telnetd_main(int argc UNUSED_PARAM, char **argv)
644 fd_set rdfdset, wrfdset;
648 #if ENABLE_FEATURE_TELNETD_STANDALONE
649 #define IS_INETD (opt & OPT_INETD)
650 int master_fd = master_fd; /* for compiler */
651 int sec_linger = sec_linger;
652 char *opt_bindaddr = NULL;
662 /* Even if !STANDALONE, we accept (and ignore) -i, thus people
663 * don't need to guess whether it's ok to pass -i to us */
664 opt = getopt32(argv, "^"
666 IF_FEATURE_TELNETD_STANDALONE("p:b:F")
667 IF_FEATURE_TELNETD_INETD_WAIT("Sw:+") /* -w NUM */
669 /* -w implies -F. -w and -i don't mix */
670 IF_FEATURE_TELNETD_INETD_WAIT("wF:i--w:w--i"),
671 &G.issuefile, &G.loginpath
672 IF_FEATURE_TELNETD_STANDALONE(, &opt_portnbr, &opt_bindaddr)
673 IF_FEATURE_TELNETD_INETD_WAIT(, &sec_linger)
675 if (!IS_INETD /*&& !re_execed*/) {
676 /* inform that we start in standalone mode?
677 * May be useful when people forget to give -i */
678 /*bb_error_msg("listening for connections");*/
679 if (!(opt & OPT_FOREGROUND)) {
680 /* DAEMON_CHDIR_ROOT was giving inconsistent
681 * behavior with/without -F, -i */
682 bb_daemonize_or_rexec(0 /*was DAEMON_CHDIR_ROOT*/, argv);
685 /* Redirect log to syslog early, if needed */
686 if (IS_INETD || (opt & OPT_SYSLOG) || !(opt & OPT_FOREGROUND)) {
687 openlog(applet_name, LOG_PID, LOG_DAEMON);
688 logmode = LOGMODE_SYSLOG;
690 #if ENABLE_FEATURE_TELNETD_STANDALONE
692 G.sessions = make_new_session(0);
693 if (!G.sessions) /* pty opening or vfork problem, exit */
694 return 1; /* make_new_session printed error message */
697 if (!(opt & OPT_WAIT)) {
698 unsigned portnbr = 23;
700 portnbr = xatou16(opt_portnbr);
701 master_fd = create_and_bind_stream_or_die(opt_bindaddr, portnbr);
702 xlisten(master_fd, 1);
704 close_on_exec_on(master_fd);
707 G.sessions = make_new_session();
708 if (!G.sessions) /* pty opening or vfork problem, exit */
709 return 1; /* make_new_session printed error message */
712 /* We don't want to die if just one session is broken */
713 signal(SIGPIPE, SIG_IGN);
715 if (opt & OPT_WATCHCHILD)
716 signal(SIGCHLD, handle_sigchld);
717 else /* prevent dead children from becoming zombies */
718 signal(SIGCHLD, SIG_IGN);
721 This is how the buffers are used. The arrows indicate data flow.
723 +-------+ wridx1++ +------+ rdidx1++ +----------+
724 | | <-------------- | buf1 | <-------------- | |
725 | | size1-- +------+ size1++ | |
727 | | rdidx2++ +------+ wridx2++ | |
728 | | --------------> | buf2 | --------------> | |
729 +-------+ size2++ +------+ size2-- +----------+
731 size1: "how many bytes are buffered for pty between rdidx1 and wridx1?"
732 size2: "how many bytes are buffered for socket between rdidx2 and wridx2?"
734 Each session has got two buffers. Buffers are circular. If sizeN == 0,
735 buffer is empty. If sizeN == BUFSIZE, buffer is full. In both these cases
742 /* Select on the master socket, all telnet sockets and their
743 * ptys if there is room in their session buffers.
744 * NB: scalability problem: we recalculate entire bitmap
745 * before each select. Can be a problem with 500+ connections. */
748 struct tsession *next = ts->next; /* in case we free ts */
749 if (ts->shell_pid == -1) {
750 /* Child died and we detected that */
753 if (ts->size1 > 0) /* can write to pty */
754 FD_SET(ts->ptyfd, &wrfdset);
755 if (ts->size1 < BUFSIZE) /* can read from socket */
756 FD_SET(ts->sockfd_read, &rdfdset);
757 if (ts->size2 > 0) /* can write to socket */
758 FD_SET(ts->sockfd_write, &wrfdset);
759 if (ts->size2 < BUFSIZE) /* can read from pty */
760 FD_SET(ts->ptyfd, &rdfdset);
765 FD_SET(master_fd, &rdfdset);
766 /* This is needed because free_session() does not
767 * take master_fd into account when it finds new
768 * maxfd among remaining fd's */
769 if (master_fd > G.maxfd)
774 struct timeval *tv_ptr = NULL;
775 #if ENABLE_FEATURE_TELNETD_INETD_WAIT
777 if ((opt & OPT_WAIT) && !G.sessions) {
778 tv.tv_sec = sec_linger;
783 count = select(G.maxfd + 1, &rdfdset, &wrfdset, NULL, tv_ptr);
785 if (count == 0) /* "telnetd -w SEC" timed out */
788 goto again; /* EINTR or ENOMEM */
790 #if ENABLE_FEATURE_TELNETD_STANDALONE
791 /* Check for and accept new sessions */
792 if (!IS_INETD && FD_ISSET(master_fd, &rdfdset)) {
794 struct tsession *new_ts;
796 fd = accept(master_fd, NULL, NULL);
799 close_on_exec_on(fd);
801 /* Create a new session and link it into active list */
802 new_ts = make_new_session(fd);
804 new_ts->next = G.sessions;
812 /* Then check for data tunneling */
814 while (ts) { /* For all sessions... */
815 struct tsession *next = ts->next; /* in case we free ts */
817 if (/*ts->size1 &&*/ FD_ISSET(ts->ptyfd, &wrfdset)) {
818 /* Write to pty from buffer 1 */
819 count = safe_write_to_pty_decode_iac(ts);
827 if (/*ts->size2 &&*/ FD_ISSET(ts->sockfd_write, &wrfdset)) {
828 /* Write to socket from buffer 2 */
829 count = MIN(BUFSIZE - ts->wridx2, ts->size2);
830 count = safe_write_double_iac(ts->sockfd_write, (void*)(TS_BUF2(ts) + ts->wridx2), count);
837 if (ts->wridx2 >= BUFSIZE) /* actually == BUFSIZE */
840 if (ts->size2 == 0) {
847 if (/*ts->size1 < BUFSIZE &&*/ FD_ISSET(ts->sockfd_read, &rdfdset)) {
848 /* Read from socket to buffer 1 */
849 count = MIN(BUFSIZE - ts->rdidx1, BUFSIZE - ts->size1);
850 count = safe_read(ts->sockfd_read, TS_BUF1(ts) + ts->rdidx1, count);
852 if (count < 0 && errno == EAGAIN)
856 /* Ignore trailing NUL if it is there */
857 if (!TS_BUF1(ts)[ts->rdidx1 + count - 1]) {
862 if (ts->rdidx1 >= BUFSIZE) /* actually == BUFSIZE */
866 if (/*ts->size2 < BUFSIZE &&*/ FD_ISSET(ts->ptyfd, &rdfdset)) {
867 /* Read from pty to buffer 2 */
868 count = MIN(BUFSIZE - ts->rdidx2, BUFSIZE - ts->size2);
869 count = safe_read(ts->ptyfd, TS_BUF2(ts) + ts->rdidx2, count);
871 if (count < 0 && errno == EAGAIN)
877 if (ts->rdidx2 >= BUFSIZE) /* actually == BUFSIZE */
884 if (ts->shell_pid > 0)
885 update_utmp_DEAD_PROCESS(ts->shell_pid);