BlockerHelper/backends/ipfw.pm

filter: Statementstmt 6.2% 19 / 304 Branchbran 0.0% 0 / 124 Conditioncond 0.0% 0 / 108 MC/DCmcdc 0.0% 0 / 52 Subroutinesub 46.1% 6 / 13 Podpod 100% 7 / 7 totaltotal 5.2% 32 / 608 CC 106 SCAR 78.8
CC
106
Cov
4%
new
4556.0
init
182.0
ban
156.0
CRAP 2632.5
↑ Summary BlockerHelper/backends/dummy.pm »
1 package Net::Firewall::BlockerHelper::backends::ipfw;
2  
3 1 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use base 'Error::Helper';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Regexp::IPv4 qw($IPv4_re);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Regexp::IPv6 qw($IPv6_re);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 =head1 NAME
11  
12 Net::Firewall::BlockerHelper::backends::ipfw - IPFW backend for Net::Firewall::BlockerHelper.
13  
14 =head1 VERSION
15  
16 Version 0.0.1
17  
18 =cut
19  
20 1 our $VERSION = '0.0.1';
21  
22 =head1 SYNOPSIS
23  
24 use Net::Firewall::BlockerHelper::backends::ipfw;
25  
26 my $backend1;
27 my $backend2;
28 eval {
29 $backend1 = Net::Firewall::BlockerHelper::backends::ipfw->new(
30 backend => 'ipfw',
31 name => 'all',
32 options=>{ rule=>150, kill=>1 },
33 );
34 $backend2 = Net::Firewall::BlockerHelper::backends::ipfw->new(
35 backend => 'ipfw',
36 ports => ['143'],
37 protocols => ['tcp'],
38 name => 'imap',
39 options=>{ rule=>151 },
40 );
41 };
42 if ($@) {
43 print 'Error: '
44 . $Error::Helper::error
45 . "\nError String: "
46 . $Error::Helper::errorString
47 . "\nError Flag: "
48 . $Error::Helper::errorFlag . "\n";
49 }
50  
51 print `ipfw list`
52  
53 $backend1->init;
54 $backend2->init;
55  
56 print `ipfw list`
57  
58 $backend1->ban(ban=>'1.2.3.4');
59 $backend1->ban(ban=>'4.3.2.1');
60 $backend2->ban(ban=>'4.3.2.1');
61  
62 use Data::Dumper;
63 print Dumper($backend1->list);
64 print Dumper($backend2->list);
65  
66 $backend1->unban(ban=>'4.3.2.1');
67  
68 use Data::Dumper;
69 print Dumper($backend1->list);
70 print Dumper($backend2->list);
71  
72 $backend1->teardown;
73  
74 print `ipfw list`
75  
76 $backend2->teardown;
77  
78 print `ipfw list`
79  
80 =head1 METHODS
81  
82 =head2 new
83  
84 Initiates the the object.
85  
86 - options :: Backend specific options that will be passed to the backend unchecked
87 outside of making sure it is a hash ref if defined. See below for furhter info.
88 - Default :: {}
89  
90 - ports :: A array of ports to block. Checked to make sure they are positive ints or a valid
91 service name via getservbyname. All ports will be blocked if non are specified. If
92 duplicates are removed.
93 - Default :: []
94  
95 - protocols :: A array of protocols to block. By default will block all. This
96 is checked against /etc/protocols via the function getprotobyname. Duplicates
97 will be discarded.
98 - Default :: ['ip']
99  
100 - prefix :: Prefix to use. Must match the regex /^[a-zA-Z0-9]+$/
101 - default :: kur
102  
103 - name :: Name of this specific instance. This must be specified.
104 - default :: undef
105  
106 The options hash accepts the following.
107  
108 - rule :: The rule name to use for the IPFW rule. This should not
109 re-used or it will result in the other rules being removed
110 when init is called.
111 - Default :: 150
112  
113 - type :: The drop method to use. Should either be 'deny',
114 'unreach', or 'unreach6'. See ipfw(8) for more info.
115 - Default :: deny
116  
117 - unreach :: The if using unreach, the unreach type to use.
118 See ipfw(8) for more info.
119 - Default :: port
120  
121 - unreach6 :: The if using unreach, the unreach type to use.
122 See ipfw(8) for more info.
123 - Default :: port
124  
125 - kill :: Use tcpdrop to kill TCP connections for that IP.
126 - Default :: 0
127  
128 All errors are considered fatal, meaning if new fails it will die.
129  
130 my $backend;
131 eval {
132 $backend = Net::Firewall::BlockerHelper::backends::ipfw->new(
133 backend => 'ipfw',
134 ports => ['22'],
135 protocols => ['tcp'],
136 name => 'ssh',
137 );
138 };
139 if ($@) {
140 print 'Error: '
141 . $Error::Helper::error
142 . "\nError String: "
143 . $Error::Helper::errorString
144 . "\nError Flag: "
145 . $Error::Helper::errorFlag . "\n";
146 }
147  
148 =cut
149  
150 sub new {
151 0 ▶ my ( $blank, %opts ) = @_;
Subroutine new: not called CC=67 SCAR=84.2
Pod documented
152  
153 0 my $self = {
154 perror => undef,
155 error => undef,
156 errorLine => undef,
157 errorFilename => undef,
158 errorString => "",
159 errorExtra => {
160 all_errors_fatal => 1,
161 flags => {
162 1 => 'notInited',
163 2 => 'invalidPortSpecified',
164 3 => 'portsNotArray',
165 4 => 'protocolsNotArray',
166 5 => 'invalidPortSpecified',
167 6 => 'invalidPrefixSpecified',
168 7 => 'invalidName',
169 8 => 'optionsNotHash',
170 9 => 'noBanItem',
171 10 => 'banItemNotIP',
172 11 => 'invalidBackend',
173 12 => 'backendInitError',
174 13 => 'banFailed',
175 14 => 'unbanFailed',
176 15 => 'listFailed',
177 16 => 'reInitFailed',
178 17 => 'teardownFailed',
179 18 => 'alreadyInited',
180 19 => 'ruleInvalid',
181 20 => 'typeInvalid',
182 21 => 'unreachInvalid',
183 22 => 'unreach6Invalid',
184 23 => 'initFailed',
185 },
186 fatal_flags => {},
187 perror_not_fatal => 0,
188 },
189 options => {
190 rule => 150,
191 type => 'deny',
192 unreach => 'port',
193 unreach6 => 'port',
194 kill => 0,
195 },
196 ports => [],
197 protocols => [],
198 testing => undef,
199 test_data => undef,
200 prefix => 'kur',
201 postfix => undef,
202 frontend_obj => undef,
203 inited => 0,
204 banned => {},
205 };
206 0 bless $self;
207  
208 0 ▶ if ( defined( $opts{ports} ) && ref( $opts{ports} ) ne 'ARRAY' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $opts{'ports'} and ref $opts{'ports'} ne 'ARRAY') { } 0 0 0
elsif (defined $opts{'ports'}) { } 0 0 0
Condition0%0/3
defined $opts{"ports"} and ref $opts{"ports"} ne "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $opts{"ports"} and ref $opts{"ports"} ne "ARRAY"
defined $opts{"ports"} ref $opts{"ports"} ne "ARRAY"
Truth table0%0/3
A and B
A= defined $opts{"ports"}B= ref $opts{"ports"} ne "ARRAY"
ABresult
0X0
100
111
209 0 $self->{perror} = 1;
210 0 $self->{error} = 3;
211 0 $self->{errorString} = 'ports is defined and type is not array but "' . ref( $opts{ports} ) . '"';
212 0 $self->warn;
213 } elsif ( defined( $opts{ports} ) ) {
214 0 my %ports;
215 0 foreach my $item ( @{ $opts{ports} } ) {
216 0 ▶ if ( $item =~ /^[0-9]+$/ && $item >= 1 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($item =~ /^[0-9]+$/ and $item >= 1) { } 0 0 0
elsif ($item =~ /^[0-9]+$/ and $item < 1) { } 0 0 0
Condition0%0/3
$item =~ /^[0-9]+$/ and $item >= 1
!ll&&!rl&&r
000
MC/DC0%0/2
$item =~ /^[0-9]+$/ and $item >= 1
$item =~ /^[0-9]+$/ $item >= 1
Truth table0%0/3
A and B
A= $item =~ /^[0-9]+$/B= $item >= 1
ABresult
0X0
100
111
217 0 $ports{$item} = 1;
218 ▶ } elsif ( $item =~ /^[0-9]+$/ && $item < 1 ) {
Condition0%0/3
$item =~ /^[0-9]+$/ and $item < 1
!ll&&!rl&&r
000
MC/DC0%0/2
$item =~ /^[0-9]+$/ and $item < 1
$item =~ /^[0-9]+$/ $item < 1
Truth table0%0/3
A and B
A= $item =~ /^[0-9]+$/B= $item < 1
ABresult
0X0
100
111
219 0 $self->{perror} = 1;
220 0 $self->{error} = 2;
221 $self->{errorString}
222 0 = $item . ' is not a valid value for a port as it must be a int greater or equal to 1';
223 0 $self->warn;
224 } else {
225 # just using tcp here as protocol must be specified
226 0 my ( $name, $aliases, $port, $proto ) = getservbyname( $item, 'tcp' );
227 0 ▶ if ( !defined($port) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $port) 0 0 0
228 0 $self->{perror} = 1;
229 0 $self->{error} = 2;
230 $self->{errorString}
231 0 = $item . ' could not be resolved to a port name via getservbyname("' . $item . '", "tcp")';
232 0 $self->warn;
233 }
234 0 $ports{$port} = 1;
235 } ## end else [ if ( $item =~ /^[0-9]+$/ && $item >= 1 ) ]
236 } ## end foreach my $item ( @{ $opts{ports} } )
237 0 my @port_keys = keys(%ports);
238 0 @port_keys = sort { $a <=> $b } @port_keys;
239 0 push( @{ $self->{ports} }, @port_keys );
240 } ## end elsif ( defined( $opts{ports} ) )
241  
242 0 ▶ if ( defined( $opts{protocols} ) && ref( $opts{protocols} ) ne 'ARRAY' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $opts{'protocols'} and ref $opts{'protocols'} ne 'ARRAY') { } 0 0 0
elsif (defined $opts{'protocols'}) { } 0 0 0
Condition0%0/3
defined $opts{"protocols"} and ref $opts{"protocols"} ne "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $opts{"protocols"} and ref $opts{"protocols"} ne "ARRAY"
defined $opts{"protocols"} ref $opts{"protocols"} ne "ARRAY"
Truth table0%0/3
A and B
A= defined $opts{"protocols"}B= ref $opts{"protocols"} ne "ARRAY"
ABresult
0X0
100
111
243 0 $self->{perror} = 1;
244 0 $self->{error} = 4;
245 0 $self->{errorString} = 'protocols is defined and type is not array but "' . ref( $opts{protocols} ) . '"';
246 0 $self->warn;
247 } elsif ( defined( $opts{protocols} ) ) {
248 0 my %protocols;
249 0 foreach my $item ( @{ $opts{protocols} } ) {
250 0 my ( $name, $aliases, $proto ) = getprotobyname($item);
251 # if this is undef, it means it is not a known protocol
252 0 ▶ if ( !defined($proto) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $proto) 0 0 0
253 0 $self->{perror} = 1;
254 0 $self->{error} = 5;
255 $self->{errorString}
256 0 = $item . ' could not be resolved to a port name via getservbyname("' . $item . '", "tcp")';
257 0 $self->warn;
258 }
259 0 $protocols{$item} = 1;
260 } ## end foreach my $item ( @{ $opts{protocols} } )
261 0 my @protocols_keys = keys(%protocols);
262 0 @protocols_keys = sort { $a cmp $b } @protocols_keys;
263 0 push( @{ $self->{protocols} }, @protocols_keys );
264 } ## end elsif ( defined( $opts{protocols} ) )
265  
266 # make sure prefix is sane if defiend
267 0 ▶ if ( defined( $opts{prefix} ) && $opts{prefix} !~ /^[a-zA-Z0-9]+$/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $opts{'prefix'} and not $opts{'prefix'} =~ /^[a-zA-Z0-9]+$/) { } 0 0 0
elsif (defined $opts{'prefix'}) { } 0 0 0
Condition0%0/3
defined $opts{"prefix"} and not $opts{"prefix"} =~ /^[a-zA-Z0-9]+$/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $opts{"prefix"} and not $opts{"prefix"} =~ /^[a-zA-Z0-9]+$/
defined $opts{"prefix"} not $opts{"prefix"} =~ /^[a-zA-Z0-9]+$/
Truth table0%0/3
A and not(B)
A= defined $opts{"prefix"}B= not $opts{"prefix"} =~ /^[a-zA-Z0-9]+$/
ABresult
0X0
100
111
268 0 $self->{perror} = 1;
269 0 $self->{error} = 6;
270 $self->{errorString}
271 0 = '"' . $opts{prefix} . '" is not a valid prefix as it does not match the regex /^[a-zA-Z0-9]+$/';
272 0 $self->warn;
273 } elsif ( defined( $opts{prefix} ) ) {
274 0 $self->{prefix} = $opts{prefix};
275 }
276  
277 # make sure we have a name and that it is valid
278 0 ▶ if ( !defined( $opts{name} ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $opts{'name'}) { } 0 0 0
elsif (not $opts{'name'} =~ /^[a-zA-Z0-9\-]+$/) { } 0 0 0
279 0 $self->{perror} = 1;
280 0 $self->{error} = 6;
281 0 $self->{errorString} = 'name is undef';
282 0 $self->warn;
283 } elsif ( $opts{name} !~ /^[a-zA-Z0-9\-]+$/ ) {
284 0 $self->{perror} = 1;
285 0 $self->{error} = 6;
286 0 $self->{errorString} = 'name set to "' . $opts{name} . '" which does not match the regexp /^[a-zA-Z0-9\-]+$/';
287 0 $self->warn;
288 }
289 0 $self->{name} = $opts{name};
290  
291 # used internally for testing
292 0 ▶ if ( defined( $opts{testing} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"testing"}) 0 0 0
293 0 $self->{testing} = $opts{testing};
294 }
295 0 ▶ if ( defined( $opts{frontend_obj} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"frontend_obj"}) 0 0 0
296 0 $self->{frontend_obj} = $opts{frontend_obj};
297 }
298  
299 0 ▶ if ( defined( $opts{options} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"options"}) 0 0 0
300 0 ▶ if ( ref( $opts{options} ) ne 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $opts{"options"} ne "HASH") 0 0 0
301 0 $self->{perror} = 1;
302 0 $self->{error} = 8;
303 0 $self->{errorString} = 'ref for options is "' . ref( $opts{options} ) . '" and not HASH';
304 0 $self->warn;
305 }
306 0 $self->{options} = $opts{options};
307  
308 0 ▶ if ( !defined( $opts{options}{kill} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $opts{"options"}{"kill"}) 0 0 0
309 0 $self->{options}{kill} = 0;
310 }
311  
312 0 ▶ if ( defined( $opts{options}{rule} ) && ref( $opts{options}{rule} ) ne '' ) {
Branch: 4 branches
BranchTrueFalseTotal
if (defined $opts{'options'}{'rule'} and ref $opts{'options'}{'rule'} ne '') { } 0 0 0
elsif (defined $opts{'options'}{'rule'} and not $opts{'options'}{'rule'} =~ /^[0-9]+$/) { } 0 0 0
elsif (defined $opts{'options'}{'rule'} and $opts{'options'}{'rule'} < 1) { } 0 0 0
elsif (not defined $opts{'options'}{'rule'}) { } 0 0 0
Condition0%0/3
defined $opts{"options"}{"rule"} and ref $opts{"options"}{"rule"} ne ""
!ll&&!rl&&r
000
MC/DC0%0/2
defined $opts{"options"}{"rule"} and ref $opts{"options"}{"rule"} ne ""
defined $opts{"options"}{"rule"} ref $opts{"options"}{"rule"} ne ""
Truth table0%0/3
A and B
A= defined $opts{"options"}{"rule"}B= ref $opts{"options"}{"rule"} ne ""
ABresult
0X0
100
111
313 0 $self->{perror} = 1;
314 0 $self->{error} = 19;
315 0 $self->{errorString} = 'ref for $opts{options}{rule} is "' . ref( $opts{options}{rule} ) . '" and not ""';
316 0 $self->warn;
317 ▶ } elsif ( defined( $opts{options}{rule} ) && $opts{options}{rule} !~ /^[0-9]+$/ ) {
Condition0%0/3
defined $opts{"options"}{"rule"} and not $opts{"options"}{"rule"} =~ /^[0-9]+$/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $opts{"options"}{"rule"} and not $opts{"options"}{"rule"} =~ /^[0-9]+$/
defined $opts{"options"}{"rule"} not $opts{"options"}{"rule"} =~ /^[0-9]+$/
Truth table0%0/3
A and not(B)
A= defined $opts{"options"}{"rule"}B= not $opts{"options"}{"rule"} =~ /^[0-9]+$/
ABresult
0X0
100
111
318 0 $self->{perror} = 1;
319 0 $self->{error} = 19;
320 0 $self->{errorString} = '$opts{options}{rule} is "' . $opts{options}{rule} . '" and not "150"';
321 0 $self->warn;
322 ▶ } elsif ( defined( $opts{options}{rule} ) && $opts{options}{rule} < 1 ) {
Condition0%0/3
defined $opts{"options"}{"rule"} and $opts{"options"}{"rule"} < 1
!ll&&!rl&&r
000
MC/DC0%0/2
defined $opts{"options"}{"rule"} and $opts{"options"}{"rule"} < 1
defined $opts{"options"}{"rule"} $opts{"options"}{"rule"} < 1
Truth table0%0/3
A and B
A= defined $opts{"options"}{"rule"}B= $opts{"options"}{"rule"} < 1
ABresult
0X0
100
111
323 0 $self->{perror} = 1;
324 0 $self->{error} = 19;
325 0 $self->{errorString} = '$opts{options}{rule} is "' . $opts{options}{rule} . '" is less than 1';
326 0 $self->warn;
327 } elsif ( !defined( $opts{options}{rule} ) ) {
328 0 $self->{options}{rule} = 150;
329 }
330  
331 0 ▶ if ( defined( $opts{optsions}{type} ) && ref( $opts{options}{type} ) ne '' ) {
Branch: 3 branches
BranchTrueFalseTotal
if (defined $opts{'optsions'}{'type'} and ref $opts{'options'}{'type'} ne '') { } 0 0 0
elsif (defined $opts{'optsions'}{'type'} and $opts{'optsions'}{'type'} ne 'unreach' and $opts{'optsions'}{'type'} ne 'deny' and $opts{'optsions'}{'type'} ne 'unreach6') { } 0 0 0
elsif (not defined $opts{'options'}{'type'}) { } 0 0 0
Condition0%0/3
defined $opts{"optsions"}{"type"} and ref $opts{"options"}{"type"} ne ""
!ll&&!rl&&r
000
MC/DC0%0/2
defined $opts{"optsions"}{"type"} and ref $opts{"options"}{"type"} ne ""
defined $opts{"optsions"}{"type"} ref $opts{"options"}{"type"} ne ""
Truth table0%0/3
A and B
A= defined $opts{"optsions"}{"type"}B= ref $opts{"options"}{"type"} ne ""
ABresult
0X0
100
111
332 0 $self->{perror} = 1;
333 0 $self->{error} = 20;
334 0 $self->{errorString} = 'ref for $opts{options}{type} is "' . ref( $opts{options}{type} ) . '" and not ""';
335 0 $self->warn;
336 ▶ } elsif ( defined( $opts{optsions}{type} )
Condition0%0/9
defined $opts{"optsions"}{"type"} and $opts{"optsions"}{"type"} ne "unreach" and $opts{"optsions"}{"type"} ne "deny" and $opts{"optsions"}{"type"} ne "unreach6"
!ll&&!rl&&r
000
defined $opts{"optsions"}{"type"} and $opts{"optsions"}{"type"} ne "unreach" and $opts{"optsions"}{"type"} ne "deny"
!ll&&!rl&&r
000
defined $opts{"optsions"}{"type"} and $opts{"optsions"}{"type"} ne "unreach"
!ll&&!rl&&r
000
MC/DC0%0/4
defined $opts{"optsions"}{"type"} and $opts{"optsions"}{"type"} ne "unreach" and $opts{"optsions"}{"type"} ne "deny" and $opts{"optsions"}{"type"} ne "unreach6"
defined $opts{"optsions"}{"type"} $opts{"optsions"}{"type"} ne "unreach" $opts{"optsions"}{"type"} ne "deny" $opts{"optsions"}{"type"} ne "unreach6"
Truth table0%0/5
A and B and C and D
A= defined $opts{"optsions"}{"type"}B= $opts{"optsions"}{"type"} ne "unreach"C= $opts{"optsions"}{"type"} ne "deny"D= $opts{"optsions"}{"type"} ne "unreach6"
ABCDresult
0XXX0
10XX0
110X0
11100
11111
337 && $opts{optsions}{type} ne 'unreach'
338 && $opts{optsions}{type} ne 'deny'
339 && $opts{optsions}{type} ne 'unreach6' )
340 {
341 0 $self->{perror} = 1;
342 0 $self->{error} = 20;
343 $self->{errorString}
344 0 = '$opts{options}{type} is "' . $opts{options}{type} . '" and not "deny", "unreach", or "unreach6"';
345 0 $self->warn;
346 } elsif ( !defined( $opts{options}{type} ) ) {
347 0 $self->{options}{type} = 'deny';
348 }
349  
350 0 ▶ if ( defined( $opts{optsions}{unreach} ) && ref( $opts{options}{unreach} ) ne '' ) {
Branch: 3 branches
BranchTrueFalseTotal
if (defined $opts{'optsions'}{'unreach'} and ref $opts{'options'}{'unreach'} ne '') { } 0 0 0
elsif (defined $opts{'optsions'}{'unreach'} and $opts{'optsions'}{'unreach'} ne 'net' and $opts{'optsions'}{'unreach'} ne 'host' and $opts{'optsions'}{'unreach'} ne 'protocol' and $opts{'optsions'}{'unreach'} ne 'port' and $opts{'optsions'}{'unreach'} ne 'needfrag' and $opts{'optsions'}{'unreach'} ne 'srcfail' and $opts{'optsions'}{'unreach'} ne 'net-unknown' and $opts{'optsions'}{'unreach'} ne 'host-unknown' and $opts{'optsions'}{'unreach'} ne 'isolated' and $opts{'optsions'}{'unreach'} ne 'net-prohib' and $opts{'optsions'}{'unreach'} ne 'host-prohib' and $opts{'optsions'}{'unreach'} ne 'tosnet' and $opts{'optsions'}{'unreach'} ne 'toshost' and $opts{'optsions'}{'unreach'} ne 'filter-prohib' and $opts{'optsions'}{'unreach'} ne 'host-precedence' and $opts{'optsions'}{'unreach'} ne 'precedence-cutoff') { } 0 0 0
elsif (not defined $opts{'options'}{'unreach'}) { } 0 0 0
Condition0%0/3
defined $opts{"optsions"}{"unreach"} and ref $opts{"options"}{"unreach"} ne ""
!ll&&!rl&&r
000
MC/DC0%0/2
defined $opts{"optsions"}{"unreach"} and ref $opts{"options"}{"unreach"} ne ""
defined $opts{"optsions"}{"unreach"} ref $opts{"options"}{"unreach"} ne ""
Truth table0%0/3
A and B
A= defined $opts{"optsions"}{"unreach"}B= ref $opts{"options"}{"unreach"} ne ""
ABresult
0X0
100
111
351 0 $self->{perror} = 1;
352 0 $self->{error} = 21;
353 $self->{errorString}
354 0 = 'ref for $opts{options}{unreach} is "' . ref( $opts{options}{unreach} ) . '" and not ""';
355 0 $self->warn;
356 ▶ } elsif ( defined( $opts{optsions}{unreach} )
Condition0%0/48
defined $opts{"optsions"}{"unreach"} and $opts{"optsions"}{"unreach"} ne "net" and $opts{"optsions"}{"unreach"} ne "host" and $opts{"optsions"}{"unreach"} ne "protocol" and $opts{"optsions"}{"unreach"} ne "port" and $opts{"optsions"}{"unreach"} ne "needfrag" and $opts{"optsions"}{"unreach"} ne "srcfail" and $opts{"optsions"}{"unreach"} ne "net-unknown" and $opts{"optsions"}{"unreach"} ne "host-unknown" and $opts{"optsions"}{"unreach"} ne "isolated" and $opts{"optsions"}{"unreach"} ne "net-prohib" and $opts{"optsions"}{"unreach"} ne "host-prohib" and $opts{"optsions"}{"unreach"} ne "tosnet" and $opts{"optsions"}{"unreach"} ne "toshost" and $opts{"optsions"}{"unreach"} ne "filter-prohib" and $opts{"optsions"}{"unreach"} ne "host-precedence" and $opts{"optsions"}{"unreach"} ne "precedence-cutoff"
!ll&&!rl&&r
000
defined $opts{"optsions"}{"unreach"} and $opts{"optsions"}{"unreach"} ne "net" and $opts{"optsions"}{"unreach"} ne "host" and $opts{"optsions"}{"unreach"} ne "protocol" and $opts{"optsions"}{"unreach"} ne "port" and $opts{"optsions"}{"unreach"} ne "needfrag" and $opts{"optsions"}{"unreach"} ne "srcfail" and $opts{"optsions"}{"unreach"} ne "net-unknown" and $opts{"optsions"}{"unreach"} ne "host-unknown" and $opts{"optsions"}{"unreach"} ne "isolated" and $opts{"optsions"}{"unreach"} ne "net-prohib" and $opts{"optsions"}{"unreach"} ne "host-prohib" and $opts{"optsions"}{"unreach"} ne "tosnet" and $opts{"optsions"}{"unreach"} ne "toshost" and $opts{"optsions"}{"unreach"} ne "filter-prohib" and $opts{"optsions"}{"unreach"} ne "host-precedence"
!ll&&!rl&&r
000
defined $opts{"optsions"}{"unreach"} and $opts{"optsions"}{"unreach"} ne "net" and $opts{"optsions"}{"unreach"} ne "host" and $opts{"optsions"}{"unreach"} ne "protocol" and $opts{"optsions"}{"unreach"} ne "port" and $opts{"optsions"}{"unreach"} ne "needfrag" and $opts{"optsions"}{"unreach"} ne "srcfail" and $opts{"optsions"}{"unreach"} ne "net-unknown" and $opts{"optsions"}{"unreach"} ne "host-unknown" and $opts{"optsions"}{"unreach"} ne "isolated" and $opts{"optsions"}{"unreach"} ne "net-prohib" and $opts{"optsions"}{"unreach"} ne "host-prohib" and $opts{"optsions"}{"unreach"} ne "tosnet" and $opts{"optsions"}{"unreach"} ne "toshost" and $opts{"optsions"}{"unreach"} ne "filter-prohib"
!ll&&!rl&&r
000
defined $opts{"optsions"}{"unreach"} and $opts{"optsions"}{"unreach"} ne "net" and $opts{"optsions"}{"unreach"} ne "host" and $opts{"optsions"}{"unreach"} ne "protocol" and $opts{"optsions"}{"unreach"} ne "port" and $opts{"optsions"}{"unreach"} ne "needfrag" and $opts{"optsions"}{"unreach"} ne "srcfail" and $opts{"optsions"}{"unreach"} ne "net-unknown" and $opts{"optsions"}{"unreach"} ne "host-unknown" and $opts{"optsions"}{"unreach"} ne "isolated" and $opts{"optsions"}{"unreach"} ne "net-prohib" and $opts{"optsions"}{"unreach"} ne "host-prohib" and $opts{"optsions"}{"unreach"} ne "tosnet" and $opts{"optsions"}{"unreach"} ne "toshost"
!ll&&!rl&&r
000
defined $opts{"optsions"}{"unreach"} and $opts{"optsions"}{"unreach"} ne "net" and $opts{"optsions"}{"unreach"} ne "host" and $opts{"optsions"}{"unreach"} ne "protocol" and $opts{"optsions"}{"unreach"} ne "port" and $opts{"optsions"}{"unreach"} ne "needfrag" and $opts{"optsions"}{"unreach"} ne "srcfail" and $opts{"optsions"}{"unreach"} ne "net-unknown" and $opts{"optsions"}{"unreach"} ne "host-unknown" and $opts{"optsions"}{"unreach"} ne "isolated" and $opts{"optsions"}{"unreach"} ne "net-prohib" and $opts{"optsions"}{"unreach"} ne "host-prohib" and $opts{"optsions"}{"unreach"} ne "tosnet"
!ll&&!rl&&r
000
defined $opts{"optsions"}{"unreach"} and $opts{"optsions"}{"unreach"} ne "net" and $opts{"optsions"}{"unreach"} ne "host" and $opts{"optsions"}{"unreach"} ne "protocol" and $opts{"optsions"}{"unreach"} ne "port" and $opts{"optsions"}{"unreach"} ne "needfrag" and $opts{"optsions"}{"unreach"} ne "srcfail" and $opts{"optsions"}{"unreach"} ne "net-unknown" and $opts{"optsions"}{"unreach"} ne "host-unknown" and $opts{"optsions"}{"unreach"} ne "isolated" and $opts{"optsions"}{"unreach"} ne "net-prohib" and $opts{"optsions"}{"unreach"} ne "host-prohib"
!ll&&!rl&&r
000
defined $opts{"optsions"}{"unreach"} and $opts{"optsions"}{"unreach"} ne "net" and $opts{"optsions"}{"unreach"} ne "host" and $opts{"optsions"}{"unreach"} ne "protocol" and $opts{"optsions"}{"unreach"} ne "port" and $opts{"optsions"}{"unreach"} ne "needfrag" and $opts{"optsions"}{"unreach"} ne "srcfail" and $opts{"optsions"}{"unreach"} ne "net-unknown" and $opts{"optsions"}{"unreach"} ne "host-unknown" and $opts{"optsions"}{"unreach"} ne "isolated" and $opts{"optsions"}{"unreach"} ne "net-prohib"
!ll&&!rl&&r
000
defined $opts{"optsions"}{"unreach"} and $opts{"optsions"}{"unreach"} ne "net" and $opts{"optsions"}{"unreach"} ne "host" and $opts{"optsions"}{"unreach"} ne "protocol" and $opts{"optsions"}{"unreach"} ne "port" and $opts{"optsions"}{"unreach"} ne "needfrag" and $opts{"optsions"}{"unreach"} ne "srcfail" and $opts{"optsions"}{"unreach"} ne "net-unknown" and $opts{"optsions"}{"unreach"} ne "host-unknown" and $opts{"optsions"}{"unreach"} ne "isolated"
!ll&&!rl&&r
000
defined $opts{"optsions"}{"unreach"} and $opts{"optsions"}{"unreach"} ne "net" and $opts{"optsions"}{"unreach"} ne "host" and $opts{"optsions"}{"unreach"} ne "protocol" and $opts{"optsions"}{"unreach"} ne "port" and $opts{"optsions"}{"unreach"} ne "needfrag" and $opts{"optsions"}{"unreach"} ne "srcfail" and $opts{"optsions"}{"unreach"} ne "net-unknown" and $opts{"optsions"}{"unreach"} ne "host-unknown"
!ll&&!rl&&r
000
defined $opts{"optsions"}{"unreach"} and $opts{"optsions"}{"unreach"} ne "net" and $opts{"optsions"}{"unreach"} ne "host" and $opts{"optsions"}{"unreach"} ne "protocol" and $opts{"optsions"}{"unreach"} ne "port" and $opts{"optsions"}{"unreach"} ne "needfrag" and $opts{"optsions"}{"unreach"} ne "srcfail" and $opts{"optsions"}{"unreach"} ne "net-unknown"
!ll&&!rl&&r
000
defined $opts{"optsions"}{"unreach"} and $opts{"optsions"}{"unreach"} ne "net" and $opts{"optsions"}{"unreach"} ne "host" and $opts{"optsions"}{"unreach"} ne "protocol" and $opts{"optsions"}{"unreach"} ne "port" and $opts{"optsions"}{"unreach"} ne "needfrag" and $opts{"optsions"}{"unreach"} ne "srcfail"
!ll&&!rl&&r
000
defined $opts{"optsions"}{"unreach"} and $opts{"optsions"}{"unreach"} ne "net" and $opts{"optsions"}{"unreach"} ne "host" and $opts{"optsions"}{"unreach"} ne "protocol" and $opts{"optsions"}{"unreach"} ne "port" and $opts{"optsions"}{"unreach"} ne "needfrag"
!ll&&!rl&&r
000
defined $opts{"optsions"}{"unreach"} and $opts{"optsions"}{"unreach"} ne "net" and $opts{"optsions"}{"unreach"} ne "host" and $opts{"optsions"}{"unreach"} ne "protocol" and $opts{"optsions"}{"unreach"} ne "port"
!ll&&!rl&&r
000
defined $opts{"optsions"}{"unreach"} and $opts{"optsions"}{"unreach"} ne "net" and $opts{"optsions"}{"unreach"} ne "host" and $opts{"optsions"}{"unreach"} ne "protocol"
!ll&&!rl&&r
000
defined $opts{"optsions"}{"unreach"} and $opts{"optsions"}{"unreach"} ne "net" and $opts{"optsions"}{"unreach"} ne "host"
!ll&&!rl&&r
000
defined $opts{"optsions"}{"unreach"} and $opts{"optsions"}{"unreach"} ne "net"
!ll&&!rl&&r
000
MC/DC0%0/17
defined $opts{"optsions"}{"unreach"} and $opts{"optsions"}{"unreach"} ne "net" and $opts{"optsions"}{"unreach"} ne "host" and $opts{"optsions"}{"unreach"} ne "protocol" and $opts{"optsions"}{"unreach"} ne "port" and $opts{"optsions"}{"unreach"} ne "needfrag" and $opts{"optsions"}{"unreach"} ne "srcfail" and $opts{"optsions"}{"unreach"} ne "net-unknown" and $opts{"optsions"}{"unreach"} ne "host-unknown" and $opts{"optsions"}{"unreach"} ne "isolated" and $opts{"optsions"}{"unreach"} ne "net-prohib" and $opts{"optsions"}{"unreach"} ne "host-prohib" and $opts{"optsions"}{"unreach"} ne "tosnet" and $opts{"optsions"}{"unreach"} ne "toshost" and $opts{"optsions"}{"unreach"} ne "filter-prohib" and $opts{"optsions"}{"unreach"} ne "host-precedence" and $opts{"optsions"}{"unreach"} ne "precedence-cutoff"
too many conditions
357 && $opts{optsions}{unreach} ne 'net'
358 && $opts{optsions}{unreach} ne 'host'
359 && $opts{optsions}{unreach} ne 'protocol'
360 && $opts{optsions}{unreach} ne 'port'
361 && $opts{optsions}{unreach} ne 'needfrag'
362 && $opts{optsions}{unreach} ne 'srcfail'
363 && $opts{optsions}{unreach} ne 'net-unknown'
364 && $opts{optsions}{unreach} ne 'host-unknown'
365 && $opts{optsions}{unreach} ne 'isolated'
366 && $opts{optsions}{unreach} ne 'net-prohib'
367 && $opts{optsions}{unreach} ne 'host-prohib'
368 && $opts{optsions}{unreach} ne 'tosnet'
369 && $opts{optsions}{unreach} ne 'toshost'
370 && $opts{optsions}{unreach} ne 'filter-prohib'
371 && $opts{optsions}{unreach} ne 'host-precedence'
372 && $opts{optsions}{unreach} ne 'precedence-cutoff' )
373 {
374 0 $self->{perror} = 1;
375 0 $self->{error} = 21;
376 $self->{errorString}
377 = '$opts{options}{unreach} is "'
378 . $opts{options}{unreach}
379 0 . '" and a value understood by ipfw(8) for unreach';
380 0 $self->warn;
381 } elsif ( !defined( $opts{options}{unreach} ) ) {
382 0 $self->{options}{unreach} = 'port';
383 }
384  
385 0 ▶ if ( defined( $opts{optsions}{unreach6} ) && ref( $opts{options}{unreach6} ) ne '' ) {
Branch: 3 branches
BranchTrueFalseTotal
if (defined $opts{'optsions'}{'unreach6'} and ref $opts{'options'}{'unreach6'} ne '') { } 0 0 0
elsif (defined $opts{'optsions'}{'unreach6'} and $opts{'optsions'}{'unreach6'} ne 'no-route' and $opts{'optsions'}{'unreach6'} ne 'admin-prohib' and $opts{'optsions'}{'unreach6'} ne 'address' and $opts{'optsions'}{'unreach6'} ne 'port') { } 0 0 0
elsif (not defined $opts{'options'}{'unreach6'}) { } 0 0 0
Condition0%0/3
defined $opts{"optsions"}{"unreach6"} and ref $opts{"options"}{"unreach6"} ne ""
!ll&&!rl&&r
000
MC/DC0%0/2
defined $opts{"optsions"}{"unreach6"} and ref $opts{"options"}{"unreach6"} ne ""
defined $opts{"optsions"}{"unreach6"} ref $opts{"options"}{"unreach6"} ne ""
Truth table0%0/3
A and B
A= defined $opts{"optsions"}{"unreach6"}B= ref $opts{"options"}{"unreach6"} ne ""
ABresult
0X0
100
111
386 0 $self->{perror} = 1;
387 0 $self->{error} = 22;
388 $self->{errorString}
389 0 = 'ref for $opts{options}{unreach6} is "' . ref( $opts{options}{unreach6} ) . '" and not ""';
390 0 $self->warn;
391 ▶ } elsif ( defined( $opts{optsions}{unreach6} )
Condition0%0/12
defined $opts{"optsions"}{"unreach6"} and $opts{"optsions"}{"unreach6"} ne "no-route" and $opts{"optsions"}{"unreach6"} ne "admin-prohib" and $opts{"optsions"}{"unreach6"} ne "address" and $opts{"optsions"}{"unreach6"} ne "port"
!ll&&!rl&&r
000
defined $opts{"optsions"}{"unreach6"} and $opts{"optsions"}{"unreach6"} ne "no-route" and $opts{"optsions"}{"unreach6"} ne "admin-prohib" and $opts{"optsions"}{"unreach6"} ne "address"
!ll&&!rl&&r
000
defined $opts{"optsions"}{"unreach6"} and $opts{"optsions"}{"unreach6"} ne "no-route" and $opts{"optsions"}{"unreach6"} ne "admin-prohib"
!ll&&!rl&&r
000
defined $opts{"optsions"}{"unreach6"} and $opts{"optsions"}{"unreach6"} ne "no-route"
!ll&&!rl&&r
000
MC/DC0%0/5
defined $opts{"optsions"}{"unreach6"} and $opts{"optsions"}{"unreach6"} ne "no-route" and $opts{"optsions"}{"unreach6"} ne "admin-prohib" and $opts{"optsions"}{"unreach6"} ne "address" and $opts{"optsions"}{"unreach6"} ne "port"
defined $opts{"optsions"}{"unreach6"} $opts{"optsions"}{"unreach6"} ne "no-route" $opts{"optsions"}{"unreach6"} ne "admin-prohib" $opts{"optsions"}{"unreach6"} ne "address" $opts{"optsions"}{"unreach6"} ne "port"
Truth table0%0/6
A and B and C and D and E
A= defined $opts{"optsions"}{"unreach6"}B= $opts{"optsions"}{"unreach6"} ne "no-route"C= $opts{"optsions"}{"unreach6"} ne "admin-prohib"D= $opts{"optsions"}{"unreach6"} ne "address"E= $opts{"optsions"}{"unreach6"} ne "port"
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
392 && $opts{optsions}{unreach6} ne 'no-route'
393 && $opts{optsions}{unreach6} ne 'admin-prohib'
394 && $opts{optsions}{unreach6} ne 'address'
395 && $opts{optsions}{unreach6} ne 'port' )
396 {
397 0 $self->{perror} = 1;
398 0 $self->{error} = 22;
399 $self->{errorString}
400 = '$opts{options}{unreach6} is "'
401 . $opts{options}{unreach6}
402 0 . '" and a value understood by ipfw(8) for unreach6';
403 0 $self->warn;
404 } elsif ( !defined( $opts{options}{unreach6} ) ) {
405 0 $self->{options}{unreach6} = 'port';
406 }
407  
408 } ## end if ( defined( $opts{options} ) )
409  
410 0 return $self;
411 } ## end sub new
412  
413 =head2 init
414  
415 Initiates the backend. This will attempt to drop the rule number and table
416 prior to re-adding them.
417  
418 No arguments are taken.
419  
420 May called a second time, it will error.
421  
422 $backend->init;
423  
424 =cut
425  
426 sub init {
427 0 ▶ my ( $self, %opts ) = @_;
Subroutine init: not called CC=13 SCAR=52.0
Pod documented
428  
429 0 $self->errorblank;
430  
431 0 ▶ if ( $self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"inited"}) 0 0 0
432 0 $self->{error} = 18;
433 0 $self->{errorString} = 'backend has already been inited';
434 0 $self->warn;
435 }
436  
437 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
438 0 $self->{frontend_obj}->{test_data} = {};
439 }
440  
441 0 my $ports;
442 0 ▶ if ( defined( $self->{ports}[0] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"ports"}[0]) 0 0 0
443 0 $ports = join( ',', @{ $self->{ports} } );
444 }
445  
446 0 my @protocols;
447 0 ▶ if ( defined( $self->{protocols}[0] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'protocols'}[0]) { } 0 0 0
448 0 push( @protocols, @{ $self->{protocols} } );
449 } else {
450 0 push( @protocols, 'ip' );
451 }
452  
453 0 my @fail_okay_commands;
454 0 push( @fail_okay_commands, 'ipfw table ' . $self->{prefix} . '_' . $self->{name} . ' destroy' );
455 0 push( @fail_okay_commands, 'ipfw delete ' . $self->{options}{rule} );
456  
457 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
458 0 $self->{frontend_obj}->{test_data}{fail_okay_commands} = \@fail_okay_commands;
459 } else {
460 0 foreach my $item (@fail_okay_commands) {
461 0 my $output = `$item 2>&1`;
462 }
463 }
464  
465 0 my @commands;
466 # add the table to create the command
467 0 push( @commands, 'ipfw table ' . $self->{prefix} . '_' . $self->{name} . ' create' );
468  
469 # generates the block rules
470 0 foreach my $item (@protocols) {
471 0 my $command = 'ipfw add ' . $self->{options}{rule} . ' ' . $self->{options}{type} . ' ';
472 0 ▶ if ( $self->{options}{type} ne 'deny' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"options"}{"type"} ne "deny") 0 0 0
473 0 $command = $command . $self->{options}{ $self->{options}{type} } . ' ';
474 }
475 0 $command = $command . $item . ' from "table(' . $self->{prefix} . '_' . $self->{name} . ')" to me';
476 0 ▶ if ( defined($ports) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ports) 0 0 0
477 0 $command = $command . ' ' . $ports;
478 }
479 0 push( @commands, $command );
480 } ## end foreach my $item (@protocols)
481  
482 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
483 0 $self->{frontend_obj}->{test_data}{commands} = \@commands;
484 } else {
485 0 foreach my $item (@fail_okay_commands) {
486 0 my $output = `$item 2>&1`;
487 0 ▶ if ( $? ne '0' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? ne 0) 0 0 0
488 0 $self->{error} = 22;
489 $self->{errorString}
490 0 = 'init failed. non-zero exit code for the dommand... "' . $item . '"... output... ' . $output;
491 0 $self->warn;
492 }
493 }
494 } ## end else [ if ( $self->{testing} ) ]
495  
496 0 $self->{inited} = 1;
497 } ## end sub init
498  
499 =head2 ban
500  
501 Bans the IP.
502  
503 $backend->ban(ban => $ip);
504  
505 =cut
506  
507 sub ban {
508 0 ▶ my ( $self, %opts ) = @_;
Subroutine ban: not called CC=12 SCAR=50.5
Pod documented
509  
510 0 $self->errorblank;
511  
512 0 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 0 0
513 0 $self->{error} = 1;
514 0 $self->{errorString} = 'backend has not been inited';
515 0 $self->warn;
516 0 return;
517 }
518  
519 0 ▶ if ( !defined( $opts{ban} ) ) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $opts{'ban'}) { } 0 0 0
elsif (ref $opts{'ban'} ne '') { } 0 0 0
elsif (not $opts{'ban'} =~ /$Net::Firewall::BlockerHelper::backends::ipfw::IPv4_re/ || $opts{'ban'} =~ /$Net::Firewall::BlockerHelper::backends::ipfw::IPv6_re/) { } 0 0 0
520 0 $self->{error} = 9;
521 0 $self->{errorString} = 'Nothing specified for the value ban';
522 0 $self->warn;
523 0 return;
524 } elsif ( ref( $opts{ban} ) ne '' ) {
525 0 $self->{error} = 10;
526 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
527 0 $self->warn;
528 0 return;
529 ▶ } elsif ( $opts{ban} !~ /$IPv4_re/
Condition0%0/3
$opts{'ban'} =~ /$Net::Firewall::BlockerHelper::backends::ipfw::IPv4_re/ || $opts{'ban'} =~ /$Net::Firewall::BlockerHelper::backends::ipfw::IPv6_re/
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts{'ban'} =~ /$Net::Firewall::BlockerHelper::backends::ipfw::IPv4_re/ || $opts{'ban'} =~ /$Net::Firewall::BlockerHelper::backends::ipfw::IPv6_re/
$opts{'ban'} =~ /$Net::Firewall::BlockerHelper::backends::ipfw::IPv4_re/ $opts{'ban'} =~ /$Net::Firewall::BlockerHelper::backends::ipfw::IPv6_re/
Truth table0%0/3
A || B
A= $opts{'ban'} =~ /$Net::Firewall::BlockerHelper::backends::ipfw::IPv4_re/B= $opts{'ban'} =~ /$Net::Firewall::BlockerHelper::backends::ipfw::IPv6_re/
ABresult
1X1
011
000
530 && $opts{ban} !~ /$IPv6_re/ )
531 {
532 0 $self->{error} = 10;
533 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 IP';
534 0 $self->warn;
535 0 return;
536 }
537  
538 0 ▶ if ( $self->{banned}{ $opts{ban} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"banned"}{$opts{"ban"}}) 0 0 0
539 0 $self->{frontend_obj}->{test_data} = 'already banned';
540 0 return;
541 }
542  
543 0 my $command = 'ipfw table ' . $self->{prefix} . '_' . $self->{name} . ' add ' . $opts{ban};
544  
545 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
546 0 $self->{frontend_obj}->{test_data} = [$command];
547 } else {
548 0 my $output = `$command 2>&1`;
549 0 ▶ if ( $? ne '0' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? ne 0) 0 0 0
550 0 $self->{error} = 13;
551 $self->{errorString}
552 0 = 'ban failed. non-zero exit code for the command... "' . $command . '"... output... ' . $output;
553 0 $self->warn;
554 }
555 }
556  
557 0 ▶ if ( $self->{options}{kill} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"options"}{"kill"}) 0 0 0
558 $command
559 = 'sockstat -nc4 -P tcp |sed "s/.*tcp[46] *//" | sed "s/:/ /g" | grep -i '
560 . $opts{ban}
561 0 . ' | xargs -n 4 tcpdrop';
562 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
563 0 push( @{ $self->{frontend_obj}->{test_data} }, $command );
564 } else {
565 0 my $output = `$command 2>&1`;
566 }
567  
568 $command
569 = 'sockstat -n6 -P udp | grep -i '
570 . $opts{ban}
571 0 . ' | perl -lpe \'$_=~s/.*udp[46] *//; $_=~s/:([0-9]+) / $1 /; $_=~s/:([0-9]+)$/ $1/; $_=~s/\%[a-zA-Z0-9]+/ /g ; print $_\'';
572 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
573 0 push( @{ $self->{frontend_obj}->{test_data} }, $command );
574 } else {
575 0 my $output = `$command 2>&1`;
576 }
577 } ## end if ( $self->{options}{kill} )
578  
579 0 $self->{banned}{ $opts{ban} } = 1;
580 } ## end sub ban
581  
582 =head2 unban
583  
584 Unbans the an IP.
585  
586 $backend->ban(ban => $ip);
587  
588 =cut
589  
590 sub unban {
591 0 ▶ my ( $self, %opts ) = @_;
Subroutine unban: not called CC=9 SCAR=45.0
Pod documented
592  
593 0 $self->errorblank;
594  
595 0 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 0 0
596 0 $self->{error} = 1;
597 0 $self->{errorString} = 'backend has not been inited';
598 0 $self->warn;
599 0 return;
600 }
601  
602 0 ▶ if ( !defined( $opts{ban} ) ) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $opts{'ban'}) { } 0 0 0
elsif (ref $opts{'ban'} ne '') { } 0 0 0
elsif (not $opts{'ban'} =~ /$Net::Firewall::BlockerHelper::backends::ipfw::IPv4_re/ || $opts{'ban'} =~ /$Net::Firewall::BlockerHelper::backends::ipfw::IPv6_re/) { } 0 0 0
603 0 $self->{error} = 9;
604 0 $self->{errorString} = 'Nothing specified for the value ban';
605 0 $self->warn;
606 0 return;
607 } elsif ( ref( $opts{ban} ) ne '' ) {
608 0 $self->{error} = 10;
609 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
610 0 $self->warn;
611 0 return;
612 ▶ } elsif ( $opts{ban} !~ /$IPv4_re/
Condition0%0/3
$opts{'ban'} =~ /$Net::Firewall::BlockerHelper::backends::ipfw::IPv4_re/ || $opts{'ban'} =~ /$Net::Firewall::BlockerHelper::backends::ipfw::IPv6_re/
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts{'ban'} =~ /$Net::Firewall::BlockerHelper::backends::ipfw::IPv4_re/ || $opts{'ban'} =~ /$Net::Firewall::BlockerHelper::backends::ipfw::IPv6_re/
$opts{'ban'} =~ /$Net::Firewall::BlockerHelper::backends::ipfw::IPv4_re/ $opts{'ban'} =~ /$Net::Firewall::BlockerHelper::backends::ipfw::IPv6_re/
Truth table0%0/3
A || B
A= $opts{'ban'} =~ /$Net::Firewall::BlockerHelper::backends::ipfw::IPv4_re/B= $opts{'ban'} =~ /$Net::Firewall::BlockerHelper::backends::ipfw::IPv6_re/
ABresult
1X1
011
000
613 && $opts{ban} !~ /$IPv6_re/ )
614 {
615 0 $self->{error} = 10;
616 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 IP';
617 0 $self->warn;
618 0 return;
619 }
620  
621 0 ▶ if ( !$self->{banned}{ $opts{ban} } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"banned"}{$opts{"ban"}}) 0 0 0
622 0 $self->{frontend_obj}->{test_data} = 'not banned';
623 0 return;
624 }
625  
626 0 my $command = 'ipfw table ' . $self->{prefix} . '_' . $self->{name} . ' delete ' . $opts{ban};
627  
628 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
629 0 $self->{frontend_obj}->{test_data} = $command;
630 } else {
631 0 my $output = `$command 2>&1`;
632 0 ▶ if ( $? ne '0' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? ne 0) 0 0 0
633 0 $self->{error} = 14;
634 $self->{errorString}
635 0 = 'unban failed. non-zero exit code for the command... "' . $command . '"... output... ' . $output;
636 0 $self->warn;
637 }
638 }
639  
640 0 delete( $self->{banned}{ $opts{ban} } );
641 } ## end sub unban
642  
643 =head2 list
644  
645 List banned IPs.
646  
647 my @banned = $backend->list;
648  
649 =cut
650  
651 sub list {
652 0 ▶ my ( $self, %opts ) = @_;
Subroutine list: not called CC=1 SCAR=6.9
Pod documented
653  
654 0 $self->errorblank;
655  
656 0 $self->{frontend_obj}->{test_data} = 'list';
657  
658 0 return keys( %{ $self->{banned} } );
659 }
660  
661 =head2 re_init
662  
663 Tells the backend to re-init it's self.
664  
665 This will call teardown and init again. After that it will
666 re-added all previously added bans.
667  
668 $backend->re_init;
669  
670 =cut
671  
672 sub re_init {
673 0 ▶ my ( $self, %opts ) = @_;
Subroutine re_init: not called CC=6 SCAR=37.4
Pod documented
674  
675 0 $self->errorblank;
676  
677 0 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 0 0
678 0 $self->{error} = 1;
679 0 $self->{errorString} = 'backend has not been inited';
680 0 $self->warn;
681 0 return;
682 }
683  
684 0 $self->teardown;
685 0 $self->init;
686  
687 0 my @to_ban = keys( %{ $self->{banned} } );
688  
689 0 my @re_init_test_data;
690 0 foreach my $item (@to_ban) {
691 0 my $command = 'ipfw table ' . $self->{prefix} . '_' . $self->{name} . ' add ' . $item;
692  
693 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
694 0 push( @re_init_test_data, $command );
695 } else {
696 0 my $output = `$command 2>&1`;
697 0 ▶ if ( $? ne '0' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? ne 0) 0 0 0
698 0 $self->{error} = 13;
699 $self->{errorString}
700 0 = 'ban failed. non-zero exit code for the command... "' . $command . '"... output... ' . $output;
701 0 $self->warn;
702 }
703 }
704 } ## end foreach my $item (@to_ban)
705  
706 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
707 0 $self->{frontend_obj}->{test_data} = \@re_init_test_data;
708 }
709  
710 0 $self->{inited} = 1;
711 } ## end sub re_init
712  
713 =head2 teardown
714  
715 Tears down the setup for the backend.
716  
717 This will delete the table as well as the firewall rule.
718  
719 If called prior to calling init, this will error. It won't check if it has been
720 inited or not.
721  
722 $backend->teardown;
723  
724 =cut
725  
726 sub teardown {
727 0 ▶ my ( $self, %opts ) = @_;
Subroutine teardown: not called CC=4 SCAR=30.0
Pod documented
728  
729 0 $self->errorblank;
730  
731 0 $self->{inited} = 0;
732  
733 0 $self->{frontend_obj}->{test_data} = {};
734  
735 0 my @commands;
736 0 push( @commands, 'ipfw table ' . $self->{prefix} . '_' . $self->{name} . ' destroy' );
737 0 push( @commands, 'ipfw delete ' . $self->{options}{rule} );
738  
739 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
740 0 $self->{frontend_obj}->{test_data} = \@commands;
741 } else {
742 0 foreach my $item (@commands) {
743 0 my $output = `$item 2>&1`;
744 0 ▶ if ( $? ne '0' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? ne 0) 0 0 0
745 0 $self->{error} = 17;
746 $self->{errorString}
747 0 = 'teardown failed. non-zero exit code for the command... "' . $item . '"... output... ' . $output;
748 0 $self->warn;
749 }
750 }
751 } ## end else [ if ( $self->{testing} ) ]
752 } ## end sub teardown
753  
754 =head1 ERROR CODES / FLAGS
755  
756 Error handling is provided by L<Error::Helper>. All
757 errors are considered fatal.
758  
759 =head2 1, notInited
760  
761 Backend has not been initted yet.
762  
763 =head2 2, invalidPortSpecified
764  
765 Port is either not a positive int or a name that can be resolved by getservbyname.
766  
767 =head2 3, portsNotArray
768  
769 The data passed to new for ports is not an array.
770  
771 =head2 4, protocolsNotArray
772  
773 The data passed to new for protocols is not an array.
774  
775 =head2 5, invalidPortSpecified
776  
777 Port is either not a positive int or a name that can be resolved by getservbyname.
778  
779 =head2 6, invalidPrefixSpecified
780  
781 The specified prefix did not match /^[a-zA-Z0-9]+$/.
782  
783 =head2 7, invalidName
784  
785 The name is either undef or does not match /^[a-zA-Z0-9\-]+$/.
786  
787 =head2 8, optionsNotHash
788  
789 The item passed to new for options is not a hash.
790  
791 =head2 9, noBanItem
792  
793 No IP specified to ban.
794  
795 =head2 10, banItemNotIP
796  
797 The item to ban is not an IP. Either wrong ref type or regexp
798 test using L<Regexp::IPv4> and L<Regexp::IPv6> failed.
799  
800 =head2 11, invalidBackend
801  
802 The specified backend failed to pass a basic sanity check of making sure it
803 matches the regexp /^[a-zA-Z0-9\_]+$/.
804  
805 =head2 12, backendInitError
806  
807 Failed to init the backend.
808  
809 =head2 13, banFailed
810  
811 Failed to ban the item.
812  
813 =head2 14, unbanFailed
814  
815 Failed to unban the item.
816  
817 =head2 15, listFailed
818  
819 Failed get a list of bans.
820  
821 =head2 16, reInitFailed
822  
823 Failed to re_init the backend.
824  
825 =head2 17, teardownFailed
826  
827 Failed to teardown the backend.
828  
829 =head2 18, alreadyInited
830  
831 Backend has already been initiated.
832  
833 =head2 19, ruleInvalid
834  
835 The specified rule is not a int or 1 or less.
836  
837 =head2 20, typeInvalid
838  
839 The value for type is not valid.
840  
841 =head2 21, unreachInvalid
842  
843 The value for the uncreach option is invalid. Should be of a
844 value unstood by unreach for ipfw(8).
845  
846 =head2 22, unreach6Invalid
847  
848 The value for the uncreach6 option is invalid. Should be of a
849 value unstood by unreach6 for ipfw(8).
850  
851 =head2 23, initFailed
852  
853 One of the required commands for init failed.
854  
855 =head1 AUTHOR
856  
857 Zane C. Bowers-Hadley, C<< <vvelox at vvelox.ent> >>
858  
859 =head1 BUGS
860  
861 Please report any bugs or feature requests to C<bug-net-firewall-blockerhelper at rt.cpan.org>, or through
862 the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Net-Firewall-BlockerHelper>. I will be notified, and then you'll
863 automatically be notified of progress on your bug as I make changes.
864  
865  
866  
867  
868 =head1 SUPPORT
869  
870 You can find documentation for this module with the perldoc command.
871  
872 perldoc Net::Firewall::BlockerHelper
873  
874  
875 You can also look for information at:
876  
877 =over 4
878  
879 =item * RT: CPAN's request tracker (report bugs here)
880  
881 L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=Net-Firewall-BlockerHelper>
882  
883 =item * Search CPAN
884  
885 L<https://metacpan.org/release/Net-Firewall-BlockerHelper>
886  
887 =back
888  
889  
890 =head1 ACKNOWLEDGEMENTS
891  
892  
893 =head1 LICENSE AND COPYRIGHT
894  
895 This software is Copyright (c) 2023 by Zane C. Bowers-Hadley.
896  
897 This is free software, licensed under:
898  
899 The GNU Lesser General Public License, Version 2.1, February 1999
900  
901  
902 =cut
903  
904 1 1; # End of Net::Firewall::BlockerHelper
↑ Summary BlockerHelper/backends/dummy.pm »