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