]>
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 "SSH" => "/var/log/messages",
58 our %netsettings = ();
59 &General
::readhash
("${General::swroot}/ethernet/settings", \
%netsettings);
62 our %mainsettings = ();
63 &General
::readhash
("${General::swroot}/main/settings", \
%mainsettings);
64 &General
::readhash
("/srv/web/ipfire/html/themes/".$mainsettings{'THEME'}."/include/colors.txt", \
%color);
67 my $settingsfile = "${General::swroot}/guardian/settings";
68 my $ignoredfile = "${General::swroot}/guardian/ignored";
70 # Create empty settings and ignoredfile if they do not exist yet.
71 unless (-e
"$settingsfile") { system("touch $settingsfile"); }
72 unless (-e
"$ignoredfile") { system("touch $ignoredfile"); }
77 $settings{'ACTION'} = '';
79 $settings{'GUARDIAN_ENABLED'} = 'off';
80 $settings{'GUARDIAN_MONITOR_SSH'} = 'on';
81 $settings{'GUARDIAN_MONITOR_HTTPD'} = 'on';
82 $settings{'GUARDIAN_MONITOR_OWNCLOUD'} = '';
83 $settings{'GUARDIAN_LOG_FACILITY'} = 'syslog';
84 $settings{'GUARDIAN_LOGLEVEL'} = 'info';
85 $settings{'GUARDIAN_BLOCKCOUNT'} = '3';
86 $settings{'GUARDIAN_BLOCKTIME'} = '86400';
87 $settings{'GUARDIAN_FIREWALL_ACTION'} = 'DROP';
88 $settings{'GUARDIAN_LOGFILE'} = '/var/log/guardian/guardian.log';
90 my $errormessage = '';
92 &Header
::showhttpheaders
();
95 &Header
::getcgihash
(\
%settings);
97 # Check if guardian is running and grab some stats.
101 ## Perform input checks and save settings.
103 if ($settings{'ACTION'} eq $Lang::tr
{'save'}) {
104 # Check for valid blocktime.
105 unless(($settings{'GUARDIAN_BLOCKTIME'} =~ /^\d+$/) && ($settings{'GUARDIAN_BLOCKTIME'} ne "0")) {
106 $errormessage = "$Lang::tr{'guardian invalid blocktime'}";
109 # Check if the blockcount is valid.
110 unless(($settings{'GUARDIAN_BLOCKCOUNT'} =~ /^\d+$/) && ($settings{'GUARDIAN_BLOCKCOUNT'} ne "0")) {
111 $errormessage = "$Lang::tr{'guardian invalid blockcount'}";
115 unless($settings{'GUARDIAN_LOGFILE'} =~ /^[a-zA-Z0-9\.\/]+$/) {
116 $errormessage = "$Lang::tr{'guardian invalid logfile'}";
119 # Only continue if no error message has been set.
120 if($errormessage eq '') {
121 # Write configuration settings to file.
122 &General
::writehash
("${General::swroot}/guardian/settings", \
%settings);
124 # Update configuration files.
125 &BuildConfiguration
();
128 ## Add/edit an entry to the ignore file.
130 } elsif (($settings{'ACTION'} eq $Lang::tr
{'add'}) || ($settings{'ACTION'} eq $Lang::tr
{'update'})) {
132 # Check if any input has been performed.
133 if ($settings{'IGNORE_ENTRY_ADDRESS'} ne '') {
135 # Check if the given input is no valid IP-address or IP-address with subnet, display an error message.
136 if ((!&General
::validip
($settings{'IGNORE_ENTRY_ADDRESS'})) && (!&General
::validipandmask
($settings{'IGNORE_ENTRY_ADDRESS'}))) {
137 $errormessage = "$Lang::tr{'guardian invalid address or subnet'}";
140 $errormessage = "$Lang::tr{'guardian empty input'}";
143 # Go further if there was no error.
144 if ($errormessage eq '') {
149 # Assign hash values.
150 my $new_entry_address = $settings{'IGNORE_ENTRY_ADDRESS'};
151 my $new_entry_remark = $settings{'IGNORE_ENTRY_REMARK'};
153 # Read-in ignoredfile.
154 &General
::readhasharray
($ignoredfile, \
%ignored);
156 # Check if we should edit an existing entry and got an ID.
157 if (($settings{'ACTION'} eq $Lang::tr
{'update'}) && ($settings{'ID'})) {
158 # Assin the provided id.
159 $id = $settings{'ID'};
161 # Undef the given ID.
162 undef($settings{'ID'});
164 # Grab the configured status of the corresponding entry.
165 $status = $ignored{$id}[2];
167 # Each newly added entry automatically should be enabled.
170 # Generate the ID for the new entry.
172 # Sort the keys by their ID and store them in an array.
173 my @keys = sort { $a <=> $b } keys %ignored;
175 # Reverse the key array.
176 my @reversed = reverse(@keys);
178 # Obtain the last used id.
179 my $last_id = @reversed[0];
181 # Increase the last id by one and use it as id for the new entry.
185 # Add/Modify the entry to/in the ignored hash.
186 $ignored{$id} = ["$new_entry_address", "$new_entry_remark", "$status"];
188 # Write the changed ignored hash to the ignored file.
189 &General
::writehasharray
($ignoredfile, \
%ignored);
191 # Regenerate the ignore file.
192 &GenerateIgnoreFile
();
195 # Check if guardian is running.
197 # Send reload command through socket connection.
198 &Guardian
::Socket
::Client
("reload-ignore-list");
201 ## Toggle Enabled/Disabled for an existing entry on the ignore list.
204 } elsif ($settings{'ACTION'} eq $Lang::tr
{'toggle enable disable'}) {
207 # Only go further, if an ID has been passed.
208 if ($settings{'ID'}) {
209 # Assign the given ID.
210 my $id = $settings{'ID'};
212 # Undef the given ID.
213 undef($settings{'ID'});
215 # Read-in ignoredfile.
216 &General
::readhasharray
($ignoredfile, \
%ignored);
218 # Grab the configured status of the corresponding entry.
219 my $status = $ignored{$id}[2];
222 if ($status eq "disabled") {
225 $status = "disabled";
228 # Modify the status of the existing entry.
229 $ignored{$id} = ["$ignored{$id}[0]", "$ignored{$id}[1]", "$status"];
231 # Write the changed ignored hash to the ignored file.
232 &General
::writehasharray
($ignoredfile, \
%ignored);
234 # Regenerate the ignore file.
235 &GenerateIgnoreFile
();
237 # Check if guardian is running.
239 # Send reload command through socket connection.
240 &Guardian
::Socket
::Client
("reload-ignore-list");
244 ## Remove entry from ignore list.
246 } elsif ($settings{'ACTION'} eq $Lang::tr
{'remove'}) {
249 # Read-in ignoredfile.
250 &General
::readhasharray
($ignoredfile, \
%ignored);
252 # Drop entry from the hash.
253 delete($ignored{$settings{'ID'}});
255 # Undef the given ID.
256 undef($settings{'ID'});
258 # Write the changed ignored hash to the ignored file.
259 &General
::writehasharray
($ignoredfile, \
%ignored);
261 # Regenerate the ignore file.
262 &GenerateIgnoreFile
();
264 # Check if guardian is running.
266 # Send reload command through socket connection.
267 &Guardian
::Socket
::Client
("reload-ignore-list");
270 ## Block a user given address or subnet.
272 } elsif ($settings{'ACTION'} eq $Lang::tr
{'block'}) {
274 # Assign some temporary variables used for input validation.
275 my $input = $settings{'ADDRESS_BLOCK'};
276 my $green = $netsettings{'GREEN_ADDRESS'};
277 my $blue = $netsettings{'BLUE_ADDRESS'};
278 my $orange = $netsettings{'ORANGE_ADDRESS'};
279 my $red = $netsettings{'RED_ADDRESS'};
282 my $gatewayfile = "${General::swroot}/red/remote-ipaddress";
283 my $dns1file = "${General::swroot}/red/dns1";
284 my $dns2file = "${General::swroot}/red/dns2";
286 # Get gateway address.
287 my $gateway = &_get_address_from_file
($gatewayfile);
289 # Get addresses from the used dns servers.
290 my $dns1 = &_get_address_from_file
($dns1file);
291 my $dns2 = &_get_address_from_file
($dns2file);
293 # Check if any input has been performed.
295 $errormessage = "$Lang::tr{'guardian empty input'}";
298 # Check if the given input is localhost (127.0.0.1).
299 elsif ($input eq "127.0.0.1") {
300 $errormessage = "$Lang::tr{'guardian blocking of this address is not allowed'}";
303 # Check if the given input is anywhere (0.0.0.0).
304 elsif ($input eq "0.0.0.0") {
305 $errormessage = "$Lang::tr{'guardian blocking of this address is not allowed'}";
308 # Check if the given input is one of the interface addresses or our gateway.
309 elsif ($input eq "$green" || $input eq "$blue" || $input eq "$orange" || $input eq "$red" || $input eq "$gateway" || $input eq "$dns1" || $input eq "$dns2") {
310 $errormessage = "$Lang::tr{'guardian blocking of this address is not allowed'}";
313 # Check if the given input is a valid IP address.
314 elsif (!&General
::validip
($input)) {
315 $errormessage = "$Lang::tr{'guardian invalid address or subnet'}";
318 # Go further if there was no error.
319 if ($errormessage eq '') {
320 my $block = $settings{'ADDRESS_BLOCK'};
322 # Send command to block the specified address through socket connection.
323 &Guardian
::Socket
::Client
("block $block");
326 ## Unblock address or subnet.
328 } elsif ($settings{'ACTION'} eq $Lang::tr
{'unblock'}) {
330 # Check if no empty input has been performed.
331 if ($settings{'ADDRESS_UNBLOCK'} ne '') {
333 # Check if the given input is no valid IP-address or IP-address with subnet, display an error message.
334 if ((!&General
::validip
($settings{'ADDRESS_UNBLOCK'})) && (!&General
::validipandmask
($settings{'ADDRESS_UNBLOCK'}))) {
335 $errormessage = "$Lang::tr{'guardian invalid address or subnet'}";
339 $errormessage = "$Lang::tr{'guardian empty input'}";
342 # Go further if there was no error.
343 if ($errormessage eq '') {
344 my $unblock = $settings{'ADDRESS_UNBLOCK'};
346 # Send command to unblock the given address through socket connection.
347 &Guardian
::Socket
::Client
("unblock $unblock");
352 } elsif ($settings{'ACTION'} eq $Lang::tr
{'unblock all'}) {
354 # Send flush command through socket connection.
355 &Guardian
::Socket
::Client
("flush");
358 # Load settings from files.
359 &General
::readhash
("${General::swroot}/guardian/settings", \
%settings);
360 &General
::readhasharray
("${General::swroot}/guardian/ignored", \
%ignored);
362 # Call functions to generate whole page.
366 # Display area only if guardian is running.
367 if ( ($memory != 0) && ($pid > 0) ) {
371 # Function to display the status of guardian and allow base configuration.
376 $checked{'GUARDIAN_ENABLED'}{'on'} = '';
377 $checked{'GUARDIAN_ENABLED'}{'off'} = '';
378 $checked{'GUARDIAN_ENABLED'}{$settings{'GUARDIAN_ENABLED'}} = 'checked';
379 $checked{'GUARDIAN_MONITOR_SSH'}{'off'} = '';
380 $checked{'GUARDIAN_MONITOR_SSH'}{'on'} = '';
381 $checked{'GUARDIAN_MONITOR_SSH'}{$settings{'GUARDIAN_MONITOR_SSH'}} = "checked='checked'";
382 $checked{'GUARDIAN_MONITOR_HTTPD'}{'off'} = '';
383 $checked{'GUARDIAN_MONITOR_HTTPD'}{'on'} = '';
384 $checked{'GUARDIAN_MONITOR_HTTPD'}{$settings{'GUARDIAN_MONITOR_HTTPD'}} = "checked='checked'";
385 $checked{'GUARDIAN_MONITOR_OWNCLOUD'}{'off'} = '';
386 $checked{'GUARDIAN_MONITOR_OWNCLOUD'}{'on'} = '';
387 $checked{'GUARDIAN_MONITOR_OWNCLOUD'}{$settings{'GUARDIAN_MONITOR_OWNCLOUD'}} = "checked='checked'";
389 $selected{'GUARDIAN_LOG_FACILITY'}{$settings{'GUARDIAN_LOG_FACILITY'}} = 'selected';
390 $selected{'GUARDIAN_LOGLEVEL'}{$settings{'GUARDIAN_LOGLEVEL'}} = 'selected';
391 $selected{'GUARDIAN_FIREWALL_ACTION'}{$settings{'GUARDIAN_FIREWALL_ACTION'}} = 'selected';
393 &Header
::openpage
($Lang::tr
{'guardian configuration'}, 1, '');
394 &Header
::openbigbox
('100%', 'left', '', $errormessage);
396 # Print errormessage if there is one.
398 &Header
::openbox
('100%', 'left', $Lang::tr
{'error messages'});
399 print "<font class='base'>$errormessage </font>\n";
406 var update_options = function() {
408 var logfacility = \$("#GUARDIAN_LOG_FACILITY").val();
409 var loglevel = \$("#GUARDIAN_LOGLEVEL").val();
411 if (logfacility === undefined)
414 if (loglevel === undefined)
417 // Show / Hide input for specifying the path to the logfile.
418 if (logfacility === "file") {
419 \$(".GUARDIAN_LOGFILE").show();
421 \$(".GUARDIAN_LOGFILE").hide();
424 // Show / Hide loglevel debug if the facility is set to syslog.
425 if (logfacility === "syslog") {
426 \$("#loglevel_debug").hide();
428 \$("#loglevel_debug").show();
431 // Show / Hide logfacility syslog if the loglevel is set to debug.
432 if (loglevel === "debug") {
433 \$("#logfacility_syslog").hide();
435 \$("#logfacility_syslog").show();
439 \$(document).ready(function() {
440 \$("#GUARDIAN_LOG_FACILITY").change(update_options);
441 \$("#GUARDIAN_LOGLEVEL").change(update_options);
449 # Draw current guardian state.
450 &Header
::openbox
('100%', 'center', $Lang::tr
{'guardian'});
452 # Get current status of guardian.
456 # Display some useful information related to guardian, if daemon is running.
457 if ( ($memory != 0) && ($pid > 0) ){
459 <table width='95%' cellspacing='0' class='tbl'>
461 <th bgcolor='$color{'color20'}' colspan='3' align='left'><strong>$Lang::tr{'guardian service'}</strong></th>
464 <td class='base'>$Lang::tr{'guardian daemon'}</td>
465 <td align='center' colspan='2' width='75%' bgcolor='${Header::colourgreen}'><font color='white'><strong>$Lang::tr{'running'}</strong></font></td>
468 <td class='base'></td>
469 <td bgcolor='$color{'color20'}' align='center'><strong>PID</strong></td>
470 <td bgcolor='$color{'color20'}' align='center'><strong>$Lang::tr{'memory'}</strong></td>
473 <td class='base'></td>
474 <td bgcolor='$color{'color22'}' align='center'>$pid</td>
475 <td bgcolor='$color{'color22'}' align='center'>$memory KB</td>
480 # Otherwise display a hint that the service is not launched.
482 <table width='95%' cellspacing='0' class='tbl'>
484 <th bgcolor='$color{'color20'}' colspan='3' align='left'><strong>$Lang::tr{'guardian service'}</strong></th>
487 <td class='base'>$Lang::tr{'guardian daemon'}</td>
488 <td align='center' width='75%' bgcolor='${Header::colourred}'><font color='white'><strong>$Lang::tr{'stopped'}</strong></font></td>
496 # Draw elements for guardian configuration.
497 &Header
::openbox
('100%', 'center', $Lang::tr
{'guardian configuration'});
500 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
504 <td colspan='2' class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'guardian common settings'}</b></td>
508 <td width='25%' class='base'>$Lang::tr{'guardian enabled'}:</td>
509 <td><input type='checkbox' name='GUARDIAN_ENABLED' $checked{'GUARDIAN_ENABLED'}{'on'} /></td>
513 <td colspan='2'><br></td>
517 <td width='25%' class='base'>$Lang::tr{'guardian block ssh brute-force'}</td>
518 <td align='left'>on <input type='radio' name='GUARDIAN_MONITOR_SSH' value='on' $checked{'GUARDIAN_MONITOR_SSH'}{'on'} /> /
519 <input type='radio' name='GUARDIAN_MONITOR_SSH' value='off' $checked{'GUARDIAN_MONITOR_SSH'}{'off'} /> off</td>
523 <td width='25%' class='base'>$Lang::tr{'guardian block httpd brute-force'}</td>
524 <td align='left'>on <input type='radio' name='GUARDIAN_MONITOR_HTTPD' value='on' $checked{'GUARDIAN_MONITOR_HTTPD'}{'on'} /> /
525 <input type='radio' name='GUARDIAN_MONITOR_HTTPD' value='off' $checked{'GUARDIAN_MONITOR_HTTPD'}{'off'} /> off</td>
529 <td colspan='2'><br></td>
533 <td align='left' width='20%'>$Lang::tr{'guardian logfacility'}:</td>
534 <td width='25%'><select id='GUARDIAN_LOG_FACILITY' name='GUARDIAN_LOG_FACILITY'>
535 <option id='logfacility_syslog' value='syslog' $selected{'GUARDIAN_LOG_FACILITY'}{'syslog'}>$Lang::tr{'guardian logtarget_syslog'}</option>
536 <option id='logfacility_file' value='file' $selected{'GUARDIAN_LOG_FACILITY'}{'file'}>$Lang::tr{'guardian logtarget_file'}</option>
537 <option id='logfacility_console' value='console' $selected{'GUARDIAN_LOG_FACILITY'}{'console'}>$Lang::tr{'guardian logtarget_console'}</option>
540 <td align='left' width='20%'>$Lang::tr{'guardian loglevel'}:</td>
541 <td width='25%'><select id='GUARDIAN_LOGLEVEL' name='GUARDIAN_LOGLEVEL'>
542 <option id='loglevel_off' value='off' $selected{'GUARDIAN_LOGLEVEL'}{'off'}>$Lang::tr{'guardian loglevel_off'}</option>
543 <option id='loglevel_info' value='info' $selected{'GUARDIAN_LOGLEVEL'}{'info'}>$Lang::tr{'guardian loglevel_info'}</option>
544 <option id='loglevel_debug' value='debug' $selected{'GUARDIAN_LOGLEVEL'}{'debug'}>$Lang::tr{'guardian loglevel_debug'}</option>
548 <tr class="GUARDIAN_LOGFILE">
549 <td colspan='2'><br></td>
552 <tr class="GUARDIAN_LOGFILE">
553 <td width='25%' class='base'>$Lang::tr{'guardian logfile'}:</td>
554 <td><input type='text' name='GUARDIAN_LOGFILE' value='$settings{'GUARDIAN_LOGFILE'}' size='30' /></td>
558 <td colspan='2'><br></td>
562 <td width='25%' class='base'>$Lang::tr{'guardian firewallaction'}:</td>
563 <td><select name='GUARDIAN_FIREWALL_ACTION'>
564 <option value='DROP' $selected{'GUARDIAN_FIREWALL_ACTION'}{'DROP'}>Drop</option>
565 <option value='REJECT' $selected{'GUARDIAN_FIREWALL_ACTION'}{'REJECT'}>Reject</option>
568 <td width='25%' class='base'>$Lang::tr{'guardian blockcount'}:</td>
569 <td><input type='text' name='GUARDIAN_BLOCKCOUNT' value='$settings{'GUARDIAN_BLOCKCOUNT'}' size='5' /></td>
573 <td colspan='2'><br></td>
577 <td width='25%' class='base'>$Lang::tr{'guardian blocktime'}:</td>
578 <td><input type='text' name='GUARDIAN_BLOCKTIME' value='$settings{'GUARDIAN_BLOCKTIME'}' size='10' /></td>
590 <td align='center'><input type='submit' name='ACTION' value='$Lang::tr{'save'}' /></td>
600 # Function to show elements of the guardian ignorefile and allow to add or remove single members of it.
601 sub showIgnoreBox
() {
602 &Header
::openbox
('100%', 'center', $Lang::tr
{'guardian ignored hosts'});
607 <td class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'ip address'}</b></td>
608 <td class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'remark'}</b></td>
609 <td class='base' colspan='3' bgcolor='$color{'color20'}'></td>
612 # Check if some hosts have been added to be ignored.
613 if (keys (%ignored)) {
616 # Loop through all entries of the hash.
617 while( (my $key) = each %ignored) {
618 # Assign data array positions to some nice variable names.
619 my $address = $ignored{$key}[0];
620 my $remark = $ignored{$key}[1];
621 my $status = $ignored{$key}[2];
623 # Check if the key (id) number is even or not.
624 if ($settings{'ID'} eq $key) {
625 $col="bgcolor='${Header::colouryellow}'";
627 $col="bgcolor='$color{'color22'}'";
629 $col="bgcolor='$color{'color20'}'";
632 # Choose icon for the checkbox.
636 # Check if the status is enabled and select the correct image and description.
637 if ($status eq 'enabled' ) {
639 $gdesc = $Lang::tr
{'click to disable'};
642 $gdesc = $Lang::tr
{'click to enable'};
647 <td width='20%' class='base' $col>$address</td>
648 <td width='65%' class='base' $col>$remark</td>
650 <td align='center' $col>
651 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
652 <input type='hidden' name='ACTION' value='$Lang::tr{'toggle enable disable'}' />
653 <input type='image' name='$Lang::tr{'toggle enable disable'}' src='/images/$gif' alt='$gdesc' title='$gdesc' />
654 <input type='hidden' name='ID' value='$key' />
658 <td align='center' $col>
659 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
660 <input type='hidden' name='ACTION' value='$Lang::tr{'edit'}' />
661 <input type='image' name='$Lang::tr{'edit'}' src='/images/edit.gif' alt='$Lang::tr{'edit'}' title='$Lang::tr{'edit'}' />
662 <input type='hidden' name='ID' value='$key' />
666 <td align='center' $col>
667 <form method='post' name='$key' action='$ENV{'SCRIPT_NAME'}'>
668 <input type='image' name='$Lang::tr{'remove'}' src='/images/delete.gif' title='$Lang::tr{'remove'}' alt='$Lang::tr{'remove'}'>
669 <input type='hidden' name='ID' value='$key'>
670 <input type='hidden' name='ACTION' value='$Lang::tr{'remove'}'>
677 # Print notice that currently no hosts are ignored.
679 print "<td class='base' colspan='2'>$Lang::tr{'guardian no entries'}</td>\n";
685 # Section to add new elements or edit existing ones.
695 # Assign correct headline and button text.
700 # Check if an ID (key) has been given, in this case an existing entry should be edited.
701 if ($settings{'ID'} ne '') {
702 $buttontext = $Lang::tr
{'update'};
703 print "<tr><td class='boldbase' colspan='3'><b>$Lang::tr{'update'}</b></td></tr>\n";
705 # Grab address and remark for the given key.
706 $entry_address = $ignored{$settings{'ID'}}[0];
707 $entry_remark = $ignored{$settings{'ID'}}[1];
709 $buttontext = $Lang::tr
{'add'};
710 print "<tr><td class='boldbase' colspan='3'><b>$Lang::tr{'dnsforward add a new entry'}</b></td></tr>\n";
714 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
715 <input type='hidden' name='ID' value='$settings{'ID'}'>
717 <td width='30%'>$Lang::tr{'ip address'}: </td>
718 <td width='50%'><input type='text' name='IGNORE_ENTRY_ADDRESS' value='$entry_address' size='24' /></td>
720 <td width='30%'>$Lang::tr{'remark'}: </td>
721 <td wicth='50%'><input type='text' name=IGNORE_ENTRY_REMARK value='$entry_remark' size='24' /></td>
722 <td align='center' width='20%'><input type='submit' name='ACTION' value='$buttontext' /></td>
732 # Function to list currently blocked addresses from guardian and unblock them or add custom entries to block.
733 sub showBlockedBox
() {
734 &Header
::openbox
('100%', 'center', $Lang::tr
{'guardian blocked hosts'});
739 <td colspan='2' class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'guardian blocked hosts'}</b></td>
743 # Launch function to get the currently blocked hosts.
744 my @blocked_hosts = &GetBlockedHosts
();
749 # Loop through our blocked hosts array.
750 foreach my $blocked_host (@blocked_hosts) {
752 # Increase id number for each element in the ignore file.
755 # Check if the id number is even or not.
757 $col="bgcolor='$color{'color22'}'";
759 $col="bgcolor='$color{'color20'}'";
764 <td width='80%' class='base' $col><a href='/cgi-bin/ipinfo.cgi?ip=$blocked_host'>$blocked_host</a></td>
765 <td width='20%' align='center' $col>
766 <form method='post' name='frmb$id' action='$ENV{'SCRIPT_NAME'}'>
767 <input type='image' name='$Lang::tr{'unblock'}' src='/images/delete.gif' title='$Lang::tr{'unblock'}' alt='$Lang::tr{'unblock'}'>
768 <input type='hidden' name='ADDRESS_UNBLOCK' value='$blocked_host'>
769 <input type='hidden' name='ACTION' value='$Lang::tr{'unblock'}'>
776 # If the loop only has been run once the id still is "0", which means there are no
777 # additional entries (blocked hosts) in the iptables chain.
780 # Print notice that currently no hosts are blocked.
782 print "<td class='base' colspan='2'>$Lang::tr{'guardian no entries'}</td>\n";
788 # Section for a manual block of an IP-address.
792 <table width='60%' border='0'>
793 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
795 <td width='30%'>$Lang::tr{'guardian block a host'}: </td>
796 <td width='40%'><input type='text' name='ADDRESS_BLOCK' value='' size='24' /></td>
797 <td align='center' width='15%'><input type='submit' name='ACTION' value='$Lang::tr{'block'}'></td>
798 <td align='center' width='15%'><input type='submit' name='ACTION' value='$Lang::tr{'unblock all'}'></td>
808 &Header
::closebigbox
();
809 &Header
::closepage
();
811 # Function to check if guardian has been started.
812 # Grab process id and consumed memory if the daemon is running.
816 open(FILE
, '/usr/local/bin/addonctrl guardian status | ');
819 $string = join("", @guardian);
820 $string =~ s/[a-z_]//gi;
821 $string =~ s/\[[0-1]\;[0-9]+//gi;
822 $string =~ s/[\(\)\.]//gi;
825 @pid = split(/\s/,$string);
826 if (open(FILE
, "/proc/$pid[0]/statm")){
828 @memory = split(/ /,$temp);
834 sub GetBlockedHosts
() {
835 # Create new, empty array.
838 # Launch helper to get chains from iptables.
839 system('/usr/local/bin/getipstat');
841 # Open temporary file which contains the chains and rules.
842 open (FILE
, '/var/tmp/iptables.txt');
844 # Loop through the entire file.
848 # Search for the guardian chain and extract
849 # the lines between it and the next empty line
850 # which is placed before the next firewall
852 if ($line =~ /^Chain GUARDIAN/ .. /^\s*$/) {
853 # Skip descriptive lines.
854 next if ($line =~ /^Chain/);
855 next if ($line =~ /^ pkts/);
857 # Generate array, based on the line content (separator is a single or multiple space)
858 my @comps = split(/\s{1,}/, $line);
859 my ($lead, $pkts, $bytes, $target, $prot, $opt, $in, $out, $source, $destination) = @comps;
861 # Assign different variable names.
862 my $blocked_host = $source;
864 # Add host to our hosts array.
866 push(@hosts, $blocked_host);
874 # Remove recently created temporary files of the "getipstat" binary.
875 system("rm -f /var/tmp/iptables.txt");
876 system("rm -f /var/tmp/iptablesmangle.txt");
877 system("rm -f /var/tmp/iptablesnat.txt");
879 # Convert entries, sort them, write back and store the sorted entries into new array.
880 my @sorted = map { $_->[0] }
881 sort { $a->[1] <=> $b->[1] }
882 map { [$_, int sprintf("%03.f%03.f%03.f%03.f", split(/\./, $_))] }
885 # Return our sorted list.
889 sub BuildConfiguration
() {
891 &General
::readhash
("${General::swroot}/guardian/settings", \
%settings);
893 my $configfile = "${General::swroot}/guardian/guardian.conf";
895 # Create the configfile if none exists yet.
896 unless (-e
"$configfile") { system("touch $configfile"); }
898 # Open configfile for writing.
899 open(FILE
, ">$configfile");
901 # Config file header.
902 print FILE
"# Autogenerated configuration file.\n";
903 print FILE
"# All user modifications will be overwritten.\n\n";
905 # Settings for the logging mechanism.
906 print FILE
"# Log settings.\n";
907 print FILE
"LogFacility = $settings{'GUARDIAN_LOG_FACILITY'}\n";
909 if ($settings{'GUARDIAN_LOG_FACILITY'} eq "file") {
910 print FILE
"LogFile = $settings{'GUARDIAN_LOGFILE'}\n";
913 print FILE
"LogLevel = $settings{'GUARDIAN_LOGLEVEL'}\n\n";
915 # IPFire related static settings.
916 print FILE
"# IPFire related settings.\n";
917 print FILE
"FirewallEngine = IPtables\n";
918 print FILE
"SocketOwner = nobody:nobody\n";
919 print FILE
"IgnoreFile = $ignorefile\n\n";
921 # Configured block values.
922 print FILE
"# Configured block settings.\n";
923 print FILE
"BlockCount = $settings{'GUARDIAN_BLOCKCOUNT'}\n";
924 print FILE
"BlockTime = $settings{'GUARDIAN_BLOCKTIME'}\n";
925 print FILE
"FirewallAction = $settings{'GUARDIAN_FIREWALL_ACTION'}\n\n";
928 # Loop through whole settings hash.
929 print FILE
"# Enabled modules.\n";
930 foreach my $option (keys %settings) {
931 # Search for enabled modules.
932 if ($option =~ /GUARDIAN_MONITOR_(.*)/) {
933 # Skip if module is not enabled.
934 next unless($settings{$option} eq "on");
936 # Skip module if no file location is available.
937 next unless(exists($module_file_locations{$1}));
939 # Add enabled module and defined path to the config file.
940 print FILE
"Monitor_$1 = $module_file_locations{$1}\n";
945 print FILE
"\n# Module settings.\n";
948 # Generate ignore file.
949 &GenerateIgnoreFile
();
951 # Check if guardian should be started or stopped.
952 if($settings{'GUARDIAN_ENABLED'} eq 'on') {
954 # Send reload command through socket connection.
955 &Guardian
::Socket
::Client
("reload");
958 system("/usr/local/bin/addonctrl guardian start &>/dev/null");
962 system("/usr/local/bin/addonctrl guardian stop &>/dev/null");
966 sub GenerateIgnoreFile
() {
969 # Read-in ignoredfile.
970 &General
::readhasharray
($ignoredfile, \
%ignored);
972 # Create the guardian.ignore file if not exist yet.
973 unless (-e
"$ignorefile") { system("touch $ignorefile"); }
975 # Open ignorefile for writing.
976 open(FILE
, ">$ignorefile");
978 # Config file header.
979 print FILE
"# Autogenerated configuration file.\n";
980 print FILE
"# All user modifications will be overwritten.\n\n";
982 # Add IFPire interfaces and gateway to the ignore file.
984 # Assign some temporary variables for the IPFire interfaces.
985 my $green = $netsettings{'GREEN_ADDRESS'};
986 my $blue = $netsettings{'BLUE_ADDRESS'};
987 my $orange = $netsettings{'ORANGE_ADDRESS'};
990 my $public_address_file = "${General::swroot}/red/local-ipaddress";
991 my $gatewayfile = "${General::swroot}/red/remote-ipaddress";
992 my $dns1file = "${General::swroot}/red/dns1";
993 my $dns2file = "${General::swroot}/red/dns2";
995 # Write the obtained addresses to the ignore file.
996 print FILE
"# IPFire local interfaces.\n";
997 print FILE
"$green\n";
999 # Check if a blue interface exists.
1002 print FILE
"$blue\n";
1005 # Check if an orange interface exists.
1007 # Add orange address.
1008 print FILE
"$orange\n";
1011 print FILE
"\n# IPFire red interface, gateway and used DNS-servers.\n";
1012 print FILE
"# Include the corresponding files to obtain the addresses.\n";
1013 print FILE
"Include_File = $public_address_file\n";
1014 print FILE
"Include_File = $gatewayfile\n";
1015 print FILE
"Include_File = $dns1file\n";
1016 print FILE
"Include_File = $dns2file\n";
1018 # Add all user defined hosts and networks to the ignore file.
1020 # Check if the hash contains any elements.
1021 if (keys (%ignored)) {
1023 print FILE
"\n# User defined hosts/networks.\n";
1025 # Loop through the entire hash and write the host/network
1026 # and remark to the ignore file.
1027 while ( (my $key) = each %ignored) {
1028 my $address = $ignored{$key}[0];
1029 my $remark = $ignored{$key}[1];
1030 my $status = $ignored{$key}[2];
1032 # Check if the status of the entry is "enabled".
1033 if ($status eq "enabled") {
1034 # Check if the address/network is valid.
1035 if ((&General
::validip
($address)) || (&General
::validipandmask
($address))) {
1036 # Write the remark to the file.
1037 print FILE
"# $remark\n";
1039 # Write the address/network to the ignore file.
1040 print FILE
"$address\n\n";
1049 # Private subfunction to obtain IP-addresses from given file names.
1051 sub _get_address_from_file
($) {
1054 # Check if the file exists.
1056 # Open the given file.
1057 open(FILE
, "$file") or die "Could not open $file.";
1059 # Obtain the address from the first line of the file.
1060 my $address = <FILE
>;
1068 # Check if the grabbed address is valid.
1069 if (&General
::validip
($address)) {
1070 # Return the address.