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