2025-02-14 17:39:57 +01:00
|
|
|
/* $OpenBSD: mib.c,v 1.9 2024/05/22 08:44:02 martijn Exp $ */
|
|
|
|
|
|
|
|
/*
|
2025-02-14 18:10:30 +01:00
|
|
|
* Copyright (c) 2025 Wolfgang Hottgenroth <woho@hottis.de>
|
2025-02-14 17:39:57 +01:00
|
|
|
* Copyright (c) 2022 Martijn van Duren <martijn@openbsd.org>
|
|
|
|
* Copyright (c) 2012 Joel Knight <joel@openbsd.org>
|
|
|
|
* Copyright (c) 2007, 2008, 2012 Reyk Floeter <reyk@openbsd.org>
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/signal.h>
|
|
|
|
#include <sys/proc.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/un.h>
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <event.h>
|
|
|
|
#include <grp.h>
|
|
|
|
#include <pwd.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <syslog.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <libgen.h>
|
|
|
|
#include <limits.h>
|
2025-02-15 00:07:31 +01:00
|
|
|
// #include <agentx.h> MUST BE INCLUDED ONLY ONCE, WILL BE INCLUDED IN mib.h
|
2025-02-14 17:39:57 +01:00
|
|
|
#include "mib.h"
|
2025-02-15 00:00:14 +01:00
|
|
|
#include "log.h"
|
2025-02-14 17:39:57 +01:00
|
|
|
|
|
|
|
struct event connev;
|
|
|
|
|
|
|
|
void snmp_connect(struct agentx *, void *, int);
|
|
|
|
void snmp_tryconnect(int, short, void *);
|
|
|
|
void snmp_read(int, short, void *);
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
struct agentx *sa;
|
2025-02-14 23:53:10 +01:00
|
|
|
struct agentx_context *sac;
|
2025-02-14 17:39:57 +01:00
|
|
|
struct agentx_session *sas;
|
|
|
|
struct passwd *pw;
|
|
|
|
struct group *gr;
|
|
|
|
int ch;
|
2025-02-14 23:53:10 +01:00
|
|
|
int verbose = 0, daemonize = 1;
|
2025-02-14 17:39:57 +01:00
|
|
|
|
|
|
|
log_init(2, LOG_DAEMON);
|
|
|
|
|
|
|
|
agentx_log_fatal = fatalx;
|
|
|
|
agentx_log_warn = log_warnx;
|
|
|
|
agentx_log_info = log_info;
|
|
|
|
agentx_log_debug = log_debug;
|
|
|
|
|
2025-02-14 23:53:10 +01:00
|
|
|
while ((ch = getopt(argc, argv, "dv")) != -1) {
|
2025-02-14 17:39:57 +01:00
|
|
|
switch (ch) {
|
|
|
|
case 'd':
|
|
|
|
daemonize = 0;
|
|
|
|
break;
|
|
|
|
case 'v':
|
|
|
|
verbose++;
|
|
|
|
break;
|
|
|
|
default:
|
2025-02-15 00:00:14 +01:00
|
|
|
fatalx("usage: subagent_ntpd [-dv]\n");
|
2025-02-14 17:39:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((pw = getpwnam("_snmpd")) == NULL)
|
|
|
|
fatal("can't find _snmpd user");
|
|
|
|
if ((gr = getgrnam("_agentx")) == NULL)
|
|
|
|
fatal("can't find _agentx group");
|
|
|
|
|
|
|
|
event_init();
|
|
|
|
|
|
|
|
if ((sa = agentx(snmp_connect, NULL)) == NULL)
|
|
|
|
fatal("agentx");
|
2025-02-14 23:53:10 +01:00
|
|
|
if ((sas = agentx_session(sa, NULL, 0, "Hottis NTP Metrics for OpenNTPD", 0)) == NULL)
|
2025-02-14 17:39:57 +01:00
|
|
|
fatal("agentx_session");
|
2025-02-14 23:53:10 +01:00
|
|
|
if ((sac = agentx_context(sas, NULL)) == NULL)
|
2025-02-14 17:39:57 +01:00
|
|
|
fatal("agentx_context");
|
|
|
|
|
|
|
|
|
|
|
|
/* Can't pledge: kvm_getfiles */
|
|
|
|
if (unveil(NULL, NULL) == -1)
|
|
|
|
fatal("unveil");
|
|
|
|
|
|
|
|
if (setgid(gr->gr_gid) == -1)
|
|
|
|
fatal("setgid");
|
|
|
|
if (setuid(pw->pw_uid) == -1)
|
|
|
|
fatal("setuid");
|
|
|
|
|
2025-02-14 23:53:10 +01:00
|
|
|
mib_register(sac);
|
2025-02-14 17:39:57 +01:00
|
|
|
|
|
|
|
if (daemonize) {
|
|
|
|
log_init(0, LOG_DAEMON);
|
|
|
|
daemon(0, 0);
|
|
|
|
}
|
|
|
|
log_setverbose(verbose);
|
|
|
|
|
|
|
|
event_dispatch();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
snmp_connect(struct agentx *sa, void *cookie, int close)
|
|
|
|
{
|
|
|
|
if (close) {
|
|
|
|
event_del(&connev);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2025-02-14 23:53:10 +01:00
|
|
|
snmp_tryconnect(-1, 0, sa);
|
2025-02-14 17:39:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
snmp_tryconnect(int fd, short event, void *cookie)
|
|
|
|
{
|
|
|
|
struct timeval timeout = {3, 0};
|
|
|
|
struct agentx *sa = cookie;
|
|
|
|
struct sockaddr_un sun;
|
|
|
|
|
|
|
|
sun.sun_len = sizeof(sun);
|
|
|
|
sun.sun_family = AF_UNIX;
|
|
|
|
strlcpy(sun.sun_path, AGENTX_MASTER_PATH, sizeof(sun.sun_path));
|
|
|
|
|
|
|
|
if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1 ||
|
|
|
|
connect(fd, (struct sockaddr *)&sun, sizeof(sun)) == -1) {
|
|
|
|
if (fd != -1)
|
|
|
|
close(fd);
|
|
|
|
log_warn("Failed to connect to snmpd");
|
|
|
|
evtimer_set(&connev, snmp_tryconnect, sa);
|
|
|
|
evtimer_add(&connev, &timeout);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
event_set(&connev, fd, EV_READ | EV_PERSIST, snmp_read, sa);
|
|
|
|
event_add(&connev, NULL);
|
|
|
|
|
|
|
|
agentx_connect(sa, fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
snmp_read(int fd, short event, void *cookie)
|
|
|
|
{
|
|
|
|
struct agentx *sa = cookie;
|
|
|
|
|
|
|
|
agentx_read(sa);
|
|
|
|
}
|
|
|
|
|