BlockerHelper/backends/nftables.pm

filter: Statementstmt 4.9% 19 / 382 Branchbran 0.0% 0 / 158 Conditioncond 0.0% 0 / 60 MC/DCmcdc 0.0% 0 / 38 Subroutinesub 26.0% 6 / 23 Podpod 100% 13 / 13 totaltotal 5.6% 38 / 674 CC 115 SCAR 59.5
CC
115
Cov
3%
new
1190.0
_rule_specs
210.0
ban
210.0
CRAP 382.1
« BlockerHelper/backends/ufw.pm ↑ Summary BlockerHelper/backends/firewalld.pm »
1 package Net::Firewall::BlockerHelper::backends::nftables;
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::nftables - nftables 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 => 'nftables',
28 ports => ['22'],
29 protocols => ['tcp'],
30 name => 'ssh',
31 );
32  
33 $fw_helper->init_backend;
34 $fw_helper->ban(ban => '1.2.3.4');
35 $fw_helper->ban(ban => 'dead::1');
36 $fw_helper->unban(ban => '1.2.3.4');
37 $fw_helper->teardown;
38  
39 =head1 DESCRIPTION
40  
41 Blocks IPs using L<nft(8)>.
42  
43 A dedicated table C<< inet <prefix>_<name> >> is created per instance,
44 containing a base chain hooked to input, one set for IPv4
45 (C<< <prefix>_<name>_4 >>) and one for IPv6 (C<< <prefix>_<name>_6 >>), and
46 the block rules referencing them. Banning an IP adds it to the relevant set.
47 As the whole setup lives in its own table, teardown is simply deleting the
48 table.
49  
50 Requires C<nft> to be installed and in the C<PATH> of the process, which must
51 have sufficient privileges to run it.
52  
53 =head1 METHODS
54  
55 =head2 new
56  
57 Initiates the object.
58  
59 - options :: Backend specific options that will be passed to the backend unchecked
60 outside of making sure it is a hash ref if defined. See below for furhter info.
61 - Default :: {}
62  
63 - ports :: A array of ports to block. Checked to make sure they are ints within the
64 range 1 to 65535 or a valid service name via getservbyname. All ports will be
65 blocked if non are specified. Duplicates are removed.
66 - Default :: []
67  
68 - protocols :: A array of protocols to block. This is checked against
69 /etc/protocols via the function getprotobyname. Duplicates will be
70 discarded. If no protocols are given, all IPv4 and IPv6 traffic
71 sourced from the sets is blocked, unless ports are given, in which
72 case it defaults to tcp and udp. Ports are only attached to
73 port-capable protocols (tcp/udp/sctp); other protocols are blocked
74 without a port.
75 - Default :: [], or ['tcp','udp'] when ports are given
76  
77 - prefix :: Prefix to use. Must match the regex /^[a-zA-Z0-9]+$/
78 - default :: kur
79  
80 - name :: Name of this specific instance. This must be specified.
81 - default :: undef
82  
83 The options hash accepts the following.
84  
85 - type :: The drop method to use. 'drop' silently drops. 'reject' sends
86 the family-appropriate ICMP port-unreachable back.
87 - Default :: drop
88  
89 - priority :: Priority of the base chain. See nft(8).
90 - Default :: -1
91  
92 - kill :: Use conntrack(8) to drop existing connection tracking entries
93 for the banned IP, for both IPv4 and IPv6. When protocols are
94 configured the kill is scoped to them via -p, so blocking only
95 udp will not drop tcp entries and vice versa; protocols conntrack
96 can not filter by are skipped. With no protocols configured
97 everything is being blocked, so entries of every protocol are
98 dropped.
99 - Default :: 0
100  
101 All errors are considered fatal, meaning if new fails it will die.
102  
103 =cut
104  
105 sub new {
106 0 ▶ my ( $blank, %opts ) = @_;
Subroutine new: not called CC=34 SCAR=70.8
Pod documented
107  
108 0 my $self = {
109 perror => undef,
110 error => undef,
111 errorLine => undef,
112 errorFilename => undef,
113 errorString => "",
114 errorExtra => {
115 all_errors_fatal => 1,
116 # all_fatal is what Error::Helper 2.1.0 actually checks; all_errors_fatal
117 # is kept for the name documented in its POD
118 all_fatal => 1,
119 flags => {
120 1 => 'notInited',
121 2 => 'invalidPortSpecified',
122 3 => 'portsNotArray',
123 4 => 'protocolsNotArray',
124 5 => 'invalidPortSpecified',
125 6 => 'invalidPrefixSpecified',
126 7 => 'invalidName',
127 8 => 'optionsNotHash',
128 9 => 'noBanItem',
129 10 => 'banItemNotIP',
130 12 => 'backendInitError',
131 13 => 'banFailed',
132 14 => 'unbanFailed',
133 15 => 'listFailed',
134 16 => 'reInitFailed',
135 17 => 'teardownFailed',
136 18 => 'alreadyInited',
137 19 => 'priorityInvalid',
138 20 => 'typeInvalid',
139 23 => 'initFailed',
140 24 => 'checkFailed',
141 25 => 'flushFailed',
142 26 => 'banCidrFailed',
143 27 => 'unbanCidrFailed',
144 28 => 'cidrItemNotCidr',
145 29 => 'cidrNotSupported',
146 30 => 'listCidrFailed',
147 },
148 fatal_flags => {},
149 perror_not_fatal => 0,
150 },
151 options => {
152 type => 'drop',
153 priority => -1,
154 kill => 0,
155 },
156 ports => [],
157 protocols => [],
158 testing => undef,
159 test_data => undef,
160 prefix => 'kur',
161 frontend_obj => undef,
162 inited => 0,
163 banned => {},
164 cidr_supported => 0,
165 };
166 0 bless $self;
167  
168 0 ▶ if ( defined( $opts{ports} ) && ref( $opts{ports} ) ne 'ARRAY' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $opts{'ports'} and ref $opts{'ports'} ne 'ARRAY') { } 0 0 0
elsif (defined $opts{'ports'}) { } 0 0 0
Condition0%0/3
defined $opts{"ports"} and ref $opts{"ports"} ne "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $opts{"ports"} and ref $opts{"ports"} ne "ARRAY"
defined $opts{"ports"} ref $opts{"ports"} ne "ARRAY"
Truth table0%0/3
A and B
A= defined $opts{"ports"}B= ref $opts{"ports"} ne "ARRAY"
ABresult
0X0
100
111
169 0 $self->{perror} = 1;
170 0 $self->{error} = 3;
171 0 $self->{errorString} = 'ports is defined and type is not array but "' . ref( $opts{ports} ) . '"';
172 0 $self->warn;
173 } elsif ( defined( $opts{ports} ) ) {
174 0 my %ports;
175 0 foreach my $item ( @{ $opts{ports} } ) {
176 0 ▶ if ( $item =~ /^[0-9]+$/ && $item >= 1 && $item <= 65535 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($item =~ /^[0-9]+$/ and $item >= 1 and $item <= 65535) { } 0 0 0
elsif ($item =~ /^[0-9]+$/) { } 0 0 0
Condition0%0/6
$item =~ /^[0-9]+$/ and $item >= 1 and $item <= 65535
!ll&&!rl&&r
000
$item =~ /^[0-9]+$/ and $item >= 1
!ll&&!rl&&r
000
MC/DC0%0/3
$item =~ /^[0-9]+$/ and $item >= 1 and $item <= 65535
$item =~ /^[0-9]+$/ $item >= 1 $item <= 65535
Truth table0%0/4
A and B and C
A= $item =~ /^[0-9]+$/B= $item >= 1C= $item <= 65535
ABCresult
0XX0
10X0
1100
1111
177 0 $ports{$item} = 1;
178 } elsif ( $item =~ /^[0-9]+$/ ) {
179 0 $self->{perror} = 1;
180 0 $self->{error} = 2;
181 $self->{errorString}
182 0 = $item . ' is not a valid value for a port as it must be a int within the range 1 to 65535';
183 0 $self->warn;
184 } else {
185 # just using tcp here as protocol must be specified
186 0 my ( $name, $aliases, $port, $proto ) = getservbyname( $item, 'tcp' );
187 0 ▶ if ( !defined($port) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $port) 0 0 0
188 0 $self->{perror} = 1;
189 0 $self->{error} = 2;
190 $self->{errorString}
191 0 = $item . ' could not be resolved to a port name via getservbyname("' . $item . '", "tcp")';
192 0 $self->warn;
193 }
194 0 $ports{$port} = 1;
195 } ## end else [ if ( $item =~ /^[0-9]+$/ && $item >= 1 &&...)]
196 } ## end foreach my $item ( @{ $opts{ports} } )
197 0 my @port_keys = keys(%ports);
198 0 @port_keys = sort { $a <=> $b } @port_keys;
199 0 push( @{ $self->{ports} }, @port_keys );
200 } ## end elsif ( defined( $opts{ports} ) )
201  
202 0 ▶ if ( defined( $opts{protocols} ) && ref( $opts{protocols} ) ne 'ARRAY' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $opts{'protocols'} and ref $opts{'protocols'} ne 'ARRAY') { } 0 0 0
elsif (defined $opts{'protocols'}) { } 0 0 0
Condition0%0/3
defined $opts{"protocols"} and ref $opts{"protocols"} ne "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $opts{"protocols"} and ref $opts{"protocols"} ne "ARRAY"
defined $opts{"protocols"} ref $opts{"protocols"} ne "ARRAY"
Truth table0%0/3
A and B
A= defined $opts{"protocols"}B= ref $opts{"protocols"} ne "ARRAY"
ABresult
0X0
100
111
203 0 $self->{perror} = 1;
204 0 $self->{error} = 4;
205 0 $self->{errorString} = 'protocols is defined and type is not array but "' . ref( $opts{protocols} ) . '"';
206 0 $self->warn;
207 } elsif ( defined( $opts{protocols} ) ) {
208 0 my %protocols;
209 0 foreach my $item ( @{ $opts{protocols} } ) {
210 0 my ( $name, $aliases, $proto ) = getprotobyname($item);
211 # if this is undef, it means it is not a known protocol
212 0 ▶ if ( !defined($proto) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $proto) 0 0 0
213 0 $self->{perror} = 1;
214 0 $self->{error} = 5;
215 $self->{errorString}
216 0 = $item . ' could not be resolved to a protocol via getprotobyname("' . $item . '")';
217 0 $self->warn;
218 }
219 0 $protocols{$item} = 1;
220 } ## end foreach my $item ( @{ $opts{protocols} } )
221 0 my @protocols_keys = keys(%protocols);
222 0 @protocols_keys = sort { $a cmp $b } @protocols_keys;
223 0 push( @{ $self->{protocols} }, @protocols_keys );
224 } ## end elsif ( defined( $opts{protocols} ) )
225  
226 # make sure prefix is sane if defiend
227 0 ▶ if ( defined( $opts{prefix} ) && $opts{prefix} !~ /^[a-zA-Z0-9]+$/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $opts{'prefix'} and not $opts{'prefix'} =~ /^[a-zA-Z0-9]+$/) { } 0 0 0
elsif (defined $opts{'prefix'}) { } 0 0 0
Condition0%0/3
defined $opts{"prefix"} and not $opts{"prefix"} =~ /^[a-zA-Z0-9]+$/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $opts{"prefix"} and not $opts{"prefix"} =~ /^[a-zA-Z0-9]+$/
defined $opts{"prefix"} not $opts{"prefix"} =~ /^[a-zA-Z0-9]+$/
Truth table0%0/3
A and not(B)
A= defined $opts{"prefix"}B= not $opts{"prefix"} =~ /^[a-zA-Z0-9]+$/
ABresult
0X0
100
111
228 0 $self->{perror} = 1;
229 0 $self->{error} = 6;
230 $self->{errorString}
231 0 = '"' . $opts{prefix} . '" is not a valid prefix as it does not match the regex /^[a-zA-Z0-9]+$/';
232 0 $self->warn;
233 } elsif ( defined( $opts{prefix} ) ) {
234 0 $self->{prefix} = $opts{prefix};
235 }
236  
237 # make sure we have a name and that it is valid
238 0 ▶ if ( !defined( $opts{name} ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $opts{'name'}) { } 0 0 0
elsif (not $opts{'name'} =~ /^[a-zA-Z0-9\-]+$/) { } 0 0 0
239 0 $self->{perror} = 1;
240 0 $self->{error} = 7;
241 0 $self->{errorString} = 'name is undef';
242 0 $self->warn;
243 } elsif ( $opts{name} !~ /^[a-zA-Z0-9\-]+$/ ) {
244 0 $self->{perror} = 1;
245 0 $self->{error} = 7;
246 0 $self->{errorString} = 'name set to "' . $opts{name} . '" which does not match the regexp /^[a-zA-Z0-9\-]+$/';
247 0 $self->warn;
248 }
249 0 $self->{name} = $opts{name};
250  
251 # used internally for testing
252 0 ▶ if ( defined( $opts{testing} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"testing"}) 0 0 0
253 0 $self->{testing} = $opts{testing};
254 }
255 0 ▶ if ( defined( $opts{frontend_obj} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"frontend_obj"}) 0 0 0
256 0 $self->{frontend_obj} = $opts{frontend_obj};
257 }
258  
259 0 ▶ if ( defined( $opts{options} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"options"}) 0 0 0
260 0 ▶ if ( ref( $opts{options} ) ne 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $opts{"options"} ne "HASH") 0 0 0
261 0 $self->{perror} = 1;
262 0 $self->{error} = 8;
263 0 $self->{errorString} = 'ref for options is "' . ref( $opts{options} ) . '" and not HASH';
264 0 $self->warn;
265 }
266 0 $self->{options} = $opts{options};
267  
268 0 ▶ if ( defined( $opts{options}{type} ) && ref( $opts{options}{type} ) ne '' ) {
Branch: 3 branches
BranchTrueFalseTotal
if (defined $opts{'options'}{'type'} and ref $opts{'options'}{'type'} ne '') { } 0 0 0
elsif (defined $opts{'options'}{'type'} and $opts{'options'}{'type'} ne 'drop' and $opts{'options'}{'type'} ne 'reject') { } 0 0 0
elsif (not defined $opts{'options'}{'type'}) { } 0 0 0
Condition0%0/3
defined $opts{"options"}{"type"} and ref $opts{"options"}{"type"} ne ""
!ll&&!rl&&r
000
MC/DC0%0/2
defined $opts{"options"}{"type"} and ref $opts{"options"}{"type"} ne ""
defined $opts{"options"}{"type"} ref $opts{"options"}{"type"} ne ""
Truth table0%0/3
A and B
A= defined $opts{"options"}{"type"}B= ref $opts{"options"}{"type"} ne ""
ABresult
0X0
100
111
269 0 $self->{perror} = 1;
270 0 $self->{error} = 20;
271 0 $self->{errorString} = 'ref for $opts{options}{type} is "' . ref( $opts{options}{type} ) . '" and not ""';
272 0 $self->warn;
273 ▶ } elsif ( defined( $opts{options}{type} )
Condition0%0/6
defined $opts{"options"}{"type"} and $opts{"options"}{"type"} ne "drop" and $opts{"options"}{"type"} ne "reject"
!ll&&!rl&&r
000
defined $opts{"options"}{"type"} and $opts{"options"}{"type"} ne "drop"
!ll&&!rl&&r
000
MC/DC0%0/3
defined $opts{"options"}{"type"} and $opts{"options"}{"type"} ne "drop" and $opts{"options"}{"type"} ne "reject"
defined $opts{"options"}{"type"} $opts{"options"}{"type"} ne "drop" $opts{"options"}{"type"} ne "reject"
Truth table0%0/4
A and B and C
A= defined $opts{"options"}{"type"}B= $opts{"options"}{"type"} ne "drop"C= $opts{"options"}{"type"} ne "reject"
ABCresult
0XX0
10X0
1100
1111
274 && $opts{options}{type} ne 'drop'
275 && $opts{options}{type} ne 'reject' )
276 {
277 0 $self->{perror} = 1;
278 0 $self->{error} = 20;
279 $self->{errorString}
280 0 = '$opts{options}{type} is "' . $opts{options}{type} . '" and not "drop" or "reject"';
281 0 $self->warn;
282 } elsif ( !defined( $opts{options}{type} ) ) {
283 0 $self->{options}{type} = 'drop';
284 }
285  
286 0 ▶ if ( !defined( $opts{options}{kill} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $opts{"options"}{"kill"}) 0 0 0
287 0 $self->{options}{kill} = 0;
288 }
289  
290 0 ▶ if ( defined( $opts{options}{priority} ) && $opts{options}{priority} !~ /^\-?[0-9]+$/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $opts{'options'}{'priority'} and not $opts{'options'}{'priority'} =~ /^\-?[0-9]+$/) { } 0 0 0
elsif (not defined $opts{'options'}{'priority'}) { } 0 0 0
Condition0%0/3
defined $opts{"options"}{"priority"} and not $opts{"options"}{"priority"} =~ /^\-?[0-9]+$/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $opts{"options"}{"priority"} and not $opts{"options"}{"priority"} =~ /^\-?[0-9]+$/
defined $opts{"options"}{"priority"} not $opts{"options"}{"priority"} =~ /^\-?[0-9]+$/
Truth table0%0/3
A and not(B)
A= defined $opts{"options"}{"priority"}B= not $opts{"options"}{"priority"} =~ /^\-?[0-9]+$/
ABresult
0X0
100
111
291 0 $self->{perror} = 1;
292 0 $self->{error} = 19;
293 $self->{errorString}
294 0 = '$opts{options}{priority} is "' . $opts{options}{priority} . '" and not an int';
295 0 $self->warn;
296 } elsif ( !defined( $opts{options}{priority} ) ) {
297 0 $self->{options}{priority} = -1;
298 }
299 } ## end if ( defined( $opts{options} ) )
300  
301 0 return $self;
302 } ## end sub new
303  
304 # Internal helper. Returns the table/chain name, IPv4 set name, and IPv6 set
305 # name for this instance.
306 sub _names {
307 0 ▶ my ($self) = @_;
Subroutine _names: not called CC=1 SCAR=6.9
308  
309 0 my $table = $self->{prefix} . '_' . $self->{name};
310 0 return ( $table, $table . '_4', $table . '_6' );
311 }
312  
313 # Internal helper. Returns the list of rule statements that populate the
314 # chain, based on the configured protocols and ports, followed by a hash ref
315 # of which sets the rules reference (used by check).
316 sub _rule_specs {
317 0 ▶ my ($self) = @_;
Subroutine _rule_specs: not called CC=14 SCAR=53.5
318  
319 0 my ( $table, $set4, $set6 ) = $self->_names;
320  
321 0 my @ports = @{ $self->{ports} };
322 0 my @protos = @{ $self->{protocols} };
323 0 my $port_str = join( ', ', @ports );
324  
325 0 ▶ my $target = $self->{options}{type} eq 'reject' ? 'reject' : 'drop';
Branch: 1 branch
BranchTrueFalseTotal
$self->{'options'}{'type'} eq 'reject' ? : 0 0 0
326  
327 # protocols that accept a port specification
328 0 my %port_ok = ( tcp => 1, udp => 1, sctp => 1 );
329  
330 # the various names the IPv6 ICMP protocol may go by
331 0 my %v6_icmp = ( 'ipv6-icmp' => 1, 'icmp6' => 1, 'icmpv6' => 1 );
332  
333 0 my @families = (
334 { set => $set4, saddr => 'ip', family => 4 },
335 { set => $set6, saddr => 'ip6', family => 6 },
336 );
337  
338 0 my @specs;
339 my %referenced;
340 0 foreach my $fam (@families) {
341 0 my $match = $fam->{saddr} . ' saddr @' . $fam->{set};
342  
343 0 ▶ if ( !@protos && !@ports ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not @protos || @ports) { } 0 0 0
elsif (not @protos and @ports) { } 0 0 0
Condition0%0/3
@protos || @ports
l!l&&r!l&&!r
000
MC/DC0%0/2
@protos || @ports
@protos @ports
Truth table0%0/3
A || B
A= @protosB= @ports
ABresult
1X1
011
000
344 # block everything sourced from the set
345 0 push( @specs, $match . ' ' . $target );
346 0 $referenced{ $fam->{set} } = 1;
347 ▶ } elsif ( !@protos && @ports ) {
Condition0%0/3
not @protos and @ports
!ll&&!rl&&r
000
MC/DC0%0/2
not @protos and @ports
not @protos @ports
Truth table0%0/3
not(A) and B
A= not @protosB= @ports
ABresult
0X0
100
111
348 # ports require a protocol, default to tcp and udp
349 0 foreach my $proto ( 'tcp', 'udp' ) {
350 0 push( @specs, $proto . ' dport { ' . $port_str . ' } ' . $match . ' ' . $target );
351 0 $referenced{ $fam->{set} } = 1;
352 }
353 } else {
354 0 foreach my $proto (@protos) {
355 # skip protocols that do not belong to this family
356 0 ▶ if ( $fam->{family} == 6 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fam->{'family'} == 6) { } 0 0 0
357 0 ▶ next if ( $proto eq 'icmp' );
Branch: 1 branch
BranchTrueFalseTotal
if $proto eq "icmp" 0 0 0
358 } else {
359 0 ▶ next if ( $v6_icmp{$proto} );
Branch: 1 branch
BranchTrueFalseTotal
if $v6_icmp{$proto} 0 0 0
360 }
361  
362 0 ▶ if ( @ports && $port_ok{$proto} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@ports and $port_ok{$proto}) { } 0 0 0
Condition0%0/3
@ports and $port_ok{$proto}
!ll&&!rl&&r
000
MC/DC0%0/2
@ports and $port_ok{$proto}
@ports $port_ok{$proto}
Truth table0%0/3
A and B
A= @portsB= $port_ok{$proto}
ABresult
0X0
100
111
363 0 push( @specs, $proto . ' dport { ' . $port_str . ' } ' . $match . ' ' . $target );
364 } else {
365 0 push( @specs, 'meta l4proto ' . $proto . ' ' . $match . ' ' . $target );
366 }
367 0 $referenced{ $fam->{set} } = 1;
368 } ## end foreach my $proto (@protos)
369 } ## end else [ if ( !@protos && !@ports ) ]
370 } ## end foreach my $fam (@families)
371  
372 0 return ( \@specs, \%referenced );
373 } ## end sub _rule_specs
374  
375 # Internal helper. Returns the conntrack commands used to drop existing
376 # connection tracking entries for the passed IP. When protocols are
377 # configured, the kill is scoped to them via -p so protocols that are not
378 # being blocked are left alone; otherwise every entry for the IP is dropped.
379 sub _kill_commands {
380 0 ▶ my ( $self, $ip ) = @_;
Subroutine _kill_commands: not called CC=13 SCAR=52.0
381  
382 # conntrack defaults to IPv4, so IPv6 IPs need the family specified
383 0 ▶ my $is_v4 = ( $ip =~ /\A$IPv4_re\z/ ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$ip =~ /\A$Net::Firewall::BlockerHelper::backends::nftables::IPv4_re\z/ ? : 0 0 0
384 0 ▶ my $family = $is_v4 ? '' : '-f ipv6 ';
Branch: 1 branch
BranchTrueFalseTotal
$is_v4 ? : 0 0 0
385  
386 0 my @protos = @{ $self->{protocols} };
387 0 ▶ if ( !@protos && defined( $self->{ports}[0] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not @protos and defined $self->{"ports"}[0]) 0 0 0
Condition0%0/3
not @protos and defined $self->{"ports"}[0]
!ll&&!rl&&r
000
MC/DC0%0/2
not @protos and defined $self->{"ports"}[0]
not @protos defined $self->{"ports"}[0]
Truth table0%0/3
not(A) and B
A= not @protosB= defined $self->{"ports"}[0]
ABresult
0X0
100
111
388 # ports without protocols means tcp and udp are being blocked
389 0 @protos = ( 'tcp', 'udp' );
390 }
391  
392 # nothing configured means everything is being blocked, so drop every
393 # entry for the IP
394 0 ▶ if ( !@protos ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@protos) 0 0 0
395 0 return ( 'conntrack ' . $family . '-D -s ' . $ip );
396 }
397  
398 # scope the kill to the blocked protocols; ones conntrack can not filter
399 # by are skipped as are the icmps of the wrong family
400 0 my %conntrack_protos = (
401 tcp => 'tcp',
402 udp => 'udp',
403 udplite => 'udplite',
404 sctp => 'sctp',
405 dccp => 'dccp',
406 gre => 'gre',
407 icmp => 'icmp',
408 icmpv6 => 'icmpv6',
409 icmp6 => 'icmpv6',
410 'ipv6-icmp' => 'icmpv6',
411 );
412  
413 0 my @commands;
414 my %seen;
415 0 foreach my $proto (@protos) {
416 0 my $conntrack_proto = $conntrack_protos{$proto};
417 0 ▶ next if ( !defined($conntrack_proto) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $conntrack_proto 0 0 0
418 0 ▶ next if ( $conntrack_proto eq 'icmp' && !$is_v4 );
Branch: 1 branch
BranchTrueFalseTotal
if $conntrack_proto eq "icmp" and not $is_v4 0 0 0
Condition0%0/3
$conntrack_proto eq "icmp" and not $is_v4
!ll&&!rl&&r
000
MC/DC0%0/2
$conntrack_proto eq "icmp" and not $is_v4
$conntrack_proto eq "icmp" not $is_v4
Truth table0%0/3
A and not(B)
A= $conntrack_proto eq "icmp"B= not $is_v4
ABresult
0X0
100
111
419 0 ▶ next if ( $conntrack_proto eq 'icmpv6' && $is_v4 );
Branch: 1 branch
BranchTrueFalseTotal
if $conntrack_proto eq "icmpv6" and $is_v4 0 0 0
Condition0%0/3
$conntrack_proto eq "icmpv6" and $is_v4
!ll&&!rl&&r
000
MC/DC0%0/2
$conntrack_proto eq "icmpv6" and $is_v4
$conntrack_proto eq "icmpv6" $is_v4
Truth table0%0/3
A and B
A= $conntrack_proto eq "icmpv6"B= $is_v4
ABresult
0X0
100
111
420 0 ▶ next if ( $seen{$conntrack_proto} );
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$conntrack_proto} 0 0 0
421 0 $seen{$conntrack_proto} = 1;
422 0 push( @commands, 'conntrack ' . $family . '-D -p ' . $conntrack_proto . ' -s ' . $ip );
423 }
424  
425 0 return @commands;
426 } ## end sub _kill_commands
427  
428 =head2 init
429  
430 Initiates the backend. First a best effort C<nft delete table> is run to
431 clear any stale copy of the table, then the table
432 C<< inet <prefix>_<name> >>, a base chain of the same name hooked to input
433 at the configured priority, the IPv4 set (C<< <prefix>_<name>_4 >>), the
434 IPv6 set (C<< <prefix>_<name>_6 >>), and the block rules referencing the
435 sets are created via C<nft add>. A failure of any of the creation commands
436 raises an error.
437  
438 $backend->init;
439  
440 =cut
441  
442 sub init {
443 0 ▶ my ( $self, %opts ) = @_;
Subroutine init: not called CC=9 SCAR=45.0
Pod documented
444  
445 0 $self->errorblank;
446  
447 0 ▶ if ( $self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"inited"}) 0 0 0
448 0 $self->{error} = 18;
449 0 $self->{errorString} = 'backend has already been inited';
450 0 $self->warn;
451 }
452  
453 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
454 0 $self->{frontend_obj}->{test_data} = {};
455 }
456  
457 0 my ( $table, $set4, $set6 ) = $self->_names;
458  
459 # stale state cleanup, allowed to fail
460 0 my @fail_okay_commands = ( "nft 'delete table inet " . $table . "'" );
461  
462 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
463 0 $self->{frontend_obj}->{test_data}{fail_okay_commands} = \@fail_okay_commands;
464 } else {
465 0 foreach my $item (@fail_okay_commands) {
466 0 my $output = `$item 2>&1`;
467 }
468 }
469  
470 0 my ($specs) = $self->_rule_specs;
471  
472 0 my @commands;
473 0 push( @commands, "nft 'add table inet " . $table . "'" );
474 push( @commands,
475 "nft 'add chain inet "
476 . $table . ' '
477 . $table
478 . ' { type filter hook input priority '
479 . $self->{options}{priority}
480 0 . " ; policy accept ; }'" );
481 0 push( @commands, "nft 'add set inet " . $table . ' ' . $set4 . " { type ipv4_addr; }'" );
482 0 push( @commands, "nft 'add set inet " . $table . ' ' . $set6 . " { type ipv6_addr; }'" );
483 0 foreach my $spec ( @{$specs} ) {
484 0 push( @commands, "nft 'add rule inet " . $table . ' ' . $table . ' ' . $spec . "'" );
485 }
486  
487 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
488 0 $self->{frontend_obj}->{test_data}{commands} = \@commands;
489 } else {
490 0 foreach my $item (@commands) {
491 0 my $output = `$item 2>&1`;
492 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
493 0 $self->{error} = 23;
494 $self->{errorString}
495 0 = 'init failed. non-zero exit code for the command... "' . $item . '"... output... ' . $output;
496 0 $self->warn;
497 }
498 }
499 } ## end else [ if ( $self->{testing} ) ]
500  
501 0 $self->{inited} = 1;
502 } ## end sub init
503  
504 =head2 ban
505  
506 Bans an IP. The value of ban is validated as being a IPv4 or IPv6 address
507 and lowercased, then added to the family-appropriate set via
508 C<nft add element>. If the C<kill> option is set, conntrack(8) is then used
509 to drop existing connection tracking entries for the IP. Banning an already
510 banned IP is a noop.
511  
512 $backend->ban(ban => $ip);
513  
514 =cut
515  
516 sub ban {
517 0 ▶ my ( $self, %opts ) = @_;
Subroutine ban: not called CC=14 SCAR=53.5
Pod documented
518  
519 0 $self->errorblank;
520  
521 0 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 0 0
522 0 $self->{error} = 1;
523 0 $self->{errorString} = 'backend has not been inited';
524 0 $self->warn;
525 0 return;
526 }
527  
528 0 ▶ if ( !defined( $opts{ban} ) ) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $opts{'ban'}) { } 0 0 0
elsif (ref $opts{'ban'} ne '') { } 0 0 0
elsif (not $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::nftables::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::nftables::IPv6_re\z/) { } 0 0 0
529 0 $self->{error} = 9;
530 0 $self->{errorString} = 'Nothing specified for the value ban';
531 0 $self->warn;
532 0 return;
533 } elsif ( ref( $opts{ban} ) ne '' ) {
534 0 $self->{error} = 10;
535 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
536 0 $self->warn;
537 0 return;
538 ▶ } elsif ( $opts{ban} !~ /\A$IPv4_re\z/
Condition0%0/3
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::nftables::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::nftables::IPv6_re\z/
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::nftables::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::nftables::IPv6_re\z/
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::nftables::IPv4_re\z/ $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::nftables::IPv6_re\z/
Truth table0%0/3
A || B
A= $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::nftables::IPv4_re\z/B= $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::nftables::IPv6_re\z/
ABresult
1X1
011
000
539 && $opts{ban} !~ /\A$IPv6_re\z/ )
540 {
541 0 $self->{error} = 10;
542 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 IP';
543 0 $self->warn;
544 0 return;
545 }
546  
547 # lowercase so the same IPv6 IP in differing cases can't result in duplicate entries
548 0 $opts{ban} = lc( $opts{ban} );
549  
550 0 ▶ if ( $self->{banned}{ $opts{ban} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"banned"}{$opts{"ban"}}) 0 0 0
551 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
552 0 $self->{frontend_obj}->{test_data} = 'already banned';
553 }
554 0 return;
555 }
556  
557 0 my ( $table, $set4, $set6 ) = $self->_names;
558 0 ▶ my $set = ( $opts{ban} =~ /\A$IPv4_re\z/ ) ? $set4 : $set6;
Branch: 1 branch
BranchTrueFalseTotal
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::nftables::IPv4_re\z/ ? : 0 0 0
559  
560 0 my $command = "nft 'add element inet " . $table . ' ' . $set . ' { ' . $opts{ban} . " }'";
561  
562 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
563 0 $self->{frontend_obj}->{test_data} = [$command];
564 } else {
565 0 my $output = `$command 2>&1`;
566 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
567 0 $self->{error} = 13;
568 $self->{errorString}
569 0 = 'ban failed. non-zero exit code for the command... "' . $command . '"... output... ' . $output;
570 0 $self->warn;
571 }
572 }
573  
574 0 ▶ if ( $self->{options}{kill} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"options"}{"kill"}) 0 0 0
575 0 foreach my $kill_command ( $self->_kill_commands( $opts{ban} ) ) {
576 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
577 0 push( @{ $self->{frontend_obj}->{test_data} }, $kill_command );
578 } else {
579 # conntrack returns non-zero when there is nothing to delete,
580 # so its exit code is intentionally ignored
581 0 my $output = `$kill_command 2>&1`;
582 }
583 }
584 } ## end if ( $self->{options}{kill} )
585  
586 0 $self->{banned}{ $opts{ban} } = 1;
587 } ## end sub ban
588  
589 =head2 unban
590  
591 Unbans an IP. The value of ban is validated as being a IPv4 or IPv6 address
592 and lowercased, then removed from the family-appropriate set via
593 C<nft delete element>. Unbanning an IP that is not banned is a noop.
594  
595 $backend->unban(ban => $ip);
596  
597 =cut
598  
599 sub unban {
600 0 ▶ my ( $self, %opts ) = @_;
Subroutine unban: not called CC=11 SCAR=48.8
Pod documented
601  
602 0 $self->errorblank;
603  
604 0 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 0 0
605 0 $self->{error} = 1;
606 0 $self->{errorString} = 'backend has not been inited';
607 0 $self->warn;
608 0 return;
609 }
610  
611 0 ▶ if ( !defined( $opts{ban} ) ) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $opts{'ban'}) { } 0 0 0
elsif (ref $opts{'ban'} ne '') { } 0 0 0
elsif (not $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::nftables::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::nftables::IPv6_re\z/) { } 0 0 0
612 0 $self->{error} = 9;
613 0 $self->{errorString} = 'Nothing specified for the value ban';
614 0 $self->warn;
615 0 return;
616 } elsif ( ref( $opts{ban} ) ne '' ) {
617 0 $self->{error} = 10;
618 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
619 0 $self->warn;
620 0 return;
621 ▶ } elsif ( $opts{ban} !~ /\A$IPv4_re\z/
Condition0%0/3
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::nftables::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::nftables::IPv6_re\z/
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::nftables::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::nftables::IPv6_re\z/
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::nftables::IPv4_re\z/ $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::nftables::IPv6_re\z/
Truth table0%0/3
A || B
A= $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::nftables::IPv4_re\z/B= $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::nftables::IPv6_re\z/
ABresult
1X1
011
000
622 && $opts{ban} !~ /\A$IPv6_re\z/ )
623 {
624 0 $self->{error} = 10;
625 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 IP';
626 0 $self->warn;
627 0 return;
628 }
629  
630 # lowercase so the same IPv6 IP in differing cases can't result in duplicate entries
631 0 $opts{ban} = lc( $opts{ban} );
632  
633 0 ▶ if ( !$self->{banned}{ $opts{ban} } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"banned"}{$opts{"ban"}}) 0 0 0
634 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
635 0 $self->{frontend_obj}->{test_data} = 'not banned';
636 }
637 0 return;
638 }
639  
640 0 my ( $table, $set4, $set6 ) = $self->_names;
641 0 ▶ my $set = ( $opts{ban} =~ /\A$IPv4_re\z/ ) ? $set4 : $set6;
Branch: 1 branch
BranchTrueFalseTotal
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::nftables::IPv4_re\z/ ? : 0 0 0
642  
643 0 my $command = "nft 'delete element inet " . $table . ' ' . $set . ' { ' . $opts{ban} . " }'";
644  
645 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
646 0 $self->{frontend_obj}->{test_data} = $command;
647 } else {
648 0 my $output = `$command 2>&1`;
649 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
650 0 $self->{error} = 14;
651 $self->{errorString}
652 0 = 'unban failed. non-zero exit code for the command... "' . $command . '"... output... ' . $output;
653 0 $self->warn;
654 }
655 }
656  
657 0 delete( $self->{banned}{ $opts{ban} } );
658 } ## end sub unban
659  
660 =head2 list
661  
662 List banned IPs. Returns an array of the currently banned IPs from the
663 retained in-memory state; the sets are not queried.
664  
665 my @banned = $backend->list;
666  
667 =cut
668  
669 sub list {
670 0 ▶ my ( $self, %opts ) = @_;
Subroutine list: not called CC=2 SCAR=17.9
Pod documented
671  
672 0 $self->errorblank;
673  
674 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
675 0 $self->{frontend_obj}->{test_data} = 'list';
676 }
677  
678 0 return keys( %{ $self->{banned} } );
679 }
680  
681 =head2 re_init
682  
683 Tears down and re-initiates the backend, recreating the table, chain, sets,
684 and rules, then re-adds every previously banned IP to the relevant set via
685 C<nft add element>. The teardown is best effort as a partially or fully
686 wiped setup is exactly what this needs to recover from.
687  
688 $backend->re_init;
689  
690 =cut
691  
692 sub re_init {
693 0 ▶ my ( $self, %opts ) = @_;
Subroutine re_init: not called CC=7 SCAR=40.3
Pod documented
694  
695 0 $self->errorblank;
696  
697 0 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 0 0
698 0 $self->{error} = 1;
699 0 $self->{errorString} = 'backend has not been inited';
700 0 $self->warn;
701 0 return;
702 }
703  
704 # teardown is best effort here as a partially or fully wiped setup is
705 # exactly what re_init needs to recover from; init cleans up any remnants
706 {
707 0 local $@;
708 0 eval { $self->teardown; };
709 }
710 0 $self->init;
711  
712 0 my ( $table, $set4, $set6 ) = $self->_names;
713  
714 0 my @to_ban = keys( %{ $self->{banned} } );
715  
716 0 my @re_init_test_data;
717 0 foreach my $item (@to_ban) {
718 0 ▶ my $set = ( $item =~ /\A$IPv4_re\z/ ) ? $set4 : $set6;
Branch: 1 branch
BranchTrueFalseTotal
$item =~ /\A$Net::Firewall::BlockerHelper::backends::nftables::IPv4_re\z/ ? : 0 0 0
719 0 my $command = "nft 'add element inet " . $table . ' ' . $set . ' { ' . $item . " }'";
720  
721 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
722 0 push( @re_init_test_data, $command );
723 } else {
724 0 my $output = `$command 2>&1`;
725 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
726 0 $self->{error} = 13;
727 $self->{errorString}
728 0 = 'ban failed. non-zero exit code for the command... "' . $command . '"... output... ' . $output;
729 0 $self->warn;
730 }
731 }
732 } ## end foreach my $item (@to_ban)
733  
734 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
735 0 $self->{frontend_obj}->{test_data} = \@re_init_test_data;
736 }
737  
738 0 $self->{inited} = 1;
739 } ## end sub re_init
740  
741 =head2 teardown
742  
743 Tears down the setup for the backend by deleting the table, which removes
744 the chain, sets, and rules with it.
745  
746 $backend->teardown;
747  
748 =cut
749  
750 sub teardown {
751 0 ▶ my ( $self, %opts ) = @_;
Subroutine teardown: not called CC=4 SCAR=30.0
Pod documented
752  
753 0 $self->errorblank;
754  
755 0 $self->{inited} = 0;
756  
757 0 my ( $table, $set4, $set6 ) = $self->_names;
758  
759 0 my @commands = ( "nft 'delete table inet " . $table . "'" );
760  
761 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
762 0 $self->{frontend_obj}->{test_data} = \@commands;
763 } else {
764 0 foreach my $item (@commands) {
765 0 my $output = `$item 2>&1`;
766 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
767 0 $self->{error} = 17;
768 $self->{errorString}
769 0 = 'teardown failed. non-zero exit code for the command... "' . $item . '"... output... ' . $output;
770 0 $self->warn;
771 }
772 }
773 } ## end else [ if ( $self->{testing} ) ]
774 } ## end sub teardown
775  
776 =head2 stop
777  
778 Alias for L</teardown>, provided for parity with the fail2ban C<actionstop>
779 concept.
780  
781 $backend->stop;
782  
783 =cut
784  
785 sub stop {
786 0 ▶ my ( $self, %opts ) = @_;
Subroutine stop: not called CC=1 SCAR=6.9
Pod documented
787  
788 0 return $self->teardown(%opts);
789 }
790  
791 =head2 check
792  
793 Verifies that the table is still present and that the chain still contains
794 rules referencing each set the block rules use. Returns a true value if the
795 setup is intact and a false value if any part of it appears to have been
796 removed. This is the equivalent of fail2ban's C<actioncheck>.
797  
798 if ( !$backend->check ) {
799 $backend->re_init;
800 }
801  
802 =cut
803  
804 sub check {
805 0 ▶ my ( $self, %opts ) = @_;
Subroutine check: not called CC=5 SCAR=34.0
Pod documented
806  
807 0 $self->errorblank;
808  
809 0 my ( $table, $set4, $set6 ) = $self->_names;
810  
811 0 my ( $specs, $referenced ) = $self->_rule_specs;
812  
813 0 my @commands = ( "nft 'list table inet " . $table . "'" );
814 0 foreach my $set ( sort( keys( %{$referenced} ) ) ) {
815 0 push( @commands, "nft 'list chain inet " . $table . ' ' . $table . "' | grep -q '\@" . $set . "'" );
816 }
817  
818 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
819 0 $self->{frontend_obj}->{test_data} = \@commands;
820 0 return 1;
821 }
822  
823 0 foreach my $item (@commands) {
824 0 my $output = `$item 2>&1`;
825 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
826 0 return 0;
827 }
828 }
829  
830 0 return 1;
831 } ## end sub check
832  
833 =head2 flush
834  
835 Removes all currently banned IPs at once by flushing both sets, leaving the
836 table, chain, and rules in place. This is the equivalent of fail2ban's
837 C<actionflush>.
838  
839 $backend->flush;
840  
841 =cut
842  
843 sub flush {
844 0 ▶ my ( $self, %opts ) = @_;
Subroutine flush: not called CC=5 SCAR=34.0
Pod documented
845  
846 0 $self->errorblank;
847  
848 0 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 0 0
849 0 $self->{error} = 1;
850 0 $self->{errorString} = 'backend has not been inited';
851 0 $self->warn;
852 0 return;
853 }
854  
855 0 my ( $table, $set4, $set6 ) = $self->_names;
856  
857 0 my @commands = (
858 "nft 'flush set inet " . $table . ' ' . $set4 . "'",
859 "nft 'flush set inet " . $table . ' ' . $set6 . "'",
860 );
861  
862 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
863 0 $self->{frontend_obj}->{test_data} = \@commands;
864 } else {
865 0 foreach my $item (@commands) {
866 0 my $output = `$item 2>&1`;
867 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
868 0 $self->{error} = 25;
869 $self->{errorString}
870 0 = 'flush failed. non-zero exit code for the command... "' . $item . '"... output... ' . $output;
871 0 $self->warn;
872 }
873 }
874 } ## end else [ if ( $self->{testing} ) ]
875  
876 0 $self->{banned} = {};
877 } ## end sub flush
878  
879 # Internal helper. Returns a true value if the passed scalar is a valid IPv4 or
880 # IPv6 CIDR range, that is an address followed by "/" and a prefix length that
881 # is within the range valid for its family (0 to 32 for IPv4, 0 to 128 for
882 # IPv6). Returns false otherwise.
883 sub _valid_cidr {
884 0 ▶ my ( $self, $cidr ) = @_;
Subroutine _valid_cidr: not called CC=8 SCAR=42.8
885  
886 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
887  
888 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
889 0 my ( $addr, $prefix ) = ( $1, $2 );
890 0 ▶ return 1 if ( $addr =~ /\A$IPv4_re\z/ && $prefix <= 32 );
Branch: 1 branch
BranchTrueFalseTotal
if $addr =~ /\A$Net::Firewall::BlockerHelper::backends::nftables::IPv4_re\z/ and $prefix <= 32 0 0 0
Condition0%0/3
$addr =~ /\A$Net::Firewall::BlockerHelper::backends::nftables::IPv4_re\z/ and $prefix <= 32
!ll&&!rl&&r
000
MC/DC0%0/2
$addr =~ /\A$Net::Firewall::BlockerHelper::backends::nftables::IPv4_re\z/ and $prefix <= 32
$addr =~ /\A$Net::Firewall::BlockerHelper::backends::nftables::IPv4_re\z/ $prefix <= 32
Truth table0%0/3
A and B
A= $addr =~ /\A$Net::Firewall::BlockerHelper::backends::nftables::IPv4_re\z/B= $prefix <= 32
ABresult
0X0
100
111
891 0 ▶ return 1 if ( $addr =~ /\A$IPv6_re\z/ && $prefix <= 128 );
Branch: 1 branch
BranchTrueFalseTotal
if $addr =~ /\A$Net::Firewall::BlockerHelper::backends::nftables::IPv6_re\z/ and $prefix <= 128 0 0 0
Condition0%0/3
$addr =~ /\A$Net::Firewall::BlockerHelper::backends::nftables::IPv6_re\z/ and $prefix <= 128
!ll&&!rl&&r
000
MC/DC0%0/2
$addr =~ /\A$Net::Firewall::BlockerHelper::backends::nftables::IPv6_re\z/ and $prefix <= 128
$addr =~ /\A$Net::Firewall::BlockerHelper::backends::nftables::IPv6_re\z/ $prefix <= 128
Truth table0%0/3
A and B
A= $addr =~ /\A$Net::Firewall::BlockerHelper::backends::nftables::IPv6_re\z/B= $prefix <= 128
ABresult
0X0
100
111
892 }
893  
894 0 return 0;
895 } ## end sub _valid_cidr
896  
897 =head2 ban_cidr
898  
899 CIDR bans are not supported by this backend; this always sets the
900 cidrNotSupported error.
901  
902 $backend->ban_cidr(ban => '1.2.3.0/24');
903  
904 =cut
905  
906 sub ban_cidr {
907 0 ▶ my ( $self, %opts ) = @_;
Subroutine ban_cidr: not called CC=1 SCAR=6.9
Pod documented
908  
909 0 $self->errorblank;
910  
911 0 $self->{error} = 29;
912 0 $self->{errorString} = 'the ' . __PACKAGE__ . ' backend does not support CIDR bans';
913 0 $self->warn;
914  
915 0 return;
916 } ## end sub ban_cidr
917  
918 =head2 unban_cidr
919  
920 CIDR bans are not supported by this backend; this always sets the
921 cidrNotSupported error.
922  
923 $backend->unban_cidr(ban => '1.2.3.0/24');
924  
925 =cut
926  
927 sub unban_cidr {
928 0 ▶ my ( $self, %opts ) = @_;
Subroutine unban_cidr: not called CC=1 SCAR=6.9
Pod documented
929  
930 0 $self->errorblank;
931  
932 0 $self->{error} = 29;
933 0 $self->{errorString} = 'the ' . __PACKAGE__ . ' backend does not support CIDR bans';
934 0 $self->warn;
935  
936 0 return;
937 } ## end sub unban_cidr
938  
939 =head2 list_cidr
940  
941 CIDR bans are not supported by this backend, so this always returns an empty
942 list.
943  
944 my @banned_cidrs = $backend->list_cidr;
945  
946 =cut
947  
948 sub list_cidr {
949 0 ▶ my ( $self, %opts ) = @_;
Subroutine list_cidr: not called CC=1 SCAR=6.9
Pod documented
950  
951 0 $self->errorblank;
952  
953 0 return ();
954 }
955  
956 =head1 ERROR CODES / FLAGS
957  
958 Error handling is provided by L<Error::Helper>. All
959 errors are considered fatal.
960  
961 =head2 1, notInited
962  
963 The backend has not been inited yet.
964  
965 =head2 2, invalidPortSpecified
966  
967 Port is either not an int within the range 1 to 65535 or a name that can be resolved by getservbyname.
968  
969 =head2 3, portsNotArray
970  
971 The data passed to new for ports is not an array.
972  
973 =head2 4, protocolsNotArray
974  
975 The data passed to new for protocols is not an array.
976  
977 =head2 5, invalidPortSpecified
978  
979 Port is either not an int within the range 1 to 65535 or a name that can be resolved by getservbyname.
980  
981 =head2 6, invalidPrefixSpecified
982  
983 The specified prefix did not match /^[a-zA-Z0-9]+$/.
984  
985 =head2 7, invalidName
986  
987 The name is either undef or does not match /^[a-zA-Z0-9\-]+$/.
988  
989 =head2 8, optionsNotHash
990  
991 The item passed to new for options is not a hash.
992  
993 =head2 9, noBanItem
994  
995 No IP specified to ban or unban.
996  
997 =head2 10, banItemNotIP
998  
999 The item to ban is not an IP. Either wrong ref type or regexp
1000 test using L<Regexp::IPv4> and L<Regexp::IPv6> failed.
1001  
1002 =head2 12, backendInitError
1003  
1004 Failed to init the backend.
1005  
1006 =head2 13, banFailed
1007  
1008 Failed to ban the item.
1009  
1010 =head2 14, unbanFailed
1011  
1012 Failed to unban the item.
1013  
1014 =head2 15, listFailed
1015  
1016 Failed to get a list of bans.
1017  
1018 =head2 16, reInitFailed
1019  
1020 Failed to re_init the backend.
1021  
1022 =head2 17, teardownFailed
1023  
1024 Failed to teardown the backend.
1025  
1026 =head2 18, alreadyInited
1027  
1028 init called, but the backend has already been inited.
1029  
1030 =head2 19, priorityInvalid
1031  
1032 The option priority is not an int.
1033  
1034 =head2 20, typeInvalid
1035  
1036 The option type is not "drop" or "reject".
1037  
1038 =head2 23, initFailed
1039  
1040 One of the required commands for init failed.
1041  
1042 =head2 24, checkFailed
1043  
1044 The backend check raised an error.
1045  
1046 =head2 25, flushFailed
1047  
1048 Failed to flush the bans.
1049  
1050 =head2 26, banCidrFailed
1051  
1052 Failed to ban the CIDR range.
1053  
1054 =head2 27, unbanCidrFailed
1055  
1056 Failed to unban the CIDR range.
1057  
1058 =head2 28, cidrItemNotCidr
1059  
1060 The item to ban is not a CIDR range. Either wrong ref type or it is not an
1061 IPv4 or IPv6 address followed by a prefix length valid for its family.
1062  
1063 =head2 29, cidrNotSupported
1064  
1065 The backend does not support CIDR bans.
1066  
1067 =head2 30, listCidrFailed
1068  
1069 Failed to get a list of CIDR bans.
1070  
1071 =head1 AUTHOR
1072  
1073 Zane C. Bowers-Hadley, C<< <vvelox at vvelox.ent> >>
1074  
1075 =head1 BUGS
1076  
1077 Please report any bugs or feature requests to C<bug-net-firewall-blockerhelper at rt.cpan.org>, or through
1078 the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Net-Firewall-BlockerHelper>.
1079  
1080 =head1 LICENSE AND COPYRIGHT
1081  
1082 This software is Copyright (c) 2023 by Zane C. Bowers-Hadley.
1083  
1084 This is free software, licensed under:
1085  
1086 The GNU Lesser General Public License, Version 2.1, February 1999
1087  
1088  
1089 =cut
1090  
1091 1 1; # End of Net::Firewall::BlockerHelper
« BlockerHelper/backends/ufw.pm ↑ Summary BlockerHelper/backends/firewalld.pm »