| 
									
										
										
										
											2016-03-27 07:37:23 +01:00
										 |  |  | package file | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-29 08:17:45 +01:00
										 |  |  | import ( | 
					
						
							| 
									
										
										
										
											2019-03-26 14:37:30 +00:00
										 |  |  | 	"context" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-23 18:32:44 +00:00
										 |  |  | 	"github.com/coredns/coredns/plugin/file/rrutil" | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 	"github.com/coredns/coredns/plugin/file/tree" | 
					
						
							| 
									
										
										
										
											2017-02-21 22:51:47 -08:00
										 |  |  | 	"github.com/coredns/coredns/request" | 
					
						
							| 
									
										
										
										
											2016-03-30 16:45:02 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-29 08:17:45 +01:00
										 |  |  | 	"github.com/miekg/dns" | 
					
						
							|  |  |  | ) | 
					
						
							| 
									
										
										
										
											2016-03-27 07:37:23 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | // Result is the result of a Lookup | 
					
						
							|  |  |  | type Result int | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const ( | 
					
						
							| 
									
										
										
										
											2016-09-23 09:14:12 +01:00
										 |  |  | 	// Success is a successful lookup. | 
					
						
							| 
									
										
										
										
											2016-03-27 07:37:23 +01:00
										 |  |  | 	Success Result = iota | 
					
						
							| 
									
										
										
										
											2016-09-23 09:14:12 +01:00
										 |  |  | 	// NameError indicates a nameerror | 
					
						
							| 
									
										
										
										
											2016-03-27 07:37:23 +01:00
										 |  |  | 	NameError | 
					
						
							| 
									
										
										
										
											2016-09-23 09:14:12 +01:00
										 |  |  | 	// Delegation indicates the lookup resulted in a delegation. | 
					
						
							| 
									
										
										
										
											2016-04-16 16:16:52 +01:00
										 |  |  | 	Delegation | 
					
						
							| 
									
										
										
										
											2016-09-23 09:14:12 +01:00
										 |  |  | 	// NoData indicates the lookup resulted in a NODATA. | 
					
						
							| 
									
										
										
										
											2016-03-29 08:17:45 +01:00
										 |  |  | 	NoData | 
					
						
							| 
									
										
										
										
											2016-09-23 09:14:12 +01:00
										 |  |  | 	// ServerFailure indicates a server failure during the lookup. | 
					
						
							| 
									
										
										
										
											2016-03-29 08:17:45 +01:00
										 |  |  | 	ServerFailure | 
					
						
							| 
									
										
										
										
											2016-03-27 07:37:23 +01:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-02 16:56:16 +01:00
										 |  |  | // Lookup looks up qname and qtype in the zone. When do is true DNSSEC records are included. | 
					
						
							| 
									
										
										
										
											2016-03-29 08:17:45 +01:00
										 |  |  | // Three sets of records are returned, one for the answer, one for authority  and one for the additional section. | 
					
						
							| 
									
										
										
										
											2019-03-26 14:37:30 +00:00
										 |  |  | func (z *Zone) Lookup(ctx context.Context, state request.Request, qname string) ([]dns.RR, []dns.RR, []dns.RR, Result) { | 
					
						
							| 
									
										
										
										
											2016-11-10 07:48:47 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	qtype := state.QType() | 
					
						
							|  |  |  | 	do := state.Do() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-04 09:06:06 -07:00
										 |  |  | 	// If z is a secondary zone we might not have transferred it, meaning we have | 
					
						
							| 
									
										
										
										
											2017-06-01 12:33:40 +01:00
										 |  |  | 	// all zone context setup, except the actual record. This means (for one thing) the apex | 
					
						
							|  |  |  | 	// is empty and we don't have a SOA record. | 
					
						
							| 
									
										
										
										
											2019-07-23 18:32:44 +00:00
										 |  |  | 	z.RLock() | 
					
						
							|  |  |  | 	ap := z.Apex | 
					
						
							|  |  |  | 	tr := z.Tree | 
					
						
							|  |  |  | 	z.RUnlock() | 
					
						
							|  |  |  | 	if ap.SOA == nil { | 
					
						
							| 
									
										
										
										
											2017-06-01 12:33:40 +01:00
										 |  |  | 		return nil, nil, nil, ServerFailure | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-05 14:39:49 +00:00
										 |  |  | 	if qtype == dns.TypeSOA { | 
					
						
							| 
									
										
										
										
											2019-07-23 18:32:44 +00:00
										 |  |  | 		return ap.soa(do), ap.ns(do), nil, Success | 
					
						
							| 
									
										
										
										
											2016-03-27 07:37:23 +01:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-04-16 16:16:52 +01:00
										 |  |  | 	if qtype == dns.TypeNS && qname == z.origin { | 
					
						
							| 
									
										
										
										
											2019-07-23 18:32:44 +00:00
										 |  |  | 		nsrrs := ap.ns(do) | 
					
						
							|  |  |  | 		glue := tr.Glue(nsrrs, do) // technically this isn't glue | 
					
						
							| 
									
										
										
										
											2016-11-05 14:39:49 +00:00
										 |  |  | 		return nsrrs, nil, glue, Success | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	var ( | 
					
						
							|  |  |  | 		found, shot    bool | 
					
						
							|  |  |  | 		parts          string | 
					
						
							|  |  |  | 		i              int | 
					
						
							|  |  |  | 		elem, wildElem *tree.Elem | 
					
						
							|  |  |  | 	) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Lookup: | 
					
						
							|  |  |  | 	// * Per label from the right, look if it exists. We do this to find potential | 
					
						
							|  |  |  | 	//   delegation records. | 
					
						
							|  |  |  | 	// * If the per-label search finds nothing, we will look for the wildcard at the | 
					
						
							|  |  |  | 	//   level. If found we keep it around. If we don't find the complete name we will | 
					
						
							|  |  |  | 	//   use the wildcard. | 
					
						
							|  |  |  | 	// | 
					
						
							|  |  |  | 	// Main for-loop handles delegation and finding or not finding the qname. | 
					
						
							| 
									
										
										
										
											2017-05-26 17:37:06 +08:00
										 |  |  | 	// If found we check if it is a CNAME/DNAME and do CNAME processing | 
					
						
							| 
									
										
										
										
											2019-08-21 16:08:55 -04:00
										 |  |  | 	// We also check if we have type and do a nodata response. | 
					
						
							| 
									
										
										
										
											2016-11-05 14:39:49 +00:00
										 |  |  | 	// | 
					
						
							|  |  |  | 	// If not found, we check the potential wildcard, and use that for further processing. | 
					
						
							|  |  |  | 	// If not found and no wildcard we will process this as an NXDOMAIN response. | 
					
						
							|  |  |  | 	for { | 
					
						
							|  |  |  | 		parts, shot = z.nameFromRight(qname, i) | 
					
						
							|  |  |  | 		// We overshot the name, break and check if we previously found something. | 
					
						
							|  |  |  | 		if shot { | 
					
						
							|  |  |  | 			break | 
					
						
							| 
									
										
										
										
											2016-10-27 21:01:04 +01:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2016-10-28 12:57:02 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-23 18:32:44 +00:00
										 |  |  | 		elem, found = tr.Search(parts) | 
					
						
							| 
									
										
										
										
											2016-11-05 14:39:49 +00:00
										 |  |  | 		if !found { | 
					
						
							|  |  |  | 			// Apex will always be found, when we are here we can search for a wildcard | 
					
						
							|  |  |  | 			// and save the result of that search. So when nothing match, but we have a | 
					
						
							| 
									
										
										
										
											2017-07-24 14:21:40 -07:00
										 |  |  | 			// wildcard we should expand the wildcard. | 
					
						
							| 
									
										
										
										
											2016-10-28 12:57:02 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-24 14:21:40 -07:00
										 |  |  | 			wildcard := replaceWithAsteriskLabel(parts) | 
					
						
							| 
									
										
										
										
											2019-07-23 18:32:44 +00:00
										 |  |  | 			if wild, found := tr.Search(wildcard); found { | 
					
						
							| 
									
										
										
										
											2017-07-24 14:21:40 -07:00
										 |  |  | 				wildElem = wild | 
					
						
							| 
									
										
										
										
											2016-11-05 14:39:49 +00:00
										 |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			// Keep on searching, because maybe we hit an empty-non-terminal (which aren't | 
					
						
							|  |  |  | 			// stored in the tree. Only when we have match the full qname (and possible wildcard | 
					
						
							|  |  |  | 			// we can be confident that we didn't find anything. | 
					
						
							|  |  |  | 			i++ | 
					
						
							|  |  |  | 			continue | 
					
						
							| 
									
										
										
										
											2016-10-27 21:01:04 +01:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2016-03-27 07:37:23 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-26 17:37:06 +08:00
										 |  |  | 		// If we see DNAME records, we should return those. | 
					
						
							| 
									
										
										
										
											2019-07-18 17:44:47 +00:00
										 |  |  | 		if dnamerrs := elem.Type(dns.TypeDNAME); dnamerrs != nil { | 
					
						
							| 
									
										
										
										
											2017-06-02 17:18:58 +01:00
										 |  |  | 			// Only one DNAME is allowed per name. We just pick the first one to synthesize from. | 
					
						
							| 
									
										
										
										
											2017-05-26 17:37:06 +08:00
										 |  |  | 			dname := dnamerrs[0] | 
					
						
							|  |  |  | 			if cname := synthesizeCNAME(state.Name(), dname.(*dns.DNAME)); cname != nil { | 
					
						
							| 
									
										
										
										
											2019-07-23 18:32:44 +00:00
										 |  |  | 				answer, ns, extra, rcode := z.externalLookup(ctx, state, elem, []dns.RR{cname}) | 
					
						
							| 
									
										
										
										
											2017-05-26 17:37:06 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-02 17:18:58 +01:00
										 |  |  | 				if do { | 
					
						
							| 
									
										
										
										
											2019-07-18 17:44:47 +00:00
										 |  |  | 					sigs := elem.Type(dns.TypeRRSIG) | 
					
						
							| 
									
										
										
										
											2019-07-23 18:32:44 +00:00
										 |  |  | 					sigs = rrutil.SubTypeSignature(sigs, dns.TypeDNAME) | 
					
						
							| 
									
										
										
										
											2017-06-02 17:18:58 +01:00
										 |  |  | 					dnamerrs = append(dnamerrs, sigs...) | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-26 17:37:06 +08:00
										 |  |  | 				// The relevant DNAME RR should be included in the answer section, | 
					
						
							|  |  |  | 				// if the DNAME is being employed as a substitution instruction. | 
					
						
							| 
									
										
										
										
											2017-06-02 17:18:58 +01:00
										 |  |  | 				answer = append(dnamerrs, answer...) | 
					
						
							| 
									
										
										
										
											2017-05-26 17:37:06 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 				return answer, ns, extra, rcode | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			// The domain name that owns a DNAME record is allowed to have other RR types | 
					
						
							|  |  |  | 			// at that domain name, except those have restrictions on what they can coexist | 
					
						
							|  |  |  | 			// with (e.g. another DNAME). So there is nothing special left here. | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-05 14:39:49 +00:00
										 |  |  | 		// If we see NS records, it means the name as been delegated, and we should return the delegation. | 
					
						
							| 
									
										
										
										
											2019-07-18 17:44:47 +00:00
										 |  |  | 		if nsrrs := elem.Type(dns.TypeNS); nsrrs != nil { | 
					
						
							| 
									
										
										
										
											2017-12-11 14:17:41 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			// If the query is specifically for DS and the qname matches the delegated name, we should | 
					
						
							|  |  |  | 			// return the DS in the answer section and leave the rest empty, i.e. just continue the loop | 
					
						
							|  |  |  | 			// and continue searching. | 
					
						
							|  |  |  | 			if qtype == dns.TypeDS && elem.Name() == qname { | 
					
						
							|  |  |  | 				i++ | 
					
						
							|  |  |  | 				continue | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-23 18:32:44 +00:00
										 |  |  | 			glue := tr.Glue(nsrrs, do) | 
					
						
							| 
									
										
										
										
											2016-11-05 14:39:49 +00:00
										 |  |  | 			if do { | 
					
						
							| 
									
										
										
										
											2019-07-23 18:32:44 +00:00
										 |  |  | 				dss := typeFromElem(elem, dns.TypeDS, do) | 
					
						
							| 
									
										
										
										
											2016-11-05 14:39:49 +00:00
										 |  |  | 				nsrrs = append(nsrrs, dss...) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			return nil, nsrrs, glue, Delegation | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		i++ | 
					
						
							| 
									
										
										
										
											2016-10-27 21:01:04 +01:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-10-28 12:57:02 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-05 14:39:49 +00:00
										 |  |  | 	// What does found and !shot mean - do we ever hit it? | 
					
						
							|  |  |  | 	if found && !shot { | 
					
						
							|  |  |  | 		return nil, nil, nil, ServerFailure | 
					
						
							| 
									
										
										
										
											2016-10-27 21:01:04 +01:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-10-28 12:57:02 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-05 14:39:49 +00:00
										 |  |  | 	// Found entire name. | 
					
						
							|  |  |  | 	if found && shot { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-18 17:44:47 +00:00
										 |  |  | 		if rrs := elem.Type(dns.TypeCNAME); len(rrs) > 0 && qtype != dns.TypeCNAME { | 
					
						
							| 
									
										
										
										
											2019-07-23 18:32:44 +00:00
										 |  |  | 			return z.externalLookup(ctx, state, elem, rrs) | 
					
						
							| 
									
										
										
										
											2016-11-05 14:39:49 +00:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-18 17:44:47 +00:00
										 |  |  | 		rrs := elem.Type(qtype) | 
					
						
							| 
									
										
										
										
											2016-11-05 14:39:49 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		// NODATA | 
					
						
							|  |  |  | 		if len(rrs) == 0 { | 
					
						
							| 
									
										
										
										
											2019-07-23 18:32:44 +00:00
										 |  |  | 			ret := ap.soa(do) | 
					
						
							| 
									
										
										
										
											2016-11-05 14:39:49 +00:00
										 |  |  | 			if do { | 
					
						
							| 
									
										
										
										
											2019-07-23 18:32:44 +00:00
										 |  |  | 				nsec := typeFromElem(elem, dns.TypeNSEC, do) | 
					
						
							| 
									
										
										
										
											2016-11-05 14:39:49 +00:00
										 |  |  | 				ret = append(ret, nsec...) | 
					
						
							| 
									
										
										
										
											2016-04-16 16:16:52 +01:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2016-11-05 14:39:49 +00:00
										 |  |  | 			return nil, ret, nil, NoData | 
					
						
							| 
									
										
										
										
											2016-04-16 16:16:52 +01:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2016-03-28 21:18:16 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-21 16:08:55 -04:00
										 |  |  | 		// Additional section processing for MX, SRV. Check response and see if any of the names are in bailiwick - | 
					
						
							| 
									
										
										
										
											2017-02-19 20:42:34 +00:00
										 |  |  | 		// if so add IP addresses to the additional section. | 
					
						
							| 
									
										
										
										
											2019-07-23 18:32:44 +00:00
										 |  |  | 		additional := z.additionalProcessing(rrs, do) | 
					
						
							| 
									
										
										
										
											2017-02-19 20:42:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-05 14:39:49 +00:00
										 |  |  | 		if do { | 
					
						
							| 
									
										
										
										
											2019-07-18 17:44:47 +00:00
										 |  |  | 			sigs := elem.Type(dns.TypeRRSIG) | 
					
						
							| 
									
										
										
										
											2019-07-23 18:32:44 +00:00
										 |  |  | 			sigs = rrutil.SubTypeSignature(sigs, qtype) | 
					
						
							| 
									
										
										
										
											2016-11-05 14:39:49 +00:00
										 |  |  | 			rrs = append(rrs, sigs...) | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2016-03-27 07:37:23 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-23 18:32:44 +00:00
										 |  |  | 		return rrs, ap.ns(do), additional, Success | 
					
						
							| 
									
										
										
										
											2016-03-29 13:22:17 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-27 07:37:23 +01:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-10-08 15:22:31 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-05 14:39:49 +00:00
										 |  |  | 	// Haven't found the original name. | 
					
						
							| 
									
										
										
										
											2016-03-27 07:37:23 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-06 08:32:07 +00:00
										 |  |  | 	// Found wildcard. | 
					
						
							| 
									
										
										
										
											2016-11-05 14:39:49 +00:00
										 |  |  | 	if wildElem != nil { | 
					
						
							| 
									
										
										
										
											2019-07-23 18:32:44 +00:00
										 |  |  | 		auth := ap.ns(do) | 
					
						
							| 
									
										
										
										
											2016-04-02 16:56:16 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-18 17:44:47 +00:00
										 |  |  | 		if rrs := wildElem.TypeForWildcard(dns.TypeCNAME, qname); len(rrs) > 0 { | 
					
						
							| 
									
										
										
										
											2019-07-23 18:32:44 +00:00
										 |  |  | 			return z.externalLookup(ctx, state, wildElem, rrs) | 
					
						
							| 
									
										
										
										
											2016-11-05 14:39:49 +00:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2016-04-02 16:56:16 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-18 17:44:47 +00:00
										 |  |  | 		rrs := wildElem.TypeForWildcard(qtype, qname) | 
					
						
							| 
									
										
										
										
											2016-03-31 09:25:22 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-06 08:32:07 +00:00
										 |  |  | 		// NODATA response. | 
					
						
							| 
									
										
										
										
											2016-11-05 14:39:49 +00:00
										 |  |  | 		if len(rrs) == 0 { | 
					
						
							| 
									
										
										
										
											2019-07-23 18:32:44 +00:00
										 |  |  | 			ret := ap.soa(do) | 
					
						
							| 
									
										
										
										
											2016-03-31 09:25:22 +00:00
										 |  |  | 			if do { | 
					
						
							| 
									
										
										
										
											2019-07-23 18:32:44 +00:00
										 |  |  | 				nsec := typeFromElem(wildElem, dns.TypeNSEC, do) | 
					
						
							| 
									
										
										
										
											2016-11-05 14:39:49 +00:00
										 |  |  | 				ret = append(ret, nsec...) | 
					
						
							| 
									
										
										
										
											2016-03-31 09:25:22 +00:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2016-11-05 14:39:49 +00:00
										 |  |  | 			return nil, ret, nil, Success | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2016-11-06 08:32:07 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-05 14:39:49 +00:00
										 |  |  | 		if do { | 
					
						
							| 
									
										
										
										
											2016-11-06 08:32:07 +00:00
										 |  |  | 			// An NSEC is needed to say no longer name exists under this wildcard. | 
					
						
							| 
									
										
										
										
											2019-07-23 18:32:44 +00:00
										 |  |  | 			if deny, found := tr.Prev(qname); found { | 
					
						
							|  |  |  | 				nsec := typeFromElem(deny, dns.TypeNSEC, do) | 
					
						
							| 
									
										
										
										
											2016-11-06 08:32:07 +00:00
										 |  |  | 				auth = append(auth, nsec...) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-18 17:44:47 +00:00
										 |  |  | 			sigs := wildElem.TypeForWildcard(dns.TypeRRSIG, qname) | 
					
						
							| 
									
										
										
										
											2019-07-23 18:32:44 +00:00
										 |  |  | 			sigs = rrutil.SubTypeSignature(sigs, qtype) | 
					
						
							| 
									
										
										
										
											2016-11-05 14:39:49 +00:00
										 |  |  | 			rrs = append(rrs, sigs...) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-31 09:25:22 +00:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2016-11-06 08:32:07 +00:00
										 |  |  | 		return rrs, auth, nil, Success | 
					
						
							| 
									
										
										
										
											2016-03-31 09:25:22 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-05 14:39:49 +00:00
										 |  |  | 	rcode := NameError | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Hacky way to get around empty-non-terminals. If a longer name does exist, but this qname, does not, it | 
					
						
							| 
									
										
										
										
											2017-02-19 20:42:34 +00:00
										 |  |  | 	// must be an empty-non-terminal. If so, we do the proper NXDOMAIN handling, but set the rcode to be success. | 
					
						
							| 
									
										
										
										
											2019-07-23 18:32:44 +00:00
										 |  |  | 	if x, found := tr.Next(qname); found { | 
					
						
							| 
									
										
										
										
											2016-11-05 14:39:49 +00:00
										 |  |  | 		if dns.IsSubDomain(qname, x.Name()) { | 
					
						
							|  |  |  | 			rcode = Success | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-23 18:32:44 +00:00
										 |  |  | 	ret := ap.soa(do) | 
					
						
							| 
									
										
										
										
											2016-03-28 21:18:16 +01:00
										 |  |  | 	if do { | 
					
						
							| 
									
										
										
										
											2019-07-23 18:32:44 +00:00
										 |  |  | 		deny, found := tr.Prev(qname) | 
					
						
							| 
									
										
										
										
											2017-12-11 14:32:51 +00:00
										 |  |  | 		if !found { | 
					
						
							|  |  |  | 			goto Out | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2019-07-23 18:32:44 +00:00
										 |  |  | 		nsec := typeFromElem(deny, dns.TypeNSEC, do) | 
					
						
							| 
									
										
										
										
											2016-11-05 14:39:49 +00:00
										 |  |  | 		ret = append(ret, nsec...) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if rcode != NameError { | 
					
						
							|  |  |  | 			goto Out | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		ce, found := z.ClosestEncloser(qname) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// wildcard denial only for NXDOMAIN | 
					
						
							|  |  |  | 		if found { | 
					
						
							|  |  |  | 			// wildcard denial | 
					
						
							|  |  |  | 			wildcard := "*." + ce.Name() | 
					
						
							| 
									
										
										
										
											2019-07-23 18:32:44 +00:00
										 |  |  | 			if ss, found := tr.Prev(wildcard); found { | 
					
						
							| 
									
										
										
										
											2016-11-05 14:39:49 +00:00
										 |  |  | 				// Only add this nsec if it is different than the one already added | 
					
						
							|  |  |  | 				if ss.Name() != deny.Name() { | 
					
						
							| 
									
										
										
										
											2019-07-23 18:32:44 +00:00
										 |  |  | 					nsec := typeFromElem(ss, dns.TypeNSEC, do) | 
					
						
							| 
									
										
										
										
											2016-11-05 14:39:49 +00:00
										 |  |  | 					ret = append(ret, nsec...) | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-28 21:18:16 +01:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-11-05 14:39:49 +00:00
										 |  |  | Out: | 
					
						
							|  |  |  | 	return nil, ret, nil, rcode | 
					
						
							| 
									
										
										
										
											2016-03-29 08:17:45 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-23 18:32:44 +00:00
										 |  |  | // typeFromElem returns the type tp from e and adds signatures (if they exist) and do is true. | 
					
						
							|  |  |  | func typeFromElem(elem *tree.Elem, tp uint16, do bool) []dns.RR { | 
					
						
							| 
									
										
										
										
											2019-07-18 17:44:47 +00:00
										 |  |  | 	rrs := elem.Type(tp) | 
					
						
							| 
									
										
										
										
											2016-03-29 08:17:45 +01:00
										 |  |  | 	if do { | 
					
						
							| 
									
										
										
										
											2019-07-18 17:44:47 +00:00
										 |  |  | 		sigs := elem.Type(dns.TypeRRSIG) | 
					
						
							| 
									
										
										
										
											2019-07-23 18:32:44 +00:00
										 |  |  | 		sigs = rrutil.SubTypeSignature(sigs, tp) | 
					
						
							|  |  |  | 		rrs = append(rrs, sigs...) | 
					
						
							| 
									
										
										
										
											2016-04-16 16:16:52 +01:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-11-05 14:39:49 +00:00
										 |  |  | 	return rrs | 
					
						
							| 
									
										
										
										
											2016-04-16 16:16:52 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-23 18:32:44 +00:00
										 |  |  | func (a Apex) soa(do bool) []dns.RR { | 
					
						
							| 
									
										
										
										
											2016-04-16 16:16:52 +01:00
										 |  |  | 	if do { | 
					
						
							| 
									
										
										
										
											2019-07-23 18:32:44 +00:00
										 |  |  | 		ret := append([]dns.RR{a.SOA}, a.SIGSOA...) | 
					
						
							| 
									
										
										
										
											2016-11-05 14:39:49 +00:00
										 |  |  | 		return ret | 
					
						
							| 
									
										
										
										
											2016-03-29 08:17:45 +01:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-07-23 18:32:44 +00:00
										 |  |  | 	return []dns.RR{a.SOA} | 
					
						
							| 
									
										
										
										
											2016-03-29 08:17:45 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-23 18:32:44 +00:00
										 |  |  | func (a Apex) ns(do bool) []dns.RR { | 
					
						
							| 
									
										
										
										
											2016-03-29 08:17:45 +01:00
										 |  |  | 	if do { | 
					
						
							| 
									
										
										
										
											2019-07-23 18:32:44 +00:00
										 |  |  | 		ret := append(a.NS, a.SIGNS...) | 
					
						
							| 
									
										
										
										
											2016-11-05 14:39:49 +00:00
										 |  |  | 		return ret | 
					
						
							| 
									
										
										
										
											2016-03-29 08:17:45 +01:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-07-23 18:32:44 +00:00
										 |  |  | 	return a.NS | 
					
						
							| 
									
										
										
										
											2016-03-27 07:37:23 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-23 18:32:44 +00:00
										 |  |  | // externalLookup adds signatures and tries to resolve CNAMEs that point to external names. | 
					
						
							|  |  |  | func (z *Zone) externalLookup(ctx context.Context, state request.Request, elem *tree.Elem, rrs []dns.RR) ([]dns.RR, []dns.RR, []dns.RR, Result) { | 
					
						
							| 
									
										
										
										
											2016-11-10 07:48:47 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	qtype := state.QType() | 
					
						
							|  |  |  | 	do := state.Do() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-09 10:02:26 +00:00
										 |  |  | 	if do { | 
					
						
							| 
									
										
										
										
											2019-07-18 17:44:47 +00:00
										 |  |  | 		sigs := elem.Type(dns.TypeRRSIG) | 
					
						
							| 
									
										
										
										
											2019-07-23 18:32:44 +00:00
										 |  |  | 		sigs = rrutil.SubTypeSignature(sigs, dns.TypeCNAME) | 
					
						
							|  |  |  | 		rrs = append(rrs, sigs...) | 
					
						
							| 
									
										
										
										
											2016-11-09 10:02:26 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-09 21:26:49 +00:00
										 |  |  | 	targetName := rrs[0].(*dns.CNAME).Target | 
					
						
							|  |  |  | 	elem, _ = z.Tree.Search(targetName) | 
					
						
							| 
									
										
										
										
											2016-03-28 21:18:16 +01:00
										 |  |  | 	if elem == nil { | 
					
						
							| 
									
										
										
										
											2019-07-23 18:32:44 +00:00
										 |  |  | 		rrs = append(rrs, z.doLookup(ctx, state, targetName, qtype)...) | 
					
						
							|  |  |  | 		return rrs, z.Apex.ns(do), nil, Success | 
					
						
							| 
									
										
										
										
											2016-03-28 21:18:16 +01:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-10-08 15:22:31 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-09 10:02:26 +00:00
										 |  |  | 	i := 0 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Redo: | 
					
						
							| 
									
										
										
										
											2019-07-18 17:44:47 +00:00
										 |  |  | 	cname := elem.Type(dns.TypeCNAME) | 
					
						
							| 
									
										
										
										
											2016-11-09 10:02:26 +00:00
										 |  |  | 	if len(cname) > 0 { | 
					
						
							|  |  |  | 		rrs = append(rrs, cname...) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if do { | 
					
						
							| 
									
										
										
										
											2019-07-18 17:44:47 +00:00
										 |  |  | 			sigs := elem.Type(dns.TypeRRSIG) | 
					
						
							| 
									
										
										
										
											2019-07-23 18:32:44 +00:00
										 |  |  | 			sigs = rrutil.SubTypeSignature(sigs, dns.TypeCNAME) | 
					
						
							|  |  |  | 			rrs = append(rrs, sigs...) | 
					
						
							| 
									
										
										
										
											2016-11-09 10:02:26 +00:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2016-11-09 21:26:49 +00:00
										 |  |  | 		targetName := cname[0].(*dns.CNAME).Target | 
					
						
							|  |  |  | 		elem, _ = z.Tree.Search(targetName) | 
					
						
							| 
									
										
										
										
											2016-11-09 10:02:26 +00:00
										 |  |  | 		if elem == nil { | 
					
						
							| 
									
										
										
										
											2019-07-23 18:32:44 +00:00
										 |  |  | 			rrs = append(rrs, z.doLookup(ctx, state, targetName, qtype)...) | 
					
						
							|  |  |  | 			return rrs, z.Apex.ns(do), nil, Success | 
					
						
							| 
									
										
										
										
											2016-11-09 10:02:26 +00:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		i++ | 
					
						
							| 
									
										
										
										
											2019-07-23 18:32:44 +00:00
										 |  |  | 		if i > 8 { | 
					
						
							|  |  |  | 			return rrs, z.Apex.ns(do), nil, Success | 
					
						
							| 
									
										
										
										
											2016-11-09 10:02:26 +00:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		goto Redo | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-23 18:32:44 +00:00
										 |  |  | 	targets := rrutil.CNAMEForType(elem.All(), qtype) | 
					
						
							| 
									
										
										
										
											2016-11-09 10:02:26 +00:00
										 |  |  | 	if len(targets) > 0 { | 
					
						
							|  |  |  | 		rrs = append(rrs, targets...) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if do { | 
					
						
							| 
									
										
										
										
											2019-07-18 17:44:47 +00:00
										 |  |  | 			sigs := elem.Type(dns.TypeRRSIG) | 
					
						
							| 
									
										
										
										
											2019-07-23 18:32:44 +00:00
										 |  |  | 			sigs = rrutil.SubTypeSignature(sigs, qtype) | 
					
						
							|  |  |  | 			rrs = append(rrs, sigs...) | 
					
						
							| 
									
										
										
										
											2016-03-28 21:18:16 +01:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-11-09 10:02:26 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-23 18:32:44 +00:00
										 |  |  | 	return rrs, z.Apex.ns(do), nil, Success | 
					
						
							| 
									
										
										
										
											2016-03-28 21:18:16 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-23 18:32:44 +00:00
										 |  |  | func (z *Zone) doLookup(ctx context.Context, state request.Request, target string, qtype uint16) []dns.RR { | 
					
						
							| 
									
										
										
										
											2019-03-26 14:37:30 +00:00
										 |  |  | 	m, e := z.Upstream.Lookup(ctx, state, target, qtype) | 
					
						
							| 
									
										
										
										
											2016-11-10 12:58:40 +00:00
										 |  |  | 	if e != nil { | 
					
						
							| 
									
										
										
										
											2018-06-12 14:54:37 +01:00
										 |  |  | 		return nil | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if m == nil { | 
					
						
							| 
									
										
										
										
											2016-11-10 07:48:47 +00:00
										 |  |  | 		return nil | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return m.Answer | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-19 20:42:34 +00:00
										 |  |  | // additionalProcessing checks the current answer section and retrieves A or AAAA records | 
					
						
							|  |  |  | // (and possible SIGs) to need to be put in the additional section. | 
					
						
							| 
									
										
										
										
											2019-07-23 18:32:44 +00:00
										 |  |  | func (z *Zone) additionalProcessing(answer []dns.RR, do bool) (extra []dns.RR) { | 
					
						
							| 
									
										
										
										
											2017-02-19 20:42:34 +00:00
										 |  |  | 	for _, rr := range answer { | 
					
						
							|  |  |  | 		name := "" | 
					
						
							|  |  |  | 		switch x := rr.(type) { | 
					
						
							|  |  |  | 		case *dns.SRV: | 
					
						
							|  |  |  | 			name = x.Target | 
					
						
							|  |  |  | 		case *dns.MX: | 
					
						
							|  |  |  | 			name = x.Mx | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2019-10-01 15:22:42 +03:00
										 |  |  | 		if len(name) == 0 || !dns.IsSubDomain(z.origin, name) { | 
					
						
							| 
									
										
										
										
											2017-02-19 20:42:34 +00:00
										 |  |  | 			continue | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		elem, _ := z.Tree.Search(name) | 
					
						
							|  |  |  | 		if elem == nil { | 
					
						
							|  |  |  | 			continue | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-18 17:44:47 +00:00
										 |  |  | 		sigs := elem.Type(dns.TypeRRSIG) | 
					
						
							| 
									
										
										
										
											2017-02-19 20:42:34 +00:00
										 |  |  | 		for _, addr := range []uint16{dns.TypeA, dns.TypeAAAA} { | 
					
						
							| 
									
										
										
										
											2019-07-18 17:44:47 +00:00
										 |  |  | 			if a := elem.Type(addr); a != nil { | 
					
						
							| 
									
										
										
										
											2017-02-19 20:42:34 +00:00
										 |  |  | 				extra = append(extra, a...) | 
					
						
							|  |  |  | 				if do { | 
					
						
							| 
									
										
										
										
											2019-07-23 18:32:44 +00:00
										 |  |  | 					sig := rrutil.SubTypeSignature(sigs, addr) | 
					
						
							| 
									
										
										
										
											2017-02-19 20:42:34 +00:00
										 |  |  | 					extra = append(extra, sig...) | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return extra | 
					
						
							|  |  |  | } |