BlockerHelper/backends/dummy.pm

filter: Statementstmt 35.6% 87 / 244 Branchbran 23.9% 23 / 96 Conditioncond 13.3% 4 / 30 MC/DCmcdc 0.0% 0 / 19 Subroutinesub 65.0% 13 / 20 Podpod 100% 13 / 13 totaltotal 33.1% 140 / 422 CC 61 SCAR 42.6
CC
61
Cov
31%
new
167.4
_valid_cidr
72.0
unban
56.0
CRAP 71.0
« BlockerHelper/backends/pf.pm ↑ Summary BlockerHelper/backends/fastly.pm »
1 package Net::Firewall::BlockerHelper::backends::dummy;
2  
3 3 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use base 'Error::Helper';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Regexp::IPv4 qw($IPv4_re);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Regexp::IPv6 qw($IPv6_re);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 =head1 NAME
11  
12 Net::Firewall::BlockerHelper::backends::dummy - Example dummy backend for testing purposes.
13  
14 =head1 VERSION
15  
16 Version 0.1.0
17  
18 =cut
19  
20 3 our $VERSION = '0.1.0';
21  
22 =head1 SYNOPSIS
23  
24 use Net::Firewall::BlockerHelper::backends::dummy;
25  
26 # create a backend named ssh with a dummy backend for port 22 tcp
27 my $backend;
28 eval {
29 $backend = Net::Firewall::BlockerHelper::backends::dummy->new(
30 backend => 'dummy',
31 ports => ['22'],
32 protocols => ['tcp'],
33 name => 'ssh',
34 );
35 };
36 if ($@) {
37 print 'Error: '
38 . $Error::Helper::error
39 . "\nError String: "
40 . $Error::Helper::errorString
41 . "\nError Flag: "
42 . $Error::Helper::errorFlag . "\n";
43 }
44  
45 $backend->init;
46  
47 # ban some IPs
48 $backend->ban(ban => '1.2.3.4');
49 $backend->ban(ban => '5.6.7.8');
50  
51 # unban a IP
52 $backend->unban(ban => '1.2.3.4');
53  
54 # get a list of banned IPs
55 my @banned = $backend->list;
56 foreach my $ip (@banned) {
57 print 'Banned IP: '.$ip."\n";
58 }
59  
60 # teardown the backend, re-init, and re-ban everything
61 $backend->re_init;
62  
63 # teardown the backend
64 $backend->teardown;
65  
66 =head1 METHODS
67  
68 =head2 new
69  
70 Initiates the the object.
71  
72 - options :: Backend specific options that will be passed to the backend unchecked
73 outside of making sure it is a hash ref if defined.
74 - Default :: {}
75  
76 - ports :: A array of ports to block. Checked to make sure they are positive ints or a valid
77 service name via getservbyname.
78 - Default :: []
79  
80 - protocols :: A array of protocols to block. By default will block all. This
81 is checked against /etc/protocols via the function getprotobyname.
82 - Default :: []
83  
84 - prefix :: Prefix to use. Must match the regex /^[a-zA-Z0-9]+$/
85 - default :: kur
86  
87 - name :: Name of this specific instance.
88 - default :: undef
89  
90 All errors are considered fatal, meaning if new fails it will die.
91  
92 my $backend;
93 eval {
94 $backend = Net::Firewall::BlockerHelper::backends::dummy->new(
95 backend => 'dummy',
96 ports => ['22'],
97 protocols => ['tcp'],
98 name => 'ssh',
99 );
100 };
101 if ($@) {
102 print 'Error: '
103 . $Error::Helper::error
104 . "\nError String: "
105 . $Error::Helper::errorString
106 . "\nError Flag: "
107 . $Error::Helper::errorFlag . "\n";
108 }
109  
110 =cut
111  
112 sub new {
113 2 ▶ my ( $blank, %opts ) = @_;
Subroutine new: called CC=24 SCAR=51.2
Pod documented
114  
115 2 my $self = {
116 perror => undef,
117 error => undef,
118 errorLine => undef,
119 errorFilename => undef,
120 errorString => "",
121 errorExtra => {
122 all_errors_fatal => 1,
123 # all_fatal is what Error::Helper 2.1.0 actually checks; all_errors_fatal
124 # is kept for the name documented in its POD
125 all_fatal => 1,
126 flags => {
127 1 => 'notInited',
128 2 => 'invalidPortSpecified',
129 3 => 'portsNotArray',
130 4 => 'protocolsNotArray',
131 5 => 'invalidPortSpecified',
132 6 => 'invalidPrefixSpecified',
133 7 => 'invalidName',
134 8 => 'optionsNotHash',
135 9 => 'noBanItem',
136 10 => 'banItemNotIP',
137 11 => 'invalidBackend',
138 12 => 'backendInitError',
139 13 => 'banFailed',
140 14 => 'unbanFailed',
141 15 => 'listFailed',
142 16 => 'reInitFailed',
143 17 => 'teardownFailed',
144 18 => 'alreadyInited',
145 24 => 'checkFailed',
146 25 => 'flushFailed',
147 26 => 'banCidrFailed',
148 27 => 'unbanCidrFailed',
149 28 => 'cidrItemNotCidr',
150 29 => 'cidrNotSupported',
151 30 => 'listCidrFailed',
152 },
153 fatal_flags => {},
154 perror_not_fatal => 0,
155 },
156 backend => undef,
157 options => {},
158 ports => [],
159 protocols => [],
160 testing => undef,
161 test_data => undef,
162 prefix => 'kur',
163 frontend_obj => undef,
164 inited => 0,
165 banned => {},
166 banned_cidr => {},
167 cidr_supported => 1,
168 };
169 2 bless $self;
170  
171 2 ▶ if ( defined( $opts{ports} ) && ref( $opts{ports} ) ne 'ARRAY' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $opts{'ports'} and ref $opts{'ports'} ne 'ARRAY') { } 0 2 2
elsif (defined $opts{'ports'}) { } 2 0 2
Condition33%1/3
defined $opts{"ports"} and ref $opts{"ports"} ne "ARRAY"
!ll&&!rl&&r
020
MC/DC0%0/2
defined $opts{"ports"} and ref $opts{"ports"} ne "ARRAY"
defined $opts{"ports"} ref $opts{"ports"} ne "ARRAY"
Truth table33%1/3
A and B
A= defined $opts{"ports"}B= ref $opts{"ports"} ne "ARRAY"
ABresult
0X0
100
111
172 0 $self->{perror} = 1;
173 0 $self->{error} = 3;
174 0 $self->{errorString} = 'ports is defined and type is not array but "' . ref( $opts{ports} ) . '"';
175 0 $self->warn;
176 } elsif ( defined( $opts{ports} ) ) {
177 2 my %ports;
178 2 foreach my $item ( @{ $opts{ports} } ) {
179 0 ▶ if ( $item =~ /^[0-9]+$/ && $item >= 1 && $item <= 65535 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($item =~ /^[0-9]+$/ and $item >= 1 and $item <= 65535) { } 0 0 0
elsif ($item =~ /^[0-9]+$/) { } 0 0 0
Condition0%0/6
$item =~ /^[0-9]+$/ and $item >= 1 and $item <= 65535
!ll&&!rl&&r
000
$item =~ /^[0-9]+$/ and $item >= 1
!ll&&!rl&&r
000
MC/DC0%0/3
$item =~ /^[0-9]+$/ and $item >= 1 and $item <= 65535
$item =~ /^[0-9]+$/ $item >= 1 $item <= 65535
Truth table0%0/4
A and B and C
A= $item =~ /^[0-9]+$/B= $item >= 1C= $item <= 65535
ABCresult
0XX0
10X0
1100
1111
180 0 $ports{$item} = 1;
181 } elsif ( $item =~ /^[0-9]+$/ ) {
182 0 $self->{perror} = 1;
183 0 $self->{error} = 2;
184 $self->{errorString}
185 0 = $item . ' is not a valid value for a port as it must be a int within the range 1 to 65535';
186 0 $self->warn;
187 } else {
188 # just using tcp here as protocol must be specified
189 0 my ( $name, $aliases, $port, $proto ) = getservbyname( $item, 'tcp' );
190 0 ▶ if ( !defined($port) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $port) 0 0 0
191 0 $self->{perror} = 1;
192 0 $self->{error} = 2;
193 $self->{errorString}
194 0 = $item . ' could not be resolved to a port name via getservbyname("' . $item . '", "tcp")';
195 0 $self->warn;
196 }
197 0 $ports{$port} = 1;
198 } ## end else [ if ( $item =~ /^[0-9]+$/ && $item >= 1 ) ]
199 } ## end foreach my $item ( @{ $opts{ports} } )
200 2 my @port_keys = keys(%ports);
201 2 @port_keys = sort { $a <=> $b } @port_keys;
202 2 push( @{ $self->{ports} }, @port_keys );
203 } ## end elsif ( defined( $opts{ports} ) )
204  
205 2 ▶ if ( defined( $opts{protocols} ) && ref( $opts{protocols} ) ne 'ARRAY' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $opts{'protocols'} and ref $opts{'protocols'} ne 'ARRAY') { } 0 2 2
elsif (defined $opts{'protocols'}) { } 2 0 2
Condition33%1/3
defined $opts{"protocols"} and ref $opts{"protocols"} ne "ARRAY"
!ll&&!rl&&r
020
MC/DC0%0/2
defined $opts{"protocols"} and ref $opts{"protocols"} ne "ARRAY"
defined $opts{"protocols"} ref $opts{"protocols"} ne "ARRAY"
Truth table33%1/3
A and B
A= defined $opts{"protocols"}B= ref $opts{"protocols"} ne "ARRAY"
ABresult
0X0
100
111
206 0 $self->{perror} = 1;
207 0 $self->{error} = 4;
208 0 $self->{errorString} = 'protocols is defined and type is not array but "' . ref( $opts{protocols} ) . '"';
209 0 $self->warn;
210 } elsif ( defined( $opts{protocols} ) ) {
211 2 my %protocols;
212 2 foreach my $item ( @{ $opts{protocols} } ) {
213 0 my ( $name, $aliases, $proto ) = getprotobyname($item);
214 # if this is undef, it means it is not a known protocol
215 0 ▶ if ( !defined($proto) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $proto) 0 0 0
216 0 $self->{perror} = 1;
217 0 $self->{error} = 5;
218 $self->{errorString}
219 0 = $item . ' could not be resolved to a protocol via getprotobyname("' . $item . '")';
220 0 $self->warn;
221 }
222 0 $protocols{$item} = 1;
223 } ## end foreach my $item ( @{ $opts{protocols} } )
224 2 my @protocols_keys = keys(%protocols);
225 2 @protocols_keys = sort { $a cmp $b } @protocols_keys;
226 2 push( @{ $self->{protocols} }, @protocols_keys );
227 } ## end elsif ( defined( $opts{protocols} ) )
228  
229 # make sure prefix is sane if defiend
230 2 ▶ 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 2 2
elsif (defined $opts{'prefix'}) { } 2 0 2
Condition33%1/3
defined $opts{"prefix"} and not $opts{"prefix"} =~ /^[a-zA-Z0-9]+$/
!ll&&!rl&&r
020
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 table33%1/3
A and not(B)
A= defined $opts{"prefix"}B= not $opts{"prefix"} =~ /^[a-zA-Z0-9]+$/
ABresult
0X0
100
111
231 0 $self->{perror} = 1;
232 0 $self->{error} = 6;
233 $self->{errorString}
234 0 = '"' . $opts{prefix} . '" is not a valid prefix as it does not match the regex /^[a-zA-Z0-9]+$/';
235 0 $self->warn;
236 } elsif ( defined( $opts{prefix} ) ) {
237 2 $self->{prefix} = $opts{prefix};
238 }
239  
240 # make sure we have a name and that it is valid
241 2 ▶ if ( !defined( $opts{name} ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $opts{'name'}) { } 0 2 2
elsif (not $opts{'name'} =~ /^[a-zA-Z0-9\-]+$/) { } 0 2 2
242 0 $self->{perror} = 1;
243 0 $self->{error} = 7;
244 0 $self->{errorString} = 'name is undef';
245 0 $self->warn;
246 } elsif ( $opts{name} !~ /^[a-zA-Z0-9\-]+$/ ) {
247 0 $self->{perror} = 1;
248 0 $self->{error} = 7;
249 0 $self->{errorString} = 'name set to "' . $opts{name} . '" which does not match the regexp /^[a-zA-Z0-9\-]+$/';
250 0 $self->warn;
251 }
252 2 $self->{name} = $opts{name};
253  
254 # used internally for testing
255 2 ▶ if ( defined( $opts{testing} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"testing"}) 2 0 2
256 2 $self->{testing} = $opts{testing};
257 }
258 2 ▶ if ( defined( $opts{frontend_obj} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"frontend_obj"}) 2 0 2
259 2 $self->{frontend_obj} = $opts{frontend_obj};
260 }
261  
262 2 ▶ if ( defined( $opts{options} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"options"}) 2 0 2
263 2 ▶ if ( ref( $opts{options} ) ne 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $opts{"options"} ne "HASH") 0 2 2
264 0 $self->{perror} = 1;
265 0 $self->{error} = 8;
266 0 $self->{errorString} = 'ref for options is "' . ref( $opts{options} ) . '" and not HASH';
267 0 $self->warn;
268 }
269 2 $self->{options} = $opts{options};
270 }
271  
272 2 return $self;
273 } ## end sub new
274  
275 =head2 init
276  
277 Initiates the backend. As this is the dummy testing backend, nothing
278 external is touched; the backend is just marked as inited and the call is
279 recorded when in testing mode.
280  
281 No arguments are taken.
282  
283 $backend->init;
284  
285 =cut
286  
287 sub init {
288 2 ▶ my ( $self, %opts ) = @_;
Subroutine init: called CC=3 SCAR=12.6
Pod documented
289  
290 2 $self->errorblank;
291  
292 2 ▶ if ( $self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"inited"}) 0 2 2
293 0 $self->{error} = 18;
294 0 $self->{errorString} = 'backend has already been inited';
295 0 $self->warn;
296 }
297  
298 2 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 2 0 2
299 2 $self->{frontend_obj}->{test_data} = 'inited';
300 }
301  
302 2 $self->{inited} = 1;
303 } ## end sub init
304  
305 =head2 ban
306  
307 Bans an IP. The value of ban is validated as being a IPv4 or IPv6 address
308 and lowercased, then recorded in the internal ban list. As this is the
309 dummy testing backend, nothing external is touched.
310  
311 $backend->ban(ban => $ip);
312  
313 =cut
314  
315 sub ban {
316 1 ▶ my ( $self, %opts ) = @_;
Subroutine ban: called CC=7 SCAR=29.3
Pod documented
317  
318 1 $self->errorblank;
319  
320 1 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 1 1
321 0 $self->{error} = 1;
322 0 $self->{errorString} = 'backend has not been inited';
323 0 $self->warn;
324 0 return;
325 }
326  
327 1 ▶ if ( !defined( $opts{ban} ) ) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $opts{'ban'}) { } 0 1 1
elsif (ref $opts{'ban'} ne '') { } 0 1 1
elsif (not $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::dummy::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::dummy::IPv6_re\z/) { } 0 1 1
328 0 $self->{error} = 9;
329 0 $self->{errorString} = 'Nothing specified for the value ban';
330 0 $self->warn;
331 0 return;
332 } elsif ( ref( $opts{ban} ) ne '' ) {
333 0 $self->{error} = 10;
334 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
335 0 $self->warn;
336 0 return;
337 ▶ } elsif ( $opts{ban} !~ /\A$IPv4_re\z/
Condition33%1/3
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::dummy::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::dummy::IPv6_re\z/
l!l&&r!l&&!r
100
MC/DC0%0/2
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::dummy::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::dummy::IPv6_re\z/
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::dummy::IPv4_re\z/ $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::dummy::IPv6_re\z/
Truth table33%1/3
A || B
A= $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::dummy::IPv4_re\z/B= $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::dummy::IPv6_re\z/
ABresult
1X1
011
000
338 && $opts{ban} !~ /\A$IPv6_re\z/ )
339 {
340 0 $self->{error} = 10;
341 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 IP';
342 0 $self->warn;
343 0 return;
344 }
345  
346 # lowercase so the same IPv6 IP in differing cases can't result in duplicate entries
347 1 $opts{ban} = lc( $opts{ban} );
348  
349 1 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 1 0 1
350 1 $self->{frontend_obj}->{test_data} = 'banned ' . $opts{ban};
351 }
352  
353 1 $self->{banned}{ $opts{ban} } = 1;
354 } ## end sub ban
355  
356 =head2 unban
357  
358 Unbans an IP. The value of ban is validated as being a IPv4 or IPv6 address
359 and lowercased, then removed from the internal ban list. As this is the
360 dummy testing backend, nothing external is touched.
361  
362 $backend->unban(ban => $ip);
363  
364 =cut
365  
366 sub unban {
367 0 ▶ my ( $self, %opts ) = @_;
Subroutine unban: not called CC=7 SCAR=40.3
Pod documented
368  
369 0 $self->errorblank;
370  
371 0 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 0 0
372 0 $self->{error} = 1;
373 0 $self->{errorString} = 'backend has not been inited';
374 0 $self->warn;
375 0 return;
376 }
377  
378 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'} =~ /\A$Net::Firewall::BlockerHelper::backends::dummy::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::dummy::IPv6_re\z/) { } 0 0 0
379 0 $self->{error} = 9;
380 0 $self->{errorString} = 'Nothing specified for the value ban';
381 0 $self->warn;
382 0 return;
383 } elsif ( ref( $opts{ban} ) ne '' ) {
384 0 $self->{error} = 10;
385 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
386 0 $self->warn;
387 0 return;
388 ▶ } elsif ( $opts{ban} !~ /\A$IPv4_re\z/
Condition0%0/3
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::dummy::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::dummy::IPv6_re\z/
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::dummy::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::dummy::IPv6_re\z/
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::dummy::IPv4_re\z/ $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::dummy::IPv6_re\z/
Truth table0%0/3
A || B
A= $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::dummy::IPv4_re\z/B= $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::dummy::IPv6_re\z/
ABresult
1X1
011
000
389 && $opts{ban} !~ /\A$IPv6_re\z/ )
390 {
391 0 $self->{error} = 10;
392 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 IP';
393 0 $self->warn;
394 0 return;
395 }
396  
397 # lowercase so the same IPv6 IP in differing cases can't result in duplicate entries
398 0 $opts{ban} = lc( $opts{ban} );
399  
400 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
401 0 $self->{frontend_obj}->{test_data} = 'unbanned ' . $opts{ban};
402 }
403  
404 0 delete( $self->{banned}{ $opts{ban} } );
405 } ## end sub unban
406  
407 # Internal helper. Returns a true value if the passed scalar is a valid IPv4 or
408 # IPv6 CIDR range, that is an address followed by "/" and a prefix length that
409 # is within the range valid for its family (0 to 32 for IPv4, 0 to 128 for
410 # IPv6). Returns false otherwise.
411 sub _valid_cidr {
412 0 ▶ my ( $self, $cidr ) = @_;
Subroutine _valid_cidr: not called CC=8 SCAR=42.8
413  
414 0 ▶ return 0 if ( !defined($cidr) || ref($cidr) ne '' );
Branch: 1 branch
BranchTrueFalseTotal
if not defined $cidr or ref $cidr ne "" 0 0 0
Condition0%0/3
not defined $cidr or ref $cidr ne ""
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $cidr or ref $cidr ne ""
not defined $cidr ref $cidr ne ""
Truth table0%0/3
not(A) or B
A= not defined $cidrB= ref $cidr ne ""
ABresult
1X1
011
000
415  
416 0 ▶ if ( $cidr =~ m!\A(.+)/([0-9]{1,3})\z! ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cidr =~ m[\A(.+)/([0-9]{1,3})\z]) 0 0 0
417 0 my ( $addr, $prefix ) = ( $1, $2 );
418 0 ▶ return 1 if ( $addr =~ /\A$IPv4_re\z/ && $prefix <= 32 );
Branch: 1 branch
BranchTrueFalseTotal
if $addr =~ /\A$Net::Firewall::BlockerHelper::backends::dummy::IPv4_re\z/ and $prefix <= 32 0 0 0
Condition0%0/3
$addr =~ /\A$Net::Firewall::BlockerHelper::backends::dummy::IPv4_re\z/ and $prefix <= 32
!ll&&!rl&&r
000
MC/DC0%0/2
$addr =~ /\A$Net::Firewall::BlockerHelper::backends::dummy::IPv4_re\z/ and $prefix <= 32
$addr =~ /\A$Net::Firewall::BlockerHelper::backends::dummy::IPv4_re\z/ $prefix <= 32
Truth table0%0/3
A and B
A= $addr =~ /\A$Net::Firewall::BlockerHelper::backends::dummy::IPv4_re\z/B= $prefix <= 32
ABresult
0X0
100
111
419 0 ▶ return 1 if ( $addr =~ /\A$IPv6_re\z/ && $prefix <= 128 );
Branch: 1 branch
BranchTrueFalseTotal
if $addr =~ /\A$Net::Firewall::BlockerHelper::backends::dummy::IPv6_re\z/ and $prefix <= 128 0 0 0
Condition0%0/3
$addr =~ /\A$Net::Firewall::BlockerHelper::backends::dummy::IPv6_re\z/ and $prefix <= 128
!ll&&!rl&&r
000
MC/DC0%0/2
$addr =~ /\A$Net::Firewall::BlockerHelper::backends::dummy::IPv6_re\z/ and $prefix <= 128
$addr =~ /\A$Net::Firewall::BlockerHelper::backends::dummy::IPv6_re\z/ $prefix <= 128
Truth table0%0/3
A and B
A= $addr =~ /\A$Net::Firewall::BlockerHelper::backends::dummy::IPv6_re\z/B= $prefix <= 128
ABresult
0X0
100
111
420 }
421  
422 0 return 0;
423 } ## end sub _valid_cidr
424  
425 =head2 ban_cidr
426  
427 Bans a CIDR range. The value of ban is validated as being a IPv4 or IPv6
428 CIDR range and lowercased, then recorded in the internal CIDR ban list.
429 Nothing external is touched.
430  
431 $backend->ban_cidr(ban => '1.2.3.0/24');
432  
433 =cut
434  
435 sub ban_cidr {
436 0 ▶ my ( $self, %opts ) = @_;
Subroutine ban_cidr: not called CC=6 SCAR=37.4
Pod documented
437  
438 0 $self->errorblank;
439  
440 0 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 0 0
441 0 $self->{error} = 1;
442 0 $self->{errorString} = 'backend has not been inited';
443 0 $self->warn;
444 0 return;
445 }
446  
447 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 $self->_valid_cidr($opts{'ban'})) { } 0 0 0
448 0 $self->{error} = 9;
449 0 $self->{errorString} = 'Nothing specified for the value ban';
450 0 $self->warn;
451 0 return;
452 } elsif ( ref( $opts{ban} ) ne '' ) {
453 0 $self->{error} = 28;
454 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
455 0 $self->warn;
456 0 return;
457 } elsif ( !$self->_valid_cidr( $opts{ban} ) ) {
458 0 $self->{error} = 28;
459 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 CIDR';
460 0 $self->warn;
461 0 return;
462 }
463  
464 # lowercase so the same IPv6 CIDR in differing cases can't result in duplicate entries
465 0 $opts{ban} = lc( $opts{ban} );
466  
467 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
468 0 $self->{frontend_obj}->{test_data} = 'banned cidr ' . $opts{ban};
469 }
470  
471 0 $self->{banned_cidr}{ $opts{ban} } = 1;
472 } ## end sub ban_cidr
473  
474 =head2 unban_cidr
475  
476 Unbans a CIDR range. The value of ban is validated as being a IPv4 or IPv6
477 CIDR range and lowercased, then removed from the internal CIDR ban list.
478 Nothing external is touched.
479  
480 $backend->unban_cidr(ban => '1.2.3.0/24');
481  
482 =cut
483  
484 sub unban_cidr {
485 0 ▶ my ( $self, %opts ) = @_;
Subroutine unban_cidr: not called CC=6 SCAR=37.4
Pod documented
486  
487 0 $self->errorblank;
488  
489 0 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 0 0
490 0 $self->{error} = 1;
491 0 $self->{errorString} = 'backend has not been inited';
492 0 $self->warn;
493 0 return;
494 }
495  
496 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 $self->_valid_cidr($opts{'ban'})) { } 0 0 0
497 0 $self->{error} = 9;
498 0 $self->{errorString} = 'Nothing specified for the value ban';
499 0 $self->warn;
500 0 return;
501 } elsif ( ref( $opts{ban} ) ne '' ) {
502 0 $self->{error} = 28;
503 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
504 0 $self->warn;
505 0 return;
506 } elsif ( !$self->_valid_cidr( $opts{ban} ) ) {
507 0 $self->{error} = 28;
508 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 CIDR';
509 0 $self->warn;
510 0 return;
511 }
512  
513 # lowercase so the same IPv6 CIDR in differing cases can't result in duplicate entries
514 0 $opts{ban} = lc( $opts{ban} );
515  
516 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
517 0 $self->{frontend_obj}->{test_data} = 'unbanned cidr ' . $opts{ban};
518 }
519  
520 0 delete( $self->{banned_cidr}{ $opts{ban} } );
521 } ## end sub unban_cidr
522  
523 =head2 list_cidr
524  
525 List banned CIDR ranges. Returns an array of the currently banned CIDR
526 ranges. Single IPs are not included; for those see L</list>.
527  
528 my @banned_cidrs = $backend->list_cidr;
529  
530 =cut
531  
532 sub list_cidr {
533 0 ▶ my ( $self, %opts ) = @_;
Subroutine list_cidr: not called CC=2 SCAR=17.9
Pod documented
534  
535 0 $self->errorblank;
536  
537 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
538 0 $self->{frontend_obj}->{test_data} = 'list_cidr';
539 }
540  
541 0 return keys( %{ $self->{banned_cidr} } );
542 }
543  
544 =head2 list
545  
546 List banned IPs. Returns an array of the currently banned single IPs. CIDR
547 ranges are not included; for those see L</list_cidr>.
548  
549 my @banned = $backend->list;
550  
551 =cut
552  
553 sub list {
554 1 ▶ my ( $self, %opts ) = @_;
Subroutine list: called CC=2 SCAR=7.0
Pod documented
555  
556 1 $self->errorblank;
557  
558 1 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 1 0 1
559 1 $self->{frontend_obj}->{test_data} = 'list';
560 }
561  
562 1 return keys( %{ $self->{banned} } );
563 }
564  
565 =head2 re_init
566  
567 Tells the backend to re-init it's self. As this is the dummy testing
568 backend, this just marks it as inited again; the internal ban lists are
569 retained.
570  
571 =cut
572  
573 sub re_init {
574 0 ▶ my ( $self, %opts ) = @_;
Subroutine re_init: not called CC=2 SCAR=17.9
Pod documented
575  
576 0 $self->errorblank;
577  
578 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
579 0 $self->{frontend_obj}->{test_data} = 're_inited';
580 }
581  
582 0 $self->{inited} = 1;
583 }
584  
585 =head2 teardown
586  
587 Tears down the setup for the backend. As this is the dummy testing backend,
588 there is nothing external to remove; the backend is just marked as not
589 inited. The internal ban lists are kept.
590  
591 $backend->teardown;
592  
593 =cut
594  
595 sub teardown {
596 1 ▶ my ( $self, %opts ) = @_;
Subroutine teardown: called CC=2 SCAR=7.0
Pod documented
597  
598 1 $self->errorblank;
599  
600 1 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 1 0 1
601 1 $self->{frontend_obj}->{test_data} = 'teardown';
602 }
603  
604 1 $self->{inited} = 0;
605 } ## end sub teardown
606  
607 =head2 stop
608  
609 Alias for L</teardown>, provided for parity with the fail2ban C<actionstop>
610 concept.
611  
612 $backend->stop;
613  
614 =cut
615  
616 sub stop {
617 0 ▶ my ( $self, %opts ) = @_;
Subroutine stop: not called CC=1 SCAR=6.9
Pod documented
618  
619 0 return $self->teardown(%opts);
620 }
621  
622 =head2 check
623  
624 Dummy check. Always reports the setup as intact. This is the equivalent of
625 fail2ban's C<actioncheck>.
626  
627 my $healthy = $backend->check;
628  
629 =cut
630  
631 sub check {
632 2 ▶ my ( $self, %opts ) = @_;
Subroutine check: called CC=2 SCAR=7.0
Pod documented
633  
634 2 $self->errorblank;
635  
636 2 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 2 0 2
637 2 $self->{frontend_obj}->{test_data} = 'check';
638 }
639  
640 2 return 1;
641 }
642  
643 =head2 flush
644  
645 Dummy flush. Clears the internal lists of banned IPs and CIDR ranges. This
646 is the equivalent of fail2ban's C<actionflush>.
647  
648 $backend->flush;
649  
650 =cut
651  
652 sub flush {
653 1 ▶ my ( $self, %opts ) = @_;
Subroutine flush: called CC=2 SCAR=7.0
Pod documented
654  
655 1 $self->errorblank;
656  
657 1 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 1 0 1
658 1 $self->{frontend_obj}->{test_data} = 'flush';
659 }
660  
661 1 $self->{banned} = {};
662 1 $self->{banned_cidr} = {};
663  
664 1 return;
665 }
666  
667 =head1 ERROR CODES / FLAGS
668  
669 Error handling is provided by L<Error::Helper>. All
670 errors are considered fatal.
671  
672 =head2 1, notInited
673  
674 The backend has not been inited yet.
675  
676 =head2 2, invalidPortSpecified
677  
678 Port is either not an int within the range 1 to 65535 or a name that can be resolved by getservbyname.
679  
680 =head2 3, portsNotArray
681  
682 The data passed to new for ports is not an array.
683  
684 =head2 4, protocolsNotArray
685  
686 The data passed to new for protocols is not an array.
687  
688 =head2 5, invalidPortSpecified
689  
690 Port is either not an int within the range 1 to 65535 or a name that can be resolved by getservbyname.
691  
692 =head2 6, invalidPrefixSpecified
693  
694 The specified prefix did not match /^[a-zA-Z0-9]+$/.
695  
696 =head2 7, invalidName
697  
698 The name is either undef or does not match /^[a-zA-Z0-9\-]+$/.
699  
700 =head2 8, optionsNotHash
701  
702 The item passed to new for options is not a hash.
703  
704 =head2 9, noBanItem
705  
706 No IP or CIDR range specified to ban or unban.
707  
708 =head2 10, banItemNotIP
709  
710 The item to ban is not an IP. Either wrong ref type or regexp
711 test using L<Regexp::IPv4> and L<Regexp::IPv6> failed.
712  
713 =head2 11, invalidBackend
714  
715 The specified backend failed to pass a basic sanity check of making sure it
716 matches the regexp /^[a-zA-Z0-9\_]+$/.
717  
718 =head2 12, backendInitError
719  
720 Failed to init the backend.
721  
722 =head2 13, banFailed
723  
724 Failed to ban the item.
725  
726 =head2 14, unbanFailed
727  
728 Failed to unban the item.
729  
730 =head2 15, listFailed
731  
732 Failed to get a list of bans.
733  
734 =head2 16, reInitFailed
735  
736 Failed to re_init the backend.
737  
738 =head2 17, teardownFailed
739  
740 Failed to teardown the backend.
741  
742 =head2 18, alreadyInited
743  
744 init called, but the backend has already been inited.
745  
746 =head2 24, checkFailed
747  
748 The backend check raised an error.
749  
750 =head2 25, flushFailed
751  
752 Failed to flush the bans.
753  
754 =head2 26, banCidrFailed
755  
756 Failed to ban the CIDR range.
757  
758 =head2 27, unbanCidrFailed
759  
760 Failed to unban the CIDR range.
761  
762 =head2 28, cidrItemNotCidr
763  
764 The item to ban is not a CIDR range. Either wrong ref type or it is not an
765 IPv4 or IPv6 address followed by a prefix length valid for its family.
766  
767 =head2 29, cidrNotSupported
768  
769 The backend does not support CIDR bans.
770  
771 =head2 30, listCidrFailed
772  
773 Failed to get a list of CIDR bans.
774  
775 =head1 AUTHOR
776  
777 Zane C. Bowers-Hadley, C<< <vvelox at vvelox.ent> >>
778  
779 =head1 BUGS
780  
781 Please report any bugs or feature requests to C<bug-net-firewall-blockerhelper at rt.cpan.org>, or through
782 the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Net-Firewall-BlockerHelper>. I will be notified, and then you'll
783 automatically be notified of progress on your bug as I make changes.
784  
785  
786  
787  
788 =head1 SUPPORT
789  
790 You can find documentation for this module with the perldoc command.
791  
792 perldoc Net::Firewall::BlockerHelper
793  
794  
795 You can also look for information at:
796  
797 =over 4
798  
799 =item * RT: CPAN's request tracker (report bugs here)
800  
801 L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=Net-Firewall-BlockerHelper>
802  
803 =item * Search CPAN
804  
805 L<https://metacpan.org/release/Net-Firewall-BlockerHelper>
806  
807 =back
808  
809  
810 =head1 ACKNOWLEDGEMENTS
811  
812  
813 =head1 LICENSE AND COPYRIGHT
814  
815 This software is Copyright (c) 2023 by Zane C. Bowers-Hadley.
816  
817 This is free software, licensed under:
818  
819 The GNU Lesser General Public License, Version 2.1, February 1999
820  
821  
822 =cut
823  
824 3 1; # End of Net::Firewall::BlockerHelper
« BlockerHelper/backends/pf.pm ↑ Summary BlockerHelper/backends/fastly.pm »