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