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