From 2de234316b897570f2e7de920d22561b24d13ef8 Mon Sep 17 00:00:00 2001 From: teastep Date: Thu, 29 Mar 2007 15:47:47 +0000 Subject: [PATCH] Convert the Accounting and Actions modules to use the new INCLUDE-aware file read routines git-svn-id: https://shorewall.svn.sourceforge.net/svnroot/shorewall/trunk@5743 fbd18981-670d-0410-9b5c-8dc0c1a9a2bb --- New/Shorewall/Accounting.pm | 6 ++--- New/Shorewall/Actions.pm | 31 ++++++++++--------------- New/Shorewall/Config.pm | 46 +++++++++++++++++++++++++++++++++---- New/Shorewall/Zones.pm | 6 ++--- 4 files changed, 57 insertions(+), 32 deletions(-) diff --git a/New/Shorewall/Accounting.pm b/New/Shorewall/Accounting.pm index 9e1337b3a..7dbf4bbfc 100644 --- a/New/Shorewall/Accounting.pm +++ b/New/Shorewall/Accounting.pm @@ -110,17 +110,15 @@ sub process_accounting_rule( $$$$$$$$ ) { sub setup_accounting() { - open ACC, "$ENV{TMP_DIR}/accounting" or fatal_error "Unable to open stripped accounting file: $!"; + open_file 'accounting'; - while ( $line = ) { + while ( read_a_line ) { my ( $action, $chain, $source, $dest, $proto, $ports, $sports, $user ) = split_line 8, 'Accounting File'; process_accounting_rule $action, $chain, $source, $dest, $proto, $ports, $sports, $user; } - close ACC; - if ( $filter_table->{accounting} ) { for my $chain qw/INPUT FORWARD OUTPUT/ { insert_rule $filter_table->{$chain}, 1, '-j accounting'; diff --git a/New/Shorewall/Actions.pm b/New/Shorewall/Actions.pm index a9b3d0be9..e876c96b1 100644 --- a/New/Shorewall/Actions.pm +++ b/New/Shorewall/Actions.pm @@ -243,11 +243,11 @@ sub process_actions1() { for my $act ( grep $targets{$_} & ACTION , keys %targets ) { new_action $act; } + + for my $file ( qw/actions.std actions/ ) { + open_file $file; - for my $file qw/actions.std actions/ { - open F, "$ENV{TMP_DIR}/$file" or fatal_error "Unable to open stripped $file file: $!"; - - while ( $line = ) { + while ( read_a_line ) { my ( $action ) = split_line 1, 'action file'; if ( $action =~ /:/ ) { @@ -274,13 +274,9 @@ sub process_actions1() { progress_message2 " Pre-processing $actionfile..."; - open A, $actionfile or fatal_error "Unable to open $actionfile: $!"; + push_open( $actionfile ); - while ( $line = ) { - chomp $line; - next if $line =~ /^\s*#/; - next if $line =~ /^\s*$/; - $line =~ s/#.*$//; + while ( read_a_line ) { my ($wholetarget, $source, $dest, $proto, $ports, $sports, $rate, $users ) = split_line 8, 'action file'; @@ -304,13 +300,9 @@ sub process_actions1() { progress_message " ..Expanding Macro $macrofile..."; - open M, $macrofile or fatal_error "Unable to open $macrofile: $!"; - - while ( $line = ) { - next if $line =~ /^\s*#/; - $line =~ s/#.*$//; - next if $line =~ /^\s*$/; + push_open( $macrofile ); + while ( read_a_line ) { my ( $mtarget, $msource, $mdest, $mproto, $mports, $msports, $ mrate, $muser ) = split_line 8, 'macro file'; $mtarget =~ s/:.*$//; @@ -324,15 +316,16 @@ sub process_actions1() { } progress_message " ..End Macro"; - close M; + + pop_open; } else { fatal_error "Invalid TARGET ($target) in rule \"$line\""; } } } - close A; + + pop_open; } - close F; } } diff --git a/New/Shorewall/Config.pm b/New/Shorewall/Config.pm index 28ab08fc7..454adcd88 100644 --- a/New/Shorewall/Config.pm +++ b/New/Shorewall/Config.pm @@ -28,7 +28,11 @@ use warnings; use Shorewall::Common; our @ISA = qw(Exporter); -our @EXPORT = qw(find_file +our @EXPORT = ( qw(find_file + open_file + push_open + pop_open + read_a_line expand_shell_variables get_configuration require_capability @@ -40,7 +44,7 @@ our @EXPORT = qw(find_file %config %env - %capabilities ); + %capabilities ) ); our @EXPORT_OK = (); our @VERSION = 1.00; @@ -283,8 +287,42 @@ sub expand_shell_variables( $ ) { my @openstack; my $currentfile; +# +# Open a file, setting $currentfile. +# +sub open_file( $ ) { + my $fname = find_file $_[0]; + + fatal_error 'Internal Error in open_file()' if defined $currentfile; + + if ( -f $fname ) { + open $currentfile, '<', $fname or fatal_error "Unable to open $fname: $!"; + } +} + +# +# Allow nested opens +# +my @pushstack; + +sub push_open( $ ) { + + push @openstack, $currentfile; + my @a = @openstack; + push @pushstack, \@a; + @openstack = (); + $currentfile = undef; + open_file( $_[0] ); + +} + +sub pop_open() { + @openstack = @{pop @pushstack}; + $currentfile = pop @openstack; +} + sub read_a_line { - while ( 1 ) { + while ( $currentfile ) { while ( $line = <$currentfile> ) { next if $line =~ /^\s*#/; next if $line =~ /^\s*$/; @@ -322,8 +360,6 @@ sub read_a_line { close $currentfile; - return 0 unless @openstack; - $currentfile = pop @openstack; } } diff --git a/New/Shorewall/Zones.pm b/New/Shorewall/Zones.pm index 00b81a304..6e22ca625 100644 --- a/New/Shorewall/Zones.pm +++ b/New/Shorewall/Zones.pm @@ -180,9 +180,9 @@ sub determine_zones() { my @z; - open ZONES, "$ENV{TMP_DIR}/zones" or fatal_error "Unable to open stripped zones file: $!"; + open_file 'zones'; - while ( $line = ) { + while ( read_a_line ) { my @parents; @@ -244,8 +244,6 @@ sub determine_zones() push @z, $zone; } - close ZONES; - my $pushed = 1; my %ordered;