]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blob - html/cgi-bin/connections.cgi
ipsecctrl gefixt und connections.cgi gefixt
[people/pmueller/ipfire-2.x.git] / html / cgi-bin / connections.cgi
1 #!/usr/bin/perl
2 #
3 # (c) 2001 Jack Beglinger <jackb_guppy@yahoo.com>
4 #
5 # (c) 2003 Dave Roberts <countzerouk@hotmail.com> - colour coded netfilter/iptables rewrite for 1.3
6 #
7 # (c) 2006 Franck - add sorting+filtering capability
8 #
9 # (c) 2006 Peter Schälchli -inetwork (bug)
10 #
11
12 # Setup GREEN, ORANGE, IPFIRE, VPN CIDR networks, masklengths and colours only once
13
14 my @network=();
15 my @masklen=();
16 my @colour=();
17
18 use Net::IPv4Addr qw( :all );
19
20 use strict;
21
22 # enable only the following on debugging purpose
23 #use warnings;
24 use CGI::Carp 'fatalsToBrowser';
25
26 require '/var/ipfire/general-functions.pl';
27 require "${General::swroot}/lang.pl";
28 require "${General::swroot}/header.pl";
29
30 #workaround to suppress a warning when a variable is used only once
31 my @dummy = ( ${Header::table1colour} );
32 undef (@dummy);
33
34 # Read various files
35
36 my %netsettings=();
37 &General::readhash("${General::swroot}/ethernet/settings", \%netsettings);
38
39 open (ACTIVE, 'iptstate -1rbt |') or die 'Unable to open ip_conntrack';
40 my @active = <ACTIVE>;
41 close (ACTIVE);
42
43 my @vpn = ` route -n | grep ipsec | awk '{ print \$1" "\$3}'`;
44 foreach my $route (@vpn) {
45 chomp($route);
46 my @temp = split(/[\t ]+/, $route);
47 push(@network, $temp[0]);
48 push(@masklen, $temp[1]);
49 push(@colour, ${Header::colourvpn} );
50 }
51
52 my $aliasfile = "${General::swroot}/ethernet/aliases";
53 open(ALIASES, $aliasfile) or die 'Unable to open aliases file.';
54 my @aliases = <ALIASES>;
55 close(ALIASES);
56
57 # Add Green Firewall Interface
58 push(@network, $netsettings{'GREEN_ADDRESS'});
59 push(@masklen, "255.255.255.255" );
60 push(@colour, ${Header::colourfw} );
61
62 # Add Green Network to Array
63 push(@network, $netsettings{'GREEN_NETADDRESS'});
64 push(@masklen, $netsettings{'GREEN_NETMASK'} );
65 push(@colour, ${Header::colourgreen} );
66
67 # Add Green Routes to Array
68 my @routes = `/sbin/route -n | /bin/grep $netsettings{'GREEN_DEV'}`;
69 foreach my $route (@routes) {
70 chomp($route);
71 my @temp = split(/[\t ]+/, $route);
72 push(@network, $temp[0]);
73 push(@masklen, $temp[2]);
74 push(@colour, ${Header::colourgreen} );
75 }
76
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} );
81
82 # Add Orange Network
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) {
90 chomp($route);
91 my @temp = split(/[\t ]+/, $route);
92 push(@network, $temp[0]);
93 push(@masklen, $temp[2]);
94 push(@colour, ${Header::colourorange} );
95 }
96 }
97
98 # Add Blue Firewall Interface
99 push(@network, $netsettings{'BLUE_ADDRESS'});
100 push(@masklen, "255.255.255.255" );
101 push(@colour, ${Header::colourfw} );
102
103 # Add Blue Network
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) {
111 chomp($route);
112 my @temp = split(/[\t ]+/, $route);
113 push(@network, $temp[0]);
114 push(@masklen, $temp[2]);
115 push(@colour, ${Header::colourblue} );
116 }
117 }
118
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'});
124
125 # add OpenVPN net
126 push(@network, $tempovpnsubnet[0]);
127 push(@masklen, $tempovpnsubnet[1]);
128 push(@colour, ${Header::colourovpn} );
129
130
131 if ( ($ovpnsettings{'ENABLED_BLUE'} eq 'on') && $netsettings{'BLUE_DEV'} ) {
132 # add BLUE:port / proto
133 push(@network, $netsettings{'BLUE_ADDRESS'} );
134 push(@masklen, '255.255.255.255' );
135 push(@colour, ${Header::colourovpn} );
136 }
137 if ( ($ovpnsettings{'ENABLED_ORANGE'} eq 'on') && $netsettings{'ORANGE_DEV'} ) {
138 # add ORANGE:port / proto
139 push(@network, $netsettings{'ORANGE_ADDRESS'} );
140 push(@masklen, '255.255.255.255' );
141 push(@colour, ${Header::colourovpn} );
142 }
143 }
144
145 # Add STATIC RED aliases
146 if ($netsettings{'RED_DEV'}) {
147 # We have a RED eth iface
148 if ($netsettings{'RED_TYPE'} eq 'STATIC') {
149 # We have a STATIC RED eth iface
150 foreach my $line (@aliases)
151 {
152 chomp($line);
153 my @temp = split(/\,/,$line);
154 if ( $temp[0] ) {
155 push(@network, $temp[0]);
156 push(@masklen, $netsettings{'RED_NETMASK'} );
157 push(@colour, ${Header::colourfw} );
158 }
159 }
160 }
161 }
162
163 # Add VPNs
164 if ( $vpn[0] ne 'none' ) {
165 foreach my $line (@vpn) {
166 my @temp = split(/[\t ]+/,$line);
167 my @temp1 = split(/[\/:]+/,$temp[3]);
168 push(@network, $temp1[0]);
169 push(@masklen, ipv4_cidr2msk($temp1[1]));
170 push(@colour, ${Header::colourvpn} );
171 }
172 }
173 if (open(IP, "${General::swroot}/red/local-ipaddress")) {
174 my $redip = <IP>;
175 close(IP);
176 chomp $redip;
177 push(@network, $redip);
178 push(@masklen, '255.255.255.255' );
179 push(@colour, ${Header::colourfw} );
180 }
181
182
183 #Establish simple filtering&sorting boxes on top of table
184
185 our %cgiparams;
186 &Header::getcgihash(\%cgiparams);
187
188 my @list_proto = ($Lang::tr{'all'}, 'icmp', 'udp', 'tcp');
189 my @list_state = ($Lang::tr{'all'}, 'SYN_SENT', 'SYN_RECV', 'ESTABLISHED', 'FIN_WAIT',
190 'CLOSE_WAIT', 'LAST_ACK', 'TIME_WAIT', 'CLOSE', 'LISTEN');
191 my @list_mark = ($Lang::tr{'all'}, '[ASSURED]', '[UNREPLIED]');
192 my @list_sort = ('orgsip','protocol', 'expires', 'status', 'orgdip', 'orgsp',
193 'orgdp', 'exsip', 'exdip', 'exsp', 'exdp', 'marked');
194
195 # init or silently correct unknown value...
196 if ( ! grep ( /^$cgiparams{'SEE_PROTO'}$/ , @list_proto )) { $cgiparams{'SEE_PROTO'} = $list_proto[0] };
197 if ( ! grep ( /^$cgiparams{'SEE_STATE'}$/ , @list_state )) { $cgiparams{'SEE_STATE'} = $list_state[0] };
198 if ( ($cgiparams{'SEE_MARK'} ne $Lang::tr{'all'}) && # ok the grep should work but it doesn't because of
199 ($cgiparams{'SEE_MARK'} ne '[ASSURED]') && # the '[' & ']' interpreted as list separator.
200 ($cgiparams{'SEE_MARK'} ne '[UNREPLIED]') # So, explicitly enumerate items.
201 ) { $cgiparams{'SEE_MARK'} = $list_mark[0] };
202 if ( ! grep ( /^$cgiparams{'SEE_SORT'}$/ , @list_sort )) { $cgiparams{'SEE_SORT'} = $list_sort[0] };
203 # *.*.*.* or a valid IP
204 if ( $cgiparams{'SEE_SRC'} !~ /^(\*\.\*\.\*\.\*\.|\d+\.\d+\.\d+\.\d+)$/) { $cgiparams{'SEE_SRC'} = '*.*.*.*' };
205 if ( $cgiparams{'SEE_DEST'} !~ /^(\*\.\*\.\*\.\*\.|\d+\.\d+\.\d+\.\d+)$/) { $cgiparams{'SEE_DEST'} = '*.*.*.*' };
206
207
208 our %entries = (); # will hold the lines analyzed correctly
209 my $unknownlines = ''; # should be empty all the time...
210 my $index = 0; # just a counter to make unique entryies in entries
211
212 &Header::showhttpheaders();
213 &Header::openpage($Lang::tr{'connections'}, 1, '');
214 &Header::openbigbox('100%', 'left');
215 &Header::openbox('100%', 'left', $Lang::tr{'connection tracking'});
216
217 # Build listbox objects
218 my $menu_proto = &make_select ('SEE_PROTO', $cgiparams{'SEE_PROTO'}, @list_proto);
219 my $menu_state = &make_select ('SEE_STATE', $cgiparams{'SEE_STATE'}, @list_state);
220
221 print <<END
222 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
223 <table width='100%'>
224 <tr><td align='center'><b>$Lang::tr{'legend'} : </b></td>
225 <td align='center' bgcolor='${Header::colourgreen}'><b><font color='#FFFFFF'>$Lang::tr{'lan'}</font></b></td>
226 <td align='center' bgcolor='${Header::colourred}'><b><font color='#FFFFFF'>$Lang::tr{'internet'}</font></b></td>
227 <td align='center' bgcolor='${Header::colourorange}'><b><font color='#FFFFFF'>$Lang::tr{'dmz'}</font></b></td>
228 <td align='center' bgcolor='${Header::colourblue}'><b><font color='#FFFFFF'>$Lang::tr{'wireless'}</font></b></td>
229 <td align='center' bgcolor='${Header::colourfw}'><b><font color='#FFFFFF'>IPFire</font></b></td>
230 <td align='center' bgcolor='${Header::colourvpn}'><b><font color='#FFFFFF'>$Lang::tr{'vpn'}</font></b></td>
231 <td align='center' bgcolor='${Header::colourovpn}'><b><font color='#FFFFFF'>$Lang::tr{'OpenVPN'}</font></b></td>
232 </tr>
233 </table>
234 <br />
235 <table width='100%'>
236 <tr><td align='center'><font size=2>$Lang::tr{'source ip and port'}</font></td>
237 <td>&nbsp;</td>
238 <td align='center'><font size=2>$Lang::tr{'dest ip and port'}</font></td>
239 <td>&nbsp;</td>
240 <td align='center'><font size=2>$Lang::tr{'protocol'}</font></td>
241 <td align='center'><font size=2>$Lang::tr{'connection'}<br></br>$Lang::tr{'status'}</font></td>
242 <td align='center'><font size=2>$Lang::tr{'expires'}<br></br>($Lang::tr{'seconds'})</font></td>
243
244 </tr>
245 <tr><td colspan='4'>&nbsp;</td>
246 <td align='center'>$menu_proto</td>
247 <td align='center'>$menu_state</td>
248 <td>&nbsp;</td>
249 </tr>
250 <tr>
251 <td align='center' colspan='7'></td>
252 </tr>
253 <tr>
254 <td align='center' colspan='7'><input type='submit' value="$Lang::tr{'update'}" /></td>
255 </tr>
256
257 END
258 ;
259
260 my $i=0;
261 foreach my $line (@active) {
262 $i++;
263 if ($i < 3) {
264 next;
265 }
266 chomp($line);
267 my @temp = split(' ',$line);
268
269 my ($sip, $sport) = split(':', $temp[0]);
270 my ($dip, $dport) = split(':', $temp[1]);
271 my $proto = $temp[2];
272 my $state; my $ttl;
273 if ( $proto eq "esp" ){$state = "";$ttl = $temp[3];}
274 elsif ( $proto eq "icmp" ){$state = "";$ttl = $temp[4];}
275 else{$state = $temp[3];$ttl = $temp[4];}
276
277 next if( !(
278 (($cgiparams{'SEE_PROTO'} eq $Lang::tr{'all'}) || ($proto eq $cgiparams{'SEE_PROTO'} ))
279 && (($cgiparams{'SEE_STATE'} eq $Lang::tr{'all'}) || ($state eq $cgiparams{'SEE_STATE'} ))
280 && (($cgiparams{'SEE_SRC'} eq "*.*.*.*") || ($sip eq $cgiparams{'SEE_SRC'} ))
281 && (($cgiparams{'SEE_DEST'} eq "*.*.*.*") || ($dip eq $cgiparams{'SEE_DEST'} ))
282 ));
283
284 if (($proto eq 'udp') && ($ttl eq '')) {
285 $ttl = $state;
286 $state = '&nbsp;';
287 }
288
289 my $sipcol = ipcolour($sip);
290 my $dipcol = ipcolour($dip);
291
292 my $sserv = '';
293 if ($sport < 1024) {
294 $sserv = uc(getservbyport($sport, lc($proto)));
295 if ($sserv ne '') {
296 $sserv = "&nbsp;($sserv)";
297 }
298 }
299
300 my $dserv = '';
301 if ($dport < 1024) {
302 $dserv = uc(getservbyport($dport, lc($proto)));
303 if ($dserv ne '') {
304 $dserv = "&nbsp;($dserv)";
305 }
306 }
307
308 print <<END
309 <tr >
310 <td align='center' bgcolor='$sipcol'>
311 <a href='/cgi-bin/ipinfo.cgi?ip=$sip'>
312 <font color='#FFFFFF'>$sip</font>
313 </a>
314 </td>
315 <td align='center' bgcolor='$sipcol'>
316 <a href='http://isc.sans.org/port_details.php?port=$sport' target='top'>
317 <font color='#FFFFFF'>$sport$sserv</font>
318 </a>
319 </td>
320 <td align='center' bgcolor='$dipcol'>
321 <a href='/cgi-bin/ipinfo.cgi?ip=$dip'>
322 <font color='#FFFFFF'>$dip</font>
323 </a>
324 </td>
325 <td align='center' bgcolor='$dipcol'>
326 <a href='http://isc.sans.org/port_details.php?port=$dport' target='top'>
327 <font color='#FFFFFF'>$dport$dserv</font>
328 </a>
329 </td>
330 <td align='center'>$proto</td>
331 <td align='center'>$state</td>
332 <td align='center'>$ttl</td>
333 </tr>
334 END
335 ;
336 }
337
338 print "</table></form>";
339
340 &Header::closebox();
341 &Header::closebigbox();
342 &Header::closepage();
343
344 sub ipcolour($) {
345 my $id = 0;
346 my $line;
347 my $colour = ${Header::colourred};
348 my ($ip) = $_[0];
349 my $found = 0;
350 foreach $line (@network) {
351 if ($network[$id] eq '') {
352 $id++;
353 } else {
354 if (!$found && ipv4_in_network( $network[$id] , $masklen[$id], $ip) ) {
355 $found = 1;
356 $colour = $colour[$id];
357 }
358 $id++;
359 }
360 }
361 return $colour
362 }
363
364 # Create a string containing a complete SELECT html object
365 # param1: name
366 # param2: current value selected
367 # param3: field list
368 sub make_select ($,$,$) {
369 my $select_name = shift;
370 my $selected = shift;
371 my $select = "<select name='$select_name'>";
372
373 foreach my $value (@_) {
374 my $check = $selected eq $value ? "selected='selected'" : '';
375 $select .= "<option $check value='$value'>$value</option>";
376 }
377 $select .= "</select>";
378 return $select;
379 }
380
381 # Build a list of IP obtained from the %entries hash
382 # param1: IP field name
383 sub get_known_ips ($) {
384 my $field = shift;
385 my $qs = $cgiparams{'SEE_SORT'}; # switch the sort order
386 $cgiparams{'SEE_SORT'} = $field;
387
388 my @liste=('*.*.*.*');
389 foreach my $entry ( sort sort_entries keys %entries) {
390 push (@liste, $entries{$entry}->{$field}) if (! grep (/^$entries{$entry}->{$field}$/,@liste) );
391 }
392
393 $cgiparams{'SEE_SORT'} = $qs; #restore sort order
394 return @liste;
395 }
396
397 # Used to sort the table containing the lines displayed.
398 sub sort_entries { #Reverse is not implemented
399 my $qs=$cgiparams{'SEE_SORT'};
400 if ($qs =~ /orgsip|orgdip|exsip|exdip/) {
401 my @a = split(/\./,$entries{$a}->{$qs});
402 my @b = split(/\./,$entries{$b}->{$qs});
403 ($a[0]<=>$b[0]) ||
404 ($a[1]<=>$b[1]) ||
405 ($a[2]<=>$b[2]) ||
406 ($a[3]<=>$b[3]);
407 } elsif ($qs =~ /expire|orgsp|orgdp|exsp|exdp/) {
408 $entries{$a}->{$qs} <=> $entries{$b}->{$qs};
409 } else {
410 $entries{$a}->{$qs} cmp $entries{$b}->{$qs};
411 }
412 }
413
414 1;