900526
diff --git a/lib/dns/resolver.c b/lib/dns/resolver.c
900526
index 5ef2dd6..1b987dd 100644
900526
--- a/lib/dns/resolver.c
900526
+++ b/lib/dns/resolver.c
900526
@@ -526,7 +526,9 @@ valcreate(fetchctx_t *fctx, dns_adbaddrinfo_t *addrinfo, dns_name_t *name,
900526
 	valarg->addrinfo = addrinfo;
900526
 
900526
 	if (!ISC_LIST_EMPTY(fctx->validators))
900526
-		INSIST((valoptions & DNS_VALIDATOR_DEFER) != 0);
900526
+		valoptions |= DNS_VALIDATOR_DEFER;
900526
+	else
900526
+		valoptions &= ~DNS_VALIDATOR_DEFER;
900526
 
900526
 	result = dns_validator_create(fctx->res->view, name, type, rdataset,
900526
 				      sigrdataset, fctx->rmessage,
900526
@@ -4872,13 +4874,6 @@ cache_name(fetchctx_t *fctx, dns_name_t *name, dns_adbaddrinfo_t *addrinfo,
900526
 							   rdataset,
900526
 							   sigrdataset,
900526
 							   valoptions, task);
900526
-					/*
900526
-					 * Defer any further validations.
900526
-					 * This prevents multiple validators
900526
-					 * from manipulating fctx->rmessage
900526
-					 * simultaneously.
900526
-					 */
900526
-					valoptions |= DNS_VALIDATOR_DEFER;
900526
 				}
900526
 			} else if (CHAINING(rdataset)) {
900526
 				if (rdataset->type == dns_rdatatype_cname)
900526
@@ -4984,6 +4979,11 @@ cache_name(fetchctx_t *fctx, dns_name_t *name, dns_adbaddrinfo_t *addrinfo,
900526
 				       eresult == DNS_R_NCACHENXRRSET);
900526
 			}
900526
 			event->result = eresult;
900526
+			if (adbp != NULL && *adbp != NULL) {
900526
+				if (anodep != NULL && *anodep != NULL)
900526
+					dns_db_detachnode(*adbp, anodep);
900526
+				dns_db_detach(adbp);
900526
+			}
900526
 			dns_db_attach(fctx->cache, adbp);
900526
 			dns_db_transfernode(fctx->cache, &node, anodep);
900526
 			clone_results(fctx);
900526
@@ -5231,6 +5231,11 @@ ncache_message(fetchctx_t *fctx, dns_adbaddrinfo_t *addrinfo,
900526
 		fctx->attributes |= FCTX_ATTR_HAVEANSWER;
900526
 		if (event != NULL) {
900526
 			event->result = eresult;
900526
+			if (adbp != NULL && *adbp != NULL) {
900526
+				if (anodep != NULL && *anodep != NULL)
900526
+					dns_db_detachnode(*adbp, anodep);
900526
+				dns_db_detach(adbp);
900526
+			}
900526
 			dns_db_attach(fctx->cache, adbp);
900526
 			dns_db_transfernode(fctx->cache, &node, anodep);
900526
 			clone_results(fctx);
900526
@@ -6039,13 +6044,15 @@ static isc_result_t
900526
 answer_response(fetchctx_t *fctx) {
900526
 	isc_result_t result;
900526
 	dns_message_t *message;
900526
-	dns_name_t *name, *dname = NULL, *qname, tname, *ns_name;
900526
+	dns_name_t *name, *dname = NULL, *qname, *dqname, tname, *ns_name;
900526
+	dns_name_t *cname = NULL;
900526
 	dns_rdataset_t *rdataset, *ns_rdataset;
900526
 	isc_boolean_t done, external, chaining, aa, found, want_chaining;
900526
-	isc_boolean_t have_answer, found_cname, found_type, wanted_chaining;
900526
+	isc_boolean_t have_answer, found_cname, found_dname, found_type;
900526
+	isc_boolean_t wanted_chaining;
900526
 	unsigned int aflag;
900526
 	dns_rdatatype_t type;
900526
-	dns_fixedname_t fdname, fqname;
900526
+	dns_fixedname_t fdname, fqname, fqdname;
900526
 	dns_view_t *view;
900526
 
900526
 	FCTXTRACE("answer_response");
900526
@@ -6059,6 +6066,7 @@ answer_response(fetchctx_t *fctx) {
900526
 
900526
 	done = ISC_FALSE;
900526
 	found_cname = ISC_FALSE;
900526
+	found_dname = ISC_FALSE;
900526
 	found_type = ISC_FALSE;
900526
 	chaining = ISC_FALSE;
900526
 	have_answer = ISC_FALSE;
900526
@@ -6068,12 +6076,13 @@ answer_response(fetchctx_t *fctx) {
900526
 		aa = ISC_TRUE;
900526
 	else
900526
 		aa = ISC_FALSE;
900526
-	qname = &fctx->name;
900526
+	dqname = qname = &fctx->name;
900526
 	type = fctx->type;
900526
 	view = fctx->res->view;
900526
+	dns_fixedname_init(&fqdname);
900526
 	result = dns_message_firstname(message, DNS_SECTION_ANSWER);
900526
 	while (!done && result == ISC_R_SUCCESS) {
900526
-		dns_namereln_t namereln;
900526
+		dns_namereln_t namereln, dnamereln;
900526
 		int order;
900526
 		unsigned int nlabels;
900526
 
900526
@@ -6081,6 +6090,8 @@ answer_response(fetchctx_t *fctx) {
900526
 		dns_message_currentname(message, DNS_SECTION_ANSWER, &name);
900526
 		external = ISC_TF(!dns_name_issubdomain(name, &fctx->domain));
900526
 		namereln = dns_name_fullcompare(qname, name, &order, &nlabels);
900526
+		dnamereln = dns_name_fullcompare(dqname, name, &order,
900526
+						 &nlabels);
900526
 		if (namereln == dns_namereln_equal) {
900526
 			wanted_chaining = ISC_FALSE;
900526
 			for (rdataset = ISC_LIST_HEAD(name->list);
900526
@@ -6205,9 +6216,16 @@ answer_response(fetchctx_t *fctx) {
900526
 						 * a CNAME or DNAME).
900526
 						 */
900526
 						INSIST(!external);
900526
-						if (aflag ==
900526
-						    DNS_RDATASETATTR_ANSWER) {
900526
+						if ((rdataset->type !=
900526
+						     dns_rdatatype_cname) ||
900526
+						    !found_dname ||
900526
+						    (aflag ==
900526
+						     DNS_RDATASETATTR_ANSWER))
900526
+						{
900526
 							have_answer = ISC_TRUE;
900526
+							if (rdataset->type ==
900526
+							    dns_rdatatype_cname)
900526
+								cname = name;
900526
 							name->attributes |=
900526
 								DNS_NAMEATTR_ANSWER;
900526
 						}
900526
@@ -6303,11 +6321,11 @@ answer_response(fetchctx_t *fctx) {
900526
 					return (DNS_R_FORMERR);
900526
 				}
900526
 
900526
-				if (namereln != dns_namereln_subdomain) {
900526
+				if (dnamereln != dns_namereln_subdomain) {
900526
 					char qbuf[DNS_NAME_FORMATSIZE];
900526
 					char obuf[DNS_NAME_FORMATSIZE];
900526
 
900526
-					dns_name_format(qname, qbuf,
900526
+					dns_name_format(dqname, qbuf,
900526
 							sizeof(qbuf));
900526
 					dns_name_format(name, obuf,
900526
 							sizeof(obuf));
900526
@@ -6322,7 +6340,7 @@ answer_response(fetchctx_t *fctx) {
900526
 					want_chaining = ISC_TRUE;
900526
 					POST(want_chaining);
900526
 					aflag = DNS_RDATASETATTR_ANSWER;
900526
-					result = dname_target(rdataset, qname,
900526
+					result = dname_target(rdataset, dqname,
900526
 							      nlabels, &fdname);
900526
 					if (result == ISC_R_NOSPACE) {
900526
 						/*
900526
@@ -6339,10 +6357,13 @@ answer_response(fetchctx_t *fctx) {
900526
 
900526
 					dname = dns_fixedname_name(&fdname);
900526
 					if (!is_answertarget_allowed(view,
900526
-							qname, rdataset->type,
900526
-							dname, &fctx->domain)) {
900526
+						     dqname, rdataset->type,
900526
+						     dname, &fctx->domain))
900526
+					{
900526
 						return (DNS_R_SERVFAIL);
900526
 					}
900526
+					dqname = dns_fixedname_name(&fqdname);
900526
+					dns_name_copy(dname, dqname, NULL);
900526
 				} else {
900526
 					/*
900526
 					 * We've found a signature that
900526
@@ -6367,6 +6388,10 @@ answer_response(fetchctx_t *fctx) {
900526
 					INSIST(!external);
900526
 					if (aflag == DNS_RDATASETATTR_ANSWER) {
900526
 						have_answer = ISC_TRUE;
900526
+						found_dname = ISC_TRUE;
900526
+						if (cname != NULL)
900526
+							cname->attributes &=
900526
+							   ~DNS_NAMEATTR_ANSWER;
900526
 						name->attributes |=
900526
 							DNS_NAMEATTR_ANSWER;
900526
 					}