diff --git a/actions/avahi b/actions/avahi new file mode 100755 index 000000000..882c08582 --- /dev/null +++ b/actions/avahi @@ -0,0 +1,61 @@ +#!/usr/bin/python3 +# +# 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 . +# + +""" +Configuration helper for service discovery +""" + +import argparse + +from plinth import action_utils + + +def parse_arguments(): + """Return parsed command line arguments as dictionary.""" + parser = argparse.ArgumentParser() + subparsers = parser.add_subparsers(dest='subcommand', help='Sub command') + + subparsers.add_parser('enable', help='Enable avahi-daemon service') + subparsers.add_parser('disable', help='Disable avahi-daemon service') + + return parser.parse_args() + + +def subcommand_enable(_): + """Start service.""" + action_utils.service_enable('avahi-daemon.socket') + action_utils.service_enable('avahi-daemon.service') + + +def subcommand_disable(_): + """Stop service.""" + action_utils.service_disable('avahi-daemon.service') + action_utils.service_disable('avahi-daemon.socket') + + +def main(): + """Parse arguments and perform all duties.""" + arguments = parse_arguments() + + subcommand = arguments.subcommand.replace('-', '_') + subcommand_method = globals()['subcommand_' + subcommand] + subcommand_method(arguments) + + +if __name__ == '__main__': + main() diff --git a/data/etc/plinth/modules-enabled/avahi b/data/etc/plinth/modules-enabled/avahi new file mode 100644 index 000000000..bf2d295be --- /dev/null +++ b/data/etc/plinth/modules-enabled/avahi @@ -0,0 +1 @@ +plinth.modules.avahi diff --git a/plinth/modules/avahi/__init__.py b/plinth/modules/avahi/__init__.py new file mode 100644 index 000000000..782602259 --- /dev/null +++ b/plinth/modules/avahi/__init__.py @@ -0,0 +1,56 @@ +# +# 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 . +# + +""" +Plinth module for service discovery +""" + +from gettext import gettext as _ +import subprocess + +from plinth import actions +from plinth import action_utils +from plinth import cfg +from plinth import service as service_module + +# pylint: disable=C0103 + +depends = ['plinth.modules.system'] + +service = None + + +def init(): + """Intialize the service discovery module.""" + menu = cfg.main_menu.get('system:index') + menu.add_urlname(_('Service Discovery'), 'glyphicon-lamp', + 'avahi:index', 950) + + global service # pylint: disable=W0603 + service = service_module.Service( + 'avahi', _('Service Discovery'), ['mdns'], + is_external=False, enabled=is_enabled()) + + +def is_enabled(): + """Return whether the module is enabled.""" + return action_utils.service_is_enabled('avahi-daemon') + + +def is_running(): + """Return whether the service is running.""" + return action_utils.service_is_running('avahi-daemon') diff --git a/plinth/modules/avahi/forms.py b/plinth/modules/avahi/forms.py new file mode 100644 index 000000000..2c4ddec3b --- /dev/null +++ b/plinth/modules/avahi/forms.py @@ -0,0 +1,30 @@ +# +# 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 . +# + +""" +Plinth module for service discovery forms +""" + +from django import forms +from gettext import gettext as _ + + +class ServiceDiscoveryForm(forms.Form): + """Service discovery form.""" + enabled = forms.BooleanField( + label=_('Enable service discovery'), + required=False) diff --git a/plinth/modules/avahi/templates/avahi.html b/plinth/modules/avahi/templates/avahi.html new file mode 100644 index 000000000..a2938a4b0 --- /dev/null +++ b/plinth/modules/avahi/templates/avahi.html @@ -0,0 +1,54 @@ +{% extends "base.html" %} +{% comment %} +# +# 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 . +# +{% endcomment %} + +{% load bootstrap %} + +{% block content %} + +

Service Discovery

+ +

Service discovery is a program that can discover other machines and services +running on your local network. It can also helps those other machines find your +FreedomBox, and the services running on it. Service discovery is not required, +though. Some users decide to disable it to minimize the +attack surface +of their FreedomBox. + +

Status

+ +

+ {% if status.is_running %} + Service discovery server is running + {% else %} + Service discovery server is not running + {% endif %} +

+ +

Configuration

+ +
+ {% csrf_token %} + + {{ form|bootstrap }} + + +
+ +{% endblock %} diff --git a/plinth/modules/avahi/tests/__init__.py b/plinth/modules/avahi/tests/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/plinth/modules/avahi/urls.py b/plinth/modules/avahi/urls.py new file mode 100644 index 000000000..4f65f4813 --- /dev/null +++ b/plinth/modules/avahi/urls.py @@ -0,0 +1,27 @@ +# +# 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 . +# + +""" +URLs for the service discovery module +""" + +from django.conf.urls import patterns, url + +urlpatterns = patterns( # pylint: disable=C0103 + 'plinth.modules.avahi.views', + url(r'^sys/avahi/$', 'index', name='index'), + ) diff --git a/plinth/modules/avahi/views.py b/plinth/modules/avahi/views.py new file mode 100644 index 000000000..0e76ccebd --- /dev/null +++ b/plinth/modules/avahi/views.py @@ -0,0 +1,76 @@ +# +# 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 . +# + +""" +Plinth module for service discovery views +""" + +from django.contrib import messages +from django.template.response import TemplateResponse +from gettext import gettext as _ +import logging + +from .forms import ServiceDiscoveryForm +from plinth import actions +from plinth import package +from plinth.modules import avahi + + +logger = logging.getLogger(__name__) # pylint: disable=C0103 + + +@package.required(['avahi-daemon']) +def index(request): + """Serve configuration page.""" + status = get_status() + + form = None + + if request.method == 'POST': + form = ServiceDiscoveryForm(request.POST) + if form.is_valid(): + _apply_changes(request, status, form.cleaned_data) + status = get_status() + form = ServiceDiscoveryForm(initial=status) + else: + form = ServiceDiscoveryForm(initial=status) + + return TemplateResponse(request, 'avahi.html', + {'title': _('Service Discovery'), + 'status': status, + 'form': form}) + + +def get_status(): + """Get the current settings from server.""" + return {'enabled': avahi.is_enabled(), + 'is_running': avahi.is_running()} + + +def _apply_changes(request, old_status, new_status): + """Apply the changes.""" + modified = False + + if old_status['enabled'] != new_status['enabled']: + sub_command = 'enable' if new_status['enabled'] else 'disable' + modified = True + actions.superuser_run('avahi', [sub_command]) + avahi.service.notify_enabled(None, new_status['enabled']) + messages.success(request, _('Configuration updated')) + + if not modified: + messages.info(request, _('Setting unchanged'))