busybox/loginutils/sulogin.c

126 lines
3.3 KiB
C
Raw Normal View History

/* vi: set sw=4 ts=4: */
/*
2006-09-08 22:52:05 +05:30
* Mini sulogin implementation for busybox
*
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
*/
//config:config SULOGIN
//config: bool "sulogin (17 kb)"
//config: default y
//config: select FEATURE_SYSLOG
//config: help
//config: sulogin is invoked when the system goes into single user
//config: mode (this is done through an entry in inittab).
//applet:IF_SULOGIN(APPLET_NOEXEC(sulogin, sulogin, BB_DIR_SBIN, BB_SUID_DROP, sulogin))
//kbuild:lib-$(CONFIG_SULOGIN) += sulogin.o
//usage:#define sulogin_trivial_usage
//usage: "[-t N] [TTY]"
//usage:#define sulogin_full_usage "\n\n"
//usage: "Single user login\n"
//usage: "\n -p Start a login shell"
//usage: "\n -t SEC Timeout"
#include "libbb.h"
#include <syslog.h>
int sulogin_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
2008-07-05 14:48:54 +05:30
int sulogin_main(int argc UNUSED_PARAM, char **argv)
{
int tsid;
int timeout = 0;
unsigned opts;
2006-09-08 22:52:05 +05:30
struct passwd *pwd;
const char *shell;
2006-01-25 05:38:53 +05:30
/* Note: sulogin is not a suid app. It is meant to be run by init
* for single user / emergency mode. init starts it as root.
* Normal users (potentially malicious ones) can only run it under
* their UID, therefore no paranoia here is warranted:
* $LD_LIBRARY_PATH in env, TTY = /dev/sda
* are no more dangerous here than in e.g. cp applet.
*/
2006-09-09 19:30:58 +05:30
logmode = LOGMODE_BOTH;
2006-10-04 02:30:43 +05:30
openlog(applet_name, 0, LOG_AUTH);
2006-01-25 05:38:53 +05:30
opts = getopt32(argv, "pt:+", &timeout);
argv += optind;
2006-09-08 22:52:05 +05:30
if (argv[0]) {
2006-09-08 22:52:05 +05:30
close(0);
close(1);
dup(xopen(argv[0], O_RDWR));
close(2);
2006-09-08 22:52:05 +05:30
dup(0);
}
2006-09-08 22:52:05 +05:30
pwd = getpwuid(0);
if (!pwd) {
libbb: reduce the overhead of single parameter bb_error_msg() calls Back in 2007, commit 0c97c9d43707 ("'simple' error message functions by Loic Grenie") introduced bb_simple_perror_msg() to allow for a lower overhead call to bb_perror_msg() when only a string was being printed with no parameters. This saves space for some CPU architectures because it avoids the overhead of a call to a variadic function. However there has never been a simple version of bb_error_msg(), and since 2007 many new calls to bb_perror_msg() have been added that only take a single parameter and so could have been using bb_simple_perror_message(). This changeset introduces 'simple' versions of bb_info_msg(), bb_error_msg(), bb_error_msg_and_die(), bb_herror_msg() and bb_herror_msg_and_die(), and replaces all calls that only take a single parameter, or use something like ("%s", arg), with calls to the corresponding 'simple' version. Since it is likely that single parameter calls to the variadic functions may be accidentally reintroduced in the future a new debugging config option WARN_SIMPLE_MSG has been introduced. This uses some macro magic which will cause any such calls to generate a warning, but this is turned off by default to avoid use of the unpleasant macros in normal circumstances. This is a large changeset due to the number of calls that have been replaced. The only files that contain changes other than simple substitution of function calls are libbb.h, libbb/herror_msg.c, libbb/verror_msg.c and libbb/xfuncs_printf.c. In miscutils/devfsd.c, networking/udhcp/common.h and util-linux/mdev.c additonal macros have been added for logging so that single parameter and multiple parameter logging variants exist. The amount of space saved varies considerably by architecture, and was found to be as follows (for 'defconfig' using GCC 7.4): Arm: -92 bytes MIPS: -52 bytes PPC: -1836 bytes x86_64: -938 bytes Note that for the MIPS architecture only an exception had to be made disabling the 'simple' calls for 'udhcp' (in networking/udhcp/common.h) because it made these files larger on MIPS. Signed-off-by: James Byrne <james.byrne@origamienergy.com> Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2019-07-02 15:05:03 +05:30
bb_simple_error_msg_and_die("no password entry for root");
2006-09-17 21:58:10 +05:30
}
2006-09-08 22:52:05 +05:30
while (1) {
int r;
r = ask_and_check_password_extended(pwd, timeout,
"Give root password for maintenance\n"
"(or type Ctrl-D to continue): "
);
if (r < 0) {
/* ^D, ^C, timeout, or read error */
/* util-linux 2.36.1 compat: no message */
/*bb_simple_info_msg("normal startup");*/
2006-09-09 19:30:58 +05:30
return 0;
}
if (r > 0) {
break;
}
pause_after_failed_login();
libbb: reduce the overhead of single parameter bb_error_msg() calls Back in 2007, commit 0c97c9d43707 ("'simple' error message functions by Loic Grenie") introduced bb_simple_perror_msg() to allow for a lower overhead call to bb_perror_msg() when only a string was being printed with no parameters. This saves space for some CPU architectures because it avoids the overhead of a call to a variadic function. However there has never been a simple version of bb_error_msg(), and since 2007 many new calls to bb_perror_msg() have been added that only take a single parameter and so could have been using bb_simple_perror_message(). This changeset introduces 'simple' versions of bb_info_msg(), bb_error_msg(), bb_error_msg_and_die(), bb_herror_msg() and bb_herror_msg_and_die(), and replaces all calls that only take a single parameter, or use something like ("%s", arg), with calls to the corresponding 'simple' version. Since it is likely that single parameter calls to the variadic functions may be accidentally reintroduced in the future a new debugging config option WARN_SIMPLE_MSG has been introduced. This uses some macro magic which will cause any such calls to generate a warning, but this is turned off by default to avoid use of the unpleasant macros in normal circumstances. This is a large changeset due to the number of calls that have been replaced. The only files that contain changes other than simple substitution of function calls are libbb.h, libbb/herror_msg.c, libbb/verror_msg.c and libbb/xfuncs_printf.c. In miscutils/devfsd.c, networking/udhcp/common.h and util-linux/mdev.c additonal macros have been added for logging so that single parameter and multiple parameter logging variants exist. The amount of space saved varies considerably by architecture, and was found to be as follows (for 'defconfig' using GCC 7.4): Arm: -92 bytes MIPS: -52 bytes PPC: -1836 bytes x86_64: -938 bytes Note that for the MIPS architecture only an exception had to be made disabling the 'simple' calls for 'udhcp' (in networking/udhcp/common.h) because it made these files larger on MIPS. Signed-off-by: James Byrne <james.byrne@origamienergy.com> Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2019-07-02 15:05:03 +05:30
bb_simple_info_msg("Login incorrect");
}
/* util-linux 2.36.1 compat: no message */
/*bb_simple_info_msg("starting shell for system maintenance");*/
IF_SELINUX(renew_current_security_context());
shell = getenv("SUSHELL");
if (!shell)
shell = getenv("sushell");
if (!shell)
shell = pwd->pw_shell;
/* util-linux 2.36.1 compat: cd to root's HOME, set a few envvars */
setup_environment(shell, SETUP_ENV_CHANGEENV | SETUP_ENV_CHANGEENV_LOGNAME, pwd);
// no SETUP_ENV_CLEARENV
// SETUP_ENV_CHANGEENV[+LOGNAME] - set HOME, SHELL, USER,and LOGNAME
// no SETUP_ENV_NO_CHDIR - IOW: cd to $HOME
/* util-linux 2.36.1 compat: steal ctty if we don't have it yet
* (yes, util-linux uses force=1) */
tsid = tcgetsid(STDIN_FILENO);
if (tsid < 0 || getpid() != tsid) {
if (ioctl(STDIN_FILENO, TIOCSCTTY, /*force:*/ (long)1) != 0) {
// bb_perror_msg("TIOCSCTTY1 tsid:%d", tsid);
if (setsid() > 0) {
// bb_error_msg("done setsid()");
/* If it still does not work, ignore */
if (ioctl(STDIN_FILENO, TIOCSCTTY, /*force:*/ (long)1) != 0) {
// bb_perror_msg("TIOCSCTTY2 tsid:%d", tsid);
}
}
}
}
/*
* Note: login does this (should we do it too?):
*/
/*signal(SIGINT, SIG_DFL);*/
/* Exec shell with no additional parameters. Never returns. */
exec_shell(shell, /* -p? then shell is login:*/(opts & 1), NULL);
}