zcip: fix unaligned trap on ARM
[oweals/busybox.git] / networking / telnetd.c
index 549488507127e2c1cb04858729df8444c6f04fd5..b21991212cacd05fa56ffc15758b45f33c4964b1 100644 (file)
  *     Set process group corrections, initial busybox port
  */
 
-/*#define DEBUG 1 */
 #define DEBUG 0
 
-#include "busybox.h"
+#include "libbb.h"
 
 #if DEBUG
 #define TELCMDS
 #include <sys/syslog.h>
 
 
-#define BUFSIZE 4000
-
-#if ENABLE_FEATURE_IPV6
-#define SOCKET_TYPE    AF_INET6
-typedef struct sockaddr_in6 sockaddr_type;
-#else
-#define SOCKET_TYPE    AF_INET
-typedef struct sockaddr_in sockaddr_type;
-#endif
-
 #if ENABLE_LOGIN
 static const char *loginpath = "/bin/login";
 #else
@@ -52,10 +41,6 @@ static const char *loginpath = DEFAULT_SHELL;
 
 static const char *issuefile = "/etc/issue.net";
 
-/* shell name and arguments */
-
-static const char *argv_init[2];
-
 /* structure that describes a session */
 
 struct tsession {
@@ -68,6 +53,10 @@ struct tsession {
        int rdidx2, wridx2, size2;
 };
 
+/* Two buffers are directly after tsession in malloced memory.
+ * Make whole thing fit in 4k */
+enum { BUFSIZE = (4*1024 - sizeof(struct tsession)) / 2 };
+
 /*
    This is how the buffers are used. The arrows indicate the movement
    of data.
@@ -239,6 +228,7 @@ make_new_session(
                USE_FEATURE_TELNETD_STANDALONE(int sock_r, int sock_w)
                SKIP_FEATURE_TELNETD_STANDALONE(void)
 ) {
+       const char *login_argv[2];
        struct termios termbuf;
        int fd, pid;
        char tty_name[32];
@@ -288,18 +278,21 @@ make_new_session(
                ts->shell_pid = pid;
                return ts;
        }
-       
+
        /* child */
 
+       /* make new session and process group */
+       setsid();
+
        /* open the child's side of the tty. */
-       fd = xopen(tty_name, O_RDWR /*| O_NOCTTY*/);
+       /* NB: setsid() disconnects from any previous ctty's. Therefore
+        * we must open child's side of the tty AFTER setsid! */
+       fd = xopen(tty_name, O_RDWR); /* becomes our ctty */
        dup2(fd, 0);
        dup2(fd, 1);
        dup2(fd, 2);
        while (fd > 2) close(fd--);
-       /* make new process group */
-       setsid();
-       tcsetpgrp(0, getpid());
+       tcsetpgrp(0, getpid()); /* switch this tty's process group to us */
 
        /* The pseudo-terminal allocated to the client is configured to operate in
         * cooked mode, and with XTABS CRMOD enabled (see tty(4)). */
@@ -313,9 +306,12 @@ make_new_session(
 
        print_login_issue(issuefile, NULL);
 
-       /* exec shell, with correct argv and env */
-       execv(loginpath, (char *const *)argv_init);
-       bb_perror_msg_and_die("execv");
+       /* exec shell / login /whatever */
+       login_argv[0] = loginpath;
+       login_argv[1] = NULL;
+       execv(loginpath, (char **)login_argv);
+       /* Hmmm... this gets sent to the client thru fd#2! Is it ok?? */
+       bb_perror_msg_and_die("execv %s", loginpath);
 }
 
 #if ENABLE_FEATURE_TELNETD_STANDALONE
@@ -343,7 +339,7 @@ free_session(struct tsession *ts)
        free(ts);
 
        /* scan all sessions and find new maxfd */
-        ts = sessions;
+       ts = sessions;
        maxfd = 0;
        while (ts) {
                if (maxfd < ts->ptyfd)
@@ -356,47 +352,16 @@ free_session(struct tsession *ts)
        }
 }
 
-static int
-create_socket(int port, const char *opt_bindaddr)
-{
-       static const int on = 1;
-       int fd;
-       sockaddr_type sa;
-#if !ENABLE_FEATURE_IPV6
-       struct in_addr bind_addr = { .s_addr = 0x0 };
-
-       /* TODO: generic string -> sockaddr converter */
-       if (opt_bindaddr && inet_aton(opt_bindaddr, &bind_addr) == 0)
-               bb_show_usage();
-#endif
-       fd = xsocket(SOCKET_TYPE, SOCK_STREAM, 0);
-       setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
-       memset((void *)&sa, 0, sizeof(sa));
-#if ENABLE_FEATURE_IPV6
-       sa.sin6_family = AF_INET6;
-       sa.sin6_port = htons(port);
-       /* sa.sin6_addr = bind_addr6; */
-#else
-       sa.sin_family = AF_INET;
-       sa.sin_port = htons(port);
-       sa.sin_addr = bind_addr;
-#endif
-       xbind(fd, (struct sockaddr *) &sa, sizeof(sa));
-       xlisten(fd, 1);
-       return fd;
-}
-
 #else /* !FEATURE_TELNETD_STANDALONE */
 
 /* Never actually called */
 void free_session(struct tsession *ts);
-int create_socket(int port, const char *opt_bindaddr);
 
 #endif
 
 
-int
-telnetd_main(int argc, char **argv)
+int telnetd_main(int argc, char **argv);
+int telnetd_main(int argc, char **argv)
 {
        fd_set rdfdset, wrfdset;
        unsigned opt;
@@ -421,7 +386,7 @@ telnetd_main(int argc, char **argv)
                OPT_INETD = 0x20 * ENABLE_FEATURE_TELNETD_STANDALONE,
        };
 
-       opt = getopt32(argc, argv, "f:l:" USE_FEATURE_TELNETD_STANDALONE("p:b:Fi"),
+       opt = getopt32(argv, "f:l:" USE_FEATURE_TELNETD_STANDALONE("p:b:Fi"),
                        &issuefile, &loginpath
                        USE_FEATURE_TELNETD_STANDALONE(, &opt_portnbr, &opt_bindaddr));
        /* Redirect log to syslog early, if needed */
@@ -441,15 +406,15 @@ telnetd_main(int argc, char **argv)
 
        /* Used to check access(loginpath, X_OK) here. Pointless.
         * exec will do this for us for free later. */
-       argv_init[0] = loginpath;
 
 #if ENABLE_FEATURE_TELNETD_STANDALONE
        if (IS_INETD) {
                sessions = make_new_session(0, 1);
        } else {
-               master_fd = create_socket(portnbr, opt_bindaddr);
+               master_fd = create_and_bind_stream_or_die(opt_bindaddr, portnbr);
+               xlisten(master_fd, 1);
                if (!(opt & OPT_FOREGROUND))
-                       xdaemon(0, 0);
+                       bb_daemonize(DAEMON_CHDIR_ROOT);
        }
 #else
        sessions = make_new_session();
@@ -494,13 +459,10 @@ telnetd_main(int argc, char **argv)
 #if ENABLE_FEATURE_TELNETD_STANDALONE
        /* First check for and accept new sessions. */
        if (!IS_INETD && FD_ISSET(master_fd, &rdfdset)) {
-               sockaddr_type sa;
                int fd;
-               socklen_t salen;
                struct tsession *new_ts;
 
-               salen = sizeof(sa);
-               fd = accept(master_fd, (struct sockaddr *)&sa, &salen);
+               fd = accept(master_fd, NULL, 0);
                if (fd < 0)
                        goto again;
                /* Create a new session and link it into our active list */