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