From a90078175b22c61a7dadfd3663f30675f4100b63 Mon Sep 17 00:00:00 2001 From: Anders Blomdell <anders.blomdell@control.lth.se> Date: Thu, 1 Nov 2018 10:11:37 +0100 Subject: [PATCH] Cleanup handling of names that equals domain --- src/hostinfo/named.py | 41 ++++++++++++++++++----------------------- 1 file changed, 18 insertions(+), 23 deletions(-) diff --git a/src/hostinfo/named.py b/src/hostinfo/named.py index 2d2e0a5..428a5c4 100755 --- a/src/hostinfo/named.py +++ b/src/hostinfo/named.py @@ -6,6 +6,13 @@ import re import sys import os +class NoNameserverException(Exception): + pass + +class InvalidHostnameException(Exception): + pass + + def generate(tree, options): # # A. Check if host is a nameserver @@ -100,7 +107,7 @@ def header(tree, domain, origin=None): if not filter(lambda ns: ns.domain[0] == domain and ns.primary[0] == 'yes', tree._host_._interface_._nameserver_): - raise Exception("No nameserver defined for %s" % domain) + raise NoNameserverException("No nameserver defined for %s" % domain) result = util.StringArray() if origin: result += "$ORIGIN %s." % origin @@ -153,8 +160,8 @@ class DomainDict: pass def add_host(self, name, ttl, kind, value): - if not re.match('^[*0-9a-zA-Z][-.0-9a-zA-Z]*$', name): - raise Exception('Invalid host name "%s"' % name) + if not re.match('^([*0-9a-zA-Z][-.0-9a-zA-Z]*|@)$', name): + raise InvalidHostnameException('Invalid host name "%s"' % name) if not name in self.host: self.host[name] = set() pass @@ -197,18 +204,6 @@ class DomainDict: def generate_forward(tree, hosts): def callback(domain): result = header(tree, domain, domain) - for name,address,ttl in hosts: - if name == '%s.' % (domain): - # Host with same name as domain goes into header - if address.version == 4: - result += ('@ %-8s IN A %s' % - (ttl, str(address.exploded))) - elif address.version == 6: - result += ('@ %-8s IN AAAA %s' % - (ttl, str(address.exploded))) - pass - pass - pass for mx in [ m for m in tree._host_._interface_._mailhost_ if m.domain[0] == domain]: pri = int(mx.priority[0] or 0) @@ -276,15 +271,15 @@ def generate_forward(tree, hosts): for s in tree._subnet_ if s.domain[0] and util.network(s)]: for name,address,ttl in hosts: - try: - if name.endswith('.'): - d = '.'.join(name.split('.')[1:-1]) - n = name.split('.')[0] - add_host(d, n, ttl, address) - continue - except Exception,e: + if name.endswith('.'): + if name[0:-1] == domain: + add_host(domain, '@', ttl, address) + pass + elif '.'.join(name.split('.')[1:-1]) == domain: + add_host(domain, name.split('.')[0], ttl, address) + pass pass - if address in net: + elif address in net: add_host(domain, name, ttl, address) pass pass -- GitLab