]>
git.ipfire.org Git - ipfire-2.x.git/blob - html/cgi-bin/guardian.cgi
2 ###############################################################################
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2016 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 ###############################################################################
23 use Locale
::Codes
::Country
;
26 # enable only the following on debugging purpose
28 #use CGI::Carp 'fatalsToBrowser';
30 require '/var/ipfire/general-functions.pl';
31 require "${General::swroot}/lang.pl";
32 require "${General::swroot}/header.pl";
34 #workaround to suppress a warning when a variable is used only once
37 ${Header
::colourgreen
}
48 # Path to the guardian.ignore file.
49 my $ignorefile ='/var/ipfire/guardian/guardian.ignore';
51 # Hash which contains the supported modules and the
52 # file locations on IPFire systems.
53 my %module_file_locations = (
54 "HTTPD" => "/var/log/httpd/error_log",
55 "OWNCLOUD" => "/var/owncloud/data/owncloud.log",
56 "SNORT" => "/var/log/snort/alert",
57 "SSH" => "/var/log/messages",
60 our %netsettings = ();
61 &General
::readhash
("${General::swroot}/ethernet/settings", \
%netsettings);
64 our %mainsettings = ();
65 &General
::readhash
("${General::swroot}/main/settings", \
%mainsettings);
66 &General
::readhash
("/srv/web/ipfire/html/themes/".$mainsettings{'THEME'}."/include/colors.txt", \
%color);
68 # Pakfire meta file for owncloud.
69 # (File exists when the addon is installed.)
70 my $owncloud_meta = "/opt/pakfire/db/installed/meta-owncloud";
74 my $settingsfile = "${General::swroot}/guardian/settings";
75 my $ignoredfile = "${General::swroot}/guardian/ignored";
77 # Create empty settings and ignoredfile if they do not exist yet.
78 unless (-e
"$settingsfile") { system("touch $settingsfile"); }
79 unless (-e
"$ignoredfile") { system("touch $ignoredfile"); }
84 $settings{'ACTION'} = '';
86 $settings{'GUARDIAN_ENABLED'} = 'off';
87 $settings{'GUARDIAN_MONITOR_SNORT'} = 'on';
88 $settings{'GUARDIAN_MONITOR_SSH'} = 'on';
89 $settings{'GUARDIAN_MONITOR_HTTPD'} = 'on';
90 $settings{'GUARDIAN_MONITOR_OWNCLOUD'} = '';
91 $settings{'GUARDIAN_LOG_FACILITY'} = 'syslog';
92 $settings{'GUARDIAN_LOGLEVEL'} = 'info';
93 $settings{'GUARDIAN_BLOCKCOUNT'} = '3';
94 $settings{'GUARDIAN_BLOCKTIME'} = '86400';
95 $settings{'GUARDIAN_FIREWALL_ACTION'} = 'DROP';
96 $settings{'GUARDIAN_LOGFILE'} = '/var/log/guardian/guardian.log';
97 $settings{'GUARDIAN_SNORT_PRIORITY_LEVEL'} = '3';
99 # Default settings for owncloud if installed.
100 if ( -e
"$owncloud_meta") {
101 $settings{'GUARDIAN_MONITOR_OWNCLOUD'} = 'off';
104 my $errormessage = '';
106 &Header
::showhttpheaders
();
109 &Header
::getcgihash
(\
%settings);
111 # Check if guardian is running and grab some stats.
115 ## Perform input checks and save settings.
117 if ($settings{'ACTION'} eq $Lang::tr
{'save'}) {
118 # Check for valid blocktime.
119 unless(($settings{'GUARDIAN_BLOCKTIME'} =~ /^\d+$/) && ($settings{'GUARDIAN_BLOCKTIME'} ne "0")) {
120 $errormessage = "$Lang::tr{'guardian invalid blocktime'}";
123 # Check if the blockcount is valid.
124 unless(($settings{'GUARDIAN_BLOCKCOUNT'} =~ /^\d+$/) && ($settings{'GUARDIAN_BLOCKCOUNT'} ne "0")) {
125 $errormessage = "$Lang::tr{'guardian invalid blockcount'}";
129 unless($settings{'GUARDIAN_LOGFILE'} =~ /^[a-zA-Z0-9\.\/]+$/) {
130 $errormessage = "$Lang::tr{'guardian invalid logfile'}";
133 # Only continue if no error message has been set.
134 if($errormessage eq '') {
135 # Write configuration settings to file.
136 &General
::writehash
("${General::swroot}/guardian/settings", \
%settings);
138 # Update configuration files.
139 &BuildConfiguration
();
142 ## Add/edit an entry to the ignore file.
144 } elsif (($settings{'ACTION'} eq $Lang::tr
{'add'}) || ($settings{'ACTION'} eq $Lang::tr
{'update'})) {
146 # Check if any input has been performed.
147 if ($settings{'IGNORE_ENTRY_ADDRESS'} ne '') {
149 # Check if the given input is no valid IP-address or IP-address with subnet, display an error message.
150 if ((!&General
::validip
($settings{'IGNORE_ENTRY_ADDRESS'})) && (!&General
::validipandmask
($settings{'IGNORE_ENTRY_ADDRESS'}))) {
151 $errormessage = "$Lang::tr{'guardian invalid address or subnet'}";
154 $errormessage = "$Lang::tr{'guardian empty input'}";
157 # Go further if there was no error.
158 if ($errormessage eq '') {
163 # Assign hash values.
164 my $new_entry_address = $settings{'IGNORE_ENTRY_ADDRESS'};
165 my $new_entry_remark = $settings{'IGNORE_ENTRY_REMARK'};
167 # Read-in ignoredfile.
168 &General
::readhasharray
($ignoredfile, \
%ignored);
170 # Check if we should edit an existing entry and got an ID.
171 if (($settings{'ACTION'} eq $Lang::tr
{'update'}) && ($settings{'ID'})) {
172 # Assin the provided id.
173 $id = $settings{'ID'};
175 # Undef the given ID.
176 undef($settings{'ID'});
178 # Grab the configured status of the corresponding entry.
179 $status = $ignored{$id}[2];
181 # Each newly added entry automatically should be enabled.
184 # Generate the ID for the new entry.
186 # Sort the keys by their ID and store them in an array.
187 my @keys = sort { $a <=> $b } keys %ignored;
189 # Reverse the key array.
190 my @reversed = reverse(@keys);
192 # Obtain the last used id.
193 my $last_id = @reversed[0];
195 # Increase the last id by one and use it as id for the new entry.
199 # Add/Modify the entry to/in the ignored hash.
200 $ignored{$id} = ["$new_entry_address", "$new_entry_remark", "$status"];
202 # Write the changed ignored hash to the ignored file.
203 &General
::writehasharray
($ignoredfile, \
%ignored);
205 # Regenerate the ignore file.
206 &GenerateIgnoreFile
();
209 # Check if guardian is running.
211 # Send reload command through socket connection.
212 &Guardian
::Socket
::Client
("reload-ignore-list");
215 ## Toggle Enabled/Disabled for an existing entry on the ignore list.
218 } elsif ($settings{'ACTION'} eq $Lang::tr
{'toggle enable disable'}) {
221 # Only go further, if an ID has been passed.
222 if ($settings{'ID'}) {
223 # Assign the given ID.
224 my $id = $settings{'ID'};
226 # Undef the given ID.
227 undef($settings{'ID'});
229 # Read-in ignoredfile.
230 &General
::readhasharray
($ignoredfile, \
%ignored);
232 # Grab the configured status of the corresponding entry.
233 my $status = $ignored{$id}[2];
236 if ($status eq "disabled") {
239 $status = "disabled";
242 # Modify the status of the existing entry.
243 $ignored{$id} = ["$ignored{$id}[0]", "$ignored{$id}[1]", "$status"];
245 # Write the changed ignored hash to the ignored file.
246 &General
::writehasharray
($ignoredfile, \
%ignored);
248 # Regenerate the ignore file.
249 &GenerateIgnoreFile
();
251 # Check if guardian is running.
253 # Send reload command through socket connection.
254 &Guardian
::Socket
::Client
("reload-ignore-list");
258 ## Remove entry from ignore list.
260 } elsif ($settings{'ACTION'} eq $Lang::tr
{'remove'}) {
263 # Read-in ignoredfile.
264 &General
::readhasharray
($ignoredfile, \
%ignored);
266 # Drop entry from the hash.
267 delete($ignored{$settings{'ID'}});
269 # Undef the given ID.
270 undef($settings{'ID'});
272 # Write the changed ignored hash to the ignored file.
273 &General
::writehasharray
($ignoredfile, \
%ignored);
275 # Regenerate the ignore file.
276 &GenerateIgnoreFile
();
278 # Check if guardian is running.
280 # Send reload command through socket connection.
281 &Guardian
::Socket
::Client
("reload-ignore-list");
284 ## Block a user given address or subnet.
286 } elsif ($settings{'ACTION'} eq $Lang::tr
{'block'}) {
288 # Assign some temporary variables used for input validation.
289 my $input = $settings{'ADDRESS_BLOCK'};
290 my $green = $netsettings{'GREEN_ADDRESS'};
291 my $blue = $netsettings{'BLUE_ADDRESS'};
292 my $orange = $netsettings{'ORANGE_ADDRESS'};
293 my $red = $netsettings{'RED_ADDRESS'};
296 my $gatewayfile = "${General::swroot}/red/remote-ipaddress";
297 my $dns1file = "${General::swroot}/red/dns1";
298 my $dns2file = "${General::swroot}/red/dns2";
300 # Get gateway address.
301 my $gateway = &_get_address_from_file
($gatewayfile);
303 # Get addresses from the used dns servers.
304 my $dns1 = &_get_address_from_file
($dns1file);
305 my $dns2 = &_get_address_from_file
($dns2file);
307 # Check if any input has been performed.
309 $errormessage = "$Lang::tr{'guardian empty input'}";
312 # Check if the given input is localhost (127.0.0.1).
313 elsif ($input eq "127.0.0.1") {
314 $errormessage = "$Lang::tr{'guardian blocking of this address is not allowed'}";
317 # Check if the given input is anywhere (0.0.0.0).
318 elsif ($input eq "0.0.0.0") {
319 $errormessage = "$Lang::tr{'guardian blocking of this address is not allowed'}";
322 # Check if the given input is one of the interface addresses or our gateway.
323 elsif ($input eq "$green" || $input eq "$blue" || $input eq "$orange" || $input eq "$red" || $input eq "$gateway" || $input eq "$dns1" || $input eq "$dns2") {
324 $errormessage = "$Lang::tr{'guardian blocking of this address is not allowed'}";
327 # Check if the given input is a valid IP address.
328 elsif (!&General
::validip
($input)) {
329 $errormessage = "$Lang::tr{'guardian invalid address or subnet'}";
332 # Go further if there was no error.
333 if ($errormessage eq '') {
334 my $block = $settings{'ADDRESS_BLOCK'};
336 # Send command to block the specified address through socket connection.
337 &Guardian
::Socket
::Client
("block $block");
340 ## Unblock address or subnet.
342 } elsif ($settings{'ACTION'} eq $Lang::tr
{'unblock'}) {
344 # Check if no empty input has been performed.
345 if ($settings{'ADDRESS_UNBLOCK'} ne '') {
347 # Check if the given input is no valid IP-address or IP-address with subnet, display an error message.
348 if ((!&General
::validip
($settings{'ADDRESS_UNBLOCK'})) && (!&General
::validipandmask
($settings{'ADDRESS_UNBLOCK'}))) {
349 $errormessage = "$Lang::tr{'guardian invalid address or subnet'}";
353 $errormessage = "$Lang::tr{'guardian empty input'}";
356 # Go further if there was no error.
357 if ($errormessage eq '') {
358 my $unblock = $settings{'ADDRESS_UNBLOCK'};
360 # Send command to unblock the given address through socket connection.
361 &Guardian
::Socket
::Client
("unblock $unblock");
366 } elsif ($settings{'ACTION'} eq $Lang::tr
{'unblock all'}) {
368 # Send flush command through socket connection.
369 &Guardian
::Socket
::Client
("flush");
372 # Load settings from files.
373 &General
::readhash
("${General::swroot}/guardian/settings", \
%settings);
374 &General
::readhasharray
("${General::swroot}/guardian/ignored", \
%ignored);
376 # Call functions to generate whole page.
380 # Display area only if guardian is running.
381 if ( ($memory != 0) && ($pid > 0) ) {
385 # Function to display the status of guardian and allow base configuration.
390 $checked{'GUARDIAN_ENABLED'}{'on'} = '';
391 $checked{'GUARDIAN_ENABLED'}{'off'} = '';
392 $checked{'GUARDIAN_ENABLED'}{$settings{'GUARDIAN_ENABLED'}} = 'checked';
393 $checked{'GUARDIAN_MONITOR_SNORT'}{'off'} = '';
394 $checked{'GUARDIAN_MONITOR_SNORT'}{'on'} = '';
395 $checked{'GUARDIAN_MONITOR_SNORT'}{$settings{'GUARDIAN_MONITOR_SNORT'}} = "checked='checked'";
396 $checked{'GUARDIAN_MONITOR_SSH'}{'off'} = '';
397 $checked{'GUARDIAN_MONITOR_SSH'}{'on'} = '';
398 $checked{'GUARDIAN_MONITOR_SSH'}{$settings{'GUARDIAN_MONITOR_SSH'}} = "checked='checked'";
399 $checked{'GUARDIAN_MONITOR_HTTPD'}{'off'} = '';
400 $checked{'GUARDIAN_MONITOR_HTTPD'}{'on'} = '';
401 $checked{'GUARDIAN_MONITOR_HTTPD'}{$settings{'GUARDIAN_MONITOR_HTTPD'}} = "checked='checked'";
402 $checked{'GUARDIAN_MONITOR_OWNCLOUD'}{'off'} = '';
403 $checked{'GUARDIAN_MONITOR_OWNCLOUD'}{'on'} = '';
404 $checked{'GUARDIAN_MONITOR_OWNCLOUD'}{$settings{'GUARDIAN_MONITOR_OWNCLOUD'}} = "checked='checked'";
406 $selected{'GUARDIAN_LOG_FACILITY'}{$settings{'GUARDIAN_LOG_FACILITY'}} = 'selected';
407 $selected{'GUARDIAN_LOGLEVEL'}{$settings{'GUARDIAN_LOGLEVEL'}} = 'selected';
408 $selected{'GUARDIAN_SNORT_PRIORITY_LEVEL'}{$settings{'GUARDIAN_SNORT_PRIORITY_LEVEL'}} = 'selected';
409 $selected{'GUARDIAN_FIREWALL_ACTION'}{$settings{'GUARDIAN_FIREWALL_ACTION'}} = 'selected';
411 &Header
::openpage
($Lang::tr
{'guardian configuration'}, 1, '');
412 &Header
::openbigbox
('100%', 'left', '', $errormessage);
414 # Print errormessage if there is one.
416 &Header
::openbox
('100%', 'left', $Lang::tr
{'error messages'});
417 print "<font class='base'>$errormessage </font>\n";
424 var update_options = function() {
426 var logfacility = \$("#GUARDIAN_LOG_FACILITY").val();
427 var loglevel = \$("#GUARDIAN_LOGLEVEL").val();
429 if (logfacility === undefined)
432 if (loglevel === undefined)
435 // Show / Hide input for specifying the path to the logfile.
436 if (logfacility === "file") {
437 \$(".GUARDIAN_LOGFILE").show();
439 \$(".GUARDIAN_LOGFILE").hide();
442 // Show / Hide loglevel debug if the facility is set to syslog.
443 if (logfacility === "syslog") {
444 \$("#loglevel_debug").hide();
446 \$("#loglevel_debug").show();
449 // Show / Hide logfacility syslog if the loglevel is set to debug.
450 if (loglevel === "debug") {
451 \$("#logfacility_syslog").hide();
453 \$("#logfacility_syslog").show();
457 \$(document).ready(function() {
458 \$("#GUARDIAN_LOG_FACILITY").change(update_options);
459 \$("#GUARDIAN_LOGLEVEL").change(update_options);
462 // Show / Hide snort priority level option, based if
463 // snort is enabled / disabled.
464 if (\$('input[name=GUARDIAN_MONITOR_SNORT]:checked').val() == 'on') {
465 \$('.GUARDIAN_SNORT_PRIORITY_LEVEL').show();
467 \$('.GUARDIAN_SNORT_PRIORITY_LEVEL').hide();
470 // Show/Hide snort priority level when GUARDIAN_MONITOR_SNORT get changed.
471 \$('input[name=GUARDIAN_MONITOR_SNORT]').change(function() {
472 \$('.GUARDIAN_SNORT_PRIORITY_LEVEL').toggle();
480 # Draw current guardian state.
481 &Header
::openbox
('100%', 'center', $Lang::tr
{'guardian'});
483 # Get current status of guardian.
487 # Display some useful information related to guardian, if daemon is running.
488 if ( ($memory != 0) && ($pid > 0) ){
490 <table width='95%' cellspacing='0' class='tbl'>
492 <th bgcolor='$color{'color20'}' colspan='3' align='left'><strong>$Lang::tr{'guardian service'}</strong></th>
495 <td class='base'>$Lang::tr{'guardian daemon'}</td>
496 <td align='center' colspan='2' width='75%' bgcolor='${Header::colourgreen}'><font color='white'><strong>$Lang::tr{'running'}</strong></font></td>
499 <td class='base'></td>
500 <td bgcolor='$color{'color20'}' align='center'><strong>PID</strong></td>
501 <td bgcolor='$color{'color20'}' align='center'><strong>$Lang::tr{'memory'}</strong></td>
504 <td class='base'></td>
505 <td bgcolor='$color{'color22'}' align='center'>$pid</td>
506 <td bgcolor='$color{'color22'}' align='center'>$memory KB</td>
511 # Otherwise display a hint that the service is not launched.
513 <table width='95%' cellspacing='0' class='tbl'>
515 <th bgcolor='$color{'color20'}' colspan='3' align='left'><strong>$Lang::tr{'guardian service'}</strong></th>
518 <td class='base'>$Lang::tr{'guardian daemon'}</td>
519 <td align='center' width='75%' bgcolor='${Header::colourred}'><font color='white'><strong>$Lang::tr{'stopped'}</strong></font></td>
527 # Draw elements for guardian configuration.
528 &Header
::openbox
('100%', 'center', $Lang::tr
{'guardian configuration'});
531 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
535 <td colspan='2' class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'guardian common settings'}</b></td>
539 <td width='25%' class='base'>$Lang::tr{'guardian enabled'}:</td>
540 <td><input type='checkbox' name='GUARDIAN_ENABLED' $checked{'GUARDIAN_ENABLED'}{'on'} /></td>
544 <td colspan='2'><br></td>
548 <td width='25%' class='base'>$Lang::tr{'guardian watch snort alertfile'}</td>
549 <td align='left'>on <input type='radio' name='GUARDIAN_MONITOR_SNORT' value='on' $checked{'GUARDIAN_MONITOR_SNORT'}{'on'} /> /
550 <input type='radio' name='GUARDIAN_MONITOR_SNORT' value='off' $checked{'GUARDIAN_MONITOR_SNORT'}{'off'} /> off</td>
554 <td width='25%' class='base'>$Lang::tr{'guardian block ssh brute-force'}</td>
555 <td align='left'>on <input type='radio' name='GUARDIAN_MONITOR_SSH' value='on' $checked{'GUARDIAN_MONITOR_SSH'}{'on'} /> /
556 <input type='radio' name='GUARDIAN_MONITOR_SSH' value='off' $checked{'GUARDIAN_MONITOR_SSH'}{'off'} /> off</td>
560 <td width='25%' class='base'>$Lang::tr{'guardian block httpd brute-force'}</td>
561 <td align='left'>on <input type='radio' name='GUARDIAN_MONITOR_HTTPD' value='on' $checked{'GUARDIAN_MONITOR_HTTPD'}{'on'} /> /
562 <input type='radio' name='GUARDIAN_MONITOR_HTTPD' value='off' $checked{'GUARDIAN_MONITOR_HTTPD'}{'off'} /> off</td>
566 # Display owncloud checkbox when the addon is installed.
567 if ( -e
"$owncloud_meta" ) {
569 print"<td width='25%' class='base'>$Lang::tr{'guardian block owncloud brute-force'}</td>\n";
570 print"<td align='left'>on <input type='radio' name='GUARDIAN_MONITOR_OWNCLOUD' value='on' $checked{'GUARDIAN_MONITOR_OWNCLOUD'}{'on'} /> /\n";
571 print"<input type='radio' name='GUARDIAN_MONITOR_OWNCLOUD' value='off' $checked{'GUARDIAN_MONITOR_OWNCLOUD'}{'off'} /> off</td>\n";
576 <td colspan='2'><br></td>
580 <td align='left' width='20%'>$Lang::tr{'guardian logfacility'}:</td>
581 <td width='25%'><select id='GUARDIAN_LOG_FACILITY' name='GUARDIAN_LOG_FACILITY'>
582 <option id='logfacility_syslog' value='syslog' $selected{'GUARDIAN_LOG_FACILITY'}{'syslog'}>$Lang::tr{'guardian logtarget_syslog'}</option>
583 <option id='logfacility_file' value='file' $selected{'GUARDIAN_LOG_FACILITY'}{'file'}>$Lang::tr{'guardian logtarget_file'}</option>
584 <option id='logfacility_console' value='console' $selected{'GUARDIAN_LOG_FACILITY'}{'console'}>$Lang::tr{'guardian logtarget_console'}</option>
587 <td align='left' width='20%'>$Lang::tr{'guardian loglevel'}:</td>
588 <td width='25%'><select id='GUARDIAN_LOGLEVEL' name='GUARDIAN_LOGLEVEL'>
589 <option id='loglevel_off' value='off' $selected{'GUARDIAN_LOGLEVEL'}{'off'}>$Lang::tr{'guardian loglevel_off'}</option>
590 <option id='loglevel_info' value='info' $selected{'GUARDIAN_LOGLEVEL'}{'info'}>$Lang::tr{'guardian loglevel_info'}</option>
591 <option id='loglevel_debug' value='debug' $selected{'GUARDIAN_LOGLEVEL'}{'debug'}>$Lang::tr{'guardian loglevel_debug'}</option>
595 <tr class="GUARDIAN_LOGFILE">
596 <td colspan='2'><br></td>
599 <tr class="GUARDIAN_LOGFILE">
600 <td width='25%' class='base'>$Lang::tr{'guardian logfile'}:</td>
601 <td><input type='text' name='GUARDIAN_LOGFILE' value='$settings{'GUARDIAN_LOGFILE'}' size='30' /></td>
604 <tr class="GUARDIAN_SNORT_PRIORITY_LEVEL">
605 <td colspan='2'><br></td>
608 <tr class="GUARDIAN_SNORT_PRIORITY_LEVEL">
609 <td align='left' width='20%'>$Lang::tr{'guardian priority level'}:</td>
610 <td><select name='GUARDIAN_SNORT_PRIORITY_LEVEL'>
611 <option value='1' $selected{'GUARDIAN_SNORT_PRIORITY_LEVEL'}{'1'}>$Lang::tr{'guardian priolevel_high'}</option>
612 <option value='2' $selected{'GUARDIAN_SNORT_PRIORITY_LEVEL'}{'2'}>$Lang::tr{'guardian priolevel_medium'}</option>
613 <option value='3' $selected{'GUARDIAN_SNORT_PRIORITY_LEVEL'}{'3'}>$Lang::tr{'guardian priolevel_low'}</option>
614 <option value='4' $selected{'GUARDIAN_SNORT_PRIORITY_LEVEL'}{'4'}>$Lang::tr{'guardian priolevel_very_low'}</option>
617 <td width='25%' class='base'>$Lang::tr{'guardian blockcount'}:</td>
618 <td><input type='text' name='GUARDIAN_BLOCKCOUNT' value='$settings{'GUARDIAN_BLOCKCOUNT'}' size='5' /></td>
622 <td colspan='2'><br></td>
626 <td width='25%' class='base'>$Lang::tr{'guardian firewallaction'}:</td>
627 <td><select name='GUARDIAN_FIREWALL_ACTION'>
628 <option value='DROP' $selected{'GUARDIAN_FIREWALL_ACTION'}{'DROP'}>Drop</option>
629 <option value='REJECT' $selected{'GUARDIAN_FIREWALL_ACTION'}{'REJECT'}>Reject</option>
632 <td width='25%' class='base'>$Lang::tr{'guardian blocktime'}:</td>
633 <td><input type='text' name='GUARDIAN_BLOCKTIME' value='$settings{'GUARDIAN_BLOCKTIME'}' size='10' /></td>
645 <td align='center'><input type='submit' name='ACTION' value='$Lang::tr{'save'}' /></td>
655 # Function to show elements of the guardian ignorefile and allow to add or remove single members of it.
656 sub showIgnoreBox
() {
657 &Header
::openbox
('100%', 'center', $Lang::tr
{'guardian ignored hosts'});
662 <td class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'ip address'}</b></td>
663 <td class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'remark'}</b></td>
664 <td class='base' colspan='3' bgcolor='$color{'color20'}'></td>
667 # Check if some hosts have been added to be ignored.
668 if (keys (%ignored)) {
671 # Loop through all entries of the hash.
672 while( (my $key) = each %ignored) {
673 # Assign data array positions to some nice variable names.
674 my $address = $ignored{$key}[0];
675 my $remark = $ignored{$key}[1];
676 my $status = $ignored{$key}[2];
678 # Check if the key (id) number is even or not.
679 if ($settings{'ID'} eq $key) {
680 $col="bgcolor='${Header::colouryellow}'";
682 $col="bgcolor='$color{'color22'}'";
684 $col="bgcolor='$color{'color20'}'";
687 # Choose icon for the checkbox.
691 # Check if the status is enabled and select the correct image and description.
692 if ($status eq 'enabled' ) {
694 $gdesc = $Lang::tr
{'click to disable'};
697 $gdesc = $Lang::tr
{'click to enable'};
702 <td width='20%' class='base' $col>$address</td>
703 <td width='65%' class='base' $col>$remark</td>
705 <td align='center' $col>
706 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
707 <input type='hidden' name='ACTION' value='$Lang::tr{'toggle enable disable'}' />
708 <input type='image' name='$Lang::tr{'toggle enable disable'}' src='/images/$gif' alt='$gdesc' title='$gdesc' />
709 <input type='hidden' name='ID' value='$key' />
713 <td align='center' $col>
714 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
715 <input type='hidden' name='ACTION' value='$Lang::tr{'edit'}' />
716 <input type='image' name='$Lang::tr{'edit'}' src='/images/edit.gif' alt='$Lang::tr{'edit'}' title='$Lang::tr{'edit'}' />
717 <input type='hidden' name='ID' value='$key' />
721 <td align='center' $col>
722 <form method='post' name='$key' action='$ENV{'SCRIPT_NAME'}'>
723 <input type='image' name='$Lang::tr{'remove'}' src='/images/delete.gif' title='$Lang::tr{'remove'}' alt='$Lang::tr{'remove'}'>
724 <input type='hidden' name='ID' value='$key'>
725 <input type='hidden' name='ACTION' value='$Lang::tr{'remove'}'>
732 # Print notice that currently no hosts are ignored.
734 print "<td class='base' colspan='2'>$Lang::tr{'guardian no entries'}</td>\n";
740 # Section to add new elements or edit existing ones.
750 # Assign correct headline and button text.
755 # Check if an ID (key) has been given, in this case an existing entry should be edited.
756 if ($settings{'ID'} ne '') {
757 $buttontext = $Lang::tr
{'update'};
758 print "<tr><td class='boldbase' colspan='3'><b>$Lang::tr{'update'}</b></td></tr>\n";
760 # Grab address and remark for the given key.
761 $entry_address = $ignored{$settings{'ID'}}[0];
762 $entry_remark = $ignored{$settings{'ID'}}[1];
764 $buttontext = $Lang::tr
{'add'};
765 print "<tr><td class='boldbase' colspan='3'><b>$Lang::tr{'dnsforward add a new entry'}</b></td></tr>\n";
769 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
770 <input type='hidden' name='ID' value='$settings{'ID'}'>
772 <td width='30%'>$Lang::tr{'ip address'}: </td>
773 <td width='50%'><input type='text' name='IGNORE_ENTRY_ADDRESS' value='$entry_address' size='24' /></td>
775 <td width='30%'>$Lang::tr{'remark'}: </td>
776 <td wicth='50%'><input type='text' name=IGNORE_ENTRY_REMARK value='$entry_remark' size='24' /></td>
777 <td align='center' width='20%'><input type='submit' name='ACTION' value='$buttontext' /></td>
787 # Function to list currently blocked addresses from guardian and unblock them or add custom entries to block.
788 sub showBlockedBox
() {
789 &Header
::openbox
('100%', 'center', $Lang::tr
{'guardian blocked hosts'});
794 <td colspan='2' class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'guardian blocked hosts'}</b></td>
798 # Launch function to get the currently blocked hosts.
799 my @blocked_hosts = &GetBlockedHosts
();
804 # Loop through our blocked hosts array.
805 foreach my $blocked_host (@blocked_hosts) {
807 # Increase id number for each element in the ignore file.
810 # Check if the id number is even or not.
812 $col="bgcolor='$color{'color22'}'";
814 $col="bgcolor='$color{'color20'}'";
819 <td width='80%' class='base' $col><a href='/cgi-bin/ipinfo.cgi?ip=$blocked_host'>$blocked_host</a></td>
820 <td width='20%' align='center' $col>
821 <form method='post' name='frmb$id' action='$ENV{'SCRIPT_NAME'}'>
822 <input type='image' name='$Lang::tr{'unblock'}' src='/images/delete.gif' title='$Lang::tr{'unblock'}' alt='$Lang::tr{'unblock'}'>
823 <input type='hidden' name='ADDRESS_UNBLOCK' value='$blocked_host'>
824 <input type='hidden' name='ACTION' value='$Lang::tr{'unblock'}'>
831 # If the loop only has been run once the id still is "0", which means there are no
832 # additional entries (blocked hosts) in the iptables chain.
835 # Print notice that currently no hosts are blocked.
837 print "<td class='base' colspan='2'>$Lang::tr{'guardian no entries'}</td>\n";
843 # Section for a manual block of an IP-address.
847 <table width='60%' border='0'>
848 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
850 <td width='30%'>$Lang::tr{'guardian block a host'}: </td>
851 <td width='40%'><input type='text' name='ADDRESS_BLOCK' value='' size='24' /></td>
852 <td align='center' width='15%'><input type='submit' name='ACTION' value='$Lang::tr{'block'}'></td>
853 <td align='center' width='15%'><input type='submit' name='ACTION' value='$Lang::tr{'unblock all'}'></td>
863 &Header
::closebigbox
();
864 &Header
::closepage
();
866 # Function to check if guardian has been started.
867 # Grab process id and consumed memory if the daemon is running.
871 open(FILE
, '/usr/local/bin/addonctrl guardian status | ');
874 $string = join("", @guardian);
875 $string =~ s/[a-z_]//gi;
876 $string =~ s/\[[0-1]\;[0-9]+//gi;
877 $string =~ s/[\(\)\.]//gi;
880 @pid = split(/\s/,$string);
881 if (open(FILE
, "/proc/$pid[0]/statm")){
883 @memory = split(/ /,$temp);
889 sub GetBlockedHosts
() {
890 # Create new, empty array.
893 # Launch helper to get chains from iptables.
894 system('/usr/local/bin/getipstat');
896 # Open temporary file which contains the chains and rules.
897 open (FILE
, '/var/tmp/iptables.txt');
899 # Loop through the entire file.
903 # Search for the guardian chain and extract
904 # the lines between it and the next empty line
905 # which is placed before the next firewall
907 if ($line =~ /^Chain GUARDIAN/ .. /^\s*$/) {
908 # Skip descriptive lines.
909 next if ($line =~ /^Chain/);
910 next if ($line =~ /^ pkts/);
912 # Generate array, based on the line content (separator is a single or multiple space)
913 my @comps = split(/\s{1,}/, $line);
914 my ($lead, $pkts, $bytes, $target, $prot, $opt, $in, $out, $source, $destination) = @comps;
916 # Assign different variable names.
917 my $blocked_host = $source;
919 # Add host to our hosts array.
921 push(@hosts, $blocked_host);
929 # Remove recently created temporary files of the "getipstat" binary.
930 system("rm -f /var/tmp/iptables.txt");
931 system("rm -f /var/tmp/iptablesmangle.txt");
932 system("rm -f /var/tmp/iptablesnat.txt");
934 # Convert entries, sort them, write back and store the sorted entries into new array.
935 my @sorted = map { $_->[0] }
936 sort { $a->[1] <=> $b->[1] }
937 map { [$_, int sprintf("%03.f%03.f%03.f%03.f", split(/\./, $_))] }
940 # Return our sorted list.
944 sub BuildConfiguration
() {
946 &General
::readhash
("${General::swroot}/guardian/settings", \
%settings);
948 my $configfile = "${General::swroot}/guardian/guardian.conf";
950 # Create the configfile if none exists yet.
951 unless (-e
"$configfile") { system("touch $configfile"); }
953 # Open configfile for writing.
954 open(FILE
, ">$configfile");
956 # Config file header.
957 print FILE
"# Autogenerated configuration file.\n";
958 print FILE
"# All user modifications will be overwritten.\n\n";
960 # Settings for the logging mechanism.
961 print FILE
"# Log settings.\n";
962 print FILE
"LogFacility = $settings{'GUARDIAN_LOG_FACILITY'}\n";
964 if ($settings{'GUARDIAN_LOG_FACILITY'} eq "file") {
965 print FILE
"LogFile = $settings{'GUARDIAN_LOGFILE'}\n";
968 print FILE
"LogLevel = $settings{'GUARDIAN_LOGLEVEL'}\n\n";
970 # IPFire related static settings.
971 print FILE
"# IPFire related settings.\n";
972 print FILE
"FirewallEngine = IPtables\n";
973 print FILE
"SocketOwner = nobody:nobody\n";
974 print FILE
"IgnoreFile = $ignorefile\n\n";
976 # Configured block values.
977 print FILE
"# Configured block settings.\n";
978 print FILE
"BlockCount = $settings{'GUARDIAN_BLOCKCOUNT'}\n";
979 print FILE
"BlockTime = $settings{'GUARDIAN_BLOCKTIME'}\n";
980 print FILE
"FirewallAction = $settings{'GUARDIAN_FIREWALL_ACTION'}\n\n";
983 # Loop through whole settings hash.
984 print FILE
"# Enabled modules.\n";
985 foreach my $option (keys %settings) {
986 # Search for enabled modules.
987 if ($option =~ /GUARDIAN_MONITOR_(.*)/) {
988 # Skip if module is not enabled.
989 next unless($settings{$option} eq "on");
991 # Skip module if no file location is available.
992 next unless(exists($module_file_locations{$1}));
994 # Add enabled module and defined path to the config file.
995 print FILE
"Monitor_$1 = $module_file_locations{$1}\n";
1000 print FILE
"\n# Module settings.\n";
1001 # Check if SNORT is enabled and add snort priority.
1002 if ($settings{'GUARDIAN_MONITOR_SNORT'} eq "on") {
1003 print FILE
"SnortPriorityLevel = $settings{'GUARDIAN_SNORT_PRIORITY_LEVEL'}\n";
1008 # Generate ignore file.
1009 &GenerateIgnoreFile
();
1011 # Check if guardian should be started or stopped.
1012 if($settings{'GUARDIAN_ENABLED'} eq 'on') {
1014 # Send reload command through socket connection.
1015 &Guardian
::Socket
::Client
("reload");
1018 system("/usr/local/bin/addonctrl guardian start &>/dev/null");
1022 system("/usr/local/bin/addonctrl guardian stop &>/dev/null");
1026 sub GenerateIgnoreFile
() {
1029 # Read-in ignoredfile.
1030 &General
::readhasharray
($ignoredfile, \
%ignored);
1032 # Create the guardian.ignore file if not exist yet.
1033 unless (-e
"$ignorefile") { system("touch $ignorefile"); }
1035 # Open ignorefile for writing.
1036 open(FILE
, ">$ignorefile");
1038 # Config file header.
1039 print FILE
"# Autogenerated configuration file.\n";
1040 print FILE
"# All user modifications will be overwritten.\n\n";
1042 # Add IFPire interfaces and gateway to the ignore file.
1044 # Assign some temporary variables for the IPFire interfaces.
1045 my $green = $netsettings{'GREEN_ADDRESS'};
1046 my $blue = $netsettings{'BLUE_ADDRESS'};
1047 my $orange = $netsettings{'ORANGE_ADDRESS'};
1049 # File declarations.
1050 my $public_address_file = "${General::swroot}/red/local-ipaddress";
1051 my $gatewayfile = "${General::swroot}/red/remote-ipaddress";
1052 my $dns1file = "${General::swroot}/red/dns1";
1053 my $dns2file = "${General::swroot}/red/dns2";
1055 # Write the obtained addresses to the ignore file.
1056 print FILE
"# IPFire local interfaces.\n";
1057 print FILE
"$green\n";
1059 # Check if a blue interface exists.
1062 print FILE
"$blue\n";
1065 # Check if an orange interface exists.
1067 # Add orange address.
1068 print FILE
"$orange\n";
1071 print FILE
"\n# IPFire red interface, gateway and used DNS-servers.\n";
1072 print FILE
"# Include the corresponding files to obtain the addresses.\n";
1073 print FILE
"Include_File = $public_address_file\n";
1074 print FILE
"Include_File = $gatewayfile\n";
1075 print FILE
"Include_File = $dns1file\n";
1076 print FILE
"Include_File = $dns2file\n";
1078 # Add all user defined hosts and networks to the ignore file.
1080 # Check if the hash contains any elements.
1081 if (keys (%ignored)) {
1083 print FILE
"\n# User defined hosts/networks.\n";
1085 # Loop through the entire hash and write the host/network
1086 # and remark to the ignore file.
1087 while ( (my $key) = each %ignored) {
1088 my $address = $ignored{$key}[0];
1089 my $remark = $ignored{$key}[1];
1090 my $status = $ignored{$key}[2];
1092 # Check if the status of the entry is "enabled".
1093 if ($status eq "enabled") {
1094 # Check if the address/network is valid.
1095 if ((&General
::validip
($address)) || (&General
::validipandmask
($address))) {
1096 # Write the remark to the file.
1097 print FILE
"# $remark\n";
1099 # Write the address/network to the ignore file.
1100 print FILE
"$address\n\n";
1109 # Private subfunction to obtain IP-addresses from given file names.
1111 sub _get_address_from_file
($) {
1114 # Check if the file exists.
1116 # Open the given file.
1117 open(FILE
, "$file") or die "Could not open $file.";
1119 # Obtain the address from the first line of the file.
1120 my $address = <FILE
>;
1128 # Check if the grabbed address is valid.
1129 if (&General
::validip
($address)) {
1130 # Return the address.