blob: 10285b715e867772485503d24738253cf500e69b [file] [log] [blame]
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01001<?php
2header('Content-Type: text/plain');
3ini_set('error_reporting', 0);
4
5$redis = new Redis();
6$redis->connect('redis-mailcow', 6379);
7
8function in_net($addr, $net) {
9 $net = explode('/', $net);
10 if (count($net) > 1) {
11 $mask = $net[1];
12 }
13 $net = inet_pton($net[0]);
14 $addr = inet_pton($addr);
15 $length = strlen($net); // 4 for IPv4, 16 for IPv6
16 if (strlen($net) != strlen($addr)) {
17 return false;
18 }
19 if (!isset($mask)) {
20 $mask = $length * 8;
21 }
22 $addr_bin = '';
23 $net_bin = '';
24 for ($i = 0; $i < $length; ++$i) {
25 $addr_bin .= str_pad(decbin(ord(substr($addr, $i, $i+1))), 8, '0', STR_PAD_LEFT);
26 $net_bin .= str_pad(decbin(ord(substr($net, $i, $i+1))), 8, '0', STR_PAD_LEFT);
27 }
28 return substr($addr_bin, 0, $mask) == substr($net_bin, 0, $mask);
29}
30
31if (isset($_GET['host'])) {
32 try {
33 foreach ($redis->hGetAll('WHITELISTED_FWD_HOST') as $host => $source) {
34 if (in_net($_GET['host'], $host)) {
35 echo '200 PERMIT';
36 exit;
37 }
38 }
39 echo '200 DUNNO';
40 }
41 catch (RedisException $e) {
42 echo '200 DUNNO';
43 exit;
44 }
45} else {
46 try {
47 echo '240.240.240.240' . PHP_EOL;
48 foreach ($redis->hGetAll('WHITELISTED_FWD_HOST') as $host => $source) {
49 echo $host . PHP_EOL;
50 }
51 }
52 catch (RedisException $e) {
53 echo '240.240.240.240' . PHP_EOL;
54 exit;
55 }
56}
57?>