]>
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 "SNORT" => "/var/log/snort/alert",
56 "SSH" => "/var/log/messages",
59 our %netsettings = ();
60 &General
::readhash
("${General::swroot}/ethernet/settings", \
%netsettings);
63 our %mainsettings = ();
64 &General
::readhash
("${General::swroot}/main/settings", \
%mainsettings);
65 &General
::readhash
("/srv/web/ipfire/html/themes/".$mainsettings{'THEME'}."/include/colors.txt", \
%color);
68 my $settingsfile = "${General::swroot}/guardian/settings";
69 my $ignoredfile = "${General::swroot}/guardian/ignored";
71 # Create empty settings and ignoredfile if they do not exist yet.
72 unless (-e
"$settingsfile") { system("touch $settingsfile"); }
73 unless (-e
"$ignoredfile") { system("touch $ignoredfile"); }
78 $settings{'ACTION'} = '';
80 $settings{'GUARDIAN_ENABLED'} = 'off';
81 $settings{'GUARDIAN_MONITOR_SNORT'} = 'on';
82 $settings{'GUARDIAN_MONITOR_SSH'} = 'on';
83 $settings{'GUARDIAN_MONITOR_HTTPD'} = 'on';
84 $settings{'GUARDIAN_MONITOR_OWNCLOUD'} = '';
85 $settings{'GUARDIAN_LOG_FACILITY'} = 'syslog';
86 $settings{'GUARDIAN_LOGLEVEL'} = 'info';
87 $settings{'GUARDIAN_BLOCKCOUNT'} = '3';
88 $settings{'GUARDIAN_BLOCKTIME'} = '86400';
89 $settings{'GUARDIAN_FIREWALL_ACTION'} = 'DROP';
90 $settings{'GUARDIAN_LOGFILE'} = '/var/log/guardian/guardian.log';
91 $settings{'GUARDIAN_SNORT_PRIORITY_LEVEL'} = '3';
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 blockcount 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/edit an entry to the ignore file.
133 } elsif (($settings{'ACTION'} eq $Lang::tr
{'add'}) || ($settings{'ACTION'} eq $Lang::tr
{'update'})) {
135 # Check if any input has been performed.
136 if ($settings{'IGNORE_ENTRY_ADDRESS'} 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{'IGNORE_ENTRY_ADDRESS'})) && (!&General
::validipandmask
($settings{'IGNORE_ENTRY_ADDRESS'}))) {
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 '') {
152 # Assign hash values.
153 my $new_entry_address = $settings{'IGNORE_ENTRY_ADDRESS'};
154 my $new_entry_remark = $settings{'IGNORE_ENTRY_REMARK'};
156 # Read-in ignoredfile.
157 &General
::readhasharray
($ignoredfile, \
%ignored);
159 # Check if we should edit an existing entry and got an ID.
160 if (($settings{'ACTION'} eq $Lang::tr
{'update'}) && ($settings{'ID'})) {
161 # Assin the provided id.
162 $id = $settings{'ID'};
164 # Undef the given ID.
165 undef($settings{'ID'});
167 # Grab the configured status of the corresponding entry.
168 $status = $ignored{$id}[2];
170 # Each newly added entry automatically should be enabled.
173 # Generate the ID for the new entry.
175 # Sort the keys by their ID and store them in an array.
176 my @keys = sort { $a <=> $b } keys %ignored;
178 # Reverse the key array.
179 my @reversed = reverse(@keys);
181 # Obtain the last used id.
182 my $last_id = @reversed[0];
184 # Increase the last id by one and use it as id for the new entry.
188 # Add/Modify the entry to/in the ignored hash.
189 $ignored{$id} = ["$new_entry_address", "$new_entry_remark", "$status"];
191 # Write the changed ignored hash to the ignored file.
192 &General
::writehasharray
($ignoredfile, \
%ignored);
194 # Regenerate the ignore file.
195 &GenerateIgnoreFile
();
198 # Check if guardian is running.
200 # Send reload command through socket connection.
201 &Guardian
::Socket
::Client
("reload-ignore-list");
204 ## Toggle Enabled/Disabled for an existing entry on the ignore list.
207 } elsif ($settings{'ACTION'} eq $Lang::tr
{'toggle enable disable'}) {
210 # Only go further, if an ID has been passed.
211 if ($settings{'ID'}) {
212 # Assign the given ID.
213 my $id = $settings{'ID'};
215 # Undef the given ID.
216 undef($settings{'ID'});
218 # Read-in ignoredfile.
219 &General
::readhasharray
($ignoredfile, \
%ignored);
221 # Grab the configured status of the corresponding entry.
222 my $status = $ignored{$id}[2];
225 if ($status eq "disabled") {
228 $status = "disabled";
231 # Modify the status of the existing entry.
232 $ignored{$id} = ["$ignored{$id}[0]", "$ignored{$id}[1]", "$status"];
234 # Write the changed ignored hash to the ignored file.
235 &General
::writehasharray
($ignoredfile, \
%ignored);
237 # Regenerate the ignore file.
238 &GenerateIgnoreFile
();
240 # Check if guardian is running.
242 # Send reload command through socket connection.
243 &Guardian
::Socket
::Client
("reload-ignore-list");
247 ## Remove entry from ignore list.
249 } elsif ($settings{'ACTION'} eq $Lang::tr
{'remove'}) {
252 # Read-in ignoredfile.
253 &General
::readhasharray
($ignoredfile, \
%ignored);
255 # Drop entry from the hash.
256 delete($ignored{$settings{'ID'}});
258 # Undef the given ID.
259 undef($settings{'ID'});
261 # Write the changed ignored hash to the ignored file.
262 &General
::writehasharray
($ignoredfile, \
%ignored);
264 # Regenerate the ignore file.
265 &GenerateIgnoreFile
();
267 # Check if guardian is running.
269 # Send reload command through socket connection.
270 &Guardian
::Socket
::Client
("reload-ignore-list");
273 ## Block a user given address or subnet.
275 } elsif ($settings{'ACTION'} eq $Lang::tr
{'block'}) {
277 # Assign some temporary variables used for input validation.
278 my $input = $settings{'ADDRESS_BLOCK'};
279 my $green = $netsettings{'GREEN_ADDRESS'};
280 my $blue = $netsettings{'BLUE_ADDRESS'};
281 my $orange = $netsettings{'ORANGE_ADDRESS'};
282 my $red = $netsettings{'RED_ADDRESS'};
285 my $gatewayfile = "${General::swroot}/red/remote-ipaddress";
286 my $dns1file = "${General::swroot}/red/dns1";
287 my $dns2file = "${General::swroot}/red/dns2";
289 # Get gateway address.
290 my $gateway = &_get_address_from_file
($gatewayfile);
292 # Get addresses from the used dns servers.
293 my $dns1 = &_get_address_from_file
($dns1file);
294 my $dns2 = &_get_address_from_file
($dns2file);
296 # Check if any input has been performed.
298 $errormessage = "$Lang::tr{'guardian empty input'}";
301 # Check if the given input is localhost (127.0.0.1).
302 elsif ($input eq "127.0.0.1") {
303 $errormessage = "$Lang::tr{'guardian blocking of this address is not allowed'}";
306 # Check if the given input is anywhere (0.0.0.0).
307 elsif ($input eq "0.0.0.0") {
308 $errormessage = "$Lang::tr{'guardian blocking of this address is not allowed'}";
311 # Check if the given input is one of the interface addresses or our gateway.
312 elsif ($input eq "$green" || $input eq "$blue" || $input eq "$orange" || $input eq "$red" || $input eq "$gateway" || $input eq "$dns1" || $input eq "$dns2") {
313 $errormessage = "$Lang::tr{'guardian blocking of this address is not allowed'}";
316 # Check if the given input is a valid IP address.
317 elsif (!&General
::validip
($input)) {
318 $errormessage = "$Lang::tr{'guardian invalid address or subnet'}";
321 # Go further if there was no error.
322 if ($errormessage eq '') {
323 my $block = $settings{'ADDRESS_BLOCK'};
325 # Send command to block the specified address through socket connection.
326 &Guardian
::Socket
::Client
("block $block");
329 ## Unblock address or subnet.
331 } elsif ($settings{'ACTION'} eq $Lang::tr
{'unblock'}) {
333 # Check if no empty input has been performed.
334 if ($settings{'ADDRESS_UNBLOCK'} ne '') {
336 # Check if the given input is no valid IP-address or IP-address with subnet, display an error message.
337 if ((!&General
::validip
($settings{'ADDRESS_UNBLOCK'})) && (!&General
::validipandmask
($settings{'ADDRESS_UNBLOCK'}))) {
338 $errormessage = "$Lang::tr{'guardian invalid address or subnet'}";
342 $errormessage = "$Lang::tr{'guardian empty input'}";
345 # Go further if there was no error.
346 if ($errormessage eq '') {
347 my $unblock = $settings{'ADDRESS_UNBLOCK'};
349 # Send command to unblock the given address through socket connection.
350 &Guardian
::Socket
::Client
("unblock $unblock");
355 } elsif ($settings{'ACTION'} eq $Lang::tr
{'unblock all'}) {
357 # Send flush command through socket connection.
358 &Guardian
::Socket
::Client
("flush");
361 # Load settings from files.
362 &General
::readhash
("${General::swroot}/guardian/settings", \
%settings);
363 &General
::readhasharray
("${General::swroot}/guardian/ignored", \
%ignored);
365 # Call functions to generate whole page.
369 # Display area only if guardian is running.
370 if ( ($memory != 0) && ($pid > 0) ) {
374 # Function to display the status of guardian and allow base configuration.
379 $checked{'GUARDIAN_ENABLED'}{'on'} = '';
380 $checked{'GUARDIAN_ENABLED'}{'off'} = '';
381 $checked{'GUARDIAN_ENABLED'}{$settings{'GUARDIAN_ENABLED'}} = 'checked';
382 $checked{'GUARDIAN_MONITOR_SNORT'}{'off'} = '';
383 $checked{'GUARDIAN_MONITOR_SNORT'}{'on'} = '';
384 $checked{'GUARDIAN_MONITOR_SNORT'}{$settings{'GUARDIAN_MONITOR_SNORT'}} = "checked='checked'";
385 $checked{'GUARDIAN_MONITOR_SSH'}{'off'} = '';
386 $checked{'GUARDIAN_MONITOR_SSH'}{'on'} = '';
387 $checked{'GUARDIAN_MONITOR_SSH'}{$settings{'GUARDIAN_MONITOR_SSH'}} = "checked='checked'";
388 $checked{'GUARDIAN_MONITOR_HTTPD'}{'off'} = '';
389 $checked{'GUARDIAN_MONITOR_HTTPD'}{'on'} = '';
390 $checked{'GUARDIAN_MONITOR_HTTPD'}{$settings{'GUARDIAN_MONITOR_HTTPD'}} = "checked='checked'";
391 $checked{'GUARDIAN_MONITOR_OWNCLOUD'}{'off'} = '';
392 $checked{'GUARDIAN_MONITOR_OWNCLOUD'}{'on'} = '';
393 $checked{'GUARDIAN_MONITOR_OWNCLOUD'}{$settings{'GUARDIAN_MONITOR_OWNCLOUD'}} = "checked='checked'";
395 $selected{'GUARDIAN_LOG_FACILITY'}{$settings{'GUARDIAN_LOG_FACILITY'}} = 'selected';
396 $selected{'GUARDIAN_LOGLEVEL'}{$settings{'GUARDIAN_LOGLEVEL'}} = 'selected';
397 $selected{'GUARDIAN_SNORT_PRIORITY_LEVEL'}{$settings{'GUARDIAN_SNORT_PRIORITY_LEVEL'}} = 'selected';
398 $selected{'GUARDIAN_FIREWALL_ACTION'}{$settings{'GUARDIAN_FIREWALL_ACTION'}} = 'selected';
400 &Header
::openpage
($Lang::tr
{'guardian configuration'}, 1, '');
401 &Header
::openbigbox
('100%', 'left', '', $errormessage);
403 # Print errormessage if there is one.
405 &Header
::openbox
('100%', 'left', $Lang::tr
{'error messages'});
406 print "<font class='base'>$errormessage </font>\n";
413 var update_options = function() {
415 var logfacility = \$("#GUARDIAN_LOG_FACILITY").val();
416 var loglevel = \$("#GUARDIAN_LOGLEVEL").val();
418 if (logfacility === undefined)
421 if (loglevel === undefined)
424 // Show / Hide input for specifying the path to the logfile.
425 if (logfacility === "file") {
426 \$(".GUARDIAN_LOGFILE").show();
428 \$(".GUARDIAN_LOGFILE").hide();
431 // Show / Hide loglevel debug if the facility is set to syslog.
432 if (logfacility === "syslog") {
433 \$("#loglevel_debug").hide();
435 \$("#loglevel_debug").show();
438 // Show / Hide logfacility syslog if the loglevel is set to debug.
439 if (loglevel === "debug") {
440 \$("#logfacility_syslog").hide();
442 \$("#logfacility_syslog").show();
446 \$(document).ready(function() {
447 \$("#GUARDIAN_LOG_FACILITY").change(update_options);
448 \$("#GUARDIAN_LOGLEVEL").change(update_options);
451 // Show / Hide snort priority level option, based if
452 // snort is enabled / disabled.
453 if (\$('input[name=GUARDIAN_MONITOR_SNORT]:checked').val() == 'on') {
454 \$('.GUARDIAN_SNORT_PRIORITY_LEVEL').show();
456 \$('.GUARDIAN_SNORT_PRIORITY_LEVEL').hide();
459 // Show/Hide snort priority level when GUARDIAN_MONITOR_SNORT get changed.
460 \$('input[name=GUARDIAN_MONITOR_SNORT]').change(function() {
461 \$('.GUARDIAN_SNORT_PRIORITY_LEVEL').toggle();
469 # Draw current guardian state.
470 &Header
::openbox
('100%', 'center', $Lang::tr
{'guardian'});
472 # Get current status of guardian.
476 # Display some useful information related to guardian, if daemon is running.
477 if ( ($memory != 0) && ($pid > 0) ){
479 <table width='95%' cellspacing='0' class='tbl'>
481 <th bgcolor='$color{'color20'}' colspan='3' align='left'><strong>$Lang::tr{'guardian service'}</strong></th>
484 <td class='base'>$Lang::tr{'guardian daemon'}</td>
485 <td align='center' colspan='2' width='75%' bgcolor='${Header::colourgreen}'><font color='white'><strong>$Lang::tr{'running'}</strong></font></td>
488 <td class='base'></td>
489 <td bgcolor='$color{'color20'}' align='center'><strong>PID</strong></td>
490 <td bgcolor='$color{'color20'}' align='center'><strong>$Lang::tr{'memory'}</strong></td>
493 <td class='base'></td>
494 <td bgcolor='$color{'color22'}' align='center'>$pid</td>
495 <td bgcolor='$color{'color22'}' align='center'>$memory KB</td>
500 # Otherwise display a hint that the service is not launched.
502 <table width='95%' cellspacing='0' class='tbl'>
504 <th bgcolor='$color{'color20'}' colspan='3' align='left'><strong>$Lang::tr{'guardian service'}</strong></th>
507 <td class='base'>$Lang::tr{'guardian daemon'}</td>
508 <td align='center' width='75%' bgcolor='${Header::colourred}'><font color='white'><strong>$Lang::tr{'stopped'}</strong></font></td>
516 # Draw elements for guardian configuration.
517 &Header
::openbox
('100%', 'center', $Lang::tr
{'guardian configuration'});
520 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
524 <td colspan='2' class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'guardian common settings'}</b></td>
528 <td width='25%' class='base'>$Lang::tr{'guardian enabled'}:</td>
529 <td><input type='checkbox' name='GUARDIAN_ENABLED' $checked{'GUARDIAN_ENABLED'}{'on'} /></td>
533 <td colspan='2'><br></td>
537 <td width='25%' class='base'>$Lang::tr{'guardian watch snort alertfile'}</td>
538 <td align='left'>on <input type='radio' name='GUARDIAN_MONITOR_SNORT' value='on' $checked{'GUARDIAN_MONITOR_SNORT'}{'on'} /> /
539 <input type='radio' name='GUARDIAN_MONITOR_SNORT' value='off' $checked{'GUARDIAN_MONITOR_SNORT'}{'off'} /> off</td>
543 <td width='25%' class='base'>$Lang::tr{'guardian block ssh brute-force'}</td>
544 <td align='left'>on <input type='radio' name='GUARDIAN_MONITOR_SSH' value='on' $checked{'GUARDIAN_MONITOR_SSH'}{'on'} /> /
545 <input type='radio' name='GUARDIAN_MONITOR_SSH' value='off' $checked{'GUARDIAN_MONITOR_SSH'}{'off'} /> off</td>
549 <td width='25%' class='base'>$Lang::tr{'guardian block httpd brute-force'}</td>
550 <td align='left'>on <input type='radio' name='GUARDIAN_MONITOR_HTTPD' value='on' $checked{'GUARDIAN_MONITOR_HTTPD'}{'on'} /> /
551 <input type='radio' name='GUARDIAN_MONITOR_HTTPD' value='off' $checked{'GUARDIAN_MONITOR_HTTPD'}{'off'} /> off</td>
555 <td colspan='2'><br></td>
559 <td align='left' width='20%'>$Lang::tr{'guardian logfacility'}:</td>
560 <td width='25%'><select id='GUARDIAN_LOG_FACILITY' name='GUARDIAN_LOG_FACILITY'>
561 <option id='logfacility_syslog' value='syslog' $selected{'GUARDIAN_LOG_FACILITY'}{'syslog'}>$Lang::tr{'guardian logtarget_syslog'}</option>
562 <option id='logfacility_file' value='file' $selected{'GUARDIAN_LOG_FACILITY'}{'file'}>$Lang::tr{'guardian logtarget_file'}</option>
563 <option id='logfacility_console' value='console' $selected{'GUARDIAN_LOG_FACILITY'}{'console'}>$Lang::tr{'guardian logtarget_console'}</option>
566 <td align='left' width='20%'>$Lang::tr{'guardian loglevel'}:</td>
567 <td width='25%'><select id='GUARDIAN_LOGLEVEL' name='GUARDIAN_LOGLEVEL'>
568 <option id='loglevel_off' value='off' $selected{'GUARDIAN_LOGLEVEL'}{'off'}>$Lang::tr{'guardian loglevel_off'}</option>
569 <option id='loglevel_info' value='info' $selected{'GUARDIAN_LOGLEVEL'}{'info'}>$Lang::tr{'guardian loglevel_info'}</option>
570 <option id='loglevel_debug' value='debug' $selected{'GUARDIAN_LOGLEVEL'}{'debug'}>$Lang::tr{'guardian loglevel_debug'}</option>
574 <tr class="GUARDIAN_LOGFILE">
575 <td colspan='2'><br></td>
578 <tr class="GUARDIAN_LOGFILE">
579 <td width='25%' class='base'>$Lang::tr{'guardian logfile'}:</td>
580 <td><input type='text' name='GUARDIAN_LOGFILE' value='$settings{'GUARDIAN_LOGFILE'}' size='30' /></td>
583 <tr class="GUARDIAN_SNORT_PRIORITY_LEVEL">
584 <td colspan='2'><br></td>
587 <tr class="GUARDIAN_SNORT_PRIORITY_LEVEL">
588 <td align='left' width='20%'>$Lang::tr{'guardian priority level'}:</td>
589 <td><select name='GUARDIAN_SNORT_PRIORITY_LEVEL'>
590 <option value='1' $selected{'GUARDIAN_SNORT_PRIORITY_LEVEL'}{'1'}>$Lang::tr{'guardian priolevel_high'}</option>
591 <option value='2' $selected{'GUARDIAN_SNORT_PRIORITY_LEVEL'}{'2'}>$Lang::tr{'guardian priolevel_medium'}</option>
592 <option value='3' $selected{'GUARDIAN_SNORT_PRIORITY_LEVEL'}{'3'}>$Lang::tr{'guardian priolevel_low'}</option>
593 <option value='4' $selected{'GUARDIAN_SNORT_PRIORITY_LEVEL'}{'4'}>$Lang::tr{'guardian priolevel_very_low'}</option>
596 <td width='25%' class='base'>$Lang::tr{'guardian blockcount'}:</td>
597 <td><input type='text' name='GUARDIAN_BLOCKCOUNT' value='$settings{'GUARDIAN_BLOCKCOUNT'}' size='5' /></td>
601 <td colspan='2'><br></td>
605 <td width='25%' class='base'>$Lang::tr{'guardian firewallaction'}:</td>
606 <td><select name='GUARDIAN_FIREWALL_ACTION'>
607 <option value='DROP' $selected{'GUARDIAN_FIREWALL_ACTION'}{'DROP'}>Drop</option>
608 <option value='REJECT' $selected{'GUARDIAN_FIREWALL_ACTION'}{'REJECT'}>Reject</option>
611 <td width='25%' class='base'>$Lang::tr{'guardian blocktime'}:</td>
612 <td><input type='text' name='GUARDIAN_BLOCKTIME' value='$settings{'GUARDIAN_BLOCKTIME'}' size='10' /></td>
624 <td align='center'><input type='submit' name='ACTION' value='$Lang::tr{'save'}' /></td>
634 # Function to show elements of the guardian ignorefile and allow to add or remove single members of it.
635 sub showIgnoreBox
() {
636 &Header
::openbox
('100%', 'center', $Lang::tr
{'guardian ignored hosts'});
641 <td class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'ip address'}</b></td>
642 <td class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'remark'}</b></td>
643 <td class='base' colspan='3' bgcolor='$color{'color20'}'></td>
646 # Check if some hosts have been added to be ignored.
647 if (keys (%ignored)) {
650 # Loop through all entries of the hash.
651 while( (my $key) = each %ignored) {
652 # Assign data array positions to some nice variable names.
653 my $address = $ignored{$key}[0];
654 my $remark = $ignored{$key}[1];
655 my $status = $ignored{$key}[2];
657 # Check if the key (id) number is even or not.
658 if ($settings{'ID'} eq $key) {
659 $col="bgcolor='${Header::colouryellow}'";
661 $col="bgcolor='$color{'color22'}'";
663 $col="bgcolor='$color{'color20'}'";
666 # Choose icon for the checkbox.
670 # Check if the status is enabled and select the correct image and description.
671 if ($status eq 'enabled' ) {
673 $gdesc = $Lang::tr
{'click to disable'};
676 $gdesc = $Lang::tr
{'click to enable'};
681 <td width='20%' class='base' $col>$address</td>
682 <td width='65%' class='base' $col>$remark</td>
684 <td align='center' $col>
685 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
686 <input type='hidden' name='ACTION' value='$Lang::tr{'toggle enable disable'}' />
687 <input type='image' name='$Lang::tr{'toggle enable disable'}' src='/images/$gif' alt='$gdesc' title='$gdesc' />
688 <input type='hidden' name='ID' value='$key' />
692 <td align='center' $col>
693 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
694 <input type='hidden' name='ACTION' value='$Lang::tr{'edit'}' />
695 <input type='image' name='$Lang::tr{'edit'}' src='/images/edit.gif' alt='$Lang::tr{'edit'}' title='$Lang::tr{'edit'}' />
696 <input type='hidden' name='ID' value='$key' />
700 <td align='center' $col>
701 <form method='post' name='$key' action='$ENV{'SCRIPT_NAME'}'>
702 <input type='image' name='$Lang::tr{'remove'}' src='/images/delete.gif' title='$Lang::tr{'remove'}' alt='$Lang::tr{'remove'}'>
703 <input type='hidden' name='ID' value='$key'>
704 <input type='hidden' name='ACTION' value='$Lang::tr{'remove'}'>
711 # Print notice that currently no hosts are ignored.
713 print "<td class='base' colspan='2'>$Lang::tr{'guardian no entries'}</td>\n";
719 # Section to add new elements or edit existing ones.
729 # Assign correct headline and button text.
734 # Check if an ID (key) has been given, in this case an existing entry should be edited.
735 if ($settings{'ID'} ne '') {
736 $buttontext = $Lang::tr
{'update'};
737 print "<tr><td class='boldbase' colspan='3'><b>$Lang::tr{'update'}</b></td></tr>\n";
739 # Grab address and remark for the given key.
740 $entry_address = $ignored{$settings{'ID'}}[0];
741 $entry_remark = $ignored{$settings{'ID'}}[1];
743 $buttontext = $Lang::tr
{'add'};
744 print "<tr><td class='boldbase' colspan='3'><b>$Lang::tr{'dnsforward add a new entry'}</b></td></tr>\n";
748 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
749 <input type='hidden' name='ID' value='$settings{'ID'}'>
751 <td width='30%'>$Lang::tr{'ip address'}: </td>
752 <td width='50%'><input type='text' name='IGNORE_ENTRY_ADDRESS' value='$entry_address' size='24' /></td>
754 <td width='30%'>$Lang::tr{'remark'}: </td>
755 <td wicth='50%'><input type='text' name=IGNORE_ENTRY_REMARK value='$entry_remark' size='24' /></td>
756 <td align='center' width='20%'><input type='submit' name='ACTION' value='$buttontext' /></td>
766 # Function to list currently blocked addresses from guardian and unblock them or add custom entries to block.
767 sub showBlockedBox
() {
768 &Header
::openbox
('100%', 'center', $Lang::tr
{'guardian blocked hosts'});
773 <td colspan='2' class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'guardian blocked hosts'}</b></td>
777 # Launch function to get the currently blocked hosts.
778 my @blocked_hosts = &GetBlockedHosts
();
783 # Loop through our blocked hosts array.
784 foreach my $blocked_host (@blocked_hosts) {
786 # Increase id number for each element in the ignore file.
789 # Check if the id number is even or not.
791 $col="bgcolor='$color{'color22'}'";
793 $col="bgcolor='$color{'color20'}'";
798 <td width='80%' class='base' $col><a href='/cgi-bin/ipinfo.cgi?ip=$blocked_host'>$blocked_host</a></td>
799 <td width='20%' align='center' $col>
800 <form method='post' name='frmb$id' action='$ENV{'SCRIPT_NAME'}'>
801 <input type='image' name='$Lang::tr{'unblock'}' src='/images/delete.gif' title='$Lang::tr{'unblock'}' alt='$Lang::tr{'unblock'}'>
802 <input type='hidden' name='ADDRESS_UNBLOCK' value='$blocked_host'>
803 <input type='hidden' name='ACTION' value='$Lang::tr{'unblock'}'>
810 # If the loop only has been run once the id still is "0", which means there are no
811 # additional entries (blocked hosts) in the iptables chain.
814 # Print notice that currently no hosts are blocked.
816 print "<td class='base' colspan='2'>$Lang::tr{'guardian no entries'}</td>\n";
822 # Section for a manual block of an IP-address.
826 <table width='60%' border='0'>
827 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
829 <td width='30%'>$Lang::tr{'guardian block a host'}: </td>
830 <td width='40%'><input type='text' name='ADDRESS_BLOCK' value='' size='24' /></td>
831 <td align='center' width='15%'><input type='submit' name='ACTION' value='$Lang::tr{'block'}'></td>
832 <td align='center' width='15%'><input type='submit' name='ACTION' value='$Lang::tr{'unblock all'}'></td>
842 &Header
::closebigbox
();
843 &Header
::closepage
();
845 # Function to check if guardian has been started.
846 # Grab process id and consumed memory if the daemon is running.
850 open(FILE
, '/usr/local/bin/addonctrl guardian status | ');
853 $string = join("", @guardian);
854 $string =~ s/[a-z_]//gi;
855 $string =~ s/\[[0-1]\;[0-9]+//gi;
856 $string =~ s/[\(\)\.]//gi;
859 @pid = split(/\s/,$string);
860 if (open(FILE
, "/proc/$pid[0]/statm")){
862 @memory = split(/ /,$temp);
868 sub GetBlockedHosts
() {
869 # Create new, empty array.
872 # Launch helper to get chains from iptables.
873 system('/usr/local/bin/getipstat');
875 # Open temporary file which contains the chains and rules.
876 open (FILE
, '/var/tmp/iptables.txt');
878 # Loop through the entire file.
882 # Search for the guardian chain and extract
883 # the lines between it and the next empty line
884 # which is placed before the next firewall
886 if ($line =~ /^Chain GUARDIAN/ .. /^\s*$/) {
887 # Skip descriptive lines.
888 next if ($line =~ /^Chain/);
889 next if ($line =~ /^ pkts/);
891 # Generate array, based on the line content (separator is a single or multiple space)
892 my @comps = split(/\s{1,}/, $line);
893 my ($lead, $pkts, $bytes, $target, $prot, $opt, $in, $out, $source, $destination) = @comps;
895 # Assign different variable names.
896 my $blocked_host = $source;
898 # Add host to our hosts array.
900 push(@hosts, $blocked_host);
908 # Remove recently created temporary files of the "getipstat" binary.
909 system("rm -f /var/tmp/iptables.txt");
910 system("rm -f /var/tmp/iptablesmangle.txt");
911 system("rm -f /var/tmp/iptablesnat.txt");
913 # Convert entries, sort them, write back and store the sorted entries into new array.
914 my @sorted = map { $_->[0] }
915 sort { $a->[1] <=> $b->[1] }
916 map { [$_, int sprintf("%03.f%03.f%03.f%03.f", split(/\./, $_))] }
919 # Return our sorted list.
923 sub BuildConfiguration
() {
925 &General
::readhash
("${General::swroot}/guardian/settings", \
%settings);
927 my $configfile = "${General::swroot}/guardian/guardian.conf";
929 # Create the configfile if none exists yet.
930 unless (-e
"$configfile") { system("touch $configfile"); }
932 # Open configfile for writing.
933 open(FILE
, ">$configfile");
935 # Config file header.
936 print FILE
"# Autogenerated configuration file.\n";
937 print FILE
"# All user modifications will be overwritten.\n\n";
939 # Settings for the logging mechanism.
940 print FILE
"# Log settings.\n";
941 print FILE
"LogFacility = $settings{'GUARDIAN_LOG_FACILITY'}\n";
943 if ($settings{'GUARDIAN_LOG_FACILITY'} eq "file") {
944 print FILE
"LogFile = $settings{'GUARDIAN_LOGFILE'}\n";
947 print FILE
"LogLevel = $settings{'GUARDIAN_LOGLEVEL'}\n\n";
949 # IPFire related static settings.
950 print FILE
"# IPFire related settings.\n";
951 print FILE
"FirewallEngine = IPtables\n";
952 print FILE
"SocketOwner = nobody:nobody\n";
953 print FILE
"IgnoreFile = $ignorefile\n\n";
955 # Configured block values.
956 print FILE
"# Configured block settings.\n";
957 print FILE
"BlockCount = $settings{'GUARDIAN_BLOCKCOUNT'}\n";
958 print FILE
"BlockTime = $settings{'GUARDIAN_BLOCKTIME'}\n";
959 print FILE
"FirewallAction = $settings{'GUARDIAN_FIREWALL_ACTION'}\n\n";
962 # Loop through whole settings hash.
963 print FILE
"# Enabled modules.\n";
964 foreach my $option (keys %settings) {
965 # Search for enabled modules.
966 if ($option =~ /GUARDIAN_MONITOR_(.*)/) {
967 # Skip if module is not enabled.
968 next unless($settings{$option} eq "on");
970 # Skip module if no file location is available.
971 next unless(exists($module_file_locations{$1}));
973 # Add enabled module and defined path to the config file.
974 print FILE
"Monitor_$1 = $module_file_locations{$1}\n";
979 print FILE
"\n# Module settings.\n";
980 # Check if SNORT is enabled and add snort priority.
981 if ($settings{'GUARDIAN_MONITOR_SNORT'} eq "on") {
982 print FILE
"SnortPriorityLevel = $settings{'GUARDIAN_SNORT_PRIORITY_LEVEL'}\n";
987 # Generate ignore file.
988 &GenerateIgnoreFile
();
990 # Check if guardian should be started or stopped.
991 if($settings{'GUARDIAN_ENABLED'} eq 'on') {
993 # Send reload command through socket connection.
994 &Guardian
::Socket
::Client
("reload");
997 system("/usr/local/bin/addonctrl guardian start &>/dev/null");
1001 system("/usr/local/bin/addonctrl guardian stop &>/dev/null");
1005 sub GenerateIgnoreFile
() {
1008 # Read-in ignoredfile.
1009 &General
::readhasharray
($ignoredfile, \
%ignored);
1011 # Create the guardian.ignore file if not exist yet.
1012 unless (-e
"$ignorefile") { system("touch $ignorefile"); }
1014 # Open ignorefile for writing.
1015 open(FILE
, ">$ignorefile");
1017 # Config file header.
1018 print FILE
"# Autogenerated configuration file.\n";
1019 print FILE
"# All user modifications will be overwritten.\n\n";
1021 # Add IFPire interfaces and gateway to the ignore file.
1023 # Assign some temporary variables for the IPFire interfaces.
1024 my $green = $netsettings{'GREEN_ADDRESS'};
1025 my $blue = $netsettings{'BLUE_ADDRESS'};
1026 my $orange = $netsettings{'ORANGE_ADDRESS'};
1028 # File declarations.
1029 my $public_address_file = "${General::swroot}/red/local-ipaddress";
1030 my $gatewayfile = "${General::swroot}/red/remote-ipaddress";
1031 my $dns1file = "${General::swroot}/red/dns1";
1032 my $dns2file = "${General::swroot}/red/dns2";
1034 # Write the obtained addresses to the ignore file.
1035 print FILE
"# IPFire local interfaces.\n";
1036 print FILE
"$green\n";
1038 # Check if a blue interface exists.
1041 print FILE
"$blue\n";
1044 # Check if an orange interface exists.
1046 # Add orange address.
1047 print FILE
"$orange\n";
1050 print FILE
"\n# IPFire red interface, gateway and used DNS-servers.\n";
1051 print FILE
"# Include the corresponding files to obtain the addresses.\n";
1052 print FILE
"Include_File = $public_address_file\n";
1053 print FILE
"Include_File = $gatewayfile\n";
1054 print FILE
"Include_File = $dns1file\n";
1055 print FILE
"Include_File = $dns2file\n";
1057 # Add all user defined hosts and networks to the ignore file.
1059 # Check if the hash contains any elements.
1060 if (keys (%ignored)) {
1062 print FILE
"\n# User defined hosts/networks.\n";
1064 # Loop through the entire hash and write the host/network
1065 # and remark to the ignore file.
1066 while ( (my $key) = each %ignored) {
1067 my $address = $ignored{$key}[0];
1068 my $remark = $ignored{$key}[1];
1069 my $status = $ignored{$key}[2];
1071 # Check if the status of the entry is "enabled".
1072 if ($status eq "enabled") {
1073 # Check if the address/network is valid.
1074 if ((&General
::validip
($address)) || (&General
::validipandmask
($address))) {
1075 # Write the remark to the file.
1076 print FILE
"# $remark\n";
1078 # Write the address/network to the ignore file.
1079 print FILE
"$address\n\n";
1088 # Private subfunction to obtain IP-addresses from given file names.
1090 sub _get_address_from_file
($) {
1093 # Check if the file exists.
1095 # Open the given file.
1096 open(FILE
, "$file") or die "Could not open $file.";
1098 # Obtain the address from the first line of the file.
1099 my $address = <FILE
>;
1107 # Check if the grabbed address is valid.
1108 if (&General
::validip
($address)) {
1109 # Return the address.