BlockerHelper/backends/nsupdate.pm

filter: Statementstmt 51.8% 142 / 274 Branchbran 53.8% 56 / 104 Conditioncond 71.4% 15 / 21 MC/DCmcdc 33.3% 4 / 12 Subroutinesub 80.9% 17 / 21 Podpod 100% 13 / 13 totaltotal 55.5% 247 / 445 CC 65 SCAR 35.2
CC
65
Cov
53%
new
70.0
unban
44.6
ban
26.1
CRAP 33.8
« BlockerHelper/backends/routeros.pm ↑ Summary BlockerHelper/backends/dns_rpz.pm »
1 package Net::Firewall::BlockerHelper::backends::nsupdate;
2  
3 1 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use base 'Error::Helper';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Regexp::IPv4 qw($IPv4_re);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Regexp::IPv6 qw($IPv6_re);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 =head1 NAME
11  
12 Net::Firewall::BlockerHelper::backends::nsupdate - Dynamic DNS blocklist backend for Net::Firewall::BlockerHelper.
13  
14 =head1 VERSION
15  
16 Version 0.1.0
17  
18 =cut
19  
20 1 our $VERSION = '0.1.0';
21  
22 =head1 SYNOPSIS
23  
24 All this backend does is add and remove TXT records, so two bits of
25 external setup are required for bans to actually mean anything. First the
26 zone the records live under must exist on the DNS server and allow dynamic
27 updates authenticated via the TSIG key, the like of the following in
28 C<named.conf> for BIND, with the key also declared there via a C<key>
29 statement matching the keyfile.
30  
31 zone "rbl.foo.bar" {
32 type primary;
33 file "rbl.foo.bar.db";
34 allow-update { key "nsupdate-key"; };
35 };
36  
37 Second, a DNS record on its own blocks nothing. Whatever should be turning
38 away the banned IPs must be configured to consult the resulting blocklist.
39 Also worth noting is that only TXT records are created, while the usual
40 DNSBL style lookup checks for an A record, so the consumer needs to be
41 something that can be pointed at TXT records.
42  
43 use Net::Firewall::BlockerHelper;
44  
45 my $fw_helper = Net::Firewall::BlockerHelper->new(
46 backend => 'nsupdate',
47 name => 'ssh',
48 options => {
49 domain => 'rbl.foo.bar',
50 keyfile => '/etc/nsupdate.key',
51 },
52 );
53  
54 $fw_helper->init_backend;
55 $fw_helper->ban(ban => '1.2.3.4');
56 $fw_helper->unban(ban => '1.2.3.4');
57  
58 =head1 DESCRIPTION
59  
60 Maintains a DNS based blocklist (RBL style) by using L<nsupdate(1)> to
61 dynamically add and remove TXT records under a BIND zone, equivalent to the
62 fail2ban nsupdate action. Banning C<1.2.3.4> adds a TXT record at
63 C<4.3.2.1.E<lt>domainE<gt>>.
64  
65 nsupdate speaks the DNS update protocol authenticated via a TSIG key, not
66 HTTP, so this backend runs the nsupdate command rather than using
67 L<LWP::UserAgent>.
68  
69 Only IPv4 is supported, matching the fail2ban action; banning an IPv6 IP is
70 an error. Ports and protocols are not supported and specifying them is an
71 error. check has nothing it can probe cheaply and always reports healthy.
72  
73 =head1 METHODS
74  
75 =head2 new
76  
77 Initiates the the object.
78  
79 - options :: Backend specific options. See below.
80 - Default :: {}
81  
82 - prefix :: Prefix to use. Must match the regex /^[a-zA-Z0-9]+$/
83 - default :: kur
84  
85 - name :: Name of this specific instance. This must be specified.
86 - default :: undef
87  
88 Ports and protocols are not supported by this backend and specifying either
89 is an error.
90  
91 The options hash accepts the following. domain and keyfile must be
92 specified.
93  
94 - domain :: DNS domain the records are managed under.
95 - Default :: undef
96  
97 - keyfile :: Full path to the TSIG key file used to authenticate to
98 BIND. Verified to exist at init.
99 - Default :: undef
100  
101 - ttl :: TTL in seconds for the created TXT records.
102 - Default :: 60
103  
104 - rdata :: Data of the created TXT records.
105 - Default :: banned
106  
107 - nsupdate :: The nsupdate command to use.
108 - Default :: nsupdate
109  
110 All errors are considered fatal, meaning if new fails it will die.
111  
112 =cut
113  
114 sub new {
115 6 ▶ my ( $blank, %opts ) = @_;
Subroutine new: called CC=28 SCAR=42.5
Pod documented
116  
117 6 my $self = {
118 perror => undef,
119 error => undef,
120 errorLine => undef,
121 errorFilename => undef,
122 errorString => "",
123 errorExtra => {
124 all_errors_fatal => 1,
125 # all_fatal is what Error::Helper 2.1.0 actually checks; all_errors_fatal
126 # is kept for the name documented in its POD
127 all_fatal => 1,
128 flags => {
129 1 => 'notInited',
130 6 => 'invalidPrefixSpecified',
131 7 => 'invalidName',
132 8 => 'optionsNotHash',
133 9 => 'noBanItem',
134 10 => 'banItemNotIP',
135 12 => 'backendInitError',
136 13 => 'banFailed',
137 14 => 'unbanFailed',
138 15 => 'listFailed',
139 16 => 'reInitFailed',
140 17 => 'teardownFailed',
141 18 => 'alreadyInited',
142 23 => 'initFailed',
143 24 => 'checkFailed',
144 25 => 'flushFailed',
145 26 => 'portsNotSupported',
146 27 => 'protocolsNotSupported',
147 28 => 'optionInvalid',
148 29 => 'ipv6NotSupported',
149 30 => 'banCidrFailed',
150 31 => 'unbanCidrFailed',
151 32 => 'cidrItemNotCidr',
152 33 => 'cidrNotSupported',
153 34 => 'listCidrFailed',
154 },
155 fatal_flags => {},
156 perror_not_fatal => 0,
157 },
158 options => {},
159 ports => [],
160 protocols => [],
161 testing => undef,
162 test_data => undef,
163 prefix => 'kur',
164 frontend_obj => undef,
165 inited => 0,
166 banned => {},
167 cidr_supported => 0,
168 };
169 6 bless $self;
170  
171 # blocking is per IP; DNS records have no concept of ports or protocols
172 6 ▶ 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]) 1 5 6
Condition83%5/6
defined $opts{"ports"} and ref $opts{"ports"} eq "ARRAY" and defined $opts{"ports"}[0]
!ll&&!rl&&r
231
defined $opts{"ports"} and ref $opts{"ports"} eq "ARRAY"
!ll&&!rl&&r
204
MC/DC66%2/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 table75%3/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
173 1 $self->{perror} = 1;
174 1 $self->{error} = 26;
175 1 $self->{errorString} = 'the nsupdate backend lists whole IPs and does not support ports';
176 1 $self->warn;
177 }
178 5 ▶ 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 5 5
Condition66%4/6
defined $opts{"protocols"} and ref $opts{"protocols"} eq "ARRAY" and defined $opts{"protocols"}[0]
!ll&&!rl&&r
230
defined $opts{"protocols"} and ref $opts{"protocols"} eq "ARRAY"
!ll&&!rl&&r
203
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 table50%2/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
179 0 $self->{perror} = 1;
180 0 $self->{error} = 27;
181 0 $self->{errorString} = 'the nsupdate backend lists whole IPs and does not support protocols';
182 0 $self->warn;
183 }
184  
185 # make sure prefix is sane if defiend
186 5 ▶ 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 5 5
elsif (defined $opts{'prefix'}) { } 3 2 5
Condition66%2/3
defined $opts{"prefix"} and not $opts{"prefix"} =~ /^[a-zA-Z0-9]+$/
!ll&&!rl&&r
230
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 table66%2/3
A and not(B)
A= defined $opts{"prefix"}B= not $opts{"prefix"} =~ /^[a-zA-Z0-9]+$/
ABresult
0X0
100
111
187 0 $self->{perror} = 1;
188 0 $self->{error} = 6;
189 $self->{errorString}
190 0 = '"' . $opts{prefix} . '" is not a valid prefix as it does not match the regex /^[a-zA-Z0-9]+$/';
191 0 $self->warn;
192 } elsif ( defined( $opts{prefix} ) ) {
193 3 $self->{prefix} = $opts{prefix};
194 }
195  
196 # make sure we have a name and that it is valid
197 5 ▶ if ( !defined( $opts{name} ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $opts{'name'}) { } 0 5 5
elsif (not $opts{'name'} =~ /^[a-zA-Z0-9\-]+$/) { } 0 5 5
198 0 $self->{perror} = 1;
199 0 $self->{error} = 7;
200 0 $self->{errorString} = 'name is undef';
201 0 $self->warn;
202 } elsif ( $opts{name} !~ /^[a-zA-Z0-9\-]+$/ ) {
203 0 $self->{perror} = 1;
204 0 $self->{error} = 7;
205 0 $self->{errorString} = 'name set to "' . $opts{name} . '" which does not match the regexp /^[a-zA-Z0-9\-]+$/';
206 0 $self->warn;
207 }
208 5 $self->{name} = $opts{name};
209  
210 # used internally for testing
211 5 ▶ if ( defined( $opts{testing} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"testing"}) 3 2 5
212 3 $self->{testing} = $opts{testing};
213 }
214 5 ▶ if ( defined( $opts{frontend_obj} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"frontend_obj"}) 3 2 5
215 3 $self->{frontend_obj} = $opts{frontend_obj};
216 }
217  
218 5 ▶ if ( defined( $opts{options} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"options"}) 5 0 5
219 5 ▶ if ( ref( $opts{options} ) ne 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $opts{"options"} ne "HASH") 0 5 5
220 0 $self->{perror} = 1;
221 0 $self->{error} = 8;
222 0 $self->{errorString} = 'ref for options is "' . ref( $opts{options} ) . '" and not HASH';
223 0 $self->warn;
224 }
225 5 $self->{options} = $opts{options};
226 }
227  
228 5 ▶ if ( !defined( $self->{options}{domain} ) || $self->{options}{domain} !~ /^[a-zA-Z0-9.\-]+$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"options"}{"domain"} and $self->{"options"}{"domain"} =~ /^[a-zA-Z0-9.\-]+$/) 1 4 5
Condition66%2/3
defined $self->{"options"}{"domain"} and $self->{"options"}{"domain"} =~ /^[a-zA-Z0-9.\-]+$/
!ll&&!rl&&r
104
MC/DC50%1/2
defined $self->{"options"}{"domain"} and $self->{"options"}{"domain"} =~ /^[a-zA-Z0-9.\-]+$/
defined $self->{"options"}{"domain"} $self->{"options"}{"domain"} =~ /^[a-zA-Z0-9.\-]+$/
Truth table66%2/3
A and B
A= defined $self->{"options"}{"domain"}B= $self->{"options"}{"domain"} =~ /^[a-zA-Z0-9.\-]+$/
ABresult
0X0
100
111
229 1 $self->{perror} = 1;
230 1 $self->{error} = 28;
231 $self->{errorString}
232 = 'the option domain is '
233 . (
234 defined( $self->{options}{domain} )
235 1 ▶ ? '"' . $self->{options}{domain} . '" and not a valid domain'
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'options'}{'domain'} ? : 0 1 1
236 : 'undef'
237 );
238 1 $self->warn;
239 } ## end if ( !defined( $self->{options}{domain} ) ...)
240  
241 # no whitespace or single quotes given it is single quoted when passed to the shell
242 4 ▶ if ( !defined( $self->{options}{keyfile} ) || $self->{options}{keyfile} !~ /^[^'\s]+$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"options"}{"keyfile"} and $self->{"options"}{"keyfile"} =~ /^[^'\s]+$/) 1 3 4
Condition66%2/3
defined $self->{"options"}{"keyfile"} and $self->{"options"}{"keyfile"} =~ /^[^'\s]+$/
!ll&&!rl&&r
013
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
243 1 $self->{perror} = 1;
244 1 $self->{error} = 28;
245 $self->{errorString}
246 = 'the option keyfile is '
247 . (
248 defined( $self->{options}{keyfile} )
249 1 ▶ ? '"' . $self->{options}{keyfile} . '" which contains whitespace or single quotes'
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'options'}{'keyfile'} ? : 1 0 1
250 : 'undef'
251 );
252 1 $self->warn;
253 } ## end if ( !defined( $self->{options}{keyfile} )...)
254  
255 3 ▶ if ( !defined( $self->{options}{ttl} ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $self->{'options'}{'ttl'}) { } 2 1 3
elsif (not $self->{'options'}{'ttl'} =~ /^[0-9]+$/) { } 0 1 1
256 2 $self->{options}{ttl} = 60;
257 } elsif ( $self->{options}{ttl} !~ /^[0-9]+$/ ) {
258 0 $self->{perror} = 1;
259 0 $self->{error} = 28;
260 0 $self->{errorString} = 'the option ttl, "' . $self->{options}{ttl} . '", is not an int';
261 0 $self->warn;
262 }
263  
264 3 ▶ if ( !defined( $self->{options}{rdata} ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $self->{'options'}{'rdata'}) { } 2 1 3
elsif (not $self->{'options'}{'rdata'} =~ /^[a-zA-Z0-9 .,:_\-]+$/) { } 0 1 1
265 2 $self->{options}{rdata} = 'banned';
266 } elsif ( $self->{options}{rdata} !~ /^[a-zA-Z0-9 .,:_\-]+$/ ) {
267 0 $self->{perror} = 1;
268 0 $self->{error} = 28;
269 $self->{errorString}
270 = 'the option rdata, "'
271 . $self->{options}{rdata}
272 0 . '", does not match /^[a-zA-Z0-9 .,:_\-]+$/';
273 0 $self->warn;
274 }
275  
276 3 ▶ if ( !defined( $self->{options}{nsupdate} ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $self->{'options'}{'nsupdate'}) { } 2 1 3
elsif (not $self->{'options'}{'nsupdate'} =~ m[^[a-zA-Z0-9./_\-]+$]) { } 0 1 1
277 2 $self->{options}{nsupdate} = 'nsupdate';
278 } elsif ( $self->{options}{nsupdate} !~ /^[a-zA-Z0-9.\/_\-]+$/ ) {
279 0 $self->{perror} = 1;
280 0 $self->{error} = 28;
281 $self->{errorString}
282 = 'the option nsupdate, "'
283 . $self->{options}{nsupdate}
284 0 . '", does not match /^[a-zA-Z0-9.\/_\-]+$/';
285 0 $self->warn;
286 }
287  
288 3 return $self;
289 } ## end sub new
290  
291 # Internal helper. Returns the record name for the passed IPv4 IP, the octets
292 # reversed under the domain.
293 sub _record_name {
294 8 ▶ my ( $self, $ip ) = @_;
Subroutine _record_name: called CC=1 SCAR=0.0
295  
296 8 my @octets = split( /\./, $ip );
297  
298 8 return join( '.', reverse(@octets) ) . '.' . $self->{options}{domain};
299 }
300  
301 # Internal helper. Returns the shell command piping the passed update
302 # statements into nsupdate.
303 sub _nsupdate_command {
304 8 ▶ my ( $self, @statements ) = @_;
Subroutine _nsupdate_command: called CC=1 SCAR=0.0
305  
306 return
307 "printf '"
308 . join( '\n', @statements )
309 . "\\n' | "
310 . $self->{options}{nsupdate} . " -k '"
311 8 . $self->{options}{keyfile} . "'";
312 } ## end sub _nsupdate_command
313  
314 =head2 init
315  
316 Initiates the backend. Verifies the keyfile exists.
317  
318 Note that the zone must allow updates via the key and something must be
319 consuming the blocklist for bans to have an effect. See L</SYNOPSIS>.
320  
321 $backend->init;
322  
323 =cut
324  
325 sub init {
326 5 ▶ my ( $self, %opts ) = @_;
Subroutine init: called CC=4 SCAR=19.1
Pod documented
327  
328 5 $self->errorblank;
329  
330 5 ▶ if ( $self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"inited"}) 0 5 5
331 0 $self->{error} = 18;
332 0 $self->{errorString} = 'backend has already been inited';
333 0 $self->warn;
334 }
335  
336 5 ▶ if ( $self->{testing} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'testing'}) { } 5 0 5
elsif (not -f $self->{'options'}{'keyfile'}) { } 0 0 0
337 5 $self->{frontend_obj}->{test_data} = 'inited';
338 } elsif ( !-f $self->{options}{keyfile} ) {
339 0 $self->{error} = 23;
340 $self->{errorString}
341 0 = 'init failed. the keyfile, "' . $self->{options}{keyfile} . '", does not exist or is not a file';
342 0 $self->warn;
343 }
344  
345 5 $self->{inited} = 1;
346 } ## end sub init
347  
348 =head2 ban
349  
350 Bans an IP. The value of ban is validated as being a IPv4 address; IPv6 is
351 not supported and is an error. The octets are reversed and a TXT record is
352 added at that name under the domain, eg banning 1.2.3.4 adds a TXT record
353 at C<4.3.2.1.E<lt>domainE<gt>>, by piping an update with a nxrrset prereq
354 into nsupdate authenticated via the TSIG keyfile. Banning an already banned
355 IP is a noop.
356  
357 $backend->ban(ban => $ip);
358  
359 =cut
360  
361 sub ban {
362 5 ▶ my ( $self, %opts ) = @_;
Subroutine ban: called CC=10 SCAR=32.6
Pod documented
363  
364 5 $self->errorblank;
365  
366 5 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 5 5
367 0 $self->{error} = 1;
368 0 $self->{errorString} = 'backend has not been inited';
369 0 $self->warn;
370 0 return;
371 }
372  
373 5 ▶ if ( !defined( $opts{ban} ) ) {
Branch: 4 branches
BranchTrueFalseTotal
if (not defined $opts{'ban'}) { } 0 5 5
elsif (ref $opts{'ban'} ne '') { } 0 5 5
elsif ($opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::nsupdate::IPv6_re\z/) { } 1 4 5
elsif (not $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::nsupdate::IPv4_re\z/) { } 0 4 4
374 0 $self->{error} = 9;
375 0 $self->{errorString} = 'Nothing specified for the value ban';
376 0 $self->warn;
377 0 return;
378 } elsif ( ref( $opts{ban} ) ne '' ) {
379 0 $self->{error} = 10;
380 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
381 0 $self->warn;
382 0 return;
383 } elsif ( $opts{ban} =~ /\A$IPv6_re\z/ ) {
384 1 $self->{error} = 29;
385 1 $self->{errorString} = 'the nsupdate backend only handles IPv4 and "' . $opts{ban} . '" is IPv6';
386 1 $self->warn;
387 0 return;
388 } elsif ( $opts{ban} !~ /\A$IPv4_re\z/ ) {
389 0 $self->{error} = 10;
390 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 IP';
391 0 $self->warn;
392 0 return;
393 }
394  
395 4 ▶ if ( $self->{banned}{ $opts{ban} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"banned"}{$opts{"ban"}}) 1 3 4
396 1 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 1 0 1
397 1 $self->{frontend_obj}->{test_data} = 'already banned';
398 }
399 1 return;
400 }
401  
402 3 my $record = $self->_record_name( $opts{ban} );
403 my $command = $self->_nsupdate_command(
404 'prereq nxrrset ' . $record . ' TXT',
405 3 'update add ' . $record . ' ' . $self->{options}{ttl} . ' IN TXT "' . $self->{options}{rdata} . '"',
406 'send'
407 );
408  
409 3 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 3 0 3
410 3 $self->{frontend_obj}->{test_data} = [$command];
411 } else {
412 0 my $output = `$command 2>&1`;
413 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
414 0 $self->{error} = 13;
415 $self->{errorString}
416 0 = 'ban failed. non-zero exit code for the command... "' . $command . '"... output... ' . $output;
417 0 $self->warn;
418 0 return;
419 }
420 }
421  
422 3 $self->{banned}{ $opts{ban} } = 1;
423 } ## end sub ban
424  
425 =head2 unban
426  
427 Unbans an IP. The value of ban is validated as being a IPv4 address; IPv6
428 is not supported and is an error. The TXT record for the IP is deleted by
429 piping an update delete for its record name into nsupdate. Unbanning an IP
430 that is not banned is a noop.
431  
432 $backend->unban(ban => $ip);
433  
434 =cut
435  
436 sub unban {
437 1 ▶ my ( $self, %opts ) = @_;
Subroutine unban: called CC=10 SCAR=38.0
Pod documented
438  
439 1 $self->errorblank;
440  
441 1 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 1 1
442 0 $self->{error} = 1;
443 0 $self->{errorString} = 'backend has not been inited';
444 0 $self->warn;
445 0 return;
446 }
447  
448 1 ▶ if ( !defined( $opts{ban} ) ) {
Branch: 4 branches
BranchTrueFalseTotal
if (not defined $opts{'ban'}) { } 0 1 1
elsif (ref $opts{'ban'} ne '') { } 0 1 1
elsif ($opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::nsupdate::IPv6_re\z/) { } 0 1 1
elsif (not $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::nsupdate::IPv4_re\z/) { } 0 1 1
449 0 $self->{error} = 9;
450 0 $self->{errorString} = 'Nothing specified for the value ban';
451 0 $self->warn;
452 0 return;
453 } elsif ( ref( $opts{ban} ) ne '' ) {
454 0 $self->{error} = 10;
455 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
456 0 $self->warn;
457 0 return;
458 } elsif ( $opts{ban} =~ /\A$IPv6_re\z/ ) {
459 0 $self->{error} = 29;
460 0 $self->{errorString} = 'the nsupdate backend only handles IPv4 and "' . $opts{ban} . '" is IPv6';
461 0 $self->warn;
462 0 return;
463 } elsif ( $opts{ban} !~ /\A$IPv4_re\z/ ) {
464 0 $self->{error} = 10;
465 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 IP';
466 0 $self->warn;
467 0 return;
468 }
469  
470 1 ▶ if ( !$self->{banned}{ $opts{ban} } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"banned"}{$opts{"ban"}}) 0 1 1
471 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
472 0 $self->{frontend_obj}->{test_data} = 'not banned';
473 }
474 0 return;
475 }
476  
477 1 my $record = $self->_record_name( $opts{ban} );
478 1 my $command = $self->_nsupdate_command( 'update delete ' . $record . ' TXT', 'send' );
479  
480 1 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 1 0 1
481 1 $self->{frontend_obj}->{test_data} = $command;
482 } else {
483 0 my $output = `$command 2>&1`;
484 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
485 0 $self->{error} = 14;
486 $self->{errorString}
487 0 = 'unban failed. non-zero exit code for the command... "' . $command . '"... output... ' . $output;
488 0 $self->warn;
489 0 return;
490 }
491 }
492  
493 1 delete( $self->{banned}{ $opts{ban} } );
494 } ## end sub unban
495  
496 =head2 ban_cidr
497  
498 CIDR bans are not supported by this backend; this always sets the
499 cidrNotSupported error.
500  
501 $backend->ban_cidr(ban => '1.2.3.0/24');
502  
503 =cut
504  
505 sub ban_cidr {
506 0 ▶ my ( $self, %opts ) = @_;
Subroutine ban_cidr: not called CC=1 SCAR=6.9
Pod documented
507  
508 0 $self->errorblank;
509  
510 0 $self->{error} = 33;
511 0 $self->{errorString} = 'the ' . __PACKAGE__ . ' backend does not support CIDR bans';
512 0 $self->warn;
513  
514 0 return;
515 } ## end sub ban_cidr
516  
517 =head2 unban_cidr
518  
519 CIDR bans are not supported by this backend; this always sets the
520 cidrNotSupported error.
521  
522 $backend->unban_cidr(ban => '1.2.3.0/24');
523  
524 =cut
525  
526 sub unban_cidr {
527 0 ▶ my ( $self, %opts ) = @_;
Subroutine unban_cidr: not called CC=1 SCAR=6.9
Pod documented
528  
529 0 $self->errorblank;
530  
531 0 $self->{error} = 33;
532 0 $self->{errorString} = 'the ' . __PACKAGE__ . ' backend does not support CIDR bans';
533 0 $self->warn;
534  
535 0 return;
536 } ## end sub unban_cidr
537  
538 =head2 list_cidr
539  
540 CIDR bans are not supported by this backend, so this always returns an empty
541 list.
542  
543 my @banned_cidrs = $backend->list_cidr;
544  
545 =cut
546  
547 sub list_cidr {
548 0 ▶ my ( $self, %opts ) = @_;
Subroutine list_cidr: not called CC=1 SCAR=6.9
Pod documented
549  
550 0 $self->errorblank;
551  
552 0 return ();
553 }
554  
555 =head2 list
556  
557 List banned IPs. Returns an array of the currently banned IPs from the
558 internal ban list.
559  
560 my @banned = $backend->list;
561  
562 =cut
563  
564 sub list {
565 2 ▶ my ( $self, %opts ) = @_;
Subroutine list: called CC=2 SCAR=7.0
Pod documented
566  
567 2 $self->errorblank;
568  
569 2 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 2 0 2
570 2 $self->{frontend_obj}->{test_data} = 'list';
571 }
572  
573 2 return keys( %{ $self->{banned} } );
574 }
575  
576 =head2 re_init
577  
578 Tells the backend to re-init it's self.
579  
580 This will call teardown, which deletes the record for each banned IP, then
581 init, and then re-add the TXT record for every retained ban via nsupdate.
582  
583 $backend->re_init;
584  
585 =cut
586  
587 sub re_init {
588 1 ▶ my ( $self, %opts ) = @_;
Subroutine re_init: called CC=6 SCAR=20.9
Pod documented
589  
590 1 $self->errorblank;
591  
592 1 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 1 1
593 0 $self->{error} = 1;
594 0 $self->{errorString} = 'backend has not been inited';
595 0 $self->warn;
596 0 return;
597 }
598  
599 # teardown is best effort here as a partially or fully wiped setup is
600 # exactly what re_init needs to recover from; init cleans up any remnants
601 {
602 1 local $@;
603 1 eval { $self->teardown; };
604 }
605 1 $self->init;
606  
607 1 my @to_ban = keys( %{ $self->{banned} } );
608  
609 1 my @re_init_test_data;
610 1 foreach my $item (@to_ban) {
611 1 my $record = $self->_record_name($item);
612 my $command = $self->_nsupdate_command(
613 'prereq nxrrset ' . $record . ' TXT',
614 1 'update add ' . $record . ' ' . $self->{options}{ttl} . ' IN TXT "' . $self->{options}{rdata} . '"',
615 'send'
616 );
617  
618 1 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 1 0 1
619 1 push( @re_init_test_data, $command );
620 } else {
621 0 my $output = `$command 2>&1`;
622 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
623 0 $self->{error} = 13;
624 $self->{errorString}
625 0 = 'ban failed. non-zero exit code for the command... "' . $command . '"... output... ' . $output;
626 0 $self->warn;
627 }
628 }
629 } ## end foreach my $item (@to_ban)
630  
631 1 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 1 0 1
632 1 $self->{frontend_obj}->{test_data} = \@re_init_test_data;
633 }
634  
635 1 $self->{inited} = 1;
636 } ## end sub re_init
637  
638 =head2 teardown
639  
640 Tears down the setup for the backend by deleting the record for each
641 currently banned IP. The internal list of bans is kept, so a following
642 re_init will re-add them.
643  
644 $backend->teardown;
645  
646 =cut
647  
648 sub teardown {
649 2 ▶ my ( $self, %opts ) = @_;
Subroutine teardown: called CC=5 SCAR=20.4
Pod documented
650  
651 2 $self->errorblank;
652  
653 2 $self->{inited} = 0;
654  
655 2 my @commands;
656 2 foreach my $item ( sort( keys( %{ $self->{banned} } ) ) ) {
657 2 push( @commands, $self->_nsupdate_command( 'update delete ' . $self->_record_name($item) . ' TXT', 'send' ) );
658 }
659  
660 2 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 2 0 2
661 2 $self->{frontend_obj}->{test_data} = \@commands;
662 } else {
663 0 foreach my $item (@commands) {
664 0 my $output = `$item 2>&1`;
665 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
666 0 $self->{error} = 17;
667 $self->{errorString}
668 0 = 'teardown failed. non-zero exit code for the command... "' . $item . '"... output... ' . $output;
669 0 $self->warn;
670 }
671 }
672 } ## end else [ if ( $self->{testing} ) ]
673 } ## end sub teardown
674  
675 =head2 stop
676  
677 Alias for L</teardown>, provided for parity with the fail2ban C<actionstop>
678 concept.
679  
680 $backend->stop;
681  
682 =cut
683  
684 sub stop {
685 0 ▶ my ( $self, %opts ) = @_;
Subroutine stop: not called CC=1 SCAR=6.9
Pod documented
686  
687 0 return $self->teardown(%opts);
688 }
689  
690 =head2 check
691  
692 There is nothing this backend can cheaply probe, so this always reports
693 healthy. This is the equivalent of fail2ban's C<actioncheck>.
694  
695 my $healthy = $backend->check;
696  
697 =cut
698  
699 sub check {
700 7 ▶ my ( $self, %opts ) = @_;
Subroutine check: called CC=2 SCAR=7.0
Pod documented
701  
702 7 $self->errorblank;
703  
704 7 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 7 0 7
705 7 $self->{frontend_obj}->{test_data} = 'check';
706 }
707  
708 7 return 1;
709 } ## end sub check
710  
711 =head2 flush
712  
713 Removes all currently banned IPs at once by deleting their records and
714 forgetting them. This is the equivalent of fail2ban's C<actionflush>.
715  
716 $backend->flush;
717  
718 =cut
719  
720 sub flush {
721 1 ▶ my ( $self, %opts ) = @_;
Subroutine flush: called CC=6 SCAR=24.5
Pod documented
722  
723 1 $self->errorblank;
724  
725 1 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 1 1
726 0 $self->{error} = 1;
727 0 $self->{errorString} = 'backend has not been inited';
728 0 $self->warn;
729 0 return;
730 }
731  
732 1 my @commands;
733 1 foreach my $item ( sort( keys( %{ $self->{banned} } ) ) ) {
734 1 push( @commands, $self->_nsupdate_command( 'update delete ' . $self->_record_name($item) . ' TXT', 'send' ) );
735 }
736  
737 1 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 1 0 1
738 1 $self->{frontend_obj}->{test_data} = \@commands;
739 } else {
740 0 foreach my $item (@commands) {
741 0 my $output = `$item 2>&1`;
742 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
743 0 $self->{error} = 25;
744 $self->{errorString}
745 0 = 'flush failed. non-zero exit code for the command... "' . $item . '"... output... ' . $output;
746 0 $self->warn;
747 }
748 }
749 } ## end else [ if ( $self->{testing} ) ]
750  
751 1 $self->{banned} = {};
752 } ## end sub flush
753  
754 =head1 ERROR CODES / FLAGS
755  
756 Error handling is provided by L<Error::Helper>. All
757 errors are considered fatal.
758  
759 =head2 1, notInited
760  
761 The backend has not been inited yet.
762  
763 =head2 6, invalidPrefixSpecified
764  
765 The specified prefix did not match /^[a-zA-Z0-9]+$/.
766  
767 =head2 7, invalidName
768  
769 The name is either undef or does not match /^[a-zA-Z0-9\-]+$/.
770  
771 =head2 8, optionsNotHash
772  
773 The item passed to new for options is not a hash.
774  
775 =head2 9, noBanItem
776  
777 No IP specified to ban or unban.
778  
779 =head2 10, banItemNotIP
780  
781 The item to ban is not an IP. Either wrong ref type or regexp
782 test using L<Regexp::IPv4> and L<Regexp::IPv6> failed.
783  
784 =head2 12, backendInitError
785  
786 Failed to init the backend.
787  
788 =head2 13, banFailed
789  
790 Failed to ban the item.
791  
792 =head2 14, unbanFailed
793  
794 Failed to unban the item.
795  
796 =head2 15, listFailed
797  
798 Failed to get a list of bans.
799  
800 =head2 16, reInitFailed
801  
802 Failed to re_init the backend.
803  
804 =head2 17, teardownFailed
805  
806 Failed to teardown the backend.
807  
808 =head2 18, alreadyInited
809  
810 init called, but the backend has already been inited.
811  
812 =head2 23, initFailed
813  
814 init failed. The keyfile does not exist or is not a file.
815  
816 =head2 24, checkFailed
817  
818 The backend check raised an error.
819  
820 =head2 25, flushFailed
821  
822 Failed to flush the bans.
823  
824 =head2 26, portsNotSupported
825  
826 Ports were specified, but the backend does not support ports.
827  
828 =head2 27, protocolsNotSupported
829  
830 Protocols were specified, but the backend does not support protocols.
831  
832 =head2 28, optionInvalid
833  
834 One of the specified options failed validation.
835  
836 =head2 29, ipv6NotSupported
837  
838 The backend only supports IPv4 and the item is an IPv6 IP.
839  
840 =head2 30, banCidrFailed
841  
842 Failed to ban the CIDR range.
843  
844 =head2 31, unbanCidrFailed
845  
846 Failed to unban the CIDR range.
847  
848 =head2 32, cidrItemNotCidr
849  
850 The item to ban is not a CIDR range. Either wrong ref type or it is not an
851 IPv4 or IPv6 address followed by a prefix length valid for its family.
852  
853 =head2 33, cidrNotSupported
854  
855 The backend does not support CIDR bans.
856  
857 =head2 34, listCidrFailed
858  
859 Failed to get a list of CIDR bans.
860  
861 =head1 AUTHOR
862  
863 Zane C. Bowers-Hadley, C<< <vvelox at vvelox.ent> >>
864  
865 =head1 BUGS
866  
867 Please report any bugs or feature requests to C<bug-net-firewall-blockerhelper at rt.cpan.org>, or through
868 the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Net-Firewall-BlockerHelper>.
869  
870 =head1 LICENSE AND COPYRIGHT
871  
872 This software is Copyright (c) 2023 by Zane C. Bowers-Hadley.
873  
874 This is free software, licensed under:
875  
876 The GNU Lesser General Public License, Version 2.1, February 1999
877  
878  
879 =cut
880  
881 1 1; # End of Net::Firewall::BlockerHelper
« BlockerHelper/backends/routeros.pm ↑ Summary BlockerHelper/backends/dns_rpz.pm »