Denys Vlasenko | 37f5bef | 2010-04-05 03:18:40 +0200 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * utmp/wtmp support routines. |
| 4 | * |
| 5 | * Copyright (C) 2010 Denys Vlasenko |
| 6 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 7 | * Licensed under GPLv2, see file LICENSE in this source tree. |
Denys Vlasenko | 37f5bef | 2010-04-05 03:18:40 +0200 | [diff] [blame] | 8 | */ |
| 9 | #include "libbb.h" |
| 10 | #include <utmp.h> |
| 11 | |
| 12 | static 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 | |
Denys Vlasenko | 3a41611 | 2010-04-05 22:10:38 +0200 | [diff] [blame] | 18 | void FAST_FUNC write_new_utmp(pid_t pid, int new_type, const char *tty_name, const char *username, const char *hostname) |
| 19 | { |
| 20 | struct utmp utent; |
| 21 | char *id; |
| 22 | unsigned width; |
| 23 | |
| 24 | memset(&utent, 0, sizeof(utent)); |
| 25 | utent.ut_pid = pid; |
| 26 | utent.ut_type = new_type; |
| 27 | tty_name = skip_dev_pfx(tty_name); |
| 28 | safe_strncpy(utent.ut_line, tty_name, sizeof(utent.ut_line)); |
| 29 | if (username) |
| 30 | safe_strncpy(utent.ut_user, username, sizeof(utent.ut_user)); |
| 31 | if (hostname) |
| 32 | safe_strncpy(utent.ut_host, hostname, sizeof(utent.ut_host)); |
| 33 | utent.ut_tv.tv_sec = time(NULL); |
| 34 | |
| 35 | /* Invent our own ut_id. ut_id is only 4 chars wide. |
| 36 | * Try to fit something remotely meaningful... */ |
| 37 | id = utent.ut_id; |
| 38 | width = sizeof(utent.ut_id); |
| 39 | if (tty_name[0] == 'p') { |
| 40 | /* if "ptyXXX", map to "pXXX" */ |
| 41 | /* if "pts/XX", map to "p/XX" */ |
| 42 | *id++ = 'p'; |
| 43 | width--; |
| 44 | } /* else: usually it's "ttyXXXX", map to "XXXX" */ |
| 45 | if (strlen(tty_name) > 3) |
| 46 | tty_name += 3; |
| 47 | strncpy(id, tty_name, width); |
| 48 | |
| 49 | touch(_PATH_UTMP); |
| 50 | //utmpname(_PATH_UTMP); |
| 51 | setutent(); |
| 52 | /* Append new one (hopefully, unless we collide on ut_id) */ |
| 53 | pututline(&utent); |
| 54 | endutent(); |
| 55 | |
| 56 | #if ENABLE_FEATURE_WTMP |
| 57 | /* "man utmp" says wtmp file should *not* be created automagically */ |
| 58 | /*touch(bb_path_wtmp_file);*/ |
| 59 | updwtmp(bb_path_wtmp_file, &utent); |
| 60 | #endif |
| 61 | } |
| 62 | |
Denys Vlasenko | 37f5bef | 2010-04-05 03:18:40 +0200 | [diff] [blame] | 63 | /* |
| 64 | * Read "man utmp" to make sense out of it. |
| 65 | */ |
Denys Vlasenko | 3a41611 | 2010-04-05 22:10:38 +0200 | [diff] [blame] | 66 | void FAST_FUNC update_utmp(pid_t pid, int new_type, const char *tty_name, const char *username, const char *hostname) |
Denys Vlasenko | 37f5bef | 2010-04-05 03:18:40 +0200 | [diff] [blame] | 67 | { |
| 68 | struct utmp utent; |
Denys Vlasenko | 3a41611 | 2010-04-05 22:10:38 +0200 | [diff] [blame] | 69 | struct utmp *utp; |
Denys Vlasenko | 37f5bef | 2010-04-05 03:18:40 +0200 | [diff] [blame] | 70 | |
| 71 | touch(_PATH_UTMP); |
Denys Vlasenko | 3a41611 | 2010-04-05 22:10:38 +0200 | [diff] [blame] | 72 | //utmpname(_PATH_UTMP); |
Denys Vlasenko | 37f5bef | 2010-04-05 03:18:40 +0200 | [diff] [blame] | 73 | setutent(); |
| 74 | |
Denys Vlasenko | 37f5bef | 2010-04-05 03:18:40 +0200 | [diff] [blame] | 75 | /* Did init/getty/telnetd/sshd/... create an entry for us? |
| 76 | * It should be (new_type-1), but we'd also reuse |
| 77 | * any other potentially stale xxx_PROCESS entry */ |
Denys Vlasenko | 3a41611 | 2010-04-05 22:10:38 +0200 | [diff] [blame] | 78 | while ((utp = getutent()) != NULL) { |
| 79 | if (utp->ut_pid == pid |
Denys Vlasenko | 37f5bef | 2010-04-05 03:18:40 +0200 | [diff] [blame] | 80 | // && ut->ut_line[0] |
Denys Vlasenko | 3a41611 | 2010-04-05 22:10:38 +0200 | [diff] [blame] | 81 | && utp->ut_id[0] /* must have nonzero id */ |
| 82 | && ( utp->ut_type == INIT_PROCESS |
| 83 | || utp->ut_type == LOGIN_PROCESS |
| 84 | || utp->ut_type == USER_PROCESS |
| 85 | || utp->ut_type == DEAD_PROCESS |
Denys Vlasenko | 37f5bef | 2010-04-05 03:18:40 +0200 | [diff] [blame] | 86 | ) |
| 87 | ) { |
Denys Vlasenko | 3a41611 | 2010-04-05 22:10:38 +0200 | [diff] [blame] | 88 | if (utp->ut_type >= new_type) { |
Denys Vlasenko | 37f5bef | 2010-04-05 03:18:40 +0200 | [diff] [blame] | 89 | /* Stale record. Nuke hostname */ |
Denys Vlasenko | 3a41611 | 2010-04-05 22:10:38 +0200 | [diff] [blame] | 90 | memset(utp->ut_host, 0, sizeof(utp->ut_host)); |
Denys Vlasenko | 37f5bef | 2010-04-05 03:18:40 +0200 | [diff] [blame] | 91 | } |
| 92 | /* NB: pututline (see later) searches for matching utent |
| 93 | * using getutid(utent) - we must not change ut_id |
| 94 | * if we want *exactly this* record to be overwritten! |
| 95 | */ |
| 96 | break; |
| 97 | } |
| 98 | } |
Denys Vlasenko | 3a41611 | 2010-04-05 22:10:38 +0200 | [diff] [blame] | 99 | //endutent(); - no need, pututline can deal with (and actually likes) |
| 100 | //the situation when utmp file is positioned on found record |
Denys Vlasenko | 37f5bef | 2010-04-05 03:18:40 +0200 | [diff] [blame] | 101 | |
Denys Vlasenko | 3a41611 | 2010-04-05 22:10:38 +0200 | [diff] [blame] | 102 | if (!utp) { |
| 103 | if (new_type != DEAD_PROCESS) |
| 104 | write_new_utmp(pid, new_type, tty_name, username, hostname); |
| 105 | else |
| 106 | endutent(); |
| 107 | return; |
Denys Vlasenko | 37f5bef | 2010-04-05 03:18:40 +0200 | [diff] [blame] | 108 | } |
| 109 | |
Denys Vlasenko | 3a41611 | 2010-04-05 22:10:38 +0200 | [diff] [blame] | 110 | /* Make a copy. We can't use *utp, pututline's internal getutid |
| 111 | * will overwrite it before it is used! */ |
| 112 | utent = *utp; |
| 113 | |
Denys Vlasenko | 37f5bef | 2010-04-05 03:18:40 +0200 | [diff] [blame] | 114 | utent.ut_type = new_type; |
Denys Vlasenko | 3a41611 | 2010-04-05 22:10:38 +0200 | [diff] [blame] | 115 | if (tty_name) |
| 116 | safe_strncpy(utent.ut_line, skip_dev_pfx(tty_name), sizeof(utent.ut_line)); |
| 117 | if (username) |
| 118 | safe_strncpy(utent.ut_user, username, sizeof(utent.ut_user)); |
| 119 | if (hostname) |
| 120 | safe_strncpy(utent.ut_host, hostname, sizeof(utent.ut_host)); |
Denys Vlasenko | 37f5bef | 2010-04-05 03:18:40 +0200 | [diff] [blame] | 121 | utent.ut_tv.tv_sec = time(NULL); |
| 122 | |
| 123 | /* Update, or append new one */ |
Denys Vlasenko | 3a41611 | 2010-04-05 22:10:38 +0200 | [diff] [blame] | 124 | //setutent(); |
Denys Vlasenko | 37f5bef | 2010-04-05 03:18:40 +0200 | [diff] [blame] | 125 | pututline(&utent); |
| 126 | endutent(); |
| 127 | |
| 128 | #if ENABLE_FEATURE_WTMP |
Denys Vlasenko | 3a41611 | 2010-04-05 22:10:38 +0200 | [diff] [blame] | 129 | /* "man utmp" says wtmp file should *not* be created automagically */ |
| 130 | /*touch(bb_path_wtmp_file);*/ |
Denys Vlasenko | 37f5bef | 2010-04-05 03:18:40 +0200 | [diff] [blame] | 131 | updwtmp(bb_path_wtmp_file, &utent); |
| 132 | #endif |
| 133 | } |