X-Git-Url: https://git.ralfj.de/zonemaker.git/blobdiff_plain/febb638742bc5f95520e96695f5d0a7ba14ff7d3..e755148dea6ccfd48ee64670bd2f59f311dde159:/zonemaker/zone.py?ds=inline diff --git a/zonemaker/zone.py b/zonemaker/zone.py index 5f8e065..add2bde 100644 --- a/zonemaker/zone.py +++ b/zonemaker/zone.py @@ -1,6 +1,5 @@ -import re -from ipaddress import IPv4Address, IPv6Address -from typing import List, Dict, Any, Iterator, Tuple, Sequence +import re, datetime +#from typing import * second = 1 @@ -10,14 +9,18 @@ day = 24*hour week = 7*day REGEX_label = r'[a-zA-Z90-9]([a-zA-Z90-9-]{0,61}[a-zA-Z90-9])?' # max. 63 characters; must not start or end with hyphen +REGEX_ipv4 = r'^\d{1,3}(\.\d{1,3}){3}$' +REGEX_ipv6 = r'^[a-fA-F0-9]{1,4}(:[a-fA-F0-9]{1,4}){7}$' def check_label(label: str) -> str: + label = str(label) pattern = r'^{0}$'.format(REGEX_label) if re.match(pattern, label): return label raise Exception(label+" is not a valid label") def check_hostname(name: str) -> str: + name = str(name) # check hostname for validity pattern = r'^{0}(\.{0})*\.?$'.format(REGEX_label) if re.match(pattern, name): @@ -25,10 +28,23 @@ def check_hostname(name: str) -> str: raise Exception(name+" is not a valid hostname") def check_hex(data: str) -> str: + data = str(data) if re.match('^[a-fA-F0-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): + return address + raise Exception(address+" is not a valid IPv4 address") + +def check_ipv6(address: str) -> str: + address = str(address) + if re.match(REGEX_ipv6, address): + return address + raise Exception(address+" is not a valid IPv6 address") + def time(time: int) -> str: if time == 0: return "0" @@ -43,7 +59,7 @@ def time(time: int) -> str: else: return str(time) -def column_widths(datas: Sequence, widths: Sequence[int]): +def column_widths(datas: 'Sequence', widths: 'Sequence[int]'): assert len(datas) == len(widths)+1, "There must be as one more data points as widths" result = "" width_sum = 0 @@ -72,17 +88,17 @@ class Digest: ## Record types class A: def __init__(self, address: str) -> None: - self._address = IPv4Address(address) + self._address = check_ipv4(address) - def generate_rr(self, owner: str, zone: 'Zone') -> Any: + def generate_rr(self, owner: str, zone: 'Zone') -> 'Any': return zone.RR(owner, 'A', self._address) class AAAA: def __init__(self, address: str) -> None: - self._address = IPv6Address(address) + self._address = check_ipv6(address) - def generate_rr(self, owner: str, zone: 'Zone') -> Any: + def generate_rr(self, owner: str, zone: 'Zone') -> 'Any': return zone.RR(owner, 'AAAA', self._address) @@ -91,7 +107,7 @@ class MX: self._priority = int(prio) self._name = check_hostname(name) - def generate_rr(self, owner: str, zone: 'Zone') -> Any: + def generate_rr(self, owner: str, zone: 'Zone') -> 'Any': return zone.RR(owner, 'MX', '{0} {1}'.format(self._priority, zone.abs_hostname(self._name))) @@ -104,7 +120,7 @@ class SRV: self._port = int(port) self._name = check_hostname(name) - def generate_rr(self, owner: str, zone: 'Zone') -> Any: + def generate_rr(self, owner: str, zone: 'Zone') -> 'Any': return zone.RR('_{0}._{1}.{2}'.format(self._service, self._protocol, owner), 'SRV', '{0} {1} {2} {3}'.format(self._priority, self._weight, self._port, zone.abs_hostname(self._name))) @@ -133,7 +149,7 @@ class TLSA: self._matching_type = int(matching_type) self._data = check_hex(data) - def generate_rr(self, owner: str, zone: 'Zone') -> Any: + def generate_rr(self, owner: str, zone: 'Zone') -> 'Any': return zone.RR('_{0}._{1}.{2}'.format(self._port, self._protocol, owner), 'TLSA', '{0} {1} {2} {3}'.format(self._usage, self._selector, self._matching_type, self._data)) @@ -141,7 +157,7 @@ class CNAME: def __init__(self, name: str) -> None: self._name = check_hostname(name) - def generate_rr(self, owner: str, zone: 'Zone') -> Any: + def generate_rr(self, owner: str, zone: 'Zone') -> 'Any': return zone.RR(owner, 'CNAME', zone.abs_hostname(self._name)) @@ -149,7 +165,7 @@ class NS: def __init__(self, name: str) -> None: self._name = check_hostname(name) - def generate_rr(self, owner: str, zone: 'Zone') -> Any: + def generate_rr(self, owner: str, zone: 'Zone') -> 'Any': return zone.RR(owner, 'NS', zone.abs_hostname(self._name)) @@ -160,15 +176,15 @@ class DS: self._alg = int(alg) self._digest = int(digest) - def generate_rr(self, owner: str, zone: 'Zone') -> Any: + def generate_rr(self, owner: str, zone: 'Zone') -> 'Any': return zone.RR(owner, 'DS', '{0} {1} {2} {3}'.format(self._tag, self._alg, self._digest, self._key)) ## Higher-level classes class Name: - def __init__(self, *records: List[Any]) -> None: + def __init__(self, *records: 'List[Any]') -> None: self._records = records - def generate_rrs(self, owner: str, zone: 'Zone') -> Iterator: + def generate_rrs(self, owner: str, zone: 'Zone') -> 'Iterator': for record in self._records: # this could still be a list if isinstance(record, list): @@ -191,12 +207,11 @@ def SecureDelegation(name: str, tag: int, alg: int, digest: int, key: str) -> Na class Zone: - def __init__(self, name: str, serialfile: str, dbfile: str, mail: str, NS: List[str], + def __init__(self, name: str, serialfile: str, mail: str, NS: 'List[str]', secondary_refresh: int, secondary_retry: int, secondary_expire: int, NX_TTL: int = None, A_TTL: int = None, other_TTL: int = None, - domains: Dict[str, Any] = {}) -> None: + domains: 'Dict[str, Any]' = {}) -> None: self._serialfile = serialfile - self._dbfile = dbfile if not name.endswith('.'): raise Exception("Expected an absolute hostname") self._name = check_hostname(name) @@ -243,7 +258,7 @@ class Zone: try: with open(self._serialfile) as f: cur_serial = int(f.read()) - except FileNotFoundError: + except (OSError, IOError): # FileNotFoundError has been added in Python 3.3 pass # increment serial cur_serial += 1 @@ -253,12 +268,12 @@ class Zone: # be done return cur_serial - def generate_rrs(self) -> Iterator: + def generate_rrs(self) -> 'Iterator': # SOA record serial = self.inc_serial() yield self.RR(self._name, 'SOA', - ('{NS} {mail} ({serial} {refresh} {retry} {expire} {NX_TTL}) ; '+ - '(serial refresh retry expire NX_TTL)').format( + ('{NS} {mail} {serial} {refresh} {retry} {expire} {NX_TTL}'+ + ' ; primns mail serial refresh retry expire NX_TTL').format( NS=self.abs_hostname(self._NS[0]), mail=self._mail, serial=serial, refresh=time(self._refresh), retry=time(self._retry), expire=time(self._expire), NX_TTL=time(self._NX_TTL)) @@ -272,7 +287,6 @@ class Zone: yield rr def write(self) -> None: - with open(self._dbfile, 'w') as f: - for rr in self.generate_rrs(): - f.write(rr+"\n") - print(rr) + print(";; {0} zone file, generated by zonemaker on {1}".format(self._name, datetime.datetime.now())) + for rr in self.generate_rrs(): + print(rr)