forked from AndLindemann/php-dyndns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update.php
185 lines (160 loc) · 6.05 KB
/
update.php
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<?php
//error_reporting(E_ALL); /* for development */
@ini_set("display_errors", 0); /* don't show errors onscreen */
/* Config */
include_once("config.php");
include_once("functions.php");
// Custom error handler that writes to a file
set_error_handler("dyndns_error_handler");
/* Opts */
if (php_sapi_name() == "cli") {
// Command line
$shortopts = "";
$longopts = array("pass:", "domain:", "ipaddr:", "ip6addr:");
$options = getopt($shortopts, $longopts);
$pass = isset($options['pass']) ? $options['pass'] : null;
$domain = isset($options['domain']) ? $options['domain'] : null;
$ipaddr = isset($options['ipaddr']) ? $options['ipaddr'] : null;
$ip6addr = isset($options['ip6addr']) ? $options['ip6addr'] : null;
} else {
// HTTP
$pass = isset($_GET['pass']) ? $_GET['pass'] : null;
$domain = isset($_GET['domain']) ? $_GET['domain'] : null;
$ipaddr = isset($_GET['ipaddr']) ? $_GET['ipaddr'] : null;
$ip6addr = isset($_GET['ip6addr']) ? $_GET['ip6addr'] : null;
}
/* Validation */
if (!validCred($pass)) {
trigger_error("bad credentials", E_USER_WARNING);
respond("failed", "bad credentials");
}
if (!isset($ipaddr) && !isset($ip6addr)) {
trigger_error("no ip(v6) address given", E_USER_WARNING);
respond("failed", "no ip(v6) address given");
}
if (isset($ipaddr) && !validIP($ipaddr)) {
trigger_error("not a valid ip address", E_USER_WARNING);
respond("failed", "not a valid ip address");
}
if (isset($ip6addr) && !validIPv6($ip6addr)) {
trigger_error("not a valid ipv6 address", E_USER_WARNING);
respond("failed", "not a valid ipv6 address");
}
if (!validDomain($domain)) {
trigger_error("not a valid domain", E_USER_WARNING);
respond("failed", "not a valid domain");
}
if (!configuredDomain($domain)) {
trigger_error("unsupported domain", E_USER_WARNING);
respond("failed", "unsupported domain");
}
// Multi-domain
if (defined('DOMAINS')) {
$domains = unserialize(DOMAINS);
$subdomains = $domains[$domain];
} else {
$subdomains[] = SUBDOMAIN;
}
/*
* Request: Get all records of the domain
*/
/* Build request */
$xml_get = implode("", file(XML_GET_ZONE));
$doc_get = new DOMDocument();
$doc_get->loadXML($xml_get);
$doc_get->formatOutput = true;
$doc_get->getElementsByTagName('user')->item(0)->nodeValue = USER;
$doc_get->getElementsByTagName('password')->item(0)->nodeValue = PASSWORD;
$doc_get->getElementsByTagName('context')->item(0)->nodeValue = CONTEXT;
$doc_get->getElementsByTagName('name')->item(0)->nodeValue = $domain;
$doc_get->getElementsByTagName('system_ns')->item(0)->nodeValue = SYSTEM_NS;
// ATTENTION: This dom document contains credentials
//trigger_error($doc_get->saveXML(), E_USER_NOTICE);
/* Send */
trigger_error("get current zone records", E_USER_NOTICE);
$result = requestCurl($doc_get->saveXML());
/* Receive */
$doc_result = new DOMDocument();
$doc_result->loadXML($result);
$doc_result->formatOutput = true;
//trigger_error($doc_result->saveXML(), E_USER_NOTICE);
/* Abort if we cannot get the current zone records */
$xpath = new DOMXPath($doc_result);
$query = "status/status";
$entries = $xpath->query($query);
if ($entries->length > 0) {
$status = $entries->item(0)->nodeValue;
if ($status == "error") {
trigger_error("cannot get current zone records", E_USER_ERROR);
respond("failed", "cannot get current zone records");
}
}
/*
* Request: Set modified zone records
*/
/* Settings */
$file_put = implode("", file(XML_PUT_ZONE));
$doc_put = new DOMDocument();
$doc_put->loadXML($file_put);
$doc_put->formatOutput = true;
$doc_put->getElementsByTagName('user')->item(0)->nodeValue = USER;
$doc_put->getElementsByTagName('password')->item(0)->nodeValue = PASSWORD;
$doc_put->getElementsByTagName('context')->item(0)->nodeValue = CONTEXT;
/* Zone */
$frag_data = $doc_result->getElementsByTagName('zone')->item(0);
$frag_data->removeChild($frag_data->getElementsByTagName('created')->item(0));
$frag_data->removeChild($frag_data->getElementsByTagName('changed')->item(0));
$frag_data->removeChild($frag_data->getElementsByTagName('domainsafe')->item(0));
$frag_data->removeChild($frag_data->getElementsByTagName('owner')->item(0));
$frag_data->removeChild($frag_data->getElementsByTagName('updated_by')->item(0));
$frag = $doc_put->importNode($frag_data, TRUE);
$doc_put->getElementsByTagName('task')->item(0)->appendChild($frag);
/* New dynamic DNS IP address for each subdomain */
foreach($subdomains as $subdomain) {
if (isset($ipaddr)) {
$xpath = new DOMXPath($doc_put);
$query = "//task/zone/rr[name='" . $subdomain . "' and type='A']/value";
$entries = $xpath->query($query);
if ($entries->length != 1) {
trigger_error("domain has no dyndns A-record for " . $subdomain, E_USER_ERROR);
respond("failed", "domain has no dyndns A-record for " . $subdomain);
}
$entries->item(0)->nodeValue = $ipaddr;
}
/* New dynamic DNS IPv6 */
if (isset($ip6addr)) {
$xpath = new DOMXPath($doc_put);
$query = "//task/zone/rr[name='" . $subdomain . "' and type='AAAA']/value";
$entries = $xpath->query($query);
if ($entries->length != 1) {
trigger_error("domain has no dyndns AAAA-record for " . $subdomain, E_USER_ERROR);
respond("failed", "domain has no dyndns AAAA-record for " . $subdomain);
}
$entries->item(0)->nodeValue = $ip6addr;
}
}
/* Send */
$xml_put = $doc_put->saveXML();
// ATTENTION: This dom document contains credentials
//trigger_error($xml_put, E_USER_NOTICE);
trigger_error("set new zone records", E_USER_NOTICE);
$result = requestCurl($xml_put);
/* Receive */
$doc_result = new DOMDocument();
$doc_result->loadXML($result);
$doc_result->formatOutput = true;
//trigger_error($doc_result->saveXML(), E_USER_NOTICE);
/* Abort if setting the new zone records failed */
$xpath = new DOMXPath($doc_result);
$query = "status/type";
$entries = $xpath->query($query);
if ($entries->length > 0) {
$status = $entries->item(0)->nodeValue;
if ($status != "success") {
trigger_error("cannot set new zone records", E_USER_ERROR);
// respond("failed", "cannot set new zone records");
}
}
trigger_error("Update successful", E_USER_NOTICE);
// respond("success");
?>