CIDR netmask bans

This commit is contained in:
Savetheinternet 2011-11-22 00:25:00 +11:00
parent 3380c3008f
commit 6aa24cce92
2 changed files with 20 additions and 2 deletions

View File

@ -718,6 +718,10 @@
// A little more load on the database // A little more load on the database
$config['ban_range'] = true; $config['ban_range'] = true;
// Enable CDIR netmask bans (eg. "10.0.0.0/8" for 10.0.0.0.0 - 10.255.255.255). Useful for stopping persistent spammers.
// Again, a little more database load.
$config['ban_cidr'] = true;
// Do a DNS lookup on IP addresses to get their hostname on the IP summary page // Do a DNS lookup on IP addresses to get their hostname on the IP summary page
$config['mod']['dns_lookup'] = true; $config['mod']['dns_lookup'] = true;
// Show ban form on the IP summary page // Show ban form on the IP summary page

View File

@ -537,8 +537,7 @@
if(!isset($_SERVER['REMOTE_ADDR'])) { if(!isset($_SERVER['REMOTE_ADDR'])) {
// Server misconfiguration // Server misconfiguration
return; return;
} }
$query = prepare("SELECT `set`, `expires`, `reason`, `board`, `uri`, `bans`.`id` FROM `bans` LEFT JOIN `boards` ON `boards`.`id` = `board` WHERE (`board` IS NULL OR `uri` = :board) AND `ip` = :ip ORDER BY `expires` IS NULL DESC, `expires` DESC, `expires` DESC LIMIT 1"); $query = prepare("SELECT `set`, `expires`, `reason`, `board`, `uri`, `bans`.`id` FROM `bans` LEFT JOIN `boards` ON `boards`.`id` = `board` WHERE (`board` IS NULL OR `uri` = :board) AND `ip` = :ip ORDER BY `expires` IS NULL DESC, `expires` DESC, `expires` DESC LIMIT 1");
$query->bindValue(':ip', $_SERVER['REMOTE_ADDR']); $query->bindValue(':ip', $_SERVER['REMOTE_ADDR']);
@ -550,6 +549,21 @@
$query->bindValue(':board', $board); $query->bindValue(':board', $board);
$query->execute() or error(db_error($query)); $query->execute() or error(db_error($query));
} }
if($query->rowCount() < 1 && $config['ban_cidr']) {
// my most insane SQL query yet
$query = prepare("SELECT `set`, `expires`, `reason`, `board`, `uri`, `bans`.`id` FROM `bans` LEFT JOIN `boards` ON `boards`.`id` = `board` WHERE (`board` IS NULL OR `uri` = :board)
AND (
`ip` REGEXP '^(\[0-9]+\.\[0-9]+\.\[0-9]+\.\[0-9]+\)\/(\[0-9]+)$'
AND
:ip >= INET_ATON(SUBSTRING_INDEX(`ip`, '/', 1))
AND
:ip < INET_ATON(SUBSTRING_INDEX(`ip`, '/', 1)) + POW(2, 32 - SUBSTRING_INDEX(`ip`, '/', -1))
)
ORDER BY `expires` IS NULL DESC, `expires` DESC LIMIT 1");
$query->bindValue(':ip', ip2long($_SERVER['REMOTE_ADDR']));
$query->bindValue(':board', $board);
$query->execute() or error(db_error($query));
}
if($ban = $query->fetch()) { if($ban = $query->fetch()) {
if($ban['expires'] && $ban['expires'] < time()) { if($ban['expires'] && $ban['expires'] < time()) {