1 /* 2 * Copyright (c) 1983, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34 #include <sys/cdefs.h> 35 #ifndef lint 36 __COPYRIGHT("@(#) Copyright (c) 1983, 1993\n\ 37 The Regents of the University of California. All rights reserved.\n"); 38 #endif /* not lint */ 39 40 #ifndef lint 41 #if 0 42 static char sccsid[] = "@(#)rwhod.c 8.1 (Berkeley) 6/6/93"; 43 #else 44 __RCSID("$NetBSD: rwhod.c,v 1.14 1998/07/08 15:17:57 mrg Exp $"); 45 #endif 46 #endif /* not lint */ 47 48 #include <sys/param.h> 49 #include <sys/socket.h> 50 #include <sys/stat.h> 51 #include <sys/signal.h> 52 #include <sys/ioctl.h> 53 #include <sys/sysctl.h> 54 55 #include <net/if.h> 56 #include <net/if_dl.h> 57 #include <net/route.h> 58 #include <netinet/in.h> 59 #include <protocols/rwhod.h> 60 #include <arpa/inet.h> 61 62 #include <ctype.h> 63 #include <err.h> 64 #include <errno.h> 65 #include <fcntl.h> 66 #include <netdb.h> 67 #include <paths.h> 68 #include <stdio.h> 69 #include <stdlib.h> 70 #include <string.h> 71 #include <syslog.h> 72 #include <unistd.h> 73 #include <utmp.h> 74 75 /* 76 * Alarm interval. Don't forget to change the down time check in ruptime 77 * if this is changed. 78 */ 79 #define AL_INTERVAL (3 * 60) 80 81 char myname[MAXHOSTNAMELEN + 1]; 82 83 /* 84 * We communicate with each neighbor in a list constructed at the time we're 85 * started up. Neighbors are currently directly connected via a hardware 86 * interface. 87 */ 88 struct neighbor { 89 struct neighbor *n_next; 90 char *n_name; /* interface name */ 91 struct sockaddr *n_addr; /* who to send to */ 92 int n_addrlen; /* size of address */ 93 int n_flags; /* should forward?, interface flags */ 94 }; 95 96 struct neighbor *neighbors; 97 struct whod mywd; 98 struct servent *sp; 99 int s, utmpf; 100 101 #define WHDRSIZE (sizeof(mywd) - sizeof(mywd.wd_we)) 102 103 int configure __P((int)); 104 void getboottime __P((int)); 105 int main __P((int, char **)); 106 void onalrm __P((int)); 107 void quit __P((char *)); 108 void rt_xaddrs __P((caddr_t, caddr_t, struct rt_addrinfo *)); 109 int verify __P((char *)); 110 #ifdef DEBUG 111 char *interval __P((int, char *)); 112 void Sendto __P((int, char *, int, int, char *, int)); 113 #define sendto Sendto 114 #endif 115 116 int 117 main(argc, argv) 118 int argc; 119 char *argv[]; 120 { 121 struct sockaddr_in from; 122 struct stat st; 123 char path[64]; 124 int on = 1; 125 char *cp; 126 struct sockaddr_in sin; 127 128 if (getuid()) 129 errx(1, "not super user"); 130 sp = getservbyname("who", "udp"); 131 if (sp == NULL) 132 errx(1, "udp/who: unknown service"); 133 #ifndef DEBUG 134 daemon(1, 0); 135 #endif 136 if (chdir(_PATH_RWHODIR) < 0) 137 err(1, "%s", _PATH_RWHODIR); 138 (void)signal(SIGHUP, getboottime); 139 openlog("rwhod", LOG_PID, LOG_DAEMON); 140 /* 141 * Establish host name as returned by system. 142 */ 143 if (gethostname(myname, sizeof(myname) - 1) < 0) { 144 syslog(LOG_ERR, "gethostname: %m"); 145 exit(1); 146 } 147 myname[sizeof(myname) - 1] = '\0'; 148 if ((cp = strchr(myname, '.')) != NULL) 149 *cp = '\0'; 150 strncpy(mywd.wd_hostname, myname, sizeof(mywd.wd_hostname) - 1); 151 utmpf = open(_PATH_UTMP, O_RDONLY|O_CREAT, 0644); 152 if (utmpf < 0) { 153 syslog(LOG_ERR, "%s: %m", _PATH_UTMP); 154 exit(1); 155 } 156 getboottime(0); 157 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { 158 syslog(LOG_ERR, "socket: %m"); 159 exit(1); 160 } 161 if (setsockopt(s, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on)) < 0) { 162 syslog(LOG_ERR, "setsockopt SO_BROADCAST: %m"); 163 exit(1); 164 } 165 memset(&sin, 0, sizeof(sin)); 166 sin.sin_family = AF_INET; 167 sin.sin_port = sp->s_port; 168 if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) { 169 syslog(LOG_ERR, "bind: %m"); 170 exit(1); 171 } 172 if (!configure(s)) 173 exit(1); 174 signal(SIGALRM, onalrm); 175 onalrm(0); 176 for (;;) { 177 struct whod wd; 178 int cc, whod, len = sizeof(from); 179 180 cc = recvfrom(s, (char *)&wd, sizeof(struct whod), 0, 181 (struct sockaddr *)&from, &len); 182 if (cc <= 0) { 183 if (cc < 0 && errno != EINTR) 184 syslog(LOG_WARNING, "recv: %m"); 185 continue; 186 } 187 if (from.sin_port != sp->s_port) { 188 syslog(LOG_WARNING, "%d: bad from port", 189 ntohs(from.sin_port)); 190 continue; 191 } 192 if (wd.wd_vers != WHODVERSION) 193 continue; 194 if (wd.wd_type != WHODTYPE_STATUS) 195 continue; 196 /* 197 * Ensure null termination of the name within the packet. 198 * Otherwise we might overflow or read past the end. 199 */ 200 wd.wd_hostname[sizeof(wd.wd_hostname)-1] = 0; 201 if (!verify(wd.wd_hostname)) { 202 syslog(LOG_WARNING, "malformed host name from %s", 203 inet_ntoa(from.sin_addr)); 204 continue; 205 } 206 snprintf(path, sizeof(path), "whod.%s", wd.wd_hostname); 207 /* 208 * Rather than truncating and growing the file each time, 209 * use ftruncate if size is less than previous size. 210 */ 211 whod = open(path, O_WRONLY | O_CREAT, 0644); 212 if (whod < 0) { 213 syslog(LOG_WARNING, "%s: %m", path); 214 continue; 215 } 216 #if ENDIAN != BIG_ENDIAN 217 { 218 int i, n = (cc - WHDRSIZE)/sizeof(struct whoent); 219 struct whoent *we; 220 221 /* undo header byte swapping before writing to file */ 222 wd.wd_sendtime = ntohl(wd.wd_sendtime); 223 for (i = 0; i < 3; i++) 224 wd.wd_loadav[i] = ntohl(wd.wd_loadav[i]); 225 wd.wd_boottime = ntohl(wd.wd_boottime); 226 we = wd.wd_we; 227 for (i = 0; i < n; i++) { 228 we->we_idle = ntohl(we->we_idle); 229 we->we_utmp.out_time = 230 ntohl(we->we_utmp.out_time); 231 we++; 232 } 233 } 234 #endif 235 (void)time((time_t *)&wd.wd_recvtime); 236 (void)write(whod, (char *)&wd, cc); 237 if (fstat(whod, &st) < 0 || st.st_size > cc) 238 ftruncate(whod, cc); 239 (void)close(whod); 240 } 241 } 242 243 /* 244 * Check out host name for unprintables 245 * and other funnies before allowing a file 246 * to be created. Sorry, but blanks aren't allowed. 247 */ 248 int 249 verify(name) 250 char *name; 251 { 252 int size = 0; 253 254 while (*name) { 255 if (!isascii(*name) || !(isalnum(*name) || ispunct(*name))) 256 return (0); 257 name++, size++; 258 } 259 return (size > 0); 260 } 261 262 int utmptime; 263 int utmpent; 264 int utmpsize = 0; 265 struct utmp *utmp; 266 int alarmcount; 267 268 void 269 onalrm(signo) 270 int signo; 271 { 272 struct neighbor *np; 273 struct whoent *we = mywd.wd_we, *wlast; 274 int i; 275 struct stat stb; 276 double avenrun[3]; 277 time_t now; 278 int cc; 279 280 now = time(NULL); 281 if (alarmcount % 10 == 0) 282 getboottime(0); 283 alarmcount++; 284 (void)fstat(utmpf, &stb); 285 if ((stb.st_mtime != utmptime) || (stb.st_size > utmpsize)) { 286 utmptime = stb.st_mtime; 287 if (stb.st_size > utmpsize) { 288 utmpsize = stb.st_size + 10 * sizeof(struct utmp); 289 if (utmp) 290 utmp = (struct utmp *)realloc(utmp, utmpsize); 291 else 292 utmp = (struct utmp *)malloc(utmpsize); 293 if (! utmp) { 294 warn("malloc failed"); 295 utmpsize = 0; 296 goto done; 297 } 298 } 299 (void)lseek(utmpf, (off_t)0, SEEK_SET); 300 cc = read(utmpf, (char *)utmp, stb.st_size); 301 if (cc < 0) { 302 warn("%s", _PATH_UTMP); 303 goto done; 304 } 305 wlast = &mywd.wd_we[1024 / sizeof(struct whoent) - 1]; 306 utmpent = cc / sizeof(struct utmp); 307 for (i = 0; i < utmpent; i++) 308 if (utmp[i].ut_name[0]) { 309 memcpy(we->we_utmp.out_line, utmp[i].ut_line, 310 sizeof(utmp[i].ut_line)); 311 memcpy(we->we_utmp.out_name, utmp[i].ut_name, 312 sizeof(utmp[i].ut_name)); 313 we->we_utmp.out_time = htonl(utmp[i].ut_time); 314 if (we >= wlast) 315 break; 316 we++; 317 } 318 utmpent = we - mywd.wd_we; 319 } 320 321 /* 322 * The test on utmpent looks silly---after all, if no one is 323 * logged on, why worry about efficiency?---but is useful on 324 * (e.g.) compute servers. 325 */ 326 if (utmpent && chdir(_PATH_DEV)) { 327 syslog(LOG_ERR, "chdir(%s): %m", _PATH_DEV); 328 exit(1); 329 } 330 we = mywd.wd_we; 331 for (i = 0; i < utmpent; i++) { 332 if (stat(we->we_utmp.out_line, &stb) >= 0) 333 we->we_idle = htonl(now - stb.st_atime); 334 we++; 335 } 336 (void)getloadavg(avenrun, sizeof(avenrun)/sizeof(avenrun[0])); 337 for (i = 0; i < 3; i++) 338 mywd.wd_loadav[i] = htonl((u_long)(avenrun[i] * 100)); 339 cc = (char *)we - (char *)&mywd; 340 mywd.wd_sendtime = htonl(time(0)); 341 mywd.wd_vers = WHODVERSION; 342 mywd.wd_type = WHODTYPE_STATUS; 343 for (np = neighbors; np != NULL; np = np->n_next) 344 (void)sendto(s, (char *)&mywd, cc, 0, 345 np->n_addr, np->n_addrlen); 346 if (utmpent && chdir(_PATH_RWHODIR)) { 347 syslog(LOG_ERR, "chdir(%s): %m", _PATH_RWHODIR); 348 exit(1); 349 } 350 done: 351 (void)alarm(AL_INTERVAL); 352 } 353 354 void 355 getboottime(signo) 356 int signo; 357 { 358 int mib[2]; 359 size_t size; 360 struct timeval tm; 361 362 mib[0] = CTL_KERN; 363 mib[1] = KERN_BOOTTIME; 364 size = sizeof(tm); 365 if (sysctl(mib, 2, &tm, &size, NULL, 0) == -1) { 366 syslog(LOG_ERR, "cannot get boottime: %m"); 367 exit(1); 368 } 369 mywd.wd_boottime = htonl(tm.tv_sec); 370 } 371 372 void 373 quit(msg) 374 char *msg; 375 { 376 syslog(LOG_ERR, msg); 377 exit(1); 378 } 379 380 #define ROUNDUP(a) \ 381 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long)) 382 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len)) 383 384 void 385 rt_xaddrs(cp, cplim, rtinfo) 386 caddr_t cp, cplim; 387 struct rt_addrinfo *rtinfo; 388 { 389 struct sockaddr *sa; 390 int i; 391 392 memset(rtinfo->rti_info, 0, sizeof(rtinfo->rti_info)); 393 for (i = 0; (i < RTAX_MAX) && (cp < cplim); i++) { 394 if ((rtinfo->rti_addrs & (1 << i)) == 0) 395 continue; 396 rtinfo->rti_info[i] = sa = (struct sockaddr *)cp; 397 ADVANCE(cp, sa); 398 } 399 } 400 401 /* 402 * Figure out device configuration and select 403 * networks which deserve status information. 404 */ 405 int 406 configure(s) 407 int s; 408 { 409 struct neighbor *np; 410 struct if_msghdr *ifm; 411 struct ifa_msghdr *ifam; 412 struct sockaddr_dl *sdl; 413 size_t needed; 414 int mib[6], flags = 0, len; 415 char *buf, *lim, *next; 416 struct rt_addrinfo info; 417 418 mib[0] = CTL_NET; 419 mib[1] = PF_ROUTE; 420 mib[2] = 0; 421 mib[3] = AF_INET; 422 mib[4] = NET_RT_IFLIST; 423 mib[5] = 0; 424 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) 425 quit("route-sysctl-estimate"); 426 if ((buf = malloc(needed)) == NULL) 427 quit("malloc"); 428 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) 429 quit("actual retrieval of interface table"); 430 lim = buf + needed; 431 432 sdl = NULL; /* XXX just to keep gcc -Wall happy */ 433 for (next = buf; next < lim; next += ifm->ifm_msglen) { 434 ifm = (struct if_msghdr *)next; 435 if (ifm->ifm_type == RTM_IFINFO) { 436 sdl = (struct sockaddr_dl *)(ifm + 1); 437 flags = ifm->ifm_flags; 438 continue; 439 } 440 if ((flags & IFF_UP) == 0 || 441 (flags & (IFF_BROADCAST|IFF_POINTOPOINT)) == 0) 442 continue; 443 if (ifm->ifm_type != RTM_NEWADDR) 444 quit("out of sync parsing NET_RT_IFLIST"); 445 ifam = (struct ifa_msghdr *)ifm; 446 info.rti_addrs = ifam->ifam_addrs; 447 rt_xaddrs((char *)(ifam + 1), ifam->ifam_msglen + (char *)ifam, 448 &info); 449 /* gag, wish we could get rid of Internet dependencies */ 450 #define dstaddr info.rti_info[RTAX_BRD] 451 #define IPADDR_SA(x) ((struct sockaddr_in *)(x))->sin_addr.s_addr 452 #define PORT_SA(x) ((struct sockaddr_in *)(x))->sin_port 453 if (dstaddr == 0 || dstaddr->sa_family != AF_INET) 454 continue; 455 PORT_SA(dstaddr) = sp->s_port; 456 for (np = neighbors; np != NULL; np = np->n_next) 457 if (memcmp(sdl->sdl_data, np->n_name, 458 sdl->sdl_nlen) == 0 && 459 IPADDR_SA(np->n_addr) == IPADDR_SA(dstaddr)) 460 break; 461 if (np != NULL) 462 continue; 463 len = sizeof(*np) + dstaddr->sa_len + sdl->sdl_nlen + 1; 464 np = (struct neighbor *)malloc(len); 465 if (np == NULL) 466 quit("malloc of neighbor structure"); 467 memset(np, 0, len); 468 np->n_flags = flags; 469 np->n_addr = (struct sockaddr *)(np + 1); 470 np->n_addrlen = dstaddr->sa_len; 471 np->n_name = np->n_addrlen + (char *)np->n_addr; 472 np->n_next = neighbors; 473 neighbors = np; 474 memcpy((char *)np->n_addr, (char *)dstaddr, np->n_addrlen); 475 memcpy(np->n_name, sdl->sdl_data, sdl->sdl_nlen); 476 } 477 free(buf); 478 return (1); 479 } 480 481 #ifdef DEBUG 482 void 483 Sendto(s, buf, cc, flags, to, tolen) 484 int s; 485 char *buf; 486 int cc, flags; 487 char *to; 488 int tolen; 489 { 490 struct whod *w = (struct whod *)buf; 491 struct whoent *we; 492 struct sockaddr_in *sin = (struct sockaddr_in *)to; 493 494 printf("sendto %x.%d\n", ntohl(sin->sin_addr), ntohs(sin->sin_port)); 495 printf("hostname %s %s\n", w->wd_hostname, 496 interval(ntohl(w->wd_sendtime) - ntohl(w->wd_boottime), " up")); 497 printf("load %4.2f, %4.2f, %4.2f\n", 498 ntohl(w->wd_loadav[0]) / 100.0, ntohl(w->wd_loadav[1]) / 100.0, 499 ntohl(w->wd_loadav[2]) / 100.0); 500 cc -= WHDRSIZE; 501 for (we = w->wd_we, cc /= sizeof(struct whoent); cc > 0; cc--, we++) { 502 time_t t = ntohl(we->we_utmp.out_time); 503 printf("%-8.8s %s:%s %.12s", 504 we->we_utmp.out_name, 505 w->wd_hostname, we->we_utmp.out_line, 506 ctime(&t)+4); 507 we->we_idle = ntohl(we->we_idle) / 60; 508 if (we->we_idle) { 509 if (we->we_idle >= 100*60) 510 we->we_idle = 100*60 - 1; 511 if (we->we_idle >= 60) 512 printf(" %2d", we->we_idle / 60); 513 else 514 printf(" "); 515 printf(":%02d", we->we_idle % 60); 516 } 517 printf("\n"); 518 } 519 } 520 521 char * 522 interval(time, updown) 523 int time; 524 char *updown; 525 { 526 static char resbuf[32]; 527 int days, hours, minutes; 528 529 if (time < 0 || time > 3*30*24*60*60) { 530 (void)sprintf(resbuf, " %s ??:??", updown); 531 return (resbuf); 532 } 533 minutes = (time + 59) / 60; /* round to minutes */ 534 hours = minutes / 60; minutes %= 60; 535 days = hours / 24; hours %= 24; 536 if (days) 537 (void)sprintf(resbuf, "%s %2d+%02d:%02d", 538 updown, days, hours, minutes); 539 else 540 (void)sprintf(resbuf, "%s %2d:%02d", 541 updown, hours, minutes); 542 return (resbuf); 543 } 544 #endif 545