]> git.ipfire.org Git - ipfire-2.x.git/blame - html/cgi-bin/ids.cgi
ids-functions.pl: Add function to call suricatactrl binary
[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 #
9d18656b 5# Copyright (C) 2007-2018 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
22use strict;
23
24# enable only the following on debugging purpose
90c2e164
CS
25#use warnings;
26#use CGI::Carp 'fatalsToBrowser';
ac1cfefa 27
986e08d9 28require '/var/ipfire/general-functions.pl';
ac1cfefa
MT
29require "${General::swroot}/lang.pl";
30require "${General::swroot}/header.pl";
8dcebe53 31require "${General::swroot}/ids-functions.pl";
ac1cfefa 32
f2fdd0c1
CS
33my %color = ();
34my %mainsettings = ();
9d18656b 35my %idsrules = ();
1286e0d4 36my %idssettings=();
43263ea6 37my %rulesetsources = ();
298723b9 38my %cgiparams=();
ac1cfefa 39my %checked=();
5a3e0dca 40my %selected=();
0b89daee
SS
41
42# Read-in main settings, for language, theme and colors.
43&General::readhash("${General::swroot}/main/settings", \%mainsettings);
44&General::readhash("/srv/web/ipfire/html/themes/".$mainsettings{'THEME'}."/include/colors.txt", \%color);
45
1286e0d4
SS
46# Get the available network zones, based on the config type of the system and store
47# the list of zones in an array.
48my @network_zones = &IDS::get_available_network_zones();
ac1cfefa 49
8d2f6b0b 50# File where the used rulefiles are stored.
101c8881 51my $idsusedrulefilesfile = "$IDS::settingsdir/suricata-used-rulefiles.yaml";
8d2f6b0b
SS
52
53# File where the addresses of the homenet are stored.
54my $idshomenetfile = "$IDS::settingsdir/suricata-homenet.yaml";
55
43263ea6
SS
56my $errormessage;
57
ac1cfefa
MT
58&Header::showhttpheaders();
59
298723b9
SS
60#Get GUI values
61&Header::getcgihash(\%cgiparams);
ac1cfefa 62
3983aebd
SS
63# Check if any error has been stored.
64if (-e $IDS::storederrorfile) {
65 # Open file to read in the stored error message.
66 open(FILE, "<$IDS::storederrorfile") or die "Could not open $IDS::storederrorfile. $!\n";
67
68 # Read the stored error message.
69 $errormessage = <FILE>;
70
71 # Close file.
72 close (FILE);
73
74 # Delete the file, which is now not longer required.
75 unlink($IDS::storederrorfile);
76}
77
78
9d18656b 79## Grab all available snort rules and store them in the idsrules hash.
3da6e01b 80#
422204ff 81# Open snort rules directory and do a directory listing.
298ef5ba 82opendir(DIR, $IDS::rulespath) or die $!;
422204ff
SS
83 # Loop through the direcory.
84 while (my $file = readdir(DIR)) {
85
86 # We only want files.
298ef5ba 87 next unless (-f "$IDS::rulespath/$file");
422204ff
SS
88
89 # Ignore empty files.
298ef5ba 90 next if (-z "$IDS::rulespath/$file");
422204ff 91
3da6e01b 92 # Use a regular expression to find files ending in .rules
422204ff
SS
93 next unless ($file =~ m/\.rules$/);
94
3da6e01b 95 # Ignore files which are not read-able.
298ef5ba 96 next unless (-R "$IDS::rulespath/$file");
395e3b90 97
3da6e01b 98 # Call subfunction to read-in rulefile and add rules to
9d18656b 99 # the idsrules hash.
3da6e01b 100 &readrulesfile("$file");
395e3b90 101 }
395e3b90 102
3da6e01b 103closedir(DIR);
395e3b90 104
e5738079
SS
105# Gather used rulefiles.
106#
107# Check if the file for activated rulefiles is not empty.
02844177 108if(-f $idsusedrulefilesfile) {
e5738079 109 # Open the file for used rulefile and read-in content.
02844177 110 open(FILE, $idsusedrulefilesfile) or die "Could not open $idsusedrulefilesfile. $!\n";
e5738079
SS
111
112 # Read-in content.
113 my @lines = <FILE>;
114
115 # Close file.
116 close(FILE);
117
118 # Loop through the array.
119 foreach my $line (@lines) {
120 # Remove newlines.
121 chomp($line);
122
123 # Skip comments.
124 next if ($line =~ /\#/);
125
126 # Skip blank lines.
127 next if ($line =~ /^\s*$/);
128
129 # Gather rule sid and message from the ruleline.
101c8881 130 if ($line =~ /.*- (.*)/) {
e5738079
SS
131 my $rulefile = $1;
132
9d18656b
SS
133 # Add the rulefile to the %idsrules hash.
134 $idsrules{$rulefile}{'Rulefile'}{'State'} = "on";
e5738079
SS
135 }
136 }
137}
138
298723b9
SS
139# Save ruleset.
140if ($cgiparams{'RULESET'} eq $Lang::tr{'update'}) {
02844177
SS
141 my $enabled_sids_file = "$IDS::settingsdir/oinkmaster-enabled-sids.conf";
142 my $disabled_sids_file = "$IDS::settingsdir/oinkmaster-disabled-sids.conf";
298723b9
SS
143
144 # Arrays to store sid which should be added to the corresponding files.
145 my @enabled_sids;
146 my @disabled_sids;
e5738079 147 my @enabled_rulefiles;
298723b9 148
9d18656b
SS
149 # Loop through the hash of idsrules.
150 foreach my $rulefile(keys %idsrules) {
e5738079
SS
151 # Check if the rulefile is enabled.
152 if ($cgiparams{$rulefile} eq "on") {
153 # Add rulefile to the array of enabled rulefiles.
154 push(@enabled_rulefiles, $rulefile);
b65b5ef3
SS
155
156 # Drop item from cgiparams hash.
157 delete $cgiparams{$rulefile};
e5738079 158 }
466c6779 159 }
e5738079 160
9d18656b
SS
161 # Loop through the hash of idsrules.
162 foreach my $rulefile (keys %idsrules) {
298723b9 163 # Loop through the single rules of the rulefile.
9d18656b 164 foreach my $sid (keys %{$idsrules{$rulefile}}) {
c51a044a
SS
165 # Skip the current sid if it is not numeric.
166 next unless ($sid =~ /\d+/ );
167
298723b9
SS
168 # Check if there exists a key in the cgiparams hash for this sid.
169 if (exists($cgiparams{$sid})) {
170 # Look if the rule is disabled.
9d18656b 171 if ($idsrules{$rulefile}{$sid}{'State'} eq "off") {
298723b9
SS
172 # Check if the state has been set to 'on'.
173 if ($cgiparams{$sid} eq "on") {
174 # Add the sid to the enabled_sids array.
175 push(@enabled_sids, $sid);
176
177 # Drop item from cgiparams hash.
60333473 178 delete $cgiparams{$rulefile}{$sid};
298723b9
SS
179 }
180 }
181 } else {
182 # Look if the rule is enabled.
9d18656b 183 if ($idsrules{$rulefile}{$sid}{'State'} eq "on") {
298723b9
SS
184 # Check if the state is 'on' and should be disabled.
185 # In this case there is no entry
186 # for the sid in the cgiparams hash.
187 # Add it to the disabled_sids array.
188 push(@disabled_sids, $sid);
189
190 # Drop item from cgiparams hash.
60333473 191 delete $cgiparams{$rulefile}{$sid};
298723b9
SS
192 }
193 }
194 }
195 }
196
37659505
SS
197 # Open enabled sid's file for writing.
198 open(FILE, ">$enabled_sids_file") or die "Could not write to $enabled_sids_file. $!\n";
298723b9 199
37659505
SS
200 # Write header to file.
201 print FILE "#Autogenerated file. Any custom changes will be overwritten!\n";
298723b9 202
37659505
SS
203 # Check if the enabled_sids array contains any sid's.
204 if (@enabled_sids) {
298723b9
SS
205 # Loop through the array of enabled sids and write them to the file.
206 foreach my $sid (@enabled_sids) {
37659505 207 print FILE "enablesid $sid\n";
298723b9 208 }
298723b9
SS
209 }
210
37659505
SS
211 # Close file after writing.
212 close(FILE);
213
214 # Open disabled sid's file for writing.
215 open(FILE, ">$disabled_sids_file") or die "Could not write to $disabled_sids_file. $!\n";
298723b9 216
37659505
SS
217 # Write header to file.
218 print FILE "#Autogenerated file. Any custom changes will be overwritten!\n";
298723b9 219
37659505
SS
220 # Check if the enabled_sids array contains any sid's.
221 if (@disabled_sids) {
222 # Loop through the array of disabled sids and write them to the file.
223 foreach my $sid (@disabled_sids) {
224 print FILE "disablesid $sid\n";
225 }
226 }
298723b9 227
37659505
SS
228 # Close file after writing.
229 close(FILE);
e5738079
SS
230
231 # Open file for used rulefiles.
02844177 232 open (FILE, ">$idsusedrulefilesfile") or die "Could not write to $idsusedrulefilesfile. $!\n";
e5738079 233
101c8881
SS
234 # Write yaml header to the file.
235 print FILE "%YAML 1.1\n";
236 print FILE "---\n\n";
237
e5738079
SS
238 # Write header to file.
239 print FILE "#Autogenerated file. Any custom changes will be overwritten!\n";
240
241 # Check if the enabled_rulefiles array contains any entries.
242 if (@enabled_rulefiles) {
243 # Loop through the array of rulefiles which should be loaded and write the to the file.
244 foreach my $file (@enabled_rulefiles) {
101c8881 245 print FILE " - $file\n";
e5738079
SS
246 }
247 }
248
249 # Close file after writing.
250 close(FILE);
52599865 251
27760092
SS
252 # Lock the webpage and print message.
253 &working_notice("$Lang::tr{'snort working'}");
254
52599865 255 # Call oinkmaster to alter the ruleset.
27760092
SS
256 &IDS::oinkmaster();
257
258 # Reload page.
259 &reload();
52599865
SS
260
261# Download new ruleset.
262} elsif ($cgiparams{'RULESET'} eq $Lang::tr{'download new ruleset'}) {
43263ea6
SS
263 # Check if the red device is active.
264 unless (-e "${General::swroot}/red/active") {
265 $errormessage = $Lang::tr{'could not download latest updates'};
266 }
52599865 267
3983aebd 268 # Check if enought free disk space is availabe.
434001d0
SS
269 if(&IDS::checkdiskspace()) {
270 $errormessage = "$Lang::tr{'not enough disk space'}";
271 }
52599865 272
43263ea6
SS
273 # Check if any errors happend.
274 unless ($errormessage) {
27760092
SS
275 # Lock the webpage and print notice about downloading
276 # a new ruleset.
277 &working_notice("$Lang::tr{'snort working'}");
3983aebd 278
43263ea6 279 # Call subfunction to download the ruleset.
434001d0
SS
280 if(&IDS::downloadruleset()) {
281 $errormessage = $Lang::tr{'could not download latest updates'};
8f22237b 282
3983aebd 283 # Call function to store the errormessage.
434001d0 284 &IDS::_store_error_message($errormessage);
52599865 285
3983aebd
SS
286 # Preform a reload of the page.
287 &reload();
288 } else {
289 # Call subfunction to launch oinkmaster.
290 &IDS::oinkmaster();
43263ea6 291
3983aebd
SS
292 # Perform a reload of the page.
293 &reload();
294 }
52599865 295 }
a232b58c 296# Save snort settings.
e0bfd338 297} elsif ($cgiparams{'IDS'} eq $Lang::tr{'save'}) {
a232b58c 298 # Prevent form name from been stored in conf file.
e0bfd338 299 delete $cgiparams{'IDS'};
a232b58c
SS
300
301 # Check if an oinkcode has been provided.
302 if ($cgiparams{'OINKCODE'}) {
303 # Check if the oinkcode contains unallowed chars.
304 unless ($cgiparams{'OINKCODE'} =~ /^[a-z0-9]+$/) {
305 $errormessage = $Lang::tr{'invalid input for oink code'};
306 }
f9c2147d 307 }
ac1cfefa 308
a232b58c
SS
309 # Go on if there are no error messages.
310 if (!$errormessage) {
311 # Store settings into settings file.
02844177 312 &General::writehash("$IDS::settingsdir/settings", \%cgiparams);
a9a91e5f 313 }
8d2f6b0b
SS
314
315 # Generate file to store the home net.
316 &generate_home_net_file();
ac1cfefa
MT
317}
318
1286e0d4
SS
319# Read-in idssettings
320&General::readhash("$IDS::settingsdir/settings", \%idssettings);
321
322$checked{'ENABLE_IDS'}{'off'} = '';
323$checked{'ENABLE_IDS'}{'on'} = '';
324$checked{'ENABLE_IDS'}{$idssettings{'ENABLE_IDS'}} = "checked='checked'";
5a3e0dca 325$selected{'RULES'}{'nothing'} = '';
5a3e0dca 326$selected{'RULES'}{'community'} = '';
a0fa489f 327$selected{'RULES'}{'emerging'} = '';
5a3e0dca
MT
328$selected{'RULES'}{'registered'} = '';
329$selected{'RULES'}{'subscripted'} = '';
1286e0d4 330$selected{'RULES'}{$idssettings{'RULES'}} = "selected='selected'";
ac1cfefa
MT
331
332&Header::openpage($Lang::tr{'intrusion detection system'}, 1, '');
333
17726644
SS
334### Java Script ###
335print <<END
336<script>
337 // Tiny java script function to show/hide the rules
338 // of a given category.
339 function showhide(tblname) {
340 \$("#" + tblname).toggle();
341 }
342</script>
343END
344;
345
ac1cfefa
MT
346&Header::openbigbox('100%', 'left', '', $errormessage);
347
348if ($errormessage) {
349 &Header::openbox('100%', 'left', $Lang::tr{'error messages'});
350 print "<class name='base'>$errormessage\n";
351 print "&nbsp;</class>\n";
352 &Header::closebox();
353}
354
87660964 355# Draw current state of the IDS
7cc8a0e5 356&Header::openbox('100%', 'left', $Lang::tr{'intrusion detection system'});
1504a375 357
87660964
SS
358# Check if the IDS is running and obtain the process-id.
359my $pid = &IDS::ids_is_running();
360
361# Display some useful information, if suricata daemon is running.
362if ($pid) {
363 # Gather used memory.
364 my $memory = &get_memory_usage($pid);
365
366 print <<END;
367 <table width='95%' cellspacing='0' class='tbl'>
368 <tr>
369 <th bgcolor='$color{'color20'}' colspan='3' align='left'><strong>$Lang::tr{'intrusion detection'}</strong></th>
370 </tr>
371
372 <tr>
373 <td class='base'>$Lang::tr{'guardian daemon'}</td>
374 <td align='center' colspan='2' width='75%' bgcolor='${Header::colourgreen}'><font color='white'><strong>$Lang::tr{'running'}</strong></font></td>
375 </tr>
376
377 <tr>
378 <td class='base'></td>
379 <td bgcolor='$color{'color20'}' align='center'><strong>PID</strong></td>
380 <td bgcolor='$color{'color20'}' align='center'><strong>$Lang::tr{'memory'}</strong></td>
381 </tr>
382
383 <tr>
384 <td class='base'></td>
385 <td bgcolor='$color{'color22'}' align='center'>$pid</td>
386 <td bgcolor='$color{'color22'}' align='center'>$memory KB</td>
387 </tr>
388 </table>
389END
390} else {
391 # Otherwise display a hint that the service is not launched.
392 print <<END;
393 <table width='95%' cellspacing='0' class='tbl'>
394 <tr>
395 <th bgcolor='$color{'color20'}' colspan='3' align='left'><strong>$Lang::tr{'intrusion detection'}</strong></th>
396 </tr>
397
398 <tr>
399 <td class='base'>$Lang::tr{'guardian daemon'}</td>
400 <td align='center' width='75%' bgcolor='${Header::colourred}'><font color='white'><strong>$Lang::tr{'stopped'}</strong></font></td>
401 </tr>
402 </table>
87660964
SS
403END
404}
405&Header::closebox();
406
407# Draw elements for IDS configuration.
408&Header::openbox('100%', 'center', $Lang::tr{'settings'});
409
1504a375
SS
410my $rulesdate;
411
412# Check if a ruleset allready has been downloaded.
a69b96d2 413if ( -f "$IDS::rulestarball"){
1504a375 414 # Call stat on the filename to obtain detailed information.
a69b96d2 415 my @Info = stat("$IDS::rulestarball");
1504a375
SS
416
417 # Grab details about the creation time.
418 $rulesdate = localtime($Info[9]);
419}
420
ac1cfefa 421print <<END
1504a375
SS
422<form method='post' action='$ENV{'SCRIPT_NAME'}'>
423 <table width='100%' border='0'>
424 <tr>
425 <td class='base' width='25%'>
1286e0d4
SS
426 <input type='checkbox' name='ENABLE_IDS' $checked{'ENABLE_IDS'}{'on'}>$Lang::tr{'ids activate'} $Lang::tr{'intrusion detection system'}
427 </td>
428
429 <td class='base' width='25%'>
430 &nbsp
1504a375 431 </td>
1286e0d4
SS
432 </tr>
433
434 <tr>
435 <td colspan='2'><br><br>
436 </tr>
1504a375 437
1286e0d4 438 <tr>
1504a375 439 <td class='base' width='25%'>
1286e0d4 440 <b>$Lang::tr{'ids analyze incomming traffic'}</b>
1504a375
SS
441 </td>
442
443 <td class='base' width='25%'>
1286e0d4
SS
444 <b>$Lang::tr{'ids analyze routing traffic'}</b>
445 </td>
446 </tr>
ac1cfefa
MT
447END
448;
1504a375 449
1286e0d4
SS
450# Loop through the array of available networks and print config options.
451foreach my $zone (@network_zones) {
452 my $checked_input;
453 my $checked_forward;
1504a375 454
1286e0d4
SS
455 # Convert current zone name to upper case.
456 my $zone_upper = uc($zone);
1504a375 457
1286e0d4
SS
458 # Grab checkbox status from settings hash.
459 if ($idssettings{"ENABLE_IDS_INPUT_$zone_upper"} eq "on") {
460 $checked_input = "checked = 'checked'";
461 }
1504a375 462
1286e0d4
SS
463 # Do the same for the forward setting.
464 if ($idssettings{"ENABLE_IDS_FORWARD_$zone_upper"} eq "on") {
465 $checked_forward = "checked = 'checked'";
466 }
467
468 print "<tr>\n";
469 print "<td class='base' width='25%'>\n";
470 print "<input type='checkbox' name='ENABLE_IDS_INPUT_$zone_upper' $checked_input>$Lang::tr{'ids active on'} $Lang::tr{$zone}\n";
471 print "</td>\n";
472
473 print "<td class='base' width='25%'>\n";
474 print "<input type='checkbox' name='ENABLE_IDS_FORWARD_$zone_upper' $checked_forward>$Lang::tr{'ids active on'} $Lang::tr{$zone}\n";
475 print "</td>\n";
476 print "</tr>\n";
ac1cfefa 477}
1b73b07e 478
ac1cfefa 479print <<END
1504a375
SS
480 </tr>
481
482 <tr>
483 <td colspan='4'><br><br></td>
484 </tr>
485
486 <tr>
487 <td colspan='4'><b>$Lang::tr{'ids rules update'}</b></td>
488 </tr>
489
490 <tr>
491 <td colspan='4'><select name='RULES'>
5a3e0dca 492 <option value='nothing' $selected{'RULES'}{'nothing'} >$Lang::tr{'no'}</option>
a0fa489f 493 <option value='emerging' $selected{'RULES'}{'emerging'} >$Lang::tr{'emerging rules'}</option>
5a3e0dca
MT
494 <option value='community' $selected{'RULES'}{'community'} >$Lang::tr{'community rules'}</option>
495 <option value='registered' $selected{'RULES'}{'registered'} >$Lang::tr{'registered user rules'}</option>
496 <option value='subscripted' $selected{'RULES'}{'subscripted'} >$Lang::tr{'subscripted user rules'}</option>
497 </select>
1504a375
SS
498 </td>
499 </tr>
500
501 <tr>
502 <td colspan='4'>
503 <br>$Lang::tr{'ids rules license'} <a href='https://www.snort.org/subscribe' target='_blank'>www.snort.org</a>$Lang::tr{'ids rules license1'}</br>
504 <br>$Lang::tr{'ids rules license2'} <a href='https://www.snort.org/account/oinkcode' target='_blank'>Get an Oinkcode</a>, $Lang::tr{'ids rules license3'}</br>
505 </td>
506 </tr>
507
508 <tr>
1286e0d4 509 <td colspan='4' nowrap='nowrap'>Oinkcode:&nbsp;<input type='text' size='40' name='OINKCODE' value='$idssettings{'OINKCODE'}'></td>
1504a375
SS
510 </tr>
511
512 <tr>
513 <td colspan='4' align='left'><br>
514 <input type='submit' name='RULESET' value='$Lang::tr{'download new ruleset'}'>&nbsp;$Lang::tr{'updates installed'}: $rulesdate
515 </td>
516
517 </tr>
518 </table>
519
520 <br><br>
521
522 <table width='100%'>
523 <tr>
e0bfd338 524 <td align='right'><input type='submit' name='IDS' value='$Lang::tr{'save'}' /></td>
1504a375
SS
525 </tr>
526 </table>
ac1cfefa
MT
527</form>
528END
529;
530
ac1cfefa 531&Header::closebox();
fbfdb241 532
f7fcd1c0 533&Header::openbox('100%', 'LEFT', $Lang::tr{'intrusion detection system rules'});
298723b9
SS
534 print"<form method='POST' action='$ENV{'SCRIPT_NAME'}'>\n";
535
f7fcd1c0 536 # Output display table for rule files
17726644 537 print "<table width='100%'>\n";
ce0e83b3 538
17726644
SS
539 # Local variable required for java script to show/hide
540 # rules of a rulefile.
541 my $rulesetcount = 1;
f7fcd1c0
SS
542
543 # Loop over each rule file
9d18656b 544 foreach my $rulefile (sort keys(%idsrules)) {
f7fcd1c0
SS
545 my $rulechecked = '';
546
f7fcd1c0 547 # Check if rule file is enabled
9d18656b 548 if ($idsrules{$rulefile}{'Rulefile'}{'State'} eq 'on') {
f7fcd1c0
SS
549 $rulechecked = 'CHECKED';
550 }
3ffee04b 551
17726644
SS
552 # Table and rows for the rule files.
553 print"<tr>\n";
554 print"<td class='base' width='5%'>\n";
e5738079 555 print"<input type='checkbox' name='$rulefile' $rulechecked>\n";
17726644
SS
556 print"</td>\n";
557 print"<td class='base' width='90%'><b>$rulefile</b></td>\n";
558 print"<td class='base' width='5%' align='right'>\n";
559 print"<a href=\"javascript:showhide('ruleset$rulesetcount')\">SHOW</a>\n";
560 print"</td>\n";
561 print"</tr>\n";
562
563 # Rows which will be hidden per default and will contain the single rules.
564 print"<tr style='display:none' id='ruleset$rulesetcount'>\n";
565 print"<td colspan='3'>\n";
566
567 # Local vars
568 my $lines;
569 my $rows;
570 my $col;
571
572 # New table for the single rules.
573 print "<table width='100%'>\n";
574
575 # Loop over rule file rules
9d18656b 576 foreach my $sid (sort {$a <=> $b} keys(%{$idsrules{$rulefile}})) {
f7fcd1c0 577 # Local vars
17726644 578 my $ruledefchecked = '';
f9c2147d 579
e5738079
SS
580 # Skip rulefile itself.
581 next if ($sid eq "Rulefile");
582
17726644
SS
583 # If 2 rules have been displayed, start a new row
584 if (($lines % 2) == 0) {
585 print "</tr><tr>\n";
3ffee04b 586
17726644
SS
587 # Increase rows by once.
588 $rows++;
589 }
f7fcd1c0 590
17726644
SS
591 # Colour lines.
592 if ($rows % 2) {
593 $col="bgcolor='$color{'color20'}'";
594 } else {
595 $col="bgcolor='$color{'color22'}'";
f7fcd1c0
SS
596 }
597
17726644 598 # Set rule state
9d18656b 599 if ($idsrules{$rulefile}{$sid}{'State'} eq 'on') {
17726644 600 $ruledefchecked = 'CHECKED';
f7fcd1c0 601 }
3ffee04b 602
17726644
SS
603 # Create rule checkbox and display rule description
604 print "<td class='base' width='5%' align='right' $col>\n";
605 print "<input type='checkbox' NAME='$sid' $ruledefchecked>\n";
606 print "</td>\n";
9d18656b 607 print "<td class='base' width='45%' $col>$idsrules{$rulefile}{$sid}{'Description'}</td>";
17726644
SS
608
609 # Increment rule count
610 $lines++;
611 }
612
613 # If do not have a second rule for row, create empty cell
614 if (($lines % 2) != 0) {
615 print "<td class='base'></td>";
f7fcd1c0
SS
616 }
617
618 # Close display table
17726644 619 print "</tr></table></td></tr>";
3ffee04b 620
17726644
SS
621 # Finished whith the rule file, increase count.
622 $rulesetcount++;
f7fcd1c0 623 }
17726644
SS
624
625 # Close display table
626 print "</table>";
627
f7fcd1c0 628print <<END
2999f1d2
CS
629<table width='100%'>
630<tr>
298723b9 631 <td width='100%' align='right'><input type='submit' name='RULESET' value='$Lang::tr{'update'}'>
3ffee04b
CS
632 &nbsp; <!-- space for future online help link -->
633 </td>
2999f1d2
CS
634</tr>
635</table>
298723b9 636</form>
3ffee04b
CS
637END
638;
f7fcd1c0 639&Header::closebox();
ac1cfefa
MT
640&Header::closebigbox();
641&Header::closepage();
642
27760092
SS
643#
644## A function to display a notice, to lock the webpage and
645## tell the user which action currently will be performed.
646#
647sub working_notice ($) {
648 my ($message) = @_;
649
650 &Header::openpage($Lang::tr{'intrusion detection system'}, 1, '');
651 &Header::openbigbox('100%', 'left', '', $errormessage);
652 &Header::openbox( 'Waiting', 1,);
653 print <<END;
654 <table>
655 <tr>
656 <td><img src='/images/indicator.gif' alt='$Lang::tr{'aktiv'}' /></td>
657 <td>$message</td>
658 </tr>
659 </table>
660END
661 &Header::closebox();
662 &Header::closebigbox();
663 &Header::closepage();
664}
665
3983aebd
SS
666#
667## A tiny function to perform a reload of the webpage after one second.
668#
669sub reload () {
670 print "<meta http-equiv='refresh' content='1'>\n";
671
672 # Stop the script.
673 exit;
a70d269a
SS
674}
675
25f5cb0d
SS
676#
677## Private function to read-in and parse rules of a given rulefile.
678#
679## The given file will be read, parsed and all valid rules will be stored by ID,
9d18656b 680## message/description and it's state in the idsrules hash.
25f5cb0d 681#
3da6e01b
SS
682sub readrulesfile ($) {
683 my $rulefile = shift;
684
685 # Open rule file and read in contents
298ef5ba 686 open(RULEFILE, "$IDS::rulespath/$rulefile") or die "Unable to read $rulefile!";
3da6e01b
SS
687
688 # Store file content in an array.
689 my @lines = <RULEFILE>;
690
691 # Close file.
692 close(RULEFILE);
693
694 # Loop over rule file contents
695 foreach my $line (@lines) {
696 # Remove whitespaces.
697 chomp $line;
698
699 # Skip blank lines.
700 next if ($line =~ /^\s*$/);
701
702 # Local vars.
703 my $sid;
704 my $msg;
705
706 # Gather rule sid and message from the ruleline.
707 if ($line =~ m/.*msg:\"(.*?)\"\; .* sid:(.*?); /) {
708 $msg = $1;
709 $sid = $2;
710
711 # Check if a rule has been found.
712 if ($sid && $msg) {
9d18656b
SS
713 # Add rule to the idsrules hash.
714 $idsrules{$rulefile}{$sid}{'Description'} = $msg;
3da6e01b
SS
715
716 # Grab status of the rule. Check if ruleline starts with a "dash".
717 if ($line =~ /^\#/) {
718 # If yes, the rule is disabled.
9d18656b 719 $idsrules{$rulefile}{$sid}{'State'} = "off";
3da6e01b
SS
720 } else {
721 # Otherwise the rule is enabled.
9d18656b 722 $idsrules{$rulefile}{$sid}{'State'} = "on";
3da6e01b
SS
723 }
724 }
725 }
726 }
727}
87660964 728
8d2f6b0b
SS
729#
730## Function to get the used memory of a given process-id.
731#
87660964
SS
732sub get_memory_usage($) {
733 my $pid = @_;
734
735 my $memory=0;
736
737 # Try to open statm file for the given process-id on the pseudo
738 # file system proc.
739 if (open(FILE, "/proc/$pid/statm")) {
740 # Read file content.
741 my $temp = <FILE>;
742
743 # Splitt file content and store in an array.
744 my @memory = split(/ /,$temp);
745
746 # Close file handle.
747 close(FILE);
748
749 # Calculate memory usage.
750 $memory+=$memory[0];
751
752 # Return memory usage.
753 return $memory;
754 }
755
756 # If the file could not be open, return nothing.
757 return;
758}
759
8d2f6b0b
SS
760#
761## Function to generate the file which contains the home net information.
762#
763sub generate_home_net_file() {
764 my %netsettings;
765
766 # Read-in network settings.
767 &General::readhash("${General::swroot}/ethernet/settings", \%netsettings);
768
769 # Get available network zones.
770 my @network_zones = &IDS::get_available_network_zones();
771
772 # Temporary array to store network address and prefix of the configured
773 # networks.
774 my @networks;
775
776 # Loop through the array of available network zones.
777 foreach my $zone (@network_zones) {
778 # Skip the red network - It never can be part to the home_net!
779 next if($zone eq "red");
780
781 # Convert current zone name into upper case.
782 $zone = uc($zone);
783
784 # Generate key to access the required data from the netsettings hash.
785 my $zone_netaddress = $zone . "_NETADDRESS";
786 my $zone_netmask = $zone . "_NETMASK";
787
788 # Obtain the settings from the netsettings hash.
789 my $netaddress = $netsettings{$zone_netaddress};
790 my $netmask = $netsettings{$zone_netmask};
791
792 # Convert the subnetmask into prefix notation.
793 my $prefix = &Network::convert_netmask2prefix($netmask);
794
795 # Generate full network string.
796 my $network = join("/", $netaddress,$prefix);
797
798 # Check if the network is valid.
799 if(&Network::check_subnet($network)) {
800 # Add the generated network to the array of networks.
801 push(@networks, $network);
802 }
803 }
804
805 # Format home net declaration.
806 my $line = "\"\[";
807
808 # Loop through the array of networks.
809 foreach my $network (@networks) {
810 # Add the network to the line.
811 $line = "$line" . "$network";
812
813 # Check if the current network was the last in the array.
814 if ($network eq $networks[-1]) {
815 # Close the line.
816 $line = "$line" . "\]\"";
817 } else {
818 # Add "," for the next network.
819 $line = "$line" . "\,";
820 }
821 }
822
823 # Open file to store the addresses of the home net.
824 open(FILE, ">$idshomenetfile") or die "Could not open $idshomenetfile. $!\n";
825
826 # Print yaml header.
827 print FILE "%YAML 1.1\n";
828 print FILE "---\n\n";
829
830 # Print notice about autogenerated file.
831 print FILE "#Autogenerated file. Any custom changes will be overwritten!\n";
832
833 # Print the generated and required HOME_NET declaration to the file.
834 print FILE "HOME_NET:\t$line\n";
835
836 # Close file handle.
837 close(FILE);
838
839}