]> git.ipfire.org Git - ipfire-2.x.git/blame - html/cgi-bin/logs.cgi/firewalllog.dat
Hardcode theme to ipfire
[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';
e4f1e36c 23require "${General::swroot}/location-functions.pl";
cd1a2927
MT
24require "${General::swroot}/lang.pl";
25require "${General::swroot}/header.pl";
26
f2fdd0c1
CS
27my %color = ();
28my %mainsettings = ();
29&General::readhash("${General::swroot}/main/settings", \%mainsettings);
8186b372 30&General::readhash("/srv/web/ipfire/html/themes/ipfire/include/colors.txt", \%color);
f2fdd0c1 31
cd1a2927
MT
32use POSIX();
33
34#workaround to suppress a warning when a variable is used only once
35my @dummy = ( ${Header::table2colour} );
36undef (@dummy);
37
38my %cgiparams=();
39my %logsettings=();
40my $errormessage = '';
41
42my @shortmonths = ( 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug',
43638be5 43 'Sep', 'Oct', 'Nov', 'Dec' );
cd1a2927 44my @longmonths = ( $Lang::tr{'january'}, $Lang::tr{'february'}, $Lang::tr{'march'},
43638be5
ME
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'} );
cd1a2927
MT
48
49my @now = localtime();
50my $dow = $now[6];
51my $doy = $now[7];
52my $tdoy = $now[7];
53my $year = $now[5]+1900;
54
55$cgiparams{'DAY'} = $now[3];
56$cgiparams{'MONTH'} = $now[4];
57$cgiparams{'ACTION'} = '';
58
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;
63
64my $start = ($logsettings{'LOGVIEW_REVERSE'} eq 'on') ? 0x7FFFF000 : 0; #index of firts line number to display
65
66if ($ENV{'QUERY_STRING'} && $cgiparams{'ACTION'} ne $Lang::tr{'update'})
67{
43638be5
ME
68 my @temp = split(',',$ENV{'QUERY_STRING'});
69 $start = $temp[0];
70 $cgiparams{'MONTH'} = $temp[1];
71 $cgiparams{'DAY'} = $temp[2];
cd1a2927
MT
72}
73
74if (!($cgiparams{'MONTH'} =~ /^(0|1|2|3|4|5|6|7|8|9|10|11)$/) ||
43638be5 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)$/))
cd1a2927 76{
43638be5
ME
77 $cgiparams{'DAY'} = $now[3];
78 $cgiparams{'MONTH'} = $now[4];
cd1a2927
MT
79}
80elsif($cgiparams{'ACTION'} eq '>>')
81{
43638be5
ME
82 my @temp_then=();
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
90 } else {
91 $temp_now[3] = 1;
92 $temp_now[4] = ($temp_now[4]+1) %12;
93 @temp_then = localtime(POSIX::mktime(@temp_now) );
94 $temp_then[3] = 0;
95 }
96 $cgiparams{'MONTH'} = $temp_then[4];
97 $cgiparams{'DAY'} = $temp_then[3];
cd1a2927
MT
98}
99elsif($cgiparams{'ACTION'} eq '<<')
100{
43638be5
ME
101 my @temp_then=();
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
109 } else {
110 $temp_now[3] = 1;
111 $temp_now[4] = ($temp_now[4]-1) %12;
112 @temp_then = localtime(POSIX::mktime(@temp_now) );
113 $temp_then[3] = 0;
114 }
115 $cgiparams{'MONTH'} = $temp_then[4];
116 $cgiparams{'DAY'} = $temp_then[3];
cd1a2927
MT
117}
118
119# Find in which file.gz is the log. Can be calculated because WEEKLY ROTATING of access.log
120my $gzindex;
121my $date = $cgiparams{'DAY'} == 0 ? '' : $cgiparams{'DAY'} <= 9 ? "0$cgiparams{'DAY'}" : "$cgiparams{'DAY'}";
122
123{
43638be5
ME
124 my $xday;
125
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);
131 } else {
132 $xday = POSIX::mktime( 0, 0, 0, $cgiparams{'DAY'}, $cgiparams{'MONTH'}, $year - 1900 );
133 $date = "$longmonths[$cgiparams{'MONTH'}] $date, $year";
134 }
135
136 # calculate end of active week (saturday 23H59)
137 my @then = ();
138 @then = localtime(time());
139 my $sunday = POSIX::mktime( 0, 0, 0, @then[3], @then[4], @then[5]);
140 $sunday += (6-$then[6]) * 86400;
141
142 # Convert delta in second to full weeks
143 $gzindex = int (($sunday-$xday)/604800 );
cd1a2927 144}
43638be5 145
cd1a2927
MT
146my $monthstr = $shortmonths[$cgiparams{'MONTH'}];
147my $daystr = $cgiparams{'DAY'} == 0 ? '..' : $cgiparams{'DAY'} <= 9 ? " $cgiparams{'DAY'}" : "$cgiparams{'DAY'}";
43638be5 148
cd1a2927
MT
149my $lines = 0;
150my @log=();
151
152my $loop = 1;
153my $filestr = 0;
43638be5 154my $lastdatetime; # for debug
cd1a2927 155my $search_for_end = 0;
43638be5 156
cd1a2927 157while ($gzindex >=0 && $loop) {
43638be5
ME
158 # calculate file name
159 if ($gzindex == 0) {
160 $filestr = "/var/log/messages";
161 } else {
162 $filestr = "/var/log/messages.$gzindex";
163 $filestr = "$filestr.gz" if -f "$filestr.gz";
164 }
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>) {
169 my $line = $_;
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";
176 } else {
177 if ($lines++ < ($start + $Header::viewsize)) {
178 push(@log,"$line");
179 if (@log > $Header::viewsize) {
180 shift (@log);
181 }
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
185 }
186 }
187 $search_for_end = 1; # we find the start of slice, can look for end now
188 } else {
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) ) );
193 }
194 }
195 }
196 close (FILE);
197 }
198 $gzindex--; # will try next gz file eg 40,39,38,.... because it may have holes when ipcop stopped
199 # for a long time
cd1a2927
MT
200}# while
201
202# $errormessage = "$Lang::tr{'date not in logs'}: $filestr $Lang::tr{'could not be opened'}";
203
204if ($cgiparams{'ACTION'} eq $Lang::tr{'export'})
205{
43638be5
ME
206 print "Content-type: text/plain\n\n";
207 print "IPFire firewall log\r\n";
208 print "$Lang::{'date'}: $date\r\n\r\n";
209
210 if ($logsettings{'LOGVIEW_REVERSE'} eq 'on') { @log = reverse @log; }
211
212 foreach $_ (@log)
213 {
214 /^... (..) (..:..:..) [\w\-]+ kernel:.*(IN=.*)$/;
215 my $day = $1;
216 $day =~ tr / /0/;
217 my $time = $cgiparams{'DAY'} ? "$2" : "$day/$2" ;
218 print "$time $3\r\n";
219
220 }
221 exit 0;
cd1a2927
MT
222}
223
224&Header::showhttpheaders();
225
226&Header::openpage($Lang::tr{'firewall log'}, 1, '');
227
228&Header::openbigbox('100%', 'left', '', $errormessage);
229
230if ($errormessage) {
43638be5
ME
231 &Header::openbox('100%', 'left', $Lang::tr{'error messages'});
232 print "<font class='base'>$errormessage&nbsp;</font>\n";
233 &Header::closebox();
cd1a2927
MT
234}
235
73ede29f 236&Header::openbox('100%', 'left', "$Lang::tr{'settings'}");
cd1a2927
MT
237
238print <<END
239<form method='post' action='$ENV{'SCRIPT_NAME'}'>
240<table width='100%'>
241<tr>
43638be5
ME
242 <td width='10%' class='base'>$Lang::tr{'month'}:&nbsp;</td>
243 <td width='10%'>
244 <select name='MONTH'>
cd1a2927
MT
245END
246;
247for (my $month = 0; $month < 12; $month++)
248{
43638be5
ME
249 print "\t<option ";
250 if ($month == $cgiparams{'MONTH'}) {
251 print "selected='selected' ";
252 }
253 print "value='$month'>$longmonths[$month]</option>\n";
cd1a2927
MT
254}
255print <<END
43638be5
ME
256 </select>
257 </td>
258 <td width='10%' class='base' align='right'>&nbsp;$Lang::tr{'day'}:&nbsp;</td>
259 <td width='40%'>
260 <select name='DAY'>
cd1a2927
MT
261END
262;
263print "<option value='0'>$Lang::tr{'all'}</option>\n";
264for (my $day = 1; $day <= 31; $day++)
265{
43638be5
ME
266 print "\t<option ";
267 if ($day == $cgiparams{'DAY'}) {
268 print "selected='selected' ";
269 }
270 print "value='$day'>$day</option>\n";
cd1a2927
MT
271}
272print <<END
273</select>
274</td>
275<td width='5%' align='center'><input type='submit' name='ACTION' title='$Lang::tr{'day before'}' value='&lt;&lt;' /></td>
276<td width='5%' align='center'><input type='submit' name='ACTION' title='$Lang::tr{'day after'}' value='&gt;&gt;' /></td>
277<td width='10%' align='center'><input type='submit' name='ACTION' value='$Lang::tr{'update'}' /></td>
278<td width='10%' align='center'><input type='submit' name='ACTION' value='$Lang::tr{'export'}' /></td>
279</tr>
280</table>
281</form>
282END
283;
284
285&Header::closebox();
286
287&Header::openbox('100%', 'left', $Lang::tr{'log'});
288print "<p><b>$Lang::tr{'firewall hits'} $date: $lines</b></p>";
289
f69446d2
AH
290my $lastPageIndex = $lines - ${Header::viewsize};
291$start = $lastPageIndex if ($start >= $lastPageIndex);
cd1a2927
MT
292$start = 0 if ($start < 0);
293
294my $prev;
43638be5
ME
295if ($start == 0) {
296 $prev = -1;
297} else {
298 $prev = $start - ${Header::viewsize};
299 $prev = 0 if ( $prev < 0);
300}
301
cd1a2927 302my $next;
43638be5
ME
303if ($start == $lastPageIndex) {
304 $next = -1;
305} else {
306 $next = $start + ${Header::viewsize};
307 $next = $lines - ${Header::viewsize} if ($next >= $lines - ${Header::viewsize});
308}
cd1a2927
MT
309
310if ($logsettings{'LOGVIEW_REVERSE'} eq 'on') { @log = reverse @log; }
311if ($lines != 0) { &oldernewer(); }
312
313print <<END
fda1f52f 314<table width='100%' class='tbl'>
cd1a2927 315<tr>
43638be5
ME
316 <th align='center' class='boldbase'><b>$Lang::tr{'time'}</b></th>
317 <th align='center' class='boldbase'><b>$Lang::tr{'chain'}</b></th>
318 <th align='center' class='boldbase'><b>$Lang::tr{'iface'}</b></th>
319 <th align='center' class='boldbase'><b>$Lang::tr{'proto'}</b></th>
320 <th align='center' class='boldbase'><b>$Lang::tr{'source'}<br/>$Lang::tr{'destination'}</b></th>
321 <th align='center' class='boldbase'><b>$Lang::tr{'src port'}<br />$Lang::tr{'dst port'}</b></th>
322 <th align='center' class='boldbase'><b>$Lang::tr{'country'}</b></th>
323 <th align='center' class='boldbase'><b>$Lang::tr{'mac address'}</b></th>
cd1a2927
MT
324</tr>
325END
326;
327
328
329$lines = 0;
330foreach $_ (@log)
331{
9877d28d
ME
332 # If ipv6 uses a bridge, PHYSIN= contains the relevant iface information
333 # otherwise use IN=
43638be5
ME
334 my $packet = '';
335 if ($_ =~ /^... (..) (..:..:..) [\w\-]+ kernel:(.*)(IN=.*)(PHYSIN=.*)$/) { $packet = $5; }
336 elsif ($_ =~ /^... (..) (..:..:..) [\w\-]+ kernel:(.*)(IN=.*)$/) { $packet = $4; }
337 my $day = $1;
338 $day =~ tr / /0/;
339 my $time = $cgiparams{'DAY'} ? "$2" : "$day/$2" ;
340 my $chain = $3;
341
342 my ($iface, $srcaddr, $dstaddr, $macaddr, $proto, $srcport, $dstport);
343 if ($packet =~ /PHYSIN=(\w+)/) { $iface=$1; } elsif ($packet =~ /IN=(\w+)/) { $iface = $1; }
344 # Identify whether ipv4 or ipv6. Both are mutally exclusive.
345 if ($packet =~ /SRC\=(([\d]{1,3})(\.([\d]{1,3})){3})/) { $srcaddr=$1; }
346 if ($packet =~ /SRC\=(([0-9a-fA-F]{0,4})(\:([0-9a-fA-F]{0,4})){2,7})/) { $srcaddr=$1; }
347 if ($packet =~ /DST\=(([\d]{1,3})(\.([\d]{1,3})){3})/) { $dstaddr=$1; }
348 if ($packet =~ /DST\=(([0-9a-fA-F]{0,4})(\:([0-9a-fA-F]{0,4})){2,7})/) { $dstaddr=$1; }
349 $macaddr=$1 if $packet =~ /MAC=([\w+\:]+)/;
350 $proto=$1 if $packet =~ /PROTO=(\w+)/;
351 $srcport=$1 if $packet =~ /SPT=(\d+)/;
352 $dstport=$1 if $packet =~ /DPT=(\d+)/;
353
07e42be9
SS
354 # Get the country code.
355 my $ccode = &Location::Functions::lookup_country_code($srcaddr);
43638be5
ME
356
357 my $servi = uc(getservbyport($srcport, lc($proto)));
358 if ($servi ne '' && $srcport < 1024) {
359 $srcport = "$srcport($servi)";
360 }
361 $servi = uc(getservbyport($dstport, lc($proto)));
362 if ($servi ne '' && $dstport < 1024) {
363 $dstport = "$dstport($servi)";
364 }
365 my @mactemp = split(/:/,$macaddr);
366 $macaddr = "$mactemp[6]:$mactemp[7]:$mactemp[8]:$mactemp[9]:$mactemp[10]:$mactemp[11]";
367 my $col="";
368 if ($lines % 2) {
369 print "<tr>";
370 $col="bgcolor='$color{'color20'}'";
371 }
372 else {
373 print "<tr>";
374 $col="bgcolor='$color{'color22'}'";
375 }
376 print <<END
377
378 <td align='center' $col>$time</td>
379 <td align='center' $col>$chain</td>
380 <td align='center' $col>$iface</td>
381 <td align='center' $col>$proto</td>
382 <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>
383 <td align='center' $col>$srcport<br/>$dstport</td>
8c384f97 384END
43638be5 385 ;
3ea01fe0 386 # Get flag icon for of the country.
e4f1e36c 387 my $flag_icon = &Location::Functions::get_flag_icon($ccode);
3ea01fe0 388
43638be5 389 if ( $flag_icon) {
00793c27 390 print "<td align='center' $col><a href='../country.cgi#$ccode'><img src='$flag_icon' border='0' align='absmiddle' alt='$ccode'></a></td>";
3ea01fe0
SS
391 } else {
392 print "<td align='center' $col></td>";
393 }
394
43638be5
ME
395 print <<END
396 <td align='center' $col>$macaddr</td>
397 </tr>
cd1a2927 398END
43638be5
ME
399 ;
400 $lines++;
cd1a2927
MT
401}
402
403print "</table>";
404
405&oldernewer();
406
407&Header::closebox();
408
409&Header::closebigbox();
410
411&Header::closepage();
412
413sub oldernewer
414{
43638be5
ME
415 print <<END
416 <table width='100%'>
417 <tr>
cd1a2927 418END
43638be5 419 ;
cd1a2927 420
43638be5
ME
421 print "<td align='center' width='50%'>";
422 if ($prev != -1) {
423 print "<a href='/cgi-bin/logs.cgi/firewalllog.dat?0,$cgiparams{'MONTH'},$cgiparams{'DAY'}'>$Lang::tr{'first'}</a> ";
424 print "<a href='/cgi-bin/logs.cgi/firewalllog.dat?$prev,$cgiparams{'MONTH'},$cgiparams{'DAY'}'>$Lang::tr{'older'}</a>";
425 }
426 else {
427 print "$Lang::tr{'first'} $Lang::tr{'older'}";
428 }
429 print "</td>\n";
cd1a2927 430
43638be5
ME
431 print "<td align='center' width='50%'>";
432 if ($next >= 0) {
433 print "<a href='/cgi-bin/logs.cgi/firewalllog.dat?$next,$cgiparams{'MONTH'},$cgiparams{'DAY'}'>$Lang::tr{'newer'}</a> ";
434 print "<a href='/cgi-bin/logs.cgi/firewalllog.dat?$lastPageIndex,$cgiparams{'MONTH'},$cgiparams{'DAY'}'>$Lang::tr{'last'}</a>";
435 }
436 else {
437 print "$Lang::tr{'newer'} $Lang::tr{'last'} ";
438 }
439 print "</td>\n";
440
441 print <<END
442 </tr>
443 </table>
cd1a2927 444END
43638be5 445 ;
cd1a2927 446}
71dfc4b7 447