Commit b3cfb853 authored by Franco Fichtner's avatar Franco Fichtner

rfc2136: still prep for #996

This needs much maintenance, it's amazing really.
parent 9e65b123
......@@ -123,6 +123,13 @@ function dyndns_list()
);
}
function dyndns_cache_file($conf, $ipver = 4)
{
$ipver = $ipver == 6 ? '_v6' : '';
return "/var/cache/dyndns_{$conf['interface']}_{$conf['host']}_{$dyndns['id']}{$ipver}.cache";
}
function dyndns_configure_client($conf)
{
if (!isset($conf['enable'])) {
......
......@@ -137,11 +137,11 @@
$dnsZoneID ='', $dnsTTL='', $dnsResultMatch = '', $dnsRequestIf = '',
$dnsID = '', $dnsVerboseLog = false, $curlIpresolveV4 = false, $curlSslVerifypeer = true) {
global $config;
$this->_cacheFile = "/conf/dyndns_{$dnsIf}{$dnsService}" . escapeshellarg($dnsHost) . "{$dnsID}.cache";
$this->_cacheFile_v6 = "/conf/dyndns_{$dnsIf}{$dnsService}" . escapeshellarg($dnsHost) . "{$dnsID}_v6.cache";
$this->_debugFile = "/var/etc/dyndns_{$dnsIf}{$dnsService}" . escapeshellarg($dnsHost) . "{$dnsID}.debug";
/* XXX because the call stack is upside down we need to reassemble config parts here... */
$conf = array('host' => $dnsHost, 'id' => $dnsID, 'interface' => $dnsIf);
$this->_cacheFile = dyndns_cache_file($conf, 4);
$this->_cacheFile_v6 = dyndns_cache_file($conf, 6);
$this->_debugFile = dyndns_cache_file($conf, 4) . '.debug';
$this->_curlIpresolveV4 = $curlIpresolveV4;
$this->_curlSslVerifypeer = $curlSslVerifypeer;
......
......@@ -84,6 +84,13 @@ function rfc2136_cron()
return $jobs;
}
function rfc2136_cache_file($dnspdate, $ipver = 4)
{
$ipver = $ipver == 6 ? '_v6' : '';
return "/var/cache/rfc2136_{$dnsupdate['interface']}_{$dnsupdate['host']}_{$dnsupdate['server']}{$ipver}.cache";
}
function rfc2136_configure_do($verbose = false, $int = '', $updatehost = '', $forced = false)
{
global $config;
......@@ -108,20 +115,8 @@ function rfc2136_configure_do($verbose = false, $int = '', $updatehost = '', $fo
continue;
}
$if = get_real_interface($dnsupdate['interface']);
if (isset($dnsupdate['usepublicip'])) {
$wanip = get_dyndns_ip($dnsupdate['interface'], 4);
$wanipv6 = get_dyndns_ip($dnsupdate['interface'], 6);
} else {
$wanip = get_interface_ip($dnsupdate['interface']);
$wanipv6 = get_interface_ipv6($dnsupdate['interface']);
}
$cacheFile = "/conf/dyndns_{$dnsupdate['interface']}_rfc2136_" . escapeshellarg($dnsupdate['host']) . "_{$dnsupdate['server']}.cache";
$currentTime = time();
if ($wanip || $wanipv6) {
$keyname = $dnsupdate['keyname'];
/* trailing dot */
if (substr($keyname, -1) != ".") {
......@@ -169,23 +164,26 @@ EOD;
$upinst .= "server {$dnsupdate['server']}\n";
}
if (file_exists($cacheFile)) {
list($cachedipv4, $cacheTimev4) = explode("|", file_get_contents($cacheFile));
}
if (file_exists("{$cacheFile}.ipv6")) {
list($cachedipv6, $cacheTimev6) = explode("|", file_get_contents("{$cacheFile}.ipv6"));
}
// 25 Days
$maxCacheAgeSecs = 25 * 24 * 60 * 60;
$need_update = false;
/* Update IPv4 if we have it. */
if (is_ipaddrv4($wanip) && (empty($dnsupdate['recordtype']) || $dnsupdate['recordtype'] == 'A')) {
if (empty($dnsupdate['recordtype']) || $dnsupdate['recordtype'] == 'A') {
$cacheFile = rfc2136_cache_file($dnsupdate, 4);
if (file_exists($cacheFile)) {
list($cachedipv4, $cacheTimev4) = explode('|', file_get_contents($cacheFile));
} else {
list($cachedipv4, $cacheTimev4) = array('', '');
}
if (isset($dnsupdate['usepublicip'])) {
$wanip = get_dyndns_ip($dnsupdate['interface'], 4);
} else {
$wanip = get_interface_ip($dnsupdate['interface']);
}
if (is_ipaddrv4($wanip)) {
if (($wanip != $cachedipv4) || (($currentTime - $cacheTimev4) > $maxCacheAgeSecs) || $forced) {
$upinst .= "update delete {$dnsupdate['host']}. A\n";
$upinst .= "update add {$dnsupdate['host']}. {$dnsupdate['ttl']} A {$wanip}\n";
$notify_text .= sprintf(gettext('Dynamic DNS updated IP Address (A) for %s on %s (%s) to %s'), $dnsupdate['host'], convert_real_interface_to_friendly_descr($if), $if, $wanip) . "\n";
$notify_text .= sprintf(gettext('Dynamic DNS updated IP Address (A) for %s on %s to %s'), $dnsupdate['host'], strtoupper($dnsupdate['interface']), $wanip) . "\n";
@file_put_contents($cacheFile, "{$wanip}|{$currentTime}");
log_error("Dynamic DNS: updating cache file {$cacheFile}: {$wanip}");
$need_update = true;
......@@ -195,21 +193,34 @@ EOD;
} else {
@unlink($cacheFile);
}
}
/* Update IPv6 if we have it. */
if (is_ipaddrv6($wanipv6) && (empty($dnsupdate['recordtype']) || $dnsupdate['recordtype'] == 'AAAA')) {
if (empty($dnsupdate['recordtype']) || $dnsupdate['recordtype'] == 'AAAA') {
$cacheFile6 = rfc2136_cache_file($dnsupdate, 6);
if (file_exists($cacheFile6)) {
list($cachedipv6, $cacheTimev6) = explode('|', file_get_contents($cacheFile6));
} else {
list($cachedipv6, $cacheTimev6) = array('', '');
}
if (isset($dnsupdate['usepublicip'])) {
$wanipv6 = get_dyndns_ip($dnsupdate['interface'], 6);
} else {
$wanipv6 = get_interface_ipv6($dnsupdate['interface']);
}
if (is_ipaddrv6($wanipv6)) {
if (($wanipv6 != $cachedipv6) || (($currentTime - $cacheTimev6) > $maxCacheAgeSecs) || $forced) {
$upinst .= "update delete {$dnsupdate['host']}. AAAA\n";
$upinst .= "update add {$dnsupdate['host']}. {$dnsupdate['ttl']} AAAA {$wanipv6}\n";
$notify_text .= sprintf(gettext('Dynamic DNS updated IPv6 Address (AAAA) for %s on %s (%s) to %s'), $dnsupdate['host'], convert_real_interface_to_friendly_descr($if), $if, $wanipv6) . "\n";
@file_put_contents("{$cacheFile}.ipv6", "{$wanipv6}|{$currentTime}");
log_error("Dynamic DNS: updating cache file {$cacheFile}.ipv6: {$wanipv6}");
$notify_text .= sprintf(gettext('Dynamic DNS updated IPv6 Address (AAAA) for %s on %s to %s'), $dnsupdate['host'], strtoupper($dnsupdate['interface']), $wanipv6) . "\n";
@file_put_contents($cacheFile6, "{$wanipv6}|{$currentTime}");
log_error("Dynamic DNS: updating cache file {$cacheFile6}: {$wanipv6}");
$need_update = true;
} else {
log_error("Dynamic DNS: Not updating {$dnsupdate['host']} AAAA record because the IPv6 address has not changed.");
}
} else {
@unlink("{$cacheFile}.ipv6");
@unlink($cacheFile6);
}
}
$upinst .= "\n"; /* mind that trailing newline! */
......@@ -226,7 +237,6 @@ EOD;
unset($cmd);
}
}
}
if (!empty($notify_text)) {
notify_all_remote($notify_text);
......
......@@ -42,7 +42,8 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if (isset($_POST['act']) && $_POST['act'] == "del" && isset($_POST['id'])) {
if (!empty($a_dyndns[$_POST['id']])) {
$conf = $a_dyndns[$_POST['id']];
@unlink("/conf/dyndns_{$conf['interface']}{$conf['type']}" . escapeshellarg($conf['host']) . "{$conf['id']}.cache");
@unlink(dyndns_cache_file($conf, 4));
@unlink(dyndns_cache_file($conf, 6));
unset($a_dyndns[$_POST['id']]);
write_config();
system_cron_configure();
......@@ -146,14 +147,14 @@ $main_buttons = array(
<td><?=$dyndns['host'];?></td>
<td>
<?php
$filename = "/conf/dyndns_{$dyndns['interface']}{$dyndns['type']}" . escapeshellarg($dyndns['host']) . "{$dyndns['id']}.cache";
$filename = dyndns_cache_file($dyndns, 4);
$fdata = '';
if (file_exists($filename) && !empty($dyndns['enable'])) {
$ipaddr = get_dyndns_ip($dyndns['interface'], 4);
$fdata = @file_get_contents($filename);
}
$filename_v6 = "/conf/dyndns_{$dyndns['interface']}{$dyndns['type']}" . escapeshellarg($dyndns['host']) . "{$dyndns['id']}_v6.cache";
$filename_v6 = dyndns_cache_file($dyndns, 6);
$fdata6 = '';
if (file_exists($filename_v6) && !empty($dyndns['enable'])) {
$ipv6addr = get_dyndns_ip($dyndns['interface'], 6);
......
......@@ -145,7 +145,7 @@ $main_buttons = array(
<td><?=$rfc2136['host'];?></td>
<td>
<?php
$filename = "/conf/dyndns_{$rfc2136['interface']}_rfc2136_" . escapeshellarg($rfc2136['host']) . "_{$rfc2136['server']}.cache";
$filename = rfc2136_cache_file($rfc2136, 4);
if (file_exists($filename) && !empty($rfc2136['enable']) && (empty($dnsupdate['recordtype']) || $dnsupdate['recordtype'] == 'A')) {
echo "IPv4: ";
if (isset($rfc2136['usepublicip'])) {
......@@ -166,14 +166,15 @@ $main_buttons = array(
echo "IPv4: N/A";
}
echo "<br />";
if (file_exists("{$filename}.ipv6") && !empty($rfc2136['enable']) && (empty($dnsupdate['recordtype']) || $dnsupdate['recordtype'] == 'AAAA')) {
$filename6 = rfc2136_cache_file($rfc2136, 6);
if (file_exists($filename6) && !empty($rfc2136['enable']) && (empty($dnsupdate['recordtype']) || $dnsupdate['recordtype'] == 'AAAA')) {
echo "IPv6: ";
if (isset($rfc2136['usepublicip'])) {
$ipaddr = get_dyndns_ip($rfc2136['interface'], 6);
} else {
$ipaddr = get_interface_ipv6($rfc2136['interface']);
}
$cached_ip_s = explode("|", file_get_contents("{$filename}.ipv6"));
$cached_ip_s = explode("|", file_get_contents($filename6));
$cached_ip = $cached_ip_s[0];
if ($ipaddr <> $cached_ip) {
echo "<font color='red'>";
......
......@@ -50,14 +50,14 @@ if (!empty($_REQUEST['getdyndnsstatus'])) {
echo '|';
}
$filename = "/conf/dyndns_{$dyndns['interface']}{$dyndns['type']}" . escapeshellarg($dyndns['host']) . "{$dyndns['id']}.cache";
$filename = dyndns_cache_file($dyndns, 4);
$fdata = '';
if (!empty($dyndns['enable']) && file_exists($filename)) {
$ipaddr = get_dyndns_ip($dyndns['interface'], 4);
$fdata = @file_get_contents($filename);
}
$filename_v6 = "/conf/dyndns_{$dyndns['interface']}{$dyndns['type']}" . escapeshellarg($dyndns['host']) . "{$dyndns['id']}_v6.cache";
$filename_v6 = dyndns_cache_file($dyndns, 6);
$fdata6 = '';
if (!empty($dyndns['enable']) && file_exists($filename_v6)) {
$ipv6addr = get_dyndns_ip($dyndns['interface'], 6);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment