diff options
| author | Denys Vlasenko <vda.linux@googlemail.com> | 2010-04-05 22:10:38 +0200 |
|---|---|---|
| committer | Denys Vlasenko <vda.linux@googlemail.com> | 2010-04-05 22:10:38 +0200 |
| commit | 3a41611bc5ddeda6044e1f1e2956174b25389ce0 (patch) | |
| tree | adfc8a982cf321cc859900a28e9e92c82d43caea /libbb | |
| parent | 37f5bef63c0db4892d8ffa3c38a04c7998e10f83 (diff) | |
| download | busybox-w32-3a41611bc5ddeda6044e1f1e2956174b25389ce0.tar.gz busybox-w32-3a41611bc5ddeda6044e1f1e2956174b25389ce0.tar.bz2 busybox-w32-3a41611bc5ddeda6044e1f1e2956174b25389ce0.zip | |
telnetd: write LOGIN/DEAD_PROCESS utmp records. Closes bug 1363
function old new delta
write_new_utmp - 253 +253
skip_dev_pfx - 30 +30
handle_sigchld 42 72 +30
telnetd_main 1650 1673 +23
make_new_session 415 438 +23
...
login_main 1140 1148 +8
update_utmp 337 313 -24
write_wtmp 220 154 -66
------------------------------------------------------------------------------
(add/remove: 2/0 grow/shrink: 11/6 up/down: 406/-115) Total: ~291 bytes
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to 'libbb')
| -rw-r--r-- | libbb/utmp.c | 123 |
1 files changed, 86 insertions, 37 deletions
diff --git a/libbb/utmp.c b/libbb/utmp.c index 06b939b9d..d6ba336e3 100644 --- a/libbb/utmp.c +++ b/libbb/utmp.c | |||
| @@ -15,37 +15,87 @@ static void touch(const char *filename) | |||
| 15 | close(open(filename, O_WRONLY | O_CREAT, 0664)); | 15 | close(open(filename, O_WRONLY | O_CREAT, 0664)); |
| 16 | } | 16 | } |
| 17 | 17 | ||
| 18 | // TODO: move to libbb | ||
| 19 | static const char* skip_dev_pfx(const char *tty_name) | ||
| 20 | { | ||
| 21 | if (strncmp(tty_name, "/dev/", 5) == 0) | ||
| 22 | tty_name += 5; | ||
| 23 | return tty_name; | ||
| 24 | } | ||
| 25 | |||
| 26 | void FAST_FUNC write_new_utmp(pid_t pid, int new_type, const char *tty_name, const char *username, const char *hostname) | ||
| 27 | { | ||
| 28 | struct utmp utent; | ||
| 29 | char *id; | ||
| 30 | unsigned width; | ||
| 31 | |||
| 32 | memset(&utent, 0, sizeof(utent)); | ||
| 33 | utent.ut_pid = pid; | ||
| 34 | utent.ut_type = new_type; | ||
| 35 | tty_name = skip_dev_pfx(tty_name); | ||
| 36 | safe_strncpy(utent.ut_line, tty_name, sizeof(utent.ut_line)); | ||
| 37 | if (username) | ||
| 38 | safe_strncpy(utent.ut_user, username, sizeof(utent.ut_user)); | ||
| 39 | if (hostname) | ||
| 40 | safe_strncpy(utent.ut_host, hostname, sizeof(utent.ut_host)); | ||
| 41 | utent.ut_tv.tv_sec = time(NULL); | ||
| 42 | |||
| 43 | /* Invent our own ut_id. ut_id is only 4 chars wide. | ||
| 44 | * Try to fit something remotely meaningful... */ | ||
| 45 | id = utent.ut_id; | ||
| 46 | width = sizeof(utent.ut_id); | ||
| 47 | if (tty_name[0] == 'p') { | ||
| 48 | /* if "ptyXXX", map to "pXXX" */ | ||
| 49 | /* if "pts/XX", map to "p/XX" */ | ||
| 50 | *id++ = 'p'; | ||
| 51 | width--; | ||
| 52 | } /* else: usually it's "ttyXXXX", map to "XXXX" */ | ||
| 53 | if (strlen(tty_name) > 3) | ||
| 54 | tty_name += 3; | ||
| 55 | strncpy(id, tty_name, width); | ||
| 56 | |||
| 57 | touch(_PATH_UTMP); | ||
| 58 | //utmpname(_PATH_UTMP); | ||
| 59 | setutent(); | ||
| 60 | /* Append new one (hopefully, unless we collide on ut_id) */ | ||
| 61 | pututline(&utent); | ||
| 62 | endutent(); | ||
| 63 | |||
| 64 | #if ENABLE_FEATURE_WTMP | ||
| 65 | /* "man utmp" says wtmp file should *not* be created automagically */ | ||
| 66 | /*touch(bb_path_wtmp_file);*/ | ||
| 67 | updwtmp(bb_path_wtmp_file, &utent); | ||
| 68 | #endif | ||
| 69 | } | ||
| 70 | |||
| 18 | /* | 71 | /* |
| 19 | * Read "man utmp" to make sense out of it. | 72 | * Read "man utmp" to make sense out of it. |
| 20 | */ | 73 | */ |
| 21 | void FAST_FUNC update_utmp(int new_type, const char *short_tty, const char *username, const char *opt_host) | 74 | void FAST_FUNC update_utmp(pid_t pid, int new_type, const char *tty_name, const char *username, const char *hostname) |
| 22 | { | 75 | { |
| 23 | struct utmp utent; | 76 | struct utmp utent; |
| 24 | struct utmp *ut; | 77 | struct utmp *utp; |
| 25 | pid_t pid; | ||
| 26 | 78 | ||
| 27 | touch(_PATH_UTMP); | 79 | touch(_PATH_UTMP); |
| 28 | utmpname(_PATH_UTMP); | 80 | //utmpname(_PATH_UTMP); |
| 29 | setutent(); | 81 | setutent(); |
| 30 | 82 | ||
| 31 | pid = getpid(); | ||
| 32 | /* Did init/getty/telnetd/sshd/... create an entry for us? | 83 | /* Did init/getty/telnetd/sshd/... create an entry for us? |
| 33 | * It should be (new_type-1), but we'd also reuse | 84 | * It should be (new_type-1), but we'd also reuse |
| 34 | * any other potentially stale xxx_PROCESS entry */ | 85 | * any other potentially stale xxx_PROCESS entry */ |
| 35 | while ((ut = getutent()) != NULL) { | 86 | while ((utp = getutent()) != NULL) { |
| 36 | if (ut->ut_pid == pid | 87 | if (utp->ut_pid == pid |
| 37 | // && ut->ut_line[0] | 88 | // && ut->ut_line[0] |
| 38 | && ut->ut_id[0] /* must have nonzero id */ | 89 | && utp->ut_id[0] /* must have nonzero id */ |
| 39 | && ( ut->ut_type == INIT_PROCESS | 90 | && ( utp->ut_type == INIT_PROCESS |
| 40 | || ut->ut_type == LOGIN_PROCESS | 91 | || utp->ut_type == LOGIN_PROCESS |
| 41 | || ut->ut_type == USER_PROCESS | 92 | || utp->ut_type == USER_PROCESS |
| 42 | || ut->ut_type == DEAD_PROCESS | 93 | || utp->ut_type == DEAD_PROCESS |
| 43 | ) | 94 | ) |
| 44 | ) { | 95 | ) { |
| 45 | utent = *ut; /* struct copy */ | 96 | if (utp->ut_type >= new_type) { |
| 46 | if (ut->ut_type >= new_type) { | ||
| 47 | /* Stale record. Nuke hostname */ | 97 | /* Stale record. Nuke hostname */ |
| 48 | memset(utent.ut_host, 0, sizeof(utent.ut_host)); | 98 | memset(utp->ut_host, 0, sizeof(utp->ut_host)); |
| 49 | } | 99 | } |
| 50 | /* NB: pututline (see later) searches for matching utent | 100 | /* NB: pututline (see later) searches for matching utent |
| 51 | * using getutid(utent) - we must not change ut_id | 101 | * using getutid(utent) - we must not change ut_id |
| @@ -54,39 +104,38 @@ void FAST_FUNC update_utmp(int new_type, const char *short_tty, const char *user | |||
| 54 | break; | 104 | break; |
| 55 | } | 105 | } |
| 56 | } | 106 | } |
| 57 | endutent(); | 107 | //endutent(); - no need, pututline can deal with (and actually likes) |
| 108 | //the situation when utmp file is positioned on found record | ||
| 58 | 109 | ||
| 59 | if (!ut) { | 110 | if (!utp) { |
| 60 | /* Didn't find anything, create new one */ | 111 | if (new_type != DEAD_PROCESS) |
| 61 | memset(&utent, 0, sizeof(utent)); | 112 | write_new_utmp(pid, new_type, tty_name, username, hostname); |
| 62 | utent.ut_pid = pid; | 113 | else |
| 63 | /* Invent our own ut_id. ut_id is only 4 chars wide. | 114 | endutent(); |
| 64 | * Try to fit something remotely meaningful... */ | 115 | return; |
| 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 | } | 116 | } |
| 75 | 117 | ||
| 118 | /* Make a copy. We can't use *utp, pututline's internal getutid | ||
| 119 | * will overwrite it before it is used! */ | ||
| 120 | utent = *utp; | ||
| 121 | |||
| 76 | utent.ut_type = new_type; | 122 | utent.ut_type = new_type; |
| 77 | safe_strncpy(utent.ut_line, short_tty, sizeof(utent.ut_line)); | 123 | if (tty_name) |
| 78 | safe_strncpy(utent.ut_user, username, sizeof(utent.ut_user)); | 124 | safe_strncpy(utent.ut_line, skip_dev_pfx(tty_name), sizeof(utent.ut_line)); |
| 79 | if (opt_host) | 125 | if (username) |
| 80 | safe_strncpy(utent.ut_host, opt_host, sizeof(utent.ut_host)); | 126 | safe_strncpy(utent.ut_user, username, sizeof(utent.ut_user)); |
| 127 | if (hostname) | ||
| 128 | safe_strncpy(utent.ut_host, hostname, sizeof(utent.ut_host)); | ||
| 81 | utent.ut_tv.tv_sec = time(NULL); | 129 | utent.ut_tv.tv_sec = time(NULL); |
| 82 | 130 | ||
| 83 | /* Update, or append new one */ | 131 | /* Update, or append new one */ |
| 84 | setutent(); | 132 | //setutent(); |
| 85 | pututline(&utent); | 133 | pututline(&utent); |
| 86 | endutent(); | 134 | endutent(); |
| 87 | 135 | ||
| 88 | #if ENABLE_FEATURE_WTMP | 136 | #if ENABLE_FEATURE_WTMP |
| 89 | touch(bb_path_wtmp_file); | 137 | /* "man utmp" says wtmp file should *not* be created automagically */ |
| 138 | /*touch(bb_path_wtmp_file);*/ | ||
| 90 | updwtmp(bb_path_wtmp_file, &utent); | 139 | updwtmp(bb_path_wtmp_file, &utent); |
| 91 | #endif | 140 | #endif |
| 92 | } | 141 | } |
