BlockerHelper/backends/dummy.pm

filter: Statementstmt 36.7% 87 / 237 Branchbran 26.1% 23 / 88 Conditioncond 19.0% 4 / 21 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 68.4% 13 / 19 Podpod 100% 13 / 13 totaltotal 35.8% 140 / 391 CC 54 SCAR 42.6
CC
54
Cov
33%
new
167.4
unban
56.0
ban_cidr
42.0
CRAP 70.9
« 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 qw( Error::Helper Net::Firewall::BlockerHelper::Util );
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.2.0
17  
18 =cut
19  
20 3 our $VERSION = '0.2.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 2 ▶sub new {
Subroutine new: called CC=24 SCAR=51.2
Pod documented
113 2 my ( $blank, %opts ) = @_;
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 0 @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 0 @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 2 ▶sub init {
Subroutine init: called CC=3 SCAR=12.6
Pod documented
288 2 my ( $self, %opts ) = @_;
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 1 ▶sub ban {
Subroutine ban: called CC=7 SCAR=29.3
Pod documented
316 1 my ( $self, %opts ) = @_;
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 0 ▶sub unban {
Subroutine unban: not called CC=7 SCAR=40.3
Pod documented
367 0 my ( $self, %opts ) = @_;
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 =head2 ban_cidr
408  
409 Bans a CIDR range. The value of ban is validated as being a IPv4 or IPv6
410 CIDR range and lowercased, then recorded in the internal CIDR ban list.
411 Nothing external is touched.
412  
413 $backend->ban_cidr(ban => '1.2.3.0/24');
414  
415 =cut
416  
417 0 ▶sub ban_cidr {
Subroutine ban_cidr: not called CC=6 SCAR=37.4
Pod documented
418 0 my ( $self, %opts ) = @_;
419  
420 0 $self->errorblank;
421  
422 0 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 0 0
423 0 $self->{error} = 1;
424 0 $self->{errorString} = 'backend has not been inited';
425 0 $self->warn;
426 0 return;
427 }
428  
429 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
430 0 $self->{error} = 9;
431 0 $self->{errorString} = 'Nothing specified for the value ban';
432 0 $self->warn;
433 0 return;
434 } elsif ( ref( $opts{ban} ) ne '' ) {
435 0 $self->{error} = 28;
436 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
437 0 $self->warn;
438 0 return;
439 } elsif ( !$self->_valid_cidr( $opts{ban} ) ) {
440 0 $self->{error} = 28;
441 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 CIDR';
442 0 $self->warn;
443 0 return;
444 }
445  
446 # lowercase so the same IPv6 CIDR in differing cases can't result in duplicate entries
447 0 $opts{ban} = lc( $opts{ban} );
448  
449 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
450 0 $self->{frontend_obj}->{test_data} = 'banned cidr ' . $opts{ban};
451 }
452  
453 0 $self->{banned_cidr}{ $opts{ban} } = 1;
454 } ## end sub ban_cidr
455  
456 =head2 unban_cidr
457  
458 Unbans a CIDR range. The value of ban is validated as being a IPv4 or IPv6
459 CIDR range and lowercased, then removed from the internal CIDR ban list.
460 Nothing external is touched.
461  
462 $backend->unban_cidr(ban => '1.2.3.0/24');
463  
464 =cut
465  
466 0 ▶sub unban_cidr {
Subroutine unban_cidr: not called CC=6 SCAR=37.4
Pod documented
467 0 my ( $self, %opts ) = @_;
468  
469 0 $self->errorblank;
470  
471 0 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 0 0
472 0 $self->{error} = 1;
473 0 $self->{errorString} = 'backend has not been inited';
474 0 $self->warn;
475 0 return;
476 }
477  
478 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
479 0 $self->{error} = 9;
480 0 $self->{errorString} = 'Nothing specified for the value ban';
481 0 $self->warn;
482 0 return;
483 } elsif ( ref( $opts{ban} ) ne '' ) {
484 0 $self->{error} = 28;
485 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
486 0 $self->warn;
487 0 return;
488 } elsif ( !$self->_valid_cidr( $opts{ban} ) ) {
489 0 $self->{error} = 28;
490 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 CIDR';
491 0 $self->warn;
492 0 return;
493 }
494  
495 # lowercase so the same IPv6 CIDR in differing cases can't result in duplicate entries
496 0 $opts{ban} = lc( $opts{ban} );
497  
498 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
499 0 $self->{frontend_obj}->{test_data} = 'unbanned cidr ' . $opts{ban};
500 }
501  
502 0 delete( $self->{banned_cidr}{ $opts{ban} } );
503 } ## end sub unban_cidr
504  
505 =head2 list_cidr
506  
507 List banned CIDR ranges. Returns an array of the currently banned CIDR
508 ranges. Single IPs are not included; for those see L</list>.
509  
510 my @banned_cidrs = $backend->list_cidr;
511  
512 =cut
513  
514 0 ▶sub list_cidr {
Subroutine list_cidr: not called CC=2 SCAR=17.9
Pod documented
515 0 my ( $self, %opts ) = @_;
516  
517 0 $self->errorblank;
518  
519 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
520 0 $self->{frontend_obj}->{test_data} = 'list_cidr';
521 }
522  
523 0 return keys( %{ $self->{banned_cidr} } );
524 }
525  
526 =head2 list
527  
528 List banned IPs. Returns an array of the currently banned single IPs. CIDR
529 ranges are not included; for those see L</list_cidr>.
530  
531 my @banned = $backend->list;
532  
533 =cut
534  
535 1 ▶sub list {
Subroutine list: called CC=2 SCAR=7.0
Pod documented
536 1 my ( $self, %opts ) = @_;
537  
538 1 $self->errorblank;
539  
540 1 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 1 0 1
541 1 $self->{frontend_obj}->{test_data} = 'list';
542 }
543  
544 1 return keys( %{ $self->{banned} } );
545 }
546  
547 =head2 re_init
548  
549 Tells the backend to re-init it's self. As this is the dummy testing
550 backend, this just marks it as inited again; the internal ban lists are
551 retained.
552  
553 =cut
554  
555 0 ▶sub re_init {
Subroutine re_init: not called CC=2 SCAR=17.9
Pod documented
556 0 my ( $self, %opts ) = @_;
557  
558 0 $self->errorblank;
559  
560 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
561 0 $self->{frontend_obj}->{test_data} = 're_inited';
562 }
563  
564 0 $self->{inited} = 1;
565 }
566  
567 =head2 teardown
568  
569 Tears down the setup for the backend. As this is the dummy testing backend,
570 there is nothing external to remove; the backend is just marked as not
571 inited. The internal ban lists are kept.
572  
573 $backend->teardown;
574  
575 =cut
576  
577 1 ▶sub teardown {
Subroutine teardown: called CC=2 SCAR=7.0
Pod documented
578 1 my ( $self, %opts ) = @_;
579  
580 1 $self->errorblank;
581  
582 1 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 1 0 1
583 1 $self->{frontend_obj}->{test_data} = 'teardown';
584 }
585  
586 1 $self->{inited} = 0;
587 } ## end sub teardown
588  
589 =head2 stop
590  
591 Alias for L</teardown>, provided for parity with the fail2ban C<actionstop>
592 concept.
593  
594 $backend->stop;
595  
596 =cut
597  
598 0 ▶sub stop {
Subroutine stop: not called CC=1 SCAR=6.9
Pod documented
599 0 my ( $self, %opts ) = @_;
600  
601 0 return $self->teardown(%opts);
602 }
603  
604 =head2 check
605  
606 Dummy check. Always reports the setup as intact. This is the equivalent of
607 fail2ban's C<actioncheck>.
608  
609 my $healthy = $backend->check;
610  
611 =cut
612  
613 2 ▶sub check {
Subroutine check: called CC=2 SCAR=7.0
Pod documented
614 2 my ( $self, %opts ) = @_;
615  
616 2 $self->errorblank;
617  
618 2 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 2 0 2
619 2 $self->{frontend_obj}->{test_data} = 'check';
620 }
621  
622 2 return 1;
623 }
624  
625 =head2 flush
626  
627 Dummy flush. Clears the internal lists of banned IPs and CIDR ranges. This
628 is the equivalent of fail2ban's C<actionflush>.
629  
630 $backend->flush;
631  
632 =cut
633  
634 1 ▶sub flush {
Subroutine flush: called CC=2 SCAR=7.0
Pod documented
635 1 my ( $self, %opts ) = @_;
636  
637 1 $self->errorblank;
638  
639 1 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 1 0 1
640 1 $self->{frontend_obj}->{test_data} = 'flush';
641 }
642  
643 1 $self->{banned} = {};
644 1 $self->{banned_cidr} = {};
645  
646 1 return;
647 }
648  
649 =head1 ERROR CODES / FLAGS
650  
651 Error handling is provided by L<Error::Helper>. All
652 errors are considered fatal.
653  
654 =head2 1, notInited
655  
656 The backend has not been inited yet.
657  
658 =head2 2, invalidPortSpecified
659  
660 Port is either not an int within the range 1 to 65535 or a name that can be resolved by getservbyname.
661  
662 =head2 3, portsNotArray
663  
664 The data passed to new for ports is not an array.
665  
666 =head2 4, protocolsNotArray
667  
668 The data passed to new for protocols is not an array.
669  
670 =head2 5, invalidPortSpecified
671  
672 Port is either not an int within the range 1 to 65535 or a name that can be resolved by getservbyname.
673  
674 =head2 6, invalidPrefixSpecified
675  
676 The specified prefix did not match /^[a-zA-Z0-9]+$/.
677  
678 =head2 7, invalidName
679  
680 The name is either undef or does not match /^[a-zA-Z0-9\-]+$/.
681  
682 =head2 8, optionsNotHash
683  
684 The item passed to new for options is not a hash.
685  
686 =head2 9, noBanItem
687  
688 No IP or CIDR range specified to ban or unban.
689  
690 =head2 10, banItemNotIP
691  
692 The item to ban is not an IP. Either wrong ref type or regexp
693 test using L<Regexp::IPv4> and L<Regexp::IPv6> failed.
694  
695 =head2 11, invalidBackend
696  
697 The specified backend failed to pass a basic sanity check of making sure it
698 matches the regexp /^[a-zA-Z0-9\_]+$/.
699  
700 =head2 12, backendInitError
701  
702 Failed to init the backend.
703  
704 =head2 13, banFailed
705  
706 Failed to ban the item.
707  
708 =head2 14, unbanFailed
709  
710 Failed to unban the item.
711  
712 =head2 15, listFailed
713  
714 Failed to get a list of bans.
715  
716 =head2 16, reInitFailed
717  
718 Failed to re_init the backend.
719  
720 =head2 17, teardownFailed
721  
722 Failed to teardown the backend.
723  
724 =head2 18, alreadyInited
725  
726 init called, but the backend has already been inited.
727  
728 =head2 24, checkFailed
729  
730 The backend check raised an error.
731  
732 =head2 25, flushFailed
733  
734 Failed to flush the bans.
735  
736 =head2 26, banCidrFailed
737  
738 Failed to ban the CIDR range.
739  
740 =head2 27, unbanCidrFailed
741  
742 Failed to unban the CIDR range.
743  
744 =head2 28, cidrItemNotCidr
745  
746 The item to ban is not a CIDR range. Either wrong ref type or it is not an
747 IPv4 or IPv6 address followed by a prefix length valid for its family.
748  
749 =head2 29, cidrNotSupported
750  
751 The backend does not support CIDR bans.
752  
753 =head2 30, listCidrFailed
754  
755 Failed to get a list of CIDR bans.
756  
757 =head1 AUTHOR
758  
759 Zane C. Bowers-Hadley, C<< <vvelox at vvelox.ent> >>
760  
761 =head1 BUGS
762  
763 Please report any bugs or feature requests to C<bug-net-firewall-blockerhelper at rt.cpan.org>, or through
764 the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Net-Firewall-BlockerHelper>. I will be notified, and then you'll
765 automatically be notified of progress on your bug as I make changes.
766  
767  
768  
769  
770 =head1 SUPPORT
771  
772 You can find documentation for this module with the perldoc command.
773  
774 perldoc Net::Firewall::BlockerHelper
775  
776  
777 You can also look for information at:
778  
779 =over 4
780  
781 =item * RT: CPAN's request tracker (report bugs here)
782  
783 L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=Net-Firewall-BlockerHelper>
784  
785 =item * Search CPAN
786  
787 L<https://metacpan.org/release/Net-Firewall-BlockerHelper>
788  
789 =back
790  
791  
792 =head1 ACKNOWLEDGEMENTS
793  
794  
795 =head1 LICENSE AND COPYRIGHT
796  
797 This software is Copyright (c) 2023 by Zane C. Bowers-Hadley.
798  
799 This is free software, licensed under:
800  
801 The GNU Lesser General Public License, Version 2.1, February 1999
802  
803  
804 =cut
805  
806 3 1; # End of Net::Firewall::BlockerHelper
« BlockerHelper/backends/pf.pm ↑ Summary BlockerHelper/backends/fastly.pm »