]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blob - html/cgi-bin/logs.cgi/ipblocklists.dat
log.dat: Add NUT entry for System Logs - Fixes bug#12921
[people/pmueller/ipfire-2.x.git] / html / cgi-bin / logs.cgi / ipblocklists.dat
1 #!/usr/bin/perl
2 #
3 # SmoothWall CGIs
4 #
5 # This code is distributed under the terms of the GPL
6 #
7 # JC HERITIER
8 # page inspired from the initial firewalllog.dat
9 #
10 # Modified for IPFire by Christian Schmidt
11 # and Michael Tremer (www.ipfire.org)
12
13 use strict;
14
15 # enable only the following on debugging purpose
16 #use warnings;
17 #use CGI::Carp 'fatalsToBrowser';
18
19 require '/var/ipfire/general-functions.pl';
20 require "${General::swroot}/lang.pl";
21 require "${General::swroot}/header.pl";
22
23 require "${General::swroot}/ipblocklist-functions.pl";
24 require "${General::swroot}/ipblocklist/sources";
25
26 use POSIX();
27
28 my %cgiparams=();
29 my $errormessage = '';
30
31 my @shortmonths = ( 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug',
32 'Sep', 'Oct', 'Nov', 'Dec' );
33 my @longmonths = ( $Lang::tr{'january'}, $Lang::tr{'february'}, $Lang::tr{'march'},
34 $Lang::tr{'april'}, $Lang::tr{'may'}, $Lang::tr{'june'}, $Lang::tr{'july'}, $Lang::tr{'august'},
35 $Lang::tr{'september'}, $Lang::tr{'october'}, $Lang::tr{'november'},
36 $Lang::tr{'december'} );
37
38 my @now = localtime();
39 my $dow = $now[6];
40 my $doy = $now[7];
41 my $tdoy = $now[7];
42 my $year = $now[5]+1900;
43
44 $cgiparams{'DAY'} = $now[3];
45 $cgiparams{'MONTH'} = $now[4];
46 $cgiparams{'ACTION'} = '';
47
48 &Header::getcgihash(\%cgiparams);
49
50 my $start = -1;
51 if ($ENV{'QUERY_STRING'} && $cgiparams{'ACTION'} ne $Lang::tr{'update'})
52 {
53 my @temp = split(',',$ENV{'QUERY_STRING'});
54 $start = $temp[0];
55 $cgiparams{'MONTH'} = $temp[1];
56 $cgiparams{'DAY'} = $temp[2];
57 }
58
59 if (!($cgiparams{'MONTH'} =~ /^(0|1|2|3|4|5|6|7|8|9|10|11)$/) ||
60 !($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)$/))
61 {
62 $cgiparams{'DAY'} = $now[3];
63 $cgiparams{'MONTH'} = $now[4];
64 }
65 elsif($cgiparams{'ACTION'} eq '>>')
66 {
67 my @temp_then=();
68 my @temp_now = localtime(time);
69 $temp_now[4] = $cgiparams{'MONTH'};
70 $temp_now[3] = $cgiparams{'DAY'};
71 @temp_then = localtime(POSIX::mktime(@temp_now) + 86400);
72 ## Retrieve the same time on the next day -
73 ## 86400 seconds in a day
74 $cgiparams{'MONTH'} = $temp_then[4];
75 $cgiparams{'DAY'} = $temp_then[3];
76 }
77 elsif($cgiparams{'ACTION'} eq '<<')
78 {
79 my @temp_then=();
80 my @temp_now = localtime(time);
81 $temp_now[4] = $cgiparams{'MONTH'};
82 $temp_now[3] = $cgiparams{'DAY'};
83 @temp_then = localtime(POSIX::mktime(@temp_now) - 86400);
84 ## Retrieve the same time on the previous day -
85 ## 86400 seconds in a day
86 $cgiparams{'MONTH'} = $temp_then[4];
87 $cgiparams{'DAY'} = $temp_then[3];
88 }
89
90 if (($cgiparams{'DAY'} ne $now[3]) || ($cgiparams{'MONTH'} ne $now[4]))
91 {
92 my @then = ();
93 if ( ( $cgiparams{'MONTH'} eq $now[4]) && ($cgiparams{'DAY'} > $now[3]) ||
94 ( $cgiparams{'MONTH'} > $now[4] ) ) {
95 @then = localtime(POSIX::mktime( 0, 0, 0, $cgiparams{'DAY'}, $cgiparams{'MONTH'}, $year - 1901 ));
96 } else {
97 @then = localtime(POSIX::mktime( 0, 0, 0, $cgiparams{'DAY'}, $cgiparams{'MONTH'}, $year - 1900 ));
98 }
99 $tdoy = $then[7];
100 my $lastleap=($year-1)%4;
101 if ($tdoy>$doy) {
102 if ($lastleap == 0 && $tdoy < 60) {
103 $doy=$tdoy+366;
104 } else {
105 $doy=$doy+365;
106 }
107 }
108 }
109
110 my $datediff=0;
111 my $dowd=0;
112 my $multifile=0;
113 if ($tdoy ne $doy) {
114 $datediff=int(($doy-$tdoy)/7);
115 $dowd=($doy-$tdoy)%7;
116 if (($dow-$dowd)<1) {
117 $datediff=$datediff+1;
118 }
119 if (($dow-$dowd)==0) {
120 $multifile=1;
121 }
122 }
123
124 my $monthstr = $shortmonths[$cgiparams{'MONTH'}];
125 my $longmonthstr = $longmonths[$cgiparams{'MONTH'}];
126 my $day = $cgiparams{'DAY'};
127 my $daystr='';
128 if ($day <= 9) {
129 $daystr = " $day"; }
130 else {
131 $daystr = $day;
132 }
133
134 my %lists;
135 my %directions;
136 my %sources = ();
137 my %settings = ();
138 &General::readhash("${General::swroot}/ipblocklist/settings", \%settings);
139
140 # Get all available blocklists.
141 my @blocklists = &IPblocklist::get_blocklists();
142
143 foreach my $blocklist (@blocklists)
144 {
145 $lists{$blocklist} = {} if ($settings{$blocklist} eq 'on');
146 }
147
148 my $skip=0;
149 my $filestr='';
150 if ($datediff==0) {
151 $filestr="/var/log/messages";
152 } else {
153 $filestr="/var/log/messages.$datediff";
154 $filestr = "$filestr.gz" if -f "$filestr.gz";
155 }
156
157 if (!(open (FILE,($filestr =~ /.gz$/ ? "gzip -dc $filestr |" : $filestr)))) {
158 $errormessage = "$Lang::tr{'date not in logs'}: $filestr $Lang::tr{'could not be opened'}";
159 $skip=1;
160 # Note: This is in case the log does not exist for that date
161 }
162
163 my $lines = 0;
164 my $directions = 0;
165
166 if (!$skip)
167 {
168 while (<FILE>)
169 {
170 if (/^${monthstr} ${daystr} ..:..:.. [\w\-]+ kernel:.*BLKLST_(\w+)\s*IN=(\w*)/)
171 {
172 my $list = $1;
173
174 if ($2 =~ m/ppp|red/)
175 {
176 $lists{$list}{in}++;
177 $directions{in}++;
178 }
179 else
180 {
181 $lists{$list}{out}++;
182 $directions{out}++;
183 }
184
185 $lines++;
186 }
187
188 }
189 close (FILE);
190 }
191
192 if ($multifile) {
193 $datediff=$datediff-1;
194 if ($datediff==0) {
195 $filestr="/var/log/messages";
196 } else {
197 $filestr="/var/log/messages.$datediff";
198 $filestr = "$filestr.gz" if -f "$filestr.gz";
199 }
200 if (!(open (FILE,($filestr =~ /.gz$/ ? "gzip -dc $filestr |" : $filestr)))) {
201 $errormessage="$Lang::tr{'date not in logs'}: $filestr $Lang::tr{'could not be opened'}";
202 $skip=1;
203 }
204 if (!$skip) {
205 while (<FILE>) {
206 if (/^${monthstr} ${daystr} ..:..:.. [\w\-]+ kernel:.*BLKLST_(\w+)\s*IN=(\w+)/)
207 {
208 my $list = $1;
209
210 if ($2 =~ m/ppp|red/)
211 {
212 $lists{$list}{in}++;
213 $directions{in}++;
214 }
215 else
216 {
217 $lists{$list}{out}++;
218 $directions{out}++;
219 }
220
221 $lines++;
222 }
223 }
224 close (FILE);
225 }
226 }
227
228 my $MODNAME="fwlogs";
229
230 &Header::showhttpheaders();
231 &Header::openpage($Lang::tr{'ipblocklist logs'}, 1, '');
232 &Header::openbigbox('100%', 'left', '', $errormessage);
233
234
235 if ($errormessage) {
236 &Header::openbox('100%', 'left', $Lang::tr{'error messages'});
237 print "<font class='base'>$errormessage&nbsp;</font>\n";
238 &Header::closebox();
239 }
240
241 &Header::openbox('100%', 'left', "$Lang::tr{'settings'}");
242
243 print <<END
244 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
245 <table width='100%'>
246 <tr>
247 <td width='10%' class='base'>$Lang::tr{'month'}:&nbsp;</td>
248 <td width='10%'>
249 <select name='MONTH'>
250 END
251 ;
252 my $month;
253 for ($month = 0; $month < 12; $month++)
254 {
255 print "\t<option ";
256 if ($month == $cgiparams{'MONTH'}) {
257 print "selected='selected' ";
258 }
259 print "value='$month'>$longmonths[$month]</option>\n";
260 }
261 print <<END
262 </select>
263 </td>
264 <td width='10%' class='base' align='right'>&nbsp;$Lang::tr{'day'}:&nbsp;</td>
265 <td width='40%'>
266 <select name='DAY'>
267 END
268 ;
269 for ($day = 1; $day <= 31; $day++)
270 {
271 print "\t<option ";
272 if ($day == $cgiparams{'DAY'}) {
273 print "selected='selected' ";
274 }
275 print "value='$day'>$day</option>\n";
276 }
277
278 print <<END
279 </select>
280 </td>
281 <td width='5%' align='center'><input type='submit' name='ACTION' title='$Lang::tr{'day before'}' value='&lt;&lt;' /></td>
282 <td width='5%' align='center'><input type='submit' name='ACTION' title='$Lang::tr{'day after'}' value='&gt;&gt;' /></td>
283 <td width='20%' align='right'><input type='submit' name='ACTION' value='$Lang::tr{'update'}' /></td>
284 </tr>
285 </table>
286 </form>
287 END
288 ;
289
290 &Header::closebox();
291
292 &Header::openbox('100%', 'left', $Lang::tr{'firewall log'});
293 print "<p><b>$Lang::tr{'ipblocklist hits'} $longmonthstr $daystr: $lines</b></p>";
294
295 my %color = ();
296 my %mainsettings = ();
297 &General::readhash("${General::swroot}/main/settings", \%mainsettings);
298 &General::readhash("/srv/web/ipfire/html/themes/ipfire/include/colors.txt", \%color);
299
300 my @lists = sort keys (%lists);
301
302 print <<END
303 <table width='100%' class='tbl'>
304 <tr>
305 <th align='center' class='boldbase' rowspan='2'></th>
306 <th align='left' class='boldbase' rowspan='2'><b>$Lang::tr{'ipblocklist id'}</b></th>
307 <th align='left' class='boldbase' rowspan='2'><b>$Lang::tr{'ipblocklist category'}</b></th>
308 <th align='center' class='boldbase' colspan='2'><b>$Lang::tr{'ipblocklist input'}</b></th>
309 <th align='center' class='boldbase' colspan='2'><b>$Lang::tr{'ipblocklist output'}</b></th>
310 </tr>
311 <tr>
312 <th align='center' class='boldbase'>$Lang::tr{'count'}</th>
313 <th align='center' class='boldbase'>$Lang::tr{'percentage'}</th>
314 <th align='center' class='boldbase'>$Lang::tr{'count'}</th>
315 <th align='center' class='boldbase'>$Lang::tr{'percentage'}</th>
316 </tr>
317 END
318 ;
319
320 $lines = 0;
321 my $lists = join ',', @lists;
322
323 foreach my $list (@lists)
324 {
325 my $col = ($lines++ % 2) ? "bgcolor='$color{'color20'}'" : "bgcolor='$color{'color22'}'";
326 my $category = exists( $IPblocklist::List::sources{$list}) ? $Lang::tr{"ipblocklist category $IPblocklist::List::sources{$list}{'category'}"} : '&nbsp;';
327
328 print "<tr>";
329
330 print "<td align='center' $col><form method='post' action='showrequestfromblocklist.dat'><input type='hidden' name='MONTH' value='$cgiparams{'MONTH'}'> <input type='hidden' name='DAY' value='$cgiparams{'DAY'}'> <input type='hidden' name='blocklist' value='$list'><input type='hidden' name='blocklists' value='$lists'> <input type='submit' value='$Lang::tr{'details'}'></form></td>";
331
332 if (exists($IPblocklist::List::sources{$list}) and $IPblocklist::List::sources{$list}{'info'})
333 {
334 print "<td $col><a href='$IPblocklist::List::sources{$list}{info}' target='_blank'>$list</a></td>";
335 }
336 else
337 {
338 print "<td $col>$list</td>";
339 }
340
341 print "<td $col>$category</td>";
342
343 foreach my $direction ('in', 'out')
344 {
345 my $count = $lists{$list}{$direction} || 0;
346 my $percent = $directions{$direction} > 0 ? $count * 100 / $directions{$direction} : 0;
347 $percent = sprintf("%.f", $percent);
348 print "<td align='center' class='boldbase' $col>$count</th>";
349 print "<td align='center' class='boldbase' $col>$percent%</th>";
350 }
351
352 print "</tr>";
353 }
354 print <<END
355 </table>
356 END
357 ;
358
359 &Header::closebox();
360 &Header::closebigbox();
361 &Header::closepage();
362
363 sub checkversion {
364 #Automatic Updates is disabled
365 return "0","0";
366 }