2009-02-22 22:15:05 +01:00
|
|
|
#
|
2009-06-13 16:07:55 +02:00
|
|
|
# Shorewall 4.4 -- /usr/share/shorewall/Shorewall/Raw.pm
|
2009-02-22 22:15:05 +01:00
|
|
|
#
|
|
|
|
# This program is under GPL [http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt]
|
|
|
|
#
|
2011-05-23 19:06:56 +02:00
|
|
|
# (c) 2009,2010,2011 - Tom Eastep (teastep@shorewall.net)
|
2009-02-22 22:15:05 +01:00
|
|
|
#
|
|
|
|
# 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
|
|
|
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
#
|
2012-08-03 19:53:20 +02:00
|
|
|
# This module contains the code that handles the /etc/shorewall/conntrack file.
|
2009-02-22 22:15:05 +01:00
|
|
|
#
|
|
|
|
package Shorewall::Raw;
|
|
|
|
require Exporter;
|
|
|
|
use Shorewall::Config qw(:DEFAULT :internal);
|
|
|
|
use Shorewall::IPAddrs;
|
|
|
|
use Shorewall::Zones;
|
|
|
|
use Shorewall::Chains qw(:DEFAULT :internal);
|
|
|
|
|
|
|
|
use strict;
|
|
|
|
|
|
|
|
our @ISA = qw(Exporter);
|
|
|
|
our @EXPORT = qw( setup_notrack );
|
|
|
|
our @EXPORT_OK = qw( );
|
2011-07-11 00:10:27 +02:00
|
|
|
our $VERSION = 'MODULEVERSION';
|
2009-02-22 22:15:05 +01:00
|
|
|
|
2011-12-05 02:15:58 +01:00
|
|
|
my %valid_ctevent = ( new => 1, related => 1, destroy => 1, reply => 1, assured => 1, protoinfo => 1, helper => 1, mark => 1, natseqinfo => 1, secmark => 1 );
|
|
|
|
|
2009-02-22 22:15:05 +01:00
|
|
|
#
|
|
|
|
# Notrack
|
|
|
|
#
|
2011-12-05 02:15:58 +01:00
|
|
|
sub process_notrack_rule( $$$$$$$ ) {
|
2009-02-22 22:15:05 +01:00
|
|
|
|
2011-12-05 02:15:58 +01:00
|
|
|
my ($action, $source, $dest, $proto, $ports, $sports, $user ) = @_;
|
2009-02-22 22:15:05 +01:00
|
|
|
|
|
|
|
$proto = '' if $proto eq 'any';
|
|
|
|
$ports = '' if $ports eq 'any' || $ports eq 'all';
|
|
|
|
$sports = '' if $sports eq 'any' || $sports eq 'all';
|
|
|
|
|
2009-08-20 23:32:15 +02:00
|
|
|
( my $zone, $source) = split /:/, $source, 2;
|
2009-02-22 22:15:05 +01:00
|
|
|
my $zoneref = find_zone $zone;
|
|
|
|
my $chainref = ensure_raw_chain( notrack_chain $zone );
|
2010-07-01 05:35:46 +02:00
|
|
|
my $restriction = $zoneref->{type} == FIREWALL || $zoneref->{type} == VSERVER ? OUTPUT_RESTRICT : PREROUTE_RESTRICT;
|
2009-02-22 22:15:05 +01:00
|
|
|
|
2010-07-01 05:35:46 +02:00
|
|
|
fatal_error 'USER/GROUP is not allowed unless the SOURCE zone is $FW or a Vserver zone' if $user ne '-' && $restriction != OUTPUT_RESTRICT;
|
2012-08-03 19:53:20 +02:00
|
|
|
require_capability 'RAW_TABLE', 'conntrack rules', '';
|
2009-02-22 22:15:05 +01:00
|
|
|
|
2011-12-05 02:15:58 +01:00
|
|
|
my $target = $action;
|
2011-12-18 16:21:32 +01:00
|
|
|
my $exception_rule = '';
|
2011-12-24 02:55:13 +01:00
|
|
|
my $rule = do_proto( $proto, $ports, $sports ) . do_user ( $user );
|
2011-12-05 02:15:58 +01:00
|
|
|
|
|
|
|
unless ( $action eq 'NOTRACK' ) {
|
|
|
|
( $target, my ( $option, $args, $junk ) ) = split ':', $action, 4;
|
|
|
|
|
|
|
|
fatal_error "Invalid notrack ACTION ( $action )" if $junk || $target ne 'CT';
|
|
|
|
|
2012-08-03 19:53:20 +02:00
|
|
|
require_capability 'CT_TARGET', 'CT entries in the conntrack file', '';
|
2011-12-05 02:15:58 +01:00
|
|
|
|
|
|
|
if ( $option eq 'notrack' ) {
|
2012-08-03 19:53:20 +02:00
|
|
|
fatal_error "Invalid conntrack ACTION ( $action )" if supplied $args;
|
2011-12-05 02:15:58 +01:00
|
|
|
$action = 'CT --notrack';
|
|
|
|
} else {
|
|
|
|
fatal_error "Invalid or missing CT option and arguments" unless supplied $option && supplied $args;
|
|
|
|
|
2011-12-17 17:59:27 +01:00
|
|
|
if ( $option eq 'helper' ) {
|
2012-08-02 20:10:08 +02:00
|
|
|
my $modifiers = '';
|
|
|
|
|
2012-08-02 22:25:44 +02:00
|
|
|
if ( $args =~ /^([-\w.]+)\((.+)\)$/ ) {
|
2012-08-02 20:10:08 +02:00
|
|
|
$args = $1;
|
|
|
|
$modifiers = $2;
|
|
|
|
}
|
|
|
|
|
2011-12-17 17:59:27 +01:00
|
|
|
fatal_error "Invalid helper' ($args)" if $args =~ /,/;
|
2011-12-24 18:24:01 +01:00
|
|
|
validate_helper( $args, $proto );
|
2011-12-17 17:59:27 +01:00
|
|
|
$action = "CT --helper $args";
|
2011-12-18 16:21:32 +01:00
|
|
|
$exception_rule = do_proto( $proto, '-', '-' );
|
2011-12-05 02:15:58 +01:00
|
|
|
|
2012-08-05 17:09:17 +02:00
|
|
|
for my $mod ( split_list1( $modifiers, 'ctevents' ) ) {
|
2012-08-02 20:10:08 +02:00
|
|
|
fatal_error "Invalid helper option ($mod)" unless $mod =~ /^(\w+)=(.+)$/;
|
2012-08-05 17:09:17 +02:00
|
|
|
$mod = $1;
|
|
|
|
my $val = $2;
|
2012-08-02 20:10:08 +02:00
|
|
|
|
|
|
|
if ( $mod eq 'ctevents' ) {
|
2012-08-05 17:09:17 +02:00
|
|
|
for ( split_list( $val, 'ctevents' ) ) {
|
2012-08-02 20:10:08 +02:00
|
|
|
fatal_error "Invalid 'ctevents' event ($_)" unless $valid_ctevent{$_};
|
|
|
|
}
|
|
|
|
|
2012-08-05 17:09:17 +02:00
|
|
|
$action .= " --ctevents $val";
|
2012-08-02 20:10:08 +02:00
|
|
|
} elsif ( $mod eq 'expevents' ) {
|
2012-08-05 17:09:17 +02:00
|
|
|
fatal_error "Invalid expevent argument ($val)" unless $val eq 'new';
|
2012-08-02 20:10:08 +02:00
|
|
|
$action .= ' --expevents new';
|
|
|
|
} else {
|
|
|
|
fatal_error "Invalid helper option ($mod)";
|
|
|
|
}
|
|
|
|
}
|
2012-08-05 17:09:17 +02:00
|
|
|
} else {
|
|
|
|
fatal_error "Invalid CT option ($option)";
|
2011-12-05 02:15:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-19 00:01:13 +01:00
|
|
|
expand_rule( $chainref ,
|
|
|
|
$restriction ,
|
2011-12-24 02:55:13 +01:00
|
|
|
$rule,
|
2011-12-19 00:01:13 +01:00
|
|
|
$source ,
|
|
|
|
$dest ,
|
|
|
|
'' ,
|
|
|
|
$action ,
|
|
|
|
'' ,
|
|
|
|
$target ,
|
|
|
|
$exception_rule );
|
2012-04-24 23:52:57 +02:00
|
|
|
|
2009-02-22 22:15:05 +01:00
|
|
|
progress_message " Notrack rule \"$currentline\" $done";
|
|
|
|
|
|
|
|
$globals{UNTRACKED} = 1;
|
|
|
|
}
|
|
|
|
|
2011-12-05 02:15:58 +01:00
|
|
|
sub process_format( $ ) {
|
|
|
|
my $format = shift;
|
|
|
|
|
|
|
|
fatal_error q(FORMAT must be '1' or '2') unless $format =~ /^[12]$/;
|
|
|
|
|
|
|
|
$format;
|
|
|
|
}
|
|
|
|
|
2009-02-22 22:15:05 +01:00
|
|
|
sub setup_notrack() {
|
|
|
|
|
2011-12-05 02:15:58 +01:00
|
|
|
my $format = 1;
|
|
|
|
my $action = 'NOTRACK';
|
|
|
|
|
2012-08-02 20:02:17 +02:00
|
|
|
my $fn = open_file( 'notrack' );
|
|
|
|
|
|
|
|
if ( $fn ) {
|
|
|
|
if ( -f ( my $fn1 = find_file 'conntrack' ) ) {
|
|
|
|
warning_message "Both $fn and $fn1 exist: $fn1 will be ignored";
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$fn = open_file( 'conntrack' );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $fn ) {
|
2009-02-22 22:15:05 +01:00
|
|
|
|
2010-09-28 19:48:44 +02:00
|
|
|
first_entry "$doing $fn...";
|
2009-02-22 22:15:05 +01:00
|
|
|
|
2010-09-28 19:48:44 +02:00
|
|
|
my $nonEmpty = 0;
|
2009-02-22 22:15:05 +01:00
|
|
|
|
2012-04-24 23:52:57 +02:00
|
|
|
while ( read_a_line( NORMAL_READ ) ) {
|
2011-12-06 02:45:09 +01:00
|
|
|
my ( $source, $dest, $proto, $ports, $sports, $user );
|
2011-12-05 02:15:58 +01:00
|
|
|
|
|
|
|
if ( $format == 1 ) {
|
2012-08-03 19:53:20 +02:00
|
|
|
( $source, $dest, $proto, $ports, $sports, $user ) = split_line1 'Conntrack File', { source => 0, dest => 1, proto => 2, dport => 3, sport => 4, user => 5 };
|
2012-04-24 23:52:57 +02:00
|
|
|
|
2011-12-05 02:15:58 +01:00
|
|
|
if ( $source eq 'FORMAT' ) {
|
|
|
|
$format = process_format( $dest );
|
|
|
|
next;
|
|
|
|
}
|
2012-04-24 23:52:57 +02:00
|
|
|
|
2011-12-05 02:15:58 +01:00
|
|
|
if ( $source eq 'COMMENT' ) {
|
|
|
|
process_comment;
|
|
|
|
next;
|
2012-04-24 23:52:57 +02:00
|
|
|
}
|
2010-09-28 19:48:44 +02:00
|
|
|
} else {
|
2012-08-03 19:53:20 +02:00
|
|
|
( $action, $source, $dest, $proto, $ports, $sports, $user ) = split_line1 'Conntrack File', { action => 0, source => 1, dest => 2, proto => 3, dport => 4, sport => 5, user => 6 }, { COMMENT => 0, FORMAT => 2 };
|
2012-04-24 23:52:57 +02:00
|
|
|
|
2011-12-05 02:15:58 +01:00
|
|
|
if ( $action eq 'FORMAT' ) {
|
|
|
|
$format = process_format( $source );
|
|
|
|
$action = 'NOTRACK';
|
|
|
|
next;
|
|
|
|
}
|
2012-04-24 23:52:57 +02:00
|
|
|
|
2011-12-05 02:15:58 +01:00
|
|
|
if ( $action eq 'COMMENT' ) {
|
|
|
|
process_comment;
|
|
|
|
next;
|
2012-04-24 23:52:57 +02:00
|
|
|
}
|
2010-09-28 19:48:44 +02:00
|
|
|
}
|
2012-04-24 23:52:57 +02:00
|
|
|
|
2012-08-02 20:10:08 +02:00
|
|
|
if ( $source eq 'all' ) {
|
|
|
|
for my $zone (all_zones) {
|
|
|
|
process_notrack_rule( $action, $zone, $dest, $proto, $ports, $sports, $user );
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
process_notrack_rule( $action, $source, $dest, $proto, $ports, $sports, $user );
|
|
|
|
}
|
2009-02-22 22:15:05 +01:00
|
|
|
}
|
|
|
|
|
2010-09-28 19:48:44 +02:00
|
|
|
clear_comment;
|
|
|
|
}
|
2009-02-22 22:15:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
1;
|