Skip to content

Commit

Permalink
Better handling of SERVFAIL responses
Browse files Browse the repository at this point in the history
  • Loading branch information
jsf9k committed Jan 5, 2018
1 parent afb0577 commit baaf564
Showing 1 changed file with 28 additions and 7 deletions.
35 changes: 28 additions & 7 deletions trustymail/trustymail.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,13 @@ def mx_scan(resolver, domain):
# records more than whether their records fit in a single UDP packet.
for record in resolver.query(domain.domain_name, 'MX', tcp=True):
domain.add_mx_record(record)
except (dns.resolver.NoNameservers, dns.resolver.NoAnswer, dns.exception.Timeout, dns.resolver.NXDOMAIN) as error:
except dns.resolver.NoNameservers as error:
# This exception means that we got a SERVFAIL response. These
# responses are almost always permanent, not temporary, so let's treat
# the domain as not live.
domain.is_live = False
handle_error('[MX]', domain, error)
except (dns.resolver.NoAnswer, dns.exception.Timeout, dns.resolver.NXDOMAIN) as error:
handle_error('[MX]', domain, error)


Expand Down Expand Up @@ -247,7 +253,13 @@ def get_spf_record_text(resolver, domain_name, domain, follow_redirect=False):
record_to_return = get_spf_record_text(resolver, redirect_domain_name, domain)
else:
record_to_return = record_text
except (dns.resolver.NoNameservers, dns.resolver.NoAnswer, dns.exception.Timeout, dns.resolver.NXDOMAIN) as error:
except dns.resolver.NoNameservers as error:
# This exception means that we got a SERVFAIL response. These
# responses are almost always permanent, not temporary, so let's treat
# the domain as not live.
domain.is_live = False
handle_error('[SPF]', domain, error)
except (dns.resolver.NoAnswer, dns.exception.Timeout, dns.resolver.NXDOMAIN) as error:
handle_error('[SPF]', domain, error)

return record_to_return
Expand Down Expand Up @@ -472,8 +484,13 @@ def dmarc_scan(resolver, domain):

domain.dmarc_has_aggregate_uri = len(domain.dmarc_aggregate_uris) > 0
domain.dmarc_has_forensic_uri = len(domain.dmarc_forensic_uris) > 0

except (dns.resolver.NoNameservers, dns.resolver.NoAnswer, dns.exception.Timeout, dns.resolver.NXDOMAIN) as error:
except dns.resolver.NoNameservers as error:
# This exception means that we got a SERVFAIL response. These
# responses are almost always permanent, not temporary, so let's treat
# the domain as not live.
domain.is_live = False
handle_error('[DMARC]', domain, error)
except (dns.resolver.NoAnswer, dns.exception.Timeout, dns.resolver.NXDOMAIN) as error:
handle_error('[DMARC]', domain, error)


Expand All @@ -498,9 +515,13 @@ def scan(domain_name, timeout, smtp_timeout, smtp_localhost, smtp_ports, smtp_ca
# Note that it uses the system configuration in /etc/resolv.conf
# if no DNS hostnames are specified.
resolver = dns.resolver.Resolver(configure=not dns_hostnames)
# Retry queries if we receive a SERVFAIL response. This may only indicate
# a temporary network problem.
resolver.retry_servfail = True
# Retry DNS servers if we receive a SERVFAIL response from them. We set
# this to False because, unless the reason for the SERVFAIL is truly
# temporary and resolves before trustymail finishes scanning the domain,
# this obscures the potentially informative SERVFAIL error as a DNS timeout
# because of the way dns.resolver.query() is written. See
# http://www.dnspython.org/docs/1.14.0/dns.resolver-pysrc.html#query.
resolver.retry_servfail = False
# If the user passed in DNS hostnames to query against then use them
if dns_hostnames:
resolver.nameservers = dns_hostnames
Expand Down

0 comments on commit baaf564

Please sign in to comment.