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