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