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