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/Common.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-03-14 03:44:41 +01:00
|
|
|
package Shorewall::Common;
|
|
|
|
require Exporter;
|
2007-03-15 01:34:17 +01:00
|
|
|
use File::Basename;
|
2007-03-14 05:06:32 +01:00
|
|
|
use File::Temp qw/ tempfile tempdir /;
|
2007-03-17 19:18:54 +01:00
|
|
|
use Cwd 'abs_path';
|
2007-03-14 03:44:41 +01:00
|
|
|
|
2007-03-15 01:34:17 +01:00
|
|
|
use strict;
|
|
|
|
|
2007-03-14 03:44:41 +01:00
|
|
|
our @ISA = qw(Exporter);
|
2007-03-15 01:34:17 +01:00
|
|
|
our @EXPORT = qw(ALLIPv4
|
|
|
|
|
2007-03-14 05:06:32 +01:00
|
|
|
create_temp_object
|
2007-03-15 01:34:17 +01:00
|
|
|
finalize_object
|
2007-03-26 04:53:51 +02:00
|
|
|
emit
|
|
|
|
emitj
|
2007-03-14 03:44:41 +01:00
|
|
|
emit_unindented
|
2007-03-15 04:56:40 +01:00
|
|
|
emit_as_is
|
2007-03-14 03:44:41 +01:00
|
|
|
save_progress_message
|
|
|
|
save_progress_message_short
|
|
|
|
progress_message
|
|
|
|
progress_message2
|
|
|
|
progress_message3
|
|
|
|
push_indent
|
|
|
|
pop_indent
|
|
|
|
copy
|
|
|
|
copy1
|
2007-03-17 19:18:54 +01:00
|
|
|
create_temp_aux_config
|
|
|
|
finalize_aux_config
|
2007-03-27 01:17:46 +02:00
|
|
|
|
2007-03-15 01:34:17 +01:00
|
|
|
@allipv4
|
|
|
|
@rfc1918_networks
|
2007-03-15 02:04:43 +01:00
|
|
|
$line
|
|
|
|
$command
|
|
|
|
$doing
|
|
|
|
$done
|
|
|
|
);
|
2007-03-14 03:44:41 +01:00
|
|
|
our @EXPORT_OK = ();
|
|
|
|
our @VERSION = 1.00;
|
|
|
|
|
2007-03-15 01:34:17 +01:00
|
|
|
#
|
|
|
|
# Some IPv4 useful stuff
|
|
|
|
#
|
|
|
|
our @allipv4 = ( '0.0.0.0/0' );
|
|
|
|
|
|
|
|
use constant { ALLIPv4 => '0.0.0.0/0' };
|
|
|
|
|
|
|
|
our @rfc1918_networks = ( "10.0.0.0/24", "172.16.0.0/12", "192.168.0.0/16" );
|
|
|
|
|
2007-03-14 05:29:14 +01:00
|
|
|
our $line = ''; # Current config file line
|
2007-03-15 02:04:43 +01:00
|
|
|
|
|
|
|
our ( $command, $doing, $done ) = qw/ compile Compiling Compiled/; #describe the current command, it's present progressive, and it's completion.
|
|
|
|
|
2007-03-15 01:34:17 +01:00
|
|
|
my $object = 0; # Object file Handle Reference
|
|
|
|
my $lastlineblank = 0; # Avoid extra blank lines in the output
|
2007-03-14 03:44:41 +01:00
|
|
|
my $indent = '';
|
2007-03-15 01:34:17 +01:00
|
|
|
my ( $dir, $file ); # Object's Directory and File
|
|
|
|
my $tempfile; # Temporary File Name
|
2007-03-14 03:44:41 +01:00
|
|
|
|
|
|
|
#
|
|
|
|
# Fatal Error
|
|
|
|
#
|
|
|
|
sub fatal_error
|
|
|
|
{
|
|
|
|
print STDERR " ERROR: @_\n";
|
2007-03-29 21:45:53 +02:00
|
|
|
|
|
|
|
exit 1;
|
2007-03-14 03:44:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Write the argument to the object file (if any) with the current indentation.
|
|
|
|
#
|
|
|
|
# Replaces leading spaces with tabs as appropriate and suppresses consecutive blank lines.
|
|
|
|
#
|
|
|
|
sub emit ( $ ) {
|
|
|
|
if ( $object ) {
|
2007-03-20 19:18:22 +01:00
|
|
|
#
|
|
|
|
# 'compile' as opposed to 'check'
|
|
|
|
#
|
2007-03-28 21:09:27 +02:00
|
|
|
my $line = $_[0]; # This copy is necessary :-(. We modify the value below and we can't modify subroutine arguments.
|
2007-03-14 03:44:41 +01:00
|
|
|
|
2007-03-16 19:41:08 +01:00
|
|
|
unless ( $line =~ /^\s*$/ ) {
|
|
|
|
$line =~ s/^\n// if $lastlineblank;
|
|
|
|
$line =~ s/^/$indent/gm if $indent;
|
2007-03-27 19:07:43 +02:00
|
|
|
$line =~ s/ /\t/gm;
|
2007-03-14 03:44:41 +01:00
|
|
|
print $object "$line\n";
|
2007-03-16 19:41:08 +01:00
|
|
|
$lastlineblank = ( substr( $line, -1, 1 ) eq "\n" );
|
2007-03-14 03:44:41 +01:00
|
|
|
} else {
|
2007-03-27 18:51:33 +02:00
|
|
|
print $object "\n" unless $lastlineblank;
|
2007-03-14 03:44:41 +01:00
|
|
|
$lastlineblank = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-26 04:57:35 +02:00
|
|
|
#
|
2007-03-27 19:07:43 +02:00
|
|
|
# Version of emit() that accepts an indefinite number of arguments; each argument will be emitted as a separate line
|
2007-03-26 04:57:35 +02:00
|
|
|
#
|
2007-03-26 04:53:51 +02:00
|
|
|
sub emitj {
|
2007-03-26 21:01:38 +02:00
|
|
|
if ( $object ) {
|
2007-03-27 18:51:33 +02:00
|
|
|
#
|
|
|
|
# 'compile' as opposed to 'check'
|
|
|
|
#
|
|
|
|
for ( @_ ) {
|
|
|
|
unless ( /^\s*$/ ) {
|
2007-03-28 21:09:27 +02:00
|
|
|
my $line = $_; # This copy is necessary :-(. We modify the value below and we can't modify subroutine arguments.
|
2007-03-27 18:51:33 +02:00
|
|
|
$line =~ s/^\n// if $lastlineblank;
|
|
|
|
$line =~ s/^/$indent/gm if $indent;
|
2007-03-27 19:07:43 +02:00
|
|
|
$line =~ s/ /\t/g;
|
2007-03-27 18:51:33 +02:00
|
|
|
print $object "$line\n";
|
|
|
|
$lastlineblank = ( substr( $line, -1, 1 ) eq "\n" );
|
|
|
|
} else {
|
|
|
|
print $object "\n" unless $lastlineblank;
|
|
|
|
$lastlineblank = 1;
|
|
|
|
}
|
|
|
|
}
|
2007-03-26 21:01:38 +02:00
|
|
|
}
|
2007-03-26 04:53:51 +02:00
|
|
|
}
|
2007-03-27 01:17:46 +02:00
|
|
|
|
2007-03-26 04:53:51 +02:00
|
|
|
|
2007-03-14 03:44:41 +01:00
|
|
|
#
|
2007-03-27 20:41:55 +02:00
|
|
|
# Write passed message to the object with newline but no indentation.
|
2007-03-14 03:44:41 +01:00
|
|
|
#
|
|
|
|
|
|
|
|
sub emit_unindented( $ ) {
|
|
|
|
print $object "$_[0]\n" if $object;
|
|
|
|
}
|
|
|
|
|
2007-03-20 19:18:22 +01:00
|
|
|
#
|
|
|
|
# Write passed message to the object with no indentation or added newline.
|
|
|
|
#
|
|
|
|
|
2007-03-15 04:56:40 +01:00
|
|
|
sub emit_as_is( $ ) {
|
|
|
|
print $object "$_[0]" if $object;
|
|
|
|
}
|
|
|
|
|
2007-03-14 03:44:41 +01:00
|
|
|
#
|
|
|
|
# Write a progress_message2 command to the output file.
|
|
|
|
#
|
|
|
|
sub save_progress_message( $ ) {
|
2007-03-20 19:18:22 +01:00
|
|
|
emit "\nprogress_message2 @_\n" if $object;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub save_progress_message_short( $ ) {
|
|
|
|
emit "progress_message $_[0]" if $object;
|
2007-03-14 03:44:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sub progress_message {
|
|
|
|
if ( $ENV{VERBOSE} > 1 ) {
|
|
|
|
my $ts = '';
|
|
|
|
$ts = ( localtime ) . ' ' if $ENV{TIMESTAMP};
|
|
|
|
print "${ts}@_\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sub timestamp() {
|
|
|
|
my ($sec, $min, $hr) = ( localtime ) [0,1,2];
|
|
|
|
printf '%02d:%02d:%02d ', $hr, $min, $sec;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub progress_message2 {
|
|
|
|
if ( $ENV{VERBOSE} > 0 ) {
|
|
|
|
timestamp if $ENV{TIMESTAMP};
|
|
|
|
print "@_\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sub progress_message3 {
|
|
|
|
if ( $ENV{VERBOSE} >= 0 ) {
|
|
|
|
timestamp if $ENV{TIMESTAMP};
|
|
|
|
print "@_\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Push/Pop Indent
|
|
|
|
#
|
|
|
|
sub push_indent() {
|
|
|
|
$indent = "$indent ";
|
|
|
|
}
|
|
|
|
|
|
|
|
sub pop_indent() {
|
|
|
|
$indent = substr( $indent , 0 , ( length $indent ) - 4 );
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Functions for copying files into the object
|
|
|
|
#
|
|
|
|
sub copy( $ ) {
|
2007-03-14 05:06:32 +01:00
|
|
|
if ( $object ) {
|
|
|
|
my $file = $_[0];
|
2007-03-27 01:17:46 +02:00
|
|
|
|
2007-03-14 05:06:32 +01:00
|
|
|
open IF , $file or fatal_error "Unable to open $file: $!";
|
2007-03-27 01:17:46 +02:00
|
|
|
|
2007-03-14 05:06:32 +01:00
|
|
|
while ( my $line = <IF> ) {
|
|
|
|
$line =~ s/^/$indent/ if $indent;
|
|
|
|
print $object $line;
|
|
|
|
}
|
2007-03-27 01:17:46 +02:00
|
|
|
|
2007-03-14 05:06:32 +01:00
|
|
|
close IF;
|
2007-03-14 03:44:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sub copy1( $ ) {
|
2007-03-14 05:06:32 +01:00
|
|
|
if ( $object ) {
|
|
|
|
my $file = $_[0];
|
2007-03-27 01:17:46 +02:00
|
|
|
|
2007-03-14 05:06:32 +01:00
|
|
|
open IF , $file or fatal_error "Unable to open $file: $!";
|
2007-03-27 01:17:46 +02:00
|
|
|
|
2007-03-14 05:06:32 +01:00
|
|
|
my $do_indent = 1;
|
|
|
|
|
|
|
|
while ( my $line = <IF> ) {
|
|
|
|
if ( $line =~ /^\s+$/ ) {
|
|
|
|
print $object "\n";
|
|
|
|
$do_indent = 1;
|
|
|
|
next;
|
|
|
|
}
|
2007-03-27 01:17:46 +02:00
|
|
|
|
2007-03-14 05:06:32 +01:00
|
|
|
$line =~ s/^/$indent/ if $indent && $do_indent;
|
|
|
|
print $object $line;
|
|
|
|
$do_indent = ! ( $line =~ /\\$/ );
|
2007-03-14 03:44:41 +01:00
|
|
|
}
|
2007-03-27 01:17:46 +02:00
|
|
|
|
2007-03-14 05:06:32 +01:00
|
|
|
close IF;
|
2007-03-14 03:44:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-27 20:41:55 +02:00
|
|
|
sub create_temp_object( $ ) {
|
|
|
|
my $objectfile = $_[0];
|
|
|
|
my $suffix;
|
|
|
|
|
|
|
|
eval {
|
|
|
|
( $file, $dir, $suffix ) = fileparse( $objectfile );
|
|
|
|
};
|
|
|
|
|
|
|
|
die $@ if $@;
|
|
|
|
|
|
|
|
fatal_error "Directory $dir does not exist" unless -d $dir;
|
|
|
|
fatal_error "$dir is a Symbolic Link" if -l $dir;
|
|
|
|
fatal_error "$objectfile is a Directory" if -d $objectfile;
|
|
|
|
fatal_error "$dir is a Symbolic Link" if -l $objectfile;
|
|
|
|
fatal_error "$objectfile exists and is not a compiled script" if -e _ && ! -x _;
|
|
|
|
|
|
|
|
eval {
|
|
|
|
$dir = abs_path $dir;
|
|
|
|
( $object, $tempfile ) = tempfile ( 'tempfileXXXX' , DIR => $dir );
|
|
|
|
};
|
|
|
|
|
|
|
|
die $@ if $@;
|
|
|
|
|
|
|
|
$file = "$file.$suffix" if $suffix;
|
|
|
|
$dir .= '/' unless substr( $dir, -1, 1 ) eq '/';
|
|
|
|
$file = $dir . $file;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
sub finalize_object() {
|
|
|
|
close $object;
|
|
|
|
$object = 0;
|
|
|
|
rename $tempfile, $file or fatal_error "Cannot Rename $tempfile to $file: $!";
|
|
|
|
chmod 0700, $file;
|
|
|
|
progress_message3 "Shorewall configuration compiled to $file" unless $ENV{EXPORT};
|
|
|
|
}
|
|
|
|
|
2007-03-17 19:18:54 +01:00
|
|
|
sub create_temp_aux_config() {
|
|
|
|
eval {
|
|
|
|
( $object, $tempfile ) = tempfile ( 'tempfileXXXX' , DIR => $dir );
|
|
|
|
};
|
|
|
|
|
|
|
|
die if $@;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
sub finalize_aux_config() {
|
|
|
|
close $object;
|
|
|
|
$object = 0;
|
|
|
|
rename $tempfile, "$file.conf" or fatal_error "Cannot Rename $tempfile to $file.conf: $!";
|
|
|
|
progress_message3 "Shorewall configuration compiled to $file";
|
|
|
|
}
|
|
|
|
|
2007-03-29 22:24:24 +02:00
|
|
|
END {
|
|
|
|
if ( $object ) {
|
|
|
|
close $object;
|
|
|
|
unlink $tempfile;
|
|
|
|
}
|
|
|
|
|
2007-03-29 22:47:14 +02:00
|
|
|
my $exitstatus = $?; #Get's changed by system()
|
|
|
|
|
2007-03-29 22:24:24 +02:00
|
|
|
system "rm -rf $ENV{TMP_DIR}" if $ENV{TMP_DIR};
|
|
|
|
|
|
|
|
$? = $exitstatus;
|
|
|
|
}
|
|
|
|
|
2007-03-14 03:44:41 +01:00
|
|
|
1;
|