[#759] Add fail2ban to Plinth UI

Run sudo-required action via actions.superuser_run

Actions related to services those require `sudo` permissions need to
be executed via actions.superuser_run.

NOTE:
If plinth service is started via `sudo ./run --debug` (in dev mode)
all actions will be executed silently. But plinth in user machines
won't be executed with sudo permissions.
This commit is contained in:
nsaikiran authored and James Valleroy committed 2017-07-29 09:42:31 -04:00
1 parent 09f3f23e10
commit a8a5da996e
5 files changed
+42 -2

No files matched your search

+15 -1
View File
@@ -25,12 +25,15 @@ from plinth import actions
from plinth.menu import main_menu
version = 1
version = 2
is_essential = True
title = _('Security')
managed_packages = ['fail2ban']
managed_services = ['fail2ban']
ACCESS_CONF_FILE = '/etc/security/access.conf'
ACCESS_CONF_SNIPPET = '-:ALL EXCEPT root fbx (admin) (sudo):ALL'
@@ -42,6 +45,17 @@ def init():
menu.add_urlname(title, 'glyphicon-lock', 'security:index')
def setup(helper, old_version=None):
"""Install the required packages"""
helper.install(managed_packages)
setup_fail2ban()
def setup_fail2ban():
actions.superuser_run('service', ['unmask', 'fail2ban'])
actions.superuser_run('service', ['enable', 'fail2ban'])
def get_restricted_access_enabled():
"""Return whether restricted access is enabled"""
with open(ACCESS_CONF_FILE, 'r') as conffile:
+6
View File
@@ -31,3 +31,9 @@ class SecurityForm(forms.Form):
'group will be able to log in to console or via SSH. '
'Console users may be able to access some services '
'without further authorization.'))
fail2ban_enabled = forms.BooleanField(
label=_('Fail2ban (recommended)'), required=False,
help_text=_('When this option is enabled, fail2ban will limit brute force '
'break-in attempts to the SSH server and other password protected '
'internet-services which are enabled.'
))
+10 -1
View File
@@ -25,6 +25,8 @@ from django.utils.translation import ugettext as _
from .forms import SecurityForm
from plinth.modules import security
from plinth import actions
from plinth import action_utils
def index(request):
@@ -49,7 +51,8 @@ def index(request):
def get_status(request):
"""Return the current status"""
return {'restricted_access': security.get_restricted_access_enabled()}
return {'restricted_access': security.get_restricted_access_enabled(),
'fail2ban_enabled': action_utils.service_is_enabled('fail2ban')}
def _apply_changes(request, old_status, new_status):
@@ -64,3 +67,9 @@ def _apply_changes(request, old_status, new_status):
.format(exception=exception))
else:
messages.success(request, _('Updated security configuration'))
if old_status['fail2ban_enabled'] != new_status['fail2ban_enabled']:
if new_status['fail2ban_enabled']:
actions.superuser_run('service',['enable','fail2ban'])
else:
actions.superuser_run('service',['disable','fail2ban'])