xref: /freebsd/usr.bin/rwho/rwho.c (revision 0b8224d1cc9dc6c9778ba04a75b2c8d47e5d7481)
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 #include <sys/capsicum.h>
34 #include <sys/param.h>
35 #include <sys/file.h>
36 
37 #include <protocols/rwhod.h>
38 
39 #include <capsicum_helpers.h>
40 #include <dirent.h>
41 #include <err.h>
42 #include <errno.h>
43 #include <langinfo.h>
44 #include <locale.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <time.h>
49 #include <timeconv.h>
50 #include <unistd.h>
51 
52 #define	NUSERS		1000
53 #define	WHDRSIZE	(ssize_t)(sizeof(wd) - sizeof(wd.wd_we))
54 /*
55  * this macro should be shared with ruptime.
56  */
57 #define	down(w,now)	((now) - (w)->wd_recvtime > 11 * 60)
58 
59 static DIR	*dirp;
60 static struct	whod wd;
61 static int	nusers;
62 static struct	myutmp {
63 	char    myhost[sizeof(wd.wd_hostname)];
64 	int	myidle;
65 	struct	outmp myutmp;
66 } myutmp[NUSERS];
67 
68 static time_t	now;
69 static int	aflg;
70 
71 static void usage(void) __dead2;
72 static int utmpcmp(const void *, const void *);
73 
74 int
main(int argc,char * argv[])75 main(int argc, char *argv[])
76 {
77 	int ch;
78 	struct dirent *dp;
79 	int width;
80 	ssize_t cc;
81 	struct whod *w;
82 	struct whoent *we;
83 	struct myutmp *mp;
84 	cap_rights_t rights;
85 	int f, n, i;
86 	int d_first;
87 	int dfd;
88 	time_t ct;
89 
90 	w = &wd;
91 	(void) setlocale(LC_TIME, "");
92 	d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
93 
94 	while ((ch = getopt(argc, argv, "a")) != -1) {
95 		switch ((char)ch) {
96 		case 'a':
97 			aflg = 1;
98 			break;
99 		case '?':
100 		default:
101 			usage();
102 		}
103 	}
104 	argc -= optind;
105 	argv += optind;
106 
107 	if (argc != 0)
108 		usage();
109 
110 	if (chdir(_PATH_RWHODIR) < 0)
111 		err(1, "chdir(%s)", _PATH_RWHODIR);
112 	if ((dirp = opendir(".")) == NULL)
113 		err(1, "opendir(%s)", _PATH_RWHODIR);
114 	dfd = dirfd(dirp);
115 	mp = myutmp;
116 	cap_rights_init(&rights, CAP_READ, CAP_LOOKUP);
117 	if (caph_rights_limit(dfd, &rights) < 0)
118 		err(1, "cap_rights_limit failed: %s", _PATH_RWHODIR);
119 	/*
120 	 * Cache files required for time(3) and localtime(3) before entering
121 	 * capability mode.
122 	 */
123 	(void) time(&ct);
124 	(void) localtime(&ct);
125 	if (caph_enter() < 0)
126 		err(1, "cap_enter");
127 	(void) time(&now);
128 	cap_rights_init(&rights, CAP_READ);
129 	while ((dp = readdir(dirp)) != NULL) {
130 		if (dp->d_ino == 0 || strncmp(dp->d_name, "whod.", 5) != 0)
131 			continue;
132 		f = openat(dfd, dp->d_name, O_RDONLY);
133 		if (f < 0)
134 			continue;
135 		if (caph_rights_limit(f, &rights) < 0)
136 			err(1, "cap_rights_limit failed: %s", dp->d_name);
137 		cc = read(f, (char *)&wd, sizeof(struct whod));
138 		if (cc < WHDRSIZE) {
139 			(void) close(f);
140 			continue;
141 		}
142 		if (down(w, now) != 0) {
143 			(void) close(f);
144 			continue;
145 		}
146 		cc -= WHDRSIZE;
147 		we = w->wd_we;
148 		for (n = cc / sizeof(struct whoent); n > 0; n--) {
149 			if (aflg == 0 && we->we_idle >= 60 * 60) {
150 				we++;
151 				continue;
152 			}
153 			if (nusers >= NUSERS)
154 				errx(1, "too many users");
155 			mp->myutmp = we->we_utmp;
156 			mp->myidle = we->we_idle;
157 			(void) strcpy(mp->myhost, w->wd_hostname);
158 			nusers++;
159 			we++;
160 			mp++;
161 		}
162 		(void) close(f);
163 	}
164 	qsort((char *)myutmp, nusers, sizeof(struct myutmp), utmpcmp);
165 	mp = myutmp;
166 	width = 0;
167 	for (i = 0; i < nusers; i++) {
168 		/* append one for the blank and use 8 for the out_line */
169 		int j;
170 
171 		j = strlen(mp->myhost) + 1 + sizeof(mp->myutmp.out_line);
172 		if (j > width)
173 			width = j;
174 		mp++;
175 	}
176 	mp = myutmp;
177 	for (i = 0; i < nusers; i++) {
178 		char buf[BUFSIZ], cbuf[80];
179 		time_t t;
180 
181 		t = _int_to_time(mp->myutmp.out_time);
182 		strftime(cbuf, sizeof(cbuf), d_first ? "%e %b %R" : "%b %e %R",
183 		    localtime(&t));
184 		(void) sprintf(buf, "%s:%-.*s", mp->myhost,
185 		    (int)sizeof(mp->myutmp.out_line), mp->myutmp.out_line);
186 		printf("%-*.*s %-*s %s",
187 		    (int)sizeof(mp->myutmp.out_name),
188 		    (int)sizeof(mp->myutmp.out_name),
189 		    mp->myutmp.out_name, width, buf, cbuf);
190 		mp->myidle /= 60;
191 		if (mp->myidle != 0) {
192 			if (aflg != 0) {
193 				if (mp->myidle >= 100 * 60)
194 					mp->myidle = 100 * 60 - 1;
195 				if (mp->myidle >= 60)
196 					printf(" %2d", mp->myidle / 60);
197 				else
198 					printf("   ");
199 			} else {
200 				printf(" ");
201 			}
202 			printf(":%02d", mp->myidle % 60);
203 		}
204 		printf("\n");
205 		mp++;
206 	}
207 	exit(0);
208 }
209 
210 
211 static void
usage(void)212 usage(void)
213 {
214 
215 	fprintf(stderr, "usage: rwho [-a]\n");
216 	exit(1);
217 }
218 
219 #define MYUTMP(a) ((const struct myutmp *)(a))
220 
221 static int
utmpcmp(const void * u1,const void * u2)222 utmpcmp(const void *u1, const void *u2)
223 {
224 	int rc;
225 
226 	rc = strncmp(MYUTMP(u1)->myutmp.out_name, MYUTMP(u2)->myutmp.out_name,
227 	    sizeof(MYUTMP(u2)->myutmp.out_name));
228 	if (rc != 0)
229 		return (rc);
230 	rc = strcmp(MYUTMP(u1)->myhost, MYUTMP(u2)->myhost);
231 	if (rc != 0)
232 		return (rc);
233 	return (strncmp(MYUTMP(u1)->myutmp.out_line,
234 	    MYUTMP(u2)->myutmp.out_line, sizeof(MYUTMP(u2)->myutmp.out_line)));
235 }
236