diff options
Diffstat (limited to 'contrib/bsnmp')
-rw-r--r-- | contrib/bsnmp/lib/bsnmpclient.3 | 19 | ||||
-rw-r--r-- | contrib/bsnmp/lib/snmpclient.c | 267 | ||||
-rw-r--r-- | contrib/bsnmp/lib/snmpclient.h | 6 | ||||
-rw-r--r-- | contrib/bsnmp/lib/snmppriv.h | 1 | ||||
-rw-r--r-- | contrib/bsnmp/snmpd/main.c | 37 | ||||
-rw-r--r-- | contrib/bsnmp/snmpd/snmpd.h | 6 | ||||
-rw-r--r-- | contrib/bsnmp/snmpd/trans_inet.c | 24 | ||||
-rw-r--r-- | contrib/bsnmp/snmpd/trans_lsock.c | 43 | ||||
-rw-r--r-- | contrib/bsnmp/snmpd/trans_udp.c | 439 | ||||
-rw-r--r-- | contrib/bsnmp/snmpd/tree.def | 9 |
10 files changed, 191 insertions, 660 deletions
diff --git a/contrib/bsnmp/lib/bsnmpclient.3 b/contrib/bsnmp/lib/bsnmpclient.3 index 0a2286eb14c4..1a5aa2e5e3a6 100644 --- a/contrib/bsnmp/lib/bsnmpclient.3 +++ b/contrib/bsnmp/lib/bsnmpclient.3 @@ -31,7 +31,7 @@ .\" .\" $Begemot: bsnmp/lib/bsnmpclient.3,v 1.12 2005/10/04 08:46:50 brandt_h Exp $ .\" -.Dd March 31, 2020 +.Dd June 24, 2025 .Dt BSNMPCLIENT 3 .Os .Sh NAME @@ -155,7 +155,7 @@ struct snmp_client { snmp_timeout_start_f timeout_start; snmp_timeout_stop_f timeout_stop; - char local_path[sizeof(SNMP_LOCAL_PATH)]; + char local_path[SUNPATHLEN]; }; .Ed .Pp @@ -285,8 +285,19 @@ The function will be called with the return value of the corresponding .Fn timeout_start function. .It Va local_path -If in local socket mode, the name of the clients socket. -Not needed by the application. +In local socket mode, optional path name the client socket shall be bound to +before connecting to the server. +For +.Dv SOCK_STREAM +local socket the named path is optional, and library will skip +.Xr bind 2 +if path is not provided. +For +.Dv SOCK_DGRAM +local socket the named path is required, thus library will create a random +one in +.Pa /tmp +if path is not provided. .El .Pp In the current implementation there is a global variable diff --git a/contrib/bsnmp/lib/snmpclient.c b/contrib/bsnmp/lib/snmpclient.c index b312a37ed3ed..44dfbd5a06b1 100644 --- a/contrib/bsnmp/lib/snmpclient.c +++ b/contrib/bsnmp/lib/snmpclient.c @@ -977,18 +977,15 @@ remove_local(void) static int open_client_local(const char *path) { - struct sockaddr_un sa; + struct sockaddr_un sa = { + .sun_family = AF_LOCAL, + .sun_len = sizeof(sa), + }; char *ptr; int stype; - if (snmp_client.chost == NULL) { - if ((snmp_client.chost = malloc(1 + sizeof(DEFAULT_LOCAL))) - == NULL) { - seterr(&snmp_client, "%s", strerror(errno)); - return (-1); - } - strcpy(snmp_client.chost, DEFAULT_LOCAL); - } + if (snmp_client.chost == NULL && path == NULL) + path = SNMP_DEFAULT_LOCAL; if (path != NULL) { if ((ptr = malloc(1 + strlen(path))) == NULL) { seterr(&snmp_client, "%s", strerror(errno)); @@ -1009,43 +1006,56 @@ open_client_local(const char *path) return (-1); } - snprintf(snmp_client.local_path, sizeof(snmp_client.local_path), - "%s", SNMP_LOCAL_PATH); - - if (mkstemp(snmp_client.local_path) == -1) { - seterr(&snmp_client, "%s", strerror(errno)); - (void)close(snmp_client.fd); - snmp_client.fd = -1; - return (-1); + /* + * A datagram socket requires a name to receive replies back. Would + * be cool to have an extension to unix(4) sockets similar to ip(4) + * IP_RECVDSTADDR/IP_SENDSRCADDR, so that a one-to-many datagram + * UNIX socket can send replies to its anonymous peers. + */ + if (snmp_client.trans == SNMP_TRANS_LOC_DGRAM && + snmp_client.local_path[0] == '\0') { + (void)strlcpy(snmp_client.local_path, "/tmp/snmpXXXXXXXXXXXXXX", + sizeof(snmp_client.local_path)); + if (mktemp(snmp_client.local_path) == NULL) { + seterr(&snmp_client, "mktemp(3): %s", strerror(errno)); + goto fail; + } } - sa.sun_family = AF_LOCAL; - sa.sun_len = sizeof(sa); - strcpy(sa.sun_path, snmp_client.local_path); - - if (bind(snmp_client.fd, (struct sockaddr *)&sa, sizeof(sa)) == -1) { - seterr(&snmp_client, "%s", strerror(errno)); - (void)close(snmp_client.fd); - snmp_client.fd = -1; - (void)remove(snmp_client.local_path); - return (-1); + if (snmp_client.local_path[0] != '\0') { + if (strlcpy(sa.sun_path, snmp_client.local_path, + sizeof(sa.sun_path)) >= + sizeof(sa.sun_path)) { + seterr(&snmp_client, "%s", + "Local socket pathname too long"); + goto fail; + } + if (bind(snmp_client.fd, (struct sockaddr *)&sa, sizeof(sa)) == + -1) { + seterr(&snmp_client, "%s", strerror(errno)); + goto fail; + } + atexit(remove_local); } - atexit(remove_local); - sa.sun_family = AF_LOCAL; - sa.sun_len = offsetof(struct sockaddr_un, sun_path) + - strlen(snmp_client.chost); - strncpy(sa.sun_path, snmp_client.chost, sizeof(sa.sun_path) - 1); - sa.sun_path[sizeof(sa.sun_path) - 1] = '\0'; + if (strlcpy(sa.sun_path, snmp_client.chost, sizeof(sa.sun_path)) >= + sizeof(sa.sun_path)) { + seterr(&snmp_client, "%s", "Server socket pathname too long"); + goto fail; + } if (connect(snmp_client.fd, (struct sockaddr *)&sa, sa.sun_len) == -1) { seterr(&snmp_client, "%s", strerror(errno)); - (void)close(snmp_client.fd); - snmp_client.fd = -1; - (void)remove(snmp_client.local_path); - return (-1); + goto fail; } return (0); + +fail: + (void)close(snmp_client.fd); + snmp_client.fd = -1; + if (snmp_client.local_path[0] != '\0') + (void)remove(snmp_client.local_path); + return (-1); } /* @@ -1932,70 +1942,64 @@ get_transp(struct snmp_client *sc, const char **strp) * community strings are legal. * * \param sc client struct to set errors - * \param strp possible start of community; updated to the point to - * the next character to parse + * \param comm possible start of community; updated to start & end * - * \return end of community; equals *strp if there is none; NULL if there - * was an error + * \return the next character to parse; NULL if there was an error */ static inline const char * -get_comm(struct snmp_client *sc, const char **strp) +get_comm(struct snmp_client *sc, const char *comm[2]) { - const char *p = strrchr(*strp, '@'); + const char *p = strrchr(comm[0], '@'); if (p == NULL) /* no community string */ - return (*strp); + return (comm[1] = comm[0]); - if (p - *strp > SNMP_COMMUNITY_MAXLEN) { + if (p - comm[0] > SNMP_COMMUNITY_MAXLEN) { seterr(sc, "community string too long '%.*s'", - p - *strp, *strp); + p - comm[0], comm[0]); return (NULL); } - *strp = p + 1; - return (p); + return ((comm[1] = p) + 1); } /** * Try to get an IPv6 address. This starts with an [ and should end with an ] * and everything between should be not longer than INET6_ADDRSTRLEN and - * parseable by inet_pton(). + * parseable by getaddrinfo(). * * \param sc client struct to set errors - * \param strp possible start of IPv6 address (the '['); updated to point to - * the next character to parse (the one after the closing ']') + * \param ipv6 possible start of IPv6 address (the '['); updated to actual + * start (one after '[') and actual end (the '[' itself) * - * \return end of address (equals *strp + 1 if there is none) or NULL - * on errors + * \return the next character to parse (the one after the closing ']') + * or NULL on errors */ static inline const char * -get_ipv6(struct snmp_client *sc, const char **strp) +get_ipv6(struct snmp_client *sc, const char *ipv6[2]) { - char str[INET6_ADDRSTRLEN + IF_NAMESIZE]; + char str[INET6_ADDRSTRLEN]; + const char *p; struct addrinfo hints, *res; int error; - if (**strp != '[') - return (*strp + 1); + if (ipv6[0][0] != '[') + return (ipv6[1] = ipv6[0]); - const char *p = *strp + 1; - while (*p != ']' ) { - if (*p == '\0') { - seterr(sc, "unterminated IPv6 address '%.*s'", - p - *strp, *strp); - return (NULL); - } - p++; + if ((p = strchr(++(ipv6[0]), ']')) == NULL) { + seterr(sc, "unterminated IPv6 address '%s'", ipv6[0]); + return (NULL); } - if (p - *strp > INET6_ADDRSTRLEN + IF_NAMESIZE) { - seterr(sc, "IPv6 address too long '%.*s'", p - *strp, *strp); + if ((size_t)(p - ipv6[0]) >= sizeof(str)) { + seterr(sc, "IPv6 address too long '%.*s'", + p - ipv6[0], ipv6[0]); return (NULL); } - strncpy(str, *strp + 1, p - (*strp + 1)); - str[p - (*strp + 1)] = '\0'; + strncpy(str, ipv6[0], p - ipv6[0]); + str[p - ipv6[0]] = '\0'; memset(&hints, 0, sizeof(hints)); hints.ai_flags = AI_CANONNAME | AI_NUMERICHOST; @@ -2008,8 +2012,7 @@ get_ipv6(struct snmp_client *sc, const char **strp) return (NULL); } freeaddrinfo(res); - *strp = p + 1; - return (p); + return ((ipv6[1] = p) + 1); } /** @@ -2018,30 +2021,29 @@ get_ipv6(struct snmp_client *sc, const char **strp) * inet_aton(). * * \param sc client struct to set errors - * \param strp possible start of IPv4 address; updated to point to the - * next character to parse + * \param ipv4 possible start of IPv4 address; updated to start & end * - * \return end of address (equals *strp if there is none) or NULL - * on errors + * \return the next character to parse; or NULL on errors */ static inline const char * -get_ipv4(struct snmp_client *sc, const char **strp) +get_ipv4(struct snmp_client *sc, const char *ipv4[2]) { - const char *p = *strp; + char str[INET_ADDRSTRLEN]; + const char *p = ipv4[0]; while (isascii(*p) && (isdigit(*p) || *p == '.')) p++; - if (p - *strp > INET_ADDRSTRLEN) { - seterr(sc, "IPv4 address too long '%.*s'", p - *strp, *strp); + if ((size_t)(p - ipv4[0]) >= sizeof(str)) { + seterr(sc, "IPv4 address too long '%.*s'", + p - ipv4[0], ipv4[0]); return (NULL); } - if (*strp == p) - return *strp; + if (p == ipv4[0]) + return (ipv4[1] = ipv4[0]); - char str[INET_ADDRSTRLEN + 1]; - strncpy(str, *strp, p - *strp); - str[p - *strp] = '\0'; + strncpy(str, ipv4[0], p - ipv4[0]); + str[p - ipv4[0]] = '\0'; struct in_addr addr; if (inet_aton(str, &addr) != 1) { @@ -2049,8 +2051,7 @@ get_ipv4(struct snmp_client *sc, const char **strp) return (NULL); } - *strp = p; - return (p); + return (ipv4[1] = p); } /** @@ -2058,24 +2059,19 @@ get_ipv4(struct snmp_client *sc, const char **strp) * the last colon (if any). There is no length restriction. * * \param sc client struct to set errors - * \param strp possible start of hostname; updated to point to the next - * character to parse (the trailing NUL character or the last - * colon) + * \param host possible start of hostname; start & end updated * - * \return end of address (equals *strp if there is none) + * \return next character to parse (semicolon or NUL) */ static inline const char * -get_host(struct snmp_client *sc __unused, const char **strp) +get_host(struct snmp_client *sc __unused, const char *host[2]) { - const char *p = strrchr(*strp, ':'); + const char *p = strrchr(host[0], ':'); - if (p == NULL) { - *strp += strlen(*strp); - return (*strp); - } + if (p == NULL) + return (host[1] = host[0] + strlen(host[0])); - *strp = p; - return (p); + return (host[1] = p); } /** @@ -2083,25 +2079,24 @@ get_host(struct snmp_client *sc __unused, const char **strp) * of string. The port number must not be empty. * * \param sc client struct to set errors - * \param strp possible start of port specification; if this points to a + * \param port possible start of port specification; if this points to a * colon there is a port specification * * \return end of port number (equals *strp if there is none); NULL * if there is no port number */ static inline const char * -get_port(struct snmp_client *sc, const char **strp) +get_port(struct snmp_client *sc, const char *port[2]) { - if (**strp != ':') - return (*strp + 1); + if (*port[0] != ':') + return (port[1] = port[0]); - if ((*strp)[1] == '\0') { + if (port[0][1] == '\0') { seterr(sc, "empty port name"); return (NULL); } - *strp += strlen(*strp); - return (*strp); + return (port[1] = ++(port[0]) + strlen(port[0])); } /** @@ -2162,6 +2157,7 @@ int snmp_parse_server(struct snmp_client *sc, const char *str) { const char *const orig = str; + const char *comm[2], *ipv6[2], *ipv4[2], *host[2], *port[2]; /* parse input */ int def_trans = 0, trans = get_transp(sc, &str); @@ -2171,42 +2167,32 @@ snmp_parse_server(struct snmp_client *sc, const char *str) if (orig == str) def_trans = 1; - const char *const comm[2] = { - str, - get_comm(sc, &str), - }; - if (comm[1] == NULL) + comm[0] = str; + if ((str = get_comm(sc, comm)) == NULL) return (-1); - const char *const ipv6[2] = { - str + 1, - get_ipv6(sc, &str), - }; - if (ipv6[1] == NULL) + ipv6[0] = str; + if ((str = get_ipv6(sc, ipv6)) == NULL) return (-1); - const char *ipv4[2] = { - str, - str, - }; - - const char *host[2] = { - str, - str, - }; - if (ipv6[0] == ipv6[1]) { - ipv4[1] = get_ipv4(sc, &str); + ipv4[0] = str; + if ((str = get_ipv4(sc, ipv4)) == NULL) { + /* This failure isn't fatal: restore str. */ + str = ipv4[0]; + ipv4[0] = ipv4[1] = NULL; + } - if (ipv4[0] == ipv4[1]) - host[1] = get_host(sc, &str); - } + if (ipv4[0] == ipv4[1]) { + host[0] = str; + str = get_host(sc, host); + } else + host[0] = host[1] = NULL; + } else + ipv4[0] = ipv4[1] = host[0] = host[1] = NULL; - const char *port[2] = { - str + 1, - get_port(sc, &str), - }; - if (port[1] == NULL) + port[0] = str; + if ((str = get_port(sc, port)) == NULL) return (-1); if (*str != '\0') { @@ -2237,7 +2223,7 @@ snmp_parse_server(struct snmp_client *sc, const char *str) return (-1); if (def_trans) trans = SNMP_TRANS_UDP; - } else { + } else if (host[0] != host[1]) { if ((chost = save_str(sc, host)) == NULL) return (-1); @@ -2252,6 +2238,17 @@ snmp_parse_server(struct snmp_client *sc, const char *str) break; } } + } else switch (trans) { + case SNMP_TRANS_UDP: + case SNMP_TRANS_UDP6: + if ((chost = strdup(DEFAULT_HOST)) == NULL) + return (-1); + break; + case SNMP_TRANS_LOC_DGRAM: + case SNMP_TRANS_LOC_STREAM: + if ((chost = strdup(SNMP_DEFAULT_LOCAL)) == NULL) + return (-1); + break; } char *cport; diff --git a/contrib/bsnmp/lib/snmpclient.h b/contrib/bsnmp/lib/snmpclient.h index a19bdb2ea653..a8a79ff824bc 100644 --- a/contrib/bsnmp/lib/snmpclient.h +++ b/contrib/bsnmp/lib/snmpclient.h @@ -35,13 +35,13 @@ #include <sys/types.h> #include <sys/socket.h> #include <sys/time.h> +#include <sys/un.h> #include <netinet/in.h> #include <stddef.h> #define SNMP_STRERROR_LEN 200 - -#define SNMP_LOCAL_PATH "/tmp/snmpXXXXXXXXXXXXXX" +#define SNMP_DEFAULT_LOCAL "/var/run/snmpd.sock" /* * transport methods @@ -110,7 +110,7 @@ struct snmp_client { snmp_timeout_start_f timeout_start; snmp_timeout_stop_f timeout_stop; - char local_path[sizeof(SNMP_LOCAL_PATH)]; + char local_path[SUNPATHLEN]; }; /* the global context */ diff --git a/contrib/bsnmp/lib/snmppriv.h b/contrib/bsnmp/lib/snmppriv.h index 5b66992ca985..6ed51cf39369 100644 --- a/contrib/bsnmp/lib/snmppriv.h +++ b/contrib/bsnmp/lib/snmppriv.h @@ -44,4 +44,3 @@ enum snmp_code snmp_pdu_decrypt(const struct snmp_pdu *); #define DEFAULT_HOST "localhost" #define DEFAULT_PORT "snmp" -#define DEFAULT_LOCAL "/var/run/snmp.sock" diff --git a/contrib/bsnmp/snmpd/main.c b/contrib/bsnmp/snmpd/main.c index 928b84121f82..933ab7aa655a 100644 --- a/contrib/bsnmp/snmpd/main.c +++ b/contrib/bsnmp/snmpd/main.c @@ -42,6 +42,7 @@ #include <sys/un.h> #include <sys/ucred.h> #include <sys/uio.h> +#include <stdbool.h> #include <stdio.h> #include <stdlib.h> #include <stddef.h> @@ -236,7 +237,6 @@ static struct request_info req; #endif /* transports */ -extern const struct transport_def udp_trans; extern const struct transport_def lsock_trans; struct transport_list transport_list = TAILQ_HEAD_INITIALIZER(transport_list); @@ -1186,12 +1186,7 @@ snmpd_input(struct port_input *pi, struct tport *tport) sndbuf, &sndlen, "SNMP", ierr, vi, NULL); if (ferr == SNMPD_INPUT_OK) { - if (tport->transport->vtab->send != NULL) - slen = tport->transport->vtab->send(tport, sndbuf, - sndlen, pi->peer, pi->peerlen); - else - slen = tport->transport->vtab->send2(tport, sndbuf, - sndlen, pi); + slen = tport->transport->vtab->send(tport, sndbuf, sndlen, pi); if (slen == -1) syslog(LOG_ERR, "send*: %m"); else if ((size_t)slen != sndlen) @@ -1214,6 +1209,11 @@ void snmp_send_port(void *targ, const struct asn_oid *port, struct snmp_pdu *pdu, const struct sockaddr *addr, socklen_t addrlen) { + struct port_input pi = { + .fd = -1, + .peer = __DECONST(struct sockaddr *, addr), + .peerlen = addrlen, + }; struct transport *trans = targ; struct tport *tp; u_char *sndbuf; @@ -1231,10 +1231,7 @@ snmp_send_port(void *targ, const struct asn_oid *port, struct snmp_pdu *pdu, snmp_output(pdu, sndbuf, &sndlen, "SNMP PROXY"); - if (trans->vtab->send != NULL) - len = trans->vtab->send(tp, sndbuf, sndlen, addr, addrlen); - else - len = trans->vtab->send2(tp, sndbuf, sndlen, NULL); + len = trans->vtab->send(tp, sndbuf, sndlen, &pi); if (len == -1) syslog(LOG_ERR, "sendto: %m"); @@ -1509,7 +1506,7 @@ main(int argc, char *argv[]) { int opt; FILE *fp; - int background = 1; + bool background = true; struct tport *p; const char *prefix = "snmpd"; struct lmodule *m; @@ -1526,11 +1523,6 @@ main(int argc, char *argv[]) NULL }; - snmp_printf = snmp_printf_func; - snmp_error = snmp_error_func; - snmp_debug = snmp_debug_func; - asn_error = asn_error_func; - while ((opt = getopt(argc, argv, "c:dD:e:hI:l:m:p:")) != EOF) switch (opt) { @@ -1539,7 +1531,7 @@ main(int argc, char *argv[]) break; case 'd': - background = 0; + background = false; break; case 'D': @@ -1601,6 +1593,13 @@ main(int argc, char *argv[]) break; } + if (background) { + snmp_printf = snmp_printf_func; + snmp_error = snmp_error_func; + snmp_debug = snmp_debug_func; + asn_error = asn_error_func; + } + openlog(prefix, LOG_PID | (background ? 0 : LOG_PERROR), LOG_USER); setlogmask(LOG_UPTO(debug.logpri - 1)); @@ -1658,8 +1657,6 @@ main(int argc, char *argv[]) syslog(LOG_ERR, "atexit failed: %m"); exit(1); } - if (udp_trans.start() != SNMP_ERR_NOERROR) - syslog(LOG_WARNING, "cannot start UDP transport"); if (lsock_trans.start() != SNMP_ERR_NOERROR) syslog(LOG_WARNING, "cannot start LSOCK transport"); if (inet_trans.start() != SNMP_ERR_NOERROR) diff --git a/contrib/bsnmp/snmpd/snmpd.h b/contrib/bsnmp/snmpd/snmpd.h index 394a4f4736d6..b0e60040d025 100644 --- a/contrib/bsnmp/snmpd/snmpd.h +++ b/contrib/bsnmp/snmpd/snmpd.h @@ -192,12 +192,8 @@ struct transport_def { int (*init_port)(struct tport *); ssize_t (*send)(struct tport *, const u_char *, size_t, - const struct sockaddr *, size_t); - ssize_t (*recv)(struct tport *, struct port_input *); - - /** send via a multi-socket port */ - ssize_t (*send2)(struct tport *, const u_char *, size_t, struct port_input *); + ssize_t (*recv)(struct tport *, struct port_input *); }; struct transport { struct asn_oid index; /* transport table index */ diff --git a/contrib/bsnmp/snmpd/trans_inet.c b/contrib/bsnmp/snmpd/trans_inet.c index dccfb6234222..d06b85ac11f6 100644 --- a/contrib/bsnmp/snmpd/trans_inet.c +++ b/contrib/bsnmp/snmpd/trans_inet.c @@ -375,17 +375,16 @@ inet_recv(struct tport *tp, struct port_input *pi) * \param tp port * \param buf data to send * \param len number of bytes to send - * \param addr destination address - * \param addlen destination address length + * \param pi destination * * \return number of bytes sent */ static ssize_t -inet_send2(struct tport *tp, const u_char *buf, size_t len, +inet_send(struct tport *tp, const u_char *buf, size_t len, struct port_input *pi) { struct inet_port *p = __containerof(tp, struct inet_port, tport); - struct port_sock *s = (pi == NULL) ? TAILQ_FIRST(&p->socks) : + struct port_sock *s = (pi->fd == -1) ? TAILQ_FIRST(&p->socks) : __containerof(pi, struct port_sock, input); struct iovec iov; @@ -414,15 +413,14 @@ inet_send2(struct tport *tp, const u_char *buf, size_t len, /** exported to daemon */ const struct transport_def inet_trans = { - "inet", - OIDX_begemotSnmpdTransInet, - inet_start, - inet_stop, - inet_destroy_port, - inet_activate, - NULL, - inet_recv, - inet_send2, + .name = "inet", + .id = OIDX_begemotSnmpdTransInet, + .start = inet_start, + .stop = inet_stop, + .close_port = inet_destroy_port, + .init_port = inet_activate, + .recv = inet_recv, + .send = inet_send, }; struct inet_port_params { diff --git a/contrib/bsnmp/snmpd/trans_lsock.c b/contrib/bsnmp/snmpd/trans_lsock.c index fa3bd34d14f0..01beb01927ec 100644 --- a/contrib/bsnmp/snmpd/trans_lsock.c +++ b/contrib/bsnmp/snmpd/trans_lsock.c @@ -58,20 +58,19 @@ static int lsock_stop(int); static void lsock_close_port(struct tport *); static int lsock_init_port(struct tport *); static ssize_t lsock_send(struct tport *, const u_char *, size_t, - const struct sockaddr *, size_t); + struct port_input *); static ssize_t lsock_recv(struct tport *, struct port_input *); /* exported */ const struct transport_def lsock_trans = { - "lsock", - OIDX_begemotSnmpdTransLsock, - lsock_start, - lsock_stop, - lsock_close_port, - lsock_init_port, - lsock_send, - lsock_recv, - NULL + .name = "lsock", + .id = OIDX_begemotSnmpdTransLsock, + .start = lsock_start, + .stop = lsock_stop, + .close_port = lsock_close_port, + .init_port = lsock_init_port, + .send = lsock_send, + .recv = lsock_recv, }; static struct transport *my_trans; @@ -396,28 +395,10 @@ lsock_init_port(struct tport *tp) * Send something */ static ssize_t -lsock_send(struct tport *tp, const u_char *buf, size_t len, - const struct sockaddr *addr, size_t addrlen) +lsock_send(struct tport *tp __unused, const u_char *buf, size_t len, + struct port_input *pi) { - struct lsock_port *p = (struct lsock_port *)tp; - struct lsock_peer *peer; - - if (p->type == LOCP_DGRAM_PRIV || p->type == LOCP_DGRAM_UNPRIV) { - peer = LIST_FIRST(&p->peers); - - } else { - /* search for the peer */ - LIST_FOREACH(peer, &p->peers, link) - if (peer->input.peerlen == addrlen && - memcmp(peer->input.peer, addr, addrlen) == 0) - break; - if (peer == NULL) { - errno = ENOTCONN; - return (-1); - } - } - - return (sendto(peer->input.fd, buf, len, 0, addr, addrlen)); + return (sendto(pi->fd, buf, len, MSG_NOSIGNAL, pi->peer, pi->peerlen)); } static void diff --git a/contrib/bsnmp/snmpd/trans_udp.c b/contrib/bsnmp/snmpd/trans_udp.c deleted file mode 100644 index 8e9d1510d1d7..000000000000 --- a/contrib/bsnmp/snmpd/trans_udp.c +++ /dev/null @@ -1,439 +0,0 @@ -/* - * Copyright (c) 2003 - * Fraunhofer Institute for Open Communication Systems (FhG Fokus). - * All rights reserved. - * - * Author: Harti Brandt <harti@freebsd.org> - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - * - * $Begemot: bsnmp/snmpd/trans_udp.c,v 1.5 2005/10/04 08:46:56 brandt_h Exp $ - * - * UDP transport - */ -#include <sys/types.h> -#include <sys/queue.h> -#include <sys/ucred.h> - -#include <stdbool.h> -#include <stdlib.h> -#include <syslog.h> -#include <string.h> -#include <errno.h> -#include <unistd.h> - -#include <netinet/in.h> -#include <arpa/inet.h> - -#include "snmpmod.h" -#include "snmpd.h" -#include "trans_udp.h" -#include "tree.h" -#include "oid.h" - -static int udp_start(void); -static int udp_stop(int); -static void udp_close_port(struct tport *); -static int udp_init_port(struct tport *); -static ssize_t udp_send(struct tport *, const u_char *, size_t, - const struct sockaddr *, size_t); -static ssize_t udp_recv(struct tport *, struct port_input *); - -/* exported */ -const struct transport_def udp_trans = { - "udp", - OIDX_begemotSnmpdTransUdp, - udp_start, - udp_stop, - udp_close_port, - udp_init_port, - udp_send, - udp_recv, - NULL -}; -static struct transport *my_trans; - -static int -udp_start(void) -{ - return (trans_register(&udp_trans, &my_trans)); -} - -static int -udp_stop(int force __unused) -{ - if (my_trans != NULL) - if (trans_unregister(my_trans) != 0) - return (SNMP_ERR_GENERR); - return (SNMP_ERR_NOERROR); -} - -/* - * A UDP port is ready - */ -static void -udp_input(int fd __unused, void *udata) -{ - struct udp_port *p = udata; - - p->input.peerlen = sizeof(p->ret); - snmpd_input(&p->input, &p->tport); -} - -/* - * Create a UDP socket and bind it to the given port - */ -static int -udp_init_port(struct tport *tp) -{ - struct udp_port *p = (struct udp_port *)tp; - struct sockaddr_in addr; - u_int32_t ip; - const int on = 1; - - if ((p->input.fd = socket(PF_INET, SOCK_DGRAM, 0)) < 0) { - syslog(LOG_ERR, "creating UDP socket: %m"); - return (SNMP_ERR_RES_UNAVAIL); - } - ip = (p->addr[0] << 24) | (p->addr[1] << 16) | (p->addr[2] << 8) | - p->addr[3]; - memset(&addr, 0, sizeof(addr)); - addr.sin_addr.s_addr = htonl(ip); - addr.sin_port = htons(p->port); - addr.sin_family = AF_INET; - addr.sin_len = sizeof(addr); - if (addr.sin_addr.s_addr == INADDR_ANY) { - if (setsockopt(p->input.fd, IPPROTO_IP, IP_RECVDSTADDR, &on, - sizeof(on)) == -1) { - syslog(LOG_ERR, "setsockopt(IP_RECVDSTADDR): %m"); - close(p->input.fd); - p->input.fd = -1; - return (SNMP_ERR_GENERR); - } - p->recvdstaddr = true; - } - if (bind(p->input.fd, (struct sockaddr *)&addr, sizeof(addr))) { - if (errno == EADDRNOTAVAIL) { - close(p->input.fd); - p->input.fd = -1; - return (SNMP_ERR_INCONS_NAME); - } - syslog(LOG_ERR, "bind: %s:%u %m", inet_ntoa(addr.sin_addr), - p->port); - close(p->input.fd); - p->input.fd = -1; - return (SNMP_ERR_GENERR); - } - if ((p->input.id = fd_select(p->input.fd, udp_input, - p, NULL)) == NULL) { - close(p->input.fd); - p->input.fd = -1; - return (SNMP_ERR_GENERR); - } - return (SNMP_ERR_NOERROR); -} - -/* - * Create a new SNMP Port object and start it, if we are not - * in initialization mode. The arguments are in host byte order. - */ -static int -udp_open_port(u_int8_t *addr, u_int32_t udp_port, struct udp_port **pp) -{ - struct udp_port *port; - int err; - - if (udp_port > 0xffff) - return (SNMP_ERR_NO_CREATION); - if ((port = malloc(sizeof(*port))) == NULL) - return (SNMP_ERR_GENERR); - memset(port, 0, sizeof(*port)); - - /* initialize common part */ - port->tport.index.len = 5; - port->tport.index.subs[0] = addr[0]; - port->tport.index.subs[1] = addr[1]; - port->tport.index.subs[2] = addr[2]; - port->tport.index.subs[3] = addr[3]; - port->tport.index.subs[4] = udp_port; - - port->addr[0] = addr[0]; - port->addr[1] = addr[1]; - port->addr[2] = addr[2]; - port->addr[3] = addr[3]; - port->port = udp_port; - - port->input.fd = -1; - port->input.id = NULL; - port->input.stream = 0; - port->input.cred = 0; - port->input.peer = (struct sockaddr *)&port->ret; - port->input.peerlen = sizeof(port->ret); - - trans_insert_port(my_trans, &port->tport); - - if (community != COMM_INITIALIZE && - (err = udp_init_port(&port->tport)) != SNMP_ERR_NOERROR) { - udp_close_port(&port->tport); - return (err); - } - *pp = port; - return (SNMP_ERR_NOERROR); -} - -/* - * Close an SNMP port - */ -static void -udp_close_port(struct tport *tp) -{ - struct udp_port *port = (struct udp_port *)tp; - - snmpd_input_close(&port->input); - trans_remove_port(tp); - free(port); -} - -/* - * Send something - */ -static ssize_t -udp_send(struct tport *tp, const u_char *buf, size_t len, - const struct sockaddr *addr, size_t addrlen) -{ - struct udp_port *p = (struct udp_port *)tp; - struct cmsghdr *cmsg; - struct msghdr msg; - char cbuf[CMSG_SPACE(sizeof(struct in_addr))]; - struct iovec iov; - - iov.iov_base = __DECONST(void*, buf); - iov.iov_len = len; - - msg.msg_flags = 0; - msg.msg_iov = &iov; - msg.msg_iovlen = 1; - msg.msg_name = __DECONST(void *, addr); - msg.msg_namelen = addrlen; - - if (p->recvdstaddr) { - msg.msg_control = cbuf; - msg.msg_controllen = sizeof(cbuf); - - cmsg = CMSG_FIRSTHDR(&msg); - cmsg->cmsg_level = IPPROTO_IP; - cmsg->cmsg_type = IP_SENDSRCADDR; - cmsg->cmsg_len = CMSG_LEN(sizeof(struct in_addr)); - memcpy(CMSG_DATA(cmsg), &p->dstaddr, sizeof(struct in_addr)); - } else { - msg.msg_control = NULL; - msg.msg_controllen = 0; - } - - return (sendmsg(p->input.fd, &msg, 0)); -} - -static void -check_priv_dgram(struct port_input *pi, struct sockcred *cred) -{ - - /* process explicitly sends credentials */ - if (cred) - pi->priv = (cred->sc_euid == 0); - else - pi->priv = 0; -} - -/* - * Input from a datagram socket. - * Each receive should return one datagram. - */ -static ssize_t -udp_recv(struct tport *tp, struct port_input *pi) -{ - u_char embuf[1000]; - char cbuf[CMSG_SPACE(SOCKCREDSIZE(CMGROUP_MAX)) + - CMSG_SPACE(sizeof(struct in_addr))]; - struct udp_port *p = (struct udp_port *)tp; - struct msghdr msg; - struct iovec iov[1]; - ssize_t len; - struct cmsghdr *cmsg; - struct sockcred *cred = NULL; - - if (pi->buf == NULL) { - /* no buffer yet - allocate one */ - if ((pi->buf = buf_alloc(0)) == NULL) { - /* ups - could not get buffer. Read away input - * and drop it */ - (void)recvfrom(pi->fd, embuf, sizeof(embuf), - 0, NULL, NULL); - /* return error */ - return (-1); - } - pi->buflen = buf_size(0); - } - - /* try to get a message */ - msg.msg_name = pi->peer; - msg.msg_namelen = pi->peerlen; - msg.msg_iov = iov; - msg.msg_iovlen = 1; - memset(cbuf, 0, sizeof(cbuf)); - msg.msg_control = cbuf; - msg.msg_controllen = sizeof(cbuf); - msg.msg_flags = 0; - - iov[0].iov_base = pi->buf; - iov[0].iov_len = pi->buflen; - - len = recvmsg(pi->fd, &msg, 0); - - if (len == -1 || len == 0) - /* receive error */ - return (-1); - - if (msg.msg_flags & MSG_TRUNC) { - /* truncated - drop */ - snmpd_stats.silentDrops++; - snmpd_stats.inTooLong++; - return (-1); - } - - pi->length = (size_t)len; - - for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL; - cmsg = CMSG_NXTHDR(&msg, cmsg)) { - if (cmsg->cmsg_level == IPPROTO_IP && - cmsg->cmsg_type == IP_RECVDSTADDR) - memcpy(&p->dstaddr, CMSG_DATA(cmsg), - sizeof(struct in_addr)); - if (cmsg->cmsg_level == SOL_SOCKET && - cmsg->cmsg_type == SCM_CREDS) - cred = (struct sockcred *)(void *)CMSG_DATA(cmsg); - } - - if (pi->cred) - check_priv_dgram(pi, cred); - - return (0); -} - -/* - * Port table - */ -int -op_snmp_port(struct snmp_context *ctx, struct snmp_value *value, - u_int sub, u_int iidx, enum snmp_op op) -{ - asn_subid_t which = value->var.subs[sub-1]; - struct udp_port *p; - u_int8_t addr[4]; - u_int32_t port; - - switch (op) { - - case SNMP_OP_GETNEXT: - if ((p = (struct udp_port *)trans_next_port(my_trans, - &value->var, sub)) == NULL) - return (SNMP_ERR_NOSUCHNAME); - index_append(&value->var, sub, &p->tport.index); - break; - - case SNMP_OP_GET: - if ((p = (struct udp_port *)trans_find_port(my_trans, - &value->var, sub)) == NULL) - return (SNMP_ERR_NOSUCHNAME); - break; - - case SNMP_OP_SET: - p = (struct udp_port *)trans_find_port(my_trans, - &value->var, sub); - ctx->scratch->int1 = (p != NULL); - - if (which != LEAF_begemotSnmpdPortStatus) - abort(); - if (!TRUTH_OK(value->v.integer)) - return (SNMP_ERR_WRONG_VALUE); - - ctx->scratch->int2 = TRUTH_GET(value->v.integer); - - if (ctx->scratch->int2) { - /* open an SNMP port */ - if (p != NULL) - /* already open - do nothing */ - return (SNMP_ERR_NOERROR); - - if (index_decode(&value->var, sub, iidx, addr, &port)) - return (SNMP_ERR_NO_CREATION); - return (udp_open_port(addr, port, &p)); - - } else { - /* close SNMP port - do in commit */ - } - return (SNMP_ERR_NOERROR); - - case SNMP_OP_ROLLBACK: - p = (struct udp_port *)trans_find_port(my_trans, - &value->var, sub); - if (ctx->scratch->int1 == 0) { - /* did not exist */ - if (ctx->scratch->int2 == 1) { - /* created */ - if (p != NULL) - udp_close_port(&p->tport); - } - } - return (SNMP_ERR_NOERROR); - - case SNMP_OP_COMMIT: - p = (struct udp_port *)trans_find_port(my_trans, - &value->var, sub); - if (ctx->scratch->int1 == 1) { - /* did exist */ - if (ctx->scratch->int2 == 0) { - /* delete */ - if (p != NULL) - udp_close_port(&p->tport); - } - } - return (SNMP_ERR_NOERROR); - - default: - abort(); - } - - /* - * Come here to fetch the value - */ - switch (which) { - - case LEAF_begemotSnmpdPortStatus: - value->v.integer = 1; - break; - - default: - abort(); - } - - return (SNMP_ERR_NOERROR); -} diff --git a/contrib/bsnmp/snmpd/tree.def b/contrib/bsnmp/snmpd/tree.def index 61b581120528..074a6e7e5390 100644 --- a/contrib/bsnmp/snmpd/tree.def +++ b/contrib/bsnmp/snmpd/tree.def @@ -117,15 +117,6 @@ typedef BegemotSnmpdTransportProto ENUM ( ) ) # -# Port table -# - (4 begemotSnmpdPortTable - (1 begemotSnmpdPortEntry : IPADDRESS INTEGER op_snmp_port - (1 begemotSnmpdPortAddress IPADDRESS) - (2 begemotSnmpdPortPort UNSIGNED32) - (3 begemotSnmpdPortStatus INTEGER GET SET) - )) -# # Community table # (5 begemotSnmpdCommunityTable |