]> git.ipfire.org Git - ipfire-2.x.git/blob - html/cgi-bin/guardian.cgi
guardian.cgi: Adjust code for generating the config 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
25 # enable only the following on debugging purpose
26 #use warnings;
27 #use CGI::Carp 'fatalsToBrowser';
28
29 require '/var/ipfire/general-functions.pl';
30 require "${General::swroot}/lang.pl";
31 require "${General::swroot}/header.pl";
32
33 #workaround to suppress a warning when a variable is used only once
34 my @dummy = (
35 ${Header::colourred},
36 ${Header::colourgreen}
37 );
38
39 undef (@dummy);
40
41 my $string=();
42 my $memory=();
43 my @memory=();
44 my @pid=();
45 my @guardian=();
46
47 # Path to the guardian.ignore file.
48 my $ignorefile ='/var/ipfire/guardian/guardian.ignore';
49
50 # Hash which contains the supported modules and the
51 # file locations on IPFire systems.
52 my %module_file_locations = (
53 "HTTPD" => "/var/log/httpd/error_log",
54 "OWNCLOUD" => "/var/owncloud/data/owncloud.log",
55 "SNORT" => "/var/log/snort.alert",
56 "SSH" => "/var/log/messages",
57 );
58
59 our %netsettings = ();
60 &General::readhash("${General::swroot}/ethernet/settings", \%netsettings);
61
62 our %color = ();
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);
66
67 # Pakfire meta file for owncloud.
68 # (File exists when the addon is installed.)
69 my $owncloud_meta = "/opt/pakfire/db/installed/meta-owncloud";
70
71 our %settings = ();
72
73 $settings{'ACTION'} = '';
74
75 $settings{'GUARDIAN_ENABLED'} = 'off';
76 $settings{'GUARDIAN_MONITOR_SNORT'} = 'on';
77 $settings{'GUARDIAN_MONITOR_SSH'} = 'on';
78 $settings{'GUARDIAN_MONITOR_HTTPD'} = 'on';
79 $settings{'GUARDIAN_MONITOR_OWNCLOUD'} = '';
80 $settings{'GUARDIAN_LOG_FACILITY'} = 'syslog';
81 $settings{'GUARDIAN_LOGLEVEL'} = 'info';
82 $settings{'GUARDIAN_BLOCKCOUNT'} = '3';
83 $settings{'GUARDIAN_BLOCKTIME'} = '86400';
84 $settings{'GUARDIAN_LOGFILE'} = '/var/log/guardian/guardian.log';
85 $settings{'GUARDIAN_SNORT_PRIORITY_LEVEL'} = '3';
86
87 # Default settings for owncloud if installed.
88 if ( -e "$owncloud_meta") {
89 $settings{'GUARDIAN_MONITOR_OWNCLOUD'} = 'off';
90 }
91
92 my $errormessage = '';
93
94 &Header::showhttpheaders();
95
96 # Get GUI values.
97 &Header::getcgihash(\%settings);
98
99 # Check if guardian is running and grab some stats.
100 &daemonstats();
101 my $pid = $pid[0];
102
103 ## Perform input checks and save settings.
104 #
105 if ($settings{'ACTION'} eq $Lang::tr{'save'}) {
106 # Check for valid blocktime.
107 unless(($settings{'GUARDIAN_BLOCKTIME'} =~ /^\d+$/) && ($settings{'GUARDIAN_BLOCKTIME'} ne "0")) {
108 $errormessage = "$Lang::tr{'guardian invalid blocktime'}";
109 }
110
111 # Check if the bloccount is valid.
112 unless(($settings{'GUARDIAN_BLOCKCOUNT'} =~ /^\d+$/) && ($settings{'GUARDIAN_BLOCKCOUNT'} ne "0")) {
113 $errormessage = "$Lang::tr{'guardian invalid blockcount'}";
114 }
115
116 # Check Logfile.
117 unless($settings{'GUARDIAN_LOGFILE'} =~ /^[a-zA-Z0-9\.\/]+$/) {
118 $errormessage = "$Lang::tr{'guardian invalid logfile'}";
119 }
120
121 # Only continue if no error message has been set.
122 if($errormessage eq '') {
123 # Write configuration settings to file.
124 &General::writehash("${General::swroot}/guardian/settings", \%settings);
125
126 # Update configuration files.
127 &BuildConfiguration();
128 }
129
130 ## Add a new entry to the ignore file.
131 #
132 } elsif ($settings{'ACTION'} eq $Lang::tr{'add'}) {
133
134 # Check if any input has been performed.
135 if ($settings{'NEW_IGNORE_ENTRY'} ne '') {
136
137 # Check if the given input is no valid IP-address or IP-address with subnet, display an error message.
138 if ((!&General::validip($settings{'NEW_IGNORE_ENTRY'})) && (!&General::validipandmask($settings{'NEW_IGNORE_ENTRY'}))) {
139 $errormessage = "$Lang::tr{'guardian invalid address or subnet'}";
140 }
141 } else {
142 $errormessage = "$Lang::tr{'guardian empty input'}";
143 }
144
145 # Go further if there was no error.
146 if ($errormessage eq '') {
147 my $new_entry = $settings{'NEW_IGNORE_ENTRY'};
148
149 # Open file for appending the new entry.
150 open (FILE, ">>$ignorefile") or die "Unable to open $ignorefile for writing";
151
152 # Write the new entry to the ignore file.
153 print FILE "$new_entry\n";
154 close(FILE);
155 }
156
157 # Check if guardian is running.
158 if ($pid > 0) {
159 # Call guardianctrl to perform a reload.
160 system("/usr/local/bin/guardianctrl reload &>/dev/null");
161 }
162
163 ## Remove entry from ignore list.
164 #
165 } elsif ($settings{'ACTION'} eq $Lang::tr{'remove'}) {
166 my $id = 0;
167
168 # Open ignorefile and read content.
169 open(FILE, "<$ignorefile") or die "Unable to open $ignorefile.";
170 my @current = <FILE>;
171 close(FILE);
172
173 # Re-open ignorefile for writing.
174 open(FILE, ">$ignorefile") or die "Unable to open $ignorefile for writing";
175 flock FILE, 2;
176
177 # Read line by line from ignorefile and write them back except the line with the given ID.
178 # So this line is missing in the new file and the entry has been deleted.
179 foreach my $line (@current) {
180 $id++;
181 unless ($settings{'ID'} eq $id) {
182 print FILE "$line";
183 }
184 }
185 close(FILE);
186
187 # Check if guardian is running.
188 if ($pid > 0) {
189 # Call guardianctrl to perform a reload.
190 system("/usr/local/bin/guardianctrl reload &>/dev/null");
191 }
192
193 ## Block a user given address or subnet.
194 #
195 } elsif ($settings{'ACTION'} eq $Lang::tr{'block'}) {
196
197 # Assign some temporary variables used for input validation.
198 my $input = $settings{'ADDRESS_BLOCK'};
199 my $green = $netsettings{'GREEN_ADDRESS'};
200 my $blue = $netsettings{'BLUE_ADDRESS'};
201 my $orange = $netsettings{'ORANGE_ADDRESS'};
202 my $red = $netsettings{'RED_ADDRESS'};
203
204 # Get gateway address.
205 my $gateway = &General::get_gateway();
206
207 # Check if any input has been performed.
208 if ($input eq '') {
209 $errormessage = "$Lang::tr{'guardian empty input'}";
210 }
211
212 # Check if the given input is localhost (127.0.0.1).
213 elsif ($input eq "127.0.0.1") {
214 $errormessage = "$Lang::tr{'guardian blocking of this address is not allowed'}";
215 }
216
217 # Check if the given input is anywhere (0.0.0.0).
218 elsif ($input eq "0.0.0.0") {
219 $errormessage = "$Lang::tr{'guardian blocking of this address is not allowed'}";
220 }
221
222 # Check if the given input is one of the interface addresses or our gateway.
223 elsif ($input eq "$green" || $input eq "$blue" || $input eq "$orange" || $input eq "$red" || $input eq "$gateway") {
224 $errormessage = "$Lang::tr{'guardian blocking of this address is not allowed'}";
225 }
226
227 # Check if the given input is a valid IP address.
228 elsif (!&General::validip($input)) {
229 $errormessage = "$Lang::tr{'guardian invalid address or subnet'}";
230 }
231
232 # Go further if there was no error.
233 if ($errormessage eq '') {
234 my $block = $settings{'ADDRESS_BLOCK'};
235
236 # Call helper to unblock address.
237 system("/usr/local/bin/guardianctrl block $block &>/dev/null");
238 }
239
240 ## Unblock address or subnet.
241 #
242 } elsif ($settings{'ACTION'} eq $Lang::tr{'unblock'}) {
243
244 # Check if no empty input has been performed.
245 if ($settings{'ADDRESS_UNBLOCK'} ne '') {
246
247 # Check if the given input is no valid IP-address or IP-address with subnet, display an error message.
248 if ((!&General::validip($settings{'ADDRESS_UNBLOCK'})) && (!&General::validipandmask($settings{'ADDRESS_UNBLOCK'}))) {
249 $errormessage = "$Lang::tr{'guardian invalid address or subnet'}";
250 }
251
252 } else {
253 $errormessage = "$Lang::tr{'guardian empty input'}";
254 }
255
256 # Go further if there was no error.
257 if ($errormessage eq '') {
258 my $unblock = $settings{'ADDRESS_UNBLOCK'};
259
260 # Call helper to unblock address.
261 system("/usr/local/bin/guardianctrl unblock $unblock &>/dev/null");
262 }
263
264 ## Unblock all.
265 #
266 } elsif ($settings{'ACTION'} eq $Lang::tr{'unblock all'}) {
267
268 # Call helper to flush iptables chain from guardian.
269 system("/usr/local/bin/guardianctrl flush-chain &>/dev/null");
270 }
271
272 # Load settings from file.
273 &General::readhash("${General::swroot}/guardian/settings", \%settings);
274
275 # Call functions to generate whole page.
276 &showMainBox();
277 &showIgnoreBox();
278
279 # Display area only if guardian is running.
280 if ( ($memory != 0) && ($pid > 0) ) {
281 &showBlockedBox();
282 }
283
284 # Function to display the status of guardian and allow base configuration.
285 sub showMainBox() {
286 my %checked = ();
287 my %selected = ();
288
289 $checked{'GUARDIAN_ENABLED'}{'on'} = '';
290 $checked{'GUARDIAN_ENABLED'}{'off'} = '';
291 $checked{'GUARDIAN_ENABLED'}{$settings{'GUARDIAN_ENABLED'}} = 'checked';
292 $checked{'GUARDIAN_MONITOR_SNORT'}{'off'} = '';
293 $checked{'GUARDIAN_MONITOR_SNORT'}{'on'} = '';
294 $checked{'GUARDIAN_MONITOR_SNORT'}{$settings{'GUARDIAN_MONITOR_SNORT'}} = "checked='checked'";
295 $checked{'GUARDIAN_MONITOR_SSH'}{'off'} = '';
296 $checked{'GUARDIAN_MONITOR_SSH'}{'on'} = '';
297 $checked{'GUARDIAN_MONITOR_SSH'}{$settings{'GUARDIAN_MONITOR_SSH'}} = "checked='checked'";
298 $checked{'GUARDIAN_MONITOR_HTTPD'}{'off'} = '';
299 $checked{'GUARDIAN_MONITOR_HTTPD'}{'on'} = '';
300 $checked{'GUARDIAN_MONITOR_HTTPD'}{$settings{'GUARDIAN_MONITOR_HTTPD'}} = "checked='checked'";
301 $checked{'GUARDIAN_MONITOR_OWNCLOUD'}{'off'} = '';
302 $checked{'GUARDIAN_MONITOR_OWNCLOUD'}{'on'} = '';
303 $checked{'GUARDIAN_MONITOR_OWNCLOUD'}{$settings{'GUARDIAN_MONITOR_OWNCLOUD'}} = "checked='checked'";
304
305 $selected{'GUARDIAN_LOG_FACILITY'}{$settings{'GUARDIAN_LOG_FACILITY'}} = 'selected';
306 $selected{'GUARDIAN_LOGLEVEL'}{$settings{'GUARDIAN_LOGLEVEL'}} = 'selected';
307 $selected{'GUARDIAN_SNORT_PRIORITY_LEVEL'}{$settings{'GUARDIAN_SNORT_PRIORITY_LEVEL'}} = 'selected';
308
309 &Header::openpage($Lang::tr{'guardian configuration'}, 1, '');
310 &Header::openbigbox('100%', 'left', '', $errormessage);
311
312 # Print errormessage if there is one.
313 if ($errormessage) {
314 &Header::openbox('100%', 'left', $Lang::tr{'error messages'});
315 print "<font class='base'>$errormessage&nbsp;</font>\n";
316 &Header::closebox();
317 }
318
319
320 # Draw current guardian state.
321 &Header::openbox('100%', 'center', $Lang::tr{'guardian'});
322
323 # Get current status of guardian.
324 &daemonstats();
325 $pid = $pid[0];
326
327 # Display some useful information related to guardian, if daemon is running.
328 if ( ($memory != 0) && ($pid > 0) ){
329 print <<END;
330 <table width='95%' cellspacing='0' class='tbl'>
331 <tr>
332 <th bgcolor='$color{'color20'}' colspan='3' align='left'><strong>$Lang::tr{'guardian service'}</strong></th>
333 </tr>
334 <tr>
335 <td class='base'>$Lang::tr{'guardian daemon'}</td>
336 <td align='center' colspan='2' width='75%' bgcolor='${Header::colourgreen}'><font color='white'><strong>$Lang::tr{'running'}</strong></font></td>
337 </tr>
338 <tr>
339 <td class='base'></td>
340 <td bgcolor='$color{'color20'}' align='center'><strong>PID</strong></td>
341 <td bgcolor='$color{'color20'}' align='center'><strong>$Lang::tr{'memory'}</strong></td>
342 </tr>
343 <tr>
344 <td class='base'></td>
345 <td bgcolor='$color{'color22'}' align='center'>$pid</td>
346 <td bgcolor='$color{'color22'}' align='center'>$memory KB</td>
347 </tr>
348 </table>
349 END
350 } else {
351 # Otherwise display a hint that the service is not launched.
352 print <<END;
353 <table width='95%' cellspacing='0' class='tbl'>
354 <tr>
355 <th bgcolor='$color{'color20'}' colspan='3' align='left'><strong>$Lang::tr{'guardian service'}</strong></th>
356 </tr>
357 <tr>
358 <td class='base'>$Lang::tr{'guardian daemon'}</td>
359 <td align='center' width='75%' bgcolor='${Header::colourred}'><font color='white'><strong>$Lang::tr{'stopped'}</strong></font></td>
360 </tr>
361 </table>
362 END
363 }
364
365 &Header::closebox();
366
367 # Draw elements for guardian configuration.
368 &Header::openbox('100%', 'center', $Lang::tr{'guardian configuration'});
369
370 print <<END;
371 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
372
373 <table width='95%'>
374 <tr>
375 <td colspan='2' class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'guardian common settings'}</b></td>
376 </tr>
377 <tr>
378 <td width='20%' class='base'>$Lang::tr{'guardian enabled'}:</td>
379 <td><input type='checkbox' name='GUARDIAN_ENABLED' $checked{'GUARDIAN_ENABLED'}{'on'} /></td>
380 </tr>
381 <tr>
382 <td colspan='2'><br></td>
383 </tr>
384 <tr>
385 <td width='20%' class='base'>$Lang::tr{'guardian watch snort alertfile'}</td>
386 <td align='left'>on <input type='radio' name='GUARDIAN_MONITOR_SNORT' value='on' $checked{'GUARDIAN_MONITOR_SNORT'}{'on'} /> /
387 <input type='radio' name='GUARDIAN_MONITOR_SNORT' value='off' $checked{'GUARDIAN_MONITOR_SNORT'}{'off'} /> off</td>
388 </tr>
389 <tr>
390 <td width='20%' class='base'>$Lang::tr{'guardian block ssh brute-force'}</td>
391 <td align='left'>on <input type='radio' name='GUARDIAN_MONITOR_SSH' value='on' $checked{'GUARDIAN_MONITOR_SSH'}{'on'} /> /
392 <input type='radio' name='GUARDIAN_MONITOR_SSH' value='off' $checked{'GUARDIAN_MONITOR_SSH'}{'off'} /> off</td>
393 </tr>
394 <tr>
395 <td width='20%' class='base'>$Lang::tr{'guardian block httpd brute-force'}</td>
396 <td align='left'>on <input type='radio' name='GUARDIAN_MONITOR_HTTPD' value='on' $checked{'GUARDIAN_MONITOR_HTTPD'}{'on'} /> /
397 <input type='radio' name='GUARDIAN_MONITOR_HTTPD' value='off' $checked{'GUARDIAN_MONITOR_HTTPD'}{'off'} /> off</td>
398 </tr>
399 END
400 # Display owncloud checkbox when the addon is installed.
401 if ( -e "$owncloud_meta" ) {
402 print"<tr>\n";
403 print"<td width='20%' class='base'>$Lang::tr{'guardian block owncloud brute-force'}</td>\n";
404 print"<td align='left'>on <input type='radio' name='GUARDIAN_MONITOR_OWNCLOUD' value='on' $checked{'GUARDIAN_MONITOR_OWNCLOUD'}{'on'} /> /\n";
405 print"<input type='radio' name='GUARDIAN_MONITOR_OWNCLOUD' value='off' $checked{'GUARDIAN_MONITOR_OWNCLOUD'}{'off'} /> off</td>\n";
406 print"</tr>\n";
407 }
408 print <<END;
409 <tr>
410 <td colspan='2'><br></td>
411 </tr>
412 <tr>
413 <td align='left' width='20%'>$Lang::tr{'guardian logfacility'}:</td>
414 <td><select name='GUARDIAN_LOG_FACILITY'>
415 <option value='syslog' $selected{'GUARDIAN_LOG_FACILITY'}{'syslog'}>syslog</option>
416 <option value='file' $selected{'GUARDIAN_LOG_FACILITY'}{'file'}>file</option>
417 <option value='console' $selected{'GUARDIAN_LOG_FACILITY'}{'console'}>console</option>
418 </select></td>
419 </tr>
420 <tr>
421 <td colspan='2'><br></td>
422 </tr>
423 <tr>
424 <td align='left' width='20%'>$Lang::tr{'guardian loglevel'}:</td>
425 <td><select name='GUARDIAN_LOGLEVEL'>
426 <option value='off' $selected{'GUARDIAN_LOGLEVEL'}{'off'}>off</option>
427 <option value='info' $selected{'GUARDIAN_LOGLEVEL'}{'info'}>info</option>
428 <option value='debug' $selected{'GUARDIAN_LOGLEVEL'}{'debug'}>debug</option>
429 </select></td>
430 </tr>
431 <tr>
432 <td colspan='2'><br></td>
433 </tr>
434 <tr>
435 <td align='left' width='20%'>$Lang::tr{'guardian priority level'}:</td>
436 <td><select name='GUARDIAN_SNORT_PRIORITY_LEVEL'>
437 <option value='1' $selected{'GUARDIAN_SNORT_PRIORITY_LEVEL'}{'1'}>1</option>
438 <option value='2' $selected{'GUARDIAN_SNORT_PRIORITY_LEVEL'}{'2'}>2</option>
439 <option value='3' $selected{'GUARDIAN_SNORT_PRIORITY_LEVEL'}{'3'}>3</option>
440 <option value='4' $selected{'GUARDIAN_SNORT_PRIORITY_LEVEL'}{'4'}>4</option>
441 </select></td>
442 </tr>
443 <tr>
444 <td colspan='2'><br></td>
445 </tr>
446 <tr>
447 <td width='20%' class='base'>$Lang::tr{'guardian blockcount'}:</td>
448 <td><input type='text' name='GUARDIAN_BLOCKCOUNT' value='$settings{'GUARDIAN_BLOCKCOUNT'}' size='5' /></td>
449 </tr>
450 <tr>
451 <td width='20%' class='base'>$Lang::tr{'guardian blocktime'}:</td>
452 <td><input type='text' name='GUARDIAN_BLOCKTIME' value='$settings{'GUARDIAN_BLOCKTIME'}' size='10' /></td>
453 </tr>
454 <tr>
455 <td width='20%' class='base'>$Lang::tr{'guardian logfile'}:</td>
456 <td><input type='text' name='GUARDIAN_LOGFILE' value='$settings{'GUARDIAN_LOGFILE'}' size='30' /></td>
457 </tr>
458 </table>
459 END
460
461 print <<END;
462 <hr>
463
464 <table width='95%'>
465 <tr>
466 <td>&nbsp;</td>
467 <td align='center'><input type='submit' name='ACTION' value='$Lang::tr{'save'}' /></td>
468 <td>&nbsp;</td>
469 </tr>
470 </table>
471 </form>
472 END
473
474 &Header::closebox();
475 }
476
477 # Function to show elements of the guardian ignorefile and allow to add or remove single members of it.
478 sub showIgnoreBox() {
479 &Header::openbox('100%', 'center', $Lang::tr{'guardian ignored hosts'});
480
481 print <<END;
482 <table width='60%'>
483 <tr>
484 <td colspan='2' class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'guardian ignored hosts'}</b></td>
485 </tr>
486 END
487 # Check if the guardian ignore file contains any content.
488 if (-s $ignorefile) {
489
490 # Open file and print contents.
491 open FILE, $ignorefile or die "Could not open $ignorefile";
492
493 my $id = 0;
494 my $col = "";
495
496 # Read file line by line and print out the elements.
497 while( my $ignored_element = <FILE> ) {
498
499 # Increase id number for each element in the ignore file.
500 $id++;
501
502 # Check if the id number is even or not.
503 if ($id % 2) {
504 $col="bgcolor='$color{'color22'}'";
505 } else {
506 $col="bgcolor='$color{'color20'}'";
507 }
508
509 print <<END;
510 <tr>
511 <td width='80%' class='base' $col>$ignored_element</td>
512 <td width='20%' align='center' $col>
513 <form method='post' name='$id' action='$ENV{'SCRIPT_NAME'}'>
514 <input type='image' name='$Lang::tr{'remove'}' src='/images/delete.gif' title='$Lang::tr{'remove'}' alt='$Lang::tr{'remove'}'>
515 <input type='hidden' name='ID' value='$id'>
516 <input type='hidden' name='ACTION' value='$Lang::tr{'remove'}'>
517 </form>
518 </td>
519 </tr>
520 END
521 }
522 close (FILE);
523 } else {
524 # Print notice that currently no elements are stored in the ignore file.
525 print "<tr>\n";
526 print "<td class='base' colspan='2'>$Lang::tr{'guardian no entries'}</td>\n";
527 print "</tr>\n";
528 }
529
530 print "</table>\n";
531
532 # Section to add new elements to the ignore list.
533 print <<END;
534 <br>
535 <div align='center'>
536 <table width='60%'>
537 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
538 <tr>
539 <td width='30%'>$Lang::tr{'dnsforward add a new entry'}: </td>
540 <td width='50%'><input type='text' name='NEW_IGNORE_ENTRY' value='' size='24' /></td>
541 <td align='center' width='20%'><input type='submit' name='ACTION' value='$Lang::tr{'add'}' /></td>
542 </tr>
543 </form>
544 </table>
545 </div>
546 END
547
548 &Header::closebox();
549 }
550
551 # Function to list currently bocked addresses from guardian and unblock them or add custom entries to block.
552 sub showBlockedBox() {
553 &Header::openbox('100%', 'center', $Lang::tr{'guardian blocked hosts'});
554
555 print <<END;
556 <table width='60%'>
557 <tr>
558 <td colspan='2' class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'guardian blocked hosts'}</b></td>
559 </tr>
560 END
561
562 # Lauch function to get the currently blocked hosts.
563 my @blocked_hosts = &GetBlockedHosts();
564
565 my $id = 0;
566 my $col = "";
567
568 # Loop through our blocked hosts array.
569 foreach my $blocked_host (@blocked_hosts) {
570
571 # Increase id number for each element in the ignore file.
572 $id++;
573
574 # Check if the id number is even or not.
575 if ($id % 2) {
576 $col="bgcolor='$color{'color22'}'";
577 } else {
578 $col="bgcolor='$color{'color20'}'";
579 }
580
581 print <<END;
582 <tr>
583 <td width='80%' class='base' $col><a href='/cgi-bin/ipinfo.cgi?ip=$blocked_host'>$blocked_host</a></td>
584 <td width='20%' align='center' $col>
585 <form method='post' name='frmb$id' action='$ENV{'SCRIPT_NAME'}'>
586 <input type='image' name='$Lang::tr{'unblock'}' src='/images/delete.gif' title='$Lang::tr{'unblock'}' alt='$Lang::tr{'unblock'}'>
587 <input type='hidden' name='ADDRESS_UNBLOCK' value='$blocked_host'>
588 <input type='hidden' name='ACTION' value='$Lang::tr{'unblock'}'>
589 </form>
590 </td>
591 </tr>
592 END
593 }
594
595 # If the loop only has been runs once the id still is "0", which means there are no
596 # additional entries (blocked hosts) in the iptables chain.
597 if ($id == 0) {
598
599 # Print notice that currently no hosts are blocked.
600 print "<tr>\n";
601 print "<td class='base' colspan='2'>$Lang::tr{'guardian no entries'}</td>\n";
602 print "</tr>\n";
603 }
604
605 print "</table>\n";
606
607 # Section for a manual block of an IP-address.
608 print <<END;
609 <br>
610 <div align='center'>
611 <table width='60%' border='0'>
612 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
613 <tr>
614 <td width='30%'>$Lang::tr{'guardian block a host'}: </td>
615 <td width='40%'><input type='text' name='ADDRESS_BLOCK' value='' size='24' /></td>
616 <td align='center' width='15%'><input type='submit' name='ACTION' value='$Lang::tr{'block'}'></td>
617 <td align='center' width='15%'><input type='submit' name='ACTION' value='$Lang::tr{'unblock all'}'></td>
618 </tr>
619 </form>
620 </table>
621 </div>
622 END
623
624 &Header::closebox();
625 }
626
627 &Header::closebigbox();
628 &Header::closepage();
629
630 # Function to check if guardian has been started.
631 # Grab process id and consumed memory if the daemon is running.
632 sub daemonstats() {
633 $memory = 0;
634 # for pid and memory
635 open(FILE, '/usr/local/bin/addonctrl guardian status | ');
636 @guardian = <FILE>;
637 close(FILE);
638 $string = join("", @guardian);
639 $string =~ s/[a-z_]//gi;
640 $string =~ s/\[[0-1]\;[0-9]+//gi;
641 $string =~ s/[\(\)\.]//gi;
642 $string =~ s/ //gi;
643 $string =~ s/\e//gi;
644 @pid = split(/\s/,$string);
645 if (open(FILE, "/proc/$pid[0]/statm")){
646 my $temp = <FILE>;
647 @memory = split(/ /,$temp);
648 close(FILE);
649 }
650 $memory+=$memory[0];
651 }
652
653 sub GetBlockedHosts() {
654
655 # Create new, empty array.
656 my @hosts;
657
658 # Lauch helper to get chains from iptables.
659 open(FILE, "/usr/local/bin/guardianctrl get-chain |");
660
661 # Read file line by line and print out the elements.
662 foreach my $line (<FILE>) {
663
664 # Skip descriptive lines.
665 next if ($line =~ /^Chain/);
666 next if ($line =~ /^ pkts/);
667
668 # Generate array, based on the line content (seperator is a single or multiple space's)
669 my @comps = split(/\s{1,}/, $line);
670 my ($lead, $pkts, $bytes, $target, $prot, $opt, $in, $out, $source, $destination) = @comps;
671
672 # Assign different variable names.
673 my $blocked_host = $source;
674
675 # Add host to our hosts array.
676 push(@hosts, $blocked_host);
677 }
678
679 # Convert entries, sort them, write back and store the sorted entries into new array.
680 my @sorted = map { $_->[0] }
681 sort { $a->[1] <=> $b->[1] }
682 map { [$_, int sprintf("%03.f%03.f%03.f%03.f", split(/\./, $_))] }
683 @hosts;
684
685 # Return our sorted list.
686 return @sorted
687 }
688
689 sub BuildConfiguration() {
690 my %settings = ();
691 &General::readhash("${General::swroot}/guardian/settings", \%settings);
692
693 my $configfile = "${General::swroot}/guardian/guardian.conf";
694
695 # Open configfile for writing.
696 open(FILE, ">$configfile");
697
698 # Config file header.
699 print FILE "# Autogenerated configuration file.\n";
700 print FILE "# All user modifications will be overwritten.\n\n";
701
702 # Settings for the logging mechanism.
703 print FILE "# Log settings.\n";
704 print FILE "LogFacility = $settings{'GUARDIAN_LOG_FACILITY'}\n";
705
706 if ($settings{'GUARDIAN_LOG_FACILITY'} eq "file") {
707 print FILE "LogFile = $settings{'GUARDIAN_LOGFILE'}\n";
708 }
709
710 print FILE "LogLevel = $settings{'GUARDIAN_LOGLEVEL'}\n\n";
711
712 # IPFire related static settings.
713 print FILE "# IPFire related settings.\n";
714 print FILE "FirewallEngine = IPtables\n";
715 print FILE "SocketOwner = nobody:nobody\n";
716 print FILE "IgnoreFile = $ignorefile\n\n";
717
718 # Configured block values.
719 print FILE "# Configured block values.\n";
720 print FILE "BlockCount = $settings{'GUARDIAN_BLOCKCOUNT'}\n";
721 print FILE "BlockTime = $settings{'GUARDIAN_BLOCKTIME'}\n\n";
722
723 # Enabled modules.
724 # Loop through whole settings hash.
725 print FILE "# Enabled modules.\n";
726 foreach my $option (keys %settings) {
727 # Search for enabled modules.
728 if ($option =~ /GUARDIAN_MONITOR_(.*)/) {
729 # Skip if module is not enabled.
730 next unless($settings{$option} eq "on");
731
732 # Skip module if no file location is available.
733 next unless(exists($module_file_locations{$1}));
734
735 # Add enabled module and defined path to the config file.
736 print FILE "Monitor_$1 = $module_file_locations{$1}\n";
737 }
738 }
739
740 # Module settings.
741 print FILE "\n# Module settings.\n";
742 # Check if SNORT is enabled and add snort priority.
743 if ($settings{'GUARDIAN_MONITOR_SNORT'} eq "on") {
744 print FILE "SnortPriorityLevel = $settings{'GUARDIAN_SNORT_PRIORITY_LEVEL'}\n";
745 }
746
747 close(FILE);
748
749 # Check if guardian should be started or stopped.
750 if($settings{'GUARDIAN_ENABLED'} eq 'on') {
751 if($pid > 0) {
752 # Call guardianctl to perform a reload.
753 system("/usr/local/bin/guardianctrl reload &>/dev/null");
754 } else {
755 # Launch guardian.
756 system("/usr/local/bin/guardianctrl start &>/dev/null");
757 }
758 } else {
759 # Stop the daemon.
760 system("/usr/local/bin/guardianctrl stop &>/dev/null");
761 }
762 }