BlockerHelper/backends/npf.pm

filter: Statementstmt 50.8% 151 / 297 Branchbran 46.6% 55 / 118 Conditioncond 74.0% 20 / 27 MC/DCmcdc 43.7% 7 / 16 Subroutinesub 94.7% 18 / 19 Podpod 100% 13 / 13 totaltotal 53.8% 264 / 490 CC 74 SCAR 31.1
CC
74
Cov
51%
unban
41.3
ban_cidr
34.5
unban_cidr
34.5
CRAP 22.5
« BlockerHelper/backends/linux_ip_route.pm ↑ Summary BlockerHelper/backends/xdp.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 qw( Error::Helper Net::Firewall::BlockerHelper::Util );
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.2.0
17  
18 =cut
19  
20 1 our $VERSION = '0.2.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 5 ▶sub new {
Subroutine new: called CC=20 SCAR=33.2
Pod documented
90 5 my ( $blank, %opts ) = @_;
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 3 ▶sub init {
Subroutine init: called CC=5 SCAR=23.2
Pod documented
232 3 my ( $self, %opts ) = @_;
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 4 ▶sub ban {
Subroutine ban: called CC=10 SCAR=33.4
Pod documented
275 4 my ( $self, %opts ) = @_;
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 1 ▶sub unban {
Subroutine unban: called CC=10 SCAR=37.2
Pod documented
343 1 my ( $self, %opts ) = @_;
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 1 ▶sub list {
Subroutine list: called CC=2 SCAR=7.0
Pod documented
411 1 my ( $self, %opts ) = @_;
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 =head2 ban_cidr
423  
424 Bans a CIDR range by adding it to the npf table. Tables of type C<lpm>
425 accept a network prefix in the same manner as a single address; C<ipset>
426 tables do not, so for this to work the table must be declared C<type lpm>
427 in C<npf.conf>.
428  
429 $backend->ban_cidr(ban => '1.2.3.0/24');
430  
431 =cut
432  
433 1 ▶sub ban_cidr {
Subroutine ban_cidr: called CC=9 SCAR=35.4
Pod documented
434 1 my ( $self, %opts ) = @_;
435  
436 1 $self->errorblank;
437  
438 1 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 1 1
439 0 $self->{error} = 1;
440 0 $self->{errorString} = 'backend has not been inited';
441 0 $self->warn;
442 0 return;
443 }
444  
445 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
446 0 $self->{error} = 9;
447 0 $self->{errorString} = 'Nothing specified for the value ban';
448 0 $self->warn;
449 0 return;
450 } elsif ( ref( $opts{ban} ) ne '' ) {
451 0 $self->{error} = 30;
452 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
453 0 $self->warn;
454 0 return;
455 } elsif ( !$self->_valid_cidr( $opts{ban} ) ) {
456 0 $self->{error} = 30;
457 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 CIDR';
458 0 $self->warn;
459 0 return;
460 }
461  
462 # lowercase so the same IPv6 CIDR in differing cases can't result in duplicate entries
463 1 $opts{ban} = lc( $opts{ban} );
464  
465 1 ▶ if ( $self->{banned_cidr}{ $opts{ban} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"banned_cidr"}{$opts{"ban"}}) 0 1 1
466 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
467 0 $self->{frontend_obj}->{test_data} = 'already banned';
468 }
469 0 return;
470 }
471  
472 1 my $command = 'npfctl table ' . $self->{options}{table} . ' add ' . $opts{ban};
473  
474 1 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 1 0 1
475 1 $self->{frontend_obj}->{test_data} = [$command];
476 } else {
477 0 my $output = `$command 2>&1`;
478 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
479 0 $self->{error} = 28;
480 $self->{errorString}
481 0 = 'ban failed. non-zero exit code for the command... "' . $command . '"... output... ' . $output;
482 0 $self->warn;
483 }
484 }
485  
486 1 $self->{banned_cidr}{ $opts{ban} } = 1;
487 } ## end sub ban_cidr
488  
489 =head2 unban_cidr
490  
491 Unbans a CIDR range by removing it from the npf table.
492  
493 $backend->unban_cidr(ban => '1.2.3.0/24');
494  
495 =cut
496  
497 1 ▶sub unban_cidr {
Subroutine unban_cidr: called CC=9 SCAR=35.4
Pod documented
498 1 my ( $self, %opts ) = @_;
499  
500 1 $self->errorblank;
501  
502 1 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 1 1
503 0 $self->{error} = 1;
504 0 $self->{errorString} = 'backend has not been inited';
505 0 $self->warn;
506 0 return;
507 }
508  
509 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
510 0 $self->{error} = 9;
511 0 $self->{errorString} = 'Nothing specified for the value ban';
512 0 $self->warn;
513 0 return;
514 } elsif ( ref( $opts{ban} ) ne '' ) {
515 0 $self->{error} = 30;
516 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
517 0 $self->warn;
518 0 return;
519 } elsif ( !$self->_valid_cidr( $opts{ban} ) ) {
520 0 $self->{error} = 30;
521 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 CIDR';
522 0 $self->warn;
523 0 return;
524 }
525  
526 # lowercase so the same IPv6 CIDR in differing cases can't result in duplicate entries
527 1 $opts{ban} = lc( $opts{ban} );
528  
529 1 ▶ if ( !$self->{banned_cidr}{ $opts{ban} } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"banned_cidr"}{$opts{"ban"}}) 0 1 1
530 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
531 0 $self->{frontend_obj}->{test_data} = 'not banned';
532 }
533 0 return;
534 }
535  
536 1 my $command = 'npfctl table ' . $self->{options}{table} . ' rem ' . $opts{ban};
537  
538 1 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 1 0 1
539 1 $self->{frontend_obj}->{test_data} = $command;
540 } else {
541 0 my $output = `$command 2>&1`;
542 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
543 0 $self->{error} = 29;
544 $self->{errorString}
545 0 = 'unban failed. non-zero exit code for the command... "' . $command . '"... output... ' . $output;
546 0 $self->warn;
547 }
548 }
549  
550 1 delete( $self->{banned_cidr}{ $opts{ban} } );
551 } ## end sub unban_cidr
552  
553 =head2 list_cidr
554  
555 List banned CIDR ranges. Returns an array of the currently banned CIDR
556 ranges from the internal ban list.
557  
558 my @banned_cidrs = $backend->list_cidr;
559  
560 =cut
561  
562 2 ▶sub list_cidr {
Subroutine list_cidr: called CC=2 SCAR=7.0
Pod documented
563 2 my ( $self, %opts ) = @_;
564  
565 2 $self->errorblank;
566  
567 2 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 2 0 2
568 2 $self->{frontend_obj}->{test_data} = 'list_cidr';
569 }
570  
571 2 return keys( %{ $self->{banned_cidr} } );
572 }
573  
574 =head2 re_init
575  
576 Tells the backend to re-init it's self.
577  
578 This will call teardown, which flushes the table, then init, which
579 re-verifies the table is declared, and then re-add all retained bans, both
580 single IPs and CIDR ranges, via C<npfctl table ... add>.
581  
582 $backend->re_init;
583  
584 =cut
585  
586 1 ▶sub re_init {
Subroutine re_init: called CC=5 SCAR=17.5
Pod documented
587 1 my ( $self, %opts ) = @_;
588  
589 1 $self->errorblank;
590  
591 # teardown is best effort here as a partially or fully wiped setup is
592 # exactly what re_init needs to recover from; init cleans up any remnants
593 {
594 1 local $@;
595 1 eval { $self->teardown; };
596 }
597 1 $self->init;
598  
599 1 my @to_ban = ( keys( %{ $self->{banned} } ), keys( %{ $self->{banned_cidr} } ) );
600  
601 1 my @re_init_test_data;
602 1 foreach my $item (@to_ban) {
603 1 my $command = 'npfctl table ' . $self->{options}{table} . ' add ' . $item;
604  
605 1 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 1 0 1
606 1 push( @re_init_test_data, $command );
607 } else {
608 0 my $output = `$command 2>&1`;
609 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
610 0 $self->{error} = 13;
611 $self->{errorString}
612 0 = 'ban failed. non-zero exit code for the command... "' . $command . '"... output... ' . $output;
613 0 $self->warn;
614 }
615 }
616 } ## end foreach my $item (@to_ban)
617  
618 1 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 1 0 1
619 1 $self->{frontend_obj}->{test_data} = \@re_init_test_data;
620 }
621  
622 1 $self->{inited} = 1;
623 } ## end sub re_init
624  
625 =head2 teardown
626  
627 Tears down the setup for the backend by flushing the table. The table itself
628 lives in npf.conf and can not be removed. The internal list of bans is kept,
629 so a following re_init will re-add them.
630  
631 $backend->teardown;
632  
633 =cut
634  
635 2 ▶sub teardown {
Subroutine teardown: called CC=4 SCAR=19.2
Pod documented
636 2 my ( $self, %opts ) = @_;
637  
638 2 $self->errorblank;
639  
640 2 $self->{inited} = 0;
641  
642 2 my @commands = ( 'npfctl table ' . $self->{options}{table} . ' flush' );
643  
644 2 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 2 0 2
645 2 $self->{frontend_obj}->{test_data} = \@commands;
646 } else {
647 0 foreach my $item (@commands) {
648 0 my $output = `$item 2>&1`;
649 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
650 0 $self->{error} = 17;
651 $self->{errorString}
652 0 = 'teardown failed. non-zero exit code for the command... "' . $item . '"... output... ' . $output;
653 0 $self->warn;
654 }
655 }
656 } ## end else [ if ( $self->{testing} ) ]
657 } ## end sub teardown
658  
659 =head2 stop
660  
661 Alias for L</teardown>, provided for parity with the fail2ban C<actionstop>
662 concept.
663  
664 $backend->stop;
665  
666 =cut
667  
668 0 ▶sub stop {
Subroutine stop: not called CC=1 SCAR=6.9
Pod documented
669 0 my ( $self, %opts ) = @_;
670  
671 0 return $self->teardown(%opts);
672 }
673  
674 =head2 check
675  
676 Verifies that the npf table is still present and usable. Returns a true
677 value if it is and a false value otherwise. This is the equivalent of
678 fail2ban's C<actioncheck>.
679  
680 if ( !$backend->check ) {
681 $backend->re_init;
682 }
683  
684 =cut
685  
686 8 ▶sub check {
Subroutine check: called CC=4 SCAR=18.6
Pod documented
687 8 my ( $self, %opts ) = @_;
688  
689 8 $self->errorblank;
690  
691 8 my @commands = ( 'npfctl table ' . $self->{options}{table} . ' list' );
692  
693 8 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 8 0 8
694 8 $self->{frontend_obj}->{test_data} = \@commands;
695 8 return 1;
696 }
697  
698 0 foreach my $item (@commands) {
699 0 my $output = `$item 2>&1`;
700 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
701 0 return 0;
702 }
703 }
704  
705 0 return 1;
706 } ## end sub check
707  
708 =head2 flush
709  
710 Removes all currently banned IPs at once by flushing the table and
711 forgetting them. This is the equivalent of fail2ban's C<actionflush>.
712  
713 $backend->flush;
714  
715 =cut
716  
717 1 ▶sub flush {
Subroutine flush: called CC=5 SCAR=23.0
Pod documented
718 1 my ( $self, %opts ) = @_;
719  
720 1 $self->errorblank;
721  
722 1 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 1 1
723 0 $self->{error} = 1;
724 0 $self->{errorString} = 'backend has not been inited';
725 0 $self->warn;
726 0 return;
727 }
728  
729 1 my @commands = ( 'npfctl table ' . $self->{options}{table} . ' flush' );
730  
731 1 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 1 0 1
732 1 $self->{frontend_obj}->{test_data} = \@commands;
733 } else {
734 0 foreach my $item (@commands) {
735 0 my $output = `$item 2>&1`;
736 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
737 0 $self->{error} = 25;
738 $self->{errorString}
739 0 = 'flush failed. non-zero exit code for the command... "' . $item . '"... output... ' . $output;
740 0 $self->warn;
741 }
742 }
743 } ## end else [ if ( $self->{testing} ) ]
744  
745 # the table flush above removes both single IP and CIDR entries at once
746 1 $self->{banned} = {};
747 1 $self->{banned_cidr} = {};
748 } ## end sub flush
749  
750 =head1 ERROR CODES / FLAGS
751  
752 Error handling is provided by L<Error::Helper>. All
753 errors are considered fatal.
754  
755 =head2 1, notInited
756  
757 The backend has not been inited yet.
758  
759 =head2 6, invalidPrefixSpecified
760  
761 The specified prefix did not match /^[a-zA-Z0-9]+$/.
762  
763 =head2 7, invalidName
764  
765 The name is either undef or does not match /^[a-zA-Z0-9\-]+$/.
766  
767 =head2 8, optionsNotHash
768  
769 The item passed to new for options is not a hash.
770  
771 =head2 9, noBanItem
772  
773 No IP or CIDR range specified to ban or unban.
774  
775 =head2 10, banItemNotIP
776  
777 The item to ban is not an IP. Either wrong ref type or regexp
778 test using L<Regexp::IPv4> and L<Regexp::IPv6> failed.
779  
780 =head2 12, backendInitError
781  
782 Failed to init the backend.
783  
784 =head2 13, banFailed
785  
786 Failed to ban the item.
787  
788 =head2 14, unbanFailed
789  
790 Failed to unban the item.
791  
792 =head2 15, listFailed
793  
794 Failed to get a list of bans.
795  
796 =head2 16, reInitFailed
797  
798 Failed to re_init the backend.
799  
800 =head2 17, teardownFailed
801  
802 Failed to teardown the backend.
803  
804 =head2 18, alreadyInited
805  
806 init called, but the backend has already been inited.
807  
808 =head2 19, tableInvalid
809  
810 The option table does not match /^[a-zA-Z0-9_\-]+$/.
811  
812 =head2 23, initFailed
813  
814 The table does not appear to be declared in npf.conf.
815  
816 =head2 24, checkFailed
817  
818 The backend check raised an error.
819  
820 =head2 25, flushFailed
821  
822 Failed to flush the bans.
823  
824 =head2 26, portsNotSupported
825  
826 The npf backend does not support ports. Ports belong to the rule referencing
827 the table in npf.conf.
828  
829 =head2 27, protocolsNotSupported
830  
831 The npf backend does not support protocols. Protocols belong to the rule
832 referencing the table in npf.conf.
833  
834 =head2 28, banCidrFailed
835  
836 Failed to ban the CIDR range.
837  
838 =head2 29, unbanCidrFailed
839  
840 Failed to unban the CIDR range.
841  
842 =head2 30, cidrItemNotCidr
843  
844 The item to ban is not a CIDR range. Either wrong ref type or it is not an
845 IPv4 or IPv6 address followed by a prefix length valid for its family.
846  
847 =head2 31, cidrNotSupported
848  
849 The backend does not support CIDR bans.
850  
851 =head2 32, listCidrFailed
852  
853 Failed to get a list of CIDR bans.
854  
855 =head1 AUTHOR
856  
857 Zane C. Bowers-Hadley, C<< <vvelox at vvelox.ent> >>
858  
859 =head1 BUGS
860  
861 Please report any bugs or feature requests to C<bug-net-firewall-blockerhelper at rt.cpan.org>, or through
862 the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Net-Firewall-BlockerHelper>.
863  
864 =head1 LICENSE AND COPYRIGHT
865  
866 This software is Copyright (c) 2023 by Zane C. Bowers-Hadley.
867  
868 This is free software, licensed under:
869  
870 The GNU Lesser General Public License, Version 2.1, February 1999
871  
872  
873 =cut
874  
875 1 1; # End of Net::Firewall::BlockerHelper
« BlockerHelper/backends/linux_ip_route.pm ↑ Summary BlockerHelper/backends/xdp.pm »