From 2bd79193ccc9b225accc7932847f3df0d6c57aae Mon Sep 17 00:00:00 2001 From: Frank Brehm Date: Mon, 15 Jan 2018 16:16:14 +0100 Subject: [PATCH] Moving method is_local() to class PpConfigApplication --- pp_lib/cfg_app.py | 64 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 63 insertions(+), 1 deletion(-) diff --git a/pp_lib/cfg_app.py b/pp_lib/cfg_app.py index d706aab..59d068c 100644 --- a/pp_lib/cfg_app.py +++ b/pp_lib/cfg_app.py @@ -20,6 +20,7 @@ import socket import pwd import pipes import codecs +import ipaddress from subprocess import Popen, PIPE @@ -49,7 +50,7 @@ from .mailaddress import MailAddress from .app import PpApplication -__version__ = '0.6.4' +__version__ = '0.6.5' LOG = logging.getLogger(__name__) VALID_MAIL_METHODS = ('smtp', 'sendmail') @@ -789,6 +790,67 @@ class PpConfigApplication(PpApplication): self.initialized = True + # ------------------------------------------------------------------------- + def is_local(self, domain): + + if self.verbose > 1: + LOG.debug("Checking, whether {!r} is a not public zone.".format(domain)) + + tld = domain.split('.')[-1] + if tld in ('intern', 'internal', 'local', 'localdomain', 'lokal'): + LOG.debug("Zone {!r} has a local TLD {!r}.".format(domain, tld)) + return True + + zone_base = domain.split('.')[0] + if zone_base in ('intern', 'internal', 'local', 'localdomain', 'lokal'): + LOG.debug("Zone {!r} has a local base {!r}.".format(domain, tld)) + return True + + if tld != 'arpa': + if self.verbose > 2: + LOG.debug("Zone {!r} has a public TLD {!r}.".format(domain, tld)) + return False + + if domain.endswith('.in-addr.arpa'): + tupels = [] + for tupel in reversed(domain.replace('.in-addr.arpa', '').split('.')): + tupels.append(tupel) + if self.verbose > 2: + LOG.debug("Got IPv4 tupels from zone {!r}: {}".format(domain, pp(tupels))) + bitmask = None + if len(tupels) == 1: + bitmask = 8 + tupels.append('0') + tupels.append('0') + tupels.append('0') + elif len(tupels) == 2: + tupels.append('0') + tupels.append('0') + bitmask = 16 + elif len(tupels) == 3: + bitmask = 24 + tupels.append('0') + else: + LOG.warn("Could not interprete reverse IPv4 zone {!r}.".format(domain)) + return False + net_address = '.'.join(tupels) + '/{}'.format(bitmask) + if self.verbose > 2: + LOG.debug("Got IPv4 network address of zone {!r}: {!r}.".format(domain, net_address)) + network = ipaddress.ip_network(net_address) + if network.is_global: + if self.verbose > 1: + LOG.debug("The network {!r} of zone {!r} is allocated for public networks.".format( + net_address, domain)) + return False + LOG.debug("The network {!r} of zone {!r} is allocated for local networks.".format( + net_address, domain)) + return True + + if self.verbose > 2: + LOG.debug("Zone {!r} seems to be a reverse zone for a public network.".format(domain)) + return False + + # ============================================================================= if __name__ == "__main__": -- 2.39.5