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 | * |
Rob Landley | 1ec5b29 | 2006-05-29 07:42:02 +0000 | [diff] [blame] | 6 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
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 | * |
| 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. |
| 19 | * |
| 20 | * Vladimir Oleynik <dzo@simtreas.ru> 2001 |
| 21 | * Set process group corrections, initial busybox port |
| 22 | */ |
| 23 | |
| 24 | /*#define DEBUG 1 */ |
Denis Vlasenko | 75f8d08 | 2006-11-22 15:54:52 +0000 | [diff] [blame] | 25 | #define DEBUG 0 |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 26 | |
Rob Landley | 099ed50 | 2006-08-28 09:41:49 +0000 | [diff] [blame] | 27 | #include "busybox.h" |
| 28 | |
Denis Vlasenko | 75f8d08 | 2006-11-22 15:54:52 +0000 | [diff] [blame] | 29 | #if DEBUG |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 30 | #define TELCMDS |
| 31 | #define TELOPTS |
| 32 | #endif |
| 33 | #include <arpa/telnet.h> |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 34 | #include <sys/syslog.h> |
| 35 | |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 36 | |
| 37 | #define BUFSIZE 4000 |
| 38 | |
Denis Vlasenko | 75f8d08 | 2006-11-22 15:54:52 +0000 | [diff] [blame] | 39 | #if ENABLE_FEATURE_IPV6 |
Rob Landley | 00e76cb | 2005-05-10 23:53:33 +0000 | [diff] [blame] | 40 | typedef struct sockaddr_in6 sockaddr_type; |
| 41 | #else |
Rob Landley | 00e76cb | 2005-05-10 23:53:33 +0000 | [diff] [blame] | 42 | typedef struct sockaddr_in sockaddr_type; |
| 43 | #endif |
| 44 | |
Denis Vlasenko | 75f8d08 | 2006-11-22 15:54:52 +0000 | [diff] [blame] | 45 | #if ENABLE_LOGIN |
Glenn L McGrath | d4004ee | 2004-09-14 17:24:59 +0000 | [diff] [blame] | 46 | static const char *loginpath = "/bin/login"; |
Glenn L McGrath | 9e5d6c0 | 2003-01-21 20:55:56 +0000 | [diff] [blame] | 47 | #else |
Denis Vlasenko | 75f8d08 | 2006-11-22 15:54:52 +0000 | [diff] [blame] | 48 | static const char *loginpath = DEFAULT_SHELL; |
Glenn L McGrath | 9e5d6c0 | 2003-01-21 20:55:56 +0000 | [diff] [blame] | 49 | #endif |
Denis Vlasenko | 75f8d08 | 2006-11-22 15:54:52 +0000 | [diff] [blame] | 50 | |
Glenn L McGrath | 9e5d6c0 | 2003-01-21 20:55:56 +0000 | [diff] [blame] | 51 | static const char *issuefile = "/etc/issue.net"; |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 52 | |
| 53 | /* shell name and arguments */ |
| 54 | |
Denis Vlasenko | 75f8d08 | 2006-11-22 15:54:52 +0000 | [diff] [blame] | 55 | static const char *argv_init[2]; |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 56 | |
| 57 | /* structure that describes a session */ |
| 58 | |
| 59 | struct tsession { |
| 60 | struct tsession *next; |
Denis Vlasenko | 75f8d08 | 2006-11-22 15:54:52 +0000 | [diff] [blame] | 61 | int sockfd_read, sockfd_write, ptyfd; |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 62 | int shell_pid; |
| 63 | /* two circular buffers */ |
| 64 | char *buf1, *buf2; |
| 65 | int rdidx1, wridx1, size1; |
| 66 | int rdidx2, wridx2, size2; |
| 67 | }; |
| 68 | |
| 69 | /* |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 70 | This is how the buffers are used. The arrows indicate the movement |
| 71 | of data. |
| 72 | |
| 73 | +-------+ wridx1++ +------+ rdidx1++ +----------+ |
| 74 | | | <-------------- | buf1 | <-------------- | | |
| 75 | | | size1-- +------+ size1++ | | |
Mike Frysinger | d2c8fd6 | 2006-05-10 17:17:09 +0000 | [diff] [blame] | 76 | | pty | | socket | |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 77 | | | rdidx2++ +------+ wridx2++ | | |
| 78 | | | --------------> | buf2 | --------------> | | |
| 79 | +-------+ size2++ +------+ size2-- +----------+ |
| 80 | |
| 81 | Each session has got two buffers. |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 82 | */ |
| 83 | |
| 84 | static int maxfd; |
| 85 | |
| 86 | static struct tsession *sessions; |
| 87 | |
| 88 | |
| 89 | /* |
Bernhard Reutner-Fischer | e0387a6 | 2006-06-07 13:31:59 +0000 | [diff] [blame] | 90 | Remove all IAC's from the buffer pointed to by bf (received IACs are ignored |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 91 | and must be removed so as to not be interpreted by the terminal). Make an |
| 92 | uninterrupted string of characters fit for the terminal. Do this by packing |
| 93 | all characters meant for the terminal sequentially towards the end of bf. |
| 94 | |
| 95 | Return a pointer to the beginning of the characters meant for the terminal. |
| 96 | and make *num_totty the number of characters that should be sent to |
| 97 | the terminal. |
| 98 | |
| 99 | Note - If an IAC (3 byte quantity) starts before (bf + len) but extends |
| 100 | past (bf + len) then that IAC will be left unprocessed and *processed will be |
| 101 | less than len. |
| 102 | |
| 103 | FIXME - if we mean to send 0xFF to the terminal then it will be escaped, |
| 104 | what is the escape character? We aren't handling that situation here. |
| 105 | |
Eric Andersen | 3752d33 | 2003-12-19 11:30:13 +0000 | [diff] [blame] | 106 | CR-LF ->'s CR mapping is also done here, for convenience |
Denis Vlasenko | 75f8d08 | 2006-11-22 15:54:52 +0000 | [diff] [blame] | 107 | */ |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 108 | static char * |
Denis Vlasenko | 75f8d08 | 2006-11-22 15:54:52 +0000 | [diff] [blame] | 109 | remove_iacs(struct tsession *ts, int *pnum_totty) |
| 110 | { |
Eric Andersen | 0cb6f35 | 2006-01-30 22:30:41 +0000 | [diff] [blame] | 111 | unsigned char *ptr0 = (unsigned char *)ts->buf1 + ts->wridx1; |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 112 | unsigned char *ptr = ptr0; |
| 113 | unsigned char *totty = ptr; |
| 114 | unsigned char *end = ptr + MIN(BUFSIZE - ts->wridx1, ts->size1); |
| 115 | int processed; |
| 116 | int num_totty; |
| 117 | |
| 118 | while (ptr < end) { |
| 119 | if (*ptr != IAC) { |
Eric Andersen | 3752d33 | 2003-12-19 11:30:13 +0000 | [diff] [blame] | 120 | int c = *ptr; |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 121 | *totty++ = *ptr++; |
Eric Andersen | 3752d33 | 2003-12-19 11:30:13 +0000 | [diff] [blame] | 122 | /* We now map \r\n ==> \r for pragmatic reasons. |
| 123 | * Many client implementations send \r\n when |
| 124 | * the user hits the CarriageReturn key. |
| 125 | */ |
| 126 | if (c == '\r' && (*ptr == '\n' || *ptr == 0) && ptr < end) |
| 127 | ptr++; |
Denis Vlasenko | 75f8d08 | 2006-11-22 15:54:52 +0000 | [diff] [blame] | 128 | } else { |
Glenn L McGrath | 90ed9a0 | 2004-02-22 09:45:57 +0000 | [diff] [blame] | 129 | /* |
| 130 | * TELOPT_NAWS support! |
| 131 | */ |
| 132 | if ((ptr+2) >= end) { |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 133 | /* only the beginning of the IAC is in the |
| 134 | buffer we were asked to process, we can't |
| 135 | process this char. */ |
| 136 | break; |
| 137 | } |
Glenn L McGrath | 90ed9a0 | 2004-02-22 09:45:57 +0000 | [diff] [blame] | 138 | |
| 139 | /* |
| 140 | * IAC -> SB -> TELOPT_NAWS -> 4-byte -> IAC -> SE |
| 141 | */ |
| 142 | else if (ptr[1] == SB && ptr[2] == TELOPT_NAWS) { |
| 143 | struct winsize ws; |
| 144 | if ((ptr+8) >= end) |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 145 | break; /* incomplete, can't process */ |
Glenn L McGrath | 90ed9a0 | 2004-02-22 09:45:57 +0000 | [diff] [blame] | 146 | ws.ws_col = (ptr[3] << 8) | ptr[4]; |
| 147 | ws.ws_row = (ptr[5] << 8) | ptr[6]; |
Denis Vlasenko | 75f8d08 | 2006-11-22 15:54:52 +0000 | [diff] [blame] | 148 | ioctl(ts->ptyfd, TIOCSWINSZ, (char *)&ws); |
Glenn L McGrath | 90ed9a0 | 2004-02-22 09:45:57 +0000 | [diff] [blame] | 149 | ptr += 9; |
Denis Vlasenko | 75f8d08 | 2006-11-22 15:54:52 +0000 | [diff] [blame] | 150 | } else { |
Glenn L McGrath | 90ed9a0 | 2004-02-22 09:45:57 +0000 | [diff] [blame] | 151 | /* skip 3-byte IAC non-SB cmd */ |
Denis Vlasenko | 75f8d08 | 2006-11-22 15:54:52 +0000 | [diff] [blame] | 152 | #if DEBUG |
Glenn L McGrath | 90ed9a0 | 2004-02-22 09:45:57 +0000 | [diff] [blame] | 153 | fprintf(stderr, "Ignoring IAC %s,%s\n", |
Denis Vlasenko | 75f8d08 | 2006-11-22 15:54:52 +0000 | [diff] [blame] | 154 | TELCMD(ptr[1]), TELOPT(ptr[2])); |
Glenn L McGrath | 90ed9a0 | 2004-02-22 09:45:57 +0000 | [diff] [blame] | 155 | #endif |
| 156 | ptr += 3; |
| 157 | } |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 158 | } |
| 159 | } |
| 160 | |
| 161 | processed = ptr - ptr0; |
| 162 | num_totty = totty - ptr0; |
| 163 | /* the difference between processed and num_to tty |
| 164 | is all the iacs we removed from the stream. |
| 165 | Adjust buf1 accordingly. */ |
| 166 | ts->wridx1 += processed - num_totty; |
| 167 | ts->size1 -= processed - num_totty; |
| 168 | *pnum_totty = num_totty; |
| 169 | /* move the chars meant for the terminal towards the end of the |
| 170 | buffer. */ |
| 171 | return memmove(ptr - num_totty, ptr0, num_totty); |
| 172 | } |
| 173 | |
| 174 | |
| 175 | static int |
Denis Vlasenko | 75f8d08 | 2006-11-22 15:54:52 +0000 | [diff] [blame] | 176 | getpty(char *line, int size) |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 177 | { |
| 178 | int p; |
Denis Vlasenko | 75f8d08 | 2006-11-22 15:54:52 +0000 | [diff] [blame] | 179 | #if ENABLE_FEATURE_DEVPTS |
Denis Vlasenko | c6ec8c9 | 2006-10-15 18:22:05 +0000 | [diff] [blame] | 180 | p = open("/dev/ptmx", O_RDWR); |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 181 | if (p > 0) { |
Denis Vlasenko | c6ec8c9 | 2006-10-15 18:22:05 +0000 | [diff] [blame] | 182 | const char *name; |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 183 | grantpt(p); |
| 184 | unlockpt(p); |
Denis Vlasenko | c6ec8c9 | 2006-10-15 18:22:05 +0000 | [diff] [blame] | 185 | name = ptsname(p); |
| 186 | if (!name) { |
| 187 | bb_perror_msg("ptsname error (is /dev/pts mounted?)"); |
| 188 | return -1; |
| 189 | } |
Denis Vlasenko | 75f8d08 | 2006-11-22 15:54:52 +0000 | [diff] [blame] | 190 | safe_strncpy(line, name, size); |
Denis Vlasenko | c6ec8c9 | 2006-10-15 18:22:05 +0000 | [diff] [blame] | 191 | return p; |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 192 | } |
| 193 | #else |
| 194 | struct stat stb; |
| 195 | int i; |
| 196 | int j; |
| 197 | |
| 198 | strcpy(line, "/dev/ptyXX"); |
| 199 | |
| 200 | for (i = 0; i < 16; i++) { |
| 201 | line[8] = "pqrstuvwxyzabcde"[i]; |
| 202 | line[9] = '0'; |
| 203 | if (stat(line, &stb) < 0) { |
| 204 | continue; |
| 205 | } |
| 206 | for (j = 0; j < 16; j++) { |
| 207 | line[9] = j < 10 ? j + '0' : j - 10 + 'a'; |
Denis Vlasenko | 75f8d08 | 2006-11-22 15:54:52 +0000 | [diff] [blame] | 208 | if (DEBUG) |
| 209 | fprintf(stderr, "Trying to open device: %s\n", line); |
Denis Vlasenko | c6ec8c9 | 2006-10-15 18:22:05 +0000 | [diff] [blame] | 210 | p = open(line, O_RDWR | O_NOCTTY); |
| 211 | if (p >= 0) { |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 212 | line[5] = 't'; |
| 213 | return p; |
| 214 | } |
| 215 | } |
| 216 | } |
Denis Vlasenko | 75f8d08 | 2006-11-22 15:54:52 +0000 | [diff] [blame] | 217 | #endif /* FEATURE_DEVPTS */ |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 218 | return -1; |
| 219 | } |
| 220 | |
| 221 | |
| 222 | static void |
| 223 | send_iac(struct tsession *ts, unsigned char command, int option) |
| 224 | { |
Denis Vlasenko | 75f8d08 | 2006-11-22 15:54:52 +0000 | [diff] [blame] | 225 | /* We rely on that there is space in the buffer for now. */ |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 226 | char *b = ts->buf2 + ts->rdidx2; |
| 227 | *b++ = IAC; |
| 228 | *b++ = command; |
| 229 | *b++ = option; |
| 230 | ts->rdidx2 += 3; |
| 231 | ts->size2 += 3; |
| 232 | } |
| 233 | |
| 234 | |
| 235 | static struct tsession * |
Denis Vlasenko | 75f8d08 | 2006-11-22 15:54:52 +0000 | [diff] [blame] | 236 | make_new_session( |
| 237 | USE_FEATURE_TELNETD_STANDALONE(int sock_r, int sock_w) |
| 238 | SKIP_FEATURE_TELNETD_STANDALONE(void) |
| 239 | ) { |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 240 | struct termios termbuf; |
Denis Vlasenko | 75f8d08 | 2006-11-22 15:54:52 +0000 | [diff] [blame] | 241 | int fd, pid; |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 242 | char tty_name[32]; |
Rob Landley | 1ec5b29 | 2006-05-29 07:42:02 +0000 | [diff] [blame] | 243 | struct tsession *ts = xzalloc(sizeof(struct tsession) + BUFSIZE * 2); |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 244 | |
| 245 | ts->buf1 = (char *)(&ts[1]); |
| 246 | ts->buf2 = ts->buf1 + BUFSIZE; |
| 247 | |
Denis Vlasenko | 75f8d08 | 2006-11-22 15:54:52 +0000 | [diff] [blame] | 248 | /* Got a new connection, set up a tty. */ |
| 249 | fd = getpty(tty_name, 32); |
| 250 | if (fd < 0) { |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 251 | bb_error_msg("all terminals in use"); |
Denis Vlasenko | 75f8d08 | 2006-11-22 15:54:52 +0000 | [diff] [blame] | 252 | return NULL; |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 253 | } |
Denis Vlasenko | 75f8d08 | 2006-11-22 15:54:52 +0000 | [diff] [blame] | 254 | if (fd > maxfd) maxfd = fd; |
| 255 | ndelay_on(ts->ptyfd = fd); |
| 256 | #if ENABLE_FEATURE_TELNETD_STANDALONE |
| 257 | if (sock_w > maxfd) maxfd = sock_w; |
| 258 | if (sock_r > maxfd) maxfd = sock_r; |
| 259 | ndelay_on(ts->sockfd_write = sock_w); |
| 260 | ndelay_on(ts->sockfd_read = sock_r); |
| 261 | #else |
| 262 | ts->sockfd_write = 1; |
| 263 | /* xzalloc: ts->sockfd_read = 0; */ |
| 264 | ndelay_on(0); |
| 265 | ndelay_on(1); |
| 266 | #endif |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 267 | /* Make the telnet client understand we will echo characters so it |
| 268 | * should not do it locally. We don't tell the client to run linemode, |
| 269 | * because we want to handle line editing and tab completion and other |
Denis Vlasenko | 75f8d08 | 2006-11-22 15:54:52 +0000 | [diff] [blame] | 270 | * stuff that requires char-by-char support. */ |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 271 | send_iac(ts, DO, TELOPT_ECHO); |
Glenn L McGrath | 90ed9a0 | 2004-02-22 09:45:57 +0000 | [diff] [blame] | 272 | send_iac(ts, DO, TELOPT_NAWS); |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 273 | send_iac(ts, DO, TELOPT_LFLOW); |
| 274 | send_iac(ts, WILL, TELOPT_ECHO); |
| 275 | send_iac(ts, WILL, TELOPT_SGA); |
| 276 | |
Denis Vlasenko | 75f8d08 | 2006-11-22 15:54:52 +0000 | [diff] [blame] | 277 | pid = fork(); |
| 278 | if (pid < 0) { |
| 279 | free(ts); |
| 280 | close(fd); |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 281 | bb_perror_msg("fork"); |
Denis Vlasenko | 75f8d08 | 2006-11-22 15:54:52 +0000 | [diff] [blame] | 282 | return NULL; |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 283 | } |
Denis Vlasenko | 75f8d08 | 2006-11-22 15:54:52 +0000 | [diff] [blame] | 284 | if (pid > 0) { |
| 285 | /* parent */ |
| 286 | ts->shell_pid = pid; |
| 287 | return ts; |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 288 | } |
Denis Vlasenko | 75f8d08 | 2006-11-22 15:54:52 +0000 | [diff] [blame] | 289 | |
| 290 | /* child */ |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 291 | |
Denis Vlasenko | 75f8d08 | 2006-11-22 15:54:52 +0000 | [diff] [blame] | 292 | /* open the child's side of the tty. */ |
| 293 | fd = xopen(tty_name, O_RDWR /*| O_NOCTTY*/); |
| 294 | dup2(fd, 0); |
| 295 | dup2(fd, 1); |
| 296 | dup2(fd, 2); |
| 297 | while (fd > 2) close(fd--); |
| 298 | /* make new process group */ |
| 299 | setsid(); |
| 300 | tcsetpgrp(0, getpid()); |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 301 | |
Denis Vlasenko | 75f8d08 | 2006-11-22 15:54:52 +0000 | [diff] [blame] | 302 | /* The pseudo-terminal allocated to the client is configured to operate in |
| 303 | * cooked mode, and with XTABS CRMOD enabled (see tty(4)). */ |
| 304 | tcgetattr(0, &termbuf); |
| 305 | termbuf.c_lflag |= ECHO; /* if we use readline we dont want this */ |
| 306 | termbuf.c_oflag |= ONLCR|XTABS; |
| 307 | termbuf.c_iflag |= ICRNL; |
| 308 | termbuf.c_iflag &= ~IXOFF; |
| 309 | /*termbuf.c_lflag &= ~ICANON;*/ |
| 310 | tcsetattr(0, TCSANOW, &termbuf); |
| 311 | |
| 312 | print_login_issue(issuefile, NULL); |
| 313 | |
| 314 | /* exec shell, with correct argv and env */ |
| 315 | execv(loginpath, (char *const *)argv_init); |
| 316 | bb_perror_msg_and_die("execv"); |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 317 | } |
| 318 | |
Denis Vlasenko | 75f8d08 | 2006-11-22 15:54:52 +0000 | [diff] [blame] | 319 | #if ENABLE_FEATURE_TELNETD_STANDALONE |
| 320 | |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 321 | static void |
| 322 | free_session(struct tsession *ts) |
| 323 | { |
| 324 | struct tsession *t = sessions; |
| 325 | |
Denis Vlasenko | 75f8d08 | 2006-11-22 15:54:52 +0000 | [diff] [blame] | 326 | /* unlink this telnet session from the session list */ |
Mike Frysinger | 731f81c | 2006-05-10 15:23:12 +0000 | [diff] [blame] | 327 | if (t == ts) |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 328 | sessions = ts->next; |
| 329 | else { |
Denis Vlasenko | 75f8d08 | 2006-11-22 15:54:52 +0000 | [diff] [blame] | 330 | while (t->next != ts) |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 331 | t = t->next; |
| 332 | t->next = ts->next; |
| 333 | } |
| 334 | |
| 335 | kill(ts->shell_pid, SIGKILL); |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 336 | wait4(ts->shell_pid, NULL, 0, NULL); |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 337 | close(ts->ptyfd); |
Denis Vlasenko | 75f8d08 | 2006-11-22 15:54:52 +0000 | [diff] [blame] | 338 | close(ts->sockfd_read); |
| 339 | /* error if ts->sockfd_read == ts->sockfd_write. So what? ;) */ |
| 340 | close(ts->sockfd_write); |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 341 | free(ts); |
Denis Vlasenko | 75f8d08 | 2006-11-22 15:54:52 +0000 | [diff] [blame] | 342 | |
| 343 | /* scan all sessions and find new maxfd */ |
| 344 | ts = sessions; |
| 345 | maxfd = 0; |
| 346 | while (ts) { |
| 347 | if (maxfd < ts->ptyfd) |
| 348 | maxfd = ts->ptyfd; |
| 349 | if (maxfd < ts->sockfd_read) |
| 350 | maxfd = ts->sockfd_read; |
| 351 | if (maxfd < ts->sockfd_write) |
| 352 | maxfd = ts->sockfd_write; |
| 353 | ts = ts->next; |
| 354 | } |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 355 | } |
Denis Vlasenko | 75f8d08 | 2006-11-22 15:54:52 +0000 | [diff] [blame] | 356 | |
Denis Vlasenko | 75f8d08 | 2006-11-22 15:54:52 +0000 | [diff] [blame] | 357 | #else /* !FEATURE_TELNETD_STANDALONE */ |
| 358 | |
| 359 | /* Never actually called */ |
| 360 | void free_session(struct tsession *ts); |
Denis Vlasenko | 75f8d08 | 2006-11-22 15:54:52 +0000 | [diff] [blame] | 361 | |
| 362 | #endif |
| 363 | |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 364 | |
| 365 | int |
| 366 | telnetd_main(int argc, char **argv) |
| 367 | { |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 368 | fd_set rdfdset, wrfdset; |
Denis Vlasenko | 75f8d08 | 2006-11-22 15:54:52 +0000 | [diff] [blame] | 369 | unsigned opt; |
| 370 | int selret, maxlen, w, r; |
| 371 | struct tsession *ts; |
| 372 | #if ENABLE_FEATURE_TELNETD_STANDALONE |
| 373 | #define IS_INETD (opt & OPT_INETD) |
| 374 | int master_fd = -1; /* be happy, gcc */ |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 375 | unsigned portnbr = 23; |
Denis Vlasenko | 75f8d08 | 2006-11-22 15:54:52 +0000 | [diff] [blame] | 376 | char *opt_bindaddr = NULL; |
| 377 | char *opt_portnbr; |
| 378 | #else |
| 379 | enum { |
| 380 | IS_INETD = 1, |
| 381 | master_fd = -1, |
| 382 | portnbr = 23, |
| 383 | }; |
Glenn L McGrath | c2b9186 | 2003-09-12 11:27:15 +0000 | [diff] [blame] | 384 | #endif |
Denis Vlasenko | 75f8d08 | 2006-11-22 15:54:52 +0000 | [diff] [blame] | 385 | enum { |
| 386 | OPT_PORT = 4 * ENABLE_FEATURE_TELNETD_STANDALONE, |
| 387 | OPT_FOREGROUND = 0x10 * ENABLE_FEATURE_TELNETD_STANDALONE, |
| 388 | OPT_INETD = 0x20 * ENABLE_FEATURE_TELNETD_STANDALONE, |
| 389 | }; |
Glenn L McGrath | c2b9186 | 2003-09-12 11:27:15 +0000 | [diff] [blame] | 390 | |
Denis Vlasenko | 75f8d08 | 2006-11-22 15:54:52 +0000 | [diff] [blame] | 391 | opt = getopt32(argc, argv, "f:l:" USE_FEATURE_TELNETD_STANDALONE("p:b:Fi"), |
Denis Vlasenko | 0e87d34 | 2006-09-22 08:50:29 +0000 | [diff] [blame] | 392 | &issuefile, &loginpath |
Denis Vlasenko | 75f8d08 | 2006-11-22 15:54:52 +0000 | [diff] [blame] | 393 | USE_FEATURE_TELNETD_STANDALONE(, &opt_portnbr, &opt_bindaddr)); |
| 394 | /* Redirect log to syslog early, if needed */ |
| 395 | if (IS_INETD || !(opt & OPT_FOREGROUND)) { |
| 396 | openlog(applet_name, 0, LOG_USER); |
| 397 | logmode = LOGMODE_SYSLOG; |
| 398 | } |
Denis Vlasenko | 0e87d34 | 2006-09-22 08:50:29 +0000 | [diff] [blame] | 399 | //if (opt & 1) // -f |
| 400 | //if (opt & 2) // -l |
Denis Vlasenko | 75f8d08 | 2006-11-22 15:54:52 +0000 | [diff] [blame] | 401 | USE_FEATURE_TELNETD_STANDALONE( |
| 402 | if (opt & OPT_PORT) // -p |
| 403 | portnbr = xatou16(opt_portnbr); |
| 404 | //if (opt & 8) // -b |
| 405 | //if (opt & 0x10) // -F |
| 406 | //if (opt & 0x20) // -i |
| 407 | ); |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 408 | |
Denis Vlasenko | 75f8d08 | 2006-11-22 15:54:52 +0000 | [diff] [blame] | 409 | /* Used to check access(loginpath, X_OK) here. Pointless. |
| 410 | * exec will do this for us for free later. */ |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 411 | argv_init[0] = loginpath; |
Glenn L McGrath | 9e5d6c0 | 2003-01-21 20:55:56 +0000 | [diff] [blame] | 412 | |
Denis Vlasenko | 75f8d08 | 2006-11-22 15:54:52 +0000 | [diff] [blame] | 413 | #if ENABLE_FEATURE_TELNETD_STANDALONE |
| 414 | if (IS_INETD) { |
| 415 | sessions = make_new_session(0, 1); |
| 416 | } else { |
Denis Vlasenko | c8717cd | 2006-11-22 16:10:39 +0000 | [diff] [blame] | 417 | master_fd = create_and_bind_socket_ip4or6(opt_bindaddr, portnbr); |
| 418 | xlisten(master_fd, 1); |
Denis Vlasenko | 75f8d08 | 2006-11-22 15:54:52 +0000 | [diff] [blame] | 419 | if (!(opt & OPT_FOREGROUND)) |
| 420 | xdaemon(0, 0); |
| 421 | } |
Rob Landley | 00e76cb | 2005-05-10 23:53:33 +0000 | [diff] [blame] | 422 | #else |
Denis Vlasenko | 75f8d08 | 2006-11-22 15:54:52 +0000 | [diff] [blame] | 423 | sessions = make_new_session(); |
Rob Landley | 00e76cb | 2005-05-10 23:53:33 +0000 | [diff] [blame] | 424 | #endif |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 425 | |
Denis Vlasenko | 75f8d08 | 2006-11-22 15:54:52 +0000 | [diff] [blame] | 426 | /* We don't want to die if just one session is broken */ |
| 427 | signal(SIGPIPE, SIG_IGN); |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 428 | |
Denis Vlasenko | 75f8d08 | 2006-11-22 15:54:52 +0000 | [diff] [blame] | 429 | again: |
| 430 | FD_ZERO(&rdfdset); |
| 431 | FD_ZERO(&wrfdset); |
| 432 | if (!IS_INETD) { |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 433 | FD_SET(master_fd, &rdfdset); |
Denis Vlasenko | 75f8d08 | 2006-11-22 15:54:52 +0000 | [diff] [blame] | 434 | /* This is needed because free_session() does not |
| 435 | * take into account master_fd when it finds new |
| 436 | * maxfd among remaining fd's: */ |
| 437 | if (master_fd > maxfd) |
| 438 | maxfd = master_fd; |
| 439 | } |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 440 | |
Denis Vlasenko | 75f8d08 | 2006-11-22 15:54:52 +0000 | [diff] [blame] | 441 | /* select on the master socket, all telnet sockets and their |
| 442 | * ptys if there is room in their session buffers. */ |
| 443 | ts = sessions; |
| 444 | while (ts) { |
| 445 | /* buf1 is used from socket to pty |
| 446 | * buf2 is used from pty to socket */ |
| 447 | if (ts->size1 > 0) /* can write to pty */ |
| 448 | FD_SET(ts->ptyfd, &wrfdset); |
| 449 | if (ts->size1 < BUFSIZE) /* can read from socket */ |
| 450 | FD_SET(ts->sockfd_read, &rdfdset); |
| 451 | if (ts->size2 > 0) /* can write to socket */ |
| 452 | FD_SET(ts->sockfd_write, &wrfdset); |
| 453 | if (ts->size2 < BUFSIZE) /* can read from pty */ |
| 454 | FD_SET(ts->ptyfd, &rdfdset); |
| 455 | ts = ts->next; |
| 456 | } |
| 457 | |
| 458 | selret = select(maxfd + 1, &rdfdset, &wrfdset, 0, 0); |
| 459 | if (!selret) |
| 460 | return 0; |
| 461 | |
| 462 | #if ENABLE_FEATURE_TELNETD_STANDALONE |
| 463 | /* First check for and accept new sessions. */ |
| 464 | if (!IS_INETD && FD_ISSET(master_fd, &rdfdset)) { |
| 465 | sockaddr_type sa; |
| 466 | int fd; |
| 467 | socklen_t salen; |
| 468 | struct tsession *new_ts; |
| 469 | |
| 470 | salen = sizeof(sa); |
| 471 | fd = accept(master_fd, (struct sockaddr *)&sa, &salen); |
| 472 | if (fd < 0) |
| 473 | goto again; |
| 474 | /* Create a new session and link it into our active list */ |
| 475 | new_ts = make_new_session(fd, fd); |
| 476 | if (new_ts) { |
| 477 | new_ts->next = sessions; |
| 478 | sessions = new_ts; |
| 479 | } else { |
| 480 | close(fd); |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 481 | } |
Denis Vlasenko | 75f8d08 | 2006-11-22 15:54:52 +0000 | [diff] [blame] | 482 | } |
| 483 | #endif |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 484 | |
Denis Vlasenko | 75f8d08 | 2006-11-22 15:54:52 +0000 | [diff] [blame] | 485 | /* Then check for data tunneling. */ |
| 486 | ts = sessions; |
| 487 | while (ts) { /* For all sessions... */ |
| 488 | struct tsession *next = ts->next; /* in case we free ts. */ |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 489 | |
Denis Vlasenko | 75f8d08 | 2006-11-22 15:54:52 +0000 | [diff] [blame] | 490 | if (ts->size1 && FD_ISSET(ts->ptyfd, &wrfdset)) { |
| 491 | int num_totty; |
| 492 | char *ptr; |
| 493 | /* Write to pty from buffer 1. */ |
| 494 | ptr = remove_iacs(ts, &num_totty); |
| 495 | w = safe_write(ts->ptyfd, ptr, num_totty); |
| 496 | /* needed? if (w < 0 && errno == EAGAIN) continue; */ |
| 497 | if (w < 0) { |
| 498 | if (IS_INETD) |
| 499 | return 0; |
| 500 | free_session(ts); |
| 501 | ts = next; |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 502 | continue; |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 503 | } |
Denis Vlasenko | 75f8d08 | 2006-11-22 15:54:52 +0000 | [diff] [blame] | 504 | ts->wridx1 += w; |
| 505 | ts->size1 -= w; |
| 506 | if (ts->wridx1 == BUFSIZE) |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 507 | ts->wridx1 = 0; |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 508 | } |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 509 | |
Denis Vlasenko | 75f8d08 | 2006-11-22 15:54:52 +0000 | [diff] [blame] | 510 | if (ts->size2 && FD_ISSET(ts->sockfd_write, &wrfdset)) { |
| 511 | /* Write to socket from buffer 2. */ |
| 512 | maxlen = MIN(BUFSIZE - ts->wridx2, ts->size2); |
| 513 | w = safe_write(ts->sockfd_write, ts->buf2 + ts->wridx2, maxlen); |
| 514 | /* needed? if (w < 0 && errno == EAGAIN) continue; */ |
| 515 | if (w < 0) { |
| 516 | if (IS_INETD) |
| 517 | return 0; |
| 518 | free_session(ts); |
| 519 | ts = next; |
| 520 | continue; |
| 521 | } |
| 522 | ts->wridx2 += w; |
| 523 | ts->size2 -= w; |
| 524 | if (ts->wridx2 == BUFSIZE) |
| 525 | ts->wridx2 = 0; |
| 526 | } |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 527 | |
Denis Vlasenko | 75f8d08 | 2006-11-22 15:54:52 +0000 | [diff] [blame] | 528 | if (ts->size1 < BUFSIZE && FD_ISSET(ts->sockfd_read, &rdfdset)) { |
| 529 | /* Read from socket to buffer 1. */ |
| 530 | maxlen = MIN(BUFSIZE - ts->rdidx1, BUFSIZE - ts->size1); |
| 531 | r = safe_read(ts->sockfd_read, ts->buf1 + ts->rdidx1, maxlen); |
| 532 | if (r < 0 && errno == EAGAIN) continue; |
| 533 | if (r <= 0) { |
| 534 | if (IS_INETD) |
| 535 | return 0; |
| 536 | free_session(ts); |
| 537 | ts = next; |
| 538 | continue; |
| 539 | } |
| 540 | if (!ts->buf1[ts->rdidx1 + r - 1]) |
| 541 | if (!--r) |
| 542 | continue; |
| 543 | ts->rdidx1 += r; |
| 544 | ts->size1 += r; |
| 545 | if (ts->rdidx1 == BUFSIZE) |
| 546 | ts->rdidx1 = 0; |
| 547 | } |
| 548 | |
| 549 | if (ts->size2 < BUFSIZE && FD_ISSET(ts->ptyfd, &rdfdset)) { |
| 550 | /* Read from pty to buffer 2. */ |
| 551 | maxlen = MIN(BUFSIZE - ts->rdidx2, BUFSIZE - ts->size2); |
| 552 | r = safe_read(ts->ptyfd, ts->buf2 + ts->rdidx2, maxlen); |
| 553 | if (r < 0 && errno == EAGAIN) continue; |
| 554 | if (r <= 0) { |
| 555 | if (IS_INETD) |
| 556 | return 0; |
| 557 | free_session(ts); |
| 558 | ts = next; |
| 559 | continue; |
| 560 | } |
| 561 | ts->rdidx2 += r; |
| 562 | ts->size2 += r; |
| 563 | if (ts->rdidx2 == BUFSIZE) |
| 564 | ts->rdidx2 = 0; |
| 565 | } |
| 566 | |
| 567 | if (ts->size1 == 0) { |
| 568 | ts->rdidx1 = 0; |
| 569 | ts->wridx1 = 0; |
| 570 | } |
| 571 | if (ts->size2 == 0) { |
| 572 | ts->rdidx2 = 0; |
| 573 | ts->wridx2 = 0; |
| 574 | } |
| 575 | ts = next; |
| 576 | } |
| 577 | goto again; |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 578 | } |