71
+ − 1
<?php
1
+ − 2
47
+ − 3
/* PowerAdmin, a friendly web-based admin tool for PowerDNS.
+ − 4
* See <https://rejo.zenger.nl/poweradmin> for more details.
+ − 5
*
+ − 6
* Copyright 2007, 2008 Rejo Zenger <rejo@zenger.nl>
+ − 7
*
+ − 8
* This program is free software: you can redistribute it and/or modify
+ − 9
* it under the terms of the GNU General Public License as published by
+ − 10
* the Free Software Foundation, either version 3 of the License, or
+ − 11
* (at your option) any later version.
+ − 12
*
+ − 13
* This program is distributed in the hope that it will be useful,
+ − 14
* but WITHOUT ANY WARRANTY; without even the implied warranty of
+ − 15
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ − 16
* GNU General Public License for more details.
+ − 17
*
+ − 18
* You should have received a copy of the GNU General Public License
+ − 19
* along with this program. If not, see <http://www.gnu.org/licenses/>.
+ − 20
*/
+ − 21
1
+ − 22
/*
+ − 23
* Validates an IPv4 IP.
+ − 24
* returns true if valid.
+ − 25
*/
16
+ − 26
function validate_input ( $zoneid , $type , & $content , & $name , & $prio , & $ttl )
1
+ − 27
{
+ − 28
global $db ;
+ − 29
+ − 30
// Has to validate content first then it can do the rest
+ − 31
// Since if content is invalid already it can aswell be just removed
+ − 32
// Check first if content is IPv4, IPv6 or Hostname
+ − 33
// We accomplish this by just running all tests over it
+ − 34
// We start with IPv6 since its not able to have these ip's in domains.
+ − 35
//
+ − 36
// <TODO>
+ − 37
// The nocheck has to move to the configuration file
+ − 38
// </TODO>
+ − 39
//
+ − 40
$domain = get_domain_name_from_id ( $zoneid );
+ − 41
$nocheck = array ( 'SOA' , 'HINFO' , 'NAPTR' , 'URL' , 'MBOXFW' , 'TXT' );
+ − 42
$hostname = false ;
+ − 43
$ip4 = false ;
+ − 44
$ip6 = false ;
+ − 45
+ − 46
if ( ! in_array ( strtoupper ( $type ), $nocheck ))
+ − 47
{
+ − 48
+ − 49
if ( ! is_valid_ip6 ( $content ))
+ − 50
{
+ − 51
if ( ! is_valid_ip ( $content ))
+ − 52
{
+ − 53
if ( ! is_valid_hostname ( $content ))
+ − 54
{
+ − 55
error ( ERR_DNS_CONTENT );
+ − 56
}
+ − 57
else
+ − 58
{
+ − 59
$hostname = true ;
+ − 60
}
+ − 61
}
+ − 62
else
+ − 63
{
+ − 64
$ip4 = true ;
+ − 65
}
+ − 66
}
+ − 67
else
+ − 68
{
+ − 69
$ip6 = true ;
+ − 70
}
+ − 71
}
+ − 72
+ − 73
// Prepare total hostname.
+ − 74
+ − 75
if ( $name == '*' )
+ − 76
{
+ − 77
$wildcard = true ;
79
+ − 78
} else {
+ − 79
$wildcard = false ;
1
+ − 80
}
+ − 81
+ − 82
if ( $name == "0" ) {
+ − 83
$name = $name . "." . $domain ;
+ − 84
} else {
+ − 85
$name = ( $name ) ? $name . "." . $domain : $domain ;
+ − 86
}
+ − 87
+ − 88
if ( preg_match ( '!@\.!i' , $name ))
+ − 89
{
+ − 90
$name = str_replace ( '@.' , '@' , $name );
+ − 91
}
+ − 92
+ − 93
if ( ! $wildcard )
+ − 94
{
+ − 95
if ( ! is_valid_hostname ( $name ))
+ − 96
{
+ − 97
error ( ERR_DNS_HOSTNAME );
+ − 98
}
+ − 99
}
+ − 100
+ − 101
// Check record type (if it exists in our allowed list.
+ − 102
if ( ! in_array ( strtoupper ( $type ), get_record_types ()))
+ − 103
{
+ − 104
error ( ERR_DNS_RECORDTYPE );
+ − 105
}
+ − 106
+ − 107
// Start handling the demands for the functions.
+ − 108
// Validation for IN A records. Can only have an IP. Nothing else.
+ − 109
if ( $type == 'A' && ! $ip4 )
+ − 110
{
+ − 111
error ( ERR_DNS_IPV4 );
+ − 112
}
+ − 113
+ − 114
if ( $type == 'AAAA' && ! $ip6 )
+ − 115
{
+ − 116
error ( ERR_DNS_IPV6 );
+ − 117
}
+ − 118
+ − 119
if ( $type == 'CNAME' && $hostname )
+ − 120
{
+ − 121
if ( ! is_valid_cname ( $name ))
+ − 122
{
+ − 123
error ( ERR_DNS_CNAME );
+ − 124
}
+ − 125
}
+ − 126
+ − 127
if ( $type == 'NS' )
+ − 128
{
+ − 129
$status = is_valid_ns ( $content , $hostname );
+ − 130
if ( $status == - 1 )
+ − 131
{
+ − 132
error ( ERR_DNS_NS_HNAME );
+ − 133
}
+ − 134
elseif ( $status == - 2 )
+ − 135
{
+ − 136
error ( ERR_DNS_NS_CNAME );
+ − 137
}
+ − 138
// Otherwise its ok
+ − 139
}
+ − 140
+ − 141
if ( $type == 'SOA' )
+ − 142
{
+ − 143
$status = is_valid_soa ( $content , $zoneid );
+ − 144
if ( $status == - 1 )
+ − 145
{
+ − 146
error ( ERR_DNS_SOA_UNIQUE );
+ − 147
// Make nicer error
+ − 148
}
+ − 149
elseif ( $status == - 2 )
+ − 150
{
+ − 151
error ( ERR_DNS_SOA_NUMERIC );
+ − 152
}
+ − 153
}
+ − 154
+ − 155
// HINFO and TXT require no validation.
+ − 156
+ − 157
if ( $type == 'URL' )
+ − 158
{
+ − 159
if ( ! is_valid_url ( $content ))
+ − 160
{
+ − 161
error ( ERR_INV_URL );
+ − 162
}
+ − 163
}
+ − 164
if ( $type == 'MBOXFW' )
+ − 165
{
+ − 166
if ( ! is_valid_mboxfw ( $content ))
+ − 167
{
+ − 168
error ( ERR_INV_EMAIL );
+ − 169
}
+ − 170
}
+ − 171
+ − 172
// NAPTR has to be done.
+ − 173
// Do we want that?
+ − 174
// http://www.ietf.org/rfc/rfc2915.txt
+ − 175
// http://www.zvon.org/tmRFC/RFC2915/Output/chapter2.html
+ − 176
// http://www.zvon.org/tmRFC/RFC3403/Output/chapter4.html
+ − 177
+ − 178
// See if the prio field is valid and if we have one.
+ − 179
// If we dont have one and the type is MX record, give it value '10'
+ − 180
if ( $type == 'NAPTR' )
+ − 181
{
+ − 182
+ − 183
}
+ − 184
+ − 185
if ( $type == 'MX' )
+ − 186
{
+ − 187
if ( $hostname )
+ − 188
{
+ − 189
$status = is_valid_mx ( $content , $prio );
+ − 190
if ( $status == - 1 )
+ − 191
{
+ − 192
error ( ERR_DNS_MX_CNAME );
+ − 193
}
+ − 194
elseif ( $status == - 2 )
+ − 195
{
+ − 196
error ( ERR_DNS_MX_PRIO );
+ − 197
}
+ − 198
}
+ − 199
else
+ − 200
{
6
+ − 201
error ( _ ( 'If you specify an MX record it must be a hostname.' ) );
1
+ − 202
}
+ − 203
}
+ − 204
else
+ − 205
{
55
+ − 206
$prio = 0 ;
1
+ − 207
}
+ − 208
// Validate the TTL, it has to be numeric.
+ − 209
$ttl = ( ! isset ( $ttl ) || ! is_numeric ( $ttl )) ? $DEFAULT_TTL : $ttl ;
+ − 210
}
+ − 211
+ − 212
+ − 213
+ − 214
/****************************************
+ − 215
* *
+ − 216
* RECORD VALIDATING PART. *
+ − 217
* CHANGES HERE SHOULD BE CONSIDERED *
+ − 218
* THEY REQUIRE KNOWLEDGE ABOUT THE *
+ − 219
* DNS SPECIFICATIONS *
+ − 220
* *
+ − 221
***************************************/
+ − 222
+ − 223
+ − 224
/*
+ − 225
* Validatis a CNAME record by the name it will have and its destination
+ − 226
*
+ − 227
*/
+ − 228
function is_valid_cname ( $dest )
+ − 229
{
+ − 230
/*
+ − 231
* This is really EVIL.
+ − 232
* If the new record (a CNAME) record is being pointed to by a MX record or NS record we have to bork.
+ − 233
* this is the idea.
+ − 234
*
+ − 235
* MX record: blaat.nl MX mail.blaat.nl
+ − 236
* Now we look what mail.blaat.nl is
+ − 237
* We discover the following:
+ − 238
* mail.blaat.nl CNAME bork.blaat.nl
+ − 239
* This is NOT allowed! mail.onthanet.nl can not be a CNAME!
+ − 240
* The same goes for NS. mail.blaat.nl must have a normal IN A record.
+ − 241
* It MAY point to a CNAME record but its not wished. Lets not support it.
+ − 242
*/
+ − 243
+ − 244
global $db ;
+ − 245
+ − 246
// Check if there are other records with this information of the following types.
+ − 247
// P.S. we might add CNAME to block CNAME recursion and chains.
+ − 248
$blockedtypes = " AND (type='MX' OR type='NS')" ;
+ − 249
65
+ − 250
$cnamec = "SELECT type, content FROM records WHERE content=" . $db -> quote ( $dest ) . $blockedtypes ;
1
+ − 251
$result = $db -> query ( $cnamec );
+ − 252
+ − 253
if ( $result -> numRows () > 0 )
+ − 254
{
+ − 255
return false ;
+ − 256
// Lets inform the user he is doing something EVIL.
+ − 257
// Ok we found a record that has our content field in their content field.
+ − 258
}
+ − 259
return true ;
+ − 260
}
+ − 261
+ − 262
+ − 263
/*
+ − 264
* Checks if something is a valid domain.
+ − 265
* Checks for domainname with the allowed characters <a,b,...z,A,B,...Z> and - and _.
+ − 266
* This part must be followed by a 2 to 4 character TLD.
+ − 267
*/
+ − 268
function is_valid_domain ( $domain )
+ − 269
{
+ − 270
if (( eregi ( "^[0-9a-z]([-.]?[0-9a-z])* \\ .[a-z]{2,4}$" , $domain )) && ( strlen ( $domain ) <= 128 ))
+ − 271
{
+ − 272
return true ;
+ − 273
}
+ − 274
return false ;
+ − 275
}
+ − 276
+ − 277
+ − 278
/*
+ − 279
* Validates if given hostname is allowed.
+ − 280
* returns true if allowed.
+ − 281
*/
+ − 282
function is_valid_hostname ( $host )
+ − 283
{
+ − 284
if ( count ( explode ( "." , $host )) == 1 )
+ − 285
{
+ − 286
return false ;
+ − 287
}
+ − 288
+ − 289
// Its not perfect (in_addr.int is allowed) but works for now.
+ − 290
+ − 291
if ( preg_match ( '!(ip6|in-addr).(arpa|int)$!i' , $host ))
+ − 292
{
+ − 293
if ( preg_match ( '!^(([A-Z\d]|[A-Z\d][A-Z\d-]*[A-Z\d])\.)*[A-Z\d]+$!i' , $host ))
+ − 294
{
+ − 295
return true ;
+ − 296
}
+ − 297
return false ;
+ − 298
}
+ − 299
+ − 300
// Validate further.
+ − 301
return ( preg_match ( '!^(([A-Z\d]|[A-Z\d][A-Z\d-]*[A-Z\d])\.)*[A-Z\d]+$!i' , $host )) ? true : false ;
+ − 302
}
+ − 303
+ − 304
+ − 305
/*
+ − 306
* Validates an IPv4 IP.
+ − 307
* returns true if valid.
+ − 308
*/
+ − 309
function is_valid_ip ( $ip )
+ − 310
{
+ − 311
// Stop reading at this point. Scroll down to the next function...
+ − 312
// Ok... you didn't stop reading... now you have to rewrite the whole function! enjoy ;-)
+ − 313
// Trance unborked it. Twice even!
+ − 314
return ( $ip == long2ip ( ip2long ( $ip ))) ? true : false ;
+ − 315
+ − 316
}
+ − 317
+ − 318
+ − 319
/*
+ − 320
* Validates an IPv6 IP.
+ − 321
* returns true if valid.
+ − 322
*/
+ − 323
function is_valid_ip6 ( $ip )
+ − 324
{
+ − 325
// Validates if the given IP is truly an IPv6 address.
+ − 326
// Precondition: have a string
+ − 327
// Postcondition: false: Error in IP
+ − 328
// true: IP is correct
+ − 329
// Requires: String
+ − 330
// Date: 10-sep-2002
+ − 331
if ( preg_match ( '!^[A-F0-9:]{1,39}$!i' , $ip ) == true )
+ − 332
{
+ − 333
// Not 3 ":" or more.
+ − 334
$p = explode ( ':::' , $ip );
+ − 335
if ( sizeof ( $p ) > 1 )
+ − 336
{
+ − 337
return false ;
+ − 338
}
+ − 339
// Find if there is only one occurence of "::".
+ − 340
$p = explode ( '::' , $ip );
+ − 341
if ( sizeof ( $p ) > 2 )
+ − 342
{
+ − 343
return false ;
+ − 344
}
+ − 345
// Not more than 8 octects
+ − 346
$p = explode ( ':' , $ip );
+ − 347
+ − 348
if ( sizeof ( $p ) > 8 )
+ − 349
{
+ − 350
return false ;
+ − 351
}
+ − 352
+ − 353
// Check octet length
+ − 354
foreach ( $p as $checkPart )
+ − 355
{
+ − 356
if ( strlen ( $checkPart ) > 4 )
+ − 357
{
+ − 358
return false ;
+ − 359
}
+ − 360
}
+ − 361
return true ;
+ − 362
}
+ − 363
return false ;
+ − 364
}
+ − 365
+ − 366
+ − 367
/*
+ − 368
* FANCY RECORD.
+ − 369
* Validates if the fancy record mboxfw is an actual email address.
+ − 370
*/
+ − 371
function is_valid_mboxfw ( $email )
+ − 372
{
+ − 373
return is_valid_email ( $email );
+ − 374
}
+ − 375
+ − 376
+ − 377
/*
+ − 378
* Validates MX records.
+ − 379
* an MX record cant point to a CNAME record. This has to be checked.
+ − 380
* this function also sets a proper priority.
+ − 381
*/
+ − 382
function is_valid_mx ( $content , & $prio )
+ − 383
{
+ − 384
global $db ;
+ − 385
// See if the destination to which this MX is pointing is NOT a CNAME record.
+ − 386
// Check inside our dns server.
65
+ − 387
if ( $db -> queryOne ( "SELECT count(id) FROM records WHERE name=" . $db -> quote ( $content ) . " AND type='CNAME'" ) > 0 )
1
+ − 388
{
+ − 389
return - 1 ;
+ − 390
}
+ − 391
else
+ − 392
{
+ − 393
// Fix the proper priority for the record.
+ − 394
// Bugfix, thanks Oscar :)
+ − 395
if ( ! isset ( $prio ))
+ − 396
{
+ − 397
$prio = 10 ;
+ − 398
}
+ − 399
if ( ! is_numeric ( $prio ))
+ − 400
{
+ − 401
if ( $prio == '' )
+ − 402
{
+ − 403
$prio = 10 ;
+ − 404
}
+ − 405
else
+ − 406
{
+ − 407
return - 2 ;
+ − 408
}
+ − 409
}
+ − 410
}
+ − 411
return 1 ;
+ − 412
}
+ − 413
+ − 414
/*
+ − 415
* Validates NS records.
+ − 416
* an NS record cant point to a CNAME record. This has to be checked.
+ − 417
* $hostname directive means if its a hostname or not (this to avoid that NS records get ip fields)
+ − 418
* NS must have a hostname, it is not allowed to have an IP.
+ − 419
*/
+ − 420
function is_valid_ns ( $content , $hostname )
+ − 421
{
+ − 422
global $db ;
+ − 423
// Check if the field is a hostname, it MUST be a hostname.
+ − 424
if ( ! $hostname )
+ − 425
{
+ − 426
return - 1 ;
+ − 427
// "an IN NS field must be a hostname."
+ − 428
}
+ − 429
65
+ − 430
if ( $db -> queryOne ( "SELECT count(id) FROM records WHERE name=" . $db -> quote ( $content ) . " AND type='CNAME'" ) > 0 )
1
+ − 431
{
+ − 432
return - 2 ;
+ − 433
// "You can not point a NS record to a CNAME record. Remove/rename the CNAME record first or take another name."
+ − 434
+ − 435
}
+ − 436
return 1 ;
+ − 437
}
+ − 438
+ − 439
+ − 440
/*
+ − 441
* Function to check the validity of SOA records.
+ − 442
* return values: true if succesful
+ − 443
*/
+ − 444
function is_valid_soa ( & $content , $zoneid )
+ − 445
{
+ − 446
+ − 447
/*
+ − 448
* SOA (start of authority)
+ − 449
* there is only _ONE_ SOA record allowed in every zone.
+ − 450
* Validate SOA record
+ − 451
* The Start of Authority record is one of the most complex available. It specifies a lot
+ − 452
* about a domain: the name of the master nameserver ('the primary'), the hostmaster and
+ − 453
* a set of numbers indicating how the data in this domain expires and how often it needs
+ − 454
* to be checked. Further more, it contains a serial number which should rise on each change
+ − 455
* of the domain.
+ − 456
2002120902 28800 7200 604800 10800
+ − 457
* The stored format is: primary hostmaster serial refresh retry expire default_ttl
+ − 458
* From the powerdns documentation.
+ − 459
*/
+ − 460
+ − 461
+ − 462
// Check if there already is an occurence of a SOA, if so see if its not the one we are currently changing
+ − 463
$return = get_records_by_type_from_domid ( "SOA" , $zoneid );
+ − 464
if ( $return -> numRows () > 1 )
+ − 465
{
+ − 466
return - 1 ;
+ − 467
}
+ − 468
+ − 469
+ − 470
$soacontent = explode ( " " , $content );
+ − 471
// Field is at least one otherwise it wouldnt even get here.
+ − 472
if ( is_valid_hostname ( $soacontent [ 0 ]))
+ − 473
{
+ − 474
$totalsoa = $soacontent [ 0 ];
+ − 475
// It doesnt matter what field 2 contains, but lets check if its there
+ − 476
// We assume the 2nd field wont have numbers, otherwise its a TTL field
+ − 477
if ( count ( $soacontent ) > 1 )
+ − 478
{
+ − 479
if ( is_numeric ( $soacontent [ 1 ]))
+ − 480
{
+ − 481
// its a TTL field, or at least not hostmaster or alike
+ − 482
// Set final string to the default hostmaster addy
+ − 483
global $HOSTMASTER ;
+ − 484
$totalsoa .= " " . $HOSTMASTER ;
+ − 485
}
+ − 486
else
+ − 487
{
+ − 488
$totalsoa .= " " . $soacontent [ 1 ];
+ − 489
}
+ − 490
// For loop to iterate over the numbers
+ − 491
$imax = count ( $soacontent );
+ − 492
for ( $i = 2 ; ( $i < $imax ) && ( $i < 7 ); $i ++ )
+ − 493
{
+ − 494
if ( ! is_numeric ( $soacontent [ $i ]))
+ − 495
{
+ − 496
return - 2 ;
+ − 497
}
+ − 498
else
+ − 499
{
+ − 500
$totalsoa .= " " . $soacontent [ $i ];
+ − 501
}
+ − 502
}
+ − 503
if ( $i > 7 )
+ − 504
{
+ − 505
error ( ERR_DNS_SOA_NUMERIC_FIELDS );
+ − 506
}
+ − 507
}
+ − 508
}
+ − 509
else
+ − 510
{
+ − 511
error ( ERR_DNS_SOA_HOSTNAME );
+ − 512
}
+ − 513
$content = $totalsoa ;
+ − 514
return 1 ;
+ − 515
}
+ − 516
+ − 517
+ − 518
function is_valid_url ( $url )
+ − 519
{
+ − 520
return preg_match ( '!^(http://)(([A-Z\d]|[A-Z\d][A-Z\d-]*[A-Z\d])\.)*[A-Z\d]+([//]([0-9a-z//~#%&\'_\-+=:?.]*))?$!i' , $url );
+ − 521
}
+ − 522
62
+ − 523
function is_valid_search ( $holygrail )
+ − 524
{
+ − 525
// Only allow for alphanumeric, numeric, dot, dash, underscore and
+ − 526
// percent in search string. The last two are wildcards for SQL.
+ − 527
// Needs extension probably for more usual record types.
+ − 528
+ − 529
return preg_match ( '/^[a-z0-9.\-%_]+$/i' , $holygrail );
+ − 530
}
+ − 531
+ − 532
1
+ − 533
?>