main netmisc / finger / utmpentry.c
  1/*	$NetBSD: utmpentry.c,v 1.22 2021/02/26 02:45:43 christos Exp $	*/
  2
  3/*-
  4 * Copyright (c) 2002 The NetBSD Foundation, Inc.
  5 * All rights reserved.
  6 *
  7 * This code is derived from software contributed to The NetBSD Foundation
  8 * by Christos Zoulas.
  9 *
 10 * Redistribution and use in source and binary forms, with or without
 11 * modification, are permitted provided that the following conditions
 12 * are met:
 13 * 1. Redistributions of source code must retain the above copyright
 14 *    notice, this list of conditions and the following disclaimer.
 15 * 2. Redistributions in binary form must reproduce the above copyright
 16 *    notice, this list of conditions and the following disclaimer in the
 17 *    documentation and/or other materials provided with the distribution.
 18 *
 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 22 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 29 * POSSIBILITY OF SUCH DAMAGE.
 30 */
 31
 32#include <sys/cdefs.h>
 33#ifndef lint
 34__RCSID("$NetBSD: utmpentry.c,v 1.22 2021/02/26 02:45:43 christos Exp $");
 35#endif
 36
 37#include <sys/stat.h>
 38
 39#include <time.h>
 40#include <string.h>
 41#include <err.h>
 42#include <stdlib.h>
 43
 44#ifdef SUPPORT_UTMP
 45#include <utmp.h>
 46#endif
 47#ifdef SUPPORT_UTMPX
 48#include <utmpx.h>
 49#endif
 50
 51#include "utmpentry.h"
 52
 53
 54#define COMPILE_ASSERT(x) ((void)0)
 55
 56
 57#ifdef SUPPORT_UTMP
 58static void getentry(struct utmpentry *, struct utmp *);
 59static struct timespec utmptime = {0, 0};
 60#endif
 61#ifdef SUPPORT_UTMPX
 62static void getentryx(struct utmpentry *, struct utmpx *);
 63static struct timespec utmpxtime = {0, 0};
 64#endif
 65#if defined(SUPPORT_UTMPX) || defined(SUPPORT_UTMP)
 66static int setup(const char *);
 67static void adjust_size(struct utmpentry *e);
 68#endif
 69
 70size_t maxname = 8, maxline = 8, maxhost = 16;
 71int etype = 1 << USER_PROCESS;
 72static size_t numutmp = 0;
 73static struct utmpentry *ehead;
 74
 75#if defined(SUPPORT_UTMPX) || defined(SUPPORT_UTMP)
 76static void
 77adjust_size(struct utmpentry *e)
 78{
 79	size_t max;
 80
 81	if ((max = strlen(e->name)) > maxname)
 82		maxname = max;
 83	if ((max = strlen(e->line)) > maxline)
 84		maxline = max;
 85	if ((max = strlen(e->host)) > maxhost)
 86		maxhost = max;
 87}
 88
 89static int
 90setup(const char *fname)
 91{
 92	int what = 3;
 93	struct stat st;
 94	const char *sfname;
 95
 96	if (fname != NULL) {
 97		size_t len = strlen(fname);
 98		if (len == 0)
 99			errx(1, "Filename cannot be 0 length.");
100		what = fname[len - 1] == 'x' ? 1 : 2;
101		if (what == 1) {
102#ifdef SUPPORT_UTMPX
103			if (utmpxname(fname) == 0)
104				warnx("Cannot set utmpx file to `%s'",
105				    fname);
106#else
107			warnx("utmpx support not compiled in");
108#endif
109		} else {
110#ifdef SUPPORT_UTMP
111			if (utmpname(fname) == 0)
112				warnx("Cannot set utmp file to `%s'",
113				    fname);
114#else
115			warnx("utmp support not compiled in");
116#endif
117		}
118	}
119#ifdef SUPPORT_UTMPX
120	if (what & 1) {
121		sfname = fname ? fname : _PATH_UTMPX;
122		if (stat(sfname, &st) == -1) {
123			warn("Cannot stat `%s'", sfname);
124			what &= ~1;
125		} else {
126			if (timespeccmp(&st.st_mtim, &utmpxtime, >))
127				utmpxtime = st.st_mtim;
128			else
129				what &= ~1;
130		}
131	}
132#endif
133#ifdef SUPPORT_UTMP
134	if (what & 2) {
135		sfname = fname ? fname : _PATH_UTMP;
136		if (stat(sfname, &st) == -1) {
137			warn("Cannot stat `%s'", sfname);
138			what &= ~2;
139		} else {
140			if (timespeccmp(&st.st_mtim, &utmptime, >))
141				utmptime = st.st_mtim;
142			else
143				what &= ~2;
144		}
145	}
146#endif
147	return what;
148}
149#endif
150
151void
152endutentries(void)
153{
154	struct utmpentry *ep;
155
156#ifdef SUPPORT_UTMP
157	timespecclear(&utmptime);
158#endif
159#ifdef SUPPORT_UTMPX
160	timespecclear(&utmpxtime);
161#endif
162	ep = ehead;
163	while (ep) {
164		struct utmpentry *sep = ep;
165		ep = ep->next;
166		free(sep);
167	}
168	ehead = NULL;
169	numutmp = 0;
170}
171
172size_t
173getutentries(const char *fname, struct utmpentry **epp)
174{
175#ifdef SUPPORT_UTMPX
176	struct utmpx *utx;
177#endif
178#ifdef SUPPORT_UTMP
179	struct utmp *ut;
180#endif
181#if defined(SUPPORT_UTMP) || defined(SUPPORT_UTMPX)
182	struct utmpentry *ep;
183	int what = setup(fname);
184	struct utmpentry **nextp = &ehead;
185	switch (what) {
186	case 0:
187		/* No updates */
188		*epp = ehead;
189		return numutmp;
190	default:
191		/* Need to re-scan */
192		ehead = NULL;
193		numutmp = 0;
194	}
195#endif
196
197#ifdef SUPPORT_UTMPX
198	setutxent();
199	while ((what & 1) && (utx = getutxent()) != NULL) {
200		if (fname == NULL && ((1 << utx->ut_type) & etype) == 0)
201			continue;
202		if ((ep = calloc(1, sizeof(*ep))) == NULL) {
203			warn(NULL);
204			return 0;
205		}
206		getentryx(ep, utx);
207		*nextp = ep;
208		nextp = &(ep->next);
209	}
210#endif
211
212#ifdef SUPPORT_UTMP
213	setutent();
214	if ((etype & (1 << USER_PROCESS)) != 0) {
215		while ((what & 2) && (ut = getutent()) != NULL) {
216			if (fname == NULL && (*ut->ut_name == '\0' ||
217			    *ut->ut_line == '\0'))
218				continue;
219			/* Don't process entries that we have utmpx for */
220			for (ep = ehead; ep != NULL; ep = ep->next) {
221				if (strncmp(ep->line, ut->ut_line,
222				    sizeof(ut->ut_line)) == 0)
223					break;
224			}
225			if (ep != NULL)
226				continue;
227			if ((ep = calloc(1, sizeof(*ep))) == NULL) {
228				warn(NULL);
229				return 0;
230			}
231			getentry(ep, ut);
232			*nextp = ep;
233			nextp = &(ep->next);
234		}
235	}
236#endif
237	numutmp = 0;
238#if defined(SUPPORT_UTMP) || defined(SUPPORT_UTMPX)
239	if (ehead != NULL) {
240		struct utmpentry *from = ehead, *save;
241
242		ehead = NULL;
243		while (from != NULL) {
244			for (nextp = &ehead;
245			    (*nextp) && strcmp(from->line, (*nextp)->line) > 0;
246			    nextp = &(*nextp)->next)
247				continue;
248			save = from;
249			from = from->next;
250			save->next = *nextp;
251			*nextp = save;
252			numutmp++;
253		}
254	}
255	*epp = ehead;
256	return numutmp;
257#else
258	*epp = NULL;
259	return 0;
260#endif
261}
262
263#ifdef SUPPORT_UTMP
264static void
265getentry(struct utmpentry *e, struct utmp *up)
266{
267	COMPILE_ASSERT(sizeof(e->name) > sizeof(up->ut_name));
268	COMPILE_ASSERT(sizeof(e->line) > sizeof(up->ut_line));
269	COMPILE_ASSERT(sizeof(e->host) > sizeof(up->ut_host));
270
271	/*
272	 * e has just been calloc'd. We don't need to clear it or
273	 * append null-terminators, because its length is strictly
274	 * greater than the source string. Use memcpy to read up->ut_*
275	 * because they may not be terminated. For this reason we use
276	 * the size of the source as the length argument.
277	 */
278	memcpy(e->name, up->ut_name, sizeof(up->ut_name));
279	memcpy(e->line, up->ut_line, sizeof(up->ut_line));
280	memcpy(e->host, up->ut_host, sizeof(up->ut_host));
281
282	e->tv.tv_sec = up->ut_time;
283	e->tv.tv_usec = 0;
284	e->pid = 0;
285	e->term = 0;
286	e->exit = 0;
287	e->sess = 0;
288	e->type = USER_PROCESS;
289	adjust_size(e);
290}
291#endif
292
293#ifdef SUPPORT_UTMPX
294static void
295getentryx(struct utmpentry *e, struct utmpx *up)
296{
297	COMPILE_ASSERT(sizeof(e->name) > sizeof(up->ut_name));
298	COMPILE_ASSERT(sizeof(e->line) > sizeof(up->ut_line));
299	COMPILE_ASSERT(sizeof(e->host) > sizeof(up->ut_host));
300
301	/*
302	 * e has just been calloc'd. We don't need to clear it or
303	 * append null-terminators, because its length is strictly
304	 * greater than the source string. Use memcpy to read up->ut_*
305	 * because they may not be terminated. For this reason we use
306	 * the size of the source as the length argument.
307	 */
308	memcpy(e->name, up->ut_name, sizeof(up->ut_name));
309	memcpy(e->line, up->ut_line, sizeof(up->ut_line));
310	memcpy(e->host, up->ut_host, sizeof(up->ut_host));
311
312	e->tv = up->ut_tv;
313	e->pid = up->ut_pid;
314	e->term = up->ut_exit.e_termination;
315	e->exit = up->ut_exit.e_exit;
316	e->sess = up->ut_session;
317	e->type = up->ut_type;
318	adjust_size(e);
319}
320#endif