You are here

function httpbl_check in http:BL 7

Same name and namespace in other branches
  1. 6.2 httpbl.module \httpbl_check()
  2. 6 httpbl.module \httpbl_check()

Check if an IP should be banned

Return value

constant: HTTPBL_LIST_*

3 calls to httpbl_check()
httpbl_boot in ./httpbl.module
Implementation of hook_boot()
httpbl_comment_presave in ./httpbl.module
Implementation of hook_comment_presave()
httpbl_whitelist_access in ./httpbl.module
Determine whether a user has access to the session whitelist functionality.
4 string references to 'httpbl_check'
httpbl_admin_settings in ./httpbl.admin.inc
Implementation of hook_admin_settings()
httpbl_boot in ./httpbl.module
Implementation of hook_boot()
httpbl_comment_presave in ./httpbl.module
Implementation of hook_comment_presave()
httpbl_requirements in ./httpbl.module
Implementation of hook_requirements().

File

./httpbl.module, line 184
Implementation of http:BL for Drupal. It provides IP-based blacklisting through http:BL and allows linking to a honeypot.

Code

function httpbl_check() {
  static $result;

  // Result was already calculated -- return.
  if (is_int($result)) {
    return $result;
  }
  $ip = ip_address();

  //$ip = '127.1.40.1'; // simulate greylist response for testing

  //$ip = '127.1.80.1'; // simulate blacklist response for testing

  // Check if user is whitelisted in any way
  if (_httpbl_whitelisted($ip)) {
    $result = HTTPBL_LIST_SAFE;
    if ($logs = variable_get('httpbl_log', HTTPBL_LOG_MIN) == HTTPBL_LOG_VERBOSE) {
      watchdog('httpbl', 'This IP has been whitelisted.', array(), WATCHDOG_DEBUG, NULL);
    }
  }
  elseif ($cache = variable_get('httpbl_cache', HTTPBL_CACHE_DBDRUPAL)) {
    $result = _httpbl_cache_get($ip);
    if ($logs = variable_get('httpbl_log', HTTPBL_LOG_MIN) == HTTPBL_LOG_VERBOSE) {
      watchdog('httpbl', 'Queried cache for host.  Host status is %results', array(
        '%results' => $result,
      ), WATCHDOG_DEBUG, NULL);
    }
  }

  // If user not white listed and not found in Honeyblock cache, we'll do a DNS Lookup
  if (!is_numeric($result)) {
    if ($logs = variable_get('httpbl_log', HTTPBL_LOG_MIN) == HTTPBL_LOG_VERBOSE) {
      watchdog('httpbl', 'Honeypot DNS Lookup for IP %ip.', array(
        '%ip' => $ip,
      ), WATCHDOG_DEBUG, NULL);
    }

    // Do a Project Honeypot DNS lookup, and continue if lookup was succesful
    if ($response = httpbl_dnslookup($ip)) {

      //     drupal_set_message(t('Check DNS IP - @ip.', array('@ip' => $ip)));
      $stats = variable_get('httpbl_stats', TRUE);

      // Blacklist?
      if ($response['threat'] > variable_get('httpbl_black_threshold', HTTPBL_THRESHOLD_BLACK) && $response['type']) {
        drupal_set_message(t('BLACKLISTED! - Extreme Honeypot Threat Level = @response.', array(
          '@response' => $response['threat'],
        )), 'error', FALSE);
        if (variable_get('httpbl_log', HTTPBL_LOG_MIN)) {
          watchdog('httpbl', '%ip will be blacklisted (%response)', array(
            '%ip' => $ip,
            '%response' => $response['raw'],
          ), WATCHDOG_WARNING, _httpbl_ipdata($ip));
        }
        if ($stats) {
          variable_set('httpbl_stat_black', variable_get('httpbl_stat_black', 0) + 1);
        }
        $result = HTTPBL_LIST_BLACK;
      }
      elseif ($response['threat'] > variable_get('httpbl_grey_threshold', HTTPBL_THRESHOLD_GREY) && $response['type']) {
        drupal_set_message(t('GREY-LISTED! - Honeypot Threat Level = @response.', array(
          '@response' => $response['threat'],
        )), 'warning', FALSE);
        if (variable_get('httpbl_log', HTTPBL_LOG_MIN)) {
          watchdog('httpbl', '%ip will be greylisted (%response)', array(
            '%ip' => $ip,
            '%response' => $response['raw'],
          ), WATCHDOG_WARNING, _httpbl_ipdata($ip));
        }
        if ($stats) {
          variable_set('httpbl_stat_grey', variable_get('httpbl_stat_grey', 0) + 1);
        }
        $result = HTTPBL_LIST_GREY;
      }
      else {
        if ($logs = variable_get('httpbl_log', HTTPBL_LOG_MIN) == HTTPBL_LOG_VERBOSE) {
          watchdog('httpbl', 'Honeypot profile found, but no threat. Will treat as safe.', array(), WATCHDOG_DEBUG, _httpbl_ipdata($ip));
        }
        $result = HTTPBL_LIST_SAFE;
      }

      // Cache the results - Use Blacklist offset settings (default 1 year)
      //             or Greylist offset settings (default 1 day)
      if ($cache) {
        if ($result == HTTPBL_LIST_BLACK) {
          _httpbl_cache_set($ip, $result, variable_get('httpbl_blacklist_offset', 31536000));
        }
        elseif ($result == HTTPBL_LIST_GREY) {
          _httpbl_cache_set($ip, $result, variable_get('httpbl_greylist_offset', 86400));
        }
      }
    }
    else {
      if ($cache) {
        _httpbl_cache_set($ip, HTTPBL_LIST_SAFE, variable_get('httpbl_safe_offset', 10800));
      }
      if ($logs = variable_get('httpbl_log', HTTPBL_LOG_MIN) == HTTPBL_LOG_VERBOSE) {
        watchdog('httpbl', 'No Honeypot profile. Will treat as safe.', array(), WATCHDOG_DEBUG, _httpbl_ipdata($ip));
      }
      $result = HTTPBL_LIST_SAFE;
    }
  }
  elseif (!($result == HTTPBL_LIST_SAFE)) {

    //drupal_set_message(t('Final result is @result.',array('@result' => $result)));
    drupal_set_message(t('Your IP address is restricted on this site.'), 'error', FALSE);
  }
  return $result;
}