]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blob - html/cgi-bin/guardian.cgi
guardian.cgi: Add hyperlink to ipinfo page for blocked hosts.
[people/pmueller/ipfire-2.x.git] / html / cgi-bin / guardian.cgi
1 #!/usr/bin/perl
2 ###############################################################################
3 # #
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2014 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 use Locale::Country;
24
25 # enable only the following on debugging purpose
26 use warnings;
27 use CGI::Carp 'fatalsToBrowser';
28
29 require '/var/ipfire/general-functions.pl';
30 require "${General::swroot}/lang.pl";
31 require "${General::swroot}/header.pl";
32
33 #workaround to suppress a warning when a variable is used only once
34 my @dummy = ( ${Header::colouryellow} );
35 undef (@dummy);
36
37 my $string=();
38 my $memory=();
39 my @memory=();
40 my @pid=();
41 my @guardian=();
42 my %cgiparams=();
43
44 # Path to the guardian.ignore file.
45 my $ignorefile ='/var/ipfire/guardian/guardian.ignore';
46
47 # Path to the guardian.target file.
48 my $targetfile ='/var/ipfire/guardian/guardian.ignore';
49
50 our %netsettings = ();
51 &General::readhash("${General::swroot}/ethernet/settings", \%netsettings);
52
53 our %color = ();
54 our %mainsettings = ();
55 &General::readhash("${General::swroot}/main/settings", \%mainsettings);
56 &General::readhash("/srv/web/ipfire/html/themes/".$mainsettings{'THEME'}."/include/colors.txt", \%color);
57
58 our %settings = ();
59
60 $settings{'GUARDIAN_ENABLED'} = 'off';
61 $settings{'GUARDIAN_BLOCKINTERFACES'} ='default';
62 $settings{'GUARDIAN_BLOCKTIME'} = '86400';
63 $settings{'GUARDIAN_LOGFILE'} = '/var/log/guardian/guardian.log';
64 $settings{'GUARDIAN_SNORT_ALERTFILE'} = '/var/log/snort/alert';
65
66 $settings{'ACTION'} = '';
67
68 my $errormessage = '';
69
70 &Header::showhttpheaders();
71
72 # Get GUI values.
73 &Header::getcgihash(\%settings);
74 &Header::getcgihash(\%cgiparams);
75
76 ## Perform input checks and save settings.
77 #
78 if ($settings{'ACTION'} eq $Lang::tr{'save'}) {
79
80 # Check for valid blocktime.
81 if ($settings{'GUARDIAN_BLOCKTIME'} ne '') {
82 if (($settings{'GUARDIAN_BLOCKTIME'} !~ /^[0-9]+$/) || ($settings{'GUARDIAN_BLOCKTIME'} le '0')) {
83 $errormessage = "$Lang::tr{'guardian invalid blocktime'}";
84 }
85 }
86
87 # Check Logfile.
88 if ($settings{'GUARDIAN_LOGFILE'} ne '') {
89 if ($settings{'GUARDIAN_LOGFILE'} !~ /^[a-zA-Z0-9\.\/]+$/) {
90 $errormessage = "$Lang::tr{'guardian invalid logfile'}";
91 }
92 }
93
94 # Check input for snort alert file.
95 if ($settings{'GUARDIAN_SNORT_ALERTFILE'} ne '') {
96 if ($settings{'GUARDIAN_SNORT_ALERTFILE'} !~ /^[a-zA-Z0-9\.\/]+$/) {
97 $errormessage = "$Lang::tr{'guardian invalid alertfile'}";
98 }
99 }
100
101 # Only continue if no error message has been set.
102 if ($errormessage eq '') {
103 # Write configuration settings to file.
104 &General::writehash("${General::swroot}/guardian/settings", \%settings);
105
106 # Update configuration files.
107 &BuildConfiguration();
108 }
109
110 ## Add a new entry to the ignore file.
111 #
112 } elsif ($settings{'ACTION'} eq $Lang::tr{'add'}) {
113
114 # Check if any input has been performed.
115 if ($settings{'NEW_IGNORE_ENTRY'} ne '') {
116
117 # Check if the given input is no valid IP-address or IP-address with subnet, display an error message.
118 if ((!&General::validip($settings{'NEW_IGNORE_ENTRY'})) && (!&General::validipandmask($settings{'NEW_IGNORE_ENTRY'}))) {
119 $errormessage = "$Lang::tr{'guardian invalid address or subnet'}";
120 }
121 } else {
122 $errormessage = "$Lang::tr{'guardian empty input'}";
123 }
124
125 # Go further if there was no error.
126 if ($errormessage eq '') {
127 my $new_entry = $settings{'NEW_IGNORE_ENTRY'};
128
129 # Open file for appending the new entry.
130 open (FILE, ">>$ignorefile") or die "Unable to open $ignorefile for writing";
131
132 # Write the new entry to the ignore file.
133 print FILE "$new_entry\n";
134 close(FILE);
135 }
136
137 ## Remove entry from ignore list.
138 #
139 } elsif ($settings{'ACTION'} eq $Lang::tr{'remove'}) {
140 my $id = 0;
141
142 # Open ignorefile and read content.
143 open(FILE, "<$ignorefile") or die "Unable to open $ignorefile.";
144 my @current = <FILE>;
145 close(FILE);
146
147 # Re-open ignorefile for writing.
148 open(FILE, ">$ignorefile") or die "Unable to open $ignorefile for writing";
149 flock FILE, 2;
150
151 # Read line by line from ignorefile and write them back except the line with the given ID.
152 # So this line is missing in the new file and the entry has been deleted.
153 foreach my $line (@current) {
154 $id++;
155 unless ($cgiparams{'ID'} eq $id) {
156 print FILE "$line";
157 }
158 }
159 close(FILE);
160
161 ## Block a user given address or subnet.
162 #
163 } elsif ($settings{'ACTION'} eq $Lang::tr{'block'}) {
164
165 # Check if no empty input has been performed.
166 if ($settings{'ADDRESS_BLOCK'} ne '') {
167
168 # Check if the given input is no valid IP-address or IP-address with subnet, display an error message.
169 if ((!&General::validip($settings{'ADDRESS_BLOCK'})) && (!&General::validipandmask($settings{'ADDRESS_BLOCK'}))) {
170 $errormessage = "$Lang::tr{'guardian invalid address or subnet'}";
171 }
172
173 } else {
174 $errormessage = "$Lang::tr{'guardian empty input'}";
175 }
176
177 # Go further if there was no error.
178 if ($errormessage eq '') {
179 my $block = $settings{'ADDRESS_BLOCK'};
180
181 # Call helper to unblock address.
182 system("/usr/local/bin/guardianctrl block $block &>/dev/null");
183 }
184
185 ## Unblock address or subnet.
186 #
187 } elsif ($settings{'ACTION'} eq $Lang::tr{'unblock'}) {
188
189 # Check if no empty input has been performed.
190 if ($settings{'ADDRESS_UNBLOCK'} ne '') {
191
192 # Check if the given input is no valid IP-address or IP-address with subnet, display an error message.
193 if ((!&General::validip($settings{'ADDRESS_UNBLOCK'})) && (!&General::validipandmask($settings{'ADDRESS_UNBLOCK'}))) {
194 $errormessage = "$Lang::tr{'guardian invalid address or subnet'}";
195 }
196
197 } else {
198 $errormessage = "$Lang::tr{'guardian empty input'}";
199 }
200
201 # Go further if there was no error.
202 if ($errormessage eq '') {
203 my $unblock = $settings{'ADDRESS_UNBLOCK'};
204
205 # Call helper to unblock address.
206 system("/usr/local/bin/guardianctrl unblock $unblock &>/dev/null");
207 }
208
209 ## Unblock all.
210 #
211 } elsif ($settings{'ACTION'} eq $Lang::tr{'unblock all'}) {
212
213 # Call helper to flush iptables chain from guardian.
214 system("/usr/local/bin/guardianctrl flush-chain &>/dev/null");
215
216 } else {
217 # Load settings from file.
218 &General::readhash("${General::swroot}/guardian/settings", \%settings);
219 }
220
221
222 # Call functions to generate whole page.
223 &showMainBox();
224 &showIgnoreBox();
225
226 # Display area only if guardian is running.
227 if ( ($memory != 0) && (@pid[0] ne "///") ) {
228 &showBlockedBox();
229 }
230
231 # Function to display the status of guardian and allow base configuration.
232 sub showMainBox() {
233 my %checked = ();
234
235 $checked{'GUARDIAN_ENABLED'}{'on'} = '';
236 $checked{'GUARDIAN_ENABLED'}{'off'} = '';
237 $checked{'GUARDIAN_ENABLED'}{$settings{'GUARDIAN_ENABLED'}} = 'checked';
238
239 $checked{'GUARDIAN_BLOCKINTERFACES'}{'default'} = '';
240 $checked{'GUARDIAN_BLOCKINTERFACES'}{'all'} = '';
241 $checked{'GUARDIAN_BLOCKINTERFACES'}{$settings{'GUARDIAN_BLOCKINTERFACES'}} = "checked='checked'";
242
243
244 &Header::openpage($Lang::tr{'guardian configuration'}, 1, '');
245 &Header::openbigbox('100%', 'left', '', $errormessage);
246
247 # Print errormessage if there is one.
248 if ($errormessage) {
249 &Header::openbox('100%', 'left', $Lang::tr{'error messages'});
250 print "<font class='base'>$errormessage&nbsp;</font>\n";
251 &Header::closebox();
252 }
253
254
255 # Draw current guardian state.
256 &Header::openbox('100%', 'center', $Lang::tr{'guardian'});
257
258 # Check if guardian is running and grab some stats.
259 &daemonstats();
260
261 # Display some useful information related to guardian, if daemon is running.
262 if ( ($memory != 0) && (@pid[0] ne "///") ){
263 print <<END;
264 <table width='95%' cellspacing='0' class='tbl'>
265 <tr>
266 <th bgcolor='$color{'color20'}' colspan='3' align='left'><strong>$Lang::tr{'guardian service'}</strong></th>
267 </tr>
268 <tr>
269 <td class='base'>$Lang::tr{'guardian daemon'}</td>
270 <td align='center' colspan='2' width='75%' bgcolor='${Header::colourgreen}'><font color='white'><strong>$Lang::tr{'running'}</strong></font></td>
271 </tr>
272 <tr>
273 <td class='base'></td>
274 <td bgcolor='$color{'color20'}' align='center'><strong>PID</strong></td>
275 <td bgcolor='$color{'color20'}' align='center'><strong>$Lang::tr{'memory'}</strong></td>
276 </tr>
277 <tr>
278 <td class='base'></td>
279 <td bgcolor='$color{'color22'}' align='center'>@pid[0]</td>
280 <td bgcolor='$color{'color22'}' align='center'>$memory KB</td>
281 </tr>
282 </table>
283 END
284 } else {
285 # Otherwise display a hint that the service is not launched.
286 print <<END;
287 <table width='95%' cellspacing='0' class='tbl'>
288 <tr>
289 <th bgcolor='$color{'color20'}' colspan='3' align='left'><strong>$Lang::tr{'guardian service'}</strong></th>
290 </tr>
291 <tr>
292 <td class='base'>$Lang::tr{'guardian daemon'}</td>
293 <td align='center' width='75%' bgcolor='${Header::colourred}'><font color='white'><strong>$Lang::tr{'stopped'}</strong></font></td>
294 </tr>
295 </table>
296 END
297 }
298
299 &Header::closebox();
300
301 # Draw elements for guardian configuration.
302 &Header::openbox('100%', 'center', $Lang::tr{'guardian configuration'});
303
304 print <<END;
305 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
306
307 <table width='95%'>
308 <tr>
309 <td colspan='5' class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'guardian common settings'}</b></td>
310 </tr>
311 <tr>
312 <td width='20%' class='base'>$Lang::tr{'guardian enabled'}:</td>
313 <td width='30%'><input type='checkbox' name='GUARDIAN_ENABLED' $checked{'GUARDIAN_ENABLED'}{'on'} /></td>
314 <td width='20%' class='base'>$Lang::tr{'guardian blockinterfaces'}:</td>
315 <td width='15%'> $Lang::tr{'guardian blockinterfaces default'}
316 <input type='radio' name='GUARDIAN_BLOCKINTERFACES' value='default' $checked{'GUARDIAN_BLOCKINTERFACES'}{'default'} /></td>
317 <td width='15%'> $Lang::tr{'guardian blockinterfaces all'}
318 <input type='radio' name='GUARDIAN_BLOCKINTERFACES' value='all' $checked{'GUARDIAN_BLOCKINTERFACES'}{'all'} /></td>
319
320 </tr>
321 <tr>
322 <td width='20%' class='base'>$Lang::tr{'guardian blocktime'}:</td>
323 <td colspan='5'><input type='text' name='GUARDIAN_BLOCKTIME' value='$settings{'GUARDIAN_BLOCKTIME'}' size='10' /></td>
324 </tr>
325 <tr>
326 <td width='20%' class='base'>$Lang::tr{'guardian logfile'}:</td>
327 <td colspan='5'><input type='text' name='GUARDIAN_LOGFILE' value='$settings{'GUARDIAN_LOGFILE'}' size='30' /></td>
328 </tr>
329 <tr>
330 <td width='20%' class='base'>$Lang::tr{'guardian snort alertfile'}:</td>
331 <td colspan='5'><input type='text' name='GUARDIAN_SNORT_ALERTFILE' value='$settings{'GUARDIAN_SNORT_ALERTFILE'}' size='30' /></td>
332 </tr>
333 </table>
334 END
335
336 print <<END;
337 <hr>
338
339 <table width='95%'>
340 <tr>
341 <td>&nbsp;</td>
342 <td align='center'><input type='submit' name='ACTION' value='$Lang::tr{'save'}' /></td>
343 <td>&nbsp;</td>
344 </tr>
345 </table>
346 </form>
347 END
348
349 &Header::closebox();
350 }
351
352 # Function to show elements of the guardian ignorefile and allow to add or remove single members of it.
353 sub showIgnoreBox() {
354 &Header::openbox('100%', 'center', $Lang::tr{'guardian ignored hosts'});
355
356 print <<END;
357 <table width='60%'>
358 <tr>
359 <td colspan='2' class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'guardian ignored hosts'}</b></td>
360 </tr>
361 END
362 # Check if the guardian ignore file contains any content.
363 if (-s $ignorefile) {
364
365 # Open file and print contents.
366 open FILE, $ignorefile or die "Could not open $ignorefile";
367
368 my $id = 0;
369 my $col = "";
370
371 # Read file line by line and print out the elements.
372 while( my $ignored_element = <FILE> ) {
373
374 # Increase id number for each element in the ignore file.
375 $id++;
376
377 # Check if the id number is even or not.
378 if ($id % 2) {
379 $col="bgcolor='$color{'color22'}'";
380 } else {
381 $col="bgcolor='$color{'color20'}'";
382 }
383
384 print <<END;
385 <tr>
386 <td width='80%' class='base' $col>$ignored_element</td>
387 <td width='20%' align='center' $col>
388 <form method='post' name='frma$id' action='$ENV{'SCRIPT_NAME'}'>
389 <input type='image' name='$Lang::tr{'remove'}' src='/images/delete.gif' title='$Lang::tr{'remove'}' alt='$Lang::tr{'remove'}'>
390 <input type='hidden' name='ID' value='$id'>
391 <input type='hidden' name='ACTION' value='$Lang::tr{'remove'}'>
392 </form>
393 </td>
394 </tr>
395 END
396 }
397 close (FILE);
398 } else {
399 # Print notice that currently no elements are stored in the ignore file.
400 print "<tr>\n";
401 print "<td class='base' colspan='2'>$Lang::tr{'guardian no entries'}</td>\n";
402 print "</tr>\n";
403 }
404
405 print "</table>\n";
406
407 &Header::closebox();
408
409 # Section to add new elements to the ignore list.
410 print <<END;
411 <div class='post' align='center'>
412 <table width='60%'>
413 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
414 <tr>
415 <td width='30%'>$Lang::tr{'dnsforward add a new entry'}: </td>
416 <td width='50%'><input type='text' name='NEW_IGNORE_ENTRY' value='' size='24' /></td>
417 <td align='center' width='20%'><input type='submit' name='ACTION' value='$Lang::tr{'add'}' /></td>
418 </tr>
419 </form>
420 </table>
421 </div>
422 END
423 }
424
425 # Function to list currently bocked addresses from guardian and unblock them or add custom entries to block.
426 sub showBlockedBox() {
427 &Header::openbox('100%', 'center', $Lang::tr{'guardian blocked hosts'});
428
429 # Lauch helper to get chains from iptables.
430 open(FILE, "/usr/local/bin/guardianctrl get-chain |");
431
432 print <<END;
433 <table width='60%'>
434 <tr>
435 <td colspan='2' class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'guardian blocked hosts'}</b></td>
436 </tr>
437 END
438
439 my $id = 0;
440 my $address = "";
441 my $col = "";
442 my $interface = "";
443
444 # Read file line by line and print out the elements.
445 foreach my $line (<FILE>) {
446 # Skip descriptive lines.
447 next if ($line =~ /^Chain/);
448 next if ($line =~ /^ pkts/);
449
450 # Generate array based on the line content (seperator is a single or multiple space's)
451 my @comps = split(/\s{1,}/, $line);
452 my ($lead, $pkts, $bytes, $target, $prot, $opt, $in, $out, $source, $destination) = @comps;
453
454 # Assign different variable names.
455 my $blocked_host = $source;
456
457 # Increase id number for each element in the ignore file.
458 $id++;
459
460 # Check if the id number is even or not.
461 if ($id % 2) {
462 $col="bgcolor='$color{'color22'}'";
463 } else {
464 $col="bgcolor='$color{'color20'}'";
465 }
466
467 print <<END;
468 <tr>
469 <td width='80%' class='base' $col><a href='/cgi-bin/ipinfo.cgi?ip=$blocked_host'>$blocked_host</a></td>
470 <td width='20%' align='center' $col>
471 <form method='post' name='frmb$id' action='$ENV{'SCRIPT_NAME'}'>
472 <input type='image' name='$Lang::tr{'unblock'}' src='/images/delete.gif' title='$Lang::tr{'unblock'}' alt='$Lang::tr{'unblock'}'>
473 <input type='hidden' name='ADDRESS_UNBLOCK' value='$blocked_host'>
474 <input type='hidden' name='ACTION' value='$Lang::tr{'unblock'}'>
475 </form>
476 </td>
477 </tr>
478 END
479 }
480
481 close (FILE);
482
483 # If the loop only has been runs once the id still is "0", which means there are no
484 # additional entries (blocked hosts) in the iptables chain.
485 if ($id == 0) {
486
487 # Print notice that currently no hosts are blocked.
488 print "<tr>\n";
489 print "<td class='base' colspan='2'>$Lang::tr{'guardian no entries'}</td>\n";
490 print "</tr>\n";
491 }
492
493 print "</table>\n";
494
495 &Header::closebox();
496
497 # Section for a manual block of an IP-address.
498 print <<END;
499 <div class='post' align='center'>
500 <table width='60%'>
501 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
502 <tr>
503 <td width='30%'>$Lang::tr{'guardian block a host'}: </td>
504 <td width='40%'><input type='text' name='ADDRESS_BLOCK' value='' size='24' /></td>
505 <td align='center' width='15%'><input type='submit' name='ACTION' value='$Lang::tr{'block'}'></td>
506 <td align='center' width='15%'><input type='submit' name='ACTION' value='$Lang::tr{'unblock all'}'></td>
507 </tr>
508 </form>
509 </table>
510 </div>
511 END
512 }
513
514 &Header::closebigbox();
515 &Header::closepage();
516
517 # Function to check if guardian has been started.
518 # Grab process id and consumed memory if the daemon is running.
519 sub daemonstats() {
520 $memory = 0;
521 # for pid and memory
522 open(FILE, '/usr/local/bin/addonctrl guardian status | ');
523 @guardian = <FILE>;
524 close(FILE);
525 $string = join("", @guardian);
526 $string =~ s/[a-z_]//gi;
527 $string =~ s/\[[0-1]\;[0-9]+//gi;
528 $string =~ s/[\(\)\.]//gi;
529 $string =~ s/ //gi;
530 $string =~ s/\e//gi;
531 @pid = split(/\s/,$string);
532 if (open(FILE, "/proc/$pid[0]/statm")){
533 my $temp = <FILE>;
534 @memory = split(/ /,$temp);
535 close(FILE);
536 }
537 $memory+=$memory[0];
538 }
539
540 sub BuildConfiguration() {
541 my %settings = ();
542 &General::readhash("${General::swroot}/guardian/settings", \%settings);
543
544 my $configfile = "${General::swroot}/guardian/guardian.conf";
545
546 # We set this to 1 (enabled) to prevent guardian from blocking the ISP gateway.
547 my $HostGatewayByte = "1";
548
549 # Grab interface of red network zone.
550 my $red_interface = &General::get_red_interface();
551
552 # Open configfile for writing.
553 open(FILE, ">$configfile");
554
555 print FILE "Interface $red_interface\n";
556 print FILE "BlockOption $settings{'GUARDIAN_BLOCKINTERFACES'}\n";
557 print FILE "HostGatewayByte $HostGatewayByte\n";
558 print FILE "LogFile $settings{'GUARDIAN_LOGFILE'}\n";
559 print FILE "AlertFile $settings{'GUARDIAN_SNORT_ALERTFILE'}\n";
560 print FILE "IgnoreFile $ignorefile\n";
561 print FILE "TargetFile $targetfile\n";
562 print FILE "TimeLimit $settings{'GUARDIAN_BLOCKTIME'}\n";
563
564 close(FILE);
565
566 # Restart the service.
567 if ($settings{'GUARDIAN_ENABLED'} eq 'on') {
568 system("/usr/local/bin/guardianctrl restart &>/dev/null");
569 } else {
570 system("/usr/local/bin/guardianctrl stop &>/dev/null");
571 }
572 }