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