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