From 7921f5dd0bc789268459f63f8f6ab3baf8a9d571 Mon Sep 17 00:00:00 2001 From: missytake Date: Tue, 19 Dec 2023 17:05:08 +0100 Subject: [PATCH] DNS: fix some crashes in cmdeploy dns --- cmdeploy/src/cmdeploy/dns.py | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/cmdeploy/src/cmdeploy/dns.py b/cmdeploy/src/cmdeploy/dns.py index dee23e14..c0d5e423 100644 --- a/cmdeploy/src/cmdeploy/dns.py +++ b/cmdeploy/src/cmdeploy/dns.py @@ -47,10 +47,11 @@ class DNS: def check_ptr_record(self, ip: str, mail_domain) -> str: """Check the PTR record for an IPv4 or IPv6 address.""" result = self.get("-x", ip) - if ip_address(ip).version == 6: - result = result.split()[-1] - if result[:-1] == mail_domain: - return result + if result: + if ip_address(ip).version == 6: + result = result.split()[-1] + if result[:-1] == mail_domain: + return result def show_dns(args, out): @@ -101,7 +102,7 @@ def show_dns(args, out): zf.write(zonefile) print(f"DNS records successfully written to: {args.zonefile}") return - except AttributeError: + except TypeError: pass started_dkim_parsing = False for line in zonefile.splitlines(): @@ -135,8 +136,9 @@ def show_dns(args, out): domain, value = line.split(" TXT ") current = dns.get("TXT", domain.strip()[:-1]) if domain.startswith("_mta-sts."): - if current.split("id=")[0] == value.split("id=")[0]: - continue + if current: + if current.split("id=")[0] == value.split("id=")[0]: + continue if current != value: to_print.append(line) if " IN TXT ( " in line: @@ -145,9 +147,12 @@ def show_dns(args, out): if started_dkim_parsing and line.startswith('"'): dkim_lines.append(" " + line) domain, data = "\n".join(dkim_lines).split(" IN TXT ") - current = dns.get("TXT", domain.strip()[:-1]).replace('" "', '"\n "') - current = f"( {current} )" - if current.replace(";", "\\;") != data: + current = dns.get("TXT", domain.strip()[:-1]) + if current: + current = "( %s )" % (current.replace('" "', '"\n "')) + if current.replace(";", "\\;") != data: + to_print.append(dkim_entry) + else: to_print.append(dkim_entry) if to_print: @@ -190,8 +195,6 @@ def check_necessary_dns(out, mail_domain): if not (ipv4 or ipv6): to_print.append(f"\t{mail_domain}.\t\t\tA") if not mta_ip or not (mta_ip == ipv4 or mta_ip == ipv6): - #print(mta_entry, mta_ip) - #print(ipv4, ipv6) to_print.append(f"\tmta-sts.{mail_domain}.\tCNAME\t{mail_domain}.") if to_print: to_print.insert(