123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891 |
- #define DEBUG 0
- #include "libbb.h"
- #include "common_bufsiz.h"
- #include <syslog.h>
- #if DEBUG
- # define TELCMDS
- # define TELOPTS
- #endif
- #include <arpa/telnet.h>
- struct tsession {
- struct tsession *next;
- pid_t shell_pid;
- int sockfd_read;
- int sockfd_write;
- int ptyfd;
- smallint buffered_IAC_for_pty;
-
-
- #define TS_BUF1(ts) ((unsigned char*)(ts + 1))
- #define TS_BUF2(ts) (((unsigned char*)(ts + 1)) + BUFSIZE)
- int rdidx1, wridx1, size1;
- int rdidx2, wridx2, size2;
- };
- enum { BUFSIZE = (4 * 1024 - sizeof(struct tsession)) / 2 };
- struct globals {
- struct tsession *sessions;
- const char *loginpath;
- const char *issuefile;
- int maxfd;
- } FIX_ALIASING;
- #define G (*(struct globals*)bb_common_bufsiz1)
- #define INIT_G() do { \
- setup_common_bufsiz(); \
- G.loginpath = "/bin/login"; \
- G.issuefile = "/etc/issue.net"; \
- } while (0)
- static ssize_t
- safe_write_to_pty_decode_iac(struct tsession *ts)
- {
- unsigned wr;
- ssize_t rc;
- unsigned char *buf;
- unsigned char *found;
- buf = TS_BUF1(ts) + ts->wridx1;
- wr = MIN(BUFSIZE - ts->wridx1, ts->size1);
-
- if (ts->buffered_IAC_for_pty) {
-
- ts->buffered_IAC_for_pty = 0;
- wr++;
- ts->size1++;
- buf--;
- ts->wridx1--;
- goto handle_iac;
- }
- found = memchr(buf, IAC, wr);
- if (found != buf) {
-
- if (found)
- wr = found - buf;
-
- rc = wr;
- found = memchr(buf, '\r', wr);
- if (found)
- rc = found - buf + 1;
- rc = safe_write(ts->ptyfd, buf, rc);
- if (rc <= 0)
- return rc;
- if (rc < wr
- && buf[rc-1] == '\r'
- && (buf[rc] == '\n' || buf[rc] == '\0')
- ) {
- rc++;
- }
- goto update_and_return;
- }
-
- if (wr <= 1) {
-
-
- ts->buffered_IAC_for_pty = 1;
- rc = 1;
- goto update_and_return;
- }
- handle_iac:
-
- if (buf[1] == IAC) {
-
-
- rc = safe_write(ts->ptyfd, &buf[1], 1);
-
- #if 0
- if (rc <= 0)
- return rc;
- #endif
- rc = 2;
- goto update_and_return;
- }
- if (buf[1] >= 240 && buf[1] <= 249) {
-
-
- rc = 2;
- goto update_and_return;
- }
- if (wr <= 2) {
- rc = 2;
- goto update_and_return;
- }
- if (buf[1] == SB) {
- if (buf[2] == TELOPT_NAWS) {
-
- struct winsize ws;
- if (wr <= 6) {
- rc = wr;
- goto update_and_return;
- }
- memset(&ws, 0, sizeof(ws));
- ws.ws_col = (buf[3] << 8) | buf[4];
- ws.ws_row = (buf[5] << 8) | buf[6];
- ioctl(ts->ptyfd, TIOCSWINSZ, (char *)&ws);
- rc = 7;
-
- goto update_and_return;
- }
-
- }
-
- #if DEBUG
- fprintf(stderr, "Ignoring IAC %s,%s\n",
- TELCMD(buf[1]), TELOPT(buf[2]));
- #endif
- rc = 3;
- update_and_return:
- ts->wridx1 += rc;
- if (ts->wridx1 >= BUFSIZE)
- ts->wridx1 = 0;
- ts->size1 -= rc;
-
- if (ts->size1 == 0) {
-
- ts->rdidx1 = 0;
- ts->wridx1 = 0;
- return rc;
- }
- wr = ts->wridx1;
- if (wr != 0 && wr < ts->rdidx1) {
-
-
- memmove(TS_BUF1(ts), TS_BUF1(ts) + wr, ts->size1);
- ts->rdidx1 -= wr;
- ts->wridx1 = 0;
- }
- return rc;
- }
- static size_t safe_write_double_iac(int fd, const char *buf, size_t count)
- {
- const char *IACptr;
- size_t wr, rc, total;
- total = 0;
- while (1) {
- if (count == 0)
- return total;
- if (*buf == (char)IAC) {
- static const char IACIAC[] ALIGN1 = { IAC, IAC };
- rc = safe_write(fd, IACIAC, 2);
- if (rc != 2)
- break;
- buf++;
- total++;
- count--;
- continue;
- }
-
- IACptr = memchr(buf, IAC, count);
- wr = count;
- if (IACptr)
- wr = IACptr - buf;
- rc = safe_write(fd, buf, wr);
- if (rc != wr)
- break;
- buf += rc;
- total += rc;
- count -= rc;
- }
-
- if ((ssize_t)rc < 0) {
- if (total == 0)
- return rc;
- rc = 0;
- }
- return total + rc;
- }
- enum {
- OPT_WATCHCHILD = (1 << 2),
- OPT_INETD = (1 << 3) * ENABLE_FEATURE_TELNETD_STANDALONE,
- OPT_PORT = (1 << 4) * ENABLE_FEATURE_TELNETD_STANDALONE,
- OPT_FOREGROUND = (1 << 6) * ENABLE_FEATURE_TELNETD_STANDALONE,
- OPT_SYSLOG = (1 << 7) * ENABLE_FEATURE_TELNETD_INETD_WAIT,
- OPT_WAIT = (1 << 8) * ENABLE_FEATURE_TELNETD_INETD_WAIT,
- };
- static struct tsession *
- make_new_session(
- IF_FEATURE_TELNETD_STANDALONE(int sock)
- IF_NOT_FEATURE_TELNETD_STANDALONE(void)
- ) {
- #if !ENABLE_FEATURE_TELNETD_STANDALONE
- enum { sock = 0 };
- #endif
- const char *login_argv[2];
- struct termios termbuf;
- int fd, pid;
- char tty_name[GETPTY_BUFSIZE];
- struct tsession *ts = xzalloc(sizeof(struct tsession) + BUFSIZE * 2);
-
-
-
- fd = xgetpty(tty_name);
- if (fd > G.maxfd)
- G.maxfd = fd;
- ts->ptyfd = fd;
- ndelay_on(fd);
- close_on_exec_on(fd);
-
- setsockopt_keepalive(sock);
- #if ENABLE_FEATURE_TELNETD_STANDALONE
- ts->sockfd_read = sock;
- ndelay_on(sock);
- if (sock == 0) {
- sock++;
- ndelay_on(sock);
- }
- ts->sockfd_write = sock;
- if (sock > G.maxfd)
- G.maxfd = sock;
- #else
-
- ts->sockfd_write = 1;
- ndelay_on(0);
- ndelay_on(1);
- #endif
-
- {
- static const char iacs_to_send[] ALIGN1 = {
- IAC, DO, TELOPT_ECHO,
- IAC, DO, TELOPT_NAWS,
-
-
- IAC, WILL, TELOPT_ECHO,
- IAC, WILL, TELOPT_SGA
- };
-
-
-
-
-
- #if ENABLE_FEATURE_TELNETD_STANDALONE
- safe_write(sock, iacs_to_send, sizeof(iacs_to_send));
- #else
- safe_write(1, iacs_to_send, sizeof(iacs_to_send));
- #endif
-
-
- }
- fflush_all();
- pid = vfork();
- if (pid < 0) {
- free(ts);
- close(fd);
-
- bb_perror_msg("vfork");
- return NULL;
- }
- if (pid > 0) {
-
- ts->shell_pid = pid;
- return ts;
- }
-
-
-
- bb_signals((1 << SIGCHLD) + (1 << SIGPIPE), SIG_DFL);
- pid = getpid();
- if (ENABLE_FEATURE_UTMP) {
- len_and_sockaddr *lsa = get_peer_lsa(sock);
- char *hostname = NULL;
- if (lsa) {
- hostname = xmalloc_sockaddr2dotted(&lsa->u.sa);
- free(lsa);
- }
- write_new_utmp(pid, LOGIN_PROCESS, tty_name, "LOGIN", hostname);
- free(hostname);
- }
-
- setsid();
-
-
- close(0);
- xopen(tty_name, O_RDWR);
- xdup2(0, 1);
- xdup2(0, 2);
- tcsetpgrp(0, pid);
-
- tcgetattr(0, &termbuf);
- termbuf.c_lflag |= ECHO;
- termbuf.c_oflag |= ONLCR | XTABS;
- termbuf.c_iflag |= ICRNL;
- termbuf.c_iflag &= ~IXOFF;
-
- tcsetattr_stdin_TCSANOW(&termbuf);
-
- print_login_issue(G.issuefile, tty_name);
-
- login_argv[0] = G.loginpath;
- login_argv[1] = NULL;
-
- BB_EXECVP(G.loginpath, (char **)login_argv);
-
- _exit(EXIT_FAILURE);
- }
- #if ENABLE_FEATURE_TELNETD_STANDALONE
- static void
- free_session(struct tsession *ts)
- {
- struct tsession *t;
- if (option_mask32 & OPT_INETD)
- exit(EXIT_SUCCESS);
-
- t = G.sessions;
- if (t == ts)
- G.sessions = ts->next;
- else {
- while (t->next != ts)
- t = t->next;
- t->next = ts->next;
- }
- #if 0
-
- kill(ts->shell_pid, SIGKILL);
- waitpid(ts->shell_pid, NULL, 0);
- #endif
- close(ts->ptyfd);
- close(ts->sockfd_read);
-
- free(ts);
-
- G.maxfd = 0;
- ts = G.sessions;
- while (ts) {
- if (G.maxfd < ts->ptyfd)
- G.maxfd = ts->ptyfd;
- if (G.maxfd < ts->sockfd_read)
- G.maxfd = ts->sockfd_read;
- #if 0
-
- if (G.maxfd < ts->sockfd_write)
- G.maxfd = ts->sockfd_write;
- #endif
- ts = ts->next;
- }
- }
- #else
- #define free_session(ts) return 0
- #endif
- static void handle_sigchld(int sig UNUSED_PARAM)
- {
- pid_t pid;
- struct tsession *ts;
- int save_errno = errno;
-
- while (1) {
- pid = wait_any_nohang(NULL);
- if (pid <= 0)
- break;
- ts = G.sessions;
- while (ts) {
- if (ts->shell_pid == pid) {
- ts->shell_pid = -1;
- update_utmp_DEAD_PROCESS(pid);
- break;
- }
- ts = ts->next;
- }
- }
- errno = save_errno;
- }
- int telnetd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
- int telnetd_main(int argc UNUSED_PARAM, char **argv)
- {
- fd_set rdfdset, wrfdset;
- unsigned opt;
- int count;
- struct tsession *ts;
- #if ENABLE_FEATURE_TELNETD_STANDALONE
- #define IS_INETD (opt & OPT_INETD)
- int master_fd = master_fd;
- int sec_linger = sec_linger;
- char *opt_bindaddr = NULL;
- char *opt_portnbr;
- #else
- enum {
- IS_INETD = 1,
- master_fd = -1,
- };
- #endif
- INIT_G();
-
- opt = getopt32(argv, "^"
- "f:l:Ki"
- IF_FEATURE_TELNETD_STANDALONE("p:b:F")
- IF_FEATURE_TELNETD_INETD_WAIT("Sw:+")
- "\0"
-
- IF_FEATURE_TELNETD_INETD_WAIT("wF:i--w:w--i"),
- &G.issuefile, &G.loginpath
- IF_FEATURE_TELNETD_STANDALONE(, &opt_portnbr, &opt_bindaddr)
- IF_FEATURE_TELNETD_INETD_WAIT(, &sec_linger)
- );
- if (!IS_INETD ) {
-
-
- if (!(opt & OPT_FOREGROUND)) {
-
- bb_daemonize_or_rexec(0 , argv);
- }
- }
-
- if (IS_INETD || (opt & OPT_SYSLOG) || !(opt & OPT_FOREGROUND)) {
- openlog(applet_name, LOG_PID, LOG_DAEMON);
- logmode = LOGMODE_SYSLOG;
- }
- #if ENABLE_FEATURE_TELNETD_STANDALONE
- if (IS_INETD) {
- G.sessions = make_new_session(0);
- if (!G.sessions)
- return 1;
- } else {
- master_fd = 0;
- if (!(opt & OPT_WAIT)) {
- unsigned portnbr = 23;
- if (opt & OPT_PORT)
- portnbr = xatou16(opt_portnbr);
- master_fd = create_and_bind_stream_or_die(opt_bindaddr, portnbr);
- xlisten(master_fd, 1);
- }
- close_on_exec_on(master_fd);
- }
- #else
- G.sessions = make_new_session();
- if (!G.sessions)
- return 1;
- #endif
-
- signal(SIGPIPE, SIG_IGN);
- if (opt & OPT_WATCHCHILD)
- signal(SIGCHLD, handle_sigchld);
- else
- signal(SIGCHLD, SIG_IGN);
- again:
- FD_ZERO(&rdfdset);
- FD_ZERO(&wrfdset);
-
- ts = G.sessions;
- while (ts) {
- struct tsession *next = ts->next;
- if (ts->shell_pid == -1) {
-
- free_session(ts);
- } else {
- if (ts->size1 > 0)
- FD_SET(ts->ptyfd, &wrfdset);
- if (ts->size1 < BUFSIZE)
- FD_SET(ts->sockfd_read, &rdfdset);
- if (ts->size2 > 0)
- FD_SET(ts->sockfd_write, &wrfdset);
- if (ts->size2 < BUFSIZE)
- FD_SET(ts->ptyfd, &rdfdset);
- }
- ts = next;
- }
- if (!IS_INETD) {
- FD_SET(master_fd, &rdfdset);
-
- if (master_fd > G.maxfd)
- G.maxfd = master_fd;
- }
- {
- struct timeval *tv_ptr = NULL;
- #if ENABLE_FEATURE_TELNETD_INETD_WAIT
- struct timeval tv;
- if ((opt & OPT_WAIT) && !G.sessions) {
- tv.tv_sec = sec_linger;
- tv.tv_usec = 0;
- tv_ptr = &tv;
- }
- #endif
- count = select(G.maxfd + 1, &rdfdset, &wrfdset, NULL, tv_ptr);
- }
- if (count == 0)
- return 0;
- if (count < 0)
- goto again;
- #if ENABLE_FEATURE_TELNETD_STANDALONE
-
- if (!IS_INETD && FD_ISSET(master_fd, &rdfdset)) {
- int fd;
- struct tsession *new_ts;
- fd = accept(master_fd, NULL, NULL);
- if (fd < 0)
- goto again;
- close_on_exec_on(fd);
-
- new_ts = make_new_session(fd);
- if (new_ts) {
- new_ts->next = G.sessions;
- G.sessions = new_ts;
- } else {
- close(fd);
- }
- }
- #endif
-
- ts = G.sessions;
- while (ts) {
- struct tsession *next = ts->next;
- if ( FD_ISSET(ts->ptyfd, &wrfdset)) {
-
- count = safe_write_to_pty_decode_iac(ts);
- if (count < 0) {
- if (errno == EAGAIN)
- goto skip1;
- goto kill_session;
- }
- }
- skip1:
- if ( FD_ISSET(ts->sockfd_write, &wrfdset)) {
-
- count = MIN(BUFSIZE - ts->wridx2, ts->size2);
- count = safe_write_double_iac(ts->sockfd_write, (void*)(TS_BUF2(ts) + ts->wridx2), count);
- if (count < 0) {
- if (errno == EAGAIN)
- goto skip2;
- goto kill_session;
- }
- ts->wridx2 += count;
- if (ts->wridx2 >= BUFSIZE)
- ts->wridx2 = 0;
- ts->size2 -= count;
- if (ts->size2 == 0) {
- ts->rdidx2 = 0;
- ts->wridx2 = 0;
- }
- }
- skip2:
- if ( FD_ISSET(ts->sockfd_read, &rdfdset)) {
-
- count = MIN(BUFSIZE - ts->rdidx1, BUFSIZE - ts->size1);
- count = safe_read(ts->sockfd_read, TS_BUF1(ts) + ts->rdidx1, count);
- if (count <= 0) {
- if (count < 0 && errno == EAGAIN)
- goto skip3;
- goto kill_session;
- }
-
- if (!TS_BUF1(ts)[ts->rdidx1 + count - 1]) {
- --count;
- }
- ts->size1 += count;
- ts->rdidx1 += count;
- if (ts->rdidx1 >= BUFSIZE)
- ts->rdidx1 = 0;
- }
- skip3:
- if ( FD_ISSET(ts->ptyfd, &rdfdset)) {
-
- count = MIN(BUFSIZE - ts->rdidx2, BUFSIZE - ts->size2);
- count = safe_read(ts->ptyfd, TS_BUF2(ts) + ts->rdidx2, count);
- if (count <= 0) {
- if (count < 0 && errno == EAGAIN)
- goto skip4;
- goto kill_session;
- }
- ts->size2 += count;
- ts->rdidx2 += count;
- if (ts->rdidx2 >= BUFSIZE)
- ts->rdidx2 = 0;
- }
- skip4:
- ts = next;
- continue;
- kill_session:
- if (ts->shell_pid > 0)
- update_utmp_DEAD_PROCESS(ts->shell_pid);
- free_session(ts);
- ts = next;
- }
- goto again;
- }
|