]>
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) 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 $settings{'ACTION'} = '';
76 $settings{'GUARDIAN_ENABLED'} = 'off';
77 $settings{'GUARDIAN_MONITOR_SNORT'} = 'on';
78 $settings{'GUARDIAN_MONITOR_SSH'} = 'on';
79 $settings{'GUARDIAN_MONITOR_HTTPD'} = 'on';
80 $settings{'GUARDIAN_MONITOR_OWNCLOUD'} = '';
81 $settings{'GUARDIAN_LOG_FACILITY'} = 'syslog';
82 $settings{'GUARDIAN_LOGLEVEL'} = 'info';
83 $settings{'GUARDIAN_BLOCKCOUNT'} = '3';
84 $settings{'GUARDIAN_BLOCKTIME'} = '86400';
85 $settings{'GUARDIAN_LOGFILE'} = '/var/log/guardian/guardian.log';
86 $settings{'GUARDIAN_SNORT_PRIORITY_LEVEL'} = '3';
88 # Default settings for owncloud if installed.
89 if ( -e
"$owncloud_meta") {
90 $settings{'GUARDIAN_MONITOR_OWNCLOUD'} = 'off';
93 my $errormessage = '';
95 &Header
::showhttpheaders
();
98 &Header
::getcgihash
(\
%settings);
100 # Check if guardian is running and grab some stats.
104 ## Perform input checks and save settings.
106 if ($settings{'ACTION'} eq $Lang::tr
{'save'}) {
107 # Check for valid blocktime.
108 unless(($settings{'GUARDIAN_BLOCKTIME'} =~ /^\d+$/) && ($settings{'GUARDIAN_BLOCKTIME'} ne "0")) {
109 $errormessage = "$Lang::tr{'guardian invalid blocktime'}";
112 # Check if the bloccount is valid.
113 unless(($settings{'GUARDIAN_BLOCKCOUNT'} =~ /^\d+$/) && ($settings{'GUARDIAN_BLOCKCOUNT'} ne "0")) {
114 $errormessage = "$Lang::tr{'guardian invalid blockcount'}";
118 unless($settings{'GUARDIAN_LOGFILE'} =~ /^[a-zA-Z0-9\.\/]+$/) {
119 $errormessage = "$Lang::tr{'guardian invalid logfile'}";
122 # Only continue if no error message has been set.
123 if($errormessage eq '') {
124 # Write configuration settings to file.
125 &General
::writehash
("${General::swroot}/guardian/settings", \
%settings);
127 # Update configuration files.
128 &BuildConfiguration
();
131 ## Add a new entry to the ignore file.
133 } elsif ($settings{'ACTION'} eq $Lang::tr
{'add'}) {
135 # Check if any input has been performed.
136 if ($settings{'NEW_IGNORE_ENTRY'} ne '') {
138 # Check if the given input is no valid IP-address or IP-address with subnet, display an error message.
139 if ((!&General
::validip
($settings{'NEW_IGNORE_ENTRY'})) && (!&General
::validipandmask
($settings{'NEW_IGNORE_ENTRY'}))) {
140 $errormessage = "$Lang::tr{'guardian invalid address or subnet'}";
143 $errormessage = "$Lang::tr{'guardian empty input'}";
146 # Go further if there was no error.
147 if ($errormessage eq '') {
148 my $new_entry = $settings{'NEW_IGNORE_ENTRY'};
150 # Open file for appending the new entry.
151 open (FILE
, ">>$ignorefile") or die "Unable to open $ignorefile for writing";
153 # Write the new entry to the ignore file.
154 print FILE
"$new_entry\n";
158 # Check if guardian is running.
160 # Send reload command through socket connection.
161 &Guardian
::Socket
::Client
("reload");
164 ## Remove entry from ignore list.
166 } elsif ($settings{'ACTION'} eq $Lang::tr
{'remove'}) {
169 # Open ignorefile and read content.
170 open(FILE
, "<$ignorefile") or die "Unable to open $ignorefile.";
171 my @current = <FILE
>;
174 # Re-open ignorefile for writing.
175 open(FILE
, ">$ignorefile") or die "Unable to open $ignorefile for writing";
178 # Read line by line from ignorefile and write them back except the line with the given ID.
179 # So this line is missing in the new file and the entry has been deleted.
180 foreach my $line (@current) {
182 unless ($settings{'ID'} eq $id) {
188 # Check if guardian is running.
190 # Send reload command through socket connection.
191 &Guardian
::Socket
::Client
("reload");
194 ## Block a user given address or subnet.
196 } elsif ($settings{'ACTION'} eq $Lang::tr
{'block'}) {
198 # Assign some temporary variables used for input validation.
199 my $input = $settings{'ADDRESS_BLOCK'};
200 my $green = $netsettings{'GREEN_ADDRESS'};
201 my $blue = $netsettings{'BLUE_ADDRESS'};
202 my $orange = $netsettings{'ORANGE_ADDRESS'};
203 my $red = $netsettings{'RED_ADDRESS'};
205 # Get gateway address.
206 my $gateway = &General
::get_gateway
();
208 # Check if any input has been performed.
210 $errormessage = "$Lang::tr{'guardian empty input'}";
213 # Check if the given input is localhost (127.0.0.1).
214 elsif ($input eq "127.0.0.1") {
215 $errormessage = "$Lang::tr{'guardian blocking of this address is not allowed'}";
218 # Check if the given input is anywhere (0.0.0.0).
219 elsif ($input eq "0.0.0.0") {
220 $errormessage = "$Lang::tr{'guardian blocking of this address is not allowed'}";
223 # Check if the given input is one of the interface addresses or our gateway.
224 elsif ($input eq "$green" || $input eq "$blue" || $input eq "$orange" || $input eq "$red" || $input eq "$gateway") {
225 $errormessage = "$Lang::tr{'guardian blocking of this address is not allowed'}";
228 # Check if the given input is a valid IP address.
229 elsif (!&General
::validip
($input)) {
230 $errormessage = "$Lang::tr{'guardian invalid address or subnet'}";
233 # Go further if there was no error.
234 if ($errormessage eq '') {
235 my $block = $settings{'ADDRESS_BLOCK'};
237 # Send command to block the specified address through socket connection.
238 &Guardian
::Socket
::Client
("block $block");
241 ## Unblock address or subnet.
243 } elsif ($settings{'ACTION'} eq $Lang::tr
{'unblock'}) {
245 # Check if no empty input has been performed.
246 if ($settings{'ADDRESS_UNBLOCK'} ne '') {
248 # Check if the given input is no valid IP-address or IP-address with subnet, display an error message.
249 if ((!&General
::validip
($settings{'ADDRESS_UNBLOCK'})) && (!&General
::validipandmask
($settings{'ADDRESS_UNBLOCK'}))) {
250 $errormessage = "$Lang::tr{'guardian invalid address or subnet'}";
254 $errormessage = "$Lang::tr{'guardian empty input'}";
257 # Go further if there was no error.
258 if ($errormessage eq '') {
259 my $unblock = $settings{'ADDRESS_UNBLOCK'};
261 # Send command to unblock the given address through socket connection.
262 &Guardian
::Socket
::Client
("unblock $unblock");
267 } elsif ($settings{'ACTION'} eq $Lang::tr
{'unblock all'}) {
269 # Send flush command through socket connection.
270 &Guardian
::Socket
::Client
("flush");
273 # Load settings from file.
274 &General
::readhash
("${General::swroot}/guardian/settings", \
%settings);
276 # Call functions to generate whole page.
280 # Display area only if guardian is running.
281 if ( ($memory != 0) && ($pid > 0) ) {
285 # Function to display the status of guardian and allow base configuration.
290 $checked{'GUARDIAN_ENABLED'}{'on'} = '';
291 $checked{'GUARDIAN_ENABLED'}{'off'} = '';
292 $checked{'GUARDIAN_ENABLED'}{$settings{'GUARDIAN_ENABLED'}} = 'checked';
293 $checked{'GUARDIAN_MONITOR_SNORT'}{'off'} = '';
294 $checked{'GUARDIAN_MONITOR_SNORT'}{'on'} = '';
295 $checked{'GUARDIAN_MONITOR_SNORT'}{$settings{'GUARDIAN_MONITOR_SNORT'}} = "checked='checked'";
296 $checked{'GUARDIAN_MONITOR_SSH'}{'off'} = '';
297 $checked{'GUARDIAN_MONITOR_SSH'}{'on'} = '';
298 $checked{'GUARDIAN_MONITOR_SSH'}{$settings{'GUARDIAN_MONITOR_SSH'}} = "checked='checked'";
299 $checked{'GUARDIAN_MONITOR_HTTPD'}{'off'} = '';
300 $checked{'GUARDIAN_MONITOR_HTTPD'}{'on'} = '';
301 $checked{'GUARDIAN_MONITOR_HTTPD'}{$settings{'GUARDIAN_MONITOR_HTTPD'}} = "checked='checked'";
302 $checked{'GUARDIAN_MONITOR_OWNCLOUD'}{'off'} = '';
303 $checked{'GUARDIAN_MONITOR_OWNCLOUD'}{'on'} = '';
304 $checked{'GUARDIAN_MONITOR_OWNCLOUD'}{$settings{'GUARDIAN_MONITOR_OWNCLOUD'}} = "checked='checked'";
306 $selected{'GUARDIAN_LOG_FACILITY'}{$settings{'GUARDIAN_LOG_FACILITY'}} = 'selected';
307 $selected{'GUARDIAN_LOGLEVEL'}{$settings{'GUARDIAN_LOGLEVEL'}} = 'selected';
308 $selected{'GUARDIAN_SNORT_PRIORITY_LEVEL'}{$settings{'GUARDIAN_SNORT_PRIORITY_LEVEL'}} = 'selected';
310 &Header
::openpage
($Lang::tr
{'guardian configuration'}, 1, '');
311 &Header
::openbigbox
('100%', 'left', '', $errormessage);
313 # Print errormessage if there is one.
315 &Header
::openbox
('100%', 'left', $Lang::tr
{'error messages'});
316 print "<font class='base'>$errormessage </font>\n";
321 # Draw current guardian state.
322 &Header
::openbox
('100%', 'center', $Lang::tr
{'guardian'});
324 # Get current status of guardian.
328 # Display some useful information related to guardian, if daemon is running.
329 if ( ($memory != 0) && ($pid > 0) ){
331 <table width='95%' cellspacing='0' class='tbl'>
333 <th bgcolor='$color{'color20'}' colspan='3' align='left'><strong>$Lang::tr{'guardian service'}</strong></th>
336 <td class='base'>$Lang::tr{'guardian daemon'}</td>
337 <td align='center' colspan='2' width='75%' bgcolor='${Header::colourgreen}'><font color='white'><strong>$Lang::tr{'running'}</strong></font></td>
340 <td class='base'></td>
341 <td bgcolor='$color{'color20'}' align='center'><strong>PID</strong></td>
342 <td bgcolor='$color{'color20'}' align='center'><strong>$Lang::tr{'memory'}</strong></td>
345 <td class='base'></td>
346 <td bgcolor='$color{'color22'}' align='center'>$pid</td>
347 <td bgcolor='$color{'color22'}' align='center'>$memory KB</td>
352 # Otherwise display a hint that the service is not launched.
354 <table width='95%' cellspacing='0' class='tbl'>
356 <th bgcolor='$color{'color20'}' colspan='3' align='left'><strong>$Lang::tr{'guardian service'}</strong></th>
359 <td class='base'>$Lang::tr{'guardian daemon'}</td>
360 <td align='center' width='75%' bgcolor='${Header::colourred}'><font color='white'><strong>$Lang::tr{'stopped'}</strong></font></td>
368 # Draw elements for guardian configuration.
369 &Header
::openbox
('100%', 'center', $Lang::tr
{'guardian configuration'});
372 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
376 <td colspan='2' class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'guardian common settings'}</b></td>
379 <td width='20%' class='base'>$Lang::tr{'guardian enabled'}:</td>
380 <td><input type='checkbox' name='GUARDIAN_ENABLED' $checked{'GUARDIAN_ENABLED'}{'on'} /></td>
383 <td colspan='2'><br></td>
386 <td width='20%' class='base'>$Lang::tr{'guardian watch snort alertfile'}</td>
387 <td align='left'>on <input type='radio' name='GUARDIAN_MONITOR_SNORT' value='on' $checked{'GUARDIAN_MONITOR_SNORT'}{'on'} /> /
388 <input type='radio' name='GUARDIAN_MONITOR_SNORT' value='off' $checked{'GUARDIAN_MONITOR_SNORT'}{'off'} /> off</td>
391 <td width='20%' class='base'>$Lang::tr{'guardian block ssh brute-force'}</td>
392 <td align='left'>on <input type='radio' name='GUARDIAN_MONITOR_SSH' value='on' $checked{'GUARDIAN_MONITOR_SSH'}{'on'} /> /
393 <input type='radio' name='GUARDIAN_MONITOR_SSH' value='off' $checked{'GUARDIAN_MONITOR_SSH'}{'off'} /> off</td>
396 <td width='20%' class='base'>$Lang::tr{'guardian block httpd brute-force'}</td>
397 <td align='left'>on <input type='radio' name='GUARDIAN_MONITOR_HTTPD' value='on' $checked{'GUARDIAN_MONITOR_HTTPD'}{'on'} /> /
398 <input type='radio' name='GUARDIAN_MONITOR_HTTPD' value='off' $checked{'GUARDIAN_MONITOR_HTTPD'}{'off'} /> off</td>
401 # Display owncloud checkbox when the addon is installed.
402 if ( -e
"$owncloud_meta" ) {
404 print"<td width='20%' class='base'>$Lang::tr{'guardian block owncloud brute-force'}</td>\n";
405 print"<td align='left'>on <input type='radio' name='GUARDIAN_MONITOR_OWNCLOUD' value='on' $checked{'GUARDIAN_MONITOR_OWNCLOUD'}{'on'} /> /\n";
406 print"<input type='radio' name='GUARDIAN_MONITOR_OWNCLOUD' value='off' $checked{'GUARDIAN_MONITOR_OWNCLOUD'}{'off'} /> off</td>\n";
411 <td colspan='2'><br></td>
414 <td align='left' width='20%'>$Lang::tr{'guardian logfacility'}:</td>
415 <td><select name='GUARDIAN_LOG_FACILITY'>
416 <option value='syslog' $selected{'GUARDIAN_LOG_FACILITY'}{'syslog'}>syslog</option>
417 <option value='file' $selected{'GUARDIAN_LOG_FACILITY'}{'file'}>file</option>
418 <option value='console' $selected{'GUARDIAN_LOG_FACILITY'}{'console'}>console</option>
422 <td colspan='2'><br></td>
425 <td align='left' width='20%'>$Lang::tr{'guardian loglevel'}:</td>
426 <td><select name='GUARDIAN_LOGLEVEL'>
427 <option value='off' $selected{'GUARDIAN_LOGLEVEL'}{'off'}>off</option>
428 <option value='info' $selected{'GUARDIAN_LOGLEVEL'}{'info'}>info</option>
429 <option value='debug' $selected{'GUARDIAN_LOGLEVEL'}{'debug'}>debug</option>
433 <td colspan='2'><br></td>
436 <td align='left' width='20%'>$Lang::tr{'guardian priority level'}:</td>
437 <td><select name='GUARDIAN_SNORT_PRIORITY_LEVEL'>
438 <option value='1' $selected{'GUARDIAN_SNORT_PRIORITY_LEVEL'}{'1'}>1</option>
439 <option value='2' $selected{'GUARDIAN_SNORT_PRIORITY_LEVEL'}{'2'}>2</option>
440 <option value='3' $selected{'GUARDIAN_SNORT_PRIORITY_LEVEL'}{'3'}>3</option>
441 <option value='4' $selected{'GUARDIAN_SNORT_PRIORITY_LEVEL'}{'4'}>4</option>
445 <td colspan='2'><br></td>
448 <td width='20%' class='base'>$Lang::tr{'guardian blockcount'}:</td>
449 <td><input type='text' name='GUARDIAN_BLOCKCOUNT' value='$settings{'GUARDIAN_BLOCKCOUNT'}' size='5' /></td>
452 <td width='20%' class='base'>$Lang::tr{'guardian blocktime'}:</td>
453 <td><input type='text' name='GUARDIAN_BLOCKTIME' value='$settings{'GUARDIAN_BLOCKTIME'}' size='10' /></td>
456 <td width='20%' class='base'>$Lang::tr{'guardian logfile'}:</td>
457 <td><input type='text' name='GUARDIAN_LOGFILE' value='$settings{'GUARDIAN_LOGFILE'}' size='30' /></td>
468 <td align='center'><input type='submit' name='ACTION' value='$Lang::tr{'save'}' /></td>
478 # Function to show elements of the guardian ignorefile and allow to add or remove single members of it.
479 sub showIgnoreBox
() {
480 &Header
::openbox
('100%', 'center', $Lang::tr
{'guardian ignored hosts'});
485 <td colspan='2' class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'guardian ignored hosts'}</b></td>
488 # Check if the guardian ignore file contains any content.
489 if (-s
$ignorefile) {
491 # Open file and print contents.
492 open FILE
, $ignorefile or die "Could not open $ignorefile";
497 # Read file line by line and print out the elements.
498 while( my $ignored_element = <FILE
> ) {
500 # Increase id number for each element in the ignore file.
503 # Check if the id number is even or not.
505 $col="bgcolor='$color{'color22'}'";
507 $col="bgcolor='$color{'color20'}'";
512 <td width='80%' class='base' $col>$ignored_element</td>
513 <td width='20%' align='center' $col>
514 <form method='post' name='$id' action='$ENV{'SCRIPT_NAME'}'>
515 <input type='image' name='$Lang::tr{'remove'}' src='/images/delete.gif' title='$Lang::tr{'remove'}' alt='$Lang::tr{'remove'}'>
516 <input type='hidden' name='ID' value='$id'>
517 <input type='hidden' name='ACTION' value='$Lang::tr{'remove'}'>
525 # Print notice that currently no elements are stored in the ignore file.
527 print "<td class='base' colspan='2'>$Lang::tr{'guardian no entries'}</td>\n";
533 # Section to add new elements to the ignore list.
538 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
540 <td width='30%'>$Lang::tr{'dnsforward add a new entry'}: </td>
541 <td width='50%'><input type='text' name='NEW_IGNORE_ENTRY' value='' size='24' /></td>
542 <td align='center' width='20%'><input type='submit' name='ACTION' value='$Lang::tr{'add'}' /></td>
552 # Function to list currently bocked addresses from guardian and unblock them or add custom entries to block.
553 sub showBlockedBox
() {
554 &Header
::openbox
('100%', 'center', $Lang::tr
{'guardian blocked hosts'});
559 <td colspan='2' class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'guardian blocked hosts'}</b></td>
563 # Lauch function to get the currently blocked hosts.
564 my @blocked_hosts = &GetBlockedHosts
();
569 # Loop through our blocked hosts array.
570 foreach my $blocked_host (@blocked_hosts) {
572 # Increase id number for each element in the ignore file.
575 # Check if the id number is even or not.
577 $col="bgcolor='$color{'color22'}'";
579 $col="bgcolor='$color{'color20'}'";
584 <td width='80%' class='base' $col><a href='/cgi-bin/ipinfo.cgi?ip=$blocked_host'>$blocked_host</a></td>
585 <td width='20%' align='center' $col>
586 <form method='post' name='frmb$id' action='$ENV{'SCRIPT_NAME'}'>
587 <input type='image' name='$Lang::tr{'unblock'}' src='/images/delete.gif' title='$Lang::tr{'unblock'}' alt='$Lang::tr{'unblock'}'>
588 <input type='hidden' name='ADDRESS_UNBLOCK' value='$blocked_host'>
589 <input type='hidden' name='ACTION' value='$Lang::tr{'unblock'}'>
596 # If the loop only has been runs once the id still is "0", which means there are no
597 # additional entries (blocked hosts) in the iptables chain.
600 # Print notice that currently no hosts are blocked.
602 print "<td class='base' colspan='2'>$Lang::tr{'guardian no entries'}</td>\n";
608 # Section for a manual block of an IP-address.
612 <table width='60%' border='0'>
613 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
615 <td width='30%'>$Lang::tr{'guardian block a host'}: </td>
616 <td width='40%'><input type='text' name='ADDRESS_BLOCK' value='' size='24' /></td>
617 <td align='center' width='15%'><input type='submit' name='ACTION' value='$Lang::tr{'block'}'></td>
618 <td align='center' width='15%'><input type='submit' name='ACTION' value='$Lang::tr{'unblock all'}'></td>
628 &Header
::closebigbox
();
629 &Header
::closepage
();
631 # Function to check if guardian has been started.
632 # Grab process id and consumed memory if the daemon is running.
636 open(FILE
, '/usr/local/bin/addonctrl guardian status | ');
639 $string = join("", @guardian);
640 $string =~ s/[a-z_]//gi;
641 $string =~ s/\[[0-1]\;[0-9]+//gi;
642 $string =~ s/[\(\)\.]//gi;
645 @pid = split(/\s/,$string);
646 if (open(FILE
, "/proc/$pid[0]/statm")){
648 @memory = split(/ /,$temp);
654 sub GetBlockedHosts
() {
655 # Create new, empty array.
658 # Lauch helper to get chains from iptables.
659 system('/usr/local/bin/getipstat');
661 # Open temporary file which contains the chains and rules.
662 open (FILE
, '/srv/web/ipfire/html/iptables.txt');
664 # Loop through the entire file.
668 # Search for the guardian chain and extract
669 # the lines between it and the next empty line
670 # which is placed before the next firewall
672 if ($line =~ /^Chain GUARDIAN/ .. /^\s*$/) {
673 # Skip descriptive lines.
674 next if ($line =~ /^Chain/);
675 next if ($line =~ /^ pkts/);
677 # Generate array, based on the line content (seperator is a single or multiple space's)
678 my @comps = split(/\s{1,}/, $line);
679 my ($lead, $pkts, $bytes, $target, $prot, $opt, $in, $out, $source, $destination) = @comps;
681 # Assign different variable names.
682 my $blocked_host = $source;
684 # Add host to our hosts array.
686 push(@hosts, $blocked_host);
694 # Remove recently created temporary files of the "getipstat" binary.
695 system(rm
-f
"/srv/web/ipfire/html/iptables.txt");
696 system(rm
-f
"/srv/web/ipfire/html/iptablesmangle.txt");
697 system(rm
-f
"/srv/web/ipfire/html/iptablesnat.txt");
699 # Convert entries, sort them, write back and store the sorted entries into new array.
700 my @sorted = map { $_->[0] }
701 sort { $a->[1] <=> $b->[1] }
702 map { [$_, int sprintf("%03.f%03.f%03.f%03.f", split(/\./, $_))] }
705 # Return our sorted list.
709 sub BuildConfiguration
() {
711 &General
::readhash
("${General::swroot}/guardian/settings", \
%settings);
713 my $configfile = "${General::swroot}/guardian/guardian.conf";
715 # Open configfile for writing.
716 open(FILE
, ">$configfile");
718 # Config file header.
719 print FILE
"# Autogenerated configuration file.\n";
720 print FILE
"# All user modifications will be overwritten.\n\n";
722 # Settings for the logging mechanism.
723 print FILE
"# Log settings.\n";
724 print FILE
"LogFacility = $settings{'GUARDIAN_LOG_FACILITY'}\n";
726 if ($settings{'GUARDIAN_LOG_FACILITY'} eq "file") {
727 print FILE
"LogFile = $settings{'GUARDIAN_LOGFILE'}\n";
730 print FILE
"LogLevel = $settings{'GUARDIAN_LOGLEVEL'}\n\n";
732 # IPFire related static settings.
733 print FILE
"# IPFire related settings.\n";
734 print FILE
"FirewallEngine = IPtables\n";
735 print FILE
"SocketOwner = nobody:nobody\n";
736 print FILE
"IgnoreFile = $ignorefile\n\n";
738 # Configured block values.
739 print FILE
"# Configured block values.\n";
740 print FILE
"BlockCount = $settings{'GUARDIAN_BLOCKCOUNT'}\n";
741 print FILE
"BlockTime = $settings{'GUARDIAN_BLOCKTIME'}\n\n";
744 # Loop through whole settings hash.
745 print FILE
"# Enabled modules.\n";
746 foreach my $option (keys %settings) {
747 # Search for enabled modules.
748 if ($option =~ /GUARDIAN_MONITOR_(.*)/) {
749 # Skip if module is not enabled.
750 next unless($settings{$option} eq "on");
752 # Skip module if no file location is available.
753 next unless(exists($module_file_locations{$1}));
755 # Add enabled module and defined path to the config file.
756 print FILE
"Monitor_$1 = $module_file_locations{$1}\n";
761 print FILE
"\n# Module settings.\n";
762 # Check if SNORT is enabled and add snort priority.
763 if ($settings{'GUARDIAN_MONITOR_SNORT'} eq "on") {
764 print FILE
"SnortPriorityLevel = $settings{'GUARDIAN_SNORT_PRIORITY_LEVEL'}\n";
769 # Check if guardian should be started or stopped.
770 if($settings{'GUARDIAN_ENABLED'} eq 'on') {
772 # Send reload command through socket connection.
773 &Guardian
::Socket
::Client
("reload");
776 system("/usr/local/bin/addonctrl guardian start &>/dev/null");
780 system("/usr/local/bin/addonctrl guardian stop &>/dev/null");