BlockerHelper/backends/nsupdate.pm

filter: Statementstmt 52.4% 141 / 269 Branchbran 53.9% 55 / 102 Conditioncond 71.4% 15 / 21 MC/DCmcdc 33.3% 4 / 12 Subroutinesub 80.9% 17 / 21 Podpod 100% 13 / 13 totaltotal 55.9% 245 / 438 CC 64 SCAR 35.3
CC
64
Cov
54%
new
70.0
unban
44.6
ban
26.1
CRAP 34.0
« BlockerHelper/backends/bgp_rtbh.pm ↑ Summary BlockerHelper/backends/routeros.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.2.0
17  
18 =cut
19  
20 1 our $VERSION = '0.2.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 6 ▶sub new {
Subroutine new: called CC=28 SCAR=42.5
Pod documented
115 6 my ( $blank, %opts ) = @_;
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 DNS name that the blocklist record for the
292 # passed IP lives at, which is the address written backwards under the
293 # configured domain.
294 #
295 # This is the DNSBL naming convention: the octets are reversed so that the
296 # name reads from most specific to least specific the way DNS names do, then
297 # placed under the zone. A consumer checking whether an address is listed
298 # builds the same name and looks it up.
299 #
300 # Only IPv4 is handled. This backend rejects IPv6 at ban and unban with
301 # ipv6NotSupported, matching the fail2ban action it mirrors, so nothing ever
302 # reaches here with an IPv6 address. Passing one anyway would not error, it
303 # would just split on dots that are not there and produce a single label,
304 # giving a nonsense name.
305 #
306 # Args:
307 #
308 # ip - The address to encode, as a plain string. Expected to be an already
309 # validated IPv4 address such as "10.0.0.1". Nothing here validates
310 # it.
311 #
312 # Returns the fully qualified record name as a string, with no trailing dot.
313 #
314 # # with the domain option set to rbl.foo.bar
315 # $self->_record_name('10.0.0.1'); # 1.0.0.10.rbl.foo.bar
316 # $self->_record_name('192.0.2.5'); # 5.2.0.192.rbl.foo.bar
317 8 ▶sub _record_name {
Subroutine _record_name: called CC=1 SCAR=0.0
318 8 my ( $self, $ip ) = @_;
319  
320 8 my @octets = split( /\./, $ip );
321  
322 8 return join( '.', reverse(@octets) ) . '.' . $self->{options}{domain};
323 }
324  
325 # Internal helper. Wraps a list of nsupdate statements into the complete shell
326 # command that feeds them to nsupdate. Every change this backend makes to the
327 # zone goes out through here.
328 #
329 # nsupdate reads its statements from stdin rather than from arguments, so they
330 # are printed into a pipe. Note the statements are joined with a literal
331 # backslash-n inside a single quoted printf format, so it is printf that turns
332 # them into real newlines, not perl.
333 #
334 # Unlike the same named helper in the dns_rpz backend, this one adds nothing
335 # of its own. There is no server line and no zone line, so nsupdate works out
336 # the zone from the record name and the server from its SOA, and crucially the
337 # trailing send is not appended either. Callers pass the complete statement
338 # list including their own 'send'; leaving it off would build a command that
339 # connects and then exits without transmitting anything.
340 #
341 # Args:
342 #
343 # @statements - The complete list of nsupdate statement lines, as plain
344 # strings with no trailing newline, in the order they should
345 # be fed in, and including the final 'send'. Callers
346 # typically pass either a prereq, an update add, and a send
347 # for a ban, or an update delete and a send for an unban.
348 #
349 # Returns the command as a single string ready to hand to the runner, of the
350 # shape "printf '<statements>\n' | <nsupdate> -k '<keyfile>'". The nsupdate
351 # binary and the keyfile come from the options, and the keyfile is single
352 # quoted so a path holding spaces survives.
353 #
354 # # a ban: only add the record if it is not already there
355 # $self->_nsupdate_command(
356 # 'prereq nxrrset 1.0.0.10.rbl.foo.bar TXT',
357 # 'update add 1.0.0.10.rbl.foo.bar 60 IN TXT "blocked"',
358 # 'send'
359 # );
360 # # printf 'prereq nxrrset 1.0.0.10.rbl.foo.bar TXT\nupdate add 1.0.0.10.rbl.foo.bar 60 IN TXT "blocked"\nsend\n' | nsupdate -k '/etc/nsupdate.key'
361 #
362 # # an unban
363 # $self->_nsupdate_command( 'update delete 1.0.0.10.rbl.foo.bar TXT', 'send' );
364 # # printf 'update delete 1.0.0.10.rbl.foo.bar TXT\nsend\n' | nsupdate -k '/etc/nsupdate.key'
365 8 ▶sub _nsupdate_command {
Subroutine _nsupdate_command: called CC=1 SCAR=0.0
366 8 my ( $self, @statements ) = @_;
367  
368 return
369 "printf '"
370 . join( '\n', @statements )
371 . "\\n' | "
372 . $self->{options}{nsupdate} . " -k '"
373 8 . $self->{options}{keyfile} . "'";
374 } ## end sub _nsupdate_command
375  
376 =head2 init
377  
378 Initiates the backend. Verifies the keyfile exists.
379  
380 Note that the zone must allow updates via the key and something must be
381 consuming the blocklist for bans to have an effect. See L</SYNOPSIS>.
382  
383 $backend->init;
384  
385 =cut
386  
387 5 ▶sub init {
Subroutine init: called CC=4 SCAR=19.1
Pod documented
388 5 my ( $self, %opts ) = @_;
389  
390 5 $self->errorblank;
391  
392 5 ▶ if ( $self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"inited"}) 0 5 5
393 0 $self->{error} = 18;
394 0 $self->{errorString} = 'backend has already been inited';
395 0 $self->warn;
396 }
397  
398 5 ▶ if ( $self->{testing} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'testing'}) { } 5 0 5
elsif (not -f $self->{'options'}{'keyfile'}) { } 0 0 0
399 5 $self->{frontend_obj}->{test_data} = 'inited';
400 } elsif ( !-f $self->{options}{keyfile} ) {
401 0 $self->{error} = 23;
402 $self->{errorString}
403 0 = 'init failed. the keyfile, "' . $self->{options}{keyfile} . '", does not exist or is not a file';
404 0 $self->warn;
405 }
406  
407 5 $self->{inited} = 1;
408 } ## end sub init
409  
410 =head2 ban
411  
412 Bans an IP. The value of ban is validated as being a IPv4 address; IPv6 is
413 not supported and is an error. The octets are reversed and a TXT record is
414 added at that name under the domain, eg banning 1.2.3.4 adds a TXT record
415 at C<4.3.2.1.E<lt>domainE<gt>>, by piping an update with a nxrrset prereq
416 into nsupdate authenticated via the TSIG keyfile. Banning an already banned
417 IP is a noop.
418  
419 $backend->ban(ban => $ip);
420  
421 =cut
422  
423 5 ▶sub ban {
Subroutine ban: called CC=10 SCAR=32.6
Pod documented
424 5 my ( $self, %opts ) = @_;
425  
426 5 $self->errorblank;
427  
428 5 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 5 5
429 0 $self->{error} = 1;
430 0 $self->{errorString} = 'backend has not been inited';
431 0 $self->warn;
432 0 return;
433 }
434  
435 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
436 0 $self->{error} = 9;
437 0 $self->{errorString} = 'Nothing specified for the value ban';
438 0 $self->warn;
439 0 return;
440 } elsif ( ref( $opts{ban} ) ne '' ) {
441 0 $self->{error} = 10;
442 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
443 0 $self->warn;
444 0 return;
445 } elsif ( $opts{ban} =~ /\A$IPv6_re\z/ ) {
446 1 $self->{error} = 29;
447 1 $self->{errorString} = 'the nsupdate backend only handles IPv4 and "' . $opts{ban} . '" is IPv6';
448 1 $self->warn;
449 0 return;
450 } elsif ( $opts{ban} !~ /\A$IPv4_re\z/ ) {
451 0 $self->{error} = 10;
452 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 IP';
453 0 $self->warn;
454 0 return;
455 }
456  
457 4 ▶ if ( $self->{banned}{ $opts{ban} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"banned"}{$opts{"ban"}}) 1 3 4
458 1 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 1 0 1
459 1 $self->{frontend_obj}->{test_data} = 'already banned';
460 }
461 1 return;
462 }
463  
464 3 my $record = $self->_record_name( $opts{ban} );
465 my $command = $self->_nsupdate_command(
466 'prereq nxrrset ' . $record . ' TXT',
467 3 'update add ' . $record . ' ' . $self->{options}{ttl} . ' IN TXT "' . $self->{options}{rdata} . '"',
468 'send'
469 );
470  
471 3 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 3 0 3
472 3 $self->{frontend_obj}->{test_data} = [$command];
473 } else {
474 0 my $output = `$command 2>&1`;
475 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
476 0 $self->{error} = 13;
477 $self->{errorString}
478 0 = 'ban failed. non-zero exit code for the command... "' . $command . '"... output... ' . $output;
479 0 $self->warn;
480 0 return;
481 }
482 }
483  
484 3 $self->{banned}{ $opts{ban} } = 1;
485 } ## end sub ban
486  
487 =head2 unban
488  
489 Unbans an IP. The value of ban is validated as being a IPv4 address; IPv6
490 is not supported and is an error. The TXT record for the IP is deleted by
491 piping an update delete for its record name into nsupdate. Unbanning an IP
492 that is not banned is a noop.
493  
494 $backend->unban(ban => $ip);
495  
496 =cut
497  
498 1 ▶sub unban {
Subroutine unban: called CC=10 SCAR=38.0
Pod documented
499 1 my ( $self, %opts ) = @_;
500  
501 1 $self->errorblank;
502  
503 1 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 1 1
504 0 $self->{error} = 1;
505 0 $self->{errorString} = 'backend has not been inited';
506 0 $self->warn;
507 0 return;
508 }
509  
510 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
511 0 $self->{error} = 9;
512 0 $self->{errorString} = 'Nothing specified for the value ban';
513 0 $self->warn;
514 0 return;
515 } elsif ( ref( $opts{ban} ) ne '' ) {
516 0 $self->{error} = 10;
517 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
518 0 $self->warn;
519 0 return;
520 } elsif ( $opts{ban} =~ /\A$IPv6_re\z/ ) {
521 0 $self->{error} = 29;
522 0 $self->{errorString} = 'the nsupdate backend only handles IPv4 and "' . $opts{ban} . '" is IPv6';
523 0 $self->warn;
524 0 return;
525 } elsif ( $opts{ban} !~ /\A$IPv4_re\z/ ) {
526 0 $self->{error} = 10;
527 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 IP';
528 0 $self->warn;
529 0 return;
530 }
531  
532 1 ▶ if ( !$self->{banned}{ $opts{ban} } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"banned"}{$opts{"ban"}}) 0 1 1
533 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
534 0 $self->{frontend_obj}->{test_data} = 'not banned';
535 }
536 0 return;
537 }
538  
539 1 my $record = $self->_record_name( $opts{ban} );
540 1 my $command = $self->_nsupdate_command( 'update delete ' . $record . ' TXT', 'send' );
541  
542 1 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 1 0 1
543 1 $self->{frontend_obj}->{test_data} = $command;
544 } else {
545 0 my $output = `$command 2>&1`;
546 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
547 0 $self->{error} = 14;
548 $self->{errorString}
549 0 = 'unban failed. non-zero exit code for the command... "' . $command . '"... output... ' . $output;
550 0 $self->warn;
551 0 return;
552 }
553 }
554  
555 1 delete( $self->{banned}{ $opts{ban} } );
556 } ## end sub unban
557  
558 =head2 ban_cidr
559  
560 CIDR bans are not supported by this backend; this always sets the
561 cidrNotSupported error.
562  
563 $backend->ban_cidr(ban => '1.2.3.0/24');
564  
565 =cut
566  
567 0 ▶sub ban_cidr {
Subroutine ban_cidr: not called CC=1 SCAR=6.9
Pod documented
568 0 my ( $self, %opts ) = @_;
569  
570 0 $self->errorblank;
571  
572 0 $self->{error} = 33;
573 0 $self->{errorString} = 'the ' . __PACKAGE__ . ' backend does not support CIDR bans';
574 0 $self->warn;
575  
576 0 return;
577 } ## end sub ban_cidr
578  
579 =head2 unban_cidr
580  
581 CIDR bans are not supported by this backend; this always sets the
582 cidrNotSupported error.
583  
584 $backend->unban_cidr(ban => '1.2.3.0/24');
585  
586 =cut
587  
588 0 ▶sub unban_cidr {
Subroutine unban_cidr: not called CC=1 SCAR=6.9
Pod documented
589 0 my ( $self, %opts ) = @_;
590  
591 0 $self->errorblank;
592  
593 0 $self->{error} = 33;
594 0 $self->{errorString} = 'the ' . __PACKAGE__ . ' backend does not support CIDR bans';
595 0 $self->warn;
596  
597 0 return;
598 } ## end sub unban_cidr
599  
600 =head2 list_cidr
601  
602 CIDR bans are not supported by this backend, so this always returns an empty
603 list.
604  
605 my @banned_cidrs = $backend->list_cidr;
606  
607 =cut
608  
609 0 ▶sub list_cidr {
Subroutine list_cidr: not called CC=1 SCAR=6.9
Pod documented
610 0 my ( $self, %opts ) = @_;
611  
612 0 $self->errorblank;
613  
614 0 return ();
615 }
616  
617 =head2 list
618  
619 List banned IPs. Returns an array of the currently banned IPs from the
620 internal ban list.
621  
622 my @banned = $backend->list;
623  
624 =cut
625  
626 2 ▶sub list {
Subroutine list: called CC=2 SCAR=7.0
Pod documented
627 2 my ( $self, %opts ) = @_;
628  
629 2 $self->errorblank;
630  
631 2 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 2 0 2
632 2 $self->{frontend_obj}->{test_data} = 'list';
633 }
634  
635 2 return keys( %{ $self->{banned} } );
636 }
637  
638 =head2 re_init
639  
640 Tells the backend to re-init it's self.
641  
642 This will call teardown, which deletes the record for each banned IP, then
643 init, and then re-add the TXT record for every retained ban via nsupdate.
644  
645 $backend->re_init;
646  
647 =cut
648  
649 1 ▶sub re_init {
Subroutine re_init: called CC=5 SCAR=17.5
Pod documented
650 1 my ( $self, %opts ) = @_;
651  
652 1 $self->errorblank;
653  
654 # teardown is best effort here as a partially or fully wiped setup is
655 # exactly what re_init needs to recover from; init cleans up any remnants
656 {
657 1 local $@;
658 1 eval { $self->teardown; };
659 }
660 1 $self->init;
661  
662 1 my @to_ban = keys( %{ $self->{banned} } );
663  
664 1 my @re_init_test_data;
665 1 foreach my $item (@to_ban) {
666 1 my $record = $self->_record_name($item);
667 my $command = $self->_nsupdate_command(
668 'prereq nxrrset ' . $record . ' TXT',
669 1 'update add ' . $record . ' ' . $self->{options}{ttl} . ' IN TXT "' . $self->{options}{rdata} . '"',
670 'send'
671 );
672  
673 1 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 1 0 1
674 1 push( @re_init_test_data, $command );
675 } else {
676 0 my $output = `$command 2>&1`;
677 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
678 0 $self->{error} = 13;
679 $self->{errorString}
680 0 = 'ban failed. non-zero exit code for the command... "' . $command . '"... output... ' . $output;
681 0 $self->warn;
682 }
683 }
684 } ## end foreach my $item (@to_ban)
685  
686 1 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 1 0 1
687 1 $self->{frontend_obj}->{test_data} = \@re_init_test_data;
688 }
689  
690 1 $self->{inited} = 1;
691 } ## end sub re_init
692  
693 =head2 teardown
694  
695 Tears down the setup for the backend by deleting the record for each
696 currently banned IP. The internal list of bans is kept, so a following
697 re_init will re-add them.
698  
699 $backend->teardown;
700  
701 =cut
702  
703 2 ▶sub teardown {
Subroutine teardown: called CC=5 SCAR=20.4
Pod documented
704 2 my ( $self, %opts ) = @_;
705  
706 2 $self->errorblank;
707  
708 2 $self->{inited} = 0;
709  
710 2 my @commands;
711 2 foreach my $item ( sort( keys( %{ $self->{banned} } ) ) ) {
712 2 push( @commands, $self->_nsupdate_command( 'update delete ' . $self->_record_name($item) . ' TXT', 'send' ) );
713 }
714  
715 2 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 2 0 2
716 2 $self->{frontend_obj}->{test_data} = \@commands;
717 } else {
718 0 foreach my $item (@commands) {
719 0 my $output = `$item 2>&1`;
720 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
721 0 $self->{error} = 17;
722 $self->{errorString}
723 0 = 'teardown failed. non-zero exit code for the command... "' . $item . '"... output... ' . $output;
724 0 $self->warn;
725 }
726 }
727 } ## end else [ if ( $self->{testing} ) ]
728 } ## end sub teardown
729  
730 =head2 stop
731  
732 Alias for L</teardown>, provided for parity with the fail2ban C<actionstop>
733 concept.
734  
735 $backend->stop;
736  
737 =cut
738  
739 0 ▶sub stop {
Subroutine stop: not called CC=1 SCAR=6.9
Pod documented
740 0 my ( $self, %opts ) = @_;
741  
742 0 return $self->teardown(%opts);
743 }
744  
745 =head2 check
746  
747 There is nothing this backend can cheaply probe, so this always reports
748 healthy. This is the equivalent of fail2ban's C<actioncheck>.
749  
750 my $healthy = $backend->check;
751  
752 =cut
753  
754 7 ▶sub check {
Subroutine check: called CC=2 SCAR=7.0
Pod documented
755 7 my ( $self, %opts ) = @_;
756  
757 7 $self->errorblank;
758  
759 7 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 7 0 7
760 7 $self->{frontend_obj}->{test_data} = 'check';
761 }
762  
763 7 return 1;
764 } ## end sub check
765  
766 =head2 flush
767  
768 Removes all currently banned IPs at once by deleting their records and
769 forgetting them. This is the equivalent of fail2ban's C<actionflush>.
770  
771 $backend->flush;
772  
773 =cut
774  
775 1 ▶sub flush {
Subroutine flush: called CC=6 SCAR=24.5
Pod documented
776 1 my ( $self, %opts ) = @_;
777  
778 1 $self->errorblank;
779  
780 1 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 1 1
781 0 $self->{error} = 1;
782 0 $self->{errorString} = 'backend has not been inited';
783 0 $self->warn;
784 0 return;
785 }
786  
787 1 my @commands;
788 1 foreach my $item ( sort( keys( %{ $self->{banned} } ) ) ) {
789 1 push( @commands, $self->_nsupdate_command( 'update delete ' . $self->_record_name($item) . ' TXT', 'send' ) );
790 }
791  
792 1 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 1 0 1
793 1 $self->{frontend_obj}->{test_data} = \@commands;
794 } else {
795 0 foreach my $item (@commands) {
796 0 my $output = `$item 2>&1`;
797 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
798 0 $self->{error} = 25;
799 $self->{errorString}
800 0 = 'flush failed. non-zero exit code for the command... "' . $item . '"... output... ' . $output;
801 0 $self->warn;
802 }
803 }
804 } ## end else [ if ( $self->{testing} ) ]
805  
806 1 $self->{banned} = {};
807 } ## end sub flush
808  
809 =head1 ERROR CODES / FLAGS
810  
811 Error handling is provided by L<Error::Helper>. All
812 errors are considered fatal.
813  
814 =head2 1, notInited
815  
816 The backend has not been inited yet.
817  
818 =head2 6, invalidPrefixSpecified
819  
820 The specified prefix did not match /^[a-zA-Z0-9]+$/.
821  
822 =head2 7, invalidName
823  
824 The name is either undef or does not match /^[a-zA-Z0-9\-]+$/.
825  
826 =head2 8, optionsNotHash
827  
828 The item passed to new for options is not a hash.
829  
830 =head2 9, noBanItem
831  
832 No IP specified to ban or unban.
833  
834 =head2 10, banItemNotIP
835  
836 The item to ban is not an IP. Either wrong ref type or regexp
837 test using L<Regexp::IPv4> and L<Regexp::IPv6> failed.
838  
839 =head2 12, backendInitError
840  
841 Failed to init the backend.
842  
843 =head2 13, banFailed
844  
845 Failed to ban the item.
846  
847 =head2 14, unbanFailed
848  
849 Failed to unban the item.
850  
851 =head2 15, listFailed
852  
853 Failed to get a list of bans.
854  
855 =head2 16, reInitFailed
856  
857 Failed to re_init the backend.
858  
859 =head2 17, teardownFailed
860  
861 Failed to teardown the backend.
862  
863 =head2 18, alreadyInited
864  
865 init called, but the backend has already been inited.
866  
867 =head2 23, initFailed
868  
869 init failed. The keyfile does not exist or is not a file.
870  
871 =head2 24, checkFailed
872  
873 The backend check raised an error.
874  
875 =head2 25, flushFailed
876  
877 Failed to flush the bans.
878  
879 =head2 26, portsNotSupported
880  
881 Ports were specified, but the backend does not support ports.
882  
883 =head2 27, protocolsNotSupported
884  
885 Protocols were specified, but the backend does not support protocols.
886  
887 =head2 28, optionInvalid
888  
889 One of the specified options failed validation.
890  
891 =head2 29, ipv6NotSupported
892  
893 The backend only supports IPv4 and the item is an IPv6 IP.
894  
895 =head2 30, banCidrFailed
896  
897 Failed to ban the CIDR range.
898  
899 =head2 31, unbanCidrFailed
900  
901 Failed to unban the CIDR range.
902  
903 =head2 32, cidrItemNotCidr
904  
905 The item to ban is not a CIDR range. Either wrong ref type or it is not an
906 IPv4 or IPv6 address followed by a prefix length valid for its family.
907  
908 =head2 33, cidrNotSupported
909  
910 The backend does not support CIDR bans.
911  
912 =head2 34, listCidrFailed
913  
914 Failed to get a list of CIDR bans.
915  
916 =head1 AUTHOR
917  
918 Zane C. Bowers-Hadley, C<< <vvelox at vvelox.ent> >>
919  
920 =head1 BUGS
921  
922 Please report any bugs or feature requests to C<bug-net-firewall-blockerhelper at rt.cpan.org>, or through
923 the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Net-Firewall-BlockerHelper>.
924  
925 =head1 LICENSE AND COPYRIGHT
926  
927 This software is Copyright (c) 2023 by Zane C. Bowers-Hadley.
928  
929 This is free software, licensed under:
930  
931 The GNU Lesser General Public License, Version 2.1, February 1999
932  
933  
934 =cut
935  
936 1 1; # End of Net::Firewall::BlockerHelper
« BlockerHelper/backends/bgp_rtbh.pm ↑ Summary BlockerHelper/backends/routeros.pm »