]> git.ipfire.org Git - thirdparty/util-linux.git/blobdiff - login-utils/sulogin.c
Merge branch 'mps_losetup_has_device_inline' of https://github.com/marcosps/util...
[thirdparty/util-linux.git] / login-utils / sulogin.c
index fdbda7c8772a7bf53000e892bbea6f250fc3fbae..5f09bd48e4ab9ddc8b7ab7deb703f25d24a3bd89 100644 (file)
@@ -56,6 +56,7 @@
 
 #include "c.h"
 #include "closestream.h"
+#include "env.h"
 #include "nls.h"
 #include "pathnames.h"
 #ifdef USE_PLYMOUTH_SUPPORT
@@ -70,11 +71,11 @@ static unsigned int timeout;
 static int profile;
 static volatile uint32_t openfd;               /* Remember higher file descriptors */
 
-struct sigaction saved_sigint;
-struct sigaction saved_sigtstp;
-struct sigaction saved_sigquit;
-struct sigaction saved_sighup;
-struct sigaction saved_sigchld;
+static struct sigaction saved_sigint;
+static struct sigaction saved_sigtstp;
+static struct sigaction saved_sigquit;
+static struct sigaction saved_sighup;
+static struct sigaction saved_sigchld;
 
 static volatile sig_atomic_t alarm_rised;
 static volatile sig_atomic_t sigchild;
@@ -88,7 +89,7 @@ static volatile sig_atomic_t sigchild;
 # define WEXITED 0
 #endif
 
-static int locked_account_password(const char *passwd)
+static int locked_account_password(const char * const passwd)
 {
        if (passwd && (*passwd == '*' || *passwd == '!'))
                return 1;
@@ -100,9 +101,9 @@ static int locked_account_password(const char *passwd)
  */
 static void tcinit(struct console *con)
 {
-       int mode = 0, flags = 0;
+       int flags = 0, mode = 0;
        struct termios *tio = &con->tio;
-       int fd = con->fd;
+       const int fd = con->fd;
 #ifdef USE_PLYMOUTH_SUPPORT
        struct termios lock;
        int i = (plymouth_command(MAGIC_PING)) ? PLYMOUTH_TERMIOS_FLAGS_DELAY : 0;
@@ -215,26 +216,23 @@ setattr:
  */
 static void tcfinal(struct console *con)
 {
-       struct termios *tio;
-       int fd;
+       struct termios *tio = &con->tio;
+       const int fd = con->fd;
 
        if ((con->flags & CON_SERIAL) == 0) {
-               setenv("TERM", "linux", 1);
+               xsetenv("TERM", "linux", 1);
                return;
        }
        if (con->flags & CON_NOTTY) {
-               setenv("TERM", "dumb", 1);
+               xsetenv("TERM", "dumb", 1);
                return;
        }
 
 #if defined (__s390__) || defined (__s390x__)
-       setenv("TERM", "dumb", 1);
+       xsetenv("TERM", "dumb", 1);
 #else
-       setenv("TERM", "vt102", 1);
+       xsetenv("TERM", "vt102", 1);
 #endif
-       tio = &con->tio;
-       fd = con->fd;
-
        tio->c_iflag |= (IXON | IXOFF);
        tio->c_lflag |= (ICANON | ISIG | ECHO|ECHOE|ECHOK|ECHOKE);
        tio->c_oflag |= OPOST;
@@ -269,11 +267,11 @@ static void tcfinal(struct console *con)
                break;
        case 1:                         /* odd parity */
                tio->c_cflag |= PARODD;
-               /* fall through */
+               /* fallthrough */
        case 2:                         /* even parity */
                tio->c_cflag |= PARENB;
                tio->c_iflag |= (INPCK | ISTRIP);
-               /* fall through */
+               /* fallthrough */
        case (1 | 2):                   /* no parity bit */
                tio->c_cflag &= ~CSIZE;
                tio->c_cflag |= CS7;
@@ -522,8 +520,9 @@ static void doprompt(const char *crypted, struct console *con, int deny)
                tty.c_oflag |= (ONLCR | OPOST);
                tcsetattr(con->fd, TCSADRAIN, &tty);
        }
-       if (con->file == (FILE*)0) {
-               if  ((con->file = fdopen(con->fd, "r+")) == (FILE*)0)
+       if (!con->file) {
+               con->file = fdopen(con->fd, "r+");
+               if (!con->file)
                        goto err;
        }
 
@@ -556,22 +555,17 @@ err:
  */
 static void setup(struct console *con)
 {
-       pid_t pid, pgrp, ppgrp, ttypgrp;
-       int fd;
+       int fd = con->fd;
+       const pid_t pid = getpid(), pgrp = getpgid(0), ppgrp =
+           getpgid(getppid()), ttypgrp = tcgetpgrp(fd);
 
        if (con->flags & CON_NOTTY)
                return;
-       fd = con->fd;
 
        /*
         * Only go through this trouble if the new
         * tty doesn't fall in this process group.
         */
-       pid = getpid();
-       pgrp = getpgid(0);
-       ppgrp = getpgid(getppid());
-       ttypgrp = tcgetpgrp(fd);
-
        if (pgrp != ttypgrp && ppgrp != ttypgrp) {
                if (pid != getsid(0)) {
                        if (pid == getpgid(0))
@@ -607,21 +601,20 @@ static void setup(struct console *con)
  * Ask for the password. Note that there is no default timeout as we normally
  * skip this during boot.
  */
-static char *getpasswd(struct console *con)
+static const char *getpasswd(struct console *con)
 {
        struct sigaction sa;
        struct termios tty;
        static char pass[128], *ptr;
        struct chardata *cp;
-       char *ret = pass;
+       const char *ret = pass;
        unsigned char tc;
        char c, ascval;
        int eightbit;
-       int fd;
+       const int fd = con->fd;
 
        if (con->flags & CON_NOTTY)
                goto out;
-       fd = con->fd;
        cp = &con->cp;
        tty = con->tio;
 
@@ -644,10 +637,14 @@ static char *getpasswd(struct console *con)
        while (cp->eol == '\0') {
                if (read(fd, &c, 1) < 1) {
                        if (errno == EINTR || errno == EAGAIN) {
+                               if (alarm_rised) {
+                                       ret = NULL;
+                                       goto quit;
+                               }
                                xusleep(250000);
                                continue;
                        }
-                       ret = (char*)0;
+                       ret = NULL;
                        switch (errno) {
                        case 0:
                        case EIO:
@@ -694,11 +691,12 @@ static char *getpasswd(struct console *con)
                                ptr--;
                        break;
                case CEOF:
+                       ret = NULL;
                        goto quit;
                default:
                        if ((size_t)(ptr - &pass[0]) >= (sizeof(pass) -1 )) {
                                 fprintf(stderr, "sulogin: input overrun at %s\n\r", con->tty);
-                                ret = (char*)0;
+                                ret = NULL;
                                 goto quit;
                        }
                        *ptr++ = ascval;
@@ -722,8 +720,8 @@ static void sushell(struct passwd *pwd)
 {
        char shell[PATH_MAX];
        char home[PATH_MAX];
-       char *p;
-       char *su_shell;
+       char const *p;
+       char const *su_shell;
 
        /*
         * Set directory and shell.
@@ -759,16 +757,16 @@ static void sushell(struct passwd *pwd)
        if (getcwd(home, sizeof(home)) == NULL)
                strcpy(home, "/");
 
-       setenv("HOME", home, 1);
-       setenv("LOGNAME", "root", 1);
-       setenv("USER", "root", 1);
+       xsetenv("HOME", home, 1);
+       xsetenv("LOGNAME", "root", 1);
+       xsetenv("USER", "root", 1);
        if (!profile)
-               setenv("SHLVL","0",1);
+               xsetenv("SHLVL","0",1);
 
        /*
         * Try to execute a shell.
         */
-       setenv("SHELL", su_shell, 1);
+       xsetenv("SHELL", su_shell, 1);
        unmask_signal(SIGINT, &saved_sigint);
        unmask_signal(SIGTSTP, &saved_sigtstp);
        unmask_signal(SIGQUIT, &saved_sigquit);
@@ -793,13 +791,14 @@ static void sushell(struct passwd *pwd)
        execl(su_shell, shell, NULL);
        warn(_("failed to execute %s"), su_shell);
 
-       setenv("SHELL", "/bin/sh", 1);
+       xsetenv("SHELL", "/bin/sh", 1);
        execl("/bin/sh", profile ? "-sh" : "sh", NULL);
        warn(_("failed to execute %s"), "/bin/sh");
 }
 
-static void usage(FILE *out)
+static void usage(void)
 {
+       FILE *out = stdout;
        fputs(USAGE_HEADER, out);
        fprintf(out, _(
                " %s [options] [tty device]\n"), program_invocation_short_name);
@@ -814,20 +813,18 @@ static void usage(FILE *out)
                out);
 
        fputs(USAGE_SEPARATOR, out);
-       fputs(USAGE_HELP, out);
-       fputs(USAGE_VERSION, out);
-       fprintf(out, USAGE_MAN_TAIL("sulogin(8)"));
+       printf(USAGE_HELP_OPTIONS(26));
+       printf(USAGE_MAN_TAIL("sulogin(8)"));
 }
 
 int main(int argc, char **argv)
 {
-       LIST_HEAD(consoles);
-       struct list_head *ptr;
+       struct list_head *ptr, consoles;
        struct console *con;
        char *tty = NULL;
        struct passwd *pwd;
-       struct timespec sigwait = { .tv_sec = 0, .tv_nsec = 50000000 };
-       siginfo_t status = {};
+       const struct timespec sigwait = { .tv_sec = 0, .tv_nsec = 50000000 };
+       siginfo_t status = { 0 };
        sigset_t set;
        int c, reconnect = 0;
        int opt_e = 0;
@@ -835,14 +832,16 @@ int main(int argc, char **argv)
        pid_t pid;
 
        static const struct option longopts[] = {
-               { "login-shell",  0, 0, 'p' },
-               { "timeout",      1, 0, 't' },
-               { "force",        0, 0, 'e' },
-               { "help",         0, 0, 'h' },
-               { "version",      0, 0, 'V' },
-               { NULL,           0, 0, 0 }
+               { "login-shell",  no_argument,       NULL, 'p' },
+               { "timeout",      required_argument, NULL, 't' },
+               { "force",        no_argument,       NULL, 'e' },
+               { "help",         no_argument,       NULL, 'h' },
+               { "version",      no_argument,       NULL, 'V' },
+               { NULL, 0, NULL, 0 }
        };
 
+       INIT_LIST_HEAD(&consoles);
+
        /*
         * If we are init we need to set up a own session.
         */
@@ -874,11 +873,10 @@ int main(int argc, char **argv)
                        printf(UTIL_LINUX_VERSION);
                        return EXIT_SUCCESS;
                case 'h':
-                       usage(stdout);
+                       usage();
                        return EXIT_SUCCESS;
                default:
-                       usage(stderr);
-                       /* Do not exit! */
+                       /* Do not exit! getopt prints a warning. */
                        break;
                }
        }
@@ -973,13 +971,12 @@ int main(int argc, char **argv)
                switch ((con->pid = fork())) {
                case 0:
                        mask_signal(SIGCHLD, SIG_DFL, NULL);
-                       /* fall through */
                nofork:
                        setup(con);
                        while (1) {
                                const char *passwd = pwd->pw_passwd;
                                const char *answer;
-                               int failed = 0, doshell = 0;
+                               int doshell = 0;
                                int deny = !opt_e && locked_account_password(pwd->pw_passwd);
 
                                doprompt(passwd, con, deny);
@@ -1002,16 +999,14 @@ int main(int argc, char **argv)
                                }
 
                                if (doshell) {
+                                       /* sushell() unmask signals */
                                        sushell(pwd);
-                                       failed++;
-                               }
 
-                               mask_signal(SIGQUIT, SIG_IGN, &saved_sigquit);
-                               mask_signal(SIGTSTP, SIG_IGN, &saved_sigtstp);
-                               mask_signal(SIGINT,  SIG_IGN, &saved_sigint);
+                                       mask_signal(SIGQUIT, SIG_IGN, &saved_sigquit);
+                                       mask_signal(SIGTSTP, SIG_IGN, &saved_sigtstp);
+                                       mask_signal(SIGINT,  SIG_IGN, &saved_sigint);
 
-                               if (failed) {
-                                       fprintf(stderr, _("Can not execute su shell\n\n"));
+                                       fprintf(stderr, _("cannot execute su shell\n\n"));
                                        break;
                                }
                                fprintf(stderr, _("Login incorrect\n\n"));
@@ -1026,7 +1021,7 @@ int main(int argc, char **argv)
                        exit(0);
                case -1:
                        warn(_("fork failed"));
-                       /* fall through */
+                       /* fallthrough */
                default:
                        break;
                }
@@ -1050,7 +1045,7 @@ int main(int argc, char **argv)
                                continue;
                }
 
-               errx(EXIT_FAILURE, _("Can not wait on su shell\n\n"));
+               errx(EXIT_FAILURE, _("cannot wait on su shell\n\n"));
 
        } while (1);