]>
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}/lang.pl";
25 require "${General::swroot}/header.pl";
28 my %mainsettings = ();
29 &General
::readhash
("${General::swroot}/main/settings", \
%mainsettings);
30 &General
::readhash
("/srv/web/ipfire/html/themes/".$mainsettings{'THEME'}."/include/colors.txt", \
%color);
34 #workaround to suppress a warning when a variable is used only once
35 my @dummy = ( ${Header
::table2colour
} );
40 my $errormessage = '';
42 my @shortmonths = ( 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug',
43 'Sep', 'Oct', 'Nov', 'Dec' );
44 my @longmonths = ( $Lang::tr
{'january'}, $Lang::tr
{'february'}, $Lang::tr
{'march'},
45 $Lang::tr
{'april'}, $Lang::tr
{'may'}, $Lang::tr
{'june'}, $Lang::tr
{'july'}, $Lang::tr
{'august'},
46 $Lang::tr
{'september'}, $Lang::tr
{'october'}, $Lang::tr
{'november'},
47 $Lang::tr
{'december'} );
49 my @now = localtime();
53 my $year = $now[5]+1900;
55 $cgiparams{'DAY'} = $now[3];
56 $cgiparams{'MONTH'} = $now[4];
57 $cgiparams{'ACTION'} = '';
59 &Header
::getcgihash
(\
%cgiparams);
60 $logsettings{'LOGVIEW_REVERSE'} = 'off';
61 &General
::readhash
("${General::swroot}/logging/settings", \
%logsettings);
62 ${Header
::viewsize
} = defined ($logsettings{'LOGVIEW_VIEWSIZE'}) ?
$logsettings{'LOGVIEW_VIEWSIZE'} : 150;
64 my $start = ($logsettings{'LOGVIEW_REVERSE'} eq 'on') ?
0x7FFFF000 : 0; #index of firts line number to display
66 if ($ENV{'QUERY_STRING'} && $cgiparams{'ACTION'} ne $Lang::tr
{'update'})
68 my @temp = split(',',$ENV{'QUERY_STRING'});
70 $cgiparams{'MONTH'} = $temp[1];
71 $cgiparams{'DAY'} = $temp[2];
74 if (!($cgiparams{'MONTH'} =~ /^(0|1|2|3|4|5|6|7|8|9|10|11)$/) ||
75 !($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)$/))
77 $cgiparams{'DAY'} = $now[3];
78 $cgiparams{'MONTH'} = $now[4];
80 elsif($cgiparams{'ACTION'} eq '>>')
83 my @temp_now = localtime(time);
84 $temp_now[4] = $cgiparams{'MONTH'};
85 $temp_now[3] = $cgiparams{'DAY'};
86 if ($cgiparams{'DAY'}) {
87 @temp_then = localtime(POSIX
::mktime
(@temp_now) + 86400);
88 ## Retrieve the same time on the next day +
89 ## 86400 seconds in a day
92 $temp_now[4] = ($temp_now[4]+1) %12;
93 @temp_then = localtime(POSIX
::mktime
(@temp_now) );
96 $cgiparams{'MONTH'} = $temp_then[4];
97 $cgiparams{'DAY'} = $temp_then[3];
99 elsif($cgiparams{'ACTION'} eq '<<')
102 my @temp_now = localtime(time);
103 $temp_now[4] = $cgiparams{'MONTH'};
104 $temp_now[3] = $cgiparams{'DAY'};
105 if ($cgiparams{'DAY'}) {
106 @temp_then = localtime(POSIX
::mktime
(@temp_now) - 86400);
107 ## Retrieve the same time on the next day -
108 ## 86400 seconds in a day
111 $temp_now[4] = ($temp_now[4]-1) %12;
112 @temp_then = localtime(POSIX
::mktime
(@temp_now) );
115 $cgiparams{'MONTH'} = $temp_then[4];
116 $cgiparams{'DAY'} = $temp_then[3];
119 # Find in which file.gz is the log. Can be calculated because WEEKLY ROTATING of access.log
121 my $date = $cgiparams{'DAY'} == 0 ?
'' : $cgiparams{'DAY'} <= 9 ?
"0$cgiparams{'DAY'}" : "$cgiparams{'DAY'}";
126 # Calculate time. If future date, calculate for past year !!!
127 if (( $cgiparams{'MONTH'} eq $now[4]) && ($cgiparams{'DAY'} > $now[3]) ||
128 ( $cgiparams{'MONTH'} > $now[4] ) ) {
129 $xday = POSIX
::mktime
( 0, 0, 0, $cgiparams{'DAY'}, $cgiparams{'MONTH'}, $year - 1901 );
130 $date = "$longmonths[$cgiparams{'MONTH'}] $date, ". int($year-1);
132 $xday = POSIX
::mktime
( 0, 0, 0, $cgiparams{'DAY'}, $cgiparams{'MONTH'}, $year - 1900 );
133 $date = "$longmonths[$cgiparams{'MONTH'}] $date, $year";
136 # calculate end of active week (saturday 23H59)
138 @then = localtime(time());
139 my $sunday = POSIX
::mktime
( 0, 0, 0, @then[3], @then[4], @then[5]);
140 $sunday += (6-$then[6]) * 86400;
142 # Convert delta in second to full weeks
143 $gzindex = int (($sunday-$xday)/604800 );
146 my $monthstr = $shortmonths[$cgiparams{'MONTH'}];
147 my $daystr = $cgiparams{'DAY'} == 0 ?
'..' : $cgiparams{'DAY'} <= 9 ?
" $cgiparams{'DAY'}" : "$cgiparams{'DAY'}";
154 my $lastdatetime; # for debug
155 my $search_for_end = 0;
157 while ($gzindex >=0 && $loop) {
158 # calculate file name
160 $filestr = "/var/log/messages";
162 $filestr = "/var/log/messages.$gzindex";
163 $filestr = "$filestr.gz" if -f
"$filestr.gz";
165 # now read file if existing
166 if (open (FILE
,($filestr =~ /.gz$/ ?
"gzip -dc $filestr |" : $filestr))) {
167 #&General::log("reading $filestr");
168 READ
:while (<FILE
>) {
170 if ($line =~ /^${monthstr} ${daystr} ..:..:.. [\w\-]+ kernel:.*IN=.*$/) {
171 # when standart viewing, just keep in memory the correct slice
172 # it starts a '$start' and size is $viewport
173 # If export, then keep all lines...
174 if ($cgiparams{'ACTION'} eq $Lang::tr
{'export'}){
175 $log[$lines++] = "$line";
177 if ($lines++ < ($start + $Header::viewsize
)) {
179 if (@log > $Header::viewsize
) {
182 #} else { dont do this optimisation, need to count lines !
183 # $datetime = $maxtime; # we have read viewsize lines, stop main loop
184 # last READ; # exit read file
187 $search_for_end = 1; # we find the start of slice, can look for end now
189 if ($search_for_end == 1) {
190 #finish read files when date is over (test month equality only)
191 $line =~ /^(...) (..) ..:..:..*$/;
192 $loop = 0 if ( ($1 ne $monthstr) || ( ($daystr ne '..') && ($daystr ne $2) ) );
198 $gzindex--; # will try next gz file eg 40,39,38,.... because it may have holes when ipcop stopped
202 # $errormessage = "$Lang::tr{'date not in logs'}: $filestr $Lang::tr{'could not be opened'}";
204 if ($cgiparams{'ACTION'} eq $Lang::tr
{'export'})
206 print "Content-type: text/plain\n\n";
207 print "IPFire firewall log\r\n";
208 print "$Lang::{'date'}: $date\r\n\r\n";
210 if ($logsettings{'LOGVIEW_REVERSE'} eq 'on') { @log = reverse @log; }
214 /^... (..) (..:..:..) [\w\-]+ kernel:.*(IN=.*)$/;
217 my $time = $cgiparams{'DAY'} ?
"$2" : "$day/$2" ;
218 print "$time $3\r\n";
224 &Header
::showhttpheaders
();
226 &Header
::openpage
($Lang::tr
{'firewall log'}, 1, '');
228 &Header
::openbigbox
('100%', 'left', '', $errormessage);
231 &Header
::openbox
('100%', 'left', $Lang::tr
{'error messages'});
232 print "<font class='base'>$errormessage </font>\n";
236 &Header
::openbox
('100%', 'left', "$Lang::tr{'settings'}");
239 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
242 <td width='10%' class='base'>$Lang::tr{'month'}: </td>
244 <select name='MONTH'>
247 for (my $month = 0; $month < 12; $month++)
250 if ($month == $cgiparams{'MONTH'}) {
251 print "selected='selected' "; }
252 print "value='$month'>$longmonths[$month]</option>\n";
257 <td width='10%' class='base' align='right'> $Lang::tr{'day'}: </td>
262 print "<option value='0'>$Lang::tr{'all'}</option>\n";
263 for (my $day = 1; $day <= 31; $day++)
266 if ($day == $cgiparams{'DAY'}) {
267 print "selected='selected' "; }
268 print "value='$day'>$day</option>\n";
273 <td width='5%' align='center'><input type='submit' name='ACTION' title='$Lang::tr{'day before'}' value='<<' /></td>
274 <td width='5%' align='center'><input type='submit' name='ACTION' title='$Lang::tr{'day after'}' value='>>' /></td>
275 <td width='10%' align='center'><input type='submit' name='ACTION' value='$Lang::tr{'update'}' /></td>
276 <td width='10%' align='center'><input type='submit' name='ACTION' value='$Lang::tr{'export'}' /></td>
285 &Header
::openbox
('100%', 'left', $Lang::tr
{'log'});
286 print "<p><b>$Lang::tr{'firewall hits'} $date: $lines</b></p>";
288 my $lastPageIndex = $lines - ${Header
::viewsize
};
289 $start = $lastPageIndex if ($start >= $lastPageIndex);
290 $start = 0 if ($start < 0);
296 $prev = $start - ${Header
::viewsize
};
297 $prev = 0 if ( $prev < 0);
301 if ($start == $lastPageIndex) {
304 $next = $start + ${Header
::viewsize
};
305 $next = $lines - ${Header
::viewsize
} if ($next >= $lines - ${Header
::viewsize
});
308 if ($logsettings{'LOGVIEW_REVERSE'} eq 'on') { @log = reverse @log; }
309 if ($lines != 0) { &oldernewer
(); }
312 <table width='100%' class='tbl'>
314 <th align='center' class='boldbase'><b>$Lang::tr{'time'}</b></th>
315 <th align='center' class='boldbase'><b>$Lang::tr{'chain'}</b></th>
316 <th align='center' class='boldbase'><b>$Lang::tr{'iface'}</b></th>
317 <th align='center' class='boldbase'><b>$Lang::tr{'proto'}</b></th>
318 <th align='center' class='boldbase'><b>$Lang::tr{'source'}<br/>$Lang::tr{'destination'}</b></th>
319 <th align='center' class='boldbase'><b>$Lang::tr{'src port'}<br />$Lang::tr{'dst port'}</b></th>
320 <th align='center' class='boldbase'><b>$Lang::tr{'country'}</b></th>
321 <th align='center' class='boldbase'><b>$Lang::tr{'mac address'}</b></th>
330 /^... (..) (..:..:..) [\w\-]+ kernel:(.*)(IN=.*)$/;
333 my $time = $cgiparams{'DAY'} ?
"$2" : "$day/$2" ;
337 my ($iface, $srcaddr, $dstaddr, $macaddr, $proto, $srcport, $dstport);
338 $iface=$1 if $packet =~ /IN=(\w+)/;
339 $srcaddr=$1 if $packet =~ /SRC=([\d\.]+)/;
340 $dstaddr=$1 if $packet =~ /DST=([\d\.]+)/;
341 $macaddr=$1 if $packet =~ /MAC=([\w+\:]+)/;
342 $proto=$1 if $packet =~ /PROTO=(\w+)/;
343 $srcport=$1 if $packet =~ /SPT=(\d+)/;
344 $dstport=$1 if $packet =~ /DPT=(\d+)/;
346 my $gi = Geo
::IP
::PurePerl
->new();
347 my $ccode = $gi->country_code_by_name($srcaddr);
348 my $fcode = lc($ccode);
350 my $servi = uc(getservbyport($srcport, lc($proto)));
351 if ($servi ne '' && $srcport < 1024) {
352 $srcport = "$srcport($servi)"; }
353 $servi = uc(getservbyport($dstport, lc($proto)));
354 if ($servi ne '' && $dstport < 1024) {
355 $dstport = "$dstport($servi)";}
356 my @mactemp = split(/:/,$macaddr);
357 $macaddr = "$mactemp[6]:$mactemp[7]:$mactemp[8]:$mactemp[9]:$mactemp[10]:$mactemp[11]";
361 $col="bgcolor='$color{'color20'}'"; }
364 $col="bgcolor='$color{'color22'}'"; }
367 <td align='center' $col>$time</td>
368 <td align='center' $col>$comment</td>
369 <td align='center' $col>$iface</td>
370 <td align='center' $col>$proto</td>
371 <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>
372 <td align='center' $col>$srcport<br/>$dstport</td>
376 print "<td align='center' $col><a href='../country.cgi#$fcode'><img src='/images/flags/$fcode.png' border='0' align='absmiddle' alt='$ccode'></a></td>";}
378 print "<td align='center' $col></td>";}
380 <td align='center' $col>$macaddr</td>
393 &Header
::closebigbox
();
395 &Header
::closepage
();
405 print "<td align='center' width='50%'>";
407 print "<a href='/cgi-bin/logs.cgi/firewalllog.dat?0,$cgiparams{'MONTH'},$cgiparams{'DAY'}'>$Lang::tr{'first'}</a> ";
408 print "<a href='/cgi-bin/logs.cgi/firewalllog.dat?$prev,$cgiparams{'MONTH'},$cgiparams{'DAY'}'>$Lang::tr{'older'}</a>"; }
410 print "$Lang::tr{'first'} $Lang::tr{'older'}"; }
413 print "<td align='center' width='50%'>";
415 print "<a href='/cgi-bin/logs.cgi/firewalllog.dat?$next,$cgiparams{'MONTH'},$cgiparams{'DAY'}'>$Lang::tr{'newer'}</a> ";
416 print "<a href='/cgi-bin/logs.cgi/firewalllog.dat?$lastPageIndex,$cgiparams{'MONTH'},$cgiparams{'DAY'}'>$Lang::tr{'last'}</a>"; }
418 print "$Lang::tr{'newer'} $Lang::tr{'last'} "; }