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