- maybeDelegate = False
- deferredResult = authority.BindAuthority._lookup(self, name, cls,
- type, timeout)
- # If we didn't find an exact match for the name we were seeking,
- # check if it's within a subdomain we're supposed to delegate to
- # some other DNS server.
- while (isinstance(deferredResult.result, failure.Failure)
- and '.' in name):
- maybeDelegate = True
- name = name[name.find('.') + 1 :]
- deferredResult = authority.BindAuthority._lookup(self, name, cls,
- dns.NS, timeout)
- # If we found somewhere to delegate the query to, our _lookup()
- # for the NS record resulted in it being in the 'results' section.
- # We need to instead return that information in the 'authority'
- # section to delegate, and return an empty 'results' section
- # (because we didn't find the name we were asked about). We
- # leave the 'additional' section as we received it because it
- # may contain A records for the DNS server we're delegating to.
- if maybeDelegate and not isinstance(deferredResult.result,
- failure.Failure):
- (nsResults, nsAuthority, nsAdditional) = deferredResult.result
- deferredResult = defer.succeed(([], nsResults, nsAdditional))
- return deferredResult
+ try:
+ result = yield authority.BindAuthority._lookup(self, name, cls,
+ type, timeout)
+ defer.returnValue(result)
+ except Exception as e:
+ # XXX: Twisted returns DomainError even if it is
+ # authoritative for the domain because our SOA record
+ # incorrectly contains (origin + "." + origin)
+ if not isinstance(e, (dns.DomainError, dns.AuthoritativeDomainError)):
+ sys.stderr.write("while looking up '%s', got: %s\n" % (name, e))
+
+ # If we didn't find an exact match for the name we were
+ # seeking, check if it's within a subdomain we're supposed
+ # to delegate to some other DNS server.
+ while '.' in name:
+ _, name = name.split('.', 1)
+ try:
+ # BindAuthority puts the NS in the authority
+ # section automatically for us, so just return
+ # it. We override the type to NS.
+ result = yield authority.BindAuthority._lookup(self, name, cls,
+ dns.NS, timeout)
+ defer.returnValue(result)
+ except Exception: # Should be one of (dns.DomainError, dns.AuthoritativeDomainError)
+ pass
+ # We didn't find a delegation, so return the original
+ # NXDOMAIN.
+ raise
+
+class TypeLenientResolverChain(resolve.ResolverChain):
+ """
+ This is a ResolverChain which is more lenient in its handling of
+ queries requesting unimplemented record types.
+ """
+
+ def query(self, query, timeout = None):
+ try:
+ return self.typeToMethod[query.type](str(query.name), timeout)
+ except KeyError, e:
+ # We don't support the requested record type. Twisted would
+ # have us return SERVFAIL. Instead, we'll check whether the
+ # name exists in our zone at all and return NXDOMAIN or an empty
+ # result set with NOERROR as appropriate.
+ deferredResult = self.lookupAllRecords(str(query.name), timeout)
+ if isinstance(deferredResult.result, failure.Failure):
+ return deferredResult
+ return defer.succeed(([], [], []))