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