From f3d3703fe38d8dee6bd86349a8fb8b30749d8b49 Mon Sep 17 00:00:00 2001
From: Matthijs Mekking <matthijs@isc.org>
Date: Tue, 26 Feb 2019 15:55:29 +0100
Subject: [PATCH] Fix nxdomain-redirect assertion failure

- Always set is_zonep in query_getdb; previously it was only set if
  result was ISC_R_SUCCESS or ISC_R_NOTFOUND.
- Don't reset is_zone for redirect.
- Style cleanup.

(cherry picked from commit a85cc641d7a4c66cbde03cc4e31edc038a24df46)
(cherry picked from commit 486a201149ab7523e3b3089fc84f20d7f1a07a24)
Conflict: mv lib/ns/query.c to bin/named/query.c
Reference: https://gitlab.isc.org/isc-projects/bind9/-/commit/f3d3703fe38d8dee6bd86349a8fb8b30749d8b49
---
 bin/named/query.c | 32 +++++++++++++++++++-------------
 1 file changed, 19 insertions(+), 13 deletions(-)
diff --git a/bin/named/query.c b/bin/named/query.c
index 33e6d59..eebbd6d 100644
--- a/bin/named/query.c
+++ b/bin/named/query.c
@@ -1260,7 +1260,6 @@ query_getdb(ns_client_t *client, dns_name_t *name, dns_rdatatype_t qtype,
 	    dns_dbversion_t **versionp, bool *is_zonep)
 {
 	isc_result_t result;
-
 	isc_result_t tresult;
 	unsigned int namelabels;
 	unsigned int zonelabels;
@@ -1277,8 +1276,9 @@ query_getdb(ns_client_t *client, dns_name_t *name, dns_rdatatype_t qtype,
 				 dbp, versionp);
 
 	/* See how many labels are in the zone's name.	  */
-	if (result == ISC_R_SUCCESS && zone != NULL)
+	if (result == ISC_R_SUCCESS && zone != NULL) {
 		zonelabels = dns_name_countlabels(dns_zone_getorigin(zone));
+	}
 
 	/*
 	 * If # zone labels < # name labels, try to find an even better match
@@ -1345,8 +1345,11 @@ query_getdb(ns_client_t *client, dns_name_t *name, dns_rdatatype_t qtype,
 		 * If neither attempt above succeeded, return the cache instead
 		 */
 		*is_zonep = true;
-	} else if (result == ISC_R_NOTFOUND) {
-		result = query_getcachedb(client, name, qtype, dbp, options);
+	} else {
+		if (result == ISC_R_NOTFOUND) {
+			result = query_getcachedb(client, name, qtype, dbp,
+						  options);
+		}
 		*is_zonep = false;
 	}
 	return (result);
@@ -6667,11 +6670,13 @@ redirect2(ns_client_t *client, dns_name_t *name, dns_rdataset_t *rdataset,
 
 	CTRACE(ISC_LOG_DEBUG(3), "redirect2");
 
-	if (client->view->redirectzone == NULL)
+	if (client->view->redirectzone == NULL) {
 		return (ISC_R_NOTFOUND);
+	}
 
-	if (dns_name_issubdomain(name, client->view->redirectzone))
+	if (dns_name_issubdomain(name, client->view->redirectzone)) {
 		return (ISC_R_NOTFOUND);
+	}
 
 	found = dns_fixedname_initname(&fixed);
 	dns_rdataset_init(&trdataset);
@@ -6679,8 +6684,9 @@ redirect2(ns_client_t *client, dns_name_t *name, dns_rdataset_t *rdataset,
 	dns_clientinfomethods_init(&cm, ns_client_sourceip);
 	dns_clientinfo_init(&ci, client, NULL);
 
-	if (WANTDNSSEC(client) && dns_db_iszone(*dbp) && dns_db_issecure(*dbp))
+	if (WANTDNSSEC(client) && dns_db_iszone(*dbp) && dns_db_issecure(*dbp)) {
 		return (ISC_R_NOTFOUND);
+	}
 
 	if (WANTDNSSEC(client) && dns_rdataset_isassociated(rdataset)) {
 		if (rdataset->trust == dns_trust_secure)
@@ -6717,16 +6723,19 @@ redirect2(ns_client_t *client, dns_name_t *name, dns_rdataset_t *rdataset,
 					      redirectname, NULL);
 		if (result != ISC_R_SUCCESS)
 			return (ISC_R_NOTFOUND);
-	} else
+	} else {
 		dns_name_copy(redirectname, client->view->redirectzone, NULL);
+	}
 
 	options = 0;
 	result = query_getdb(client, redirectname, qtype, options, &zone,
 			     &db, &version, &is_zone);
-	if (result != ISC_R_SUCCESS)
+	if (result != ISC_R_SUCCESS) {
 		return (ISC_R_NOTFOUND);
-	if (zone != NULL)
+	}
+	if (zone != NULL) {
 		dns_zone_detach(&zone);
+	}
 
 	/*
 	 * Lookup the requested data in the redirect zone.
@@ -6996,7 +7005,6 @@ query_find(ns_client_t *client, dns_fetchevent_t *event, dns_rdatatype_t qtype)
 			RESTORE(node, client->query.redirect.node);
 			RESTORE(zone, client->query.redirect.zone);
 			authoritative = client->query.redirect.authoritative;
-			is_zone = client->query.redirect.is_zone;
 
 			/*
 			 * Free resources used while recursing.
@@ -7093,7 +7101,6 @@ query_find(ns_client_t *client, dns_fetchevent_t *event, dns_rdatatype_t qtype)
 			free_devent(client, ISC_EVENT_PTR(&event), &event);
 		} else if (REDIRECT(client)) {
 			result = client->query.redirect.result;
-			is_zone = client->query.redirect.is_zone;
 		} else {
 			result = event->result;
 		}
-- 
2.23.0

