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