Bernhard Reutner-Fischer | 2c99851 | 2006-04-12 18:09:26 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Bernhard Reutner-Fischer | dac7ff1 | 2006-04-12 17:55:51 +0000 | [diff] [blame] | 2 | /* |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 3 | * Simple telnet server |
| 4 | * Bjorn Wesen, Axis Communications AB (bjornw@axis.com) |
| 5 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 6 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 7 | * |
| 8 | * --------------------------------------------------------------------------- |
| 9 | * (C) Copyright 2000, Axis Communications AB, LUND, SWEDEN |
| 10 | **************************************************************************** |
| 11 | * |
| 12 | * The telnetd manpage says it all: |
| 13 | * |
Denys Vlasenko | a4bcbd0 | 2009-06-09 23:01:24 +0200 | [diff] [blame] | 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. |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 19 | * |
| 20 | * Vladimir Oleynik <dzo@simtreas.ru> 2001 |
Denys Vlasenko | a4bcbd0 | 2009-06-09 23:01:24 +0200 | [diff] [blame] | 21 | * Set process group corrections, initial busybox port |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 22 | */ |
Denys Vlasenko | 47367e1 | 2016-11-23 09:05:14 +0100 | [diff] [blame] | 23 | //config:config TELNETD |
Denys Vlasenko | 4eed2c6 | 2017-07-18 22:01:24 +0200 | [diff] [blame] | 24 | //config: bool "telnetd (12 kb)" |
Denys Vlasenko | 47367e1 | 2016-11-23 09:05:14 +0100 | [diff] [blame] | 25 | //config: default y |
| 26 | //config: select FEATURE_SYSLOG |
| 27 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 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: |
Denys Vlasenko | 47367e1 | 2016-11-23 09:05:14 +0100 | [diff] [blame] | 34 | //config: http://matt.ucc.asn.au/dropbear/dropbear.html |
| 35 | //config: |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 36 | //config: Note that for busybox telnetd to work you need several things: |
| 37 | //config: First of all, your kernel needs: |
Denys Vlasenko | 47367e1 | 2016-11-23 09:05:14 +0100 | [diff] [blame] | 38 | //config: CONFIG_UNIX98_PTYS=y |
| 39 | //config: |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 40 | //config: Next, you need a /dev/pts directory on your root filesystem: |
Denys Vlasenko | 47367e1 | 2016-11-23 09:05:14 +0100 | [diff] [blame] | 41 | //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 Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 45 | //config: Next you need the pseudo terminal master multiplexer /dev/ptmx: |
Denys Vlasenko | 47367e1 | 2016-11-23 09:05:14 +0100 | [diff] [blame] | 46 | //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 Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 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: |
Denys Vlasenko | 47367e1 | 2016-11-23 09:05:14 +0100 | [diff] [blame] | 52 | //config: |
| 53 | //config: mount -t devpts devpts /dev/pts |
| 54 | //config: |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 55 | //config: You need to be sure that busybox has LOGIN and |
| 56 | //config: FEATURE_SUID enabled. And finally, you should make |
Denys Vlasenko | 68b653b | 2017-07-27 10:53:09 +0200 | [diff] [blame] | 57 | //config: certain that busybox has been installed setuid root: |
Denys Vlasenko | 47367e1 | 2016-11-23 09:05:14 +0100 | [diff] [blame] | 58 | //config: |
| 59 | //config: chown root.root /bin/busybox |
| 60 | //config: chmod 4755 /bin/busybox |
| 61 | //config: |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 62 | //config: with all that done, telnetd _should_ work.... |
Denys Vlasenko | 47367e1 | 2016-11-23 09:05:14 +0100 | [diff] [blame] | 63 | //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 Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 69 | //config: Selecting this will make telnetd able to run standalone. |
Denys Vlasenko | 47367e1 | 2016-11-23 09:05:14 +0100 | [diff] [blame] | 70 | //config: |
Sergey Ponomarev | 82c5eb8 | 2021-08-25 23:12:37 +0300 | [diff] [blame] | 71 | //config:config FEATURE_TELNETD_PORT_DEFAULT |
| 72 | //config: int "Default port" |
| 73 | //config: default 23 |
| 74 | //config: range 1 65535 |
| 75 | //config: depends on FEATURE_TELNETD_STANDALONE |
| 76 | //config: |
Denys Vlasenko | 47367e1 | 2016-11-23 09:05:14 +0100 | [diff] [blame] | 77 | //config:config FEATURE_TELNETD_INETD_WAIT |
| 78 | //config: bool "Support -w SEC option (inetd wait mode)" |
| 79 | //config: default y |
| 80 | //config: depends on FEATURE_TELNETD_STANDALONE |
| 81 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 82 | //config: This option allows you to run telnetd in "inet wait" mode. |
| 83 | //config: Example inetd.conf line (note "wait", not usual "nowait"): |
Denys Vlasenko | 47367e1 | 2016-11-23 09:05:14 +0100 | [diff] [blame] | 84 | //config: |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 85 | //config: telnet stream tcp wait root /bin/telnetd telnetd -w10 |
Denys Vlasenko | 47367e1 | 2016-11-23 09:05:14 +0100 | [diff] [blame] | 86 | //config: |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 87 | //config: In this example, inetd passes _listening_ socket_ as fd 0 |
| 88 | //config: to telnetd when connection appears. |
| 89 | //config: telnetd will wait for connections until all existing |
| 90 | //config: connections are closed, and no new connections |
| 91 | //config: appear during 10 seconds. Then it exits, and inetd continues |
| 92 | //config: to listen for new connections. |
Denys Vlasenko | 47367e1 | 2016-11-23 09:05:14 +0100 | [diff] [blame] | 93 | //config: |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 94 | //config: This option is rarely used. "tcp nowait" is much more usual |
| 95 | //config: way of running tcp services, including telnetd. |
| 96 | //config: You most probably want to say N here. |
Denys Vlasenko | 47367e1 | 2016-11-23 09:05:14 +0100 | [diff] [blame] | 97 | |
| 98 | //applet:IF_TELNETD(APPLET(telnetd, BB_DIR_USR_SBIN, BB_SUID_DROP)) |
| 99 | |
| 100 | //kbuild:lib-$(CONFIG_TELNETD) += telnetd.o |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 101 | |
| 102 | //usage:#define telnetd_trivial_usage |
| 103 | //usage: "[OPTIONS]" |
| 104 | //usage:#define telnetd_full_usage "\n\n" |
| 105 | //usage: "Handle incoming telnet connections" |
| 106 | //usage: IF_NOT_FEATURE_TELNETD_STANDALONE(" via inetd") "\n" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 107 | //usage: "\n -l LOGIN Exec LOGIN on connect" |
| 108 | //usage: "\n -f ISSUE_FILE Display ISSUE_FILE instead of /etc/issue" |
| 109 | //usage: "\n -K Close connection as soon as login exits" |
| 110 | //usage: "\n (normally wait until all programs close slave pty)" |
| 111 | //usage: IF_FEATURE_TELNETD_STANDALONE( |
Sergey Ponomarev | 82c5eb8 | 2021-08-25 23:12:37 +0300 | [diff] [blame] | 112 | //usage: "\n -p PORT Port to listen on. Default "STR(CONFIG_FEATURE_TELNETD_PORT_DEFAULT) |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 113 | //usage: "\n -b ADDR[:PORT] Address to bind to" |
| 114 | //usage: "\n -F Run in foreground" |
| 115 | //usage: "\n -i Inetd mode" |
| 116 | //usage: IF_FEATURE_TELNETD_INETD_WAIT( |
| 117 | //usage: "\n -w SEC Inetd 'wait' mode, linger time SEC" |
Denys Vlasenko | 1abaa6b | 2021-06-16 10:49:18 +0200 | [diff] [blame] | 118 | //usage: "\n inetd.conf line: 23 stream tcp wait root telnetd telnetd -w10" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 119 | //usage: "\n -S Log to syslog (implied by -i or without -F and -w)" |
| 120 | //usage: ) |
| 121 | //usage: ) |
| 122 | |
Denis Vlasenko | 75f8d08 | 2006-11-22 15:54:52 +0000 | [diff] [blame] | 123 | #define DEBUG 0 |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 124 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 125 | #include "libbb.h" |
Denys Vlasenko | e6a2f4c | 2016-04-21 16:26:30 +0200 | [diff] [blame] | 126 | #include "common_bufsiz.h" |
Bernhard Reutner-Fischer | f470196 | 2008-01-27 12:50:12 +0000 | [diff] [blame] | 127 | #include <syslog.h> |
Rob Landley | 099ed50 | 2006-08-28 09:41:49 +0000 | [diff] [blame] | 128 | |
Denis Vlasenko | 75f8d08 | 2006-11-22 15:54:52 +0000 | [diff] [blame] | 129 | #if DEBUG |
Denys Vlasenko | 3a41611 | 2010-04-05 22:10:38 +0200 | [diff] [blame] | 130 | # define TELCMDS |
| 131 | # define TELOPTS |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 132 | #endif |
| 133 | #include <arpa/telnet.h> |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 134 | |
Denys Vlasenko | 3a41611 | 2010-04-05 22:10:38 +0200 | [diff] [blame] | 135 | |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 136 | struct tsession { |
| 137 | struct tsession *next; |
Denis Vlasenko | d814c98 | 2009-02-02 23:43:57 +0000 | [diff] [blame] | 138 | pid_t shell_pid; |
Denys Vlasenko | 1d77db8 | 2009-06-10 13:38:08 +0200 | [diff] [blame] | 139 | int sockfd_read; |
| 140 | int sockfd_write; |
| 141 | int ptyfd; |
Denys Vlasenko | 122c47a | 2016-10-12 19:13:46 +0200 | [diff] [blame] | 142 | smallint buffered_IAC_for_pty; |
Denis Vlasenko | 59d7c43 | 2007-10-15 15:19:36 +0000 | [diff] [blame] | 143 | |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 144 | /* two circular buffers */ |
Denis Vlasenko | 59d7c43 | 2007-10-15 15:19:36 +0000 | [diff] [blame] | 145 | /*char *buf1, *buf2;*/ |
Denys Vlasenko | a4bcbd0 | 2009-06-09 23:01:24 +0200 | [diff] [blame] | 146 | /*#define TS_BUF1(ts) ts->buf1*/ |
| 147 | /*#define TS_BUF2(ts) TS_BUF2(ts)*/ |
| 148 | #define TS_BUF1(ts) ((unsigned char*)(ts + 1)) |
| 149 | #define TS_BUF2(ts) (((unsigned char*)(ts + 1)) + BUFSIZE) |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 150 | int rdidx1, wridx1, size1; |
| 151 | int rdidx2, wridx2, size2; |
| 152 | }; |
| 153 | |
Denis Vlasenko | 88308fe | 2007-06-25 10:35:11 +0000 | [diff] [blame] | 154 | /* Two buffers are directly after tsession in malloced memory. |
| 155 | * Make whole thing fit in 4k */ |
Denis Vlasenko | 59d7c43 | 2007-10-15 15:19:36 +0000 | [diff] [blame] | 156 | enum { BUFSIZE = (4 * 1024 - sizeof(struct tsession)) / 2 }; |
Denis Vlasenko | 88308fe | 2007-06-25 10:35:11 +0000 | [diff] [blame] | 157 | |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 158 | |
Denis Vlasenko | 59d7c43 | 2007-10-15 15:19:36 +0000 | [diff] [blame] | 159 | /* Globals */ |
Denys Vlasenko | a4bcbd0 | 2009-06-09 23:01:24 +0200 | [diff] [blame] | 160 | struct globals { |
| 161 | struct tsession *sessions; |
| 162 | const char *loginpath; |
| 163 | const char *issuefile; |
| 164 | int maxfd; |
Denys Vlasenko | 98a4c7c | 2010-02-04 15:00:15 +0100 | [diff] [blame] | 165 | } FIX_ALIASING; |
Denys Vlasenko | e6a2f4c | 2016-04-21 16:26:30 +0200 | [diff] [blame] | 166 | #define G (*(struct globals*)bb_common_bufsiz1) |
Denys Vlasenko | a4bcbd0 | 2009-06-09 23:01:24 +0200 | [diff] [blame] | 167 | #define INIT_G() do { \ |
Denys Vlasenko | 47cfbf3 | 2016-04-21 18:18:48 +0200 | [diff] [blame] | 168 | setup_common_bufsiz(); \ |
Denys Vlasenko | a4bcbd0 | 2009-06-09 23:01:24 +0200 | [diff] [blame] | 169 | G.loginpath = "/bin/login"; \ |
| 170 | G.issuefile = "/etc/issue.net"; \ |
| 171 | } while (0) |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 172 | |
| 173 | |
Denys Vlasenko | 122c47a | 2016-10-12 19:13:46 +0200 | [diff] [blame] | 174 | /* Write some buf1 data to pty, processing IACs. |
Denys Vlasenko | 2a54b3e | 2016-10-12 14:54:10 +0200 | [diff] [blame] | 175 | * Update wridx1 and size1. Return < 0 on error. |
| 176 | * Buggy if IAC is present but incomplete: skips them. |
Denis Vlasenko | 75f8d08 | 2006-11-22 15:54:52 +0000 | [diff] [blame] | 177 | */ |
Denys Vlasenko | 2a54b3e | 2016-10-12 14:54:10 +0200 | [diff] [blame] | 178 | static ssize_t |
| 179 | safe_write_to_pty_decode_iac(struct tsession *ts) |
Denis Vlasenko | 75f8d08 | 2006-11-22 15:54:52 +0000 | [diff] [blame] | 180 | { |
Denys Vlasenko | 2a54b3e | 2016-10-12 14:54:10 +0200 | [diff] [blame] | 181 | unsigned wr; |
| 182 | ssize_t rc; |
| 183 | unsigned char *buf; |
| 184 | unsigned char *found; |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 185 | |
Denys Vlasenko | 2a54b3e | 2016-10-12 14:54:10 +0200 | [diff] [blame] | 186 | buf = TS_BUF1(ts) + ts->wridx1; |
| 187 | wr = MIN(BUFSIZE - ts->wridx1, ts->size1); |
Denys Vlasenko | 122c47a | 2016-10-12 19:13:46 +0200 | [diff] [blame] | 188 | /* wr is at least 1 here */ |
| 189 | |
| 190 | if (ts->buffered_IAC_for_pty) { |
| 191 | /* Last time we stopped on a "dangling" IAC byte. |
| 192 | * We removed it from the buffer back then. |
| 193 | * Now pretend it's still there, and jump to IAC processing. |
| 194 | */ |
| 195 | ts->buffered_IAC_for_pty = 0; |
| 196 | wr++; |
| 197 | ts->size1++; |
| 198 | buf--; /* Yes, this can point before the buffer. It's ok */ |
| 199 | ts->wridx1--; |
| 200 | goto handle_iac; |
| 201 | } |
| 202 | |
Denys Vlasenko | 2a54b3e | 2016-10-12 14:54:10 +0200 | [diff] [blame] | 203 | found = memchr(buf, IAC, wr); |
| 204 | if (found != buf) { |
| 205 | /* There is a "prefix" of non-IAC chars. |
| 206 | * Write only them, and return. |
Denis Vlasenko | b0150d2 | 2008-11-07 01:58:21 +0000 | [diff] [blame] | 207 | */ |
Denys Vlasenko | 2a54b3e | 2016-10-12 14:54:10 +0200 | [diff] [blame] | 208 | if (found) |
| 209 | wr = found - buf; |
| 210 | |
| 211 | /* We map \r\n ==> \r for pragmatic reasons: |
| 212 | * many client implementations send \r\n when |
| 213 | * the user hits the CarriageReturn key. |
| 214 | * See RFC 1123 3.3.1 Telnet End-of-Line Convention. |
Denis Vlasenko | b0150d2 | 2008-11-07 01:58:21 +0000 | [diff] [blame] | 215 | */ |
Denys Vlasenko | 2a54b3e | 2016-10-12 14:54:10 +0200 | [diff] [blame] | 216 | rc = wr; |
| 217 | found = memchr(buf, '\r', wr); |
| 218 | if (found) |
| 219 | rc = found - buf + 1; |
| 220 | rc = safe_write(ts->ptyfd, buf, rc); |
| 221 | if (rc <= 0) |
| 222 | return rc; |
Denys Vlasenko | 0190c41 | 2016-10-12 17:36:57 +0200 | [diff] [blame] | 223 | if (rc < wr /* don't look past available data */ |
| 224 | && buf[rc-1] == '\r' /* need this: imagine that write was _short_ */ |
| 225 | && (buf[rc] == '\n' || buf[rc] == '\0') |
| 226 | ) { |
Denys Vlasenko | 2a54b3e | 2016-10-12 14:54:10 +0200 | [diff] [blame] | 227 | rc++; |
Denys Vlasenko | 0190c41 | 2016-10-12 17:36:57 +0200 | [diff] [blame] | 228 | } |
Denys Vlasenko | 2a54b3e | 2016-10-12 14:54:10 +0200 | [diff] [blame] | 229 | goto update_and_return; |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 230 | } |
| 231 | |
Denys Vlasenko | 2a54b3e | 2016-10-12 14:54:10 +0200 | [diff] [blame] | 232 | /* buf starts with IAC char. Process that sequence. |
| 233 | * Example: we get this from our own (bbox) telnet client: |
Denys Vlasenko | 0190c41 | 2016-10-12 17:36:57 +0200 | [diff] [blame] | 234 | * read(5, "\377\374\1""\377\373\37""\377\372\37\0\262\0@\377\360""\377\375\1""\377\375\3"): |
| 235 | * IAC WONT ECHO, IAC WILL NAWS, IAC SB NAWS <cols> <rows> IAC SE, IAC DO SGA |
Denys Vlasenko | 26d88d6 | 2016-10-12 20:09:22 +0200 | [diff] [blame] | 236 | * Another example (telnet-0.17 from old-netkit): |
| 237 | * read(4, "\377\375\3""\377\373\30""\377\373\37""\377\373 ""\377\373!""\377\373\"""\377\373'" |
| 238 | * "\377\375\5""\377\373#""\377\374\1""\377\372\37\0\257\0I\377\360""\377\375\1"): |
| 239 | * IAC DO SGA, IAC WILL TTYPE, IAC WILL NAWS, IAC WILL TSPEED, IAC WILL LFLOW, IAC WILL LINEMODE, IAC WILL NEW_ENVIRON, |
| 240 | * IAC DO STATUS, IAC WILL XDISPLOC, IAC WONT ECHO, IAC SB NAWS <cols> <rows> IAC SE, IAC DO ECHO |
Denys Vlasenko | 2a54b3e | 2016-10-12 14:54:10 +0200 | [diff] [blame] | 241 | */ |
| 242 | if (wr <= 1) { |
Denys Vlasenko | 122c47a | 2016-10-12 19:13:46 +0200 | [diff] [blame] | 243 | /* Only the single IAC byte is in the buffer, eat it |
| 244 | * and set a flag "process the rest of the sequence |
| 245 | * next time we are here". |
| 246 | */ |
| 247 | //bb_error_msg("dangling IAC!"); |
| 248 | ts->buffered_IAC_for_pty = 1; |
Denys Vlasenko | 2a54b3e | 2016-10-12 14:54:10 +0200 | [diff] [blame] | 249 | rc = 1; |
| 250 | goto update_and_return; |
| 251 | } |
| 252 | |
Denys Vlasenko | 122c47a | 2016-10-12 19:13:46 +0200 | [diff] [blame] | 253 | handle_iac: |
Denys Vlasenko | 0190c41 | 2016-10-12 17:36:57 +0200 | [diff] [blame] | 254 | /* 2-byte commands (240..250 and 255): |
| 255 | * IAC IAC (255) Literal 255. Supported. |
Denys Vlasenko | b6d421b | 2016-10-12 19:41:33 +0200 | [diff] [blame] | 256 | * IAC SE (240) End of subnegotiation. Treated as NOP. |
Denys Vlasenko | 0190c41 | 2016-10-12 17:36:57 +0200 | [diff] [blame] | 257 | * IAC NOP (241) NOP. Supported. |
| 258 | * IAC BRK (243) Break. Like serial line break. TODO via tcsendbreak()? |
Martin Lewis | 93594b1 | 2019-04-04 13:29:32 +0200 | [diff] [blame] | 259 | * IAC AYT (246) Are you there. |
Denys Vlasenko | 0190c41 | 2016-10-12 17:36:57 +0200 | [diff] [blame] | 260 | * These don't look useful: |
| 261 | * IAC DM (242) Data mark. What is this? |
| 262 | * IAC IP (244) Suspend, interrupt or abort the process. (Ancient cousin of ^C). |
| 263 | * IAC AO (245) Abort output. "You can continue running, but do not send me the output". |
| 264 | * IAC EC (247) Erase character. The receiver should delete the last received char. |
| 265 | * IAC EL (248) Erase line. The receiver should delete everything up tp last newline. |
| 266 | * IAC GA (249) Go ahead. For half-duplex lines: "now you talk". |
Denys Vlasenko | b6d421b | 2016-10-12 19:41:33 +0200 | [diff] [blame] | 267 | * Implemented only as part of NAWS: |
| 268 | * IAC SB (250) Subnegotiation of an option follows. |
Denys Vlasenko | 0190c41 | 2016-10-12 17:36:57 +0200 | [diff] [blame] | 269 | */ |
Denys Vlasenko | b6d421b | 2016-10-12 19:41:33 +0200 | [diff] [blame] | 270 | if (buf[1] == IAC) { |
| 271 | /* Literal 255 (emacs M-DEL) */ |
Denys Vlasenko | 122c47a | 2016-10-12 19:13:46 +0200 | [diff] [blame] | 272 | //bb_error_msg("255!"); |
| 273 | rc = safe_write(ts->ptyfd, &buf[1], 1); |
Denys Vlasenko | 662634b | 2016-10-13 16:17:06 +0200 | [diff] [blame] | 274 | /* |
| 275 | * If we went through buffered_IAC_for_pty==1 path, |
| 276 | * bailing out on error like below messes up the buffer. |
| 277 | * EAGAIN is highly unlikely here, other errors will be |
| 278 | * repeated on next write, let's just skip error check. |
| 279 | */ |
| 280 | #if 0 |
Denys Vlasenko | 2a54b3e | 2016-10-12 14:54:10 +0200 | [diff] [blame] | 281 | if (rc <= 0) |
| 282 | return rc; |
Denys Vlasenko | 662634b | 2016-10-13 16:17:06 +0200 | [diff] [blame] | 283 | #endif |
Denys Vlasenko | 2a54b3e | 2016-10-12 14:54:10 +0200 | [diff] [blame] | 284 | rc = 2; |
| 285 | goto update_and_return; |
| 286 | } |
Martin Lewis | 93594b1 | 2019-04-04 13:29:32 +0200 | [diff] [blame] | 287 | if (buf[1] == AYT) { |
Denys Vlasenko | c6a8965 | 2019-04-04 16:00:23 +0200 | [diff] [blame] | 288 | if (ts->size2 == 0) { /* if nothing buffered yet... */ |
| 289 | /* Send back evidence that AYT was seen */ |
| 290 | unsigned char *buf2 = TS_BUF2(ts); |
| 291 | buf2[0] = IAC; |
| 292 | buf2[1] = NOP; |
| 293 | ts->wridx2 = 0; |
| 294 | ts->rdidx2 = ts->size2 = 2; |
Denys Vlasenko | 29c2dcf | 2019-04-04 16:54:14 +0200 | [diff] [blame] | 295 | } |
Martin Lewis | 93594b1 | 2019-04-04 13:29:32 +0200 | [diff] [blame] | 296 | rc = 2; |
| 297 | goto update_and_return; |
| 298 | } |
Denys Vlasenko | b6d421b | 2016-10-12 19:41:33 +0200 | [diff] [blame] | 299 | if (buf[1] >= 240 && buf[1] <= 249) { |
| 300 | /* NOP (241). Ignore (putty keepalive, etc) */ |
| 301 | /* All other 2-byte commands also treated as NOPs here */ |
Denys Vlasenko | 2a54b3e | 2016-10-12 14:54:10 +0200 | [diff] [blame] | 302 | rc = 2; |
| 303 | goto update_and_return; |
| 304 | } |
| 305 | |
| 306 | if (wr <= 2) { |
Denys Vlasenko | 122c47a | 2016-10-12 19:13:46 +0200 | [diff] [blame] | 307 | /* BUG: only 2 bytes of the IAC is in the buffer, we just eat them. |
| 308 | * This is not a practical problem since >2 byte IACs are seen only |
| 309 | * in initial negotiation, when buffer is empty |
| 310 | */ |
Denys Vlasenko | 2a54b3e | 2016-10-12 14:54:10 +0200 | [diff] [blame] | 311 | rc = 2; |
| 312 | goto update_and_return; |
| 313 | } |
| 314 | |
Denys Vlasenko | b6d421b | 2016-10-12 19:41:33 +0200 | [diff] [blame] | 315 | if (buf[1] == SB) { |
| 316 | if (buf[2] == TELOPT_NAWS) { |
| 317 | /* IAC SB, TELOPT_NAWS, 4-byte, IAC SE */ |
| 318 | struct winsize ws; |
| 319 | if (wr <= 6) { |
Denys Vlasenko | 0190c41 | 2016-10-12 17:36:57 +0200 | [diff] [blame] | 320 | /* BUG: incomplete, can't process */ |
Denys Vlasenko | b6d421b | 2016-10-12 19:41:33 +0200 | [diff] [blame] | 321 | rc = wr; |
| 322 | goto update_and_return; |
| 323 | } |
| 324 | memset(&ws, 0, sizeof(ws)); /* pixel sizes are set to 0 */ |
| 325 | ws.ws_col = (buf[3] << 8) | buf[4]; |
| 326 | ws.ws_row = (buf[5] << 8) | buf[6]; |
| 327 | ioctl(ts->ptyfd, TIOCSWINSZ, (char *)&ws); |
| 328 | rc = 7; |
| 329 | /* trailing IAC SE will be eaten separately, as 2-byte NOP */ |
Denys Vlasenko | 2a54b3e | 2016-10-12 14:54:10 +0200 | [diff] [blame] | 330 | goto update_and_return; |
| 331 | } |
Denys Vlasenko | b6d421b | 2016-10-12 19:41:33 +0200 | [diff] [blame] | 332 | /* else: other subnegs not supported yet */ |
Denys Vlasenko | 2a54b3e | 2016-10-12 14:54:10 +0200 | [diff] [blame] | 333 | } |
Denys Vlasenko | 0190c41 | 2016-10-12 17:36:57 +0200 | [diff] [blame] | 334 | |
Denys Vlasenko | b6d421b | 2016-10-12 19:41:33 +0200 | [diff] [blame] | 335 | /* Assume it is a 3-byte WILL/WONT/DO/DONT 251..254 command and skip it */ |
Denys Vlasenko | 2a54b3e | 2016-10-12 14:54:10 +0200 | [diff] [blame] | 336 | #if DEBUG |
| 337 | fprintf(stderr, "Ignoring IAC %s,%s\n", |
| 338 | TELCMD(buf[1]), TELOPT(buf[2])); |
| 339 | #endif |
| 340 | rc = 3; |
| 341 | |
| 342 | update_and_return: |
| 343 | ts->wridx1 += rc; |
| 344 | if (ts->wridx1 >= BUFSIZE) /* actually == BUFSIZE */ |
| 345 | ts->wridx1 = 0; |
| 346 | ts->size1 -= rc; |
| 347 | /* |
Denys Vlasenko | 0190c41 | 2016-10-12 17:36:57 +0200 | [diff] [blame] | 348 | * Hack. We cannot process IACs which wrap around buffer's end. |
Denys Vlasenko | 2a54b3e | 2016-10-12 14:54:10 +0200 | [diff] [blame] | 349 | * Since properly fixing it requires writing bigger code, |
| 350 | * we rely instead on this code making it virtually impossible |
Denys Vlasenko | 0190c41 | 2016-10-12 17:36:57 +0200 | [diff] [blame] | 351 | * to have wrapped IAC (people don't type at 2k/second). |
Denys Vlasenko | 2a54b3e | 2016-10-12 14:54:10 +0200 | [diff] [blame] | 352 | * It also allows for bigger reads in common case. |
| 353 | */ |
| 354 | if (ts->size1 == 0) { /* very typical */ |
| 355 | //bb_error_msg("zero size1"); |
| 356 | ts->rdidx1 = 0; |
| 357 | ts->wridx1 = 0; |
| 358 | return rc; |
| 359 | } |
| 360 | wr = ts->wridx1; |
| 361 | if (wr != 0 && wr < ts->rdidx1) { |
| 362 | /* Buffer is not wrapped yet. |
| 363 | * We can easily move it to the beginning. |
| 364 | */ |
| 365 | //bb_error_msg("moved %d", wr); |
| 366 | memmove(TS_BUF1(ts), TS_BUF1(ts) + wr, ts->size1); |
| 367 | ts->rdidx1 -= wr; |
| 368 | ts->wridx1 = 0; |
| 369 | } |
| 370 | return rc; |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 371 | } |
| 372 | |
Denis Vlasenko | 9f2f808 | 2008-11-11 02:56:39 +0000 | [diff] [blame] | 373 | /* |
Denis Vlasenko | 81c6a91 | 2008-11-12 21:14:50 +0000 | [diff] [blame] | 374 | * Converting single IAC into double on output |
Denis Vlasenko | 9f2f808 | 2008-11-11 02:56:39 +0000 | [diff] [blame] | 375 | */ |
Denys Vlasenko | 2a54b3e | 2016-10-12 14:54:10 +0200 | [diff] [blame] | 376 | static size_t safe_write_double_iac(int fd, const char *buf, size_t count) |
Denis Vlasenko | 9f2f808 | 2008-11-11 02:56:39 +0000 | [diff] [blame] | 377 | { |
Denis Vlasenko | 81c6a91 | 2008-11-12 21:14:50 +0000 | [diff] [blame] | 378 | const char *IACptr; |
Denis Vlasenko | 9f2f808 | 2008-11-11 02:56:39 +0000 | [diff] [blame] | 379 | size_t wr, rc, total; |
| 380 | |
| 381 | total = 0; |
| 382 | while (1) { |
| 383 | if (count == 0) |
| 384 | return total; |
Denis Vlasenko | 81c6a91 | 2008-11-12 21:14:50 +0000 | [diff] [blame] | 385 | if (*buf == (char)IAC) { |
| 386 | static const char IACIAC[] ALIGN1 = { IAC, IAC }; |
| 387 | rc = safe_write(fd, IACIAC, 2); |
Denys Vlasenko | 0190c41 | 2016-10-12 17:36:57 +0200 | [diff] [blame] | 388 | /* BUG: if partial write was only 1 byte long, we end up emitting just one IAC */ |
Denis Vlasenko | 9f2f808 | 2008-11-11 02:56:39 +0000 | [diff] [blame] | 389 | if (rc != 2) |
| 390 | break; |
| 391 | buf++; |
| 392 | total++; |
| 393 | count--; |
| 394 | continue; |
| 395 | } |
Denis Vlasenko | 81c6a91 | 2008-11-12 21:14:50 +0000 | [diff] [blame] | 396 | /* count != 0, *buf != IAC */ |
| 397 | IACptr = memchr(buf, IAC, count); |
Denis Vlasenko | 9f2f808 | 2008-11-11 02:56:39 +0000 | [diff] [blame] | 398 | wr = count; |
Denis Vlasenko | 81c6a91 | 2008-11-12 21:14:50 +0000 | [diff] [blame] | 399 | if (IACptr) |
| 400 | wr = IACptr - buf; |
Denis Vlasenko | 9f2f808 | 2008-11-11 02:56:39 +0000 | [diff] [blame] | 401 | rc = safe_write(fd, buf, wr); |
| 402 | if (rc != wr) |
| 403 | break; |
| 404 | buf += rc; |
| 405 | total += rc; |
| 406 | count -= rc; |
| 407 | } |
| 408 | /* here: rc - result of last short write */ |
| 409 | if ((ssize_t)rc < 0) { /* error? */ |
| 410 | if (total == 0) |
| 411 | return rc; |
| 412 | rc = 0; |
| 413 | } |
| 414 | return total + rc; |
| 415 | } |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 416 | |
Denys Vlasenko | a4bcbd0 | 2009-06-09 23:01:24 +0200 | [diff] [blame] | 417 | /* Must match getopt32 string */ |
| 418 | enum { |
| 419 | OPT_WATCHCHILD = (1 << 2), /* -K */ |
| 420 | OPT_INETD = (1 << 3) * ENABLE_FEATURE_TELNETD_STANDALONE, /* -i */ |
Denys Vlasenko | ed1667e | 2009-09-04 02:21:13 +0200 | [diff] [blame] | 421 | OPT_PORT = (1 << 4) * ENABLE_FEATURE_TELNETD_STANDALONE, /* -p PORT */ |
Denys Vlasenko | a4bcbd0 | 2009-06-09 23:01:24 +0200 | [diff] [blame] | 422 | OPT_FOREGROUND = (1 << 6) * ENABLE_FEATURE_TELNETD_STANDALONE, /* -F */ |
Denys Vlasenko | ed1667e | 2009-09-04 02:21:13 +0200 | [diff] [blame] | 423 | OPT_SYSLOG = (1 << 7) * ENABLE_FEATURE_TELNETD_INETD_WAIT, /* -S */ |
| 424 | OPT_WAIT = (1 << 8) * ENABLE_FEATURE_TELNETD_INETD_WAIT, /* -w SEC */ |
Denys Vlasenko | a4bcbd0 | 2009-06-09 23:01:24 +0200 | [diff] [blame] | 425 | }; |
| 426 | |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 427 | static struct tsession * |
Denis Vlasenko | 75f8d08 | 2006-11-22 15:54:52 +0000 | [diff] [blame] | 428 | make_new_session( |
Denys Vlasenko | 1d77db8 | 2009-06-10 13:38:08 +0200 | [diff] [blame] | 429 | IF_FEATURE_TELNETD_STANDALONE(int sock) |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 430 | IF_NOT_FEATURE_TELNETD_STANDALONE(void) |
Denis Vlasenko | 75f8d08 | 2006-11-22 15:54:52 +0000 | [diff] [blame] | 431 | ) { |
Denys Vlasenko | f6916db | 2010-04-06 17:43:29 +0200 | [diff] [blame] | 432 | #if !ENABLE_FEATURE_TELNETD_STANDALONE |
Denys Vlasenko | ff0e875 | 2010-05-10 04:16:43 +0200 | [diff] [blame] | 433 | enum { sock = 0 }; |
Denys Vlasenko | f6916db | 2010-04-06 17:43:29 +0200 | [diff] [blame] | 434 | #endif |
Denis Vlasenko | 88308fe | 2007-06-25 10:35:11 +0000 | [diff] [blame] | 435 | const char *login_argv[2]; |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 436 | struct termios termbuf; |
Denis Vlasenko | 75f8d08 | 2006-11-22 15:54:52 +0000 | [diff] [blame] | 437 | int fd, pid; |
Denis Vlasenko | 85c2471 | 2008-03-17 09:04:04 +0000 | [diff] [blame] | 438 | char tty_name[GETPTY_BUFSIZE]; |
Rob Landley | 1ec5b29 | 2006-05-29 07:42:02 +0000 | [diff] [blame] | 439 | struct tsession *ts = xzalloc(sizeof(struct tsession) + BUFSIZE * 2); |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 440 | |
Denis Vlasenko | 59d7c43 | 2007-10-15 15:19:36 +0000 | [diff] [blame] | 441 | /*ts->buf1 = (char *)(ts + 1);*/ |
| 442 | /*ts->buf2 = ts->buf1 + BUFSIZE;*/ |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 443 | |
Denys Vlasenko | 1d77db8 | 2009-06-10 13:38:08 +0200 | [diff] [blame] | 444 | /* Got a new connection, set up a tty */ |
Bernhard Reutner-Fischer | ae4342c | 2008-05-19 08:18:50 +0000 | [diff] [blame] | 445 | fd = xgetpty(tty_name); |
Denys Vlasenko | a4bcbd0 | 2009-06-09 23:01:24 +0200 | [diff] [blame] | 446 | if (fd > G.maxfd) |
| 447 | G.maxfd = fd; |
Denis Vlasenko | 59d7c43 | 2007-10-15 15:19:36 +0000 | [diff] [blame] | 448 | ts->ptyfd = fd; |
| 449 | ndelay_on(fd); |
Denys Vlasenko | 1d77db8 | 2009-06-10 13:38:08 +0200 | [diff] [blame] | 450 | close_on_exec_on(fd); |
| 451 | |
Denis Vlasenko | 6d04435 | 2008-11-09 00:44:40 +0000 | [diff] [blame] | 452 | /* SO_KEEPALIVE by popular demand */ |
Denys Vlasenko | c52cbea | 2015-08-24 19:48:03 +0200 | [diff] [blame] | 453 | setsockopt_keepalive(sock); |
Denys Vlasenko | f6916db | 2010-04-06 17:43:29 +0200 | [diff] [blame] | 454 | #if ENABLE_FEATURE_TELNETD_STANDALONE |
Denys Vlasenko | 1d77db8 | 2009-06-10 13:38:08 +0200 | [diff] [blame] | 455 | ts->sockfd_read = sock; |
Denis Vlasenko | f472b23 | 2007-10-16 21:35:17 +0000 | [diff] [blame] | 456 | ndelay_on(sock); |
Denys Vlasenko | 1d77db8 | 2009-06-10 13:38:08 +0200 | [diff] [blame] | 457 | if (sock == 0) { /* We are called with fd 0 - we are in inetd mode */ |
Denis Vlasenko | 9e23767 | 2007-10-17 11:18:49 +0000 | [diff] [blame] | 458 | sock++; /* so use fd 1 for output */ |
| 459 | ndelay_on(sock); |
| 460 | } |
Denis Vlasenko | f472b23 | 2007-10-16 21:35:17 +0000 | [diff] [blame] | 461 | ts->sockfd_write = sock; |
Denys Vlasenko | a4bcbd0 | 2009-06-09 23:01:24 +0200 | [diff] [blame] | 462 | if (sock > G.maxfd) |
| 463 | G.maxfd = sock; |
Denis Vlasenko | 75f8d08 | 2006-11-22 15:54:52 +0000 | [diff] [blame] | 464 | #else |
Denis Vlasenko | 59d7c43 | 2007-10-15 15:19:36 +0000 | [diff] [blame] | 465 | /* ts->sockfd_read = 0; - done by xzalloc */ |
Denis Vlasenko | f472b23 | 2007-10-16 21:35:17 +0000 | [diff] [blame] | 466 | ts->sockfd_write = 1; |
Denis Vlasenko | 75f8d08 | 2006-11-22 15:54:52 +0000 | [diff] [blame] | 467 | ndelay_on(0); |
| 468 | ndelay_on(1); |
| 469 | #endif |
Denys Vlasenko | 1d77db8 | 2009-06-10 13:38:08 +0200 | [diff] [blame] | 470 | |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 471 | /* Make the telnet client understand we will echo characters so it |
| 472 | * should not do it locally. We don't tell the client to run linemode, |
| 473 | * because we want to handle line editing and tab completion and other |
Denis Vlasenko | 75f8d08 | 2006-11-22 15:54:52 +0000 | [diff] [blame] | 474 | * stuff that requires char-by-char support. */ |
Denis Vlasenko | 59d7c43 | 2007-10-15 15:19:36 +0000 | [diff] [blame] | 475 | { |
| 476 | static const char iacs_to_send[] ALIGN1 = { |
| 477 | IAC, DO, TELOPT_ECHO, |
| 478 | IAC, DO, TELOPT_NAWS, |
Denys Vlasenko | fb132e4 | 2010-10-29 11:46:52 +0200 | [diff] [blame] | 479 | /* This requires telnetd.ctrlSQ.patch (incomplete) */ |
| 480 | /*IAC, DO, TELOPT_LFLOW,*/ |
Denis Vlasenko | 59d7c43 | 2007-10-15 15:19:36 +0000 | [diff] [blame] | 481 | IAC, WILL, TELOPT_ECHO, |
| 482 | IAC, WILL, TELOPT_SGA |
| 483 | }; |
Denys Vlasenko | 2a54b3e | 2016-10-12 14:54:10 +0200 | [diff] [blame] | 484 | /* This confuses safe_write_double_iac(), it will try to duplicate |
Denis Vlasenko | 9f2f808 | 2008-11-11 02:56:39 +0000 | [diff] [blame] | 485 | * each IAC... */ |
Denys Vlasenko | a4bcbd0 | 2009-06-09 23:01:24 +0200 | [diff] [blame] | 486 | //memcpy(TS_BUF2(ts), iacs_to_send, sizeof(iacs_to_send)); |
Denis Vlasenko | 9f2f808 | 2008-11-11 02:56:39 +0000 | [diff] [blame] | 487 | //ts->rdidx2 = sizeof(iacs_to_send); |
| 488 | //ts->size2 = sizeof(iacs_to_send); |
Denis Vlasenko | 81c6a91 | 2008-11-12 21:14:50 +0000 | [diff] [blame] | 489 | /* So just stuff it into TCP stream! (no error check...) */ |
| 490 | #if ENABLE_FEATURE_TELNETD_STANDALONE |
Denis Vlasenko | 9f2f808 | 2008-11-11 02:56:39 +0000 | [diff] [blame] | 491 | safe_write(sock, iacs_to_send, sizeof(iacs_to_send)); |
Denis Vlasenko | 81c6a91 | 2008-11-12 21:14:50 +0000 | [diff] [blame] | 492 | #else |
| 493 | safe_write(1, iacs_to_send, sizeof(iacs_to_send)); |
| 494 | #endif |
| 495 | /*ts->rdidx2 = 0; - xzalloc did it */ |
Denis Vlasenko | 9f2f808 | 2008-11-11 02:56:39 +0000 | [diff] [blame] | 496 | /*ts->size2 = 0;*/ |
Denis Vlasenko | 59d7c43 | 2007-10-15 15:19:36 +0000 | [diff] [blame] | 497 | } |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 498 | |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 499 | fflush_all(); |
Denis Vlasenko | 59d7c43 | 2007-10-15 15:19:36 +0000 | [diff] [blame] | 500 | pid = vfork(); /* NOMMU-friendly */ |
Denis Vlasenko | 75f8d08 | 2006-11-22 15:54:52 +0000 | [diff] [blame] | 501 | if (pid < 0) { |
| 502 | free(ts); |
| 503 | close(fd); |
Denis Vlasenko | 23c8128 | 2007-10-16 22:01:23 +0000 | [diff] [blame] | 504 | /* sock will be closed by caller */ |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 505 | bb_simple_perror_msg("vfork"); |
Denis Vlasenko | 75f8d08 | 2006-11-22 15:54:52 +0000 | [diff] [blame] | 506 | return NULL; |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 507 | } |
Denis Vlasenko | 75f8d08 | 2006-11-22 15:54:52 +0000 | [diff] [blame] | 508 | if (pid > 0) { |
Denis Vlasenko | 59d7c43 | 2007-10-15 15:19:36 +0000 | [diff] [blame] | 509 | /* Parent */ |
Denis Vlasenko | 2450c45 | 2007-10-15 22:09:15 +0000 | [diff] [blame] | 510 | ts->shell_pid = pid; |
Denis Vlasenko | 75f8d08 | 2006-11-22 15:54:52 +0000 | [diff] [blame] | 511 | return ts; |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 512 | } |
Denis Vlasenko | f7996f3 | 2007-01-11 17:20:00 +0000 | [diff] [blame] | 513 | |
Denis Vlasenko | 59d7c43 | 2007-10-15 15:19:36 +0000 | [diff] [blame] | 514 | /* Child */ |
| 515 | /* Careful - we are after vfork! */ |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 516 | |
Denis Vlasenko | d814c98 | 2009-02-02 23:43:57 +0000 | [diff] [blame] | 517 | /* Restore default signal handling ASAP */ |
Denis Vlasenko | 25591c3 | 2008-02-16 22:58:56 +0000 | [diff] [blame] | 518 | bb_signals((1 << SIGCHLD) + (1 << SIGPIPE), SIG_DFL); |
Denis Vlasenko | 018b155 | 2007-11-06 01:38:46 +0000 | [diff] [blame] | 519 | |
Denys Vlasenko | 58c3d21 | 2010-11-30 09:17:30 +0100 | [diff] [blame] | 520 | pid = getpid(); |
| 521 | |
Denys Vlasenko | f6916db | 2010-04-06 17:43:29 +0200 | [diff] [blame] | 522 | if (ENABLE_FEATURE_UTMP) { |
| 523 | len_and_sockaddr *lsa = get_peer_lsa(sock); |
| 524 | char *hostname = NULL; |
| 525 | if (lsa) { |
| 526 | hostname = xmalloc_sockaddr2dotted(&lsa->u.sa); |
| 527 | free(lsa); |
| 528 | } |
| 529 | write_new_utmp(pid, LOGIN_PROCESS, tty_name, /*username:*/ "LOGIN", hostname); |
| 530 | free(hostname); |
| 531 | } |
| 532 | |
Denis Vlasenko | d814c98 | 2009-02-02 23:43:57 +0000 | [diff] [blame] | 533 | /* Make new session and process group */ |
| 534 | setsid(); |
| 535 | |
Denys Vlasenko | 1d77db8 | 2009-06-10 13:38:08 +0200 | [diff] [blame] | 536 | /* Open the child's side of the tty */ |
Denis Vlasenko | 9af7c9d | 2007-01-19 21:19:35 +0000 | [diff] [blame] | 537 | /* NB: setsid() disconnects from any previous ctty's. Therefore |
| 538 | * we must open child's side of the tty AFTER setsid! */ |
Denys Vlasenko | 1d77db8 | 2009-06-10 13:38:08 +0200 | [diff] [blame] | 539 | close(0); |
Denis Vlasenko | 1101d1c | 2008-07-21 09:22:28 +0000 | [diff] [blame] | 540 | xopen(tty_name, O_RDWR); /* becomes our ctty */ |
| 541 | xdup2(0, 1); |
| 542 | xdup2(0, 2); |
Denys Vlasenko | 3a41611 | 2010-04-05 22:10:38 +0200 | [diff] [blame] | 543 | tcsetpgrp(0, pid); /* switch this tty's process group to us */ |
| 544 | |
Denys Vlasenko | 1d77db8 | 2009-06-10 13:38:08 +0200 | [diff] [blame] | 545 | /* The pseudo-terminal allocated to the client is configured to operate |
| 546 | * in cooked mode, and with XTABS CRMOD enabled (see tty(4)) */ |
Denis Vlasenko | 75f8d08 | 2006-11-22 15:54:52 +0000 | [diff] [blame] | 547 | tcgetattr(0, &termbuf); |
| 548 | termbuf.c_lflag |= ECHO; /* if we use readline we dont want this */ |
Denis Vlasenko | 59d7c43 | 2007-10-15 15:19:36 +0000 | [diff] [blame] | 549 | termbuf.c_oflag |= ONLCR | XTABS; |
Denis Vlasenko | 75f8d08 | 2006-11-22 15:54:52 +0000 | [diff] [blame] | 550 | termbuf.c_iflag |= ICRNL; |
| 551 | termbuf.c_iflag &= ~IXOFF; |
| 552 | /*termbuf.c_lflag &= ~ICANON;*/ |
Denis Vlasenko | 202ac50 | 2008-11-05 13:20:58 +0000 | [diff] [blame] | 553 | tcsetattr_stdin_TCSANOW(&termbuf); |
Denis Vlasenko | 75f8d08 | 2006-11-22 15:54:52 +0000 | [diff] [blame] | 554 | |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 555 | /* Uses FILE-based I/O to stdout, but does fflush_all(), |
Denis Vlasenko | 59d7c43 | 2007-10-15 15:19:36 +0000 | [diff] [blame] | 556 | * so should be safe with vfork. |
| 557 | * I fear, though, that some users will have ridiculously big |
Denis Vlasenko | 018b155 | 2007-11-06 01:38:46 +0000 | [diff] [blame] | 558 | * issue files, and they may block writing to fd 1, |
| 559 | * (parent is supposed to read it, but parent waits |
| 560 | * for vforked child to exec!) */ |
Denys Vlasenko | a4bcbd0 | 2009-06-09 23:01:24 +0200 | [diff] [blame] | 561 | print_login_issue(G.issuefile, tty_name); |
Denis Vlasenko | 75f8d08 | 2006-11-22 15:54:52 +0000 | [diff] [blame] | 562 | |
Denis Vlasenko | 59d7c43 | 2007-10-15 15:19:36 +0000 | [diff] [blame] | 563 | /* Exec shell / login / whatever */ |
Denys Vlasenko | a4bcbd0 | 2009-06-09 23:01:24 +0200 | [diff] [blame] | 564 | login_argv[0] = G.loginpath; |
Denis Vlasenko | 88308fe | 2007-06-25 10:35:11 +0000 | [diff] [blame] | 565 | login_argv[1] = NULL; |
Denis Vlasenko | 018b155 | 2007-11-06 01:38:46 +0000 | [diff] [blame] | 566 | /* exec busybox applet (if PREFER_APPLETS=y), if that fails, |
Denys Vlasenko | 1d77db8 | 2009-06-10 13:38:08 +0200 | [diff] [blame] | 567 | * exec external program. |
| 568 | * NB: sock is either 0 or has CLOEXEC set on it. |
| 569 | * fd has CLOEXEC set on it too. These two fds will be closed here. |
| 570 | */ |
Denys Vlasenko | a4bcbd0 | 2009-06-09 23:01:24 +0200 | [diff] [blame] | 571 | BB_EXECVP(G.loginpath, (char **)login_argv); |
Denis Vlasenko | 018b155 | 2007-11-06 01:38:46 +0000 | [diff] [blame] | 572 | /* _exit is safer with vfork, and we shouldn't send message |
Denis Vlasenko | 2450c45 | 2007-10-15 22:09:15 +0000 | [diff] [blame] | 573 | * to remote clients anyway */ |
Denys Vlasenko | a4bcbd0 | 2009-06-09 23:01:24 +0200 | [diff] [blame] | 574 | _exit(EXIT_FAILURE); /*bb_perror_msg_and_die("execv %s", G.loginpath);*/ |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 575 | } |
| 576 | |
Denis Vlasenko | 75f8d08 | 2006-11-22 15:54:52 +0000 | [diff] [blame] | 577 | #if ENABLE_FEATURE_TELNETD_STANDALONE |
| 578 | |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 579 | static void |
| 580 | free_session(struct tsession *ts) |
| 581 | { |
Denys Vlasenko | 3a41611 | 2010-04-05 22:10:38 +0200 | [diff] [blame] | 582 | struct tsession *t; |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 583 | |
Denis Vlasenko | e87b868 | 2007-10-17 14:33:31 +0000 | [diff] [blame] | 584 | if (option_mask32 & OPT_INETD) |
Denys Vlasenko | db5546c | 2022-01-05 22:16:06 +0100 | [diff] [blame] | 585 | exit_SUCCESS(); |
Denis Vlasenko | e87b868 | 2007-10-17 14:33:31 +0000 | [diff] [blame] | 586 | |
Denis Vlasenko | 59d7c43 | 2007-10-15 15:19:36 +0000 | [diff] [blame] | 587 | /* Unlink this telnet session from the session list */ |
Denys Vlasenko | 3a41611 | 2010-04-05 22:10:38 +0200 | [diff] [blame] | 588 | t = G.sessions; |
Mike Frysinger | 731f81c | 2006-05-10 15:23:12 +0000 | [diff] [blame] | 589 | if (t == ts) |
Denys Vlasenko | a4bcbd0 | 2009-06-09 23:01:24 +0200 | [diff] [blame] | 590 | G.sessions = ts->next; |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 591 | else { |
Denis Vlasenko | 75f8d08 | 2006-11-22 15:54:52 +0000 | [diff] [blame] | 592 | while (t->next != ts) |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 593 | t = t->next; |
| 594 | t->next = ts->next; |
| 595 | } |
| 596 | |
Denis Vlasenko | f472b23 | 2007-10-16 21:35:17 +0000 | [diff] [blame] | 597 | #if 0 |
Denis Vlasenko | 59d7c43 | 2007-10-15 15:19:36 +0000 | [diff] [blame] | 598 | /* It was said that "normal" telnetd just closes ptyfd, |
| 599 | * doesn't send SIGKILL. When we close ptyfd, |
| 600 | * kernel sends SIGHUP to processes having slave side opened. */ |
Denis Vlasenko | f472b23 | 2007-10-16 21:35:17 +0000 | [diff] [blame] | 601 | kill(ts->shell_pid, SIGKILL); |
Denis Vlasenko | d814c98 | 2009-02-02 23:43:57 +0000 | [diff] [blame] | 602 | waitpid(ts->shell_pid, NULL, 0); |
Denis Vlasenko | f472b23 | 2007-10-16 21:35:17 +0000 | [diff] [blame] | 603 | #endif |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 604 | close(ts->ptyfd); |
Denis Vlasenko | 75f8d08 | 2006-11-22 15:54:52 +0000 | [diff] [blame] | 605 | close(ts->sockfd_read); |
Denis Vlasenko | f472b23 | 2007-10-16 21:35:17 +0000 | [diff] [blame] | 606 | /* We do not need to close(ts->sockfd_write), it's the same |
| 607 | * as sockfd_read unless we are in inetd mode. But in inetd mode |
Denis Vlasenko | e87b868 | 2007-10-17 14:33:31 +0000 | [diff] [blame] | 608 | * we do not reach this */ |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 609 | free(ts); |
Denis Vlasenko | 75f8d08 | 2006-11-22 15:54:52 +0000 | [diff] [blame] | 610 | |
Denis Vlasenko | 59d7c43 | 2007-10-15 15:19:36 +0000 | [diff] [blame] | 611 | /* Scan all sessions and find new maxfd */ |
Denys Vlasenko | a4bcbd0 | 2009-06-09 23:01:24 +0200 | [diff] [blame] | 612 | G.maxfd = 0; |
| 613 | ts = G.sessions; |
Denis Vlasenko | 75f8d08 | 2006-11-22 15:54:52 +0000 | [diff] [blame] | 614 | while (ts) { |
Denys Vlasenko | a4bcbd0 | 2009-06-09 23:01:24 +0200 | [diff] [blame] | 615 | if (G.maxfd < ts->ptyfd) |
| 616 | G.maxfd = ts->ptyfd; |
| 617 | if (G.maxfd < ts->sockfd_read) |
| 618 | G.maxfd = ts->sockfd_read; |
Denis Vlasenko | f472b23 | 2007-10-16 21:35:17 +0000 | [diff] [blame] | 619 | #if 0 |
| 620 | /* Again, sockfd_write == sockfd_read here */ |
Denys Vlasenko | a4bcbd0 | 2009-06-09 23:01:24 +0200 | [diff] [blame] | 621 | if (G.maxfd < ts->sockfd_write) |
| 622 | G.maxfd = ts->sockfd_write; |
Denis Vlasenko | f472b23 | 2007-10-16 21:35:17 +0000 | [diff] [blame] | 623 | #endif |
Denis Vlasenko | 75f8d08 | 2006-11-22 15:54:52 +0000 | [diff] [blame] | 624 | ts = ts->next; |
| 625 | } |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 626 | } |
Denis Vlasenko | 75f8d08 | 2006-11-22 15:54:52 +0000 | [diff] [blame] | 627 | |
Denis Vlasenko | 75f8d08 | 2006-11-22 15:54:52 +0000 | [diff] [blame] | 628 | #else /* !FEATURE_TELNETD_STANDALONE */ |
| 629 | |
Bernhard Reutner-Fischer | 636a1f8 | 2008-05-19 09:29:47 +0000 | [diff] [blame] | 630 | /* Used in main() only, thus "return 0" actually is exit(EXIT_SUCCESS). */ |
Denis Vlasenko | e87b868 | 2007-10-17 14:33:31 +0000 | [diff] [blame] | 631 | #define free_session(ts) return 0 |
Denis Vlasenko | 75f8d08 | 2006-11-22 15:54:52 +0000 | [diff] [blame] | 632 | |
| 633 | #endif |
| 634 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 635 | static void handle_sigchld(int sig UNUSED_PARAM) |
Denis Vlasenko | 2450c45 | 2007-10-15 22:09:15 +0000 | [diff] [blame] | 636 | { |
| 637 | pid_t pid; |
| 638 | struct tsession *ts; |
Denys Vlasenko | 3a41611 | 2010-04-05 22:10:38 +0200 | [diff] [blame] | 639 | int save_errno = errno; |
Denis Vlasenko | 2450c45 | 2007-10-15 22:09:15 +0000 | [diff] [blame] | 640 | |
Denis Vlasenko | 018b155 | 2007-11-06 01:38:46 +0000 | [diff] [blame] | 641 | /* Looping: more than one child may have exited */ |
| 642 | while (1) { |
Denis Vlasenko | fb0eba7 | 2008-01-02 19:55:04 +0000 | [diff] [blame] | 643 | pid = wait_any_nohang(NULL); |
Denis Vlasenko | 018b155 | 2007-11-06 01:38:46 +0000 | [diff] [blame] | 644 | if (pid <= 0) |
| 645 | break; |
Denys Vlasenko | a4bcbd0 | 2009-06-09 23:01:24 +0200 | [diff] [blame] | 646 | ts = G.sessions; |
Denis Vlasenko | 2450c45 | 2007-10-15 22:09:15 +0000 | [diff] [blame] | 647 | while (ts) { |
| 648 | if (ts->shell_pid == pid) { |
| 649 | ts->shell_pid = -1; |
Denys Vlasenko | da92126 | 2015-01-05 15:37:58 +0100 | [diff] [blame] | 650 | update_utmp_DEAD_PROCESS(pid); |
Denis Vlasenko | 018b155 | 2007-11-06 01:38:46 +0000 | [diff] [blame] | 651 | break; |
Denis Vlasenko | 2450c45 | 2007-10-15 22:09:15 +0000 | [diff] [blame] | 652 | } |
| 653 | ts = ts->next; |
| 654 | } |
| 655 | } |
Denys Vlasenko | 3a41611 | 2010-04-05 22:10:38 +0200 | [diff] [blame] | 656 | |
| 657 | errno = save_errno; |
Denis Vlasenko | 2450c45 | 2007-10-15 22:09:15 +0000 | [diff] [blame] | 658 | } |
| 659 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 660 | int telnetd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 661 | int telnetd_main(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 662 | { |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 663 | fd_set rdfdset, wrfdset; |
Denis Vlasenko | 75f8d08 | 2006-11-22 15:54:52 +0000 | [diff] [blame] | 664 | unsigned opt; |
Denis Vlasenko | 10916c5 | 2007-10-15 17:28:00 +0000 | [diff] [blame] | 665 | int count; |
Denis Vlasenko | 75f8d08 | 2006-11-22 15:54:52 +0000 | [diff] [blame] | 666 | struct tsession *ts; |
| 667 | #if ENABLE_FEATURE_TELNETD_STANDALONE |
| 668 | #define IS_INETD (opt & OPT_INETD) |
Denys Vlasenko | ed1667e | 2009-09-04 02:21:13 +0200 | [diff] [blame] | 669 | int master_fd = master_fd; /* for compiler */ |
| 670 | int sec_linger = sec_linger; |
Denis Vlasenko | 75f8d08 | 2006-11-22 15:54:52 +0000 | [diff] [blame] | 671 | char *opt_bindaddr = NULL; |
| 672 | char *opt_portnbr; |
| 673 | #else |
| 674 | enum { |
| 675 | IS_INETD = 1, |
| 676 | master_fd = -1, |
Denis Vlasenko | 75f8d08 | 2006-11-22 15:54:52 +0000 | [diff] [blame] | 677 | }; |
Glenn L McGrath | c2b9186 | 2003-09-12 11:27:15 +0000 | [diff] [blame] | 678 | #endif |
Denys Vlasenko | a4bcbd0 | 2009-06-09 23:01:24 +0200 | [diff] [blame] | 679 | INIT_G(); |
| 680 | |
Denis Vlasenko | 2450c45 | 2007-10-15 22:09:15 +0000 | [diff] [blame] | 681 | /* Even if !STANDALONE, we accept (and ignore) -i, thus people |
Denis Vlasenko | 59d7c43 | 2007-10-15 15:19:36 +0000 | [diff] [blame] | 682 | * don't need to guess whether it's ok to pass -i to us */ |
Denys Vlasenko | 22542ec | 2017-08-08 21:55:02 +0200 | [diff] [blame] | 683 | opt = getopt32(argv, "^" |
| 684 | "f:l:Ki" |
Denys Vlasenko | ed1667e | 2009-09-04 02:21:13 +0200 | [diff] [blame] | 685 | IF_FEATURE_TELNETD_STANDALONE("p:b:F") |
Denys Vlasenko | 22542ec | 2017-08-08 21:55:02 +0200 | [diff] [blame] | 686 | IF_FEATURE_TELNETD_INETD_WAIT("Sw:+") /* -w NUM */ |
| 687 | "\0" |
| 688 | /* -w implies -F. -w and -i don't mix */ |
| 689 | IF_FEATURE_TELNETD_INETD_WAIT("wF:i--w:w--i"), |
Denys Vlasenko | a4bcbd0 | 2009-06-09 23:01:24 +0200 | [diff] [blame] | 690 | &G.issuefile, &G.loginpath |
Denys Vlasenko | ed1667e | 2009-09-04 02:21:13 +0200 | [diff] [blame] | 691 | IF_FEATURE_TELNETD_STANDALONE(, &opt_portnbr, &opt_bindaddr) |
| 692 | IF_FEATURE_TELNETD_INETD_WAIT(, &sec_linger) |
| 693 | ); |
Denis Vlasenko | 2450c45 | 2007-10-15 22:09:15 +0000 | [diff] [blame] | 694 | if (!IS_INETD /*&& !re_execed*/) { |
| 695 | /* inform that we start in standalone mode? |
| 696 | * May be useful when people forget to give -i */ |
| 697 | /*bb_error_msg("listening for connections");*/ |
| 698 | if (!(opt & OPT_FOREGROUND)) { |
| 699 | /* DAEMON_CHDIR_ROOT was giving inconsistent |
Denis Vlasenko | 008eda2 | 2007-10-16 10:47:27 +0000 | [diff] [blame] | 700 | * behavior with/without -F, -i */ |
Denis Vlasenko | 018b155 | 2007-11-06 01:38:46 +0000 | [diff] [blame] | 701 | bb_daemonize_or_rexec(0 /*was DAEMON_CHDIR_ROOT*/, argv); |
Denis Vlasenko | 2450c45 | 2007-10-15 22:09:15 +0000 | [diff] [blame] | 702 | } |
| 703 | } |
Denis Vlasenko | 75f8d08 | 2006-11-22 15:54:52 +0000 | [diff] [blame] | 704 | /* Redirect log to syslog early, if needed */ |
Denys Vlasenko | ed1667e | 2009-09-04 02:21:13 +0200 | [diff] [blame] | 705 | if (IS_INETD || (opt & OPT_SYSLOG) || !(opt & OPT_FOREGROUND)) { |
Denis Vlasenko | 5e4fda0 | 2009-03-08 23:46:48 +0000 | [diff] [blame] | 706 | openlog(applet_name, LOG_PID, LOG_DAEMON); |
Denis Vlasenko | 75f8d08 | 2006-11-22 15:54:52 +0000 | [diff] [blame] | 707 | logmode = LOGMODE_SYSLOG; |
| 708 | } |
Denis Vlasenko | 75f8d08 | 2006-11-22 15:54:52 +0000 | [diff] [blame] | 709 | #if ENABLE_FEATURE_TELNETD_STANDALONE |
| 710 | if (IS_INETD) { |
Denys Vlasenko | 1d77db8 | 2009-06-10 13:38:08 +0200 | [diff] [blame] | 711 | G.sessions = make_new_session(0); |
Denys Vlasenko | a4bcbd0 | 2009-06-09 23:01:24 +0200 | [diff] [blame] | 712 | if (!G.sessions) /* pty opening or vfork problem, exit */ |
Denys Vlasenko | ed1667e | 2009-09-04 02:21:13 +0200 | [diff] [blame] | 713 | return 1; /* make_new_session printed error message */ |
Denis Vlasenko | 75f8d08 | 2006-11-22 15:54:52 +0000 | [diff] [blame] | 714 | } else { |
Denys Vlasenko | ed1667e | 2009-09-04 02:21:13 +0200 | [diff] [blame] | 715 | master_fd = 0; |
| 716 | if (!(opt & OPT_WAIT)) { |
Sergey Ponomarev | 82c5eb8 | 2021-08-25 23:12:37 +0300 | [diff] [blame] | 717 | unsigned portnbr = CONFIG_FEATURE_TELNETD_PORT_DEFAULT; |
Denys Vlasenko | ed1667e | 2009-09-04 02:21:13 +0200 | [diff] [blame] | 718 | if (opt & OPT_PORT) |
| 719 | portnbr = xatou16(opt_portnbr); |
| 720 | master_fd = create_and_bind_stream_or_die(opt_bindaddr, portnbr); |
| 721 | xlisten(master_fd, 1); |
| 722 | } |
Denys Vlasenko | 1d77db8 | 2009-06-10 13:38:08 +0200 | [diff] [blame] | 723 | close_on_exec_on(master_fd); |
Denis Vlasenko | 75f8d08 | 2006-11-22 15:54:52 +0000 | [diff] [blame] | 724 | } |
Rob Landley | 00e76cb | 2005-05-10 23:53:33 +0000 | [diff] [blame] | 725 | #else |
Denys Vlasenko | a4bcbd0 | 2009-06-09 23:01:24 +0200 | [diff] [blame] | 726 | G.sessions = make_new_session(); |
| 727 | if (!G.sessions) /* pty opening or vfork problem, exit */ |
Denys Vlasenko | ed1667e | 2009-09-04 02:21:13 +0200 | [diff] [blame] | 728 | return 1; /* make_new_session printed error message */ |
Rob Landley | 00e76cb | 2005-05-10 23:53:33 +0000 | [diff] [blame] | 729 | #endif |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 730 | |
Denis Vlasenko | 75f8d08 | 2006-11-22 15:54:52 +0000 | [diff] [blame] | 731 | /* We don't want to die if just one session is broken */ |
| 732 | signal(SIGPIPE, SIG_IGN); |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 733 | |
Denis Vlasenko | 2450c45 | 2007-10-15 22:09:15 +0000 | [diff] [blame] | 734 | if (opt & OPT_WATCHCHILD) |
| 735 | signal(SIGCHLD, handle_sigchld); |
Denis Vlasenko | 018b155 | 2007-11-06 01:38:46 +0000 | [diff] [blame] | 736 | else /* prevent dead children from becoming zombies */ |
| 737 | signal(SIGCHLD, SIG_IGN); |
Denis Vlasenko | 2450c45 | 2007-10-15 22:09:15 +0000 | [diff] [blame] | 738 | |
Denis Vlasenko | 59d7c43 | 2007-10-15 15:19:36 +0000 | [diff] [blame] | 739 | /* |
Denys Vlasenko | 1d77db8 | 2009-06-10 13:38:08 +0200 | [diff] [blame] | 740 | This is how the buffers are used. The arrows indicate data flow. |
| 741 | |
Denis Vlasenko | 59d7c43 | 2007-10-15 15:19:36 +0000 | [diff] [blame] | 742 | +-------+ wridx1++ +------+ rdidx1++ +----------+ |
| 743 | | | <-------------- | buf1 | <-------------- | | |
| 744 | | | size1-- +------+ size1++ | | |
| 745 | | pty | | socket | |
| 746 | | | rdidx2++ +------+ wridx2++ | | |
| 747 | | | --------------> | buf2 | --------------> | | |
| 748 | +-------+ size2++ +------+ size2-- +----------+ |
| 749 | |
| 750 | size1: "how many bytes are buffered for pty between rdidx1 and wridx1?" |
| 751 | size2: "how many bytes are buffered for socket between rdidx2 and wridx2?" |
| 752 | |
| 753 | Each session has got two buffers. Buffers are circular. If sizeN == 0, |
| 754 | buffer is empty. If sizeN == BUFSIZE, buffer is full. In both these cases |
| 755 | rdidxN == wridxN. |
| 756 | */ |
Denis Vlasenko | 75f8d08 | 2006-11-22 15:54:52 +0000 | [diff] [blame] | 757 | again: |
| 758 | FD_ZERO(&rdfdset); |
| 759 | FD_ZERO(&wrfdset); |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 760 | |
Denis Vlasenko | 59d7c43 | 2007-10-15 15:19:36 +0000 | [diff] [blame] | 761 | /* Select on the master socket, all telnet sockets and their |
| 762 | * ptys if there is room in their session buffers. |
| 763 | * NB: scalability problem: we recalculate entire bitmap |
| 764 | * before each select. Can be a problem with 500+ connections. */ |
Denys Vlasenko | a4bcbd0 | 2009-06-09 23:01:24 +0200 | [diff] [blame] | 765 | ts = G.sessions; |
Denis Vlasenko | 75f8d08 | 2006-11-22 15:54:52 +0000 | [diff] [blame] | 766 | while (ts) { |
Denys Vlasenko | 1d77db8 | 2009-06-10 13:38:08 +0200 | [diff] [blame] | 767 | struct tsession *next = ts->next; /* in case we free ts */ |
Denis Vlasenko | 2450c45 | 2007-10-15 22:09:15 +0000 | [diff] [blame] | 768 | if (ts->shell_pid == -1) { |
Denis Vlasenko | 018b155 | 2007-11-06 01:38:46 +0000 | [diff] [blame] | 769 | /* Child died and we detected that */ |
Denis Vlasenko | 2450c45 | 2007-10-15 22:09:15 +0000 | [diff] [blame] | 770 | free_session(ts); |
| 771 | } else { |
| 772 | if (ts->size1 > 0) /* can write to pty */ |
| 773 | FD_SET(ts->ptyfd, &wrfdset); |
| 774 | if (ts->size1 < BUFSIZE) /* can read from socket */ |
| 775 | FD_SET(ts->sockfd_read, &rdfdset); |
| 776 | if (ts->size2 > 0) /* can write to socket */ |
| 777 | FD_SET(ts->sockfd_write, &wrfdset); |
| 778 | if (ts->size2 < BUFSIZE) /* can read from pty */ |
| 779 | FD_SET(ts->ptyfd, &rdfdset); |
| 780 | } |
| 781 | ts = next; |
| 782 | } |
| 783 | if (!IS_INETD) { |
| 784 | FD_SET(master_fd, &rdfdset); |
| 785 | /* This is needed because free_session() does not |
Denis Vlasenko | 018b155 | 2007-11-06 01:38:46 +0000 | [diff] [blame] | 786 | * take master_fd into account when it finds new |
Denis Vlasenko | 2450c45 | 2007-10-15 22:09:15 +0000 | [diff] [blame] | 787 | * maxfd among remaining fd's */ |
Denys Vlasenko | a4bcbd0 | 2009-06-09 23:01:24 +0200 | [diff] [blame] | 788 | if (master_fd > G.maxfd) |
| 789 | G.maxfd = master_fd; |
Denis Vlasenko | 75f8d08 | 2006-11-22 15:54:52 +0000 | [diff] [blame] | 790 | } |
| 791 | |
Denys Vlasenko | ed1667e | 2009-09-04 02:21:13 +0200 | [diff] [blame] | 792 | { |
Denys Vlasenko | ed1667e | 2009-09-04 02:21:13 +0200 | [diff] [blame] | 793 | struct timeval *tv_ptr = NULL; |
Denys Vlasenko | 36df048 | 2009-10-19 16:07:28 +0200 | [diff] [blame] | 794 | #if ENABLE_FEATURE_TELNETD_INETD_WAIT |
| 795 | struct timeval tv; |
Denys Vlasenko | ed1667e | 2009-09-04 02:21:13 +0200 | [diff] [blame] | 796 | if ((opt & OPT_WAIT) && !G.sessions) { |
| 797 | tv.tv_sec = sec_linger; |
| 798 | tv.tv_usec = 0; |
| 799 | tv_ptr = &tv; |
| 800 | } |
Denys Vlasenko | 36df048 | 2009-10-19 16:07:28 +0200 | [diff] [blame] | 801 | #endif |
Denys Vlasenko | ed1667e | 2009-09-04 02:21:13 +0200 | [diff] [blame] | 802 | count = select(G.maxfd + 1, &rdfdset, &wrfdset, NULL, tv_ptr); |
| 803 | } |
| 804 | if (count == 0) /* "telnetd -w SEC" timed out */ |
| 805 | return 0; |
Denis Vlasenko | 10916c5 | 2007-10-15 17:28:00 +0000 | [diff] [blame] | 806 | if (count < 0) |
Denis Vlasenko | 59d7c43 | 2007-10-15 15:19:36 +0000 | [diff] [blame] | 807 | goto again; /* EINTR or ENOMEM */ |
Denis Vlasenko | 75f8d08 | 2006-11-22 15:54:52 +0000 | [diff] [blame] | 808 | |
| 809 | #if ENABLE_FEATURE_TELNETD_STANDALONE |
Denys Vlasenko | 1d77db8 | 2009-06-10 13:38:08 +0200 | [diff] [blame] | 810 | /* Check for and accept new sessions */ |
Denis Vlasenko | 75f8d08 | 2006-11-22 15:54:52 +0000 | [diff] [blame] | 811 | if (!IS_INETD && FD_ISSET(master_fd, &rdfdset)) { |
Denis Vlasenko | 75f8d08 | 2006-11-22 15:54:52 +0000 | [diff] [blame] | 812 | int fd; |
Denis Vlasenko | 75f8d08 | 2006-11-22 15:54:52 +0000 | [diff] [blame] | 813 | struct tsession *new_ts; |
| 814 | |
Denis Vlasenko | 59d7c43 | 2007-10-15 15:19:36 +0000 | [diff] [blame] | 815 | fd = accept(master_fd, NULL, NULL); |
Denis Vlasenko | 75f8d08 | 2006-11-22 15:54:52 +0000 | [diff] [blame] | 816 | if (fd < 0) |
| 817 | goto again; |
Denys Vlasenko | 1d77db8 | 2009-06-10 13:38:08 +0200 | [diff] [blame] | 818 | close_on_exec_on(fd); |
| 819 | |
| 820 | /* Create a new session and link it into active list */ |
| 821 | new_ts = make_new_session(fd); |
Denis Vlasenko | 75f8d08 | 2006-11-22 15:54:52 +0000 | [diff] [blame] | 822 | if (new_ts) { |
Denys Vlasenko | a4bcbd0 | 2009-06-09 23:01:24 +0200 | [diff] [blame] | 823 | new_ts->next = G.sessions; |
| 824 | G.sessions = new_ts; |
Denis Vlasenko | 75f8d08 | 2006-11-22 15:54:52 +0000 | [diff] [blame] | 825 | } else { |
| 826 | close(fd); |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 827 | } |
Denis Vlasenko | 75f8d08 | 2006-11-22 15:54:52 +0000 | [diff] [blame] | 828 | } |
| 829 | #endif |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 830 | |
Denys Vlasenko | 1d77db8 | 2009-06-10 13:38:08 +0200 | [diff] [blame] | 831 | /* Then check for data tunneling */ |
Denys Vlasenko | a4bcbd0 | 2009-06-09 23:01:24 +0200 | [diff] [blame] | 832 | ts = G.sessions; |
Denis Vlasenko | 75f8d08 | 2006-11-22 15:54:52 +0000 | [diff] [blame] | 833 | while (ts) { /* For all sessions... */ |
Denys Vlasenko | 1d77db8 | 2009-06-10 13:38:08 +0200 | [diff] [blame] | 834 | struct tsession *next = ts->next; /* in case we free ts */ |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 835 | |
Denis Vlasenko | 59d7c43 | 2007-10-15 15:19:36 +0000 | [diff] [blame] | 836 | if (/*ts->size1 &&*/ FD_ISSET(ts->ptyfd, &wrfdset)) { |
Denys Vlasenko | 1d77db8 | 2009-06-10 13:38:08 +0200 | [diff] [blame] | 837 | /* Write to pty from buffer 1 */ |
Denys Vlasenko | 2a54b3e | 2016-10-12 14:54:10 +0200 | [diff] [blame] | 838 | count = safe_write_to_pty_decode_iac(ts); |
Denis Vlasenko | 10916c5 | 2007-10-15 17:28:00 +0000 | [diff] [blame] | 839 | if (count < 0) { |
Denis Vlasenko | 59d7c43 | 2007-10-15 15:19:36 +0000 | [diff] [blame] | 840 | if (errno == EAGAIN) |
| 841 | goto skip1; |
Denis Vlasenko | f472b23 | 2007-10-16 21:35:17 +0000 | [diff] [blame] | 842 | goto kill_session; |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 843 | } |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 844 | } |
Denis Vlasenko | 59d7c43 | 2007-10-15 15:19:36 +0000 | [diff] [blame] | 845 | skip1: |
| 846 | if (/*ts->size2 &&*/ FD_ISSET(ts->sockfd_write, &wrfdset)) { |
Denys Vlasenko | 1d77db8 | 2009-06-10 13:38:08 +0200 | [diff] [blame] | 847 | /* Write to socket from buffer 2 */ |
Denis Vlasenko | 10916c5 | 2007-10-15 17:28:00 +0000 | [diff] [blame] | 848 | count = MIN(BUFSIZE - ts->wridx2, ts->size2); |
Denys Vlasenko | 2a54b3e | 2016-10-12 14:54:10 +0200 | [diff] [blame] | 849 | count = safe_write_double_iac(ts->sockfd_write, (void*)(TS_BUF2(ts) + ts->wridx2), count); |
Denis Vlasenko | 10916c5 | 2007-10-15 17:28:00 +0000 | [diff] [blame] | 850 | if (count < 0) { |
Denis Vlasenko | 59d7c43 | 2007-10-15 15:19:36 +0000 | [diff] [blame] | 851 | if (errno == EAGAIN) |
| 852 | goto skip2; |
Denis Vlasenko | f472b23 | 2007-10-16 21:35:17 +0000 | [diff] [blame] | 853 | goto kill_session; |
Denis Vlasenko | 75f8d08 | 2006-11-22 15:54:52 +0000 | [diff] [blame] | 854 | } |
Denis Vlasenko | 10916c5 | 2007-10-15 17:28:00 +0000 | [diff] [blame] | 855 | ts->wridx2 += count; |
Denis Vlasenko | 59d7c43 | 2007-10-15 15:19:36 +0000 | [diff] [blame] | 856 | if (ts->wridx2 >= BUFSIZE) /* actually == BUFSIZE */ |
Denis Vlasenko | 75f8d08 | 2006-11-22 15:54:52 +0000 | [diff] [blame] | 857 | ts->wridx2 = 0; |
Denys Vlasenko | 2a54b3e | 2016-10-12 14:54:10 +0200 | [diff] [blame] | 858 | ts->size2 -= count; |
| 859 | if (ts->size2 == 0) { |
| 860 | ts->rdidx2 = 0; |
| 861 | ts->wridx2 = 0; |
| 862 | } |
Denis Vlasenko | 75f8d08 | 2006-11-22 15:54:52 +0000 | [diff] [blame] | 863 | } |
Denis Vlasenko | 59d7c43 | 2007-10-15 15:19:36 +0000 | [diff] [blame] | 864 | skip2: |
Denis Vlasenko | 10916c5 | 2007-10-15 17:28:00 +0000 | [diff] [blame] | 865 | |
Denis Vlasenko | 59d7c43 | 2007-10-15 15:19:36 +0000 | [diff] [blame] | 866 | if (/*ts->size1 < BUFSIZE &&*/ FD_ISSET(ts->sockfd_read, &rdfdset)) { |
Denys Vlasenko | 1d77db8 | 2009-06-10 13:38:08 +0200 | [diff] [blame] | 867 | /* Read from socket to buffer 1 */ |
Denis Vlasenko | 10916c5 | 2007-10-15 17:28:00 +0000 | [diff] [blame] | 868 | count = MIN(BUFSIZE - ts->rdidx1, BUFSIZE - ts->size1); |
Denys Vlasenko | a4bcbd0 | 2009-06-09 23:01:24 +0200 | [diff] [blame] | 869 | count = safe_read(ts->sockfd_read, TS_BUF1(ts) + ts->rdidx1, count); |
Denis Vlasenko | 10916c5 | 2007-10-15 17:28:00 +0000 | [diff] [blame] | 870 | if (count <= 0) { |
| 871 | if (count < 0 && errno == EAGAIN) |
Denis Vlasenko | 59d7c43 | 2007-10-15 15:19:36 +0000 | [diff] [blame] | 872 | goto skip3; |
Denis Vlasenko | f472b23 | 2007-10-16 21:35:17 +0000 | [diff] [blame] | 873 | goto kill_session; |
Denis Vlasenko | 59d7c43 | 2007-10-15 15:19:36 +0000 | [diff] [blame] | 874 | } |
| 875 | /* Ignore trailing NUL if it is there */ |
Denys Vlasenko | a4bcbd0 | 2009-06-09 23:01:24 +0200 | [diff] [blame] | 876 | if (!TS_BUF1(ts)[ts->rdidx1 + count - 1]) { |
Denis Vlasenko | f472b23 | 2007-10-16 21:35:17 +0000 | [diff] [blame] | 877 | --count; |
Denis Vlasenko | 59d7c43 | 2007-10-15 15:19:36 +0000 | [diff] [blame] | 878 | } |
Denis Vlasenko | 10916c5 | 2007-10-15 17:28:00 +0000 | [diff] [blame] | 879 | ts->size1 += count; |
| 880 | ts->rdidx1 += count; |
Denis Vlasenko | 59d7c43 | 2007-10-15 15:19:36 +0000 | [diff] [blame] | 881 | if (ts->rdidx1 >= BUFSIZE) /* actually == BUFSIZE */ |
| 882 | ts->rdidx1 = 0; |
| 883 | } |
| 884 | skip3: |
| 885 | if (/*ts->size2 < BUFSIZE &&*/ FD_ISSET(ts->ptyfd, &rdfdset)) { |
Denys Vlasenko | 1d77db8 | 2009-06-10 13:38:08 +0200 | [diff] [blame] | 886 | /* Read from pty to buffer 2 */ |
Denys Vlasenko | 39b1819 | 2019-01-06 19:06:01 +0100 | [diff] [blame] | 887 | int eio = 0; |
| 888 | read_pty: |
Denis Vlasenko | 10916c5 | 2007-10-15 17:28:00 +0000 | [diff] [blame] | 889 | count = MIN(BUFSIZE - ts->rdidx2, BUFSIZE - ts->size2); |
Denys Vlasenko | a4bcbd0 | 2009-06-09 23:01:24 +0200 | [diff] [blame] | 890 | count = safe_read(ts->ptyfd, TS_BUF2(ts) + ts->rdidx2, count); |
Denis Vlasenko | 10916c5 | 2007-10-15 17:28:00 +0000 | [diff] [blame] | 891 | if (count <= 0) { |
Denys Vlasenko | 39b1819 | 2019-01-06 19:06:01 +0100 | [diff] [blame] | 892 | if (count < 0) { |
| 893 | if (errno == EAGAIN) |
| 894 | goto skip4; |
| 895 | /* login process might call vhangup(), |
| 896 | * which causes intermittent EIOs on read above |
| 897 | * (observed on kernel 4.12.0). Try up to 10 ms. |
| 898 | */ |
| 899 | if (errno == EIO && eio < 10) { |
| 900 | eio++; |
| 901 | //bb_error_msg("EIO pty %u", eio); |
| 902 | usleep(1000); |
| 903 | goto read_pty; |
| 904 | } |
| 905 | } |
Denis Vlasenko | f472b23 | 2007-10-16 21:35:17 +0000 | [diff] [blame] | 906 | goto kill_session; |
Denis Vlasenko | 59d7c43 | 2007-10-15 15:19:36 +0000 | [diff] [blame] | 907 | } |
Denis Vlasenko | 10916c5 | 2007-10-15 17:28:00 +0000 | [diff] [blame] | 908 | ts->size2 += count; |
| 909 | ts->rdidx2 += count; |
Denis Vlasenko | 59d7c43 | 2007-10-15 15:19:36 +0000 | [diff] [blame] | 910 | if (ts->rdidx2 >= BUFSIZE) /* actually == BUFSIZE */ |
| 911 | ts->rdidx2 = 0; |
| 912 | } |
| 913 | skip4: |
Denis Vlasenko | 75f8d08 | 2006-11-22 15:54:52 +0000 | [diff] [blame] | 914 | ts = next; |
Denis Vlasenko | f472b23 | 2007-10-16 21:35:17 +0000 | [diff] [blame] | 915 | continue; |
| 916 | kill_session: |
Denys Vlasenko | 3a41611 | 2010-04-05 22:10:38 +0200 | [diff] [blame] | 917 | if (ts->shell_pid > 0) |
Denys Vlasenko | da92126 | 2015-01-05 15:37:58 +0100 | [diff] [blame] | 918 | update_utmp_DEAD_PROCESS(ts->shell_pid); |
Denis Vlasenko | f472b23 | 2007-10-16 21:35:17 +0000 | [diff] [blame] | 919 | free_session(ts); |
| 920 | ts = next; |
Denis Vlasenko | 75f8d08 | 2006-11-22 15:54:52 +0000 | [diff] [blame] | 921 | } |
Denis Vlasenko | f472b23 | 2007-10-16 21:35:17 +0000 | [diff] [blame] | 922 | |
Denis Vlasenko | 75f8d08 | 2006-11-22 15:54:52 +0000 | [diff] [blame] | 923 | goto again; |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 924 | } |