fix adding DKIM in the root of a zone
[zonemaker.git] / zone.py
diff --git a/zone.py b/zone.py
index 473b79f829bdf2da85d7046449422bae2c041406..c311a214c6843ea1baa5834a0add9d636ef1c91b 100644 (file)
--- a/zone.py
+++ b/zone.py
@@ -56,6 +56,13 @@ def check_hex(data: str) -> str:
         return data
     raise Exception(data+" is not valid hex data")
 
+def check_base64(data: str) -> str:
+    data = str(data)
+    if re.match('^[a-zA-Z0-9+/=]+$', data):
+        return data
+    raise Exception(data+" is not valid hex data")
+
+
 def check_ipv4(address: str) -> str:
     address = str(address)
     if re.match(REGEX_ipv4, address):
@@ -135,11 +142,11 @@ class MX:
 
 
 class TXT:
-    def __init__(self, name: str, text: str) -> None:
+    def __init__(self, text: str) -> None:
         # test for bad characters
         for c in ('\n', '\r', '\t'):
             if c in text:
-                raise Exception("TXT record {0} containts invalid character")
+                raise Exception("TXT record {0} contains invalid character")
         # escape text
         for c in ('\\', '\"'):
             text = text.replace(c, '\\'+c)
@@ -149,6 +156,24 @@ class TXT:
         return zone.RR(owner, 'TXT', '"{0}"'.format(self._text))
 
 
+class DKIM(TXT): # helper class to treat DKIM more antively
+    class Version:
+        DKIM1 = "DKIM1"
+    
+    class Algorithm:
+        RSA = "rsa"
+    
+    def __init__(self, selector, version, alg, key):
+        self._selector = check_label(selector)
+        version = check_label(version)
+        alg = check_label(alg)
+        key = check_base64(key)
+        super().__init__("v={0}; k={1}; p={2}".format(version, alg, key))
+    
+    def generate_rr(self, owner, zone):
+        return super().generate_rr('{0}._domainkey.{1}'.format(self._selector, zone.abs_hostname(owner)), zone)
+
+
 class SRV:
     def __init__(self, protocol: str, service: str, name: str, port: int, prio: int, weight: int) -> None:
         self._service = check_label(service)