xref: /netbsd/usr.sbin/rwhod/rwhod.c (revision fc1f8942)
1*fc1f8942Skre /*	$NetBSD: rwhod.c,v 1.41 2018/10/30 21:15:09 kre Exp $	*/
2f17eeccaSchristos 
3e541169cScgd /*
49afcf17bSjtc  * Copyright (c) 1983, 1993
59afcf17bSjtc  *	The Regents of the University of California.  All rights reserved.
6e541169cScgd  *
7e541169cScgd  * Redistribution and use in source and binary forms, with or without
8e541169cScgd  * modification, are permitted provided that the following conditions
9e541169cScgd  * are met:
10e541169cScgd  * 1. Redistributions of source code must retain the above copyright
11e541169cScgd  *    notice, this list of conditions and the following disclaimer.
12e541169cScgd  * 2. Redistributions in binary form must reproduce the above copyright
13e541169cScgd  *    notice, this list of conditions and the following disclaimer in the
14e541169cScgd  *    documentation and/or other materials provided with the distribution.
15326b2259Sagc  * 3. Neither the name of the University nor the names of its contributors
16e541169cScgd  *    may be used to endorse or promote products derived from this software
17e541169cScgd  *    without specific prior written permission.
18e541169cScgd  *
19e541169cScgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20e541169cScgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21e541169cScgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22e541169cScgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23e541169cScgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24e541169cScgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25e541169cScgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26e541169cScgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27e541169cScgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28e541169cScgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29e541169cScgd  * SUCH DAMAGE.
30e541169cScgd  */
31e541169cScgd 
328d79db10Slukem #include <sys/cdefs.h>
33e541169cScgd #ifndef lint
349c194566Slukem __COPYRIGHT("@(#) Copyright (c) 1983, 1993\
359c194566Slukem  The Regents of the University of California.  All rights reserved.");
36e541169cScgd #endif /* not lint */
37e541169cScgd 
38e541169cScgd #ifndef lint
398d79db10Slukem #if 0
408d79db10Slukem static char sccsid[] = "@(#)rwhod.c	8.1 (Berkeley) 6/6/93";
418d79db10Slukem #else
42*fc1f8942Skre __RCSID("$NetBSD: rwhod.c,v 1.41 2018/10/30 21:15:09 kre Exp $");
438d79db10Slukem #endif
44e541169cScgd #endif /* not lint */
45e541169cScgd 
46e541169cScgd #include <sys/param.h>
47e541169cScgd #include <sys/socket.h>
48e541169cScgd #include <sys/stat.h>
49e541169cScgd #include <sys/signal.h>
50e541169cScgd #include <sys/ioctl.h>
519afcf17bSjtc #include <sys/sysctl.h>
52e541169cScgd 
53e541169cScgd #include <net/if.h>
549afcf17bSjtc #include <net/if_dl.h>
559afcf17bSjtc #include <net/route.h>
56e541169cScgd #include <netinet/in.h>
57e541169cScgd #include <protocols/rwhod.h>
588d79db10Slukem #include <arpa/inet.h>
599afcf17bSjtc 
609afcf17bSjtc #include <ctype.h>
6145502454Schristos #include <pwd.h>
62e5d6d67cSlukem #include <err.h>
639afcf17bSjtc #include <errno.h>
649afcf17bSjtc #include <fcntl.h>
659afcf17bSjtc #include <netdb.h>
66e541169cScgd #include <paths.h>
67f17eeccaSchristos #include <poll.h>
689afcf17bSjtc #include <stdio.h>
699afcf17bSjtc #include <stdlib.h>
709afcf17bSjtc #include <string.h>
719afcf17bSjtc #include <syslog.h>
729afcf17bSjtc #include <unistd.h>
73bcd46591Sthorpej #include <util.h>
74e541169cScgd 
75b166b5b4Schristos #include "utmpentry.h"
766b7013d1Schristos 
77c14c7a6bSpeter #define CHECK_INTERVAL (3 * 60)
78e541169cScgd 
796b7013d1Schristos /* Time interval limit; ruptime will think that we are down > than this */
806b7013d1Schristos #define MAX_INTERVAL (11 * 60)
816b7013d1Schristos 
826b7013d1Schristos 
83f17eeccaSchristos static char	myname[MAXHOSTNAMELEN + 1];
84e541169cScgd 
85e541169cScgd /*
869afcf17bSjtc  * We communicate with each neighbor in a list constructed at the time we're
879afcf17bSjtc  * started up.  Neighbors are currently directly connected via a hardware
889afcf17bSjtc  * interface.
89e541169cScgd  */
90e541169cScgd struct neighbor {
91e541169cScgd 	struct	neighbor *n_next;
92e541169cScgd 	char	*n_name;		/* interface name */
939afcf17bSjtc 	struct	sockaddr *n_addr;	/* who to send to */
94e541169cScgd 	int	n_addrlen;		/* size of address */
95e541169cScgd 	int	n_flags;		/* should forward?, interface flags */
96e541169cScgd };
97e541169cScgd 
98f17eeccaSchristos static struct	neighbor *neighbors;
99f17eeccaSchristos static struct	whod mywd;
100f17eeccaSchristos static struct	servent *sp;
101f17eeccaSchristos static volatile sig_atomic_t  onsighup;
102e541169cScgd 
103e541169cScgd #define	WHDRSIZE	(sizeof(mywd) - sizeof(mywd.wd_we))
104e541169cScgd 
105f17eeccaSchristos static int	 configure(int);
106f17eeccaSchristos static void	 getboottime(void);
107f17eeccaSchristos static void	 send_host_information(int);
108c14c7a6bSpeter static void	 sighup(int);
109f17eeccaSchristos static void	 handleread(int);
1107f26fba0Sjoerg __dead static void	 quit(const char *);
111f17eeccaSchristos static void	 rt_xaddrs(void *, void *, struct rt_addrinfo *);
11204414a37Stsarna static int	 drop_privs(char *);
1138b0f9554Sperry static void	 usage(void) __dead;
114f17eeccaSchristos static int	 verify(const char *);
1159afcf17bSjtc #ifdef DEBUG
116f17eeccaSchristos static char	*interval(int, const char *);
117f17eeccaSchristos static ssize_t	 Sendto(int, const void *, size_t, int,
118f17eeccaSchristos     const struct sockaddr *, socklen_t);
119cb1d6897Schristos #else
120cb1d6897Schristos #define	 Sendto sendto
1219afcf17bSjtc #endif
122e541169cScgd 
1239afcf17bSjtc int
main(int argc,char * argv[])124f17eeccaSchristos main(int argc, char *argv[])
125e541169cScgd {
126c36c99efSchristos 	int s, ch;
127c36c99efSchristos 	int time_interval = 180;	/* Default time (180 seconds) */
128c36c99efSchristos 	char *cp, *ep;
129c14c7a6bSpeter 	socklen_t on = 1;
130f17eeccaSchristos 	struct sockaddr_in sasin;
131f17eeccaSchristos 	struct pollfd pfd[1];
132c14c7a6bSpeter 	struct timeval delta, next, now;
13304414a37Stsarna 	char *newuser = NULL;
134e541169cScgd 
135c36c99efSchristos 	setprogname(argv[0]);
136c36c99efSchristos 
137e5d6d67cSlukem 	if (getuid())
138c14c7a6bSpeter 		errx(EXIT_FAILURE, "not super user");
139c36c99efSchristos 
14004414a37Stsarna 	while ((ch = getopt(argc, argv, "i:u:")) != -1) {
141c36c99efSchristos 		switch (ch) {
1426b7013d1Schristos 		case 'i':
143c36c99efSchristos 			time_interval = (int)strtol(optarg, &ep, 10);
144c36c99efSchristos 
145c36c99efSchristos 			switch (*ep) {
146c36c99efSchristos 			case '\0':
147c36c99efSchristos 				break;
148c36c99efSchristos 			case 'm':
149c36c99efSchristos 			case 'M':
150c36c99efSchristos 				/* Time in minutes. */
151c36c99efSchristos 				time_interval *= 60;
152c36c99efSchristos 				if (ep[1] == '\0')
153c36c99efSchristos 					break;
154c36c99efSchristos 				/*FALLTHROUGH*/
155c36c99efSchristos 			default:
156c36c99efSchristos 				errx(1, "Invalid argument: `%s'", optarg);
157c36c99efSchristos 			}
158c36c99efSchristos 
159c36c99efSchristos 			if (time_interval <= 0)
1606b7013d1Schristos 				errx(1, "Interval must be greater than 0");
161c36c99efSchristos 
1626b7013d1Schristos 			if (time_interval > MAX_INTERVAL)
1636b7013d1Schristos 				errx(1, "Interval cannot be greater than"
1646b7013d1Schristos 				    " %d minutes", MAX_INTERVAL / 60);
165c36c99efSchristos 			break;
16604414a37Stsarna 
16704414a37Stsarna 		case 'u':
16804414a37Stsarna 			newuser = optarg;
16904414a37Stsarna 			break;
17004414a37Stsarna 
171c36c99efSchristos 		default:
172c36c99efSchristos 			usage();
173c36c99efSchristos 		}
174c36c99efSchristos 	}
175c36c99efSchristos 
176e541169cScgd 	sp = getservbyname("who", "udp");
177e5d6d67cSlukem 	if (sp == NULL)
178c14c7a6bSpeter 		errx(EXIT_FAILURE, "udp/who: unknown service");
179e541169cScgd #ifndef DEBUG
180c14c7a6bSpeter 	(void)daemon(1, 0);
181c14c7a6bSpeter 	(void)pidfile(NULL);
182e541169cScgd #endif
183e5d6d67cSlukem 	if (chdir(_PATH_RWHODIR) < 0)
184c14c7a6bSpeter 		err(EXIT_FAILURE, "%s", _PATH_RWHODIR);
185c14c7a6bSpeter 	(void)signal(SIGHUP, sighup);
186cb1d6897Schristos 	openlog(getprogname(), LOG_PID, LOG_DAEMON);
187e541169cScgd 	/*
188e541169cScgd 	 * Establish host name as returned by system.
189e541169cScgd 	 */
190e541169cScgd 	if (gethostname(myname, sizeof(myname) - 1) < 0) {
191e541169cScgd 		syslog(LOG_ERR, "gethostname: %m");
192c14c7a6bSpeter 		exit(EXIT_FAILURE);
193e541169cScgd 	}
19432f51971Smrg 	myname[sizeof(myname) - 1] = '\0';
195e5d6d67cSlukem 	if ((cp = strchr(myname, '.')) != NULL)
196e541169cScgd 		*cp = '\0';
197f17eeccaSchristos 	(void)strncpy(mywd.wd_hostname, myname, sizeof(mywd.wd_hostname) - 1);
198f17eeccaSchristos 	getboottime();
199e541169cScgd 	if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
200e541169cScgd 		syslog(LOG_ERR, "socket: %m");
201c14c7a6bSpeter 		exit(EXIT_FAILURE);
202e541169cScgd 	}
203e541169cScgd 	if (setsockopt(s, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on)) < 0) {
204e541169cScgd 		syslog(LOG_ERR, "setsockopt SO_BROADCAST: %m");
205c14c7a6bSpeter 		exit(EXIT_FAILURE);
206e541169cScgd 	}
207f17eeccaSchristos 	(void)memset(&sasin, 0, sizeof(sasin));
208f17eeccaSchristos 	sasin.sin_family = AF_INET;
209f17eeccaSchristos 	sasin.sin_port = sp->s_port;
210f17eeccaSchristos 	if (bind(s, (struct sockaddr *)&sasin, sizeof(sasin)) < 0) {
211e541169cScgd 		syslog(LOG_ERR, "bind: %m");
212c14c7a6bSpeter 		exit(EXIT_FAILURE);
213e541169cScgd 	}
214e541169cScgd 	if (!configure(s))
215c14c7a6bSpeter 		exit(EXIT_FAILURE);
216f17eeccaSchristos 
21704414a37Stsarna 	if (newuser)
21804414a37Stsarna 		if (!drop_privs(newuser))
21904414a37Stsarna 			exit(EXIT_FAILURE);
22004414a37Stsarna 
221f17eeccaSchristos 	send_host_information(s);
222c36c99efSchristos 	delta.tv_sec = time_interval;
223c14c7a6bSpeter 	delta.tv_usec = 0;
224f17eeccaSchristos 	gettimeofday(&now, NULL);
225c14c7a6bSpeter 	timeradd(&now, &delta, &next);
226f17eeccaSchristos 
227f17eeccaSchristos 	pfd[0].fd = s;
228a8b658e3Schristos 	pfd[0].events = POLLIN;
229f17eeccaSchristos 
230e541169cScgd 	for (;;) {
231f17eeccaSchristos 		int n;
232f17eeccaSchristos 
233f17eeccaSchristos 		n = poll(pfd, 1, 1000);
234f17eeccaSchristos 
235f17eeccaSchristos 		if (onsighup) {
236f17eeccaSchristos 			onsighup = 0;
237f17eeccaSchristos 			getboottime();
238f17eeccaSchristos 		}
239f17eeccaSchristos 
240f17eeccaSchristos 		if (n == 1)
241f17eeccaSchristos 			handleread(s);
242f17eeccaSchristos 
243f17eeccaSchristos 		(void)gettimeofday(&now, NULL);
244c14c7a6bSpeter 		if (timercmp(&now, &next, >)) {
245f17eeccaSchristos 			send_host_information(s);
246c14c7a6bSpeter 			timeradd(&now, &delta, &next);
247f17eeccaSchristos 		}
248f17eeccaSchristos 	}
249f17eeccaSchristos 
250c14c7a6bSpeter 	/* NOTREACHED */
251c14c7a6bSpeter 	return 0;
252c14c7a6bSpeter }
253c14c7a6bSpeter 
254f17eeccaSchristos static void
sighup(int signo __unused)255c14c7a6bSpeter sighup(int signo __unused)
256f17eeccaSchristos {
257f17eeccaSchristos 	onsighup = 1;
258f17eeccaSchristos }
259f17eeccaSchristos 
260f17eeccaSchristos static void
handleread(int s)261f17eeccaSchristos handleread(int s)
262f17eeccaSchristos {
263f17eeccaSchristos 	struct sockaddr_in from;
264f17eeccaSchristos 	struct stat st;
265f17eeccaSchristos 	char path[64];
266e541169cScgd 	struct whod wd;
267f17eeccaSchristos 	int cc, whod;
268f17eeccaSchristos 	socklen_t len = sizeof(from);
269e541169cScgd 
270e541169cScgd 	cc = recvfrom(s, (char *)&wd, sizeof(struct whod), 0,
271e541169cScgd 		(struct sockaddr *)&from, &len);
272e541169cScgd 	if (cc <= 0) {
273e541169cScgd 		if (cc < 0 && errno != EINTR)
274e541169cScgd 			syslog(LOG_WARNING, "recv: %m");
275f17eeccaSchristos 		return;
276e541169cScgd 	}
277e541169cScgd 	if (from.sin_port != sp->s_port) {
278e541169cScgd 		syslog(LOG_WARNING, "%d: bad from port",
279e541169cScgd 			ntohs(from.sin_port));
280f17eeccaSchristos 		return;
281e541169cScgd 	}
282e3823f58Slukem 	if (cc < (int)WHDRSIZE) {
283f214fa32Smjl 		syslog(LOG_WARNING, "Short packet from %s",
284f214fa32Smjl 			inet_ntoa(from.sin_addr));
285f17eeccaSchristos 		return;
286f214fa32Smjl 	}
287f214fa32Smjl 
288e541169cScgd 	if (wd.wd_vers != WHODVERSION)
289f17eeccaSchristos 		return;
290e541169cScgd 	if (wd.wd_type != WHODTYPE_STATUS)
291f17eeccaSchristos 		return;
292d3a4eeb1Sexplorer 	/*
293d3a4eeb1Sexplorer 	 * Ensure null termination of the name within the packet.
294d3a4eeb1Sexplorer 	 * Otherwise we might overflow or read past the end.
295d3a4eeb1Sexplorer 	 */
296d3a4eeb1Sexplorer 	wd.wd_hostname[sizeof(wd.wd_hostname)-1] = 0;
297e541169cScgd 	if (!verify(wd.wd_hostname)) {
2988d79db10Slukem 		syslog(LOG_WARNING, "malformed host name from %s",
2998d79db10Slukem 		    inet_ntoa(from.sin_addr));
300f17eeccaSchristos 		return;
301e541169cScgd 	}
302f17eeccaSchristos 	(void)snprintf(path, sizeof(path), "whod.%s", wd.wd_hostname);
303e541169cScgd 	/*
304e541169cScgd 	 * Rather than truncating and growing the file each time,
305e541169cScgd 	 * use ftruncate if size is less than previous size.
306e541169cScgd 	 */
307e541169cScgd 	whod = open(path, O_WRONLY | O_CREAT, 0644);
308e541169cScgd 	if (whod < 0) {
309e541169cScgd 		syslog(LOG_WARNING, "%s: %m", path);
310f17eeccaSchristos 		return;
311e541169cScgd 	}
312e541169cScgd #if ENDIAN != BIG_ENDIAN
313e541169cScgd 	{
314e541169cScgd 		int i, n = (cc - WHDRSIZE) / sizeof(struct whoent);
315e541169cScgd 		struct whoent *we;
316e541169cScgd 
317e541169cScgd 		/* undo header byte swapping before writing to file */
318e541169cScgd 		wd.wd_sendtime = ntohl(wd.wd_sendtime);
319e541169cScgd 		for (i = 0; i < 3; i++)
320e541169cScgd 			wd.wd_loadav[i] = ntohl(wd.wd_loadav[i]);
321e541169cScgd 		wd.wd_boottime = ntohl(wd.wd_boottime);
322e541169cScgd 		we = wd.wd_we;
323e541169cScgd 		for (i = 0; i < n; i++) {
324e541169cScgd 			we->we_idle = ntohl(we->we_idle);
325e541169cScgd 			we->we_utmp.out_time =
326e541169cScgd 			    ntohl(we->we_utmp.out_time);
327e541169cScgd 			we++;
328e541169cScgd 		}
329e541169cScgd 	}
330e541169cScgd #endif
331c48f3941Smrg 	wd.wd_recvtime = time(NULL);
332e541169cScgd 	(void)write(whod, (char *)&wd, cc);
333e541169cScgd 	if (fstat(whod, &st) < 0 || st.st_size > cc)
334f17eeccaSchristos 		(void)ftruncate(whod, cc);
335e541169cScgd 	(void)close(whod);
336e541169cScgd }
337e541169cScgd 
338e541169cScgd /*
339e541169cScgd  * Check out host name for unprintables
340e541169cScgd  * and other funnies before allowing a file
341e541169cScgd  * to be created.  Sorry, but blanks aren't allowed.
342e541169cScgd  */
343f17eeccaSchristos static int
verify(const char * name)344f17eeccaSchristos verify(const char *name)
345e541169cScgd {
346e5d6d67cSlukem 	int size = 0;
347e541169cScgd 
348e541169cScgd 	while (*name) {
349f17eeccaSchristos 		if (!isascii((unsigned char)*name) ||
350f17eeccaSchristos 		    !(isalnum((unsigned char)*name) ||
3519122339bSdsl 		    ispunct((unsigned char)*name)))
352f17eeccaSchristos 			return 0;
353e541169cScgd 		name++, size++;
354e541169cScgd 	}
355f17eeccaSchristos 	return size > 0;
356e541169cScgd }
357e541169cScgd 
358f17eeccaSchristos static void
send_host_information(int s)359f17eeccaSchristos send_host_information(int s)
360e541169cScgd {
361e5d6d67cSlukem 	struct neighbor *np;
362e5d6d67cSlukem 	struct whoent *we = mywd.wd_we, *wlast;
363632e7ed5Sjunyoung 	int i, cc, utmpent = 0;
364e541169cScgd 	struct stat stb;
365e541169cScgd 	double avenrun[3];
3669afcf17bSjtc 	time_t now;
367b166b5b4Schristos 	static struct utmpentry *ohead = NULL;
368b166b5b4Schristos 	struct utmpentry *ep;
369c14c7a6bSpeter 	static int count = 0;
370e541169cScgd 
3719afcf17bSjtc 	now = time(NULL);
372c14c7a6bSpeter 	if (count % 10 == 0)
373f17eeccaSchristos 		getboottime();
374c14c7a6bSpeter 	count++;
375b166b5b4Schristos 
376b166b5b4Schristos 	(void)getutentries(NULL, &ep);
37716e3bec2Sdholland 	/* XXX probably should expose utmp mtime, check that instead */
378b166b5b4Schristos 	if (ep != ohead) {
379e541169cScgd 		wlast = &mywd.wd_we[1024 / sizeof(struct whoent) - 1];
380b166b5b4Schristos 		for (; ep; ep = ep->next) {
381b166b5b4Schristos 			(void)strncpy(we->we_utmp.out_line, ep->line,
38218b5fc9eSchristos 			    sizeof(we->we_utmp.out_line));
383b166b5b4Schristos 			(void)strncpy(we->we_utmp.out_name, ep->name,
38418b5fc9eSchristos 			    sizeof(we->we_utmp.out_name));
385b166b5b4Schristos 			we->we_utmp.out_time = htonl(ep->tv.tv_sec);
386e541169cScgd 			if (we >= wlast)
387e541169cScgd 				break;
388e541169cScgd 			we++;
389e541169cScgd 		}
390e541169cScgd 		utmpent = we - mywd.wd_we;
391e541169cScgd 	}
392e541169cScgd 
393e541169cScgd 	/*
394e541169cScgd 	 * The test on utmpent looks silly---after all, if no one is
395e541169cScgd 	 * logged on, why worry about efficiency?---but is useful on
396e541169cScgd 	 * (e.g.) compute servers.
397e541169cScgd 	 */
398e541169cScgd 	if (utmpent && chdir(_PATH_DEV)) {
399e541169cScgd 		syslog(LOG_ERR, "chdir(%s): %m", _PATH_DEV);
400c14c7a6bSpeter 		exit(EXIT_FAILURE);
401e541169cScgd 	}
402e541169cScgd 	we = mywd.wd_we;
403e541169cScgd 	for (i = 0; i < utmpent; i++) {
404e541169cScgd 		if (stat(we->we_utmp.out_line, &stb) >= 0)
405e541169cScgd 			we->we_idle = htonl(now - stb.st_atime);
406e541169cScgd 		we++;
407e541169cScgd 	}
408e541169cScgd 	(void)getloadavg(avenrun, sizeof(avenrun)/sizeof(avenrun[0]));
409e541169cScgd 	for (i = 0; i < 3; i++)
410e541169cScgd 		mywd.wd_loadav[i] = htonl((u_long)(avenrun[i] * 100));
411e541169cScgd 	cc = (char *)we - (char *)&mywd;
412e541169cScgd 	mywd.wd_sendtime = htonl(time(0));
413e541169cScgd 	mywd.wd_vers = WHODVERSION;
414e541169cScgd 	mywd.wd_type = WHODTYPE_STATUS;
415e541169cScgd 	for (np = neighbors; np != NULL; np = np->n_next)
416cb1d6897Schristos 		(void)Sendto(s, (char *)&mywd, cc, 0,
4179afcf17bSjtc 				np->n_addr, np->n_addrlen);
418e541169cScgd 	if (utmpent && chdir(_PATH_RWHODIR)) {
419e541169cScgd 		syslog(LOG_ERR, "chdir(%s): %m", _PATH_RWHODIR);
420c14c7a6bSpeter 		exit(EXIT_FAILURE);
421e541169cScgd 	}
422e541169cScgd }
423e541169cScgd 
424f17eeccaSchristos static void
getboottime(void)425f17eeccaSchristos getboottime(void)
426e541169cScgd {
4279afcf17bSjtc 	int mib[2];
4289afcf17bSjtc 	size_t size;
429*fc1f8942Skre 	struct timespec tm;
430e541169cScgd 
4319afcf17bSjtc 	mib[0] = CTL_KERN;
4329afcf17bSjtc 	mib[1] = KERN_BOOTTIME;
4339afcf17bSjtc 	size = sizeof(tm);
4349afcf17bSjtc 	if (sysctl(mib, 2, &tm, &size, NULL, 0) == -1) {
4359afcf17bSjtc 		syslog(LOG_ERR, "cannot get boottime: %m");
436c14c7a6bSpeter 		exit(EXIT_FAILURE);
4379afcf17bSjtc 	}
4389afcf17bSjtc 	mywd.wd_boottime = htonl(tm.tv_sec);
439e541169cScgd }
440b55bc39bSandrew 
441f17eeccaSchristos static void
quit(const char * msg)442f17eeccaSchristos quit(const char *msg)
4439afcf17bSjtc {
444d8302e2dSis 	syslog(LOG_ERR, "%s", msg);
445c14c7a6bSpeter 	exit(EXIT_FAILURE);
446e541169cScgd }
4479afcf17bSjtc 
4489afcf17bSjtc #define ROUNDUP(a) \
4499afcf17bSjtc 	((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
450f17eeccaSchristos #define ADVANCE(x, n) ((char *)(x) + ROUNDUP((n)->sa_len))
4519afcf17bSjtc 
452f17eeccaSchristos static void
rt_xaddrs(void * cp,void * cplim,struct rt_addrinfo * rtinfo)453f17eeccaSchristos rt_xaddrs(void *cp, void *cplim, struct rt_addrinfo *rtinfo)
4549afcf17bSjtc {
455e5d6d67cSlukem 	struct sockaddr *sa;
456e5d6d67cSlukem 	int i;
4579afcf17bSjtc 
458f17eeccaSchristos 	(void)memset(rtinfo->rti_info, 0, sizeof(rtinfo->rti_info));
4599afcf17bSjtc 	for (i = 0; (i < RTAX_MAX) && (cp < cplim); i++) {
4609afcf17bSjtc 		if ((rtinfo->rti_addrs & (1 << i)) == 0)
4619afcf17bSjtc 			continue;
4629afcf17bSjtc 		rtinfo->rti_info[i] = sa = (struct sockaddr *)cp;
463f17eeccaSchristos 		cp = ADVANCE(cp, sa);
4649afcf17bSjtc 	}
465e541169cScgd }
466e541169cScgd 
467e541169cScgd /*
468e541169cScgd  * Figure out device configuration and select
469e541169cScgd  * networks which deserve status information.
470e541169cScgd  */
471f17eeccaSchristos static int
configure(int s)472f17eeccaSchristos configure(int s)
473e541169cScgd {
474e5d6d67cSlukem 	struct neighbor *np;
475e5d6d67cSlukem 	struct if_msghdr *ifm;
476e5d6d67cSlukem 	struct ifa_msghdr *ifam;
4779afcf17bSjtc 	struct sockaddr_dl *sdl;
4789afcf17bSjtc 	size_t needed;
4799afcf17bSjtc 	int mib[6], flags = 0, len;
4809afcf17bSjtc 	char *buf, *lim, *next;
4819afcf17bSjtc 	struct rt_addrinfo info;
482f17eeccaSchristos 	struct sockaddr_in dstaddr;
483e541169cScgd 
4849afcf17bSjtc 	mib[0] = CTL_NET;
4859afcf17bSjtc 	mib[1] = PF_ROUTE;
4869afcf17bSjtc 	mib[2] = 0;
4879afcf17bSjtc 	mib[3] = AF_INET;
4889afcf17bSjtc 	mib[4] = NET_RT_IFLIST;
4899afcf17bSjtc 	mib[5] = 0;
4909afcf17bSjtc 	if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
4919afcf17bSjtc 		quit("route-sysctl-estimate");
4929afcf17bSjtc 	if ((buf = malloc(needed)) == NULL)
4939afcf17bSjtc 		quit("malloc");
4949afcf17bSjtc 	if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
4959afcf17bSjtc 		quit("actual retrieval of interface table");
4969afcf17bSjtc 	lim = buf + needed;
4979afcf17bSjtc 
4989afcf17bSjtc 	sdl = NULL;		/* XXX just to keep gcc -Wall happy */
4999afcf17bSjtc 	for (next = buf; next < lim; next += ifm->ifm_msglen) {
5009afcf17bSjtc 		ifm = (struct if_msghdr *)next;
5019afcf17bSjtc 		if (ifm->ifm_type == RTM_IFINFO) {
5029afcf17bSjtc 			sdl = (struct sockaddr_dl *)(ifm + 1);
5039afcf17bSjtc 			flags = ifm->ifm_flags;
5049afcf17bSjtc 			continue;
505e541169cScgd 		}
5069afcf17bSjtc 		if ((flags & IFF_UP) == 0 ||
5079afcf17bSjtc 		    (flags & (IFF_BROADCAST|IFF_POINTOPOINT)) == 0)
5089afcf17bSjtc 			continue;
5099afcf17bSjtc 		if (ifm->ifm_type != RTM_NEWADDR)
5109afcf17bSjtc 			quit("out of sync parsing NET_RT_IFLIST");
5119afcf17bSjtc 		ifam = (struct ifa_msghdr *)ifm;
5129afcf17bSjtc 		info.rti_addrs = ifam->ifam_addrs;
513f17eeccaSchristos 		rt_xaddrs((ifam + 1), ifam->ifam_msglen + (char *)ifam, &info);
5149afcf17bSjtc 		/* gag, wish we could get rid of Internet dependencies */
515f17eeccaSchristos 		if (info.rti_info[RTAX_BRD] == NULL ||
516f17eeccaSchristos 		    info.rti_info[RTAX_BRD]->sa_family != AF_INET)
517f17eeccaSchristos 			continue;
518f17eeccaSchristos 		(void)memcpy(&dstaddr, info.rti_info[RTAX_BRD],
519f17eeccaSchristos 		    sizeof(dstaddr));
5209afcf17bSjtc #define IPADDR_SA(x) ((struct sockaddr_in *)(x))->sin_addr.s_addr
5219afcf17bSjtc #define PORT_SA(x) ((struct sockaddr_in *)(x))->sin_port
522f17eeccaSchristos 		PORT_SA(&dstaddr) = sp->s_port;
523e541169cScgd 		for (np = neighbors; np != NULL; np = np->n_next)
5249afcf17bSjtc 			if (memcmp(sdl->sdl_data, np->n_name,
5259afcf17bSjtc 				   sdl->sdl_nlen) == 0 &&
526f17eeccaSchristos 			    IPADDR_SA(np->n_addr) == IPADDR_SA(&dstaddr))
527e541169cScgd 				break;
528e541169cScgd 		if (np != NULL)
529e541169cScgd 			continue;
530f17eeccaSchristos 		len = sizeof(*np) + dstaddr.sin_len + sdl->sdl_nlen + 1;
5319afcf17bSjtc 		np = (struct neighbor *)malloc(len);
532e541169cScgd 		if (np == NULL)
5339afcf17bSjtc 			quit("malloc of neighbor structure");
534f17eeccaSchristos 		(void)memset(np, 0, len);
5359afcf17bSjtc 		np->n_flags = flags;
5369afcf17bSjtc 		np->n_addr = (struct sockaddr *)(np + 1);
537f17eeccaSchristos 		np->n_addrlen = dstaddr.sin_len;
5389afcf17bSjtc 		np->n_name = np->n_addrlen + (char *)np->n_addr;
539e541169cScgd 		np->n_next = neighbors;
540e541169cScgd 		neighbors = np;
541f17eeccaSchristos 		(void)memcpy(np->n_addr, &dstaddr, np->n_addrlen);
542f17eeccaSchristos 		(void)memcpy(np->n_name, sdl->sdl_data, sdl->sdl_nlen);
543e541169cScgd 	}
5449afcf17bSjtc 	free(buf);
545e541169cScgd 	return (1);
546e541169cScgd }
547e541169cScgd 
548e541169cScgd #ifdef DEBUG
549f17eeccaSchristos static ssize_t
Sendto(int s,const void * buf,size_t cc,int flags,const struct sockaddr * to,socklen_t tolen)550f17eeccaSchristos Sendto(int s, const void *buf, size_t cc, int flags, const struct sockaddr *to,
551f17eeccaSchristos     socklen_t tolen)
552e541169cScgd {
553e5d6d67cSlukem 	struct whod *w = (struct whod *)buf;
554e5d6d67cSlukem 	struct whoent *we;
555f17eeccaSchristos 	struct sockaddr_in *sasin = (struct sockaddr_in *)to;
556cb1d6897Schristos 	ssize_t ret;
557cb1d6897Schristos 
558cb1d6897Schristos 	ret = sendto(s, buf, cc, flags, to, tolen);
559e541169cScgd 
560c36c99efSchristos 	printf("sendto %s.%d\n", inet_ntoa(sasin->sin_addr),
561f17eeccaSchristos 	    ntohs(sasin->sin_port));
562e541169cScgd 	printf("hostname %s %s\n", w->wd_hostname,
563e541169cScgd 	   interval(ntohl(w->wd_sendtime) - ntohl(w->wd_boottime), "  up"));
564e541169cScgd 	printf("load %4.2f, %4.2f, %4.2f\n",
565e541169cScgd 	    ntohl(w->wd_loadav[0]) / 100.0, ntohl(w->wd_loadav[1]) / 100.0,
566e541169cScgd 	    ntohl(w->wd_loadav[2]) / 100.0);
567e541169cScgd 	cc -= WHDRSIZE;
568e541169cScgd 	for (we = w->wd_we, cc /= sizeof(struct whoent); cc > 0; cc--, we++) {
569e541169cScgd 		time_t t = ntohl(we->we_utmp.out_time);
57050455a91Schristos 		printf("%-8.8s %s:%s %.12s", we->we_utmp.out_name,
57150455a91Schristos 		    w->wd_hostname, we->we_utmp.out_line, ctime(&t)+4);
572e541169cScgd 		we->we_idle = ntohl(we->we_idle) / 60;
573e541169cScgd 		if (we->we_idle) {
574e541169cScgd 			if (we->we_idle >= 100*60)
575e541169cScgd 				we->we_idle = 100*60 - 1;
576e541169cScgd 			if (we->we_idle >= 60)
577e541169cScgd 				printf(" %2d", we->we_idle / 60);
578e541169cScgd 			else
579e541169cScgd 				printf("   ");
580e541169cScgd 			printf(":%02d", we->we_idle % 60);
581e541169cScgd 		}
582e541169cScgd 		printf("\n");
583e541169cScgd 	}
584cb1d6897Schristos 	return ret;
585e541169cScgd }
586e541169cScgd 
587f17eeccaSchristos static char *
interval(int time,const char * updown)588f17eeccaSchristos interval(int time, const char *updown)
589e541169cScgd {
590e541169cScgd 	static char resbuf[32];
591e541169cScgd 	int days, hours, minutes;
592e541169cScgd 
593e541169cScgd 	if (time < 0 || time > 3*30*24*60*60) {
594d9f2774cSitojun 		(void)snprintf(resbuf, sizeof(resbuf), "   %s ??:??", updown);
595e541169cScgd 		return (resbuf);
596e541169cScgd 	}
597e541169cScgd 	minutes = (time + 59) / 60;		/* round to minutes */
598e541169cScgd 	hours = minutes / 60; minutes %= 60;
599e541169cScgd 	days = hours / 24; hours %= 24;
600e541169cScgd 	if (days)
601d9f2774cSitojun 		(void)snprintf(resbuf, sizeof(resbuf), "%s %2d+%02d:%02d",
602e541169cScgd 		    updown, days, hours, minutes);
603e541169cScgd 	else
604d9f2774cSitojun 		(void)snprintf(resbuf, sizeof(resbuf), "%s    %2d:%02d",
605e541169cScgd 		    updown, hours, minutes);
606f17eeccaSchristos 	return resbuf;
607e541169cScgd }
608e541169cScgd #endif
609c36c99efSchristos 
61004414a37Stsarna static int
drop_privs(char * newuser)61104414a37Stsarna drop_privs(char *newuser)
61204414a37Stsarna {
61304414a37Stsarna 	struct passwd *pw;
61404414a37Stsarna 	gid_t gidset[1];
61504414a37Stsarna 
61604414a37Stsarna 	pw = getpwnam(newuser);
61704414a37Stsarna 	if (pw == NULL) {
61804414a37Stsarna 		syslog(LOG_ERR, "no user %.100s", newuser);
61904414a37Stsarna 		return 0;
62004414a37Stsarna 	}
62104414a37Stsarna 
62204414a37Stsarna 	endpwent();
62304414a37Stsarna 
62404414a37Stsarna 	gidset[0] = pw->pw_gid;
62504414a37Stsarna 	if (setgroups(1, gidset) == -1) {
62604414a37Stsarna 		syslog(LOG_ERR, "setgroups: %m");
62704414a37Stsarna 		return 0;
62804414a37Stsarna 	}
62904414a37Stsarna 
63004414a37Stsarna 	if (setgid(pw->pw_gid) == -1) {
63104414a37Stsarna 		syslog(LOG_ERR, "setgid: %m");
63204414a37Stsarna 		return 0;
63304414a37Stsarna 	}
63404414a37Stsarna 
63504414a37Stsarna 	if (setuid(pw->pw_uid) == -1) {
63604414a37Stsarna 		syslog(LOG_ERR, "setuid: %m");
63704414a37Stsarna 		return 0;
63804414a37Stsarna 	}
63904414a37Stsarna 
64004414a37Stsarna 	return 1;
64104414a37Stsarna }
64204414a37Stsarna 
643c36c99efSchristos static void
usage(void)644c36c99efSchristos usage(void)
645c36c99efSchristos {
6466438e0bfSwiz 	(void)fprintf(stderr, "Usage: %s [-i interval] [-u user]\n", getprogname());
647cb1d6897Schristos 	exit(EXIT_FAILURE);
648c36c99efSchristos }
649