]> mj.ucw.cz Git - pynsc.git/commitdiff
Implement add_null_mx
authorMartin Mares <mj@ucw.cz>
Mon, 22 Apr 2024 09:00:04 +0000 (11:00 +0200)
committerMartin Mares <mj@ucw.cz>
Mon, 22 Apr 2024 09:00:04 +0000 (11:00 +0200)
TODO
example/example_org.py
nsconfig/core.py

diff --git a/TODO b/TODO
index e1613f142aa2e51b6890cd2299a2488a33986dd6..36631b1c8fa28a1ebfc116f355d0cfb4c93d0f70 100644 (file)
--- 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
index f3b49a4e2eeb7fc9e6c5358b35d9ec2b006de6ee..02c5afee3a840233c9506d465343d62ba16317bf 100644 (file)
@@ -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')
index a5434ec6c63df2ee1e5db97f9b8a3ee3a0896cc9..a091ffeedb32851c234eb50b5e58e3e92acd29ca 100644 (file)
@@ -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