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