]>
Commit | Line | Data |
---|---|---|
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; | |
16 | use Geo::IP::PurePerl; | |
17 | ||
18 | require '/var/ipfire/general-functions.pl'; | |
19 | require "${General::swroot}/lang.pl"; | |
20 | require "${General::swroot}/header.pl"; | |
21 | ||
22 | use POSIX(); | |
23 | ||
24 | #workaround to suppress a warning when a variable is used only once | |
25 | my @dummy = ( ${Header::table2colour} ); | |
26 | undef (@dummy); | |
27 | ||
28 | my %cgiparams=(); | |
29 | my %logsettings=(); | |
30 | my $errormessage = ''; | |
31 | ||
32 | my @shortmonths = ( 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', | |
33 | 'Sep', 'Oct', 'Nov', 'Dec' ); | |
34 | my @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 | ||
39 | my @now = localtime(); | |
40 | my $dow = $now[6]; | |
41 | my $doy = $now[7]; | |
42 | my $tdoy = $now[7]; | |
43 | my $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 | ||
54 | my $start = -1; | |
55 | if ($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]; | |
61 | $cgiparams{ip} = $temp[3]; | |
62 | } | |
63 | ||
64 | if (!($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 | } | |
70 | elsif($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 | } | |
82 | elsif($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 | ||
95 | if (($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 | } | |
114 | my $datediff=0; | |
115 | my $dowd=0; | |
116 | my $multifile=0; | |
117 | if ($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 | ||
128 | my $monthstr = $shortmonths[$cgiparams{'MONTH'}]; | |
129 | my $longmonthstr = $longmonths[$cgiparams{'MONTH'}]; | |
130 | my $day = $cgiparams{'DAY'}; | |
131 | my $daystr=''; | |
132 | if ($day <= 9) { | |
133 | $daystr = " $day"; } | |
134 | else { | |
135 | $daystr = $day; | |
136 | } | |
137 | ||
138 | my $skip=0; | |
139 | my $filestr=''; | |
140 | if ($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 | ||
147 | if (!(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 | } | |
152 | my $lines = 0; | |
153 | my @log=(); | |
154 | my $country = $cgiparams{country}; | |
155 | my $gi = Geo::IP::PurePerl->new(); | |
156 | ||
157 | if (!$skip) | |
158 | { | |
159 | while (<FILE>) | |
160 | { | |
161 | if (/(^${monthstr} ${daystr} ..:..:..) [\w\-]+ kernel:.*(IN=.*)$/) { | |
162 | if($_ =~ /SRC\=([\d\.]+)/){ | |
163 | my $srcaddr=$1; | |
164 | my $ccode = $gi->country_code_by_name($srcaddr); | |
165 | if($ccode eq $country){ | |
166 | $log[$lines] = $_; | |
167 | $lines++; | |
168 | } | |
169 | } | |
170 | } | |
171 | } | |
172 | close (FILE); | |
173 | } | |
174 | ||
175 | $skip=0; | |
176 | if ($multifile) { | |
177 | $datediff=$datediff-1; | |
178 | if ($datediff==0) { | |
179 | $filestr="/var/log/messages"; | |
180 | } else { | |
181 | $filestr="/var/log/messages.$datediff"; | |
182 | $filestr = "$filestr.gz" if -f "$filestr.gz"; | |
183 | } | |
184 | if (!(open (FILE,($filestr =~ /.gz$/ ? "gzip -dc $filestr |" : $filestr)))) { | |
185 | $errormessage="$Lang::tr{'date not in logs'}: $filestr $Lang::tr{'could not be opened'}"; | |
186 | $skip=1; | |
187 | } | |
188 | if (!$skip) { | |
189 | while (<FILE>) { | |
190 | if (/(^${monthstr} ${daystr} ..:..:..) [\w\-]+ kernel:.*(IN=.*)$/) { | |
191 | if($_ =~ /SRC\=([\d\.]+)/){ | |
192 | my $srcaddr=$1; | |
193 | my $ccode = $gi->country_code_by_name($srcaddr); | |
194 | if($ccode eq $country){ | |
195 | $log[$lines] = $_; | |
196 | $lines++; | |
197 | } | |
198 | } | |
199 | } | |
200 | } | |
201 | close (FILE); | |
202 | } | |
203 | } | |
204 | ||
205 | &Header::showhttpheaders(); | |
206 | &Header::openpage($Lang::tr{'firewall log'}, 1, ''); | |
207 | &Header::openbigbox('100%', 'left', '', $errormessage); | |
208 | ||
209 | if ($errormessage) { | |
210 | &Header::openbox('100%', 'left', $Lang::tr{'error messages'}); | |
211 | print "<font class='base'>$errormessage </font>\n"; | |
212 | &Header::closebox(); | |
213 | } | |
214 | ||
215 | &Header::openbox('100%', 'left', "$Lang::tr{'settings'}:"); | |
216 | ||
217 | print <<END | |
218 | <form method='post' action='$ENV{'SCRIPT_NAME'}'> | |
219 | <table width='100%'> | |
220 | <tr> | |
221 | <td width='10%' class='base'>$Lang::tr{'month'}: </td> | |
222 | <td width='10%'> | |
223 | <select name='MONTH'> | |
224 | END | |
225 | ; | |
226 | my $month; | |
227 | for ($month = 0; $month < 12; $month++) | |
228 | { | |
229 | print "\t<option "; | |
230 | if ($month == $cgiparams{'MONTH'}) { | |
231 | print "selected='selected' "; } | |
232 | print "value='$month'>$longmonths[$month]</option>\n"; | |
233 | } | |
234 | print <<END | |
235 | </select> | |
236 | </td> | |
237 | <td width='10%' class='base' align='right'> $Lang::tr{'day'}: </td> | |
238 | <td width='40%'> | |
239 | <select name='DAY'> | |
240 | END | |
241 | ; | |
242 | for ($day = 1; $day <= 31; $day++) | |
243 | { | |
244 | print "\t<option "; | |
245 | if ($day == $cgiparams{'DAY'}) { | |
246 | print "selected='selected' "; } | |
247 | print "value='$day'>$day</option>\n"; | |
248 | } | |
249 | print <<END | |
250 | </select> | |
251 | </td> | |
252 | <td width='5%' align='center'><input type='submit' name='ACTION' title='$Lang::tr{'day before'}' value='<<' /></td> | |
253 | <td width='5%' align='center'><input type='submit' name='ACTION' title='$Lang::tr{'day after'}' value='>>' /></td> | |
254 | <td width='10%' align='center'><input type='submit' name='ACTION' value='$Lang::tr{'update'}' /></td> | |
255 | <tr><td width='15%'>$Lang::tr{'source ip'}</td><td><input type='text' name='ip' value='$cgiparams{ip}'size='15'></td></tr> | |
256 | </tr> | |
257 | </table> | |
258 | </form> | |
259 | END | |
260 | ; | |
261 | ||
262 | &Header::closebox(); | |
263 | ||
264 | &Header::openbox('100%', 'left', $Lang::tr{'firewall log'}); | |
265 | print "<p><b>$Lang::tr{'firewall hits'} $longmonthstr $daystr: $lines</b></p>"; | |
266 | ||
267 | if ($start == -1) { | |
268 | $start = $lines - ${Header::viewsize}; } | |
269 | if ($start >= $lines - ${Header::viewsize}) { $start = $lines - ${Header::viewsize}; }; | |
270 | if ($start < 0) { $start = 0; } | |
271 | ||
272 | my $prev = $start - ${Header::viewsize}; | |
273 | my $next = $start + ${Header::viewsize}; | |
274 | ||
275 | if ($prev < 0) { $prev = 0; } | |
276 | if ($next >= $lines) { $next = -1 } | |
277 | if ($start == 0) { $prev = -1; } | |
278 | ||
279 | if ($lines != 0) { &oldernewer(); } | |
280 | ||
281 | print <<END | |
282 | <TABLE WIDTH='100%'> | |
283 | <TR> | |
284 | <TD WIDTH='10%' ALIGN='CENTER' CLASS='boldbase'><B>$Lang::tr{'time'}</B></TD> | |
285 | <TD WIDTH='13%' ALIGN='CENTER' CLASS='boldbase'><B>$Lang::tr{'chain'}</B></TD> | |
286 | <TD WIDTH='5%' ALIGN='CENTER' CLASS='boldbase'><B>$Lang::tr{'iface'}</B></TD> | |
287 | <TD WIDTH='5%' ALIGN='CENTER' CLASS='boldbase'><B>$Lang::tr{'proto'}</B></TD> | |
288 | <TD WIDTH='16%' ALIGN='CENTER' CLASS='boldbase'><B>$Lang::tr{'source'}</B></TD> | |
289 | <TD WIDTH='10%' ALIGN='CENTER' CLASS='boldbase'><B>$Lang::tr{'src port'}</B></TD> | |
290 | <TD WIDTH='16%' ALIGN='CENTER' CLASS='boldbase'><B>$Lang::tr{'destination'}</B></TD> | |
291 | <TD WIDTH='16%' ALIGN='CENTER' CLASS='boldbase'><B>$Lang::tr{'dst port'}</B></TD> | |
292 | </TR> | |
293 | END | |
294 | ; | |
295 | ||
296 | my @slice = splice(@log, $start, ${Header::viewsize}); | |
297 | ||
298 | if ($logsettings{'LOGVIEW_REVERSE'} eq 'on') { @slice = reverse @slice; } | |
299 | ||
300 | $lines = 0; | |
301 | foreach $_ (@slice) | |
302 | { | |
303 | $a = $_; | |
304 | if($_ =~ /SRC\=([\d\.]+)/){ | |
305 | my $srcaddr=$1; | |
306 | my $ccode = $gi->country_code_by_name($srcaddr); | |
307 | if($ccode eq $country){ | |
308 | my $chain = ''; | |
309 | my $in = '-'; my $out = '-'; | |
310 | my $srcaddr = ''; my $dstaddr = ''; | |
311 | my $protostr = ''; | |
312 | my $srcport = ''; my $dstport = ''; | |
313 | ||
314 | $_ =~ /(^.* ..:..:..) [\w\-]+ kernel:(.*)(IN=.*)$/; | |
315 | my $timestamp = $1; my $chain = $2; my $packet = $3; | |
316 | $timestamp =~ /(...) (..) (..:..:..)/; | |
317 | my $month = $1; my $day = $2; my $time = $3; | |
318 | ||
319 | if ($a =~ /IN\=(\w+)/) { $iface = $1; } | |
320 | if ($a =~ /OUT\=(\w+)/) { $out = $1; } | |
321 | if ($a =~ /SRC\=([\d\.]+)/) { $srcaddr = $1; } | |
322 | if ($a =~ /DST\=([\d\.]+)/) { $dstaddr = $1; } | |
323 | if ($a =~ /PROTO\=(\w+)/) { $protostr = $1; } | |
324 | my $protostrlc = lc($protostr); | |
325 | if ($a =~ /SPT\=([\d\.]+)/){ $srcport = $1; } | |
326 | if ($a =~ /DPT\=([\d\.]+)/){ $dstport = $1; } | |
327 | ||
328 | if ($lines % 2) { | |
329 | print "<tr bgcolor='${Header::table1colour}'>\n"; } | |
330 | else { | |
331 | print "<tr bgcolor='${Header::table2colour}'>\n"; } | |
332 | print <<END | |
333 | <TD ALIGN='CENTER'>$time</TD> | |
334 | <TD ALIGN='CENTER'>$chain</TD> | |
335 | <TD ALIGN='CENTER'>$iface</TD> | |
336 | <TD ALIGN='CENTER'>$protostr</TD> | |
337 | <td align='center'> | |
338 | <table width='100%' cellpadding='0' cellspacing='0'><tr> | |
339 | <td align='center'><a href='/cgi-bin/ipinfo.cgi?ip=$srcaddr'>$srcaddr</a></td> | |
340 | </tr></table> | |
341 | </td> | |
342 | <TD ALIGN='CENTER'>$srcport</TD> | |
343 | <td align='center'> | |
344 | <table width='100%' cellpadding='0' cellspacing='0'><tr> | |
345 | <td align='center'><a href='/cgi-bin/ipinfo.cgi?ip=$dstaddr'>$dstaddr</a></td> | |
346 | </tr></table> | |
347 | </td> | |
348 | <TD ALIGN='CENTER'>$dstport</TD> | |
349 | </TR> | |
350 | END | |
351 | ; | |
352 | $lines++; | |
353 | } | |
354 | } | |
355 | } | |
356 | ||
357 | print <<END | |
358 | </TABLE> | |
359 | END | |
360 | ; | |
361 | ||
362 | &oldernewer(); | |
363 | ||
364 | &Header::closebox(); | |
365 | ||
366 | &Header::closebigbox(); | |
367 | ||
368 | &Header::closepage(); | |
369 | ||
370 | sub oldernewer | |
371 | { | |
372 | print <<END | |
373 | <table width='100%'> | |
374 | <tr> | |
375 | END | |
376 | ; | |
377 | ||
378 | print "<td align='center' width='50%'>"; | |
379 | if ($prev != -1) { | |
380 | print "<a href='/cgi-bin/logs.cgi/showrequestfromip.dat?$prev,$cgiparams{'MONTH'},$cgiparams{'DAY'},$cgiparams{ip}'>$Lang::tr{'older'}</a>"; } | |
381 | else { | |
382 | print "$Lang::tr{'older'}"; } | |
383 | print "</td>\n"; | |
384 | ||
385 | print "<td align='center' width='50%'>"; | |
386 | if ($next != -1) { | |
387 | print "<a href='/cgi-bin/logs.cgi/showrequestfromip.dat?$next,$cgiparams{'MONTH'},$cgiparams{'DAY'},$cgiparams{ip}'>$Lang::tr{'newer'}</a>"; } | |
388 | else { | |
389 | print "$Lang::tr{'newer'}"; } | |
390 | print "</td>\n"; | |
391 | ||
392 | print <<END | |
393 | </tr> | |
394 | </table> | |
395 | END | |
396 | ; | |
397 | } |