]> git.ipfire.org Git - ipfire-2.x.git/blob - html/cgi-bin/logs.cgi/firewalllog.dat
Merge branch 'next' of ssh://git.ipfire.org/pub/git/ipfire-2.x into next-switch-to...
[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 Getopt::Std;
17
18 # enable only the following on debugging purpose
19 #use warnings;
20 #use CGI::Carp 'fatalsToBrowser';
21
22 require '/var/ipfire/general-functions.pl';
23 require "${General::swroot}/geoip-functions.pl";
24 require "${General::swroot}/lang.pl";
25 require "${General::swroot}/header.pl";
26
27 # Libloc database handle.
28 my $libloc_db_handle = &GeoIP::init();
29
30 my %color = ();
31 my %mainsettings = ();
32 &General::readhash("${General::swroot}/main/settings", \%mainsettings);
33 &General::readhash("/srv/web/ipfire/html/themes/".$mainsettings{'THEME'}."/include/colors.txt", \%color);
34
35 use POSIX();
36
37 #workaround to suppress a warning when a variable is used only once
38 my @dummy = ( ${Header::table2colour} );
39 undef (@dummy);
40
41 my %cgiparams=();
42 my %logsettings=();
43 my $errormessage = '';
44
45 my @shortmonths = ( 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug',
46 'Sep', 'Oct', 'Nov', 'Dec' );
47 my @longmonths = ( $Lang::tr{'january'}, $Lang::tr{'february'}, $Lang::tr{'march'},
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'} );
51
52 my @now = localtime();
53 my $dow = $now[6];
54 my $doy = $now[7];
55 my $tdoy = $now[7];
56 my $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
67 my $start = ($logsettings{'LOGVIEW_REVERSE'} eq 'on') ? 0x7FFFF000 : 0; #index of firts line number to display
68
69 if ($ENV{'QUERY_STRING'} && $cgiparams{'ACTION'} ne $Lang::tr{'update'})
70 {
71 my @temp = split(',',$ENV{'QUERY_STRING'});
72 $start = $temp[0];
73 $cgiparams{'MONTH'} = $temp[1];
74 $cgiparams{'DAY'} = $temp[2];
75 }
76
77 if (!($cgiparams{'MONTH'} =~ /^(0|1|2|3|4|5|6|7|8|9|10|11)$/) ||
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)$/))
79 {
80 $cgiparams{'DAY'} = $now[3];
81 $cgiparams{'MONTH'} = $now[4];
82 }
83 elsif($cgiparams{'ACTION'} eq '>>')
84 {
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];
101 }
102 elsif($cgiparams{'ACTION'} eq '<<')
103 {
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];
120 }
121
122 # Find in which file.gz is the log. Can be calculated because WEEKLY ROTATING of access.log
123 my $gzindex;
124 my $date = $cgiparams{'DAY'} == 0 ? '' : $cgiparams{'DAY'} <= 9 ? "0$cgiparams{'DAY'}" : "$cgiparams{'DAY'}";
125
126 {
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 );
147 }
148
149 my $monthstr = $shortmonths[$cgiparams{'MONTH'}];
150 my $daystr = $cgiparams{'DAY'} == 0 ? '..' : $cgiparams{'DAY'} <= 9 ? " $cgiparams{'DAY'}" : "$cgiparams{'DAY'}";
151
152 my $lines = 0;
153 my @log=();
154
155 my $loop = 1;
156 my $filestr = 0;
157 my $lastdatetime; # for debug
158 my $search_for_end = 0;
159
160 while ($gzindex >=0 && $loop) {
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
203 }# while
204
205 # $errormessage = "$Lang::tr{'date not in logs'}: $filestr $Lang::tr{'could not be opened'}";
206
207 if ($cgiparams{'ACTION'} eq $Lang::tr{'export'})
208 {
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;
225 }
226
227 &Header::showhttpheaders();
228
229 &Header::openpage($Lang::tr{'firewall log'}, 1, '');
230
231 &Header::openbigbox('100%', 'left', '', $errormessage);
232
233 if ($errormessage) {
234 &Header::openbox('100%', 'left', $Lang::tr{'error messages'});
235 print "<font class='base'>$errormessage&nbsp;</font>\n";
236 &Header::closebox();
237 }
238
239 &Header::openbox('100%', 'left', "$Lang::tr{'settings'}");
240
241 print <<END
242 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
243 <table width='100%'>
244 <tr>
245 <td width='10%' class='base'>$Lang::tr{'month'}:&nbsp;</td>
246 <td width='10%'>
247 <select name='MONTH'>
248 END
249 ;
250 for (my $month = 0; $month < 12; $month++)
251 {
252 print "\t<option ";
253 if ($month == $cgiparams{'MONTH'}) {
254 print "selected='selected' ";
255 }
256 print "value='$month'>$longmonths[$month]</option>\n";
257 }
258 print <<END
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'>
264 END
265 ;
266 print "<option value='0'>$Lang::tr{'all'}</option>\n";
267 for (my $day = 1; $day <= 31; $day++)
268 {
269 print "\t<option ";
270 if ($day == $cgiparams{'DAY'}) {
271 print "selected='selected' ";
272 }
273 print "value='$day'>$day</option>\n";
274 }
275 print <<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>
285 END
286 ;
287
288 &Header::closebox();
289
290 &Header::openbox('100%', 'left', $Lang::tr{'log'});
291 print "<p><b>$Lang::tr{'firewall hits'} $date: $lines</b></p>";
292
293 my $lastPageIndex = $lines - ${Header::viewsize};
294 $start = $lastPageIndex if ($start >= $lastPageIndex);
295 $start = 0 if ($start < 0);
296
297 my $prev;
298 if ($start == 0) {
299 $prev = -1;
300 } else {
301 $prev = $start - ${Header::viewsize};
302 $prev = 0 if ( $prev < 0);
303 }
304
305 my $next;
306 if ($start == $lastPageIndex) {
307 $next = -1;
308 } else {
309 $next = $start + ${Header::viewsize};
310 $next = $lines - ${Header::viewsize} if ($next >= $lines - ${Header::viewsize});
311 }
312
313 if ($logsettings{'LOGVIEW_REVERSE'} eq 'on') { @log = reverse @log; }
314 if ($lines != 0) { &oldernewer(); }
315
316 print <<END
317 <table width='100%' class='tbl'>
318 <tr>
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>
327 </tr>
328 END
329 ;
330
331
332 $lines = 0;
333 foreach $_ (@log)
334 {
335 # If ipv6 uses a bridge, PHYSIN= contains the relevant iface information
336 # otherwise use IN=
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
357 my $ccode = &GeoIP::lookup_country_code($libloc_db_handle, $srcaddr);
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>
386 END
387 ;
388 # Get flag icon for of the country.
389 my $flag_icon = &GeoIP::get_flag_icon($ccode);
390
391 if ( $flag_icon) {
392 print "<td align='center' $col><a href='../country.cgi#$ccode'><img src='$flag_icon' border='0' align='absmiddle' alt='$ccode'></a></td>";
393 } else {
394 print "<td align='center' $col></td>";
395 }
396
397 print <<END
398 <td align='center' $col>$macaddr</td>
399 </tr>
400 END
401 ;
402 $lines++;
403 }
404
405 print "</table>";
406
407 &oldernewer();
408
409 &Header::closebox();
410
411 &Header::closebigbox();
412
413 &Header::closepage();
414
415 sub oldernewer
416 {
417 print <<END
418 <table width='100%'>
419 <tr>
420 END
421 ;
422
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";
432
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>
446 END
447 ;
448 }
449