| 1 | package Farly::ASA::Annotator; | ||||||||||
| 2 | |||||||||||
| 3 | 9 | ▶ | use 5.008008; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 9 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 9 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 9 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 9 | ▶ | use Scalar::Util qw(blessed); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 9 | ▶ | use Log::Any qw($log); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 9 | ▶ | use Farly::ASA::PortFormatter; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 9 | ▶ | use Farly::ASA::ProtocolFormatter; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 9 | ▶ | use Farly::ASA::ICMPFormatter; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | |||||||||||
| 13 | 9 | our $VERSION = '0.26'; | |||||||||
| 14 | 9 | our $AUTOLOAD; | |||||||||
| 15 | |||||||||||
| 16 | #each token type maps to a class | ||||||||||
| 17 | 9 | our $Token_Class_Map = { | |||||||||
| 18 | 'STRING' => 'Farly::Value::String', | ||||||||||
| 19 | 'DIGIT' => 'Farly::Value::Integer', | ||||||||||
| 20 | 'NAME' => 'Farly::Value::String', #method replaces name with IP | ||||||||||
| 21 | 'NAME_ID' => 'Farly::Value::String', #this is just the name string | ||||||||||
| 22 | 'IF_REF' => 'Farly::Object::Ref', | ||||||||||
| 23 | 'OBJECT_REF' => 'Farly::Object::Ref', | ||||||||||
| 24 | 'GROUP_REF' => 'Farly::Object::Ref', | ||||||||||
| 25 | 'RULE_REF' => 'Farly::Object::Ref', | ||||||||||
| 26 | 'GROUP_TYPE' => 'Farly::Value::String', | ||||||||||
| 27 | 'OBJECT_ENTRY' => 'Farly::Value::String', | ||||||||||
| 28 | 'OBJECT_TYPE' => 'Farly::Value::String', | ||||||||||
| 29 | 'ANY' => 'Farly::IPv4::Network', #method ANY = '0.0.0.0 0.0.0.0' | ||||||||||
| 30 | 'IPADDRESS' => 'Farly::IPv4::Address', | ||||||||||
| 31 | 'MASK' => 'Farly::IPv4::Address', | ||||||||||
| 32 | 'IPNETWORK' => 'Farly::IPv4::Network', | ||||||||||
| 33 | 'IPRANGE' => 'Farly::IPv4::Range', | ||||||||||
| 34 | 'NAMED_NET' => 'Farly::Value::String', #method replaces name with IP | ||||||||||
| 35 | 'PROTOCOL' => 'Farly::Transport::Protocol', | ||||||||||
| 36 | 'GROUP_PROTOCOL' => 'Farly::Value::String', #not ::Protocol because of 'tcp-udp' | ||||||||||
| 37 | 'ICMP_TYPE' => 'Farly::IPv4::ICMPType', #method maps string to int | ||||||||||
| 38 | 'PORT_ID' => 'Farly::Transport::Port', #method maps string to int | ||||||||||
| 39 | 'PORT_RANGE' => 'Farly::Transport::PortRange', #method maps string to int | ||||||||||
| 40 | 'PORT_GT' => 'Farly::Transport::PortGT', #method maps string to int | ||||||||||
| 41 | 'PORT_LT' => 'Farly::Transport::PortLT', #method maps string to int | ||||||||||
| 42 | 'ACTIONS' => 'Farly::Value::String', | ||||||||||
| 43 | 'ACL_TYPES' => 'Farly::Value::String', | ||||||||||
| 44 | 'REMARKS' => 'Farly::Value::String', | ||||||||||
| 45 | 'ACL_DIRECTION' => 'Farly::Value::String', | ||||||||||
| 46 | 'ACL_GLOBAL' => 'Farly::Value::String', | ||||||||||
| 47 | 'STATE' => 'Farly::Value::String', | ||||||||||
| 48 | 'ACL_STATUS' => 'Farly::Value::String', | ||||||||||
| 49 | 'LOG_LEVEL' => 'Farly::Value::String', | ||||||||||
| 50 | 'DEFAULT_ROUTE' => 'Farly::IPv4::Network', #method DEFAULT_ROUTE sets '0.0.0.0 0.0.0.0' | ||||||||||
| 51 | 'TUNNELED' => 'Farly::Value::String' | ||||||||||
| 52 | }; | ||||||||||
| 53 | |||||||||||
| 54 | # 'ENTRY' is like a namespace in which an ID must be unique | ||||||||||
| 55 | # A <type>_REF refers to a Farly::Object by ENTRY and ID | ||||||||||
| 56 | 9 | our $Entry_Map = { | |||||||||
| 57 | 'IF_REF' => 'INTERFACE', | ||||||||||
| 58 | 'OBJECT_REF' => 'OBJECT', | ||||||||||
| 59 | 'GROUP_REF' => 'GROUP', | ||||||||||
| 60 | 'RULE_REF' => 'RULE', | ||||||||||
| 61 | }; | ||||||||||
| 62 | |||||||||||
| 63 | 7 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 64 | 7 | my ($class) = @_; | |||||||||
| 65 | |||||||||||
| 66 | 7 | my $self = { | |||||||||
| 67 | NAMES => {}, #name to address 'symbol table' | ||||||||||
| 68 | PORT_FMT => Farly::ASA::PortFormatter->new(), | ||||||||||
| 69 | PROTOCOL_FMT => Farly::ASA::ProtocolFormatter->new(), | ||||||||||
| 70 | ICMP_FMT => Farly::ASA::ICMPFormatter->new() | ||||||||||
| 71 | }; | ||||||||||
| 72 | 7 | bless $self, $class; | |||||||||
| 73 | |||||||||||
| 74 | 7 | $log->info("$self new"); | |||||||||
| 75 | |||||||||||
| 76 | 7 | return $self; | |||||||||
| 77 | } | ||||||||||
| 78 | |||||||||||
| 79 | 120 | ▶ | sub port_formatter { | ||||||||
|
Subroutine
port_formatter: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 80 | 120 | return $_[0]->{PORT_FMT}; | |||||||||
| 81 | } | ||||||||||
| 82 | |||||||||||
| 83 | 321 | ▶ | sub protocol_formatter { | ||||||||
|
Subroutine
protocol_formatter: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 84 | 321 | return $_[0]->{PROTOCOL_FMT}; | |||||||||
| 85 | } | ||||||||||
| 86 | |||||||||||
| 87 | 40 | ▶ | sub icmp_formatter { | ||||||||
|
Subroutine
icmp_formatter: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 88 | 40 | return $_[0]->{ICMP_FMT}; | |||||||||
| 89 | } | ||||||||||
| 90 | |||||||||||
| 91 | 330 | ▶ | sub visit { | ||||||||
|
Subroutine
visit: called CC=8 SCAR=20.8
Pod
undocumented
| |||||||||||
| 92 | 330 | my ( $self, $node ) = @_; | |||||||||
| 93 | |||||||||||
| 94 | # set s of explored vertices | ||||||||||
| 95 | 330 | my %seen; | |||||||||
| 96 | |||||||||||
| 97 | #stack is all neighbors of s | ||||||||||
| 98 | my @stack; | ||||||||||
| 99 | 330 | push @stack, $node; | |||||||||
| 100 | |||||||||||
| 101 | #my $key; | ||||||||||
| 102 | |||||||||||
| 103 | 330 | ▶ | while (@stack) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 104 | |||||||||||
| 105 | 4308 | $node = pop @stack; | |||||||||
| 106 | |||||||||||
| 107 | 4308 | ▶ | next if ( $seen{$node}++ ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 108 | |||||||||||
| 109 | #visit this node if its a token | ||||||||||
| 110 | 4308 | ▶ | if ( exists( $node->{'__VALUE__'} ) ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 111 | 1663 | my $method = ref($node); | |||||||||
| 112 | 1663 | $self->$method($node); | |||||||||
| 113 | 1663 | next; | |||||||||
| 114 | } | ||||||||||
| 115 | |||||||||||
| 116 | # add name info the the names "symbol table" | ||||||||||
| 117 | 2645 | ▶ | if ( $node->isa('named_ip') ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 118 | 12 | $self->named_ip($node); | |||||||||
| 119 | } | ||||||||||
| 120 | |||||||||||
| 121 | # continue walking the parse tree | ||||||||||
| 122 | 2645 | foreach my $key ( keys %$node ) { | |||||||||
| 123 | |||||||||||
| 124 | 7976 | ▶ | next if ( $key eq 'EOL' ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 125 | |||||||||||
| 126 | 7568 | my $next = $node->{$key}; | |||||||||
| 127 | |||||||||||
| 128 | 7568 | ▶ | if ( blessed($next) ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 129 | |||||||||||
| 130 | 3978 | push @stack, $next; | |||||||||
| 131 | } | ||||||||||
| 132 | } | ||||||||||
| 133 | } | ||||||||||
| 134 | 330 | return 1; | |||||||||
| 135 | } | ||||||||||
| 136 | |||||||||||
| 137 | 12 | ▶ | sub named_ip { | ||||||||
|
Subroutine
named_ip: called CC=3 SCAR=11.3
Pod
undocumented
| |||||||||||
| 138 | 12 | my ( $self, $node ) = @_; | |||||||||
| 139 | |||||||||||
| 140 | my $name = $node->{name}->{NAME_ID}->{__VALUE__} | ||||||||||
| 141 | 12 | ▶ | or confess "$self error: name not found for ", ref($node); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 142 | |||||||||||
| 143 | my $ip = $node->{IPADDRESS}->{__VALUE__} | ||||||||||
| 144 | 12 | ▶ | or confess "$self error: IP address not found for ", ref($node); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 145 | |||||||||||
| 146 | 12 | $log->debug("name = $name : ip = $ip"); | |||||||||
| 147 | |||||||||||
| 148 | 12 | $self->{NAMES}->{$name} = $ip; | |||||||||
| 149 | } | ||||||||||
| 150 | |||||||||||
| 151 | 26 | ▶ | sub NAME { | ||||||||
|
Subroutine
NAME: called CC=3 SCAR=11.4
Pod
undocumented
| |||||||||||
| 152 | 26 | my ( $self, $node ) = @_; | |||||||||
| 153 | |||||||||||
| 154 | 26 | ▶ | my $name = $node->{'__VALUE__'} | ||||||||
|
Branch: 1 branch
| |||||||||||
| 155 | or confess "$self error: __VALUE__ not found for name"; | ||||||||||
| 156 | |||||||||||
| 157 | 26 | ▶ | my $ip = $self->{NAMES}->{$name} | ||||||||
|
Branch: 1 branch
| |||||||||||
| 158 | or confess "$self error: IP address not found for name $name"; | ||||||||||
| 159 | |||||||||||
| 160 | 26 | $node->{'__VALUE__'} = Farly::IPv4::Address->new($ip); | |||||||||
| 161 | } | ||||||||||
| 162 | |||||||||||
| 163 | 6 | ▶ | sub NAMED_NET { | ||||||||
|
Subroutine
NAMED_NET: called CC=3 SCAR=11.3
Pod
undocumented
| |||||||||||
| 164 | 6 | my ( $self, $node ) = @_; | |||||||||
| 165 | |||||||||||
| 166 | 6 | ▶ | my $named_net = $node->{'__VALUE__'} | ||||||||
|
Branch: 1 branch
| |||||||||||
| 167 | or confess "$self error: __VALUE__ not found for name"; | ||||||||||
| 168 | |||||||||||
| 169 | 6 | my ( $name, $mask ) = split( /\s+/, $named_net ); | |||||||||
| 170 | |||||||||||
| 171 | 6 | ▶ | my $ip = $self->{NAMES}->{$name} | ||||||||
|
Branch: 1 branch
| |||||||||||
| 172 | or confess "$self error: IP address not found for name $name"; | ||||||||||
| 173 | |||||||||||
| 174 | 6 | $node->{'__VALUE__'} = Farly::IPv4::Network->new("$ip $mask"); | |||||||||
| 175 | } | ||||||||||
| 176 | |||||||||||
| 177 | 72 | ▶ | sub ANY { | ||||||||
|
Subroutine
ANY: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 178 | 72 | my ( $self, $node ) = @_; | |||||||||
| 179 | 72 | $node->{'__VALUE__'} = Farly::IPv4::Network->new("0.0.0.0 0.0.0.0"); | |||||||||
| 180 | } | ||||||||||
| 181 | |||||||||||
| 182 | 5 | ▶ | sub DEFAULT_ROUTE { | ||||||||
|
Subroutine
DEFAULT_ROUTE: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 183 | 5 | my ( $self, $node ) = @_; | |||||||||
| 184 | 5 | $node->{'__VALUE__'} = Farly::IPv4::Network->new("0.0.0.0 0.0.0.0"); | |||||||||
| 185 | } | ||||||||||
| 186 | |||||||||||
| 187 | 20 | ▶ | sub ICMP_TYPE { | ||||||||
|
Subroutine
ICMP_TYPE: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||
| 188 | 20 | my ( $self, $node ) = @_; | |||||||||
| 189 | |||||||||||
| 190 | 20 | my $icmp_type = $node->{'__VALUE__'}; | |||||||||
| 191 | |||||||||||
| 192 | 20 | ▶ | $node->{'__VALUE__'} = defined( $self->icmp_formatter()->as_integer($icmp_type) ) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 193 | ? Farly::IPv4::ICMPType->new( $self->icmp_formatter()->as_integer($icmp_type) ) | ||||||||||
| 194 | : Farly::IPv4::ICMPType->new($icmp_type); | ||||||||||
| 195 | } | ||||||||||
| 196 | |||||||||||
| 197 | 168 | ▶ | sub PROTOCOL { | ||||||||
|
Subroutine
PROTOCOL: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||
| 198 | 168 | my ( $self, $node ) = @_; | |||||||||
| 199 | |||||||||||
| 200 | 168 | my $protocol = $node->{'__VALUE__'}; | |||||||||
| 201 | |||||||||||
| 202 | 168 | ▶ | $node->{'__VALUE__'} = defined( $self->protocol_formatter()->as_integer($protocol) ) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 203 | ? Farly::Transport::Protocol->new( $self->protocol_formatter()->as_integer($protocol) ) | ||||||||||
| 204 | : Farly::Transport::Protocol->new($protocol); | ||||||||||
| 205 | } | ||||||||||
| 206 | |||||||||||
| 207 | 65 | ▶ | sub PORT_ID { | ||||||||
|
Subroutine
PORT_ID: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||
| 208 | 65 | my ( $self, $node ) = @_; | |||||||||
| 209 | |||||||||||
| 210 | 65 | my $port = $node->{'__VALUE__'}; | |||||||||
| 211 | |||||||||||
| 212 | 65 | ▶ | $node->{'__VALUE__'} = defined( $self->port_formatter()->as_integer($port) ) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 213 | ? Farly::Transport::Port->new( $self->port_formatter()->as_integer($port) ) | ||||||||||
| 214 | : Farly::Transport::Port->new($port); | ||||||||||
| 215 | } | ||||||||||
| 216 | |||||||||||
| 217 | 15 | ▶ | sub PORT_RANGE { | ||||||||
|
Subroutine
PORT_RANGE: called CC=3 SCAR=12.0
Pod
undocumented
| |||||||||||
| 218 | 15 | my ( $self, $node ) = @_; | |||||||||
| 219 | |||||||||||
| 220 | 15 | my $port_range = $node->{'__VALUE__'}; | |||||||||
| 221 | |||||||||||
| 222 | 15 | my ( $low, $high ) = split( /\s+/, $port_range ); | |||||||||
| 223 | |||||||||||
| 224 | 15 | ▶ | if ( defined $self->port_formatter()->as_integer($low) ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 225 | 0 | $low = $self->port_formatter()->as_integer($low); | |||||||||
| 226 | } | ||||||||||
| 227 | |||||||||||
| 228 | 15 | ▶ | if ( defined $self->port_formatter()->as_integer($high) ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 229 | 0 | $high = $self->port_formatter()->as_integer($high); | |||||||||
| 230 | } | ||||||||||
| 231 | |||||||||||
| 232 | 15 | $node->{'__VALUE__'} = Farly::Transport::PortRange->new("$low $high"); | |||||||||
| 233 | } | ||||||||||
| 234 | |||||||||||
| 235 | 15 | ▶ | sub PORT_GT { | ||||||||
|
Subroutine
PORT_GT: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||
| 236 | 15 | my ( $self, $node ) = @_; | |||||||||
| 237 | |||||||||||
| 238 | 15 | my $port = $node->{'__VALUE__'}; | |||||||||
| 239 | |||||||||||
| 240 | 15 | ▶ | $node->{'__VALUE__'} = defined( $self->port_formatter()->as_integer($port) ) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 241 | ? Farly::Transport::PortGT->new( $self->port_formatter()->as_integer($port) ) | ||||||||||
| 242 | : Farly::Transport::PortGT->new($port); | ||||||||||
| 243 | } | ||||||||||
| 244 | |||||||||||
| 245 | 0 | ▶ | sub PORT_LT { | ||||||||
|
Subroutine
PORT_LT: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 246 | 0 | my ( $self, $node ) = @_; | |||||||||
| 247 | |||||||||||
| 248 | 0 | my $port = $node->{'__VALUE__'}; | |||||||||
| 249 | |||||||||||
| 250 | 0 | ▶ | $node->{'__VALUE__'} = defined( $self->port_formatter()->as_integer($port) ) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 251 | ? Farly::Transport::PortLT->new( $self->port_formatter()->as_integer($port) ) | ||||||||||
| 252 | : Farly::Transport::PortLT->new($port); | ||||||||||
| 253 | } | ||||||||||
| 254 | |||||||||||
| 255 | 107 | ▶ | sub _new_ObjectRef { | ||||||||
|
Subroutine
_new_ObjectRef: called CC=2 SCAR=7.0
| |||||||||||
| 256 | 107 | my ( $self, $token_class, $value ) = @_; | |||||||||
| 257 | |||||||||||
| 258 | 107 | ▶ | my $entry = $Entry_Map->{$token_class} | ||||||||
|
Branch: 1 branch
| |||||||||||
| 259 | or confess "No token type to ENTRY mapping for token $token_class\n"; | ||||||||||
| 260 | |||||||||||
| 261 | 107 | my $ce = Farly::Object::Ref->new(); | |||||||||
| 262 | |||||||||||
| 263 | 107 | $ce->set( 'ENTRY', Farly::Value::String->new($entry) ); | |||||||||
| 264 | 107 | $ce->set( 'ID', Farly::Value::String->new($value) ); | |||||||||
| 265 | |||||||||||
| 266 | 107 | return $ce; | |||||||||
| 267 | } | ||||||||||
| 268 | |||||||||||
| 269 | 1271 | ▶ | sub AUTOLOAD { | ||||||||
|
Subroutine
AUTOLOAD: called CC=6 SCAR=18.3
| |||||||||||
| 270 | 1271 | my ( $self, $node ) = @_; | |||||||||
| 271 | |||||||||||
| 272 | 1271 | ▶ | my $type = ref($self) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 273 | or confess "$self is not an object"; | ||||||||||
| 274 | |||||||||||
| 275 | 1271 | ▶ | confess "tree node for $type required" | ||||||||
|
Branch: 1 branch
| |||||||||||
| 276 | unless defined($node); | ||||||||||
| 277 | |||||||||||
| 278 | confess "value not found in node ", ref($node) | ||||||||||
| 279 | 1271 | ▶ | unless defined( $node->{'__VALUE__'} ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 280 | |||||||||||
| 281 | 1271 | my $token_class = ref($node); | |||||||||
| 282 | |||||||||||
| 283 | 1271 | ▶ | my $class = $Token_Class_Map->{$token_class} | ||||||||
|
Branch: 1 branch
| |||||||||||
| 284 | or confess "$self error: class not found for $token_class\n"; | ||||||||||
| 285 | |||||||||||
| 286 | 1271 | my $object; | |||||||||
| 287 | |||||||||||
| 288 | 1271 | my $value = $node->{'__VALUE__'}; | |||||||||
| 289 | |||||||||||
| 290 | 1271 | ▶ | if ( $class eq 'Farly::Object::Ref' ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 291 | #need to set 'ENTRY' and 'ID' properties | ||||||||||
| 292 | 107 | $object = $self->_new_ObjectRef( $token_class, $value ); | |||||||||
| 293 | } | ||||||||||
| 294 | else { | ||||||||||
| 295 | #create the object right away | ||||||||||
| 296 | 1164 | $object = $class->new($value); | |||||||||
| 297 | } | ||||||||||
| 298 | |||||||||||
| 299 | 1271 | $node->{'__VALUE__'} = $object; | |||||||||
| 300 | } | ||||||||||
| 301 | |||||||||||
| 302 | 0 | ▶ | sub DESTROY { } | ||||||||
|
Subroutine
DESTROY: not called CC=1 SCAR=0.0
| |||||||||||
| 303 | |||||||||||
| 304 | 9 | 1; | |||||||||
| 305 | __END__ | ||||||||||