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/Accounting.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 that handles the /etc/shorewall/accounting
|
|
|
|
# file.
|
2007-03-15 22:55:22 +01:00
|
|
|
#
|
2007-03-15 16:04:29 +01:00
|
|
|
package Shorewall::Accounting;
|
|
|
|
require Exporter;
|
|
|
|
use Shorewall::Common;
|
|
|
|
use Shorewall::Config;
|
|
|
|
use Shorewall::Zones;
|
|
|
|
use Shorewall::Chains;
|
|
|
|
|
|
|
|
use strict;
|
|
|
|
|
|
|
|
our @ISA = qw(Exporter);
|
|
|
|
our @EXPORT = qw( setup_accounting );
|
|
|
|
our @EXPORT_OK = qw( );
|
|
|
|
our @VERSION = 1.00;
|
|
|
|
|
|
|
|
#
|
|
|
|
# Accounting
|
|
|
|
#
|
|
|
|
my $jumpchainref;
|
|
|
|
|
2007-05-01 19:50:50 +02:00
|
|
|
sub process_accounting_rule( $$$$$$$$$ ) {
|
|
|
|
my ($action, $chain, $source, $dest, $proto, $ports, $sports, $user, $mark ) = @_;
|
2007-03-15 16:04:29 +01:00
|
|
|
|
|
|
|
sub accounting_error() {
|
2007-03-30 04:05:11 +02:00
|
|
|
warning_message "Invalid Accounting rule";
|
2007-03-15 16:04:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sub jump_to_chain( $ ) {
|
|
|
|
my $jumpchain = $_[0];
|
|
|
|
$jumpchainref = ensure_chain( 'filter', $jumpchain );
|
2007-05-05 15:41:36 +02:00
|
|
|
mark_referenced $jumpchainref;
|
2007-03-15 16:04:29 +01:00
|
|
|
"-j $jumpchain";
|
|
|
|
}
|
|
|
|
|
|
|
|
$chain = 'accounting' unless $chain and $chain ne '-';
|
2007-03-27 01:17:46 +02:00
|
|
|
|
2007-03-15 16:04:29 +01:00
|
|
|
my $chainref = ensure_filter_chain $chain , 0;
|
|
|
|
|
|
|
|
my $target = '';
|
|
|
|
|
2007-05-01 20:30:10 +02:00
|
|
|
my $rule = do_proto( $proto, $ports, $sports ) . do_user ( $user ) . do_test ( $mark, 0xFF );
|
2007-03-15 16:04:29 +01:00
|
|
|
my $rule2 = 0;
|
|
|
|
|
|
|
|
unless ( $action eq 'COUNT' ) {
|
|
|
|
if ( $action eq 'DONE' ) {
|
|
|
|
$target = '-j RETURN';
|
|
|
|
} else {
|
|
|
|
( $action, my $cmd ) = split /:/, $action;
|
|
|
|
if ( $cmd ) {
|
|
|
|
if ( $cmd eq 'COUNT' ) {
|
|
|
|
$rule2=1;
|
|
|
|
$target = jump_to_chain $action;
|
|
|
|
} elsif ( $cmd ne 'JUMP' ) {
|
|
|
|
accounting_error;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$target = jump_to_chain $action;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
expand_rule
|
|
|
|
$chainref ,
|
2007-03-18 19:50:34 +01:00
|
|
|
NO_RESTRICT ,
|
2007-03-15 16:04:29 +01:00
|
|
|
$rule ,
|
|
|
|
$source ,
|
|
|
|
$dest ,
|
|
|
|
'' ,
|
|
|
|
$target ,
|
|
|
|
'' ,
|
|
|
|
'' ,
|
|
|
|
'' ;
|
|
|
|
|
|
|
|
if ( $rule2 ) {
|
2007-04-08 16:42:26 +02:00
|
|
|
expand_rule
|
2007-03-15 16:04:29 +01:00
|
|
|
$jumpchainref ,
|
2007-03-18 19:50:34 +01:00
|
|
|
NO_RESTRICT ,
|
2007-03-15 16:04:29 +01:00
|
|
|
$rule ,
|
|
|
|
$source ,
|
|
|
|
$dest ,
|
|
|
|
'' ,
|
|
|
|
'' ,
|
|
|
|
'' ,
|
|
|
|
'' ,
|
|
|
|
'' ;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sub setup_accounting() {
|
2007-04-09 19:34:20 +02:00
|
|
|
|
2007-03-30 17:57:08 +02:00
|
|
|
my $first_entry = 1;
|
|
|
|
|
|
|
|
my $fn = open_file 'accounting';
|
2007-03-15 16:04:29 +01:00
|
|
|
|
2007-03-29 17:47:47 +02:00
|
|
|
while ( read_a_line ) {
|
2007-03-15 16:04:29 +01:00
|
|
|
|
2007-05-01 20:30:10 +02:00
|
|
|
my ( $action, $chain, $source, $dest, $proto, $ports, $sports, $user, $mark ) = split_line 1, 9, 'Accounting File';
|
2007-03-15 16:04:29 +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-04-08 16:42:26 +02:00
|
|
|
|
2007-05-01 20:30:10 +02:00
|
|
|
process_accounting_rule $action, $chain, $source, $dest, $proto, $ports, $sports, $user, $mark;
|
2007-03-15 16:04:29 +01:00
|
|
|
}
|
2007-03-27 01:17:46 +02:00
|
|
|
|
2007-03-15 16:04:29 +01:00
|
|
|
if ( $filter_table->{accounting} ) {
|
2007-04-09 19:34:20 +02:00
|
|
|
for my $chain ( qw/INPUT FORWARD OUTPUT/ ) {
|
2007-03-15 16:04:29 +01:00
|
|
|
insert_rule $filter_table->{$chain}, 1, '-j accounting';
|
2007-04-09 19:34:20 +02:00
|
|
|
insert_rule $filter_table->{$chain}, 2, '-m state --state ESTABLISHED,RELATED -j ACCEPT' if $config{FASTACCEPT};
|
|
|
|
}
|
|
|
|
} elsif ( $config{FASTACCEPT} ) {
|
|
|
|
for my $chain ( qw/INPUT FORWARD OUTPUT/ ) {
|
|
|
|
insert_rule $filter_table->{$chain}, 1, '-m state --state ESTABLISHED,RELATED -j ACCEPT';
|
2007-03-15 16:04:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
1;
|