From 2ea53612feb07aea2c303e1330ea144a7f6148e5 Mon Sep 17 00:00:00 2001 From: Frank Brehm Date: Thu, 3 Aug 2017 12:13:14 +0200 Subject: [PATCH] Many new class properties in call PpConfigNamedApp --- pp_lib/config_named_app.py | 125 +++++++++++++++++++++++++++++++------ 1 file changed, 107 insertions(+), 18 deletions(-) diff --git a/pp_lib/config_named_app.py b/pp_lib/config_named_app.py index e5e2e35..adac6f6 100644 --- a/pp_lib/config_named_app.py +++ b/pp_lib/config_named_app.py @@ -18,6 +18,8 @@ import glob import copy import textwrap import socket +import pwd +import grp # Third party modules import six @@ -27,7 +29,7 @@ from .common import pp, to_bool from .cfg_app import PpCfgAppError, PpConfigApplication -__version__ = '0.1.0' +__version__ = '0.2.0' LOG = logging.getLogger(__name__) @@ -45,12 +47,20 @@ class PpConfigNamedApp(PpConfigApplication): default_pdns_api_host = 'systemshare.pixelpark.com' default_pdns_api_port = 8081 default_pdns_api_root_path = '/api/v1/servers/localhost' + default_named_conf = '/etc/named.conf' + default_named_basedir = '/var/named' + default_named_datadir = os.path.join(default_named_basedir, 'data') + default_named_slavedir = os.path.join(default_named_basedir, 'slaves') + default_named_iscdlv_key_file = '/etc/named.iscdlv.key' + default_named_rundir = '/run/named' + default_named_version2show = 'none' default_zone_masters = [ '217.66.53.86', ] re_split_addresses = re.compile(r'[,;\s]+') + re_integer = re.compile(r'^\s*(\d+)\s*$') # ------------------------------------------------------------------------- def __init__(self, appname=None, version=__version__): @@ -61,9 +71,29 @@ class PpConfigNamedApp(PpConfigApplication): self.pdns_api_key = None self.is_internal = False + self.named_listen_on_v6 = False + self.named_conf = self.default_named_conf self.zone_masters = copy.copy(self.default_zone_masters) + + self.named_user = 'named' + self.named_uid = None + self.named_group = 'named' + self.named_gid = None + + self.named_basedir = self.default_named_basedir + self.named_datadir = self.default_named_datadir + self.named_slavedir = self.default_named_slavedir + self.named_iscdlv_key_file = self.default_named_iscdlv_key_file + self.named_rundir = self.default_named_rundir + + self.named_dnssec = False + + self.named_logdir = '/var/log/named' self.query_log = False + self.named_show_bind_version = False + self.named_version2show = self.default_named_version2show + description = textwrap.dedent('''\ Generation of configuration of named (the BIND 9 name daemon). ''').strip() @@ -73,6 +103,8 @@ class PpConfigNamedApp(PpConfigApplication): cfg_stems='dns-deploy-zones', ) + self.post_init() + # ------------------------------------------------------------------------- def init_arg_parser(self): @@ -157,6 +189,7 @@ class PpConfigNamedApp(PpConfigApplication): except (TypeError, ValueError) as e: LOG.error("Wrong port number {!r} in configuration section {!r}: {}".format( section['port'], section_name, e)) + self.config_has_errors = True else: self.pdns_api_port = port @@ -167,6 +200,7 @@ class PpConfigNamedApp(PpConfigApplication): "The root path of the PowerDNS must be an absolute pathname " "(found [{}]/root_path => {!r} in configuration.").format(section_name, path) LOG.error(msg) + self.config_has_errors = True else: self.pdns_api_root_path = path @@ -191,23 +225,78 @@ class PpConfigNamedApp(PpConfigApplication): self.query_log = to_bool(section['query_log']) if 'masters' in section: - masters = [] - for m in self.re_split_addresses.split(section['masters']): - if m: - m = m.strip().lower() - try: - addr_info = socket.getaddrinfo( - m, 53, proto=socket.IPPROTO_TCP, family=socket.AF_INET) - except socket.gaierror as e: - msg = ( - "Invalid hostname or address {!r} found in " - "[{}]/masters: {}").format(m, section_name, e) - LOG.error(msg) - m = None - if m: - masters.append(m) - if masters: - self.zone_masters = masters + self._get_masters_from_cfg(section['masters'], section_name) + + # ------------------------------------------------------------------------- + def _get_masters_from_cfg(self, value, section_name): + + value = value.strip() + if not value: + msg = "No masters given in [{}]/masters.".format(section_name) + LOG.error(msg) + self.config_has_errors = True + return + + masters = [] + + for m in self.re_split_addresses.split(value): + if m: + m = m.strip().lower() + try: + addr_info = socket.getaddrinfo( + m, 53, proto=socket.IPPROTO_TCP, family=socket.AF_INET) + except socket.gaierror as e: + msg = ( + "Invalid hostname or address {!r} found in " + "[{}]/masters: {}").format(m, section_name, e) + LOG.error(msg) + self.config_has_errors = True + m = None + if m: + masters.append(m) + if masters: + self.zone_masters = masters + + # ------------------------------------------------------------------------- + def post_init(self): + + super(PpConfigNamedApp, self).post_init() + self.initialized = False + + cred_ok = True + LOG.debug("Checking named user {!r} and group {!r} ...".format( + self.named_user, self.named_group)) + + match = self.re_integer.search(self.named_user) + if match: + self.named_uid = int(match.group(1)) + else: + try: + uid = pwd.getpwnam(self.named_user).pw_uid + except KeyError: + msg = "Username {!r} not found.".format(self.named_user) + LOG.error(msg) + cred_ok = False + else: + self.named_uid = uid + + match = self.re_integer.search(self.named_group) + if match: + self.named_gid = int(match.group(1)) + else: + try: + gid = grp.getgrnam(self.named_group).gr_gid + except KeyError: + msg = "Group {!r} not found.".format(self.named_group) + LOG.error(msg) + cred_ok = False + else: + self.named_gid = gid + + if not cred_ok: + self.exit(1) + + self.initialized = True # ------------------------------------------------------------------------- def _run(self): -- 2.39.5