]>
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'; | |
7899718f | 61 | $settings{'GUARDIAN_LOGLEVEL'} ='info'; |
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 = (); | |
7899718f | 234 | my %selected = (); |
01dbccb1 SS |
235 | |
236 | $checked{'GUARDIAN_ENABLED'}{'on'} = ''; | |
237 | $checked{'GUARDIAN_ENABLED'}{'off'} = ''; | |
238 | $checked{'GUARDIAN_ENABLED'}{$settings{'GUARDIAN_ENABLED'}} = 'checked'; | |
26fcd31e SS |
239 | $checked{'GUARDIAN_ENABLE_SNORT'}{'off'} = ''; |
240 | $checked{'GUARDIAN_ENABLE_SNORT'}{'on'} = ''; | |
241 | $checked{'GUARDIAN_ENABLE_SNORT'}{$settings{'GUARDIAN_ENABLE_SNORT'}} = "checked='checked'"; | |
242 | $checked{'GUARDIAN_ENABLE_SSH'}{'off'} = ''; | |
243 | $checked{'GUARDIAN_ENABLE_SSH'}{'on'} = ''; | |
244 | $checked{'GUARDIAN_ENABLE_SSH'}{$settings{'GUARDIAN_ENABLE_SSH'}} = "checked='checked'"; | |
245 | $checked{'GUARDIAN_ENABLE_HTTPD'}{'off'} = ''; | |
246 | $checked{'GUARDIAN_ENABLE_HTTPD'}{'on'} = ''; | |
247 | $checked{'GUARDIAN_ENABLE_HTTPD'}{$settings{'GUARDIAN_ENABLE_HTTPD'}} = "checked='checked'"; | |
01dbccb1 | 248 | |
7899718f SS |
249 | $selected{'GUARDIAN_LOGLEVEL'}{$settings{'GUARDIAN_LOGLEVEL'}}= 'selected'; |
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> | |
06ff7e28 | 326 | <td width='20%' class='base'>$Lang::tr{'guardian watch snort alertfile'}</td> |
26fcd31e SS |
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> | |
06ff7e28 | 331 | <td width='20%' class='base'>$Lang::tr{'guardian block ssh brute-force'}</td> |
26fcd31e SS |
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> | |
06ff7e28 | 336 | <td width='20%' class='base'>$Lang::tr{'guardian block httpd brute-force'}</td> |
26fcd31e SS |
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> | |
7899718f SS |
343 | <tr> |
344 | <td align='left' width='20%'>$Lang::tr{'guardian loglevel'}</td> | |
345 | <td><select name='GUARDIAN_LOGLEVEL'> | |
346 | <option value='off' $selected{'GUARDIAN_LOGLEVEL'}{'off'}>off</option> | |
347 | <option value='info' $selected{'GUARDIAN_LOGLEVEL'}{'info'}>info</option> | |
348 | <option value='debug' $selected{'GUARDIAN_LOGLEVEL'}{'debug'}>debug</option> | |
349 | </select></td> | |
350 | </tr> | |
351 | ||
01dbccb1 SS |
352 | <tr> |
353 | <td width='20%' class='base'>$Lang::tr{'guardian blocktime'}:</td> | |
d2fea55e | 354 | <td><input type='text' name='GUARDIAN_BLOCKTIME' value='$settings{'GUARDIAN_BLOCKTIME'}' size='10' /></td> |
01dbccb1 SS |
355 | </tr> |
356 | <tr> | |
357 | <td width='20%' class='base'>$Lang::tr{'guardian logfile'}:</td> | |
d2fea55e | 358 | <td><input type='text' name='GUARDIAN_LOGFILE' value='$settings{'GUARDIAN_LOGFILE'}' size='30' /></td> |
01dbccb1 SS |
359 | </tr> |
360 | <tr> | |
361 | <td width='20%' class='base'>$Lang::tr{'guardian snort alertfile'}:</td> | |
d2fea55e | 362 | <td><input type='text' name='GUARDIAN_SNORT_ALERTFILE' value='$settings{'GUARDIAN_SNORT_ALERTFILE'}' size='30' /></td> |
01dbccb1 SS |
363 | </tr> |
364 | </table> | |
365 | END | |
366 | ||
367 | print <<END; | |
368 | <hr> | |
369 | ||
370 | <table width='95%'> | |
371 | <tr> | |
372 | <td> </td> | |
373 | <td align='center'><input type='submit' name='ACTION' value='$Lang::tr{'save'}' /></td> | |
374 | <td> </td> | |
375 | </tr> | |
376 | </table> | |
377 | </form> | |
378 | END | |
379 | ||
380 | &Header::closebox(); | |
381 | } | |
382 | ||
383 | # Function to show elements of the guardian ignorefile and allow to add or remove single members of it. | |
384 | sub showIgnoreBox() { | |
385 | &Header::openbox('100%', 'center', $Lang::tr{'guardian ignored hosts'}); | |
386 | ||
387 | print <<END; | |
388 | <table width='60%'> | |
389 | <tr> | |
390 | <td colspan='2' class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'guardian ignored hosts'}</b></td> | |
391 | </tr> | |
392 | END | |
393 | # Check if the guardian ignore file contains any content. | |
394 | if (-s $ignorefile) { | |
395 | ||
396 | # Open file and print contents. | |
397 | open FILE, $ignorefile or die "Could not open $ignorefile"; | |
398 | ||
399 | my $id = 0; | |
400 | my $col = ""; | |
401 | ||
402 | # Read file line by line and print out the elements. | |
403 | while( my $ignored_element = <FILE> ) { | |
404 | ||
405 | # Increase id number for each element in the ignore file. | |
406 | $id++; | |
407 | ||
408 | # Check if the id number is even or not. | |
409 | if ($id % 2) { | |
410 | $col="bgcolor='$color{'color22'}'"; | |
411 | } else { | |
412 | $col="bgcolor='$color{'color20'}'"; | |
413 | } | |
414 | ||
415 | print <<END; | |
416 | <tr> | |
417 | <td width='80%' class='base' $col>$ignored_element</td> | |
418 | <td width='20%' align='center' $col> | |
419 | <form method='post' name='frma$id' action='$ENV{'SCRIPT_NAME'}'> | |
420 | <input type='image' name='$Lang::tr{'remove'}' src='/images/delete.gif' title='$Lang::tr{'remove'}' alt='$Lang::tr{'remove'}'> | |
421 | <input type='hidden' name='ID' value='$id'> | |
422 | <input type='hidden' name='ACTION' value='$Lang::tr{'remove'}'> | |
423 | </form> | |
424 | </td> | |
425 | </tr> | |
426 | END | |
427 | } | |
428 | close (FILE); | |
429 | } else { | |
430 | # Print notice that currently no elements are stored in the ignore file. | |
431 | print "<tr>\n"; | |
432 | print "<td class='base' colspan='2'>$Lang::tr{'guardian no entries'}</td>\n"; | |
433 | print "</tr>\n"; | |
434 | } | |
435 | ||
436 | print "</table>\n"; | |
437 | ||
01dbccb1 SS |
438 | # Section to add new elements to the ignore list. |
439 | print <<END; | |
1d5702a7 SS |
440 | <br> |
441 | <div align='center'> | |
01dbccb1 SS |
442 | <table width='60%'> |
443 | <form method='post' action='$ENV{'SCRIPT_NAME'}'> | |
444 | <tr> | |
445 | <td width='30%'>$Lang::tr{'dnsforward add a new entry'}: </td> | |
446 | <td width='50%'><input type='text' name='NEW_IGNORE_ENTRY' value='' size='24' /></td> | |
447 | <td align='center' width='20%'><input type='submit' name='ACTION' value='$Lang::tr{'add'}' /></td> | |
448 | </tr> | |
449 | </form> | |
450 | </table> | |
451 | </div> | |
452 | END | |
1d5702a7 SS |
453 | |
454 | &Header::closebox(); | |
01dbccb1 SS |
455 | } |
456 | ||
457 | # Function to list currently bocked addresses from guardian and unblock them or add custom entries to block. | |
458 | sub showBlockedBox() { | |
459 | &Header::openbox('100%', 'center', $Lang::tr{'guardian blocked hosts'}); | |
460 | ||
01dbccb1 SS |
461 | print <<END; |
462 | <table width='60%'> | |
463 | <tr> | |
464 | <td colspan='2' class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'guardian blocked hosts'}</b></td> | |
465 | </tr> | |
466 | END | |
467 | ||
5f462919 SS |
468 | # Lauch function to get the currently blocked hosts. |
469 | my @blocked_hosts = &GetBlockedHosts(); | |
470 | ||
01dbccb1 | 471 | my $id = 0; |
01dbccb1 | 472 | my $col = ""; |
01dbccb1 | 473 | |
5f462919 SS |
474 | # Loop through our blocked hosts array. |
475 | foreach my $blocked_host (@blocked_hosts) { | |
01dbccb1 SS |
476 | |
477 | # Increase id number for each element in the ignore file. | |
478 | $id++; | |
479 | ||
480 | # Check if the id number is even or not. | |
481 | if ($id % 2) { | |
482 | $col="bgcolor='$color{'color22'}'"; | |
483 | } else { | |
484 | $col="bgcolor='$color{'color20'}'"; | |
485 | } | |
486 | ||
487 | print <<END; | |
488 | <tr> | |
8b8413e5 | 489 | <td width='80%' class='base' $col><a href='/cgi-bin/ipinfo.cgi?ip=$blocked_host'>$blocked_host</a></td> |
01dbccb1 SS |
490 | <td width='20%' align='center' $col> |
491 | <form method='post' name='frmb$id' action='$ENV{'SCRIPT_NAME'}'> | |
492 | <input type='image' name='$Lang::tr{'unblock'}' src='/images/delete.gif' title='$Lang::tr{'unblock'}' alt='$Lang::tr{'unblock'}'> | |
493 | <input type='hidden' name='ADDRESS_UNBLOCK' value='$blocked_host'> | |
494 | <input type='hidden' name='ACTION' value='$Lang::tr{'unblock'}'> | |
495 | </form> | |
496 | </td> | |
497 | </tr> | |
498 | END | |
499 | } | |
500 | ||
01dbccb1 SS |
501 | # If the loop only has been runs once the id still is "0", which means there are no |
502 | # additional entries (blocked hosts) in the iptables chain. | |
503 | if ($id == 0) { | |
504 | ||
505 | # Print notice that currently no hosts are blocked. | |
506 | print "<tr>\n"; | |
507 | print "<td class='base' colspan='2'>$Lang::tr{'guardian no entries'}</td>\n"; | |
508 | print "</tr>\n"; | |
509 | } | |
510 | ||
511 | print "</table>\n"; | |
512 | ||
01dbccb1 SS |
513 | # Section for a manual block of an IP-address. |
514 | print <<END; | |
1d5702a7 SS |
515 | <br> |
516 | <div align='center'> | |
517 | <table width='60%' border='0'> | |
01dbccb1 SS |
518 | <form method='post' action='$ENV{'SCRIPT_NAME'}'> |
519 | <tr> | |
520 | <td width='30%'>$Lang::tr{'guardian block a host'}: </td> | |
521 | <td width='40%'><input type='text' name='ADDRESS_BLOCK' value='' size='24' /></td> | |
522 | <td align='center' width='15%'><input type='submit' name='ACTION' value='$Lang::tr{'block'}'></td> | |
523 | <td align='center' width='15%'><input type='submit' name='ACTION' value='$Lang::tr{'unblock all'}'></td> | |
524 | </tr> | |
525 | </form> | |
526 | </table> | |
527 | </div> | |
528 | END | |
1d5702a7 SS |
529 | |
530 | &Header::closebox(); | |
01dbccb1 SS |
531 | } |
532 | ||
533 | &Header::closebigbox(); | |
534 | &Header::closepage(); | |
535 | ||
536 | # Function to check if guardian has been started. | |
537 | # Grab process id and consumed memory if the daemon is running. | |
538 | sub daemonstats() { | |
539 | $memory = 0; | |
540 | # for pid and memory | |
541 | open(FILE, '/usr/local/bin/addonctrl guardian status | '); | |
542 | @guardian = <FILE>; | |
543 | close(FILE); | |
544 | $string = join("", @guardian); | |
545 | $string =~ s/[a-z_]//gi; | |
546 | $string =~ s/\[[0-1]\;[0-9]+//gi; | |
547 | $string =~ s/[\(\)\.]//gi; | |
548 | $string =~ s/ //gi; | |
549 | $string =~ s/\e//gi; | |
550 | @pid = split(/\s/,$string); | |
551 | if (open(FILE, "/proc/$pid[0]/statm")){ | |
552 | my $temp = <FILE>; | |
553 | @memory = split(/ /,$temp); | |
554 | close(FILE); | |
555 | } | |
556 | $memory+=$memory[0]; | |
557 | } | |
558 | ||
5f462919 SS |
559 | sub GetBlockedHosts() { |
560 | ||
561 | # Create new, empty array. | |
562 | my @hosts; | |
563 | ||
564 | # Lauch helper to get chains from iptables. | |
565 | open(FILE, "/usr/local/bin/guardianctrl get-chain |"); | |
566 | ||
567 | # Read file line by line and print out the elements. | |
568 | foreach my $line (<FILE>) { | |
569 | ||
570 | # Skip descriptive lines. | |
571 | next if ($line =~ /^Chain/); | |
572 | next if ($line =~ /^ pkts/); | |
573 | ||
574 | # Generate array, based on the line content (seperator is a single or multiple space's) | |
575 | my @comps = split(/\s{1,}/, $line); | |
576 | my ($lead, $pkts, $bytes, $target, $prot, $opt, $in, $out, $source, $destination) = @comps; | |
577 | ||
578 | # Assign different variable names. | |
579 | my $blocked_host = $source; | |
580 | ||
581 | # Add host to our hosts array. | |
582 | push(@hosts, $blocked_host); | |
583 | } | |
584 | ||
585 | # Convert entries, sort them, write back and store the sorted entries into new array. | |
586 | my @sorted = map { $_->[0] } | |
587 | sort { $a->[1] <=> $b->[1] } | |
588 | map { [$_, int sprintf("%03.f%03.f%03.f%03.f", split(/\./, $_))] } | |
589 | @hosts; | |
590 | ||
591 | # Return our sorted list. | |
592 | return @sorted | |
593 | } | |
594 | ||
01dbccb1 SS |
595 | sub BuildConfiguration() { |
596 | my %settings = (); | |
597 | &General::readhash("${General::swroot}/guardian/settings", \%settings); | |
598 | ||
599 | my $configfile = "${General::swroot}/guardian/guardian.conf"; | |
600 | ||
7f728591 SS |
601 | # We set this to 1 (enabled) to prevent guardian from blocking the ISP gateway. |
602 | my $HostGatewayByte = "1"; | |
603 | ||
7f728591 | 604 | # Open configfile for writing. |
01dbccb1 SS |
605 | open(FILE, ">$configfile"); |
606 | ||
26fcd31e SS |
607 | print FILE "EnableSnortMonitoring $settings{'GUARDIAN_ENABLE_SNORT'}\n"; |
608 | print FILE "EnableSSHMonitoring $settings{'GUARDIAN_ENABLE_SSH'}\n"; | |
609 | print FILE "EnableHTTPDMonitoring $settings{'GUARDIAN_ENABLE_HTTPD'}\n"; | |
7899718f | 610 | print FILE "LogLevel $settings{'GUARDIAN_LOGLEVEL'}\n"; |
26fcd31e SS |
611 | print FILE "HostGatewayByte $HostGatewayByte\n"; |
612 | print FILE "LogFile $settings{'GUARDIAN_LOGFILE'}\n"; | |
613 | print FILE "AlertFile $settings{'GUARDIAN_SNORT_ALERTFILE'}\n"; | |
614 | print FILE "IgnoreFile $ignorefile\n"; | |
26fcd31e | 615 | print FILE "TimeLimit $settings{'GUARDIAN_BLOCKTIME'}\n"; |
01dbccb1 SS |
616 | |
617 | close(FILE); | |
618 | ||
619 | # Restart the service. | |
620 | if ($settings{'GUARDIAN_ENABLED'} eq 'on') { | |
621 | system("/usr/local/bin/guardianctrl restart &>/dev/null"); | |
622 | } else { | |
623 | system("/usr/local/bin/guardianctrl stop &>/dev/null"); | |
624 | } | |
625 | } |