aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2010-04-05 03:18:40 +0200
committerDenys Vlasenko <vda.linux@googlemail.com>2010-04-05 03:18:40 +0200
commit37f5bef63c0db4892d8ffa3c38a04c7998e10f83 (patch)
treea2867f7b8d8a50954f331729bb55e4352f48ff9a
parent4c721049c3114c946848fbbd38d6180aa7af537d (diff)
downloadbusybox-w32-37f5bef63c0db4892d8ffa3c38a04c7998e10f83.tar.gz
busybox-w32-37f5bef63c0db4892d8ffa3c38a04c7998e10f83.tar.bz2
busybox-w32-37f5bef63c0db4892d8ffa3c38a04c7998e10f83.zip
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>
-rw-r--r--coreutils/who.c2
-rw-r--r--include/libbb.h6
-rw-r--r--init/halt.c1
-rw-r--r--libbb/Kbuild2
-rw-r--r--libbb/utmp.c92
-rw-r--r--loginutils/getty.c61
-rw-r--r--loginutils/login.c112
7 files changed, 117 insertions, 159 deletions
diff --git a/coreutils/who.c b/coreutils/who.c
index dfbb4c046..2b4331025 100644
--- a/coreutils/who.c
+++ b/coreutils/who.c
@@ -16,7 +16,7 @@
16 * 16 *
17 *---------------------------------------------------------------------- 17 *----------------------------------------------------------------------
18 */ 18 */
19/* BB_AUDIT SUSv3 _NOT_ compliant -- missing options -b, -d, -H, -l, -m, -p, -q, -r, -s, -t, -T, -u; Missing argument 'file'. */ 19/* BB_AUDIT SUSv3 _NOT_ compliant -- missing options -b, -d, -l, -m, -p, -q, -r, -s, -t, -T, -u; Missing argument 'file'. */
20 20
21#include "libbb.h" 21#include "libbb.h"
22#include <utmp.h> 22#include <utmp.h>
diff --git a/include/libbb.h b/include/libbb.h
index e674e4aea..ea61701b7 100644
--- a/include/libbb.h
+++ b/include/libbb.h
@@ -796,6 +796,12 @@ void die_if_bad_username(const char* name) FAST_FUNC;
796#define die_if_bad_username(name) ((void)(name)) 796#define die_if_bad_username(name) ((void)(name))
797#endif 797#endif
798 798
799#if ENABLE_FEATURE_UTMP
800void FAST_FUNC update_utmp(int new_type, const char *short_tty, const char *username, const char *opt_host);
801#else
802# define update_utmp(new_type, short_tty, username, opt_host) ((void)0)
803#endif
804
799int execable_file(const char *name) FAST_FUNC; 805int execable_file(const char *name) FAST_FUNC;
800char *find_execable(const char *filename, char **PATHp) FAST_FUNC; 806char *find_execable(const char *filename, char **PATHp) FAST_FUNC;
801int exists_execable(const char *filename) FAST_FUNC; 807int exists_execable(const char *filename) FAST_FUNC;
diff --git a/init/halt.c b/init/halt.c
index 16906dfe0..a3459ee48 100644
--- a/init/halt.c
+++ b/init/halt.c
@@ -30,7 +30,6 @@ static void write_wtmp(void)
30 if (uname(&uts) == 0) 30 if (uname(&uts) == 0)
31 safe_strncpy(utmp.ut_host, uts.release, sizeof(utmp.ut_host)); 31 safe_strncpy(utmp.ut_host, uts.release, sizeof(utmp.ut_host));
32 updwtmp(bb_path_wtmp_file, &utmp); 32 updwtmp(bb_path_wtmp_file, &utmp);
33
34} 33}
35#else 34#else
36#define write_wtmp() ((void)0) 35#define write_wtmp() ((void)0)
diff --git a/libbb/Kbuild b/libbb/Kbuild
index 4606d5aa7..1b11d5d39 100644
--- a/libbb/Kbuild
+++ b/libbb/Kbuild
@@ -120,6 +120,8 @@ lib-y += xgethostbyname.o
120lib-y += xreadlink.o 120lib-y += xreadlink.o
121lib-y += xrealloc_vector.o 121lib-y += xrealloc_vector.o
122 122
123lib-$(CONFIG_FEATURE_UTMP) += utmp.o
124
123# A mix of optimizations (why build stuff we know won't be used) 125# A mix of optimizations (why build stuff we know won't be used)
124# and objects which may fail to build (SELinux on selinux-less system) 126# and objects which may fail to build (SELinux on selinux-less system)
125lib-$(CONFIG_SELINUX) += selinux_common.o 127lib-$(CONFIG_SELINUX) += selinux_common.o
diff --git a/libbb/utmp.c b/libbb/utmp.c
new file mode 100644
index 000000000..06b939b9d
--- /dev/null
+++ b/libbb/utmp.c
@@ -0,0 +1,92 @@
1/* vi: set sw=4 ts=4: */
2/*
3 * utmp/wtmp support routines.
4 *
5 * Copyright (C) 2010 Denys Vlasenko
6 *
7 * Licensed under GPL version 2, see file LICENSE in this tarball for details.
8 */
9#include "libbb.h"
10#include <utmp.h>
11
12static void touch(const char *filename)
13{
14 if (access(filename, R_OK | W_OK) == -1)
15 close(open(filename, O_WRONLY | O_CREAT, 0664));
16}
17
18/*
19 * Read "man utmp" to make sense out of it.
20 */
21void FAST_FUNC update_utmp(int new_type, const char *short_tty, const char *username, const char *opt_host)
22{
23 struct utmp utent;
24 struct utmp *ut;
25 pid_t pid;
26
27 touch(_PATH_UTMP);
28 utmpname(_PATH_UTMP);
29 setutent();
30
31 pid = getpid();
32 /* Did init/getty/telnetd/sshd/... create an entry for us?
33 * It should be (new_type-1), but we'd also reuse
34 * any other potentially stale xxx_PROCESS entry */
35 while ((ut = getutent()) != NULL) {
36 if (ut->ut_pid == pid
37 // && ut->ut_line[0]
38 && ut->ut_id[0] /* must have nonzero id */
39 && ( ut->ut_type == INIT_PROCESS
40 || ut->ut_type == LOGIN_PROCESS
41 || ut->ut_type == USER_PROCESS
42 || ut->ut_type == DEAD_PROCESS
43 )
44 ) {
45 utent = *ut; /* struct copy */
46 if (ut->ut_type >= new_type) {
47 /* Stale record. Nuke hostname */
48 memset(utent.ut_host, 0, sizeof(utent.ut_host));
49 }
50 /* NB: pututline (see later) searches for matching utent
51 * using getutid(utent) - we must not change ut_id
52 * if we want *exactly this* record to be overwritten!
53 */
54 break;
55 }
56 }
57 endutent();
58
59 if (!ut) {
60 /* Didn't find anything, create new one */
61 memset(&utent, 0, sizeof(utent));
62 utent.ut_pid = pid;
63 /* Invent our own ut_id. ut_id is only 4 chars wide.
64 * Try to fit something remotely meaningful... */
65 if (short_tty[0] == 'p') {
66 /* if "ptyXXX", map to "pXXX" */
67 /* if "pts/XX", map to "p/XX" */
68 utent.ut_id[0] = 'p';
69 strncpy(utent.ut_id + 1, short_tty + 3, sizeof(utent.ut_id)-1);
70 } else {
71 /* assuming it's "ttyXXXX", map to "XXXX" */
72 strncpy(utent.ut_id, short_tty + 3, sizeof(utent.ut_id));
73 }
74 }
75
76 utent.ut_type = new_type;
77 safe_strncpy(utent.ut_line, short_tty, sizeof(utent.ut_line));
78 safe_strncpy(utent.ut_user, username, sizeof(utent.ut_user));
79 if (opt_host)
80 safe_strncpy(utent.ut_host, opt_host, sizeof(utent.ut_host));
81 utent.ut_tv.tv_sec = time(NULL);
82
83 /* Update, or append new one */
84 setutent();
85 pututline(&utent);
86 endutent();
87
88#if ENABLE_FEATURE_WTMP
89 touch(bb_path_wtmp_file);
90 updwtmp(bb_path_wtmp_file, &utent);
91#endif
92}
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 @@
19#include <syslog.h> 19#include <syslog.h>
20 20
21#if ENABLE_FEATURE_UTMP 21#if ENABLE_FEATURE_UTMP
22#include <utmp.h> /* updwtmp() */ 22#include <utmp.h> /* LOGIN_PROCESS */
23#endif 23#endif
24 24
25#ifndef IUCLC 25#ifndef IUCLC
@@ -575,61 +575,6 @@ static void termios_final(struct options *op, struct termios *tp, struct chardat
575 ioctl_or_perror_and_die(0, TCSETS, tp, "%s: TCSETS", op->tty); 575 ioctl_or_perror_and_die(0, TCSETS, tp, "%s: TCSETS", op->tty);
576} 576}
577 577
578#if ENABLE_FEATURE_UTMP
579static void touch(const char *filename)
580{
581 if (access(filename, R_OK | W_OK) == -1)
582 close(open(filename, O_WRONLY | O_CREAT, 0664));
583}
584
585/* update_utmp - update our utmp entry */
586static NOINLINE void update_utmp(const char *line, char *fakehost)
587{
588 struct utmp ut;
589 struct utmp *utp;
590 int mypid = getpid();
591
592 /* In case we won't find an entry below... */
593 memset(&ut, 0, sizeof(ut));
594 safe_strncpy(ut.ut_id, line + 3, sizeof(ut.ut_id));
595
596 /*
597 * The utmp file holds miscellaneous information about things started by
598 * /sbin/init and other system-related events. Our purpose is to update
599 * the utmp entry for the current process, in particular the process type
600 * and the tty line we are listening to. Return successfully only if the
601 * utmp file can be opened for update, and if we are able to find our
602 * entry in the utmp file.
603 */
604 touch(_PATH_UTMP);
605
606 utmpname(_PATH_UTMP);
607 setutent();
608 while ((utp = getutent()) != NULL) {
609 if (utp->ut_type == INIT_PROCESS && utp->ut_pid == mypid) {
610 memcpy(&ut, utp, sizeof(ut));
611 break;
612 }
613 }
614
615 strcpy(ut.ut_user, "LOGIN");
616 safe_strncpy(ut.ut_line, line, sizeof(ut.ut_line));
617 if (fakehost)
618 safe_strncpy(ut.ut_host, fakehost, sizeof(ut.ut_host));
619 ut.ut_tv.tv_sec = time(NULL);
620 ut.ut_type = LOGIN_PROCESS;
621 ut.ut_pid = mypid;
622
623 pututline(&ut);
624 endutent();
625
626#if ENABLE_FEATURE_WTMP
627 touch(bb_path_wtmp_file);
628 updwtmp(bb_path_wtmp_file, &ut);
629#endif
630}
631#endif /* CONFIG_FEATURE_UTMP */
632
633int getty_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; 578int getty_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
634int getty_main(int argc UNUSED_PARAM, char **argv) 579int getty_main(int argc UNUSED_PARAM, char **argv)
635{ 580{
@@ -715,10 +660,8 @@ int getty_main(int argc UNUSED_PARAM, char **argv)
715 tcsetpgrp(0, getpid()); 660 tcsetpgrp(0, getpid());
716#endif 661#endif
717 662
718#if ENABLE_FEATURE_UTMP
719 /* Update the utmp file. This tty is ours now! */ 663 /* Update the utmp file. This tty is ours now! */
720 update_utmp(options.tty, fakehost); 664 update_utmp(LOGIN_PROCESS, options.tty, "LOGIN", fakehost);
721#endif
722 665
723 /* Initialize the termios settings (raw mode, eight-bit, blocking i/o). */ 666 /* Initialize the termios settings (raw mode, eight-bit, blocking i/o). */
724 debug("calling termios_init\n"); 667 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 @@
2/* 2/*
3 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. 3 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
4 */ 4 */
5
6#include "libbb.h" 5#include "libbb.h"
7#include <syslog.h> 6#include <syslog.h>
8#include <utmp.h> 7#if ENABLE_FEATURE_UTMP
8# include <utmp.h> /* USER_PROCESS */
9#endif
9#include <sys/resource.h> 10#include <sys/resource.h>
10 11
11#if ENABLE_SELINUX 12#if ENABLE_SELINUX
12#include <selinux/selinux.h> /* for is_selinux_enabled() */ 13# include <selinux/selinux.h> /* for is_selinux_enabled() */
13#include <selinux/get_context_list.h> /* for get_default_context() */ 14# include <selinux/get_context_list.h> /* for get_default_context() */
14#include <selinux/flask.h> /* for security class definitions */ 15# include <selinux/flask.h> /* for security class definitions */
15#endif 16#endif
16 17
17#if ENABLE_PAM 18#if ENABLE_PAM
18/* PAM may include <locale.h>. We may need to undefine bbox's stub define: */ 19/* PAM may include <locale.h>. We may need to undefine bbox's stub define: */
19#undef setlocale 20# undef setlocale
20/* For some obscure reason, PAM is not in pam/xxx, but in security/xxx. 21/* For some obscure reason, PAM is not in pam/xxx, but in security/xxx.
21 * Apparently they like to confuse people. */ 22 * Apparently they like to confuse people. */
22#include <security/pam_appl.h> 23# include <security/pam_appl.h>
23#include <security/pam_misc.h> 24# include <security/pam_misc.h>
24static const struct pam_conv conv = { 25static const struct pam_conv conv = {
25 misc_conv, 26 misc_conv,
26 NULL 27 NULL
@@ -36,87 +37,6 @@ enum {
36 37
37static char* short_tty; 38static char* short_tty;
38 39
39#if ENABLE_FEATURE_UTMP
40/* vv Taken from tinylogin utmp.c vv */
41/*
42 * read_or_build_utent - see if utmp file is correct for this process
43 *
44 * System V is very picky about the contents of the utmp file
45 * and requires that a slot for the current process exist.
46 * The utmp file is scanned for an entry with the same process
47 * ID. If no entry exists the process exits with a message.
48 *
49 * The "picky" flag is for network and other logins that may
50 * use special flags. It allows the pid checks to be overridden.
51 * This means that getty should never invoke login with any
52 * command line flags.
53 */
54
55static void read_or_build_utent(struct utmp *utptr, int run_by_root)
56{
57 struct utmp *ut;
58 pid_t pid = getpid();
59
60 setutent();
61
62 /* First, try to find a valid utmp entry for this process. */
63 /* If there is one, just use it. */
64 while ((ut = getutent()) != NULL) {
65 if (ut->ut_pid == pid && ut->ut_line[0] && ut->ut_id[0]
66 && (ut->ut_type == LOGIN_PROCESS || ut->ut_type == USER_PROCESS)
67 ) {
68 *utptr = *ut; /* struct copy */
69 if (run_by_root) /* why only for root? */
70 memset(utptr->ut_host, 0, sizeof(utptr->ut_host));
71 return;
72 }
73 }
74
75// Why? Do we require non-root to exec login from another
76// former login process (e.g. login shell)? Some login's have
77// login shells as children, so it won't work...
78// if (!run_by_root)
79// bb_error_msg_and_die("no utmp entry found");
80
81 /* Otherwise create a new one. */
82 memset(utptr, 0, sizeof(*utptr));
83 utptr->ut_type = LOGIN_PROCESS;
84 utptr->ut_pid = pid;
85 strncpy(utptr->ut_line, short_tty, sizeof(utptr->ut_line));
86 /* This one is only 4 chars wide. Try to fit something
87 * remotely meaningful by skipping "tty"... */
88 strncpy(utptr->ut_id, short_tty + 3, sizeof(utptr->ut_id));
89 strncpy(utptr->ut_user, "LOGIN", sizeof(utptr->ut_user));
90 utptr->ut_tv.tv_sec = time(NULL);
91}
92
93/*
94 * write_utent - put a USER_PROCESS entry in the utmp file
95 *
96 * write_utent changes the type of the current utmp entry to
97 * USER_PROCESS. the wtmp file will be updated as well.
98 */
99static void write_utent(struct utmp *utptr, const char *username)
100{
101 utptr->ut_type = USER_PROCESS;
102 strncpy(utptr->ut_user, username, sizeof(utptr->ut_user));
103 utptr->ut_tv.tv_sec = time(NULL);
104 /* other fields already filled in by read_or_build_utent above */
105 setutent();
106 pututline(utptr);
107 endutent();
108#if ENABLE_FEATURE_WTMP
109 if (access(bb_path_wtmp_file, R_OK|W_OK) == -1) {
110 close(creat(bb_path_wtmp_file, 0664));
111 }
112 updwtmp(bb_path_wtmp_file, utptr);
113#endif
114}
115#else /* !ENABLE_FEATURE_UTMP */
116#define read_or_build_utent(utptr, run_by_root) ((void)0)
117#define write_utent(utptr, username) ((void)0)
118#endif /* !ENABLE_FEATURE_UTMP */
119
120#if ENABLE_FEATURE_NOLOGIN 40#if ENABLE_FEATURE_NOLOGIN
121static void die_if_nologin(void) 41static void die_if_nologin(void)
122{ 42{
@@ -144,7 +64,7 @@ static void die_if_nologin(void)
144 exit(EXIT_FAILURE); 64 exit(EXIT_FAILURE);
145} 65}
146#else 66#else
147static ALWAYS_INLINE void die_if_nologin(void) {} 67# define die_if_nologin() ((void)0)
148#endif 68#endif
149 69
150#if ENABLE_FEATURE_SECURETTY && !ENABLE_PAM 70#if ENABLE_FEATURE_SECURETTY && !ENABLE_PAM
@@ -286,11 +206,10 @@ int login_main(int argc UNUSED_PARAM, char **argv)
286 unsigned opt; 206 unsigned opt;
287 int count = 0; 207 int count = 0;
288 struct passwd *pw; 208 struct passwd *pw;
289 char *opt_host = opt_host; /* for compiler */ 209 char *opt_host = NULL;
290 char *opt_user = opt_user; /* for compiler */ 210 char *opt_user = opt_user; /* for compiler */
291 char *full_tty; 211 char *full_tty;
292 IF_SELINUX(security_context_t user_sid = NULL;) 212 IF_SELINUX(security_context_t user_sid = NULL;)
293 IF_FEATURE_UTMP(struct utmp utent;)
294#if ENABLE_PAM 213#if ENABLE_PAM
295 int pamret; 214 int pamret;
296 pam_handle_t *pamh; 215 pam_handle_t *pamh;
@@ -334,10 +253,7 @@ int login_main(int argc UNUSED_PARAM, char **argv)
334 if (strncmp(full_tty, "/dev/", 5) == 0) 253 if (strncmp(full_tty, "/dev/", 5) == 0)
335 short_tty += 5; 254 short_tty += 5;
336 255
337 read_or_build_utent(&utent, run_by_root); 256 if (opt_host) {
338
339 if (opt & LOGIN_OPT_h) {
340 IF_FEATURE_UTMP(safe_strncpy(utent.ut_host, opt_host, sizeof(utent.ut_host));)
341 fromhost = xasprintf(" on '%s' from '%s'", short_tty, opt_host); 257 fromhost = xasprintf(" on '%s' from '%s'", short_tty, opt_host);
342 } else { 258 } else {
343 fromhost = xasprintf(" on '%s'", short_tty); 259 fromhost = xasprintf(" on '%s'", short_tty);
@@ -461,8 +377,6 @@ int login_main(int argc UNUSED_PARAM, char **argv)
461 if (pw->pw_uid != 0) 377 if (pw->pw_uid != 0)
462 die_if_nologin(); 378 die_if_nologin();
463 379
464 write_utent(&utent, username);
465
466 IF_SELINUX(initselinux(username, full_tty, &user_sid)); 380 IF_SELINUX(initselinux(username, full_tty, &user_sid));
467 381
468 /* Try these, but don't complain if they fail. 382 /* Try these, but don't complain if they fail.
@@ -470,6 +384,8 @@ int login_main(int argc UNUSED_PARAM, char **argv)
470 fchown(0, pw->pw_uid, pw->pw_gid); 384 fchown(0, pw->pw_uid, pw->pw_gid);
471 fchmod(0, 0600); 385 fchmod(0, 0600);
472 386
387 update_utmp(USER_PROCESS, short_tty, username, run_by_root ? opt_host : NULL);
388
473 /* We trust environment only if we run by root */ 389 /* We trust environment only if we run by root */
474 if (ENABLE_LOGIN_SCRIPTS && run_by_root) 390 if (ENABLE_LOGIN_SCRIPTS && run_by_root)
475 run_login_script(pw, full_tty); 391 run_login_script(pw, full_tty);