mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-11-29 08:25:10 +00:00
Reorganize python sources into 'plinth' package
This commit is contained in:
1 parent
eaacd3864b
commit
65fa648d9f
109 files changed
+63
-61
No files matched your search
@@ -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 <http://www.gnu.org/licenses/>.
|
||||
#
|
||||
|
||||
"""
|
||||
Plinth module to configure a firewall
|
||||
"""
|
||||
|
||||
from . import firewall
|
||||
from .firewall import init
|
||||
|
||||
__all__ = ['firewall', 'init']
|
||||
|
||||
DEPENDS = ['system']
|
||||
@@ -0,0 +1,147 @@
|
||||
#
|
||||
# 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 <http://www.gnu.org/licenses/>.
|
||||
#
|
||||
|
||||
"""
|
||||
Plinth module to configure a firewall
|
||||
"""
|
||||
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from django.template.response import TemplateResponse
|
||||
from gettext import gettext as _
|
||||
import logging
|
||||
|
||||
from plinth import actions
|
||||
from plinth import cfg
|
||||
import plinth.service as service_module
|
||||
|
||||
|
||||
LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def init():
|
||||
"""Initailze firewall module"""
|
||||
menu = cfg.main_menu.get('system:index')
|
||||
menu.add_urlname(_('Firewall'), 'icon-flag', 'firewall:index', 50)
|
||||
|
||||
service_module.ENABLED.connect(on_service_enabled)
|
||||
|
||||
|
||||
@login_required
|
||||
def index(request):
|
||||
"""Serve introcution page"""
|
||||
if not get_installed_status():
|
||||
return TemplateResponse(request, 'firewall.html',
|
||||
{'title': _('Firewall'),
|
||||
'firewall_status': 'not_installed'})
|
||||
|
||||
if not get_enabled_status():
|
||||
return TemplateResponse(request, 'firewall.html',
|
||||
{'title': _('Firewall'),
|
||||
'firewall_status': 'not_running'})
|
||||
|
||||
internal_enabled_services = get_enabled_services(zone='internal')
|
||||
external_enabled_services = get_enabled_services(zone='external')
|
||||
|
||||
return TemplateResponse(
|
||||
request, 'firewall.html',
|
||||
{'title': _('Firewall'),
|
||||
'services': service_module.SERVICES.values(),
|
||||
'internal_enabled_services': internal_enabled_services,
|
||||
'external_enabled_services': external_enabled_services})
|
||||
|
||||
|
||||
def get_installed_status():
|
||||
"""Return whether firewall is installed"""
|
||||
output = _run(['get-installed'], superuser=True)
|
||||
return output.split()[0] == 'installed'
|
||||
|
||||
|
||||
def get_enabled_status():
|
||||
"""Return whether firewall is installed"""
|
||||
output = _run(['get-status'], superuser=True)
|
||||
return output.split()[0] == 'running'
|
||||
|
||||
|
||||
def get_enabled_services(zone):
|
||||
"""Return the status of various services currently enabled"""
|
||||
output = _run(['get-enabled-services', '--zone', zone], superuser=True)
|
||||
return output.split()
|
||||
|
||||
|
||||
def add_service(port, zone):
|
||||
"""Enable a service in firewall"""
|
||||
_run(['add-service', port, '--zone', zone], superuser=True)
|
||||
|
||||
|
||||
def remove_service(port, zone):
|
||||
"""Remove a service in firewall"""
|
||||
_run(['remove-service', port, '--zone', zone], superuser=True)
|
||||
|
||||
|
||||
def on_service_enabled(sender, service_id, enabled, **kwargs):
|
||||
"""
|
||||
Enable/disable firewall ports when a service is
|
||||
enabled/disabled.
|
||||
"""
|
||||
del sender # Unused
|
||||
del kwargs # Unused
|
||||
|
||||
internal_enabled_services = get_enabled_services(zone='internal')
|
||||
external_enabled_services = get_enabled_services(zone='external')
|
||||
|
||||
LOGGER.info('Service enabled - %s, %s', service_id, enabled)
|
||||
service = service_module.SERVICES[service_id]
|
||||
for port in service.ports:
|
||||
if enabled:
|
||||
if port not in internal_enabled_services:
|
||||
add_service(port, zone='internal')
|
||||
|
||||
if (service.is_external and
|
||||
port not in external_enabled_services):
|
||||
add_service(port, zone='external')
|
||||
else:
|
||||
# service already configured.
|
||||
pass
|
||||
else:
|
||||
if port in internal_enabled_services:
|
||||
enabled_services_on_port = [
|
||||
service_.is_enabled()
|
||||
for service_ in service_module.SERVICES.values()
|
||||
if port in service_.ports and
|
||||
service_id != service_.service_id]
|
||||
if not any(enabled_services_on_port):
|
||||
remove_service(port, zone='internal')
|
||||
|
||||
if port in external_enabled_services:
|
||||
enabled_services_on_port = [
|
||||
service_.is_enabled()
|
||||
for service_ in service_module.SERVICES.values()
|
||||
if port in service_.ports and
|
||||
service_id != service_.service_id and
|
||||
service_.is_external]
|
||||
if not any(enabled_services_on_port):
|
||||
remove_service(port, zone='external')
|
||||
|
||||
|
||||
def _run(arguments, superuser=False):
|
||||
"""Run an given command and raise exception if there was an error"""
|
||||
command = 'firewall'
|
||||
|
||||
if superuser:
|
||||
return actions.superuser_run(command, arguments)
|
||||
else:
|
||||
return actions.run(command, arguments)
|
||||
@@ -0,0 +1,94 @@
|
||||
{% 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 <http://www.gnu.org/licenses/>.
|
||||
#
|
||||
{% endcomment %}
|
||||
|
||||
{% block main_block %}
|
||||
|
||||
<p>Firewall is a network security system that controls the incoming
|
||||
and outgoing network traffic on your {{ cfg.box_name }}. Keeping a
|
||||
firewall enabled and properly configured reduces risk of security
|
||||
threat from the Internet.</p>
|
||||
|
||||
<p>The following is the current status:</p>
|
||||
|
||||
{% if firewall_status = 'not_installed' %}
|
||||
<p>Firewall is not installed. Please install it. Firewall comes
|
||||
pre-installed with {{ cfg.box_name }}. On any Debian based system
|
||||
(such as {{ cfg.box_name }}) you may install it using the command 'aptitude
|
||||
install firewalld'</p>
|
||||
|
||||
{% elif firewall_status = 'not_running' %}
|
||||
|
||||
<p>Firewall daemon is not running. Please run it. Firewall comes
|
||||
enabled by default on {{ cfg.box_name }}. On any Debian based system
|
||||
(such as {{ cfg.box_name }}) you may run it using the command 'service
|
||||
firewalld start' or in case of a system with systemd 'systemctl start
|
||||
firewalld'</p>
|
||||
|
||||
{% else %}
|
||||
|
||||
<table class='table table-autowidth'>
|
||||
<thead>
|
||||
<th>Service/Port</th>
|
||||
<th>Status</th>
|
||||
</thead>
|
||||
|
||||
<tbody>
|
||||
{% for service in services %}
|
||||
<tr>
|
||||
<td><strong>{{ service.name }}</strong></td>
|
||||
<td>
|
||||
{% if service.is_enabled %}
|
||||
<span class='label label-success'>Enabled</span>
|
||||
{% else %}
|
||||
<span class='label label-important'>Disabled</span>
|
||||
{% endif %}
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
{% for port in service.ports %}
|
||||
<tr>
|
||||
<td class='cell-indented'><em>{{ port }}</em></td>
|
||||
<td>
|
||||
{% if port in internal_enabled_services and port in external_enabled_services %}
|
||||
<span class='label label-success'>Permitted</span>
|
||||
{% elif port in internal_enabled_services %}
|
||||
<span class='label label-warning'>Permitted (internal only)</span>
|
||||
{% elif port in external_enabled_services %}
|
||||
<span class='label label-warning'>Permitted (external only)</span>
|
||||
{% else %}
|
||||
<span class='label label-important'>Blocked</span>
|
||||
{% endif %}
|
||||
</td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
|
||||
{% endfor %}
|
||||
|
||||
</tbody>
|
||||
|
||||
</table>
|
||||
|
||||
<p><em>The operation of the firewall is automatic. When you enable a
|
||||
service it is automatically permitted in the firewall and you disable
|
||||
a service is automatically disabled in the firewall.</em></p>
|
||||
|
||||
{% endif %}
|
||||
|
||||
{% endblock %}
|
||||
@@ -0,0 +1,28 @@
|
||||
#
|
||||
# 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 <http://www.gnu.org/licenses/>.
|
||||
#
|
||||
|
||||
"""
|
||||
URLs for the Firewall module
|
||||
"""
|
||||
|
||||
from django.conf.urls import patterns, url
|
||||
|
||||
|
||||
urlpatterns = patterns( # pylint: disable-msg=C0103
|
||||
'plinth.modules.firewall.firewall',
|
||||
url(r'^sys/firewall/$', 'index', name='index')
|
||||
)
|
||||
Reference in new issue
Block a user