BlockerHelper/backends/shell.pm

filter: Statementstmt 65.6% 201 / 306 Branchbran 66.6% 80 / 120 Conditioncond 38.1% 8 / 21 MC/DCmcdc 14.2% 2 / 14 Subroutinesub 95.0% 19 / 20 Podpod 100% 13 / 13 totaltotal 65.3% 323 / 494 CC 71 SCAR 25.0
CC
71
Cov
65%
_valid_cidr
20.7
ban_cidr
20.5
unban_cidr
20.5
CRAP 12.2
« BlockerHelper.pm ↑ Summary
1 package Net::Firewall::BlockerHelper::backends::shell;
2  
3 4 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use base 'Error::Helper';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use Regexp::IPv4 qw($IPv4_re);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use Regexp::IPv6 qw($IPv6_re);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 =head1 NAME
11  
12 Net::Firewall::BlockerHelper::backends::shell - A shell backend for Net::Firewall::BlockerHelper.
13  
14 =head1 VERSION
15  
16 Version 0.1.0
17  
18 =cut
19  
20 4 our $VERSION = '0.1.0';
21  
22 =head1 SYNOPSIS
23  
24 This backend just runs the commands configured via the options hash, so
25 what init, ban, and the like actually do is entirely up to the commands
26 supplied. Wiring them up to something that really blocks traffic is the
27 user's job. The example below just creates and removes files under C</tmp>
28 to show the flow and blocks nothing.
29  
30 use Net::Firewall::BlockerHelper;
31  
32 my $fw_helper;
33 eval {
34 $fw_helper = Net::Firewall::BlockerHelper->new(
35 backend=>'shell',
36 name=>'derp',
37 options=>{
38 init=>'mkdir /tmp/fw_helper_example/',
39 teardown=>'rm -rf /tmp/fw_helper_example/',
40 unban=>'rm -rf /tmp/fw_helper_example/%%%BAN%%%',
41 ban=>'touch /tmp/fw_helper_example/%%%BAN%%%',
42 },
43 );
44 };
45 if ($@) {
46 print 'Error: '
47 . $Error::Helper::error
48 . "\nError String: "
49 . $Error::Helper::errorString
50 . "\nError Flag: "
51 . $Error::Helper::errorFlag . "\n";
52 }
53  
54 $fw_helper->init_backend;
55  
56 $fw_helper->ban(ban => '5.6.7.8');
57 $fw_helper->ban(ban => '1.2.3.4');
58  
59 $fw_helper->unban(ban => '5.6.7.8');
60  
61 $fw_helper->teardown;
62  
63 =head1 METHODS
64  
65 =head2 new
66  
67 Initiates the the object. Unlike other backends, this only takes uses
68 the options hash.
69  
70 - options :: A hash of options to pass to the backend.
71 Default :: {}
72  
73 - name :: Not used but is required by Net::Firewall::BlockerHelper.
74 Default :: undef
75  
76 The values used for options is as below. All must be defined and can't be ''.
77 '2>&1' is appended to the end of the commands.
78  
79 - init :: The command to run to init the blocking.
80 Default :: undef
81  
82 - teardown :: The command to run to teardown the blocking.
83 Default :: undef
84  
85 - ban :: The command to run to ban a IP. %%%BAN%%% is replaced with the IP.
86 Default :: undef
87  
88 - unban :: The command to run to un ban a IP. %%%BAN%%% is replaced with the IP.
89 Default :: undef
90  
91 - check :: Optional command to run to verify the blocking is still in place.
92 A zero exit is treated as healthy. If not defined, check always
93 reports healthy.
94 Default :: undef
95  
96 - flush :: Optional command to run to remove all bans at once. If not
97 defined, flush falls back to unbanning each currently banned IP.
98 Default :: undef
99  
100 All errors are considered fatal, meaning if new fails it will die.
101  
102 my $fw_helper;
103 eval {
104 $fw_helper = Net::Firewall::BlockerHelper->new(
105 backend=>'shell',
106 name=>'derp',
107 options=>{
108 init=>'mkdir /tmp/fw_helper_example/',
109 teardown=>'rm -rf /tmp/fw_helper_example/',
110 unban=>'rm -rf /tmp/fw_helper_example/%%%BAN%%%',
111 ban=>'touch /tmp/fw_helper_example/%%%BAN%%%',
112 },
113 );
114 };
115 if ($@) {
116 print 'Error: '
117 . $Error::Helper::error
118 . "\nError String: "
119 . $Error::Helper::errorString
120 . "\nError Flag: "
121 . $Error::Helper::errorFlag . "\n";
122 }
123  
124 =cut
125  
126 sub new {
127 19 ▶ my ( $blank, %opts ) = @_;
Subroutine new: called CC=14 SCAR=27.1
Pod documented
128  
129 19 my $self = {
130 perror => undef,
131 error => undef,
132 errorLine => undef,
133 errorFilename => undef,
134 errorString => "",
135 errorExtra => {
136 all_errors_fatal => 1,
137 # all_fatal is what Error::Helper 2.1.0 actually checks; all_errors_fatal
138 # is kept for the name documented in its POD
139 all_fatal => 1,
140 flags => {
141 1 => 'notInited',
142 2 => 'initInvalid',
143 3 => 'optionsUndef',
144 4 => 'teardownInvalid',
145 5 => 'banInvalid',
146 6 => 'unbanInvalid',
147 8 => 'optionsNotHash',
148 9 => 'noBanItem',
149 10 => 'banItemNotIP',
150 12 => 'backendInitError',
151 13 => 'banFailed',
152 14 => 'unbanFailed',
153 15 => 'listFailed',
154 16 => 'reInitFailed',
155 17 => 'teardownFailed',
156 18 => 'alreadyInited',
157 24 => 'checkFailed',
158 25 => 'flushFailed',
159 26 => 'banCidrFailed',
160 27 => 'unbanCidrFailed',
161 28 => 'cidrItemNotCidr',
162 29 => 'cidrNotSupported',
163 30 => 'listCidrFailed',
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 frontend_obj => undef,
176 inited => 0,
177 banned => {},
178 banned_cidr => {},
179 cidr_supported => 1,
180 };
181 19 bless $self;
182  
183 # used internally for testing
184 19 ▶ if ( defined( $opts{testing} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"testing"}) 10 9 19
185 10 $self->{testing} = $opts{testing};
186 }
187 19 ▶ if ( defined( $opts{frontend_obj} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"frontend_obj"}) 19 0 19
188 19 $self->{frontend_obj} = $opts{frontend_obj};
189 }
190  
191 19 ▶ if ( defined( $opts{options} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{'options'}) { } 19 0 19
192 19 ▶ if ( ref( $opts{options} ) ne 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $opts{"options"} ne "HASH") 0 19 19
193 0 $self->{perror} = 1;
194 0 $self->{error} = 8;
195 0 $self->{errorString} = 'ref for options is "' . ref( $opts{options} ) . '" and not HASH';
196 0 $self->warn;
197 }
198 19 $self->{options} = $opts{options};
199  
200 19 ▶ if ( !defined( $opts{options}{init} ) ) {
Branch: 8 branches
BranchTrueFalseTotal
if (not defined $opts{'options'}{'init'}) { } 2 17 19
elsif ($opts{'options'}{'init'} eq '') { } 1 16 17
elsif (not defined $opts{'options'}{'teardown'}) { } 1 15 16
elsif ($opts{'options'}{'teardown'} eq '') { } 1 14 15
elsif (not defined $opts{'options'}{'ban'}) { } 1 13 14
elsif ($opts{'options'}{'ban'} eq '') { } 1 12 13
elsif (not defined $opts{'options'}{'unban'}) { } 1 11 12
elsif ($opts{'options'}{'unban'} eq '') { } 1 10 11
201 2 $self->{perror} = 1;
202 2 $self->{error} = 2;
203 2 $self->{errorString} = 'init is not defined';
204 2 $self->warn;
205 } elsif ( $opts{options}{init} eq '' ) {
206 1 $self->{perror} = 1;
207 1 $self->{error} = 2;
208 1 $self->{errorString} = 'init is blank';
209 1 $self->warn;
210 } elsif ( !defined( $opts{options}{teardown} ) ) {
211 1 $self->{perror} = 1;
212 1 $self->{error} = 4;
213 1 $self->{errorString} = 'teardown is not defined';
214 1 $self->warn;
215 } elsif ( $opts{options}{teardown} eq '' ) {
216 1 $self->{perror} = 1;
217 1 $self->{error} = 4;
218 1 $self->{errorString} = 'teardown is blank';
219 1 $self->warn;
220 } elsif ( !defined( $opts{options}{ban} ) ) {
221 1 $self->{perror} = 1;
222 1 $self->{error} = 5;
223 1 $self->{errorString} = 'ban is not defined';
224 1 $self->warn;
225 } elsif ( $opts{options}{ban} eq '' ) {
226 1 $self->{perror} = 1;
227 1 $self->{error} = 5;
228 1 $self->{errorString} = 'ban is blank';
229 1 $self->warn;
230 } elsif ( !defined( $opts{options}{unban} ) ) {
231 1 $self->{perror} = 1;
232 1 $self->{error} = 6;
233 1 $self->{errorString} = 'unban is not defined';
234 1 $self->warn;
235 } elsif ( $opts{options}{unban} eq '' ) {
236 1 $self->{perror} = 1;
237 1 $self->{error} = 6;
238 1 $self->{errorString} = 'unban is blank';
239 1 $self->warn;
240 }
241 } else {
242 0 ▶ if ( ref( $opts{options} ) ne 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $opts{"options"} ne "HASH") 0 0 0
243 0 $self->{perror} = 1;
244 0 $self->{error} = 3;
245 0 $self->{errorString} = 'options is undef and not a hash';
246 0 $self->warn;
247 }
248 }
249  
250 10 return $self;
251 } ## end sub new
252  
253 =head2 init
254  
255 Initiates the backend by running the configured C<init> command with
256 '2>&1' appended. A non-zero exit is an error.
257  
258 Note that what this actually sets up is entirely down to the configured
259 command. See L</SYNOPSIS>.
260  
261 No arguments are taken.
262  
263 If called a second time, it will error.
264  
265 $backend->init;
266  
267 =cut
268  
269 sub init {
270 13 ▶ my ( $self, %opts ) = @_;
Subroutine init: called CC=4 SCAR=14.1
Pod documented
271  
272 13 $self->errorblank;
273  
274 13 ▶ if ( $self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"inited"}) 0 13 13
275 0 $self->{error} = 18;
276 0 $self->{errorString} = 'backend has already been inited';
277 0 $self->warn;
278 }
279  
280 13 my $command = $self->{options}{init};
281  
282 13 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 2 11 13
283 2 $self->{frontend_obj}->{test_data} = $command;
284 } else {
285 11 my $output = `$command 2>&1`;
286 11 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 1 10 11
287 1 $self->{error} = 12;
288 1 $self->{errorString} = 'Init failed... command "' . $command . '" resulted in... ' . $output;
289 1 $self->warn;
290 }
291 }
292  
293 12 $self->{inited} = 1;
294 } ## end sub init
295  
296 =head2 ban
297  
298 Bans an IP. The value of ban is validated as being a IPv4 or IPv6 address
299 and lowercased, then the configured C<ban> command is run with %%%BAN%%%
300 replaced by the IP. A non-zero exit is an error.
301  
302 $fw_helper->ban(ban => $ip);
303  
304 =cut
305  
306 sub ban {
307 14 ▶ my ( $self, %opts ) = @_;
Subroutine ban: called CC=8 SCAR=27.1
Pod documented
308  
309 14 $self->errorblank;
310  
311 14 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 14 14
312 0 $self->{error} = 1;
313 0 $self->{errorString} = 'backend has not been inited';
314 0 $self->warn;
315 0 return;
316 }
317  
318 14 ▶ if ( !defined( $opts{ban} ) ) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $opts{'ban'}) { } 0 14 14
elsif (ref $opts{'ban'} ne '') { } 0 14 14
elsif (not $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::shell::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::shell::IPv6_re\z/) { } 0 14 14
319 0 $self->{error} = 9;
320 0 $self->{errorString} = 'Nothing specified for the value ban';
321 0 $self->warn;
322 0 return;
323 } elsif ( ref( $opts{ban} ) ne '' ) {
324 0 $self->{error} = 10;
325 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
326 0 $self->warn;
327 0 return;
328 ▶ } elsif ( $opts{ban} !~ /\A$IPv4_re\z/
Condition33%1/3
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::shell::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::shell::IPv6_re\z/
l!l&&r!l&&!r
1400
MC/DC0%0/2
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::shell::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::shell::IPv6_re\z/
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::shell::IPv4_re\z/ $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::shell::IPv6_re\z/
Truth table33%1/3
A || B
A= $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::shell::IPv4_re\z/B= $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::shell::IPv6_re\z/
ABresult
1X1
011
000
329 && $opts{ban} !~ /\A$IPv6_re\z/ )
330 {
331 0 $self->{error} = 10;
332 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 IP';
333 0 $self->warn;
334 0 return;
335 }
336  
337 # lowercase so the same IPv6 IP in differing cases can't result in duplicate entries
338 14 $opts{ban} = lc( $opts{ban} );
339  
340 14 my $command = $self->{options}{ban};
341 14 $command =~ s/\%\%\%BAN\%\%\%/$opts{ban}/g;
342  
343 14 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 2 12 14
344 2 $self->{frontend_obj}->{test_data} = $command;
345 } else {
346 12 my $output = `$command 2>&1`;
347 12 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 2 10 12
348 2 $self->{error} = 13;
349 2 $self->{errorString} = 'Ban failed... command "' . $command . '" resulted in... ' . $output;
350 2 $self->warn;
351 }
352 }
353  
354 12 $self->{banned}{ $opts{ban} } = 1;
355 } ## end sub ban
356  
357 =head2 unban
358  
359 Unbans an IP. The value of ban is validated as being a IPv4 or IPv6 address
360 and lowercased, then the configured C<unban> command is run with %%%BAN%%%
361 replaced by the IP. A non-zero exit is an error.
362  
363 $fw_helper->unban(ban => $ip);
364  
365 =cut
366  
367 sub unban {
368 5 ▶ my ( $self, %opts ) = @_;
Subroutine unban: called CC=8 SCAR=27.1
Pod documented
369  
370 5 $self->errorblank;
371  
372 5 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 5 5
373 0 $self->{error} = 1;
374 0 $self->{errorString} = 'backend has not been inited';
375 0 $self->warn;
376 0 return;
377 }
378  
379 5 ▶ if ( !defined( $opts{ban} ) ) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $opts{'ban'}) { } 0 5 5
elsif (ref $opts{'ban'} ne '') { } 0 5 5
elsif (not $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::shell::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::shell::IPv6_re\z/) { } 0 5 5
380 0 $self->{error} = 9;
381 0 $self->{errorString} = 'Nothing specified for the value ban';
382 0 $self->warn;
383 0 return;
384 } elsif ( ref( $opts{ban} ) ne '' ) {
385 0 $self->{error} = 10;
386 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
387 0 $self->warn;
388 0 return;
389 ▶ } elsif ( $opts{ban} !~ /\A$IPv4_re\z/
Condition33%1/3
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::shell::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::shell::IPv6_re\z/
l!l&&r!l&&!r
500
MC/DC0%0/2
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::shell::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::shell::IPv6_re\z/
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::shell::IPv4_re\z/ $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::shell::IPv6_re\z/
Truth table33%1/3
A || B
A= $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::shell::IPv4_re\z/B= $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::shell::IPv6_re\z/
ABresult
1X1
011
000
390 && $opts{ban} !~ /\A$IPv6_re\z/ )
391 {
392 0 $self->{error} = 10;
393 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 IP';
394 0 $self->warn;
395 0 return;
396 }
397  
398 # lowercase so the same IPv6 IP in differing cases can't result in duplicate entries
399 5 $opts{ban} = lc( $opts{ban} );
400  
401 5 my $command = $self->{options}{unban};
402 5 $command =~ s/\%\%\%BAN\%\%\%/$opts{ban}/g;
403  
404 5 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 1 4 5
405 1 $self->{frontend_obj}->{test_data} = $command;
406 } else {
407 4 my $output = `$command 2>&1`;
408 4 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 1 3 4
409 1 $self->{error} = 14;
410 1 $self->{errorString} = 'Unban failed... command "' . $command . '" resulted in... ' . $output;
411 1 $self->warn;
412 }
413 }
414  
415 4 delete( $self->{banned}{ $opts{ban} } );
416 } ## end sub unban
417  
418 # Internal helper. Returns a true value if the passed scalar is a valid IPv4 or
419 # IPv6 CIDR range, that is an address followed by "/" and a prefix length that
420 # is within the range valid for its family (0 to 32 for IPv4, 0 to 128 for
421 # IPv6). Returns false otherwise.
422 sub _valid_cidr {
423 2 ▶ my ( $self, $cidr ) = @_;
Subroutine _valid_cidr: called CC=8 SCAR=30.3
424  
425 2 ▶ return 0 if ( !defined($cidr) || ref($cidr) ne '' );
Branch: 1 branch
BranchTrueFalseTotal
if not defined $cidr or ref $cidr ne "" 0 2 2
Condition33%1/3
not defined $cidr or ref $cidr ne ""
l!l&&r!l&&!r
002
MC/DC0%0/2
not defined $cidr or ref $cidr ne ""
not defined $cidr ref $cidr ne ""
Truth table33%1/3
not(A) or B
A= not defined $cidrB= ref $cidr ne ""
ABresult
1X1
011
000
426  
427 2 ▶ if ( $cidr =~ m!\A(.+)/([0-9]{1,3})\z! ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cidr =~ m[\A(.+)/([0-9]{1,3})\z]) 2 0 2
428 2 my ( $addr, $prefix ) = ( $1, $2 );
429 2 ▶ return 1 if ( $addr =~ /\A$IPv4_re\z/ && $prefix <= 32 );
Branch: 1 branch
BranchTrueFalseTotal
if $addr =~ /\A$Net::Firewall::BlockerHelper::backends::shell::IPv4_re\z/ and $prefix <= 32 2 0 2
Condition33%1/3
$addr =~ /\A$Net::Firewall::BlockerHelper::backends::shell::IPv4_re\z/ and $prefix <= 32
!ll&&!rl&&r
002
MC/DC0%0/2
$addr =~ /\A$Net::Firewall::BlockerHelper::backends::shell::IPv4_re\z/ and $prefix <= 32
$addr =~ /\A$Net::Firewall::BlockerHelper::backends::shell::IPv4_re\z/ $prefix <= 32
Truth table33%1/3
A and B
A= $addr =~ /\A$Net::Firewall::BlockerHelper::backends::shell::IPv4_re\z/B= $prefix <= 32
ABresult
0X0
100
111
430 0 ▶ return 1 if ( $addr =~ /\A$IPv6_re\z/ && $prefix <= 128 );
Branch: 1 branch
BranchTrueFalseTotal
if $addr =~ /\A$Net::Firewall::BlockerHelper::backends::shell::IPv6_re\z/ and $prefix <= 128 0 0 0
Condition0%0/3
$addr =~ /\A$Net::Firewall::BlockerHelper::backends::shell::IPv6_re\z/ and $prefix <= 128
!ll&&!rl&&r
000
MC/DC0%0/2
$addr =~ /\A$Net::Firewall::BlockerHelper::backends::shell::IPv6_re\z/ and $prefix <= 128
$addr =~ /\A$Net::Firewall::BlockerHelper::backends::shell::IPv6_re\z/ $prefix <= 128
Truth table0%0/3
A and B
A= $addr =~ /\A$Net::Firewall::BlockerHelper::backends::shell::IPv6_re\z/B= $prefix <= 128
ABresult
0X0
100
111
431 }
432  
433 0 return 0;
434 } ## end sub _valid_cidr
435  
436 =head2 ban_cidr
437  
438 Bans a CIDR range. The value of ban is validated as being a IPv4 or IPv6
439 CIDR range and lowercased, then the configured C<ban> command is run with
440 %%%BAN%%% replaced by the CIDR.
441  
442 $fw_helper->ban_cidr(ban => '1.2.3.0/24');
443  
444 =cut
445  
446 sub ban_cidr {
447 1 ▶ my ( $self, %opts ) = @_;
Subroutine ban_cidr: called CC=7 SCAR=30.2
Pod documented
448  
449 1 $self->errorblank;
450  
451 1 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 1 1
452 0 $self->{error} = 1;
453 0 $self->{errorString} = 'backend has not been inited';
454 0 $self->warn;
455 0 return;
456 }
457  
458 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 $self->_valid_cidr($opts{'ban'})) { } 0 1 1
459 0 $self->{error} = 9;
460 0 $self->{errorString} = 'Nothing specified for the value ban';
461 0 $self->warn;
462 0 return;
463 } elsif ( ref( $opts{ban} ) ne '' ) {
464 0 $self->{error} = 28;
465 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
466 0 $self->warn;
467 0 return;
468 } elsif ( !$self->_valid_cidr( $opts{ban} ) ) {
469 0 $self->{error} = 28;
470 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 CIDR';
471 0 $self->warn;
472 0 return;
473 }
474  
475 # lowercase so the same IPv6 CIDR in differing cases can't result in duplicate entries
476 1 $opts{ban} = lc( $opts{ban} );
477  
478 1 my $command = $self->{options}{ban};
479 1 $command =~ s/\%\%\%BAN\%\%\%/$opts{ban}/g;
480  
481 1 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 1 0 1
482 1 $self->{frontend_obj}->{test_data} = $command;
483 } else {
484 0 my $output = `$command 2>&1`;
485 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
486 0 $self->{error} = 26;
487 0 $self->{errorString} = 'Ban failed... command "' . $command . '" resulted in... ' . $output;
488 0 $self->warn;
489 }
490 }
491  
492 1 $self->{banned_cidr}{ $opts{ban} } = 1;
493 } ## end sub ban_cidr
494  
495 =head2 unban_cidr
496  
497 Unbans a CIDR range. The value of ban is validated as being a IPv4 or IPv6
498 CIDR range and lowercased, then the configured C<unban> command is run with
499 %%%BAN%%% replaced by the CIDR.
500  
501 $fw_helper->unban_cidr(ban => '1.2.3.0/24');
502  
503 =cut
504  
505 sub unban_cidr {
506 1 ▶ my ( $self, %opts ) = @_;
Subroutine unban_cidr: called CC=7 SCAR=30.2
Pod documented
507  
508 1 $self->errorblank;
509  
510 1 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 1 1
511 0 $self->{error} = 1;
512 0 $self->{errorString} = 'backend has not been inited';
513 0 $self->warn;
514 0 return;
515 }
516  
517 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 $self->_valid_cidr($opts{'ban'})) { } 0 1 1
518 0 $self->{error} = 9;
519 0 $self->{errorString} = 'Nothing specified for the value ban';
520 0 $self->warn;
521 0 return;
522 } elsif ( ref( $opts{ban} ) ne '' ) {
523 0 $self->{error} = 28;
524 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
525 0 $self->warn;
526 0 return;
527 } elsif ( !$self->_valid_cidr( $opts{ban} ) ) {
528 0 $self->{error} = 28;
529 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 CIDR';
530 0 $self->warn;
531 0 return;
532 }
533  
534 # lowercase so the same IPv6 CIDR in differing cases can't result in duplicate entries
535 1 $opts{ban} = lc( $opts{ban} );
536  
537 1 my $command = $self->{options}{unban};
538 1 $command =~ s/\%\%\%BAN\%\%\%/$opts{ban}/g;
539  
540 1 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 1 0 1
541 1 $self->{frontend_obj}->{test_data} = $command;
542 } else {
543 0 my $output = `$command 2>&1`;
544 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
545 0 $self->{error} = 27;
546 0 $self->{errorString} = 'Unban failed... command "' . $command . '" resulted in... ' . $output;
547 0 $self->warn;
548 }
549 }
550  
551 1 delete( $self->{banned_cidr}{ $opts{ban} } );
552 } ## end sub unban_cidr
553  
554 =head2 list_cidr
555  
556 List banned CIDR ranges. Returns an array of the currently banned CIDR
557 ranges. Single IPs are not included; for those see L</list>.
558  
559 my @banned_cidrs = $fw_helper->list_cidr;
560  
561 =cut
562  
563 sub list_cidr {
564 2 ▶ my ( $self, %opts ) = @_;
Subroutine list_cidr: called CC=2 SCAR=7.0
Pod documented
565  
566 2 $self->errorblank;
567  
568 2 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 2 0 2
569 2 $self->{frontend_obj}->{test_data} = 'list_cidr';
570 }
571  
572 2 return keys( %{ $self->{banned_cidr} } );
573 }
574  
575 =head2 list
576  
577 List banned IPs. Returns an array of the currently banned single IPs. CIDR
578 ranges are not included; for those see L</list_cidr>.
579  
580 my @banned = $fw_helper->list;
581  
582 =cut
583  
584 sub list {
585 4 ▶ my ( $self, %opts ) = @_;
Subroutine list: called CC=2 SCAR=6.9
Pod documented
586  
587 4 $self->errorblank;
588  
589 4 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 2 2 4
590 2 $self->{frontend_obj}->{test_data} = 'list';
591 }
592  
593 4 return keys( %{ $self->{banned} } );
594 }
595  
596 =head2 re_init
597  
598 Tells the backend to re-init it's self.
599  
600 This will call teardown and init again, re-running the configured
601 C<teardown> and C<init> commands. teardown is best effort. After that the
602 configured C<ban> command is re-run for every previously banned IP and CIDR
603 range.
604  
605 $fw_helper->re_init;
606  
607 =cut
608  
609 sub re_init {
610 3 ▶ my ( $self, %opts ) = @_;
Subroutine re_init: called CC=6 SCAR=18.9
Pod documented
611  
612 3 $self->errorblank;
613  
614 3 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 3 3
615 0 $self->{error} = 1;
616 0 $self->{errorString} = 'backend has not been inited';
617 0 $self->warn;
618 0 return;
619 }
620  
621 # teardown is best effort here as a partially or fully wiped setup is
622 # exactly what re_init needs to recover from; init cleans up any remnants
623 {
624 3 local $@;
625 3 eval { $self->teardown; };
626 }
627 3 $self->init;
628  
629 # both single IPs and CIDR ranges are re-added the same way
630 3 my @to_re_ban = ( keys( %{ $self->{banned} } ), keys( %{ $self->{banned_cidr} } ) );
631  
632 3 foreach my $item (@to_re_ban) {
633 3 my $command = $self->{options}{ban};
634 3 $command =~ s/\%\%\%BAN\%\%\%/$item/g;
635  
636 3 ▶ if ( !$self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"testing"}) 2 1 3
637 2 my $output = `$command 2>&1`;
638 2 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 2 2
639 0 $self->{error} = 13;
640 0 $self->{errorString} = 'Ban failed... command "' . $command . '" resulted in... ' . $output;
641 0 $self->warn;
642 }
643 }
644 } ## end foreach my $items (@to_re_ban)
645  
646 3 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 1 2 3
647 1 $self->{frontend_obj}->{test_data} = 're-inited';
648 }
649  
650 3 $self->{inited} = 1;
651 } ## end sub re_init
652  
653 =head2 teardown
654  
655 Tears down the setup for the backend by running the configured C<teardown>
656 command. A non-zero exit is an error.
657  
658 $fw_helper->teardown;
659  
660 =cut
661  
662 sub teardown {
663 11 ▶ my ( $self, %opts ) = @_;
Subroutine teardown: called CC=3 SCAR=11.0
Pod documented
664  
665 11 $self->errorblank;
666  
667 11 $self->{inited} = 0;
668  
669 11 my $command = $self->{options}{teardown};
670  
671 11 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 2 9 11
672 2 $self->{frontend_obj}->{test_data} = $command;
673 } else {
674 9 my $output = `$command 2>&1`;
675 9 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 2 7 9
676 2 $self->{error} = 17;
677 2 $self->{errorString} = 'Teardown failed... command "' . $command . '" resulted in... ' . $output;
678 2 $self->warn;
679 }
680 }
681 } ## end sub teardown
682  
683 =head2 stop
684  
685 Alias for L</teardown>, provided for parity with the fail2ban C<actionstop>
686 concept.
687  
688 $fw_helper->stop;
689  
690 =cut
691  
692 sub stop {
693 0 ▶ my ( $self, %opts ) = @_;
Subroutine stop: not called CC=1 SCAR=6.9
Pod documented
694  
695 0 return $self->teardown(%opts);
696 }
697  
698 =head2 check
699  
700 Runs the optional C<check> command from the options hash. A zero exit code
701 is treated as healthy. If no C<check> command was configured, this always
702 reports healthy. This is the equivalent of fail2ban's C<actioncheck>.
703  
704 if ( !$fw_helper->check ) {
705 $fw_helper->re_init;
706 }
707  
708 =cut
709  
710 sub check {
711 21 ▶ my ( $self, %opts ) = @_;
Subroutine check: called CC=6 SCAR=18.5
Pod documented
712  
713 21 $self->errorblank;
714  
715 # no check command configured -> assume healthy
716 21 ▶ if ( !defined( $self->{options}{check} ) || $self->{options}{check} eq '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->{"options"}{"check"} or $self->{"options"}{"check"} eq "") 5 16 21
Condition66%2/3
not defined $self->{"options"}{"check"} or $self->{"options"}{"check"} eq ""
l!l&&r!l&&!r
5016
MC/DC50%1/2
not defined $self->{"options"}{"check"} or $self->{"options"}{"check"} eq ""
not defined $self->{"options"}{"check"} $self->{"options"}{"check"} eq ""
Truth table66%2/3
not(A) or B
A= not defined $self->{"options"}{"check"}B= $self->{"options"}{"check"} eq ""
ABresult
1X1
011
000
717 5 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 5 0 5
718 5 $self->{frontend_obj}->{test_data} = 'check';
719 }
720 5 return 1;
721 }
722  
723 16 my $command = $self->{options}{check};
724  
725 16 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 16 16
726 0 $self->{frontend_obj}->{test_data} = $command;
727 0 return 1;
728 }
729  
730 16 my $output = `$command 2>&1`;
731 16 ▶ return $? == 0 ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$? == 0 ? : 12 4 16
732 } ## end sub check
733  
734 =head2 flush
735  
736 Removes all currently banned IPs at once. If a C<flush> command was
737 configured in the options hash it is run; otherwise it falls back to
738 unbanning each currently banned IP. This is the equivalent of fail2ban's
739 C<actionflush>.
740  
741 $fw_helper->flush;
742  
743 =cut
744  
745 sub flush {
746 3 ▶ my ( $self, %opts ) = @_;
Subroutine flush: called CC=8 SCAR=22.1
Pod documented
747  
748 3 $self->errorblank;
749  
750 3 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 3 3
751 0 $self->{error} = 1;
752 0 $self->{errorString} = 'backend has not been inited';
753 0 $self->warn;
754 0 return;
755 }
756  
757 3 ▶ if ( defined( $self->{options}{flush} ) && $self->{options}{flush} ne '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'options'}{'flush'} and $self->{'options'}{'flush'} ne '') { } 2 1 3
Condition66%2/3
defined $self->{"options"}{"flush"} and $self->{"options"}{"flush"} ne ""
!ll&&!rl&&r
102
MC/DC50%1/2
defined $self->{"options"}{"flush"} and $self->{"options"}{"flush"} ne ""
defined $self->{"options"}{"flush"} $self->{"options"}{"flush"} ne ""
Truth table66%2/3
A and B
A= defined $self->{"options"}{"flush"}B= $self->{"options"}{"flush"} ne ""
ABresult
0X0
100
111
758 2 my $command = $self->{options}{flush};
759  
760 2 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 2 2
761 0 $self->{frontend_obj}->{test_data} = $command;
762 } else {
763 2 my $output = `$command 2>&1`;
764 2 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 1 1 2
765 1 $self->{error} = 25;
766 1 $self->{errorString} = 'Flush failed... command "' . $command . '" resulted in... ' . $output;
767 1 $self->warn;
768 }
769 }
770  
771 1 $self->{banned} = {};
772 1 $self->{banned_cidr} = {};
773 } else {
774 # no bulk flush command configured -> unban each currently banned IP
775 1 foreach my $item ( keys( %{ $self->{banned} } ) ) {
776 2 $self->unban( ban => $item );
777 }
778 1 foreach my $item ( keys( %{ $self->{banned_cidr} } ) ) {
779 0 $self->unban_cidr( ban => $item );
780 }
781 }
782 } ## end sub flush
783  
784 =head1 ERROR CODES / FLAGS
785  
786 Error handling is provided by L<Error::Helper>. All
787 errors are considered fatal.
788  
789 =head2 1, notInited
790  
791 The backend has not been inited yet.
792  
793 =head2 2, initInvalid
794  
795 The option init is undef or blank.
796  
797 =head2 3, optionsUndef
798  
799 The options hash passed to new is undef.
800  
801 =head2 4, teardownInvalid
802  
803 The option teardown is undef or blank.
804  
805 =head2 5, banInvalid
806  
807 The option ban is undef or blank.
808  
809 =head2 6, unbanInvalid
810  
811 The option unban is undef or blank.
812  
813 =head2 8, optionsNotHash
814  
815 The item passed to new for options is not a hash.
816  
817 =head2 9, noBanItem
818  
819 No IP or CIDR range specified to ban or unban.
820  
821 =head2 10, banItemNotIP
822  
823 The item to ban is not an IP. Either wrong ref type or regexp
824 test using L<Regexp::IPv4> and L<Regexp::IPv6> failed.
825  
826 =head2 12, backendInitError
827  
828 Failed to init the backend.
829  
830 =head2 13, banFailed
831  
832 Failed to ban the item.
833  
834 =head2 14, unbanFailed
835  
836 Failed to unban the item.
837  
838 =head2 15, listFailed
839  
840 Failed to get a list of bans.
841  
842 =head2 16, reInitFailed
843  
844 Failed to re_init the backend.
845  
846 =head2 17, teardownFailed
847  
848 Failed to teardown the backend.
849  
850 =head2 18, alreadyInited
851  
852 init called, but the backend has already been inited.
853  
854 =head2 24, checkFailed
855  
856 The backend check raised an error.
857  
858 =head2 25, flushFailed
859  
860 Failed to flush the bans.
861  
862 =head2 26, banCidrFailed
863  
864 Failed to ban the CIDR range.
865  
866 =head2 27, unbanCidrFailed
867  
868 Failed to unban the CIDR range.
869  
870 =head2 28, cidrItemNotCidr
871  
872 The item to ban is not a CIDR range. Either wrong ref type or it is not an
873 IPv4 or IPv6 address followed by a prefix length valid for its family.
874  
875 =head2 29, cidrNotSupported
876  
877 The backend does not support CIDR bans.
878  
879 =head2 30, listCidrFailed
880  
881 Failed to get a list of CIDR bans.
882  
883 =head1 AUTHOR
884  
885 Zane C. Bowers-Hadley, C<< <vvelox at vvelox.ent> >>
886  
887 =head1 BUGS
888  
889 Please report any bugs or feature requests to C<bug-net-firewall-blockerhelper at rt.cpan.org>, or through
890 the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Net-Firewall-BlockerHelper>. I will be notified, and then you'll
891 automatically be notified of progress on your bug as I make changes.
892  
893  
894  
895  
896 =head1 SUPPORT
897  
898 You can find documentation for this module with the perldoc command.
899  
900 perldoc Net::Firewall::BlockerHelper
901  
902  
903 You can also look for information at:
904  
905 =over 4
906  
907 =item * RT: CPAN's request tracker (report bugs here)
908  
909 L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=Net-Firewall-BlockerHelper>
910  
911 =item * Search CPAN
912  
913 L<https://metacpan.org/release/Net-Firewall-BlockerHelper>
914  
915 =back
916  
917  
918 =head1 ACKNOWLEDGEMENTS
919  
920  
921 =head1 LICENSE AND COPYRIGHT
922  
923 This software is Copyright (c) 2023 by Zane C. Bowers-Hadley.
924  
925 This is free software, licensed under:
926  
927 The GNU Lesser General Public License, Version 2.1, February 1999
928  
929  
930 =cut
931  
932 4 1; # End of Net::Firewall::BlockerHelper
« BlockerHelper.pm ↑ Summary