diff --git a/actions/bind b/actions/bind index e40cfb5a6..2023929ad 100755 --- a/actions/bind +++ b/actions/bind @@ -28,9 +28,9 @@ from plinth import action_utils CONFIG_FILE = '/etc/bind/named.conf.options' -value1 = 'acl goodclients { \n localhost;\n};\n' +value1 = 'acl goodclients { \n localnets;\n};\n' value2 = ' recursion yes;\n allow-query { goodclients; };\n\n' -value3 = ' // 8.8.8.8;\n // 8.8.4.4;\n' +value3 = ' // 8.8.8.8 8.8.4.4;\n' value4 = ' //forward first;\n' value5 = ' //dnssec-enable yes;\n' @@ -46,6 +46,9 @@ def parse_arguments(): configure.add_argument('--enable-dnssec', choices=['true', 'false'], help='Set DNSSEC true/false') + dns = subparsers.add_parser('dns', help='Set up DNS server') + dns.add_argument('--set', help='Set DNS server') + return parser.parse_args() @@ -69,6 +72,15 @@ def subcommand_setup(_): enable_dnssec(True) action_utils.service_restart('bind9') +def subcommand_dns(arguments): + """Setting DNS servers""" + + if arguments.set: + set(arguments.set) + + action_utils.service_restart('bind9') + + def subcommand_configure(arguments): """Configure BIND.""" @@ -98,8 +110,6 @@ def set_forwarding(choice): f.write(line+'\n') f.close() - - else: if '// forwarders {' in data: f = open(CONFIG_FILE, 'w') @@ -136,6 +146,22 @@ def enable_dnssec(choice): f.close() +def set(DNS): + flag = 0 + data = [line.strip() for line in open(CONFIG_FILE, 'r')] + f = open(CONFIG_FILE, 'w') + for line in data: + if 'forwarders {' in line : + f.write(line+'\n') + f.write(DNS+';\n') + flag = 1 + elif '};' and flag == 1: + flag = 0 + elif flag == 0: + f.write(line+'\n') + f.close() + + def main(): """Parse arguments and perform all duties""" arguments = parse_arguments() diff --git a/plinth/modules/bind/__init__.py b/plinth/modules/bind/__init__.py index 07519970d..bec5e2321 100644 --- a/plinth/modules/bind/__init__.py +++ b/plinth/modules/bind/__init__.py @@ -65,7 +65,7 @@ def init(): setup_helper = globals()['setup_helper'] if setup_helper.get_state() != 'needs-setup': service = service_module.Service( - managed_services[0], title, ports=['bind-plinth'], + managed_services[0], title, ports=['dns'], is_external=False, ) @@ -127,8 +127,19 @@ def get_default(): enable_dnssec = False else: enable_dnssec = True + + flag = 0 + for line in data: + + if flag == 1: + dns_set = line[:len(line)-1] + flag = 0 + if 'forwarders {' in line: + flag = 1 + conf = { 'set_forwarding': set_forwarding, - 'enable_dnssec': enable_dnssec + 'enable_dnssec': enable_dnssec, + 'dns_set': dns_set } return conf diff --git a/plinth/modules/bind/forms.py b/plinth/modules/bind/forms.py index 1ea6629ab..ed4764d51 100644 --- a/plinth/modules/bind/forms.py +++ b/plinth/modules/bind/forms.py @@ -24,6 +24,8 @@ from django.utils.translation import ugettext_lazy as _ from plinth.forms import ServiceForm +from . import get_default + class BindForm(ServiceForm): """BIND configuration form""" @@ -36,3 +38,8 @@ class BindForm(ServiceForm): label=_('Enable DNSSEC'), required=False, help_text=_('Enable Domain Name System Security Extensions')) + + dns_set = forms.CharField( + label=_('Set DNS server'), + required=False, + help_text=_('Set new DNS server')) diff --git a/plinth/modules/bind/views.py b/plinth/modules/bind/views.py index d030914c8..fc1e3ce04 100644 --- a/plinth/modules/bind/views.py +++ b/plinth/modules/bind/views.py @@ -65,5 +65,11 @@ class BindServiceView(ServiceView): # pylint: disable=too-many-ancestors messages.success(self.request, _('Enable DNSSEC configuration updated')) + if old_config['dns_set'] != data['dns_set']: + actions.superuser_run( + 'bind', + ['dns', '--set', data['dns_set']]) + messages.success(self.request, + _('DNS server configuration updated')) return super().form_valid(form)