line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Farly::Template::Cisco;
|
2
|
|
|
|
|
|
|
|
3
|
4
|
|
|
4
|
|
2039
|
use 5.008008;
|
|
4
|
|
|
|
|
16
|
|
|
4
|
|
|
|
|
197
|
|
4
|
4
|
|
|
4
|
|
22
|
use strict;
|
|
4
|
|
|
|
|
10
|
|
|
4
|
|
|
|
|
122
|
|
5
|
4
|
|
|
4
|
|
21
|
use warnings;
|
|
4
|
|
|
|
|
7
|
|
|
4
|
|
|
|
|
112
|
|
6
|
4
|
|
|
4
|
|
21
|
use Carp;
|
|
4
|
|
|
|
|
7
|
|
|
4
|
|
|
|
|
269
|
|
7
|
4
|
|
|
4
|
|
36
|
use File::Spec;
|
|
4
|
|
|
|
|
8
|
|
|
4
|
|
|
|
|
111
|
|
8
|
4
|
|
|
4
|
|
778451
|
use Template;
|
|
4
|
|
|
|
|
1720907
|
|
|
4
|
|
|
|
|
147
|
|
9
|
4
|
|
|
4
|
|
50
|
use Log::Any qw($log);
|
|
4
|
|
|
|
|
10
|
|
|
4
|
|
|
|
|
40
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
our $VERSION = '0.26';
|
12
|
|
|
|
|
|
|
our ( $volume, $dir, $file ) = File::Spec->splitpath( $INC{'Farly/Template/Cisco.pm'} );
|
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
sub new {
|
15
|
10
|
|
|
10
|
1
|
1903
|
my ( $class, $file, %args ) = @_;
|
16
|
|
|
|
|
|
|
|
17
|
10
|
|
|
|
|
67
|
my $self = {
|
18
|
|
|
|
|
|
|
FILE => $file,
|
19
|
|
|
|
|
|
|
TEMPLATE => undef,
|
20
|
|
|
|
|
|
|
TEXT => undef, #set this to use text port and protocol names
|
21
|
|
|
|
|
|
|
};
|
22
|
|
|
|
|
|
|
|
23
|
10
|
|
|
|
|
32
|
bless $self, $class;
|
24
|
|
|
|
|
|
|
|
25
|
10
|
|
|
|
|
56
|
$self->_init(%args);
|
26
|
|
|
|
|
|
|
|
27
|
10
|
|
|
|
|
286462
|
$log->info("$self new");
|
28
|
|
|
|
|
|
|
|
29
|
10
|
|
|
|
|
105
|
return $self;
|
30
|
|
|
|
|
|
|
}
|
31
|
|
|
|
|
|
|
|
32
|
59
|
|
|
59
|
|
199
|
sub _template { return $_[0]->{TEMPLATE}; }
|
33
|
59
|
|
|
59
|
|
312
|
sub _file { return $_[0]->{FILE}; }
|
34
|
435
|
|
|
435
|
|
1715
|
sub _text { $_[0]->{TEXT} }
|
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
sub _init {
|
37
|
10
|
|
|
10
|
|
209
|
my ( $self, %args ) = @_;
|
38
|
|
|
|
|
|
|
|
39
|
10
|
|
|
|
|
46
|
my $path = "$volume$dir" . "Files/";
|
40
|
|
|
|
|
|
|
|
41
|
10
|
50
|
|
|
|
217
|
$self->{TEMPLATE} = Template->new(
|
42
|
|
|
|
|
|
|
{
|
43
|
|
|
|
|
|
|
%args,
|
44
|
|
|
|
|
|
|
INCLUDE_PATH => $path,
|
45
|
|
|
|
|
|
|
TRIM => 1,
|
46
|
|
|
|
|
|
|
}
|
47
|
|
|
|
|
|
|
)
|
48
|
|
|
|
|
|
|
or die "$Template::ERROR\n";
|
49
|
|
|
|
|
|
|
}
|
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
sub use_text {
|
52
|
2
|
|
|
2
|
1
|
713
|
my ( $self, $flag ) = @_;
|
53
|
2
|
|
|
|
|
10
|
$self->{TEXT} = $flag;
|
54
|
|
|
|
|
|
|
}
|
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
# port_formatter => Farly::ASA::PortFormatter->new(),
|
57
|
|
|
|
|
|
|
# protocol_formatter => Farly::ASA::ProtocolFormatter->new(),
|
58
|
|
|
|
|
|
|
# icmp_formatter => Farly::ASA::ICMPFormatter->new(),
|
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
sub set_formatters {
|
61
|
1
|
|
|
1
|
1
|
6
|
my ( $self, $formatter ) = @_;
|
62
|
1
|
|
|
|
|
9
|
foreach my $key ( keys %$formatter ) {
|
63
|
3
|
50
|
|
|
|
26
|
confess "invalid formatters"
|
64
|
|
|
|
|
|
|
if ( $key !~ /port_formatter|protocol_formatter|icmp_formatter/ );
|
65
|
3
|
|
|
|
|
10
|
$self->{$key} = $formatter->{$key};
|
66
|
|
|
|
|
|
|
}
|
67
|
|
|
|
|
|
|
}
|
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
sub _value_format {
|
70
|
369
|
|
|
369
|
|
432
|
my ( $self, $value ) = @_;
|
71
|
|
|
|
|
|
|
|
72
|
369
|
|
|
|
|
353
|
my $string;
|
73
|
|
|
|
|
|
|
|
74
|
369
|
100
|
|
|
|
7485
|
if ( $value->isa('Farly::IPv4::Address') ) {
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
75
|
|
|
|
|
|
|
|
76
|
19
|
|
|
|
|
82
|
$string = "host " . $value->as_string();
|
77
|
|
|
|
|
|
|
}
|
78
|
|
|
|
|
|
|
elsif ( $value->isa('Farly::Transport::Protocol') ) {
|
79
|
|
|
|
|
|
|
|
80
|
27
|
100
|
|
|
|
54
|
if ( $self->_text ) {
|
81
|
16
|
100
|
66
|
|
|
84
|
$string = defined($self->{protocol_formatter}) && defined( $self->{protocol_formatter}->as_string( $value->as_string() ) )
|
82
|
|
|
|
|
|
|
? $self->{protocol_formatter}->as_string( $value->as_string() )
|
83
|
|
|
|
|
|
|
: $value->as_string();
|
84
|
|
|
|
|
|
|
}
|
85
|
|
|
|
|
|
|
else {
|
86
|
11
|
|
|
|
|
42
|
$string = $value->as_string();
|
87
|
|
|
|
|
|
|
}
|
88
|
|
|
|
|
|
|
}
|
89
|
|
|
|
|
|
|
elsif ( $value->isa('Farly::Transport::PortGT') ) {
|
90
|
6
|
|
|
|
|
10
|
$string = "gt ";
|
91
|
6
|
100
|
|
|
|
13
|
if ( $self->_text ) {
|
92
|
4
|
50
|
66
|
|
|
29
|
$string .= defined($self->{port_formatter}) && defined( $self->{port_formatter}->as_string( $value->as_string() ) )
|
93
|
|
|
|
|
|
|
? $self->{port_formatter}->as_string( $value->as_string() )
|
94
|
|
|
|
|
|
|
: $value->as_string();
|
95
|
|
|
|
|
|
|
}
|
96
|
|
|
|
|
|
|
else {
|
97
|
2
|
|
|
|
|
6
|
$string .= $value->as_string();
|
98
|
|
|
|
|
|
|
}
|
99
|
|
|
|
|
|
|
}
|
100
|
|
|
|
|
|
|
elsif ( $value->isa('Farly::Transport::PortLT') ) {
|
101
|
3
|
|
|
|
|
5
|
$string = "lt ";
|
102
|
3
|
100
|
|
|
|
7
|
if ( $self->_text ) {
|
103
|
2
|
100
|
66
|
|
|
18
|
$string .= defined($self->{port_formatter}) && defined( $self->{port_formatter}->as_string( $value->as_string() ) )
|
104
|
|
|
|
|
|
|
? $self->{port_formatter}->as_string( $value->as_string() )
|
105
|
|
|
|
|
|
|
: $value->as_string();
|
106
|
|
|
|
|
|
|
}
|
107
|
|
|
|
|
|
|
else {
|
108
|
1
|
|
|
|
|
5
|
$string .= $value->as_string();
|
109
|
|
|
|
|
|
|
}
|
110
|
|
|
|
|
|
|
}
|
111
|
|
|
|
|
|
|
elsif ( $value->isa('Farly::Transport::Port') ) {
|
112
|
27
|
|
|
|
|
39
|
$string = "eq ";
|
113
|
27
|
100
|
|
|
|
57
|
if ( $self->_text ) {
|
114
|
16
|
100
|
100
|
|
|
90
|
$string .= defined($self->{port_formatter}) && defined( $self->{port_formatter}->as_string( $value->as_string() ) )
|
115
|
|
|
|
|
|
|
? $self->{port_formatter}->as_string( $value->as_string() )
|
116
|
|
|
|
|
|
|
: $value->as_string();
|
117
|
|
|
|
|
|
|
}
|
118
|
|
|
|
|
|
|
else {
|
119
|
11
|
|
|
|
|
38
|
$string .= $value->as_string();
|
120
|
|
|
|
|
|
|
}
|
121
|
|
|
|
|
|
|
}
|
122
|
|
|
|
|
|
|
elsif ( $value->isa('Farly::Transport::PortRange') ) {
|
123
|
|
|
|
|
|
|
|
124
|
9
|
|
|
|
|
18
|
$string = "range ";
|
125
|
|
|
|
|
|
|
|
126
|
9
|
100
|
|
|
|
43
|
if ( $self->_text ) {
|
127
|
|
|
|
|
|
|
|
128
|
6
|
50
|
66
|
|
|
36
|
$string .= defined($self->{port_formatter}) && defined( $self->{port_formatter}->as_string( $value->first() ) )
|
129
|
|
|
|
|
|
|
? $self->{port_formatter}->as_string( $value->first() )
|
130
|
|
|
|
|
|
|
: $value->first();
|
131
|
|
|
|
|
|
|
|
132
|
6
|
|
|
|
|
11
|
$string .= " ";
|
133
|
|
|
|
|
|
|
|
134
|
6
|
50
|
66
|
|
|
37
|
$string .= defined($self->{port_formatter}) && defined( $self->{port_formatter}->as_string( $value->last() ) )
|
135
|
|
|
|
|
|
|
? $self->{port_formatter}->as_string( $value->last() )
|
136
|
|
|
|
|
|
|
: $value->last();
|
137
|
|
|
|
|
|
|
}
|
138
|
|
|
|
|
|
|
else {
|
139
|
3
|
|
|
|
|
12
|
$string .= $value->as_string();
|
140
|
|
|
|
|
|
|
}
|
141
|
|
|
|
|
|
|
}
|
142
|
|
|
|
|
|
|
elsif ( $value->isa('Farly::Object::Ref') ) {
|
143
|
|
|
|
|
|
|
|
144
|
28
|
|
|
|
|
94
|
$string = $value->get("ID")->as_string();
|
145
|
|
|
|
|
|
|
}
|
146
|
|
|
|
|
|
|
else {
|
147
|
|
|
|
|
|
|
|
148
|
250
|
|
|
|
|
629
|
$string = $value->as_string();
|
149
|
250
|
|
|
|
|
479
|
$string =~ s/0.0.0.0 0.0.0.0/any/g;
|
150
|
250
|
|
|
|
|
811
|
$string =~ s/^\s+|\s+$//g;
|
151
|
|
|
|
|
|
|
}
|
152
|
|
|
|
|
|
|
|
153
|
369
|
|
|
|
|
785
|
return $string;
|
154
|
|
|
|
|
|
|
}
|
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
sub _format {
|
157
|
59
|
|
|
59
|
|
81
|
my ( $self, $ce ) = @_;
|
158
|
|
|
|
|
|
|
|
159
|
59
|
|
|
|
|
299
|
my $GROUP_REF = Farly::Object::Ref->new();
|
160
|
59
|
|
|
|
|
296
|
$GROUP_REF->set( 'ENTRY', Farly::Value::String->new('GROUP') );
|
161
|
|
|
|
|
|
|
|
162
|
59
|
|
|
|
|
217
|
my $OBJECT_REF = Farly::Object::Ref->new();
|
163
|
59
|
|
|
|
|
185
|
$OBJECT_REF->set( 'ENTRY', Farly::Value::String->new('OBJECT') );
|
164
|
|
|
|
|
|
|
|
165
|
59
|
|
|
|
|
193
|
my $IF_REF = Farly::Object::Ref->new();
|
166
|
59
|
|
|
|
|
191
|
$IF_REF->set( 'ENTRY', Farly::Value::String->new('INTERFACE') );
|
167
|
|
|
|
|
|
|
|
168
|
59
|
|
|
|
|
252
|
my $NAME = Farly::Object->new();
|
169
|
59
|
|
|
|
|
185
|
$NAME->set( 'ENTRY', Farly::Value::String->new('NAME') );
|
170
|
|
|
|
|
|
|
|
171
|
59
|
|
|
|
|
170
|
my $INTERFACE = Farly::Object->new();
|
172
|
59
|
|
|
|
|
176
|
$INTERFACE->set( 'ENTRY', Farly::Value::String->new('INTERFACE') );
|
173
|
|
|
|
|
|
|
|
174
|
59
|
|
|
|
|
187
|
my $ROUTE = Farly::Object->new();
|
175
|
59
|
|
|
|
|
188
|
$ROUTE->set( 'ENTRY', Farly::Value::String->new('ROUTE') );
|
176
|
|
|
|
|
|
|
|
177
|
59
|
|
|
|
|
176
|
my $RULE = Farly::Value::String->new('RULE');
|
178
|
|
|
|
|
|
|
|
179
|
59
|
|
|
|
|
312
|
my $ALL = Farly::Transport::PortRange->new('1 65535');
|
180
|
59
|
|
|
|
|
286
|
my $ANY_ICMP_TYPE = Farly::IPv4::ICMPType->new(-1);
|
181
|
|
|
|
|
|
|
|
182
|
59
|
|
|
|
|
92
|
my $hash;
|
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
#names should not be prefixed with 'host'
|
185
|
59
|
100
|
|
|
|
212
|
if ( $ce->matches($NAME) ) {
|
186
|
3
|
|
|
|
|
12
|
foreach my $key ( $ce->get_keys() ) {
|
187
|
12
|
|
|
|
|
42
|
$hash->{$key} = $ce->get($key)->as_string();
|
188
|
|
|
|
|
|
|
}
|
189
|
3
|
|
|
|
|
69
|
return $hash;
|
190
|
|
|
|
|
|
|
}
|
191
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
#interface ip addresses should not be prefixed with 'host'
|
193
|
56
|
100
|
|
|
|
197
|
if ( $ce->matches($INTERFACE) ) {
|
194
|
3
|
|
|
|
|
13
|
foreach my $key ( $ce->get_keys() ) {
|
195
|
21
|
|
|
|
|
51
|
$hash->{$key} = $ce->get($key)->as_string();
|
196
|
|
|
|
|
|
|
}
|
197
|
3
|
|
|
|
|
24
|
return $hash;
|
198
|
|
|
|
|
|
|
}
|
199
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
# for routes, neither interface names nor ip addresses should be prefixed
|
201
|
53
|
100
|
|
|
|
175
|
if ( $ce->matches($ROUTE) ) {
|
202
|
6
|
|
|
|
|
22
|
foreach my $key ( $ce->get_keys() ) {
|
203
|
36
|
100
|
|
|
|
82
|
if ( $key eq 'INTERFACE' ) {
|
204
|
|
|
|
|
|
|
|
205
|
|
|
|
|
|
|
# use _value_format because $ce isa 'Farly::Object::Ref'
|
206
|
6
|
|
|
|
|
20
|
$hash->{$key} = $self->_value_format( $ce->get($key) );
|
207
|
6
|
|
|
|
|
13
|
next;
|
208
|
|
|
|
|
|
|
}
|
209
|
30
|
|
|
|
|
77
|
$hash->{$key} = $ce->get($key)->as_string();
|
210
|
|
|
|
|
|
|
}
|
211
|
6
|
|
|
|
|
55
|
return $hash;
|
212
|
|
|
|
|
|
|
}
|
213
|
|
|
|
|
|
|
|
214
|
47
|
|
|
|
|
182
|
foreach my $key ( $ce->get_keys() ) {
|
215
|
|
|
|
|
|
|
|
216
|
365
|
|
|
|
|
930
|
my $value = $ce->get($key);
|
217
|
|
|
|
|
|
|
|
218
|
365
|
|
|
|
|
451
|
my $prefix;
|
219
|
|
|
|
|
|
|
my $string;
|
220
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
# skip port range 1 - 65535
|
222
|
365
|
100
|
|
|
|
1002
|
if ( $value->equals($ALL) ) {
|
223
|
2
|
|
|
|
|
15
|
next;
|
224
|
|
|
|
|
|
|
}
|
225
|
|
|
|
|
|
|
|
226
|
|
|
|
|
|
|
# skip default ICMP type '-1'
|
227
|
363
|
50
|
|
|
|
964
|
if ( $value->equals($ANY_ICMP_TYPE) ) {
|
228
|
0
|
|
|
|
|
0
|
next;
|
229
|
|
|
|
|
|
|
}
|
230
|
|
|
|
|
|
|
|
231
|
363
|
100
|
|
|
|
1846
|
if ( $value->isa('Farly::Object::Ref') ) {
|
232
|
|
|
|
|
|
|
|
233
|
22
|
100
|
|
|
|
77
|
if ( $value->matches($GROUP_REF) ) {
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
234
|
12
|
100
|
|
|
|
40
|
if ( $ce->get('ENTRY')->equals($RULE) ) {
|
235
|
9
|
|
|
|
|
18
|
$prefix = 'object-group';
|
236
|
|
|
|
|
|
|
}
|
237
|
|
|
|
|
|
|
}
|
238
|
|
|
|
|
|
|
elsif ( $value->matches($OBJECT_REF) ) {
|
239
|
4
|
|
|
|
|
7
|
$prefix = 'object';
|
240
|
|
|
|
|
|
|
}
|
241
|
|
|
|
|
|
|
elsif ( $value->matches($IF_REF) ) {
|
242
|
3
|
|
|
|
|
6
|
$prefix = 'interface';
|
243
|
|
|
|
|
|
|
}
|
244
|
|
|
|
|
|
|
}
|
245
|
|
|
|
|
|
|
|
246
|
363
|
100
|
|
|
|
971
|
$string = defined($prefix)
|
247
|
|
|
|
|
|
|
? $prefix . ' ' . $self->_value_format($value)
|
248
|
|
|
|
|
|
|
: $self->_value_format($value);
|
249
|
|
|
|
|
|
|
|
250
|
363
|
100
|
100
|
|
|
732
|
if ( $self->_text && $key eq 'ICMP_TYPE' ) {
|
251
|
|
|
|
|
|
|
|
252
|
2
|
100
|
66
|
|
|
18
|
$string = defined($self->{icmp_formatter}) && defined( $self->{icmp_formatter}->as_string( $value->as_string() ) )
|
253
|
|
|
|
|
|
|
? $self->{icmp_formatter}->as_string( $value->as_string() )
|
254
|
|
|
|
|
|
|
: $value->as_string();
|
255
|
|
|
|
|
|
|
}
|
256
|
|
|
|
|
|
|
|
257
|
363
|
|
|
|
|
967
|
$hash->{$key} = $string;
|
258
|
|
|
|
|
|
|
}
|
259
|
|
|
|
|
|
|
|
260
|
47
|
|
|
|
|
444
|
return $hash;
|
261
|
|
|
|
|
|
|
}
|
262
|
|
|
|
|
|
|
|
263
|
|
|
|
|
|
|
sub as_string {
|
264
|
59
|
|
|
59
|
1
|
5703
|
my ( $self, $ce ) = @_;
|
265
|
|
|
|
|
|
|
|
266
|
59
|
|
|
|
|
160
|
my $hash = $self->_format($ce);
|
267
|
|
|
|
|
|
|
|
268
|
59
|
50
|
|
|
|
224
|
$self->_template()->process( $self->_file, $hash )
|
269
|
|
|
|
|
|
|
or die $self->_template()->error();
|
270
|
|
|
|
|
|
|
}
|
271
|
|
|
|
|
|
|
|
272
|
|
|
|
|
|
|
1;
|
273
|
|
|
|
|
|
|
__END__
|