]>
git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blob - html/cgi-bin/connections.cgi
3 # (c) 2001 Jack Beglinger <jackb_guppy@yahoo.com>
5 # (c) 2003 Dave Roberts <countzerouk@hotmail.com> - colour coded netfilter/iptables rewrite for 1.3
7 # (c) 2006 Franck - add sorting+filtering capability
9 # (c) 2006 Peter Schälchli -inetwork (bug)
12 # Setup GREEN, ORANGE, IPFIRE, VPN CIDR networks, masklengths and colours only once
18 use Net
::IPv4Addr
qw( :all );
22 # enable only the following on debugging purpose
24 use CGI
::Carp
'fatalsToBrowser';
26 require '/var/ipfire/general-functions.pl';
27 require "${General::swroot}/lang.pl";
28 require "${General::swroot}/header.pl";
30 #workaround to suppress a warning when a variable is used only once
31 my @dummy = ( ${Header
::table1colour
} );
37 &General
::readhash
("${General::swroot}/ethernet/settings", \
%netsettings);
39 open (ACTIVE
, 'iptstate -1rbt |') or die 'Unable to open ip_conntrack';
40 my @active = <ACTIVE
>;
43 my @vpn = ` route -n | grep ipsec | awk '{ print \$1" "\$3}'`;
44 foreach my $route (@vpn) {
46 my @temp = split(/[\t ]+/, $route);
47 push(@network, $temp[0]);
48 push(@masklen, $temp[1]);
49 push(@colour, ${Header
::colourvpn
} );
52 my $aliasfile = "${General::swroot}/ethernet/aliases";
53 open(ALIASES
, $aliasfile) or die 'Unable to open aliases file.';
54 my @aliases = <ALIASES
>;
57 # Add Green Firewall Interface
58 push(@network, $netsettings{'GREEN_ADDRESS'});
59 push(@masklen, "255.255.255.255" );
60 push(@colour, ${Header
::colourfw
} );
62 # Add Green Network to Array
63 push(@network, $netsettings{'GREEN_NETADDRESS'});
64 push(@masklen, $netsettings{'GREEN_NETMASK'} );
65 push(@colour, ${Header
::colourgreen
} );
67 # Add Green Routes to Array
68 my @routes = `/sbin/route -n | /bin/grep $netsettings{'GREEN_DEV'}`;
69 foreach my $route (@routes) {
71 my @temp = split(/[\t ]+/, $route);
72 push(@network, $temp[0]);
73 push(@masklen, $temp[2]);
74 push(@colour, ${Header
::colourgreen
} );
77 # Add Firewall Localhost 127.0.0.1
78 push(@network, '127.0.0.1');
79 push(@masklen, '255.255.255.255' );
80 push(@colour, ${Header
::colourfw
} );
83 if ($netsettings{'ORANGE_DEV'}) {
84 push(@network, $netsettings{'ORANGE_NETADDRESS'});
85 push(@masklen, $netsettings{'ORANGE_NETMASK'} );
86 push(@colour, ${Header
::colourorange
} );
87 # Add Orange Routes to Array
88 @routes = `/sbin/route -n | /bin/grep $netsettings{'ORANGE_DEV'}`;
89 foreach my $route (@routes) {
91 my @temp = split(/[\t ]+/, $route);
92 push(@network, $temp[0]);
93 push(@masklen, $temp[2]);
94 push(@colour, ${Header
::colourorange
} );
98 # Add Blue Firewall Interface
99 push(@network, $netsettings{'BLUE_ADDRESS'});
100 push(@masklen, "255.255.255.255" );
101 push(@colour, ${Header
::colourfw
} );
104 if ($netsettings{'BLUE_DEV'}) {
105 push(@network, $netsettings{'BLUE_NETADDRESS'});
106 push(@masklen, $netsettings{'BLUE_NETMASK'} );
107 push(@colour, ${Header
::colourblue
} );
108 # Add Blue Routes to Array
109 @routes = `/sbin/route -n | /bin/grep $netsettings{'BLUE_DEV'}`;
110 foreach my $route (@routes) {
112 my @temp = split(/[\t ]+/, $route);
113 push(@network, $temp[0]);
114 push(@masklen, $temp[2]);
115 push(@colour, ${Header
::colourblue
} );
119 # Add OpenVPN net and RED/BLUE/ORANGE entry (when appropriate)
120 if (-e
"${General::swroot}/ovpn/settings") {
121 my %ovpnsettings = ();
122 &General
::readhash
("${General::swroot}/ovpn/settings", \
%ovpnsettings);
123 my @tempovpnsubnet = split("\/",$ovpnsettings{'DOVPN_SUBNET'});
126 push(@network, $tempovpnsubnet[0]);
127 push(@masklen, $tempovpnsubnet[1]);
128 push(@colour, ${Header
::colourovpn
} );
130 if ( ($ovpnsettings{'ENABLED'} eq 'on') && open(IP
, "${General::swroot}/red/local-ipaddress") ) {
131 # add RED:port / proto
135 push(@network, $redip );
136 push(@masklen, '255.255.255.255' );
137 push(@colour, ${Header
::colourovpn
} );
139 if ( ($ovpnsettings{'ENABLED_BLUE'} eq 'on') && $netsettings{'BLUE_DEV'} ) {
140 # add BLUE:port / proto
141 push(@network, $netsettings{'BLUE_ADDRESS'} );
142 push(@masklen, '255.255.255.255' );
143 push(@colour, ${Header
::colourovpn
} );
145 if ( ($ovpnsettings{'ENABLED_ORANGE'} eq 'on') && $netsettings{'ORANGE_DEV'} ) {
146 # add ORANGE:port / proto
147 push(@network, $netsettings{'ORANGE_ADDRESS'} );
148 push(@masklen, '255.255.255.255' );
149 push(@colour, ${Header
::colourovpn
} );
153 # Add STATIC RED aliases
154 if ($netsettings{'RED_DEV'}) {
155 # We have a RED eth iface
156 if ($netsettings{'RED_TYPE'} eq 'STATIC') {
157 # We have a STATIC RED eth iface
158 foreach my $line (@aliases)
161 my @temp = split(/\,/,$line);
163 push(@network, $temp[0]);
164 push(@masklen, $netsettings{'RED_NETMASK'} );
165 push(@colour, ${Header
::colourfw
} );
172 if ( $vpn[0] ne 'none' ) {
173 foreach my $line (@vpn) {
174 my @temp = split(/[\t ]+/,$line);
175 my @temp1 = split(/[\/:]+/,$temp[3]);
176 push(@network, $temp1[0]);
177 push(@masklen, ipv4_cidr2msk
($temp1[1]));
178 push(@colour, ${Header
::colourvpn
} );
181 if (open(IP
, "${General::swroot}/red/local-ipaddress")) {
185 push(@network, $redip);
186 push(@masklen, '255.255.255.255' );
187 push(@colour, ${Header
::colourfw
} );
191 #Establish simple filtering&sorting boxes on top of table
194 &Header
::getcgihash
(\
%cgiparams);
196 my @list_proto = ($Lang::tr
{'all'}, 'icmp', 'udp', 'tcp');
197 my @list_state = ($Lang::tr
{'all'}, 'SYN_SENT', 'SYN_RECV', 'ESTABLISHED', 'FIN_WAIT',
198 'CLOSE_WAIT', 'LAST_ACK', 'TIME_WAIT', 'CLOSE', 'LISTEN');
199 my @list_mark = ($Lang::tr
{'all'}, '[ASSURED]', '[UNREPLIED]');
200 my @list_sort = ('orgsip','protocol', 'expires', 'status', 'orgdip', 'orgsp',
201 'orgdp', 'exsip', 'exdip', 'exsp', 'exdp', 'marked');
203 # init or silently correct unknown value...
204 if ( ! grep ( /^$cgiparams{'SEE_PROTO'}$/ , @list_proto )) { $cgiparams{'SEE_PROTO'} = $list_proto[0] };
205 if ( ! grep ( /^$cgiparams{'SEE_STATE'}$/ , @list_state )) { $cgiparams{'SEE_STATE'} = $list_state[0] };
206 if ( ($cgiparams{'SEE_MARK'} ne $Lang::tr
{'all'}) && # ok the grep should work but it doesn't because of
207 ($cgiparams{'SEE_MARK'} ne '[ASSURED]') && # the '[' & ']' interpreted as list separator.
208 ($cgiparams{'SEE_MARK'} ne '[UNREPLIED]') # So, explicitly enumerate items.
209 ) { $cgiparams{'SEE_MARK'} = $list_mark[0] };
210 if ( ! grep ( /^$cgiparams{'SEE_SORT'}$/ , @list_sort )) { $cgiparams{'SEE_SORT'} = $list_sort[0] };
211 # *.*.*.* or a valid IP
212 if ( $cgiparams{'SEE_SRC'} !~ /^(\*\.\*\.\*\.\*\.|\d+\.\d+\.\d+\.\d+)$/) { $cgiparams{'SEE_SRC'} = '*.*.*.*' };
213 if ( $cgiparams{'SEE_DEST'} !~ /^(\*\.\*\.\*\.\*\.|\d+\.\d+\.\d+\.\d+)$/) { $cgiparams{'SEE_DEST'} = '*.*.*.*' };
216 our %entries = (); # will hold the lines analyzed correctly
217 my $unknownlines = ''; # should be empty all the time...
218 my $index = 0; # just a counter to make unique entryies in entries
220 &Header
::showhttpheaders
();
221 &Header
::openpage
($Lang::tr
{'connections'}, 1, '');
222 &Header
::openbigbox
('100%', 'left');
223 &Header
::openbox
('100%', 'left', $Lang::tr
{'connection tracking'});
225 # Build listbox objects
226 my $menu_proto = &make_select
('SEE_PROTO', $cgiparams{'SEE_PROTO'}, @list_proto);
227 my $menu_state = &make_select
('SEE_STATE', $cgiparams{'SEE_STATE'}, @list_state);
230 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
232 <tr><td align='center'><b>$Lang::tr{'legend'} : </b></td>
233 <td align='center' bgcolor='${Header::colourgreen}'><b><font color='#FFFFFF'>$Lang::tr{'lan'}</font></b></td>
234 <td align='center' bgcolor='${Header::colourred}'><b><font color='#FFFFFF'>$Lang::tr{'internet'}</font></b></td>
235 <td align='center' bgcolor='${Header::colourorange}'><b><font color='#FFFFFF'>$Lang::tr{'dmz'}</font></b></td>
236 <td align='center' bgcolor='${Header::colourblue}'><b><font color='#FFFFFF'>$Lang::tr{'wireless'}</font></b></td>
237 <td align='center' bgcolor='${Header::colourfw}'><b><font color='#FFFFFF'>IPFire</font></b></td>
238 <td align='center' bgcolor='${Header::colourvpn}'><b><font color='#FFFFFF'>$Lang::tr{'vpn'}</font></b></td>
239 <td align='center' bgcolor='${Header::colourovpn}'><b><font color='#FFFFFF'>$Lang::tr{'OpenVPN'}</font></b></td>
244 <tr><td align='center'><font size=2>$Lang::tr{'source ip and port'}</font></td>
246 <td align='center'><font size=2>$Lang::tr{'dest ip and port'}</font></td>
248 <td align='center'><font size=2>$Lang::tr{'protocol'}</font></td>
249 <td align='center'><font size=2>$Lang::tr{'connection'}<br></br>$Lang::tr{'status'}</font></td>
250 <td align='center'><font size=2>$Lang::tr{'expires'}<br></br>($Lang::tr{'seconds'})</font></td>
253 <tr><td colspan='4'> </td>
254 <td align='center'>$menu_proto</td>
255 <td align='center'>$menu_state</td>
259 <td align='center' colspan='7'></td>
262 <td align='center' colspan='7'><input type='submit' value="$Lang::tr{'update'}" /></td>
269 foreach my $line (@active) {
275 my @temp = split(' ',$line);
277 my ($sip, $sport) = split(':', $temp[0]);
278 my ($dip, $dport) = split(':', $temp[1]);
279 my $proto = $temp[2];
280 my $state = $temp[3];
284 (($cgiparams{'SEE_PROTO'} eq $Lang::tr
{'all'}) || ($proto eq $cgiparams{'SEE_PROTO'} ))
285 && (($cgiparams{'SEE_STATE'} eq $Lang::tr
{'all'}) || ($state eq $cgiparams{'SEE_STATE'} ))
286 && (($cgiparams{'SEE_SRC'} eq "*.*.*.*") || ($sip eq $cgiparams{'SEE_SRC'} ))
287 && (($cgiparams{'SEE_DEST'} eq "*.*.*.*") || ($dip eq $cgiparams{'SEE_DEST'} ))
290 if (($proto eq 'udp') && ($ttl eq '')) {
295 my $sipcol = ipcolour
($sip);
296 my $dipcol = ipcolour
($dip);
300 $sserv = uc(getservbyport($sport, lc($proto)));
302 $sserv = " ($sserv)";
308 $dserv = uc(getservbyport($dport, lc($proto)));
310 $dserv = " ($dserv)";
316 <td align='center' bgcolor='$sipcol'>
317 <a href='/cgi-bin/ipinfo.cgi?ip=$sip'>
318 <font color='#FFFFFF'>$sip</font>
321 <td align='center' bgcolor='$sipcol'>
322 <a href='http://isc.sans.org/port_details.php?port=$sport' target='top'>
323 <font color='#FFFFFF'>$sport$sserv</font>
326 <td align='center' bgcolor='$dipcol'>
327 <a href='/cgi-bin/ipinfo.cgi?ip=$dip'>
328 <font color='#FFFFFF'>$dip</font>
331 <td align='center' bgcolor='$dipcol'>
332 <a href='http://isc.sans.org/port_details.php?port=$dport' target='top'>
333 <font color='#FFFFFF'>$dport$dserv</font>
336 <td align='center'>$proto</td>
337 <td align='center'>$state</td>
338 <td align='center'>$ttl</td>
344 print "</table></form>";
347 &Header
::closebigbox
();
348 &Header
::closepage
();
353 my $colour = ${Header
::colourred
};
356 foreach $line (@network) {
357 if ($network[$id] eq '') {
360 if (!$found && ipv4_in_network
( $network[$id] , $masklen[$id], $ip) ) {
362 $colour = $colour[$id];
370 # Create a string containing a complete SELECT html object
372 # param2: current value selected
374 sub make_select
($,$,$) {
375 my $select_name = shift;
376 my $selected = shift;
377 my $select = "<select name='$select_name'>";
379 foreach my $value (@_) {
380 my $check = $selected eq $value ?
"selected='selected'" : '';
381 $select .= "<option $check value='$value'>$value</option>";
383 $select .= "</select>";
387 # Build a list of IP obtained from the %entries hash
388 # param1: IP field name
389 sub get_known_ips
($) {
391 my $qs = $cgiparams{'SEE_SORT'}; # switch the sort order
392 $cgiparams{'SEE_SORT'} = $field;
394 my @liste=('*.*.*.*');
395 foreach my $entry ( sort sort_entries
keys %entries) {
396 push (@liste, $entries{$entry}->{$field}) if (! grep (/^$entries{$entry}->{$field}$/,@liste) );
399 $cgiparams{'SEE_SORT'} = $qs; #restore sort order
403 # Used to sort the table containing the lines displayed.
404 sub sort_entries
{ #Reverse is not implemented
405 my $qs=$cgiparams{'SEE_SORT'};
406 if ($qs =~ /orgsip|orgdip|exsip|exdip/) {
407 my @a = split(/\./,$entries{$a}->{$qs});
408 my @b = split(/\./,$entries{$b}->{$qs});
413 } elsif ($qs =~ /expire|orgsp|orgdp|exsp|exdp/) {
414 $entries{$a}->{$qs} <=> $entries{$b}->{$qs};
416 $entries{$a}->{$qs} cmp $entries{$b}->{$qs};