Files
ddns/php/index.php

201 lines
5.4 KiB
PHP
Raw Permalink Normal View History

<?php
//error_reporting(-1);
ini_set('display_errors', 'On');
// main domain for dynamic DNS
$dyndns = "ddns.kawomi.com";
// short sanity check for given IP
function checkip($ip) {
$iptupel = explode(".", $ip);
foreach ($iptupel as $value)
{
if ($value < 0 || $value > 255)
return false;
}
return true;
}
function getFormValue($varname, $defaultValue){
$retVal=$defaultValue;
if ( isset($_POST[$varname]) )
{
$retVal = $_POST[$varname];
}
else if ( isset($_GET[$varname]) )
{
$retVal = $_GET[$varname];
}
return $retVal;
}
$remoteip = $_SERVER['REMOTE_ADDR'];
// retrieve parameters
$ip = getFormValue('address',$_SERVER['REMOTE_ADDR']);
if ($ip=="") {$ip=$remoteip;}
$autoupdate=getFormValue('autoupdate',0);
$subdomain = getFormValue('domain',null);
// open log session
openlog("DDNS-Provider", LOG_PID | LOG_PERROR, LOG_LOCAL0);
// check for given domain
if (is_null($subdomain)) {
// No Domain; display form
?>
<!DOCTYPE html>
<html>
<head>
<title>Dynamic DNS Updater</title>
<style type="text/css">
.box {
border:2px solid;
border-radius:5px;
}
td {
padding:0px;
}
</style>
<script>
function buildUrl(){
var domain=document.getElementsByName('domain')[0].value;
var url=window.location.href+"?domain="+domain;
var autoupdate=document.getElementsByName("autoupdate")[0].value
if (autoupdate > 0 ) {
url = url + "&autoupdate=" + autoupdate;
document.getElementsByName('address')[0].value="";
}
var ip=document.getElementsByName('address')[0].value;
if (""!=ip) {
url = url + "&address="+ip;
}
var password=document.getElementsByName('password')[0].value;
if (""!=password) {
url = url + "&password="+password;
}
var email=document.getElementsByName('email')[0].value;
if (""!=email) {
url = url + "&email="+email;
}
document.getElementById('updateUrl').innerText=url;
}
</script>
</head>
<body onload='buildUrl();'>
<h1>Enter Dynamic DNS details</h1>
<hr/>
<form type="GET">
<div class='box' style='width:600px'><div>Subdomain-name (required):</div><div><input onkeyup='buildUrl()' onchange='buildUrl()' name='domain' value='' />.ddns.kawomi.com</div></div>
<div class='box' style='width:600px'><div>Password (optional, sent as clear text, required to update if given)</div><div><input onkeyup='buildUrl()' onchange='buildUrl()' type='text' name='password'></div></div>
<div class='box' style='width:600px'><div>E-Mail (optional, password recovery mail is sent to this address)</div><div><input onkeyup='buildUrl()' onchange='buildUrl()' type='email' name='email'></div></div>
<table border='0'>
<tr>
<td><div class='box' style='width:250px'><div>Manual Overwrite IP Address(optional)</div><div><input onkeyup='buildUrl()' onchange='buildUrl()' name='address' value='' /></div></div></td>
<td><div style='border:0px;width:90px;text-align:center;font-weight:bold;'> &lt;-- OR --&gt; </div></td>
<td><div class='box' style='width:250px'><div>AutoUpdate (only AutoAddress)</div><div><span><input name="autoupdate" onkeyup='buildUrl()' onchange='buildUrl();document.getElementById("autoUpdMins").value=(this.value>0?this.value:"OFF");' type="range" min="0" max="60" step="1" value="0" /><input readonly='readonly' size='1' id='autoUpdMins' value='OFF' type='Text' /> Minutes. </span></div></div></td>
</tr>
</table>
<hr>
Update-URL:<div id='updateUrl'></div>
<hr>
<input type='submit' />
</form>
</body>
</html>
<?php
exit(0);
}
// check for needed variables
if (!( isset($subdomain) && isset($ip))) {
$msg= "DDNS change for ip $ip with subdomain $subdomain failed because of missing values";
syslog(LOG_INFO, $msg);
echo $msg;
exit(0);
}
// short sanity check for given IP
if (!( preg_match("/^(\d{1,3}\.){3}\d{1,3}$/", $ip) && checkip($ip) && $ip != "0.0.0.0" && $ip != "255.255.255.255" )) {
$msg="IP $ip subdomain $subdomain was invalid remoteip: $remoteip";
syslog(LOG_INFO, $msg);
echo $msg;
exit(0);
}
// short sanity check for given domain
if (!( preg_match("/^[\w\d\-\_\.]+$/", $subdomain))) {
$msg="Domain $subdomain for $user from $ip with $subdomain was invalid ";
syslog(LOG_INFO, $msg);
echo $msg;
exit(0);
}
if ($subdomain == "ns") {
$msg="subdomain ns is reserved and can't be changed.";
syslog(LOG_INFO, $msg);
echo $msg;
exit(0);
}
// shell escape all values
$subdomain = escapeshellcmd($subdomain);
$ip = escapeshellcmd($ip);
// prepare command
$data = "<<EOF
server ns.ddns.kawomi.com
zone $dyndns
update delete $subdomain.$dyndns. A
update add $subdomain.$dyndns. 60 A $ip
send
EOF
";
// run DNS update
exec("/usr/bin/nsupdate $data", $cmdout, $ret);
// check whether DNS update was successful
if ($ret != 0)
{
$msg= "Changing DNS for $subdomain.$dyndns to $ip failed with code $ret";
syslog(LOG_INFO, $msg);
echo $msg;
exit(0);
}
$msg= "Changing DNS for $subdomain.$dyndns to $ip successful.";
syslog(LOG_INFO, $msg);
echo $msg;
if ($autoupdate > 0 and $autoupdate <= 60) {
?>
<hr/>
autoupdate requested, refreshing address every <?=$autoupdate?> minutes.<br/>
Countdown: <span id='countdown'></span>
<script>
var i=60*<?=$autoupdate?>; window.setInterval(function(){i--; if (i<1) { i=60*<?=$autoupdate?>;window.clearInterval();window.location.reload(); } else {document.getElementById('countdown').innerHTML=i;}},1000);
</script>
<?php
}
// close log session
closelog();
exit(0);
?>
<!-- vim: ts=2 sw=2 nowrap et -->