ids.cgi: Rework "Enable IPS" section
[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
912d7472 326 # Check if a ruleset is present - if not or the source has been changed download it.
5709768b 327 if((! %idsrules) || ($oldsettings{'RULES'} ne $cgiparams{'RULES'})) {
5fd2e9d6
SS
328 # Check if the red device is active.
329 unless (-e "${General::swroot}/red/active") {
013274d7 330 $errormessage = "$Lang::tr{'could not download latest updates'} - $Lang::tr{'system is offline'}";
5fd2e9d6
SS
331 }
332
333 # Check if enought free disk space is availabe.
334 if(&IDS::checkdiskspace()) {
335 $errormessage = "$Lang::tr{'not enough disk space'}";
336 }
337
338 # Check if any errors happend.
339 unless ($errormessage) {
340 # Lock the webpage and print notice about downloading
341 # a new ruleset.
342 &working_notice("$Lang::tr{'snort working'}");
343
344 # Call subfunction to download the ruleset.
345 if(&IDS::downloadruleset()) {
346 $errormessage = $Lang::tr{'could not download latest updates'};
347
348 # Call function to store the errormessage.
349 &IDS::_store_error_message($errormessage);
350 } else {
351 # Call subfunction to launch oinkmaster.
352 &IDS::oinkmaster();
353 }
354
97870bf2
SS
355 # Check if the IDS is running.
356 if(&IDS::ids_is_running()) {
357 # Call suricatactrl to stop the IDS - because of the changed
358 # ruleset - the use has to configure it before suricata can be
359 # used again.
360 &IDS::call_suricatactrl("stop");
361 }
362
5fd2e9d6
SS
363 # Perform a reload of the page.
364 &reload();
365 }
366 }
367
298723b9 368# Save ruleset.
ea5c8eeb 369} elsif ($cgiparams{'RULESET'} eq $Lang::tr{'update'}) {
d2212836 370 # Arrays to store which rulefiles have been enabled and will be used.
e5738079 371 my @enabled_rulefiles;
298723b9 372
d2212836
SS
373 # Hash to store the user-enabled and disabled sids.
374 my %enabled_disabled_sids;
375
9d18656b
SS
376 # Loop through the hash of idsrules.
377 foreach my $rulefile(keys %idsrules) {
e5738079
SS
378 # Check if the rulefile is enabled.
379 if ($cgiparams{$rulefile} eq "on") {
380 # Add rulefile to the array of enabled rulefiles.
381 push(@enabled_rulefiles, $rulefile);
b65b5ef3
SS
382
383 # Drop item from cgiparams hash.
384 delete $cgiparams{$rulefile};
e5738079 385 }
466c6779 386 }
e5738079 387
d2212836
SS
388 # Read-in the files for enabled/disabled sids.
389 # This will be done by calling the read_enabled_disabled_sids_file function two times
390 # and merge the returned hashes together into the enabled_disabled_sids hash.
391 %enabled_disabled_sids = (
b02e30fd
SS
392 &read_enabled_disabled_sids_file($IDS::disabled_sids_file),
393 &read_enabled_disabled_sids_file($IDS::enabled_sids_file));
d2212836 394
9d18656b
SS
395 # Loop through the hash of idsrules.
396 foreach my $rulefile (keys %idsrules) {
298723b9 397 # Loop through the single rules of the rulefile.
9d18656b 398 foreach my $sid (keys %{$idsrules{$rulefile}}) {
c51a044a
SS
399 # Skip the current sid if it is not numeric.
400 next unless ($sid =~ /\d+/ );
401
298723b9
SS
402 # Check if there exists a key in the cgiparams hash for this sid.
403 if (exists($cgiparams{$sid})) {
404 # Look if the rule is disabled.
9d18656b 405 if ($idsrules{$rulefile}{$sid}{'State'} eq "off") {
298723b9
SS
406 # Check if the state has been set to 'on'.
407 if ($cgiparams{$sid} eq "on") {
d2212836
SS
408 # Add/Modify the sid to/in the enabled_disabled_sids hash.
409 $enabled_disabled_sids{$sid} = "enabled";
298723b9
SS
410
411 # Drop item from cgiparams hash.
60333473 412 delete $cgiparams{$rulefile}{$sid};
298723b9
SS
413 }
414 }
415 } else {
416 # Look if the rule is enabled.
9d18656b 417 if ($idsrules{$rulefile}{$sid}{'State'} eq "on") {
298723b9
SS
418 # Check if the state is 'on' and should be disabled.
419 # In this case there is no entry
420 # for the sid in the cgiparams hash.
d2212836
SS
421 # Add/Modify it to/in the enabled_disabled_sids hash.
422 $enabled_disabled_sids{$sid} = "disabled";
298723b9
SS
423
424 # Drop item from cgiparams hash.
60333473 425 delete $cgiparams{$rulefile}{$sid};
298723b9
SS
426 }
427 }
428 }
429 }
430
37659505 431 # Open enabled sid's file for writing.
b02e30fd 432 open(ENABLED_FILE, ">$IDS::enabled_sids_file") or die "Could not write to $IDS::enabled_sids_file. $!\n";
37659505
SS
433
434 # Open disabled sid's file for writing.
b02e30fd 435 open(DISABLED_FILE, ">$IDS::disabled_sids_file") or die "Could not write to $IDS::disabled_sids_file. $!\n";
d2212836
SS
436
437 # Write header to the files.
438 print ENABLED_FILE "#Autogenerated file. Any custom changes will be overwritten!\n";
439 print DISABLED_FILE "#Autogenerated file. Any custom changes will be overwritten!\n";
440
441 # Check if the hash for enabled/disabled files contains any entries.
442 if (%enabled_disabled_sids) {
443 # Loop through the hash.
444 foreach my $sid (keys %enabled_disabled_sids) {
445 # Check if the sid is enabled.
446 if ($enabled_disabled_sids{$sid} eq "enabled") {
447 # Print the sid to the enabled_sids file.
448 print ENABLED_FILE "enablesid $sid\n";
449 # Check if the sid is disabled.
450 } elsif ($enabled_disabled_sids{$sid} eq "disabled") {
451 # Print the sid to the disabled_sids file.
452 print DISABLED_FILE "disablesid $sid\n";
453 # Something strange happende - skip the current sid.
454 } else {
455 next;
456 }
37659505
SS
457 }
458 }
298723b9 459
d2212836
SS
460 # Close file for enabled_sids after writing.
461 close(ENABLED_FILE);
462
463 # Close file for disabled_sids after writing.
464 close(DISABLED_FILE);
e5738079 465
b02e30fd
SS
466 # Call function to generate and write the used rulefiles file.
467 &IDS::write_used_rulefiles_file(@enabled_rulefiles);
52599865 468
27760092
SS
469 # Lock the webpage and print message.
470 &working_notice("$Lang::tr{'snort working'}");
471
52599865 472 # Call oinkmaster to alter the ruleset.
27760092
SS
473 &IDS::oinkmaster();
474
e2e7880d 475 # Check if the IDS is running.
5a28e721 476 if(&IDS::ids_is_running()) {
e2e7880d
SS
477 # Call suricatactrl to perform a reload.
478 &IDS::call_suricatactrl("reload");
479 }
480
27760092
SS
481 # Reload page.
482 &reload();
52599865
SS
483
484# Download new ruleset.
5fd2e9d6 485} elsif ($cgiparams{'RULESET'} eq $Lang::tr{'update ruleset'}) {
43263ea6
SS
486 # Check if the red device is active.
487 unless (-e "${General::swroot}/red/active") {
013274d7 488 $errormessage = "$Lang::tr{'could not download latest updates'} - $Lang::tr{'system is offline'}";
43263ea6 489 }
52599865 490
3983aebd 491 # Check if enought free disk space is availabe.
434001d0
SS
492 if(&IDS::checkdiskspace()) {
493 $errormessage = "$Lang::tr{'not enough disk space'}";
494 }
52599865 495
43263ea6
SS
496 # Check if any errors happend.
497 unless ($errormessage) {
27760092
SS
498 # Lock the webpage and print notice about downloading
499 # a new ruleset.
500 &working_notice("$Lang::tr{'snort working'}");
3983aebd 501
43263ea6 502 # Call subfunction to download the ruleset.
434001d0
SS
503 if(&IDS::downloadruleset()) {
504 $errormessage = $Lang::tr{'could not download latest updates'};
8f22237b 505
3983aebd 506 # Call function to store the errormessage.
434001d0 507 &IDS::_store_error_message($errormessage);
52599865 508
3983aebd
SS
509 # Preform a reload of the page.
510 &reload();
511 } else {
512 # Call subfunction to launch oinkmaster.
513 &IDS::oinkmaster();
43263ea6 514
e2e7880d 515 # Check if the IDS is running.
5a28e721 516 if(&IDS::ids_is_running()) {
e2e7880d
SS
517 # Call suricatactrl to perform a reload.
518 &IDS::call_suricatactrl("reload");
519 }
520
3983aebd
SS
521 # Perform a reload of the page.
522 &reload();
523 }
52599865 524 }
a232b58c 525# Save snort settings.
e0bfd338 526} elsif ($cgiparams{'IDS'} eq $Lang::tr{'save'}) {
bbb6efae
SS
527 my %oldidssettings;
528 my $reload_page;
ebdd0f9a 529 my $monitored_zones = 0;
bbb6efae
SS
530
531 # Read-in current (old) IDS settings.
b02e30fd 532 &General::readhash("$IDS::ids_settings_file", \%oldidssettings);
bbb6efae 533
a232b58c 534 # Prevent form name from been stored in conf file.
e0bfd338 535 delete $cgiparams{'IDS'};
a232b58c 536
ebdd0f9a
SS
537 # Check if the IDS should be enabled.
538 if ($cgiparams{'ENABLE_IDS'} eq "on") {
539 # Check if any ruleset is available. Otherwise abort and display an error.
540 unless(%idsrules) {
541 $errormessage = $Lang::tr{'ids no ruleset available'};
542 }
543
544 # Loop through the array of available interfaces.
545 foreach my $zone (@network_zones) {
546 # Convert interface name into upper case.
547 my $zone_upper = uc($zone);
548
549 # Check if the IDS is enabled for this interaces.
550 if ($cgiparams{"ENABLE_IDS_$zone_upper"}) {
551 # Increase count.
552 $monitored_zones++;
553 }
554 }
555
556 # Check if at least one zone should be monitored, or show an error.
557 unless ($monitored_zones >= 1) {
558 $errormessage = $Lang::tr{'ids no network zone'};
559 }
560 }
561
a232b58c
SS
562 # Go on if there are no error messages.
563 if (!$errormessage) {
564 # Store settings into settings file.
b02e30fd 565 &General::writehash("$IDS::ids_settings_file", \%cgiparams);
a9a91e5f 566 }
8d2f6b0b
SS
567
568 # Generate file to store the home net.
b02e30fd 569 &IDS::generate_home_net_file();
e2e7880d 570
74cc8f5a
SS
571 # Temporary variable to set the ruleaction.
572 # Default is "drop" to use suricata as IPS.
573 my $ruleaction="drop";
01d02eb6
SS
574
575 # Check if the traffic only should be monitored.
74cc8f5a
SS
576 if($cgiparams{'MONITOR_TRAFFIC_ONLY'} eq 'on') {
577 # Switch the ruleaction to "alert".
578 # Suricata acts as an IDS only.
579 $ruleaction="alert";
01d02eb6 580 }
bbb6efae 581
74cc8f5a
SS
582 # Write the modify sid's file and pass the taken ruleaction.
583 &IDS::write_modify_sids_file($ruleaction);
bbb6efae 584
01d02eb6
SS
585 # Check if "MONITOR_TRAFFIC_ONLY" has been changed.
586 if($cgiparams{'MONITOR_TRAFFIC_ONLY'} ne $oldidssettings{'MONITOR_TRAFFIC_ONLY'}) {
bbb6efae
SS
587 # Check if a ruleset exists.
588 if (%idsrules) {
589 # Lock the webpage and print message.
590 &working_notice("$Lang::tr{'snort working'}");
591
592 # Call oinkmaster to alter the ruleset.
593 &IDS::oinkmaster();
594
595 # Set reload_page to "True".
596 $reload_page="True";
597 }
598 }
599
e2e7880d
SS
600 # Check if the IDS currently is running.
601 if(&IDS::ids_is_running()) {
602 # Check if ENABLE_IDS is set to on.
603 if($cgiparams{'ENABLE_IDS'} eq "on") {
604 # Call suricatactrl to perform a reload of suricata.
605 &IDS::call_suricatactrl("reload");
606 } else {
607 # Call suricatactrl to stop suricata.
608 &IDS::call_suricatactrl("stop");
609 }
610 } else {
611 # Call suricatactrl to start suricata.
612 &IDS::call_suricatactrl("start");
613 }
bbb6efae
SS
614
615 # Check if the page should be reloaded.
616 if ($reload_page) {
617 # Perform a reload of the page.
618 &reload();
619 }
ac1cfefa
MT
620}
621
ea5c8eeb 622# Read-in idssettings and rulesetsettings
b02e30fd
SS
623&General::readhash("$IDS::ids_settings_file", \%idssettings);
624&General::readhash("$IDS::rules_settings_file", \%rulessettings);
1286e0d4 625
ea5c8eeb
SS
626# If no autoupdate intervall has been configured yet, set default value.
627unless(exists($rulessettings{'AUTOUPDATE_INTERVAL'})) {
628 # Set default to "weekly".
629 $rulessettings{'AUTOUPDATE_INTERVAL'} = 'weekly';
630}
631
b7e29743
SS
632# Read-in ignored hosts.
633&General::readhasharray("$IDS::settingsdir/ignored", \%ignored);
634
1286e0d4
SS
635$checked{'ENABLE_IDS'}{'off'} = '';
636$checked{'ENABLE_IDS'}{'on'} = '';
637$checked{'ENABLE_IDS'}{$idssettings{'ENABLE_IDS'}} = "checked='checked'";
01d02eb6
SS
638$checked{'MONITOR_TRAFFIC_ONLY'}{'off'} = '';
639$checked{'MONITOR_TRAFFIC_ONLY'}{'on'} = '';
640$checked{'MONITOR_TRAFFIC_ONLY'}{$idssettings{'MONITOR_TRAFFIC_ONLY'}} = "checked='checked'";
5a3e0dca 641$selected{'RULES'}{'nothing'} = '';
5a3e0dca 642$selected{'RULES'}{'community'} = '';
a0fa489f 643$selected{'RULES'}{'emerging'} = '';
5a3e0dca
MT
644$selected{'RULES'}{'registered'} = '';
645$selected{'RULES'}{'subscripted'} = '';
ea5c8eeb 646$selected{'RULES'}{$rulessettings{'RULES'}} = "selected='selected'";
eadad5fd
SS
647$selected{'AUTOUPDATE_INTERVAL'}{'off'} = '';
648$selected{'AUTOUPDATE_INTERVAL'}{'daily'} = '';
649$selected{'AUTOUPDATE_INTERVAL'}{'weekly'} = '';
ea5c8eeb 650$selected{'AUTOUPDATE_INTERVAL'}{$rulessettings{'AUTOUPDATE_INTERVAL'}} = "selected='selected'";
ac1cfefa
MT
651
652&Header::openpage($Lang::tr{'intrusion detection system'}, 1, '');
653
17726644 654### Java Script ###
e0cec9fe
SS
655print"<script>\n";
656
657# Java script variable declaration for show and hide.
658print"var show = \"$Lang::tr{'ids show'}\"\;\n";
659print"var hide = \"$Lang::tr{'ids hide'}\"\;\n";
660
17726644 661print <<END
029b8ed2
SS
662 // JQuery function to show/hide the text input field for
663 // Oinkcode/Subscription code.
664 \$(function() {
665 \$('#RULES').change(function(){
666 if(\$('#RULES').val() == 'registered') {
667 \$('#code').show();
668 } else if(\$('#RULES').val() == 'subscripted') {
669 \$('#code').show();
670 } else if(\$('#RULES').val() == 'emerging_pro') {
671 \$('#code').show();
672 } else {
673 \$('#code').hide();
674 }
675 });
676 });
677
17726644
SS
678 // Tiny java script function to show/hide the rules
679 // of a given category.
680 function showhide(tblname) {
681 \$("#" + tblname).toggle();
e0cec9fe
SS
682
683 // Get current content of the span element.
684 var content = document.getElementById("span_" + tblname);
685
686 if (content.innerHTML === show) {
687 content.innerHTML = hide;
688 } else {
689 content.innerHTML = show;
690 }
17726644
SS
691 }
692</script>
693END
694;
695
ac1cfefa
MT
696&Header::openbigbox('100%', 'left', '', $errormessage);
697
698if ($errormessage) {
699 &Header::openbox('100%', 'left', $Lang::tr{'error messages'});
700 print "<class name='base'>$errormessage\n";
701 print "&nbsp;</class>\n";
702 &Header::closebox();
703}
704
87660964 705# Draw current state of the IDS
7cc8a0e5 706&Header::openbox('100%', 'left', $Lang::tr{'intrusion detection system'});
1504a375 707
87660964
SS
708# Check if the IDS is running and obtain the process-id.
709my $pid = &IDS::ids_is_running();
710
711# Display some useful information, if suricata daemon is running.
712if ($pid) {
713 # Gather used memory.
714 my $memory = &get_memory_usage($pid);
715
716 print <<END;
717 <table width='95%' cellspacing='0' class='tbl'>
718 <tr>
719 <th bgcolor='$color{'color20'}' colspan='3' align='left'><strong>$Lang::tr{'intrusion detection'}</strong></th>
720 </tr>
721
722 <tr>
723 <td class='base'>$Lang::tr{'guardian daemon'}</td>
724 <td align='center' colspan='2' width='75%' bgcolor='${Header::colourgreen}'><font color='white'><strong>$Lang::tr{'running'}</strong></font></td>
725 </tr>
726
727 <tr>
728 <td class='base'></td>
729 <td bgcolor='$color{'color20'}' align='center'><strong>PID</strong></td>
730 <td bgcolor='$color{'color20'}' align='center'><strong>$Lang::tr{'memory'}</strong></td>
731 </tr>
732
733 <tr>
734 <td class='base'></td>
735 <td bgcolor='$color{'color22'}' align='center'>$pid</td>
736 <td bgcolor='$color{'color22'}' align='center'>$memory KB</td>
737 </tr>
738 </table>
739END
740} else {
741 # Otherwise display a hint that the service is not launched.
742 print <<END;
743 <table width='95%' cellspacing='0' class='tbl'>
744 <tr>
745 <th bgcolor='$color{'color20'}' colspan='3' align='left'><strong>$Lang::tr{'intrusion detection'}</strong></th>
746 </tr>
747
748 <tr>
749 <td class='base'>$Lang::tr{'guardian daemon'}</td>
750 <td align='center' width='75%' bgcolor='${Header::colourred}'><font color='white'><strong>$Lang::tr{'stopped'}</strong></font></td>
751 </tr>
752 </table>
87660964
SS
753END
754}
87660964 755
01d02eb6
SS
756my $rulesdate;
757
758# Check if a ruleset allready has been downloaded.
759if ( -f "$IDS::rulestarball"){
760 # Call stat on the filename to obtain detailed information.
761 my @Info = stat("$IDS::rulestarball");
762
763 # Grab details about the creation time.
764 $rulesdate = localtime($Info[9]);
765}
766
cf02bf2f
SS
767# Only show this area, if a ruleset is present.
768if (%idsrules) {
674912fc 769
cf02bf2f 770 print <<END
674912fc 771
cf02bf2f 772 <br><br><h2>$Lang::tr{'settings'}</h2>
1286e0d4 773
cf02bf2f
SS
774 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
775 <table width='100%' border='0'>
776 <tr>
777 <td class='base' colspan='2'>
9e9b477d 778 <input type='checkbox' name='ENABLE_IDS' $checked{'ENABLE_IDS'}{'on'}>&nbsp$Lang::tr{'ids enable'}
cf02bf2f
SS
779 </td>
780
781 <td class='base' colspan='2'>
9e9b477d 782 <input type='checkbox' name='MONITOR_TRAFFIC_ONLY' $checked{'MONITOR_TRAFFIC_ONLY'}{'on'}>&nbsp$Lang::tr{'ids monitor traffic only'}
1504a375 783 </td>
cf02bf2f 784 </tr>
1504a375 785
cf02bf2f
SS
786 <tr>
787 <td><br><br></td>
788 <td><br><br></td>
789 <td><br><br></td>
790 <td><br><br></td>
791 </tr>
a4ccfcbb 792
cf02bf2f
SS
793 <tr>
794 <td colspan='4'><b>$Lang::tr{'ids monitored interfaces'}</b><br></td>
795 </tr>
a4ccfcbb 796
cf02bf2f 797 <tr>
ac1cfefa
MT
798END
799;
1504a375 800
cf02bf2f
SS
801 # Loop through the array of available networks and print config options.
802 foreach my $zone (@network_zones) {
803 my $checked_input;
804 my $checked_forward;
1504a375 805
cf02bf2f
SS
806 # Convert current zone name to upper case.
807 my $zone_upper = uc($zone);
1504a375 808
cf02bf2f
SS
809 # Set zone name.
810 my $zone_name = $zone;
53817b89 811
cf02bf2f
SS
812 # Dirty hack to get the correct language string for the red zone.
813 if ($zone eq "red") {
814 $zone_name = "red1";
815 }
53817b89 816
cf02bf2f
SS
817 # Grab checkbox status from settings hash.
818 if ($idssettings{"ENABLE_IDS_$zone_upper"} eq "on") {
819 $checked_input = "checked = 'checked'";
820 }
1504a375 821
cf02bf2f
SS
822 print "<td class='base' width='25%'>\n";
823 print "<input type='checkbox' name='ENABLE_IDS_$zone_upper' $checked_input>\n";
824 print "&nbsp$Lang::tr{'enabled on'}<font color='$colourhash{$zone}'> $Lang::tr{$zone_name}</font>\n";
825 print "</td>\n";
826 }
1b73b07e 827
ac1cfefa 828print <<END
cf02bf2f
SS
829 </tr>
830 </table>
1504a375 831
cf02bf2f 832 <br><br>
ea5c8eeb 833
cf02bf2f
SS
834 <table width='100%'>
835 <tr>
836 <td align='right'><input type='submit' name='IDS' value='$Lang::tr{'save'}' /></td>
837 </tr>
838 </table>
839 </form>
ea5c8eeb
SS
840END
841;
842
cf02bf2f
SS
843}
844
ea5c8eeb
SS
845&Header::closebox();
846
847# Draw elements for ruleset configuration.
848&Header::openbox('100%', 'center', $Lang::tr{'ids ruleset settings'});
849
ea5c8eeb
SS
850print <<END
851<form method='post' action='$ENV{'SCRIPT_NAME'}'>
852 <table width='100%' border='0'>
1504a375 853 <tr>
ea5c8eeb
SS
854 <td><b>$Lang::tr{'ids rules update'}</b></td>
855 <td><b>$Lang::tr{'ids automatic rules update'}</b></td>
1504a375
SS
856 </tr>
857
858 <tr>
029b8ed2 859 <td><select name='RULES' id='RULES'>
a0fa489f 860 <option value='emerging' $selected{'RULES'}{'emerging'} >$Lang::tr{'emerging rules'}</option>
34a38438 861 <option value='emerging_pro' $selected{'RULES'}{'emerging_pro'} >$Lang::tr{'emerging pro rules'}</option>
5a3e0dca
MT
862 <option value='community' $selected{'RULES'}{'community'} >$Lang::tr{'community rules'}</option>
863 <option value='registered' $selected{'RULES'}{'registered'} >$Lang::tr{'registered user rules'}</option>
864 <option value='subscripted' $selected{'RULES'}{'subscripted'} >$Lang::tr{'subscripted user rules'}</option>
865 </select>
1504a375 866 </td>
eadad5fd 867
ea5c8eeb 868 <td>
eadad5fd 869 <select name='AUTOUPDATE_INTERVAL'>
b7a9b4ed
SS
870 <option value='off' $selected{'AUTOUPDATE_INTERVAL'}{'off'} >- $Lang::tr{'Disabled'} -</option>
871 <option value='daily' $selected{'AUTOUPDATE_INTERVAL'}{'daily'} >$Lang::tr{'Daily'}</option>
872 <option value='weekly' $selected{'AUTOUPDATE_INTERVAL'}{'weekly'} >$Lang::tr{'Weekly'}</option>
eadad5fd
SS
873 </select>
874 </td>
1504a375
SS
875 </tr>
876
877 <tr>
bc4a2223 878 <td colspan='2'><br><br></td>
1504a375
SS
879 </tr>
880
029b8ed2
SS
881 <tr style='display:none' id='code'>
882 <td colspan='2'>Oinkcode:&nbsp;<input type='text' size='40' name='OINKCODE' value='$rulessettings{'OINKCODE'}'></td>
1504a375
SS
883 </tr>
884
885 <tr>
ea5c8eeb 886 <td>&nbsp;</td>
1504a375 887
ea5c8eeb
SS
888 <td align='right'>
889END
890;
d618d67e 891 # Check if a ruleset has been downloaded yet.
ea5c8eeb 892 if (%idsrules) {
d618d67e 893 # Display button to update the ruleset.
ea5c8eeb 894 print"<input type='submit' name='RULESET' value='$Lang::tr{'update ruleset'}'>\n";
ea5c8eeb
SS
895 }
896print <<END;
897 <input type='submit' name='RULESET' value='$Lang::tr{'save'}'>
898 </td>
1504a375 899
1504a375
SS
900 </tr>
901 </table>
ac1cfefa
MT
902</form>
903END
904;
905
ac1cfefa 906&Header::closebox();
fbfdb241 907
b7e29743
SS
908#
909# Whitelist / Ignorelist
910#
911&Header::openbox('100%', 'center', $Lang::tr{'guardian ignored hosts'});
912
913print <<END;
914 <table width='100%'>
915 <tr>
916 <td class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'ip address'}</b></td>
917 <td class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'remark'}</b></td>
918 <td class='base' colspan='3' bgcolor='$color{'color20'}'></td>
919 </tr>
920END
921 # Check if some hosts have been added to be ignored.
922 if (keys (%ignored)) {
923 my $col = "";
924
925 # Loop through all entries of the hash.
926 while( (my $key) = each %ignored) {
927 # Assign data array positions to some nice variable names.
928 my $address = $ignored{$key}[0];
929 my $remark = $ignored{$key}[1];
930 my $status = $ignored{$key}[2];
931
932 # Check if the key (id) number is even or not.
933 if ($cgiparams{'ID'} eq $key) {
934 $col="bgcolor='${Header::colouryellow}'";
935 } elsif ($key % 2) {
936 $col="bgcolor='$color{'color22'}'";
937 } else {
938 $col="bgcolor='$color{'color20'}'";
939 }
940
941 # Choose icon for the checkbox.
942 my $gif;
943 my $gdesc;
944
945 # Check if the status is enabled and select the correct image and description.
946 if ($status eq 'enabled' ) {
947 $gif = 'on.gif';
948 $gdesc = $Lang::tr{'click to disable'};
949 } else {
950 $gif = 'off.gif';
951 $gdesc = $Lang::tr{'click to enable'};
952 }
953
954print <<END;
955 <tr>
956 <td width='20%' class='base' $col>$address</td>
957 <td width='65%' class='base' $col>$remark</td>
958
959 <td align='center' $col>
960 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
961 <input type='hidden' name='WHITELIST' value='$Lang::tr{'toggle enable disable'}' />
962 <input type='image' name='$Lang::tr{'toggle enable disable'}' src='/images/$gif' alt='$gdesc' title='$gdesc' />
963 <input type='hidden' name='ID' value='$key' />
964 </form>
965 </td>
966
967 <td align='center' $col>
968 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
969 <input type='hidden' name='WHITELIST' value='$Lang::tr{'edit'}' />
970 <input type='image' name='$Lang::tr{'edit'}' src='/images/edit.gif' alt='$Lang::tr{'edit'}' title='$Lang::tr{'edit'}' />
971 <input type='hidden' name='ID' value='$key' />
972 </form>
973 </td>
974
975 <td align='center' $col>
976 <form method='post' name='$key' action='$ENV{'SCRIPT_NAME'}'>
977 <input type='image' name='$Lang::tr{'remove'}' src='/images/delete.gif' title='$Lang::tr{'remove'}' alt='$Lang::tr{'remove'}'>
978 <input type='hidden' name='ID' value='$key'>
979 <input type='hidden' name='WHITELIST' value='$Lang::tr{'remove'}'>
980 </form>
981 </td>
982 </tr>
983END
984 }
985 } else {
986 # Print notice that currently no hosts are ignored.
987 print "<tr>\n";
988 print "<td class='base' colspan='2'>$Lang::tr{'guardian no entries'}</td>\n";
989 print "</tr>\n";
990 }
991
992 print "</table>\n";
993
994 # Section to add new elements or edit existing ones.
995print <<END;
996 <br>
997 <hr>
998 <br>
999
1000 <div align='center'>
1001 <table width='100%'>
1002END
1003
1004 # Assign correct headline and button text.
1005 my $buttontext;
1006 my $entry_address;
1007 my $entry_remark;
1008
1009 # Check if an ID (key) has been given, in this case an existing entry should be edited.
1010 if ($cgiparams{'ID'} ne '') {
1011 $buttontext = $Lang::tr{'update'};
1012 print "<tr><td class='boldbase' colspan='3'><b>$Lang::tr{'update'}</b></td></tr>\n";
1013
1014 # Grab address and remark for the given key.
1015 $entry_address = $ignored{$cgiparams{'ID'}}[0];
1016 $entry_remark = $ignored{$cgiparams{'ID'}}[1];
1017 } else {
1018 $buttontext = $Lang::tr{'add'};
1019 print "<tr><td class='boldbase' colspan='3'><b>$Lang::tr{'dnsforward add a new entry'}</b></td></tr>\n";
1020 }
1021
1022print <<END;
1023 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
1024 <input type='hidden' name='ID' value='$cgiparams{'ID'}'>
1025 <tr>
1026 <td width='30%'>$Lang::tr{'ip address'}: </td>
1027 <td width='50%'><input type='text' name='IGNORE_ENTRY_ADDRESS' value='$entry_address' size='24' /></td>
1028
1029 <td width='30%'>$Lang::tr{'remark'}: </td>
1030 <td wicth='50%'><input type='text' name=IGNORE_ENTRY_REMARK value='$entry_remark' size='24' /></td>
1031 <td align='center' width='20%'><input type='submit' name='WHITELIST' value='$buttontext' /></td>
1032 </tr>
1033 </form>
1034 </table>
1035 </div>
1036END
1037
1038&Header::closebox();
1039
80bcd4dd
SS
1040# Only show the section for configuring the ruleset if one is present.
1041if (%idsrules) {
1042 &Header::openbox('100%', 'LEFT', $Lang::tr{'intrusion detection system rules'});
298723b9 1043
80bcd4dd 1044 print"<form method='POST' action='$ENV{'SCRIPT_NAME'}'>\n";
ce0e83b3 1045
80bcd4dd
SS
1046 # Output display table for rule files
1047 print "<table width='100%'>\n";
f7fcd1c0 1048
80bcd4dd
SS
1049 # Loop over each rule file
1050 foreach my $rulefile (sort keys(%idsrules)) {
1051 my $rulechecked = '';
3ffee04b 1052
80bcd4dd
SS
1053 # Check if rule file is enabled
1054 if ($idsrules{$rulefile}{'Rulefile'}{'State'} eq 'on') {
1055 $rulechecked = 'CHECKED';
1056 }
1057
0a1bba1a
SS
1058 # Convert rulefile name into category name.
1059 my $categoryname = &_rulefile_to_category($rulefile);
1060
80bcd4dd
SS
1061 # Table and rows for the rule files.
1062 print"<tr>\n";
1063 print"<td class='base' width='5%'>\n";
1064 print"<input type='checkbox' name='$rulefile' $rulechecked>\n";
1065 print"</td>\n";
1066 print"<td class='base' width='90%'><b>$rulefile</b></td>\n";
1067 print"<td class='base' width='5%' align='right'>\n";
e0cec9fe 1068 print"<a href=\"javascript:showhide('$categoryname')\"><span id='span_$categoryname'>$Lang::tr{'ids show'}</span></a>\n";
80bcd4dd
SS
1069 print"</td>\n";
1070 print"</tr>\n";
1071
1072 # Rows which will be hidden per default and will contain the single rules.
0a1bba1a 1073 print"<tr style='display:none' id='$categoryname'>\n";
80bcd4dd 1074 print"<td colspan='3'>\n";
17726644 1075
f7fcd1c0 1076 # Local vars
80bcd4dd
SS
1077 my $lines;
1078 my $rows;
1079 my $col;
f9c2147d 1080
80bcd4dd
SS
1081 # New table for the single rules.
1082 print "<table width='100%'>\n";
e5738079 1083
80bcd4dd
SS
1084 # Loop over rule file rules
1085 foreach my $sid (sort {$a <=> $b} keys(%{$idsrules{$rulefile}})) {
1086 # Local vars
1087 my $ruledefchecked = '';
3ffee04b 1088
80bcd4dd
SS
1089 # Skip rulefile itself.
1090 next if ($sid eq "Rulefile");
f7fcd1c0 1091
80bcd4dd
SS
1092 # If 2 rules have been displayed, start a new row
1093 if (($lines % 2) == 0) {
1094 print "</tr><tr>\n";
1095
1096 # Increase rows by once.
1097 $rows++;
1098 }
1099
1100 # Colour lines.
1101 if ($rows % 2) {
1102 $col="bgcolor='$color{'color20'}'";
1103 } else {
1104 $col="bgcolor='$color{'color22'}'";
1105 }
f7fcd1c0 1106
80bcd4dd
SS
1107 # Set rule state
1108 if ($idsrules{$rulefile}{$sid}{'State'} eq 'on') {
1109 $ruledefchecked = 'CHECKED';
1110 }
1111
1112 # Create rule checkbox and display rule description
1113 print "<td class='base' width='5%' align='right' $col>\n";
1114 print "<input type='checkbox' NAME='$sid' $ruledefchecked>\n";
1115 print "</td>\n";
1116 print "<td class='base' width='45%' $col>$idsrules{$rulefile}{$sid}{'Description'}</td>";
1117
1118 # Increment rule count
1119 $lines++;
f7fcd1c0 1120 }
3ffee04b 1121
80bcd4dd
SS
1122 # If do not have a second rule for row, create empty cell
1123 if (($lines % 2) != 0) {
1124 print "<td class='base'></td>";
1125 }
17726644 1126
80bcd4dd
SS
1127 # Close display table
1128 print "</tr></table></td></tr>";
f7fcd1c0
SS
1129 }
1130
1131 # Close display table
80bcd4dd 1132 print "</table>";
17726644 1133
f7fcd1c0 1134print <<END
2999f1d2
CS
1135<table width='100%'>
1136<tr>
298723b9 1137 <td width='100%' align='right'><input type='submit' name='RULESET' value='$Lang::tr{'update'}'>
3ffee04b
CS
1138 &nbsp; <!-- space for future online help link -->
1139 </td>
2999f1d2
CS
1140</tr>
1141</table>
298723b9 1142</form>
3ffee04b
CS
1143END
1144;
80bcd4dd
SS
1145 &Header::closebox();
1146}
1147
ac1cfefa
MT
1148&Header::closebigbox();
1149&Header::closepage();
1150
27760092
SS
1151#
1152## A function to display a notice, to lock the webpage and
1153## tell the user which action currently will be performed.
1154#
1155sub working_notice ($) {
1156 my ($message) = @_;
1157
1158 &Header::openpage($Lang::tr{'intrusion detection system'}, 1, '');
1159 &Header::openbigbox('100%', 'left', '', $errormessage);
1160 &Header::openbox( 'Waiting', 1,);
1161 print <<END;
1162 <table>
1163 <tr>
1164 <td><img src='/images/indicator.gif' alt='$Lang::tr{'aktiv'}' /></td>
1165 <td>$message</td>
1166 </tr>
1167 </table>
1168END
1169 &Header::closebox();
1170 &Header::closebigbox();
1171 &Header::closepage();
1172}
1173
3983aebd
SS
1174#
1175## A tiny function to perform a reload of the webpage after one second.
1176#
1177sub reload () {
1178 print "<meta http-equiv='refresh' content='1'>\n";
1179
1180 # Stop the script.
1181 exit;
a70d269a
SS
1182}
1183
25f5cb0d
SS
1184#
1185## Private function to read-in and parse rules of a given rulefile.
1186#
1187## The given file will be read, parsed and all valid rules will be stored by ID,
9d18656b 1188## message/description and it's state in the idsrules hash.
25f5cb0d 1189#
3da6e01b
SS
1190sub readrulesfile ($) {
1191 my $rulefile = shift;
1192
1193 # Open rule file and read in contents
298ef5ba 1194 open(RULEFILE, "$IDS::rulespath/$rulefile") or die "Unable to read $rulefile!";
3da6e01b
SS
1195
1196 # Store file content in an array.
1197 my @lines = <RULEFILE>;
1198
1199 # Close file.
1200 close(RULEFILE);
1201
1202 # Loop over rule file contents
1203 foreach my $line (@lines) {
1204 # Remove whitespaces.
1205 chomp $line;
1206
1207 # Skip blank lines.
1208 next if ($line =~ /^\s*$/);
1209
1210 # Local vars.
1211 my $sid;
1212 my $msg;
1213
1214 # Gather rule sid and message from the ruleline.
1215 if ($line =~ m/.*msg:\"(.*?)\"\; .* sid:(.*?); /) {
1216 $msg = $1;
1217 $sid = $2;
1218
1219 # Check if a rule has been found.
1220 if ($sid && $msg) {
9d18656b
SS
1221 # Add rule to the idsrules hash.
1222 $idsrules{$rulefile}{$sid}{'Description'} = $msg;
3da6e01b
SS
1223
1224 # Grab status of the rule. Check if ruleline starts with a "dash".
1225 if ($line =~ /^\#/) {
1226 # If yes, the rule is disabled.
9d18656b 1227 $idsrules{$rulefile}{$sid}{'State'} = "off";
3da6e01b
SS
1228 } else {
1229 # Otherwise the rule is enabled.
9d18656b 1230 $idsrules{$rulefile}{$sid}{'State'} = "on";
3da6e01b
SS
1231 }
1232 }
1233 }
b7e29743 1234 }
3da6e01b 1235}
87660964 1236
8d2f6b0b
SS
1237#
1238## Function to get the used memory of a given process-id.
1239#
87660964 1240sub get_memory_usage($) {
004b13b7 1241 my ($pid) = @_;
87660964 1242
004b13b7 1243 my $memory = 0;
87660964 1244
004b13b7 1245 # Try to open the status file for the given process-id on the pseudo
87660964 1246 # file system proc.
004b13b7
SS
1247 if (open(FILE, "/proc/$pid/status")) {
1248 # Loop through the entire file.
1249 while (<FILE>) {
1250 # Splitt current line content and store them into variables.
1251 my ($key, $value) = split(":", $_, 2);
1252
1253 # Check if the current key is the one which contains the memory usage.
1254 # The wanted one is VmRSS which contains the Real-memory (resident set)
1255 # of the entire process.
1256 if ($key eq "VmRSS") {
1257 # Found the memory usage add it to the memory variable.
1258 $memory += $value;
1259
1260 # Break the loop.
1261 last;
1262 }
1263 }
87660964
SS
1264
1265 # Close file handle.
004b13b7 1266 close(FILE);
87660964
SS
1267
1268 # Return memory usage.
1269 return $memory;
004b13b7 1270 }
87660964
SS
1271
1272 # If the file could not be open, return nothing.
1273 return;
1274}
1275
a5d61752
SS
1276#
1277## Function to read-in the given enabled or disables sids file.
1278#
1279sub read_enabled_disabled_sids_file($) {
1280 my ($file) = @_;
1281
1282 # Temporary hash to store the sids and their state. It will be
1283 # returned at the end of this function.
1284 my %temphash;
1285
1286 # Open the given filename.
1287 open(FILE, "$file") or die "Could not open $file. $!\n";
1288
1289 # Loop through the file.
1290 while(<FILE>) {
1291 # Remove newlines.
1292 chomp $_;
1293
1294 # Skip blank lines.
1295 next if ($_ =~ /^\s*$/);
1296
1297 # Skip coments.
1298 next if ($_ =~ /^\#/);
1299
1300 # Splitt line into sid and state part.
1301 my ($state, $sid) = split(" ", $_);
1302
1303 # Skip line if the sid is not numeric.
1304 next unless ($sid =~ /\d+/ );
1305
1306 # Check if the sid was enabled.
1307 if ($state eq "enablesid") {
1308 # Add the sid and its state as enabled to the temporary hash.
1309 $temphash{$sid} = "enabled";
1310 # Check if the sid was disabled.
1311 } elsif ($state eq "disablesid") {
1312 # Add the sid and its state as disabled to the temporary hash.
1313 $temphash{$sid} = "disabled";
1314 # Invalid state - skip the current sid and state.
1315 } else {
1316 next;
1317 }
1318 }
1319
1320 # Close filehandle.
1321 close(FILE);
1322
1323 # Return the hash.
1324 return %temphash;
1325}
0a1bba1a
SS
1326
1327#
1328## Private function to convert a given rulefile to a category name.
1329## ( No file extension anymore and if the name contained a dot, it
1330## would be replaced by a underline sign.)
1331#
1332sub _rulefile_to_category($) {
1333 my ($filename) = @_;
1334
1335 # Splitt the filename into single chunks and store them in a
1336 # temorary array.
1337 my @parts = split(/\./, $filename);
1338
1339 # Return / Remove last element of the temporary array.
1340 # This removes the file extension.
1341 pop @parts;
1342
1343 # Join together the single elements of the temporary array.
1344 # If these are more than one, use a "underline" for joining.
1345 my $category = join '_', @parts;
1346
1347 # Return the converted filename.
1348 return $category;
1349}