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