]> git.ipfire.org Git - ipfire-2.x.git/blobdiff - html/cgi-bin/ids.cgi
ids.cgi: Rework "Enable IPS" section
[ipfire-2.x.git] / html / cgi-bin / ids.cgi
index 0c3664547e613bf5f77835dd1423c5dcc38684d3..1235d477668bd1da437d8733e0cbe5f4221ed687 100644 (file)
@@ -131,7 +131,7 @@ if (($cgiparams{'WHITELIST'} eq $Lang::tr{'add'}) || ($cgiparams{'WHITELIST'} eq
                &General::writehasharray($IDS::ignored_file, \%ignored);
 
                # Regenerate the ignore file.
-               &GenerateIgnoreFile();
+               &IDS::generate_ignore_file();
        }
 
        # Check if the IDS is running.
@@ -174,7 +174,7 @@ if (($cgiparams{'WHITELIST'} eq $Lang::tr{'add'}) || ($cgiparams{'WHITELIST'} eq
                &General::writehasharray($IDS::ignored_file, \%ignored);
 
                # Regenerate the ignore file.
-               &GenerateIgnoreFile();
+               &IDS::generate_ignore_file();
 
                # Check if the IDS is running.
                if(&IDS::ids_is_running()) {
@@ -201,7 +201,7 @@ if (($cgiparams{'WHITELIST'} eq $Lang::tr{'add'}) || ($cgiparams{'WHITELIST'} eq
        &General::writehasharray($IDS::ignored_file, \%ignored);
 
        # Regenerate the ignore file.
-       &GenerateIgnoreFile();
+       &IDS::generate_ignore_file();
 
        # Check if the IDS is running.
        if(&IDS::ids_is_running()) {
@@ -323,6 +323,48 @@ if ($cgiparams{'RULESET'} eq $Lang::tr{'save'}) {
                &IDS::call_suricatactrl("cron", $cgiparams{'AUTOUPDATE_INTERVAL'});
        }
 
+       # Check if a ruleset is present - if not or the source has been changed download it.
+       if((! %idsrules) || ($oldsettings{'RULES'} ne $cgiparams{'RULES'})) {
+               # Check if the red device is active.
+               unless (-e "${General::swroot}/red/active") {
+                       $errormessage = "$Lang::tr{'could not download latest updates'} - $Lang::tr{'system is offline'}";
+               }
+
+               # Check if enought free disk space is availabe.
+               if(&IDS::checkdiskspace()) {
+                       $errormessage = "$Lang::tr{'not enough disk space'}";
+               }
+
+               # Check if any errors happend.
+               unless ($errormessage) {
+                       # Lock the webpage and print notice about downloading
+                       # a new ruleset.
+                       &working_notice("$Lang::tr{'snort working'}");
+
+                       # Call subfunction to download the ruleset.
+                       if(&IDS::downloadruleset()) {
+                               $errormessage = $Lang::tr{'could not download latest updates'};
+
+                               # Call function to store the errormessage.
+                               &IDS::_store_error_message($errormessage);
+                       } else {
+                               # Call subfunction to launch oinkmaster.
+                               &IDS::oinkmaster();
+                       }
+
+                       # Check if the IDS is running.
+                       if(&IDS::ids_is_running()) {
+                               # Call suricatactrl to stop the IDS - because of the changed
+                               # ruleset - the use has to configure it before suricata can be
+                               # used again.
+                               &IDS::call_suricatactrl("stop");
+                       }
+
+                       # Perform a reload of the page.
+                       &reload();
+               }
+       }
+
 # Save ruleset.
 } elsif ($cgiparams{'RULESET'} eq $Lang::tr{'update'}) {
        # Arrays to store which rulefiles have been enabled and will be used.
@@ -440,10 +482,10 @@ if ($cgiparams{'RULESET'} eq $Lang::tr{'save'}) {
        &reload();
 
 # Download new ruleset.
-} elsif ($cgiparams{'RULESET'} eq $Lang::tr{'download new ruleset'}) {
+} elsif ($cgiparams{'RULESET'} eq $Lang::tr{'update ruleset'}) {
        # Check if the red device is active.
        unless (-e "${General::swroot}/red/active") {
-               $errormessage = $Lang::tr{'could not download latest updates'};
+               $errormessage = "$Lang::tr{'could not download latest updates'} - $Lang::tr{'system is offline'}";
        }
 
        # Check if enought free disk space is availabe.
@@ -610,12 +652,42 @@ $selected{'AUTOUPDATE_INTERVAL'}{$rulessettings{'AUTOUPDATE_INTERVAL'}} = "selec
 &Header::openpage($Lang::tr{'intrusion detection system'}, 1, '');
 
 ### Java Script ###
+print"<script>\n";
+
+# Java script variable declaration for show and hide.
+print"var show = \"$Lang::tr{'ids show'}\"\;\n";
+print"var hide = \"$Lang::tr{'ids hide'}\"\;\n";
+
 print <<END
-<script>
+       // JQuery function to show/hide the text input field for
+       // Oinkcode/Subscription code.
+       \$(function() {
+               \$('#RULES').change(function(){
+                       if(\$('#RULES').val() == 'registered') {
+                               \$('#code').show();
+                       } else if(\$('#RULES').val() == 'subscripted') {
+                               \$('#code').show();
+                       } else if(\$('#RULES').val() == 'emerging_pro') {
+                               \$('#code').show();
+                       } else {
+                               \$('#code').hide();
+                       }
+               });
+       });
+
        // Tiny java script function to show/hide the rules
        // of a given category.
        function showhide(tblname) {
                \$("#" + tblname).toggle();
+
+               // Get current content of the span element.
+               var content = document.getElementById("span_" + tblname);
+
+               if (content.innerHTML === show) {
+                       content.innerHTML = hide;
+               } else {
+                       content.innerHTML = show;
+               }
        }
 </script>
 END
@@ -680,7 +752,6 @@ END
                </table>
 END
 }
-&Header::closebox();
 
 my $rulesdate;
 
@@ -693,79 +764,84 @@ if ( -f "$IDS::rulestarball"){
         $rulesdate = localtime($Info[9]);
 }
 
-# Draw elements for IDS configuration.
-&Header::openbox('100%', 'center', $Lang::tr{'settings'});
+# Only show this area, if a ruleset is present.
+if (%idsrules) {
 
-print <<END
-<form method='post' action='$ENV{'SCRIPT_NAME'}'>
-       <table width='100%' border='0'>
-               <tr>
-                       <td class='base' colspan='2'>
-                               <input type='checkbox' name='ENABLE_IDS' $checked{'ENABLE_IDS'}{'on'}>$Lang::tr{'ids activate'} $Lang::tr{'intrusion detection system'}
-                       </td>
+       print <<END
+
+       <br><br><h2>$Lang::tr{'settings'}</h2>
 
-                       <td class='base' colspan='2'>
-                               <input type='checkbox' name='MONITOR_TRAFFIC_ONLY' $checked{'MONITOR_TRAFFIC_ONLY'}{'on'}>$Lang::tr{'ids monitor traffic only'}
+       <form method='post' action='$ENV{'SCRIPT_NAME'}'>
+               <table width='100%' border='0'>
+                       <tr>
+                               <td class='base' colspan='2'>
+                                       <input type='checkbox' name='ENABLE_IDS' $checked{'ENABLE_IDS'}{'on'}>&nbsp$Lang::tr{'ids enable'}
+                               </td>
+
+                               <td class='base' colspan='2'>
+                                       <input type='checkbox' name='MONITOR_TRAFFIC_ONLY' $checked{'MONITOR_TRAFFIC_ONLY'}{'on'}>&nbsp$Lang::tr{'ids monitor traffic only'}
                        </td>
-               </tr>
+                       </tr>
 
-               <tr>
-                       <td><br><br></td>
-                       <td><br><br></td>
-                       <td><br><br></td>
-                       <td><br><br></td>
-               </tr>
+                       <tr>
+                               <td><br><br></td>
+                               <td><br><br></td>
+                               <td><br><br></td>
+                               <td><br><br></td>
+                       </tr>
 
-               <tr>
-                       <td colspan='4'><b>$Lang::tr{'ids monitored interfaces'}</b><br></td>
-               </tr>
+                       <tr>
+                               <td colspan='4'><b>$Lang::tr{'ids monitored interfaces'}</b><br></td>
+                       </tr>
 
-               <tr>
+                       <tr>
 END
 ;
 
-# Loop through the array of available networks and print config options.
-foreach my $zone (@network_zones) {
-       my $checked_input;
-       my $checked_forward;
+       # Loop through the array of available networks and print config options.
+       foreach my $zone (@network_zones) {
+               my $checked_input;
+               my $checked_forward;
 
-       # Convert current zone name to upper case.
-       my $zone_upper = uc($zone);
+               # Convert current zone name to upper case.
+               my $zone_upper = uc($zone);
 
-       # Set zone name.
-       my $zone_name = $zone;
+               # Set zone name.
+               my $zone_name = $zone;
 
-       # Dirty hack to get the correct language string for the red zone.
-       if ($zone eq "red") {
-               $zone_name = "red1";
-       }
+               # Dirty hack to get the correct language string for the red zone.
+               if ($zone eq "red") {
+                       $zone_name = "red1";
+               }
 
-       # Grab checkbox status from settings hash.
-       if ($idssettings{"ENABLE_IDS_$zone_upper"} eq "on") {
-               $checked_input = "checked = 'checked'";
-       }
+               # Grab checkbox status from settings hash.
+               if ($idssettings{"ENABLE_IDS_$zone_upper"} eq "on") {
+                       $checked_input = "checked = 'checked'";
+               }
 
-       print "<td class='base' width='25%'>\n";
-       print "<input type='checkbox' name='ENABLE_IDS_$zone_upper' $checked_input>\n";
-       print "&nbsp$Lang::tr{'enabled on'}<font color='$colourhash{$zone}'> $Lang::tr{$zone_name}</font>\n";
-       print "</td>\n";
-}
+               print "<td class='base' width='25%'>\n";
+               print "<input type='checkbox' name='ENABLE_IDS_$zone_upper' $checked_input>\n";
+               print "&nbsp$Lang::tr{'enabled on'}<font color='$colourhash{$zone}'> $Lang::tr{$zone_name}</font>\n";
+               print "</td>\n";
+       }
 
 print <<END
-               </tr>
-        </table>
+                       </tr>
+               </table>
 
-        <br><br>
+               <br><br>
 
-        <table width='100%'>
-                <tr>
-                        <td align='right'><input type='submit' name='IDS' value='$Lang::tr{'save'}' /></td>
-                </tr>
-        </table>
-</form>
+               <table width='100%'>
+                       <tr>
+                               <td align='right'><input type='submit' name='IDS' value='$Lang::tr{'save'}' /></td>
+                       </tr>
+               </table>
+       </form>
 END
 ;
 
+}
+
 &Header::closebox();
 
 # Draw elements for ruleset configuration.
@@ -780,8 +856,9 @@ print <<END
                </tr>
 
                <tr>
-                       <td><select name='RULES'>
+                       <td><select name='RULES' id='RULES'>
                                <option value='emerging' $selected{'RULES'}{'emerging'} >$Lang::tr{'emerging rules'}</option>
+                               <option value='emerging_pro' $selected{'RULES'}{'emerging_pro'} >$Lang::tr{'emerging pro rules'}</option>
                                <option value='community' $selected{'RULES'}{'community'} >$Lang::tr{'community rules'}</option>
                                <option value='registered' $selected{'RULES'}{'registered'} >$Lang::tr{'registered user rules'}</option>
                                <option value='subscripted' $selected{'RULES'}{'subscripted'} >$Lang::tr{'subscripted user rules'}</option>
@@ -790,22 +867,19 @@ print <<END
 
                        <td>
                                <select name='AUTOUPDATE_INTERVAL'>
-                                       <option value='off' $selected{'AUTOUPDATE_INTERVAL'}{'off'} >$Lang::tr{'no'}</option>
-                                       <option value='daily' $selected{'AUTOUPDATE_INTERVAL'}{'daily'} >$Lang::tr{'urlfilter daily'}</option>
-                                       <option value='weekly' $selected{'AUTOUPDATE_INTERVAL'}{'weekly'} >$Lang::tr{'urlfilter weekly'}</option>
+                                       <option value='off' $selected{'AUTOUPDATE_INTERVAL'}{'off'} >- $Lang::tr{'Disabled'} -</option>
+                                       <option value='daily' $selected{'AUTOUPDATE_INTERVAL'}{'daily'} >$Lang::tr{'Daily'}</option>
+                                       <option value='weekly' $selected{'AUTOUPDATE_INTERVAL'}{'weekly'} >$Lang::tr{'Weekly'}</option>
                                </select>
                        </td>
                </tr>
 
                <tr>
-                       <td colspan='2'>
-                               <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>
-                               <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>
-                       </td>
+                       <td colspan='2'><br><br></td>
                </tr>
 
-               <tr>
-                       <td colspan='2' nowrap='nowrap'>Oinkcode:&nbsp;<input type='text' size='40' name='OINKCODE' value='$rulessettings{'OINKCODE'}'></td>
+               <tr style='display:none' id='code'>
+                       <td colspan='2'>Oinkcode:&nbsp;<input type='text' size='40' name='OINKCODE' value='$rulessettings{'OINKCODE'}'></td>
                </tr>
 
                <tr>
@@ -814,19 +888,10 @@ print <<END
                        <td align='right'>
 END
 ;
-               # Check if a ruleset source has been configured yet.
-               unless($rulessettings{'RULES'}) {
-                       # If no ruleset settings have been saved yet, disable the button to download / update the ruleset.
-                       print"<input type='submit' name='RULESET' disabled='disabled' value='$Lang::tr{'download new ruleset'}'>\n";
-               } else {
-                       # Ruleset setting have been saved. - Check if a ruleset already is downloaded.
+                       # Check if a ruleset has been downloaded yet.
                        if (%idsrules) {
-                               # Allow to press the button and show it as "update ruleset".
+                               # Display button to update the ruleset.
                                print"<input type='submit' name='RULESET' value='$Lang::tr{'update ruleset'}'>\n";
-                       } else {
-                               # Also allow to press the button, but show it as "download new ruleset".
-                               print"<input type='submit' name='RULESET' value='$Lang::tr{'download new ruleset'}'>\n";
-                       }
                }
 print <<END;
                                <input type='submit' name='RULESET' value='$Lang::tr{'save'}'>
@@ -1000,7 +1065,7 @@ if (%idsrules) {
                        print"</td>\n";
                        print"<td class='base' width='90%'><b>$rulefile</b></td>\n";
                        print"<td class='base' width='5%' align='right'>\n";
-                       print"<a href=\"javascript:showhide('$categoryname')\">SHOW</a>\n";
+                       print"<a href=\"javascript:showhide('$categoryname')\"><span id='span_$categoryname'>$Lang::tr{'ids show'}</span></a>\n";
                        print"</td>\n";
                        print"</tr>\n";
 
@@ -1208,54 +1273,6 @@ sub get_memory_usage($) {
        return;
 }
 
-#
-## Function to generate the rules file with whitelisted addresses.
-#
-sub GenerateIgnoreFile() {
-       my %ignored = ();
-
-       # SID range 1000000-1999999 Reserved for Local Use
-       # Put your custom rules in this range to avoid conflicts
-       my $sid = 1500000;
-
-       # Read-in ignoredfile.
-       &General::readhasharray($IDS::ignored_file, \%ignored);
-
-       # Open ignorefile for writing.
-       open(FILE, ">$IDS::whitelist_file") or die "Could not write to $IDS::whitelist_file. $!\n";
-
-       # Config file header.
-       print FILE "# Autogenerated file.\n";
-       print FILE "# All user modifications will be overwritten.\n\n";
-
-       # Add all user defined addresses to the whitelist.
-       #
-       # Check if the hash contains any elements.
-       if (keys (%ignored)) {
-               # Loop through the entire hash and write the host/network
-               # and remark to the ignore file.
-               while ( (my $key) = each %ignored) {
-                       my $address = $ignored{$key}[0];
-                       my $remark = $ignored{$key}[1];
-                       my $status = $ignored{$key}[2];
-
-                       # Check if the status of the entry is "enabled".
-                       if ($status eq "enabled") {
-                               # Check if the address/network is valid.
-                               if ((&General::validip($address)) || (&General::validipandmask($address))) {
-                                       # Write rule line to the file to pass any traffic from this IP
-                                       print FILE "pass ip $address any -> any any (msg:\"pass all traffic from/to $address\"\; sid:$sid\;)\n";
-
-                                       # Increment sid.
-                                       $sid++;
-                               }
-                       }
-                }
-       }
-
-       close(FILE);
-}
-
 #
 ## Function to read-in the given enabled or disables sids file.
 #