From 60cc2743c98d898e323f643ffde7a30d2e7eacd5 Mon Sep 17 00:00:00 2001 From: martijn Date: Sat, 4 Nov 2023 09:38:47 +0000 Subject: [PATCH] Move SNMPv2-MIB::system from mib.c into the new application_internal.c. This was the last consumer of mib.c, so send it to the great bitbucket in the sky. This move removes the sysORTable, but it was basically non-functioning anyway and removal of previous entries from the table didn't caused any fallout that reached my ears. I hope to add proper sysORTable/agent-capabilities support, which is compatible with AgentX's {Add,Remove}AgentCaps, soon. OK tb@ --- usr.sbin/snmpd/Makefile | 4 +- usr.sbin/snmpd/application_internal.c | 36 +++- usr.sbin/snmpd/mib.c | 237 -------------------------- usr.sbin/snmpd/parse.y | 107 ++++++++++-- usr.sbin/snmpd/smi.c | 5 +- usr.sbin/snmpd/snmpd.h | 12 +- 6 files changed, 145 insertions(+), 256 deletions(-) delete mode 100644 usr.sbin/snmpd/mib.c diff --git a/usr.sbin/snmpd/Makefile b/usr.sbin/snmpd/Makefile index 26f2ff8aeb6..1bc8ed521b8 100644 --- a/usr.sbin/snmpd/Makefile +++ b/usr.sbin/snmpd/Makefile @@ -1,11 +1,11 @@ -# $OpenBSD: Makefile,v 1.23 2023/11/04 09:22:52 martijn Exp $ +# $OpenBSD: Makefile,v 1.24 2023/11/04 09:38:47 martijn Exp $ PROG= snmpd MAN= snmpd.8 snmpd.conf.5 SRCS= parse.y log.c snmpe.c application.c application_legacy.c \ application_blocklist.c application_internal.c \ application_agentx.c ax.c \ - mps.c trap.c mib.c smi.c snmpd.c \ + mps.c trap.c smi.c snmpd.c \ proc.c usm.c traphandler.c util.c LDADD= -levent -lutil -lcrypto diff --git a/usr.sbin/snmpd/application_internal.c b/usr.sbin/snmpd/application_internal.c index cf6721a30ac..00380091e59 100644 --- a/usr.sbin/snmpd/application_internal.c +++ b/usr.sbin/snmpd/application_internal.c @@ -1,4 +1,4 @@ -/* $OpenBSD: application_internal.c,v 1.3 2023/11/04 09:30:28 martijn Exp $ */ +/* $OpenBSD: application_internal.c,v 1.4 2023/11/04 09:38:47 martijn Exp $ */ /* * Copyright (c) 2023 Martijn van Duren @@ -47,6 +47,7 @@ void appl_internal_getnext(struct appl_backend *, int32_t, int32_t, struct ber_element *appl_internal_snmp(struct ber_oid *); struct ber_element *appl_internal_engine(struct ber_oid *); struct ber_element *appl_internal_usmstats(struct ber_oid *); +struct ber_element *appl_internal_system(struct ber_oid *); struct appl_internal_object *appl_internal_object_parent(struct ber_oid *); int appl_internal_object_cmp(struct appl_internal_object *, struct appl_internal_object *); @@ -73,6 +74,15 @@ RB_PROTOTYPE_STATIC(appl_internal_objects, appl_internal_object, entry, void appl_internal_init(void) { + appl_internal_region(&OID(MIB_system)); + appl_internal_object(&OID(MIB_sysDescr), appl_internal_system, NULL); + appl_internal_object(&OID(MIB_sysOID), appl_internal_system, NULL); + appl_internal_object(&OID(MIB_sysUpTime), appl_internal_system, NULL); + appl_internal_object(&OID(MIB_sysContact), appl_internal_system, NULL); + appl_internal_object(&OID(MIB_sysName), appl_internal_system, NULL); + appl_internal_object(&OID(MIB_sysLocation), appl_internal_system, NULL); + appl_internal_object(&OID(MIB_sysServices), appl_internal_system, NULL); + appl_internal_region(&OID(MIB_snmp)); appl_internal_object(&OID(MIB_snmpInPkts), appl_internal_snmp, NULL); appl_internal_object(&OID(MIB_snmpOutPkts), appl_internal_snmp, NULL); @@ -441,6 +451,30 @@ appl_internal_usmstats(struct ber_oid *oid) return value; } +struct ber_element * +appl_internal_system(struct ber_oid *oid) +{ + struct snmp_system *s = &snmpd_env->sc_system; + struct ber_element *value = NULL; + + if (ober_oid_cmp(&OID(MIB_sysDescr, 0), oid) == 0) + return ober_add_string(NULL, s->sys_descr); + else if (ober_oid_cmp(&OID(MIB_sysOID, 0), oid) == 0) + return ober_add_oid(NULL, &s->sys_oid); + else if (ober_oid_cmp(&OID(MIB_sysUpTime, 0), oid) == 0) { + value = ober_add_integer(NULL, smi_getticks()); + ober_set_header(value, BER_CLASS_APPLICATION, SNMP_T_TIMETICKS); + } else if (ober_oid_cmp(&OID(MIB_sysContact, 0), oid) == 0) + return ober_add_string(NULL, s->sys_contact); + else if (ober_oid_cmp(&OID(MIB_sysName, 0), oid) == 0) + return ober_add_string(NULL, s->sys_name); + else if (ober_oid_cmp(&OID(MIB_sysLocation, 0), oid) == 0) + return ober_add_string(NULL, s->sys_location); + else if (ober_oid_cmp(&OID(MIB_sysServices, 0), oid) == 0) + return ober_add_integer(NULL, s->sys_services); + return value; +} + struct appl_internal_object * appl_internal_object_parent(struct ber_oid *oid) { diff --git a/usr.sbin/snmpd/mib.c b/usr.sbin/snmpd/mib.c deleted file mode 100644 index 8e39c2f25aa..00000000000 --- a/usr.sbin/snmpd/mib.c +++ /dev/null @@ -1,237 +0,0 @@ -/* $OpenBSD: mib.c,v 1.107 2023/11/04 09:30:28 martijn Exp $ */ - -/* - * Copyright (c) 2012 Joel Knight - * Copyright (c) 2007, 2008, 2012 Reyk Floeter - * - * 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 -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "snmpd.h" -#include "mib.h" - -/* - * Defined in SNMPv2-MIB.txt (RFC 3418) - */ - -int mib_getsys(struct oid *, struct ber_oid *, struct ber_element **); -int mib_sysor(struct oid *, struct ber_oid *, struct ber_element **); - -static struct oid mib_tree[] = MIB_TREE; - -/* base MIB tree */ -static struct oid base_mib[] = { - { MIB(mib_2), OID_MIB }, - { MIB(sysDescr), OID_RD, mib_getsys }, - { MIB(sysOID), OID_RD, mib_getsys }, - { MIB(sysUpTime), OID_RD, mib_getsys }, - { MIB(sysContact), OID_RD, mib_getsys }, - { MIB(sysName), OID_RD, mib_getsys }, - { MIB(sysLocation), OID_RD, mib_getsys }, - { MIB(sysServices), OID_RS, mib_getsys }, - { MIB(sysORLastChange), OID_RD, mps_getts }, - { MIB(sysORIndex), OID_TRD, mib_sysor }, - { MIB(sysORID), OID_TRD, mib_sysor }, - { MIB(sysORDescr), OID_TRD, mib_sysor }, - { MIB(sysORUpTime), OID_TRD, mib_sysor }, - { MIBEND } -}; - -int -mib_getsys(struct oid *oid, struct ber_oid *o, struct ber_element **elm) -{ - struct ber_oid sysoid = OID(MIB_SYSOID_DEFAULT); - char *s = oid->o_data; - struct ber_oid *so = oid->o_data; - struct utsname u; - long long ticks; - - if (uname(&u) == -1) - return (-1); - - switch (oid->o_oid[OIDIDX_system]) { - case 1: - if (s == NULL) { - if (asprintf(&s, "%s %s %s %s %s", - u.sysname, u.nodename, u.release, - u.version, u.machine) == -1) - return (-1); - oid->o_data = s; - oid->o_val = strlen(s); - } - *elm = ober_add_string(*elm, s); - break; - case 2: - if (so == NULL) - so = &sysoid; - smi_oidlen(so); - *elm = ober_add_oid(*elm, so); - break; - case 3: - ticks = smi_getticks(); - *elm = ober_add_integer(*elm, ticks); - ober_set_header(*elm, BER_CLASS_APPLICATION, SNMP_T_TIMETICKS); - break; - case 4: - if (s == NULL) { - if (asprintf(&s, "root@%s", u.nodename) == -1) - return (-1); - oid->o_data = s; - oid->o_val = strlen(s); - } - *elm = ober_add_string(*elm, s); - break; - case 5: - if (s == NULL) { - if ((s = strdup(u.nodename)) == NULL) - return (-1); - oid->o_data = s; - oid->o_val = strlen(s); - } - *elm = ober_add_string(*elm, s); - break; - case 6: - if (s == NULL) - s = ""; - *elm = ober_add_string(*elm, s); - break; - case 7: - *elm = ober_add_integer(*elm, oid->o_val); - break; - default: - return (-1); - } - return (0); -} - -int -mib_sysor(struct oid *oid, struct ber_oid *o, struct ber_element **elm) -{ - struct ber_element *ber = *elm; - u_int32_t idx = 1, nmib = 0; - struct oid *next, *miboid; - char buf[SNMPD_MAXSTRLEN]; - - /* Count MIB root OIDs in the tree */ - for (next = NULL; - (next = smi_foreach(next, OID_MIB)) != NULL; nmib++); - - /* Get and verify the current row index */ - idx = o->bo_id[OIDIDX_sysOREntry]; - if (idx > nmib) - return (1); - - /* Find the MIB root element for this Id */ - for (next = miboid = NULL, nmib = 1; - (next = smi_foreach(next, OID_MIB)) != NULL; nmib++) { - if (nmib == idx) - miboid = next; - } - if (miboid == NULL) - return (-1); - - /* Tables need to prepend the OID on their own */ - ber = ober_add_oid(ber, o); - - switch (o->bo_id[OIDIDX_sysOR]) { - case 1: - ber = ober_add_integer(ber, idx); - break; - case 2: - ber = ober_add_oid(ber, &miboid->o_id); - break; - case 3: - /* - * This should be a description of the MIB. - * But we use the symbolic OID string for now, it may - * help to display names of internal OIDs. - */ - smi_oid2string(&miboid->o_id, buf, sizeof(buf), 0); - ber = ober_add_string(ber, buf); - break; - case 4: - /* - * We do not support dynamic loading of MIB at runtime, - * the sysORUpTime value of 0 will indicate "loaded at - * startup". - */ - ber = ober_add_integer(ber, 0); - ober_set_header(ber, - BER_CLASS_APPLICATION, SNMP_T_TIMETICKS); - break; - default: - return (-1); - } - - return (0); -} - -/* - * Import all MIBs - */ - -void -mib_init(void) -{ - /* - * MIB declarations (to register the OID names) - */ - smi_mibtree(mib_tree); - - /* - * MIB definitions (the implementation) - */ - - /* SNMPv2-MIB */ - smi_mibtree(base_mib); -} diff --git a/usr.sbin/snmpd/parse.y b/usr.sbin/snmpd/parse.y index 8f9bad07a8c..deded9ab0cf 100644 --- a/usr.sbin/snmpd/parse.y +++ b/usr.sbin/snmpd/parse.y @@ -1,4 +1,4 @@ -/* $OpenBSD: parse.y,v 1.79 2023/10/24 08:41:20 martijn Exp $ */ +/* $OpenBSD: parse.y,v 1.80 2023/11/04 09:38:47 martijn Exp $ */ /* * Copyright (c) 2007, 2008, 2012 Reyk Floeter @@ -28,6 +28,7 @@ #include #include #include +#include #include #include @@ -760,28 +761,87 @@ system : SYSTEM sysmib ; sysmib : CONTACT STRING { - struct ber_oid o = OID(MIB_sysContact); - mps_set(&o, $2, strlen($2)); + if (conf->sc_system.sys_contact[0] != '\0') { + yyerror("system contact already defined"); + free($2); + YYERROR; + } + if (strlcpy(conf->sc_system.sys_contact, $2, + sizeof(conf->sc_system.sys_contact)) >= + sizeof(conf->sc_system.sys_contact)) { + yyerror("system contact too long"); + free($2); + YYERROR; + } + free($2); } | DESCR STRING { - struct ber_oid o = OID(MIB_sysDescr); - mps_set(&o, $2, strlen($2)); + if (conf->sc_system.sys_descr[0] != '\0') { + yyerror("system description already defined"); + free($2); + YYERROR; + } + if (strlcpy(conf->sc_system.sys_descr, $2, + sizeof(conf->sc_system.sys_descr)) >= + sizeof(conf->sc_system.sys_descr)) { + yyerror("system description too long"); + free($2); + YYERROR; + } + free($2); } | LOCATION STRING { - struct ber_oid o = OID(MIB_sysLocation); - mps_set(&o, $2, strlen($2)); + if (conf->sc_system.sys_location[0] != '\0') { + yyerror("system location already defined"); + free($2); + YYERROR; + } + if (strlcpy(conf->sc_system.sys_location, $2, + sizeof(conf->sc_system.sys_location)) >= + sizeof(conf->sc_system.sys_location)) { + yyerror("system location too long"); + free($2); + YYERROR; + } + free($2); } | NAME STRING { - struct ber_oid o = OID(MIB_sysName); - mps_set(&o, $2, strlen($2)); + if (conf->sc_system.sys_name[0] != '\0') { + yyerror("system name already defined"); + free($2); + YYERROR; + } + if (strlcpy(conf->sc_system.sys_name, $2, + sizeof(conf->sc_system.sys_name)) >= + sizeof(conf->sc_system.sys_name)) { + yyerror("system name too long"); + free($2); + YYERROR; + } + free($2); } | OBJECTID oid { - struct ber_oid o = OID(MIB_sysOID); - mps_set(&o, $2, sizeof(struct ber_oid)); + if (conf->sc_system.sys_oid.bo_n != 0) { + yyerror("system oid already defined"); + free($2); + YYERROR; + } + conf->sc_system.sys_oid = *$2; + free($2); } | SERVICES NUMBER { - struct ber_oid o = OID(MIB_sysServices); - mps_set(&o, NULL, $2); + if (conf->sc_system.sys_services != -1) { + yyerror("system services already defined"); + YYERROR; + } + if ($2 < 0) { + yyerror("system services too small"); + YYERROR; + } else if ($2 > 127) { + yyerror("system services too large"); + YYERROR; + } + conf->sc_system.sys_services = $2; } ; @@ -1575,6 +1635,7 @@ struct snmpd * parse_config(const char *filename, u_int flags) { struct sockaddr_storage ss; + struct utsname u; struct sym *sym, *next; struct address *h; struct trap_address *tr; @@ -1589,6 +1650,7 @@ parse_config(const char *filename, u_int flags) return (NULL); } + conf->sc_system.sys_services = -1; conf->sc_flags = flags; conf->sc_confpath = filename; TAILQ_INIT(&conf->sc_addresses); @@ -1609,6 +1671,25 @@ parse_config(const char *filename, u_int flags) endservent(); + if (uname(&u) == -1) + fatal("uname"); + + if (conf->sc_system.sys_descr[0] == '\0') + snprintf(conf->sc_system.sys_descr, + sizeof(conf->sc_system.sys_descr), "%s %s %s %s %s", + u.sysname, u.nodename, u.release, u.version, u.machine); + if (conf->sc_system.sys_oid.bo_n == 0) + conf->sc_system.sys_oid = OID(MIB_SYSOID_DEFAULT); + if (conf->sc_system.sys_contact[0] == '\0') + snprintf(conf->sc_system.sys_contact, + sizeof(conf->sc_system.sys_contact), "root@%s", u.nodename); + if (conf->sc_system.sys_name[0] == '\0') + snprintf(conf->sc_system.sys_name, + sizeof(conf->sc_system.sys_name), "%s", u.nodename); + if (conf->sc_system.sys_services == -1) + conf->sc_system.sys_services = 0; + + /* Must be identical to enginefmt_local:HOSTHASH */ if (conf->sc_engineid_len == 0) { if (gethostname(hostname, sizeof(hostname)) == -1) diff --git a/usr.sbin/snmpd/smi.c b/usr.sbin/snmpd/smi.c index 3a5b3badf74..5033b45cb9e 100644 --- a/usr.sbin/snmpd/smi.c +++ b/usr.sbin/snmpd/smi.c @@ -1,4 +1,4 @@ -/* $OpenBSD: smi.c,v 1.32 2022/10/06 14:41:08 martijn Exp $ */ +/* $OpenBSD: smi.c,v 1.33 2023/11/04 09:38:47 martijn Exp $ */ /* * Copyright (c) 2007, 2008 Reyk Floeter @@ -53,6 +53,7 @@ RB_HEAD(oidtree, oid); RB_PROTOTYPE(oidtree, oid, o_element, smi_oid_cmp); struct oidtree smi_oidtree; +static struct oid smi_objects[] = MIB_TREE; RB_HEAD(keytree, oid); RB_PROTOTYPE(keytree, oid, o_keyword, smi_key_cmp); @@ -231,7 +232,7 @@ smi_init(void) { /* Initialize the Structure of Managed Information (SMI) */ RB_INIT(&smi_oidtree); - mib_init(); + smi_mibtree(smi_objects); return (0); } diff --git a/usr.sbin/snmpd/snmpd.h b/usr.sbin/snmpd/snmpd.h index 3c46a451b1e..b90e4cb17ee 100644 --- a/usr.sbin/snmpd/snmpd.h +++ b/usr.sbin/snmpd/snmpd.h @@ -1,4 +1,4 @@ -/* $OpenBSD: snmpd.h,v 1.107 2023/11/04 09:22:52 martijn Exp $ */ +/* $OpenBSD: snmpd.h,v 1.108 2023/11/04 09:38:47 martijn Exp $ */ /* * Copyright (c) 2007, 2008, 2012 Reyk Floeter @@ -428,6 +428,15 @@ struct usmuser { SLIST_ENTRY(usmuser) uu_next; }; +struct snmp_system { + char sys_descr[256]; + struct ber_oid sys_oid; + char sys_contact[256]; + char sys_name[256]; + char sys_location[256]; + int8_t sys_services; +}; + struct snmpd { u_int8_t sc_flags; #define SNMPD_F_VERBOSE 0x01 @@ -448,6 +457,7 @@ struct snmpd { size_t sc_engineid_len; struct snmp_stats sc_stats; + struct snmp_system sc_system; struct trap_addresslist sc_trapreceivers; -- 2.20.1