]> git.ipfire.org Git - ipfire-2.x.git/blame - html/cgi-bin/logs.cgi/showrequestfromcountry.dat
firewalllogcountry.dat: Show green0,blue0, and orange0 as countries
[ipfire-2.x.git] / html / cgi-bin / logs.cgi / showrequestfromcountry.dat
CommitLineData
e4aac473
AH
1#!/usr/bin/perl
2# SmoothWall CGIs
3#
4# This code is distributed under the terms of the GPL
5#
6# JC HERITIER
7# page inspired from the initial firewalllog.dat
8#
9# Modified for IPFire by Christian Schmidt (www.ipfire.org)
10
11# enable only the following on debugging purpose
12#use warnings;
13#use CGI::Carp 'fatalsToBrowser';
14
15#use strict;
16use Geo::IP::PurePerl;
17
18require '/var/ipfire/general-functions.pl';
19require "${General::swroot}/lang.pl";
20require "${General::swroot}/header.pl";
21
22use POSIX();
23
24#workaround to suppress a warning when a variable is used only once
25my @dummy = ( ${Header::table2colour} );
26undef (@dummy);
27
28my %cgiparams=();
29my %logsettings=();
30my $errormessage = '';
31
32my @shortmonths = ( 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug',
33 'Sep', 'Oct', 'Nov', 'Dec' );
34my @longmonths = ( $Lang::tr{'january'}, $Lang::tr{'february'}, $Lang::tr{'march'},
35 $Lang::tr{'april'}, $Lang::tr{'may'}, $Lang::tr{'june'}, $Lang::tr{'july'}, $Lang::tr{'august'},
36 $Lang::tr{'september'}, $Lang::tr{'october'}, $Lang::tr{'november'},
37 $Lang::tr{'december'} );
38
39my @now = localtime();
40my $dow = $now[6];
41my $doy = $now[7];
42my $tdoy = $now[7];
43my $year = $now[5]+1900;
44
45$cgiparams{'DAY'} = $now[3];
46$cgiparams{'MONTH'} = $now[4];
47$cgiparams{'ACTION'} = '';
48
49&Header::getcgihash(\%cgiparams);
50
51$logsettings{'LOGVIEW_REVERSE'} = 'off';
52&General::readhash("${General::swroot}/logging/settings", \%logsettings);
53
54my $start = -1;
55if ($ENV{'QUERY_STRING'} && $cgiparams{'ACTION'} ne $Lang::tr{'update'})
56{
57 my @temp = split(',',$ENV{'QUERY_STRING'});
58 $start = $temp[0];
59 $cgiparams{'MONTH'} = $temp[1];
60 $cgiparams{'DAY'} = $temp[2];
bde7a7d2 61 $cgiparams{country} = $temp[3];
e4aac473
AH
62}
63
64if (!($cgiparams{'MONTH'} =~ /^(0|1|2|3|4|5|6|7|8|9|10|11)$/) ||
65 !($cgiparams{'DAY'} =~ /^(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)$/))
66{
67 $cgiparams{'DAY'} = $now[3];
68 $cgiparams{'MONTH'} = $now[4];
69}
70elsif($cgiparams{'ACTION'} eq '>>')
71{
72 my @temp_then=();
73 my @temp_now = localtime(time);
74 $temp_now[4] = $cgiparams{'MONTH'};
75 $temp_now[3] = $cgiparams{'DAY'};
76 @temp_then = localtime(POSIX::mktime(@temp_now) + 86400);
77 ## Retrieve the same time on the next day -
78 ## 86400 seconds in a day
79 $cgiparams{'MONTH'} = $temp_then[4];
80 $cgiparams{'DAY'} = $temp_then[3];
81}
82elsif($cgiparams{'ACTION'} eq '<<')
83{
84 my @temp_then=();
85 my @temp_now = localtime(time);
86 $temp_now[4] = $cgiparams{'MONTH'};
87 $temp_now[3] = $cgiparams{'DAY'};
88 @temp_then = localtime(POSIX::mktime(@temp_now) - 86400);
89 ## Retrieve the same time on the previous day -
90 ## 86400 seconds in a day
91 $cgiparams{'MONTH'} = $temp_then[4];
92 $cgiparams{'DAY'} = $temp_then[3];
93}
94
95if (($cgiparams{'DAY'} ne $now[3]) || ($cgiparams{'MONTH'} ne $now[4]))
96{
97 my @then = ();
98 if ( ( $cgiparams{'MONTH'} eq $now[4]) && ($cgiparams{'DAY'} > $now[3]) ||
99 ( $cgiparams{'MONTH'} > $now[4] ) ) {
100 @then = localtime(POSIX::mktime( 0, 0, 0, $cgiparams{'DAY'}, $cgiparams{'MONTH'}, $year - 1901 ));
101 } else {
102 @then = localtime(POSIX::mktime( 0, 0, 0, $cgiparams{'DAY'}, $cgiparams{'MONTH'}, $year - 1900 ));
103 }
104 $tdoy = $then[7];
105 my $lastleap=($year-1)%4;
106 if ($tdoy>$doy) {
107 if ($lastleap == 0 && $tdoy < 60) {
108 $doy=$tdoy+366;
109 } else {
110 $doy=$doy+365;
111 }
112 }
113}
114my $datediff=0;
115my $dowd=0;
116my $multifile=0;
117if ($tdoy ne $doy) {
118 $datediff=int(($doy-$tdoy)/7);
119 $dowd=($doy-$tdoy)%7;
120 if (($dow-$dowd)<1) {
121 $datediff=$datediff+1;
122 }
123 if (($dow-$dowd)==0) {
124 $multifile=1;
125 }
126}
127
128my $monthstr = $shortmonths[$cgiparams{'MONTH'}];
129my $longmonthstr = $longmonths[$cgiparams{'MONTH'}];
130my $day = $cgiparams{'DAY'};
131my $daystr='';
132if ($day <= 9) {
133 $daystr = " $day"; }
134else {
135 $daystr = $day;
136}
137
138my $skip=0;
139my $filestr='';
140if ($datediff==0) {
141 $filestr="/var/log/messages";
142} else {
143 $filestr="/var/log/messages.$datediff";
144 $filestr = "$filestr.gz" if -f "$filestr.gz";
145}
146
147if (!(open (FILE,($filestr =~ /.gz$/ ? "gzip -dc $filestr |" : $filestr)))) {
148 $errormessage = "$Lang::tr{'date not in logs'}: $filestr $Lang::tr{'could not be opened'}";
149 $skip=1;
150 # Note: This is in case the log does not exist for that date
151}
152my $lines = 0;
153my @log=();
154my $country = $cgiparams{country};
155my $gi = Geo::IP::PurePerl->new();
156
157if (!$skip)
158{
bde7a7d2
AH
159 while (<FILE>)
160 {
e4aac473 161 if (/(^${monthstr} ${daystr} ..:..:..) [\w\-]+ kernel:.*(IN=.*)$/) {
fde47f5a
AH
162 my $packet = $2;
163 $packet =~ /IN=(\w+)/; my $iface=$1; if ( $1 =~ /2./ ){ $iface="";}
164 $packet =~ /SRC=([\d\.]+)/; my $srcaddr=$1;
165
166 if($iface eq $country) {
167 $log[$lines] = $_;
168 $lines++;
169 }
170 elsif($srcaddr ne '') {
e4aac473
AH
171 my $ccode = $gi->country_code_by_name($srcaddr);
172 if($ccode eq $country){
173 $log[$lines] = $_;
174 $lines++;
175 }
176 }
177 }
178 }
179 close (FILE);
180}
181
182$skip=0;
183if ($multifile) {
184 $datediff=$datediff-1;
185 if ($datediff==0) {
186 $filestr="/var/log/messages";
187 } else {
188 $filestr="/var/log/messages.$datediff";
189 $filestr = "$filestr.gz" if -f "$filestr.gz";
190 }
191 if (!(open (FILE,($filestr =~ /.gz$/ ? "gzip -dc $filestr |" : $filestr)))) {
192 $errormessage="$Lang::tr{'date not in logs'}: $filestr $Lang::tr{'could not be opened'}";
193 $skip=1;
194 }
195 if (!$skip) {
196 while (<FILE>) {
197 if (/(^${monthstr} ${daystr} ..:..:..) [\w\-]+ kernel:.*(IN=.*)$/) {
198 if($_ =~ /SRC\=([\d\.]+)/){
199 my $srcaddr=$1;
200 my $ccode = $gi->country_code_by_name($srcaddr);
201 if($ccode eq $country){
202 $log[$lines] = $_;
203 $lines++;
204 }
205 }
206 }
207 }
208 close (FILE);
209 }
210}
211
212&Header::showhttpheaders();
213&Header::openpage($Lang::tr{'firewall log'}, 1, '');
214&Header::openbigbox('100%', 'left', '', $errormessage);
215
216if ($errormessage) {
217 &Header::openbox('100%', 'left', $Lang::tr{'error messages'});
218 print "<font class='base'>$errormessage&nbsp;</font>\n";
219 &Header::closebox();
220}
221
222&Header::openbox('100%', 'left', "$Lang::tr{'settings'}:");
223
224print <<END
225<form method='post' action='$ENV{'SCRIPT_NAME'}'>
226<table width='100%'>
227<tr>
228 <td width='10%' class='base'>$Lang::tr{'month'}:&nbsp;</td>
229 <td width='10%'>
230 <select name='MONTH'>
231END
232;
233my $month;
234for ($month = 0; $month < 12; $month++)
235{
236 print "\t<option ";
237 if ($month == $cgiparams{'MONTH'}) {
238 print "selected='selected' "; }
239 print "value='$month'>$longmonths[$month]</option>\n";
240}
241print <<END
242 </select>
243 </td>
244 <td width='10%' class='base' align='right'>&nbsp;$Lang::tr{'day'}:&nbsp;</td>
245 <td width='40%'>
246 <select name='DAY'>
247END
248;
249for ($day = 1; $day <= 31; $day++)
250{
251 print "\t<option ";
252 if ($day == $cgiparams{'DAY'}) {
253 print "selected='selected' "; }
254 print "value='$day'>$day</option>\n";
255}
256print <<END
257</select>
258</td>
259<td width='5%' align='center'><input type='submit' name='ACTION' title='$Lang::tr{'day before'}' value='&lt;&lt;' /></td>
260<td width='5%' align='center'><input type='submit' name='ACTION' title='$Lang::tr{'day after'}' value='&gt;&gt;' /></td>
261<td width='10%' align='center'><input type='submit' name='ACTION' value='$Lang::tr{'update'}' /></td>
bde7a7d2 262<tr><td width='15%'>$Lang::tr{'source ip country'}</td><td><input type='text' name='country' value='$cgiparams{country}'size='15'></td></tr>
e4aac473
AH
263</tr>
264</table>
265</form>
266END
267;
268
269&Header::closebox();
270
271&Header::openbox('100%', 'left', $Lang::tr{'firewall log'});
272print "<p><b>$Lang::tr{'firewall hits'} $longmonthstr $daystr: $lines</b></p>";
273
274if ($start == -1) {
275 $start = $lines - ${Header::viewsize}; }
276if ($start >= $lines - ${Header::viewsize}) { $start = $lines - ${Header::viewsize}; };
277if ($start < 0) { $start = 0; }
278
279my $prev = $start - ${Header::viewsize};
280my $next = $start + ${Header::viewsize};
281
282if ($prev < 0) { $prev = 0; }
283if ($next >= $lines) { $next = -1 }
284if ($start == 0) { $prev = -1; }
285
286if ($lines != 0) { &oldernewer(); }
287
288print <<END
bde7a7d2
AH
289<table width='100%'>
290<tr>
291<td width='10%' align='center' class='boldbase'><b>$Lang::tr{'time'}</b></td>
292<td width='13%' align='center' class='boldbase'><b>$Lang::tr{'chain'}</b></td>
293<td width='5%' align='center' class='boldbase'><b>$Lang::tr{'iface'}</b></td>
294<td width='5%' align='center' class='boldbase'><b>$Lang::tr{'proto'}</b></td>
295<td width='16%' align='center' class='boldbase'><b>$Lang::tr{'source'}</b></td>
296<td width='10%' align='center' class='boldbase'><b>$Lang::tr{'src port'}</b></td>
297<td width='16%' align='center' class='boldbase'><b>$Lang::tr{'destination'}</b></td>
298<td width='16%' align='center' class='boldbase'><b>$Lang::tr{'dst port'}</b></td>
299</tr>
e4aac473
AH
300END
301;
302
303my @slice = splice(@log, $start, ${Header::viewsize});
304
305if ($logsettings{'LOGVIEW_REVERSE'} eq 'on') { @slice = reverse @slice; }
306
307$lines = 0;
308foreach $_ (@slice)
309{
310 $a = $_;
fde47f5a
AH
311 /^... (..) (..:..:..) [\w\-]+ kernel:(.*)(IN=.*)$/;
312 my $packet = $4;
313 $packet =~ /IN=(\w+)/; my $iface=$1; if ( $1 =~ /2./ ){ $iface="";}
314 $packet =~ /SRC=([\d\.]+)/; my $srcaddr=$1;
315
316 if($iface eq $country || $srcaddr ne '') {
317 my $ccode;
318 if($iface ne $country) {
319 $ccode = $gi->country_code_by_name($srcaddr);
320 }
321 if($iface eq $country || $ccode eq $country) {
bde7a7d2
AH
322 my $chain = '';
323 my $in = '-'; my $out = '-';
324 my $srcaddr = ''; my $dstaddr = '';
325 my $protostr = '';
326 my $srcport = ''; my $dstport = '';
327
328 $_ =~ /(^.* ..:..:..) [\w\-]+ kernel:(.*)(IN=.*)$/;
329 my $timestamp = $1; my $chain = $2; my $packet = $3;
330 $timestamp =~ /(...) (..) (..:..:..)/;
331 my $month = $1; my $day = $2; my $time = $3;
332
333 if ($a =~ /IN\=(\w+)/) { $iface = $1; }
334 if ($a =~ /OUT\=(\w+)/) { $out = $1; }
335 if ($a =~ /SRC\=([\d\.]+)/) { $srcaddr = $1; }
336 if ($a =~ /DST\=([\d\.]+)/) { $dstaddr = $1; }
337 if ($a =~ /PROTO\=(\w+)/) { $protostr = $1; }
338 my $protostrlc = lc($protostr);
339 if ($a =~ /SPT\=([\d\.]+)/){ $srcport = $1; }
340 if ($a =~ /DPT\=([\d\.]+)/){ $dstport = $1; }
341
342 if ($lines % 2) {
343 print "<tr bgcolor='${Header::table1colour}'>\n"; }
344 else {
345 print "<tr bgcolor='${Header::table2colour}'>\n"; }
346 print <<END
347 <td align='center'>$time</td>
348 <td align='center'>$chain</td>
349 <td align='center'>$iface</td>
350 <td align='center'>$protostr</td>
351 <td align='center'>
352 <table width='100%' cellpadding='0' cellspacing='0'><tr>
353 <td align='center'><a href='/cgi-bin/ipinfo.cgi?ip=$srcaddr'>$srcaddr</a></td>
354 </tr></table>
355 </td>
356 <td align='center'>$srcport</td>
357 <td align='center'>
358 <table width='100%' cellpadding='0' cellspacing='0'><tr>
359 <td align='center'><a href='/cgi-bin/ipinfo.cgi?ip=$dstaddr'>$dstaddr</a></td>
360 </tr></table>
361 </td>
362 <td align='center'>$dstport</td>
363 </tr>
e4aac473
AH
364END
365 ;
bde7a7d2 366 $lines++;
e4aac473
AH
367 }
368 }
369}
370
371print <<END
bde7a7d2 372</table>
e4aac473
AH
373END
374;
375
376&oldernewer();
377
378&Header::closebox();
379
380&Header::closebigbox();
381
382&Header::closepage();
383
384sub oldernewer
385{
bde7a7d2
AH
386 print <<END
387 <table width='100%'>
388 <tr>
e4aac473
AH
389END
390;
391
bde7a7d2
AH
392 print "<td align='center' width='50%'>";
393 if ($prev != -1) {
394 print "<a href='/cgi-bin/logs.cgi/showrequestfromcountry.dat?$prev,$cgiparams{'MONTH'},$cgiparams{'DAY'},$cgiparams{country}'>$Lang::tr{'older'}</a>"; }
395 else {
396 print "$Lang::tr{'older'}"; }
397 print "</td>\n";
e4aac473 398
bde7a7d2
AH
399 print "<td align='center' width='50%'>";
400 if ($next != -1) {
401 print "<a href='/cgi-bin/logs.cgi/showrequestfromcountry.dat?$next,$cgiparams{'MONTH'},$cgiparams{'DAY'},$cgiparams{country}'>$Lang::tr{'newer'}</a>"; }
402 else {
403 print "$Lang::tr{'newer'}"; }
404 print "</td>\n";
e4aac473
AH
405
406print <<END
bde7a7d2
AH
407 </tr>
408 </table>
e4aac473
AH
409END
410;
411}
bde7a7d2 412