C:\xampp\phpMyAdmin\libraries\classes\IpAllowDeny.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
<?php
/* vim: set expandtab sw=4 ts=4 sts=4: */
/**
 * This library is used with the server IP allow/deny host authentication
 * feature
 *
 * @package PhpMyAdmin
 */
namespace PhpMyAdmin;

use 
PhpMyAdmin\Core;

require_once 
'./libraries/hash.lib.php';

/**
 * PhpMyAdmin\IpAllowDeny class
 *
 * @package PhpMyAdmin
 */
class IpAllowDeny
{
    
/**
     * Matches for IPv4 or IPv6 addresses
     *
     * @param string $testRange string of IP range to match
     * @param string $ipToTest  string of IP to test against range
     *
     * @return boolean    whether the IP mask matches
     *
     * @access  public
     */
    
public static function ipMaskTest($testRange$ipToTest)
    {
        if (
mb_strpos($testRange':') > -1
            
|| mb_strpos($ipToTest':') > -1
        
) {
            
// assume IPv6
            
$result self::ipv6MaskTest($testRange$ipToTest);
        } else {
            
$result self::ipv4MaskTest($testRange$ipToTest);
        }

        return 
$result;
    } 
// end of the "self::ipMaskTest()" function

    /**
     * Based on IP Pattern Matcher
     * Originally by J.Adams <jna@retina.net>
     * Found on <https://secure.php.net/manual/en/function.ip2long.php>
     * Modified for phpMyAdmin
     *
     * Matches:
     * xxx.xxx.xxx.xxx        (exact)
     * xxx.xxx.xxx.[yyy-zzz]  (range)
     * xxx.xxx.xxx.xxx/nn     (CIDR)
     *
     * Does not match:
     * xxx.xxx.xxx.xx[yyy-zzz]  (range, partial octets not supported)
     *
     * @param string $testRange string of IP range to match
     * @param string $ipToTest  string of IP to test against range
     *
     * @return boolean    whether the IP mask matches
     *
     * @access  public
     */
    
public static function ipv4MaskTest($testRange$ipToTest)
    {
        
$result true;
        
$match preg_match(
            
'|([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)/([0-9]+)|',
            
$testRange,
            
$regs
        
);
        if (
$match) {
            
// performs a mask match
            
$ipl    ip2long($ipToTest);
            
$rangel ip2long(
                
$regs[1] . '.' $regs[2] . '.' $regs[3] . '.' $regs[4]
            );

            
$maskl  0;

            for (
$i 0$i 31$i++) {
                if (
$i $regs[5] - 1) {
                    
$maskl $maskl pow(2, (30 $i));
                } 
// end if
            
// end for

            
return ($maskl $rangel) == ($maskl $ipl);
        }

        
// range based
        
$maskocts explode('.'$testRange);
        
$ipocts   explode('.'$ipToTest);

        
// perform a range match
        
for ($i 0$i 4$i++) {
            if (
preg_match('|\[([0-9]+)\-([0-9]+)\]|'$maskocts[$i], $regs)) {
                if ((
$ipocts[$i] > $regs[2]) || ($ipocts[$i] < $regs[1])) {
                    
$result false;
                } 
// end if
            
} else {
                if (
$maskocts[$i] <> $ipocts[$i]) {
                    
$result false;
                } 
// end if
            
// end if/else
        
//end for

        
return $result;
    } 
// end of the "self::ipv4MaskTest()" function

    /**
     * IPv6 matcher
     * CIDR section taken from https://stackoverflow.com/a/10086404
     * Modified for phpMyAdmin
     *
     * Matches:
     * xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx
     * (exact)
     * xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:[yyyy-zzzz]
     * (range, only at end of IP - no subnets)
     * xxxx:xxxx:xxxx:xxxx/nn
     * (CIDR)
     *
     * Does not match:
     * xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xx[yyy-zzz]
     * (range, partial octets not supported)
     *
     * @param string $test_range string of IP range to match
     * @param string $ip_to_test string of IP to test against range
     *
     * @return boolean    whether the IP mask matches
     *
     * @access  public
     */
    
public static function ipv6MaskTest($test_range$ip_to_test)
    {
        
$result true;

        
// convert to lowercase for easier comparison
        
$test_range mb_strtolower($test_range);
        
$ip_to_test mb_strtolower($ip_to_test);

        
$is_cidr mb_strpos($test_range'/') > -1;
        
$is_range mb_strpos($test_range'[') > -1;
        
$is_single = ! $is_cidr && ! $is_range;

        
$ip_hex bin2hex(inet_pton($ip_to_test));

        if (
$is_single) {
            
$range_hex bin2hex(inet_pton($test_range));
            
$result hash_equals($ip_hex$range_hex);
            return 
$result;
        }

        if (
$is_range) {
            
// what range do we operate on?
            
$range_match = array();
            
$match preg_match(
                
'/\[([0-9a-f]+)\-([0-9a-f]+)\]/'$test_range$range_match
            
);
            if (
$match) {
                
$range_start $range_match[1];
                
$range_end   $range_match[2];

                
// get the first and last allowed IPs
                
$first_ip  str_replace($range_match[0], $range_start$test_range);
                
$first_hex bin2hex(inet_pton($first_ip));
                
$last_ip   str_replace($range_match[0], $range_end$test_range);
                
$last_hex  bin2hex(inet_pton($last_ip));

                
// check if the IP to test is within the range
                
$result = ($ip_hex >= $first_hex && $ip_hex <= $last_hex);
            }
            return 
$result;
        }

        if (
$is_cidr) {
            
// Split in address and prefix length
            
list($first_ip$subnet) = explode('/'$test_range);

            
// Parse the address into a binary string
            
$first_bin inet_pton($first_ip);
            
$first_hex bin2hex($first_bin);

            
$flexbits 128 $subnet;

            
// Build the hexadecimal string of the last address
            
$last_hex $first_hex;

            
$pos 31;
            while (
$flexbits 0) {
                
// Get the character at this position
                
$orig mb_substr($last_hex$pos1);

                
// Convert it to an integer
                
$origval hexdec($orig);

                
// OR it with (2^flexbits)-1, with flexbits limited to 4 at a time
                
$newval $origval | (pow(2min(4$flexbits)) - 1);

                
// Convert it back to a hexadecimal character
                
$new dechex($newval);

                
// And put that character back in the string
                
$last_hex substr_replace($last_hex$new$pos1);

                
// We processed one nibble, move to previous position
                
$flexbits -= 4;
                --
$pos;
            }

            
// check if the IP to test is within the range
            
$result = ($ip_hex >= $first_hex && $ip_hex <= $last_hex);
        }

        return 
$result;
    } 
// end of the "self::ipv6MaskTest()" function

    /**
     * Runs through IP Allow/Deny rules the use of it below for more information
     *
     * @param string $type 'allow' | 'deny' type of rule to match
     *
     * @return bool   Whether rule has matched
     *
     * @access  public
     *
     * @see     Core::getIp()
     */
    
public static function allowDeny($type)
    {
        global 
$cfg;

        
// Grabs true IP of the user and returns if it can't be found
        
$remote_ip Core::getIp();
        if (empty(
$remote_ip)) {
            return 
false;
        }

        
// copy username
        
$username  $cfg['Server']['user'];

        
// copy rule database
        
if (isset($cfg['Server']['AllowDeny']['rules'])) {
            
$rules     $cfg['Server']['AllowDeny']['rules'];
            if (! 
is_array($rules)) {
                
$rules = array();
            }
        } else {
            
$rules = array();
        }

        
// lookup table for some name shortcuts
        
$shortcuts = array(
            
'all'       => '0.0.0.0/0',
            
'localhost' => '127.0.0.1/8'
        
);

        
// Provide some useful shortcuts if server gives us address:
        
if (Core::getenv('SERVER_ADDR')) {
            
$shortcuts['localnetA'] = Core::getenv('SERVER_ADDR') . '/8';
            
$shortcuts['localnetB'] = Core::getenv('SERVER_ADDR') . '/16';
            
$shortcuts['localnetC'] = Core::getenv('SERVER_ADDR') . '/24';
        }

        foreach (
$rules as $rule) {
            
// extract rule data
            
$rule_data explode(' '$rule);

            
// check for rule type
            
if ($rule_data[0] != $type) {
                continue;
            }

            
// check for username
            
if (($rule_data[1] != '%'//wildcarded first
                
&& (! hash_equals($rule_data[1], $username))
            ) {
                continue;
            }

            
// check if the config file has the full string with an extra
            // 'from' in it and if it does, just discard it
            
if ($rule_data[2] == 'from') {
                
$rule_data[2] = $rule_data[3];
            }

            
// Handle shortcuts with above array
            
if (isset($shortcuts[$rule_data[2]])) {
                
$rule_data[2] = $shortcuts[$rule_data[2]];
            }

            
// Add code for host lookups here
            // Excluded for the moment

            // Do the actual matching now
            
if (self::ipMaskTest($rule_data[2], $remote_ip)) {
                return 
true;
            }
        } 
// end while

        
return false;
    } 
// end of the "self::allowDeny()" function
}
x

Windows NT KPTV 6.2 build 9200 (Windows Server 2012 Datacenter Edition) i586