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