2007-03-15 22:55:22 +01:00
|
|
|
#
|
2007-12-01 18:45:43 +01:00
|
|
|
# Shorewall-perl 4.1 -- /usr/share/shorewall-perl/Shorewall/Policy.pm
|
2007-03-15 22:55:22 +01:00
|
|
|
#
|
2007-09-08 18:09:51 +02:00
|
|
|
# This program is under GPL [http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt]
|
2007-03-15 22:55:22 +01:00
|
|
|
#
|
|
|
|
# (c) 2007 - Tom Eastep (teastep@shorewall.net)
|
|
|
|
#
|
|
|
|
# Complete documentation is available at http://shorewall.net
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of Version 2 of the GNU General Public License
|
|
|
|
# as published by the Free Software Foundation.
|
|
|
|
#
|
|
|
|
# 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 General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program; if not, write to the Free Software
|
2007-09-08 18:09:51 +02:00
|
|
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2007-03-15 22:55:22 +01:00
|
|
|
#
|
2007-04-19 01:55:25 +02:00
|
|
|
# This module deals with the /etc/shorewall/policy file.
|
2007-03-15 22:55:22 +01:00
|
|
|
#
|
2007-03-15 03:18:29 +01:00
|
|
|
package Shorewall::Policy;
|
|
|
|
require Exporter;
|
2007-11-16 00:24:54 +01:00
|
|
|
use Shorewall::Config qw(:DEFAULT :internal);
|
2007-03-15 03:18:29 +01:00
|
|
|
use Shorewall::Zones;
|
2007-11-16 00:24:54 +01:00
|
|
|
use Shorewall::Chains qw( :DEFAULT :internal) ;
|
2007-03-15 03:18:29 +01:00
|
|
|
use Shorewall::Actions;
|
|
|
|
|
|
|
|
use strict;
|
|
|
|
|
|
|
|
our @ISA = qw(Exporter);
|
2007-07-19 20:36:04 +02:00
|
|
|
our @EXPORT = qw( validate_policy apply_policy_rules complete_standard_chain sub setup_syn_flood_chains );
|
2007-03-15 03:18:29 +01:00
|
|
|
our @EXPORT_OK = qw( );
|
2007-11-21 19:48:23 +01:00
|
|
|
our $VERSION = 4.1.1;
|
2007-09-10 17:52:57 +02:00
|
|
|
|
|
|
|
# @policy_chains is a list of references to policy chains in the filter table
|
|
|
|
|
|
|
|
our @policy_chains;
|
|
|
|
|
|
|
|
#
|
|
|
|
# Initialize globals -- we take this novel approach to globals initialization to allow
|
|
|
|
# the compiler to run multiple times in the same process. The
|
|
|
|
# initialize() function does globals initialization for this
|
|
|
|
# module and is called from an INIT block below. The function is
|
|
|
|
# also called by Shorewall::Compiler::compiler at the beginning of
|
|
|
|
# the second and subsequent calls to that function.
|
|
|
|
#
|
|
|
|
|
|
|
|
sub initialize() {
|
|
|
|
@policy_chains = ();
|
|
|
|
}
|
|
|
|
|
|
|
|
INIT {
|
|
|
|
initialize;
|
|
|
|
}
|
2007-03-15 03:18:29 +01:00
|
|
|
|
|
|
|
#
|
2007-08-26 17:12:04 +02:00
|
|
|
# Convert a chain into a policy chain.
|
2007-03-15 03:18:29 +01:00
|
|
|
#
|
2007-08-26 17:12:04 +02:00
|
|
|
sub convert_to_policy_chain($$$$$)
|
2007-03-15 03:18:29 +01:00
|
|
|
{
|
2007-08-26 17:12:04 +02:00
|
|
|
my ($chainref, $source, $dest, $policy, $optional ) = @_;
|
2007-03-27 01:17:46 +02:00
|
|
|
|
2007-03-15 03:18:29 +01:00
|
|
|
$chainref->{is_policy} = 1;
|
|
|
|
$chainref->{policy} = $policy;
|
|
|
|
$chainref->{is_optional} = $optional;
|
2007-08-26 17:12:04 +02:00
|
|
|
$chainref->{policychain} = $chainref->{name};
|
|
|
|
$chainref->{policypair} = [ $source, $dest ];
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Create a new policy chain and return a reference to it.
|
|
|
|
#
|
|
|
|
sub new_policy_chain($$$$)
|
|
|
|
{
|
|
|
|
my ($source, $dest, $policy, $optional) = @_;
|
|
|
|
|
2007-09-12 17:03:47 +02:00
|
|
|
my $chainref = new_chain( 'filter', "${source}2${dest}" );
|
2007-08-26 17:12:04 +02:00
|
|
|
|
|
|
|
convert_to_policy_chain( $chainref, $source, $dest, $policy, $optional );
|
2007-06-14 01:02:39 +02:00
|
|
|
|
|
|
|
$chainref;
|
2007-03-15 03:18:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Set the passed chain's policychain and policy to the passed values.
|
|
|
|
#
|
2007-08-26 17:12:04 +02:00
|
|
|
sub set_policy_chain($$$$$)
|
2007-03-15 03:18:29 +01:00
|
|
|
{
|
2007-08-26 17:12:04 +02:00
|
|
|
my ($source, $dest, $chain1, $chainref, $policy ) = @_;
|
2007-03-15 03:18:29 +01:00
|
|
|
|
2007-09-12 17:03:47 +02:00
|
|
|
my $chainref1 = $filter_table->{$chain1};
|
2007-07-19 20:36:04 +02:00
|
|
|
|
2007-09-12 17:03:47 +02:00
|
|
|
$chainref1 = new_chain 'filter', $chain1 unless $chainref1;
|
2007-07-19 20:36:04 +02:00
|
|
|
|
2007-03-15 03:18:29 +01:00
|
|
|
unless ( $chainref1->{policychain} ) {
|
2007-07-19 20:36:04 +02:00
|
|
|
if ( $config{EXPAND_POLICIES} ) {
|
|
|
|
#
|
|
|
|
# We convert the canonical chain into a policy chain, using the settings of the
|
|
|
|
# passed policy chain.
|
|
|
|
#
|
|
|
|
$chainref1->{policychain} = $chain1;
|
|
|
|
$chainref1->{loglevel} = $chainref->{loglevel} if defined $chainref->{loglevel};
|
|
|
|
|
|
|
|
if ( defined $chainref->{synparams} ) {
|
|
|
|
$chainref1->{synparams} = $chainref->{synparams};
|
|
|
|
$chainref1->{synchain} = $chainref->{synchain};
|
|
|
|
}
|
|
|
|
|
|
|
|
$chainref1->{default} = $chainref->{default} if defined $chainref->{default};
|
|
|
|
$chainref1->{is_policy} = 1;
|
|
|
|
push @policy_chains, $chainref1;
|
|
|
|
} else {
|
|
|
|
$chainref1->{policychain} = $chainref->{name};
|
|
|
|
}
|
|
|
|
|
2007-03-15 03:18:29 +01:00
|
|
|
$chainref1->{policy} = $policy;
|
2007-08-26 17:12:04 +02:00
|
|
|
$chainref1->{policypair} = [ $source, $dest ];
|
2007-03-15 03:18:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Process the policy file
|
|
|
|
#
|
2007-09-10 17:52:57 +02:00
|
|
|
use constant { OPTIONAL => 1 };
|
|
|
|
|
|
|
|
sub add_or_modify_policy_chain( $$ ) {
|
|
|
|
my ( $zone, $zone1 ) = @_;
|
|
|
|
my $chain = "${zone}2${zone1}";
|
2007-09-12 17:03:47 +02:00
|
|
|
my $chainref = $filter_table->{$chain};
|
2007-09-10 17:52:57 +02:00
|
|
|
|
|
|
|
if ( $chainref ) {
|
|
|
|
unless( $chainref->{is_policy} ) {
|
|
|
|
convert_to_policy_chain( $chainref, $zone, $zone1, 'CONTINUE', OPTIONAL );
|
|
|
|
push @policy_chains, $chainref;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
push @policy_chains, ( new_policy_chain $zone, $zone1, 'CONTINUE', OPTIONAL );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sub print_policy($$$$) {
|
|
|
|
my ( $source, $dest, $policy , $chain ) = @_;
|
|
|
|
unless ( ( $source eq 'all' ) || ( $dest eq 'all' ) ) {
|
|
|
|
if ( $policy eq 'CONTINUE' ) {
|
|
|
|
my ( $sourceref, $destref ) = ( find_zone($source) ,find_zone( $dest ) );
|
|
|
|
warning_message "CONTINUE policy between two un-nested zones ($source, $dest)" if ! ( @{$sourceref->{parents}} || @{$destref->{parents}} );
|
2007-08-26 17:12:04 +02:00
|
|
|
}
|
2007-09-10 17:52:57 +02:00
|
|
|
progress_message " Policy for $source to $dest is $policy using chain $chain" unless $source eq $dest;
|
2007-03-15 03:18:29 +01:00
|
|
|
}
|
2007-09-10 17:52:57 +02:00
|
|
|
}
|
2007-03-15 03:18:29 +01:00
|
|
|
|
2007-09-10 17:52:57 +02:00
|
|
|
sub validate_policy()
|
|
|
|
{
|
2007-04-08 16:42:26 +02:00
|
|
|
my %validpolicies = (
|
2007-03-15 03:18:29 +01:00
|
|
|
ACCEPT => undef,
|
|
|
|
REJECT => undef,
|
|
|
|
DROP => undef,
|
|
|
|
CONTINUE => undef,
|
|
|
|
QUEUE => undef,
|
2007-08-26 17:12:04 +02:00
|
|
|
NFQUEUE => undef,
|
2007-03-15 03:18:29 +01:00
|
|
|
NONE => undef
|
|
|
|
);
|
2007-03-27 01:17:46 +02:00
|
|
|
|
2007-08-26 17:12:04 +02:00
|
|
|
my %map = ( DROP_DEFAULT => 'DROP' ,
|
|
|
|
REJECT_DEFAULT => 'REJECT' ,
|
|
|
|
ACCEPT_DEFAULT => 'ACCEPT' ,
|
|
|
|
QUEUE_DEFAULT => 'QUEUE' ,
|
|
|
|
NFQUEUE_DEFAULT => 'NFQUEUE' );
|
2007-03-27 01:17:46 +02:00
|
|
|
|
2007-03-15 03:18:29 +01:00
|
|
|
my $zone;
|
2008-04-09 08:00:08 +02:00
|
|
|
my @zonelist = $config{EXPAND_POLICIES} ? all_zones : ( all_zones, 'all' );
|
2007-03-15 03:18:29 +01:00
|
|
|
|
2007-08-26 17:12:04 +02:00
|
|
|
for my $option qw/DROP_DEFAULT REJECT_DEFAULT ACCEPT_DEFAULT QUEUE_DEFAULT NFQUEUE_DEFAULT/ {
|
2007-03-15 03:18:29 +01:00
|
|
|
my $action = $config{$option};
|
|
|
|
next if $action eq 'none';
|
|
|
|
my $actiontype = $targets{$action};
|
2007-03-27 01:17:46 +02:00
|
|
|
|
2007-03-15 03:18:29 +01:00
|
|
|
if ( defined $actiontype ) {
|
|
|
|
fatal_error "Invalid setting ($action) for $option" unless $actiontype & ACTION;
|
|
|
|
} else {
|
|
|
|
fatal_error "Default Action $option=$action not found";
|
|
|
|
}
|
|
|
|
|
|
|
|
unless ( $usedactions{$action} ) {
|
|
|
|
$usedactions{$action} = 1;
|
|
|
|
createactionchain $action;
|
|
|
|
}
|
|
|
|
|
|
|
|
$default_actions{$map{$option}} = $action;
|
|
|
|
}
|
2007-03-27 01:17:46 +02:00
|
|
|
|
2007-09-10 17:52:57 +02:00
|
|
|
for $zone ( all_zones ) {
|
2007-08-26 17:12:04 +02:00
|
|
|
push @policy_chains, ( new_policy_chain $zone, $zone, 'ACCEPT', OPTIONAL );
|
2007-03-15 03:18:29 +01:00
|
|
|
|
2007-09-10 17:52:57 +02:00
|
|
|
if ( $config{IMPLICIT_CONTINUE} && ( @{find_zone( $zone )->{parents}} ) ) {
|
|
|
|
for my $zone1 ( all_zones ) {
|
|
|
|
unless( $zone eq $zone1 ) {
|
|
|
|
add_or_modify_policy_chain( $zone, $zone1 );
|
|
|
|
add_or_modify_policy_chain( $zone1, $zone );
|
|
|
|
}
|
2007-03-15 03:18:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-30 17:57:08 +02:00
|
|
|
my $fn = open_file 'policy';
|
|
|
|
|
2007-11-16 00:24:54 +01:00
|
|
|
first_entry "$doing $fn...";
|
2007-03-15 03:18:29 +01:00
|
|
|
|
2007-03-29 19:02:13 +02:00
|
|
|
while ( read_a_line ) {
|
2007-03-15 03:18:29 +01:00
|
|
|
|
2008-03-26 16:36:23 +01:00
|
|
|
my ( $client, $server, $originalpolicy, $loglevel, $synparams ) = split_line 3, 5, 'policy file';
|
2007-05-09 21:05:27 +02:00
|
|
|
|
2007-03-15 03:18:29 +01:00
|
|
|
$loglevel = '' if $loglevel eq '-';
|
|
|
|
$synparams = '' if $synparams eq '-';
|
2007-03-27 01:17:46 +02:00
|
|
|
|
2007-03-15 03:18:29 +01:00
|
|
|
my $clientwild = ( "\L$client" eq 'all' );
|
|
|
|
|
2008-04-10 00:56:23 +02:00
|
|
|
fatal_error "Undefined zone ($client)" unless $clientwild || defined_zone( $client );
|
2007-03-15 03:18:29 +01:00
|
|
|
|
|
|
|
my $serverwild = ( "\L$server" eq 'all' );
|
|
|
|
|
2008-04-10 00:56:23 +02:00
|
|
|
fatal_error "Undefined zone ($server)" unless $serverwild || defined_zone( $server );
|
2007-03-15 03:18:29 +01:00
|
|
|
|
2008-03-26 16:36:23 +01:00
|
|
|
my ( $policy, $default, $remainder ) = split( /:/, $originalpolicy, 3 );
|
2007-05-17 16:17:29 +02:00
|
|
|
|
2008-03-26 16:15:34 +01:00
|
|
|
fatal_error "Invalid or missing POLICY ($originalpolicy)" unless $policy;
|
|
|
|
|
2007-05-17 16:17:29 +02:00
|
|
|
fatal_error "Invalid default action ($default:$remainder)" if defined $remainder;
|
2007-03-15 03:18:29 +01:00
|
|
|
|
2007-11-20 17:01:27 +01:00
|
|
|
( $policy , my $queue ) = get_target_param $policy;
|
2007-08-26 17:12:04 +02:00
|
|
|
|
2007-04-30 00:29:10 +02:00
|
|
|
if ( $default ) {
|
|
|
|
if ( "\L$default" eq 'none' ) {
|
|
|
|
$default = 'none';
|
2007-03-15 03:18:29 +01:00
|
|
|
} else {
|
2007-04-30 00:29:10 +02:00
|
|
|
my $defaulttype = $targets{$default} || 0;
|
|
|
|
|
|
|
|
if ( $defaulttype & ACTION ) {
|
|
|
|
unless ( $usedactions{$default} ) {
|
|
|
|
$usedactions{$default} = 1;
|
|
|
|
createactionchain $default;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fatal_error "Unknown Default Action ($default)";
|
|
|
|
}
|
2007-03-27 01:17:46 +02:00
|
|
|
}
|
2007-03-15 03:18:29 +01:00
|
|
|
} else {
|
|
|
|
$default = $default_actions{$policy} || '';
|
|
|
|
}
|
|
|
|
|
2008-04-10 00:56:23 +02:00
|
|
|
fatal_error "Invalid policy ($policy)" unless exists $validpolicies{$policy};
|
2007-03-15 03:18:29 +01:00
|
|
|
|
2007-08-26 17:12:04 +02:00
|
|
|
if ( defined $queue ) {
|
2008-04-10 00:56:23 +02:00
|
|
|
fatal_error "Invalid policy ($policy($queue))" unless $policy eq 'NFQUEUE';
|
2007-08-26 17:12:04 +02:00
|
|
|
require_capability( 'NFQUEUE_TARGET', 'An NFQUEUE Policy', 's' );
|
2007-12-07 20:06:01 +01:00
|
|
|
my $queuenum = numeric_value( $queue );
|
|
|
|
fatal_error "Invalid NFQUEUE queue number ($queue)" unless defined( $queuenum) && $queuenum <= 65535;
|
|
|
|
$policy = "NFQUEUE --queue-num $queuenum";
|
2007-08-26 17:12:04 +02:00
|
|
|
} elsif ( $policy eq 'NONE' ) {
|
2007-08-09 17:16:08 +02:00
|
|
|
fatal_error "NONE policy not allowed with \"all\""
|
2007-03-15 03:18:29 +01:00
|
|
|
if $clientwild || $serverwild;
|
2007-08-09 17:16:08 +02:00
|
|
|
fatal_error "NONE policy not allowed to/from firewall zone"
|
2007-09-12 17:03:47 +02:00
|
|
|
if ( zone_type( $client ) eq 'firewall' ) || ( zone_type( $server ) eq 'firewall' );
|
2007-03-15 03:18:29 +01:00
|
|
|
}
|
2007-03-27 01:17:46 +02:00
|
|
|
|
2007-06-07 01:40:40 +02:00
|
|
|
unless ( $clientwild || $serverwild ) {
|
2007-09-12 17:03:47 +02:00
|
|
|
if ( zone_type( $server ) eq 'bport4' ) {
|
2007-07-26 20:36:18 +02:00
|
|
|
fatal_error "Invalid policy - DEST zone is a Bridge Port zone but the SOURCE zone is not associated with the same bridge"
|
2007-09-10 17:52:57 +02:00
|
|
|
unless find_zone( $client )->{bridge} eq find_zone( $server)->{bridge} || single_interface( $client ) eq find_zone( $server )->{bridge};
|
2007-06-07 01:40:40 +02:00
|
|
|
}
|
2007-07-26 20:36:18 +02:00
|
|
|
}
|
2007-06-07 01:40:40 +02:00
|
|
|
|
2007-03-15 03:18:29 +01:00
|
|
|
my $chain = "${client}2${server}";
|
|
|
|
my $chainref;
|
|
|
|
|
2007-09-12 17:03:47 +02:00
|
|
|
if ( defined $filter_table->{$chain} ) {
|
|
|
|
$chainref = $filter_table->{$chain};
|
2007-03-27 01:17:46 +02:00
|
|
|
|
2007-03-15 03:18:29 +01:00
|
|
|
if ( $chainref->{is_policy} ) {
|
|
|
|
if ( $chainref->{is_optional} ) {
|
|
|
|
$chainref->{is_optional} = 0;
|
2007-04-23 00:33:37 +02:00
|
|
|
$chainref->{policy} = $policy;
|
2007-03-15 03:18:29 +01:00
|
|
|
} else {
|
2007-08-26 17:12:04 +02:00
|
|
|
fatal_error qq(Policy "$client $server $policy" duplicates earlier policy "@{$chainref->{policypair}} $chainref->{policy}");
|
2007-03-15 03:18:29 +01:00
|
|
|
}
|
2007-07-27 06:11:44 +02:00
|
|
|
} elsif ( $chainref->{policy} ) {
|
2007-08-26 17:12:04 +02:00
|
|
|
fatal_error qq(Policy "$client $server $policy" duplicates earlier policy "@{$chainref->{policypair}} $chainref->{policy}");
|
2007-03-15 03:18:29 +01:00
|
|
|
} else {
|
2007-08-26 17:12:04 +02:00
|
|
|
convert_to_policy_chain( $chainref, $client, $server, $policy, 0 );
|
2007-07-19 20:36:04 +02:00
|
|
|
push @policy_chains, ( $chainref ) unless $config{EXPAND_POLICIES} && ( $clientwild || $serverwild );
|
2007-03-15 03:18:29 +01:00
|
|
|
}
|
|
|
|
} else {
|
2007-08-26 17:12:04 +02:00
|
|
|
$chainref = new_policy_chain $client, $server, $policy, 0;
|
2007-07-19 20:36:04 +02:00
|
|
|
push @policy_chains, ( $chainref ) unless $config{EXPAND_POLICIES} && ( $clientwild || $serverwild );
|
2007-03-15 03:18:29 +01:00
|
|
|
}
|
|
|
|
|
2007-07-19 20:36:04 +02:00
|
|
|
$chainref->{loglevel} = validate_level( $loglevel ) if defined $loglevel && $loglevel ne '';
|
2007-07-26 20:36:18 +02:00
|
|
|
|
2007-07-19 20:36:04 +02:00
|
|
|
if ( $synparams ne '' ) {
|
|
|
|
$chainref->{synparams} = do_ratelimit $synparams, 'ACCEPT';
|
2007-07-26 20:36:18 +02:00
|
|
|
$chainref->{synchain} = $chain
|
2007-07-19 20:36:04 +02:00
|
|
|
}
|
2007-07-26 20:36:18 +02:00
|
|
|
|
2007-07-19 20:36:04 +02:00
|
|
|
$chainref->{default} = $default if $default;
|
2007-03-15 03:18:29 +01:00
|
|
|
|
|
|
|
if ( $clientwild ) {
|
|
|
|
if ( $serverwild ) {
|
2008-04-09 08:00:08 +02:00
|
|
|
for my $zone ( @zonelist ) {
|
|
|
|
for my $zone1 ( @zonelist ) {
|
2007-08-26 17:12:04 +02:00
|
|
|
set_policy_chain $client, $server, "${zone}2${zone1}", $chainref, $policy;
|
2007-03-15 03:18:29 +01:00
|
|
|
print_policy $zone, $zone1, $policy, $chain;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2007-09-10 17:52:57 +02:00
|
|
|
for my $zone ( all_zones ) {
|
2007-08-26 17:12:04 +02:00
|
|
|
set_policy_chain $client, $server, "${zone}2${server}", $chainref, $policy;
|
2007-03-15 03:18:29 +01:00
|
|
|
print_policy $zone, $server, $policy, $chain;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} elsif ( $serverwild ) {
|
2008-04-09 08:00:08 +02:00
|
|
|
for my $zone ( @zonelist ) {
|
2007-08-26 17:12:04 +02:00
|
|
|
set_policy_chain $client, $server, "${client}2${zone}", $chainref, $policy;
|
2007-03-15 03:18:29 +01:00
|
|
|
print_policy $client, $zone, $policy, $chain;
|
|
|
|
}
|
2007-03-27 01:17:46 +02:00
|
|
|
|
2007-03-15 03:18:29 +01:00
|
|
|
} else {
|
|
|
|
print_policy $client, $server, $policy, $chain;
|
|
|
|
}
|
|
|
|
}
|
2007-10-19 21:43:14 +02:00
|
|
|
|
|
|
|
for $zone ( all_zones ) {
|
|
|
|
for my $zone1 ( all_zones ) {
|
|
|
|
fatal_error "No policy defined from zone $zone to zone $zone1" unless $filter_table->{"${zone}2${zone1}"}{policy};
|
|
|
|
}
|
|
|
|
}
|
2007-03-15 03:18:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Policy Rule application
|
|
|
|
#
|
2007-09-29 00:26:29 +02:00
|
|
|
sub policy_rules( $$$$$ ) {
|
|
|
|
my ( $chainref , $target, $loglevel, $default, $dropmulticast ) = @_;
|
2007-07-26 20:36:18 +02:00
|
|
|
|
2007-06-06 15:49:30 +02:00
|
|
|
unless ( $target eq 'NONE' ) {
|
2007-09-29 00:26:29 +02:00
|
|
|
add_rule $chainref, "-d 224.0.0.0/24 -j RETURN" if $dropmulticast && $target ne 'CONTINUE';
|
2007-06-06 15:49:30 +02:00
|
|
|
add_rule $chainref, "-j $default" if $default && $default ne 'none';
|
|
|
|
log_rule $loglevel , $chainref , $target , '' if $loglevel ne '';
|
|
|
|
fatal_error "Null target in policy_rules()" unless $target;
|
2007-11-20 21:47:19 +01:00
|
|
|
$target = 'reject' if $target eq 'REJECT';
|
2007-08-26 17:12:04 +02:00
|
|
|
|
|
|
|
add_rule( $chainref , "-j $target" ) unless $target eq 'CONTINUE';
|
2007-06-06 15:49:30 +02:00
|
|
|
}
|
2007-03-15 03:18:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sub report_syn_flood_protection() {
|
|
|
|
progress_message ' Enabled SYN flood protection';
|
|
|
|
}
|
|
|
|
|
|
|
|
sub default_policy( $$$ ) {
|
|
|
|
my $chainref = $_[0];
|
2007-09-12 17:03:47 +02:00
|
|
|
my $policyref = $filter_table->{$chainref->{policychain}};
|
2007-03-15 03:18:29 +01:00
|
|
|
my $synparams = $policyref->{synparams};
|
|
|
|
my $default = $policyref->{default};
|
|
|
|
my $policy = $policyref->{policy};
|
|
|
|
my $loglevel = $policyref->{loglevel};
|
|
|
|
|
2007-10-19 21:43:14 +02:00
|
|
|
fatal_error "Internal error in default_policy()" unless $policyref;
|
2007-03-15 03:18:29 +01:00
|
|
|
|
|
|
|
if ( $chainref eq $policyref ) {
|
2007-09-29 00:26:29 +02:00
|
|
|
policy_rules $chainref , $policy, $loglevel , $default, $config{MULTICAST};
|
2007-03-15 03:18:29 +01:00
|
|
|
} else {
|
2007-08-26 17:12:04 +02:00
|
|
|
if ( $policy eq 'ACCEPT' || $policy eq 'QUEUE' || $policy =~ /^NFQUEUE/ ) {
|
2007-03-15 03:18:29 +01:00
|
|
|
if ( $synparams ) {
|
|
|
|
report_syn_flood_protection;
|
2007-09-29 00:26:29 +02:00
|
|
|
policy_rules $chainref , $policy , $loglevel , $default, $config{MULTICAST};
|
2007-03-15 03:18:29 +01:00
|
|
|
} else {
|
|
|
|
add_rule $chainref, "-j $policyref->{name}";
|
|
|
|
$chainref = $policyref;
|
|
|
|
}
|
|
|
|
} elsif ( $policy eq 'CONTINUE' ) {
|
|
|
|
report_syn_flood_protection if $synparams;
|
2007-09-29 00:26:29 +02:00
|
|
|
policy_rules $chainref , $policy , $loglevel , $default, $config{MULTICAST};
|
2007-03-15 03:18:29 +01:00
|
|
|
} else {
|
|
|
|
report_syn_flood_protection if $synparams;
|
|
|
|
add_rule $chainref , "-j $policyref->{name}";
|
|
|
|
$chainref = $policyref;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
progress_message " Policy $policy from $_[1] to $_[2] using chain $chainref->{name}";
|
2007-03-27 01:17:46 +02:00
|
|
|
|
2007-03-15 03:18:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sub apply_policy_rules() {
|
2007-07-11 01:09:33 +02:00
|
|
|
progress_message2 'Applying Policies...';
|
|
|
|
|
2007-03-15 03:18:29 +01:00
|
|
|
for my $chainref ( @policy_chains ) {
|
|
|
|
my $policy = $chainref->{policy};
|
|
|
|
my $loglevel = $chainref->{loglevel};
|
|
|
|
my $optional = $chainref->{is_optional};
|
|
|
|
my $default = $chainref->{default};
|
|
|
|
my $name = $chainref->{name};
|
|
|
|
|
|
|
|
if ( $policy ne 'NONE' ) {
|
|
|
|
if ( ! $chainref->{referenced} && ( ! $optional && $policy ne 'CONTINUE' ) ) {
|
2007-09-12 17:03:47 +02:00
|
|
|
ensure_filter_chain $name, 1;
|
2007-03-15 03:18:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( $name =~ /^all2|2all$/ ) {
|
2007-03-23 00:06:16 +01:00
|
|
|
run_user_exit $chainref;
|
2007-09-29 00:26:29 +02:00
|
|
|
policy_rules $chainref , $policy, $loglevel , $default, $config{MULTICAST};
|
2007-03-15 03:18:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-09-10 17:52:57 +02:00
|
|
|
for my $zone ( all_zones ) {
|
|
|
|
for my $zone1 ( all_zones ) {
|
2007-09-12 17:03:47 +02:00
|
|
|
my $chainref = $filter_table->{"${zone}2${zone1}"};
|
2007-03-23 00:06:16 +01:00
|
|
|
|
|
|
|
if ( $chainref->{referenced} ) {
|
|
|
|
run_user_exit $chainref;
|
|
|
|
default_policy $chainref, $zone, $zone1;
|
|
|
|
}
|
2007-03-15 03:18:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Complete a standard chain
|
|
|
|
#
|
|
|
|
# - run any supplied user exit
|
|
|
|
# - search the policy file for an applicable policy and add rules as
|
|
|
|
# appropriate
|
|
|
|
# - If no applicable policy is found, add rules for an assummed
|
|
|
|
# policy of DROP INFO
|
|
|
|
#
|
|
|
|
sub complete_standard_chain ( $$$ ) {
|
|
|
|
my ( $stdchainref, $zone, $zone2 ) = @_;
|
|
|
|
|
2007-08-26 17:12:04 +02:00
|
|
|
add_rule $stdchainref, '-m state --state ESTABLISHED,RELATED -j ACCEPT' unless $config{FASTACCEPT};
|
|
|
|
|
2007-03-23 00:06:16 +01:00
|
|
|
run_user_exit $stdchainref;
|
|
|
|
|
2007-09-12 17:03:47 +02:00
|
|
|
my $ruleschainref = $filter_table->{"${zone}2${zone2}"};
|
2007-04-24 23:27:40 +02:00
|
|
|
my ( $policy, $loglevel, $default ) = ( 'DROP', 6, $config{DROP_DEFAULT} );
|
2007-03-15 03:18:29 +01:00
|
|
|
my $policychainref;
|
|
|
|
|
2007-09-12 17:03:47 +02:00
|
|
|
$policychainref = $filter_table->{$ruleschainref->{policychain}} if $ruleschainref;
|
2007-03-15 03:18:29 +01:00
|
|
|
|
2007-03-15 04:56:40 +01:00
|
|
|
( $policy, $loglevel, $default ) = @{$policychainref}{'policy', 'loglevel', 'default' } if $policychainref;
|
2007-03-15 03:18:29 +01:00
|
|
|
|
2007-09-29 00:26:29 +02:00
|
|
|
policy_rules $stdchainref , $policy , $loglevel, $default, 0;
|
2007-03-15 03:18:29 +01:00
|
|
|
}
|
|
|
|
|
2007-07-19 20:36:04 +02:00
|
|
|
#
|
|
|
|
# Create and populate the synflood chains corresponding to entries in /etc/shorewall/policy
|
|
|
|
#
|
|
|
|
sub setup_syn_flood_chains() {
|
|
|
|
for my $chainref ( @policy_chains ) {
|
|
|
|
my $limit = $chainref->{synparams};
|
2007-09-12 17:03:47 +02:00
|
|
|
if ( $limit && ! $filter_table->{syn_flood_chain $chainref} ) {
|
2007-07-26 20:36:18 +02:00
|
|
|
my $level = $chainref->{loglevel};
|
2007-09-12 17:03:47 +02:00
|
|
|
my $synchainref = new_chain 'filter' , syn_flood_chain $chainref;
|
2007-07-19 20:36:04 +02:00
|
|
|
add_rule $synchainref , "${limit}-j RETURN";
|
|
|
|
log_rule_limit $level , $synchainref , $chainref->{name} , 'DROP', '-m limit --limit 5/min --limit-burst 5 ' , '' , 'add' , ''
|
|
|
|
if $level ne '';
|
|
|
|
add_rule $synchainref, '-j DROP';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-15 03:18:29 +01:00
|
|
|
1;
|