Browse Source

! Few things related to IPv6

Signed-off-by: emanuele <[email protected]>
emanuele 12 years ago
parent
commit
c03e08d83e
5 changed files with 68 additions and 80 deletions
  1. 14 7
      Sources/ManageBans.php
  2. 5 21
      Sources/Profile-View.php
  3. 2 2
      Sources/QueryString.php
  4. 46 49
      Sources/Security.php
  5. 1 1
      Sources/Subs.php

+ 14 - 7
Sources/ManageBans.php

@@ -1261,18 +1261,26 @@ function BanBrowseTriggers()
 					$rowData[\'ip_low1\'],
 					$rowData[\'ip_low2\'],
 					$rowData[\'ip_low3\'],
-					$rowData[\'ip_low4\']
+					$rowData[\'ip_low4\'],
+					$rowData[\'ip_low5\'],
+					$rowData[\'ip_low6\'],
+					$rowData[\'ip_low7\'],
+					$rowData[\'ip_low8\']
 				), array(
 					$rowData[\'ip_high1\'],
 					$rowData[\'ip_high2\'],
 					$rowData[\'ip_high3\'],
-					$rowData[\'ip_high4\']
+					$rowData[\'ip_high4\'],
+					$rowData[\'ip_high5\'],
+					$rowData[\'ip_high6\'],
+					$rowData[\'ip_high7\'],
+					$rowData[\'ip_high8\']
 				));
 			'),
 		);
 		$listOptions['columns']['banned_entity']['sort'] = array(
-			'default' => 'bi.ip_low1, bi.ip_high1, bi.ip_low2, bi.ip_high2, bi.ip_low3, bi.ip_high3, bi.ip_low4, bi.ip_high4',
-			'reverse' => 'bi.ip_low1 DESC, bi.ip_high1 DESC, bi.ip_low2 DESC, bi.ip_high2 DESC, bi.ip_low3 DESC, bi.ip_high3 DESC, bi.ip_low4 DESC, bi.ip_high4 DESC',
+			'default' => 'bi.ip_low1, bi.ip_high1, bi.ip_low2, bi.ip_high2, bi.ip_low3, bi.ip_high3, bi.ip_low4, bi.ip_high4, bi.ip_low5, bi.ip_high5, bi.ip_low6, bi.ip_high6, bi.ip_low7, bi.ip_high7, bi.ip_low8, bi.ip_high8',
+			'reverse' => 'bi.ip_low1 DESC, bi.ip_high1 DESC, bi.ip_low2 DESC, bi.ip_high2 DESC, bi.ip_low3 DESC, bi.ip_high3 DESC, bi.ip_low4 DESC, bi.ip_high4 DESC, bi.ip_low5 DESC, bi.ip_high5 DESC, bi.ip_low6 DESC, bi.ip_high6 DESC, bi.ip_low7 DESC, bi.ip_high7 DESC, bi.ip_low8 DESC, bi.ip_high8 DESC',
 		);
 	}
 	elseif ($context['selected_entity'] === 'hostname')
@@ -1625,7 +1633,7 @@ function list_getNumBanLogEntries()
 function range2ip($low, $high)
 {
 	// IPv6 check.
-	if (!empty($low[5]))
+	if (!empty($high[4]) || !empty($high[5]) || !empty($high[6]) || !empty($high[7]))
 	{
 		if (count($low) != 8 || count($high) != 8)
 			return '';
@@ -1646,8 +1654,7 @@ function range2ip($low, $high)
 
 	// Legacy IPv4 stuff.
 	// (count($low) != 4 || count($high) != 4) would not work because $low and $high always contain 8 elements!
-	for ($i = 4; $i < 8; $i++)
-		if (!empty($low[$i]) || !empty($high[$i]))
+	if ((count($low) != 4 || count($high) != 4) && (count($low) != 8 || count($high) != 8))
 			return '';
 
 	$ip = array();

+ 5 - 21
Sources/Profile-View.php

@@ -125,29 +125,13 @@ function summary($memID)
 			'time' => time(),
 		);
 		$ban_query[] = 'id_member = ' . $context['member']['id'];
-
-		// Valid IP?
-		if (preg_match('/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/', $memberContext[$memID]['ip'], $ip_parts) == 1)
+		$ban_query[] = constructBanQueryIP($memberContext[$memID]['ip']);
+		// Do we have a hostname already?
+		if (!empty($context['member']['hostname']))
 		{
-			$ban_query[] = '((' . $ip_parts[1] . ' BETWEEN bi.ip_low1 AND bi.ip_high1)
-						AND (' . $ip_parts[2] . ' BETWEEN bi.ip_low2 AND bi.ip_high2)
-						AND (' . $ip_parts[3] . ' BETWEEN bi.ip_low3 AND bi.ip_high3)
-						AND (' . $ip_parts[4] . ' BETWEEN bi.ip_low4 AND bi.ip_high4))';
-
-			// Do we have a hostname already?
-			if (!empty($context['member']['hostname']))
-			{
-				$ban_query[] = '({string:hostname} LIKE hostname)';
-				$ban_query_vars['hostname'] = $context['member']['hostname'];
-			}
+			$ban_query[] = '({string:hostname} LIKE hostname)';
+			$ban_query_vars['hostname'] = $context['member']['hostname'];
 		}
-		// Use '255.255.255.255' for 'unknown' - it's not valid anyway.
-		elseif ($memberContext[$memID]['ip'] == 'unknown')
-			$ban_query[] = '(bi.ip_low1 = 255 AND bi.ip_high1 = 255
-						AND bi.ip_low2 = 255 AND bi.ip_high2 = 255
-						AND bi.ip_low3 = 255 AND bi.ip_high3 = 255
-						AND bi.ip_low4 = 255 AND bi.ip_high4 = 255)';
-
 		// Check their email as well...
 		if (strlen($context['member']['email']) != 0)
 		{

+ 2 - 2
Sources/QueryString.php

@@ -308,7 +308,7 @@ function isValidIPv6($ip)
 /**
  * Converts IPv6s to numbers.  This makes ban checks much easier.
  * @param string $ip ip address to be converted
- * @return string 
+ * @return array 
  */
 function convertIPv6toInts($ip)
 {
@@ -326,7 +326,7 @@ function convertIPv6toInts($ip)
 		$new_ip[] = hexdec($int);
 
 	// Save this incase of repeated use.
-	$expanded[$ip] = implode($new_ip, '-');
+	$expanded[$ip] = $new_ip;
 
 	return $expanded[$ip];
 }

+ 46 - 49
Sources/Security.php

@@ -192,59 +192,19 @@ function is_not_banned($forceCheck = false)
 		// Check both IP addresses.
 		foreach (array('ip', 'ip2') as $ip_number)
 		{
-			// First attempt a IPv6 address.
-			if (strpos($user_info[$ip_number], ':') !== false)
+			if ($ip_number == 'ip2' && $user_info['ip2'] == $user_info['ip'])
+				continue;
+			$ban_query[] = constructBanQueryIP($user_info[$ip_number]);
+			// IP was valid, maybe there's also a hostname...
+			if (empty($modSettings['disableHostnameLookup']) && $user_info[$ip_number] != 'unknown')
 			{
-				if ($ip_number == 'ip2' && $user_info['ip2'] == $user_info['ip'])
-					continue;
-
-				$ip_parts = array_map('hexdec', explode(':', expandIPv6($user_info[$ip_number])));
-
-				$ban_query[] = '((' . $ip_parts[0] . ' BETWEEN bi.ip_low1 AND bi.ip_high1)
-							AND (' . $ip_parts[1] . ' BETWEEN bi.ip_low2 AND bi.ip_high2)
-							AND (' . $ip_parts[2] . ' BETWEEN bi.ip_low3 AND bi.ip_high3)
-							AND (' . $ip_parts[3] . ' BETWEEN bi.ip_low4 AND bi.ip_high4)
-							AND (' . $ip_parts[4] . ' BETWEEN bi.ip_low5 AND bi.ip_high5)
-							AND (' . $ip_parts[5] . ' BETWEEN bi.ip_low6 AND bi.ip_high6)
-							AND (' . $ip_parts[6] . ' BETWEEN bi.ip_low7 AND bi.ip_high7)
-							AND (' . $ip_parts[7] . ' BETWEEN bi.ip_low8 AND bi.ip_high8))';
-
-				// IP was valid, maybe there's also a hostname...
-				if (empty($modSettings['disableHostnameLookup']))
+				$hostname = host_from_ip($user_info[$ip_number]);
+				if (strlen($hostname) > 0)
 				{
-					$hostname = host_from_ip($user_info[$ip_number]);
-					if (strlen($hostname) > 0)
-					{
-						$ban_query[] = '({string:hostname} LIKE bi.hostname)';
-						$ban_query_vars['hostname'] = $hostname;
-					}
-				}
-			}
-			// Check if we have a valid IPv4 address.
-			elseif (preg_match('/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/', $user_info[$ip_number], $ip_parts) == 1)
-			{
-				$ban_query[] = '((' . $ip_parts[1] . ' BETWEEN bi.ip_low1 AND bi.ip_high1)
-							AND (' . $ip_parts[2] . ' BETWEEN bi.ip_low2 AND bi.ip_high2)
-							AND (' . $ip_parts[3] . ' BETWEEN bi.ip_low3 AND bi.ip_high3)
-							AND (' . $ip_parts[4] . ' BETWEEN bi.ip_low4 AND bi.ip_high4))';
-
-				// IP was valid, maybe there's also a hostname...
-				if (empty($modSettings['disableHostnameLookup']))
-				{
-					$hostname = host_from_ip($user_info[$ip_number]);
-					if (strlen($hostname) > 0)
-					{
-						$ban_query[] = '({string:hostname} LIKE bi.hostname)';
-						$ban_query_vars['hostname'] = $hostname;
-					}
+					$ban_query[] = '({string:hostname} LIKE bi.hostname)';
+					$ban_query_vars['hostname'] = $hostname;
 				}
 			}
-			// We use '255.255.255.255' for 'unknown' since it's not valid anyway.
-			elseif ($user_info['ip'] == 'unknown')
-				$ban_query[] = '(bi.ip_low1 = 255 AND bi.ip_high1 = 255
-							AND bi.ip_low2 = 255 AND bi.ip_high2 = 255
-							AND bi.ip_low3 = 255 AND bi.ip_high3 = 255
-							AND bi.ip_low4 = 255 AND bi.ip_high4 = 255)';
 		}
 
 		// Is their email address banned?
@@ -1255,4 +1215,41 @@ else
 		return true;
 }
 
+/**
+ * Another helper function that put together the 
+ * @param string $fullip An IP address either IPv6 or not
+ * @return string A SQL condition
+ */
+function constructBanQueryIP($fullip)
+{
+	// First attempt a IPv6 address.
+	if (isValidIPv6($fullip))
+	{
+		$ip_parts = convertIPv6toInts($fullip);
+
+		$ban_query = '((' . $ip_parts[0] . ' BETWEEN bi.ip_low1 AND bi.ip_high1)
+			AND (' . $ip_parts[1] . ' BETWEEN bi.ip_low2 AND bi.ip_high2)
+			AND (' . $ip_parts[2] . ' BETWEEN bi.ip_low3 AND bi.ip_high3)
+			AND (' . $ip_parts[3] . ' BETWEEN bi.ip_low4 AND bi.ip_high4)
+			AND (' . $ip_parts[4] . ' BETWEEN bi.ip_low5 AND bi.ip_high5)
+			AND (' . $ip_parts[5] . ' BETWEEN bi.ip_low6 AND bi.ip_high6)
+			AND (' . $ip_parts[6] . ' BETWEEN bi.ip_low7 AND bi.ip_high7)
+			AND (' . $ip_parts[7] . ' BETWEEN bi.ip_low8 AND bi.ip_high8))';
+	}
+	// Check if we have a valid IPv4 address.
+	elseif (preg_match('/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/', $fullip, $ip_parts) == 1)
+		$ban_query = '((' . $ip_parts[1] . ' BETWEEN bi.ip_low1 AND bi.ip_high1)
+			AND (' . $ip_parts[2] . ' BETWEEN bi.ip_low2 AND bi.ip_high2)
+			AND (' . $ip_parts[3] . ' BETWEEN bi.ip_low3 AND bi.ip_high3)
+			AND (' . $ip_parts[4] . ' BETWEEN bi.ip_low4 AND bi.ip_high4))';
+	// We use '255.255.255.255' for 'unknown' since it's not valid anyway.
+	else
+		$ban_query = '(bi.ip_low1 = 255 AND bi.ip_high1 = 255
+			AND bi.ip_low2 = 255 AND bi.ip_high2 = 255
+			AND bi.ip_low3 = 255 AND bi.ip_high3 = 255
+			AND bi.ip_low4 = 255 AND bi.ip_high4 = 255)';
+
+	return $ban_query;
+}
+
 ?>

+ 1 - 1
Sources/Subs.php

@@ -3331,7 +3331,7 @@ function getLegacyAttachmentFilename($filename, $attachment_id, $dir = null, $ne
  * internal function used to convert a user-readable format to a format suitable for the database.
  *
  * @param string $fullip
- * @return array|string 'unknown' if the ip in the input was '255.255.255.255'
+ * @return array 
  */
 function ip2range($fullip)
 {