xref: /src/usr.sbin/lastlogin/lastlogin.c (revision afdbf109c6a661a729938f68211054a0a50d38ac)
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 #define LASTLOGIN_XO_VERSION "1"
52 
53 	int	main(int, char **);
54 static	void	output(struct utmpx *);
55 static	void	usage(void);
56 static int	utcmp_user(const void *, const void *);
57 
58 static int	order = 1;
59 static const char *file = NULL;
60 static int	(*utcmp)(const void *, const void *) = utcmp_user;
61 
62 static int
63 utcmp_user(const void *u1, const void *u2)
64 {
65 
66 	return (order * strcmp(((const struct utmpx *)u1)->ut_user,
67 	    ((const struct utmpx *)u2)->ut_user));
68 }
69 
70 static int
71 utcmp_time(const void *u1, const void *u2)
72 {
73 	time_t t1, t2;
74 
75 	t1 = ((const struct utmpx *)u1)->ut_tv.tv_sec;
76 	t2 = ((const struct utmpx *)u2)->ut_tv.tv_sec;
77 	return (t1 < t2 ? order : t1 > t2 ? -order : 0);
78 }
79 
80 int
81 main(int argc, char *argv[])
82 {
83 	int	ch, i, ulistsize;
84 	struct utmpx *u, *ulist;
85 
86 	argc = xo_parse_args(argc, argv);
87 	if (argc < 0)
88 		exit(1);
89 
90 	while ((ch = getopt(argc, argv, "f:rt")) != -1) {
91 		switch (ch) {
92 		case 'f':
93 			file = optarg;
94 			break;
95 		case 'r':
96 			order = -1;
97 			break;
98 		case 't':
99 			utcmp = utcmp_time;
100 			break;
101 		default:
102 			usage();
103 		}
104 	}
105 	argc -= optind;
106 	argv += optind;
107 
108 	xo_set_version(LASTLOGIN_XO_VERSION);
109 	xo_open_container("lastlogin-information");
110 	xo_open_list("lastlogin");
111 
112 	if (argc > 0) {
113 		/* Process usernames given on the command line. */
114 		for (i = 0; i < argc; i++) {
115 			if (setutxdb(UTXDB_LASTLOGIN, file) != 0)
116 				xo_err(1, "failed to open lastlog database");
117 			if ((u = getutxuser(argv[i])) == NULL) {
118 				xo_warnx("user '%s' not found", argv[i]);
119 				continue;
120 			}
121 			output(u);
122 			endutxent();
123 		}
124 	} else {
125 		/* Read all lastlog entries, looking for active ones. */
126 		if (setutxdb(UTXDB_LASTLOGIN, file) != 0)
127 			xo_err(1, "failed to open lastlog database");
128 		ulist = NULL;
129 		ulistsize = 0;
130 		while ((u = getutxent()) != NULL) {
131 			if (u->ut_type != USER_PROCESS)
132 				continue;
133 			if ((ulistsize % 16) == 0) {
134 				ulist = realloc(ulist,
135 				    (ulistsize + 16) * sizeof(struct utmpx));
136 				if (ulist == NULL)
137 					xo_err(1, "malloc");
138 			}
139 			ulist[ulistsize++] = *u;
140 		}
141 		endutxent();
142 
143 		qsort(ulist, ulistsize, sizeof(struct utmpx), utcmp);
144 		for (i = 0; i < ulistsize; i++)
145 			output(&ulist[i]);
146 	}
147 
148 	xo_close_list("lastlogin");
149 	xo_close_container("lastlogin-information");
150 	xo_finish();
151 
152 	exit(0);
153 }
154 
155 /* Duplicate the output of last(1) */
156 static void
157 output(struct utmpx *u)
158 {
159 	time_t t = u->ut_tv.tv_sec;
160 
161 	xo_open_instance("lastlogin");
162 	xo_emit("{:user/%-10s/%s} {:tty/%-8s/%s} {:from/%-22.22s/%s}",
163 		u->ut_user, u->ut_line, u->ut_host);
164 	xo_attr("seconds", "%lu", (unsigned long)t);
165 	xo_emit(" {:login-time/%.24s/%.24s}\n", ctime(&t));
166 	xo_close_instance("lastlogin");
167 }
168 
169 static void
170 usage(void)
171 {
172 	xo_error("usage: lastlogin [-f file] [-rt] [user ...]\n");
173 	xo_finish();
174 	exit(1);
175 }
176