| 1 | package Farly::Opts::Search; | ||||||||||||||||||||||
| 2 | |||||||||||||||||||||||
| 3 | 1 | ▶ | use 5.008008; | ||||||||||||||||||||
|
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 Carp; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 7 | 1 | ▶ | use Socket; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 8 | 1 | ▶ | use Log::Any qw($log); | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 9 | 1 | ▶ | use Farly::ASA::PortFormatter; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 10 | 1 | ▶ | use Farly::ASA::ProtocolFormatter; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 11 | |||||||||||||||||||||||
| 12 | 1 | our $VERSION = '0.26'; | |||||||||||||||||||||
| 13 | |||||||||||||||||||||||
| 14 | 6 | ▶ | sub new { | ||||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=11.1
Pod
documented
| |||||||||||||||||||||||
| 15 | 6 | my ( $class, $opts ) = @_; | |||||||||||||||||||||
| 16 | |||||||||||||||||||||||
| 17 | 6 | ▶ | defined($opts) | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 18 | or confess "options hash object required"; | ||||||||||||||||||||||
| 19 | |||||||||||||||||||||||
| 20 | 6 | ▶ | confess "invalid options type ", ref($opts) | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 21 | unless ( ref($opts) eq 'HASH' ); | ||||||||||||||||||||||
| 22 | |||||||||||||||||||||||
| 23 | 6 | my $self = { | |||||||||||||||||||||
| 24 | SEARCH => Farly::Object->new(), | ||||||||||||||||||||||
| 25 | FILTER => Farly::Object::List->new(), | ||||||||||||||||||||||
| 26 | }; | ||||||||||||||||||||||
| 27 | |||||||||||||||||||||||
| 28 | 6 | bless( $self, $class ); | |||||||||||||||||||||
| 29 | |||||||||||||||||||||||
| 30 | 6 | $log->info("$self new"); | |||||||||||||||||||||
| 31 | |||||||||||||||||||||||
| 32 | 6 | $self->_check_opts($opts); | |||||||||||||||||||||
| 33 | |||||||||||||||||||||||
| 34 | 4 | return $self; | |||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||
| 36 | |||||||||||||||||||||||
| 37 | 40 | ▶ | sub search { return $_[0]->{SEARCH} } | ||||||||||||||||||||
|
Subroutine
search: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||
| 38 | 10 | ▶ | sub filter { return $_[0]->{FILTER} } | ||||||||||||||||||||
|
Subroutine
filter: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||
| 39 | |||||||||||||||||||||||
| 40 | 6 | ▶ | sub _check_opts { | ||||||||||||||||||||
|
Subroutine
_check_opts: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 41 | 6 | my ( $self, $opts ) = @_; | |||||||||||||||||||||
| 42 | |||||||||||||||||||||||
| 43 | 6 | $self->_id($opts); | |||||||||||||||||||||
| 44 | 6 | $self->_action($opts); | |||||||||||||||||||||
| 45 | 6 | $self->_protocol($opts); | |||||||||||||||||||||
| 46 | 6 | $self->_src_ip($opts); | |||||||||||||||||||||
| 47 | 5 | $self->_dst_ip($opts); | |||||||||||||||||||||
| 48 | 5 | $self->_src_port($opts); | |||||||||||||||||||||
| 49 | 5 | $self->_dst_port($opts); | |||||||||||||||||||||
| 50 | 5 | $self->_exclude_src($opts); | |||||||||||||||||||||
| 51 | 4 | $self->_exclude_dst($opts); | |||||||||||||||||||||
| 52 | |||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||
| 54 | |||||||||||||||||||||||
| 55 | 6 | ▶ | sub _id { | ||||||||||||||||||||
|
Subroutine
_id: called CC=2 SCAR=7.1
| |||||||||||||||||||||||
| 56 | 6 | my ( $self, $opts ) = @_; | |||||||||||||||||||||
| 57 | |||||||||||||||||||||||
| 58 | 6 | ▶ | if ( defined $opts->{'id'} ) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 59 | 6 | $self->search->set( 'ID', Farly::Value::String->new( $opts->{'id'} ) ); | |||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||
| 62 | |||||||||||||||||||||||
| 63 | 6 | ▶ | sub _action { | ||||||||||||||||||||
|
Subroutine
_action: called CC=3 SCAR=11.8
| |||||||||||||||||||||||
| 64 | 6 | my ( $self, $opts ) = @_; | |||||||||||||||||||||
| 65 | |||||||||||||||||||||||
| 66 | 6 | ▶ | if ( defined $opts->{'action'} ) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 67 | 6 | my $action = $opts->{'action'}; | |||||||||||||||||||||
| 68 | 6 | ▶ | if ( $action !~ /permit|deny/ ) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 69 | 0 | die "action must be 'permit' or 'deny'"; | |||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||
| 71 | 6 | $self->search->set( 'ACTION', Farly::Value::String->new($action) ); | |||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||
| 74 | |||||||||||||||||||||||
| 75 | 6 | ▶ | sub _protocol { | ||||||||||||||||||||
|
Subroutine
_protocol: called CC=5 SCAR=17.1
| |||||||||||||||||||||||
| 76 | 6 | my ( $self, $opts ) = @_; | |||||||||||||||||||||
| 77 | |||||||||||||||||||||||
| 78 | 6 | ▶ | if ( defined $opts->{'p'} ) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 79 | 6 | my $protocol = $opts->{'p'}; | |||||||||||||||||||||
| 80 | 6 | $protocol =~ s/^\s+|\s+$//g; | |||||||||||||||||||||
| 81 | 6 | my $protocol_formatter = Farly::ASA::ProtocolFormatter->new(); | |||||||||||||||||||||
| 82 | 6 | ▶ | if ( $protocol =~ /^\d+$/ ) { | ||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||
| 83 | 0 | $self->search->set( 'PROTOCOL', Farly::Transport::Protocol->new($protocol) ); | |||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||
| 85 | elsif ( $protocol =~ /^\S+$/ ) { | ||||||||||||||||||||||
| 86 | 6 | my $protocol_number = $protocol_formatter->as_integer($protocol); | |||||||||||||||||||||
| 87 | 6 | ▶ | die "unknown protocol '$protocol'\n" | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 88 | if ( !defined $protocol_number ); | ||||||||||||||||||||||
| 89 | 6 | $self->search->set( 'PROTOCOL', Farly::Transport::Protocol->new($protocol_number) ); | |||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||
| 91 | |||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||
| 94 | |||||||||||||||||||||||
| 95 | 11 | ▶ | sub _set_ip { | ||||||||||||||||||||
|
Subroutine
_set_ip: called CC=6 SCAR=24.1
| |||||||||||||||||||||||
| 96 | 11 | my ( $self, $property, $ip ) = @_; | |||||||||||||||||||||
| 97 | |||||||||||||||||||||||
| 98 | 11 | ▶ | if ( $ip =~ /((\d{1,3})((\.)(\d{1,3})){3})\s+((\d{1,3})((\.)(\d{1,3})){3})/ ) | ||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||
| 99 | { | ||||||||||||||||||||||
| 100 | 0 | $self->search->set( $property, Farly::IPv4::Network->new($ip) ); | |||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||
| 102 | elsif ( $ip =~ /(\d{1,3}(\.\d{1,3}){3})(\/)(\d+)/ ) { | ||||||||||||||||||||||
| 103 | 8 | $self->search->set( $property, Farly::IPv4::Network->new($ip) ); | |||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||
| 105 | elsif ( $ip =~ /((\d{1,3})((\.)(\d{1,3})){3})/ ) { | ||||||||||||||||||||||
| 106 | 2 | $self->search->set( $property, Farly::IPv4::Address->new($ip) ); | |||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||
| 108 | elsif ( $ip =~ /\S+/ ) { | ||||||||||||||||||||||
| 109 | 0 | my @addresses = gethostbyname($ip); | |||||||||||||||||||||
| 110 | 0 | ▶ | if (@addresses) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 111 | 0 | @addresses = map { inet_ntoa($_) } @addresses[ 4 .. $#addresses ]; | |||||||||||||||||||||
| 112 | 0 | $self->search->set( $property, Farly::IPv4::Address->new( $addresses[0] ) ); | |||||||||||||||||||||
| 113 | 0 | print "$ip [", $addresses[0], "]\n"; | |||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||
| 115 | else { | ||||||||||||||||||||||
| 116 | 0 | die "Unable to resolve host '$ip'\n"; | |||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||
| 119 | else { | ||||||||||||||||||||||
| 120 | 1 | die "Invalid IP '$ip'\n"; | |||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||
| 123 | |||||||||||||||||||||||
| 124 | 6 | ▶ | sub _src_ip { | ||||||||||||||||||||
|
Subroutine
_src_ip: called CC=2 SCAR=7.0
| |||||||||||||||||||||||
| 125 | 6 | my ( $self, $opts ) = @_; | |||||||||||||||||||||
| 126 | |||||||||||||||||||||||
| 127 | 6 | ▶ | if ( defined $opts->{'s'} ) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 128 | 6 | my $ip = $opts->{'s'}; | |||||||||||||||||||||
| 129 | 6 | $self->_set_ip( 'SRC_IP', $ip ); | |||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||
| 132 | |||||||||||||||||||||||
| 133 | 5 | ▶ | sub _dst_ip { | ||||||||||||||||||||
|
Subroutine
_dst_ip: called CC=2 SCAR=7.0
| |||||||||||||||||||||||
| 134 | 5 | my ( $self, $opts ) = @_; | |||||||||||||||||||||
| 135 | |||||||||||||||||||||||
| 136 | 5 | ▶ | if ( defined $opts->{'d'} ) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 137 | 5 | my $ip = $opts->{'d'}; | |||||||||||||||||||||
| 138 | 5 | $self->_set_ip( 'DST_IP', $ip ); | |||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||
| 141 | |||||||||||||||||||||||
| 142 | 10 | ▶ | sub _set_port { | ||||||||||||||||||||
|
Subroutine
_set_port: called CC=4 SCAR=19.4
| |||||||||||||||||||||||
| 143 | 10 | my ( $self, $property, $port ) = @_; | |||||||||||||||||||||
| 144 | |||||||||||||||||||||||
| 145 | 10 | my $port_formatter = Farly::ASA::PortFormatter->new(); | |||||||||||||||||||||
| 146 | |||||||||||||||||||||||
| 147 | 10 | $port =~ s/^\s+|\s+$//g; | |||||||||||||||||||||
| 148 | |||||||||||||||||||||||
| 149 | 10 | ▶ | if ( $port =~ /^\d+$/ ) { | ||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||
| 150 | 10 | $self->search->set( $property, Farly::Transport::Port->new($port) ); | |||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||
| 152 | elsif ( $port =~ /^\S+$/ ) { | ||||||||||||||||||||||
| 153 | 0 | ▶ | my $port_number = $port_formatter->as_integer($port) | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 154 | or die "unknown port '$port'\n"; | ||||||||||||||||||||||
| 155 | 0 | $self->search->set( $property, Farly::Transport::Port->new($port_number) ); | |||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||
| 157 | else { | ||||||||||||||||||||||
| 158 | 0 | die "$port is not a port number\n"; | |||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||
| 161 | |||||||||||||||||||||||
| 162 | 5 | ▶ | sub _src_port { | ||||||||||||||||||||
|
Subroutine
_src_port: called CC=2 SCAR=7.0
| |||||||||||||||||||||||
| 163 | 5 | my ( $self, $opts ) = @_; | |||||||||||||||||||||
| 164 | |||||||||||||||||||||||
| 165 | 5 | ▶ | if ( defined $opts->{'sport'} ) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 166 | 5 | my $port = $opts->{'sport'}; | |||||||||||||||||||||
| 167 | 5 | $self->_set_port( 'SRC_PORT', $port ); | |||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||
| 170 | |||||||||||||||||||||||
| 171 | 5 | ▶ | sub _dst_port { | ||||||||||||||||||||
|
Subroutine
_dst_port: called CC=2 SCAR=7.0
| |||||||||||||||||||||||
| 172 | 5 | my ( $self, $opts ) = @_; | |||||||||||||||||||||
| 173 | |||||||||||||||||||||||
| 174 | 5 | ▶ | if ( defined $opts->{'dport'} ) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 175 | 5 | my $port = $opts->{'dport'}; | |||||||||||||||||||||
| 176 | 5 | $self->_set_port( 'DST_PORT', $port ); | |||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||
| 179 | |||||||||||||||||||||||
| 180 | 2 | ▶ | sub _set_exclude { | ||||||||||||||||||||
|
Subroutine
_set_exclude: called CC=4 SCAR=14.0
| |||||||||||||||||||||||
| 181 | 2 | my ( $self, $property, $file_name ) = @_; | |||||||||||||||||||||
| 182 | |||||||||||||||||||||||
| 183 | 2 | ▶ | my $file = IO::File->new($file_name) | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 184 | or croak "Please specify a valid file for exclusion\n"; | ||||||||||||||||||||||
| 185 | |||||||||||||||||||||||
| 186 | 2 | ▶ | while ( my $line = $file->getline() ) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 187 | 4 | ▶ | next if ( $line !~ /\S+/ ); | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 188 | 4 | my $exlude = Farly::Object->new(); | |||||||||||||||||||||
| 189 | 4 | $exlude->set( $property, Farly::IPv4::Network->new($line) ); | |||||||||||||||||||||
| 190 | 4 | $self->filter->add($exlude); | |||||||||||||||||||||
| 191 | } | ||||||||||||||||||||||
| 192 | |||||||||||||||||||||||
| 193 | } | ||||||||||||||||||||||
| 194 | |||||||||||||||||||||||
| 195 | 5 | ▶ | sub _exclude_src { | ||||||||||||||||||||
|
Subroutine
_exclude_src: called CC=3 SCAR=11.0
| |||||||||||||||||||||||
| 196 | 5 | my ( $self, $opts ) = @_; | |||||||||||||||||||||
| 197 | |||||||||||||||||||||||
| 198 | 5 | ▶ | if ( defined $opts->{'exclude-src'} ) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 199 | 2 | my $file_name = $opts->{'exclude-src'}; | |||||||||||||||||||||
| 200 | 2 | ▶ | croak "$file_name is not a valid file" unless ( -f $file_name ); | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 201 | 1 | $self->_set_exclude( 'SRC_IP', $file_name ); | |||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||
| 203 | } | ||||||||||||||||||||||
| 204 | |||||||||||||||||||||||
| 205 | 4 | ▶ | sub _exclude_dst { | ||||||||||||||||||||
|
Subroutine
_exclude_dst: called CC=3 SCAR=11.0
| |||||||||||||||||||||||
| 206 | 4 | my ( $self, $opts ) = @_; | |||||||||||||||||||||
| 207 | |||||||||||||||||||||||
| 208 | 4 | ▶ | if ( defined $opts->{'exclude-dst'} ) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 209 | 1 | my $file_name = $opts->{'exclude-dst'}; | |||||||||||||||||||||
| 210 | 1 | ▶ | croak "$file_name is not a valid file" unless ( -f $file_name ); | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 211 | 1 | $self->_set_exclude( 'DST_IP', $file_name ); | |||||||||||||||||||||
| 212 | } | ||||||||||||||||||||||
| 213 | } | ||||||||||||||||||||||
| 214 | |||||||||||||||||||||||
| 215 | 1 | 1; | |||||||||||||||||||||
| 216 | __END__ | ||||||||||||||||||||||