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