1a627ac61SEd Schouten /*-
2*4d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
3d915a14eSPedro F. Giffuni *
4a627ac61SEd Schouten * Copyright (c) 2010 Ed Schouten <ed@FreeBSD.org>
5a627ac61SEd Schouten * All rights reserved.
6a627ac61SEd Schouten *
7a627ac61SEd Schouten * Redistribution and use in source and binary forms, with or without
8a627ac61SEd Schouten * modification, are permitted provided that the following conditions
9a627ac61SEd Schouten * are met:
10a627ac61SEd Schouten * 1. Redistributions of source code must retain the above copyright
11a627ac61SEd Schouten * notice, this list of conditions and the following disclaimer.
12a627ac61SEd Schouten * 2. Redistributions in binary form must reproduce the above copyright
13a627ac61SEd Schouten * notice, this list of conditions and the following disclaimer in the
14a627ac61SEd Schouten * documentation and/or other materials provided with the distribution.
15a627ac61SEd Schouten *
16a627ac61SEd Schouten * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17a627ac61SEd Schouten * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18a627ac61SEd Schouten * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19a627ac61SEd Schouten * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20a627ac61SEd Schouten * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21a627ac61SEd Schouten * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22a627ac61SEd Schouten * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23a627ac61SEd Schouten * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24a627ac61SEd Schouten * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25a627ac61SEd Schouten * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26a627ac61SEd Schouten * SUCH DAMAGE.
27a627ac61SEd Schouten */
28a627ac61SEd Schouten
29a627ac61SEd Schouten #include "namespace.h"
30a627ac61SEd Schouten #include <sys/endian.h>
31a627ac61SEd Schouten #include <sys/stat.h>
32a627ac61SEd Schouten #include <sys/uio.h>
33a22175f9SEd Schouten #include <errno.h>
34a627ac61SEd Schouten #include <fcntl.h>
35a627ac61SEd Schouten #include <stdio.h>
36a627ac61SEd Schouten #include <string.h>
37a627ac61SEd Schouten #include <unistd.h>
38a627ac61SEd Schouten #include <utmpx.h>
39a627ac61SEd Schouten #include "utxdb.h"
40a627ac61SEd Schouten #include "un-namespace.h"
41a627ac61SEd Schouten
42a627ac61SEd Schouten static FILE *
futx_open(const char * file)43a627ac61SEd Schouten futx_open(const char *file)
44a627ac61SEd Schouten {
451ae6a21dSEd Schouten FILE *fp;
46339f34e3SEd Schouten struct stat sb;
471ae6a21dSEd Schouten int fd;
48a627ac61SEd Schouten
4905eb11cbSJilles Tjoelker fd = _open(file, O_CREAT|O_RDWR|O_EXLOCK|O_CLOEXEC, 0644);
50a627ac61SEd Schouten if (fd < 0)
51a627ac61SEd Schouten return (NULL);
52a627ac61SEd Schouten
53a627ac61SEd Schouten /* Safety check: never use broken files. */
54a627ac61SEd Schouten if (_fstat(fd, &sb) != -1 && sb.st_size % sizeof(struct futx) != 0) {
55a627ac61SEd Schouten _close(fd);
561ae6a21dSEd Schouten errno = EFTYPE;
57a627ac61SEd Schouten return (NULL);
58a627ac61SEd Schouten }
59a627ac61SEd Schouten
60a627ac61SEd Schouten fp = fdopen(fd, "r+");
61a627ac61SEd Schouten if (fp == NULL) {
62a627ac61SEd Schouten _close(fd);
63a627ac61SEd Schouten return (NULL);
64a627ac61SEd Schouten }
65a627ac61SEd Schouten return (fp);
66a627ac61SEd Schouten }
67a627ac61SEd Schouten
689301df81SEd Schouten static int
utx_active_add(const struct futx * fu)69a627ac61SEd Schouten utx_active_add(const struct futx *fu)
70a627ac61SEd Schouten {
711ae6a21dSEd Schouten FILE *fp;
72339f34e3SEd Schouten struct futx fe;
73339f34e3SEd Schouten off_t partial;
741ae6a21dSEd Schouten int error, ret;
75a627ac61SEd Schouten
76339f34e3SEd Schouten partial = -1;
77339f34e3SEd Schouten ret = 0;
78339f34e3SEd Schouten
79a627ac61SEd Schouten /*
80a627ac61SEd Schouten * Register user login sessions. Overwrite entries of sessions
81a627ac61SEd Schouten * that have already been terminated.
82a627ac61SEd Schouten */
83a627ac61SEd Schouten fp = futx_open(_PATH_UTX_ACTIVE);
84a627ac61SEd Schouten if (fp == NULL)
851ae6a21dSEd Schouten return (-1);
861ae6a21dSEd Schouten while (fread(&fe, sizeof(fe), 1, fp) == 1) {
87a627ac61SEd Schouten switch (fe.fu_type) {
88c5cf53fcSEd Schouten case BOOT_TIME:
89c5cf53fcSEd Schouten /* Leave these intact. */
90c5cf53fcSEd Schouten break;
91a627ac61SEd Schouten case USER_PROCESS:
92a627ac61SEd Schouten case INIT_PROCESS:
93a627ac61SEd Schouten case LOGIN_PROCESS:
94a627ac61SEd Schouten case DEAD_PROCESS:
95a627ac61SEd Schouten /* Overwrite when ut_id matches. */
961ae6a21dSEd Schouten if (memcmp(fu->fu_id, fe.fu_id, sizeof(fe.fu_id)) ==
971ae6a21dSEd Schouten 0) {
981ae6a21dSEd Schouten ret = fseeko(fp, -(off_t)sizeof(fe), SEEK_CUR);
99a627ac61SEd Schouten goto exact;
100a627ac61SEd Schouten }
101a627ac61SEd Schouten if (fe.fu_type != DEAD_PROCESS)
102a627ac61SEd Schouten break;
103a627ac61SEd Schouten /* FALLTHROUGH */
104a627ac61SEd Schouten default:
105a627ac61SEd Schouten /* Allow us to overwrite unused records. */
1061ae6a21dSEd Schouten if (partial == -1) {
1071ae6a21dSEd Schouten partial = ftello(fp);
1081ae6a21dSEd Schouten /*
1091ae6a21dSEd Schouten * Distinguish errors from valid values so we
1101ae6a21dSEd Schouten * don't overwrite good data by accident.
1111ae6a21dSEd Schouten */
1121ae6a21dSEd Schouten if (partial != -1)
1131ae6a21dSEd Schouten partial -= (off_t)sizeof(fe);
1141ae6a21dSEd Schouten }
115a627ac61SEd Schouten break;
116a627ac61SEd Schouten }
117a627ac61SEd Schouten }
118a627ac61SEd Schouten
119a627ac61SEd Schouten /*
120a627ac61SEd Schouten * No exact match found. Use the partial match. If no partial
121a627ac61SEd Schouten * match was found, just append a new record.
122a627ac61SEd Schouten */
123a627ac61SEd Schouten if (partial != -1)
1241ae6a21dSEd Schouten ret = fseeko(fp, partial, SEEK_SET);
125a627ac61SEd Schouten exact:
1261ae6a21dSEd Schouten if (ret == -1)
1271ae6a21dSEd Schouten error = errno;
1281ae6a21dSEd Schouten else if (fwrite(fu, sizeof(*fu), 1, fp) < 1)
1291ae6a21dSEd Schouten error = errno;
1301ae6a21dSEd Schouten else
1311ae6a21dSEd Schouten error = 0;
132a627ac61SEd Schouten fclose(fp);
13300e080beSJilles Tjoelker if (error != 0)
1341ae6a21dSEd Schouten errno = error;
1351ae6a21dSEd Schouten return (error == 0 ? 0 : 1);
136a627ac61SEd Schouten }
137a627ac61SEd Schouten
138a627ac61SEd Schouten static int
utx_active_remove(struct futx * fu)139a627ac61SEd Schouten utx_active_remove(struct futx *fu)
140a627ac61SEd Schouten {
1411ae6a21dSEd Schouten FILE *fp;
142339f34e3SEd Schouten struct futx fe;
1431ae6a21dSEd Schouten int error, ret;
144a627ac61SEd Schouten
145a627ac61SEd Schouten /*
146a627ac61SEd Schouten * Remove user login sessions, having the same ut_id.
147a627ac61SEd Schouten */
148a627ac61SEd Schouten fp = futx_open(_PATH_UTX_ACTIVE);
149a627ac61SEd Schouten if (fp == NULL)
1501ae6a21dSEd Schouten return (-1);
1511ae6a21dSEd Schouten error = ESRCH;
1521ae6a21dSEd Schouten ret = -1;
1531ae6a21dSEd Schouten while (fread(&fe, sizeof(fe), 1, fp) == 1 && ret != 0)
154a627ac61SEd Schouten switch (fe.fu_type) {
155a627ac61SEd Schouten case USER_PROCESS:
156a627ac61SEd Schouten case INIT_PROCESS:
157a627ac61SEd Schouten case LOGIN_PROCESS:
1581ae6a21dSEd Schouten if (memcmp(fu->fu_id, fe.fu_id, sizeof(fe.fu_id)) != 0)
159a627ac61SEd Schouten continue;
160a627ac61SEd Schouten
161a627ac61SEd Schouten /* Terminate session. */
1621ae6a21dSEd Schouten if (fseeko(fp, -(off_t)sizeof(fe), SEEK_CUR) == -1)
1631ae6a21dSEd Schouten error = errno;
1641ae6a21dSEd Schouten else if (fwrite(fu, sizeof(*fu), 1, fp) < 1)
1651ae6a21dSEd Schouten error = errno;
1661ae6a21dSEd Schouten else
1671ae6a21dSEd Schouten ret = 0;
1681ae6a21dSEd Schouten
169a627ac61SEd Schouten }
170a627ac61SEd Schouten
171a627ac61SEd Schouten fclose(fp);
17200e080beSJilles Tjoelker if (ret != 0)
1731ae6a21dSEd Schouten errno = error;
1741ae6a21dSEd Schouten return (ret);
175a627ac61SEd Schouten }
176a627ac61SEd Schouten
177a627ac61SEd Schouten static void
utx_active_init(const struct futx * fu)178c5cf53fcSEd Schouten utx_active_init(const struct futx *fu)
179c5cf53fcSEd Schouten {
180c5cf53fcSEd Schouten int fd;
181c5cf53fcSEd Schouten
182c5cf53fcSEd Schouten /* Initialize utx.active with a single BOOT_TIME record. */
183c5cf53fcSEd Schouten fd = _open(_PATH_UTX_ACTIVE, O_CREAT|O_RDWR|O_TRUNC, 0644);
184c5cf53fcSEd Schouten if (fd < 0)
185c5cf53fcSEd Schouten return;
186c5cf53fcSEd Schouten _write(fd, fu, sizeof(*fu));
187c5cf53fcSEd Schouten _close(fd);
188c5cf53fcSEd Schouten }
189c5cf53fcSEd Schouten
190c5cf53fcSEd Schouten static void
utx_active_purge(void)191a627ac61SEd Schouten utx_active_purge(void)
192a627ac61SEd Schouten {
193a627ac61SEd Schouten
194a627ac61SEd Schouten truncate(_PATH_UTX_ACTIVE, 0);
195a627ac61SEd Schouten }
196a627ac61SEd Schouten
1979301df81SEd Schouten static int
utx_lastlogin_add(const struct futx * fu)198a627ac61SEd Schouten utx_lastlogin_add(const struct futx *fu)
199a627ac61SEd Schouten {
200a627ac61SEd Schouten struct futx fe;
2011ae6a21dSEd Schouten FILE *fp;
2021ae6a21dSEd Schouten int error, ret;
2031ae6a21dSEd Schouten
2041ae6a21dSEd Schouten ret = 0;
205a627ac61SEd Schouten
206a627ac61SEd Schouten /*
207a627ac61SEd Schouten * Write an entry to lastlogin. Overwrite the entry if the
208a627ac61SEd Schouten * current user already has an entry. If not, append a new
209a627ac61SEd Schouten * entry.
210a627ac61SEd Schouten */
211a627ac61SEd Schouten fp = futx_open(_PATH_UTX_LASTLOGIN);
212a627ac61SEd Schouten if (fp == NULL)
2131ae6a21dSEd Schouten return (-1);
214a627ac61SEd Schouten while (fread(&fe, sizeof fe, 1, fp) == 1) {
215a627ac61SEd Schouten if (strncmp(fu->fu_user, fe.fu_user, sizeof fe.fu_user) != 0)
216a627ac61SEd Schouten continue;
217a627ac61SEd Schouten
218a627ac61SEd Schouten /* Found a previous lastlogin entry for this user. */
2191ae6a21dSEd Schouten ret = fseeko(fp, -(off_t)sizeof fe, SEEK_CUR);
220a627ac61SEd Schouten break;
221a627ac61SEd Schouten }
2221ae6a21dSEd Schouten if (ret == -1)
2231ae6a21dSEd Schouten error = errno;
2241ae6a21dSEd Schouten else if (fwrite(fu, sizeof *fu, 1, fp) < 1) {
2251ae6a21dSEd Schouten error = errno;
2261ae6a21dSEd Schouten ret = -1;
2271ae6a21dSEd Schouten }
228a627ac61SEd Schouten fclose(fp);
22900e080beSJilles Tjoelker if (ret == -1)
2301ae6a21dSEd Schouten errno = error;
2311ae6a21dSEd Schouten return (ret);
232a627ac61SEd Schouten }
233a627ac61SEd Schouten
234a627ac61SEd Schouten static void
utx_lastlogin_upgrade(void)235a627ac61SEd Schouten utx_lastlogin_upgrade(void)
236a627ac61SEd Schouten {
237a627ac61SEd Schouten struct stat sb;
2381ae6a21dSEd Schouten int fd;
239a627ac61SEd Schouten
24005eb11cbSJilles Tjoelker fd = _open(_PATH_UTX_LASTLOGIN, O_RDWR|O_CLOEXEC, 0644);
241a627ac61SEd Schouten if (fd < 0)
242a627ac61SEd Schouten return;
243a627ac61SEd Schouten
244a627ac61SEd Schouten /*
245a627ac61SEd Schouten * Truncate broken lastlogin files. In the future we should
246a627ac61SEd Schouten * check for older versions of the file format here and try to
247a627ac61SEd Schouten * upgrade it.
248a627ac61SEd Schouten */
249a627ac61SEd Schouten if (_fstat(fd, &sb) != -1 && sb.st_size % sizeof(struct futx) != 0)
250a627ac61SEd Schouten ftruncate(fd, 0);
251a627ac61SEd Schouten _close(fd);
252a627ac61SEd Schouten }
253a627ac61SEd Schouten
2549301df81SEd Schouten static int
utx_log_add(const struct futx * fu)255a627ac61SEd Schouten utx_log_add(const struct futx *fu)
256a627ac61SEd Schouten {
257a627ac61SEd Schouten struct iovec vec[2];
2581ae6a21dSEd Schouten int error, fd;
2591ae6a21dSEd Schouten uint16_t l;
260a627ac61SEd Schouten
261a627ac61SEd Schouten /*
262a627ac61SEd Schouten * Append an entry to the log file. We only need to append
263a627ac61SEd Schouten * records to this file, so to conserve space, trim any trailing
264a627ac61SEd Schouten * zero-bytes. Prepend a length field, indicating the length of
265a627ac61SEd Schouten * the record, excluding the length field itself.
266a627ac61SEd Schouten */
2671ae6a21dSEd Schouten for (l = sizeof(*fu); l > 0 && ((const char *)fu)[l - 1] == '\0'; l--) ;
268a627ac61SEd Schouten vec[0].iov_base = &l;
2691ae6a21dSEd Schouten vec[0].iov_len = sizeof(l);
270a627ac61SEd Schouten vec[1].iov_base = __DECONST(void *, fu);
271a627ac61SEd Schouten vec[1].iov_len = l;
272a627ac61SEd Schouten l = htobe16(l);
273a627ac61SEd Schouten
27405eb11cbSJilles Tjoelker fd = _open(_PATH_UTX_LOG, O_CREAT|O_WRONLY|O_APPEND|O_CLOEXEC, 0644);
275a627ac61SEd Schouten if (fd < 0)
2761ae6a21dSEd Schouten return (-1);
2771ae6a21dSEd Schouten if (_writev(fd, vec, 2) == -1)
2781ae6a21dSEd Schouten error = errno;
2791ae6a21dSEd Schouten else
2801ae6a21dSEd Schouten error = 0;
281a627ac61SEd Schouten _close(fd);
28200e080beSJilles Tjoelker if (error != 0)
2831ae6a21dSEd Schouten errno = error;
2841ae6a21dSEd Schouten return (error == 0 ? 0 : 1);
285a627ac61SEd Schouten }
286a627ac61SEd Schouten
287a627ac61SEd Schouten struct utmpx *
pututxline(const struct utmpx * utmpx)288a627ac61SEd Schouten pututxline(const struct utmpx *utmpx)
289a627ac61SEd Schouten {
290a627ac61SEd Schouten struct futx fu;
291339f34e3SEd Schouten int bad;
292339f34e3SEd Schouten
293339f34e3SEd Schouten bad = 0;
294a627ac61SEd Schouten
295a627ac61SEd Schouten utx_to_futx(utmpx, &fu);
296a627ac61SEd Schouten
297a627ac61SEd Schouten switch (fu.fu_type) {
298a627ac61SEd Schouten case BOOT_TIME:
299c5cf53fcSEd Schouten utx_active_init(&fu);
300c5cf53fcSEd Schouten utx_lastlogin_upgrade();
301c5cf53fcSEd Schouten break;
302a627ac61SEd Schouten case SHUTDOWN_TIME:
303a627ac61SEd Schouten utx_active_purge();
304a627ac61SEd Schouten break;
305a627ac61SEd Schouten case OLD_TIME:
306a627ac61SEd Schouten case NEW_TIME:
307a627ac61SEd Schouten break;
308a627ac61SEd Schouten case USER_PROCESS:
3099301df81SEd Schouten bad |= utx_active_add(&fu);
3109301df81SEd Schouten bad |= utx_lastlogin_add(&fu);
311a627ac61SEd Schouten break;
312a627ac61SEd Schouten #if 0 /* XXX: Are these records of any use to us? */
313a627ac61SEd Schouten case INIT_PROCESS:
314a627ac61SEd Schouten case LOGIN_PROCESS:
3159301df81SEd Schouten bad |= utx_active_add(&fu);
316a627ac61SEd Schouten break;
317a627ac61SEd Schouten #endif
318a627ac61SEd Schouten case DEAD_PROCESS:
3199301df81SEd Schouten /*
3209301df81SEd Schouten * In case writing a logout entry fails, never attempt
3219301df81SEd Schouten * to write it to utx.log. The logout entry's ut_id
3229301df81SEd Schouten * might be invalid.
3239301df81SEd Schouten */
324a627ac61SEd Schouten if (utx_active_remove(&fu) != 0)
325a627ac61SEd Schouten return (NULL);
326a627ac61SEd Schouten break;
327a627ac61SEd Schouten default:
3281ae6a21dSEd Schouten errno = EINVAL;
329a627ac61SEd Schouten return (NULL);
330a627ac61SEd Schouten }
331a627ac61SEd Schouten
3329301df81SEd Schouten bad |= utx_log_add(&fu);
3339301df81SEd Schouten return (bad ? NULL : futx_to_utx(&fu));
334a627ac61SEd Schouten }
335