inc/dns.inc.php
changeset 164 4c6adb48dcfd
parent 162 e109f389316c
child 167 0b992665ac4d
equal deleted inserted replaced
163:32d4c63c50c9 164:4c6adb48dcfd
    33 	
    33 	
    34 	switch ($type) {
    34 	switch ($type) {
    35 
    35 
    36 		case "A":
    36 		case "A":
    37 			if (!is_valid_ipv4($content)) return false;
    37 			if (!is_valid_ipv4($content)) return false;
       
    38 			if (!is_valid_hostname_fqdn($name,1)) return false;
    38 			break;
    39 			break;
    39 
    40 
    40 		case "AAAA":
    41 		case "AAAA":
    41 			if (!is_valid_ipv6($content)) return false;
    42 			if (!is_valid_ipv6($content)) return false;
       
    43 			if (!is_valid_hostname_fqdn($name,1)) return false;
    42 			break;
    44 			break;
    43 
    45 
    44 		case "CNAME":
    46 		case "CNAME":
    45 			if (!is_valid_rr_cname_name($name)) return false;
    47 			if (!is_valid_rr_cname_name($name)) return false;
       
    48 			if (!is_valid_hostname_fqdn($name,1)) return false;
    46 			if (!is_valid_hostname_fqdn($content,0)) return false;
    49 			if (!is_valid_hostname_fqdn($content,0)) return false;
    47 			break;
    50 			break;
    48 
    51 
    49 		case "HINFO":
    52 		case "HINFO":
    50 			if (!is_valid_rr_hinfo_content($content)) return false;
    53 			if (!is_valid_rr_hinfo_content($content)) return false;
       
    54 			if (!is_valid_hostname_fqdn($name,1)) return false;
    51 			break;
    55 			break;
    52 
    56 
    53 		case "MX":
    57 		case "MX":
    54 			if (!is_valid_hostname_fqdn($content,0)) return false;
    58 			if (!is_valid_hostname_fqdn($content,0)) return false;
    55 			if (!is_valid_mx_or_ns_target($content)) return false;
    59 			if (!is_valid_hostname_fqdn($name,1)) return false;
       
    60 			if (!is_valid_non_alias_target($content)) return false;
    56 			break;
    61 			break;
    57 
    62 
    58 		case "NS":
    63 		case "NS":
    59 			if (!is_valid_hostname_fqdn($content,0)) return false;
    64 			if (!is_valid_hostname_fqdn($content,0)) return false;
    60 			if (!is_valid_mx_or_ns_target($content)) return false;
    65 			if (!is_valid_hostname_fqdn($name,1)) return false;
       
    66 			if (!is_valid_non_alias_target($content)) return false;
    61 			break;
    67 			break;
    62 
    68 
    63 		case "PTR":
    69 		case "PTR":
    64 			if (!is_valid_hostname_fqdn($content,0)) return false;
    70 			if (!is_valid_hostname_fqdn($content,0)) return false;
       
    71 			if (!is_valid_hostname_fqdn($name,1)) return false;
    65 			break;
    72 			break;
    66 
    73 
    67 		case "SOA":
    74 		case "SOA":
    68 			if (!is_valid_rr_soa_name($name,$zone)) return false;
    75 			if (!is_valid_rr_soa_name($name,$zone)) return false;
       
    76 			if (!is_valid_hostname_fqdn($name,1)) return false;
    69 			if (!is_valid_rr_soa_content($content)) return false;
    77 			if (!is_valid_rr_soa_content($content)) return false;
    70 			break;
    78 			break;
       
    79 		
       
    80 		case "SRV":
       
    81 			if (!is_valid_rr_srv_name($name)) return false;
       
    82 			if (!is_valid_rr_srv_content($content)) return false;
       
    83 			break;
    71 
    84 
    72 		case "TXT":
    85 		case "TXT":
    73 			if (!is_valid_rr_txt_content($content)) return false;
    86 			if (!is_valid_printable($name)) return false;
       
    87 			if (!is_valid_printable($content)) return false;
    74 			break;
    88 			break;
    75 
    89 
    76 		case "MBOXFW":
    90 		case "MBOXFW":
    77 		case "NAPTR":
    91 		case "NAPTR":
    78 		case "SRV":
       
    79 		case "URL":
    92 		case "URL":
    80 			// These types are supported by PowerDNS, but there is not
    93 			// These types are supported by PowerDNS, but there is not
    81 			// yet code for validation. Validation needs to be added 
    94 			// yet code for validation. Validation needs to be added 
    82 			// for these types. One Day Real Soon Now. [tm]
    95 			// for these types. One Day Real Soon Now. [tm]
    83 			break;
    96 			break;
    85 		default:
    98 		default:
    86 			error(ERR_DNS_RR_TYPE);
    99 			error(ERR_DNS_RR_TYPE);
    87 			return false;
   100 			return false;
    88 	}
   101 	}
    89 
   102 
    90 	if (!is_valid_hostname_fqdn($name,1)) return false;
       
    91 	if (!is_valid_rr_prio($prio,$type)) return false;
   103 	if (!is_valid_rr_prio($prio,$type)) return false;
    92 	if (!is_valid_rr_ttl($ttl)) return false;
   104 	if (!is_valid_rr_ttl($ttl)) return false;
    93 
   105 
    94 	return true;
   106 	return true;
    95 }
   107 }
   206 	}
   218 	}
   207 
   219 
   208 	return true;
   220 	return true;
   209 }
   221 }
   210 
   222 
       
   223 function is_valid_printable($string) {
       
   224 	if (!ereg('^[[:print:]]+$', trim($string))) { error(ERR_DNS_PRINTABLE); return false; }
       
   225 	return true;
       
   226 }
       
   227 
   211 function is_valid_rr_cname_name($name) {
   228 function is_valid_rr_cname_name($name) {
   212 	global $db;
   229 	global $db;
   213 
   230 
   214 	$query = "SELECT type, content 
   231 	$query = "SELECT type, content 
   215 			FROM records 
   232 			FROM records 
   224 	}
   241 	}
   225 
   242 
   226 	return true;
   243 	return true;
   227 }
   244 }
   228 
   245 
   229 function is_valid_mx_or_ns_target($content) {
   246 function is_valid_non_alias_target($target) {
   230 	global $db;
   247 	global $db;
   231 	
   248 	
   232 	$query = "SELECT type, name
   249 	$query = "SELECT type, name
   233 			FROM records
   250 			FROM records
   234 			WHERE name = " . $db->quote($content) . "
   251 			WHERE name = " . $db->quote($target) . "
   235 			AND TYPE = 'CNAME'";
   252 			AND TYPE = 'CNAME'";
   236 
   253 
   237 	$response = $db->query($query);
   254 	$response = $db->query($query);
   238 	if (PEAR::isError($response)) { error($response->getMessage()); return false; };
   255 	if (PEAR::isError($response)) { error($response->getMessage()); return false; };
   239 
       
   240 	if ($response->numRows() > 0) {
   256 	if ($response->numRows() > 0) {
   241 		error(ERR_DNS_MX_NS_TO_CNAME); return false;
   257 		error(ERR_DNS_MX_NS_TO_CNAME); return false;
   242 	}
   258 	}
   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;
   259 	return true;
   254 }
   260 }
   255 
   261 
   256 function is_valid_rr_hinfo_content($content) {
   262 function is_valid_rr_hinfo_content($content) {
   257 
   263 
   332 	}
   338 	}
   333 	return true;
   339 	return true;
   334 }
   340 }
   335 
   341 
   336 function is_valid_rr_prio(&$prio, $type) {
   342 function is_valid_rr_prio(&$prio, $type) {
   337 
   343 	if ($type == "MX" || $type == "SRV" ) {
   338 	if ($type == "MX" ) {
       
   339 		if (!is_numeric($prio) || $prio < 0 || $prio > 65535 ) {
   344 		if (!is_numeric($prio) || $prio < 0 || $prio > 65535 ) {
   340 			error(ERR_DNS_INV_PRIO); return false;
   345 			error(ERR_DNS_INV_PRIO); return false;
   341 		}
   346 		}
   342 	} else {
   347 	} else {
   343 		$prio = "";
   348 		$prio = "";
   344 	}
   349 	}
   345 
   350 
       
   351 	return true;
       
   352 }
       
   353 
       
   354 function is_valid_rr_srv_name($name){
       
   355 	$fields = explode('.', $name, 3);
       
   356 	if (!preg_match('/^_[a-z0-9]+$/i', $fields[0])) { error(ERR_DNS_SRV_NAME) ; return false; }
       
   357 	if (!preg_match('/^_[a-z0-9]+$/i', $fields[1])) { error(ERR_DNS_SRV_NAME) ; return false; }
       
   358 	if (!is_valid_hostname_fqdn($fields[2],0)) { error(ERR_DNS_SRV_NAME) ; return false ; }
       
   359 	return true ;
       
   360 }
       
   361 
       
   362 function is_valid_rr_srv_content($content) {
       
   363 	$fields = preg_split("/\s+/", trim($content), 3);
       
   364 	if (!is_numeric($fields[0]) || $fields[0] < 0 || $fields[0] > 65535) { error(ERR_DNS_SRV_WGHT) ; return false; } 
       
   365 	if (!is_numeric($fields[1]) || $fields[1] < 0 || $fields[1] > 65535) { error(ERR_DNS_SRV_PORT) ; return false; } 
       
   366 	if ($fields[2] == "" || ($fields[2] != "." && !is_valid_hostname_fqdn($fields[2],0))) {
       
   367 		error(ERR_DNS_SRV_TRGT) ; return false; 
       
   368 	} 
   346 	return true;
   369 	return true;
   347 }
   370 }
   348 
   371 
   349 function is_valid_rr_ttl(&$ttl) {
   372 function is_valid_rr_ttl(&$ttl) {
   350 
   373