2007-03-15 22:55:22 +01:00
|
|
|
#
|
2007-06-11 21:39:30 +02:00
|
|
|
# Shorewall-perl 4.0 -- /usr/share/shorewall-perl/Shorewall/Zones.pm
|
2007-03-15 22:55:22 +01:00
|
|
|
#
|
|
|
|
# This program is under GPL [http://www.gnu.org/copyleft/gpl.htm]
|
|
|
|
#
|
|
|
|
# (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
|
|
|
|
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA
|
|
|
|
#
|
2007-04-19 01:43:54 +02:00
|
|
|
# This module contains the code which deals with /etc/shorewall/zones.
|
2007-03-15 22:55:22 +01:00
|
|
|
#
|
2007-03-14 04:19:25 +01:00
|
|
|
package Shorewall::Zones;
|
|
|
|
require Exporter;
|
|
|
|
use Shorewall::Common;
|
|
|
|
use Shorewall::Config;
|
|
|
|
|
2007-03-15 01:34:17 +01:00
|
|
|
use strict;
|
|
|
|
|
2007-03-14 04:19:25 +01:00
|
|
|
our @ISA = qw(Exporter);
|
2007-03-15 00:09:05 +01:00
|
|
|
our @EXPORT = qw( NOTHING
|
|
|
|
NUMERIC
|
|
|
|
NETWORK
|
|
|
|
IPSECPROTO
|
|
|
|
IPSECMODE
|
2007-03-27 01:17:46 +02:00
|
|
|
|
2007-03-15 00:09:05 +01:00
|
|
|
determine_zones
|
|
|
|
zone_report
|
2007-03-21 21:35:40 +01:00
|
|
|
dump_zone_contents
|
2007-05-08 00:20:00 +02:00
|
|
|
haveipseczones
|
2007-06-07 04:21:54 +02:00
|
|
|
single_interface
|
2007-03-15 00:09:05 +01:00
|
|
|
|
2007-04-08 16:42:26 +02:00
|
|
|
@zones
|
2007-03-15 00:09:05 +01:00
|
|
|
%zones
|
2007-04-08 16:42:26 +02:00
|
|
|
$firewall_zone
|
2007-03-15 01:34:17 +01:00
|
|
|
%interfaces );
|
|
|
|
|
2007-06-14 01:02:39 +02:00
|
|
|
our @EXPORT_OK = qw( initialize );
|
2007-07-01 02:08:23 +02:00
|
|
|
our $VERSION = 4.00;
|
2007-03-14 04:19:25 +01:00
|
|
|
|
2007-03-15 00:09:05 +01:00
|
|
|
#
|
|
|
|
# IPSEC Option types
|
|
|
|
#
|
|
|
|
use constant { NOTHING => 'NOTHING',
|
|
|
|
NUMERIC => '0x[\da-fA-F]+|\d+',
|
|
|
|
NETWORK => '\d+.\d+.\d+.\d+(\/\d+)?',
|
|
|
|
IPSECPROTO => 'ah|esp|ipcomp',
|
|
|
|
IPSECMODE => 'tunnel|transport'
|
|
|
|
};
|
|
|
|
|
2007-03-14 04:19:25 +01:00
|
|
|
#
|
2007-04-08 16:42:26 +02:00
|
|
|
# Zone Table.
|
2007-03-14 04:19:25 +01:00
|
|
|
#
|
|
|
|
# @zones contains the ordered list of zones with sub-zones appearing before their parents.
|
|
|
|
#
|
|
|
|
# %zones{<zone1> => {type = > <zone type> 'firewall', 'ipv4', 'ipsec4';
|
|
|
|
# options => { complex => 0|1
|
|
|
|
# in_out => < policy match string >
|
|
|
|
# in => < policy match string >
|
2007-04-08 16:42:26 +02:00
|
|
|
# out => < policy match string >
|
2007-03-14 04:19:25 +01:00
|
|
|
# }
|
|
|
|
# parents => [ <parents> ] Parents, Children and interfaces are listed by name
|
|
|
|
# children => [ <children> ]
|
|
|
|
# interfaces => [ <interfaces> ]
|
2007-06-06 02:47:27 +02:00
|
|
|
# bridge => <bridge>
|
2007-03-14 04:19:25 +01:00
|
|
|
# hosts { <type> } => [ { <interface1> => { ipsec => 'ipsec'|'none'
|
|
|
|
# options => { <option1> => <value1>
|
|
|
|
# ...
|
|
|
|
# }
|
|
|
|
# hosts => [ <net1> , <net2> , ... ]
|
|
|
|
# }
|
|
|
|
# <interface2> => ...
|
|
|
|
# }
|
|
|
|
# ]
|
|
|
|
# }
|
|
|
|
# <zone2> => ...
|
|
|
|
# }
|
|
|
|
#
|
|
|
|
# $firewall_zone names the firewall zone.
|
|
|
|
#
|
2007-04-08 16:42:26 +02:00
|
|
|
our @zones;
|
2007-03-14 05:29:14 +01:00
|
|
|
our %zones;
|
|
|
|
our $firewall_zone;
|
2007-03-14 04:19:25 +01:00
|
|
|
|
2007-06-05 18:49:13 +02:00
|
|
|
our %reservedName = ( all => 1,
|
|
|
|
none => 1,
|
|
|
|
SOURCE => 1,
|
|
|
|
DEST => 1 );
|
2007-04-20 16:13:57 +02:00
|
|
|
|
2007-06-15 00:07:45 +02:00
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
#
|
|
|
|
|
2007-06-14 01:02:39 +02:00
|
|
|
sub initialize() {
|
|
|
|
@zones = ();
|
|
|
|
%zones = ();
|
2007-06-19 01:04:17 +02:00
|
|
|
$firewall_zone = '';
|
2007-06-14 01:02:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
INIT {
|
|
|
|
initialize;
|
|
|
|
}
|
|
|
|
|
2007-03-14 04:19:25 +01:00
|
|
|
#
|
|
|
|
# Parse the passed option list and return a reference to a hash as follows:
|
|
|
|
#
|
|
|
|
# => mss = <MSS setting>
|
|
|
|
# => ipsec = <-m policy arguments to match options>
|
|
|
|
#
|
2007-04-22 17:41:08 +02:00
|
|
|
sub parse_zone_option_list($$)
|
2007-03-14 04:19:25 +01:00
|
|
|
{
|
|
|
|
my %validoptions = ( mss => NUMERIC,
|
2007-04-18 22:53:25 +02:00
|
|
|
strict => NOTHING,
|
|
|
|
next => NOTHING,
|
|
|
|
reqid => NUMERIC,
|
|
|
|
spi => NUMERIC,
|
|
|
|
proto => IPSECPROTO,
|
|
|
|
mode => IPSECMODE,
|
|
|
|
"tunnel-src" => NETWORK,
|
|
|
|
"tunnel-dst" => NETWORK,
|
2007-03-14 04:19:25 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
#
|
|
|
|
# Hash of options that have their own key in the returned hash.
|
|
|
|
#
|
|
|
|
my %key = ( mss => "mss" );
|
|
|
|
|
2007-04-22 17:41:08 +02:00
|
|
|
my ( $list, $zonetype ) = @_;
|
2007-03-14 04:19:25 +01:00
|
|
|
my %h;
|
|
|
|
my $options = '';
|
|
|
|
my $fmt;
|
|
|
|
|
|
|
|
if ( $list ne '-' ) {
|
|
|
|
for my $e ( split ',' , $list ) {
|
|
|
|
my $val = undef;
|
|
|
|
my $invert = '';
|
|
|
|
|
|
|
|
if ( $e =~ /([\w-]+)!=(.+)/ ) {
|
|
|
|
$val = $2;
|
|
|
|
$e = $1;
|
|
|
|
$invert = '! ';
|
|
|
|
} elsif ( $e =~ /([\w-]+)=(.+)/ ) {
|
|
|
|
$val = $2;
|
|
|
|
$e = $1;
|
|
|
|
}
|
2007-03-27 01:17:46 +02:00
|
|
|
|
2007-03-14 04:19:25 +01:00
|
|
|
$fmt = $validoptions{$e};
|
|
|
|
|
|
|
|
fatal_error "Invalid Option ($e)" unless $fmt;
|
2007-03-27 01:17:46 +02:00
|
|
|
|
2007-03-14 04:19:25 +01:00
|
|
|
if ( $fmt eq NOTHING ) {
|
2007-04-22 16:29:30 +02:00
|
|
|
fatal_error "Option \"$e\" does not take a value" if defined $val;
|
2007-03-14 04:19:25 +01:00
|
|
|
} else {
|
2007-04-22 16:29:30 +02:00
|
|
|
fatal_error "Missing value for option \"$e\"" unless defined $val;
|
2007-03-30 04:05:11 +02:00
|
|
|
fatal_error "Invalid value ($val) for option \"$e\"" unless $val =~ /^($fmt)$/;
|
2007-03-14 04:19:25 +01:00
|
|
|
}
|
2007-03-27 01:17:46 +02:00
|
|
|
|
2007-03-14 04:19:25 +01:00
|
|
|
if ( $key{$e} ) {
|
|
|
|
$h{$e} = $val;
|
|
|
|
} else {
|
2007-04-22 17:41:08 +02:00
|
|
|
fatal_error "The \"$e\" option may only be specified for ipsec zones" unless $zonetype eq 'ipsec4';
|
2007-03-14 04:19:25 +01:00
|
|
|
$options .= $invert;
|
|
|
|
$options .= "--$e ";
|
|
|
|
$options .= "$val "if defined $val;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$h{ipsec} = $options ? "$options " : '';
|
|
|
|
|
2007-04-08 16:42:26 +02:00
|
|
|
\%h;
|
2007-03-14 04:19:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Parse the zones file.
|
2007-03-27 01:17:46 +02:00
|
|
|
#
|
2007-03-14 04:19:25 +01:00
|
|
|
sub determine_zones()
|
|
|
|
{
|
|
|
|
my @z;
|
|
|
|
|
2007-03-30 17:57:08 +02:00
|
|
|
my $fn = open_file 'zones';
|
|
|
|
|
|
|
|
my $first_entry = 1;
|
2007-03-14 04:19:25 +01:00
|
|
|
|
2007-03-29 17:47:47 +02:00
|
|
|
while ( read_a_line ) {
|
2007-03-27 01:17:46 +02:00
|
|
|
|
2007-03-30 17:57:08 +02:00
|
|
|
if ( $first_entry ) {
|
2007-04-08 16:42:26 +02:00
|
|
|
progress_message2 "$doing $fn...";
|
2007-03-30 17:57:08 +02:00
|
|
|
$first_entry = 0;
|
|
|
|
}
|
|
|
|
|
2007-05-09 21:05:27 +02:00
|
|
|
my @parents;
|
|
|
|
|
|
|
|
my ($zone, $type, $options, $in_options, $out_options ) = split_line 1, 5, 'zones file';
|
|
|
|
|
2007-03-14 04:19:25 +01:00
|
|
|
if ( $zone =~ /(\w+):([\w,]+)/ ) {
|
|
|
|
$zone = $1;
|
|
|
|
@parents = split ',', $2;
|
|
|
|
|
|
|
|
for my $p ( @parents ) {
|
|
|
|
fatal_error "Invalid Parent List ($2)" unless $p;
|
|
|
|
fatal_error "Unknown parent zone ($p)" unless $zones{$p};
|
|
|
|
fatal_error 'Subzones of firewall zone not allowed' if $zones{$p}{type} eq 'firewall';
|
|
|
|
push @{$zones{$p}{children}}, $zone;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-16 23:08:12 +02:00
|
|
|
fatal_error "Invalid zone name ($zone)" unless "\L$zone" =~ /^[a-z]\w*$/ && length $zone <= $globals{MAXZONENAMELENGTH};
|
|
|
|
fatal_error "Invalid zone name ($zone)" if $reservedName{$zone} || $zone =~ /^all2|2all$/;
|
|
|
|
fatal_error( "Duplicate zone name ($zone)" ) if $zones{$zone};
|
2007-03-14 04:19:25 +01:00
|
|
|
|
|
|
|
$type = "ipv4" unless $type;
|
|
|
|
|
|
|
|
if ( $type =~ /ipv4/i ) {
|
2007-06-07 01:39:27 +02:00
|
|
|
$type = 'ipv4';
|
2007-03-14 04:19:25 +01:00
|
|
|
} elsif ( $type =~ /^ipsec4?$/i ) {
|
2007-06-07 01:39:27 +02:00
|
|
|
$type = 'ipsec4';
|
2007-06-06 02:47:27 +02:00
|
|
|
} elsif ( $type =~ /^bport4?$/i ) {
|
2007-06-06 22:06:16 +02:00
|
|
|
warning_message "Bridge Port zones should have a parent zone" unless @parents;
|
2007-06-07 01:39:27 +02:00
|
|
|
$type = 'bport4';
|
2007-03-14 04:19:25 +01:00
|
|
|
} elsif ( $type eq 'firewall' ) {
|
|
|
|
fatal_error 'Firewall zone may not be nested' if @parents;
|
2007-06-16 23:08:12 +02:00
|
|
|
fatal_error "Only one firewall zone may be defined ($zone)" if $firewall_zone;
|
2007-03-14 04:19:25 +01:00
|
|
|
$firewall_zone = $zone;
|
2007-04-01 21:48:20 +02:00
|
|
|
$ENV{FW} = $zone;
|
2007-06-07 01:39:27 +02:00
|
|
|
$type = "firewall";
|
2007-03-14 04:19:25 +01:00
|
|
|
} elsif ( $type eq '-' ) {
|
2007-06-07 01:39:27 +02:00
|
|
|
$type = 'ipv4';
|
2007-03-14 04:19:25 +01:00
|
|
|
} else {
|
|
|
|
fatal_error "Invalid zone type ($type)" ;
|
|
|
|
}
|
|
|
|
|
2007-06-23 18:06:16 +02:00
|
|
|
for ( $options, $in_options, $out_options ) {
|
|
|
|
$_ = '' if $_ eq '-';
|
|
|
|
}
|
2007-06-07 01:39:27 +02:00
|
|
|
|
|
|
|
$zones{$zone} = { type => $type,
|
|
|
|
parents => \@parents,
|
|
|
|
exclusions => [],
|
|
|
|
bridge => '',
|
2007-06-23 18:06:16 +02:00
|
|
|
options => { in_out => parse_zone_option_list( $options || '', $type ) ,
|
|
|
|
in => parse_zone_option_list( $in_options || '', $type ) ,
|
|
|
|
out => parse_zone_option_list( $out_options || '', $type ) ,
|
|
|
|
complex => ($type eq 'ipsec4' || $options || $in_options || $out_options ? 1 : 0) } ,
|
2007-06-07 01:39:27 +02:00
|
|
|
interfaces => {} ,
|
|
|
|
children => [] ,
|
|
|
|
hosts => {}
|
|
|
|
};
|
2007-03-14 04:19:25 +01:00
|
|
|
push @z, $zone;
|
|
|
|
}
|
|
|
|
|
2007-04-20 16:58:11 +02:00
|
|
|
fatal_error "No firewall zone defined" unless $firewall_zone;
|
|
|
|
|
2007-03-14 04:19:25 +01:00
|
|
|
my $pushed = 1;
|
|
|
|
my %ordered;
|
|
|
|
|
|
|
|
while ( $pushed )
|
|
|
|
{
|
|
|
|
$pushed = 0;
|
|
|
|
ZONE:
|
|
|
|
for my $zone ( @z ) {
|
|
|
|
unless ( $ordered{$zone} ) {
|
|
|
|
for my $child ( @{$zones{$zone}{children}} ) {
|
|
|
|
next ZONE unless $ordered{$child};
|
|
|
|
}
|
|
|
|
$ordered{$zone} = 1;
|
|
|
|
push @zones, $zone;
|
2007-04-08 16:42:26 +02:00
|
|
|
$pushed = 1;
|
2007-03-14 04:19:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-05-08 00:12:42 +02:00
|
|
|
#
|
|
|
|
# Return true of we have any ipsec zones
|
|
|
|
#
|
2007-05-08 00:20:00 +02:00
|
|
|
sub haveipseczones() {
|
2007-05-08 00:12:42 +02:00
|
|
|
for my $zoneref ( values %zones ) {
|
|
|
|
return 1 if $zoneref->{type} eq 'ipsec4';
|
|
|
|
}
|
|
|
|
|
|
|
|
0;
|
|
|
|
}
|
|
|
|
|
2007-03-14 04:27:29 +01:00
|
|
|
#
|
|
|
|
# Report about zones.
|
|
|
|
#
|
2007-04-08 16:42:26 +02:00
|
|
|
sub zone_report()
|
2007-03-14 04:27:29 +01:00
|
|
|
{
|
|
|
|
for my $zone ( @zones )
|
|
|
|
{
|
|
|
|
my $zoneref = $zones{$zone};
|
|
|
|
my $hostref = $zoneref->{hosts};
|
|
|
|
my $type = $zoneref->{type};
|
|
|
|
my $optionref = $zoneref->{options};
|
|
|
|
|
|
|
|
progress_message " $zone ($type)";
|
|
|
|
|
|
|
|
my $printed = 0;
|
2007-03-27 01:17:46 +02:00
|
|
|
|
2007-03-14 04:27:29 +01:00
|
|
|
if ( $hostref ) {
|
|
|
|
for my $type ( sort keys %$hostref ) {
|
|
|
|
my $interfaceref = $hostref->{$type};
|
2007-03-27 01:17:46 +02:00
|
|
|
|
2007-03-14 04:27:29 +01:00
|
|
|
for my $interface ( sort keys %$interfaceref ) {
|
|
|
|
my $arrayref = $interfaceref->{$interface};
|
|
|
|
for my $groupref ( @$arrayref ) {
|
|
|
|
my $hosts = $groupref->{hosts};
|
|
|
|
if ( $hosts ) {
|
|
|
|
my $grouplist = join ',', ( @$hosts );
|
|
|
|
progress_message " $interface:$grouplist";
|
|
|
|
$printed = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-03-27 01:17:46 +02:00
|
|
|
|
2007-06-20 00:39:41 +02:00
|
|
|
unless ( $printed ) {
|
|
|
|
fatal_error "No bridge has been associated with zone $zone" if $type eq 'bport4' && ! $zoneref->{bridge};
|
|
|
|
warning_message "*** $zone is an EMPTY ZONE ***" unless $type eq 'firewall';
|
|
|
|
}
|
|
|
|
|
2007-03-14 04:27:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-04-08 16:42:26 +02:00
|
|
|
sub dump_zone_contents()
|
2007-03-21 21:35:40 +01:00
|
|
|
{
|
|
|
|
for my $zone ( @zones )
|
|
|
|
{
|
|
|
|
my $zoneref = $zones{$zone};
|
|
|
|
my $hostref = $zoneref->{hosts};
|
|
|
|
my $type = $zoneref->{type};
|
|
|
|
my $optionref = $zoneref->{options};
|
|
|
|
my $exclusions = $zoneref->{exclusions};
|
|
|
|
my $entry = "$zone $type";
|
|
|
|
|
2007-06-20 00:39:41 +02:00
|
|
|
$entry .= ":$zoneref->{bridge}" if $type eq 'bport4';
|
|
|
|
|
2007-03-21 21:35:40 +01:00
|
|
|
if ( $hostref ) {
|
|
|
|
for my $type ( sort keys %$hostref ) {
|
|
|
|
my $interfaceref = $hostref->{$type};
|
2007-03-27 01:17:46 +02:00
|
|
|
|
2007-03-21 21:35:40 +01:00
|
|
|
for my $interface ( sort keys %$interfaceref ) {
|
|
|
|
my $arrayref = $interfaceref->{$interface};
|
|
|
|
for my $groupref ( @$arrayref ) {
|
|
|
|
my $hosts = $groupref->{hosts};
|
|
|
|
if ( $hosts ) {
|
|
|
|
my $grouplist = join ',', ( @$hosts );
|
|
|
|
$entry .= " $interface:$grouplist";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( @$exclusions ) {
|
|
|
|
$entry .= ' exclude';
|
2007-03-27 01:17:46 +02:00
|
|
|
|
2007-03-21 21:35:40 +01:00
|
|
|
for my $host ( @$exclusions ) {
|
|
|
|
$entry .= " $host";
|
|
|
|
}
|
2007-03-27 01:17:46 +02:00
|
|
|
}
|
|
|
|
|
2007-03-21 21:35:40 +01:00
|
|
|
emit_unindented $entry;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-07 04:21:54 +02:00
|
|
|
#
|
|
|
|
# If the passed zone is associated with a single interface, the name of the interface is returned. Otherwise, the funtion returns '';
|
|
|
|
#
|
|
|
|
sub single_interface( $ ) {
|
|
|
|
my $zone = $_[0];
|
|
|
|
my $zoneref = $zones{$zone};
|
|
|
|
fatal_error "Internal Error in single_zone()" unless $zoneref;
|
|
|
|
|
|
|
|
{
|
|
|
|
no warnings;
|
|
|
|
if ( %{$zoneref->{interfaces}} == 1 ) {
|
|
|
|
( keys %{$zoneref->{interfaces}} )[0];
|
|
|
|
} else {
|
|
|
|
'';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-14 04:19:25 +01:00
|
|
|
1;
|