From f0bcdb5c6239ddfcf095a4b7b5b5205416faadb1 Mon Sep 17 00:00:00 2001 From: martijn Date: Tue, 6 Feb 2024 12:44:27 +0000 Subject: [PATCH] Replace most smi_oid2string() calls with the new mib_oid2string(). smi_oid2string() is still called from trap handle context to not break any existing scripts. OK tb@ --- usr.sbin/snmpd/application.c | 77 ++++++++++++++++----------- usr.sbin/snmpd/application_agentx.c | 5 +- usr.sbin/snmpd/application_internal.c | 12 ++--- usr.sbin/snmpd/parse.y | 4 +- usr.sbin/snmpd/smi.c | 6 +-- usr.sbin/snmpd/snmpd.h | 4 +- usr.sbin/snmpd/trap.c | 4 +- usr.sbin/snmpd/traphandler.c | 12 +++-- 8 files changed, 74 insertions(+), 50 deletions(-) diff --git a/usr.sbin/snmpd/application.c b/usr.sbin/snmpd/application.c index 973856aadeb..f6bd6096449 100644 --- a/usr.sbin/snmpd/application.c +++ b/usr.sbin/snmpd/application.c @@ -1,4 +1,4 @@ -/* $OpenBSD: application.c,v 1.41 2023/12/21 12:43:30 martijn Exp $ */ +/* $OpenBSD: application.c,v 1.42 2024/02/06 12:44:27 martijn Exp $ */ /* * Copyright (c) 2021 Martijn van Duren @@ -31,10 +31,11 @@ #include "application.h" #include "log.h" +#include "mib.h" #include "smi.h" #include "snmp.h" +#include "snmpd.h" #include "snmpe.h" -#include "mib.h" #define OID(...) (struct ber_oid){ { __VA_ARGS__ }, \ (sizeof((uint32_t []) { __VA_ARGS__ }) / sizeof(uint32_t)) } @@ -135,7 +136,7 @@ struct snmp_target_mib { void appl_agentcap_free(struct appl_agentcap *); enum appl_error appl_region(struct appl_context *, uint32_t, uint8_t, - struct ber_oid *, int, int, struct appl_backend *); + struct ber_oid *, uint8_t, int, int, struct appl_backend *); void appl_region_free(struct appl_context *, struct appl_region *); enum appl_error appl_region_unregister_match(struct appl_context *, uint8_t, struct ber_oid *, char *, struct appl_backend *, int); @@ -248,7 +249,7 @@ appl_addagentcaps(const char *ctxname, struct ber_oid *oid, const char *descr, if (ctxname == NULL) ctxname = ""; - (void)smi_oid2string(oid, oidbuf, sizeof(oidbuf), 0); + mib_oid2string(oid, oidbuf, sizeof(oidbuf), snmpd_env->sc_oidfmt); log_info("%s: Adding agent capabilities %s context(%s)", backend->ab_name, oidbuf, ctxname); @@ -297,7 +298,7 @@ appl_removeagentcaps(const char *ctxname, struct ber_oid *oid, if (ctxname == NULL) ctxname = ""; - (void)smi_oid2string(oid, oidbuf, sizeof(oidbuf), 0); + mib_oid2string(oid, oidbuf, sizeof(oidbuf), snmpd_env->sc_oidfmt); log_info("%s: Removing agent capabilities %s context(%s)", backend->ab_name, oidbuf, ctxname); @@ -449,18 +450,24 @@ appl_targetmib(struct ber_oid *oid) enum appl_error appl_region(struct appl_context *ctx, uint32_t timeout, uint8_t priority, - struct ber_oid *oid, int instance, int subtree, + struct ber_oid *oid, uint8_t range_subid, int instance, int subtree, struct appl_backend *backend) { struct appl_region *region = NULL, *nregion; char oidbuf[1024], regionbuf[1024], subidbuf[11]; - size_t i; - - /* Don't use smi_oid2string, because appl_register can't use it */ - oidbuf[0] = '\0'; - for (i = 0; i < oid->bo_n; i++) { - if (i != 0) - strlcat(oidbuf, ".", sizeof(oidbuf)); + size_t i, bo_n; + + bo_n = oid->bo_n; + if (range_subid != 0) + oid->bo_n = range_subid; + mib_oid2string(oid, oidbuf, sizeof(oidbuf), snmpd_env->sc_oidfmt); + if (range_subid != 0) { + oid->bo_n = bo_n; + i = range_subid + 1; + } else + i = oid->bo_n; + for (; i < oid->bo_n; i++) { + strlcat(oidbuf, ".", sizeof(oidbuf)); snprintf(subidbuf, sizeof(subidbuf), "%"PRIu32, oid->bo_id[i]); strlcat(oidbuf, subidbuf, sizeof(oidbuf)); @@ -539,15 +546,21 @@ appl_register(const char *ctxname, uint32_t timeout, uint8_t priority, struct appl_region *region, search; char oidbuf[1024], subidbuf[11]; enum appl_error error; - size_t i; + size_t i, bo_n; uint32_t lower_bound; - oidbuf[0] = '\0'; - /* smi_oid2string can't do ranges */ - for (i = 0; i < oid->bo_n; i++) { + bo_n = oid->bo_n; + if (range_subid != 0) + oid->bo_n = range_subid; + mib_oid2string(oid, oidbuf, sizeof(oidbuf), snmpd_env->sc_oidfmt); + if (range_subid != 0) { + oid->bo_n = bo_n; + i = range_subid + 1; + } else + i = oid->bo_n; + for (; i < oid->bo_n; i++) { + strlcat(oidbuf, ".", sizeof(oidbuf)); snprintf(subidbuf, sizeof(subidbuf), "%"PRIu32, oid->bo_id[i]); - if (i != 0) - strlcat(oidbuf, ".", sizeof(oidbuf)); if (range_subid == i + 1) { strlcat(oidbuf, "[", sizeof(oidbuf)); strlcat(oidbuf, subidbuf, sizeof(oidbuf)); @@ -587,8 +600,8 @@ appl_register(const char *ctxname, uint32_t timeout, uint8_t priority, } if (range_subid == 0) - return appl_region(ctx, timeout, priority, oid, instance, - subtree, backend); + return appl_region(ctx, timeout, priority, oid, range_subid, + instance, subtree, backend); range_subid--; if (range_subid >= oid->bo_n) { @@ -604,12 +617,13 @@ appl_register(const char *ctxname, uint32_t timeout, uint8_t priority, lower_bound = oid->bo_id[range_subid]; do { - if ((error = appl_region(ctx, timeout, priority, oid, instance, - subtree, backend)) != APPL_ERROR_NOERROR) + if ((error = appl_region(ctx, timeout, priority, oid, + range_subid, instance, subtree, + backend)) != APPL_ERROR_NOERROR) goto fail; } while (oid->bo_id[range_subid]++ != upper_bound); - if ((error = appl_region(ctx, timeout, priority, oid, instance, subtree, - backend)) != APPL_ERROR_NOERROR) + if ((error = appl_region(ctx, timeout, priority, oid, range_subid, + instance, subtree, backend)) != APPL_ERROR_NOERROR) goto fail; return APPL_ERROR_NOERROR; @@ -1311,8 +1325,8 @@ appl_response(struct appl_backend *backend, int32_t requestid, for (i = 1; vb != NULL; vb = vb->av_next, i++) { if (!appl_varbind_valid(vb, origvb, next, error != APPL_ERROR_NOERROR, backend->ab_range, &errstr)) { - smi_oid2string(&(vb->av_oid), oidbuf, - sizeof(oidbuf), 0); + mib_oid2string(&(vb->av_oid), oidbuf, sizeof(oidbuf), + snmpd_env->sc_oidfmt); log_warnx("%s: %"PRIu32" %s: %s", backend->ab_name, requestid, oidbuf, errstr); invalid = 1; @@ -1756,15 +1770,16 @@ appl_pdu_log(struct appl_backend *backend, enum snmp_pdutype pdutype, buf[0] = '\0'; for (vb = vblist; vb != NULL; vb = vb->av_next) { strlcat(buf, "{", sizeof(buf)); - strlcat(buf, smi_oid2string(&(vb->av_oid), oidbuf, - sizeof(oidbuf), 0), sizeof(buf)); + strlcat(buf, mib_oid2string(&(vb->av_oid), oidbuf, + sizeof(oidbuf), snmpd_env->sc_oidfmt), sizeof(buf)); if (next) { if (vb->av_include) strlcat(buf, "(incl)", sizeof(buf)); if (vb->av_oid_end.bo_n > 0) { strlcat(buf, "-", sizeof(buf)); - strlcat(buf, smi_oid2string(&(vb->av_oid_end), - oidbuf, sizeof(oidbuf), 0), sizeof(buf)); + strlcat(buf, mib_oid2string(&(vb->av_oid_end), + oidbuf, sizeof(oidbuf), + snmpd_env->sc_oidfmt), sizeof(buf)); } } strlcat(buf, ":", sizeof(buf)); diff --git a/usr.sbin/snmpd/application_agentx.c b/usr.sbin/snmpd/application_agentx.c index 44e50438f6a..cf6c628e9b5 100644 --- a/usr.sbin/snmpd/application_agentx.c +++ b/usr.sbin/snmpd/application_agentx.c @@ -1,4 +1,4 @@ -/* $OpenBSD: application_agentx.c,v 1.15 2023/12/21 12:43:30 martijn Exp $ */ +/* $OpenBSD: application_agentx.c,v 1.16 2024/02/06 12:44:27 martijn Exp $ */ /* * Copyright (c) 2022 Martijn van Duren * @@ -34,6 +34,7 @@ #include "application.h" #include "ax.h" #include "log.h" +#include "mib.h" #include "smi.h" #include "snmp.h" #include "snmpd.h" @@ -558,7 +559,7 @@ appl_agentx_open(struct appl_agentx_connection *conn, struct ax_pdu *pdu) TAILQ_INSERT_TAIL(&(conn->conn_sessions), session, sess_conn_entry); appl_agentx_oid2ber_oid(&(session->sess_oid), &oid); - smi_oid2string(&oid, oidbuf, sizeof(oidbuf), 0); + mib_oid2string(&oid, oidbuf, sizeof(oidbuf), snmpd_env->sc_oidfmt); log_info("%s: %s %s: Open", session->sess_backend.ab_name, oidbuf, session->sess_descr.aos_string); diff --git a/usr.sbin/snmpd/application_internal.c b/usr.sbin/snmpd/application_internal.c index 9d9166cbe54..95032aea33b 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.11 2023/12/21 12:43:31 martijn Exp $ */ +/* $OpenBSD: application_internal.c,v 1.12 2024/02/06 12:44:27 martijn Exp $ */ /* * Copyright (c) 2023 Martijn van Duren @@ -243,10 +243,9 @@ appl_internal_region(struct ber_oid *oid) * Ignore requestDenied, duplicateRegistration, and unsupportedContext */ if (error == APPL_ERROR_PROCESSINGERROR || - error == APPL_ERROR_PARSEERROR) { - smi_oid2string(oid, oidbuf, sizeof(oidbuf), 0); - fatalx("internal: Failed to register %s", oidbuf); - } + error == APPL_ERROR_PARSEERROR) + fatalx("internal: Failed to register %s", mib_oid2string(oid, + oidbuf, sizeof(oidbuf), snmpd_env->sc_oidfmt)); } void @@ -267,7 +266,8 @@ appl_internal_object(struct ber_oid *oid, if (RB_INSERT(appl_internal_objects, &appl_internal_objects, obj) != NULL) fatalx("%s: %s already registered", __func__, - smi_oid2string(oid, buf, sizeof(buf), 0)); + mib_oid2string(oid, buf, sizeof(buf), + snmpd_env->sc_oidfmt)); } const char * diff --git a/usr.sbin/snmpd/parse.y b/usr.sbin/snmpd/parse.y index dd2456c1bf0..6eeb5c8fbfd 100644 --- a/usr.sbin/snmpd/parse.y +++ b/usr.sbin/snmpd/parse.y @@ -1,4 +1,4 @@ -/* $OpenBSD: parse.y,v 1.87 2024/02/06 12:39:13 martijn Exp $ */ +/* $OpenBSD: parse.y,v 1.88 2024/02/06 12:44:27 martijn Exp $ */ /* * Copyright (c) 2007, 2008, 2012 Reyk Floeter @@ -1662,6 +1662,8 @@ parse_config(const char *filename, u_int flags) conf->sc_system.sys_services = -1; conf->sc_flags = flags; + conf->sc_oidfmt = + flags & SNMPD_F_NONAMES ? MIB_OIDNUMERIC : MIB_OIDSYMBOLIC; conf->sc_confpath = filename; TAILQ_INIT(&conf->sc_addresses); TAILQ_INIT(&conf->sc_agentx_masters); diff --git a/usr.sbin/snmpd/smi.c b/usr.sbin/snmpd/smi.c index cdd3044c48b..09a34fafd0b 100644 --- a/usr.sbin/snmpd/smi.c +++ b/usr.sbin/snmpd/smi.c @@ -1,4 +1,4 @@ -/* $OpenBSD: smi.c,v 1.39 2023/12/21 12:43:31 martijn Exp $ */ +/* $OpenBSD: smi.c,v 1.40 2024/02/06 12:44:27 martijn Exp $ */ /* * Copyright (c) 2007, 2008 Reyk Floeter @@ -470,8 +470,8 @@ smi_print_element(struct ber_element *root) case BER_TYPE_OBJECT: if (ober_get_oid(root, &o) == -1) goto fail; - if (asprintf(&str, "%s", smi_oid2string(&o, strbuf, - sizeof(strbuf), 0)) == -1) + if (asprintf(&str, "%s", mib_oid2string(&o, strbuf, + sizeof(strbuf), snmpd_env->sc_oidfmt)) == -1) goto fail; break; case BER_TYPE_OCTETSTRING: diff --git a/usr.sbin/snmpd/snmpd.h b/usr.sbin/snmpd/snmpd.h index a25ad588083..300b91f0bc8 100644 --- a/usr.sbin/snmpd/snmpd.h +++ b/usr.sbin/snmpd/snmpd.h @@ -1,4 +1,4 @@ -/* $OpenBSD: snmpd.h,v 1.117 2024/01/16 13:33:12 claudio Exp $ */ +/* $OpenBSD: snmpd.h,v 1.118 2024/02/06 12:44:28 martijn Exp $ */ /* * Copyright (c) 2007, 2008, 2012 Reyk Floeter @@ -36,6 +36,7 @@ #include #include +#include "mib.h" #include "snmp.h" #ifndef nitems @@ -396,6 +397,7 @@ struct snmpd { #define SNMPD_F_VERBOSE 0x01 #define SNMPD_F_DEBUG 0x02 #define SNMPD_F_NONAMES 0x04 + enum mib_oidfmt sc_oidfmt; const char *sc_confpath; struct addresslist sc_addresses; diff --git a/usr.sbin/snmpd/trap.c b/usr.sbin/snmpd/trap.c index 94fe4a7b8d8..469b33098e9 100644 --- a/usr.sbin/snmpd/trap.c +++ b/usr.sbin/snmpd/trap.c @@ -1,4 +1,4 @@ -/* $OpenBSD: trap.c,v 1.41 2023/12/21 12:43:31 martijn Exp $ */ +/* $OpenBSD: trap.c,v 1.42 2024/02/06 12:44:28 martijn Exp $ */ /* * Copyright (c) 2008 Reyk Floeter @@ -54,7 +54,7 @@ trap_send(struct ber_oid *oid, struct ber_element *elm) if (TAILQ_EMPTY(&snmpd_env->sc_trapreceivers)) return (0); - smi_oid2string(oid, ostr, sizeof(ostr), 0); + mib_oid2string(oid, ostr, sizeof(ostr), snmpd_env->sc_oidfmt); log_debug("trap_send: oid %s", ostr); /* Add mandatory varbind elements */ diff --git a/usr.sbin/snmpd/traphandler.c b/usr.sbin/snmpd/traphandler.c index c22ea136307..681c035b3cf 100644 --- a/usr.sbin/snmpd/traphandler.c +++ b/usr.sbin/snmpd/traphandler.c @@ -1,4 +1,4 @@ -/* $OpenBSD: traphandler.c,v 1.25 2023/12/21 12:43:31 martijn Exp $ */ +/* $OpenBSD: traphandler.c,v 1.26 2024/02/06 12:44:28 martijn Exp $ */ /* * Copyright (c) 2014 Bret Stephen Lambert @@ -34,6 +34,7 @@ #include #include "log.h" +#include "mib.h" #include "smi.h" #include "snmp.h" #include "snmpd.h" @@ -332,7 +333,8 @@ trapcmd_exec(struct trapcmd *cmd, struct sockaddr *sa, if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, s) == -1) { log_warn("could not create pipe for OID '%s'", - smi_oid2string(cmd->cmd_oid, oidbuf, sizeof(oidbuf), 0)); + mib_oid2string(cmd->cmd_oid, oidbuf, sizeof(oidbuf), + snmpd_env->sc_oidfmt)); return; } @@ -350,13 +352,15 @@ trapcmd_exec(struct trapcmd *cmd, struct sockaddr *sa, /* this shouldn't happen */ log_warn("could not exec trap command for OID '%s'", - smi_oid2string(cmd->cmd_oid, oidbuf, sizeof(oidbuf), 0)); + mib_oid2string(cmd->cmd_oid, oidbuf, sizeof(oidbuf), + snmpd_env->sc_oidfmt)); _exit(1); /* NOTREACHED */ case -1: log_warn("could not fork trap command for OID '%s'", - smi_oid2string(cmd->cmd_oid, oidbuf, sizeof(oidbuf), 0)); + mib_oid2string(cmd->cmd_oid, oidbuf, sizeof(oidbuf), + snmpd_env->sc_oidfmt)); close(s[0]); close(s[1]); return; -- 2.20.1