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