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