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