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