config: Use privileged decorator for set domainname action

Tests:

- Running flake8 as in .gitlab-ci.yml works.
- Setting the domain name again to update /etc/hosts file after hostname change
  works
- Setting the domain name from the text box works. New domain name is read back
  and shown properly.

Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
This commit is contained in:
Sunil Mohan Adapa 2022-09-01 22:07:43 -07:00 committed by James Valleroy
parent 40bf6add75
commit b607174434
No known key found for this signature in database
GPG Key ID: 77C0C75E7B650808
4 changed files with 36 additions and 31 deletions

View File

@ -16,7 +16,7 @@ code-quality:
stage: test
needs: []
script:
- python3 -m flake8 --exclude actions/domainname-change,actions/networks container plinth actions/*
- python3 -m flake8 --exclude actions/networks container plinth actions/*
unit-tests:
stage: test

View File

@ -1,21 +0,0 @@
#!/bin/sh
# SPDX-License-Identifier: AGPL-3.0-or-later
domainname="$1"
hostname=$(hostname)
if [ -z "$domainname" ] ; then
if grep -q 127.0.1.1 /etc/hosts ; then
sed -i "s/127.0.1.1.*/127.0.1.1 $hostname/" /etc/hosts
else
sed -i "/127.0.0.1.*/a \
127.0.1.1 $hostname" /etc/hosts
fi
else
if grep -q 127.0.1.1 /etc/hosts ; then
sed -i "s/127.0.1.1.*/127.0.1.1 $hostname.$domainname $hostname/" /etc/hosts
else
sed -i "/127.0.0.1.*/a \
127.0.1.1 $hostname.$domainname $hostname" /etc/hosts
fi
fi

View File

@ -4,6 +4,7 @@
import os
import pathlib
import subprocess
from typing import Optional
import augeas
@ -29,6 +30,32 @@ def set_hostname(hostname: str):
action_utils.service_restart('avahi-daemon')
@privileged
def set_domainname(domainname: Optional[str] = None):
"""Set system domainname in /etc/hosts."""
hostname = subprocess.check_output(['hostname']).decode().strip()
hosts_path = pathlib.Path('/etc/hosts')
if domainname:
insert_line = f'127.0.1.1 {hostname}.{domainname} {hostname}\n'
else:
insert_line = f'127.0.1.1 {hostname}\n'
lines = hosts_path.read_text(encoding='utf-8').splitlines(keepends=True)
new_lines = []
found = False
for line in lines:
if '127.0.1.1' in line:
new_lines.append(insert_line)
found = True
else:
new_lines.append(line)
if not found:
new_lines.append(insert_line)
hosts_path.write_text(''.join(new_lines), encoding='utf-8')
def load_augeas():
"""Initialize Augeas."""
aug = augeas.Augeas(flags=augeas.Augeas.NO_LOAD +

View File

@ -1,14 +1,12 @@
# SPDX-License-Identifier: AGPL-3.0-or-later
"""
FreedomBox views for basic system configuration.
"""
"""FreedomBox views for basic system configuration."""
import logging
from django.contrib import messages
from django.utils.translation import gettext as _
from plinth import actions, views
from plinth import views
from plinth.modules import config
from plinth.signals import (domain_added, domain_removed, post_hostname_change,
pre_hostname_change)
@ -21,11 +19,12 @@ LOGGER = logging.getLogger(__name__)
class ConfigAppView(views.AppView):
"""Serve configuration page."""
form_class = ConfigurationForm
app_id = 'config'
def get_initial(self):
"""Return the current status"""
"""Return the current status."""
return {
'hostname': config.get_hostname(),
'domainname': config.get_domainname(),
@ -35,7 +34,7 @@ class ConfigAppView(views.AppView):
}
def form_valid(self, form):
"""Apply the form changes"""
"""Apply the form changes."""
old_status = form.initial
new_status = form.cleaned_data
@ -117,7 +116,7 @@ def set_hostname(hostname):
privileged.set_hostname(hostname)
LOGGER.info('Setting domain name after hostname change - %s', domainname)
actions.superuser_run('domainname-change', [domainname])
privileged.set_domainname(domainname)
post_hostname_change.send_robust(sender='config',
old_hostname=old_hostname,
@ -125,7 +124,7 @@ def set_hostname(hostname):
def set_domainname(domainname, old_domainname):
"""Sets machine domain name to domainname"""
"""Set machine domain name to domainname."""
old_domainname = config.get_domainname()
# Domain name is not case sensitive, but Let's Encrypt certificate
@ -133,7 +132,7 @@ def set_domainname(domainname, old_domainname):
domainname = domainname.lower()
LOGGER.info('Changing domain name to - %s', domainname)
actions.superuser_run('domainname-change', [domainname])
privileged.set_domainname(domainname)
# Update domain registered with Name Services module.
if old_domainname: