From ed11997bde84a952c40cda6c92af762712ee1627 Mon Sep 17 00:00:00 2001 From: Martin Mares Date: Mon, 22 Apr 2024 11:00:04 +0200 Subject: [PATCH] Implement add_null_mx --- TODO | 2 ++ example/example_org.py | 6 +++++- nsconfig/core.py | 23 ++++++++++++++++++++--- 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/TODO b/TODO index e1613f1..36631b1 100644 --- a/TODO +++ b/TODO @@ -4,3 +4,5 @@ - DNSSEC - Automated generation of Null MX - Logging +- Use dns.reversename.from_address? +- Decouple min_ttl from default TTL diff --git a/example/example_org.py b/example/example_org.py index f3b49a4..02c5afe 100644 --- a/example/example_org.py +++ b/example/example_org.py @@ -1,6 +1,10 @@ from example import nsc -z = nsc.add_zone('example.org', daemon_options=['check-integrity yes;']) +z = nsc.add_zone( + 'example.org', + daemon_options=['check-integrity yes;'], + add_null_mx=True, +) (z[""] .NS('ns1', 'ns2') diff --git a/nsconfig/core.py b/nsconfig/core.py index a5434ec..a091ffe 100644 --- a/nsconfig/core.py +++ b/nsconfig/core.py @@ -102,6 +102,7 @@ class NscZoneConfig: min_ttl: timedelta origin_server: str daemon_options: List[str] + add_null_mx: bool default_config: Optional['NscZoneConfig'] = None @@ -114,6 +115,7 @@ class NscZoneConfig: origin_server: Optional[str] = None, daemon_options: Optional[List[str]] = None, add_daemon_options: Optional[List[str]] = None, + add_null_mx: Optional[bool] = None, inherit_config: Optional['NscZoneConfig'] = None, ) -> None: if inherit_config is None: @@ -125,6 +127,7 @@ class NscZoneConfig: self.min_ttl = min_ttl if min_ttl is not None else inherit_config.min_ttl self.origin_server = origin_server if origin_server is not None else inherit_config.origin_server self.daemon_options = daemon_options if daemon_options is not None else inherit_config.daemon_options + self.add_null_mx = add_null_mx if add_null_mx is not None else inherit_config.add_null_mx if add_daemon_options is not None: self.daemon_options += add_daemon_options @@ -144,6 +147,7 @@ NscZoneConfig.default_config = NscZoneConfig( min_ttl=timedelta(days=1), origin_server="", daemon_options=[], + add_null_mx=False, ) @@ -309,9 +313,10 @@ class NscZonePrimary(NscZone): print(f'WARNING: Serial number overflow for zone {self.name}, current is {self.state.serial}') def process(self) -> None: - if self.zone_type == ZoneType.primary: - self.gen_hash() - self.gen_serial() + if self.config.add_null_mx: + self.gen_null_mx() + self.gen_hash() + self.gen_serial() def write_zone(self) -> None: self.update_soa() @@ -347,6 +352,18 @@ class NscZonePrimary(NscZone): return self[subdomain] + def gen_null_mx(self) -> None: + for name, node in self.zone.items(): + rds_a = node.get_rdataset(RdataClass.IN, RdataType.A) + rds_aaaa = node.get_rdataset(RdataClass.IN, RdataType.AAAA) + if rds_a or rds_aaaa: + mx_rds = node.get_rdataset(RdataClass.IN, RdataType.MX, create=True) + if not mx_rds: + mx_rds.add( + dns.rdtypes.ANY.MX.MX(RdataClass.IN, RdataType.MX, 0, dns.name.root), + ttl=self._min_ttl, + ) + class NscZoneSecondary(NscZone): primary_server: IPAddress -- 2.39.2