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.18 2000/10/11 20:23:56 is 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 <util.h> 74 #include <utmp.h> 75 76 /* 77 * Alarm interval. Don't forget to change the down time check in ruptime 78 * if this is changed. 79 */ 80 #define AL_INTERVAL (3 * 60) 81 82 char myname[MAXHOSTNAMELEN + 1]; 83 84 /* 85 * We communicate with each neighbor in a list constructed at the time we're 86 * started up. Neighbors are currently directly connected via a hardware 87 * interface. 88 */ 89 struct neighbor { 90 struct neighbor *n_next; 91 char *n_name; /* interface name */ 92 struct sockaddr *n_addr; /* who to send to */ 93 int n_addrlen; /* size of address */ 94 int n_flags; /* should forward?, interface flags */ 95 }; 96 97 struct neighbor *neighbors; 98 struct whod mywd; 99 struct servent *sp; 100 int s, utmpf; 101 102 #define WHDRSIZE (sizeof(mywd) - sizeof(mywd.wd_we)) 103 104 int configure __P((int)); 105 void getboottime __P((int)); 106 int main __P((int, char **)); 107 void onalrm __P((int)); 108 void quit __P((char *)); 109 void rt_xaddrs __P((caddr_t, caddr_t, struct rt_addrinfo *)); 110 int verify __P((char *)); 111 #ifdef DEBUG 112 char *interval __P((int, char *)); 113 void Sendto __P((int, char *, int, int, char *, int)); 114 #define sendto Sendto 115 #endif 116 117 int 118 main(argc, argv) 119 int argc; 120 char *argv[]; 121 { 122 struct sockaddr_in from; 123 struct stat st; 124 char path[64]; 125 int on = 1; 126 char *cp; 127 struct sockaddr_in sin; 128 129 if (getuid()) 130 errx(1, "not super user"); 131 sp = getservbyname("who", "udp"); 132 if (sp == NULL) 133 errx(1, "udp/who: unknown service"); 134 #ifndef DEBUG 135 daemon(1, 0); 136 pidfile(NULL); 137 #endif 138 if (chdir(_PATH_RWHODIR) < 0) 139 err(1, "%s", _PATH_RWHODIR); 140 (void)signal(SIGHUP, getboottime); 141 openlog("rwhod", LOG_PID, LOG_DAEMON); 142 /* 143 * Establish host name as returned by system. 144 */ 145 if (gethostname(myname, sizeof(myname) - 1) < 0) { 146 syslog(LOG_ERR, "gethostname: %m"); 147 exit(1); 148 } 149 myname[sizeof(myname) - 1] = '\0'; 150 if ((cp = strchr(myname, '.')) != NULL) 151 *cp = '\0'; 152 strncpy(mywd.wd_hostname, myname, sizeof(mywd.wd_hostname) - 1); 153 utmpf = open(_PATH_UTMP, O_RDONLY|O_CREAT, 0644); 154 if (utmpf < 0) { 155 syslog(LOG_ERR, "%s: %m", _PATH_UTMP); 156 exit(1); 157 } 158 getboottime(0); 159 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { 160 syslog(LOG_ERR, "socket: %m"); 161 exit(1); 162 } 163 if (setsockopt(s, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on)) < 0) { 164 syslog(LOG_ERR, "setsockopt SO_BROADCAST: %m"); 165 exit(1); 166 } 167 memset(&sin, 0, sizeof(sin)); 168 sin.sin_family = AF_INET; 169 sin.sin_port = sp->s_port; 170 if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) { 171 syslog(LOG_ERR, "bind: %m"); 172 exit(1); 173 } 174 if (!configure(s)) 175 exit(1); 176 signal(SIGALRM, onalrm); 177 onalrm(0); 178 for (;;) { 179 struct whod wd; 180 int cc, whod, len = sizeof(from); 181 182 cc = recvfrom(s, (char *)&wd, sizeof(struct whod), 0, 183 (struct sockaddr *)&from, &len); 184 if (cc <= 0) { 185 if (cc < 0 && errno != EINTR) 186 syslog(LOG_WARNING, "recv: %m"); 187 continue; 188 } 189 if (from.sin_port != sp->s_port) { 190 syslog(LOG_WARNING, "%d: bad from port", 191 ntohs(from.sin_port)); 192 continue; 193 } 194 if (cc < WHDRSIZE) { 195 syslog(LOG_WARNING, "Short packet from %s", 196 inet_ntoa(from.sin_addr)); 197 continue; 198 } 199 200 if (wd.wd_vers != WHODVERSION) 201 continue; 202 if (wd.wd_type != WHODTYPE_STATUS) 203 continue; 204 /* 205 * Ensure null termination of the name within the packet. 206 * Otherwise we might overflow or read past the end. 207 */ 208 wd.wd_hostname[sizeof(wd.wd_hostname)-1] = 0; 209 if (!verify(wd.wd_hostname)) { 210 syslog(LOG_WARNING, "malformed host name from %s", 211 inet_ntoa(from.sin_addr)); 212 continue; 213 } 214 snprintf(path, sizeof(path), "whod.%s", wd.wd_hostname); 215 /* 216 * Rather than truncating and growing the file each time, 217 * use ftruncate if size is less than previous size. 218 */ 219 whod = open(path, O_WRONLY | O_CREAT, 0644); 220 if (whod < 0) { 221 syslog(LOG_WARNING, "%s: %m", path); 222 continue; 223 } 224 #if ENDIAN != BIG_ENDIAN 225 { 226 int i, n = (cc - WHDRSIZE)/sizeof(struct whoent); 227 struct whoent *we; 228 229 /* undo header byte swapping before writing to file */ 230 wd.wd_sendtime = ntohl(wd.wd_sendtime); 231 for (i = 0; i < 3; i++) 232 wd.wd_loadav[i] = ntohl(wd.wd_loadav[i]); 233 wd.wd_boottime = ntohl(wd.wd_boottime); 234 we = wd.wd_we; 235 for (i = 0; i < n; i++) { 236 we->we_idle = ntohl(we->we_idle); 237 we->we_utmp.out_time = 238 ntohl(we->we_utmp.out_time); 239 we++; 240 } 241 } 242 #endif 243 (void)time((time_t *)&wd.wd_recvtime); 244 (void)write(whod, (char *)&wd, cc); 245 if (fstat(whod, &st) < 0 || st.st_size > cc) 246 ftruncate(whod, cc); 247 (void)close(whod); 248 } 249 } 250 251 /* 252 * Check out host name for unprintables 253 * and other funnies before allowing a file 254 * to be created. Sorry, but blanks aren't allowed. 255 */ 256 int 257 verify(name) 258 char *name; 259 { 260 int size = 0; 261 262 while (*name) { 263 if (!isascii(*name) || !(isalnum(*name) || ispunct(*name))) 264 return (0); 265 name++, size++; 266 } 267 return (size > 0); 268 } 269 270 int utmptime; 271 int utmpent; 272 int utmpsize = 0; 273 struct utmp *utmp; 274 int alarmcount; 275 276 void 277 onalrm(signo) 278 int signo; 279 { 280 struct neighbor *np; 281 struct whoent *we = mywd.wd_we, *wlast; 282 int i; 283 struct stat stb; 284 double avenrun[3]; 285 time_t now; 286 struct utmp *nutmp; 287 int cc; 288 289 now = time(NULL); 290 if (alarmcount % 10 == 0) 291 getboottime(0); 292 alarmcount++; 293 (void)fstat(utmpf, &stb); 294 if ((stb.st_mtime != utmptime) || (stb.st_size > utmpsize)) { 295 utmptime = stb.st_mtime; 296 if (stb.st_size > utmpsize) { 297 utmpsize = stb.st_size + 10 * sizeof(struct utmp); 298 if (utmp) 299 nutmp = (struct utmp *)realloc(utmp, utmpsize); 300 else 301 nutmp = (struct utmp *)malloc(utmpsize); 302 if (! nutmp) { 303 warn("malloc failed"); 304 if (utmp) 305 free(utmp); 306 utmpsize = 0; 307 goto done; 308 } 309 utmp = nutmp; 310 } 311 (void)lseek(utmpf, (off_t)0, SEEK_SET); 312 cc = read(utmpf, (char *)utmp, stb.st_size); 313 if (cc < 0) { 314 warn("%s", _PATH_UTMP); 315 goto done; 316 } 317 wlast = &mywd.wd_we[1024 / sizeof(struct whoent) - 1]; 318 utmpent = cc / sizeof(struct utmp); 319 for (i = 0; i < utmpent; i++) 320 if (utmp[i].ut_name[0]) { 321 memcpy(we->we_utmp.out_line, utmp[i].ut_line, 322 sizeof(utmp[i].ut_line)); 323 memcpy(we->we_utmp.out_name, utmp[i].ut_name, 324 sizeof(utmp[i].ut_name)); 325 we->we_utmp.out_time = htonl(utmp[i].ut_time); 326 if (we >= wlast) 327 break; 328 we++; 329 } 330 utmpent = we - mywd.wd_we; 331 } 332 333 /* 334 * The test on utmpent looks silly---after all, if no one is 335 * logged on, why worry about efficiency?---but is useful on 336 * (e.g.) compute servers. 337 */ 338 if (utmpent && chdir(_PATH_DEV)) { 339 syslog(LOG_ERR, "chdir(%s): %m", _PATH_DEV); 340 exit(1); 341 } 342 we = mywd.wd_we; 343 for (i = 0; i < utmpent; i++) { 344 if (stat(we->we_utmp.out_line, &stb) >= 0) 345 we->we_idle = htonl(now - stb.st_atime); 346 we++; 347 } 348 (void)getloadavg(avenrun, sizeof(avenrun)/sizeof(avenrun[0])); 349 for (i = 0; i < 3; i++) 350 mywd.wd_loadav[i] = htonl((u_long)(avenrun[i] * 100)); 351 cc = (char *)we - (char *)&mywd; 352 mywd.wd_sendtime = htonl(time(0)); 353 mywd.wd_vers = WHODVERSION; 354 mywd.wd_type = WHODTYPE_STATUS; 355 for (np = neighbors; np != NULL; np = np->n_next) 356 (void)sendto(s, (char *)&mywd, cc, 0, 357 np->n_addr, np->n_addrlen); 358 if (utmpent && chdir(_PATH_RWHODIR)) { 359 syslog(LOG_ERR, "chdir(%s): %m", _PATH_RWHODIR); 360 exit(1); 361 } 362 done: 363 (void)alarm(AL_INTERVAL); 364 } 365 366 void 367 getboottime(signo) 368 int signo; 369 { 370 int mib[2]; 371 size_t size; 372 struct timeval tm; 373 374 mib[0] = CTL_KERN; 375 mib[1] = KERN_BOOTTIME; 376 size = sizeof(tm); 377 if (sysctl(mib, 2, &tm, &size, NULL, 0) == -1) { 378 syslog(LOG_ERR, "cannot get boottime: %m"); 379 exit(1); 380 } 381 mywd.wd_boottime = htonl(tm.tv_sec); 382 } 383 384 void 385 quit(msg) 386 char *msg; 387 { 388 syslog(LOG_ERR, "%s", msg); 389 exit(1); 390 } 391 392 #define ROUNDUP(a) \ 393 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long)) 394 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len)) 395 396 void 397 rt_xaddrs(cp, cplim, rtinfo) 398 caddr_t cp, cplim; 399 struct rt_addrinfo *rtinfo; 400 { 401 struct sockaddr *sa; 402 int i; 403 404 memset(rtinfo->rti_info, 0, sizeof(rtinfo->rti_info)); 405 for (i = 0; (i < RTAX_MAX) && (cp < cplim); i++) { 406 if ((rtinfo->rti_addrs & (1 << i)) == 0) 407 continue; 408 rtinfo->rti_info[i] = sa = (struct sockaddr *)cp; 409 ADVANCE(cp, sa); 410 } 411 } 412 413 /* 414 * Figure out device configuration and select 415 * networks which deserve status information. 416 */ 417 int 418 configure(s) 419 int s; 420 { 421 struct neighbor *np; 422 struct if_msghdr *ifm; 423 struct ifa_msghdr *ifam; 424 struct sockaddr_dl *sdl; 425 size_t needed; 426 int mib[6], flags = 0, len; 427 char *buf, *lim, *next; 428 struct rt_addrinfo info; 429 430 mib[0] = CTL_NET; 431 mib[1] = PF_ROUTE; 432 mib[2] = 0; 433 mib[3] = AF_INET; 434 mib[4] = NET_RT_IFLIST; 435 mib[5] = 0; 436 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) 437 quit("route-sysctl-estimate"); 438 if ((buf = malloc(needed)) == NULL) 439 quit("malloc"); 440 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) 441 quit("actual retrieval of interface table"); 442 lim = buf + needed; 443 444 sdl = NULL; /* XXX just to keep gcc -Wall happy */ 445 for (next = buf; next < lim; next += ifm->ifm_msglen) { 446 ifm = (struct if_msghdr *)next; 447 if (ifm->ifm_type == RTM_IFINFO) { 448 sdl = (struct sockaddr_dl *)(ifm + 1); 449 flags = ifm->ifm_flags; 450 continue; 451 } 452 if ((flags & IFF_UP) == 0 || 453 (flags & (IFF_BROADCAST|IFF_POINTOPOINT)) == 0) 454 continue; 455 if (ifm->ifm_type != RTM_NEWADDR) 456 quit("out of sync parsing NET_RT_IFLIST"); 457 ifam = (struct ifa_msghdr *)ifm; 458 info.rti_addrs = ifam->ifam_addrs; 459 rt_xaddrs((char *)(ifam + 1), ifam->ifam_msglen + (char *)ifam, 460 &info); 461 /* gag, wish we could get rid of Internet dependencies */ 462 #define dstaddr info.rti_info[RTAX_BRD] 463 #define IPADDR_SA(x) ((struct sockaddr_in *)(x))->sin_addr.s_addr 464 #define PORT_SA(x) ((struct sockaddr_in *)(x))->sin_port 465 if (dstaddr == 0 || dstaddr->sa_family != AF_INET) 466 continue; 467 PORT_SA(dstaddr) = sp->s_port; 468 for (np = neighbors; np != NULL; np = np->n_next) 469 if (memcmp(sdl->sdl_data, np->n_name, 470 sdl->sdl_nlen) == 0 && 471 IPADDR_SA(np->n_addr) == IPADDR_SA(dstaddr)) 472 break; 473 if (np != NULL) 474 continue; 475 len = sizeof(*np) + dstaddr->sa_len + sdl->sdl_nlen + 1; 476 np = (struct neighbor *)malloc(len); 477 if (np == NULL) 478 quit("malloc of neighbor structure"); 479 memset(np, 0, len); 480 np->n_flags = flags; 481 np->n_addr = (struct sockaddr *)(np + 1); 482 np->n_addrlen = dstaddr->sa_len; 483 np->n_name = np->n_addrlen + (char *)np->n_addr; 484 np->n_next = neighbors; 485 neighbors = np; 486 memcpy((char *)np->n_addr, (char *)dstaddr, np->n_addrlen); 487 memcpy(np->n_name, sdl->sdl_data, sdl->sdl_nlen); 488 } 489 free(buf); 490 return (1); 491 } 492 493 #ifdef DEBUG 494 void 495 Sendto(s, buf, cc, flags, to, tolen) 496 int s; 497 char *buf; 498 int cc, flags; 499 char *to; 500 int tolen; 501 { 502 struct whod *w = (struct whod *)buf; 503 struct whoent *we; 504 struct sockaddr_in *sin = (struct sockaddr_in *)to; 505 506 printf("sendto %x.%d\n", ntohl(sin->sin_addr), ntohs(sin->sin_port)); 507 printf("hostname %s %s\n", w->wd_hostname, 508 interval(ntohl(w->wd_sendtime) - ntohl(w->wd_boottime), " up")); 509 printf("load %4.2f, %4.2f, %4.2f\n", 510 ntohl(w->wd_loadav[0]) / 100.0, ntohl(w->wd_loadav[1]) / 100.0, 511 ntohl(w->wd_loadav[2]) / 100.0); 512 cc -= WHDRSIZE; 513 for (we = w->wd_we, cc /= sizeof(struct whoent); cc > 0; cc--, we++) { 514 time_t t = ntohl(we->we_utmp.out_time); 515 printf("%-8.8s %s:%s %.12s", 516 we->we_utmp.out_name, 517 w->wd_hostname, we->we_utmp.out_line, 518 ctime(&t)+4); 519 we->we_idle = ntohl(we->we_idle) / 60; 520 if (we->we_idle) { 521 if (we->we_idle >= 100*60) 522 we->we_idle = 100*60 - 1; 523 if (we->we_idle >= 60) 524 printf(" %2d", we->we_idle / 60); 525 else 526 printf(" "); 527 printf(":%02d", we->we_idle % 60); 528 } 529 printf("\n"); 530 } 531 } 532 533 char * 534 interval(time, updown) 535 int time; 536 char *updown; 537 { 538 static char resbuf[32]; 539 int days, hours, minutes; 540 541 if (time < 0 || time > 3*30*24*60*60) { 542 (void)sprintf(resbuf, " %s ??:??", updown); 543 return (resbuf); 544 } 545 minutes = (time + 59) / 60; /* round to minutes */ 546 hours = minutes / 60; minutes %= 60; 547 days = hours / 24; hours %= 24; 548 if (days) 549 (void)sprintf(resbuf, "%s %2d+%02d:%02d", 550 updown, days, hours, minutes); 551 else 552 (void)sprintf(resbuf, "%s %2d:%02d", 553 updown, hours, minutes); 554 return (resbuf); 555 } 556 #endif 557