]>
git.ipfire.org Git - people/dweismueller/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_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';
66 $settings{'ACTION'} = '';
68 my $errormessage = '';
70 &Header
::showhttpheaders
();
73 &Header
::getcgihash
(\
%settings);
74 &Header
::getcgihash
(\
%cgiparams);
76 ## Perform input checks and save settings.
78 if ($settings{'ACTION'} eq $Lang::tr
{'save'}) {
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'}";
88 if ($settings{'GUARDIAN_LOGFILE'} ne '') {
89 if ($settings{'GUARDIAN_LOGFILE'} !~ /^[a-zA-Z0-9\.\/]+$/) {
90 $errormessage = "$Lang::tr{'guardian invalid logfile'}";
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'}";
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);
106 # Update configuration files.
107 &BuildConfiguration
();
110 ## Add a new entry to the ignore file.
112 } elsif ($settings{'ACTION'} eq $Lang::tr
{'add'}) {
114 # Check if any input has been performed.
115 if ($settings{'NEW_IGNORE_ENTRY'} ne '') {
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'}";
122 $errormessage = "$Lang::tr{'guardian empty input'}";
125 # Go further if there was no error.
126 if ($errormessage eq '') {
127 my $new_entry = $settings{'NEW_IGNORE_ENTRY'};
129 # Open file for appending the new entry.
130 open (FILE
, ">>$ignorefile") or die "Unable to open $ignorefile for writing";
132 # Write the new entry to the ignore file.
133 print FILE
"$new_entry\n";
137 ## Remove entry from ignore list.
139 } elsif ($settings{'ACTION'} eq $Lang::tr
{'remove'}) {
142 # Open ignorefile and read content.
143 open(FILE
, "<$ignorefile") or die "Unable to open $ignorefile.";
144 my @current = <FILE
>;
147 # Re-open ignorefile for writing.
148 open(FILE
, ">$ignorefile") or die "Unable to open $ignorefile for writing";
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) {
155 unless ($cgiparams{'ID'} eq $id) {
161 ## Block a user given address or subnet.
163 } elsif ($settings{'ACTION'} eq $Lang::tr
{'block'}) {
165 # Check if no empty input has been performed.
166 if ($settings{'ADDRESS_BLOCK'} ne '') {
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'}";
174 $errormessage = "$Lang::tr{'guardian empty input'}";
177 # Go further if there was no error.
178 if ($errormessage eq '') {
179 my $block = $settings{'ADDRESS_BLOCK'};
181 # Call helper to unblock address.
182 system("/usr/local/bin/guardianctrl block $block &>/dev/null");
185 ## Unblock address or subnet.
187 } elsif ($settings{'ACTION'} eq $Lang::tr
{'unblock'}) {
189 # Check if no empty input has been performed.
190 if ($settings{'ADDRESS_UNBLOCK'} ne '') {
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'}";
198 $errormessage = "$Lang::tr{'guardian empty input'}";
201 # Go further if there was no error.
202 if ($errormessage eq '') {
203 my $unblock = $settings{'ADDRESS_UNBLOCK'};
205 # Call helper to unblock address.
206 system("/usr/local/bin/guardianctrl unblock $unblock &>/dev/null");
211 } elsif ($settings{'ACTION'} eq $Lang::tr
{'unblock all'}) {
213 # Call helper to flush iptables chain from guardian.
214 system("/usr/local/bin/guardianctrl flush-chain &>/dev/null");
217 # Load settings from file.
218 &General
::readhash
("${General::swroot}/guardian/settings", \
%settings);
222 # Call functions to generate whole page.
226 # Display area only if guardian is running.
227 if ( ($memory != 0) && (@pid[0] ne "///") ) {
231 # Function to display the status of guardian and allow base configuration.
235 $checked{'GUARDIAN_ENABLED'}{'on'} = '';
236 $checked{'GUARDIAN_ENABLED'}{'off'} = '';
237 $checked{'GUARDIAN_ENABLED'}{$settings{'GUARDIAN_ENABLED'}} = 'checked';
239 $checked{'GUARDIAN_BLOCKINTERFACES'}{'default'} = '';
240 $checked{'GUARDIAN_BLOCKINTERFACES'}{'all'} = '';
241 $checked{'GUARDIAN_BLOCKINTERFACES'}{$settings{'GUARDIAN_BLOCKINTERFACES'}} = "checked='checked'";
244 &Header
::openpage
($Lang::tr
{'guardian configuration'}, 1, '');
245 &Header
::openbigbox
('100%', 'left', '', $errormessage);
247 # Print errormessage if there is one.
249 &Header
::openbox
('100%', 'left', $Lang::tr
{'error messages'});
250 print "<font class='base'>$errormessage </font>\n";
255 # Draw current guardian state.
256 &Header
::openbox
('100%', 'center', $Lang::tr
{'guardian'});
258 # Check if guardian is running and grab some stats.
261 # Display some useful information related to guardian, if daemon is running.
262 if ( ($memory != 0) && (@pid[0] ne "///") ){
264 <table width='95%' cellspacing='0' class='tbl'>
266 <th bgcolor='$color{'color20'}' colspan='3' align='left'><strong>$Lang::tr{'guardian service'}</strong></th>
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>
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>
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>
285 # Otherwise display a hint that the service is not launched.
287 <table width='95%' cellspacing='0' class='tbl'>
289 <th bgcolor='$color{'color20'}' colspan='3' align='left'><strong>$Lang::tr{'guardian service'}</strong></th>
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>
301 # Draw elements for guardian configuration.
302 &Header
::openbox
('100%', 'center', $Lang::tr
{'guardian configuration'});
305 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
309 <td colspan='5' class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'guardian common settings'}</b></td>
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>
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>
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>
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>
342 <td align='center'><input type='submit' name='ACTION' value='$Lang::tr{'save'}' /></td>
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'});
359 <td colspan='2' class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'guardian ignored hosts'}</b></td>
362 # Check if the guardian ignore file contains any content.
363 if (-s
$ignorefile) {
365 # Open file and print contents.
366 open FILE
, $ignorefile or die "Could not open $ignorefile";
371 # Read file line by line and print out the elements.
372 while( my $ignored_element = <FILE
> ) {
374 # Increase id number for each element in the ignore file.
377 # Check if the id number is even or not.
379 $col="bgcolor='$color{'color22'}'";
381 $col="bgcolor='$color{'color20'}'";
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'}'>
399 # Print notice that currently no elements are stored in the ignore file.
401 print "<td class='base' colspan='2'>$Lang::tr{'guardian no entries'}</td>\n";
409 # Section to add new elements to the ignore list.
411 <div class='post' align='center'>
413 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
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>
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'});
429 # Lauch helper to get chains from iptables.
430 open(FILE
, "/usr/local/bin/guardianctrl get-chain |");
435 <td colspan='2' class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'guardian blocked hosts'}</b></td>
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/);
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;
454 # Assign different variable names.
455 my $blocked_host = $source;
457 # Increase id number for each element in the ignore file.
460 # Check if the id number is even or not.
462 $col="bgcolor='$color{'color22'}'";
464 $col="bgcolor='$color{'color20'}'";
469 <td width='80%' class='base' $col>$blocked_host</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'}'>
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.
487 # Print notice that currently no hosts are blocked.
489 print "<td class='base' colspan='2'>$Lang::tr{'guardian no entries'}</td>\n";
497 # Section for a manual block of an IP-address.
499 <div class='post' align='center'>
501 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
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>
514 &Header
::closebigbox
();
515 &Header
::closepage
();
517 # Function to check if guardian has been started.
518 # Grab process id and consumed memory if the daemon is running.
522 open(FILE
, '/usr/local/bin/addonctrl guardian status | ');
525 $string = join("", @guardian);
526 $string =~ s/[a-z_]//gi;
527 $string =~ s/\[[0-1]\;[0-9]+//gi;
528 $string =~ s/[\(\)\.]//gi;
531 @pid = split(/\s/,$string);
532 if (open(FILE
, "/proc/$pid[0]/statm")){
534 @memory = split(/ /,$temp);
540 sub BuildConfiguration
() {
542 &General
::readhash
("${General::swroot}/guardian/settings", \
%settings);
544 my $configfile = "${General::swroot}/guardian/guardian.conf";
546 # We set this to 1 (enabled) to prevent guardian from blocking the ISP gateway.
547 my $HostGatewayByte = "1";
549 # Grab interface of red network zone.
550 my $red_interface = &General
::get_red_interface
();
552 # Open configfile for writing.
553 open(FILE
, ">$configfile");
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";
566 # Restart the service.
567 if ($settings{'GUARDIAN_ENABLED'} eq 'on') {
568 system("/usr/local/bin/guardianctrl restart &>/dev/null");
570 system("/usr/local/bin/guardianctrl stop &>/dev/null");