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