]>
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
16 use Geo
::IP
::PurePerl
;
19 # enable only the following on debugging purpose
21 #use CGI::Carp 'fatalsToBrowser';
23 require '/var/ipfire/general-functions.pl';
24 require "${General::swroot}/geoip-functions.pl";
25 require "${General::swroot}/lang.pl";
26 require "${General::swroot}/header.pl";
29 my %mainsettings = ();
30 &General
::readhash
("${General::swroot}/main/settings", \
%mainsettings);
31 &General
::readhash
("/srv/web/ipfire/html/themes/".$mainsettings{'THEME'}."/include/colors.txt", \
%color);
35 #workaround to suppress a warning when a variable is used only once
36 my @dummy = ( ${Header
::table2colour
} );
41 my $errormessage = '';
43 my @shortmonths = ( 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug',
44 'Sep', 'Oct', 'Nov', 'Dec' );
45 my @longmonths = ( $Lang::tr
{'january'}, $Lang::tr
{'february'}, $Lang::tr
{'march'},
46 $Lang::tr
{'april'}, $Lang::tr
{'may'}, $Lang::tr
{'june'}, $Lang::tr
{'july'}, $Lang::tr
{'august'},
47 $Lang::tr
{'september'}, $Lang::tr
{'october'}, $Lang::tr
{'november'},
48 $Lang::tr
{'december'} );
50 my @now = localtime();
54 my $year = $now[5]+1900;
56 $cgiparams{'DAY'} = $now[3];
57 $cgiparams{'MONTH'} = $now[4];
58 $cgiparams{'ACTION'} = '';
60 &Header
::getcgihash
(\
%cgiparams);
61 $logsettings{'LOGVIEW_REVERSE'} = 'off';
62 &General
::readhash
("${General::swroot}/logging/settings", \
%logsettings);
63 ${Header
::viewsize
} = defined ($logsettings{'LOGVIEW_VIEWSIZE'}) ?
$logsettings{'LOGVIEW_VIEWSIZE'} : 150;
65 my $start = ($logsettings{'LOGVIEW_REVERSE'} eq 'on') ?
0x7FFFF000 : 0; #index of firts line number to display
67 if ($ENV{'QUERY_STRING'} && $cgiparams{'ACTION'} ne $Lang::tr
{'update'})
69 my @temp = split(',',$ENV{'QUERY_STRING'});
71 $cgiparams{'MONTH'} = $temp[1];
72 $cgiparams{'DAY'} = $temp[2];
75 if (!($cgiparams{'MONTH'} =~ /^(0|1|2|3|4|5|6|7|8|9|10|11)$/) ||
76 !($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)$/))
78 $cgiparams{'DAY'} = $now[3];
79 $cgiparams{'MONTH'} = $now[4];
81 elsif($cgiparams{'ACTION'} eq '>>')
84 my @temp_now = localtime(time);
85 $temp_now[4] = $cgiparams{'MONTH'};
86 $temp_now[3] = $cgiparams{'DAY'};
87 if ($cgiparams{'DAY'}) {
88 @temp_then = localtime(POSIX
::mktime
(@temp_now) + 86400);
89 ## Retrieve the same time on the next day +
90 ## 86400 seconds in a day
93 $temp_now[4] = ($temp_now[4]+1) %12;
94 @temp_then = localtime(POSIX
::mktime
(@temp_now) );
97 $cgiparams{'MONTH'} = $temp_then[4];
98 $cgiparams{'DAY'} = $temp_then[3];
100 elsif($cgiparams{'ACTION'} eq '<<')
103 my @temp_now = localtime(time);
104 $temp_now[4] = $cgiparams{'MONTH'};
105 $temp_now[3] = $cgiparams{'DAY'};
106 if ($cgiparams{'DAY'}) {
107 @temp_then = localtime(POSIX
::mktime
(@temp_now) - 86400);
108 ## Retrieve the same time on the next day -
109 ## 86400 seconds in a day
112 $temp_now[4] = ($temp_now[4]-1) %12;
113 @temp_then = localtime(POSIX
::mktime
(@temp_now) );
116 $cgiparams{'MONTH'} = $temp_then[4];
117 $cgiparams{'DAY'} = $temp_then[3];
120 # Find in which file.gz is the log. Can be calculated because WEEKLY ROTATING of access.log
122 my $date = $cgiparams{'DAY'} == 0 ?
'' : $cgiparams{'DAY'} <= 9 ?
"0$cgiparams{'DAY'}" : "$cgiparams{'DAY'}";
127 # Calculate time. If future date, calculate for past year !!!
128 if (( $cgiparams{'MONTH'} eq $now[4]) && ($cgiparams{'DAY'} > $now[3]) ||
129 ( $cgiparams{'MONTH'} > $now[4] ) ) {
130 $xday = POSIX
::mktime
( 0, 0, 0, $cgiparams{'DAY'}, $cgiparams{'MONTH'}, $year - 1901 );
131 $date = "$longmonths[$cgiparams{'MONTH'}] $date, ". int($year-1);
133 $xday = POSIX
::mktime
( 0, 0, 0, $cgiparams{'DAY'}, $cgiparams{'MONTH'}, $year - 1900 );
134 $date = "$longmonths[$cgiparams{'MONTH'}] $date, $year";
137 # calculate end of active week (saturday 23H59)
139 @then = localtime(time());
140 my $sunday = POSIX
::mktime
( 0, 0, 0, @then[3], @then[4], @then[5]);
141 $sunday += (6-$then[6]) * 86400;
143 # Convert delta in second to full weeks
144 $gzindex = int (($sunday-$xday)/604800 );
147 my $monthstr = $shortmonths[$cgiparams{'MONTH'}];
148 my $daystr = $cgiparams{'DAY'} == 0 ?
'..' : $cgiparams{'DAY'} <= 9 ?
" $cgiparams{'DAY'}" : "$cgiparams{'DAY'}";
155 my $lastdatetime; # for debug
156 my $search_for_end = 0;
158 while ($gzindex >=0 && $loop) {
159 # calculate file name
161 $filestr = "/var/log/messages";
163 $filestr = "/var/log/messages.$gzindex";
164 $filestr = "$filestr.gz" if -f
"$filestr.gz";
166 # now read file if existing
167 if (open (FILE
,($filestr =~ /.gz$/ ?
"gzip -dc $filestr |" : $filestr))) {
168 #&General::log("reading $filestr");
169 READ
:while (<FILE
>) {
171 if ($line =~ /^${monthstr} ${daystr} ..:..:.. [\w\-]+ kernel:.*IN=.*$/) {
172 # when standart viewing, just keep in memory the correct slice
173 # it starts a '$start' and size is $viewport
174 # If export, then keep all lines...
175 if ($cgiparams{'ACTION'} eq $Lang::tr
{'export'}){
176 $log[$lines++] = "$line";
178 if ($lines++ < ($start + $Header::viewsize
)) {
180 if (@log > $Header::viewsize
) {
183 #} else { dont do this optimisation, need to count lines !
184 # $datetime = $maxtime; # we have read viewsize lines, stop main loop
185 # last READ; # exit read file
188 $search_for_end = 1; # we find the start of slice, can look for end now
190 if ($search_for_end == 1) {
191 #finish read files when date is over (test month equality only)
192 $line =~ /^(...) (..) ..:..:..*$/;
193 $loop = 0 if ( ($1 ne $monthstr) || ( ($daystr ne '..') && ($daystr ne $2) ) );
199 $gzindex--; # will try next gz file eg 40,39,38,.... because it may have holes when ipcop stopped
203 # $errormessage = "$Lang::tr{'date not in logs'}: $filestr $Lang::tr{'could not be opened'}";
205 if ($cgiparams{'ACTION'} eq $Lang::tr
{'export'})
207 print "Content-type: text/plain\n\n";
208 print "IPFire firewall log\r\n";
209 print "$Lang::{'date'}: $date\r\n\r\n";
211 if ($logsettings{'LOGVIEW_REVERSE'} eq 'on') { @log = reverse @log; }
215 /^... (..) (..:..:..) [\w\-]+ kernel:.*(IN=.*)$/;
218 my $time = $cgiparams{'DAY'} ?
"$2" : "$day/$2" ;
219 print "$time $3\r\n";
225 &Header
::showhttpheaders
();
227 &Header
::openpage
($Lang::tr
{'firewall log'}, 1, '');
229 &Header
::openbigbox
('100%', 'left', '', $errormessage);
232 &Header
::openbox
('100%', 'left', $Lang::tr
{'error messages'});
233 print "<font class='base'>$errormessage </font>\n";
237 &Header
::openbox
('100%', 'left', "$Lang::tr{'settings'}");
240 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
243 <td width='10%' class='base'>$Lang::tr{'month'}: </td>
245 <select name='MONTH'>
248 for (my $month = 0; $month < 12; $month++)
251 if ($month == $cgiparams{'MONTH'}) {
252 print "selected='selected' "; }
253 print "value='$month'>$longmonths[$month]</option>\n";
258 <td width='10%' class='base' align='right'> $Lang::tr{'day'}: </td>
263 print "<option value='0'>$Lang::tr{'all'}</option>\n";
264 for (my $day = 1; $day <= 31; $day++)
267 if ($day == $cgiparams{'DAY'}) {
268 print "selected='selected' "; }
269 print "value='$day'>$day</option>\n";
274 <td width='5%' align='center'><input type='submit' name='ACTION' title='$Lang::tr{'day before'}' value='<<' /></td>
275 <td width='5%' align='center'><input type='submit' name='ACTION' title='$Lang::tr{'day after'}' value='>>' /></td>
276 <td width='10%' align='center'><input type='submit' name='ACTION' value='$Lang::tr{'update'}' /></td>
277 <td width='10%' align='center'><input type='submit' name='ACTION' value='$Lang::tr{'export'}' /></td>
286 &Header
::openbox
('100%', 'left', $Lang::tr
{'log'});
287 print "<p><b>$Lang::tr{'firewall hits'} $date: $lines</b></p>";
289 my $lastPageIndex = $lines - ${Header
::viewsize
};
290 $start = $lastPageIndex if ($start >= $lastPageIndex);
291 $start = 0 if ($start < 0);
297 $prev = $start - ${Header
::viewsize
};
298 $prev = 0 if ( $prev < 0);
302 if ($start == $lastPageIndex) {
305 $next = $start + ${Header
::viewsize
};
306 $next = $lines - ${Header
::viewsize
} if ($next >= $lines - ${Header
::viewsize
});
309 if ($logsettings{'LOGVIEW_REVERSE'} eq 'on') { @log = reverse @log; }
310 if ($lines != 0) { &oldernewer
(); }
313 <table width='100%' class='tbl'>
315 <th align='center' class='boldbase'><b>$Lang::tr{'time'}</b></th>
316 <th align='center' class='boldbase'><b>$Lang::tr{'chain'}</b></th>
317 <th align='center' class='boldbase'><b>$Lang::tr{'iface'}</b></th>
318 <th align='center' class='boldbase'><b>$Lang::tr{'proto'}</b></th>
319 <th align='center' class='boldbase'><b>$Lang::tr{'source'}<br/>$Lang::tr{'destination'}</b></th>
320 <th align='center' class='boldbase'><b>$Lang::tr{'src port'}<br />$Lang::tr{'dst port'}</b></th>
321 <th align='center' class='boldbase'><b>$Lang::tr{'country'}</b></th>
322 <th align='center' class='boldbase'><b>$Lang::tr{'mac address'}</b></th>
331 /^... (..) (..:..:..) [\w\-]+ kernel:(.*)(IN=.*)$/;
334 my $time = $cgiparams{'DAY'} ?
"$2" : "$day/$2" ;
338 my ($iface, $srcaddr, $dstaddr, $macaddr, $proto, $srcport, $dstport);
339 $iface=$1 if $packet =~ /IN=(\w+)/;
340 $srcaddr=$1 if $packet =~ /SRC=([\d\.]+)/;
341 $dstaddr=$1 if $packet =~ /DST=([\d\.]+)/;
342 $macaddr=$1 if $packet =~ /MAC=([\w+\:]+)/;
343 $proto=$1 if $packet =~ /PROTO=(\w+)/;
344 $srcport=$1 if $packet =~ /SPT=(\d+)/;
345 $dstport=$1 if $packet =~ /DPT=(\d+)/;
347 my $gi = Geo
::IP
::PurePerl
->new();
348 my $ccode = $gi->country_code_by_name($srcaddr);
349 my $fcode = lc($ccode);
351 my $servi = uc(getservbyport($srcport, lc($proto)));
352 if ($servi ne '' && $srcport < 1024) {
353 $srcport = "$srcport($servi)"; }
354 $servi = uc(getservbyport($dstport, lc($proto)));
355 if ($servi ne '' && $dstport < 1024) {
356 $dstport = "$dstport($servi)";}
357 my @mactemp = split(/:/,$macaddr);
358 $macaddr = "$mactemp[6]:$mactemp[7]:$mactemp[8]:$mactemp[9]:$mactemp[10]:$mactemp[11]";
362 $col="bgcolor='$color{'color20'}'"; }
365 $col="bgcolor='$color{'color22'}'"; }
368 <td align='center' $col>$time</td>
369 <td align='center' $col>$comment</td>
370 <td align='center' $col>$iface</td>
371 <td align='center' $col>$proto</td>
372 <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>
373 <td align='center' $col>$srcport<br/>$dstport</td>
376 # Get flag icon for of the country.
377 my $flag_icon = &GeoIP
::get_flag_icon
($fcode);
380 print "<td align='center' $col><a href='../country.cgi#$fcode'><img src='$flag_icon' border='0' align='absmiddle' alt='$ccode'></a></td>";
382 print "<td align='center' $col></td>";
386 <td align='center' $col>$macaddr</td>
399 &Header
::closebigbox
();
401 &Header
::closepage
();
411 print "<td align='center' width='50%'>";
413 print "<a href='/cgi-bin/logs.cgi/firewalllog.dat?0,$cgiparams{'MONTH'},$cgiparams{'DAY'}'>$Lang::tr{'first'}</a> ";
414 print "<a href='/cgi-bin/logs.cgi/firewalllog.dat?$prev,$cgiparams{'MONTH'},$cgiparams{'DAY'}'>$Lang::tr{'older'}</a>"; }
416 print "$Lang::tr{'first'} $Lang::tr{'older'}"; }
419 print "<td align='center' width='50%'>";
421 print "<a href='/cgi-bin/logs.cgi/firewalllog.dat?$next,$cgiparams{'MONTH'},$cgiparams{'DAY'}'>$Lang::tr{'newer'}</a> ";
422 print "<a href='/cgi-bin/logs.cgi/firewalllog.dat?$lastPageIndex,$cgiparams{'MONTH'},$cgiparams{'DAY'}'>$Lang::tr{'last'}</a>"; }
424 print "$Lang::tr{'newer'} $Lang::tr{'last'} "; }