| 1 | package PIX::Object::service; | ||
| 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::service - Class for "service" 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} = 1; | ||
| 37 | |||
| 38 | $self->{services} = []; # services directly defined in this object group | ||
| 39 | $self->{groups} = []; # other groups that this object points to for services | ||
| 40 | $self->{desc} = ''; | ||
| 41 | |||
| 42 | $self->_nextline; # remove the first line 'object-group blah' | ||
| 43 | while (defined(my $line = $self->_nextline)) { | ||
| 44 | my ($type, $p1, $p2); | ||
| 45 | # print "$self->{name}: $line\n"; | ||
| 46 | if ($line =~ /\s*port-object (eq|gt|lt|range) (\S+)\s*(\S*)/) { | ||
| 47 | ($type, $p1, $p2) = ($1, $2, $3); # 'range' is handled automatically if it's present | ||
| 48 | if ($type eq 'eq') { | ||
| 49 | $p2 = $p1; | ||
| 50 | } elsif ($type eq 'lt') { | ||
| 51 | $p2 = $p1; | ||
| 52 | $p1 = 0; | ||
| 53 | } elsif ($type eq 'gt') { | ||
| 54 | $p2 = 65535; | ||
| 55 | } | ||
| 56 | $p1 = $self->{walker}->portnum($p1); | ||
| 57 | $p2 = $self->{walker}->portnum($p2); | ||
| 58 | $self->add($p1, $p2); | ||
| 59 | } elsif ($line =~ /^\s*group-object (\S+)/) { | ||
| 60 | $self->add($1); | ||
| 61 | } elsif ($line =~ /^\s*description (.+)/) { | ||
| 62 | $self->{desc} = $1; | ||
| 63 | } else { | ||
| 64 | carp "$self->{name}: Unknown service object line: $line\n"; | ||
| 65 | } | ||
| 66 | } | ||
| 67 | } | ||
| 68 | |||
| 69 | =item B<add($low_port_or_group, [$high_port])> | ||
| 70 | |||
| 71 | =over | ||
| 72 | |||
| 73 | Add a service port or nested group to the object-group. If $high_port is undef | ||
| 74 | then $low_port_or_group is assumed to be a nested hostgroup name. | ||
| 75 | |||
| 76 | =back | ||
| 77 | |||
| 78 | =cut | ||
| 79 | sub add { | ||
| 80 | my ($self, $p1, $p2) = @_; | ||
| 81 | if (defined $p2) { | ||
| 82 | push(@{$self->{services}}, [ $p1, $p2 ]); | ||
| 83 | } else { | ||
| 84 | push(@{$self->{groups}}, $p1); | ||
| 85 | } | ||
| 86 | } | ||
| 87 | |||
| 88 | =item B<list( )> | ||
| 89 | |||
| 90 | =over | ||
| 91 | |||
| 92 | Returns a list of service ports from the object group. Each element in the | ||
| 93 | list is an arrayref. Each arrayref has: [ high_port, low_port ]. | ||
| 94 | |||
| 95 | =back | ||
| 96 | |||
| 97 | =cut | ||
| 98 | sub list { | ||
| 99 | my $self = shift; | ||
| 100 | my @list = @{$self->{services}}; | ||
| 101 | foreach my $name ($self->groups) { | ||
| 102 | my $grp = $self->{walker}->obj($name) || next; | ||
| 103 | push(@list, $grp->list); | ||
| 104 | } | ||
| 105 | return @list; | ||
| 106 | } | ||
| 107 | |||
| 108 | =item B<matchport( )> | ||
| 109 | |||
| 110 | =over | ||
| 111 | |||
| 112 | Searches the services within our group for the PORT given. Deligates out to | ||
| 113 | nested groups and maintains the state of the trace. | ||
| 114 | |||
| 115 | Returns the name of the of the object-group that matches the PORT (which | ||
| 116 | evaluates to true). This is usually called from the PIX::Accesslist::Line | ||
| 117 | object instead of directly. | ||
| 118 | |||
| 119 | =back | ||
| 120 | |||
| 121 | =cut | ||
| 122 | sub matchport { | ||
| 123 | my ($self, $portstr, $trace) = @_; | ||
| 124 | my $port = $self->{walker}->portnum($portstr); | ||
| 125 | my $idx = 0; | ||
| 126 | |||
| 127 | # first search our defined services. | ||
| 128 | #print "searching services in $self->{name} ...\n" if $self->{debug}; | ||
| 129 | foreach my $p (@{$self->{services}}) { | ||
| 130 | $idx++; | ||
| 131 | if ($port >= $p->[0] and $port <= $p->[1]) { | ||
| 132 | push(@$trace, $self->{name}, $idx, 0) if defined $trace; | ||
| 133 | return $self->{name}; | ||
| 134 | } | ||
| 135 | } | ||
| 136 | |||
| 137 | # search all nested groups, if any. | ||
| 138 | #print "searching groups in $self->{name} (" . (join(',', @{$self->{groups}})) . ")...\n" if $self->{debug} and scalar @{$self->{groups}}; | ||
| 139 | foreach my $name (@{$self->{groups}}) { | ||
| 140 | my $grp = $self->{walker}->obj($name) || next; | ||
| 141 | my $localtrace = [ $self->{name}, 0, 0 ]; | ||
| 142 | next unless $grp->type eq 'service'; | ||
| 143 | my $found = $grp->matchport($port, $localtrace); | ||
| 144 | if ($found) { | ||
| 145 | push(@$trace, @$localtrace) if defined $trace; | ||
| 146 | return $grp->{name}; | ||
| 147 | } | ||
| 148 | } | ||
| 149 | |||
| 150 | return 0; | ||
| 151 | } | ||
| 152 | |||
| 153 | sub services { return wantarray ? @{$_[0]->{services}} : $_[0]->{services} } | ||
| 154 | sub groups { return wantarray ? @{$_[0]->{groups}} : $_[0]->{groups} } | ||
| 155 | |||
| 156 | =item B<enumerate([$compact=0])> | ||
| 157 | |||
| 158 | =over | ||
| 159 | |||
| 160 | Returns a list of ports that the object-group encompasses. If $compact is true | ||
| 161 | then ranges are condensed into a smaller list, ie: 1-1024,80,81,80,443 | ||
| 162 | |||
| 163 | =back | ||
| 164 | |||
| 165 | =cut | ||
| 166 | sub enumerate { | ||
| 167 | my $self = shift; | ||
| 168 | my $compact = shift; | ||
| 169 | my @list = (); | ||
| 170 | for (my $i=0; $i < @{$self->{services}}; $i++) { | ||
| 171 | my $low = $self->{services}[$i][0]; | ||
| 172 | my $high = $self->{services}[$i][1]; | ||
| 173 | if ($compact) { | ||
| 174 | push(@list, $low eq $high ? $low : "$low-$high"); | ||
| 175 | } else { | ||
| 176 | for (my $j=$low; $j <= $high; $j++) { | ||
| 177 | push(@list, $j); | ||
| 178 | } | ||
| 179 | } | ||
| 180 | } | ||
| 181 | foreach my $name ($self->groups) { | ||
| 182 | my $grp = $self->{walker}->obj($name) || next; | ||
| 183 | push(@list, $grp->enumerate($compact)); | ||
| 184 | } | ||
| 185 | @list = sort { (split(/-/, $a))[0] <=> (split(/-/, $b))[0] } @list; | ||
| 186 | return @list; | ||
| 187 | } | ||
| 188 | |||
| 189 | |||
| 190 | 1; | ||
| 191 | |||
| 192 | =pod | ||
| 193 | |||
| 194 | =head1 AUTHOR | ||
| 195 | |||
| 196 | Jason Morriss <lifo 101 at - gmail dot com> | ||
| 197 | |||
| 198 | =head1 BUGS | ||
| 199 | |||
| 200 | Please report any bugs or feature requests to | ||
| 201 | C<bug-pix-walker at rt.cpan.org>, or through the web interface at | ||
| 202 | L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=PIX-Walker>. | ||
| 203 | I will be notified, and then you'll automatically be notified of progress on | ||
| 204 | your bug as I make changes. | ||
| 205 | |||
| 206 | =head1 SUPPORT | ||
| 207 | |||
| 208 | perldoc PIX::Walker | ||
| 209 | |||
| 210 | perldoc PIX::Accesslist | ||
| 211 | perldoc PIX::Accesslist::Line | ||
| 212 | |||
| 213 | perldoc PIX::Object | ||
| 214 | perldoc PIX::Object::network | ||
| 215 | perldoc PIX::Object::service | ||
| 216 | perldoc PIX::Object::protocol | ||
| 217 | perldoc PIX::Object::icmp_type | ||
| 218 | |||
| 219 | =head1 COPYRIGHT & LICENSE | ||
| 220 | |||
| 221 | Copyright 2006-2008 Jason Morriss, all rights reserved. | ||
| 222 | |||
| 223 | This program is free software; you can redistribute it and/or modify it | ||
| 224 | under the same terms as Perl itself. | ||
| 225 | |||
| 226 | =cut |