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/Hosts.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 dealing with the /etc/shorewall/hosts
|
|
|
|
# file.
|
2007-03-15 22:55:22 +01:00
|
|
|
#
|
2007-03-14 16:23:07 +01:00
|
|
|
package Shorewall::Hosts;
|
|
|
|
require Exporter;
|
|
|
|
use Shorewall::Common;
|
|
|
|
use Shorewall::Config;
|
2007-05-11 17:39:11 +02:00
|
|
|
use Shorewall::IPAddrs;
|
2007-03-14 16:23:07 +01:00
|
|
|
use Shorewall::Zones;
|
|
|
|
use Shorewall::Interfaces;
|
|
|
|
|
2007-03-15 01:34:17 +01:00
|
|
|
use strict;
|
|
|
|
|
2007-03-14 16:23:07 +01:00
|
|
|
our @ISA = qw(Exporter);
|
2007-03-15 01:42:37 +01:00
|
|
|
our @EXPORT = qw( validate_hosts_file find_hosts_by_option );
|
2007-03-14 16:23:07 +01:00
|
|
|
our @EXPORT_OK = ();
|
|
|
|
our @VERSION = 1.00;
|
|
|
|
|
|
|
|
#
|
2007-04-08 16:42:26 +02:00
|
|
|
# Validates the hosts file. Generates entries in %zone{..}{hosts}
|
2007-03-14 16:23:07 +01:00
|
|
|
#
|
|
|
|
sub validate_hosts_file()
|
|
|
|
{
|
|
|
|
my %validoptions = (
|
|
|
|
blacklist => 1,
|
|
|
|
maclist => 1,
|
|
|
|
norfc1918 => 1,
|
|
|
|
nosmurfs => 1,
|
2007-03-27 01:17:46 +02:00
|
|
|
routeback => 1,
|
2007-03-14 16:23:07 +01:00
|
|
|
routefilter => 1,
|
|
|
|
tcpflags => 1,
|
|
|
|
);
|
|
|
|
|
2007-03-20 15:15:11 +01:00
|
|
|
my $ipsec = 0;
|
2007-03-29 20:57:53 +02:00
|
|
|
my $first_entry = 1;
|
2007-03-20 15:15:11 +01:00
|
|
|
|
2007-03-30 17:57:08 +02:00
|
|
|
my $fn = open_file 'hosts';
|
2007-03-14 16:23:07 +01:00
|
|
|
|
2007-03-29 19:02:13 +02:00
|
|
|
while ( read_a_line ) {
|
2007-03-14 16:23:07 +01:00
|
|
|
|
2007-03-29 20:57:53 +02:00
|
|
|
if ( $first_entry ) {
|
2007-03-30 17:57:08 +02:00
|
|
|
progress_message2 "$doing $fn...";
|
2007-03-29 20:57:53 +02:00
|
|
|
$first_entry = 0;
|
|
|
|
}
|
|
|
|
|
2007-05-09 21:05:27 +02:00
|
|
|
my ($zone, $hosts, $options ) = split_line 2, 3, 'hosts file';
|
|
|
|
|
2007-03-14 16:23:07 +01:00
|
|
|
my $zoneref = $zones{$zone};
|
|
|
|
my $type = $zoneref->{type};
|
|
|
|
|
|
|
|
fatal_error "Unknown ZONE ($zone)" unless $type;
|
|
|
|
fatal_error 'Firewall zone not allowed in ZONE column of hosts record' if $type eq 'firewall';
|
|
|
|
|
|
|
|
my $interface;
|
|
|
|
|
2007-04-16 22:10:53 +02:00
|
|
|
if ( $hosts =~ /^([\w.@%-]+\+?):(.*)$/ ) {
|
2007-03-14 16:23:07 +01:00
|
|
|
$interface = $1;
|
|
|
|
$hosts = $2;
|
|
|
|
$zoneref->{options}{complex} = 1 if $hosts =~ /^\+/;
|
|
|
|
fatal_error "Unknown interface ($interface)" unless $interfaces{$interface}{root};
|
|
|
|
} else {
|
2007-03-25 18:38:00 +02:00
|
|
|
fatal_error "Invalid HOST(S) column contents: $hosts";
|
2007-03-14 16:23:07 +01:00
|
|
|
}
|
|
|
|
|
2007-06-06 02:47:27 +02:00
|
|
|
if ( $type eq 'bport4' ) {
|
|
|
|
if ( $zoneref->{bridge} eq '' ) {
|
|
|
|
fatal_error 'Bridge Port Zones may only be associated with bridge ports' unless $interfaces{$interface}{options}{port};
|
|
|
|
$zoneref->{bridge} = $interfaces{$interface}{bridge};
|
|
|
|
} elsif ( $zoneref->{bridge} ne $interfaces{$interface}{bridge} ) {
|
|
|
|
fatal_error "Interface $interface is not a port on bridge $zoneref->{bridge}";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-05-07 23:44:55 +02:00
|
|
|
my $optionsref = {};
|
2007-03-27 01:17:46 +02:00
|
|
|
|
2007-03-25 18:53:33 +02:00
|
|
|
if ( $options ne '-' ) {
|
2007-03-14 16:23:07 +01:00
|
|
|
my @options = split ',', $options;
|
|
|
|
my %options;
|
|
|
|
|
|
|
|
for my $option ( @options )
|
|
|
|
{
|
|
|
|
if ( $option eq 'ipsec' ) {
|
|
|
|
$type = 'ipsec';
|
|
|
|
$zoneref->{options}{complex} = 1;
|
2007-03-20 15:15:11 +01:00
|
|
|
$ipsec = 1;
|
2007-03-14 16:23:07 +01:00
|
|
|
} elsif ( $validoptions{$option}) {
|
|
|
|
$options{$option} = 1;
|
|
|
|
} else {
|
|
|
|
fatal_error "Invalid option ($option)";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$optionsref = \%options;
|
|
|
|
}
|
|
|
|
|
2007-04-25 18:45:46 +02:00
|
|
|
#
|
|
|
|
# Looking for the '!' at the beginning of a list element is more straight-foward than looking for it in the middle.
|
|
|
|
#
|
|
|
|
# Be sure we don't have a ',!' in the original
|
|
|
|
#
|
2007-04-25 18:26:16 +02:00
|
|
|
fatal_error "Invalid hosts list" if $hosts =~ /,!/;
|
2007-04-25 18:45:46 +02:00
|
|
|
#
|
|
|
|
# Now add a comma before '!'. Do it globally - add_group_to_zone() correctly checks for multiple exclusions
|
|
|
|
#
|
2007-05-07 19:36:21 +02:00
|
|
|
$hosts =~ s/!/,!/g;
|
|
|
|
#
|
|
|
|
# Take care of case where the hosts list begins with '!'
|
|
|
|
#
|
|
|
|
$hosts = join( '', ALLIPv4 , $hosts ) if substr($hosts, 0, 2 ) eq ',!';
|
2007-04-25 18:26:16 +02:00
|
|
|
|
2007-05-06 03:17:58 +02:00
|
|
|
add_group_to_zone( $zone, $type , $interface, [ split( ',', $hosts ) ] , $optionsref);
|
2007-03-14 16:23:07 +01:00
|
|
|
|
|
|
|
progress_message " Host \"$line\" validated";
|
|
|
|
}
|
|
|
|
|
2007-05-08 00:20:00 +02:00
|
|
|
$capabilities{POLICY_MATCH} = '' unless $ipsec || haveipseczones;
|
2007-03-14 16:23:07 +01:00
|
|
|
}
|
2007-03-15 01:42:37 +01:00
|
|
|
#
|
2007-04-08 16:42:26 +02:00
|
|
|
# Returns a reference to a array of host entries. Each entry is a
|
2007-05-08 00:12:42 +02:00
|
|
|
# reference to an array containing ( interface , polciy match type {ipsec|none} , network );
|
2007-03-15 01:42:37 +01:00
|
|
|
#
|
|
|
|
sub find_hosts_by_option( $ ) {
|
|
|
|
my $option = $_[0];
|
|
|
|
my @hosts;
|
|
|
|
|
|
|
|
for my $zone ( grep $zones{$_}{type} ne 'firewall' , @zones ) {
|
|
|
|
while ( my ($type, $interfaceref) = each %{$zones{$zone}{hosts}} ) {
|
|
|
|
while ( my ( $interface, $arrayref) = ( each %{$interfaceref} ) ) {
|
|
|
|
for my $host ( @{$arrayref} ) {
|
2007-05-07 23:44:55 +02:00
|
|
|
if ( $host->{options}{$option} ) {
|
2007-03-15 01:42:37 +01:00
|
|
|
for my $net ( @{$host->{hosts}} ) {
|
2007-05-08 00:12:42 +02:00
|
|
|
push @hosts, [ $interface, $host->{ipsec} , $net ];
|
2007-03-15 01:42:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-05-07 23:51:19 +02:00
|
|
|
for my $interface ( @interfaces ) {
|
|
|
|
if ( ! $interfaces{$interface}{zone} && $interfaces{$interface}{options}{$option} ) {
|
|
|
|
push @hosts, [ $interface, 'none', ALLIPv4 ];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-15 01:42:37 +01:00
|
|
|
\@hosts;
|
|
|
|
}
|
|
|
|
|
|
|
|
1;
|