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