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