| 1 | # | ||||||||||||||||||||||||||||||||||
| 2 | # (c) Andrew Beverley | ||||||||||||||||||||||||||||||||||
| 3 | # (c) Jan Gehring <jan.gehring@gmail.com> | ||||||||||||||||||||||||||||||||||
| 4 | # | ||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||
| 6 | package Rex::Resource::firewall::Provider::ufw; | ||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use v5.14.4; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||
| 11 | 1 | our $VERSION = '1.16.1'; # VERSION | |||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||
| 13 | 1 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 14 | 1 | ▶ | use Rex::Commands::Run; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 15 | 1 | ▶ | use Rex::Helper::Run; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||
| 17 | 1 | ▶ | use base qw(Rex::Resource::firewall::Provider::base); | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||
| 19 | 1 | my %__action_map = ( | |||||||||||||||||||||||||||||||||
| 20 | accept => "allow", | ||||||||||||||||||||||||||||||||||
| 21 | allow => "allow", | ||||||||||||||||||||||||||||||||||
| 22 | deny => "deny", ## -j DROP | ||||||||||||||||||||||||||||||||||
| 23 | drop => "deny", ## -j DROP | ||||||||||||||||||||||||||||||||||
| 24 | reject => "reject", ## -j REJECT | ||||||||||||||||||||||||||||||||||
| 25 | limit => "limit", | ||||||||||||||||||||||||||||||||||
| 26 | ); | ||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||
| 28 | 0 | ▶ | sub new { | ||||||||||||||||||||||||||||||||
|
Subroutine
new: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 29 | 0 | my $that = shift; | |||||||||||||||||||||||||||||||||
| 30 | 0 | ▶ | my $proto = ref($that) || $that; | ||||||||||||||||||||||||||||||||
|
Condition0%0/3
ref $that || $that
MC/DC0%0/2
ref $that || $that
ref $that
$that
Truth table0%0/3
A || B
A= ref $thatB= $that
| |||||||||||||||||||||||||||||||||||
| 31 | 0 | my $self = $proto->SUPER::new(@_); | |||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||
| 33 | 0 | bless( $self, $proto ); | |||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||
| 35 | 0 | return $self; | |||||||||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||
| 38 | 0 | ▶ | sub present { | ||||||||||||||||||||||||||||||||
|
Subroutine
present: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 39 | 0 | my ( $self, $rule_config ) = @_; | |||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||
| 41 | 0 | my @ufw_params = $self->_generate_rule_array($rule_config); | |||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||
| 43 | 0 | return $self->_ufw_rule( grep { defined } @ufw_params ); | |||||||||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||
| 46 | 0 | ▶ | sub absent { | ||||||||||||||||||||||||||||||||
|
Subroutine
absent: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 47 | 0 | my ( $self, $rule_config ) = @_; | |||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||
| 49 | 0 | $rule_config->{delete} = 1; | |||||||||||||||||||||||||||||||||
| 50 | 0 | my @ufw_params = $self->_generate_rule_array($rule_config); | |||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||
| 52 | 0 | return $self->_ufw_rule( grep { defined } @ufw_params ); | |||||||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||
| 55 | 0 | ▶ | sub enable { | ||||||||||||||||||||||||||||||||
|
Subroutine
enable: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 56 | 0 | my ( $self, $rule_config ) = @_; | |||||||||||||||||||||||||||||||||
| 57 | 0 | return $self->_ufw_disable_enable("enable"); | |||||||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||
| 60 | 0 | ▶ | sub disable { | ||||||||||||||||||||||||||||||||
|
Subroutine
disable: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 61 | 0 | my ( $self, $rule_config ) = @_; | |||||||||||||||||||||||||||||||||
| 62 | 0 | return $self->_ufw_disable_enable("disable"); | |||||||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||
| 65 | 0 | ▶ | sub logging { | ||||||||||||||||||||||||||||||||
|
Subroutine
logging: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 66 | 0 | my ( $self, $logging ) = @_; | |||||||||||||||||||||||||||||||||
| 67 | 0 | return $self->_ufw_logging($logging); | |||||||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||
| 70 | 0 | ▶ | sub _ufw_rule { | ||||||||||||||||||||||||||||||||
|
Subroutine
_ufw_rule: not called CC=31 SCAR=69.0
| |||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||
| 72 | 0 | my ( $self, $action, @params ) = @_; | |||||||||||||||||||||||||||||||||
| 73 | 0 | my %torun = ( | |||||||||||||||||||||||||||||||||
| 74 | action => $action, # allow, deny, limit etc | ||||||||||||||||||||||||||||||||||
| 75 | commands => [], | ||||||||||||||||||||||||||||||||||
| 76 | ); | ||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||
| 78 | 0 | my $has_app; # Has app parameters | |||||||||||||||||||||||||||||||||
| 79 | my $has_port; # Has port parameters | ||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||
| 81 | 0 | ▶ | while ( my $param = shift @params ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 82 | 0 | ▶ | if ( $param eq 'proto' ) { | ||||||||||||||||||||||||||||||||
|
Branch: 7 branches
| |||||||||||||||||||||||||||||||||||
| 83 | 0 | my $proto = shift @params; | |||||||||||||||||||||||||||||||||
| 84 | 0 | ▶ | die "Invalid protocol $proto" | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$proto eq "tcp" or $proto eq "udp"
MC/DC0%0/2
$proto eq "tcp" or $proto eq "udp"
$proto eq "tcp"
$proto eq "udp"
Truth table0%0/3
A or B
A= $proto eq "tcp"B= $proto eq "udp"
| |||||||||||||||||||||||||||||||||||
| 85 | unless ( $proto eq 'tcp' || $proto eq 'udp' ); | ||||||||||||||||||||||||||||||||||
| 86 | 0 | $torun{proto} = "proto $proto"; | |||||||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||||||||
| 88 | ▶ | elsif ( $param eq 'from' || $param eq 'to' ) { | |||||||||||||||||||||||||||||||||
|
Condition0%0/3
$param eq "from" or $param eq "to"
MC/DC0%0/2
$param eq "from" or $param eq "to"
$param eq "from"
$param eq "to"
Truth table0%0/3
A or B
A= $param eq "from"B= $param eq "to"
| |||||||||||||||||||||||||||||||||||
| 89 | 0 | my $address = shift @params; | |||||||||||||||||||||||||||||||||
| 90 | 0 | push @{ $torun{commands} }, ( $param => $address ); | |||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||
| 92 | # See if next rule is a port | ||||||||||||||||||||||||||||||||||
| 93 | 0 | ▶ | if ( $params[0] && $params[0] eq 'port' ) { | ||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition0%0/3
$params[0] and $params[0] eq "port"
MC/DC0%0/2
$params[0] and $params[0] eq "port"
$params[0]
$params[0] eq "port"
Truth table0%0/3
A and B
A= $params[0]B= $params[0] eq "port"
| |||||||||||||||||||||||||||||||||||
| 94 | 0 | shift @params; | |||||||||||||||||||||||||||||||||
| 95 | 0 | my $port = shift @params; | |||||||||||||||||||||||||||||||||
| 96 | 0 | push @{ $torun{commands} }, ( port => $port ); | |||||||||||||||||||||||||||||||||
| 97 | 0 | $has_port = 1; | |||||||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||||||||
| 99 | ▶ | elsif ( $params[0] && $params[0] eq 'app' ) { | |||||||||||||||||||||||||||||||||
|
Condition0%0/3
$params[0] and $params[0] eq "app"
MC/DC0%0/2
$params[0] and $params[0] eq "app"
$params[0]
$params[0] eq "app"
Truth table0%0/3
A and B
A= $params[0]B= $params[0] eq "app"
| |||||||||||||||||||||||||||||||||||
| 100 | 0 | shift @params; | |||||||||||||||||||||||||||||||||
| 101 | 0 | my $app = shift @params; | |||||||||||||||||||||||||||||||||
| 102 | 0 | push @{ $torun{commands} }, ( app => $app ); | |||||||||||||||||||||||||||||||||
| 103 | 0 | $has_app = 1; | |||||||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||||||||
| 106 | elsif ( $param eq 'app' ) { | ||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||
| 108 | # App can appear on its own, or in combination with from/to above | ||||||||||||||||||||||||||||||||||
| 109 | 0 | my $app = shift @params; | |||||||||||||||||||||||||||||||||
| 110 | 0 | $torun{app} = $app; | |||||||||||||||||||||||||||||||||
| 111 | 0 | $has_app = 1; | |||||||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||||||||
| 113 | elsif ( $param eq 'direction' ) { | ||||||||||||||||||||||||||||||||||
| 114 | 0 | my $direction = shift @params; | |||||||||||||||||||||||||||||||||
| 115 | 0 | ▶ | die "Invalid direction $direction" | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$direction eq "in" or $direction eq "out"
MC/DC0%0/2
$direction eq "in" or $direction eq "out"
$direction eq "in"
$direction eq "out"
Truth table0%0/3
A or B
A= $direction eq "in"B= $direction eq "out"
| |||||||||||||||||||||||||||||||||||
| 116 | unless ( $direction eq 'in' || $direction eq 'out' ); | ||||||||||||||||||||||||||||||||||
| 117 | 0 | $torun{direction} = $direction; | |||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||
| 119 | # See if next rule is an interface | ||||||||||||||||||||||||||||||||||
| 120 | 0 | ▶ | if ( $params[0] && $params[0] eq 'on' ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$params[0] and $params[0] eq "on"
MC/DC0%0/2
$params[0] and $params[0] eq "on"
$params[0]
$params[0] eq "on"
Truth table0%0/3
A and B
A= $params[0]B= $params[0] eq "on"
| |||||||||||||||||||||||||||||||||||
| 121 | 0 | shift @params; | |||||||||||||||||||||||||||||||||
| 122 | 0 | my $interface = shift @params; | |||||||||||||||||||||||||||||||||
| 123 | 0 | $torun{on} = "on $interface"; | |||||||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||||||||
| 126 | elsif ( $param eq 'log' ) { | ||||||||||||||||||||||||||||||||||
| 127 | 0 | my $log = shift @params; | |||||||||||||||||||||||||||||||||
| 128 | 0 | ▶ | if ( $log eq 'new' ) { | ||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||
| 129 | 0 | $torun{log} = 'log'; | |||||||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||||||||
| 131 | elsif ( $log eq 'all' ) { | ||||||||||||||||||||||||||||||||||
| 132 | 0 | $torun{log} = 'log-all'; | |||||||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||||||||
| 134 | else { | ||||||||||||||||||||||||||||||||||
| 135 | 0 | die "Invalid logging option $log"; | |||||||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||||||||
| 138 | elsif ( $param eq 'delete' ) { | ||||||||||||||||||||||||||||||||||
| 139 | 0 | ▶ | $torun{delete} = 'delete' if shift @params; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||||||||
| 141 | elsif ( $param =~ m/^\d+(\/(tcp|udp))?$/ ) { | ||||||||||||||||||||||||||||||||||
| 142 | 0 | ▶ | if ( scalar @{ $torun{commands} } == 0 ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 143 | 0 | push @{ $torun{commands} }, $param; | |||||||||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||||||||
| 146 | else { | ||||||||||||||||||||||||||||||||||
| 147 | 0 | die qq(Unexpected parameter "$param" supplied to ufw rule $action); | |||||||||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||||
| 151 | 0 | ▶ | die "Do not specify port parameter with app parameter" | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$has_app and $has_port
MC/DC0%0/2
$has_app and $has_port
$has_app
$has_port
Truth table0%0/3
A and B
A= $has_appB= $has_port
| |||||||||||||||||||||||||||||||||||
| 152 | if $has_app && $has_port; | ||||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||||
| 154 | die "Do not specify protocol parameter with app parameter" | ||||||||||||||||||||||||||||||||||
| 155 | 0 | ▶ | if $has_app && $torun{proto}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$has_app and $torun{"proto"}
MC/DC0%0/2
$has_app and $torun{"proto"}
$has_app
$torun{"proto"}
Truth table0%0/3
A and B
A= $has_appB= $torun{"proto"}
| |||||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||||
| 157 | 0 | my $cmd; | |||||||||||||||||||||||||||||||||
| 158 | 0 | for my $param (qw/delete action direction on log app proto/) { | |||||||||||||||||||||||||||||||||
| 159 | 0 | ▶ | $cmd .= " $torun{$param}" if $torun{$param}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||||||||
| 161 | 0 | $cmd .= " @{$torun{commands}}"; | |||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||
| 163 | 0 | my $return = $self->_ufw_exec($cmd); | |||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||||
| 165 | 0 | ▶ | if ( $return =~ /(inserted|updated|deleted|added)/ ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 166 | 0 | return 1; | |||||||||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||||
| 169 | 0 | return 0; | |||||||||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||||||||
| 172 | 0 | ▶ | sub _ufw_disable_enable { | ||||||||||||||||||||||||||||||||
|
Subroutine
_ufw_disable_enable: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||||||||
| 173 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||
| 174 | 0 | my $action = shift; | |||||||||||||||||||||||||||||||||
| 175 | 0 | my $return = $self->_ufw_exec('status'); | |||||||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||||||||
| 177 | 0 | ▶ | my $needed = $action eq 'enable' ? 'inactive' : 'active'; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 178 | 0 | ▶ | if ( $return =~ /Status: $needed/ ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 179 | 0 | my $ret = $self->_ufw_exec("--force $action"); | |||||||||||||||||||||||||||||||||
| 180 | 0 | ▶ | my $success = | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 181 | $action eq 'enable' | ||||||||||||||||||||||||||||||||||
| 182 | ? 'Firewall is active and enabled' | ||||||||||||||||||||||||||||||||||
| 183 | : 'Firewall stopped and disabled'; | ||||||||||||||||||||||||||||||||||
| 184 | 0 | ▶ | if ( $ret =~ /$success/ ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 185 | 0 | return 1; | |||||||||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||||||||
| 187 | else { | ||||||||||||||||||||||||||||||||||
| 188 | 0 | Rex::Logger::info( "Unexpected ufw response: $ret", "warn" ); | |||||||||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||||||||
| 190 | } | ||||||||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||||||||
| 192 | 0 | return 0; | |||||||||||||||||||||||||||||||||
| 193 | } | ||||||||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||||||||
| 195 | 0 | ▶ | sub _ufw_logging { | ||||||||||||||||||||||||||||||||
|
Subroutine
_ufw_logging: not called CC=10 SCAR=47.0
| |||||||||||||||||||||||||||||||||||
| 196 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||
| 197 | 0 | my $param = shift; | |||||||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||||||||
| 199 | 0 | ▶ | $param =~ /(on|off|low|medium|high|full)/ | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 200 | or die "Invalid logging parameter: $param"; | ||||||||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||||||||
| 202 | 0 | my $current = $self->_ufw_exec('status verbose'); | |||||||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||||||||
| 204 | 0 | my $need_update; | |||||||||||||||||||||||||||||||||
| 205 | 0 | ▶ | if ( $param eq 'on' ) { | ||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||
| 206 | 0 | ▶ | $need_update = 1 unless $current =~ /^Logging: on/m; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 207 | } | ||||||||||||||||||||||||||||||||||
| 208 | elsif ( $param eq 'off' ) { | ||||||||||||||||||||||||||||||||||
| 209 | 0 | ▶ | $need_update = 1 unless $current =~ /^Logging: off$/m; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 210 | } | ||||||||||||||||||||||||||||||||||
| 211 | else { | ||||||||||||||||||||||||||||||||||
| 212 | 0 | ▶ | $need_update = 1 unless $current =~ /^Logging: on \($param\)$/m; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 213 | } | ||||||||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||||||||
| 215 | 0 | ▶ | if ($need_update) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 216 | 0 | my $ret = $self->_ufw_exec("logging $param"); | |||||||||||||||||||||||||||||||||
| 217 | 0 | ▶ | my $success = | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 218 | $param eq 'off' | ||||||||||||||||||||||||||||||||||
| 219 | ? 'Logging disabled' | ||||||||||||||||||||||||||||||||||
| 220 | : 'Logging enabled'; | ||||||||||||||||||||||||||||||||||
| 221 | 0 | ▶ | if ( $ret eq $success ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 222 | 0 | return 1; | |||||||||||||||||||||||||||||||||
| 223 | } | ||||||||||||||||||||||||||||||||||
| 224 | else { | ||||||||||||||||||||||||||||||||||
| 225 | 0 | Rex::Logger::info( "Unexpected ufw response: $ret", "warn" ); | |||||||||||||||||||||||||||||||||
| 226 | } | ||||||||||||||||||||||||||||||||||
| 227 | } | ||||||||||||||||||||||||||||||||||
| 228 | } | ||||||||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||||||||
| 230 | 0 | ▶ | sub _ufw_exec { | ||||||||||||||||||||||||||||||||
|
Subroutine
_ufw_exec: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||||
| 231 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||
| 232 | 0 | my $cmd = shift; | |||||||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||||||||
| 234 | 0 | $cmd = "ufw $cmd"; | |||||||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||||||||
| 236 | 0 | ▶ | if ( can_run("ufw") ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 237 | 0 | ▶ | my ( $output, $err ) = i_run $cmd, sub { @_ }, fail_ok => 1; | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||||||||
| 239 | 0 | ▶ | if ( $? != 0 ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 240 | 0 | Rex::Logger::info( "Error running ufw command: $cmd, received $err", | |||||||||||||||||||||||||||||||||
| 241 | "warn" ); | ||||||||||||||||||||||||||||||||||
| 242 | 0 | die("Error running ufw rule: $cmd"); | |||||||||||||||||||||||||||||||||
| 243 | } | ||||||||||||||||||||||||||||||||||
| 244 | 0 | Rex::Logger::debug("Output from ufw: $output"); | |||||||||||||||||||||||||||||||||
| 245 | 0 | return $output; | |||||||||||||||||||||||||||||||||
| 246 | } | ||||||||||||||||||||||||||||||||||
| 247 | else { | ||||||||||||||||||||||||||||||||||
| 248 | 0 | Rex::Logger::info("UFW not found."); | |||||||||||||||||||||||||||||||||
| 249 | 0 | die("UFW not found."); | |||||||||||||||||||||||||||||||||
| 250 | } | ||||||||||||||||||||||||||||||||||
| 251 | } | ||||||||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||||||||
| 253 | 0 | ▶ | sub _generate_rule_array { | ||||||||||||||||||||||||||||||||
|
Subroutine
_generate_rule_array: not called CC=17 SCAR=57.2
| |||||||||||||||||||||||||||||||||||
| 254 | 0 | my ( $self, $rule_config ) = @_; | |||||||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||||||||
| 256 | my $action = $__action_map{ $rule_config->{action} } | ||||||||||||||||||||||||||||||||||
| 257 | 0 | ▶ | or die qq(Unknown action "$rule_config->{action}" for UFW provider); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 258 | 0 | ▶ | $rule_config->{dport} ||= $rule_config->{port}; | ||||||||||||||||||||||||||||||||
|
Condition0%0/3
$rule_config->{"dport"} ||= $rule_config->{"port"}
MC/DC0%0/2
$rule_config->{"dport"} ||= $rule_config->{"port"}
$rule_config->{"dport"}
$rule_config->{"port"}
Truth table0%0/3
A ||= B
A= $rule_config->{"dport"}B= $rule_config->{"port"}
| |||||||||||||||||||||||||||||||||||
| 259 | 0 | ▶ | $rule_config->{dapp} ||= $rule_config->{app}; | ||||||||||||||||||||||||||||||||
|
Condition0%0/3
$rule_config->{"dapp"} ||= $rule_config->{"app"}
MC/DC0%0/2
$rule_config->{"dapp"} ||= $rule_config->{"app"}
$rule_config->{"dapp"}
$rule_config->{"app"}
Truth table0%0/3
A ||= B
A= $rule_config->{"dapp"}B= $rule_config->{"app"}
| |||||||||||||||||||||||||||||||||||
| 260 | 0 | ▶ | $rule_config->{source} ||= "any"; | ||||||||||||||||||||||||||||||||
|
Condition0%0/2
$rule_config->{"source"} ||= "any"
MC/DC0%0/1
$rule_config->{"source"} ||= "any"
$rule_config->{"source"}
Truth table0%0/2
A
A= $rule_config->{"source"}
| |||||||||||||||||||||||||||||||||||
| 261 | 0 | ▶ | $rule_config->{destination} ||= "any"; | ||||||||||||||||||||||||||||||||
|
Condition0%0/2
$rule_config->{"destination"} ||= "any"
MC/DC0%0/1
$rule_config->{"destination"} ||= "any"
$rule_config->{"destination"}
Truth table0%0/2
A
A= $rule_config->{"destination"}
| |||||||||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||||||||
| 263 | 0 | my @ufw_params = (); | |||||||||||||||||||||||||||||||||
| 264 | 0 | push @ufw_params, $action; | |||||||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||||||||
| 266 | push( @ufw_params, "proto", $rule_config->{proto} ) | ||||||||||||||||||||||||||||||||||
| 267 | 0 | ▶ | if ( defined $rule_config->{proto} ); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||||||||
| 269 | push( @ufw_params, "from", $rule_config->{source} ) | ||||||||||||||||||||||||||||||||||
| 270 | 0 | ▶ | if ( defined $rule_config->{source} ); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||||||||
| 272 | push( @ufw_params, "port", $rule_config->{sport} ) | ||||||||||||||||||||||||||||||||||
| 273 | 0 | ▶ | if ( defined $rule_config->{sport} ); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||||||||
| 275 | push( @ufw_params, "app", qq("$rule_config->{sapp}") ) | ||||||||||||||||||||||||||||||||||
| 276 | 0 | ▶ | if ( defined $rule_config->{sapp} ); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||||||||
| 278 | push( @ufw_params, "to", $rule_config->{destination} ) | ||||||||||||||||||||||||||||||||||
| 279 | 0 | ▶ | if ( defined $rule_config->{destination} ); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||||||||
| 281 | push( @ufw_params, "port", $rule_config->{dport} ) | ||||||||||||||||||||||||||||||||||
| 282 | 0 | ▶ | if ( defined $rule_config->{dport} ); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||||||||
| 284 | push( @ufw_params, "app", qq("$rule_config->{dapp}") ) | ||||||||||||||||||||||||||||||||||
| 285 | 0 | ▶ | if ( defined $rule_config->{dapp} ); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||||||||
| 287 | push( @ufw_params, "direction", "in" ) | ||||||||||||||||||||||||||||||||||
| 288 | 0 | ▶ | if ( defined $rule_config->{iniface} ); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||||||||
| 290 | push( @ufw_params, "on", $rule_config->{iniface} ) | ||||||||||||||||||||||||||||||||||
| 291 | 0 | ▶ | if ( defined $rule_config->{iniface} ); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||||||||
| 293 | push( @ufw_params, "log", $rule_config->{log} ) | ||||||||||||||||||||||||||||||||||
| 294 | 0 | ▶ | if ( defined $rule_config->{log} ); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||||||||
| 296 | push( @ufw_params, "delete", $rule_config->{delete} ) | ||||||||||||||||||||||||||||||||||
| 297 | 0 | ▶ | if ( defined $rule_config->{delete} ); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||||||||
| 299 | 0 | return @ufw_params; | |||||||||||||||||||||||||||||||||
| 300 | } | ||||||||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||||||||
| 302 | 1 | 1; | |||||||||||||||||||||||||||||||||