BlockerHelper/backends/npf.pm

filter: Statementstmt 50.8% 157 / 309 Branchbran 46.0% 59 / 128 Conditioncond 61.1% 22 / 36 MC/DCmcdc 31.8% 7 / 22 Subroutinesub 95.0% 19 / 20 Podpod 100% 13 / 13 totaltotal 52.4% 277 / 528 CC 82 SCAR 31.1
CC
82
Cov
50%
unban
41.3
ban_cidr
34.5
unban_cidr
34.5
CRAP 22.3
« BlockerHelper/backends/xdp.pm ↑ Summary BlockerHelper/backends/azure.pm »
1 package Net::Firewall::BlockerHelper::backends::npf;
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::npf - NetBSD npf backend for Net::Firewall::BlockerHelper.
13  
14 =head1 VERSION
15  
16 Version 0.1.0
17  
18 =cut
19  
20 1 our $VERSION = '0.1.0';
21  
22 =head1 SYNOPSIS
23  
24 use Net::Firewall::BlockerHelper;
25  
26 my $fw_helper = Net::Firewall::BlockerHelper->new(
27 backend => 'npf',
28 name => 'ssh',
29 options => { table => 'fail2ban' },
30 );
31  
32 $fw_helper->init_backend;
33 $fw_helper->ban(ban => '1.2.3.4');
34 $fw_helper->unban(ban => '1.2.3.4');
35 $fw_helper->teardown;
36  
37 =head1 DESCRIPTION
38  
39 Blocks IPs using L<npfctl(8)> on NetBSD by adding them to a npf table.
40  
41 Unlike the pf backend, npf tables and the rules referencing them can not be
42 created on the fly; the table and a block rule using it must already be
43 declared in C<npf.conf>, for example...
44  
45 table <fail2ban> type ipset
46 block in final from <fail2ban>
47  
48 Note that C<ipset> tables only hold single addresses; if CIDR bans are
49 going to be used, the table needs to be declared C<type lpm> instead.
50  
51 init and check verify the table exists. Ports and protocols are not
52 supported by this backend as they belong to the rule in C<npf.conf>;
53 specifying either is an error. teardown flushes the table (the table itself
54 can not be removed) while keeping the internal list of bans, so re_init can
55 re-add them.
56  
57 Requires C<npfctl> to be in the C<PATH> of the process, which must have
58 sufficient privileges to run it.
59  
60 =head1 METHODS
61  
62 =head2 new
63  
64 Initiates the the object.
65  
66 - options :: Backend specific options that will be passed to the backend unchecked
67 outside of making sure it is a hash ref if defined. See below for furhter info.
68 - Default :: {}
69  
70 - prefix :: Prefix to use. Must match the regex /^[a-zA-Z0-9]+$/
71 - default :: kur
72  
73 - name :: Name of this specific instance. This must be specified.
74 - default :: undef
75  
76 Ports and protocols are not supported by this backend and specifying either
77 is an error.
78  
79 The options hash accepts the following.
80  
81 - table :: The npf table name to use. Must be declared in npf.conf and
82 must match /^[a-zA-Z0-9_\-]+$/.
83 - Default :: <prefix>_<name>
84  
85 All errors are considered fatal, meaning if new fails it will die.
86  
87 =cut
88  
89 sub new {
90 5 ▶ my ( $blank, %opts ) = @_;
Subroutine new: called CC=20 SCAR=33.2
Pod documented
91  
92 5 my $self = {
93 perror => undef,
94 error => undef,
95 errorLine => undef,
96 errorFilename => undef,
97 errorString => "",
98 errorExtra => {
99 all_errors_fatal => 1,
100 # all_fatal is what Error::Helper 2.1.0 actually checks; all_errors_fatal
101 # is kept for the name documented in its POD
102 all_fatal => 1,
103 flags => {
104 1 => 'notInited',
105 6 => 'invalidPrefixSpecified',
106 7 => 'invalidName',
107 8 => 'optionsNotHash',
108 9 => 'noBanItem',
109 10 => 'banItemNotIP',
110 12 => 'backendInitError',
111 13 => 'banFailed',
112 14 => 'unbanFailed',
113 15 => 'listFailed',
114 16 => 'reInitFailed',
115 17 => 'teardownFailed',
116 18 => 'alreadyInited',
117 19 => 'tableInvalid',
118 23 => 'initFailed',
119 24 => 'checkFailed',
120 25 => 'flushFailed',
121 26 => 'portsNotSupported',
122 27 => 'protocolsNotSupported',
123 28 => 'banCidrFailed',
124 29 => 'unbanCidrFailed',
125 30 => 'cidrItemNotCidr',
126 31 => 'cidrNotSupported',
127 32 => 'listCidrFailed',
128 },
129 fatal_flags => {},
130 perror_not_fatal => 0,
131 },
132 options => {},
133 ports => [],
134 protocols => [],
135 testing => undef,
136 test_data => undef,
137 prefix => 'kur',
138 frontend_obj => undef,
139 inited => 0,
140 banned => {},
141 banned_cidr => {},
142 cidr_supported => 1,
143 };
144 5 bless $self;
145  
146 # the table and its rule live in npf.conf; ports and protocols belong there
147 5 ▶ if ( defined( $opts{ports} ) && ref( $opts{ports} ) eq 'ARRAY' && defined( $opts{ports}[0] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"ports"} and ref $opts{"ports"} eq "ARRAY" and defined $opts{"ports"}[0]) 1 4 5
Condition83%5/6
defined $opts{"ports"} and ref $opts{"ports"} eq "ARRAY" and defined $opts{"ports"}[0]
!ll&&!rl&&r
221
defined $opts{"ports"} and ref $opts{"ports"} eq "ARRAY"
!ll&&!rl&&r
203
MC/DC66%2/3
defined $opts{"ports"} and ref $opts{"ports"} eq "ARRAY" and defined $opts{"ports"}[0]
defined $opts{"ports"} ref $opts{"ports"} eq "ARRAY" defined $opts{"ports"}[0]
Truth table75%3/4
A and B and C
A= defined $opts{"ports"}B= ref $opts{"ports"} eq "ARRAY"C= defined $opts{"ports"}[0]
ABCresult
0XX0
10X0
1100
1111
148 1 $self->{perror} = 1;
149 1 $self->{error} = 26;
150 $self->{errorString}
151 1 = 'the npf backend does not support ports... ports belong to the rule referencing the table in npf.conf';
152 1 $self->warn;
153 }
154 4 ▶ if ( defined( $opts{protocols} ) && ref( $opts{protocols} ) eq 'ARRAY' && defined( $opts{protocols}[0] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"protocols"} and ref $opts{"protocols"} eq "ARRAY" and defined $opts{"protocols"}[0]) 1 3 4
Condition83%5/6
defined $opts{"protocols"} and ref $opts{"protocols"} eq "ARRAY" and defined $opts{"protocols"}[0]
!ll&&!rl&&r
121
defined $opts{"protocols"} and ref $opts{"protocols"} eq "ARRAY"
!ll&&!rl&&r
103
MC/DC66%2/3
defined $opts{"protocols"} and ref $opts{"protocols"} eq "ARRAY" and defined $opts{"protocols"}[0]
defined $opts{"protocols"} ref $opts{"protocols"} eq "ARRAY" defined $opts{"protocols"}[0]
Truth table75%3/4
A and B and C
A= defined $opts{"protocols"}B= ref $opts{"protocols"} eq "ARRAY"C= defined $opts{"protocols"}[0]
ABCresult
0XX0
10X0
1100
1111
155 1 $self->{perror} = 1;
156 1 $self->{error} = 27;
157 $self->{errorString}
158 1 = 'the npf backend does not support protocols... protocols belong to the rule referencing the table in npf.conf';
159 1 $self->warn;
160 }
161  
162 # make sure prefix is sane if defiend
163 3 ▶ 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 3 3
elsif (defined $opts{'prefix'}) { } 2 1 3
Condition66%2/3
defined $opts{"prefix"} and not $opts{"prefix"} =~ /^[a-zA-Z0-9]+$/
!ll&&!rl&&r
120
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 table66%2/3
A and not(B)
A= defined $opts{"prefix"}B= not $opts{"prefix"} =~ /^[a-zA-Z0-9]+$/
ABresult
0X0
100
111
164 0 $self->{perror} = 1;
165 0 $self->{error} = 6;
166 $self->{errorString}
167 0 = '"' . $opts{prefix} . '" is not a valid prefix as it does not match the regex /^[a-zA-Z0-9]+$/';
168 0 $self->warn;
169 } elsif ( defined( $opts{prefix} ) ) {
170 2 $self->{prefix} = $opts{prefix};
171 }
172  
173 # make sure we have a name and that it is valid
174 3 ▶ if ( !defined( $opts{name} ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $opts{'name'}) { } 0 3 3
elsif (not $opts{'name'} =~ /^[a-zA-Z0-9\-]+$/) { } 0 3 3
175 0 $self->{perror} = 1;
176 0 $self->{error} = 7;
177 0 $self->{errorString} = 'name is undef';
178 0 $self->warn;
179 } elsif ( $opts{name} !~ /^[a-zA-Z0-9\-]+$/ ) {
180 0 $self->{perror} = 1;
181 0 $self->{error} = 7;
182 0 $self->{errorString} = 'name set to "' . $opts{name} . '" which does not match the regexp /^[a-zA-Z0-9\-]+$/';
183 0 $self->warn;
184 }
185 3 $self->{name} = $opts{name};
186  
187 # used internally for testing
188 3 ▶ if ( defined( $opts{testing} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"testing"}) 2 1 3
189 2 $self->{testing} = $opts{testing};
190 }
191 3 ▶ if ( defined( $opts{frontend_obj} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"frontend_obj"}) 2 1 3
192 2 $self->{frontend_obj} = $opts{frontend_obj};
193 }
194  
195 3 ▶ if ( defined( $opts{options} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"options"}) 3 0 3
196 3 ▶ if ( ref( $opts{options} ) ne 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $opts{"options"} ne "HASH") 0 3 3
197 0 $self->{perror} = 1;
198 0 $self->{error} = 8;
199 0 $self->{errorString} = 'ref for options is "' . ref( $opts{options} ) . '" and not HASH';
200 0 $self->warn;
201 }
202 3 $self->{options} = $opts{options};
203  
204 3 ▶ if ( defined( $opts{options}{table} ) && $opts{options}{table} !~ /^[a-zA-Z0-9_\-]+$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"options"}{"table"} and not $opts{"options"}{"table"} =~ /^[a-zA-Z0-9_\-]+$/) 1 2 3
Condition100%3/3
defined $opts{"options"}{"table"} and not $opts{"options"}{"table"} =~ /^[a-zA-Z0-9_\-]+$/
!ll&&!rl&&r
111
MC/DC100%2/2
defined $opts{"options"}{"table"} and not $opts{"options"}{"table"} =~ /^[a-zA-Z0-9_\-]+$/
defined $opts{"options"}{"table"} not $opts{"options"}{"table"} =~ /^[a-zA-Z0-9_\-]+$/
Truth table100%3/3
A and not(B)
A= defined $opts{"options"}{"table"}B= not $opts{"options"}{"table"} =~ /^[a-zA-Z0-9_\-]+$/
ABresult
0X0
100
111
205 1 $self->{perror} = 1;
206 1 $self->{error} = 19;
207 $self->{errorString}
208 = '$opts{options}{table} is "'
209 . $opts{options}{table}
210 1 . '" and does not match /^[a-zA-Z0-9_\-]+$/';
211 1 $self->warn;
212 }
213 } ## end if ( defined( $opts{options} ) )
214  
215 2 ▶ if ( !defined( $self->{options}{table} ) && defined( $self->{name} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->{"options"}{"table"} and defined $self->{"name"}) 1 1 2
Condition66%2/3
not defined $self->{"options"}{"table"} and defined $self->{"name"}
!ll&&!rl&&r
101
MC/DC50%1/2
not defined $self->{"options"}{"table"} and defined $self->{"name"}
not defined $self->{"options"}{"table"} defined $self->{"name"}
Truth table66%2/3
not(A) and B
A= not defined $self->{"options"}{"table"}B= defined $self->{"name"}
ABresult
0X0
100
111
216 1 $self->{options}{table} = $self->{prefix} . '_' . $self->{name};
217 }
218  
219 2 return $self;
220 } ## end sub new
221  
222 =head2 init
223  
224 Initiates the backend. Verifies the table is declared and usable via
225 npfctl table list, as it can not be created on the fly.
226  
227 $backend->init;
228  
229 =cut
230  
231 sub init {
232 3 ▶ my ( $self, %opts ) = @_;
Subroutine init: called CC=5 SCAR=23.2
Pod documented
233  
234 3 $self->errorblank;
235  
236 3 ▶ if ( $self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"inited"}) 0 3 3
237 0 $self->{error} = 18;
238 0 $self->{errorString} = 'backend has already been inited';
239 0 $self->warn;
240 }
241  
242 3 my @commands = ( 'npfctl table ' . $self->{options}{table} . ' list' );
243  
244 3 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 3 0 3
245 3 $self->{frontend_obj}->{test_data} = { commands => \@commands };
246 } else {
247 0 foreach my $item (@commands) {
248 0 my $output = `$item 2>&1`;
249 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
250 0 $self->{error} = 23;
251 $self->{errorString}
252 0 = 'init failed. the table does not appear to be declared in npf.conf... the command "'
253 . $item
254 . '" exited non-zero... output... '
255 . $output;
256 0 $self->warn;
257 }
258 }
259 } ## end else [ if ( $self->{testing} ) ]
260  
261 3 $self->{inited} = 1;
262 } ## end sub init
263  
264 =head2 ban
265  
266 Bans an IP. The value of ban is validated as being a IPv4 or IPv6 address
267 and lowercased, then added to the configured npf table via
268 C<npfctl table ... add>. Banning an already banned IP is a noop.
269  
270 $backend->ban(ban => $ip);
271  
272 =cut
273  
274 sub ban {
275 4 ▶ my ( $self, %opts ) = @_;
Subroutine ban: called CC=10 SCAR=33.4
Pod documented
276  
277 4 $self->errorblank;
278  
279 4 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 4 4
280 0 $self->{error} = 1;
281 0 $self->{errorString} = 'backend has not been inited';
282 0 $self->warn;
283 0 return;
284 }
285  
286 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::npf::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::npf::IPv6_re\z/) { } 0 4 4
287 0 $self->{error} = 9;
288 0 $self->{errorString} = 'Nothing specified for the value ban';
289 0 $self->warn;
290 0 return;
291 } elsif ( ref( $opts{ban} ) ne '' ) {
292 0 $self->{error} = 10;
293 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
294 0 $self->warn;
295 0 return;
296 ▶ } elsif ( $opts{ban} !~ /\A$IPv4_re\z/
Condition66%2/3
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::npf::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::npf::IPv6_re\z/
l!l&&r!l&&!r
310
MC/DC0%0/2
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::npf::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::npf::IPv6_re\z/
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::npf::IPv4_re\z/ $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::npf::IPv6_re\z/
Truth table66%2/3
A || B
A= $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::npf::IPv4_re\z/B= $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::npf::IPv6_re\z/
ABresult
1X1
011
000
297 && $opts{ban} !~ /\A$IPv6_re\z/ )
298 {
299 0 $self->{error} = 10;
300 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 IP';
301 0 $self->warn;
302 0 return;
303 }
304  
305 # lowercase so the same IPv6 IP in differing cases can't result in duplicate entries
306 4 $opts{ban} = lc( $opts{ban} );
307  
308 4 ▶ if ( $self->{banned}{ $opts{ban} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"banned"}{$opts{"ban"}}) 1 3 4
309 1 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 1 0 1
310 1 $self->{frontend_obj}->{test_data} = 'already banned';
311 }
312 1 return;
313 }
314  
315 3 my $command = 'npfctl table ' . $self->{options}{table} . ' add ' . $opts{ban};
316  
317 3 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 3 0 3
318 3 $self->{frontend_obj}->{test_data} = [$command];
319 } else {
320 0 my $output = `$command 2>&1`;
321 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
322 0 $self->{error} = 13;
323 $self->{errorString}
324 0 = 'ban failed. non-zero exit code for the command... "' . $command . '"... output... ' . $output;
325 0 $self->warn;
326 }
327 }
328  
329 3 $self->{banned}{ $opts{ban} } = 1;
330 } ## end sub ban
331  
332 =head2 unban
333  
334 Unbans an IP. The value of ban is validated as being a IPv4 or IPv6 address
335 and lowercased, then removed from the configured npf table via
336 C<npfctl table ... rem>. Unbanning an IP that is not banned is a noop.
337  
338 $backend->unban(ban => $ip);
339  
340 =cut
341  
342 sub unban {
343 1 ▶ my ( $self, %opts ) = @_;
Subroutine unban: called CC=10 SCAR=37.2
Pod documented
344  
345 1 $self->errorblank;
346  
347 1 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 1 1
348 0 $self->{error} = 1;
349 0 $self->{errorString} = 'backend has not been inited';
350 0 $self->warn;
351 0 return;
352 }
353  
354 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::npf::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::npf::IPv6_re\z/) { } 0 1 1
355 0 $self->{error} = 9;
356 0 $self->{errorString} = 'Nothing specified for the value ban';
357 0 $self->warn;
358 0 return;
359 } elsif ( ref( $opts{ban} ) ne '' ) {
360 0 $self->{error} = 10;
361 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
362 0 $self->warn;
363 0 return;
364 ▶ } elsif ( $opts{ban} !~ /\A$IPv4_re\z/
Condition33%1/3
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::npf::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::npf::IPv6_re\z/
l!l&&r!l&&!r
010
MC/DC0%0/2
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::npf::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::npf::IPv6_re\z/
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::npf::IPv4_re\z/ $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::npf::IPv6_re\z/
Truth table33%1/3
A || B
A= $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::npf::IPv4_re\z/B= $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::npf::IPv6_re\z/
ABresult
1X1
011
000
365 && $opts{ban} !~ /\A$IPv6_re\z/ )
366 {
367 0 $self->{error} = 10;
368 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 IP';
369 0 $self->warn;
370 0 return;
371 }
372  
373 # lowercase so the same IPv6 IP in differing cases can't result in duplicate entries
374 1 $opts{ban} = lc( $opts{ban} );
375  
376 1 ▶ if ( !$self->{banned}{ $opts{ban} } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"banned"}{$opts{"ban"}}) 0 1 1
377 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
378 0 $self->{frontend_obj}->{test_data} = 'not banned';
379 }
380 0 return;
381 }
382  
383 1 my $command = 'npfctl table ' . $self->{options}{table} . ' rem ' . $opts{ban};
384  
385 1 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 1 0 1
386 1 $self->{frontend_obj}->{test_data} = $command;
387 } else {
388 0 my $output = `$command 2>&1`;
389 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
390 0 $self->{error} = 14;
391 $self->{errorString}
392 0 = 'unban failed. non-zero exit code for the command... "' . $command . '"... output... ' . $output;
393 0 $self->warn;
394 }
395 }
396  
397 1 delete( $self->{banned}{ $opts{ban} } );
398 } ## end sub unban
399  
400 =head2 list
401  
402 List banned IPs. Returns an array of the currently banned single IPs from
403 the internal ban list. CIDR ranges are not included; for those see
404 L</list_cidr>.
405  
406 my @banned = $backend->list;
407  
408 =cut
409  
410 sub list {
411 1 ▶ my ( $self, %opts ) = @_;
Subroutine list: called CC=2 SCAR=7.0
Pod documented
412  
413 1 $self->errorblank;
414  
415 1 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 1 0 1
416 1 $self->{frontend_obj}->{test_data} = 'list';
417 }
418  
419 1 return keys( %{ $self->{banned} } );
420 }
421  
422 # Internal helper. Returns a true value if the passed scalar is a valid IPv4 or
423 # IPv6 CIDR range, that is an address followed by "/" and a prefix length that
424 # is within the range valid for its family (0 to 32 for IPv4, 0 to 128 for
425 # IPv6). Returns false otherwise.
426 sub _valid_cidr {
427 2 ▶ my ( $self, $cidr ) = @_;
Subroutine _valid_cidr: called CC=8 SCAR=30.3
428  
429 2 ▶ return 0 if ( !defined($cidr) || ref($cidr) ne '' );
Branch: 1 branch
BranchTrueFalseTotal
if not defined $cidr or ref $cidr ne "" 0 2 2
Condition33%1/3
not defined $cidr or ref $cidr ne ""
l!l&&r!l&&!r
002
MC/DC0%0/2
not defined $cidr or ref $cidr ne ""
not defined $cidr ref $cidr ne ""
Truth table33%1/3
not(A) or B
A= not defined $cidrB= ref $cidr ne ""
ABresult
1X1
011
000
430  
431 2 ▶ if ( $cidr =~ m!\A(.+)/([0-9]{1,3})\z! ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cidr =~ m[\A(.+)/([0-9]{1,3})\z]) 2 0 2
432 2 my ( $addr, $prefix ) = ( $1, $2 );
433 2 ▶ return 1 if ( $addr =~ /\A$IPv4_re\z/ && $prefix <= 32 );
Branch: 1 branch
BranchTrueFalseTotal
if $addr =~ /\A$Net::Firewall::BlockerHelper::backends::npf::IPv4_re\z/ and $prefix <= 32 2 0 2
Condition33%1/3
$addr =~ /\A$Net::Firewall::BlockerHelper::backends::npf::IPv4_re\z/ and $prefix <= 32
!ll&&!rl&&r
002
MC/DC0%0/2
$addr =~ /\A$Net::Firewall::BlockerHelper::backends::npf::IPv4_re\z/ and $prefix <= 32
$addr =~ /\A$Net::Firewall::BlockerHelper::backends::npf::IPv4_re\z/ $prefix <= 32
Truth table33%1/3
A and B
A= $addr =~ /\A$Net::Firewall::BlockerHelper::backends::npf::IPv4_re\z/B= $prefix <= 32
ABresult
0X0
100
111
434 0 ▶ return 1 if ( $addr =~ /\A$IPv6_re\z/ && $prefix <= 128 );
Branch: 1 branch
BranchTrueFalseTotal
if $addr =~ /\A$Net::Firewall::BlockerHelper::backends::npf::IPv6_re\z/ and $prefix <= 128 0 0 0
Condition0%0/3
$addr =~ /\A$Net::Firewall::BlockerHelper::backends::npf::IPv6_re\z/ and $prefix <= 128
!ll&&!rl&&r
000
MC/DC0%0/2
$addr =~ /\A$Net::Firewall::BlockerHelper::backends::npf::IPv6_re\z/ and $prefix <= 128
$addr =~ /\A$Net::Firewall::BlockerHelper::backends::npf::IPv6_re\z/ $prefix <= 128
Truth table0%0/3
A and B
A= $addr =~ /\A$Net::Firewall::BlockerHelper::backends::npf::IPv6_re\z/B= $prefix <= 128
ABresult
0X0
100
111
435 }
436  
437 0 return 0;
438 } ## end sub _valid_cidr
439  
440 =head2 ban_cidr
441  
442 Bans a CIDR range by adding it to the npf table. Tables of type C<lpm>
443 accept a network prefix in the same manner as a single address; C<ipset>
444 tables do not, so for this to work the table must be declared C<type lpm>
445 in C<npf.conf>.
446  
447 $backend->ban_cidr(ban => '1.2.3.0/24');
448  
449 =cut
450  
451 sub ban_cidr {
452 1 ▶ my ( $self, %opts ) = @_;
Subroutine ban_cidr: called CC=9 SCAR=35.4
Pod documented
453  
454 1 $self->errorblank;
455  
456 1 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 1 1
457 0 $self->{error} = 1;
458 0 $self->{errorString} = 'backend has not been inited';
459 0 $self->warn;
460 0 return;
461 }
462  
463 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
464 0 $self->{error} = 9;
465 0 $self->{errorString} = 'Nothing specified for the value ban';
466 0 $self->warn;
467 0 return;
468 } elsif ( ref( $opts{ban} ) ne '' ) {
469 0 $self->{error} = 30;
470 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
471 0 $self->warn;
472 0 return;
473 } elsif ( !$self->_valid_cidr( $opts{ban} ) ) {
474 0 $self->{error} = 30;
475 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 CIDR';
476 0 $self->warn;
477 0 return;
478 }
479  
480 # lowercase so the same IPv6 CIDR in differing cases can't result in duplicate entries
481 1 $opts{ban} = lc( $opts{ban} );
482  
483 1 ▶ if ( $self->{banned_cidr}{ $opts{ban} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"banned_cidr"}{$opts{"ban"}}) 0 1 1
484 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
485 0 $self->{frontend_obj}->{test_data} = 'already banned';
486 }
487 0 return;
488 }
489  
490 1 my $command = 'npfctl table ' . $self->{options}{table} . ' add ' . $opts{ban};
491  
492 1 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 1 0 1
493 1 $self->{frontend_obj}->{test_data} = [$command];
494 } else {
495 0 my $output = `$command 2>&1`;
496 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
497 0 $self->{error} = 28;
498 $self->{errorString}
499 0 = 'ban failed. non-zero exit code for the command... "' . $command . '"... output... ' . $output;
500 0 $self->warn;
501 }
502 }
503  
504 1 $self->{banned_cidr}{ $opts{ban} } = 1;
505 } ## end sub ban_cidr
506  
507 =head2 unban_cidr
508  
509 Unbans a CIDR range by removing it from the npf table.
510  
511 $backend->unban_cidr(ban => '1.2.3.0/24');
512  
513 =cut
514  
515 sub unban_cidr {
516 1 ▶ my ( $self, %opts ) = @_;
Subroutine unban_cidr: called CC=9 SCAR=35.4
Pod documented
517  
518 1 $self->errorblank;
519  
520 1 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 1 1
521 0 $self->{error} = 1;
522 0 $self->{errorString} = 'backend has not been inited';
523 0 $self->warn;
524 0 return;
525 }
526  
527 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
528 0 $self->{error} = 9;
529 0 $self->{errorString} = 'Nothing specified for the value ban';
530 0 $self->warn;
531 0 return;
532 } elsif ( ref( $opts{ban} ) ne '' ) {
533 0 $self->{error} = 30;
534 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
535 0 $self->warn;
536 0 return;
537 } elsif ( !$self->_valid_cidr( $opts{ban} ) ) {
538 0 $self->{error} = 30;
539 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 CIDR';
540 0 $self->warn;
541 0 return;
542 }
543  
544 # lowercase so the same IPv6 CIDR in differing cases can't result in duplicate entries
545 1 $opts{ban} = lc( $opts{ban} );
546  
547 1 ▶ if ( !$self->{banned_cidr}{ $opts{ban} } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"banned_cidr"}{$opts{"ban"}}) 0 1 1
548 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
549 0 $self->{frontend_obj}->{test_data} = 'not banned';
550 }
551 0 return;
552 }
553  
554 1 my $command = 'npfctl table ' . $self->{options}{table} . ' rem ' . $opts{ban};
555  
556 1 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 1 0 1
557 1 $self->{frontend_obj}->{test_data} = $command;
558 } else {
559 0 my $output = `$command 2>&1`;
560 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
561 0 $self->{error} = 29;
562 $self->{errorString}
563 0 = 'unban failed. non-zero exit code for the command... "' . $command . '"... output... ' . $output;
564 0 $self->warn;
565 }
566 }
567  
568 1 delete( $self->{banned_cidr}{ $opts{ban} } );
569 } ## end sub unban_cidr
570  
571 =head2 list_cidr
572  
573 List banned CIDR ranges. Returns an array of the currently banned CIDR
574 ranges from the internal ban list.
575  
576 my @banned_cidrs = $backend->list_cidr;
577  
578 =cut
579  
580 sub list_cidr {
581 2 ▶ my ( $self, %opts ) = @_;
Subroutine list_cidr: called CC=2 SCAR=7.0
Pod documented
582  
583 2 $self->errorblank;
584  
585 2 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 2 0 2
586 2 $self->{frontend_obj}->{test_data} = 'list_cidr';
587 }
588  
589 2 return keys( %{ $self->{banned_cidr} } );
590 }
591  
592 =head2 re_init
593  
594 Tells the backend to re-init it's self.
595  
596 This will call teardown, which flushes the table, then init, which
597 re-verifies the table is declared, and then re-add all retained bans, both
598 single IPs and CIDR ranges, via C<npfctl table ... add>.
599  
600 $backend->re_init;
601  
602 =cut
603  
604 sub re_init {
605 1 ▶ my ( $self, %opts ) = @_;
Subroutine re_init: called CC=6 SCAR=20.9
Pod documented
606  
607 1 $self->errorblank;
608  
609 1 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 1 1
610 0 $self->{error} = 1;
611 0 $self->{errorString} = 'backend has not been inited';
612 0 $self->warn;
613 0 return;
614 }
615  
616 # teardown is best effort here as a partially or fully wiped setup is
617 # exactly what re_init needs to recover from; init cleans up any remnants
618 {
619 1 local $@;
620 1 eval { $self->teardown; };
621 }
622 1 $self->init;
623  
624 1 my @to_ban = ( keys( %{ $self->{banned} } ), keys( %{ $self->{banned_cidr} } ) );
625  
626 1 my @re_init_test_data;
627 1 foreach my $item (@to_ban) {
628 1 my $command = 'npfctl table ' . $self->{options}{table} . ' add ' . $item;
629  
630 1 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 1 0 1
631 1 push( @re_init_test_data, $command );
632 } else {
633 0 my $output = `$command 2>&1`;
634 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
635 0 $self->{error} = 13;
636 $self->{errorString}
637 0 = 'ban failed. non-zero exit code for the command... "' . $command . '"... output... ' . $output;
638 0 $self->warn;
639 }
640 }
641 } ## end foreach my $item (@to_ban)
642  
643 1 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 1 0 1
644 1 $self->{frontend_obj}->{test_data} = \@re_init_test_data;
645 }
646  
647 1 $self->{inited} = 1;
648 } ## end sub re_init
649  
650 =head2 teardown
651  
652 Tears down the setup for the backend by flushing the table. The table itself
653 lives in npf.conf and can not be removed. The internal list of bans is kept,
654 so a following re_init will re-add them.
655  
656 $backend->teardown;
657  
658 =cut
659  
660 sub teardown {
661 2 ▶ my ( $self, %opts ) = @_;
Subroutine teardown: called CC=4 SCAR=19.2
Pod documented
662  
663 2 $self->errorblank;
664  
665 2 $self->{inited} = 0;
666  
667 2 my @commands = ( 'npfctl table ' . $self->{options}{table} . ' flush' );
668  
669 2 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 2 0 2
670 2 $self->{frontend_obj}->{test_data} = \@commands;
671 } else {
672 0 foreach my $item (@commands) {
673 0 my $output = `$item 2>&1`;
674 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
675 0 $self->{error} = 17;
676 $self->{errorString}
677 0 = 'teardown failed. non-zero exit code for the command... "' . $item . '"... output... ' . $output;
678 0 $self->warn;
679 }
680 }
681 } ## end else [ if ( $self->{testing} ) ]
682 } ## end sub teardown
683  
684 =head2 stop
685  
686 Alias for L</teardown>, provided for parity with the fail2ban C<actionstop>
687 concept.
688  
689 $backend->stop;
690  
691 =cut
692  
693 sub stop {
694 0 ▶ my ( $self, %opts ) = @_;
Subroutine stop: not called CC=1 SCAR=6.9
Pod documented
695  
696 0 return $self->teardown(%opts);
697 }
698  
699 =head2 check
700  
701 Verifies that the npf table is still present and usable. Returns a true
702 value if it is and a false value otherwise. This is the equivalent of
703 fail2ban's C<actioncheck>.
704  
705 if ( !$backend->check ) {
706 $backend->re_init;
707 }
708  
709 =cut
710  
711 sub check {
712 8 ▶ my ( $self, %opts ) = @_;
Subroutine check: called CC=4 SCAR=18.6
Pod documented
713  
714 8 $self->errorblank;
715  
716 8 my @commands = ( 'npfctl table ' . $self->{options}{table} . ' list' );
717  
718 8 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 8 0 8
719 8 $self->{frontend_obj}->{test_data} = \@commands;
720 8 return 1;
721 }
722  
723 0 foreach my $item (@commands) {
724 0 my $output = `$item 2>&1`;
725 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
726 0 return 0;
727 }
728 }
729  
730 0 return 1;
731 } ## end sub check
732  
733 =head2 flush
734  
735 Removes all currently banned IPs at once by flushing the table and
736 forgetting them. This is the equivalent of fail2ban's C<actionflush>.
737  
738 $backend->flush;
739  
740 =cut
741  
742 sub flush {
743 1 ▶ my ( $self, %opts ) = @_;
Subroutine flush: called CC=5 SCAR=23.0
Pod documented
744  
745 1 $self->errorblank;
746  
747 1 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 1 1
748 0 $self->{error} = 1;
749 0 $self->{errorString} = 'backend has not been inited';
750 0 $self->warn;
751 0 return;
752 }
753  
754 1 my @commands = ( 'npfctl table ' . $self->{options}{table} . ' flush' );
755  
756 1 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 1 0 1
757 1 $self->{frontend_obj}->{test_data} = \@commands;
758 } else {
759 0 foreach my $item (@commands) {
760 0 my $output = `$item 2>&1`;
761 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
762 0 $self->{error} = 25;
763 $self->{errorString}
764 0 = 'flush failed. non-zero exit code for the command... "' . $item . '"... output... ' . $output;
765 0 $self->warn;
766 }
767 }
768 } ## end else [ if ( $self->{testing} ) ]
769  
770 # the table flush above removes both single IP and CIDR entries at once
771 1 $self->{banned} = {};
772 1 $self->{banned_cidr} = {};
773 } ## end sub flush
774  
775 =head1 ERROR CODES / FLAGS
776  
777 Error handling is provided by L<Error::Helper>. All
778 errors are considered fatal.
779  
780 =head2 1, notInited
781  
782 The backend has not been inited yet.
783  
784 =head2 6, invalidPrefixSpecified
785  
786 The specified prefix did not match /^[a-zA-Z0-9]+$/.
787  
788 =head2 7, invalidName
789  
790 The name is either undef or does not match /^[a-zA-Z0-9\-]+$/.
791  
792 =head2 8, optionsNotHash
793  
794 The item passed to new for options is not a hash.
795  
796 =head2 9, noBanItem
797  
798 No IP or CIDR range specified to ban or unban.
799  
800 =head2 10, banItemNotIP
801  
802 The item to ban is not an IP. Either wrong ref type or regexp
803 test using L<Regexp::IPv4> and L<Regexp::IPv6> failed.
804  
805 =head2 12, backendInitError
806  
807 Failed to init the backend.
808  
809 =head2 13, banFailed
810  
811 Failed to ban the item.
812  
813 =head2 14, unbanFailed
814  
815 Failed to unban the item.
816  
817 =head2 15, listFailed
818  
819 Failed to get a list of bans.
820  
821 =head2 16, reInitFailed
822  
823 Failed to re_init the backend.
824  
825 =head2 17, teardownFailed
826  
827 Failed to teardown the backend.
828  
829 =head2 18, alreadyInited
830  
831 init called, but the backend has already been inited.
832  
833 =head2 19, tableInvalid
834  
835 The option table does not match /^[a-zA-Z0-9_\-]+$/.
836  
837 =head2 23, initFailed
838  
839 The table does not appear to be declared in npf.conf.
840  
841 =head2 24, checkFailed
842  
843 The backend check raised an error.
844  
845 =head2 25, flushFailed
846  
847 Failed to flush the bans.
848  
849 =head2 26, portsNotSupported
850  
851 The npf backend does not support ports. Ports belong to the rule referencing
852 the table in npf.conf.
853  
854 =head2 27, protocolsNotSupported
855  
856 The npf backend does not support protocols. Protocols belong to the rule
857 referencing the table in npf.conf.
858  
859 =head2 28, banCidrFailed
860  
861 Failed to ban the CIDR range.
862  
863 =head2 29, unbanCidrFailed
864  
865 Failed to unban the CIDR range.
866  
867 =head2 30, cidrItemNotCidr
868  
869 The item to ban is not a CIDR range. Either wrong ref type or it is not an
870 IPv4 or IPv6 address followed by a prefix length valid for its family.
871  
872 =head2 31, cidrNotSupported
873  
874 The backend does not support CIDR bans.
875  
876 =head2 32, listCidrFailed
877  
878 Failed to get a list of CIDR bans.
879  
880 =head1 AUTHOR
881  
882 Zane C. Bowers-Hadley, C<< <vvelox at vvelox.ent> >>
883  
884 =head1 BUGS
885  
886 Please report any bugs or feature requests to C<bug-net-firewall-blockerhelper at rt.cpan.org>, or through
887 the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Net-Firewall-BlockerHelper>.
888  
889 =head1 LICENSE AND COPYRIGHT
890  
891 This software is Copyright (c) 2023 by Zane C. Bowers-Hadley.
892  
893 This is free software, licensed under:
894  
895 The GNU Lesser General Public License, Version 2.1, February 1999
896  
897  
898 =cut
899  
900 1 1; # End of Net::Firewall::BlockerHelper
« BlockerHelper/backends/xdp.pm ↑ Summary BlockerHelper/backends/azure.pm »