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