From d9670b8feb102b0ea17fc87aa2dba8055cc12e99 Mon Sep 17 00:00:00 2001 From: Paolo Schiro Date: Wed, 18 Sep 2024 23:36:23 +0200 Subject: [PATCH] Added nameservers pre-flight check, issue #543 --- docs/source/usage.md | 2 ++ parsedmarc/cli.py | 23 ++++++++++++++++++++--- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/docs/source/usage.md b/docs/source/usage.md index c29c258c..3bd0487c 100644 --- a/docs/source/usage.md +++ b/docs/source/usage.md @@ -136,6 +136,8 @@ The full set of configuration options are: - `reverse_dns_map_url` - Overrides the default download URL for the reverse DNS map - `nameservers` - str: A comma separated list of DNS resolvers (Default: `[Cloudflare's public resolvers]`) + - `dns_test_address` - str: a dummy address used for DNS pre-flight checks + (Default: 1.1.1.1) - `dns_timeout` - float: DNS timeout period - `debug` - bool: Print debugging messages - `silent` - bool: Only print errors (Default: `True`) diff --git a/parsedmarc/cli.py b/parsedmarc/cli.py index d6ff5f0a..3149ea07 100644 --- a/parsedmarc/cli.py +++ b/parsedmarc/cli.py @@ -26,7 +26,7 @@ from parsedmarc.mail.graph import AuthMethod from parsedmarc.log import logger -from parsedmarc.utils import is_mbox +from parsedmarc.utils import is_mbox, get_reverse_dns formatter = logging.Formatter( fmt='%(levelname)8s:%(filename)s:%(lineno)d:%(message)s', @@ -447,6 +447,7 @@ def process_reports(reports_): smtp_tls_json_filename=args.smtp_tls_json_filename, smtp_tls_csv_filename=args.smtp_tls_csv_filename, nameservers=args.nameservers, + dns_test_address='1.1.1.1', silent=args.silent, warnings=args.warnings, dns_timeout=args.dns_timeout, @@ -599,10 +600,26 @@ def process_reports(reports_): if "smtp_tls_csv_filename" in general_config: opts.smtp_tls_csv_filename = general_config[ "smtp_tls_csv_filename"] - if "nameservers" in general_config: - opts.nameservers = _str_to_list(general_config["nameservers"]) if "dns_timeout" in general_config: opts.dns_timeout = general_config.getfloat("dns_timeout") + if "dns_test_address" in general_config: + opts.dns_test_address=general_config["dns_test_address"] + if "nameservers" in general_config: + opts.nameservers = _str_to_list(general_config["nameservers"]) + # nameservers pre-flight check + dummy_hostname=None + ns_error=None + try: + dummy_hostname=get_reverse_dns(opts.dns_test_address, + nameservers=opts.nameservers, + timeout=opts.dns_timeout) + except Exception as ns_error: + logger.critical("DNS pre-flight check failed: {}".format(ns_error)) + exit(-1) + if not dummy_hostname or ns_error: + logger.critical("DNS pre-flight check failed: no PTR record for " + "{} from {}".format(opts.dns_test_address,opts.nameservers)) + exit(-1) if "save_aggregate" in general_config: opts.save_aggregate = general_config["save_aggregate"] if "save_forensic" in general_config: