1 /* vi: set sw=4 ts=4: */
4 * Bjorn Wesen, Axis Communications AB (bjornw@axis.com)
6 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
8 * ---------------------------------------------------------------------------
9 * (C) Copyright 2000, Axis Communications AB, LUND, SWEDEN
10 ****************************************************************************
12 * The telnetd manpage says it all:
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.
20 * Vladimir Oleynik <dzo@simtreas.ru> 2001
21 * Set process group corrections, initial busybox port
27 #include <sys/socket.h>
29 #include <sys/ioctl.h>
34 #include <netinet/in.h>
35 #include <arpa/inet.h>
44 #include <arpa/telnet.h>
46 #include <sys/syslog.h>
52 #ifdef CONFIG_FEATURE_IPV6
53 #define SOCKET_TYPE AF_INET6
54 typedef struct sockaddr_in6 sockaddr_type;
56 #define SOCKET_TYPE AF_INET
57 typedef struct sockaddr_in sockaddr_type;
62 static const char *loginpath = "/bin/login";
64 static const char *loginpath;
66 static const char *issuefile = "/etc/issue.net";
68 /* shell name and arguments */
70 static const char *argv_init[] = {NULL, NULL};
72 /* structure that describes a session */
75 #ifdef CONFIG_FEATURE_TELNETD_INETD
76 int sockfd_read, sockfd_write, ptyfd;
77 #else /* CONFIG_FEATURE_TELNETD_INETD */
78 struct tsession *next;
80 #endif /* CONFIG_FEATURE_TELNETD_INETD */
82 /* two circular buffers */
84 int rdidx1, wridx1, size1;
85 int rdidx2, wridx2, size2;
90 This is how the buffers are used. The arrows indicate the movement
93 +-------+ wridx1++ +------+ rdidx1++ +----------+
94 | | <-------------- | buf1 | <-------------- | |
95 | | size1-- +------+ size1++ | |
97 | | rdidx2++ +------+ wridx2++ | |
98 | | --------------> | buf2 | --------------> | |
99 +-------+ size2++ +------+ size2-- +----------+
101 Each session has got two buffers.
107 static struct tsession *sessions;
112 Remove all IAC's from the buffer pointed to by bf (received 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.
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
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
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.
128 CR-LF ->'s CR mapping is also done here, for convenience
132 remove_iacs(struct tsession *ts, int *pnum_totty) {
133 unsigned char *ptr0 = (unsigned char *)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);
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.
148 if (c == '\r' && (*ptr == '\n' || *ptr == 0) && ptr < end)
153 * TELOPT_NAWS support!
155 if ((ptr+2) >= end) {
156 /* only the beginning of the IAC is in the
157 buffer we were asked to process, we can't
158 process this char. */
163 * IAC -> SB -> TELOPT_NAWS -> 4-byte -> IAC -> SE
165 else if (ptr[1] == SB && ptr[2] == TELOPT_NAWS) {
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);
175 /* skip 3-byte IAC non-SB cmd */
177 fprintf(stderr, "Ignoring IAC %s,%s\n",
178 TELCMD(*(ptr+1)), TELOPT(*(ptr+2)));
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
195 return memmove(ptr - num_totty, ptr0, num_totty);
203 #ifdef CONFIG_FEATURE_DEVPTS
204 p = open("/dev/ptmx", 2);
208 strcpy(line, ptsname(p));
216 strcpy(line, "/dev/ptyXX");
218 for (i = 0; i < 16; i++) {
219 line[8] = "pqrstuvwxyzabcde"[i];
221 if (stat(line, &stb) < 0) {
224 for (j = 0; j < 16; j++) {
225 line[9] = j < 10 ? j + '0' : j - 10 + 'a';
227 fprintf(stderr, "Trying to open device: %s\n", line);
229 if ((p = open(line, O_RDWR | O_NOCTTY)) >= 0) {
235 #endif /* CONFIG_FEATURE_DEVPTS */
241 send_iac(struct tsession *ts, unsigned char command, int option)
243 /* We rely on that there is space in the buffer for now. */
244 char *b = ts->buf2 + ts->rdidx2;
253 static struct tsession *
254 #ifdef CONFIG_FEATURE_TELNETD_INETD
255 make_new_session(void)
256 #else /* CONFIG_FEATURE_TELNETD_INETD */
257 make_new_session(int sockfd)
258 #endif /* CONFIG_FEATURE_TELNETD_INETD */
260 struct termios termbuf;
263 struct tsession *ts = xzalloc(sizeof(struct tsession) + BUFSIZE * 2);
265 ts->buf1 = (char *)(&ts[1]);
266 ts->buf2 = ts->buf1 + BUFSIZE;
268 #ifdef CONFIG_FEATURE_TELNETD_INETD
269 ts->sockfd_write = 1;
270 #else /* CONFIG_FEATURE_TELNETD_INETD */
272 #endif /* CONFIG_FEATURE_TELNETD_INETD */
274 /* Got a new connection, set up a tty and spawn a shell. */
276 pty = getpty(tty_name);
279 syslog(LOG_ERR, "All terminals in use!");
288 /* Make the telnet client understand we will echo characters so it
289 * should not do it locally. We don't tell the client to run linemode,
290 * because we want to handle line editing and tab completion and other
291 * stuff that requires char-by-char support.
294 send_iac(ts, DO, TELOPT_ECHO);
295 send_iac(ts, DO, TELOPT_NAWS);
296 send_iac(ts, DO, TELOPT_LFLOW);
297 send_iac(ts, WILL, TELOPT_ECHO);
298 send_iac(ts, WILL, TELOPT_SGA);
300 if ((pid = fork()) < 0) {
301 syslog(LOG_ERR, "Could not fork");
304 /* In child, open the child's side of the tty. */
307 for(i = 0; i <= maxfd; i++)
309 /* make new process group */
312 if (open(tty_name, O_RDWR /*| O_NOCTTY*/) < 0) {
313 syslog(LOG_ERR, "Could not open tty");
319 tcsetpgrp(0, getpid());
321 /* The pseudo-terminal allocated to the client is configured to operate in
322 * cooked mode, and with XTABS CRMOD enabled (see tty(4)).
325 tcgetattr(0, &termbuf);
326 termbuf.c_lflag |= ECHO; /* if we use readline we dont want this */
327 termbuf.c_oflag |= ONLCR|XTABS;
328 termbuf.c_iflag |= ICRNL;
329 termbuf.c_iflag &= ~IXOFF;
330 /*termbuf.c_lflag &= ~ICANON;*/
331 tcsetattr(0, TCSANOW, &termbuf);
333 print_login_issue(issuefile, NULL);
335 /* exec shell, with correct argv and env */
336 execv(loginpath, (char *const *)argv_init);
339 syslog(LOG_ERR, "execv error");
348 #ifndef CONFIG_FEATURE_TELNETD_INETD
350 free_session(struct tsession *ts)
352 struct tsession *t = sessions;
354 /* Unlink this telnet session from the session list. */
363 kill(ts->shell_pid, SIGKILL);
365 wait4(ts->shell_pid, NULL, 0, NULL);
370 if (ts->ptyfd == maxfd || ts->sockfd == maxfd)
372 if (ts->ptyfd == maxfd || ts->sockfd == maxfd)
377 #endif /* CONFIG_FEATURE_TELNETD_INETD */
380 telnetd_main(int argc, char **argv)
382 #ifndef CONFIG_FEATURE_TELNETD_INETD
385 #endif /* CONFIG_FEATURE_TELNETD_INETD */
386 fd_set rdfdset, wrfdset;
388 #ifndef CONFIG_FEATURE_TELNETD_INETD
391 struct in_addr bind_addr = { .s_addr = 0x0 };
392 #endif /* CONFIG_FEATURE_TELNETD_INETD */
394 static const char options[] =
395 #ifdef CONFIG_FEATURE_TELNETD_INETD
397 #else /* CONFIG_EATURE_TELNETD_INETD */
399 #endif /* CONFIG_FEATURE_TELNETD_INETD */
403 loginpath = DEFAULT_SHELL;
407 c = getopt( argc, argv, options);
416 #ifndef CONFIG_FEATURE_TELNETD_INETD
418 portnbr = atoi(optarg);
421 if (inet_aton(optarg, &bind_addr) == 0)
424 #endif /* CONFIG_FEATURE_TELNETD_INETD */
430 if (access(loginpath, X_OK) < 0) {
431 bb_error_msg_and_die ("'%s' unavailable.", loginpath);
434 argv_init[0] = loginpath;
436 openlog(bb_applet_name, 0, LOG_USER);
438 #ifdef CONFIG_FEATURE_TELNETD_INETD
440 sessions = make_new_session();
441 #else /* CONFIG_EATURE_TELNETD_INETD */
444 /* Grab a TCP socket. */
446 master_fd = bb_xsocket(SOCKET_TYPE, SOCK_STREAM, 0);
447 (void)setsockopt(master_fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
449 /* Set it to listen to specified port. */
451 memset((void *)&sa, 0, sizeof(sa));
452 #ifdef CONFIG_FEATURE_IPV6
453 sa.sin6_family = AF_INET6;
454 sa.sin6_port = htons(portnbr);
455 /* sa.sin6_addr = bind_addr6; */
457 sa.sin_family = AF_INET;
458 sa.sin_port = htons(portnbr);
459 sa.sin_addr = bind_addr;
462 bb_xbind(master_fd, (struct sockaddr *) &sa, sizeof(sa));
463 bb_xlisten(master_fd, 1);
467 #endif /* CONFIG_FEATURE_TELNETD_INETD */
475 /* select on the master socket, all telnet sockets and their
476 * ptys if there is room in their respective session buffers.
479 #ifndef CONFIG_FEATURE_TELNETD_INETD
480 FD_SET(master_fd, &rdfdset);
481 #endif /* CONFIG_FEATURE_TELNETD_INETD */
484 #ifndef CONFIG_FEATURE_TELNETD_INETD
486 #endif /* CONFIG_FEATURE_TELNETD_INETD */
487 /* buf1 is used from socket to pty
488 * buf2 is used from pty to socket
491 FD_SET(ts->ptyfd, &wrfdset); /* can write to pty */
493 if (ts->size1 < BUFSIZE) {
494 #ifdef CONFIG_FEATURE_TELNETD_INETD
495 FD_SET(ts->sockfd_read, &rdfdset); /* can read from socket */
496 #else /* CONFIG_FEATURE_TELNETD_INETD */
497 FD_SET(ts->sockfd, &rdfdset); /* can read from socket */
498 #endif /* CONFIG_FEATURE_TELNETD_INETD */
501 #ifdef CONFIG_FEATURE_TELNETD_INETD
502 FD_SET(ts->sockfd_write, &wrfdset); /* can write to socket */
503 #else /* CONFIG_FEATURE_TELNETD_INETD */
504 FD_SET(ts->sockfd, &wrfdset); /* can write to socket */
505 #endif /* CONFIG_FEATURE_TELNETD_INETD */
507 if (ts->size2 < BUFSIZE) {
508 FD_SET(ts->ptyfd, &rdfdset); /* can read from pty */
510 #ifndef CONFIG_FEATURE_TELNETD_INETD
513 #endif /* CONFIG_FEATURE_TELNETD_INETD */
515 selret = select(maxfd + 1, &rdfdset, &wrfdset, 0, 0);
520 #ifndef CONFIG_FEATURE_TELNETD_INETD
521 /* First check for and accept new sessions. */
522 if (FD_ISSET(master_fd, &rdfdset)) {
527 if ((fd = accept(master_fd, (struct sockaddr *)&sa,
531 /* Create a new session and link it into
533 struct tsession *new_ts = make_new_session(fd);
535 new_ts->next = sessions;
545 /* Then check for data tunneling. */
548 while (ts) { /* For all sessions... */
549 #endif /* CONFIG_FEATURE_TELNETD_INETD */
550 #ifndef CONFIG_FEATURE_TELNETD_INETD
551 struct tsession *next = ts->next; /* in case we free ts. */
552 #endif /* CONFIG_FEATURE_TELNETD_INETD */
554 if (ts->size1 && FD_ISSET(ts->ptyfd, &wrfdset)) {
557 /* Write to pty from buffer 1. */
559 ptr = remove_iacs(ts, &num_totty);
561 w = write(ts->ptyfd, ptr, num_totty);
563 #ifdef CONFIG_FEATURE_TELNETD_INETD
565 #else /* CONFIG_FEATURE_TELNETD_INETD */
569 #endif /* CONFIG_FEATURE_TELNETD_INETD */
573 if (ts->wridx1 == BUFSIZE)
577 #ifdef CONFIG_FEATURE_TELNETD_INETD
578 if (ts->size2 && FD_ISSET(ts->sockfd_write, &wrfdset)) {
579 #else /* CONFIG_FEATURE_TELNETD_INETD */
580 if (ts->size2 && FD_ISSET(ts->sockfd, &wrfdset)) {
581 #endif /* CONFIG_FEATURE_TELNETD_INETD */
582 /* Write to socket from buffer 2. */
583 maxlen = MIN(BUFSIZE - ts->wridx2, ts->size2);
584 #ifdef CONFIG_FEATURE_TELNETD_INETD
585 w = write(ts->sockfd_write, ts->buf2 + ts->wridx2, maxlen);
588 #else /* CONFIG_FEATURE_TELNETD_INETD */
589 w = write(ts->sockfd, ts->buf2 + ts->wridx2, maxlen);
595 #endif /* CONFIG_FEATURE_TELNETD_INETD */
598 if (ts->wridx2 == BUFSIZE)
602 #ifdef CONFIG_FEATURE_TELNETD_INETD
603 if (ts->size1 < BUFSIZE && FD_ISSET(ts->sockfd_read, &rdfdset)) {
604 #else /* CONFIG_FEATURE_TELNETD_INETD */
605 if (ts->size1 < BUFSIZE && FD_ISSET(ts->sockfd, &rdfdset)) {
606 #endif /* CONFIG_FEATURE_TELNETD_INETD */
607 /* Read from socket to buffer 1. */
608 maxlen = MIN(BUFSIZE - ts->rdidx1,
609 BUFSIZE - ts->size1);
610 #ifdef CONFIG_FEATURE_TELNETD_INETD
611 r = read(ts->sockfd_read, ts->buf1 + ts->rdidx1, maxlen);
612 if (!r || (r < 0 && errno != EINTR))
614 #else /* CONFIG_FEATURE_TELNETD_INETD */
615 r = read(ts->sockfd, ts->buf1 + ts->rdidx1, maxlen);
616 if (!r || (r < 0 && errno != EINTR)) {
621 #endif /* CONFIG_FEATURE_TELNETD_INETD */
622 if (!*(ts->buf1 + ts->rdidx1 + r - 1)) {
629 if (ts->rdidx1 == BUFSIZE)
633 if (ts->size2 < BUFSIZE && FD_ISSET(ts->ptyfd, &rdfdset)) {
634 /* Read from pty to buffer 2. */
635 maxlen = MIN(BUFSIZE - ts->rdidx2,
636 BUFSIZE - ts->size2);
637 r = read(ts->ptyfd, ts->buf2 + ts->rdidx2, maxlen);
638 if (!r || (r < 0 && errno != EINTR)) {
639 #ifdef CONFIG_FEATURE_TELNETD_INETD
641 #else /* CONFIG_FEATURE_TELNETD_INETD */
645 #endif /* CONFIG_FEATURE_TELNETD_INETD */
649 if (ts->rdidx2 == BUFSIZE)
653 if (ts->size1 == 0) {
657 if (ts->size2 == 0) {
661 #ifndef CONFIG_FEATURE_TELNETD_INETD
664 #endif /* CONFIG_FEATURE_TELNETD_INETD */