Object/icmp_type.pm untested

filter: Statementstmt 0.0% 0 / 78 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 133 CC 13 SCAR 27.6
CC
13
Cov
0%
matchport
42.0
_init
20.0
add
6.0
CRAP 15.8
1 package PIX::Object::icmp_type;
2  
3 use strict;
4 use warnings;
5 use base qw( PIX::Object );
6 use Carp;
7  
8 our $VERSION = '1.10';
9  
10 our $ICMP_TYPES = {
11 # Cisco PIX/ASA firewalls define the following aliases for various ICMP
12 # type codes. Values defined from:
13 # http://www.iana.org/assignments/icmp-parameters
14 'alternate-address' => 6,
15 'conversion-error' => 31,
16 'echo' => 8,
17 'echo-reply' => 0,
18 'information-reply' => 16,
19 'information-request' => 15,
20 'mask-reply' => 18,
21 'mask-request' => 17,
22 'mobile-redirect' => 32,
23 'parameter-problem' => 12,
24 'redirect' => 5,
25 'router-advertisement' => 9,
26 'router-solicitation' => 10,
27 'source-quench' => 4,
28 'time-exceeded' => 11,
29 'timestamp-reply' => 14,
30 'timestamp-request' => 13,
31 'traceroute' => 30,
32 'unreachable' => 3
33 };
34  
35 =pod
36  
37 =head1 NAME
38  
39 PIX::Object::icmp_type - Class for "icmp-type" object-groups found in a PIX
40 config.
41  
42 =head1 SYNOPSIS
43  
44 This is a subclass of PIX::Object that maintains a single object-group as read
45 from a firewall config. You can list and search for elements in the list.
46  
47 =head1 SEE ALSO
48  
49 B<PIX::Object>
50  
51 =head1 METHODS
52  
53 =over
54  
55 =cut
56  
57 sub _init {
58 my $self = shift;
59 $self->SUPER::_init;
60  
61 $self->{debug} = 0;
62  
63 $self->{icmptypes} = []; # icmp-types directly defined in this object group
64 $self->{groups} = []; # other groups that this object points to for icmptypes
65 $self->{desc} = '';
66  
67 $self->_nextline; # remove the first line 'object-group blah'
68 while (defined(my $line = $self->_nextline)) {
69 if ($line =~ /\s*icmp-object (\S*)/) {
70 $self->add($1);
71 } elsif ($line =~ /^\s*group-object (\S+)/) {
72 $self->add($1, 1);
73 } elsif ($line =~ /^\s+description (.+)/) {
74 $self->{desc} = $1;
75 } else {
76 carp "$self->{name}: Unknown protocol object line: $line\n";
77 }
78 }
79 }
80  
81 =item B<add($icmp_or_grp, [$is_icmp])>
82  
83 =over
84  
85 Add an icmp-type or nested group to the object-group. If $is_group is true
86 then $icmp_or_grp will be recorded as a nested hostgroup name, and not an icmp
87 type name. Note: This logic is slightly different than the other PIX::Object
88 sub-classes.
89  
90 =back
91  
92 =cut
93 sub add {
94 my ($self, $icmp_or_group, $is_group) = @_;
95 if (!$is_group) {
96 push(@{$self->{icmptypes}}, $self->icmp_alias($icmp_or_group));
97 } else {
98 push(@{$self->{groups}}, $icmp_or_group);
99 }
100 }
101  
102 =item B<enumerate( )>
103  
104 =over
105  
106 Returns a list of icmp types that the object-group encompasses.
107  
108 =back
109  
110 =cut
111 sub enumerate {
112 my $self = shift;
113 my @list = @{$self->{icmptypes}};
114 foreach my $name ($self->groups) {
115 my $grp = $self->{walker}->obj($name) || next;
116 push(@list, $grp->enumerate);
117 }
118 return sort { $a <=> $b } @list;
119 }
120  
121 =item B<icmp_alias( )>
122  
123 =over
124  
125 Returns the ICMP Type code that matches the alias given, or the unmodified
126 alias if no known alias matches. See $PIX::Object::icmp_type::ICMP_TYPES for
127 a list of avaliable aliases that cisco defines.
128  
129 =back
130  
131 =cut
132 sub icmp_alias {
133 my ($self, $type) = @_;
134 return exists $ICMP_TYPES->{$type} ? $ICMP_TYPES->{$type} : $type;
135 }
136  
137  
138 =item B<list( )>
139  
140 =over
141  
142 Returns a list of icmp types from the object group. A plain list of icmp types
143 are returned.
144  
145 =back
146  
147 =cut
148 sub list {
149 my $self = shift;
150 my @list = @{$self->{icmptypes}};
151 foreach my $name ($self->groups) {
152 my $grp = $self->{walker}->obj($name) || next;
153 push(@list, $grp->list);
154 }
155 return @list;
156 }
157  
158 =item B<matchicmp( )>
159  
160 =over
161  
162 Alias for B<matchport()>.
163  
164 =back
165  
166 =cut
167 sub matchicmp {
168 my $self = shift;
169 return $self->matchport(@_);
170 }
171  
172 =item B<matchport( )>
173  
174 =over
175  
176 Searches the icmp types within our group for the TYPE given. Deligates out to
177 nested groups and maintains the state of the trace.
178  
179 Returns the name of the of the object-group that matches the TYPE (which
180 evaluates to true). This is usually called from the PIX::Accesslist::Line
181 object instead of directly.
182  
183 =back
184  
185 =cut
186 sub matchport {
187 my ($self, $portstr, $trace) = @_;
188 my $port = $self->icmp_alias($portstr);
189 my $idx = 0;
190  
191 # first search our defined icmp types
192 #print "searching icmp types in $self->{name} ...\n" if $self->{debug};
193 foreach my $p (@{$self->{icmptypes}}) {
194 $idx++;
195 if ($port eq $p) {
196 push(@$trace, $self->{name}, $idx, 0) if defined $trace;
197 return $self->{name};
198 }
199 }
200  
201 # search all nested groups, if any.
202 #print "searching groups in $self->{name} (" . (join(',', @{$self->{groups}})) . ")...\n" if $self->{debug} and scalar @{$self->{groups}};
203 foreach my $name (@{$self->{groups}}) {
204 my $grp = $self->{walker}->obj($name) || next;
205 my $localtrace = [ $self->{name}, 0, 0 ];
206 next unless $grp->type eq $self->type;
207 my $found = $grp->matchport($port, $localtrace);
208 if ($found) {
209 push(@$trace, @$localtrace) if defined $trace;
210 return $grp->{name};
211 }
212 }
213  
214 return 0;
215 }
216  
217 sub icmptypes { return wantarray ? @{$_[0]->{icmptypes}} : $_[0]->{icmptypes} }
218 sub groups { return wantarray ? @{$_[0]->{groups}} : $_[0]->{groups} }
219  
220 1;
221  
222 =pod
223  
224 =head1 AUTHOR
225  
226 Jason Morriss <lifo 101 at - gmail dot com>
227  
228 =head1 BUGS
229  
230 Please report any bugs or feature requests to
231 C<bug-pix-walker at rt.cpan.org>, or through the web interface at
232 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=PIX-Walker>.
233 I will be notified, and then you'll automatically be notified of progress on
234 your bug as I make changes.
235  
236 =head1 SUPPORT
237  
238 perldoc PIX::Walker
239  
240 perldoc PIX::Accesslist
241 perldoc PIX::Accesslist::Line
242  
243 perldoc PIX::Object
244 perldoc PIX::Object::network
245 perldoc PIX::Object::service
246 perldoc PIX::Object::protocol
247 perldoc PIX::Object::icmp_type
248  
249 =head1 COPYRIGHT & LICENSE
250  
251 Copyright 2006-2008 Jason Morriss, all rights reserved.
252  
253 This program is free software; you can redistribute it and/or modify it
254 under the same terms as Perl itself.
255  
256 =cut