* * Copyright (c) 2002 ePark Labs, Inc. * * * * License: GPL * * This program is free software; you can redistribute it and/or * * modify it under the terms of the GNU General Public License * * as published by the Free Software Foundation; either version 2 * * of the License, or (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the Free Software * * Foundation, Inc., 59 Temple Place - Suite 330, * * Boston, MA 02111-1307, USA * * * * INSTRUCTIONS: * * 1. Place this file in your web server doc tree. * * 2. Edit the User Settings below. * * 3. Make sure your PHP can understand ";" for QUERY_STRING * * delimiters. Enter: arg_separator.input = ";&" in your php.ini * * 4. Make sure the MySQL user has Select, Insert, Update, and * * Delete privileges on the records table. * * 5. Open this page (pdns.php) in your web browser and login * * with the username and password defined below. * * * * v0.1 - 31-DEC-2002 * ********************************************************************* */ ////////////////////////////////////////////////////////////////////// // USER SETTINGS (Change these for your system.) ////////////////////////////////////////////////////////////////////// // Page password define('PDNS_USER','hostmaster'); define('PDNS_PASSWORD',''); // Database connection info for the DNS data define('DNS_DB_HOST','localhost:3306'); define('DNS_DB_USER','user'); define('DNS_DB_PASSWORD','pw'); define('DNS_DB_DATABASE','pdns'); define('DNS_RECORDS_TABLE','records'); // The default value of "content" for the SOA type. (Required) // Replace example.com with your domain define('DEFAULT_SOA','ns1.example.com hostmaster@example.com 0'); // Additional default records for new domains (Optional) // Five-part array: Host Name (Without the domain), Record Type, Content, TTL, Prio // Set the TTL to 0 to use the default defined in the next section. // Replace example.com with your domain $default_record_array = array( array('host_name'=>'', 'type'=>'NS', 'content'=>'ns1.example.com', 'ttl'=>0, 'prio'=>'NULL'), array('host_name'=>'', 'type'=>'NS', 'content'=>'ns2.example.com', 'ttl'=>0, 'prio'=>'NULL'), array('host_name'=>'', 'type'=>'MX', 'content'=>'mx1.example.com', 'ttl'=>0, 'prio'=>'10'), array('host_name'=>'', 'type'=>'MX', 'content'=>'mx2.example.com', 'ttl'=>0, 'prio'=>'20'), array('host_name'=>'www', 'type'=>'A', 'content'=>'10.0.0.10', 'ttl'=>0, 'prio'=>'NULL') ); // The default TTL for all new hosts (in seconds. 3600 = 1 hour) define('DEFAULT_TTL','14400'); // Page path (The name of this file. // (Only needed if your webserver/php doesn't know about PHP_SELF) define('PDNS_PHP_PATH',$_SERVER['PHP_SELF']); ////////////////////////////////////////////////////////////////////// // END USER SETTINGS. ////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////// // System Settings $dns_record_type_array = array('A','AAAA','CNAME','HINFO','MX','NAPTR','NS','PTR','RP','SOA','TXT'); ////////////////////////////////////////////////////////////////////// // Access Control function userAuth($message) { header('WWW-Authenticate: Basic realm="PowerDNS"'); header('HTTP/1.0 401 Unauthorized'); header('Status: 401 Unauthorized'); die(htmlspecialchars($message)); } // Make sure a password is defined with a value if(PDNS_PASSWORD == '') die('You must define PDNS_PASSWORD in the PHP file.'); // Serve login form if no credentials if(empty($_SERVER['PHP_AUTH_USER'])) { userAuth('You must login to access this page.'); } // Test credentials if($_SERVER['PHP_AUTH_USER'] != PDNS_USER || $_SERVER['PHP_AUTH_PW'] != PDNS_PASSWORD) { userAuth('User Name and Password are not recognized. Reload/refresh to try again.'); } ////////////////////////////////////////////////////////////////////// // Connect to the database $db = @mysql_pconnect(DNS_DB_HOST,DNS_DB_USER,DNS_DB_PASSWORD); if(mysql_errno() != 0) { die('Could not connect to the database. ('.mysql_error().')'); } // Select the database mysql_select_db(DNS_DB_DATABASE) || die('Could not select database'); ////////////////////////////////////////////////////////////////////// // Functions // HTML Header function htmlHead($title='Untitled') { return ' '.$title.'
'; } // HTML Footer function htmlFoot() { return '
'; } // HTML Select Type function typeSelect($field_name,$field_value) { GLOBAL $dns_record_type_array; $re = ''; return $re; } ////////////////////////////////////////////////////////////////////// // Switch based on $act if(empty($act)) $act = 'list'; switch($act) { // List all the domains case 'list': // Get the domains $sql = "SELECT domain_id, name FROM ".DNS_RECORDS_TABLE." WHERE type = 'SOA' ORDER BY name"; $q_domains = mysql_query($sql,$db); // Get the latest mod dates $sql = "SELECT domain_id, max(change_date) AS max_mod FROM ".DNS_RECORDS_TABLE." GROUP BY domain_id ORDER BY domain_id"; $q_mods = mysql_query($sql,$db); $mod_array = array(); // Build the mods array (domain_id=>mod_date) while($f = mysql_fetch_object($q_mods)) { $mod_array[$f->domain_id] = date('r',$f->max_mod); } // Start HTML echo htmlHead('PDNS.PHP Domain List'); echo '

Domain List

'; echo '
'; // Output the domains while($f = mysql_fetch_object($q_domains)) { echo ''; } // Form to add a new domain echo ''; echo '
Domain Last Modified
'.$f->name.' '.$mod_array[$f->domain_id].'
'; echo htmlFoot(); break; // Insert a new domain (SOA, NS, and MX records only) case 'insert': if(empty($domain_name)) die('Application Error
The Domain Name is not defined.'); // Make sure there is no SOA for this domain already // Get the next domain_id $sql = "SELECT MAX(domain_id) FROM ".DNS_RECORDS_TABLE; $q_id = mysql_query($sql,$db); $domain_id = (int) @mysql_result($q_id,0,0) + 1; // Insert the new domain SOA record $sql = "INSERT INTO ".DNS_RECORDS_TABLE." (id,domain_id,name,type,content,ttl,prio,change_date) VALUES (0,".(int) $domain_id.",'".addslashes($domain_name)."', 'SOA','".addslashes(DEFAULT_SOA)."', ".(int) DEFAULT_TTL.",NULL,UNIX_TIMESTAMP())"; mysql_query($sql,$db); // Loop through the optional defaults records $record_count = count($default_record_array); for($i = 0; $i < $record_count; $i++) { // Set the vars for this record extract($default_record_array[$i]); // Set the FQN for hosts with a value $name = $host_name != '' ? $host_name.'.'.$domain_name : $domain_name; // Uppercase the type $type = strtoupper($type); // Set the master default TTL if this TTL is 0 $ttl = $ttl > 0 ? $ttl : DEFAULT_TTL; // If type and content are not empty, insert a record if(strlen($type) && strlen($content)) { // Insert the new domain SOA record $sql = "INSERT INTO ".DNS_RECORDS_TABLE." (id,domain_id,name,type,content,ttl,prio,change_date) VALUES (0,".(int) $domain_id.",'".addslashes($name)."', '".addslashes($type)."','".addslashes($content)."', ".(int) $ttl.",".$prio.",UNIX_TIMESTAMP())"; mysql_query($sql,$db); } } // Go edit the domain $message = urlencode('The domain '.$domain_name.' was added successfully.'); header('Location: '.PDNS_PHP_PATH.'?act=edit;domain_id='.$domain_id.';domain_name='.$domain_name.';message='.$message); exit; break; // Update a domain case 'update': // Make sure required fields exist if(empty($domain_id)) die('Application Error
Domain ID not defined.'); if(empty($domain_name)) die('Application Error
Domain Name not defined.'); if(empty($id_list)) die('Application Error
ID List not defined.'); // Clean input $domain_id = (int) $domain_id; $domain_name = htmlspecialchars($domain_name); // Report vars $update_count = 0; $insert_count = 0; $delete_count = 0; $unchanged_count = 0; // Loop through the id_list and update the records $id_array = explode(',',$id_list); foreach($id_array AS $id) { $id = (int) $id; // Define the variable vars $v_name = 'n'.$id; $v_type = 't'.$id; $v_content = 'v'.$id; $v_ttl = 'l'.$id; $v_prio = 'p'.$id; $v_hash = 'h'.$id; // Set the var values $name = $$v_name; $type = $$v_type; $content = $$v_content; $ttl = (int) $$v_ttl; $prio = $$v_prio == '' ? 'NULL' : (int) $$v_prio; $hash = $$v_hash; // Validate input // Some logic should go here to make sure the data is right for the type // and that each field has a reasonable value // Delete empty names if($id > 0 && strlen(trim($name)) == 0) { $sql = "DELETE FROM ".DNS_RECORDS_TABLE." WHERE id = ".$id; mysql_query($sql,$db); $delete_count ++; // Update the SOA change_date $sql = "UPDATE ".DNS_RECORDS_TABLE." SET change_date = UNIX_TIMESTAMP() WHERE domain_id = ".$domain_id." AND type = 'SOA'"; mysql_query($sql,$db); } // Insert new record elseif($id == 0 && strlen(trim($name)) > 0) { $host_name = $name == 'null' ? $domain_name : $name .'.'.$domain_name; // Generate the PTR info if($type == 'PTR') { $ip_array = explode('.',$content); $content = $host_name; $host_name = $ip_array[3].'.'.$ip_array[2].'.'.$ip_array[1].'.'.$ip_array[0].'.in-addr.arpa'; } $sql = "INSERT INTO ".DNS_RECORDS_TABLE." (id,domain_id,name,type,content,ttl,prio,change_date) VALUES (0,".$domain_id.",'".addslashes($host_name)."', '".addslashes($type)."','".addslashes($content)."', ".$ttl.",".$prio.",UNIX_TIMESTAMP())"; mysql_query($sql,$db); $insert_count ++; } // Save changed records elseif($id > 0 && $hash != md5($name.$type.$content.$$v_ttl.$$v_prio)) { $sql = "UPDATE ".DNS_RECORDS_TABLE." SET name = '".addslashes($name)."', type = '".addslashes($type)."', content = '".addslashes($content)."', ttl = ".$ttl.", prio = ".$prio.", change_date = UNIX_TIMESTAMP() WHERE id = ".$id; mysql_query($sql,$db); $update_count ++; } // No changes else { $unchanged_count ++; } } // Send them on to edit more $message = urlencode('Update Report: Inserted: '.$insert_count.'; Updated: '.$update_count.'; Deleted: '.$delete_count.'; Unchanged: '.$unchanged_count); header('Location: '.PDNS_PHP_PATH.'?act=edit;domain_id='.$domain_id.';domain_name='.$domain_name.';message='.$message); exit; break; // Delete a domain case 'delete': if(empty($domain_name)) die('Application Error
Domain Name not defined.'); if(empty($domain_id)) die('Application Error
The Domain ID is not defined.'); // Clean input $domain_id = (int) $domain_id; $domain_name = htmlspecialchars($domain_name); // Delete all records in the domain $sql = "DELETE FROM ".DNS_RECORDS_TABLE." WHERE domain_id = ".$domain_id ." AND name LIKE '%".addslashes($domain_name)."'"; mysql_query($sql,$db); // HTML Output echo htmlHead('phpDNS: '. $domain_name); echo '

'.strtoupper($domain_name).'

'; echo '
The domain '.strtoupper($domain_name).' was deleted.
'; echo ''; echo htmlFoot(); break; // Edit a domain case 'edit': // Verify input'.strtoupper($domain_name).''; echo ''; if(!empty($message)) echo '
'.htmlspecialchars($message).'
'; echo '
'; $id_list = 0; while($f = mysql_fetch_object($q_records)) { echo ''; $id_list .= ','.$f->id; } echo ''; echo '
Name Type Value TTL Priority Mod Date
'.typeSelect('t'.$f->id,$f->type).' '.date('r',$f->change_date).'
.'.htmlspecialchars($domain_name).' '.typeSelect('t0',htmlspecialchars($t0)).'  New Record
'; echo '
Enter values in each field. All field are required except Priority.
To create a PTR record, enter the host name in the host field and the IP address in the value field.
To enter an empty host name ('.$domain_name.'), enter "null" in the new host field.
To delete a record, clear the Name field and click Save Changes.

See PowerDNS Docs or specifically Record Type Info
'; echo htmlFoot(); break; // Unknown $act sent in default: die('Application Error
Unknown act ('.htmlspecialchars($act).').'); break; } ?>