]>
git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blob - html/cgi-bin/logs.cgi/firewalllog.dat
5 # This code is distributed under the terms of the GPL
7 # (c) The SmoothWall Team
9 # $Id: firewalllog.dat,v 1.4.2.18 2005/08/23 12:01:50 eoberlander Exp $
11 # July 28, 2003 - Darren Critchley - darren@kdi.ca
12 # - added source mac adapter to layout
18 # enable only the following on debugging purpose
20 #use CGI::Carp 'fatalsToBrowser';
22 require '/var/ipfire/general-functions.pl';
23 require "${General::swroot}/geoip-functions.pl";
24 require "${General::swroot}/lang.pl";
25 require "${General::swroot}/header.pl";
27 # Libloc database handle.
28 my $libloc_db_handle = &GeoIP
::init
();
31 my %mainsettings = ();
32 &General
::readhash
("${General::swroot}/main/settings", \
%mainsettings);
33 &General
::readhash
("/srv/web/ipfire/html/themes/".$mainsettings{'THEME'}."/include/colors.txt", \
%color);
37 #workaround to suppress a warning when a variable is used only once
38 my @dummy = ( ${Header
::table2colour
} );
43 my $errormessage = '';
45 my @shortmonths = ( 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug',
46 'Sep', 'Oct', 'Nov', 'Dec' );
47 my @longmonths = ( $Lang::tr
{'january'}, $Lang::tr
{'february'}, $Lang::tr
{'march'},
48 $Lang::tr
{'april'}, $Lang::tr
{'may'}, $Lang::tr
{'june'}, $Lang::tr
{'july'}, $Lang::tr
{'august'},
49 $Lang::tr
{'september'}, $Lang::tr
{'october'}, $Lang::tr
{'november'},
50 $Lang::tr
{'december'} );
52 my @now = localtime();
56 my $year = $now[5]+1900;
58 $cgiparams{'DAY'} = $now[3];
59 $cgiparams{'MONTH'} = $now[4];
60 $cgiparams{'ACTION'} = '';
62 &Header
::getcgihash
(\
%cgiparams);
63 $logsettings{'LOGVIEW_REVERSE'} = 'off';
64 &General
::readhash
("${General::swroot}/logging/settings", \
%logsettings);
65 ${Header
::viewsize
} = defined ($logsettings{'LOGVIEW_VIEWSIZE'}) ?
$logsettings{'LOGVIEW_VIEWSIZE'} : 150;
67 my $start = ($logsettings{'LOGVIEW_REVERSE'} eq 'on') ?
0x7FFFF000 : 0; #index of firts line number to display
69 if ($ENV{'QUERY_STRING'} && $cgiparams{'ACTION'} ne $Lang::tr
{'update'})
71 my @temp = split(',',$ENV{'QUERY_STRING'});
73 $cgiparams{'MONTH'} = $temp[1];
74 $cgiparams{'DAY'} = $temp[2];
77 if (!($cgiparams{'MONTH'} =~ /^(0|1|2|3|4|5|6|7|8|9|10|11)$/) ||
78 !($cgiparams{'DAY'} =~ /^(0|1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24|25|26|27|28|29|30|31)$/))
80 $cgiparams{'DAY'} = $now[3];
81 $cgiparams{'MONTH'} = $now[4];
83 elsif($cgiparams{'ACTION'} eq '>>')
86 my @temp_now = localtime(time);
87 $temp_now[4] = $cgiparams{'MONTH'};
88 $temp_now[3] = $cgiparams{'DAY'};
89 if ($cgiparams{'DAY'}) {
90 @temp_then = localtime(POSIX
::mktime
(@temp_now) + 86400);
91 ## Retrieve the same time on the next day +
92 ## 86400 seconds in a day
95 $temp_now[4] = ($temp_now[4]+1) %12;
96 @temp_then = localtime(POSIX
::mktime
(@temp_now) );
99 $cgiparams{'MONTH'} = $temp_then[4];
100 $cgiparams{'DAY'} = $temp_then[3];
102 elsif($cgiparams{'ACTION'} eq '<<')
105 my @temp_now = localtime(time);
106 $temp_now[4] = $cgiparams{'MONTH'};
107 $temp_now[3] = $cgiparams{'DAY'};
108 if ($cgiparams{'DAY'}) {
109 @temp_then = localtime(POSIX
::mktime
(@temp_now) - 86400);
110 ## Retrieve the same time on the next day -
111 ## 86400 seconds in a day
114 $temp_now[4] = ($temp_now[4]-1) %12;
115 @temp_then = localtime(POSIX
::mktime
(@temp_now) );
118 $cgiparams{'MONTH'} = $temp_then[4];
119 $cgiparams{'DAY'} = $temp_then[3];
122 # Find in which file.gz is the log. Can be calculated because WEEKLY ROTATING of access.log
124 my $date = $cgiparams{'DAY'} == 0 ?
'' : $cgiparams{'DAY'} <= 9 ?
"0$cgiparams{'DAY'}" : "$cgiparams{'DAY'}";
129 # Calculate time. If future date, calculate for past year !!!
130 if (( $cgiparams{'MONTH'} eq $now[4]) && ($cgiparams{'DAY'} > $now[3]) ||
131 ( $cgiparams{'MONTH'} > $now[4] ) ) {
132 $xday = POSIX
::mktime
( 0, 0, 0, $cgiparams{'DAY'}, $cgiparams{'MONTH'}, $year - 1901 );
133 $date = "$longmonths[$cgiparams{'MONTH'}] $date, ". int($year-1);
135 $xday = POSIX
::mktime
( 0, 0, 0, $cgiparams{'DAY'}, $cgiparams{'MONTH'}, $year - 1900 );
136 $date = "$longmonths[$cgiparams{'MONTH'}] $date, $year";
139 # calculate end of active week (saturday 23H59)
141 @then = localtime(time());
142 my $sunday = POSIX
::mktime
( 0, 0, 0, @then[3], @then[4], @then[5]);
143 $sunday += (6-$then[6]) * 86400;
145 # Convert delta in second to full weeks
146 $gzindex = int (($sunday-$xday)/604800 );
149 my $monthstr = $shortmonths[$cgiparams{'MONTH'}];
150 my $daystr = $cgiparams{'DAY'} == 0 ?
'..' : $cgiparams{'DAY'} <= 9 ?
" $cgiparams{'DAY'}" : "$cgiparams{'DAY'}";
157 my $lastdatetime; # for debug
158 my $search_for_end = 0;
160 while ($gzindex >=0 && $loop) {
161 # calculate file name
163 $filestr = "/var/log/messages";
165 $filestr = "/var/log/messages.$gzindex";
166 $filestr = "$filestr.gz" if -f
"$filestr.gz";
168 # now read file if existing
169 if (open (FILE
,($filestr =~ /.gz$/ ?
"gzip -dc $filestr |" : $filestr))) {
170 #&General::log("reading $filestr");
171 READ
:while (<FILE
>) {
173 if ($line =~ /^${monthstr} ${daystr} ..:..:.. [\w\-]+ kernel:.*IN=.*$/) {
174 # when standart viewing, just keep in memory the correct slice
175 # it starts a '$start' and size is $viewport
176 # If export, then keep all lines...
177 if ($cgiparams{'ACTION'} eq $Lang::tr
{'export'}){
178 $log[$lines++] = "$line";
180 if ($lines++ < ($start + $Header::viewsize
)) {
182 if (@log > $Header::viewsize
) {
185 #} else { dont do this optimisation, need to count lines !
186 # $datetime = $maxtime; # we have read viewsize lines, stop main loop
187 # last READ; # exit read file
190 $search_for_end = 1; # we find the start of slice, can look for end now
192 if ($search_for_end == 1) {
193 #finish read files when date is over (test month equality only)
194 $line =~ /^(...) (..) ..:..:..*$/;
195 $loop = 0 if ( ($1 ne $monthstr) || ( ($daystr ne '..') && ($daystr ne $2) ) );
201 $gzindex--; # will try next gz file eg 40,39,38,.... because it may have holes when ipcop stopped
205 # $errormessage = "$Lang::tr{'date not in logs'}: $filestr $Lang::tr{'could not be opened'}";
207 if ($cgiparams{'ACTION'} eq $Lang::tr
{'export'})
209 print "Content-type: text/plain\n\n";
210 print "IPFire firewall log\r\n";
211 print "$Lang::{'date'}: $date\r\n\r\n";
213 if ($logsettings{'LOGVIEW_REVERSE'} eq 'on') { @log = reverse @log; }
217 /^... (..) (..:..:..) [\w\-]+ kernel:.*(IN=.*)$/;
220 my $time = $cgiparams{'DAY'} ?
"$2" : "$day/$2" ;
221 print "$time $3\r\n";
227 &Header
::showhttpheaders
();
229 &Header
::openpage
($Lang::tr
{'firewall log'}, 1, '');
231 &Header
::openbigbox
('100%', 'left', '', $errormessage);
234 &Header
::openbox
('100%', 'left', $Lang::tr
{'error messages'});
235 print "<font class='base'>$errormessage </font>\n";
239 &Header
::openbox
('100%', 'left', "$Lang::tr{'settings'}");
242 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
245 <td width='10%' class='base'>$Lang::tr{'month'}: </td>
247 <select name='MONTH'>
250 for (my $month = 0; $month < 12; $month++)
253 if ($month == $cgiparams{'MONTH'}) {
254 print "selected='selected' ";
256 print "value='$month'>$longmonths[$month]</option>\n";
261 <td width='10%' class='base' align='right'> $Lang::tr{'day'}: </td>
266 print "<option value='0'>$Lang::tr{'all'}</option>\n";
267 for (my $day = 1; $day <= 31; $day++)
270 if ($day == $cgiparams{'DAY'}) {
271 print "selected='selected' ";
273 print "value='$day'>$day</option>\n";
278 <td width='5%' align='center'><input type='submit' name='ACTION' title='$Lang::tr{'day before'}' value='<<' /></td>
279 <td width='5%' align='center'><input type='submit' name='ACTION' title='$Lang::tr{'day after'}' value='>>' /></td>
280 <td width='10%' align='center'><input type='submit' name='ACTION' value='$Lang::tr{'update'}' /></td>
281 <td width='10%' align='center'><input type='submit' name='ACTION' value='$Lang::tr{'export'}' /></td>
290 &Header
::openbox
('100%', 'left', $Lang::tr
{'log'});
291 print "<p><b>$Lang::tr{'firewall hits'} $date: $lines</b></p>";
293 my $lastPageIndex = $lines - ${Header
::viewsize
};
294 $start = $lastPageIndex if ($start >= $lastPageIndex);
295 $start = 0 if ($start < 0);
301 $prev = $start - ${Header
::viewsize
};
302 $prev = 0 if ( $prev < 0);
306 if ($start == $lastPageIndex) {
309 $next = $start + ${Header
::viewsize
};
310 $next = $lines - ${Header
::viewsize
} if ($next >= $lines - ${Header
::viewsize
});
313 if ($logsettings{'LOGVIEW_REVERSE'} eq 'on') { @log = reverse @log; }
314 if ($lines != 0) { &oldernewer
(); }
317 <table width='100%' class='tbl'>
319 <th align='center' class='boldbase'><b>$Lang::tr{'time'}</b></th>
320 <th align='center' class='boldbase'><b>$Lang::tr{'chain'}</b></th>
321 <th align='center' class='boldbase'><b>$Lang::tr{'iface'}</b></th>
322 <th align='center' class='boldbase'><b>$Lang::tr{'proto'}</b></th>
323 <th align='center' class='boldbase'><b>$Lang::tr{'source'}<br/>$Lang::tr{'destination'}</b></th>
324 <th align='center' class='boldbase'><b>$Lang::tr{'src port'}<br />$Lang::tr{'dst port'}</b></th>
325 <th align='center' class='boldbase'><b>$Lang::tr{'country'}</b></th>
326 <th align='center' class='boldbase'><b>$Lang::tr{'mac address'}</b></th>
335 # If ipv6 uses a bridge, PHYSIN= contains the relevant iface information
338 if ($_ =~ /^... (..) (..:..:..) [\w\-]+ kernel:(.*)(IN=.*)(PHYSIN=.*)$/) { $packet = $5; }
339 elsif ($_ =~ /^... (..) (..:..:..) [\w\-]+ kernel:(.*)(IN=.*)$/) { $packet = $4; }
342 my $time = $cgiparams{'DAY'} ?
"$2" : "$day/$2" ;
345 my ($iface, $srcaddr, $dstaddr, $macaddr, $proto, $srcport, $dstport);
346 if ($packet =~ /PHYSIN=(\w+)/) { $iface=$1; } elsif ($packet =~ /IN=(\w+)/) { $iface = $1; }
347 # Identify whether ipv4 or ipv6. Both are mutally exclusive.
348 if ($packet =~ /SRC\=(([\d]{1,3})(\.([\d]{1,3})){3})/) { $srcaddr=$1; }
349 if ($packet =~ /SRC\=(([0-9a-fA-F]{0,4})(\:([0-9a-fA-F]{0,4})){2,7})/) { $srcaddr=$1; }
350 if ($packet =~ /DST\=(([\d]{1,3})(\.([\d]{1,3})){3})/) { $dstaddr=$1; }
351 if ($packet =~ /DST\=(([0-9a-fA-F]{0,4})(\:([0-9a-fA-F]{0,4})){2,7})/) { $dstaddr=$1; }
352 $macaddr=$1 if $packet =~ /MAC=([\w+\:]+)/;
353 $proto=$1 if $packet =~ /PROTO=(\w+)/;
354 $srcport=$1 if $packet =~ /SPT=(\d+)/;
355 $dstport=$1 if $packet =~ /DPT=(\d+)/;
357 my $ccode = &GeoIP
::lookup_country_code
($libloc_db_handle, $srcaddr);
359 my $servi = uc(getservbyport($srcport, lc($proto)));
360 if ($servi ne '' && $srcport < 1024) {
361 $srcport = "$srcport($servi)";
363 $servi = uc(getservbyport($dstport, lc($proto)));
364 if ($servi ne '' && $dstport < 1024) {
365 $dstport = "$dstport($servi)";
367 my @mactemp = split(/:/,$macaddr);
368 $macaddr = "$mactemp[6]:$mactemp[7]:$mactemp[8]:$mactemp[9]:$mactemp[10]:$mactemp[11]";
372 $col="bgcolor='$color{'color20'}'";
376 $col="bgcolor='$color{'color22'}'";
380 <td align='center' $col>$time</td>
381 <td align='center' $col>$chain</td>
382 <td align='center' $col>$iface</td>
383 <td align='center' $col>$proto</td>
384 <td align='center' $col><a href='/cgi-bin/ipinfo.cgi?ip=$srcaddr'>$srcaddr</a><br /><a href='/cgi-bin/ipinfo.cgi?ip=$dstaddr'>$dstaddr</a></td>
385 <td align='center' $col>$srcport<br/>$dstport</td>
388 # Get flag icon for of the country.
389 my $flag_icon = &GeoIP
::get_flag_icon
($ccode);
392 print "<td align='center' $col><a href='../country.cgi#$ccode'><img src='$flag_icon' border='0' align='absmiddle' alt='$ccode'></a></td>";
394 print "<td align='center' $col></td>";
398 <td align='center' $col>$macaddr</td>
411 &Header
::closebigbox
();
413 &Header
::closepage
();
423 print "<td align='center' width='50%'>";
425 print "<a href='/cgi-bin/logs.cgi/firewalllog.dat?0,$cgiparams{'MONTH'},$cgiparams{'DAY'}'>$Lang::tr{'first'}</a> ";
426 print "<a href='/cgi-bin/logs.cgi/firewalllog.dat?$prev,$cgiparams{'MONTH'},$cgiparams{'DAY'}'>$Lang::tr{'older'}</a>";
429 print "$Lang::tr{'first'} $Lang::tr{'older'}";
433 print "<td align='center' width='50%'>";
435 print "<a href='/cgi-bin/logs.cgi/firewalllog.dat?$next,$cgiparams{'MONTH'},$cgiparams{'DAY'}'>$Lang::tr{'newer'}</a> ";
436 print "<a href='/cgi-bin/logs.cgi/firewalllog.dat?$lastPageIndex,$cgiparams{'MONTH'},$cgiparams{'DAY'}'>$Lang::tr{'last'}</a>";
439 print "$Lang::tr{'newer'} $Lang::tr{'last'} ";