Module Name:    src
Committed By:   christos
Date:           Mon Oct 26 14:48:04 UTC 2015

Modified Files:
        src/lib/libc/net: getaddrinfo.c

Log Message:
PR/50367: Stefan Schaeckeler: libc resolver library does not resolve host
names with underscores ("_"). According to resolv.conf(5) RES_NOCHECKNAME
is on by default; well, it is and gethostbyname(3) obeys it
(gethnamaddr.c:maybe_ok), but getaddrinfo(3) up till this commit not.
XXX: pullup-7, pullup-6


To generate a diff of this commit:
cvs rdiff -u -r1.109 -r1.110 src/lib/libc/net/getaddrinfo.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/lib/libc/net/getaddrinfo.c
diff -u src/lib/libc/net/getaddrinfo.c:1.109 src/lib/libc/net/getaddrinfo.c:1.110
--- src/lib/libc/net/getaddrinfo.c:1.109	Thu Sep 10 10:05:06 2015
+++ src/lib/libc/net/getaddrinfo.c	Mon Oct 26 10:48:04 2015
@@ -1,4 +1,4 @@
-/*	$NetBSD: getaddrinfo.c,v 1.109 2015/09/10 14:05:06 christos Exp $	*/
+/*	$NetBSD: getaddrinfo.c,v 1.110 2015/10/26 14:48:04 christos Exp $	*/
 /*	$KAME: getaddrinfo.c,v 1.29 2000/08/31 17:26:57 itojun Exp $	*/
 
 /*
@@ -55,7 +55,7 @@
 
 #include <sys/cdefs.h>
 #if defined(LIBC_SCCS) && !defined(lint)
-__RCSID("$NetBSD: getaddrinfo.c,v 1.109 2015/09/10 14:05:06 christos Exp $");
+__RCSID("$NetBSD: getaddrinfo.c,v 1.110 2015/10/26 14:48:04 christos Exp $");
 #endif /* LIBC_SCCS and not lint */
 
 #ifndef RUMP_ACTION
@@ -226,8 +226,8 @@ static int addrconfig(uint64_t *);
 static int ip6_str2scopeid(char *, struct sockaddr_in6 *, u_int32_t *);
 #endif
 
-static struct addrinfo *getanswer(const querybuf *, int, const char *, int,
-    const struct addrinfo *);
+static struct addrinfo *getanswer(res_state, const querybuf *, int,
+    const char *, int, const struct addrinfo *);
 static void aisort(struct addrinfo *s, res_state res);
 static struct addrinfo * _dns_query(struct res_target *,
     const struct addrinfo *, res_state, int);
@@ -1162,9 +1162,11 @@ ip6_str2scopeid(char *scope, struct sock
 static const char AskedForGot[] =
 	"gethostby*.getanswer: asked for \"%s\", got \"%s\"";
 
+#define maybe_ok(res, nm, ok) (((res)->options & RES_NOCHECKNAME) != 0U || \
+                               (ok)(nm) != 0)
 static struct addrinfo *
-getanswer(const querybuf *answer, int anslen, const char *qname, int qtype,
-    const struct addrinfo *pai)
+getanswer(res_state res, const querybuf *answer, int anslen, const char *qname,
+    int qtype, const struct addrinfo *pai)
 {
 	struct addrinfo sentinel, *cur;
 	struct addrinfo ai, *aip;
@@ -1186,6 +1188,7 @@ getanswer(const querybuf *answer, int an
 	_DIAGASSERT(answer != NULL);
 	_DIAGASSERT(qname != NULL);
 	_DIAGASSERT(pai != NULL);
+	_DIAGASSERT(res != NULL);
 
 	memset(&sentinel, 0, sizeof(sentinel));
 	cur = &sentinel;
@@ -1218,7 +1221,7 @@ getanswer(const querybuf *answer, int an
 		return (NULL);
 	}
 	n = dn_expand(answer->buf, eom, cp, bp, (int)(ep - bp));
-	if ((n < 0) || !(*name_ok)(bp)) {
+	if ((n < 0) || !maybe_ok(res, bp, name_ok)) {
 		h_errno = NO_RECOVERY;
 		return (NULL);
 	}
@@ -1243,7 +1246,7 @@ getanswer(const querybuf *answer, int an
 	srvlist = NULL;
 	while (ancount-- > 0 && cp < eom && !had_error) {
 		n = dn_expand(answer->buf, eom, cp, bp, (int)(ep - bp));
-		if ((n < 0) || !(*name_ok)(bp)) {
+		if ((n < 0) || !maybe_ok(res, bp, name_ok)) {
 			had_error++;
 			continue;
 		}
@@ -1262,7 +1265,7 @@ getanswer(const querybuf *answer, int an
 		if ((qtype == T_A || qtype == T_AAAA || qtype == T_ANY) &&
 		    type == T_CNAME) {
 			n = dn_expand(answer->buf, eom, cp, tbuf, (int)sizeof tbuf);
-			if ((n < 0) || !(*name_ok)(tbuf)) {
+			if ((n < 0) || !maybe_ok(res, tbuf, name_ok)) {
 				had_error++;
 				continue;
 			}
@@ -1353,7 +1356,7 @@ getanswer(const querybuf *answer, int an
 			cp += INT16SZ;
 			n = dn_expand(answer->buf, eom, cp, tbuf,
 			    (int)sizeof(tbuf));
-			if ((n < 0) || !res_hnok(tbuf)) {
+			if ((n < 0) || !maybe_ok(res, tbuf, res_hnok)) {
 				had_error++;
 				continue;
 			}
@@ -1399,7 +1402,6 @@ getanswer(const querybuf *answer, int an
 	}
 
 	if (srvlist) {
-		res_state res;
 		/*
 		 * Check for explicit rejection.
 		 */
@@ -1408,16 +1410,6 @@ getanswer(const querybuf *answer, int an
 			h_errno = HOST_NOT_FOUND;
 			return NULL;
 		}
-		res = __res_get_state();
-		if (res == NULL) {
-			while (srvlist != NULL) {
-				srv = srvlist;
-				srvlist = srvlist->next;
-				free(srv);
-			}
-			h_errno = NETDB_INTERNAL;
-			return NULL;
-		}
 
 		while (srvlist) {
 			struct res_target q, q2;
@@ -1452,7 +1444,6 @@ getanswer(const querybuf *answer, int an
 			}
 			free(srv);
 		}
-		__res_put_state(res);
 	}
 	if (haveanswer) {
 		if (!sentinel.ai_next->ai_canonname)
@@ -1541,14 +1532,14 @@ _dns_query(struct res_target *q, const s
 			goto out;
 	}
 
-	ai = getanswer(buf, q->n, q->name, q->qtype, pai);
+	ai = getanswer(res, buf, q->n, q->name, q->qtype, pai);
 	if (ai) {
 		cur->ai_next = ai;
 		while (cur && cur->ai_next)
 			cur = cur->ai_next;
 	}
 	if (q2) {
-		ai = getanswer(buf2, q2->n, q2->name, q2->qtype, pai);
+		ai = getanswer(res, buf2, q2->n, q2->name, q2->qtype, pai);
 		if (ai)
 			cur->ai_next = ai;
  	}

Reply via email to