BlockerHelper/backends/routeros_api.pm

filter: Statementstmt 32.5% 142 / 436 Branchbran 33.8% 61 / 180 Conditioncond 39.2% 20 / 51 MC/DCmcdc 18.7% 6 / 32 Subroutinesub 72.4% 21 / 29 Podpod 100% 13 / 13 totaltotal 35.4% 263 / 741 CC 113 SCAR 37.7
CC
113
Cov
33%
re_init
90.0
flush
90.0
_request
72.0
CRAP 43.3
« BlockerHelper/backends/netscaler.pm ↑ Summary BlockerHelper/backends/juniper_srx.pm »
1 package Net::Firewall::BlockerHelper::backends::routeros_api;
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_api - MikroTik RouterOS backend using the REST API.
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 = Net::Firewall::BlockerHelper->new(
27 backend => 'routeros_api',
28 name => 'ssh',
29 options => {
30 host => '10.0.0.1',
31 user => 'blocker',
32 password => $password,
33 },
34 );
35  
36 $fw_helper->init_backend;
37 $fw_helper->ban( ban => '1.2.3.4' );
38 $fw_helper->unban( ban => '1.2.3.4' );
39  
40 =head1 DESCRIPTION
41  
42 Blocks IPs on a MikroTik RouterOS device via its REST API (RouterOS 7.1+),
43 adding and removing entries in a firewall address-list. IPv4 addresses go in
44 the C<< /ip/firewall/address-list >> and IPv6 in C<< /ipv6/firewall/address-list >>,
45 under the list names given by the C<list4>/C<list6> options (both defaulting
46 to C<< <prefix>_<name> >>).
47  
48 This backend manages only the address-list membership. A firewall filter (or
49 raw) rule that drops traffic matching the address-list must already exist on
50 the router. If you would rather have the referencing rules created for you, or
51 you are on RouterOS 6 without the REST API, use the ssh driven C<routeros>
52 backend instead.
53  
54 Blocking is per IP; ports and protocols belong on the referencing rule, so
55 specifying them here is an error.
56  
57 L<LWP::UserAgent> is loaded at run time, so it is only required when this
58 backend is actually used. For https, L<LWP::Protocol::https> must be present
59 as well.
60  
61 =head1 NOTES
62  
63 This backend was written going off the API docs and actual testing is
64 needed to double check a few things as the exact behavior is not clear.
65  
66 teardown currently only removes the single IP address-list entries,
67 leaving CIDR entries on the router, while flush removes both. A re_init
68 after a teardown will re-add the CIDR entries, and whether RouterOS then
69 errors or ends up with duplicate entries needs checking.
70  
71 =head1 METHODS
72  
73 =head2 new
74  
75 Initiates the object.
76  
77 - options :: Backend specific options. See below.
78 - prefix :: Prefix to use. Must match /^[a-zA-Z0-9]+$/. Default kur.
79 - name :: Name of this instance. Required.
80  
81 Ports and protocols are not supported and specifying either is an error.
82  
83 The options hash accepts the following.
84  
85 - host :: RouterOS host, optionally host:port. Required.
86 - Default :: undef
87  
88 - user :: REST API user. Required.
89 - Default :: undef
90  
91 - password :: REST API password. Required.
92 - Default :: undef
93  
94 - scheme :: 'https' or 'http'.
95 - Default :: https
96  
97 - insecure :: If true, skip TLS certificate verification (RouterOS ships a
98 self-signed certificate).
99 - Default :: 0
100  
101 - list4 :: IPv4 address-list name.
102 - Default :: <prefix>_<name>
103  
104 - list6 :: IPv6 address-list name.
105 - Default :: <prefix>_<name>
106  
107 - timeout :: HTTP timeout in seconds.
108 - Default :: 30
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=41.8
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 30 => 'hostNotDefined',
148 31 => 'userNotDefined',
149 32 => 'passwordNotDefined',
150 33 => 'banCidrFailed',
151 34 => 'unbanCidrFailed',
152 35 => 'cidrItemNotCidr',
153 36 => 'cidrNotSupported',
154 37 => 'listCidrFailed',
155 },
156 fatal_flags => {},
157 perror_not_fatal => 0,
158 },
159 options => {},
160 ports => [],
161 protocols => [],
162 testing => undef,
163 test_data => undef,
164 prefix => 'kur',
165 name => undef,
166 frontend_obj => undef,
167 inited => 0,
168 banned => {},
169 banned_cidr => {},
170 cidr_supported => 1,
171 ua => undef,
172 };
173 6 bless $self;
174  
175 # blocking is per IP; ports/protocols belong on the referencing rule
176 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
Condition50%3/6
defined $opts{"ports"} and ref $opts{"ports"} eq "ARRAY" and defined $opts{"ports"}[0]
!ll&&!rl&&r
051
defined $opts{"ports"} and ref $opts{"ports"} eq "ARRAY"
!ll&&!rl&&r
006
MC/DC33%1/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 table50%2/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
177 1 $self->{perror} = 1;
178 1 $self->{error} = 26;
179 1 $self->{errorString} = 'the routeros_api backend manages whole IPs and does not support ports';
180 1 $self->warn;
181 }
182 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
Condition33%2/6
defined $opts{"protocols"} and ref $opts{"protocols"} eq "ARRAY" and defined $opts{"protocols"}[0]
!ll&&!rl&&r
050
defined $opts{"protocols"} and ref $opts{"protocols"} eq "ARRAY"
!ll&&!rl&&r
005
MC/DC0%0/3
defined $opts{"protocols"} and ref $opts{"protocols"} eq "ARRAY" and defined $opts{"protocols"}[0]
defined $opts{"protocols"} ref $opts{"protocols"} eq "ARRAY" defined $opts{"protocols"}[0]
Truth table25%1/4
A and B and C
A= defined $opts{"protocols"}B= ref $opts{"protocols"} eq "ARRAY"C= defined $opts{"protocols"}[0]
ABCresult
0XX0
10X0
1100
1111
183 0 $self->{perror} = 1;
184 0 $self->{error} = 27;
185 0 $self->{errorString} = 'the routeros_api backend manages whole IPs and does not support protocols';
186 0 $self->warn;
187 }
188  
189 # make sure prefix is sane if defined
190 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'}) { } 5 0 5
Condition33%1/3
defined $opts{"prefix"} and not $opts{"prefix"} =~ /^[a-zA-Z0-9]+$/
!ll&&!rl&&r
050
MC/DC0%0/2
defined $opts{"prefix"} and not $opts{"prefix"} =~ /^[a-zA-Z0-9]+$/
defined $opts{"prefix"} not $opts{"prefix"} =~ /^[a-zA-Z0-9]+$/
Truth table33%1/3
A and not(B)
A= defined $opts{"prefix"}B= not $opts{"prefix"} =~ /^[a-zA-Z0-9]+$/
ABresult
0X0
100
111
191 0 $self->{perror} = 1;
192 0 $self->{error} = 6;
193 $self->{errorString}
194 0 = '"' . $opts{prefix} . '" is not a valid prefix as it does not match the regex /^[a-zA-Z0-9]+$/';
195 0 $self->warn;
196 } elsif ( defined( $opts{prefix} ) ) {
197 5 $self->{prefix} = $opts{prefix};
198 }
199  
200 # make sure we have a name and that it is valid
201 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
202 0 $self->{perror} = 1;
203 0 $self->{error} = 7;
204 0 $self->{errorString} = 'name is undef';
205 0 $self->warn;
206 } elsif ( $opts{name} !~ /^[a-zA-Z0-9\-]+$/ ) {
207 0 $self->{perror} = 1;
208 0 $self->{error} = 7;
209 0 $self->{errorString} = 'name set to "' . $opts{name} . '" which does not match the regexp /^[a-zA-Z0-9\-]+$/';
210 0 $self->warn;
211 }
212 5 $self->{name} = $opts{name};
213  
214 5 ▶ if ( defined( $opts{testing} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"testing"}) 5 0 5
215 5 $self->{testing} = $opts{testing};
216 }
217 5 ▶ if ( defined( $opts{frontend_obj} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"frontend_obj"}) 5 0 5
218 5 $self->{frontend_obj} = $opts{frontend_obj};
219 }
220  
221 5 ▶ if ( defined( $opts{options} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"options"}) 5 0 5
222 5 ▶ if ( ref( $opts{options} ) ne 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $opts{"options"} ne "HASH") 0 5 5
223 0 $self->{perror} = 1;
224 0 $self->{error} = 8;
225 0 $self->{errorString} = 'ref for options is "' . ref( $opts{options} ) . '" and not HASH';
226 0 $self->warn;
227 }
228 5 $self->{options} = $opts{options};
229 }
230  
231 # required connection options
232 5 ▶ if ( !defined( $self->{options}{host} ) || $self->{options}{host} eq '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->{"options"}{"host"} or $self->{"options"}{"host"} eq "") 1 4 5
Condition66%2/3
not defined $self->{"options"}{"host"} or $self->{"options"}{"host"} eq ""
l!l&&r!l&&!r
104
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
233 1 $self->{perror} = 1;
234 1 $self->{error} = 30;
235 1 $self->{errorString} = 'the option host is undef or blank';
236 1 $self->warn;
237 }
238 4 ▶ if ( !defined( $self->{options}{user} ) || $self->{options}{user} eq '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->{"options"}{"user"} or $self->{"options"}{"user"} eq "") 1 3 4
Condition66%2/3
not defined $self->{"options"}{"user"} or $self->{"options"}{"user"} eq ""
l!l&&r!l&&!r
103
MC/DC50%1/2
not defined $self->{"options"}{"user"} or $self->{"options"}{"user"} eq ""
not defined $self->{"options"}{"user"} $self->{"options"}{"user"} eq ""
Truth table66%2/3
not(A) or B
A= not defined $self->{"options"}{"user"}B= $self->{"options"}{"user"} eq ""
ABresult
1X1
011
000
239 1 $self->{perror} = 1;
240 1 $self->{error} = 31;
241 1 $self->{errorString} = 'the option user is undef or blank';
242 1 $self->warn;
243 }
244 3 ▶ if ( !defined( $self->{options}{password} ) || $self->{options}{password} eq '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->{"options"}{"password"} or $self->{"options"}{"password"} eq "") 1 2 3
Condition66%2/3
not defined $self->{"options"}{"password"} or $self->{"options"}{"password"} eq ""
l!l&&r!l&&!r
102
MC/DC50%1/2
not defined $self->{"options"}{"password"} or $self->{"options"}{"password"} eq ""
not defined $self->{"options"}{"password"} $self->{"options"}{"password"} eq ""
Truth table66%2/3
not(A) or B
A= not defined $self->{"options"}{"password"}B= $self->{"options"}{"password"} eq ""
ABresult
1X1
011
000
245 1 $self->{perror} = 1;
246 1 $self->{error} = 32;
247 1 $self->{errorString} = 'the option password is undef or blank';
248 1 $self->warn;
249 }
250  
251 # defaults
252 2 ▶ $self->{options}{scheme} = 'https' if ( !defined( $self->{options}{scheme} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"options"}{"scheme"} 1 1 2
253 2 ▶ $self->{options}{insecure} = 0 if ( !defined( $self->{options}{insecure} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"options"}{"insecure"} 2 0 2
254 2 ▶ $self->{options}{timeout} = 30 if ( !defined( $self->{options}{timeout} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"options"}{"timeout"} 2 0 2
255 2 ▶ if ( defined( $self->{name} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"name"}) 2 0 2
256 2 ▶ $self->{options}{list4} = $self->{prefix} . '_' . $self->{name} if ( !defined( $self->{options}{list4} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"options"}{"list4"} 1 1 2
257 2 ▶ $self->{options}{list6} = $self->{prefix} . '_' . $self->{name} if ( !defined( $self->{options}{list6} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"options"}{"list6"} 2 0 2
258 }
259  
260 2 return $self;
261 } ## end sub new
262  
263 # Internal helper. Returns the address-list name for the passed IP's family.
264 sub _list_name {
265 6 ▶ my ( $self, $ip ) = @_;
Subroutine _list_name: called CC=2 SCAR=6.9
266  
267 6 ▶ return ( $ip =~ /\A$IPv4_re\z/ ) ? $self->{options}{list4} : $self->{options}{list6};
Branch: 1 branch
BranchTrueFalseTotal
$ip =~ /\A$Net::Firewall::BlockerHelper::backends::routeros_api::IPv4_re\z/ ? : 5 1 6
268 }
269  
270 # Internal helper. Returns the address-list REST endpoint for the passed IP's
271 # family (or IPv4 if no IP is given).
272 sub _list_url {
273 16 ▶ my ( $self, $ip ) = @_;
Subroutine _list_url: called CC=3 SCAR=11.0
274  
275 16 ▶ my $menu = ( defined($ip) && $ip =~ /\A$IPv6_re\z/ ) ? 'ipv6' : 'ip';
Branch: 1 branch
BranchTrueFalseTotal
defined $ip && $ip =~ /\A$Net::Firewall::BlockerHelper::backends::routeros_api::IPv6_re\z/ ? : 1 15 16
Condition100%3/3
defined $ip and $ip =~ /\A$Net::Firewall::BlockerHelper::backends::routeros_api::IPv6_re\z/
!ll&&!rl&&r
871
MC/DC100%2/2
defined $ip and $ip =~ /\A$Net::Firewall::BlockerHelper::backends::routeros_api::IPv6_re\z/
defined $ip $ip =~ /\A$Net::Firewall::BlockerHelper::backends::routeros_api::IPv6_re\z/
Truth table100%3/3
A and B
A= defined $ipB= $ip =~ /\A$Net::Firewall::BlockerHelper::backends::routeros_api::IPv6_re\z/
ABresult
0X0
100
111
276  
277 16 return $self->{options}{scheme} . '://' . $self->{options}{host} . '/rest/' . $menu . '/firewall/address-list';
278 }
279  
280 # Internal helper. Minimal percent encoder so URI::Escape is not needed.
281 sub _uri_escape {
282 13 ▶ my ( $self, $string ) = @_;
Subroutine _uri_escape: called CC=1 SCAR=0.0
283  
284 13 $string =~ s/([^A-Za-z0-9\-._~])/sprintf('%%%02X', ord($1))/ge;
285  
286 13 return $string;
287 }
288  
289 # Internal helper. Returns a canonical JSON::PP encoder/decoder.
290 sub _json {
291 4 ▶ my ($self) = @_;
Subroutine _json: called CC=1 SCAR=0.0
292  
293 4 require JSON::PP;
294 4 return JSON::PP->new->canonical->utf8;
295 }
296  
297 # Internal helper. Performs a HTTP request via LWP::UserAgent using HTTP basic
298 # auth, returning the decoded JSON body (or undef for an empty body) and dying
299 # with an explanation on any HTTP level failure. Never called in testing mode.
300 sub _request {
301 0 ▶ my ( $self, $method, $url, $body ) = @_;
Subroutine _request: not called CC=8 SCAR=42.8
302  
303 0 ▶ if ( !defined( $self->{ua} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"ua"}) 0 0 0
304 0 local $@;
305 0 ▶ eval {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require LWP::UserAgent; my(%args) = ("agent", 'Net::Firewall::BlockerHelper/' . $Net::Firewall::BlockerHelper::backends::routeros_api::VERSION, "timeout", $self->{"options"}{"timeout"}); if ($self->{"options"}{"insecure"}) { $args{"ssl_opts"} = {"verify_hostname", 0, "SSL_verify_mode", 0}; } ; $self->{"ua"} = "LWP::UserAgent"->new(%args); 1 } } or die 'failed to load LWP::UserAgent, which the routeros_api backend requires... ' . $@ 0 0 0
306 0 require LWP::UserAgent;
307 my %args = (
308 agent => 'Net::Firewall::BlockerHelper/' . $VERSION,
309 timeout => $self->{options}{timeout},
310 0 );
311 0 ▶ if ( $self->{options}{insecure} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"options"}{"insecure"}) 0 0 0
312 0 $args{ssl_opts} = { verify_hostname => 0, SSL_verify_mode => 0 };
313 }
314 0 $self->{ua} = LWP::UserAgent->new(%args);
315 0 1;
316 } or die( 'failed to load LWP::UserAgent, which the routeros_api backend requires... ' . $@ );
317 }
318  
319 0 require MIME::Base64;
320 0 my $auth = 'Basic ' . MIME::Base64::encode_base64( $self->{options}{user} . ':' . $self->{options}{password}, '' );
321  
322 0 my @headers = ( 'Content-Type' => 'application/json', 'Authorization' => $auth );
323  
324 0 require HTTP::Request;
325 0 my $request = HTTP::Request->new( $method, $url, \@headers, $body );
326  
327 0 my $response = $self->{ua}->request($request);
328  
329 0 ▶ if ( !$response->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->is_success) 0 0 0
330 0 my $detail = $response->decoded_content;
331 0 ▶ $detail = defined($detail) ? ( ' body... ' . $detail ) : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $detail ? : 0 0 0
332 0 die( $method . ' ' . $url . ' failed... HTTP status... ' . $response->status_line . $detail );
333 }
334  
335 0 my $decoded;
336 0 my $content = $response->decoded_content;
337 0 ▶ if ( defined($content) && $content ne '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $content and $content ne "") 0 0 0
Condition0%0/3
defined $content and $content ne ""
!ll&&!rl&&r
000
MC/DC0%0/2
defined $content and $content ne ""
defined $content $content ne ""
Truth table0%0/3
A and B
A= defined $contentB= $content ne ""
ABresult
0X0
100
111
338 0 local $@;
339 0 eval { $decoded = $self->_json->decode($content); };
340 }
341  
342 0 return $decoded;
343 } ## end sub _request
344  
345 =head2 init
346  
347 Initiates the backend. Verifies the credentials and reachability by
348 performing a GET against the IPv4 address-list endpoint. Nothing is created
349 on the router; the referencing firewall rule must already exist.
350  
351 $backend->init;
352  
353 =cut
354  
355 sub init {
356 2 ▶ my ( $self, %opts ) = @_;
Subroutine init: called CC=4 SCAR=20.3
Pod documented
357  
358 2 $self->errorblank;
359  
360 2 ▶ if ( $self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"inited"}) 0 2 2
361 0 $self->{error} = 18;
362 0 $self->{errorString} = 'backend has already been inited';
363 0 $self->warn;
364 }
365  
366 2 my $url = $self->_list_url . '?list=' . $self->_uri_escape( $self->{options}{list4} );
367  
368 2 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 2 0 2
369 2 $self->{frontend_obj}->{test_data} = [ { method => 'GET', url => $url } ];
370 } else {
371 0 local $@;
372 0 ▶ eval { $self->_request( 'GET', $url ); 1; } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->_request("GET", $url); 1 } } or do { $self->{"error"} = 23; $self->{"errorString"} = 'init failed. probing the address-list endpoint failed... ' . $@; $self->warn } 0 0 0
373 0 $self->{error} = 23;
374 0 $self->{errorString} = 'init failed. probing the address-list endpoint failed... ' . $@;
375 0 $self->warn;
376 };
377 }
378  
379 2 $self->{inited} = 1;
380 } ## end sub init
381  
382 =head2 ban
383  
384 Bans an IP. The value of ban is validated as being a IPv4 or IPv6 address
385 and lowercased, then added to the relevant address-list via a PUT to
386 C<< /rest/ip/firewall/address-list >> or
387 C<< /rest/ipv6/firewall/address-list >>. Banning an already banned IP is a
388 noop.
389  
390 $backend->ban(ban => $ip);
391  
392 =cut
393  
394 sub ban {
395 3 ▶ my ( $self, %opts ) = @_;
Subroutine ban: called CC=10 SCAR=37.2
Pod documented
396  
397 3 $self->errorblank;
398  
399 3 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 3 3
400 0 $self->{error} = 1;
401 0 $self->{errorString} = 'backend has not been inited';
402 0 $self->warn;
403 0 return;
404 }
405  
406 3 ▶ if ( !defined( $opts{ban} ) ) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $opts{'ban'}) { } 0 3 3
elsif (ref $opts{'ban'} ne '') { } 0 3 3
elsif (not $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::routeros_api::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::routeros_api::IPv6_re\z/) { } 0 3 3
407 0 $self->{error} = 9;
408 0 $self->{errorString} = 'Nothing specified for the value ban';
409 0 $self->warn;
410 0 return;
411 } elsif ( ref( $opts{ban} ) ne '' ) {
412 0 $self->{error} = 10;
413 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
414 0 $self->warn;
415 0 return;
416 ▶ } elsif ( $opts{ban} !~ /\A$IPv4_re\z/
Condition66%2/3
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::routeros_api::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::routeros_api::IPv6_re\z/
l!l&&r!l&&!r
210
MC/DC0%0/2
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::routeros_api::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::routeros_api::IPv6_re\z/
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::routeros_api::IPv4_re\z/ $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::routeros_api::IPv6_re\z/
Truth table66%2/3
A || B
A= $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::routeros_api::IPv4_re\z/B= $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::routeros_api::IPv6_re\z/
ABresult
1X1
011
000
417 && $opts{ban} !~ /\A$IPv6_re\z/ )
418 {
419 0 $self->{error} = 10;
420 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 IP';
421 0 $self->warn;
422 0 return;
423 }
424  
425 # lowercase so the same IPv6 IP in differing cases can't result in duplicate entries
426 3 $opts{ban} = lc( $opts{ban} );
427  
428 3 ▶ if ( $self->{banned}{ $opts{ban} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"banned"}{$opts{"ban"}}) 0 3 3
429 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
430 0 $self->{frontend_obj}->{test_data} = 'already banned';
431 }
432 0 return;
433 }
434  
435 3 my $body = $self->_json->encode( { list => $self->_list_name( $opts{ban} ), address => $opts{ban} } );
436  
437 3 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 3 0 3
438 3 $self->{frontend_obj}->{test_data} = [ { method => 'PUT', url => $self->_list_url( $opts{ban} ), content => $body } ];
439 } else {
440 0 local $@;
441 0 ▶ eval { $self->_request( 'PUT', $self->_list_url( $opts{ban} ), $body ); 1; } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->_request("PUT", $self->_list_url($opts{"ban"}), $body); 1 } } or do { $self->{"error"} = 13; $self->{"errorString"} = 'banning "' . $opts{"ban"} . '" failed... ' . $@; $self->warn; return } 0 0 0
442 0 $self->{error} = 13;
443 0 $self->{errorString} = 'banning "' . $opts{ban} . '" failed... ' . $@;
444 0 $self->warn;
445 0 return;
446 };
447 }
448  
449 3 $self->{banned}{ $opts{ban} } = 1;
450 } ## end sub ban
451  
452 # Internal helper. Returns the lookup URL used to find the address-list entry
453 # ID for the passed IP.
454 sub _unban_lookup_url {
455 2 ▶ my ( $self, $ip ) = @_;
Subroutine _unban_lookup_url: called CC=1 SCAR=0.0
456  
457 return
458 2 $self->_list_url($ip)
459 . '?list='
460 . $self->_uri_escape( $self->_list_name($ip) )
461 . '&address='
462 . $self->_uri_escape($ip);
463 } ## end sub _unban_lookup_url
464  
465 # Internal helper. Looks up the address-list entry for the IP and deletes it by
466 # its ".id". An entry that can not be found is treated as already unbanned.
467 # Dies on failure. Never called in testing mode.
468 sub _unban_ip {
469 0 ▶ my ( $self, $ip ) = @_;
Subroutine _unban_ip: not called CC=4 SCAR=30.0
470  
471 0 my $decoded = $self->_request( 'GET', $self->_unban_lookup_url($ip) );
472  
473 0 my $id;
474 0 ▶ if ( ref($decoded) eq 'ARRAY' && defined( $decoded->[0] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $decoded eq "ARRAY" and defined $decoded->[0]) 0 0 0
Condition0%0/3
ref $decoded eq "ARRAY" and defined $decoded->[0]
!ll&&!rl&&r
000
MC/DC0%0/2
ref $decoded eq "ARRAY" and defined $decoded->[0]
ref $decoded eq "ARRAY" defined $decoded->[0]
Truth table0%0/3
A and B
A= ref $decoded eq "ARRAY"B= defined $decoded->[0]
ABresult
0X0
100
111
475 0 $id = $decoded->[0]{'.id'};
476 }
477  
478 # no entry found means something else removed it already, which is fine
479 0 ▶ if ( !defined($id) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $id) 0 0 0
480 0 return;
481 }
482  
483 0 $self->_request( 'DELETE', $self->_list_url($ip) . '/' . $id );
484  
485 0 return;
486 } ## end sub _unban_ip
487  
488 =head2 unban
489  
490 Unbans an IP. The value of ban is validated as being a IPv4 or IPv6 address
491 and lowercased. The address-list entry is looked up via a GET to find its
492 C<.id> and then removed via a DELETE. An entry that can not be found is
493 treated as already unbanned. Unbanning an IP that is not banned is a noop.
494  
495 $backend->unban(ban => $ip);
496  
497 =cut
498  
499 sub unban {
500 1 ▶ my ( $self, %opts ) = @_;
Subroutine unban: called CC=10 SCAR=38.2
Pod documented
501  
502 1 $self->errorblank;
503  
504 1 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 1 1
505 0 $self->{error} = 1;
506 0 $self->{errorString} = 'backend has not been inited';
507 0 $self->warn;
508 0 return;
509 }
510  
511 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_api::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::routeros_api::IPv6_re\z/) { } 0 1 1
512 0 $self->{error} = 9;
513 0 $self->{errorString} = 'Nothing specified for the value ban';
514 0 $self->warn;
515 0 return;
516 } elsif ( ref( $opts{ban} ) ne '' ) {
517 0 $self->{error} = 10;
518 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
519 0 $self->warn;
520 0 return;
521 ▶ } elsif ( $opts{ban} !~ /\A$IPv4_re\z/
Condition33%1/3
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::routeros_api::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::routeros_api::IPv6_re\z/
l!l&&r!l&&!r
100
MC/DC0%0/2
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::routeros_api::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::routeros_api::IPv6_re\z/
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::routeros_api::IPv4_re\z/ $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::routeros_api::IPv6_re\z/
Truth table33%1/3
A || B
A= $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::routeros_api::IPv4_re\z/B= $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::routeros_api::IPv6_re\z/
ABresult
1X1
011
000
522 && $opts{ban} !~ /\A$IPv6_re\z/ )
523 {
524 0 $self->{error} = 10;
525 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 IP';
526 0 $self->warn;
527 0 return;
528 }
529  
530 # lowercase so the same IPv6 IP in differing cases can't result in duplicate entries
531 1 $opts{ban} = lc( $opts{ban} );
532  
533 1 ▶ if ( !$self->{banned}{ $opts{ban} } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"banned"}{$opts{"ban"}}) 0 1 1
534 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
535 0 $self->{frontend_obj}->{test_data} = 'not banned';
536 }
537 0 return;
538 }
539  
540 1 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 1 0 1
541 $self->{frontend_obj}->{test_data} = [
542 { method => 'GET', url => $self->_unban_lookup_url( $opts{ban} ) },
543 1 { method => 'DELETE', url => $self->_list_url( $opts{ban} ) . '/<id>' },
544 ];
545 } else {
546 0 local $@;
547 0 ▶ eval { $self->_unban_ip( $opts{ban} ); 1; } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->_unban_ip($opts{"ban"}); 1 } } or do { $self->{"error"} = 14; $self->{"errorString"} = 'unbanning "' . $opts{"ban"} . '" failed... ' . $@; $self->warn; return } 0 0 0
548 0 $self->{error} = 14;
549 0 $self->{errorString} = 'unbanning "' . $opts{ban} . '" failed... ' . $@;
550 0 $self->warn;
551 0 return;
552 };
553 }
554  
555 1 delete( $self->{banned}{ $opts{ban} } );
556 } ## end sub unban
557  
558 # Internal helper. Returns a true value if the passed scalar is a valid IPv4 or
559 # IPv6 CIDR range, that is an address followed by "/" and a prefix length that
560 # is within the range valid for its family (0 to 32 for IPv4, 0 to 128 for
561 # IPv6). Returns false otherwise.
562 sub _valid_cidr {
563 2 ▶ my ( $self, $cidr ) = @_;
Subroutine _valid_cidr: called CC=8 SCAR=30.3
564  
565 2 ▶ return 0 if ( !defined($cidr) || ref($cidr) ne '' );
Branch: 1 branch
BranchTrueFalseTotal
if not defined $cidr or ref $cidr ne "" 0 2 2
Condition33%1/3
not defined $cidr or ref $cidr ne ""
l!l&&r!l&&!r
002
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
566  
567 2 ▶ if ( $cidr =~ m!\A(.+)/([0-9]{1,3})\z! ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cidr =~ m[\A(.+)/([0-9]{1,3})\z]) 2 0 2
568 2 my ( $addr, $prefix ) = ( $1, $2 );
569 2 ▶ return 1 if ( $addr =~ /\A$IPv4_re\z/ && $prefix <= 32 );
Branch: 1 branch
BranchTrueFalseTotal
if $addr =~ /\A$Net::Firewall::BlockerHelper::backends::routeros_api::IPv4_re\z/ and $prefix <= 32 2 0 2
Condition33%1/3
$addr =~ /\A$Net::Firewall::BlockerHelper::backends::routeros_api::IPv4_re\z/ and $prefix <= 32
!ll&&!rl&&r
002
MC/DC0%0/2
$addr =~ /\A$Net::Firewall::BlockerHelper::backends::routeros_api::IPv4_re\z/ and $prefix <= 32
$addr =~ /\A$Net::Firewall::BlockerHelper::backends::routeros_api::IPv4_re\z/ $prefix <= 32
Truth table33%1/3
A and B
A= $addr =~ /\A$Net::Firewall::BlockerHelper::backends::routeros_api::IPv4_re\z/B= $prefix <= 32
ABresult
0X0
100
111
570 0 ▶ return 1 if ( $addr =~ /\A$IPv6_re\z/ && $prefix <= 128 );
Branch: 1 branch
BranchTrueFalseTotal
if $addr =~ /\A$Net::Firewall::BlockerHelper::backends::routeros_api::IPv6_re\z/ and $prefix <= 128 0 0 0
Condition0%0/3
$addr =~ /\A$Net::Firewall::BlockerHelper::backends::routeros_api::IPv6_re\z/ and $prefix <= 128
!ll&&!rl&&r
000
MC/DC0%0/2
$addr =~ /\A$Net::Firewall::BlockerHelper::backends::routeros_api::IPv6_re\z/ and $prefix <= 128
$addr =~ /\A$Net::Firewall::BlockerHelper::backends::routeros_api::IPv6_re\z/ $prefix <= 128
Truth table0%0/3
A and B
A= $addr =~ /\A$Net::Firewall::BlockerHelper::backends::routeros_api::IPv6_re\z/B= $prefix <= 128
ABresult
0X0
100
111
571 }
572  
573 0 return 0;
574 } ## end sub _valid_cidr
575  
576 # Internal helper. Returns the address portion of a CIDR range so its family can
577 # be determined via the same helpers used for single IPs.
578 sub _cidr_addr {
579 2 ▶ my ( $self, $cidr ) = @_;
Subroutine _cidr_addr: called CC=2 SCAR=7.6
580  
581 2 ▶ if ( $cidr =~ m!\A(.+)/[0-9]{1,3}\z! ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cidr =~ m[\A(.+)/[0-9]{1,3}\z]) 2 0 2
582 2 return $1;
583 }
584  
585 0 return $cidr;
586 } ## end sub _cidr_addr
587  
588 =head2 ban_cidr
589  
590 Bans a CIDR range by adding it to the relevant address-list via a PUT.
591 RouterOS address-lists accept a network prefix in the same manner as a
592 single address. The value of ban is validated as being a IPv4 or IPv6 CIDR
593 range and lowercased. Banning an already banned range is a noop.
594  
595 $backend->ban_cidr(ban => '1.2.3.0/24');
596  
597 =cut
598  
599 sub ban_cidr {
600 1 ▶ my ( $self, %opts ) = @_;
Subroutine ban_cidr: called CC=9 SCAR=35.6
Pod documented
601  
602 1 $self->errorblank;
603  
604 1 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 1 1
605 0 $self->{error} = 1;
606 0 $self->{errorString} = 'backend has not been inited';
607 0 $self->warn;
608 0 return;
609 }
610  
611 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
612 0 $self->{error} = 9;
613 0 $self->{errorString} = 'Nothing specified for the value ban';
614 0 $self->warn;
615 0 return;
616 } elsif ( ref( $opts{ban} ) ne '' ) {
617 0 $self->{error} = 35;
618 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
619 0 $self->warn;
620 0 return;
621 } elsif ( !$self->_valid_cidr( $opts{ban} ) ) {
622 0 $self->{error} = 35;
623 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 CIDR';
624 0 $self->warn;
625 0 return;
626 }
627  
628 # lowercase so the same IPv6 CIDR in differing cases can't result in duplicate entries
629 1 $opts{ban} = lc( $opts{ban} );
630  
631 1 ▶ if ( $self->{banned_cidr}{ $opts{ban} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"banned_cidr"}{$opts{"ban"}}) 0 1 1
632 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
633 0 $self->{frontend_obj}->{test_data} = 'already banned';
634 }
635 0 return;
636 }
637  
638 1 my $addr = $self->_cidr_addr( $opts{ban} );
639 1 my $body = $self->_json->encode( { list => $self->_list_name($addr), address => $opts{ban} } );
640  
641 1 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 1 0 1
642 1 $self->{frontend_obj}->{test_data} = [ { method => 'PUT', url => $self->_list_url($addr), content => $body } ];
643 } else {
644 0 local $@;
645 0 ▶ eval { $self->_request( 'PUT', $self->_list_url($addr), $body ); 1; } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->_request("PUT", $self->_list_url($addr), $body); 1 } } or do { $self->{"error"} = 33; $self->{"errorString"} = 'banning "' . $opts{"ban"} . '" failed... ' . $@; $self->warn; return } 0 0 0
646 0 $self->{error} = 33;
647 0 $self->{errorString} = 'banning "' . $opts{ban} . '" failed... ' . $@;
648 0 $self->warn;
649 0 return;
650 };
651 }
652  
653 1 $self->{banned_cidr}{ $opts{ban} } = 1;
654 } ## end sub ban_cidr
655  
656 =head2 unban_cidr
657  
658 Unbans a CIDR range. The value of ban is validated as being a IPv4 or IPv6
659 CIDR range and lowercased. Its address-list entry is looked up via a GET to
660 find its C<.id> and then removed via a DELETE. An entry that can not be
661 found is treated as already unbanned. Unbanning a range that is not banned
662 is a noop.
663  
664 $backend->unban_cidr(ban => '1.2.3.0/24');
665  
666 =cut
667  
668 sub unban_cidr {
669 1 ▶ my ( $self, %opts ) = @_;
Subroutine unban_cidr: called CC=9 SCAR=36.0
Pod documented
670  
671 1 $self->errorblank;
672  
673 1 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 1 1
674 0 $self->{error} = 1;
675 0 $self->{errorString} = 'backend has not been inited';
676 0 $self->warn;
677 0 return;
678 }
679  
680 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
681 0 $self->{error} = 9;
682 0 $self->{errorString} = 'Nothing specified for the value ban';
683 0 $self->warn;
684 0 return;
685 } elsif ( ref( $opts{ban} ) ne '' ) {
686 0 $self->{error} = 35;
687 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
688 0 $self->warn;
689 0 return;
690 } elsif ( !$self->_valid_cidr( $opts{ban} ) ) {
691 0 $self->{error} = 35;
692 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 CIDR';
693 0 $self->warn;
694 0 return;
695 }
696  
697 # lowercase so the same IPv6 CIDR in differing cases can't result in duplicate entries
698 1 $opts{ban} = lc( $opts{ban} );
699  
700 1 ▶ if ( !$self->{banned_cidr}{ $opts{ban} } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"banned_cidr"}{$opts{"ban"}}) 0 1 1
701 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
702 0 $self->{frontend_obj}->{test_data} = 'not banned';
703 }
704 0 return;
705 }
706  
707 1 my $addr = $self->_cidr_addr( $opts{ban} );
708  
709 1 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 1 0 1
710 $self->{frontend_obj}->{test_data} = [
711 1 { method => 'GET', url => $self->_unban_lookup_url($addr) . '&address=' . $self->_uri_escape( $opts{ban} ) },
712 { method => 'DELETE', url => $self->_list_url($addr) . '/<id>' },
713 ];
714 } else {
715 0 local $@;
716 0 ▶ eval { $self->_unban_cidr_entry( $opts{ban} ); 1; } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->_unban_cidr_entry($opts{"ban"}); 1 } } or do { $self->{"error"} = 34; $self->{"errorString"} = 'unbanning "' . $opts{"ban"} . '" failed... ' . $@; $self->warn; return } 0 0 0
717 0 $self->{error} = 34;
718 0 $self->{errorString} = 'unbanning "' . $opts{ban} . '" failed... ' . $@;
719 0 $self->warn;
720 0 return;
721 };
722 }
723  
724 1 delete( $self->{banned_cidr}{ $opts{ban} } );
725 } ## end sub unban_cidr
726  
727 # Internal helper. Looks up the address-list entry for the CIDR range and deletes
728 # it by its ".id". An entry that can not be found is treated as already
729 # unbanned. Dies on failure. Never called in testing mode.
730 sub _unban_cidr_entry {
731 0 ▶ my ( $self, $cidr ) = @_;
Subroutine _unban_cidr_entry: not called CC=4 SCAR=30.0
732  
733 0 my $addr = $self->_cidr_addr($cidr);
734  
735 0 my $url
736 = $self->_list_url($addr)
737 . '?list='
738 . $self->_uri_escape( $self->_list_name($addr) )
739 . '&address='
740 . $self->_uri_escape($cidr);
741  
742 0 my $decoded = $self->_request( 'GET', $url );
743  
744 0 my $id;
745 0 ▶ if ( ref($decoded) eq 'ARRAY' && defined( $decoded->[0] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $decoded eq "ARRAY" and defined $decoded->[0]) 0 0 0
Condition0%0/3
ref $decoded eq "ARRAY" and defined $decoded->[0]
!ll&&!rl&&r
000
MC/DC0%0/2
ref $decoded eq "ARRAY" and defined $decoded->[0]
ref $decoded eq "ARRAY" defined $decoded->[0]
Truth table0%0/3
A and B
A= ref $decoded eq "ARRAY"B= defined $decoded->[0]
ABresult
0X0
100
111
746 0 $id = $decoded->[0]{'.id'};
747 }
748  
749 # no entry found means something else removed it already, which is fine
750 0 ▶ if ( !defined($id) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $id) 0 0 0
751 0 return;
752 }
753  
754 0 $self->_request( 'DELETE', $self->_list_url($addr) . '/' . $id );
755  
756 0 return;
757 } ## end sub _unban_cidr_entry
758  
759 =head2 list_cidr
760  
761 List banned CIDR ranges. Returns an array of the currently banned CIDR
762 ranges. Single IPs are not included; for those see L</list>.
763  
764 my @banned_cidrs = $backend->list_cidr;
765  
766 =cut
767  
768 sub list_cidr {
769 2 ▶ my ( $self, %opts ) = @_;
Subroutine list_cidr: called CC=2 SCAR=7.0
Pod documented
770  
771 2 $self->errorblank;
772  
773 2 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 2 0 2
774 2 $self->{frontend_obj}->{test_data} = 'list_cidr';
775 }
776  
777 2 return keys( %{ $self->{banned_cidr} } );
778 }
779  
780 =head2 list
781  
782 List banned IPs. Returns an array of the currently banned single IPs. CIDR
783 ranges are not included; for those see L</list_cidr>.
784  
785 my @banned = $backend->list;
786  
787 =cut
788  
789 sub list {
790 0 ▶ my ( $self, %opts ) = @_;
Subroutine list: not called CC=2 SCAR=17.9
Pod documented
791  
792 0 $self->errorblank;
793  
794 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
795 0 $self->{frontend_obj}->{test_data} = 'list';
796 }
797  
798 0 return keys( %{ $self->{banned} } );
799 }
800  
801 =head2 re_init
802  
803 Tears down and re-inits, then re-adds all previously added bans. teardown
804 is best effort, as a partially or fully wiped setup is what this needs to
805 recover from. Each retained single IP and CIDR ban is re-added via a PUT to
806 the relevant address-list endpoint.
807  
808 $backend->re_init;
809  
810 =cut
811  
812 sub re_init {
813 0 ▶ my ( $self, %opts ) = @_;
Subroutine re_init: not called CC=9 SCAR=45.0
Pod documented
814  
815 0 $self->errorblank;
816  
817 0 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 0 0
818 0 $self->{error} = 1;
819 0 $self->{errorString} = 'backend has not been inited';
820 0 $self->warn;
821 0 return;
822 }
823  
824 # teardown is best effort here as a partially or fully wiped setup is
825 # exactly what re_init needs to recover from
826 {
827 0 local $@;
828 0 eval { $self->teardown; };
829 }
830 0 $self->init;
831  
832 0 my @re_init_test_data;
833 0 foreach my $item ( keys( %{ $self->{banned} } ) ) {
834 0 my $body = $self->_json->encode( { list => $self->_list_name($item), address => $item } );
835  
836 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
837 0 push( @re_init_test_data, { method => 'PUT', url => $self->_list_url($item), content => $body } );
838 } else {
839 0 local $@;
840 0 ▶ eval { $self->_request( 'PUT', $self->_list_url($item), $body ); 1; } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->_request("PUT", $self->_list_url($item), $body); 1 } } or do { $self->{"error"} = 13; $self->{"errorString"} = 'banning "' . $item . '" failed... ' . $@; $self->warn } 0 0 0
841 0 $self->{error} = 13;
842 0 $self->{errorString} = 'banning "' . $item . '" failed... ' . $@;
843 0 $self->warn;
844 };
845 }
846 } ## end foreach my $item ( keys( %{ ...}))
847  
848 # CIDR ranges are re-added the same way, keyed off the family of the address
849 0 foreach my $item ( keys( %{ $self->{banned_cidr} } ) ) {
850 0 my $addr = $self->_cidr_addr($item);
851 0 my $body = $self->_json->encode( { list => $self->_list_name($addr), address => $item } );
852  
853 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
854 0 push( @re_init_test_data, { method => 'PUT', url => $self->_list_url($addr), content => $body } );
855 } else {
856 0 local $@;
857 0 ▶ eval { $self->_request( 'PUT', $self->_list_url($addr), $body ); 1; } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->_request("PUT", $self->_list_url($addr), $body); 1 } } or do { $self->{"error"} = 33; $self->{"errorString"} = 'banning "' . $item . '" failed... ' . $@; $self->warn } 0 0 0
858 0 $self->{error} = 33;
859 0 $self->{errorString} = 'banning "' . $item . '" failed... ' . $@;
860 0 $self->warn;
861 };
862 }
863 } ## end foreach my $item ( keys( %{ ...}))
864  
865 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
866 0 $self->{frontend_obj}->{test_data} = \@re_init_test_data;
867 }
868  
869 0 $self->{inited} = 1;
870 } ## end sub re_init
871  
872 =head2 teardown
873  
874 Tears down the setup by removing the address-list entry for each currently
875 banned IP. The internal list of bans is kept, so a following re_init will
876 re-add them.
877  
878 =cut
879  
880 sub teardown {
881 0 ▶ my ( $self, %opts ) = @_;
Subroutine teardown: not called CC=5 SCAR=34.0
Pod documented
882  
883 0 $self->errorblank;
884  
885 0 $self->{inited} = 0;
886  
887 0 my @requests;
888 0 foreach my $item ( sort( keys( %{ $self->{banned} } ) ) ) {
889 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
890 0 push(
891 @requests,
892 { method => 'GET', url => $self->_unban_lookup_url($item) },
893 { method => 'DELETE', url => $self->_list_url($item) . '/<id>' }
894 );
895 } else {
896 0 local $@;
897 0 ▶ eval { $self->_unban_ip($item); 1; } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->_unban_ip($item); 1 } } or do { $self->{"error"} = 17; $self->{"errorString"} = 'teardown failed removing the entry for "' . $item . '"... ' . $@; $self->warn } 0 0 0
898 0 $self->{error} = 17;
899 0 $self->{errorString} = 'teardown failed removing the entry for "' . $item . '"... ' . $@;
900 0 $self->warn;
901 };
902 }
903 } ## end foreach my $item ( sort( keys( %{ $self->{banned...})))
904  
905 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
906 0 $self->{frontend_obj}->{test_data} = \@requests;
907 }
908 } ## end sub teardown
909  
910 =head2 stop
911  
912 Alias for L</teardown>.
913  
914 =cut
915  
916 sub stop {
917 0 ▶ my ( $self, %opts ) = @_;
Subroutine stop: not called CC=1 SCAR=6.9
Pod documented
918  
919 0 return $self->teardown(%opts);
920 }
921  
922 =head2 check
923  
924 Verifies the endpoint and credentials are still usable by fetching the
925 address-list. Returns a true value if so and a false value otherwise.
926  
927 =cut
928  
929 sub check {
930 6 ▶ my ( $self, %opts ) = @_;
Subroutine check: called CC=3 SCAR=14.7
Pod documented
931  
932 6 $self->errorblank;
933  
934 6 my $url = $self->_list_url . '?list=' . $self->_uri_escape( $self->{options}{list4} );
935  
936 6 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 6 0 6
937 6 $self->{frontend_obj}->{test_data} = [ { method => 'GET', url => $url } ];
938 6 return 1;
939 }
940  
941 0 local $@;
942 0 ▶ eval { $self->_request( 'GET', $url ); 1; } or return 0;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->_request("GET", $url); 1 } } or return 0 0 0 0
943  
944 0 return 1;
945 } ## end sub check
946  
947 =head2 flush
948  
949 Removes all currently banned IPs and CIDR ranges at once by removing their
950 address-list entries and forgetting them.
951  
952 $backend->flush;
953  
954 =cut
955  
956 sub flush {
957 0 ▶ my ( $self, %opts ) = @_;
Subroutine flush: not called CC=9 SCAR=45.0
Pod documented
958  
959 0 $self->errorblank;
960  
961 0 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 0 0
962 0 $self->{error} = 1;
963 0 $self->{errorString} = 'backend has not been inited';
964 0 $self->warn;
965 0 return;
966 }
967  
968 0 my @requests;
969 0 foreach my $item ( sort( keys( %{ $self->{banned} } ) ) ) {
970 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
971 0 push(
972 @requests,
973 { method => 'GET', url => $self->_unban_lookup_url($item) },
974 { method => 'DELETE', url => $self->_list_url($item) . '/<id>' }
975 );
976 } else {
977 0 local $@;
978 0 ▶ eval { $self->_unban_ip($item); 1; } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->_unban_ip($item); 1 } } or do { $self->{"error"} = 25; $self->{"errorString"} = 'flush failed removing the entry for "' . $item . '"... ' . $@; $self->warn } 0 0 0
979 0 $self->{error} = 25;
980 0 $self->{errorString} = 'flush failed removing the entry for "' . $item . '"... ' . $@;
981 0 $self->warn;
982 };
983 }
984 } ## end foreach my $item ( sort( keys( %{ $self->{banned...})))
985  
986 # CIDR ranges are removed the same way as single IPs
987 0 foreach my $item ( sort( keys( %{ $self->{banned_cidr} } ) ) ) {
988 0 my $addr = $self->_cidr_addr($item);
989 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 0 0 0
990 0 push(
991 @requests,
992 {
993 method => 'GET',
994 url => $self->_list_url($addr)
995 . '?list='
996 . $self->_uri_escape( $self->_list_name($addr) )
997 . '&address='
998 . $self->_uri_escape($item)
999 },
1000 { method => 'DELETE', url => $self->_list_url($addr) . '/<id>' }
1001 );
1002 } else {
1003 0 local $@;
1004 0 ▶ eval { $self->_unban_cidr_entry($item); 1; } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->_unban_cidr_entry($item); 1 } } or do { $self->{"error"} = 25; $self->{"errorString"} = 'flush failed removing the entry for "' . $item . '"... ' . $@; $self->warn } 0 0 0
1005 0 $self->{error} = 25;
1006 0 $self->{errorString} = 'flush failed removing the entry for "' . $item . '"... ' . $@;
1007 0 $self->warn;
1008 };
1009 }
1010 } ## end foreach my $item ( sort( keys( %{ $self->{banned_cidr...})))
1011  
1012 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
1013 0 $self->{frontend_obj}->{test_data} = \@requests;
1014 }
1015  
1016 0 $self->{banned} = {};
1017 0 $self->{banned_cidr} = {};
1018 } ## end sub flush
1019  
1020 =head1 ERROR CODES / FLAGS
1021  
1022 Error handling is provided by L<Error::Helper>. All
1023 errors are considered fatal.
1024  
1025 =head2 1, notInited
1026  
1027 The backend has not been inited yet.
1028  
1029 =head2 6, invalidPrefixSpecified
1030  
1031 The specified prefix did not match /^[a-zA-Z0-9]+$/.
1032  
1033 =head2 7, invalidName
1034  
1035 The name is either undef or does not match /^[a-zA-Z0-9\-]+$/.
1036  
1037 =head2 8, optionsNotHash
1038  
1039 The item passed to new for options is not a hash.
1040  
1041 =head2 9, noBanItem
1042  
1043 No IP or CIDR range specified to ban or unban.
1044  
1045 =head2 10, banItemNotIP
1046  
1047 The item to ban is not an IP. Either wrong ref type or regexp
1048 test using L<Regexp::IPv4> and L<Regexp::IPv6> failed.
1049  
1050 =head2 12, backendInitError
1051  
1052 Failed to init the backend.
1053  
1054 =head2 13, banFailed
1055  
1056 Failed to ban the item.
1057  
1058 =head2 14, unbanFailed
1059  
1060 Failed to unban the item.
1061  
1062 =head2 15, listFailed
1063  
1064 Failed to get a list of bans.
1065  
1066 =head2 16, reInitFailed
1067  
1068 Failed to re_init the backend.
1069  
1070 =head2 17, teardownFailed
1071  
1072 Failed to teardown the backend.
1073  
1074 =head2 18, alreadyInited
1075  
1076 init called, but the backend has already been inited.
1077  
1078 =head2 23, initFailed
1079  
1080 init failed. Probing the address-list endpoint failed.
1081  
1082 =head2 24, checkFailed
1083  
1084 The backend check raised an error.
1085  
1086 =head2 25, flushFailed
1087  
1088 Failed to flush the bans.
1089  
1090 =head2 26, portsNotSupported
1091  
1092 Ports were specified, but this backend manages whole IPs and does not
1093 support ports.
1094  
1095 =head2 27, protocolsNotSupported
1096  
1097 Protocols were specified, but this backend manages whole IPs and does not
1098 support protocols.
1099  
1100 =head2 30, hostNotDefined
1101  
1102 The option host is undef or blank.
1103  
1104 =head2 31, userNotDefined
1105  
1106 The option user is undef or blank.
1107  
1108 =head2 32, passwordNotDefined
1109  
1110 The option password is undef or blank.
1111  
1112 =head2 33, banCidrFailed
1113  
1114 Failed to ban the CIDR range.
1115  
1116 =head2 34, unbanCidrFailed
1117  
1118 Failed to unban the CIDR range.
1119  
1120 =head2 35, cidrItemNotCidr
1121  
1122 The item to ban is not a CIDR range. Either wrong ref type or it is not an
1123 IPv4 or IPv6 address followed by a prefix length valid for its family.
1124  
1125 =head2 36, cidrNotSupported
1126  
1127 The backend does not support CIDR bans.
1128  
1129 =head2 37, listCidrFailed
1130  
1131 Failed to get a list of CIDR bans.
1132  
1133 =head1 AUTHOR
1134  
1135 Zane C. Bowers-Hadley, C<< <vvelox at vvelox.ent> >>
1136  
1137 =head1 LICENSE AND COPYRIGHT
1138  
1139 This software is Copyright (c) 2023 by Zane C. Bowers-Hadley.
1140  
1141 This is free software, licensed under:
1142  
1143 The GNU Lesser General Public License, Version 2.1, February 1999
1144  
1145 =cut
1146  
1147 1 1; # End of Net::Firewall::BlockerHelper::backends::routeros_api
« BlockerHelper/backends/netscaler.pm ↑ Summary BlockerHelper/backends/juniper_srx.pm »