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