BlockerHelper/backends/dns_rpz.pm

filter: Statementstmt 50.0% 149 / 298 Branchbran 47.5% 59 / 124 Conditioncond 54.9% 28 / 51 MC/DCmcdc 29.0% 9 / 31 Subroutinesub 66.6% 16 / 24 Podpod 100% 13 / 13 totaltotal 50.6% 274 / 541 CC 84 SCAR 33.4
CC
84
Cov
50%
new
49.1
flush
42.0
unban
41.3
CRAP 28.3
« BlockerHelper/backends/routeros.pm ↑ Summary BlockerHelper/backends/opnsense.pm »
1 package Net::Firewall::BlockerHelper::backends::dns_rpz;
2  
3 1 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use base qw( Error::Helper Net::Firewall::BlockerHelper::Util );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Regexp::IPv4 qw($IPv4_re);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Regexp::IPv6 qw($IPv6_re);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 =head1 NAME
11  
12 Net::Firewall::BlockerHelper::backends::dns_rpz - DNS RPZ blocklist backend via nsupdate.
13  
14 =head1 VERSION
15  
16 Version 0.2.0
17  
18 =cut
19  
20 1 our $VERSION = '0.2.0';
21  
22 =head1 SYNOPSIS
23  
24 use Net::Firewall::BlockerHelper;
25  
26 my $fw_helper = Net::Firewall::BlockerHelper->new(
27 backend => 'dns_rpz',
28 name => 'resolver',
29 options => {
30 zone => 'rpz.example.org',
31 keyfile => '/etc/rpz.key',
32 },
33 );
34  
35 $fw_helper->init_backend;
36 $fw_helper->ban( ban => '1.2.3.4' );
37 $fw_helper->unban( ban => '1.2.3.4' );
38  
39 =head1 DESCRIPTION
40  
41 Blocks IPs at the DNS layer using a BIND Response Policy Zone (RPZ). By
42 default the C<rpz-client-ip> trigger is used, so a banned IP is prevented from
43 resolving anything through the resolver (the policy action is NXDOMAIN via a
44 C<CNAME .> record). Set the C<trigger> option to C<ip> to instead use the
45 C<rpz-ip> trigger, which rewrites answers that contain the banned IP.
46  
47 Records are added and removed with L<nsupdate(1)> (the DNS update protocol,
48 TSIG authenticated), so this backend runs the nsupdate command rather than
49 speaking HTTP. The RPZ zone must already exist and be referenced from the
50 resolver's C<response-policy> configuration.
51  
52 The owner name is the RPZ IP format: for IPv4 C<1.2.3.4> it is
53 C<< 32.4.3.2.1.rpz-client-ip.<zone> >>, and for IPv6 the eight groups are
54 reversed with the longest zero run replaced by C<zz>, prefixed with the
55 C<128> length.
56  
57 Blocking is per IP; ports and protocols are not supported and specifying
58 either is an error.
59  
60 =head1 METHODS
61  
62 =head2 new
63  
64 Initiates the object.
65  
66 - options :: Backend specific options. See below.
67 - name :: Required by Net::Firewall::BlockerHelper, otherwise unused.
68  
69 The options hash accepts the following. zone and keyfile are required.
70  
71 - zone :: The RPZ zone name.
72 - Default :: undef
73  
74 - keyfile :: Full path to the TSIG key file authenticating to the server.
75 - Default :: undef
76  
77 - trigger :: 'client-ip' (block the querying client) or 'ip' (rewrite
78 answers containing the IP).
79 - Default :: client-ip
80  
81 - server :: Optional server to send the updates to; adds a server line.
82 - Default :: undef
83  
84 - ttl :: TTL in seconds for the created records.
85 - Default :: 60
86  
87 - nsupdate :: The nsupdate command to use.
88 - Default :: nsupdate
89  
90 All errors are considered fatal, meaning if new fails it will die.
91  
92 =cut
93  
94 8 ▶sub new {
Subroutine new: called CC=29 SCAR=38.9
Pod documented
95 8 my ( $blank, %opts ) = @_;
96  
97 8 my $self = {
98 perror => undef,
99 error => undef,
100 errorLine => undef,
101 errorFilename => undef,
102 errorString => "",
103 errorExtra => {
104 all_errors_fatal => 1,
105 # all_fatal is what Error::Helper 2.1.0 actually checks; all_errors_fatal
106 # is kept for the name documented in its POD
107 all_fatal => 1,
108 flags => {
109 1 => 'notInited',
110 8 => 'optionsNotHash',
111 9 => 'noBanItem',
112 10 => 'banItemNotIP',
113 12 => 'backendInitError',
114 13 => 'banFailed',
115 14 => 'unbanFailed',
116 15 => 'listFailed',
117 16 => 'reInitFailed',
118 17 => 'teardownFailed',
119 18 => 'alreadyInited',
120 20 => 'triggerInvalid',
121 23 => 'initFailed',
122 24 => 'checkFailed',
123 25 => 'flushFailed',
124 26 => 'portsNotSupported',
125 27 => 'protocolsNotSupported',
126 30 => 'zoneInvalid',
127 31 => 'keyfileInvalid',
128 32 => 'banCidrFailed',
129 33 => 'unbanCidrFailed',
130 34 => 'cidrItemNotCidr',
131 35 => 'cidrNotSupported',
132 36 => 'listCidrFailed',
133 37 => 'serverInvalid',
134 38 => 'ttlInvalid',
135 39 => 'nsupdateInvalid',
136 },
137 fatal_flags => {},
138 perror_not_fatal => 0,
139 },
140 options => {},
141 ports => [],
142 protocols => [],
143 testing => undef,
144 test_data => undef,
145 prefix => 'kur',
146 name => undef,
147 frontend_obj => undef,
148 inited => 0,
149 banned => {},
150 cidr_supported => 0,
151 };
152 8 bless $self;
153  
154 8 ▶ if ( defined( $opts{ports} ) && ref( $opts{ports} ) eq 'ARRAY' && defined( $opts{ports}[0] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"ports"} and ref $opts{"ports"} eq "ARRAY" and defined $opts{"ports"}[0]) 0 8 8
Condition33%2/6
defined $opts{"ports"} and ref $opts{"ports"} eq "ARRAY" and defined $opts{"ports"}[0]
!ll&&!rl&&r
080
defined $opts{"ports"} and ref $opts{"ports"} eq "ARRAY"
!ll&&!rl&&r
008
MC/DC0%0/3
defined $opts{"ports"} and ref $opts{"ports"} eq "ARRAY" and defined $opts{"ports"}[0]
defined $opts{"ports"} ref $opts{"ports"} eq "ARRAY" defined $opts{"ports"}[0]
Truth table25%1/4
A and B and C
A= defined $opts{"ports"}B= ref $opts{"ports"} eq "ARRAY"C= defined $opts{"ports"}[0]
ABCresult
0XX0
10X0
1100
1111
155 0 $self->{perror} = 1;
156 0 $self->{error} = 26;
157 0 $self->{errorString} = 'the dns_rpz backend blocks whole IPs and does not support ports';
158 0 $self->warn;
159 }
160 8 ▶ if ( defined( $opts{protocols} ) && ref( $opts{protocols} ) eq 'ARRAY' && defined( $opts{protocols}[0] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"protocols"} and ref $opts{"protocols"} eq "ARRAY" and defined $opts{"protocols"}[0]) 0 8 8
Condition33%2/6
defined $opts{"protocols"} and ref $opts{"protocols"} eq "ARRAY" and defined $opts{"protocols"}[0]
!ll&&!rl&&r
080
defined $opts{"protocols"} and ref $opts{"protocols"} eq "ARRAY"
!ll&&!rl&&r
008
MC/DC0%0/3
defined $opts{"protocols"} and ref $opts{"protocols"} eq "ARRAY" and defined $opts{"protocols"}[0]
defined $opts{"protocols"} ref $opts{"protocols"} eq "ARRAY" defined $opts{"protocols"}[0]
Truth table25%1/4
A and B and C
A= defined $opts{"protocols"}B= ref $opts{"protocols"} eq "ARRAY"C= defined $opts{"protocols"}[0]
ABCresult
0XX0
10X0
1100
1111
161 0 $self->{perror} = 1;
162 0 $self->{error} = 27;
163 0 $self->{errorString} = 'the dns_rpz backend blocks whole IPs and does not support protocols';
164 0 $self->warn;
165 }
166  
167 8 ▶ if ( defined( $opts{testing} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"testing"}) 8 0 8
168 8 $self->{testing} = $opts{testing};
169 }
170 8 ▶ if ( defined( $opts{frontend_obj} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"frontend_obj"}) 8 0 8
171 8 $self->{frontend_obj} = $opts{frontend_obj};
172 }
173 8 ▶ if ( defined( $opts{name} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"name"}) 8 0 8
174 8 $self->{name} = $opts{name};
175 }
176  
177 8 ▶ if ( defined( $opts{options} ) && ref( $opts{options} ) ne 'HASH' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $opts{'options'} and ref $opts{'options'} ne 'HASH') { } 0 8 8
elsif (defined $opts{'options'}) { } 8 0 8
Condition33%1/3
defined $opts{"options"} and ref $opts{"options"} ne "HASH"
!ll&&!rl&&r
080
MC/DC0%0/2
defined $opts{"options"} and ref $opts{"options"} ne "HASH"
defined $opts{"options"} ref $opts{"options"} ne "HASH"
Truth table33%1/3
A and B
A= defined $opts{"options"}B= ref $opts{"options"} ne "HASH"
ABresult
0X0
100
111
178 0 $self->{perror} = 1;
179 0 $self->{error} = 8;
180 0 $self->{errorString} = 'ref for options is "' . ref( $opts{options} ) . '" and not HASH';
181 0 $self->warn;
182 } elsif ( defined( $opts{options} ) ) {
183 8 $self->{options} = $opts{options};
184 }
185  
186 8 ▶ if ( !defined( $self->{options}{zone} ) || $self->{options}{zone} !~ /^[a-zA-Z0-9.\-]+$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"options"}{"zone"} and $self->{"options"}{"zone"} =~ /^[a-zA-Z0-9.\-]+$/) 1 7 8
Condition66%2/3
defined $self->{"options"}{"zone"} and $self->{"options"}{"zone"} =~ /^[a-zA-Z0-9.\-]+$/
!ll&&!rl&&r
107
MC/DC50%1/2
defined $self->{"options"}{"zone"} and $self->{"options"}{"zone"} =~ /^[a-zA-Z0-9.\-]+$/
defined $self->{"options"}{"zone"} $self->{"options"}{"zone"} =~ /^[a-zA-Z0-9.\-]+$/
Truth table66%2/3
A and B
A= defined $self->{"options"}{"zone"}B= $self->{"options"}{"zone"} =~ /^[a-zA-Z0-9.\-]+$/
ABresult
0X0
100
111
187 1 $self->{perror} = 1;
188 1 $self->{error} = 30;
189 $self->{errorString}
190 = 'the option zone is '
191 1 ▶ . ( defined( $self->{options}{zone} ) ? '"' . $self->{options}{zone} . '" and not a valid zone' : 'undef' );
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'options'}{'zone'} ? : 0 1 1
192 1 $self->warn;
193 }
194  
195 # keyfile must be free of whitespace and single quotes so it is safe to embed
196 7 ▶ if ( !defined( $self->{options}{keyfile} ) || $self->{options}{keyfile} !~ /^[^'\s]+$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"options"}{"keyfile"} and $self->{"options"}{"keyfile"} =~ /^[^'\s]+$/) 1 6 7
Condition66%2/3
defined $self->{"options"}{"keyfile"} and $self->{"options"}{"keyfile"} =~ /^[^'\s]+$/
!ll&&!rl&&r
106
MC/DC50%1/2
defined $self->{"options"}{"keyfile"} and $self->{"options"}{"keyfile"} =~ /^[^'\s]+$/
defined $self->{"options"}{"keyfile"} $self->{"options"}{"keyfile"} =~ /^[^'\s]+$/
Truth table66%2/3
A and B
A= defined $self->{"options"}{"keyfile"}B= $self->{"options"}{"keyfile"} =~ /^[^'\s]+$/
ABresult
0X0
100
111
197 1 $self->{perror} = 1;
198 1 $self->{error} = 31;
199 $self->{errorString}
200 = 'the option keyfile is '
201 . (
202 defined( $self->{options}{keyfile} )
203 1 ▶ ? '"' . $self->{options}{keyfile} . '" which contains whitespace or single quotes'
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'options'}{'keyfile'} ? : 0 1 1
204 : 'undef'
205 );
206 1 $self->warn;
207 } ## end if ( !defined( $self->{options}{keyfile} )...)
208  
209 6 ▶ $self->{options}{trigger} = 'client-ip' if ( !defined( $self->{options}{trigger} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"options"}{"trigger"} 4 2 6
210  
211 # the following are embedded in the shell command _nsupdate_command builds,
212 # so limit each to characters safe inside its single quoted printf string
213  
214 # '' is treated the same as undef when the command is built
215 6 ▶ if ( defined( $self->{options}{server} )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"options"}{"server"} and $self->{"options"}{"server"} ne "" and not $self->{"options"}{"server"} =~ /^[a-zA-Z0-9.:\-]+$/) 1 5 6
Condition83%5/6
defined $self->{"options"}{"server"} and $self->{"options"}{"server"} ne "" and not $self->{"options"}{"server"} =~ /^[a-zA-Z0-9.:\-]+$/
!ll&&!rl&&r
411
defined $self->{"options"}{"server"} and $self->{"options"}{"server"} ne ""
!ll&&!rl&&r
402
MC/DC66%2/3
defined $self->{"options"}{"server"} and $self->{"options"}{"server"} ne "" and not $self->{"options"}{"server"} =~ /^[a-zA-Z0-9.:\-]+$/
defined $self->{"options"}{"server"} $self->{"options"}{"server"} ne "" not $self->{"options"}{"server"} =~ /^[a-zA-Z0-9.:\-]+$/
Truth table75%3/4
A and B and not(C)
A= defined $self->{"options"}{"server"}B= $self->{"options"}{"server"} ne ""C= not $self->{"options"}{"server"} =~ /^[a-zA-Z0-9.:\-]+$/
ABCresult
0XX0
10X0
1100
1111
216 && $self->{options}{server} ne ''
217 && $self->{options}{server} !~ /^[a-zA-Z0-9.:\-]+$/ )
218 {
219 1 $self->{perror} = 1;
220 1 $self->{error} = 37;
221 $self->{errorString}
222 1 = 'the option server, "' . $self->{options}{server} . '", does not match /^[a-zA-Z0-9.:\-]+$/';
223 1 $self->warn;
224 }
225  
226 5 ▶ if ( !defined( $self->{options}{ttl} ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $self->{'options'}{'ttl'}) { } 4 1 5
elsif (not $self->{'options'}{'ttl'} =~ /^[0-9]+$/) { } 1 0 1
227 4 $self->{options}{ttl} = 60;
228 } elsif ( $self->{options}{ttl} !~ /^[0-9]+$/ ) {
229 1 $self->{perror} = 1;
230 1 $self->{error} = 38;
231 1 $self->{errorString} = 'the option ttl, "' . $self->{options}{ttl} . '", is not an int';
232 1 $self->warn;
233 }
234  
235 4 ▶ if ( !defined( $self->{options}{nsupdate} ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $self->{'options'}{'nsupdate'}) { } 3 1 4
elsif (not $self->{'options'}{'nsupdate'} =~ m[^[a-zA-Z0-9./_\-]+$]) { } 1 0 1
236 3 $self->{options}{nsupdate} = 'nsupdate';
237 } elsif ( $self->{options}{nsupdate} !~ /^[a-zA-Z0-9.\/_\-]+$/ ) {
238 1 $self->{perror} = 1;
239 1 $self->{error} = 39;
240 $self->{errorString}
241 1 = 'the option nsupdate, "' . $self->{options}{nsupdate} . '", does not match /^[a-zA-Z0-9.\/_\-]+$/';
242 1 $self->warn;
243 }
244  
245 3 ▶ if ( $self->{options}{trigger} ne 'client-ip' && $self->{options}{trigger} ne 'ip' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"options"}{"trigger"} ne "client-ip" and $self->{"options"}{"trigger"} ne "ip") 1 2 3
Condition100%3/3
$self->{"options"}{"trigger"} ne "client-ip" and $self->{"options"}{"trigger"} ne "ip"
!ll&&!rl&&r
111
MC/DC100%2/2
$self->{"options"}{"trigger"} ne "client-ip" and $self->{"options"}{"trigger"} ne "ip"
$self->{"options"}{"trigger"} ne "client-ip" $self->{"options"}{"trigger"} ne "ip"
Truth table100%3/3
A and B
A= $self->{"options"}{"trigger"} ne "client-ip"B= $self->{"options"}{"trigger"} ne "ip"
ABresult
0X0
100
111
246 1 $self->{perror} = 1;
247 1 $self->{error} = 20;
248 1 $self->{errorString} = 'the option trigger is "' . $self->{options}{trigger} . '" and not "client-ip" or "ip"';
249 1 $self->warn;
250 }
251  
252 2 return $self;
253 } ## end sub new
254  
255 # Internal helper. Returns the DNS owner name that the RPZ record for the
256 # passed IP has to be created under. This is the name the ban and unban
257 # nsupdate statements operate on, so getting it wrong means writing a record
258 # that the resolver never consults.
259 #
260 # RPZ encodes an address into a domain name by writing a prefix length first
261 # and then the address backwards, label by label, since DNS names go from most
262 # specific to least specific while addresses read the other way. A single host
263 # is a full length prefix, so IPv4 uses 32 and IPv6 uses 128. The result is
264 # placed under the rpz-<trigger> label inside the policy zone.
265 #
266 # For IPv6 the address is first expanded to all eight groups, then reversed,
267 # then the longest run of zero groups is collapsed to the literal "zz". That
268 # "zz" is the RPZ spelling of the "::" shorthand. Only the longest run may be
269 # collapsed, which is why the run is searched for explicitly rather than
270 # replacing the first run found; collapsing a shorter run would produce a
271 # different, non canonical name and the record would not be found.
272 #
273 # Args:
274 #
275 # ip - The address to encode, as a plain string. Expected to be an already
276 # validated and lowercased IPv4 or IPv6 address. The family is
277 # decided by matching against $IPv4_re, so anything that is not a
278 # valid IPv4 address is encoded as IPv6.
279 #
280 # Returns the fully qualified owner name as a string, with no trailing dot.
281 # The trigger and zone come from the options, so the same IP produces a
282 # different name under a client-ip instance than under an ip instance.
283 #
284 # # trigger 'client-ip', zone 'rpz.example.org'
285 # $self->_owner('10.0.0.1');
286 # # 32.1.0.0.10.rpz-client-ip.rpz.example.org
287 #
288 # $self->_owner('2001:db8::1');
289 # # 128.1.zz.db8.2001.rpz-client-ip.rpz.example.org
290 #
291 # # trigger 'ip', so answers containing the IP are rewritten instead
292 # $self->_owner('10.0.0.1');
293 # # 32.1.0.0.10.rpz-ip.rpz.example.org
294 4 ▶sub _owner {
Subroutine _owner: called CC=10 SCAR=23.1
295 4 my ( $self, $ip ) = @_;
296  
297 4 my $rpz_ip;
298 4 ▶ if ( $ip =~ /\A$IPv4_re\z/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ip =~ /\A$Net::Firewall::BlockerHelper::backends::dns_rpz::IPv4_re\z/) { } 3 1 4
299 3 $rpz_ip = '32.' . join( '.', reverse( split( /\./, $ip ) ) );
300 } else {
301 # expand the eight IPv6 groups, reverse them, and replace the longest
302 # run of zero groups with zz (the RPZ IPv6 encoding)
303 1 my @groups = $self->_v6_groups($ip);
304 1 my @rev = reverse(@groups);
305  
306 # find the longest run of '0' groups
307 1 my ( $best_start, $best_len ) = ( -1, 0 );
308 1 my $i = 0;
309 1 ▶ while ( $i < scalar(@rev) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @rev) 4 1 5
310 4 ▶ if ( $rev[$i] eq '0' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rev[$i] eq '0') { } 1 3 4
311 1 my $j = $i;
312 1 ▶ $j++ while ( $j < scalar(@rev) && $rev[$j] eq '0' );
Branch: 1 branch
BranchTrueFalseTotal
$j < scalar @rev and $rev[$j] eq 0 and do { ++$j } 5 1 6
Condition66%2/3
$j < scalar @rev and $rev[$j] eq 0
!ll&&!rl&&r
015
MC/DC50%1/2
$j < scalar @rev and $rev[$j] eq 0
$j < scalar @rev $rev[$j] eq 0
Truth table66%2/3
A and B
A= $j < scalar @revB= $rev[$j] eq 0
ABresult
0X0
100
111
313 1 ▶ if ( ( $j - $i ) > $best_len ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j - $i > $best_len) 1 0 1
314 1 $best_len = $j - $i;
315 1 $best_start = $i;
316 }
317 1 $i = $j;
318 } else {
319 3 $i++;
320 }
321 } ## end while ( $i < scalar(@rev) )
322  
323 1 my @out;
324 1 $i = 0;
325 1 ▶ while ( $i < scalar(@rev) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @rev) 4 1 5
326 4 ▶ if ( $i == $best_start && $best_len > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i == $best_start and $best_len > 0) { } 1 3 4
Condition66%2/3
$i == $best_start and $best_len > 0
!ll&&!rl&&r
301
MC/DC50%1/2
$i == $best_start and $best_len > 0
$i == $best_start $best_len > 0
Truth table66%2/3
A and B
A= $i == $best_startB= $best_len > 0
ABresult
0X0
100
111
327 1 push( @out, 'zz' );
328 1 $i += $best_len;
329 } else {
330 3 push( @out, $rev[$i] );
331 3 $i++;
332 }
333 }
334  
335 1 $rpz_ip = '128.' . join( '.', @out );
336 } ## end else [ if ( $ip =~ /\A$IPv4_re\z/)]
337  
338 4 return $rpz_ip . '.rpz-' . $self->{options}{trigger} . '.' . $self->{options}{zone};
339 } ## end sub _owner
340  
341 # Internal helper. Expands a possibly abbreviated IPv6 address into its eight
342 # groups, normalized so that two spellings of the same address always come out
343 # identical.
344 #
345 # This exists because _owner has to reverse the groups and find the longest
346 # run of zeros, and neither is possible while the address still holds a "::"
347 # standing in for an unknown number of groups. Normalizing here also means
348 # "2001:0db8::0001" and "2001:db8::1" produce the same owner name, so a ban
349 # and a later unban of the same host cannot end up touching two different
350 # records.
351 #
352 # The "::" is expanded by splitting on it and filling the gap with as many
353 # zero groups as are needed to reach eight. Each group is then lowercased and
354 # has its leading zeros stripped, with an all zero group left as a single "0"
355 # rather than an empty string.
356 #
357 # Args:
358 #
359 # ip - The IPv6 address to expand, as a plain string. May be in any of the
360 # usual spellings: fully written out, abbreviated with "::" anywhere
361 # in it including at either end, and in any case. Expected to be a
362 # valid address; nothing here checks that, and an IPv4 address or a
363 # malformed string will simply produce a nonsense group list rather
364 # than an error.
365 #
366 # Returns the eight groups as a list of strings, in address order, each a
367 # lowercase hex string with no leading zeros. Note the groups are returned in
368 # the order they appear in the address, not reversed; reversing is the
369 # caller's job.
370 #
371 # $self->_v6_groups('2001:db8::1');
372 # # ( '2001', 'db8', '0', '0', '0', '0', '0', '1' )
373 #
374 # $self->_v6_groups('2001:0DB8:0000:0000:0000:0000:0000:0001');
375 # # ( '2001', 'db8', '0', '0', '0', '0', '0', '1' ) -- same as above
376 #
377 # $self->_v6_groups('::1');
378 # # ( '0', '0', '0', '0', '0', '0', '0', '1' )
379 #
380 # $self->_v6_groups('fe80::');
381 # # ( 'fe80', '0', '0', '0', '0', '0', '0', '0' )
382 1 ▶sub _v6_groups {
Subroutine _v6_groups: called CC=8 SCAR=23.2
383 1 my ( $self, $ip ) = @_;
384  
385 1 my @groups;
386 1 ▶ if ( $ip =~ /::/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ip =~ /::/) { } 1 0 1
387 1 my ( $left, $right ) = split( /::/, $ip, 2 );
388 1 ▶ my @l = ( defined($left) && $left ne '' ) ? split( /:/, $left ) : ();
Branch: 1 branch
BranchTrueFalseTotal
defined $left && $left ne '' ? : 1 0 1
Condition33%1/3
defined $left and $left ne ""
!ll&&!rl&&r
001
MC/DC0%0/2
defined $left and $left ne ""
defined $left $left ne ""
Truth table33%1/3
A and B
A= defined $leftB= $left ne ""
ABresult
0X0
100
111
389 1 ▶ my @r = ( defined($right) && $right ne '' ) ? split( /:/, $right ) : ();
Branch: 1 branch
BranchTrueFalseTotal
defined $right && $right ne '' ? : 1 0 1
Condition33%1/3
defined $right and $right ne ""
!ll&&!rl&&r
001
MC/DC0%0/2
defined $right and $right ne ""
defined $right $right ne ""
Truth table33%1/3
A and B
A= defined $rightB= $right ne ""
ABresult
0X0
100
111
390 1 my $fill = 8 - scalar(@l) - scalar(@r);
391 1 @groups = ( @l, ('0') x $fill, @r );
392 } else {
393 0 @groups = split( /:/, $ip );
394 }
395  
396 # normalize each group: lowercase, strip leading zeros, empty becomes 0
397 1 foreach my $g (@groups) {
398 8 $g = lc($g);
399 8 $g =~ s/^0+(?=.)//;
400 8 ▶ $g = '0' if ( $g eq '' );
Branch: 1 branch
BranchTrueFalseTotal
if $g eq "" 0 8 8
401 }
402  
403 1 return @groups;
404 } ## end sub _v6_groups
405  
406 # Internal helper. Wraps one or more update statements into the complete shell
407 # command that feeds them to nsupdate. Every change this backend makes to the
408 # policy zone goes out through here.
409 #
410 # nsupdate reads its statements from stdin rather than from arguments, so the
411 # statements are printed into a pipe. They are assembled in the order nsupdate
412 # needs: an optional server line, then the zone, then the caller's updates,
413 # then send. The server line is only emitted when the server option is set to
414 # something non empty, since leaving it out is what makes nsupdate fall back
415 # to the SOA MNAME for the zone. The trailing send is what actually transmits
416 # the update; without it nsupdate would exit having done nothing.
417 #
418 # Note the statements are joined with a literal backslash-n inside a single
419 # quoted printf format, so it is printf that turns them into real newlines,
420 # not perl.
421 #
422 # Args:
423 #
424 # @updates - One or more nsupdate statement lines, as plain strings with
425 # no trailing newline, such as
426 # 'update add 32.1.0.0.10.rpz-client-ip.rpz.example.org 3600 IN CNAME .'
427 # They are emitted in the order given, between the zone line
428 # and the send. Passing none is not an error and produces a
429 # command that connects and sends an empty update.
430 #
431 # Returns the command as a single string ready to hand to the runner, of the
432 # shape "printf '<statements>\n' | <nsupdate> -k '<keyfile>'". The nsupdate
433 # binary and the keyfile come from the options, and the keyfile is single
434 # quoted so a path holding spaces survives.
435 #
436 # $self->_nsupdate_command(
437 # 'update add 32.1.0.0.10.rpz-client-ip.rpz.example.org 3600 IN CNAME .' );
438 #
439 # # with no server option set:
440 # # printf 'zone rpz.example.org\nupdate add 32.1.0.0.10.rpz-client-ip.rpz.example.org 3600 IN CNAME .\nsend\n' | nsupdate -k '/etc/rpz.key'
441 #
442 # # with the server option set to ns1.example.org, a server line leads:
443 # # printf 'server ns1.example.org\nzone rpz.example.org\n...\nsend\n' | nsupdate -k '/etc/rpz.key'
444 #
445 # # several statements go out as one update, so they apply atomically
446 # $self->_nsupdate_command( @add_statements );
447 4 ▶sub _nsupdate_command {
Subroutine _nsupdate_command: called CC=3 SCAR=11.0
448 4 my ( $self, @updates ) = @_;
449  
450 4 my @statements;
451 4 ▶ if ( defined( $self->{options}{server} ) && $self->{options}{server} ne '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"options"}{"server"} and $self->{"options"}{"server"} ne "") 1 3 4
Condition66%2/3
defined $self->{"options"}{"server"} and $self->{"options"}{"server"} ne ""
!ll&&!rl&&r
301
MC/DC50%1/2
defined $self->{"options"}{"server"} and $self->{"options"}{"server"} ne ""
defined $self->{"options"}{"server"} $self->{"options"}{"server"} ne ""
Truth table66%2/3
A and B
A= defined $self->{"options"}{"server"}B= $self->{"options"}{"server"} ne ""
ABresult
0X0
100
111
452 1 push( @statements, 'server ' . $self->{options}{server} );
453 }
454 4 push( @statements, 'zone ' . $self->{options}{zone} );
455 4 push( @statements, @updates );
456 4 push( @statements, 'send' );
457  
458 return
459 "printf '"
460 . join( '\n', @statements )
461 . "\\n' | "
462 . $self->{options}{nsupdate} . " -k '"
463 4 . $self->{options}{keyfile} . "'";
464 } ## end sub _nsupdate_command
465  
466 # Internal helper. Returns the complete shell command that bans the passed IP
467 # by adding its RPZ record to the policy zone.
468 #
469 # The record added is a CNAME pointing at the root, ".", which is how RPZ
470 # spells the NXDOMAIN action: a resolver consulting the zone answers as though
471 # the name did not exist. Under the default client-ip trigger that means the
472 # banned host gets NXDOMAIN for everything it looks up; under the ip trigger
473 # it means answers containing the banned address are rewritten.
474 #
475 # The record is created with the configured ttl, 60 seconds by default. That
476 # ttl bounds how long a resolver may keep serving the ban after it has been
477 # lifted, so it is the main reason an unban does not always take effect
478 # immediately.
479 #
480 # Args:
481 #
482 # ip - The address to ban, as a plain string. Expected to be an already
483 # validated and lowercased IPv4 or IPv6 address. It is turned into an
484 # owner name by _owner, so the encoding rules there apply.
485 #
486 # Returns the full shell command as a single string, ready for the runner, as
487 # built by _nsupdate_command.
488 #
489 # $self->_ban_command('10.0.0.1');
490 # # printf 'zone rpz.example.org\nupdate add 32.1.0.0.10.rpz-client-ip.rpz.example.org 60 IN CNAME .\nsend\n' | nsupdate -k '/etc/rpz.key'
491 3 ▶sub _ban_command {
Subroutine _ban_command: called CC=1 SCAR=0.0
492 3 my ( $self, $ip ) = @_;
493  
494 return $self->_nsupdate_command(
495 3 'update add ' . $self->_owner($ip) . ' ' . $self->{options}{ttl} . ' IN CNAME .' );
496 }
497  
498 # Internal helper. Returns the complete shell command that unbans the passed IP
499 # by deleting its RPZ record from the policy zone.
500 #
501 # This is the exact inverse of _ban_command and deletes the same CNAME record
502 # it created, matched by owner name, type, and rdata. No ttl appears, as
503 # nsupdate delete statements do not take one.
504 #
505 # Deleting a record that is not there is not an error for nsupdate, so
506 # unbanning an IP that was never banned succeeds quietly. Note that resolvers
507 # may keep answering from cache until the record's ttl expires.
508 #
509 # Args:
510 #
511 # ip - The address to unban, as a plain string. Expected to be an already
512 # validated and lowercased IPv4 or IPv6 address, and to be spelled
513 # the same way it was when banned. _owner normalizes IPv6 spellings,
514 # so an abbreviated and a fully written out form of the same address
515 # both resolve to the same record.
516 #
517 # Returns the full shell command as a single string, ready for the runner, as
518 # built by _nsupdate_command.
519 #
520 # $self->_unban_command('10.0.0.1');
521 # # printf 'zone rpz.example.org\nupdate delete 32.1.0.0.10.rpz-client-ip.rpz.example.org IN CNAME .\nsend\n' | nsupdate -k '/etc/rpz.key'
522 1 ▶sub _unban_command {
Subroutine _unban_command: called CC=1 SCAR=0.0
523 1 my ( $self, $ip ) = @_;
524  
525 1 return $self->_nsupdate_command( 'update delete ' . $self->_owner($ip) . ' IN CNAME .' );
526 }
527  
528 =head2 init
529  
530 Initiates the backend. Nothing is created on the server; the configured
531 keyfile is just verified to exist and be a file.
532  
533 =cut
534  
535 2 ▶sub init {
Subroutine init: called CC=4 SCAR=19.1
Pod documented
536 2 my ( $self, %opts ) = @_;
537  
538 2 $self->errorblank;
539  
540 2 ▶ if ( $self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"inited"}) 0 2 2
541 0 $self->{error} = 18;
542 0 $self->{errorString} = 'backend has already been inited';
543 0 $self->warn;
544 }
545  
546 2 ▶ if ( $self->{testing} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'testing'}) { } 2 0 2
elsif (not -f $self->{'options'}{'keyfile'}) { } 0 0 0
547 2 $self->{frontend_obj}->{test_data} = 'inited';
548 } elsif ( !-f $self->{options}{keyfile} ) {
549 0 $self->{error} = 23;
550 $self->{errorString}
551 0 = 'init failed. the keyfile, "' . $self->{options}{keyfile} . '", does not exist or is not a file';
552 0 $self->warn;
553 }
554  
555 2 $self->{inited} = 1;
556 } ## end sub init
557  
558 =head2 ban
559  
560 Bans an IP. The value of ban is validated as being a IPv4 or IPv6 address
561 and lowercased, then a C<CNAME .> record is added at the RPZ owner name for
562 the IP by running nsupdate. Banning an already banned IP is a noop.
563  
564 $fw_helper->ban( ban => $ip );
565  
566 =cut
567  
568 3 ▶sub ban {
Subroutine ban: called CC=10 SCAR=36.6
Pod documented
569 3 my ( $self, %opts ) = @_;
570  
571 3 $self->errorblank;
572  
573 3 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 3 3
574 0 $self->{error} = 1;
575 0 $self->{errorString} = 'backend has not been inited';
576 0 $self->warn;
577 0 return;
578 }
579  
580 3 ▶ if ( !defined( $opts{ban} ) ) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $opts{'ban'}) { } 0 3 3
elsif (ref $opts{'ban'} ne '') { } 0 3 3
elsif (not $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::dns_rpz::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::dns_rpz::IPv6_re\z/) { } 0 3 3
581 0 $self->{error} = 9;
582 0 $self->{errorString} = 'Nothing specified for the value ban';
583 0 $self->warn;
584 0 return;
585 } elsif ( ref( $opts{ban} ) ne '' ) {
586 0 $self->{error} = 10;
587 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
588 0 $self->warn;
589 0 return;
590 ▶ } elsif ( $opts{ban} !~ /\A$IPv4_re\z/
Condition66%2/3
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::dns_rpz::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::dns_rpz::IPv6_re\z/
l!l&&r!l&&!r
210
MC/DC0%0/2
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::dns_rpz::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::dns_rpz::IPv6_re\z/
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::dns_rpz::IPv4_re\z/ $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::dns_rpz::IPv6_re\z/
Truth table66%2/3
A || B
A= $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::dns_rpz::IPv4_re\z/B= $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::dns_rpz::IPv6_re\z/
ABresult
1X1
011
000
591 && $opts{ban} !~ /\A$IPv6_re\z/ )
592 {
593 0 $self->{error} = 10;
594 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 IP';
595 0 $self->warn;
596 0 return;
597 }
598  
599 # lowercase so the same IPv6 IP in differing cases can't result in duplicate entries
600 3 $opts{ban} = lc( $opts{ban} );
601  
602 3 ▶ if ( $self->{banned}{ $opts{ban} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"banned"}{$opts{"ban"}}) 0 3 3
603 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
604 0 $self->{frontend_obj}->{test_data} = 'already banned';
605 }
606 0 return;
607 }
608  
609 3 my $command = $self->_ban_command( $opts{ban} );
610 3 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 3 0 3
611 3 $self->{frontend_obj}->{test_data} = $command;
612 } else {
613 0 my $output = `$command 2>&1`;
614 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
615 0 $self->{error} = 13;
616 $self->{errorString}
617 0 = 'ban failed. non-zero exit code for the command... "' . $command . '"... output... ' . $output;
618 0 $self->warn;
619 }
620 }
621  
622 3 $self->{banned}{ $opts{ban} } = 1;
623 } ## end sub ban
624  
625 =head2 unban
626  
627 Unbans an IP. The value of ban is validated as being a IPv4 or IPv6 address
628 and lowercased, then the C<CNAME .> record at the RPZ owner name for the IP
629 is deleted by running nsupdate. Unbanning an IP that is not banned is a
630 noop.
631  
632 $fw_helper->unban( ban => $ip );
633  
634 =cut
635  
636 1 ▶sub unban {
Subroutine unban: called CC=10 SCAR=37.2
Pod documented
637 1 my ( $self, %opts ) = @_;
638  
639 1 $self->errorblank;
640  
641 1 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 1 1
642 0 $self->{error} = 1;
643 0 $self->{errorString} = 'backend has not been inited';
644 0 $self->warn;
645 0 return;
646 }
647  
648 1 ▶ if ( !defined( $opts{ban} ) ) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $opts{'ban'}) { } 0 1 1
elsif (ref $opts{'ban'} ne '') { } 0 1 1
elsif (not $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::dns_rpz::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::dns_rpz::IPv6_re\z/) { } 0 1 1
649 0 $self->{error} = 9;
650 0 $self->{errorString} = 'Nothing specified for the value ban';
651 0 $self->warn;
652 0 return;
653 } elsif ( ref( $opts{ban} ) ne '' ) {
654 0 $self->{error} = 10;
655 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
656 0 $self->warn;
657 0 return;
658 ▶ } elsif ( $opts{ban} !~ /\A$IPv4_re\z/
Condition33%1/3
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::dns_rpz::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::dns_rpz::IPv6_re\z/
l!l&&r!l&&!r
100
MC/DC0%0/2
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::dns_rpz::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::dns_rpz::IPv6_re\z/
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::dns_rpz::IPv4_re\z/ $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::dns_rpz::IPv6_re\z/
Truth table33%1/3
A || B
A= $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::dns_rpz::IPv4_re\z/B= $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::dns_rpz::IPv6_re\z/
ABresult
1X1
011
000
659 && $opts{ban} !~ /\A$IPv6_re\z/ )
660 {
661 0 $self->{error} = 10;
662 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 IP';
663 0 $self->warn;
664 0 return;
665 }
666  
667 # lowercase so the same IPv6 IP in differing cases can't result in duplicate entries
668 1 $opts{ban} = lc( $opts{ban} );
669  
670 1 ▶ if ( !$self->{banned}{ $opts{ban} } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"banned"}{$opts{"ban"}}) 0 1 1
671 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
672 0 $self->{frontend_obj}->{test_data} = 'not banned';
673 }
674 0 return;
675 }
676  
677 1 my $command = $self->_unban_command( $opts{ban} );
678 1 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 1 0 1
679 1 $self->{frontend_obj}->{test_data} = $command;
680 } else {
681 0 my $output = `$command 2>&1`;
682 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
683 0 $self->{error} = 14;
684 $self->{errorString}
685 0 = 'unban failed. non-zero exit code for the command... "' . $command . '"... output... ' . $output;
686 0 $self->warn;
687 }
688 }
689  
690 1 delete( $self->{banned}{ $opts{ban} } );
691 } ## end sub unban
692  
693 =head2 list
694  
695 List banned IPs. Returns an array of the currently banned IPs.
696  
697 my @banned = $fw_helper->list;
698  
699 =cut
700  
701 0 ▶sub list {
Subroutine list: not called CC=2 SCAR=17.9
Pod documented
702 0 my ( $self, %opts ) = @_;
703  
704 0 $self->errorblank;
705  
706 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
707 0 $self->{frontend_obj}->{test_data} = 'list';
708 }
709  
710 0 return keys( %{ $self->{banned} } );
711 }
712  
713 =head2 ban_cidr
714  
715 CIDR bans are not supported by this backend; this always sets the
716 cidrNotSupported error.
717  
718 $backend->ban_cidr(ban => '1.2.3.0/24');
719  
720 =cut
721  
722 0 ▶sub ban_cidr {
Subroutine ban_cidr: not called CC=1 SCAR=6.9
Pod documented
723 0 my ( $self, %opts ) = @_;
724  
725 0 $self->errorblank;
726  
727 0 $self->{error} = 35;
728 0 $self->{errorString} = 'the ' . __PACKAGE__ . ' backend does not support CIDR bans';
729 0 $self->warn;
730  
731 0 return;
732 } ## end sub ban_cidr
733  
734 =head2 unban_cidr
735  
736 CIDR bans are not supported by this backend; this always sets the
737 cidrNotSupported error.
738  
739 $backend->unban_cidr(ban => '1.2.3.0/24');
740  
741 =cut
742  
743 0 ▶sub unban_cidr {
Subroutine unban_cidr: not called CC=1 SCAR=6.9
Pod documented
744 0 my ( $self, %opts ) = @_;
745  
746 0 $self->errorblank;
747  
748 0 $self->{error} = 35;
749 0 $self->{errorString} = 'the ' . __PACKAGE__ . ' backend does not support CIDR bans';
750 0 $self->warn;
751  
752 0 return;
753 } ## end sub unban_cidr
754  
755 =head2 list_cidr
756  
757 CIDR bans are not supported by this backend, so this always returns an empty
758 list.
759  
760 my @banned_cidrs = $backend->list_cidr;
761  
762 =cut
763  
764 0 ▶sub list_cidr {
Subroutine list_cidr: not called CC=1 SCAR=6.9
Pod documented
765 0 my ( $self, %opts ) = @_;
766  
767 0 $self->errorblank;
768  
769 0 return ();
770 }
771  
772 =head2 re_init
773  
774 Tears down and re-initiates. teardown is called best effort, init is run
775 again, and then the RPZ record for every retained banned IP is re-added
776 via nsupdate.
777  
778 =cut
779  
780 0 ▶sub re_init {
Subroutine re_init: not called CC=5 SCAR=34.0
Pod documented
781 0 my ( $self, %opts ) = @_;
782  
783 0 $self->errorblank;
784  
785 {
786 0 local $@;
787 0 eval { $self->teardown; };
788 }
789 0 $self->init;
790  
791 0 my @commands;
792 0 foreach my $item ( keys( %{ $self->{banned} } ) ) {
793 0 my $command = $self->_ban_command($item);
794 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
795 0 push( @commands, $command );
796 } else {
797 0 my $output = `$command 2>&1`;
798 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
799 0 $self->{error} = 13;
800 $self->{errorString}
801 0 = 'ban failed. non-zero exit code for the command... "' . $command . '"... output... ' . $output;
802 0 $self->warn;
803 }
804 }
805 } ## end foreach my $item ( keys( %{ ...}))
806  
807 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
808 0 $self->{frontend_obj}->{test_data} = \@commands;
809 }
810  
811 0 $self->{inited} = 1;
812 } ## end sub re_init
813  
814 =head2 teardown
815  
816 Removes the RPZ record for each currently banned IP. The internal ban list is
817 kept so a following re_init restores them.
818  
819 =cut
820  
821 0 ▶sub teardown {
Subroutine teardown: not called CC=5 SCAR=34.0
Pod documented
822 0 my ( $self, %opts ) = @_;
823  
824 0 $self->errorblank;
825  
826 0 $self->{inited} = 0;
827  
828 0 my @commands;
829 0 foreach my $item ( sort( keys( %{ $self->{banned} } ) ) ) {
830 0 my $command = $self->_unban_command($item);
831 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
832 0 push( @commands, $command );
833 } else {
834 0 my $output = `$command 2>&1`;
835 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
836 0 $self->{error} = 17;
837 $self->{errorString}
838 0 = 'teardown failed. non-zero exit code for the command... "' . $command . '"... output... ' . $output;
839 0 $self->warn;
840 }
841 }
842 } ## end foreach my $item ( sort( keys...))
843  
844 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
845 0 $self->{frontend_obj}->{test_data} = \@commands;
846 }
847 } ## end sub teardown
848  
849 =head2 stop
850  
851 Alias for L</teardown>.
852  
853 =cut
854  
855 0 ▶sub stop {
Subroutine stop: not called CC=1 SCAR=6.9
Pod documented
856 0 my ( $self, %opts ) = @_;
857  
858 0 return $self->teardown(%opts);
859 }
860  
861 =head2 check
862  
863 Verifies the keyfile still exists and is a file. Returns a true value if so
864 and a false value otherwise.
865  
866 =cut
867  
868 4 ▶sub check {
Subroutine check: called CC=3 SCAR=12.7
Pod documented
869 4 my ( $self, %opts ) = @_;
870  
871 4 $self->errorblank;
872  
873 4 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 4 0 4
874 4 $self->{frontend_obj}->{test_data} = 'check';
875 4 return 1;
876 }
877  
878 0 ▶ return ( -f $self->{options}{keyfile} ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
-f $self->{'options'}{'keyfile'} ? : 0 0 0
879 } ## end sub check
880  
881 =head2 flush
882  
883 Removes all currently banned IPs at once by running a nsupdate delete for
884 each RPZ record and clearing the ban list.
885  
886 =cut
887  
888 0 ▶sub flush {
Subroutine flush: not called CC=6 SCAR=37.4
Pod documented
889 0 my ( $self, %opts ) = @_;
890  
891 0 $self->errorblank;
892  
893 0 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 0 0
894 0 $self->{error} = 1;
895 0 $self->{errorString} = 'backend has not been inited';
896 0 $self->warn;
897 0 return;
898 }
899  
900 0 my @commands;
901 0 foreach my $item ( sort( keys( %{ $self->{banned} } ) ) ) {
902 0 my $command = $self->_unban_command($item);
903 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
904 0 push( @commands, $command );
905 } else {
906 0 my $output = `$command 2>&1`;
907 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
908 0 $self->{error} = 25;
909 $self->{errorString}
910 0 = 'flush failed. non-zero exit code for the command... "' . $command . '"... output... ' . $output;
911 0 $self->warn;
912 }
913 }
914 } ## end foreach my $item ( sort( keys...))
915  
916 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
917 0 $self->{frontend_obj}->{test_data} = \@commands;
918 }
919  
920 0 $self->{banned} = {};
921 } ## end sub flush
922  
923 =head1 ERROR CODES / FLAGS
924  
925 Error handling is provided by L<Error::Helper>. All
926 errors are considered fatal.
927  
928 =head2 1, notInited
929  
930 The backend has not been inited yet.
931  
932 =head2 8, optionsNotHash
933  
934 The item passed to new for options is not a hash.
935  
936 =head2 9, noBanItem
937  
938 No IP specified to ban or unban.
939  
940 =head2 10, banItemNotIP
941  
942 The item to ban is not an IP. Either wrong ref type or regexp
943 test using L<Regexp::IPv4> and L<Regexp::IPv6> failed.
944  
945 =head2 12, backendInitError
946  
947 Failed to init the backend.
948  
949 =head2 13, banFailed
950  
951 Failed to ban the item.
952  
953 =head2 14, unbanFailed
954  
955 Failed to unban the item.
956  
957 =head2 15, listFailed
958  
959 Failed to get a list of bans.
960  
961 =head2 16, reInitFailed
962  
963 Failed to re_init the backend.
964  
965 =head2 17, teardownFailed
966  
967 Failed to teardown the backend.
968  
969 =head2 18, alreadyInited
970  
971 init called, but the backend has already been inited.
972  
973 =head2 20, triggerInvalid
974  
975 The option trigger is not "client-ip" or "ip".
976  
977 =head2 23, initFailed
978  
979 Init failed. The keyfile does not exist or is not a file.
980  
981 =head2 24, checkFailed
982  
983 The backend check raised an error.
984  
985 =head2 25, flushFailed
986  
987 Failed to flush the bans.
988  
989 =head2 26, portsNotSupported
990  
991 The dns_rpz backend blocks whole IPs and does not support ports.
992  
993 =head2 27, protocolsNotSupported
994  
995 The dns_rpz backend blocks whole IPs and does not support protocols.
996  
997 =head2 30, zoneInvalid
998  
999 The option zone is undef or not a valid zone name.
1000  
1001 =head2 31, keyfileInvalid
1002  
1003 The option keyfile is undef or contains whitespace or single quotes.
1004  
1005 =head2 32, banCidrFailed
1006  
1007 Failed to ban the CIDR range.
1008  
1009 =head2 33, unbanCidrFailed
1010  
1011 Failed to unban the CIDR range.
1012  
1013 =head2 34, cidrItemNotCidr
1014  
1015 The item to ban is not a CIDR range. Either wrong ref type or it is not an
1016 IPv4 or IPv6 address followed by a prefix length valid for its family.
1017  
1018 =head2 35, cidrNotSupported
1019  
1020 The backend does not support CIDR bans.
1021  
1022 =head2 36, listCidrFailed
1023  
1024 Failed to get a list of CIDR bans.
1025  
1026 =head2 37, serverInvalid
1027  
1028 The option server does not match /^[a-zA-Z0-9.:\-]+$/.
1029  
1030 =head2 38, ttlInvalid
1031  
1032 The option ttl is not an int.
1033  
1034 =head2 39, nsupdateInvalid
1035  
1036 The option nsupdate does not match /^[a-zA-Z0-9.\/_\-]+$/.
1037  
1038 =head1 AUTHOR
1039  
1040 Zane C. Bowers-Hadley, C<< <vvelox at vvelox.ent> >>
1041  
1042 =head1 LICENSE AND COPYRIGHT
1043  
1044 This software is Copyright (c) 2023 by Zane C. Bowers-Hadley.
1045  
1046 This is free software, licensed under:
1047  
1048 The GNU Lesser General Public License, Version 2.1, February 1999
1049  
1050 =cut
1051  
1052 1 1; # End of Net::Firewall::BlockerHelper::backends::dns_rpz
« BlockerHelper/backends/routeros.pm ↑ Summary BlockerHelper/backends/opnsense.pm »