"Update Booster" fertiggestellt und getestet.
[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
12# - added source mac adapter to layout
13#
14use strict;
15
dace10b5
MT
16use Geo::IP::PurePerl;
17use Getopt::Std;
18
cd1a2927
MT
19# enable only the following on debugging purpose
20#use warnings;
21#use CGI::Carp 'fatalsToBrowser';
22
986e08d9 23require '/var/ipfire/general-functions.pl';
cd1a2927
MT
24require "${General::swroot}/lang.pl";
25require "${General::swroot}/header.pl";
26
27use POSIX();
28
29#workaround to suppress a warning when a variable is used only once
30my @dummy = ( ${Header::table2colour} );
31undef (@dummy);
32
33my %cgiparams=();
34my %logsettings=();
35my $errormessage = '';
36
37my @shortmonths = ( 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug',
38 'Sep', 'Oct', 'Nov', 'Dec' );
39my @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'} );
43
44my @now = localtime();
45my $dow = $now[6];
46my $doy = $now[7];
47my $tdoy = $now[7];
48my $year = $now[5]+1900;
49
50$cgiparams{'DAY'} = $now[3];
51$cgiparams{'MONTH'} = $now[4];
52$cgiparams{'ACTION'} = '';
53
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;
58
59my $start = ($logsettings{'LOGVIEW_REVERSE'} eq 'on') ? 0x7FFFF000 : 0; #index of firts line number to display
60
61if ($ENV{'QUERY_STRING'} && $cgiparams{'ACTION'} ne $Lang::tr{'update'})
62{
63 my @temp = split(',',$ENV{'QUERY_STRING'});
64 $start = $temp[0];
65 $cgiparams{'MONTH'} = $temp[1];
66 $cgiparams{'DAY'} = $temp[2];
67}
68
69if (!($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)$/))
71{
72 $cgiparams{'DAY'} = $now[3];
73 $cgiparams{'MONTH'} = $now[4];
74}
75elsif($cgiparams{'ACTION'} eq '>>')
76{
77 my @temp_then=();
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
85 } else {
86 $temp_now[3] = 1;
87 $temp_now[4] = ($temp_now[4]+1) %12;
88 @temp_then = localtime(POSIX::mktime(@temp_now) );
89 $temp_then[3] = 0;
90 }
91 $cgiparams{'MONTH'} = $temp_then[4];
92 $cgiparams{'DAY'} = $temp_then[3];
93}
94elsif($cgiparams{'ACTION'} eq '<<')
95{
96 my @temp_then=();
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
104 } else {
105 $temp_now[3] = 1;
106 $temp_now[4] = ($temp_now[4]-1) %12;
107 @temp_then = localtime(POSIX::mktime(@temp_now) );
108 $temp_then[3] = 0;
109 }
110 $cgiparams{'MONTH'} = $temp_then[4];
111 $cgiparams{'DAY'} = $temp_then[3];
112}
113
114# Find in which file.gz is the log. Can be calculated because WEEKLY ROTATING of access.log
115my $gzindex;
116my $date = $cgiparams{'DAY'} == 0 ? '' : $cgiparams{'DAY'} <= 9 ? "0$cgiparams{'DAY'}" : "$cgiparams{'DAY'}";
117
118{
119 my $xday;
120
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);
126 } else {
127 $xday = POSIX::mktime( 0, 0, 0, $cgiparams{'DAY'}, $cgiparams{'MONTH'}, $year - 1900 );
128 $date = "$longmonths[$cgiparams{'MONTH'}] $date, $year";
129 }
130
131 # calculate end of active week (saturday 23H59)
132 my @then = ();
133 @then = localtime(time());
134 my $sunday = POSIX::mktime( 0, 0, 0, @then[3], @then[4], @then[5]);
135 $sunday += (6-$then[6]) * 86400;
136
137 # Convert delta in second to full weeks
138 $gzindex = int (($sunday-$xday)/604800 );
139}
140
141my $monthstr = $shortmonths[$cgiparams{'MONTH'}];
142my $daystr = $cgiparams{'DAY'} == 0 ? '..' : $cgiparams{'DAY'} <= 9 ? " $cgiparams{'DAY'}" : "$cgiparams{'DAY'}";
143
144my $lines = 0;
145my @log=();
146
147my $loop = 1;
148my $filestr = 0;
149my $lastdatetime; # for debug
150my $search_for_end = 0;
151
152while ($gzindex >=0 && $loop) {
153 # calculate file name
154 if ($gzindex == 0) {
155 $filestr = "/var/log/messages";
156 } else {
157 $filestr = "/var/log/messages.$gzindex";
158 $filestr = "$filestr.gz" if -f "$filestr.gz";
159 }
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>) {
164 my $line = $_;
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";
171 } else {
172 if ($lines++ < ($start + $Header::viewsize)) {
173 push(@log,"$line");
174 if (@log > $Header::viewsize) {
175 shift (@log);
176 }
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
180 }
181 }
182 $search_for_end = 1; # we find the start of slice, can look for end now
183 } else {
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) ) );
188 }
189 }
190 }
191 close (FILE);
192 }
193 $gzindex--; # will try next gz file eg 40,39,38,.... because it may have holes when ipcop stopped
194 # for a long time
195}# while
196
197# $errormessage = "$Lang::tr{'date not in logs'}: $filestr $Lang::tr{'could not be opened'}";
198
199if ($cgiparams{'ACTION'} eq $Lang::tr{'export'})
200{
201 print "Content-type: text/plain\n\n";
78331e30 202 print "IPFire firewall log\r\n";
cd1a2927
MT
203 print "$Lang::{'date'}: $date\r\n\r\n";
204
205 if ($logsettings{'LOGVIEW_REVERSE'} eq 'on') { @log = reverse @log; }
206
207 foreach $_ (@log)
208 {
209 /^... (..) (..:..:..) [\w\-]+ kernel:.*(IN=.*)$/;
210 my $day = $1;
211 $day =~ tr / /0/;
212 my $time = $cgiparams{'DAY'} ? "$2" : "$day/$2" ;
213 print "$time $3\r\n";
214
215 }
216 exit 0;
217}
218
219&Header::showhttpheaders();
220
221&Header::openpage($Lang::tr{'firewall log'}, 1, '');
222
223&Header::openbigbox('100%', 'left', '', $errormessage);
224
225if ($errormessage) {
226 &Header::openbox('100%', 'left', $Lang::tr{'error messages'});
227 print "<font class='base'>$errormessage&nbsp;</font>\n";
228 &Header::closebox();
229}
230
231&Header::openbox('100%', 'left', "$Lang::tr{'settings'}:");
232
233print <<END
234<form method='post' action='$ENV{'SCRIPT_NAME'}'>
235<table width='100%'>
236<tr>
237 <td width='10%' class='base'>$Lang::tr{'month'}:&nbsp;</td>
238 <td width='10%'>
239 <select name='MONTH'>
240END
241;
242for (my $month = 0; $month < 12; $month++)
243{
244 print "\t<option ";
245 if ($month == $cgiparams{'MONTH'}) {
246 print "selected='selected' "; }
247 print "value='$month'>$longmonths[$month]</option>\n";
248}
249print <<END
250 </select>
251 </td>
252 <td width='10%' class='base' align='right'>&nbsp;$Lang::tr{'day'}:&nbsp;</td>
253 <td width='40%'>
254 <select name='DAY'>
255END
256;
257print "<option value='0'>$Lang::tr{'all'}</option>\n";
258for (my $day = 1; $day <= 31; $day++)
259{
260 print "\t<option ";
261 if ($day == $cgiparams{'DAY'}) {
262 print "selected='selected' "; }
263 print "value='$day'>$day</option>\n";
264}
265print <<END
266</select>
267</td>
268<td width='5%' align='center'><input type='submit' name='ACTION' title='$Lang::tr{'day before'}' value='&lt;&lt;' /></td>
269<td width='5%' align='center'><input type='submit' name='ACTION' title='$Lang::tr{'day after'}' value='&gt;&gt;' /></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>
272</tr>
273</table>
274</form>
275END
276;
277
278&Header::closebox();
279
280&Header::openbox('100%', 'left', $Lang::tr{'log'});
281print "<p><b>$Lang::tr{'firewall hits'} $date: $lines</b></p>";
282
283$start = $lines - ${Header::viewsize} if ($start >= $lines - ${Header::viewsize});
284$start = 0 if ($start < 0);
285
286my $prev;
287 if ($start == 0) {
288 $prev = -1;
289 } else {
290 $prev = $start - ${Header::viewsize};
291 $prev = 0 if ( $prev < 0);
292 }
293
294my $next;
295 if ($start == $lines - ${Header::viewsize}) {
296 $next = -1;
297 } else {
298 $next = $start + ${Header::viewsize};
299 $next = $lines - ${Header::viewsize} if ($next >= $lines - ${Header::viewsize});
300 }
301
302if ($logsettings{'LOGVIEW_REVERSE'} eq 'on') { @log = reverse @log; }
303if ($lines != 0) { &oldernewer(); }
304
305print <<END
306<table width='100%'>
307<tr>
9fb25b1c
MT
308 <td align='center' class='boldbase'><b>$Lang::tr{'time'}</b></td>
309 <td align='center' class='boldbase'><b>$Lang::tr{'chain'}</b></td>
310 <td align='center' class='boldbase'><b>$Lang::tr{'iface'}</b></td>
311 <td align='center' class='boldbase'><b>$Lang::tr{'proto'}</b></td>
312 <td align='center' class='boldbase'><b>$Lang::tr{'source'}<br/>$Lang::tr{'destination'}</b></td>
313 <td align='center' class='boldbase'><b>$Lang::tr{'src port'}<br />$Lang::tr{'dst port'}</b></td>
dace10b5 314 <td align='center' class='boldbase'><b>Flag</b></td>
9fb25b1c 315 <td align='center' class='boldbase'><b>$Lang::tr{'mac address'}</b></td>
cd1a2927
MT
316</tr>
317END
318;
319
320
321$lines = 0;
322foreach $_ (@log)
323{
324 /^... (..) (..:..:..) [\w\-]+ kernel:(.*)(IN=.*)$/;
325 my $day = $1;
326 $day =~ tr / /0/;
327 my $time = $cgiparams{'DAY'} ? "$2" : "$day/$2" ;
328 my $comment = $3;
329 my $packet = $4;
330
cd1a2927
MT
331 $packet =~ /IN=(\w+)/; my $iface=$1;
332 $packet =~ /SRC=([\d\.]+)/; my $srcaddr=$1;
333 $packet =~ /DST=([\d\.]+)/; my $dstaddr=$1;
334 $packet =~ /MAC=([\w+\:]+)/; my $macaddr=$1;
335 $packet =~ /PROTO=(\w+)/; my $proto=$1;
336 $packet =~ /SPT=(\d+)/; my $srcport=$1;
337 $packet =~ /DPT=(\d+)/; my $dstport=$1;
dace10b5
MT
338
339 my $gi = Geo::IP::PurePerl->new();
340 my $ccode = $gi->country_code_by_name($srcaddr);
341 my $fcode = lc($ccode);
342
cd1a2927
MT
343 my $servi = uc(getservbyport($srcport, lc($proto)));
344 if ($servi ne '' && $srcport < 1024) {
345 $srcport = "$srcport($servi)"; }
346 $servi = uc(getservbyport($dstport, lc($proto)));
347 if ($servi ne '' && $dstport < 1024) {
348 $dstport = "$dstport($servi)";}
349 my @mactemp = split(/:/,$macaddr);
350 $macaddr = "$mactemp[6]:$mactemp[7]:$mactemp[8]:$mactemp[9]:$mactemp[10]:$mactemp[11]";
351 if ($lines % 2) {
352 print "<tr bgcolor='${Header::table1colour}'>\n"; }
353 else {
354 print "<tr bgcolor='${Header::table2colour}'>\n"; }
355 print <<END
356
357 <td align='center'>$time</td>
358 <td align='center'>$comment</td>
359 <td align='center'>$iface</td>
360 <td align='center'>$proto</td>
9fb25b1c
MT
361 <td align='center'><a href='/cgi-bin/ipinfo.cgi?ip=$srcaddr'>$srcaddr</a><br /><a href='/cgi-bin/ipinfo.cgi?ip=$dstaddr'>$dstaddr</a></td>
362 <td align='center'>$srcport<br/>$dstport</td>
dace10b5 363 <td align='center'><a href='../country.cgi#$fcode'><img src='/images/flags/$fcode.png' border='0' align='absmiddle' alt='$ccode'></a></td>
cd1a2927 364 <td align='center'>$macaddr</td>
cd1a2927
MT
365</tr>
366END
367 ;
368 $lines++;
369}
370
371print "</table>";
372
373&oldernewer();
374
375&Header::closebox();
376
377&Header::closebigbox();
378
379&Header::closepage();
380
381sub oldernewer
382{
383print <<END
384<table width='100%'>
385<tr>
386END
387;
388
389print "<td align='center' width='50%'>";
390if ($prev != -1) {
391 print "<a href='/cgi-bin/logs.cgi/firewalllog.dat?$prev,$cgiparams{'MONTH'},$cgiparams{'DAY'}'>$Lang::tr{'older'}</a>"; }
392else {
393 print "$Lang::tr{'older'}"; }
394print "</td>\n";
395
396print "<td align='center' width='50%'>";
397if ($next >= 0) {
398 print "<a href='/cgi-bin/logs.cgi/firewalllog.dat?$next,$cgiparams{'MONTH'},$cgiparams{'DAY'}'>$Lang::tr{'newer'}</a>"; }
399else {
400 print "$Lang::tr{'newer'}"; }
401print "</td>\n";
402
403print <<END
404</tr>
405</table>
406END
407;
408}