]> git.ipfire.org Git - ipfire-2.x.git/blame - html/cgi-bin/connections.cgi
Ein Paar Dateien fuer die GPLv3 angepasst.
[ipfire-2.x.git] / html / cgi-bin / connections.cgi
CommitLineData
ac1cfefa 1#!/usr/bin/perl
70df8302
MT
2###############################################################################
3# #
4# IPFire.org - A linux based firewall #
5# Copyright (C) 2007 Michael Tremer & Christian Schmidt #
6# #
7# This program is free software: you can redistribute it and/or modify #
8# it under the terms of the GNU General Public License as published by #
9# the Free Software Foundation, either version 3 of the License, or #
10# (at your option) any later version. #
11# #
12# This program is distributed in the hope that it will be useful, #
13# but WITHOUT ANY WARRANTY; without even the implied warranty of #
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15# GNU General Public License for more details. #
16# #
17# You should have received a copy of the GNU General Public License #
18# along with this program. If not, see <http://www.gnu.org/licenses/>. #
19# #
20###############################################################################
ac1cfefa
MT
21
22my @network=();
23my @masklen=();
24my @colour=();
25
26use Net::IPv4Addr qw( :all );
27
28use strict;
29
30# enable only the following on debugging purpose
1465b127 31#use warnings;
cb5e9c6c 32#use CGI::Carp 'fatalsToBrowser';
ac1cfefa 33
986e08d9 34require '/var/ipfire/general-functions.pl';
ac1cfefa
MT
35require "${General::swroot}/lang.pl";
36require "${General::swroot}/header.pl";
37
38#workaround to suppress a warning when a variable is used only once
39my @dummy = ( ${Header::table1colour} );
40undef (@dummy);
41
42# Read various files
43
44my %netsettings=();
45&General::readhash("${General::swroot}/ethernet/settings", \%netsettings);
46
1465b127 47open (ACTIVE, 'iptstate -1rbt |') or die 'Unable to open ip_conntrack';
ac1cfefa
MT
48my @active = <ACTIVE>;
49close (ACTIVE);
50
18322edf
CS
51my @vpn = ` route -n | grep ipsec | awk '{ print \$1" "\$3}'`;
52 foreach my $route (@vpn) {
53 chomp($route);
54 my @temp = split(/[\t ]+/, $route);
55 push(@network, $temp[0]);
56 push(@masklen, $temp[1]);
57 push(@colour, ${Header::colourvpn} );
58 }
ac1cfefa
MT
59
60my $aliasfile = "${General::swroot}/ethernet/aliases";
61open(ALIASES, $aliasfile) or die 'Unable to open aliases file.';
62my @aliases = <ALIASES>;
63close(ALIASES);
64
65# Add Green Firewall Interface
66push(@network, $netsettings{'GREEN_ADDRESS'});
67push(@masklen, "255.255.255.255" );
68push(@colour, ${Header::colourfw} );
69
70# Add Green Network to Array
71push(@network, $netsettings{'GREEN_NETADDRESS'});
72push(@masklen, $netsettings{'GREEN_NETMASK'} );
73push(@colour, ${Header::colourgreen} );
74
75# Add Green Routes to Array
76my @routes = `/sbin/route -n | /bin/grep $netsettings{'GREEN_DEV'}`;
77foreach my $route (@routes) {
f9aaffa6
MT
78 chomp($route);
79 my @temp = split(/[\t ]+/, $route);
80 push(@network, $temp[0]);
81 push(@masklen, $temp[2]);
82 push(@colour, ${Header::colourgreen} );
ac1cfefa
MT
83}
84
85# Add Firewall Localhost 127.0.0.1
86push(@network, '127.0.0.1');
87push(@masklen, '255.255.255.255' );
88push(@colour, ${Header::colourfw} );
89
5433e2c9
MT
90# Add Orange Network
91if ($netsettings{'ORANGE_DEV'}) {
f9aaffa6
MT
92 push(@network, $netsettings{'ORANGE_NETADDRESS'});
93 push(@masklen, $netsettings{'ORANGE_NETMASK'} );
94 push(@colour, ${Header::colourorange} );
95 # Add Orange Routes to Array
96 @routes = `/sbin/route -n | /bin/grep $netsettings{'ORANGE_DEV'}`;
97 foreach my $route (@routes) {
98 chomp($route);
99 my @temp = split(/[\t ]+/, $route);
100 push(@network, $temp[0]);
101 push(@masklen, $temp[2]);
102 push(@colour, ${Header::colourorange} );
103 }
5433e2c9
MT
104}
105
f9aaffa6
MT
106# Add Blue Firewall Interface
107push(@network, $netsettings{'BLUE_ADDRESS'});
108push(@masklen, "255.255.255.255" );
109push(@colour, ${Header::colourfw} );
110
5433e2c9
MT
111# Add Blue Network
112if ($netsettings{'BLUE_DEV'}) {
f9aaffa6
MT
113 push(@network, $netsettings{'BLUE_NETADDRESS'});
114 push(@masklen, $netsettings{'BLUE_NETMASK'} );
115 push(@colour, ${Header::colourblue} );
116 # Add Blue Routes to Array
117 @routes = `/sbin/route -n | /bin/grep $netsettings{'BLUE_DEV'}`;
118 foreach my $route (@routes) {
119 chomp($route);
120 my @temp = split(/[\t ]+/, $route);
121 push(@network, $temp[0]);
122 push(@masklen, $temp[2]);
123 push(@colour, ${Header::colourblue} );
124 }
5433e2c9
MT
125}
126
6e13d0a5
MT
127# Add OpenVPN net and RED/BLUE/ORANGE entry (when appropriate)
128if (-e "${General::swroot}/ovpn/settings") {
129 my %ovpnsettings = ();
130 &General::readhash("${General::swroot}/ovpn/settings", \%ovpnsettings);
131 my @tempovpnsubnet = split("\/",$ovpnsettings{'DOVPN_SUBNET'});
132
133 # add OpenVPN net
f9aaffa6
MT
134 push(@network, $tempovpnsubnet[0]);
135 push(@masklen, $tempovpnsubnet[1]);
136 push(@colour, ${Header::colourovpn} );
6e13d0a5 137
7dbf47dc 138
6e13d0a5
MT
139 if ( ($ovpnsettings{'ENABLED_BLUE'} eq 'on') && $netsettings{'BLUE_DEV'} ) {
140 # add BLUE:port / proto
f9aaffa6
MT
141 push(@network, $netsettings{'BLUE_ADDRESS'} );
142 push(@masklen, '255.255.255.255' );
143 push(@colour, ${Header::colourovpn} );
6e13d0a5
MT
144 }
145 if ( ($ovpnsettings{'ENABLED_ORANGE'} eq 'on') && $netsettings{'ORANGE_DEV'} ) {
146 # add ORANGE:port / proto
f9aaffa6
MT
147 push(@network, $netsettings{'ORANGE_ADDRESS'} );
148 push(@masklen, '255.255.255.255' );
149 push(@colour, ${Header::colourovpn} );
6e13d0a5
MT
150 }
151}
152
ac1cfefa
MT
153# Add STATIC RED aliases
154if ($netsettings{'RED_DEV'}) {
f9aaffa6
MT
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)
159 {
160 chomp($line);
161 my @temp = split(/\,/,$line);
162 if ( $temp[0] ) {
163 push(@network, $temp[0]);
164 push(@masklen, $netsettings{'RED_NETMASK'} );
165 push(@colour, ${Header::colourfw} );
166 }
167 }
168 }
ac1cfefa
MT
169}
170
171# Add VPNs
172if ( $vpn[0] ne 'none' ) {
f9aaffa6
MT
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} );
179 }
ac1cfefa
MT
180}
181if (open(IP, "${General::swroot}/red/local-ipaddress")) {
f9aaffa6
MT
182 my $redip = <IP>;
183 close(IP);
184 chomp $redip;
185 push(@network, $redip);
186 push(@masklen, '255.255.255.255' );
187 push(@colour, ${Header::colourfw} );
ac1cfefa
MT
188}
189
ac1cfefa 190
c2b15814
MT
191#Establish simple filtering&sorting boxes on top of table
192
193our %cgiparams;
194&Header::getcgihash(\%cgiparams);
195
196my @list_proto = ($Lang::tr{'all'}, 'icmp', 'udp', 'tcp');
197my @list_state = ($Lang::tr{'all'}, 'SYN_SENT', 'SYN_RECV', 'ESTABLISHED', 'FIN_WAIT',
f9aaffa6 198 'CLOSE_WAIT', 'LAST_ACK', 'TIME_WAIT', 'CLOSE', 'LISTEN');
c2b15814
MT
199my @list_mark = ($Lang::tr{'all'}, '[ASSURED]', '[UNREPLIED]');
200my @list_sort = ('orgsip','protocol', 'expires', 'status', 'orgdip', 'orgsp',
f9aaffa6 201 'orgdp', 'exsip', 'exdip', 'exsp', 'exdp', 'marked');
c2b15814
MT
202
203# init or silently correct unknown value...
204if ( ! grep ( /^$cgiparams{'SEE_PROTO'}$/ , @list_proto )) { $cgiparams{'SEE_PROTO'} = $list_proto[0] };
205if ( ! grep ( /^$cgiparams{'SEE_STATE'}$/ , @list_state )) { $cgiparams{'SEE_STATE'} = $list_state[0] };
5433e2c9
MT
206if ( ($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] };
c2b15814
MT
210if ( ! grep ( /^$cgiparams{'SEE_SORT'}$/ , @list_sort )) { $cgiparams{'SEE_SORT'} = $list_sort[0] };
211# *.*.*.* or a valid IP
212if ( $cgiparams{'SEE_SRC'} !~ /^(\*\.\*\.\*\.\*\.|\d+\.\d+\.\d+\.\d+)$/) { $cgiparams{'SEE_SRC'} = '*.*.*.*' };
213if ( $cgiparams{'SEE_DEST'} !~ /^(\*\.\*\.\*\.\*\.|\d+\.\d+\.\d+\.\d+)$/) { $cgiparams{'SEE_DEST'} = '*.*.*.*' };
214
215
216our %entries = (); # will hold the lines analyzed correctly
217my $unknownlines = ''; # should be empty all the time...
218my $index = 0; # just a counter to make unique entryies in entries
ac1cfefa 219
c2b15814
MT
220&Header::showhttpheaders();
221&Header::openpage($Lang::tr{'connections'}, 1, '');
222&Header::openbigbox('100%', 'left');
223&Header::openbox('100%', 'left', $Lang::tr{'connection tracking'});
224
1465b127
MT
225# Build listbox objects
226my $menu_proto = &make_select ('SEE_PROTO', $cgiparams{'SEE_PROTO'}, @list_proto);
227my $menu_state = &make_select ('SEE_STATE', $cgiparams{'SEE_STATE'}, @list_state);
228
c2b15814 229print <<END
5433e2c9 230<form method='post' action='$ENV{'SCRIPT_NAME'}'>
4809e64e 231<table width='100%'>
c2b15814
MT
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>
78331e30 237 <td align='center' bgcolor='${Header::colourfw}'><b><font color='#FFFFFF'>IPFire</font></b></td>
c2b15814 238 <td align='center' bgcolor='${Header::colourvpn}'><b><font color='#FFFFFF'>$Lang::tr{'vpn'}</font></b></td>
6e13d0a5 239 <td align='center' bgcolor='${Header::colourovpn}'><b><font color='#FFFFFF'>$Lang::tr{'OpenVPN'}</font></b></td>
c2b15814
MT
240</tr>
241</table>
1465b127 242<br />
4809e64e 243<table width='100%'>
1465b127
MT
244<tr><td align='center'><font size=2>$Lang::tr{'source ip and port'}</font></td>
245 <td>&nbsp;</td>
246 <td align='center'><font size=2>$Lang::tr{'dest ip and port'}</font></td>
247 <td>&nbsp;</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>
4809e64e 250 <td align='center'><font size=2>$Lang::tr{'expires'}<br></br>($Lang::tr{'seconds'})</font></td>
1465b127 251
c2b15814 252</tr>
1465b127
MT
253<tr><td colspan='4'>&nbsp;</td>
254 <td align='center'>$menu_proto</td>
c2b15814 255 <td align='center'>$menu_state</td>
1465b127 256 <td>&nbsp;</td>
c2b15814 257</tr>
4809e64e 258<tr>
1465b127 259 <td align='center' colspan='7'></td>
4809e64e
MT
260</tr>
261<tr>
1465b127 262 <td align='center' colspan='7'><input type='submit' value="$Lang::tr{'update'}" /></td>
4809e64e
MT
263</tr>
264
c2b15814
MT
265END
266;
267
1465b127
MT
268my $i=0;
269foreach my $line (@active) {
270 $i++;
271 if ($i < 3) {
272 next;
273 }
274 chomp($line);
275 my @temp = split(' ',$line);
276
277 my ($sip, $sport) = split(':', $temp[0]);
278 my ($dip, $dport) = split(':', $temp[1]);
279 my $proto = $temp[2];
7dbf47dc
CS
280 my $state; my $ttl;
281 if ( $proto eq "esp" ){$state = "";$ttl = $temp[3];}
282 elsif ( $proto eq "icmp" ){$state = "";$ttl = $temp[4];}
283 else{$state = $temp[3];$ttl = $temp[4];}
1465b127
MT
284
285 next if( !(
286 (($cgiparams{'SEE_PROTO'} eq $Lang::tr{'all'}) || ($proto eq $cgiparams{'SEE_PROTO'} ))
287 && (($cgiparams{'SEE_STATE'} eq $Lang::tr{'all'}) || ($state eq $cgiparams{'SEE_STATE'} ))
288 && (($cgiparams{'SEE_SRC'} eq "*.*.*.*") || ($sip eq $cgiparams{'SEE_SRC'} ))
289 && (($cgiparams{'SEE_DEST'} eq "*.*.*.*") || ($dip eq $cgiparams{'SEE_DEST'} ))
290 ));
291
292 if (($proto eq 'udp') && ($ttl eq '')) {
293 $ttl = $state;
294 $state = '&nbsp;';
295 }
296
297 my $sipcol = ipcolour($sip);
298 my $dipcol = ipcolour($dip);
299
300 my $sserv = '';
301 if ($sport < 1024) {
302 $sserv = uc(getservbyport($sport, lc($proto)));
303 if ($sserv ne '') {
304 $sserv = "&nbsp;($sserv)";
305 }
306 }
307
308 my $dserv = '';
309 if ($dport < 1024) {
310 $dserv = uc(getservbyport($dport, lc($proto)));
311 if ($dserv ne '') {
312 $dserv = "&nbsp;($dserv)";
313 }
314 }
315
316 print <<END
317 <tr >
318 <td align='center' bgcolor='$sipcol'>
319 <a href='/cgi-bin/ipinfo.cgi?ip=$sip'>
320 <font color='#FFFFFF'>$sip</font>
321 </a>
322 </td>
323 <td align='center' bgcolor='$sipcol'>
324 <a href='http://isc.sans.org/port_details.php?port=$sport' target='top'>
325 <font color='#FFFFFF'>$sport$sserv</font>
326 </a>
327 </td>
328 <td align='center' bgcolor='$dipcol'>
329 <a href='/cgi-bin/ipinfo.cgi?ip=$dip'>
330 <font color='#FFFFFF'>$dip</font>
331 </a>
332 </td>
333 <td align='center' bgcolor='$dipcol'>
334 <a href='http://isc.sans.org/port_details.php?port=$dport' target='top'>
335 <font color='#FFFFFF'>$dport$dserv</font>
336 </a>
337 </td>
338 <td align='center'>$proto</td>
f2fdd0c1 339 <td align='center'>$state</td>
1465b127
MT
340 <td align='center'>$ttl</td>
341 </tr>
ac1cfefa 342END
c2b15814 343;
ac1cfefa 344}
c2b15814 345
1465b127 346print "</table></form>";
ac1cfefa
MT
347
348&Header::closebox();
349&Header::closebigbox();
350&Header::closepage();
351
352sub ipcolour($) {
f9aaffa6
MT
353 my $id = 0;
354 my $line;
355 my $colour = ${Header::colourred};
356 my ($ip) = $_[0];
c2b15814
MT
357 my $found = 0;
358 foreach $line (@network) {
1465b127
MT
359 if ($network[$id] eq '') {
360 $id++;
361 } else {
362 if (!$found && ipv4_in_network( $network[$id] , $masklen[$id], $ip) ) {
363 $found = 1;
364 $colour = $colour[$id];
365 }
366 $id++;
367 }
f9aaffa6
MT
368 }
369 return $colour
ac1cfefa 370}
c2b15814
MT
371
372# Create a string containing a complete SELECT html object
373# param1: name
374# param2: current value selected
375# param3: field list
376sub make_select ($,$,$) {
f9aaffa6
MT
377 my $select_name = shift;
378 my $selected = shift;
379 my $select = "<select name='$select_name'>";
380
381 foreach my $value (@_) {
382 my $check = $selected eq $value ? "selected='selected'" : '';
383 $select .= "<option $check value='$value'>$value</option>";
384 }
385 $select .= "</select>";
386 return $select;
c2b15814
MT
387}
388
389# Build a list of IP obtained from the %entries hash
390# param1: IP field name
391sub get_known_ips ($) {
f9aaffa6
MT
392 my $field = shift;
393 my $qs = $cgiparams{'SEE_SORT'}; # switch the sort order
394 $cgiparams{'SEE_SORT'} = $field;
c2b15814 395
f9aaffa6
MT
396 my @liste=('*.*.*.*');
397 foreach my $entry ( sort sort_entries keys %entries) {
398 push (@liste, $entries{$entry}->{$field}) if (! grep (/^$entries{$entry}->{$field}$/,@liste) );
399 }
c2b15814 400
f9aaffa6 401 $cgiparams{'SEE_SORT'} = $qs; #restore sort order
c2b15814
MT
402 return @liste;
403}
404
405# Used to sort the table containing the lines displayed.
406sub sort_entries { #Reverse is not implemented
407 my $qs=$cgiparams{'SEE_SORT'};
408 if ($qs =~ /orgsip|orgdip|exsip|exdip/) {
f9aaffa6
MT
409 my @a = split(/\./,$entries{$a}->{$qs});
410 my @b = split(/\./,$entries{$b}->{$qs});
411 ($a[0]<=>$b[0]) ||
412 ($a[1]<=>$b[1]) ||
413 ($a[2]<=>$b[2]) ||
414 ($a[3]<=>$b[3]);
415 } elsif ($qs =~ /expire|orgsp|orgdp|exsp|exdp/) {
416 $entries{$a}->{$qs} <=> $entries{$b}->{$qs};
417 } else {
418 $entries{$a}->{$qs} cmp $entries{$b}->{$qs};
419 }
c2b15814
MT
420}
421
4221;