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