diff options
author | Erich Eckner <git@eckner.net> | 2019-03-30 21:25:39 +0100 |
---|---|---|
committer | Erich Eckner <git@eckner.net> | 2019-03-30 21:25:39 +0100 |
commit | e427cd05681b57a5b40915cfbba29ff867c441bb (patch) | |
tree | dc95d73b72f523ea250b51b68a2b95fe115ec4ae | |
parent | 7955c1d641eb0a58dfb83653ca833d430643c4e0 (diff) | |
download | ddns-e427cd05681b57a5b40915cfbba29ff867c441bb.tar.xz |
ddns.php: add ip to each subdomain with a matching filter (v4 and v6 predefined)
-rw-r--r-- | ddns.php | 68 |
1 files changed, 52 insertions, 16 deletions
@@ -1,5 +1,9 @@ <?php +// $filter = array(v4, v6, file('/srv/http/vhosts/eckner.net/ddns/ip-filter')) +// $x \in array($token, $token.$filter foreach $filter with match): +// $x.ddns.eckner.net (all) + function lifetime($addr) { $addrTeile = explode('.', $addr); if (($addrTeile[0] == '10') || @@ -33,43 +37,75 @@ $ips = array_unique($ips); - unset($hasRrType); + $hasRrType = array(); + $rrTypes = array(); + $filterMatches = array(); + + $filters = array(); + foreach (file('/srv/http/vhosts/eckner.net/ddns/ip-filters') as $filterLine) { + if (substr($filterLine, 0, 1) == '#') + continue; + $filter = explode(' ', $filterLine); + if (count($filter) < 2) + continue; + $filters[] = array( + 'name' => $filter[0], + 'regex' => substr($filter[1], 0, -1) + ); + } foreach ($ips as $i => $ip) { + $filterMatches[$i] = array(); if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) { $rrTypes[$i] = 'A'; $hasRrType['A'] = 1; + $filterMatches[$i][] = 'v4'; } elseif (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) { $rrTypes[$i] = 'AAAA'; $hasRrType['AAAA'] = 1; + $filterMatches[$i][] = 'v6'; } else die(); + foreach ($filters as $filter) { + if (preg_match('@' . $filter['regex'] . '@', $ip) == 1) + $filterMatches[$i][] = $filter['name']; + } + $filterMatches[$i] = array_unique($filterMatches[$i]); } - $updateCommand = ''; + $updateCommand = "zone ddns.eckner.net.\n"; $logContent = ''; foreach ($hasRrType as $rrType => $dummy) { // delete old record if existing $updateCommand .= - "zone ddns.eckner.net.\n". - "prereq yxrrset " . $domain . ".ddns.eckner.net IN " . $rrType . "\n". - "update delete " . $domain . ".ddns.eckner.net IN " . $rrType . "\n". - "send\n". - "zone ddns.eckner.net.\n"; - // set new records no matter what - foreach ($ips as $i => $ip) - if ($rrTypes[$i] == $rrType) { - $updateCommand .= - "update add " . $domain . ".ddns.eckner.net. " . lifetime($ip) . " IN " . $rrType . " " . $ip . "\n"; - $logContent .= - date('Y-m-d H:i:s') . ' ' . $domain . ' ' . $ip . "\n"; - } - $updateCommand .= + "prereq yxrrset " . $domain . ".ddns.eckner.net IN " . $rrType . "\n" . + "update delete " . $domain . ".ddns.eckner.net IN " . $rrType . "\n" . "send\n"; + foreach ($filters as $filter) { + $updateCommand .= + "prereq yxrrset " . $domain . "." . $filter['name'] . ".ddns.eckner.net IN " . $rrType . "\n" . + "update delete " . $domain . "." . $filter['name'] . ".ddns.eckner.net IN " . $rrType . "\n" . + "send\n"; + } + } + // set new records no matter what + foreach ($ips as $i => $ip) { + $updateCommand .= + "update add " . $domain . ".ddns.eckner.net. " . lifetime($ip) . " IN " . $rrTypes[$i] . " " . $ip . "\n"; + $logContent .= + date('Y-m-d H:i:s') . ' ' . $domain . ' ' . $ip . "\n"; + foreach ($filterMatches[$i] as $filter) { + $updateCommand .= + "update add " . $domain . "." . $filter . ".ddns.eckner.net. " . lifetime($ip) . " IN " . $rrTypes[$i] . " " . $ip . "\n"; + $logContent .= + date('Y-m-d H:i:s') . ' ' . $domain . '.' . $filter . ' ' . $ip . "\n"; + } } + $updateCommand .= + "send\n"; // actually do something $pin = popen('nsupdate -l', 'w'); |