| 
									
										
										
										
											2016-03-27 07:37:23 +01:00
										 |  |  | package file
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-29 08:17:45 +01:00
										 |  |  | import (
 | 
					
						
							|  |  |  | 	"github.com/miekg/coredns/middleware/file/tree"
 | 
					
						
							| 
									
										
										
										
											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 (
 | 
					
						
							|  |  |  | 	Success Result = iota
 | 
					
						
							|  |  |  | 	NameError
 | 
					
						
							| 
									
										
										
										
											2016-03-29 08:17:45 +01:00
										 |  |  | 	NoData
 | 
					
						
							|  |  |  | 	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.
 | 
					
						
							|  |  |  | func (z *Zone) Lookup(qname string, qtype uint16, do bool) ([]dns.RR, []dns.RR, []dns.RR, Result) {
 | 
					
						
							| 
									
										
										
										
											2016-03-27 07:37:23 +01:00
										 |  |  | 	if qtype == dns.TypeSOA {
 | 
					
						
							|  |  |  | 		return z.lookupSOA(do)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-02 17:49:13 +01:00
										 |  |  | 	elem, res := z.Tree.Search(qname, qtype)
 | 
					
						
							| 
									
										
										
										
											2016-03-27 07:37:23 +01:00
										 |  |  | 	if elem == nil {
 | 
					
						
							| 
									
										
										
										
											2016-04-02 16:56:16 +01:00
										 |  |  | 		if res == tree.EmptyNonTerminal {
 | 
					
						
							| 
									
										
										
										
											2016-04-02 17:49:13 +01:00
										 |  |  | 			return z.emptyNonTerminal(qname, do)
 | 
					
						
							| 
									
										
										
										
											2016-03-29 21:25:06 +01:00
										 |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2016-04-02 17:49:13 +01:00
										 |  |  | 		return z.nameError(qname, qtype, do)
 | 
					
						
							| 
									
										
										
										
											2016-03-27 07:37:23 +01:00
										 |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2016-03-28 21:18:16 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-27 07:37:23 +01:00
										 |  |  | 	rrs := elem.Types(dns.TypeCNAME)
 | 
					
						
							|  |  |  | 	if len(rrs) > 0 { // should only ever be 1 actually; TODO(miek) check for this?
 | 
					
						
							| 
									
										
										
										
											2016-04-02 17:49:13 +01:00
										 |  |  | 		return z.lookupCNAME(rrs, qtype, do)
 | 
					
						
							| 
									
										
										
										
											2016-03-27 07:37:23 +01:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	rrs = elem.Types(qtype)
 | 
					
						
							|  |  |  | 	if len(rrs) == 0 {
 | 
					
						
							| 
									
										
										
										
											2016-03-29 08:17:45 +01:00
										 |  |  | 		return z.noData(elem, do)
 | 
					
						
							| 
									
										
										
										
											2016-03-28 21:18:16 +01:00
										 |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2016-03-29 13:22:17 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-28 21:18:16 +01:00
										 |  |  | 	if do {
 | 
					
						
							|  |  |  | 		sigs := elem.Types(dns.TypeRRSIG)
 | 
					
						
							|  |  |  | 		sigs = signatureForSubType(sigs, qtype)
 | 
					
						
							| 
									
										
										
										
											2016-03-31 09:25:22 +00:00
										 |  |  | 		rrs = append(rrs, sigs...)
 | 
					
						
							| 
									
										
										
										
											2016-03-27 07:37:23 +01:00
										 |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2016-03-29 08:17:45 +01:00
										 |  |  | 	return rrs, nil, nil, Success
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (z *Zone) noData(elem *tree.Elem, do bool) ([]dns.RR, []dns.RR, []dns.RR, Result) {
 | 
					
						
							|  |  |  | 	soa, _, _, _ := z.lookupSOA(do)
 | 
					
						
							|  |  |  | 	nsec := z.lookupNSEC(elem, do)
 | 
					
						
							|  |  |  | 	return nil, append(soa, nsec...), nil, Success
 | 
					
						
							| 
									
										
										
										
											2016-03-27 07:37:23 +01:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-02 17:49:13 +01:00
										 |  |  | func (z *Zone) emptyNonTerminal(qname string, do bool) ([]dns.RR, []dns.RR, []dns.RR, Result) {
 | 
					
						
							| 
									
										
										
										
											2016-04-02 16:56:16 +01:00
										 |  |  | 	soa, _, _, _ := z.lookupSOA(do)
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-02 17:49:13 +01:00
										 |  |  | 	elem := z.Tree.Prev(qname)
 | 
					
						
							| 
									
										
										
										
											2016-04-02 16:56:16 +01:00
										 |  |  | 	nsec := z.lookupNSEC(elem, do)
 | 
					
						
							|  |  |  | 	return nil, append(soa, nsec...), nil, Success
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-02 17:49:13 +01:00
										 |  |  | func (z *Zone) nameError(qname string, qtype uint16, do bool) ([]dns.RR, []dns.RR, []dns.RR, Result) {
 | 
					
						
							| 
									
										
										
										
											2016-03-31 09:25:22 +00:00
										 |  |  | 	// Is there a wildcard?
 | 
					
						
							| 
									
										
										
										
											2016-04-02 17:49:13 +01:00
										 |  |  | 	ce := z.ClosestEncloser(qname, qtype)
 | 
					
						
							|  |  |  | 	elem, _ := z.Tree.Search("*."+ce, qtype) // use result here?
 | 
					
						
							| 
									
										
										
										
											2016-03-31 09:25:22 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if elem != nil {
 | 
					
						
							| 
									
										
										
										
											2016-04-02 17:49:13 +01:00
										 |  |  | 		ret := elem.Types(qtype) // there can only be one of these (or zero)
 | 
					
						
							| 
									
										
										
										
											2016-03-31 09:25:22 +00:00
										 |  |  | 		switch {
 | 
					
						
							|  |  |  | 		case ret != nil:
 | 
					
						
							|  |  |  | 			if do {
 | 
					
						
							|  |  |  | 				sigs := elem.Types(dns.TypeRRSIG)
 | 
					
						
							| 
									
										
										
										
											2016-04-02 17:49:13 +01:00
										 |  |  | 				sigs = signatureForSubType(sigs, qtype)
 | 
					
						
							| 
									
										
										
										
											2016-03-31 09:25:22 +00:00
										 |  |  | 				ret = append(ret, sigs...)
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							| 
									
										
										
										
											2016-04-02 17:49:13 +01:00
										 |  |  | 			ret = wildcardReplace(qname, ce, ret)
 | 
					
						
							| 
									
										
										
										
											2016-03-31 09:25:22 +00:00
										 |  |  | 			return ret, nil, nil, Success
 | 
					
						
							|  |  |  | 		case ret == nil:
 | 
					
						
							|  |  |  | 			// nodata, nsec from the wildcard - type does not exist
 | 
					
						
							|  |  |  | 			// nsec proof that name does not exist
 | 
					
						
							|  |  |  | 			// TODO(miek)
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// name error
 | 
					
						
							| 
									
										
										
										
											2016-03-29 20:59:54 +01:00
										 |  |  | 	ret := []dns.RR{z.SOA}
 | 
					
						
							| 
									
										
										
										
											2016-03-28 21:18:16 +01:00
										 |  |  | 	if do {
 | 
					
						
							| 
									
										
										
										
											2016-03-29 20:47:45 +01:00
										 |  |  | 		ret = append(ret, z.SIG...)
 | 
					
						
							| 
									
										
										
										
											2016-04-02 17:49:13 +01:00
										 |  |  | 		ret = append(ret, z.nameErrorProof(qname, qtype)...)
 | 
					
						
							| 
									
										
										
										
											2016-03-28 21:18:16 +01:00
										 |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2016-03-29 20:47:45 +01:00
										 |  |  | 	return nil, ret, nil, NameError
 | 
					
						
							| 
									
										
										
										
											2016-03-29 08:17:45 +01:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (z *Zone) lookupSOA(do bool) ([]dns.RR, []dns.RR, []dns.RR, Result) {
 | 
					
						
							|  |  |  | 	if do {
 | 
					
						
							|  |  |  | 		ret := append([]dns.RR{z.SOA}, z.SIG...)
 | 
					
						
							|  |  |  | 		return ret, nil, nil, Success
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	return []dns.RR{z.SOA}, nil, nil, Success
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // lookupNSEC looks up nsec and sigs.
 | 
					
						
							|  |  |  | func (z *Zone) lookupNSEC(elem *tree.Elem, do bool) []dns.RR {
 | 
					
						
							|  |  |  | 	if !do {
 | 
					
						
							|  |  |  | 		return nil
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	nsec := elem.Types(dns.TypeNSEC)
 | 
					
						
							|  |  |  | 	if do {
 | 
					
						
							|  |  |  | 		sigs := elem.Types(dns.TypeRRSIG)
 | 
					
						
							|  |  |  | 		sigs = signatureForSubType(sigs, dns.TypeNSEC)
 | 
					
						
							|  |  |  | 		if len(sigs) > 0 {
 | 
					
						
							|  |  |  | 			nsec = append(nsec, sigs...)
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	return nsec
 | 
					
						
							| 
									
										
										
										
											2016-03-27 07:37:23 +01:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-02 17:49:13 +01:00
										 |  |  | func (z *Zone) lookupCNAME(rrs []dns.RR, qtype uint16, do bool) ([]dns.RR, []dns.RR, []dns.RR, Result) {
 | 
					
						
							|  |  |  | 	elem, _ := z.Tree.Search(rrs[0].(*dns.CNAME).Target, qtype)
 | 
					
						
							| 
									
										
										
										
											2016-03-28 21:18:16 +01:00
										 |  |  | 	if elem == nil {
 | 
					
						
							| 
									
										
										
										
											2016-03-29 08:17:45 +01:00
										 |  |  | 		return rrs, nil, nil, Success
 | 
					
						
							| 
									
										
										
										
											2016-03-28 21:18:16 +01:00
										 |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2016-04-02 17:49:13 +01:00
										 |  |  | 	extra := cnameForType(elem.All(), qtype)
 | 
					
						
							| 
									
										
										
										
											2016-03-28 21:18:16 +01:00
										 |  |  | 	if do {
 | 
					
						
							|  |  |  | 		sigs := elem.Types(dns.TypeRRSIG)
 | 
					
						
							| 
									
										
										
										
											2016-04-02 17:49:13 +01:00
										 |  |  | 		sigs = signatureForSubType(sigs, qtype)
 | 
					
						
							| 
									
										
										
										
											2016-03-28 21:18:16 +01:00
										 |  |  | 		if len(sigs) > 0 {
 | 
					
						
							|  |  |  | 			extra = append(extra, sigs...)
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2016-03-29 08:17:45 +01:00
										 |  |  | 	return rrs, nil, extra, Success
 | 
					
						
							| 
									
										
										
										
											2016-03-28 21:18:16 +01:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func cnameForType(targets []dns.RR, origQtype uint16) []dns.RR {
 | 
					
						
							|  |  |  | 	ret := []dns.RR{}
 | 
					
						
							|  |  |  | 	for _, target := range targets {
 | 
					
						
							|  |  |  | 		if target.Header().Rrtype == origQtype {
 | 
					
						
							|  |  |  | 			ret = append(ret, target)
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	return ret
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-27 07:37:23 +01:00
										 |  |  | // signatureForSubType range through the signature and return the correct
 | 
					
						
							|  |  |  | // ones for the subtype.
 | 
					
						
							| 
									
										
										
										
											2016-03-28 21:18:16 +01:00
										 |  |  | func signatureForSubType(rrs []dns.RR, subtype uint16) []dns.RR {
 | 
					
						
							| 
									
										
										
										
											2016-03-27 07:37:23 +01:00
										 |  |  | 	sigs := []dns.RR{}
 | 
					
						
							|  |  |  | 	for _, sig := range rrs {
 | 
					
						
							|  |  |  | 		if s, ok := sig.(*dns.RRSIG); ok {
 | 
					
						
							|  |  |  | 			if s.TypeCovered == subtype {
 | 
					
						
							|  |  |  | 				sigs = append(sigs, s)
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	return sigs
 | 
					
						
							|  |  |  | }
 | 
					
						
							| 
									
										
										
										
											2016-03-31 09:25:22 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-02 17:49:13 +01:00
										 |  |  | // wildcardReplace replaces the ownername with the original query name.
 | 
					
						
							|  |  |  | func wildcardReplace(qname, ce string, rrs []dns.RR) []dns.RR {
 | 
					
						
							| 
									
										
										
										
											2016-03-31 09:25:22 +00:00
										 |  |  | 	// need to copy here, otherwise we change in zone stuff
 | 
					
						
							|  |  |  | 	ret := make([]dns.RR, len(rrs))
 | 
					
						
							|  |  |  | 	for i, r := range rrs {
 | 
					
						
							|  |  |  | 		ret[i] = dns.Copy(r)
 | 
					
						
							| 
									
										
										
										
											2016-04-02 17:49:13 +01:00
										 |  |  | 		ret[i].Header().Name = qname
 | 
					
						
							| 
									
										
										
										
											2016-03-31 09:25:22 +00:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | 	return ret
 | 
					
						
							|  |  |  | }
 |