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