BlockerHelper/backends/pf.pm

filter: Statementstmt 7.2% 19 / 264 Branchbran 0.0% 0 / 92 Conditioncond 0.0% 0 / 21 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 46.1% 6 / 13 Podpod 100% 7 / 7 totaltotal 7.7% 32 / 411 CC 63 SCAR 55.8
CC
63
Cov
5%
new
600.0
init
182.0
ban
156.0
CRAP 263.8
« BlockerHelper/backends/dummy.pm ↑ Summary BlockerHelper.pm »
1 package Net::Firewall::BlockerHelper::backends::pf;
2  
3 1 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use base 'Error::Helper';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Regexp::IPv4 qw($IPv4_re);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Regexp::IPv6 qw($IPv6_re);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 =head1 NAME
11  
12 Net::Firewall::BlockerHelper::backends::pf - pf backend for Net::Firewall::BlockerHelper.
13  
14 =head1 VERSION
15  
16 Version 0.0.1
17  
18 =cut
19  
20 1 our $VERSION = '0.0.1';
21  
22 =head1 SYNOPSIS
23  
24 use Net::Firewall::BlockerHelper::backends::pf;
25  
26 my $backend1;
27 my $backend2;
28 eval {
29 $backend1 = Net::Firewall::BlockerHelper::backends::pf->new(
30 backend => 'ipfw',
31 name => 'all',
32 options=>{ kill=>1 },
33 );
34 $backend2 = Net::Firewall::BlockerHelper::backends::pf->new(
35 backend => 'ipfw',
36 ports => ['143'],
37 protocols => ['tcp'],
38 name => 'imap',
39 options=>{ kill=>0 },
40 );
41 };
42 if ($@) {
43 print 'Error: '
44 . $Error::Helper::error
45 . "\nError String: "
46 . $Error::Helper::errorString
47 . "\nError Flag: "
48 . $Error::Helper::errorFlag . "\n";
49 }
50  
51 print `ipfw list`
52  
53 $backend1->init;
54 $backend2->init;
55  
56 print `ipfw list`
57  
58 $backend1->ban(ban=>'1.2.3.4');
59 $backend1->ban(ban=>'4.3.2.1');
60 $backend2->ban(ban=>'4.3.2.1');
61  
62 use Data::Dumper;
63 print Dumper($backend1->list);
64 print Dumper($backend2->list);
65  
66 $backend1->unban(ban=>'4.3.2.1');
67  
68 use Data::Dumper;
69 print Dumper($backend1->list);
70 print Dumper($backend2->list);
71  
72 $backend1->teardown;
73  
74 print `ipfw list`
75  
76 $backend2->teardown;
77  
78 print `ipfw list`
79  
80 =head1 METHODS
81  
82 =head2 new
83  
84 Initiates the the object.
85  
86 - options :: Backend specific options that will be passed to the backend unchecked
87 outside of making sure it is a hash ref if defined. See below for furhter info.
88 - Default :: {}
89  
90 - ports :: A array of ports to block. Checked to make sure they are positive ints or a valid
91 service name via getservbyname. All ports will be blocked if non are specified. If
92 duplicates are removed.
93 - Default :: []
94  
95 - protocols :: A array of protocols to block. By default will block all. This
96 is checked against /etc/protocols via the function getprotobyname. Duplicates
97 will be discarded.
98 - Default :: []
99  
100 - prefix :: Prefix to use. Must match the regex /^[a-zA-Z0-9]+$/
101 - default :: kur
102  
103 - name :: Name of this specific instance. This must be specified.
104 - default :: undef
105  
106 The options hash accepts the following.
107  
108 - kill :: If it should kill connections to the banned IP or not.
109 - Default :: 0
110  
111 All errors are considered fatal, meaning if new fails it will die.
112  
113 my $backend;
114 eval {
115 $backend = Net::Firewall::BlockerHelper::backends::pf->new(
116 backend => 'ipfw',
117 ports => ['22'],
118 protocols => ['tcp'],
119 name => 'ssh',
120 );
121 };
122 if ($@) {
123 print 'Error: '
124 . $Error::Helper::error
125 . "\nError String: "
126 . $Error::Helper::errorString
127 . "\nError Flag: "
128 . $Error::Helper::errorFlag . "\n";
129 }
130  
131 =cut
132  
133 sub new {
134 0 ▶ my ( $blank, %opts ) = @_;
Subroutine new: not called CC=24 SCAR=64.0
Pod documented
135  
136 0 my $self = {
137 perror => undef,
138 error => undef,
139 errorLine => undef,
140 errorFilename => undef,
141 errorString => "",
142 errorExtra => {
143 all_errors_fatal => 1,
144 flags => {
145 1 => 'notInited',
146 2 => 'invalidPortSpecified',
147 3 => 'portsNotArray',
148 4 => 'protocolsNotArray',
149 5 => 'invalidPortSpecified',
150 6 => 'invalidPrefixSpecified',
151 7 => 'invalidName',
152 8 => 'optionsNotHash',
153 9 => 'noBanItem',
154 10 => 'banItemNotIP',
155 11 => 'invalidBackend',
156 12 => 'backendInitError',
157 13 => 'banFailed',
158 14 => 'unbanFailed',
159 15 => 'listFailed',
160 16 => 'reInitFailed',
161 17 => 'teardownFailed',
162 18 => 'alreadyInited',
163 23 => 'initFailed',
164 },
165 fatal_flags => {},
166 perror_not_fatal => 0,
167 },
168 options => {
169 kill => 0,
170 },
171 ports => [],
172 protocols => [],
173 testing => undef,
174 test_data => undef,
175 prefix => 'kur',
176 postfix => undef,
177 frontend_obj => undef,
178 inited => 0,
179 banned => {},
180 };
181 0 bless $self;
182  
183 0 ▶ if ( defined( $opts{ports} ) && ref( $opts{ports} ) ne 'ARRAY' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $opts{'ports'} and ref $opts{'ports'} ne 'ARRAY') { } 0 0 0
elsif (defined $opts{'ports'}) { } 0 0 0
Condition0%0/3
defined $opts{"ports"} and ref $opts{"ports"} ne "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $opts{"ports"} and ref $opts{"ports"} ne "ARRAY"
defined $opts{"ports"} ref $opts{"ports"} ne "ARRAY"
Truth table0%0/3
A and B
A= defined $opts{"ports"}B= ref $opts{"ports"} ne "ARRAY"
ABresult
0X0
100
111
184 0 $self->{perror} = 1;
185 0 $self->{error} = 3;
186 0 $self->{errorString} = 'ports is defined and type is not array but "' . ref( $opts{ports} ) . '"';
187 0 $self->warn;
188 } elsif ( defined( $opts{ports} ) ) {
189 0 my %ports;
190 0 foreach my $item ( @{ $opts{ports} } ) {
191 0 ▶ if ( $item =~ /^[0-9]+$/ && $item >= 1 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($item =~ /^[0-9]+$/ and $item >= 1) { } 0 0 0
elsif ($item =~ /^[0-9]+$/ and $item < 1) { } 0 0 0
Condition0%0/3
$item =~ /^[0-9]+$/ and $item >= 1
!ll&&!rl&&r
000
MC/DC0%0/2
$item =~ /^[0-9]+$/ and $item >= 1
$item =~ /^[0-9]+$/ $item >= 1
Truth table0%0/3
A and B
A= $item =~ /^[0-9]+$/B= $item >= 1
ABresult
0X0
100
111
192 0 $ports{$item} = 1;
193 ▶ } elsif ( $item =~ /^[0-9]+$/ && $item < 1 ) {
Condition0%0/3
$item =~ /^[0-9]+$/ and $item < 1
!ll&&!rl&&r
000
MC/DC0%0/2
$item =~ /^[0-9]+$/ and $item < 1
$item =~ /^[0-9]+$/ $item < 1
Truth table0%0/3
A and B
A= $item =~ /^[0-9]+$/B= $item < 1
ABresult
0X0
100
111
194 0 $self->{perror} = 1;
195 0 $self->{error} = 2;
196 $self->{errorString}
197 0 = $item . ' is not a valid value for a port as it must be a int greater or equal to 1';
198 0 $self->warn;
199 } else {
200 # just using tcp here as protocol must be specified
201 0 my ( $name, $aliases, $port, $proto ) = getservbyname( $item, 'tcp' );
202 0 ▶ if ( !defined($port) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $port) 0 0 0
203 0 $self->{perror} = 1;
204 0 $self->{error} = 2;
205 $self->{errorString}
206 0 = $item . ' could not be resolved to a port name via getservbyname("' . $item . '", "tcp")';
207 0 $self->warn;
208 }
209 0 $ports{$port} = 1;
210 } ## end else [ if ( $item =~ /^[0-9]+$/ && $item >= 1 ) ]
211 } ## end foreach my $item ( @{ $opts{ports} } )
212 0 my @port_keys = keys(%ports);
213 0 @port_keys = sort { $a <=> $b } @port_keys;
214 0 push( @{ $self->{ports} }, @port_keys );
215 } ## end elsif ( defined( $opts{ports} ) )
216  
217 0 ▶ if ( defined( $opts{protocols} ) && ref( $opts{protocols} ) ne 'ARRAY' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $opts{'protocols'} and ref $opts{'protocols'} ne 'ARRAY') { } 0 0 0
elsif (defined $opts{'protocols'}) { } 0 0 0
Condition0%0/3
defined $opts{"protocols"} and ref $opts{"protocols"} ne "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $opts{"protocols"} and ref $opts{"protocols"} ne "ARRAY"
defined $opts{"protocols"} ref $opts{"protocols"} ne "ARRAY"
Truth table0%0/3
A and B
A= defined $opts{"protocols"}B= ref $opts{"protocols"} ne "ARRAY"
ABresult
0X0
100
111
218 0 $self->{perror} = 1;
219 0 $self->{error} = 4;
220 0 $self->{errorString} = 'protocols is defined and type is not array but "' . ref( $opts{protocols} ) . '"';
221 0 $self->warn;
222 } elsif ( defined( $opts{protocols} ) ) {
223 0 my %protocols;
224 0 foreach my $item ( @{ $opts{protocols} } ) {
225 0 my ( $name, $aliases, $proto ) = getprotobyname($item);
226 # if this is undef, it means it is not a known protocol
227 0 ▶ if ( !defined($proto) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $proto) 0 0 0
228 0 $self->{perror} = 1;
229 0 $self->{error} = 5;
230 $self->{errorString}
231 0 = $item . ' could not be resolved to a port name via getservbyname("' . $item . '", "tcp")';
232 0 $self->warn;
233 }
234 0 $protocols{$item} = 1;
235 } ## end foreach my $item ( @{ $opts{protocols} } )
236 0 my @protocols_keys = keys(%protocols);
237 0 @protocols_keys = sort { $a cmp $b } @protocols_keys;
238 0 push( @{ $self->{protocols} }, @protocols_keys );
239 } ## end elsif ( defined( $opts{protocols} ) )
240  
241 # make sure prefix is sane if defiend
242 0 ▶ 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 0 0
elsif (defined $opts{'prefix'}) { } 0 0 0
Condition0%0/3
defined $opts{"prefix"} and not $opts{"prefix"} =~ /^[a-zA-Z0-9]+$/
!ll&&!rl&&r
000
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 table0%0/3
A and not(B)
A= defined $opts{"prefix"}B= not $opts{"prefix"} =~ /^[a-zA-Z0-9]+$/
ABresult
0X0
100
111
243 0 $self->{perror} = 1;
244 0 $self->{error} = 6;
245 $self->{errorString}
246 0 = '"' . $opts{prefix} . '" is not a valid prefix as it does not match the regex /^[a-zA-Z0-9]+$/';
247 0 $self->warn;
248 } elsif ( defined( $opts{prefix} ) ) {
249 0 $self->{prefix} = $opts{prefix};
250 }
251  
252 # make sure we have a name and that it is valid
253 0 ▶ if ( !defined( $opts{name} ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $opts{'name'}) { } 0 0 0
elsif (not $opts{'name'} =~ /^[a-zA-Z0-9\-]+$/) { } 0 0 0
254 0 $self->{perror} = 1;
255 0 $self->{error} = 6;
256 0 $self->{errorString} = 'name is undef';
257 0 $self->warn;
258 } elsif ( $opts{name} !~ /^[a-zA-Z0-9\-]+$/ ) {
259 0 $self->{perror} = 1;
260 0 $self->{error} = 6;
261 0 $self->{errorString} = 'name set to "' . $opts{name} . '" which does not match the regexp /^[a-zA-Z0-9\-]+$/';
262 0 $self->warn;
263 }
264 0 $self->{name} = $opts{name};
265  
266 # used internally for testing
267 0 ▶ if ( defined( $opts{testing} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"testing"}) 0 0 0
268 0 $self->{testing} = $opts{testing};
269 }
270 0 ▶ if ( defined( $opts{frontend_obj} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"frontend_obj"}) 0 0 0
271 0 $self->{frontend_obj} = $opts{frontend_obj};
272 }
273  
274 0 ▶ if ( defined( $opts{options} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"options"}) 0 0 0
275 0 ▶ if ( ref( $opts{options} ) ne 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $opts{"options"} ne "HASH") 0 0 0
276 0 $self->{perror} = 1;
277 0 $self->{error} = 8;
278 0 $self->{errorString} = 'ref for options is "' . ref( $opts{options} ) . '" and not HASH';
279 0 $self->warn;
280 }
281 0 $self->{options} = $opts{options};
282 }
283  
284 0 return $self;
285 } ## end sub new
286  
287 =head2 init
288  
289 Initiates the backend. This will attempt to drop the rule number and table
290 prior to re-adding them.
291  
292 No arguments are taken.
293  
294 May called a second time, it will error.
295  
296 $backend->init;
297  
298 =cut
299  
300 sub init {
301 0 ▶ my ( $self, %opts ) = @_;
Subroutine init: not called CC=13 SCAR=52.0
Pod documented
302  
303 0 $self->errorblank;
304  
305 0 ▶ if ( $self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"inited"}) 0 0 0
306 0 $self->{error} = 18;
307 0 $self->{errorString} = 'backend has already been inited';
308 0 $self->warn;
309 }
310  
311 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
312 0 $self->{frontend_obj}->{test_data} = {};
313 }
314  
315 0 my $ports;
316 0 ▶ if ( defined( $self->{ports}[0] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"ports"}[0]) 0 0 0
317 0 $ports = join( ',', @{ $self->{ports} } );
318 }
319  
320 0 my @protocols;
321 0 ▶ if ( defined( $self->{protocols}[0] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'protocols'}[0]) { } 0 0 0
322 0 push( @protocols, @{ $self->{protocols} } );
323 } else {
324 0 push( @protocols, 'tcp', 'udp', 'icmp', 'icmp6' );
325 }
326  
327 0 my $pfctl = 'pfctl -a ' . $self->{prefix} . '/' . $self->{name};
328  
329 0 my $table = $self->{prefix} . '_' . $self->{name};
330  
331 0 my @fail_okay_commands;
332 0 push( @fail_okay_commands, $pfctl . ' -t ' . $table . ' -T flush' );
333 0 push( @fail_okay_commands, $pfctl . ' -t ' . $table . ' -T kill' );
334 0 push( @fail_okay_commands, $pfctl . ' -F rules' );
335  
336 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
337 0 $self->{frontend_obj}->{test_data}{fail_okay_commands} = \@fail_okay_commands;
338 } else {
339 0 foreach my $item (@fail_okay_commands) {
340 0 my $output = `$item 2>&1`;
341 }
342 }
343  
344 0 my $rules_init = '';
345 0 foreach my $item (@protocols) {
346 0 my $new_line = 'block drop quick proto ' . $item . ' from <' . $table . '> to any';
347 0 my $to_add = '';
348 0 ▶ if ( defined( $self->{ports}[0] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'ports'}[0]) { } 0 0 0
349 0 foreach my $port ( @{ $self->{ports} } ) {
350 0 $to_add = $to_add . $new_line . ' port ' . $port . "\n";
351 }
352 } else {
353 0 $to_add = $new_line . "\n";
354 }
355 0 $rules_init = $rules_init . $to_add;
356 } ## end foreach my $item (@protocols)
357  
358 0 my @commands;
359 # add the table to create the command
360 0 push( @commands, "echo 'table <" . $table . "> persist counters' | " . $pfctl . ' -f-' );
361 0 push( @commands, "echo '" . $rules_init . "' | " . $pfctl . ' -f-' );
362  
363 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
364 0 $self->{frontend_obj}->{test_data}{commands} = \@commands;
365 } else {
366 0 foreach my $item (@fail_okay_commands) {
367 0 my $output = `$item 2>&1`;
368 0 ▶ if ( $? ne '0' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? ne 0) 0 0 0
369 0 $self->{error} = 22;
370 $self->{errorString}
371 0 = 'init failed. non-zero exit code for the dommand... "' . $item . '"... output... ' . $output;
372 0 $self->warn;
373 }
374 }
375 } ## end else [ if ( $self->{testing} ) ]
376  
377 0 $self->{inited} = 1;
378 } ## end sub init
379  
380 =head2 ban
381  
382 Bans the IP.
383  
384 $backend->ban(ban => $ip);
385  
386 =cut
387  
388 sub ban {
389 0 ▶ my ( $self, %opts ) = @_;
Subroutine ban: not called CC=12 SCAR=50.5
Pod documented
390  
391 0 $self->errorblank;
392  
393 0 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 0 0
394 0 $self->{error} = 1;
395 0 $self->{errorString} = 'backend has not been inited';
396 0 $self->warn;
397 0 return;
398 }
399  
400 0 ▶ if ( !defined( $opts{ban} ) ) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $opts{'ban'}) { } 0 0 0
elsif (ref $opts{'ban'} ne '') { } 0 0 0
elsif (not $opts{'ban'} =~ /$Net::Firewall::BlockerHelper::backends::pf::IPv4_re/ || $opts{'ban'} =~ /$Net::Firewall::BlockerHelper::backends::pf::IPv6_re/) { } 0 0 0
401 0 $self->{error} = 9;
402 0 $self->{errorString} = 'Nothing specified for the value ban';
403 0 $self->warn;
404 0 return;
405 } elsif ( ref( $opts{ban} ) ne '' ) {
406 0 $self->{error} = 10;
407 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
408 0 $self->warn;
409 0 return;
410 ▶ } elsif ( $opts{ban} !~ /$IPv4_re/
Condition0%0/3
$opts{'ban'} =~ /$Net::Firewall::BlockerHelper::backends::pf::IPv4_re/ || $opts{'ban'} =~ /$Net::Firewall::BlockerHelper::backends::pf::IPv6_re/
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts{'ban'} =~ /$Net::Firewall::BlockerHelper::backends::pf::IPv4_re/ || $opts{'ban'} =~ /$Net::Firewall::BlockerHelper::backends::pf::IPv6_re/
$opts{'ban'} =~ /$Net::Firewall::BlockerHelper::backends::pf::IPv4_re/ $opts{'ban'} =~ /$Net::Firewall::BlockerHelper::backends::pf::IPv6_re/
Truth table0%0/3
A || B
A= $opts{'ban'} =~ /$Net::Firewall::BlockerHelper::backends::pf::IPv4_re/B= $opts{'ban'} =~ /$Net::Firewall::BlockerHelper::backends::pf::IPv6_re/
ABresult
1X1
011
000
411 && $opts{ban} !~ /$IPv6_re/ )
412 {
413 0 $self->{error} = 10;
414 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 IP';
415 0 $self->warn;
416 0 return;
417 }
418  
419 0 ▶ if ( $self->{banned}{ $opts{ban} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"banned"}{$opts{"ban"}}) 0 0 0
420 0 $self->{frontend_obj}->{test_data} = 'already banned';
421 0 return;
422 }
423  
424 my $command
425 = 'pfctl -a '
426 . $self->{prefix} . '/'
427 . $self->{name} . ' -T '
428 . $self->{prefix} . '_'
429 . $self->{name} . ' add '
430 0 . $opts{ban};
431  
432 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
433 0 $self->{frontend_obj}->{test_data} = $command;
434 } else {
435 0 my $output = `$command 2>&1`;
436 0 ▶ if ( $? ne '0' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? ne 0) 0 0 0
437 0 $self->{error} = 13;
438 $self->{errorString}
439 0 = 'ban failed. non-zero exit code for the command... "' . $command . '"... output... ' . $output;
440 0 $self->warn;
441 }
442 }
443  
444 0 ▶ if ( $self->{options}{kill} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"options"}{"kill"}) 0 0 0
445 0 ▶ if ( defined( $self->{ports} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'ports'}) { } 0 0 0
446 0 foreach my $port ( @{ $self->{ports} } ) {
447 $command
448 = 'pfctl -s state -vv 2> /dev/null | grep -E \'<*->*|id:\' | paste - - | grep -E ":'
449 . $port
450 . ' " | grep -E "[[ ]'
451 . $opts{ban}
452 0 . ']*:" | sed "s/.*[\ \t]id:[\ \t]//" | cut -d " " -f 1 | paste -s - | xargs -n 1 pfctl -k id -k ';
453  
454 0 my $output = `$command 2>&1`;
455 } ## end foreach my $port ( @{ $self->{ports} } )
456 } else {
457 0 $command = 'pfctl -k ' . $opts{ban};
458 0 my $output = `$command 2>&1`;
459 }
460 } ## end if ( $self->{options}{kill} )
461  
462 0 $self->{banned}{ $opts{ban} } = 1;
463 } ## end sub ban
464  
465 =head2 unban
466  
467 Unbans the an IP.
468  
469 $backend->ban(ban => $ip);
470  
471 =cut
472  
473 sub unban {
474 0 ▶ my ( $self, %opts ) = @_;
Subroutine unban: not called CC=9 SCAR=45.0
Pod documented
475  
476 0 $self->errorblank;
477  
478 0 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 0 0
479 0 $self->{error} = 1;
480 0 $self->{errorString} = 'backend has not been inited';
481 0 $self->warn;
482 0 return;
483 }
484  
485 0 ▶ if ( !defined( $opts{ban} ) ) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $opts{'ban'}) { } 0 0 0
elsif (ref $opts{'ban'} ne '') { } 0 0 0
elsif (not $opts{'ban'} =~ /$Net::Firewall::BlockerHelper::backends::pf::IPv4_re/ || $opts{'ban'} =~ /$Net::Firewall::BlockerHelper::backends::pf::IPv6_re/) { } 0 0 0
486 0 $self->{error} = 9;
487 0 $self->{errorString} = 'Nothing specified for the value ban';
488 0 $self->warn;
489 0 return;
490 } elsif ( ref( $opts{ban} ) ne '' ) {
491 0 $self->{error} = 10;
492 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
493 0 $self->warn;
494 0 return;
495 ▶ } elsif ( $opts{ban} !~ /$IPv4_re/
Condition0%0/3
$opts{'ban'} =~ /$Net::Firewall::BlockerHelper::backends::pf::IPv4_re/ || $opts{'ban'} =~ /$Net::Firewall::BlockerHelper::backends::pf::IPv6_re/
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts{'ban'} =~ /$Net::Firewall::BlockerHelper::backends::pf::IPv4_re/ || $opts{'ban'} =~ /$Net::Firewall::BlockerHelper::backends::pf::IPv6_re/
$opts{'ban'} =~ /$Net::Firewall::BlockerHelper::backends::pf::IPv4_re/ $opts{'ban'} =~ /$Net::Firewall::BlockerHelper::backends::pf::IPv6_re/
Truth table0%0/3
A || B
A= $opts{'ban'} =~ /$Net::Firewall::BlockerHelper::backends::pf::IPv4_re/B= $opts{'ban'} =~ /$Net::Firewall::BlockerHelper::backends::pf::IPv6_re/
ABresult
1X1
011
000
496 && $opts{ban} !~ /$IPv6_re/ )
497 {
498 0 $self->{error} = 10;
499 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 IP';
500 0 $self->warn;
501 0 return;
502 }
503  
504 0 ▶ if ( !$self->{banned}{ $opts{ban} } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"banned"}{$opts{"ban"}}) 0 0 0
505 0 $self->{frontend_obj}->{test_data} = 'not banned';
506 0 return;
507 }
508  
509 my $command
510 = 'pfctl -a '
511 . $self->{prefix} . '/'
512 . $self->{name} . ' -T '
513 . $self->{prefix} . '_'
514 . $self->{name}
515 . ' delete '
516 0 . $opts{ban};
517  
518 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
519 0 $self->{frontend_obj}->{test_data} = $command;
520 } else {
521 0 my $output = `$command 2>&1`;
522 0 ▶ if ( $? ne '0' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? ne 0) 0 0 0
523 0 $self->{error} = 14;
524 $self->{errorString}
525 0 = 'unban failed. non-zero exit code for the command... "' . $command . '"... output... ' . $output;
526 0 $self->warn;
527 }
528 }
529  
530 0 delete( $self->{banned}{ $opts{ban} } );
531 } ## end sub unban
532  
533 =head2 list
534  
535 List banned IPs.
536  
537 my @banned = $backend->list;
538  
539 =cut
540  
541 sub list {
542 0 ▶ my ( $self, %opts ) = @_;
Subroutine list: not called CC=1 SCAR=6.9
Pod documented
543  
544 0 $self->errorblank;
545  
546 0 $self->{frontend_obj}->{test_data} = 'list';
547  
548 0 return keys( %{ $self->{banned} } );
549 }
550  
551 =head2 re_init
552  
553 Tells the backend to re-init it's self.
554  
555 This will call teardown and init again. After that it will
556 re-added all previously added bans.
557  
558 Will error if already inited.
559  
560 $backend->re_init;
561  
562 =cut
563  
564 sub re_init {
565 0 ▶ my ( $self, %opts ) = @_;
Subroutine re_init: not called CC=6 SCAR=37.4
Pod documented
566  
567 0 $self->errorblank;
568  
569 0 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 0 0
570 0 $self->{error} = 1;
571 0 $self->{errorString} = 'backend has not been inited';
572 0 $self->warn;
573 0 return;
574 }
575  
576 0 $self->teardown;
577 0 $self->init;
578  
579 0 my @to_ban = keys( %{ $self->{banned} } );
580  
581 0 my @re_init_test_data;
582 0 foreach my $item (@to_ban) {
583 my $command
584 = 'pfctl -a '
585 . $self->{prefix} . '/'
586 . $self->{name} . ' -T '
587 . $self->{prefix} . '_'
588 0 . $self->{name} . ' add '
589 . $item;
590  
591 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
592 0 push( @re_init_test_data, $command );
593 } else {
594 0 my $output = `$command 2>&1`;
595 0 ▶ if ( $? ne '0' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? ne 0) 0 0 0
596 0 $self->{error} = 13;
597 $self->{errorString}
598 0 = 'ban failed. non-zero exit code for the command... "' . $command . '"... output... ' . $output;
599 0 $self->warn;
600 }
601 }
602 } ## end foreach my $item (@to_ban)
603  
604 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
605 0 $self->{frontend_obj}->{test_data} = \@re_init_test_data;
606 }
607  
608 0 $self->{inited} = 1;
609 } ## end sub re_init
610  
611 =head2 teardown
612  
613 Tears down the setup for the backend.
614  
615 This will delete the table as well as the firewall rule.
616  
617 If called prior to calling init, this will error. It won't check if it has been
618 inited or not.
619  
620 $backend->teardown;
621  
622 =cut
623  
624 sub teardown {
625 0 ▶ my ( $self, %opts ) = @_;
Subroutine teardown: not called CC=4 SCAR=30.0
Pod documented
626  
627 0 $self->errorblank;
628  
629 0 $self->{inited} = 0;
630  
631 0 $self->{frontend_obj}->{test_data} = {};
632  
633 0 my $pfctl = 'pfctl -a ' . $self->{prefix} . '/' . $self->{name};
634  
635 0 my $table = $self->{prefix} . '_' . $self->{name};
636  
637 0 my @commands;
638 0 push( @commands, $pfctl . ' -t ' . $table . ' -T flush' );
639 0 push( @commands, $pfctl . ' -t ' . $table . ' -T kill' );
640 0 push( @commands, $pfctl . ' -F rules' );
641  
642 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
643 0 $self->{frontend_obj}->{test_data} = \@commands;
644 } else {
645 0 foreach my $item (@commands) {
646 0 my $output = `$item 2>&1`;
647 0 ▶ if ( $? ne '0' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? ne 0) 0 0 0
648 0 $self->{error} = 17;
649 $self->{errorString}
650 0 = 'teardown failed. non-zero exit code for the command... "' . $item . '"... output... ' . $output;
651 0 $self->warn;
652 }
653 }
654 } ## end else [ if ( $self->{testing} ) ]
655 } ## end sub teardown
656  
657 =head1 ERROR CODES / FLAGS
658  
659 Error handling is provided by L<Error::Helper>. All
660 errors are considered fatal.
661  
662 =head2 1, notInited
663  
664 Backend has not been initted yet.
665  
666 =head2 2, invalidPortSpecified
667  
668 Port is either not a positive int or a name that can be resolved by getservbyname.
669  
670 =head2 3, portsNotArray
671  
672 The data passed to new for ports is not an array.
673  
674 =head2 4, protocolsNotArray
675  
676 The data passed to new for protocols is not an array.
677  
678 =head2 5, invalidPortSpecified
679  
680 Port is either not a positive int or a name that can be resolved by getservbyname.
681  
682 =head2 6, invalidPrefixSpecified
683  
684 The specified prefix did not match /^[a-zA-Z0-9]+$/.
685  
686 =head2 7, invalidName
687  
688 The name is either undef or does not match /^[a-zA-Z0-9\-]+$/.
689  
690 =head2 8, optionsNotHash
691  
692 The item passed to new for options is not a hash.
693  
694 =head2 9, noBanItem
695  
696 No IP specified to ban.
697  
698 =head2 10, banItemNotIP
699  
700 The item to ban is not an IP. Either wrong ref type or regexp
701 test using L<Regexp::IPv4> and L<Regexp::IPv6> failed.
702  
703 =head2 11, invalidBackend
704  
705 The specified backend failed to pass a basic sanity check of making sure it
706 matches the regexp /^[a-zA-Z0-9\_]+$/.
707  
708 =head2 12, backendInitError
709  
710 Failed to init the backend.
711  
712 =head2 13, banFailed
713  
714 Failed to ban the item.
715  
716 =head2 14, unbanFailed
717  
718 Failed to unban the item.
719  
720 =head2 15, listFailed
721  
722 Failed get a list of bans.
723  
724 =head2 16, reInitFailed
725  
726 Failed to re_init the backend.
727  
728 =head2 17, teardownFailed
729  
730 Failed to teardown the backend.
731  
732 =head2 18, alreadyInited
733  
734 Backend has already been initiated.
735  
736 =head2 23, initFailed
737  
738 One of the required commands for init failed.
739  
740 =head1 AUTHOR
741  
742 Zane C. Bowers-Hadley, C<< <vvelox at vvelox.ent> >>
743  
744 =head1 BUGS
745  
746 Please report any bugs or feature requests to C<bug-net-firewall-blockerhelper at rt.cpan.org>, or through
747 the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Net-Firewall-BlockerHelper>. I will be notified, and then you'll
748 automatically be notified of progress on your bug as I make changes.
749  
750  
751  
752  
753 =head1 SUPPORT
754  
755 You can find documentation for this module with the perldoc command.
756  
757 perldoc Net::Firewall::BlockerHelper
758  
759  
760 You can also look for information at:
761  
762 =over 4
763  
764 =item * RT: CPAN's request tracker (report bugs here)
765  
766 L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=Net-Firewall-BlockerHelper>
767  
768 =item * Search CPAN
769  
770 L<https://metacpan.org/release/Net-Firewall-BlockerHelper>
771  
772 =back
773  
774  
775 =head1 ACKNOWLEDGEMENTS
776  
777  
778 =head1 LICENSE AND COPYRIGHT
779  
780 This software is Copyright (c) 2023 by Zane C. Bowers-Hadley.
781  
782 This is free software, licensed under:
783  
784 The GNU Lesser General Public License, Version 2.1, February 1999
785  
786  
787 =cut
788  
789 1 1; # End of Net::Firewall::BlockerHelper
« BlockerHelper/backends/dummy.pm ↑ Summary BlockerHelper.pm »