2006-05-11 12:40:28 +04:00
|
|
|
/* $NetBSD: rwhod.c,v 1.34 2006/05/11 08:40:28 mrg Exp $ */
|
2005-06-24 17:22:09 +04:00
|
|
|
|
1993-03-21 21:04:42 +03:00
|
|
|
/*
|
1994-05-29 06:53:21 +04:00
|
|
|
* Copyright (c) 1983, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
1993-03-21 21:04:42 +03:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
2003-08-07 15:25:11 +04:00
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
1993-03-21 21:04:42 +03:00
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
1997-10-17 17:13:40 +04:00
|
|
|
#include <sys/cdefs.h>
|
1993-03-21 21:04:42 +03:00
|
|
|
#ifndef lint
|
1997-10-17 17:13:40 +04:00
|
|
|
__COPYRIGHT("@(#) Copyright (c) 1983, 1993\n\
|
|
|
|
The Regents of the University of California. All rights reserved.\n");
|
1993-03-21 21:04:42 +03:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#ifndef lint
|
1997-10-17 17:13:40 +04:00
|
|
|
#if 0
|
|
|
|
static char sccsid[] = "@(#)rwhod.c 8.1 (Berkeley) 6/6/93";
|
|
|
|
#else
|
2006-05-11 12:40:28 +04:00
|
|
|
__RCSID("$NetBSD: rwhod.c,v 1.34 2006/05/11 08:40:28 mrg Exp $");
|
1997-10-17 17:13:40 +04:00
|
|
|
#endif
|
1993-03-21 21:04:42 +03:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/signal.h>
|
|
|
|
#include <sys/ioctl.h>
|
1994-05-29 06:53:21 +04:00
|
|
|
#include <sys/sysctl.h>
|
1993-03-21 21:04:42 +03:00
|
|
|
|
|
|
|
#include <net/if.h>
|
1994-05-29 06:53:21 +04:00
|
|
|
#include <net/if_dl.h>
|
|
|
|
#include <net/route.h>
|
1993-03-21 21:04:42 +03:00
|
|
|
#include <netinet/in.h>
|
1994-05-29 06:53:21 +04:00
|
|
|
#include <protocols/rwhod.h>
|
1997-10-17 17:13:40 +04:00
|
|
|
#include <arpa/inet.h>
|
1993-03-21 21:04:42 +03:00
|
|
|
|
|
|
|
#include <ctype.h>
|
1997-10-18 15:37:10 +04:00
|
|
|
#include <err.h>
|
1994-05-29 06:53:21 +04:00
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
1993-03-21 21:04:42 +03:00
|
|
|
#include <netdb.h>
|
|
|
|
#include <paths.h>
|
2005-06-24 17:22:09 +04:00
|
|
|
#include <poll.h>
|
1994-05-29 06:53:21 +04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <syslog.h>
|
|
|
|
#include <unistd.h>
|
1999-06-06 07:27:35 +04:00
|
|
|
#include <util.h>
|
1993-03-21 21:04:42 +03:00
|
|
|
|
2002-08-02 06:38:15 +04:00
|
|
|
#include "utmpentry.h"
|
2005-07-01 19:31:18 +04:00
|
|
|
|
2005-06-24 17:47:30 +04:00
|
|
|
#define CHECK_INTERVAL (3 * 60)
|
1993-03-21 21:04:42 +03:00
|
|
|
|
2005-07-01 19:31:18 +04:00
|
|
|
/* Time interval limit; ruptime will think that we are down > than this */
|
|
|
|
#define MAX_INTERVAL (11 * 60)
|
|
|
|
|
|
|
|
|
2005-06-24 17:22:09 +04:00
|
|
|
static char myname[MAXHOSTNAMELEN + 1];
|
1993-03-21 21:04:42 +03:00
|
|
|
|
|
|
|
/*
|
1994-05-29 06:53:21 +04:00
|
|
|
* We communicate with each neighbor in a list constructed at the time we're
|
|
|
|
* started up. Neighbors are currently directly connected via a hardware
|
|
|
|
* interface.
|
1993-03-21 21:04:42 +03:00
|
|
|
*/
|
2005-06-24 17:47:30 +04:00
|
|
|
struct neighbor {
|
1993-03-21 21:04:42 +03:00
|
|
|
struct neighbor *n_next;
|
|
|
|
char *n_name; /* interface name */
|
1998-07-08 19:17:57 +04:00
|
|
|
struct sockaddr *n_addr; /* who to send to */
|
1993-03-21 21:04:42 +03:00
|
|
|
int n_addrlen; /* size of address */
|
|
|
|
int n_flags; /* should forward?, interface flags */
|
|
|
|
};
|
|
|
|
|
2005-06-24 17:22:09 +04:00
|
|
|
static struct neighbor *neighbors;
|
|
|
|
static struct whod mywd;
|
|
|
|
static struct servent *sp;
|
|
|
|
static volatile sig_atomic_t onsighup;
|
1993-03-21 21:04:42 +03:00
|
|
|
|
1994-05-29 06:53:21 +04:00
|
|
|
#define WHDRSIZE (sizeof(mywd) - sizeof(mywd.wd_we))
|
1993-03-21 21:04:42 +03:00
|
|
|
|
2005-06-24 17:22:09 +04:00
|
|
|
static int configure(int);
|
|
|
|
static void getboottime(void);
|
|
|
|
static void send_host_information(int);
|
2005-06-24 17:47:30 +04:00
|
|
|
static void sighup(int);
|
2005-06-24 17:22:09 +04:00
|
|
|
static void handleread(int);
|
|
|
|
static void quit(const char *);
|
|
|
|
static void rt_xaddrs(void *, void *, struct rt_addrinfo *);
|
2005-09-12 20:13:13 +04:00
|
|
|
static int drop_privs(char *);
|
2005-07-01 17:07:21 +04:00
|
|
|
static void usage(void) __attribute__((__noreturn__));
|
2005-06-24 17:22:09 +04:00
|
|
|
static int verify(const char *);
|
1994-05-29 06:53:21 +04:00
|
|
|
#ifdef DEBUG
|
2005-06-24 17:22:09 +04:00
|
|
|
static char *interval(int, const char *);
|
|
|
|
static ssize_t Sendto(int, const void *, size_t, int,
|
|
|
|
const struct sockaddr *, socklen_t);
|
2005-07-05 06:46:36 +04:00
|
|
|
#else
|
|
|
|
#define Sendto sendto
|
1994-05-29 06:53:21 +04:00
|
|
|
#endif
|
1993-03-21 21:04:42 +03:00
|
|
|
|
1994-05-29 06:53:21 +04:00
|
|
|
int
|
2005-06-24 17:22:09 +04:00
|
|
|
main(int argc, char *argv[])
|
1993-03-21 21:04:42 +03:00
|
|
|
{
|
2005-07-01 17:07:21 +04:00
|
|
|
int s, ch;
|
|
|
|
int time_interval = 180; /* Default time (180 seconds) */
|
|
|
|
char *cp, *ep;
|
2005-06-24 17:47:30 +04:00
|
|
|
socklen_t on = 1;
|
2005-06-24 17:22:09 +04:00
|
|
|
struct sockaddr_in sasin;
|
|
|
|
struct pollfd pfd[1];
|
2005-06-24 17:47:30 +04:00
|
|
|
struct timeval delta, next, now;
|
2005-09-12 20:13:13 +04:00
|
|
|
char *newuser = NULL;
|
1993-03-21 21:04:42 +03:00
|
|
|
|
2005-07-01 17:07:21 +04:00
|
|
|
setprogname(argv[0]);
|
|
|
|
|
1997-10-18 15:37:10 +04:00
|
|
|
if (getuid())
|
2005-06-24 17:47:30 +04:00
|
|
|
errx(EXIT_FAILURE, "not super user");
|
2005-07-01 17:07:21 +04:00
|
|
|
|
2005-09-12 20:13:13 +04:00
|
|
|
while ((ch = getopt(argc, argv, "i:u:")) != -1) {
|
2005-07-01 17:07:21 +04:00
|
|
|
switch (ch) {
|
2005-07-01 19:31:18 +04:00
|
|
|
case 'i':
|
2005-07-01 17:07:21 +04:00
|
|
|
time_interval = (int)strtol(optarg, &ep, 10);
|
|
|
|
|
|
|
|
switch (*ep) {
|
|
|
|
case '\0':
|
|
|
|
break;
|
|
|
|
case 'm':
|
|
|
|
case 'M':
|
|
|
|
/* Time in minutes. */
|
|
|
|
time_interval *= 60;
|
|
|
|
if (ep[1] == '\0')
|
|
|
|
break;
|
|
|
|
/*FALLTHROUGH*/
|
|
|
|
default:
|
|
|
|
errx(1, "Invalid argument: `%s'", optarg);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (time_interval <= 0)
|
2005-07-01 19:31:18 +04:00
|
|
|
errx(1, "Interval must be greater than 0");
|
2005-07-01 17:07:21 +04:00
|
|
|
|
2005-07-01 19:31:18 +04:00
|
|
|
if (time_interval > MAX_INTERVAL)
|
|
|
|
errx(1, "Interval cannot be greater than"
|
|
|
|
" %d minutes", MAX_INTERVAL / 60);
|
2005-07-01 17:07:21 +04:00
|
|
|
break;
|
2005-09-12 20:13:13 +04:00
|
|
|
|
|
|
|
case 'u':
|
|
|
|
newuser = optarg;
|
|
|
|
break;
|
|
|
|
|
2005-07-01 17:07:21 +04:00
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1993-03-21 21:04:42 +03:00
|
|
|
sp = getservbyname("who", "udp");
|
1997-10-18 15:37:10 +04:00
|
|
|
if (sp == NULL)
|
2005-06-24 17:47:30 +04:00
|
|
|
errx(EXIT_FAILURE, "udp/who: unknown service");
|
1993-03-21 21:04:42 +03:00
|
|
|
#ifndef DEBUG
|
2005-06-24 17:47:30 +04:00
|
|
|
(void)daemon(1, 0);
|
|
|
|
(void)pidfile(NULL);
|
1993-03-21 21:04:42 +03:00
|
|
|
#endif
|
1997-10-18 15:37:10 +04:00
|
|
|
if (chdir(_PATH_RWHODIR) < 0)
|
2005-06-24 17:47:30 +04:00
|
|
|
err(EXIT_FAILURE, "%s", _PATH_RWHODIR);
|
|
|
|
(void)signal(SIGHUP, sighup);
|
2005-07-05 06:46:36 +04:00
|
|
|
openlog(getprogname(), LOG_PID, LOG_DAEMON);
|
1993-03-21 21:04:42 +03:00
|
|
|
/*
|
|
|
|
* Establish host name as returned by system.
|
|
|
|
*/
|
1994-05-29 06:53:21 +04:00
|
|
|
if (gethostname(myname, sizeof(myname) - 1) < 0) {
|
1993-03-21 21:04:42 +03:00
|
|
|
syslog(LOG_ERR, "gethostname: %m");
|
2005-06-24 17:47:30 +04:00
|
|
|
exit(EXIT_FAILURE);
|
1993-03-21 21:04:42 +03:00
|
|
|
}
|
1998-07-06 10:56:06 +04:00
|
|
|
myname[sizeof(myname) - 1] = '\0';
|
1997-10-18 15:37:10 +04:00
|
|
|
if ((cp = strchr(myname, '.')) != NULL)
|
1993-03-21 21:04:42 +03:00
|
|
|
*cp = '\0';
|
2005-06-24 17:22:09 +04:00
|
|
|
(void)strncpy(mywd.wd_hostname, myname, sizeof(mywd.wd_hostname) - 1);
|
|
|
|
getboottime();
|
1993-03-21 21:04:42 +03:00
|
|
|
if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
|
|
|
|
syslog(LOG_ERR, "socket: %m");
|
2005-06-24 17:47:30 +04:00
|
|
|
exit(EXIT_FAILURE);
|
1993-03-21 21:04:42 +03:00
|
|
|
}
|
1994-05-29 06:53:21 +04:00
|
|
|
if (setsockopt(s, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on)) < 0) {
|
1993-03-21 21:04:42 +03:00
|
|
|
syslog(LOG_ERR, "setsockopt SO_BROADCAST: %m");
|
2005-06-24 17:47:30 +04:00
|
|
|
exit(EXIT_FAILURE);
|
1993-03-21 21:04:42 +03:00
|
|
|
}
|
2005-06-24 17:22:09 +04:00
|
|
|
(void)memset(&sasin, 0, sizeof(sasin));
|
|
|
|
sasin.sin_family = AF_INET;
|
|
|
|
sasin.sin_port = sp->s_port;
|
|
|
|
if (bind(s, (struct sockaddr *)&sasin, sizeof(sasin)) < 0) {
|
1993-03-21 21:04:42 +03:00
|
|
|
syslog(LOG_ERR, "bind: %m");
|
2005-06-24 17:47:30 +04:00
|
|
|
exit(EXIT_FAILURE);
|
1993-03-21 21:04:42 +03:00
|
|
|
}
|
|
|
|
if (!configure(s))
|
2005-06-24 17:47:30 +04:00
|
|
|
exit(EXIT_FAILURE);
|
2005-06-24 17:22:09 +04:00
|
|
|
|
2005-09-12 20:13:13 +04:00
|
|
|
if (newuser)
|
|
|
|
if (!drop_privs(newuser))
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
|
2005-06-24 17:22:09 +04:00
|
|
|
send_host_information(s);
|
2005-07-01 17:07:21 +04:00
|
|
|
delta.tv_sec = time_interval;
|
2005-06-24 17:47:30 +04:00
|
|
|
delta.tv_usec = 0;
|
2005-06-24 17:22:09 +04:00
|
|
|
gettimeofday(&now, NULL);
|
2005-06-24 17:47:30 +04:00
|
|
|
timeradd(&now, &delta, &next);
|
2005-06-24 17:22:09 +04:00
|
|
|
|
|
|
|
pfd[0].fd = s;
|
2005-06-24 17:24:23 +04:00
|
|
|
pfd[0].events = POLLIN;
|
2005-06-24 17:22:09 +04:00
|
|
|
|
1993-03-21 21:04:42 +03:00
|
|
|
for (;;) {
|
2005-06-24 17:22:09 +04:00
|
|
|
int n;
|
1999-11-15 18:59:24 +03:00
|
|
|
|
2005-06-24 17:22:09 +04:00
|
|
|
n = poll(pfd, 1, 1000);
|
|
|
|
|
|
|
|
if (onsighup) {
|
|
|
|
onsighup = 0;
|
|
|
|
getboottime();
|
1993-03-21 21:04:42 +03:00
|
|
|
}
|
2005-06-24 17:22:09 +04:00
|
|
|
|
|
|
|
if (n == 1)
|
|
|
|
handleread(s);
|
|
|
|
|
|
|
|
(void)gettimeofday(&now, NULL);
|
2005-06-24 17:47:30 +04:00
|
|
|
if (timercmp(&now, &next, >)) {
|
2005-06-24 17:22:09 +04:00
|
|
|
send_host_information(s);
|
2005-06-24 17:47:30 +04:00
|
|
|
timeradd(&now, &delta, &next);
|
1993-03-21 21:04:42 +03:00
|
|
|
}
|
2005-06-24 17:22:09 +04:00
|
|
|
}
|
|
|
|
|
2005-06-24 17:47:30 +04:00
|
|
|
/* NOTREACHED */
|
|
|
|
return 0;
|
2005-06-24 17:22:09 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2005-06-24 17:47:30 +04:00
|
|
|
sighup(int signo __unused)
|
2005-06-24 17:22:09 +04:00
|
|
|
{
|
2005-06-24 17:47:30 +04:00
|
|
|
onsighup = 1;
|
2005-06-24 17:22:09 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
handleread(int s)
|
|
|
|
{
|
|
|
|
struct sockaddr_in from;
|
|
|
|
struct stat st;
|
|
|
|
char path[64];
|
|
|
|
struct whod wd;
|
|
|
|
int cc, whod;
|
|
|
|
socklen_t len = sizeof(from);
|
|
|
|
|
|
|
|
cc = recvfrom(s, (char *)&wd, sizeof(struct whod), 0,
|
|
|
|
(struct sockaddr *)&from, &len);
|
|
|
|
if (cc <= 0) {
|
|
|
|
if (cc < 0 && errno != EINTR)
|
|
|
|
syslog(LOG_WARNING, "recv: %m");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (from.sin_port != sp->s_port) {
|
|
|
|
syslog(LOG_WARNING, "%d: bad from port",
|
|
|
|
ntohs(from.sin_port));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (cc < WHDRSIZE) {
|
|
|
|
syslog(LOG_WARNING, "Short packet from %s",
|
|
|
|
inet_ntoa(from.sin_addr));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (wd.wd_vers != WHODVERSION)
|
|
|
|
return;
|
|
|
|
if (wd.wd_type != WHODTYPE_STATUS)
|
|
|
|
return;
|
2005-06-27 16:07:46 +04:00
|
|
|
/*
|
2005-06-24 17:22:09 +04:00
|
|
|
* Ensure null termination of the name within the packet.
|
|
|
|
* Otherwise we might overflow or read past the end.
|
|
|
|
*/
|
|
|
|
wd.wd_hostname[sizeof(wd.wd_hostname)-1] = 0;
|
|
|
|
if (!verify(wd.wd_hostname)) {
|
|
|
|
syslog(LOG_WARNING, "malformed host name from %s",
|
|
|
|
inet_ntoa(from.sin_addr));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
(void)snprintf(path, sizeof(path), "whod.%s", wd.wd_hostname);
|
|
|
|
/*
|
|
|
|
* Rather than truncating and growing the file each time,
|
|
|
|
* use ftruncate if size is less than previous size.
|
|
|
|
*/
|
|
|
|
whod = open(path, O_WRONLY | O_CREAT, 0644);
|
|
|
|
if (whod < 0) {
|
|
|
|
syslog(LOG_WARNING, "%s: %m", path);
|
|
|
|
return;
|
|
|
|
}
|
1993-03-21 21:04:42 +03:00
|
|
|
#if ENDIAN != BIG_ENDIAN
|
2005-06-24 17:22:09 +04:00
|
|
|
{
|
2005-06-24 17:47:30 +04:00
|
|
|
int i, n = (cc - WHDRSIZE) / sizeof(struct whoent);
|
2005-06-24 17:22:09 +04:00
|
|
|
struct whoent *we;
|
|
|
|
|
|
|
|
/* undo header byte swapping before writing to file */
|
|
|
|
wd.wd_sendtime = ntohl(wd.wd_sendtime);
|
|
|
|
for (i = 0; i < 3; i++)
|
|
|
|
wd.wd_loadav[i] = ntohl(wd.wd_loadav[i]);
|
|
|
|
wd.wd_boottime = ntohl(wd.wd_boottime);
|
|
|
|
we = wd.wd_we;
|
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
we->we_idle = ntohl(we->we_idle);
|
|
|
|
we->we_utmp.out_time =
|
|
|
|
ntohl(we->we_utmp.out_time);
|
|
|
|
we++;
|
1993-03-21 21:04:42 +03:00
|
|
|
}
|
|
|
|
}
|
2005-06-24 17:22:09 +04:00
|
|
|
#endif
|
2006-05-11 12:40:28 +04:00
|
|
|
wd.wd_recvtime = time(NULL);
|
2005-06-24 17:22:09 +04:00
|
|
|
(void)write(whod, (char *)&wd, cc);
|
|
|
|
if (fstat(whod, &st) < 0 || st.st_size > cc)
|
|
|
|
(void)ftruncate(whod, cc);
|
|
|
|
(void)close(whod);
|
1993-03-21 21:04:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check out host name for unprintables
|
|
|
|
* and other funnies before allowing a file
|
|
|
|
* to be created. Sorry, but blanks aren't allowed.
|
|
|
|
*/
|
2005-06-24 17:22:09 +04:00
|
|
|
static int
|
|
|
|
verify(const char *name)
|
1993-03-21 21:04:42 +03:00
|
|
|
{
|
1997-10-18 15:37:10 +04:00
|
|
|
int size = 0;
|
1993-03-21 21:04:42 +03:00
|
|
|
|
|
|
|
while (*name) {
|
2005-06-24 17:22:09 +04:00
|
|
|
if (!isascii((unsigned char)*name) ||
|
|
|
|
!(isalnum((unsigned char)*name) ||
|
|
|
|
ispunct((unsigned char)*name)))
|
|
|
|
return 0;
|
1993-03-21 21:04:42 +03:00
|
|
|
name++, size++;
|
|
|
|
}
|
2005-06-24 17:22:09 +04:00
|
|
|
return size > 0;
|
1993-03-21 21:04:42 +03:00
|
|
|
}
|
|
|
|
|
2005-06-24 17:22:09 +04:00
|
|
|
static void
|
|
|
|
send_host_information(int s)
|
1993-03-21 21:04:42 +03:00
|
|
|
{
|
1997-10-18 15:37:10 +04:00
|
|
|
struct neighbor *np;
|
|
|
|
struct whoent *we = mywd.wd_we, *wlast;
|
2005-06-27 16:07:46 +04:00
|
|
|
int i, cc, utmpent = 0;
|
1993-03-21 21:04:42 +03:00
|
|
|
struct stat stb;
|
|
|
|
double avenrun[3];
|
1994-05-29 06:53:21 +04:00
|
|
|
time_t now;
|
2002-08-02 06:38:15 +04:00
|
|
|
static struct utmpentry *ohead = NULL;
|
|
|
|
struct utmpentry *ep;
|
2005-06-24 17:47:30 +04:00
|
|
|
static int count = 0;
|
1993-03-21 21:04:42 +03:00
|
|
|
|
1994-05-29 06:53:21 +04:00
|
|
|
now = time(NULL);
|
2005-06-24 17:47:30 +04:00
|
|
|
if (count % 10 == 0)
|
2005-06-24 17:22:09 +04:00
|
|
|
getboottime();
|
2005-06-24 17:47:30 +04:00
|
|
|
count++;
|
2002-08-02 06:38:15 +04:00
|
|
|
|
|
|
|
(void)getutentries(NULL, &ep);
|
|
|
|
if (ep != ohead) {
|
|
|
|
freeutentries(ep);
|
1994-05-29 06:53:21 +04:00
|
|
|
wlast = &mywd.wd_we[1024 / sizeof(struct whoent) - 1];
|
2002-08-02 06:38:15 +04:00
|
|
|
for (; ep; ep = ep->next) {
|
|
|
|
(void)strncpy(we->we_utmp.out_line, ep->line,
|
2005-10-02 20:49:46 +04:00
|
|
|
sizeof(we->we_utmp.out_line));
|
2002-08-02 06:38:15 +04:00
|
|
|
(void)strncpy(we->we_utmp.out_name, ep->name,
|
2005-10-02 20:49:46 +04:00
|
|
|
sizeof(we->we_utmp.out_name));
|
2002-08-02 06:38:15 +04:00
|
|
|
we->we_utmp.out_time = htonl(ep->tv.tv_sec);
|
|
|
|
if (we >= wlast)
|
|
|
|
break;
|
|
|
|
we++;
|
|
|
|
}
|
1993-03-21 21:04:42 +03:00
|
|
|
utmpent = we - mywd.wd_we;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The test on utmpent looks silly---after all, if no one is
|
|
|
|
* logged on, why worry about efficiency?---but is useful on
|
|
|
|
* (e.g.) compute servers.
|
|
|
|
*/
|
|
|
|
if (utmpent && chdir(_PATH_DEV)) {
|
|
|
|
syslog(LOG_ERR, "chdir(%s): %m", _PATH_DEV);
|
2005-06-24 17:47:30 +04:00
|
|
|
exit(EXIT_FAILURE);
|
1993-03-21 21:04:42 +03:00
|
|
|
}
|
|
|
|
we = mywd.wd_we;
|
|
|
|
for (i = 0; i < utmpent; i++) {
|
|
|
|
if (stat(we->we_utmp.out_line, &stb) >= 0)
|
|
|
|
we->we_idle = htonl(now - stb.st_atime);
|
|
|
|
we++;
|
|
|
|
}
|
1994-05-29 06:53:21 +04:00
|
|
|
(void)getloadavg(avenrun, sizeof(avenrun)/sizeof(avenrun[0]));
|
1993-03-21 21:04:42 +03:00
|
|
|
for (i = 0; i < 3; i++)
|
|
|
|
mywd.wd_loadav[i] = htonl((u_long)(avenrun[i] * 100));
|
|
|
|
cc = (char *)we - (char *)&mywd;
|
|
|
|
mywd.wd_sendtime = htonl(time(0));
|
|
|
|
mywd.wd_vers = WHODVERSION;
|
|
|
|
mywd.wd_type = WHODTYPE_STATUS;
|
|
|
|
for (np = neighbors; np != NULL; np = np->n_next)
|
2005-07-05 06:46:36 +04:00
|
|
|
(void)Sendto(s, (char *)&mywd, cc, 0,
|
1994-05-29 06:53:21 +04:00
|
|
|
np->n_addr, np->n_addrlen);
|
1993-03-21 21:04:42 +03:00
|
|
|
if (utmpent && chdir(_PATH_RWHODIR)) {
|
|
|
|
syslog(LOG_ERR, "chdir(%s): %m", _PATH_RWHODIR);
|
2005-06-24 17:47:30 +04:00
|
|
|
exit(EXIT_FAILURE);
|
1993-03-21 21:04:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-06-24 17:22:09 +04:00
|
|
|
static void
|
|
|
|
getboottime(void)
|
1993-03-21 21:04:42 +03:00
|
|
|
{
|
1994-05-29 06:53:21 +04:00
|
|
|
int mib[2];
|
|
|
|
size_t size;
|
|
|
|
struct timeval tm;
|
|
|
|
|
|
|
|
mib[0] = CTL_KERN;
|
|
|
|
mib[1] = KERN_BOOTTIME;
|
|
|
|
size = sizeof(tm);
|
|
|
|
if (sysctl(mib, 2, &tm, &size, NULL, 0) == -1) {
|
|
|
|
syslog(LOG_ERR, "cannot get boottime: %m");
|
2005-06-24 17:47:30 +04:00
|
|
|
exit(EXIT_FAILURE);
|
1993-03-21 21:04:42 +03:00
|
|
|
}
|
1994-05-29 06:53:21 +04:00
|
|
|
mywd.wd_boottime = htonl(tm.tv_sec);
|
|
|
|
}
|
|
|
|
|
2005-06-24 17:22:09 +04:00
|
|
|
static void
|
|
|
|
quit(const char *msg)
|
1994-05-29 06:53:21 +04:00
|
|
|
{
|
2000-10-12 00:23:46 +04:00
|
|
|
syslog(LOG_ERR, "%s", msg);
|
2005-06-24 17:47:30 +04:00
|
|
|
exit(EXIT_FAILURE);
|
1994-05-29 06:53:21 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
#define ROUNDUP(a) \
|
|
|
|
((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
|
2005-06-24 17:22:09 +04:00
|
|
|
#define ADVANCE(x, n) ((char *)(x) + ROUNDUP((n)->sa_len))
|
1994-05-29 06:53:21 +04:00
|
|
|
|
2005-06-24 17:22:09 +04:00
|
|
|
static void
|
|
|
|
rt_xaddrs(void *cp, void *cplim, struct rt_addrinfo *rtinfo)
|
1994-05-29 06:53:21 +04:00
|
|
|
{
|
1997-10-18 15:37:10 +04:00
|
|
|
struct sockaddr *sa;
|
|
|
|
int i;
|
1994-04-06 07:01:46 +04:00
|
|
|
|
2005-06-24 17:22:09 +04:00
|
|
|
(void)memset(rtinfo->rti_info, 0, sizeof(rtinfo->rti_info));
|
1994-05-29 06:53:21 +04:00
|
|
|
for (i = 0; (i < RTAX_MAX) && (cp < cplim); i++) {
|
|
|
|
if ((rtinfo->rti_addrs & (1 << i)) == 0)
|
|
|
|
continue;
|
|
|
|
rtinfo->rti_info[i] = sa = (struct sockaddr *)cp;
|
2005-06-24 17:22:09 +04:00
|
|
|
cp = ADVANCE(cp, sa);
|
1993-03-21 21:04:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Figure out device configuration and select
|
|
|
|
* networks which deserve status information.
|
|
|
|
*/
|
2005-06-24 17:22:09 +04:00
|
|
|
static int
|
|
|
|
configure(int s)
|
1993-03-21 21:04:42 +03:00
|
|
|
{
|
1997-10-18 15:37:10 +04:00
|
|
|
struct neighbor *np;
|
|
|
|
struct if_msghdr *ifm;
|
|
|
|
struct ifa_msghdr *ifam;
|
1994-05-29 06:53:21 +04:00
|
|
|
struct sockaddr_dl *sdl;
|
|
|
|
size_t needed;
|
|
|
|
int mib[6], flags = 0, len;
|
|
|
|
char *buf, *lim, *next;
|
|
|
|
struct rt_addrinfo info;
|
2005-06-24 17:22:09 +04:00
|
|
|
struct sockaddr_in dstaddr;
|
1994-05-29 06:53:21 +04:00
|
|
|
|
|
|
|
mib[0] = CTL_NET;
|
|
|
|
mib[1] = PF_ROUTE;
|
|
|
|
mib[2] = 0;
|
|
|
|
mib[3] = AF_INET;
|
|
|
|
mib[4] = NET_RT_IFLIST;
|
|
|
|
mib[5] = 0;
|
|
|
|
if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
|
|
|
|
quit("route-sysctl-estimate");
|
|
|
|
if ((buf = malloc(needed)) == NULL)
|
|
|
|
quit("malloc");
|
|
|
|
if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
|
|
|
|
quit("actual retrieval of interface table");
|
|
|
|
lim = buf + needed;
|
|
|
|
|
|
|
|
sdl = NULL; /* XXX just to keep gcc -Wall happy */
|
|
|
|
for (next = buf; next < lim; next += ifm->ifm_msglen) {
|
|
|
|
ifm = (struct if_msghdr *)next;
|
|
|
|
if (ifm->ifm_type == RTM_IFINFO) {
|
|
|
|
sdl = (struct sockaddr_dl *)(ifm + 1);
|
|
|
|
flags = ifm->ifm_flags;
|
1993-03-21 21:04:42 +03:00
|
|
|
continue;
|
|
|
|
}
|
1994-05-29 06:53:21 +04:00
|
|
|
if ((flags & IFF_UP) == 0 ||
|
|
|
|
(flags & (IFF_BROADCAST|IFF_POINTOPOINT)) == 0)
|
1993-03-21 21:04:42 +03:00
|
|
|
continue;
|
1994-05-29 06:53:21 +04:00
|
|
|
if (ifm->ifm_type != RTM_NEWADDR)
|
|
|
|
quit("out of sync parsing NET_RT_IFLIST");
|
|
|
|
ifam = (struct ifa_msghdr *)ifm;
|
|
|
|
info.rti_addrs = ifam->ifam_addrs;
|
2005-06-24 17:22:09 +04:00
|
|
|
rt_xaddrs((ifam + 1), ifam->ifam_msglen + (char *)ifam, &info);
|
1994-05-29 06:53:21 +04:00
|
|
|
/* gag, wish we could get rid of Internet dependencies */
|
2005-06-24 17:22:09 +04:00
|
|
|
if (info.rti_info[RTAX_BRD] == NULL ||
|
|
|
|
info.rti_info[RTAX_BRD]->sa_family != AF_INET)
|
|
|
|
continue;
|
|
|
|
(void)memcpy(&dstaddr, info.rti_info[RTAX_BRD],
|
|
|
|
sizeof(dstaddr));
|
1994-05-29 06:53:21 +04:00
|
|
|
#define IPADDR_SA(x) ((struct sockaddr_in *)(x))->sin_addr.s_addr
|
|
|
|
#define PORT_SA(x) ((struct sockaddr_in *)(x))->sin_port
|
2005-06-24 17:22:09 +04:00
|
|
|
PORT_SA(&dstaddr) = sp->s_port;
|
1994-05-29 06:53:21 +04:00
|
|
|
for (np = neighbors; np != NULL; np = np->n_next)
|
|
|
|
if (memcmp(sdl->sdl_data, np->n_name,
|
|
|
|
sdl->sdl_nlen) == 0 &&
|
2005-06-24 17:22:09 +04:00
|
|
|
IPADDR_SA(np->n_addr) == IPADDR_SA(&dstaddr))
|
1994-05-29 06:53:21 +04:00
|
|
|
break;
|
|
|
|
if (np != NULL)
|
1993-03-21 21:04:42 +03:00
|
|
|
continue;
|
2005-06-24 17:22:09 +04:00
|
|
|
len = sizeof(*np) + dstaddr.sin_len + sdl->sdl_nlen + 1;
|
1994-05-29 06:53:21 +04:00
|
|
|
np = (struct neighbor *)malloc(len);
|
|
|
|
if (np == NULL)
|
|
|
|
quit("malloc of neighbor structure");
|
2005-06-24 17:22:09 +04:00
|
|
|
(void)memset(np, 0, len);
|
1994-05-29 06:53:21 +04:00
|
|
|
np->n_flags = flags;
|
|
|
|
np->n_addr = (struct sockaddr *)(np + 1);
|
2005-06-24 17:22:09 +04:00
|
|
|
np->n_addrlen = dstaddr.sin_len;
|
1994-05-29 06:53:21 +04:00
|
|
|
np->n_name = np->n_addrlen + (char *)np->n_addr;
|
1993-03-21 21:04:42 +03:00
|
|
|
np->n_next = neighbors;
|
|
|
|
neighbors = np;
|
2005-06-24 17:22:09 +04:00
|
|
|
(void)memcpy(np->n_addr, &dstaddr, np->n_addrlen);
|
|
|
|
(void)memcpy(np->n_name, sdl->sdl_data, sdl->sdl_nlen);
|
1993-03-21 21:04:42 +03:00
|
|
|
}
|
1994-05-29 06:53:21 +04:00
|
|
|
free(buf);
|
1993-03-21 21:04:42 +03:00
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
2005-06-24 17:22:09 +04:00
|
|
|
static ssize_t
|
|
|
|
Sendto(int s, const void *buf, size_t cc, int flags, const struct sockaddr *to,
|
|
|
|
socklen_t tolen)
|
1993-03-21 21:04:42 +03:00
|
|
|
{
|
1997-10-18 15:37:10 +04:00
|
|
|
struct whod *w = (struct whod *)buf;
|
|
|
|
struct whoent *we;
|
2005-06-24 17:22:09 +04:00
|
|
|
struct sockaddr_in *sasin = (struct sockaddr_in *)to;
|
2005-07-05 06:46:36 +04:00
|
|
|
ssize_t ret;
|
|
|
|
|
|
|
|
ret = sendto(s, buf, cc, flags, to, tolen);
|
1993-03-21 21:04:42 +03:00
|
|
|
|
2005-07-01 17:07:21 +04:00
|
|
|
printf("sendto %s.%d\n", inet_ntoa(sasin->sin_addr),
|
2005-06-24 17:22:09 +04:00
|
|
|
ntohs(sasin->sin_port));
|
1993-03-21 21:04:42 +03:00
|
|
|
printf("hostname %s %s\n", w->wd_hostname,
|
|
|
|
interval(ntohl(w->wd_sendtime) - ntohl(w->wd_boottime), " up"));
|
|
|
|
printf("load %4.2f, %4.2f, %4.2f\n",
|
|
|
|
ntohl(w->wd_loadav[0]) / 100.0, ntohl(w->wd_loadav[1]) / 100.0,
|
|
|
|
ntohl(w->wd_loadav[2]) / 100.0);
|
|
|
|
cc -= WHDRSIZE;
|
1994-05-29 06:53:21 +04:00
|
|
|
for (we = w->wd_we, cc /= sizeof(struct whoent); cc > 0; cc--, we++) {
|
1993-03-21 21:04:42 +03:00
|
|
|
time_t t = ntohl(we->we_utmp.out_time);
|
2003-02-12 20:49:35 +03:00
|
|
|
printf("%-8.8s %s:%s %.12s", we->we_utmp.out_name,
|
|
|
|
w->wd_hostname, we->we_utmp.out_line, ctime(&t)+4);
|
1993-03-21 21:04:42 +03:00
|
|
|
we->we_idle = ntohl(we->we_idle) / 60;
|
|
|
|
if (we->we_idle) {
|
|
|
|
if (we->we_idle >= 100*60)
|
|
|
|
we->we_idle = 100*60 - 1;
|
|
|
|
if (we->we_idle >= 60)
|
|
|
|
printf(" %2d", we->we_idle / 60);
|
|
|
|
else
|
|
|
|
printf(" ");
|
|
|
|
printf(":%02d", we->we_idle % 60);
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
}
|
2005-07-05 06:46:36 +04:00
|
|
|
return ret;
|
1993-03-21 21:04:42 +03:00
|
|
|
}
|
|
|
|
|
2005-06-24 17:22:09 +04:00
|
|
|
static char *
|
|
|
|
interval(int time, const char *updown)
|
1993-03-21 21:04:42 +03:00
|
|
|
{
|
|
|
|
static char resbuf[32];
|
|
|
|
int days, hours, minutes;
|
|
|
|
|
|
|
|
if (time < 0 || time > 3*30*24*60*60) {
|
2003-07-13 16:08:28 +04:00
|
|
|
(void)snprintf(resbuf, sizeof(resbuf), " %s ??:??", updown);
|
1993-03-21 21:04:42 +03:00
|
|
|
return (resbuf);
|
|
|
|
}
|
|
|
|
minutes = (time + 59) / 60; /* round to minutes */
|
|
|
|
hours = minutes / 60; minutes %= 60;
|
|
|
|
days = hours / 24; hours %= 24;
|
|
|
|
if (days)
|
2003-07-13 16:08:28 +04:00
|
|
|
(void)snprintf(resbuf, sizeof(resbuf), "%s %2d+%02d:%02d",
|
1993-03-21 21:04:42 +03:00
|
|
|
updown, days, hours, minutes);
|
|
|
|
else
|
2003-07-13 16:08:28 +04:00
|
|
|
(void)snprintf(resbuf, sizeof(resbuf), "%s %2d:%02d",
|
1993-03-21 21:04:42 +03:00
|
|
|
updown, hours, minutes);
|
2005-06-24 17:22:09 +04:00
|
|
|
return resbuf;
|
1993-03-21 21:04:42 +03:00
|
|
|
}
|
|
|
|
#endif
|
2005-07-01 17:07:21 +04:00
|
|
|
|
2005-09-12 20:13:13 +04:00
|
|
|
static int
|
|
|
|
drop_privs(char *newuser)
|
|
|
|
{
|
|
|
|
struct passwd *pw;
|
|
|
|
gid_t gidset[1];
|
|
|
|
|
|
|
|
pw = getpwnam(newuser);
|
|
|
|
if (pw == NULL) {
|
|
|
|
syslog(LOG_ERR, "no user %.100s", newuser);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
endpwent();
|
|
|
|
|
|
|
|
gidset[0] = pw->pw_gid;
|
|
|
|
if (setgroups(1, gidset) == -1) {
|
|
|
|
syslog(LOG_ERR, "setgroups: %m");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (setgid(pw->pw_gid) == -1) {
|
|
|
|
syslog(LOG_ERR, "setgid: %m");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (setuid(pw->pw_uid) == -1) {
|
|
|
|
syslog(LOG_ERR, "setuid: %m");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2005-07-01 17:07:21 +04:00
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
2005-09-12 22:34:37 +04:00
|
|
|
(void)fprintf(stderr, "Usage: %s [-i interval] [-u user]\n", getprogname());
|
2005-07-05 06:46:36 +04:00
|
|
|
exit(EXIT_FAILURE);
|
2005-07-01 17:07:21 +04:00
|
|
|
}
|