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