]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blob - html/cgi-bin/logs.cgi/firewalllog.dat
Merge remote-tracking branch 'origin/master' into next
[people/pmueller/ipfire-2.x.git] / html / cgi-bin / logs.cgi / firewalllog.dat
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 #
14 use strict;
15
16 use Geo::IP::PurePerl;
17 use Getopt::Std;
18
19 # enable only the following on debugging purpose
20 #use warnings;
21 #use CGI::Carp 'fatalsToBrowser';
22
23 require '/var/ipfire/general-functions.pl';
24 require "${General::swroot}/lang.pl";
25 require "${General::swroot}/header.pl";
26
27 my %color = ();
28 my %mainsettings = ();
29 &General::readhash("${General::swroot}/main/settings", \%mainsettings);
30 &General::readhash("/srv/web/ipfire/html/themes/".$mainsettings{'THEME'}."/include/colors.txt", \%color);
31
32 use POSIX();
33
34 #workaround to suppress a warning when a variable is used only once
35 my @dummy = ( ${Header::table2colour} );
36 undef (@dummy);
37
38 my %cgiparams=();
39 my %logsettings=();
40 my $errormessage = '';
41
42 my @shortmonths = ( 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug',
43 'Sep', 'Oct', 'Nov', 'Dec' );
44 my @longmonths = ( $Lang::tr{'january'}, $Lang::tr{'february'}, $Lang::tr{'march'},
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'} );
48
49 my @now = localtime();
50 my $dow = $now[6];
51 my $doy = $now[7];
52 my $tdoy = $now[7];
53 my $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
64 my $start = ($logsettings{'LOGVIEW_REVERSE'} eq 'on') ? 0x7FFFF000 : 0; #index of firts line number to display
65
66 if ($ENV{'QUERY_STRING'} && $cgiparams{'ACTION'} ne $Lang::tr{'update'})
67 {
68 my @temp = split(',',$ENV{'QUERY_STRING'});
69 $start = $temp[0];
70 $cgiparams{'MONTH'} = $temp[1];
71 $cgiparams{'DAY'} = $temp[2];
72 }
73
74 if (!($cgiparams{'MONTH'} =~ /^(0|1|2|3|4|5|6|7|8|9|10|11)$/) ||
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)$/))
76 {
77 $cgiparams{'DAY'} = $now[3];
78 $cgiparams{'MONTH'} = $now[4];
79 }
80 elsif($cgiparams{'ACTION'} eq '>>')
81 {
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];
98 }
99 elsif($cgiparams{'ACTION'} eq '<<')
100 {
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];
117 }
118
119 # Find in which file.gz is the log. Can be calculated because WEEKLY ROTATING of access.log
120 my $gzindex;
121 my $date = $cgiparams{'DAY'} == 0 ? '' : $cgiparams{'DAY'} <= 9 ? "0$cgiparams{'DAY'}" : "$cgiparams{'DAY'}";
122
123 {
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 );
144 }
145
146 my $monthstr = $shortmonths[$cgiparams{'MONTH'}];
147 my $daystr = $cgiparams{'DAY'} == 0 ? '..' : $cgiparams{'DAY'} <= 9 ? " $cgiparams{'DAY'}" : "$cgiparams{'DAY'}";
148
149 my $lines = 0;
150 my @log=();
151
152 my $loop = 1;
153 my $filestr = 0;
154 my $lastdatetime; # for debug
155 my $search_for_end = 0;
156
157 while ($gzindex >=0 && $loop) {
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
200 }# while
201
202 # $errormessage = "$Lang::tr{'date not in logs'}: $filestr $Lang::tr{'could not be opened'}";
203
204 if ($cgiparams{'ACTION'} eq $Lang::tr{'export'})
205 {
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;
222 }
223
224 &Header::showhttpheaders();
225
226 &Header::openpage($Lang::tr{'firewall log'}, 1, '');
227
228 &Header::openbigbox('100%', 'left', '', $errormessage);
229
230 if ($errormessage) {
231 &Header::openbox('100%', 'left', $Lang::tr{'error messages'});
232 print "<font class='base'>$errormessage&nbsp;</font>\n";
233 &Header::closebox();
234 }
235
236 &Header::openbox('100%', 'left', "$Lang::tr{'settings'}");
237
238 print <<END
239 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
240 <table width='100%'>
241 <tr>
242 <td width='10%' class='base'>$Lang::tr{'month'}:&nbsp;</td>
243 <td width='10%'>
244 <select name='MONTH'>
245 END
246 ;
247 for (my $month = 0; $month < 12; $month++)
248 {
249 print "\t<option ";
250 if ($month == $cgiparams{'MONTH'}) {
251 print "selected='selected' "; }
252 print "value='$month'>$longmonths[$month]</option>\n";
253 }
254 print <<END
255 </select>
256 </td>
257 <td width='10%' class='base' align='right'>&nbsp;$Lang::tr{'day'}:&nbsp;</td>
258 <td width='40%'>
259 <select name='DAY'>
260 END
261 ;
262 print "<option value='0'>$Lang::tr{'all'}</option>\n";
263 for (my $day = 1; $day <= 31; $day++)
264 {
265 print "\t<option ";
266 if ($day == $cgiparams{'DAY'}) {
267 print "selected='selected' "; }
268 print "value='$day'>$day</option>\n";
269 }
270 print <<END
271 </select>
272 </td>
273 <td width='5%' align='center'><input type='submit' name='ACTION' title='$Lang::tr{'day before'}' value='&lt;&lt;' /></td>
274 <td width='5%' align='center'><input type='submit' name='ACTION' title='$Lang::tr{'day after'}' value='&gt;&gt;' /></td>
275 <td width='10%' align='center'><input type='submit' name='ACTION' value='$Lang::tr{'update'}' /></td>
276 <td width='10%' align='center'><input type='submit' name='ACTION' value='$Lang::tr{'export'}' /></td>
277 </tr>
278 </table>
279 </form>
280 END
281 ;
282
283 &Header::closebox();
284
285 &Header::openbox('100%', 'left', $Lang::tr{'log'});
286 print "<p><b>$Lang::tr{'firewall hits'} $date: $lines</b></p>";
287
288 my $lastPageIndex = $lines - ${Header::viewsize};
289 $start = $lastPageIndex if ($start >= $lastPageIndex);
290 $start = 0 if ($start < 0);
291
292 my $prev;
293 if ($start == 0) {
294 $prev = -1;
295 } else {
296 $prev = $start - ${Header::viewsize};
297 $prev = 0 if ( $prev < 0);
298 }
299
300 my $next;
301 if ($start == $lastPageIndex) {
302 $next = -1;
303 } else {
304 $next = $start + ${Header::viewsize};
305 $next = $lines - ${Header::viewsize} if ($next >= $lines - ${Header::viewsize});
306 }
307
308 if ($logsettings{'LOGVIEW_REVERSE'} eq 'on') { @log = reverse @log; }
309 if ($lines != 0) { &oldernewer(); }
310
311 print <<END
312 <table width='100%' class='tbl'>
313 <tr>
314 <th align='center' class='boldbase'><b>$Lang::tr{'time'}</b></th>
315 <th align='center' class='boldbase'><b>$Lang::tr{'chain'}</b></th>
316 <th align='center' class='boldbase'><b>$Lang::tr{'iface'}</b></th>
317 <th align='center' class='boldbase'><b>$Lang::tr{'proto'}</b></th>
318 <th align='center' class='boldbase'><b>$Lang::tr{'source'}<br/>$Lang::tr{'destination'}</b></th>
319 <th align='center' class='boldbase'><b>$Lang::tr{'src port'}<br />$Lang::tr{'dst port'}</b></th>
320 <th align='center' class='boldbase'><b>$Lang::tr{'country'}</b></th>
321 <th align='center' class='boldbase'><b>$Lang::tr{'mac address'}</b></th>
322 </tr>
323 END
324 ;
325
326
327 $lines = 0;
328 foreach $_ (@log)
329 {
330 /^... (..) (..:..:..) [\w\-]+ kernel:(.*)(IN=.*)$/;
331 my $day = $1;
332 $day =~ tr / /0/;
333 my $time = $cgiparams{'DAY'} ? "$2" : "$day/$2" ;
334 my $comment = $3;
335 my $packet = $4;
336
337 my ($iface, $srcaddr, $dstaddr, $macaddr, $proto, $srcport, $dstport);
338 $iface=$1 if $packet =~ /IN=(\w+)/;
339 $srcaddr=$1 if $packet =~ /SRC=([\d\.]+)/;
340 $dstaddr=$1 if $packet =~ /DST=([\d\.]+)/;
341 $macaddr=$1 if $packet =~ /MAC=([\w+\:]+)/;
342 $proto=$1 if $packet =~ /PROTO=(\w+)/;
343 $srcport=$1 if $packet =~ /SPT=(\d+)/;
344 $dstport=$1 if $packet =~ /DPT=(\d+)/;
345
346 my $gi = Geo::IP::PurePerl->new();
347 my $ccode = $gi->country_code_by_name($srcaddr);
348 my $fcode = lc($ccode);
349
350 my $servi = uc(getservbyport($srcport, lc($proto)));
351 if ($servi ne '' && $srcport < 1024) {
352 $srcport = "$srcport($servi)"; }
353 $servi = uc(getservbyport($dstport, lc($proto)));
354 if ($servi ne '' && $dstport < 1024) {
355 $dstport = "$dstport($servi)";}
356 my @mactemp = split(/:/,$macaddr);
357 $macaddr = "$mactemp[6]:$mactemp[7]:$mactemp[8]:$mactemp[9]:$mactemp[10]:$mactemp[11]";
358 my $col="";
359 if ($lines % 2) {
360 print "<tr>";
361 $col="bgcolor='$color{'color20'}'"; }
362 else {
363 print "<tr>";
364 $col="bgcolor='$color{'color22'}'"; }
365 print <<END
366
367 <td align='center' $col>$time</td>
368 <td align='center' $col>$comment</td>
369 <td align='center' $col>$iface</td>
370 <td align='center' $col>$proto</td>
371 <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>
372 <td align='center' $col>$srcport<br/>$dstport</td>
373 END
374 ;
375 if ( $fcode ne "" ){
376 print "<td align='center' $col><a href='../country.cgi#$fcode'><img src='/images/flags/$fcode.png' border='0' align='absmiddle' alt='$ccode'></a></td>";}
377 else {
378 print "<td align='center' $col></td>";}
379 print <<END
380 <td align='center' $col>$macaddr</td>
381 </tr>
382 END
383 ;
384 $lines++;
385 }
386
387 print "</table>";
388
389 &oldernewer();
390
391 &Header::closebox();
392
393 &Header::closebigbox();
394
395 &Header::closepage();
396
397 sub oldernewer
398 {
399 print <<END
400 <table width='100%'>
401 <tr>
402 END
403 ;
404
405 print "<td align='center' width='50%'>";
406 if ($prev != -1) {
407 print "<a href='/cgi-bin/logs.cgi/firewalllog.dat?0,$cgiparams{'MONTH'},$cgiparams{'DAY'}'>$Lang::tr{'first'}</a> ";
408 print "<a href='/cgi-bin/logs.cgi/firewalllog.dat?$prev,$cgiparams{'MONTH'},$cgiparams{'DAY'}'>$Lang::tr{'older'}</a>"; }
409 else {
410 print "$Lang::tr{'first'} $Lang::tr{'older'}"; }
411 print "</td>\n";
412
413 print "<td align='center' width='50%'>";
414 if ($next >= 0) {
415 print "<a href='/cgi-bin/logs.cgi/firewalllog.dat?$next,$cgiparams{'MONTH'},$cgiparams{'DAY'}'>$Lang::tr{'newer'}</a> ";
416 print "<a href='/cgi-bin/logs.cgi/firewalllog.dat?$lastPageIndex,$cgiparams{'MONTH'},$cgiparams{'DAY'}'>$Lang::tr{'last'}</a>"; }
417 else {
418 print "$Lang::tr{'newer'} $Lang::tr{'last'} "; }
419 print "</td>\n";
420
421 print <<END
422 </tr>
423 </table>
424 END
425 ;
426 }
427