From 45fc3211b0ff4b451d379b89d45f4dc7e070f446 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Sat, 8 Nov 2014 22:46:50 +0100 Subject: [PATCH] validate hostnames --- zonemaker/zone.py | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/zonemaker/zone.py b/zonemaker/zone.py index 5e83854..8e87f6e 100644 --- a/zonemaker/zone.py +++ b/zonemaker/zone.py @@ -1,10 +1,19 @@ -import ipaddress +import ipaddress, re second = 1 minute = 60*second hour = 60*minute day = 24*hour +def hostname(name): + # check hostname for validity + label = r'[a-zA-Z90-9]([a-zA-Z90-9-]{0,61}[a-zA-Z90-9])?' # must not start or end with hyphen + pattern = r'^{0}(\.{0})*\.?'.format(label) + print(pattern) + if re.match(pattern, name): + return name + raise Exception(name+" is not a valid hostname") + class Address: def __init__(self, IPv4 = None, IPv6 = None): self._IPv4 = None if IPv4 is None else ipaddress.IPv4Address(IPv4) @@ -38,6 +47,17 @@ class Zone: secondary_refresh, secondary_retry, secondary_discard, NX_TTL = None, A_TTL = None, other_TTL = None, domains = []): + self._name = hostname(name) + assert mail.endswith('.'), "Mail must be absolute, end with a dot" + atpos = mail.find('@') + assert atpos >= 0 and atpos < mail.find('.'), "Mail must contain an @ before the first dot" + self._mail = hostname(mail.replace('@', '.', 1)) + self._NS = list(map(hostname, NS)) + + self._secondary_refresh = secondary_refresh + self._secondary_retry = secondary_retry + self._secondary_discard = secondary_discard + assert other_TTL is not None self._NX_TTL = other_TTL if NX_TTL is None else NX_TTL self._A_TTL = other_TTL if A_TTL is None else A_TTL -- 2.30.2