]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blame - html/cgi-bin/guardian.cgi
ddns: Import latest upstream patches for ddns-013
[people/pmueller/ipfire-2.x.git] / html / cgi-bin / guardian.cgi
CommitLineData
01dbccb1
SS
1#!/usr/bin/perl
2###############################################################################
3# #
4# IPFire.org - A linux based firewall #
b5f7d903 5# Copyright (C) 2016 IPFire Team <info@ipfire.org> #
01dbccb1
SS
6# #
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. #
11# #
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. #
16# #
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/>. #
19# #
20###############################################################################
21
22use strict;
b5f7d903 23use Locale::Codes::Country;
af6856af 24use Guardian::Socket;
01dbccb1
SS
25
26# enable only the following on debugging purpose
eff1feb8
SS
27#use warnings;
28#use CGI::Carp 'fatalsToBrowser';
01dbccb1
SS
29
30require '/var/ipfire/general-functions.pl';
31require "${General::swroot}/lang.pl";
32require "${General::swroot}/header.pl";
33
34#workaround to suppress a warning when a variable is used only once
b1597f87
MF
35my @dummy = (
36 ${Header::colourred},
37 ${Header::colourgreen}
38);
39
01dbccb1
SS
40undef (@dummy);
41
42my $string=();
43my $memory=();
44my @memory=();
45my @pid=();
46my @guardian=();
01dbccb1
SS
47
48# Path to the guardian.ignore file.
49my $ignorefile ='/var/ipfire/guardian/guardian.ignore';
50
52958991
SS
51# Hash which contains the supported modules and the
52# file locations on IPFire systems.
53my %module_file_locations = (
54 "HTTPD" => "/var/log/httpd/error_log",
52958991
SS
55 "SSH" => "/var/log/messages",
56);
57
01dbccb1
SS
58our %netsettings = ();
59&General::readhash("${General::swroot}/ethernet/settings", \%netsettings);
60
61our %color = ();
62our %mainsettings = ();
63&General::readhash("${General::swroot}/main/settings", \%mainsettings);
64&General::readhash("/srv/web/ipfire/html/themes/".$mainsettings{'THEME'}."/include/colors.txt", \%color);
65
7edbe063
SS
66# File declarations.
67my $settingsfile = "${General::swroot}/guardian/settings";
68my $ignoredfile = "${General::swroot}/guardian/ignored";
69
70# Create empty settings and ignoredfile if they do not exist yet.
71unless (-e "$settingsfile") { system("touch $settingsfile"); }
72unless (-e "$ignoredfile") { system("touch $ignoredfile"); }
73
01dbccb1 74our %settings = ();
7edbe063 75our %ignored = ();
01dbccb1 76
28981fac
SS
77$settings{'ACTION'} = '';
78
01dbccb1 79$settings{'GUARDIAN_ENABLED'} = 'off';
723648ac
SS
80$settings{'GUARDIAN_MONITOR_SSH'} = 'on';
81$settings{'GUARDIAN_MONITOR_HTTPD'} = 'on';
82$settings{'GUARDIAN_MONITOR_OWNCLOUD'} = '';
52958991 83$settings{'GUARDIAN_LOG_FACILITY'} = 'syslog';
a35a0668
SS
84$settings{'GUARDIAN_LOGLEVEL'} = 'info';
85$settings{'GUARDIAN_BLOCKCOUNT'} = '3';
01dbccb1 86$settings{'GUARDIAN_BLOCKTIME'} = '86400';
2d17c6e6 87$settings{'GUARDIAN_FIREWALL_ACTION'} = 'DROP';
01dbccb1 88$settings{'GUARDIAN_LOGFILE'} = '/var/log/guardian/guardian.log';
01dbccb1 89
01dbccb1
SS
90my $errormessage = '';
91
92&Header::showhttpheaders();
93
94# Get GUI values.
95&Header::getcgihash(\%settings);
01dbccb1 96
f8c3bfe0
SS
97# Check if guardian is running and grab some stats.
98&daemonstats();
922ddf0e 99my $pid = $pid[0];
f8c3bfe0 100
01dbccb1
SS
101## Perform input checks and save settings.
102#
103if ($settings{'ACTION'} eq $Lang::tr{'save'}) {
01dbccb1 104 # Check for valid blocktime.
96655fa6 105 unless(($settings{'GUARDIAN_BLOCKTIME'} =~ /^\d+$/) && ($settings{'GUARDIAN_BLOCKTIME'} ne "0")) {
01dbccb1 106 $errormessage = "$Lang::tr{'guardian invalid blocktime'}";
a35a0668
SS
107 }
108
d68ead3d 109 # Check if the blockcount is valid.
96655fa6 110 unless(($settings{'GUARDIAN_BLOCKCOUNT'} =~ /^\d+$/) && ($settings{'GUARDIAN_BLOCKCOUNT'} ne "0")) {
a35a0668 111 $errormessage = "$Lang::tr{'guardian invalid blockcount'}";
01dbccb1
SS
112 }
113
114 # Check Logfile.
96655fa6 115 unless($settings{'GUARDIAN_LOGFILE'} =~ /^[a-zA-Z0-9\.\/]+$/) {
a35a0668 116 $errormessage = "$Lang::tr{'guardian invalid logfile'}";
01dbccb1
SS
117 }
118
01dbccb1 119 # Only continue if no error message has been set.
96655fa6 120 if($errormessage eq '') {
01dbccb1
SS
121 # Write configuration settings to file.
122 &General::writehash("${General::swroot}/guardian/settings", \%settings);
123
124 # Update configuration files.
125 &BuildConfiguration();
126 }
127
7edbe063 128## Add/edit an entry to the ignore file.
01dbccb1 129#
7edbe063 130} elsif (($settings{'ACTION'} eq $Lang::tr{'add'}) || ($settings{'ACTION'} eq $Lang::tr{'update'})) {
01dbccb1
SS
131
132 # Check if any input has been performed.
7edbe063 133 if ($settings{'IGNORE_ENTRY_ADDRESS'} ne '') {
01dbccb1
SS
134
135 # Check if the given input is no valid IP-address or IP-address with subnet, display an error message.
7edbe063 136 if ((!&General::validip($settings{'IGNORE_ENTRY_ADDRESS'})) && (!&General::validipandmask($settings{'IGNORE_ENTRY_ADDRESS'}))) {
01dbccb1
SS
137 $errormessage = "$Lang::tr{'guardian invalid address or subnet'}";
138 }
139 } else {
140 $errormessage = "$Lang::tr{'guardian empty input'}";
141 }
142
143 # Go further if there was no error.
144 if ($errormessage eq '') {
7edbe063
SS
145 my %ignored = ();
146 my $id;
147 my $status;
148
149 # Assign hash values.
150 my $new_entry_address = $settings{'IGNORE_ENTRY_ADDRESS'};
151 my $new_entry_remark = $settings{'IGNORE_ENTRY_REMARK'};
152
153 # Read-in ignoredfile.
154 &General::readhasharray($ignoredfile, \%ignored);
155
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'};
160
161 # Undef the given ID.
162 undef($settings{'ID'});
163
164 # Grab the configured status of the corresponding entry.
165 $status = $ignored{$id}[2];
166 } else {
167 # Each newly added entry automatically should be enabled.
168 $status = "enabled";
01dbccb1 169
7edbe063
SS
170 # Generate the ID for the new entry.
171 #
d68ead3d 172 # Sort the keys by their ID and store them in an array.
7edbe063 173 my @keys = sort { $a <=> $b } keys %ignored;
01dbccb1 174
7edbe063
SS
175 # Reverse the key array.
176 my @reversed = reverse(@keys);
177
178 # Obtain the last used id.
179 my $last_id = @reversed[0];
180
181 # Increase the last id by one and use it as id for the new entry.
182 $id = ++$last_id;
183 }
184
185 # Add/Modify the entry to/in the ignored hash.
186 $ignored{$id} = ["$new_entry_address", "$new_entry_remark", "$status"];
187
188 # Write the changed ignored hash to the ignored file.
189 &General::writehasharray($ignoredfile, \%ignored);
190
191 # Regenerate the ignore file.
97849142 192 &GenerateIgnoreFile();
01dbccb1
SS
193 }
194
f8c3bfe0
SS
195 # Check if guardian is running.
196 if ($pid > 0) {
af6856af 197 # Send reload command through socket connection.
1cc65323 198 &Guardian::Socket::Client("reload-ignore-list");
f8c3bfe0
SS
199 }
200
7edbe063 201## Toggle Enabled/Disabled for an existing entry on the ignore list.
01dbccb1 202#
7edbe063
SS
203
204} elsif ($settings{'ACTION'} eq $Lang::tr{'toggle enable disable'}) {
205 my %ignored = ();
206
207 # Only go further, if an ID has been passed.
208 if ($settings{'ID'}) {
209 # Assign the given ID.
210 my $id = $settings{'ID'};
211
212 # Undef the given ID.
213 undef($settings{'ID'});
214
215 # Read-in ignoredfile.
216 &General::readhasharray($ignoredfile, \%ignored);
217
218 # Grab the configured status of the corresponding entry.
219 my $status = $ignored{$id}[2];
220
221 # Switch the status.
222 if ($status eq "disabled") {
223 $status = "enabled";
224 } else {
225 $status = "disabled";
226 }
227
228 # Modify the status of the existing entry.
229 $ignored{$id} = ["$ignored{$id}[0]", "$ignored{$id}[1]", "$status"];
230
231 # Write the changed ignored hash to the ignored file.
232 &General::writehasharray($ignoredfile, \%ignored);
233
234 # Regenerate the ignore file.
97849142 235 &GenerateIgnoreFile();
7edbe063
SS
236
237 # Check if guardian is running.
238 if ($pid > 0) {
239 # Send reload command through socket connection.
1cc65323 240 &Guardian::Socket::Client("reload-ignore-list");
01dbccb1
SS
241 }
242 }
7edbe063
SS
243
244## Remove entry from ignore list.
245#
246} elsif ($settings{'ACTION'} eq $Lang::tr{'remove'}) {
247 my %ignored = ();
248
249 # Read-in ignoredfile.
250 &General::readhasharray($ignoredfile, \%ignored);
251
252 # Drop entry from the hash.
253 delete($ignored{$settings{'ID'}});
254
255 # Undef the given ID.
256 undef($settings{'ID'});
257
258 # Write the changed ignored hash to the ignored file.
259 &General::writehasharray($ignoredfile, \%ignored);
260
261 # Regenerate the ignore file.
97849142 262 &GenerateIgnoreFile();
01dbccb1 263
f8c3bfe0
SS
264 # Check if guardian is running.
265 if ($pid > 0) {
af6856af 266 # Send reload command through socket connection.
1cc65323 267 &Guardian::Socket::Client("reload-ignore-list");
f8c3bfe0
SS
268 }
269
01dbccb1
SS
270## Block a user given address or subnet.
271#
272} elsif ($settings{'ACTION'} eq $Lang::tr{'block'}) {
273
c973d6da
SS
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'};
01dbccb1 280
c232e348
SS
281 # File declarations.
282 my $gatewayfile = "${General::swroot}/red/remote-ipaddress";
c232e348 283
c973d6da 284 # Get gateway address.
9f9b2b8e 285 my $gateway = &General::grab_address_from_file($gatewayfile);
01dbccb1 286
c973d6da
SS
287 # Check if any input has been performed.
288 if ($input eq '') {
289 $errormessage = "$Lang::tr{'guardian empty input'}";
290 }
291
292 # Check if the given input is localhost (127.0.0.1).
293 elsif ($input eq "127.0.0.1") {
294 $errormessage = "$Lang::tr{'guardian blocking of this address is not allowed'}";
295 }
296
297 # Check if the given input is anywhere (0.0.0.0).
298 elsif ($input eq "0.0.0.0") {
299 $errormessage = "$Lang::tr{'guardian blocking of this address is not allowed'}";
300 }
301
302 # Check if the given input is one of the interface addresses or our gateway.
9f9b2b8e 303 elsif ($input eq "$green" || $input eq "$blue" || $input eq "$orange" || $input eq "$red" || $input eq "$gateway") {
c973d6da
SS
304 $errormessage = "$Lang::tr{'guardian blocking of this address is not allowed'}";
305 }
306
307 # Check if the given input is a valid IP address.
308 elsif (!&General::validip($input)) {
309 $errormessage = "$Lang::tr{'guardian invalid address or subnet'}";
310 }
01dbccb1
SS
311
312 # Go further if there was no error.
313 if ($errormessage eq '') {
314 my $block = $settings{'ADDRESS_BLOCK'};
315
af6856af
SS
316 # Send command to block the specified address through socket connection.
317 &Guardian::Socket::Client("block $block");
01dbccb1
SS
318 }
319
320## Unblock address or subnet.
321#
322} elsif ($settings{'ACTION'} eq $Lang::tr{'unblock'}) {
323
324 # Check if no empty input has been performed.
325 if ($settings{'ADDRESS_UNBLOCK'} ne '') {
326
327 # Check if the given input is no valid IP-address or IP-address with subnet, display an error message.
328 if ((!&General::validip($settings{'ADDRESS_UNBLOCK'})) && (!&General::validipandmask($settings{'ADDRESS_UNBLOCK'}))) {
329 $errormessage = "$Lang::tr{'guardian invalid address or subnet'}";
330 }
331
332 } else {
333 $errormessage = "$Lang::tr{'guardian empty input'}";
334 }
335
336 # Go further if there was no error.
337 if ($errormessage eq '') {
338 my $unblock = $settings{'ADDRESS_UNBLOCK'};
339
af6856af
SS
340 # Send command to unblock the given address through socket connection.
341 &Guardian::Socket::Client("unblock $unblock");
01dbccb1
SS
342 }
343
344## Unblock all.
345#
346} elsif ($settings{'ACTION'} eq $Lang::tr{'unblock all'}) {
347
af6856af
SS
348 # Send flush command through socket connection.
349 &Guardian::Socket::Client("flush");
01dbccb1
SS
350}
351
7edbe063 352# Load settings from files.
36dbcf2e 353&General::readhash("${General::swroot}/guardian/settings", \%settings);
7edbe063 354&General::readhasharray("${General::swroot}/guardian/ignored", \%ignored);
01dbccb1
SS
355
356# Call functions to generate whole page.
357&showMainBox();
358&showIgnoreBox();
359
360# Display area only if guardian is running.
f8c3bfe0 361if ( ($memory != 0) && ($pid > 0) ) {
01dbccb1
SS
362 &showBlockedBox();
363}
364
365# Function to display the status of guardian and allow base configuration.
366sub showMainBox() {
367 my %checked = ();
7899718f 368 my %selected = ();
01dbccb1
SS
369
370 $checked{'GUARDIAN_ENABLED'}{'on'} = '';
371 $checked{'GUARDIAN_ENABLED'}{'off'} = '';
372 $checked{'GUARDIAN_ENABLED'}{$settings{'GUARDIAN_ENABLED'}} = 'checked';
723648ac
SS
373 $checked{'GUARDIAN_MONITOR_SSH'}{'off'} = '';
374 $checked{'GUARDIAN_MONITOR_SSH'}{'on'} = '';
375 $checked{'GUARDIAN_MONITOR_SSH'}{$settings{'GUARDIAN_MONITOR_SSH'}} = "checked='checked'";
376 $checked{'GUARDIAN_MONITOR_HTTPD'}{'off'} = '';
377 $checked{'GUARDIAN_MONITOR_HTTPD'}{'on'} = '';
378 $checked{'GUARDIAN_MONITOR_HTTPD'}{$settings{'GUARDIAN_MONITOR_HTTPD'}} = "checked='checked'";
379 $checked{'GUARDIAN_MONITOR_OWNCLOUD'}{'off'} = '';
380 $checked{'GUARDIAN_MONITOR_OWNCLOUD'}{'on'} = '';
381 $checked{'GUARDIAN_MONITOR_OWNCLOUD'}{$settings{'GUARDIAN_MONITOR_OWNCLOUD'}} = "checked='checked'";
01dbccb1 382
52958991 383 $selected{'GUARDIAN_LOG_FACILITY'}{$settings{'GUARDIAN_LOG_FACILITY'}} = 'selected';
4a7fc9f6 384 $selected{'GUARDIAN_LOGLEVEL'}{$settings{'GUARDIAN_LOGLEVEL'}} = 'selected';
2d17c6e6 385 $selected{'GUARDIAN_FIREWALL_ACTION'}{$settings{'GUARDIAN_FIREWALL_ACTION'}} = 'selected';
7899718f 386
01dbccb1
SS
387 &Header::openpage($Lang::tr{'guardian configuration'}, 1, '');
388 &Header::openbigbox('100%', 'left', '', $errormessage);
389
390 # Print errormessage if there is one.
391 if ($errormessage) {
392 &Header::openbox('100%', 'left', $Lang::tr{'error messages'});
393 print "<font class='base'>$errormessage&nbsp;</font>\n";
394 &Header::closebox();
395 }
396
2daa1f5b
SS
397 ### Java Script ###
398 print<<END;
399 <script>
f617f21c 400 var update_options = function() {
2daa1f5b
SS
401
402 var logfacility = \$("#GUARDIAN_LOG_FACILITY").val();
f617f21c 403 var loglevel = \$("#GUARDIAN_LOGLEVEL").val();
2daa1f5b
SS
404
405 if (logfacility === undefined)
406 return;
407
f617f21c
SS
408 if (loglevel === undefined)
409 return;
410
411 // Show / Hide input for specifying the path to the logfile.
2daa1f5b
SS
412 if (logfacility === "file") {
413 \$(".GUARDIAN_LOGFILE").show();
414 } else {
415 \$(".GUARDIAN_LOGFILE").hide();
416 }
f617f21c
SS
417
418 // Show / Hide loglevel debug if the facility is set to syslog.
419 if (logfacility === "syslog") {
420 \$("#loglevel_debug").hide();
421 } else {
422 \$("#loglevel_debug").show();
423 }
424
425 // Show / Hide logfacility syslog if the loglevel is set to debug.
426 if (loglevel === "debug") {
427 \$("#logfacility_syslog").hide();
428 } else {
429 \$("#logfacility_syslog").show();
430 }
2daa1f5b
SS
431 };
432
433 \$(document).ready(function() {
f617f21c
SS
434 \$("#GUARDIAN_LOG_FACILITY").change(update_options);
435 \$("#GUARDIAN_LOGLEVEL").change(update_options);
436 update_options();
2daa1f5b
SS
437 });
438 </script>
439END
440
441
01dbccb1
SS
442
443 # Draw current guardian state.
444 &Header::openbox('100%', 'center', $Lang::tr{'guardian'});
445
f8c3bfe0 446 # Get current status of guardian.
01dbccb1 447 &daemonstats();
922ddf0e 448 $pid = $pid[0];
01dbccb1
SS
449
450 # Display some useful information related to guardian, if daemon is running.
f8c3bfe0 451 if ( ($memory != 0) && ($pid > 0) ){
01dbccb1
SS
452 print <<END;
453 <table width='95%' cellspacing='0' class='tbl'>
454 <tr>
455 <th bgcolor='$color{'color20'}' colspan='3' align='left'><strong>$Lang::tr{'guardian service'}</strong></th>
456 </tr>
457 <tr>
458 <td class='base'>$Lang::tr{'guardian daemon'}</td>
459 <td align='center' colspan='2' width='75%' bgcolor='${Header::colourgreen}'><font color='white'><strong>$Lang::tr{'running'}</strong></font></td>
460 </tr>
461 <tr>
462 <td class='base'></td>
463 <td bgcolor='$color{'color20'}' align='center'><strong>PID</strong></td>
464 <td bgcolor='$color{'color20'}' align='center'><strong>$Lang::tr{'memory'}</strong></td>
465 </tr>
466 <tr>
467 <td class='base'></td>
922ddf0e 468 <td bgcolor='$color{'color22'}' align='center'>$pid</td>
01dbccb1
SS
469 <td bgcolor='$color{'color22'}' align='center'>$memory KB</td>
470 </tr>
471 </table>
472END
473 } else {
474 # Otherwise display a hint that the service is not launched.
475 print <<END;
476 <table width='95%' cellspacing='0' class='tbl'>
477 <tr>
478 <th bgcolor='$color{'color20'}' colspan='3' align='left'><strong>$Lang::tr{'guardian service'}</strong></th>
479 </tr>
480 <tr>
481 <td class='base'>$Lang::tr{'guardian daemon'}</td>
482 <td align='center' width='75%' bgcolor='${Header::colourred}'><font color='white'><strong>$Lang::tr{'stopped'}</strong></font></td>
483 </tr>
484 </table>
485END
486 }
487
488 &Header::closebox();
489
490 # Draw elements for guardian configuration.
491 &Header::openbox('100%', 'center', $Lang::tr{'guardian configuration'});
492
493 print <<END;
494 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
495
496 <table width='95%'>
497 <tr>
d2fea55e 498 <td colspan='2' class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'guardian common settings'}</b></td>
01dbccb1 499 </tr>
c5f633c9 500
01dbccb1 501 <tr>
c5f633c9 502 <td width='25%' class='base'>$Lang::tr{'guardian enabled'}:</td>
d2fea55e 503 <td><input type='checkbox' name='GUARDIAN_ENABLED' $checked{'GUARDIAN_ENABLED'}{'on'} /></td>
01dbccb1 504 </tr>
c5f633c9 505
26fcd31e
SS
506 <tr>
507 <td colspan='2'><br></td>
508 </tr>
c5f633c9 509
26fcd31e 510 <tr>
c5f633c9 511 <td width='25%' class='base'>$Lang::tr{'guardian block ssh brute-force'}</td>
723648ac
SS
512 <td align='left'>on <input type='radio' name='GUARDIAN_MONITOR_SSH' value='on' $checked{'GUARDIAN_MONITOR_SSH'}{'on'} /> /
513 <input type='radio' name='GUARDIAN_MONITOR_SSH' value='off' $checked{'GUARDIAN_MONITOR_SSH'}{'off'} /> off</td>
26fcd31e 514 </tr>
c5f633c9 515
26fcd31e 516 <tr>
c5f633c9 517 <td width='25%' class='base'>$Lang::tr{'guardian block httpd brute-force'}</td>
723648ac
SS
518 <td align='left'>on <input type='radio' name='GUARDIAN_MONITOR_HTTPD' value='on' $checked{'GUARDIAN_MONITOR_HTTPD'}{'on'} /> /
519 <input type='radio' name='GUARDIAN_MONITOR_HTTPD' value='off' $checked{'GUARDIAN_MONITOR_HTTPD'}{'off'} /> off</td>
26fcd31e 520 </tr>
c5f633c9 521
52958991
SS
522 <tr>
523 <td colspan='2'><br></td>
524 </tr>
c5f633c9 525
52958991
SS
526 <tr>
527 <td align='left' width='20%'>$Lang::tr{'guardian logfacility'}:</td>
c5f633c9
MF
528 <td width='25%'><select id='GUARDIAN_LOG_FACILITY' name='GUARDIAN_LOG_FACILITY'>
529 <option id='logfacility_syslog' value='syslog' $selected{'GUARDIAN_LOG_FACILITY'}{'syslog'}>$Lang::tr{'guardian logtarget_syslog'}</option>
530 <option id='logfacility_file' value='file' $selected{'GUARDIAN_LOG_FACILITY'}{'file'}>$Lang::tr{'guardian logtarget_file'}</option>
531 <option id='logfacility_console' value='console' $selected{'GUARDIAN_LOG_FACILITY'}{'console'}>$Lang::tr{'guardian logtarget_console'}</option>
532 </select></td>
533
534 <td align='left' width='20%'>$Lang::tr{'guardian loglevel'}:</td>
535 <td width='25%'><select id='GUARDIAN_LOGLEVEL' name='GUARDIAN_LOGLEVEL'>
536 <option id='loglevel_off' value='off' $selected{'GUARDIAN_LOGLEVEL'}{'off'}>$Lang::tr{'guardian loglevel_off'}</option>
537 <option id='loglevel_info' value='info' $selected{'GUARDIAN_LOGLEVEL'}{'info'}>$Lang::tr{'guardian loglevel_info'}</option>
538 <option id='loglevel_debug' value='debug' $selected{'GUARDIAN_LOGLEVEL'}{'debug'}>$Lang::tr{'guardian loglevel_debug'}</option>
52958991
SS
539 </select></td>
540 </tr>
c5f633c9
MF
541
542 <tr class="GUARDIAN_LOGFILE">
26fcd31e
SS
543 <td colspan='2'><br></td>
544 </tr>
c5f633c9
MF
545
546 <tr class="GUARDIAN_LOGFILE">
547 <td width='25%' class='base'>$Lang::tr{'guardian logfile'}:</td>
548 <td><input type='text' name='GUARDIAN_LOGFILE' value='$settings{'GUARDIAN_LOGFILE'}' size='30' /></td>
7899718f 549 </tr>
c5f633c9 550
d2b54a31 551 <tr>
a35a0668
SS
552 <td colspan='2'><br></td>
553 </tr>
c5f633c9 554
d2b54a31
SS
555 <tr>
556 <td width='25%' class='base'>$Lang::tr{'guardian firewallaction'}:</td>
557 <td><select name='GUARDIAN_FIREWALL_ACTION'>
558 <option value='DROP' $selected{'GUARDIAN_FIREWALL_ACTION'}{'DROP'}>Drop</option>
559 <option value='REJECT' $selected{'GUARDIAN_FIREWALL_ACTION'}{'REJECT'}>Reject</option>
4a7fc9f6 560 </select></td>
c5f633c9
MF
561
562 <td width='25%' class='base'>$Lang::tr{'guardian blockcount'}:</td>
563 <td><input type='text' name='GUARDIAN_BLOCKCOUNT' value='$settings{'GUARDIAN_BLOCKCOUNT'}' size='5' /></td>
4a7fc9f6 564 </tr>
c5f633c9 565
4a7fc9f6
SS
566 <tr>
567 <td colspan='2'><br></td>
568 </tr>
c5f633c9 569
2d17c6e6 570 <tr>
c5f633c9 571 <td width='25%' class='base'>$Lang::tr{'guardian blocktime'}:</td>
d2fea55e 572 <td><input type='text' name='GUARDIAN_BLOCKTIME' value='$settings{'GUARDIAN_BLOCKTIME'}' size='10' /></td>
01dbccb1 573 </tr>
c5f633c9 574
01dbccb1
SS
575 </table>
576END
577
578 print <<END;
579 <hr>
580
581 <table width='95%'>
582 <tr>
583 <td>&nbsp;</td>
584 <td align='center'><input type='submit' name='ACTION' value='$Lang::tr{'save'}' /></td>
585 <td>&nbsp;</td>
586 </tr>
587 </table>
588 </form>
589END
590
591 &Header::closebox();
592}
593
594# Function to show elements of the guardian ignorefile and allow to add or remove single members of it.
595sub showIgnoreBox() {
596 &Header::openbox('100%', 'center', $Lang::tr{'guardian ignored hosts'});
597
598 print <<END;
7edbe063 599 <table width='80%'>
01dbccb1 600 <tr>
7edbe063
SS
601 <td class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'ip address'}</b></td>
602 <td class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'remark'}</b></td>
603 <td class='base' colspan='3' bgcolor='$color{'color20'}'></td>
01dbccb1
SS
604 </tr>
605END
d68ead3d 606 # Check if some hosts have been added to be ignored.
7edbe063 607 if (keys (%ignored)) {
01dbccb1
SS
608 my $col = "";
609
d68ead3d 610 # Loop through all entries of the hash.
7edbe063
SS
611 while( (my $key) = each %ignored) {
612 # Assign data array positions to some nice variable names.
613 my $address = $ignored{$key}[0];
614 my $remark = $ignored{$key}[1];
615 my $status = $ignored{$key}[2];
616
617 # Check if the key (id) number is even or not.
618 if ($settings{'ID'} eq $key) {
619 $col="bgcolor='${Header::colouryellow}'";
620 } elsif ($key % 2) {
01dbccb1
SS
621 $col="bgcolor='$color{'color22'}'";
622 } else {
623 $col="bgcolor='$color{'color20'}'";
624 }
625
7edbe063
SS
626 # Choose icon for the checkbox.
627 my $gif;
628 my $gdesc;
629
630 # Check if the status is enabled and select the correct image and description.
631 if ($status eq 'enabled' ) {
632 $gif = 'on.gif';
633 $gdesc = $Lang::tr{'click to disable'};
634 } else {
635 $gif = 'off.gif';
636 $gdesc = $Lang::tr{'click to enable'};
637 }
638
01dbccb1
SS
639 print <<END;
640 <tr>
7edbe063
SS
641 <td width='20%' class='base' $col>$address</td>
642 <td width='65%' class='base' $col>$remark</td>
643
644 <td align='center' $col>
645 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
646 <input type='hidden' name='ACTION' value='$Lang::tr{'toggle enable disable'}' />
647 <input type='image' name='$Lang::tr{'toggle enable disable'}' src='/images/$gif' alt='$gdesc' title='$gdesc' />
648 <input type='hidden' name='ID' value='$key' />
649 </form>
650 </td>
651
652 <td align='center' $col>
653 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
654 <input type='hidden' name='ACTION' value='$Lang::tr{'edit'}' />
655 <input type='image' name='$Lang::tr{'edit'}' src='/images/edit.gif' alt='$Lang::tr{'edit'}' title='$Lang::tr{'edit'}' />
656 <input type='hidden' name='ID' value='$key' />
657 </form>
658 </td>
659
660 <td align='center' $col>
661 <form method='post' name='$key' action='$ENV{'SCRIPT_NAME'}'>
01dbccb1 662 <input type='image' name='$Lang::tr{'remove'}' src='/images/delete.gif' title='$Lang::tr{'remove'}' alt='$Lang::tr{'remove'}'>
7edbe063 663 <input type='hidden' name='ID' value='$key'>
01dbccb1
SS
664 <input type='hidden' name='ACTION' value='$Lang::tr{'remove'}'>
665 </form>
666 </td>
667 </tr>
668END
669 }
01dbccb1 670 } else {
7edbe063 671 # Print notice that currently no hosts are ignored.
01dbccb1
SS
672 print "<tr>\n";
673 print "<td class='base' colspan='2'>$Lang::tr{'guardian no entries'}</td>\n";
674 print "</tr>\n";
675 }
676
677 print "</table>\n";
678
7edbe063 679 # Section to add new elements or edit existing ones.
01dbccb1 680 print <<END;
1d5702a7 681 <br>
7edbe063
SS
682 <hr>
683 <br>
684
1d5702a7 685 <div align='center'>
7edbe063
SS
686 <table width='100%'>
687END
688
689 # Assign correct headline and button text.
690 my $buttontext;
691 my $entry_address;
692 my $entry_remark;
693
694 # Check if an ID (key) has been given, in this case an existing entry should be edited.
695 if ($settings{'ID'} ne '') {
696 $buttontext = $Lang::tr{'update'};
697 print "<tr><td class='boldbase' colspan='3'><b>$Lang::tr{'update'}</b></td></tr>\n";
698
699 # Grab address and remark for the given key.
700 $entry_address = $ignored{$settings{'ID'}}[0];
701 $entry_remark = $ignored{$settings{'ID'}}[1];
702 } else {
703 $buttontext = $Lang::tr{'add'};
704 print "<tr><td class='boldbase' colspan='3'><b>$Lang::tr{'dnsforward add a new entry'}</b></td></tr>\n";
705 }
706
707 print <<END;
01dbccb1 708 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
7edbe063 709 <input type='hidden' name='ID' value='$settings{'ID'}'>
01dbccb1 710 <tr>
7edbe063
SS
711 <td width='30%'>$Lang::tr{'ip address'}: </td>
712 <td width='50%'><input type='text' name='IGNORE_ENTRY_ADDRESS' value='$entry_address' size='24' /></td>
713
714 <td width='30%'>$Lang::tr{'remark'}: </td>
715 <td wicth='50%'><input type='text' name=IGNORE_ENTRY_REMARK value='$entry_remark' size='24' /></td>
716 <td align='center' width='20%'><input type='submit' name='ACTION' value='$buttontext' /></td>
01dbccb1
SS
717 </tr>
718 </form>
719 </table>
720 </div>
721END
1d5702a7
SS
722
723 &Header::closebox();
01dbccb1
SS
724}
725
d68ead3d 726# Function to list currently blocked addresses from guardian and unblock them or add custom entries to block.
01dbccb1
SS
727sub showBlockedBox() {
728 &Header::openbox('100%', 'center', $Lang::tr{'guardian blocked hosts'});
729
01dbccb1
SS
730 print <<END;
731 <table width='60%'>
732 <tr>
733 <td colspan='2' class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'guardian blocked hosts'}</b></td>
734 </tr>
735END
736
d68ead3d 737 # Launch function to get the currently blocked hosts.
5f462919
SS
738 my @blocked_hosts = &GetBlockedHosts();
739
01dbccb1 740 my $id = 0;
01dbccb1 741 my $col = "";
01dbccb1 742
5f462919
SS
743 # Loop through our blocked hosts array.
744 foreach my $blocked_host (@blocked_hosts) {
01dbccb1
SS
745
746 # Increase id number for each element in the ignore file.
747 $id++;
748
749 # Check if the id number is even or not.
750 if ($id % 2) {
751 $col="bgcolor='$color{'color22'}'";
752 } else {
753 $col="bgcolor='$color{'color20'}'";
754 }
755
756 print <<END;
757 <tr>
8b8413e5 758 <td width='80%' class='base' $col><a href='/cgi-bin/ipinfo.cgi?ip=$blocked_host'>$blocked_host</a></td>
01dbccb1
SS
759 <td width='20%' align='center' $col>
760 <form method='post' name='frmb$id' action='$ENV{'SCRIPT_NAME'}'>
761 <input type='image' name='$Lang::tr{'unblock'}' src='/images/delete.gif' title='$Lang::tr{'unblock'}' alt='$Lang::tr{'unblock'}'>
762 <input type='hidden' name='ADDRESS_UNBLOCK' value='$blocked_host'>
763 <input type='hidden' name='ACTION' value='$Lang::tr{'unblock'}'>
764 </form>
765 </td>
766 </tr>
767END
768 }
769
d68ead3d 770 # If the loop only has been run once the id still is "0", which means there are no
01dbccb1
SS
771 # additional entries (blocked hosts) in the iptables chain.
772 if ($id == 0) {
773
774 # Print notice that currently no hosts are blocked.
775 print "<tr>\n";
776 print "<td class='base' colspan='2'>$Lang::tr{'guardian no entries'}</td>\n";
777 print "</tr>\n";
778 }
779
780 print "</table>\n";
781
01dbccb1
SS
782 # Section for a manual block of an IP-address.
783 print <<END;
1d5702a7
SS
784 <br>
785 <div align='center'>
786 <table width='60%' border='0'>
01dbccb1
SS
787 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
788 <tr>
789 <td width='30%'>$Lang::tr{'guardian block a host'}: </td>
790 <td width='40%'><input type='text' name='ADDRESS_BLOCK' value='' size='24' /></td>
791 <td align='center' width='15%'><input type='submit' name='ACTION' value='$Lang::tr{'block'}'></td>
792 <td align='center' width='15%'><input type='submit' name='ACTION' value='$Lang::tr{'unblock all'}'></td>
793 </tr>
794 </form>
795 </table>
796 </div>
797END
1d5702a7
SS
798
799 &Header::closebox();
01dbccb1
SS
800}
801
802&Header::closebigbox();
803&Header::closepage();
804
805# Function to check if guardian has been started.
806# Grab process id and consumed memory if the daemon is running.
807sub daemonstats() {
808 $memory = 0;
809 # for pid and memory
810 open(FILE, '/usr/local/bin/addonctrl guardian status | ');
811 @guardian = <FILE>;
812 close(FILE);
813 $string = join("", @guardian);
814 $string =~ s/[a-z_]//gi;
815 $string =~ s/\[[0-1]\;[0-9]+//gi;
816 $string =~ s/[\(\)\.]//gi;
817 $string =~ s/ //gi;
818 $string =~ s/\e//gi;
819 @pid = split(/\s/,$string);
820 if (open(FILE, "/proc/$pid[0]/statm")){
821 my $temp = <FILE>;
822 @memory = split(/ /,$temp);
823 close(FILE);
824 }
825 $memory+=$memory[0];
826}
827
5f462919 828sub GetBlockedHosts() {
5f462919
SS
829 # Create new, empty array.
830 my @hosts;
831
d68ead3d 832 # Launch helper to get chains from iptables.
891ba055
SS
833 system('/usr/local/bin/getipstat');
834
835 # Open temporary file which contains the chains and rules.
d68ead3d 836 open (FILE, '/var/tmp/iptables.txt');
891ba055
SS
837
838 # Loop through the entire file.
839 while (<FILE>) {
840 my $line = $_;
841
842 # Search for the guardian chain and extract
843 # the lines between it and the next empty line
844 # which is placed before the next firewall
845 # chain starts.
846 if ($line =~ /^Chain GUARDIAN/ .. /^\s*$/) {
847 # Skip descriptive lines.
848 next if ($line =~ /^Chain/);
849 next if ($line =~ /^ pkts/);
850
d68ead3d 851 # Generate array, based on the line content (separator is a single or multiple space)
891ba055
SS
852 my @comps = split(/\s{1,}/, $line);
853 my ($lead, $pkts, $bytes, $target, $prot, $opt, $in, $out, $source, $destination) = @comps;
854
855 # Assign different variable names.
856 my $blocked_host = $source;
857
858 # Add host to our hosts array.
859 if ($blocked_host) {
860 push(@hosts, $blocked_host);
861 }
862 }
863 }
5f462919 864
891ba055
SS
865 # Close filehandle.
866 close(FILE);
5f462919 867
891ba055 868 # Remove recently created temporary files of the "getipstat" binary.
d68ead3d
MF
869 system("rm -f /var/tmp/iptables.txt");
870 system("rm -f /var/tmp/iptablesmangle.txt");
871 system("rm -f /var/tmp/iptablesnat.txt");
5f462919
SS
872
873 # Convert entries, sort them, write back and store the sorted entries into new array.
874 my @sorted = map { $_->[0] }
875 sort { $a->[1] <=> $b->[1] }
876 map { [$_, int sprintf("%03.f%03.f%03.f%03.f", split(/\./, $_))] }
877 @hosts;
878
879 # Return our sorted list.
880 return @sorted
881}
882
01dbccb1
SS
883sub BuildConfiguration() {
884 my %settings = ();
885 &General::readhash("${General::swroot}/guardian/settings", \%settings);
886
887 my $configfile = "${General::swroot}/guardian/guardian.conf";
888
d68ead3d 889 # Create the configfile if none exists yet.
c880c2cb
SS
890 unless (-e "$configfile") { system("touch $configfile"); }
891
7f728591 892 # Open configfile for writing.
01dbccb1
SS
893 open(FILE, ">$configfile");
894
52958991
SS
895 # Config file header.
896 print FILE "# Autogenerated configuration file.\n";
897 print FILE "# All user modifications will be overwritten.\n\n";
28981fac 898
52958991
SS
899 # Settings for the logging mechanism.
900 print FILE "# Log settings.\n";
901 print FILE "LogFacility = $settings{'GUARDIAN_LOG_FACILITY'}\n";
902
903 if ($settings{'GUARDIAN_LOG_FACILITY'} eq "file") {
904 print FILE "LogFile = $settings{'GUARDIAN_LOGFILE'}\n";
28981fac
SS
905 }
906
52958991
SS
907 print FILE "LogLevel = $settings{'GUARDIAN_LOGLEVEL'}\n\n";
908
909 # IPFire related static settings.
910 print FILE "# IPFire related settings.\n";
911 print FILE "FirewallEngine = IPtables\n";
912 print FILE "SocketOwner = nobody:nobody\n";
913 print FILE "IgnoreFile = $ignorefile\n\n";
914
915 # Configured block values.
2d17c6e6 916 print FILE "# Configured block settings.\n";
52958991 917 print FILE "BlockCount = $settings{'GUARDIAN_BLOCKCOUNT'}\n";
2d17c6e6
SS
918 print FILE "BlockTime = $settings{'GUARDIAN_BLOCKTIME'}\n";
919 print FILE "FirewallAction = $settings{'GUARDIAN_FIREWALL_ACTION'}\n\n";
52958991
SS
920
921 # Enabled modules.
922 # Loop through whole settings hash.
923 print FILE "# Enabled modules.\n";
924 foreach my $option (keys %settings) {
925 # Search for enabled modules.
926 if ($option =~ /GUARDIAN_MONITOR_(.*)/) {
927 # Skip if module is not enabled.
928 next unless($settings{$option} eq "on");
929
930 # Skip module if no file location is available.
931 next unless(exists($module_file_locations{$1}));
932
933 # Add enabled module and defined path to the config file.
934 print FILE "Monitor_$1 = $module_file_locations{$1}\n";
935 }
936 }
937
938 # Module settings.
939 print FILE "\n# Module settings.\n";
01dbccb1
SS
940 close(FILE);
941
efd9c5ff
SS
942 # Generate ignore file.
943 &GenerateIgnoreFile();
944
f8c3bfe0
SS
945 # Check if guardian should be started or stopped.
946 if($settings{'GUARDIAN_ENABLED'} eq 'on') {
947 if($pid > 0) {
af6856af
SS
948 # Send reload command through socket connection.
949 &Guardian::Socket::Client("reload");
f8c3bfe0
SS
950 } else {
951 # Launch guardian.
af6856af 952 system("/usr/local/bin/addonctrl guardian start &>/dev/null");
f8c3bfe0 953 }
01dbccb1 954 } else {
f8c3bfe0 955 # Stop the daemon.
af6856af 956 system("/usr/local/bin/addonctrl guardian stop &>/dev/null");
01dbccb1
SS
957 }
958}
97849142
SS
959
960sub GenerateIgnoreFile() {
961 my %ignored = ();
962
963 # Read-in ignoredfile.
964 &General::readhasharray($ignoredfile, \%ignored);
965
c880c2cb
SS
966 # Create the guardian.ignore file if not exist yet.
967 unless (-e "$ignorefile") { system("touch $ignorefile"); }
968
97849142
SS
969 # Open ignorefile for writing.
970 open(FILE, ">$ignorefile");
971
972 # Config file header.
973 print FILE "# Autogenerated configuration file.\n";
974 print FILE "# All user modifications will be overwritten.\n\n";
975
976 # Add IFPire interfaces and gateway to the ignore file.
977 #
978 # Assign some temporary variables for the IPFire interfaces.
979 my $green = $netsettings{'GREEN_ADDRESS'};
980 my $blue = $netsettings{'BLUE_ADDRESS'};
981 my $orange = $netsettings{'ORANGE_ADDRESS'};
97849142
SS
982
983 # File declarations.
1cc65323 984 my $public_address_file = "${General::swroot}/red/local-ipaddress";
97849142 985 my $gatewayfile = "${General::swroot}/red/remote-ipaddress";
97849142 986
97849142
SS
987 # Write the obtained addresses to the ignore file.
988 print FILE "# IPFire local interfaces.\n";
989 print FILE "$green\n";
990
991 # Check if a blue interface exists.
992 if ($blue) {
993 # Add blue address.
994 print FILE "$blue\n";
995 }
996
997 # Check if an orange interface exists.
998 if ($orange) {
999 # Add orange address.
1000 print FILE "$orange\n";
1001 }
1002
1003 print FILE "\n# IPFire red interface, gateway and used DNS-servers.\n";
1cc65323
SS
1004 print FILE "# Include the corresponding files to obtain the addresses.\n";
1005 print FILE "Include_File = $public_address_file\n";
1006 print FILE "Include_File = $gatewayfile\n";
97849142
SS
1007
1008 # Add all user defined hosts and networks to the ignore file.
1009 #
1010 # Check if the hash contains any elements.
1011 if (keys (%ignored)) {
1012 # Write headline.
1cc65323 1013 print FILE "\n# User defined hosts/networks.\n";
97849142
SS
1014
1015 # Loop through the entire hash and write the host/network
1016 # and remark to the ignore file.
1017 while ( (my $key) = each %ignored) {
1018 my $address = $ignored{$key}[0];
1019 my $remark = $ignored{$key}[1];
1020 my $status = $ignored{$key}[2];
1021
1022 # Check if the status of the entry is "enabled".
1023 if ($status eq "enabled") {
1024 # Check if the address/network is valid.
1025 if ((&General::validip($address)) || (&General::validipandmask($address))) {
1026 # Write the remark to the file.
1027 print FILE "# $remark\n";
1028
1029 # Write the address/network to the ignore file.
1030 print FILE "$address\n\n";
1031 }
1032 }
1033 }
1034 }
1035
1036 close(FILE);
1037}