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