| 1 | package Net::Firewall::BlockerHelper::backends::file_reload; | ||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use 5.006; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use base 'Error::Helper'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use Regexp::IPv4 qw($IPv4_re); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use Regexp::IPv6 qw($IPv6_re); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||
| 10 | =head1 NAME | ||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||
| 12 | Net::Firewall::BlockerHelper::backends::file_reload - Render banned IPs to a file and run a reload hook. | ||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||
| 14 | =head1 VERSION | ||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||
| 16 | Version 0.1.0 | ||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||
| 18 | =cut | ||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||
| 20 | 1 | our $VERSION = '0.1.0'; | |||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||
| 22 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||
| 24 | The rendered file has no effect on its own; whatever is meant to consume it | ||||||||||||||||||||||||||||||||
| 25 | must be configured to read it and the C<reload> option set to whatever | ||||||||||||||||||||||||||||||||
| 26 | makes it re-read it. So for the nginx example below, nginx needs a line | ||||||||||||||||||||||||||||||||
| 27 | like the following in the relevant C<http> or C<server> context. | ||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||
| 29 | include /etc/nginx/blocklist.conf; | ||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||
| 31 | Also worth noting is that with C<remove_on_teardown> left at its default | ||||||||||||||||||||||||||||||||
| 32 | of 1, teardown unlinks the file, so a consumer that errors on a missing | ||||||||||||||||||||||||||||||||
| 33 | include, nginx included, will then fail to reload or start till the file | ||||||||||||||||||||||||||||||||
| 34 | exists again. For consumers like that either set C<remove_on_teardown> | ||||||||||||||||||||||||||||||||
| 35 | to 0, which leaves an empty file in place instead, or make sure the file | ||||||||||||||||||||||||||||||||
| 36 | gets created before the consumer needs it. | ||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||
| 38 | use Net::Firewall::BlockerHelper; | ||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||
| 40 | my $fw_helper = Net::Firewall::BlockerHelper->new( | ||||||||||||||||||||||||||||||||
| 41 | backend => 'file_reload', | ||||||||||||||||||||||||||||||||
| 42 | name => 'blocklist', | ||||||||||||||||||||||||||||||||
| 43 | options => { | ||||||||||||||||||||||||||||||||
| 44 | file => '/etc/nginx/blocklist.conf', | ||||||||||||||||||||||||||||||||
| 45 | format => 'deny %%%BAN%%%;', | ||||||||||||||||||||||||||||||||
| 46 | reload => 'systemctl reload nginx', | ||||||||||||||||||||||||||||||||
| 47 | }, | ||||||||||||||||||||||||||||||||
| 48 | ); | ||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||
| 50 | $fw_helper->init_backend; | ||||||||||||||||||||||||||||||||
| 51 | $fw_helper->ban( ban => '1.2.3.4' ); | ||||||||||||||||||||||||||||||||
| 52 | $fw_helper->unban( ban => '1.2.3.4' ); | ||||||||||||||||||||||||||||||||
| 53 | $fw_helper->teardown; | ||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||
| 55 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||
| 57 | This is a generic backend for anything that reads a list of IPs from a file | ||||||||||||||||||||||||||||||||
| 58 | and needs to be told to reload after that file changes: web servers, DNS RPZ | ||||||||||||||||||||||||||||||||
| 59 | zones, hand rolled ipset restore files, and so on. | ||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||
| 61 | The full set of currently banned IPs is rendered to C<file> from in-memory | ||||||||||||||||||||||||||||||||
| 62 | state on every change; the file is never parsed back, so this module is the | ||||||||||||||||||||||||||||||||
| 63 | sole author of its contents. After each write the optional C<reload> command | ||||||||||||||||||||||||||||||||
| 64 | is run. Because the file is rewritten wholesale, ban/unban are idempotent and | ||||||||||||||||||||||||||||||||
| 65 | there is never any partially applied state. | ||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||
| 67 | =head1 METHODS | ||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||
| 69 | =head2 new | ||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||
| 71 | - options :: A hash of options. See below. | ||||||||||||||||||||||||||||||||
| 72 | - Default :: {} | ||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||
| 74 | - name :: Required by Net::Firewall::BlockerHelper, otherwise unused. | ||||||||||||||||||||||||||||||||
| 75 | - Default :: undef | ||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||
| 77 | The options hash accepts the following. | ||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||
| 79 | - file :: Path of the file to render the banned IPs to. Required. | ||||||||||||||||||||||||||||||||
| 80 | - Default :: undef | ||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||
| 82 | - reload :: Command run after the file is written. '2>&1' is appended. | ||||||||||||||||||||||||||||||||
| 83 | If undef or blank, nothing is run and only the file is updated. | ||||||||||||||||||||||||||||||||
| 84 | - Default :: undef | ||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||
| 86 | - blank_reload_error :: If true, the reload command producing no output | ||||||||||||||||||||||||||||||||
| 87 | at all is treated as a failure, even with a zero exit. Set to 0 | ||||||||||||||||||||||||||||||||
| 88 | for reload commands that are silent on success, such as | ||||||||||||||||||||||||||||||||
| 89 | 'systemctl reload nginx'. | ||||||||||||||||||||||||||||||||
| 90 | - Default :: 1 | ||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||
| 92 | - format :: Per-IP line template. '%%%BAN%%%' is replaced with the IP. | ||||||||||||||||||||||||||||||||
| 93 | The rendered lines are newline joined. | ||||||||||||||||||||||||||||||||
| 94 | - Default :: '%%%BAN%%%' | ||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||
| 96 | - header :: String emitted at the top of the file, before the IP lines. | ||||||||||||||||||||||||||||||||
| 97 | - Default :: '' | ||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||
| 99 | - footer :: String emitted at the bottom of the file, after the IP lines. | ||||||||||||||||||||||||||||||||
| 100 | - Default :: '' | ||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||
| 102 | - check :: Optional command used by check(). A zero exit is healthy. If | ||||||||||||||||||||||||||||||||
| 103 | not set, check() verifies the file still exists instead. | ||||||||||||||||||||||||||||||||
| 104 | - Default :: undef | ||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||
| 106 | - remove_on_teardown :: If true, teardown unlinks the file. If false, the | ||||||||||||||||||||||||||||||||
| 107 | file is rendered empty (header/footer only) and left in place. | ||||||||||||||||||||||||||||||||
| 108 | - Default :: 1 | ||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||
| 110 | All errors are considered fatal, meaning if new fails it will die. | ||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||
| 112 | =cut | ||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||
| 114 | sub new { | ||||||||||||||||||||||||||||||||
| 115 | 11 | ▶ | my ( $blank, %opts ) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=14 SCAR=27.2
Pod
documented
| |||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||
| 117 | 11 | 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 | 8 => 'optionsNotHash', | ||||||||||||||||||||||||||||||||
| 131 | 9 => 'noBanItem', | ||||||||||||||||||||||||||||||||
| 132 | 10 => 'banItemNotIP', | ||||||||||||||||||||||||||||||||
| 133 | 12 => 'backendInitError', | ||||||||||||||||||||||||||||||||
| 134 | 13 => 'banFailed', | ||||||||||||||||||||||||||||||||
| 135 | 14 => 'unbanFailed', | ||||||||||||||||||||||||||||||||
| 136 | 15 => 'listFailed', | ||||||||||||||||||||||||||||||||
| 137 | 16 => 'reInitFailed', | ||||||||||||||||||||||||||||||||
| 138 | 17 => 'teardownFailed', | ||||||||||||||||||||||||||||||||
| 139 | 18 => 'alreadyInited', | ||||||||||||||||||||||||||||||||
| 140 | 24 => 'checkFailed', | ||||||||||||||||||||||||||||||||
| 141 | 25 => 'flushFailed', | ||||||||||||||||||||||||||||||||
| 142 | 30 => 'fileNotDefined', | ||||||||||||||||||||||||||||||||
| 143 | 31 => 'fileWriteFailed', | ||||||||||||||||||||||||||||||||
| 144 | 32 => 'banCidrFailed', | ||||||||||||||||||||||||||||||||
| 145 | 33 => 'unbanCidrFailed', | ||||||||||||||||||||||||||||||||
| 146 | 34 => 'cidrItemNotCidr', | ||||||||||||||||||||||||||||||||
| 147 | 35 => 'cidrNotSupported', | ||||||||||||||||||||||||||||||||
| 148 | 36 => 'listCidrFailed', | ||||||||||||||||||||||||||||||||
| 149 | }, | ||||||||||||||||||||||||||||||||
| 150 | fatal_flags => {}, | ||||||||||||||||||||||||||||||||
| 151 | perror_not_fatal => 0, | ||||||||||||||||||||||||||||||||
| 152 | }, | ||||||||||||||||||||||||||||||||
| 153 | options => {}, | ||||||||||||||||||||||||||||||||
| 154 | ports => [], | ||||||||||||||||||||||||||||||||
| 155 | protocols => [], | ||||||||||||||||||||||||||||||||
| 156 | testing => undef, | ||||||||||||||||||||||||||||||||
| 157 | test_data => undef, | ||||||||||||||||||||||||||||||||
| 158 | prefix => 'kur', | ||||||||||||||||||||||||||||||||
| 159 | name => undef, | ||||||||||||||||||||||||||||||||
| 160 | frontend_obj => undef, | ||||||||||||||||||||||||||||||||
| 161 | inited => 0, | ||||||||||||||||||||||||||||||||
| 162 | banned => {}, | ||||||||||||||||||||||||||||||||
| 163 | banned_cidr => {}, | ||||||||||||||||||||||||||||||||
| 164 | cidr_supported => 1, | ||||||||||||||||||||||||||||||||
| 165 | }; | ||||||||||||||||||||||||||||||||
| 166 | 11 | bless $self; | |||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||||
| 168 | 11 | ▶ | if ( defined( $opts{testing} ) ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 169 | 4 | $self->{testing} = $opts{testing}; | |||||||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||||||
| 171 | 11 | ▶ | if ( defined( $opts{frontend_obj} ) ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 172 | 7 | $self->{frontend_obj} = $opts{frontend_obj}; | |||||||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||||||
| 174 | 11 | ▶ | if ( defined( $opts{name} ) ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 175 | 11 | $self->{name} = $opts{name}; | |||||||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||
| 178 | 11 | ▶ | if ( defined( $opts{options} ) && ref( $opts{options} ) ne 'HASH' ) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition33%1/3
defined $opts{"options"} and ref $opts{"options"} ne "HASH"
MC/DC0%0/2
defined $opts{"options"} and ref $opts{"options"} ne "HASH"
defined $opts{"options"}
ref $opts{"options"} ne "HASH"
Truth table33%1/3
A and B
A= defined $opts{"options"}B= ref $opts{"options"} ne "HASH"
| |||||||||||||||||||||||||||||||||
| 179 | 0 | $self->{perror} = 1; | |||||||||||||||||||||||||||||||
| 180 | 0 | $self->{error} = 8; | |||||||||||||||||||||||||||||||
| 181 | 0 | $self->{errorString} = 'ref for options is "' . ref( $opts{options} ) . '" and not HASH'; | |||||||||||||||||||||||||||||||
| 182 | 0 | $self->warn; | |||||||||||||||||||||||||||||||
| 183 | } elsif ( defined( $opts{options} ) ) { | ||||||||||||||||||||||||||||||||
| 184 | 11 | $self->{options} = $opts{options}; | |||||||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||||||
| 187 | # file is the one hard requirement | ||||||||||||||||||||||||||||||||
| 188 | 11 | ▶ | if ( !defined( $self->{options}{file} ) || $self->{options}{file} eq '' ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not defined $self->{"options"}{"file"} or $self->{"options"}{"file"} eq ""
MC/DC50%1/2
not defined $self->{"options"}{"file"} or $self->{"options"}{"file"} eq ""
not defined $self->{"options"}{"file"}
$self->{"options"}{"file"} eq ""
Truth table66%2/3
not(A) or B
A= not defined $self->{"options"}{"file"}B= $self->{"options"}{"file"} eq ""
| |||||||||||||||||||||||||||||||||
| 189 | 1 | $self->{perror} = 1; | |||||||||||||||||||||||||||||||
| 190 | 1 | $self->{error} = 30; | |||||||||||||||||||||||||||||||
| 191 | 1 | $self->{errorString} = 'options{file} is undef or blank'; | |||||||||||||||||||||||||||||||
| 192 | 1 | $self->warn; | |||||||||||||||||||||||||||||||
| 193 | } | ||||||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||||||
| 195 | # fill in the templating defaults | ||||||||||||||||||||||||||||||||
| 196 | 10 | ▶ | $self->{options}{format} = '%%%BAN%%%' if ( !defined( $self->{options}{format} ) ); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 197 | 10 | ▶ | $self->{options}{header} = '' if ( !defined( $self->{options}{header} ) ); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 198 | 10 | ▶ | $self->{options}{footer} = '' if ( !defined( $self->{options}{footer} ) ); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 199 | $self->{options}{remove_on_teardown} = 1 | ||||||||||||||||||||||||||||||||
| 200 | 10 | ▶ | if ( !defined( $self->{options}{remove_on_teardown} ) ); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 201 | $self->{options}{blank_reload_error} = 1 | ||||||||||||||||||||||||||||||||
| 202 | 10 | ▶ | if ( !defined( $self->{options}{blank_reload_error} ) ); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||||||
| 204 | 10 | return $self; | |||||||||||||||||||||||||||||||
| 205 | } ## end sub new | ||||||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||||||
| 207 | # Internal helper. Builds the full file contents from the current ban list, | ||||||||||||||||||||||||||||||||
| 208 | # sorted so the output is stable, wrapped in the configured header/footer. | ||||||||||||||||||||||||||||||||
| 209 | sub _render { | ||||||||||||||||||||||||||||||||
| 210 | 29 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
_render: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||||||
| 212 | 29 | my @lines; | |||||||||||||||||||||||||||||||
| 213 | 29 | ▶ | push( @lines, $self->{options}{header} ) if ( $self->{options}{header} ne '' ); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||||||
| 215 | # single IPs and CIDR ranges are rendered into the same file the same way | ||||||||||||||||||||||||||||||||
| 216 | 29 | my @to_render = ( keys( %{ $self->{banned} } ), keys( %{ $self->{banned_cidr} } ) ); | |||||||||||||||||||||||||||||||
| 217 | 29 | foreach my $ip ( sort(@to_render) ) { | |||||||||||||||||||||||||||||||
| 218 | 19 | my $line = $self->{options}{format}; | |||||||||||||||||||||||||||||||
| 219 | 19 | $line =~ s/\%\%\%BAN\%\%\%/$ip/g; | |||||||||||||||||||||||||||||||
| 220 | 19 | push( @lines, $line ); | |||||||||||||||||||||||||||||||
| 221 | } | ||||||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||||||
| 223 | 29 | ▶ | push( @lines, $self->{options}{footer} ) if ( $self->{options}{footer} ne '' ); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||||||
| 225 | 29 | return join( "\n", @lines ) . "\n"; | |||||||||||||||||||||||||||||||
| 226 | } ## end sub _render | ||||||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||||||
| 228 | # Internal helper. Renders the file and runs the reload hook. In testing mode | ||||||||||||||||||||||||||||||||
| 229 | # it records what would happen in test_data instead of touching the disk. The | ||||||||||||||||||||||||||||||||
| 230 | # $error_flag is the error code to raise on a write/reload failure so the | ||||||||||||||||||||||||||||||||
| 231 | # caller's context (ban vs unban vs ...) is preserved. | ||||||||||||||||||||||||||||||||
| 232 | sub _apply { | ||||||||||||||||||||||||||||||||
| 233 | 29 | ▶ | my ( $self, $error_flag ) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
_apply: called CC=9 SCAR=22.2
| |||||||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||||||
| 235 | 29 | my $content = $self->_render; | |||||||||||||||||||||||||||||||
| 236 | 29 | my $reload = $self->{options}{reload}; | |||||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||||||
| 238 | 29 | ▶ | if ( $self->{testing} ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 239 | $self->{frontend_obj}->{test_data} = { | ||||||||||||||||||||||||||||||||
| 240 | file => $self->{options}{file}, | ||||||||||||||||||||||||||||||||
| 241 | 9 | content => $content, | |||||||||||||||||||||||||||||||
| 242 | reload => $reload, | ||||||||||||||||||||||||||||||||
| 243 | }; | ||||||||||||||||||||||||||||||||
| 244 | 9 | return; | |||||||||||||||||||||||||||||||
| 245 | } | ||||||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||||||
| 247 | 20 | my $fh; | |||||||||||||||||||||||||||||||
| 248 | 20 | ▶ | if ( !open( $fh, '>', $self->{options}{file} ) ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 249 | 0 | $self->{error} = 31; | |||||||||||||||||||||||||||||||
| 250 | 0 | $self->{errorString} = 'could not open "' . $self->{options}{file} . '" for writing... ' . $!; | |||||||||||||||||||||||||||||||
| 251 | 0 | $self->warn; | |||||||||||||||||||||||||||||||
| 252 | 0 | return; | |||||||||||||||||||||||||||||||
| 253 | } | ||||||||||||||||||||||||||||||||
| 254 | 20 | print( $fh $content ); | |||||||||||||||||||||||||||||||
| 255 | 20 | close($fh); | |||||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||||||
| 257 | 20 | ▶ | if ( defined($reload) && $reload ne '' ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $reload and $reload ne ""
MC/DC50%1/2
defined $reload and $reload ne ""
defined $reload
$reload ne ""
Truth table66%2/3
A and B
A= defined $reloadB= $reload ne ""
| |||||||||||||||||||||||||||||||||
| 258 | 15 | my $output = `$reload 2>&1`; | |||||||||||||||||||||||||||||||
| 259 | 15 | ▶ | $output = '' if ( !defined($output) ); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 260 | 15 | ▶ | if ( $? != 0 ) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 261 | 5 | $self->{error} = $error_flag; | |||||||||||||||||||||||||||||||
| 262 | 5 | $self->{errorString} = 'reload command "' . $reload . '" failed... ' . $output; | |||||||||||||||||||||||||||||||
| 263 | 5 | $self->warn; | |||||||||||||||||||||||||||||||
| 264 | ▶ | } elsif ( $self->{options}{blank_reload_error} && $output !~ /\S/ ) { | |||||||||||||||||||||||||||||||
|
Condition100%3/3
$self->{"options"}{"blank_reload_error"} and not $output =~ /\S/
MC/DC100%2/2
$self->{"options"}{"blank_reload_error"} and not $output =~ /\S/
$self->{"options"}{"blank_reload_error"}
not $output =~ /\S/
Truth table100%3/3
A and not(B)
A= $self->{"options"}{"blank_reload_error"}B= not $output =~ /\S/
| |||||||||||||||||||||||||||||||||
| 265 | 1 | $self->{error} = $error_flag; | |||||||||||||||||||||||||||||||
| 266 | $self->{errorString} | ||||||||||||||||||||||||||||||||
| 267 | 1 | = 'reload command "' . $reload . '" produced no output and blank_reload_error is true'; | |||||||||||||||||||||||||||||||
| 268 | 1 | $self->warn; | |||||||||||||||||||||||||||||||
| 269 | } | ||||||||||||||||||||||||||||||||
| 270 | } | ||||||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||||||
| 272 | 14 | return; | |||||||||||||||||||||||||||||||
| 273 | } ## end sub _apply | ||||||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||||||
| 275 | =head2 init | ||||||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||||||
| 277 | Initiates the backend, rendering an initial (empty unless bans were | ||||||||||||||||||||||||||||||||
| 278 | pre-seeded) file and running the reload hook. | ||||||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||||||
| 280 | Note that the consumer must be configured to read the rendered file for any | ||||||||||||||||||||||||||||||||
| 281 | of this to have an effect. See L</SYNOPSIS>. | ||||||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||||||
| 283 | =cut | ||||||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||||||
| 285 | sub init { | ||||||||||||||||||||||||||||||||
| 286 | 10 | ▶ | my ( $self, %opts ) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
init: called CC=2 SCAR=8.1
Pod
documented
| |||||||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||||||
| 288 | 10 | $self->errorblank; | |||||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||||||
| 290 | 10 | ▶ | if ( $self->{inited} ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 291 | 0 | $self->{error} = 18; | |||||||||||||||||||||||||||||||
| 292 | 0 | $self->{errorString} = 'backend has already been inited'; | |||||||||||||||||||||||||||||||
| 293 | 0 | $self->warn; | |||||||||||||||||||||||||||||||
| 294 | } | ||||||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||||||
| 296 | 10 | $self->_apply(12); | |||||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||||||
| 298 | 8 | $self->{inited} = 1; | |||||||||||||||||||||||||||||||
| 299 | } ## end sub init | ||||||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||||||
| 301 | =head2 ban | ||||||||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||||||
| 303 | Bans an IP. The value of ban is validated as being a IPv4 or IPv6 address | ||||||||||||||||||||||||||||||||
| 304 | and lowercased, then added to the ban list. The file is re-rendered and the | ||||||||||||||||||||||||||||||||
| 305 | reload command, if one is configured, is run. Banning an already banned IP | ||||||||||||||||||||||||||||||||
| 306 | is a noop. | ||||||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||||||
| 308 | $fw_helper->ban( ban => $ip ); | ||||||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||||||
| 310 | =cut | ||||||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||||||
| 312 | sub ban { | ||||||||||||||||||||||||||||||||
| 313 | 11 | ▶ | my ( $self, %opts ) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
ban: called CC=8 SCAR=28.4
Pod
documented
| |||||||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||||||
| 315 | 11 | $self->errorblank; | |||||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||||||
| 317 | 11 | ▶ | if ( !$self->{inited} ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 318 | 0 | $self->{error} = 1; | |||||||||||||||||||||||||||||||
| 319 | 0 | $self->{errorString} = 'backend has not been inited'; | |||||||||||||||||||||||||||||||
| 320 | 0 | $self->warn; | |||||||||||||||||||||||||||||||
| 321 | 0 | return; | |||||||||||||||||||||||||||||||
| 322 | } | ||||||||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||||||
| 324 | 11 | ▶ | if ( !defined( $opts{ban} ) ) { | ||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||
| 325 | 0 | $self->{error} = 9; | |||||||||||||||||||||||||||||||
| 326 | 0 | $self->{errorString} = 'Nothing specified for the value ban'; | |||||||||||||||||||||||||||||||
| 327 | 0 | $self->warn; | |||||||||||||||||||||||||||||||
| 328 | 0 | return; | |||||||||||||||||||||||||||||||
| 329 | } elsif ( ref( $opts{ban} ) ne '' ) { | ||||||||||||||||||||||||||||||||
| 330 | 0 | $self->{error} = 10; | |||||||||||||||||||||||||||||||
| 331 | 0 | $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"'; | |||||||||||||||||||||||||||||||
| 332 | 0 | $self->warn; | |||||||||||||||||||||||||||||||
| 333 | 0 | return; | |||||||||||||||||||||||||||||||
| 334 | ▶ | } elsif ( $opts{ban} !~ /\A$IPv4_re\z/ | |||||||||||||||||||||||||||||||
|
Condition66%2/3
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::file_reload::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::file_reload::IPv6_re\z/
MC/DC0%0/2
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::file_reload::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::file_reload::IPv6_re\z/
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::file_reload::IPv4_re\z/
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::file_reload::IPv6_re\z/
Truth table66%2/3
A || B
A= $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::file_reload::IPv4_re\z/B= $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::file_reload::IPv6_re\z/
| |||||||||||||||||||||||||||||||||
| 335 | && $opts{ban} !~ /\A$IPv6_re\z/ ) | ||||||||||||||||||||||||||||||||
| 336 | { | ||||||||||||||||||||||||||||||||
| 337 | 0 | $self->{error} = 10; | |||||||||||||||||||||||||||||||
| 338 | 0 | $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 IP'; | |||||||||||||||||||||||||||||||
| 339 | 0 | $self->warn; | |||||||||||||||||||||||||||||||
| 340 | 0 | return; | |||||||||||||||||||||||||||||||
| 341 | } | ||||||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||||||
| 343 | # lowercase so the same IPv6 IP in differing cases can't result in duplicate entries | ||||||||||||||||||||||||||||||||
| 344 | 11 | $opts{ban} = lc( $opts{ban} ); | |||||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||||||
| 346 | 11 | ▶ | if ( $self->{banned}{ $opts{ban} } ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 347 | 1 | ▶ | if ( $self->{testing} ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 348 | 1 | $self->{frontend_obj}->{test_data} = 'already banned'; | |||||||||||||||||||||||||||||||
| 349 | } | ||||||||||||||||||||||||||||||||
| 350 | 1 | return; | |||||||||||||||||||||||||||||||
| 351 | } | ||||||||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||||||
| 353 | 10 | $self->{banned}{ $opts{ban} } = 1; | |||||||||||||||||||||||||||||||
| 354 | 10 | $self->_apply(13); | |||||||||||||||||||||||||||||||
| 355 | } ## end sub ban | ||||||||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||||||
| 357 | =head2 unban | ||||||||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||||||
| 359 | Unbans an IP. The value of ban is validated as being a IPv4 or IPv6 address | ||||||||||||||||||||||||||||||||
| 360 | and lowercased, then removed from the ban list. The file is re-rendered | ||||||||||||||||||||||||||||||||
| 361 | without it and the reload command, if one is configured, is run. Unbanning | ||||||||||||||||||||||||||||||||
| 362 | an IP that is not banned is a noop. | ||||||||||||||||||||||||||||||||
| 363 | |||||||||||||||||||||||||||||||||
| 364 | $fw_helper->unban( ban => $ip ); | ||||||||||||||||||||||||||||||||
| 365 | |||||||||||||||||||||||||||||||||
| 366 | =cut | ||||||||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||||||
| 368 | sub unban { | ||||||||||||||||||||||||||||||||
| 369 | 3 | ▶ | my ( $self, %opts ) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
unban: called CC=8 SCAR=32.2
Pod
documented
| |||||||||||||||||||||||||||||||||
| 370 | |||||||||||||||||||||||||||||||||
| 371 | 3 | $self->errorblank; | |||||||||||||||||||||||||||||||
| 372 | |||||||||||||||||||||||||||||||||
| 373 | 3 | ▶ | if ( !$self->{inited} ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 374 | 0 | $self->{error} = 1; | |||||||||||||||||||||||||||||||
| 375 | 0 | $self->{errorString} = 'backend has not been inited'; | |||||||||||||||||||||||||||||||
| 376 | 0 | $self->warn; | |||||||||||||||||||||||||||||||
| 377 | 0 | return; | |||||||||||||||||||||||||||||||
| 378 | } | ||||||||||||||||||||||||||||||||
| 379 | |||||||||||||||||||||||||||||||||
| 380 | 3 | ▶ | if ( !defined( $opts{ban} ) ) { | ||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||
| 381 | 0 | $self->{error} = 9; | |||||||||||||||||||||||||||||||
| 382 | 0 | $self->{errorString} = 'Nothing specified for the value ban'; | |||||||||||||||||||||||||||||||
| 383 | 0 | $self->warn; | |||||||||||||||||||||||||||||||
| 384 | 0 | return; | |||||||||||||||||||||||||||||||
| 385 | } elsif ( ref( $opts{ban} ) ne '' ) { | ||||||||||||||||||||||||||||||||
| 386 | 0 | $self->{error} = 10; | |||||||||||||||||||||||||||||||
| 387 | 0 | $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"'; | |||||||||||||||||||||||||||||||
| 388 | 0 | $self->warn; | |||||||||||||||||||||||||||||||
| 389 | 0 | return; | |||||||||||||||||||||||||||||||
| 390 | ▶ | } elsif ( $opts{ban} !~ /\A$IPv4_re\z/ | |||||||||||||||||||||||||||||||
|
Condition66%2/3
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::file_reload::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::file_reload::IPv6_re\z/
MC/DC0%0/2
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::file_reload::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::file_reload::IPv6_re\z/
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::file_reload::IPv4_re\z/
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::file_reload::IPv6_re\z/
Truth table66%2/3
A || B
A= $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::file_reload::IPv4_re\z/B= $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::file_reload::IPv6_re\z/
| |||||||||||||||||||||||||||||||||
| 391 | && $opts{ban} !~ /\A$IPv6_re\z/ ) | ||||||||||||||||||||||||||||||||
| 392 | { | ||||||||||||||||||||||||||||||||
| 393 | 0 | $self->{error} = 10; | |||||||||||||||||||||||||||||||
| 394 | 0 | $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 IP'; | |||||||||||||||||||||||||||||||
| 395 | 0 | $self->warn; | |||||||||||||||||||||||||||||||
| 396 | 0 | return; | |||||||||||||||||||||||||||||||
| 397 | } | ||||||||||||||||||||||||||||||||
| 398 | |||||||||||||||||||||||||||||||||
| 399 | # lowercase so the same IPv6 IP in differing cases can't result in duplicate entries | ||||||||||||||||||||||||||||||||
| 400 | 3 | $opts{ban} = lc( $opts{ban} ); | |||||||||||||||||||||||||||||||
| 401 | |||||||||||||||||||||||||||||||||
| 402 | 3 | ▶ | if ( !$self->{banned}{ $opts{ban} } ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 403 | 0 | ▶ | if ( $self->{testing} ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 404 | 0 | $self->{frontend_obj}->{test_data} = 'not banned'; | |||||||||||||||||||||||||||||||
| 405 | } | ||||||||||||||||||||||||||||||||
| 406 | 0 | return; | |||||||||||||||||||||||||||||||
| 407 | } | ||||||||||||||||||||||||||||||||
| 408 | |||||||||||||||||||||||||||||||||
| 409 | 3 | delete( $self->{banned}{ $opts{ban} } ); | |||||||||||||||||||||||||||||||
| 410 | 3 | $self->_apply(14); | |||||||||||||||||||||||||||||||
| 411 | } ## end sub unban | ||||||||||||||||||||||||||||||||
| 412 | |||||||||||||||||||||||||||||||||
| 413 | # Internal helper. Returns a true value if the passed scalar is a valid IPv4 or | ||||||||||||||||||||||||||||||||
| 414 | # IPv6 CIDR range, that is an address followed by "/" and a prefix length that | ||||||||||||||||||||||||||||||||
| 415 | # is within the range valid for its family (0 to 32 for IPv4, 0 to 128 for | ||||||||||||||||||||||||||||||||
| 416 | # IPv6). Returns false otherwise. | ||||||||||||||||||||||||||||||||
| 417 | sub _valid_cidr { | ||||||||||||||||||||||||||||||||
| 418 | 3 | ▶ | my ( $self, $cidr ) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
_valid_cidr: called CC=8 SCAR=30.3
| |||||||||||||||||||||||||||||||||
| 419 | |||||||||||||||||||||||||||||||||
| 420 | 3 | ▶ | return 0 if ( !defined($cidr) || ref($cidr) ne '' ); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not defined $cidr or ref $cidr ne ""
MC/DC0%0/2
not defined $cidr or ref $cidr ne ""
not defined $cidr
ref $cidr ne ""
Truth table33%1/3
not(A) or B
A= not defined $cidrB= ref $cidr ne ""
| |||||||||||||||||||||||||||||||||
| 421 | |||||||||||||||||||||||||||||||||
| 422 | 3 | ▶ | if ( $cidr =~ m!\A(.+)/([0-9]{1,3})\z! ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 423 | 3 | my ( $addr, $prefix ) = ( $1, $2 ); | |||||||||||||||||||||||||||||||
| 424 | 3 | ▶ | return 1 if ( $addr =~ /\A$IPv4_re\z/ && $prefix <= 32 ); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$addr =~ /\A$Net::Firewall::BlockerHelper::backends::file_reload::IPv4_re\z/ and $prefix <= 32
MC/DC0%0/2
$addr =~ /\A$Net::Firewall::BlockerHelper::backends::file_reload::IPv4_re\z/ and $prefix <= 32
$addr =~ /\A$Net::Firewall::BlockerHelper::backends::file_reload::IPv4_re\z/
$prefix <= 32
Truth table33%1/3
A and B
A= $addr =~ /\A$Net::Firewall::BlockerHelper::backends::file_reload::IPv4_re\z/B= $prefix <= 32
| |||||||||||||||||||||||||||||||||
| 425 | 0 | ▶ | return 1 if ( $addr =~ /\A$IPv6_re\z/ && $prefix <= 128 ); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$addr =~ /\A$Net::Firewall::BlockerHelper::backends::file_reload::IPv6_re\z/ and $prefix <= 128
MC/DC0%0/2
$addr =~ /\A$Net::Firewall::BlockerHelper::backends::file_reload::IPv6_re\z/ and $prefix <= 128
$addr =~ /\A$Net::Firewall::BlockerHelper::backends::file_reload::IPv6_re\z/
$prefix <= 128
Truth table0%0/3
A and B
A= $addr =~ /\A$Net::Firewall::BlockerHelper::backends::file_reload::IPv6_re\z/B= $prefix <= 128
| |||||||||||||||||||||||||||||||||
| 426 | } | ||||||||||||||||||||||||||||||||
| 427 | |||||||||||||||||||||||||||||||||
| 428 | 0 | return 0; | |||||||||||||||||||||||||||||||
| 429 | } ## end sub _valid_cidr | ||||||||||||||||||||||||||||||||
| 430 | |||||||||||||||||||||||||||||||||
| 431 | =head2 ban_cidr | ||||||||||||||||||||||||||||||||
| 432 | |||||||||||||||||||||||||||||||||
| 433 | Bans a CIDR range by rendering it into the file the same way a single IP is. | ||||||||||||||||||||||||||||||||
| 434 | |||||||||||||||||||||||||||||||||
| 435 | $fw_helper->ban_cidr(ban => '1.2.3.0/24'); | ||||||||||||||||||||||||||||||||
| 436 | |||||||||||||||||||||||||||||||||
| 437 | =cut | ||||||||||||||||||||||||||||||||
| 438 | |||||||||||||||||||||||||||||||||
| 439 | sub ban_cidr { | ||||||||||||||||||||||||||||||||
| 440 | 2 | ▶ | my ( $self, %opts ) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
ban_cidr: called CC=7 SCAR=30.7
Pod
documented
| |||||||||||||||||||||||||||||||||
| 441 | |||||||||||||||||||||||||||||||||
| 442 | 2 | $self->errorblank; | |||||||||||||||||||||||||||||||
| 443 | |||||||||||||||||||||||||||||||||
| 444 | 2 | ▶ | if ( !$self->{inited} ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 445 | 0 | $self->{error} = 1; | |||||||||||||||||||||||||||||||
| 446 | 0 | $self->{errorString} = 'backend has not been inited'; | |||||||||||||||||||||||||||||||
| 447 | 0 | $self->warn; | |||||||||||||||||||||||||||||||
| 448 | 0 | return; | |||||||||||||||||||||||||||||||
| 449 | } | ||||||||||||||||||||||||||||||||
| 450 | |||||||||||||||||||||||||||||||||
| 451 | 2 | ▶ | if ( !defined( $opts{ban} ) ) { | ||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||
| 452 | 0 | $self->{error} = 9; | |||||||||||||||||||||||||||||||
| 453 | 0 | $self->{errorString} = 'Nothing specified for the value ban'; | |||||||||||||||||||||||||||||||
| 454 | 0 | $self->warn; | |||||||||||||||||||||||||||||||
| 455 | 0 | return; | |||||||||||||||||||||||||||||||
| 456 | } elsif ( ref( $opts{ban} ) ne '' ) { | ||||||||||||||||||||||||||||||||
| 457 | 0 | $self->{error} = 34; | |||||||||||||||||||||||||||||||
| 458 | 0 | $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"'; | |||||||||||||||||||||||||||||||
| 459 | 0 | $self->warn; | |||||||||||||||||||||||||||||||
| 460 | 0 | return; | |||||||||||||||||||||||||||||||
| 461 | } elsif ( !$self->_valid_cidr( $opts{ban} ) ) { | ||||||||||||||||||||||||||||||||
| 462 | 0 | $self->{error} = 34; | |||||||||||||||||||||||||||||||
| 463 | 0 | $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 CIDR'; | |||||||||||||||||||||||||||||||
| 464 | 0 | $self->warn; | |||||||||||||||||||||||||||||||
| 465 | 0 | return; | |||||||||||||||||||||||||||||||
| 466 | } | ||||||||||||||||||||||||||||||||
| 467 | |||||||||||||||||||||||||||||||||
| 468 | # lowercase so the same IPv6 CIDR in differing cases can't result in duplicate entries | ||||||||||||||||||||||||||||||||
| 469 | 2 | $opts{ban} = lc( $opts{ban} ); | |||||||||||||||||||||||||||||||
| 470 | |||||||||||||||||||||||||||||||||
| 471 | 2 | ▶ | if ( $self->{banned_cidr}{ $opts{ban} } ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 472 | 0 | ▶ | if ( $self->{testing} ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 473 | 0 | $self->{frontend_obj}->{test_data} = 'already banned'; | |||||||||||||||||||||||||||||||
| 474 | } | ||||||||||||||||||||||||||||||||
| 475 | 0 | return; | |||||||||||||||||||||||||||||||
| 476 | } | ||||||||||||||||||||||||||||||||
| 477 | |||||||||||||||||||||||||||||||||
| 478 | 2 | $self->{banned_cidr}{ $opts{ban} } = 1; | |||||||||||||||||||||||||||||||
| 479 | 2 | $self->_apply(32); | |||||||||||||||||||||||||||||||
| 480 | } ## end sub ban_cidr | ||||||||||||||||||||||||||||||||
| 481 | |||||||||||||||||||||||||||||||||
| 482 | =head2 unban_cidr | ||||||||||||||||||||||||||||||||
| 483 | |||||||||||||||||||||||||||||||||
| 484 | Unbans a CIDR range by re-rendering the file without it. | ||||||||||||||||||||||||||||||||
| 485 | |||||||||||||||||||||||||||||||||
| 486 | $fw_helper->unban_cidr(ban => '1.2.3.0/24'); | ||||||||||||||||||||||||||||||||
| 487 | |||||||||||||||||||||||||||||||||
| 488 | =cut | ||||||||||||||||||||||||||||||||
| 489 | |||||||||||||||||||||||||||||||||
| 490 | sub unban_cidr { | ||||||||||||||||||||||||||||||||
| 491 | 1 | ▶ | my ( $self, %opts ) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
unban_cidr: called CC=7 SCAR=30.7
Pod
documented
| |||||||||||||||||||||||||||||||||
| 492 | |||||||||||||||||||||||||||||||||
| 493 | 1 | $self->errorblank; | |||||||||||||||||||||||||||||||
| 494 | |||||||||||||||||||||||||||||||||
| 495 | 1 | ▶ | if ( !$self->{inited} ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 496 | 0 | $self->{error} = 1; | |||||||||||||||||||||||||||||||
| 497 | 0 | $self->{errorString} = 'backend has not been inited'; | |||||||||||||||||||||||||||||||
| 498 | 0 | $self->warn; | |||||||||||||||||||||||||||||||
| 499 | 0 | return; | |||||||||||||||||||||||||||||||
| 500 | } | ||||||||||||||||||||||||||||||||
| 501 | |||||||||||||||||||||||||||||||||
| 502 | 1 | ▶ | if ( !defined( $opts{ban} ) ) { | ||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||
| 503 | 0 | $self->{error} = 9; | |||||||||||||||||||||||||||||||
| 504 | 0 | $self->{errorString} = 'Nothing specified for the value ban'; | |||||||||||||||||||||||||||||||
| 505 | 0 | $self->warn; | |||||||||||||||||||||||||||||||
| 506 | 0 | return; | |||||||||||||||||||||||||||||||
| 507 | } elsif ( ref( $opts{ban} ) ne '' ) { | ||||||||||||||||||||||||||||||||
| 508 | 0 | $self->{error} = 34; | |||||||||||||||||||||||||||||||
| 509 | 0 | $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"'; | |||||||||||||||||||||||||||||||
| 510 | 0 | $self->warn; | |||||||||||||||||||||||||||||||
| 511 | 0 | return; | |||||||||||||||||||||||||||||||
| 512 | } elsif ( !$self->_valid_cidr( $opts{ban} ) ) { | ||||||||||||||||||||||||||||||||
| 513 | 0 | $self->{error} = 34; | |||||||||||||||||||||||||||||||
| 514 | 0 | $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 CIDR'; | |||||||||||||||||||||||||||||||
| 515 | 0 | $self->warn; | |||||||||||||||||||||||||||||||
| 516 | 0 | return; | |||||||||||||||||||||||||||||||
| 517 | } | ||||||||||||||||||||||||||||||||
| 518 | |||||||||||||||||||||||||||||||||
| 519 | # lowercase so the same IPv6 CIDR in differing cases can't result in duplicate entries | ||||||||||||||||||||||||||||||||
| 520 | 1 | $opts{ban} = lc( $opts{ban} ); | |||||||||||||||||||||||||||||||
| 521 | |||||||||||||||||||||||||||||||||
| 522 | 1 | ▶ | if ( !$self->{banned_cidr}{ $opts{ban} } ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 523 | 0 | ▶ | if ( $self->{testing} ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 524 | 0 | $self->{frontend_obj}->{test_data} = 'not banned'; | |||||||||||||||||||||||||||||||
| 525 | } | ||||||||||||||||||||||||||||||||
| 526 | 0 | return; | |||||||||||||||||||||||||||||||
| 527 | } | ||||||||||||||||||||||||||||||||
| 528 | |||||||||||||||||||||||||||||||||
| 529 | 1 | delete( $self->{banned_cidr}{ $opts{ban} } ); | |||||||||||||||||||||||||||||||
| 530 | 1 | $self->_apply(33); | |||||||||||||||||||||||||||||||
| 531 | } ## end sub unban_cidr | ||||||||||||||||||||||||||||||||
| 532 | |||||||||||||||||||||||||||||||||
| 533 | =head2 list_cidr | ||||||||||||||||||||||||||||||||
| 534 | |||||||||||||||||||||||||||||||||
| 535 | List banned CIDR ranges. | ||||||||||||||||||||||||||||||||
| 536 | |||||||||||||||||||||||||||||||||
| 537 | my @banned_cidrs = $fw_helper->list_cidr; | ||||||||||||||||||||||||||||||||
| 538 | |||||||||||||||||||||||||||||||||
| 539 | =cut | ||||||||||||||||||||||||||||||||
| 540 | |||||||||||||||||||||||||||||||||
| 541 | sub list_cidr { | ||||||||||||||||||||||||||||||||
| 542 | 2 | ▶ | my ( $self, %opts ) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
list_cidr: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 543 | |||||||||||||||||||||||||||||||||
| 544 | 2 | $self->errorblank; | |||||||||||||||||||||||||||||||
| 545 | |||||||||||||||||||||||||||||||||
| 546 | 2 | ▶ | if ( $self->{testing} ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 547 | 2 | $self->{frontend_obj}->{test_data} = 'list_cidr'; | |||||||||||||||||||||||||||||||
| 548 | } | ||||||||||||||||||||||||||||||||
| 549 | |||||||||||||||||||||||||||||||||
| 550 | 2 | return keys( %{ $self->{banned_cidr} } ); | |||||||||||||||||||||||||||||||
| 551 | } | ||||||||||||||||||||||||||||||||
| 552 | |||||||||||||||||||||||||||||||||
| 553 | =head2 list | ||||||||||||||||||||||||||||||||
| 554 | |||||||||||||||||||||||||||||||||
| 555 | List banned IPs. Returns an array of the currently banned single IPs. CIDR | ||||||||||||||||||||||||||||||||
| 556 | ranges are not included; for those see L</list_cidr>. | ||||||||||||||||||||||||||||||||
| 557 | |||||||||||||||||||||||||||||||||
| 558 | my @banned = $fw_helper->list; | ||||||||||||||||||||||||||||||||
| 559 | |||||||||||||||||||||||||||||||||
| 560 | =cut | ||||||||||||||||||||||||||||||||
| 561 | |||||||||||||||||||||||||||||||||
| 562 | sub list { | ||||||||||||||||||||||||||||||||
| 563 | 1 | ▶ | my ( $self, %opts ) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
list: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 564 | |||||||||||||||||||||||||||||||||
| 565 | 1 | $self->errorblank; | |||||||||||||||||||||||||||||||
| 566 | |||||||||||||||||||||||||||||||||
| 567 | 1 | ▶ | if ( $self->{testing} ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 568 | 1 | $self->{frontend_obj}->{test_data} = 'list'; | |||||||||||||||||||||||||||||||
| 569 | } | ||||||||||||||||||||||||||||||||
| 570 | |||||||||||||||||||||||||||||||||
| 571 | 1 | return keys( %{ $self->{banned} } ); | |||||||||||||||||||||||||||||||
| 572 | } | ||||||||||||||||||||||||||||||||
| 573 | |||||||||||||||||||||||||||||||||
| 574 | =head2 re_init | ||||||||||||||||||||||||||||||||
| 575 | |||||||||||||||||||||||||||||||||
| 576 | Re-renders the file from the retained ban list and runs the reload hook. As | ||||||||||||||||||||||||||||||||
| 577 | the file is always rendered from full state, nothing needs tearing down | ||||||||||||||||||||||||||||||||
| 578 | first; a single re-render restores everything. | ||||||||||||||||||||||||||||||||
| 579 | |||||||||||||||||||||||||||||||||
| 580 | =cut | ||||||||||||||||||||||||||||||||
| 581 | |||||||||||||||||||||||||||||||||
| 582 | sub re_init { | ||||||||||||||||||||||||||||||||
| 583 | 1 | ▶ | my ( $self, %opts ) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
re_init: called CC=2 SCAR=8.7
Pod
documented
| |||||||||||||||||||||||||||||||||
| 584 | |||||||||||||||||||||||||||||||||
| 585 | 1 | $self->errorblank; | |||||||||||||||||||||||||||||||
| 586 | |||||||||||||||||||||||||||||||||
| 587 | 1 | ▶ | if ( !$self->{inited} ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 588 | 0 | $self->{error} = 1; | |||||||||||||||||||||||||||||||
| 589 | 0 | $self->{errorString} = 'backend has not been inited'; | |||||||||||||||||||||||||||||||
| 590 | 0 | $self->warn; | |||||||||||||||||||||||||||||||
| 591 | 0 | return; | |||||||||||||||||||||||||||||||
| 592 | } | ||||||||||||||||||||||||||||||||
| 593 | |||||||||||||||||||||||||||||||||
| 594 | # render the current ban list back out; the file is authored purely from | ||||||||||||||||||||||||||||||||
| 595 | # state so this restores everything in one write | ||||||||||||||||||||||||||||||||
| 596 | 1 | $self->_apply(16); | |||||||||||||||||||||||||||||||
| 597 | |||||||||||||||||||||||||||||||||
| 598 | 1 | $self->{inited} = 1; | |||||||||||||||||||||||||||||||
| 599 | } ## end sub re_init | ||||||||||||||||||||||||||||||||
| 600 | |||||||||||||||||||||||||||||||||
| 601 | =head2 teardown | ||||||||||||||||||||||||||||||||
| 602 | |||||||||||||||||||||||||||||||||
| 603 | Removes the rendered file (or empties it when remove_on_teardown is false) | ||||||||||||||||||||||||||||||||
| 604 | and runs the reload hook. | ||||||||||||||||||||||||||||||||
| 605 | |||||||||||||||||||||||||||||||||
| 606 | =cut | ||||||||||||||||||||||||||||||||
| 607 | |||||||||||||||||||||||||||||||||
| 608 | sub teardown { | ||||||||||||||||||||||||||||||||
| 609 | 3 | ▶ | my ( $self, %opts ) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
teardown: called CC=9 SCAR=24.2
Pod
documented
| |||||||||||||||||||||||||||||||||
| 610 | |||||||||||||||||||||||||||||||||
| 611 | 3 | $self->errorblank; | |||||||||||||||||||||||||||||||
| 612 | |||||||||||||||||||||||||||||||||
| 613 | 3 | $self->{inited} = 0; | |||||||||||||||||||||||||||||||
| 614 | |||||||||||||||||||||||||||||||||
| 615 | 3 | ▶ | if ( $self->{options}{remove_on_teardown} ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 616 | 2 | ▶ | if ( $self->{testing} ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 617 | $self->{frontend_obj}->{test_data} = { | ||||||||||||||||||||||||||||||||
| 618 | file => $self->{options}{file}, | ||||||||||||||||||||||||||||||||
| 619 | content => undef, | ||||||||||||||||||||||||||||||||
| 620 | reload => $self->{options}{reload}, | ||||||||||||||||||||||||||||||||
| 621 | 1 | }; | |||||||||||||||||||||||||||||||
| 622 | } else { | ||||||||||||||||||||||||||||||||
| 623 | # unlink is best effort; a missing file is already the desired state | ||||||||||||||||||||||||||||||||
| 624 | 1 | unlink( $self->{options}{file} ); | |||||||||||||||||||||||||||||||
| 625 | 1 | my $reload = $self->{options}{reload}; | |||||||||||||||||||||||||||||||
| 626 | 1 | ▶ | if ( defined($reload) && $reload ne '' ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $reload and $reload ne ""
MC/DC0%0/2
defined $reload and $reload ne ""
defined $reload
$reload ne ""
Truth table33%1/3
A and B
A= defined $reloadB= $reload ne ""
| |||||||||||||||||||||||||||||||||
| 627 | 1 | my $output = `$reload 2>&1`; | |||||||||||||||||||||||||||||||
| 628 | 1 | ▶ | $output = '' if ( !defined($output) ); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 629 | 1 | ▶ | if ( $? != 0 ) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 630 | 0 | $self->{error} = 17; | |||||||||||||||||||||||||||||||
| 631 | 0 | $self->{errorString} = 'reload command "' . $reload . '" failed... ' . $output; | |||||||||||||||||||||||||||||||
| 632 | 0 | $self->warn; | |||||||||||||||||||||||||||||||
| 633 | ▶ | } elsif ( $self->{options}{blank_reload_error} && $output !~ /\S/ ) { | |||||||||||||||||||||||||||||||
|
Condition33%1/3
$self->{"options"}{"blank_reload_error"} and not $output =~ /\S/
MC/DC0%0/2
$self->{"options"}{"blank_reload_error"} and not $output =~ /\S/
$self->{"options"}{"blank_reload_error"}
not $output =~ /\S/
Truth table33%1/3
A and not(B)
A= $self->{"options"}{"blank_reload_error"}B= not $output =~ /\S/
| |||||||||||||||||||||||||||||||||
| 634 | 0 | $self->{error} = 17; | |||||||||||||||||||||||||||||||
| 635 | $self->{errorString} | ||||||||||||||||||||||||||||||||
| 636 | 0 | = 'reload command "' . $reload . '" produced no output and blank_reload_error is true'; | |||||||||||||||||||||||||||||||
| 637 | 0 | $self->warn; | |||||||||||||||||||||||||||||||
| 638 | } | ||||||||||||||||||||||||||||||||
| 639 | } | ||||||||||||||||||||||||||||||||
| 640 | } | ||||||||||||||||||||||||||||||||
| 641 | 2 | return; | |||||||||||||||||||||||||||||||
| 642 | } ## end if ( $self->{options}{remove_on_teardown...}) | ||||||||||||||||||||||||||||||||
| 643 | |||||||||||||||||||||||||||||||||
| 644 | # leave an empty (header/footer only) file behind; both the single IP and | ||||||||||||||||||||||||||||||||
| 645 | # CIDR state are stashed so the empty render omits them but they survive | ||||||||||||||||||||||||||||||||
| 646 | 1 | my %saved_banned = %{ $self->{banned} }; | |||||||||||||||||||||||||||||||
| 647 | 1 | my %saved_banned_cidr = %{ $self->{banned_cidr} }; | |||||||||||||||||||||||||||||||
| 648 | 1 | $self->{banned} = {}; | |||||||||||||||||||||||||||||||
| 649 | 1 | $self->{banned_cidr} = {}; | |||||||||||||||||||||||||||||||
| 650 | 1 | $self->_apply(17); | |||||||||||||||||||||||||||||||
| 651 | 1 | $self->{banned} = \%saved_banned; | |||||||||||||||||||||||||||||||
| 652 | 1 | $self->{banned_cidr} = \%saved_banned_cidr; | |||||||||||||||||||||||||||||||
| 653 | } ## end sub teardown | ||||||||||||||||||||||||||||||||
| 654 | |||||||||||||||||||||||||||||||||
| 655 | =head2 stop | ||||||||||||||||||||||||||||||||
| 656 | |||||||||||||||||||||||||||||||||
| 657 | Alias for L</teardown>. | ||||||||||||||||||||||||||||||||
| 658 | |||||||||||||||||||||||||||||||||
| 659 | =cut | ||||||||||||||||||||||||||||||||
| 660 | |||||||||||||||||||||||||||||||||
| 661 | sub stop { | ||||||||||||||||||||||||||||||||
| 662 | 0 | ▶ | my ( $self, %opts ) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
stop: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 663 | |||||||||||||||||||||||||||||||||
| 664 | 0 | return $self->teardown(%opts); | |||||||||||||||||||||||||||||||
| 665 | } | ||||||||||||||||||||||||||||||||
| 666 | |||||||||||||||||||||||||||||||||
| 667 | =head2 check | ||||||||||||||||||||||||||||||||
| 668 | |||||||||||||||||||||||||||||||||
| 669 | If a C<check> command is configured, runs it and treats a zero exit as | ||||||||||||||||||||||||||||||||
| 670 | healthy. Otherwise verifies the rendered file still exists. | ||||||||||||||||||||||||||||||||
| 671 | |||||||||||||||||||||||||||||||||
| 672 | =cut | ||||||||||||||||||||||||||||||||
| 673 | |||||||||||||||||||||||||||||||||
| 674 | sub check { | ||||||||||||||||||||||||||||||||
| 675 | 17 | ▶ | my ( $self, %opts ) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
check: called CC=7 SCAR=19.7
Pod
documented
| |||||||||||||||||||||||||||||||||
| 676 | |||||||||||||||||||||||||||||||||
| 677 | 17 | $self->errorblank; | |||||||||||||||||||||||||||||||
| 678 | |||||||||||||||||||||||||||||||||
| 679 | 17 | ▶ | if ( defined( $self->{options}{check} ) && $self->{options}{check} ne '' ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $self->{"options"}{"check"} and $self->{"options"}{"check"} ne ""
MC/DC50%1/2
defined $self->{"options"}{"check"} and $self->{"options"}{"check"} ne ""
defined $self->{"options"}{"check"}
$self->{"options"}{"check"} ne ""
Truth table66%2/3
A and B
A= defined $self->{"options"}{"check"}B= $self->{"options"}{"check"} ne ""
| |||||||||||||||||||||||||||||||||
| 680 | 3 | my $command = $self->{options}{check}; | |||||||||||||||||||||||||||||||
| 681 | 3 | ▶ | if ( $self->{testing} ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 682 | 0 | $self->{frontend_obj}->{test_data} = $command; | |||||||||||||||||||||||||||||||
| 683 | 0 | return 1; | |||||||||||||||||||||||||||||||
| 684 | } | ||||||||||||||||||||||||||||||||
| 685 | 3 | my $output = `$command 2>&1`; | |||||||||||||||||||||||||||||||
| 686 | 3 | ▶ | return $? == 0 ? 1 : 0; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 687 | } | ||||||||||||||||||||||||||||||||
| 688 | |||||||||||||||||||||||||||||||||
| 689 | 14 | ▶ | if ( $self->{testing} ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 690 | 7 | $self->{frontend_obj}->{test_data} = 'check'; | |||||||||||||||||||||||||||||||
| 691 | 7 | return 1; | |||||||||||||||||||||||||||||||
| 692 | } | ||||||||||||||||||||||||||||||||
| 693 | |||||||||||||||||||||||||||||||||
| 694 | 7 | ▶ | return ( -e $self->{options}{file} ) ? 1 : 0; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 695 | } ## end sub check | ||||||||||||||||||||||||||||||||
| 696 | |||||||||||||||||||||||||||||||||
| 697 | =head2 flush | ||||||||||||||||||||||||||||||||
| 698 | |||||||||||||||||||||||||||||||||
| 699 | Removes all currently banned IPs at once by clearing the ban list and | ||||||||||||||||||||||||||||||||
| 700 | re-rendering the now empty file. | ||||||||||||||||||||||||||||||||
| 701 | |||||||||||||||||||||||||||||||||
| 702 | =cut | ||||||||||||||||||||||||||||||||
| 703 | |||||||||||||||||||||||||||||||||
| 704 | sub flush { | ||||||||||||||||||||||||||||||||
| 705 | 1 | ▶ | my ( $self, %opts ) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
flush: called CC=2 SCAR=8.3
Pod
documented
| |||||||||||||||||||||||||||||||||
| 706 | |||||||||||||||||||||||||||||||||
| 707 | 1 | $self->errorblank; | |||||||||||||||||||||||||||||||
| 708 | |||||||||||||||||||||||||||||||||
| 709 | 1 | ▶ | if ( !$self->{inited} ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 710 | 0 | $self->{error} = 1; | |||||||||||||||||||||||||||||||
| 711 | 0 | $self->{errorString} = 'backend has not been inited'; | |||||||||||||||||||||||||||||||
| 712 | 0 | $self->warn; | |||||||||||||||||||||||||||||||
| 713 | 0 | return; | |||||||||||||||||||||||||||||||
| 714 | } | ||||||||||||||||||||||||||||||||
| 715 | |||||||||||||||||||||||||||||||||
| 716 | # the file is re-rendered from full state, so clearing both hashes removes | ||||||||||||||||||||||||||||||||
| 717 | # every single IP and CIDR ban at once | ||||||||||||||||||||||||||||||||
| 718 | 1 | $self->{banned} = {}; | |||||||||||||||||||||||||||||||
| 719 | 1 | $self->{banned_cidr} = {}; | |||||||||||||||||||||||||||||||
| 720 | 1 | $self->_apply(25); | |||||||||||||||||||||||||||||||
| 721 | } ## end sub flush | ||||||||||||||||||||||||||||||||
| 722 | |||||||||||||||||||||||||||||||||
| 723 | =head1 ERROR CODES / FLAGS | ||||||||||||||||||||||||||||||||
| 724 | |||||||||||||||||||||||||||||||||
| 725 | Error handling is provided by L<Error::Helper>. All | ||||||||||||||||||||||||||||||||
| 726 | errors are considered fatal. | ||||||||||||||||||||||||||||||||
| 727 | |||||||||||||||||||||||||||||||||
| 728 | =head2 1, notInited | ||||||||||||||||||||||||||||||||
| 729 | |||||||||||||||||||||||||||||||||
| 730 | The backend has not been inited yet. | ||||||||||||||||||||||||||||||||
| 731 | |||||||||||||||||||||||||||||||||
| 732 | =head2 8, optionsNotHash | ||||||||||||||||||||||||||||||||
| 733 | |||||||||||||||||||||||||||||||||
| 734 | The item passed to new for options is not a hash. | ||||||||||||||||||||||||||||||||
| 735 | |||||||||||||||||||||||||||||||||
| 736 | =head2 9, noBanItem | ||||||||||||||||||||||||||||||||
| 737 | |||||||||||||||||||||||||||||||||
| 738 | No IP or CIDR range specified to ban or unban. | ||||||||||||||||||||||||||||||||
| 739 | |||||||||||||||||||||||||||||||||
| 740 | =head2 10, banItemNotIP | ||||||||||||||||||||||||||||||||
| 741 | |||||||||||||||||||||||||||||||||
| 742 | The item to ban is not an IP. Either wrong ref type or regexp | ||||||||||||||||||||||||||||||||
| 743 | test using L<Regexp::IPv4> and L<Regexp::IPv6> failed. | ||||||||||||||||||||||||||||||||
| 744 | |||||||||||||||||||||||||||||||||
| 745 | =head2 12, backendInitError | ||||||||||||||||||||||||||||||||
| 746 | |||||||||||||||||||||||||||||||||
| 747 | Failed to init the backend. | ||||||||||||||||||||||||||||||||
| 748 | |||||||||||||||||||||||||||||||||
| 749 | =head2 13, banFailed | ||||||||||||||||||||||||||||||||
| 750 | |||||||||||||||||||||||||||||||||
| 751 | Failed to ban the item. | ||||||||||||||||||||||||||||||||
| 752 | |||||||||||||||||||||||||||||||||
| 753 | =head2 14, unbanFailed | ||||||||||||||||||||||||||||||||
| 754 | |||||||||||||||||||||||||||||||||
| 755 | Failed to unban the item. | ||||||||||||||||||||||||||||||||
| 756 | |||||||||||||||||||||||||||||||||
| 757 | =head2 15, listFailed | ||||||||||||||||||||||||||||||||
| 758 | |||||||||||||||||||||||||||||||||
| 759 | Failed to get a list of bans. | ||||||||||||||||||||||||||||||||
| 760 | |||||||||||||||||||||||||||||||||
| 761 | =head2 16, reInitFailed | ||||||||||||||||||||||||||||||||
| 762 | |||||||||||||||||||||||||||||||||
| 763 | Failed to re_init the backend. | ||||||||||||||||||||||||||||||||
| 764 | |||||||||||||||||||||||||||||||||
| 765 | =head2 17, teardownFailed | ||||||||||||||||||||||||||||||||
| 766 | |||||||||||||||||||||||||||||||||
| 767 | Failed to teardown the backend. | ||||||||||||||||||||||||||||||||
| 768 | |||||||||||||||||||||||||||||||||
| 769 | =head2 18, alreadyInited | ||||||||||||||||||||||||||||||||
| 770 | |||||||||||||||||||||||||||||||||
| 771 | init called, but the backend has already been inited. | ||||||||||||||||||||||||||||||||
| 772 | |||||||||||||||||||||||||||||||||
| 773 | =head2 24, checkFailed | ||||||||||||||||||||||||||||||||
| 774 | |||||||||||||||||||||||||||||||||
| 775 | The backend check raised an error. | ||||||||||||||||||||||||||||||||
| 776 | |||||||||||||||||||||||||||||||||
| 777 | =head2 25, flushFailed | ||||||||||||||||||||||||||||||||
| 778 | |||||||||||||||||||||||||||||||||
| 779 | Failed to flush the bans. | ||||||||||||||||||||||||||||||||
| 780 | |||||||||||||||||||||||||||||||||
| 781 | =head2 30, fileNotDefined | ||||||||||||||||||||||||||||||||
| 782 | |||||||||||||||||||||||||||||||||
| 783 | The option file is undef or blank. | ||||||||||||||||||||||||||||||||
| 784 | |||||||||||||||||||||||||||||||||
| 785 | =head2 31, fileWriteFailed | ||||||||||||||||||||||||||||||||
| 786 | |||||||||||||||||||||||||||||||||
| 787 | Could not open the file for writing. | ||||||||||||||||||||||||||||||||
| 788 | |||||||||||||||||||||||||||||||||
| 789 | =head2 32, banCidrFailed | ||||||||||||||||||||||||||||||||
| 790 | |||||||||||||||||||||||||||||||||
| 791 | Failed to ban the CIDR range. | ||||||||||||||||||||||||||||||||
| 792 | |||||||||||||||||||||||||||||||||
| 793 | =head2 33, unbanCidrFailed | ||||||||||||||||||||||||||||||||
| 794 | |||||||||||||||||||||||||||||||||
| 795 | Failed to unban the CIDR range. | ||||||||||||||||||||||||||||||||
| 796 | |||||||||||||||||||||||||||||||||
| 797 | =head2 34, cidrItemNotCidr | ||||||||||||||||||||||||||||||||
| 798 | |||||||||||||||||||||||||||||||||
| 799 | The item to ban is not a CIDR range. Either wrong ref type or it is not an | ||||||||||||||||||||||||||||||||
| 800 | IPv4 or IPv6 address followed by a prefix length valid for its family. | ||||||||||||||||||||||||||||||||
| 801 | |||||||||||||||||||||||||||||||||
| 802 | =head2 35, cidrNotSupported | ||||||||||||||||||||||||||||||||
| 803 | |||||||||||||||||||||||||||||||||
| 804 | The backend does not support CIDR bans. | ||||||||||||||||||||||||||||||||
| 805 | |||||||||||||||||||||||||||||||||
| 806 | =head2 36, listCidrFailed | ||||||||||||||||||||||||||||||||
| 807 | |||||||||||||||||||||||||||||||||
| 808 | Failed to get a list of CIDR bans. | ||||||||||||||||||||||||||||||||
| 809 | |||||||||||||||||||||||||||||||||
| 810 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||
| 811 | |||||||||||||||||||||||||||||||||
| 812 | Zane C. Bowers-Hadley, C<< <vvelox at vvelox.ent> >> | ||||||||||||||||||||||||||||||||
| 813 | |||||||||||||||||||||||||||||||||
| 814 | =head1 LICENSE AND COPYRIGHT | ||||||||||||||||||||||||||||||||
| 815 | |||||||||||||||||||||||||||||||||
| 816 | This software is Copyright (c) 2023 by Zane C. Bowers-Hadley. | ||||||||||||||||||||||||||||||||
| 817 | |||||||||||||||||||||||||||||||||
| 818 | This is free software, licensed under: | ||||||||||||||||||||||||||||||||
| 819 | |||||||||||||||||||||||||||||||||
| 820 | The GNU Lesser General Public License, Version 2.1, February 1999 | ||||||||||||||||||||||||||||||||
| 821 | |||||||||||||||||||||||||||||||||
| 822 | =cut | ||||||||||||||||||||||||||||||||
| 823 | |||||||||||||||||||||||||||||||||
| 824 | 1 | 1; # End of Net::Firewall::BlockerHelper::backends::file_reload | |||||||||||||||||||||||||||||||