2007-03-15 22:55:22 +01:00
|
|
|
#
|
2007-03-28 18:19:35 +02:00
|
|
|
# Shorewall-perl 3.9 -- /usr/share/shorewall-perl/Shorewall/Interfaces.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:55:25 +02:00
|
|
|
# This Module contains the code for processing the /etc/shorewall/interfaces
|
|
|
|
# file. It also exports 'add_group_to_zone()' which other modules call to
|
|
|
|
# alter zone membership.
|
2007-03-15 22:55:22 +01:00
|
|
|
#
|
2007-04-19 01:55:25 +02:00
|
|
|
|
2007-03-14 04:24:28 +01:00
|
|
|
package Shorewall::Interfaces;
|
|
|
|
require Exporter;
|
|
|
|
use Shorewall::Common;
|
|
|
|
use Shorewall::Config;
|
2007-05-11 17:39:11 +02:00
|
|
|
use Shorewall::IPAddrs;
|
2007-03-14 05:29:14 +01:00
|
|
|
use Shorewall::Zones;
|
2007-03-14 04:24:28 +01:00
|
|
|
|
2007-03-15 01:34:17 +01:00
|
|
|
use strict;
|
|
|
|
|
2007-03-14 04:24:28 +01:00
|
|
|
our @ISA = qw(Exporter);
|
2007-04-08 16:42:26 +02:00
|
|
|
our @EXPORT = qw( add_group_to_zone
|
2007-03-15 01:42:37 +01:00
|
|
|
validate_interfaces_file
|
2007-04-08 16:42:26 +02:00
|
|
|
known_interface
|
2007-04-18 22:56:41 +02:00
|
|
|
interface_is_optional
|
2007-03-15 01:42:37 +01:00
|
|
|
find_interfaces_by_option
|
2007-03-16 17:26:34 +01:00
|
|
|
get_interface_option
|
2007-03-15 01:42:37 +01:00
|
|
|
|
|
|
|
@interfaces );
|
2007-03-14 04:24:28 +01:00
|
|
|
our @EXPORT_OK = ();
|
|
|
|
our @VERSION = 1.00;
|
|
|
|
|
|
|
|
#
|
|
|
|
# Interface Table.
|
|
|
|
#
|
|
|
|
# @interfaces lists the interface names in the order that they appear in the interfaces file.
|
|
|
|
#
|
|
|
|
# %interfaces { <interface1> => { root => <name without trailing '+'>
|
|
|
|
# options => { <option1> = <val1> ,
|
|
|
|
# ...
|
|
|
|
# }
|
|
|
|
# zone => <zone name>
|
2007-03-31 20:44:48 +02:00
|
|
|
# }
|
2007-03-14 04:24:28 +01:00
|
|
|
# }
|
|
|
|
#
|
2007-03-14 05:29:14 +01:00
|
|
|
our @interfaces;
|
|
|
|
our %interfaces;
|
2007-03-14 04:24:28 +01:00
|
|
|
|
2007-03-15 01:34:17 +01:00
|
|
|
sub add_group_to_zone($$$$$)
|
|
|
|
{
|
|
|
|
my ($zone, $type, $interface, $networks, $options) = @_;
|
|
|
|
my $typeref;
|
|
|
|
my $interfaceref;
|
|
|
|
my $arrayref;
|
|
|
|
my $zoneref = $zones{$zone};
|
|
|
|
my $zonetype = $zoneref->{type};
|
|
|
|
my $ifacezone = $interfaces{$interface}{zone};
|
|
|
|
|
|
|
|
$zoneref->{interfaces}{$interface} = 1;
|
|
|
|
|
|
|
|
my @newnetworks;
|
|
|
|
my @exclusions;
|
|
|
|
my $new = \@newnetworks;
|
|
|
|
my $switched = 0;
|
|
|
|
|
|
|
|
$ifacezone = '' unless defined $ifacezone;
|
|
|
|
|
|
|
|
for my $host ( @$networks ) {
|
2007-05-07 19:36:21 +02:00
|
|
|
fatal_error "Invalid Host List" unless defined $host and $host ne '';
|
|
|
|
|
2007-04-25 18:45:46 +02:00
|
|
|
if ( substr( $host, 0, 1 ) eq '!' ) {
|
|
|
|
fatal_error "Only one exclusion allowed in a host list" if $switched;
|
2007-03-15 01:34:17 +01:00
|
|
|
$switched = 1;
|
2007-04-25 18:45:46 +02:00
|
|
|
$host = substr( $host, 1 );
|
2007-03-15 01:34:17 +01:00
|
|
|
$new = \@exclusions;
|
|
|
|
}
|
|
|
|
|
|
|
|
unless ( $switched ) {
|
|
|
|
if ( $type eq $zonetype ) {
|
|
|
|
fatal_error "Duplicate Host Group ($interface:$host) in zone $zone" if $ifacezone eq $zone;
|
|
|
|
$ifacezone = $zone if $host eq ALLIPv4;
|
|
|
|
}
|
|
|
|
}
|
2007-03-27 01:17:46 +02:00
|
|
|
|
2007-03-15 01:34:17 +01:00
|
|
|
push @$new, $switched ? "$interface:$host" : $host;
|
|
|
|
}
|
|
|
|
|
|
|
|
$zoneref->{options}{in_out}{routeback} = 1 if $options->{routeback};
|
|
|
|
|
|
|
|
$typeref = ( $zoneref->{hosts} || ( $zoneref->{hosts} = {} ) );
|
|
|
|
$interfaceref = ( $typeref->{$type} || ( $interfaceref = $typeref->{$type} = {} ) );
|
|
|
|
$arrayref = ( $interfaceref->{$interface} || ( $interfaceref->{$interface} = [] ) );
|
|
|
|
|
2007-04-25 20:44:46 +02:00
|
|
|
$zoneref->{options}{complex} = 1 if @$arrayref || ( @newnetworks > 1 ) || ( @exclusions );
|
2007-03-15 01:34:17 +01:00
|
|
|
|
|
|
|
my %h;
|
|
|
|
|
|
|
|
$h{options} = $options;
|
|
|
|
$h{hosts} = \@newnetworks;
|
|
|
|
$h{ipsec} = $type eq 'ipsec' ? 'ipsec' : 'none';
|
|
|
|
|
|
|
|
push @{$zoneref->{exclusions}}, @exclusions;
|
|
|
|
push @{$arrayref}, \%h;
|
|
|
|
}
|
|
|
|
|
2007-04-22 02:40:20 +02:00
|
|
|
#
|
|
|
|
# Return a list of networks routed out of the passed interface
|
|
|
|
#
|
2007-04-22 01:57:30 +02:00
|
|
|
sub get_routed_networks ( $$ ) {
|
|
|
|
my ( $interface , $error_message ) = @_;
|
|
|
|
my @networks;
|
|
|
|
|
2007-04-22 02:40:20 +02:00
|
|
|
if ( open IP , '-|' , "/sbin/ip route show dev $interface 2> /dev/null" ) {
|
2007-04-22 01:57:30 +02:00
|
|
|
while ( my $route = <IP> ) {
|
|
|
|
$route =~ s/^\s+//;
|
|
|
|
my $network = ( split /\s+/, $route )[0];
|
|
|
|
if ( $network eq 'default' ) {
|
2007-04-22 02:40:20 +02:00
|
|
|
fatal_error $error_message if $error_message;
|
|
|
|
warning_message "default route ignored on interface $interface";
|
2007-04-22 01:57:30 +02:00
|
|
|
} else {
|
|
|
|
my ( $address, $vlsm ) = split '/', $network;
|
|
|
|
$vlsm = 32 unless defined $vlsm;
|
|
|
|
push @networks, "$address/$vlsm";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
close IP
|
|
|
|
}
|
|
|
|
|
|
|
|
@networks;
|
|
|
|
}
|
|
|
|
|
2007-03-14 04:24:28 +01:00
|
|
|
#
|
|
|
|
# Parse the interfaces file.
|
2007-03-27 01:17:46 +02:00
|
|
|
#
|
2007-04-08 16:42:26 +02:00
|
|
|
|
2007-03-14 04:24:28 +01:00
|
|
|
sub validate_interfaces_file()
|
|
|
|
{
|
2007-04-22 16:50:19 +02:00
|
|
|
use constant { SIMPLE_IF_OPTION => 1,
|
|
|
|
BINARY_IF_OPTION => 2,
|
|
|
|
ENUM_IF_OPTION => 3,
|
|
|
|
MASK_IF_OPTION => 3,
|
|
|
|
|
|
|
|
IF_OPTION_ZONEONLY => 4 };
|
2007-04-18 20:15:46 +02:00
|
|
|
|
|
|
|
my %validoptions = (arp_filter => BINARY_IF_OPTION,
|
|
|
|
arp_ignore => ENUM_IF_OPTION,
|
|
|
|
blacklist => SIMPLE_IF_OPTION,
|
|
|
|
detectnets => SIMPLE_IF_OPTION,
|
|
|
|
dhcp => SIMPLE_IF_OPTION,
|
|
|
|
maclist => SIMPLE_IF_OPTION,
|
|
|
|
logmartians => BINARY_IF_OPTION,
|
|
|
|
norfc1918 => SIMPLE_IF_OPTION,
|
|
|
|
nosmurfs => SIMPLE_IF_OPTION,
|
|
|
|
optional => SIMPLE_IF_OPTION,
|
|
|
|
proxyarp => BINARY_IF_OPTION,
|
2007-04-22 16:50:19 +02:00
|
|
|
routeback => SIMPLE_IF_OPTION + IF_OPTION_ZONEONLY,
|
2007-04-18 20:15:46 +02:00
|
|
|
routefilter => BINARY_IF_OPTION,
|
|
|
|
sourceroute => BINARY_IF_OPTION,
|
|
|
|
tcpflags => SIMPLE_IF_OPTION,
|
|
|
|
upnp => SIMPLE_IF_OPTION,
|
2007-03-14 04:24:28 +01:00
|
|
|
);
|
|
|
|
|
2007-03-30 17:57:08 +02:00
|
|
|
my $fn = open_file 'interfaces';
|
|
|
|
|
|
|
|
my $first_entry = 1;
|
2007-03-14 04:24:28 +01:00
|
|
|
|
2007-03-29 19:02:13 +02:00
|
|
|
while ( read_a_line ) {
|
2007-03-14 04:24:28 +01: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 ($zone, $interface, $networks, $options ) = split_line 2, 4, 'interfaces file';
|
|
|
|
my $zoneref;
|
|
|
|
|
2007-03-14 04:24:28 +01:00
|
|
|
if ( $zone eq '-' ) {
|
|
|
|
$zone = '';
|
|
|
|
} else {
|
|
|
|
$zoneref = $zones{$zone};
|
|
|
|
|
|
|
|
fatal_error "Unknown zone ($zone)" unless $zoneref;
|
|
|
|
fatal_error "Firewall zone not allowed in ZONE column of interface record" if $zoneref->{type} eq 'firewall';
|
|
|
|
}
|
|
|
|
|
|
|
|
$networks = '' if $networks eq '-';
|
2007-03-25 18:38:00 +02:00
|
|
|
$options = '' if $options eq '-';
|
2007-03-14 04:24:28 +01:00
|
|
|
|
|
|
|
fatal_error "Duplicate Interface ($interface)" if $interfaces{$interface};
|
|
|
|
|
|
|
|
fatal_error "Invalid Interface Name: $interface" if $interface =~ /:|^\+$/;
|
|
|
|
|
2007-04-18 21:05:41 +02:00
|
|
|
my $wildcard = 0;
|
|
|
|
|
|
|
|
if ( $interface =~ /\+$/ ) {
|
|
|
|
$wildcard = 1;
|
|
|
|
$interfaces{$interface}{root} = substr( $interface, 0, -1 );
|
|
|
|
} else {
|
|
|
|
$interfaces{$interface}{root} = $interface;
|
|
|
|
}
|
2007-03-14 04:24:28 +01:00
|
|
|
|
2007-04-22 16:42:07 +02:00
|
|
|
unless ( $networks eq '' || $networks eq 'detect' ) {
|
|
|
|
|
|
|
|
for my $address ( split /,/, $networks ) {
|
|
|
|
fatal_error 'Invalid BROADCAST address' unless $address =~ /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/;
|
|
|
|
}
|
|
|
|
|
|
|
|
warning_message 'Shorewall no longer uses broadcast addresses in rule generation';
|
|
|
|
}
|
2007-03-14 04:24:28 +01:00
|
|
|
|
2007-03-20 05:31:19 +01:00
|
|
|
my $optionsref = {};
|
|
|
|
|
2007-04-22 01:57:30 +02:00
|
|
|
my %options;
|
|
|
|
|
|
|
|
if ( $options ) {
|
|
|
|
for my $option (split ',', $options ) {
|
2007-03-14 04:24:28 +01:00
|
|
|
next if $option eq '-';
|
|
|
|
|
2007-04-18 20:15:46 +02:00
|
|
|
( $option, my $value ) = split /=/, $option;
|
|
|
|
|
2007-04-18 21:05:41 +02:00
|
|
|
fatal_error "Invalid Interface option ($option)" unless my $type = $validoptions{$option};
|
2007-04-22 16:50:19 +02:00
|
|
|
|
|
|
|
fatal_error "The \"$option\" option may not be specified on a multi-zone interface" if $type & IF_OPTION_ZONEONLY && ! $zone;
|
2007-04-18 21:05:41 +02:00
|
|
|
|
2007-04-22 16:50:19 +02:00
|
|
|
$type &= MASK_IF_OPTION;
|
|
|
|
|
2007-04-18 21:05:41 +02:00
|
|
|
if ( $type == SIMPLE_IF_OPTION ) {
|
2007-04-18 20:15:46 +02:00
|
|
|
fatal_error "Option $option does not take a value" if defined $value;
|
|
|
|
$options{$option} = 1;
|
|
|
|
} elsif ( $type == BINARY_IF_OPTION ) {
|
|
|
|
$value = 1 unless defined $value;
|
|
|
|
fatal_error "Option value for $option must be 0 or 1" unless ( $value eq '0' || $value eq '1' );
|
2007-04-18 21:05:41 +02:00
|
|
|
fatal_error "The $option option may not be used with a wild-card interface name" if $wildcard;
|
2007-04-18 20:15:46 +02:00
|
|
|
$options{$option} = $value;
|
|
|
|
} elsif ( $type == ENUM_IF_OPTION ) {
|
2007-04-18 21:05:41 +02:00
|
|
|
fatal_error "The $option option may not be used with a wild-card interface name" if $wildcard;
|
2007-04-21 23:24:38 +02:00
|
|
|
if ( $option eq 'arp_ignore' ) {
|
2007-04-22 00:41:47 +02:00
|
|
|
if ( defined $value ) {
|
|
|
|
if ( $value =~ /^[1-3,8]$/ ) {
|
|
|
|
$options{arp_ignore} = $value;
|
|
|
|
} else {
|
|
|
|
fatal_error "Invalid value ($value) for arp_ignore";
|
|
|
|
}
|
2007-04-18 20:15:46 +02:00
|
|
|
} else {
|
2007-04-22 00:41:47 +02:00
|
|
|
$options{arp_ignore} = 1;
|
2007-04-18 20:15:46 +02:00
|
|
|
}
|
|
|
|
} else {
|
2007-04-22 01:57:30 +02:00
|
|
|
fatal_error "Internal Error in validate_interfaces_file";
|
2007-04-18 20:15:46 +02:00
|
|
|
}
|
2007-03-14 04:24:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-04-22 02:40:20 +02:00
|
|
|
$zoneref->{options}{in_out}{routeback} = 1 if $options{routeback};
|
2007-03-14 04:24:28 +01:00
|
|
|
}
|
2007-04-22 01:57:30 +02:00
|
|
|
|
|
|
|
$interfaces{$interface}{options} = $optionsref = \%options;
|
2007-03-14 04:24:28 +01:00
|
|
|
|
|
|
|
push @interfaces, $interface;
|
|
|
|
|
2007-04-22 02:40:20 +02:00
|
|
|
my @networks;
|
|
|
|
|
|
|
|
if ( $options{detectnets} ) {
|
|
|
|
fatal_error "'detectnets' not allowed with multi-zone interface" unless $zone;
|
|
|
|
fatal_error "The 'detectnets' option may not be used with a wild-card interface name" if $wildcard;
|
|
|
|
@networks = get_routed_networks( $interface, 'detectnets not allowed on interface with default route' );
|
|
|
|
fatal_error "No routes through 'detectnets' interface $interface" unless @networks || $options{optional};
|
|
|
|
} else {
|
|
|
|
@networks = @allipv4;
|
|
|
|
}
|
2007-04-22 01:57:30 +02:00
|
|
|
|
2007-04-22 16:21:48 +02:00
|
|
|
add_group_to_zone( $zone, $zoneref->{type}, $interface, \@networks, $optionsref ) if $zone && @networks;
|
2007-03-27 01:17:46 +02:00
|
|
|
|
2007-03-14 04:24:28 +01:00
|
|
|
$interfaces{$interface}{zone} = $zone; #Must follow the call to add_group_to_zone()
|
|
|
|
|
|
|
|
progress_message " Interface \"$line\" Validated";
|
|
|
|
|
2007-03-27 01:17:46 +02:00
|
|
|
}
|
2007-03-14 04:24:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Returns true if passed interface matches an entry in /etc/shorewall/interfaces
|
|
|
|
#
|
|
|
|
# If the passed name matches a wildcard, a entry for the name is added in %interfaces to speed up validation of other references to that name.
|
|
|
|
#
|
|
|
|
sub known_interface($)
|
|
|
|
{
|
|
|
|
my $interface = $_[0];
|
|
|
|
|
|
|
|
return 1 if exists $interfaces{$interface};
|
|
|
|
|
|
|
|
for my $i ( @interfaces ) {
|
|
|
|
my $val = $interfaces{$i}{root};
|
|
|
|
next if $val eq $i;
|
|
|
|
my $len = length $val;
|
|
|
|
if ( substr( $interface, 0, $len ) eq $val ) {
|
|
|
|
#
|
|
|
|
# Cache this result for future reference
|
|
|
|
#
|
|
|
|
$interfaces{$interface} = undef;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
0;
|
|
|
|
}
|
|
|
|
|
2007-04-18 03:07:39 +02:00
|
|
|
#
|
|
|
|
# Return the 'optional' setting of the passed interface
|
|
|
|
#
|
|
|
|
sub interface_is_optional($) {
|
|
|
|
my $optionsref = $interfaces{$_[0]}{options};
|
|
|
|
$optionsref && $optionsref->{optional};
|
|
|
|
}
|
|
|
|
|
2007-03-15 01:42:37 +01:00
|
|
|
#
|
|
|
|
# Returns reference to array of interfaces with the passed option
|
|
|
|
#
|
|
|
|
sub find_interfaces_by_option( $ ) {
|
|
|
|
my $option = $_[0];
|
|
|
|
my @ints = ();
|
|
|
|
|
|
|
|
for my $interface ( @interfaces ) {
|
|
|
|
my $optionsref = $interfaces{$interface}{options};
|
2007-04-18 20:15:46 +02:00
|
|
|
if ( $optionsref && defined $optionsref->{$option} ) {
|
|
|
|
push @ints , $interface
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
\@ints;
|
|
|
|
}
|
|
|
|
|
2007-03-16 17:26:34 +01:00
|
|
|
#
|
|
|
|
# Return the value of an option for an interface
|
|
|
|
#
|
|
|
|
sub get_interface_option( $$ ) {
|
|
|
|
my ( $interface, $option ) = @_;
|
|
|
|
|
|
|
|
$interfaces{$interface}{options}{$option};
|
|
|
|
}
|
|
|
|
|
2007-03-14 04:24:28 +01:00
|
|
|
1;
|