BlockerHelper/backends/iptables.pm

filter: Statementstmt 33.5% 150 / 447 Branchbran 23.3% 43 / 184 Conditioncond 27.7% 20 / 72 MC/DCmcdc 6.5% 3 / 46 Subroutinesub 53.5% 15 / 28 Podpod 100% 13 / 13 totaltotal 30.8% 244 / 790 CC 133 SCAR 47.5
CC
133
Cov
30%
new
230.1
ban
210.0
_kill_commands
182.0
CRAP 115.3
« BlockerHelper/backends/firewalld.pm ↑ Summary BlockerHelper/backends/pf.pm »
1 package Net::Firewall::BlockerHelper::backends::iptables;
2  
3 3 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use base 'Error::Helper';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Regexp::IPv4 qw($IPv4_re);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Regexp::IPv6 qw($IPv6_re);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 =head1 NAME
11  
12 Net::Firewall::BlockerHelper::backends::iptables - iptables/ip6tables backend for Net::Firewall::BlockerHelper.
13  
14 =head1 VERSION
15  
16 Version 0.1.0
17  
18 =cut
19  
20 3 our $VERSION = '0.1.0';
21  
22 =head1 SYNOPSIS
23  
24 Everything this backend needs is wired in by init itself, the jump from
25 C<INPUT> to its chain included, so no ruleset changes are needed
26 beforehand. The jump is appended to the end of C<INPUT> though, and
27 iptables is first match wins, so any earlier rule accepting the traffic in
28 question, such as a blanket ACCEPT for a port or an established/related
29 state rule, will win out over the bans. If bans appear to have no effect,
30 check where the jump to C<< <prefix>_<name> >> sits via
31 C<iptables -L INPUT> and relocate it above such rules; the backend finds
32 it by rule spec rather than position, so moving it is safe.
33  
34 use Net::Firewall::BlockerHelper::backends::iptables;
35  
36 my $backend1;
37 my $backend2;
38 eval {
39 $backend1 = Net::Firewall::BlockerHelper::backends::iptables->new(
40 name => 'all',
41 options=>{ kill=>1 },
42 );
43 $backend2 = Net::Firewall::BlockerHelper::backends::iptables->new(
44 ports => ['143'],
45 protocols => ['tcp'],
46 name => 'imap',
47 );
48 };
49 if ($@) {
50 print 'Error: '
51 . $Error::Helper::error
52 . "\nError String: "
53 . $Error::Helper::errorString
54 . "\nError Flag: "
55 . $Error::Helper::errorFlag . "\n";
56 }
57  
58 $backend1->init;
59 $backend2->init;
60  
61 $backend1->ban(ban=>'1.2.3.4');
62 $backend1->ban(ban=>'4.3.2.1');
63 $backend2->ban(ban=>'4.3.2.1');
64  
65 use Data::Dumper;
66 print Dumper($backend1->list);
67 print Dumper($backend2->list);
68  
69 $backend1->unban(ban=>'4.3.2.1');
70  
71 $backend1->teardown;
72 $backend2->teardown;
73  
74 =head1 DESCRIPTION
75  
76 This backend blocks IPs using L<ipset(8)> in combination with
77 L<iptables(8)> and L<ip6tables(8)>.
78  
79 For each instance two ipsets are created, one for IPv4
80 (C<< <prefix>_<name>_4 >>) and one for IPv6 (C<< <prefix>_<name>_6 >>),
81 along with a dedicated chain (C<< <prefix>_<name> >>) in each of the
82 C<filter> tables. The chain is populated with the block rules and jumped
83 to from C<INPUT>. Banning an IP is then simply a matter of adding it to
84 the relevant ipset.
85  
86 When the C<tarpit> or C<delude> type is used a same-named chain is also
87 created in the C<raw> table and jumped to from C<PREROUTING>, holding
88 C<< -j CT --notrack >> rules that match the same traffic. This exempts the
89 tarpitted/deluded connections from connection tracking, which those
90 xtables-addons targets require to work. It is created and removed
91 automatically with the filter chain.
92  
93 Requires C<ipset>, C<iptables>, and C<ip6tables> to be installed and in
94 the C<PATH> of the process, which must have sufficient privileges to run
95 them. The C<tarpit>/C<delude> types additionally require the C<TARPIT>/
96 C<DELUDE> targets from xtables-addons.
97  
98 =head1 METHODS
99  
100 =head2 new
101  
102 Initiates the the object.
103  
104 - options :: Backend specific options that will be passed to the backend unchecked
105 outside of making sure it is a hash ref if defined. See below for furhter info.
106 - Default :: {}
107  
108 - ports :: A array of ports to block. Checked to make sure they are positive ints or a valid
109 service name via getservbyname. All ports will be blocked if non are specified. If
110 duplicates are removed.
111 - Default :: []
112  
113 - protocols :: A array of protocols to block. By default will block all,
114 unless ports are given, in which case it defaults to tcp and udp. This
115 is checked against /etc/protocols via the function getprotobyname. Duplicates
116 will be discarded.
117 - Default :: [], or ['tcp','udp'] when ports are given
118  
119 - prefix :: Prefix to use. Must match the regex /^[a-zA-Z0-9]+$/
120 - default :: kur
121  
122 - name :: Name of this specific instance. This must be specified.
123 - default :: undef
124  
125 The options hash accepts the following.
126  
127 - type :: The block method to use. One of 'drop', 'reject', 'tarpit',
128 or 'delude'.
129 - drop :: Silently drop the packet. See iptables(8).
130 - reject :: Send an ICMP port-unreachable back. See iptables(8).
131 - tarpit :: Hold the TCP connection open with a zero window so
132 the attacker wastes resources. Requires the TARPIT
133 target from xtables-addons. TCP only.
134 - delude :: Answer a SYN with a SYN/ACK and everything else with
135 a RST, so the port looks open but never completes a
136 session. Requires the DELUDE target from xtables-addons.
137 TCP only. The target itself is also IPv4 only, as
138 xtables-addons provides no IPv6 version, so the IPv6
139 rule falls back to DROP; banned IPv6 IPs are still
140 blocked, just silently dropped rather than deluded.
141 The tarpit and delude types only ever emit '-p tcp' rules; any
142 non-tcp protocol or port default (eg the implicit udp) is skipped
143 rather than handed to a target the kernel would reject. For these
144 types the backend also installs a matching '-j CT --notrack' rule
145 in a raw table chain jumped from PREROUTING, so conntrack does not
146 process (and fight) the crafted replies; this is set up and torn
147 down automatically alongside the filter chain.
148 - Default :: drop
149  
150 - tarpit_mode :: When type is 'tarpit', selects the TARPIT mode, one of
151 'tarpit' (default, zero-window hold), 'honeypot' (accept then hold),
152 or 'reset'. Ignored for the other types.
153 - Default :: tarpit
154  
155 - kill :: Use conntrack(8) to drop existing connection tracking entries
156 for the banned IP, for both IPv4 and IPv6. When protocols are
157 configured the kill is scoped to them via -p, so blocking only
158 udp will not drop tcp entries and vice versa; protocols conntrack
159 can not filter by are skipped. With no protocols configured
160 everything is being blocked, so entries of every protocol are
161 dropped.
162 - Default :: 0
163  
164 All errors are considered fatal, meaning if new fails it will die.
165  
166 my $backend;
167 eval {
168 $backend = Net::Firewall::BlockerHelper::backends::iptables->new(
169 ports => ['22'],
170 protocols => ['tcp'],
171 name => 'ssh',
172 );
173 };
174 if ($@) {
175 print 'Error: '
176 . $Error::Helper::error
177 . "\nError String: "
178 . $Error::Helper::errorString
179 . "\nError Flag: "
180 . $Error::Helper::errorFlag . "\n";
181 }
182  
183 =cut
184  
185 sub new {
186 4 ▶ my ( $blank, %opts ) = @_;
Subroutine new: called CC=36 SCAR=54.4
Pod documented
187  
188 4 my $self = {
189 perror => undef,
190 error => undef,
191 errorLine => undef,
192 errorFilename => undef,
193 errorString => "",
194 errorExtra => {
195 all_errors_fatal => 1,
196 # all_fatal is what Error::Helper 2.1.0 actually checks; all_errors_fatal
197 # is kept for the name documented in its POD
198 all_fatal => 1,
199 flags => {
200 1 => 'notInited',
201 2 => 'invalidPortSpecified',
202 3 => 'portsNotArray',
203 4 => 'protocolsNotArray',
204 5 => 'invalidPortSpecified',
205 6 => 'invalidPrefixSpecified',
206 7 => 'invalidName',
207 8 => 'optionsNotHash',
208 9 => 'noBanItem',
209 10 => 'banItemNotIP',
210 11 => 'invalidBackend',
211 12 => 'backendInitError',
212 13 => 'banFailed',
213 14 => 'unbanFailed',
214 15 => 'listFailed',
215 16 => 'reInitFailed',
216 17 => 'teardownFailed',
217 18 => 'alreadyInited',
218 20 => 'typeInvalid',
219 21 => 'nameTooLong',
220 23 => 'initFailed',
221 24 => 'checkFailed',
222 25 => 'flushFailed',
223 26 => 'banCidrFailed',
224 27 => 'unbanCidrFailed',
225 28 => 'cidrItemNotCidr',
226 29 => 'cidrNotSupported',
227 30 => 'listCidrFailed',
228 },
229 fatal_flags => {},
230 perror_not_fatal => 0,
231 },
232 options => {
233 type => 'drop',
234 kill => 0,
235 },
236 ports => [],
237 protocols => [],
238 testing => undef,
239 test_data => undef,
240 prefix => 'kur',
241 frontend_obj => undef,
242 inited => 0,
243 banned => {},
244 cidr_supported => 0,
245 };
246 4 bless $self;
247  
248 4 ▶ if ( defined( $opts{ports} ) && ref( $opts{ports} ) ne 'ARRAY' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $opts{'ports'} and ref $opts{'ports'} ne 'ARRAY') { } 0 4 4
elsif (defined $opts{'ports'}) { } 2 2 4
Condition66%2/3
defined $opts{"ports"} and ref $opts{"ports"} ne "ARRAY"
!ll&&!rl&&r
220
MC/DC0%0/2
defined $opts{"ports"} and ref $opts{"ports"} ne "ARRAY"
defined $opts{"ports"} ref $opts{"ports"} ne "ARRAY"
Truth table66%2/3
A and B
A= defined $opts{"ports"}B= ref $opts{"ports"} ne "ARRAY"
ABresult
0X0
100
111
249 0 $self->{perror} = 1;
250 0 $self->{error} = 3;
251 0 $self->{errorString} = 'ports is defined and type is not array but "' . ref( $opts{ports} ) . '"';
252 0 $self->warn;
253 } elsif ( defined( $opts{ports} ) ) {
254 2 my %ports;
255 2 foreach my $item ( @{ $opts{ports} } ) {
256 1 ▶ if ( $item =~ /^[0-9]+$/ && $item >= 1 && $item <= 65535 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($item =~ /^[0-9]+$/ and $item >= 1 and $item <= 65535) { } 1 0 1
elsif ($item =~ /^[0-9]+$/) { } 0 0 0
Condition33%2/6
$item =~ /^[0-9]+$/ and $item >= 1 and $item <= 65535
!ll&&!rl&&r
001
$item =~ /^[0-9]+$/ and $item >= 1
!ll&&!rl&&r
001
MC/DC0%0/3
$item =~ /^[0-9]+$/ and $item >= 1 and $item <= 65535
$item =~ /^[0-9]+$/ $item >= 1 $item <= 65535
Truth table25%1/4
A and B and C
A= $item =~ /^[0-9]+$/B= $item >= 1C= $item <= 65535
ABCresult
0XX0
10X0
1100
1111
257 1 $ports{$item} = 1;
258 } elsif ( $item =~ /^[0-9]+$/ ) {
259 0 $self->{perror} = 1;
260 0 $self->{error} = 2;
261 $self->{errorString}
262 0 = $item . ' is not a valid value for a port as it must be a int within the range 1 to 65535';
263 0 $self->warn;
264 } else {
265 # just using tcp here as protocol must be specified
266 0 my ( $name, $aliases, $port, $proto ) = getservbyname( $item, 'tcp' );
267 0 ▶ if ( !defined($port) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $port) 0 0 0
268 0 $self->{perror} = 1;
269 0 $self->{error} = 2;
270 $self->{errorString}
271 0 = $item . ' could not be resolved to a port name via getservbyname("' . $item . '", "tcp")';
272 0 $self->warn;
273 }
274 0 $ports{$port} = 1;
275 } ## end else [ if ( $item =~ /^[0-9]+$/ && $item >= 1 ) ]
276 } ## end foreach my $item ( @{ $opts{ports} } )
277 2 my @port_keys = keys(%ports);
278 2 @port_keys = sort { $a <=> $b } @port_keys;
279 2 push( @{ $self->{ports} }, @port_keys );
280 } ## end elsif ( defined( $opts{ports} ) )
281  
282 4 ▶ if ( defined( $opts{protocols} ) && ref( $opts{protocols} ) ne 'ARRAY' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $opts{'protocols'} and ref $opts{'protocols'} ne 'ARRAY') { } 0 4 4
elsif (defined $opts{'protocols'}) { } 2 2 4
Condition66%2/3
defined $opts{"protocols"} and ref $opts{"protocols"} ne "ARRAY"
!ll&&!rl&&r
220
MC/DC0%0/2
defined $opts{"protocols"} and ref $opts{"protocols"} ne "ARRAY"
defined $opts{"protocols"} ref $opts{"protocols"} ne "ARRAY"
Truth table66%2/3
A and B
A= defined $opts{"protocols"}B= ref $opts{"protocols"} ne "ARRAY"
ABresult
0X0
100
111
283 0 $self->{perror} = 1;
284 0 $self->{error} = 4;
285 0 $self->{errorString} = 'protocols is defined and type is not array but "' . ref( $opts{protocols} ) . '"';
286 0 $self->warn;
287 } elsif ( defined( $opts{protocols} ) ) {
288 2 my %protocols;
289 2 foreach my $item ( @{ $opts{protocols} } ) {
290 0 my ( $name, $aliases, $proto ) = getprotobyname($item);
291 # if this is undef, it means it is not a known protocol
292 0 ▶ if ( !defined($proto) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $proto) 0 0 0
293 0 $self->{perror} = 1;
294 0 $self->{error} = 5;
295 $self->{errorString}
296 0 = $item . ' could not be resolved to a protocol via getprotobyname("' . $item . '")';
297 0 $self->warn;
298 }
299 0 $protocols{$item} = 1;
300 } ## end foreach my $item ( @{ $opts{protocols} } )
301 2 my @protocols_keys = keys(%protocols);
302 2 @protocols_keys = sort { $a cmp $b } @protocols_keys;
303 2 push( @{ $self->{protocols} }, @protocols_keys );
304 } ## end elsif ( defined( $opts{protocols} ) )
305  
306 # make sure prefix is sane if defiend
307 4 ▶ 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 4 4
elsif (defined $opts{'prefix'}) { } 4 0 4
Condition33%1/3
defined $opts{"prefix"} and not $opts{"prefix"} =~ /^[a-zA-Z0-9]+$/
!ll&&!rl&&r
040
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
308 0 $self->{perror} = 1;
309 0 $self->{error} = 6;
310 $self->{errorString}
311 0 = '"' . $opts{prefix} . '" is not a valid prefix as it does not match the regex /^[a-zA-Z0-9]+$/';
312 0 $self->warn;
313 } elsif ( defined( $opts{prefix} ) ) {
314 4 $self->{prefix} = $opts{prefix};
315 }
316  
317 # make sure we have a name and that it is valid
318 4 ▶ if ( !defined( $opts{name} ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $opts{'name'}) { } 0 4 4
elsif (not $opts{'name'} =~ /^[a-zA-Z0-9\-]+$/) { } 0 4 4
319 0 $self->{perror} = 1;
320 0 $self->{error} = 7;
321 0 $self->{errorString} = 'name is undef';
322 0 $self->warn;
323 } elsif ( $opts{name} !~ /^[a-zA-Z0-9\-]+$/ ) {
324 0 $self->{perror} = 1;
325 0 $self->{error} = 7;
326 0 $self->{errorString} = 'name set to "' . $opts{name} . '" which does not match the regexp /^[a-zA-Z0-9\-]+$/';
327 0 $self->warn;
328 }
329 4 $self->{name} = $opts{name};
330  
331 # iptables limits chain names to 28 characters and ipset set names to 31;
332 # the chain is <prefix>_<name> and the sets are <prefix>_<name>_4/_6, so
333 # catch over-long combos here rather than as a confusing error at init
334 4 ▶ if ( defined( $self->{name} ) && length( $self->{prefix} . '_' . $self->{name} ) > 28 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"name"} and length $self->{"prefix"} . '_' . $self->{"name"} > 28) 1 3 4
Condition66%2/3
defined $self->{"name"} and length $self->{"prefix"} . '_' . $self->{"name"} > 28
!ll&&!rl&&r
031
MC/DC50%1/2
defined $self->{"name"} and length $self->{"prefix"} . '_' . $self->{"name"} > 28
defined $self->{"name"} length $self->{"prefix"} . '_' . $self->{"name"} > 28
Truth table66%2/3
A and B
A= defined $self->{"name"}B= length $self->{"prefix"} . '_' . $self->{"name"} > 28
ABresult
0X0
100
111
335 1 $self->{perror} = 1;
336 1 $self->{error} = 21;
337 $self->{errorString}
338 = 'the combined prefix and name, "'
339 . $self->{prefix} . '_'
340 . $self->{name}
341 1 . '", is longer than 28 characters, the max iptables chain name length';
342 1 $self->warn;
343 }
344  
345 # used internally for testing
346 3 ▶ if ( defined( $opts{testing} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"testing"}) 2 1 3
347 2 $self->{testing} = $opts{testing};
348 }
349 3 ▶ if ( defined( $opts{frontend_obj} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"frontend_obj"}) 2 1 3
350 2 $self->{frontend_obj} = $opts{frontend_obj};
351 }
352  
353 3 ▶ if ( defined( $opts{options} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"options"}) 2 1 3
354 2 ▶ if ( ref( $opts{options} ) ne 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $opts{"options"} ne "HASH") 0 2 2
355 0 $self->{perror} = 1;
356 0 $self->{error} = 8;
357 0 $self->{errorString} = 'ref for options is "' . ref( $opts{options} ) . '" and not HASH';
358 0 $self->warn;
359 }
360 2 $self->{options} = $opts{options};
361  
362 2 ▶ if ( !defined( $opts{options}{kill} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $opts{"options"}{"kill"}) 2 0 2
363 2 $self->{options}{kill} = 0;
364 }
365  
366 # tarpit and delude are xtables-addons targets, hence not lumped in
367 # with the base drop/reject that plain iptables always provides
368 2 my %valid_types = ( drop => 1, reject => 1, tarpit => 1, delude => 1 );
369  
370 2 ▶ 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 2 2
elsif (defined $opts{'options'}{'type'} and not $valid_types{$opts{'options'}{'type'}}) { } 0 2 2
elsif (not defined $opts{'options'}{'type'}) { } 0 2 2
Condition33%1/3
defined $opts{"options"}{"type"} and ref $opts{"options"}{"type"} ne ""
!ll&&!rl&&r
020
MC/DC0%0/2
defined $opts{"options"}{"type"} and ref $opts{"options"}{"type"} ne ""
defined $opts{"options"}{"type"} ref $opts{"options"}{"type"} ne ""
Truth table33%1/3
A and B
A= defined $opts{"options"}{"type"}B= ref $opts{"options"}{"type"} ne ""
ABresult
0X0
100
111
371 0 $self->{perror} = 1;
372 0 $self->{error} = 20;
373 0 $self->{errorString} = 'ref for $opts{options}{type} is "' . ref( $opts{options}{type} ) . '" and not ""';
374 0 $self->warn;
375 ▶ } elsif ( defined( $opts{options}{type} )
Condition33%1/3
defined $opts{"options"}{"type"} and not $valid_types{$opts{"options"}{"type"}}
!ll&&!rl&&r
020
MC/DC0%0/2
defined $opts{"options"}{"type"} and not $valid_types{$opts{"options"}{"type"}}
defined $opts{"options"}{"type"} not $valid_types{$opts{"options"}{"type"}}
Truth table33%1/3
A and not(B)
A= defined $opts{"options"}{"type"}B= not $valid_types{$opts{"options"}{"type"}}
ABresult
0X0
100
111
376 && !$valid_types{ $opts{options}{type} } )
377 {
378 0 $self->{perror} = 1;
379 0 $self->{error} = 20;
380 $self->{errorString}
381 = '$opts{options}{type} is "'
382 . $opts{options}{type}
383 0 . '" and not one of "drop", "reject", "tarpit", or "delude"';
384 0 $self->warn;
385 } elsif ( !defined( $opts{options}{type} ) ) {
386 0 $self->{options}{type} = 'drop';
387 }
388  
389 # tarpit has three modes; delude and the plain targets take none. Only
390 # validate it here, _rule_commands decides when it is actually emitted.
391 2 my %valid_tarpit_modes = ( tarpit => 1, honeypot => 1, reset => 1 );
392 2 ▶ if ( defined( $opts{options}{tarpit_mode} )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"options"}{"tarpit_mode"} and ref $opts{"options"}{"tarpit_mode"} ne "" || !$valid_tarpit_modes{$opts{"options"}{"tarpit_mode"}}) 0 2 2
Condition50%3/6
defined $opts{"options"}{"tarpit_mode"} and ref $opts{"options"}{"tarpit_mode"} ne "" || !$valid_tarpit_modes{$opts{"options"}{"tarpit_mode"}}
!ll&&!rl&&r
110
ref $opts{"options"}{"tarpit_mode"} ne "" or not $valid_tarpit_modes{$opts{"options"}{"tarpit_mode"}}
l!l&&r!l&&!r
001
MC/DC0%0/3
defined $opts{"options"}{"tarpit_mode"} and ref $opts{"options"}{"tarpit_mode"} ne "" || !$valid_tarpit_modes{$opts{"options"}{"tarpit_mode"}}
defined $opts{"options"}{"tarpit_mode"} ref $opts{"options"}{"tarpit_mode"} ne "" not $valid_tarpit_modes{$opts{"options"}{"tarpit_mode"}}
Truth table50%2/4
A and B or not(C)
A= defined $opts{"options"}{"tarpit_mode"}B= ref $opts{"options"}{"tarpit_mode"} ne ""C= not $valid_tarpit_modes{$opts{"options"}{"tarpit_mode"}}
ABCresult
0XX0
1000
11X1
1011
393 && ( ref( $opts{options}{tarpit_mode} ) ne ''
394 || !$valid_tarpit_modes{ $opts{options}{tarpit_mode} } ) )
395 {
396 0 $self->{perror} = 1;
397 0 $self->{error} = 20;
398 $self->{errorString}
399 = '$opts{options}{tarpit_mode} is "'
400 . ( ref( $opts{options}{tarpit_mode} ) ne '' ? ref( $opts{options}{tarpit_mode} ) : $opts{options}{tarpit_mode} )
401 0 ▶ . '" and not one of "tarpit", "honeypot", or "reset"';
Branch: 1 branch
BranchTrueFalseTotal
ref $opts{'options'}{'tarpit_mode'} ne '' ? : 0 0 0
402 0 $self->warn;
403 }
404  
405 } ## end if ( defined( $opts{options} ) )
406  
407 3 return $self;
408 } ## end sub new
409  
410 # Internal helper. Returns the IPv4 ipset name, IPv6 ipset name, and chain
411 # name for this instance.
412 sub _set_names {
413 14 ▶ my ($self) = @_;
Subroutine _set_names: called CC=1 SCAR=0.0
414  
415 14 my $chain = $self->{prefix} . '_' . $self->{name};
416 14 return ( $chain . '_4', $chain . '_6', $chain );
417 }
418  
419 # Internal helper. True when the configured type is one of the xtables-addons
420 # TCP targets (tarpit/delude) that require the matching traffic to be exempted
421 # from connection tracking. See _notrack_commands.
422 sub _needs_notrack {
423 10 ▶ my ($self) = @_;
Subroutine _needs_notrack: called CC=3 SCAR=12.5
424  
425 10 my $type = $self->{options}{type};
426 10 ▶ return ( $type eq 'tarpit' || $type eq 'delude' ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$type eq 'tarpit' || $type eq 'delude' ? : 10 0 10
Condition33%1/3
$type eq "tarpit" or $type eq "delude"
l!l&&r!l&&!r
1000
MC/DC0%0/2
$type eq "tarpit" or $type eq "delude"
$type eq "tarpit" $type eq "delude"
Truth table33%1/3
A or B
A= $type eq "tarpit"B= $type eq "delude"
ABresult
1X1
011
000
427 }
428  
429 # Internal helper. Returns the list of per-rule specs the block rules are built
430 # from, as hashrefs of { fam => \%family, match => $match } where
431 # match is the portion of the rule between src and -j (eg
432 # ' -p tcp -m multiport --dports 22', or '' for match-everything).
433 #
434 # Both _rule_commands (the filter table block rules) and
435 # _notrack_commands (the raw table conntrack exemptions) are generated from
436 # these specs so the two always match exactly the same traffic.
437 sub _rule_specs {
438 4 ▶ my ($self) = @_;
Subroutine _rule_specs: called CC=22 SCAR=44.1
439  
440 4 my ( $set4, $set6 ) = $self->_set_names;
441  
442 4 my @ports = @{ $self->{ports} };
443 4 my @protos = @{ $self->{protocols} };
444 4 my $port_str = join( ',', @ports );
445  
446 4 my $type = $self->{options}{type};
447  
448 # work out the target for each family
449 4 my $t4 = 'DROP';
450 4 my $t6 = 'DROP';
451 4 ▶ if ( $type eq 'reject' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($type eq 'reject') { } 0 4 4
elsif ($type eq 'tarpit') { } 4 0 4
elsif ($type eq 'delude') { } 0 0 0
452 0 $t4 = 'REJECT --reject-with icmp-port-unreachable';
453 0 $t6 = 'REJECT --reject-with icmp6-port-unreachable';
454 } elsif ( $type eq 'tarpit' ) {
455 # xtables-addons TARPIT; --honeypot/--reset select the alternate modes,
456 # the default --tarpit is left implicit so older builds still accept it
457 4 my $mode = $self->{options}{tarpit_mode};
458 4 ▶ $t4 = ( defined($mode) && $mode ne 'tarpit' ) ? 'TARPIT --' . $mode : 'TARPIT';
Branch: 1 branch
BranchTrueFalseTotal
defined $mode && $mode ne 'tarpit' ? : 2 2 4
Condition66%2/3
defined $mode and $mode ne "tarpit"
!ll&&!rl&&r
202
MC/DC50%1/2
defined $mode and $mode ne "tarpit"
defined $mode $mode ne "tarpit"
Truth table66%2/3
A and B
A= defined $modeB= $mode ne "tarpit"
ABresult
0X0
100
111
459 4 $t6 = $t4;
460 } elsif ( $type eq 'delude' ) {
461 0 $t4 = 'DELUDE';
462 # xtables-addons provides no IPv6 DELUDE, so v6 falls back to DROP;
463 # banned IPv6 IPs are still blocked, just not deluded
464 0 $t6 = 'DROP';
465 }
466  
467 # TARPIT and DELUDE only handle TCP, so their rules are forced to -p tcp
468 # and any non-tcp protocol/port combination is skipped rather than emitted
469 # with a target the kernel would reject.
470 4 my $tcp_only = $self->_needs_notrack;
471  
472 4 my @families = (
473 { cmd => 'iptables', set => $set4, tgt => $t4, family => 4 },
474 { cmd => 'ip6tables', set => $set6, tgt => $t6, family => 6 },
475 );
476  
477 # protocols that accept a port specification
478 4 my %port_ok = ( tcp => 1, udp => 1, sctp => 1 );
479  
480 # the various names the IPv6 ICMP protocol may go by
481 4 my %v6_icmp = ( 'ipv6-icmp' => 1, 'icmp6' => 1, 'icmpv6' => 1 );
482  
483 4 my @specs;
484 4 foreach my $fam (@families) {
485 8 ▶ if ( !@protos && !@ports ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not @protos || @ports) { } 4 4 8
elsif (not @protos and @ports) { } 4 0 4
Condition66%2/3
@protos || @ports
l!l&&r!l&&!r
044
MC/DC50%1/2
@protos || @ports
@protos @ports
Truth table66%2/3
A || B
A= @protosB= @ports
ABresult
1X1
011
000
486 4 ▶ if ($tcp_only) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tcp_only) { } 4 0 4
487 # tarpit/delude have no all-protocol form; scope to tcp
488 4 push( @specs, { fam => $fam, match => ' -p tcp' } );
489 } else {
490 # block everything sourced from the set
491 0 push( @specs, { fam => $fam, match => '' } );
492 }
493 ▶ } elsif ( !@protos && @ports ) {
Condition33%1/3
not @protos and @ports
!ll&&!rl&&r
004
MC/DC0%0/2
not @protos and @ports
not @protos @ports
Truth table33%1/3
not(A) and B
A= not @protosB= @ports
ABresult
0X0
100
111
494 # ports require a protocol, default to tcp and udp, but tarpit/delude
495 # are tcp only so udp is dropped from that default
496 4 ▶ my @default_protos = $tcp_only ? ('tcp') : ( 'tcp', 'udp' );
Branch: 1 branch
BranchTrueFalseTotal
$tcp_only ? : 4 0 4
497 4 foreach my $proto (@default_protos) {
498 4 push( @specs, { fam => $fam, match => ' -p ' . $proto . ' -m multiport --dports ' . $port_str } );
499 }
500 } else {
501 0 foreach my $proto (@protos) {
502 # skip protocols that do not belong to this family
503 0 ▶ if ( $fam->{family} == 6 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fam->{'family'} == 6) { } 0 0 0
504 0 ▶ next if ( $proto eq 'icmp' );
Branch: 1 branch
BranchTrueFalseTotal
if $proto eq "icmp" 0 0 0
505 } else {
506 0 ▶ next if ( $v6_icmp{$proto} );
Branch: 1 branch
BranchTrueFalseTotal
if $v6_icmp{$proto} 0 0 0
507 }
508  
509 # tarpit/delude can only target tcp, so leave the rest untouched
510 0 ▶ next if ( $tcp_only && $proto ne 'tcp' );
Branch: 1 branch
BranchTrueFalseTotal
if $tcp_only and $proto ne "tcp" 0 0 0
Condition0%0/3
$tcp_only and $proto ne "tcp"
!ll&&!rl&&r
000
MC/DC0%0/2
$tcp_only and $proto ne "tcp"
$tcp_only $proto ne "tcp"
Truth table0%0/3
A and B
A= $tcp_onlyB= $proto ne "tcp"
ABresult
0X0
100
111
511  
512 0 my $match = ' -p ' . $proto;
513 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
514 0 $match .= ' -m multiport --dports ' . $port_str;
515 }
516 0 push( @specs, { fam => $fam, match => $match } );
517 } ## end foreach my $proto (@protos)
518 } ## end else [ if ( !@protos && !@ports ) ]
519 } ## end foreach my $fam (@families)
520  
521 4 return @specs;
522 } ## end sub _rule_specs
523  
524 # Internal helper. Returns the list of commands that populate the filter chain
525 # with the block rules, based on the configured protocols and ports.
526 sub _rule_commands {
527 2 ▶ my ($self) = @_;
Subroutine _rule_commands: called CC=2 SCAR=6.9
528  
529 2 my ( undef, undef, $chain ) = $self->_set_names;
530  
531 2 my @commands;
532 2 foreach my $spec ( $self->_rule_specs ) {
533 4 my $fam = $spec->{fam};
534 push( @commands,
535 $fam->{cmd}
536 . ' -A '
537 . $chain
538 . ' -m set --match-set '
539 . $fam->{set} . ' src'
540 . $spec->{match} . ' -j '
541 4 . $fam->{tgt} );
542 }
543  
544 2 return @commands;
545 } ## end sub _rule_commands
546  
547 # Internal helper. Returns the raw table rules that exempt the tarpitted or
548 # deluded traffic from connection tracking, or an empty list for the plain
549 # drop/reject types that do not need it.
550 #
551 # TARPIT and DELUDE craft their own TCP replies (a zero-window ACK stream and a
552 # SYN/ACK-then-RST respectively) with no local socket. If conntrack tracks that
553 # traffic the kernel's own stack also processes it, which both undoes the trick
554 # and pins an INVALID conntrack entry per attacker packet. Adding a
555 # -j CT --notrack rule in raw/PREROUTING that matches exactly the same
556 # source set, protocol, and ports as the block rule keeps conntrack out of the
557 # way. These populate a same-named chain in the raw table (chain names are
558 # per-table, so it does not collide with the filter chain).
559 sub _notrack_commands {
560 2 ▶ my ($self) = @_;
Subroutine _notrack_commands: called CC=4 SCAR=14.0
561  
562 2 ▶ return () if ( !$self->_needs_notrack );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_needs_notrack 0 2 2
563  
564 2 my ( undef, undef, $chain ) = $self->_set_names;
565  
566 2 my @commands;
567 2 foreach my $spec ( $self->_rule_specs ) {
568 4 my $fam = $spec->{fam};
569  
570 # only the traffic actually handled by a reply-crafting target needs
571 # exempting from conntrack; the v6 DROP fallback delude uses does not
572 4 ▶ next if ( $fam->{tgt} !~ /^(TARPIT|DELUDE)/ );
Branch: 1 branch
BranchTrueFalseTotal
unless $fam->{"tgt"} =~ /^(TARPIT|DELUDE)/ 0 4 4
573  
574 push( @commands,
575 $fam->{cmd}
576 . ' -t raw -A '
577 . $chain
578 . ' -m set --match-set '
579 . $fam->{set} . ' src'
580 . $spec->{match}
581 4 . ' -j CT --notrack' );
582 }
583  
584 2 return @commands;
585 } ## end sub _notrack_commands
586  
587 # Internal helper. Returns the commands that create the raw table chain, fill
588 # it with the _notrack_commands, and jump to it from raw/PREROUTING. Empty
589 # unless the type needs notrack.
590 sub _raw_setup_commands {
591 2 ▶ my ($self) = @_;
Subroutine _raw_setup_commands: called CC=2 SCAR=7.0
592  
593 2 ▶ return () if ( !$self->_needs_notrack );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_needs_notrack 0 2 2
594  
595 2 my ( undef, undef, $chain ) = $self->_set_names;
596  
597 return (
598 2 'iptables -t raw -N ' . $chain,
599 'ip6tables -t raw -N ' . $chain,
600 $self->_notrack_commands,
601 'iptables -t raw -A PREROUTING -j ' . $chain,
602 'ip6tables -t raw -A PREROUTING -j ' . $chain,
603 );
604 } ## end sub _raw_setup_commands
605  
606 # Internal helper. Returns the commands that remove the raw table chain and its
607 # jump from raw/PREROUTING. Empty unless the type needs notrack.
608 sub _raw_teardown_commands {
609 2 ▶ my ($self) = @_;
Subroutine _raw_teardown_commands: called CC=2 SCAR=7.0
610  
611 2 ▶ return () if ( !$self->_needs_notrack );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_needs_notrack 0 2 2
612  
613 2 my ( undef, undef, $chain ) = $self->_set_names;
614  
615 return (
616 2 'iptables -t raw -D PREROUTING -j ' . $chain,
617 'ip6tables -t raw -D PREROUTING -j ' . $chain,
618 'iptables -t raw -F ' . $chain,
619 'ip6tables -t raw -F ' . $chain,
620 'iptables -t raw -X ' . $chain,
621 'ip6tables -t raw -X ' . $chain,
622 );
623 } ## end sub _raw_teardown_commands
624  
625 # Internal helper. Returns the conntrack commands used to drop existing
626 # connection tracking entries for the passed IP. When protocols are
627 # configured, the kill is scoped to them via -p so protocols that are not
628 # being blocked are left alone; otherwise every entry for the IP is dropped.
629 sub _kill_commands {
630 0 ▶ my ( $self, $ip ) = @_;
Subroutine _kill_commands: not called CC=13 SCAR=52.0
631  
632 # conntrack defaults to IPv4, so IPv6 IPs need the family specified
633 0 ▶ my $is_v4 = ( $ip =~ /\A$IPv4_re\z/ ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$ip =~ /\A$Net::Firewall::BlockerHelper::backends::iptables::IPv4_re\z/ ? : 0 0 0
634 0 ▶ my $family = $is_v4 ? '' : '-f ipv6 ';
Branch: 1 branch
BranchTrueFalseTotal
$is_v4 ? : 0 0 0
635  
636 0 my @protos = @{ $self->{protocols} };
637 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
638 # ports without protocols means tcp and udp are being blocked
639 0 @protos = ( 'tcp', 'udp' );
640 }
641  
642 # nothing configured means everything is being blocked, so drop every
643 # entry for the IP
644 0 ▶ if ( !@protos ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@protos) 0 0 0
645 0 return ( 'conntrack ' . $family . '-D -s ' . $ip );
646 }
647  
648 # scope the kill to the blocked protocols; ones conntrack can not filter
649 # by are skipped as are the icmps of the wrong family
650 0 my %conntrack_protos = (
651 tcp => 'tcp',
652 udp => 'udp',
653 udplite => 'udplite',
654 sctp => 'sctp',
655 dccp => 'dccp',
656 gre => 'gre',
657 icmp => 'icmp',
658 icmpv6 => 'icmpv6',
659 icmp6 => 'icmpv6',
660 'ipv6-icmp' => 'icmpv6',
661 );
662  
663 0 my @commands;
664 my %seen;
665 0 foreach my $proto (@protos) {
666 0 my $conntrack_proto = $conntrack_protos{$proto};
667 0 ▶ next if ( !defined($conntrack_proto) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $conntrack_proto 0 0 0
668 0 ▶ next if ( $conntrack_proto eq 'icmp' && !$is_v4 );
Branch: 1 branch
BranchTrueFalseTotal
if $conntrack_proto eq "icmp" and not $is_v4 0 0 0
Condition0%0/3
$conntrack_proto eq "icmp" and not $is_v4
!ll&&!rl&&r
000
MC/DC0%0/2
$conntrack_proto eq "icmp" and not $is_v4
$conntrack_proto eq "icmp" not $is_v4
Truth table0%0/3
A and not(B)
A= $conntrack_proto eq "icmp"B= not $is_v4
ABresult
0X0
100
111
669 0 ▶ next if ( $conntrack_proto eq 'icmpv6' && $is_v4 );
Branch: 1 branch
BranchTrueFalseTotal
if $conntrack_proto eq "icmpv6" and $is_v4 0 0 0
Condition0%0/3
$conntrack_proto eq "icmpv6" and $is_v4
!ll&&!rl&&r
000
MC/DC0%0/2
$conntrack_proto eq "icmpv6" and $is_v4
$conntrack_proto eq "icmpv6" $is_v4
Truth table0%0/3
A and B
A= $conntrack_proto eq "icmpv6"B= $is_v4
ABresult
0X0
100
111
670 0 ▶ next if ( $seen{$conntrack_proto} );
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$conntrack_proto} 0 0 0
671 0 $seen{$conntrack_proto} = 1;
672 0 push( @commands, 'conntrack ' . $family . '-D -p ' . $conntrack_proto . ' -s ' . $ip );
673 }
674  
675 0 return @commands;
676 } ## end sub _kill_commands
677  
678 =head2 init
679  
680 Initiates the backend. Creates the two ipsets, C<< <prefix>_<name>_4 >>
681 (hash:ip family inet) and C<< <prefix>_<name>_6 >> (hash:ip family inet6),
682 creates the C<< <prefix>_<name> >> chain via C<iptables -N> and
683 C<ip6tables -N>, populates it with the block rules built from the configured
684 type, protocols, and ports, and appends a jump to it from C<INPUT>. For the
685 tarpit and delude types a same-named chain holding the C<-j CT --notrack>
686 rules is also created in the C<raw> table and jumped to from C<PREROUTING>.
687  
688 Note that the jump is appended to the end of C<INPUT>, so earlier rules
689 accepting the traffic win out over the bans. See L</SYNOPSIS>.
690  
691 Before any of that, matching stale chains and ipsets from a previous run are
692 removed; those cleanup commands are allowed to fail.
693  
694 No arguments are taken.
695  
696 If called a second time without an intervening teardown, it will error.
697  
698 $backend->init;
699  
700 =cut
701  
702 sub init {
703 2 ▶ my ( $self, %opts ) = @_;
Subroutine init: called CC=8 SCAR=23.4
Pod documented
704  
705 2 $self->errorblank;
706  
707 2 ▶ if ( $self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"inited"}) 0 2 2
708 0 $self->{error} = 18;
709 0 $self->{errorString} = 'backend has already been inited';
710 0 $self->warn;
711 }
712  
713 2 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 2 0 2
714 2 $self->{frontend_obj}->{test_data} = {};
715 }
716  
717 2 my ( $set4, $set6, $chain ) = $self->_set_names;
718  
719 # these are cleanup commands for any stale state and are allowed to fail
720 2 my @fail_okay_commands;
721 2 push( @fail_okay_commands, 'iptables -D INPUT -j ' . $chain );
722 2 push( @fail_okay_commands, 'ip6tables -D INPUT -j ' . $chain );
723 2 push( @fail_okay_commands, 'iptables -F ' . $chain );
724 2 push( @fail_okay_commands, 'ip6tables -F ' . $chain );
725 2 push( @fail_okay_commands, 'iptables -X ' . $chain );
726 2 push( @fail_okay_commands, 'ip6tables -X ' . $chain );
727  
728 # the stale raw table notrack chain (tarpit/delude only; empty otherwise)
729 # has to go before the ipset destroys, as its rules reference the sets
730 # and a referenced set can not be destroyed
731 2 push( @fail_okay_commands, $self->_raw_teardown_commands );
732  
733 2 push( @fail_okay_commands, 'ipset destroy ' . $set4 );
734 2 push( @fail_okay_commands, 'ipset destroy ' . $set6 );
735  
736 2 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 2 0 2
737 2 $self->{frontend_obj}->{test_data}{fail_okay_commands} = \@fail_okay_commands;
738 } else {
739 0 foreach my $item (@fail_okay_commands) {
740 0 my $output = `$item 2>&1`;
741 }
742 }
743  
744 2 my @commands;
745 # create the ipsets and the chain
746 2 push( @commands, 'ipset create ' . $set4 . ' hash:ip family inet' );
747 2 push( @commands, 'ipset create ' . $set6 . ' hash:ip family inet6' );
748 2 push( @commands, 'iptables -N ' . $chain );
749 2 push( @commands, 'ip6tables -N ' . $chain );
750  
751 # add the block rules to the chain
752 2 push( @commands, $self->_rule_commands );
753  
754 # jump to the chain from INPUT
755 2 push( @commands, 'iptables -A INPUT -j ' . $chain );
756 2 push( @commands, 'ip6tables -A INPUT -j ' . $chain );
757  
758 # for tarpit/delude, exempt the matching traffic from conntrack via a raw
759 # table chain; empty for the plain drop/reject types
760 2 push( @commands, $self->_raw_setup_commands );
761  
762 2 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 2 0 2
763 2 $self->{frontend_obj}->{test_data}{commands} = \@commands;
764 } else {
765 0 foreach my $item (@commands) {
766 0 my $output = `$item 2>&1`;
767 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
768 0 $self->{error} = 23;
769 $self->{errorString}
770 0 = 'init failed. non-zero exit code for the command... "' . $item . '"... output... ' . $output;
771 0 $self->warn;
772 }
773 }
774 } ## end else [ if ( $self->{testing} ) ]
775  
776 2 $self->{inited} = 1;
777 } ## end sub init
778  
779 =head2 ban
780  
781 Bans an IP. The value of ban is validated as being a IPv4 or IPv6 address
782 and lowercased, then added to the instance's IPv4 or IPv6 ipset, as
783 appropriate for its family, via C<ipset add>. Banning an already banned IP
784 is a noop.
785  
786 If the C<kill> option is set, L<conntrack(8)> is then used to drop existing
787 connection tracking entries for the IP; its exit code is ignored, as it is
788 non-zero when there is nothing to delete.
789  
790 $backend->ban(ban => $ip);
791  
792 =cut
793  
794 sub ban {
795 0 ▶ my ( $self, %opts ) = @_;
Subroutine ban: not called CC=14 SCAR=53.5
Pod documented
796  
797 0 $self->errorblank;
798  
799 0 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 0 0
800 0 $self->{error} = 1;
801 0 $self->{errorString} = 'backend has not been inited';
802 0 $self->warn;
803 0 return;
804 }
805  
806 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::iptables::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::iptables::IPv6_re\z/) { } 0 0 0
807 0 $self->{error} = 9;
808 0 $self->{errorString} = 'Nothing specified for the value ban';
809 0 $self->warn;
810 0 return;
811 } elsif ( ref( $opts{ban} ) ne '' ) {
812 0 $self->{error} = 10;
813 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
814 0 $self->warn;
815 0 return;
816 ▶ } elsif ( $opts{ban} !~ /\A$IPv4_re\z/
Condition0%0/3
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::iptables::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::iptables::IPv6_re\z/
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::iptables::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::iptables::IPv6_re\z/
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::iptables::IPv4_re\z/ $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::iptables::IPv6_re\z/
Truth table0%0/3
A || B
A= $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::iptables::IPv4_re\z/B= $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::iptables::IPv6_re\z/
ABresult
1X1
011
000
817 && $opts{ban} !~ /\A$IPv6_re\z/ )
818 {
819 0 $self->{error} = 10;
820 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 IP';
821 0 $self->warn;
822 0 return;
823 }
824  
825 # lowercase so the same IPv6 IP in differing cases can't result in duplicate entries
826 0 $opts{ban} = lc( $opts{ban} );
827  
828 0 ▶ if ( $self->{banned}{ $opts{ban} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"banned"}{$opts{"ban"}}) 0 0 0
829 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
830 0 $self->{frontend_obj}->{test_data} = 'already banned';
831 }
832 0 return;
833 }
834  
835 0 my ( $set4, $set6 ) = $self->_set_names;
836 0 ▶ my $set = ( $opts{ban} =~ /\A$IPv4_re\z/ ) ? $set4 : $set6;
Branch: 1 branch
BranchTrueFalseTotal
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::iptables::IPv4_re\z/ ? : 0 0 0
837  
838 0 my $command = 'ipset add ' . $set . ' ' . $opts{ban};
839  
840 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
841 0 $self->{frontend_obj}->{test_data} = [$command];
842 } else {
843 0 my $output = `$command 2>&1`;
844 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
845 0 $self->{error} = 13;
846 $self->{errorString}
847 0 = 'ban failed. non-zero exit code for the command... "' . $command . '"... output... ' . $output;
848 0 $self->warn;
849 }
850 }
851  
852 0 ▶ if ( $self->{options}{kill} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"options"}{"kill"}) 0 0 0
853 0 foreach my $kill_command ( $self->_kill_commands( $opts{ban} ) ) {
854 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
855 0 push( @{ $self->{frontend_obj}->{test_data} }, $kill_command );
856 } else {
857 # conntrack returns non-zero when there is nothing to delete,
858 # so its exit code is intentionally ignored
859 0 my $output = `$kill_command 2>&1`;
860 }
861 }
862 } ## end if ( $self->{options}{kill} )
863  
864 0 $self->{banned}{ $opts{ban} } = 1;
865 } ## end sub ban
866  
867 =head2 unban
868  
869 Unbans an IP. The value of ban is validated as being a IPv4 or IPv6 address
870 and lowercased, then removed from the instance's IPv4 or IPv6 ipset, as
871 appropriate for its family, via C<ipset del>. Unbanning an IP that is not
872 banned is a noop.
873  
874 $backend->unban(ban => $ip);
875  
876 =cut
877  
878 sub unban {
879 0 ▶ my ( $self, %opts ) = @_;
Subroutine unban: not called CC=11 SCAR=48.8
Pod documented
880  
881 0 $self->errorblank;
882  
883 0 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 0 0
884 0 $self->{error} = 1;
885 0 $self->{errorString} = 'backend has not been inited';
886 0 $self->warn;
887 0 return;
888 }
889  
890 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::iptables::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::iptables::IPv6_re\z/) { } 0 0 0
891 0 $self->{error} = 9;
892 0 $self->{errorString} = 'Nothing specified for the value ban';
893 0 $self->warn;
894 0 return;
895 } elsif ( ref( $opts{ban} ) ne '' ) {
896 0 $self->{error} = 10;
897 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
898 0 $self->warn;
899 0 return;
900 ▶ } elsif ( $opts{ban} !~ /\A$IPv4_re\z/
Condition0%0/3
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::iptables::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::iptables::IPv6_re\z/
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::iptables::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::iptables::IPv6_re\z/
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::iptables::IPv4_re\z/ $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::iptables::IPv6_re\z/
Truth table0%0/3
A || B
A= $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::iptables::IPv4_re\z/B= $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::iptables::IPv6_re\z/
ABresult
1X1
011
000
901 && $opts{ban} !~ /\A$IPv6_re\z/ )
902 {
903 0 $self->{error} = 10;
904 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 IP';
905 0 $self->warn;
906 0 return;
907 }
908  
909 # lowercase so the same IPv6 IP in differing cases can't result in duplicate entries
910 0 $opts{ban} = lc( $opts{ban} );
911  
912 0 ▶ if ( !$self->{banned}{ $opts{ban} } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"banned"}{$opts{"ban"}}) 0 0 0
913 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
914 0 $self->{frontend_obj}->{test_data} = 'not banned';
915 }
916 0 return;
917 }
918  
919 0 my ( $set4, $set6 ) = $self->_set_names;
920 0 ▶ my $set = ( $opts{ban} =~ /\A$IPv4_re\z/ ) ? $set4 : $set6;
Branch: 1 branch
BranchTrueFalseTotal
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::iptables::IPv4_re\z/ ? : 0 0 0
921  
922 0 my $command = 'ipset del ' . $set . ' ' . $opts{ban};
923  
924 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
925 0 $self->{frontend_obj}->{test_data} = $command;
926 } else {
927 0 my $output = `$command 2>&1`;
928 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
929 0 $self->{error} = 14;
930 $self->{errorString}
931 0 = 'unban failed. non-zero exit code for the command... "' . $command . '"... output... ' . $output;
932 0 $self->warn;
933 }
934 }
935  
936 0 delete( $self->{banned}{ $opts{ban} } );
937 } ## end sub unban
938  
939 =head2 list
940  
941 List banned IPs. Returns an array of the currently banned IPs from the
942 retained ban state; the ipsets themselves are not queried.
943  
944 my @banned = $backend->list;
945  
946 =cut
947  
948 sub list {
949 0 ▶ my ( $self, %opts ) = @_;
Subroutine list: not called CC=2 SCAR=17.9
Pod documented
950  
951 0 $self->errorblank;
952  
953 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
954 0 $self->{frontend_obj}->{test_data} = 'list';
955 }
956  
957 0 return keys( %{ $self->{banned} } );
958 }
959  
960 =head2 re_init
961  
962 Tells the backend to re-init it's self.
963  
964 Calls teardown, with any errors from it ignored as a partially wiped setup
965 is exactly what re_init exists to recover from, then init, recreating the
966 ipsets, chain, and rules. Each previously banned IP is then re-added to the
967 relevant ipset via C<ipset add>.
968  
969 $backend->re_init;
970  
971 =cut
972  
973 sub re_init {
974 0 ▶ my ( $self, %opts ) = @_;
Subroutine re_init: not called CC=7 SCAR=40.3
Pod documented
975  
976 0 $self->errorblank;
977  
978 0 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 0 0
979 0 $self->{error} = 1;
980 0 $self->{errorString} = 'backend has not been inited';
981 0 $self->warn;
982 0 return;
983 }
984  
985 # teardown is best effort here as a partially or fully wiped setup is
986 # exactly what re_init needs to recover from; init cleans up any remnants
987 {
988 0 local $@;
989 0 eval { $self->teardown; };
990 }
991 0 $self->init;
992  
993 0 my ( $set4, $set6 ) = $self->_set_names;
994  
995 0 my @to_ban = keys( %{ $self->{banned} } );
996  
997 0 my @re_init_test_data;
998 0 foreach my $item (@to_ban) {
999 0 ▶ my $set = ( $item =~ /\A$IPv4_re\z/ ) ? $set4 : $set6;
Branch: 1 branch
BranchTrueFalseTotal
$item =~ /\A$Net::Firewall::BlockerHelper::backends::iptables::IPv4_re\z/ ? : 0 0 0
1000 0 my $command = 'ipset add ' . $set . ' ' . $item;
1001  
1002 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
1003 0 push( @re_init_test_data, $command );
1004 } else {
1005 0 my $output = `$command 2>&1`;
1006 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
1007 0 $self->{error} = 13;
1008 $self->{errorString}
1009 0 = 'ban failed. non-zero exit code for the command... "' . $command . '"... output... ' . $output;
1010 0 $self->warn;
1011 }
1012 }
1013 } ## end foreach my $item (@to_ban)
1014  
1015 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
1016 0 $self->{frontend_obj}->{test_data} = \@re_init_test_data;
1017 }
1018  
1019 0 $self->{inited} = 1;
1020 } ## end sub re_init
1021  
1022 =head2 teardown
1023  
1024 Tears down the setup for the backend.
1025  
1026 Removes the jump from C<INPUT>, flushes and deletes the chain, and destroys
1027 both ipsets, via C<iptables>, C<ip6tables>, and C<ipset destroy>. For the
1028 tarpit and delude types the raw table chain and its C<PREROUTING> jump are
1029 removed as well, before the ipsets its rules reference are destroyed. The
1030 retained ban state is kept, so a later init/re_init can restore the bans.
1031  
1032 It does not check if the backend has been inited, so if called prior to
1033 init the removal commands will fail and it will error.
1034  
1035 $backend->teardown;
1036  
1037 =cut
1038  
1039 sub teardown {
1040 0 ▶ my ( $self, %opts ) = @_;
Subroutine teardown: not called CC=4 SCAR=30.0
Pod documented
1041  
1042 0 $self->errorblank;
1043  
1044 0 $self->{inited} = 0;
1045  
1046 0 my ( $set4, $set6, $chain ) = $self->_set_names;
1047  
1048 0 my @commands;
1049 0 push( @commands, 'iptables -D INPUT -j ' . $chain );
1050 0 push( @commands, 'ip6tables -D INPUT -j ' . $chain );
1051 0 push( @commands, 'iptables -F ' . $chain );
1052 0 push( @commands, 'ip6tables -F ' . $chain );
1053 0 push( @commands, 'iptables -X ' . $chain );
1054 0 push( @commands, 'ip6tables -X ' . $chain );
1055  
1056 # remove the raw notrack chain (tarpit/delude only) before destroying the
1057 # ipsets, as its rules reference them; empty otherwise
1058 0 push( @commands, $self->_raw_teardown_commands );
1059  
1060 0 push( @commands, 'ipset destroy ' . $set4 );
1061 0 push( @commands, 'ipset destroy ' . $set6 );
1062  
1063 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
1064 0 $self->{frontend_obj}->{test_data} = \@commands;
1065 } else {
1066 0 foreach my $item (@commands) {
1067 0 my $output = `$item 2>&1`;
1068 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
1069 0 $self->{error} = 17;
1070 $self->{errorString}
1071 0 = 'teardown failed. non-zero exit code for the command... "' . $item . '"... output... ' . $output;
1072 0 $self->warn;
1073 }
1074 }
1075 } ## end else [ if ( $self->{testing} ) ]
1076 } ## end sub teardown
1077  
1078 =head2 stop
1079  
1080 Alias for L</teardown>, provided for parity with the fail2ban C<actionstop>
1081 concept.
1082  
1083 $backend->stop;
1084  
1085 =cut
1086  
1087 sub stop {
1088 0 ▶ my ( $self, %opts ) = @_;
Subroutine stop: not called CC=1 SCAR=6.9
Pod documented
1089  
1090 0 return $self->teardown(%opts);
1091 }
1092  
1093 =head2 check
1094  
1095 Verifies that the ipsets, the chain jump, and each of the block rules in the
1096 chain are still in place. Returns a true value if the setup is intact and a
1097 false value if any part is missing. This is the equivalent of fail2ban's
1098 C<actioncheck>.
1099  
1100 if ( !$backend->check ) {
1101 $backend->re_init;
1102 }
1103  
1104 =cut
1105  
1106 sub check {
1107 0 ▶ my ( $self, %opts ) = @_;
Subroutine check: not called CC=6 SCAR=37.4
Pod documented
1108  
1109 0 $self->errorblank;
1110  
1111 0 my ( $set4, $set6, $chain ) = $self->_set_names;
1112  
1113 # reuse the -A commands that populate the chains as -C existence checks, so
1114 # every block rule (and, for tarpit/delude, every raw notrack rule) the
1115 # setup should contain is verified
1116 0 my @rule_checks = ( $self->_rule_commands, $self->_notrack_commands );
1117 0 foreach my $item (@rule_checks) {
1118 0 $item =~ s/ -A / -C /;
1119 }
1120  
1121 0 my @commands = (
1122 'ipset list ' . $set4,
1123 'ipset list ' . $set6,
1124 'iptables -C INPUT -j ' . $chain,
1125 'ip6tables -C INPUT -j ' . $chain,
1126 @rule_checks,
1127 );
1128  
1129 # the raw table jump only exists for tarpit/delude; empty otherwise
1130 0 ▶ if ( $self->_needs_notrack ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_needs_notrack) 0 0 0
1131 0 push( @commands, 'iptables -t raw -C PREROUTING -j ' . $chain );
1132 0 push( @commands, 'ip6tables -t raw -C PREROUTING -j ' . $chain );
1133 }
1134  
1135 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
1136 0 $self->{frontend_obj}->{test_data} = \@commands;
1137 0 return 1;
1138 }
1139  
1140 0 foreach my $item (@commands) {
1141 0 my $output = `$item 2>&1`;
1142 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
1143 0 return 0;
1144 }
1145 }
1146  
1147 0 return 1;
1148 } ## end sub check
1149  
1150 =head2 flush
1151  
1152 Removes all currently banned IPs at once by flushing the ipsets, leaving the
1153 chain and rules in place. This is the equivalent of fail2ban's
1154 C<actionflush>.
1155  
1156 $backend->flush;
1157  
1158 =cut
1159  
1160 sub flush {
1161 0 ▶ my ( $self, %opts ) = @_;
Subroutine flush: not called CC=5 SCAR=34.0
Pod documented
1162  
1163 0 $self->errorblank;
1164  
1165 0 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 0 0
1166 0 $self->{error} = 1;
1167 0 $self->{errorString} = 'backend has not been inited';
1168 0 $self->warn;
1169 0 return;
1170 }
1171  
1172 0 my ( $set4, $set6 ) = $self->_set_names;
1173  
1174 0 my @commands = ( 'ipset flush ' . $set4, 'ipset flush ' . $set6 );
1175  
1176 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
1177 0 $self->{frontend_obj}->{test_data} = \@commands;
1178 } else {
1179 0 foreach my $item (@commands) {
1180 0 my $output = `$item 2>&1`;
1181 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
1182 0 $self->{error} = 25;
1183 $self->{errorString}
1184 0 = 'flush failed. non-zero exit code for the command... "' . $item . '"... output... ' . $output;
1185 0 $self->warn;
1186 }
1187 }
1188 } ## end else [ if ( $self->{testing} ) ]
1189  
1190 0 $self->{banned} = {};
1191 } ## end sub flush
1192  
1193 # Internal helper. Returns a true value if the passed scalar is a valid IPv4 or
1194 # IPv6 CIDR range, that is an address followed by "/" and a prefix length that
1195 # is within the range valid for its family (0 to 32 for IPv4, 0 to 128 for
1196 # IPv6). Returns false otherwise.
1197 sub _valid_cidr {
1198 0 ▶ my ( $self, $cidr ) = @_;
Subroutine _valid_cidr: not called CC=8 SCAR=42.8
1199  
1200 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
1201  
1202 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
1203 0 my ( $addr, $prefix ) = ( $1, $2 );
1204 0 ▶ return 1 if ( $addr =~ /\A$IPv4_re\z/ && $prefix <= 32 );
Branch: 1 branch
BranchTrueFalseTotal
if $addr =~ /\A$Net::Firewall::BlockerHelper::backends::iptables::IPv4_re\z/ and $prefix <= 32 0 0 0
Condition0%0/3
$addr =~ /\A$Net::Firewall::BlockerHelper::backends::iptables::IPv4_re\z/ and $prefix <= 32
!ll&&!rl&&r
000
MC/DC0%0/2
$addr =~ /\A$Net::Firewall::BlockerHelper::backends::iptables::IPv4_re\z/ and $prefix <= 32
$addr =~ /\A$Net::Firewall::BlockerHelper::backends::iptables::IPv4_re\z/ $prefix <= 32
Truth table0%0/3
A and B
A= $addr =~ /\A$Net::Firewall::BlockerHelper::backends::iptables::IPv4_re\z/B= $prefix <= 32
ABresult
0X0
100
111
1205 0 ▶ return 1 if ( $addr =~ /\A$IPv6_re\z/ && $prefix <= 128 );
Branch: 1 branch
BranchTrueFalseTotal
if $addr =~ /\A$Net::Firewall::BlockerHelper::backends::iptables::IPv6_re\z/ and $prefix <= 128 0 0 0
Condition0%0/3
$addr =~ /\A$Net::Firewall::BlockerHelper::backends::iptables::IPv6_re\z/ and $prefix <= 128
!ll&&!rl&&r
000
MC/DC0%0/2
$addr =~ /\A$Net::Firewall::BlockerHelper::backends::iptables::IPv6_re\z/ and $prefix <= 128
$addr =~ /\A$Net::Firewall::BlockerHelper::backends::iptables::IPv6_re\z/ $prefix <= 128
Truth table0%0/3
A and B
A= $addr =~ /\A$Net::Firewall::BlockerHelper::backends::iptables::IPv6_re\z/B= $prefix <= 128
ABresult
0X0
100
111
1206 }
1207  
1208 0 return 0;
1209 } ## end sub _valid_cidr
1210  
1211 =head2 ban_cidr
1212  
1213 CIDR bans are not supported by this backend; this always sets the
1214 cidrNotSupported error.
1215  
1216 $backend->ban_cidr(ban => '1.2.3.0/24');
1217  
1218 =cut
1219  
1220 sub ban_cidr {
1221 0 ▶ my ( $self, %opts ) = @_;
Subroutine ban_cidr: not called CC=1 SCAR=6.9
Pod documented
1222  
1223 0 $self->errorblank;
1224  
1225 0 $self->{error} = 29;
1226 0 $self->{errorString} = 'the ' . __PACKAGE__ . ' backend does not support CIDR bans';
1227 0 $self->warn;
1228  
1229 0 return;
1230 } ## end sub ban_cidr
1231  
1232 =head2 unban_cidr
1233  
1234 CIDR bans are not supported by this backend; this always sets the
1235 cidrNotSupported error.
1236  
1237 $backend->unban_cidr(ban => '1.2.3.0/24');
1238  
1239 =cut
1240  
1241 sub unban_cidr {
1242 0 ▶ my ( $self, %opts ) = @_;
Subroutine unban_cidr: not called CC=1 SCAR=6.9
Pod documented
1243  
1244 0 $self->errorblank;
1245  
1246 0 $self->{error} = 29;
1247 0 $self->{errorString} = 'the ' . __PACKAGE__ . ' backend does not support CIDR bans';
1248 0 $self->warn;
1249  
1250 0 return;
1251 } ## end sub unban_cidr
1252  
1253 =head2 list_cidr
1254  
1255 CIDR bans are not supported by this backend, so this always returns an empty
1256 list.
1257  
1258 my @banned_cidrs = $backend->list_cidr;
1259  
1260 =cut
1261  
1262 sub list_cidr {
1263 0 ▶ my ( $self, %opts ) = @_;
Subroutine list_cidr: not called CC=1 SCAR=6.9
Pod documented
1264  
1265 0 $self->errorblank;
1266  
1267 0 return ();
1268 }
1269  
1270 =head1 ERROR CODES / FLAGS
1271  
1272 Error handling is provided by L<Error::Helper>. All
1273 errors are considered fatal.
1274  
1275 =head2 1, notInited
1276  
1277 The backend has not been inited yet.
1278  
1279 =head2 2, invalidPortSpecified
1280  
1281 Port is either not an int within the range 1 to 65535 or a name that can be resolved by getservbyname.
1282  
1283 =head2 3, portsNotArray
1284  
1285 The data passed to new for ports is not an array.
1286  
1287 =head2 4, protocolsNotArray
1288  
1289 The data passed to new for protocols is not an array.
1290  
1291 =head2 5, invalidPortSpecified
1292  
1293 Port is either not an int within the range 1 to 65535 or a name that can be resolved by getservbyname.
1294  
1295 =head2 6, invalidPrefixSpecified
1296  
1297 The specified prefix did not match /^[a-zA-Z0-9]+$/.
1298  
1299 =head2 7, invalidName
1300  
1301 The name is either undef or does not match /^[a-zA-Z0-9\-]+$/.
1302  
1303 =head2 8, optionsNotHash
1304  
1305 The item passed to new for options is not a hash.
1306  
1307 =head2 9, noBanItem
1308  
1309 No IP specified to ban or unban.
1310  
1311 =head2 10, banItemNotIP
1312  
1313 The item to ban is not an IP. Either wrong ref type or regexp
1314 test using L<Regexp::IPv4> and L<Regexp::IPv6> failed.
1315  
1316 =head2 11, invalidBackend
1317  
1318 The specified backend failed to pass a basic sanity check of making sure it
1319 matches the regexp /^[a-zA-Z0-9\_]+$/.
1320  
1321 =head2 12, backendInitError
1322  
1323 Failed to init the backend.
1324  
1325 =head2 13, banFailed
1326  
1327 Failed to ban the item.
1328  
1329 =head2 14, unbanFailed
1330  
1331 Failed to unban the item.
1332  
1333 =head2 15, listFailed
1334  
1335 Failed to get a list of bans.
1336  
1337 =head2 16, reInitFailed
1338  
1339 Failed to re_init the backend.
1340  
1341 =head2 17, teardownFailed
1342  
1343 Failed to teardown the backend.
1344  
1345 =head2 18, alreadyInited
1346  
1347 init called, but the backend has already been inited.
1348  
1349 =head2 20, typeInvalid
1350  
1351 The option type is not one of "drop", "reject", "tarpit", or "delude", or the
1352 option tarpit_mode is not one of "tarpit", "honeypot", or "reset".
1353  
1354 =head2 21, nameTooLong
1355  
1356 The combined prefix and name is longer than 28 characters, the max iptables
1357 chain name length.
1358  
1359 =head2 23, initFailed
1360  
1361 One of the required commands for init failed.
1362  
1363 =head2 24, checkFailed
1364  
1365 The backend check raised an error.
1366  
1367 =head2 25, flushFailed
1368  
1369 Failed to flush the bans.
1370  
1371 =head2 26, banCidrFailed
1372  
1373 Failed to ban the CIDR range.
1374  
1375 =head2 27, unbanCidrFailed
1376  
1377 Failed to unban the CIDR range.
1378  
1379 =head2 28, cidrItemNotCidr
1380  
1381 The item to ban is not a CIDR range. Either wrong ref type or it is not an
1382 IPv4 or IPv6 address followed by a prefix length valid for its family.
1383  
1384 =head2 29, cidrNotSupported
1385  
1386 The backend does not support CIDR bans.
1387  
1388 =head2 30, listCidrFailed
1389  
1390 Failed to get a list of CIDR bans.
1391  
1392 =head1 AUTHOR
1393  
1394 Zane C. Bowers-Hadley, C<< <vvelox at vvelox.ent> >>
1395  
1396 =head1 BUGS
1397  
1398 Please report any bugs or feature requests to C<bug-net-firewall-blockerhelper at rt.cpan.org>, or through
1399 the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Net-Firewall-BlockerHelper>. I will be notified, and then you'll
1400 automatically be notified of progress on your bug as I make changes.
1401  
1402  
1403  
1404  
1405 =head1 SUPPORT
1406  
1407 You can find documentation for this module with the perldoc command.
1408  
1409 perldoc Net::Firewall::BlockerHelper
1410  
1411  
1412 You can also look for information at:
1413  
1414 =over 4
1415  
1416 =item * RT: CPAN's request tracker (report bugs here)
1417  
1418 L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=Net-Firewall-BlockerHelper>
1419  
1420 =item * Search CPAN
1421  
1422 L<https://metacpan.org/release/Net-Firewall-BlockerHelper>
1423  
1424 =back
1425  
1426  
1427 =head1 ACKNOWLEDGEMENTS
1428  
1429  
1430 =head1 LICENSE AND COPYRIGHT
1431  
1432 This software is Copyright (c) 2023 by Zane C. Bowers-Hadley.
1433  
1434 This is free software, licensed under:
1435  
1436 The GNU Lesser General Public License, Version 2.1, February 1999
1437  
1438  
1439 =cut
1440  
1441 3 1; # End of Net::Firewall::BlockerHelper
« BlockerHelper/backends/firewalld.pm ↑ Summary BlockerHelper/backends/pf.pm »