]>
Commit | Line | Data |
---|---|---|
ac1cfefa | 1 | #!/usr/bin/perl |
70df8302 MT |
2 | ############################################################################### |
3 | # # | |
4 | # IPFire.org - A linux based firewall # | |
e698090e | 5 | # Copyright (C) 2007-2020 IPFire Team <info@ipfire.org> # |
70df8302 MT |
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 | ||
ac1cfefa MT |
22 | use strict; |
23 | ||
24 | # enable only the following on debugging purpose | |
90c2e164 CS |
25 | #use warnings; |
26 | #use CGI::Carp 'fatalsToBrowser'; | |
ac1cfefa | 27 | |
986e08d9 | 28 | require '/var/ipfire/general-functions.pl'; |
ac1cfefa MT |
29 | require "${General::swroot}/lang.pl"; |
30 | require "${General::swroot}/header.pl"; | |
8dcebe53 | 31 | require "${General::swroot}/ids-functions.pl"; |
abffcc99 | 32 | require "${General::swroot}/network-functions.pl"; |
ac1cfefa | 33 | |
3e12c6e6 SS |
34 | # Import ruleset providers file. |
35 | require "$IDS::rulesetsourcesfile"; | |
36 | ||
f2fdd0c1 CS |
37 | my %color = (); |
38 | my %mainsettings = (); | |
9d18656b | 39 | my %idsrules = (); |
1286e0d4 | 40 | my %idssettings=(); |
2f252efa | 41 | my %used_providers=(); |
298723b9 | 42 | my %cgiparams=(); |
ac1cfefa | 43 | my %checked=(); |
5a3e0dca | 44 | my %selected=(); |
b7e29743 | 45 | my %ignored=(); |
0b89daee SS |
46 | |
47 | # Read-in main settings, for language, theme and colors. | |
48 | &General::readhash("${General::swroot}/main/settings", \%mainsettings); | |
8186b372 | 49 | &General::readhash("/srv/web/ipfire/html/themes/ipfire/include/colors.txt", \%color); |
0b89daee | 50 | |
1286e0d4 SS |
51 | # Get the available network zones, based on the config type of the system and store |
52 | # the list of zones in an array. | |
abffcc99 | 53 | my @network_zones = &Network::get_available_network_zones(); |
ac1cfefa | 54 | |
51b63b41 SS |
55 | # Check if openvpn is started and add it to the array of network zones. |
56 | if ( -e "/var/run/openvpn.pid") { | |
57 | push(@network_zones, "ovpn"); | |
58 | } | |
59 | ||
43263ea6 SS |
60 | my $errormessage; |
61 | ||
00512a5a | 62 | # Create files if they does not exist yet. |
b02e30fd | 63 | &IDS::check_and_create_filelayout(); |
01ba4be4 | 64 | |
99b372b5 SS |
65 | # Hash which contains the colour code of a network zone. |
66 | my %colourhash = ( | |
67 | 'red' => $Header::colourred, | |
68 | 'green' => $Header::colourgreen, | |
69 | 'blue' => $Header::colourblue, | |
51b63b41 SS |
70 | 'orange' => $Header::colourorange, |
71 | 'ovpn' => $Header::colourovpn | |
99b372b5 SS |
72 | ); |
73 | ||
ac1cfefa MT |
74 | &Header::showhttpheaders(); |
75 | ||
298723b9 SS |
76 | #Get GUI values |
77 | &Header::getcgihash(\%cgiparams); | |
ac1cfefa | 78 | |
b7e29743 SS |
79 | ## Add/edit an entry to the ignore file. |
80 | # | |
81 | if (($cgiparams{'WHITELIST'} eq $Lang::tr{'add'}) || ($cgiparams{'WHITELIST'} eq $Lang::tr{'update'})) { | |
82 | ||
83 | # Check if any input has been performed. | |
84 | if ($cgiparams{'IGNORE_ENTRY_ADDRESS'} ne '') { | |
85 | ||
86 | # Check if the given input is no valid IP-address or IP-address with subnet, display an error message. | |
87 | if ((!&General::validip($cgiparams{'IGNORE_ENTRY_ADDRESS'})) && (!&General::validipandmask($cgiparams{'IGNORE_ENTRY_ADDRESS'}))) { | |
88 | $errormessage = "$Lang::tr{'guardian invalid address or subnet'}"; | |
89 | } | |
90 | } else { | |
91 | $errormessage = "$Lang::tr{'guardian empty input'}"; | |
92 | } | |
93 | ||
94 | # Go further if there was no error. | |
95 | if ($errormessage eq '') { | |
96 | my %ignored = (); | |
97 | my $id; | |
98 | my $status; | |
99 | ||
100 | # Assign hash values. | |
101 | my $new_entry_address = $cgiparams{'IGNORE_ENTRY_ADDRESS'}; | |
102 | my $new_entry_remark = $cgiparams{'IGNORE_ENTRY_REMARK'}; | |
103 | ||
104 | # Read-in ignoredfile. | |
b02e30fd | 105 | &General::readhasharray($IDS::ignored_file, \%ignored); |
b7e29743 SS |
106 | |
107 | # Check if we should edit an existing entry and got an ID. | |
108 | if (($cgiparams{'WHITELIST'} eq $Lang::tr{'update'}) && ($cgiparams{'ID'})) { | |
109 | # Assin the provided id. | |
110 | $id = $cgiparams{'ID'}; | |
111 | ||
112 | # Undef the given ID. | |
113 | undef($cgiparams{'ID'}); | |
114 | ||
115 | # Grab the configured status of the corresponding entry. | |
116 | $status = $ignored{$id}[2]; | |
117 | } else { | |
118 | # Each newly added entry automatically should be enabled. | |
119 | $status = "enabled"; | |
120 | ||
121 | # Generate the ID for the new entry. | |
122 | # | |
123 | # Sort the keys by their ID and store them in an array. | |
124 | my @keys = sort { $a <=> $b } keys %ignored; | |
125 | ||
126 | # Reverse the key array. | |
127 | my @reversed = reverse(@keys); | |
128 | ||
129 | # Obtain the last used id. | |
130 | my $last_id = @reversed[0]; | |
131 | ||
132 | # Increase the last id by one and use it as id for the new entry. | |
133 | $id = ++$last_id; | |
134 | } | |
135 | ||
136 | # Add/Modify the entry to/in the ignored hash. | |
137 | $ignored{$id} = ["$new_entry_address", "$new_entry_remark", "$status"]; | |
138 | ||
139 | # Write the changed ignored hash to the ignored file. | |
b02e30fd | 140 | &General::writehasharray($IDS::ignored_file, \%ignored); |
b7e29743 SS |
141 | |
142 | # Regenerate the ignore file. | |
9283e9b9 | 143 | &IDS::generate_ignore_file(); |
b7e29743 SS |
144 | } |
145 | ||
146 | # Check if the IDS is running. | |
147 | if(&IDS::ids_is_running()) { | |
148 | # Call suricatactrl to perform a reload. | |
149 | &IDS::call_suricatactrl("reload"); | |
150 | } | |
151 | ||
152 | ## Toggle Enabled/Disabled for an existing entry on the ignore list. | |
153 | # | |
154 | ||
155 | } elsif ($cgiparams{'WHITELIST'} eq $Lang::tr{'toggle enable disable'}) { | |
156 | my %ignored = (); | |
157 | ||
158 | # Only go further, if an ID has been passed. | |
159 | if ($cgiparams{'ID'}) { | |
160 | # Assign the given ID. | |
161 | my $id = $cgiparams{'ID'}; | |
162 | ||
163 | # Undef the given ID. | |
164 | undef($cgiparams{'ID'}); | |
165 | ||
166 | # Read-in ignoredfile. | |
b02e30fd | 167 | &General::readhasharray($IDS::ignored_file, \%ignored); |
b7e29743 SS |
168 | |
169 | # Grab the configured status of the corresponding entry. | |
170 | my $status = $ignored{$id}[2]; | |
171 | ||
172 | # Switch the status. | |
173 | if ($status eq "disabled") { | |
174 | $status = "enabled"; | |
175 | } else { | |
176 | $status = "disabled"; | |
177 | } | |
178 | ||
179 | # Modify the status of the existing entry. | |
180 | $ignored{$id} = ["$ignored{$id}[0]", "$ignored{$id}[1]", "$status"]; | |
181 | ||
182 | # Write the changed ignored hash to the ignored file. | |
b02e30fd | 183 | &General::writehasharray($IDS::ignored_file, \%ignored); |
b7e29743 SS |
184 | |
185 | # Regenerate the ignore file. | |
9283e9b9 | 186 | &IDS::generate_ignore_file(); |
b7e29743 SS |
187 | |
188 | # Check if the IDS is running. | |
189 | if(&IDS::ids_is_running()) { | |
190 | # Call suricatactrl to perform a reload. | |
191 | &IDS::call_suricatactrl("reload"); | |
192 | } | |
193 | } | |
194 | ||
195 | ## Remove entry from ignore list. | |
196 | # | |
197 | } elsif ($cgiparams{'WHITELIST'} eq $Lang::tr{'remove'}) { | |
198 | my %ignored = (); | |
199 | ||
200 | # Read-in ignoredfile. | |
b02e30fd | 201 | &General::readhasharray($IDS::ignored_file, \%ignored); |
b7e29743 SS |
202 | |
203 | # Drop entry from the hash. | |
204 | delete($ignored{$cgiparams{'ID'}}); | |
205 | ||
206 | # Undef the given ID. | |
207 | undef($cgiparams{'ID'}); | |
208 | ||
209 | # Write the changed ignored hash to the ignored file. | |
b02e30fd | 210 | &General::writehasharray($IDS::ignored_file, \%ignored); |
b7e29743 SS |
211 | |
212 | # Regenerate the ignore file. | |
9283e9b9 | 213 | &IDS::generate_ignore_file(); |
b7e29743 SS |
214 | |
215 | # Check if the IDS is running. | |
216 | if(&IDS::ids_is_running()) { | |
217 | # Call suricatactrl to perform a reload. | |
218 | &IDS::call_suricatactrl("reload"); | |
219 | } | |
220 | } | |
221 | ||
9074e3d7 SS |
222 | # Check if the page is locked, in this case, the ids_page_lock_file exists. |
223 | if (-e $IDS::ids_page_lock_file) { | |
224 | # Lock the webpage and print notice about autoupgrade of the ruleset | |
225 | # is in progess. | |
226 | &working_notice("$Lang::tr{'ids ruleset autoupdate in progress'}"); | |
227 | ||
228 | # Loop and check if the file still exists. | |
229 | while(-e $IDS::ids_page_lock_file) { | |
230 | # Sleep for a second and re-check. | |
231 | sleep 1; | |
232 | } | |
233 | ||
234 | # Page has been unlocked, perform a reload. | |
235 | &reload(); | |
236 | } | |
237 | ||
3983aebd SS |
238 | # Check if any error has been stored. |
239 | if (-e $IDS::storederrorfile) { | |
240 | # Open file to read in the stored error message. | |
241 | open(FILE, "<$IDS::storederrorfile") or die "Could not open $IDS::storederrorfile. $!\n"; | |
242 | ||
243 | # Read the stored error message. | |
244 | $errormessage = <FILE>; | |
245 | ||
246 | # Close file. | |
247 | close (FILE); | |
248 | ||
249 | # Delete the file, which is now not longer required. | |
250 | unlink($IDS::storederrorfile); | |
251 | } | |
252 | ||
a468b62b SS |
253 | # Gather ruleset details. |
254 | if ($cgiparams{'RULESET'}) { | |
255 | ## Grab all available rules and store them in the idsrules hash. | |
256 | # | |
ddaf8ae1 SS |
257 | |
258 | # Get enabled providers. | |
259 | my @enabled_providers = &IDS::get_enabled_providers(); | |
260 | ||
a468b62b SS |
261 | # Open rules directory and do a directory listing. |
262 | opendir(DIR, $IDS::rulespath) or die $!; | |
263 | # Loop through the direcory. | |
264 | while (my $file = readdir(DIR)) { | |
422204ff | 265 | |
a468b62b SS |
266 | # We only want files. |
267 | next unless (-f "$IDS::rulespath/$file"); | |
422204ff | 268 | |
a468b62b SS |
269 | # Ignore empty files. |
270 | next if (-z "$IDS::rulespath/$file"); | |
422204ff | 271 | |
a468b62b SS |
272 | # Use a regular expression to find files ending in .rules |
273 | next unless ($file =~ m/\.rules$/); | |
422204ff | 274 | |
a468b62b SS |
275 | # Ignore files which are not read-able. |
276 | next unless (-R "$IDS::rulespath/$file"); | |
395e3b90 | 277 | |
a468b62b SS |
278 | # Skip whitelist rules file. |
279 | next if( $file eq "whitelist.rules"); | |
b7e29743 | 280 | |
ddaf8ae1 SS |
281 | # Splitt vendor from filename. |
282 | my @filename_parts = split(/-/, $file); | |
283 | ||
284 | # Assign vendor name for easy processing. | |
285 | my $vendor = @filename_parts[0]; | |
286 | ||
287 | # Skip rulefile if the provider is disabled. | |
288 | next unless ($vendor ~~ @enabled_providers); | |
289 | ||
a468b62b SS |
290 | # Call subfunction to read-in rulefile and add rules to |
291 | # the idsrules hash. | |
292 | &readrulesfile("$file"); | |
293 | } | |
395e3b90 | 294 | |
a468b62b | 295 | closedir(DIR); |
395e3b90 | 296 | |
ddaf8ae1 SS |
297 | # Loop through the array of used providers. |
298 | foreach my $provider (@enabled_providers) { | |
299 | # Gather used rulefiles. | |
300 | my @used_rulesfiles = &IDS::read_used_provider_rulesfiles($provider); | |
301 | ||
302 | # Loop through the array of used rulesfiles. | |
303 | foreach my $rulefile (@used_rulesfiles) { | |
304 | # Check if the current rulefile exists in the %idsrules hash. | |
305 | # If not, the file probably does not exist anymore or contains | |
306 | # no rules. | |
307 | if($idsrules{$rulefile}) { | |
308 | # Add the rulefile state to the %idsrules hash. | |
309 | $idsrules{$rulefile}{'Rulefile'}{'State'} = "on"; | |
310 | } | |
e5738079 SS |
311 | } |
312 | } | |
313 | } | |
314 | ||
298723b9 | 315 | # Save ruleset. |
0943ad8c | 316 | if ($cgiparams{'RULESET'} eq $Lang::tr{'ids apply'}) { |
d2212836 | 317 | # Arrays to store which rulefiles have been enabled and will be used. |
e5738079 | 318 | my @enabled_rulefiles; |
298723b9 | 319 | |
af8e5145 SS |
320 | # Store if a restart of suricata is required. |
321 | my $suricata_restart_required; | |
322 | ||
9d18656b SS |
323 | # Loop through the hash of idsrules. |
324 | foreach my $rulefile(keys %idsrules) { | |
1622e5c1 SS |
325 | # Check if the state of the rulefile has been changed. |
326 | unless ($cgiparams{$rulefile} eq $idsrules{$rulefile}{'Rulefile'}{'State'}) { | |
327 | # A restart of suricata is required to apply the changes of the used rulefiles. | |
328 | $suricata_restart_required = 1; | |
329 | } | |
330 | ||
e5738079 SS |
331 | # Check if the rulefile is enabled. |
332 | if ($cgiparams{$rulefile} eq "on") { | |
333 | # Add rulefile to the array of enabled rulefiles. | |
334 | push(@enabled_rulefiles, $rulefile); | |
b65b5ef3 SS |
335 | |
336 | # Drop item from cgiparams hash. | |
337 | delete $cgiparams{$rulefile}; | |
e5738079 | 338 | } |
466c6779 | 339 | } |
e5738079 | 340 | |
15832b10 SS |
341 | # Open oinkmaster main include file for provider modifications. |
342 | open(OINKM_INCL_FILE, ">", "$IDS::oinkmaster_provider_includes_file") or die "Could not open $IDS::oinkmaster_provider_includes_file. $!\n"; | |
d2212836 | 343 | |
15832b10 SS |
344 | # Print file header and notice about autogenerated file. |
345 | print OINKM_INCL_FILE "#Autogenerated file. Any custom changes will be overwritten!\n"; | |
346 | ||
347 | # Get enabled providers. | |
348 | my @enabled_providers = &IDS::get_enabled_providers(); | |
349 | ||
350 | # Loop through the array of enabled providers. | |
351 | foreach my $provider (@enabled_providers) { | |
352 | # Hash to store the used-enabled and disabled sids. | |
353 | my %enabled_disabled_sids; | |
354 | ||
355 | # Generate modified sids file name for the current processed provider. | |
5d523e41 | 356 | my $providers_modified_sids_file = &IDS::get_oinkmaster_provider_modified_sids_file($provider); |
15832b10 SS |
357 | |
358 | # Check if a modified sids file for this provider exists. | |
359 | if (-f $providers_modified_sids_file) { | |
360 | # Read-in the file for enabled/disabled sids. | |
361 | %enabled_disabled_sids = &read_enabled_disabled_sids_file($providers_modified_sids_file); | |
362 | } | |
363 | ||
364 | # Loop through the hash of idsrules. | |
365 | foreach my $rulefile (keys %idsrules) { | |
366 | # Split the rulefile to get the vendor. | |
367 | my @filename_parts = split(/-/, $rulefile); | |
368 | ||
369 | # Assign rulefile vendor. | |
370 | my $rulefile_vendor = @filename_parts[0]; | |
371 | ||
372 | # Skip the rulefile if the vendor is not our current processed provider. | |
373 | next unless ($rulefile_vendor eq $provider); | |
374 | ||
375 | # Loop through the single rules of the rulefile. | |
376 | foreach my $sid (keys %{$idsrules{$rulefile}}) { | |
377 | # Skip the current sid if it is not numeric. | |
378 | next unless ($sid =~ /\d+/ ); | |
379 | ||
380 | # Check if there exists a key in the cgiparams hash for this sid. | |
381 | if (exists($cgiparams{$sid})) { | |
382 | # Look if the rule is disabled. | |
383 | if ($idsrules{$rulefile}{$sid}{'State'} eq "off") { | |
384 | # Check if the state has been set to 'on'. | |
385 | if ($cgiparams{$sid} eq "on") { | |
386 | # Add/Modify the sid to/in the enabled_disabled_sids hash. | |
387 | $enabled_disabled_sids{$sid} = "enabled"; | |
388 | ||
389 | # Drop item from cgiparams hash. | |
390 | delete $cgiparams{$rulefile}{$sid}; | |
391 | } | |
392 | } | |
393 | } else { | |
394 | # Look if the rule is enabled. | |
395 | if ($idsrules{$rulefile}{$sid}{'State'} eq "on") { | |
396 | # Check if the state is 'on' and should be disabled. | |
397 | # In this case there is no entry | |
398 | # for the sid in the cgiparams hash. | |
399 | # Add/Modify it to/in the enabled_disabled_sids hash. | |
400 | $enabled_disabled_sids{$sid} = "disabled"; | |
298723b9 SS |
401 | |
402 | # Drop item from cgiparams hash. | |
60333473 | 403 | delete $cgiparams{$rulefile}{$sid}; |
298723b9 SS |
404 | } |
405 | } | |
298723b9 SS |
406 | } |
407 | } | |
298723b9 | 408 | |
15832b10 SS |
409 | # Check if the hash for enabled/disabled sids contains any entries. |
410 | if (%enabled_disabled_sids) { | |
411 | # Open providers modified sids file for writing. | |
412 | open(PROVIDER_MOD_FILE, ">$providers_modified_sids_file") or die "Could not write to $providers_modified_sids_file. $!\n"; | |
413 | ||
414 | # Write header to the files. | |
415 | print PROVIDER_MOD_FILE "#Autogenerated file. Any custom changes will be overwritten!\n"; | |
416 | ||
417 | # Loop through the hash. | |
418 | foreach my $sid (keys %enabled_disabled_sids) { | |
419 | # Check if the sid is enabled. | |
420 | if ($enabled_disabled_sids{$sid} eq "enabled") { | |
421 | # Print the sid to the enabled_sids file. | |
422 | print PROVIDER_MOD_FILE "enablesid $sid\n"; | |
423 | # Check if the sid is disabled. | |
424 | } elsif ($enabled_disabled_sids{$sid} eq "disabled") { | |
425 | # Print the sid to the disabled_sids file. | |
426 | print PROVIDER_MOD_FILE "disablesid $sid\n"; | |
427 | # Something strange happende - skip the current sid. | |
428 | } else { | |
429 | next; | |
430 | } | |
d2212836 | 431 | } |
15832b10 SS |
432 | |
433 | # Close file handle for the providers modified sids file. | |
434 | close(PROVIDER_MOD_FILE); | |
435 | ||
436 | # Add the file to the oinkmasters include file. | |
437 | print OINKM_INCL_FILE "include $providers_modified_sids_file\n"; | |
37659505 SS |
438 | } |
439 | } | |
298723b9 | 440 | |
15832b10 SS |
441 | # Close the file handle after writing. |
442 | close(OINKM_INCL_FILE); | |
e5738079 | 443 | |
ddaf8ae1 SS |
444 | # Handle enabled / disabled rulefiles. |
445 | # | |
ddaf8ae1 SS |
446 | |
447 | # Loop through the array of enabled providers. | |
448 | foreach my $provider(@enabled_providers) { | |
449 | # Array to store the rulefiles which belong to the current processed provider. | |
450 | my @provider_rulefiles = (); | |
451 | ||
452 | # Loop through the array of enabled rulefiles. | |
453 | foreach my $rulesfile (@enabled_rulefiles) { | |
454 | # Split the rulefile name. | |
455 | my @filename_parts = split(/-/, "$rulesfile"); | |
456 | ||
457 | # Assign vendor name for easy processings. | |
458 | my $vendor = @filename_parts[0]; | |
459 | ||
460 | # Check if the rulesvendor is our current processed enabled provider. | |
461 | if ("$vendor" eq "$provider") { | |
462 | # Add the rulesfile to the array of provider rulesfiles. | |
463 | push(@provider_rulefiles, $rulesfile); | |
464 | } | |
465 | ||
466 | # Check if any rulesfiles have been found for this provider. | |
467 | if (@provider_rulefiles) { | |
468 | # Call function and write the providers used rulesfile file. | |
469 | &IDS::write_used_provider_rulefiles_file($provider, @provider_rulefiles); | |
470 | } | |
471 | } | |
472 | } | |
473 | ||
b02e30fd | 474 | # Call function to generate and write the used rulefiles file. |
ddaf8ae1 | 475 | &IDS::write_main_used_rulefiles_file(@enabled_providers); |
52599865 | 476 | |
27760092 | 477 | # Lock the webpage and print message. |
5bd8940d | 478 | &working_notice("$Lang::tr{'ids apply ruleset changes'}"); |
27760092 | 479 | |
52599865 | 480 | # Call oinkmaster to alter the ruleset. |
27760092 SS |
481 | &IDS::oinkmaster(); |
482 | ||
e2e7880d | 483 | # Check if the IDS is running. |
5a28e721 | 484 | if(&IDS::ids_is_running()) { |
af8e5145 SS |
485 | # Check if a restart of suricata is required. |
486 | if ($suricata_restart_required) { | |
487 | # Call suricatactrl to perform the restart. | |
488 | &IDS::call_suricatactrl("restart"); | |
489 | } else { | |
490 | # Call suricatactrl to perform a reload. | |
491 | &IDS::call_suricatactrl("reload"); | |
492 | } | |
e2e7880d SS |
493 | } |
494 | ||
27760092 SS |
495 | # Reload page. |
496 | &reload(); | |
52599865 SS |
497 | |
498 | # Download new ruleset. | |
5fd2e9d6 | 499 | } elsif ($cgiparams{'RULESET'} eq $Lang::tr{'update ruleset'}) { |
43263ea6 SS |
500 | # Check if the red device is active. |
501 | unless (-e "${General::swroot}/red/active") { | |
013274d7 | 502 | $errormessage = "$Lang::tr{'could not download latest updates'} - $Lang::tr{'system is offline'}"; |
43263ea6 | 503 | } |
52599865 | 504 | |
3983aebd | 505 | # Check if enought free disk space is availabe. |
434001d0 SS |
506 | if(&IDS::checkdiskspace()) { |
507 | $errormessage = "$Lang::tr{'not enough disk space'}"; | |
508 | } | |
52599865 | 509 | |
43263ea6 SS |
510 | # Check if any errors happend. |
511 | unless ($errormessage) { | |
27760092 SS |
512 | # Lock the webpage and print notice about downloading |
513 | # a new ruleset. | |
5bd8940d | 514 | &working_notice("$Lang::tr{'ids download new ruleset'}"); |
3983aebd | 515 | |
43263ea6 | 516 | # Call subfunction to download the ruleset. |
434001d0 SS |
517 | if(&IDS::downloadruleset()) { |
518 | $errormessage = $Lang::tr{'could not download latest updates'}; | |
8f22237b | 519 | |
3983aebd | 520 | # Call function to store the errormessage. |
434001d0 | 521 | &IDS::_store_error_message($errormessage); |
52599865 | 522 | |
3983aebd SS |
523 | # Preform a reload of the page. |
524 | &reload(); | |
525 | } else { | |
526 | # Call subfunction to launch oinkmaster. | |
527 | &IDS::oinkmaster(); | |
43263ea6 | 528 | |
e2e7880d | 529 | # Check if the IDS is running. |
5a28e721 | 530 | if(&IDS::ids_is_running()) { |
e2e7880d SS |
531 | # Call suricatactrl to perform a reload. |
532 | &IDS::call_suricatactrl("reload"); | |
533 | } | |
534 | ||
3983aebd SS |
535 | # Perform a reload of the page. |
536 | &reload(); | |
537 | } | |
52599865 | 538 | } |
5bd8940d | 539 | # Save IDS settings. |
e0bfd338 | 540 | } elsif ($cgiparams{'IDS'} eq $Lang::tr{'save'}) { |
bbb6efae SS |
541 | my %oldidssettings; |
542 | my $reload_page; | |
ebdd0f9a | 543 | my $monitored_zones = 0; |
bbb6efae SS |
544 | |
545 | # Read-in current (old) IDS settings. | |
b02e30fd | 546 | &General::readhash("$IDS::ids_settings_file", \%oldidssettings); |
bbb6efae | 547 | |
a232b58c | 548 | # Prevent form name from been stored in conf file. |
e0bfd338 | 549 | delete $cgiparams{'IDS'}; |
a232b58c | 550 | |
ebdd0f9a SS |
551 | # Check if the IDS should be enabled. |
552 | if ($cgiparams{'ENABLE_IDS'} eq "on") { | |
4b6cf2a5 SS |
553 | # Get enabled providers. |
554 | my @enabled_providers = &IDS::get_enabled_providers(); | |
555 | ||
ebdd0f9a | 556 | # Check if any ruleset is available. Otherwise abort and display an error. |
4b6cf2a5 SS |
557 | unless(@enabled_providers) { |
558 | $errormessage = $Lang::tr{'ids no enabled ruleset provider'}; | |
ebdd0f9a SS |
559 | } |
560 | ||
561 | # Loop through the array of available interfaces. | |
562 | foreach my $zone (@network_zones) { | |
563 | # Convert interface name into upper case. | |
564 | my $zone_upper = uc($zone); | |
565 | ||
566 | # Check if the IDS is enabled for this interaces. | |
567 | if ($cgiparams{"ENABLE_IDS_$zone_upper"}) { | |
568 | # Increase count. | |
569 | $monitored_zones++; | |
570 | } | |
571 | } | |
572 | ||
573 | # Check if at least one zone should be monitored, or show an error. | |
574 | unless ($monitored_zones >= 1) { | |
575 | $errormessage = $Lang::tr{'ids no network zone'}; | |
576 | } | |
577 | } | |
578 | ||
a232b58c SS |
579 | # Go on if there are no error messages. |
580 | if (!$errormessage) { | |
581 | # Store settings into settings file. | |
b02e30fd | 582 | &General::writehash("$IDS::ids_settings_file", \%cgiparams); |
a9a91e5f | 583 | } |
8d2f6b0b | 584 | |
77351a6b SS |
585 | # Check if the the automatic rule update hass been touched. |
586 | if($cgiparams{'AUTOUPDATE_INTERVAL'} ne $oldidssettings{'AUTOUPDATE_INTERVAL'}) { | |
587 | # Call suricatactrl to set the new interval. | |
588 | &IDS::call_suricatactrl("cron", $cgiparams{'AUTOUPDATE_INTERVAL'}); | |
589 | } | |
590 | ||
8d2f6b0b | 591 | # Generate file to store the home net. |
b02e30fd | 592 | &IDS::generate_home_net_file(); |
e2e7880d | 593 | |
a40ee6b9 SS |
594 | # Generate file to the store the DNS servers. |
595 | &IDS::generate_dns_servers_file(); | |
596 | ||
e698090e SS |
597 | # Generate file to store the HTTP ports. |
598 | &IDS::generate_http_ports_file(); | |
599 | ||
74cc8f5a | 600 | # Write the modify sid's file and pass the taken ruleaction. |
81bae51f | 601 | &IDS::write_modify_sids_file(); |
bbb6efae | 602 | |
01d02eb6 SS |
603 | # Check if "MONITOR_TRAFFIC_ONLY" has been changed. |
604 | if($cgiparams{'MONITOR_TRAFFIC_ONLY'} ne $oldidssettings{'MONITOR_TRAFFIC_ONLY'}) { | |
bbb6efae | 605 | # Check if a ruleset exists. |
2f252efa | 606 | if (%used_providers) { |
bbb6efae | 607 | # Lock the webpage and print message. |
5bd8940d | 608 | &working_notice("$Lang::tr{'ids working'}"); |
bbb6efae SS |
609 | |
610 | # Call oinkmaster to alter the ruleset. | |
611 | &IDS::oinkmaster(); | |
612 | ||
613 | # Set reload_page to "True". | |
614 | $reload_page="True"; | |
615 | } | |
616 | } | |
617 | ||
e2e7880d SS |
618 | # Check if the IDS currently is running. |
619 | if(&IDS::ids_is_running()) { | |
620 | # Check if ENABLE_IDS is set to on. | |
621 | if($cgiparams{'ENABLE_IDS'} eq "on") { | |
622 | # Call suricatactrl to perform a reload of suricata. | |
623 | &IDS::call_suricatactrl("reload"); | |
624 | } else { | |
625 | # Call suricatactrl to stop suricata. | |
626 | &IDS::call_suricatactrl("stop"); | |
627 | } | |
628 | } else { | |
629 | # Call suricatactrl to start suricata. | |
630 | &IDS::call_suricatactrl("start"); | |
631 | } | |
bbb6efae SS |
632 | |
633 | # Check if the page should be reloaded. | |
634 | if ($reload_page) { | |
635 | # Perform a reload of the page. | |
636 | &reload(); | |
637 | } | |
4c067847 | 638 | |
9bf260de SS |
639 | # Toggle Enable/Disable autoupdate for a provider |
640 | } elsif ($cgiparams{'AUTOUPDATE'} eq $Lang::tr{'toggle enable disable'}) { | |
641 | my %used_providers = (); | |
642 | ||
643 | # Only go further, if an ID has been passed. | |
644 | if ($cgiparams{'ID'}) { | |
645 | # Assign the given ID. | |
646 | my $id = $cgiparams{'ID'}; | |
647 | ||
648 | # Undef the given ID. | |
649 | undef($cgiparams{'ID'}); | |
650 | ||
651 | # Read-in providers settings file. | |
652 | &General::readhasharray($IDS::providers_settings_file, \%used_providers); | |
653 | ||
654 | # Grab the configured status of the corresponding entry. | |
655 | my $status_autoupdate = $used_providers{$id}[2]; | |
656 | ||
657 | # Switch the status. | |
658 | if ($status_autoupdate eq "disabled") { | |
659 | $status_autoupdate = "enabled"; | |
660 | } else { | |
661 | $status_autoupdate = "disabled"; | |
662 | } | |
663 | ||
664 | # Modify the status of the existing entry. | |
665 | $used_providers{$id} = ["$used_providers{$id}[0]", "$used_providers{$id}[1]", "$status_autoupdate", "$used_providers{$id}[3]"]; | |
666 | ||
667 | # Write the changed hash to the providers settings file. | |
668 | &General::writehasharray($IDS::providers_settings_file, \%used_providers); | |
669 | } | |
670 | ||
671 | # Add/Edit a provider to the list of used providers. | |
672 | # | |
4c067847 | 673 | } elsif (($cgiparams{'PROVIDERS'} eq "$Lang::tr{'add'}") || ($cgiparams{'PROVIDERS'} eq "$Lang::tr{'update'}")) { |
aba3cbe5 SS |
674 | my %used_providers = (); |
675 | ||
676 | # Read-in providers settings file. | |
677 | &General::readhasharray("$IDS::providers_settings_file", \%used_providers); | |
678 | ||
4c067847 SS |
679 | # Assign some nice human-readable values. |
680 | my $provider = $cgiparams{'PROVIDER'}; | |
681 | my $subscription_code = $cgiparams{'SUBSCRIPTION_CODE'}; | |
bb4c30c6 SS |
682 | my $status_autoupdate; |
683 | ||
684 | # Handle autoupdate checkbox. | |
685 | if ($cgiparams{'ENABLE_AUTOUPDATE'} eq "on") { | |
686 | $status_autoupdate = "enabled"; | |
687 | } else { | |
688 | $status_autoupdate = "disabled"; | |
689 | } | |
4c067847 SS |
690 | |
691 | # Check if we are going to add a new provider. | |
692 | if ($cgiparams{'PROVIDERS'} eq "$Lang::tr{'add'}") { | |
693 | # Loop through the hash of used providers. | |
694 | foreach my $id ( keys %used_providers) { | |
695 | # Check if the choosen provider is already in use. | |
696 | if ($used_providers{$id}[0] eq "$provider") { | |
4c067847 | 697 | # Assign error message. |
1fa18733 | 698 | $errormessage = "$Lang::tr{'ids the choosen provider is already in use'}"; |
4c067847 SS |
699 | } |
700 | } | |
701 | } | |
702 | ||
703 | # Check if the provider requires a subscription code. | |
704 | if ($IDS::Ruleset::Providers{$provider}{'requires_subscription'} eq "True") { | |
705 | # Check if an subscription code has been provided. | |
706 | if ($subscription_code) { | |
707 | # Check if the code contains unallowed chars. | |
708 | unless ($subscription_code =~ /^[a-z0-9]+$/) { | |
709 | $errormessage = $Lang::tr{'invalid input for subscription code'}; | |
710 | } | |
711 | } else { | |
712 | # Print an error message, that an subsription code is required for this | |
713 | # provider. | |
714 | $errormessage = $Lang::tr{'ids subscription code required'}; | |
715 | } | |
716 | } | |
717 | ||
718 | # Go further if there was no error. | |
719 | if ($errormessage eq '') { | |
720 | my $id; | |
721 | my $status; | |
722 | ||
723 | # Check if we should edit an existing entry and got an ID. | |
724 | if (($cgiparams{'PROVIDERS'} eq $Lang::tr{'update'}) && ($cgiparams{'ID'})) { | |
725 | # Assin the provided id. | |
726 | $id = $cgiparams{'ID'}; | |
727 | ||
728 | # Undef the given ID. | |
729 | undef($cgiparams{'ID'}); | |
730 | ||
731 | # Grab the configured status of the corresponding entry. | |
732 | $status = $used_providers{$id}[3]; | |
733 | } else { | |
734 | # Each newly added entry automatically should be enabled. | |
735 | $status = "enabled"; | |
736 | ||
737 | # Generate the ID for the new entry. | |
738 | # | |
739 | # Sort the keys by their ID and store them in an array. | |
740 | my @keys = sort { $a <=> $b } keys %used_providers; | |
741 | ||
742 | # Reverse the key array. | |
743 | my @reversed = reverse(@keys); | |
744 | ||
745 | # Obtain the last used id. | |
746 | my $last_id = @reversed[0]; | |
747 | ||
748 | # Increase the last id by one and use it as id for the new entry. | |
749 | $id = ++$last_id; | |
750 | } | |
751 | ||
752 | # Add/Modify the entry to/in the used providers hash.. | |
753 | $used_providers{$id} = ["$provider", "$subscription_code", "$status_autoupdate", "$status"]; | |
754 | ||
755 | # Write the changed hash to the providers settings file. | |
756 | &General::writehasharray($IDS::providers_settings_file, \%used_providers); | |
757 | ||
b734df0e SS |
758 | # Check if a new provider will be added. |
759 | if ($cgiparams{'PROVIDERS'} eq $Lang::tr{'add'}) { | |
760 | # Lock the webpage and print notice about downloading | |
761 | # a new ruleset. | |
762 | &working_notice("$Lang::tr{'ids working'}"); | |
763 | ||
764 | # Download the ruleset. | |
765 | &IDS::downloadruleset($provider); | |
766 | ||
767 | # Extract the ruleset | |
768 | &IDS::extractruleset($provider); | |
769 | ||
770 | # Move the ruleset. | |
771 | &IDS::move_tmp_ruleset(); | |
772 | ||
773 | # Cleanup temporary directory. | |
774 | &IDS::cleanup_tmp_directory(); | |
775 | ||
ddaf8ae1 SS |
776 | # Create new empty file for used rulefiles |
777 | # for this provider. | |
778 | &IDS::write_used_provider_rulefiles_file($provider); | |
779 | ||
b734df0e SS |
780 | # Perform a reload of the page. |
781 | &reload(); | |
782 | } | |
783 | ||
4c067847 SS |
784 | # Undefine providers flag. |
785 | undef($cgiparams{'PROVIDERS'}); | |
786 | } | |
73eb03a3 SS |
787 | |
788 | ## Toggle Enabled/Disabled for an existing provider. | |
789 | # | |
790 | } elsif ($cgiparams{'PROVIDERS'} eq $Lang::tr{'toggle enable disable'}) { | |
791 | my %used_providers = (); | |
792 | ||
793 | # Only go further, if an ID has been passed. | |
794 | if ($cgiparams{'ID'}) { | |
795 | # Assign the given ID. | |
796 | my $id = $cgiparams{'ID'}; | |
797 | ||
798 | # Undef the given ID. | |
799 | undef($cgiparams{'ID'}); | |
800 | ||
801 | # Read-in file which contains the provider settings. | |
802 | &General::readhasharray($IDS::providers_settings_file, \%used_providers); | |
803 | ||
804 | # Grab the configured status of the corresponding entry. | |
805 | my $status = $used_providers{$id}[3]; | |
806 | ||
807 | # Switch the status. | |
808 | if ($status eq "enabled") { | |
809 | $status = "disabled"; | |
810 | } else { | |
811 | $status = "enabled"; | |
812 | } | |
813 | ||
814 | # Modify the status of the existing entry. | |
815 | $used_providers{$id} = ["$used_providers{$id}[0]", "$used_providers{$id}[1]", "$used_providers{$id}[2]", "$status"]; | |
816 | ||
817 | # Write the changed hash to the providers settings file. | |
818 | &General::writehasharray($IDS::providers_settings_file, \%used_providers); | |
819 | ||
a2b4488a SS |
820 | # Get all enabled providers. |
821 | my @enabled_providers = &IDS::get_enabled_providers(); | |
822 | ||
823 | # Write the main providers include file. | |
824 | &IDS::write_main_used_rulefiles_file(@enabled_providers); | |
825 | ||
73eb03a3 | 826 | # Check if the IDS is running. |
a2b4488a SS |
827 | if(&IDS::ids_is_running()) { |
828 | # Gather the amount of enabled providers (elements in the array). | |
829 | my $amount = @enabled_providers; | |
830 | ||
831 | # Check if there are still enabled ruleset providers. | |
832 | if ($amount >= 1) { | |
833 | # Call suricatactrl to perform a restart. | |
834 | &IDS::call_suricatactrl("restart"); | |
835 | ||
836 | # No active ruleset provider, suricata has to be stopped. | |
837 | } else { | |
838 | # Stop suricata. | |
839 | &IDS::call_suricatactrl("stop"); | |
840 | } | |
841 | } | |
73eb03a3 SS |
842 | |
843 | # Undefine providers flag. | |
844 | undef($cgiparams{'PROVIDERS'}); | |
845 | } | |
846 | ||
847 | ## Remove provider from the list of used providers. | |
848 | # | |
849 | } elsif ($cgiparams{'PROVIDERS'} eq $Lang::tr{'remove'}) { | |
850 | my %used_providers = (); | |
851 | ||
852 | # Read-in provider settings file. | |
853 | &General::readhasharray($IDS::providers_settings_file, \%used_providers); | |
854 | ||
2fded6d2 SS |
855 | # Grab the provider name bevore deleting it from hash. |
856 | my $provider = $used_providers{$cgiparams{'ID'}}[0]; | |
857 | ||
73eb03a3 SS |
858 | # Drop entry from the hash. |
859 | delete($used_providers{$cgiparams{'ID'}}); | |
860 | ||
861 | # Undef the given ID. | |
862 | undef($cgiparams{'ID'}); | |
863 | ||
864 | # Write the changed hash to the provide settings file. | |
865 | &General::writehasharray($IDS::providers_settings_file, \%used_providers); | |
866 | ||
106f00bd SS |
867 | # Lock the webpage and print message. |
868 | &working_notice("$Lang::tr{'ids apply ruleset changes'}"); | |
869 | ||
2fded6d2 SS |
870 | # Drop the stored ruleset file. |
871 | &IDS::drop_dl_rulesfile($provider); | |
872 | ||
873 | # Get the name of the provider rulessets include file. | |
874 | my $provider_used_rulefile = &get_used_provider_rulesfile_file($provider); | |
875 | ||
876 | # Drop the file, it is not longer needed. | |
877 | unlink("$provider_used_rulefile"); | |
878 | ||
879 | # Regenerate ruleset. | |
880 | &IDS::oinkmaster(); | |
881 | ||
882 | # Gather all enabled providers. | |
883 | my @enabled_providers = &IDS::get_enabled_providers(); | |
884 | ||
885 | # Regenerate main providers include file. | |
886 | &IDS::write_main_used_rulefiles_file(@enabled_providers); | |
887 | ||
73eb03a3 | 888 | # Check if the IDS is running. |
2fded6d2 SS |
889 | if(&IDS::ids_is_running()) { |
890 | # Get amount of enabled providers. | |
891 | my $amount = @enabled_providers; | |
892 | ||
893 | # Check if at least one enabled provider remains. | |
894 | if ($amount >= 1) { | |
895 | # Call suricatactrl to perform a reload. | |
896 | &IDS::call_suricatactrl("restart"); | |
897 | ||
898 | # Stop suricata if no enabled provider remains. | |
899 | } else { | |
900 | # Call suricatactrel to perform the stop. | |
901 | &IDS::call_suricatactrl("stop"); | |
902 | } | |
903 | } | |
73eb03a3 SS |
904 | |
905 | # Undefine providers flag. | |
906 | undef($cgiparams{'PROVIDERS'}); | |
106f00bd SS |
907 | |
908 | # Reload page. | |
909 | &reload(); | |
ac1cfefa MT |
910 | } |
911 | ||
ac1cfefa MT |
912 | &Header::openpage($Lang::tr{'intrusion detection system'}, 1, ''); |
913 | ||
2bbe6ede | 914 | &Header::openbigbox('100%', 'left', '', $errormessage); |
e0cec9fe | 915 | |
2bbe6ede | 916 | &show_display_error_message(); |
0d34a479 | 917 | |
2bbe6ede SS |
918 | if ($cgiparams{'RULESET'} eq "$Lang::tr{'ids customize ruleset'}" ) { |
919 | &show_customize_ruleset(); | |
2f252efa SS |
920 | } elsif ($cgiparams{'PROVIDERS'} ne "") { |
921 | &show_add_provider(); | |
2bbe6ede SS |
922 | } else { |
923 | &show_mainpage(); | |
924 | } | |
029b8ed2 | 925 | |
2bbe6ede SS |
926 | &Header::closebigbox(); |
927 | &Header::closepage(); | |
e0cec9fe | 928 | |
2bbe6ede SS |
929 | # |
930 | ## Tiny function to show if a error message happened. | |
931 | # | |
932 | sub show_display_error_message() { | |
933 | if ($errormessage) { | |
934 | &Header::openbox('100%', 'left', $Lang::tr{'error messages'}); | |
935 | print "<class name='base'>$errormessage\n"; | |
936 | print " </class>\n"; | |
937 | &Header::closebox(); | |
938 | } | |
939 | } | |
e0cec9fe | 940 | |
2bbe6ede SS |
941 | # |
942 | ## Function to display the main IDS page. | |
943 | # | |
944 | sub show_mainpage() { | |
aba3cbe5 | 945 | # Read-in idssettings and provider settings. |
2bbe6ede | 946 | &General::readhash("$IDS::ids_settings_file", \%idssettings); |
aba3cbe5 | 947 | &General::readhasharray("$IDS::providers_settings_file", \%used_providers); |
2bbe6ede SS |
948 | |
949 | # If no autoupdate intervall has been configured yet, set default value. | |
77351a6b | 950 | unless(exists($idssettings{'AUTOUPDATE_INTERVAL'})) { |
2bbe6ede | 951 | # Set default to "weekly". |
77351a6b | 952 | $idssettings{'AUTOUPDATE_INTERVAL'} = 'weekly'; |
17726644 | 953 | } |
2bbe6ede SS |
954 | |
955 | # Read-in ignored hosts. | |
956 | &General::readhasharray("$IDS::settingsdir/ignored", \%ignored); | |
957 | ||
958 | $checked{'ENABLE_IDS'}{'off'} = ''; | |
959 | $checked{'ENABLE_IDS'}{'on'} = ''; | |
960 | $checked{'ENABLE_IDS'}{$idssettings{'ENABLE_IDS'}} = "checked='checked'"; | |
961 | $checked{'MONITOR_TRAFFIC_ONLY'}{'off'} = ''; | |
962 | $checked{'MONITOR_TRAFFIC_ONLY'}{'on'} = ''; | |
963 | $checked{'MONITOR_TRAFFIC_ONLY'}{$idssettings{'MONITOR_TRAFFIC_ONLY'}} = "checked='checked'"; | |
2bbe6ede SS |
964 | $selected{'AUTOUPDATE_INTERVAL'}{'off'} = ''; |
965 | $selected{'AUTOUPDATE_INTERVAL'}{'daily'} = ''; | |
966 | $selected{'AUTOUPDATE_INTERVAL'}{'weekly'} = ''; | |
2f252efa | 967 | $selected{'AUTOUPDATE_INTERVAL'}{$idssettings{'AUTOUPDATE_INTERVAL'}} = "selected='selected'"; |
17726644 | 968 | |
2bbe6ede SS |
969 | # Draw current state of the IDS |
970 | &Header::openbox('100%', 'left', $Lang::tr{'intrusion detection system'}); | |
1504a375 | 971 | |
2bbe6ede SS |
972 | # Check if the IDS is running and obtain the process-id. |
973 | my $pid = &IDS::ids_is_running(); | |
87660964 | 974 | |
2bbe6ede SS |
975 | # Display some useful information, if suricata daemon is running. |
976 | if ($pid) { | |
977 | # Gather used memory. | |
978 | my $memory = &get_memory_usage($pid); | |
87660964 | 979 | |
2bbe6ede SS |
980 | print <<END; |
981 | <table width='95%' cellspacing='0' class='tbl'> | |
982 | <tr> | |
983 | <th bgcolor='$color{'color20'}' colspan='3' align='left'><strong>$Lang::tr{'intrusion detection'}</strong></th> | |
984 | </tr> | |
87660964 | 985 | |
2bbe6ede SS |
986 | <tr> |
987 | <td class='base'>$Lang::tr{'guardian daemon'}</td> | |
988 | <td align='center' colspan='2' width='75%' bgcolor='${Header::colourgreen}'><font color='white'><strong>$Lang::tr{'running'}</strong></font></td> | |
989 | </tr> | |
87660964 | 990 | |
2bbe6ede SS |
991 | <tr> |
992 | <td class='base'></td> | |
993 | <td bgcolor='$color{'color20'}' align='center'><strong>PID</strong></td> | |
994 | <td bgcolor='$color{'color20'}' align='center'><strong>$Lang::tr{'memory'}</strong></td> | |
995 | </tr> | |
87660964 | 996 | |
2bbe6ede SS |
997 | <tr> |
998 | <td class='base'></td> | |
999 | <td bgcolor='$color{'color22'}' align='center'>$pid</td> | |
1000 | <td bgcolor='$color{'color22'}' align='center'>$memory KB</td> | |
1001 | </tr> | |
1002 | </table> | |
87660964 | 1003 | END |
2bbe6ede SS |
1004 | } else { |
1005 | # Otherwise display a hint that the service is not launched. | |
1006 | print <<END; | |
1007 | <table width='95%' cellspacing='0' class='tbl'> | |
1008 | <tr> | |
1009 | <th bgcolor='$color{'color20'}' colspan='3' align='left'><strong>$Lang::tr{'intrusion detection'}</strong></th> | |
1010 | </tr> | |
87660964 | 1011 | |
2bbe6ede SS |
1012 | <tr> |
1013 | <td class='base'>$Lang::tr{'guardian daemon'}</td> | |
1014 | <td align='center' width='75%' bgcolor='${Header::colourred}'><font color='white'><strong>$Lang::tr{'stopped'}</strong></font></td> | |
1015 | </tr> | |
1016 | </table> | |
87660964 | 1017 | END |
2bbe6ede | 1018 | } |
87660964 | 1019 | |
2f252efa SS |
1020 | # Only show this area, if at least one ruleset provider is configured. |
1021 | if (%used_providers) { | |
674912fc | 1022 | |
2bbe6ede | 1023 | print <<END |
674912fc | 1024 | |
2bbe6ede | 1025 | <br><br><h2>$Lang::tr{'settings'}</h2> |
1286e0d4 | 1026 | |
2bbe6ede SS |
1027 | <form method='post' action='$ENV{'SCRIPT_NAME'}'> |
1028 | <table width='100%' border='0'> | |
1029 | <tr> | |
1030 | <td class='base' colspan='2'> | |
1031 | <input type='checkbox' name='ENABLE_IDS' $checked{'ENABLE_IDS'}{'on'}> $Lang::tr{'ids enable'} | |
1032 | </td> | |
cf02bf2f | 1033 | |
2bbe6ede SS |
1034 | <td class='base' colspan='2'> |
1035 | <input type='checkbox' name='MONITOR_TRAFFIC_ONLY' $checked{'MONITOR_TRAFFIC_ONLY'}{'on'}> $Lang::tr{'ids monitor traffic only'} | |
1036 | </td> | |
1037 | </tr> | |
1504a375 | 1038 | |
2bbe6ede SS |
1039 | <tr> |
1040 | <td><br><br></td> | |
1041 | <td><br><br></td> | |
1042 | <td><br><br></td> | |
1043 | <td><br><br></td> | |
1044 | </tr> | |
a4ccfcbb | 1045 | |
2bbe6ede SS |
1046 | <tr> |
1047 | <td colspan='4'><b>$Lang::tr{'ids monitored interfaces'}</b><br></td> | |
1048 | </tr> | |
a4ccfcbb | 1049 | |
2bbe6ede | 1050 | <tr> |
ac1cfefa MT |
1051 | END |
1052 | ; | |
1504a375 | 1053 | |
2bbe6ede SS |
1054 | # Loop through the array of available networks and print config options. |
1055 | foreach my $zone (@network_zones) { | |
1056 | my $checked_input; | |
1057 | my $checked_forward; | |
1504a375 | 1058 | |
2bbe6ede SS |
1059 | # Convert current zone name to upper case. |
1060 | my $zone_upper = uc($zone); | |
1504a375 | 1061 | |
2bbe6ede SS |
1062 | # Set zone name. |
1063 | my $zone_name = $zone; | |
53817b89 | 1064 | |
2bbe6ede SS |
1065 | # Dirty hack to get the correct language string for the red zone. |
1066 | if ($zone eq "red") { | |
1067 | $zone_name = "red1"; | |
1068 | } | |
53817b89 | 1069 | |
2bbe6ede SS |
1070 | # Grab checkbox status from settings hash. |
1071 | if ($idssettings{"ENABLE_IDS_$zone_upper"} eq "on") { | |
1072 | $checked_input = "checked = 'checked'"; | |
1073 | } | |
1504a375 | 1074 | |
2bbe6ede SS |
1075 | print "<td class='base' width='20%'>\n"; |
1076 | print "<input type='checkbox' name='ENABLE_IDS_$zone_upper' $checked_input>\n"; | |
1077 | print " $Lang::tr{'enabled on'}<font color='$colourhash{$zone}'> $Lang::tr{$zone_name}</font>\n"; | |
1078 | print "</td>\n"; | |
1079 | } | |
1b73b07e | 1080 | |
ac1cfefa | 1081 | print <<END |
2bbe6ede | 1082 | </tr> |
77351a6b SS |
1083 | |
1084 | <tr> | |
1085 | <td><br><br></td> | |
1086 | <td><br><br></td> | |
1087 | <td><br><br></td> | |
1088 | <td><br><br></td> | |
1089 | </tr> | |
1090 | ||
1091 | <tr> | |
1092 | <td colspan='4'><b>$Lang::tr{'ids automatic rules update'}</b></td> | |
1093 | </tr> | |
1094 | ||
1095 | <tr> | |
1096 | <td> | |
1097 | <select name='AUTOUPDATE_INTERVAL'> | |
1098 | <option value='off' $selected{'AUTOUPDATE_INTERVAL'}{'off'} >- $Lang::tr{'Disabled'} -</option> | |
1099 | <option value='daily' $selected{'AUTOUPDATE_INTERVAL'}{'daily'} >$Lang::tr{'Daily'}</option> | |
1100 | <option value='weekly' $selected{'AUTOUPDATE_INTERVAL'}{'weekly'} >$Lang::tr{'Weekly'}</option> | |
1101 | </select> | |
1102 | </td> | |
1103 | </tr> | |
2bbe6ede | 1104 | </table> |
1504a375 | 1105 | |
2bbe6ede | 1106 | <br><br> |
ea5c8eeb | 1107 | |
2bbe6ede SS |
1108 | <table width='100%'> |
1109 | <tr> | |
1110 | <td align='right'><input type='submit' name='IDS' value='$Lang::tr{'save'}' /></td> | |
1111 | </tr> | |
1112 | </table> | |
1113 | </form> | |
ea5c8eeb SS |
1114 | END |
1115 | ; | |
1116 | ||
2bbe6ede | 1117 | } |
cf02bf2f | 1118 | |
2bbe6ede | 1119 | &Header::closebox(); |
ea5c8eeb | 1120 | |
2f252efa SS |
1121 | # |
1122 | # Used Ruleset Providers section. | |
1123 | # | |
2bbe6ede | 1124 | &Header::openbox('100%', 'center', $Lang::tr{'ids ruleset settings'}); |
ea5c8eeb | 1125 | |
2f252efa SS |
1126 | print <<END; |
1127 | <table width='100%' border='0'> | |
1128 | <tr> | |
1129 | <td class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'ids provider'}</b></td> | |
1130 | <td class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'date'}</b></td> | |
1131 | <td class='base' bgcolor='$color{'color20'}' align='center'><b>$Lang::tr{'ids autoupdates'}</b></td> | |
1132 | <td class='base' bgcolor='$color{'color20'}'></td> | |
1133 | <td class='base' colspan='3' bgcolor='$color{'color20'}'></td> | |
1134 | </tr> | |
3e12c6e6 | 1135 | END |
2f252efa SS |
1136 | # Check if some providers has been configured. |
1137 | if (keys (%used_providers)) { | |
1138 | my $col = ""; | |
3e12c6e6 | 1139 | |
2f252efa | 1140 | # Loop through all entries of the hash. |
2f252efa SS |
1141 | foreach my $id (sort keys(%used_providers)) { |
1142 | # Assign data array positions to some nice variable names. | |
1143 | my $provider = $used_providers{$id}[0]; | |
1144 | my $provider_name = &get_provider_name($provider); | |
593abb35 | 1145 | my $rulesetdate = &IDS::get_ruleset_date($provider); |
2f252efa SS |
1146 | |
1147 | my $subscription_code = $used_providers{$id}[1]; | |
1148 | my $autoupdate_status = $used_providers{$id}[2]; | |
1149 | my $status = $used_providers{$id}[3]; | |
1150 | ||
1151 | # Check if the item number is even or not. | |
1152 | if ($id % 2) { | |
1153 | $col="bgcolor='$color{'color22'}'"; | |
1154 | } else { | |
1155 | $col="bgcolor='$color{'color20'}'"; | |
2bbe6ede | 1156 | } |
1504a375 | 1157 | |
2f252efa SS |
1158 | # Choose icons for the checkboxes. |
1159 | my $status_gif; | |
1160 | my $status_gdesc; | |
1161 | my $autoupdate_status_gif; | |
1162 | my $autoupdate_status_gdesc; | |
1504a375 | 1163 | |
2f252efa SS |
1164 | # Check if the status is enabled and select the correct image and description. |
1165 | if ($status eq 'enabled' ) { | |
1166 | $status_gif = 'on.gif'; | |
1167 | $status_gdesc = $Lang::tr{'click to disable'}; | |
1168 | } else { | |
1169 | $status_gif = 'off.gif'; | |
1170 | $status_gdesc = $Lang::tr{'click to enable'}; | |
1171 | } | |
1504a375 | 1172 | |
2f252efa SS |
1173 | # Check if the autoupdate status is enabled and select the correct image and description. |
1174 | if ($autoupdate_status eq 'enabled') { | |
1175 | $autoupdate_status_gif = 'on.gif'; | |
1176 | $autoupdate_status_gdesc = $Lang::tr{'click to disable'}; | |
1177 | } else { | |
1178 | $autoupdate_status_gif = 'off.gif'; | |
1179 | $autoupdate_status_gdesc = $Lang::tr{'click to enable'}; | |
1180 | } | |
2bbe6ede | 1181 | |
2f252efa SS |
1182 | print <<END; |
1183 | <tr> | |
1184 | <td width='33%' class='base' $col>$provider_name</td> | |
593abb35 | 1185 | <td width='30%' class='base' $col>$rulesetdate</td> |
2f252efa SS |
1186 | |
1187 | <td align='center' $col> | |
1188 | <form method='post' action='$ENV{'SCRIPT_NAME'}'> | |
1189 | <input type='hidden' name='AUTOUPDATE' value='$Lang::tr{'toggle enable disable'}' /> | |
1190 | <input type='image' name='$Lang::tr{'toggle enable disable'}' src='/images/$autoupdate_status_gif' alt='$autoupdate_status_gdesc' title='$autoupdate_status_gdesc' /> | |
1191 | <input type='hidden' name='ID' value='$id' /> | |
1192 | </form> | |
1193 | </td> | |
1194 | ||
1195 | <td align='center' $col> | |
7323c72d | 1196 | <form method='post' action='$ENV{'SCRIPT_NAME'}'> |
2f252efa SS |
1197 | <input type='hidden' name='PROVIDERS' value='$Lang::tr{'toggle enable disable'}'> |
1198 | <input type='image' name='$Lang::tr{'toggle enable disable'}' src='/images/$status_gif' alt='$status_gdesc' title='$status_gdesc'> | |
1199 | <input type='hidden' name='ID' value='$id'> | |
1200 | </form> | |
1201 | </td> | |
1202 | ||
1203 | <td align='center' $col> | |
1204 | <form method='post' action='$ENV{'SCRIPT_NAME'}'> | |
1205 | <input type='hidden' name='PROVIDERS' value='$Lang::tr{'edit'}'> | |
1206 | <input type='image' name='$Lang::tr{'edit'}' src='/images/edit.gif' alt='$Lang::tr{'edit'}' title='$Lang::tr{'edit'}'> | |
1207 | <input type='hidden' name='ID' value='$id'> | |
1208 | </form> | |
1209 | </td> | |
1504a375 | 1210 | |
2f252efa SS |
1211 | <td align='center' $col> |
1212 | <form method='post' name='$provider' action='$ENV{'SCRIPT_NAME'}'> | |
1213 | <input type='image' name='$Lang::tr{'remove'}' src='/images/delete.gif' title='$Lang::tr{'remove'}' alt='$Lang::tr{'remove'}'> | |
1214 | <input type='hidden' name='ID' value='$id'> | |
1215 | <input type='hidden' name='PROVIDERS' value='$Lang::tr{'remove'}'> | |
1216 | </form> | |
1217 | </td> | |
1218 | </tr> | |
ea5c8eeb | 1219 | END |
2bbe6ede | 1220 | } |
2f252efa SS |
1221 | |
1222 | } else { | |
1223 | # Print notice that currently no hosts are ignored. | |
1224 | print "<tr>\n"; | |
1225 | print "<td class='base' colspan='2'>$Lang::tr{'guardian no entries'}</td>\n"; | |
1226 | print "</tr>\n"; | |
1227 | } | |
1228 | ||
1229 | print "</table>\n"; | |
1230 | ||
1231 | # Section to add new elements or edit existing ones. | |
ea5c8eeb | 1232 | print <<END; |
2f252efa SS |
1233 | <br> |
1234 | <hr> | |
1235 | <br> | |
1504a375 | 1236 | |
2f252efa SS |
1237 | <div align='right'> |
1238 | <table width='100%'> | |
1239 | <form method='post' action='$ENV{'SCRIPT_NAME'}'> | |
1240 | <tr> | |
1241 | END | |
1242 | ||
1243 | # Only show this button if a ruleset provider is configured. | |
1244 | if (%used_providers) { | |
1245 | print "<input type='submit' name='RULESET' value='$Lang::tr{'ids customize ruleset'}'>\n"; | |
1246 | } | |
1247 | print <<END; | |
1248 | <input type='submit' name='PROVIDERS' value='$Lang::tr{'ids add provider'}'> | |
1249 | </tr> | |
1250 | </form> | |
2bbe6ede | 1251 | </table> |
2f252efa | 1252 | </div> |
ac1cfefa | 1253 | END |
ac1cfefa | 1254 | |
2bbe6ede | 1255 | &Header::closebox(); |
fbfdb241 | 1256 | |
2bbe6ede SS |
1257 | # |
1258 | # Whitelist / Ignorelist | |
1259 | # | |
1260 | &Header::openbox('100%', 'center', $Lang::tr{'ids ignored hosts'}); | |
b7e29743 | 1261 | |
2bbe6ede | 1262 | print <<END; |
b7e29743 SS |
1263 | <table width='100%'> |
1264 | <tr> | |
1265 | <td class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'ip address'}</b></td> | |
1266 | <td class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'remark'}</b></td> | |
1267 | <td class='base' colspan='3' bgcolor='$color{'color20'}'></td> | |
1268 | </tr> | |
1269 | END | |
1270 | # Check if some hosts have been added to be ignored. | |
1271 | if (keys (%ignored)) { | |
1272 | my $col = ""; | |
1273 | ||
1274 | # Loop through all entries of the hash. | |
1275 | while( (my $key) = each %ignored) { | |
1276 | # Assign data array positions to some nice variable names. | |
1277 | my $address = $ignored{$key}[0]; | |
1278 | my $remark = $ignored{$key}[1]; | |
1279 | my $status = $ignored{$key}[2]; | |
1280 | ||
1281 | # Check if the key (id) number is even or not. | |
1282 | if ($cgiparams{'ID'} eq $key) { | |
1283 | $col="bgcolor='${Header::colouryellow}'"; | |
1284 | } elsif ($key % 2) { | |
1285 | $col="bgcolor='$color{'color22'}'"; | |
1286 | } else { | |
1287 | $col="bgcolor='$color{'color20'}'"; | |
1288 | } | |
1289 | ||
1290 | # Choose icon for the checkbox. | |
1291 | my $gif; | |
1292 | my $gdesc; | |
1293 | ||
1294 | # Check if the status is enabled and select the correct image and description. | |
1295 | if ($status eq 'enabled' ) { | |
1296 | $gif = 'on.gif'; | |
1297 | $gdesc = $Lang::tr{'click to disable'}; | |
1298 | } else { | |
1299 | $gif = 'off.gif'; | |
1300 | $gdesc = $Lang::tr{'click to enable'}; | |
1301 | } | |
1302 | ||
1303 | print <<END; | |
1304 | <tr> | |
1305 | <td width='20%' class='base' $col>$address</td> | |
1306 | <td width='65%' class='base' $col>$remark</td> | |
1307 | ||
1308 | <td align='center' $col> | |
1309 | <form method='post' action='$ENV{'SCRIPT_NAME'}'> | |
2f252efa SS |
1310 | <input type='hidden' name='WHITELIST' value='$Lang::tr{'toggle enable disable'}'> |
1311 | <input type='image' name='$Lang::tr{'toggle enable disable'}' src='/images/$gif' alt='$gdesc' title='$gdesc'> | |
1312 | <input type='hidden' name='ID' value='$key'> | |
b7e29743 SS |
1313 | </form> |
1314 | </td> | |
1315 | ||
1316 | <td align='center' $col> | |
1317 | <form method='post' action='$ENV{'SCRIPT_NAME'}'> | |
2f252efa SS |
1318 | <input type='hidden' name='WHITELIST' value='$Lang::tr{'edit'}'> |
1319 | <input type='image' name='$Lang::tr{'edit'}' src='/images/edit.gif' alt='$Lang::tr{'edit'}' title='$Lang::tr{'edit'}'> | |
1320 | <input type='hidden' name='ID' value='$key'> | |
b7e29743 SS |
1321 | </form> |
1322 | </td> | |
1323 | ||
1324 | <td align='center' $col> | |
1325 | <form method='post' name='$key' action='$ENV{'SCRIPT_NAME'}'> | |
1326 | <input type='image' name='$Lang::tr{'remove'}' src='/images/delete.gif' title='$Lang::tr{'remove'}' alt='$Lang::tr{'remove'}'> | |
1327 | <input type='hidden' name='ID' value='$key'> | |
1328 | <input type='hidden' name='WHITELIST' value='$Lang::tr{'remove'}'> | |
1329 | </form> | |
2bbe6ede SS |
1330 | </td> |
1331 | </tr> | |
b7e29743 | 1332 | END |
2bbe6ede SS |
1333 | } |
1334 | } else { | |
1335 | # Print notice that currently no hosts are ignored. | |
1336 | print "<tr>\n"; | |
1337 | print "<td class='base' colspan='2'>$Lang::tr{'guardian no entries'}</td>\n"; | |
1338 | print "</tr>\n"; | |
b7e29743 | 1339 | } |
b7e29743 | 1340 | |
2bbe6ede | 1341 | print "</table>\n"; |
b7e29743 | 1342 | |
2bbe6ede | 1343 | # Section to add new elements or edit existing ones. |
b7e29743 | 1344 | print <<END; |
2bbe6ede SS |
1345 | <br> |
1346 | <hr> | |
1347 | <br> | |
1348 | ||
1349 | <div align='center'> | |
1350 | <table width='100%'> | |
b7e29743 SS |
1351 | END |
1352 | ||
2bbe6ede SS |
1353 | # Assign correct headline and button text. |
1354 | my $buttontext; | |
1355 | my $entry_address; | |
1356 | my $entry_remark; | |
b7e29743 | 1357 | |
2bbe6ede SS |
1358 | # Check if an ID (key) has been given, in this case an existing entry should be edited. |
1359 | if ($cgiparams{'ID'} ne '') { | |
1360 | $buttontext = $Lang::tr{'update'}; | |
1361 | print "<tr><td class='boldbase' colspan='3'><b>$Lang::tr{'update'}</b></td></tr>\n"; | |
b7e29743 | 1362 | |
2bbe6ede SS |
1363 | # Grab address and remark for the given key. |
1364 | $entry_address = $ignored{$cgiparams{'ID'}}[0]; | |
1365 | $entry_remark = $ignored{$cgiparams{'ID'}}[1]; | |
1366 | } else { | |
1367 | $buttontext = $Lang::tr{'add'}; | |
1368 | print "<tr><td class='boldbase' colspan='3'><b>$Lang::tr{'dnsforward add a new entry'}</b></td></tr>\n"; | |
1369 | } | |
b7e29743 SS |
1370 | |
1371 | print <<END; | |
2bbe6ede SS |
1372 | <form method='post' action='$ENV{'SCRIPT_NAME'}'> |
1373 | <input type='hidden' name='ID' value='$cgiparams{'ID'}'> | |
1374 | <tr> | |
1375 | <td width='30%'>$Lang::tr{'ip address'}: </td> | |
1376 | <td width='50%'><input type='text' name='IGNORE_ENTRY_ADDRESS' value='$entry_address' size='24' /></td> | |
b7e29743 | 1377 | |
2bbe6ede SS |
1378 | <td width='30%'>$Lang::tr{'remark'}: </td> |
1379 | <td wicth='50%'><input type='text' name=IGNORE_ENTRY_REMARK value='$entry_remark' size='24' /></td> | |
1380 | <td align='center' width='20%'><input type='submit' name='WHITELIST' value='$buttontext' /></td> | |
1381 | </tr> | |
1382 | </form> | |
1383 | </table> | |
1384 | </div> | |
b7e29743 SS |
1385 | END |
1386 | ||
2bbe6ede | 1387 | &Header::closebox(); |
fed57fe7 SS |
1388 | } |
1389 | ||
fed57fe7 SS |
1390 | # |
1391 | ## Function to show the customize ruleset section. | |
1392 | # | |
1393 | sub show_customize_ruleset() { | |
2bbe6ede SS |
1394 | ### Java Script ### |
1395 | print"<script>\n"; | |
1396 | ||
1397 | # Java script variable declaration for show and hide. | |
1398 | print"var show = \"$Lang::tr{'ids show'}\"\;\n"; | |
1399 | print"var hide = \"$Lang::tr{'ids hide'}\"\;\n"; | |
1400 | ||
1401 | print <<END | |
1402 | // Tiny java script function to show/hide the rules | |
1403 | // of a given category. | |
1404 | function showhide(tblname) { | |
1405 | \$("#" + tblname).toggle(); | |
1406 | ||
1407 | // Get current content of the span element. | |
1408 | var content = document.getElementById("span_" + tblname); | |
1409 | ||
1410 | if (content.innerHTML === show) { | |
1411 | content.innerHTML = hide; | |
1412 | } else { | |
1413 | content.innerHTML = show; | |
1414 | } | |
1415 | } | |
1416 | </script> | |
1417 | END | |
1418 | ; | |
87df37da | 1419 | &Header::openbox('100%', 'LEFT', "$Lang::tr{'intrusion detection system rules'}" ); |
80bcd4dd | 1420 | print"<form method='POST' action='$ENV{'SCRIPT_NAME'}'>\n"; |
ce0e83b3 | 1421 | |
80bcd4dd SS |
1422 | # Output display table for rule files |
1423 | print "<table width='100%'>\n"; | |
f7fcd1c0 | 1424 | |
80bcd4dd SS |
1425 | # Loop over each rule file |
1426 | foreach my $rulefile (sort keys(%idsrules)) { | |
1427 | my $rulechecked = ''; | |
3ffee04b | 1428 | |
80bcd4dd SS |
1429 | # Check if rule file is enabled |
1430 | if ($idsrules{$rulefile}{'Rulefile'}{'State'} eq 'on') { | |
1431 | $rulechecked = 'CHECKED'; | |
1432 | } | |
1433 | ||
0a1bba1a SS |
1434 | # Convert rulefile name into category name. |
1435 | my $categoryname = &_rulefile_to_category($rulefile); | |
1436 | ||
80bcd4dd SS |
1437 | # Table and rows for the rule files. |
1438 | print"<tr>\n"; | |
1439 | print"<td class='base' width='5%'>\n"; | |
1440 | print"<input type='checkbox' name='$rulefile' $rulechecked>\n"; | |
1441 | print"</td>\n"; | |
1442 | print"<td class='base' width='90%'><b>$rulefile</b></td>\n"; | |
1443 | print"<td class='base' width='5%' align='right'>\n"; | |
e0cec9fe | 1444 | print"<a href=\"javascript:showhide('$categoryname')\"><span id='span_$categoryname'>$Lang::tr{'ids show'}</span></a>\n"; |
80bcd4dd SS |
1445 | print"</td>\n"; |
1446 | print"</tr>\n"; | |
1447 | ||
1448 | # Rows which will be hidden per default and will contain the single rules. | |
0a1bba1a | 1449 | print"<tr style='display:none' id='$categoryname'>\n"; |
80bcd4dd | 1450 | print"<td colspan='3'>\n"; |
17726644 | 1451 | |
f7fcd1c0 | 1452 | # Local vars |
80bcd4dd SS |
1453 | my $lines; |
1454 | my $rows; | |
1455 | my $col; | |
f9c2147d | 1456 | |
80bcd4dd SS |
1457 | # New table for the single rules. |
1458 | print "<table width='100%'>\n"; | |
e5738079 | 1459 | |
80bcd4dd SS |
1460 | # Loop over rule file rules |
1461 | foreach my $sid (sort {$a <=> $b} keys(%{$idsrules{$rulefile}})) { | |
1462 | # Local vars | |
1463 | my $ruledefchecked = ''; | |
3ffee04b | 1464 | |
80bcd4dd SS |
1465 | # Skip rulefile itself. |
1466 | next if ($sid eq "Rulefile"); | |
f7fcd1c0 | 1467 | |
80bcd4dd SS |
1468 | # If 2 rules have been displayed, start a new row |
1469 | if (($lines % 2) == 0) { | |
1470 | print "</tr><tr>\n"; | |
1471 | ||
1472 | # Increase rows by once. | |
1473 | $rows++; | |
1474 | } | |
1475 | ||
1476 | # Colour lines. | |
1477 | if ($rows % 2) { | |
1478 | $col="bgcolor='$color{'color20'}'"; | |
1479 | } else { | |
1480 | $col="bgcolor='$color{'color22'}'"; | |
1481 | } | |
f7fcd1c0 | 1482 | |
80bcd4dd SS |
1483 | # Set rule state |
1484 | if ($idsrules{$rulefile}{$sid}{'State'} eq 'on') { | |
1485 | $ruledefchecked = 'CHECKED'; | |
1486 | } | |
1487 | ||
1488 | # Create rule checkbox and display rule description | |
1489 | print "<td class='base' width='5%' align='right' $col>\n"; | |
1490 | print "<input type='checkbox' NAME='$sid' $ruledefchecked>\n"; | |
1491 | print "</td>\n"; | |
1492 | print "<td class='base' width='45%' $col>$idsrules{$rulefile}{$sid}{'Description'}</td>"; | |
1493 | ||
1494 | # Increment rule count | |
1495 | $lines++; | |
f7fcd1c0 | 1496 | } |
3ffee04b | 1497 | |
80bcd4dd SS |
1498 | # If do not have a second rule for row, create empty cell |
1499 | if (($lines % 2) != 0) { | |
1500 | print "<td class='base'></td>"; | |
1501 | } | |
17726644 | 1502 | |
80bcd4dd SS |
1503 | # Close display table |
1504 | print "</tr></table></td></tr>"; | |
f7fcd1c0 SS |
1505 | } |
1506 | ||
1507 | # Close display table | |
80bcd4dd | 1508 | print "</table>"; |
17726644 | 1509 | |
9268cddf | 1510 | print <<END |
2999f1d2 CS |
1511 | <table width='100%'> |
1512 | <tr> | |
4efc8ccd SS |
1513 | <td width='100%' align='right'> |
1514 | <input type='submit' value='$Lang::tr{'fwhost back'}'> | |
1515 | <input type='submit' name='RULESET' value='$Lang::tr{'ids apply'}'> | |
1516 | </td> | |
2999f1d2 CS |
1517 | </tr> |
1518 | </table> | |
298723b9 | 1519 | </form> |
3ffee04b CS |
1520 | END |
1521 | ; | |
9268cddf MT |
1522 | &Header::closebox(); |
1523 | } | |
80bcd4dd SS |
1524 | } |
1525 | ||
2f252efa SS |
1526 | # |
1527 | ## Function to show section for add/edit a provider. | |
1528 | # | |
1529 | sub show_add_provider() { | |
aba3cbe5 | 1530 | my %used_providers = (); |
2f252efa SS |
1531 | my @subscription_providers; |
1532 | ||
aba3cbe5 SS |
1533 | # Read -in providers settings file. |
1534 | &General::readhasharray("$IDS::providers_settings_file", \%used_providers); | |
1535 | ||
2f252efa SS |
1536 | # Get all supported ruleset providers. |
1537 | my @ruleset_providers = &IDS::get_ruleset_providers(); | |
1538 | ||
1539 | ### Java Script ### | |
1540 | print "<script>\n"; | |
1541 | ||
1542 | # Generate Java Script Object which contains the URL of the providers. | |
1543 | print "\t// Object, which contains the webpages of the ruleset providers.\n"; | |
1544 | print "\tvar url = {\n"; | |
1545 | ||
1546 | # Loop through the array of supported providers. | |
1547 | foreach my $provider (@ruleset_providers) { | |
1548 | # Check if the provider requires a subscription. | |
1549 | if ($IDS::Ruleset::Providers{$provider}{'requires_subscription'} eq "True") { | |
1550 | # Add the provider to the array of subscription_providers. | |
1551 | push(@subscription_providers, $provider); | |
1552 | } | |
1553 | ||
1554 | # Grab the URL for the provider. | |
1555 | my $url = $IDS::Ruleset::Providers{$provider}{'website'}; | |
1556 | ||
1557 | # Print the URL to the Java Script Object. | |
1558 | print "\t\t$provider: \"$url\"\,\n"; | |
1559 | } | |
1560 | ||
1561 | # Close the Java Script Object declaration. | |
1562 | print "\t}\;\n\n"; | |
1563 | ||
1564 | # Generate Java Script Array which contains the provider that requires a subscription. | |
1565 | my $line = ""; | |
1566 | $line = join("', '", @subscription_providers); | |
1567 | ||
1568 | print "\t// Array which contains the providers that requires a subscription.\n"; | |
1569 | print "\tsubscription_provider = ['$line']\;\n\n"; | |
1570 | ||
1571 | print <<END | |
1572 | // Java Script function to swap the text input field for | |
1573 | // entering a subscription code. | |
1574 | var update_provider = function() { | |
1575 | if(inArray(\$('#PROVIDER').val(), subscription_provider)) { | |
1576 | \$('.subscription_code').show(); | |
1577 | } else { | |
1578 | \$('.subscription_code').hide(); | |
1579 | } | |
1580 | ||
1581 | // Call function to change the website url. | |
1582 | change_url(\$('#PROVIDER').val()); | |
1583 | }; | |
1584 | ||
1585 | // Java Script function to check if a given value is part of | |
1586 | // an array. | |
1587 | function inArray(value,array) { | |
1588 | var count=array.length; | |
1589 | ||
1590 | for(var i=0;i<count;i++) { | |
1591 | if(array[i]===value){ | |
1592 | return true; | |
1593 | } | |
1594 | } | |
1595 | ||
1596 | return false; | |
1597 | } | |
1598 | ||
1599 | // Tiny function to change the website url based on the selected element in the "PROVIDERS" | |
1600 | // dropdown menu. | |
1601 | function change_url(provider) { | |
1602 | // Get and change the href to the corresponding url. | |
1603 | document.getElementById("website").href = url[provider]; | |
1604 | } | |
1605 | ||
1606 | // JQuery function to call corresponding function when | |
1607 | // the ruleset provider is changed or the page is loaded for showing/hiding | |
1608 | // the subscription_code area. | |
1609 | \$(document).ready(function() { | |
1610 | \$('#PROVIDER').change(update_provider); | |
1611 | update_provider(); | |
1612 | }); | |
1613 | ||
1614 | </script> | |
1615 | END | |
1616 | ; | |
1617 | ||
1618 | &Header::openbox('100%', 'center', $Lang::tr{'ids provider settings'}); | |
1619 | ||
1620 | # Check if an existing provider should be edited. | |
1621 | if($cgiparams{'PROVIDERS'} eq "$Lang::tr{'edit'}") { | |
1622 | # Check if autoupdate is enabled for this provider. | |
bb4c30c6 | 1623 | if ($used_providers{$cgiparams{'ID'}}[2] eq "enabled") { |
2f252efa SS |
1624 | # Set the checkbox to be checked. |
1625 | $checked{'ENABLE_AUTOUPDATE'} = "checked='checked'"; | |
1626 | } | |
1627 | } elsif ($cgiparams{'PROVIDERS'} eq "$Lang::tr{'ids add provider'}") { | |
1628 | # Set the autoupdate to true as default. | |
1629 | $checked{'ENABLE_AUTOUPDATE'} = "checked='checked'"; | |
1630 | } | |
1631 | ||
1632 | print <<END | |
1633 | <form method='post' action='$ENV{'SCRIPT_NAME'}'> | |
1634 | <table width='100%' border='0'> | |
1635 | <tr> | |
1636 | <td colspan='2'><b>$Lang::tr{'ids provider'}</b></td> | |
1637 | </tr> | |
1638 | ||
1639 | <tr> | |
1640 | <td width='40%'> | |
1641 | <input type='hidden' name='ID' value='$cgiparams{'ID'}'> | |
2f252efa SS |
1642 | END |
1643 | ; | |
02fee15e SS |
1644 | # Value to allow disabling the dropdown menu. |
1645 | my $disabled; | |
1646 | ||
1647 | # Check if we are in edit mode. | |
1648 | if ($cgiparams{'PROVIDERS'} eq "$Lang::tr{'edit'}") { | |
1649 | $disabled = "disabled"; | |
1650 | ||
1651 | # Add hidden input with the provider because the disable select does not provider | |
1652 | # this. | |
1653 | print "<input type='hidden' name='PROVIDER' value='$used_providers{$cgiparams{'ID'}}[0]'>\n"; | |
1654 | } | |
1655 | ||
1656 | print "<select name='PROVIDER' id='PROVIDER' $disabled>\n"; | |
2f252efa SS |
1657 | # Loop through the array of ruleset providers. |
1658 | foreach my $provider (@ruleset_providers) { | |
1659 | # Get the provider name. | |
1660 | my $provider_name = &get_provider_name($provider); | |
1661 | ||
1662 | # Pre-select the provider if one is given. | |
1663 | if (($used_providers{$cgiparams{'ID'}}[0] eq "$provider") || ($cgiparams{'PROVIDER'} eq "$provider")) { | |
1664 | $selected{$provider} = "selected='selected'"; | |
1665 | } | |
1666 | ||
1667 | # Add the provider to the dropdown menu. | |
1668 | print "<option value='$provider' $selected{$provider}>$provider_name</option>\n"; | |
1669 | } | |
1670 | print <<END | |
1671 | </select> | |
1672 | </td> | |
1673 | ||
1674 | <td width='60%'> | |
1675 | <b><a id="website" target="_blank" href="#">$Lang::tr{'ids visit provider website'}</a></b> | |
1676 | </td> | |
1677 | </tr> | |
1678 | ||
1679 | <tr> | |
1680 | <td colspan='2'><br><br></td> | |
1681 | </tr> | |
1682 | ||
1683 | <tr class='subscription_code' style='display:none' id='subscription_code'> | |
1684 | <td colspan='2'> | |
1685 | <table border='0'> | |
1686 | <tr> | |
1687 | <td> | |
1688 | <b>$Lang::tr{'subscription code'}</b> | |
1689 | </td> | |
1690 | </tr> | |
1691 | ||
1692 | <tr> | |
1693 | <td> | |
1694 | <input type='text' size='40' name='SUBSCRIPTION_CODE' value='$used_providers{$cgiparams{'ID'}}[1]'> | |
1695 | </td> | |
1696 | </tr> | |
1697 | ||
1698 | <tr> | |
1699 | <td><br><br></td> | |
1700 | </tr> | |
1701 | </table> | |
1702 | </td> | |
1703 | </tr> | |
1704 | ||
1705 | <tr> | |
1706 | <td colspan='2'> | |
1707 | <input type='checkbox' name='ENABLE_AUTOUPDATE' $checked{'ENABLE_AUTOUPDATE'}> $Lang::tr{'ids enable automatic updates'} | |
1708 | </td> | |
1709 | </tr> | |
1710 | ||
1711 | <tr> | |
1712 | <td colspan='2' align='right'> | |
1713 | <input type='submit' value='$Lang::tr{'back'}'> | |
1714 | END | |
1715 | ; | |
1716 | # Check if a provider should be added or edited. | |
1717 | if ($cgiparams{'PROVIDERS'} eq "$Lang::tr{'edit'}") { | |
1718 | # Display button for updating the existing provider. | |
1719 | print "<input type='submit' name='PROVIDERS' value='$Lang::tr{'update'}'>\n"; | |
1720 | } else { | |
1721 | # Display button to add the new provider. | |
1722 | print "<input type='submit' name='PROVIDERS' value='$Lang::tr{'add'}'>\n"; | |
1723 | } | |
1724 | print <<END | |
1725 | </td> | |
1726 | </tr> | |
1727 | </table> | |
1728 | </form> | |
1729 | END | |
1730 | ; | |
1731 | &Header::closebox(); | |
1732 | } | |
1733 | ||
27760092 SS |
1734 | # |
1735 | ## A function to display a notice, to lock the webpage and | |
1736 | ## tell the user which action currently will be performed. | |
1737 | # | |
1738 | sub working_notice ($) { | |
1739 | my ($message) = @_; | |
1740 | ||
1741 | &Header::openpage($Lang::tr{'intrusion detection system'}, 1, ''); | |
1742 | &Header::openbigbox('100%', 'left', '', $errormessage); | |
1743 | &Header::openbox( 'Waiting', 1,); | |
1744 | print <<END; | |
1745 | <table> | |
1746 | <tr> | |
1747 | <td><img src='/images/indicator.gif' alt='$Lang::tr{'aktiv'}' /></td> | |
1748 | <td>$message</td> | |
1749 | </tr> | |
1750 | </table> | |
1751 | END | |
1752 | &Header::closebox(); | |
1753 | &Header::closebigbox(); | |
1754 | &Header::closepage(); | |
1755 | } | |
1756 | ||
3983aebd SS |
1757 | # |
1758 | ## A tiny function to perform a reload of the webpage after one second. | |
1759 | # | |
1760 | sub reload () { | |
1761 | print "<meta http-equiv='refresh' content='1'>\n"; | |
1762 | ||
1763 | # Stop the script. | |
1764 | exit; | |
a70d269a SS |
1765 | } |
1766 | ||
25f5cb0d SS |
1767 | # |
1768 | ## Private function to read-in and parse rules of a given rulefile. | |
1769 | # | |
1770 | ## The given file will be read, parsed and all valid rules will be stored by ID, | |
9d18656b | 1771 | ## message/description and it's state in the idsrules hash. |
25f5cb0d | 1772 | # |
3da6e01b SS |
1773 | sub readrulesfile ($) { |
1774 | my $rulefile = shift; | |
1775 | ||
1776 | # Open rule file and read in contents | |
298ef5ba | 1777 | open(RULEFILE, "$IDS::rulespath/$rulefile") or die "Unable to read $rulefile!"; |
3da6e01b SS |
1778 | |
1779 | # Store file content in an array. | |
1780 | my @lines = <RULEFILE>; | |
1781 | ||
1782 | # Close file. | |
1783 | close(RULEFILE); | |
1784 | ||
1785 | # Loop over rule file contents | |
1786 | foreach my $line (@lines) { | |
1787 | # Remove whitespaces. | |
1788 | chomp $line; | |
1789 | ||
1790 | # Skip blank lines. | |
1791 | next if ($line =~ /^\s*$/); | |
1792 | ||
1793 | # Local vars. | |
1794 | my $sid; | |
1795 | my $msg; | |
1796 | ||
1797 | # Gather rule sid and message from the ruleline. | |
1798 | if ($line =~ m/.*msg:\"(.*?)\"\; .* sid:(.*?); /) { | |
1799 | $msg = $1; | |
1800 | $sid = $2; | |
1801 | ||
1802 | # Check if a rule has been found. | |
1803 | if ($sid && $msg) { | |
9d18656b SS |
1804 | # Add rule to the idsrules hash. |
1805 | $idsrules{$rulefile}{$sid}{'Description'} = $msg; | |
3da6e01b SS |
1806 | |
1807 | # Grab status of the rule. Check if ruleline starts with a "dash". | |
1808 | if ($line =~ /^\#/) { | |
1809 | # If yes, the rule is disabled. | |
9d18656b | 1810 | $idsrules{$rulefile}{$sid}{'State'} = "off"; |
3da6e01b SS |
1811 | } else { |
1812 | # Otherwise the rule is enabled. | |
9d18656b | 1813 | $idsrules{$rulefile}{$sid}{'State'} = "on"; |
3da6e01b SS |
1814 | } |
1815 | } | |
1816 | } | |
b7e29743 | 1817 | } |
3da6e01b | 1818 | } |
87660964 | 1819 | |
8d2f6b0b SS |
1820 | # |
1821 | ## Function to get the used memory of a given process-id. | |
1822 | # | |
87660964 | 1823 | sub get_memory_usage($) { |
004b13b7 | 1824 | my ($pid) = @_; |
87660964 | 1825 | |
004b13b7 | 1826 | my $memory = 0; |
87660964 | 1827 | |
004b13b7 | 1828 | # Try to open the status file for the given process-id on the pseudo |
87660964 | 1829 | # file system proc. |
004b13b7 SS |
1830 | if (open(FILE, "/proc/$pid/status")) { |
1831 | # Loop through the entire file. | |
1832 | while (<FILE>) { | |
1833 | # Splitt current line content and store them into variables. | |
1834 | my ($key, $value) = split(":", $_, 2); | |
1835 | ||
1836 | # Check if the current key is the one which contains the memory usage. | |
1837 | # The wanted one is VmRSS which contains the Real-memory (resident set) | |
1838 | # of the entire process. | |
1839 | if ($key eq "VmRSS") { | |
1840 | # Found the memory usage add it to the memory variable. | |
1841 | $memory += $value; | |
1842 | ||
1843 | # Break the loop. | |
1844 | last; | |
1845 | } | |
1846 | } | |
87660964 SS |
1847 | |
1848 | # Close file handle. | |
004b13b7 | 1849 | close(FILE); |
87660964 SS |
1850 | |
1851 | # Return memory usage. | |
1852 | return $memory; | |
004b13b7 | 1853 | } |
87660964 SS |
1854 | |
1855 | # If the file could not be open, return nothing. | |
1856 | return; | |
1857 | } | |
1858 | ||
a5d61752 SS |
1859 | # |
1860 | ## Function to read-in the given enabled or disables sids file. | |
1861 | # | |
1862 | sub read_enabled_disabled_sids_file($) { | |
1863 | my ($file) = @_; | |
1864 | ||
1865 | # Temporary hash to store the sids and their state. It will be | |
1866 | # returned at the end of this function. | |
1867 | my %temphash; | |
1868 | ||
1869 | # Open the given filename. | |
1870 | open(FILE, "$file") or die "Could not open $file. $!\n"; | |
1871 | ||
1872 | # Loop through the file. | |
1873 | while(<FILE>) { | |
1874 | # Remove newlines. | |
1875 | chomp $_; | |
1876 | ||
1877 | # Skip blank lines. | |
1878 | next if ($_ =~ /^\s*$/); | |
1879 | ||
1880 | # Skip coments. | |
1881 | next if ($_ =~ /^\#/); | |
1882 | ||
1883 | # Splitt line into sid and state part. | |
1884 | my ($state, $sid) = split(" ", $_); | |
1885 | ||
1886 | # Skip line if the sid is not numeric. | |
1887 | next unless ($sid =~ /\d+/ ); | |
1888 | ||
1889 | # Check if the sid was enabled. | |
1890 | if ($state eq "enablesid") { | |
1891 | # Add the sid and its state as enabled to the temporary hash. | |
1892 | $temphash{$sid} = "enabled"; | |
1893 | # Check if the sid was disabled. | |
1894 | } elsif ($state eq "disablesid") { | |
1895 | # Add the sid and its state as disabled to the temporary hash. | |
1896 | $temphash{$sid} = "disabled"; | |
1897 | # Invalid state - skip the current sid and state. | |
1898 | } else { | |
1899 | next; | |
1900 | } | |
1901 | } | |
1902 | ||
1903 | # Close filehandle. | |
1904 | close(FILE); | |
1905 | ||
1906 | # Return the hash. | |
1907 | return %temphash; | |
1908 | } | |
0a1bba1a | 1909 | |
019e5e9b SS |
1910 | # |
1911 | ## Function to get the provider name from the language file or providers file for a given handle. | |
1912 | # | |
1913 | sub get_provider_name($) { | |
1914 | my ($handle) = @_; | |
1915 | my $provider_name; | |
1916 | ||
1917 | # Get the required translation string for the given provider handle. | |
1918 | my $tr_string = $IDS::Ruleset::Providers{$handle}{'tr_string'}; | |
1919 | ||
1920 | # Check if the translation string is available in the language files. | |
1921 | if ($Lang::tr{$tr_string}) { | |
1922 | # Use the translated string from the language file. | |
1923 | $provider_name = $Lang::tr{$tr_string}; | |
1924 | } else { | |
1925 | # Fallback and use the provider summary from the providers file. | |
1926 | $provider_name = $IDS::Ruleset::Providers{$handle}{'summary'}; | |
1927 | } | |
1928 | ||
1929 | # Return the obtained provider name. | |
1930 | return $provider_name; | |
1931 | } | |
1932 | ||
0a1bba1a SS |
1933 | # |
1934 | ## Private function to convert a given rulefile to a category name. | |
1935 | ## ( No file extension anymore and if the name contained a dot, it | |
1936 | ## would be replaced by a underline sign.) | |
1937 | # | |
1938 | sub _rulefile_to_category($) { | |
1939 | my ($filename) = @_; | |
1940 | ||
1941 | # Splitt the filename into single chunks and store them in a | |
1942 | # temorary array. | |
1943 | my @parts = split(/\./, $filename); | |
1944 | ||
1945 | # Return / Remove last element of the temporary array. | |
1946 | # This removes the file extension. | |
1947 | pop @parts; | |
1948 | ||
1949 | # Join together the single elements of the temporary array. | |
1950 | # If these are more than one, use a "underline" for joining. | |
1951 | my $category = join '_', @parts; | |
1952 | ||
1953 | # Return the converted filename. | |
1954 | return $category; | |
1955 | } |