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