BlockerHelper/backends/routeros.pm

filter: Statementstmt 39.7% 134 / 337 Branchbran 36.8% 56 / 152 Conditioncond 45.8% 11 / 24 MC/DCmcdc 12.5% 2 / 16 Subroutinesub 77.2% 17 / 22 Podpod 100% 13 / 13 totaltotal 41.3% 233 / 564 CC 90 SCAR 35.5
CC
90
Cov
39%
re_init
132.0
unban
45.0
unban_cidr
38.0
CRAP 34.9
« BlockerHelper/backends/bgp_rtbh.pm ↑ Summary BlockerHelper/backends/nsupdate.pm »
1 package Net::Firewall::BlockerHelper::backends::routeros;
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::routeros - MikroTik RouterOS 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 use Net::Firewall::BlockerHelper;
25  
26 my $fw_helper;
27 eval {
28 $fw_helper = Net::Firewall::BlockerHelper->new(
29 backend=>'routeros',
30 name=>'ssh',
31 options=>{
32 host => '192.0.2.1',
33 user => 'admin',
34 identity => '/home/user/.ssh/id_ed25519',
35 },
36 );
37 };
38 if ($@) {
39 print 'Error: '
40 . $Error::Helper::error
41 . "\nError String: "
42 . $Error::Helper::errorString
43 . "\nError Flag: "
44 . $Error::Helper::errorFlag . "\n";
45 }
46  
47 $fw_helper->init_backend;
48  
49 $fw_helper->ban(ban => '5.6.7.8');
50 $fw_helper->ban(ban => '1.2.3.4');
51  
52 $fw_helper->unban(ban => '5.6.7.8');
53  
54 $fw_helper->teardown;
55  
56 =head1 DESCRIPTION
57  
58 This backend blocks IPs on a MikroTik RouterOS device by driving its CLI
59 over SSH.
60  
61 For each instance two address-lists are used, one for IPv4 (default
62 C<< <prefix>_<name> >> under the C</ip firewall> menu) and one for IPv6
63 (default C<< <prefix>_<name> >> under the C</ipv6 firewall> menu). A
64 filter rule in the C<input> chain references each address-list and applies
65 the configured action. Banning an IP is then simply a matter of adding it
66 to the relevant address-list.
67  
68 The filter rules are created via a plain C<add>, which appends them to the
69 end of the C<input> chain. As RouterOS filter rules are evaluated first
70 match wins, any earlier rule accepting the traffic will win out over them,
71 so on a router with the likes of a broad accept rule in C<input> the rules
72 will need to be moved up the chain for bans to actually take effect. Also,
73 being in the C<input> chain, they only cover traffic to the router itself
74 and not traffic forwarded through it.
75  
76 Requires an SSH client in the C<PATH> of the process and credentials that
77 allow the configured user to run firewall commands on the router.
78  
79 =head1 METHODS
80  
81 =head2 new
82  
83 Initiates the the object.
84  
85 - options :: A hash of options to pass to the backend.
86 Default :: {}
87  
88 - ports :: Not used by this backend.
89 Default :: []
90  
91 - protocols :: Not used by this backend.
92 Default :: []
93  
94 - prefix :: Prefix to use.
95 - default :: kur
96  
97 - name :: Name of this specific instance.
98 - default :: undef
99  
100 The options hash accepts the following.
101  
102 - host :: Router hostname or IP. This must be specified.
103 - Default :: undef
104  
105 - user :: SSH user to connect as.
106 - Default :: admin
107  
108 - ssh_cmd :: SSH binary plus any base arguments.
109 - Default :: ssh
110  
111 - ssh_port :: Optional SSH port. When set, '-p <port>' is added.
112 - Default :: undef
113  
114 - identity :: Optional path to a SSH private key. When set, '-i <identity>'
115 is added.
116 - Default :: undef
117  
118 - list4 :: IPv4 address-list name.
119 - Default :: <prefix>_<name>
120  
121 - list6 :: IPv6 address-list name.
122 - Default :: <prefix>_<name>
123  
124 - action :: Firewall action for the drop rules.
125 - Default :: drop
126  
127 All errors are considered fatal, meaning if new fails it will die.
128  
129 my $fw_helper;
130 eval {
131 $fw_helper = Net::Firewall::BlockerHelper->new(
132 backend=>'routeros',
133 name=>'ssh',
134 options=>{ host => '192.0.2.1' },
135 );
136 };
137 if ($@) {
138 print 'Error: '
139 . $Error::Helper::error
140 . "\nError String: "
141 . $Error::Helper::errorString
142 . "\nError Flag: "
143 . $Error::Helper::errorFlag . "\n";
144 }
145  
146 =cut
147  
148 sub new {
149 2 ▶ my ( $blank, %opts ) = @_;
Subroutine new: called CC=16 SCAR=31.1
Pod documented
150  
151 2 my $self = {
152 perror => undef,
153 error => undef,
154 errorLine => undef,
155 errorFilename => undef,
156 errorString => "",
157 errorExtra => {
158 all_errors_fatal => 1,
159 # all_fatal is what Error::Helper 2.1.0 actually checks; all_errors_fatal
160 # is kept for the name documented in its POD
161 all_fatal => 1,
162 flags => {
163 1 => 'notInited',
164 8 => 'optionsNotHash',
165 9 => 'noBanItem',
166 10 => 'banItemNotIP',
167 12 => 'backendInitError',
168 13 => 'banFailed',
169 14 => 'unbanFailed',
170 15 => 'listFailed',
171 16 => 'reInitFailed',
172 17 => 'teardownFailed',
173 18 => 'alreadyInited',
174 24 => 'checkFailed',
175 25 => 'flushFailed',
176 30 => 'hostNotDefined',
177 31 => 'banCidrFailed',
178 32 => 'unbanCidrFailed',
179 33 => 'cidrItemNotCidr',
180 34 => 'cidrNotSupported',
181 35 => 'listCidrFailed',
182 },
183 fatal_flags => {},
184 perror_not_fatal => 0,
185 },
186 backend => undef,
187 options => {},
188 ports => [],
189 protocols => [],
190 testing => undef,
191 test_data => undef,
192 prefix => 'kur',
193 name => undef,
194 frontend_obj => undef,
195 inited => 0,
196 banned => {},
197 banned_cidr => {},
198 cidr_supported => 1,
199 };
200 2 bless $self;
201  
202 # used internally for testing
203 2 ▶ if ( defined( $opts{testing} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"testing"}) 2 0 2
204 2 $self->{testing} = $opts{testing};
205 }
206 2 ▶ if ( defined( $opts{frontend_obj} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"frontend_obj"}) 2 0 2
207 2 $self->{frontend_obj} = $opts{frontend_obj};
208 }
209  
210 2 ▶ if ( defined( $opts{prefix} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"prefix"}) 2 0 2
211 2 $self->{prefix} = $opts{prefix};
212 }
213  
214 2 ▶ if ( defined( $opts{name} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"name"}) 2 0 2
215 2 $self->{name} = $opts{name};
216 }
217  
218 2 ▶ if ( defined( $opts{options} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"options"}) 2 0 2
219 2 ▶ if ( ref( $opts{options} ) ne 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $opts{"options"} ne "HASH") 0 2 2
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 2 $self->{options} = $opts{options};
226 }
227  
228 # host is required
229 2 ▶ if ( !defined( $self->{options}{host} )
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->{"options"}{"host"} or $self->{"options"}{"host"} eq "") 1 1 2
Condition66%2/3
not defined $self->{"options"}{"host"} or $self->{"options"}{"host"} eq ""
l!l&&r!l&&!r
101
MC/DC50%1/2
not defined $self->{"options"}{"host"} or $self->{"options"}{"host"} eq ""
not defined $self->{"options"}{"host"} $self->{"options"}{"host"} eq ""
Truth table66%2/3
not(A) or B
A= not defined $self->{"options"}{"host"}B= $self->{"options"}{"host"} eq ""
ABresult
1X1
011
000
230 || $self->{options}{host} eq '' )
231 {
232 1 $self->{perror} = 1;
233 1 $self->{error} = 30;
234 1 $self->{errorString} = 'host is not defined or is blank';
235 1 $self->warn;
236 }
237  
238 # fill in the defaults for anything not specified
239 1 ▶ if ( !defined( $self->{options}{user} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"options"}{"user"}) 0 1 1
240 0 $self->{options}{user} = 'admin';
241 }
242 1 ▶ if ( !defined( $self->{options}{ssh_cmd} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"options"}{"ssh_cmd"}) 1 0 1
243 1 $self->{options}{ssh_cmd} = 'ssh';
244 }
245 1 ▶ if ( !defined( $self->{options}{action} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"options"}{"action"}) 1 0 1
246 1 $self->{options}{action} = 'drop';
247 }
248 1 ▶ if ( !defined( $self->{options}{list4} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"options"}{"list4"}) 1 0 1
249 1 ▶ $self->{options}{list4} = $self->{prefix} . '_' . ( defined( $self->{name} ) ? $self->{name} : '' );
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'name'} ? : 1 0 1
250 }
251 1 ▶ if ( !defined( $self->{options}{list6} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"options"}{"list6"}) 1 0 1
252 1 ▶ $self->{options}{list6} = $self->{prefix} . '_' . ( defined( $self->{name} ) ? $self->{name} : '' );
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'name'} ? : 1 0 1
253 }
254  
255 1 return $self;
256 } ## end sub new
257  
258 # Internal helper. Returns the SSH command prefix string built from the
259 # ssh_cmd, ssh_port, identity, user, and host options, e.g.
260 # "ssh -p 22 -i /key admin@host".
261 sub _ssh {
262 14 ▶ my ($self) = @_;
Subroutine _ssh: called CC=5 SCAR=19.7
263  
264 14 my $ssh = $self->{options}{ssh_cmd};
265  
266 14 ▶ if ( defined( $self->{options}{ssh_port} )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"options"}{"ssh_port"} and $self->{"options"}{"ssh_port"} ne "") 0 14 14
Condition33%1/3
defined $self->{"options"}{"ssh_port"} and $self->{"options"}{"ssh_port"} ne ""
!ll&&!rl&&r
1400
MC/DC0%0/2
defined $self->{"options"}{"ssh_port"} and $self->{"options"}{"ssh_port"} ne ""
defined $self->{"options"}{"ssh_port"} $self->{"options"}{"ssh_port"} ne ""
Truth table33%1/3
A and B
A= defined $self->{"options"}{"ssh_port"}B= $self->{"options"}{"ssh_port"} ne ""
ABresult
0X0
100
111
267 && $self->{options}{ssh_port} ne '' )
268 {
269 0 $ssh .= ' -p ' . $self->{options}{ssh_port};
270 }
271  
272 14 ▶ if ( defined( $self->{options}{identity} )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"options"}{"identity"} and $self->{"options"}{"identity"} ne "") 0 14 14
Condition33%1/3
defined $self->{"options"}{"identity"} and $self->{"options"}{"identity"} ne ""
!ll&&!rl&&r
1400
MC/DC0%0/2
defined $self->{"options"}{"identity"} and $self->{"options"}{"identity"} ne ""
defined $self->{"options"}{"identity"} $self->{"options"}{"identity"} ne ""
Truth table33%1/3
A and B
A= defined $self->{"options"}{"identity"}B= $self->{"options"}{"identity"} ne ""
ABresult
0X0
100
111
273 && $self->{options}{identity} ne '' )
274 {
275 0 $ssh .= ' -i ' . $self->{options}{identity};
276 }
277  
278 14 $ssh .= ' ' . $self->{options}{user} . '@' . $self->{options}{host};
279  
280 14 return $ssh;
281 } ## end sub _ssh
282  
283 # Internal helper. Given a RouterOS CLI line, returns the full command to
284 # run it over SSH, e.g. <_ssh> '<routeros_cli>'.
285 sub _remote {
286 14 ▶ my ( $self, $routeros_cli ) = @_;
Subroutine _remote: called CC=1 SCAR=0.0
287  
288 14 return $self->_ssh . " '" . $routeros_cli . "'";
289 }
290  
291 =head2 init
292  
293 Initiates the backend. Over SSH this runs C</ip firewall filter add> and
294 C</ipv6 firewall filter add> to create a filter rule in the C<input> chain
295 for each family, matching its address-list and applying the configured
296 action.
297  
298 Note that the rules are appended to the end of the C<input> chain, so they
299 may need moving up past any earlier accept rules to have an effect. See
300 L</DESCRIPTION>.
301  
302 No arguments are taken.
303  
304 May called a second time, it will error.
305  
306 $backend->init;
307  
308 =cut
309  
310 sub init {
311 1 ▶ my ( $self, %opts ) = @_;
Subroutine init: called CC=5 SCAR=21.4
Pod documented
312  
313 1 $self->errorblank;
314  
315 1 ▶ if ( $self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"inited"}) 0 1 1
316 0 $self->{error} = 18;
317 0 $self->{errorString} = 'backend has already been inited';
318 0 $self->warn;
319 }
320  
321 1 my $list4 = $self->{options}{list4};
322 1 my $list6 = $self->{options}{list6};
323 1 my $action = $self->{options}{action};
324  
325 1 my @commands = (
326 $self->_remote(
327 '/ip firewall filter add chain=input src-address-list=' . $list4 . ' action=' . $action ),
328 $self->_remote(
329 '/ipv6 firewall filter add chain=input src-address-list=' . $list6 . ' action=' . $action ),
330 );
331  
332 1 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 1 0 1
333 1 $self->{frontend_obj}->{test_data} = \@commands;
334 } else {
335 0 foreach my $command (@commands) {
336 0 my $output = `$command 2>&1`;
337 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
338 0 $self->{error} = 12;
339 0 $self->{errorString} = 'Init failed... command "' . $command . '" resulted in... ' . $output;
340 0 $self->warn;
341 }
342 }
343 }
344  
345 1 $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 or IPv6 address
351 and lowercased, then added to the relevant address-list by running
352 C</ip firewall address-list add> or C</ipv6 firewall address-list add>
353 over SSH. Banning an already banned IP is a noop.
354  
355 $fw_helper->ban(ban => $ip);
356  
357 =cut
358  
359 sub ban {
360 2 ▶ my ( $self, %opts ) = @_;
Subroutine ban: called CC=11 SCAR=36.3
Pod documented
361  
362 2 $self->errorblank;
363  
364 2 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 2 2
365 0 $self->{error} = 1;
366 0 $self->{errorString} = 'backend has not been inited';
367 0 $self->warn;
368 0 return;
369 }
370  
371 2 ▶ if ( !defined( $opts{ban} ) ) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $opts{'ban'}) { } 0 2 2
elsif (ref $opts{'ban'} ne '') { } 0 2 2
elsif (not $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::routeros::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::routeros::IPv6_re\z/) { } 0 2 2
372 0 $self->{error} = 9;
373 0 $self->{errorString} = 'Nothing specified for the value ban';
374 0 $self->warn;
375 0 return;
376 } elsif ( ref( $opts{ban} ) ne '' ) {
377 0 $self->{error} = 10;
378 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
379 0 $self->warn;
380 0 return;
381 ▶ } elsif ( $opts{ban} !~ /\A$IPv4_re\z/
Condition66%2/3
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::routeros::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::routeros::IPv6_re\z/
l!l&&r!l&&!r
110
MC/DC0%0/2
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::routeros::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::routeros::IPv6_re\z/
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::routeros::IPv4_re\z/ $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::routeros::IPv6_re\z/
Truth table66%2/3
A || B
A= $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::routeros::IPv4_re\z/B= $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::routeros::IPv6_re\z/
ABresult
1X1
011
000
382 && $opts{ban} !~ /\A$IPv6_re\z/ )
383 {
384 0 $self->{error} = 10;
385 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 IP';
386 0 $self->warn;
387 0 return;
388 }
389  
390 # lowercase so the same IPv6 IP in differing cases can't result in duplicate entries
391 2 $opts{ban} = lc( $opts{ban} );
392  
393 2 ▶ if ( $self->{banned}{ $opts{ban} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"banned"}{$opts{"ban"}}) 0 2 2
394 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
395 0 $self->{frontend_obj}->{test_data} = 'already banned';
396 }
397 0 return;
398 }
399  
400 2 my $command;
401 2 ▶ if ( $opts{ban} =~ /\A$IPv4_re\z/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::routeros::IPv4_re\z/) { } 1 1 2
402 $command = $self->_remote(
403 1 '/ip firewall address-list add list=' . $self->{options}{list4} . ' address=' . $opts{ban} );
404 } else {
405 $command = $self->_remote(
406 1 '/ipv6 firewall address-list add list=' . $self->{options}{list6} . ' address=' . $opts{ban} );
407 }
408  
409 2 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 2 0 2
410 2 $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 0 $self->{errorString} = 'Ban failed... command "' . $command . '" resulted in... ' . $output;
416 0 $self->warn;
417 }
418 }
419  
420 2 $self->{banned}{ $opts{ban} } = 1;
421 } ## end sub ban
422  
423 =head2 unban
424  
425 Unbans an IP. The value of ban is validated as being a IPv4 or IPv6 address
426 and lowercased, then its address-list entry is removed by running
427 C<< /ip firewall address-list remove [find ...] >> or the IPv6 equivalent
428 over SSH. Unbanning an IP that is not banned is a noop.
429  
430 $fw_helper->unban(ban => $ip);
431  
432 =cut
433  
434 sub unban {
435 1 ▶ my ( $self, %opts ) = @_;
Subroutine unban: called CC=11 SCAR=38.1
Pod documented
436  
437 1 $self->errorblank;
438  
439 1 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 1 1
440 0 $self->{error} = 1;
441 0 $self->{errorString} = 'backend has not been inited';
442 0 $self->warn;
443 0 return;
444 }
445  
446 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::routeros::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::routeros::IPv6_re\z/) { } 0 1 1
447 0 $self->{error} = 9;
448 0 $self->{errorString} = 'Nothing specified for the value ban';
449 0 $self->warn;
450 0 return;
451 } elsif ( ref( $opts{ban} ) ne '' ) {
452 0 $self->{error} = 10;
453 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
454 0 $self->warn;
455 0 return;
456 ▶ } elsif ( $opts{ban} !~ /\A$IPv4_re\z/
Condition33%1/3
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::routeros::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::routeros::IPv6_re\z/
l!l&&r!l&&!r
100
MC/DC0%0/2
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::routeros::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::routeros::IPv6_re\z/
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::routeros::IPv4_re\z/ $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::routeros::IPv6_re\z/
Truth table33%1/3
A || B
A= $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::routeros::IPv4_re\z/B= $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::routeros::IPv6_re\z/
ABresult
1X1
011
000
457 && $opts{ban} !~ /\A$IPv6_re\z/ )
458 {
459 0 $self->{error} = 10;
460 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 IP';
461 0 $self->warn;
462 0 return;
463 }
464  
465 # lowercase so the same IPv6 IP in differing cases can't result in duplicate entries
466 1 $opts{ban} = lc( $opts{ban} );
467  
468 1 ▶ if ( !$self->{banned}{ $opts{ban} } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"banned"}{$opts{"ban"}}) 0 1 1
469 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
470 0 $self->{frontend_obj}->{test_data} = 'not banned';
471 }
472 0 return;
473 }
474  
475 1 my $command;
476 1 ▶ if ( $opts{ban} =~ /\A$IPv4_re\z/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::routeros::IPv4_re\z/) { } 1 0 1
477 $command = $self->_remote( '/ip firewall address-list remove [find where list='
478 . $self->{options}{list4}
479 . ' address='
480 . $opts{ban}
481 1 . ']' );
482 } else {
483 $command = $self->_remote( '/ipv6 firewall address-list remove [find where list='
484 . $self->{options}{list6}
485 . ' address='
486 . $opts{ban}
487 0 . ']' );
488 }
489  
490 1 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 1 0 1
491 1 $self->{frontend_obj}->{test_data} = $command;
492 } else {
493 0 my $output = `$command 2>&1`;
494 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
495 0 $self->{error} = 14;
496 0 $self->{errorString} = 'Unban failed... command "' . $command . '" resulted in... ' . $output;
497 0 $self->warn;
498 }
499 }
500  
501 1 delete( $self->{banned}{ $opts{ban} } );
502 } ## end sub unban
503  
504 # Internal helper. Returns a true value if the passed scalar is a valid IPv4 or
505 # IPv6 CIDR range, that is an address followed by "/" and a prefix length that
506 # is within the range valid for its family (0 to 32 for IPv4, 0 to 128 for
507 # IPv6). Returns false otherwise.
508 sub _valid_cidr {
509 3 ▶ my ( $self, $cidr ) = @_;
Subroutine _valid_cidr: called CC=8 SCAR=24.3
510  
511 3 ▶ return 0 if ( !defined($cidr) || ref($cidr) ne '' );
Branch: 1 branch
BranchTrueFalseTotal
if not defined $cidr or ref $cidr ne "" 0 3 3
Condition33%1/3
not defined $cidr or ref $cidr ne ""
l!l&&r!l&&!r
003
MC/DC0%0/2
not defined $cidr or ref $cidr ne ""
not defined $cidr ref $cidr ne ""
Truth table33%1/3
not(A) or B
A= not defined $cidrB= ref $cidr ne ""
ABresult
1X1
011
000
512  
513 3 ▶ if ( $cidr =~ m!\A(.+)/([0-9]{1,3})\z! ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cidr =~ m[\A(.+)/([0-9]{1,3})\z]) 3 0 3
514 3 my ( $addr, $prefix ) = ( $1, $2 );
515 3 ▶ return 1 if ( $addr =~ /\A$IPv4_re\z/ && $prefix <= 32 );
Branch: 1 branch
BranchTrueFalseTotal
if $addr =~ /\A$Net::Firewall::BlockerHelper::backends::routeros::IPv4_re\z/ and $prefix <= 32 2 1 3
Condition66%2/3
$addr =~ /\A$Net::Firewall::BlockerHelper::backends::routeros::IPv4_re\z/ and $prefix <= 32
!ll&&!rl&&r
102
MC/DC50%1/2
$addr =~ /\A$Net::Firewall::BlockerHelper::backends::routeros::IPv4_re\z/ and $prefix <= 32
$addr =~ /\A$Net::Firewall::BlockerHelper::backends::routeros::IPv4_re\z/ $prefix <= 32
Truth table66%2/3
A and B
A= $addr =~ /\A$Net::Firewall::BlockerHelper::backends::routeros::IPv4_re\z/B= $prefix <= 32
ABresult
0X0
100
111
516 1 ▶ return 1 if ( $addr =~ /\A$IPv6_re\z/ && $prefix <= 128 );
Branch: 1 branch
BranchTrueFalseTotal
if $addr =~ /\A$Net::Firewall::BlockerHelper::backends::routeros::IPv6_re\z/ and $prefix <= 128 1 0 1
Condition33%1/3
$addr =~ /\A$Net::Firewall::BlockerHelper::backends::routeros::IPv6_re\z/ and $prefix <= 128
!ll&&!rl&&r
001
MC/DC0%0/2
$addr =~ /\A$Net::Firewall::BlockerHelper::backends::routeros::IPv6_re\z/ and $prefix <= 128
$addr =~ /\A$Net::Firewall::BlockerHelper::backends::routeros::IPv6_re\z/ $prefix <= 128
Truth table33%1/3
A and B
A= $addr =~ /\A$Net::Firewall::BlockerHelper::backends::routeros::IPv6_re\z/B= $prefix <= 128
ABresult
0X0
100
111
517 }
518  
519 0 return 0;
520 } ## end sub _valid_cidr
521  
522 =head2 ban_cidr
523  
524 Bans a CIDR range by adding it to the relevant address-list. RouterOS
525 address-lists accept a network prefix in the same manner as a single
526 address. The value of ban is validated as being a IPv4 or IPv6 CIDR range
527 and lowercased. Banning an already banned range is a noop.
528  
529 $fw_helper->ban_cidr(ban => '1.2.3.0/24');
530  
531 =cut
532  
533 sub ban_cidr {
534 2 ▶ my ( $self, %opts ) = @_;
Subroutine ban_cidr: called CC=10 SCAR=35.2
Pod documented
535  
536 2 $self->errorblank;
537  
538 2 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 2 2
539 0 $self->{error} = 1;
540 0 $self->{errorString} = 'backend has not been inited';
541 0 $self->warn;
542 0 return;
543 }
544  
545 2 ▶ if ( !defined( $opts{ban} ) ) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $opts{'ban'}) { } 0 2 2
elsif (ref $opts{'ban'} ne '') { } 0 2 2
elsif (not $self->_valid_cidr($opts{'ban'})) { } 0 2 2
546 0 $self->{error} = 9;
547 0 $self->{errorString} = 'Nothing specified for the value ban';
548 0 $self->warn;
549 0 return;
550 } elsif ( ref( $opts{ban} ) ne '' ) {
551 0 $self->{error} = 33;
552 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
553 0 $self->warn;
554 0 return;
555 } elsif ( !$self->_valid_cidr( $opts{ban} ) ) {
556 0 $self->{error} = 33;
557 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 CIDR';
558 0 $self->warn;
559 0 return;
560 }
561  
562 # lowercase so the same IPv6 CIDR in differing cases can't result in duplicate entries
563 2 $opts{ban} = lc( $opts{ban} );
564  
565 2 ▶ if ( $self->{banned_cidr}{ $opts{ban} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"banned_cidr"}{$opts{"ban"}}) 0 2 2
566 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
567 0 $self->{frontend_obj}->{test_data} = 'already banned';
568 }
569 0 return;
570 }
571  
572 2 my $command;
573 2 ▶ if ( $opts{ban} =~ m!\A$IPv4_re/! ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{'ban'} =~ m[\A$Net::Firewall::BlockerHelper::backends::routeros::IPv4_re/]) { } 1 1 2
574 $command = $self->_remote(
575 1 '/ip firewall address-list add list=' . $self->{options}{list4} . ' address=' . $opts{ban} );
576 } else {
577 $command = $self->_remote(
578 1 '/ipv6 firewall address-list add list=' . $self->{options}{list6} . ' address=' . $opts{ban} );
579 }
580  
581 2 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 2 0 2
582 2 $self->{frontend_obj}->{test_data} = $command;
583 } else {
584 0 my $output = `$command 2>&1`;
585 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
586 0 $self->{error} = 31;
587 0 $self->{errorString} = 'Ban failed... command "' . $command . '" resulted in... ' . $output;
588 0 $self->warn;
589 }
590 }
591  
592 2 $self->{banned_cidr}{ $opts{ban} } = 1;
593 } ## end sub ban_cidr
594  
595 =head2 unban_cidr
596  
597 Unbans a CIDR range by removing it from the relevant address-list via
598 C<< address-list remove [find ...] >> over SSH. The value of ban is
599 validated as being a IPv4 or IPv6 CIDR range and lowercased. Unbanning a
600 range that is not banned is a noop.
601  
602 $fw_helper->unban_cidr(ban => '1.2.3.0/24');
603  
604 =cut
605  
606 sub unban_cidr {
607 1 ▶ my ( $self, %opts ) = @_;
Subroutine unban_cidr: called CC=10 SCAR=36.4
Pod documented
608  
609 1 $self->errorblank;
610  
611 1 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 1 1
612 0 $self->{error} = 1;
613 0 $self->{errorString} = 'backend has not been inited';
614 0 $self->warn;
615 0 return;
616 }
617  
618 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 $self->_valid_cidr($opts{'ban'})) { } 0 1 1
619 0 $self->{error} = 9;
620 0 $self->{errorString} = 'Nothing specified for the value ban';
621 0 $self->warn;
622 0 return;
623 } elsif ( ref( $opts{ban} ) ne '' ) {
624 0 $self->{error} = 33;
625 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
626 0 $self->warn;
627 0 return;
628 } elsif ( !$self->_valid_cidr( $opts{ban} ) ) {
629 0 $self->{error} = 33;
630 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 CIDR';
631 0 $self->warn;
632 0 return;
633 }
634  
635 # lowercase so the same IPv6 CIDR in differing cases can't result in duplicate entries
636 1 $opts{ban} = lc( $opts{ban} );
637  
638 1 ▶ if ( !$self->{banned_cidr}{ $opts{ban} } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"banned_cidr"}{$opts{"ban"}}) 0 1 1
639 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
640 0 $self->{frontend_obj}->{test_data} = 'not banned';
641 }
642 0 return;
643 }
644  
645 1 my $command;
646 1 ▶ if ( $opts{ban} =~ m!\A$IPv4_re/! ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{'ban'} =~ m[\A$Net::Firewall::BlockerHelper::backends::routeros::IPv4_re/]) { } 1 0 1
647 $command = $self->_remote( '/ip firewall address-list remove [find where list='
648 . $self->{options}{list4}
649 . ' address='
650 . $opts{ban}
651 1 . ']' );
652 } else {
653 $command = $self->_remote( '/ipv6 firewall address-list remove [find where list='
654 . $self->{options}{list6}
655 . ' address='
656 . $opts{ban}
657 0 . ']' );
658 }
659  
660 1 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 1 0 1
661 1 $self->{frontend_obj}->{test_data} = $command;
662 } else {
663 0 my $output = `$command 2>&1`;
664 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
665 0 $self->{error} = 32;
666 0 $self->{errorString} = 'Unban failed... command "' . $command . '" resulted in... ' . $output;
667 0 $self->warn;
668 }
669 }
670  
671 1 delete( $self->{banned_cidr}{ $opts{ban} } );
672 } ## end sub unban_cidr
673  
674 =head2 list_cidr
675  
676 List banned CIDR ranges. Returns an array of the currently banned CIDR
677 ranges. Single IPs are not included; for those see L</list>.
678  
679 my @banned_cidrs = $fw_helper->list_cidr;
680  
681 =cut
682  
683 sub list_cidr {
684 2 ▶ my ( $self, %opts ) = @_;
Subroutine list_cidr: called CC=2 SCAR=7.0
Pod documented
685  
686 2 $self->errorblank;
687  
688 2 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 2 0 2
689 2 $self->{frontend_obj}->{test_data} = 'list_cidr';
690 }
691  
692 2 return keys( %{ $self->{banned_cidr} } );
693 }
694  
695 =head2 list
696  
697 List banned IPs. Returns an array of the currently banned single IPs. CIDR
698 ranges are not included; for those see L</list_cidr>.
699  
700 my @banned = $fw_helper->list;
701  
702 =cut
703  
704 sub list {
705 0 ▶ my ( $self, %opts ) = @_;
Subroutine list: not called CC=2 SCAR=17.9
Pod documented
706  
707 0 $self->errorblank;
708  
709 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
710 0 $self->{frontend_obj}->{test_data} = 'list';
711 }
712  
713 0 return keys( %{ $self->{banned} } );
714 }
715  
716 =head2 re_init
717  
718 Tells the backend to re-init it's self.
719  
720 This will call teardown and init again. After that it will
721 re-add all previously added bans.
722  
723 $fw_helper->re_init;
724  
725 =cut
726  
727 sub re_init {
728 0 ▶ my ( $self, %opts ) = @_;
Subroutine re_init: not called CC=11 SCAR=48.8
Pod documented
729  
730 0 $self->errorblank;
731  
732 0 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 0 0
733 0 $self->{error} = 1;
734 0 $self->{errorString} = 'backend has not been inited';
735 0 $self->warn;
736 0 return;
737 }
738  
739 # teardown is best effort here as a partially or fully wiped setup is
740 # exactly what re_init needs to recover from; init cleans up any remnants
741 {
742 0 local $@;
743 0 eval { $self->teardown; };
744 }
745 0 $self->init;
746  
747 0 my @to_re_ban = keys( %{ $self->{banned} } );
748 0 my @to_re_ban_cidr = keys( %{ $self->{banned_cidr} } );
749  
750 0 my @re_init_test_data;
751 0 foreach my $item (@to_re_ban) {
752 0 my $command;
753 0 ▶ if ( $item =~ /\A$IPv4_re\z/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($item =~ /\A$Net::Firewall::BlockerHelper::backends::routeros::IPv4_re\z/) { } 0 0 0
754 $command = $self->_remote(
755 0 '/ip firewall address-list add list=' . $self->{options}{list4} . ' address=' . $item );
756 } else {
757 $command = $self->_remote(
758 0 '/ipv6 firewall address-list add list=' . $self->{options}{list6} . ' address=' . $item );
759 }
760  
761 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
762 0 push( @re_init_test_data, $command );
763 } else {
764 0 my $output = `$command 2>&1`;
765 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
766 0 $self->{error} = 13;
767 0 $self->{errorString} = 'Ban failed... command "' . $command . '" resulted in... ' . $output;
768 0 $self->warn;
769 }
770 }
771 } ## end foreach my $item (@to_re_ban)
772  
773 0 foreach my $item (@to_re_ban_cidr) {
774 0 my $command;
775 0 ▶ if ( $item =~ m!\A$IPv4_re/! ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($item =~ m[\A$Net::Firewall::BlockerHelper::backends::routeros::IPv4_re/]) { } 0 0 0
776 $command = $self->_remote(
777 0 '/ip firewall address-list add list=' . $self->{options}{list4} . ' address=' . $item );
778 } else {
779 $command = $self->_remote(
780 0 '/ipv6 firewall address-list add list=' . $self->{options}{list6} . ' address=' . $item );
781 }
782  
783 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
784 0 push( @re_init_test_data, $command );
785 } else {
786 0 my $output = `$command 2>&1`;
787 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
788 0 $self->{error} = 31;
789 0 $self->{errorString} = 'Ban failed... command "' . $command . '" resulted in... ' . $output;
790 0 $self->warn;
791 }
792 }
793 } ## end foreach my $item (@to_re_ban_cidr)
794  
795 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
796 0 $self->{frontend_obj}->{test_data} = \@re_init_test_data;
797 }
798  
799 0 $self->{inited} = 1;
800 } ## end sub re_init
801  
802 =head2 teardown
803  
804 Tears down the setup for the backend.
805  
806 This will remove the filter rules and all address-list entries for both
807 families.
808  
809 $fw_helper->teardown;
810  
811 =cut
812  
813 sub teardown {
814 0 ▶ my ( $self, %opts ) = @_;
Subroutine teardown: not called CC=4 SCAR=30.0
Pod documented
815  
816 0 $self->errorblank;
817  
818 0 $self->{inited} = 0;
819  
820 0 my $list4 = $self->{options}{list4};
821 0 my $list6 = $self->{options}{list6};
822  
823 0 my @commands = (
824 $self->_remote( '/ip firewall filter remove [find where src-address-list=' . $list4 . ']' ),
825 $self->_remote( '/ipv6 firewall filter remove [find where src-address-list=' . $list6 . ']' ),
826 $self->_remote( '/ip firewall address-list remove [find where list=' . $list4 . ']' ),
827 $self->_remote( '/ipv6 firewall address-list remove [find where list=' . $list6 . ']' ),
828 );
829  
830 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
831 0 $self->{frontend_obj}->{test_data} = \@commands;
832 } else {
833 0 foreach my $command (@commands) {
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 0 $self->{errorString} = 'Teardown failed... command "' . $command . '" resulted in... ' . $output;
838 0 $self->warn;
839 }
840 }
841 } ## end else [ if ( $self->{testing} ) ]
842 } ## end sub teardown
843  
844 =head2 stop
845  
846 Alias for L</teardown>, provided for parity with the fail2ban C<actionstop>
847 concept.
848  
849 $fw_helper->stop;
850  
851 =cut
852  
853 sub stop {
854 0 ▶ my ( $self, %opts ) = @_;
Subroutine stop: not called CC=1 SCAR=6.9
Pod documented
855  
856 0 return $self->teardown(%opts);
857 }
858  
859 =head2 check
860  
861 Runs a remote C<< /ip firewall filter print >> for the IPv4 address-list. A
862 zero exit code is treated as healthy. This is the equivalent of fail2ban's
863 C<actioncheck>.
864  
865 if ( !$fw_helper->check ) {
866 $fw_helper->re_init;
867 }
868  
869 =cut
870  
871 sub check {
872 6 ▶ my ( $self, %opts ) = @_;
Subroutine check: called CC=3 SCAR=13.0
Pod documented
873  
874 6 $self->errorblank;
875  
876 my $command
877 6 = $self->_remote( '/ip firewall filter print where src-address-list=' . $self->{options}{list4} );
878  
879 6 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 6 0 6
880 6 $self->{frontend_obj}->{test_data} = $command;
881 6 return 1;
882 }
883  
884 0 my $output = `$command 2>&1`;
885 0 ▶ return $? == 0 ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$? == 0 ? : 0 0 0
886 } ## end sub check
887  
888 =head2 flush
889  
890 Removes all currently banned IPs at once by removing every entry from both
891 address-lists, leaving the filter rules in place. This is the equivalent of
892 fail2ban's C<actionflush>.
893  
894 $fw_helper->flush;
895  
896 =cut
897  
898 sub flush {
899 0 ▶ my ( $self, %opts ) = @_;
Subroutine flush: not called CC=5 SCAR=34.0
Pod documented
900  
901 0 $self->errorblank;
902  
903 0 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 0 0
904 0 $self->{error} = 1;
905 0 $self->{errorString} = 'backend has not been inited';
906 0 $self->warn;
907 0 return;
908 }
909  
910 0 my $list4 = $self->{options}{list4};
911 0 my $list6 = $self->{options}{list6};
912  
913 0 my @commands = (
914 $self->_remote( '/ip firewall address-list remove [find where list=' . $list4 . ']' ),
915 $self->_remote( '/ipv6 firewall address-list remove [find where list=' . $list6 . ']' ),
916 );
917  
918 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
919 0 $self->{frontend_obj}->{test_data} = \@commands;
920 } else {
921 0 foreach my $command (@commands) {
922 0 my $output = `$command 2>&1`;
923 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
924 0 $self->{error} = 25;
925 0 $self->{errorString} = 'Flush failed... command "' . $command . '" resulted in... ' . $output;
926 0 $self->warn;
927 }
928 }
929 } ## end else [ if ( $self->{testing} ) ]
930  
931 # the address-list removals above clear both single IP and CIDR entries at once
932 0 $self->{banned} = {};
933 0 $self->{banned_cidr} = {};
934 } ## end sub flush
935  
936 =head1 ERROR CODES / FLAGS
937  
938 Error handling is provided by L<Error::Helper>. All
939 errors are considered fatal.
940  
941 =head2 1, notInited
942  
943 The backend has not been inited yet.
944  
945 =head2 8, optionsNotHash
946  
947 The item passed to new for options is not a hash.
948  
949 =head2 9, noBanItem
950  
951 No IP or CIDR range specified to ban or unban.
952  
953 =head2 10, banItemNotIP
954  
955 The item to ban is not an IP. Either wrong ref type or regexp
956 test using L<Regexp::IPv4> and L<Regexp::IPv6> failed.
957  
958 =head2 12, backendInitError
959  
960 Failed to init the backend.
961  
962 =head2 13, banFailed
963  
964 Failed to ban the item.
965  
966 =head2 14, unbanFailed
967  
968 Failed to unban the item.
969  
970 =head2 15, listFailed
971  
972 Failed to get a list of bans.
973  
974 =head2 16, reInitFailed
975  
976 Failed to re_init the backend.
977  
978 =head2 17, teardownFailed
979  
980 Failed to teardown the backend.
981  
982 =head2 18, alreadyInited
983  
984 init called, but the backend has already been inited.
985  
986 =head2 24, checkFailed
987  
988 The backend check raised an error.
989  
990 =head2 25, flushFailed
991  
992 Failed to flush the bans.
993  
994 =head2 30, hostNotDefined
995  
996 The option host is undef or blank.
997  
998 =head2 31, banCidrFailed
999  
1000 Failed to ban the CIDR range.
1001  
1002 =head2 32, unbanCidrFailed
1003  
1004 Failed to unban the CIDR range.
1005  
1006 =head2 33, cidrItemNotCidr
1007  
1008 The item to ban is not a CIDR range. Either wrong ref type or it is not an
1009 IPv4 or IPv6 address followed by a prefix length valid for its family.
1010  
1011 =head2 34, cidrNotSupported
1012  
1013 The backend does not support CIDR bans.
1014  
1015 =head2 35, listCidrFailed
1016  
1017 Failed to get a list of CIDR bans.
1018  
1019 =head1 AUTHOR
1020  
1021 Zane C. Bowers-Hadley, C<< <vvelox at vvelox.ent> >>
1022  
1023 =head1 BUGS
1024  
1025 Please report any bugs or feature requests to C<bug-net-firewall-blockerhelper at rt.cpan.org>, or through
1026 the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Net-Firewall-BlockerHelper>. I will be notified, and then you'll
1027 automatically be notified of progress on your bug as I make changes.
1028  
1029  
1030  
1031  
1032 =head1 SUPPORT
1033  
1034 You can find documentation for this module with the perldoc command.
1035  
1036 perldoc Net::Firewall::BlockerHelper
1037  
1038  
1039 You can also look for information at:
1040  
1041 =over 4
1042  
1043 =item * RT: CPAN's request tracker (report bugs here)
1044  
1045 L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=Net-Firewall-BlockerHelper>
1046  
1047 =item * Search CPAN
1048  
1049 L<https://metacpan.org/release/Net-Firewall-BlockerHelper>
1050  
1051 =back
1052  
1053  
1054 =head1 ACKNOWLEDGEMENTS
1055  
1056  
1057 =head1 LICENSE AND COPYRIGHT
1058  
1059 This software is Copyright (c) 2023 by Zane C. Bowers-Hadley.
1060  
1061 This is free software, licensed under:
1062  
1063 The GNU Lesser General Public License, Version 2.1, February 1999
1064  
1065  
1066 =cut
1067  
1068 1 1; # End of Net::Firewall::BlockerHelper
« BlockerHelper/backends/bgp_rtbh.pm ↑ Summary BlockerHelper/backends/nsupdate.pm »