BlockerHelper/backends/ufw.pm

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