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