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