2 * Copyright (c) 2002 The NetBSD Foundation, Inc.
5 * This code is derived from software contributed to The NetBSD Foundation
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
30 #include "namespace.h"
31 #include <sys/types.h>
32 #include <sys/param.h>
33 #include <sys/socket.h>
48 #include "un-namespace.h"
53 static int readonly = 0;
54 static struct utmpx ut;
55 static char utfile[MAXPATHLEN] = _PATH_UTMPX;
57 static struct utmpx *utmp_update(const struct utmpx *);
59 static const char vers[] = "utmpx-2.00";
60 static utx_db_t dbtype = UTX_DB_UTMPX;
61 DB *lastlogx_db = NULL;
63 void past_getutmp(void *, void *);
64 void past_getutmpx(void *, void *);
67 _open_db(const char *fname)
71 if ((fp = fopen(fname, "re+")) == NULL)
72 if ((fp = fopen(fname, "we+")) == NULL) {
73 if ((fp = fopen(fname, "re")) == NULL)
79 /* get file size in order to check if new file */
80 if (_fstat(fileno(fp), &st) == -1)
83 if (st.st_size == 0) {
84 /* new file, add signature record */
85 (void)memset(&ut, 0, sizeof(ut));
86 ut.ut_type = SIGNATURE;
87 (void)memcpy(ut.ut_user, vers, sizeof(vers));
88 if (fwrite(&ut, sizeof(ut), 1, fp) != 1)
91 /* old file, read signature record */
92 if (fread(&ut, sizeof(ut), 1, fp) != 1)
94 if (memcmp(ut.ut_user, vers, 5) != 0 ||
95 ut.ut_type != SIGNATURE) {
105 (void)memset(&ut, 0, sizeof(ut));
110 setutxdb(utx_db_t db_type, const char *fname)
128 /* A previous db file is still open */
133 if ((_open_db(fname)) == -1)
143 (void)memset(&ut, 0, sizeof(ut));
146 (void)fseeko(fp, (off_t)sizeof(ut), SEEK_SET);
152 (void)memset(&ut, 0, sizeof(ut));
165 if ((_open_db(utfile)) == -1)
169 if (fread(&ut, sizeof(ut), 1, fp) != 1)
174 (void)memset(&ut, 0, sizeof(ut));
179 getutxid(const struct utmpx *utx)
182 _DIAGASSERT(utx != NULL);
184 if (utx->ut_type == EMPTY)
188 if (ut.ut_type == EMPTY)
190 switch (utx->ut_type) {
197 if (ut.ut_type == utx->ut_type)
204 switch (ut.ut_type) {
209 if (memcmp(ut.ut_id, utx->ut_id,
210 sizeof(ut.ut_id)) == 0)
220 } while (getutxent() != NULL);
225 getutxline(const struct utmpx *utx)
228 _DIAGASSERT(utx != NULL);
231 switch (ut.ut_type) {
236 if (strncmp(ut.ut_line, utx->ut_line,
237 sizeof(ut.ut_line)) == 0)
243 } while (getutxent() != NULL);
248 getutxuser(const char *user)
250 _DIAGASSERT(utx != NULL);
253 switch (ut.ut_type) {
257 if (strncmp(ut.ut_user, user, sizeof(ut.ut_user)) == 0)
263 } while (getutxent() != NULL);
268 pututxline(const struct utmpx *utx)
272 struct utmpx temp, *u = NULL;
275 _DIAGASSERT(utx != NULL);
280 if (utx->ut_type == USER_PROCESS) {
281 ll.ll_tv = utx->ut_tv;
282 strcpy(ll.ll_host, utx->ut_host);
283 strcpy(ll.ll_line, utx->ut_line);
284 pw = getpwnam(utx->ut_name);
286 updlastlogx(_PATH_LASTLOGX, pw->pw_uid, &ll);
289 if (strcmp(_PATH_UTMPX, utfile) == 0)
290 if ((fp != NULL && readonly) || (fp == NULL && geteuid() != 0))
291 return utmp_update(utx);
294 (void)memcpy(&temp, utx, sizeof(temp));
298 if (fp == NULL || readonly)
302 if (getutxid(&temp) == NULL) {
304 if (getutxid(&temp) == NULL) {
305 if (lockf(fileno(fp), F_LOCK, (off_t)0) == -1)
308 if (fseeko(fp, (off_t)0, SEEK_END) == -1)
314 /* we are not appending */
315 if (fseeko(fp, -(off_t)sizeof(ut), SEEK_CUR) == -1)
319 if (fwrite(&temp, sizeof (temp), 1, fp) != 1)
322 if (fflush(fp) == -1)
325 u = memcpy(&ut, &temp, sizeof(ut));
328 if (lockf(fileno(fp), F_ULOCK, (off_t)0) == -1)
334 static struct utmpx *
335 utmp_update(const struct utmpx *utx)
337 char buf[sizeof(*utx) * 4 + 1];
341 _DIAGASSERT(utx != NULL);
343 (void)strvisx(buf, (const char *)(const void *)utx, sizeof(*utx),
344 VIS_WHITE | VIS_NOLOCALE);
345 switch (pid = fork()) {
347 (void)execl(_PATH_UTMP_UPDATE,
348 strrchr(_PATH_UTMP_UPDATE, '/') + 1, buf, NULL);
354 if (_waitpid(pid, &status, 0) == -1)
356 if (WIFEXITED(status) && WEXITSTATUS(status) == 0)
357 return memcpy(&ut, utx, sizeof(ut));
364 * The following are extensions and not part of the X/Open spec.
367 updwtmpx(const char *file, const struct utmpx *utx)
369 (void)_updwtmpx(file, utx);
373 _updwtmpx(const char *file, const struct utmpx *utx)
379 _DIAGASSERT(file != NULL);
380 _DIAGASSERT(utx != NULL);
382 fd = _open(file, O_WRONLY|O_APPEND|O_SHLOCK|O_CLOEXEC);
385 if ((fd = _open(file, O_CREAT|O_WRONLY|O_EXLOCK|O_CLOEXEC, 0644)) == -1)
388 if (_fstat(fd, &st) == -1)
390 if (st.st_size == 0) {
391 /* new file, add signature record */
392 (void)memset(&ut, 0, sizeof(ut));
393 ut.ut_type = SIGNATURE;
394 (void)memcpy(ut.ut_user, vers, sizeof(vers));
395 if (_write(fd, &ut, sizeof(ut)) == -1)
398 if (_write(fd, utx, sizeof(*utx)) == -1)
400 if (_close(fd) == -1)
413 utmpxname(const char *fname)
417 _DIAGASSERT(fname != NULL);
421 if (len >= sizeof(utfile))
425 if (fname[len - 1] != 'x')
428 (void)strlcpy(utfile, fname, sizeof(utfile));
434 __sym_compat(getutmp, past_getutmp, DF404.0);
436 past_getutmp(void *ux __unused, void *u __unused)
440 __sym_compat(getutmpx, past_getutmpx, DF404.0);
442 past_getutmpx(void *u __unused, void *ux __unused)
447 getlastlogx(const char *fname, uid_t uid, struct lastlogx *ll)
452 _DIAGASSERT(fname != NULL);
453 _DIAGASSERT(ll != NULL);
455 db = dbopen(fname, O_RDONLY|O_SHLOCK|O_CLOEXEC, 0, DB_HASH, NULL);
461 key.size = sizeof(uid);
463 if ((db->get)(db, &key, &data, 0) != 0)
466 if (data.size != sizeof(*ll)) {
472 if ((ll = malloc(sizeof(*ll))) == NULL)
475 (void)memcpy(ll, data.data, sizeof(*ll));
485 updlastlogx(const char *fname, uid_t uid, struct lastlogx *ll)
491 _DIAGASSERT(fname != NULL);
492 _DIAGASSERT(ll != NULL);
494 db = dbopen(fname, O_RDWR|O_CREAT|O_EXLOCK|O_CLOEXEC, 0644, DB_HASH, NULL);
500 key.size = sizeof(uid);
502 data.size = sizeof(*ll);
503 if ((db->put)(db, &key, &data, 0) != 0)