900526
commit 524f5c0d8fa5bd55c98243be889528f48437a2f7
900526
Author: Mark Andrews <marka@isc.org>
900526
Date:   Fri Dec 9 12:50:18 2016 +1100
900526
900526
    4530.   [bug]           Change 4489 broke the handling of CNAME -> DNAME
900526
                            in responses resulting in SERVFAIL being returned.
900526
                            [RT #43779]
900526
    
900526
    (cherry picked from commit 60cb462c56536f307fac4db8bdebf1247e2b5f66)
900526
900526
diff --git a/bin/tests/system/dname/ns2/example.db b/bin/tests/system/dname/ns2/example.db
900526
index ece3506..4289134 100644
900526
--- a/bin/tests/system/dname/ns2/example.db
900526
+++ b/bin/tests/system/dname/ns2/example.db
900526
@@ -29,4 +29,6 @@ a.short			A	10.0.0.1
900526
 short-dname		DNAME	short
900526
 a.longlonglonglonglonglonglonglonglonglonglonglonglong	A 10.0.0.2
900526
 long-dname		DNAME	longlonglonglonglonglonglonglonglonglonglonglonglong
900526
-;
900526
+cname			CNAME	a.cnamedname
900526
+cnamedname		DNAME	target
900526
+a.target		A	10.0.0.3
900526
diff --git a/bin/tests/system/dname/tests.sh b/bin/tests/system/dname/tests.sh
900526
index d22f54b..04bfcb2 100644
900526
--- a/bin/tests/system/dname/tests.sh
900526
+++ b/bin/tests/system/dname/tests.sh
900526
@@ -63,6 +63,24 @@ grep "status: YXDOMAIN" dig.out.ns4.toolong > /dev/null || ret=1
900526
 if [ $ret != 0 ]; then echo "I:failed"; fi
900526
 status=`expr $status + $ret`
900526
 
900526
+echo "I:checking cname to dname from authoritative"
900526
+ret=0
900526
+$DIG cname.example @10.53.0.2 a -p 5300 > dig.out.ns2.cname
900526
+grep "status: NOERROR" dig.out.ns2.cname > /dev/null || ret=1
900526
+if [ $ret != 0 ]; then echo "I:failed"; fi
900526
+status=`expr $status + $ret`
900526
+
900526
+echo "I:checking cname to dname from recursive"
900526
+ret=0
900526
+$DIG cname.example @10.53.0.4 a -p 5300 > dig.out.ns4.cname
900526
+grep "status: NOERROR" dig.out.ns4.cname > /dev/null || ret=1
900526
+grep '^cname.example.' dig.out.ns4.cname > /dev/null || ret=1
900526
+grep '^cnamedname.example.' dig.out.ns4.cname > /dev/null || ret=1
900526
+grep '^a.cnamedname.example.' dig.out.ns4.cname > /dev/null || ret=1
900526
+grep '^a.target.example.' dig.out.ns4.cname > /dev/null || ret=1
900526
+if [ $ret != 0 ]; then echo "I:failed"; fi
900526
+status=`expr $status + $ret`
900526
+
900526
 echo "I:exit status: $status"
900526
 
900526
 exit $status
900526
diff --git a/lib/dns/resolver.c b/lib/dns/resolver.c
900526
index 4bef072..de80928 100644
900526
--- a/lib/dns/resolver.c
900526
+++ b/lib/dns/resolver.c
900526
@@ -6463,7 +6463,7 @@ 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, *dqname, tname, *ns_name;
900526
+	dns_name_t *name, *dname = NULL, *qname, 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
@@ -6471,7 +6471,7 @@ answer_response(fetchctx_t *fctx) {
900526
 	isc_boolean_t wanted_chaining;
900526
 	unsigned int aflag;
900526
 	dns_rdatatype_t type;
900526
-	dns_fixedname_t fdname, fqname, fqdname;
900526
+	dns_fixedname_t fdname, fqname;
900526
 	dns_view_t *view;
900526
 
900526
 	FCTXTRACE("answer_response");
900526
@@ -6495,13 +6495,12 @@ answer_response(fetchctx_t *fctx) {
900526
 		aa = ISC_TRUE;
900526
 	else
900526
 		aa = ISC_FALSE;
900526
-	dqname = qname = &fctx->name;
900526
+	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, dnamereln;
900526
+		dns_namereln_t namereln;
900526
 		int order;
900526
 		unsigned int nlabels;
900526
 
900526
@@ -6509,8 +6508,6 @@ 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
@@ -6763,11 +6760,24 @@ answer_response(fetchctx_t *fctx) {
900526
 					return (DNS_R_FORMERR);
900526
 				}
900526
 
900526
-				if (dnamereln != dns_namereln_subdomain) {
900526
+				/*
900526
+				 * If DNAME + synthetic CNAME then the
900526
+				 * namereln is dns_namereln_subdomain.
900526
+				 *
900526
+				 * If synthetic CNAME + DNAME then the
900526
+				 * namereln is dns_namereln_commonancestor
900526
+				 * and the number of label must match the
900526
+				 * DNAME.  This order is not RFC compliant.
900526
+				 */
900526
+
900526
+				if (namereln != dns_namereln_subdomain &&
900526
+				    (namereln != dns_namereln_commonancestor ||
900526
+				     nlabels != dns_name_countlabels(name)))
900526
+				{
900526
 					char qbuf[DNS_NAME_FORMATSIZE];
900526
 					char obuf[DNS_NAME_FORMATSIZE];
900526
 
900526
-					dns_name_format(dqname, qbuf,
900526
+					dns_name_format(qname, qbuf,
900526
 							sizeof(qbuf));
900526
 					dns_name_format(name, obuf,
900526
 							sizeof(obuf));
900526
@@ -6782,7 +6792,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, dqname,
900526
+					result = dname_target(rdataset, qname,
900526
 							      nlabels, &fdname);
900526
 					if (result == ISC_R_NOSPACE) {
900526
 						/*
900526
@@ -6799,13 +6809,11 @@ answer_response(fetchctx_t *fctx) {
900526
 
900526
 					dname = dns_fixedname_name(&fdname);
900526
 					if (!is_answertarget_allowed(view,
900526
-						     dqname, rdataset->type,
900526
+						     qname, 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
@@ -6951,7 +6959,8 @@ answer_response(fetchctx_t *fctx) {
900526
 						rdataset->trust =
900526
 						    dns_trust_additional;
900526
 
900526
-					if (rdataset->type == dns_rdatatype_ns) {
900526
+					if (rdataset->type == dns_rdatatype_ns)
900526
+					{
900526
 						ns_name = name;
900526
 						ns_rdataset = rdataset;
900526
 					}