#include "c.h"
#include "closestream.h"
+#include "env.h"
#include "nls.h"
#include "pathnames.h"
+#ifdef USE_PLYMOUTH_SUPPORT
+# include "plymouth-ctrl.h"
+#endif
#include "strutils.h"
#include "ttyutils.h"
#include "sulogin-consoles.h"
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;
# define IUCLC 0
#endif
-static int locked_account_password(const char *passwd)
+#ifndef WEXITED
+# warning "WEXITED is missing, sulogin may not work as expected"
+# define WEXITED 0
+#endif
+
+static int locked_account_password(const char * const passwd)
{
if (passwd && (*passwd == '*' || *passwd == '!'))
return 1;
return 0;
}
-#ifdef TIOCGLCKTRMIOS
-/*
- * For the case plymouth is found on this system
- */
-static int plymouth_command(const char* arg)
-{
- const char *cmd = "/usr/bin/plymouth";
- static int has_plymouth = 1;
- pid_t pid;
-
- if (!has_plymouth)
- return 127;
-
- pid = fork();
- if (!pid) {
- int fd = open("/dev/null", O_RDWR);
- if (fd < 0)
- exit(127);
- dup2(fd, 0);
- dup2(fd, 1);
- dup2(fd, 2);
- if (fd > 2)
- close(fd);
- execl(cmd, cmd, arg, (char *) NULL);
- exit(127);
- } else if (pid > 0) {
- int status;
- waitpid(pid, &status, 0);
- if (status == 127)
- has_plymouth = 0;
- return status;
- }
- return 1;
-}
-#endif
-
/*
* Fix the tty modes and set reasonable defaults.
*/
static void tcinit(struct console *con)
{
- int mode = 0, flags = 0;
+ int flags = 0, mode = 0;
struct termios *tio = &con->tio;
+ const int fd = con->fd;
+#ifdef USE_PLYMOUTH_SUPPORT
struct termios lock;
- int fd = con->fd;
-#ifdef TIOCGLCKTRMIOS
- int i = (plymouth_command("--ping")) ? 20 : 0;
-
+ int i = (plymouth_command(MAGIC_PING)) ? PLYMOUTH_TERMIOS_FLAGS_DELAY : 0;
+ if (i)
+ plymouth_command(MAGIC_QUIT);
while (i-- > 0) {
/*
* With plymouth the termios flags become changed after this
break;
if (!lock.c_iflag && !lock.c_oflag && !lock.c_cflag && !lock.c_lflag)
break;
- if (i == 15 && plymouth_command("quit") != 0)
- break;
sleep(1);
}
memset(&lock, 0, sizeof(struct termios));
ioctl(fd, TIOCSLCKTRMIOS, &lock);
#endif
-
errno = 0;
if (tcgetattr(fd, tio) < 0) {
*/
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;
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;
warnx(_("%s: no entry for root"), _PATH_SHADOW_PASSWD);
*pwd.pw_passwd = '\0';
}
- /* locked accont passwords are valid too */
+ /* locked account passwords are valid too */
if (!locked_account_password(pwd.pw_passwd) && !valid(pwd.pw_passwd)) {
warnx(_("%s: root password garbled"), _PATH_SHADOW_PASSWD);
*pwd.pw_passwd = '\0';
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;
}
*/
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))
* 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;
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:
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;
alarm(0);
if (tc)
tcsetattr(fd, TCSAFLUSH, &con->tio);
- if (ret && *ret != '\0')
- tcfinal(con);
+ tcfinal(con);
printf("\r\n");
out:
return ret;
{
char shell[PATH_MAX];
char home[PATH_MAX];
- char *p;
- char *su_shell;
+ char const *p;
+ char const *su_shell;
/*
* Set directory and shell.
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);
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);
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 = {};
- sigset_t set = {};
+ 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;
int wait = 0;
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.
*/
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;
}
}
reconnect = detect_consoles(tty, STDIN_FILENO, &consoles);
/*
- * If previous stdin was not the speified tty and therefore reconnected
+ * If previous stdin was not the specified tty and therefore reconnected
* to the specified tty also reconnect stdout and stderr.
*/
if (reconnect) {
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);
}
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"));
exit(0);
case -1:
warn(_("fork failed"));
- /* fall through */
+ /* fallthrough */
default:
break;
}
continue;
}
- errx(EXIT_FAILURE, _("Can not wait on su shell\n\n"));
+ errx(EXIT_FAILURE, _("cannot wait on su shell\n\n"));
} while (1);
}
signum = sigtimedwait(&set, NULL, &sigwait);
- if (signum != SIGCHLD) {
- if (signum < 0 && errno == EAGAIN)
- break;
- }
+ if (signum != SIGCHLD && signum < 0 && errno == EAGAIN)
+ break;
} while (1);