Fix: filesystem report
[gnucomo.git] / src / gcm_daemon / gcm_daemon.php
index d4292e1..38e0d3a 100755 (executable)
@@ -99,9 +99,6 @@ ini_set('html_errors', 'false');
 
 define("BATCHSIZE", 50000);
 
-//Tell the log that we're up.
-define_syslog_variables();
-
 require_once "gnucomo_config.php";
 require_once "db.class.php";
 
@@ -110,7 +107,7 @@ require_once "db.class.php";
 $project_name   = "gnucomo";    // name of the entire project
 $app_name       = "gcm_daemon"; // name of the application running
 $developrelease = "FALSE";      // Indicates if special debug settings are needed
-$db_version     = 53;           // The db_version indicates what the level of
+$db_version     = 54;           // The db_version indicates what the level of
                                 // the database should be. If the database is
                                 // old an update will be generated.
 $gcmd_version   = 5;            // This value indicates the active version of
@@ -225,7 +222,7 @@ if ($dbms->fetch_row() == "TRUE")
 do
 {
 
-   echo "Processing logs...\n";
+   //echo "Processing logs...\n";
    process_log ();
    service_check();
    //mail_notifications();
@@ -236,7 +233,7 @@ do
    for ($obj = 0; $obj < $dbms->num_rows($obj_result); $obj++)
    {
       $object = $dbms->fetch_object($obj_result, $obj);
-      echo "Gathering statistics for object " . $object->objectid . "\n";
+      // echo "Gathering statistics for object " . $object->objectid . "\n";
       GatherStatistics($object->objectid);
    }
 
@@ -275,7 +272,7 @@ function process_log ()
      $last_log = $dbms->db_result_row[0];
   }
 
-  echo "Last processed logid = $last_log \n";
+  //echo "Last processed logid = $last_log \n";
 
   //Query the log-table
   $log_limit = $last_log + BATCHSIZE;
@@ -327,7 +324,7 @@ function process_log ()
        $local_sql = "UPDATE db_value SET setting_value = '"
                    . $local_upper_row ."' where setting = 'log_processing'";
        $dbms->query($local_sql);
-
+   
 
        // Update the statistics for the object-table
 
@@ -487,20 +484,26 @@ function match_log_patterns($logstart)
                //echo "Recording abuse for address ", $srv_pat->argument, "\n  Log entry:\n  ";
                //echo $logentry->rawdata, "\n  Pattern:\n  ", $srv_pat->pattern, "\n\n";
 
-               if (record_abuse($logentry->logid, $logentry->objectid, $srv_pat->argument, 1) >= 32)
+               $nr_abuses = record_abuse($logentry->logid, $logentry->objectid, $srv_pat->argument, 1);
+               if ($nr_abuses < 0)
+               {
+                  echo "ERROR in recording abuse for address ", $srv_pat->argument, "\n  Log entry:\n  ";
+                  echo $logentry->rawdata, "\n  Pattern:\n  ", $srv_pat->pattern, "\n\n";
+               }
+               if ($nr_abuses >= 32)
                {
                   $source_ip = gethostbyname($srv_pat->argument);
                   $notif = 'abuses exceeded';
                   if (!isset($notifications[$logentry->objectid][$notif][$source_ip]))
                   {
-                     echo "Creating notification $notif for object " . $logentry->objectid . ".\n";
+                     //echo "Creating notification $notif for object " . $logentry->objectid . ".\n";
                      $remark = "Abuses from IP address $source_ip exceeded the limit.";
                      $notifid = $dbms->new_notification($logentry->objectid, $notif, $remark);
                      $notifications[$logentry->objectid][$notif][$source_ip] = $notifid;
 
                      //  Add log entries from previously detected abuses
 
-                     echo " Add log entries from previously detected abuses\n";
+                     //echo " Add log entries from previously detected abuses\n";
                      $abuses = $dbms->query("SELECT logid FROM log_abuse WHERE objectid = '" .
                                     $logentry->objectid . "' AND source = '$source_ip'");
                      for ($abusenr = 0; $abusenr < $dbms->num_rows($abuses); $abusenr++)
@@ -559,36 +562,43 @@ function record_abuse($logid, $objectid, $sourceip, $points)
 
    $abres = $dbms->query("SELECT * FROM object_abuse WHERE objectid='$objectid' AND source='$sourceip'");
 
-   if (pg_numrows($abres) == 0 && $points > 0)
+   if ($abres == false)
    {
-      //echo "$sourceip is new.\n";
-      $dbms->query("INSERT INTO object_abuse VALUES ('$objectid', '$sourceip', '$points', '', NOW())");
-      $dbms->query("INSERT INTO log_abuse VALUES ('$logid', '$objectid', '$sourceip')");
+      $abuse_points = -1;   // return an error
    }
-   else if (pg_numrows($abres) != 0)
+   else
    {
-      $abuse = $dbms->fetch_object($abres, 0);
-      if ($abuse->status == '' || $abuse->status == 'dropped')
+      if (pg_numrows($abres) == 0 && $points > 0)
+      {
+         //echo "$sourceip is new.\n";
+         $dbms->query("INSERT INTO object_abuse VALUES ('$objectid', '$sourceip', '$points', '', NOW())");
+         $dbms->query("INSERT INTO log_abuse VALUES ('$logid', '$objectid', '$sourceip')");
+      }
+      else if (pg_numrows($abres) != 0)
       {
-         $abuse_points = $abuse->nr_abuses + $points;
-         if ($abuse_points < 0)
+         $abuse = $dbms->fetch_object($abres, 0);
+         if ($abuse->status == '' || $abuse->status == 'dropped')
          {
-            $abuse_points = 0;
-         }
-         //echo $sourceip . " will get " . $abuse_points . " abuse points, ";
-         //echo "Status was " . $abuse->status . "\n";
-         $dbms->query("UPDATE object_abuse SET nr_abuses='$abuse_points'" .
-                     ", last_change=NOW() WHERE objectid='$objectid' AND source='$sourceip'");
+            $abuse_points = $abuse->nr_abuses + $points;
+            if ($abuse_points < 0)
+            {
+               $abuse_points = 0;
+            }
+            //echo $sourceip . " will get " . $abuse_points . " abuse points, ";
+            //echo "Status was " . $abuse->status . "\n";
+            $dbms->query("UPDATE object_abuse SET nr_abuses='$abuse_points'" .
+                        ", last_change=NOW() WHERE objectid='$objectid' AND source='$sourceip'");
 
-         if ($points > 0)
-         {
-            $dbms->query("INSERT INTO log_abuse VALUES ('$logid', '$objectid', '$sourceip')");
-         }
-         if ($abuse_points >= 32)
-         {
-            //echo "      BLOCK IP adrress $sourceip on the firewall.\n";
-            $dbms->query("UPDATE object_abuse SET status='dropped'" .
-                     " WHERE objectid='$objectid' AND source='$sourceip'");
+            if ($points > 0)
+            {
+               $dbms->query("INSERT INTO log_abuse VALUES ('$logid', '$objectid', '$sourceip')");
+            }
+            if ($abuse_points >= 32)
+            {
+               //echo "      BLOCK IP adrress $sourceip on the firewall.\n";
+               $dbms->query("UPDATE object_abuse SET status='dropped'" .
+                        " WHERE objectid='$objectid' AND source='$sourceip'");
+            }
          }
       }
    }
@@ -625,7 +635,7 @@ function service_check()
                             VALUES ('log_servicecheck', '0')");
    }
 
-   echo "Running service check from log id $last_log.\n";
+   //echo "Running service check from log id $last_log.\n";
    // Query the log-table
 
    $log_limit = $last_log + BATCHSIZE;
@@ -756,5 +766,18 @@ function expand_arguments($command, $args)
    return $command;
 }
 
-?>
+// ereg function are removed from PHP since version 7.0
+// These wrappers use the alternative preg functions.
 
+function ereg($pattern, $string, &$matches)
+{
+   $pattern = "#" . $pattern . "#";
+   return preg_match($pattern, $string, $matches);
+}
+
+function ereg_replace($pattern, $replacement, $string)
+{
+   $pattern = "#" . $pattern . "#";
+   return preg_replace($pattern, $replacement, $string);
+}
+?>