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