From 72544ae4714a1cef726d960a7eaeaefcd6459caa Mon Sep 17 00:00:00 2001 From: Reyk Floeter Date: Wed, 16 Jan 2008 09:42:30 +0000 Subject: move trap code to an own file; there will be more --- usr.sbin/snmpd/Makefile | 4 +- usr.sbin/snmpd/control.c | 4 +- usr.sbin/snmpd/snmpd.h | 6 +- usr.sbin/snmpd/snmpe.c | 154 +------------------------------------ usr.sbin/snmpd/trap.c | 192 +++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 201 insertions(+), 159 deletions(-) create mode 100644 usr.sbin/snmpd/trap.c (limited to 'usr.sbin/snmpd') diff --git a/usr.sbin/snmpd/Makefile b/usr.sbin/snmpd/Makefile index 4e303733c40..b09519cfb13 100644 --- a/usr.sbin/snmpd/Makefile +++ b/usr.sbin/snmpd/Makefile @@ -1,9 +1,9 @@ -# $OpenBSD: Makefile,v 1.3 2007/12/28 16:59:31 reyk Exp $ +# $OpenBSD: Makefile,v 1.4 2008/01/16 09:42:29 reyk Exp $ PROG= snmpd MAN= snmpd.8 snmpd.conf.5 SRCS= parse.y ber.c log.c control.c buffer.c imsg.c snmpe.c \ - mps.c mib.c smi.c kroute.c snmpd.c + mps.c trap.c mib.c smi.c kroute.c snmpd.c LDADD= -levent DPADD= ${LIBEVENT} diff --git a/usr.sbin/snmpd/control.c b/usr.sbin/snmpd/control.c index a048f652fa5..8ee5be5d1b7 100644 --- a/usr.sbin/snmpd/control.c +++ b/usr.sbin/snmpd/control.c @@ -1,4 +1,4 @@ -/* $OpenBSD: control.c,v 1.2 2008/01/16 09:36:30 reyk Exp $ */ +/* $OpenBSD: control.c,v 1.3 2008/01/16 09:42:29 reyk Exp $ */ /* * Copyright (c) 2003, 2004 Henning Brauer @@ -231,7 +231,7 @@ control_dispatch_imsg(int fd, short event, void *arg) c->flags |= CTL_CONN_NOTIFY; break; case IMSG_SNMP_TRAP: - if (snmpe_trap(&c->ibuf, imsg.hdr.pid) == -1) { + if (trap_request(&c->ibuf, imsg.hdr.pid) == -1) { log_debug("control_dispatch_imsg: " "received invalid trap (pid %d)", imsg.hdr.pid); diff --git a/usr.sbin/snmpd/snmpd.h b/usr.sbin/snmpd/snmpd.h index 7499ea4231e..37867e60e66 100644 --- a/usr.sbin/snmpd/snmpd.h +++ b/usr.sbin/snmpd/snmpd.h @@ -1,4 +1,4 @@ -/* $OpenBSD: snmpd.h,v 1.11 2008/01/16 09:36:30 reyk Exp $ */ +/* $OpenBSD: snmpd.h,v 1.12 2008/01/16 09:42:29 reyk Exp $ */ /* * Copyright (c) 2007 Reyk Floeter @@ -387,7 +387,9 @@ struct kif_addr *kr_getnextaddr(struct in_addr *); /* snmpe.c */ pid_t snmpe(struct snmpd *, int [2]); -int snmpe_trap(struct imsgbuf *, pid_t); + +/* trap.c */ +int trap_request(struct imsgbuf *, pid_t); /* mps.c */ struct ber_element * diff --git a/usr.sbin/snmpd/snmpe.c b/usr.sbin/snmpd/snmpe.c index 44960a0abf7..9023020bb1a 100644 --- a/usr.sbin/snmpd/snmpe.c +++ b/usr.sbin/snmpd/snmpe.c @@ -1,4 +1,4 @@ -/* $OpenBSD: snmpe.c,v 1.11 2008/01/16 09:36:30 reyk Exp $ */ +/* $OpenBSD: snmpe.c,v 1.12 2008/01/16 09:42:29 reyk Exp $ */ /* * Copyright (c) 2007 Reyk Floeter @@ -804,155 +804,3 @@ snmpe_recvmsg(int fd, short sig, void *arg) if (resp != NULL) ber_free_elements(resp); } - -int -snmpe_trap(struct imsgbuf *ibuf, pid_t pid) -{ - struct imsg imsg; - int ret = -1, n, x = 0; - int done = 0; - struct snmp_imsg *sm; - u_int32_t d; - u_int64_t l; - u_int8_t *c; - char o[SNMP_MAX_OID_LEN]; - struct ber_element *ber, *trap = NULL, *oid = NULL, *a; - size_t len; - - ber = trap = ber_add_sequence(NULL); - - while (!done) { - while (!done) { - if ((n = imsg_get(ibuf, &imsg)) == -1) - goto done; - if (n == 0) - break; - switch (imsg.hdr.type) { - case IMSG_SNMP_ELEMENT: - if (imsg.hdr.len < (IMSG_HEADER_SIZE + - sizeof(struct snmp_imsg))) - goto imsgdone; - - sm = (struct snmp_imsg *)imsg.data; - - if (oid == NULL) { - /* First element must be the trap OID */ - if (sm->snmp_type != SNMP_NULL) - goto imsgdone; - ber = oid = ber_printf_elements(ber, - "{o0}", sm->snmp_oid); - break; - } - - ber = a = ber_add_sequence(ber); - a = ber_add_oidstring(a, sm->snmp_oid); - - switch (sm->snmp_type) { - case SNMP_OBJECT: - if (sm->snmp_len != sizeof(o)) - goto imsgdone; - bcopy(sm + 1, &o, sm->snmp_len); - a = ber_add_oidstring(a, o); - break; - case SNMP_BITSTRING: - case SNMP_OCTETSTRING: - if ((sm->snmp_len < 1) || - (sm->snmp_len >= SNMPD_MAXSTRLEN)) - goto imsgdone; - if ((c = - calloc(1, sm->snmp_len)) == NULL) - goto imsgdone; - bcopy(sm + 1, c, sm->snmp_len); - if (sm->snmp_type == SNMP_BITSTRING) - a = ber_add_bitstring(a, c, - sm->snmp_len); - else - a = ber_add_nstring(a, c, - sm->snmp_len); - a->be_free = 1; - break; - case SNMP_NULL: - a = ber_add_null(a); - break; - case SNMP_INTEGER32: - case SNMP_IPADDR: - case SNMP_COUNTER32: - case SNMP_GAUGE32: - case SNMP_TIMETICKS: - case SNMP_OPAQUE: - case SNMP_UINTEGER32: - if (sm->snmp_len != sizeof(d)) - goto imsgdone; - bcopy(sm + 1, &d, sm->snmp_len); - a = ber_add_integer(a, d); - break; - case SNMP_COUNTER64: - if (sm->snmp_len != sizeof(l)) - goto imsgdone; - bcopy(sm + 1, &l, sm->snmp_len); - a = ber_add_integer(a, d); - break; - default: - log_debug("snmpe_trap: illegal type %d", - sm->snmp_type); - imsg_free(&imsg); - goto imsgdone; - } - switch (sm->snmp_type) { - case SNMP_INTEGER32: - case SNMP_BITSTRING: - case SNMP_OCTETSTRING: - case SNMP_NULL: - case SNMP_OBJECT: - /* universal types */ - break; - case SNMP_IPADDR: - case SNMP_COUNTER32: - case SNMP_GAUGE32: - case SNMP_TIMETICKS: - case SNMP_OPAQUE: - case SNMP_NSAPADDR: - case SNMP_COUNTER64: - case SNMP_UINTEGER32: - /* application-specific types */ - ber_set_header(a, BER_CLASS_APPLICATION, - sm->snmp_type); - break; - } - x++; - break; - case IMSG_SNMP_END: - done = 1; - break; - default: - log_debug("snmpe_trap: illegal imsg %d", - imsg.hdr.type); - goto imsgdone; - } - imsg_free(&imsg); - } - if (done) - break; - if ((n = imsg_read(ibuf)) == -1) - goto done; - if (n == 0) - goto done; - } - - len = ber_calc_len(trap); - log_debug("snmpe_trap: %d bytes from pid %d", len, pid); - - /* XXX send trap to registered receivers */ - -#ifdef DEBUG - snmpe_debug_elements(trap); -#endif - - ret = 0; - imsgdone: - if (ret != 0) - imsg_free(&imsg); - done: - ber_free_elements(trap); - return (ret); -} diff --git a/usr.sbin/snmpd/trap.c b/usr.sbin/snmpd/trap.c new file mode 100644 index 00000000000..569d82010ce --- /dev/null +++ b/usr.sbin/snmpd/trap.c @@ -0,0 +1,192 @@ +/* $OpenBSD: trap.c,v 1.1 2008/01/16 09:42:29 reyk Exp $ */ + +/* + * Copyright (c) 2008 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 "snmpd.h" + +int +trap_request(struct imsgbuf *ibuf, pid_t pid) +{ + struct imsg imsg; + int ret = -1, n, x = 0; + int done = 0; + struct snmp_imsg *sm; + u_int32_t d; + u_int64_t l; + u_int8_t *c; + char o[SNMP_MAX_OID_LEN]; + struct ber_element *ber, *trap = NULL, *oid = NULL, *a; + size_t len; + + ber = trap = ber_add_sequence(NULL); + + while (!done) { + while (!done) { + if ((n = imsg_get(ibuf, &imsg)) == -1) + goto done; + if (n == 0) + break; + switch (imsg.hdr.type) { + case IMSG_SNMP_ELEMENT: + if (imsg.hdr.len < (IMSG_HEADER_SIZE + + sizeof(struct snmp_imsg))) + goto imsgdone; + + sm = (struct snmp_imsg *)imsg.data; + + if (oid == NULL) { + /* First element must be the trap OID */ + if (sm->snmp_type != SNMP_NULL) + goto imsgdone; + ber = oid = ber_printf_elements(ber, + "{o0}", sm->snmp_oid); + break; + } + + ber = a = ber_add_sequence(ber); + a = ber_add_oidstring(a, sm->snmp_oid); + + switch (sm->snmp_type) { + case SNMP_OBJECT: + if (sm->snmp_len != sizeof(o)) + goto imsgdone; + bcopy(sm + 1, &o, sm->snmp_len); + a = ber_add_oidstring(a, o); + break; + case SNMP_BITSTRING: + case SNMP_OCTETSTRING: + if ((sm->snmp_len < 1) || + (sm->snmp_len >= SNMPD_MAXSTRLEN)) + goto imsgdone; + if ((c = + calloc(1, sm->snmp_len)) == NULL) + goto imsgdone; + bcopy(sm + 1, c, sm->snmp_len); + if (sm->snmp_type == SNMP_BITSTRING) + a = ber_add_bitstring(a, c, + sm->snmp_len); + else + a = ber_add_nstring(a, c, + sm->snmp_len); + a->be_free = 1; + break; + case SNMP_NULL: + a = ber_add_null(a); + break; + case SNMP_INTEGER32: + case SNMP_IPADDR: + case SNMP_COUNTER32: + case SNMP_GAUGE32: + case SNMP_TIMETICKS: + case SNMP_OPAQUE: + case SNMP_UINTEGER32: + if (sm->snmp_len != sizeof(d)) + goto imsgdone; + bcopy(sm + 1, &d, sm->snmp_len); + a = ber_add_integer(a, d); + break; + case SNMP_COUNTER64: + if (sm->snmp_len != sizeof(l)) + goto imsgdone; + bcopy(sm + 1, &l, sm->snmp_len); + a = ber_add_integer(a, d); + break; + default: + log_debug("snmpe_trap: illegal type %d", + sm->snmp_type); + imsg_free(&imsg); + goto imsgdone; + } + switch (sm->snmp_type) { + case SNMP_INTEGER32: + case SNMP_BITSTRING: + case SNMP_OCTETSTRING: + case SNMP_NULL: + case SNMP_OBJECT: + /* universal types */ + break; + case SNMP_IPADDR: + case SNMP_COUNTER32: + case SNMP_GAUGE32: + case SNMP_TIMETICKS: + case SNMP_OPAQUE: + case SNMP_NSAPADDR: + case SNMP_COUNTER64: + case SNMP_UINTEGER32: + /* application-specific types */ + ber_set_header(a, BER_CLASS_APPLICATION, + sm->snmp_type); + break; + } + x++; + break; + case IMSG_SNMP_END: + done = 1; + break; + default: + log_debug("snmpe_trap: illegal imsg %d", + imsg.hdr.type); + goto imsgdone; + } + imsg_free(&imsg); + } + if (done) + break; + if ((n = imsg_read(ibuf)) == -1) + goto done; + if (n == 0) + goto done; + } + + len = ber_calc_len(trap); + log_debug("snmpe_trap: %d bytes from pid %d", len, pid); + + /* XXX send trap to registered receivers */ + +#ifdef DEBUG + snmpe_debug_elements(trap); +#endif + + ret = 0; + imsgdone: + if (ret != 0) + imsg_free(&imsg); + done: + ber_free_elements(trap); + return (ret); +} -- cgit v1.2.3