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