coredns/plugin/file/lookup.go

436 lines
12 KiB
Go
Raw Normal View History

package file
2016-03-29 08:17:45 +01:00
import (
"context"
"github.com/coredns/coredns/core/dnsserver"
"github.com/coredns/coredns/plugin/file/rrutil"
"github.com/coredns/coredns/plugin/file/tree"
"github.com/coredns/coredns/plugin/metadata"
"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"
)
// Result is the result of a Lookup
type Result int
const (
Golint2 (#280) * Fix linter errors * More linting fixes * More docs and making members private that dont need to be public * Fix linter errors * More linting fixes * More docs and making members private that dont need to be public * More lint fixes This leaves: ~~~ middleware/kubernetes/nametemplate/nametemplate.go:64:6: exported type NameTemplate should have comment or be unexported middleware/kubernetes/nametemplate/nametemplate.go:71:1: exported method NameTemplate.SetTemplate should have comment or be unexported middleware/kubernetes/nametemplate/nametemplate.go:108:1: exported method NameTemplate.GetZoneFromSegmentArray should have comment or be unexported middleware/kubernetes/nametemplate/nametemplate.go:116:1: exported method NameTemplate.GetNamespaceFromSegmentArray should have comment or be unexported middleware/kubernetes/nametemplate/nametemplate.go:120:1: exported method NameTemplate.GetServiceFromSegmentArray should have comment or be unexported middleware/kubernetes/nametemplate/nametemplate.go:124:1: exported method NameTemplate.GetTypeFromSegmentArray should have comment or be unexported middleware/kubernetes/nametemplate/nametemplate.go:135:1: exported method NameTemplate.GetSymbolFromSegmentArray should have comment or be unexported middleware/kubernetes/nametemplate/nametemplate.go:167:1: exported method NameTemplate.IsValid should have comment or be unexported middleware/kubernetes/nametemplate/nametemplate.go:182:6: exported type NameValues should have comment or be unexported middleware/kubernetes/util/util.go:1:1: package comment should be of the form "Package util ..." middleware/kubernetes/util/util.go:27:2: exported const WildcardStar should have comment (or a comment on this block) or be unexported middleware/proxy/lookup.go:66:1: exported method Proxy.Forward should have comment or be unexported middleware/proxy/proxy.go:24:6: exported type Client should have comment or be unexported middleware/proxy/proxy.go:107:1: exported function Clients should have comment or be unexported middleware/proxy/reverseproxy.go:10:6: exported type ReverseProxy should have comment or be unexported middleware/proxy/reverseproxy.go:16:1: exported method ReverseProxy.ServeDNS should have comment or be unexported middleware/proxy/upstream.go:42:6: exported type Options should have comment or be unexported ~~~ I plan on reworking the proxy anyway, so I'll leave that be.
2016-09-23 09:14:12 +01:00
// Success is a successful lookup.
Success Result = iota
Golint2 (#280) * Fix linter errors * More linting fixes * More docs and making members private that dont need to be public * Fix linter errors * More linting fixes * More docs and making members private that dont need to be public * More lint fixes This leaves: ~~~ middleware/kubernetes/nametemplate/nametemplate.go:64:6: exported type NameTemplate should have comment or be unexported middleware/kubernetes/nametemplate/nametemplate.go:71:1: exported method NameTemplate.SetTemplate should have comment or be unexported middleware/kubernetes/nametemplate/nametemplate.go:108:1: exported method NameTemplate.GetZoneFromSegmentArray should have comment or be unexported middleware/kubernetes/nametemplate/nametemplate.go:116:1: exported method NameTemplate.GetNamespaceFromSegmentArray should have comment or be unexported middleware/kubernetes/nametemplate/nametemplate.go:120:1: exported method NameTemplate.GetServiceFromSegmentArray should have comment or be unexported middleware/kubernetes/nametemplate/nametemplate.go:124:1: exported method NameTemplate.GetTypeFromSegmentArray should have comment or be unexported middleware/kubernetes/nametemplate/nametemplate.go:135:1: exported method NameTemplate.GetSymbolFromSegmentArray should have comment or be unexported middleware/kubernetes/nametemplate/nametemplate.go:167:1: exported method NameTemplate.IsValid should have comment or be unexported middleware/kubernetes/nametemplate/nametemplate.go:182:6: exported type NameValues should have comment or be unexported middleware/kubernetes/util/util.go:1:1: package comment should be of the form "Package util ..." middleware/kubernetes/util/util.go:27:2: exported const WildcardStar should have comment (or a comment on this block) or be unexported middleware/proxy/lookup.go:66:1: exported method Proxy.Forward should have comment or be unexported middleware/proxy/proxy.go:24:6: exported type Client should have comment or be unexported middleware/proxy/proxy.go:107:1: exported function Clients should have comment or be unexported middleware/proxy/reverseproxy.go:10:6: exported type ReverseProxy should have comment or be unexported middleware/proxy/reverseproxy.go:16:1: exported method ReverseProxy.ServeDNS should have comment or be unexported middleware/proxy/upstream.go:42:6: exported type Options should have comment or be unexported ~~~ I plan on reworking the proxy anyway, so I'll leave that be.
2016-09-23 09:14:12 +01:00
// NameError indicates a nameerror
NameError
Golint2 (#280) * Fix linter errors * More linting fixes * More docs and making members private that dont need to be public * Fix linter errors * More linting fixes * More docs and making members private that dont need to be public * More lint fixes This leaves: ~~~ middleware/kubernetes/nametemplate/nametemplate.go:64:6: exported type NameTemplate should have comment or be unexported middleware/kubernetes/nametemplate/nametemplate.go:71:1: exported method NameTemplate.SetTemplate should have comment or be unexported middleware/kubernetes/nametemplate/nametemplate.go:108:1: exported method NameTemplate.GetZoneFromSegmentArray should have comment or be unexported middleware/kubernetes/nametemplate/nametemplate.go:116:1: exported method NameTemplate.GetNamespaceFromSegmentArray should have comment or be unexported middleware/kubernetes/nametemplate/nametemplate.go:120:1: exported method NameTemplate.GetServiceFromSegmentArray should have comment or be unexported middleware/kubernetes/nametemplate/nametemplate.go:124:1: exported method NameTemplate.GetTypeFromSegmentArray should have comment or be unexported middleware/kubernetes/nametemplate/nametemplate.go:135:1: exported method NameTemplate.GetSymbolFromSegmentArray should have comment or be unexported middleware/kubernetes/nametemplate/nametemplate.go:167:1: exported method NameTemplate.IsValid should have comment or be unexported middleware/kubernetes/nametemplate/nametemplate.go:182:6: exported type NameValues should have comment or be unexported middleware/kubernetes/util/util.go:1:1: package comment should be of the form "Package util ..." middleware/kubernetes/util/util.go:27:2: exported const WildcardStar should have comment (or a comment on this block) or be unexported middleware/proxy/lookup.go:66:1: exported method Proxy.Forward should have comment or be unexported middleware/proxy/proxy.go:24:6: exported type Client should have comment or be unexported middleware/proxy/proxy.go:107:1: exported function Clients should have comment or be unexported middleware/proxy/reverseproxy.go:10:6: exported type ReverseProxy should have comment or be unexported middleware/proxy/reverseproxy.go:16:1: exported method ReverseProxy.ServeDNS should have comment or be unexported middleware/proxy/upstream.go:42:6: exported type Options should have comment or be unexported ~~~ I plan on reworking the proxy anyway, so I'll leave that be.
2016-09-23 09:14:12 +01:00
// Delegation indicates the lookup resulted in a delegation.
Delegation
Golint2 (#280) * Fix linter errors * More linting fixes * More docs and making members private that dont need to be public * Fix linter errors * More linting fixes * More docs and making members private that dont need to be public * More lint fixes This leaves: ~~~ middleware/kubernetes/nametemplate/nametemplate.go:64:6: exported type NameTemplate should have comment or be unexported middleware/kubernetes/nametemplate/nametemplate.go:71:1: exported method NameTemplate.SetTemplate should have comment or be unexported middleware/kubernetes/nametemplate/nametemplate.go:108:1: exported method NameTemplate.GetZoneFromSegmentArray should have comment or be unexported middleware/kubernetes/nametemplate/nametemplate.go:116:1: exported method NameTemplate.GetNamespaceFromSegmentArray should have comment or be unexported middleware/kubernetes/nametemplate/nametemplate.go:120:1: exported method NameTemplate.GetServiceFromSegmentArray should have comment or be unexported middleware/kubernetes/nametemplate/nametemplate.go:124:1: exported method NameTemplate.GetTypeFromSegmentArray should have comment or be unexported middleware/kubernetes/nametemplate/nametemplate.go:135:1: exported method NameTemplate.GetSymbolFromSegmentArray should have comment or be unexported middleware/kubernetes/nametemplate/nametemplate.go:167:1: exported method NameTemplate.IsValid should have comment or be unexported middleware/kubernetes/nametemplate/nametemplate.go:182:6: exported type NameValues should have comment or be unexported middleware/kubernetes/util/util.go:1:1: package comment should be of the form "Package util ..." middleware/kubernetes/util/util.go:27:2: exported const WildcardStar should have comment (or a comment on this block) or be unexported middleware/proxy/lookup.go:66:1: exported method Proxy.Forward should have comment or be unexported middleware/proxy/proxy.go:24:6: exported type Client should have comment or be unexported middleware/proxy/proxy.go:107:1: exported function Clients should have comment or be unexported middleware/proxy/reverseproxy.go:10:6: exported type ReverseProxy should have comment or be unexported middleware/proxy/reverseproxy.go:16:1: exported method ReverseProxy.ServeDNS should have comment or be unexported middleware/proxy/upstream.go:42:6: exported type Options should have comment or be unexported ~~~ I plan on reworking the proxy anyway, so I'll leave that be.
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
Golint2 (#280) * Fix linter errors * More linting fixes * More docs and making members private that dont need to be public * Fix linter errors * More linting fixes * More docs and making members private that dont need to be public * More lint fixes This leaves: ~~~ middleware/kubernetes/nametemplate/nametemplate.go:64:6: exported type NameTemplate should have comment or be unexported middleware/kubernetes/nametemplate/nametemplate.go:71:1: exported method NameTemplate.SetTemplate should have comment or be unexported middleware/kubernetes/nametemplate/nametemplate.go:108:1: exported method NameTemplate.GetZoneFromSegmentArray should have comment or be unexported middleware/kubernetes/nametemplate/nametemplate.go:116:1: exported method NameTemplate.GetNamespaceFromSegmentArray should have comment or be unexported middleware/kubernetes/nametemplate/nametemplate.go:120:1: exported method NameTemplate.GetServiceFromSegmentArray should have comment or be unexported middleware/kubernetes/nametemplate/nametemplate.go:124:1: exported method NameTemplate.GetTypeFromSegmentArray should have comment or be unexported middleware/kubernetes/nametemplate/nametemplate.go:135:1: exported method NameTemplate.GetSymbolFromSegmentArray should have comment or be unexported middleware/kubernetes/nametemplate/nametemplate.go:167:1: exported method NameTemplate.IsValid should have comment or be unexported middleware/kubernetes/nametemplate/nametemplate.go:182:6: exported type NameValues should have comment or be unexported middleware/kubernetes/util/util.go:1:1: package comment should be of the form "Package util ..." middleware/kubernetes/util/util.go:27:2: exported const WildcardStar should have comment (or a comment on this block) or be unexported middleware/proxy/lookup.go:66:1: exported method Proxy.Forward should have comment or be unexported middleware/proxy/proxy.go:24:6: exported type Client should have comment or be unexported middleware/proxy/proxy.go:107:1: exported function Clients should have comment or be unexported middleware/proxy/reverseproxy.go:10:6: exported type ReverseProxy should have comment or be unexported middleware/proxy/reverseproxy.go:16:1: exported method ReverseProxy.ServeDNS should have comment or be unexported middleware/proxy/upstream.go:42:6: exported type Options should have comment or be unexported ~~~ I plan on reworking the proxy anyway, so I'll leave that be.
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
)
// 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(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()
// If z is a secondary zone we might not have transferred it, meaning we have
// 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.
z.RLock()
ap := z.Apex
tr := z.Tree
z.RUnlock()
if ap.SOA == nil {
return nil, nil, nil, ServerFailure
}
if qname == z.origin {
switch qtype {
case dns.TypeSOA:
return ap.soa(do), ap.ns(do), nil, Success
case dns.TypeNS:
nsrrs := ap.ns(do)
glue := tr.Glue(nsrrs, do) // technically this isn't glue
return nsrrs, nil, glue, Success
}
}
var (
found, shot bool
parts string
i int
elem, wildElem *tree.Elem
)
loop, _ := ctx.Value(dnsserver.LoopKey{}).(int)
if loop > 8 {
// We're back here for the 9th time; we have a loop and need to bail out.
// Note the answer we're returning will be incomplete (more cnames to be followed) or
// illegal (wildcard cname with multiple identical records). For now it's more important
// to protect ourselves then to give the client a valid answer. We return with an error
// to let the server handle what to do.
return nil, nil, nil, ServerFailure
}
// 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.
middleware/file: add DNAME support (#651) * Test DNAME handling If the DNAME itself matches the QTYPE, and the owner name matches QNAME, the relevant DNAME RR should be included in the answer section. Other parts of RFC 6672 are not implemented yet and hence left untested. * Implement the DNAME substitution As specified in RFC 6672, a DNAME substitution is performed by replacing the suffix labels of the name being sought matching the owner name of the DNAME resource record with the string of labels in the RDATA field. The matching labels end with the root label in all cases. Only whole labels are replaced. * Handle DNAME redirection A CNAME RR is created on-the-fly for the DNAME redirection. Be aware that we do not have all the edge cases covered yet. * Test DNAME owner name matching the QNAME A DNAME RR redirects DNS names subordinate to its owner name; the owner name of a DNAME is NOT redirected itself. * Ignore names next to and below a DNAME record According to RFC 6672, resource records MUST NOT exist at any subdomain of the owner of a DNAME RR. When loading a zone, those names below the DNAME RR will be quietly ignored. * Streamline DNAME processing Instead of checking DNAMEs during lookup, we use a preloaded list of DNAME RRs to streamline the process without any runtime performance penalty: * When loading the zone, keep a record of any DNAME RRs. * If there aren't any DNAMEs in the zone, just do the lookup as usual. * Only when the zone has one or more DNAME records, we look for the matching DNAME and ignore confronting subdomain(s) in the process. * Make it easier to trace back through test errors * Make DNAME handling part of lookup routine DNAME processing is invoked only if the zone has at least one DNAME RR. * Put DNAME resolution inside the searching of a hit We can drop some of the other ideas; we don't need to track if we have DNAMEs in the zone it just follows naturally from the current lookup code. See also: #664
2017-05-26 17:37:06 +08:00
// If found we check if it is a CNAME/DNAME and do CNAME processing
// We also check if we have type and do a nodata response.
//
// 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
}
elem, found = tr.Search(parts)
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
// wildcard we should expand the wildcard.
wildcard := replaceWithAsteriskLabel(parts)
if wild, found := tr.Search(wildcard); found {
wildElem = wild
}
// 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
}
middleware/file: add DNAME support (#651) * Test DNAME handling If the DNAME itself matches the QTYPE, and the owner name matches QNAME, the relevant DNAME RR should be included in the answer section. Other parts of RFC 6672 are not implemented yet and hence left untested. * Implement the DNAME substitution As specified in RFC 6672, a DNAME substitution is performed by replacing the suffix labels of the name being sought matching the owner name of the DNAME resource record with the string of labels in the RDATA field. The matching labels end with the root label in all cases. Only whole labels are replaced. * Handle DNAME redirection A CNAME RR is created on-the-fly for the DNAME redirection. Be aware that we do not have all the edge cases covered yet. * Test DNAME owner name matching the QNAME A DNAME RR redirects DNS names subordinate to its owner name; the owner name of a DNAME is NOT redirected itself. * Ignore names next to and below a DNAME record According to RFC 6672, resource records MUST NOT exist at any subdomain of the owner of a DNAME RR. When loading a zone, those names below the DNAME RR will be quietly ignored. * Streamline DNAME processing Instead of checking DNAMEs during lookup, we use a preloaded list of DNAME RRs to streamline the process without any runtime performance penalty: * When loading the zone, keep a record of any DNAME RRs. * If there aren't any DNAMEs in the zone, just do the lookup as usual. * Only when the zone has one or more DNAME records, we look for the matching DNAME and ignore confronting subdomain(s) in the process. * Make it easier to trace back through test errors * Make DNAME handling part of lookup routine DNAME processing is invoked only if the zone has at least one DNAME RR. * Put DNAME resolution inside the searching of a hit We can drop some of the other ideas; we don't need to track if we have DNAMEs in the zone it just follows naturally from the current lookup code. See also: #664
2017-05-26 17:37:06 +08:00
// If we see DNAME records, we should return those.
if dnamerrs := elem.Type(dns.TypeDNAME); dnamerrs != nil {
// Only one DNAME is allowed per name. We just pick the first one to synthesize from.
middleware/file: add DNAME support (#651) * Test DNAME handling If the DNAME itself matches the QTYPE, and the owner name matches QNAME, the relevant DNAME RR should be included in the answer section. Other parts of RFC 6672 are not implemented yet and hence left untested. * Implement the DNAME substitution As specified in RFC 6672, a DNAME substitution is performed by replacing the suffix labels of the name being sought matching the owner name of the DNAME resource record with the string of labels in the RDATA field. The matching labels end with the root label in all cases. Only whole labels are replaced. * Handle DNAME redirection A CNAME RR is created on-the-fly for the DNAME redirection. Be aware that we do not have all the edge cases covered yet. * Test DNAME owner name matching the QNAME A DNAME RR redirects DNS names subordinate to its owner name; the owner name of a DNAME is NOT redirected itself. * Ignore names next to and below a DNAME record According to RFC 6672, resource records MUST NOT exist at any subdomain of the owner of a DNAME RR. When loading a zone, those names below the DNAME RR will be quietly ignored. * Streamline DNAME processing Instead of checking DNAMEs during lookup, we use a preloaded list of DNAME RRs to streamline the process without any runtime performance penalty: * When loading the zone, keep a record of any DNAME RRs. * If there aren't any DNAMEs in the zone, just do the lookup as usual. * Only when the zone has one or more DNAME records, we look for the matching DNAME and ignore confronting subdomain(s) in the process. * Make it easier to trace back through test errors * Make DNAME handling part of lookup routine DNAME processing is invoked only if the zone has at least one DNAME RR. * Put DNAME resolution inside the searching of a hit We can drop some of the other ideas; we don't need to track if we have DNAMEs in the zone it just follows naturally from the current lookup code. See also: #664
2017-05-26 17:37:06 +08:00
dname := dnamerrs[0]
if cname := synthesizeCNAME(state.Name(), dname.(*dns.DNAME)); cname != nil {
var (
answer, ns, extra []dns.RR
rcode Result
)
// We don't need to chase CNAME chain for synthesized CNAME
if qtype == dns.TypeCNAME {
answer = []dns.RR{cname}
ns = ap.ns(do)
extra = nil
rcode = Success
} else {
ctx = context.WithValue(ctx, dnsserver.LoopKey{}, loop+1)
answer, ns, extra, rcode = z.externalLookup(ctx, state, elem, []dns.RR{cname})
}
middleware/file: add DNAME support (#651) * Test DNAME handling If the DNAME itself matches the QTYPE, and the owner name matches QNAME, the relevant DNAME RR should be included in the answer section. Other parts of RFC 6672 are not implemented yet and hence left untested. * Implement the DNAME substitution As specified in RFC 6672, a DNAME substitution is performed by replacing the suffix labels of the name being sought matching the owner name of the DNAME resource record with the string of labels in the RDATA field. The matching labels end with the root label in all cases. Only whole labels are replaced. * Handle DNAME redirection A CNAME RR is created on-the-fly for the DNAME redirection. Be aware that we do not have all the edge cases covered yet. * Test DNAME owner name matching the QNAME A DNAME RR redirects DNS names subordinate to its owner name; the owner name of a DNAME is NOT redirected itself. * Ignore names next to and below a DNAME record According to RFC 6672, resource records MUST NOT exist at any subdomain of the owner of a DNAME RR. When loading a zone, those names below the DNAME RR will be quietly ignored. * Streamline DNAME processing Instead of checking DNAMEs during lookup, we use a preloaded list of DNAME RRs to streamline the process without any runtime performance penalty: * When loading the zone, keep a record of any DNAME RRs. * If there aren't any DNAMEs in the zone, just do the lookup as usual. * Only when the zone has one or more DNAME records, we look for the matching DNAME and ignore confronting subdomain(s) in the process. * Make it easier to trace back through test errors * Make DNAME handling part of lookup routine DNAME processing is invoked only if the zone has at least one DNAME RR. * Put DNAME resolution inside the searching of a hit We can drop some of the other ideas; we don't need to track if we have DNAMEs in the zone it just follows naturally from the current lookup code. See also: #664
2017-05-26 17:37:06 +08:00
if do {
sigs := elem.Type(dns.TypeRRSIG)
sigs = rrutil.SubTypeSignature(sigs, dns.TypeDNAME)
dnamerrs = append(dnamerrs, sigs...)
}
middleware/file: add DNAME support (#651) * Test DNAME handling If the DNAME itself matches the QTYPE, and the owner name matches QNAME, the relevant DNAME RR should be included in the answer section. Other parts of RFC 6672 are not implemented yet and hence left untested. * Implement the DNAME substitution As specified in RFC 6672, a DNAME substitution is performed by replacing the suffix labels of the name being sought matching the owner name of the DNAME resource record with the string of labels in the RDATA field. The matching labels end with the root label in all cases. Only whole labels are replaced. * Handle DNAME redirection A CNAME RR is created on-the-fly for the DNAME redirection. Be aware that we do not have all the edge cases covered yet. * Test DNAME owner name matching the QNAME A DNAME RR redirects DNS names subordinate to its owner name; the owner name of a DNAME is NOT redirected itself. * Ignore names next to and below a DNAME record According to RFC 6672, resource records MUST NOT exist at any subdomain of the owner of a DNAME RR. When loading a zone, those names below the DNAME RR will be quietly ignored. * Streamline DNAME processing Instead of checking DNAMEs during lookup, we use a preloaded list of DNAME RRs to streamline the process without any runtime performance penalty: * When loading the zone, keep a record of any DNAME RRs. * If there aren't any DNAMEs in the zone, just do the lookup as usual. * Only when the zone has one or more DNAME records, we look for the matching DNAME and ignore confronting subdomain(s) in the process. * Make it easier to trace back through test errors * Make DNAME handling part of lookup routine DNAME processing is invoked only if the zone has at least one DNAME RR. * Put DNAME resolution inside the searching of a hit We can drop some of the other ideas; we don't need to track if we have DNAMEs in the zone it just follows naturally from the current lookup code. See also: #664
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.
answer = append(dnamerrs, answer...)
middleware/file: add DNAME support (#651) * Test DNAME handling If the DNAME itself matches the QTYPE, and the owner name matches QNAME, the relevant DNAME RR should be included in the answer section. Other parts of RFC 6672 are not implemented yet and hence left untested. * Implement the DNAME substitution As specified in RFC 6672, a DNAME substitution is performed by replacing the suffix labels of the name being sought matching the owner name of the DNAME resource record with the string of labels in the RDATA field. The matching labels end with the root label in all cases. Only whole labels are replaced. * Handle DNAME redirection A CNAME RR is created on-the-fly for the DNAME redirection. Be aware that we do not have all the edge cases covered yet. * Test DNAME owner name matching the QNAME A DNAME RR redirects DNS names subordinate to its owner name; the owner name of a DNAME is NOT redirected itself. * Ignore names next to and below a DNAME record According to RFC 6672, resource records MUST NOT exist at any subdomain of the owner of a DNAME RR. When loading a zone, those names below the DNAME RR will be quietly ignored. * Streamline DNAME processing Instead of checking DNAMEs during lookup, we use a preloaded list of DNAME RRs to streamline the process without any runtime performance penalty: * When loading the zone, keep a record of any DNAME RRs. * If there aren't any DNAMEs in the zone, just do the lookup as usual. * Only when the zone has one or more DNAME records, we look for the matching DNAME and ignore confronting subdomain(s) in the process. * Make it easier to trace back through test errors * Make DNAME handling part of lookup routine DNAME processing is invoked only if the zone has at least one DNAME RR. * Put DNAME resolution inside the searching of a hit We can drop some of the other ideas; we don't need to track if we have DNAMEs in the zone it just follows naturally from the current lookup code. See also: #664
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.
}
// If we see NS records, it means the name as been delegated, and we should return the delegation.
if nsrrs := elem.Type(dns.TypeNS); nsrrs != nil {
// 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
}
glue := tr.Glue(nsrrs, do)
if do {
dss := typeFromElem(elem, dns.TypeDS, do)
nsrrs = append(nsrrs, dss...)
}
return nil, nsrrs, glue, Delegation
}
i++
}
// What does found and !shot mean - do we ever hit it?
if found && !shot {
return nil, nil, nil, ServerFailure
}
// Found entire name.
if found && shot {
if rrs := elem.Type(dns.TypeCNAME); len(rrs) > 0 && qtype != dns.TypeCNAME {
ctx = context.WithValue(ctx, dnsserver.LoopKey{}, loop+1)
return z.externalLookup(ctx, state, elem, rrs)
}
rrs := elem.Type(qtype)
// NODATA
if len(rrs) == 0 {
ret := ap.soa(do)
if do {
nsec := typeFromElem(elem, dns.TypeNSEC, do)
ret = append(ret, nsec...)
}
return nil, ret, nil, NoData
}
2016-03-28 21:18:16 +01:00
// Additional section processing for MX, SRV. Check response and see if any of the names are in bailiwick -
// if so add IP addresses to the additional section.
additional := z.additionalProcessing(rrs, do)
if do {
sigs := elem.Type(dns.TypeRRSIG)
sigs = rrutil.SubTypeSignature(sigs, qtype)
rrs = append(rrs, sigs...)
}
return rrs, ap.ns(do), additional, Success
}
// Haven't found the original name.
// Found wildcard.
if wildElem != nil {
// set metadata value for the wildcard record that synthesized the result
metadata.SetValueFunc(ctx, "zone/wildcard", func() string {
return wildElem.Name()
})
if rrs := wildElem.TypeForWildcard(dns.TypeCNAME, qname); len(rrs) > 0 && qtype != dns.TypeCNAME {
ctx = context.WithValue(ctx, dnsserver.LoopKey{}, loop+1)
return z.externalLookup(ctx, state, wildElem, rrs)
}
rrs := wildElem.TypeForWildcard(qtype, qname)
// NODATA response.
if len(rrs) == 0 {
ret := ap.soa(do)
if do {
nsec := typeFromElem(wildElem, dns.TypeNSEC, do)
ret = append(ret, nsec...)
}
return nil, ret, nil, NoData
}
auth := ap.ns(do)
if do {
// An NSEC is needed to say no longer name exists under this wildcard.
if deny, found := tr.Prev(qname); found {
nsec := typeFromElem(deny, dns.TypeNSEC, do)
auth = append(auth, nsec...)
}
sigs := wildElem.TypeForWildcard(dns.TypeRRSIG, qname)
sigs = rrutil.SubTypeSignature(sigs, qtype)
rrs = append(rrs, sigs...)
}
return rrs, auth, nil, Success
}
rcode := NameError
// Hacky way to get around empty-non-terminals. If a longer name does exist, but this qname, does not, it
// must be an empty-non-terminal. If so, we do the proper NXDOMAIN handling, but set the rcode to be success.
if x, found := tr.Next(qname); found {
if dns.IsSubDomain(qname, x.Name()) {
rcode = Success
}
}
ret := ap.soa(do)
2016-03-28 21:18:16 +01:00
if do {
deny, found := tr.Prev(qname)
if !found {
goto Out
}
nsec := typeFromElem(deny, dns.TypeNSEC, do)
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()
if ss, found := tr.Prev(wildcard); found {
// Only add this nsec if it is different than the one already added
if ss.Name() != deny.Name() {
nsec := typeFromElem(ss, dns.TypeNSEC, do)
ret = append(ret, nsec...)
}
}
}
2016-03-28 21:18:16 +01:00
}
Out:
return nil, ret, nil, rcode
2016-03-29 08:17:45 +01: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 {
rrs := elem.Type(tp)
2016-03-29 08:17:45 +01:00
if do {
sigs := elem.Type(dns.TypeRRSIG)
sigs = rrutil.SubTypeSignature(sigs, tp)
rrs = append(rrs, sigs...)
}
return rrs
}
func (a Apex) soa(do bool) []dns.RR {
if do {
ret := append([]dns.RR{a.SOA}, a.SIGSOA...)
return ret
2016-03-29 08:17:45 +01:00
}
return []dns.RR{a.SOA}
2016-03-29 08:17:45 +01:00
}
func (a Apex) ns(do bool) []dns.RR {
2016-03-29 08:17:45 +01:00
if do {
ret := append(a.NS, a.SIGNS...)
return ret
2016-03-29 08:17:45 +01:00
}
return a.NS
}
// 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()
if do {
sigs := elem.Type(dns.TypeRRSIG)
sigs = rrutil.SubTypeSignature(sigs, dns.TypeCNAME)
rrs = append(rrs, sigs...)
}
targetName := rrs[0].(*dns.CNAME).Target
elem, _ = z.Tree.Search(targetName)
2016-03-28 21:18:16 +01:00
if elem == nil {
lookupRRs, result := z.doLookup(ctx, state, targetName, qtype)
rrs = append(rrs, lookupRRs...)
return rrs, z.Apex.ns(do), nil, result
2016-03-28 21:18:16 +01:00
}
i := 0
Redo:
cname := elem.Type(dns.TypeCNAME)
if len(cname) > 0 {
rrs = append(rrs, cname...)
if do {
sigs := elem.Type(dns.TypeRRSIG)
sigs = rrutil.SubTypeSignature(sigs, dns.TypeCNAME)
rrs = append(rrs, sigs...)
}
targetName := cname[0].(*dns.CNAME).Target
elem, _ = z.Tree.Search(targetName)
if elem == nil {
lookupRRs, result := z.doLookup(ctx, state, targetName, qtype)
rrs = append(rrs, lookupRRs...)
return rrs, z.Apex.ns(do), nil, result
}
i++
if i > 8 {
return rrs, z.Apex.ns(do), nil, Success
}
goto Redo
}
targets := elem.Type(qtype)
if len(targets) > 0 {
rrs = append(rrs, targets...)
if do {
sigs := elem.Type(dns.TypeRRSIG)
sigs = rrutil.SubTypeSignature(sigs, qtype)
rrs = append(rrs, sigs...)
2016-03-28 21:18:16 +01:00
}
}
return rrs, z.Apex.ns(do), nil, Success
2016-03-28 21:18:16 +01:00
}
func (z *Zone) doLookup(ctx context.Context, state request.Request, target string, qtype uint16) ([]dns.RR, Result) {
m, e := z.Upstream.Lookup(ctx, state, target, qtype)
2016-11-10 12:58:40 +00:00
if e != nil {
return nil, ServerFailure
}
if m == nil {
return nil, Success
}
if m.Rcode == dns.RcodeNameError {
return m.Answer, NameError
}
if m.Rcode == dns.RcodeServerFailure {
return m.Answer, ServerFailure
}
if m.Rcode == dns.RcodeSuccess && len(m.Answer) == 0 {
return m.Answer, NoData
2016-11-10 07:48:47 +00:00
}
return m.Answer, Success
2016-11-10 07:48:47 +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.
func (z *Zone) additionalProcessing(answer []dns.RR, do bool) (extra []dns.RR) {
for _, rr := range answer {
name := ""
switch x := rr.(type) {
case *dns.SRV:
name = x.Target
case *dns.MX:
name = x.Mx
}
if len(name) == 0 || !dns.IsSubDomain(z.origin, name) {
continue
}
elem, _ := z.Tree.Search(name)
if elem == nil {
continue
}
sigs := elem.Type(dns.TypeRRSIG)
for _, addr := range []uint16{dns.TypeA, dns.TypeAAAA} {
if a := elem.Type(addr); a != nil {
extra = append(extra, a...)
if do {
sig := rrutil.SubTypeSignature(sigs, addr)
extra = append(extra, sig...)
}
}
}
}
return extra
}