1 /* 2 * Copyright (c) 1983 The Regents of the University of California. 3 * All rights reserved. 4 * 5 * %sccs.include.redist.c% 6 */ 7 8 #ifndef lint 9 char copyright[] = 10 "@(#) Copyright (c) 1983 The Regents of the University of California.\n\ 11 All rights reserved.\n"; 12 #endif /* not lint */ 13 14 #ifndef lint 15 static char sccsid[] = "@(#)rwhod.c 5.21 (Berkeley) 02/19/92"; 16 #endif /* not lint */ 17 18 #include <sys/param.h> 19 #include <sys/socket.h> 20 #include <sys/stat.h> 21 #include <sys/signal.h> 22 #include <sys/ioctl.h> 23 #include <sys/kinfo.h> 24 #include <sys/file.h> 25 26 #include <net/if.h> 27 #include <net/if_dl.h> 28 #include <net/route.h> 29 #include <netinet/in.h> 30 31 #include <nlist.h> 32 #include <errno.h> 33 #include <utmp.h> 34 #include <ctype.h> 35 #include <netdb.h> 36 #include <syslog.h> 37 #include <protocols/rwhod.h> 38 #include <stdio.h> 39 #include <paths.h> 40 41 /* 42 * Alarm interval. Don't forget to change the down time check in ruptime 43 * if this is changed. 44 */ 45 #define AL_INTERVAL (3 * 60) 46 47 struct sockaddr_in sin; 48 49 char myname[MAXHOSTNAMELEN]; 50 51 struct nlist nl[] = { 52 #define NL_BOOTTIME 0 53 { "_boottime" }, 54 0 55 }; 56 57 /* 58 * We communicate with each neighbor in 59 * a list constructed at the time we're 60 * started up. Neighbors are currently 61 * directly connected via a hardware interface. 62 */ 63 struct neighbor { 64 struct neighbor *n_next; 65 char *n_name; /* interface name */ 66 struct sockaddr *n_addr; /* who to send to */ 67 int n_addrlen; /* size of address */ 68 int n_flags; /* should forward?, interface flags */ 69 }; 70 71 struct neighbor *neighbors; 72 struct whod mywd; 73 struct servent *sp; 74 int s, utmpf, kmemf = -1; 75 76 #define WHDRSIZE (sizeof (mywd) - sizeof (mywd.wd_we)) 77 78 extern int errno; 79 char *strcpy(), *malloc(); 80 long lseek(); 81 void getkmem(), onalrm(); 82 struct in_addr inet_makeaddr(); 83 84 main() 85 { 86 struct sockaddr_in from; 87 struct stat st; 88 char path[64]; 89 int on = 1; 90 char *cp, *index(), *strerror(); 91 92 if (getuid()) { 93 fprintf(stderr, "rwhod: not super user\n"); 94 exit(1); 95 } 96 sp = getservbyname("who", "udp"); 97 if (sp == 0) { 98 fprintf(stderr, "rwhod: udp/who: unknown service\n"); 99 exit(1); 100 } 101 #ifndef DEBUG 102 daemon(1, 0); 103 #endif 104 if (chdir(_PATH_RWHODIR) < 0) { 105 (void)fprintf(stderr, "rwhod: %s: %s\n", 106 _PATH_RWHODIR, strerror(errno)); 107 exit(1); 108 } 109 (void) signal(SIGHUP, getkmem); 110 openlog("rwhod", LOG_PID, LOG_DAEMON); 111 /* 112 * Establish host name as returned by system. 113 */ 114 if (gethostname(myname, sizeof (myname) - 1) < 0) { 115 syslog(LOG_ERR, "gethostname: %m"); 116 exit(1); 117 } 118 if ((cp = index(myname, '.')) != NULL) 119 *cp = '\0'; 120 strncpy(mywd.wd_hostname, myname, sizeof (myname) - 1); 121 utmpf = open(_PATH_UTMP, O_RDONLY|O_CREAT, 0644); 122 if (utmpf < 0) { 123 syslog(LOG_ERR, "%s: %m", _PATH_UTMP); 124 exit(1); 125 } 126 getkmem(); 127 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { 128 syslog(LOG_ERR, "socket: %m"); 129 exit(1); 130 } 131 if (setsockopt(s, SOL_SOCKET, SO_BROADCAST, &on, sizeof (on)) < 0) { 132 syslog(LOG_ERR, "setsockopt SO_BROADCAST: %m"); 133 exit(1); 134 } 135 sin.sin_family = AF_INET; 136 sin.sin_port = sp->s_port; 137 if (bind(s, (struct sockaddr *)&sin, sizeof (sin)) < 0) { 138 syslog(LOG_ERR, "bind: %m"); 139 exit(1); 140 } 141 if (!configure(s)) 142 exit(1); 143 signal(SIGALRM, onalrm); 144 onalrm(); 145 for (;;) { 146 struct whod wd; 147 int cc, whod, len = sizeof (from); 148 149 cc = recvfrom(s, (char *)&wd, sizeof (struct whod), 0, 150 (struct sockaddr *)&from, &len); 151 if (cc <= 0) { 152 if (cc < 0 && errno != EINTR) 153 syslog(LOG_WARNING, "recv: %m"); 154 continue; 155 } 156 if (from.sin_port != sp->s_port) { 157 syslog(LOG_WARNING, "%d: bad from port", 158 ntohs(from.sin_port)); 159 continue; 160 } 161 if (wd.wd_vers != WHODVERSION) 162 continue; 163 if (wd.wd_type != WHODTYPE_STATUS) 164 continue; 165 if (!verify(wd.wd_hostname)) { 166 syslog(LOG_WARNING, "malformed host name from %x", 167 from.sin_addr); 168 continue; 169 } 170 (void) sprintf(path, "whod.%s", wd.wd_hostname); 171 /* 172 * Rather than truncating and growing the file each time, 173 * use ftruncate if size is less than previous size. 174 */ 175 whod = open(path, O_WRONLY | O_CREAT, 0644); 176 if (whod < 0) { 177 syslog(LOG_WARNING, "%s: %m", path); 178 continue; 179 } 180 #if ENDIAN != BIG_ENDIAN 181 { 182 int i, n = (cc - WHDRSIZE)/sizeof(struct whoent); 183 struct whoent *we; 184 185 /* undo header byte swapping before writing to file */ 186 wd.wd_sendtime = ntohl(wd.wd_sendtime); 187 for (i = 0; i < 3; i++) 188 wd.wd_loadav[i] = ntohl(wd.wd_loadav[i]); 189 wd.wd_boottime = ntohl(wd.wd_boottime); 190 we = wd.wd_we; 191 for (i = 0; i < n; i++) { 192 we->we_idle = ntohl(we->we_idle); 193 we->we_utmp.out_time = 194 ntohl(we->we_utmp.out_time); 195 we++; 196 } 197 } 198 #endif 199 (void) time((time_t *)&wd.wd_recvtime); 200 (void) write(whod, (char *)&wd, cc); 201 if (fstat(whod, &st) < 0 || st.st_size > cc) 202 ftruncate(whod, cc); 203 (void) close(whod); 204 } 205 } 206 207 /* 208 * Check out host name for unprintables 209 * and other funnies before allowing a file 210 * to be created. Sorry, but blanks aren't allowed. 211 */ 212 verify(name) 213 register char *name; 214 { 215 register int size = 0; 216 217 while (*name) { 218 if (!isascii(*name) || !(isalnum(*name) || ispunct(*name))) 219 return (0); 220 name++, size++; 221 } 222 return (size > 0); 223 } 224 225 int utmptime; 226 int utmpent; 227 int utmpsize = 0; 228 struct utmp *utmp; 229 int alarmcount; 230 231 void 232 onalrm() 233 { 234 register struct neighbor *np; 235 register struct whoent *we = mywd.wd_we, *wlast; 236 register int i; 237 struct stat stb; 238 int cc; 239 double avenrun[3]; 240 time_t now = time((time_t *)NULL); 241 char *strerror(); 242 243 if (alarmcount % 10 == 0) 244 getkmem(); 245 alarmcount++; 246 (void) fstat(utmpf, &stb); 247 if ((stb.st_mtime != utmptime) || (stb.st_size > utmpsize)) { 248 utmptime = stb.st_mtime; 249 if (stb.st_size > utmpsize) { 250 utmpsize = stb.st_size + 10 * sizeof(struct utmp); 251 if (utmp) 252 utmp = (struct utmp *)realloc(utmp, utmpsize); 253 else 254 utmp = (struct utmp *)malloc(utmpsize); 255 if (! utmp) { 256 fprintf(stderr, "rwhod: malloc failed\n"); 257 utmpsize = 0; 258 goto done; 259 } 260 } 261 (void) lseek(utmpf, (long)0, L_SET); 262 cc = read(utmpf, (char *)utmp, stb.st_size); 263 if (cc < 0) { 264 fprintf(stderr, "rwhod: %s: %s\n", 265 _PATH_UTMP, strerror(errno)); 266 goto done; 267 } 268 wlast = &mywd.wd_we[1024 / sizeof (struct whoent) - 1]; 269 utmpent = cc / sizeof (struct utmp); 270 for (i = 0; i < utmpent; i++) 271 if (utmp[i].ut_name[0]) { 272 bcopy(utmp[i].ut_line, we->we_utmp.out_line, 273 sizeof (utmp[i].ut_line)); 274 bcopy(utmp[i].ut_name, we->we_utmp.out_name, 275 sizeof (utmp[i].ut_name)); 276 we->we_utmp.out_time = htonl(utmp[i].ut_time); 277 if (we >= wlast) 278 break; 279 we++; 280 } 281 utmpent = we - mywd.wd_we; 282 } 283 284 /* 285 * The test on utmpent looks silly---after all, if no one is 286 * logged on, why worry about efficiency?---but is useful on 287 * (e.g.) compute servers. 288 */ 289 if (utmpent && chdir(_PATH_DEV)) { 290 syslog(LOG_ERR, "chdir(%s): %m", _PATH_DEV); 291 exit(1); 292 } 293 we = mywd.wd_we; 294 for (i = 0; i < utmpent; i++) { 295 if (stat(we->we_utmp.out_line, &stb) >= 0) 296 we->we_idle = htonl(now - stb.st_atime); 297 we++; 298 } 299 (void) getloadavg(avenrun, sizeof(avenrun)/sizeof(avenrun[0])); 300 for (i = 0; i < 3; i++) 301 mywd.wd_loadav[i] = htonl((u_long)(avenrun[i] * 100)); 302 cc = (char *)we - (char *)&mywd; 303 mywd.wd_sendtime = htonl(time(0)); 304 mywd.wd_vers = WHODVERSION; 305 mywd.wd_type = WHODTYPE_STATUS; 306 for (np = neighbors; np != NULL; np = np->n_next) 307 (void) sendto(s, (char *)&mywd, cc, 0, 308 np->n_addr, np->n_addrlen); 309 if (utmpent && chdir(_PATH_RWHODIR)) { 310 syslog(LOG_ERR, "chdir(%s): %m", _PATH_RWHODIR); 311 exit(1); 312 } 313 done: 314 (void) alarm(AL_INTERVAL); 315 } 316 317 void 318 getkmem() 319 { 320 static ino_t vmunixino; 321 static time_t vmunixctime; 322 struct stat sb; 323 324 if (stat(_PATH_UNIX, &sb) < 0) { 325 if (vmunixctime) 326 return; 327 } else { 328 if (sb.st_ctime == vmunixctime && sb.st_ino == vmunixino) 329 return; 330 vmunixctime = sb.st_ctime; 331 vmunixino= sb.st_ino; 332 } 333 if (kmemf >= 0) 334 (void) close(kmemf); 335 loop: 336 if (nlist(_PATH_UNIX, nl)) { 337 syslog(LOG_WARNING, "%s: namelist botch", _PATH_UNIX); 338 sleep(300); 339 goto loop; 340 } 341 kmemf = open(_PATH_KMEM, O_RDONLY, 0); 342 if (kmemf < 0) { 343 syslog(LOG_ERR, "%s: %m", _PATH_KMEM); 344 exit(1); 345 } 346 (void) lseek(kmemf, (long)nl[NL_BOOTTIME].n_value, L_SET); 347 (void) read(kmemf, (char *)&mywd.wd_boottime, 348 sizeof (mywd.wd_boottime)); 349 mywd.wd_boottime = htonl(mywd.wd_boottime); 350 } 351 352 void 353 quit(msg) 354 char *msg; 355 { 356 syslog(LOG_ERR, msg); 357 exit(1); 358 } 359 360 #define ROUNDUP(a) \ 361 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long)) 362 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len)) 363 364 void 365 rt_xaddrs(cp, cplim, rtinfo) 366 register caddr_t cp, cplim; 367 register struct rt_addrinfo *rtinfo; 368 { 369 register struct sockaddr *sa; 370 register int i; 371 372 bzero(rtinfo->rti_info, sizeof(rtinfo->rti_info)); 373 for (i = 0; (i < RTAX_MAX) && (cp < cplim); i++) { 374 if ((rtinfo->rti_addrs & (1 << i)) == 0) 375 continue; 376 rtinfo->rti_info[i] = sa = (struct sockaddr *)cp; 377 ADVANCE(cp, sa); 378 } 379 } 380 381 /* 382 * Figure out device configuration and select 383 * networks which deserve status information. 384 */ 385 configure(s) 386 int s; 387 { 388 register struct neighbor *np; 389 register struct if_msghdr *ifm; 390 register struct ifa_msghdr *ifam; 391 struct sockaddr_in *sin; 392 struct sockaddr_dl *sdl; 393 int needed, rlen = 0, flags = 0, len; 394 char *buf, *lim, *next; 395 struct rt_addrinfo info; 396 397 if ((needed = getkerninfo(KINFO_RT_IFLIST, 0, 0, 0)) < 0) 398 quit("route-getkerninfo-estimate"); 399 if ((buf = malloc(needed)) == NULL) 400 quit("malloc"); 401 if ((rlen = getkerninfo(KINFO_RT_IFLIST, buf, &needed, 0)) < 0) 402 quit("actual retrieval of interface table"); 403 lim = buf + rlen; 404 405 for (next = buf; next < lim; next += ifm->ifm_msglen) { 406 ifm = (struct if_msghdr *)next; 407 if (ifm->ifm_type == RTM_IFINFO) { 408 sdl = (struct sockaddr_dl *)(ifm + 1); 409 flags = ifm->ifm_flags; 410 continue; 411 } 412 if ((flags & IFF_UP) == 0 || 413 (flags & (IFF_BROADCAST|IFF_POINTOPOINT)) == 0) 414 continue; 415 if (ifm->ifm_type != RTM_NEWADDR) 416 quit("out of sync parsing KINFO_RT_IFLIST"); 417 ifam = (struct ifa_msghdr *)ifm; 418 info.rti_addrs = ifam->ifam_addrs; 419 rt_xaddrs((char *)(ifam + 1), ifam->ifam_msglen + (char *)ifam, 420 &info); 421 /* gag, wish we could get rid of Internet dependencies */ 422 #define dstaddr info.rti_info[RTAX_BRD] 423 #define IPADDR_SA(x) ((struct sockaddr_in *)(x))->sin_addr.s_addr 424 #define PORT_SA(x) ((struct sockaddr_in *)(x))->sin_port 425 if (dstaddr == 0 || dstaddr->sa_family != AF_INET) 426 continue; 427 PORT_SA(dstaddr) = sp->s_port; 428 for (np = neighbors; np != NULL; np = np->n_next) 429 if (bcmp(sdl->sdl_data, np->n_name, sdl->sdl_nlen) == 0 430 && IPADDR_SA(np->n_addr) == IPADDR_SA(dstaddr)) 431 break; 432 if (np != NULL) 433 continue; 434 len = sizeof(*np) + dstaddr->sa_len + sdl->sdl_nlen + 1; 435 np = (struct neighbor *)malloc(len); 436 if (np == NULL) 437 quit("malloc of neighbor structure"); 438 bzero((char *)np, len); 439 np->n_flags = flags; 440 np->n_addr = (struct sockaddr *)(np + 1); 441 np->n_addrlen = dstaddr->sa_len; 442 np->n_name = np->n_addrlen + (char *)np->n_addr; 443 np->n_next = neighbors; 444 neighbors = np; 445 bcopy((char *)dstaddr, (char *)np->n_addr, np->n_addrlen); 446 bcopy(sdl->sdl_data, np->n_name, sdl->sdl_nlen); 447 } 448 free(buf); 449 return (1); 450 } 451 452 #ifdef DEBUG 453 sendto(s, buf, cc, flags, to, tolen) 454 int s; 455 char *buf; 456 int cc, flags; 457 char *to; 458 int tolen; 459 { 460 register struct whod *w = (struct whod *)buf; 461 register struct whoent *we; 462 struct sockaddr_in *sin = (struct sockaddr_in *)to; 463 char *interval(); 464 465 printf("sendto %x.%d\n", ntohl(sin->sin_addr), ntohs(sin->sin_port)); 466 printf("hostname %s %s\n", w->wd_hostname, 467 interval(ntohl(w->wd_sendtime) - ntohl(w->wd_boottime), " up")); 468 printf("load %4.2f, %4.2f, %4.2f\n", 469 ntohl(w->wd_loadav[0]) / 100.0, ntohl(w->wd_loadav[1]) / 100.0, 470 ntohl(w->wd_loadav[2]) / 100.0); 471 cc -= WHDRSIZE; 472 for (we = w->wd_we, cc /= sizeof (struct whoent); cc > 0; cc--, we++) { 473 time_t t = ntohl(we->we_utmp.out_time); 474 printf("%-8.8s %s:%s %.12s", 475 we->we_utmp.out_name, 476 w->wd_hostname, we->we_utmp.out_line, 477 ctime(&t)+4); 478 we->we_idle = ntohl(we->we_idle) / 60; 479 if (we->we_idle) { 480 if (we->we_idle >= 100*60) 481 we->we_idle = 100*60 - 1; 482 if (we->we_idle >= 60) 483 printf(" %2d", we->we_idle / 60); 484 else 485 printf(" "); 486 printf(":%02d", we->we_idle % 60); 487 } 488 printf("\n"); 489 } 490 } 491 492 char * 493 interval(time, updown) 494 int time; 495 char *updown; 496 { 497 static char resbuf[32]; 498 int days, hours, minutes; 499 500 if (time < 0 || time > 3*30*24*60*60) { 501 (void) sprintf(resbuf, " %s ??:??", updown); 502 return (resbuf); 503 } 504 minutes = (time + 59) / 60; /* round to minutes */ 505 hours = minutes / 60; minutes %= 60; 506 days = hours / 24; hours %= 24; 507 if (days) 508 (void) sprintf(resbuf, "%s %2d+%02d:%02d", 509 updown, days, hours, minutes); 510 else 511 (void) sprintf(resbuf, "%s %2d:%02d", 512 updown, hours, minutes); 513 return (resbuf); 514 } 515 #endif 516