| 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 qw( Error::Helper Net::Firewall::BlockerHelper::Util ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use Regexp::IPv4 qw($IPv4_re); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use Regexp::IPv6 qw($IPv6_re); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||
| 10 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||
| 12 | Net::Firewall::BlockerHelper::backends::routeros_api - MikroTik RouterOS backend using the REST API. | ||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||
| 14 | =head1 VERSION | ||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||
| 16 | Version 0.2.0 | ||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||
| 18 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||
| 20 | 1 | our $VERSION = '0.2.0'; | |||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||
| 22 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||
| 24 | use Net::Firewall::BlockerHelper; | ||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||
| 26 | my $fw_helper = Net::Firewall::BlockerHelper->new( | ||||||||||||||||||||||||||||||||||||||||||
| 27 | backend => '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 | 6 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=28 SCAR=41.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 115 | 6 | my ( $blank, %opts ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||||||
| 117 | 6 | my $self = { | |||||||||||||||||||||||||||||||||||||||||
| 118 | perror => undef, | ||||||||||||||||||||||||||||||||||||||||||
| 119 | error => undef, | ||||||||||||||||||||||||||||||||||||||||||
| 120 | errorLine => undef, | ||||||||||||||||||||||||||||||||||||||||||
| 121 | errorFilename => undef, | ||||||||||||||||||||||||||||||||||||||||||
| 122 | errorString => "", | ||||||||||||||||||||||||||||||||||||||||||
| 123 | errorExtra => { | ||||||||||||||||||||||||||||||||||||||||||
| 124 | all_errors_fatal => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 125 | # all_fatal is what Error::Helper 2.1.0 actually checks; all_errors_fatal | ||||||||||||||||||||||||||||||||||||||||||
| 126 | # is kept for the name documented in its POD | ||||||||||||||||||||||||||||||||||||||||||
| 127 | all_fatal => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 128 | flags => { | ||||||||||||||||||||||||||||||||||||||||||
| 129 | 1 => 'notInited', | ||||||||||||||||||||||||||||||||||||||||||
| 130 | 6 => 'invalidPrefixSpecified', | ||||||||||||||||||||||||||||||||||||||||||
| 131 | 7 => 'invalidName', | ||||||||||||||||||||||||||||||||||||||||||
| 132 | 8 => 'optionsNotHash', | ||||||||||||||||||||||||||||||||||||||||||
| 133 | 9 => 'noBanItem', | ||||||||||||||||||||||||||||||||||||||||||
| 134 | 10 => 'banItemNotIP', | ||||||||||||||||||||||||||||||||||||||||||
| 135 | 12 => 'backendInitError', | ||||||||||||||||||||||||||||||||||||||||||
| 136 | 13 => 'banFailed', | ||||||||||||||||||||||||||||||||||||||||||
| 137 | 14 => 'unbanFailed', | ||||||||||||||||||||||||||||||||||||||||||
| 138 | 15 => 'listFailed', | ||||||||||||||||||||||||||||||||||||||||||
| 139 | 16 => 'reInitFailed', | ||||||||||||||||||||||||||||||||||||||||||
| 140 | 17 => 'teardownFailed', | ||||||||||||||||||||||||||||||||||||||||||
| 141 | 18 => 'alreadyInited', | ||||||||||||||||||||||||||||||||||||||||||
| 142 | 23 => 'initFailed', | ||||||||||||||||||||||||||||||||||||||||||
| 143 | 24 => 'checkFailed', | ||||||||||||||||||||||||||||||||||||||||||
| 144 | 25 => 'flushFailed', | ||||||||||||||||||||||||||||||||||||||||||
| 145 | 26 => 'portsNotSupported', | ||||||||||||||||||||||||||||||||||||||||||
| 146 | 27 => 'protocolsNotSupported', | ||||||||||||||||||||||||||||||||||||||||||
| 147 | 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
Condition50%3/6
defined $opts{"ports"} and ref $opts{"ports"} eq "ARRAY" and defined $opts{"ports"}[0]
defined $opts{"ports"} and ref $opts{"ports"} eq "ARRAY"
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]
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition33%2/6
defined $opts{"protocols"} and ref $opts{"protocols"} eq "ARRAY" and defined $opts{"protocols"}[0]
defined $opts{"protocols"} and ref $opts{"protocols"} eq "ARRAY"
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]
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition33%1/3
defined $opts{"prefix"} and not $opts{"prefix"} =~ /^[a-zA-Z0-9]+$/
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]+$/
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 215 | 5 | $self->{testing} = $opts{testing}; | |||||||||||||||||||||||||||||||||||||||||
| 216 | } | ||||||||||||||||||||||||||||||||||||||||||
| 217 | 5 | ▶ | if ( defined( $opts{frontend_obj} ) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 218 | 5 | $self->{frontend_obj} = $opts{frontend_obj}; | |||||||||||||||||||||||||||||||||||||||||
| 219 | } | ||||||||||||||||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||||||||||||||||
| 221 | 5 | ▶ | if ( defined( $opts{options} ) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 222 | 5 | ▶ | if ( ref( $opts{options} ) ne 'HASH' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
not defined $self->{"options"}{"host"} or $self->{"options"}{"host"} eq ""
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 ""
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
not defined $self->{"options"}{"user"} or $self->{"options"}{"user"} eq ""
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 ""
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
not defined $self->{"options"}{"password"} or $self->{"options"}{"password"} eq ""
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 ""
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 253 | 2 | ▶ | $self->{options}{insecure} = 0 if ( !defined( $self->{options}{insecure} ) ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 254 | 2 | ▶ | $self->{options}{timeout} = 30 if ( !defined( $self->{options}{timeout} ) ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 255 | 2 | ▶ | if ( defined( $self->{name} ) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 256 | 2 | ▶ | $self->{options}{list4} = $self->{prefix} . '_' . $self->{name} if ( !defined( $self->{options}{list4} ) ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 257 | 2 | ▶ | $self->{options}{list6} = $self->{prefix} . '_' . $self->{name} if ( !defined( $self->{options}{list6} ) ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 258 | } | ||||||||||||||||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||||||||||||||||
| 260 | 2 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 261 | } ## end sub new | ||||||||||||||||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||||||||||||||||
| 263 | # Internal helper. Returns which configured address-list an address belongs | ||||||||||||||||||||||||||||||||||||||||||
| 264 | # in. | ||||||||||||||||||||||||||||||||||||||||||
| 265 | # | ||||||||||||||||||||||||||||||||||||||||||
| 266 | # RouterOS keeps IPv4 and IPv6 address-lists under separate menus, so a name | ||||||||||||||||||||||||||||||||||||||||||
| 267 | # is looked up per family. Note the two default to the same value, prefix and | ||||||||||||||||||||||||||||||||||||||||||
| 268 | # name joined, since the menus already keep them apart; they only differ if | ||||||||||||||||||||||||||||||||||||||||||
| 269 | # the list4 and list6 options are set explicitly. | ||||||||||||||||||||||||||||||||||||||||||
| 270 | # | ||||||||||||||||||||||||||||||||||||||||||
| 271 | # Args: | ||||||||||||||||||||||||||||||||||||||||||
| 272 | # | ||||||||||||||||||||||||||||||||||||||||||
| 273 | # ip - The address whose list is wanted, as a plain string. Expected to be | ||||||||||||||||||||||||||||||||||||||||||
| 274 | # an already validated and lowercased IPv4 or IPv6 address. Matched | ||||||||||||||||||||||||||||||||||||||||||
| 275 | # against $IPv4_re, so anything that is not valid IPv4 selects the | ||||||||||||||||||||||||||||||||||||||||||
| 276 | # IPv6 list. Ranges must have their address extracted with | ||||||||||||||||||||||||||||||||||||||||||
| 277 | # _cidr_addr first. | ||||||||||||||||||||||||||||||||||||||||||
| 278 | # | ||||||||||||||||||||||||||||||||||||||||||
| 279 | # Returns the list name as a plain string, from either the list4 or the list6 | ||||||||||||||||||||||||||||||||||||||||||
| 280 | # option. | ||||||||||||||||||||||||||||||||||||||||||
| 281 | # | ||||||||||||||||||||||||||||||||||||||||||
| 282 | # # with both defaulting from prefix "kur" and name "ssh" | ||||||||||||||||||||||||||||||||||||||||||
| 283 | # $self->_list_name('10.0.0.1'); # kur_ssh | ||||||||||||||||||||||||||||||||||||||||||
| 284 | # $self->_list_name('2001:db8::1'); # kur_ssh | ||||||||||||||||||||||||||||||||||||||||||
| 285 | 6 | ▶ | sub _list_name { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_list_name: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 286 | 6 | my ( $self, $ip ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||||||||||||||||
| 288 | 6 | ▶ | return ( $ip =~ /\A$IPv4_re\z/ ) ? $self->{options}{list4} : $self->{options}{list6}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 289 | } | ||||||||||||||||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||||||||||||||||
| 291 | # Internal helper. Returns the REST endpoint of the address-list menu for an | ||||||||||||||||||||||||||||||||||||||||||
| 292 | # address's family. | ||||||||||||||||||||||||||||||||||||||||||
| 293 | # | ||||||||||||||||||||||||||||||||||||||||||
| 294 | # RouterOS exposes IPv4 and IPv6 firewall configuration under different menus, | ||||||||||||||||||||||||||||||||||||||||||
| 295 | # ip and ipv6, so the family is part of the path rather than a parameter. | ||||||||||||||||||||||||||||||||||||||||||
| 296 | # | ||||||||||||||||||||||||||||||||||||||||||
| 297 | # Note the family test here matches against $IPv6_re and falls back to IPv4, | ||||||||||||||||||||||||||||||||||||||||||
| 298 | # which is the opposite way round to _list_name in the same file. That is what | ||||||||||||||||||||||||||||||||||||||||||
| 299 | # makes the argument optional: called with no address at all it yields the | ||||||||||||||||||||||||||||||||||||||||||
| 300 | # IPv4 endpoint, which is what the paths that just need somewhere to talk to | ||||||||||||||||||||||||||||||||||||||||||
| 301 | # use. | ||||||||||||||||||||||||||||||||||||||||||
| 302 | # | ||||||||||||||||||||||||||||||||||||||||||
| 303 | # Args: | ||||||||||||||||||||||||||||||||||||||||||
| 304 | # | ||||||||||||||||||||||||||||||||||||||||||
| 305 | # ip - Optional address whose family selects the menu, as a plain string. | ||||||||||||||||||||||||||||||||||||||||||
| 306 | # Expected to be an already validated and lowercased IPv4 or IPv6 | ||||||||||||||||||||||||||||||||||||||||||
| 307 | # address. When omitted or undef, the IPv4 menu is used. | ||||||||||||||||||||||||||||||||||||||||||
| 308 | # | ||||||||||||||||||||||||||||||||||||||||||
| 309 | # Returns the endpoint as a string, with no trailing slash. Callers append a | ||||||||||||||||||||||||||||||||||||||||||
| 310 | # query string for lookups or an entry id for deletes. | ||||||||||||||||||||||||||||||||||||||||||
| 311 | # | ||||||||||||||||||||||||||||||||||||||||||
| 312 | # $self->_list_url('10.0.0.1'); | ||||||||||||||||||||||||||||||||||||||||||
| 313 | # # https://router.example.org/rest/ip/firewall/address-list | ||||||||||||||||||||||||||||||||||||||||||
| 314 | # | ||||||||||||||||||||||||||||||||||||||||||
| 315 | # $self->_list_url('2001:db8::1'); | ||||||||||||||||||||||||||||||||||||||||||
| 316 | # # https://router.example.org/rest/ipv6/firewall/address-list | ||||||||||||||||||||||||||||||||||||||||||
| 317 | # | ||||||||||||||||||||||||||||||||||||||||||
| 318 | # # with no address, defaulting to IPv4 | ||||||||||||||||||||||||||||||||||||||||||
| 319 | # $self->_list_url; | ||||||||||||||||||||||||||||||||||||||||||
| 320 | # # https://router.example.org/rest/ip/firewall/address-list | ||||||||||||||||||||||||||||||||||||||||||
| 321 | 16 | ▶ | sub _list_url { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_list_url: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||
| 322 | 16 | my ( $self, $ip ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||||||||||||||||
| 324 | 16 | ▶ | my $menu = ( defined($ip) && $ip =~ /\A$IPv6_re\z/ ) ? 'ipv6' : 'ip'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $ip and $ip =~ /\A$Net::Firewall::BlockerHelper::backends::routeros_api::IPv6_re\z/
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/
| |||||||||||||||||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||||||||||||||||
| 326 | 16 | return $self->{options}{scheme} . '://' . $self->{options}{host} . '/rest/' . $menu . '/firewall/address-list'; | |||||||||||||||||||||||||||||||||||||||||
| 327 | } | ||||||||||||||||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||||||||||||||||
| 329 | # Internal helper. Performs one HTTP request against the RouterOS REST API. | ||||||||||||||||||||||||||||||||||||||||||
| 330 | # Every call this backend makes to the router goes through here. | ||||||||||||||||||||||||||||||||||||||||||
| 331 | # | ||||||||||||||||||||||||||||||||||||||||||
| 332 | # The user agent is built on first use and cached on the object, so a run of | ||||||||||||||||||||||||||||||||||||||||||
| 333 | # requests shares one agent. LWP::UserAgent is loaded with require at that | ||||||||||||||||||||||||||||||||||||||||||
| 334 | # point rather than at compile time, since only the HTTP backends need it; | ||||||||||||||||||||||||||||||||||||||||||
| 335 | # failing to load it dies with an explanation naming this backend. The | ||||||||||||||||||||||||||||||||||||||||||
| 336 | # insecure option turns off certificate verification, which is there because a | ||||||||||||||||||||||||||||||||||||||||||
| 337 | # RouterOS device commonly presents a self signed certificate. | ||||||||||||||||||||||||||||||||||||||||||
| 338 | # | ||||||||||||||||||||||||||||||||||||||||||
| 339 | # Authentication is HTTP basic, encoded per request; there is no session to | ||||||||||||||||||||||||||||||||||||||||||
| 340 | # establish. | ||||||||||||||||||||||||||||||||||||||||||
| 341 | # | ||||||||||||||||||||||||||||||||||||||||||
| 342 | # Any HTTP level failure dies rather than setting an error. The callers wrap | ||||||||||||||||||||||||||||||||||||||||||
| 343 | # this in eval and turn the exception into the appropriate error code, which | ||||||||||||||||||||||||||||||||||||||||||
| 344 | # is what lets one helper serve paths that report ban, unban, and teardown | ||||||||||||||||||||||||||||||||||||||||||
| 345 | # failures differently. | ||||||||||||||||||||||||||||||||||||||||||
| 346 | # | ||||||||||||||||||||||||||||||||||||||||||
| 347 | # A body that fails to decode as JSON is not fatal; the decode runs inside its | ||||||||||||||||||||||||||||||||||||||||||
| 348 | # own eval and leaves the result undef. | ||||||||||||||||||||||||||||||||||||||||||
| 349 | # | ||||||||||||||||||||||||||||||||||||||||||
| 350 | # Never called in testing mode; those paths record the request instead. | ||||||||||||||||||||||||||||||||||||||||||
| 351 | # | ||||||||||||||||||||||||||||||||||||||||||
| 352 | # Args: | ||||||||||||||||||||||||||||||||||||||||||
| 353 | # | ||||||||||||||||||||||||||||||||||||||||||
| 354 | # method - The HTTP method, as a plain string: 'GET' for the entry | ||||||||||||||||||||||||||||||||||||||||||
| 355 | # lookups, 'PUT' to add an entry, 'DELETE' to remove one. | ||||||||||||||||||||||||||||||||||||||||||
| 356 | # | ||||||||||||||||||||||||||||||||||||||||||
| 357 | # url - The full URL to request, as a plain string, built on | ||||||||||||||||||||||||||||||||||||||||||
| 358 | # _list_url. | ||||||||||||||||||||||||||||||||||||||||||
| 359 | # | ||||||||||||||||||||||||||||||||||||||||||
| 360 | # body - Optional request body, as an already encoded JSON string. undef | ||||||||||||||||||||||||||||||||||||||||||
| 361 | # for the lookups and deletes, which carry everything in the URL. | ||||||||||||||||||||||||||||||||||||||||||
| 362 | # | ||||||||||||||||||||||||||||||||||||||||||
| 363 | # Returns the decoded response body as whatever structure the JSON held. Note | ||||||||||||||||||||||||||||||||||||||||||
| 364 | # that for the lookups this is an arrayref of matching entries rather than a | ||||||||||||||||||||||||||||||||||||||||||
| 365 | # hashref, which is why the callers check the reference type. undef when the | ||||||||||||||||||||||||||||||||||||||||||
| 366 | # response body was empty or did not parse. Dies on any non success HTTP | ||||||||||||||||||||||||||||||||||||||||||
| 367 | # status. | ||||||||||||||||||||||||||||||||||||||||||
| 368 | # | ||||||||||||||||||||||||||||||||||||||||||
| 369 | # my $decoded = $self->_request( 'GET', $self->_unban_lookup_url($ip) ); | ||||||||||||||||||||||||||||||||||||||||||
| 370 | # | ||||||||||||||||||||||||||||||||||||||||||
| 371 | # # the usual shape at the call sites | ||||||||||||||||||||||||||||||||||||||||||
| 372 | # eval { $self->_request( 'PUT', $self->_list_url($ip), $body ); }; | ||||||||||||||||||||||||||||||||||||||||||
| 373 | # if ($@) { ... raise banFailed ... } | ||||||||||||||||||||||||||||||||||||||||||
| 374 | 0 | ▶ | sub _request { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_request: not called CC=8 SCAR=42.8
| |||||||||||||||||||||||||||||||||||||||||||
| 375 | 0 | my ( $self, $method, $url, $body ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 376 | |||||||||||||||||||||||||||||||||||||||||||
| 377 | 0 | ▶ | if ( !defined( $self->{ua} ) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 378 | 0 | local $@; | |||||||||||||||||||||||||||||||||||||||||
| 379 | 0 | ▶ | eval { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 380 | 0 | require LWP::UserAgent; | |||||||||||||||||||||||||||||||||||||||||
| 381 | my %args = ( | ||||||||||||||||||||||||||||||||||||||||||
| 382 | agent => 'Net::Firewall::BlockerHelper/' . $VERSION, | ||||||||||||||||||||||||||||||||||||||||||
| 383 | timeout => $self->{options}{timeout}, | ||||||||||||||||||||||||||||||||||||||||||
| 384 | 0 | ); | |||||||||||||||||||||||||||||||||||||||||
| 385 | 0 | ▶ | if ( $self->{options}{insecure} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 386 | 0 | $args{ssl_opts} = { verify_hostname => 0, SSL_verify_mode => 0 }; | |||||||||||||||||||||||||||||||||||||||||
| 387 | } | ||||||||||||||||||||||||||||||||||||||||||
| 388 | 0 | $self->{ua} = LWP::UserAgent->new(%args); | |||||||||||||||||||||||||||||||||||||||||
| 389 | 0 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 390 | } or die( 'failed to load LWP::UserAgent, which the routeros_api backend requires... ' . $@ ); | ||||||||||||||||||||||||||||||||||||||||||
| 391 | } | ||||||||||||||||||||||||||||||||||||||||||
| 392 | |||||||||||||||||||||||||||||||||||||||||||
| 393 | 0 | require MIME::Base64; | |||||||||||||||||||||||||||||||||||||||||
| 394 | 0 | my $auth = 'Basic ' . MIME::Base64::encode_base64( $self->{options}{user} . ':' . $self->{options}{password}, '' ); | |||||||||||||||||||||||||||||||||||||||||
| 395 | |||||||||||||||||||||||||||||||||||||||||||
| 396 | 0 | my @headers = ( 'Content-Type' => 'application/json', 'Authorization' => $auth ); | |||||||||||||||||||||||||||||||||||||||||
| 397 | |||||||||||||||||||||||||||||||||||||||||||
| 398 | 0 | require HTTP::Request; | |||||||||||||||||||||||||||||||||||||||||
| 399 | 0 | my $request = HTTP::Request->new( $method, $url, \@headers, $body ); | |||||||||||||||||||||||||||||||||||||||||
| 400 | |||||||||||||||||||||||||||||||||||||||||||
| 401 | 0 | my $response = $self->{ua}->request($request); | |||||||||||||||||||||||||||||||||||||||||
| 402 | |||||||||||||||||||||||||||||||||||||||||||
| 403 | 0 | ▶ | if ( !$response->is_success ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 404 | 0 | my $detail = $response->decoded_content; | |||||||||||||||||||||||||||||||||||||||||
| 405 | 0 | ▶ | $detail = defined($detail) ? ( ' body... ' . $detail ) : ''; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 406 | 0 | die( $method . ' ' . $url . ' failed... HTTP status... ' . $response->status_line . $detail ); | |||||||||||||||||||||||||||||||||||||||||
| 407 | } | ||||||||||||||||||||||||||||||||||||||||||
| 408 | |||||||||||||||||||||||||||||||||||||||||||
| 409 | 0 | my $decoded; | |||||||||||||||||||||||||||||||||||||||||
| 410 | 0 | my $content = $response->decoded_content; | |||||||||||||||||||||||||||||||||||||||||
| 411 | 0 | ▶ | if ( defined($content) && $content ne '' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $content and $content ne ""
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 ""
| |||||||||||||||||||||||||||||||||||||||||||
| 412 | 0 | local $@; | |||||||||||||||||||||||||||||||||||||||||
| 413 | 0 | eval { $decoded = $self->_json->decode($content); }; | |||||||||||||||||||||||||||||||||||||||||
| 414 | } | ||||||||||||||||||||||||||||||||||||||||||
| 415 | |||||||||||||||||||||||||||||||||||||||||||
| 416 | 0 | return $decoded; | |||||||||||||||||||||||||||||||||||||||||
| 417 | } ## end sub _request | ||||||||||||||||||||||||||||||||||||||||||
| 418 | |||||||||||||||||||||||||||||||||||||||||||
| 419 | =head2 init | ||||||||||||||||||||||||||||||||||||||||||
| 420 | |||||||||||||||||||||||||||||||||||||||||||
| 421 | Initiates the backend. Verifies the credentials and reachability by | ||||||||||||||||||||||||||||||||||||||||||
| 422 | performing a GET against the IPv4 address-list endpoint. Nothing is created | ||||||||||||||||||||||||||||||||||||||||||
| 423 | on the router; the referencing firewall rule must already exist. | ||||||||||||||||||||||||||||||||||||||||||
| 424 | |||||||||||||||||||||||||||||||||||||||||||
| 425 | $backend->init; | ||||||||||||||||||||||||||||||||||||||||||
| 426 | |||||||||||||||||||||||||||||||||||||||||||
| 427 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 428 | |||||||||||||||||||||||||||||||||||||||||||
| 429 | 2 | ▶ | sub init { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
init: called CC=4 SCAR=20.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 430 | 2 | my ( $self, %opts ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 431 | |||||||||||||||||||||||||||||||||||||||||||
| 432 | 2 | $self->errorblank; | |||||||||||||||||||||||||||||||||||||||||
| 433 | |||||||||||||||||||||||||||||||||||||||||||
| 434 | 2 | ▶ | if ( $self->{inited} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 435 | 0 | $self->{error} = 18; | |||||||||||||||||||||||||||||||||||||||||
| 436 | 0 | $self->{errorString} = 'backend has already been inited'; | |||||||||||||||||||||||||||||||||||||||||
| 437 | 0 | $self->warn; | |||||||||||||||||||||||||||||||||||||||||
| 438 | } | ||||||||||||||||||||||||||||||||||||||||||
| 439 | |||||||||||||||||||||||||||||||||||||||||||
| 440 | 2 | my $url = $self->_list_url . '?list=' . $self->_uri_escape( $self->{options}{list4} ); | |||||||||||||||||||||||||||||||||||||||||
| 441 | |||||||||||||||||||||||||||||||||||||||||||
| 442 | 2 | ▶ | if ( $self->{testing} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 443 | 2 | $self->{frontend_obj}->{test_data} = [ { method => 'GET', url => $url } ]; | |||||||||||||||||||||||||||||||||||||||||
| 444 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 445 | 0 | local $@; | |||||||||||||||||||||||||||||||||||||||||
| 446 | 0 | ▶ | eval { $self->_request( 'GET', $url ); 1; } or do { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 447 | 0 | $self->{error} = 23; | |||||||||||||||||||||||||||||||||||||||||
| 448 | 0 | $self->{errorString} = 'init failed. probing the address-list endpoint failed... ' . $@; | |||||||||||||||||||||||||||||||||||||||||
| 449 | 0 | $self->warn; | |||||||||||||||||||||||||||||||||||||||||
| 450 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 451 | } | ||||||||||||||||||||||||||||||||||||||||||
| 452 | |||||||||||||||||||||||||||||||||||||||||||
| 453 | 2 | $self->{inited} = 1; | |||||||||||||||||||||||||||||||||||||||||
| 454 | } ## end sub init | ||||||||||||||||||||||||||||||||||||||||||
| 455 | |||||||||||||||||||||||||||||||||||||||||||
| 456 | =head2 ban | ||||||||||||||||||||||||||||||||||||||||||
| 457 | |||||||||||||||||||||||||||||||||||||||||||
| 458 | Bans an IP. The value of ban is validated as being a IPv4 or IPv6 address | ||||||||||||||||||||||||||||||||||||||||||
| 459 | and lowercased, then added to the relevant address-list via a PUT to | ||||||||||||||||||||||||||||||||||||||||||
| 460 | C<< /rest/ip/firewall/address-list >> or | ||||||||||||||||||||||||||||||||||||||||||
| 461 | C<< /rest/ipv6/firewall/address-list >>. Banning an already banned IP is a | ||||||||||||||||||||||||||||||||||||||||||
| 462 | noop. | ||||||||||||||||||||||||||||||||||||||||||
| 463 | |||||||||||||||||||||||||||||||||||||||||||
| 464 | $backend->ban(ban => $ip); | ||||||||||||||||||||||||||||||||||||||||||
| 465 | |||||||||||||||||||||||||||||||||||||||||||
| 466 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 467 | |||||||||||||||||||||||||||||||||||||||||||
| 468 | 3 | ▶ | sub ban { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
ban: called CC=10 SCAR=37.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 469 | 3 | my ( $self, %opts ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 470 | |||||||||||||||||||||||||||||||||||||||||||
| 471 | 3 | $self->errorblank; | |||||||||||||||||||||||||||||||||||||||||
| 472 | |||||||||||||||||||||||||||||||||||||||||||
| 473 | 3 | ▶ | if ( !$self->{inited} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 474 | 0 | $self->{error} = 1; | |||||||||||||||||||||||||||||||||||||||||
| 475 | 0 | $self->{errorString} = 'backend has not been inited'; | |||||||||||||||||||||||||||||||||||||||||
| 476 | 0 | $self->warn; | |||||||||||||||||||||||||||||||||||||||||
| 477 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 478 | } | ||||||||||||||||||||||||||||||||||||||||||
| 479 | |||||||||||||||||||||||||||||||||||||||||||
| 480 | 3 | ▶ | if ( !defined( $opts{ban} ) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 481 | 0 | $self->{error} = 9; | |||||||||||||||||||||||||||||||||||||||||
| 482 | 0 | $self->{errorString} = 'Nothing specified for the value ban'; | |||||||||||||||||||||||||||||||||||||||||
| 483 | 0 | $self->warn; | |||||||||||||||||||||||||||||||||||||||||
| 484 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 485 | } elsif ( ref( $opts{ban} ) ne '' ) { | ||||||||||||||||||||||||||||||||||||||||||
| 486 | 0 | $self->{error} = 10; | |||||||||||||||||||||||||||||||||||||||||
| 487 | 0 | $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"'; | |||||||||||||||||||||||||||||||||||||||||
| 488 | 0 | $self->warn; | |||||||||||||||||||||||||||||||||||||||||
| 489 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 490 | ▶ | } 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/
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/
| |||||||||||||||||||||||||||||||||||||||||||
| 491 | && $opts{ban} !~ /\A$IPv6_re\z/ ) | ||||||||||||||||||||||||||||||||||||||||||
| 492 | { | ||||||||||||||||||||||||||||||||||||||||||
| 493 | 0 | $self->{error} = 10; | |||||||||||||||||||||||||||||||||||||||||
| 494 | 0 | $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 IP'; | |||||||||||||||||||||||||||||||||||||||||
| 495 | 0 | $self->warn; | |||||||||||||||||||||||||||||||||||||||||
| 496 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 497 | } | ||||||||||||||||||||||||||||||||||||||||||
| 498 | |||||||||||||||||||||||||||||||||||||||||||
| 499 | # lowercase so the same IPv6 IP in differing cases can't result in duplicate entries | ||||||||||||||||||||||||||||||||||||||||||
| 500 | 3 | $opts{ban} = lc( $opts{ban} ); | |||||||||||||||||||||||||||||||||||||||||
| 501 | |||||||||||||||||||||||||||||||||||||||||||
| 502 | 3 | ▶ | if ( $self->{banned}{ $opts{ban} } ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 503 | 0 | ▶ | if ( $self->{testing} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 504 | 0 | $self->{frontend_obj}->{test_data} = 'already banned'; | |||||||||||||||||||||||||||||||||||||||||
| 505 | } | ||||||||||||||||||||||||||||||||||||||||||
| 506 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 507 | } | ||||||||||||||||||||||||||||||||||||||||||
| 508 | |||||||||||||||||||||||||||||||||||||||||||
| 509 | 3 | my $body = $self->_json->encode( { list => $self->_list_name( $opts{ban} ), address => $opts{ban} } ); | |||||||||||||||||||||||||||||||||||||||||
| 510 | |||||||||||||||||||||||||||||||||||||||||||
| 511 | 3 | ▶ | if ( $self->{testing} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 512 | 3 | $self->{frontend_obj}->{test_data} = [ { method => 'PUT', url => $self->_list_url( $opts{ban} ), content => $body } ]; | |||||||||||||||||||||||||||||||||||||||||
| 513 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 514 | 0 | local $@; | |||||||||||||||||||||||||||||||||||||||||
| 515 | 0 | ▶ | eval { $self->_request( 'PUT', $self->_list_url( $opts{ban} ), $body ); 1; } or do { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 516 | 0 | $self->{error} = 13; | |||||||||||||||||||||||||||||||||||||||||
| 517 | 0 | $self->{errorString} = 'banning "' . $opts{ban} . '" failed... ' . $@; | |||||||||||||||||||||||||||||||||||||||||
| 518 | 0 | $self->warn; | |||||||||||||||||||||||||||||||||||||||||
| 519 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 520 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 521 | } | ||||||||||||||||||||||||||||||||||||||||||
| 522 | |||||||||||||||||||||||||||||||||||||||||||
| 523 | 3 | $self->{banned}{ $opts{ban} } = 1; | |||||||||||||||||||||||||||||||||||||||||
| 524 | } ## end sub ban | ||||||||||||||||||||||||||||||||||||||||||
| 525 | |||||||||||||||||||||||||||||||||||||||||||
| 526 | # Internal helper. Returns the URL that finds the address-list entry for one | ||||||||||||||||||||||||||||||||||||||||||
| 527 | # address. | ||||||||||||||||||||||||||||||||||||||||||
| 528 | # | ||||||||||||||||||||||||||||||||||||||||||
| 529 | # This exists because RouterOS entries are addressed by an opaque internal id | ||||||||||||||||||||||||||||||||||||||||||
| 530 | # rather than by their contents. There is no way to delete "the entry for | ||||||||||||||||||||||||||||||||||||||||||
| 531 | # 10.0.0.1"; the id has to be looked up first and then deleted. That is why | ||||||||||||||||||||||||||||||||||||||||||
| 532 | # unbanning here takes two round trips while banning takes one. | ||||||||||||||||||||||||||||||||||||||||||
| 533 | # | ||||||||||||||||||||||||||||||||||||||||||
| 534 | # The query filters on both the list name and the address, so an entry | ||||||||||||||||||||||||||||||||||||||||||
| 535 | # belonging to another instance's list on the same router is not matched. | ||||||||||||||||||||||||||||||||||||||||||
| 536 | # | ||||||||||||||||||||||||||||||||||||||||||
| 537 | # Args: | ||||||||||||||||||||||||||||||||||||||||||
| 538 | # | ||||||||||||||||||||||||||||||||||||||||||
| 539 | # ip - The address whose entry is wanted, as a plain string. Expected to | ||||||||||||||||||||||||||||||||||||||||||
| 540 | # be an already validated and lowercased IPv4 or IPv6 address. The | ||||||||||||||||||||||||||||||||||||||||||
| 541 | # family selects both the menu and the list name. | ||||||||||||||||||||||||||||||||||||||||||
| 542 | # | ||||||||||||||||||||||||||||||||||||||||||
| 543 | # Returns the URL as a string, for a GET that answers with an array of | ||||||||||||||||||||||||||||||||||||||||||
| 544 | # matching entries. | ||||||||||||||||||||||||||||||||||||||||||
| 545 | # | ||||||||||||||||||||||||||||||||||||||||||
| 546 | # $self->_unban_lookup_url('10.0.0.1'); | ||||||||||||||||||||||||||||||||||||||||||
| 547 | # # https://router.example.org/rest/ip/firewall/address-list?list=kur_ssh&address=10.0.0.1 | ||||||||||||||||||||||||||||||||||||||||||
| 548 | # | ||||||||||||||||||||||||||||||||||||||||||
| 549 | # $self->_unban_lookup_url('2001:db8::1'); | ||||||||||||||||||||||||||||||||||||||||||
| 550 | # # https://router.example.org/rest/ipv6/firewall/address-list?list=kur_ssh&address=2001%3Adb8%3A%3A1 | ||||||||||||||||||||||||||||||||||||||||||
| 551 | 2 | ▶ | sub _unban_lookup_url { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_unban_lookup_url: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 552 | 2 | my ( $self, $ip ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 553 | |||||||||||||||||||||||||||||||||||||||||||
| 554 | return | ||||||||||||||||||||||||||||||||||||||||||
| 555 | 2 | $self->_list_url($ip) | |||||||||||||||||||||||||||||||||||||||||
| 556 | . '?list=' | ||||||||||||||||||||||||||||||||||||||||||
| 557 | . $self->_uri_escape( $self->_list_name($ip) ) | ||||||||||||||||||||||||||||||||||||||||||
| 558 | . '&address=' | ||||||||||||||||||||||||||||||||||||||||||
| 559 | . $self->_uri_escape($ip); | ||||||||||||||||||||||||||||||||||||||||||
| 560 | } ## end sub _unban_lookup_url | ||||||||||||||||||||||||||||||||||||||||||
| 561 | |||||||||||||||||||||||||||||||||||||||||||
| 562 | # Internal helper. Performs the full two step unban of one address: find its | ||||||||||||||||||||||||||||||||||||||||||
| 563 | # address-list entry, then delete it by id. | ||||||||||||||||||||||||||||||||||||||||||
| 564 | # | ||||||||||||||||||||||||||||||||||||||||||
| 565 | # The two steps are wrapped together because neither is useful alone and the | ||||||||||||||||||||||||||||||||||||||||||
| 566 | # id from the first is the only way to address the second. | ||||||||||||||||||||||||||||||||||||||||||
| 567 | # | ||||||||||||||||||||||||||||||||||||||||||
| 568 | # A lookup that matches nothing is treated as success rather than as an error. | ||||||||||||||||||||||||||||||||||||||||||
| 569 | # The entry having already gone, whether removed by hand or by another | ||||||||||||||||||||||||||||||||||||||||||
| 570 | # process, means the address is not banned, which is exactly what the caller | ||||||||||||||||||||||||||||||||||||||||||
| 571 | # wanted. Erroring there would make an unban fail for having already achieved | ||||||||||||||||||||||||||||||||||||||||||
| 572 | # its goal. | ||||||||||||||||||||||||||||||||||||||||||
| 573 | # | ||||||||||||||||||||||||||||||||||||||||||
| 574 | # Only the first match is acted on. Duplicates should not exist given the list | ||||||||||||||||||||||||||||||||||||||||||
| 575 | # and address filter, and leaving any extras would be visible on the next | ||||||||||||||||||||||||||||||||||||||||||
| 576 | # unban rather than silently wrong. | ||||||||||||||||||||||||||||||||||||||||||
| 577 | # | ||||||||||||||||||||||||||||||||||||||||||
| 578 | # Never called in testing mode; that path records the request instead. | ||||||||||||||||||||||||||||||||||||||||||
| 579 | # | ||||||||||||||||||||||||||||||||||||||||||
| 580 | # Args: | ||||||||||||||||||||||||||||||||||||||||||
| 581 | # | ||||||||||||||||||||||||||||||||||||||||||
| 582 | # ip - The address to unban, as a plain string. Expected to be an already | ||||||||||||||||||||||||||||||||||||||||||
| 583 | # validated and lowercased IPv4 or IPv6 address, spelled the same way | ||||||||||||||||||||||||||||||||||||||||||
| 584 | # it was when banned. | ||||||||||||||||||||||||||||||||||||||||||
| 585 | # | ||||||||||||||||||||||||||||||||||||||||||
| 586 | # Returns nothing. Dies if either request fails at the HTTP level, which the | ||||||||||||||||||||||||||||||||||||||||||
| 587 | # caller catches and turns into unbanFailed. | ||||||||||||||||||||||||||||||||||||||||||
| 588 | # | ||||||||||||||||||||||||||||||||||||||||||
| 589 | # $self->_unban_ip('10.0.0.1'); | ||||||||||||||||||||||||||||||||||||||||||
| 590 | # | ||||||||||||||||||||||||||||||||||||||||||
| 591 | # # the usual shape at the call site | ||||||||||||||||||||||||||||||||||||||||||
| 592 | # eval { $self->_unban_ip( $opts{ban} ); }; | ||||||||||||||||||||||||||||||||||||||||||
| 593 | # if ($@) { ... raise unbanFailed ... } | ||||||||||||||||||||||||||||||||||||||||||
| 594 | 0 | ▶ | sub _unban_ip { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_unban_ip: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||||||||||||||||
| 595 | 0 | my ( $self, $ip ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 596 | |||||||||||||||||||||||||||||||||||||||||||
| 597 | 0 | my $decoded = $self->_request( 'GET', $self->_unban_lookup_url($ip) ); | |||||||||||||||||||||||||||||||||||||||||
| 598 | |||||||||||||||||||||||||||||||||||||||||||
| 599 | 0 | my $id; | |||||||||||||||||||||||||||||||||||||||||
| 600 | 0 | ▶ | if ( ref($decoded) eq 'ARRAY' && defined( $decoded->[0] ) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
ref $decoded eq "ARRAY" and defined $decoded->[0]
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]
| |||||||||||||||||||||||||||||||||||||||||||
| 601 | 0 | $id = $decoded->[0]{'.id'}; | |||||||||||||||||||||||||||||||||||||||||
| 602 | } | ||||||||||||||||||||||||||||||||||||||||||
| 603 | |||||||||||||||||||||||||||||||||||||||||||
| 604 | # no entry found means something else removed it already, which is fine | ||||||||||||||||||||||||||||||||||||||||||
| 605 | 0 | ▶ | if ( !defined($id) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 606 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 607 | } | ||||||||||||||||||||||||||||||||||||||||||
| 608 | |||||||||||||||||||||||||||||||||||||||||||
| 609 | 0 | $self->_request( 'DELETE', $self->_list_url($ip) . '/' . $id ); | |||||||||||||||||||||||||||||||||||||||||
| 610 | |||||||||||||||||||||||||||||||||||||||||||
| 611 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 612 | } ## end sub _unban_ip | ||||||||||||||||||||||||||||||||||||||||||
| 613 | |||||||||||||||||||||||||||||||||||||||||||
| 614 | =head2 unban | ||||||||||||||||||||||||||||||||||||||||||
| 615 | |||||||||||||||||||||||||||||||||||||||||||
| 616 | Unbans an IP. The value of ban is validated as being a IPv4 or IPv6 address | ||||||||||||||||||||||||||||||||||||||||||
| 617 | and lowercased. The address-list entry is looked up via a GET to find its | ||||||||||||||||||||||||||||||||||||||||||
| 618 | C<.id> and then removed via a DELETE. An entry that can not be found is | ||||||||||||||||||||||||||||||||||||||||||
| 619 | treated as already unbanned. Unbanning an IP that is not banned is a noop. | ||||||||||||||||||||||||||||||||||||||||||
| 620 | |||||||||||||||||||||||||||||||||||||||||||
| 621 | $backend->unban(ban => $ip); | ||||||||||||||||||||||||||||||||||||||||||
| 622 | |||||||||||||||||||||||||||||||||||||||||||
| 623 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 624 | |||||||||||||||||||||||||||||||||||||||||||
| 625 | 1 | ▶ | sub unban { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
unban: called CC=10 SCAR=38.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 626 | 1 | my ( $self, %opts ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 627 | |||||||||||||||||||||||||||||||||||||||||||
| 628 | 1 | $self->errorblank; | |||||||||||||||||||||||||||||||||||||||||
| 629 | |||||||||||||||||||||||||||||||||||||||||||
| 630 | 1 | ▶ | if ( !$self->{inited} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 631 | 0 | $self->{error} = 1; | |||||||||||||||||||||||||||||||||||||||||
| 632 | 0 | $self->{errorString} = 'backend has not been inited'; | |||||||||||||||||||||||||||||||||||||||||
| 633 | 0 | $self->warn; | |||||||||||||||||||||||||||||||||||||||||
| 634 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 635 | } | ||||||||||||||||||||||||||||||||||||||||||
| 636 | |||||||||||||||||||||||||||||||||||||||||||
| 637 | 1 | ▶ | if ( !defined( $opts{ban} ) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 638 | 0 | $self->{error} = 9; | |||||||||||||||||||||||||||||||||||||||||
| 639 | 0 | $self->{errorString} = 'Nothing specified for the value ban'; | |||||||||||||||||||||||||||||||||||||||||
| 640 | 0 | $self->warn; | |||||||||||||||||||||||||||||||||||||||||
| 641 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 642 | } elsif ( ref( $opts{ban} ) ne '' ) { | ||||||||||||||||||||||||||||||||||||||||||
| 643 | 0 | $self->{error} = 10; | |||||||||||||||||||||||||||||||||||||||||
| 644 | 0 | $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"'; | |||||||||||||||||||||||||||||||||||||||||
| 645 | 0 | $self->warn; | |||||||||||||||||||||||||||||||||||||||||
| 646 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 647 | ▶ | } 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/
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/
| |||||||||||||||||||||||||||||||||||||||||||
| 648 | && $opts{ban} !~ /\A$IPv6_re\z/ ) | ||||||||||||||||||||||||||||||||||||||||||
| 649 | { | ||||||||||||||||||||||||||||||||||||||||||
| 650 | 0 | $self->{error} = 10; | |||||||||||||||||||||||||||||||||||||||||
| 651 | 0 | $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 IP'; | |||||||||||||||||||||||||||||||||||||||||
| 652 | 0 | $self->warn; | |||||||||||||||||||||||||||||||||||||||||
| 653 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 654 | } | ||||||||||||||||||||||||||||||||||||||||||
| 655 | |||||||||||||||||||||||||||||||||||||||||||
| 656 | # lowercase so the same IPv6 IP in differing cases can't result in duplicate entries | ||||||||||||||||||||||||||||||||||||||||||
| 657 | 1 | $opts{ban} = lc( $opts{ban} ); | |||||||||||||||||||||||||||||||||||||||||
| 658 | |||||||||||||||||||||||||||||||||||||||||||
| 659 | 1 | ▶ | if ( !$self->{banned}{ $opts{ban} } ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 660 | 0 | ▶ | if ( $self->{testing} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 661 | 0 | $self->{frontend_obj}->{test_data} = 'not banned'; | |||||||||||||||||||||||||||||||||||||||||
| 662 | } | ||||||||||||||||||||||||||||||||||||||||||
| 663 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 664 | } | ||||||||||||||||||||||||||||||||||||||||||
| 665 | |||||||||||||||||||||||||||||||||||||||||||
| 666 | 1 | ▶ | if ( $self->{testing} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 667 | $self->{frontend_obj}->{test_data} = [ | ||||||||||||||||||||||||||||||||||||||||||
| 668 | { method => 'GET', url => $self->_unban_lookup_url( $opts{ban} ) }, | ||||||||||||||||||||||||||||||||||||||||||
| 669 | 1 | { method => 'DELETE', url => $self->_list_url( $opts{ban} ) . '/<id>' }, | |||||||||||||||||||||||||||||||||||||||||
| 670 | ]; | ||||||||||||||||||||||||||||||||||||||||||
| 671 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 672 | 0 | local $@; | |||||||||||||||||||||||||||||||||||||||||
| 673 | 0 | ▶ | eval { $self->_unban_ip( $opts{ban} ); 1; } or do { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 674 | 0 | $self->{error} = 14; | |||||||||||||||||||||||||||||||||||||||||
| 675 | 0 | $self->{errorString} = 'unbanning "' . $opts{ban} . '" failed... ' . $@; | |||||||||||||||||||||||||||||||||||||||||
| 676 | 0 | $self->warn; | |||||||||||||||||||||||||||||||||||||||||
| 677 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 678 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 679 | } | ||||||||||||||||||||||||||||||||||||||||||
| 680 | |||||||||||||||||||||||||||||||||||||||||||
| 681 | 1 | delete( $self->{banned}{ $opts{ban} } ); | |||||||||||||||||||||||||||||||||||||||||
| 682 | } ## end sub unban | ||||||||||||||||||||||||||||||||||||||||||
| 683 | |||||||||||||||||||||||||||||||||||||||||||
| 684 | # Internal helper. Strips the prefix length off a CIDR range, leaving just the | ||||||||||||||||||||||||||||||||||||||||||
| 685 | # address. | ||||||||||||||||||||||||||||||||||||||||||
| 686 | # | ||||||||||||||||||||||||||||||||||||||||||
| 687 | # This is what lets the CIDR paths reuse _list_name and _list_url unchanged. A | ||||||||||||||||||||||||||||||||||||||||||
| 688 | # range always carries a "/", which would never match either of those helpers' | ||||||||||||||||||||||||||||||||||||||||||
| 689 | # family regexps, so every range would be misfiled as IPv6. Passing the | ||||||||||||||||||||||||||||||||||||||||||
| 690 | # address portion instead gets the family right without needing CIDR aware | ||||||||||||||||||||||||||||||||||||||||||
| 691 | # copies of both helpers. | ||||||||||||||||||||||||||||||||||||||||||
| 692 | # | ||||||||||||||||||||||||||||||||||||||||||
| 693 | # Args: | ||||||||||||||||||||||||||||||||||||||||||
| 694 | # | ||||||||||||||||||||||||||||||||||||||||||
| 695 | # cidr - The range to strip, as a plain string. Expected to be an already | ||||||||||||||||||||||||||||||||||||||||||
| 696 | # validated CIDR range such as "10.0.0.0/8". A value with no prefix | ||||||||||||||||||||||||||||||||||||||||||
| 697 | # is returned unchanged, so this is safe to call on a bare address. | ||||||||||||||||||||||||||||||||||||||||||
| 698 | # | ||||||||||||||||||||||||||||||||||||||||||
| 699 | # Returns the address portion as a plain string. | ||||||||||||||||||||||||||||||||||||||||||
| 700 | # | ||||||||||||||||||||||||||||||||||||||||||
| 701 | # $self->_cidr_addr('10.0.0.0/8'); # 10.0.0.0 | ||||||||||||||||||||||||||||||||||||||||||
| 702 | # $self->_cidr_addr('2001:db8::/32'); # 2001:db8:: | ||||||||||||||||||||||||||||||||||||||||||
| 703 | # $self->_cidr_addr('10.0.0.1'); # 10.0.0.1, unchanged | ||||||||||||||||||||||||||||||||||||||||||
| 704 | 2 | ▶ | sub _cidr_addr { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_cidr_addr: called CC=2 SCAR=7.6
| |||||||||||||||||||||||||||||||||||||||||||
| 705 | 2 | my ( $self, $cidr ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 706 | |||||||||||||||||||||||||||||||||||||||||||
| 707 | 2 | ▶ | if ( $cidr =~ m!\A(.+)/[0-9]{1,3}\z! ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 708 | 2 | return $1; | |||||||||||||||||||||||||||||||||||||||||
| 709 | } | ||||||||||||||||||||||||||||||||||||||||||
| 710 | |||||||||||||||||||||||||||||||||||||||||||
| 711 | 0 | return $cidr; | |||||||||||||||||||||||||||||||||||||||||
| 712 | } ## end sub _cidr_addr | ||||||||||||||||||||||||||||||||||||||||||
| 713 | |||||||||||||||||||||||||||||||||||||||||||
| 714 | =head2 ban_cidr | ||||||||||||||||||||||||||||||||||||||||||
| 715 | |||||||||||||||||||||||||||||||||||||||||||
| 716 | Bans a CIDR range by adding it to the relevant address-list via a PUT. | ||||||||||||||||||||||||||||||||||||||||||
| 717 | RouterOS address-lists accept a network prefix in the same manner as a | ||||||||||||||||||||||||||||||||||||||||||
| 718 | single address. The value of ban is validated as being a IPv4 or IPv6 CIDR | ||||||||||||||||||||||||||||||||||||||||||
| 719 | range and lowercased. Banning an already banned range is a noop. | ||||||||||||||||||||||||||||||||||||||||||
| 720 | |||||||||||||||||||||||||||||||||||||||||||
| 721 | $backend->ban_cidr(ban => '1.2.3.0/24'); | ||||||||||||||||||||||||||||||||||||||||||
| 722 | |||||||||||||||||||||||||||||||||||||||||||
| 723 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 724 | |||||||||||||||||||||||||||||||||||||||||||
| 725 | 1 | ▶ | sub ban_cidr { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
ban_cidr: called CC=9 SCAR=35.6
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 726 | 1 | my ( $self, %opts ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 727 | |||||||||||||||||||||||||||||||||||||||||||
| 728 | 1 | $self->errorblank; | |||||||||||||||||||||||||||||||||||||||||
| 729 | |||||||||||||||||||||||||||||||||||||||||||
| 730 | 1 | ▶ | if ( !$self->{inited} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 731 | 0 | $self->{error} = 1; | |||||||||||||||||||||||||||||||||||||||||
| 732 | 0 | $self->{errorString} = 'backend has not been inited'; | |||||||||||||||||||||||||||||||||||||||||
| 733 | 0 | $self->warn; | |||||||||||||||||||||||||||||||||||||||||
| 734 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 735 | } | ||||||||||||||||||||||||||||||||||||||||||
| 736 | |||||||||||||||||||||||||||||||||||||||||||
| 737 | 1 | ▶ | if ( !defined( $opts{ban} ) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 738 | 0 | $self->{error} = 9; | |||||||||||||||||||||||||||||||||||||||||
| 739 | 0 | $self->{errorString} = 'Nothing specified for the value ban'; | |||||||||||||||||||||||||||||||||||||||||
| 740 | 0 | $self->warn; | |||||||||||||||||||||||||||||||||||||||||
| 741 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 742 | } elsif ( ref( $opts{ban} ) ne '' ) { | ||||||||||||||||||||||||||||||||||||||||||
| 743 | 0 | $self->{error} = 35; | |||||||||||||||||||||||||||||||||||||||||
| 744 | 0 | $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"'; | |||||||||||||||||||||||||||||||||||||||||
| 745 | 0 | $self->warn; | |||||||||||||||||||||||||||||||||||||||||
| 746 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 747 | } elsif ( !$self->_valid_cidr( $opts{ban} ) ) { | ||||||||||||||||||||||||||||||||||||||||||
| 748 | 0 | $self->{error} = 35; | |||||||||||||||||||||||||||||||||||||||||
| 749 | 0 | $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 CIDR'; | |||||||||||||||||||||||||||||||||||||||||
| 750 | 0 | $self->warn; | |||||||||||||||||||||||||||||||||||||||||
| 751 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 752 | } | ||||||||||||||||||||||||||||||||||||||||||
| 753 | |||||||||||||||||||||||||||||||||||||||||||
| 754 | # lowercase so the same IPv6 CIDR in differing cases can't result in duplicate entries | ||||||||||||||||||||||||||||||||||||||||||
| 755 | 1 | $opts{ban} = lc( $opts{ban} ); | |||||||||||||||||||||||||||||||||||||||||
| 756 | |||||||||||||||||||||||||||||||||||||||||||
| 757 | 1 | ▶ | if ( $self->{banned_cidr}{ $opts{ban} } ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 758 | 0 | ▶ | if ( $self->{testing} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 759 | 0 | $self->{frontend_obj}->{test_data} = 'already banned'; | |||||||||||||||||||||||||||||||||||||||||
| 760 | } | ||||||||||||||||||||||||||||||||||||||||||
| 761 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 762 | } | ||||||||||||||||||||||||||||||||||||||||||
| 763 | |||||||||||||||||||||||||||||||||||||||||||
| 764 | 1 | my $addr = $self->_cidr_addr( $opts{ban} ); | |||||||||||||||||||||||||||||||||||||||||
| 765 | 1 | my $body = $self->_json->encode( { list => $self->_list_name($addr), address => $opts{ban} } ); | |||||||||||||||||||||||||||||||||||||||||
| 766 | |||||||||||||||||||||||||||||||||||||||||||
| 767 | 1 | ▶ | if ( $self->{testing} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 768 | 1 | $self->{frontend_obj}->{test_data} = [ { method => 'PUT', url => $self->_list_url($addr), content => $body } ]; | |||||||||||||||||||||||||||||||||||||||||
| 769 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 770 | 0 | local $@; | |||||||||||||||||||||||||||||||||||||||||
| 771 | 0 | ▶ | eval { $self->_request( 'PUT', $self->_list_url($addr), $body ); 1; } or do { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 772 | 0 | $self->{error} = 33; | |||||||||||||||||||||||||||||||||||||||||
| 773 | 0 | $self->{errorString} = 'banning "' . $opts{ban} . '" failed... ' . $@; | |||||||||||||||||||||||||||||||||||||||||
| 774 | 0 | $self->warn; | |||||||||||||||||||||||||||||||||||||||||
| 775 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 776 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 777 | } | ||||||||||||||||||||||||||||||||||||||||||
| 778 | |||||||||||||||||||||||||||||||||||||||||||
| 779 | 1 | $self->{banned_cidr}{ $opts{ban} } = 1; | |||||||||||||||||||||||||||||||||||||||||
| 780 | } ## end sub ban_cidr | ||||||||||||||||||||||||||||||||||||||||||
| 781 | |||||||||||||||||||||||||||||||||||||||||||
| 782 | =head2 unban_cidr | ||||||||||||||||||||||||||||||||||||||||||
| 783 | |||||||||||||||||||||||||||||||||||||||||||
| 784 | Unbans a CIDR range. The value of ban is validated as being a IPv4 or IPv6 | ||||||||||||||||||||||||||||||||||||||||||
| 785 | CIDR range and lowercased. Its address-list entry is looked up via a GET to | ||||||||||||||||||||||||||||||||||||||||||
| 786 | find its C<.id> and then removed via a DELETE. An entry that can not be | ||||||||||||||||||||||||||||||||||||||||||
| 787 | found is treated as already unbanned. Unbanning a range that is not banned | ||||||||||||||||||||||||||||||||||||||||||
| 788 | is a noop. | ||||||||||||||||||||||||||||||||||||||||||
| 789 | |||||||||||||||||||||||||||||||||||||||||||
| 790 | $backend->unban_cidr(ban => '1.2.3.0/24'); | ||||||||||||||||||||||||||||||||||||||||||
| 791 | |||||||||||||||||||||||||||||||||||||||||||
| 792 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 793 | |||||||||||||||||||||||||||||||||||||||||||
| 794 | 1 | ▶ | sub unban_cidr { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
unban_cidr: called CC=9 SCAR=36.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 795 | 1 | my ( $self, %opts ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 796 | |||||||||||||||||||||||||||||||||||||||||||
| 797 | 1 | $self->errorblank; | |||||||||||||||||||||||||||||||||||||||||
| 798 | |||||||||||||||||||||||||||||||||||||||||||
| 799 | 1 | ▶ | if ( !$self->{inited} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 800 | 0 | $self->{error} = 1; | |||||||||||||||||||||||||||||||||||||||||
| 801 | 0 | $self->{errorString} = 'backend has not been inited'; | |||||||||||||||||||||||||||||||||||||||||
| 802 | 0 | $self->warn; | |||||||||||||||||||||||||||||||||||||||||
| 803 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 804 | } | ||||||||||||||||||||||||||||||||||||||||||
| 805 | |||||||||||||||||||||||||||||||||||||||||||
| 806 | 1 | ▶ | if ( !defined( $opts{ban} ) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 807 | 0 | $self->{error} = 9; | |||||||||||||||||||||||||||||||||||||||||
| 808 | 0 | $self->{errorString} = 'Nothing specified for the value ban'; | |||||||||||||||||||||||||||||||||||||||||
| 809 | 0 | $self->warn; | |||||||||||||||||||||||||||||||||||||||||
| 810 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 811 | } elsif ( ref( $opts{ban} ) ne '' ) { | ||||||||||||||||||||||||||||||||||||||||||
| 812 | 0 | $self->{error} = 35; | |||||||||||||||||||||||||||||||||||||||||
| 813 | 0 | $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"'; | |||||||||||||||||||||||||||||||||||||||||
| 814 | 0 | $self->warn; | |||||||||||||||||||||||||||||||||||||||||
| 815 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 816 | } elsif ( !$self->_valid_cidr( $opts{ban} ) ) { | ||||||||||||||||||||||||||||||||||||||||||
| 817 | 0 | $self->{error} = 35; | |||||||||||||||||||||||||||||||||||||||||
| 818 | 0 | $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 CIDR'; | |||||||||||||||||||||||||||||||||||||||||
| 819 | 0 | $self->warn; | |||||||||||||||||||||||||||||||||||||||||
| 820 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 821 | } | ||||||||||||||||||||||||||||||||||||||||||
| 822 | |||||||||||||||||||||||||||||||||||||||||||
| 823 | # lowercase so the same IPv6 CIDR in differing cases can't result in duplicate entries | ||||||||||||||||||||||||||||||||||||||||||
| 824 | 1 | $opts{ban} = lc( $opts{ban} ); | |||||||||||||||||||||||||||||||||||||||||
| 825 | |||||||||||||||||||||||||||||||||||||||||||
| 826 | 1 | ▶ | if ( !$self->{banned_cidr}{ $opts{ban} } ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 827 | 0 | ▶ | if ( $self->{testing} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 828 | 0 | $self->{frontend_obj}->{test_data} = 'not banned'; | |||||||||||||||||||||||||||||||||||||||||
| 829 | } | ||||||||||||||||||||||||||||||||||||||||||
| 830 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 831 | } | ||||||||||||||||||||||||||||||||||||||||||
| 832 | |||||||||||||||||||||||||||||||||||||||||||
| 833 | 1 | my $addr = $self->_cidr_addr( $opts{ban} ); | |||||||||||||||||||||||||||||||||||||||||
| 834 | |||||||||||||||||||||||||||||||||||||||||||
| 835 | 1 | ▶ | if ( $self->{testing} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 836 | $self->{frontend_obj}->{test_data} = [ | ||||||||||||||||||||||||||||||||||||||||||
| 837 | 1 | { method => 'GET', url => $self->_unban_lookup_url($addr) . '&address=' . $self->_uri_escape( $opts{ban} ) }, | |||||||||||||||||||||||||||||||||||||||||
| 838 | { method => 'DELETE', url => $self->_list_url($addr) . '/<id>' }, | ||||||||||||||||||||||||||||||||||||||||||
| 839 | ]; | ||||||||||||||||||||||||||||||||||||||||||
| 840 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 841 | 0 | local $@; | |||||||||||||||||||||||||||||||||||||||||
| 842 | 0 | ▶ | eval { $self->_unban_cidr_entry( $opts{ban} ); 1; } or do { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 843 | 0 | $self->{error} = 34; | |||||||||||||||||||||||||||||||||||||||||
| 844 | 0 | $self->{errorString} = 'unbanning "' . $opts{ban} . '" failed... ' . $@; | |||||||||||||||||||||||||||||||||||||||||
| 845 | 0 | $self->warn; | |||||||||||||||||||||||||||||||||||||||||
| 846 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 847 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 848 | } | ||||||||||||||||||||||||||||||||||||||||||
| 849 | |||||||||||||||||||||||||||||||||||||||||||
| 850 | 1 | delete( $self->{banned_cidr}{ $opts{ban} } ); | |||||||||||||||||||||||||||||||||||||||||
| 851 | } ## end sub unban_cidr | ||||||||||||||||||||||||||||||||||||||||||
| 852 | |||||||||||||||||||||||||||||||||||||||||||
| 853 | # Internal helper. Performs the full two step unban of one CIDR range, the | ||||||||||||||||||||||||||||||||||||||||||
| 854 | # range counterpart of _unban_ip. | ||||||||||||||||||||||||||||||||||||||||||
| 855 | # | ||||||||||||||||||||||||||||||||||||||||||
| 856 | # The shape is identical, a lookup for the entry id followed by a delete, and | ||||||||||||||||||||||||||||||||||||||||||
| 857 | # a lookup matching nothing is treated as success for the same reason. | ||||||||||||||||||||||||||||||||||||||||||
| 858 | # | ||||||||||||||||||||||||||||||||||||||||||
| 859 | # The one difference is which value is used where. The family is worked out | ||||||||||||||||||||||||||||||||||||||||||
| 860 | # from the address portion, via _cidr_addr, so the right menu and list name | ||||||||||||||||||||||||||||||||||||||||||
| 861 | # are chosen, but the address filter in the query is the full range, since | ||||||||||||||||||||||||||||||||||||||||||
| 862 | # that is what the entry actually holds. Building the URL inline rather than | ||||||||||||||||||||||||||||||||||||||||||
| 863 | # calling _unban_lookup_url is what allows that split. | ||||||||||||||||||||||||||||||||||||||||||
| 864 | # | ||||||||||||||||||||||||||||||||||||||||||
| 865 | # Never called in testing mode; that path records the request instead. | ||||||||||||||||||||||||||||||||||||||||||
| 866 | # | ||||||||||||||||||||||||||||||||||||||||||
| 867 | # Args: | ||||||||||||||||||||||||||||||||||||||||||
| 868 | # | ||||||||||||||||||||||||||||||||||||||||||
| 869 | # cidr - The range to unban, as a plain string. Expected to be an already | ||||||||||||||||||||||||||||||||||||||||||
| 870 | # validated and lowercased CIDR range, spelled the same way it was | ||||||||||||||||||||||||||||||||||||||||||
| 871 | # when banned. | ||||||||||||||||||||||||||||||||||||||||||
| 872 | # | ||||||||||||||||||||||||||||||||||||||||||
| 873 | # Returns nothing. Dies if either request fails at the HTTP level, which the | ||||||||||||||||||||||||||||||||||||||||||
| 874 | # caller catches and turns into unbanCidrFailed. | ||||||||||||||||||||||||||||||||||||||||||
| 875 | # | ||||||||||||||||||||||||||||||||||||||||||
| 876 | # $self->_unban_cidr_entry('10.0.0.0/8'); | ||||||||||||||||||||||||||||||||||||||||||
| 877 | # # GET https://router.example.org/rest/ip/firewall/address-list?list=kur_ssh&address=10.0.0.0%2F8 | ||||||||||||||||||||||||||||||||||||||||||
| 878 | # # then DELETE https://router.example.org/rest/ip/firewall/address-list/<id> | ||||||||||||||||||||||||||||||||||||||||||
| 879 | 0 | ▶ | sub _unban_cidr_entry { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_unban_cidr_entry: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||||||||||||||||
| 880 | 0 | my ( $self, $cidr ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 881 | |||||||||||||||||||||||||||||||||||||||||||
| 882 | 0 | my $addr = $self->_cidr_addr($cidr); | |||||||||||||||||||||||||||||||||||||||||
| 883 | |||||||||||||||||||||||||||||||||||||||||||
| 884 | 0 | my $url | |||||||||||||||||||||||||||||||||||||||||
| 885 | = $self->_list_url($addr) | ||||||||||||||||||||||||||||||||||||||||||
| 886 | . '?list=' | ||||||||||||||||||||||||||||||||||||||||||
| 887 | . $self->_uri_escape( $self->_list_name($addr) ) | ||||||||||||||||||||||||||||||||||||||||||
| 888 | . '&address=' | ||||||||||||||||||||||||||||||||||||||||||
| 889 | . $self->_uri_escape($cidr); | ||||||||||||||||||||||||||||||||||||||||||
| 890 | |||||||||||||||||||||||||||||||||||||||||||
| 891 | 0 | my $decoded = $self->_request( 'GET', $url ); | |||||||||||||||||||||||||||||||||||||||||
| 892 | |||||||||||||||||||||||||||||||||||||||||||
| 893 | 0 | my $id; | |||||||||||||||||||||||||||||||||||||||||
| 894 | 0 | ▶ | if ( ref($decoded) eq 'ARRAY' && defined( $decoded->[0] ) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
ref $decoded eq "ARRAY" and defined $decoded->[0]
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]
| |||||||||||||||||||||||||||||||||||||||||||
| 895 | 0 | $id = $decoded->[0]{'.id'}; | |||||||||||||||||||||||||||||||||||||||||
| 896 | } | ||||||||||||||||||||||||||||||||||||||||||
| 897 | |||||||||||||||||||||||||||||||||||||||||||
| 898 | # no entry found means something else removed it already, which is fine | ||||||||||||||||||||||||||||||||||||||||||
| 899 | 0 | ▶ | if ( !defined($id) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 900 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 901 | } | ||||||||||||||||||||||||||||||||||||||||||
| 902 | |||||||||||||||||||||||||||||||||||||||||||
| 903 | 0 | $self->_request( 'DELETE', $self->_list_url($addr) . '/' . $id ); | |||||||||||||||||||||||||||||||||||||||||
| 904 | |||||||||||||||||||||||||||||||||||||||||||
| 905 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 906 | } ## end sub _unban_cidr_entry | ||||||||||||||||||||||||||||||||||||||||||
| 907 | |||||||||||||||||||||||||||||||||||||||||||
| 908 | =head2 list_cidr | ||||||||||||||||||||||||||||||||||||||||||
| 909 | |||||||||||||||||||||||||||||||||||||||||||
| 910 | List banned CIDR ranges. Returns an array of the currently banned CIDR | ||||||||||||||||||||||||||||||||||||||||||
| 911 | ranges. Single IPs are not included; for those see L</list>. | ||||||||||||||||||||||||||||||||||||||||||
| 912 | |||||||||||||||||||||||||||||||||||||||||||
| 913 | my @banned_cidrs = $backend->list_cidr; | ||||||||||||||||||||||||||||||||||||||||||
| 914 | |||||||||||||||||||||||||||||||||||||||||||
| 915 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 916 | |||||||||||||||||||||||||||||||||||||||||||
| 917 | 2 | ▶ | sub list_cidr { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
list_cidr: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 918 | 2 | my ( $self, %opts ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 919 | |||||||||||||||||||||||||||||||||||||||||||
| 920 | 2 | $self->errorblank; | |||||||||||||||||||||||||||||||||||||||||
| 921 | |||||||||||||||||||||||||||||||||||||||||||
| 922 | 2 | ▶ | if ( $self->{testing} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 923 | 2 | $self->{frontend_obj}->{test_data} = 'list_cidr'; | |||||||||||||||||||||||||||||||||||||||||
| 924 | } | ||||||||||||||||||||||||||||||||||||||||||
| 925 | |||||||||||||||||||||||||||||||||||||||||||
| 926 | 2 | return keys( %{ $self->{banned_cidr} } ); | |||||||||||||||||||||||||||||||||||||||||
| 927 | } | ||||||||||||||||||||||||||||||||||||||||||
| 928 | |||||||||||||||||||||||||||||||||||||||||||
| 929 | =head2 list | ||||||||||||||||||||||||||||||||||||||||||
| 930 | |||||||||||||||||||||||||||||||||||||||||||
| 931 | List banned IPs. Returns an array of the currently banned single IPs. CIDR | ||||||||||||||||||||||||||||||||||||||||||
| 932 | ranges are not included; for those see L</list_cidr>. | ||||||||||||||||||||||||||||||||||||||||||
| 933 | |||||||||||||||||||||||||||||||||||||||||||
| 934 | my @banned = $backend->list; | ||||||||||||||||||||||||||||||||||||||||||
| 935 | |||||||||||||||||||||||||||||||||||||||||||
| 936 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 937 | |||||||||||||||||||||||||||||||||||||||||||
| 938 | 0 | ▶ | sub list { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
list: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 939 | 0 | my ( $self, %opts ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 940 | |||||||||||||||||||||||||||||||||||||||||||
| 941 | 0 | $self->errorblank; | |||||||||||||||||||||||||||||||||||||||||
| 942 | |||||||||||||||||||||||||||||||||||||||||||
| 943 | 0 | ▶ | if ( $self->{testing} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 944 | 0 | $self->{frontend_obj}->{test_data} = 'list'; | |||||||||||||||||||||||||||||||||||||||||
| 945 | } | ||||||||||||||||||||||||||||||||||||||||||
| 946 | |||||||||||||||||||||||||||||||||||||||||||
| 947 | 0 | return keys( %{ $self->{banned} } ); | |||||||||||||||||||||||||||||||||||||||||
| 948 | } | ||||||||||||||||||||||||||||||||||||||||||
| 949 | |||||||||||||||||||||||||||||||||||||||||||
| 950 | =head2 re_init | ||||||||||||||||||||||||||||||||||||||||||
| 951 | |||||||||||||||||||||||||||||||||||||||||||
| 952 | Tears down and re-inits, then re-adds all previously added bans. teardown | ||||||||||||||||||||||||||||||||||||||||||
| 953 | is best effort, as a partially or fully wiped setup is what this needs to | ||||||||||||||||||||||||||||||||||||||||||
| 954 | recover from. Each retained single IP and CIDR ban is re-added via a PUT to | ||||||||||||||||||||||||||||||||||||||||||
| 955 | the relevant address-list endpoint. | ||||||||||||||||||||||||||||||||||||||||||
| 956 | |||||||||||||||||||||||||||||||||||||||||||
| 957 | $backend->re_init; | ||||||||||||||||||||||||||||||||||||||||||
| 958 | |||||||||||||||||||||||||||||||||||||||||||
| 959 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 960 | |||||||||||||||||||||||||||||||||||||||||||
| 961 | 0 | ▶ | sub re_init { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
re_init: not called CC=8 SCAR=42.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 962 | 0 | my ( $self, %opts ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 963 | |||||||||||||||||||||||||||||||||||||||||||
| 964 | 0 | $self->errorblank; | |||||||||||||||||||||||||||||||||||||||||
| 965 | |||||||||||||||||||||||||||||||||||||||||||
| 966 | # teardown is best effort here as a partially or fully wiped setup is | ||||||||||||||||||||||||||||||||||||||||||
| 967 | # exactly what re_init needs to recover from | ||||||||||||||||||||||||||||||||||||||||||
| 968 | { | ||||||||||||||||||||||||||||||||||||||||||
| 969 | 0 | local $@; | |||||||||||||||||||||||||||||||||||||||||
| 970 | 0 | eval { $self->teardown; }; | |||||||||||||||||||||||||||||||||||||||||
| 971 | } | ||||||||||||||||||||||||||||||||||||||||||
| 972 | 0 | $self->init; | |||||||||||||||||||||||||||||||||||||||||
| 973 | |||||||||||||||||||||||||||||||||||||||||||
| 974 | 0 | my @re_init_test_data; | |||||||||||||||||||||||||||||||||||||||||
| 975 | 0 | foreach my $item ( keys( %{ $self->{banned} } ) ) { | |||||||||||||||||||||||||||||||||||||||||
| 976 | 0 | my $body = $self->_json->encode( { list => $self->_list_name($item), address => $item } ); | |||||||||||||||||||||||||||||||||||||||||
| 977 | |||||||||||||||||||||||||||||||||||||||||||
| 978 | 0 | ▶ | if ( $self->{testing} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 979 | 0 | push( @re_init_test_data, { method => 'PUT', url => $self->_list_url($item), content => $body } ); | |||||||||||||||||||||||||||||||||||||||||
| 980 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 981 | 0 | local $@; | |||||||||||||||||||||||||||||||||||||||||
| 982 | 0 | ▶ | eval { $self->_request( 'PUT', $self->_list_url($item), $body ); 1; } or do { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 983 | 0 | $self->{error} = 13; | |||||||||||||||||||||||||||||||||||||||||
| 984 | 0 | $self->{errorString} = 'banning "' . $item . '" failed... ' . $@; | |||||||||||||||||||||||||||||||||||||||||
| 985 | 0 | $self->warn; | |||||||||||||||||||||||||||||||||||||||||
| 986 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 987 | } | ||||||||||||||||||||||||||||||||||||||||||
| 988 | } ## end foreach my $item ( keys( %{ ...})) | ||||||||||||||||||||||||||||||||||||||||||
| 989 | |||||||||||||||||||||||||||||||||||||||||||
| 990 | # CIDR ranges are re-added the same way, keyed off the family of the address | ||||||||||||||||||||||||||||||||||||||||||
| 991 | 0 | foreach my $item ( keys( %{ $self->{banned_cidr} } ) ) { | |||||||||||||||||||||||||||||||||||||||||
| 992 | 0 | my $addr = $self->_cidr_addr($item); | |||||||||||||||||||||||||||||||||||||||||
| 993 | 0 | my $body = $self->_json->encode( { list => $self->_list_name($addr), address => $item } ); | |||||||||||||||||||||||||||||||||||||||||
| 994 | |||||||||||||||||||||||||||||||||||||||||||
| 995 | 0 | ▶ | if ( $self->{testing} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 996 | 0 | push( @re_init_test_data, { method => 'PUT', url => $self->_list_url($addr), content => $body } ); | |||||||||||||||||||||||||||||||||||||||||
| 997 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 998 | 0 | local $@; | |||||||||||||||||||||||||||||||||||||||||
| 999 | 0 | ▶ | eval { $self->_request( 'PUT', $self->_list_url($addr), $body ); 1; } or do { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1000 | 0 | $self->{error} = 33; | |||||||||||||||||||||||||||||||||||||||||
| 1001 | 0 | $self->{errorString} = 'banning "' . $item . '" failed... ' . $@; | |||||||||||||||||||||||||||||||||||||||||
| 1002 | 0 | $self->warn; | |||||||||||||||||||||||||||||||||||||||||
| 1003 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 1004 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1005 | } ## end foreach my $item ( keys( %{ ...})) | ||||||||||||||||||||||||||||||||||||||||||
| 1006 | |||||||||||||||||||||||||||||||||||||||||||
| 1007 | 0 | ▶ | if ( $self->{testing} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1008 | 0 | $self->{frontend_obj}->{test_data} = \@re_init_test_data; | |||||||||||||||||||||||||||||||||||||||||
| 1009 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1010 | |||||||||||||||||||||||||||||||||||||||||||
| 1011 | 0 | $self->{inited} = 1; | |||||||||||||||||||||||||||||||||||||||||
| 1012 | } ## end sub re_init | ||||||||||||||||||||||||||||||||||||||||||
| 1013 | |||||||||||||||||||||||||||||||||||||||||||
| 1014 | =head2 teardown | ||||||||||||||||||||||||||||||||||||||||||
| 1015 | |||||||||||||||||||||||||||||||||||||||||||
| 1016 | Tears down the setup by removing the address-list entry for each currently | ||||||||||||||||||||||||||||||||||||||||||
| 1017 | banned IP. The internal list of bans is kept, so a following re_init will | ||||||||||||||||||||||||||||||||||||||||||
| 1018 | re-add them. | ||||||||||||||||||||||||||||||||||||||||||
| 1019 | |||||||||||||||||||||||||||||||||||||||||||
| 1020 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 1021 | |||||||||||||||||||||||||||||||||||||||||||
| 1022 | 0 | ▶ | sub teardown { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
teardown: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 1023 | 0 | my ( $self, %opts ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 1024 | |||||||||||||||||||||||||||||||||||||||||||
| 1025 | 0 | $self->errorblank; | |||||||||||||||||||||||||||||||||||||||||
| 1026 | |||||||||||||||||||||||||||||||||||||||||||
| 1027 | 0 | $self->{inited} = 0; | |||||||||||||||||||||||||||||||||||||||||
| 1028 | |||||||||||||||||||||||||||||||||||||||||||
| 1029 | 0 | my @requests; | |||||||||||||||||||||||||||||||||||||||||
| 1030 | 0 | foreach my $item ( sort( keys( %{ $self->{banned} } ) ) ) { | |||||||||||||||||||||||||||||||||||||||||
| 1031 | 0 | ▶ | if ( $self->{testing} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1032 | 0 | push( | |||||||||||||||||||||||||||||||||||||||||
| 1033 | @requests, | ||||||||||||||||||||||||||||||||||||||||||
| 1034 | { method => 'GET', url => $self->_unban_lookup_url($item) }, | ||||||||||||||||||||||||||||||||||||||||||
| 1035 | { method => 'DELETE', url => $self->_list_url($item) . '/<id>' } | ||||||||||||||||||||||||||||||||||||||||||
| 1036 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 1037 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 1038 | 0 | local $@; | |||||||||||||||||||||||||||||||||||||||||
| 1039 | 0 | ▶ | eval { $self->_unban_ip($item); 1; } or do { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1040 | 0 | $self->{error} = 17; | |||||||||||||||||||||||||||||||||||||||||
| 1041 | 0 | $self->{errorString} = 'teardown failed removing the entry for "' . $item . '"... ' . $@; | |||||||||||||||||||||||||||||||||||||||||
| 1042 | 0 | $self->warn; | |||||||||||||||||||||||||||||||||||||||||
| 1043 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 1044 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1045 | } ## end foreach my $item ( sort( keys( %{ $self->{banned...}))) | ||||||||||||||||||||||||||||||||||||||||||
| 1046 | |||||||||||||||||||||||||||||||||||||||||||
| 1047 | 0 | ▶ | if ( $self->{testing} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1048 | 0 | $self->{frontend_obj}->{test_data} = \@requests; | |||||||||||||||||||||||||||||||||||||||||
| 1049 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1050 | } ## end sub teardown | ||||||||||||||||||||||||||||||||||||||||||
| 1051 | |||||||||||||||||||||||||||||||||||||||||||
| 1052 | =head2 stop | ||||||||||||||||||||||||||||||||||||||||||
| 1053 | |||||||||||||||||||||||||||||||||||||||||||
| 1054 | Alias for L</teardown>. | ||||||||||||||||||||||||||||||||||||||||||
| 1055 | |||||||||||||||||||||||||||||||||||||||||||
| 1056 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 1057 | |||||||||||||||||||||||||||||||||||||||||||
| 1058 | 0 | ▶ | sub stop { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
stop: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 1059 | 0 | my ( $self, %opts ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 1060 | |||||||||||||||||||||||||||||||||||||||||||
| 1061 | 0 | return $self->teardown(%opts); | |||||||||||||||||||||||||||||||||||||||||
| 1062 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1063 | |||||||||||||||||||||||||||||||||||||||||||
| 1064 | =head2 check | ||||||||||||||||||||||||||||||||||||||||||
| 1065 | |||||||||||||||||||||||||||||||||||||||||||
| 1066 | Verifies the endpoint and credentials are still usable by fetching the | ||||||||||||||||||||||||||||||||||||||||||
| 1067 | address-list. Returns a true value if so and a false value otherwise. | ||||||||||||||||||||||||||||||||||||||||||
| 1068 | |||||||||||||||||||||||||||||||||||||||||||
| 1069 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 1070 | |||||||||||||||||||||||||||||||||||||||||||
| 1071 | 6 | ▶ | sub check { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
check: called CC=3 SCAR=14.7
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 1072 | 6 | my ( $self, %opts ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 1073 | |||||||||||||||||||||||||||||||||||||||||||
| 1074 | 6 | $self->errorblank; | |||||||||||||||||||||||||||||||||||||||||
| 1075 | |||||||||||||||||||||||||||||||||||||||||||
| 1076 | 6 | my $url = $self->_list_url . '?list=' . $self->_uri_escape( $self->{options}{list4} ); | |||||||||||||||||||||||||||||||||||||||||
| 1077 | |||||||||||||||||||||||||||||||||||||||||||
| 1078 | 6 | ▶ | if ( $self->{testing} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1079 | 6 | $self->{frontend_obj}->{test_data} = [ { method => 'GET', url => $url } ]; | |||||||||||||||||||||||||||||||||||||||||
| 1080 | 6 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 1081 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1082 | |||||||||||||||||||||||||||||||||||||||||||
| 1083 | 0 | local $@; | |||||||||||||||||||||||||||||||||||||||||
| 1084 | 0 | ▶ | eval { $self->_request( 'GET', $url ); 1; } or return 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1085 | |||||||||||||||||||||||||||||||||||||||||||
| 1086 | 0 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 1087 | } ## end sub check | ||||||||||||||||||||||||||||||||||||||||||
| 1088 | |||||||||||||||||||||||||||||||||||||||||||
| 1089 | =head2 flush | ||||||||||||||||||||||||||||||||||||||||||
| 1090 | |||||||||||||||||||||||||||||||||||||||||||
| 1091 | Removes all currently banned IPs and CIDR ranges at once by removing their | ||||||||||||||||||||||||||||||||||||||||||
| 1092 | address-list entries and forgetting them. | ||||||||||||||||||||||||||||||||||||||||||
| 1093 | |||||||||||||||||||||||||||||||||||||||||||
| 1094 | $backend->flush; | ||||||||||||||||||||||||||||||||||||||||||
| 1095 | |||||||||||||||||||||||||||||||||||||||||||
| 1096 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 1097 | |||||||||||||||||||||||||||||||||||||||||||
| 1098 | 0 | ▶ | sub flush { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
flush: not called CC=9 SCAR=45.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 1099 | 0 | my ( $self, %opts ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 1100 | |||||||||||||||||||||||||||||||||||||||||||
| 1101 | 0 | $self->errorblank; | |||||||||||||||||||||||||||||||||||||||||
| 1102 | |||||||||||||||||||||||||||||||||||||||||||
| 1103 | 0 | ▶ | if ( !$self->{inited} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1104 | 0 | $self->{error} = 1; | |||||||||||||||||||||||||||||||||||||||||
| 1105 | 0 | $self->{errorString} = 'backend has not been inited'; | |||||||||||||||||||||||||||||||||||||||||
| 1106 | 0 | $self->warn; | |||||||||||||||||||||||||||||||||||||||||
| 1107 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 1108 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1109 | |||||||||||||||||||||||||||||||||||||||||||
| 1110 | 0 | my @requests; | |||||||||||||||||||||||||||||||||||||||||
| 1111 | 0 | foreach my $item ( sort( keys( %{ $self->{banned} } ) ) ) { | |||||||||||||||||||||||||||||||||||||||||
| 1112 | 0 | ▶ | if ( $self->{testing} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1113 | 0 | push( | |||||||||||||||||||||||||||||||||||||||||
| 1114 | @requests, | ||||||||||||||||||||||||||||||||||||||||||
| 1115 | { method => 'GET', url => $self->_unban_lookup_url($item) }, | ||||||||||||||||||||||||||||||||||||||||||
| 1116 | { method => 'DELETE', url => $self->_list_url($item) . '/<id>' } | ||||||||||||||||||||||||||||||||||||||||||
| 1117 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 1118 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 1119 | 0 | local $@; | |||||||||||||||||||||||||||||||||||||||||
| 1120 | 0 | ▶ | eval { $self->_unban_ip($item); 1; } or do { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1121 | 0 | $self->{error} = 25; | |||||||||||||||||||||||||||||||||||||||||
| 1122 | 0 | $self->{errorString} = 'flush failed removing the entry for "' . $item . '"... ' . $@; | |||||||||||||||||||||||||||||||||||||||||
| 1123 | 0 | $self->warn; | |||||||||||||||||||||||||||||||||||||||||
| 1124 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 1125 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1126 | } ## end foreach my $item ( sort( keys( %{ $self->{banned...}))) | ||||||||||||||||||||||||||||||||||||||||||
| 1127 | |||||||||||||||||||||||||||||||||||||||||||
| 1128 | # CIDR ranges are removed the same way as single IPs | ||||||||||||||||||||||||||||||||||||||||||
| 1129 | 0 | foreach my $item ( sort( keys( %{ $self->{banned_cidr} } ) ) ) { | |||||||||||||||||||||||||||||||||||||||||
| 1130 | 0 | my $addr = $self->_cidr_addr($item); | |||||||||||||||||||||||||||||||||||||||||
| 1131 | 0 | ▶ | if ( $self->{testing} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1132 | 0 | push( | |||||||||||||||||||||||||||||||||||||||||
| 1133 | @requests, | ||||||||||||||||||||||||||||||||||||||||||
| 1134 | { | ||||||||||||||||||||||||||||||||||||||||||
| 1135 | method => 'GET', | ||||||||||||||||||||||||||||||||||||||||||
| 1136 | url => $self->_list_url($addr) | ||||||||||||||||||||||||||||||||||||||||||
| 1137 | . '?list=' | ||||||||||||||||||||||||||||||||||||||||||
| 1138 | . $self->_uri_escape( $self->_list_name($addr) ) | ||||||||||||||||||||||||||||||||||||||||||
| 1139 | . '&address=' | ||||||||||||||||||||||||||||||||||||||||||
| 1140 | . $self->_uri_escape($item) | ||||||||||||||||||||||||||||||||||||||||||
| 1141 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 1142 | { method => 'DELETE', url => $self->_list_url($addr) . '/<id>' } | ||||||||||||||||||||||||||||||||||||||||||
| 1143 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 1144 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 1145 | 0 | local $@; | |||||||||||||||||||||||||||||||||||||||||
| 1146 | 0 | ▶ | eval { $self->_unban_cidr_entry($item); 1; } or do { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1147 | 0 | $self->{error} = 25; | |||||||||||||||||||||||||||||||||||||||||
| 1148 | 0 | $self->{errorString} = 'flush failed removing the entry for "' . $item . '"... ' . $@; | |||||||||||||||||||||||||||||||||||||||||
| 1149 | 0 | $self->warn; | |||||||||||||||||||||||||||||||||||||||||
| 1150 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 1151 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1152 | } ## end foreach my $item ( sort( keys( %{ $self->{banned_cidr...}))) | ||||||||||||||||||||||||||||||||||||||||||
| 1153 | |||||||||||||||||||||||||||||||||||||||||||
| 1154 | 0 | ▶ | if ( $self->{testing} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1155 | 0 | $self->{frontend_obj}->{test_data} = \@requests; | |||||||||||||||||||||||||||||||||||||||||
| 1156 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1157 | |||||||||||||||||||||||||||||||||||||||||||
| 1158 | 0 | $self->{banned} = {}; | |||||||||||||||||||||||||||||||||||||||||
| 1159 | 0 | $self->{banned_cidr} = {}; | |||||||||||||||||||||||||||||||||||||||||
| 1160 | } ## end sub flush | ||||||||||||||||||||||||||||||||||||||||||
| 1161 | |||||||||||||||||||||||||||||||||||||||||||
| 1162 | =head1 ERROR CODES / FLAGS | ||||||||||||||||||||||||||||||||||||||||||
| 1163 | |||||||||||||||||||||||||||||||||||||||||||
| 1164 | Error handling is provided by L<Error::Helper>. All | ||||||||||||||||||||||||||||||||||||||||||
| 1165 | errors are considered fatal. | ||||||||||||||||||||||||||||||||||||||||||
| 1166 | |||||||||||||||||||||||||||||||||||||||||||
| 1167 | =head2 1, notInited | ||||||||||||||||||||||||||||||||||||||||||
| 1168 | |||||||||||||||||||||||||||||||||||||||||||
| 1169 | The backend has not been inited yet. | ||||||||||||||||||||||||||||||||||||||||||
| 1170 | |||||||||||||||||||||||||||||||||||||||||||
| 1171 | =head2 6, invalidPrefixSpecified | ||||||||||||||||||||||||||||||||||||||||||
| 1172 | |||||||||||||||||||||||||||||||||||||||||||
| 1173 | The specified prefix did not match /^[a-zA-Z0-9]+$/. | ||||||||||||||||||||||||||||||||||||||||||
| 1174 | |||||||||||||||||||||||||||||||||||||||||||
| 1175 | =head2 7, invalidName | ||||||||||||||||||||||||||||||||||||||||||
| 1176 | |||||||||||||||||||||||||||||||||||||||||||
| 1177 | The name is either undef or does not match /^[a-zA-Z0-9\-]+$/. | ||||||||||||||||||||||||||||||||||||||||||
| 1178 | |||||||||||||||||||||||||||||||||||||||||||
| 1179 | =head2 8, optionsNotHash | ||||||||||||||||||||||||||||||||||||||||||
| 1180 | |||||||||||||||||||||||||||||||||||||||||||
| 1181 | The item passed to new for options is not a hash. | ||||||||||||||||||||||||||||||||||||||||||
| 1182 | |||||||||||||||||||||||||||||||||||||||||||
| 1183 | =head2 9, noBanItem | ||||||||||||||||||||||||||||||||||||||||||
| 1184 | |||||||||||||||||||||||||||||||||||||||||||
| 1185 | No IP or CIDR range specified to ban or unban. | ||||||||||||||||||||||||||||||||||||||||||
| 1186 | |||||||||||||||||||||||||||||||||||||||||||
| 1187 | =head2 10, banItemNotIP | ||||||||||||||||||||||||||||||||||||||||||
| 1188 | |||||||||||||||||||||||||||||||||||||||||||
| 1189 | The item to ban is not an IP. Either wrong ref type or regexp | ||||||||||||||||||||||||||||||||||||||||||
| 1190 | test using L<Regexp::IPv4> and L<Regexp::IPv6> failed. | ||||||||||||||||||||||||||||||||||||||||||
| 1191 | |||||||||||||||||||||||||||||||||||||||||||
| 1192 | =head2 12, backendInitError | ||||||||||||||||||||||||||||||||||||||||||
| 1193 | |||||||||||||||||||||||||||||||||||||||||||
| 1194 | Failed to init the backend. | ||||||||||||||||||||||||||||||||||||||||||
| 1195 | |||||||||||||||||||||||||||||||||||||||||||
| 1196 | =head2 13, banFailed | ||||||||||||||||||||||||||||||||||||||||||
| 1197 | |||||||||||||||||||||||||||||||||||||||||||
| 1198 | Failed to ban the item. | ||||||||||||||||||||||||||||||||||||||||||
| 1199 | |||||||||||||||||||||||||||||||||||||||||||
| 1200 | =head2 14, unbanFailed | ||||||||||||||||||||||||||||||||||||||||||
| 1201 | |||||||||||||||||||||||||||||||||||||||||||
| 1202 | Failed to unban the item. | ||||||||||||||||||||||||||||||||||||||||||
| 1203 | |||||||||||||||||||||||||||||||||||||||||||
| 1204 | =head2 15, listFailed | ||||||||||||||||||||||||||||||||||||||||||
| 1205 | |||||||||||||||||||||||||||||||||||||||||||
| 1206 | Failed to get a list of bans. | ||||||||||||||||||||||||||||||||||||||||||
| 1207 | |||||||||||||||||||||||||||||||||||||||||||
| 1208 | =head2 16, reInitFailed | ||||||||||||||||||||||||||||||||||||||||||
| 1209 | |||||||||||||||||||||||||||||||||||||||||||
| 1210 | Failed to re_init the backend. | ||||||||||||||||||||||||||||||||||||||||||
| 1211 | |||||||||||||||||||||||||||||||||||||||||||
| 1212 | =head2 17, teardownFailed | ||||||||||||||||||||||||||||||||||||||||||
| 1213 | |||||||||||||||||||||||||||||||||||||||||||
| 1214 | Failed to teardown the backend. | ||||||||||||||||||||||||||||||||||||||||||
| 1215 | |||||||||||||||||||||||||||||||||||||||||||
| 1216 | =head2 18, alreadyInited | ||||||||||||||||||||||||||||||||||||||||||
| 1217 | |||||||||||||||||||||||||||||||||||||||||||
| 1218 | init called, but the backend has already been inited. | ||||||||||||||||||||||||||||||||||||||||||
| 1219 | |||||||||||||||||||||||||||||||||||||||||||
| 1220 | =head2 23, initFailed | ||||||||||||||||||||||||||||||||||||||||||
| 1221 | |||||||||||||||||||||||||||||||||||||||||||
| 1222 | init failed. Probing the address-list endpoint failed. | ||||||||||||||||||||||||||||||||||||||||||
| 1223 | |||||||||||||||||||||||||||||||||||||||||||
| 1224 | =head2 24, checkFailed | ||||||||||||||||||||||||||||||||||||||||||
| 1225 | |||||||||||||||||||||||||||||||||||||||||||
| 1226 | The backend check raised an error. | ||||||||||||||||||||||||||||||||||||||||||
| 1227 | |||||||||||||||||||||||||||||||||||||||||||
| 1228 | =head2 25, flushFailed | ||||||||||||||||||||||||||||||||||||||||||
| 1229 | |||||||||||||||||||||||||||||||||||||||||||
| 1230 | Failed to flush the bans. | ||||||||||||||||||||||||||||||||||||||||||
| 1231 | |||||||||||||||||||||||||||||||||||||||||||
| 1232 | =head2 26, portsNotSupported | ||||||||||||||||||||||||||||||||||||||||||
| 1233 | |||||||||||||||||||||||||||||||||||||||||||
| 1234 | Ports were specified, but this backend manages whole IPs and does not | ||||||||||||||||||||||||||||||||||||||||||
| 1235 | support ports. | ||||||||||||||||||||||||||||||||||||||||||
| 1236 | |||||||||||||||||||||||||||||||||||||||||||
| 1237 | =head2 27, protocolsNotSupported | ||||||||||||||||||||||||||||||||||||||||||
| 1238 | |||||||||||||||||||||||||||||||||||||||||||
| 1239 | Protocols were specified, but this backend manages whole IPs and does not | ||||||||||||||||||||||||||||||||||||||||||
| 1240 | support protocols. | ||||||||||||||||||||||||||||||||||||||||||
| 1241 | |||||||||||||||||||||||||||||||||||||||||||
| 1242 | =head2 30, hostNotDefined | ||||||||||||||||||||||||||||||||||||||||||
| 1243 | |||||||||||||||||||||||||||||||||||||||||||
| 1244 | The option host is undef or blank. | ||||||||||||||||||||||||||||||||||||||||||
| 1245 | |||||||||||||||||||||||||||||||||||||||||||
| 1246 | =head2 31, userNotDefined | ||||||||||||||||||||||||||||||||||||||||||
| 1247 | |||||||||||||||||||||||||||||||||||||||||||
| 1248 | The option user is undef or blank. | ||||||||||||||||||||||||||||||||||||||||||
| 1249 | |||||||||||||||||||||||||||||||||||||||||||
| 1250 | =head2 32, passwordNotDefined | ||||||||||||||||||||||||||||||||||||||||||
| 1251 | |||||||||||||||||||||||||||||||||||||||||||
| 1252 | The option password is undef or blank. | ||||||||||||||||||||||||||||||||||||||||||
| 1253 | |||||||||||||||||||||||||||||||||||||||||||
| 1254 | =head2 33, banCidrFailed | ||||||||||||||||||||||||||||||||||||||||||
| 1255 | |||||||||||||||||||||||||||||||||||||||||||
| 1256 | Failed to ban the CIDR range. | ||||||||||||||||||||||||||||||||||||||||||
| 1257 | |||||||||||||||||||||||||||||||||||||||||||
| 1258 | =head2 34, unbanCidrFailed | ||||||||||||||||||||||||||||||||||||||||||
| 1259 | |||||||||||||||||||||||||||||||||||||||||||
| 1260 | Failed to unban the CIDR range. | ||||||||||||||||||||||||||||||||||||||||||
| 1261 | |||||||||||||||||||||||||||||||||||||||||||
| 1262 | =head2 35, cidrItemNotCidr | ||||||||||||||||||||||||||||||||||||||||||
| 1263 | |||||||||||||||||||||||||||||||||||||||||||
| 1264 | The item to ban is not a CIDR range. Either wrong ref type or it is not an | ||||||||||||||||||||||||||||||||||||||||||
| 1265 | IPv4 or IPv6 address followed by a prefix length valid for its family. | ||||||||||||||||||||||||||||||||||||||||||
| 1266 | |||||||||||||||||||||||||||||||||||||||||||
| 1267 | =head2 36, cidrNotSupported | ||||||||||||||||||||||||||||||||||||||||||
| 1268 | |||||||||||||||||||||||||||||||||||||||||||
| 1269 | The backend does not support CIDR bans. | ||||||||||||||||||||||||||||||||||||||||||
| 1270 | |||||||||||||||||||||||||||||||||||||||||||
| 1271 | =head2 37, listCidrFailed | ||||||||||||||||||||||||||||||||||||||||||
| 1272 | |||||||||||||||||||||||||||||||||||||||||||
| 1273 | Failed to get a list of CIDR bans. | ||||||||||||||||||||||||||||||||||||||||||
| 1274 | |||||||||||||||||||||||||||||||||||||||||||
| 1275 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||
| 1276 | |||||||||||||||||||||||||||||||||||||||||||
| 1277 | Zane C. Bowers-Hadley, C<< <vvelox at vvelox.ent> >> | ||||||||||||||||||||||||||||||||||||||||||
| 1278 | |||||||||||||||||||||||||||||||||||||||||||
| 1279 | =head1 LICENSE AND COPYRIGHT | ||||||||||||||||||||||||||||||||||||||||||
| 1280 | |||||||||||||||||||||||||||||||||||||||||||
| 1281 | This software is Copyright (c) 2023 by Zane C. Bowers-Hadley. | ||||||||||||||||||||||||||||||||||||||||||
| 1282 | |||||||||||||||||||||||||||||||||||||||||||
| 1283 | This is free software, licensed under: | ||||||||||||||||||||||||||||||||||||||||||
| 1284 | |||||||||||||||||||||||||||||||||||||||||||
| 1285 | The GNU Lesser General Public License, Version 2.1, February 1999 | ||||||||||||||||||||||||||||||||||||||||||
| 1286 | |||||||||||||||||||||||||||||||||||||||||||
| 1287 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 1288 | |||||||||||||||||||||||||||||||||||||||||||
| 1289 | 1 | 1; # End of Net::Firewall::BlockerHelper::backends::routeros_api | |||||||||||||||||||||||||||||||||||||||||