forked from handycz/ddnsadmin
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnsupdate.php
More file actions
105 lines (90 loc) · 3.34 KB
/
Copy pathnsupdate.php
File metadata and controls
105 lines (90 loc) · 3.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?php
// *** PHP script to automate nsupdate calls for dynamic dns updates.
// *** This script and a properly configured bind nameserver allows hosting
// *** of custom dyndns services, e.g. updating own domain from Fritzbox.
// *** usage: http://192.168.0.25/nsupdate.php?ns=ns0.mynameserver.de&domain=my.domain.de&newip=<ipaddr>
// Taken (and modified) from https://gist.github.com/mark-sch/9109343
// open log session
openlog("webnsupdater", LOG_PID | LOG_PERROR, LOG_LOCAL0);
function output_error($error_string, $status_code = 500) {
http_response_code($status_code);
die($error_string);
}
function base64_url_encode($input) {
return strtr(base64_encode($input), '+/=', '._-');
}
function base64_url_decode($input) {
return base64_decode(strtr($input, '._-', '+/='));
}
// Load Net/DNS2 library
if (file_exists('composer.json')) {
require_once 'vendor/autoload.php';
} else {
syslog(LOG_WARN, 'Net_DNS2 PHP library is missing.');
output_error("Net_DNS2 PHP library is missing.\n");
}
// configuration: domains which can be updated, nameservers must be a subdomain of any of those but cross-updates are allowed
$legitimate_domains = array('example.net', 'example.org');
// helper function: 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;
}
// helper function: get a parameter from POST first and then GET
// $param = name of parameter
function getparam($param)
{
$paramvalue = "";
if ( isset($_POST[$param]) )
{
$paramvalue = $_POST[$param];
}
else if ( isset($_GET[$param]) )
{
$paramvalue = $_GET[$param];
}
else
{
syslog(LOG_WARN, "Parameter $param not provided in POST nor GET");
}
return $paramvalue;
}
// Process request
foreach (array("zone", "key-name", "key-type", "key", "server") as $val) {
$args[$val] = getparam($val);
if (empty($args[$val]))
output_error('Invalid request, "'.$val.'" field is mandatory.', 400);
}
$args["key"] = base64_url_decode($args["key"]);
$record = $args['record'];
foreach (array("name", "ttl", "type", "data") as $val) {
syslog(LOG_WARN, "getparam($val) = ".getparam($val));
$record[$val] = getparam($val);
if (empty($record[$val]))
output_error('Invalid request, "'.$val.'" field is mandatory.', 400);
}
$type_name = $record['type'];
if (empty(Net_DNS2_Lookups::$rr_types_by_name[$type_name]))
output_error('Resource record type "'.$type_name.'" is not supported.');
$type_id = Net_DNS2_Lookups::$rr_types_by_name[$type_name];
if (empty(Net_DNS2_Lookups::$rr_types_id_to_class[$type_id]))
output_error('Resource record type "'.$type_name.'" is not supported.');
$type_class = Net_DNS2_Lookups::$rr_types_id_to_class[$type_id];
$u = new Net_DNS2_Updater($args['zone'], array('nameservers' => array($args['server'])));
try {
$u->signTSIG($args['key-name'], $args['key'], $args['key-type']);
$u->add($type_class::fromString($record['name'].' '.$record['ttl'].' IN '.$record['type'].' '.$record['data']));
$u->update();
} catch(Net_DNS2_Exception $e) {
output_error("Update failed: ".$e->getMessage());
syslog(LOG_WARN, "Update failed: ".$e->getMessage());
}
// close log session
closelog();
?>