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