Add a family argument to host(). This allows us to force a specific address
authormartijn <martijn@openbsd.org>
Mon, 18 Dec 2023 16:58:26 +0000 (16:58 +0000)
committermartijn <martijn@openbsd.org>
Mon, 18 Dec 2023 16:58:26 +0000 (16:58 +0000)
family for source-address, which must be in line with the destination
address.

OK jca@

usr.sbin/snmpd/parse.y

index 002d8aa..7d00177 100644 (file)
@@ -1,4 +1,4 @@
-/*     $OpenBSD: parse.y,v 1.84 2023/12/18 16:56:01 martijn Exp $      */
+/*     $OpenBSD: parse.y,v 1.85 2023/12/18 16:58:26 martijn Exp $      */
 
 /*
  * Copyright (c) 2007, 2008, 2012 Reyk Floeter <reyk@openbsd.org>
@@ -105,7 +105,7 @@ static uint8_t                       engineid[SNMPD_MAXENGINEIDLEN];
 static int32_t                  enginepen;
 static size_t                   engineidlen;
 
-int             host(const char *, const char *, int,
+int             host(const char *, const char *, int, int,
                    struct sockaddr_storage *, int);
 int             listen_add(struct sockaddr_storage *, int, int);
 
@@ -395,8 +395,8 @@ listen_udptcp       : listenproto STRING port listenflags   {
                        }
 
                        for (i = 0; i < addresslen; i++) {
-                               nhosts = host(address[i], port, $1, ss, nitems(ss));
-                               if (nhosts < 1) {
+                               if ((nhosts = host(address[i], port, AF_UNSPEC,
+                                   $1, ss, nitems(ss))) < 1) {
                                        yyerror("invalid address: %s", $2);
                                        free($2);
                                        free($3);
@@ -1021,7 +1021,8 @@ hostdef           : STRING hostoid hostauth srcaddr       {
                                YYERROR;
                        }
 
-                       if (host($1, SNMPTRAP_PORT, SOCK_DGRAM, &ss, 1) <= 0) {
+                       if (host($1, SNMPTRAP_PORT, AF_UNSPEC, SOCK_DGRAM,
+                           &ss, 1) <= 0) {
                                yyerror("invalid host: %s", $1);
                                free($1);
                                free($2);
@@ -1033,7 +1034,8 @@ hostdef           : STRING hostoid hostauth srcaddr       {
                        free($1);
                        memcpy(&(tr->ta_ss), &ss, sizeof(ss));
                        if ($4 != NULL) {
-                               if (host($4, "0", SOCK_DGRAM, &ss, 1) <= 0) {
+                               if (host($4, "0", ss.ss_family, SOCK_DGRAM,
+                                   &ss, 1) <= 0) {
                                        yyerror("invalid source-address: %s",
                                            $4);
                                        free($2);
@@ -1703,11 +1705,12 @@ parse_config(const char *filename, u_int flags)
 
        /* Setup default listen addresses */
        if (TAILQ_EMPTY(&conf->sc_addresses)) {
-               if (host("0.0.0.0", SNMP_PORT, SOCK_DGRAM, &ss, 1) != 1)
+               if (host("0.0.0.0", SNMP_PORT, AF_INET, SOCK_DGRAM,
+                   &ss, 1) != 1)
                        fatal("Unexpected resolving of 0.0.0.0");
                if (listen_add(&ss, SOCK_DGRAM, 0) == -1)
                        fatal("calloc");
-               if (host("::", SNMP_PORT, SOCK_DGRAM, &ss, 1) != 1)
+               if (host("::", SNMP_PORT, AF_INET6, SOCK_DGRAM, &ss, 1) != 1)
                        fatal("Unexpected resolving of ::");
                if (listen_add(&ss, SOCK_DGRAM, 0) == -1)
                        fatal("calloc");
@@ -1844,14 +1847,14 @@ symget(const char *nam)
 }
 
 int
-host(const char *s, const char *port, int type, struct sockaddr_storage *ss,
-    int max)
+host(const char *s, const char *port, int family, int type,
+    struct sockaddr_storage *ss, int max)
 {
        struct addrinfo          hints, *res0, *res;
        int                      error, i;
 
        bzero(&hints, sizeof(hints));
-       hints.ai_family = PF_UNSPEC;
+       hints.ai_family = family;
        hints.ai_socktype = type;
        /*
         * Without AI_NUMERICHOST getaddrinfo might not resolve ip addresses