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