xref: /freebsd/usr.bin/rwho/rwho.c (revision 1d386b48)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1983, 1993 The Regents of the University of California.
5  * Copyright (c) 2013 Mariusz Zaborski <oshogbo@FreeBSD.org>
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. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 #ifndef lint
34 static const char copyright[] =
35 "@(#) Copyright (c) 1983, 1993\n\
36 	The Regents of the University of California.  All rights reserved.\n";
37 #endif /* not lint */
38 
39 #ifndef lint
40 #if 0
41 static char sccsid[] = "@(#)rwho.c	8.1 (Berkeley) 6/6/93";
42 #endif
43 #endif /* not lint */
44 
45 #include <sys/cdefs.h>
46 #include <sys/capsicum.h>
47 #include <sys/param.h>
48 #include <sys/file.h>
49 
50 #include <protocols/rwhod.h>
51 
52 #include <capsicum_helpers.h>
53 #include <dirent.h>
54 #include <err.h>
55 #include <errno.h>
56 #include <langinfo.h>
57 #include <locale.h>
58 #include <stdio.h>
59 #include <stdlib.h>
60 #include <string.h>
61 #include <time.h>
62 #include <timeconv.h>
63 #include <unistd.h>
64 
65 #define	NUSERS		1000
66 #define	WHDRSIZE	(ssize_t)(sizeof(wd) - sizeof(wd.wd_we))
67 /*
68  * this macro should be shared with ruptime.
69  */
70 #define	down(w,now)	((now) - (w)->wd_recvtime > 11 * 60)
71 
72 static DIR	*dirp;
73 static struct	whod wd;
74 static int	nusers;
75 static struct	myutmp {
76 	char    myhost[sizeof(wd.wd_hostname)];
77 	int	myidle;
78 	struct	outmp myutmp;
79 } myutmp[NUSERS];
80 
81 static time_t	now;
82 static int	aflg;
83 
84 static void usage(void) __dead2;
85 static int utmpcmp(const void *, const void *);
86 
87 int
88 main(int argc, char *argv[])
89 {
90 	int ch;
91 	struct dirent *dp;
92 	int width;
93 	ssize_t cc;
94 	struct whod *w;
95 	struct whoent *we;
96 	struct myutmp *mp;
97 	cap_rights_t rights;
98 	int f, n, i;
99 	int d_first;
100 	int dfd;
101 	time_t ct;
102 
103 	w = &wd;
104 	(void) setlocale(LC_TIME, "");
105 	d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
106 
107 	while ((ch = getopt(argc, argv, "a")) != -1) {
108 		switch ((char)ch) {
109 		case 'a':
110 			aflg = 1;
111 			break;
112 		case '?':
113 		default:
114 			usage();
115 		}
116 	}
117 	argc -= optind;
118 	argv += optind;
119 
120 	if (argc != 0)
121 		usage();
122 
123 	if (chdir(_PATH_RWHODIR) < 0)
124 		err(1, "chdir(%s)", _PATH_RWHODIR);
125 	if ((dirp = opendir(".")) == NULL)
126 		err(1, "opendir(%s)", _PATH_RWHODIR);
127 	dfd = dirfd(dirp);
128 	mp = myutmp;
129 	cap_rights_init(&rights, CAP_READ, CAP_LOOKUP);
130 	if (caph_rights_limit(dfd, &rights) < 0)
131 		err(1, "cap_rights_limit failed: %s", _PATH_RWHODIR);
132 	/*
133 	 * Cache files required for time(3) and localtime(3) before entering
134 	 * capability mode.
135 	 */
136 	(void) time(&ct);
137 	(void) localtime(&ct);
138 	if (caph_enter() < 0)
139 		err(1, "cap_enter");
140 	(void) time(&now);
141 	cap_rights_init(&rights, CAP_READ);
142 	while ((dp = readdir(dirp)) != NULL) {
143 		if (dp->d_ino == 0 || strncmp(dp->d_name, "whod.", 5) != 0)
144 			continue;
145 		f = openat(dfd, dp->d_name, O_RDONLY);
146 		if (f < 0)
147 			continue;
148 		if (caph_rights_limit(f, &rights) < 0)
149 			err(1, "cap_rights_limit failed: %s", dp->d_name);
150 		cc = read(f, (char *)&wd, sizeof(struct whod));
151 		if (cc < WHDRSIZE) {
152 			(void) close(f);
153 			continue;
154 		}
155 		if (down(w, now) != 0) {
156 			(void) close(f);
157 			continue;
158 		}
159 		cc -= WHDRSIZE;
160 		we = w->wd_we;
161 		for (n = cc / sizeof(struct whoent); n > 0; n--) {
162 			if (aflg == 0 && we->we_idle >= 60 * 60) {
163 				we++;
164 				continue;
165 			}
166 			if (nusers >= NUSERS)
167 				errx(1, "too many users");
168 			mp->myutmp = we->we_utmp;
169 			mp->myidle = we->we_idle;
170 			(void) strcpy(mp->myhost, w->wd_hostname);
171 			nusers++;
172 			we++;
173 			mp++;
174 		}
175 		(void) close(f);
176 	}
177 	qsort((char *)myutmp, nusers, sizeof(struct myutmp), utmpcmp);
178 	mp = myutmp;
179 	width = 0;
180 	for (i = 0; i < nusers; i++) {
181 		/* append one for the blank and use 8 for the out_line */
182 		int j;
183 
184 		j = strlen(mp->myhost) + 1 + sizeof(mp->myutmp.out_line);
185 		if (j > width)
186 			width = j;
187 		mp++;
188 	}
189 	mp = myutmp;
190 	for (i = 0; i < nusers; i++) {
191 		char buf[BUFSIZ], cbuf[80];
192 		time_t t;
193 
194 		t = _int_to_time(mp->myutmp.out_time);
195 		strftime(cbuf, sizeof(cbuf), d_first ? "%e %b %R" : "%b %e %R",
196 		    localtime(&t));
197 		(void) sprintf(buf, "%s:%-.*s", mp->myhost,
198 		    (int)sizeof(mp->myutmp.out_line), mp->myutmp.out_line);
199 		printf("%-*.*s %-*s %s",
200 		    (int)sizeof(mp->myutmp.out_name),
201 		    (int)sizeof(mp->myutmp.out_name),
202 		    mp->myutmp.out_name, width, buf, cbuf);
203 		mp->myidle /= 60;
204 		if (mp->myidle != 0) {
205 			if (aflg != 0) {
206 				if (mp->myidle >= 100 * 60)
207 					mp->myidle = 100 * 60 - 1;
208 				if (mp->myidle >= 60)
209 					printf(" %2d", mp->myidle / 60);
210 				else
211 					printf("   ");
212 			} else {
213 				printf(" ");
214 			}
215 			printf(":%02d", mp->myidle % 60);
216 		}
217 		printf("\n");
218 		mp++;
219 	}
220 	exit(0);
221 }
222 
223 
224 static void
225 usage(void)
226 {
227 
228 	fprintf(stderr, "usage: rwho [-a]\n");
229 	exit(1);
230 }
231 
232 #define MYUTMP(a) ((const struct myutmp *)(a))
233 
234 static int
235 utmpcmp(const void *u1, const void *u2)
236 {
237 	int rc;
238 
239 	rc = strncmp(MYUTMP(u1)->myutmp.out_name, MYUTMP(u2)->myutmp.out_name,
240 	    sizeof(MYUTMP(u2)->myutmp.out_name));
241 	if (rc != 0)
242 		return (rc);
243 	rc = strcmp(MYUTMP(u1)->myhost, MYUTMP(u2)->myhost);
244 	if (rc != 0)
245 		return (rc);
246 	return (strncmp(MYUTMP(u1)->myutmp.out_line,
247 	    MYUTMP(u2)->myutmp.out_line, sizeof(MYUTMP(u2)->myutmp.out_line)));
248 }
249