BlockerHelper/backends/ipfw.pm

filter: Statementstmt 29.4% 130 / 441 Branchbran 32.9% 64 / 194 Conditioncond 34.1% 44 / 129 MC/DCmcdc 3.0% 2 / 65 Subroutinesub 50.0% 10 / 20 Podpod 100% 13 / 13 totaltotal 30.5% 263 / 862 CC 151 SCAR 61.4
CC
151
Cov
31%
new
1062.9
unban
110.0
ban_cidr
90.0
CRAP 462.8
↑ Summary BlockerHelper/backends/ufw.pm »
1 package Net::Firewall::BlockerHelper::backends::ipfw;
2  
3 4 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use base 'Error::Helper';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use Regexp::IPv4 qw($IPv4_re);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use Regexp::IPv6 qw($IPv6_re);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 =head1 NAME
11  
12 Net::Firewall::BlockerHelper::backends::ipfw - IPFW backend for Net::Firewall::BlockerHelper.
13  
14 =head1 VERSION
15  
16 Version 0.1.0
17  
18 =cut
19  
20 4 our $VERSION = '0.1.0';
21  
22 =head1 SYNOPSIS
23  
24 The block rules this backend creates are added straight into the main
25 ruleset at the configured rule number, the C<rule> option, default 150, so
26 no additional wiring is needed for them to be evaluated. What does need
27 minding is placement, as ipfw is first match wins. The rule number needs to
28 be lower than that of any rule that would pass the traffic in question,
29 such as a C<check-state> or an allow rule, otherwise the bans will never
30 match anything.
31  
32 use Net::Firewall::BlockerHelper::backends::ipfw;
33  
34 my $backend1;
35 my $backend2;
36 eval {
37 $backend1 = Net::Firewall::BlockerHelper::backends::ipfw->new(
38 backend => 'ipfw',
39 name => 'all',
40 options=>{ rule=>150, kill=>1 },
41 );
42 $backend2 = Net::Firewall::BlockerHelper::backends::ipfw->new(
43 backend => 'ipfw',
44 ports => ['143'],
45 protocols => ['tcp'],
46 name => 'imap',
47 options=>{ rule=>151 },
48 );
49 };
50 if ($@) {
51 print 'Error: '
52 . $Error::Helper::error
53 . "\nError String: "
54 . $Error::Helper::errorString
55 . "\nError Flag: "
56 . $Error::Helper::errorFlag . "\n";
57 }
58  
59 print `ipfw list`
60  
61 $backend1->init;
62 $backend2->init;
63  
64 print `ipfw list`
65  
66 $backend1->ban(ban=>'1.2.3.4');
67 $backend1->ban(ban=>'4.3.2.1');
68 $backend2->ban(ban=>'4.3.2.1');
69  
70 use Data::Dumper;
71 print Dumper($backend1->list);
72 print Dumper($backend2->list);
73  
74 $backend1->unban(ban=>'4.3.2.1');
75  
76 use Data::Dumper;
77 print Dumper($backend1->list);
78 print Dumper($backend2->list);
79  
80 $backend1->teardown;
81  
82 print `ipfw list`
83  
84 $backend2->teardown;
85  
86 print `ipfw list`
87  
88 =head1 METHODS
89  
90 =head2 new
91  
92 Initiates the object. Takes the arguments below.
93  
94 - options :: Backend specific options that will be passed to the backend unchecked
95 outside of making sure it is a hash ref if defined. See below for furhter info.
96 - Default :: {}
97  
98 - ports :: A array of ports to block. Checked to make sure they are positive ints or a valid
99 service name via getservbyname. All ports will be blocked if non are specified. If
100 duplicates are removed.
101 - Default :: []
102  
103 - protocols :: A array of protocols to block. This is checked against
104 /etc/protocols via the function getprotobyname. Duplicates will be
105 discarded. If no protocols are given, all IPv4 and IPv6 traffic is
106 blocked ('ip4' and 'ip6'), unless ports are given, in which case it
107 defaults to tcp and udp. Ports are only attached to port-capable
108 protocols (tcp/udp/sctp); other protocols are blocked without a
109 port. As the ipfw 'me' keyword only matches IPv4 addresses and
110 'me6' only IPv6 ones, family neutral protocols such as tcp/udp
111 get one rule per family while family specific ones such as icmp or
112 ip6 get a single rule for their family.
113 - Default :: ['ip4','ip6'], or ['tcp','udp'] when ports are given
114  
115 - prefix :: Prefix to use. Must match the regex /^[a-zA-Z0-9]+$/
116 - default :: kur
117  
118 - name :: Name of this specific instance. This must be specified.
119 - default :: undef
120  
121 The options hash accepts the following.
122  
123 - rule :: The rule number to use for the IPFW rules. This should not
124 be re-used or it will result in the other rules at that number
125 being removed when init is called.
126 - Default :: 150
127  
128 - type :: The drop method to use. 'deny' silently drops. 'unreach' and
129 'unreach6' both mean reject, and are treated as synonyms: the
130 family of each generated rule decides whether an IPv4 unreach or
131 an IPv6 unreach6 is sent, using the unreach and unreach6 codes
132 below. See ipfw(8) for more info.
133 - Default :: deny
134  
135 - unreach :: The IPv4 unreach code to use when type is a reject. Applied
136 to IPv4 protocols. See ipfw(8) for more info.
137 - Default :: port
138  
139 - unreach6 :: The IPv6 unreach6 code to use when type is a reject. Applied
140 to IPv6 protocols. See ipfw(8) for more info.
141 - Default :: port
142  
143 - kill :: Use tcpdrop to kill TCP connections for that IP. Handles both
144 IPv4 and IPv6, picking the sockstat family and parsing per the
145 family of the banned IP. TCP only, as FreeBSD has no tcpdrop
146 equivalent for UDP; since the generated rules are stateless, UDP
147 from the banned IP is blocked immediately regardless, so there is
148 no lingering flow state to clear. If protocols are configured and
149 tcp is not among them, nothing is killed, so blocking only udp
150 never drops TCP connections.
151 - Default :: 0
152  
153 All errors are considered fatal, meaning if new fails it will die.
154  
155 my $backend;
156 eval {
157 $backend = Net::Firewall::BlockerHelper::backends::ipfw->new(
158 backend => 'ipfw',
159 ports => ['22'],
160 protocols => ['tcp'],
161 name => 'ssh',
162 );
163 };
164 if ($@) {
165 print 'Error: '
166 . $Error::Helper::error
167 . "\nError String: "
168 . $Error::Helper::errorString
169 . "\nError Flag: "
170 . $Error::Helper::errorFlag . "\n";
171 }
172  
173 =cut
174  
175 sub new {
176 4 ▶ my ( $blank, %opts ) = @_;
Subroutine new: called CC=69 SCAR=69.7
Pod documented
177  
178 4 my $self = {
179 perror => undef,
180 error => undef,
181 errorLine => undef,
182 errorFilename => undef,
183 errorString => "",
184 errorExtra => {
185 all_errors_fatal => 1,
186 # all_fatal is what Error::Helper 2.1.0 actually checks; all_errors_fatal
187 # is kept for the name documented in its POD
188 all_fatal => 1,
189 flags => {
190 1 => 'notInited',
191 2 => 'invalidPortSpecified',
192 3 => 'portsNotArray',
193 4 => 'protocolsNotArray',
194 5 => 'invalidPortSpecified',
195 6 => 'invalidPrefixSpecified',
196 7 => 'invalidName',
197 8 => 'optionsNotHash',
198 9 => 'noBanItem',
199 10 => 'banItemNotIP',
200 11 => 'invalidBackend',
201 12 => 'backendInitError',
202 13 => 'banFailed',
203 14 => 'unbanFailed',
204 15 => 'listFailed',
205 16 => 'reInitFailed',
206 17 => 'teardownFailed',
207 18 => 'alreadyInited',
208 19 => 'ruleInvalid',
209 20 => 'typeInvalid',
210 21 => 'unreachInvalid',
211 22 => 'unreach6Invalid',
212 23 => 'initFailed',
213 26 => 'nameTooLong',
214 24 => 'checkFailed',
215 25 => 'flushFailed',
216 27 => 'banCidrFailed',
217 28 => 'unbanCidrFailed',
218 29 => 'cidrItemNotCidr',
219 30 => 'cidrNotSupported',
220 31 => 'listCidrFailed',
221 },
222 fatal_flags => {},
223 perror_not_fatal => 0,
224 },
225 options => {
226 rule => 150,
227 type => 'deny',
228 unreach => 'port',
229 unreach6 => 'port',
230 kill => 0,
231 },
232 ports => [],
233 protocols => [],
234 testing => undef,
235 test_data => undef,
236 prefix => 'kur',
237 frontend_obj => undef,
238 inited => 0,
239 banned => {},
240 banned_cidr => {},
241 cidr_supported => 1,
242 };
243 4 bless $self;
244  
245 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
246 0 $self->{perror} = 1;
247 0 $self->{error} = 3;
248 0 $self->{errorString} = 'ports is defined and type is not array but "' . ref( $opts{ports} ) . '"';
249 0 $self->warn;
250 } elsif ( defined( $opts{ports} ) ) {
251 2 my %ports;
252 2 foreach my $item ( @{ $opts{ports} } ) {
253 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
254 1 $ports{$item} = 1;
255 } elsif ( $item =~ /^[0-9]+$/ ) {
256 0 $self->{perror} = 1;
257 0 $self->{error} = 2;
258 $self->{errorString}
259 0 = $item . ' is not a valid value for a port as it must be a int within the range 1 to 65535';
260 0 $self->warn;
261 } else {
262 # just using tcp here as protocol must be specified
263 0 my ( $name, $aliases, $port, $proto ) = getservbyname( $item, 'tcp' );
264 0 ▶ if ( !defined($port) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $port) 0 0 0
265 0 $self->{perror} = 1;
266 0 $self->{error} = 2;
267 $self->{errorString}
268 0 = $item . ' could not be resolved to a port name via getservbyname("' . $item . '", "tcp")';
269 0 $self->warn;
270 }
271 0 $ports{$port} = 1;
272 } ## end else [ if ( $item =~ /^[0-9]+$/ && $item >= 1 ) ]
273 } ## end foreach my $item ( @{ $opts{ports} } )
274 2 my @port_keys = keys(%ports);
275 2 @port_keys = sort { $a <=> $b } @port_keys;
276 2 push( @{ $self->{ports} }, @port_keys );
277 } ## end elsif ( defined( $opts{ports} ) )
278  
279 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
280 0 $self->{perror} = 1;
281 0 $self->{error} = 4;
282 0 $self->{errorString} = 'protocols is defined and type is not array but "' . ref( $opts{protocols} ) . '"';
283 0 $self->warn;
284 } elsif ( defined( $opts{protocols} ) ) {
285 2 my %protocols;
286 2 foreach my $item ( @{ $opts{protocols} } ) {
287 0 my ( $name, $aliases, $proto ) = getprotobyname($item);
288 # if this is undef, it means it is not a known protocol
289 0 ▶ if ( !defined($proto) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $proto) 0 0 0
290 0 $self->{perror} = 1;
291 0 $self->{error} = 5;
292 $self->{errorString}
293 0 = $item . ' could not be resolved to a protocol via getprotobyname("' . $item . '")';
294 0 $self->warn;
295 }
296 0 $protocols{$item} = 1;
297 } ## end foreach my $item ( @{ $opts{protocols} } )
298 2 my @protocols_keys = keys(%protocols);
299 2 @protocols_keys = sort { $a cmp $b } @protocols_keys;
300 2 push( @{ $self->{protocols} }, @protocols_keys );
301 } ## end elsif ( defined( $opts{protocols} ) )
302  
303 # make sure prefix is sane if defiend
304 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
305 0 $self->{perror} = 1;
306 0 $self->{error} = 6;
307 $self->{errorString}
308 0 = '"' . $opts{prefix} . '" is not a valid prefix as it does not match the regex /^[a-zA-Z0-9]+$/';
309 0 $self->warn;
310 } elsif ( defined( $opts{prefix} ) ) {
311 4 $self->{prefix} = $opts{prefix};
312 }
313  
314 # make sure we have a name and that it is valid
315 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
316 0 $self->{perror} = 1;
317 0 $self->{error} = 7;
318 0 $self->{errorString} = 'name is undef';
319 0 $self->warn;
320 } elsif ( $opts{name} !~ /^[a-zA-Z0-9\-]+$/ ) {
321 0 $self->{perror} = 1;
322 0 $self->{error} = 7;
323 0 $self->{errorString} = 'name set to "' . $opts{name} . '" which does not match the regexp /^[a-zA-Z0-9\-]+$/';
324 0 $self->warn;
325 }
326 4 $self->{name} = $opts{name};
327  
328 # ipfw limits table names to 63 characters and the table is
329 # <prefix>_<name>, so catch over-long combos here rather than as a
330 # confusing error at init
331 4 ▶ if ( defined( $self->{name} ) && length( $self->{prefix} . '_' . $self->{name} ) > 63 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"name"} and length $self->{"prefix"} . '_' . $self->{"name"} > 63) 1 3 4
Condition66%2/3
defined $self->{"name"} and length $self->{"prefix"} . '_' . $self->{"name"} > 63
!ll&&!rl&&r
031
MC/DC50%1/2
defined $self->{"name"} and length $self->{"prefix"} . '_' . $self->{"name"} > 63
defined $self->{"name"} length $self->{"prefix"} . '_' . $self->{"name"} > 63
Truth table66%2/3
A and B
A= defined $self->{"name"}B= length $self->{"prefix"} . '_' . $self->{"name"} > 63
ABresult
0X0
100
111
332 1 $self->{perror} = 1;
333 1 $self->{error} = 26;
334 $self->{errorString}
335 = 'the combined prefix and name, "'
336 . $self->{prefix} . '_'
337 . $self->{name}
338 1 . '", is longer than 63 characters, the max ipfw table name length';
339 1 $self->warn;
340 }
341  
342 # used internally for testing
343 3 ▶ if ( defined( $opts{testing} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"testing"}) 2 1 3
344 2 $self->{testing} = $opts{testing};
345 }
346 3 ▶ if ( defined( $opts{frontend_obj} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"frontend_obj"}) 2 1 3
347 2 $self->{frontend_obj} = $opts{frontend_obj};
348 }
349  
350 3 ▶ if ( defined( $opts{options} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"options"}) 2 1 3
351 2 ▶ if ( ref( $opts{options} ) ne 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $opts{"options"} ne "HASH") 0 2 2
352 0 $self->{perror} = 1;
353 0 $self->{error} = 8;
354 0 $self->{errorString} = 'ref for options is "' . ref( $opts{options} ) . '" and not HASH';
355 0 $self->warn;
356 }
357 2 $self->{options} = $opts{options};
358  
359 2 ▶ if ( !defined( $opts{options}{kill} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $opts{"options"}{"kill"}) 1 1 2
360 1 $self->{options}{kill} = 0;
361 }
362  
363 2 ▶ if ( defined( $opts{options}{rule} ) && ref( $opts{options}{rule} ) ne '' ) {
Branch: 4 branches
BranchTrueFalseTotal
if (defined $opts{'options'}{'rule'} and ref $opts{'options'}{'rule'} ne '') { } 0 2 2
elsif (defined $opts{'options'}{'rule'} and not $opts{'options'}{'rule'} =~ /^[0-9]+$/) { } 0 2 2
elsif (defined $opts{'options'}{'rule'} and $opts{'options'}{'rule'} < 1) { } 0 2 2
elsif (not defined $opts{'options'}{'rule'}) { } 0 2 2
Condition33%1/3
defined $opts{"options"}{"rule"} and ref $opts{"options"}{"rule"} ne ""
!ll&&!rl&&r
020
MC/DC0%0/2
defined $opts{"options"}{"rule"} and ref $opts{"options"}{"rule"} ne ""
defined $opts{"options"}{"rule"} ref $opts{"options"}{"rule"} ne ""
Truth table33%1/3
A and B
A= defined $opts{"options"}{"rule"}B= ref $opts{"options"}{"rule"} ne ""
ABresult
0X0
100
111
364 0 $self->{perror} = 1;
365 0 $self->{error} = 19;
366 0 $self->{errorString} = 'ref for $opts{options}{rule} is "' . ref( $opts{options}{rule} ) . '" and not ""';
367 0 $self->warn;
368 ▶ } elsif ( defined( $opts{options}{rule} ) && $opts{options}{rule} !~ /^[0-9]+$/ ) {
Condition33%1/3
defined $opts{"options"}{"rule"} and not $opts{"options"}{"rule"} =~ /^[0-9]+$/
!ll&&!rl&&r
020
MC/DC0%0/2
defined $opts{"options"}{"rule"} and not $opts{"options"}{"rule"} =~ /^[0-9]+$/
defined $opts{"options"}{"rule"} not $opts{"options"}{"rule"} =~ /^[0-9]+$/
Truth table33%1/3
A and not(B)
A= defined $opts{"options"}{"rule"}B= not $opts{"options"}{"rule"} =~ /^[0-9]+$/
ABresult
0X0
100
111
369 0 $self->{perror} = 1;
370 0 $self->{error} = 19;
371 0 $self->{errorString} = '$opts{options}{rule} is "' . $opts{options}{rule} . '" and not "150"';
372 0 $self->warn;
373 ▶ } elsif ( defined( $opts{options}{rule} ) && $opts{options}{rule} < 1 ) {
Condition33%1/3
defined $opts{"options"}{"rule"} and $opts{"options"}{"rule"} < 1
!ll&&!rl&&r
020
MC/DC0%0/2
defined $opts{"options"}{"rule"} and $opts{"options"}{"rule"} < 1
defined $opts{"options"}{"rule"} $opts{"options"}{"rule"} < 1
Truth table33%1/3
A and B
A= defined $opts{"options"}{"rule"}B= $opts{"options"}{"rule"} < 1
ABresult
0X0
100
111
374 0 $self->{perror} = 1;
375 0 $self->{error} = 19;
376 0 $self->{errorString} = '$opts{options}{rule} is "' . $opts{options}{rule} . '" is less than 1';
377 0 $self->warn;
378 } elsif ( !defined( $opts{options}{rule} ) ) {
379 0 $self->{options}{rule} = 150;
380 }
381  
382 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 $opts{'options'}{'type'} ne 'unreach' and $opts{'options'}{'type'} ne 'deny' and $opts{'options'}{'type'} ne 'unreach6') { } 0 2 2
elsif (not defined $opts{'options'}{'type'}) { } 2 0 2
Condition33%1/3
defined $opts{"options"}{"type"} and ref $opts{"options"}{"type"} ne ""
!ll&&!rl&&r
200
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
383 0 $self->{perror} = 1;
384 0 $self->{error} = 20;
385 0 $self->{errorString} = 'ref for $opts{options}{type} is "' . ref( $opts{options}{type} ) . '" and not ""';
386 0 $self->warn;
387 ▶ } elsif ( defined( $opts{options}{type} )
Condition33%3/9
defined $opts{"options"}{"type"} and $opts{"options"}{"type"} ne "unreach" and $opts{"options"}{"type"} ne "deny" and $opts{"options"}{"type"} ne "unreach6"
!ll&&!rl&&r
200
defined $opts{"options"}{"type"} and $opts{"options"}{"type"} ne "unreach" and $opts{"options"}{"type"} ne "deny"
!ll&&!rl&&r
200
defined $opts{"options"}{"type"} and $opts{"options"}{"type"} ne "unreach"
!ll&&!rl&&r
200
MC/DC0%0/4
defined $opts{"options"}{"type"} and $opts{"options"}{"type"} ne "unreach" and $opts{"options"}{"type"} ne "deny" and $opts{"options"}{"type"} ne "unreach6"
defined $opts{"options"}{"type"} $opts{"options"}{"type"} ne "unreach" $opts{"options"}{"type"} ne "deny" $opts{"options"}{"type"} ne "unreach6"
Truth table20%1/5
A and B and C and D
A= defined $opts{"options"}{"type"}B= $opts{"options"}{"type"} ne "unreach"C= $opts{"options"}{"type"} ne "deny"D= $opts{"options"}{"type"} ne "unreach6"
ABCDresult
0XXX0
10XX0
110X0
11100
11111
388 && $opts{options}{type} ne 'unreach'
389 && $opts{options}{type} ne 'deny'
390 && $opts{options}{type} ne 'unreach6' )
391 {
392 0 $self->{perror} = 1;
393 0 $self->{error} = 20;
394 $self->{errorString}
395 0 = '$opts{options}{type} is "' . $opts{options}{type} . '" and not "deny", "unreach", or "unreach6"';
396 0 $self->warn;
397 } elsif ( !defined( $opts{options}{type} ) ) {
398 2 $self->{options}{type} = 'deny';
399 }
400  
401 2 ▶ if ( defined( $opts{options}{unreach} ) && ref( $opts{options}{unreach} ) ne '' ) {
Branch: 3 branches
BranchTrueFalseTotal
if (defined $opts{'options'}{'unreach'} and ref $opts{'options'}{'unreach'} ne '') { } 0 2 2
elsif (defined $opts{'options'}{'unreach'} and $opts{'options'}{'unreach'} ne 'net' and $opts{'options'}{'unreach'} ne 'host' and $opts{'options'}{'unreach'} ne 'protocol' and $opts{'options'}{'unreach'} ne 'port' and $opts{'options'}{'unreach'} ne 'needfrag' and $opts{'options'}{'unreach'} ne 'srcfail' and $opts{'options'}{'unreach'} ne 'net-unknown' and $opts{'options'}{'unreach'} ne 'host-unknown' and $opts{'options'}{'unreach'} ne 'isolated' and $opts{'options'}{'unreach'} ne 'net-prohib' and $opts{'options'}{'unreach'} ne 'host-prohib' and $opts{'options'}{'unreach'} ne 'tosnet' and $opts{'options'}{'unreach'} ne 'toshost' and $opts{'options'}{'unreach'} ne 'filter-prohib' and $opts{'options'}{'unreach'} ne 'host-precedence' and $opts{'options'}{'unreach'} ne 'precedence-cutoff') { } 0 2 2
elsif (not defined $opts{'options'}{'unreach'}) { } 2 0 2
Condition33%1/3
defined $opts{"options"}{"unreach"} and ref $opts{"options"}{"unreach"} ne ""
!ll&&!rl&&r
200
MC/DC0%0/2
defined $opts{"options"}{"unreach"} and ref $opts{"options"}{"unreach"} ne ""
defined $opts{"options"}{"unreach"} ref $opts{"options"}{"unreach"} ne ""
Truth table33%1/3
A and B
A= defined $opts{"options"}{"unreach"}B= ref $opts{"options"}{"unreach"} ne ""
ABresult
0X0
100
111
402 0 $self->{perror} = 1;
403 0 $self->{error} = 21;
404 $self->{errorString}
405 0 = 'ref for $opts{options}{unreach} is "' . ref( $opts{options}{unreach} ) . '" and not ""';
406 0 $self->warn;
407 ▶ } elsif ( defined( $opts{options}{unreach} )
Condition33%16/48
defined $opts{"options"}{"unreach"} and $opts{"options"}{"unreach"} ne "net" and $opts{"options"}{"unreach"} ne "host" and $opts{"options"}{"unreach"} ne "protocol" and $opts{"options"}{"unreach"} ne "port" and $opts{"options"}{"unreach"} ne "needfrag" and $opts{"options"}{"unreach"} ne "srcfail" and $opts{"options"}{"unreach"} ne "net-unknown" and $opts{"options"}{"unreach"} ne "host-unknown" and $opts{"options"}{"unreach"} ne "isolated" and $opts{"options"}{"unreach"} ne "net-prohib" and $opts{"options"}{"unreach"} ne "host-prohib" and $opts{"options"}{"unreach"} ne "tosnet" and $opts{"options"}{"unreach"} ne "toshost" and $opts{"options"}{"unreach"} ne "filter-prohib" and $opts{"options"}{"unreach"} ne "host-precedence" and $opts{"options"}{"unreach"} ne "precedence-cutoff"
!ll&&!rl&&r
200
defined $opts{"options"}{"unreach"} and $opts{"options"}{"unreach"} ne "net" and $opts{"options"}{"unreach"} ne "host" and $opts{"options"}{"unreach"} ne "protocol" and $opts{"options"}{"unreach"} ne "port" and $opts{"options"}{"unreach"} ne "needfrag" and $opts{"options"}{"unreach"} ne "srcfail" and $opts{"options"}{"unreach"} ne "net-unknown" and $opts{"options"}{"unreach"} ne "host-unknown" and $opts{"options"}{"unreach"} ne "isolated" and $opts{"options"}{"unreach"} ne "net-prohib" and $opts{"options"}{"unreach"} ne "host-prohib" and $opts{"options"}{"unreach"} ne "tosnet" and $opts{"options"}{"unreach"} ne "toshost" and $opts{"options"}{"unreach"} ne "filter-prohib" and $opts{"options"}{"unreach"} ne "host-precedence"
!ll&&!rl&&r
200
defined $opts{"options"}{"unreach"} and $opts{"options"}{"unreach"} ne "net" and $opts{"options"}{"unreach"} ne "host" and $opts{"options"}{"unreach"} ne "protocol" and $opts{"options"}{"unreach"} ne "port" and $opts{"options"}{"unreach"} ne "needfrag" and $opts{"options"}{"unreach"} ne "srcfail" and $opts{"options"}{"unreach"} ne "net-unknown" and $opts{"options"}{"unreach"} ne "host-unknown" and $opts{"options"}{"unreach"} ne "isolated" and $opts{"options"}{"unreach"} ne "net-prohib" and $opts{"options"}{"unreach"} ne "host-prohib" and $opts{"options"}{"unreach"} ne "tosnet" and $opts{"options"}{"unreach"} ne "toshost" and $opts{"options"}{"unreach"} ne "filter-prohib"
!ll&&!rl&&r
200
defined $opts{"options"}{"unreach"} and $opts{"options"}{"unreach"} ne "net" and $opts{"options"}{"unreach"} ne "host" and $opts{"options"}{"unreach"} ne "protocol" and $opts{"options"}{"unreach"} ne "port" and $opts{"options"}{"unreach"} ne "needfrag" and $opts{"options"}{"unreach"} ne "srcfail" and $opts{"options"}{"unreach"} ne "net-unknown" and $opts{"options"}{"unreach"} ne "host-unknown" and $opts{"options"}{"unreach"} ne "isolated" and $opts{"options"}{"unreach"} ne "net-prohib" and $opts{"options"}{"unreach"} ne "host-prohib" and $opts{"options"}{"unreach"} ne "tosnet" and $opts{"options"}{"unreach"} ne "toshost"
!ll&&!rl&&r
200
defined $opts{"options"}{"unreach"} and $opts{"options"}{"unreach"} ne "net" and $opts{"options"}{"unreach"} ne "host" and $opts{"options"}{"unreach"} ne "protocol" and $opts{"options"}{"unreach"} ne "port" and $opts{"options"}{"unreach"} ne "needfrag" and $opts{"options"}{"unreach"} ne "srcfail" and $opts{"options"}{"unreach"} ne "net-unknown" and $opts{"options"}{"unreach"} ne "host-unknown" and $opts{"options"}{"unreach"} ne "isolated" and $opts{"options"}{"unreach"} ne "net-prohib" and $opts{"options"}{"unreach"} ne "host-prohib" and $opts{"options"}{"unreach"} ne "tosnet"
!ll&&!rl&&r
200
defined $opts{"options"}{"unreach"} and $opts{"options"}{"unreach"} ne "net" and $opts{"options"}{"unreach"} ne "host" and $opts{"options"}{"unreach"} ne "protocol" and $opts{"options"}{"unreach"} ne "port" and $opts{"options"}{"unreach"} ne "needfrag" and $opts{"options"}{"unreach"} ne "srcfail" and $opts{"options"}{"unreach"} ne "net-unknown" and $opts{"options"}{"unreach"} ne "host-unknown" and $opts{"options"}{"unreach"} ne "isolated" and $opts{"options"}{"unreach"} ne "net-prohib" and $opts{"options"}{"unreach"} ne "host-prohib"
!ll&&!rl&&r
200
defined $opts{"options"}{"unreach"} and $opts{"options"}{"unreach"} ne "net" and $opts{"options"}{"unreach"} ne "host" and $opts{"options"}{"unreach"} ne "protocol" and $opts{"options"}{"unreach"} ne "port" and $opts{"options"}{"unreach"} ne "needfrag" and $opts{"options"}{"unreach"} ne "srcfail" and $opts{"options"}{"unreach"} ne "net-unknown" and $opts{"options"}{"unreach"} ne "host-unknown" and $opts{"options"}{"unreach"} ne "isolated" and $opts{"options"}{"unreach"} ne "net-prohib"
!ll&&!rl&&r
200
defined $opts{"options"}{"unreach"} and $opts{"options"}{"unreach"} ne "net" and $opts{"options"}{"unreach"} ne "host" and $opts{"options"}{"unreach"} ne "protocol" and $opts{"options"}{"unreach"} ne "port" and $opts{"options"}{"unreach"} ne "needfrag" and $opts{"options"}{"unreach"} ne "srcfail" and $opts{"options"}{"unreach"} ne "net-unknown" and $opts{"options"}{"unreach"} ne "host-unknown" and $opts{"options"}{"unreach"} ne "isolated"
!ll&&!rl&&r
200
defined $opts{"options"}{"unreach"} and $opts{"options"}{"unreach"} ne "net" and $opts{"options"}{"unreach"} ne "host" and $opts{"options"}{"unreach"} ne "protocol" and $opts{"options"}{"unreach"} ne "port" and $opts{"options"}{"unreach"} ne "needfrag" and $opts{"options"}{"unreach"} ne "srcfail" and $opts{"options"}{"unreach"} ne "net-unknown" and $opts{"options"}{"unreach"} ne "host-unknown"
!ll&&!rl&&r
200
defined $opts{"options"}{"unreach"} and $opts{"options"}{"unreach"} ne "net" and $opts{"options"}{"unreach"} ne "host" and $opts{"options"}{"unreach"} ne "protocol" and $opts{"options"}{"unreach"} ne "port" and $opts{"options"}{"unreach"} ne "needfrag" and $opts{"options"}{"unreach"} ne "srcfail" and $opts{"options"}{"unreach"} ne "net-unknown"
!ll&&!rl&&r
200
defined $opts{"options"}{"unreach"} and $opts{"options"}{"unreach"} ne "net" and $opts{"options"}{"unreach"} ne "host" and $opts{"options"}{"unreach"} ne "protocol" and $opts{"options"}{"unreach"} ne "port" and $opts{"options"}{"unreach"} ne "needfrag" and $opts{"options"}{"unreach"} ne "srcfail"
!ll&&!rl&&r
200
defined $opts{"options"}{"unreach"} and $opts{"options"}{"unreach"} ne "net" and $opts{"options"}{"unreach"} ne "host" and $opts{"options"}{"unreach"} ne "protocol" and $opts{"options"}{"unreach"} ne "port" and $opts{"options"}{"unreach"} ne "needfrag"
!ll&&!rl&&r
200
defined $opts{"options"}{"unreach"} and $opts{"options"}{"unreach"} ne "net" and $opts{"options"}{"unreach"} ne "host" and $opts{"options"}{"unreach"} ne "protocol" and $opts{"options"}{"unreach"} ne "port"
!ll&&!rl&&r
200
defined $opts{"options"}{"unreach"} and $opts{"options"}{"unreach"} ne "net" and $opts{"options"}{"unreach"} ne "host" and $opts{"options"}{"unreach"} ne "protocol"
!ll&&!rl&&r
200
defined $opts{"options"}{"unreach"} and $opts{"options"}{"unreach"} ne "net" and $opts{"options"}{"unreach"} ne "host"
!ll&&!rl&&r
200
defined $opts{"options"}{"unreach"} and $opts{"options"}{"unreach"} ne "net"
!ll&&!rl&&r
200
MC/DC0%0/17
defined $opts{"options"}{"unreach"} and $opts{"options"}{"unreach"} ne "net" and $opts{"options"}{"unreach"} ne "host" and $opts{"options"}{"unreach"} ne "protocol" and $opts{"options"}{"unreach"} ne "port" and $opts{"options"}{"unreach"} ne "needfrag" and $opts{"options"}{"unreach"} ne "srcfail" and $opts{"options"}{"unreach"} ne "net-unknown" and $opts{"options"}{"unreach"} ne "host-unknown" and $opts{"options"}{"unreach"} ne "isolated" and $opts{"options"}{"unreach"} ne "net-prohib" and $opts{"options"}{"unreach"} ne "host-prohib" and $opts{"options"}{"unreach"} ne "tosnet" and $opts{"options"}{"unreach"} ne "toshost" and $opts{"options"}{"unreach"} ne "filter-prohib" and $opts{"options"}{"unreach"} ne "host-precedence" and $opts{"options"}{"unreach"} ne "precedence-cutoff"
too many conditions
408 && $opts{options}{unreach} ne 'net'
409 && $opts{options}{unreach} ne 'host'
410 && $opts{options}{unreach} ne 'protocol'
411 && $opts{options}{unreach} ne 'port'
412 && $opts{options}{unreach} ne 'needfrag'
413 && $opts{options}{unreach} ne 'srcfail'
414 && $opts{options}{unreach} ne 'net-unknown'
415 && $opts{options}{unreach} ne 'host-unknown'
416 && $opts{options}{unreach} ne 'isolated'
417 && $opts{options}{unreach} ne 'net-prohib'
418 && $opts{options}{unreach} ne 'host-prohib'
419 && $opts{options}{unreach} ne 'tosnet'
420 && $opts{options}{unreach} ne 'toshost'
421 && $opts{options}{unreach} ne 'filter-prohib'
422 && $opts{options}{unreach} ne 'host-precedence'
423 && $opts{options}{unreach} ne 'precedence-cutoff' )
424 {
425 0 $self->{perror} = 1;
426 0 $self->{error} = 21;
427 $self->{errorString}
428 = '$opts{options}{unreach} is "'
429 . $opts{options}{unreach}
430 0 . '" and a value understood by ipfw(8) for unreach';
431 0 $self->warn;
432 } elsif ( !defined( $opts{options}{unreach} ) ) {
433 2 $self->{options}{unreach} = 'port';
434 }
435  
436 2 ▶ if ( defined( $opts{options}{unreach6} ) && ref( $opts{options}{unreach6} ) ne '' ) {
Branch: 3 branches
BranchTrueFalseTotal
if (defined $opts{'options'}{'unreach6'} and ref $opts{'options'}{'unreach6'} ne '') { } 0 2 2
elsif (defined $opts{'options'}{'unreach6'} and $opts{'options'}{'unreach6'} ne 'no-route' and $opts{'options'}{'unreach6'} ne 'admin-prohib' and $opts{'options'}{'unreach6'} ne 'address' and $opts{'options'}{'unreach6'} ne 'port') { } 0 2 2
elsif (not defined $opts{'options'}{'unreach6'}) { } 2 0 2
Condition33%1/3
defined $opts{"options"}{"unreach6"} and ref $opts{"options"}{"unreach6"} ne ""
!ll&&!rl&&r
200
MC/DC0%0/2
defined $opts{"options"}{"unreach6"} and ref $opts{"options"}{"unreach6"} ne ""
defined $opts{"options"}{"unreach6"} ref $opts{"options"}{"unreach6"} ne ""
Truth table33%1/3
A and B
A= defined $opts{"options"}{"unreach6"}B= ref $opts{"options"}{"unreach6"} ne ""
ABresult
0X0
100
111
437 0 $self->{perror} = 1;
438 0 $self->{error} = 22;
439 $self->{errorString}
440 0 = 'ref for $opts{options}{unreach6} is "' . ref( $opts{options}{unreach6} ) . '" and not ""';
441 0 $self->warn;
442 ▶ } elsif ( defined( $opts{options}{unreach6} )
Condition33%4/12
defined $opts{"options"}{"unreach6"} and $opts{"options"}{"unreach6"} ne "no-route" and $opts{"options"}{"unreach6"} ne "admin-prohib" and $opts{"options"}{"unreach6"} ne "address" and $opts{"options"}{"unreach6"} ne "port"
!ll&&!rl&&r
200
defined $opts{"options"}{"unreach6"} and $opts{"options"}{"unreach6"} ne "no-route" and $opts{"options"}{"unreach6"} ne "admin-prohib" and $opts{"options"}{"unreach6"} ne "address"
!ll&&!rl&&r
200
defined $opts{"options"}{"unreach6"} and $opts{"options"}{"unreach6"} ne "no-route" and $opts{"options"}{"unreach6"} ne "admin-prohib"
!ll&&!rl&&r
200
defined $opts{"options"}{"unreach6"} and $opts{"options"}{"unreach6"} ne "no-route"
!ll&&!rl&&r
200
MC/DC0%0/5
defined $opts{"options"}{"unreach6"} and $opts{"options"}{"unreach6"} ne "no-route" and $opts{"options"}{"unreach6"} ne "admin-prohib" and $opts{"options"}{"unreach6"} ne "address" and $opts{"options"}{"unreach6"} ne "port"
defined $opts{"options"}{"unreach6"} $opts{"options"}{"unreach6"} ne "no-route" $opts{"options"}{"unreach6"} ne "admin-prohib" $opts{"options"}{"unreach6"} ne "address" $opts{"options"}{"unreach6"} ne "port"
Truth table16%1/6
A and B and C and D and E
A= defined $opts{"options"}{"unreach6"}B= $opts{"options"}{"unreach6"} ne "no-route"C= $opts{"options"}{"unreach6"} ne "admin-prohib"D= $opts{"options"}{"unreach6"} ne "address"E= $opts{"options"}{"unreach6"} ne "port"
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
443 && $opts{options}{unreach6} ne 'no-route'
444 && $opts{options}{unreach6} ne 'admin-prohib'
445 && $opts{options}{unreach6} ne 'address'
446 && $opts{options}{unreach6} ne 'port' )
447 {
448 0 $self->{perror} = 1;
449 0 $self->{error} = 22;
450 $self->{errorString}
451 = '$opts{options}{unreach6} is "'
452 . $opts{options}{unreach6}
453 0 . '" and a value understood by ipfw(8) for unreach6';
454 0 $self->warn;
455 } elsif ( !defined( $opts{options}{unreach6} ) ) {
456 2 $self->{options}{unreach6} = 'port';
457 }
458  
459 } ## end if ( defined( $opts{options} ) )
460  
461 3 return $self;
462 } ## end sub new
463  
464 =head2 init
465  
466 Initiates the backend. First any old remnants are removed on a best effort
467 basis, destroying the table C<E<lt>prefixE<gt>_E<lt>nameE<gt>> and deleting
468 the configured rule number, with failures ignored. The table is then created
469 via C<ipfw table ... create> and a block rule is added at the configured rule
470 number for each configured protocol, matching from C<table(...)> to C<me> or
471 C<me6> depending on the family, with the configured ports appended for
472 port-capable protocols (tcp/udp/sctp). The action is C<deny> or, for reject
473 types, C<unreach>/C<unreach6> per the family of the rule.
474  
475 Note that as ipfw is first match wins, the configured rule number needs to
476 come before any rules that would pass the traffic. See L</SYNOPSIS>.
477  
478 No arguments are taken.
479  
480 If called a second time, it will error.
481  
482 $backend->init;
483  
484 =cut
485  
486 sub init {
487 2 ▶ my ( $self, %opts ) = @_;
Subroutine init: called CC=20 SCAR=34.4
Pod documented
488  
489 2 $self->errorblank;
490  
491 2 ▶ if ( $self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"inited"}) 0 2 2
492 0 $self->{error} = 18;
493 0 $self->{errorString} = 'backend has already been inited';
494 0 $self->warn;
495 }
496  
497 2 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 2 0 2
498 2 $self->{frontend_obj}->{test_data} = {};
499 }
500  
501 2 my $ports;
502 2 ▶ if ( defined( $self->{ports}[0] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"ports"}[0]) 1 1 2
503 1 $ports = join( ',', @{ $self->{ports} } );
504 }
505  
506 2 my @protocols;
507 2 ▶ if ( defined( $self->{protocols}[0] ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $self->{'protocols'}[0]) { } 0 2 2
elsif (defined $ports) { } 1 1 2
508 0 push( @protocols, @{ $self->{protocols} } );
509 } elsif ( defined($ports) ) {
510 # ports need a port-capable protocol, default to tcp and udp
511 1 push( @protocols, 'tcp', 'udp' );
512 } else {
513 # block all, both IPv4 and IPv6; ip4/ip6 are used rather than ip as
514 # in ipfw 'ip' means any packet, not IPv4, per ipfw(8)
515 1 push( @protocols, 'ip4', 'ip6' );
516 }
517  
518 # protocols that accept a port specification
519 2 my %port_ok = ( tcp => 1, udp => 1, sctp => 1 );
520  
521 # in ipfw 'me' matches only IPv4 addresses on the system and 'me6' only
522 # IPv6 ones, so each rule must be emitted for the right family; family
523 # neutral protocols such as tcp/udp get one rule per family
524 2 my %is_v6 = ( ip6 => 1, ipv6 => 1, icmp6 => 1, 'ipv6-icmp' => 1, icmpv6 => 1 );
525 2 my %is_v4 = ( ip4 => 1, ipv4 => 1, icmp => 1, igmp => 1 );
526  
527 2 my @fail_okay_commands;
528 2 push( @fail_okay_commands, 'ipfw table ' . $self->{prefix} . '_' . $self->{name} . ' destroy' );
529 2 push( @fail_okay_commands, 'ipfw delete ' . $self->{options}{rule} );
530  
531 2 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 2 0 2
532 2 $self->{frontend_obj}->{test_data}{fail_okay_commands} = \@fail_okay_commands;
533 } else {
534 0 foreach my $item (@fail_okay_commands) {
535 0 my $output = `$item 2>&1`;
536 }
537 }
538  
539 2 my @commands;
540 # add the table to create the command
541 2 push( @commands, 'ipfw table ' . $self->{prefix} . '_' . $self->{name} . ' create' );
542  
543 # generates the block rules
544 2 foreach my $item (@protocols) {
545 # family specific protocols get one rule for their family; family
546 # neutral protocols such as tcp/udp get a rule for each family
547 4 my @families;
548 4 ▶ if ( $is_v6{$item} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($is_v6{$item}) { } 1 3 4
elsif ($is_v4{$item}) { } 1 2 3
549 1 @families = (6);
550 } elsif ( $is_v4{$item} ) {
551 1 @families = (4);
552 } else {
553 2 @families = ( 4, 6 );
554 }
555  
556 4 foreach my $family (@families) {
557 # type is deny vs reject; for a reject the family of the rule
558 # decides between unreach (IPv4) and unreach6 (IPv6)
559 6 my $action;
560 6 ▶ if ( $self->{options}{type} eq 'deny' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'options'}{'type'} eq 'deny') { } 6 0 6
elsif ($family == 6) { } 0 0 0
561 6 $action = 'deny';
562 } elsif ( $family == 6 ) {
563 0 $action = 'unreach6 ' . $self->{options}{unreach6};
564 } else {
565 0 $action = 'unreach ' . $self->{options}{unreach};
566 }
567  
568 my $command
569 = 'ipfw add '
570 . $self->{options}{rule} . ' '
571 . $action . ' '
572 . $item
573 . ' from "table('
574 . $self->{prefix} . '_'
575 . $self->{name}
576 6 ▶ . ')" to '
Branch: 1 branch
BranchTrueFalseTotal
$family == 6 ? : 3 3 6
577 . ( $family == 6 ? 'me6' : 'me' );
578 6 ▶ if ( defined($ports) && $port_ok{$item} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ports and $port_ok{$item}) 4 2 6
Condition66%2/3
defined $ports and $port_ok{$item}
!ll&&!rl&&r
204
MC/DC50%1/2
defined $ports and $port_ok{$item}
defined $ports $port_ok{$item}
Truth table66%2/3
A and B
A= defined $portsB= $port_ok{$item}
ABresult
0X0
100
111
579 4 $command = $command . ' ' . $ports;
580 }
581 6 push( @commands, $command );
582 } ## end foreach my $family (@families)
583 } ## end foreach my $item (@protocols)
584  
585 2 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 2 0 2
586 2 $self->{frontend_obj}->{test_data}{commands} = \@commands;
587 } else {
588 0 foreach my $item (@commands) {
589 0 my $output = `$item 2>&1`;
590 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
591 0 $self->{error} = 23;
592 $self->{errorString}
593 0 = 'init failed. non-zero exit code for the command... "' . $item . '"... output... ' . $output;
594 0 $self->warn;
595 }
596 }
597 } ## end else [ if ( $self->{testing} ) ]
598  
599 2 $self->{inited} = 1;
600 } ## end sub init
601  
602 =head2 ban
603  
604 Bans an IP. The value of ban is validated as being a IPv4 or IPv6 address
605 and lowercased, then added to the IPFW table via
606 C<ipfw table E<lt>prefixE<gt>_E<lt>nameE<gt> add>. If the kill option is set
607 and tcp is among the blocked protocols, established TCP connections from the
608 IP are found via sockstat and killed via tcpdrop. Banning an already banned
609 IP is a noop.
610  
611 $backend->ban(ban => $ip);
612  
613 =cut
614  
615 sub ban {
616 2 ▶ my ( $self, %opts ) = @_;
Subroutine ban: called CC=15 SCAR=40.7
Pod documented
617  
618 2 $self->errorblank;
619  
620 2 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 2 2
621 0 $self->{error} = 1;
622 0 $self->{errorString} = 'backend has not been inited';
623 0 $self->warn;
624 0 return;
625 }
626  
627 2 ▶ if ( !defined( $opts{ban} ) ) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $opts{'ban'}) { } 0 2 2
elsif (ref $opts{'ban'} ne '') { } 0 2 2
elsif (not $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::ipfw::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::ipfw::IPv6_re\z/) { } 0 2 2
628 0 $self->{error} = 9;
629 0 $self->{errorString} = 'Nothing specified for the value ban';
630 0 $self->warn;
631 0 return;
632 } elsif ( ref( $opts{ban} ) ne '' ) {
633 0 $self->{error} = 10;
634 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
635 0 $self->warn;
636 0 return;
637 ▶ } elsif ( $opts{ban} !~ /\A$IPv4_re\z/
Condition66%2/3
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::ipfw::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::ipfw::IPv6_re\z/
l!l&&r!l&&!r
110
MC/DC0%0/2
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::ipfw::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::ipfw::IPv6_re\z/
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::ipfw::IPv4_re\z/ $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::ipfw::IPv6_re\z/
Truth table66%2/3
A || B
A= $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::ipfw::IPv4_re\z/B= $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::ipfw::IPv6_re\z/
ABresult
1X1
011
000
638 && $opts{ban} !~ /\A$IPv6_re\z/ )
639 {
640 0 $self->{error} = 10;
641 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 IP';
642 0 $self->warn;
643 0 return;
644 }
645  
646 # lowercase so the same IPv6 IP in differing cases can't result in duplicate entries
647 2 $opts{ban} = lc( $opts{ban} );
648  
649 2 ▶ if ( $self->{banned}{ $opts{ban} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"banned"}{$opts{"ban"}}) 0 2 2
650 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
651 0 $self->{frontend_obj}->{test_data} = 'already banned';
652 }
653 0 return;
654 }
655  
656 2 my $command = 'ipfw table ' . $self->{prefix} . '_' . $self->{name} . ' add ' . $opts{ban};
657  
658 2 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 2 0 2
659 2 $self->{frontend_obj}->{test_data} = [$command];
660 } else {
661 0 my $output = `$command 2>&1`;
662 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
663 0 $self->{error} = 13;
664 $self->{errorString}
665 0 = 'ban failed. non-zero exit code for the command... "' . $command . '"... output... ' . $output;
666 0 $self->warn;
667 }
668 }
669  
670 # tcpdrop only handles TCP, so only kill when tcp is among the blocked
671 # protocols; no protocols configured means everything is being blocked,
672 # tcp included
673 2 ▶ my $kill_applicable = !defined( $self->{protocols}[0] ) || ( grep { $_ eq 'tcp' } @{ $self->{protocols} } );
Condition33%1/3
!defined($self->{'protocols'}[0]) || grep({$_ eq 'tcp';} @{$$self{'protocols'};})
l!l&&r!l&&!r
200
MC/DC0%0/2
!defined($self->{'protocols'}[0]) || grep({$_ eq 'tcp';} @{$$self{'protocols'};})
!defined($self->{'protocols'}[0]) grep({$_ eq 'tcp';} @{$$self{'protocols'};})
Truth table33%1/3
not(A) || B
A= !defined($self->{'protocols'}[0])B= grep({$_ eq 'tcp';} @{$$self{'protocols'};})
ABresult
1X1
011
000
674  
675 2 ▶ if ( $self->{options}{kill} && $kill_applicable ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"options"}{"kill"} and $kill_applicable) 2 0 2
Condition33%1/3
$self->{"options"}{"kill"} and $kill_applicable
!ll&&!rl&&r
002
MC/DC0%0/2
$self->{"options"}{"kill"} and $kill_applicable
$self->{"options"}{"kill"} $kill_applicable
Truth table33%1/3
A and B
A= $self->{"options"}{"kill"}B= $kill_applicable
ABresult
0X0
100
111
676 2 ▶ if ( $opts{ban} =~ /\A$IPv4_re\z/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::ipfw::IPv4_re\z/) { } 1 1 2
677 # for IPv4 every colon is a address/port separator, so a simple
678 # split works; -wF so the IP is matched as a fixed string and not
679 # as a regexp, preventing 1.2.3.4 from also matching 11.2.3.45
680 $command
681 = 'sockstat -nc4 -P tcp |sed "s/.*tcp[46] *//" | sed "s/:/ /g" | grep -wF '
682 . $opts{ban}
683 1 . ' | xargs -n 4 tcpdrop';
684 } else {
685 # for IPv6 the address itself contains colons, so only the last
686 # colon of each field, the port, is turned into a space, and any
687 # scope ID is stripped as tcpdrop does not take them
688 $command
689 = 'sockstat -nc6 -P tcp |sed "s/.*tcp[46] *//" | grep -E "(^|[[:space:]])'
690 . $opts{ban}
691 1 . ':[0-9]+([[:space:]]|\$)" | sed "s/%[a-zA-Z0-9]*//g" | sed -E "s/:([0-9]+)([[:space:]]|\$)/ \\1\\2/g" | xargs -n 4 tcpdrop';
692 }
693 2 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 2 0 2
694 2 push( @{ $self->{frontend_obj}->{test_data} }, $command );
695 } else {
696 0 my $output = `$command 2>&1`;
697 }
698 } ## end if ( $self->{options}{kill} )
699  
700 2 $self->{banned}{ $opts{ban} } = 1;
701 } ## end sub ban
702  
703 =head2 unban
704  
705 Unbans an IP. The value of ban is validated as being a IPv4 or IPv6 address
706 and lowercased, then removed from the IPFW table via
707 C<ipfw table E<lt>prefixE<gt>_E<lt>nameE<gt> delete>. Unbanning an IP that
708 is not banned is a noop.
709  
710 $backend->unban(ban => $ip);
711  
712 =cut
713  
714 sub unban {
715 0 ▶ my ( $self, %opts ) = @_;
Subroutine unban: not called CC=10 SCAR=47.0
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 $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::ipfw::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::ipfw::IPv6_re\z/) { } 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} = 10;
733 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
734 0 $self->warn;
735 0 return;
736 ▶ } elsif ( $opts{ban} !~ /\A$IPv4_re\z/
Condition0%0/3
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::ipfw::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::ipfw::IPv6_re\z/
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::ipfw::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::ipfw::IPv6_re\z/
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::ipfw::IPv4_re\z/ $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::ipfw::IPv6_re\z/
Truth table0%0/3
A || B
A= $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::ipfw::IPv4_re\z/B= $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::ipfw::IPv6_re\z/
ABresult
1X1
011
000
737 && $opts{ban} !~ /\A$IPv6_re\z/ )
738 {
739 0 $self->{error} = 10;
740 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 IP';
741 0 $self->warn;
742 0 return;
743 }
744  
745 # lowercase so the same IPv6 IP in differing cases can't result in duplicate entries
746 0 $opts{ban} = lc( $opts{ban} );
747  
748 0 ▶ if ( !$self->{banned}{ $opts{ban} } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"banned"}{$opts{"ban"}}) 0 0 0
749 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
750 0 $self->{frontend_obj}->{test_data} = 'not banned';
751 }
752 0 return;
753 }
754  
755 0 my $command = 'ipfw table ' . $self->{prefix} . '_' . $self->{name} . ' delete ' . $opts{ban};
756  
757 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
758 0 $self->{frontend_obj}->{test_data} = $command;
759 } else {
760 0 my $output = `$command 2>&1`;
761 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
762 0 $self->{error} = 14;
763 $self->{errorString}
764 0 = 'unban failed. non-zero exit code for the command... "' . $command . '"... output... ' . $output;
765 0 $self->warn;
766 }
767 }
768  
769 0 delete( $self->{banned}{ $opts{ban} } );
770 } ## end sub unban
771  
772 # Internal helper. Returns a true value if the passed scalar is a valid IPv4 or
773 # IPv6 CIDR range, that is an address followed by "/" and a prefix length that
774 # is within the range valid for its family (0 to 32 for IPv4, 0 to 128 for
775 # IPv6). Returns false otherwise.
776 sub _valid_cidr {
777 0 ▶ my ( $self, $cidr ) = @_;
Subroutine _valid_cidr: not called CC=8 SCAR=42.8
778  
779 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
780  
781 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
782 0 my ( $addr, $prefix ) = ( $1, $2 );
783 0 ▶ return 1 if ( $addr =~ /\A$IPv4_re\z/ && $prefix <= 32 );
Branch: 1 branch
BranchTrueFalseTotal
if $addr =~ /\A$Net::Firewall::BlockerHelper::backends::ipfw::IPv4_re\z/ and $prefix <= 32 0 0 0
Condition0%0/3
$addr =~ /\A$Net::Firewall::BlockerHelper::backends::ipfw::IPv4_re\z/ and $prefix <= 32
!ll&&!rl&&r
000
MC/DC0%0/2
$addr =~ /\A$Net::Firewall::BlockerHelper::backends::ipfw::IPv4_re\z/ and $prefix <= 32
$addr =~ /\A$Net::Firewall::BlockerHelper::backends::ipfw::IPv4_re\z/ $prefix <= 32
Truth table0%0/3
A and B
A= $addr =~ /\A$Net::Firewall::BlockerHelper::backends::ipfw::IPv4_re\z/B= $prefix <= 32
ABresult
0X0
100
111
784 0 ▶ return 1 if ( $addr =~ /\A$IPv6_re\z/ && $prefix <= 128 );
Branch: 1 branch
BranchTrueFalseTotal
if $addr =~ /\A$Net::Firewall::BlockerHelper::backends::ipfw::IPv6_re\z/ and $prefix <= 128 0 0 0
Condition0%0/3
$addr =~ /\A$Net::Firewall::BlockerHelper::backends::ipfw::IPv6_re\z/ and $prefix <= 128
!ll&&!rl&&r
000
MC/DC0%0/2
$addr =~ /\A$Net::Firewall::BlockerHelper::backends::ipfw::IPv6_re\z/ and $prefix <= 128
$addr =~ /\A$Net::Firewall::BlockerHelper::backends::ipfw::IPv6_re\z/ $prefix <= 128
Truth table0%0/3
A and B
A= $addr =~ /\A$Net::Firewall::BlockerHelper::backends::ipfw::IPv6_re\z/B= $prefix <= 128
ABresult
0X0
100
111
785 }
786  
787 0 return 0;
788 } ## end sub _valid_cidr
789  
790 =head2 ban_cidr
791  
792 Bans a CIDR range by adding it to the IPFW table. IPFW tables accept a
793 network prefix in the same manner as a single address.
794  
795 $backend->ban_cidr(ban => '1.2.3.0/24');
796  
797 =cut
798  
799 sub ban_cidr {
800 0 ▶ my ( $self, %opts ) = @_;
Subroutine ban_cidr: not called CC=9 SCAR=45.0
Pod documented
801  
802 0 $self->errorblank;
803  
804 0 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 0 0
805 0 $self->{error} = 1;
806 0 $self->{errorString} = 'backend has not been inited';
807 0 $self->warn;
808 0 return;
809 }
810  
811 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
812 0 $self->{error} = 9;
813 0 $self->{errorString} = 'Nothing specified for the value ban';
814 0 $self->warn;
815 0 return;
816 } elsif ( ref( $opts{ban} ) ne '' ) {
817 0 $self->{error} = 29;
818 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
819 0 $self->warn;
820 0 return;
821 } elsif ( !$self->_valid_cidr( $opts{ban} ) ) {
822 0 $self->{error} = 29;
823 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 CIDR';
824 0 $self->warn;
825 0 return;
826 }
827  
828 # lowercase so the same IPv6 CIDR in differing cases can't result in duplicate entries
829 0 $opts{ban} = lc( $opts{ban} );
830  
831 0 ▶ if ( $self->{banned_cidr}{ $opts{ban} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"banned_cidr"}{$opts{"ban"}}) 0 0 0
832 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
833 0 $self->{frontend_obj}->{test_data} = 'already banned';
834 }
835 0 return;
836 }
837  
838 0 my $command = 'ipfw table ' . $self->{prefix} . '_' . $self->{name} . ' add ' . $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} = 27;
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 $self->{banned_cidr}{ $opts{ban} } = 1;
853 } ## end sub ban_cidr
854  
855 =head2 unban_cidr
856  
857 Unbans a CIDR range by deleting it from the IPFW table.
858  
859 $backend->unban_cidr(ban => '1.2.3.0/24');
860  
861 =cut
862  
863 sub unban_cidr {
864 0 ▶ my ( $self, %opts ) = @_;
Subroutine unban_cidr: not called CC=9 SCAR=45.0
Pod documented
865  
866 0 $self->errorblank;
867  
868 0 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 0 0
869 0 $self->{error} = 1;
870 0 $self->{errorString} = 'backend has not been inited';
871 0 $self->warn;
872 0 return;
873 }
874  
875 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
876 0 $self->{error} = 9;
877 0 $self->{errorString} = 'Nothing specified for the value ban';
878 0 $self->warn;
879 0 return;
880 } elsif ( ref( $opts{ban} ) ne '' ) {
881 0 $self->{error} = 29;
882 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
883 0 $self->warn;
884 0 return;
885 } elsif ( !$self->_valid_cidr( $opts{ban} ) ) {
886 0 $self->{error} = 29;
887 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 CIDR';
888 0 $self->warn;
889 0 return;
890 }
891  
892 # lowercase so the same IPv6 CIDR in differing cases can't result in duplicate entries
893 0 $opts{ban} = lc( $opts{ban} );
894  
895 0 ▶ if ( !$self->{banned_cidr}{ $opts{ban} } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"banned_cidr"}{$opts{"ban"}}) 0 0 0
896 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
897 0 $self->{frontend_obj}->{test_data} = 'not banned';
898 }
899 0 return;
900 }
901  
902 0 my $command = 'ipfw table ' . $self->{prefix} . '_' . $self->{name} . ' delete ' . $opts{ban};
903  
904 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
905 0 $self->{frontend_obj}->{test_data} = $command;
906 } else {
907 0 my $output = `$command 2>&1`;
908 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
909 0 $self->{error} = 28;
910 $self->{errorString}
911 0 = 'unban failed. non-zero exit code for the command... "' . $command . '"... output... ' . $output;
912 0 $self->warn;
913 }
914 }
915  
916 0 delete( $self->{banned_cidr}{ $opts{ban} } );
917 } ## end sub unban_cidr
918  
919 =head2 list_cidr
920  
921 List banned CIDR ranges.
922  
923 my @banned_cidrs = $backend->list_cidr;
924  
925 =cut
926  
927 sub list_cidr {
928 0 ▶ my ( $self, %opts ) = @_;
Subroutine list_cidr: not called CC=2 SCAR=17.9
Pod documented
929  
930 0 $self->errorblank;
931  
932 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
933 0 $self->{frontend_obj}->{test_data} = 'list_cidr';
934 }
935  
936 0 return keys( %{ $self->{banned_cidr} } );
937 }
938  
939 =head2 list
940  
941 List banned IPs. Returns an array of the currently banned single IPs. CIDR
942 ranges are not included; for those see L</list_cidr>.
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 itself.
963  
964 Calls teardown, with errors ignored as a partially wiped setup is what
965 re_init recovers from, then init to recreate the table and rules. All
966 previously banned IPs and CIDR ranges are then re-added to the table via
967 C<ipfw table ... 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=6 SCAR=37.4
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 @to_ban = ( keys( %{ $self->{banned} } ), keys( %{ $self->{banned_cidr} } ) );
994  
995 0 my @re_init_test_data;
996 0 foreach my $item (@to_ban) {
997 0 my $command = 'ipfw table ' . $self->{prefix} . '_' . $self->{name} . ' add ' . $item;
998  
999 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
1000 0 push( @re_init_test_data, $command );
1001 } else {
1002 0 my $output = `$command 2>&1`;
1003 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
1004 0 $self->{error} = 13;
1005 $self->{errorString}
1006 0 = 'ban failed. non-zero exit code for the command... "' . $command . '"... output... ' . $output;
1007 0 $self->warn;
1008 }
1009 }
1010 } ## end foreach my $item (@to_ban)
1011  
1012 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
1013 0 $self->{frontend_obj}->{test_data} = \@re_init_test_data;
1014 }
1015  
1016 0 $self->{inited} = 1;
1017 } ## end sub re_init
1018  
1019 =head2 teardown
1020  
1021 Tears down the setup for the backend, destroying the table
1022 C<E<lt>prefixE<gt>_E<lt>nameE<gt>> and deleting the configured rule number.
1023  
1024 It does not check if it has been inited or not, so if called prior to
1025 calling init, it will error as the commands will fail.
1026  
1027 $backend->teardown;
1028  
1029 =cut
1030  
1031 sub teardown {
1032 0 ▶ my ( $self, %opts ) = @_;
Subroutine teardown: not called CC=4 SCAR=30.0
Pod documented
1033  
1034 0 $self->errorblank;
1035  
1036 0 $self->{inited} = 0;
1037  
1038 0 my @commands;
1039 0 push( @commands, 'ipfw table ' . $self->{prefix} . '_' . $self->{name} . ' destroy' );
1040 0 push( @commands, 'ipfw delete ' . $self->{options}{rule} );
1041  
1042 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
1043 0 $self->{frontend_obj}->{test_data} = \@commands;
1044 } else {
1045 0 foreach my $item (@commands) {
1046 0 my $output = `$item 2>&1`;
1047 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
1048 0 $self->{error} = 17;
1049 $self->{errorString}
1050 0 = 'teardown failed. non-zero exit code for the command... "' . $item . '"... output... ' . $output;
1051 0 $self->warn;
1052 }
1053 }
1054 } ## end else [ if ( $self->{testing} ) ]
1055 } ## end sub teardown
1056  
1057 =head2 stop
1058  
1059 Alias for L</teardown>, provided for parity with the fail2ban C<actionstop>
1060 concept.
1061  
1062 $backend->stop;
1063  
1064 =cut
1065  
1066 sub stop {
1067 0 ▶ my ( $self, %opts ) = @_;
Subroutine stop: not called CC=1 SCAR=6.9
Pod documented
1068  
1069 0 return $self->teardown(%opts);
1070 }
1071  
1072 =head2 check
1073  
1074 Verifies that the IPFW table is still present and that the rule number still
1075 exists. Returns a true value if the setup is intact and a false value if any
1076 part of it appears to have been removed. This is the equivalent of fail2ban's
1077 C<actioncheck>.
1078  
1079 if ( !$backend->check ) {
1080 $backend->re_init;
1081 }
1082  
1083 =cut
1084  
1085 sub check {
1086 2 ▶ my ( $self, %opts ) = @_;
Subroutine check: called CC=4 SCAR=18.6
Pod documented
1087  
1088 2 $self->errorblank;
1089  
1090 my @commands = (
1091 'ipfw table ' . $self->{prefix} . '_' . $self->{name} . ' info',
1092 'ipfw list ' . $self->{options}{rule},
1093 2 );
1094  
1095 2 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 2 0 2
1096 2 $self->{frontend_obj}->{test_data} = \@commands;
1097 2 return 1;
1098 }
1099  
1100 0 foreach my $item (@commands) {
1101 0 my $output = `$item 2>&1`;
1102 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
1103 0 return 0;
1104 }
1105 }
1106  
1107 0 return 1;
1108 } ## end sub check
1109  
1110 =head2 flush
1111  
1112 Removes all currently banned IPs and CIDR ranges at once by flushing the
1113 IPFW table, leaving the table and rule in place. This is the equivalent of
1114 fail2ban's C<actionflush>.
1115  
1116 $backend->flush;
1117  
1118 =cut
1119  
1120 sub flush {
1121 0 ▶ my ( $self, %opts ) = @_;
Subroutine flush: not called CC=5 SCAR=34.0
Pod documented
1122  
1123 0 $self->errorblank;
1124  
1125 0 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 0 0
1126 0 $self->{error} = 1;
1127 0 $self->{errorString} = 'backend has not been inited';
1128 0 $self->warn;
1129 0 return;
1130 }
1131  
1132 0 my @commands = ( 'ipfw table ' . $self->{prefix} . '_' . $self->{name} . ' flush' );
1133  
1134 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
1135 0 $self->{frontend_obj}->{test_data} = \@commands;
1136 } else {
1137 0 foreach my $item (@commands) {
1138 0 my $output = `$item 2>&1`;
1139 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
1140 0 $self->{error} = 25;
1141 $self->{errorString}
1142 0 = 'flush failed. non-zero exit code for the command... "' . $item . '"... output... ' . $output;
1143 0 $self->warn;
1144 }
1145 }
1146 } ## end else [ if ( $self->{testing} ) ]
1147  
1148 # the table flush above removes both single IP and CIDR entries at once
1149 0 $self->{banned} = {};
1150 0 $self->{banned_cidr} = {};
1151 } ## end sub flush
1152  
1153 =head1 ERROR CODES / FLAGS
1154  
1155 Error handling is provided by L<Error::Helper>. All
1156 errors are considered fatal.
1157  
1158 =head2 1, notInited
1159  
1160 The backend has not been inited yet.
1161  
1162 =head2 2, invalidPortSpecified
1163  
1164 Port is either not an int within the range 1 to 65535 or a name that can be resolved by getservbyname.
1165  
1166 =head2 3, portsNotArray
1167  
1168 The data passed to new for ports is not an array.
1169  
1170 =head2 4, protocolsNotArray
1171  
1172 The data passed to new for protocols is not an array.
1173  
1174 =head2 5, invalidPortSpecified
1175  
1176 Port is either not an int within the range 1 to 65535 or a name that can be resolved by getservbyname.
1177  
1178 =head2 6, invalidPrefixSpecified
1179  
1180 The specified prefix did not match /^[a-zA-Z0-9]+$/.
1181  
1182 =head2 7, invalidName
1183  
1184 The name is either undef or does not match /^[a-zA-Z0-9\-]+$/.
1185  
1186 =head2 8, optionsNotHash
1187  
1188 The item passed to new for options is not a hash.
1189  
1190 =head2 9, noBanItem
1191  
1192 No IP or CIDR range specified to ban or unban.
1193  
1194 =head2 10, banItemNotIP
1195  
1196 The item to ban is not an IP. Either wrong ref type or regexp
1197 test using L<Regexp::IPv4> and L<Regexp::IPv6> failed.
1198  
1199 =head2 11, invalidBackend
1200  
1201 The specified backend failed to pass a basic sanity check of making sure it
1202 matches the regexp /^[a-zA-Z0-9\_]+$/.
1203  
1204 =head2 12, backendInitError
1205  
1206 Failed to init the backend.
1207  
1208 =head2 13, banFailed
1209  
1210 Failed to ban the item.
1211  
1212 =head2 14, unbanFailed
1213  
1214 Failed to unban the item.
1215  
1216 =head2 15, listFailed
1217  
1218 Failed to get a list of bans.
1219  
1220 =head2 16, reInitFailed
1221  
1222 Failed to re_init the backend.
1223  
1224 =head2 17, teardownFailed
1225  
1226 Failed to teardown the backend.
1227  
1228 =head2 18, alreadyInited
1229  
1230 init called, but the backend has already been inited.
1231  
1232 =head2 19, ruleInvalid
1233  
1234 The option rule is not an int of 1 or greater.
1235  
1236 =head2 20, typeInvalid
1237  
1238 The option type is not "deny", "unreach", or "unreach6".
1239  
1240 =head2 21, unreachInvalid
1241  
1242 The option unreach is not a value understood by ipfw(8) for unreach.
1243  
1244 =head2 22, unreach6Invalid
1245  
1246 The option unreach6 is not a value understood by ipfw(8) for unreach6.
1247  
1248 =head2 23, initFailed
1249  
1250 One of the required commands for init failed.
1251  
1252 =head2 24, checkFailed
1253  
1254 The backend check raised an error.
1255  
1256 =head2 25, flushFailed
1257  
1258 Failed to flush the bans.
1259  
1260 =head2 26, nameTooLong
1261  
1262 The combined prefix and name is longer than 63 characters, the max ipfw
1263 table name length.
1264  
1265 =head2 27, banCidrFailed
1266  
1267 Failed to ban the CIDR range.
1268  
1269 =head2 28, unbanCidrFailed
1270  
1271 Failed to unban the CIDR range.
1272  
1273 =head2 29, cidrItemNotCidr
1274  
1275 The item to ban is not a CIDR range. Either wrong ref type or it is not an
1276 IPv4 or IPv6 address followed by a prefix length valid for its family.
1277  
1278 =head2 30, cidrNotSupported
1279  
1280 The backend does not support CIDR bans.
1281  
1282 =head2 31, listCidrFailed
1283  
1284 Failed to get a list of CIDR bans.
1285  
1286 =head1 AUTHOR
1287  
1288 Zane C. Bowers-Hadley, C<< <vvelox at vvelox.ent> >>
1289  
1290 =head1 BUGS
1291  
1292 Please report any bugs or feature requests to C<bug-net-firewall-blockerhelper at rt.cpan.org>, or through
1293 the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Net-Firewall-BlockerHelper>. I will be notified, and then you'll
1294 automatically be notified of progress on your bug as I make changes.
1295  
1296  
1297  
1298  
1299 =head1 SUPPORT
1300  
1301 You can find documentation for this module with the perldoc command.
1302  
1303 perldoc Net::Firewall::BlockerHelper
1304  
1305  
1306 You can also look for information at:
1307  
1308 =over 4
1309  
1310 =item * RT: CPAN's request tracker (report bugs here)
1311  
1312 L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=Net-Firewall-BlockerHelper>
1313  
1314 =item * Search CPAN
1315  
1316 L<https://metacpan.org/release/Net-Firewall-BlockerHelper>
1317  
1318 =back
1319  
1320  
1321 =head1 ACKNOWLEDGEMENTS
1322  
1323  
1324 =head1 LICENSE AND COPYRIGHT
1325  
1326 This software is Copyright (c) 2023 by Zane C. Bowers-Hadley.
1327  
1328 This is free software, licensed under:
1329  
1330 The GNU Lesser General Public License, Version 2.1, February 1999
1331  
1332  
1333 =cut
1334  
1335 4 1; # End of Net::Firewall::BlockerHelper
↑ Summary BlockerHelper/backends/ufw.pm »