diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2010-04-05 05:18:40 +0400 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2010-04-05 05:18:40 +0400 |
commit | 37f5bef63c0db4892d8ffa3c38a04c7998e10f83 (patch) | |
tree | a2867f7b8d8a50954f331729bb55e4352f48ff9a /loginutils | |
parent | 4c721049c3114c946848fbbd38d6180aa7af537d (diff) |
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 <vda.linux@googlemail.com>
Diffstat (limited to 'loginutils')
-rw-r--r-- | loginutils/getty.c | 61 | ||||
-rw-r--r-- | loginutils/login.c | 112 |
2 files changed, 16 insertions, 157 deletions
diff --git a/loginutils/getty.c b/loginutils/getty.c index 20411b04c..8d1d5254e 100644 --- a/loginutils/getty.c +++ b/loginutils/getty.c @@ -19,7 +19,7 @@ #include <syslog.h> #if ENABLE_FEATURE_UTMP -#include <utmp.h> /* updwtmp() */ +#include <utmp.h> /* LOGIN_PROCESS */ #endif #ifndef IUCLC @@ -575,61 +575,6 @@ static void termios_final(struct options *op, struct termios *tp, struct chardat ioctl_or_perror_and_die(0, TCSETS, tp, "%s: TCSETS", op->tty); } -#if ENABLE_FEATURE_UTMP -static void touch(const char *filename) -{ - if (access(filename, R_OK | W_OK) == -1) - close(open(filename, O_WRONLY | O_CREAT, 0664)); -} - -/* update_utmp - update our utmp entry */ -static NOINLINE void update_utmp(const char *line, char *fakehost) -{ - struct utmp ut; - struct utmp *utp; - int mypid = getpid(); - - /* In case we won't find an entry below... */ - memset(&ut, 0, sizeof(ut)); - safe_strncpy(ut.ut_id, line + 3, sizeof(ut.ut_id)); - - /* - * The utmp file holds miscellaneous information about things started by - * /sbin/init and other system-related events. Our purpose is to update - * the utmp entry for the current process, in particular the process type - * and the tty line we are listening to. Return successfully only if the - * utmp file can be opened for update, and if we are able to find our - * entry in the utmp file. - */ - touch(_PATH_UTMP); - - utmpname(_PATH_UTMP); - setutent(); - while ((utp = getutent()) != NULL) { - if (utp->ut_type == INIT_PROCESS && utp->ut_pid == mypid) { - memcpy(&ut, utp, sizeof(ut)); - break; - } - } - - strcpy(ut.ut_user, "LOGIN"); - safe_strncpy(ut.ut_line, line, sizeof(ut.ut_line)); - if (fakehost) - safe_strncpy(ut.ut_host, fakehost, sizeof(ut.ut_host)); - ut.ut_tv.tv_sec = time(NULL); - ut.ut_type = LOGIN_PROCESS; - ut.ut_pid = mypid; - - pututline(&ut); - endutent(); - -#if ENABLE_FEATURE_WTMP - touch(bb_path_wtmp_file); - updwtmp(bb_path_wtmp_file, &ut); -#endif -} -#endif /* CONFIG_FEATURE_UTMP */ - int getty_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; int getty_main(int argc UNUSED_PARAM, char **argv) { @@ -715,10 +660,8 @@ int getty_main(int argc UNUSED_PARAM, char **argv) tcsetpgrp(0, getpid()); #endif -#if ENABLE_FEATURE_UTMP /* Update the utmp file. This tty is ours now! */ - update_utmp(options.tty, fakehost); -#endif + update_utmp(LOGIN_PROCESS, options.tty, "LOGIN", fakehost); /* Initialize the termios settings (raw mode, eight-bit, blocking i/o). */ debug("calling termios_init\n"); 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 <syslog.h> -#include <utmp.h> +#if ENABLE_FEATURE_UTMP +# include <utmp.h> /* USER_PROCESS */ +#endif #include <sys/resource.h> #if ENABLE_SELINUX -#include <selinux/selinux.h> /* for is_selinux_enabled() */ -#include <selinux/get_context_list.h> /* for get_default_context() */ -#include <selinux/flask.h> /* for security class definitions */ +# include <selinux/selinux.h> /* for is_selinux_enabled() */ +# include <selinux/get_context_list.h> /* for get_default_context() */ +# include <selinux/flask.h> /* for security class definitions */ #endif #if ENABLE_PAM /* PAM may include <locale.h>. 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 <security/pam_appl.h> -#include <security/pam_misc.h> +# include <security/pam_appl.h> +# include <security/pam_misc.h> 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); |