inc/dns.inc.php
author rejo
Mon, 12 May 2008 07:36:11 +0000
changeset 157 af604e6e1a71
parent 148 362a952b0a12
child 158 30a7f897aaf0
permissions -rw-r--r--
[feladat @ 263] Fixed create user example for PgSQL to have create a new user with a password.
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":
148
362a952b0a12 [feladat @ 254]
rejo
parents: 140
diff changeset
    78
		case "SRV":
140
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
    79
		case "URL":
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
    80
			// These types are supported by PowerDNS, but there is not
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
    81
			// yet code for validation. Validation needs to be added 
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
    82
			// for these types. One Day Real Soon Now. [tm]
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
    83
			break;
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
    84
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
    85
		default:
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
    86
			error(ERR_DNS_RR_TYPE);
82
c255196bc447 [feladat @ 182]
rejo
parents: 79
diff changeset
    87
			return false;
1
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    88
	}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    89
140
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
    90
	if (!is_valid_hostname_fqdn($name,1)) return false;
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
    91
	if (!is_valid_rr_prio($prio,$type)) return false;
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
    92
	if (!is_valid_rr_ttl($ttl)) return false;
1
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    93
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    94
	return true;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    95
}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    96
140
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
    97
function is_valid_hostname_fqdn($hostname, $wildcard) {
1
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    98
138
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
    99
	global $dns_strict_tld_check;
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   100
	global $valid_tlds;
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   101
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   102
	$hostname = ereg_replace("\.$","",$hostname);
1
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   103
138
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   104
	if (strlen($hostname) > 255) {
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   105
		error(ERR_DNS_HN_TOO_LONG);
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   106
		return false;
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   107
	}
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   108
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   109
        $hostname_labels = explode ('.', $hostname);
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   110
        $label_count = count($hostname_labels);
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   111
140
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   112
	foreach ($hostname_labels as $hostname_label) {
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   113
		if ($wildcard == 1 && !isset($first)) {
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   114
			if (!preg_match('/^(\*|[\w-\/]+)$/',$hostname_label)) { error(ERR_DNS_HN_INV_CHARS); return false; }
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   115
			$first = 1;
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   116
		} else {
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   117
			if (!preg_match('/^[\w-\/]+$/',$hostname_label)) { error(ERR_DNS_HN_INV_CHARS); return false; }
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   118
		}
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   119
		if (substr($hostname_label, 0, 1) == "-") { error(ERR_DNS_HN_DASH); return false; }
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   120
		if (substr($hostname_label, -1, 1) == "-") { error(ERR_DNS_HN_DASH); return false; }
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   121
		if (strlen($hostname_label) < 1 || strlen($hostname_label) > 63) { error(ERR_DNS_HN_LENGTH); return false; }
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   122
	}
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   123
	
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   124
	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
   125
		if (substr_count($hostname_labels[0], "/") == 1) { 
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   126
			$array = explode ("/", $hostname_labels[0]);
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   127
		} else {
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   128
			$array = explode ("/", $hostname_labels[1]);
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   129
		}
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   130
		if (count($array) != 2) { error(ERR_DNS_HOSTNAME) ; return false; }
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   131
		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
   132
		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
   133
	} else {
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   134
		if (substr_count($hostname, "/") > 0) { error(ERR_DNS_HN_SLASH) ; return false; }
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   135
	}
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   136
	
138
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   137
	if ($dns_strict_tld_check == "1" && !in_array($hostname_labels[$label_count-1], $valid_tlds)) {
140
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   138
		error(ERR_DNS_INV_TLD); return false;
1
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   139
	}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   140
140
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   141
	return true;
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   142
}
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   143
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   144
function is_valid_ipv4($ipv4) {
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   145
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   146
// 20080424/RZ: The current code may be replaced by the following if() 
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   147
// statement, but it will raise the required PHP version to ">= 5.2.0". 
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   148
// Not sure if we want that now.
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   149
//
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   150
//	if(filter_var($ipv4, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) === FALSE) {
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   151
//		error(ERR_DNS_IPV4); return false;
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   152
//	}
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   153
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   154
	if (preg_match("/^[0-9\.]{7,15}$/", $ipv4)) {
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   155
		error(ERR_DNS_IPV4); return false;
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   156
	}
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   157
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   158
	$quads = explode('.', $ipv4);
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   159
	$numquads = count($quads);
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   160
	
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   161
	if ($numquads != 4) {
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   162
		error(ERR_DNS_IPV4); return false;
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   163
	}
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   164
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   165
	for ($i = 0; $i < 4; $i++) {
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   166
		if ($quads[$i] > 255) {
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   167
			error(ERR_DNS_IPV4); return false;
138
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   168
		}
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   169
	}
140
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   170
138
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   171
	return true;
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   172
}
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   173
140
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   174
function is_valid_ipv6($ipv6) {
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   175
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   176
// 20080424/RZ: The current code may be replaced by the following if() 
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   177
// statement, but it will raise the required PHP version to ">= 5.2.0". 
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   178
// Not sure if we want that now.
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   179
//
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   180
//	if(filter_var($ipv6, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) === FALSE) {
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   181
//		error(ERR_DNS_IPV6); return false;
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   182
//	}
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   183
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   184
	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
   185
		error(ERR_DNS_IPV6); return false;
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   186
	}
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   187
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   188
	$quads = explode(':', $ipv6);
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   189
	$numquads = count ($quads);
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   190
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   191
	if ($numquads > 8 || $numquads < 3) {
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   192
		error(ERR_DNS_IPV6); return false;
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   193
	}
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   194
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   195
	$emptyquads = 0;
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   196
	for ($i = 1; $i < $numquads-1; $i++) {
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   197
		if ($quads[$i] == "") $emptyquads++;
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   198
	}
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   199
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   200
	if ($emptyquads > 1) {
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   201
		error(ERR_DNS_IPV6); return false;
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   202
	}
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   203
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   204
	if ($emptyquads == 0 && $numquads != 8) {
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   205
		error(ERR_DNS_IPV6); return false;
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   206
	}
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   207
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   208
	return true;
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   209
}
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   210
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   211
function is_valid_rr_cname_name($name) {
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   212
	global $db;
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   213
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   214
	$query = "SELECT type, content 
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   215
			FROM records 
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   216
			WHERE content = " . $db->quote($name) . "
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   217
			AND (type = 'MX' OR type = 'NS')";
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   218
	
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   219
	$response = $db->query($query);
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   220
	if (PEAR::isError($response)) { error($response->getMessage()); return false; };
1
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   221
140
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   222
	if ($response->numRows() > 0) {
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   223
		error(ERR_DNS_CNAME); return false;
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   224
	}
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   225
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   226
	return true;
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   227
}
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   228
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   229
function is_valid_mx_or_ns_target($content) {
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   230
	global $db;
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   231
	
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   232
	$query = "SELECT type, name
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   233
			FROM records
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   234
			WHERE name = " . $db->quote($content) . "
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   235
			AND TYPE = 'CNAME'";
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   236
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   237
	$response = $db->query($query);
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   238
	if (PEAR::isError($response)) { error($response->getMessage()); return false; };
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   239
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   240
	if ($response->numRows() > 0) {
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   241
		error(ERR_DNS_MX_NS_TO_CNAME); return false;
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   242
	}
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   243
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   244
	return true;
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   245
}
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   246
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   247
function is_valid_rr_txt_content($content) {
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   248
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   249
	if (!preg_match("/^([^\s]{1,1000}|\"([^\"]{1,998}\"))$/i", $content)) {
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   250
		error(ERR_DNS_TXT_INV_CONTENT); return false;
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   251
	}
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   252
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   253
	return true;
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   254
}
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   255
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   256
function is_valid_rr_hinfo_content($content) {
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   257
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   258
	if ($content[0] == "\"") {
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   259
		$fields = preg_split('/(?<=") /', $content, 2);
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   260
	} else {
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   261
		$fields = preg_split('/ /', $content, 2);
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   262
	}
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   263
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   264
	for ($i = 0; ($i < 2); $i++) {
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   265
		if (!preg_match("/^([^\s]{1,1000}|\"([^\"]{1,998}\")$/i", $fields[$i])) {
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   266
			error(ERR_DNS_HINFO_INV_CONTENT); return false;
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
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   270
	return true;
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   271
}
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   272
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   273
function is_valid_rr_soa_content(&$content) {
138
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   274
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   275
	$fields = preg_split("/\s+/", trim($content));
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   276
        $field_count = count($fields);
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   277
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   278
	if ($field_count == 0 || $field_count > 7) {
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   279
		return false;
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   280
	} else {
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   281
		if (!is_valid_hostname_fqdn($fields[0]) || preg_match('/\.arpa\.?$/',$fields[0])) {
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   282
			return false;
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   283
		}
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   284
		$final_soa = $fields[0];
121
90cab8725693 [feladat @ 224]
rejo
parents: 119
diff changeset
   285
138
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   286
		if (isset($fields[1])) {
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   287
			$addr_input = $fields[1];
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   288
		} else {
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   289
			global $dns_hostmaster;
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   290
			$addr_input = $dns_hostmaster;
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   291
		}
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   292
		if (!preg_match("/@/", $addr_input)) {
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   293
			$addr_input = preg_split('/(?<!\\\)\./', $addr_input, 2);
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   294
			$addr_to_check = str_replace("\\", "", $addr_input[0]) . "@" . $addr_input[1];
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   295
		} else {
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   296
			$addr_to_check = $addr_input;
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   297
		}
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   298
		
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   299
		if (!is_valid_email($addr_to_check)) {
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   300
			return false;
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   301
		} else {
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   302
			$addr_final = explode('@', $addr_to_check, 2);
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   303
			$final_soa .= " " . str_replace(".", "\\.", $addr_final[0]) . "." . $addr_final[1];
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   304
		}
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   305
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   306
		if (isset($fields[2])) {
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   307
			if (!is_numeric($fields[2])) {
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   308
				return false;
1
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   309
			}
138
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   310
			$final_soa .= " " . $fields[2];
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   311
		} else {
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   312
			$final_soa .= " 0";
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   313
		}
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   314
		
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   315
		if ($field_count == 7) {
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   316
			for ($i = 3; ($i < 7); $i++) {
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   317
				if (!is_numeric($fields[$i])) {
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   318
					return false;
121
90cab8725693 [feladat @ 224]
rejo
parents: 119
diff changeset
   319
				} else {
138
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   320
					$final_soa .= " " . $fields[$i];
1
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   321
				}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   322
			}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   323
		}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   324
	}
138
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   325
	$content = $final_soa;
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   326
	return true;
1
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   327
}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   328
140
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   329
function is_valid_rr_soa_name($name, $zone) {
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   330
	if ($name != $zone) {
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   331
		error(ERR_DNS_SOA_NAME); return false;
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   332
	}
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   333
	return true;
1
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   334
}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   335
140
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   336
function is_valid_rr_prio(&$prio, $type) {
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   337
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   338
	if ($type == "MX" ) {
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   339
		if (!is_numeric($prio) || $prio < 0 || $prio > 65535 ) {
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   340
			error(ERR_DNS_INV_PRIO); return false;
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   341
		}
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   342
	} else {
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   343
		$prio = "";
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   344
	}
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   345
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   346
	return true;
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   347
}
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   348
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   349
function is_valid_rr_ttl(&$ttl) {
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   350
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   351
	if (!isset($ttl) || $ttl == "" ) {
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   352
		global $dns_ttl;
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   353
		$ttl = $dns_ttl;
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   354
	}
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   355
	
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   356
	if (!is_numeric($ttl) ||  $prio < 0 || $prio > 2147483647 ) {
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   357
		error(ERR_DNS_INV_TTL);	return false;
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   358
	}
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   359
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   360
	return true;
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   361
}
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   362
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   363
function is_valid_search($holygrail) {
e68c3d6094d1 [feladat @ 246]
rejo
parents: 138
diff changeset
   364
62
ce4919f37489 [feladat @ 109]
rejo
parents: 55
diff changeset
   365
	// Only allow for alphanumeric, numeric, dot, dash, underscore and 
ce4919f37489 [feladat @ 109]
rejo
parents: 55
diff changeset
   366
	// percent in search string. The last two are wildcards for SQL.
ce4919f37489 [feladat @ 109]
rejo
parents: 55
diff changeset
   367
	// Needs extension probably for more usual record types.
ce4919f37489 [feladat @ 109]
rejo
parents: 55
diff changeset
   368
ce4919f37489 [feladat @ 109]
rejo
parents: 55
diff changeset
   369
	return preg_match('/^[a-z0-9.\-%_]+$/i', $holygrail);
ce4919f37489 [feladat @ 109]
rejo
parents: 55
diff changeset
   370
}
ce4919f37489 [feladat @ 109]
rejo
parents: 55
diff changeset
   371
1
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   372
?>