From 85ef5a704750b2398bc65652147ba161fbe015fb Mon Sep 17 00:00:00 2001
From: Lubos Slovak <lubos.slovak@nic.cz>
Date: Wed, 22 Sep 2010 17:06:59 +0200
Subject: [PATCH] Fixed answering to work with empty non-terminals.

If all labels matched & the node is empty non-terminal, continue
  the search.
If some labels matched & the node has no wildcard node, return
  NXDOMAIN.
---
 samples/example.com.zone |  1 +
 src/other/debug.h        |  2 +-
 src/server/name-server.c | 20 ++++++++++++--------
 3 files changed, 14 insertions(+), 9 deletions(-)

diff --git a/samples/example.com.zone b/samples/example.com.zone
index c48a241f4e..10c913439a 100644
--- a/samples/example.com.zone
+++ b/samples/example.com.zone
@@ -53,3 +53,4 @@ e.example.com.	DNAME	bogus25.com.
 
 f.g	A	10.0.0.20
 h.i.j.k	A	10.0.0.21
+*.j.k	A	10.0.0.22
diff --git a/src/other/debug.h b/src/other/debug.h
index 97cc4a6102..158eb97c7a 100644
--- a/src/other/debug.h
+++ b/src/other/debug.h
@@ -9,7 +9,7 @@
 //#define CUCKOO_DEBUG
 //#define CUCKOO_DEBUG_HASH
 //#define ZP_DEBUG
-//#define NS_DEBUG
+#define NS_DEBUG
 //#define ZDB_DEBUG
 #define ZDB_DEBUG_INSERT_CHECK
 //#define ZN_DEBUG
diff --git a/src/server/name-server.c b/src/server/name-server.c
index 8705d6b0be..42ae70432c 100644
--- a/src/server/name-server.c
+++ b/src/server/name-server.c
@@ -519,8 +519,8 @@ void ns_answer_from_node( const zn_node *node, const zdb_zone *zone,
 		// node is now set to the canonical name node (if found)
 		if (node == NULL) {
 			// TODO: add SOA
-			ns_put_authority_soa(zone, response);
-			ldns_pkt_set_rcode(response, LDNS_RCODE_NXDOMAIN);
+			//ns_put_authority_soa(zone, response);
+			//ldns_pkt_set_rcode(response, LDNS_RCODE_NXDOMAIN);
 			return;
 		}
 	}
@@ -669,6 +669,11 @@ search:
 	}
 
 	if (labels_found == labels) {	// whole QNAME found
+		debug_ns("All labels matched.\n");
+		// if an empty non-terminal, continue the search
+		if (zn_is_empty(node) == 0) {
+			goto search;
+		}
 		ns_answer_from_node(node, zone, qname, ldns_rr_get_type(question),
 							response, copied_rrs);
 	} else {	// only part of QNAME found
@@ -699,13 +704,12 @@ search:
 				ns_answer_from_node(
 					wildcard_node, zone, ldns_rr_owner(question),
 					ldns_rr_get_type(question), response, copied_rrs);
-			} else if (zone->apex == node) {
-				// if we ended in the zone apex, the name is not in the zone
-				ns_put_authority_soa(zone, response);
-				ldns_pkt_set_rcode(response, LDNS_RCODE_NXDOMAIN);
 			} else {
-				// continue searching for the new qname
-				goto search;
+				// return NXDOMAIN
+				ldns_pkt_set_rcode(response, LDNS_RCODE_NXDOMAIN);
+				ldns_rdf_deep_free(wildcard);
+				ldns_rdf_deep_free(qname);
+				return;
 			}
 			ldns_rdf_deep_free(wildcard);
 		}
-- 
GitLab