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>
51 static FILE *fp = NULL;
52 static int readonly = 0;
53 static struct utmpx ut;
54 static char utfile[MAXPATHLEN] = _PATH_UTMPX;
56 static struct utmpx *utmp_update(const struct utmpx *);
58 static const char vers[] = "utmpx-2.00";
59 static utx_db_t dbtype = UTX_DB_UTMPX;
60 DB *lastlogx_db = NULL;
67 if ((fp = fopen(fname, "r+")) == NULL)
68 if ((fp = fopen(fname, "w+")) == NULL) {
69 if ((fp = fopen(fname, "r")) == NULL)
75 /* get file size in order to check if new file */
76 if (fstat(fileno(fp), &st) == -1)
79 if (st.st_size == 0) {
80 /* new file, add signature record */
81 (void)memset(&ut, 0, sizeof(ut));
82 ut.ut_type = SIGNATURE;
83 (void)memcpy(ut.ut_user, vers, sizeof(vers));
84 if (fwrite(&ut, sizeof(ut), 1, fp) != 1)
87 /* old file, read signature record */
88 if (fread(&ut, sizeof(ut), 1, fp) != 1)
90 if (memcmp(ut.ut_user, vers, 5) != 0 ||
91 ut.ut_type != SIGNATURE) {
101 (void)memset(&ut, 0, sizeof(ut));
106 setutxdb(utx_db_t db_type, char *fname)
124 /* A previous db file is still open */
129 if ((_open_db(fname)) == -1)
140 (void)memset(&ut, 0, sizeof(ut));
145 if (dbtype != UTX_DB_UTMPX)
146 setutxdb(UTX_DB_UTMPX, utfile);
148 (void)fseeko(fp, (off_t)sizeof(ut), SEEK_SET);
154 (void)memset(&ut, 0, sizeof(ut));
167 if ((_open_db(utfile)) == -1)
171 if (fread(&ut, sizeof(ut), 1, fp) != 1)
176 (void)memset(&ut, 0, sizeof(ut));
181 getutxid(const struct utmpx *utx)
184 _DIAGASSERT(utx != NULL);
186 if (utx->ut_type == EMPTY)
190 if (ut.ut_type == EMPTY)
192 switch (utx->ut_type) {
199 if (ut.ut_type == utx->ut_type)
206 switch (ut.ut_type) {
211 if (memcmp(ut.ut_id, utx->ut_id,
212 sizeof(ut.ut_id)) == 0)
222 } while (getutxent() != NULL);
227 getutxline(const struct utmpx *utx)
230 _DIAGASSERT(utx != NULL);
233 switch (ut.ut_type) {
238 if (strncmp(ut.ut_line, utx->ut_line,
239 sizeof(ut.ut_line)) == 0)
245 } while (getutxent() != NULL);
250 pututxline(const struct utmpx *utx)
254 struct utmpx temp, *u = NULL;
257 _DIAGASSERT(utx != NULL);
262 if (utx->ut_type == USER_PROCESS) {
263 ll.ll_tv = utx->ut_tv;
264 strcpy(ll.ll_host, utx->ut_host);
265 strcpy(ll.ll_line, utx->ut_line);
266 pw = getpwnam(utx->ut_name);
268 updlastlogx(_PATH_LASTLOGX, pw->pw_uid, &ll);
271 if (strcmp(_PATH_UTMPX, utfile) == 0)
272 if ((fp != NULL && readonly) || (fp == NULL && geteuid() != 0))
273 return utmp_update(utx);
276 (void)memcpy(&temp, utx, sizeof(temp));
280 if (fp == NULL || readonly)
284 if (getutxid(&temp) == NULL) {
286 if (getutxid(&temp) == NULL) {
287 if (lockf(fileno(fp), F_LOCK, (off_t)0) == -1)
290 if (fseeko(fp, (off_t)0, SEEK_END) == -1)
296 /* we are not appending */
297 if (fseeko(fp, -(off_t)sizeof(ut), SEEK_CUR) == -1)
301 if (fwrite(&temp, sizeof (temp), 1, fp) != 1)
304 if (fflush(fp) == -1)
307 u = memcpy(&ut, &temp, sizeof(ut));
310 if (lockf(fileno(fp), F_ULOCK, (off_t)0) == -1)
316 static struct utmpx *
317 utmp_update(const struct utmpx *utx)
319 char buf[sizeof(*utx) * 4 + 1];
323 _DIAGASSERT(utx != NULL);
325 (void)strvisx(buf, (const char *)(const void *)utx, sizeof(*utx),
327 switch (pid = fork()) {
329 (void)execl(_PATH_UTMP_UPDATE,
330 strrchr(_PATH_UTMP_UPDATE, '/') + 1, buf, NULL);
336 if (waitpid(pid, &status, 0) == -1)
338 if (WIFEXITED(status) && WEXITSTATUS(status) == 0)
339 return memcpy(&ut, utx, sizeof(ut));
346 * The following are extensions and not part of the X/Open spec.
349 updwtmpx(const char *file, const struct utmpx *utx)
351 (void)_updwtmpx(file, utx);
355 _updwtmpx(const char *file, const struct utmpx *utx)
360 _DIAGASSERT(file != NULL);
361 _DIAGASSERT(utx != NULL);
363 fd = open(file, O_WRONLY|O_APPEND|O_SHLOCK);
366 if ((fd = open(file, O_CREAT|O_WRONLY|O_EXLOCK, 0644)) == -1)
368 (void)memset(&ut, 0, sizeof(ut));
369 ut.ut_type = SIGNATURE;
370 (void)memcpy(ut.ut_user, vers, sizeof(vers));
371 if (write(fd, &ut, sizeof(ut)) == -1)
374 if (write(fd, utx, sizeof(*utx)) == -1)
388 utmpxname(const char *fname)
392 _DIAGASSERT(fname != NULL);
396 if (len >= sizeof(utfile))
400 if (fname[len - 1] != 'x')
403 (void)strlcpy(utfile, fname, sizeof(utfile));
409 getutmp(const struct utmpx *ux, struct utmp *u)
412 _DIAGASSERT(ux != NULL);
413 _DIAGASSERT(u != NULL);
415 (void)memcpy(u->ut_name, ux->ut_name, sizeof(u->ut_name));
416 (void)memcpy(u->ut_line, ux->ut_line, sizeof(u->ut_line));
417 (void)memcpy(u->ut_host, ux->ut_host, sizeof(u->ut_host));
418 u->ut_time = ux->ut_tv.tv_sec;
422 getutmpx(const struct utmp *u, struct utmpx *ux)
425 _DIAGASSERT(ux != NULL);
426 _DIAGASSERT(u != NULL);
428 (void)memcpy(ux->ut_name, u->ut_name, sizeof(u->ut_name));
429 (void)memcpy(ux->ut_line, u->ut_line, sizeof(u->ut_line));
430 (void)memcpy(ux->ut_host, u->ut_host, sizeof(u->ut_host));
431 ux->ut_tv.tv_sec = u->ut_time;
432 ux->ut_tv.tv_usec = 0;
433 (void)memset(&ux->ut_ss, 0, sizeof(ux->ut_ss));
435 ux->ut_type = USER_PROCESS;
437 ux->ut_exit.e_termination = 0;
438 ux->ut_exit.e_exit = 0;
442 getlastlogx(const char *fname, uid_t uid, struct lastlogx *ll)
447 _DIAGASSERT(fname != NULL);
448 _DIAGASSERT(ll != NULL);
450 db = dbopen(fname, O_RDONLY|O_SHLOCK, 0, DB_HASH, NULL);
456 key.size = sizeof(uid);
458 if ((db->get)(db, &key, &data, 0) != 0)
461 if (data.size != sizeof(*ll)) {
467 if ((ll = malloc(sizeof(*ll))) == NULL)
470 (void)memcpy(ll, data.data, sizeof(*ll));
480 updlastlogx(const char *fname, uid_t uid, struct lastlogx *ll)
486 _DIAGASSERT(fname != NULL);
487 _DIAGASSERT(ll != NULL);
489 db = dbopen(fname, O_RDWR|O_CREAT|O_EXLOCK, 0644, DB_HASH, NULL);
495 key.size = sizeof(uid);
497 data.size = sizeof(*ll);
498 if ((db->put)(db, &key, &data, 0) != 0)