#include <sys/cdefs.h>
#ifndef lint
__RCSID("$NetBSD: statd.c,v 1.34 2019/12/02 19:23:53 christos Exp $");
#endif
#include <sys/param.h>
#include <sys/wait.h>
#include <err.h>
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <unistd.h>
#include <util.h>
#include <db.h>
#include <netconfig.h>
#include <rpc/rpc.h>
#include "statd.h"
struct sigaction sa;
int debug = 0;
extern int _rpcsvcdirty;
static DB *db;
Header status_info;
static char undefdata[] = "\0\1\2\3\4\5\6\7";
static DBT undefkey = {
undefdata,
sizeof(undefdata)
};
static int walk_one(int (*fun )(DBT *, HostInfo *, void *), DBT *, DBT *, void *);
static int walk_db(int (*fun )(DBT *, HostInfo *, void *), void *);
static int reset_host(DBT *, HostInfo *, void *);
static int check_work(DBT *, HostInfo *, void *);
static int unmon_host(DBT *, HostInfo *, void *);
static int notify_one(DBT *, HostInfo *, void *);
static void init_file(const char *);
static int notify_one_host(const char *);
static void die(int) __dead;
int
main(int argc, char *argv[])
{
int ch;
struct sigaction nsa;
int maxrec = RPC_MAXDATASIZE;
while ((ch = getopt(argc, argv, "d")) != (-1)) {
switch (ch) {
case 'd':
debug = 1;
break;
default:
case '?':
(void)fprintf(stderr, "usage: %s [-d]\n",
getprogname());
exit(1);
}
}
(void)rpcb_unset(SM_PROG, SM_VERS, NULL);
rpc_control(RPC_SVC_CONNMAXREC_SET, &maxrec);
if (!svc_create(sm_prog_1, SM_PROG, SM_VERS, "udp")) {
errx(EXIT_FAILURE, "cannot create udp service.");
}
if (!svc_create(sm_prog_1, SM_PROG, SM_VERS, "tcp")) {
errx(EXIT_FAILURE, "cannot create udp service.");
}
init_file("/var/db/statd.status");
if (!debug)
daemon(0, 0);
sigemptyset(&nsa.sa_mask);
nsa.sa_flags = SA_NOCLDSTOP|SA_NOCLDWAIT;
nsa.sa_handler = SIG_IGN;
(void)sigaction(SIGCHLD, &nsa, NULL);
pidfile(NULL);
openlog("rpc.statd", 0, LOG_DAEMON);
if (debug)
syslog(LOG_INFO, "Starting - debug enabled");
else
syslog(LOG_INFO, "Starting");
sa.sa_handler = die;
sa.sa_flags = 0;
sigemptyset(&sa.sa_mask);
(void)sigaction(SIGTERM, &sa, NULL);
(void)sigaction(SIGQUIT, &sa, NULL);
(void)sigaction(SIGHUP, &sa, NULL);
(void)sigaction(SIGINT, &sa, NULL);
sa.sa_handler = SIG_IGN;
sa.sa_flags = SA_RESTART;
sigemptyset(&sa.sa_mask);
sigaddset(&sa.sa_mask, SIGALRM);
notify_handler(0);
while (1)
svc_run();
die(0);
}
void
notify_handler(int sig)
{
time_t now;
NO_ALARM;
sa.sa_handler = SIG_IGN;
(void)sigaction(SIGALRM, &sa, NULL);
now = time(NULL);
(void) walk_db(notify_one, &now);
if (walk_db(check_work, &now) == 0) {
CLR_ALARM;
return;
}
sync_file();
ALARM;
alarm(5);
}
void
sync_file()
{
DBT data;
data.data = &status_info;
data.size = sizeof(status_info);
switch ((*db->put)(db, &undefkey, &data, 0)) {
case 0:
return;
case -1:
goto bad;
default:
abort();
}
if ((*db->sync)(db, 0) == -1) {
bad:
syslog(LOG_ERR, "database corrupted %m");
die(1);
}
}
void
change_host(char *hostnamep, HostInfo *hp)
{
DBT key, data;
char *ptr;
char hostname[MAXHOSTNAMELEN + 1];
HostInfo h;
strncpy(hostname, hostnamep, MAXHOSTNAMELEN + 1);
h = *hp;
for (ptr = hostname; *ptr; ptr++)
if (isupper((unsigned char) *ptr))
*ptr = tolower((unsigned char) *ptr);
key.data = hostname;
key.size = ptr - hostname + 1;
data.data = &h;
data.size = sizeof(h);
switch ((*db->put)(db, &key, &data, 0)) {
case -1:
syslog(LOG_ERR, "database corrupted %m");
die(1);
case 0:
return;
default:
abort();
}
}
HostInfo *
find_host(char *hostname, HostInfo *hp)
{
DBT key, data;
char *ptr;
for (ptr = hostname; *ptr; ptr++)
if (isupper((unsigned char) *ptr))
*ptr = tolower((unsigned char) *ptr);
key.data = hostname;
key.size = ptr - hostname + 1;
switch ((*db->get)(db, &key, &data, 0)) {
case 0:
if (data.size != sizeof(*hp))
goto bad;
return memcpy(hp, data.data, sizeof(*hp));
case 1:
return NULL;
case -1:
goto bad;
default:
abort();
}
bad:
syslog(LOG_ERR, "Database corrupted %m");
return NULL;
}
static int
walk_one(int (*fun)(DBT *, HostInfo *, void *), DBT *key, DBT *data, void *ptr)
{
HostInfo h;
if (key->size == undefkey.size &&
memcmp(key->data, undefkey.data, key->size) == 0)
return 0;
if (data->size != sizeof(HostInfo)) {
syslog(LOG_ERR, "Bad data in database");
die(1);
}
memcpy(&h, data->data, sizeof(h));
return (*fun)(key, &h, ptr);
}
static int
walk_db(int (*fun)(DBT *, HostInfo *, void *), void *ptr)
{
DBT key, data;
switch ((*db->seq)(db, &key, &data, R_FIRST)) {
case -1:
goto bad;
case 1:
abort();
case 0:
if (walk_one(fun, &key, &data, ptr) == -1)
return -1;
break;
default:
abort();
}
for (;;)
switch ((*db->seq)(db, &key, &data, R_NEXT)) {
case -1:
goto bad;
case 0:
if (walk_one(fun, &key, &data, ptr) == -1)
return -1;
break;
case 1:
return 0;
default:
abort();
}
bad:
syslog(LOG_ERR, "Corrupted database %m");
die(1);
}
static int
reset_host(DBT *key, HostInfo *hi, void *ptr)
{
if (hi->monList) {
hi->notifyReqd = *(time_t *) ptr;
hi->attempts = 0;
hi->monList = NULL;
change_host((char *)key->data, hi);
}
return 0;
}
static int
check_work(DBT *key, HostInfo *hi, void *ptr)
{
return hi->notifyReqd ? -1 : 0;
}
static int
unmon_host(DBT *key, HostInfo *hi, void *ptr)
{
char *name = key->data;
if (do_unmon(name, hi, ptr))
change_host(name, hi);
return 0;
}
static int
notify_one(DBT *key, HostInfo *hi, void *ptr)
{
time_t now = *(time_t *) ptr;
char *name = key->data;
int error;
if (hi->notifyReqd == 0 || hi->notifyReqd > now)
return 0;
if (notify_one_host(name) || hi->attempts++ >= 44) {
error = 0;
hi->notifyReqd = 0;
hi->attempts = 0;
} else {
error = -1;
if (hi->attempts < 10)
hi->notifyReqd += 5;
else if (hi->attempts < 20)
hi->notifyReqd += 60;
else
hi->notifyReqd += 60 * 60;
}
change_host(name, hi);
return error;
}
static void
init_file(const char *filename)
{
DBT data;
db = dbopen(filename, O_RDWR|O_CREAT|O_NDELAY|O_EXLOCK, 0644, DB_HASH,
NULL);
if (db == NULL)
err(EXIT_FAILURE, "Cannot open `%s'", filename);
switch ((*db->get)(db, &undefkey, &data, 0)) {
case 1:
(void)memset(&status_info, 0, sizeof(status_info));
sync_file();
return;
case -1:
err(EXIT_FAILURE, "error accessing database (%s)", strerror(errno));
case 0:
if (data.size != sizeof(status_info))
errx(EXIT_FAILURE, "database corrupted %lu != %lu",
(u_long)data.size, (u_long)sizeof(status_info));
memcpy(&status_info, data.data, data.size);
break;
default:
abort();
}
reset_database();
return;
}
void
reset_database()
{
time_t now = time(NULL);
walk_db(reset_host, &now);
status_info.ourState =
(status_info.ourState + 2) & 0xfffffffe;
status_info.ourState++;
sync_file();
}
void
unmon_hosts()
{
time_t now = time(NULL);
walk_db(unmon_host, &now);
sync_file();
}
static int
notify_one_host(const char *hostname)
{
struct timeval timeout = {20, 0};
CLIENT *cli;
char dummy;
stat_chge arg;
char our_hostname[MAXHOSTNAMELEN + 1];
gethostname(our_hostname, sizeof(our_hostname));
our_hostname[sizeof(our_hostname) - 1] = '\0';
arg.mon_name = our_hostname;
arg.state = status_info.ourState;
if (debug)
syslog(LOG_DEBUG, "Sending SM_NOTIFY to host %s from %s",
hostname, our_hostname);
cli = clnt_create(hostname, SM_PROG, SM_VERS, "udp");
if (!cli) {
syslog(LOG_ERR, "Failed to contact host %s%s", hostname,
clnt_spcreateerror(""));
return (FALSE);
}
if (clnt_call(cli, SM_NOTIFY, xdr_stat_chge, &arg, xdr_void,
&dummy, timeout) != RPC_SUCCESS) {
syslog(LOG_ERR, "Failed to contact rpc.statd at host %s",
hostname);
clnt_destroy(cli);
return (FALSE);
}
clnt_destroy(cli);
return (TRUE);
}
static void
die(int n)
{
(*db->close)(db);
exit(n);
}