rwhod.c revision 128186
1185377Ssam/*
2187831Ssam * Copyright (c) 1983, 1993
3185377Ssam *	The Regents of the University of California.  All rights reserved.
4185377Ssam *
5185377Ssam * Redistribution and use in source and binary forms, with or without
6185377Ssam * modification, are permitted provided that the following conditions
7185377Ssam * are met:
8185377Ssam * 1. Redistributions of source code must retain the above copyright
9185377Ssam *    notice, this list of conditions and the following disclaimer.
10185377Ssam * 2. Redistributions in binary form must reproduce the above copyright
11185377Ssam *    notice, this list of conditions and the following disclaimer in the
12185377Ssam *    documentation and/or other materials provided with the distribution.
13185377Ssam * 3. All advertising materials mentioning features or use of this software
14185377Ssam *    must display the following acknowledgement:
15185377Ssam *	This product includes software developed by the University of
16185377Ssam *	California, Berkeley and its contributors.
17185907Ssam * 4. Neither the name of the University nor the names of its contributors
18185377Ssam *    may be used to endorse or promote products derived from this software
19185377Ssam *    without specific prior written permission.
20185377Ssam *
21185377Ssam * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22185377Ssam * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23185377Ssam * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24185377Ssam * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25185377Ssam * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26185377Ssam * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27185377Ssam * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28185377Ssam * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29185377Ssam * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30185377Ssam * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31185377Ssam * SUCH DAMAGE.
32185377Ssam */
33185377Ssam
34185377Ssam#ifndef lint
35185377Ssamstatic const char copyright[] =
36185377Ssam"@(#) Copyright (c) 1983, 1993\n\
37185377Ssam	The Regents of the University of California.  All rights reserved.\n";
38185377Ssam#endif /* not lint */
39185377Ssam
40185377Ssam#if 0
41185377Ssam#ifndef lint
42185377Ssamstatic char sccsid[] = "@(#)rwhod.c	8.1 (Berkeley) 6/6/93";
43185377Ssam#endif /* not lint */
44185377Ssam#endif
45185377Ssam
46185377Ssam#include <sys/cdefs.h>
47185377Ssam__FBSDID("$FreeBSD: head/usr.sbin/rwhod/rwhod.c 128186 2004-04-13 11:24:43Z luigi $");
48185377Ssam
49185377Ssam#include <sys/param.h>
50185377Ssam#include <sys/socket.h>
51185377Ssam#include <sys/stat.h>
52185377Ssam#include <sys/signal.h>
53185377Ssam#include <sys/ioctl.h>
54185377Ssam#include <sys/sysctl.h>
55185377Ssam
56185377Ssam#include <net/if.h>
57185377Ssam#include <net/if_dl.h>
58185377Ssam#include <net/route.h>
59185377Ssam#include <netinet/in.h>
60185377Ssam#include <arpa/inet.h>
61185377Ssam#include <protocols/rwhod.h>
62185377Ssam
63185377Ssam#include <ctype.h>
64185377Ssam#include <err.h>
65185377Ssam#include <errno.h>
66185377Ssam#include <fcntl.h>
67185377Ssam#include <netdb.h>
68185377Ssam#include <paths.h>
69185377Ssam#include <stdio.h>
70185377Ssam#include <stdlib.h>
71185377Ssam#include <string.h>
72185377Ssam#include <syslog.h>
73185377Ssam#include <timeconv.h>
74185377Ssam#include <unistd.h>
75185377Ssam#include <utmp.h>
76185380Ssam#include <pwd.h>
77185380Ssam#include <grp.h>
78185380Ssam
79185380Ssam/*
80185380Ssam * This version of Berkeley's rwhod has been modified to use IP multicast
81185380Ssam * datagrams, under control of a new command-line option:
82185380Ssam *
83185380Ssam *	rwhod -m	causes rwhod to use IP multicast (instead of
84185380Ssam *			broadcast or unicast) on all interfaces that have
85185380Ssam *			the IFF_MULTICAST flag set in their "ifnet" structs
86185380Ssam *			(excluding the loopback interface).  The multicast
87185380Ssam *			reports are sent with a time-to-live of 1, to prevent
88185380Ssam *			forwarding beyond the directly-connected subnet(s).
89185380Ssam *
90185380Ssam *	rwhod -m <ttl>	causes rwhod to send IP multicast datagrams with a
91185380Ssam *			time-to-live of <ttl>, via a SINGLE interface rather
92185380Ssam *			than all interfaces.  <ttl> must be between 0 and
93185380Ssam *			MAX_MULTICAST_SCOPE, defined below.  Note that "-m 1"
94185380Ssam *			is different than "-m", in that "-m 1" specifies
95185380Ssam *			transmission on one interface only.
96185380Ssam *
97185380Ssam * When "-m" is used without a <ttl> argument, the program accepts multicast
98185380Ssam * rwhod reports from all multicast-capable interfaces.  If a <ttl> argument
99185380Ssam * is given, it accepts multicast reports from only one interface, the one
100185380Ssam * on which reports are sent (which may be controlled via the host's routing
101185380Ssam * table).  Regardless of the "-m" option, the program accepts broadcast or
102185380Ssam * unicast reports from all interfaces.  Thus, this program will hear the
103185380Ssam * reports of old, non-multicasting rwhods, but, if multicasting is used,
104185380Ssam * those old rwhods won't hear the reports generated by this program.
105185380Ssam *
106185380Ssam *                  -- Steve Deering, Stanford University, February 1989
107185380Ssam */
108185380Ssam
109185377Ssam#define	UNPRIV_USER		"daemon"
110185377Ssam#define	UNPRIV_GROUP		"daemon"
111185377Ssam
112185377Ssam#define NO_MULTICAST		0	  /* multicast modes */
113185377Ssam#define PER_INTERFACE_MULTICAST	1
114185377Ssam#define SCOPED_MULTICAST	2
115185377Ssam
116185377Ssam#define MAX_MULTICAST_SCOPE	32	  /* "site-wide", by convention */
117185377Ssam
118185377Ssam#define INADDR_WHOD_GROUP (u_long)0xe0000103      /* 224.0.1.3 */
119185377Ssam					  /* (belongs in protocols/rwhod.h) */
120185377Ssam
121185377Ssamint			insecure_mode;
122185377Ssamint			quiet_mode;
123185380Ssamint			iff_flag = IFF_POINTOPOINT;
124185380Ssamint			multicast_mode  = NO_MULTICAST;
125185380Ssamint			multicast_scope;
126185380Ssamstruct sockaddr_in	multicast_addr  =
127185377Ssam	{ sizeof multicast_addr, AF_INET, 0, { 0 }, { 0 } };
128185377Ssam
129185377Ssam/*
130185377Ssam * Alarm interval. Don't forget to change the down time check in ruptime
131185377Ssam * if this is changed.
132185377Ssam */
133185377Ssam#define AL_INTERVAL (3 * 60)
134185377Ssam
135185380Ssamchar	myname[MAXHOSTNAMELEN];
136185380Ssam
137185380Ssam/*
138185380Ssam * We communicate with each neighbor in a list constructed at the time we're
139185377Ssam * started up.  Neighbors are currently directly connected via a hardware
140185377Ssam * interface.
141185377Ssam */
142185377Ssamstruct	neighbor {
143185377Ssam	struct	neighbor *n_next;
144185377Ssam	char	*n_name;		/* interface name */
145185377Ssam	struct	sockaddr *n_addr;		/* who to send to */
146185377Ssam	int	n_addrlen;		/* size of address */
147185377Ssam	int	n_flags;		/* should forward?, interface flags */
148185377Ssam};
149185377Ssam
150185377Ssamstruct	neighbor *neighbors;
151185377Ssamstruct	whod mywd;
152185377Ssamstruct	servent *sp;
153185377Ssamint	s, utmpf;
154185377Ssam
155185377Ssam#define	WHDRSIZE	(int)(sizeof(mywd) - sizeof(mywd.wd_we))
156185377Ssam
157185377Ssamvoid	 run_as(uid_t *, gid_t *);
158185377Ssamint	 configure(int);
159185377Ssamvoid	 getboottime(int);
160185377Ssamvoid	 onalrm(int);
161185377Ssamvoid	 quit(const char *);
162185377Ssamvoid	 rt_xaddrs(caddr_t, caddr_t, struct rt_addrinfo *);
163185377Ssamint	 verify(char *, int);
164185377Ssamstatic void usage(void);
165185377Ssam#ifdef DEBUG
166185377Ssamchar	*interval(int, char *);
167185377Ssamvoid	 Sendto(int, const void *, size_t, int, const struct sockaddr *, int);
168185377Ssam#define	 sendto Sendto
169185377Ssam#endif
170185377Ssam
171185377Ssamint
172185377Ssammain(int argc, char *argv[])
173185377Ssam{
174185377Ssam	struct sockaddr_in from;
175185377Ssam	struct stat st;
176185377Ssam	char path[64];
177185377Ssam	int on = 1;
178185377Ssam	char *cp;
179185377Ssam	struct sockaddr_in soin;
180185377Ssam	uid_t unpriv_uid;
181185377Ssam	gid_t unpriv_gid;
182185377Ssam
183185377Ssam	if (getuid())
184185377Ssam		errx(1, "not super user");
185185377Ssam
186185377Ssam	run_as(&unpriv_uid, &unpriv_gid);
187185377Ssam
188185377Ssam	argv++; argc--;
189185377Ssam	while (argc > 0 && *argv[0] == '-') {
190185377Ssam		if (strcmp(*argv, "-m") == 0) {
191185377Ssam			if (argc > 1 && isdigit(*(argv + 1)[0])) {
192185377Ssam				argv++, argc--;
193185377Ssam				multicast_mode  = SCOPED_MULTICAST;
194185377Ssam				multicast_scope = atoi(*argv);
195185377Ssam				if (multicast_scope > MAX_MULTICAST_SCOPE)
196185377Ssam					errx(1, "ttl must not exceed %u",
197185377Ssam					MAX_MULTICAST_SCOPE);
198185377Ssam			}
199185377Ssam			else multicast_mode = PER_INTERFACE_MULTICAST;
200185377Ssam		}
201185377Ssam		else if (strcmp(*argv, "-i") == 0)
202185377Ssam			insecure_mode = 1;
203185377Ssam		else if (strcmp(*argv, "-l") == 0)
204185377Ssam			quiet_mode = 1;
205185377Ssam		else if (strcmp(*argv, "-p") == 0)
206185377Ssam			iff_flag = 0;
207185377Ssam		else
208185377Ssam			usage();
209185377Ssam		argv++, argc--;
210185377Ssam	}
211185377Ssam	if (argc > 0)
212185377Ssam		usage();
213185377Ssam#ifndef DEBUG
214185377Ssam	daemon(1, 0);
215185377Ssam#endif
216185377Ssam	(void) signal(SIGHUP, getboottime);
217185377Ssam	openlog("rwhod", LOG_PID, LOG_DAEMON);
218185377Ssam	sp = getservbyname("who", "udp");
219185377Ssam	if (sp == NULL) {
220185377Ssam		syslog(LOG_ERR, "who/udp: unknown service");
221185377Ssam		exit(1);
222185377Ssam	}
223185377Ssam	if (chdir(_PATH_RWHODIR) < 0) {
224185377Ssam		syslog(LOG_ERR, "%s: %m", _PATH_RWHODIR);
225185377Ssam		exit(1);
226185377Ssam	}
227185377Ssam	/*
228185377Ssam	 * Establish host name as returned by system.
229185377Ssam	 */
230185377Ssam	if (gethostname(myname, sizeof(myname) - 1) < 0) {
231185377Ssam		syslog(LOG_ERR, "gethostname: %m");
232185377Ssam		exit(1);
233185377Ssam	}
234185377Ssam	if ((cp = index(myname, '.')) != NULL)
235185377Ssam		*cp = '\0';
236185377Ssam	strncpy(mywd.wd_hostname, myname, sizeof(mywd.wd_hostname) - 1);
237185377Ssam	mywd.wd_hostname[sizeof(mywd.wd_hostname) - 1] = '\0';
238185377Ssam	utmpf = open(_PATH_UTMP, O_RDONLY|O_CREAT, 0644);
239185377Ssam	if (utmpf < 0) {
240185377Ssam		syslog(LOG_ERR, "%s: %m", _PATH_UTMP);
241185377Ssam		exit(1);
242185377Ssam	}
243185377Ssam	getboottime(0);
244185377Ssam	if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
245185377Ssam		syslog(LOG_ERR, "socket: %m");
246185377Ssam		exit(1);
247185377Ssam	}
248185380Ssam	if (setsockopt(s, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on)) < 0) {
249185380Ssam		syslog(LOG_ERR, "setsockopt SO_BROADCAST: %m");
250185380Ssam		exit(1);
251185380Ssam	}
252185377Ssam	memset(&soin, 0, sizeof(soin));
253185377Ssam	soin.sin_len = sizeof(soin);
254185377Ssam	soin.sin_family = AF_INET;
255185377Ssam	soin.sin_port = sp->s_port;
256185377Ssam	if (bind(s, (struct sockaddr *)&soin, sizeof(soin)) < 0) {
257185377Ssam		syslog(LOG_ERR, "bind: %m");
258185377Ssam		exit(1);
259185377Ssam	}
260185377Ssam	setgid(unpriv_gid);
261185377Ssam	setgroups(1, &unpriv_gid);	/* XXX BOGUS groups[0] = egid */
262185377Ssam	setuid(unpriv_uid);
263185377Ssam	if (!configure(s))
264185377Ssam		exit(1);
265185377Ssam	if (!quiet_mode) {
266185377Ssam		signal(SIGALRM, onalrm);
267185377Ssam		onalrm(0);
268185377Ssam	}
269185377Ssam	for (;;) {
270185377Ssam		struct whod wd;
271185377Ssam		int cc, whod, len = sizeof(from);
272185377Ssam		time_t t;
273185377Ssam
274185377Ssam		cc = recvfrom(s, (char *)&wd, sizeof(struct whod), 0,
275185377Ssam			(struct sockaddr *)&from, &len);
276185377Ssam		if (cc <= 0) {
277185377Ssam			if (cc < 0 && errno != EINTR)
278185377Ssam				syslog(LOG_WARNING, "recv: %m");
279185377Ssam			continue;
280185377Ssam		}
281185377Ssam		if (from.sin_port != sp->s_port && !insecure_mode) {
282185377Ssam			syslog(LOG_WARNING, "%d: bad source port from %s",
283185377Ssam			    ntohs(from.sin_port), inet_ntoa(from.sin_addr));
284185377Ssam			continue;
285185377Ssam		}
286185377Ssam		if (cc < WHDRSIZE) {
287185377Ssam			syslog(LOG_WARNING, "short packet from %s",
288185377Ssam			    inet_ntoa(from.sin_addr));
289185377Ssam			continue;
290185377Ssam		}
291185377Ssam		if (wd.wd_vers != WHODVERSION)
292185377Ssam			continue;
293185377Ssam		if (wd.wd_type != WHODTYPE_STATUS)
294185377Ssam			continue;
295187831Ssam		if (!verify(wd.wd_hostname, sizeof wd.wd_hostname)) {
296185377Ssam			syslog(LOG_WARNING, "malformed host name from %s",
297185377Ssam			    inet_ntoa(from.sin_addr));
298185377Ssam			continue;
299185377Ssam		}
300187831Ssam		(void) snprintf(path, sizeof path, "whod.%s", wd.wd_hostname);
301185377Ssam		/*
302185377Ssam		 * Rather than truncating and growing the file each time,
303185377Ssam		 * use ftruncate if size is less than previous size.
304185377Ssam		 */
305185377Ssam		whod = open(path, O_WRONLY | O_CREAT, 0644);
306185377Ssam		if (whod < 0) {
307185377Ssam			syslog(LOG_WARNING, "%s: %m", path);
308185377Ssam			continue;
309185377Ssam		}
310185377Ssam#if ENDIAN != BIG_ENDIAN
311185377Ssam		{
312185377Ssam			int i, n = (cc - WHDRSIZE)/sizeof(struct whoent);
313185377Ssam			struct whoent *we;
314185377Ssam
315185377Ssam			/* undo header byte swapping before writing to file */
316185377Ssam			wd.wd_sendtime = ntohl(wd.wd_sendtime);
317185377Ssam			for (i = 0; i < 3; i++)
318185377Ssam				wd.wd_loadav[i] = ntohl(wd.wd_loadav[i]);
319185377Ssam			wd.wd_boottime = ntohl(wd.wd_boottime);
320185377Ssam			we = wd.wd_we;
321185377Ssam			for (i = 0; i < n; i++) {
322185377Ssam				we->we_idle = ntohl(we->we_idle);
323185377Ssam				we->we_utmp.out_time =
324185377Ssam				    ntohl(we->we_utmp.out_time);
325185377Ssam				we++;
326185377Ssam			}
327185377Ssam		}
328185377Ssam#endif
329185377Ssam		(void) time(&t);
330185377Ssam		wd.wd_recvtime = _time_to_int(t);
331185377Ssam		(void) write(whod, (char *)&wd, cc);
332185377Ssam		if (fstat(whod, &st) < 0 || st.st_size > cc)
333185377Ssam			ftruncate(whod, cc);
334185377Ssam		(void) close(whod);
335185377Ssam	}
336185377Ssam}
337185377Ssam
338185377Ssamstatic void
339185377Ssamusage()
340185377Ssam{
341185377Ssam	fprintf(stderr, "usage: rwhod [-i] [-p] [-l] [-m [ttl]]\n");
342185377Ssam	exit(1);
343185377Ssam}
344185377Ssam
345185377Ssamvoid
346185377Ssamrun_as(uid, gid)
347185377Ssam	uid_t *uid;
348185377Ssam	gid_t *gid;
349185377Ssam{
350185377Ssam	struct passwd *pw;
351185377Ssam	struct group *gr;
352185377Ssam
353185377Ssam	pw = getpwnam(UNPRIV_USER);
354185377Ssam	if (!pw) {
355185377Ssam		syslog(LOG_ERR, "getpwnam(%s): %m", UNPRIV_USER);
356185377Ssam		exit(1);
357185377Ssam	}
358185377Ssam	*uid = pw->pw_uid;
359185377Ssam
360185377Ssam	gr = getgrnam(UNPRIV_GROUP);
361185377Ssam	if (!gr) {
362185377Ssam		syslog(LOG_ERR, "getgrnam(%s): %m", UNPRIV_GROUP);
363185377Ssam		exit(1);
364185377Ssam	}
365185377Ssam	*gid = gr->gr_gid;
366185377Ssam}
367185377Ssam
368185377Ssam/*
369185377Ssam * Check out host name for unprintables
370185377Ssam * and other funnies before allowing a file
371185377Ssam * to be created.  Sorry, but blanks aren't allowed.
372185377Ssam */
373185377Ssamint
374185377Ssamverify(name, maxlen)
375185377Ssam	register char *name;
376185377Ssam	register int   maxlen;
377185377Ssam{
378185377Ssam	register int size = 0;
379185377Ssam
380185377Ssam	while (*name && size < maxlen - 1) {
381185377Ssam		if (!isascii(*name) || !(isalnum(*name) || ispunct(*name)))
382185377Ssam			return (0);
383185377Ssam		name++, size++;
384185377Ssam	}
385185377Ssam	*name = '\0';
386185377Ssam	return (size > 0);
387185377Ssam}
388185377Ssam
389185377Ssamint	utmptime;
390185377Ssamint	utmpent;
391185377Ssamint	utmpsize = 0;
392185377Ssamstruct	utmp *utmp;
393185377Ssamint	alarmcount;
394185377Ssam
395185377Ssamvoid
396185377Ssamonalrm(signo)
397185377Ssam	int signo __unused;
398185377Ssam{
399185377Ssam	register struct neighbor *np;
400185377Ssam	register struct whoent *we = mywd.wd_we, *wlast;
401185377Ssam	register int i;
402185377Ssam	struct stat stb;
403185377Ssam	double avenrun[3];
404185377Ssam	time_t now;
405185377Ssam	int cc;
406185377Ssam
407185377Ssam	now = time(NULL);
408185377Ssam	if (alarmcount % 10 == 0)
409185377Ssam		getboottime(0);
410185377Ssam	alarmcount++;
411185377Ssam	(void) fstat(utmpf, &stb);
412185377Ssam	if ((stb.st_mtime != utmptime) || (stb.st_size > utmpsize)) {
413185377Ssam		utmptime = stb.st_mtime;
414185377Ssam		if (stb.st_size > utmpsize) {
415185377Ssam			utmpsize = stb.st_size + 10 * sizeof(struct utmp);
416185377Ssam			if (utmp)
417185380Ssam				utmp = (struct utmp *)realloc(utmp, utmpsize);
418185377Ssam			else
419185377Ssam				utmp = (struct utmp *)malloc(utmpsize);
420185377Ssam			if (! utmp) {
421185380Ssam				syslog(LOG_WARNING, "malloc failed");
422185377Ssam				utmpsize = 0;
423185380Ssam				goto done;
424187831Ssam			}
425185377Ssam		}
426187831Ssam		(void) lseek(utmpf, (off_t)0, L_SET);
427185380Ssam		cc = read(utmpf, (char *)utmp, stb.st_size);
428185380Ssam		if (cc < 0) {
429185380Ssam			syslog(LOG_ERR, "read(%s): %m", _PATH_UTMP);
430185377Ssam			goto done;
431185377Ssam		}
432187831Ssam		wlast = &mywd.wd_we[1024 / sizeof(struct whoent) - 1];
433185377Ssam		utmpent = cc / sizeof(struct utmp);
434185377Ssam		for (i = 0; i < utmpent; i++)
435185377Ssam			if (utmp[i].ut_name[0]) {
436185377Ssam				memcpy(we->we_utmp.out_line, utmp[i].ut_line,
437185377Ssam				   sizeof(utmp[i].ut_line));
438185377Ssam				memcpy(we->we_utmp.out_name, utmp[i].ut_name,
439185377Ssam				   sizeof(utmp[i].ut_name));
440185377Ssam				we->we_utmp.out_time = htonl(utmp[i].ut_time);
441185377Ssam				if (we >= wlast)
442185377Ssam					break;
443185377Ssam				we++;
444185377Ssam			}
445185377Ssam		utmpent = we - mywd.wd_we;
446185377Ssam	}
447185377Ssam
448185377Ssam	/*
449185377Ssam	 * The test on utmpent looks silly---after all, if no one is
450185377Ssam	 * logged on, why worry about efficiency?---but is useful on
451185377Ssam	 * (e.g.) compute servers.
452185377Ssam	 */
453185377Ssam	if (utmpent && chdir(_PATH_DEV)) {
454187831Ssam		syslog(LOG_ERR, "chdir(%s): %m", _PATH_DEV);
455185377Ssam		exit(1);
456185377Ssam	}
457185377Ssam	we = mywd.wd_we;
458185377Ssam	for (i = 0; i < utmpent; i++) {
459185377Ssam		if (stat(we->we_utmp.out_line, &stb) >= 0)
460185377Ssam			we->we_idle = htonl(now - stb.st_atime);
461185377Ssam		we++;
462185377Ssam	}
463185377Ssam	(void)getloadavg(avenrun, sizeof(avenrun)/sizeof(avenrun[0]));
464185377Ssam	for (i = 0; i < 3; i++)
465185377Ssam		mywd.wd_loadav[i] = htonl((u_long)(avenrun[i] * 100));
466185377Ssam	cc = (char *)we - (char *)&mywd;
467185377Ssam	mywd.wd_sendtime = htonl(_time_to_time32(time(NULL)));
468185377Ssam	mywd.wd_vers = WHODVERSION;
469185377Ssam	mywd.wd_type = WHODTYPE_STATUS;
470185377Ssam	if (multicast_mode == SCOPED_MULTICAST) {
471185377Ssam		(void) sendto(s, (char *)&mywd, cc, 0,
472185377Ssam				(struct sockaddr *)&multicast_addr,
473185377Ssam				sizeof(multicast_addr));
474185377Ssam	}
475185377Ssam	else for (np = neighbors; np != NULL; np = np->n_next) {
476185377Ssam		if (multicast_mode == PER_INTERFACE_MULTICAST &&
477185377Ssam		    np->n_flags & IFF_MULTICAST) {
478185377Ssam			/*
479185377Ssam			 * Select the outgoing interface for the multicast.
480185377Ssam			 */
481185377Ssam			if (setsockopt(s, IPPROTO_IP, IP_MULTICAST_IF,
482185377Ssam			    &(((struct sockaddr_in *)np->n_addr)->sin_addr),
483185377Ssam			    sizeof(struct in_addr)) < 0) {
484185377Ssam				syslog(LOG_ERR,
485185377Ssam					"setsockopt IP_MULTICAST_IF: %m");
486185377Ssam				exit(1);
487185377Ssam			}
488185377Ssam			(void) sendto(s, (char *)&mywd, cc, 0,
489185377Ssam				(struct sockaddr *)&multicast_addr,
490185377Ssam				sizeof(multicast_addr));
491185377Ssam		} else (void) sendto(s, (char *)&mywd, cc, 0,
492185377Ssam					np->n_addr, np->n_addrlen);
493185377Ssam	}
494185377Ssam	if (utmpent && chdir(_PATH_RWHODIR)) {
495185377Ssam		syslog(LOG_ERR, "chdir(%s): %m", _PATH_RWHODIR);
496185377Ssam		exit(1);
497185377Ssam	}
498185377Ssamdone:
499185377Ssam	(void) alarm(AL_INTERVAL);
500185377Ssam}
501185377Ssam
502185377Ssamvoid
503185377Ssamgetboottime(signo)
504185377Ssam	int signo __unused;
505185377Ssam{
506185377Ssam	int mib[2];
507185377Ssam	size_t size;
508185377Ssam	struct timeval tm;
509185377Ssam
510185377Ssam	mib[0] = CTL_KERN;
511185377Ssam	mib[1] = KERN_BOOTTIME;
512185377Ssam	size = sizeof(tm);
513185377Ssam	if (sysctl(mib, 2, &tm, &size, NULL, 0) == -1) {
514185377Ssam		syslog(LOG_ERR, "cannot get boottime: %m");
515185377Ssam		exit(1);
516185377Ssam	}
517185377Ssam	mywd.wd_boottime = htonl(_time_to_time32(tm.tv_sec));
518185377Ssam}
519185377Ssam
520185377Ssamvoid
521185377Ssamquit(msg)
522185377Ssam	const char *msg;
523185377Ssam{
524185377Ssam	syslog(LOG_ERR, "%s", msg);
525185377Ssam	exit(1);
526185377Ssam}
527185377Ssam
528185377Ssamvoid
529185377Ssamrt_xaddrs(cp, cplim, rtinfo)
530185377Ssam	register caddr_t cp, cplim;
531185377Ssam	register struct rt_addrinfo *rtinfo;
532185377Ssam{
533185377Ssam	register struct sockaddr *sa;
534185377Ssam	register int i;
535185377Ssam
536185377Ssam	memset(rtinfo->rti_info, 0, sizeof(rtinfo->rti_info));
537185377Ssam	for (i = 0; (i < RTAX_MAX) && (cp < cplim); i++) {
538185377Ssam		if ((rtinfo->rti_addrs & (1 << i)) == 0)
539185377Ssam			continue;
540185377Ssam		rtinfo->rti_info[i] = sa = (struct sockaddr *)cp;
541185377Ssam		cp += SA_SIZE(sa);
542185377Ssam	}
543185377Ssam}
544185377Ssam
545185377Ssam/*
546185377Ssam * Figure out device configuration and select
547185377Ssam * networks which deserve status information.
548185377Ssam */
549185377Ssamint
550185377Ssamconfigure(so)
551185377Ssam	int so;
552185377Ssam{
553185377Ssam	register struct neighbor *np;
554185377Ssam	register struct if_msghdr *ifm;
555185377Ssam	register struct ifa_msghdr *ifam;
556185377Ssam	struct sockaddr_dl *sdl;
557185377Ssam	size_t needed;
558185377Ssam	int mib[6], flags = 0, len;
559185377Ssam	char *buf, *lim, *next;
560185377Ssam	struct rt_addrinfo info;
561185377Ssam
562185377Ssam	if (multicast_mode != NO_MULTICAST) {
563185377Ssam		multicast_addr.sin_addr.s_addr = htonl(INADDR_WHOD_GROUP);
564185377Ssam		multicast_addr.sin_port = sp->s_port;
565185377Ssam	}
566185377Ssam
567185377Ssam	if (multicast_mode == SCOPED_MULTICAST) {
568185377Ssam		struct ip_mreq mreq;
569185377Ssam		unsigned char ttl;
570185377Ssam
571185377Ssam		mreq.imr_multiaddr.s_addr = htonl(INADDR_WHOD_GROUP);
572185377Ssam		mreq.imr_interface.s_addr = htonl(INADDR_ANY);
573185377Ssam		if (setsockopt(so, IPPROTO_IP, IP_ADD_MEMBERSHIP,
574185377Ssam					&mreq, sizeof(mreq)) < 0) {
575185377Ssam			syslog(LOG_ERR,
576185377Ssam				"setsockopt IP_ADD_MEMBERSHIP: %m");
577185377Ssam			return(0);
578185377Ssam		}
579185377Ssam		ttl = multicast_scope;
580185377Ssam		if (setsockopt(so, IPPROTO_IP, IP_MULTICAST_TTL,
581185377Ssam					&ttl, sizeof(ttl)) < 0) {
582185377Ssam			syslog(LOG_ERR,
583185377Ssam				"setsockopt IP_MULTICAST_TTL: %m");
584185377Ssam			return(0);
585185377Ssam		}
586185377Ssam		return(1);
587185377Ssam	}
588185377Ssam
589185377Ssam	mib[0] = CTL_NET;
590185377Ssam	mib[1] = PF_ROUTE;
591185377Ssam	mib[2] = 0;
592185377Ssam	mib[3] = AF_INET;
593187831Ssam	mib[4] = NET_RT_IFLIST;
594185377Ssam	mib[5] = 0;
595185377Ssam	if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
596185377Ssam		quit("route-sysctl-estimate");
597185377Ssam	if ((buf = malloc(needed)) == NULL)
598185377Ssam		quit("malloc");
599185377Ssam	if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
600185377Ssam		quit("actual retrieval of interface table");
601185377Ssam	lim = buf + needed;
602187831Ssam
603185377Ssam	sdl = NULL;		/* XXX just to keep gcc -Wall happy */
604185377Ssam	for (next = buf; next < lim; next += ifm->ifm_msglen) {
605187831Ssam		ifm = (struct if_msghdr *)next;
606185377Ssam		if (ifm->ifm_type == RTM_IFINFO) {
607185377Ssam			sdl = (struct sockaddr_dl *)(ifm + 1);
608185377Ssam			flags = ifm->ifm_flags;
609185377Ssam			continue;
610185377Ssam		}
611185377Ssam		if ((flags & IFF_UP) == 0 ||
612185377Ssam		    (flags & (((multicast_mode == PER_INTERFACE_MULTICAST) ?
613185377Ssam				IFF_MULTICAST : 0) |
614185377Ssam				IFF_BROADCAST|iff_flag)) == 0)
615185377Ssam			continue;
616185377Ssam		if (ifm->ifm_type != RTM_NEWADDR)
617185377Ssam			quit("out of sync parsing NET_RT_IFLIST");
618185377Ssam		ifam = (struct ifa_msghdr *)ifm;
619185377Ssam		info.rti_addrs = ifam->ifam_addrs;
620185377Ssam		rt_xaddrs((char *)(ifam + 1), ifam->ifam_msglen + (char *)ifam,
621185377Ssam			&info);
622185377Ssam		/* gag, wish we could get rid of Internet dependencies */
623185377Ssam#define dstaddr	info.rti_info[RTAX_BRD]
624185377Ssam#define ifaddr info.rti_info[RTAX_IFA]
625185377Ssam#define IPADDR_SA(x) ((struct sockaddr_in *)(x))->sin_addr.s_addr
626185377Ssam#define PORT_SA(x) ((struct sockaddr_in *)(x))->sin_port
627185377Ssam		if (dstaddr == 0 || dstaddr->sa_family != AF_INET)
628185377Ssam			continue;
629185377Ssam		PORT_SA(dstaddr) = sp->s_port;
630185377Ssam		for (np = neighbors; np != NULL; np = np->n_next)
631185377Ssam			if (memcmp(sdl->sdl_data, np->n_name,
632185377Ssam				   sdl->sdl_nlen) == 0 &&
633185377Ssam			    IPADDR_SA(np->n_addr) == IPADDR_SA(dstaddr))
634185377Ssam				break;
635185377Ssam		if (np != NULL)
636185377Ssam			continue;
637185377Ssam		len = sizeof(*np) + dstaddr->sa_len + sdl->sdl_nlen + 1;
638185377Ssam		np = (struct neighbor *)malloc(len);
639185377Ssam		if (np == NULL)
640185377Ssam			quit("malloc of neighbor structure");
641185377Ssam		memset(np, 0, len);
642185377Ssam		np->n_flags = flags;
643185377Ssam		np->n_addr = (struct sockaddr *)(np + 1);
644185377Ssam		np->n_addrlen = dstaddr->sa_len;
645185377Ssam		np->n_name = np->n_addrlen + (char *)np->n_addr;
646185377Ssam		memcpy((char *)np->n_addr, (char *)dstaddr, np->n_addrlen);
647185377Ssam		memcpy(np->n_name, sdl->sdl_data, sdl->sdl_nlen);
648185377Ssam		if (multicast_mode == PER_INTERFACE_MULTICAST &&
649185377Ssam		    (flags & IFF_MULTICAST) &&
650185377Ssam		   !(flags & IFF_LOOPBACK)) {
651185377Ssam			struct ip_mreq mreq;
652185377Ssam
653185377Ssam			memcpy((char *)np->n_addr, (char *)ifaddr,
654185377Ssam				np->n_addrlen);
655185377Ssam			mreq.imr_multiaddr.s_addr = htonl(INADDR_WHOD_GROUP);
656185377Ssam			mreq.imr_interface.s_addr =
657185377Ssam			  ((struct sockaddr_in *)np->n_addr)->sin_addr.s_addr;
658185377Ssam			if (setsockopt(s, IPPROTO_IP, IP_ADD_MEMBERSHIP,
659185377Ssam						&mreq, sizeof(mreq)) < 0) {
660185377Ssam				syslog(LOG_ERR,
661185377Ssam				    "setsockopt IP_ADD_MEMBERSHIP: %m");
662185377Ssam#if 0
663185377Ssam				/* Fall back to broadcast on this if. */
664185377Ssam				np->n_flags &= ~IFF_MULTICAST;
665185377Ssam#else
666185377Ssam				free((char *)np);
667185377Ssam				continue;
668185377Ssam#endif
669185377Ssam			}
670185380Ssam		}
671185380Ssam		np->n_next = neighbors;
672185377Ssam		neighbors = np;
673185377Ssam	}
674185377Ssam	free(buf);
675185377Ssam	return (1);
676185377Ssam}
677185377Ssam
678185377Ssam#ifdef DEBUG
679185377Ssamvoid
680185377SsamSendto(s, buf, cc, flags, to, tolen)
681185377Ssam	int s;
682185377Ssam	const void *buf;
683185377Ssam	size_t cc;
684185377Ssam	int flags;
685185377Ssam	const struct sockaddr *to;
686185377Ssam	int tolen;
687185377Ssam{
688185377Ssam	register struct whod *w = (struct whod *)buf;
689185377Ssam	register struct whoent *we;
690185377Ssam	struct sockaddr_in *sin = (struct sockaddr_in *)to;
691185377Ssam
692185377Ssam	printf("sendto %x.%d\n", ntohl(sin->sin_addr.s_addr),
693185377Ssam				 ntohs(sin->sin_port));
694185377Ssam	printf("hostname %s %s\n", w->wd_hostname,
695185377Ssam	   interval(ntohl(w->wd_sendtime) - ntohl(w->wd_boottime), "  up"));
696185377Ssam	printf("load %4.2f, %4.2f, %4.2f\n",
697185377Ssam	    ntohl(w->wd_loadav[0]) / 100.0, ntohl(w->wd_loadav[1]) / 100.0,
698185377Ssam	    ntohl(w->wd_loadav[2]) / 100.0);
699185377Ssam	cc -= WHDRSIZE;
700185377Ssam	for (we = w->wd_we, cc /= sizeof(struct whoent); cc > 0; cc--, we++) {
701185377Ssam		time_t t = _time32_to_time(ntohl(we->we_utmp.out_time));
702185377Ssam		printf("%-8.8s %s:%s %.12s",
703185377Ssam			we->we_utmp.out_name,
704185377Ssam			w->wd_hostname, we->we_utmp.out_line,
705185377Ssam			ctime(&t)+4);
706185377Ssam		we->we_idle = ntohl(we->we_idle) / 60;
707185377Ssam		if (we->we_idle) {
708185380Ssam			if (we->we_idle >= 100*60)
709185380Ssam				we->we_idle = 100*60 - 1;
710185380Ssam			if (we->we_idle >= 60)
711185377Ssam				printf(" %2d", we->we_idle / 60);
712185377Ssam			else
713185377Ssam				printf("   ");
714185377Ssam			printf(":%02d", we->we_idle % 60);
715185377Ssam		}
716185377Ssam		printf("\n");
717185377Ssam	}
718185377Ssam}
719185377Ssam
720185377Ssamchar *
721185377Ssaminterval(time, updown)
722185377Ssam	int time;
723185377Ssam	char *updown;
724185377Ssam{
725185377Ssam	static char resbuf[32];
726185380Ssam	int days, hours, minutes;
727185380Ssam
728185377Ssam	if (time < 0 || time > 3*30*24*60*60) {
729185377Ssam		(void) sprintf(resbuf, "   %s ??:??", updown);
730185377Ssam		return (resbuf);
731185377Ssam	}
732185377Ssam	minutes = (time + 59) / 60;		/* round to minutes */
733185377Ssam	hours = minutes / 60; minutes %= 60;
734185377Ssam	days = hours / 24; hours %= 24;
735185377Ssam	if (days)
736185377Ssam		(void) sprintf(resbuf, "%s %2d+%02d:%02d",
737185380Ssam		    updown, days, hours, minutes);
738185380Ssam	else
739185377Ssam		(void) sprintf(resbuf, "%s    %2d:%02d",
740185377Ssam		    updown, hours, minutes);
741185377Ssam	return (resbuf);
742185377Ssam}
743185377Ssam#endif
744185377Ssam