BlockerHelper.pm

filter: Statementstmt 45.1% 94 / 208 Branchbran 51.6% 32 / 62 Conditioncond 40.7% 11 / 27 MC/DCmcdc 5.8% 1 / 17 Subroutinesub 100% 14 / 14 Podpod 100% 7 / 7 totaltotal 47.4% 159 / 335 CC 43 SCAR 43.6
CC
43
Cov
46%
new
165.1
ban
15.8
unban
15.8
CRAP 78.6
« BlockerHelper/backends/pf.pm ↑ Summary BlockerHelper/backends/shell.pm »
1 package Net::Firewall::BlockerHelper;
2  
3 6 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 ▶use base 'Error::Helper';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 ▶use Regexp::IPv4 qw($IPv4_re);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 6 ▶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.0.1
17  
18 =cut
19  
20 6 our $VERSION = '0.0.1';
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 positive ints or a valid
81 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 All errors are considered fatal, meaning if new fails it will die.
95  
96 my $fw_helper;
97 eval {
98 $fw_helper = Net::Firewall::BlockerHelper->new(
99 backend => 'ipfw',
100 ports => ['22'],
101 protocols => ['tcp'],
102 name => 'ssh',
103 );
104 };
105 if ($@) {
106 print 'Error: '
107 . $Error::Helper::error
108 . "\nError String: "
109 . $Error::Helper::errorString
110 . "\nError Flag: "
111 . $Error::Helper::errorFlag . "\n";
112 }
113  
114 =cut
115  
116 sub new {
117 5 ▶ my ( $blank, %opts ) = @_;
Subroutine new: called CC=25 SCAR=51.1
Pod documented
118  
119 5 my $self = {
120 perror => undef,
121 error => undef,
122 errorLine => undef,
123 errorFilename => undef,
124 errorString => "",
125 errorExtra => {
126 all_errors_fatal => 1,
127 flags => {
128 1 => 'noBackendSpecified',
129 2 => 'invalidPortSpecified',
130 3 => 'portsNotArray',
131 4 => 'protocolsNotArray',
132 5 => 'invalidPortSpecified',
133 6 => 'invalidPrefixSpecified',
134 7 => 'invalidName',
135 8 => 'optionsNotHash',
136 9 => 'noBanItem',
137 10 => 'banItemNotIP',
138 11 => 'invalidBackend',
139 12 => 'backendInitError',
140 13 => 'banFailed',
141 14 => 'unbanFailed',
142 15 => 'listFailed',
143 16 => 'reInitFailed',
144 17 => 'teardownFailed',
145 },
146 fatal_flags => {},
147 perror_not_fatal => 0,
148 },
149 backend => undef,
150 options => {},
151 ports => [],
152 protocols => [],
153 testing => undef,
154 test_data => undef,
155 prefix => 'kur',
156 name => undef,
157 backend_obj => undef,
158 };
159 5 bless $self;
160  
161 5 ▶ if ( !defined $opts{backend} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $opts{"backend"}) 0 5 5
162 0 $self->{perror} = 1;
163 0 $self->{error} = 1;
164 0 $self->{errorString} = 'backend is undef';
165 0 $self->warn;
166 }
167 5 $self->{backend} = $opts{backend};
168  
169 5 ▶ if ( $self->{backend} !~ /^[a-zA-Z0-9\_]+$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"backend"} =~ /^[a-zA-Z0-9\_]+$/) 0 5 5
170 0 $self->{perror} = 11;
171 0 $self->{error} = 1;
172 $self->{errorString}
173 = '"'
174 . $self->{backend}
175 0 . '" does not appear to be valid backend, the regexp /^[a-zA-Z0-9\_]+$/ does not match';
176 0 $self->warn;
177 }
178  
179 5 ▶ if ( defined( $opts{ports} ) && ref( $opts{ports} ) ne 'ARRAY' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $opts{'ports'} and ref $opts{'ports'} ne 'ARRAY') { } 0 5 5
elsif (defined $opts{'ports'}) { } 4 1 5
Condition66%2/3
defined $opts{"ports"} and ref $opts{"ports"} ne "ARRAY"
!ll&&!rl&&r
140
MC/DC0%0/2
defined $opts{"ports"} and ref $opts{"ports"} ne "ARRAY"
defined $opts{"ports"} ref $opts{"ports"} ne "ARRAY"
Truth table66%2/3
A and B
A= defined $opts{"ports"}B= ref $opts{"ports"} ne "ARRAY"
ABresult
0X0
100
111
180 0 $self->{perror} = 1;
181 0 $self->{error} = 3;
182 0 $self->{errorString} = 'ports is defined and type is not array but "' . ref( $opts{ports} ) . '"';
183 0 $self->warn;
184 } elsif ( defined( $opts{ports} ) ) {
185 4 my %ports;
186 4 foreach my $item ( @{ $opts{ports} } ) {
187 8 ▶ if ( $item =~ /^[0-9]+$/ && $item >= 1 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($item =~ /^[0-9]+$/ and $item >= 1) { } 4 4 8
elsif ($item =~ /^[0-9]+$/ and $item < 1) { } 0 4 4
Condition66%2/3
$item =~ /^[0-9]+$/ and $item >= 1
!ll&&!rl&&r
404
MC/DC50%1/2
$item =~ /^[0-9]+$/ and $item >= 1
$item =~ /^[0-9]+$/ $item >= 1
Truth table66%2/3
A and B
A= $item =~ /^[0-9]+$/B= $item >= 1
ABresult
0X0
100
111
188 #push( @{ $self->{ports} }, $item );
189 4 $ports{$item} = 1;
190 ▶ } elsif ( $item =~ /^[0-9]+$/ && $item < 1 ) {
Condition33%1/3
$item =~ /^[0-9]+$/ and $item < 1
!ll&&!rl&&r
400
MC/DC0%0/2
$item =~ /^[0-9]+$/ and $item < 1
$item =~ /^[0-9]+$/ $item < 1
Truth table33%1/3
A and B
A= $item =~ /^[0-9]+$/B= $item < 1
ABresult
0X0
100
111
191 0 $self->{perror} = 1;
192 0 $self->{error} = 2;
193 $self->{errorString}
194 0 = $item . ' is not a valid value for a port as it must be a int greater or equal to 1';
195 0 $self->warn;
196 } else {
197 # just using tcp here as protocol must be specified
198 4 my ( $name, $aliases, $port, $proto ) = getservbyname( $item, 'tcp' );
199 4 ▶ if ( !defined($port) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $port) 4 0 4
200 4 $self->{perror} = 1;
201 4 $self->{error} = 2;
202 $self->{errorString}
203 4 = $item . ' could not be resolved to a port name via getservbyname("' . $item . '", "tcp")';
204 4 $self->warn;
205 }
206 0 $ports{$port} = 1;
207 #push( @{ $self->{ports} }, $port );
208 } ## end else [ if ( $item =~ /^[0-9]+$/ && $item >= 1 ) ]
209 } ## end foreach my $item ( @{ $opts{ports} } )
210 0 my @port_keys = keys(%ports);
211 0 @port_keys = sort { $a <=> $b } @port_keys;
212 0 push( @{ $self->{ports} }, @port_keys );
213 ## end foreach my $item ( @{ $opts{ports} } )
214 } ## end elsif ( defined( $opts{ports} ) )
215  
216 1 ▶ if ( defined( $opts{protocols} ) && ref( $opts{protocols} ) ne 'ARRAY' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $opts{'protocols'} and ref $opts{'protocols'} ne 'ARRAY') { } 0 1 1
elsif (defined $opts{'protocols'}) { } 0 1 1
Condition33%1/3
defined $opts{"protocols"} and ref $opts{"protocols"} ne "ARRAY"
!ll&&!rl&&r
100
MC/DC0%0/2
defined $opts{"protocols"} and ref $opts{"protocols"} ne "ARRAY"
defined $opts{"protocols"} ref $opts{"protocols"} ne "ARRAY"
Truth table33%1/3
A and B
A= defined $opts{"protocols"}B= ref $opts{"protocols"} ne "ARRAY"
ABresult
0X0
100
111
217 0 $self->{perror} = 1;
218 0 $self->{error} = 4;
219 0 $self->{errorString} = 'protocols is defined and type is not array but "' . ref( $opts{protocols} ) . '"';
220 0 $self->warn;
221 } elsif ( defined( $opts{protocols} ) ) {
222 0 my %protocols;
223 0 foreach my $item ( @{ $opts{protocols} } ) {
224 0 my ( $name, $aliases, $proto ) = getprotobyname($item);
225 # if this is undef, it means it is not a known protocol
226 0 ▶ if ( !defined($proto) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $proto) 0 0 0
227 0 $self->{perror} = 1;
228 0 $self->{error} = 5;
229 $self->{errorString}
230 0 = $item . ' could not be resolved to a port name via getservbyname("' . $item . '", "tcp")';
231 0 $self->warn;
232 }
233 0 $protocols{$item} = 1;
234 } ## end foreach my $item ( @{ $opts{protocols} } )
235 0 my @protocols_keys = keys(%protocols);
236 0 @protocols_keys = sort { $a cmp $b } @protocols_keys;
237 0 push( @{ $self->{protocols} }, @protocols_keys );
238 } ## end elsif ( defined( $opts{protocols} ) )
239  
240 # make sure prefix is sane if defiend
241 1 ▶ if ( defined( $opts{prefix} ) && $opts{prefix} !~ /^[a-zA-Z0-9]+$/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $opts{'prefix'} and not $opts{'prefix'} =~ /^[a-zA-Z0-9]+$/) { } 0 1 1
elsif (defined $opts{'prefix'}) { } 0 1 1
Condition33%1/3
defined $opts{"prefix"} and not $opts{"prefix"} =~ /^[a-zA-Z0-9]+$/
!ll&&!rl&&r
100
MC/DC0%0/2
defined $opts{"prefix"} and not $opts{"prefix"} =~ /^[a-zA-Z0-9]+$/
defined $opts{"prefix"} not $opts{"prefix"} =~ /^[a-zA-Z0-9]+$/
Truth table33%1/3
A and not(B)
A= defined $opts{"prefix"}B= not $opts{"prefix"} =~ /^[a-zA-Z0-9]+$/
ABresult
0X0
100
111
242 0 $self->{perror} = 1;
243 0 $self->{error} = 6;
244 $self->{errorString}
245 0 = '"' . $opts{prefix} . '" is not a valid prefix as it does not match the regex /^[a-zA-Z0-9]+$/';
246 0 $self->warn;
247 } elsif ( defined( $opts{prefix} ) ) {
248 0 $self->{prefix} = $opts{prefix};
249 }
250  
251 # make sure we have a name and that it is valid
252 1 ▶ if ( !defined( $opts{name} ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $opts{'name'}) { } 0 1 1
elsif (not $opts{'name'} =~ /^[a-zA-Z0-9\-]+$/) { } 0 1 1
253 0 $self->{perror} = 1;
254 0 $self->{error} = 6;
255 0 $self->{errorString} = 'name is undef';
256 0 $self->warn;
257 } elsif ( $opts{name} !~ /^[a-zA-Z0-9\-]+$/ ) {
258 0 $self->{perror} = 1;
259 0 $self->{error} = 6;
260 0 $self->{errorString} = 'name set to "' . $opts{name} . '" which does not match the regexp /^[a-zA-Z0-9\-]+$/';
261 0 $self->warn;
262 }
263 1 $self->{name} = $opts{name};
264  
265 # used internally for testing
266 1 ▶ if ( defined( $opts{testing} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"testing"}) 1 0 1
267 1 $self->{testing} = $opts{testing};
268 }
269  
270 1 ▶ if ( defined( $opts{options} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"options"}) 1 0 1
271 1 ▶ if ( ref( $opts{options} ) ne 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $opts{"options"} ne "HASH") 0 1 1
272 0 $self->{perror} = 1;
273 0 $self->{error} = 8;
274 0 $self->{errorString} = 'ref for options is "' . ref( $opts{options} ) . '" and not HASH';
275 0 $self->warn;
276 }
277 1 $self->{options} = $opts{options};
278 }
279  
280 1 return $self;
281 } ## end sub new
282  
283 =head2 init_backend
284  
285 Initiates the backend.
286  
287 No arguments are taken.
288  
289 $fw_helper->init_backend;
290  
291 =cut
292  
293 sub init_backend {
294 1 ▶ my ( $self, %opts ) = @_;
Subroutine init_backend: called CC=6 SCAR=23.9
Pod documented
295  
296 1 $self->errorblank;
297 1 $self->{test_data}=undef;
298  
299 1 my $backend = 'Net::Firewall::BlockerHelper::backends::' . $self->{backend};
300 1 my $backend_obj;
301 1 my $init_string
302 = 'use '
303 . $backend
304 . '; $backend_obj='
305 . $backend
306 . '->new('
307 . 'options=>$self->{options}, '
308 . 'ports=>$self->{ports}, '
309 . 'protocols=>$self->{protocols}, '
310 . 'testing=>$self->{testing}, '
311 . 'prefix=>$self->{prefix}, '
312 . 'name=>$self->{name}, '
313 . 'frontend_obj=>$self, '
314 . '); $backend_obj->init;';
315 1 ▶ eval($init_string);
Subroutine BEGIN: called CC=1 SCAR=0.0
316 1 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 1 1
317 0 $self->{perror} = 1;
318 0 $self->{error} = 12;
319 0 $self->{errorString} = 'Failed to init backend... ' . $@;
320 0 $self->warn;
321 }
322 # make sure we got something that is defined and is a object of some sort
323 1 ▶ if ( !defined($backend_obj) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $backend_obj) { } 0 1 1
elsif (ref $backend_obj eq '' or ref $backend_obj eq 'ARRAY' or ref $backend_obj eq 'HASH') { } 0 1 1
324 0 $self->{perror} = 1;
325 0 $self->{error} = 12;
326 0 $self->{errorString} = 'Failed to init backend. Eval did not die, but returned value is undef';
327 0 $self->warn;
328 ▶ } 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
001
ref $backend_obj eq "" or ref $backend_obj eq "ARRAY"
l!l&&r!l&&!r
001
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
329 0 $self->{perror} = 1;
330 0 $self->{error} = 12;
331 0 $self->{errorString} = 'ref($backend_obj) is "' . ref($backend_obj) . '"';
332 0 $self->warn;
333 }
334  
335 1 $self->{backend_obj} = $backend_obj;
336 } ## end sub init_backend
337  
338 =head2 ban
339  
340 Bans the IP.
341  
342 $fw_helper->ban(ban => $ip);
343  
344 =cut
345  
346 sub ban {
347 2 ▶ my ( $self, %opts ) = @_;
Subroutine ban: called CC=6 SCAR=27.6
Pod documented
348  
349 2 $self->errorblank;
350 2 $self->{test_data}=undef;
351  
352 2 ▶ if ( !defined( $opts{ban} ) ) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $opts{'ban'}) { } 0 2 2
elsif (ref $opts{'ban'} ne '') { } 0 2 2
elsif (not $opts{'ban'} =~ /$Net::Firewall::BlockerHelper::IPv4_re/ || $opts{'ban'} =~ /$Net::Firewall::BlockerHelper::IPv6_re/) { } 0 2 2
353 0 $self->{error} = 9;
354 0 $self->{errorString} = 'Nothing specified for the value ban';
355 0 $self->warn;
356 0 return;
357 } elsif ( ref( $opts{ban} ) ne '' ) {
358 0 $self->{error} = 10;
359 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
360 0 $self->warn;
361 0 return;
362 ▶ } elsif ( $opts{ban} !~ /$IPv4_re/
Condition33%1/3
$opts{'ban'} =~ /$Net::Firewall::BlockerHelper::IPv4_re/ || $opts{'ban'} =~ /$Net::Firewall::BlockerHelper::IPv6_re/
l!l&&r!l&&!r
200
MC/DC0%0/2
$opts{'ban'} =~ /$Net::Firewall::BlockerHelper::IPv4_re/ || $opts{'ban'} =~ /$Net::Firewall::BlockerHelper::IPv6_re/
$opts{'ban'} =~ /$Net::Firewall::BlockerHelper::IPv4_re/ $opts{'ban'} =~ /$Net::Firewall::BlockerHelper::IPv6_re/
Truth table33%1/3
A || B
A= $opts{'ban'} =~ /$Net::Firewall::BlockerHelper::IPv4_re/B= $opts{'ban'} =~ /$Net::Firewall::BlockerHelper::IPv6_re/
ABresult
1X1
011
000
363 && $opts{ban} !~ /$IPv6_re/ )
364 {
365 0 $self->{error} = 10;
366 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 IP';
367 0 $self->warn;
368 0 return;
369 }
370  
371 2 eval { $self->{backend_obj}->ban( ban => $opts{ban} ); };
372 2 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 2 2
373 0 $self->{error} = 13;
374 0 $self->{errorString} = 'banning item,"' . $opts{ban} . '", failed... ' . $@;
375 0 $self->warn;
376 0 return;
377 }
378 } ## end sub ban
379  
380 =head2 unban
381  
382 Unbans the an IP.
383  
384 $fw_helper->unban(ban => $ip);
385  
386 =cut
387  
388 sub unban {
389 1 ▶ my ( $self, %opts ) = @_;
Subroutine unban: called CC=6 SCAR=27.6
Pod documented
390  
391 1 $self->errorblank;
392 1 $self->{test_data}=undef;
393  
394 1 ▶ if ( !defined( $opts{ban} ) ) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $opts{'ban'}) { } 0 1 1
elsif (ref $opts{'ban'} ne '') { } 0 1 1
elsif (not $opts{'ban'} =~ /$Net::Firewall::BlockerHelper::IPv4_re/ || $opts{'ban'} =~ /$Net::Firewall::BlockerHelper::IPv6_re/) { } 0 1 1
395 0 $self->{error} = 9;
396 0 $self->{errorString} = 'Nothing specified for the value ban';
397 0 $self->warn;
398 0 return;
399 } elsif ( ref( $opts{ban} ) ne '' ) {
400 0 $self->{error} = 10;
401 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
402 0 $self->warn;
403 0 return;
404 ▶ } elsif ( $opts{ban} !~ /$IPv4_re/
Condition33%1/3
$opts{'ban'} =~ /$Net::Firewall::BlockerHelper::IPv4_re/ || $opts{'ban'} =~ /$Net::Firewall::BlockerHelper::IPv6_re/
l!l&&r!l&&!r
100
MC/DC0%0/2
$opts{'ban'} =~ /$Net::Firewall::BlockerHelper::IPv4_re/ || $opts{'ban'} =~ /$Net::Firewall::BlockerHelper::IPv6_re/
$opts{'ban'} =~ /$Net::Firewall::BlockerHelper::IPv4_re/ $opts{'ban'} =~ /$Net::Firewall::BlockerHelper::IPv6_re/
Truth table33%1/3
A || B
A= $opts{'ban'} =~ /$Net::Firewall::BlockerHelper::IPv4_re/B= $opts{'ban'} =~ /$Net::Firewall::BlockerHelper::IPv6_re/
ABresult
1X1
011
000
405 && $opts{ban} !~ /$IPv6_re/ )
406 {
407 0 $self->{error} = 10;
408 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 IP';
409 0 $self->warn;
410 0 return;
411 }
412  
413 1 eval { $self->{backend_obj}->unban( ban => $opts{ban} ); };
414 1 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 1 1
415 0 $self->{error} = 14;
416 0 $self->{errorString} = 'unbanning item,"' . $opts{ban} . '", failed... ' . $@;
417 0 $self->warn;
418 0 return;
419 }
420 } ## end sub unban
421  
422 =head2 list
423  
424 List banned IPs.
425  
426 my @banned = $fw_helper->list;
427  
428 =cut
429  
430 sub list {
431 2 ▶ my ( $self, %opts ) = @_;
Subroutine list: called CC=2 SCAR=7.8
Pod documented
432  
433 2 $self->errorblank;
434 2 $self->{test_data}=undef;
435  
436 2 my @banned;
437 2 eval { @banned = $self->{backend_obj}->list; };
438 2 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 2 2
439 0 $self->{error} = 15;
440 0 $self->{errorString} = 'listing bans failed... ' . $@;
441 0 $self->warn;
442 0 return;
443 }
444  
445 2 return @banned;
446 } ## end sub list
447  
448 =head2 re_init
449  
450 Tells the backend to re-init it's self.
451  
452 =cut
453  
454 sub re_init {
455 1 ▶ my ( $self, %opts ) = @_;
Subroutine re_init: called CC=2 SCAR=8.3
Pod documented
456  
457 1 $self->errorblank;
458 1 $self->{test_data}=undef;
459  
460 1 eval { $self->{backend_obj}->re_init; };
461 1 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 1 1
462 0 $self->{error} = 16;
463 0 $self->{errorString} = 'backend re_init failed... ' . $@;
464 0 $self->warn;
465 0 return;
466 }
467 } ## end sub re_init
468  
469 =head2 teardown
470  
471 Tears down the setup for the backend.
472  
473 =cut
474  
475 sub teardown {
476 1 ▶ my ( $self, %opts ) = @_;
Subroutine teardown: called CC=2 SCAR=8.3
Pod documented
477  
478 1 $self->errorblank;
479 1 $self->{test_data}=undef;
480  
481 1 eval { $self->{backend_obj}->teardown; };
482 1 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 1 1
483 0 $self->{error} = 17;
484 0 $self->{errorString} = 'backend teardown failed... ' . $@;
485 0 $self->warn;
486 0 return;
487 }
488 } ## end sub teardown
489  
490 =head1 ERROR CODES / FLAGS
491  
492 Error handling is provided by L<Error::Helper>. All
493 errors are considered fatal.
494  
495 =head2 1, noBackendSpecified
496  
497 No backend was specified to use.
498  
499 =head2 2, invalidPortSpecified
500  
501 Port is either not a positive int or a name that can be resolved by getservbyname.
502  
503 =head2 3, portsNotArray
504  
505 The data passed to new for ports is not an array.
506  
507 =head2 4, protocolsNotArray
508  
509 The data passed to new for protocols is not an array.
510  
511 =head2 5, invalidPortSpecified
512  
513 Port is either not a positive int or a name that can be resolved by getservbyname.
514  
515 =head2 6, invalidPrefixSpecified
516  
517 The specified prefix did not match /^[a-zA-Z0-9]+$/.
518  
519 =head2 7, invalidName
520  
521 The name is either undef or does not match /^[a-zA-Z0-9\-]+$/.
522  
523 =head2 8, optionsNotHash
524  
525 The item passed to new for options is not a hash.
526  
527 =head2 9, noBanItem
528  
529 No IP specified to ban.
530  
531 =head2 10, banItemNotIP
532  
533 The item to ban is not an IP. Either wrong ref type or regexp
534 test using L<Regexp::IPv4> and L<Regexp::IPv6> failed.
535  
536 =head2 11, invalidBackend
537  
538 The specified backend failed to pass a basic sanity check of making sure it
539 matches the regexp /^[a-zA-Z0-9\_]+$/.
540  
541 =head2 12, backendInitError
542  
543 Failed to init the backend.
544  
545 =head2 13, banFailed
546  
547 Failed to ban the item.
548  
549 =head2 14, unbanFailed
550  
551 Failed to unban the item.
552  
553 =head2 15, listFailed
554  
555 Failed get a list of bans.
556  
557 =head2 16, reInitFailed
558  
559 Failed to re_init the backend.
560  
561 =head2 17, teardownFailed
562  
563 Failed to teardown the backend.
564  
565 =head1 AUTHOR
566  
567 Zane C. Bowers-Hadley, C<< <vvelox at vvelox.ent> >>
568  
569 =head1 BUGS
570  
571 Please report any bugs or feature requests to C<bug-net-firewall-blockerhelper at rt.cpan.org>, or through
572 the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Net-Firewall-BlockerHelper>. I will be notified, and then you'll
573 automatically be notified of progress on your bug as I make changes.
574  
575  
576  
577  
578 =head1 SUPPORT
579  
580 You can find documentation for this module with the perldoc command.
581  
582 perldoc Net::Firewall::BlockerHelper
583  
584  
585 You can also look for information at:
586  
587 =over 4
588  
589 =item * RT: CPAN's request tracker (report bugs here)
590  
591 L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=Net-Firewall-BlockerHelper>
592  
593 =item * CPAN Ratings
594  
595 L<https://cpanratings.perl.org/d/Net-Firewall-BlockerHelper>
596  
597 =item * Search CPAN
598  
599 L<https://metacpan.org/release/Net-Firewall-BlockerHelper>
600  
601 =back
602  
603  
604 =head1 ACKNOWLEDGEMENTS
605  
606  
607 =head1 LICENSE AND COPYRIGHT
608  
609 This software is Copyright (c) 2023 by Zane C. Bowers-Hadley.
610  
611 This is free software, licensed under:
612  
613 The GNU Lesser General Public License, Version 2.1, February 1999
614  
615  
616 =cut
617  
618 6 1; # End of Net::Firewall::BlockerHelper
« BlockerHelper/backends/pf.pm ↑ Summary BlockerHelper/backends/shell.pm »