Object/network.pm untested

filter: Statementstmt 0.0% 0 / 115 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 0.0% 0 / 17 Podpod 0.0% 0 / 13 totaltotal 0.0% 0 / 189 CC 16 SCAR 27.6
CC
16
Cov
0%
matchip
42.0
_init
30.0
list
12.0
CRAP 15.8
1 package PIX::Object::network;
2  
3 use strict;
4 use warnings;
5 use base qw( PIX::Object );
6 use Carp;
7  
8 our $VERSION = '1.10';
9  
10  
11 =pod
12  
13 =head1 NAME
14  
15 PIX::Object::network - Class for "network" object-groups found in a PIX config.
16  
17 =head1 SYNOPSIS
18  
19 This is a subclass of PIX::Object that maintains a single object-group as read
20 from a firewall config. You can list and search for elements in the list.
21  
22 =head1 SEE ALSO
23  
24 B<PIX::Object>
25  
26 =head1 METHODS
27  
28 =over
29  
30 =cut
31  
32 sub _init {
33 my $self = shift;
34 $self->SUPER::_init;
35  
36 $self->{debug} = 0;
37  
38 $self->{networks} = []; # networks directly defined in this object group
39 $self->{groups} = []; # other groups that this object points to for networks
40 $self->{desc} = '';
41  
42 $self->_nextline; # remove the first line 'object-group blah'
43 while (defined(my $line = $self->_nextline)) {
44 my ($ip, $mask);
45 if ($line =~ /\s*network-object (\S+) (\S+)/) {
46 ($ip, $mask) = ($1, $2);
47 if ($ip eq 'host') {
48 $ip = $mask;
49 $mask = '255.255.255.255';
50 }
51 $ip = $self->alias($ip);
52 $self->add($ip, $mask);
53 } elsif ($line =~ /^\s*group-object (\S+)/) {
54 $self->add($1);
55 } elsif ($line =~ /^\s*description (.+)/) {
56 $self->{desc} = $1;
57 } else {
58 carp "$self->{name}: Unknown network object line: $line\n";
59 }
60 }
61 }
62  
63 =item B<add($ip_or_grp, [$mask])>
64  
65 =over
66  
67 Add a network or nested group to the object-group. If $mask is undef then
68 $ip_or_grp is assumed to be a nested hostgroup name.
69  
70 =back
71  
72 =cut
73 sub add {
74 my ($self, $ip_or_grp, $mask) = @_;
75 my ($first, $last, $bits);
76 if (defined $mask) {
77 $bits = ipnumbits($mask);
78 $first = ip2int($ip_or_grp);
79 $last = ip2int(ipbroadcast($first, $bits));
80 push(@{$self->{networks}}, [ $first, $last, $bits ]);
81 } else {
82 push(@{$self->{groups}}, $ip_or_grp);
83 }
84 }
85  
86 =item B<list([$raw])>
87  
88 =over
89  
90 Returns a list of networks from the object group. Normally a plain list of
91 CIDR blocks are returned, however, if $raw is true then a list of array
92 references are returned instead. Each arrayref has: [ first_ip, last_ip, bits ].
93 The IP's are 32bit integers.
94  
95 =back
96  
97 =cut
98 sub list {
99 my $self = shift;
100 my $raw = shift || 0; # if true, the raw list is returned instead of CIDR blocks
101 my @list = ();
102  
103 for (my $i=0; $i<@{$self->{networks}}; $i++) {
104 if ($raw) {
105 # add each network as a raw list [ first, last, bits ]
106 push(@list, $self->{networks}[$i]);
107 } else {
108 # add each network as a CIDR block or host
109 push(@list, int2ip($self->{networks}[$i][0]) .
110 ($self->{networks}[$i][2] ne '32'
111 ? '/' . $self->{networks}[$i][2]
112 : ''
113 )
114 );
115 }
116 }
117  
118 foreach my $name ($self->groups) {
119 my $grp = $self->{walker}->obj($name) || next;
120 push(@list, $grp->list($raw));
121 }
122  
123 return @list;
124 }
125  
126 =item B<matchip( )>
127  
128 =over
129  
130 Searches the networks within our group for the IP given. Deligates out to nested
131 groups and maintains the state of the trace.
132  
133 Returns the name of the of the object-group that matches the IP (which evaluates
134 to true). This is usually called from the PIX::Accesslist::Line object instead
135 of directly.
136  
137 =back
138  
139 =cut
140 sub matchip {
141 my ($self, $ip, $trace) = @_;
142 my $ipint = ip2int($ip);
143 my $found = undef;
144 my $idx = 0;
145  
146 # first search our defined networks.
147 #print "searching networks in $self->{name} ...\n";
148 foreach my $net (@{$self->{networks}}) {
149 $idx++;
150 if ($ipint >= $net->[0] and $ipint <= $net->[1]) {
151 push(@$trace, $self->{name}, $idx, int2ip($net->[0]) . "/" . $net->[2]) if defined $trace;
152 return $self->{name};
153 }
154 }
155  
156 # search all nested groups, if any.
157 $idx = 0;
158 #print "searching groups in $self->{name} (" . (join(',', @{$self->{groups}})) . ")...\n" if @{$self->{groups}};
159 foreach my $name (@{$self->{groups}}) {
160 $idx++;
161 my $grp = $self->{walker}->obj($name) || next;
162 my $localtrace = [ $self->{name}, 0, 0 ];
163 next unless $grp->type eq 'network';
164 my $found = $grp->matchip($ip, $localtrace);
165 if ($found) {
166 push(@$trace, @$localtrace) if defined $trace;
167 return $grp->{name};
168 }
169 }
170
171 return undef;
172 }
173  
174 #=item B<networks( )>
175 #
176 #=over
177 #
178 #Returns the internal list of networks. In scalar context an arrayref is
179 #returned.
180 #
181 #B<Note:> use the "list()" method instead of this one.
182 #
183 #=back
184 #
185 #=cut
186 sub networks { return wantarray ? @{$_[0]->{networks}} : $_[0]->{networks} }
187  
188 #=item B<groups( )>
189 #
190 #=over
191 #
192 #Returns a raw list of nested object-groups. In scalar context an arrayref is
193 #returned.
194 #
195 #=back
196 #
197 #=cut
198 sub groups { return wantarray ? @{$_[0]->{groups}} : $_[0]->{groups} }
199  
200 #=item B<remove($ip, [$mask])>
201 #
202 #=over
203 #
204 #Remove a network from the object-group.
205 #
206 #=back
207 #
208 #=cut
209 sub remove {
210 die("remove() not implemented");
211 #my ($self, $ip, $mask) = @_;
212 #my ($first, $last, $bits);
213 #$bits = ipnumbits($mask);
214 #$first = ip2int($ip);
215 #$last = ip2int(ipbroadcast($first, $bits));
216 #push(@{$self->{networks}}, [ $first, $last, $bits ]);
217 }
218  
219  
220 # 32bit IP math routines below are helper functions to convert IP addresses
221 # these are not class methods
222  
223 sub ip2int {
224 my ($ip) = split(/[:\/]/, shift); # strip off any port or CIDR bits
225 my ($i1,$i2,$i3,$i4) = split(/\./, $ip);
226 return ($i4) | ($i3 << 8) | ($i2 << 16) | ($i1 << 24);
227 }
228  
229 sub int2ip {
230 my $num = shift;
231 return join(".",
232 ($num & 0xFF000000) >> 24,
233 ($num & 0x00FF0000) >> 16,
234 ($num & 0x0000FF00) >> 8,
235 ($num & 0x000000FF)
236 );
237 }
238  
239 sub ipnetmask {
240 my $bits = shift;
241 return '0.0.0.0' unless $bits;
242 my $num = 0xFFFFFFFF;
243 my $mask = ($num >> (32 - $bits)) << (32 - $bits);
244 return int2ip($mask);
245 }
246  
247 sub ipwildmask {
248 my $bits = shift;
249 my $num = ip2int( ipnetmask($bits) );
250 $num = $num ^ 0xFFFFFFFF;
251 return int2ip($num);
252 }
253  
254 sub ipbroadcast {
255 my ($num, $bits) = @_;
256 my @ip = split(/\./, int2ip($num));
257 my @wc = split(/\./, ipwildmask($bits));
258 my $bc = "";
259 for (my $i=0; $i < 4; $i++) { $ip[$i] += $wc[$i]; }
260 return join(".",@ip);
261 }
262  
263 sub ipnumbits {
264 my ($mask) = @_;
265 my $bits = unpack('B32', pack('N', ip2int($mask)));
266 return scalar grep { $_ eq '1' } split(//, $bits);
267 }
268  
269  
270 1;
271  
272 =pod
273  
274 =head1 AUTHOR
275  
276 Jason Morriss <lifo 101 at - gmail dot com>
277  
278 =head1 BUGS
279  
280 Please report any bugs or feature requests to
281 C<bug-pix-walker at rt.cpan.org>, or through the web interface at
282 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=PIX-Walker>.
283 I will be notified, and then you'll automatically be notified of progress on
284 your bug as I make changes.
285  
286 =head1 SUPPORT
287  
288 perldoc PIX::Walker
289  
290 perldoc PIX::Accesslist
291 perldoc PIX::Accesslist::Line
292  
293 perldoc PIX::Object
294 perldoc PIX::Object::network
295 perldoc PIX::Object::service
296 perldoc PIX::Object::protocol
297 perldoc PIX::Object::icmp_type
298  
299 =head1 COPYRIGHT & LICENSE
300  
301 Copyright 2006-2008 Jason Morriss, all rights reserved.
302  
303 This program is free software; you can redistribute it and/or modify it
304 under the same terms as Perl itself.
305  
306 =cut