1 /* vi: set sw=4 ts=4: */
4 * Bjorn Wesen, Axis Communications AB (bjornw@axis.com)
6 * Licensed under GPL, 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
28 #include <sys/socket.h>
30 #include <sys/ioctl.h>
35 #include <netinet/in.h>
36 #include <arpa/inet.h>
45 #include <arpa/telnet.h>
47 #include <sys/syslog.h>
53 #ifdef CONFIG_FEATURE_IPV6
54 #define SOCKET_TYPE AF_INET6
55 typedef struct sockaddr_in6 sockaddr_type;
57 #define SOCKET_TYPE AF_INET
58 typedef struct sockaddr_in sockaddr_type;
63 static const char *loginpath = "/bin/login";
65 static const char *loginpath;
67 static const char *issuefile = "/etc/issue.net";
69 /* shell name and arguments */
71 static const char *argv_init[] = {NULL, NULL};
73 /* structure that describes a session */
76 #ifdef CONFIG_FEATURE_TELNETD_INETD
77 int sockfd_read, sockfd_write, ptyfd;
78 #else /* CONFIG_FEATURE_TELNETD_INETD */
79 struct tsession *next;
81 #endif /* CONFIG_FEATURE_TELNETD_INETD */
83 /* two circular buffers */
85 int rdidx1, wridx1, size1;
86 int rdidx2, wridx2, size2;
91 This is how the buffers are used. The arrows indicate the movement
94 +-------+ wridx1++ +------+ rdidx1++ +----------+
95 | | <-------------- | buf1 | <-------------- | |
96 | | size1-- +------+ size1++ | |
98 | | rdidx2++ +------+ wridx2++ | |
99 | | --------------> | buf2 | --------------> | |
100 +-------+ size2++ +------+ size2-- +----------+
102 Each session has got two buffers.
108 static struct tsession *sessions;
113 Remove all IAC's from the buffer pointed to by bf (recieved IACs are ignored
114 and must be removed so as to not be interpreted by the terminal). Make an
115 uninterrupted string of characters fit for the terminal. Do this by packing
116 all characters meant for the terminal sequentially towards the end of bf.
118 Return a pointer to the beginning of the characters meant for the terminal.
119 and make *num_totty the number of characters that should be sent to
122 Note - If an IAC (3 byte quantity) starts before (bf + len) but extends
123 past (bf + len) then that IAC will be left unprocessed and *processed will be
126 FIXME - if we mean to send 0xFF to the terminal then it will be escaped,
127 what is the escape character? We aren't handling that situation here.
129 CR-LF ->'s CR mapping is also done here, for convenience
133 remove_iacs(struct tsession *ts, int *pnum_totty) {
134 unsigned char *ptr0 = (unsigned char *)ts->buf1 + ts->wridx1;
135 unsigned char *ptr = ptr0;
136 unsigned char *totty = ptr;
137 unsigned char *end = ptr + MIN(BUFSIZE - ts->wridx1, ts->size1);
145 /* We now map \r\n ==> \r for pragmatic reasons.
146 * Many client implementations send \r\n when
147 * the user hits the CarriageReturn key.
149 if (c == '\r' && (*ptr == '\n' || *ptr == 0) && ptr < end)
154 * TELOPT_NAWS support!
156 if ((ptr+2) >= end) {
157 /* only the beginning of the IAC is in the
158 buffer we were asked to process, we can't
159 process this char. */
164 * IAC -> SB -> TELOPT_NAWS -> 4-byte -> IAC -> SE
166 else if (ptr[1] == SB && ptr[2] == TELOPT_NAWS) {
169 break; /* incomplete, can't process */
170 ws.ws_col = (ptr[3] << 8) | ptr[4];
171 ws.ws_row = (ptr[5] << 8) | ptr[6];
172 (void) ioctl(ts->ptyfd, TIOCSWINSZ, (char *)&ws);
176 /* skip 3-byte IAC non-SB cmd */
178 fprintf(stderr, "Ignoring IAC %s,%s\n",
179 TELCMD(*(ptr+1)), TELOPT(*(ptr+2)));
186 processed = ptr - ptr0;
187 num_totty = totty - ptr0;
188 /* the difference between processed and num_to tty
189 is all the iacs we removed from the stream.
190 Adjust buf1 accordingly. */
191 ts->wridx1 += processed - num_totty;
192 ts->size1 -= processed - num_totty;
193 *pnum_totty = num_totty;
194 /* move the chars meant for the terminal towards the end of the
196 return memmove(ptr - num_totty, ptr0, num_totty);
204 #ifdef CONFIG_FEATURE_DEVPTS
205 p = open("/dev/ptmx", 2);
209 strcpy(line, ptsname(p));
217 strcpy(line, "/dev/ptyXX");
219 for (i = 0; i < 16; i++) {
220 line[8] = "pqrstuvwxyzabcde"[i];
222 if (stat(line, &stb) < 0) {
225 for (j = 0; j < 16; j++) {
226 line[9] = j < 10 ? j + '0' : j - 10 + 'a';
228 fprintf(stderr, "Trying to open device: %s\n", line);
230 if ((p = open(line, O_RDWR | O_NOCTTY)) >= 0) {
236 #endif /* CONFIG_FEATURE_DEVPTS */
242 send_iac(struct tsession *ts, unsigned char command, int option)
244 /* We rely on that there is space in the buffer for now. */
245 char *b = ts->buf2 + ts->rdidx2;
254 static struct tsession *
255 #ifdef CONFIG_FEATURE_TELNETD_INETD
256 make_new_session(void)
257 #else /* CONFIG_FEATURE_TELNETD_INETD */
258 make_new_session(int sockfd)
259 #endif /* CONFIG_FEATURE_TELNETD_INETD */
261 struct termios termbuf;
264 struct tsession *ts = malloc(sizeof(struct tsession) + BUFSIZE * 2);
266 ts->buf1 = (char *)(&ts[1]);
267 ts->buf2 = ts->buf1 + BUFSIZE;
269 #ifdef CONFIG_FEATURE_TELNETD_INETD
271 ts->sockfd_write = 1;
272 #else /* CONFIG_FEATURE_TELNETD_INETD */
274 #endif /* CONFIG_FEATURE_TELNETD_INETD */
276 ts->rdidx1 = ts->wridx1 = ts->size1 = 0;
277 ts->rdidx2 = ts->wridx2 = ts->size2 = 0;
279 /* Got a new connection, set up a tty and spawn a shell. */
281 pty = getpty(tty_name);
284 syslog(LOG_ERR, "All terminals in use!");
293 /* Make the telnet client understand we will echo characters so it
294 * should not do it locally. We don't tell the client to run linemode,
295 * because we want to handle line editing and tab completion and other
296 * stuff that requires char-by-char support.
299 send_iac(ts, DO, TELOPT_ECHO);
300 send_iac(ts, DO, TELOPT_NAWS);
301 send_iac(ts, DO, TELOPT_LFLOW);
302 send_iac(ts, WILL, TELOPT_ECHO);
303 send_iac(ts, WILL, TELOPT_SGA);
305 if ((pid = fork()) < 0) {
306 syslog(LOG_ERR, "Could not fork");
309 /* In child, open the child's side of the tty. */
312 for(i = 0; i <= maxfd; i++)
314 /* make new process group */
317 if (open(tty_name, O_RDWR /*| O_NOCTTY*/) < 0) {
318 syslog(LOG_ERR, "Could not open tty");
324 tcsetpgrp(0, getpid());
326 /* The pseudo-terminal allocated to the client is configured to operate in
327 * cooked mode, and with XTABS CRMOD enabled (see tty(4)).
330 tcgetattr(0, &termbuf);
331 termbuf.c_lflag |= ECHO; /* if we use readline we dont want this */
332 termbuf.c_oflag |= ONLCR|XTABS;
333 termbuf.c_iflag |= ICRNL;
334 termbuf.c_iflag &= ~IXOFF;
335 /*termbuf.c_lflag &= ~ICANON;*/
336 tcsetattr(0, TCSANOW, &termbuf);
338 print_login_issue(issuefile, NULL);
340 /* exec shell, with correct argv and env */
341 execv(loginpath, (char *const *)argv_init);
344 syslog(LOG_ERR, "execv error");
353 #ifndef CONFIG_FEATURE_TELNETD_INETD
355 free_session(struct tsession *ts)
357 struct tsession *t = sessions;
359 /* Unlink this telnet session from the session list. */
368 kill(ts->shell_pid, SIGKILL);
370 wait4(ts->shell_pid, NULL, 0, NULL);
375 if (ts->ptyfd == maxfd || ts->sockfd == maxfd)
377 if (ts->ptyfd == maxfd || ts->sockfd == maxfd)
382 #endif /* CONFIG_FEATURE_TELNETD_INETD */
385 telnetd_main(int argc, char **argv)
387 #ifndef CONFIG_FEATURE_TELNETD_INETD
390 #endif /* CONFIG_FEATURE_TELNETD_INETD */
391 fd_set rdfdset, wrfdset;
393 #ifndef CONFIG_FEATURE_TELNETD_INETD
396 struct in_addr bind_addr = { .s_addr = 0x0 };
397 #endif /* CONFIG_FEATURE_TELNETD_INETD */
399 static const char options[] =
400 #ifdef CONFIG_FEATURE_TELNETD_INETD
402 #else /* CONFIG_EATURE_TELNETD_INETD */
404 #endif /* CONFIG_FEATURE_TELNETD_INETD */
408 loginpath = DEFAULT_SHELL;
412 c = getopt( argc, argv, options);
421 #ifndef CONFIG_FEATURE_TELNETD_INETD
423 portnbr = atoi(optarg);
426 if (inet_aton(optarg, &bind_addr) == 0)
429 #endif /* CONFIG_FEATURE_TELNETD_INETD */
435 if (access(loginpath, X_OK) < 0) {
436 bb_error_msg_and_die ("'%s' unavailable.", loginpath);
439 argv_init[0] = loginpath;
441 openlog(bb_applet_name, 0, LOG_USER);
443 #ifdef CONFIG_FEATURE_TELNETD_INETD
445 sessions = make_new_session();
446 #else /* CONFIG_EATURE_TELNETD_INETD */
449 /* Grab a TCP socket. */
451 master_fd = bb_xsocket(SOCKET_TYPE, SOCK_STREAM, 0);
452 (void)setsockopt(master_fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
454 /* Set it to listen to specified port. */
456 memset((void *)&sa, 0, sizeof(sa));
457 #ifdef CONFIG_FEATURE_IPV6
458 sa.sin6_family = AF_INET6;
459 sa.sin6_port = htons(portnbr);
460 /* sa.sin6_addr = bind_addr6; */
462 sa.sin_family = AF_INET;
463 sa.sin_port = htons(portnbr);
464 sa.sin_addr = bind_addr;
467 bb_xbind(master_fd, (struct sockaddr *) &sa, sizeof(sa));
468 bb_xlisten(master_fd, 1);
472 #endif /* CONFIG_FEATURE_TELNETD_INETD */
480 /* select on the master socket, all telnet sockets and their
481 * ptys if there is room in their respective session buffers.
484 #ifndef CONFIG_FEATURE_TELNETD_INETD
485 FD_SET(master_fd, &rdfdset);
486 #endif /* CONFIG_FEATURE_TELNETD_INETD */
489 #ifndef CONFIG_FEATURE_TELNETD_INETD
491 #endif /* CONFIG_FEATURE_TELNETD_INETD */
492 /* buf1 is used from socket to pty
493 * buf2 is used from pty to socket
496 FD_SET(ts->ptyfd, &wrfdset); /* can write to pty */
498 if (ts->size1 < BUFSIZE) {
499 #ifdef CONFIG_FEATURE_TELNETD_INETD
500 FD_SET(ts->sockfd_read, &rdfdset); /* can read from socket */
501 #else /* CONFIG_FEATURE_TELNETD_INETD */
502 FD_SET(ts->sockfd, &rdfdset); /* can read from socket */
503 #endif /* CONFIG_FEATURE_TELNETD_INETD */
506 #ifdef CONFIG_FEATURE_TELNETD_INETD
507 FD_SET(ts->sockfd_write, &wrfdset); /* can write to socket */
508 #else /* CONFIG_FEATURE_TELNETD_INETD */
509 FD_SET(ts->sockfd, &wrfdset); /* can write to socket */
510 #endif /* CONFIG_FEATURE_TELNETD_INETD */
512 if (ts->size2 < BUFSIZE) {
513 FD_SET(ts->ptyfd, &rdfdset); /* can read from pty */
515 #ifndef CONFIG_FEATURE_TELNETD_INETD
518 #endif /* CONFIG_FEATURE_TELNETD_INETD */
520 selret = select(maxfd + 1, &rdfdset, &wrfdset, 0, 0);
525 #ifndef CONFIG_FEATURE_TELNETD_INETD
526 /* First check for and accept new sessions. */
527 if (FD_ISSET(master_fd, &rdfdset)) {
532 if ((fd = accept(master_fd, (struct sockaddr *)&sa,
536 /* Create a new session and link it into
538 struct tsession *new_ts = make_new_session(fd);
540 new_ts->next = sessions;
550 /* Then check for data tunneling. */
553 while (ts) { /* For all sessions... */
554 #endif /* CONFIG_FEATURE_TELNETD_INETD */
555 #ifndef CONFIG_FEATURE_TELNETD_INETD
556 struct tsession *next = ts->next; /* in case we free ts. */
557 #endif /* CONFIG_FEATURE_TELNETD_INETD */
559 if (ts->size1 && FD_ISSET(ts->ptyfd, &wrfdset)) {
562 /* Write to pty from buffer 1. */
564 ptr = remove_iacs(ts, &num_totty);
566 w = write(ts->ptyfd, ptr, num_totty);
568 #ifdef CONFIG_FEATURE_TELNETD_INETD
570 #else /* CONFIG_FEATURE_TELNETD_INETD */
574 #endif /* CONFIG_FEATURE_TELNETD_INETD */
578 if (ts->wridx1 == BUFSIZE)
582 #ifdef CONFIG_FEATURE_TELNETD_INETD
583 if (ts->size2 && FD_ISSET(ts->sockfd_write, &wrfdset)) {
584 #else /* CONFIG_FEATURE_TELNETD_INETD */
585 if (ts->size2 && FD_ISSET(ts->sockfd, &wrfdset)) {
586 #endif /* CONFIG_FEATURE_TELNETD_INETD */
587 /* Write to socket from buffer 2. */
588 maxlen = MIN(BUFSIZE - ts->wridx2, ts->size2);
589 #ifdef CONFIG_FEATURE_TELNETD_INETD
590 w = write(ts->sockfd_write, ts->buf2 + ts->wridx2, maxlen);
593 #else /* CONFIG_FEATURE_TELNETD_INETD */
594 w = write(ts->sockfd, ts->buf2 + ts->wridx2, maxlen);
600 #endif /* CONFIG_FEATURE_TELNETD_INETD */
603 if (ts->wridx2 == BUFSIZE)
607 #ifdef CONFIG_FEATURE_TELNETD_INETD
608 if (ts->size1 < BUFSIZE && FD_ISSET(ts->sockfd_read, &rdfdset)) {
609 #else /* CONFIG_FEATURE_TELNETD_INETD */
610 if (ts->size1 < BUFSIZE && FD_ISSET(ts->sockfd, &rdfdset)) {
611 #endif /* CONFIG_FEATURE_TELNETD_INETD */
612 /* Read from socket to buffer 1. */
613 maxlen = MIN(BUFSIZE - ts->rdidx1,
614 BUFSIZE - ts->size1);
615 #ifdef CONFIG_FEATURE_TELNETD_INETD
616 r = read(ts->sockfd_read, ts->buf1 + ts->rdidx1, maxlen);
617 if (!r || (r < 0 && errno != EINTR))
619 #else /* CONFIG_FEATURE_TELNETD_INETD */
620 r = read(ts->sockfd, ts->buf1 + ts->rdidx1, maxlen);
621 if (!r || (r < 0 && errno != EINTR)) {
626 #endif /* CONFIG_FEATURE_TELNETD_INETD */
627 if (!*(ts->buf1 + ts->rdidx1 + r - 1)) {
634 if (ts->rdidx1 == BUFSIZE)
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)) {
644 #ifdef CONFIG_FEATURE_TELNETD_INETD
646 #else /* CONFIG_FEATURE_TELNETD_INETD */
650 #endif /* CONFIG_FEATURE_TELNETD_INETD */
654 if (ts->rdidx2 == BUFSIZE)
658 if (ts->size1 == 0) {
662 if (ts->size2 == 0) {
666 #ifndef CONFIG_FEATURE_TELNETD_INETD
669 #endif /* CONFIG_FEATURE_TELNETD_INETD */