1
0
mirror of https://git.FreeBSD.org/src.git synced 2024-12-12 09:58:36 +00:00

Add a missing STAILQ_INSERT_TAIL() for allowaddr rule.

This commit is contained in:
Hiroki Sato 2016-12-19 17:10:30 +00:00
parent c329b295dc
commit a5636d2a3e
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=310278

View File

@ -2303,7 +2303,7 @@ static int
allowaddr(char *s) allowaddr(char *s)
{ {
char *cp1, *cp2; char *cp1, *cp2;
struct allowedpeer ap; struct allowedpeer *ap;
struct servent *se; struct servent *se;
int masklen = -1; int masklen = -1;
struct addrinfo hints, *res; struct addrinfo hints, *res;
@ -2314,6 +2314,10 @@ allowaddr(char *s)
#endif #endif
char ip[NI_MAXHOST]; char ip[NI_MAXHOST];
ap = calloc(1, sizeof(*ap));
if (ap == NULL)
err(1, "malloc failed");
#ifdef INET6 #ifdef INET6
if (*s != '[' || (cp1 = strchr(s + 1, ']')) == NULL) if (*s != '[' || (cp1 = strchr(s + 1, ']')) == NULL)
#endif #endif
@ -2323,20 +2327,20 @@ allowaddr(char *s)
*cp1++ = '\0'; *cp1++ = '\0';
if (strlen(cp1) == 1 && *cp1 == '*') if (strlen(cp1) == 1 && *cp1 == '*')
/* any port allowed */ /* any port allowed */
ap.port = 0; ap->port = 0;
else if ((se = getservbyname(cp1, "udp"))) { else if ((se = getservbyname(cp1, "udp"))) {
ap.port = ntohs(se->s_port); ap->port = ntohs(se->s_port);
} else { } else {
ap.port = strtol(cp1, &cp2, 0); ap->port = strtol(cp1, &cp2, 0);
if (*cp2 != '\0') if (*cp2 != '\0')
return (-1); /* port not numeric */ return (-1); /* port not numeric */
} }
} else { } else {
if ((se = getservbyname("syslog", "udp"))) if ((se = getservbyname("syslog", "udp")))
ap.port = ntohs(se->s_port); ap->port = ntohs(se->s_port);
else else
/* sanity, should not happen */ /* sanity, should not happen */
ap.port = 514; ap->port = 514;
} }
if ((cp1 = strchr(s, '/')) != NULL && if ((cp1 = strchr(s, '/')) != NULL &&
@ -2363,14 +2367,14 @@ allowaddr(char *s)
hints.ai_socktype = SOCK_DGRAM; hints.ai_socktype = SOCK_DGRAM;
hints.ai_flags = AI_PASSIVE | AI_NUMERICHOST; hints.ai_flags = AI_PASSIVE | AI_NUMERICHOST;
if (getaddrinfo(s, NULL, &hints, &res) == 0) { if (getaddrinfo(s, NULL, &hints, &res) == 0) {
ap.isnumeric = 1; ap->isnumeric = 1;
memcpy(&ap.a_addr, res->ai_addr, res->ai_addrlen); memcpy(&ap->a_addr, res->ai_addr, res->ai_addrlen);
memset(&ap.a_mask, 0, sizeof(ap.a_mask)); memset(&ap->a_mask, 0, sizeof(ap->a_mask));
ap.a_mask.ss_family = res->ai_family; ap->a_mask.ss_family = res->ai_family;
if (res->ai_family == AF_INET) { if (res->ai_family == AF_INET) {
ap.a_mask.ss_len = sizeof(struct sockaddr_in); ap->a_mask.ss_len = sizeof(struct sockaddr_in);
maskp = &((struct sockaddr_in *)&ap.a_mask)->sin_addr; maskp = &((struct sockaddr_in *)&ap->a_mask)->sin_addr;
addrp = &((struct sockaddr_in *)&ap.a_addr)->sin_addr; addrp = &((struct sockaddr_in *)&ap->a_addr)->sin_addr;
if (masklen < 0) { if (masklen < 0) {
/* use default netmask */ /* use default netmask */
if (IN_CLASSA(ntohl(addrp->s_addr))) if (IN_CLASSA(ntohl(addrp->s_addr)))
@ -2394,10 +2398,10 @@ allowaddr(char *s)
} }
#ifdef INET6 #ifdef INET6
else if (res->ai_family == AF_INET6 && masklen <= 128) { else if (res->ai_family == AF_INET6 && masklen <= 128) {
ap.a_mask.ss_len = sizeof(struct sockaddr_in6); ap->a_mask.ss_len = sizeof(struct sockaddr_in6);
if (masklen < 0) if (masklen < 0)
masklen = 128; masklen = 128;
mask6p = (u_int32_t *)&((struct sockaddr_in6 *)&ap.a_mask)->sin6_addr; mask6p = (u_int32_t *)&((struct sockaddr_in6 *)&ap->a_mask)->sin6_addr;
/* convert masklen to netmask */ /* convert masklen to netmask */
while (masklen > 0) { while (masklen > 0) {
if (masklen < 32) { if (masklen < 32) {
@ -2408,8 +2412,8 @@ allowaddr(char *s)
masklen -= 32; masklen -= 32;
} }
/* Lose any host bits in the network number. */ /* Lose any host bits in the network number. */
mask6p = (u_int32_t *)&((struct sockaddr_in6 *)&ap.a_mask)->sin6_addr; mask6p = (u_int32_t *)&((struct sockaddr_in6 *)&ap->a_mask)->sin6_addr;
addr6p = (u_int32_t *)&((struct sockaddr_in6 *)&ap.a_addr)->sin6_addr; addr6p = (u_int32_t *)&((struct sockaddr_in6 *)&ap->a_addr)->sin6_addr;
for (i = 0; i < 4; i++) for (i = 0; i < 4; i++)
addr6p[i] &= mask6p[i]; addr6p[i] &= mask6p[i];
} }
@ -2421,8 +2425,8 @@ allowaddr(char *s)
freeaddrinfo(res); freeaddrinfo(res);
} else { } else {
/* arg `s' is domain name */ /* arg `s' is domain name */
ap.isnumeric = 0; ap->isnumeric = 0;
ap.a_name = s; ap->a_name = s;
if (cp1) if (cp1)
*cp1 = '/'; *cp1 = '/';
#ifdef INET6 #ifdef INET6
@ -2432,23 +2436,24 @@ allowaddr(char *s)
} }
#endif #endif
} }
STAILQ_INSERT_TAIL(&aphead, ap, next);
if (Debug) { if (Debug) {
printf("allowaddr: rule "); printf("allowaddr: rule ");
if (ap.isnumeric) { if (ap->isnumeric) {
printf("numeric, "); printf("numeric, ");
getnameinfo((struct sockaddr *)&ap.a_addr, getnameinfo((struct sockaddr *)&ap->a_addr,
((struct sockaddr *)&ap.a_addr)->sa_len, ((struct sockaddr *)&ap->a_addr)->sa_len,
ip, sizeof ip, NULL, 0, NI_NUMERICHOST); ip, sizeof ip, NULL, 0, NI_NUMERICHOST);
printf("addr = %s, ", ip); printf("addr = %s, ", ip);
getnameinfo((struct sockaddr *)&ap.a_mask, getnameinfo((struct sockaddr *)&ap->a_mask,
((struct sockaddr *)&ap.a_mask)->sa_len, ((struct sockaddr *)&ap->a_mask)->sa_len,
ip, sizeof ip, NULL, 0, NI_NUMERICHOST); ip, sizeof ip, NULL, 0, NI_NUMERICHOST);
printf("mask = %s; ", ip); printf("mask = %s; ", ip);
} else { } else {
printf("domainname = %s; ", ap.a_name); printf("domainname = %s; ", ap->a_name);
} }
printf("port = %d\n", ap.port); printf("port = %d\n", ap->port);
} }
return (0); return (0);
} }