From 37f5bef63c0db4892d8ffa3c38a04c7998e10f83 Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Mon, 5 Apr 2010 03:18:40 +0200 Subject: libbb: split update_utmp from login/getty in preparation to use it for telnetd function old new delta update_utent - 339 +339 login_main 1498 1128 -370 ------------------------------------------------------------------------------ (add/remove: 1/0 grow/shrink: 0/1 up/down: 339/-370) Total: -31 bytes function old new delta update_utmp 246 337 +91 login_main 1128 1140 +12 getty_main 1908 1918 +10 ... update_utent 339 - -339 ------------------------------------------------------------------------------ (add/remove: 1/1 grow/shrink: 5/6 up/down: 119/-368) Total: -249 bytes Signed-off-by: Denys Vlasenko --- loginutils/login.c | 112 +++++++---------------------------------------------- 1 file changed, 14 insertions(+), 98 deletions(-) (limited to 'loginutils/login.c') diff --git a/loginutils/login.c b/loginutils/login.c index 256c7c475..4a820379d 100644 --- a/loginutils/login.c +++ b/loginutils/login.c @@ -2,25 +2,26 @@ /* * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. */ - #include "libbb.h" #include -#include +#if ENABLE_FEATURE_UTMP +# include /* USER_PROCESS */ +#endif #include #if ENABLE_SELINUX -#include /* for is_selinux_enabled() */ -#include /* for get_default_context() */ -#include /* for security class definitions */ +# include /* for is_selinux_enabled() */ +# include /* for get_default_context() */ +# include /* for security class definitions */ #endif #if ENABLE_PAM /* PAM may include . We may need to undefine bbox's stub define: */ -#undef setlocale +# undef setlocale /* For some obscure reason, PAM is not in pam/xxx, but in security/xxx. * Apparently they like to confuse people. */ -#include -#include +# include +# include static const struct pam_conv conv = { misc_conv, NULL @@ -36,87 +37,6 @@ enum { static char* short_tty; -#if ENABLE_FEATURE_UTMP -/* vv Taken from tinylogin utmp.c vv */ -/* - * read_or_build_utent - see if utmp file is correct for this process - * - * System V is very picky about the contents of the utmp file - * and requires that a slot for the current process exist. - * The utmp file is scanned for an entry with the same process - * ID. If no entry exists the process exits with a message. - * - * The "picky" flag is for network and other logins that may - * use special flags. It allows the pid checks to be overridden. - * This means that getty should never invoke login with any - * command line flags. - */ - -static void read_or_build_utent(struct utmp *utptr, int run_by_root) -{ - struct utmp *ut; - pid_t pid = getpid(); - - setutent(); - - /* First, try to find a valid utmp entry for this process. */ - /* If there is one, just use it. */ - while ((ut = getutent()) != NULL) { - if (ut->ut_pid == pid && ut->ut_line[0] && ut->ut_id[0] - && (ut->ut_type == LOGIN_PROCESS || ut->ut_type == USER_PROCESS) - ) { - *utptr = *ut; /* struct copy */ - if (run_by_root) /* why only for root? */ - memset(utptr->ut_host, 0, sizeof(utptr->ut_host)); - return; - } - } - -// Why? Do we require non-root to exec login from another -// former login process (e.g. login shell)? Some login's have -// login shells as children, so it won't work... -// if (!run_by_root) -// bb_error_msg_and_die("no utmp entry found"); - - /* Otherwise create a new one. */ - memset(utptr, 0, sizeof(*utptr)); - utptr->ut_type = LOGIN_PROCESS; - utptr->ut_pid = pid; - strncpy(utptr->ut_line, short_tty, sizeof(utptr->ut_line)); - /* This one is only 4 chars wide. Try to fit something - * remotely meaningful by skipping "tty"... */ - strncpy(utptr->ut_id, short_tty + 3, sizeof(utptr->ut_id)); - strncpy(utptr->ut_user, "LOGIN", sizeof(utptr->ut_user)); - utptr->ut_tv.tv_sec = time(NULL); -} - -/* - * write_utent - put a USER_PROCESS entry in the utmp file - * - * write_utent changes the type of the current utmp entry to - * USER_PROCESS. the wtmp file will be updated as well. - */ -static void write_utent(struct utmp *utptr, const char *username) -{ - utptr->ut_type = USER_PROCESS; - strncpy(utptr->ut_user, username, sizeof(utptr->ut_user)); - utptr->ut_tv.tv_sec = time(NULL); - /* other fields already filled in by read_or_build_utent above */ - setutent(); - pututline(utptr); - endutent(); -#if ENABLE_FEATURE_WTMP - if (access(bb_path_wtmp_file, R_OK|W_OK) == -1) { - close(creat(bb_path_wtmp_file, 0664)); - } - updwtmp(bb_path_wtmp_file, utptr); -#endif -} -#else /* !ENABLE_FEATURE_UTMP */ -#define read_or_build_utent(utptr, run_by_root) ((void)0) -#define write_utent(utptr, username) ((void)0) -#endif /* !ENABLE_FEATURE_UTMP */ - #if ENABLE_FEATURE_NOLOGIN static void die_if_nologin(void) { @@ -144,7 +64,7 @@ static void die_if_nologin(void) exit(EXIT_FAILURE); } #else -static ALWAYS_INLINE void die_if_nologin(void) {} +# define die_if_nologin() ((void)0) #endif #if ENABLE_FEATURE_SECURETTY && !ENABLE_PAM @@ -286,11 +206,10 @@ int login_main(int argc UNUSED_PARAM, char **argv) unsigned opt; int count = 0; struct passwd *pw; - char *opt_host = opt_host; /* for compiler */ + char *opt_host = NULL; char *opt_user = opt_user; /* for compiler */ char *full_tty; IF_SELINUX(security_context_t user_sid = NULL;) - IF_FEATURE_UTMP(struct utmp utent;) #if ENABLE_PAM int pamret; pam_handle_t *pamh; @@ -334,10 +253,7 @@ int login_main(int argc UNUSED_PARAM, char **argv) if (strncmp(full_tty, "/dev/", 5) == 0) short_tty += 5; - read_or_build_utent(&utent, run_by_root); - - if (opt & LOGIN_OPT_h) { - IF_FEATURE_UTMP(safe_strncpy(utent.ut_host, opt_host, sizeof(utent.ut_host));) + if (opt_host) { fromhost = xasprintf(" on '%s' from '%s'", short_tty, opt_host); } else { fromhost = xasprintf(" on '%s'", short_tty); @@ -461,8 +377,6 @@ int login_main(int argc UNUSED_PARAM, char **argv) if (pw->pw_uid != 0) die_if_nologin(); - write_utent(&utent, username); - IF_SELINUX(initselinux(username, full_tty, &user_sid)); /* Try these, but don't complain if they fail. @@ -470,6 +384,8 @@ int login_main(int argc UNUSED_PARAM, char **argv) fchown(0, pw->pw_uid, pw->pw_gid); fchmod(0, 0600); + update_utmp(USER_PROCESS, short_tty, username, run_by_root ? opt_host : NULL); + /* We trust environment only if we run by root */ if (ENABLE_LOGIN_SCRIPTS && run_by_root) run_login_script(pw, full_tty); -- cgit v1.2.3