blob: bd60c8681b8ffed0c0de6847cf878e59d8083b25 [file] [log] [blame]
Bernhard Reutner-Fischer2c998512006-04-12 18:09:26 +00001/* vi: set sw=4 ts=4: */
Bernhard Reutner-Fischerdac7ff12006-04-12 17:55:51 +00002/*
Eric Andersen08a72202002-09-30 20:52:10 +00003 * Simple telnet server
4 * Bjorn Wesen, Axis Communications AB (bjornw@axis.com)
5 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02006 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Eric Andersen08a72202002-09-30 20:52:10 +00007 *
8 * ---------------------------------------------------------------------------
9 * (C) Copyright 2000, Axis Communications AB, LUND, SWEDEN
10 ****************************************************************************
11 *
12 * The telnetd manpage says it all:
13 *
Denys Vlasenkoa4bcbd02009-06-09 23:01:24 +020014 * 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.
Eric Andersen08a72202002-09-30 20:52:10 +000019 *
20 * Vladimir Oleynik <dzo@simtreas.ru> 2001
Denys Vlasenkoa4bcbd02009-06-09 23:01:24 +020021 * Set process group corrections, initial busybox port
Eric Andersen08a72202002-09-30 20:52:10 +000022 */
Denys Vlasenko47367e12016-11-23 09:05:14 +010023//config:config TELNETD
Denys Vlasenko4eed2c62017-07-18 22:01:24 +020024//config: bool "telnetd (12 kb)"
Denys Vlasenko47367e12016-11-23 09:05:14 +010025//config: default y
26//config: select FEATURE_SYSLOG
27//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020028//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:
Denys Vlasenko47367e12016-11-23 09:05:14 +010034//config: http://matt.ucc.asn.au/dropbear/dropbear.html
35//config:
Denys Vlasenko72089cf2017-07-21 09:50:55 +020036//config: Note that for busybox telnetd to work you need several things:
37//config: First of all, your kernel needs:
Denys Vlasenko47367e12016-11-23 09:05:14 +010038//config: CONFIG_UNIX98_PTYS=y
39//config:
Denys Vlasenko72089cf2017-07-21 09:50:55 +020040//config: Next, you need a /dev/pts directory on your root filesystem:
Denys Vlasenko47367e12016-11-23 09:05:14 +010041//config:
42//config: $ ls -ld /dev/pts
43//config: drwxr-xr-x 2 root root 0 Sep 23 13:21 /dev/pts/
44//config:
Denys Vlasenko72089cf2017-07-21 09:50:55 +020045//config: Next you need the pseudo terminal master multiplexer /dev/ptmx:
Denys Vlasenko47367e12016-11-23 09:05:14 +010046//config:
47//config: $ ls -la /dev/ptmx
48//config: crw-rw-rw- 1 root tty 5, 2 Sep 23 13:55 /dev/ptmx
49//config:
Denys Vlasenko72089cf2017-07-21 09:50:55 +020050//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:
Denys Vlasenko47367e12016-11-23 09:05:14 +010052//config:
53//config: mount -t devpts devpts /dev/pts
54//config:
Denys Vlasenko72089cf2017-07-21 09:50:55 +020055//config: You need to be sure that busybox has LOGIN and
56//config: FEATURE_SUID enabled. And finally, you should make
Denys Vlasenko68b653b2017-07-27 10:53:09 +020057//config: certain that busybox has been installed setuid root:
Denys Vlasenko47367e12016-11-23 09:05:14 +010058//config:
59//config: chown root.root /bin/busybox
60//config: chmod 4755 /bin/busybox
61//config:
Denys Vlasenko72089cf2017-07-21 09:50:55 +020062//config: with all that done, telnetd _should_ work....
Denys Vlasenko47367e12016-11-23 09:05:14 +010063//config:
64//config:config FEATURE_TELNETD_STANDALONE
65//config: bool "Support standalone telnetd (not inetd only)"
66//config: default y
67//config: depends on TELNETD
68//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020069//config: Selecting this will make telnetd able to run standalone.
Denys Vlasenko47367e12016-11-23 09:05:14 +010070//config:
71//config:config FEATURE_TELNETD_INETD_WAIT
72//config: bool "Support -w SEC option (inetd wait mode)"
73//config: default y
74//config: depends on FEATURE_TELNETD_STANDALONE
75//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020076//config: This option allows you to run telnetd in "inet wait" mode.
77//config: Example inetd.conf line (note "wait", not usual "nowait"):
Denys Vlasenko47367e12016-11-23 09:05:14 +010078//config:
Denys Vlasenko72089cf2017-07-21 09:50:55 +020079//config: telnet stream tcp wait root /bin/telnetd telnetd -w10
Denys Vlasenko47367e12016-11-23 09:05:14 +010080//config:
Denys Vlasenko72089cf2017-07-21 09:50:55 +020081//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.
Denys Vlasenko47367e12016-11-23 09:05:14 +010087//config:
Denys Vlasenko72089cf2017-07-21 09:50:55 +020088//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.
Denys Vlasenko47367e12016-11-23 09:05:14 +010091
92//applet:IF_TELNETD(APPLET(telnetd, BB_DIR_USR_SBIN, BB_SUID_DROP))
93
94//kbuild:lib-$(CONFIG_TELNETD) += telnetd.o
Pere Orga5bc8c002011-04-11 03:29:49 +020095
96//usage:#define telnetd_trivial_usage
97//usage: "[OPTIONS]"
98//usage:#define telnetd_full_usage "\n\n"
99//usage: "Handle incoming telnet connections"
100//usage: IF_NOT_FEATURE_TELNETD_STANDALONE(" via inetd") "\n"
Pere Orga5bc8c002011-04-11 03:29:49 +0200101//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)"
113//usage: )
114//usage: )
115
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000116#define DEBUG 0
Eric Andersen08a72202002-09-30 20:52:10 +0000117
Denis Vlasenkob6adbf12007-05-26 19:00:18 +0000118#include "libbb.h"
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +0200119#include "common_bufsiz.h"
Bernhard Reutner-Fischerf4701962008-01-27 12:50:12 +0000120#include <syslog.h>
Rob Landley099ed502006-08-28 09:41:49 +0000121
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000122#if DEBUG
Denys Vlasenko3a416112010-04-05 22:10:38 +0200123# define TELCMDS
124# define TELOPTS
Eric Andersen08a72202002-09-30 20:52:10 +0000125#endif
126#include <arpa/telnet.h>
Eric Andersen08a72202002-09-30 20:52:10 +0000127
Denys Vlasenko3a416112010-04-05 22:10:38 +0200128
Eric Andersen08a72202002-09-30 20:52:10 +0000129struct tsession {
130 struct tsession *next;
Denis Vlasenkod814c982009-02-02 23:43:57 +0000131 pid_t shell_pid;
Denys Vlasenko1d77db82009-06-10 13:38:08 +0200132 int sockfd_read;
133 int sockfd_write;
134 int ptyfd;
Denys Vlasenko122c47a2016-10-12 19:13:46 +0200135 smallint buffered_IAC_for_pty;
Denis Vlasenko59d7c432007-10-15 15:19:36 +0000136
Eric Andersen08a72202002-09-30 20:52:10 +0000137 /* two circular buffers */
Denis Vlasenko59d7c432007-10-15 15:19:36 +0000138 /*char *buf1, *buf2;*/
Denys Vlasenkoa4bcbd02009-06-09 23:01:24 +0200139/*#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)
Eric Andersen08a72202002-09-30 20:52:10 +0000143 int rdidx1, wridx1, size1;
144 int rdidx2, wridx2, size2;
145};
146
Denis Vlasenko88308fe2007-06-25 10:35:11 +0000147/* Two buffers are directly after tsession in malloced memory.
148 * Make whole thing fit in 4k */
Denis Vlasenko59d7c432007-10-15 15:19:36 +0000149enum { BUFSIZE = (4 * 1024 - sizeof(struct tsession)) / 2 };
Denis Vlasenko88308fe2007-06-25 10:35:11 +0000150
Eric Andersen08a72202002-09-30 20:52:10 +0000151
Denis Vlasenko59d7c432007-10-15 15:19:36 +0000152/* Globals */
Denys Vlasenkoa4bcbd02009-06-09 23:01:24 +0200153struct globals {
154 struct tsession *sessions;
155 const char *loginpath;
156 const char *issuefile;
157 int maxfd;
Denys Vlasenko98a4c7c2010-02-04 15:00:15 +0100158} FIX_ALIASING;
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +0200159#define G (*(struct globals*)bb_common_bufsiz1)
Denys Vlasenkoa4bcbd02009-06-09 23:01:24 +0200160#define INIT_G() do { \
Denys Vlasenko47cfbf32016-04-21 18:18:48 +0200161 setup_common_bufsiz(); \
Denys Vlasenkoa4bcbd02009-06-09 23:01:24 +0200162 G.loginpath = "/bin/login"; \
163 G.issuefile = "/etc/issue.net"; \
164} while (0)
Eric Andersen08a72202002-09-30 20:52:10 +0000165
166
Denys Vlasenko122c47a2016-10-12 19:13:46 +0200167/* Write some buf1 data to pty, processing IACs.
Denys Vlasenko2a54b3e2016-10-12 14:54:10 +0200168 * Update wridx1 and size1. Return < 0 on error.
169 * Buggy if IAC is present but incomplete: skips them.
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000170 */
Denys Vlasenko2a54b3e2016-10-12 14:54:10 +0200171static ssize_t
172safe_write_to_pty_decode_iac(struct tsession *ts)
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000173{
Denys Vlasenko2a54b3e2016-10-12 14:54:10 +0200174 unsigned wr;
175 ssize_t rc;
176 unsigned char *buf;
177 unsigned char *found;
Eric Andersen08a72202002-09-30 20:52:10 +0000178
Denys Vlasenko2a54b3e2016-10-12 14:54:10 +0200179 buf = TS_BUF1(ts) + ts->wridx1;
180 wr = MIN(BUFSIZE - ts->wridx1, ts->size1);
Denys Vlasenko122c47a2016-10-12 19:13:46 +0200181 /* wr is at least 1 here */
182
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.
187 */
188 ts->buffered_IAC_for_pty = 0;
189 wr++;
190 ts->size1++;
191 buf--; /* Yes, this can point before the buffer. It's ok */
192 ts->wridx1--;
193 goto handle_iac;
194 }
195
Denys Vlasenko2a54b3e2016-10-12 14:54:10 +0200196 found = memchr(buf, IAC, wr);
197 if (found != buf) {
198 /* There is a "prefix" of non-IAC chars.
199 * Write only them, and return.
Denis Vlasenkob0150d22008-11-07 01:58:21 +0000200 */
Denys Vlasenko2a54b3e2016-10-12 14:54:10 +0200201 if (found)
202 wr = found - buf;
203
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.
Denis Vlasenkob0150d22008-11-07 01:58:21 +0000208 */
Denys Vlasenko2a54b3e2016-10-12 14:54:10 +0200209 rc = wr;
210 found = memchr(buf, '\r', wr);
211 if (found)
212 rc = found - buf + 1;
213 rc = safe_write(ts->ptyfd, buf, rc);
214 if (rc <= 0)
215 return rc;
Denys Vlasenko0190c412016-10-12 17:36:57 +0200216 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')
219 ) {
Denys Vlasenko2a54b3e2016-10-12 14:54:10 +0200220 rc++;
Denys Vlasenko0190c412016-10-12 17:36:57 +0200221 }
Denys Vlasenko2a54b3e2016-10-12 14:54:10 +0200222 goto update_and_return;
Eric Andersen08a72202002-09-30 20:52:10 +0000223 }
224
Denys Vlasenko2a54b3e2016-10-12 14:54:10 +0200225 /* buf starts with IAC char. Process that sequence.
226 * Example: we get this from our own (bbox) telnet client:
Denys Vlasenko0190c412016-10-12 17:36:57 +0200227 * 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
Denys Vlasenko26d88d62016-10-12 20:09:22 +0200229 * 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
Denys Vlasenko2a54b3e2016-10-12 14:54:10 +0200234 */
235 if (wr <= 1) {
Denys Vlasenko122c47a2016-10-12 19:13:46 +0200236 /* 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".
239 */
240 //bb_error_msg("dangling IAC!");
241 ts->buffered_IAC_for_pty = 1;
Denys Vlasenko2a54b3e2016-10-12 14:54:10 +0200242 rc = 1;
243 goto update_and_return;
244 }
245
Denys Vlasenko122c47a2016-10-12 19:13:46 +0200246 handle_iac:
Denys Vlasenko0190c412016-10-12 17:36:57 +0200247 /* 2-byte commands (240..250 and 255):
248 * IAC IAC (255) Literal 255. Supported.
Denys Vlasenkob6d421b2016-10-12 19:41:33 +0200249 * IAC SE (240) End of subnegotiation. Treated as NOP.
Denys Vlasenko0190c412016-10-12 17:36:57 +0200250 * IAC NOP (241) NOP. Supported.
251 * IAC BRK (243) Break. Like serial line break. TODO via tcsendbreak()?
Martin Lewis93594b12019-04-04 13:29:32 +0200252 * IAC AYT (246) Are you there.
Denys Vlasenko0190c412016-10-12 17:36:57 +0200253 * 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".
Denys Vlasenkob6d421b2016-10-12 19:41:33 +0200260 * Implemented only as part of NAWS:
261 * IAC SB (250) Subnegotiation of an option follows.
Denys Vlasenko0190c412016-10-12 17:36:57 +0200262 */
Denys Vlasenkob6d421b2016-10-12 19:41:33 +0200263 if (buf[1] == IAC) {
264 /* Literal 255 (emacs M-DEL) */
Denys Vlasenko122c47a2016-10-12 19:13:46 +0200265 //bb_error_msg("255!");
266 rc = safe_write(ts->ptyfd, &buf[1], 1);
Denys Vlasenko662634b2016-10-13 16:17:06 +0200267 /*
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.
272 */
273#if 0
Denys Vlasenko2a54b3e2016-10-12 14:54:10 +0200274 if (rc <= 0)
275 return rc;
Denys Vlasenko662634b2016-10-13 16:17:06 +0200276#endif
Denys Vlasenko2a54b3e2016-10-12 14:54:10 +0200277 rc = 2;
278 goto update_and_return;
279 }
Martin Lewis93594b12019-04-04 13:29:32 +0200280 if (buf[1] == AYT) {
281 /* Send back evidence that AYT was seen. */
282 buf[1] = NOP;
283 /*rc =*/ safe_write(ts->sockfd_write, buf, 2);
284 rc = 2;
285 goto update_and_return;
286 }
Denys Vlasenkob6d421b2016-10-12 19:41:33 +0200287 if (buf[1] >= 240 && buf[1] <= 249) {
288 /* NOP (241). Ignore (putty keepalive, etc) */
289 /* All other 2-byte commands also treated as NOPs here */
Denys Vlasenko2a54b3e2016-10-12 14:54:10 +0200290 rc = 2;
291 goto update_and_return;
292 }
293
294 if (wr <= 2) {
Denys Vlasenko122c47a2016-10-12 19:13:46 +0200295/* BUG: only 2 bytes of the IAC is in the buffer, we just eat them.
296 * This is not a practical problem since >2 byte IACs are seen only
297 * in initial negotiation, when buffer is empty
298 */
Denys Vlasenko2a54b3e2016-10-12 14:54:10 +0200299 rc = 2;
300 goto update_and_return;
301 }
302
Denys Vlasenkob6d421b2016-10-12 19:41:33 +0200303 if (buf[1] == SB) {
304 if (buf[2] == TELOPT_NAWS) {
305 /* IAC SB, TELOPT_NAWS, 4-byte, IAC SE */
306 struct winsize ws;
307 if (wr <= 6) {
Denys Vlasenko0190c412016-10-12 17:36:57 +0200308/* BUG: incomplete, can't process */
Denys Vlasenkob6d421b2016-10-12 19:41:33 +0200309 rc = wr;
310 goto update_and_return;
311 }
312 memset(&ws, 0, sizeof(ws)); /* pixel sizes are set to 0 */
313 ws.ws_col = (buf[3] << 8) | buf[4];
314 ws.ws_row = (buf[5] << 8) | buf[6];
315 ioctl(ts->ptyfd, TIOCSWINSZ, (char *)&ws);
316 rc = 7;
317 /* trailing IAC SE will be eaten separately, as 2-byte NOP */
Denys Vlasenko2a54b3e2016-10-12 14:54:10 +0200318 goto update_and_return;
319 }
Denys Vlasenkob6d421b2016-10-12 19:41:33 +0200320 /* else: other subnegs not supported yet */
Denys Vlasenko2a54b3e2016-10-12 14:54:10 +0200321 }
Denys Vlasenko0190c412016-10-12 17:36:57 +0200322
Denys Vlasenkob6d421b2016-10-12 19:41:33 +0200323 /* Assume it is a 3-byte WILL/WONT/DO/DONT 251..254 command and skip it */
Denys Vlasenko2a54b3e2016-10-12 14:54:10 +0200324#if DEBUG
325 fprintf(stderr, "Ignoring IAC %s,%s\n",
326 TELCMD(buf[1]), TELOPT(buf[2]));
327#endif
328 rc = 3;
329
330 update_and_return:
331 ts->wridx1 += rc;
332 if (ts->wridx1 >= BUFSIZE) /* actually == BUFSIZE */
333 ts->wridx1 = 0;
334 ts->size1 -= rc;
335 /*
Denys Vlasenko0190c412016-10-12 17:36:57 +0200336 * Hack. We cannot process IACs which wrap around buffer's end.
Denys Vlasenko2a54b3e2016-10-12 14:54:10 +0200337 * Since properly fixing it requires writing bigger code,
338 * we rely instead on this code making it virtually impossible
Denys Vlasenko0190c412016-10-12 17:36:57 +0200339 * to have wrapped IAC (people don't type at 2k/second).
Denys Vlasenko2a54b3e2016-10-12 14:54:10 +0200340 * It also allows for bigger reads in common case.
341 */
342 if (ts->size1 == 0) { /* very typical */
343 //bb_error_msg("zero size1");
344 ts->rdidx1 = 0;
345 ts->wridx1 = 0;
346 return rc;
347 }
348 wr = ts->wridx1;
349 if (wr != 0 && wr < ts->rdidx1) {
350 /* Buffer is not wrapped yet.
351 * We can easily move it to the beginning.
352 */
353 //bb_error_msg("moved %d", wr);
354 memmove(TS_BUF1(ts), TS_BUF1(ts) + wr, ts->size1);
355 ts->rdidx1 -= wr;
356 ts->wridx1 = 0;
357 }
358 return rc;
Eric Andersen08a72202002-09-30 20:52:10 +0000359}
360
Denis Vlasenko9f2f8082008-11-11 02:56:39 +0000361/*
Denis Vlasenko81c6a912008-11-12 21:14:50 +0000362 * Converting single IAC into double on output
Denis Vlasenko9f2f8082008-11-11 02:56:39 +0000363 */
Denys Vlasenko2a54b3e2016-10-12 14:54:10 +0200364static size_t safe_write_double_iac(int fd, const char *buf, size_t count)
Denis Vlasenko9f2f8082008-11-11 02:56:39 +0000365{
Denis Vlasenko81c6a912008-11-12 21:14:50 +0000366 const char *IACptr;
Denis Vlasenko9f2f8082008-11-11 02:56:39 +0000367 size_t wr, rc, total;
368
369 total = 0;
370 while (1) {
371 if (count == 0)
372 return total;
Denis Vlasenko81c6a912008-11-12 21:14:50 +0000373 if (*buf == (char)IAC) {
374 static const char IACIAC[] ALIGN1 = { IAC, IAC };
375 rc = safe_write(fd, IACIAC, 2);
Denys Vlasenko0190c412016-10-12 17:36:57 +0200376/* BUG: if partial write was only 1 byte long, we end up emitting just one IAC */
Denis Vlasenko9f2f8082008-11-11 02:56:39 +0000377 if (rc != 2)
378 break;
379 buf++;
380 total++;
381 count--;
382 continue;
383 }
Denis Vlasenko81c6a912008-11-12 21:14:50 +0000384 /* count != 0, *buf != IAC */
385 IACptr = memchr(buf, IAC, count);
Denis Vlasenko9f2f8082008-11-11 02:56:39 +0000386 wr = count;
Denis Vlasenko81c6a912008-11-12 21:14:50 +0000387 if (IACptr)
388 wr = IACptr - buf;
Denis Vlasenko9f2f8082008-11-11 02:56:39 +0000389 rc = safe_write(fd, buf, wr);
390 if (rc != wr)
391 break;
392 buf += rc;
393 total += rc;
394 count -= rc;
395 }
396 /* here: rc - result of last short write */
397 if ((ssize_t)rc < 0) { /* error? */
398 if (total == 0)
399 return rc;
400 rc = 0;
401 }
402 return total + rc;
403}
Eric Andersen08a72202002-09-30 20:52:10 +0000404
Denys Vlasenkoa4bcbd02009-06-09 23:01:24 +0200405/* Must match getopt32 string */
406enum {
407 OPT_WATCHCHILD = (1 << 2), /* -K */
408 OPT_INETD = (1 << 3) * ENABLE_FEATURE_TELNETD_STANDALONE, /* -i */
Denys Vlasenkoed1667e2009-09-04 02:21:13 +0200409 OPT_PORT = (1 << 4) * ENABLE_FEATURE_TELNETD_STANDALONE, /* -p PORT */
Denys Vlasenkoa4bcbd02009-06-09 23:01:24 +0200410 OPT_FOREGROUND = (1 << 6) * ENABLE_FEATURE_TELNETD_STANDALONE, /* -F */
Denys Vlasenkoed1667e2009-09-04 02:21:13 +0200411 OPT_SYSLOG = (1 << 7) * ENABLE_FEATURE_TELNETD_INETD_WAIT, /* -S */
412 OPT_WAIT = (1 << 8) * ENABLE_FEATURE_TELNETD_INETD_WAIT, /* -w SEC */
Denys Vlasenkoa4bcbd02009-06-09 23:01:24 +0200413};
414
Eric Andersen08a72202002-09-30 20:52:10 +0000415static struct tsession *
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000416make_new_session(
Denys Vlasenko1d77db82009-06-10 13:38:08 +0200417 IF_FEATURE_TELNETD_STANDALONE(int sock)
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000418 IF_NOT_FEATURE_TELNETD_STANDALONE(void)
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000419) {
Denys Vlasenkof6916db2010-04-06 17:43:29 +0200420#if !ENABLE_FEATURE_TELNETD_STANDALONE
Denys Vlasenkoff0e8752010-05-10 04:16:43 +0200421 enum { sock = 0 };
Denys Vlasenkof6916db2010-04-06 17:43:29 +0200422#endif
Denis Vlasenko88308fe2007-06-25 10:35:11 +0000423 const char *login_argv[2];
Eric Andersen08a72202002-09-30 20:52:10 +0000424 struct termios termbuf;
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000425 int fd, pid;
Denis Vlasenko85c24712008-03-17 09:04:04 +0000426 char tty_name[GETPTY_BUFSIZE];
Rob Landley1ec5b292006-05-29 07:42:02 +0000427 struct tsession *ts = xzalloc(sizeof(struct tsession) + BUFSIZE * 2);
Eric Andersen08a72202002-09-30 20:52:10 +0000428
Denis Vlasenko59d7c432007-10-15 15:19:36 +0000429 /*ts->buf1 = (char *)(ts + 1);*/
430 /*ts->buf2 = ts->buf1 + BUFSIZE;*/
Eric Andersen08a72202002-09-30 20:52:10 +0000431
Denys Vlasenko1d77db82009-06-10 13:38:08 +0200432 /* Got a new connection, set up a tty */
Bernhard Reutner-Fischerae4342c2008-05-19 08:18:50 +0000433 fd = xgetpty(tty_name);
Denys Vlasenkoa4bcbd02009-06-09 23:01:24 +0200434 if (fd > G.maxfd)
435 G.maxfd = fd;
Denis Vlasenko59d7c432007-10-15 15:19:36 +0000436 ts->ptyfd = fd;
437 ndelay_on(fd);
Denys Vlasenko1d77db82009-06-10 13:38:08 +0200438 close_on_exec_on(fd);
439
Denis Vlasenko6d044352008-11-09 00:44:40 +0000440 /* SO_KEEPALIVE by popular demand */
Denys Vlasenkoc52cbea2015-08-24 19:48:03 +0200441 setsockopt_keepalive(sock);
Denys Vlasenkof6916db2010-04-06 17:43:29 +0200442#if ENABLE_FEATURE_TELNETD_STANDALONE
Denys Vlasenko1d77db82009-06-10 13:38:08 +0200443 ts->sockfd_read = sock;
Denis Vlasenkof472b232007-10-16 21:35:17 +0000444 ndelay_on(sock);
Denys Vlasenko1d77db82009-06-10 13:38:08 +0200445 if (sock == 0) { /* We are called with fd 0 - we are in inetd mode */
Denis Vlasenko9e237672007-10-17 11:18:49 +0000446 sock++; /* so use fd 1 for output */
447 ndelay_on(sock);
448 }
Denis Vlasenkof472b232007-10-16 21:35:17 +0000449 ts->sockfd_write = sock;
Denys Vlasenkoa4bcbd02009-06-09 23:01:24 +0200450 if (sock > G.maxfd)
451 G.maxfd = sock;
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000452#else
Denis Vlasenko59d7c432007-10-15 15:19:36 +0000453 /* ts->sockfd_read = 0; - done by xzalloc */
Denis Vlasenkof472b232007-10-16 21:35:17 +0000454 ts->sockfd_write = 1;
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000455 ndelay_on(0);
456 ndelay_on(1);
457#endif
Denys Vlasenko1d77db82009-06-10 13:38:08 +0200458
Eric Andersen08a72202002-09-30 20:52:10 +0000459 /* Make the telnet client understand we will echo characters so it
460 * should not do it locally. We don't tell the client to run linemode,
461 * because we want to handle line editing and tab completion and other
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000462 * stuff that requires char-by-char support. */
Denis Vlasenko59d7c432007-10-15 15:19:36 +0000463 {
464 static const char iacs_to_send[] ALIGN1 = {
465 IAC, DO, TELOPT_ECHO,
466 IAC, DO, TELOPT_NAWS,
Denys Vlasenkofb132e42010-10-29 11:46:52 +0200467 /* This requires telnetd.ctrlSQ.patch (incomplete) */
468 /*IAC, DO, TELOPT_LFLOW,*/
Denis Vlasenko59d7c432007-10-15 15:19:36 +0000469 IAC, WILL, TELOPT_ECHO,
470 IAC, WILL, TELOPT_SGA
471 };
Denys Vlasenko2a54b3e2016-10-12 14:54:10 +0200472 /* This confuses safe_write_double_iac(), it will try to duplicate
Denis Vlasenko9f2f8082008-11-11 02:56:39 +0000473 * each IAC... */
Denys Vlasenkoa4bcbd02009-06-09 23:01:24 +0200474 //memcpy(TS_BUF2(ts), iacs_to_send, sizeof(iacs_to_send));
Denis Vlasenko9f2f8082008-11-11 02:56:39 +0000475 //ts->rdidx2 = sizeof(iacs_to_send);
476 //ts->size2 = sizeof(iacs_to_send);
Denis Vlasenko81c6a912008-11-12 21:14:50 +0000477 /* So just stuff it into TCP stream! (no error check...) */
478#if ENABLE_FEATURE_TELNETD_STANDALONE
Denis Vlasenko9f2f8082008-11-11 02:56:39 +0000479 safe_write(sock, iacs_to_send, sizeof(iacs_to_send));
Denis Vlasenko81c6a912008-11-12 21:14:50 +0000480#else
481 safe_write(1, iacs_to_send, sizeof(iacs_to_send));
482#endif
483 /*ts->rdidx2 = 0; - xzalloc did it */
Denis Vlasenko9f2f8082008-11-11 02:56:39 +0000484 /*ts->size2 = 0;*/
Denis Vlasenko59d7c432007-10-15 15:19:36 +0000485 }
Eric Andersen08a72202002-09-30 20:52:10 +0000486
Denys Vlasenko8131eea2009-11-02 14:19:51 +0100487 fflush_all();
Denis Vlasenko59d7c432007-10-15 15:19:36 +0000488 pid = vfork(); /* NOMMU-friendly */
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000489 if (pid < 0) {
490 free(ts);
491 close(fd);
Denis Vlasenko23c81282007-10-16 22:01:23 +0000492 /* sock will be closed by caller */
Denis Vlasenko59d7c432007-10-15 15:19:36 +0000493 bb_perror_msg("vfork");
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000494 return NULL;
Eric Andersen08a72202002-09-30 20:52:10 +0000495 }
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000496 if (pid > 0) {
Denis Vlasenko59d7c432007-10-15 15:19:36 +0000497 /* Parent */
Denis Vlasenko2450c452007-10-15 22:09:15 +0000498 ts->shell_pid = pid;
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000499 return ts;
Eric Andersen08a72202002-09-30 20:52:10 +0000500 }
Denis Vlasenkof7996f32007-01-11 17:20:00 +0000501
Denis Vlasenko59d7c432007-10-15 15:19:36 +0000502 /* Child */
503 /* Careful - we are after vfork! */
Eric Andersen08a72202002-09-30 20:52:10 +0000504
Denis Vlasenkod814c982009-02-02 23:43:57 +0000505 /* Restore default signal handling ASAP */
Denis Vlasenko25591c32008-02-16 22:58:56 +0000506 bb_signals((1 << SIGCHLD) + (1 << SIGPIPE), SIG_DFL);
Denis Vlasenko018b1552007-11-06 01:38:46 +0000507
Denys Vlasenko58c3d212010-11-30 09:17:30 +0100508 pid = getpid();
509
Denys Vlasenkof6916db2010-04-06 17:43:29 +0200510 if (ENABLE_FEATURE_UTMP) {
511 len_and_sockaddr *lsa = get_peer_lsa(sock);
512 char *hostname = NULL;
513 if (lsa) {
514 hostname = xmalloc_sockaddr2dotted(&lsa->u.sa);
515 free(lsa);
516 }
517 write_new_utmp(pid, LOGIN_PROCESS, tty_name, /*username:*/ "LOGIN", hostname);
518 free(hostname);
519 }
520
Denis Vlasenkod814c982009-02-02 23:43:57 +0000521 /* Make new session and process group */
522 setsid();
523
Denys Vlasenko1d77db82009-06-10 13:38:08 +0200524 /* Open the child's side of the tty */
Denis Vlasenko9af7c9d2007-01-19 21:19:35 +0000525 /* NB: setsid() disconnects from any previous ctty's. Therefore
526 * we must open child's side of the tty AFTER setsid! */
Denys Vlasenko1d77db82009-06-10 13:38:08 +0200527 close(0);
Denis Vlasenko1101d1c2008-07-21 09:22:28 +0000528 xopen(tty_name, O_RDWR); /* becomes our ctty */
529 xdup2(0, 1);
530 xdup2(0, 2);
Denys Vlasenko3a416112010-04-05 22:10:38 +0200531 tcsetpgrp(0, pid); /* switch this tty's process group to us */
532
Denys Vlasenko1d77db82009-06-10 13:38:08 +0200533 /* The pseudo-terminal allocated to the client is configured to operate
534 * in cooked mode, and with XTABS CRMOD enabled (see tty(4)) */
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000535 tcgetattr(0, &termbuf);
536 termbuf.c_lflag |= ECHO; /* if we use readline we dont want this */
Denis Vlasenko59d7c432007-10-15 15:19:36 +0000537 termbuf.c_oflag |= ONLCR | XTABS;
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000538 termbuf.c_iflag |= ICRNL;
539 termbuf.c_iflag &= ~IXOFF;
540 /*termbuf.c_lflag &= ~ICANON;*/
Denis Vlasenko202ac502008-11-05 13:20:58 +0000541 tcsetattr_stdin_TCSANOW(&termbuf);
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000542
Denys Vlasenko8131eea2009-11-02 14:19:51 +0100543 /* Uses FILE-based I/O to stdout, but does fflush_all(),
Denis Vlasenko59d7c432007-10-15 15:19:36 +0000544 * so should be safe with vfork.
545 * I fear, though, that some users will have ridiculously big
Denis Vlasenko018b1552007-11-06 01:38:46 +0000546 * issue files, and they may block writing to fd 1,
547 * (parent is supposed to read it, but parent waits
548 * for vforked child to exec!) */
Denys Vlasenkoa4bcbd02009-06-09 23:01:24 +0200549 print_login_issue(G.issuefile, tty_name);
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000550
Denis Vlasenko59d7c432007-10-15 15:19:36 +0000551 /* Exec shell / login / whatever */
Denys Vlasenkoa4bcbd02009-06-09 23:01:24 +0200552 login_argv[0] = G.loginpath;
Denis Vlasenko88308fe2007-06-25 10:35:11 +0000553 login_argv[1] = NULL;
Denis Vlasenko018b1552007-11-06 01:38:46 +0000554 /* exec busybox applet (if PREFER_APPLETS=y), if that fails,
Denys Vlasenko1d77db82009-06-10 13:38:08 +0200555 * exec external program.
556 * NB: sock is either 0 or has CLOEXEC set on it.
557 * fd has CLOEXEC set on it too. These two fds will be closed here.
558 */
Denys Vlasenkoa4bcbd02009-06-09 23:01:24 +0200559 BB_EXECVP(G.loginpath, (char **)login_argv);
Denis Vlasenko018b1552007-11-06 01:38:46 +0000560 /* _exit is safer with vfork, and we shouldn't send message
Denis Vlasenko2450c452007-10-15 22:09:15 +0000561 * to remote clients anyway */
Denys Vlasenkoa4bcbd02009-06-09 23:01:24 +0200562 _exit(EXIT_FAILURE); /*bb_perror_msg_and_die("execv %s", G.loginpath);*/
Eric Andersen08a72202002-09-30 20:52:10 +0000563}
564
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000565#if ENABLE_FEATURE_TELNETD_STANDALONE
566
Eric Andersen08a72202002-09-30 20:52:10 +0000567static void
568free_session(struct tsession *ts)
569{
Denys Vlasenko3a416112010-04-05 22:10:38 +0200570 struct tsession *t;
Eric Andersen08a72202002-09-30 20:52:10 +0000571
Denis Vlasenkoe87b8682007-10-17 14:33:31 +0000572 if (option_mask32 & OPT_INETD)
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000573 exit(EXIT_SUCCESS);
Denis Vlasenkoe87b8682007-10-17 14:33:31 +0000574
Denis Vlasenko59d7c432007-10-15 15:19:36 +0000575 /* Unlink this telnet session from the session list */
Denys Vlasenko3a416112010-04-05 22:10:38 +0200576 t = G.sessions;
Mike Frysinger731f81c2006-05-10 15:23:12 +0000577 if (t == ts)
Denys Vlasenkoa4bcbd02009-06-09 23:01:24 +0200578 G.sessions = ts->next;
Eric Andersen08a72202002-09-30 20:52:10 +0000579 else {
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000580 while (t->next != ts)
Eric Andersen08a72202002-09-30 20:52:10 +0000581 t = t->next;
582 t->next = ts->next;
583 }
584
Denis Vlasenkof472b232007-10-16 21:35:17 +0000585#if 0
Denis Vlasenko59d7c432007-10-15 15:19:36 +0000586 /* It was said that "normal" telnetd just closes ptyfd,
587 * doesn't send SIGKILL. When we close ptyfd,
588 * kernel sends SIGHUP to processes having slave side opened. */
Denis Vlasenkof472b232007-10-16 21:35:17 +0000589 kill(ts->shell_pid, SIGKILL);
Denis Vlasenkod814c982009-02-02 23:43:57 +0000590 waitpid(ts->shell_pid, NULL, 0);
Denis Vlasenkof472b232007-10-16 21:35:17 +0000591#endif
Eric Andersen08a72202002-09-30 20:52:10 +0000592 close(ts->ptyfd);
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000593 close(ts->sockfd_read);
Denis Vlasenkof472b232007-10-16 21:35:17 +0000594 /* We do not need to close(ts->sockfd_write), it's the same
595 * as sockfd_read unless we are in inetd mode. But in inetd mode
Denis Vlasenkoe87b8682007-10-17 14:33:31 +0000596 * we do not reach this */
Eric Andersen08a72202002-09-30 20:52:10 +0000597 free(ts);
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000598
Denis Vlasenko59d7c432007-10-15 15:19:36 +0000599 /* Scan all sessions and find new maxfd */
Denys Vlasenkoa4bcbd02009-06-09 23:01:24 +0200600 G.maxfd = 0;
601 ts = G.sessions;
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000602 while (ts) {
Denys Vlasenkoa4bcbd02009-06-09 23:01:24 +0200603 if (G.maxfd < ts->ptyfd)
604 G.maxfd = ts->ptyfd;
605 if (G.maxfd < ts->sockfd_read)
606 G.maxfd = ts->sockfd_read;
Denis Vlasenkof472b232007-10-16 21:35:17 +0000607#if 0
608 /* Again, sockfd_write == sockfd_read here */
Denys Vlasenkoa4bcbd02009-06-09 23:01:24 +0200609 if (G.maxfd < ts->sockfd_write)
610 G.maxfd = ts->sockfd_write;
Denis Vlasenkof472b232007-10-16 21:35:17 +0000611#endif
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000612 ts = ts->next;
613 }
Eric Andersen08a72202002-09-30 20:52:10 +0000614}
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000615
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000616#else /* !FEATURE_TELNETD_STANDALONE */
617
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000618/* Used in main() only, thus "return 0" actually is exit(EXIT_SUCCESS). */
Denis Vlasenkoe87b8682007-10-17 14:33:31 +0000619#define free_session(ts) return 0
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000620
621#endif
622
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000623static void handle_sigchld(int sig UNUSED_PARAM)
Denis Vlasenko2450c452007-10-15 22:09:15 +0000624{
625 pid_t pid;
626 struct tsession *ts;
Denys Vlasenko3a416112010-04-05 22:10:38 +0200627 int save_errno = errno;
Denis Vlasenko2450c452007-10-15 22:09:15 +0000628
Denis Vlasenko018b1552007-11-06 01:38:46 +0000629 /* Looping: more than one child may have exited */
630 while (1) {
Denis Vlasenkofb0eba72008-01-02 19:55:04 +0000631 pid = wait_any_nohang(NULL);
Denis Vlasenko018b1552007-11-06 01:38:46 +0000632 if (pid <= 0)
633 break;
Denys Vlasenkoa4bcbd02009-06-09 23:01:24 +0200634 ts = G.sessions;
Denis Vlasenko2450c452007-10-15 22:09:15 +0000635 while (ts) {
636 if (ts->shell_pid == pid) {
637 ts->shell_pid = -1;
Denys Vlasenkoda921262015-01-05 15:37:58 +0100638 update_utmp_DEAD_PROCESS(pid);
Denis Vlasenko018b1552007-11-06 01:38:46 +0000639 break;
Denis Vlasenko2450c452007-10-15 22:09:15 +0000640 }
641 ts = ts->next;
642 }
643 }
Denys Vlasenko3a416112010-04-05 22:10:38 +0200644
645 errno = save_errno;
Denis Vlasenko2450c452007-10-15 22:09:15 +0000646}
647
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000648int telnetd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000649int telnetd_main(int argc UNUSED_PARAM, char **argv)
Eric Andersen08a72202002-09-30 20:52:10 +0000650{
Eric Andersen08a72202002-09-30 20:52:10 +0000651 fd_set rdfdset, wrfdset;
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000652 unsigned opt;
Denis Vlasenko10916c52007-10-15 17:28:00 +0000653 int count;
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000654 struct tsession *ts;
655#if ENABLE_FEATURE_TELNETD_STANDALONE
656#define IS_INETD (opt & OPT_INETD)
Denys Vlasenkoed1667e2009-09-04 02:21:13 +0200657 int master_fd = master_fd; /* for compiler */
658 int sec_linger = sec_linger;
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000659 char *opt_bindaddr = NULL;
660 char *opt_portnbr;
661#else
662 enum {
663 IS_INETD = 1,
664 master_fd = -1,
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000665 };
Glenn L McGrathc2b91862003-09-12 11:27:15 +0000666#endif
Denys Vlasenkoa4bcbd02009-06-09 23:01:24 +0200667 INIT_G();
668
Denis Vlasenko2450c452007-10-15 22:09:15 +0000669 /* Even if !STANDALONE, we accept (and ignore) -i, thus people
Denis Vlasenko59d7c432007-10-15 15:19:36 +0000670 * don't need to guess whether it's ok to pass -i to us */
Denys Vlasenko22542ec2017-08-08 21:55:02 +0200671 opt = getopt32(argv, "^"
672 "f:l:Ki"
Denys Vlasenkoed1667e2009-09-04 02:21:13 +0200673 IF_FEATURE_TELNETD_STANDALONE("p:b:F")
Denys Vlasenko22542ec2017-08-08 21:55:02 +0200674 IF_FEATURE_TELNETD_INETD_WAIT("Sw:+") /* -w NUM */
675 "\0"
676 /* -w implies -F. -w and -i don't mix */
677 IF_FEATURE_TELNETD_INETD_WAIT("wF:i--w:w--i"),
Denys Vlasenkoa4bcbd02009-06-09 23:01:24 +0200678 &G.issuefile, &G.loginpath
Denys Vlasenkoed1667e2009-09-04 02:21:13 +0200679 IF_FEATURE_TELNETD_STANDALONE(, &opt_portnbr, &opt_bindaddr)
680 IF_FEATURE_TELNETD_INETD_WAIT(, &sec_linger)
681 );
Denis Vlasenko2450c452007-10-15 22:09:15 +0000682 if (!IS_INETD /*&& !re_execed*/) {
683 /* inform that we start in standalone mode?
684 * May be useful when people forget to give -i */
685 /*bb_error_msg("listening for connections");*/
686 if (!(opt & OPT_FOREGROUND)) {
687 /* DAEMON_CHDIR_ROOT was giving inconsistent
Denis Vlasenko008eda22007-10-16 10:47:27 +0000688 * behavior with/without -F, -i */
Denis Vlasenko018b1552007-11-06 01:38:46 +0000689 bb_daemonize_or_rexec(0 /*was DAEMON_CHDIR_ROOT*/, argv);
Denis Vlasenko2450c452007-10-15 22:09:15 +0000690 }
691 }
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000692 /* Redirect log to syslog early, if needed */
Denys Vlasenkoed1667e2009-09-04 02:21:13 +0200693 if (IS_INETD || (opt & OPT_SYSLOG) || !(opt & OPT_FOREGROUND)) {
Denis Vlasenko5e4fda02009-03-08 23:46:48 +0000694 openlog(applet_name, LOG_PID, LOG_DAEMON);
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000695 logmode = LOGMODE_SYSLOG;
696 }
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000697#if ENABLE_FEATURE_TELNETD_STANDALONE
698 if (IS_INETD) {
Denys Vlasenko1d77db82009-06-10 13:38:08 +0200699 G.sessions = make_new_session(0);
Denys Vlasenkoa4bcbd02009-06-09 23:01:24 +0200700 if (!G.sessions) /* pty opening or vfork problem, exit */
Denys Vlasenkoed1667e2009-09-04 02:21:13 +0200701 return 1; /* make_new_session printed error message */
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000702 } else {
Denys Vlasenkoed1667e2009-09-04 02:21:13 +0200703 master_fd = 0;
704 if (!(opt & OPT_WAIT)) {
705 unsigned portnbr = 23;
706 if (opt & OPT_PORT)
707 portnbr = xatou16(opt_portnbr);
708 master_fd = create_and_bind_stream_or_die(opt_bindaddr, portnbr);
709 xlisten(master_fd, 1);
710 }
Denys Vlasenko1d77db82009-06-10 13:38:08 +0200711 close_on_exec_on(master_fd);
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000712 }
Rob Landley00e76cb2005-05-10 23:53:33 +0000713#else
Denys Vlasenkoa4bcbd02009-06-09 23:01:24 +0200714 G.sessions = make_new_session();
715 if (!G.sessions) /* pty opening or vfork problem, exit */
Denys Vlasenkoed1667e2009-09-04 02:21:13 +0200716 return 1; /* make_new_session printed error message */
Rob Landley00e76cb2005-05-10 23:53:33 +0000717#endif
Eric Andersen08a72202002-09-30 20:52:10 +0000718
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000719 /* We don't want to die if just one session is broken */
720 signal(SIGPIPE, SIG_IGN);
Eric Andersen08a72202002-09-30 20:52:10 +0000721
Denis Vlasenko2450c452007-10-15 22:09:15 +0000722 if (opt & OPT_WATCHCHILD)
723 signal(SIGCHLD, handle_sigchld);
Denis Vlasenko018b1552007-11-06 01:38:46 +0000724 else /* prevent dead children from becoming zombies */
725 signal(SIGCHLD, SIG_IGN);
Denis Vlasenko2450c452007-10-15 22:09:15 +0000726
Denis Vlasenko59d7c432007-10-15 15:19:36 +0000727/*
Denys Vlasenko1d77db82009-06-10 13:38:08 +0200728 This is how the buffers are used. The arrows indicate data flow.
729
Denis Vlasenko59d7c432007-10-15 15:19:36 +0000730 +-------+ wridx1++ +------+ rdidx1++ +----------+
731 | | <-------------- | buf1 | <-------------- | |
732 | | size1-- +------+ size1++ | |
733 | pty | | socket |
734 | | rdidx2++ +------+ wridx2++ | |
735 | | --------------> | buf2 | --------------> | |
736 +-------+ size2++ +------+ size2-- +----------+
737
738 size1: "how many bytes are buffered for pty between rdidx1 and wridx1?"
739 size2: "how many bytes are buffered for socket between rdidx2 and wridx2?"
740
741 Each session has got two buffers. Buffers are circular. If sizeN == 0,
742 buffer is empty. If sizeN == BUFSIZE, buffer is full. In both these cases
743 rdidxN == wridxN.
744*/
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000745 again:
746 FD_ZERO(&rdfdset);
747 FD_ZERO(&wrfdset);
Eric Andersen08a72202002-09-30 20:52:10 +0000748
Denis Vlasenko59d7c432007-10-15 15:19:36 +0000749 /* Select on the master socket, all telnet sockets and their
750 * ptys if there is room in their session buffers.
751 * NB: scalability problem: we recalculate entire bitmap
752 * before each select. Can be a problem with 500+ connections. */
Denys Vlasenkoa4bcbd02009-06-09 23:01:24 +0200753 ts = G.sessions;
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000754 while (ts) {
Denys Vlasenko1d77db82009-06-10 13:38:08 +0200755 struct tsession *next = ts->next; /* in case we free ts */
Denis Vlasenko2450c452007-10-15 22:09:15 +0000756 if (ts->shell_pid == -1) {
Denis Vlasenko018b1552007-11-06 01:38:46 +0000757 /* Child died and we detected that */
Denis Vlasenko2450c452007-10-15 22:09:15 +0000758 free_session(ts);
759 } else {
760 if (ts->size1 > 0) /* can write to pty */
761 FD_SET(ts->ptyfd, &wrfdset);
762 if (ts->size1 < BUFSIZE) /* can read from socket */
763 FD_SET(ts->sockfd_read, &rdfdset);
764 if (ts->size2 > 0) /* can write to socket */
765 FD_SET(ts->sockfd_write, &wrfdset);
766 if (ts->size2 < BUFSIZE) /* can read from pty */
767 FD_SET(ts->ptyfd, &rdfdset);
768 }
769 ts = next;
770 }
771 if (!IS_INETD) {
772 FD_SET(master_fd, &rdfdset);
773 /* This is needed because free_session() does not
Denis Vlasenko018b1552007-11-06 01:38:46 +0000774 * take master_fd into account when it finds new
Denis Vlasenko2450c452007-10-15 22:09:15 +0000775 * maxfd among remaining fd's */
Denys Vlasenkoa4bcbd02009-06-09 23:01:24 +0200776 if (master_fd > G.maxfd)
777 G.maxfd = master_fd;
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000778 }
779
Denys Vlasenkoed1667e2009-09-04 02:21:13 +0200780 {
Denys Vlasenkoed1667e2009-09-04 02:21:13 +0200781 struct timeval *tv_ptr = NULL;
Denys Vlasenko36df0482009-10-19 16:07:28 +0200782#if ENABLE_FEATURE_TELNETD_INETD_WAIT
783 struct timeval tv;
Denys Vlasenkoed1667e2009-09-04 02:21:13 +0200784 if ((opt & OPT_WAIT) && !G.sessions) {
785 tv.tv_sec = sec_linger;
786 tv.tv_usec = 0;
787 tv_ptr = &tv;
788 }
Denys Vlasenko36df0482009-10-19 16:07:28 +0200789#endif
Denys Vlasenkoed1667e2009-09-04 02:21:13 +0200790 count = select(G.maxfd + 1, &rdfdset, &wrfdset, NULL, tv_ptr);
791 }
792 if (count == 0) /* "telnetd -w SEC" timed out */
793 return 0;
Denis Vlasenko10916c52007-10-15 17:28:00 +0000794 if (count < 0)
Denis Vlasenko59d7c432007-10-15 15:19:36 +0000795 goto again; /* EINTR or ENOMEM */
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000796
797#if ENABLE_FEATURE_TELNETD_STANDALONE
Denys Vlasenko1d77db82009-06-10 13:38:08 +0200798 /* Check for and accept new sessions */
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000799 if (!IS_INETD && FD_ISSET(master_fd, &rdfdset)) {
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000800 int fd;
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000801 struct tsession *new_ts;
802
Denis Vlasenko59d7c432007-10-15 15:19:36 +0000803 fd = accept(master_fd, NULL, NULL);
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000804 if (fd < 0)
805 goto again;
Denys Vlasenko1d77db82009-06-10 13:38:08 +0200806 close_on_exec_on(fd);
807
808 /* Create a new session and link it into active list */
809 new_ts = make_new_session(fd);
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000810 if (new_ts) {
Denys Vlasenkoa4bcbd02009-06-09 23:01:24 +0200811 new_ts->next = G.sessions;
812 G.sessions = new_ts;
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000813 } else {
814 close(fd);
Eric Andersen08a72202002-09-30 20:52:10 +0000815 }
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000816 }
817#endif
Eric Andersen08a72202002-09-30 20:52:10 +0000818
Denys Vlasenko1d77db82009-06-10 13:38:08 +0200819 /* Then check for data tunneling */
Denys Vlasenkoa4bcbd02009-06-09 23:01:24 +0200820 ts = G.sessions;
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000821 while (ts) { /* For all sessions... */
Denys Vlasenko1d77db82009-06-10 13:38:08 +0200822 struct tsession *next = ts->next; /* in case we free ts */
Eric Andersen08a72202002-09-30 20:52:10 +0000823
Denis Vlasenko59d7c432007-10-15 15:19:36 +0000824 if (/*ts->size1 &&*/ FD_ISSET(ts->ptyfd, &wrfdset)) {
Denys Vlasenko1d77db82009-06-10 13:38:08 +0200825 /* Write to pty from buffer 1 */
Denys Vlasenko2a54b3e2016-10-12 14:54:10 +0200826 count = safe_write_to_pty_decode_iac(ts);
Denis Vlasenko10916c52007-10-15 17:28:00 +0000827 if (count < 0) {
Denis Vlasenko59d7c432007-10-15 15:19:36 +0000828 if (errno == EAGAIN)
829 goto skip1;
Denis Vlasenkof472b232007-10-16 21:35:17 +0000830 goto kill_session;
Eric Andersen08a72202002-09-30 20:52:10 +0000831 }
Eric Andersen08a72202002-09-30 20:52:10 +0000832 }
Denis Vlasenko59d7c432007-10-15 15:19:36 +0000833 skip1:
834 if (/*ts->size2 &&*/ FD_ISSET(ts->sockfd_write, &wrfdset)) {
Denys Vlasenko1d77db82009-06-10 13:38:08 +0200835 /* Write to socket from buffer 2 */
Denis Vlasenko10916c52007-10-15 17:28:00 +0000836 count = MIN(BUFSIZE - ts->wridx2, ts->size2);
Denys Vlasenko2a54b3e2016-10-12 14:54:10 +0200837 count = safe_write_double_iac(ts->sockfd_write, (void*)(TS_BUF2(ts) + ts->wridx2), count);
Denis Vlasenko10916c52007-10-15 17:28:00 +0000838 if (count < 0) {
Denis Vlasenko59d7c432007-10-15 15:19:36 +0000839 if (errno == EAGAIN)
840 goto skip2;
Denis Vlasenkof472b232007-10-16 21:35:17 +0000841 goto kill_session;
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000842 }
Denis Vlasenko10916c52007-10-15 17:28:00 +0000843 ts->wridx2 += count;
Denis Vlasenko59d7c432007-10-15 15:19:36 +0000844 if (ts->wridx2 >= BUFSIZE) /* actually == BUFSIZE */
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000845 ts->wridx2 = 0;
Denys Vlasenko2a54b3e2016-10-12 14:54:10 +0200846 ts->size2 -= count;
847 if (ts->size2 == 0) {
848 ts->rdidx2 = 0;
849 ts->wridx2 = 0;
850 }
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000851 }
Denis Vlasenko59d7c432007-10-15 15:19:36 +0000852 skip2:
Denis Vlasenko10916c52007-10-15 17:28:00 +0000853
Denis Vlasenko59d7c432007-10-15 15:19:36 +0000854 if (/*ts->size1 < BUFSIZE &&*/ FD_ISSET(ts->sockfd_read, &rdfdset)) {
Denys Vlasenko1d77db82009-06-10 13:38:08 +0200855 /* Read from socket to buffer 1 */
Denis Vlasenko10916c52007-10-15 17:28:00 +0000856 count = MIN(BUFSIZE - ts->rdidx1, BUFSIZE - ts->size1);
Denys Vlasenkoa4bcbd02009-06-09 23:01:24 +0200857 count = safe_read(ts->sockfd_read, TS_BUF1(ts) + ts->rdidx1, count);
Denis Vlasenko10916c52007-10-15 17:28:00 +0000858 if (count <= 0) {
859 if (count < 0 && errno == EAGAIN)
Denis Vlasenko59d7c432007-10-15 15:19:36 +0000860 goto skip3;
Denis Vlasenkof472b232007-10-16 21:35:17 +0000861 goto kill_session;
Denis Vlasenko59d7c432007-10-15 15:19:36 +0000862 }
863 /* Ignore trailing NUL if it is there */
Denys Vlasenkoa4bcbd02009-06-09 23:01:24 +0200864 if (!TS_BUF1(ts)[ts->rdidx1 + count - 1]) {
Denis Vlasenkof472b232007-10-16 21:35:17 +0000865 --count;
Denis Vlasenko59d7c432007-10-15 15:19:36 +0000866 }
Denis Vlasenko10916c52007-10-15 17:28:00 +0000867 ts->size1 += count;
868 ts->rdidx1 += count;
Denis Vlasenko59d7c432007-10-15 15:19:36 +0000869 if (ts->rdidx1 >= BUFSIZE) /* actually == BUFSIZE */
870 ts->rdidx1 = 0;
871 }
872 skip3:
873 if (/*ts->size2 < BUFSIZE &&*/ FD_ISSET(ts->ptyfd, &rdfdset)) {
Denys Vlasenko1d77db82009-06-10 13:38:08 +0200874 /* Read from pty to buffer 2 */
Denys Vlasenko39b18192019-01-06 19:06:01 +0100875 int eio = 0;
876 read_pty:
Denis Vlasenko10916c52007-10-15 17:28:00 +0000877 count = MIN(BUFSIZE - ts->rdidx2, BUFSIZE - ts->size2);
Denys Vlasenkoa4bcbd02009-06-09 23:01:24 +0200878 count = safe_read(ts->ptyfd, TS_BUF2(ts) + ts->rdidx2, count);
Denis Vlasenko10916c52007-10-15 17:28:00 +0000879 if (count <= 0) {
Denys Vlasenko39b18192019-01-06 19:06:01 +0100880 if (count < 0) {
881 if (errno == EAGAIN)
882 goto skip4;
883 /* login process might call vhangup(),
884 * which causes intermittent EIOs on read above
885 * (observed on kernel 4.12.0). Try up to 10 ms.
886 */
887 if (errno == EIO && eio < 10) {
888 eio++;
889 //bb_error_msg("EIO pty %u", eio);
890 usleep(1000);
891 goto read_pty;
892 }
893 }
Denis Vlasenkof472b232007-10-16 21:35:17 +0000894 goto kill_session;
Denis Vlasenko59d7c432007-10-15 15:19:36 +0000895 }
Denis Vlasenko10916c52007-10-15 17:28:00 +0000896 ts->size2 += count;
897 ts->rdidx2 += count;
Denis Vlasenko59d7c432007-10-15 15:19:36 +0000898 if (ts->rdidx2 >= BUFSIZE) /* actually == BUFSIZE */
899 ts->rdidx2 = 0;
900 }
901 skip4:
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000902 ts = next;
Denis Vlasenkof472b232007-10-16 21:35:17 +0000903 continue;
904 kill_session:
Denys Vlasenko3a416112010-04-05 22:10:38 +0200905 if (ts->shell_pid > 0)
Denys Vlasenkoda921262015-01-05 15:37:58 +0100906 update_utmp_DEAD_PROCESS(ts->shell_pid);
Denis Vlasenkof472b232007-10-16 21:35:17 +0000907 free_session(ts);
908 ts = next;
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000909 }
Denis Vlasenkof472b232007-10-16 21:35:17 +0000910
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000911 goto again;
Eric Andersen08a72202002-09-30 20:52:10 +0000912}