]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blame - html/cgi-bin/ids.cgi
ids.cgi: Read-in providers settings file when neccessary.
[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 #
e698090e 5# Copyright (C) 2007-2020 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";
abffcc99 32require "${General::swroot}/network-functions.pl";
ac1cfefa 33
3e12c6e6
SS
34# Import ruleset providers file.
35require "$IDS::rulesetsourcesfile";
36
f2fdd0c1
CS
37my %color = ();
38my %mainsettings = ();
9d18656b 39my %idsrules = ();
1286e0d4 40my %idssettings=();
2f252efa 41my %used_providers=();
298723b9 42my %cgiparams=();
ac1cfefa 43my %checked=();
5a3e0dca 44my %selected=();
b7e29743 45my %ignored=();
0b89daee
SS
46
47# Read-in main settings, for language, theme and colors.
48&General::readhash("${General::swroot}/main/settings", \%mainsettings);
8186b372 49&General::readhash("/srv/web/ipfire/html/themes/ipfire/include/colors.txt", \%color);
0b89daee 50
1286e0d4
SS
51# Get the available network zones, based on the config type of the system and store
52# the list of zones in an array.
abffcc99 53my @network_zones = &Network::get_available_network_zones();
ac1cfefa 54
51b63b41
SS
55# Check if openvpn is started and add it to the array of network zones.
56if ( -e "/var/run/openvpn.pid") {
57 push(@network_zones, "ovpn");
58}
59
43263ea6
SS
60my $errormessage;
61
00512a5a 62# Create files if they does not exist yet.
b02e30fd 63&IDS::check_and_create_filelayout();
01ba4be4 64
99b372b5
SS
65# Hash which contains the colour code of a network zone.
66my %colourhash = (
67 'red' => $Header::colourred,
68 'green' => $Header::colourgreen,
69 'blue' => $Header::colourblue,
51b63b41
SS
70 'orange' => $Header::colourorange,
71 'ovpn' => $Header::colourovpn
99b372b5
SS
72);
73
ac1cfefa
MT
74&Header::showhttpheaders();
75
298723b9
SS
76#Get GUI values
77&Header::getcgihash(\%cgiparams);
ac1cfefa 78
b7e29743
SS
79## Add/edit an entry to the ignore file.
80#
81if (($cgiparams{'WHITELIST'} eq $Lang::tr{'add'}) || ($cgiparams{'WHITELIST'} eq $Lang::tr{'update'})) {
82
83 # Check if any input has been performed.
84 if ($cgiparams{'IGNORE_ENTRY_ADDRESS'} ne '') {
85
86 # Check if the given input is no valid IP-address or IP-address with subnet, display an error message.
87 if ((!&General::validip($cgiparams{'IGNORE_ENTRY_ADDRESS'})) && (!&General::validipandmask($cgiparams{'IGNORE_ENTRY_ADDRESS'}))) {
88 $errormessage = "$Lang::tr{'guardian invalid address or subnet'}";
89 }
90 } else {
91 $errormessage = "$Lang::tr{'guardian empty input'}";
92 }
93
94 # Go further if there was no error.
95 if ($errormessage eq '') {
96 my %ignored = ();
97 my $id;
98 my $status;
99
100 # Assign hash values.
101 my $new_entry_address = $cgiparams{'IGNORE_ENTRY_ADDRESS'};
102 my $new_entry_remark = $cgiparams{'IGNORE_ENTRY_REMARK'};
103
104 # Read-in ignoredfile.
b02e30fd 105 &General::readhasharray($IDS::ignored_file, \%ignored);
b7e29743
SS
106
107 # Check if we should edit an existing entry and got an ID.
108 if (($cgiparams{'WHITELIST'} eq $Lang::tr{'update'}) && ($cgiparams{'ID'})) {
109 # Assin the provided id.
110 $id = $cgiparams{'ID'};
111
112 # Undef the given ID.
113 undef($cgiparams{'ID'});
114
115 # Grab the configured status of the corresponding entry.
116 $status = $ignored{$id}[2];
117 } else {
118 # Each newly added entry automatically should be enabled.
119 $status = "enabled";
120
121 # Generate the ID for the new entry.
122 #
123 # Sort the keys by their ID and store them in an array.
124 my @keys = sort { $a <=> $b } keys %ignored;
125
126 # Reverse the key array.
127 my @reversed = reverse(@keys);
128
129 # Obtain the last used id.
130 my $last_id = @reversed[0];
131
132 # Increase the last id by one and use it as id for the new entry.
133 $id = ++$last_id;
134 }
135
136 # Add/Modify the entry to/in the ignored hash.
137 $ignored{$id} = ["$new_entry_address", "$new_entry_remark", "$status"];
138
139 # Write the changed ignored hash to the ignored file.
b02e30fd 140 &General::writehasharray($IDS::ignored_file, \%ignored);
b7e29743
SS
141
142 # Regenerate the ignore file.
9283e9b9 143 &IDS::generate_ignore_file();
b7e29743
SS
144 }
145
146 # Check if the IDS is running.
147 if(&IDS::ids_is_running()) {
148 # Call suricatactrl to perform a reload.
149 &IDS::call_suricatactrl("reload");
150 }
151
152## Toggle Enabled/Disabled for an existing entry on the ignore list.
153#
154
155} elsif ($cgiparams{'WHITELIST'} eq $Lang::tr{'toggle enable disable'}) {
156 my %ignored = ();
157
158 # Only go further, if an ID has been passed.
159 if ($cgiparams{'ID'}) {
160 # Assign the given ID.
161 my $id = $cgiparams{'ID'};
162
163 # Undef the given ID.
164 undef($cgiparams{'ID'});
165
166 # Read-in ignoredfile.
b02e30fd 167 &General::readhasharray($IDS::ignored_file, \%ignored);
b7e29743
SS
168
169 # Grab the configured status of the corresponding entry.
170 my $status = $ignored{$id}[2];
171
172 # Switch the status.
173 if ($status eq "disabled") {
174 $status = "enabled";
175 } else {
176 $status = "disabled";
177 }
178
179 # Modify the status of the existing entry.
180 $ignored{$id} = ["$ignored{$id}[0]", "$ignored{$id}[1]", "$status"];
181
182 # Write the changed ignored hash to the ignored file.
b02e30fd 183 &General::writehasharray($IDS::ignored_file, \%ignored);
b7e29743
SS
184
185 # Regenerate the ignore file.
9283e9b9 186 &IDS::generate_ignore_file();
b7e29743
SS
187
188 # Check if the IDS is running.
189 if(&IDS::ids_is_running()) {
190 # Call suricatactrl to perform a reload.
191 &IDS::call_suricatactrl("reload");
192 }
193 }
194
195## Remove entry from ignore list.
196#
197} elsif ($cgiparams{'WHITELIST'} eq $Lang::tr{'remove'}) {
198 my %ignored = ();
199
200 # Read-in ignoredfile.
b02e30fd 201 &General::readhasharray($IDS::ignored_file, \%ignored);
b7e29743
SS
202
203 # Drop entry from the hash.
204 delete($ignored{$cgiparams{'ID'}});
205
206 # Undef the given ID.
207 undef($cgiparams{'ID'});
208
209 # Write the changed ignored hash to the ignored file.
b02e30fd 210 &General::writehasharray($IDS::ignored_file, \%ignored);
b7e29743
SS
211
212 # Regenerate the ignore file.
9283e9b9 213 &IDS::generate_ignore_file();
b7e29743
SS
214
215 # Check if the IDS is running.
216 if(&IDS::ids_is_running()) {
217 # Call suricatactrl to perform a reload.
218 &IDS::call_suricatactrl("reload");
219 }
220}
221
9074e3d7
SS
222# Check if the page is locked, in this case, the ids_page_lock_file exists.
223if (-e $IDS::ids_page_lock_file) {
224 # Lock the webpage and print notice about autoupgrade of the ruleset
225 # is in progess.
226 &working_notice("$Lang::tr{'ids ruleset autoupdate in progress'}");
227
228 # Loop and check if the file still exists.
229 while(-e $IDS::ids_page_lock_file) {
230 # Sleep for a second and re-check.
231 sleep 1;
232 }
233
234 # Page has been unlocked, perform a reload.
235 &reload();
236}
237
3983aebd
SS
238# Check if any error has been stored.
239if (-e $IDS::storederrorfile) {
240 # Open file to read in the stored error message.
241 open(FILE, "<$IDS::storederrorfile") or die "Could not open $IDS::storederrorfile. $!\n";
242
243 # Read the stored error message.
244 $errormessage = <FILE>;
245
246 # Close file.
247 close (FILE);
248
249 # Delete the file, which is now not longer required.
250 unlink($IDS::storederrorfile);
251}
252
a468b62b
SS
253# Gather ruleset details.
254if ($cgiparams{'RULESET'}) {
255 ## Grab all available rules and store them in the idsrules hash.
256 #
257 # Open rules directory and do a directory listing.
258 opendir(DIR, $IDS::rulespath) or die $!;
259 # Loop through the direcory.
260 while (my $file = readdir(DIR)) {
422204ff 261
a468b62b
SS
262 # We only want files.
263 next unless (-f "$IDS::rulespath/$file");
422204ff 264
a468b62b
SS
265 # Ignore empty files.
266 next if (-z "$IDS::rulespath/$file");
422204ff 267
a468b62b
SS
268 # Use a regular expression to find files ending in .rules
269 next unless ($file =~ m/\.rules$/);
422204ff 270
a468b62b
SS
271 # Ignore files which are not read-able.
272 next unless (-R "$IDS::rulespath/$file");
395e3b90 273
a468b62b
SS
274 # Skip whitelist rules file.
275 next if( $file eq "whitelist.rules");
b7e29743 276
a468b62b
SS
277 # Call subfunction to read-in rulefile and add rules to
278 # the idsrules hash.
279 &readrulesfile("$file");
280 }
395e3b90 281
a468b62b 282 closedir(DIR);
395e3b90 283
a468b62b
SS
284 # Gather used rulefiles.
285 #
286 # Check if the file for activated rulefiles is not empty.
287 if(-f $IDS::used_rulefiles_file) {
288 # Open the file for used rulefile and read-in content.
289 open(FILE, $IDS::used_rulefiles_file) or die "Could not open $IDS::used_rulefiles_file. $!\n";
290
291 # Read-in content.
292 my @lines = <FILE>;
293
294 # Close file.
295 close(FILE);
296
297 # Loop through the array.
298 foreach my $line (@lines) {
299 # Remove newlines.
300 chomp($line);
301
302 # Skip comments.
303 next if ($line =~ /\#/);
304
305 # Skip blank lines.
306 next if ($line =~ /^\s*$/);
307
308 # Gather rule sid and message from the ruleline.
309 if ($line =~ /.*- (.*)/) {
310 my $rulefile = $1;
311
312 # Check if the current rulefile exists in the %idsrules hash.
313 # If not, the file probably does not exist anymore or contains
314 # no rules.
315 if($idsrules{$rulefile}) {
316 # Add the rulefile state to the %idsrules hash.
317 $idsrules{$rulefile}{'Rulefile'}{'State'} = "on";
318 }
43ab7d9c 319 }
e5738079
SS
320 }
321 }
322}
323
ea5c8eeb
SS
324# Save ruleset configuration.
325if ($cgiparams{'RULESET'} eq $Lang::tr{'save'}) {
326 my %oldsettings;
327
328 # Read-in current (old) IDS settings.
b02e30fd 329 &General::readhash("$IDS::rules_settings_file", \%oldsettings);
ea5c8eeb
SS
330
331 # Prevent form name from been stored in conf file.
332 delete $cgiparams{'RULESET'};
333
613f58fb 334 # Check if the choosen vendor (URL) requires an subscription/oinkcode.
3e12c6e6 335 if ($IDS::Ruleset::Providers{$cgiparams{'RULES'}}{'requires_subscription'} eq "True") {
613f58fb
SS
336 # Check if an subscription/oinkcode has been provided.
337 if ($cgiparams{'OINKCODE'}) {
338 # Check if the oinkcode contains unallowed chars.
339 unless ($cgiparams{'OINKCODE'} =~ /^[a-z0-9]+$/) {
340 $errormessage = $Lang::tr{'invalid input for oink code'};
341 }
342 } else {
343 # Print an error message, that an subsription/oinkcode is required for this
344 # vendor.
345 $errormessage = $Lang::tr{'ids oinkcode required'};
ea5c8eeb
SS
346 }
347 }
348
349 # Go on if there are no error messages.
350 if (!$errormessage) {
351 # Store settings into settings file.
b02e30fd 352 &General::writehash("$IDS::rules_settings_file", \%cgiparams);
ea5c8eeb 353
f644a167
SS
354 # Check if a ruleset is present - if not or the source has been changed download it.
355 if((! %idsrules) || ($oldsettings{'RULES'} ne $cgiparams{'RULES'})) {
356 # Check if the red device is active.
357 unless (-e "${General::swroot}/red/active") {
358 $errormessage = "$Lang::tr{'could not download latest updates'} - $Lang::tr{'system is offline'}";
5fd2e9d6
SS
359 }
360
a5ba473c 361 # Check if enough free disk space is availabe.
f644a167
SS
362 if(&IDS::checkdiskspace()) {
363 $errormessage = "$Lang::tr{'not enough disk space'}";
97870bf2
SS
364 }
365
f644a167
SS
366 # Check if any errors happend.
367 unless ($errormessage) {
368 # Lock the webpage and print notice about downloading
369 # a new ruleset.
5bd8940d 370 &working_notice("$Lang::tr{'ids working'}");
f644a167 371
a5ba473c 372 # Write the modify sid's file and pass the taken ruleaction.
81bae51f 373 &IDS::write_modify_sids_file();
a5ba473c 374
f644a167
SS
375 # Call subfunction to download the ruleset.
376 if(&IDS::downloadruleset()) {
377 $errormessage = $Lang::tr{'could not download latest updates'};
378
379 # Call function to store the errormessage.
380 &IDS::_store_error_message($errormessage);
381 } else {
382 # Call subfunction to launch oinkmaster.
383 &IDS::oinkmaster();
384 }
385
386 # Check if the IDS is running.
387 if(&IDS::ids_is_running()) {
388 # Call suricatactrl to stop the IDS - because of the changed
389 # ruleset - the use has to configure it before suricata can be
390 # used again.
391 &IDS::call_suricatactrl("stop");
392 }
393
394 # Perform a reload of the page.
395 &reload();
396 }
5fd2e9d6
SS
397 }
398 }
399
298723b9 400# Save ruleset.
ee7fe87e 401} elsif ($cgiparams{'RULESET'} eq $Lang::tr{'ids apply'}) {
d2212836 402 # Arrays to store which rulefiles have been enabled and will be used.
e5738079 403 my @enabled_rulefiles;
298723b9 404
d2212836
SS
405 # Hash to store the user-enabled and disabled sids.
406 my %enabled_disabled_sids;
407
af8e5145
SS
408 # Store if a restart of suricata is required.
409 my $suricata_restart_required;
410
9d18656b
SS
411 # Loop through the hash of idsrules.
412 foreach my $rulefile(keys %idsrules) {
1622e5c1
SS
413 # Check if the state of the rulefile has been changed.
414 unless ($cgiparams{$rulefile} eq $idsrules{$rulefile}{'Rulefile'}{'State'}) {
415 # A restart of suricata is required to apply the changes of the used rulefiles.
416 $suricata_restart_required = 1;
417 }
418
e5738079
SS
419 # Check if the rulefile is enabled.
420 if ($cgiparams{$rulefile} eq "on") {
421 # Add rulefile to the array of enabled rulefiles.
422 push(@enabled_rulefiles, $rulefile);
b65b5ef3
SS
423
424 # Drop item from cgiparams hash.
425 delete $cgiparams{$rulefile};
e5738079 426 }
466c6779 427 }
e5738079 428
d2212836
SS
429 # Read-in the files for enabled/disabled sids.
430 # This will be done by calling the read_enabled_disabled_sids_file function two times
431 # and merge the returned hashes together into the enabled_disabled_sids hash.
432 %enabled_disabled_sids = (
b02e30fd
SS
433 &read_enabled_disabled_sids_file($IDS::disabled_sids_file),
434 &read_enabled_disabled_sids_file($IDS::enabled_sids_file));
d2212836 435
9d18656b
SS
436 # Loop through the hash of idsrules.
437 foreach my $rulefile (keys %idsrules) {
298723b9 438 # Loop through the single rules of the rulefile.
9d18656b 439 foreach my $sid (keys %{$idsrules{$rulefile}}) {
c51a044a
SS
440 # Skip the current sid if it is not numeric.
441 next unless ($sid =~ /\d+/ );
442
298723b9
SS
443 # Check if there exists a key in the cgiparams hash for this sid.
444 if (exists($cgiparams{$sid})) {
445 # Look if the rule is disabled.
9d18656b 446 if ($idsrules{$rulefile}{$sid}{'State'} eq "off") {
298723b9
SS
447 # Check if the state has been set to 'on'.
448 if ($cgiparams{$sid} eq "on") {
d2212836
SS
449 # Add/Modify the sid to/in the enabled_disabled_sids hash.
450 $enabled_disabled_sids{$sid} = "enabled";
298723b9
SS
451
452 # Drop item from cgiparams hash.
60333473 453 delete $cgiparams{$rulefile}{$sid};
298723b9
SS
454 }
455 }
456 } else {
457 # Look if the rule is enabled.
9d18656b 458 if ($idsrules{$rulefile}{$sid}{'State'} eq "on") {
298723b9
SS
459 # Check if the state is 'on' and should be disabled.
460 # In this case there is no entry
461 # for the sid in the cgiparams hash.
d2212836
SS
462 # Add/Modify it to/in the enabled_disabled_sids hash.
463 $enabled_disabled_sids{$sid} = "disabled";
298723b9
SS
464
465 # Drop item from cgiparams hash.
60333473 466 delete $cgiparams{$rulefile}{$sid};
298723b9
SS
467 }
468 }
469 }
470 }
471
37659505 472 # Open enabled sid's file for writing.
b02e30fd 473 open(ENABLED_FILE, ">$IDS::enabled_sids_file") or die "Could not write to $IDS::enabled_sids_file. $!\n";
37659505
SS
474
475 # Open disabled sid's file for writing.
b02e30fd 476 open(DISABLED_FILE, ">$IDS::disabled_sids_file") or die "Could not write to $IDS::disabled_sids_file. $!\n";
d2212836
SS
477
478 # Write header to the files.
479 print ENABLED_FILE "#Autogenerated file. Any custom changes will be overwritten!\n";
480 print DISABLED_FILE "#Autogenerated file. Any custom changes will be overwritten!\n";
481
482 # Check if the hash for enabled/disabled files contains any entries.
483 if (%enabled_disabled_sids) {
484 # Loop through the hash.
485 foreach my $sid (keys %enabled_disabled_sids) {
486 # Check if the sid is enabled.
487 if ($enabled_disabled_sids{$sid} eq "enabled") {
488 # Print the sid to the enabled_sids file.
489 print ENABLED_FILE "enablesid $sid\n";
490 # Check if the sid is disabled.
491 } elsif ($enabled_disabled_sids{$sid} eq "disabled") {
492 # Print the sid to the disabled_sids file.
493 print DISABLED_FILE "disablesid $sid\n";
494 # Something strange happende - skip the current sid.
495 } else {
496 next;
497 }
37659505
SS
498 }
499 }
298723b9 500
d2212836
SS
501 # Close file for enabled_sids after writing.
502 close(ENABLED_FILE);
503
504 # Close file for disabled_sids after writing.
505 close(DISABLED_FILE);
e5738079 506
b02e30fd
SS
507 # Call function to generate and write the used rulefiles file.
508 &IDS::write_used_rulefiles_file(@enabled_rulefiles);
52599865 509
27760092 510 # Lock the webpage and print message.
5bd8940d 511 &working_notice("$Lang::tr{'ids apply ruleset changes'}");
27760092 512
52599865 513 # Call oinkmaster to alter the ruleset.
27760092
SS
514 &IDS::oinkmaster();
515
e2e7880d 516 # Check if the IDS is running.
5a28e721 517 if(&IDS::ids_is_running()) {
af8e5145
SS
518 # Check if a restart of suricata is required.
519 if ($suricata_restart_required) {
520 # Call suricatactrl to perform the restart.
521 &IDS::call_suricatactrl("restart");
522 } else {
523 # Call suricatactrl to perform a reload.
524 &IDS::call_suricatactrl("reload");
525 }
e2e7880d
SS
526 }
527
27760092
SS
528 # Reload page.
529 &reload();
52599865
SS
530
531# Download new ruleset.
5fd2e9d6 532} elsif ($cgiparams{'RULESET'} eq $Lang::tr{'update ruleset'}) {
43263ea6
SS
533 # Check if the red device is active.
534 unless (-e "${General::swroot}/red/active") {
013274d7 535 $errormessage = "$Lang::tr{'could not download latest updates'} - $Lang::tr{'system is offline'}";
43263ea6 536 }
52599865 537
3983aebd 538 # Check if enought free disk space is availabe.
434001d0
SS
539 if(&IDS::checkdiskspace()) {
540 $errormessage = "$Lang::tr{'not enough disk space'}";
541 }
52599865 542
43263ea6
SS
543 # Check if any errors happend.
544 unless ($errormessage) {
27760092
SS
545 # Lock the webpage and print notice about downloading
546 # a new ruleset.
5bd8940d 547 &working_notice("$Lang::tr{'ids download new ruleset'}");
3983aebd 548
43263ea6 549 # Call subfunction to download the ruleset.
434001d0
SS
550 if(&IDS::downloadruleset()) {
551 $errormessage = $Lang::tr{'could not download latest updates'};
8f22237b 552
3983aebd 553 # Call function to store the errormessage.
434001d0 554 &IDS::_store_error_message($errormessage);
52599865 555
3983aebd
SS
556 # Preform a reload of the page.
557 &reload();
558 } else {
559 # Call subfunction to launch oinkmaster.
560 &IDS::oinkmaster();
43263ea6 561
e2e7880d 562 # Check if the IDS is running.
5a28e721 563 if(&IDS::ids_is_running()) {
e2e7880d
SS
564 # Call suricatactrl to perform a reload.
565 &IDS::call_suricatactrl("reload");
566 }
567
3983aebd
SS
568 # Perform a reload of the page.
569 &reload();
570 }
52599865 571 }
5bd8940d 572# Save IDS settings.
e0bfd338 573} elsif ($cgiparams{'IDS'} eq $Lang::tr{'save'}) {
bbb6efae
SS
574 my %oldidssettings;
575 my $reload_page;
ebdd0f9a 576 my $monitored_zones = 0;
bbb6efae
SS
577
578 # Read-in current (old) IDS settings.
b02e30fd 579 &General::readhash("$IDS::ids_settings_file", \%oldidssettings);
bbb6efae 580
a232b58c 581 # Prevent form name from been stored in conf file.
e0bfd338 582 delete $cgiparams{'IDS'};
a232b58c 583
ebdd0f9a
SS
584 # Check if the IDS should be enabled.
585 if ($cgiparams{'ENABLE_IDS'} eq "on") {
586 # Check if any ruleset is available. Otherwise abort and display an error.
2f252efa 587 unless(%used_providers) {
ebdd0f9a
SS
588 $errormessage = $Lang::tr{'ids no ruleset available'};
589 }
590
591 # Loop through the array of available interfaces.
592 foreach my $zone (@network_zones) {
593 # Convert interface name into upper case.
594 my $zone_upper = uc($zone);
595
596 # Check if the IDS is enabled for this interaces.
597 if ($cgiparams{"ENABLE_IDS_$zone_upper"}) {
598 # Increase count.
599 $monitored_zones++;
600 }
601 }
602
603 # Check if at least one zone should be monitored, or show an error.
604 unless ($monitored_zones >= 1) {
605 $errormessage = $Lang::tr{'ids no network zone'};
606 }
607 }
608
a232b58c
SS
609 # Go on if there are no error messages.
610 if (!$errormessage) {
611 # Store settings into settings file.
b02e30fd 612 &General::writehash("$IDS::ids_settings_file", \%cgiparams);
a9a91e5f 613 }
8d2f6b0b 614
77351a6b
SS
615 # Check if the the automatic rule update hass been touched.
616 if($cgiparams{'AUTOUPDATE_INTERVAL'} ne $oldidssettings{'AUTOUPDATE_INTERVAL'}) {
617 # Call suricatactrl to set the new interval.
618 &IDS::call_suricatactrl("cron", $cgiparams{'AUTOUPDATE_INTERVAL'});
619 }
620
8d2f6b0b 621 # Generate file to store the home net.
b02e30fd 622 &IDS::generate_home_net_file();
e2e7880d 623
a40ee6b9
SS
624 # Generate file to the store the DNS servers.
625 &IDS::generate_dns_servers_file();
626
e698090e
SS
627 # Generate file to store the HTTP ports.
628 &IDS::generate_http_ports_file();
629
74cc8f5a 630 # Write the modify sid's file and pass the taken ruleaction.
81bae51f 631 &IDS::write_modify_sids_file();
bbb6efae 632
01d02eb6
SS
633 # Check if "MONITOR_TRAFFIC_ONLY" has been changed.
634 if($cgiparams{'MONITOR_TRAFFIC_ONLY'} ne $oldidssettings{'MONITOR_TRAFFIC_ONLY'}) {
bbb6efae 635 # Check if a ruleset exists.
2f252efa 636 if (%used_providers) {
bbb6efae 637 # Lock the webpage and print message.
5bd8940d 638 &working_notice("$Lang::tr{'ids working'}");
bbb6efae
SS
639
640 # Call oinkmaster to alter the ruleset.
641 &IDS::oinkmaster();
642
643 # Set reload_page to "True".
644 $reload_page="True";
645 }
646 }
647
e2e7880d
SS
648 # Check if the IDS currently is running.
649 if(&IDS::ids_is_running()) {
650 # Check if ENABLE_IDS is set to on.
651 if($cgiparams{'ENABLE_IDS'} eq "on") {
652 # Call suricatactrl to perform a reload of suricata.
653 &IDS::call_suricatactrl("reload");
654 } else {
655 # Call suricatactrl to stop suricata.
656 &IDS::call_suricatactrl("stop");
657 }
658 } else {
659 # Call suricatactrl to start suricata.
660 &IDS::call_suricatactrl("start");
661 }
bbb6efae
SS
662
663 # Check if the page should be reloaded.
664 if ($reload_page) {
665 # Perform a reload of the page.
666 &reload();
667 }
4c067847
SS
668
669} elsif (($cgiparams{'PROVIDERS'} eq "$Lang::tr{'add'}") || ($cgiparams{'PROVIDERS'} eq "$Lang::tr{'update'}")) {
aba3cbe5
SS
670 my %used_providers = ();
671
672 # Read-in providers settings file.
673 &General::readhasharray("$IDS::providers_settings_file", \%used_providers);
674
4c067847
SS
675 # Assign some nice human-readable values.
676 my $provider = $cgiparams{'PROVIDER'};
677 my $subscription_code = $cgiparams{'SUBSCRIPTION_CODE'};
678 my $status_autoupdate = $cgiparams{'ENABLE_AUTOUPDATE'};
679
680 # Check if we are going to add a new provider.
681 if ($cgiparams{'PROVIDERS'} eq "$Lang::tr{'add'}") {
682 # Loop through the hash of used providers.
683 foreach my $id ( keys %used_providers) {
684 # Check if the choosen provider is already in use.
685 if ($used_providers{$id}[0] eq "$provider") {
686 # XXX - add to language file.
687 # Assign error message.
688 $errormessage = "The choosen provider is already in use.";
689 }
690 }
691 }
692
693 # Check if the provider requires a subscription code.
694 if ($IDS::Ruleset::Providers{$provider}{'requires_subscription'} eq "True") {
695 # Check if an subscription code has been provided.
696 if ($subscription_code) {
697 # Check if the code contains unallowed chars.
698 unless ($subscription_code =~ /^[a-z0-9]+$/) {
699 $errormessage = $Lang::tr{'invalid input for subscription code'};
700 }
701 } else {
702 # Print an error message, that an subsription code is required for this
703 # provider.
704 $errormessage = $Lang::tr{'ids subscription code required'};
705 }
706 }
707
708 # Go further if there was no error.
709 if ($errormessage eq '') {
710 my $id;
711 my $status;
712
713 # Check if we should edit an existing entry and got an ID.
714 if (($cgiparams{'PROVIDERS'} eq $Lang::tr{'update'}) && ($cgiparams{'ID'})) {
715 # Assin the provided id.
716 $id = $cgiparams{'ID'};
717
718 # Undef the given ID.
719 undef($cgiparams{'ID'});
720
721 # Grab the configured status of the corresponding entry.
722 $status = $used_providers{$id}[3];
723 } else {
724 # Each newly added entry automatically should be enabled.
725 $status = "enabled";
726
727 # Generate the ID for the new entry.
728 #
729 # Sort the keys by their ID and store them in an array.
730 my @keys = sort { $a <=> $b } keys %used_providers;
731
732 # Reverse the key array.
733 my @reversed = reverse(@keys);
734
735 # Obtain the last used id.
736 my $last_id = @reversed[0];
737
738 # Increase the last id by one and use it as id for the new entry.
739 $id = ++$last_id;
740 }
741
742 # Add/Modify the entry to/in the used providers hash..
743 $used_providers{$id} = ["$provider", "$subscription_code", "$status_autoupdate", "$status"];
744
745 # Write the changed hash to the providers settings file.
746 &General::writehasharray($IDS::providers_settings_file, \%used_providers);
747
748 # Undefine providers flag.
749 undef($cgiparams{'PROVIDERS'});
750 }
ac1cfefa
MT
751}
752
ac1cfefa
MT
753&Header::openpage($Lang::tr{'intrusion detection system'}, 1, '');
754
2bbe6ede 755&Header::openbigbox('100%', 'left', '', $errormessage);
e0cec9fe 756
2bbe6ede 757&show_display_error_message();
0d34a479 758
2bbe6ede
SS
759if ($cgiparams{'RULESET'} eq "$Lang::tr{'ids customize ruleset'}" ) {
760 &show_customize_ruleset();
2f252efa
SS
761} elsif ($cgiparams{'PROVIDERS'} ne "") {
762 &show_add_provider();
2bbe6ede
SS
763} else {
764 &show_mainpage();
765}
029b8ed2 766
2bbe6ede
SS
767&Header::closebigbox();
768&Header::closepage();
e0cec9fe 769
2bbe6ede
SS
770#
771## Tiny function to show if a error message happened.
772#
773sub show_display_error_message() {
774 if ($errormessage) {
775 &Header::openbox('100%', 'left', $Lang::tr{'error messages'});
776 print "<class name='base'>$errormessage\n";
777 print "&nbsp;</class>\n";
778 &Header::closebox();
779 }
780}
e0cec9fe 781
2bbe6ede
SS
782#
783## Function to display the main IDS page.
784#
785sub show_mainpage() {
aba3cbe5 786 # Read-in idssettings and provider settings.
2bbe6ede 787 &General::readhash("$IDS::ids_settings_file", \%idssettings);
aba3cbe5 788 &General::readhasharray("$IDS::providers_settings_file", \%used_providers);
2bbe6ede
SS
789
790 # If no autoupdate intervall has been configured yet, set default value.
77351a6b 791 unless(exists($idssettings{'AUTOUPDATE_INTERVAL'})) {
2bbe6ede 792 # Set default to "weekly".
77351a6b 793 $idssettings{'AUTOUPDATE_INTERVAL'} = 'weekly';
17726644 794 }
2bbe6ede
SS
795
796 # Read-in ignored hosts.
797 &General::readhasharray("$IDS::settingsdir/ignored", \%ignored);
798
799 $checked{'ENABLE_IDS'}{'off'} = '';
800 $checked{'ENABLE_IDS'}{'on'} = '';
801 $checked{'ENABLE_IDS'}{$idssettings{'ENABLE_IDS'}} = "checked='checked'";
802 $checked{'MONITOR_TRAFFIC_ONLY'}{'off'} = '';
803 $checked{'MONITOR_TRAFFIC_ONLY'}{'on'} = '';
804 $checked{'MONITOR_TRAFFIC_ONLY'}{$idssettings{'MONITOR_TRAFFIC_ONLY'}} = "checked='checked'";
2bbe6ede
SS
805 $selected{'AUTOUPDATE_INTERVAL'}{'off'} = '';
806 $selected{'AUTOUPDATE_INTERVAL'}{'daily'} = '';
807 $selected{'AUTOUPDATE_INTERVAL'}{'weekly'} = '';
2f252efa 808 $selected{'AUTOUPDATE_INTERVAL'}{$idssettings{'AUTOUPDATE_INTERVAL'}} = "selected='selected'";
17726644 809
2bbe6ede
SS
810 # Draw current state of the IDS
811 &Header::openbox('100%', 'left', $Lang::tr{'intrusion detection system'});
1504a375 812
2bbe6ede
SS
813 # Check if the IDS is running and obtain the process-id.
814 my $pid = &IDS::ids_is_running();
87660964 815
2bbe6ede
SS
816 # Display some useful information, if suricata daemon is running.
817 if ($pid) {
818 # Gather used memory.
819 my $memory = &get_memory_usage($pid);
87660964 820
2bbe6ede
SS
821 print <<END;
822 <table width='95%' cellspacing='0' class='tbl'>
823 <tr>
824 <th bgcolor='$color{'color20'}' colspan='3' align='left'><strong>$Lang::tr{'intrusion detection'}</strong></th>
825 </tr>
87660964 826
2bbe6ede
SS
827 <tr>
828 <td class='base'>$Lang::tr{'guardian daemon'}</td>
829 <td align='center' colspan='2' width='75%' bgcolor='${Header::colourgreen}'><font color='white'><strong>$Lang::tr{'running'}</strong></font></td>
830 </tr>
87660964 831
2bbe6ede
SS
832 <tr>
833 <td class='base'></td>
834 <td bgcolor='$color{'color20'}' align='center'><strong>PID</strong></td>
835 <td bgcolor='$color{'color20'}' align='center'><strong>$Lang::tr{'memory'}</strong></td>
836 </tr>
87660964 837
2bbe6ede
SS
838 <tr>
839 <td class='base'></td>
840 <td bgcolor='$color{'color22'}' align='center'>$pid</td>
841 <td bgcolor='$color{'color22'}' align='center'>$memory KB</td>
842 </tr>
843 </table>
87660964 844END
2bbe6ede
SS
845 } else {
846 # Otherwise display a hint that the service is not launched.
847 print <<END;
848 <table width='95%' cellspacing='0' class='tbl'>
849 <tr>
850 <th bgcolor='$color{'color20'}' colspan='3' align='left'><strong>$Lang::tr{'intrusion detection'}</strong></th>
851 </tr>
87660964 852
2bbe6ede
SS
853 <tr>
854 <td class='base'>$Lang::tr{'guardian daemon'}</td>
855 <td align='center' width='75%' bgcolor='${Header::colourred}'><font color='white'><strong>$Lang::tr{'stopped'}</strong></font></td>
856 </tr>
857 </table>
87660964 858END
2bbe6ede 859 }
87660964 860
2f252efa
SS
861 # Only show this area, if at least one ruleset provider is configured.
862 if (%used_providers) {
674912fc 863
2bbe6ede 864print <<END
674912fc 865
2bbe6ede 866 <br><br><h2>$Lang::tr{'settings'}</h2>
1286e0d4 867
2bbe6ede
SS
868 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
869 <table width='100%' border='0'>
870 <tr>
871 <td class='base' colspan='2'>
872 <input type='checkbox' name='ENABLE_IDS' $checked{'ENABLE_IDS'}{'on'}>&nbsp;$Lang::tr{'ids enable'}
873 </td>
cf02bf2f 874
2bbe6ede
SS
875 <td class='base' colspan='2'>
876 <input type='checkbox' name='MONITOR_TRAFFIC_ONLY' $checked{'MONITOR_TRAFFIC_ONLY'}{'on'}>&nbsp;$Lang::tr{'ids monitor traffic only'}
877 </td>
878 </tr>
1504a375 879
2bbe6ede
SS
880 <tr>
881 <td><br><br></td>
882 <td><br><br></td>
883 <td><br><br></td>
884 <td><br><br></td>
885 </tr>
a4ccfcbb 886
2bbe6ede
SS
887 <tr>
888 <td colspan='4'><b>$Lang::tr{'ids monitored interfaces'}</b><br></td>
889 </tr>
a4ccfcbb 890
2bbe6ede 891 <tr>
ac1cfefa
MT
892END
893;
1504a375 894
2bbe6ede
SS
895 # Loop through the array of available networks and print config options.
896 foreach my $zone (@network_zones) {
897 my $checked_input;
898 my $checked_forward;
1504a375 899
2bbe6ede
SS
900 # Convert current zone name to upper case.
901 my $zone_upper = uc($zone);
1504a375 902
2bbe6ede
SS
903 # Set zone name.
904 my $zone_name = $zone;
53817b89 905
2bbe6ede
SS
906 # Dirty hack to get the correct language string for the red zone.
907 if ($zone eq "red") {
908 $zone_name = "red1";
909 }
53817b89 910
2bbe6ede
SS
911 # Grab checkbox status from settings hash.
912 if ($idssettings{"ENABLE_IDS_$zone_upper"} eq "on") {
913 $checked_input = "checked = 'checked'";
914 }
1504a375 915
2bbe6ede
SS
916 print "<td class='base' width='20%'>\n";
917 print "<input type='checkbox' name='ENABLE_IDS_$zone_upper' $checked_input>\n";
918 print "&nbsp;$Lang::tr{'enabled on'}<font color='$colourhash{$zone}'> $Lang::tr{$zone_name}</font>\n";
919 print "</td>\n";
920 }
1b73b07e 921
ac1cfefa 922print <<END
2bbe6ede 923 </tr>
77351a6b
SS
924
925 <tr>
926 <td><br><br></td>
927 <td><br><br></td>
928 <td><br><br></td>
929 <td><br><br></td>
930 </tr>
931
932 <tr>
933 <td colspan='4'><b>$Lang::tr{'ids automatic rules update'}</b></td>
934 </tr>
935
936 <tr>
937 <td>
938 <select name='AUTOUPDATE_INTERVAL'>
939 <option value='off' $selected{'AUTOUPDATE_INTERVAL'}{'off'} >- $Lang::tr{'Disabled'} -</option>
940 <option value='daily' $selected{'AUTOUPDATE_INTERVAL'}{'daily'} >$Lang::tr{'Daily'}</option>
941 <option value='weekly' $selected{'AUTOUPDATE_INTERVAL'}{'weekly'} >$Lang::tr{'Weekly'}</option>
942 </select>
943 </td>
944 </tr>
2bbe6ede 945 </table>
1504a375 946
2bbe6ede 947 <br><br>
ea5c8eeb 948
2bbe6ede
SS
949 <table width='100%'>
950 <tr>
951 <td align='right'><input type='submit' name='IDS' value='$Lang::tr{'save'}' /></td>
952 </tr>
953 </table>
954 </form>
ea5c8eeb
SS
955END
956;
957
2bbe6ede 958 }
cf02bf2f 959
2bbe6ede 960 &Header::closebox();
ea5c8eeb 961
2f252efa
SS
962 #
963 # Used Ruleset Providers section.
964 #
2bbe6ede 965 &Header::openbox('100%', 'center', $Lang::tr{'ids ruleset settings'});
ea5c8eeb 966
2f252efa
SS
967print <<END;
968 <table width='100%' border='0'>
969 <tr>
970 <td class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'ids provider'}</b></td>
971 <td class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'date'}</b></td>
972 <td class='base' bgcolor='$color{'color20'}' align='center'><b>$Lang::tr{'ids autoupdates'}</b></td>
973 <td class='base' bgcolor='$color{'color20'}'></td>
974 <td class='base' colspan='3' bgcolor='$color{'color20'}'></td>
975 </tr>
3e12c6e6 976END
2f252efa
SS
977 # Check if some providers has been configured.
978 if (keys (%used_providers)) {
979 my $col = "";
3e12c6e6 980
2f252efa
SS
981 # Loop through all entries of the hash.
982 #while( (my $id) = each %used_providers) {
983 foreach my $id (sort keys(%used_providers)) {
984 # Assign data array positions to some nice variable names.
985 my $provider = $used_providers{$id}[0];
986 my $provider_name = &get_provider_name($provider);
3e12c6e6 987
2f252efa
SS
988 #XXX my $rulesdate = &IDS::get_ruleset_date($provider);
989 my $rulesdate;
990
991 my $subscription_code = $used_providers{$id}[1];
992 my $autoupdate_status = $used_providers{$id}[2];
993 my $status = $used_providers{$id}[3];
994
995 # Check if the item number is even or not.
996 if ($id % 2) {
997 $col="bgcolor='$color{'color22'}'";
998 } else {
999 $col="bgcolor='$color{'color20'}'";
2bbe6ede 1000 }
1504a375 1001
2f252efa
SS
1002 # Choose icons for the checkboxes.
1003 my $status_gif;
1004 my $status_gdesc;
1005 my $autoupdate_status_gif;
1006 my $autoupdate_status_gdesc;
1504a375 1007
2f252efa
SS
1008 # Check if the status is enabled and select the correct image and description.
1009 if ($status eq 'enabled' ) {
1010 $status_gif = 'on.gif';
1011 $status_gdesc = $Lang::tr{'click to disable'};
1012 } else {
1013 $status_gif = 'off.gif';
1014 $status_gdesc = $Lang::tr{'click to enable'};
1015 }
1504a375 1016
2f252efa
SS
1017 # Check if the autoupdate status is enabled and select the correct image and description.
1018 if ($autoupdate_status eq 'enabled') {
1019 $autoupdate_status_gif = 'on.gif';
1020 $autoupdate_status_gdesc = $Lang::tr{'click to disable'};
1021 } else {
1022 $autoupdate_status_gif = 'off.gif';
1023 $autoupdate_status_gdesc = $Lang::tr{'click to enable'};
1024 }
2bbe6ede 1025
2f252efa
SS
1026print <<END;
1027 <tr>
1028 <td width='33%' class='base' $col>$provider_name</td>
1029 <td width='30%' class='base' $col>$rulesdate</td>
1030
1031 <td align='center' $col>
1032 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
1033 <input type='hidden' name='AUTOUPDATE' value='$Lang::tr{'toggle enable disable'}' />
1034 <input type='image' name='$Lang::tr{'toggle enable disable'}' src='/images/$autoupdate_status_gif' alt='$autoupdate_status_gdesc' title='$autoupdate_status_gdesc' />
1035 <input type='hidden' name='ID' value='$id' />
1036 </form>
1037 </td>
1038
1039 <td align='center' $col>
1040 <form mehtod='post' action=$ENV{'SCRIPT_NAME'}'>
1041 <input type='hidden' name='PROVIDERS' value='$Lang::tr{'toggle enable disable'}'>
1042 <input type='image' name='$Lang::tr{'toggle enable disable'}' src='/images/$status_gif' alt='$status_gdesc' title='$status_gdesc'>
1043 <input type='hidden' name='ID' value='$id'>
1044 </form>
1045 </td>
1046
1047 <td align='center' $col>
1048 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
1049 <input type='hidden' name='PROVIDERS' value='$Lang::tr{'edit'}'>
1050 <input type='image' name='$Lang::tr{'edit'}' src='/images/edit.gif' alt='$Lang::tr{'edit'}' title='$Lang::tr{'edit'}'>
1051 <input type='hidden' name='ID' value='$id'>
1052 </form>
1053 </td>
1504a375 1054
2f252efa
SS
1055 <td align='center' $col>
1056 <form method='post' name='$provider' action='$ENV{'SCRIPT_NAME'}'>
1057 <input type='image' name='$Lang::tr{'remove'}' src='/images/delete.gif' title='$Lang::tr{'remove'}' alt='$Lang::tr{'remove'}'>
1058 <input type='hidden' name='ID' value='$id'>
1059 <input type='hidden' name='PROVIDERS' value='$Lang::tr{'remove'}'>
1060 </form>
1061 </td>
1062 </tr>
ea5c8eeb 1063END
2bbe6ede 1064 }
2f252efa
SS
1065
1066 } else {
1067 # Print notice that currently no hosts are ignored.
1068 print "<tr>\n";
1069 print "<td class='base' colspan='2'>$Lang::tr{'guardian no entries'}</td>\n";
1070 print "</tr>\n";
1071 }
1072
1073 print "</table>\n";
1074
1075 # Section to add new elements or edit existing ones.
ea5c8eeb 1076print <<END;
2f252efa
SS
1077 <br>
1078 <hr>
1079 <br>
1504a375 1080
2f252efa
SS
1081 <div align='right'>
1082 <table width='100%'>
1083 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
1084 <tr>
1085END
1086
1087 # Only show this button if a ruleset provider is configured.
1088 if (%used_providers) {
1089 print "<input type='submit' name='RULESET' value='$Lang::tr{'ids customize ruleset'}'>\n";
1090 }
1091print <<END;
1092 <input type='submit' name='PROVIDERS' value='$Lang::tr{'ids add provider'}'>
1093 </tr>
1094 </form>
2bbe6ede 1095 </table>
2f252efa 1096 </div>
ac1cfefa 1097END
ac1cfefa 1098
2bbe6ede 1099 &Header::closebox();
fbfdb241 1100
2bbe6ede
SS
1101 #
1102 # Whitelist / Ignorelist
1103 #
1104 &Header::openbox('100%', 'center', $Lang::tr{'ids ignored hosts'});
b7e29743 1105
2bbe6ede 1106 print <<END;
b7e29743
SS
1107 <table width='100%'>
1108 <tr>
1109 <td class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'ip address'}</b></td>
1110 <td class='base' bgcolor='$color{'color20'}'><b>$Lang::tr{'remark'}</b></td>
1111 <td class='base' colspan='3' bgcolor='$color{'color20'}'></td>
1112 </tr>
1113END
1114 # Check if some hosts have been added to be ignored.
1115 if (keys (%ignored)) {
1116 my $col = "";
1117
1118 # Loop through all entries of the hash.
1119 while( (my $key) = each %ignored) {
1120 # Assign data array positions to some nice variable names.
1121 my $address = $ignored{$key}[0];
1122 my $remark = $ignored{$key}[1];
1123 my $status = $ignored{$key}[2];
1124
1125 # Check if the key (id) number is even or not.
1126 if ($cgiparams{'ID'} eq $key) {
1127 $col="bgcolor='${Header::colouryellow}'";
1128 } elsif ($key % 2) {
1129 $col="bgcolor='$color{'color22'}'";
1130 } else {
1131 $col="bgcolor='$color{'color20'}'";
1132 }
1133
1134 # Choose icon for the checkbox.
1135 my $gif;
1136 my $gdesc;
1137
1138 # Check if the status is enabled and select the correct image and description.
1139 if ($status eq 'enabled' ) {
1140 $gif = 'on.gif';
1141 $gdesc = $Lang::tr{'click to disable'};
1142 } else {
1143 $gif = 'off.gif';
1144 $gdesc = $Lang::tr{'click to enable'};
1145 }
1146
1147print <<END;
1148 <tr>
1149 <td width='20%' class='base' $col>$address</td>
1150 <td width='65%' class='base' $col>$remark</td>
1151
1152 <td align='center' $col>
1153 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
2f252efa
SS
1154 <input type='hidden' name='WHITELIST' value='$Lang::tr{'toggle enable disable'}'>
1155 <input type='image' name='$Lang::tr{'toggle enable disable'}' src='/images/$gif' alt='$gdesc' title='$gdesc'>
1156 <input type='hidden' name='ID' value='$key'>
b7e29743
SS
1157 </form>
1158 </td>
1159
1160 <td align='center' $col>
1161 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
2f252efa
SS
1162 <input type='hidden' name='WHITELIST' value='$Lang::tr{'edit'}'>
1163 <input type='image' name='$Lang::tr{'edit'}' src='/images/edit.gif' alt='$Lang::tr{'edit'}' title='$Lang::tr{'edit'}'>
1164 <input type='hidden' name='ID' value='$key'>
b7e29743
SS
1165 </form>
1166 </td>
1167
1168 <td align='center' $col>
1169 <form method='post' name='$key' action='$ENV{'SCRIPT_NAME'}'>
1170 <input type='image' name='$Lang::tr{'remove'}' src='/images/delete.gif' title='$Lang::tr{'remove'}' alt='$Lang::tr{'remove'}'>
1171 <input type='hidden' name='ID' value='$key'>
1172 <input type='hidden' name='WHITELIST' value='$Lang::tr{'remove'}'>
1173 </form>
2bbe6ede
SS
1174 </td>
1175 </tr>
b7e29743 1176END
2bbe6ede
SS
1177 }
1178 } else {
1179 # Print notice that currently no hosts are ignored.
1180 print "<tr>\n";
1181 print "<td class='base' colspan='2'>$Lang::tr{'guardian no entries'}</td>\n";
1182 print "</tr>\n";
b7e29743 1183 }
b7e29743 1184
2bbe6ede 1185 print "</table>\n";
b7e29743 1186
2bbe6ede 1187 # Section to add new elements or edit existing ones.
b7e29743 1188print <<END;
2bbe6ede
SS
1189 <br>
1190 <hr>
1191 <br>
1192
1193 <div align='center'>
1194 <table width='100%'>
b7e29743
SS
1195END
1196
2bbe6ede
SS
1197 # Assign correct headline and button text.
1198 my $buttontext;
1199 my $entry_address;
1200 my $entry_remark;
b7e29743 1201
2bbe6ede
SS
1202 # Check if an ID (key) has been given, in this case an existing entry should be edited.
1203 if ($cgiparams{'ID'} ne '') {
1204 $buttontext = $Lang::tr{'update'};
1205 print "<tr><td class='boldbase' colspan='3'><b>$Lang::tr{'update'}</b></td></tr>\n";
b7e29743 1206
2bbe6ede
SS
1207 # Grab address and remark for the given key.
1208 $entry_address = $ignored{$cgiparams{'ID'}}[0];
1209 $entry_remark = $ignored{$cgiparams{'ID'}}[1];
1210 } else {
1211 $buttontext = $Lang::tr{'add'};
1212 print "<tr><td class='boldbase' colspan='3'><b>$Lang::tr{'dnsforward add a new entry'}</b></td></tr>\n";
1213 }
b7e29743
SS
1214
1215print <<END;
2bbe6ede
SS
1216 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
1217 <input type='hidden' name='ID' value='$cgiparams{'ID'}'>
1218 <tr>
1219 <td width='30%'>$Lang::tr{'ip address'}: </td>
1220 <td width='50%'><input type='text' name='IGNORE_ENTRY_ADDRESS' value='$entry_address' size='24' /></td>
b7e29743 1221
2bbe6ede
SS
1222 <td width='30%'>$Lang::tr{'remark'}: </td>
1223 <td wicth='50%'><input type='text' name=IGNORE_ENTRY_REMARK value='$entry_remark' size='24' /></td>
1224 <td align='center' width='20%'><input type='submit' name='WHITELIST' value='$buttontext' /></td>
1225 </tr>
1226 </form>
1227 </table>
1228 </div>
b7e29743
SS
1229END
1230
2bbe6ede 1231 &Header::closebox();
fed57fe7
SS
1232}
1233
fed57fe7
SS
1234#
1235## Function to show the customize ruleset section.
1236#
1237sub show_customize_ruleset() {
2bbe6ede
SS
1238 ### Java Script ###
1239 print"<script>\n";
1240
1241 # Java script variable declaration for show and hide.
1242 print"var show = \"$Lang::tr{'ids show'}\"\;\n";
1243 print"var hide = \"$Lang::tr{'ids hide'}\"\;\n";
1244
1245print <<END
1246 // Tiny java script function to show/hide the rules
1247 // of a given category.
1248 function showhide(tblname) {
1249 \$("#" + tblname).toggle();
1250
1251 // Get current content of the span element.
1252 var content = document.getElementById("span_" + tblname);
1253
1254 if (content.innerHTML === show) {
1255 content.innerHTML = hide;
1256 } else {
1257 content.innerHTML = show;
1258 }
1259 }
1260 </script>
1261END
1262;
87df37da 1263 &Header::openbox('100%', 'LEFT', "$Lang::tr{'intrusion detection system rules'}" );
80bcd4dd 1264 print"<form method='POST' action='$ENV{'SCRIPT_NAME'}'>\n";
ce0e83b3 1265
80bcd4dd
SS
1266 # Output display table for rule files
1267 print "<table width='100%'>\n";
f7fcd1c0 1268
80bcd4dd
SS
1269 # Loop over each rule file
1270 foreach my $rulefile (sort keys(%idsrules)) {
1271 my $rulechecked = '';
3ffee04b 1272
80bcd4dd
SS
1273 # Check if rule file is enabled
1274 if ($idsrules{$rulefile}{'Rulefile'}{'State'} eq 'on') {
1275 $rulechecked = 'CHECKED';
1276 }
1277
0a1bba1a
SS
1278 # Convert rulefile name into category name.
1279 my $categoryname = &_rulefile_to_category($rulefile);
1280
80bcd4dd
SS
1281 # Table and rows for the rule files.
1282 print"<tr>\n";
1283 print"<td class='base' width='5%'>\n";
1284 print"<input type='checkbox' name='$rulefile' $rulechecked>\n";
1285 print"</td>\n";
1286 print"<td class='base' width='90%'><b>$rulefile</b></td>\n";
1287 print"<td class='base' width='5%' align='right'>\n";
e0cec9fe 1288 print"<a href=\"javascript:showhide('$categoryname')\"><span id='span_$categoryname'>$Lang::tr{'ids show'}</span></a>\n";
80bcd4dd
SS
1289 print"</td>\n";
1290 print"</tr>\n";
1291
1292 # Rows which will be hidden per default and will contain the single rules.
0a1bba1a 1293 print"<tr style='display:none' id='$categoryname'>\n";
80bcd4dd 1294 print"<td colspan='3'>\n";
17726644 1295
f7fcd1c0 1296 # Local vars
80bcd4dd
SS
1297 my $lines;
1298 my $rows;
1299 my $col;
f9c2147d 1300
80bcd4dd
SS
1301 # New table for the single rules.
1302 print "<table width='100%'>\n";
e5738079 1303
80bcd4dd
SS
1304 # Loop over rule file rules
1305 foreach my $sid (sort {$a <=> $b} keys(%{$idsrules{$rulefile}})) {
1306 # Local vars
1307 my $ruledefchecked = '';
3ffee04b 1308
80bcd4dd
SS
1309 # Skip rulefile itself.
1310 next if ($sid eq "Rulefile");
f7fcd1c0 1311
80bcd4dd
SS
1312 # If 2 rules have been displayed, start a new row
1313 if (($lines % 2) == 0) {
1314 print "</tr><tr>\n";
1315
1316 # Increase rows by once.
1317 $rows++;
1318 }
1319
1320 # Colour lines.
1321 if ($rows % 2) {
1322 $col="bgcolor='$color{'color20'}'";
1323 } else {
1324 $col="bgcolor='$color{'color22'}'";
1325 }
f7fcd1c0 1326
80bcd4dd
SS
1327 # Set rule state
1328 if ($idsrules{$rulefile}{$sid}{'State'} eq 'on') {
1329 $ruledefchecked = 'CHECKED';
1330 }
1331
1332 # Create rule checkbox and display rule description
1333 print "<td class='base' width='5%' align='right' $col>\n";
1334 print "<input type='checkbox' NAME='$sid' $ruledefchecked>\n";
1335 print "</td>\n";
1336 print "<td class='base' width='45%' $col>$idsrules{$rulefile}{$sid}{'Description'}</td>";
1337
1338 # Increment rule count
1339 $lines++;
f7fcd1c0 1340 }
3ffee04b 1341
80bcd4dd
SS
1342 # If do not have a second rule for row, create empty cell
1343 if (($lines % 2) != 0) {
1344 print "<td class='base'></td>";
1345 }
17726644 1346
80bcd4dd
SS
1347 # Close display table
1348 print "</tr></table></td></tr>";
f7fcd1c0
SS
1349 }
1350
1351 # Close display table
80bcd4dd 1352 print "</table>";
17726644 1353
9268cddf 1354 print <<END
2999f1d2
CS
1355<table width='100%'>
1356<tr>
4efc8ccd
SS
1357 <td width='100%' align='right'>
1358 <input type='submit' value='$Lang::tr{'fwhost back'}'>
1359 <input type='submit' name='RULESET' value='$Lang::tr{'ids apply'}'>
1360 </td>
2999f1d2
CS
1361</tr>
1362</table>
298723b9 1363</form>
3ffee04b
CS
1364END
1365;
9268cddf
MT
1366 &Header::closebox();
1367 }
80bcd4dd
SS
1368}
1369
2f252efa
SS
1370#
1371## Function to show section for add/edit a provider.
1372#
1373sub show_add_provider() {
aba3cbe5 1374 my %used_providers = ();
2f252efa
SS
1375 my @subscription_providers;
1376
aba3cbe5
SS
1377 # Read -in providers settings file.
1378 &General::readhasharray("$IDS::providers_settings_file", \%used_providers);
1379
2f252efa
SS
1380 # Get all supported ruleset providers.
1381 my @ruleset_providers = &IDS::get_ruleset_providers();
1382
1383 ### Java Script ###
1384 print "<script>\n";
1385
1386 # Generate Java Script Object which contains the URL of the providers.
1387 print "\t// Object, which contains the webpages of the ruleset providers.\n";
1388 print "\tvar url = {\n";
1389
1390 # Loop through the array of supported providers.
1391 foreach my $provider (@ruleset_providers) {
1392 # Check if the provider requires a subscription.
1393 if ($IDS::Ruleset::Providers{$provider}{'requires_subscription'} eq "True") {
1394 # Add the provider to the array of subscription_providers.
1395 push(@subscription_providers, $provider);
1396 }
1397
1398 # Grab the URL for the provider.
1399 my $url = $IDS::Ruleset::Providers{$provider}{'website'};
1400
1401 # Print the URL to the Java Script Object.
1402 print "\t\t$provider: \"$url\"\,\n";
1403 }
1404
1405 # Close the Java Script Object declaration.
1406 print "\t}\;\n\n";
1407
1408 # Generate Java Script Array which contains the provider that requires a subscription.
1409 my $line = "";
1410 $line = join("', '", @subscription_providers);
1411
1412 print "\t// Array which contains the providers that requires a subscription.\n";
1413 print "\tsubscription_provider = ['$line']\;\n\n";
1414
1415print <<END
1416 // Java Script function to swap the text input field for
1417 // entering a subscription code.
1418 var update_provider = function() {
1419 if(inArray(\$('#PROVIDER').val(), subscription_provider)) {
1420 \$('.subscription_code').show();
1421 } else {
1422 \$('.subscription_code').hide();
1423 }
1424
1425 // Call function to change the website url.
1426 change_url(\$('#PROVIDER').val());
1427 };
1428
1429 // Java Script function to check if a given value is part of
1430 // an array.
1431 function inArray(value,array) {
1432 var count=array.length;
1433
1434 for(var i=0;i<count;i++) {
1435 if(array[i]===value){
1436 return true;
1437 }
1438 }
1439
1440 return false;
1441 }
1442
1443 // Tiny function to change the website url based on the selected element in the "PROVIDERS"
1444 // dropdown menu.
1445 function change_url(provider) {
1446 // Get and change the href to the corresponding url.
1447 document.getElementById("website").href = url[provider];
1448 }
1449
1450 // JQuery function to call corresponding function when
1451 // the ruleset provider is changed or the page is loaded for showing/hiding
1452 // the subscription_code area.
1453 \$(document).ready(function() {
1454 \$('#PROVIDER').change(update_provider);
1455 update_provider();
1456 });
1457
1458 </script>
1459END
1460;
1461
1462 &Header::openbox('100%', 'center', $Lang::tr{'ids provider settings'});
1463
1464 # Check if an existing provider should be edited.
1465 if($cgiparams{'PROVIDERS'} eq "$Lang::tr{'edit'}") {
1466 # Check if autoupdate is enabled for this provider.
1467 if ($used_providers{$cgiparams{'ID'}}[2] eq "on") {
1468 # Set the checkbox to be checked.
1469 $checked{'ENABLE_AUTOUPDATE'} = "checked='checked'";
1470 }
1471 } elsif ($cgiparams{'PROVIDERS'} eq "$Lang::tr{'ids add provider'}") {
1472 # Set the autoupdate to true as default.
1473 $checked{'ENABLE_AUTOUPDATE'} = "checked='checked'";
1474 }
1475
1476print <<END
1477 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
1478 <table width='100%' border='0'>
1479 <tr>
1480 <td colspan='2'><b>$Lang::tr{'ids provider'}</b></td>
1481 </tr>
1482
1483 <tr>
1484 <td width='40%'>
1485 <input type='hidden' name='ID' value='$cgiparams{'ID'}'>
1486 <select name='PROVIDER' id='PROVIDER'>
1487END
1488;
1489 # Loop through the array of ruleset providers.
1490 foreach my $provider (@ruleset_providers) {
1491 # Get the provider name.
1492 my $provider_name = &get_provider_name($provider);
1493
1494 # Pre-select the provider if one is given.
1495 if (($used_providers{$cgiparams{'ID'}}[0] eq "$provider") || ($cgiparams{'PROVIDER'} eq "$provider")) {
1496 $selected{$provider} = "selected='selected'";
1497 }
1498
1499 # Add the provider to the dropdown menu.
1500 print "<option value='$provider' $selected{$provider}>$provider_name</option>\n";
1501 }
1502print <<END
1503 </select>
1504 </td>
1505
1506 <td width='60%'>
1507 <b><a id="website" target="_blank" href="#">$Lang::tr{'ids visit provider website'}</a></b>
1508 </td>
1509 </tr>
1510
1511 <tr>
1512 <td colspan='2'><br><br></td>
1513 </tr>
1514
1515 <tr class='subscription_code' style='display:none' id='subscription_code'>
1516 <td colspan='2'>
1517 <table border='0'>
1518 <tr>
1519 <td>
1520 <b>$Lang::tr{'subscription code'}</b>
1521 </td>
1522 </tr>
1523
1524 <tr>
1525 <td>
1526 <input type='text' size='40' name='SUBSCRIPTION_CODE' value='$used_providers{$cgiparams{'ID'}}[1]'>
1527 </td>
1528 </tr>
1529
1530 <tr>
1531 <td><br><br></td>
1532 </tr>
1533 </table>
1534 </td>
1535 </tr>
1536
1537 <tr>
1538 <td colspan='2'>
1539 <input type='checkbox' name='ENABLE_AUTOUPDATE' $checked{'ENABLE_AUTOUPDATE'}>&nbsp;$Lang::tr{'ids enable automatic updates'}
1540 </td>
1541 </tr>
1542
1543 <tr>
1544 <td colspan='2' align='right'>
1545 <input type='submit' value='$Lang::tr{'back'}'>
1546END
1547;
1548 # Check if a provider should be added or edited.
1549 if ($cgiparams{'PROVIDERS'} eq "$Lang::tr{'edit'}") {
1550 # Display button for updating the existing provider.
1551 print "<input type='submit' name='PROVIDERS' value='$Lang::tr{'update'}'>\n";
1552 } else {
1553 # Display button to add the new provider.
1554 print "<input type='submit' name='PROVIDERS' value='$Lang::tr{'add'}'>\n";
1555 }
1556print <<END
1557 </td>
1558 </tr>
1559 </table>
1560 </form>
1561END
1562;
1563 &Header::closebox();
1564}
1565
27760092
SS
1566#
1567## A function to display a notice, to lock the webpage and
1568## tell the user which action currently will be performed.
1569#
1570sub working_notice ($) {
1571 my ($message) = @_;
1572
1573 &Header::openpage($Lang::tr{'intrusion detection system'}, 1, '');
1574 &Header::openbigbox('100%', 'left', '', $errormessage);
1575 &Header::openbox( 'Waiting', 1,);
1576 print <<END;
1577 <table>
1578 <tr>
1579 <td><img src='/images/indicator.gif' alt='$Lang::tr{'aktiv'}' /></td>
1580 <td>$message</td>
1581 </tr>
1582 </table>
1583END
1584 &Header::closebox();
1585 &Header::closebigbox();
1586 &Header::closepage();
1587}
1588
3983aebd
SS
1589#
1590## A tiny function to perform a reload of the webpage after one second.
1591#
1592sub reload () {
1593 print "<meta http-equiv='refresh' content='1'>\n";
1594
1595 # Stop the script.
1596 exit;
a70d269a
SS
1597}
1598
25f5cb0d
SS
1599#
1600## Private function to read-in and parse rules of a given rulefile.
1601#
1602## The given file will be read, parsed and all valid rules will be stored by ID,
9d18656b 1603## message/description and it's state in the idsrules hash.
25f5cb0d 1604#
3da6e01b
SS
1605sub readrulesfile ($) {
1606 my $rulefile = shift;
1607
1608 # Open rule file and read in contents
298ef5ba 1609 open(RULEFILE, "$IDS::rulespath/$rulefile") or die "Unable to read $rulefile!";
3da6e01b
SS
1610
1611 # Store file content in an array.
1612 my @lines = <RULEFILE>;
1613
1614 # Close file.
1615 close(RULEFILE);
1616
1617 # Loop over rule file contents
1618 foreach my $line (@lines) {
1619 # Remove whitespaces.
1620 chomp $line;
1621
1622 # Skip blank lines.
1623 next if ($line =~ /^\s*$/);
1624
1625 # Local vars.
1626 my $sid;
1627 my $msg;
1628
1629 # Gather rule sid and message from the ruleline.
1630 if ($line =~ m/.*msg:\"(.*?)\"\; .* sid:(.*?); /) {
1631 $msg = $1;
1632 $sid = $2;
1633
1634 # Check if a rule has been found.
1635 if ($sid && $msg) {
9d18656b
SS
1636 # Add rule to the idsrules hash.
1637 $idsrules{$rulefile}{$sid}{'Description'} = $msg;
3da6e01b
SS
1638
1639 # Grab status of the rule. Check if ruleline starts with a "dash".
1640 if ($line =~ /^\#/) {
1641 # If yes, the rule is disabled.
9d18656b 1642 $idsrules{$rulefile}{$sid}{'State'} = "off";
3da6e01b
SS
1643 } else {
1644 # Otherwise the rule is enabled.
9d18656b 1645 $idsrules{$rulefile}{$sid}{'State'} = "on";
3da6e01b
SS
1646 }
1647 }
1648 }
b7e29743 1649 }
3da6e01b 1650}
87660964 1651
8d2f6b0b
SS
1652#
1653## Function to get the used memory of a given process-id.
1654#
87660964 1655sub get_memory_usage($) {
004b13b7 1656 my ($pid) = @_;
87660964 1657
004b13b7 1658 my $memory = 0;
87660964 1659
004b13b7 1660 # Try to open the status file for the given process-id on the pseudo
87660964 1661 # file system proc.
004b13b7
SS
1662 if (open(FILE, "/proc/$pid/status")) {
1663 # Loop through the entire file.
1664 while (<FILE>) {
1665 # Splitt current line content and store them into variables.
1666 my ($key, $value) = split(":", $_, 2);
1667
1668 # Check if the current key is the one which contains the memory usage.
1669 # The wanted one is VmRSS which contains the Real-memory (resident set)
1670 # of the entire process.
1671 if ($key eq "VmRSS") {
1672 # Found the memory usage add it to the memory variable.
1673 $memory += $value;
1674
1675 # Break the loop.
1676 last;
1677 }
1678 }
87660964
SS
1679
1680 # Close file handle.
004b13b7 1681 close(FILE);
87660964
SS
1682
1683 # Return memory usage.
1684 return $memory;
004b13b7 1685 }
87660964
SS
1686
1687 # If the file could not be open, return nothing.
1688 return;
1689}
1690
a5d61752
SS
1691#
1692## Function to read-in the given enabled or disables sids file.
1693#
1694sub read_enabled_disabled_sids_file($) {
1695 my ($file) = @_;
1696
1697 # Temporary hash to store the sids and their state. It will be
1698 # returned at the end of this function.
1699 my %temphash;
1700
1701 # Open the given filename.
1702 open(FILE, "$file") or die "Could not open $file. $!\n";
1703
1704 # Loop through the file.
1705 while(<FILE>) {
1706 # Remove newlines.
1707 chomp $_;
1708
1709 # Skip blank lines.
1710 next if ($_ =~ /^\s*$/);
1711
1712 # Skip coments.
1713 next if ($_ =~ /^\#/);
1714
1715 # Splitt line into sid and state part.
1716 my ($state, $sid) = split(" ", $_);
1717
1718 # Skip line if the sid is not numeric.
1719 next unless ($sid =~ /\d+/ );
1720
1721 # Check if the sid was enabled.
1722 if ($state eq "enablesid") {
1723 # Add the sid and its state as enabled to the temporary hash.
1724 $temphash{$sid} = "enabled";
1725 # Check if the sid was disabled.
1726 } elsif ($state eq "disablesid") {
1727 # Add the sid and its state as disabled to the temporary hash.
1728 $temphash{$sid} = "disabled";
1729 # Invalid state - skip the current sid and state.
1730 } else {
1731 next;
1732 }
1733 }
1734
1735 # Close filehandle.
1736 close(FILE);
1737
1738 # Return the hash.
1739 return %temphash;
1740}
0a1bba1a 1741
019e5e9b
SS
1742#
1743## Function to get the provider name from the language file or providers file for a given handle.
1744#
1745sub get_provider_name($) {
1746 my ($handle) = @_;
1747 my $provider_name;
1748
1749 # Get the required translation string for the given provider handle.
1750 my $tr_string = $IDS::Ruleset::Providers{$handle}{'tr_string'};
1751
1752 # Check if the translation string is available in the language files.
1753 if ($Lang::tr{$tr_string}) {
1754 # Use the translated string from the language file.
1755 $provider_name = $Lang::tr{$tr_string};
1756 } else {
1757 # Fallback and use the provider summary from the providers file.
1758 $provider_name = $IDS::Ruleset::Providers{$handle}{'summary'};
1759 }
1760
1761 # Return the obtained provider name.
1762 return $provider_name;
1763}
1764
0a1bba1a
SS
1765#
1766## Private function to convert a given rulefile to a category name.
1767## ( No file extension anymore and if the name contained a dot, it
1768## would be replaced by a underline sign.)
1769#
1770sub _rulefile_to_category($) {
1771 my ($filename) = @_;
1772
1773 # Splitt the filename into single chunks and store them in a
1774 # temorary array.
1775 my @parts = split(/\./, $filename);
1776
1777 # Return / Remove last element of the temporary array.
1778 # This removes the file extension.
1779 pop @parts;
1780
1781 # Join together the single elements of the temporary array.
1782 # If these are more than one, use a "underline" for joining.
1783 my $category = join '_', @parts;
1784
1785 # Return the converted filename.
1786 return $category;
1787}