Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Excellent script! #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
109 changes: 63 additions & 46 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,59 @@
#
# (c) Karl-Martin Skontorp <kms@skontorp.net> ~ http://22pf.org/
# Licensed under the GNU GPL 2.0 or later.
#
# 2012-04-04 Vlad Vissoultchev <wqweto@gmail.com>
# - Supports local and remote DNS servers
# - Implemented defaults on every parameter
# - Output mimics http://dyn.com/support/developers/api/return-codes/
#

header("Content-Type: text/plain");

// Config
$nsupdate = '/usr/bin/nsupdate -v';
$cacheDir = 'cache';
$dnsServer = '127.0.0.1';
$cacheDir = '/tmp/nsupdate-cache'; // chmod 777 this
$default = array(
'ip' => $_SERVER['REMOTE_ADDR'],
'ttl' => 60,
'key' => 'ddns-key abcdef123456789=',
);

// GET/POST parameters
$config['hostname'] = $_REQUEST['hostname'];
$config['key'] = $_REQUEST['key'];
$config['ttl'] = $_REQUEST['ttl'];
$config['ip'] = $_REQUEST['ip'];

// Default IP is remote host address
if (empty($config['ip'])) {
$config['ip'] = $_SERVER['REMOTE_ADDR'];
}
$config = array(
'hostname' => $_REQUEST['hostname'],
'key' => $_REQUEST['key'],
'ttl' => $_REQUEST['ttl'],
'ip' => $_REQUEST['ip'],
);

// Handling of missing parameters
$error = false;
$error |= empty($config['hostname']);
$error |= empty($config['key']);
$error |= empty($config['ttl']);
$error |= empty($config['ip']);

if ($_REQUEST['debug'] == 'yes' || $error) {
echo "nsupdate web-interface\n";
echo "----------------------\n\n";
echo "Error: Missing parameters\n\n";
$error = 0;
foreach ($config as $k => $v) {
$error |= empty($v) && empty($default[$k]);
}

if (empty($config['hostname']) && empty($default['hostname'])) {
echo "nohost\n";
exit;
}
elseif ($error) {
echo "911\n";
echo "Error: Missing parameters with no defaults\n\n";
print_r($config);
exit;
}
elseif (isset($_REQUEST['debug'])) {
print_r($config);
exit;
}

// Default params
foreach ($config as $k => $v) {
if (empty($v))
$config[$k] = $default[$k];
}

// Handle cache of old/current IP address
$cacheFile = $cacheDir . '/' . basename($config['hostname']);
Expand All @@ -46,15 +67,15 @@

// Exit now unless IP address is new
if ($config['old_ip'] == $config['ip']) {
echo 'nochg ' . $config['ip'] . "\n"; // dyndns compatible output
exit;
}

// nsupdate commands
$nsupdateCommands = 'key ' . $config['hostname']
. ' ' . $config['key'] . "\n";
$nsupdateCommands = 'server ' . $dnsServer . "\n";
$nsupdateCommands .= 'key ' . $config['key'] . "\n";
$nsupdateCommands .= 'update delete ' . $config['hostname'] . "\n";
$nsupdateCommands .= 'update add ' . $config['hostname'] . ' '
. $config['ttl'] . ' A ' . $config['ip'] . "\n";
$nsupdateCommands .= 'update add ' . $config['hostname'] . ' ' . $config['ttl'] . ' A ' . $config['ip'] . "\n";
$nsupdateCommands .= "send\n\n";

// Prepare to execute nsupdate binary
Expand All @@ -69,23 +90,21 @@
// Execute nsupdate and print status info
$process = proc_open($nsupdate, $descriptors, $pipes);

echo "nsupdate web-interface\n";
echo "----------------------\n\n";
echo date("D M j G:i:s T Y") . "\n\n";
echo "Previous IP: " . $config['old_ip'] . "\n";
echo "New IP: " . $config['ip'] . "\n";

if (is_resource($process)) {
fwrite($pipes[0], $nsupdateCommands);
fclose($pipes[0]);

$prefix = 'STDOUT: ';
while ($s = fgets($pipes[1], 1024)) {
$errors .= 'STDOUT: ' . $s;
$errors .= $prefix . $s;
$prefix = '';
}
fclose($pipes[1]);

$prefix = 'STDERR: ';
while ($s = fgets($pipes[2], 1024)) {
$errors .= 'STDERR: ' . $s;
$errors .= $prefix . $s;
$prefix = '';
}
fclose($pipes[2]);

Expand All @@ -96,21 +115,19 @@

// Output errors if unsuccessfull, else update cache
if ($returnValue != 0) {
echo "\nError: DNS not updated!\n\n";
echo $errors;
echo "dnserr\n" . $errors;
exit;
}
echo 'good ' . $config['ip'] . "\n";

// Update cache
if (is_writable($cacheFile)
|| (!file_exists($cacheFile) && is_writeable($cacheDir))) {
$f = fopen($cacheFile, 'w');
fwrite($f, $config['ip']);
fclose($f);
} else {
echo "\nDNS updated!\n";

// Update cache
if (is_writable($cacheFile)
|| (!file_exists($cacheFile) && is_writeable($cacheDir))) {
$f = fopen($cacheFile, 'w');
fwrite($f, $config['ip']);
fclose($f);
echo "Cache updated!\n";
} else {
echo "Error: Cache not updated!\n";
}
echo "Error: Cache not updated!\n";
}

?>