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 #if 0 40 #ifndef lint 41 static char sccsid[] = "@(#)rwhod.c 8.1 (Berkeley) 6/6/93"; 42 #endif /* not lint */ 43 #endif 44 45 #include <sys/cdefs.h> 46 __FBSDID("$FreeBSD$"); 47 48 #include <sys/param.h> 49 #include <sys/capsicum.h> 50 #include <sys/ioctl.h> 51 #include <sys/procdesc.h> 52 #include <sys/socket.h> 53 #include <sys/stat.h> 54 #include <sys/signal.h> 55 #include <sys/sysctl.h> 56 #include <sys/wait.h> 57 58 #include <net/if.h> 59 #include <net/if_dl.h> 60 #include <net/route.h> 61 #include <netinet/in.h> 62 #include <arpa/inet.h> 63 #include <protocols/rwhod.h> 64 65 #include <ctype.h> 66 #include <err.h> 67 #include <errno.h> 68 #include <fcntl.h> 69 #include <grp.h> 70 #include <netdb.h> 71 #include <paths.h> 72 #include <pwd.h> 73 #include <stdio.h> 74 #include <stdlib.h> 75 #include <string.h> 76 #include <syslog.h> 77 #include <timeconv.h> 78 #include <utmpx.h> 79 #include <unistd.h> 80 81 #define UNPRIV_USER "daemon" 82 #define UNPRIV_GROUP "daemon" 83 84 #define NO_MULTICAST 0 /* multicast modes */ 85 #define PER_INTERFACE_MULTICAST 1 86 #define SCOPED_MULTICAST 2 87 88 #define MAX_MULTICAST_SCOPE 32 /* "site-wide", by convention */ 89 90 #define INADDR_WHOD_GROUP (u_long)0xe0000103 /* 224.0.1.3 */ 91 /* (belongs in protocols/rwhod.h) */ 92 93 int insecure_mode; 94 int quiet_mode; 95 int iff_flag = IFF_POINTOPOINT; 96 int multicast_mode = NO_MULTICAST; 97 int multicast_scope; 98 struct sockaddr_in multicast_addr = 99 { sizeof(multicast_addr), AF_INET, 0, { 0 }, { 0 } }; 100 101 /* 102 * Sleep interval. Don't forget to change the down time check in ruptime 103 * if this is changed. 104 */ 105 #define SL_INTERVAL (3 * 60) 106 107 char myname[MAXHOSTNAMELEN]; 108 109 /* 110 * We communicate with each neighbor in a list constructed at the time we're 111 * started up. Neighbors are currently directly connected via a hardware 112 * interface. 113 */ 114 struct neighbor { 115 struct neighbor *n_next; 116 char *n_name; /* interface name */ 117 struct sockaddr *n_addr; /* who to send to */ 118 int n_addrlen; /* size of address */ 119 int n_flags; /* should forward?, interface flags */ 120 }; 121 122 struct neighbor *neighbors; 123 struct whod mywd; 124 struct servent *sp; 125 int s; 126 int fdp; 127 pid_t pid_child_receiver; 128 129 #define WHDRSIZE (int)(sizeof(mywd) - sizeof(mywd.wd_we)) 130 131 int configure(int so); 132 void getboottime(int signo __unused); 133 void receiver_process(void); 134 void rt_xaddrs(caddr_t cp, caddr_t cplim, struct rt_addrinfo *rtinfo); 135 void run_as(uid_t *uid, gid_t *gid); 136 void quit(const char *msg); 137 void sender_process(void); 138 int verify(char *name, int maxlen); 139 static void usage(void); 140 141 #ifdef DEBUG 142 char *interval(int time, char *updown); 143 void Sendto(int s, const void *buf, size_t cc, int flags, 144 const struct sockaddr *to, int tolen); 145 #define sendto Sendto 146 #endif 147 148 /* 149 * This version of Berkeley's rwhod has been modified to use IP multicast 150 * datagrams, under control of a new command-line option: 151 * 152 * rwhod -m causes rwhod to use IP multicast (instead of 153 * broadcast or unicast) on all interfaces that have 154 * the IFF_MULTICAST flag set in their "ifnet" structs 155 * (excluding the loopback interface). The multicast 156 * reports are sent with a time-to-live of 1, to prevent 157 * forwarding beyond the directly-connected subnet(s). 158 * 159 * rwhod -m <ttl> causes rwhod to send IP multicast datagrams with a 160 * time-to-live of <ttl>, via a SINGLE interface rather 161 * than all interfaces. <ttl> must be between 0 and 162 * MAX_MULTICAST_SCOPE, defined below. Note that "-m 1" 163 * is different than "-m", in that "-m 1" specifies 164 * transmission on one interface only. 165 * 166 * When "-m" is used without a <ttl> argument, the program accepts multicast 167 * rwhod reports from all multicast-capable interfaces. If a <ttl> argument 168 * is given, it accepts multicast reports from only one interface, the one 169 * on which reports are sent (which may be controlled via the host's routing 170 * table). Regardless of the "-m" option, the program accepts broadcast or 171 * unicast reports from all interfaces. Thus, this program will hear the 172 * reports of old, non-multicasting rwhods, but, if multicasting is used, 173 * those old rwhods won't hear the reports generated by this program. 174 * 175 * -- Steve Deering, Stanford University, February 1989 176 */ 177 int 178 main(int argc, char *argv[]) 179 { 180 int on; 181 char *cp; 182 struct sockaddr_in soin; 183 uid_t unpriv_uid; 184 gid_t unpriv_gid; 185 186 on = 1; 187 if (getuid()) 188 errx(1, "not super user"); 189 190 run_as(&unpriv_uid, &unpriv_gid); 191 192 argv++; 193 argc--; 194 while (argc > 0 && *argv[0] == '-') { 195 if (strcmp(*argv, "-m") == 0) { 196 if (argc > 1 && isdigit(*(argv + 1)[0])) { 197 argv++; 198 argc--; 199 multicast_mode = SCOPED_MULTICAST; 200 multicast_scope = atoi(*argv); 201 if (multicast_scope > MAX_MULTICAST_SCOPE) { 202 errx(1, "ttl must not exceed %u", 203 MAX_MULTICAST_SCOPE); 204 } 205 } else { 206 multicast_mode = PER_INTERFACE_MULTICAST; 207 } 208 } else if (strcmp(*argv, "-i") == 0) { 209 insecure_mode = 1; 210 } else if (strcmp(*argv, "-l") == 0) { 211 quiet_mode = 1; 212 } else if (strcmp(*argv, "-p") == 0) { 213 iff_flag = 0; 214 } else { 215 usage(); 216 } 217 argv++; 218 argc--; 219 } 220 if (argc > 0) 221 usage(); 222 #ifndef DEBUG 223 daemon(1, 0); 224 #endif 225 (void) signal(SIGHUP, getboottime); 226 openlog("rwhod", LOG_PID | LOG_NDELAY, LOG_DAEMON); 227 sp = getservbyname("who", "udp"); 228 if (sp == NULL) { 229 syslog(LOG_ERR, "who/udp: unknown service"); 230 exit(1); 231 } 232 if (chdir(_PATH_RWHODIR) < 0) { 233 syslog(LOG_ERR, "%s: %m", _PATH_RWHODIR); 234 exit(1); 235 } 236 /* 237 * Establish host name as returned by system. 238 */ 239 if (gethostname(myname, sizeof(myname) - 1) < 0) { 240 syslog(LOG_ERR, "gethostname: %m"); 241 exit(1); 242 } 243 if ((cp = strchr(myname, '.')) != NULL) 244 *cp = '\0'; 245 strlcpy(mywd.wd_hostname, myname, sizeof(mywd.wd_hostname)); 246 getboottime(0); 247 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { 248 syslog(LOG_ERR, "socket: %m"); 249 exit(1); 250 } 251 if (setsockopt(s, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on)) < 0) { 252 syslog(LOG_ERR, "setsockopt SO_BROADCAST: %m"); 253 exit(1); 254 } 255 memset(&soin, 0, sizeof(soin)); 256 soin.sin_len = sizeof(soin); 257 soin.sin_family = AF_INET; 258 soin.sin_port = sp->s_port; 259 if (bind(s, (struct sockaddr *)&soin, sizeof(soin)) < 0) { 260 syslog(LOG_ERR, "bind: %m"); 261 exit(1); 262 } 263 if (setgid(unpriv_gid) != 0) { 264 syslog(LOG_ERR, "setgid: %m"); 265 exit(1); 266 } 267 if (setgroups(1, &unpriv_gid) != 0) { /* XXX BOGUS groups[0] = egid */ 268 syslog(LOG_ERR, "setgroups: %m"); 269 exit(1); 270 } 271 if (setuid(unpriv_uid) != 0) { 272 syslog(LOG_ERR, "setuid: %m"); 273 exit(1); 274 } 275 if (!configure(s)) 276 exit(1); 277 if (!quiet_mode) { 278 pid_child_receiver = pdfork(&fdp, 0); 279 if (pid_child_receiver == 0) { 280 receiver_process(); 281 } else if (pid_child_receiver > 0) { 282 sender_process(); 283 } else if (pid_child_receiver == -1) { 284 if (errno == ENOSYS) { 285 syslog(LOG_ERR, 286 "The pdfork(2) system call is not available - kernel too old."); 287 } else { 288 syslog(LOG_ERR, "pdfork: %m"); 289 } 290 exit(1); 291 } 292 } else { 293 receiver_process(); 294 } 295 } 296 297 static void 298 usage(void) 299 { 300 301 fprintf(stderr, "usage: rwhod [-i] [-p] [-l] [-m [ttl]]\n"); 302 exit(1); 303 } 304 305 void 306 run_as(uid_t *uid, gid_t *gid) 307 { 308 struct passwd *pw; 309 struct group *gr; 310 311 pw = getpwnam(UNPRIV_USER); 312 if (pw == NULL) { 313 syslog(LOG_ERR, "getpwnam(%s): %m", UNPRIV_USER); 314 exit(1); 315 } 316 *uid = pw->pw_uid; 317 318 gr = getgrnam(UNPRIV_GROUP); 319 if (gr == NULL) { 320 syslog(LOG_ERR, "getgrnam(%s): %m", UNPRIV_GROUP); 321 exit(1); 322 } 323 *gid = gr->gr_gid; 324 } 325 326 /* 327 * Check out host name for unprintables 328 * and other funnies before allowing a file 329 * to be created. Sorry, but blanks aren't allowed. 330 */ 331 int 332 verify(char *name, int maxlen) 333 { 334 int size; 335 336 size = 0; 337 while (*name != '\0' && size < maxlen - 1) { 338 if (!isascii((unsigned char)*name) || 339 !(isalnum((unsigned char)*name) || 340 ispunct((unsigned char)*name))) { 341 return (0); 342 } 343 name++; 344 size++; 345 } 346 *name = '\0'; 347 return (size > 0); 348 } 349 350 void 351 receiver_process(void) 352 { 353 struct sockaddr_in from; 354 struct stat st; 355 cap_rights_t rights; 356 char path[64]; 357 int dirfd; 358 struct whod wd; 359 socklen_t len; 360 int cc, whod; 361 time_t t; 362 363 len = sizeof(from); 364 dirfd = open(".", O_RDONLY | O_DIRECTORY); 365 if (dirfd < 0) { 366 syslog(LOG_WARNING, "%s: %m", _PATH_RWHODIR); 367 exit(1); 368 } 369 cap_rights_init(&rights, CAP_CREATE, CAP_FSTAT, CAP_FTRUNCATE, 370 CAP_LOOKUP, CAP_SEEK, CAP_WRITE); 371 if (cap_rights_limit(dirfd, &rights) < 0 && errno != ENOSYS) { 372 syslog(LOG_WARNING, "cap_rights_limit: %m"); 373 exit(1); 374 } 375 if (cap_enter() < 0 && errno != ENOSYS) { 376 syslog(LOG_ERR, "cap_enter: %m"); 377 exit(1); 378 } 379 for (;;) { 380 cc = recvfrom(s, &wd, sizeof(wd), 0, (struct sockaddr *)&from, 381 &len); 382 if (cc <= 0) { 383 if (cc < 0 && errno != EINTR) 384 syslog(LOG_WARNING, "recv: %m"); 385 continue; 386 } 387 if (from.sin_port != sp->s_port && !insecure_mode) { 388 syslog(LOG_WARNING, "%d: bad source port from %s", 389 ntohs(from.sin_port), inet_ntoa(from.sin_addr)); 390 continue; 391 } 392 if (cc < WHDRSIZE) { 393 syslog(LOG_WARNING, "short packet from %s", 394 inet_ntoa(from.sin_addr)); 395 continue; 396 } 397 if (wd.wd_vers != WHODVERSION) 398 continue; 399 if (wd.wd_type != WHODTYPE_STATUS) 400 continue; 401 if (!verify(wd.wd_hostname, sizeof(wd.wd_hostname))) { 402 syslog(LOG_WARNING, "malformed host name from %s", 403 inet_ntoa(from.sin_addr)); 404 continue; 405 } 406 (void) snprintf(path, sizeof(path), "whod.%s", wd.wd_hostname); 407 /* 408 * Rather than truncating and growing the file each time, 409 * use ftruncate if size is less than previous size. 410 */ 411 whod = openat(dirfd, path, O_WRONLY | O_CREAT, 0644); 412 if (whod < 0) { 413 syslog(LOG_WARNING, "%s: %m", path); 414 continue; 415 } 416 cap_rights_init(&rights, CAP_FSTAT, CAP_FTRUNCATE, CAP_WRITE); 417 if (cap_rights_limit(whod, &rights) < 0 && errno != ENOSYS) { 418 syslog(LOG_WARNING, "cap_rights_limit: %m"); 419 exit(1); 420 } 421 #if ENDIAN != BIG_ENDIAN 422 { 423 struct whoent *we; 424 int i, n; 425 426 n = (cc - WHDRSIZE) / sizeof(struct whoent); 427 /* undo header byte swapping before writing to file */ 428 wd.wd_sendtime = ntohl(wd.wd_sendtime); 429 for (i = 0; i < 3; i++) 430 wd.wd_loadav[i] = ntohl(wd.wd_loadav[i]); 431 wd.wd_boottime = ntohl(wd.wd_boottime); 432 we = wd.wd_we; 433 for (i = 0; i < n; i++) { 434 we->we_idle = ntohl(we->we_idle); 435 we->we_utmp.out_time = 436 ntohl(we->we_utmp.out_time); 437 we++; 438 } 439 } 440 #endif 441 (void) time(&t); 442 wd.wd_recvtime = _time_to_int(t); 443 (void) write(whod, (char *)&wd, cc); 444 if (fstat(whod, &st) < 0 || st.st_size > cc) 445 ftruncate(whod, cc); 446 (void) close(whod); 447 } 448 (void) close(dirfd); 449 } 450 451 void 452 sender_process(void) 453 { 454 int sendcount; 455 double avenrun[3]; 456 time_t now; 457 int i, cc, status; 458 struct utmpx *ut; 459 struct stat stb; 460 struct neighbor *np; 461 struct whoent *we, *wend; 462 463 sendcount = 0; 464 for (;;) { 465 we = mywd.wd_we; 466 now = time(NULL); 467 if (sendcount % 10 == 0) 468 getboottime(0); 469 sendcount++; 470 wend = &mywd.wd_we[1024 / sizeof(struct whoent)]; 471 setutxent(); 472 while ((ut = getutxent()) != NULL && we < wend) { 473 if (ut->ut_type != USER_PROCESS) 474 continue; 475 strncpy(we->we_utmp.out_line, ut->ut_line, 476 sizeof(we->we_utmp.out_line)); 477 strncpy(we->we_utmp.out_name, ut->ut_user, 478 sizeof(we->we_utmp.out_name)); 479 we->we_utmp.out_time = 480 htonl(_time_to_time32(ut->ut_tv.tv_sec)); 481 we++; 482 } 483 endutxent(); 484 485 if (chdir(_PATH_DEV) < 0) { 486 syslog(LOG_ERR, "chdir(%s): %m", _PATH_DEV); 487 exit(1); 488 } 489 wend = we; 490 for (we = mywd.wd_we; we < wend; we++) { 491 if (stat(we->we_utmp.out_line, &stb) >= 0) 492 we->we_idle = htonl(now - stb.st_atime); 493 } 494 (void) getloadavg(avenrun, 495 sizeof(avenrun) / sizeof(avenrun[0])); 496 for (i = 0; i < 3; i++) 497 mywd.wd_loadav[i] = htonl((u_long)(avenrun[i] * 100)); 498 cc = (char *)wend - (char *)&mywd; 499 mywd.wd_sendtime = htonl(_time_to_time32(time(NULL))); 500 mywd.wd_vers = WHODVERSION; 501 mywd.wd_type = WHODTYPE_STATUS; 502 if (multicast_mode == SCOPED_MULTICAST) { 503 (void) sendto(s, (char *)&mywd, cc, 0, 504 (struct sockaddr *)&multicast_addr, 505 sizeof(multicast_addr)); 506 } else { 507 for (np = neighbors; np != NULL; np = np->n_next) { 508 if (multicast_mode == PER_INTERFACE_MULTICAST && 509 (np->n_flags & IFF_MULTICAST) != 0) { 510 /* 511 * Select the outgoing interface for the 512 * multicast. 513 */ 514 if (setsockopt(s, IPPROTO_IP, 515 IP_MULTICAST_IF, 516 &(((struct sockaddr_in *)np->n_addr)->sin_addr), 517 sizeof(struct in_addr)) < 0) { 518 syslog(LOG_ERR, 519 "setsockopt IP_MULTICAST_IF: %m"); 520 exit(1); 521 } 522 (void) sendto(s, (char *)&mywd, cc, 0, 523 (struct sockaddr *)&multicast_addr, 524 sizeof(multicast_addr)); 525 } else { 526 (void) sendto(s, (char *)&mywd, cc, 0, 527 np->n_addr, np->n_addrlen); 528 } 529 } 530 } 531 if (chdir(_PATH_RWHODIR) < 0) { 532 syslog(LOG_ERR, "chdir(%s): %m", _PATH_RWHODIR); 533 exit(1); 534 } 535 if (waitpid(pid_child_receiver, &status, WNOHANG) == 536 pid_child_receiver) { 537 break; 538 } 539 sleep(SL_INTERVAL); 540 } 541 } 542 543 void 544 getboottime(int signo __unused) 545 { 546 int mib[2]; 547 size_t size; 548 struct timeval tm; 549 550 mib[0] = CTL_KERN; 551 mib[1] = KERN_BOOTTIME; 552 size = sizeof(tm); 553 if (sysctl(mib, nitems(mib), &tm, &size, NULL, 0) == -1) { 554 syslog(LOG_ERR, "cannot get boottime: %m"); 555 exit(1); 556 } 557 mywd.wd_boottime = htonl(_time_to_time32(tm.tv_sec)); 558 } 559 560 void 561 quit(const char *msg) 562 { 563 564 syslog(LOG_ERR, "%s", msg); 565 exit(1); 566 } 567 568 void 569 rt_xaddrs(caddr_t cp, caddr_t cplim, struct rt_addrinfo *rtinfo) 570 { 571 struct sockaddr *sa; 572 int i; 573 574 memset(rtinfo->rti_info, 0, sizeof(rtinfo->rti_info)); 575 for (i = 0; i < RTAX_MAX && cp < cplim; i++) { 576 if ((rtinfo->rti_addrs & (1 << i)) == 0) 577 continue; 578 sa = (struct sockaddr *)cp; 579 rtinfo->rti_info[i] = sa; 580 cp += SA_SIZE(sa); 581 } 582 } 583 584 /* 585 * Figure out device configuration and select 586 * networks which deserve status information. 587 */ 588 int 589 configure(int so) 590 { 591 struct neighbor *np; 592 struct if_msghdr *ifm; 593 struct ifa_msghdr *ifam; 594 struct sockaddr_dl *sdl; 595 size_t needed; 596 int mib[6], flags, lflags, len; 597 char *buf, *lim, *next; 598 struct rt_addrinfo info; 599 600 flags = 0; 601 if (multicast_mode != NO_MULTICAST) { 602 multicast_addr.sin_addr.s_addr = htonl(INADDR_WHOD_GROUP); 603 multicast_addr.sin_port = sp->s_port; 604 } 605 606 if (multicast_mode == SCOPED_MULTICAST) { 607 struct ip_mreq mreq; 608 unsigned char ttl; 609 610 mreq.imr_multiaddr.s_addr = htonl(INADDR_WHOD_GROUP); 611 mreq.imr_interface.s_addr = htonl(INADDR_ANY); 612 if (setsockopt(so, IPPROTO_IP, IP_ADD_MEMBERSHIP, 613 &mreq, sizeof(mreq)) < 0) { 614 syslog(LOG_ERR, 615 "setsockopt IP_ADD_MEMBERSHIP: %m"); 616 return (0); 617 } 618 ttl = multicast_scope; 619 if (setsockopt(so, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, 620 sizeof(ttl)) < 0) { 621 syslog(LOG_ERR, 622 "setsockopt IP_MULTICAST_TTL: %m"); 623 return (0); 624 } 625 return (1); 626 } 627 628 mib[0] = CTL_NET; 629 mib[1] = PF_ROUTE; 630 mib[2] = 0; 631 mib[3] = AF_INET; 632 mib[4] = NET_RT_IFLIST; 633 mib[5] = 0; 634 if (sysctl(mib, nitems(mib), NULL, &needed, NULL, 0) < 0) 635 quit("route-sysctl-estimate"); 636 if ((buf = malloc(needed)) == NULL) 637 quit("malloc"); 638 if (sysctl(mib, nitems(mib), buf, &needed, NULL, 0) < 0) 639 quit("actual retrieval of interface table"); 640 lim = buf + needed; 641 642 sdl = NULL; /* XXX just to keep gcc -Wall happy */ 643 for (next = buf; next < lim; next += ifm->ifm_msglen) { 644 ifm = (struct if_msghdr *)next; 645 if (ifm->ifm_type == RTM_IFINFO) { 646 sdl = (struct sockaddr_dl *)(ifm + 1); 647 flags = ifm->ifm_flags; 648 continue; 649 } 650 if ((flags & IFF_UP) == 0) 651 continue; 652 lflags = IFF_BROADCAST | iff_flag; 653 if (multicast_mode == PER_INTERFACE_MULTICAST) 654 lflags |= IFF_MULTICAST; 655 if ((flags & lflags) == 0) 656 continue; 657 if (ifm->ifm_type != RTM_NEWADDR) 658 quit("out of sync parsing NET_RT_IFLIST"); 659 ifam = (struct ifa_msghdr *)ifm; 660 info.rti_addrs = ifam->ifam_addrs; 661 rt_xaddrs((char *)(ifam + 1), ifam->ifam_msglen + (char *)ifam, 662 &info); 663 /* gag, wish we could get rid of Internet dependencies */ 664 #define dstaddr info.rti_info[RTAX_BRD] 665 #define ifaddr info.rti_info[RTAX_IFA] 666 #define IPADDR_SA(x) ((struct sockaddr_in *)(x))->sin_addr.s_addr 667 #define PORT_SA(x) ((struct sockaddr_in *)(x))->sin_port 668 if (dstaddr == 0 || dstaddr->sa_family != AF_INET) 669 continue; 670 PORT_SA(dstaddr) = sp->s_port; 671 for (np = neighbors; np != NULL; np = np->n_next) { 672 if (memcmp(sdl->sdl_data, np->n_name, 673 sdl->sdl_nlen) == 0 && 674 IPADDR_SA(np->n_addr) == IPADDR_SA(dstaddr)) { 675 break; 676 } 677 } 678 if (np != NULL) 679 continue; 680 len = sizeof(*np) + dstaddr->sa_len + sdl->sdl_nlen + 1; 681 np = malloc(len); 682 if (np == NULL) 683 quit("malloc of neighbor structure"); 684 memset(np, 0, len); 685 np->n_flags = flags; 686 np->n_addr = (struct sockaddr *)(np + 1); 687 np->n_addrlen = dstaddr->sa_len; 688 np->n_name = np->n_addrlen + (char *)np->n_addr; 689 memcpy((char *)np->n_addr, (char *)dstaddr, np->n_addrlen); 690 memcpy(np->n_name, sdl->sdl_data, sdl->sdl_nlen); 691 if (multicast_mode == PER_INTERFACE_MULTICAST && 692 (flags & IFF_MULTICAST) != 0 && 693 (flags & IFF_LOOPBACK) == 0) { 694 struct ip_mreq mreq; 695 696 memcpy((char *)np->n_addr, (char *)ifaddr, 697 np->n_addrlen); 698 mreq.imr_multiaddr.s_addr = htonl(INADDR_WHOD_GROUP); 699 mreq.imr_interface.s_addr = 700 ((struct sockaddr_in *)np->n_addr)->sin_addr.s_addr; 701 if (setsockopt(s, IPPROTO_IP, IP_ADD_MEMBERSHIP, 702 &mreq, sizeof(mreq)) < 0) { 703 syslog(LOG_ERR, 704 "setsockopt IP_ADD_MEMBERSHIP: %m"); 705 #if 0 706 /* Fall back to broadcast on this if. */ 707 np->n_flags &= ~IFF_MULTICAST; 708 #else 709 free(np); 710 continue; 711 #endif 712 } 713 } 714 np->n_next = neighbors; 715 neighbors = np; 716 } 717 free(buf); 718 return (1); 719 } 720 721 #ifdef DEBUG 722 void 723 Sendto(int s, const void *buf, size_t cc, int flags, const struct sockaddr *to, 724 int tolen) 725 { 726 struct whod *w; 727 struct whoent *we; 728 struct sockaddr_in *sin; 729 730 w = (struct whod *)buf; 731 sin = (struct sockaddr_in *)to; 732 printf("sendto %x.%d\n", ntohl(sin->sin_addr.s_addr), 733 ntohs(sin->sin_port)); 734 printf("hostname %s %s\n", w->wd_hostname, 735 interval(ntohl(w->wd_sendtime) - ntohl(w->wd_boottime), " up")); 736 printf("load %4.2f, %4.2f, %4.2f\n", 737 ntohl(w->wd_loadav[0]) / 100.0, ntohl(w->wd_loadav[1]) / 100.0, 738 ntohl(w->wd_loadav[2]) / 100.0); 739 cc -= WHDRSIZE; 740 for (we = w->wd_we, cc /= sizeof(struct whoent); cc > 0; cc--, we++) { 741 time_t t = _time32_to_time(ntohl(we->we_utmp.out_time)); 742 743 printf("%-8.8s %s:%s %.12s", we->we_utmp.out_name, 744 w->wd_hostname, we->we_utmp.out_line, ctime(&t) + 4); 745 we->we_idle = ntohl(we->we_idle) / 60; 746 if (we->we_idle != 0) { 747 if (we->we_idle >= 100 * 60) 748 we->we_idle = 100 * 60 - 1; 749 if (we->we_idle >= 60) 750 printf(" %2d", we->we_idle / 60); 751 else 752 printf(" "); 753 printf(":%02d", we->we_idle % 60); 754 } 755 printf("\n"); 756 } 757 } 758 759 char * 760 interval(int time, char *updown) 761 { 762 static char resbuf[32]; 763 int days, hours, minutes; 764 765 if (time < 0 || time > 3 * 30 * 24 * 60 * 60) { 766 (void) sprintf(resbuf, " %s ??:??", updown); 767 return (resbuf); 768 } 769 minutes = (time + 59) / 60; /* round to minutes */ 770 hours = minutes / 60; 771 minutes %= 60; 772 days = hours / 24; 773 hours %= 24; 774 if (days > 0) { 775 (void) sprintf(resbuf, "%s %2d+%02d:%02d", 776 updown, days, hours, minutes); 777 } else { 778 (void) sprintf(resbuf, "%s %2d:%02d", 779 updown, hours, minutes); 780 } 781 return (resbuf); 782 } 783 #endif 784