Skip to content

Commit bd88ccc

Browse files
committed
Gracefully handle an MX record with no corresponding A records
Note that we are giving credit for STARTTLS for such a case, since we cannot evaluate it.
1 parent 5e06e31 commit bd88ccc

File tree

1 file changed

+22
-3
lines changed

1 file changed

+22
-3
lines changed

trustymail/trustymail.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,9 +173,28 @@ def starttls_scan(domain, smtp_timeout, smtp_localhost, smtp_ports, smtp_cache):
173173
# To get around this I look up the A record and use
174174
# that instead of the hostname in DNS when I call
175175
# smtp_connection.connect().
176-
addr_info = socket.getaddrinfo(
177-
mail_server, port, socket.AF_INET, socket.SOCK_STREAM
178-
)
176+
try:
177+
addr_info = socket.getaddrinfo(
178+
mail_server, port, socket.AF_INET, socket.SOCK_STREAM
179+
)
180+
except socket.gaierror as error:
181+
# We get this exception if there is no A record
182+
# for the given mail server. This does happen,
183+
# since among their MX records some domains do
184+
# list some IPv6-only mail servers.
185+
#
186+
# Since we can't evaluate such cases we will
187+
# simply log this and give them credit. One of
188+
# the other mail servers will support IPv4.
189+
error_str = f'The mail server {mail_server} does not have an IPv4 address.'
190+
handle_error('[STARTTLS]', domain, error_str)
191+
logging.warn(error_str)
192+
domain.starttls_results[server_and_port]['is_listening'] = True
193+
domain.starttls_results[server_and_port]['supports_smtp'] = True
194+
domain.starttls_results[server_and_port]['starttls'] = True
195+
continue
196+
197+
# Extract the IP address from the socket addrinfo
179198
socket_address = addr_info[0][4]
180199
mail_server_ip_address = socket_address[0]
181200

0 commit comments

Comments
 (0)