BlockerHelper/backends/hosts_deny.pm

filter: Statementstmt 53.2% 139 / 261 Branchbran 51.0% 50 / 98 Conditioncond 25.9% 7 / 27 MC/DCmcdc 0.0% 0 / 18 Subroutinesub 70.3% 19 / 27 Podpod 100% 13 / 13 totaltotal 51.3% 228 / 444 CC 61 SCAR 30.5
CC
61
Cov
51%
_valid_cidr
72.0
_apply
37.5
unban
27.0
CRAP 21.0
« BlockerHelper/backends/azure.pm ↑ Summary BlockerHelper/backends/cloud_armor.pm »
1 package Net::Firewall::BlockerHelper::backends::hosts_deny;
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 1 ▶use Fcntl qw(:flock);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 =head1 NAME
12  
13 Net::Firewall::BlockerHelper::backends::hosts_deny - TCP wrappers hosts.deny backend.
14  
15 =head1 VERSION
16  
17 Version 0.1.0
18  
19 =cut
20  
21 1 our $VERSION = '0.1.0';
22  
23 =head1 SYNOPSIS
24  
25 use Net::Firewall::BlockerHelper;
26  
27 my $fw_helper = Net::Firewall::BlockerHelper->new(
28 backend => 'hosts_deny',
29 name => 'sshd',
30 options => { daemon => 'sshd' },
31 );
32  
33 $fw_helper->init_backend;
34 $fw_helper->ban( ban => '1.2.3.4' );
35 $fw_helper->unban( ban => '1.2.3.4' );
36 $fw_helper->teardown;
37  
38 =head1 DESCRIPTION
39  
40 Blocks IPs via TCP wrappers by maintaining a block of entries in
41 C</etc/hosts.deny>. Only libwrap aware daemons (those linked against
42 libwrap or launched via tcpd) honor this; it is not a packet filter.
43  
44 The backend owns a single marked region of the file delimited by
45  
46 # BEGIN Net::Firewall::BlockerHelper <prefix>_<name>
47 ...
48 # END Net::Firewall::BlockerHelper <prefix>_<name>
49  
50 Everything outside that region is preserved untouched, so hand maintained
51 rules and other instances (with a different prefix/name) coexist. Each ban
52 is rendered as a C<< <daemon> : <ip> >> line, with IPv6 addresses bracketed,
53 C<< <daemon> : [<ip>] >>, as that is the only IPv6 form libwrap matches. No
54 reload is needed as libwrap reads the file on each connection.
55  
56 Updates are done under an exclusive flock(2) on C<< <file>.lock >>, which is
57 created if needed and left in place, so concurrent processes updating the
58 same file serialize rather than losing each others changes. The new contents
59 are written to a temp file in the same directory and renamed into place, so
60 the update is atomic and a partial file can never be seen. The mode of the
61 file being replaced is carried over.
62  
63 =head1 METHODS
64  
65 =head2 new
66  
67 Initiates the object. Not really meant to be used directly, but instead
68 called via L<Net::Firewall::BlockerHelper>.
69  
70 - options :: A hash of options. See below.
71 - name :: Required. Used, with prefix, to tag this instance's region.
72 - prefix :: Defaults to 'kur'. Combined with name for the region tag.
73  
74 The options hash accepts the following.
75  
76 - file :: Path to the hosts.deny file.
77 - Default :: /etc/hosts.deny
78  
79 - daemon :: The daemon_list portion of each rule. 'ALL' blocks the IP for
80 every libwrap daemon; a specific name (eg 'sshd') scopes it.
81 - Default :: ALL
82  
83 All errors are considered fatal, meaning if new fails it will die.
84  
85 =cut
86  
87 sub new {
88 2 ▶ my ( $blank, %opts ) = @_;
Subroutine new: called CC=10 SCAR=26.6
Pod documented
89  
90 2 my $self = {
91 perror => undef,
92 error => undef,
93 errorLine => undef,
94 errorFilename => undef,
95 errorString => "",
96 errorExtra => {
97 all_errors_fatal => 1,
98 # all_fatal is what Error::Helper 2.1.0 actually checks; all_errors_fatal
99 # is kept for the name documented in its POD
100 all_fatal => 1,
101 flags => {
102 1 => 'notInited',
103 7 => 'invalidName',
104 8 => 'optionsNotHash',
105 9 => 'noBanItem',
106 10 => 'banItemNotIP',
107 12 => 'backendInitError',
108 13 => 'banFailed',
109 14 => 'unbanFailed',
110 15 => 'listFailed',
111 16 => 'reInitFailed',
112 17 => 'teardownFailed',
113 18 => 'alreadyInited',
114 24 => 'checkFailed',
115 25 => 'flushFailed',
116 31 => 'fileWriteFailed',
117 32 => 'banCidrFailed',
118 33 => 'unbanCidrFailed',
119 34 => 'cidrItemNotCidr',
120 35 => 'cidrNotSupported',
121 36 => 'listCidrFailed',
122 },
123 fatal_flags => {},
124 perror_not_fatal => 0,
125 },
126 options => {},
127 ports => [],
128 protocols => [],
129 testing => undef,
130 test_data => undef,
131 test_outside => '',
132 prefix => 'kur',
133 name => undef,
134 frontend_obj => undef,
135 inited => 0,
136 banned => {},
137 cidr_supported => 0,
138 };
139 2 bless $self;
140  
141 2 ▶ if ( defined( $opts{testing} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"testing"}) 1 1 2
142 1 $self->{testing} = $opts{testing};
143 }
144 2 ▶ if ( defined( $opts{frontend_obj} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"frontend_obj"}) 2 0 2
145 2 $self->{frontend_obj} = $opts{frontend_obj};
146 }
147 2 ▶ if ( defined( $opts{prefix} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"prefix"}) 2 0 2
148 2 $self->{prefix} = $opts{prefix};
149 }
150  
151 2 ▶ if ( !defined( $opts{name} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $opts{"name"}) 0 2 2
152 0 $self->{perror} = 1;
153 0 $self->{error} = 7;
154 0 $self->{errorString} = 'name is undef';
155 0 $self->warn;
156 }
157 2 $self->{name} = $opts{name};
158  
159 2 ▶ if ( defined( $opts{options} ) && ref( $opts{options} ) ne 'HASH' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $opts{'options'} and ref $opts{'options'} ne 'HASH') { } 0 2 2
elsif (defined $opts{'options'}) { } 2 0 2
Condition33%1/3
defined $opts{"options"} and ref $opts{"options"} ne "HASH"
!ll&&!rl&&r
020
MC/DC0%0/2
defined $opts{"options"} and ref $opts{"options"} ne "HASH"
defined $opts{"options"} ref $opts{"options"} ne "HASH"
Truth table33%1/3
A and B
A= defined $opts{"options"}B= ref $opts{"options"} ne "HASH"
ABresult
0X0
100
111
160 0 $self->{perror} = 1;
161 0 $self->{error} = 8;
162 0 $self->{errorString} = 'ref for options is "' . ref( $opts{options} ) . '" and not HASH';
163 0 $self->warn;
164 } elsif ( defined( $opts{options} ) ) {
165 2 $self->{options} = $opts{options};
166 }
167  
168 2 ▶ $self->{options}{file} = '/etc/hosts.deny' if ( !defined( $self->{options}{file} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"options"}{"file"} 1 1 2
169 2 ▶ $self->{options}{daemon} = 'ALL' if ( !defined( $self->{options}{daemon} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"options"}{"daemon"} 1 1 2
170  
171 2 return $self;
172 } ## end sub new
173  
174 # Internal helper. Returns the begin and end marker lines for this instance.
175 sub _markers {
176 16 ▶ my ($self) = @_;
Subroutine _markers: called CC=1 SCAR=0.0
177  
178 16 my $tag = 'Net::Firewall::BlockerHelper ' . $self->{prefix} . '_' . $self->{name};
179 16 return ( '# BEGIN ' . $tag, '# END ' . $tag );
180 }
181  
182 # Internal helper. Renders the hosts.deny line for the passed IP. IPv6
183 # addresses are bracketed as libwrap only matches the [ip] form for those;
184 # bare IPv6 patterns are silently never matched.
185 sub _render_entry {
186 12 ▶ my ( $self, $ip ) = @_;
Subroutine _render_entry: called CC=2 SCAR=6.9
187  
188 12 ▶ my $addr = ( $ip =~ /:/ ) ? '[' . $ip . ']' : $ip;
Branch: 1 branch
BranchTrueFalseTotal
$ip =~ /:/ ? : 1 11 12
189 12 return $self->{options}{daemon} . ' : ' . $addr;
190 }
191  
192 # Internal helper. Builds this instance's marked region from the current ban
193 # list. Returns an empty string when there is nothing banned so no stray
194 # markers are left in the file.
195 sub _render_block {
196 8 ▶ my ($self) = @_;
Subroutine _render_block: called CC=3 SCAR=11.0
197  
198 8 my @ips = sort( keys( %{ $self->{banned} } ) );
199 8 ▶ return '' if ( !@ips );
Branch: 1 branch
BranchTrueFalseTotal
unless @ips 3 5 8
200  
201 5 my ( $begin, $end ) = $self->_markers;
202 5 my @lines = ($begin);
203 5 foreach my $ip (@ips) {
204 7 push( @lines, $self->_render_entry($ip) );
205 }
206 5 push( @lines, $end );
207  
208 5 return join( "\n", @lines ) . "\n";
209 } ## end sub _render_block
210  
211 # Internal helper. Given the full file contents, returns them with this
212 # instance's marked region (and only this instance's) removed.
213 sub _strip_block {
214 8 ▶ my ( $self, $content ) = @_;
Subroutine _strip_block: called CC=1 SCAR=0.0
215  
216 8 my ( $begin, $end ) = $self->_markers;
217 # \Q..\E so the :: in the tag is treated literally rather than as regex
218 8 $content =~ s/^\Q$begin\E\n.*?^\Q$end\E\n//ms;
219  
220 8 return $content;
221 } ## end sub _strip_block
222  
223 # Internal helper. Returns the current file contents, or the simulated outside
224 # content when testing, or '' when the file is absent.
225 sub _read {
226 11 ▶ my ($self) = @_;
Subroutine _read: called CC=5 SCAR=16.6
227  
228 11 ▶ return $self->{test_outside} if ( $self->{testing} );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"testing"} 3 8 11
229  
230 8 ▶ return '' if ( !-e $self->{options}{file} );
Branch: 1 branch
BranchTrueFalseTotal
unless -e $self->{"options"}{"file"} 0 8 8
231  
232 8 my $fh;
233 8 ▶ if ( !open( $fh, '<', $self->{options}{file} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (open $fh, "<", $self->{"options"}{"file"}) 0 8 8
234 0 return '';
235 }
236 8 local $/ = undef;
237 8 my $content = <$fh>;
238 8 close($fh);
239  
240 8 ▶ return defined($content) ? $content : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $content ? : 8 0 8
241 } ## end sub _read
242  
243 # Internal helper. Rewrites the file with this instance's region refreshed from
244 # the current ban list, preserving all other content. An exclusive lock on
245 # <file>.lock is held across the read-modify-write so concurrent processes
246 # serialize rather than losing each others changes, and the new contents are
247 # written to a temp file that is renamed into place so a partial file can
248 # never be seen. Failures are raised using the passed error code, so they
249 # surface as the operation that triggered the write, such as banFailed. In
250 # testing mode it records the rendered file in test_data instead.
251 sub _apply {
252 8 ▶ my ( $self, $error_flag ) = @_;
Subroutine _apply: called CC=15 SCAR=36.2
253  
254 8 ▶ $error_flag = 31 if ( !defined($error_flag) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $error_flag 0 8 8
255  
256 8 my $file = $self->{options}{file};
257  
258 8 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 3 5 8
259 3 my $outside = $self->_strip_block( $self->_read );
260 3 my $block = $self->_render_block;
261 3 ▶ $outside .= "\n" if ( $outside ne '' && $outside !~ /\n\z/ );
Branch: 1 branch
BranchTrueFalseTotal
if $outside ne "" and not $outside =~ /\n\z/ 0 3 3
Condition33%1/3
$outside ne "" and not $outside =~ /\n\z/
!ll&&!rl&&r
300
MC/DC0%0/2
$outside ne "" and not $outside =~ /\n\z/
$outside ne "" not $outside =~ /\n\z/
Truth table33%1/3
A and not(B)
A= $outside ne ""B= not $outside =~ /\n\z/
ABresult
0X0
100
111
262 $self->{frontend_obj}->{test_data} = {
263 3 file => $file,
264 block => $block,
265 content => $outside . $block,
266 };
267 3 return;
268 }
269  
270 # hold an exclusive lock across the read-modify-write so concurrent
271 # processes updating the same file serialize rather than clobbering
272 5 my $lock_fh;
273 5 ▶ if ( !open( $lock_fh, '>>', $file . '.lock' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (open $lock_fh, ">>", $file . '.lock') 0 5 5
274 0 $self->{error} = $error_flag;
275 0 $self->{errorString} = 'could not open the lock file "' . $file . '.lock"... ' . $!;
276 0 $self->warn;
277 0 return;
278 }
279 5 ▶ if ( !flock( $lock_fh, LOCK_EX ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (flock $lock_fh, 2) 0 5 5
280 0 $self->{error} = $error_flag;
281 0 $self->{errorString} = 'could not get an exclusive lock on "' . $file . '.lock"... ' . $!;
282 0 $self->warn;
283 0 return;
284 }
285  
286 # the read happens under the lock so the modify is based on what is
287 # actually current
288 5 my $current = $self->_read;
289 5 my $outside = $self->_strip_block($current);
290 5 my $block = $self->_render_block;
291  
292 # make sure the preserved content ends with a newline so our block does
293 # not get glued onto its last line; beyond that it is left byte for byte
294 # as is
295 5 ▶ $outside .= "\n" if ( $outside ne '' && $outside !~ /\n\z/ );
Branch: 1 branch
BranchTrueFalseTotal
if $outside ne "" and not $outside =~ /\n\z/ 0 5 5
Condition33%1/3
$outside ne "" and not $outside =~ /\n\z/
!ll&&!rl&&r
050
MC/DC0%0/2
$outside ne "" and not $outside =~ /\n\z/
$outside ne "" not $outside =~ /\n\z/
Truth table33%1/3
A and not(B)
A= $outside ne ""B= not $outside =~ /\n\z/
ABresult
0X0
100
111
296 5 my $content = $outside . $block;
297  
298 # nothing would change, so leave the file untouched
299 5 ▶ if ( $content eq $current ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content eq $current) 1 4 5
300 1 close($lock_fh);
301 1 return;
302 }
303  
304 # write the new contents to a temp file in the same directory and rename
305 # it into place, making the change atomic; a reader or a crash mid write
306 # can never result in a partial file being seen or left
307 4 my $tmp = $file . '.tmp.' . $$;
308 4 my $fh;
309 4 ▶ if ( !open( $fh, '>', $tmp ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (open $fh, ">", $tmp) 0 4 4
310 0 $self->{error} = $error_flag;
311 0 $self->{errorString} = 'could not open "' . $tmp . '" for writing... ' . $!;
312 0 $self->warn;
313 0 return;
314 }
315 4 ▶ if ( !print( $fh $content ) or !close($fh) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (print $fh $content and close $fh) 0 4 4
Condition33%1/3
print $fh $content and close $fh
!ll&&!rl&&r
004
MC/DC0%0/2
print $fh $content and close $fh
print $fh $content close $fh
Truth table33%1/3
A and B
A= print $fh $contentB= close $fh
ABresult
0X0
100
111
316 0 my $save_err = $!;
317 0 unlink($tmp);
318 0 $self->{error} = $error_flag;
319 0 $self->{errorString} = 'failed writing "' . $tmp . '"... ' . $save_err;
320 0 $self->warn;
321 0 return;
322 }
323  
324 # carry the mode of the file being replaced over to the new one
325 4 ▶ if ( -e $file ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $file) 4 0 4
326 4 chmod( ( stat($file) )[2] & 07777, $tmp );
327 }
328  
329 4 ▶ if ( !rename( $tmp, $file ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (rename $tmp, $file) 0 4 4
330 0 my $save_err = $!;
331 0 unlink($tmp);
332 0 $self->{error} = $error_flag;
333 0 $self->{errorString} = 'could not rename "' . $tmp . '" into place as "' . $file . '"... ' . $save_err;
334 0 $self->warn;
335 0 return;
336 }
337  
338 4 close($lock_fh);
339  
340 4 return;
341 } ## end sub _apply
342  
343 =head2 init
344  
345 Initiates the backend. Removes any stale region for this instance from the
346 file. The markers are only written when there is something banned, so with
347 no bans nothing is added.
348  
349 =cut
350  
351 sub init {
352 2 ▶ my ( $self, %opts ) = @_;
Subroutine init: called CC=2 SCAR=8.1
Pod documented
353  
354 2 $self->errorblank;
355  
356 2 ▶ if ( $self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"inited"}) 0 2 2
357 0 $self->{error} = 18;
358 0 $self->{errorString} = 'backend has already been inited';
359 0 $self->warn;
360 }
361  
362 2 $self->_apply(12);
363  
364 2 $self->{inited} = 1;
365 } ## end sub init
366  
367 =head2 ban
368  
369 Bans an IP. The value of ban is validated as being a IPv4 or IPv6 address
370 and lowercased, then added to the ban list. This instance's marked region in
371 the hosts.deny file is rewritten with a C<< <daemon> : <ip> >> line for each
372 banned IP; content outside the region is preserved. Banning an already
373 banned IP is a noop.
374  
375 $fw_helper->ban( ban => $ip );
376  
377 =cut
378  
379 sub ban {
380 4 ▶ my ( $self, %opts ) = @_;
Subroutine ban: called CC=8 SCAR=32.2
Pod documented
381  
382 4 $self->errorblank;
383  
384 4 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 4 4
385 0 $self->{error} = 1;
386 0 $self->{errorString} = 'backend has not been inited';
387 0 $self->warn;
388 0 return;
389 }
390  
391 4 ▶ if ( !defined( $opts{ban} ) ) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $opts{'ban'}) { } 0 4 4
elsif (ref $opts{'ban'} ne '') { } 0 4 4
elsif (not $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::hosts_deny::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::hosts_deny::IPv6_re\z/) { } 0 4 4
392 0 $self->{error} = 9;
393 0 $self->{errorString} = 'Nothing specified for the value ban';
394 0 $self->warn;
395 0 return;
396 } elsif ( ref( $opts{ban} ) ne '' ) {
397 0 $self->{error} = 10;
398 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
399 0 $self->warn;
400 0 return;
401 ▶ } elsif ( $opts{ban} !~ /\A$IPv4_re\z/
Condition66%2/3
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::hosts_deny::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::hosts_deny::IPv6_re\z/
l!l&&r!l&&!r
310
MC/DC0%0/2
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::hosts_deny::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::hosts_deny::IPv6_re\z/
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::hosts_deny::IPv4_re\z/ $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::hosts_deny::IPv6_re\z/
Truth table66%2/3
A || B
A= $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::hosts_deny::IPv4_re\z/B= $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::hosts_deny::IPv6_re\z/
ABresult
1X1
011
000
402 && $opts{ban} !~ /\A$IPv6_re\z/ )
403 {
404 0 $self->{error} = 10;
405 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 IP';
406 0 $self->warn;
407 0 return;
408 }
409  
410 # lowercase so the same IPv6 IP in differing cases can't result in duplicate entries
411 4 $opts{ban} = lc( $opts{ban} );
412  
413 4 ▶ if ( $self->{banned}{ $opts{ban} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"banned"}{$opts{"ban"}}) 0 4 4
414 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
415 0 $self->{frontend_obj}->{test_data} = 'already banned';
416 }
417 0 return;
418 }
419  
420 4 $self->{banned}{ $opts{ban} } = 1;
421 4 $self->_apply(13);
422 } ## end sub ban
423  
424 =head2 unban
425  
426 Unbans an IP. The value of ban is validated as being a IPv4 or IPv6 address
427 and lowercased, then removed from the ban list. This instance's marked
428 region in the hosts.deny file is rewritten without it; if nothing remains
429 banned the region, markers included, is removed entirely. Unbanning an IP
430 that is not banned is a noop.
431  
432 $fw_helper->unban( ban => $ip );
433  
434 =cut
435  
436 sub unban {
437 1 ▶ my ( $self, %opts ) = @_;
Subroutine unban: called CC=8 SCAR=32.9
Pod documented
438  
439 1 $self->errorblank;
440  
441 1 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 1 1
442 0 $self->{error} = 1;
443 0 $self->{errorString} = 'backend has not been inited';
444 0 $self->warn;
445 0 return;
446 }
447  
448 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'} =~ /\A$Net::Firewall::BlockerHelper::backends::hosts_deny::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::hosts_deny::IPv6_re\z/) { } 0 1 1
449 0 $self->{error} = 9;
450 0 $self->{errorString} = 'Nothing specified for the value ban';
451 0 $self->warn;
452 0 return;
453 } elsif ( ref( $opts{ban} ) ne '' ) {
454 0 $self->{error} = 10;
455 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
456 0 $self->warn;
457 0 return;
458 ▶ } elsif ( $opts{ban} !~ /\A$IPv4_re\z/
Condition33%1/3
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::hosts_deny::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::hosts_deny::IPv6_re\z/
l!l&&r!l&&!r
100
MC/DC0%0/2
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::hosts_deny::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::hosts_deny::IPv6_re\z/
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::hosts_deny::IPv4_re\z/ $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::hosts_deny::IPv6_re\z/
Truth table33%1/3
A || B
A= $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::hosts_deny::IPv4_re\z/B= $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::hosts_deny::IPv6_re\z/
ABresult
1X1
011
000
459 && $opts{ban} !~ /\A$IPv6_re\z/ )
460 {
461 0 $self->{error} = 10;
462 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 IP';
463 0 $self->warn;
464 0 return;
465 }
466  
467 # lowercase so the same IPv6 IP in differing cases can't result in duplicate entries
468 1 $opts{ban} = lc( $opts{ban} );
469  
470 1 ▶ if ( !$self->{banned}{ $opts{ban} } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"banned"}{$opts{"ban"}}) 0 1 1
471 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
472 0 $self->{frontend_obj}->{test_data} = 'not banned';
473 }
474 0 return;
475 }
476  
477 1 delete( $self->{banned}{ $opts{ban} } );
478 1 $self->_apply(14);
479 } ## end sub unban
480  
481 =head2 list
482  
483 List banned IPs. Returns an array of the currently banned IPs from the
484 in-memory ban list; the hosts.deny file is not parsed.
485  
486 my @banned = $fw_helper->list;
487  
488 =cut
489  
490 sub list {
491 0 ▶ my ( $self, %opts ) = @_;
Subroutine list: not called CC=2 SCAR=17.9
Pod documented
492  
493 0 $self->errorblank;
494  
495 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
496 0 $self->{frontend_obj}->{test_data} = 'list';
497 }
498  
499 0 return keys( %{ $self->{banned} } );
500 }
501  
502 =head2 re_init
503  
504 Re-renders this instance's region from the retained ban list.
505  
506 =cut
507  
508 sub re_init {
509 0 ▶ my ( $self, %opts ) = @_;
Subroutine re_init: not called CC=2 SCAR=17.9
Pod documented
510  
511 0 $self->errorblank;
512  
513 0 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 0 0
514 0 $self->{error} = 1;
515 0 $self->{errorString} = 'backend has not been inited';
516 0 $self->warn;
517 0 return;
518 }
519  
520 0 $self->_apply(16);
521  
522 0 $self->{inited} = 1;
523 } ## end sub re_init
524  
525 =head2 teardown
526  
527 Removes this instance's region from the file, leaving everything else intact.
528  
529 =cut
530  
531 sub teardown {
532 1 ▶ my ( $self, %opts ) = @_;
Subroutine teardown: called CC=1 SCAR=0.0
Pod documented
533  
534 1 $self->errorblank;
535  
536 1 $self->{inited} = 0;
537  
538 # empty the ban list so _apply strips our region and writes nothing back
539 1 my %saved_banned = %{ $self->{banned} };
540 1 $self->{banned} = {};
541 1 $self->_apply(17);
542 1 $self->{banned} = \%saved_banned;
543 } ## end sub teardown
544  
545 =head2 stop
546  
547 Alias for L</teardown>.
548  
549 =cut
550  
551 sub stop {
552 0 ▶ my ( $self, %opts ) = @_;
Subroutine stop: not called CC=1 SCAR=6.9
Pod documented
553  
554 0 return $self->teardown(%opts);
555 }
556  
557 =head2 check
558  
559 Verifies our marked region is present in the file with a
560 C<< <daemon> : <ip> >> line for every banned IP. Returns 1 if healthy and 0
561 if not. With nothing banned there is nothing to verify and it reports
562 healthy.
563  
564 =cut
565  
566 sub check {
567 6 ▶ my ( $self, %opts ) = @_;
Subroutine check: called CC=6 SCAR=18.0
Pod documented
568  
569 6 $self->errorblank;
570  
571 6 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 2 4 6
572 2 $self->{frontend_obj}->{test_data} = 'check';
573 2 return 1;
574 }
575  
576 4 my @ips = keys( %{ $self->{banned} } );
577 4 ▶ return 1 if ( !@ips );
Branch: 1 branch
BranchTrueFalseTotal
unless @ips 1 3 4
578  
579 3 my $content = $self->_read;
580 3 my ( $begin, $end ) = $self->_markers;
581 3 ▶ return 0 if ( index( $content, $begin ) < 0 );
Branch: 1 branch
BranchTrueFalseTotal
if (index($content, $begin) == -1) 0 3 3
582  
583 3 foreach my $ip (@ips) {
584 5 ▶ return 0 if ( index( $content, $self->_render_entry($ip) ) < 0 );
Branch: 1 branch
BranchTrueFalseTotal
if (index($content, $self->_render_entry($ip)) == -1) 0 5 5
585 }
586  
587 3 return 1;
588 } ## end sub check
589  
590 =head2 flush
591  
592 Removes all bans at once by clearing the ban list and rewriting the file.
593  
594 =cut
595  
596 sub flush {
597 0 ▶ my ( $self, %opts ) = @_;
Subroutine flush: not called CC=2 SCAR=17.9
Pod documented
598  
599 0 $self->errorblank;
600  
601 0 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 0 0
602 0 $self->{error} = 1;
603 0 $self->{errorString} = 'backend has not been inited';
604 0 $self->warn;
605 0 return;
606 }
607  
608 0 $self->{banned} = {};
609 0 $self->_apply(25);
610 } ## end sub flush
611  
612 # Internal helper. Returns a true value if the passed scalar is a valid IPv4 or
613 # IPv6 CIDR range, that is an address followed by "/" and a prefix length that
614 # is within the range valid for its family (0 to 32 for IPv4, 0 to 128 for
615 # IPv6). Returns false otherwise.
616 sub _valid_cidr {
617 0 ▶ my ( $self, $cidr ) = @_;
Subroutine _valid_cidr: not called CC=8 SCAR=42.8
618  
619 0 ▶ return 0 if ( !defined($cidr) || ref($cidr) ne '' );
Branch: 1 branch
BranchTrueFalseTotal
if not defined $cidr or ref $cidr ne "" 0 0 0
Condition0%0/3
not defined $cidr or ref $cidr ne ""
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $cidr or ref $cidr ne ""
not defined $cidr ref $cidr ne ""
Truth table0%0/3
not(A) or B
A= not defined $cidrB= ref $cidr ne ""
ABresult
1X1
011
000
620  
621 0 ▶ if ( $cidr =~ m!\A(.+)/([0-9]{1,3})\z! ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cidr =~ m[\A(.+)/([0-9]{1,3})\z]) 0 0 0
622 0 my ( $addr, $prefix ) = ( $1, $2 );
623 0 ▶ return 1 if ( $addr =~ /\A$IPv4_re\z/ && $prefix <= 32 );
Branch: 1 branch
BranchTrueFalseTotal
if $addr =~ /\A$Net::Firewall::BlockerHelper::backends::hosts_deny::IPv4_re\z/ and $prefix <= 32 0 0 0
Condition0%0/3
$addr =~ /\A$Net::Firewall::BlockerHelper::backends::hosts_deny::IPv4_re\z/ and $prefix <= 32
!ll&&!rl&&r
000
MC/DC0%0/2
$addr =~ /\A$Net::Firewall::BlockerHelper::backends::hosts_deny::IPv4_re\z/ and $prefix <= 32
$addr =~ /\A$Net::Firewall::BlockerHelper::backends::hosts_deny::IPv4_re\z/ $prefix <= 32
Truth table0%0/3
A and B
A= $addr =~ /\A$Net::Firewall::BlockerHelper::backends::hosts_deny::IPv4_re\z/B= $prefix <= 32
ABresult
0X0
100
111
624 0 ▶ return 1 if ( $addr =~ /\A$IPv6_re\z/ && $prefix <= 128 );
Branch: 1 branch
BranchTrueFalseTotal
if $addr =~ /\A$Net::Firewall::BlockerHelper::backends::hosts_deny::IPv6_re\z/ and $prefix <= 128 0 0 0
Condition0%0/3
$addr =~ /\A$Net::Firewall::BlockerHelper::backends::hosts_deny::IPv6_re\z/ and $prefix <= 128
!ll&&!rl&&r
000
MC/DC0%0/2
$addr =~ /\A$Net::Firewall::BlockerHelper::backends::hosts_deny::IPv6_re\z/ and $prefix <= 128
$addr =~ /\A$Net::Firewall::BlockerHelper::backends::hosts_deny::IPv6_re\z/ $prefix <= 128
Truth table0%0/3
A and B
A= $addr =~ /\A$Net::Firewall::BlockerHelper::backends::hosts_deny::IPv6_re\z/B= $prefix <= 128
ABresult
0X0
100
111
625 }
626  
627 0 return 0;
628 } ## end sub _valid_cidr
629  
630 =head2 ban_cidr
631  
632 CIDR bans are not supported by this backend; this always sets the
633 cidrNotSupported error.
634  
635 $backend->ban_cidr(ban => '1.2.3.0/24');
636  
637 =cut
638  
639 sub ban_cidr {
640 0 ▶ my ( $self, %opts ) = @_;
Subroutine ban_cidr: not called CC=1 SCAR=6.9
Pod documented
641  
642 0 $self->errorblank;
643  
644 0 $self->{error} = 35;
645 0 $self->{errorString} = 'the ' . __PACKAGE__ . ' backend does not support CIDR bans';
646 0 $self->warn;
647  
648 0 return;
649 } ## end sub ban_cidr
650  
651 =head2 unban_cidr
652  
653 CIDR bans are not supported by this backend; this always sets the
654 cidrNotSupported error.
655  
656 $backend->unban_cidr(ban => '1.2.3.0/24');
657  
658 =cut
659  
660 sub unban_cidr {
661 0 ▶ my ( $self, %opts ) = @_;
Subroutine unban_cidr: not called CC=1 SCAR=6.9
Pod documented
662  
663 0 $self->errorblank;
664  
665 0 $self->{error} = 35;
666 0 $self->{errorString} = 'the ' . __PACKAGE__ . ' backend does not support CIDR bans';
667 0 $self->warn;
668  
669 0 return;
670 } ## end sub unban_cidr
671  
672 =head2 list_cidr
673  
674 CIDR bans are not supported by this backend, so this always returns an empty
675 list.
676  
677 my @banned_cidrs = $backend->list_cidr;
678  
679 =cut
680  
681 sub list_cidr {
682 0 ▶ my ( $self, %opts ) = @_;
Subroutine list_cidr: not called CC=1 SCAR=6.9
Pod documented
683  
684 0 $self->errorblank;
685  
686 0 return ();
687 }
688  
689 =head1 ERROR CODES / FLAGS
690  
691 Error handling is provided by L<Error::Helper>. All
692 errors are considered fatal.
693  
694 =head2 1, notInited
695  
696 The backend has not been inited yet.
697  
698 =head2 7, invalidName
699  
700 The name is undef.
701  
702 =head2 8, optionsNotHash
703  
704 The item passed to new for options is not a hash.
705  
706 =head2 9, noBanItem
707  
708 No IP specified to ban or unban.
709  
710 =head2 10, banItemNotIP
711  
712 The item to ban is not an IP. Either wrong ref type or regexp
713 test using L<Regexp::IPv4> and L<Regexp::IPv6> failed.
714  
715 =head2 12, backendInitError
716  
717 Failed to init the backend.
718  
719 =head2 13, banFailed
720  
721 Failed to ban the item.
722  
723 =head2 14, unbanFailed
724  
725 Failed to unban the item.
726  
727 =head2 15, listFailed
728  
729 Failed to get a list of bans.
730  
731 =head2 16, reInitFailed
732  
733 Failed to re_init the backend.
734  
735 =head2 17, teardownFailed
736  
737 Failed to teardown the backend.
738  
739 =head2 18, alreadyInited
740  
741 init called, but the backend has already been inited.
742  
743 =head2 24, checkFailed
744  
745 The backend check raised an error.
746  
747 =head2 25, flushFailed
748  
749 Failed to flush the bans.
750  
751 =head2 31, fileWriteFailed
752  
753 Failed to write the file. File write failures are normally raised using the
754 error code of the operation that triggered the write, such as banFailed;
755 this is the fallback if no operation code was passed internally.
756  
757 =head2 32, banCidrFailed
758  
759 Failed to ban the CIDR range.
760  
761 =head2 33, unbanCidrFailed
762  
763 Failed to unban the CIDR range.
764  
765 =head2 34, cidrItemNotCidr
766  
767 The item to ban is not a CIDR range. Either wrong ref type or it is not an
768 IPv4 or IPv6 address followed by a prefix length valid for its family.
769  
770 =head2 35, cidrNotSupported
771  
772 The backend does not support CIDR bans.
773  
774 =head2 36, listCidrFailed
775  
776 Failed to get a list of CIDR bans.
777  
778 =head1 AUTHOR
779  
780 Zane C. Bowers-Hadley, C<< <vvelox at vvelox.ent> >>
781  
782 =head1 LICENSE AND COPYRIGHT
783  
784 This software is Copyright (c) 2023 by Zane C. Bowers-Hadley.
785  
786 This is free software, licensed under:
787  
788 The GNU Lesser General Public License, Version 2.1, February 1999
789  
790 =cut
791  
792 1 1; # End of Net::Firewall::BlockerHelper::backends::hosts_deny
« BlockerHelper/backends/azure.pm ↑ Summary BlockerHelper/backends/cloud_armor.pm »