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/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-04-19 01:43:54 +02:00
|
|
|
# This is the lowes level Shorewall module. It provides very basic
|
|
|
|
# services such as creation of temporary 'object' files, writing
|
|
|
|
# into those files (emitters) and finalizing those files (renaming
|
|
|
|
# them to their final name and setting their mode appropriately).
|
2007-03-15 22:55:22 +01:00
|
|
|
#
|
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-05-11 17:39:11 +02:00
|
|
|
our @EXPORT = qw(
|
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
|
|
|
|
save_progress_message
|
|
|
|
save_progress_message_short
|
2007-06-12 01:17:02 +02:00
|
|
|
set_timestamp
|
|
|
|
set_verbose
|
2007-03-14 03:44:41 +01:00
|
|
|
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 02:04:43 +01:00
|
|
|
$line
|
|
|
|
$command
|
|
|
|
$doing
|
|
|
|
$done
|
2007-04-18 22:56:41 +02:00
|
|
|
$verbose
|
2007-03-15 02:04:43 +01:00
|
|
|
);
|
2007-06-14 01:02:39 +02:00
|
|
|
our @EXPORT_OK = qw( $timestamp initialize );
|
2007-06-21 16:40:40 +02:00
|
|
|
our $VERSION = 1.00;
|
2007-03-14 03:44:41 +01:00
|
|
|
|
2007-06-14 01:02:39 +02:00
|
|
|
our $line;
|
|
|
|
our ($command, $doing, $done );
|
|
|
|
our $verbose;
|
|
|
|
our $timestamp;
|
|
|
|
our $object;
|
|
|
|
our $lastlineblank;
|
|
|
|
our $indent;
|
2007-06-05 18:49:13 +02:00
|
|
|
our ( $dir, $file ); # Object's Directory and File
|
|
|
|
our $tempfile; # Temporary File Name
|
2007-03-14 03:44:41 +01: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() {
|
|
|
|
$line = ''; # Current config file line
|
|
|
|
|
|
|
|
( $command, $doing, $done ) = qw/ compile Compiling Compiled/; #describe the current command, it's present progressive, and it's completion.
|
|
|
|
|
|
|
|
$verbose = 0; # Verbosity setting. 0 = almost silent, 1 = major progress messages only, 2 = all progress messages (very noisy)
|
|
|
|
$timestamp = ''; # If true, we are to timestamp each progress message
|
|
|
|
$object = 0; # Object (script) file Handle Reference
|
|
|
|
$lastlineblank = 0; # Avoid extra blank lines in the output
|
|
|
|
$indent = ''; # Current indentation
|
|
|
|
( $dir, $file ) = ('',''); # Object's Directory and File
|
|
|
|
$tempfile = ''; # Temporary File Name
|
|
|
|
}
|
|
|
|
|
|
|
|
INIT {
|
|
|
|
initialize;
|
|
|
|
}
|
|
|
|
|
2007-03-14 03:44:41 +01:00
|
|
|
#
|
|
|
|
# Fatal Error
|
|
|
|
#
|
|
|
|
sub fatal_error
|
|
|
|
{
|
2007-06-12 01:17:02 +02:00
|
|
|
die " ERROR: @_\n";
|
2007-03-14 03:44:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Write the argument to the object file (if any) with the current indentation.
|
2007-04-08 16:42:26 +02:00
|
|
|
#
|
2007-03-14 03:44:41 +01:00
|
|
|
# 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-31 18:58:14 +02:00
|
|
|
my $line = $_[0]; # This copy is necessary because the actual arguments are almost always read-only.
|
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-31 18:08:59 +02:00
|
|
|
# Version of emit() that accepts an indefinite number of scalar 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-31 18:40:43 +02:00
|
|
|
my $line = $_; # This copy is necessary because the actual arguments are almost always read-only.
|
2007-03-27 18:51:33 +02:00
|
|
|
$line =~ s/^\n// if $lastlineblank;
|
|
|
|
$line =~ s/^/$indent/gm if $indent;
|
2007-05-26 04:57:27 +02:00
|
|
|
$line =~ s/ /\t/gm;
|
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-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
|
|
|
#
|
2007-04-16 21:04:13 +02:00
|
|
|
# Write a progress_message2 command with surrounding blank lines to the output file.
|
2007-03-14 03:44:41 +01:00
|
|
|
#
|
|
|
|
sub save_progress_message( $ ) {
|
2007-03-20 19:18:22 +01:00
|
|
|
emit "\nprogress_message2 @_\n" if $object;
|
|
|
|
}
|
|
|
|
|
2007-04-16 21:04:13 +02:00
|
|
|
#
|
|
|
|
# Write a progress_message command to the output file.
|
|
|
|
#
|
2007-03-20 19:18:22 +01:00
|
|
|
sub save_progress_message_short( $ ) {
|
|
|
|
emit "progress_message $_[0]" if $object;
|
2007-03-14 03:44:41 +01:00
|
|
|
}
|
|
|
|
|
2007-06-12 01:17:02 +02:00
|
|
|
#
|
|
|
|
# Set $timestamp
|
|
|
|
#
|
|
|
|
sub set_timestamp( $ ) {
|
|
|
|
$timestamp = shift;
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Set $verbose
|
|
|
|
#
|
|
|
|
sub set_verbose( $ ) {
|
|
|
|
$verbose = shift;
|
|
|
|
}
|
|
|
|
|
2007-04-16 21:04:13 +02:00
|
|
|
#
|
|
|
|
# Print the current TOD to STDOUT.
|
|
|
|
#
|
2007-03-14 03:44:41 +01:00
|
|
|
sub timestamp() {
|
|
|
|
my ($sec, $min, $hr) = ( localtime ) [0,1,2];
|
|
|
|
printf '%02d:%02d:%02d ', $hr, $min, $sec;
|
|
|
|
}
|
|
|
|
|
2007-04-16 21:04:13 +02:00
|
|
|
#
|
|
|
|
# Write a message if $verbose >= 2
|
|
|
|
#
|
2007-03-31 01:21:08 +02:00
|
|
|
sub progress_message {
|
|
|
|
if ( $verbose > 1 ) {
|
2007-03-31 19:29:17 +02:00
|
|
|
timestamp if $timestamp;
|
2007-03-31 01:29:13 +02:00
|
|
|
my $line = join( ' ', @_ );
|
|
|
|
$line =~ s/\s+/ /g;
|
|
|
|
print "$line\n";
|
2007-03-31 01:21:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-04-16 21:04:13 +02:00
|
|
|
#
|
|
|
|
# Write a message if $verbose >= 1
|
|
|
|
#
|
2007-03-14 03:44:41 +01:00
|
|
|
sub progress_message2 {
|
2007-03-31 01:21:08 +02:00
|
|
|
if ( $verbose > 0 ) {
|
2007-03-31 19:29:17 +02:00
|
|
|
timestamp if $timestamp;
|
2007-03-14 03:44:41 +01:00
|
|
|
print "@_\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-04-16 21:04:13 +02:00
|
|
|
#
|
|
|
|
# Write a message if $verbose >= 0
|
|
|
|
#
|
2007-03-14 03:44:41 +01:00
|
|
|
sub progress_message3 {
|
2007-03-31 01:21:08 +02:00
|
|
|
if ( $verbose >= 0 ) {
|
2007-03-31 19:29:17 +02:00
|
|
|
timestamp if $timestamp;
|
2007-03-14 03:44:41 +01:00
|
|
|
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-31 02:27:36 +02:00
|
|
|
while ( <IF> ) {
|
|
|
|
s/^/$indent/ if $indent;
|
|
|
|
print $object $_;
|
2007-03-14 05:06:32 +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-04-16 21:04:13 +02:00
|
|
|
#
|
|
|
|
# This one handles line continuation.
|
|
|
|
|
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;
|
|
|
|
|
2007-03-31 02:27:36 +02:00
|
|
|
while ( <IF> ) {
|
2007-03-31 18:08:59 +02:00
|
|
|
if ( /^\s*$/ ) {
|
2007-03-14 05:06:32 +01:00
|
|
|
print $object "\n";
|
|
|
|
$do_indent = 1;
|
|
|
|
next;
|
|
|
|
}
|
2007-03-27 01:17:46 +02:00
|
|
|
|
2007-03-31 02:27:36 +02:00
|
|
|
s/^/$indent/ if $indent && $do_indent;
|
|
|
|
print $object $_;
|
|
|
|
$do_indent = ! ( /\\$/ );
|
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-04-16 21:04:13 +02:00
|
|
|
#
|
|
|
|
# Create the temporary object file -- the passed file name is the name of the final file.
|
|
|
|
# We create a temporary file in the same directory so that we can use rename to finalize it.
|
|
|
|
#
|
2007-03-27 20:41:55 +02:00
|
|
|
sub create_temp_object( $ ) {
|
|
|
|
my $objectfile = $_[0];
|
|
|
|
my $suffix;
|
|
|
|
|
|
|
|
eval {
|
|
|
|
( $file, $dir, $suffix ) = fileparse( $objectfile );
|
|
|
|
};
|
|
|
|
|
2007-05-26 04:57:27 +02:00
|
|
|
die if $@;
|
2007-03-27 20:41:55 +02:00
|
|
|
|
2007-05-26 04:57:27 +02:00
|
|
|
fatal_error "Directory $dir does not exist" unless -d $dir;
|
|
|
|
fatal_error "Directory $dir is not writable" unless -w _;
|
|
|
|
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;
|
2007-03-27 20:41:55 +02:00
|
|
|
fatal_error "$objectfile exists and is not a compiled script" if -e _ && ! -x _;
|
|
|
|
|
|
|
|
eval {
|
|
|
|
$dir = abs_path $dir;
|
|
|
|
( $object, $tempfile ) = tempfile ( 'tempfileXXXX' , DIR => $dir );
|
|
|
|
};
|
|
|
|
|
2007-05-26 04:57:27 +02:00
|
|
|
fatal_error "Unable to create temporary file in directory $dir" if $@;
|
2007-03-27 20:41:55 +02:00
|
|
|
|
|
|
|
$file = "$file.$suffix" if $suffix;
|
|
|
|
$dir .= '/' unless substr( $dir, -1, 1 ) eq '/';
|
|
|
|
$file = $dir . $file;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2007-04-16 21:04:13 +02:00
|
|
|
#
|
|
|
|
# Finalize the object file
|
|
|
|
#
|
2007-03-31 19:29:17 +02:00
|
|
|
sub finalize_object( $ ) {
|
|
|
|
my $export = $_[0];
|
2007-03-27 20:41:55 +02:00
|
|
|
close $object;
|
|
|
|
$object = 0;
|
|
|
|
rename $tempfile, $file or fatal_error "Cannot Rename $tempfile to $file: $!";
|
|
|
|
chmod 0700, $file;
|
2007-03-31 19:29:17 +02:00
|
|
|
progress_message3 "Shorewall configuration compiled to $file" unless $export;
|
2007-03-27 20:41:55 +02:00
|
|
|
}
|
|
|
|
|
2007-04-16 21:04:13 +02:00
|
|
|
#
|
|
|
|
# Create the temporary aux config file.
|
|
|
|
#
|
2007-03-17 19:18:54 +01:00
|
|
|
sub create_temp_aux_config() {
|
|
|
|
eval {
|
|
|
|
( $object, $tempfile ) = tempfile ( 'tempfileXXXX' , DIR => $dir );
|
|
|
|
};
|
|
|
|
|
|
|
|
die if $@;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2007-04-16 21:04:13 +02:00
|
|
|
#
|
|
|
|
# Finalize the aux config file.
|
|
|
|
#
|
2007-03-17 19:18:54 +01:00
|
|
|
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-14 03:44:41 +01:00
|
|
|
1;
|