Browse Source

Add ajax-get-host tool IPv6 compatibility

spaghetti 9 years ago
parent
commit
31b1a9eef4
2 changed files with 33 additions and 38 deletions
  1. 4
    10
      classes/tools.class.php
  2. 29
    28
      sections/tools/services/get_host.php

+ 4
- 10
classes/tools.class.php View File

@@ -131,16 +131,10 @@ class Tools {
131 131
   public static function lookup_ip($IP) {
132 132
     //TODO: use the G::$Cache
133 133
     $Output = explode(' ',shell_exec('host -W 1 '.escapeshellarg($IP)));
134
-    if (count($Output) == 1 && empty($Output[0])) {
135
-      //No output at all implies the command failed
136
-      return '';
137
-    }
138
-
139
-    if (count($Output) != 5) {
140
-      return false;
141
-    } else {
142
-      return trim($Output[4]);
143
-    }
134
+    if (count($Output) == 1 && empty($Output[0]))  { return ''; }
135
+    if (count($Output) != 5)                       { return false; }
136
+    if ($Output[2].' '.$Output[3] == 'not found:') { return false; }
137
+    return trim($Output[4]);
144 138
   }
145 139
 
146 140
   /**

+ 29
- 28
sections/tools/services/get_host.php View File

@@ -10,37 +10,38 @@ header('Last-Modified: '.date('D, d-M-Y H:i:s \U\T\C', time()));
10 10
 if (!check_perms('users_view_ips')) {
11 11
   die('Access denied.');
12 12
 }
13
+if (empty($_GET['ip'])) {
14
+  die('No IP given.');
15
+}
16
+$IP = $_GET['ip'];
17
+
18
+$Delimiter = $IP[strcspn($IP, ':.')];
19
+$OctOrHextets = explode($Delimiter, $IP);
13 20
 
14
-$Octets = explode('.', $_GET['ip']);
15
-if (
16
-  empty($_GET['ip'])
17
-  || !preg_match('/'.IP_REGEX.'/', $_GET['ip'])
18
-  || $Octets[0] < 0
19
-  || $Octets[0] > 255
20
-  || $Octets[1] < 0
21
-  || $Octets[1] > 255
22
-  || $Octets[2] < 0
23
-  || $Octets[2] > 255
24
-  || $Octets[3] < 0
25
-  || $Octets[3] > 255
26
-  /*
27
-   * Per RFC 1918, the following CIDR blocks should never be found on the public Internet.
28
-   *    10.0.0.0/8
29
-   *    172.16.0.0/12
30
-   *    192.168.0.0/16
31
-   *
32
-   * Per RFC 3330, the block 127.0.0.0/8 should never appear on any network.
33
-   *
34
-   */
35
-  || $Octets[0] == 127
36
-  || $Octets[0] == 10
37
-  || ($Octets[0] == 172 && ((16 <= $Octets[1]) && ($Octets[1] <= 31)))
38
-  || ($Octets[0] == 192 && $Octets[1] == 168)
39
-) {
40
-  die('Invalid IPv4 address.');
21
+
22
+if ($Delimiter == '.' && sizeof($OctOrHextets) == 4) { // IPv4
23
+  if ( ($OctOrHextets[0] == 127 || $OctOrHextets[0] == 10)
24
+    || ($OctOrHextets[0] == 192 && $OctOrHextets[1] == 168)
25
+    || ($OctOrHextets[0] == 172 && ($OctOrHextets[1] >= 16 && $OctOrHextets[1] <= 32))
26
+  ) {
27
+    die('Invalid IPv4 address.');
28
+  }
29
+  foreach($OctOrHextets as $Octet) {
30
+    if ($Octet > 255 || $Octet < 0) {
31
+      die('Invalid IPv4 address.');
32
+    }
33
+  }
34
+} else if (sizeof($OctOrHextets) <= 8) { // IPv6
35
+  foreach($OctOrHextets as $Hextet) {
36
+    if (strlen($Hextet) > 4) {
37
+      die('Invalid IPv6 address.');
38
+    }
39
+  }
40
+} else {
41
+  die('Invalid IP address.');
41 42
 }
42 43
 
43
-$Host = Tools::lookup_ip($_GET['ip']);
44
+$Host = Tools::lookup_ip($IP);
44 45
 
45 46
 if ($Host === '') {
46 47
   trigger_error('Tools::get_host_by_ajax() command failed with no output, ensure that the host command exists on your system and accepts the argument -W');

Loading…
Cancel
Save