1/*-
2 * SPDX-License-Identifier: BSD-4-Clause
3 *
4 * Copyright (c) 1996 John M. Vinopal
5 * Copyright (c) 2018 Philip Paeps
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
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.
16 * 3. All advertising materials mentioning features or use of this software
17 *    must display the following acknowledgement:
18 *	This product includes software developed for the NetBSD Project
19 *	by John M. Vinopal.
20 * 4. The name of the author may not be used to endorse or promote products
21 *    derived from this software without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
30 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
31 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36#include <sys/cdefs.h>
37#ifndef lint
38__RCSID("$NetBSD: lastlogin.c,v 1.4 1998/02/03 04:45:35 perry Exp $");
39#endif
40
41#include <err.h>
42#include <stdio.h>
43#include <stdlib.h>
44#include <string.h>
45#include <time.h>
46#include <unistd.h>
47#include <utmpx.h>
48
49#include <libxo/xo.h>
50
51	int	main(int, char **);
52static	void	output(struct utmpx *);
53static	void	usage(void);
54static int	utcmp_user(const void *, const void *);
55
56static int	order = 1;
57static const char *file = NULL;
58static int	(*utcmp)(const void *, const void *) = utcmp_user;
59
60static int
61utcmp_user(const void *u1, const void *u2)
62{
63
64	return (order * strcmp(((const struct utmpx *)u1)->ut_user,
65	    ((const struct utmpx *)u2)->ut_user));
66}
67
68static int
69utcmp_time(const void *u1, const void *u2)
70{
71	time_t t1, t2;
72
73	t1 = ((const struct utmpx *)u1)->ut_tv.tv_sec;
74	t2 = ((const struct utmpx *)u2)->ut_tv.tv_sec;
75	return (t1 < t2 ? order : t1 > t2 ? -order : 0);
76}
77
78int
79main(int argc, char *argv[])
80{
81	int	ch, i, ulistsize;
82	struct utmpx *u, *ulist;
83
84	argc = xo_parse_args(argc, argv);
85	if (argc < 0)
86		exit(1);
87
88	while ((ch = getopt(argc, argv, "f:rt")) != -1) {
89		switch (ch) {
90		case 'f':
91			file = optarg;
92			break;
93		case 'r':
94			order = -1;
95			break;
96		case 't':
97			utcmp = utcmp_time;
98			break;
99		default:
100			usage();
101		}
102	}
103	argc -= optind;
104	argv += optind;
105
106	xo_open_container("lastlogin-information");
107	xo_open_list("lastlogin");
108
109	if (argc > 0) {
110		/* Process usernames given on the command line. */
111		for (i = 0; i < argc; i++) {
112			if (setutxdb(UTXDB_LASTLOGIN, file) != 0)
113				xo_err(1, "failed to open lastlog database");
114			if ((u = getutxuser(argv[i])) == NULL) {
115				xo_warnx("user '%s' not found", argv[i]);
116				continue;
117			}
118			output(u);
119			endutxent();
120		}
121	} else {
122		/* Read all lastlog entries, looking for active ones. */
123		if (setutxdb(UTXDB_LASTLOGIN, file) != 0)
124			xo_err(1, "failed to open lastlog database");
125		ulist = NULL;
126		ulistsize = 0;
127		while ((u = getutxent()) != NULL) {
128			if (u->ut_type != USER_PROCESS)
129				continue;
130			if ((ulistsize % 16) == 0) {
131				ulist = realloc(ulist,
132				    (ulistsize + 16) * sizeof(struct utmpx));
133				if (ulist == NULL)
134					xo_err(1, "malloc");
135			}
136			ulist[ulistsize++] = *u;
137		}
138		endutxent();
139
140		qsort(ulist, ulistsize, sizeof(struct utmpx), utcmp);
141		for (i = 0; i < ulistsize; i++)
142			output(&ulist[i]);
143	}
144
145	xo_close_list("lastlogin");
146	xo_close_container("lastlogin-information");
147	xo_finish();
148
149	exit(0);
150}
151
152/* Duplicate the output of last(1) */
153static void
154output(struct utmpx *u)
155{
156	time_t t = u->ut_tv.tv_sec;
157
158	xo_open_instance("lastlogin");
159	xo_emit("{:user/%-10s/%s} {:tty/%-8s/%s} {:from/%-22.22s/%s}",
160		u->ut_user, u->ut_line, u->ut_host);
161	xo_attr("seconds", "%lu", (unsigned long)t);
162	xo_emit(" {:login-time/%.24s/%.24s}\n", ctime(&t));
163	xo_close_instance("lastlogin");
164}
165
166static void
167usage(void)
168{
169	xo_error("usage: lastlogin [-f file] [-rt] [user ...]\n");
170	xo_finish();
171	exit(1);
172}
173