]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blob - html/cgi-bin/ids.cgi
ids.cgi: Only read-in ruleset if neccessary.
[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 %rulessettings=();
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 # Open rules directory and do a directory listing.
258 opendir(DIR, $IDS::rulespath) or die $!;
259 # Loop through the direcory.
260 while (my $file = readdir(DIR)) {
261
262 # We only want files.
263 next unless (-f "$IDS::rulespath/$file");
264
265 # Ignore empty files.
266 next if (-z "$IDS::rulespath/$file");
267
268 # Use a regular expression to find files ending in .rules
269 next unless ($file =~ m/\.rules$/);
270
271 # Ignore files which are not read-able.
272 next unless (-R "$IDS::rulespath/$file");
273
274 # Skip whitelist rules file.
275 next if( $file eq "whitelist.rules");
276
277 # Call subfunction to read-in rulefile and add rules to
278 # the idsrules hash.
279 &readrulesfile("$file");
280 }
281
282 closedir(DIR);
283
284 # Gather used rulefiles.
285 #
286 # Check if the file for activated rulefiles is not empty.
287 if(-f $IDS::used_rulefiles_file) {
288 # Open the file for used rulefile and read-in content.
289 open(FILE, $IDS::used_rulefiles_file) or die "Could not open $IDS::used_rulefiles_file. $!\n";
290
291 # Read-in content.
292 my @lines = <FILE>;
293
294 # Close file.
295 close(FILE);
296
297 # Loop through the array.
298 foreach my $line (@lines) {
299 # Remove newlines.
300 chomp($line);
301
302 # Skip comments.
303 next if ($line =~ /\#/);
304
305 # Skip blank lines.
306 next if ($line =~ /^\s*$/);
307
308 # Gather rule sid and message from the ruleline.
309 if ($line =~ /.*- (.*)/) {
310 my $rulefile = $1;
311
312 # Check if the current rulefile exists in the %idsrules hash.
313 # If not, the file probably does not exist anymore or contains
314 # no rules.
315 if($idsrules{$rulefile}) {
316 # Add the rulefile state to the %idsrules hash.
317 $idsrules{$rulefile}{'Rulefile'}{'State'} = "on";
318 }
319 }
320 }
321 }
322 }
323
324 # Save ruleset configuration.
325 if ($cgiparams{'RULESET'} eq $Lang::tr{'save'}) {
326 my %oldsettings;
327
328 # Read-in current (old) IDS settings.
329 &General::readhash("$IDS::rules_settings_file", \%oldsettings);
330
331 # Prevent form name from been stored in conf file.
332 delete $cgiparams{'RULESET'};
333
334 # Check if the choosen vendor (URL) requires an subscription/oinkcode.
335 if ($IDS::Ruleset::Providers{$cgiparams{'RULES'}}{'requires_subscription'} eq "True") {
336 # Check if an subscription/oinkcode has been provided.
337 if ($cgiparams{'OINKCODE'}) {
338 # Check if the oinkcode contains unallowed chars.
339 unless ($cgiparams{'OINKCODE'} =~ /^[a-z0-9]+$/) {
340 $errormessage = $Lang::tr{'invalid input for oink code'};
341 }
342 } else {
343 # Print an error message, that an subsription/oinkcode is required for this
344 # vendor.
345 $errormessage = $Lang::tr{'ids oinkcode required'};
346 }
347 }
348
349 # Go on if there are no error messages.
350 if (!$errormessage) {
351 # Store settings into settings file.
352 &General::writehash("$IDS::rules_settings_file", \%cgiparams);
353
354 # Check if the the automatic rule update hass been touched.
355 if($cgiparams{'AUTOUPDATE_INTERVAL'} ne $oldsettings{'AUTOUPDATE_INTERVAL'}) {
356 # Call suricatactrl to set the new interval.
357 &IDS::call_suricatactrl("cron", $cgiparams{'AUTOUPDATE_INTERVAL'});
358 }
359
360 # Check if a ruleset is present - if not or the source has been changed download it.
361 if((! %idsrules) || ($oldsettings{'RULES'} ne $cgiparams{'RULES'})) {
362 # Check if the red device is active.
363 unless (-e "${General::swroot}/red/active") {
364 $errormessage = "$Lang::tr{'could not download latest updates'} - $Lang::tr{'system is offline'}";
365 }
366
367 # Check if enough free disk space is availabe.
368 if(&IDS::checkdiskspace()) {
369 $errormessage = "$Lang::tr{'not enough disk space'}";
370 }
371
372 # Check if any errors happend.
373 unless ($errormessage) {
374 # Lock the webpage and print notice about downloading
375 # a new ruleset.
376 &working_notice("$Lang::tr{'ids working'}");
377
378 # Write the modify sid's file and pass the taken ruleaction.
379 &IDS::write_modify_sids_file();
380
381 # Call subfunction to download the ruleset.
382 if(&IDS::downloadruleset()) {
383 $errormessage = $Lang::tr{'could not download latest updates'};
384
385 # Call function to store the errormessage.
386 &IDS::_store_error_message($errormessage);
387 } else {
388 # Call subfunction to launch oinkmaster.
389 &IDS::oinkmaster();
390 }
391
392 # Check if the IDS is running.
393 if(&IDS::ids_is_running()) {
394 # Call suricatactrl to stop the IDS - because of the changed
395 # ruleset - the use has to configure it before suricata can be
396 # used again.
397 &IDS::call_suricatactrl("stop");
398 }
399
400 # Perform a reload of the page.
401 &reload();
402 }
403 }
404 }
405
406 # Save ruleset.
407 } elsif ($cgiparams{'RULESET'} eq $Lang::tr{'ids apply'}) {
408 # Arrays to store which rulefiles have been enabled and will be used.
409 my @enabled_rulefiles;
410
411 # Hash to store the user-enabled and disabled sids.
412 my %enabled_disabled_sids;
413
414 # Store if a restart of suricata is required.
415 my $suricata_restart_required;
416
417 # Loop through the hash of idsrules.
418 foreach my $rulefile(keys %idsrules) {
419 # Check if the state of the rulefile has been changed.
420 unless ($cgiparams{$rulefile} eq $idsrules{$rulefile}{'Rulefile'}{'State'}) {
421 # A restart of suricata is required to apply the changes of the used rulefiles.
422 $suricata_restart_required = 1;
423 }
424
425 # Check if the rulefile is enabled.
426 if ($cgiparams{$rulefile} eq "on") {
427 # Add rulefile to the array of enabled rulefiles.
428 push(@enabled_rulefiles, $rulefile);
429
430 # Drop item from cgiparams hash.
431 delete $cgiparams{$rulefile};
432 }
433 }
434
435 # Read-in the files for enabled/disabled sids.
436 # This will be done by calling the read_enabled_disabled_sids_file function two times
437 # and merge the returned hashes together into the enabled_disabled_sids hash.
438 %enabled_disabled_sids = (
439 &read_enabled_disabled_sids_file($IDS::disabled_sids_file),
440 &read_enabled_disabled_sids_file($IDS::enabled_sids_file));
441
442 # Loop through the hash of idsrules.
443 foreach my $rulefile (keys %idsrules) {
444 # Loop through the single rules of the rulefile.
445 foreach my $sid (keys %{$idsrules{$rulefile}}) {
446 # Skip the current sid if it is not numeric.
447 next unless ($sid =~ /\d+/ );
448
449 # Check if there exists a key in the cgiparams hash for this sid.
450 if (exists($cgiparams{$sid})) {
451 # Look if the rule is disabled.
452 if ($idsrules{$rulefile}{$sid}{'State'} eq "off") {
453 # Check if the state has been set to 'on'.
454 if ($cgiparams{$sid} eq "on") {
455 # Add/Modify the sid to/in the enabled_disabled_sids hash.
456 $enabled_disabled_sids{$sid} = "enabled";
457
458 # Drop item from cgiparams hash.
459 delete $cgiparams{$rulefile}{$sid};
460 }
461 }
462 } else {
463 # Look if the rule is enabled.
464 if ($idsrules{$rulefile}{$sid}{'State'} eq "on") {
465 # Check if the state is 'on' and should be disabled.
466 # In this case there is no entry
467 # for the sid in the cgiparams hash.
468 # Add/Modify it to/in the enabled_disabled_sids hash.
469 $enabled_disabled_sids{$sid} = "disabled";
470
471 # Drop item from cgiparams hash.
472 delete $cgiparams{$rulefile}{$sid};
473 }
474 }
475 }
476 }
477
478 # Open enabled sid's file for writing.
479 open(ENABLED_FILE, ">$IDS::enabled_sids_file") or die "Could not write to $IDS::enabled_sids_file. $!\n";
480
481 # Open disabled sid's file for writing.
482 open(DISABLED_FILE, ">$IDS::disabled_sids_file") or die "Could not write to $IDS::disabled_sids_file. $!\n";
483
484 # Write header to the files.
485 print ENABLED_FILE "#Autogenerated file. Any custom changes will be overwritten!\n";
486 print DISABLED_FILE "#Autogenerated file. Any custom changes will be overwritten!\n";
487
488 # Check if the hash for enabled/disabled files contains any entries.
489 if (%enabled_disabled_sids) {
490 # Loop through the hash.
491 foreach my $sid (keys %enabled_disabled_sids) {
492 # Check if the sid is enabled.
493 if ($enabled_disabled_sids{$sid} eq "enabled") {
494 # Print the sid to the enabled_sids file.
495 print ENABLED_FILE "enablesid $sid\n";
496 # Check if the sid is disabled.
497 } elsif ($enabled_disabled_sids{$sid} eq "disabled") {
498 # Print the sid to the disabled_sids file.
499 print DISABLED_FILE "disablesid $sid\n";
500 # Something strange happende - skip the current sid.
501 } else {
502 next;
503 }
504 }
505 }
506
507 # Close file for enabled_sids after writing.
508 close(ENABLED_FILE);
509
510 # Close file for disabled_sids after writing.
511 close(DISABLED_FILE);
512
513 # Call function to generate and write the used rulefiles file.
514 &IDS::write_used_rulefiles_file(@enabled_rulefiles);
515
516 # Lock the webpage and print message.
517 &working_notice("$Lang::tr{'ids apply ruleset changes'}");
518
519 # Call oinkmaster to alter the ruleset.
520 &IDS::oinkmaster();
521
522 # Check if the IDS is running.
523 if(&IDS::ids_is_running()) {
524 # Check if a restart of suricata is required.
525 if ($suricata_restart_required) {
526 # Call suricatactrl to perform the restart.
527 &IDS::call_suricatactrl("restart");
528 } else {
529 # Call suricatactrl to perform a reload.
530 &IDS::call_suricatactrl("reload");
531 }
532 }
533
534 # Reload page.
535 &reload();
536
537 # Download new ruleset.
538 } elsif ($cgiparams{'RULESET'} eq $Lang::tr{'update ruleset'}) {
539 # Check if the red device is active.
540 unless (-e "${General::swroot}/red/active") {
541 $errormessage = "$Lang::tr{'could not download latest updates'} - $Lang::tr{'system is offline'}";
542 }
543
544 # Check if enought free disk space is availabe.
545 if(&IDS::checkdiskspace()) {
546 $errormessage = "$Lang::tr{'not enough disk space'}";
547 }
548
549 # Check if any errors happend.
550 unless ($errormessage) {
551 # Lock the webpage and print notice about downloading
552 # a new ruleset.
553 &working_notice("$Lang::tr{'ids download new ruleset'}");
554
555 # Call subfunction to download the ruleset.
556 if(&IDS::downloadruleset()) {
557 $errormessage = $Lang::tr{'could not download latest updates'};
558
559 # Call function to store the errormessage.
560 &IDS::_store_error_message($errormessage);
561
562 # Preform a reload of the page.
563 &reload();
564 } else {
565 # Call subfunction to launch oinkmaster.
566 &IDS::oinkmaster();
567
568 # Check if the IDS is running.
569 if(&IDS::ids_is_running()) {
570 # Call suricatactrl to perform a reload.
571 &IDS::call_suricatactrl("reload");
572 }
573
574 # Perform a reload of the page.
575 &reload();
576 }
577 }
578 # Save IDS settings.
579 } elsif ($cgiparams{'IDS'} eq $Lang::tr{'save'}) {
580 my %oldidssettings;
581 my $reload_page;
582 my $monitored_zones = 0;
583
584 # Read-in current (old) IDS settings.
585 &General::readhash("$IDS::ids_settings_file", \%oldidssettings);
586
587 # Prevent form name from been stored in conf file.
588 delete $cgiparams{'IDS'};
589
590 # Check if the IDS should be enabled.
591 if ($cgiparams{'ENABLE_IDS'} eq "on") {
592 # Check if any ruleset is available. Otherwise abort and display an error.
593 unless(%idsrules) {
594 $errormessage = $Lang::tr{'ids no ruleset available'};
595 }
596
597 # Loop through the array of available interfaces.
598 foreach my $zone (@network_zones) {
599 # Convert interface name into upper case.
600 my $zone_upper = uc($zone);
601
602 # Check if the IDS is enabled for this interaces.
603 if ($cgiparams{"ENABLE_IDS_$zone_upper"}) {
604 # Increase count.
605 $monitored_zones++;
606 }
607 }
608
609 # Check if at least one zone should be monitored, or show an error.
610 unless ($monitored_zones >= 1) {
611 $errormessage = $Lang::tr{'ids no network zone'};
612 }
613 }
614
615 # Go on if there are no error messages.
616 if (!$errormessage) {
617 # Store settings into settings file.
618 &General::writehash("$IDS::ids_settings_file", \%cgiparams);
619 }
620
621 # Generate file to store the home net.
622 &IDS::generate_home_net_file();
623
624 # Generate file to the store the DNS servers.
625 &IDS::generate_dns_servers_file();
626
627 # Generate file to store the HTTP ports.
628 &IDS::generate_http_ports_file();
629
630 # Write the modify sid's file and pass the taken ruleaction.
631 &IDS::write_modify_sids_file();
632
633 # Check if "MONITOR_TRAFFIC_ONLY" has been changed.
634 if($cgiparams{'MONITOR_TRAFFIC_ONLY'} ne $oldidssettings{'MONITOR_TRAFFIC_ONLY'}) {
635 # Check if a ruleset exists.
636 if (%idsrules) {
637 # Lock the webpage and print message.
638 &working_notice("$Lang::tr{'ids working'}");
639
640 # Call oinkmaster to alter the ruleset.
641 &IDS::oinkmaster();
642
643 # Set reload_page to "True".
644 $reload_page="True";
645 }
646 }
647
648 # Check if the IDS currently is running.
649 if(&IDS::ids_is_running()) {
650 # Check if ENABLE_IDS is set to on.
651 if($cgiparams{'ENABLE_IDS'} eq "on") {
652 # Call suricatactrl to perform a reload of suricata.
653 &IDS::call_suricatactrl("reload");
654 } else {
655 # Call suricatactrl to stop suricata.
656 &IDS::call_suricatactrl("stop");
657 }
658 } else {
659 # Call suricatactrl to start suricata.
660 &IDS::call_suricatactrl("start");
661 }
662
663 # Check if the page should be reloaded.
664 if ($reload_page) {
665 # Perform a reload of the page.
666 &reload();
667 }
668 }
669
670 # Read-in idssettings and rulesetsettings
671 &General::readhash("$IDS::ids_settings_file", \%idssettings);
672 &General::readhash("$IDS::rules_settings_file", \%rulessettings);
673
674 # If no autoupdate intervall has been configured yet, set default value.
675 unless(exists($rulessettings{'AUTOUPDATE_INTERVAL'})) {
676 # Set default to "weekly".
677 $rulessettings{'AUTOUPDATE_INTERVAL'} = 'weekly';
678 }
679
680 # Read-in ignored hosts.
681 &General::readhasharray("$IDS::settingsdir/ignored", \%ignored);
682
683 $checked{'ENABLE_IDS'}{'off'} = '';
684 $checked{'ENABLE_IDS'}{'on'} = '';
685 $checked{'ENABLE_IDS'}{$idssettings{'ENABLE_IDS'}} = "checked='checked'";
686 $checked{'MONITOR_TRAFFIC_ONLY'}{'off'} = '';
687 $checked{'MONITOR_TRAFFIC_ONLY'}{'on'} = '';
688 $checked{'MONITOR_TRAFFIC_ONLY'}{$idssettings{'MONITOR_TRAFFIC_ONLY'}} = "checked='checked'";
689 $selected{'RULES'}{'nothing'} = '';
690 $selected{'RULES'}{$rulessettings{'RULES'}} = "selected='selected'";
691 $selected{'AUTOUPDATE_INTERVAL'}{'off'} = '';
692 $selected{'AUTOUPDATE_INTERVAL'}{'daily'} = '';
693 $selected{'AUTOUPDATE_INTERVAL'}{'weekly'} = '';
694 $selected{'AUTOUPDATE_INTERVAL'}{$rulessettings{'AUTOUPDATE_INTERVAL'}} = "selected='selected'";
695
696 &Header::openpage($Lang::tr{'intrusion detection system'}, 1, '');
697
698 ### Java Script ###
699 print"<script>\n";
700
701 # Java script variable declaration for show and hide.
702 print"var show = \"$Lang::tr{'ids show'}\"\;\n";
703 print"var hide = \"$Lang::tr{'ids hide'}\"\;\n";
704
705 print <<END
706 // Java Script function to show/hide the text input field for
707 // Oinkcode/Subscription code.
708 var update_code = function() {
709 if(\$('#RULES').val() == 'registered') {
710 \$('#code').show();
711 } else if(\$('#RULES').val() == 'subscripted') {
712 \$('#code').show();
713 } else if(\$('#RULES').val() == 'emerging_pro') {
714 \$('#code').show();
715 } else {
716 \$('#code').hide();
717 }
718 };
719
720 // JQuery function to call corresponding function when
721 // the ruleset is changed or the page is loaded for showing/hiding
722 // the code area.
723 \$(document).ready(function() {
724 \$('#RULES').change(update_code);
725 update_code();
726 });
727
728 // Tiny java script function to show/hide the rules
729 // of a given category.
730 function showhide(tblname) {
731 \$("#" + tblname).toggle();
732
733 // Get current content of the span element.
734 var content = document.getElementById("span_" + tblname);
735
736 if (content.innerHTML === show) {
737 content.innerHTML = hide;
738 } else {
739 content.innerHTML = show;
740 }
741 }
742 </script>
743 END
744 ;
745
746 &Header::openbigbox('100%', 'left', '', $errormessage);
747
748 if ($errormessage) {
749 &Header::openbox('100%', 'left', $Lang::tr{'error messages'});
750 print "<class name='base'>$errormessage\n";
751 print "&nbsp;</class>\n";
752 &Header::closebox();
753 }
754
755 # Draw current state of the IDS
756 &Header::openbox('100%', 'left', $Lang::tr{'intrusion detection system'});
757
758 # Check if the IDS is running and obtain the process-id.
759 my $pid = &IDS::ids_is_running();
760
761 # Display some useful information, if suricata daemon is running.
762 if ($pid) {
763 # Gather used memory.
764 my $memory = &get_memory_usage($pid);
765
766 print <<END;
767 <table width='95%' cellspacing='0' class='tbl'>
768 <tr>
769 <th bgcolor='$color{'color20'}' colspan='3' align='left'><strong>$Lang::tr{'intrusion detection'}</strong></th>
770 </tr>
771
772 <tr>
773 <td class='base'>$Lang::tr{'guardian daemon'}</td>
774 <td align='center' colspan='2' width='75%' bgcolor='${Header::colourgreen}'><font color='white'><strong>$Lang::tr{'running'}</strong></font></td>
775 </tr>
776
777 <tr>
778 <td class='base'></td>
779 <td bgcolor='$color{'color20'}' align='center'><strong>PID</strong></td>
780 <td bgcolor='$color{'color20'}' align='center'><strong>$Lang::tr{'memory'}</strong></td>
781 </tr>
782
783 <tr>
784 <td class='base'></td>
785 <td bgcolor='$color{'color22'}' align='center'>$pid</td>
786 <td bgcolor='$color{'color22'}' align='center'>$memory KB</td>
787 </tr>
788 </table>
789 END
790 } else {
791 # Otherwise display a hint that the service is not launched.
792 print <<END;
793 <table width='95%' cellspacing='0' class='tbl'>
794 <tr>
795 <th bgcolor='$color{'color20'}' colspan='3' align='left'><strong>$Lang::tr{'intrusion detection'}</strong></th>
796 </tr>
797
798 <tr>
799 <td class='base'>$Lang::tr{'guardian daemon'}</td>
800 <td align='center' width='75%' bgcolor='${Header::colourred}'><font color='white'><strong>$Lang::tr{'stopped'}</strong></font></td>
801 </tr>
802 </table>
803 END
804 }
805
806 # Only show this area, if a ruleset is present.
807 if (%idsrules) {
808
809 print <<END
810
811 <br><br><h2>$Lang::tr{'settings'}</h2>
812
813 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
814 <table width='100%' border='0'>
815 <tr>
816 <td class='base' colspan='2'>
817 <input type='checkbox' name='ENABLE_IDS' $checked{'ENABLE_IDS'}{'on'}>&nbsp;$Lang::tr{'ids enable'}
818 </td>
819
820 <td class='base' colspan='2'>
821 <input type='checkbox' name='MONITOR_TRAFFIC_ONLY' $checked{'MONITOR_TRAFFIC_ONLY'}{'on'}>&nbsp;$Lang::tr{'ids monitor traffic only'}
822 </td>
823 </tr>
824
825 <tr>
826 <td><br><br></td>
827 <td><br><br></td>
828 <td><br><br></td>
829 <td><br><br></td>
830 </tr>
831
832 <tr>
833 <td colspan='4'><b>$Lang::tr{'ids monitored interfaces'}</b><br></td>
834 </tr>
835
836 <tr>
837 END
838 ;
839
840 # Loop through the array of available networks and print config options.
841 foreach my $zone (@network_zones) {
842 my $checked_input;
843 my $checked_forward;
844
845 # Convert current zone name to upper case.
846 my $zone_upper = uc($zone);
847
848 # Set zone name.
849 my $zone_name = $zone;
850
851 # Dirty hack to get the correct language string for the red zone.
852 if ($zone eq "red") {
853 $zone_name = "red1";
854 }
855
856 # Grab checkbox status from settings hash.
857 if ($idssettings{"ENABLE_IDS_$zone_upper"} eq "on") {
858 $checked_input = "checked = 'checked'";
859 }
860
861 print "<td class='base' width='20%'>\n";
862 print "<input type='checkbox' name='ENABLE_IDS_$zone_upper' $checked_input>\n";
863 print "&nbsp;$Lang::tr{'enabled on'}<font color='$colourhash{$zone}'> $Lang::tr{$zone_name}</font>\n";
864 print "</td>\n";
865 }
866
867 print <<END
868 </tr>
869 </table>
870
871 <br><br>
872
873 <table width='100%'>
874 <tr>
875 <td align='right'><input type='submit' name='IDS' value='$Lang::tr{'save'}' /></td>
876 </tr>
877 </table>
878 </form>
879 END
880 ;
881
882 }
883
884 &Header::closebox();
885
886 # Draw elements for ruleset configuration.
887 &Header::openbox('100%', 'center', $Lang::tr{'ids ruleset settings'});
888
889 print <<END
890 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
891 <table width='100%' border='0'>
892 <tr>
893 <td><b>$Lang::tr{'ids rules update'}</b></td>
894 <td><b>$Lang::tr{'ids automatic rules update'}</b></td>
895 </tr>
896
897 <tr>
898 <td><select name='RULES' id='RULES'>
899 END
900 ;
901 # Get all available ruleset providers.
902 my @ruleset_providers = &IDS::get_ruleset_providers();
903
904 # Loop throgh the list of providers.
905 foreach my $provider (@ruleset_providers) {
906 # Call get_provider_name function to obtain the provider name.
907 my $option_string = &get_provider_name($provider);
908
909 # Print option.
910 print "<option value='$provider' $selected{'RULES'}{$provider}>$option_string</option>\n";
911 }
912 print <<END;
913 </select>
914 </td>
915
916 <td>
917 <select name='AUTOUPDATE_INTERVAL'>
918 <option value='off' $selected{'AUTOUPDATE_INTERVAL'}{'off'} >- $Lang::tr{'Disabled'} -</option>
919 <option value='daily' $selected{'AUTOUPDATE_INTERVAL'}{'daily'} >$Lang::tr{'Daily'}</option>
920 <option value='weekly' $selected{'AUTOUPDATE_INTERVAL'}{'weekly'} >$Lang::tr{'Weekly'}</option>
921 </select>
922 </td>
923 </tr>
924
925 <tr>
926 <td colspan='2'><br><br></td>
927 </tr>
928
929 <tr style='display:none' id='code'>
930 <td colspan='2'>Oinkcode:&nbsp;<input type='text' size='40' name='OINKCODE' value='$rulessettings{'OINKCODE'}'></td>
931 </tr>
932
933 <tr>
934 <td>&nbsp;</td>
935
936 <td align='right'>
937 END
938 ;
939 # Show the "Update Ruleset"-Button only if a ruleset has been downloaded yet and automatic updates are disabled.
940 if ((%idsrules) && ($rulessettings{'AUTOUPDATE_INTERVAL'} eq "off")) {
941 # Display button to update the ruleset.
942 print"<input type='submit' name='RULESET' value='$Lang::tr{'update ruleset'}'>\n";
943 }
944 print <<END;
945 <input type='submit' name='RULESET' value='$Lang::tr{'ids customize ruleset'}'>
946 <input type='submit' name='RULESET' value='$Lang::tr{'save'}'>
947 </td>
948
949 </tr>
950 </table>
951 </form>
952 END
953 ;
954
955 &Header::closebox();
956
957 #
958 # Whitelist / Ignorelist
959 #
960 &Header::openbox('100%', 'center', $Lang::tr{'ids ignored hosts'});
961
962 print <<END;
963 <table width='100%'>
964 <tr>
965 <td class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'ip address'}</b></td>
966 <td class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'remark'}</b></td>
967 <td class='base' colspan='3' bgcolor='$color{'color20'}'></td>
968 </tr>
969 END
970 # Check if some hosts have been added to be ignored.
971 if (keys (%ignored)) {
972 my $col = "";
973
974 # Loop through all entries of the hash.
975 while( (my $key) = each %ignored) {
976 # Assign data array positions to some nice variable names.
977 my $address = $ignored{$key}[0];
978 my $remark = $ignored{$key}[1];
979 my $status = $ignored{$key}[2];
980
981 # Check if the key (id) number is even or not.
982 if ($cgiparams{'ID'} eq $key) {
983 $col="bgcolor='${Header::colouryellow}'";
984 } elsif ($key % 2) {
985 $col="bgcolor='$color{'color22'}'";
986 } else {
987 $col="bgcolor='$color{'color20'}'";
988 }
989
990 # Choose icon for the checkbox.
991 my $gif;
992 my $gdesc;
993
994 # Check if the status is enabled and select the correct image and description.
995 if ($status eq 'enabled' ) {
996 $gif = 'on.gif';
997 $gdesc = $Lang::tr{'click to disable'};
998 } else {
999 $gif = 'off.gif';
1000 $gdesc = $Lang::tr{'click to enable'};
1001 }
1002
1003 print <<END;
1004 <tr>
1005 <td width='20%' class='base' $col>$address</td>
1006 <td width='65%' class='base' $col>$remark</td>
1007
1008 <td align='center' $col>
1009 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
1010 <input type='hidden' name='WHITELIST' value='$Lang::tr{'toggle enable disable'}' />
1011 <input type='image' name='$Lang::tr{'toggle enable disable'}' src='/images/$gif' alt='$gdesc' title='$gdesc' />
1012 <input type='hidden' name='ID' value='$key' />
1013 </form>
1014 </td>
1015
1016 <td align='center' $col>
1017 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
1018 <input type='hidden' name='WHITELIST' value='$Lang::tr{'edit'}' />
1019 <input type='image' name='$Lang::tr{'edit'}' src='/images/edit.gif' alt='$Lang::tr{'edit'}' title='$Lang::tr{'edit'}' />
1020 <input type='hidden' name='ID' value='$key' />
1021 </form>
1022 </td>
1023
1024 <td align='center' $col>
1025 <form method='post' name='$key' action='$ENV{'SCRIPT_NAME'}'>
1026 <input type='image' name='$Lang::tr{'remove'}' src='/images/delete.gif' title='$Lang::tr{'remove'}' alt='$Lang::tr{'remove'}'>
1027 <input type='hidden' name='ID' value='$key'>
1028 <input type='hidden' name='WHITELIST' value='$Lang::tr{'remove'}'>
1029 </form>
1030 </td>
1031 </tr>
1032 END
1033 }
1034 } else {
1035 # Print notice that currently no hosts are ignored.
1036 print "<tr>\n";
1037 print "<td class='base' colspan='2'>$Lang::tr{'guardian no entries'}</td>\n";
1038 print "</tr>\n";
1039 }
1040
1041 print "</table>\n";
1042
1043 # Section to add new elements or edit existing ones.
1044 print <<END;
1045 <br>
1046 <hr>
1047 <br>
1048
1049 <div align='center'>
1050 <table width='100%'>
1051 END
1052
1053 # Assign correct headline and button text.
1054 my $buttontext;
1055 my $entry_address;
1056 my $entry_remark;
1057
1058 # Check if an ID (key) has been given, in this case an existing entry should be edited.
1059 if ($cgiparams{'ID'} ne '') {
1060 $buttontext = $Lang::tr{'update'};
1061 print "<tr><td class='boldbase' colspan='3'><b>$Lang::tr{'update'}</b></td></tr>\n";
1062
1063 # Grab address and remark for the given key.
1064 $entry_address = $ignored{$cgiparams{'ID'}}[0];
1065 $entry_remark = $ignored{$cgiparams{'ID'}}[1];
1066 } else {
1067 $buttontext = $Lang::tr{'add'};
1068 print "<tr><td class='boldbase' colspan='3'><b>$Lang::tr{'dnsforward add a new entry'}</b></td></tr>\n";
1069 }
1070
1071 print <<END;
1072 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
1073 <input type='hidden' name='ID' value='$cgiparams{'ID'}'>
1074 <tr>
1075 <td width='30%'>$Lang::tr{'ip address'}: </td>
1076 <td width='50%'><input type='text' name='IGNORE_ENTRY_ADDRESS' value='$entry_address' size='24' /></td>
1077
1078 <td width='30%'>$Lang::tr{'remark'}: </td>
1079 <td wicth='50%'><input type='text' name=IGNORE_ENTRY_REMARK value='$entry_remark' size='24' /></td>
1080 <td align='center' width='20%'><input type='submit' name='WHITELIST' value='$buttontext' /></td>
1081 </tr>
1082 </form>
1083 </table>
1084 </div>
1085 END
1086
1087 &Header::closebox();
1088
1089 # Only show the section for configuring the ruleset if one is present.
1090 if (%idsrules) {
1091 &show_customize_ruleset();
1092 }
1093
1094 &Header::closebigbox();
1095 &Header::closepage();
1096
1097 #
1098 ## Function to show the customize ruleset section.
1099 #
1100 sub show_customize_ruleset() {
1101 # Load neccessary perl modules for file stat and to format the timestamp.
1102 use File::stat;
1103 use POSIX qw( strftime );
1104
1105 # Call stat on the rulestarball.
1106 my $stat = stat("$IDS::rulestarball");
1107
1108 if (defined $stat) {
1109 # Get timestamp the file creation.
1110 my $mtime = $stat->mtime;
1111
1112 # Convert into human read-able format.
1113 my $rulesdate = strftime('%Y-%m-%d %H:%M:%S', localtime($mtime));
1114
1115 &Header::openbox('100%', 'LEFT', "$Lang::tr{'intrusion detection system rules'} ($rulesdate)" );
1116
1117 print"<form method='POST' action='$ENV{'SCRIPT_NAME'}'>\n";
1118
1119 # Output display table for rule files
1120 print "<table width='100%'>\n";
1121
1122 # Loop over each rule file
1123 foreach my $rulefile (sort keys(%idsrules)) {
1124 my $rulechecked = '';
1125
1126 # Check if rule file is enabled
1127 if ($idsrules{$rulefile}{'Rulefile'}{'State'} eq 'on') {
1128 $rulechecked = 'CHECKED';
1129 }
1130
1131 # Convert rulefile name into category name.
1132 my $categoryname = &_rulefile_to_category($rulefile);
1133
1134 # Table and rows for the rule files.
1135 print"<tr>\n";
1136 print"<td class='base' width='5%'>\n";
1137 print"<input type='checkbox' name='$rulefile' $rulechecked>\n";
1138 print"</td>\n";
1139 print"<td class='base' width='90%'><b>$rulefile</b></td>\n";
1140 print"<td class='base' width='5%' align='right'>\n";
1141 print"<a href=\"javascript:showhide('$categoryname')\"><span id='span_$categoryname'>$Lang::tr{'ids show'}</span></a>\n";
1142 print"</td>\n";
1143 print"</tr>\n";
1144
1145 # Rows which will be hidden per default and will contain the single rules.
1146 print"<tr style='display:none' id='$categoryname'>\n";
1147 print"<td colspan='3'>\n";
1148
1149 # Local vars
1150 my $lines;
1151 my $rows;
1152 my $col;
1153
1154 # New table for the single rules.
1155 print "<table width='100%'>\n";
1156
1157 # Loop over rule file rules
1158 foreach my $sid (sort {$a <=> $b} keys(%{$idsrules{$rulefile}})) {
1159 # Local vars
1160 my $ruledefchecked = '';
1161
1162 # Skip rulefile itself.
1163 next if ($sid eq "Rulefile");
1164
1165 # If 2 rules have been displayed, start a new row
1166 if (($lines % 2) == 0) {
1167 print "</tr><tr>\n";
1168
1169 # Increase rows by once.
1170 $rows++;
1171 }
1172
1173 # Colour lines.
1174 if ($rows % 2) {
1175 $col="bgcolor='$color{'color20'}'";
1176 } else {
1177 $col="bgcolor='$color{'color22'}'";
1178 }
1179
1180 # Set rule state
1181 if ($idsrules{$rulefile}{$sid}{'State'} eq 'on') {
1182 $ruledefchecked = 'CHECKED';
1183 }
1184
1185 # Create rule checkbox and display rule description
1186 print "<td class='base' width='5%' align='right' $col>\n";
1187 print "<input type='checkbox' NAME='$sid' $ruledefchecked>\n";
1188 print "</td>\n";
1189 print "<td class='base' width='45%' $col>$idsrules{$rulefile}{$sid}{'Description'}</td>";
1190
1191 # Increment rule count
1192 $lines++;
1193 }
1194
1195 # If do not have a second rule for row, create empty cell
1196 if (($lines % 2) != 0) {
1197 print "<td class='base'></td>";
1198 }
1199
1200 # Close display table
1201 print "</tr></table></td></tr>";
1202 }
1203
1204 # Close display table
1205 print "</table>";
1206
1207 print <<END
1208 <table width='100%'>
1209 <tr>
1210 <td width='100%' align='right'><input type='submit' name='RULESET' value='$Lang::tr{'ids apply'}'></td>
1211 </tr>
1212 </table>
1213 </form>
1214 END
1215 ;
1216 &Header::closebox();
1217 }
1218 }
1219
1220 #
1221 ## A function to display a notice, to lock the webpage and
1222 ## tell the user which action currently will be performed.
1223 #
1224 sub working_notice ($) {
1225 my ($message) = @_;
1226
1227 &Header::openpage($Lang::tr{'intrusion detection system'}, 1, '');
1228 &Header::openbigbox('100%', 'left', '', $errormessage);
1229 &Header::openbox( 'Waiting', 1,);
1230 print <<END;
1231 <table>
1232 <tr>
1233 <td><img src='/images/indicator.gif' alt='$Lang::tr{'aktiv'}' /></td>
1234 <td>$message</td>
1235 </tr>
1236 </table>
1237 END
1238 &Header::closebox();
1239 &Header::closebigbox();
1240 &Header::closepage();
1241 }
1242
1243 #
1244 ## A tiny function to perform a reload of the webpage after one second.
1245 #
1246 sub reload () {
1247 print "<meta http-equiv='refresh' content='1'>\n";
1248
1249 # Stop the script.
1250 exit;
1251 }
1252
1253 #
1254 ## Private function to read-in and parse rules of a given rulefile.
1255 #
1256 ## The given file will be read, parsed and all valid rules will be stored by ID,
1257 ## message/description and it's state in the idsrules hash.
1258 #
1259 sub readrulesfile ($) {
1260 my $rulefile = shift;
1261
1262 # Open rule file and read in contents
1263 open(RULEFILE, "$IDS::rulespath/$rulefile") or die "Unable to read $rulefile!";
1264
1265 # Store file content in an array.
1266 my @lines = <RULEFILE>;
1267
1268 # Close file.
1269 close(RULEFILE);
1270
1271 # Loop over rule file contents
1272 foreach my $line (@lines) {
1273 # Remove whitespaces.
1274 chomp $line;
1275
1276 # Skip blank lines.
1277 next if ($line =~ /^\s*$/);
1278
1279 # Local vars.
1280 my $sid;
1281 my $msg;
1282
1283 # Gather rule sid and message from the ruleline.
1284 if ($line =~ m/.*msg:\"(.*?)\"\; .* sid:(.*?); /) {
1285 $msg = $1;
1286 $sid = $2;
1287
1288 # Check if a rule has been found.
1289 if ($sid && $msg) {
1290 # Add rule to the idsrules hash.
1291 $idsrules{$rulefile}{$sid}{'Description'} = $msg;
1292
1293 # Grab status of the rule. Check if ruleline starts with a "dash".
1294 if ($line =~ /^\#/) {
1295 # If yes, the rule is disabled.
1296 $idsrules{$rulefile}{$sid}{'State'} = "off";
1297 } else {
1298 # Otherwise the rule is enabled.
1299 $idsrules{$rulefile}{$sid}{'State'} = "on";
1300 }
1301 }
1302 }
1303 }
1304 }
1305
1306 #
1307 ## Function to get the used memory of a given process-id.
1308 #
1309 sub get_memory_usage($) {
1310 my ($pid) = @_;
1311
1312 my $memory = 0;
1313
1314 # Try to open the status file for the given process-id on the pseudo
1315 # file system proc.
1316 if (open(FILE, "/proc/$pid/status")) {
1317 # Loop through the entire file.
1318 while (<FILE>) {
1319 # Splitt current line content and store them into variables.
1320 my ($key, $value) = split(":", $_, 2);
1321
1322 # Check if the current key is the one which contains the memory usage.
1323 # The wanted one is VmRSS which contains the Real-memory (resident set)
1324 # of the entire process.
1325 if ($key eq "VmRSS") {
1326 # Found the memory usage add it to the memory variable.
1327 $memory += $value;
1328
1329 # Break the loop.
1330 last;
1331 }
1332 }
1333
1334 # Close file handle.
1335 close(FILE);
1336
1337 # Return memory usage.
1338 return $memory;
1339 }
1340
1341 # If the file could not be open, return nothing.
1342 return;
1343 }
1344
1345 #
1346 ## Function to read-in the given enabled or disables sids file.
1347 #
1348 sub read_enabled_disabled_sids_file($) {
1349 my ($file) = @_;
1350
1351 # Temporary hash to store the sids and their state. It will be
1352 # returned at the end of this function.
1353 my %temphash;
1354
1355 # Open the given filename.
1356 open(FILE, "$file") or die "Could not open $file. $!\n";
1357
1358 # Loop through the file.
1359 while(<FILE>) {
1360 # Remove newlines.
1361 chomp $_;
1362
1363 # Skip blank lines.
1364 next if ($_ =~ /^\s*$/);
1365
1366 # Skip coments.
1367 next if ($_ =~ /^\#/);
1368
1369 # Splitt line into sid and state part.
1370 my ($state, $sid) = split(" ", $_);
1371
1372 # Skip line if the sid is not numeric.
1373 next unless ($sid =~ /\d+/ );
1374
1375 # Check if the sid was enabled.
1376 if ($state eq "enablesid") {
1377 # Add the sid and its state as enabled to the temporary hash.
1378 $temphash{$sid} = "enabled";
1379 # Check if the sid was disabled.
1380 } elsif ($state eq "disablesid") {
1381 # Add the sid and its state as disabled to the temporary hash.
1382 $temphash{$sid} = "disabled";
1383 # Invalid state - skip the current sid and state.
1384 } else {
1385 next;
1386 }
1387 }
1388
1389 # Close filehandle.
1390 close(FILE);
1391
1392 # Return the hash.
1393 return %temphash;
1394 }
1395
1396 #
1397 ## Function to get the provider name from the language file or providers file for a given handle.
1398 #
1399 sub get_provider_name($) {
1400 my ($handle) = @_;
1401 my $provider_name;
1402
1403 # Get the required translation string for the given provider handle.
1404 my $tr_string = $IDS::Ruleset::Providers{$handle}{'tr_string'};
1405
1406 # Check if the translation string is available in the language files.
1407 if ($Lang::tr{$tr_string}) {
1408 # Use the translated string from the language file.
1409 $provider_name = $Lang::tr{$tr_string};
1410 } else {
1411 # Fallback and use the provider summary from the providers file.
1412 $provider_name = $IDS::Ruleset::Providers{$handle}{'summary'};
1413 }
1414
1415 # Return the obtained provider name.
1416 return $provider_name;
1417 }
1418
1419 #
1420 ## Private function to convert a given rulefile to a category name.
1421 ## ( No file extension anymore and if the name contained a dot, it
1422 ## would be replaced by a underline sign.)
1423 #
1424 sub _rulefile_to_category($) {
1425 my ($filename) = @_;
1426
1427 # Splitt the filename into single chunks and store them in a
1428 # temorary array.
1429 my @parts = split(/\./, $filename);
1430
1431 # Return / Remove last element of the temporary array.
1432 # This removes the file extension.
1433 pop @parts;
1434
1435 # Join together the single elements of the temporary array.
1436 # If these are more than one, use a "underline" for joining.
1437 my $category = join '_', @parts;
1438
1439 # Return the converted filename.
1440 return $category;
1441 }