71
|
1 |
<?php |
1
|
2 |
|
119
|
3 |
/* Poweradmin, a friendly web-based admin tool for PowerDNS. |
47
|
4 |
* See <https://rejo.zenger.nl/poweradmin> for more details. |
|
5 |
* |
|
6 |
* Copyright 2007, 2008 Rejo Zenger <rejo@zenger.nl> |
|
7 |
* |
|
8 |
* This program is free software: you can redistribute it and/or modify |
|
9 |
* it under the terms of the GNU General Public License as published by |
|
10 |
* the Free Software Foundation, either version 3 of the License, or |
|
11 |
* (at your option) any later version. |
|
12 |
* |
|
13 |
* This program is distributed in the hope that it will be useful, |
|
14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
16 |
* GNU General Public License for more details. |
|
17 |
* |
|
18 |
* You should have received a copy of the GNU General Public License |
|
19 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
20 |
*/ |
|
21 |
|
140
|
22 |
function validate_input($zid, $type, &$content, &$name, &$prio, &$ttl) { |
|
23 |
|
|
24 |
$zone = get_zone_name_from_id($zid); // TODO check for return |
1
|
25 |
|
140
|
26 |
if (!(preg_match("/$zone$/i", $name))) { |
|
27 |
if (isset($name) && $name != "") { |
|
28 |
$name = $name . "." . $zone; |
82
|
29 |
} else { |
140
|
30 |
$name = $zone; |
97
|
31 |
} |
96
|
32 |
} |
140
|
33 |
|
|
34 |
switch ($type) { |
96
|
35 |
|
140
|
36 |
case "A": |
|
37 |
if (!is_valid_ipv4($content)) return false; |
|
38 |
break; |
|
39 |
|
|
40 |
case "AAAA": |
|
41 |
if (!is_valid_ipv6($content)) return false; |
|
42 |
break; |
1
|
43 |
|
140
|
44 |
case "CNAME": |
|
45 |
if (!is_valid_rr_cname_name($name)) return false; |
|
46 |
if (!is_valid_hostname_fqdn($content,0)) return false; |
|
47 |
break; |
1
|
48 |
|
140
|
49 |
case "HINFO": |
|
50 |
if (!is_valid_rr_hinfo_content($content)) return false; |
|
51 |
break; |
|
52 |
|
|
53 |
case "MX": |
158
|
54 |
if (!is_valid_hostname_fqdn($content,0)) return false; |
140
|
55 |
if (!is_valid_mx_or_ns_target($content)) return false; |
|
56 |
break; |
1
|
57 |
|
140
|
58 |
case "NS": |
|
59 |
if (!is_valid_hostname_fqdn($content,0)) return false; |
|
60 |
if (!is_valid_mx_or_ns_target($content)) return false; |
|
61 |
break; |
|
62 |
|
|
63 |
case "PTR": |
|
64 |
if (!is_valid_hostname_fqdn($content,0)) return false; |
|
65 |
break; |
|
66 |
|
|
67 |
case "SOA": |
|
68 |
if (!is_valid_rr_soa_name($name,$zone)) return false; |
|
69 |
if (!is_valid_rr_soa_content($content)) return false; |
|
70 |
break; |
1
|
71 |
|
140
|
72 |
case "TXT": |
|
73 |
if (!is_valid_rr_txt_content($content)) return false; |
|
74 |
break; |
|
75 |
|
|
76 |
case "MBOXFW": |
|
77 |
case "NAPTR": |
148
|
78 |
case "SRV": |
140
|
79 |
case "URL": |
|
80 |
// These types are supported by PowerDNS, but there is not |
|
81 |
// yet code for validation. Validation needs to be added |
|
82 |
// for these types. One Day Real Soon Now. [tm] |
|
83 |
break; |
|
84 |
|
|
85 |
default: |
|
86 |
error(ERR_DNS_RR_TYPE); |
82
|
87 |
return false; |
1
|
88 |
} |
|
89 |
|
140
|
90 |
if (!is_valid_hostname_fqdn($name,1)) return false; |
|
91 |
if (!is_valid_rr_prio($prio,$type)) return false; |
|
92 |
if (!is_valid_rr_ttl($ttl)) return false; |
1
|
93 |
|
|
94 |
return true; |
|
95 |
} |
|
96 |
|
140
|
97 |
function is_valid_hostname_fqdn($hostname, $wildcard) { |
1
|
98 |
|
138
|
99 |
global $dns_strict_tld_check; |
|
100 |
global $valid_tlds; |
|
101 |
|
|
102 |
$hostname = ereg_replace("\.$","",$hostname); |
1
|
103 |
|
138
|
104 |
if (strlen($hostname) > 255) { |
|
105 |
error(ERR_DNS_HN_TOO_LONG); |
|
106 |
return false; |
|
107 |
} |
|
108 |
|
|
109 |
$hostname_labels = explode ('.', $hostname); |
|
110 |
$label_count = count($hostname_labels); |
|
111 |
|
140
|
112 |
foreach ($hostname_labels as $hostname_label) { |
|
113 |
if ($wildcard == 1 && !isset($first)) { |
|
114 |
if (!preg_match('/^(\*|[\w-\/]+)$/',$hostname_label)) { error(ERR_DNS_HN_INV_CHARS); return false; } |
|
115 |
$first = 1; |
|
116 |
} else { |
|
117 |
if (!preg_match('/^[\w-\/]+$/',$hostname_label)) { error(ERR_DNS_HN_INV_CHARS); return false; } |
|
118 |
} |
|
119 |
if (substr($hostname_label, 0, 1) == "-") { error(ERR_DNS_HN_DASH); return false; } |
|
120 |
if (substr($hostname_label, -1, 1) == "-") { error(ERR_DNS_HN_DASH); return false; } |
|
121 |
if (strlen($hostname_label) < 1 || strlen($hostname_label) > 63) { error(ERR_DNS_HN_LENGTH); return false; } |
|
122 |
} |
|
123 |
|
|
124 |
if ($hostname_labels[$label_count-1] == "arpa" && (substr_count($hostname_labels[0], "/") == 1 XOR substr_count($hostname_labels[1], "/") == 1)) { |
|
125 |
if (substr_count($hostname_labels[0], "/") == 1) { |
|
126 |
$array = explode ("/", $hostname_labels[0]); |
|
127 |
} else { |
|
128 |
$array = explode ("/", $hostname_labels[1]); |
|
129 |
} |
|
130 |
if (count($array) != 2) { error(ERR_DNS_HOSTNAME) ; return false; } |
|
131 |
if (!is_numeric($array[0]) || $array[0] < 0 || $array[0] > 255) { error(ERR_DNS_HOSTNAME) ; return false; } |
|
132 |
if (!is_numeric($array[1]) || $array[1] < 25 || $array[1] > 31) { error(ERR_DNS_HOSTNAME) ; return false; } |
|
133 |
} else { |
|
134 |
if (substr_count($hostname, "/") > 0) { error(ERR_DNS_HN_SLASH) ; return false; } |
|
135 |
} |
|
136 |
|
138
|
137 |
if ($dns_strict_tld_check == "1" && !in_array($hostname_labels[$label_count-1], $valid_tlds)) { |
140
|
138 |
error(ERR_DNS_INV_TLD); return false; |
1
|
139 |
} |
|
140 |
|
140
|
141 |
return true; |
|
142 |
} |
|
143 |
|
|
144 |
function is_valid_ipv4($ipv4) { |
|
145 |
|
|
146 |
// 20080424/RZ: The current code may be replaced by the following if() |
|
147 |
// statement, but it will raise the required PHP version to ">= 5.2.0". |
|
148 |
// Not sure if we want that now. |
|
149 |
// |
|
150 |
// if(filter_var($ipv4, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) === FALSE) { |
|
151 |
// error(ERR_DNS_IPV4); return false; |
|
152 |
// } |
|
153 |
|
|
154 |
if (preg_match("/^[0-9\.]{7,15}$/", $ipv4)) { |
|
155 |
error(ERR_DNS_IPV4); return false; |
|
156 |
} |
|
157 |
|
|
158 |
$quads = explode('.', $ipv4); |
|
159 |
$numquads = count($quads); |
|
160 |
|
|
161 |
if ($numquads != 4) { |
|
162 |
error(ERR_DNS_IPV4); return false; |
|
163 |
} |
|
164 |
|
|
165 |
for ($i = 0; $i < 4; $i++) { |
|
166 |
if ($quads[$i] > 255) { |
|
167 |
error(ERR_DNS_IPV4); return false; |
138
|
168 |
} |
|
169 |
} |
140
|
170 |
|
138
|
171 |
return true; |
|
172 |
} |
|
173 |
|
140
|
174 |
function is_valid_ipv6($ipv6) { |
|
175 |
|
|
176 |
// 20080424/RZ: The current code may be replaced by the following if() |
|
177 |
// statement, but it will raise the required PHP version to ">= 5.2.0". |
|
178 |
// Not sure if we want that now. |
|
179 |
// |
|
180 |
// if(filter_var($ipv6, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) === FALSE) { |
|
181 |
// error(ERR_DNS_IPV6); return false; |
|
182 |
// } |
|
183 |
|
|
184 |
if (preg_match("/^[0-9a-f]{0,4}:([0-9a-f]{0,4}:){0,6}[0-9a-f]{0,4}$/i", $ipv6)) { |
|
185 |
error(ERR_DNS_IPV6); return false; |
|
186 |
} |
|
187 |
|
|
188 |
$quads = explode(':', $ipv6); |
|
189 |
$numquads = count ($quads); |
|
190 |
|
|
191 |
if ($numquads > 8 || $numquads < 3) { |
|
192 |
error(ERR_DNS_IPV6); return false; |
|
193 |
} |
|
194 |
|
|
195 |
$emptyquads = 0; |
|
196 |
for ($i = 1; $i < $numquads-1; $i++) { |
|
197 |
if ($quads[$i] == "") $emptyquads++; |
|
198 |
} |
|
199 |
|
|
200 |
if ($emptyquads > 1) { |
|
201 |
error(ERR_DNS_IPV6); return false; |
|
202 |
} |
|
203 |
|
|
204 |
if ($emptyquads == 0 && $numquads != 8) { |
|
205 |
error(ERR_DNS_IPV6); return false; |
|
206 |
} |
|
207 |
|
|
208 |
return true; |
|
209 |
} |
|
210 |
|
|
211 |
function is_valid_rr_cname_name($name) { |
|
212 |
global $db; |
|
213 |
|
|
214 |
$query = "SELECT type, content |
|
215 |
FROM records |
|
216 |
WHERE content = " . $db->quote($name) . " |
|
217 |
AND (type = 'MX' OR type = 'NS')"; |
|
218 |
|
|
219 |
$response = $db->query($query); |
|
220 |
if (PEAR::isError($response)) { error($response->getMessage()); return false; }; |
1
|
221 |
|
140
|
222 |
if ($response->numRows() > 0) { |
|
223 |
error(ERR_DNS_CNAME); return false; |
|
224 |
} |
|
225 |
|
|
226 |
return true; |
|
227 |
} |
|
228 |
|
|
229 |
function is_valid_mx_or_ns_target($content) { |
|
230 |
global $db; |
|
231 |
|
|
232 |
$query = "SELECT type, name |
|
233 |
FROM records |
|
234 |
WHERE name = " . $db->quote($content) . " |
|
235 |
AND TYPE = 'CNAME'"; |
|
236 |
|
|
237 |
$response = $db->query($query); |
|
238 |
if (PEAR::isError($response)) { error($response->getMessage()); return false; }; |
|
239 |
|
|
240 |
if ($response->numRows() > 0) { |
|
241 |
error(ERR_DNS_MX_NS_TO_CNAME); return false; |
|
242 |
} |
|
243 |
|
|
244 |
return true; |
|
245 |
} |
|
246 |
|
|
247 |
function is_valid_rr_txt_content($content) { |
|
248 |
|
|
249 |
if (!preg_match("/^([^\s]{1,1000}|\"([^\"]{1,998}\"))$/i", $content)) { |
|
250 |
error(ERR_DNS_TXT_INV_CONTENT); return false; |
|
251 |
} |
|
252 |
|
|
253 |
return true; |
|
254 |
} |
|
255 |
|
|
256 |
function is_valid_rr_hinfo_content($content) { |
|
257 |
|
|
258 |
if ($content[0] == "\"") { |
|
259 |
$fields = preg_split('/(?<=") /', $content, 2); |
|
260 |
} else { |
|
261 |
$fields = preg_split('/ /', $content, 2); |
|
262 |
} |
|
263 |
|
|
264 |
for ($i = 0; ($i < 2); $i++) { |
|
265 |
if (!preg_match("/^([^\s]{1,1000}|\"([^\"]{1,998}\")$/i", $fields[$i])) { |
|
266 |
error(ERR_DNS_HINFO_INV_CONTENT); return false; |
|
267 |
} |
|
268 |
} |
|
269 |
|
|
270 |
return true; |
|
271 |
} |
|
272 |
|
|
273 |
function is_valid_rr_soa_content(&$content) { |
138
|
274 |
|
|
275 |
$fields = preg_split("/\s+/", trim($content)); |
|
276 |
$field_count = count($fields); |
|
277 |
|
|
278 |
if ($field_count == 0 || $field_count > 7) { |
|
279 |
return false; |
|
280 |
} else { |
|
281 |
if (!is_valid_hostname_fqdn($fields[0]) || preg_match('/\.arpa\.?$/',$fields[0])) { |
|
282 |
return false; |
|
283 |
} |
|
284 |
$final_soa = $fields[0]; |
121
|
285 |
|
138
|
286 |
if (isset($fields[1])) { |
|
287 |
$addr_input = $fields[1]; |
|
288 |
} else { |
|
289 |
global $dns_hostmaster; |
|
290 |
$addr_input = $dns_hostmaster; |
|
291 |
} |
|
292 |
if (!preg_match("/@/", $addr_input)) { |
|
293 |
$addr_input = preg_split('/(?<!\\\)\./', $addr_input, 2); |
|
294 |
$addr_to_check = str_replace("\\", "", $addr_input[0]) . "@" . $addr_input[1]; |
|
295 |
} else { |
|
296 |
$addr_to_check = $addr_input; |
|
297 |
} |
|
298 |
|
|
299 |
if (!is_valid_email($addr_to_check)) { |
|
300 |
return false; |
|
301 |
} else { |
|
302 |
$addr_final = explode('@', $addr_to_check, 2); |
|
303 |
$final_soa .= " " . str_replace(".", "\\.", $addr_final[0]) . "." . $addr_final[1]; |
|
304 |
} |
|
305 |
|
|
306 |
if (isset($fields[2])) { |
|
307 |
if (!is_numeric($fields[2])) { |
|
308 |
return false; |
1
|
309 |
} |
138
|
310 |
$final_soa .= " " . $fields[2]; |
|
311 |
} else { |
|
312 |
$final_soa .= " 0"; |
|
313 |
} |
|
314 |
|
|
315 |
if ($field_count == 7) { |
|
316 |
for ($i = 3; ($i < 7); $i++) { |
|
317 |
if (!is_numeric($fields[$i])) { |
|
318 |
return false; |
121
|
319 |
} else { |
138
|
320 |
$final_soa .= " " . $fields[$i]; |
1
|
321 |
} |
|
322 |
} |
|
323 |
} |
|
324 |
} |
138
|
325 |
$content = $final_soa; |
|
326 |
return true; |
1
|
327 |
} |
|
328 |
|
140
|
329 |
function is_valid_rr_soa_name($name, $zone) { |
|
330 |
if ($name != $zone) { |
|
331 |
error(ERR_DNS_SOA_NAME); return false; |
|
332 |
} |
|
333 |
return true; |
1
|
334 |
} |
|
335 |
|
140
|
336 |
function is_valid_rr_prio(&$prio, $type) { |
|
337 |
|
|
338 |
if ($type == "MX" ) { |
|
339 |
if (!is_numeric($prio) || $prio < 0 || $prio > 65535 ) { |
|
340 |
error(ERR_DNS_INV_PRIO); return false; |
|
341 |
} |
|
342 |
} else { |
|
343 |
$prio = ""; |
|
344 |
} |
|
345 |
|
|
346 |
return true; |
|
347 |
} |
|
348 |
|
|
349 |
function is_valid_rr_ttl(&$ttl) { |
|
350 |
|
|
351 |
if (!isset($ttl) || $ttl == "" ) { |
|
352 |
global $dns_ttl; |
|
353 |
$ttl = $dns_ttl; |
|
354 |
} |
|
355 |
|
|
356 |
if (!is_numeric($ttl) || $prio < 0 || $prio > 2147483647 ) { |
|
357 |
error(ERR_DNS_INV_TTL); return false; |
|
358 |
} |
|
359 |
|
|
360 |
return true; |
|
361 |
} |
|
362 |
|
|
363 |
function is_valid_search($holygrail) { |
|
364 |
|
62
|
365 |
// Only allow for alphanumeric, numeric, dot, dash, underscore and |
|
366 |
// percent in search string. The last two are wildcards for SQL. |
|
367 |
// Needs extension probably for more usual record types. |
|
368 |
|
|
369 |
return preg_match('/^[a-z0-9.\-%_]+$/i', $holygrail); |
|
370 |
} |
|
371 |
|
1
|
372 |
?> |