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