BlockerHelper/backends/firewalld.pm

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