BlockerHelper.pm

filter: Statementstmt 68.2% 267 / 391 Branchbran 76.9% 97 / 126 Conditioncond 83.3% 30 / 36 MC/DCmcdc 76.1% 16 / 21 Subroutinesub 95.2% 20 / 21 Podpod 100% 14 / 14 totaltotal 72.9% 444 / 609 CC 78 SCAR 26.2
CC
78
Cov
71%
new
27.9
commit
20.0
unban_cidr
19.7
CRAP 13.7
« BlockerHelper/backends/hosts_deny.pm ↑ Summary BlockerHelper/backends/cloud_armor.pm »
1 package Net::Firewall::BlockerHelper;
2  
3 52 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 52 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 52 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 52 ▶use base qw( Error::Helper Net::Firewall::BlockerHelper::Util );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 52 ▶use Regexp::IPv4 qw($IPv4_re);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 52 ▶use Regexp::IPv6 qw($IPv6_re);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 =head1 NAME
11  
12 Net::Firewall::BlockerHelper - Helps with managing firewalls for banning IPs.
13  
14 =head1 VERSION
15  
16 Version 0.2.0
17  
18 =cut
19  
20 52 our $VERSION = '0.2.0';
21  
22 =head1 SYNOPSIS
23  
24 use Net::Firewall::BlockerHelper;
25  
26 # create a instance named ssh with a ipfw backend for port 22 tcp
27 my $fw_helper;
28 eval {
29 $fw_helper = Net::Firewall::BlockerHelper->new(
30 backend => 'ipfw',
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 # start the backend
46 $fw_helper->init_backend;
47  
48 # ban some IPs
49 $fw_helper->ban(ban => '1.2.3.4');
50 $fw_helper->ban(ban => '5.6.7.8');
51  
52 # unban a IP
53 $fw_helper->unban(ban => '1.2.3.4');
54  
55 # get a list of banned IPs
56 my @banned = $fw_helper->list;
57 foreach my $ip (@banned) {
58 print 'Banned IP: '.$ip."\n";
59 }
60  
61 # teardown the backend, re-init, and re-ban everything
62 $fw_helper->re_init;
63  
64 # teardown the backend
65 $fw_helper->teardown;
66  
67 =head1 METHODS
68  
69 =head2 new
70  
71 Initiates the the object.
72  
73 - backend :: The backend to use. This must be specified.
74 - Default :: undef
75  
76 - options :: Backend specific options that will be passed to the backend unchecked
77 outside of making sure it is a hash ref if defined.
78 - Default :: {}
79  
80 - ports :: A array of ports to block. Checked to make sure they are ints within
81 the range 1 to 65535 or a valid service name via getservbyname.
82 - Default :: []
83  
84 - protocols :: A array of protocols to block. By default will block all. This
85 is checked against /etc/protocols via the function getprotobyname.
86 - Default :: []
87  
88 - prefix :: Prefix to use. Must match the regex /^[a-zA-Z0-9]+$/
89 - default :: kur
90  
91 - name :: Name of this specific instance.
92 - default :: undef
93  
94 - self_heal :: Before each ban or unban, verify the firewall setup is
95 still present (via the backend's check) and re_init it if it was
96 removed externally. This is the fail2ban actioncheck-before-action
97 behavior. Adds one check probe per ban/unban. Can be overridden per
98 call by passing self_heal to ban/unban.
99 - default :: 1
100  
101 All errors are considered fatal, meaning if new fails it will die.
102  
103 my $fw_helper;
104 eval {
105 $fw_helper = Net::Firewall::BlockerHelper->new(
106 backend => 'ipfw',
107 ports => ['22'],
108 protocols => ['tcp'],
109 name => 'ssh',
110 );
111 };
112 if ($@) {
113 print 'Error: '
114 . $Error::Helper::error
115 . "\nError String: "
116 . $Error::Helper::errorString
117 . "\nError Flag: "
118 . $Error::Helper::errorFlag . "\n";
119 }
120  
121 =cut
122  
123 187 ▶sub new {
Subroutine new: called CC=27 SCAR=33.3
Pod documented
124 187 my ( $blank, %opts ) = @_;
125  
126 187 my $self = {
127 perror => undef,
128 error => undef,
129 errorLine => undef,
130 errorFilename => undef,
131 errorString => "",
132 errorExtra => {
133 all_errors_fatal => 1,
134 # all_fatal is what Error::Helper 2.1.0 actually checks; all_errors_fatal
135 # is kept for the name documented in its POD
136 all_fatal => 1,
137 flags => {
138 1 => 'noBackendSpecified',
139 2 => 'invalidPortSpecified',
140 3 => 'portsNotArray',
141 4 => 'protocolsNotArray',
142 5 => 'invalidPortSpecified',
143 6 => 'invalidPrefixSpecified',
144 7 => 'invalidName',
145 8 => 'optionsNotHash',
146 9 => 'noBanItem',
147 10 => 'banItemNotIP',
148 11 => 'invalidBackend',
149 12 => 'backendInitError',
150 13 => 'banFailed',
151 14 => 'unbanFailed',
152 15 => 'listFailed',
153 16 => 'reInitFailed',
154 17 => 'teardownFailed',
155 24 => 'checkFailed',
156 25 => 'flushFailed',
157 26 => 'banCidrFailed',
158 27 => 'unbanCidrFailed',
159 28 => 'cidrItemNotCidr',
160 29 => 'cidrNotSupported',
161 30 => 'listCidrFailed',
162 31 => 'commitFailed',
163 32 => 'commitNotSupported',
164 },
165 fatal_flags => {},
166 perror_not_fatal => 0,
167 },
168 backend => undef,
169 options => {},
170 ports => [],
171 protocols => [],
172 testing => undef,
173 test_data => undef,
174 prefix => 'kur',
175 name => undef,
176 backend_obj => undef,
177 self_heal => 1,
178 };
179 187 bless $self;
180  
181 187 ▶ if ( !defined $opts{backend} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $opts{"backend"}) 1 186 187
182 1 $self->{perror} = 1;
183 1 $self->{error} = 1;
184 1 $self->{errorString} = 'backend is undef';
185 1 $self->warn;
186 }
187 186 $self->{backend} = $opts{backend};
188  
189 186 ▶ if ( $self->{backend} !~ /^[a-zA-Z0-9\_]+$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"backend"} =~ /^[a-zA-Z0-9\_]+$/) 1 185 186
190 1 $self->{perror} = 1;
191 1 $self->{error} = 11;
192 $self->{errorString}
193 = '"'
194 . $self->{backend}
195 1 . '" does not appear to be valid backend, the regexp /^[a-zA-Z0-9\_]+$/ does not match';
196 1 $self->warn;
197 }
198  
199 185 ▶ if ( defined( $opts{ports} ) && ref( $opts{ports} ) ne 'ARRAY' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $opts{'ports'} and ref $opts{'ports'} ne 'ARRAY') { } 1 184 185
elsif (defined $opts{'ports'}) { } 25 159 184
Condition100%3/3
defined $opts{"ports"} and ref $opts{"ports"} ne "ARRAY"
!ll&&!rl&&r
159251
MC/DC100%2/2
defined $opts{"ports"} and ref $opts{"ports"} ne "ARRAY"
defined $opts{"ports"} ref $opts{"ports"} ne "ARRAY"
Truth table100%3/3
A and B
A= defined $opts{"ports"}B= ref $opts{"ports"} ne "ARRAY"
ABresult
0X0
100
111
200 1 $self->{perror} = 1;
201 1 $self->{error} = 3;
202 1 $self->{errorString} = 'ports is defined and type is not array but "' . ref( $opts{ports} ) . '"';
203 1 $self->warn;
204 } elsif ( defined( $opts{ports} ) ) {
205 25 my %ports;
206 25 foreach my $item ( @{ $opts{ports} } ) {
207 31 ▶ if ( $item =~ /^[0-9]+$/ && $item >= 1 && $item <= 65535 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($item =~ /^[0-9]+$/ and $item >= 1 and $item <= 65535) { } 23 8 31
elsif ($item =~ /^[0-9]+$/) { } 2 6 8
Condition100%6/6
$item =~ /^[0-9]+$/ and $item >= 1 and $item <= 65535
!ll&&!rl&&r
7123
$item =~ /^[0-9]+$/ and $item >= 1
!ll&&!rl&&r
6124
MC/DC100%3/3
$item =~ /^[0-9]+$/ and $item >= 1 and $item <= 65535
$item =~ /^[0-9]+$/ $item >= 1 $item <= 65535
Truth table100%4/4
A and B and C
A= $item =~ /^[0-9]+$/B= $item >= 1C= $item <= 65535
ABCresult
0XX0
10X0
1100
1111
208 #push( @{ $self->{ports} }, $item );
209 23 $ports{$item} = 1;
210 } elsif ( $item =~ /^[0-9]+$/ ) {
211 2 $self->{perror} = 1;
212 2 $self->{error} = 2;
213 $self->{errorString}
214 2 = $item . ' is not a valid value for a port as it must be a int within the range 1 to 65535';
215 2 $self->warn;
216 } else {
217 # just using tcp here as protocol must be specified
218 6 my ( $name, $aliases, $port, $proto ) = getservbyname( $item, 'tcp' );
219 6 ▶ if ( !defined($port) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $port) 6 0 6
220 6 $self->{perror} = 1;
221 6 $self->{error} = 2;
222 $self->{errorString}
223 6 = $item . ' could not be resolved to a port name via getservbyname("' . $item . '", "tcp")';
224 6 $self->warn;
225 }
226 0 $ports{$port} = 1;
227 #push( @{ $self->{ports} }, $port );
228 } ## end else [ if ( $item =~ /^[0-9]+$/ && $item >= 1 && ...)]
229 } ## end foreach my $item ( @{ $opts{ports} } )
230 17 my @port_keys = keys(%ports);
231 17 @port_keys = sort { $a <=> $b } @port_keys;
232 17 push( @{ $self->{ports} }, @port_keys );
233 ## end foreach my $item ( @{ $opts{ports} } )
234 } ## end elsif ( defined( $opts{ports} ) )
235  
236 176 ▶ if ( defined( $opts{protocols} ) && ref( $opts{protocols} ) ne 'ARRAY' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $opts{'protocols'} and ref $opts{'protocols'} ne 'ARRAY') { } 1 175 176
elsif (defined $opts{'protocols'}) { } 13 162 175
Condition100%3/3
defined $opts{"protocols"} and ref $opts{"protocols"} ne "ARRAY"
!ll&&!rl&&r
162131
MC/DC100%2/2
defined $opts{"protocols"} and ref $opts{"protocols"} ne "ARRAY"
defined $opts{"protocols"} ref $opts{"protocols"} ne "ARRAY"
Truth table100%3/3
A and B
A= defined $opts{"protocols"}B= ref $opts{"protocols"} ne "ARRAY"
ABresult
0X0
100
111
237 1 $self->{perror} = 1;
238 1 $self->{error} = 4;
239 1 $self->{errorString} = 'protocols is defined and type is not array but "' . ref( $opts{protocols} ) . '"';
240 1 $self->warn;
241 } elsif ( defined( $opts{protocols} ) ) {
242 13 my %protocols;
243 13 foreach my $item ( @{ $opts{protocols} } ) {
244 13 my ( $name, $aliases, $proto ) = getprotobyname($item);
245 # if this is undef, it means it is not a known protocol
246 13 ▶ if ( !defined($proto) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $proto) 13 0 13
247 13 $self->{perror} = 1;
248 13 $self->{error} = 5;
249 $self->{errorString}
250 13 = $item . ' could not be resolved to a protocol via getprotobyname("' . $item . '")';
251 13 $self->warn;
252 }
253 0 $protocols{$item} = 1;
254 } ## end foreach my $item ( @{ $opts{protocols} } )
255 0 my @protocols_keys = keys(%protocols);
256 0 @protocols_keys = sort { $a cmp $b } @protocols_keys;
257 0 push( @{ $self->{protocols} }, @protocols_keys );
258 } ## end elsif ( defined( $opts{protocols} ) )
259  
260 # make sure prefix is sane if defiend
261 162 ▶ 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]+$/) { } 1 161 162
elsif (defined $opts{'prefix'}) { } 25 136 161
Condition100%3/3
defined $opts{"prefix"} and not $opts{"prefix"} =~ /^[a-zA-Z0-9]+$/
!ll&&!rl&&r
136251
MC/DC100%2/2
defined $opts{"prefix"} and not $opts{"prefix"} =~ /^[a-zA-Z0-9]+$/
defined $opts{"prefix"} not $opts{"prefix"} =~ /^[a-zA-Z0-9]+$/
Truth table100%3/3
A and not(B)
A= defined $opts{"prefix"}B= not $opts{"prefix"} =~ /^[a-zA-Z0-9]+$/
ABresult
0X0
100
111
262 1 $self->{perror} = 1;
263 1 $self->{error} = 6;
264 $self->{errorString}
265 1 = '"' . $opts{prefix} . '" is not a valid prefix as it does not match the regex /^[a-zA-Z0-9]+$/';
266 1 $self->warn;
267 } elsif ( defined( $opts{prefix} ) ) {
268 25 $self->{prefix} = $opts{prefix};
269 }
270  
271 # make sure we have a name and that it is valid
272 161 ▶ if ( !defined( $opts{name} ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $opts{'name'}) { } 1 160 161
elsif (not $opts{'name'} =~ /^[a-zA-Z0-9\-]+$/) { } 0 160 160
273 1 $self->{perror} = 1;
274 1 $self->{error} = 7;
275 1 $self->{errorString} = 'name is undef';
276 1 $self->warn;
277 } elsif ( $opts{name} !~ /^[a-zA-Z0-9\-]+$/ ) {
278 0 $self->{perror} = 1;
279 0 $self->{error} = 7;
280 0 $self->{errorString} = 'name set to "' . $opts{name} . '" which does not match the regexp /^[a-zA-Z0-9\-]+$/';
281 0 $self->warn;
282 }
283 160 $self->{name} = $opts{name};
284  
285 # used internally for testing
286 160 ▶ if ( defined( $opts{testing} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"testing"}) 145 15 160
287 145 $self->{testing} = $opts{testing};
288 }
289  
290 # check-before-ban self healing, on by default
291 160 ▶ if ( defined( $opts{self_heal} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"self_heal"}) 3 157 160
292 3 ▶ $self->{self_heal} = $opts{self_heal} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$opts{'self_heal'} ? : 0 3 3
293 }
294  
295 160 ▶ if ( defined( $opts{options} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"options"}) 144 16 160
296 144 ▶ if ( ref( $opts{options} ) ne 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $opts{"options"} ne "HASH") 2 142 144
297 2 $self->{perror} = 1;
298 2 $self->{error} = 8;
299 2 $self->{errorString} = 'ref for options is "' . ref( $opts{options} ) . '" and not HASH';
300 2 $self->warn;
301 }
302 142 $self->{options} = $opts{options};
303 }
304  
305 158 return $self;
306 } ## end sub new
307  
308 =head2 init_backend
309  
310 Initiates the backend, creating the backend object and running its init. A
311 failure is raised as backendInitError.
312  
313 No arguments are taken.
314  
315 $fw_helper->init_backend;
316  
317 =cut
318  
319 148 ▶sub init_backend {
Subroutine init_backend: called CC=6 SCAR=20.7
Pod documented
320 148 my ( $self, %opts ) = @_;
321  
322 148 $self->errorblank;
323 148 $self->{test_data} = undef;
324  
325 148 my $backend = 'Net::Firewall::BlockerHelper::backends::' . $self->{backend};
326 148 my $backend_obj;
327 eval {
328 # runtime module load: translate Foo::Bar -> Foo/Bar.pm and require it.
329 # $self->{backend} is already validated /^[a-zA-Z0-9_]+$/ in new, so this path is safe.
330 148 ( my $file = $backend ) =~ s{::}{/}g;
331 148 require $file . '.pm';
332  
333 $backend_obj = $backend->new(
334 options => $self->{options},
335 ports => $self->{ports},
336 protocols => $self->{protocols},
337 testing => $self->{testing},
338 prefix => $self->{prefix},
339 name => $self->{name},
340 148 frontend_obj => $self,
341 );
342 79 $backend_obj->init;
343 78 1;
344 148 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { (my $file = $backend) =~ s[::][/]g; require $file . '.pm'; $backend_obj = $backend->new("options", $self->{"options"}, "ports", $self->{"ports"}, "protocols", $self->{"protocols"}, "testing", $self->{"testing"}, "prefix", $self->{"prefix"}, "name", $self->{"name"}, "frontend_obj", $self); $backend_obj->init; 1 } } or do { $self->{"perror"} = 1; $self->{"error"} = 12; $self->{"errorString"} = 'Failed to init backend... ' . $@; $self->warn } 78 70 148
345 70 $self->{perror} = 1;
346 70 $self->{error} = 12;
347 70 $self->{errorString} = 'Failed to init backend... ' . $@;
348 70 $self->warn;
349 };
350 # make sure we got something that is defined and is a object of some sort
351 78 ▶ if ( !defined($backend_obj) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $backend_obj) { } 0 78 78
elsif (ref $backend_obj eq '' or ref $backend_obj eq 'ARRAY' or ref $backend_obj eq 'HASH') { } 0 78 78
352 0 $self->{perror} = 1;
353 0 $self->{error} = 12;
354 0 $self->{errorString} = 'Failed to init backend. Eval did not die, but returned value is undef';
355 0 $self->warn;
356 ▶ } elsif ( ref($backend_obj) eq '' || ref($backend_obj) eq 'ARRAY' || ref($backend_obj) eq 'HASH' ) {
Condition33%2/6
ref $backend_obj eq "" or ref $backend_obj eq "ARRAY" or ref $backend_obj eq "HASH"
l!l&&r!l&&!r
0078
ref $backend_obj eq "" or ref $backend_obj eq "ARRAY"
l!l&&r!l&&!r
0078
MC/DC0%0/3
ref $backend_obj eq "" or ref $backend_obj eq "ARRAY" or ref $backend_obj eq "HASH"
ref $backend_obj eq "" ref $backend_obj eq "ARRAY" ref $backend_obj eq "HASH"
Truth table25%1/4
A or B or C
A= ref $backend_obj eq ""B= ref $backend_obj eq "ARRAY"C= ref $backend_obj eq "HASH"
ABCresult
1XX1
01X1
0011
0000
357 0 $self->{perror} = 1;
358 0 $self->{error} = 12;
359 0 $self->{errorString} = 'ref($backend_obj) is "' . ref($backend_obj) . '"';
360 0 $self->warn;
361 }
362  
363 78 $self->{backend_obj} = $backend_obj;
364 } ## end sub init_backend
365  
366 # Internal helper. If self healing is enabled and the backend is inited, asks
367 # the backend to check that its firewall setup is still present and re_inits it
368 # if it is not. This is the fail2ban actioncheck-before-action behavior: the
369 # setup can be torn down out from under a long lived process by a firewall
370 # restart or by someone flushing the rules by hand, and without this the
371 # following ban would appear to succeed while adding nothing.
372 #
373 # Called at the top of ban, unban, ban_cidr, and unban_cidr, which pass their
374 # own %opts straight through. Nothing happens at all if self healing is off,
375 # if no backend has been inited yet, or if the backend's inited flag is false.
376 #
377 # Both the check and the re_init are best effort and run inside eval. A check
378 # that dies is treated as "do not heal" rather than as unhealthy, so a backend
379 # whose check is broken does not trigger a rebuild on every call. A re_init
380 # that dies is swallowed here and left for the ban or unban that follows to
381 # surface, which keeps the error the caller sees tied to the operation they
382 # actually asked for.
383 #
384 # Args:
385 #
386 # %opts - The option hash the calling method received, passed through
387 # verbatim. Only the self_heal key is looked at, and only if it
388 # exists; it is a per-call override of the object's self_heal
389 # setting and is taken as a boolean, so 0 disables healing for
390 # this call and 1 forces it on. Every other key is ignored.
391 #
392 # Returns nothing. Whether the setup was checked, found unhealthy, or rebuilt
393 # is deliberately not reported; callers proceed with their ban or unban either
394 # way.
395 #
396 # # honor the object's self_heal setting
397 # $self->_self_heal;
398 #
399 # # inside ban, passing the caller's options through
400 # $self->_self_heal(%opts);
401 #
402 # # skip healing for this one call
403 # $self->_self_heal( self_heal => 0 );
404 212 ▶sub _self_heal {
Subroutine _self_heal: called CC=8 SCAR=20.8
405 212 my ( $self, %opts ) = @_;
406  
407 212 my $heal = $self->{self_heal};
408 212 ▶ $heal = ( $opts{self_heal} ? 1 : 0 ) if ( exists( $opts{self_heal} ) );
Branch: 2 branches
BranchTrueFalseTotal
if exists $opts{"self_heal"} 3 209 212
$opts{'self_heal'} ? : 1 2 3
409  
410 212 ▶ if ( $heal
Branch: 1 branch
BranchTrueFalseTotal
if ($heal and defined $self->{"backend_obj"} and $self->{"backend_obj"}{"inited"}) 204 8 212
Condition83%5/6
$heal and defined $self->{"backend_obj"} and $self->{"backend_obj"}{"inited"}
!ll&&!rl&&r
71204
$heal and defined $self->{"backend_obj"}
!ll&&!rl&&r
70205
MC/DC66%2/3
$heal and defined $self->{"backend_obj"} and $self->{"backend_obj"}{"inited"}
$heal defined $self->{"backend_obj"} $self->{"backend_obj"}{"inited"}
Truth table75%3/4
A and B and C
A= $healB= defined $self->{"backend_obj"}C= $self->{"backend_obj"}{"inited"}
ABCresult
0XX0
10X0
1100
1111
411 && defined( $self->{backend_obj} )
412 && $self->{backend_obj}->{inited} )
413 {
414 204 my $healthy;
415 204 eval { $healthy = $self->{backend_obj}->check; };
416 204 ▶ if ( !$@ && !$healthy ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($@ or $healthy) 5 199 204
Condition66%2/3
$@ or $healthy
l!l&&r!l&&!r
01995
MC/DC50%1/2
$@ or $healthy
$@ $healthy
Truth table66%2/3
A or B
A= $@B= $healthy
ABresult
1X1
011
000
417 # the setup was removed externally, rebuild it and re-add the bans
418 5 eval { $self->{backend_obj}->re_init; };
419 }
420 } ## end if ( $heal && defined( $self->{backend_obj...}))
421  
422 212 return;
423 } ## end sub _self_heal
424  
425 =head2 ban
426  
427 Bans an IP. The value of ban is validated as being a IPv4 or IPv6 address
428 and lowercased, then handed to the backend to ban. If self healing is
429 enabled, the backend setup is checked and restored first. A backend failure
430 is caught and re-raised as banFailed.
431  
432 $fw_helper->ban(ban => $ip);
433  
434 =cut
435  
436 130 ▶sub ban {
Subroutine ban: called CC=7 SCAR=19.6
Pod documented
437 130 my ( $self, %opts ) = @_;
438  
439 130 $self->errorblank;
440 130 $self->{test_data} = undef;
441  
442 130 ▶ if ( !defined( $opts{ban} ) ) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $opts{'ban'}) { } 1 129 130
elsif (ref $opts{'ban'} ne '') { } 1 128 129
elsif (not $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::IPv6_re\z/) { } 1 127 128
443 1 $self->{error} = 9;
444 1 $self->{errorString} = 'Nothing specified for the value ban';
445 1 $self->warn;
446 0 return;
447 } elsif ( ref( $opts{ban} ) ne '' ) {
448 1 $self->{error} = 10;
449 1 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
450 1 $self->warn;
451 0 return;
452 ▶ } elsif ( $opts{ban} !~ /\A$IPv4_re\z/
Condition100%3/3
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::IPv6_re\z/
l!l&&r!l&&!r
97301
MC/DC100%2/2
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::IPv6_re\z/
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::IPv4_re\z/ $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::IPv6_re\z/
Truth table100%3/3
A || B
A= $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::IPv4_re\z/B= $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::IPv6_re\z/
ABresult
1X1
011
000
453 && $opts{ban} !~ /\A$IPv6_re\z/ )
454 {
455 1 $self->{error} = 10;
456 1 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 IP';
457 1 $self->warn;
458 0 return;
459 }
460  
461 127 ▶ if ( !defined( $self->{backend_obj} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"backend_obj"}) 1 126 127
462 1 $self->{error} = 13;
463 1 $self->{errorString} = 'No backend object present... init_backend has not been called';
464 1 $self->warn;
465 0 return;
466 }
467  
468 # lowercase so the same IPv6 IP in differing cases can't result in duplicate entries
469 126 $opts{ban} = lc( $opts{ban} );
470  
471 126 $self->_self_heal(%opts);
472  
473 126 eval { $self->{backend_obj}->ban( ban => $opts{ban} ); };
474 126 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 5 121 126
475 5 $self->{error} = 13;
476 5 $self->{errorString} = 'banning item,"' . $opts{ban} . '", failed... ' . $@;
477 5 $self->warn;
478 0 return;
479 }
480 } ## end sub ban
481  
482 =head2 ban_cidr
483  
484 Bans a CIDR range.
485  
486 Only backends whose underlying firewall can match on a network prefix support
487 this. For backends that do not, the cidrNotSupported error is set. See the
488 individual backends for which support it.
489  
490 $fw_helper->ban_cidr(ban => '1.2.3.0/24');
491  
492 =cut
493  
494 34 ▶sub ban_cidr {
Subroutine ban_cidr: called CC=7 SCAR=24.9
Pod documented
495 34 my ( $self, %opts ) = @_;
496  
497 34 $self->errorblank;
498 34 $self->{test_data} = undef;
499  
500 34 ▶ if ( !defined( $opts{ban} ) ) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $opts{'ban'}) { } 0 34 34
elsif (ref $opts{'ban'} ne '') { } 0 34 34
elsif (not $self->_valid_cidr($opts{'ban'})) { } 0 34 34
501 0 $self->{error} = 9;
502 0 $self->{errorString} = 'Nothing specified for the value ban';
503 0 $self->warn;
504 0 return;
505 } elsif ( ref( $opts{ban} ) ne '' ) {
506 0 $self->{error} = 28;
507 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
508 0 $self->warn;
509 0 return;
510 } elsif ( !$self->_valid_cidr( $opts{ban} ) ) {
511 0 $self->{error} = 28;
512 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 CIDR';
513 0 $self->warn;
514 0 return;
515 }
516  
517 34 ▶ if ( !defined( $self->{backend_obj} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"backend_obj"}) 0 34 34
518 0 $self->{error} = 26;
519 0 $self->{errorString} = 'No backend object present... init_backend has not been called';
520 0 $self->warn;
521 0 return;
522 }
523  
524 34 ▶ if ( !$self->{backend_obj}->{cidr_supported} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"backend_obj"}{"cidr_supported"}) 6 28 34
525 6 $self->{error} = 29;
526 6 $self->{errorString} = 'the "' . $self->{backend} . '" backend does not support CIDR bans';
527 6 $self->warn;
528 0 return;
529 }
530  
531 # lowercase so the same IPv6 CIDR in differing cases can't result in duplicate entries
532 28 $opts{ban} = lc( $opts{ban} );
533  
534 28 $self->_self_heal(%opts);
535  
536 28 eval { $self->{backend_obj}->ban_cidr( ban => $opts{ban} ); };
537 28 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 1 27 28
538 1 $self->{error} = 26;
539 1 $self->{errorString} = 'banning cidr item,"' . $opts{ban} . '", failed... ' . $@;
540 1 $self->warn;
541 0 return;
542 }
543 } ## end sub ban_cidr
544  
545 =head2 unban
546  
547 Unbans an IP. The value of ban is validated as being a IPv4 or IPv6 address
548 and lowercased, then handed to the backend to unban. If self healing is
549 enabled, the backend setup is checked and restored first. A backend failure
550 is caught and re-raised as unbanFailed.
551  
552 $fw_helper->unban(ban => $ip);
553  
554 =cut
555  
556 37 ▶sub unban {
Subroutine unban: called CC=7 SCAR=21.2
Pod documented
557 37 my ( $self, %opts ) = @_;
558  
559 37 $self->errorblank;
560 37 $self->{test_data} = undef;
561  
562 37 ▶ if ( !defined( $opts{ban} ) ) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $opts{'ban'}) { } 0 37 37
elsif (ref $opts{'ban'} ne '') { } 0 37 37
elsif (not $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::IPv6_re\z/) { } 1 36 37
563 0 $self->{error} = 9;
564 0 $self->{errorString} = 'Nothing specified for the value ban';
565 0 $self->warn;
566 0 return;
567 } elsif ( ref( $opts{ban} ) ne '' ) {
568 0 $self->{error} = 10;
569 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
570 0 $self->warn;
571 0 return;
572 ▶ } elsif ( $opts{ban} !~ /\A$IPv4_re\z/
Condition100%3/3
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::IPv6_re\z/
l!l&&r!l&&!r
3331
MC/DC100%2/2
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::IPv6_re\z/
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::IPv4_re\z/ $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::IPv6_re\z/
Truth table100%3/3
A || B
A= $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::IPv4_re\z/B= $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::IPv6_re\z/
ABresult
1X1
011
000
573 && $opts{ban} !~ /\A$IPv6_re\z/ )
574 {
575 1 $self->{error} = 10;
576 1 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 IP';
577 1 $self->warn;
578 0 return;
579 }
580  
581 36 ▶ if ( !defined( $self->{backend_obj} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"backend_obj"}) 1 35 36
582 1 $self->{error} = 14;
583 1 $self->{errorString} = 'No backend object present... init_backend has not been called';
584 1 $self->warn;
585 0 return;
586 }
587  
588 # lowercase so the same IPv6 IP in differing cases can't result in duplicate entries
589 35 $opts{ban} = lc( $opts{ban} );
590  
591 35 $self->_self_heal(%opts);
592  
593 35 eval { $self->{backend_obj}->unban( ban => $opts{ban} ); };
594 35 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 1 34 35
595 1 $self->{error} = 14;
596 1 $self->{errorString} = 'unbanning item,"' . $opts{ban} . '", failed... ' . $@;
597 1 $self->warn;
598 0 return;
599 }
600 } ## end sub unban
601  
602 =head2 unban_cidr
603  
604 Unbans a CIDR range.
605  
606 Only backends whose underlying firewall can match on a network prefix support
607 this. For backends that do not, the cidrNotSupported error is set.
608  
609 $fw_helper->unban_cidr(ban => '1.2.3.0/24');
610  
611 =cut
612  
613 23 ▶sub unban_cidr {
Subroutine unban_cidr: called CC=7 SCAR=29.8
Pod documented
614 23 my ( $self, %opts ) = @_;
615  
616 23 $self->errorblank;
617 23 $self->{test_data} = undef;
618  
619 23 ▶ if ( !defined( $opts{ban} ) ) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $opts{'ban'}) { } 0 23 23
elsif (ref $opts{'ban'} ne '') { } 0 23 23
elsif (not $self->_valid_cidr($opts{'ban'})) { } 0 23 23
620 0 $self->{error} = 9;
621 0 $self->{errorString} = 'Nothing specified for the value ban';
622 0 $self->warn;
623 0 return;
624 } elsif ( ref( $opts{ban} ) ne '' ) {
625 0 $self->{error} = 28;
626 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
627 0 $self->warn;
628 0 return;
629 } elsif ( !$self->_valid_cidr( $opts{ban} ) ) {
630 0 $self->{error} = 28;
631 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 CIDR';
632 0 $self->warn;
633 0 return;
634 }
635  
636 23 ▶ if ( !defined( $self->{backend_obj} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"backend_obj"}) 0 23 23
637 0 $self->{error} = 27;
638 0 $self->{errorString} = 'No backend object present... init_backend has not been called';
639 0 $self->warn;
640 0 return;
641 }
642  
643 23 ▶ if ( !$self->{backend_obj}->{cidr_supported} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"backend_obj"}{"cidr_supported"}) 0 23 23
644 0 $self->{error} = 29;
645 0 $self->{errorString} = 'the "' . $self->{backend} . '" backend does not support CIDR bans';
646 0 $self->warn;
647 0 return;
648 }
649  
650 # lowercase so the same IPv6 CIDR in differing cases can't result in duplicate entries
651 23 $opts{ban} = lc( $opts{ban} );
652  
653 23 $self->_self_heal(%opts);
654  
655 23 eval { $self->{backend_obj}->unban_cidr( ban => $opts{ban} ); };
656 23 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 23 23
657 0 $self->{error} = 27;
658 0 $self->{errorString} = 'unbanning cidr item,"' . $opts{ban} . '", failed... ' . $@;
659 0 $self->warn;
660 0 return;
661 }
662 } ## end sub unban_cidr
663  
664 =head2 list
665  
666 List banned IPs. Returns an array of the currently banned single IPs as
667 reported by the backend. CIDR range bans are not included; for those see
668 L</list_cidr>.
669  
670 my @banned = $fw_helper->list;
671  
672 =cut
673  
674 21 ▶sub list {
Subroutine list: called CC=3 SCAR=11.7
Pod documented
675 21 my ( $self, %opts ) = @_;
676  
677 21 $self->errorblank;
678 21 $self->{test_data} = undef;
679  
680 21 ▶ if ( !defined( $self->{backend_obj} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"backend_obj"}) 1 20 21
681 1 $self->{error} = 15;
682 1 $self->{errorString} = 'No backend object present... init_backend has not been called';
683 1 $self->warn;
684 0 return;
685 }
686  
687 20 my @banned;
688 20 eval { @banned = $self->{backend_obj}->list; };
689 20 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 20 20
690 0 $self->{error} = 15;
691 0 $self->{errorString} = 'listing bans failed... ' . $@;
692 0 $self->warn;
693 0 return;
694 }
695  
696 20 return @banned;
697 } ## end sub list
698  
699 =head2 list_cidr
700  
701 List banned CIDR ranges. Backends that do not support CIDR bans always return
702 an empty list.
703  
704 my @banned_cidrs = $fw_helper->list_cidr;
705  
706 =cut
707  
708 36 ▶sub list_cidr {
Subroutine list_cidr: called CC=3 SCAR=13.8
Pod documented
709 36 my ( $self, %opts ) = @_;
710  
711 36 $self->errorblank;
712 36 $self->{test_data} = undef;
713  
714 36 ▶ if ( !defined( $self->{backend_obj} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"backend_obj"}) 0 36 36
715 0 $self->{error} = 30;
716 0 $self->{errorString} = 'No backend object present... init_backend has not been called';
717 0 $self->warn;
718 0 return;
719 }
720  
721 36 my @banned;
722 36 eval { @banned = $self->{backend_obj}->list_cidr; };
723 36 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 36 36
724 0 $self->{error} = 30;
725 0 $self->{errorString} = 'listing cidr bans failed... ' . $@;
726 0 $self->warn;
727 0 return;
728 }
729  
730 36 return @banned;
731 } ## end sub list_cidr
732  
733 =head2 re_init
734  
735 Tells the backend to re-init itself, rebuilding its firewall setup and
736 re-applying the current ban list. A backend failure is caught and re-raised
737 as reInitFailed.
738  
739 The backend does not need to be inited for this to work, so as well as
740 recovering a setup that was removed externally, this is what brings one back
741 after a L</teardown> or L</stop>. The ban list is held on the backend object
742 and survives both, so the bans are re-applied rather than lost.
743  
744 $fw_helper->re_init;
745  
746 # resuming after a stop
747 $fw_helper->stop;
748 $fw_helper->re_init;
749  
750 =cut
751  
752 8 ▶sub re_init {
Subroutine re_init: called CC=3 SCAR=11.9
Pod documented
753 8 my ( $self, %opts ) = @_;
754  
755 8 $self->errorblank;
756 8 $self->{test_data} = undef;
757  
758 8 ▶ if ( !defined( $self->{backend_obj} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"backend_obj"}) 1 7 8
759 1 $self->{error} = 16;
760 1 $self->{errorString} = 'No backend object present... init_backend has not been called';
761 1 $self->warn;
762 0 return;
763 }
764  
765 7 eval { $self->{backend_obj}->re_init; };
766 7 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 7 7
767 0 $self->{error} = 16;
768 0 $self->{errorString} = 'backend re_init failed... ' . $@;
769 0 $self->warn;
770 0 return;
771 }
772 } ## end sub re_init
773  
774 =head2 teardown
775  
776 Tells the backend to tear down its firewall setup, the equivalent of
777 fail2ban's C<actionstop>. The ban list is retained by the backend so a
778 following L</re_init> restores it. A backend failure is caught and re-raised
779 as teardownFailed.
780  
781 $fw_helper->teardown;
782  
783 =cut
784  
785 20 ▶sub teardown {
Subroutine teardown: called CC=3 SCAR=11.0
Pod documented
786 20 my ( $self, %opts ) = @_;
787  
788 20 $self->errorblank;
789 20 $self->{test_data} = undef;
790  
791 20 ▶ if ( !defined( $self->{backend_obj} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"backend_obj"}) 1 19 20
792 1 $self->{error} = 17;
793 1 $self->{errorString} = 'No backend object present... init_backend has not been called';
794 1 $self->warn;
795 0 return;
796 }
797  
798 19 eval { $self->{backend_obj}->teardown; };
799 19 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 1 18 19
800 1 $self->{error} = 17;
801 1 $self->{errorString} = 'backend teardown failed... ' . $@;
802 1 $self->warn;
803 0 return;
804 }
805 } ## end sub teardown
806  
807 =head2 stop
808  
809 Alias for L</teardown>, provided for parity with the fail2ban C<actionstop>
810 concept.
811  
812 $fw_helper->stop;
813  
814 =cut
815  
816 1 ▶sub stop {
Subroutine stop: called CC=1 SCAR=0.0
Pod documented
817 1 my ( $self, %opts ) = @_;
818  
819 1 return $self->teardown(%opts);
820 }
821  
822 =head2 check
823  
824 Asks the backend to verify that its firewall setup is still intact. This is
825 the equivalent of fail2ban's C<actioncheck>. Returns a true value if the
826 setup is present and a false value if it appears to have been removed (in
827 which case a L</re_init> is warranted). On an internal error it sets the
828 error and returns undef.
829  
830 if ( !$fw_helper->check ) {
831 $fw_helper->re_init;
832 }
833  
834 =cut
835  
836 17 ▶sub check {
Subroutine check: called CC=3 SCAR=11.7
Pod documented
837 17 my ( $self, %opts ) = @_;
838  
839 17 $self->errorblank;
840 17 $self->{test_data} = undef;
841  
842 17 ▶ if ( !defined( $self->{backend_obj} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"backend_obj"}) 1 16 17
843 1 $self->{error} = 24;
844 1 $self->{errorString} = 'No backend object present... init_backend has not been called';
845 1 $self->warn;
846 0 return;
847 }
848  
849 16 my $healthy;
850 16 eval { $healthy = $self->{backend_obj}->check; };
851 16 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 16 16
852 0 $self->{error} = 24;
853 0 $self->{errorString} = 'backend check failed... ' . $@;
854 0 $self->warn;
855 0 return;
856 }
857  
858 16 return $healthy;
859 } ## end sub check
860  
861 =head2 flush
862  
863 Removes all currently banned IPs at once while leaving the firewall rules in
864 place. This is the equivalent of fail2ban's C<actionflush>. Unlike
865 L</teardown>, the blocking rules remain active, so new bans work without a
866 L</re_init>.
867  
868 $fw_helper->flush;
869  
870 =cut
871  
872 13 ▶sub flush {
Subroutine flush: called CC=3 SCAR=11.0
Pod documented
873 13 my ( $self, %opts ) = @_;
874  
875 13 $self->errorblank;
876 13 $self->{test_data} = undef;
877  
878 13 ▶ if ( !defined( $self->{backend_obj} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"backend_obj"}) 1 12 13
879 1 $self->{error} = 25;
880 1 $self->{errorString} = 'No backend object present... init_backend has not been called';
881 1 $self->warn;
882 0 return;
883 }
884  
885 12 eval { $self->{backend_obj}->flush; };
886 12 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 1 11 12
887 1 $self->{error} = 25;
888 1 $self->{errorString} = 'backend flush failed... ' . $@;
889 1 $self->warn;
890 0 return;
891 }
892 } ## end sub flush
893  
894 =head2 commit
895  
896 Tells the backend to write out any state it has been holding back from
897 persistent storage.
898  
899 Most backends have nothing to hold back, as the firewall they drive is the
900 only copy of the state, and those do not implement this at all; calling it
901 for one of them sets the commitNotSupported error.
902 L<Net::Firewall::BlockerHelper::backends::openwrt> is currently the only
903 backend that does implement it, where bans are applied to the live nftables
904 sets straight away but only reach the UCI config, and so the router's flash,
905 when this is called.
906  
907 Where a backend does implement it, it is safe to call as often as wanted and
908 is a noop when there is nothing outstanding. How often it is worth calling is
909 a trade off between how much of the ban list can be lost and, for a backend
910 writing to flash, how much wear is acceptable.
911  
912 $fw_helper->ban(ban => '1.2.3.4');
913 $fw_helper->ban(ban => '5.6.7.8');
914 $fw_helper->commit;
915  
916 =cut
917  
918 0 ▶sub commit {
Subroutine commit: not called CC=4 SCAR=30.0
Pod documented
919 0 my ( $self, %opts ) = @_;
920  
921 0 $self->errorblank;
922 0 $self->{test_data} = undef;
923  
924 0 ▶ if ( !defined( $self->{backend_obj} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"backend_obj"}) 0 0 0
925 0 $self->{error} = 31;
926 0 $self->{errorString} = 'No backend object present... init_backend has not been called';
927 0 $self->warn;
928 0 return;
929 }
930  
931 0 ▶ if ( !$self->{backend_obj}->can('commit') ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"backend_obj"}->can("commit")) 0 0 0
932 0 $self->{error} = 32;
933 0 $self->{errorString} = 'the "' . $self->{backend} . '" backend does not implement commit';
934 0 $self->warn;
935 0 return;
936 }
937  
938 0 eval { $self->{backend_obj}->commit; };
939 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
940 0 $self->{error} = 31;
941 0 $self->{errorString} = 'backend commit failed... ' . $@;
942 0 $self->warn;
943 0 return;
944 }
945 } ## end sub commit
946  
947 =head1 ERROR CODES / FLAGS
948  
949 Error handling is provided by L<Error::Helper>. All
950 errors are considered fatal.
951  
952 =head2 1, noBackendSpecified
953  
954 No backend was specified to use.
955  
956 =head2 2, invalidPortSpecified
957  
958 Port is either not an int within the range 1 to 65535 or a name that can be resolved by getservbyname.
959  
960 =head2 3, portsNotArray
961  
962 The data passed to new for ports is not an array.
963  
964 =head2 4, protocolsNotArray
965  
966 The data passed to new for protocols is not an array.
967  
968 =head2 5, invalidPortSpecified
969  
970 Port is either not an int within the range 1 to 65535 or a name that can be resolved by getservbyname.
971  
972 =head2 6, invalidPrefixSpecified
973  
974 The specified prefix did not match /^[a-zA-Z0-9]+$/.
975  
976 =head2 7, invalidName
977  
978 The name is either undef or does not match /^[a-zA-Z0-9\-]+$/.
979  
980 =head2 8, optionsNotHash
981  
982 The item passed to new for options is not a hash.
983  
984 =head2 9, noBanItem
985  
986 No IP specified to ban.
987  
988 =head2 10, banItemNotIP
989  
990 The item to ban is not an IP. Either wrong ref type or regexp
991 test using L<Regexp::IPv4> and L<Regexp::IPv6> failed.
992  
993 =head2 11, invalidBackend
994  
995 The specified backend failed to pass a basic sanity check of making sure it
996 matches the regexp /^[a-zA-Z0-9\_]+$/.
997  
998 =head2 12, backendInitError
999  
1000 Failed to init the backend.
1001  
1002 =head2 13, banFailed
1003  
1004 Failed to ban the item.
1005  
1006 =head2 14, unbanFailed
1007  
1008 Failed to unban the item.
1009  
1010 =head2 15, listFailed
1011  
1012 Failed get a list of bans.
1013  
1014 =head2 16, reInitFailed
1015  
1016 Failed to re_init the backend.
1017  
1018 =head2 17, teardownFailed
1019  
1020 Failed to teardown the backend.
1021  
1022 =head2 24, checkFailed
1023  
1024 The backend check raised an error.
1025  
1026 =head2 25, flushFailed
1027  
1028 Failed to flush the bans.
1029  
1030 =head2 26, banCidrFailed
1031  
1032 Failed to ban the CIDR range.
1033  
1034 =head2 27, unbanCidrFailed
1035  
1036 Failed to unban the CIDR range.
1037  
1038 =head2 28, cidrItemNotCidr
1039  
1040 The item to ban is not a CIDR range. Either wrong ref type or it is not an
1041 IPv4 or IPv6 address followed by a prefix length valid for its family.
1042  
1043 =head2 29, cidrNotSupported
1044  
1045 The selected backend does not support CIDR bans.
1046  
1047 =head2 30, listCidrFailed
1048  
1049 Failed to get a list of CIDR bans.
1050  
1051 =head2 31, commitFailed
1052  
1053 Failed to commit the backend's outstanding state.
1054  
1055 =head2 32, commitNotSupported
1056  
1057 The selected backend does not implement commit, having no state that is held
1058 back from persistent storage.
1059  
1060 =head1 AUTHOR
1061  
1062 Zane C. Bowers-Hadley, C<< <vvelox at vvelox.ent> >>
1063  
1064 =head1 BUGS
1065  
1066 Please report any bugs or feature requests to C<bug-net-firewall-blockerhelper at rt.cpan.org>, or through
1067 the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Net-Firewall-BlockerHelper>. I will be notified, and then you'll
1068 automatically be notified of progress on your bug as I make changes.
1069  
1070  
1071  
1072  
1073 =head1 SUPPORT
1074  
1075 You can find documentation for this module with the perldoc command.
1076  
1077 perldoc Net::Firewall::BlockerHelper
1078  
1079  
1080 You can also look for information at:
1081  
1082 =over 4
1083  
1084 =item * RT: CPAN's request tracker (report bugs here)
1085  
1086 L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=Net-Firewall-BlockerHelper>
1087  
1088 =item * CPAN Ratings
1089  
1090 L<https://cpanratings.perl.org/d/Net-Firewall-BlockerHelper>
1091  
1092 =item * Search CPAN
1093  
1094 L<https://metacpan.org/release/Net-Firewall-BlockerHelper>
1095  
1096 =back
1097  
1098 =head1 LICENSE AND COPYRIGHT
1099  
1100 This software is Copyright (c) 2026 by Zane C. Bowers-Hadley.
1101  
1102 This is free software, licensed under:
1103  
1104 The GNU Lesser General Public License, Version 2.1, February 1999
1105  
1106 =cut
1107  
1108 52 1; # End of Net::Firewall::BlockerHelper
« BlockerHelper/backends/hosts_deny.pm ↑ Summary BlockerHelper/backends/cloud_armor.pm »