1/*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2010 Ed Schouten <ed@FreeBSD.org>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29#include "namespace.h"
30#include <sys/endian.h>
31#include <sys/param.h>
32#include <sys/stat.h>
33#include <errno.h>
34#include <stdio.h>
35#include <string.h>
36#include <utmpx.h>
37#include "utxdb.h"
38#include "un-namespace.h"
39
40static _Thread_local FILE *uf = NULL;
41static _Thread_local int udb;
42
43int
44setutxdb(int db, const char *file)
45{
46	struct stat sb;
47
48	switch (db) {
49	case UTXDB_ACTIVE:
50		if (file == NULL)
51			file = _PATH_UTX_ACTIVE;
52		break;
53	case UTXDB_LASTLOGIN:
54		if (file == NULL)
55			file = _PATH_UTX_LASTLOGIN;
56		break;
57	case UTXDB_LOG:
58		if (file == NULL)
59			file = _PATH_UTX_LOG;
60		break;
61	default:
62		errno = EINVAL;
63		return (-1);
64	}
65
66	if (uf != NULL)
67		fclose(uf);
68	uf = fopen(file, "re");
69	if (uf == NULL)
70		return (-1);
71
72	if (db != UTXDB_LOG) {
73		/* Safety check: never use broken files. */
74		if (_fstat(fileno(uf), &sb) != -1 &&
75		    sb.st_size % sizeof(struct futx) != 0) {
76			fclose(uf);
77			uf = NULL;
78			errno = EFTYPE;
79			return (-1);
80		}
81		/* Prevent reading of partial records. */
82		(void)setvbuf(uf, NULL, _IOFBF,
83		    rounddown(BUFSIZ, sizeof(struct futx)));
84	}
85
86	udb = db;
87	return (0);
88}
89
90void
91setutxent(void)
92{
93
94	setutxdb(UTXDB_ACTIVE, NULL);
95}
96
97void
98endutxent(void)
99{
100
101	if (uf != NULL) {
102		fclose(uf);
103		uf = NULL;
104	}
105}
106
107static int
108getfutxent(struct futx *fu)
109{
110
111	if (uf == NULL)
112		setutxent();
113	if (uf == NULL)
114		return (-1);
115
116	if (udb == UTXDB_LOG) {
117		uint16_t len;
118
119retry:
120		if (fread(&len, sizeof(len), 1, uf) != 1)
121			return (-1);
122		len = be16toh(len);
123		if (len == 0) {
124			/*
125			 * XXX: Though zero-size records are valid in theory,
126			 * they can never occur in practice. Zero-size records
127			 * indicate file corruption. Seek one byte forward, to
128			 * see if we can find a record there.
129			 */
130			ungetc('\0', uf);
131			goto retry;
132		}
133		if (len > sizeof *fu) {
134			/* Forward compatibility. */
135			if (fread(fu, sizeof(*fu), 1, uf) != 1)
136				return (-1);
137			fseek(uf, len - sizeof(*fu), SEEK_CUR);
138		} else {
139			/* Partial record. */
140			memset(fu, 0, sizeof(*fu));
141			if (fread(fu, len, 1, uf) != 1)
142				return (-1);
143		}
144	} else {
145		if (fread(fu, sizeof(*fu), 1, uf) != 1)
146			return (-1);
147	}
148	return (0);
149}
150
151struct utmpx *
152getutxent(void)
153{
154	struct futx fu;
155
156	if (getfutxent(&fu) != 0)
157		return (NULL);
158	return (futx_to_utx(&fu));
159}
160
161struct utmpx *
162getutxid(const struct utmpx *id)
163{
164	struct futx fu;
165
166	for (;;) {
167		if (getfutxent(&fu) != 0)
168			return (NULL);
169
170		switch (fu.fu_type) {
171		case USER_PROCESS:
172		case INIT_PROCESS:
173		case LOGIN_PROCESS:
174		case DEAD_PROCESS:
175			switch (id->ut_type) {
176			case USER_PROCESS:
177			case INIT_PROCESS:
178			case LOGIN_PROCESS:
179			case DEAD_PROCESS:
180				if (memcmp(fu.fu_id, id->ut_id,
181				    MIN(sizeof(fu.fu_id), sizeof(id->ut_id))) ==
182				    0)
183					goto found;
184			}
185			break;
186		default:
187			if (fu.fu_type == id->ut_type)
188				goto found;
189			break;
190		}
191	}
192
193found:
194	return (futx_to_utx(&fu));
195}
196
197struct utmpx *
198getutxline(const struct utmpx *line)
199{
200	struct futx fu;
201
202	for (;;) {
203		if (getfutxent(&fu) != 0)
204			return (NULL);
205
206		switch (fu.fu_type) {
207		case USER_PROCESS:
208		case LOGIN_PROCESS:
209			if (strncmp(fu.fu_line, line->ut_line,
210			    MIN(sizeof(fu.fu_line), sizeof(line->ut_line))) ==
211			    0)
212				goto found;
213			break;
214		}
215	}
216
217found:
218	return (futx_to_utx(&fu));
219}
220
221struct utmpx *
222getutxuser(const char *user)
223{
224	struct futx fu;
225
226	for (;;) {
227		if (getfutxent(&fu) != 0)
228			return (NULL);
229
230		switch (fu.fu_type) {
231		case USER_PROCESS:
232			if (strncmp(fu.fu_user, user, sizeof(fu.fu_user)) == 0)
233				goto found;
234			break;
235		}
236	}
237
238found:
239	return (futx_to_utx(&fu));
240}
241