DNS: fix some crashes in cmdeploy dns

This commit is contained in:
missytake
2023-12-19 17:05:08 +01:00
parent 39fc9d628f
commit 7921f5dd0b

View File

@@ -47,10 +47,11 @@ class DNS:
def check_ptr_record(self, ip: str, mail_domain) -> str: def check_ptr_record(self, ip: str, mail_domain) -> str:
"""Check the PTR record for an IPv4 or IPv6 address.""" """Check the PTR record for an IPv4 or IPv6 address."""
result = self.get("-x", ip) result = self.get("-x", ip)
if ip_address(ip).version == 6: if result:
result = result.split()[-1] if ip_address(ip).version == 6:
if result[:-1] == mail_domain: result = result.split()[-1]
return result if result[:-1] == mail_domain:
return result
def show_dns(args, out): def show_dns(args, out):
@@ -101,7 +102,7 @@ def show_dns(args, out):
zf.write(zonefile) zf.write(zonefile)
print(f"DNS records successfully written to: {args.zonefile}") print(f"DNS records successfully written to: {args.zonefile}")
return return
except AttributeError: except TypeError:
pass pass
started_dkim_parsing = False started_dkim_parsing = False
for line in zonefile.splitlines(): for line in zonefile.splitlines():
@@ -135,8 +136,9 @@ def show_dns(args, out):
domain, value = line.split(" TXT ") domain, value = line.split(" TXT ")
current = dns.get("TXT", domain.strip()[:-1]) current = dns.get("TXT", domain.strip()[:-1])
if domain.startswith("_mta-sts."): if domain.startswith("_mta-sts."):
if current.split("id=")[0] == value.split("id=")[0]: if current:
continue if current.split("id=")[0] == value.split("id=")[0]:
continue
if current != value: if current != value:
to_print.append(line) to_print.append(line)
if " IN TXT ( " in line: if " IN TXT ( " in line:
@@ -145,9 +147,12 @@ def show_dns(args, out):
if started_dkim_parsing and line.startswith('"'): if started_dkim_parsing and line.startswith('"'):
dkim_lines.append(" " + line) dkim_lines.append(" " + line)
domain, data = "\n".join(dkim_lines).split(" IN TXT ") domain, data = "\n".join(dkim_lines).split(" IN TXT ")
current = dns.get("TXT", domain.strip()[:-1]).replace('" "', '"\n "') current = dns.get("TXT", domain.strip()[:-1])
current = f"( {current} )" if current:
if current.replace(";", "\\;") != data: current = "( %s )" % (current.replace('" "', '"\n "'))
if current.replace(";", "\\;") != data:
to_print.append(dkim_entry)
else:
to_print.append(dkim_entry) to_print.append(dkim_entry)
if to_print: if to_print:
@@ -190,8 +195,6 @@ def check_necessary_dns(out, mail_domain):
if not (ipv4 or ipv6): if not (ipv4 or ipv6):
to_print.append(f"\t{mail_domain}.\t\t\tA<your server's IPv4 address>") to_print.append(f"\t{mail_domain}.\t\t\tA<your server's IPv4 address>")
if not mta_ip or not (mta_ip == ipv4 or mta_ip == ipv6): 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}.") to_print.append(f"\tmta-sts.{mail_domain}.\tCNAME\t{mail_domain}.")
if to_print: if to_print:
to_print.insert( to_print.insert(