summaryrefslogtreecommitdiff
path: root/ddns.php
diff options
context:
space:
mode:
authorErich Eckner <git@eckner.net>2020-03-04 11:11:23 +0100
committerErich Eckner <git@eckner.net>2020-03-04 11:11:23 +0100
commitc8599d774c8afe1c791600c7f95ca05f949f4897 (patch)
treef957dafda73e41278c0de3b73e4f2c8912a74d2f /ddns.php
parentaa208e9dd52d91e7144867982dce31d6f364d856 (diff)
downloadddns-c8599d774c8afe1c791600c7f95ca05f949f4897.tar.xz
ddns.php: set up reverse entries for ipv6, too
Diffstat (limited to 'ddns.php')
-rw-r--r--ddns.php100
1 files changed, 65 insertions, 35 deletions
diff --git a/ddns.php b/ddns.php
index 3b8793c..b9ce90e 100644
--- a/ddns.php
+++ b/ddns.php
@@ -5,31 +5,65 @@
// $x.ddns.eckner.net (all)
function lifetime($addr) {
- $addrTeile = explode('.', $addr);
- if (($addrTeile[0] == '10') ||
- (($addrTeile[0] == '192') && ($addrTeile[1] == '168')) ||
- (($addrTeile[0] == '172') && ($addrTeile[1] >= 16) && ($addrTeile[1] < 32)))
- return '7200';
+ if (filter_var($addr, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
+ $addrTeile = explode('.', $addr);
+ if (($addrTeile[0] == '10') ||
+ (($addrTeile[0] == '192') && ($addrTeile[1] == '168')) ||
+ (($addrTeile[0] == '172') && ($addrTeile[1] >= 16) && ($addrTeile[1] < 32)))
+ return '7200';
+ };
return '120';
};
+ function convert_ipv6_address_part_to_reverse_record_part(&$part) {
+ while (strlen($part)<4)
+ $part = '0' . $part;
+ $part = str_split($part);
+ $part = array_reverse($part);
+ $part = implode('.', $part);
+ };
+
+ function reverse_ipv6_address($addr) {
+ $addr_Teile = array();
+ while (count($addr_Teile) < 8) {
+ $addr_Teile = explode(':', $addr);
+ $addr = str_replace('::',':0::',$addr);
+ }
+ array_walk($addr_Teile, 'convert_ipv6_address_part_to_reverse_record_part');
+ $addr_Teile = array_reverse($addr_Teile);
+ return implode('.', $addr_Teile);
+ };
+
function reverse_entry($addr) {
- $addrTeile = explode('.', $addr);
- $lp = array_pop($addrTeile);
- $subNet = implode('.', $addrTeile);
- array_push($addrTeile, $lp);
- array_reverse($addrTeile);
- if (($subNet == '10.0.2') ||
- ($subNet == '192.168.0') ||
- ($subNet == '192.168.1'))
- return implode('.',$addrTeile).'.in-addr.arpa.';
+ if (filter_var($addr, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
+ $addrTeile = explode('.', $addr);
+ $lp = array_pop($addrTeile);
+ $subNet = implode('.', $addrTeile);
+ array_push($addrTeile, $lp);
+ array_reverse($addrTeile);
+ if (($subNet == '10.0.2') ||
+ ($subNet == '192.168.0') ||
+ ($subNet == '192.168.1'))
+ return implode('.',$addrTeile).'.in-addr.arpa.';
+ } elseif (filter_var($addr, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
+ $reversed_address = reverse_ipv6_address($addr);
+ if (stripos(implode('.',array_reverse(explode('.',$reversed_address))),'2.a.0.1.0.0.d.0.f.6.c.e.')===0)
+ return $reversed_address.'.ip6.arpa.';
+ };
return NULL;
};
function reverse_zone($addr) {
- $addrTeile = explode('.', $addr);
- array_pop($addrTeile);
- return implode('.',$addrTeile).'.in-addr.arpa.';
+ if (filter_var($addr, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
+ $addrTeile = explode('.', $addr);
+ array_pop($addrTeile);
+ return implode('.',$addrTeile).'.in-addr.arpa.';
+ } elseif (filter_var($addr, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
+ $reversed_address = reverse_ipv6_address($addr);
+ if (stripos(implode('.',array_reverse(explode('.',$reversed_address))),'2.a.0.1.0.0.d.0.f.6.c.e.')===0)
+ return 'e.c.6.f.0.d.0.0.1.0.a.2.ip6.arpa.';
+ };
+ return NULL;
};
// invalid syntax?
@@ -140,22 +174,20 @@
$updateCommand .=
"send\n";
foreach ($ips as $i => $ip) {
- if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
- $reverse_entry = reverse_entry($ip);
- if (!$reverse_entry)
- continue;
- $updateCommand .=
- "zone ".reverse_zone($ip)."\n";
- // delete old record if existing
- $updateCommand .=
- "prereq yxrrset " . $reverse_entry . " IN PTR\n" .
- "update delete " . $reverse_entry . " IN PTR\n" .
- "send\n";
- // set new records no matter what
- $updateCommand .=
- "update add " . $reverse_entry . " " . lifetime($ip) . " IN PTR " . $domain . ".ddns.eckner.net.\n" .
- "send\n";
- }
+ $reverse_entry = reverse_entry($ip);
+ if (!$reverse_entry)
+ continue;
+ $updateCommand .=
+ "zone ".reverse_zone($ip)."\n";
+ // delete old record if existing
+ $updateCommand .=
+ "prereq yxrrset " . $reverse_entry . " IN PTR\n" .
+ "update delete " . $reverse_entry . " IN PTR\n" .
+ "send\n";
+ // set new records no matter what
+ $updateCommand .=
+ "update add " . $reverse_entry . " " . lifetime($ip) . " IN PTR " . $domain . ".ddns.eckner.net.\n" .
+ "send\n";
}
// actually do something
@@ -167,8 +199,6 @@
pclose($pin);
-echo $updateCommand;
-
file_put_contents('/srv/http/vhosts/eckner.net/ddns/log', $logContent, FILE_APPEND | LOCK_EX);
print "updated ips for domain \"" . $domain . ".ddns.eckner.net\": " . implode($ips,", ") . "\n";