BlockerHelper/backends/shell.pm

filter: Statementstmt 66.3% 195 / 294 Branchbran 69.0% 76 / 110 Conditioncond 50.0% 6 / 12 MC/DCmcdc 25.0% 2 / 8 Subroutinesub 94.7% 18 / 19 Podpod 100% 13 / 13 totaltotal 67.9% 310 / 456 CC 63 SCAR 24.3
CC
63
Cov
67%
ban_cidr
20.5
unban_cidr
20.5
new
15.1
CRAP 11.3
« BlockerHelper/backends/file_reload.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 qw( Error::Helper Net::Firewall::BlockerHelper::Util );
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.2.0
17  
18 =cut
19  
20 4 our $VERSION = '0.2.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 19 ▶sub new {
Subroutine new: called CC=14 SCAR=27.1
Pod documented
127 19 my ( $blank, %opts ) = @_;
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 13 ▶sub init {
Subroutine init: called CC=4 SCAR=14.1
Pod documented
270 13 my ( $self, %opts ) = @_;
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 14 ▶sub ban {
Subroutine ban: called CC=8 SCAR=27.1
Pod documented
307 14 my ( $self, %opts ) = @_;
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 5 ▶sub unban {
Subroutine unban: called CC=8 SCAR=27.1
Pod documented
368 5 my ( $self, %opts ) = @_;
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 =head2 ban_cidr
419  
420 Bans a CIDR range. The value of ban is validated as being a IPv4 or IPv6
421 CIDR range and lowercased, then the configured C<ban> command is run with
422 %%%BAN%%% replaced by the CIDR.
423  
424 $fw_helper->ban_cidr(ban => '1.2.3.0/24');
425  
426 =cut
427  
428 1 ▶sub ban_cidr {
Subroutine ban_cidr: called CC=7 SCAR=30.2
Pod documented
429 1 my ( $self, %opts ) = @_;
430  
431 1 $self->errorblank;
432  
433 1 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 1 1
434 0 $self->{error} = 1;
435 0 $self->{errorString} = 'backend has not been inited';
436 0 $self->warn;
437 0 return;
438 }
439  
440 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
441 0 $self->{error} = 9;
442 0 $self->{errorString} = 'Nothing specified for the value ban';
443 0 $self->warn;
444 0 return;
445 } elsif ( ref( $opts{ban} ) ne '' ) {
446 0 $self->{error} = 28;
447 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
448 0 $self->warn;
449 0 return;
450 } elsif ( !$self->_valid_cidr( $opts{ban} ) ) {
451 0 $self->{error} = 28;
452 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 CIDR';
453 0 $self->warn;
454 0 return;
455 }
456  
457 # lowercase so the same IPv6 CIDR in differing cases can't result in duplicate entries
458 1 $opts{ban} = lc( $opts{ban} );
459  
460 1 my $command = $self->{options}{ban};
461 1 $command =~ s/\%\%\%BAN\%\%\%/$opts{ban}/g;
462  
463 1 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 1 0 1
464 1 $self->{frontend_obj}->{test_data} = $command;
465 } else {
466 0 my $output = `$command 2>&1`;
467 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
468 0 $self->{error} = 26;
469 0 $self->{errorString} = 'Ban failed... command "' . $command . '" resulted in... ' . $output;
470 0 $self->warn;
471 }
472 }
473  
474 1 $self->{banned_cidr}{ $opts{ban} } = 1;
475 } ## end sub ban_cidr
476  
477 =head2 unban_cidr
478  
479 Unbans a CIDR range. The value of ban is validated as being a IPv4 or IPv6
480 CIDR range and lowercased, then the configured C<unban> command is run with
481 %%%BAN%%% replaced by the CIDR.
482  
483 $fw_helper->unban_cidr(ban => '1.2.3.0/24');
484  
485 =cut
486  
487 1 ▶sub unban_cidr {
Subroutine unban_cidr: called CC=7 SCAR=30.2
Pod documented
488 1 my ( $self, %opts ) = @_;
489  
490 1 $self->errorblank;
491  
492 1 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 1 1
493 0 $self->{error} = 1;
494 0 $self->{errorString} = 'backend has not been inited';
495 0 $self->warn;
496 0 return;
497 }
498  
499 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
500 0 $self->{error} = 9;
501 0 $self->{errorString} = 'Nothing specified for the value ban';
502 0 $self->warn;
503 0 return;
504 } elsif ( ref( $opts{ban} ) ne '' ) {
505 0 $self->{error} = 28;
506 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
507 0 $self->warn;
508 0 return;
509 } elsif ( !$self->_valid_cidr( $opts{ban} ) ) {
510 0 $self->{error} = 28;
511 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 CIDR';
512 0 $self->warn;
513 0 return;
514 }
515  
516 # lowercase so the same IPv6 CIDR in differing cases can't result in duplicate entries
517 1 $opts{ban} = lc( $opts{ban} );
518  
519 1 my $command = $self->{options}{unban};
520 1 $command =~ s/\%\%\%BAN\%\%\%/$opts{ban}/g;
521  
522 1 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 1 0 1
523 1 $self->{frontend_obj}->{test_data} = $command;
524 } else {
525 0 my $output = `$command 2>&1`;
526 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
527 0 $self->{error} = 27;
528 0 $self->{errorString} = 'Unban failed... command "' . $command . '" resulted in... ' . $output;
529 0 $self->warn;
530 }
531 }
532  
533 1 delete( $self->{banned_cidr}{ $opts{ban} } );
534 } ## end sub unban_cidr
535  
536 =head2 list_cidr
537  
538 List banned CIDR ranges. Returns an array of the currently banned CIDR
539 ranges. Single IPs are not included; for those see L</list>.
540  
541 my @banned_cidrs = $fw_helper->list_cidr;
542  
543 =cut
544  
545 2 ▶sub list_cidr {
Subroutine list_cidr: called CC=2 SCAR=7.0
Pod documented
546 2 my ( $self, %opts ) = @_;
547  
548 2 $self->errorblank;
549  
550 2 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 2 0 2
551 2 $self->{frontend_obj}->{test_data} = 'list_cidr';
552 }
553  
554 2 return keys( %{ $self->{banned_cidr} } );
555 }
556  
557 =head2 list
558  
559 List banned IPs. Returns an array of the currently banned single IPs. CIDR
560 ranges are not included; for those see L</list_cidr>.
561  
562 my @banned = $fw_helper->list;
563  
564 =cut
565  
566 4 ▶sub list {
Subroutine list: called CC=2 SCAR=6.9
Pod documented
567 4 my ( $self, %opts ) = @_;
568  
569 4 $self->errorblank;
570  
571 4 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 2 2 4
572 2 $self->{frontend_obj}->{test_data} = 'list';
573 }
574  
575 4 return keys( %{ $self->{banned} } );
576 }
577  
578 =head2 re_init
579  
580 Tells the backend to re-init it's self.
581  
582 This will call teardown and init again, re-running the configured
583 C<teardown> and C<init> commands. teardown is best effort. After that the
584 configured C<ban> command is re-run for every previously banned IP and CIDR
585 range.
586  
587 $fw_helper->re_init;
588  
589 =cut
590  
591 3 ▶sub re_init {
Subroutine re_init: called CC=5 SCAR=16.2
Pod documented
592 3 my ( $self, %opts ) = @_;
593  
594 3 $self->errorblank;
595  
596 # teardown is best effort here as a partially or fully wiped setup is
597 # exactly what re_init needs to recover from; init cleans up any remnants
598 {
599 3 local $@;
600 3 eval { $self->teardown; };
601 }
602 3 $self->init;
603  
604 # both single IPs and CIDR ranges are re-added the same way
605 3 my @to_re_ban = ( keys( %{ $self->{banned} } ), keys( %{ $self->{banned_cidr} } ) );
606  
607 3 foreach my $item (@to_re_ban) {
608 3 my $command = $self->{options}{ban};
609 3 $command =~ s/\%\%\%BAN\%\%\%/$item/g;
610  
611 3 ▶ if ( !$self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"testing"}) 2 1 3
612 2 my $output = `$command 2>&1`;
613 2 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 2 2
614 0 $self->{error} = 13;
615 0 $self->{errorString} = 'Ban failed... command "' . $command . '" resulted in... ' . $output;
616 0 $self->warn;
617 }
618 }
619 } ## end foreach my $items (@to_re_ban)
620  
621 3 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 1 2 3
622 1 $self->{frontend_obj}->{test_data} = 're-inited';
623 }
624  
625 3 $self->{inited} = 1;
626 } ## end sub re_init
627  
628 =head2 teardown
629  
630 Tears down the setup for the backend by running the configured C<teardown>
631 command. A non-zero exit is an error.
632  
633 $fw_helper->teardown;
634  
635 =cut
636  
637 11 ▶sub teardown {
Subroutine teardown: called CC=3 SCAR=11.0
Pod documented
638 11 my ( $self, %opts ) = @_;
639  
640 11 $self->errorblank;
641  
642 11 $self->{inited} = 0;
643  
644 11 my $command = $self->{options}{teardown};
645  
646 11 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 2 9 11
647 2 $self->{frontend_obj}->{test_data} = $command;
648 } else {
649 9 my $output = `$command 2>&1`;
650 9 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 2 7 9
651 2 $self->{error} = 17;
652 2 $self->{errorString} = 'Teardown failed... command "' . $command . '" resulted in... ' . $output;
653 2 $self->warn;
654 }
655 }
656 } ## end sub teardown
657  
658 =head2 stop
659  
660 Alias for L</teardown>, provided for parity with the fail2ban C<actionstop>
661 concept.
662  
663 $fw_helper->stop;
664  
665 =cut
666  
667 0 ▶sub stop {
Subroutine stop: not called CC=1 SCAR=6.9
Pod documented
668 0 my ( $self, %opts ) = @_;
669  
670 0 return $self->teardown(%opts);
671 }
672  
673 =head2 check
674  
675 Runs the optional C<check> command from the options hash. A zero exit code
676 is treated as healthy. If no C<check> command was configured, this always
677 reports healthy. This is the equivalent of fail2ban's C<actioncheck>.
678  
679 if ( !$fw_helper->check ) {
680 $fw_helper->re_init;
681 }
682  
683 =cut
684  
685 21 ▶sub check {
Subroutine check: called CC=6 SCAR=18.5
Pod documented
686 21 my ( $self, %opts ) = @_;
687  
688 21 $self->errorblank;
689  
690 # no check command configured -> assume healthy
691 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
692 5 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 5 0 5
693 5 $self->{frontend_obj}->{test_data} = 'check';
694 }
695 5 return 1;
696 }
697  
698 16 my $command = $self->{options}{check};
699  
700 16 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 16 16
701 0 $self->{frontend_obj}->{test_data} = $command;
702 0 return 1;
703 }
704  
705 16 my $output = `$command 2>&1`;
706 16 ▶ return $? == 0 ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$? == 0 ? : 12 4 16
707 } ## end sub check
708  
709 =head2 flush
710  
711 Removes all currently banned IPs at once. If a C<flush> command was
712 configured in the options hash it is run; otherwise it falls back to
713 unbanning each currently banned IP. This is the equivalent of fail2ban's
714 C<actionflush>.
715  
716 $fw_helper->flush;
717  
718 =cut
719  
720 3 ▶sub flush {
Subroutine flush: called CC=8 SCAR=22.1
Pod documented
721 3 my ( $self, %opts ) = @_;
722  
723 3 $self->errorblank;
724  
725 3 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 3 3
726 0 $self->{error} = 1;
727 0 $self->{errorString} = 'backend has not been inited';
728 0 $self->warn;
729 0 return;
730 }
731  
732 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
733 2 my $command = $self->{options}{flush};
734  
735 2 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 2 2
736 0 $self->{frontend_obj}->{test_data} = $command;
737 } else {
738 2 my $output = `$command 2>&1`;
739 2 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 1 1 2
740 1 $self->{error} = 25;
741 1 $self->{errorString} = 'Flush failed... command "' . $command . '" resulted in... ' . $output;
742 1 $self->warn;
743 }
744 }
745  
746 1 $self->{banned} = {};
747 1 $self->{banned_cidr} = {};
748 } else {
749 # no bulk flush command configured -> unban each currently banned IP
750 1 foreach my $item ( keys( %{ $self->{banned} } ) ) {
751 2 $self->unban( ban => $item );
752 }
753 1 foreach my $item ( keys( %{ $self->{banned_cidr} } ) ) {
754 0 $self->unban_cidr( ban => $item );
755 }
756 }
757 } ## end sub flush
758  
759 =head1 ERROR CODES / FLAGS
760  
761 Error handling is provided by L<Error::Helper>. All
762 errors are considered fatal.
763  
764 =head2 1, notInited
765  
766 The backend has not been inited yet.
767  
768 =head2 2, initInvalid
769  
770 The option init is undef or blank.
771  
772 =head2 3, optionsUndef
773  
774 The options hash passed to new is undef.
775  
776 =head2 4, teardownInvalid
777  
778 The option teardown is undef or blank.
779  
780 =head2 5, banInvalid
781  
782 The option ban is undef or blank.
783  
784 =head2 6, unbanInvalid
785  
786 The option unban is undef or blank.
787  
788 =head2 8, optionsNotHash
789  
790 The item passed to new for options is not a hash.
791  
792 =head2 9, noBanItem
793  
794 No IP or CIDR range specified to ban or unban.
795  
796 =head2 10, banItemNotIP
797  
798 The item to ban is not an IP. Either wrong ref type or regexp
799 test using L<Regexp::IPv4> and L<Regexp::IPv6> failed.
800  
801 =head2 12, backendInitError
802  
803 Failed to init the backend.
804  
805 =head2 13, banFailed
806  
807 Failed to ban the item.
808  
809 =head2 14, unbanFailed
810  
811 Failed to unban the item.
812  
813 =head2 15, listFailed
814  
815 Failed to get a list of bans.
816  
817 =head2 16, reInitFailed
818  
819 Failed to re_init the backend.
820  
821 =head2 17, teardownFailed
822  
823 Failed to teardown the backend.
824  
825 =head2 18, alreadyInited
826  
827 init called, but the backend has already been inited.
828  
829 =head2 24, checkFailed
830  
831 The backend check raised an error.
832  
833 =head2 25, flushFailed
834  
835 Failed to flush the bans.
836  
837 =head2 26, banCidrFailed
838  
839 Failed to ban the CIDR range.
840  
841 =head2 27, unbanCidrFailed
842  
843 Failed to unban the CIDR range.
844  
845 =head2 28, cidrItemNotCidr
846  
847 The item to ban is not a CIDR range. Either wrong ref type or it is not an
848 IPv4 or IPv6 address followed by a prefix length valid for its family.
849  
850 =head2 29, cidrNotSupported
851  
852 The backend does not support CIDR bans.
853  
854 =head2 30, listCidrFailed
855  
856 Failed to get a list of CIDR bans.
857  
858 =head1 AUTHOR
859  
860 Zane C. Bowers-Hadley, C<< <vvelox at vvelox.ent> >>
861  
862 =head1 BUGS
863  
864 Please report any bugs or feature requests to C<bug-net-firewall-blockerhelper at rt.cpan.org>, or through
865 the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Net-Firewall-BlockerHelper>. I will be notified, and then you'll
866 automatically be notified of progress on your bug as I make changes.
867  
868  
869  
870  
871 =head1 SUPPORT
872  
873 You can find documentation for this module with the perldoc command.
874  
875 perldoc Net::Firewall::BlockerHelper
876  
877  
878 You can also look for information at:
879  
880 =over 4
881  
882 =item * RT: CPAN's request tracker (report bugs here)
883  
884 L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=Net-Firewall-BlockerHelper>
885  
886 =item * Search CPAN
887  
888 L<https://metacpan.org/release/Net-Firewall-BlockerHelper>
889  
890 =back
891  
892  
893 =head1 ACKNOWLEDGEMENTS
894  
895  
896 =head1 LICENSE AND COPYRIGHT
897  
898 This software is Copyright (c) 2023 by Zane C. Bowers-Hadley.
899  
900 This is free software, licensed under:
901  
902 The GNU Lesser General Public License, Version 2.1, February 1999
903  
904  
905 =cut
906  
907 4 1; # End of Net::Firewall::BlockerHelper
« BlockerHelper/backends/file_reload.pm ↑ Summary