]>
git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blob - html/cgi-bin/logs.cgi/firewalllog.dat
2 ###############################################################################
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2007-2023 IPFire Team <info@ipfire.org> #
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. #
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. #
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/>. #
20 ###############################################################################
26 # enable only the following on debugging purpose
28 #use CGI::Carp 'fatalsToBrowser';
30 require '/var/ipfire/general-functions.pl';
31 require "${General::swroot}/location-functions.pl";
32 require "${General::swroot}/lang.pl";
33 require "${General::swroot}/header.pl";
36 my %mainsettings = ();
37 &General
::readhash
("${General::swroot}/main/settings", \
%mainsettings);
38 &General
::readhash
("/srv/web/ipfire/html/themes/ipfire/include/colors.txt", \
%color);
42 #workaround to suppress a warning when a variable is used only once
43 my @dummy = ( ${Header
::table2colour
} );
48 my $errormessage = '';
50 my @shortmonths = ( 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug',
51 'Sep', 'Oct', 'Nov', 'Dec' );
52 my @longmonths = ( $Lang::tr
{'january'}, $Lang::tr
{'february'}, $Lang::tr
{'march'},
53 $Lang::tr
{'april'}, $Lang::tr
{'may'}, $Lang::tr
{'june'}, $Lang::tr
{'july'}, $Lang::tr
{'august'},
54 $Lang::tr
{'september'}, $Lang::tr
{'october'}, $Lang::tr
{'november'},
55 $Lang::tr
{'december'} );
57 my @now = localtime();
61 my $year = $now[5]+1900;
63 $cgiparams{'DAY'} = $now[3];
64 $cgiparams{'MONTH'} = $now[4];
65 $cgiparams{'ACTION'} = '';
67 &Header
::getcgihash
(\
%cgiparams);
68 $logsettings{'LOGVIEW_REVERSE'} = 'off';
69 &General
::readhash
("${General::swroot}/logging/settings", \
%logsettings);
70 ${Header
::viewsize
} = defined ($logsettings{'LOGVIEW_VIEWSIZE'}) ?
$logsettings{'LOGVIEW_VIEWSIZE'} : 150;
72 my $start = ($logsettings{'LOGVIEW_REVERSE'} eq 'on') ?
0x7FFFF000 : 0; #index of firts line number to display
74 if ($ENV{'QUERY_STRING'} && $cgiparams{'ACTION'} ne $Lang::tr
{'update'})
76 my @temp = split(',',$ENV{'QUERY_STRING'});
78 $cgiparams{'MONTH'} = $temp[1];
79 $cgiparams{'DAY'} = $temp[2];
82 if (!($cgiparams{'MONTH'} =~ /^(0|1|2|3|4|5|6|7|8|9|10|11)$/) ||
83 !($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)$/))
85 $cgiparams{'DAY'} = $now[3];
86 $cgiparams{'MONTH'} = $now[4];
88 elsif($cgiparams{'ACTION'} eq '>>')
91 my @temp_now = localtime(time);
92 $temp_now[4] = $cgiparams{'MONTH'};
93 $temp_now[3] = $cgiparams{'DAY'};
94 if ($cgiparams{'DAY'}) {
95 @temp_then = localtime(POSIX
::mktime
(@temp_now) + 86400);
96 ## Retrieve the same time on the next day +
97 ## 86400 seconds in a day
100 $temp_now[4] = ($temp_now[4]+1) %12;
101 @temp_then = localtime(POSIX
::mktime
(@temp_now) );
104 $cgiparams{'MONTH'} = $temp_then[4];
105 $cgiparams{'DAY'} = $temp_then[3];
107 elsif($cgiparams{'ACTION'} eq '<<')
110 my @temp_now = localtime(time);
111 $temp_now[4] = $cgiparams{'MONTH'};
112 $temp_now[3] = $cgiparams{'DAY'};
113 if ($cgiparams{'DAY'}) {
114 @temp_then = localtime(POSIX
::mktime
(@temp_now) - 86400);
115 ## Retrieve the same time on the next day -
116 ## 86400 seconds in a day
119 $temp_now[4] = ($temp_now[4]-1) %12;
120 @temp_then = localtime(POSIX
::mktime
(@temp_now) );
123 $cgiparams{'MONTH'} = $temp_then[4];
124 $cgiparams{'DAY'} = $temp_then[3];
127 # Find in which file.gz is the log. Can be calculated because WEEKLY ROTATING of access.log
129 my $date = $cgiparams{'DAY'} == 0 ?
'' : $cgiparams{'DAY'} <= 9 ?
"0$cgiparams{'DAY'}" : "$cgiparams{'DAY'}";
134 # Calculate time. If future date, calculate for past year !!!
135 if (( $cgiparams{'MONTH'} eq $now[4]) && ($cgiparams{'DAY'} > $now[3]) ||
136 ( $cgiparams{'MONTH'} > $now[4] ) ) {
137 $xday = POSIX
::mktime
( 0, 0, 0, $cgiparams{'DAY'}, $cgiparams{'MONTH'}, $year - 1901 );
138 $date = "$longmonths[$cgiparams{'MONTH'}] $date, ". int($year-1);
140 $xday = POSIX
::mktime
( 0, 0, 0, $cgiparams{'DAY'}, $cgiparams{'MONTH'}, $year - 1900 );
141 $date = "$longmonths[$cgiparams{'MONTH'}] $date, $year";
144 # calculate end of active week (saturday 23H59)
146 @then = localtime(time());
147 my $sunday = POSIX
::mktime
( 0, 0, 0, @then[3], @then[4], @then[5]);
148 $sunday += (6-$then[6]) * 86400;
150 # Convert delta in second to full weeks
151 $gzindex = int (($sunday-$xday)/604800 );
154 my $monthstr = $shortmonths[$cgiparams{'MONTH'}];
155 my $daystr = $cgiparams{'DAY'} == 0 ?
'..' : $cgiparams{'DAY'} <= 9 ?
" $cgiparams{'DAY'}" : "$cgiparams{'DAY'}";
162 my $lastdatetime; # for debug
163 my $search_for_end = 0;
165 while ($gzindex >=0 && $loop) {
166 # calculate file name
168 $filestr = "/var/log/messages";
170 $filestr = "/var/log/messages.$gzindex";
171 $filestr = "$filestr.gz" if -f
"$filestr.gz";
173 # now read file if existing
174 if (open (FILE
,($filestr =~ /.gz$/ ?
"gzip -dc $filestr |" : $filestr))) {
175 #&General::log("reading $filestr");
176 READ
:while (<FILE
>) {
178 if ($line =~ /^${monthstr} ${daystr} ..:..:.. [\w\-]+ kernel:.*IN=.*$/) {
179 # when standart viewing, just keep in memory the correct slice
180 # it starts a '$start' and size is $viewport
181 # If export, then keep all lines...
182 if ($cgiparams{'ACTION'} eq $Lang::tr
{'export'}){
183 $log[$lines++] = "$line";
185 if ($lines++ < ($start + $Header::viewsize
)) {
187 if (@log > $Header::viewsize
) {
190 #} else { dont do this optimisation, need to count lines !
191 # $datetime = $maxtime; # we have read viewsize lines, stop main loop
192 # last READ; # exit read file
195 $search_for_end = 1; # we find the start of slice, can look for end now
197 if ($search_for_end == 1) {
198 #finish read files when date is over (test month equality only)
199 $line =~ /^(...) (..) ..:..:..*$/;
200 $loop = 0 if ( ($1 ne $monthstr) || ( ($daystr ne '..') && ($daystr ne $2) ) );
206 $gzindex--; # will try next gz file eg 40,39,38,.... because it may have holes when ipcop stopped
210 # $errormessage = "$Lang::tr{'date not in logs'}: $filestr $Lang::tr{'could not be opened'}";
212 if ($cgiparams{'ACTION'} eq $Lang::tr
{'export'})
214 print "Content-type: text/plain\n\n";
215 print "IPFire firewall log\r\n";
216 print "$Lang::{'date'}: $date\r\n\r\n";
218 if ($logsettings{'LOGVIEW_REVERSE'} eq 'on') { @log = reverse @log; }
222 /^... (..) (..:..:..) [\w\-]+ kernel:(.*)(IN=.*)$/;
225 my $time = $cgiparams{'DAY'} ?
"$2" : "$day/$2" ;
227 print "$time $chain $4\r\n";
233 &Header
::showhttpheaders
();
235 &Header
::openpage
($Lang::tr
{'firewall log'}, 1, '');
237 &Header
::openbigbox
('100%', 'left', '', $errormessage);
240 &Header
::openbox
('100%', 'left', $Lang::tr
{'error messages'});
241 print "<font class='base'>$errormessage </font>\n";
245 &Header
::openbox
('100%', 'left', "$Lang::tr{'settings'}");
248 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
251 <td width='10%' class='base'>$Lang::tr{'month'}: </td>
253 <select name='MONTH'>
256 for (my $month = 0; $month < 12; $month++)
259 if ($month == $cgiparams{'MONTH'}) {
260 print "selected='selected' ";
262 print "value='$month'>$longmonths[$month]</option>\n";
267 <td width='10%' class='base' align='right'> $Lang::tr{'day'}: </td>
272 print "<option value='0'>$Lang::tr{'all'}</option>\n";
273 for (my $day = 1; $day <= 31; $day++)
276 if ($day == $cgiparams{'DAY'}) {
277 print "selected='selected' ";
279 print "value='$day'>$day</option>\n";
284 <td width='5%' align='center'><input type='submit' name='ACTION' title='$Lang::tr{'day before'}' value='<<' /></td>
285 <td width='5%' align='center'><input type='submit' name='ACTION' title='$Lang::tr{'day after'}' value='>>' /></td>
286 <td width='10%' align='center'><input type='submit' name='ACTION' value='$Lang::tr{'update'}' /></td>
287 <td width='10%' align='center'><input type='submit' name='ACTION' value='$Lang::tr{'export'}' /></td>
296 &Header
::openbox
('100%', 'left', $Lang::tr
{'log'});
297 print "<p><b>$Lang::tr{'firewall hits'} $date: $lines</b></p>";
299 my $lastPageIndex = $lines - ${Header
::viewsize
};
300 $start = $lastPageIndex if ($start >= $lastPageIndex);
301 $start = 0 if ($start < 0);
307 $prev = $start - ${Header
::viewsize
};
308 $prev = 0 if ( $prev < 0);
312 if ($start == $lastPageIndex) {
315 $next = $start + ${Header
::viewsize
};
316 $next = $lines - ${Header
::viewsize
} if ($next >= $lines - ${Header
::viewsize
});
319 if ($logsettings{'LOGVIEW_REVERSE'} eq 'on') { @log = reverse @log; }
320 if ($lines != 0) { &oldernewer
(); }
323 <table width='100%' class='tbl'>
325 <th align='center' class='boldbase'><b>$Lang::tr{'time'}</b></th>
326 <th align='center' class='boldbase'><b>$Lang::tr{'chain'}</b></th>
327 <th align='center' class='boldbase'><b>$Lang::tr{'iface'}</b></th>
328 <th align='center' class='boldbase'><b>$Lang::tr{'proto'}</b></th>
329 <th align='center' class='boldbase'><b>$Lang::tr{'source'}<br/>$Lang::tr{'destination'}</b></th>
330 <th align='center' class='boldbase'><b>$Lang::tr{'src port'}<br />$Lang::tr{'dst port'}</b></th>
331 <th align='center' class='boldbase'><b>$Lang::tr{'country'}</b></th>
332 <th align='center' class='boldbase'><b>$Lang::tr{'mac address'}</b></th>
337 # Generate hash to translate protocol numbers into protocol names.
338 my %protocols = &General
::generateProtoTransHash
();
343 # If ipv6 uses a bridge, PHYSIN= contains the relevant iface information
346 if ($_ =~ /^... (..) (..:..:..) [\w\-]+ kernel:(.*)(IN=.*)(PHYSIN=.*)$/) { $packet = $5; }
347 elsif ($_ =~ /^... (..) (..:..:..) [\w\-]+ kernel:(.*)(IN=.*)$/) { $packet = $4; }
350 my $time = $cgiparams{'DAY'} ?
"$2" : "$day/$2" ;
353 my ($iface, $srcaddr, $dstaddr, $macaddr, $proto, $srcport, $dstport);
354 if ($packet =~ /PHYSIN=(\w+)/) { $iface=$1; } elsif ($packet =~ /IN=(\w+)/) { $iface = $1; }
355 # Identify whether ipv4 or ipv6. Both are mutally exclusive.
356 if ($packet =~ /SRC\=(([\d]{1,3})(\.([\d]{1,3})){3})/) { $srcaddr=$1; }
357 if ($packet =~ /SRC\=(([0-9a-fA-F]{0,4})(\:([0-9a-fA-F]{0,4})){2,7})/) { $srcaddr=$1; }
358 if ($packet =~ /DST\=(([\d]{1,3})(\.([\d]{1,3})){3})/) { $dstaddr=$1; }
359 if ($packet =~ /DST\=(([0-9a-fA-F]{0,4})(\:([0-9a-fA-F]{0,4})){2,7})/) { $dstaddr=$1; }
360 $macaddr=$1 if $packet =~ /MAC=([\w+\:]+)/;
361 $proto=$1 if $packet =~ /PROTO=(\w+)/;
362 $srcport=$1 if $packet =~ /SPT=(\d+)/;
363 $dstport=$1 if $packet =~ /DPT=(\d+)/;
365 # Get the country code.
366 my $ccode = &Location
::Functions
::lookup_country_code
($srcaddr);
368 # Lookup if the grabbed protocol is part of the protocols hash.
369 if (exists ($protocols{$proto})) {
370 # Translate protocol number into protocol name.
371 $proto = $protocols{$proto};
374 my $servi = uc(getservbyport($srcport, lc($proto)));
375 if ($servi ne '' && $srcport < 1024) {
376 $srcport = "$srcport($servi)";
378 $servi = uc(getservbyport($dstport, lc($proto)));
379 if ($servi ne '' && $dstport < 1024) {
380 $dstport = "$dstport($servi)";
382 my @mactemp = split(/:/,$macaddr);
383 $macaddr = "$mactemp[6]:$mactemp[7]:$mactemp[8]:$mactemp[9]:$mactemp[10]:$mactemp[11]";
387 $col="bgcolor='$color{'color20'}'";
391 $col="bgcolor='$color{'color22'}'";
395 <td align='center' $col>$time</td>
396 <td align='center' $col>$chain</td>
397 <td align='center' $col>$iface</td>
398 <td align='center' $col>$proto</td>
399 <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>
400 <td align='center' $col>$srcport<br/>$dstport</td>
403 # Get flag icon for of the country.
404 my $flag_icon = &Location
::Functions
::get_flag_icon
($ccode);
407 print "<td align='center' $col><a href='../country.cgi#$ccode'><img src='$flag_icon' border='0' align='absmiddle' alt='$ccode'></a></td>";
409 print "<td align='center' $col></td>";
413 <td align='center' $col>$macaddr</td>
426 &Header
::closebigbox
();
428 &Header
::closepage
();
438 print "<td align='center' width='50%'>";
440 print "<a href='/cgi-bin/logs.cgi/firewalllog.dat?0,$cgiparams{'MONTH'},$cgiparams{'DAY'}'>$Lang::tr{'first'}</a> ";
441 print "<a href='/cgi-bin/logs.cgi/firewalllog.dat?$prev,$cgiparams{'MONTH'},$cgiparams{'DAY'}'>$Lang::tr{'older'}</a>";
444 print "$Lang::tr{'first'} $Lang::tr{'older'}";
448 print "<td align='center' width='50%'>";
450 print "<a href='/cgi-bin/logs.cgi/firewalllog.dat?$next,$cgiparams{'MONTH'},$cgiparams{'DAY'}'>$Lang::tr{'newer'}</a> ";
451 print "<a href='/cgi-bin/logs.cgi/firewalllog.dat?$lastPageIndex,$cgiparams{'MONTH'},$cgiparams{'DAY'}'>$Lang::tr{'last'}</a>";
454 print "$Lang::tr{'newer'} $Lang::tr{'last'} ";