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