diff --git a/actions/fqdn-change b/actions/fqdn-change new file mode 100755 index 000000000..e12d038c3 --- /dev/null +++ b/actions/fqdn-change @@ -0,0 +1,27 @@ +#!/bin/sh +# +# This file is part of Plinth. +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . +# + +fqdn="$1" +hostname=$(hostname) + +if grep -q 127.0.1.1 /etc/hosts ; then + sed -i "s/127.0.1.1.*/127.0.1.1 $fqdn $hostname/" /etc/hosts +else + sed -i "/127.0.0.1.*/a \ +127.0.1.1 $fqdn $hostname" /etc/hosts +fi diff --git a/plinth/modules/config/config.py b/plinth/modules/config/config.py index d91215d45..9c5efd641 100644 --- a/plinth/modules/config/config.py +++ b/plinth/modules/config/config.py @@ -32,6 +32,7 @@ import socket from plinth import actions from plinth import cfg from plinth.signals import pre_hostname_change, post_hostname_change +from plinth.signals import fqdn_change LOGGER = logging.getLogger(__name__) @@ -42,6 +43,11 @@ def get_hostname(): return socket.gethostname() +def get_fqdn(): + """Return the fully qualified domain name""" + return socket.getfqdn() + + class TrimmedCharField(forms.CharField): """Trim the contents of a CharField""" def clean(self, value): @@ -69,6 +75,15 @@ and must not be greater than 63 characters in length.'), validators.RegexValidator(r'^[a-zA-Z][a-zA-Z0-9]{,62}$', _('Invalid hostname'))]) + fqdn = TrimmedCharField( + label=_('FQDN'), + help_text=_('Your FQDN is the global name by which other machines \ +on the Internet can reach you. It must consist of alphanumeric words \ +separated by dots.'), + validators=[ + validators.RegexValidator(r'^[a-zA-Z][a-zA-Z0-9.]*$', + _('Invalid FQDN'))]) + def __init__(self, *args, **kwargs): # pylint: disable-msg=E1101, W0233 forms.Form.__init__(self, *args, **kwargs) @@ -125,6 +140,7 @@ def index(request): def get_status(): """Return the current status""" return {'hostname': get_hostname(), + 'fqdn': get_fqdn(), 'time_zone': open('/etc/timezone').read().rstrip()} @@ -141,6 +157,17 @@ def _apply_changes(request, old_status, new_status): else: messages.info(request, _('Hostname is unchanged')) + if old_status['fqdn'] != new_status['fqdn']: + try: + set_fqdn(new_status['fqdn']) + except Exception as exception: + messages.error(request, _('Error setting FQDN: %s') % + exception) + else: + messages.success(request, _('FQDN set')) + else: + messages.info(request, _('FQDN is unchanged')) + if old_status['time_zone'] != new_status['time_zone']: try: actions.superuser_run('timezone-change', [new_status['time_zone']]) @@ -171,3 +198,18 @@ def set_hostname(hostname): post_hostname_change.send_robust(sender='config', old_hostname=old_hostname, new_hostname=hostname) + + +def set_fqdn(fqdn): + """Sets machine FQDN to fqdn""" + old_fqdn = get_fqdn() + + # FQDN should be ASCII. If it's unicode, convert to ASCII. + fqdn = str(fqdn) + + LOGGER.info('Changing FQDN to - %s', fqdn) + actions.superuser_run('fqdn-change', fqdn) + + fqdn_change.send_robust(sender='config', + old_fqdn=old_fqdn, + new_fqdn=fqdn) diff --git a/plinth/signals.py b/plinth/signals.py index e5e5d718c..e07d9380b 100644 --- a/plinth/signals.py +++ b/plinth/signals.py @@ -27,3 +27,4 @@ pre_module_loading = Signal() post_module_loading = Signal() pre_hostname_change = Signal(providing_args=['old_hostname', 'new_hostname']) post_hostname_change = Signal(providing_args=['old_hostname', 'new_hostname']) +fqdn_change = Signal(providing_args=['old_fqdn', 'new_fqdn'])