]>
git.ipfire.org Git - people/teissler/ipfire-2.x.git/blob - html/cgi-bin/logs.cgi/firewalllog.dat
3857f7d8e8ed6f231805eef80b9925655e64dc01
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";
29 #workaround to suppress a warning when a variable is used only once
30 my @dummy = ( ${Header
::table2colour
} );
35 my $errormessage = '';
37 my @shortmonths = ( 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug',
38 'Sep', 'Oct', 'Nov', 'Dec' );
39 my @longmonths = ( $Lang::tr
{'january'}, $Lang::tr
{'february'}, $Lang::tr
{'march'},
40 $Lang::tr
{'april'}, $Lang::tr
{'may'}, $Lang::tr
{'june'}, $Lang::tr
{'july'}, $Lang::tr
{'august'},
41 $Lang::tr
{'september'}, $Lang::tr
{'october'}, $Lang::tr
{'november'},
42 $Lang::tr
{'december'} );
44 my @now = localtime();
48 my $year = $now[5]+1900;
50 $cgiparams{'DAY'} = $now[3];
51 $cgiparams{'MONTH'} = $now[4];
52 $cgiparams{'ACTION'} = '';
54 &Header
::getcgihash
(\
%cgiparams);
55 $logsettings{'LOGVIEW_REVERSE'} = 'off';
56 &General
::readhash
("${General::swroot}/logging/settings", \
%logsettings);
57 ${Header
::viewsize
} = defined ($logsettings{'LOGVIEW_VIEWSIZE'}) ?
$logsettings{'LOGVIEW_VIEWSIZE'} : 150;
59 my $start = ($logsettings{'LOGVIEW_REVERSE'} eq 'on') ?
0x7FFFF000 : 0; #index of firts line number to display
61 if ($ENV{'QUERY_STRING'} && $cgiparams{'ACTION'} ne $Lang::tr
{'update'})
63 my @temp = split(',',$ENV{'QUERY_STRING'});
65 $cgiparams{'MONTH'} = $temp[1];
66 $cgiparams{'DAY'} = $temp[2];
69 if (!($cgiparams{'MONTH'} =~ /^(0|1|2|3|4|5|6|7|8|9|10|11)$/) ||
70 !($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)$/))
72 $cgiparams{'DAY'} = $now[3];
73 $cgiparams{'MONTH'} = $now[4];
75 elsif($cgiparams{'ACTION'} eq '>>')
78 my @temp_now = localtime(time);
79 $temp_now[4] = $cgiparams{'MONTH'};
80 $temp_now[3] = $cgiparams{'DAY'};
81 if ($cgiparams{'DAY'}) {
82 @temp_then = localtime(POSIX
::mktime
(@temp_now) + 86400);
83 ## Retrieve the same time on the next day +
84 ## 86400 seconds in a day
87 $temp_now[4] = ($temp_now[4]+1) %12;
88 @temp_then = localtime(POSIX
::mktime
(@temp_now) );
91 $cgiparams{'MONTH'} = $temp_then[4];
92 $cgiparams{'DAY'} = $temp_then[3];
94 elsif($cgiparams{'ACTION'} eq '<<')
97 my @temp_now = localtime(time);
98 $temp_now[4] = $cgiparams{'MONTH'};
99 $temp_now[3] = $cgiparams{'DAY'};
100 if ($cgiparams{'DAY'}) {
101 @temp_then = localtime(POSIX
::mktime
(@temp_now) - 86400);
102 ## Retrieve the same time on the next day -
103 ## 86400 seconds in a day
106 $temp_now[4] = ($temp_now[4]-1) %12;
107 @temp_then = localtime(POSIX
::mktime
(@temp_now) );
110 $cgiparams{'MONTH'} = $temp_then[4];
111 $cgiparams{'DAY'} = $temp_then[3];
114 # Find in which file.gz is the log. Can be calculated because WEEKLY ROTATING of access.log
116 my $date = $cgiparams{'DAY'} == 0 ?
'' : $cgiparams{'DAY'} <= 9 ?
"0$cgiparams{'DAY'}" : "$cgiparams{'DAY'}";
121 # Calculate time. If future date, calculate for past year !!!
122 if (( $cgiparams{'MONTH'} eq $now[4]) && ($cgiparams{'DAY'} > $now[3]) ||
123 ( $cgiparams{'MONTH'} > $now[4] ) ) {
124 $xday = POSIX
::mktime
( 0, 0, 0, $cgiparams{'DAY'}, $cgiparams{'MONTH'}, $year - 1901 );
125 $date = "$longmonths[$cgiparams{'MONTH'}] $date, ". int($year-1);
127 $xday = POSIX
::mktime
( 0, 0, 0, $cgiparams{'DAY'}, $cgiparams{'MONTH'}, $year - 1900 );
128 $date = "$longmonths[$cgiparams{'MONTH'}] $date, $year";
131 # calculate end of active week (saturday 23H59)
133 @then = localtime(time());
134 my $sunday = POSIX
::mktime
( 0, 0, 0, @then[3], @then[4], @then[5]);
135 $sunday += (6-$then[6]) * 86400;
137 # Convert delta in second to full weeks
138 $gzindex = int (($sunday-$xday)/604800 );
141 my $monthstr = $shortmonths[$cgiparams{'MONTH'}];
142 my $daystr = $cgiparams{'DAY'} == 0 ?
'..' : $cgiparams{'DAY'} <= 9 ?
" $cgiparams{'DAY'}" : "$cgiparams{'DAY'}";
149 my $lastdatetime; # for debug
150 my $search_for_end = 0;
152 while ($gzindex >=0 && $loop) {
153 # calculate file name
155 $filestr = "/var/log/messages";
157 $filestr = "/var/log/messages.$gzindex";
158 $filestr = "$filestr.gz" if -f
"$filestr.gz";
160 # now read file if existing
161 if (open (FILE
,($filestr =~ /.gz$/ ?
"gzip -dc $filestr |" : $filestr))) {
162 #&General::log("reading $filestr");
163 READ
:while (<FILE
>) {
165 if ($line =~ /^${monthstr} ${daystr} ..:..:.. [\w\-]+ kernel:.*IN=.*$/) {
166 # when standart viewing, just keep in memory the correct slice
167 # it starts a '$start' and size is $viewport
168 # If export, then keep all lines...
169 if ($cgiparams{'ACTION'} eq $Lang::tr
{'export'}){
170 $log[$lines++] = "$line";
172 if ($lines++ < ($start + $Header::viewsize
)) {
174 if (@log > $Header::viewsize
) {
177 #} else { dont do this optimisation, need to count lines !
178 # $datetime = $maxtime; # we have read viewsize lines, stop main loop
179 # last READ; # exit read file
182 $search_for_end = 1; # we find the start of slice, can look for end now
184 if ($search_for_end == 1) {
185 #finish read files when date is over (test month equality only)
186 $line =~ /^(...) (..) ..:..:..*$/;
187 $loop = 0 if ( ($1 ne $monthstr) || ( ($daystr ne '..') && ($daystr ne $2) ) );
193 $gzindex--; # will try next gz file eg 40,39,38,.... because it may have holes when ipcop stopped
197 # $errormessage = "$Lang::tr{'date not in logs'}: $filestr $Lang::tr{'could not be opened'}";
199 if ($cgiparams{'ACTION'} eq $Lang::tr
{'export'})
201 print "Content-type: text/plain\n\n";
202 print "IPFire firewall log\r\n";
203 print "$Lang::{'date'}: $date\r\n\r\n";
205 if ($logsettings{'LOGVIEW_REVERSE'} eq 'on') { @log = reverse @log; }
209 /^... (..) (..:..:..) [\w\-]+ kernel:.*(IN=.*)$/;
212 my $time = $cgiparams{'DAY'} ?
"$2" : "$day/$2" ;
213 print "$time $3\r\n";
219 &Header
::showhttpheaders
();
221 &Header
::openpage
($Lang::tr
{'firewall log'}, 1, '');
223 &Header
::openbigbox
('100%', 'left', '', $errormessage);
226 &Header
::openbox
('100%', 'left', $Lang::tr
{'error messages'});
227 print "<font class='base'>$errormessage </font>\n";
231 &Header
::openbox
('100%', 'left', "$Lang::tr{'settings'}:");
234 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
237 <td width='10%' class='base'>$Lang::tr{'month'}: </td>
239 <select name='MONTH'>
242 for (my $month = 0; $month < 12; $month++)
245 if ($month == $cgiparams{'MONTH'}) {
246 print "selected='selected' "; }
247 print "value='$month'>$longmonths[$month]</option>\n";
252 <td width='10%' class='base' align='right'> $Lang::tr{'day'}: </td>
257 print "<option value='0'>$Lang::tr{'all'}</option>\n";
258 for (my $day = 1; $day <= 31; $day++)
261 if ($day == $cgiparams{'DAY'}) {
262 print "selected='selected' "; }
263 print "value='$day'>$day</option>\n";
268 <td width='5%' align='center'><input type='submit' name='ACTION' title='$Lang::tr{'day before'}' value='<<' /></td>
269 <td width='5%' align='center'><input type='submit' name='ACTION' title='$Lang::tr{'day after'}' value='>>' /></td>
270 <td width='10%' align='center'><input type='submit' name='ACTION' value='$Lang::tr{'update'}' /></td>
271 <td width='10%' align='center'><input type='submit' name='ACTION' value='$Lang::tr{'export'}' /></td>
280 &Header
::openbox
('100%', 'left', $Lang::tr
{'log'});
281 print "<p><b>$Lang::tr{'firewall hits'} $date: $lines</b></p>";
283 $start = $lines - ${Header
::viewsize
} if ($start >= $lines - ${Header
::viewsize
});
284 $start = 0 if ($start < 0);
290 $prev = $start - ${Header
::viewsize
};
291 $prev = 0 if ( $prev < 0);
295 if ($start == $lines - ${Header
::viewsize
}) {
298 $next = $start + ${Header
::viewsize
};
299 $next = $lines - ${Header
::viewsize
} if ($next >= $lines - ${Header
::viewsize
});
302 if ($logsettings{'LOGVIEW_REVERSE'} eq 'on') { @log = reverse @log; }
303 if ($lines != 0) { &oldernewer
(); }
308 <td width='10%' align='center' class='boldbase'><b>$Lang::tr{'time'}</b></td>
309 <td width='13%' align='center' class='boldbase'><b>$Lang::tr{'chain'}</b></td>
310 <td width='5%' align='center' class='boldbase'><b>$Lang::tr{'iface'}</b></td>
311 <td width='5%' align='center' class='boldbase'><b>$Lang::tr{'proto'}</b></td>
312 <td width='16%' align='center' class='boldbase'><b>$Lang::tr{'source'}</b></td>
313 <td align='center' class='boldbase'><b>Flag</b></td>
314 <td width='10%' align='center' class='boldbase'><b>$Lang::tr{'src port'}</b></td>
315 <td width='5%' align='center' class='boldbase'><b>$Lang::tr{'mac address'}</b></td>
316 <td width='16%' align='center' class='boldbase'><b>$Lang::tr{'destination'}</b></td>
317 <td width='20%' align='center' class='boldbase'><b>$Lang::tr{'dst port'}</b></td>
326 /^... (..) (..:..:..) [\w\-]+ kernel:(.*)(IN=.*)$/;
329 my $time = $cgiparams{'DAY'} ?
"$2" : "$day/$2" ;
333 $packet =~ /IN=(\w+)/; my $iface=$1;
334 $packet =~ /SRC=([\d\.]+)/; my $srcaddr=$1;
335 $packet =~ /DST=([\d\.]+)/; my $dstaddr=$1;
336 $packet =~ /MAC=([\w+\:]+)/; my $macaddr=$1;
337 $packet =~ /PROTO=(\w+)/; my $proto=$1;
338 $packet =~ /SPT=(\d+)/; my $srcport=$1;
339 $packet =~ /DPT=(\d+)/; my $dstport=$1;
341 my $gi = Geo
::IP
::PurePerl
->new();
342 my $ccode = $gi->country_code_by_name($srcaddr);
343 my $fcode = lc($ccode);
345 my $servi = uc(getservbyport($srcport, lc($proto)));
346 if ($servi ne '' && $srcport < 1024) {
347 $srcport = "$srcport($servi)"; }
348 $servi = uc(getservbyport($dstport, lc($proto)));
349 if ($servi ne '' && $dstport < 1024) {
350 $dstport = "$dstport($servi)";}
351 my @mactemp = split(/:/,$macaddr);
352 $macaddr = "$mactemp[6]:$mactemp[7]:$mactemp[8]:$mactemp[9]:$mactemp[10]:$mactemp[11]";
354 print "<tr bgcolor='${Header::table1colour}'>\n"; }
356 print "<tr bgcolor='${Header::table2colour}'>\n"; }
359 <td align='center'>$time</td>
360 <td align='center'>$comment</td>
361 <td align='center'>$iface</td>
362 <td align='center'>$proto</td>
363 <td align='center'><a href='/cgi-bin/ipinfo.cgi?ip=$srcaddr'>$srcaddr</a></td>
364 <td align='center'><a href='../country.cgi#$fcode'><img src='/images/flags/$fcode.png' border='0' align='absmiddle' alt='$ccode'></a></td>
365 <td align='center'>$srcport</td>
366 <td align='center'>$macaddr</td>
368 <table width='100%' cellpadding='0' cellspacing='0'><tr>
369 <td align='center'><a href='/cgi-bin/ipinfo.cgi?ip=$dstaddr'>$dstaddr</a></td>
372 <td align='center'>$dstport</td>
385 &Header
::closebigbox
();
387 &Header
::closepage
();
397 print "<td align='center' width='50%'>";
399 print "<a href='/cgi-bin/logs.cgi/firewalllog.dat?$prev,$cgiparams{'MONTH'},$cgiparams{'DAY'}'>$Lang::tr{'older'}</a>"; }
401 print "$Lang::tr{'older'}"; }
404 print "<td align='center' width='50%'>";
406 print "<a href='/cgi-bin/logs.cgi/firewalllog.dat?$next,$cgiparams{'MONTH'},$cgiparams{'DAY'}'>$Lang::tr{'newer'}</a>"; }
408 print "$Lang::tr{'newer'}"; }