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