BlockerHelper/backends/firewalld.pm

filter: Statementstmt 10.1% 37 / 364 Branchbran 9.7% 13 / 134 Conditioncond 11.1% 5 / 45 MC/DCmcdc 3.5% 1 / 28 Subroutinesub 33.3% 7 / 21 Podpod 100% 13 / 13 totaltotal 12.5% 76 / 605 CC 99 SCAR 56.5
CC
99
Cov
10%
new
688.3
_rule_args
210.0
ban
210.0
CRAP 285.2
« BlockerHelper/backends/openwrt.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 qw( Error::Helper Net::Firewall::BlockerHelper::Util );
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.2.0
17  
18 =cut
19  
20 2 our $VERSION = '0.2.0';
21  
22 =head1 SYNOPSIS
23  
24 use Net::Firewall::BlockerHelper;
25  
26 my $fw_helper = Net::Firewall::BlockerHelper->new(
27 backend => '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 2 ▶sub new {
Subroutine new: called CC=36 SCAR=65.3
Pod documented
109 2 my ( $blank, %opts ) = @_;
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 names of the two ipsets this instance owns.
325 #
326 # Both are derived from the prefix and the name rather than stored, so every
327 # part of the backend agrees on what to create, match against, and tear down
328 # without threading the names around. The prefix and name pair is unique per
329 # instance, so two instances can share a host without colliding.
330 #
331 # There are two sets because an ipset is single family: a hash:ip set is
332 # created either family inet or family inet6 and will not hold addresses of
333 # the other. Note firewalld manages the sets itself, via firewall-cmd
334 # --new-ipset, rather than the ipset command being called directly.
335 #
336 # Unlike the iptables backend there is no chain name here. This backend adds
337 # its rules through firewalld's direct interface rather than owning a chain of
338 # its own, so there is nothing else to name.
339 #
340 # Takes no arguments.
341 #
342 # Returns a two element list of the IPv4 set name and the IPv6 set name, in
343 # that order.
344 #
345 # my ( $set4, $set6 ) = $self->_set_names;
346 #
347 # # with prefix "kur" and name "ssh"
348 # # $set4 is 'kur_ssh_4'
349 # # $set6 is 'kur_ssh_6'
350 0 ▶sub _set_names {
Subroutine _set_names: not called CC=1 SCAR=6.9
351 0 my ($self) = @_;
352  
353 0 my $base = $self->{prefix} . '_' . $self->{name};
354 0 return ( $base . '_4', $base . '_6' );
355 }
356  
357 # Internal helper. Works out the full cross product of family, protocol, and
358 # ports this instance needs to block, and returns it as the argument strings
359 # for firewalld's direct interface. Essentially all of the backend's rule
360 # logic lives here.
361 #
362 # The rules go in through firewall-cmd --direct --add-rule, which takes a
363 # family and then passes the rest through to iptables more or less verbatim.
364 # That is why the argument strings are iptables syntax rather than anything
365 # firewalld specific, and why the family has to be handed over separately as
366 # 'ipv4' or 'ipv6' instead of being implied by which binary is invoked.
367 #
368 # The target is chosen per family because reject needs a different ICMP type
369 # for each. Protocols belonging to the wrong family are skipped, so icmp never
370 # lands in an ipv6 rule and the various spellings of IPv6 ICMP never land in
371 # an ipv4 one. Ports are only attached to protocols that actually have them.
372 #
373 # Unlike the iptables backend there is no tarpit or delude here, so there is
374 # no TCP only restriction and no conntrack exemption to generate.
375 #
376 # Takes no arguments; family, protocols, ports, and type all come from the
377 # object.
378 #
379 # Returns a list of hashrefs, one per rule to be created, each of the form:
380 #
381 # {
382 # fam => 'ipv4', # or 'ipv6', for the --direct family argument
383 # args => '-m set --match-set kur_ssh_4 src -j DROP',
384 # }
385 #
386 # The args are the complete rule body after the chain, ready to be appended to
387 # a --direct --add-rule invocation. The list is empty only if every protocol
388 # was filtered out.
389 #
390 # # no protocols and no ports, type drop
391 # my @rules = $self->_rule_args;
392 # # { fam => 'ipv4', args => '-m set --match-set kur_ssh_4 src -j DROP' }
393 # # { fam => 'ipv6', args => '-m set --match-set kur_ssh_6 src -j DROP' }
394 #
395 # # type reject, protocols tcp, ports 22
396 # # { fam => 'ipv4', args => '-m set --match-set kur_ssh_4 src -p tcp -m multiport --dports 22 -j REJECT --reject-with icmp-port-unreachable' }
397 # # { fam => 'ipv6', args => '-m set --match-set kur_ssh_6 src -p tcp -m multiport --dports 22 -j REJECT --reject-with icmp6-port-unreachable' }
398 0 ▶sub _rule_args {
Subroutine _rule_args: not called CC=14 SCAR=53.5
399 0 my ($self) = @_;
400  
401 0 my ( $set4, $set6 ) = $self->_set_names;
402  
403 0 my @ports = @{ $self->{ports} };
404 0 my @protos = @{ $self->{protocols} };
405 0 my $port_str = join( ',', @ports );
406  
407 # work out the target for each family
408 0 my $t4 = 'DROP';
409 0 my $t6 = 'DROP';
410 0 ▶ if ( $self->{options}{type} eq 'reject' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"options"}{"type"} eq "reject") 0 0 0
411 0 $t4 = 'REJECT --reject-with icmp-port-unreachable';
412 0 $t6 = 'REJECT --reject-with icmp6-port-unreachable';
413 }
414  
415 # protocols that accept a port specification
416 0 my %port_ok = ( tcp => 1, udp => 1, sctp => 1 );
417  
418 # the various names the IPv6 ICMP protocol may go by
419 0 my %v6_icmp = ( 'ipv6-icmp' => 1, 'icmp6' => 1, 'icmpv6' => 1 );
420  
421 0 my @families = (
422 { fam => 'ipv4', set => $set4, tgt => $t4, family => 4 },
423 { fam => 'ipv6', set => $set6, tgt => $t6, family => 6 },
424 );
425  
426 0 my @rules;
427 0 foreach my $fam (@families) {
428 0 my $base = '-m set --match-set ' . $fam->{set} . ' src';
429  
430 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
431 # block everything sourced from the set
432 0 push( @rules, { fam => $fam->{fam}, args => $base . ' -j ' . $fam->{tgt} } );
433 ▶ } 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
434 # ports require a protocol, default to tcp and udp
435 0 foreach my $proto ( 'tcp', 'udp' ) {
436 push(
437 @rules,
438 {
439 fam => $fam->{fam},
440 args => $base . ' -p ' . $proto . ' -m multiport --dports ' . $port_str . ' -j ' . $fam->{tgt}
441 }
442 0 );
443 }
444 } else {
445 0 foreach my $proto (@protos) {
446 # skip protocols that do not belong to this family
447 0 ▶ if ( $fam->{family} == 6 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fam->{'family'} == 6) { } 0 0 0
448 0 ▶ next if ( $proto eq 'icmp' );
Branch: 1 branch
BranchTrueFalseTotal
if $proto eq "icmp" 0 0 0
449 } else {
450 0 ▶ next if ( $v6_icmp{$proto} );
Branch: 1 branch
BranchTrueFalseTotal
if $v6_icmp{$proto} 0 0 0
451 }
452  
453 0 my $args = $base . ' -p ' . $proto;
454 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
455 0 $args .= ' -m multiport --dports ' . $port_str;
456 }
457 0 $args .= ' -j ' . $fam->{tgt};
458 0 push( @rules, { fam => $fam->{fam}, args => $args } );
459 } ## end foreach my $proto (@protos)
460 } ## end else [ if ( !@protos && !@ports ) ]
461 } ## end foreach my $fam (@families)
462  
463 0 return @rules;
464 } ## end sub _rule_args
465  
466 =head2 init
467  
468 Initiates the backend. Any stale direct rules and ipsets from a previous
469 run are removed first, with failures ignored. Then the two ipsets are
470 created via C<ipset create> and the block rules are added to the configured
471 chain via C<firewall-cmd --direct --add-rule>.
472  
473 $backend->init;
474  
475 =cut
476  
477 0 ▶sub init {
Subroutine init: not called CC=10 SCAR=47.0
Pod documented
478 0 my ( $self, %opts ) = @_;
479  
480 0 $self->errorblank;
481  
482 0 ▶ if ( $self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"inited"}) 0 0 0
483 0 $self->{error} = 18;
484 0 $self->{errorString} = 'backend has already been inited';
485 0 $self->warn;
486 }
487  
488 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
489 0 $self->{frontend_obj}->{test_data} = {};
490 }
491  
492 0 my ( $set4, $set6 ) = $self->_set_names;
493 0 my $chain = $self->{options}{chain};
494  
495 0 my @rules = $self->_rule_args;
496  
497 # stale state cleanup, allowed to fail
498 0 my @fail_okay_commands;
499 0 foreach my $rule (@rules) {
500 push( @fail_okay_commands,
501 0 'firewall-cmd --direct --remove-rule ' . $rule->{fam} . ' filter ' . $chain . ' 0 ' . $rule->{args} );
502 }
503 0 push( @fail_okay_commands, 'ipset destroy ' . $set4 );
504 0 push( @fail_okay_commands, 'ipset destroy ' . $set6 );
505  
506 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
507 0 $self->{frontend_obj}->{test_data}{fail_okay_commands} = \@fail_okay_commands;
508 } else {
509 0 foreach my $item (@fail_okay_commands) {
510 0 my $output = `$item 2>&1`;
511 }
512 }
513  
514 0 my @commands;
515 0 push( @commands, 'ipset create ' . $set4 . ' hash:ip family inet' );
516 0 push( @commands, 'ipset create ' . $set6 . ' hash:ip family inet6' );
517 0 foreach my $rule (@rules) {
518 push( @commands,
519 0 'firewall-cmd --direct --add-rule ' . $rule->{fam} . ' filter ' . $chain . ' 0 ' . $rule->{args} );
520 }
521  
522 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
523 0 $self->{frontend_obj}->{test_data}{commands} = \@commands;
524 } else {
525 0 foreach my $item (@commands) {
526 0 my $output = `$item 2>&1`;
527 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
528 0 $self->{error} = 23;
529 $self->{errorString}
530 0 = 'init failed. non-zero exit code for the command... "' . $item . '"... output... ' . $output;
531 0 $self->warn;
532 }
533 }
534 } ## end else [ if ( $self->{testing} ) ]
535  
536 0 $self->{inited} = 1;
537 } ## end sub init
538  
539 =head2 ban
540  
541 Bans an IP. The value of ban is validated as being a IPv4 or IPv6 address
542 and lowercased, then added to the relevant ipset via C<ipset add>. If the
543 kill option is set, conntrack(8) is used to drop existing connection
544 tracking entries for the IP. Banning an already banned IP is a noop.
545  
546 $backend->ban(ban => $ip);
547  
548 =cut
549  
550 0 ▶sub ban {
Subroutine ban: not called CC=14 SCAR=53.5
Pod documented
551 0 my ( $self, %opts ) = @_;
552  
553 0 $self->errorblank;
554  
555 0 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 0 0
556 0 $self->{error} = 1;
557 0 $self->{errorString} = 'backend has not been inited';
558 0 $self->warn;
559 0 return;
560 }
561  
562 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
563 0 $self->{error} = 9;
564 0 $self->{errorString} = 'Nothing specified for the value ban';
565 0 $self->warn;
566 0 return;
567 } elsif ( ref( $opts{ban} ) ne '' ) {
568 0 $self->{error} = 10;
569 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
570 0 $self->warn;
571 0 return;
572 ▶ } 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
573 && $opts{ban} !~ /\A$IPv6_re\z/ )
574 {
575 0 $self->{error} = 10;
576 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 IP';
577 0 $self->warn;
578 0 return;
579 }
580  
581 # lowercase so the same IPv6 IP in differing cases can't result in duplicate entries
582 0 $opts{ban} = lc( $opts{ban} );
583  
584 0 ▶ if ( $self->{banned}{ $opts{ban} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"banned"}{$opts{"ban"}}) 0 0 0
585 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
586 0 $self->{frontend_obj}->{test_data} = 'already banned';
587 }
588 0 return;
589 }
590  
591 0 my ( $set4, $set6 ) = $self->_set_names;
592 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
593  
594 0 my $command = 'ipset add ' . $set . ' ' . $opts{ban};
595  
596 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
597 0 $self->{frontend_obj}->{test_data} = [$command];
598 } else {
599 0 my $output = `$command 2>&1`;
600 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
601 0 $self->{error} = 13;
602 $self->{errorString}
603 0 = 'ban failed. non-zero exit code for the command... "' . $command . '"... output... ' . $output;
604 0 $self->warn;
605 }
606 }
607  
608 0 ▶ if ( $self->{options}{kill} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"options"}{"kill"}) 0 0 0
609 0 foreach my $kill_command ( $self->_kill_commands( $opts{ban} ) ) {
610 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
611 0 push( @{ $self->{frontend_obj}->{test_data} }, $kill_command );
612 } else {
613 # conntrack returns non-zero when there is nothing to delete,
614 # so its exit code is intentionally ignored
615 0 my $output = `$kill_command 2>&1`;
616 }
617 }
618 } ## end if ( $self->{options}{kill} )
619  
620 0 $self->{banned}{ $opts{ban} } = 1;
621 } ## end sub ban
622  
623 =head2 unban
624  
625 Unbans an IP. The value of ban is validated as being a IPv4 or IPv6 address
626 and lowercased, then removed from the relevant ipset via C<ipset del>.
627 Unbanning an IP that is not banned is a noop.
628  
629 $backend->unban(ban => $ip);
630  
631 =cut
632  
633 0 ▶sub unban {
Subroutine unban: not called CC=11 SCAR=48.8
Pod documented
634 0 my ( $self, %opts ) = @_;
635  
636 0 $self->errorblank;
637  
638 0 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 0 0
639 0 $self->{error} = 1;
640 0 $self->{errorString} = 'backend has not been inited';
641 0 $self->warn;
642 0 return;
643 }
644  
645 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
646 0 $self->{error} = 9;
647 0 $self->{errorString} = 'Nothing specified for the value ban';
648 0 $self->warn;
649 0 return;
650 } elsif ( ref( $opts{ban} ) ne '' ) {
651 0 $self->{error} = 10;
652 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
653 0 $self->warn;
654 0 return;
655 ▶ } 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
656 && $opts{ban} !~ /\A$IPv6_re\z/ )
657 {
658 0 $self->{error} = 10;
659 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 IP';
660 0 $self->warn;
661 0 return;
662 }
663  
664 # lowercase so the same IPv6 IP in differing cases can't result in duplicate entries
665 0 $opts{ban} = lc( $opts{ban} );
666  
667 0 ▶ if ( !$self->{banned}{ $opts{ban} } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"banned"}{$opts{"ban"}}) 0 0 0
668 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
669 0 $self->{frontend_obj}->{test_data} = 'not banned';
670 }
671 0 return;
672 }
673  
674 0 my ( $set4, $set6 ) = $self->_set_names;
675 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
676  
677 0 my $command = 'ipset del ' . $set . ' ' . $opts{ban};
678  
679 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
680 0 $self->{frontend_obj}->{test_data} = $command;
681 } else {
682 0 my $output = `$command 2>&1`;
683 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
684 0 $self->{error} = 14;
685 $self->{errorString}
686 0 = 'unban failed. non-zero exit code for the command... "' . $command . '"... output... ' . $output;
687 0 $self->warn;
688 }
689 }
690  
691 0 delete( $self->{banned}{ $opts{ban} } );
692 } ## end sub unban
693  
694 =head2 list
695  
696 List banned IPs. Returns an array of the currently banned IPs.
697  
698 my @banned = $backend->list;
699  
700 =cut
701  
702 0 ▶sub list {
Subroutine list: not called CC=2 SCAR=17.9
Pod documented
703 0 my ( $self, %opts ) = @_;
704  
705 0 $self->errorblank;
706  
707 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
708 0 $self->{frontend_obj}->{test_data} = 'list';
709 }
710  
711 0 return keys( %{ $self->{banned} } );
712 }
713  
714 =head2 re_init
715  
716 Tells the backend to re-init it's self.
717  
718 This will call teardown and init again. After that it will re-add all
719 previously added bans via C<ipset add>.
720  
721 $backend->re_init;
722  
723 =cut
724  
725 0 ▶sub re_init {
Subroutine re_init: not called CC=6 SCAR=37.4
Pod documented
726 0 my ( $self, %opts ) = @_;
727  
728 0 $self->errorblank;
729  
730 # teardown is best effort here as a partially or fully wiped setup is
731 # exactly what re_init needs to recover from; init cleans up any remnants
732 {
733 0 local $@;
734 0 eval { $self->teardown; };
735 }
736 0 $self->init;
737  
738 0 my ( $set4, $set6 ) = $self->_set_names;
739  
740 0 my @to_ban = keys( %{ $self->{banned} } );
741  
742 0 my @re_init_test_data;
743 0 foreach my $item (@to_ban) {
744 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
745 0 my $command = 'ipset add ' . $set . ' ' . $item;
746  
747 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
748 0 push( @re_init_test_data, $command );
749 } else {
750 0 my $output = `$command 2>&1`;
751 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
752 0 $self->{error} = 13;
753 $self->{errorString}
754 0 = 'ban failed. non-zero exit code for the command... "' . $command . '"... output... ' . $output;
755 0 $self->warn;
756 }
757 }
758 } ## end foreach my $item (@to_ban)
759  
760 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
761 0 $self->{frontend_obj}->{test_data} = \@re_init_test_data;
762 }
763  
764 0 $self->{inited} = 1;
765 } ## end sub re_init
766  
767 =head2 teardown
768  
769 Tears down the setup for the backend.
770  
771 This will remove the direct rules and the ipsets.
772  
773 $backend->teardown;
774  
775 =cut
776  
777 0 ▶sub teardown {
Subroutine teardown: not called CC=5 SCAR=34.0
Pod documented
778 0 my ( $self, %opts ) = @_;
779  
780 0 $self->errorblank;
781  
782 0 $self->{inited} = 0;
783  
784 0 my ( $set4, $set6 ) = $self->_set_names;
785 0 my $chain = $self->{options}{chain};
786  
787 0 my @commands;
788 0 foreach my $rule ( $self->_rule_args ) {
789 push( @commands,
790 0 'firewall-cmd --direct --remove-rule ' . $rule->{fam} . ' filter ' . $chain . ' 0 ' . $rule->{args} );
791 }
792 0 push( @commands, 'ipset destroy ' . $set4 );
793 0 push( @commands, 'ipset destroy ' . $set6 );
794  
795 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
796 0 $self->{frontend_obj}->{test_data} = \@commands;
797 } else {
798 0 foreach my $item (@commands) {
799 0 my $output = `$item 2>&1`;
800 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
801 0 $self->{error} = 17;
802 $self->{errorString}
803 0 = 'teardown failed. non-zero exit code for the command... "' . $item . '"... output... ' . $output;
804 0 $self->warn;
805 }
806 }
807 } ## end else [ if ( $self->{testing} ) ]
808 } ## end sub teardown
809  
810 =head2 stop
811  
812 Alias for L</teardown>, provided for parity with the fail2ban C<actionstop>
813 concept.
814  
815 $backend->stop;
816  
817 =cut
818  
819 0 ▶sub stop {
Subroutine stop: not called CC=1 SCAR=6.9
Pod documented
820 0 my ( $self, %opts ) = @_;
821  
822 0 return $self->teardown(%opts);
823 }
824  
825 =head2 check
826  
827 Verifies that firewalld is running, the ipsets are still present, and each
828 of the direct rules is still in place. Returns a true value if the setup is
829 intact and a false value if any part is missing. This is the equivalent of
830 fail2ban's C<actioncheck>.
831  
832 if ( !$backend->check ) {
833 $backend->re_init;
834 }
835  
836 =cut
837  
838 0 ▶sub check {
Subroutine check: not called CC=5 SCAR=34.0
Pod documented
839 0 my ( $self, %opts ) = @_;
840  
841 0 $self->errorblank;
842  
843 0 my ( $set4, $set6 ) = $self->_set_names;
844 0 my $chain = $self->{options}{chain};
845  
846 0 my @commands = ( 'firewall-cmd --state', 'ipset list ' . $set4, 'ipset list ' . $set6 );
847 0 foreach my $rule ( $self->_rule_args ) {
848 push( @commands,
849 0 'firewall-cmd --direct --query-rule ' . $rule->{fam} . ' filter ' . $chain . ' 0 ' . $rule->{args} );
850 }
851  
852 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
853 0 $self->{frontend_obj}->{test_data} = \@commands;
854 0 return 1;
855 }
856  
857 0 foreach my $item (@commands) {
858 0 my $output = `$item 2>&1`;
859 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
860 0 return 0;
861 }
862 }
863  
864 0 return 1;
865 } ## end sub check
866  
867 =head2 flush
868  
869 Removes all currently banned IPs at once by flushing the ipsets, leaving the
870 rules in place. This is the equivalent of fail2ban's C<actionflush>.
871  
872 $backend->flush;
873  
874 =cut
875  
876 0 ▶sub flush {
Subroutine flush: not called CC=5 SCAR=34.0
Pod documented
877 0 my ( $self, %opts ) = @_;
878  
879 0 $self->errorblank;
880  
881 0 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 0 0
882 0 $self->{error} = 1;
883 0 $self->{errorString} = 'backend has not been inited';
884 0 $self->warn;
885 0 return;
886 }
887  
888 0 my ( $set4, $set6 ) = $self->_set_names;
889  
890 0 my @commands = ( 'ipset flush ' . $set4, 'ipset flush ' . $set6 );
891  
892 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
893 0 $self->{frontend_obj}->{test_data} = \@commands;
894 } else {
895 0 foreach my $item (@commands) {
896 0 my $output = `$item 2>&1`;
897 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
898 0 $self->{error} = 25;
899 $self->{errorString}
900 0 = 'flush failed. non-zero exit code for the command... "' . $item . '"... output... ' . $output;
901 0 $self->warn;
902 }
903 }
904 } ## end else [ if ( $self->{testing} ) ]
905  
906 0 $self->{banned} = {};
907 } ## end sub flush
908  
909 =head2 ban_cidr
910  
911 CIDR bans are not supported by this backend; this always sets the
912 cidrNotSupported error.
913  
914 $backend->ban_cidr(ban => '1.2.3.0/24');
915  
916 =cut
917  
918 0 ▶sub ban_cidr {
Subroutine ban_cidr: not called CC=1 SCAR=6.9
Pod documented
919 0 my ( $self, %opts ) = @_;
920  
921 0 $self->errorblank;
922  
923 0 $self->{error} = 29;
924 0 $self->{errorString} = 'the ' . __PACKAGE__ . ' backend does not support CIDR bans';
925 0 $self->warn;
926  
927 0 return;
928 } ## end sub ban_cidr
929  
930 =head2 unban_cidr
931  
932 CIDR bans are not supported by this backend; this always sets the
933 cidrNotSupported error.
934  
935 $backend->unban_cidr(ban => '1.2.3.0/24');
936  
937 =cut
938  
939 0 ▶sub unban_cidr {
Subroutine unban_cidr: not called CC=1 SCAR=6.9
Pod documented
940 0 my ( $self, %opts ) = @_;
941  
942 0 $self->errorblank;
943  
944 0 $self->{error} = 29;
945 0 $self->{errorString} = 'the ' . __PACKAGE__ . ' backend does not support CIDR bans';
946 0 $self->warn;
947  
948 0 return;
949 } ## end sub unban_cidr
950  
951 =head2 list_cidr
952  
953 CIDR bans are not supported by this backend, so this always returns an empty
954 list.
955  
956 my @banned_cidrs = $backend->list_cidr;
957  
958 =cut
959  
960 0 ▶sub list_cidr {
Subroutine list_cidr: not called CC=1 SCAR=6.9
Pod documented
961 0 my ( $self, %opts ) = @_;
962  
963 0 $self->errorblank;
964  
965 0 return ();
966 }
967  
968 =head1 ERROR CODES / FLAGS
969  
970 Error handling is provided by L<Error::Helper>. All
971 errors are considered fatal.
972  
973 =head2 1, notInited
974  
975 The backend has not been inited yet.
976  
977 =head2 2, 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 3, portsNotArray
982  
983 The data passed to new for ports is not an array.
984  
985 =head2 4, protocolsNotArray
986  
987 The data passed to new for protocols is not an array.
988  
989 =head2 5, invalidPortSpecified
990  
991 Port is either not an int within the range 1 to 65535 or a name that can be resolved by getservbyname.
992  
993 =head2 6, invalidPrefixSpecified
994  
995 The specified prefix did not match /^[a-zA-Z0-9]+$/.
996  
997 =head2 7, invalidName
998  
999 The name is either undef or does not match /^[a-zA-Z0-9\-]+$/.
1000  
1001 =head2 8, optionsNotHash
1002  
1003 The item passed to new for options is not a hash.
1004  
1005 =head2 9, noBanItem
1006  
1007 No IP specified to ban or unban.
1008  
1009 =head2 10, banItemNotIP
1010  
1011 The item to ban is not an IP. Either wrong ref type or regexp
1012 test using L<Regexp::IPv4> and L<Regexp::IPv6> failed.
1013  
1014 =head2 12, backendInitError
1015  
1016 Failed to init the backend.
1017  
1018 =head2 13, banFailed
1019  
1020 Failed to ban the item.
1021  
1022 =head2 14, unbanFailed
1023  
1024 Failed to unban the item.
1025  
1026 =head2 15, listFailed
1027  
1028 Failed to get a list of bans.
1029  
1030 =head2 16, reInitFailed
1031  
1032 Failed to re_init the backend.
1033  
1034 =head2 17, teardownFailed
1035  
1036 Failed to teardown the backend.
1037  
1038 =head2 18, alreadyInited
1039  
1040 init called, but the backend has already been inited.
1041  
1042 =head2 19, chainInvalid
1043  
1044 The option chain does not match /^[a-zA-Z0-9_\-]+$/.
1045  
1046 =head2 20, typeInvalid
1047  
1048 The option type is either a ref or not "drop" or "reject".
1049  
1050 =head2 21, nameTooLong
1051  
1052 The combined prefix and name is longer than 29 characters, leaving no room
1053 for the _4/_6 suffix within the 31 character ipset name limit.
1054  
1055 =head2 23, initFailed
1056  
1057 Init failed. A command run during init exited non-zero.
1058  
1059 =head2 24, checkFailed
1060  
1061 The backend check raised an error.
1062  
1063 =head2 25, flushFailed
1064  
1065 Failed to flush the bans.
1066  
1067 =head2 26, banCidrFailed
1068  
1069 Failed to ban the CIDR range.
1070  
1071 =head2 27, unbanCidrFailed
1072  
1073 Failed to unban the CIDR range.
1074  
1075 =head2 28, cidrItemNotCidr
1076  
1077 The item to ban is not a CIDR range. Either wrong ref type or it is not an
1078 IPv4 or IPv6 address followed by a prefix length valid for its family.
1079  
1080 =head2 29, cidrNotSupported
1081  
1082 The backend does not support CIDR bans.
1083  
1084 =head2 30, listCidrFailed
1085  
1086 Failed to get a list of CIDR bans.
1087  
1088 =head1 AUTHOR
1089  
1090 Zane C. Bowers-Hadley, C<< <vvelox at vvelox.ent> >>
1091  
1092 =head1 BUGS
1093  
1094 Please report any bugs or feature requests to C<bug-net-firewall-blockerhelper at rt.cpan.org>, or through
1095 the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Net-Firewall-BlockerHelper>.
1096  
1097 =head1 LICENSE AND COPYRIGHT
1098  
1099 This software is Copyright (c) 2023 by Zane C. Bowers-Hadley.
1100  
1101 This is free software, licensed under:
1102  
1103 The GNU Lesser General Public License, Version 2.1, February 1999
1104  
1105  
1106 =cut
1107  
1108 2 1; # End of Net::Firewall::BlockerHelper
« BlockerHelper/backends/openwrt.pm ↑ Summary BlockerHelper/backends/iptables.pm »