inc/dns.inc.php
changeset 140 e68c3d6094d1
parent 138 3e36ebbfe048
child 148 362a952b0a12
equal deleted inserted replaced
139:98ce0ffae5b8 140:e68c3d6094d1
    17  *
    17  *
    18  *  You should have received a copy of the GNU General Public License
    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/>.
    19  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
    20  */
    20  */
    21 
    21 
    22 function validate_input($zid, $type, &$content, &$name, &$prio, &$ttl)
    22 function validate_input($zid, $type, &$content, &$name, &$prio, &$ttl) {
    23 {
    23 
    24 	global $db;
    24 	$zone = get_zone_name_from_id($zid);				// TODO check for return
    25 	$domain = get_domain_name_from_id($zid);
    25 
    26 	$nocheck = array('SOA', 'HINFO', 'NAPTR', 'URL', 'MBOXFW', 'TXT');
    26 	if (!(preg_match("/$zone$/i", $name))) {
    27 	$hostname = false;
    27 		if (isset($name) && $name != "") {
    28 	$ip4 = false;
    28 			$name = $name . "." . $zone;
    29 	$ip6 = false;
    29 		} else {
    30 
    30 			$name = $zone;
    31 	if(!in_array(strtoupper($type), $nocheck)) {
    31 		}
    32 		if(!is_valid_ip6($content)) {
    32 	}
    33 			if(!is_valid_ip($content)) {
    33 	
    34 				if(!is_valid_hostname($content)) {
    34 	switch ($type) {
    35 					error(ERR_DNS_CONTENT);
    35 
    36 					return false;
    36 		case "A":
    37 				} else {
    37 			if (!is_valid_ipv4($content)) return false;
    38 					$hostname = true;
    38 			break;
    39 				}
    39 
    40 			} else {
    40 		case "AAAA":
    41 				$ip4 = true;
    41 			if (!is_valid_ipv6($content)) return false;
    42 			}
    42 			break;
    43 		} else {
    43 
    44 			$ip6 = true;
    44 		case "CNAME":
    45 		}
    45 			if (!is_valid_rr_cname_name($name)) return false;
    46 	}
    46 			if (!is_valid_hostname_fqdn($content,0)) return false;
    47 
    47 			break;
    48 	// Prepare total hostname.
    48 
    49 	if ($name == '*') {
    49 		case "HINFO":
    50 		$wildcard = true;
    50 			if (!is_valid_rr_hinfo_content($content)) return false;
    51 	} else {
    51 			break;
    52 		$wildcard = false;
    52 
    53 	}
    53 		case "MX":
    54 
    54 			if (!is_valid_hostname_fqdn($contenti,0)) return false;
    55 	if (preg_match("/@/", $name)) {
    55 			if (!is_valid_mx_or_ns_target($content)) return false;
    56 		$name = $domain ;
    56 			break;
    57 	} elseif ( !(preg_match("/$domain$/i", $name))) {
    57 
    58 
    58 		case "NS":
    59 		if ( isset($name) && $name != "" ) {
    59 			if (!is_valid_hostname_fqdn($content,0)) return false;
    60 			$name = $name . "." . $domain ;
    60 			if (!is_valid_mx_or_ns_target($content)) return false;
    61 		} else {
    61 			break;
    62 			$name = $domain ;
    62 
    63 		}
    63 		case "PTR":
    64 	}
    64 			if (!is_valid_hostname_fqdn($content,0)) return false;
    65 
    65 			break;
    66 	if(!$wildcard) {
    66 
    67 		if(!is_valid_hostname($name)) {
    67 		case "SOA":
    68 			error(ERR_DNS_HOSTNAME);
    68 			if (!is_valid_rr_soa_name($name,$zone)) return false;
       
    69 			if (!is_valid_rr_soa_content($content)) return false;
       
    70 			break;
       
    71 
       
    72 		case "TXT":
       
    73 			if (!is_valid_rr_txt_content($content)) return false;
       
    74 			break;
       
    75 
       
    76 		case "MBOXFW":
       
    77 		case "NAPTR":
       
    78 		case "URL":
       
    79 			// These types are supported by PowerDNS, but there is not
       
    80 			// yet code for validation. Validation needs to be added 
       
    81 			// for these types. One Day Real Soon Now. [tm]
       
    82 			break;
       
    83 
       
    84 		default:
       
    85 			error(ERR_DNS_RR_TYPE);
    69 			return false;
    86 			return false;
    70 		}
    87 	}
    71 	}
    88 
    72 
    89 	if (!is_valid_hostname_fqdn($name,1)) return false;
    73 	// Check record type (if it exists in our allowed list.
    90 	if (!is_valid_rr_prio($prio,$type)) return false;
    74 	if (!in_array(strtoupper($type), get_record_types())) {
    91 	if (!is_valid_rr_ttl($ttl)) return false;
    75 		error(ERR_DNS_RECORDTYPE);
    92 
    76 		return false;
    93 	return true;
    77 	}
    94 }
    78 
    95 
    79 	// Start handling the demands for the functions.
    96 function is_valid_hostname_fqdn($hostname, $wildcard) {
    80 	// Validation for IN A records. Can only have an IP. Nothing else.
    97 
    81 	if ($type == 'A' && !$ip4) {
       
    82 		error(ERR_DNS_IPV4);
       
    83 		return false;
       
    84 	}
       
    85 
       
    86 	if ($type == 'AAAA' && !$ip6) {
       
    87 		error(ERR_DNS_IPV6);
       
    88 		return false;
       
    89 	}
       
    90 
       
    91 	if ($type == 'CNAME' && $hostname) {
       
    92 		if(!is_valid_cname($name)) {
       
    93 			error(ERR_DNS_CNAME);
       
    94 			return false;
       
    95 		}
       
    96 	}
       
    97 
       
    98 	if ($type == 'NS') {
       
    99 		$status = is_valid_ns($content, $hostname);
       
   100 		if($status == -1) {
       
   101 			error(ERR_DNS_NS_HNAME);
       
   102 			return false;
       
   103 		}
       
   104 		elseif($status == -2) {
       
   105 			error(ERR_DNS_NS_CNAME);
       
   106 			return false;
       
   107 		}
       
   108 	}
       
   109 
       
   110 	if ($type == 'SOA' && !is_valid_rr_soa($content)) {
       
   111 		return false;
       
   112 	}
       
   113 
       
   114 	// HINFO and TXT require no validation.
       
   115 
       
   116 	if ($type == 'URL') {
       
   117 		if(!is_valid_url($content)) {
       
   118 			error(ERR_INV_URL);
       
   119 			return false;
       
   120 		}
       
   121 	}
       
   122 	if ($type == 'MBOXFW') 	{
       
   123 		if(!is_valid_mboxfw($content)) {
       
   124 			error(ERR_INV_EMAIL);
       
   125 			return false;
       
   126 		}
       
   127 	}
       
   128 
       
   129 	// NAPTR has to be done.
       
   130 	// Do we want that?
       
   131 	// http://www.ietf.org/rfc/rfc2915.txt
       
   132 	// http://www.zvon.org/tmRFC/RFC2915/Output/chapter2.html
       
   133 	// http://www.zvon.org/tmRFC/RFC3403/Output/chapter4.html
       
   134 
       
   135 	// See if the prio field is valid and if we have one.
       
   136 	// If we dont have one and the type is MX record, give it value '10'
       
   137 	if($type == 'NAPTR') {
       
   138 
       
   139 	}
       
   140 	
       
   141 	if($type == 'MX') {
       
   142 		if($hostname) {
       
   143 			$status = is_valid_mx($content, $prio);
       
   144 			if($status == -1) {
       
   145 				error(ERR_DNS_MX_CNAME);
       
   146 				return false;
       
   147 			}
       
   148 			elseif($status == -2) {
       
   149 				error(ERR_DNS_MX_PRIO);
       
   150 				return false;
       
   151 			}
       
   152 		} else {
       
   153 			error( _('If you specify an MX record it must be a hostname.') ); // TODO make error
       
   154 			return false;
       
   155 		}
       
   156 	} else {
       
   157 		$prio=0;
       
   158 	}
       
   159 	// Validate the TTL, it has to be numeric.
       
   160 	$ttl = (!isset($ttl) || !is_numeric($ttl)) ? $dns_ttl : $ttl;
       
   161 	
       
   162 	return true;
       
   163 }
       
   164 
       
   165 /*
       
   166  * Validatis a CNAME record by the name it will have and its destination
       
   167  *
       
   168  */
       
   169 function is_valid_cname($dest)
       
   170 {
       
   171 	/*
       
   172 	 * This is really EVIL.
       
   173 	 * If the new record (a CNAME) record is being pointed to by a MX record or NS record we have to bork.
       
   174 	 * this is the idea.
       
   175 	 *
       
   176 	 * MX record: blaat.nl MX mail.blaat.nl
       
   177 	 * Now we look what mail.blaat.nl is
       
   178 	 * We discover the following:
       
   179 	 * mail.blaat.nl CNAME bork.blaat.nl
       
   180 	 * This is NOT allowed! mail.onthanet.nl can not be a CNAME!
       
   181 	 * The same goes for NS. mail.blaat.nl must have a normal IN A record.
       
   182 	 * It MAY point to a CNAME record but its not wished. Lets not support it.
       
   183 	 */
       
   184 
       
   185 	global $db;
       
   186 
       
   187 	// Check if there are other records with this information of the following types.
       
   188 	// P.S. we might add CNAME to block CNAME recursion and chains.
       
   189 	$blockedtypes = " AND (type='MX' OR type='NS')";
       
   190 
       
   191 	$cnamec = "SELECT type, content FROM records WHERE content=".$db->quote($dest) . $blockedtypes;
       
   192 	$result = $db->query($cnamec);
       
   193 
       
   194 	if($result->numRows() > 0)
       
   195 	{
       
   196 		return false;
       
   197 		// Lets inform the user he is doing something EVIL.
       
   198 		// Ok we found a record that has our content field in their content field.
       
   199 	}
       
   200 	return true;
       
   201 }
       
   202 
       
   203 
       
   204 /*
       
   205  * Checks if something is a valid domain.
       
   206  * Checks for domainname with the allowed characters <a,b,...z,A,B,...Z> and - and _.
       
   207  * This part must be followed by a 2 to 4 character TLD.
       
   208  */
       
   209 function is_valid_domain($domain)
       
   210 {
       
   211 	if ((eregi("^[0-9a-z]([-.]?[0-9a-z])*\\.[a-z]{2,4}$", $domain)) && (strlen($domain) <= 128))
       
   212 	{
       
   213 		return true;
       
   214 	}
       
   215 	return false;
       
   216 }
       
   217 
       
   218 
       
   219 /*
       
   220  * Validates if given hostname is allowed.
       
   221  * returns true if allowed.
       
   222  */
       
   223 function is_valid_hostname($host)
       
   224 {
       
   225 	if(count(explode(".", $host)) == 1)
       
   226 	{
       
   227 		return false;
       
   228 	}
       
   229 
       
   230 	// Its not perfect (in_addr.int is allowed) but works for now.
       
   231 
       
   232 	if(preg_match('!(ip6|in-addr).(arpa|int)$!i', $host))
       
   233 	{
       
   234 		if(preg_match('!^(([A-Z\d]|[A-Z\d][A-Z\d-]*[A-Z\d])\.)*[A-Z\d]+$!i', $host))
       
   235 		{
       
   236 			return true;
       
   237 		}
       
   238 		return false;
       
   239 	}
       
   240 
       
   241 	// Validate further.
       
   242 	return (preg_match('!^(([A-Z\d]|[A-Z\d][A-Z\d-]*[A-Z\d])\.)*[A-Z\d]+$!i', $host)) ? true : false;
       
   243 }
       
   244 
       
   245 
       
   246 /*
       
   247  * Validates an IPv4 IP.
       
   248  * returns true if valid.
       
   249  */
       
   250 function is_valid_ip($ip)
       
   251 {
       
   252 	// Stop reading at this point. Scroll down to the next function...
       
   253 	// Ok... you didn't stop reading... now you have to rewrite the whole function! enjoy ;-)
       
   254 	// Trance unborked it. Twice even!
       
   255 	return ($ip == long2ip(ip2long($ip))) ? true : false;
       
   256 
       
   257 }
       
   258 
       
   259 
       
   260 /*
       
   261  * Validates an IPv6 IP.
       
   262  * returns true if valid.
       
   263  */
       
   264 function is_valid_ip6($ip)
       
   265 {
       
   266 	// Validates if the given IP is truly an IPv6 address.
       
   267 	// Precondition: have a string
       
   268 	// Postcondition: false: Error in IP
       
   269 	//                true: IP is correct
       
   270 	// Requires: String
       
   271 	// Date: 10-sep-2002
       
   272 	if(preg_match('!^[A-F0-9:]{1,39}$!i', $ip) == true)
       
   273 	{
       
   274 		// Not 3 ":" or more.
       
   275 		$p = explode(':::', $ip);
       
   276 		if(sizeof($p) > 1)
       
   277 		{
       
   278 			return false;
       
   279 		}
       
   280 		// Find if there is only one occurence of "::".
       
   281 		$p = explode('::', $ip);
       
   282 		if(sizeof($p) > 2)
       
   283 		{
       
   284 			return false;
       
   285 		}
       
   286 		// Not more than 8 octects
       
   287 		$p = explode(':', $ip);
       
   288 
       
   289 		if(sizeof($p) > 8)
       
   290 		{
       
   291 			return false;
       
   292 		}
       
   293 
       
   294 		// Check octet length
       
   295 		foreach($p as $checkPart)
       
   296 		{
       
   297 			if(strlen($checkPart) > 4)
       
   298 			{
       
   299 				return false;
       
   300 			}
       
   301 		}
       
   302 		return true;
       
   303 	}
       
   304 	return false;
       
   305 }
       
   306 
       
   307 
       
   308 /*
       
   309  * FANCY RECORD.
       
   310  * Validates if the fancy record mboxfw is an actual email address.
       
   311  */
       
   312 function is_valid_mboxfw($email)
       
   313 {
       
   314 	return is_valid_email($email);
       
   315 }
       
   316 
       
   317 
       
   318 /*
       
   319  * Validates MX records.
       
   320  * an MX record cant point to a CNAME record. This has to be checked.
       
   321  * this function also sets a proper priority.
       
   322  */
       
   323 function is_valid_mx($content, &$prio)
       
   324 {
       
   325 	global $db;
       
   326 	// See if the destination to which this MX is pointing is NOT a CNAME record.
       
   327 	// Check inside our dns server.
       
   328 	if($db->queryOne("SELECT count(id) FROM records WHERE name=".$db->quote($content)." AND type='CNAME'") > 0)
       
   329 	{
       
   330 		return -1;
       
   331 	}
       
   332 	else
       
   333 	{
       
   334 		// Fix the proper priority for the record.
       
   335 		// Bugfix, thanks Oscar :)
       
   336 		if(!isset($prio))
       
   337 		{
       
   338 			$prio = 10;
       
   339 		}
       
   340 		if(!is_numeric($prio))
       
   341 		{
       
   342 			if($prio == '')
       
   343 			{
       
   344 				$prio = 10;
       
   345 			}
       
   346 			else
       
   347 			{
       
   348 				return -2;
       
   349 			}
       
   350 		}
       
   351 	}
       
   352 	return 1;
       
   353 }
       
   354 
       
   355 /*
       
   356  * Validates NS records.
       
   357  * an NS record cant point to a CNAME record. This has to be checked.
       
   358  * $hostname directive means if its a hostname or not (this to avoid that NS records get ip fields)
       
   359  * NS must have a hostname, it is not allowed to have an IP.
       
   360  */
       
   361 function is_valid_ns($content, $hostname)
       
   362 {
       
   363 	global $db;
       
   364 	// Check if the field is a hostname, it MUST be a hostname.
       
   365 	if(!$hostname)
       
   366 	{
       
   367 		return -1;
       
   368 		// "an IN NS field must be a hostname."
       
   369 	}
       
   370 
       
   371 	if($db->queryOne("SELECT count(id) FROM records WHERE name=".$db->quote($content)." AND type='CNAME'") > 0)
       
   372 	{
       
   373 		return -2;
       
   374 		// "You can not point a NS record to a CNAME record. Remove/rename the CNAME record first or take another name."
       
   375 
       
   376 	}
       
   377 	return 1;
       
   378 }
       
   379 
       
   380 
       
   381 function is_valid_hostname_label($hostname_label) {
       
   382 
       
   383         // See <https://www.poweradmin.org/trac/wiki/Documentation/DNS-hostnames>.
       
   384         if (!preg_match('/^[a-z\d]([a-z\d-]*[a-z\d])*$/i',$hostname_label)) {
       
   385 		return false;
       
   386         } elseif (preg_match('/^[\d]+$/i',$hostname_label)) {
       
   387                 return false;
       
   388         } elseif ((strlen($hostname_label) < 2) || (strlen($hostname_label) > 63)) {
       
   389                 return false;
       
   390         }
       
   391         return true;
       
   392 }
       
   393 
       
   394 function is_valid_hostname_fqdn($hostname) {
       
   395 
       
   396         // See <https://www.poweradmin.org/trac/wiki/Documentation/DNS-hostnames>.
       
   397 	global $dns_strict_tld_check;
    98 	global $dns_strict_tld_check;
   398 	global $valid_tlds;
    99 	global $valid_tlds;
   399 
   100 
   400 	$hostname = ereg_replace("\.$","",$hostname);
   101 	$hostname = ereg_replace("\.$","",$hostname);
   401 
   102 
   405 	}
   106 	}
   406 
   107 
   407         $hostname_labels = explode ('.', $hostname);
   108         $hostname_labels = explode ('.', $hostname);
   408         $label_count = count($hostname_labels);
   109         $label_count = count($hostname_labels);
   409 
   110 
       
   111 	foreach ($hostname_labels as $hostname_label) {
       
   112 		if ($wildcard == 1 && !isset($first)) {
       
   113 			if (!preg_match('/^(\*|[\w-\/]+)$/',$hostname_label)) { error(ERR_DNS_HN_INV_CHARS); return false; }
       
   114 			$first = 1;
       
   115 		} else {
       
   116 			if (!preg_match('/^[\w-\/]+$/',$hostname_label)) { error(ERR_DNS_HN_INV_CHARS); return false; }
       
   117 		}
       
   118 		if (substr($hostname_label, 0, 1) == "-") { error(ERR_DNS_HN_DASH); return false; }
       
   119 		if (substr($hostname_label, -1, 1) == "-") { error(ERR_DNS_HN_DASH); return false; }
       
   120 		if (strlen($hostname_label) < 1 || strlen($hostname_label) > 63) { error(ERR_DNS_HN_LENGTH); return false; }
       
   121 	}
       
   122 	
       
   123 	if ($hostname_labels[$label_count-1] == "arpa" && (substr_count($hostname_labels[0], "/") == 1 XOR substr_count($hostname_labels[1], "/") == 1)) {
       
   124 		if (substr_count($hostname_labels[0], "/") == 1) { 
       
   125 			$array = explode ("/", $hostname_labels[0]);
       
   126 		} else {
       
   127 			$array = explode ("/", $hostname_labels[1]);
       
   128 		}
       
   129 		if (count($array) != 2) { error(ERR_DNS_HOSTNAME) ; return false; }
       
   130 		if (!is_numeric($array[0]) || $array[0] < 0 || $array[0] > 255) { error(ERR_DNS_HOSTNAME) ; return false; }
       
   131 		if (!is_numeric($array[1]) || $array[1] < 25 || $array[1] > 31) { error(ERR_DNS_HOSTNAME) ; return false; }
       
   132 	} else {
       
   133 		if (substr_count($hostname, "/") > 0) { error(ERR_DNS_HN_SLASH) ; return false; }
       
   134 	}
       
   135 	
   410 	if ($dns_strict_tld_check == "1" && !in_array($hostname_labels[$label_count-1], $valid_tlds)) {
   136 	if ($dns_strict_tld_check == "1" && !in_array($hostname_labels[$label_count-1], $valid_tlds)) {
   411 		error(ERR_DNS_INV_TLD);
   137 		error(ERR_DNS_INV_TLD); return false;
   412 		return false;
   138 	}
   413 	}
   139 
   414 
   140 	return true;
   415 	if ($hostname_labels[$label_count-1] == "arpa") {
   141 }
   416 		// FIXME
   142 
       
   143 function is_valid_ipv4($ipv4) {
       
   144 
       
   145 // 20080424/RZ: The current code may be replaced by the following if() 
       
   146 // statement, but it will raise the required PHP version to ">= 5.2.0". 
       
   147 // Not sure if we want that now.
       
   148 //
       
   149 //	if(filter_var($ipv4, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) === FALSE) {
       
   150 //		error(ERR_DNS_IPV4); return false;
       
   151 //	}
       
   152 
       
   153 	if (preg_match("/^[0-9\.]{7,15}$/", $ipv4)) {
       
   154 		error(ERR_DNS_IPV4); return false;
       
   155 	}
       
   156 
       
   157 	$quads = explode('.', $ipv4);
       
   158 	$numquads = count($quads);
       
   159 	
       
   160 	if ($numquads != 4) {
       
   161 		error(ERR_DNS_IPV4); return false;
       
   162 	}
       
   163 
       
   164 	for ($i = 0; $i < 4; $i++) {
       
   165 		if ($quads[$i] > 255) {
       
   166 			error(ERR_DNS_IPV4); return false;
       
   167 		}
       
   168 	}
       
   169 
       
   170 	return true;
       
   171 }
       
   172 
       
   173 function is_valid_ipv6($ipv6) {
       
   174 
       
   175 // 20080424/RZ: The current code may be replaced by the following if() 
       
   176 // statement, but it will raise the required PHP version to ">= 5.2.0". 
       
   177 // Not sure if we want that now.
       
   178 //
       
   179 //	if(filter_var($ipv6, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) === FALSE) {
       
   180 //		error(ERR_DNS_IPV6); return false;
       
   181 //	}
       
   182 
       
   183 	if (preg_match("/^[0-9a-f]{0,4}:([0-9a-f]{0,4}:){0,6}[0-9a-f]{0,4}$/i", $ipv6)) {
       
   184 		error(ERR_DNS_IPV6); return false;
       
   185 	}
       
   186 
       
   187 	$quads = explode(':', $ipv6);
       
   188 	$numquads = count ($quads);
       
   189 
       
   190 	if ($numquads > 8 || $numquads < 3) {
       
   191 		error(ERR_DNS_IPV6); return false;
       
   192 	}
       
   193 
       
   194 	$emptyquads = 0;
       
   195 	for ($i = 1; $i < $numquads-1; $i++) {
       
   196 		if ($quads[$i] == "") $emptyquads++;
       
   197 	}
       
   198 
       
   199 	if ($emptyquads > 1) {
       
   200 		error(ERR_DNS_IPV6); return false;
       
   201 	}
       
   202 
       
   203 	if ($emptyquads == 0 && $numquads != 8) {
       
   204 		error(ERR_DNS_IPV6); return false;
       
   205 	}
       
   206 
       
   207 	return true;
       
   208 }
       
   209 
       
   210 function is_valid_rr_cname_name($name) {
       
   211 	global $db;
       
   212 
       
   213 	$query = "SELECT type, content 
       
   214 			FROM records 
       
   215 			WHERE content = " . $db->quote($name) . "
       
   216 			AND (type = 'MX' OR type = 'NS')";
       
   217 	
       
   218 	$response = $db->query($query);
       
   219 	if (PEAR::isError($response)) { error($response->getMessage()); return false; };
       
   220 
       
   221 	if ($response->numRows() > 0) {
       
   222 		error(ERR_DNS_CNAME); return false;
       
   223 	}
       
   224 
       
   225 	return true;
       
   226 }
       
   227 
       
   228 function is_valid_mx_or_ns_target($content) {
       
   229 	global $db;
       
   230 	
       
   231 	$query = "SELECT type, name
       
   232 			FROM records
       
   233 			WHERE name = " . $db->quote($content) . "
       
   234 			AND TYPE = 'CNAME'";
       
   235 
       
   236 	$response = $db->query($query);
       
   237 	if (PEAR::isError($response)) { error($response->getMessage()); return false; };
       
   238 
       
   239 	if ($response->numRows() > 0) {
       
   240 		error(ERR_DNS_MX_NS_TO_CNAME); return false;
       
   241 	}
       
   242 
       
   243 	return true;
       
   244 }
       
   245 
       
   246 function is_valid_rr_txt_content($content) {
       
   247 
       
   248 	if (!preg_match("/^([^\s]{1,1000}|\"([^\"]{1,998}\"))$/i", $content)) {
       
   249 		error(ERR_DNS_TXT_INV_CONTENT); return false;
       
   250 	}
       
   251 
       
   252 	return true;
       
   253 }
       
   254 
       
   255 function is_valid_rr_hinfo_content($content) {
       
   256 
       
   257 	if ($content[0] == "\"") {
       
   258 		$fields = preg_split('/(?<=") /', $content, 2);
   417 	} else {
   259 	} else {
   418 		foreach ($hostname_labels as $hostname_label) {
   260 		$fields = preg_split('/ /', $content, 2);
   419 			if (!is_valid_hostname_label($hostname_label)) {
   261 	}
   420 				error(ERR_DNS_HOSTNAME);
   262 
   421 				return false;
   263 	for ($i = 0; ($i < 2); $i++) {
   422 			}
   264 		if (!preg_match("/^([^\s]{1,1000}|\"([^\"]{1,998}\")$/i", $fields[$i])) {
   423 		}
   265 			error(ERR_DNS_HINFO_INV_CONTENT); return false;
   424 	}
   266 		}
   425 	return true;
   267 	}
   426 }
   268 
   427 
   269 	return true;
   428 function is_valid_rr_soa(&$content) {
   270 }
   429 
   271 
   430 	// TODO move to appropiate location
   272 function is_valid_rr_soa_content(&$content) {
   431 //	$return = get_records_by_type_from_domid("SOA", $zid);
       
   432 //	if($return->numRows() > 1) {
       
   433 //		return false;
       
   434 //	}
       
   435 
   273 
   436 	$fields = preg_split("/\s+/", trim($content));
   274 	$fields = preg_split("/\s+/", trim($content));
   437         $field_count = count($fields);
   275         $field_count = count($fields);
   438 
   276 
   439 	if ($field_count == 0 || $field_count > 7) {
   277 	if ($field_count == 0 || $field_count > 7) {
   485 	}
   323 	}
   486 	$content = $final_soa;
   324 	$content = $final_soa;
   487 	return true;
   325 	return true;
   488 }
   326 }
   489 
   327 
   490 
   328 function is_valid_rr_soa_name($name, $zone) {
   491 function is_valid_url($url)
   329 	if ($name != $zone) {
   492 {
   330 		error(ERR_DNS_SOA_NAME); return false;
   493 	return preg_match('!^(http://)(([A-Z\d]|[A-Z\d][A-Z\d-]*[A-Z\d])\.)*[A-Z\d]+([//]([0-9a-z//~#%&\'_\-+=:?.]*))?$!i',  $url);
   331 	}
   494 }
   332 	return true;
   495 
   333 }
   496 function is_valid_search($holygrail)
   334 
   497 {
   335 function is_valid_rr_prio(&$prio, $type) {
       
   336 
       
   337 	if ($type == "MX" ) {
       
   338 		if (!is_numeric($prio) || $prio < 0 || $prio > 65535 ) {
       
   339 			error(ERR_DNS_INV_PRIO); return false;
       
   340 		}
       
   341 	} else {
       
   342 		$prio = "";
       
   343 	}
       
   344 
       
   345 	return true;
       
   346 }
       
   347 
       
   348 function is_valid_rr_ttl(&$ttl) {
       
   349 
       
   350 	if (!isset($ttl) || $ttl == "" ) {
       
   351 		global $dns_ttl;
       
   352 		$ttl = $dns_ttl;
       
   353 	}
       
   354 	
       
   355 	if (!is_numeric($ttl) ||  $prio < 0 || $prio > 2147483647 ) {
       
   356 		error(ERR_DNS_INV_TTL);	return false;
       
   357 	}
       
   358 
       
   359 	return true;
       
   360 }
       
   361 
       
   362 function is_valid_search($holygrail) {
       
   363 
   498 	// Only allow for alphanumeric, numeric, dot, dash, underscore and 
   364 	// Only allow for alphanumeric, numeric, dot, dash, underscore and 
   499 	// percent in search string. The last two are wildcards for SQL.
   365 	// percent in search string. The last two are wildcards for SQL.
   500 	// Needs extension probably for more usual record types.
   366 	// Needs extension probably for more usual record types.
   501 
   367 
   502 	return preg_match('/^[a-z0-9.\-%_]+$/i', $holygrail);
   368 	return preg_match('/^[a-z0-9.\-%_]+$/i', $holygrail);
   503 }
   369 }
   504 
   370 
   505 
       
   506 ?>
   371 ?>