lib/Rex/Resource/firewall/Provider/iptables.pm

filter: Statementstmt 21.3% 19 / 89 Branchbran 0.0% 0 / 74 Conditioncond 0.0% 0 / 39 MC/DCmcdc 0.0% 0 / 20 Subroutinesub 60.0% 6 / 10 Podpod 0.0% 0 / 3 totaltotal 10.6% 25 / 235 CC 53 SCAR 63.8
CC
53
Cov
9%
present
702.0
absent
702.0
new
6.0
CRAP 589.3
« lib/Rex/Inventory/HP/ACU.pm ↑ Summary lib/Rex/Service.pm »
1 #
2 # (c) Jan Gehring <jan.gehring@gmail.com>
3 #
4  
5 package Rex::Resource::firewall::Provider::iptables;
6  
7 1 ▶use v5.14.4;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 our $VERSION = '1.16.1'; # VERSION
11  
12 1 ▶use Rex::Commands::Iptables;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Rex::Helper::Run;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use base qw(Rex::Resource::firewall::Provider::base);
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
Pod undocumented
18 0 my $that = shift;
19 0 ▶ my $proto = ref($that) || $that;
Condition0%0/3
ref $that || $that
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $that || $that
ref $that $that
Truth table0%0/3
A || B
A= ref $thatB= $that
ABresult
1X1
011
000
20 0 my $self = $proto->SUPER::new(@_);
21  
22 0 bless( $self, $proto );
23  
24 0 return $self;
25 }
26  
27 0 ▶sub present {
Subroutine present: not called CC=26 SCAR=65.5
Pod undocumented
28 0 my ( $self, $rule_config ) = @_;
29  
30 0 my @iptables_rule = ();
31  
32 0 ▶ $rule_config->{dport} ||= $rule_config->{port};
Condition0%0/3
$rule_config->{"dport"} ||= $rule_config->{"port"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$rule_config->{"dport"} ||= $rule_config->{"port"}
$rule_config->{"dport"} $rule_config->{"port"}
Truth table0%0/3
A ||= B
A= $rule_config->{"dport"}B= $rule_config->{"port"}
ABresult
1X1
011
000
33 0 ▶ $rule_config->{proto} ||= 'tcp';
Condition0%0/2
$rule_config->{"proto"} ||= "tcp"
l!l
00
MC/DC0%0/1
$rule_config->{"proto"} ||= "tcp"
$rule_config->{"proto"}
Truth table0%0/2
A
A= $rule_config->{"proto"}
Aresult
11
00
34 0 ▶ $rule_config->{chain} ||= 'INPUT';
Condition0%0/2
$rule_config->{"chain"} ||= "INPUT"
l!l
00
MC/DC0%0/1
$rule_config->{"chain"} ||= "INPUT"
$rule_config->{"chain"}
Truth table0%0/2
A
A= $rule_config->{"chain"}
Aresult
11
00
35 0 ▶ $rule_config->{ip_version} ||= -4;
Condition0%0/2
$rule_config->{"ip_version"} ||= -4
l!l
00
MC/DC0%0/1
$rule_config->{"ip_version"} ||= -4
$rule_config->{"ip_version"}
Truth table0%0/2
A
A= $rule_config->{"ip_version"}
Aresult
11
00
36  
37 0 ▶ if ( $rule_config->{source}
Branch: 1 branch
BranchTrueFalseTotal
if ($rule_config->{"source"} and not $rule_config->{"source"} =~ m[/(\d+)$]u and $self->_version->[0] >= 1 and $self->_version->[1] >= 4) 0 0 0
Condition0%0/9
$rule_config->{"source"} and not $rule_config->{"source"} =~ m[/(\d+)$]u and $self->_version->[0] >= 1 and $self->_version->[1] >= 4
!ll&&!rl&&r
000
$rule_config->{"source"} and not $rule_config->{"source"} =~ m[/(\d+)$]u and $self->_version->[0] >= 1
!ll&&!rl&&r
000
$rule_config->{"source"} and not $rule_config->{"source"} =~ m[/(\d+)$]u
!ll&&!rl&&r
000
MC/DC0%0/4
$rule_config->{"source"} and not $rule_config->{"source"} =~ m[/(\d+)$]u and $self->_version->[0] >= 1 and $self->_version->[1] >= 4
$rule_config->{"source"} not $rule_config->{"source"} =~ m[/(\d+)$]u $self->_version->[0] >= 1 $self->_version->[1] >= 4
Truth table0%0/5
A and not(B) and C and D
A= $rule_config->{"source"}B= not $rule_config->{"source"} =~ m[/(\d+)$]uC= $self->_version->[0] >= 1D= $self->_version->[1] >= 4
ABCDresult
0XXX0
10XX0
110X0
11100
11111
38 && $rule_config->{source} !~ m/\/(\d+)$/
39 && $self->_version()->[0] >= 1
40 && $self->_version()->[1] >= 4 )
41 {
42 0 $rule_config->{source} .= "/32";
43 }
44  
45 push( @iptables_rule, t => $rule_config->{table} )
46 0 ▶ if ( defined $rule_config->{table} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $rule_config->{"table"} 0 0 0
47 push( @iptables_rule, A => uc( $rule_config->{chain} ) )
48 0 ▶ if ( defined $rule_config->{chain} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $rule_config->{"chain"} 0 0 0
49 push( @iptables_rule, p => $rule_config->{proto} )
50 0 ▶ if ( defined $rule_config->{proto} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $rule_config->{"proto"} 0 0 0
51 push( @iptables_rule, m => $rule_config->{proto} )
52 0 ▶ if ( defined $rule_config->{proto} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $rule_config->{"proto"} 0 0 0
53 push( @iptables_rule, s => $rule_config->{source} )
54 0 ▶ if ( defined $rule_config->{source} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $rule_config->{"source"} 0 0 0
55 push( @iptables_rule, d => $rule_config->{destination} )
56 0 ▶ if ( defined $rule_config->{destination} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $rule_config->{"destination"} 0 0 0
57 push( @iptables_rule, sport => $rule_config->{sport} )
58 0 ▶ if ( defined $rule_config->{sport} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $rule_config->{"sport"} 0 0 0
59 push( @iptables_rule, dport => $rule_config->{dport} )
60 0 ▶ if ( defined $rule_config->{dport} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $rule_config->{"dport"} 0 0 0
61 push( @iptables_rule, "tcp-flags" => $rule_config->{tcp_flags} )
62 0 ▶ if ( defined $rule_config->{tcp_flags} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $rule_config->{"tcp_flags"} 0 0 0
63 push( @iptables_rule, "i" => $rule_config->{iniface} )
64 0 ▶ if ( defined $rule_config->{iniface} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $rule_config->{"iniface"} 0 0 0
65 push( @iptables_rule, "o" => $rule_config->{outiface} )
66 0 ▶ if ( defined $rule_config->{outiface} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $rule_config->{"outiface"} 0 0 0
67 push( @iptables_rule, "reject-with" => $rule_config->{reject_with} )
68 0 ▶ if ( defined $rule_config->{reject_with} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $rule_config->{"reject_with"} 0 0 0
69 push( @iptables_rule, "log-level" => $rule_config->{log_level} )
70 0 ▶ if ( defined $rule_config->{log_level} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $rule_config->{"log_level"} 0 0 0
71 push( @iptables_rule, "log-prefix" => $rule_config->{log_prefix} )
72 0 ▶ if ( defined $rule_config->{log_prefix} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $rule_config->{"log_prefix"} 0 0 0
73 push( @iptables_rule, "state" => $rule_config->{state} )
74 0 ▶ if ( defined $rule_config->{state} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $rule_config->{"state"} 0 0 0
75 push( @iptables_rule, j => uc( $rule_config->{action} ) )
76 0 ▶ if ( defined $rule_config->{action} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $rule_config->{"action"} 0 0 0
77  
78 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
unless (Rex::Commands::Iptables::_rule_exists($rule_config->{"ip_version"}, @iptables_rule)) 0 0 0
79 !Rex::Commands::Iptables::_rule_exists(
80 $rule_config->{ip_version},
81 @iptables_rule
82 )
83 )
84 {
85 0 iptables( $rule_config->{ip_version}, @iptables_rule );
86 0 return 1;
87 }
88  
89 0 return 0;
90 }
91  
92 0 ▶sub absent {
Subroutine absent: not called CC=26 SCAR=65.5
Pod undocumented
93 0 my ( $self, $rule_config ) = @_;
94  
95 0 my @iptables_rule = ();
96  
97 0 ▶ $rule_config->{dport} ||= $rule_config->{port};
Condition0%0/3
$rule_config->{"dport"} ||= $rule_config->{"port"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$rule_config->{"dport"} ||= $rule_config->{"port"}
$rule_config->{"dport"} $rule_config->{"port"}
Truth table0%0/3
A ||= B
A= $rule_config->{"dport"}B= $rule_config->{"port"}
ABresult
1X1
011
000
98 0 ▶ $rule_config->{proto} ||= 'tcp';
Condition0%0/2
$rule_config->{"proto"} ||= "tcp"
l!l
00
MC/DC0%0/1
$rule_config->{"proto"} ||= "tcp"
$rule_config->{"proto"}
Truth table0%0/2
A
A= $rule_config->{"proto"}
Aresult
11
00
99 0 ▶ $rule_config->{chain} ||= 'INPUT';
Condition0%0/2
$rule_config->{"chain"} ||= "INPUT"
l!l
00
MC/DC0%0/1
$rule_config->{"chain"} ||= "INPUT"
$rule_config->{"chain"}
Truth table0%0/2
A
A= $rule_config->{"chain"}
Aresult
11
00
100  
101 0 ▶ $rule_config->{ip_version} ||= -4;
Condition0%0/2
$rule_config->{"ip_version"} ||= -4
l!l
00
MC/DC0%0/1
$rule_config->{"ip_version"} ||= -4
$rule_config->{"ip_version"}
Truth table0%0/2
A
A= $rule_config->{"ip_version"}
Aresult
11
00
102  
103 0 ▶ if ( $rule_config->{source}
Branch: 1 branch
BranchTrueFalseTotal
if ($rule_config->{"source"} and not $rule_config->{"source"} =~ m[/(\d+)$]u and $self->_version->[0] >= 1 and $self->_version->[1] >= 4) 0 0 0
Condition0%0/9
$rule_config->{"source"} and not $rule_config->{"source"} =~ m[/(\d+)$]u and $self->_version->[0] >= 1 and $self->_version->[1] >= 4
!ll&&!rl&&r
000
$rule_config->{"source"} and not $rule_config->{"source"} =~ m[/(\d+)$]u and $self->_version->[0] >= 1
!ll&&!rl&&r
000
$rule_config->{"source"} and not $rule_config->{"source"} =~ m[/(\d+)$]u
!ll&&!rl&&r
000
MC/DC0%0/4
$rule_config->{"source"} and not $rule_config->{"source"} =~ m[/(\d+)$]u and $self->_version->[0] >= 1 and $self->_version->[1] >= 4
$rule_config->{"source"} not $rule_config->{"source"} =~ m[/(\d+)$]u $self->_version->[0] >= 1 $self->_version->[1] >= 4
Truth table0%0/5
A and not(B) and C and D
A= $rule_config->{"source"}B= not $rule_config->{"source"} =~ m[/(\d+)$]uC= $self->_version->[0] >= 1D= $self->_version->[1] >= 4
ABCDresult
0XXX0
10XX0
110X0
11100
11111
104 && $rule_config->{source} !~ m/\/(\d+)$/
105 && $self->_version()->[0] >= 1
106 && $self->_version()->[1] >= 4 )
107 {
108 0 $rule_config->{source} .= "/32";
109 }
110  
111 push( @iptables_rule, t => $rule_config->{table} )
112 0 ▶ if ( defined $rule_config->{table} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $rule_config->{"table"} 0 0 0
113 push( @iptables_rule, D => uc( $rule_config->{chain} ) )
114 0 ▶ if ( defined $rule_config->{chain} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $rule_config->{"chain"} 0 0 0
115 push( @iptables_rule, s => $rule_config->{source} )
116 0 ▶ if ( defined $rule_config->{source} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $rule_config->{"source"} 0 0 0
117 push( @iptables_rule, p => $rule_config->{proto} )
118 0 ▶ if ( defined $rule_config->{proto} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $rule_config->{"proto"} 0 0 0
119 push( @iptables_rule, m => $rule_config->{proto} )
120 0 ▶ if ( defined $rule_config->{proto} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $rule_config->{"proto"} 0 0 0
121 push( @iptables_rule, d => $rule_config->{destination} )
122 0 ▶ if ( defined $rule_config->{destination} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $rule_config->{"destination"} 0 0 0
123 push( @iptables_rule, sport => $rule_config->{sport} )
124 0 ▶ if ( defined $rule_config->{sport} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $rule_config->{"sport"} 0 0 0
125 push( @iptables_rule, dport => $rule_config->{dport} )
126 0 ▶ if ( defined $rule_config->{dport} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $rule_config->{"dport"} 0 0 0
127 push( @iptables_rule, "tcp-flags" => $rule_config->{tcp_flags} )
128 0 ▶ if ( defined $rule_config->{tcp_flags} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $rule_config->{"tcp_flags"} 0 0 0
129 push( @iptables_rule, "i" => $rule_config->{iniface} )
130 0 ▶ if ( defined $rule_config->{iniface} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $rule_config->{"iniface"} 0 0 0
131 push( @iptables_rule, "o" => $rule_config->{outiface} )
132 0 ▶ if ( defined $rule_config->{outiface} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $rule_config->{"outiface"} 0 0 0
133 push( @iptables_rule, "reject-with" => $rule_config->{reject_with} )
134 0 ▶ if ( defined $rule_config->{reject_with} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $rule_config->{"reject_with"} 0 0 0
135 push( @iptables_rule, "log-level" => $rule_config->{log_level} )
136 0 ▶ if ( defined $rule_config->{log_level} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $rule_config->{"log_level"} 0 0 0
137 push( @iptables_rule, "log-prefix" => $rule_config->{log_prefix} )
138 0 ▶ if ( defined $rule_config->{log_prefix} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $rule_config->{"log_prefix"} 0 0 0
139 push( @iptables_rule, "state" => $rule_config->{state} )
140 0 ▶ if ( defined $rule_config->{state} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $rule_config->{"state"} 0 0 0
141 push( @iptables_rule, j => uc( $rule_config->{action} ) )
142 0 ▶ if ( defined $rule_config->{action} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $rule_config->{"action"} 0 0 0
143  
144 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (Rex::Commands::Iptables::_rule_exists($rule_config->{"ip_version"}, @iptables_rule)) 0 0 0
145 Rex::Commands::Iptables::_rule_exists(
146 $rule_config->{ip_version},
147 @iptables_rule
148 )
149 )
150 {
151 0 iptables( $rule_config->{ip_version}, @iptables_rule );
152 0 return 1;
153 }
154  
155 0 return 0;
156 }
157  
158 0 ▶sub _version {
Subroutine _version: not called CC=2 SCAR=17.9
159 0 my ($self) = @_;
160 0 ▶ if ( exists $self->{__version__} ) { return $self->{__version__} }
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"__version__"}) 0 0 0
161  
162 0 my $version = i_run "iptables --version", fail_ok => 1;
163 0 $version =~ s/^.*\sv(\d+\.\d+\.\d+)/$1/;
164  
165 0 $self->{__version__} = [ split( /\./, $version ) ];
166  
167 Rex::Logger::debug(
168 0 "Got iptables version: " . join( ", ", @{ $self->{__version__} } ) );
169  
170 0 return $self->{__version__};
171 }
172  
173 1 1;
« lib/Rex/Inventory/HP/ACU.pm ↑ Summary lib/Rex/Service.pm »