]>
git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blob - html/cgi-bin/guardian.cgi
2 ###############################################################################
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2014 IPFire Team <info@ipfire.org> #
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. #
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. #
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/>. #
20 ###############################################################################
25 # enable only the following on debugging purpose
27 use CGI
::Carp
'fatalsToBrowser';
29 require '/var/ipfire/general-functions.pl';
30 require "${General::swroot}/lang.pl";
31 require "${General::swroot}/header.pl";
33 #workaround to suppress a warning when a variable is used only once
34 my @dummy = ( ${Header
::colouryellow
} );
44 # Path to the guardian.ignore file.
45 my $ignorefile ='/var/ipfire/guardian/guardian.ignore';
47 # Path to the guardian.target file.
48 my $targetfile ='/var/ipfire/guardian/guardian.ignore';
50 our %netsettings = ();
51 &General
::readhash
("${General::swroot}/ethernet/settings", \
%netsettings);
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);
60 $settings{'GUARDIAN_ENABLED'} = 'off';
61 $settings{'GUARDIAN_BLOCKTIME'} = '86400';
62 $settings{'GUARDIAN_LOGFILE'} = '/var/log/guardian/guardian.log';
63 $settings{'GUARDIAN_SNORT_ALERTFILE'} = '/var/log/snort/alert';
65 $settings{'ACTION'} = '';
67 my $errormessage = '';
69 &Header
::showhttpheaders
();
72 &Header
::getcgihash
(\
%settings);
73 &Header
::getcgihash
(\
%cgiparams);
75 ## Perform input checks and save settings.
77 if ($settings{'ACTION'} eq $Lang::tr
{'save'}) {
79 # Check for valid blocktime.
80 if ($settings{'GUARDIAN_BLOCKTIME'} ne '') {
81 if (($settings{'GUARDIAN_BLOCKTIME'} !~ /^[0-9]+$/) || ($settings{'GUARDIAN_BLOCKTIME'} le '0')) {
82 $errormessage = "$Lang::tr{'guardian invalid blocktime'}";
87 if ($settings{'GUARDIAN_LOGFILE'} ne '') {
88 if ($settings{'GUARDIAN_LOGFILE'} !~ /^[a-zA-Z0-9\.\/]+$/) {
89 $errormessage = "$Lang::tr{'guardian invalid logfile'}";
93 # Check input for snort alert file.
94 if ($settings{'GUARDIAN_SNORT_ALERTFILE'} ne '') {
95 if ($settings{'GUARDIAN_SNORT_ALERTFILE'} !~ /^[a-zA-Z0-9\.\/]+$/) {
96 $errormessage = "$Lang::tr{'guardian invalid alertfile'}";
100 # Only continue if no error message has been set.
101 if ($errormessage eq '') {
102 # Write configuration settings to file.
103 &General
::writehash
("${General::swroot}/guardian/settings", \
%settings);
105 # Update configuration files.
106 &BuildConfiguration
();
109 ## Add a new entry to the ignore file.
111 } elsif ($settings{'ACTION'} eq $Lang::tr
{'add'}) {
113 # Check if any input has been performed.
114 if ($settings{'NEW_IGNORE_ENTRY'} ne '') {
116 # Check if the given input is no valid IP-address or IP-address with subnet, display an error message.
117 if ((!&General
::validip
($settings{'NEW_IGNORE_ENTRY'})) && (!&General
::validipandmask
($settings{'NEW_IGNORE_ENTRY'}))) {
118 $errormessage = "$Lang::tr{'guardian invalid address or subnet'}";
121 $errormessage = "$Lang::tr{'guardian empty input'}";
124 # Go further if there was no error.
125 if ($errormessage eq '') {
126 my $new_entry = $settings{'NEW_IGNORE_ENTRY'};
128 # Open file for appending the new entry.
129 open (FILE
, ">>$ignorefile") or die "Unable to open $ignorefile for writing";
131 # Write the new entry to the ignore file.
132 print FILE
"$new_entry\n";
136 ## Remove entry from ignore list.
138 } elsif ($settings{'ACTION'} eq $Lang::tr
{'remove'}) {
141 # Open ignorefile and read content.
142 open(FILE
, "<$ignorefile") or die "Unable to open $ignorefile.";
143 my @current = <FILE
>;
146 # Re-open ignorefile for writing.
147 open(FILE
, ">$ignorefile") or die "Unable to open $ignorefile for writing";
150 # Read line by line from ignorefile and write them back except the line with the given ID.
151 # So this line is missing in the new file and the entry has been deleted.
152 foreach my $line (@current) {
154 unless ($cgiparams{'ID'} eq $id) {
160 ## Block a user given address or subnet.
162 } elsif ($settings{'ACTION'} eq $Lang::tr
{'block'}) {
164 # Check if no empty input has been performed.
165 if ($settings{'ADDRESS_BLOCK'} ne '') {
167 # Check if the given input is no valid IP-address or IP-address with subnet, display an error message.
168 if ((!&General
::validip
($settings{'ADDRESS_BLOCK'})) && (!&General
::validipandmask
($settings{'ADDRESS_BLOCK'}))) {
169 $errormessage = "$Lang::tr{'guardian invalid address or subnet'}";
173 $errormessage = "$Lang::tr{'guardian empty input'}";
176 # Go further if there was no error.
177 if ($errormessage eq '') {
178 my $block = $settings{'ADDRESS_BLOCK'};
180 # Call helper to unblock address.
181 system("/usr/local/bin/guardianctrl block $block &>/dev/null");
184 ## Unblock address or subnet.
186 } elsif ($settings{'ACTION'} eq $Lang::tr
{'unblock'}) {
188 # Check if no empty input has been performed.
189 if ($settings{'ADDRESS_UNBLOCK'} ne '') {
191 # Check if the given input is no valid IP-address or IP-address with subnet, display an error message.
192 if ((!&General
::validip
($settings{'ADDRESS_UNBLOCK'})) && (!&General
::validipandmask
($settings{'ADDRESS_UNBLOCK'}))) {
193 $errormessage = "$Lang::tr{'guardian invalid address or subnet'}";
197 $errormessage = "$Lang::tr{'guardian empty input'}";
200 # Go further if there was no error.
201 if ($errormessage eq '') {
202 my $unblock = $settings{'ADDRESS_UNBLOCK'};
204 # Call helper to unblock address.
205 system("/usr/local/bin/guardianctrl unblock $unblock &>/dev/null");
210 } elsif ($settings{'ACTION'} eq $Lang::tr
{'unblock all'}) {
212 # Call helper to flush iptables chain from guardian.
213 system("/usr/local/bin/guardianctrl flush-chain &>/dev/null");
216 # Load settings from file.
217 &General
::readhash
("${General::swroot}/guardian/settings", \
%settings);
221 # Call functions to generate whole page.
225 # Display area only if guardian is running.
226 if ( ($memory != 0) && (@pid[0] ne "///") ) {
230 # Function to display the status of guardian and allow base configuration.
234 $checked{'GUARDIAN_ENABLED'}{'on'} = '';
235 $checked{'GUARDIAN_ENABLED'}{'off'} = '';
236 $checked{'GUARDIAN_ENABLED'}{$settings{'GUARDIAN_ENABLED'}} = 'checked';
238 &Header
::openpage
($Lang::tr
{'guardian configuration'}, 1, '');
239 &Header
::openbigbox
('100%', 'left', '', $errormessage);
241 # Print errormessage if there is one.
243 &Header
::openbox
('100%', 'left', $Lang::tr
{'error messages'});
244 print "<font class='base'>$errormessage </font>\n";
249 # Draw current guardian state.
250 &Header
::openbox
('100%', 'center', $Lang::tr
{'guardian'});
252 # Check if guardian is running and grab some stats.
255 # Display some useful information related to guardian, if daemon is running.
256 if ( ($memory != 0) && (@pid[0] ne "///") ){
258 <table width='95%' cellspacing='0' class='tbl'>
260 <th bgcolor='$color{'color20'}' colspan='3' align='left'><strong>$Lang::tr{'guardian service'}</strong></th>
263 <td class='base'>$Lang::tr{'guardian daemon'}</td>
264 <td align='center' colspan='2' width='75%' bgcolor='${Header::colourgreen}'><font color='white'><strong>$Lang::tr{'running'}</strong></font></td>
267 <td class='base'></td>
268 <td bgcolor='$color{'color20'}' align='center'><strong>PID</strong></td>
269 <td bgcolor='$color{'color20'}' align='center'><strong>$Lang::tr{'memory'}</strong></td>
272 <td class='base'></td>
273 <td bgcolor='$color{'color22'}' align='center'>@pid[0]</td>
274 <td bgcolor='$color{'color22'}' align='center'>$memory KB</td>
279 # Otherwise display a hint that the service is not launched.
281 <table width='95%' cellspacing='0' class='tbl'>
283 <th bgcolor='$color{'color20'}' colspan='3' align='left'><strong>$Lang::tr{'guardian service'}</strong></th>
286 <td class='base'>$Lang::tr{'guardian daemon'}</td>
287 <td align='center' width='75%' bgcolor='${Header::colourred}'><font color='white'><strong>$Lang::tr{'stopped'}</strong></font></td>
295 # Draw elements for guardian configuration.
296 &Header
::openbox
('100%', 'center', $Lang::tr
{'guardian configuration'});
299 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
303 <td colspan='2' class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'guardian common settings'}</b></td>
306 <td width='20%' class='base'>$Lang::tr{'guardian enabled'}:</td>
307 <td><input type='checkbox' name='GUARDIAN_ENABLED' $checked{'GUARDIAN_ENABLED'}{'on'} /></td>
310 <td width='20%' class='base'>$Lang::tr{'guardian blocktime'}:</td>
311 <td><input type='text' name='GUARDIAN_BLOCKTIME' value='$settings{'GUARDIAN_BLOCKTIME'}' size='10' /></td>
314 <td width='20%' class='base'>$Lang::tr{'guardian logfile'}:</td>
315 <td><input type='text' name='GUARDIAN_LOGFILE' value='$settings{'GUARDIAN_LOGFILE'}' size='30' /></td>
318 <td width='20%' class='base'>$Lang::tr{'guardian snort alertfile'}:</td>
319 <td><input type='text' name='GUARDIAN_SNORT_ALERTFILE' value='$settings{'GUARDIAN_SNORT_ALERTFILE'}' size='30' /></td>
330 <td align='center'><input type='submit' name='ACTION' value='$Lang::tr{'save'}' /></td>
340 # Function to show elements of the guardian ignorefile and allow to add or remove single members of it.
341 sub showIgnoreBox
() {
342 &Header
::openbox
('100%', 'center', $Lang::tr
{'guardian ignored hosts'});
347 <td colspan='2' class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'guardian ignored hosts'}</b></td>
350 # Check if the guardian ignore file contains any content.
351 if (-s
$ignorefile) {
353 # Open file and print contents.
354 open FILE
, $ignorefile or die "Could not open $ignorefile";
359 # Read file line by line and print out the elements.
360 while( my $ignored_element = <FILE
> ) {
362 # Increase id number for each element in the ignore file.
365 # Check if the id number is even or not.
367 $col="bgcolor='$color{'color22'}'";
369 $col="bgcolor='$color{'color20'}'";
374 <td width='80%' class='base' $col>$ignored_element</td>
375 <td width='20%' align='center' $col>
376 <form method='post' name='frma$id' action='$ENV{'SCRIPT_NAME'}'>
377 <input type='image' name='$Lang::tr{'remove'}' src='/images/delete.gif' title='$Lang::tr{'remove'}' alt='$Lang::tr{'remove'}'>
378 <input type='hidden' name='ID' value='$id'>
379 <input type='hidden' name='ACTION' value='$Lang::tr{'remove'}'>
387 # Print notice that currently no elements are stored in the ignore file.
389 print "<td class='base' colspan='2'>$Lang::tr{'guardian no entries'}</td>\n";
395 # Section to add new elements to the ignore list.
400 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
402 <td width='30%'>$Lang::tr{'dnsforward add a new entry'}: </td>
403 <td width='50%'><input type='text' name='NEW_IGNORE_ENTRY' value='' size='24' /></td>
404 <td align='center' width='20%'><input type='submit' name='ACTION' value='$Lang::tr{'add'}' /></td>
414 # Function to list currently bocked addresses from guardian and unblock them or add custom entries to block.
415 sub showBlockedBox
() {
416 &Header
::openbox
('100%', 'center', $Lang::tr
{'guardian blocked hosts'});
421 <td colspan='2' class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'guardian blocked hosts'}</b></td>
425 # Lauch function to get the currently blocked hosts.
426 my @blocked_hosts = &GetBlockedHosts
();
431 # Loop through our blocked hosts array.
432 foreach my $blocked_host (@blocked_hosts) {
434 # Increase id number for each element in the ignore file.
437 # Check if the id number is even or not.
439 $col="bgcolor='$color{'color22'}'";
441 $col="bgcolor='$color{'color20'}'";
446 <td width='80%' class='base' $col><a href='/cgi-bin/ipinfo.cgi?ip=$blocked_host'>$blocked_host</a></td>
447 <td width='20%' align='center' $col>
448 <form method='post' name='frmb$id' action='$ENV{'SCRIPT_NAME'}'>
449 <input type='image' name='$Lang::tr{'unblock'}' src='/images/delete.gif' title='$Lang::tr{'unblock'}' alt='$Lang::tr{'unblock'}'>
450 <input type='hidden' name='ADDRESS_UNBLOCK' value='$blocked_host'>
451 <input type='hidden' name='ACTION' value='$Lang::tr{'unblock'}'>
458 # If the loop only has been runs once the id still is "0", which means there are no
459 # additional entries (blocked hosts) in the iptables chain.
462 # Print notice that currently no hosts are blocked.
464 print "<td class='base' colspan='2'>$Lang::tr{'guardian no entries'}</td>\n";
470 # Section for a manual block of an IP-address.
474 <table width='60%' border='0'>
475 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
477 <td width='30%'>$Lang::tr{'guardian block a host'}: </td>
478 <td width='40%'><input type='text' name='ADDRESS_BLOCK' value='' size='24' /></td>
479 <td align='center' width='15%'><input type='submit' name='ACTION' value='$Lang::tr{'block'}'></td>
480 <td align='center' width='15%'><input type='submit' name='ACTION' value='$Lang::tr{'unblock all'}'></td>
490 &Header
::closebigbox
();
491 &Header
::closepage
();
493 # Function to check if guardian has been started.
494 # Grab process id and consumed memory if the daemon is running.
498 open(FILE
, '/usr/local/bin/addonctrl guardian status | ');
501 $string = join("", @guardian);
502 $string =~ s/[a-z_]//gi;
503 $string =~ s/\[[0-1]\;[0-9]+//gi;
504 $string =~ s/[\(\)\.]//gi;
507 @pid = split(/\s/,$string);
508 if (open(FILE
, "/proc/$pid[0]/statm")){
510 @memory = split(/ /,$temp);
516 sub GetBlockedHosts
() {
518 # Create new, empty array.
521 # Lauch helper to get chains from iptables.
522 open(FILE
, "/usr/local/bin/guardianctrl get-chain |");
524 # Read file line by line and print out the elements.
525 foreach my $line (<FILE
>) {
527 # Skip descriptive lines.
528 next if ($line =~ /^Chain/);
529 next if ($line =~ /^ pkts/);
531 # Generate array, based on the line content (seperator is a single or multiple space's)
532 my @comps = split(/\s{1,}/, $line);
533 my ($lead, $pkts, $bytes, $target, $prot, $opt, $in, $out, $source, $destination) = @comps;
535 # Assign different variable names.
536 my $blocked_host = $source;
538 # Add host to our hosts array.
539 push(@hosts, $blocked_host);
542 # Convert entries, sort them, write back and store the sorted entries into new array.
543 my @sorted = map { $_->[0] }
544 sort { $a->[1] <=> $b->[1] }
545 map { [$_, int sprintf("%03.f%03.f%03.f%03.f", split(/\./, $_))] }
548 # Return our sorted list.
552 sub BuildConfiguration
() {
554 &General
::readhash
("${General::swroot}/guardian/settings", \
%settings);
556 my $configfile = "${General::swroot}/guardian/guardian.conf";
558 # We set this to 1 (enabled) to prevent guardian from blocking the ISP gateway.
559 my $HostGatewayByte = "1";
561 # Grab interface of red network zone.
562 my $red_interface = &General
::get_red_interface
();
564 # Open configfile for writing.
565 open(FILE
, ">$configfile");
567 print FILE
"Interface $red_interface\n";
568 print FILE
"HostGatewayByte $HostGatewayByte\n";
569 print FILE
"LogFile $settings{'GUARDIAN_LOGFILE'}\n";
570 print FILE
"AlertFile $settings{'GUARDIAN_SNORT_ALERTFILE'}\n";
571 print FILE
"IgnoreFile $ignorefile\n";
572 print FILE
"TargetFile $targetfile\n";
573 print FILE
"TimeLimit $settings{'GUARDIAN_BLOCKTIME'}\n";
577 # Restart the service.
578 if ($settings{'GUARDIAN_ENABLED'} eq 'on') {
579 system("/usr/local/bin/guardianctrl restart &>/dev/null");
581 system("/usr/local/bin/guardianctrl stop &>/dev/null");