]>
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 ###############################################################################
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
36 ${Header
::colourgreen
}
47 # Path to the guardian.ignore file.
48 my $ignorefile ='/var/ipfire/guardian/guardian.ignore';
50 # Hash which contains the supported modules and the
51 # file locations on IPFire systems.
52 my %module_file_locations = (
53 "HTTPD" => "/var/log/httpd/error_log",
54 "SSH" => "/var/log/messages",
57 our %netsettings = ();
58 &General
::readhash
("${General::swroot}/ethernet/settings", \
%netsettings);
61 our %mainsettings = ();
62 &General
::readhash
("${General::swroot}/main/settings", \
%mainsettings);
63 &General
::readhash
("/srv/web/ipfire/html/themes/".$mainsettings{'THEME'}."/include/colors.txt", \
%color);
66 my $settingsfile = "${General::swroot}/guardian/settings";
67 my $ignoredfile = "${General::swroot}/guardian/ignored";
69 # Create empty settings and ignoredfile if they do not exist yet.
70 unless (-e
"$settingsfile") { system("touch $settingsfile"); }
71 unless (-e
"$ignoredfile") { system("touch $ignoredfile"); }
76 $settings{'ACTION'} = '';
78 $settings{'GUARDIAN_ENABLED'} = 'off';
79 $settings{'GUARDIAN_MONITOR_SSH'} = 'on';
80 $settings{'GUARDIAN_MONITOR_HTTPD'} = 'on';
81 $settings{'GUARDIAN_MONITOR_OWNCLOUD'} = '';
82 $settings{'GUARDIAN_LOG_FACILITY'} = 'syslog';
83 $settings{'GUARDIAN_LOGLEVEL'} = 'info';
84 $settings{'GUARDIAN_BLOCKCOUNT'} = '3';
85 $settings{'GUARDIAN_BLOCKTIME'} = '86400';
86 $settings{'GUARDIAN_FIREWALL_ACTION'} = 'DROP';
87 $settings{'GUARDIAN_LOGFILE'} = '/var/log/guardian/guardian.log';
89 my $errormessage = '';
91 &Header
::showhttpheaders
();
94 &Header
::getcgihash
(\
%settings);
96 # Check if guardian is running and grab some stats.
100 ## Perform input checks and save settings.
102 if ($settings{'ACTION'} eq $Lang::tr
{'save'}) {
103 # Check for valid blocktime.
104 unless(($settings{'GUARDIAN_BLOCKTIME'} =~ /^\d+$/) && ($settings{'GUARDIAN_BLOCKTIME'} ne "0")) {
105 $errormessage = "$Lang::tr{'guardian invalid blocktime'}";
108 # Check if the blockcount is valid.
109 unless(($settings{'GUARDIAN_BLOCKCOUNT'} =~ /^\d+$/) && ($settings{'GUARDIAN_BLOCKCOUNT'} ne "0")) {
110 $errormessage = "$Lang::tr{'guardian invalid blockcount'}";
114 unless($settings{'GUARDIAN_LOGFILE'} =~ /^[a-zA-Z0-9\.\/]+$/) {
115 $errormessage = "$Lang::tr{'guardian invalid logfile'}";
118 # Only continue if no error message has been set.
119 if($errormessage eq '') {
120 # Write configuration settings to file.
121 &General
::writehash
("${General::swroot}/guardian/settings", \
%settings);
123 # Update configuration files.
124 &BuildConfiguration
();
127 ## Add/edit an entry to the ignore file.
129 } elsif (($settings{'ACTION'} eq $Lang::tr
{'add'}) || ($settings{'ACTION'} eq $Lang::tr
{'update'})) {
131 # Check if any input has been performed.
132 if ($settings{'IGNORE_ENTRY_ADDRESS'} ne '') {
134 # Check if the given input is no valid IP-address or IP-address with subnet, display an error message.
135 if ((!&General
::validip
($settings{'IGNORE_ENTRY_ADDRESS'})) && (!&General
::validipandmask
($settings{'IGNORE_ENTRY_ADDRESS'}))) {
136 $errormessage = "$Lang::tr{'guardian invalid address or subnet'}";
139 $errormessage = "$Lang::tr{'guardian empty input'}";
142 # Go further if there was no error.
143 if ($errormessage eq '') {
148 # Assign hash values.
149 my $new_entry_address = $settings{'IGNORE_ENTRY_ADDRESS'};
150 my $new_entry_remark = $settings{'IGNORE_ENTRY_REMARK'};
152 # Read-in ignoredfile.
153 &General
::readhasharray
($ignoredfile, \
%ignored);
155 # Check if we should edit an existing entry and got an ID.
156 if (($settings{'ACTION'} eq $Lang::tr
{'update'}) && ($settings{'ID'})) {
157 # Assin the provided id.
158 $id = $settings{'ID'};
160 # Undef the given ID.
161 undef($settings{'ID'});
163 # Grab the configured status of the corresponding entry.
164 $status = $ignored{$id}[2];
166 # Each newly added entry automatically should be enabled.
169 # Generate the ID for the new entry.
171 # Sort the keys by their ID and store them in an array.
172 my @keys = sort { $a <=> $b } keys %ignored;
174 # Reverse the key array.
175 my @reversed = reverse(@keys);
177 # Obtain the last used id.
178 my $last_id = @reversed[0];
180 # Increase the last id by one and use it as id for the new entry.
184 # Add/Modify the entry to/in the ignored hash.
185 $ignored{$id} = ["$new_entry_address", "$new_entry_remark", "$status"];
187 # Write the changed ignored hash to the ignored file.
188 &General
::writehasharray
($ignoredfile, \
%ignored);
190 # Regenerate the ignore file.
191 &GenerateIgnoreFile
();
194 # Check if guardian is running.
196 # Send reload command through socket connection.
197 &Guardian
::Socket
::Client
("reload-ignore-list");
200 ## Toggle Enabled/Disabled for an existing entry on the ignore list.
203 } elsif ($settings{'ACTION'} eq $Lang::tr
{'toggle enable disable'}) {
206 # Only go further, if an ID has been passed.
207 if ($settings{'ID'}) {
208 # Assign the given ID.
209 my $id = $settings{'ID'};
211 # Undef the given ID.
212 undef($settings{'ID'});
214 # Read-in ignoredfile.
215 &General
::readhasharray
($ignoredfile, \
%ignored);
217 # Grab the configured status of the corresponding entry.
218 my $status = $ignored{$id}[2];
221 if ($status eq "disabled") {
224 $status = "disabled";
227 # Modify the status of the existing entry.
228 $ignored{$id} = ["$ignored{$id}[0]", "$ignored{$id}[1]", "$status"];
230 # Write the changed ignored hash to the ignored file.
231 &General
::writehasharray
($ignoredfile, \
%ignored);
233 # Regenerate the ignore file.
234 &GenerateIgnoreFile
();
236 # Check if guardian is running.
238 # Send reload command through socket connection.
239 &Guardian
::Socket
::Client
("reload-ignore-list");
243 ## Remove entry from ignore list.
245 } elsif ($settings{'ACTION'} eq $Lang::tr
{'remove'}) {
248 # Read-in ignoredfile.
249 &General
::readhasharray
($ignoredfile, \
%ignored);
251 # Drop entry from the hash.
252 delete($ignored{$settings{'ID'}});
254 # Undef the given ID.
255 undef($settings{'ID'});
257 # Write the changed ignored hash to the ignored file.
258 &General
::writehasharray
($ignoredfile, \
%ignored);
260 # Regenerate the ignore file.
261 &GenerateIgnoreFile
();
263 # Check if guardian is running.
265 # Send reload command through socket connection.
266 &Guardian
::Socket
::Client
("reload-ignore-list");
269 ## Block a user given address or subnet.
271 } elsif ($settings{'ACTION'} eq $Lang::tr
{'block'}) {
273 # Assign some temporary variables used for input validation.
274 my $input = $settings{'ADDRESS_BLOCK'};
275 my $green = $netsettings{'GREEN_ADDRESS'};
276 my $blue = $netsettings{'BLUE_ADDRESS'};
277 my $orange = $netsettings{'ORANGE_ADDRESS'};
278 my $red = $netsettings{'RED_ADDRESS'};
281 my $gatewayfile = "${General::swroot}/red/remote-ipaddress";
283 # Get gateway address.
284 my $gateway = &General
::grab_address_from_file
($gatewayfile);
286 # Check if any input has been performed.
288 $errormessage = "$Lang::tr{'guardian empty input'}";
291 # Check if the given input is localhost (127.0.0.1).
292 elsif ($input eq "127.0.0.1") {
293 $errormessage = "$Lang::tr{'guardian blocking of this address is not allowed'}";
296 # Check if the given input is anywhere (0.0.0.0).
297 elsif ($input eq "0.0.0.0") {
298 $errormessage = "$Lang::tr{'guardian blocking of this address is not allowed'}";
301 # Check if the given input is one of the interface addresses or our gateway.
302 elsif ($input eq "$green" || $input eq "$blue" || $input eq "$orange" || $input eq "$red" || $input eq "$gateway") {
303 $errormessage = "$Lang::tr{'guardian blocking of this address is not allowed'}";
306 # Check if the given input is a valid IP address.
307 elsif (!&General
::validip
($input)) {
308 $errormessage = "$Lang::tr{'guardian invalid address or subnet'}";
311 # Go further if there was no error.
312 if ($errormessage eq '') {
313 my $block = $settings{'ADDRESS_BLOCK'};
315 # Send command to block the specified address through socket connection.
316 &Guardian
::Socket
::Client
("block $block");
319 ## Unblock address or subnet.
321 } elsif ($settings{'ACTION'} eq $Lang::tr
{'unblock'}) {
323 # Check if no empty input has been performed.
324 if ($settings{'ADDRESS_UNBLOCK'} ne '') {
326 # Check if the given input is no valid IP-address or IP-address with subnet, display an error message.
327 if ((!&General
::validip
($settings{'ADDRESS_UNBLOCK'})) && (!&General
::validipandmask
($settings{'ADDRESS_UNBLOCK'}))) {
328 $errormessage = "$Lang::tr{'guardian invalid address or subnet'}";
332 $errormessage = "$Lang::tr{'guardian empty input'}";
335 # Go further if there was no error.
336 if ($errormessage eq '') {
337 my $unblock = $settings{'ADDRESS_UNBLOCK'};
339 # Send command to unblock the given address through socket connection.
340 &Guardian
::Socket
::Client
("unblock $unblock");
345 } elsif ($settings{'ACTION'} eq $Lang::tr
{'unblock all'}) {
347 # Send flush command through socket connection.
348 &Guardian
::Socket
::Client
("flush");
351 # Load settings from files.
352 &General
::readhash
("${General::swroot}/guardian/settings", \
%settings);
353 &General
::readhasharray
("${General::swroot}/guardian/ignored", \
%ignored);
355 # Call functions to generate whole page.
359 # Display area only if guardian is running.
360 if ( ($memory != 0) && ($pid > 0) ) {
364 # Function to display the status of guardian and allow base configuration.
369 $checked{'GUARDIAN_ENABLED'}{'on'} = '';
370 $checked{'GUARDIAN_ENABLED'}{'off'} = '';
371 $checked{'GUARDIAN_ENABLED'}{$settings{'GUARDIAN_ENABLED'}} = 'checked';
372 $checked{'GUARDIAN_MONITOR_SSH'}{'off'} = '';
373 $checked{'GUARDIAN_MONITOR_SSH'}{'on'} = '';
374 $checked{'GUARDIAN_MONITOR_SSH'}{$settings{'GUARDIAN_MONITOR_SSH'}} = "checked='checked'";
375 $checked{'GUARDIAN_MONITOR_HTTPD'}{'off'} = '';
376 $checked{'GUARDIAN_MONITOR_HTTPD'}{'on'} = '';
377 $checked{'GUARDIAN_MONITOR_HTTPD'}{$settings{'GUARDIAN_MONITOR_HTTPD'}} = "checked='checked'";
378 $checked{'GUARDIAN_MONITOR_OWNCLOUD'}{'off'} = '';
379 $checked{'GUARDIAN_MONITOR_OWNCLOUD'}{'on'} = '';
380 $checked{'GUARDIAN_MONITOR_OWNCLOUD'}{$settings{'GUARDIAN_MONITOR_OWNCLOUD'}} = "checked='checked'";
382 $selected{'GUARDIAN_LOG_FACILITY'}{$settings{'GUARDIAN_LOG_FACILITY'}} = 'selected';
383 $selected{'GUARDIAN_LOGLEVEL'}{$settings{'GUARDIAN_LOGLEVEL'}} = 'selected';
384 $selected{'GUARDIAN_FIREWALL_ACTION'}{$settings{'GUARDIAN_FIREWALL_ACTION'}} = 'selected';
386 &Header
::openpage
($Lang::tr
{'guardian configuration'}, 1, '');
387 &Header
::openbigbox
('100%', 'left', '', $errormessage);
389 # Print errormessage if there is one.
391 &Header
::openbox
('100%', 'left', $Lang::tr
{'error messages'});
392 print "<font class='base'>$errormessage </font>\n";
399 var update_options = function() {
401 var logfacility = \$("#GUARDIAN_LOG_FACILITY").val();
402 var loglevel = \$("#GUARDIAN_LOGLEVEL").val();
404 if (logfacility === undefined)
407 if (loglevel === undefined)
410 // Show / Hide input for specifying the path to the logfile.
411 if (logfacility === "file") {
412 \$(".GUARDIAN_LOGFILE").show();
414 \$(".GUARDIAN_LOGFILE").hide();
417 // Show / Hide loglevel debug if the facility is set to syslog.
418 if (logfacility === "syslog") {
419 \$("#loglevel_debug").hide();
421 \$("#loglevel_debug").show();
424 // Show / Hide logfacility syslog if the loglevel is set to debug.
425 if (loglevel === "debug") {
426 \$("#logfacility_syslog").hide();
428 \$("#logfacility_syslog").show();
432 \$(document).ready(function() {
433 \$("#GUARDIAN_LOG_FACILITY").change(update_options);
434 \$("#GUARDIAN_LOGLEVEL").change(update_options);
442 # Draw current guardian state.
443 &Header
::openbox
('100%', 'center', $Lang::tr
{'guardian'});
445 # Get current status of guardian.
449 # Display some useful information related to guardian, if daemon is running.
450 if ( ($memory != 0) && ($pid > 0) ){
452 <table width='95%' cellspacing='0' class='tbl'>
454 <th bgcolor='$color{'color20'}' colspan='3' align='left'><strong>$Lang::tr{'guardian service'}</strong></th>
457 <td class='base'>$Lang::tr{'guardian daemon'}</td>
458 <td align='center' colspan='2' width='75%' bgcolor='${Header::colourgreen}'><font color='white'><strong>$Lang::tr{'running'}</strong></font></td>
461 <td class='base'></td>
462 <td bgcolor='$color{'color20'}' align='center'><strong>PID</strong></td>
463 <td bgcolor='$color{'color20'}' align='center'><strong>$Lang::tr{'memory'}</strong></td>
466 <td class='base'></td>
467 <td bgcolor='$color{'color22'}' align='center'>$pid</td>
468 <td bgcolor='$color{'color22'}' align='center'>$memory KB</td>
473 # Otherwise display a hint that the service is not launched.
475 <table width='95%' cellspacing='0' class='tbl'>
477 <th bgcolor='$color{'color20'}' colspan='3' align='left'><strong>$Lang::tr{'guardian service'}</strong></th>
480 <td class='base'>$Lang::tr{'guardian daemon'}</td>
481 <td align='center' width='75%' bgcolor='${Header::colourred}'><font color='white'><strong>$Lang::tr{'stopped'}</strong></font></td>
489 # Draw elements for guardian configuration.
490 &Header
::openbox
('100%', 'center', $Lang::tr
{'guardian configuration'});
493 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
497 <td colspan='2' class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'guardian common settings'}</b></td>
501 <td width='25%' class='base'>$Lang::tr{'guardian enabled'}:</td>
502 <td><input type='checkbox' name='GUARDIAN_ENABLED' $checked{'GUARDIAN_ENABLED'}{'on'} /></td>
506 <td colspan='2'><br></td>
510 <td width='25%' class='base'>$Lang::tr{'guardian block ssh brute-force'}</td>
511 <td align='left'>on <input type='radio' name='GUARDIAN_MONITOR_SSH' value='on' $checked{'GUARDIAN_MONITOR_SSH'}{'on'} /> /
512 <input type='radio' name='GUARDIAN_MONITOR_SSH' value='off' $checked{'GUARDIAN_MONITOR_SSH'}{'off'} /> off</td>
516 <td width='25%' class='base'>$Lang::tr{'guardian block httpd brute-force'}</td>
517 <td align='left'>on <input type='radio' name='GUARDIAN_MONITOR_HTTPD' value='on' $checked{'GUARDIAN_MONITOR_HTTPD'}{'on'} /> /
518 <input type='radio' name='GUARDIAN_MONITOR_HTTPD' value='off' $checked{'GUARDIAN_MONITOR_HTTPD'}{'off'} /> off</td>
522 <td colspan='2'><br></td>
526 <td align='left' width='20%'>$Lang::tr{'guardian logfacility'}:</td>
527 <td width='25%'><select id='GUARDIAN_LOG_FACILITY' name='GUARDIAN_LOG_FACILITY'>
528 <option id='logfacility_syslog' value='syslog' $selected{'GUARDIAN_LOG_FACILITY'}{'syslog'}>$Lang::tr{'guardian logtarget_syslog'}</option>
529 <option id='logfacility_file' value='file' $selected{'GUARDIAN_LOG_FACILITY'}{'file'}>$Lang::tr{'guardian logtarget_file'}</option>
530 <option id='logfacility_console' value='console' $selected{'GUARDIAN_LOG_FACILITY'}{'console'}>$Lang::tr{'guardian logtarget_console'}</option>
533 <td align='left' width='20%'>$Lang::tr{'guardian loglevel'}:</td>
534 <td width='25%'><select id='GUARDIAN_LOGLEVEL' name='GUARDIAN_LOGLEVEL'>
535 <option id='loglevel_off' value='off' $selected{'GUARDIAN_LOGLEVEL'}{'off'}>$Lang::tr{'guardian loglevel_off'}</option>
536 <option id='loglevel_info' value='info' $selected{'GUARDIAN_LOGLEVEL'}{'info'}>$Lang::tr{'guardian loglevel_info'}</option>
537 <option id='loglevel_debug' value='debug' $selected{'GUARDIAN_LOGLEVEL'}{'debug'}>$Lang::tr{'guardian loglevel_debug'}</option>
541 <tr class="GUARDIAN_LOGFILE">
542 <td colspan='2'><br></td>
545 <tr class="GUARDIAN_LOGFILE">
546 <td width='25%' class='base'>$Lang::tr{'guardian logfile'}:</td>
547 <td><input type='text' name='GUARDIAN_LOGFILE' value='$settings{'GUARDIAN_LOGFILE'}' size='30' /></td>
551 <td colspan='2'><br></td>
555 <td width='25%' class='base'>$Lang::tr{'guardian firewallaction'}:</td>
556 <td><select name='GUARDIAN_FIREWALL_ACTION'>
557 <option value='DROP' $selected{'GUARDIAN_FIREWALL_ACTION'}{'DROP'}>Drop</option>
558 <option value='REJECT' $selected{'GUARDIAN_FIREWALL_ACTION'}{'REJECT'}>Reject</option>
561 <td width='25%' class='base'>$Lang::tr{'guardian blockcount'}:</td>
562 <td><input type='text' name='GUARDIAN_BLOCKCOUNT' value='$settings{'GUARDIAN_BLOCKCOUNT'}' size='5' /></td>
566 <td colspan='2'><br></td>
570 <td width='25%' class='base'>$Lang::tr{'guardian blocktime'}:</td>
571 <td><input type='text' name='GUARDIAN_BLOCKTIME' value='$settings{'GUARDIAN_BLOCKTIME'}' size='10' /></td>
583 <td align='center'><input type='submit' name='ACTION' value='$Lang::tr{'save'}' /></td>
593 # Function to show elements of the guardian ignorefile and allow to add or remove single members of it.
594 sub showIgnoreBox
() {
595 &Header
::openbox
('100%', 'center', $Lang::tr
{'guardian ignored hosts'});
600 <td class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'ip address'}</b></td>
601 <td class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'remark'}</b></td>
602 <td class='base' colspan='3' bgcolor='$color{'color20'}'></td>
605 # Check if some hosts have been added to be ignored.
606 if (keys (%ignored)) {
609 # Loop through all entries of the hash.
610 while( (my $key) = each %ignored) {
611 # Assign data array positions to some nice variable names.
612 my $address = $ignored{$key}[0];
613 my $remark = $ignored{$key}[1];
614 my $status = $ignored{$key}[2];
616 # Check if the key (id) number is even or not.
617 if ($settings{'ID'} eq $key) {
618 $col="bgcolor='${Header::colouryellow}'";
620 $col="bgcolor='$color{'color22'}'";
622 $col="bgcolor='$color{'color20'}'";
625 # Choose icon for the checkbox.
629 # Check if the status is enabled and select the correct image and description.
630 if ($status eq 'enabled' ) {
632 $gdesc = $Lang::tr
{'click to disable'};
635 $gdesc = $Lang::tr
{'click to enable'};
640 <td width='20%' class='base' $col>$address</td>
641 <td width='65%' class='base' $col>$remark</td>
643 <td align='center' $col>
644 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
645 <input type='hidden' name='ACTION' value='$Lang::tr{'toggle enable disable'}' />
646 <input type='image' name='$Lang::tr{'toggle enable disable'}' src='/images/$gif' alt='$gdesc' title='$gdesc' />
647 <input type='hidden' name='ID' value='$key' />
651 <td align='center' $col>
652 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
653 <input type='hidden' name='ACTION' value='$Lang::tr{'edit'}' />
654 <input type='image' name='$Lang::tr{'edit'}' src='/images/edit.gif' alt='$Lang::tr{'edit'}' title='$Lang::tr{'edit'}' />
655 <input type='hidden' name='ID' value='$key' />
659 <td align='center' $col>
660 <form method='post' name='$key' action='$ENV{'SCRIPT_NAME'}'>
661 <input type='image' name='$Lang::tr{'remove'}' src='/images/delete.gif' title='$Lang::tr{'remove'}' alt='$Lang::tr{'remove'}'>
662 <input type='hidden' name='ID' value='$key'>
663 <input type='hidden' name='ACTION' value='$Lang::tr{'remove'}'>
670 # Print notice that currently no hosts are ignored.
672 print "<td class='base' colspan='2'>$Lang::tr{'guardian no entries'}</td>\n";
678 # Section to add new elements or edit existing ones.
688 # Assign correct headline and button text.
693 # Check if an ID (key) has been given, in this case an existing entry should be edited.
694 if ($settings{'ID'} ne '') {
695 $buttontext = $Lang::tr
{'update'};
696 print "<tr><td class='boldbase' colspan='3'><b>$Lang::tr{'update'}</b></td></tr>\n";
698 # Grab address and remark for the given key.
699 $entry_address = $ignored{$settings{'ID'}}[0];
700 $entry_remark = $ignored{$settings{'ID'}}[1];
702 $buttontext = $Lang::tr
{'add'};
703 print "<tr><td class='boldbase' colspan='3'><b>$Lang::tr{'dnsforward add a new entry'}</b></td></tr>\n";
707 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
708 <input type='hidden' name='ID' value='$settings{'ID'}'>
710 <td width='30%'>$Lang::tr{'ip address'}: </td>
711 <td width='50%'><input type='text' name='IGNORE_ENTRY_ADDRESS' value='$entry_address' size='24' /></td>
713 <td width='30%'>$Lang::tr{'remark'}: </td>
714 <td wicth='50%'><input type='text' name=IGNORE_ENTRY_REMARK value='$entry_remark' size='24' /></td>
715 <td align='center' width='20%'><input type='submit' name='ACTION' value='$buttontext' /></td>
725 # Function to list currently blocked addresses from guardian and unblock them or add custom entries to block.
726 sub showBlockedBox
() {
727 &Header
::openbox
('100%', 'center', $Lang::tr
{'guardian blocked hosts'});
732 <td colspan='2' class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'guardian blocked hosts'}</b></td>
736 # Launch function to get the currently blocked hosts.
737 my @blocked_hosts = &GetBlockedHosts
();
742 # Loop through our blocked hosts array.
743 foreach my $blocked_host (@blocked_hosts) {
745 # Increase id number for each element in the ignore file.
748 # Check if the id number is even or not.
750 $col="bgcolor='$color{'color22'}'";
752 $col="bgcolor='$color{'color20'}'";
757 <td width='80%' class='base' $col><a href='/cgi-bin/ipinfo.cgi?ip=$blocked_host'>$blocked_host</a></td>
758 <td width='20%' align='center' $col>
759 <form method='post' name='frmb$id' action='$ENV{'SCRIPT_NAME'}'>
760 <input type='image' name='$Lang::tr{'unblock'}' src='/images/delete.gif' title='$Lang::tr{'unblock'}' alt='$Lang::tr{'unblock'}'>
761 <input type='hidden' name='ADDRESS_UNBLOCK' value='$blocked_host'>
762 <input type='hidden' name='ACTION' value='$Lang::tr{'unblock'}'>
769 # If the loop only has been run once the id still is "0", which means there are no
770 # additional entries (blocked hosts) in the iptables chain.
773 # Print notice that currently no hosts are blocked.
775 print "<td class='base' colspan='2'>$Lang::tr{'guardian no entries'}</td>\n";
781 # Section for a manual block of an IP-address.
785 <table width='60%' border='0'>
786 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
788 <td width='30%'>$Lang::tr{'guardian block a host'}: </td>
789 <td width='40%'><input type='text' name='ADDRESS_BLOCK' value='' size='24' /></td>
790 <td align='center' width='15%'><input type='submit' name='ACTION' value='$Lang::tr{'block'}'></td>
791 <td align='center' width='15%'><input type='submit' name='ACTION' value='$Lang::tr{'unblock all'}'></td>
801 &Header
::closebigbox
();
802 &Header
::closepage
();
804 # Function to check if guardian has been started.
805 # Grab process id and consumed memory if the daemon is running.
809 open(FILE
, '/usr/local/bin/addonctrl guardian status | ');
812 $string = join("", @guardian);
813 $string =~ s/[a-z_]//gi;
814 $string =~ s/\[[0-1]\;[0-9]+//gi;
815 $string =~ s/[\(\)\.]//gi;
818 @pid = split(/\s/,$string);
819 if (open(FILE
, "/proc/$pid[0]/statm")){
821 @memory = split(/ /,$temp);
827 sub GetBlockedHosts
() {
828 # Create new, empty array.
831 # Launch helper to get chains from iptables.
832 system('/usr/local/bin/getipstat');
834 # Open temporary file which contains the chains and rules.
835 open (FILE
, '/var/tmp/iptables.txt');
837 # Loop through the entire file.
841 # Search for the guardian chain and extract
842 # the lines between it and the next empty line
843 # which is placed before the next firewall
845 if ($line =~ /^Chain GUARDIAN/ .. /^\s*$/) {
846 # Skip descriptive lines.
847 next if ($line =~ /^Chain/);
848 next if ($line =~ /^ pkts/);
850 # Generate array, based on the line content (separator is a single or multiple space)
851 my @comps = split(/\s{1,}/, $line);
852 my ($lead, $pkts, $bytes, $target, $prot, $opt, $in, $out, $source, $destination) = @comps;
854 # Assign different variable names.
855 my $blocked_host = $source;
857 # Add host to our hosts array.
859 push(@hosts, $blocked_host);
867 # Remove recently created temporary files of the "getipstat" binary.
868 system("rm -f /var/tmp/iptables.txt");
869 system("rm -f /var/tmp/iptablesmangle.txt");
870 system("rm -f /var/tmp/iptablesnat.txt");
872 # Convert entries, sort them, write back and store the sorted entries into new array.
873 my @sorted = map { $_->[0] }
874 sort { $a->[1] <=> $b->[1] }
875 map { [$_, int sprintf("%03.f%03.f%03.f%03.f", split(/\./, $_))] }
878 # Return our sorted list.
882 sub BuildConfiguration
() {
884 &General
::readhash
("${General::swroot}/guardian/settings", \
%settings);
886 my $configfile = "${General::swroot}/guardian/guardian.conf";
888 # Create the configfile if none exists yet.
889 unless (-e
"$configfile") { system("touch $configfile"); }
891 # Open configfile for writing.
892 open(FILE
, ">$configfile");
894 # Config file header.
895 print FILE
"# Autogenerated configuration file.\n";
896 print FILE
"# All user modifications will be overwritten.\n\n";
898 # Settings for the logging mechanism.
899 print FILE
"# Log settings.\n";
900 print FILE
"LogFacility = $settings{'GUARDIAN_LOG_FACILITY'}\n";
902 if ($settings{'GUARDIAN_LOG_FACILITY'} eq "file") {
903 print FILE
"LogFile = $settings{'GUARDIAN_LOGFILE'}\n";
906 print FILE
"LogLevel = $settings{'GUARDIAN_LOGLEVEL'}\n\n";
908 # IPFire related static settings.
909 print FILE
"# IPFire related settings.\n";
910 print FILE
"FirewallEngine = IPtables\n";
911 print FILE
"SocketOwner = nobody:nobody\n";
912 print FILE
"IgnoreFile = $ignorefile\n\n";
914 # Configured block values.
915 print FILE
"# Configured block settings.\n";
916 print FILE
"BlockCount = $settings{'GUARDIAN_BLOCKCOUNT'}\n";
917 print FILE
"BlockTime = $settings{'GUARDIAN_BLOCKTIME'}\n";
918 print FILE
"FirewallAction = $settings{'GUARDIAN_FIREWALL_ACTION'}\n\n";
921 # Loop through whole settings hash.
922 print FILE
"# Enabled modules.\n";
923 foreach my $option (keys %settings) {
924 # Search for enabled modules.
925 if ($option =~ /GUARDIAN_MONITOR_(.*)/) {
926 # Skip if module is not enabled.
927 next unless($settings{$option} eq "on");
929 # Skip module if no file location is available.
930 next unless(exists($module_file_locations{$1}));
932 # Add enabled module and defined path to the config file.
933 print FILE
"Monitor_$1 = $module_file_locations{$1}\n";
938 print FILE
"\n# Module settings.\n";
941 # Generate ignore file.
942 &GenerateIgnoreFile
();
944 # Check if guardian should be started or stopped.
945 if($settings{'GUARDIAN_ENABLED'} eq 'on') {
947 # Send reload command through socket connection.
948 &Guardian
::Socket
::Client
("reload");
951 system("/usr/local/bin/addonctrl guardian start &>/dev/null");
955 system("/usr/local/bin/addonctrl guardian stop &>/dev/null");
959 sub GenerateIgnoreFile
() {
962 # Read-in ignoredfile.
963 &General
::readhasharray
($ignoredfile, \
%ignored);
965 # Create the guardian.ignore file if not exist yet.
966 unless (-e
"$ignorefile") { system("touch $ignorefile"); }
968 # Open ignorefile for writing.
969 open(FILE
, ">$ignorefile");
971 # Config file header.
972 print FILE
"# Autogenerated configuration file.\n";
973 print FILE
"# All user modifications will be overwritten.\n\n";
975 # Add IFPire interfaces and gateway to the ignore file.
977 # Assign some temporary variables for the IPFire interfaces.
978 my $green = $netsettings{'GREEN_ADDRESS'};
979 my $blue = $netsettings{'BLUE_ADDRESS'};
980 my $orange = $netsettings{'ORANGE_ADDRESS'};
983 my $public_address_file = "${General::swroot}/red/local-ipaddress";
984 my $gatewayfile = "${General::swroot}/red/remote-ipaddress";
986 # Write the obtained addresses to the ignore file.
987 print FILE
"# IPFire local interfaces.\n";
988 print FILE
"$green\n";
990 # Check if a blue interface exists.
993 print FILE
"$blue\n";
996 # Check if an orange interface exists.
998 # Add orange address.
999 print FILE
"$orange\n";
1002 print FILE
"\n# IPFire red interface, gateway and used DNS-servers.\n";
1003 print FILE
"# Include the corresponding files to obtain the addresses.\n";
1004 print FILE
"Include_File = $public_address_file\n";
1005 print FILE
"Include_File = $gatewayfile\n";
1007 # Add all user defined hosts and networks to the ignore file.
1009 # Check if the hash contains any elements.
1010 if (keys (%ignored)) {
1012 print FILE
"\n# User defined hosts/networks.\n";
1014 # Loop through the entire hash and write the host/network
1015 # and remark to the ignore file.
1016 while ( (my $key) = each %ignored) {
1017 my $address = $ignored{$key}[0];
1018 my $remark = $ignored{$key}[1];
1019 my $status = $ignored{$key}[2];
1021 # Check if the status of the entry is "enabled".
1022 if ($status eq "enabled") {
1023 # Check if the address/network is valid.
1024 if ((&General
::validip
($address)) || (&General
::validipandmask
($address))) {
1025 # Write the remark to the file.
1026 print FILE
"# $remark\n";
1028 # Write the address/network to the ignore file.
1029 print FILE
"$address\n\n";