summaryrefslogtreecommitdiffstats
path: root/lib/libulog/ulog_getutxent.c
blob: bcfab480cbedf1df2f46cd1aedc6a5dba37b91a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/*-
 * Copyright (c) 2009 Ed Schouten <ed@FreeBSD.org>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include <sys/param.h>

#include <pwd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <timeconv.h>

#include "ulog_internal.h"

static FILE *ufile;
static int ufileindex = -1;

void
ulog_endutxent(void)
{
	if (ufile != NULL)
		fclose(ufile);
	ufile = NULL;
}

/*
 * Conversion from on-disk formats to generic ulog_utmpx structure.
 */

static void
ulog_futmp_to_utmpx(const struct futmp *ut, struct ulog_utmpx *utx)
{

	memset(utx, 0, sizeof *utx);
#define	COPY_STRING(field) do {						\
	strncpy(utx->ut_ ## field, ut->ut_ ## field,			\
	    MIN(sizeof utx->ut_ ## field - 1, sizeof ut->ut_ ## field));\
} while (0)
#define	MATCH(field, value)	(strncmp(ut->ut_ ## field, (value), \
					sizeof(ut->ut_ ## field)) == 0)
	if (MATCH(user, "reboot") && MATCH(line, "~"))
		utx->ut_type = BOOT_TIME;
	else if (MATCH(user, "date") && MATCH(line, "|"))
		utx->ut_type = OLD_TIME;
	else if (MATCH(user, "date") && MATCH(line, "{"))
		utx->ut_type = NEW_TIME;
	else if (MATCH(user, "shutdown") && MATCH(line, "~"))
		utx->ut_type = SHUTDOWN_TIME;
	else if (MATCH(user, "") && MATCH(host, "")) {
		utx->ut_type = DEAD_PROCESS;
		/* XXX: ut_id and ut_pid missing. ut_line not needed. */
		COPY_STRING(line);
	} else if (!MATCH(user, "") && !MATCH(line, "") && ut->ut_time != 0) {
		utx->ut_type = USER_PROCESS;
		/* XXX: ut_id and ut_pid missing. */
		COPY_STRING(user);
		COPY_STRING(line);
		COPY_STRING(host);
	} else {
		/* Only set ut_type for EMPTY. */
		utx->ut_type = EMPTY;
		return;
	}
#undef COPY_STRING
#undef MATCH
	utx->ut_tv.tv_sec = _time32_to_time(ut->ut_time);
	utx->ut_tv.tv_usec = 0;
}

static void
ulog_flastlog_to_utmpx(const struct flastlog *ll, struct ulog_utmpx *utx)
{

	memset(utx, 0, sizeof *utx);
#define	COPY_STRING(field) do {						\
	strncpy(utx->ut_ ## field, ll->ll_ ## field,			\
	    MIN(sizeof utx->ut_ ## field - 1, sizeof ll->ll_ ## field));\
} while (0)
#define	MATCH(field, value)	(strncmp(ll->ll_ ## field, (value), \
					sizeof(ll->ll_ ## field)) == 0)
	if (!MATCH(line, "") && ll->ll_time != 0) {
		utx->ut_type = USER_PROCESS;
		/* XXX: ut_id and ut_pid missing. */
		COPY_STRING(line);
		COPY_STRING(host);
	} else {
		/* Only set ut_type for EMPTY. */
		utx->ut_type = EMPTY;
		return;
	}
#undef COPY_STRING
#undef MATCH
	utx->ut_tv.tv_sec = _time32_to_time(ll->ll_time);
	utx->ut_tv.tv_usec = 0;
}

/*
 * File I/O.
 */

static inline off_t
ulog_tell(void)
{

	if (ufileindex == UTXI_USER)
		return (ftello(ufile) / sizeof(struct flastlog));
	else
		return (ftello(ufile) / sizeof(struct futmp));
}

static struct ulog_utmpx *
ulog_read(off_t off, int whence, int resolve_user)
{
	static struct ulog_utmpx utx;

	if (ufile == NULL)
		ulog_setutxent();
	if (ufile == NULL)
		return (NULL);

	/* Only allow seeking to move forward. */
	if (whence == SEEK_SET && ulog_tell() > off)
		return (NULL);

	if (ufileindex == UTXI_USER) {
		struct flastlog ll;
		struct passwd *pw = NULL;
		uid_t uid;

		if (fseeko(ufile, off * sizeof ll, whence) != 0)
			return (NULL);
		uid = ulog_tell();
		if (fread(&ll, sizeof ll, 1, ufile) != 1)
			return (NULL);
		ulog_flastlog_to_utmpx(&ll, &utx);
		if (utx.ut_type == USER_PROCESS && resolve_user)
			pw = getpwuid(uid);
		if (pw != NULL)
			strlcpy(utx.ut_user, pw->pw_name, sizeof utx.ut_user);
		else
			sprintf(utx.ut_user, "%u", (unsigned int)uid);
	} else {
		struct futmp ut;

		if (fseeko(ufile, off * sizeof(struct futmp), whence) != 0)
			return (NULL);
		if (fread(&ut, sizeof ut, 1, ufile) != 1)
			return (NULL);
		ulog_futmp_to_utmpx(&ut, &utx);
	}
	return (&utx);
}

/*
 * getutxent().
 *
 * Read the next entry from the file.
 */

struct ulog_utmpx *
ulog_getutxent(void)
{

	return ulog_read(0, SEEK_CUR, 1);
}

/*
 * ulog_getutxline().
 *
 * Read entries from the file, until reaching an entry which matches the
 * provided TTY device name.  We can optimize the case for utmp files,
 * because they are indexed by TTY device name.
 */

struct ulog_utmpx *
ulog_getutxline(const struct ulog_utmpx *line)
{
	struct ulog_utmpx *utx;

	if (ufile == NULL)
		ulog_setutxent();
	if (ufile == NULL)
		return (NULL);

	if (ufileindex == UTXI_TTY) {
		unsigned int slot;

		slot = ulog_ttyslot(line->ut_line);
		if (slot == 0)
			return (NULL);
		utx = ulog_read(slot, SEEK_SET, 1);
		if (utx->ut_type == USER_PROCESS &&
		    strcmp(utx->ut_line, line->ut_line) == 0)
			return (utx);
		return (NULL);
	} else {
		for (;;) {
			utx = ulog_read(0, SEEK_CUR, 1);
			if (utx == NULL)
				return (NULL);
			if (utx->ut_type == USER_PROCESS &&
			    strcmp(utx->ut_line, line->ut_line) == 0)
				return (utx);
		}
	}
}

/*
 * ulog_getutxuser().
 *
 * Read entries from the file, until reaching an entry which matches the
 * provided username.  We can optimize the case for lastlog files,
 * because they are indexed by user ID.
 */

struct ulog_utmpx *
ulog_getutxuser(const char *user)
{
	struct ulog_utmpx *utx;

	if (ufileindex == UTXI_USER) {
		struct passwd *pw;

		pw = getpwnam(user);
		if (pw == NULL)
			return (NULL);
		utx = ulog_read(pw->pw_uid, SEEK_SET, 0);
		if (utx != NULL)
			strlcpy(utx->ut_user, user, sizeof utx->ut_user);
		return (utx);
	} else {
		for (;;) {
			utx = ulog_read(0, SEEK_CUR, 1);
			if (utx == NULL)
				return (NULL);
			if (utx->ut_type == USER_PROCESS &&
			    strcmp(utx->ut_user, user) == 0)
				return (utx);
		}
	}
}

/*
 * ulog_setutxfile().
 *
 * Switch to a different record file.  When no filename is provided, the
 * system default is opened.
 */

int
ulog_setutxfile(int uidx, const char *file)
{

	/* Supply default files. */
	switch (uidx) {
	case UTXI_TTY:
		if (file == NULL)
			file = _PATH_UTMP;
		break;
	case UTXI_TIME:
		if (file == NULL)
			file = _PATH_WTMP;
		break;
	case UTXI_USER:
		if (file == NULL)
			file = _PATH_LASTLOG;
		break;
	default:
		return (-1);
	}

	if (ufile != NULL)
		fclose(ufile);
	ufile = fopen(file, "r");
	ufileindex = uidx;
	if (ufile == NULL)
		return (-1);
	return (0);
}

/*
 * ulog_endutxfile().
 *
 * Close any opened files.
 */

void
ulog_setutxent(void)
{

	ulog_setutxfile(UTXI_TTY, NULL);
}
OpenPOWER on IntegriCloud