]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blame - html/cgi-bin/logs.cgi/firewalllog.dat
Merge branch 'switch-to-libloc' into next-switch-to-libloc
[people/pmueller/ipfire-2.x.git] / html / cgi-bin / logs.cgi / firewalllog.dat
CommitLineData
cd1a2927
MT
1#!/usr/bin/perl
2#
3# SmoothWall CGIs
4#
5# This code is distributed under the terms of the GPL
6#
7# (c) The SmoothWall Team
8#
9# $Id: firewalllog.dat,v 1.4.2.18 2005/08/23 12:01:50 eoberlander Exp $
10#
11# July 28, 2003 - Darren Critchley - darren@kdi.ca
71dfc4b7 12# - added source mac adapter to layout
cd1a2927
MT
13#
14use strict;
15
dace10b5
MT
16use Getopt::Std;
17
cd1a2927
MT
18# enable only the following on debugging purpose
19#use warnings;
20#use CGI::Carp 'fatalsToBrowser';
21
986e08d9 22require '/var/ipfire/general-functions.pl';
3ea01fe0 23require "${General::swroot}/geoip-functions.pl";
cd1a2927
MT
24require "${General::swroot}/lang.pl";
25require "${General::swroot}/header.pl";
26
50494dfd
SS
27# Libloc database handle.
28my $libloc_db_handle = &GeoIP::init();
29
f2fdd0c1
CS
30my %color = ();
31my %mainsettings = ();
32&General::readhash("${General::swroot}/main/settings", \%mainsettings);
33&General::readhash("/srv/web/ipfire/html/themes/".$mainsettings{'THEME'}."/include/colors.txt", \%color);
34
cd1a2927
MT
35use POSIX();
36
37#workaround to suppress a warning when a variable is used only once
38my @dummy = ( ${Header::table2colour} );
39undef (@dummy);
40
41my %cgiparams=();
42my %logsettings=();
43my $errormessage = '';
44
45my @shortmonths = ( 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug',
43638be5 46 'Sep', 'Oct', 'Nov', 'Dec' );
cd1a2927 47my @longmonths = ( $Lang::tr{'january'}, $Lang::tr{'february'}, $Lang::tr{'march'},
43638be5
ME
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'} );
cd1a2927
MT
51
52my @now = localtime();
53my $dow = $now[6];
54my $doy = $now[7];
55my $tdoy = $now[7];
56my $year = $now[5]+1900;
57
58$cgiparams{'DAY'} = $now[3];
59$cgiparams{'MONTH'} = $now[4];
60$cgiparams{'ACTION'} = '';
61
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;
66
67my $start = ($logsettings{'LOGVIEW_REVERSE'} eq 'on') ? 0x7FFFF000 : 0; #index of firts line number to display
68
69if ($ENV{'QUERY_STRING'} && $cgiparams{'ACTION'} ne $Lang::tr{'update'})
70{
43638be5
ME
71 my @temp = split(',',$ENV{'QUERY_STRING'});
72 $start = $temp[0];
73 $cgiparams{'MONTH'} = $temp[1];
74 $cgiparams{'DAY'} = $temp[2];
cd1a2927
MT
75}
76
77if (!($cgiparams{'MONTH'} =~ /^(0|1|2|3|4|5|6|7|8|9|10|11)$/) ||
43638be5 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)$/))
cd1a2927 79{
43638be5
ME
80 $cgiparams{'DAY'} = $now[3];
81 $cgiparams{'MONTH'} = $now[4];
cd1a2927
MT
82}
83elsif($cgiparams{'ACTION'} eq '>>')
84{
43638be5
ME
85 my @temp_then=();
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
93 } else {
94 $temp_now[3] = 1;
95 $temp_now[4] = ($temp_now[4]+1) %12;
96 @temp_then = localtime(POSIX::mktime(@temp_now) );
97 $temp_then[3] = 0;
98 }
99 $cgiparams{'MONTH'} = $temp_then[4];
100 $cgiparams{'DAY'} = $temp_then[3];
cd1a2927
MT
101}
102elsif($cgiparams{'ACTION'} eq '<<')
103{
43638be5
ME
104 my @temp_then=();
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
112 } else {
113 $temp_now[3] = 1;
114 $temp_now[4] = ($temp_now[4]-1) %12;
115 @temp_then = localtime(POSIX::mktime(@temp_now) );
116 $temp_then[3] = 0;
117 }
118 $cgiparams{'MONTH'} = $temp_then[4];
119 $cgiparams{'DAY'} = $temp_then[3];
cd1a2927
MT
120}
121
122# Find in which file.gz is the log. Can be calculated because WEEKLY ROTATING of access.log
123my $gzindex;
124my $date = $cgiparams{'DAY'} == 0 ? '' : $cgiparams{'DAY'} <= 9 ? "0$cgiparams{'DAY'}" : "$cgiparams{'DAY'}";
125
126{
43638be5
ME
127 my $xday;
128
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);
134 } else {
135 $xday = POSIX::mktime( 0, 0, 0, $cgiparams{'DAY'}, $cgiparams{'MONTH'}, $year - 1900 );
136 $date = "$longmonths[$cgiparams{'MONTH'}] $date, $year";
137 }
138
139 # calculate end of active week (saturday 23H59)
140 my @then = ();
141 @then = localtime(time());
142 my $sunday = POSIX::mktime( 0, 0, 0, @then[3], @then[4], @then[5]);
143 $sunday += (6-$then[6]) * 86400;
144
145 # Convert delta in second to full weeks
146 $gzindex = int (($sunday-$xday)/604800 );
cd1a2927 147}
43638be5 148
cd1a2927
MT
149my $monthstr = $shortmonths[$cgiparams{'MONTH'}];
150my $daystr = $cgiparams{'DAY'} == 0 ? '..' : $cgiparams{'DAY'} <= 9 ? " $cgiparams{'DAY'}" : "$cgiparams{'DAY'}";
43638be5 151
cd1a2927
MT
152my $lines = 0;
153my @log=();
154
155my $loop = 1;
156my $filestr = 0;
43638be5 157my $lastdatetime; # for debug
cd1a2927 158my $search_for_end = 0;
43638be5 159
cd1a2927 160while ($gzindex >=0 && $loop) {
43638be5
ME
161 # calculate file name
162 if ($gzindex == 0) {
163 $filestr = "/var/log/messages";
164 } else {
165 $filestr = "/var/log/messages.$gzindex";
166 $filestr = "$filestr.gz" if -f "$filestr.gz";
167 }
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>) {
172 my $line = $_;
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";
179 } else {
180 if ($lines++ < ($start + $Header::viewsize)) {
181 push(@log,"$line");
182 if (@log > $Header::viewsize) {
183 shift (@log);
184 }
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
188 }
189 }
190 $search_for_end = 1; # we find the start of slice, can look for end now
191 } else {
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) ) );
196 }
197 }
198 }
199 close (FILE);
200 }
201 $gzindex--; # will try next gz file eg 40,39,38,.... because it may have holes when ipcop stopped
202 # for a long time
cd1a2927
MT
203}# while
204
205# $errormessage = "$Lang::tr{'date not in logs'}: $filestr $Lang::tr{'could not be opened'}";
206
207if ($cgiparams{'ACTION'} eq $Lang::tr{'export'})
208{
43638be5
ME
209 print "Content-type: text/plain\n\n";
210 print "IPFire firewall log\r\n";
211 print "$Lang::{'date'}: $date\r\n\r\n";
212
213 if ($logsettings{'LOGVIEW_REVERSE'} eq 'on') { @log = reverse @log; }
214
215 foreach $_ (@log)
216 {
217 /^... (..) (..:..:..) [\w\-]+ kernel:.*(IN=.*)$/;
218 my $day = $1;
219 $day =~ tr / /0/;
220 my $time = $cgiparams{'DAY'} ? "$2" : "$day/$2" ;
221 print "$time $3\r\n";
222
223 }
224 exit 0;
cd1a2927
MT
225}
226
227&Header::showhttpheaders();
228
229&Header::openpage($Lang::tr{'firewall log'}, 1, '');
230
231&Header::openbigbox('100%', 'left', '', $errormessage);
232
233if ($errormessage) {
43638be5
ME
234 &Header::openbox('100%', 'left', $Lang::tr{'error messages'});
235 print "<font class='base'>$errormessage&nbsp;</font>\n";
236 &Header::closebox();
cd1a2927
MT
237}
238
73ede29f 239&Header::openbox('100%', 'left', "$Lang::tr{'settings'}");
cd1a2927
MT
240
241print <<END
242<form method='post' action='$ENV{'SCRIPT_NAME'}'>
243<table width='100%'>
244<tr>
43638be5
ME
245 <td width='10%' class='base'>$Lang::tr{'month'}:&nbsp;</td>
246 <td width='10%'>
247 <select name='MONTH'>
cd1a2927
MT
248END
249;
250for (my $month = 0; $month < 12; $month++)
251{
43638be5
ME
252 print "\t<option ";
253 if ($month == $cgiparams{'MONTH'}) {
254 print "selected='selected' ";
255 }
256 print "value='$month'>$longmonths[$month]</option>\n";
cd1a2927
MT
257}
258print <<END
43638be5
ME
259 </select>
260 </td>
261 <td width='10%' class='base' align='right'>&nbsp;$Lang::tr{'day'}:&nbsp;</td>
262 <td width='40%'>
263 <select name='DAY'>
cd1a2927
MT
264END
265;
266print "<option value='0'>$Lang::tr{'all'}</option>\n";
267for (my $day = 1; $day <= 31; $day++)
268{
43638be5
ME
269 print "\t<option ";
270 if ($day == $cgiparams{'DAY'}) {
271 print "selected='selected' ";
272 }
273 print "value='$day'>$day</option>\n";
cd1a2927
MT
274}
275print <<END
276</select>
277</td>
278<td width='5%' align='center'><input type='submit' name='ACTION' title='$Lang::tr{'day before'}' value='&lt;&lt;' /></td>
279<td width='5%' align='center'><input type='submit' name='ACTION' title='$Lang::tr{'day after'}' value='&gt;&gt;' /></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>
282</tr>
283</table>
284</form>
285END
286;
287
288&Header::closebox();
289
290&Header::openbox('100%', 'left', $Lang::tr{'log'});
291print "<p><b>$Lang::tr{'firewall hits'} $date: $lines</b></p>";
292
f69446d2
AH
293my $lastPageIndex = $lines - ${Header::viewsize};
294$start = $lastPageIndex if ($start >= $lastPageIndex);
cd1a2927
MT
295$start = 0 if ($start < 0);
296
297my $prev;
43638be5
ME
298if ($start == 0) {
299 $prev = -1;
300} else {
301 $prev = $start - ${Header::viewsize};
302 $prev = 0 if ( $prev < 0);
303}
304
cd1a2927 305my $next;
43638be5
ME
306if ($start == $lastPageIndex) {
307 $next = -1;
308} else {
309 $next = $start + ${Header::viewsize};
310 $next = $lines - ${Header::viewsize} if ($next >= $lines - ${Header::viewsize});
311}
cd1a2927
MT
312
313if ($logsettings{'LOGVIEW_REVERSE'} eq 'on') { @log = reverse @log; }
314if ($lines != 0) { &oldernewer(); }
315
316print <<END
fda1f52f 317<table width='100%' class='tbl'>
cd1a2927 318<tr>
43638be5
ME
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>
cd1a2927
MT
327</tr>
328END
329;
330
331
332$lines = 0;
333foreach $_ (@log)
334{
9877d28d
ME
335 # If ipv6 uses a bridge, PHYSIN= contains the relevant iface information
336 # otherwise use IN=
43638be5
ME
337 my $packet = '';
338 if ($_ =~ /^... (..) (..:..:..) [\w\-]+ kernel:(.*)(IN=.*)(PHYSIN=.*)$/) { $packet = $5; }
339 elsif ($_ =~ /^... (..) (..:..:..) [\w\-]+ kernel:(.*)(IN=.*)$/) { $packet = $4; }
340 my $day = $1;
341 $day =~ tr / /0/;
342 my $time = $cgiparams{'DAY'} ? "$2" : "$day/$2" ;
343 my $chain = $3;
344
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+)/;
356
50494dfd 357 my $ccode = &GeoIP::lookup_country_code($libloc_db_handle, $srcaddr);
43638be5
ME
358
359 my $servi = uc(getservbyport($srcport, lc($proto)));
360 if ($servi ne '' && $srcport < 1024) {
361 $srcport = "$srcport($servi)";
362 }
363 $servi = uc(getservbyport($dstport, lc($proto)));
364 if ($servi ne '' && $dstport < 1024) {
365 $dstport = "$dstport($servi)";
366 }
367 my @mactemp = split(/:/,$macaddr);
368 $macaddr = "$mactemp[6]:$mactemp[7]:$mactemp[8]:$mactemp[9]:$mactemp[10]:$mactemp[11]";
369 my $col="";
370 if ($lines % 2) {
371 print "<tr>";
372 $col="bgcolor='$color{'color20'}'";
373 }
374 else {
375 print "<tr>";
376 $col="bgcolor='$color{'color22'}'";
377 }
378 print <<END
379
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>
8c384f97 386END
43638be5 387 ;
3ea01fe0 388 # Get flag icon for of the country.
00793c27 389 my $flag_icon = &GeoIP::get_flag_icon($ccode);
3ea01fe0 390
43638be5 391 if ( $flag_icon) {
00793c27 392 print "<td align='center' $col><a href='../country.cgi#$ccode'><img src='$flag_icon' border='0' align='absmiddle' alt='$ccode'></a></td>";
3ea01fe0
SS
393 } else {
394 print "<td align='center' $col></td>";
395 }
396
43638be5
ME
397 print <<END
398 <td align='center' $col>$macaddr</td>
399 </tr>
cd1a2927 400END
43638be5
ME
401 ;
402 $lines++;
cd1a2927
MT
403}
404
405print "</table>";
406
407&oldernewer();
408
409&Header::closebox();
410
411&Header::closebigbox();
412
413&Header::closepage();
414
415sub oldernewer
416{
43638be5
ME
417 print <<END
418 <table width='100%'>
419 <tr>
cd1a2927 420END
43638be5 421 ;
cd1a2927 422
43638be5
ME
423 print "<td align='center' width='50%'>";
424 if ($prev != -1) {
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>";
427 }
428 else {
429 print "$Lang::tr{'first'} $Lang::tr{'older'}";
430 }
431 print "</td>\n";
cd1a2927 432
43638be5
ME
433 print "<td align='center' width='50%'>";
434 if ($next >= 0) {
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>";
437 }
438 else {
439 print "$Lang::tr{'newer'} $Lang::tr{'last'} ";
440 }
441 print "</td>\n";
442
443 print <<END
444 </tr>
445 </table>
cd1a2927 446END
43638be5 447 ;
cd1a2927 448}
71dfc4b7 449