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