IPChains/PortFW.pm

filter: Statementstmt 25.8% 23 / 89 Branchbran 1.7% 1 / 56 Conditioncond 11.1% 1 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 46.1% 6 / 13 Podpod 87.5% 7 / 8 totaltotal 20.9% 38 / 181 CC 34 SCAR 40.9
CC
34
Cov
16%
append
132.0
run_portfw
90.0
delete
42.0
CRAP 59.7
« Fwctl/Services/ftp.pm ↑ Summary Fwctl/AcctReport.pm »
1 #
2 # PortFW.pm - Perl module to interface with ipmasqadm portfw command.
3 #
4 # This file is part of Fwctl.
5 #
6 # Author: Francis J. Lacoste <francis.lacoste@iNsu.COM>
7 #
8 # Copyright (C) 1999,2000 iNsu Innovations Inc.
9 #
10 # This program is free software; you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; either version 2 of the License, or
13 # (at your option) any later version.
14 #
15 package IPChains::PortFW;
16  
17 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use Symbol;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 1 ▶use vars qw( $VERSION $IPMASQADM );
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 1 ($VERSION) = '$Revision: 1.4 $' =~ /Revision: ([0-9.]+)/;
27  
28 }
29  
30 1 my %VALID_OPTIONS = map { $_ => 1 } qw( LocalAddr LocalPort RemAddr RemPort
31 Proto Pref
32 );
33  
34 1 ▶sub new {
Subroutine new: called CC=4 SCAR=18.5
Pod documented
35 1 my $proto = shift;
36 1 ▶ my $class = ref $proto || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
010
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
37 1 my %args = @_;
38 1 my $self = { };
39  
40 # Look for ipmasqadm
41 1 my ($path) = grep { -x "$_/ipmasqadm" } split /:/, "/sbin:/bin:/usr/sbin:/usr/bin:$ENV{PATH}";
42 1 ▶ die ( "Couldn't find ipmasqadm in PATH ($ENV{PATH})\n" ) unless $path;
Branch: 1 branch
BranchTrueFalseTotal
unless $path 1 0 1
43 0 $self->{ipmasqadm} = "$path/ipmasqadm";
44  
45 0 bless $self, $class;
46  
47 0 ▶ while ( my ($key,$value) = each %args ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %args) 0 0 0
48 0 $self->attribute( $key, $value );
49 }
50  
51 0 $self;
52 }
53  
54 0 ▶sub attribute {
Subroutine attribute: not called CC=3 SCAR=24.8
Pod documented
55 0 my ($self,$key,$value) = @_;
56  
57 0 ▶ if ( @_ == 3 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 3) 0 0 0
58 0 ▶ if ( $VALID_OPTIONS{$key} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($VALID_OPTIONS{$key}) { } 0 0 0
59 0 $self->{$key} = $value;
60 } else {
61 0 carp "Unknown option : $key";
62 }
63 }
64  
65 0 return $self->{$key};
66 }
67  
68 0 ▶sub clopts {
Subroutine clopts: not called CC=2 SCAR=17.9
Pod documented
69 0 my ( $self ) = shift;
70  
71 0 foreach my $key ( keys %VALID_OPTIONS ) {
72 0 delete $self->{$key};
73 }
74 }
75  
76 0 ▶sub run_portfw {
Subroutine run_portfw: not called CC=9 SCAR=45.0
Pod undocumented
77 0 my ( $self, @args ) = @_;
78  
79 0 my ($r_fh,$w_fh) = (gensym,gensym);
80 0 ▶ pipe $r_fh, $w_fh
Branch: 1 branch
BranchTrueFalseTotal
pipe $r_fh, $w_fh or die "can't pipe: $!\n" 0 0 0
81 or die "can't pipe: $!\n";
82  
83 0 my $pid = fork;
84 0 ▶ die "can't fork: $!\n" unless defined $pid;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pid 0 0 0
85  
86 0 ▶ if ( $pid ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pid) { } 0 0 0
87 # Don't need this one
88 0 close $w_fh;
89  
90 # Collect STDOUT and STDERR
91 0 my $output;
92 0 ▶ while ( my $line = <$r_fh> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $r_fh)) 0 0 0
93 0 $output .= $line;
94 }
95  
96 # Collect exit status
97 0 waitpid $pid,0;
98  
99 0 ▶ die "ipmasq exit with non zero status:\n$output\n" if $?;
Branch: 1 branch
BranchTrueFalseTotal
if $? 0 0 0
100  
101 0 $output;
102 } else {
103 # Don't need this one
104 0 close $r_fh;
105  
106 # Redirect STDOUT and STDERR to parent
107 0 ▶ open ( STDOUT, ">&" . fileno $w_fh )
Branch: 1 branch
BranchTrueFalseTotal
open STDOUT, '>&' . fileno($w_fh) or die "can't redirect STDOUT to proper pipe: $!\n" 0 0 0
108 or die "can't redirect STDOUT to proper pipe: $!\n";
109 0 ▶ open ( STDERR, ">&" . fileno $w_fh )
Branch: 1 branch
BranchTrueFalseTotal
open STDERR, '>&' . fileno($w_fh) or die "can't redirect STDERR to proper output fd: $!\n" 0 0 0
110 or die "can't redirect STDERR to proper output fd: $!\n";
111 0 ▶ exec( $self->{ipmasqadm}, "portfw", @args )
Branch: 1 branch
BranchTrueFalseTotal
exec $self->{"ipmasqadm"}, "portfw", @args or die "can't exec ipmasqadm: $!" 0 0 0
112 or die "can't exec ipmasqadm: $!";
113 }
114 }
115  
116 0 ▶sub append {
Subroutine append: not called CC=11 SCAR=48.8
Pod documented
117 0 my ( $self ) = shift;
118  
119 0 my @args = ( "-a" );
120 0 ▶ croak "missing protocol" unless exists $self->{Proto};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"Proto"} 0 0 0
121 0 ▶ croak "invalid protocol" unless $self->{Proto} =~ /udp|tcp|6|17/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"Proto"} =~ /udp|tcp|6|17/i 0 0 0
122 0 ▶ croak "missing local address" unless exists $self->{LocalAddr};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"LocalAddr"} 0 0 0
123 0 ▶ croak "missing local port" unless exists $self->{LocalPort};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"LocalPort"} 0 0 0
124 0 ▶ croak "missing remote address" unless exists $self->{RemAddr};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"RemAddr"} 0 0 0
125 0 ▶ croak "missing remote port" unless exists $self->{RemPort};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"RemPort"} 0 0 0
126 0 ▶ if ( exists $self->{Pref} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"Pref"}) 0 0 0
127 croak "invalid preference" unless $self->{Pref} =~ /\d+/ &&
128 0 ▶ $self->{Pref} >= 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"Pref"} =~ /\d+/ and $self->{"Pref"} >= 0 0 0 0
Condition0%0/3
$self->{"Pref"} =~ /\d+/ and $self->{"Pref"} >= 0
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"Pref"} =~ /\d+/ and $self->{"Pref"} >= 0
$self->{"Pref"} =~ /\d+/ $self->{"Pref"} >= 0
Truth table0%0/3
A and B
A= $self->{"Pref"} =~ /\d+/B= $self->{"Pref"} >= 0
ABresult
0X0
100
111
129 }
130  
131 push @args, "-P", lc $self->{Proto}, "-L", $self->{LocalAddr},
132 0 $self->{LocalPort}, "-R", $self->{RemAddr}, $self->{RemPort};
133 0 ▶ push @args, "-p", $self->{Pref} if exists $self->{Pref};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"Pref"} 0 0 0
134  
135 0 $self->run_portfw( @args );
136 }
137  
138 0 ▶sub delete {
Subroutine delete: not called CC=6 SCAR=37.4
Pod documented
139 0 my ( $self ) = shift;
140  
141 0 my @args = ( "-d" );
142 0 ▶ croak "missing protocol" unless exists $self->{Proto};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"Proto"} 0 0 0
143 0 ▶ croak "invalid protocol" unless $self->{Proto} =~ /udp|tcp|6|17/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"Proto"} =~ /udp|tcp|6|17/i 0 0 0
144 0 ▶ croak "missing local address" unless exists $self->{LocalAddr};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"LocalAddr"} 0 0 0
145 0 ▶ croak "missing local port" unless exists $self->{LocalPort};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"LocalPort"} 0 0 0
146  
147 push @args, "-P", lc $self->{Proto}, "-L", $self->{LocalAddr},
148 0 $self->{LocalPort};
149 push @args, "-R", $self->{RemAddr}, $self->{RemPort}
150 0 ▶ if exists $self->{RemAddr};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"RemAddr"} 0 0 0
151  
152 0 $self->run_portfw( @args );
153  
154 }
155  
156 0 ▶sub flush {
Subroutine flush: not called CC=1 SCAR=6.9
Pod documented
157 0 $_[0]->run_portfw( "-f" );
158 }
159  
160 0 ▶sub list {
Subroutine list: not called CC=5 SCAR=34.0
Pod documented
161 0 my ($self, $use_dns) = @_;
162  
163 0 my @args = ( "-l" );
164 0 ▶ push @args, "-n" unless defined $use_dns && $use_dns;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $use_dns and $use_dns 0 0 0
Condition0%0/3
defined $use_dns and $use_dns
!ll&&!rl&&r
000
MC/DC0%0/2
defined $use_dns and $use_dns
defined $use_dns $use_dns
Truth table0%0/3
A and B
A= defined $use_dnsB= $use_dns
ABresult
0X0
100
111
165  
166 0 my $output = $self->run_portfw( @args );
167 0 ▶ return () unless defined $output;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $output 0 0 0
168  
169 # Parse output
170 0 my @lines = split /\n/, $output;
171  
172 # Skip header line
173 0 shift @lines;
174  
175 0 my @rules = ();
176 0 foreach my $line ( @lines ) {
177 0 my ( $prot, $laddr, $raddr, $lport, $rport, $ignored, $pref ) =
178 split / +/, $line;
179 0 push @rules, $self->new( Proto => lc $prot,
180 LocalAddr => $laddr,
181 RemAddr => $raddr,
182 LocalPort => $lport,
183 RemPort => $rport,
184 );
185 }
186  
187 0 @rules;
188 }
189  
190 1 1;
191  
192 __END__
« Fwctl/Services/ftp.pm ↑ Summary Fwctl/AcctReport.pm »