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