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 */ |
Mike Frysinger | 772a346 | 2006-05-10 17:14:32 +0000 | [diff] [blame] | 25 | #undef DEBUG |
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 | |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 29 | #ifdef DEBUG |
| 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 | |
Rob Landley | 00e76cb | 2005-05-10 23:53:33 +0000 | [diff] [blame] | 39 | #ifdef CONFIG_FEATURE_IPV6 |
| 40 | #define SOCKET_TYPE AF_INET6 |
| 41 | typedef struct sockaddr_in6 sockaddr_type; |
| 42 | #else |
| 43 | #define SOCKET_TYPE AF_INET |
| 44 | typedef struct sockaddr_in sockaddr_type; |
| 45 | #endif |
| 46 | |
| 47 | |
Glenn L McGrath | 9e5d6c0 | 2003-01-21 20:55:56 +0000 | [diff] [blame] | 48 | #ifdef CONFIG_LOGIN |
Glenn L McGrath | d4004ee | 2004-09-14 17:24:59 +0000 | [diff] [blame] | 49 | static const char *loginpath = "/bin/login"; |
Glenn L McGrath | 9e5d6c0 | 2003-01-21 20:55:56 +0000 | [diff] [blame] | 50 | #else |
Glenn L McGrath | d4004ee | 2004-09-14 17:24:59 +0000 | [diff] [blame] | 51 | static const char *loginpath; |
Glenn L McGrath | 9e5d6c0 | 2003-01-21 20:55:56 +0000 | [diff] [blame] | 52 | #endif |
| 53 | static const char *issuefile = "/etc/issue.net"; |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 54 | |
| 55 | /* shell name and arguments */ |
| 56 | |
| 57 | static const char *argv_init[] = {NULL, NULL}; |
| 58 | |
| 59 | /* structure that describes a session */ |
| 60 | |
| 61 | struct tsession { |
Glenn L McGrath | 9e5d6c0 | 2003-01-21 20:55:56 +0000 | [diff] [blame] | 62 | #ifdef CONFIG_FEATURE_TELNETD_INETD |
| 63 | int sockfd_read, sockfd_write, ptyfd; |
| 64 | #else /* CONFIG_FEATURE_TELNETD_INETD */ |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 65 | struct tsession *next; |
| 66 | int sockfd, ptyfd; |
Glenn L McGrath | 9e5d6c0 | 2003-01-21 20:55:56 +0000 | [diff] [blame] | 67 | #endif /* CONFIG_FEATURE_TELNETD_INETD */ |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 68 | int shell_pid; |
| 69 | /* two circular buffers */ |
| 70 | char *buf1, *buf2; |
| 71 | int rdidx1, wridx1, size1; |
| 72 | int rdidx2, wridx2, size2; |
| 73 | }; |
| 74 | |
| 75 | /* |
| 76 | |
| 77 | This is how the buffers are used. The arrows indicate the movement |
| 78 | of data. |
| 79 | |
| 80 | +-------+ wridx1++ +------+ rdidx1++ +----------+ |
| 81 | | | <-------------- | buf1 | <-------------- | | |
| 82 | | | size1-- +------+ size1++ | | |
Mike Frysinger | d2c8fd6 | 2006-05-10 17:17:09 +0000 | [diff] [blame] | 83 | | pty | | socket | |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 84 | | | rdidx2++ +------+ wridx2++ | | |
| 85 | | | --------------> | buf2 | --------------> | | |
| 86 | +-------+ size2++ +------+ size2-- +----------+ |
| 87 | |
| 88 | Each session has got two buffers. |
| 89 | |
| 90 | */ |
| 91 | |
| 92 | static int maxfd; |
| 93 | |
| 94 | static struct tsession *sessions; |
| 95 | |
| 96 | |
| 97 | /* |
| 98 | |
Bernhard Reutner-Fischer | e0387a6 | 2006-06-07 13:31:59 +0000 | [diff] [blame] | 99 | 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] | 100 | and must be removed so as to not be interpreted by the terminal). Make an |
| 101 | uninterrupted string of characters fit for the terminal. Do this by packing |
| 102 | all characters meant for the terminal sequentially towards the end of bf. |
| 103 | |
| 104 | Return a pointer to the beginning of the characters meant for the terminal. |
| 105 | and make *num_totty the number of characters that should be sent to |
| 106 | the terminal. |
| 107 | |
| 108 | Note - If an IAC (3 byte quantity) starts before (bf + len) but extends |
| 109 | past (bf + len) then that IAC will be left unprocessed and *processed will be |
| 110 | less than len. |
| 111 | |
| 112 | FIXME - if we mean to send 0xFF to the terminal then it will be escaped, |
| 113 | what is the escape character? We aren't handling that situation here. |
| 114 | |
Eric Andersen | 3752d33 | 2003-12-19 11:30:13 +0000 | [diff] [blame] | 115 | CR-LF ->'s CR mapping is also done here, for convenience |
| 116 | |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 117 | */ |
| 118 | static char * |
| 119 | remove_iacs(struct tsession *ts, int *pnum_totty) { |
Eric Andersen | 0cb6f35 | 2006-01-30 22:30:41 +0000 | [diff] [blame] | 120 | unsigned char *ptr0 = (unsigned char *)ts->buf1 + ts->wridx1; |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 121 | unsigned char *ptr = ptr0; |
| 122 | unsigned char *totty = ptr; |
| 123 | unsigned char *end = ptr + MIN(BUFSIZE - ts->wridx1, ts->size1); |
| 124 | int processed; |
| 125 | int num_totty; |
| 126 | |
| 127 | while (ptr < end) { |
| 128 | if (*ptr != IAC) { |
Eric Andersen | 3752d33 | 2003-12-19 11:30:13 +0000 | [diff] [blame] | 129 | int c = *ptr; |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 130 | *totty++ = *ptr++; |
Eric Andersen | 3752d33 | 2003-12-19 11:30:13 +0000 | [diff] [blame] | 131 | /* We now map \r\n ==> \r for pragmatic reasons. |
| 132 | * Many client implementations send \r\n when |
| 133 | * the user hits the CarriageReturn key. |
| 134 | */ |
| 135 | if (c == '\r' && (*ptr == '\n' || *ptr == 0) && ptr < end) |
| 136 | ptr++; |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 137 | } |
| 138 | else { |
Glenn L McGrath | 90ed9a0 | 2004-02-22 09:45:57 +0000 | [diff] [blame] | 139 | /* |
| 140 | * TELOPT_NAWS support! |
| 141 | */ |
| 142 | if ((ptr+2) >= end) { |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 143 | /* only the beginning of the IAC is in the |
| 144 | buffer we were asked to process, we can't |
| 145 | process this char. */ |
| 146 | break; |
| 147 | } |
Glenn L McGrath | 90ed9a0 | 2004-02-22 09:45:57 +0000 | [diff] [blame] | 148 | |
| 149 | /* |
| 150 | * IAC -> SB -> TELOPT_NAWS -> 4-byte -> IAC -> SE |
| 151 | */ |
| 152 | else if (ptr[1] == SB && ptr[2] == TELOPT_NAWS) { |
| 153 | struct winsize ws; |
| 154 | if ((ptr+8) >= end) |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 155 | break; /* incomplete, can't process */ |
Glenn L McGrath | 90ed9a0 | 2004-02-22 09:45:57 +0000 | [diff] [blame] | 156 | ws.ws_col = (ptr[3] << 8) | ptr[4]; |
| 157 | ws.ws_row = (ptr[5] << 8) | ptr[6]; |
| 158 | (void) ioctl(ts->ptyfd, TIOCSWINSZ, (char *)&ws); |
| 159 | ptr += 9; |
| 160 | } |
| 161 | else { |
| 162 | /* skip 3-byte IAC non-SB cmd */ |
| 163 | #ifdef DEBUG |
| 164 | fprintf(stderr, "Ignoring IAC %s,%s\n", |
| 165 | TELCMD(*(ptr+1)), TELOPT(*(ptr+2))); |
| 166 | #endif |
| 167 | ptr += 3; |
| 168 | } |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 169 | } |
| 170 | } |
| 171 | |
| 172 | processed = ptr - ptr0; |
| 173 | num_totty = totty - ptr0; |
| 174 | /* the difference between processed and num_to tty |
| 175 | is all the iacs we removed from the stream. |
| 176 | Adjust buf1 accordingly. */ |
| 177 | ts->wridx1 += processed - num_totty; |
| 178 | ts->size1 -= processed - num_totty; |
| 179 | *pnum_totty = num_totty; |
| 180 | /* move the chars meant for the terminal towards the end of the |
| 181 | buffer. */ |
| 182 | return memmove(ptr - num_totty, ptr0, num_totty); |
| 183 | } |
| 184 | |
| 185 | |
| 186 | static int |
| 187 | getpty(char *line) |
| 188 | { |
| 189 | int p; |
Glenn L McGrath | f234e7c | 2002-11-10 22:26:19 +0000 | [diff] [blame] | 190 | #ifdef CONFIG_FEATURE_DEVPTS |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 191 | p = open("/dev/ptmx", 2); |
| 192 | if (p > 0) { |
| 193 | grantpt(p); |
| 194 | unlockpt(p); |
| 195 | strcpy(line, ptsname(p)); |
| 196 | return(p); |
| 197 | } |
| 198 | #else |
| 199 | struct stat stb; |
| 200 | int i; |
| 201 | int j; |
| 202 | |
| 203 | strcpy(line, "/dev/ptyXX"); |
| 204 | |
| 205 | for (i = 0; i < 16; i++) { |
| 206 | line[8] = "pqrstuvwxyzabcde"[i]; |
| 207 | line[9] = '0'; |
| 208 | if (stat(line, &stb) < 0) { |
| 209 | continue; |
| 210 | } |
| 211 | for (j = 0; j < 16; j++) { |
| 212 | line[9] = j < 10 ? j + '0' : j - 10 + 'a'; |
Mike Frysinger | 772a346 | 2006-05-10 17:14:32 +0000 | [diff] [blame] | 213 | #ifdef DEBUG |
| 214 | fprintf(stderr, "Trying to open device: %s\n", line); |
| 215 | #endif |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 216 | if ((p = open(line, O_RDWR | O_NOCTTY)) >= 0) { |
| 217 | line[5] = 't'; |
| 218 | return p; |
| 219 | } |
| 220 | } |
| 221 | } |
Glenn L McGrath | f234e7c | 2002-11-10 22:26:19 +0000 | [diff] [blame] | 222 | #endif /* CONFIG_FEATURE_DEVPTS */ |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 223 | return -1; |
| 224 | } |
| 225 | |
| 226 | |
| 227 | static void |
| 228 | send_iac(struct tsession *ts, unsigned char command, int option) |
| 229 | { |
| 230 | /* We rely on that there is space in the buffer for now. */ |
| 231 | char *b = ts->buf2 + ts->rdidx2; |
| 232 | *b++ = IAC; |
| 233 | *b++ = command; |
| 234 | *b++ = option; |
| 235 | ts->rdidx2 += 3; |
| 236 | ts->size2 += 3; |
| 237 | } |
| 238 | |
| 239 | |
| 240 | static struct tsession * |
Glenn L McGrath | 9e5d6c0 | 2003-01-21 20:55:56 +0000 | [diff] [blame] | 241 | #ifdef CONFIG_FEATURE_TELNETD_INETD |
| 242 | make_new_session(void) |
| 243 | #else /* CONFIG_FEATURE_TELNETD_INETD */ |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 244 | make_new_session(int sockfd) |
Glenn L McGrath | 9e5d6c0 | 2003-01-21 20:55:56 +0000 | [diff] [blame] | 245 | #endif /* CONFIG_FEATURE_TELNETD_INETD */ |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 246 | { |
| 247 | struct termios termbuf; |
| 248 | int pty, pid; |
| 249 | char tty_name[32]; |
Rob Landley | 1ec5b29 | 2006-05-29 07:42:02 +0000 | [diff] [blame] | 250 | struct tsession *ts = xzalloc(sizeof(struct tsession) + BUFSIZE * 2); |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 251 | |
| 252 | ts->buf1 = (char *)(&ts[1]); |
| 253 | ts->buf2 = ts->buf1 + BUFSIZE; |
| 254 | |
Glenn L McGrath | 9e5d6c0 | 2003-01-21 20:55:56 +0000 | [diff] [blame] | 255 | #ifdef CONFIG_FEATURE_TELNETD_INETD |
Glenn L McGrath | 9e5d6c0 | 2003-01-21 20:55:56 +0000 | [diff] [blame] | 256 | ts->sockfd_write = 1; |
| 257 | #else /* CONFIG_FEATURE_TELNETD_INETD */ |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 258 | ts->sockfd = sockfd; |
Glenn L McGrath | 9e5d6c0 | 2003-01-21 20:55:56 +0000 | [diff] [blame] | 259 | #endif /* CONFIG_FEATURE_TELNETD_INETD */ |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 260 | |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 261 | /* Got a new connection, set up a tty and spawn a shell. */ |
| 262 | |
| 263 | pty = getpty(tty_name); |
| 264 | |
| 265 | if (pty < 0) { |
Mike Frysinger | 772a346 | 2006-05-10 17:14:32 +0000 | [diff] [blame] | 266 | syslog(LOG_ERR, "All terminals in use!"); |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 267 | return 0; |
| 268 | } |
| 269 | |
| 270 | if (pty > maxfd) |
| 271 | maxfd = pty; |
| 272 | |
| 273 | ts->ptyfd = pty; |
| 274 | |
| 275 | /* Make the telnet client understand we will echo characters so it |
| 276 | * should not do it locally. We don't tell the client to run linemode, |
| 277 | * because we want to handle line editing and tab completion and other |
| 278 | * stuff that requires char-by-char support. |
| 279 | */ |
| 280 | |
| 281 | send_iac(ts, DO, TELOPT_ECHO); |
Glenn L McGrath | 90ed9a0 | 2004-02-22 09:45:57 +0000 | [diff] [blame] | 282 | send_iac(ts, DO, TELOPT_NAWS); |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 283 | send_iac(ts, DO, TELOPT_LFLOW); |
| 284 | send_iac(ts, WILL, TELOPT_ECHO); |
| 285 | send_iac(ts, WILL, TELOPT_SGA); |
| 286 | |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 287 | if ((pid = fork()) < 0) { |
Mike Frysinger | 62ec21d | 2006-05-10 15:59:07 +0000 | [diff] [blame] | 288 | syslog(LOG_ERR, "Could not fork"); |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 289 | } |
| 290 | if (pid == 0) { |
| 291 | /* In child, open the child's side of the tty. */ |
| 292 | int i; |
| 293 | |
| 294 | for(i = 0; i <= maxfd; i++) |
| 295 | close(i); |
| 296 | /* make new process group */ |
| 297 | setsid(); |
| 298 | |
| 299 | if (open(tty_name, O_RDWR /*| O_NOCTTY*/) < 0) { |
Eric Andersen | 36adca8 | 2004-06-22 10:07:17 +0000 | [diff] [blame] | 300 | syslog(LOG_ERR, "Could not open tty"); |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 301 | exit(1); |
Mike Frysinger | 62ec21d | 2006-05-10 15:59:07 +0000 | [diff] [blame] | 302 | } |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 303 | dup(0); |
| 304 | dup(0); |
| 305 | |
| 306 | tcsetpgrp(0, getpid()); |
| 307 | |
| 308 | /* The pseudo-terminal allocated to the client is configured to operate in |
| 309 | * cooked mode, and with XTABS CRMOD enabled (see tty(4)). |
| 310 | */ |
| 311 | |
| 312 | tcgetattr(0, &termbuf); |
| 313 | termbuf.c_lflag |= ECHO; /* if we use readline we dont want this */ |
| 314 | termbuf.c_oflag |= ONLCR|XTABS; |
| 315 | termbuf.c_iflag |= ICRNL; |
| 316 | termbuf.c_iflag &= ~IXOFF; |
| 317 | /*termbuf.c_lflag &= ~ICANON;*/ |
| 318 | tcsetattr(0, TCSANOW, &termbuf); |
| 319 | |
Glenn L McGrath | 9e5d6c0 | 2003-01-21 20:55:56 +0000 | [diff] [blame] | 320 | print_login_issue(issuefile, NULL); |
| 321 | |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 322 | /* exec shell, with correct argv and env */ |
| 323 | execv(loginpath, (char *const *)argv_init); |
| 324 | |
| 325 | /* NOT REACHED */ |
Eric Andersen | 36adca8 | 2004-06-22 10:07:17 +0000 | [diff] [blame] | 326 | syslog(LOG_ERR, "execv error"); |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 327 | exit(1); |
| 328 | } |
| 329 | |
| 330 | ts->shell_pid = pid; |
| 331 | |
| 332 | return ts; |
| 333 | } |
| 334 | |
Glenn L McGrath | 9e5d6c0 | 2003-01-21 20:55:56 +0000 | [diff] [blame] | 335 | #ifndef CONFIG_FEATURE_TELNETD_INETD |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 336 | static void |
| 337 | free_session(struct tsession *ts) |
| 338 | { |
| 339 | struct tsession *t = sessions; |
| 340 | |
| 341 | /* Unlink this telnet session from the session list. */ |
Mike Frysinger | 731f81c | 2006-05-10 15:23:12 +0000 | [diff] [blame] | 342 | if (t == ts) |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 343 | sessions = ts->next; |
| 344 | else { |
| 345 | while(t->next != ts) |
| 346 | t = t->next; |
| 347 | t->next = ts->next; |
| 348 | } |
| 349 | |
| 350 | kill(ts->shell_pid, SIGKILL); |
| 351 | |
| 352 | wait4(ts->shell_pid, NULL, 0, NULL); |
| 353 | |
| 354 | close(ts->ptyfd); |
| 355 | close(ts->sockfd); |
| 356 | |
Mike Frysinger | 731f81c | 2006-05-10 15:23:12 +0000 | [diff] [blame] | 357 | if (ts->ptyfd == maxfd || ts->sockfd == maxfd) |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 358 | maxfd--; |
Mike Frysinger | 731f81c | 2006-05-10 15:23:12 +0000 | [diff] [blame] | 359 | if (ts->ptyfd == maxfd || ts->sockfd == maxfd) |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 360 | maxfd--; |
| 361 | |
| 362 | free(ts); |
| 363 | } |
Glenn L McGrath | 9e5d6c0 | 2003-01-21 20:55:56 +0000 | [diff] [blame] | 364 | #endif /* CONFIG_FEATURE_TELNETD_INETD */ |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 365 | |
| 366 | int |
| 367 | telnetd_main(int argc, char **argv) |
| 368 | { |
Glenn L McGrath | 9e5d6c0 | 2003-01-21 20:55:56 +0000 | [diff] [blame] | 369 | #ifndef CONFIG_FEATURE_TELNETD_INETD |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 370 | sockaddr_type sa; |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 371 | int master_fd; |
Glenn L McGrath | 9e5d6c0 | 2003-01-21 20:55:56 +0000 | [diff] [blame] | 372 | #endif /* CONFIG_FEATURE_TELNETD_INETD */ |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 373 | fd_set rdfdset, wrfdset; |
| 374 | int selret; |
Glenn L McGrath | 9e5d6c0 | 2003-01-21 20:55:56 +0000 | [diff] [blame] | 375 | #ifndef CONFIG_FEATURE_TELNETD_INETD |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 376 | int on = 1; |
| 377 | int portnbr = 23; |
Rob Landley | 64a5f96 | 2005-11-10 22:37:40 +0000 | [diff] [blame] | 378 | struct in_addr bind_addr = { .s_addr = 0x0 }; |
Glenn L McGrath | 9e5d6c0 | 2003-01-21 20:55:56 +0000 | [diff] [blame] | 379 | #endif /* CONFIG_FEATURE_TELNETD_INETD */ |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 380 | int c; |
Glenn L McGrath | 9e5d6c0 | 2003-01-21 20:55:56 +0000 | [diff] [blame] | 381 | static const char options[] = |
| 382 | #ifdef CONFIG_FEATURE_TELNETD_INETD |
| 383 | "f:l:"; |
| 384 | #else /* CONFIG_EATURE_TELNETD_INETD */ |
Rob Landley | 64a5f96 | 2005-11-10 22:37:40 +0000 | [diff] [blame] | 385 | "f:l:p:b:"; |
Glenn L McGrath | 9e5d6c0 | 2003-01-21 20:55:56 +0000 | [diff] [blame] | 386 | #endif /* CONFIG_FEATURE_TELNETD_INETD */ |
Eric Andersen | 86f2cce | 2003-04-25 12:32:37 +0000 | [diff] [blame] | 387 | int maxlen, w, r; |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 388 | |
Glenn L McGrath | c2b9186 | 2003-09-12 11:27:15 +0000 | [diff] [blame] | 389 | #ifndef CONFIG_LOGIN |
| 390 | loginpath = DEFAULT_SHELL; |
| 391 | #endif |
| 392 | |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 393 | for (;;) { |
Glenn L McGrath | 9e5d6c0 | 2003-01-21 20:55:56 +0000 | [diff] [blame] | 394 | c = getopt( argc, argv, options); |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 395 | if (c == EOF) break; |
| 396 | switch (c) { |
Glenn L McGrath | 9e5d6c0 | 2003-01-21 20:55:56 +0000 | [diff] [blame] | 397 | case 'f': |
Glenn L McGrath | d4004ee | 2004-09-14 17:24:59 +0000 | [diff] [blame] | 398 | issuefile = optarg; |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 399 | break; |
| 400 | case 'l': |
Glenn L McGrath | d4004ee | 2004-09-14 17:24:59 +0000 | [diff] [blame] | 401 | loginpath = optarg; |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 402 | break; |
Glenn L McGrath | 9e5d6c0 | 2003-01-21 20:55:56 +0000 | [diff] [blame] | 403 | #ifndef CONFIG_FEATURE_TELNETD_INETD |
| 404 | case 'p': |
| 405 | portnbr = atoi(optarg); |
| 406 | break; |
Rob Landley | 64a5f96 | 2005-11-10 22:37:40 +0000 | [diff] [blame] | 407 | case 'b': |
| 408 | if (inet_aton(optarg, &bind_addr) == 0) |
| 409 | bb_show_usage(); |
| 410 | break; |
Glenn L McGrath | 9e5d6c0 | 2003-01-21 20:55:56 +0000 | [diff] [blame] | 411 | #endif /* CONFIG_FEATURE_TELNETD_INETD */ |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 412 | default: |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 413 | bb_show_usage(); |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 414 | } |
| 415 | } |
| 416 | |
| 417 | if (access(loginpath, X_OK) < 0) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 418 | bb_error_msg_and_die ("'%s' unavailable.", loginpath); |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | argv_init[0] = loginpath; |
Glenn L McGrath | 9e5d6c0 | 2003-01-21 20:55:56 +0000 | [diff] [blame] | 422 | |
Eric Andersen | 36adca8 | 2004-06-22 10:07:17 +0000 | [diff] [blame] | 423 | openlog(bb_applet_name, 0, LOG_USER); |
| 424 | |
Glenn L McGrath | 9e5d6c0 | 2003-01-21 20:55:56 +0000 | [diff] [blame] | 425 | #ifdef CONFIG_FEATURE_TELNETD_INETD |
Glenn L McGrath | 9e5d6c0 | 2003-01-21 20:55:56 +0000 | [diff] [blame] | 426 | maxfd = 1; |
Glenn L McGrath | 8eb214e | 2003-01-22 21:09:48 +0000 | [diff] [blame] | 427 | sessions = make_new_session(); |
Glenn L McGrath | 9e5d6c0 | 2003-01-21 20:55:56 +0000 | [diff] [blame] | 428 | #else /* CONFIG_EATURE_TELNETD_INETD */ |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 429 | sessions = 0; |
| 430 | |
| 431 | /* Grab a TCP socket. */ |
| 432 | |
Rob Landley | 099ed50 | 2006-08-28 09:41:49 +0000 | [diff] [blame] | 433 | master_fd = xsocket(SOCKET_TYPE, SOCK_STREAM, 0); |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 434 | (void)setsockopt(master_fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)); |
| 435 | |
| 436 | /* Set it to listen to specified port. */ |
| 437 | |
| 438 | memset((void *)&sa, 0, sizeof(sa)); |
Rob Landley | 00e76cb | 2005-05-10 23:53:33 +0000 | [diff] [blame] | 439 | #ifdef CONFIG_FEATURE_IPV6 |
| 440 | sa.sin6_family = AF_INET6; |
| 441 | sa.sin6_port = htons(portnbr); |
Rob Landley | 64a5f96 | 2005-11-10 22:37:40 +0000 | [diff] [blame] | 442 | /* sa.sin6_addr = bind_addr6; */ |
Rob Landley | 00e76cb | 2005-05-10 23:53:33 +0000 | [diff] [blame] | 443 | #else |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 444 | sa.sin_family = AF_INET; |
| 445 | sa.sin_port = htons(portnbr); |
Rob Landley | 64a5f96 | 2005-11-10 22:37:40 +0000 | [diff] [blame] | 446 | sa.sin_addr = bind_addr; |
Rob Landley | 00e76cb | 2005-05-10 23:53:33 +0000 | [diff] [blame] | 447 | #endif |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 448 | |
Rob Landley | 099ed50 | 2006-08-28 09:41:49 +0000 | [diff] [blame] | 449 | xbind(master_fd, (struct sockaddr *) &sa, sizeof(sa)); |
| 450 | xlisten(master_fd, 1); |
| 451 | xdaemon(0, 0); |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 452 | |
| 453 | maxfd = master_fd; |
Glenn L McGrath | 9e5d6c0 | 2003-01-21 20:55:56 +0000 | [diff] [blame] | 454 | #endif /* CONFIG_FEATURE_TELNETD_INETD */ |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 455 | |
| 456 | do { |
| 457 | struct tsession *ts; |
| 458 | |
| 459 | FD_ZERO(&rdfdset); |
| 460 | FD_ZERO(&wrfdset); |
| 461 | |
| 462 | /* select on the master socket, all telnet sockets and their |
| 463 | * ptys if there is room in their respective session buffers. |
| 464 | */ |
| 465 | |
Glenn L McGrath | 9e5d6c0 | 2003-01-21 20:55:56 +0000 | [diff] [blame] | 466 | #ifndef CONFIG_FEATURE_TELNETD_INETD |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 467 | FD_SET(master_fd, &rdfdset); |
Glenn L McGrath | 9e5d6c0 | 2003-01-21 20:55:56 +0000 | [diff] [blame] | 468 | #endif /* CONFIG_FEATURE_TELNETD_INETD */ |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 469 | |
| 470 | ts = sessions; |
Glenn L McGrath | 9e5d6c0 | 2003-01-21 20:55:56 +0000 | [diff] [blame] | 471 | #ifndef CONFIG_FEATURE_TELNETD_INETD |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 472 | while (ts) { |
Glenn L McGrath | 9e5d6c0 | 2003-01-21 20:55:56 +0000 | [diff] [blame] | 473 | #endif /* CONFIG_FEATURE_TELNETD_INETD */ |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 474 | /* buf1 is used from socket to pty |
| 475 | * buf2 is used from pty to socket |
| 476 | */ |
| 477 | if (ts->size1 > 0) { |
| 478 | FD_SET(ts->ptyfd, &wrfdset); /* can write to pty */ |
| 479 | } |
| 480 | if (ts->size1 < BUFSIZE) { |
Glenn L McGrath | 9e5d6c0 | 2003-01-21 20:55:56 +0000 | [diff] [blame] | 481 | #ifdef CONFIG_FEATURE_TELNETD_INETD |
| 482 | FD_SET(ts->sockfd_read, &rdfdset); /* can read from socket */ |
| 483 | #else /* CONFIG_FEATURE_TELNETD_INETD */ |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 484 | FD_SET(ts->sockfd, &rdfdset); /* can read from socket */ |
Glenn L McGrath | 9e5d6c0 | 2003-01-21 20:55:56 +0000 | [diff] [blame] | 485 | #endif /* CONFIG_FEATURE_TELNETD_INETD */ |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 486 | } |
| 487 | if (ts->size2 > 0) { |
Glenn L McGrath | 9e5d6c0 | 2003-01-21 20:55:56 +0000 | [diff] [blame] | 488 | #ifdef CONFIG_FEATURE_TELNETD_INETD |
| 489 | FD_SET(ts->sockfd_write, &wrfdset); /* can write to socket */ |
| 490 | #else /* CONFIG_FEATURE_TELNETD_INETD */ |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 491 | FD_SET(ts->sockfd, &wrfdset); /* can write to socket */ |
Glenn L McGrath | 9e5d6c0 | 2003-01-21 20:55:56 +0000 | [diff] [blame] | 492 | #endif /* CONFIG_FEATURE_TELNETD_INETD */ |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 493 | } |
| 494 | if (ts->size2 < BUFSIZE) { |
| 495 | FD_SET(ts->ptyfd, &rdfdset); /* can read from pty */ |
| 496 | } |
Glenn L McGrath | 9e5d6c0 | 2003-01-21 20:55:56 +0000 | [diff] [blame] | 497 | #ifndef CONFIG_FEATURE_TELNETD_INETD |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 498 | ts = ts->next; |
| 499 | } |
Glenn L McGrath | 9e5d6c0 | 2003-01-21 20:55:56 +0000 | [diff] [blame] | 500 | #endif /* CONFIG_FEATURE_TELNETD_INETD */ |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 501 | |
| 502 | selret = select(maxfd + 1, &rdfdset, &wrfdset, 0, 0); |
| 503 | |
| 504 | if (!selret) |
| 505 | break; |
| 506 | |
Glenn L McGrath | 9e5d6c0 | 2003-01-21 20:55:56 +0000 | [diff] [blame] | 507 | #ifndef CONFIG_FEATURE_TELNETD_INETD |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 508 | /* First check for and accept new sessions. */ |
| 509 | if (FD_ISSET(master_fd, &rdfdset)) { |
Mike Frysinger | 06b00e8 | 2006-05-10 17:18:11 +0000 | [diff] [blame] | 510 | int fd; |
| 511 | socklen_t salen; |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 512 | |
| 513 | salen = sizeof(sa); |
| 514 | if ((fd = accept(master_fd, (struct sockaddr *)&sa, |
| 515 | &salen)) < 0) { |
| 516 | continue; |
| 517 | } else { |
| 518 | /* Create a new session and link it into |
| 519 | our active list. */ |
| 520 | struct tsession *new_ts = make_new_session(fd); |
| 521 | if (new_ts) { |
| 522 | new_ts->next = sessions; |
| 523 | sessions = new_ts; |
| 524 | if (fd > maxfd) |
| 525 | maxfd = fd; |
| 526 | } else { |
| 527 | close(fd); |
| 528 | } |
| 529 | } |
| 530 | } |
| 531 | |
| 532 | /* Then check for data tunneling. */ |
| 533 | |
| 534 | ts = sessions; |
| 535 | while (ts) { /* For all sessions... */ |
Glenn L McGrath | 9e5d6c0 | 2003-01-21 20:55:56 +0000 | [diff] [blame] | 536 | #endif /* CONFIG_FEATURE_TELNETD_INETD */ |
Glenn L McGrath | 9e5d6c0 | 2003-01-21 20:55:56 +0000 | [diff] [blame] | 537 | #ifndef CONFIG_FEATURE_TELNETD_INETD |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 538 | struct tsession *next = ts->next; /* in case we free ts. */ |
Glenn L McGrath | 9e5d6c0 | 2003-01-21 20:55:56 +0000 | [diff] [blame] | 539 | #endif /* CONFIG_FEATURE_TELNETD_INETD */ |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 540 | |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 541 | if (ts->size1 && FD_ISSET(ts->ptyfd, &wrfdset)) { |
| 542 | int num_totty; |
| 543 | char *ptr; |
| 544 | /* Write to pty from buffer 1. */ |
| 545 | |
| 546 | ptr = remove_iacs(ts, &num_totty); |
| 547 | |
| 548 | w = write(ts->ptyfd, ptr, num_totty); |
| 549 | if (w < 0) { |
Glenn L McGrath | 9e5d6c0 | 2003-01-21 20:55:56 +0000 | [diff] [blame] | 550 | #ifdef CONFIG_FEATURE_TELNETD_INETD |
| 551 | exit(0); |
| 552 | #else /* CONFIG_FEATURE_TELNETD_INETD */ |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 553 | free_session(ts); |
| 554 | ts = next; |
| 555 | continue; |
Glenn L McGrath | 9e5d6c0 | 2003-01-21 20:55:56 +0000 | [diff] [blame] | 556 | #endif /* CONFIG_FEATURE_TELNETD_INETD */ |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 557 | } |
| 558 | ts->wridx1 += w; |
| 559 | ts->size1 -= w; |
| 560 | if (ts->wridx1 == BUFSIZE) |
| 561 | ts->wridx1 = 0; |
| 562 | } |
| 563 | |
Glenn L McGrath | 9e5d6c0 | 2003-01-21 20:55:56 +0000 | [diff] [blame] | 564 | #ifdef CONFIG_FEATURE_TELNETD_INETD |
| 565 | if (ts->size2 && FD_ISSET(ts->sockfd_write, &wrfdset)) { |
| 566 | #else /* CONFIG_FEATURE_TELNETD_INETD */ |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 567 | if (ts->size2 && FD_ISSET(ts->sockfd, &wrfdset)) { |
Glenn L McGrath | 9e5d6c0 | 2003-01-21 20:55:56 +0000 | [diff] [blame] | 568 | #endif /* CONFIG_FEATURE_TELNETD_INETD */ |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 569 | /* Write to socket from buffer 2. */ |
| 570 | maxlen = MIN(BUFSIZE - ts->wridx2, ts->size2); |
Glenn L McGrath | 9e5d6c0 | 2003-01-21 20:55:56 +0000 | [diff] [blame] | 571 | #ifdef CONFIG_FEATURE_TELNETD_INETD |
| 572 | w = write(ts->sockfd_write, ts->buf2 + ts->wridx2, maxlen); |
| 573 | if (w < 0) |
| 574 | exit(0); |
| 575 | #else /* CONFIG_FEATURE_TELNETD_INETD */ |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 576 | w = write(ts->sockfd, ts->buf2 + ts->wridx2, maxlen); |
| 577 | if (w < 0) { |
| 578 | free_session(ts); |
| 579 | ts = next; |
| 580 | continue; |
| 581 | } |
Glenn L McGrath | 9e5d6c0 | 2003-01-21 20:55:56 +0000 | [diff] [blame] | 582 | #endif /* CONFIG_FEATURE_TELNETD_INETD */ |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 583 | ts->wridx2 += w; |
| 584 | ts->size2 -= w; |
| 585 | if (ts->wridx2 == BUFSIZE) |
| 586 | ts->wridx2 = 0; |
| 587 | } |
| 588 | |
Glenn L McGrath | 9e5d6c0 | 2003-01-21 20:55:56 +0000 | [diff] [blame] | 589 | #ifdef CONFIG_FEATURE_TELNETD_INETD |
| 590 | if (ts->size1 < BUFSIZE && FD_ISSET(ts->sockfd_read, &rdfdset)) { |
| 591 | #else /* CONFIG_FEATURE_TELNETD_INETD */ |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 592 | if (ts->size1 < BUFSIZE && FD_ISSET(ts->sockfd, &rdfdset)) { |
Glenn L McGrath | 9e5d6c0 | 2003-01-21 20:55:56 +0000 | [diff] [blame] | 593 | #endif /* CONFIG_FEATURE_TELNETD_INETD */ |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 594 | /* Read from socket to buffer 1. */ |
| 595 | maxlen = MIN(BUFSIZE - ts->rdidx1, |
| 596 | BUFSIZE - ts->size1); |
Glenn L McGrath | 9e5d6c0 | 2003-01-21 20:55:56 +0000 | [diff] [blame] | 597 | #ifdef CONFIG_FEATURE_TELNETD_INETD |
| 598 | r = read(ts->sockfd_read, ts->buf1 + ts->rdidx1, maxlen); |
| 599 | if (!r || (r < 0 && errno != EINTR)) |
| 600 | exit(0); |
| 601 | #else /* CONFIG_FEATURE_TELNETD_INETD */ |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 602 | r = read(ts->sockfd, ts->buf1 + ts->rdidx1, maxlen); |
| 603 | if (!r || (r < 0 && errno != EINTR)) { |
| 604 | free_session(ts); |
| 605 | ts = next; |
| 606 | continue; |
| 607 | } |
Glenn L McGrath | 9e5d6c0 | 2003-01-21 20:55:56 +0000 | [diff] [blame] | 608 | #endif /* CONFIG_FEATURE_TELNETD_INETD */ |
Mike Frysinger | 731f81c | 2006-05-10 15:23:12 +0000 | [diff] [blame] | 609 | if (!*(ts->buf1 + ts->rdidx1 + r - 1)) { |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 610 | r--; |
Mike Frysinger | 731f81c | 2006-05-10 15:23:12 +0000 | [diff] [blame] | 611 | if (!r) |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 612 | continue; |
| 613 | } |
| 614 | ts->rdidx1 += r; |
| 615 | ts->size1 += r; |
| 616 | if (ts->rdidx1 == BUFSIZE) |
| 617 | ts->rdidx1 = 0; |
| 618 | } |
| 619 | |
| 620 | if (ts->size2 < BUFSIZE && FD_ISSET(ts->ptyfd, &rdfdset)) { |
| 621 | /* Read from pty to buffer 2. */ |
| 622 | maxlen = MIN(BUFSIZE - ts->rdidx2, |
| 623 | BUFSIZE - ts->size2); |
| 624 | r = read(ts->ptyfd, ts->buf2 + ts->rdidx2, maxlen); |
| 625 | if (!r || (r < 0 && errno != EINTR)) { |
Glenn L McGrath | 9e5d6c0 | 2003-01-21 20:55:56 +0000 | [diff] [blame] | 626 | #ifdef CONFIG_FEATURE_TELNETD_INETD |
| 627 | exit(0); |
| 628 | #else /* CONFIG_FEATURE_TELNETD_INETD */ |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 629 | free_session(ts); |
| 630 | ts = next; |
| 631 | continue; |
Glenn L McGrath | 9e5d6c0 | 2003-01-21 20:55:56 +0000 | [diff] [blame] | 632 | #endif /* CONFIG_FEATURE_TELNETD_INETD */ |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 633 | } |
| 634 | ts->rdidx2 += r; |
| 635 | ts->size2 += r; |
| 636 | if (ts->rdidx2 == BUFSIZE) |
| 637 | ts->rdidx2 = 0; |
| 638 | } |
| 639 | |
| 640 | if (ts->size1 == 0) { |
| 641 | ts->rdidx1 = 0; |
| 642 | ts->wridx1 = 0; |
| 643 | } |
| 644 | if (ts->size2 == 0) { |
| 645 | ts->rdidx2 = 0; |
| 646 | ts->wridx2 = 0; |
| 647 | } |
Glenn L McGrath | 9e5d6c0 | 2003-01-21 20:55:56 +0000 | [diff] [blame] | 648 | #ifndef CONFIG_FEATURE_TELNETD_INETD |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 649 | ts = next; |
| 650 | } |
Glenn L McGrath | 9e5d6c0 | 2003-01-21 20:55:56 +0000 | [diff] [blame] | 651 | #endif /* CONFIG_FEATURE_TELNETD_INETD */ |
Eric Andersen | 08a7220 | 2002-09-30 20:52:10 +0000 | [diff] [blame] | 652 | |
| 653 | } while (1); |
| 654 | |
| 655 | return 0; |
| 656 | } |