Improvements to forwarding hosts in Postfix

- No more premature EOF and no more leaking of bash processes
- Log result
- Correctly treat non-CIDR entries
- Adapt to schema change from df71e97
- Correctly report SQL failure
master
Michael Kuron 2017-04-22 14:28:37 +02:00
parent df71e97a09
commit 894d6234e9
2 changed files with 10 additions and 6 deletions

View File

@ -1,11 +1,12 @@
#!/bin/bash #!/bin/bash
while true; do while read QUERY; do
read QUERY
QUERY=($QUERY) QUERY=($QUERY)
if [ "${QUERY[0]}" != "get" ]; then if [ "${QUERY[0]}" != "get" ]; then
echo "500 dunno" echo "500 dunno"
continue continue
fi fi
echo $(curl -s http://172.22.1.251:8081/forwardinghosts.php?host=${QUERY[1]}) result=$(curl -s http://172.22.1.251:8081/forwardinghosts.php?host=${QUERY[1]})
logger -t whitelist_forwardinghosts -p mail.info "Look up ${QUERY[1]} on whitelist, result $result"
echo $result
done done

View File

@ -7,6 +7,7 @@ ini_set('error_reporting', 0);
function in_net($addr, $net) function in_net($addr, $net)
{ {
$net = explode('/', $net); $net = explode('/', $net);
if (count($net) > 1)
$mask = $net[1]; $mask = $net[1];
$net = inet_pton($net[0]); $net = inet_pton($net[0]);
$addr = inet_pton($addr); $addr = inet_pton($addr);
@ -14,6 +15,8 @@ function in_net($addr, $net)
$length = strlen($net); // 4 for IPv4, 16 for IPv6 $length = strlen($net); // 4 for IPv4, 16 for IPv6
if (strlen($net) != strlen($addr)) if (strlen($net) != strlen($addr))
return FALSE; return FALSE;
if (!isset($mask))
$mask = $length * 8;
$addr_bin = ''; $addr_bin = '';
$net_bin = ''; $net_bin = '';
@ -34,7 +37,7 @@ $opt = [
]; ];
try { try {
$pdo = new PDO($dsn, $database_user, $database_pass, $opt); $pdo = new PDO($dsn, $database_user, $database_pass, $opt);
$stmt = $pdo->query("SELECT * FROM `forwarding_hosts`"); $stmt = $pdo->query("SELECT host FROM `forwarding_hosts`");
$networks = $stmt->fetchAll(PDO::FETCH_COLUMN); $networks = $stmt->fetchAll(PDO::FETCH_COLUMN);
foreach ($networks as $network) foreach ($networks as $network)
{ {
@ -47,7 +50,7 @@ try {
echo '200 dunno'; echo '200 dunno';
} }
catch (PDOException $e) { catch (PDOException $e) {
echo 'settings { }'; echo '200 dunno';
exit; exit;
} }
?> ?>