diff --git a/lib/Virtualmin/Config/LAMP.pm b/lib/Virtualmin/Config/LAMP.pm index b99aa23..fec1e79 100644 --- a/lib/Virtualmin/Config/LAMP.pm +++ b/lib/Virtualmin/Config/LAMP.pm @@ -17,22 +17,24 @@ sub plugins { # Modern system with systemd? if (-x "/usr/bin/systemctl" || -x "/bin/systemctl") { return [ - "Webmin", "Apache", "Bind", "Dovecot", - "Net", "AWStats", "Postfix", "MySQL", - "Firewalld", "Procmail", "ProFTPd", "Quotas", - "SASL", "Shells", "Status", "Upgrade", - "Usermin", "Webalizer", "Virtualmin", - "ClamAV", "NTP", "SpamAssassin", "Fail2ban" + "Webmin", "Apache", "Bind", + "Dovecot", "Net", "AWStats", + "Postfix", "MySQL", "Firewalld", + "Procmail", "ProFTPd", "Quotas", + "SASL", "Shells", "Status", + "Upgrade", "Usermin", "Webalizer", + "Virtualmin", "ClamAV", "NTP", + "SpamAssassin", "Fail2ban-Firewalld" ]; } else { return [ - "Webmin", "Apache", "Bind", "Dovecot", - "Net", "AWStats", "Postfix", "MySQL", - "Firewall", "Procmail", "ProFTPd", "Quotas", - "SASL", "Shells", "Status", "Upgrade", - "Usermin", "Webalizer", "Virtualmin", - "ClamAV", "NTP", "SpamAssassin", "Fail2ban" + "Webmin", "Apache", "Bind", "Dovecot", + "Net", "AWStats", "Postfix", "MySQL", + "Firewall", "Procmail", "ProFTPd", "Quotas", + "SASL", "Shells", "Status", "Upgrade", + "Usermin", "Webalizer", "Virtualmin", "ClamAV", + "NTP", "SpamAssassin", "Fail2ban" ]; } } diff --git a/lib/Virtualmin/Config/Plugin/Fail2ban-Firewalld.pm b/lib/Virtualmin/Config/Plugin/Fail2ban-Firewalld.pm new file mode 100644 index 0000000..9483b36 --- /dev/null +++ b/lib/Virtualmin/Config/Plugin/Fail2ban-Firewalld.pm @@ -0,0 +1,139 @@ +package Virtualmin::Config::Plugin::Fail2ban; + +# Enables fail2ban and sets up a reasonable set of rules. +# This is currently identical to Fail2ban, with a different depends. +# We could make the dependency resolution in Config smarter to re-merge it +# back to one file. This will do for now. +use strict; +use warnings; +no warnings qw(once); +use parent 'Virtualmin::Config::Plugin'; + +our $config_directory; +our (%gconfig, %miniserv); +our $trust_unknown_referers = 1; + +sub new { + my $class = shift; + + # inherit from Plugin + my $self = $class->SUPER::new(name => 'Fail2ban', depends => 'Firewalld'); + + return $self; +} + +# actions method performs whatever configuration is needed for this +# plugin. XXX Needs to make a backup so changes can be reverted. +sub actions { + my $self = shift; + my $err; + + # XXX Webmin boilerplate. + use Cwd; + my $cwd = getcwd(); + my $root = $self->root(); + chdir($root); + $0 = "$root/virtual-server/config-system.pl"; + push(@INC, $root); + eval 'use WebminCore'; ## no critic + init_config(); + + # End of Webmin boilerplate. + + $self->spin(); + eval { + foreign_require('init', 'init-lib.pl'); + init::enable_at_boot('fail2ban'); + + if (has_command('fail2ban-server')) { + + # Create a jail.local with some basic config + create_fail2ban_jail(); + create_fail2ban_firewalld(); + } + + init::restart_action('fail2ban'); + $self->done(1); + }; + if ($@) { + $self->done(0); # NOK! + } +} + +sub create_fail2ban_jail { + if (-e "/etc/fail2ban/jail.local") { + die "Fail2ban already has local configuration. Will not overwrite."; + } + open(my $JAIL_LOCAL, '>', '/etc/fail2ban/jail.local'); + print $JAIL_LOCAL <', '/etc/fail2ban/jail.d/00-firewalld.conf'); + print $FIREWALLD_CONF <SUPER::new(name => 'Fail2ban'); + my $self = $class->SUPER::new(name => 'Fail2ban', depends => 'Firewall'); return $self; } @@ -33,6 +34,7 @@ sub actions { push(@INC, $root); eval 'use WebminCore'; ## no critic init_config(); + # End of Webmin boilerplate. $self->spin(); @@ -41,6 +43,7 @@ sub actions { init::enable_at_boot('fail2ban'); if (has_command('fail2ban-server')) { + # Create a jail.local with some basic config create_fail2ban_jail(); create_fail2ban_firewalld(); @@ -59,7 +62,7 @@ sub create_fail2ban_jail { die "Fail2ban already has local configuration. Will not overwrite."; } open(my $JAIL_LOCAL, '>', '/etc/fail2ban/jail.local'); -print $JAIL_LOCAL <', '/etc/fail2ban/jail.d/00-firewalld.conf'); + open(my $FIREWALLD_CONF, '>', '/etc/fail2ban/jail.d/00-firewalld.conf'); print $FIREWALLD_CONF <