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