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