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