You are here

function httpbl_cron in http:BL 8

Same name and namespace in other branches
  1. 5 httpbl.module \httpbl_cron()
  2. 6.2 httpbl.module \httpbl_cron()
  3. 6 httpbl.module \httpbl_cron()
  4. 7 httpbl.module \httpbl_cron()

Implements hook_cron().

Removes expired IPs from the 'httpbl' table. Also, if configured to auto ban blacklisted IPs, expired blacklisted IPs in Httpbl will be removed from Drupal's core 'ban_ip' table.

File

./httpbl.module, line 323
Implements Project Honeypot's http:BL for Drupal. It provides IP-based blacklisting through http:BL and allows linking to a honeypot.

Code

function httpbl_cron() {

  // IMPORTANT! For the sake of running this with drush cron, need to compare
  // the actual integer values represented by the CONSTANT.  The CONSTANT use
  // is fine for cron auto-running and from UI, but running cron from command
  // line via drush, the CONSTANT values appear to be ignored, resulting in
  // incorrect results from the comparison logic.
  // Only continue if any level of host storage is enabled.
  if (\Drupal::state()
    ->get('httpbl.storage') > HTTPBL_DB_OFF || \Drupal::state()
    ->get('httpbl.storage') > 0) {

    // Make sure both managers are available or else we abort.
    if (\Drupal::hasService('ban.ip_manager') && \Drupal::hasService('httpbl.evaluator')) {
      $httpblManager = \Drupal::service('httpbl.evaluator');
      $banManager = \Drupal::service('ban.ip_manager');
      $logTrapper = \Drupal::service('httpbl.logtrapper');
    }
    else {

      // Log the failure.
      \Drupal::logger('httpbl')
        ->error('Httpbl cron expire failed! Critical services (httpbl.evaluator and/or ban.ip_manager) are not available.');
      return NULL;
    }

    // Count all expired Hosts in httpbl_host.
    $now = \Drupal::time()
      ->getRequestTime();
    $hostsCount = HostQuery::countExpiredHosts($now);

    // Log the count.
    $logTrapper
      ->trapInfo('@count evaluated hosts to be expired.', [
      '@count' => $hostsCount,
    ]);

    // Gather all expired Hosts.
    $expiredHosts = HostQuery::loadExpiredHosts($now);

    // Check if also auto-banning in Drupal's 'ban_ip' table, then remove
    // any of those first, based on httpbl expiry.
    if (\Drupal::state()
      ->get('httpbl.storage') == HTTPBL_DB_HH_DRUPAL || \Drupal::state()
      ->get('httpbl.storage') == 2) {
      foreach ($expiredHosts as $key => $host) {
        $host = Host::load($key);
        $host_ip = $host
          ->getHostIp();
        $status = $host
          ->getHostStatus();

        // Humanize the status codes for messages.
        $human = $httpblManager
          ->getHumanStatus($status);

        // Find expired IPs that have also been banned.
        $banned = $banManager
          ->isBanned($host_ip);

        // If found in ban_ip, un-ban them.
        if ($banned) {
          $banManager
            ->unBanIp($host_ip);
          $logTrapper
            ->trapDebug('Expired @human host @ip has been un-banned.', [
            '@human' => $human,
            '@ip' => $host_ip,
          ]);
        }
      }
    }

    // Now remove expired IPs from httpbl.
    foreach ($expiredHosts as $key => $host) {
      $host = Host::load($key);
      $host_ip = $host
        ->getHostIp();
      $status = $host
        ->getHostStatus();
      $human = $httpblManager
        ->getHumanStatus($status);
      $logTrapper
        ->trapDebug('@human @ip has expired.', [
        '@human' => $human,
        '@ip' => $host_ip,
      ]);

      // There could be a situation where auto-banning was once enabled, then
      // later disabled, which would leave httpbl expired and removed IPs still
      // being banned in core ban_ip table.  So check for those while removing
      // from httpbl, and issue a warning that they are still banned.
      $banned = $banManager
        ->isBanned($host_ip);
      $host
        ->delete();

      // Warning that expired IP still banned.
      if ($banned) {
        $logTrapper
          ->trapWarning('Expired @human @ip is still banned but removed from httpbl_host.', [
          '@human' => $human,
          '@ip' => $host_ip,
        ]);
      }
      else {

        // Simple info message that the IP has been expired.
        $logTrapper
          ->trapDebug('Expired @human host @ip has been deleted.', [
          '@human' => $human,
          '@ip' => $host_ip,
        ]);
      }
    }
  }
}