<? /*
// File: named.sys.phps
// Purpose: named system update
// Author: Johan <hide@address.com>
*/
///////////////////////
// CONFIGURATION
// filename containing the PTR counter
$filename = "ptrcount.dat";
// Domain Update
if ($type == "domain") {
// Load current files
$sysfile['include.named'] = implode("",file($cfg['basedir']."/named/include/include.named"));
if ($cfg['dns2_system'] == 'webcp')
$sysfile['include2.named'] = implode("",file($cfg['basedir']."/web/".$cfg['dns2_file']));
// New files
$newfile['include.named'] = '# This file is automatically generated by web://cp
# Please make custom changes with the web panel
# ================================================
';
if ($cfg['dns2_system'] == 'webcp') {
$newfile['include2.named'] = '# This file is automatically generated by web://cp
# Please make custom changes with the web panel
# ================================================
';
}
// Initialize vars
unset($oldowner);
unset($newfile);
// make include.named file
$dbp = mysql_query("SELECT DISTINCT(domain) AS domain FROM domains ORDER BY domain");
while ($data = mysql_fetch_array($dbp))
$newfile['include.named'] .= "zone \"".$data['domain']."\" {\n\ttype master;\n\tfile \"".$cfg['basedir']."/named/include/".$data['domain'].".named\";\n\tallow-update { ".$cfg['allow-update']."; };\n};\n";
// make include2.named file (dns file secondary ns will fetch)
if ($cfg['dns2_system'] == 'webcp') {
$dbp = mysql_query("SELECT DISTINCT(domain) AS domain,ip FROM domains ORDER BY domain");
while ($data = mysql_fetch_array($dbp))
$newfile['include2.named'] .= "zone \"".$data['domain']."\" {\n\ttype slave;\n\tfile \"".$cfg['dns2_dir']."/".$data['domain'].".named\";\n\tmasters { ".$data['ip']."; };\n};\n\n";
}
// Make PTR (IP => domain) records
// use domain.named.tpl as a header for our PTR files, strip whats not needed
$file = $cfg['basedir']."/headers/reverse.named.tpl";
$namedconf = trim(implode("",file($file)));
// Replace variables by values
$namedconf = str_replace('{TTL}', $cfg['dns_ttl'], $namedconf);
// change @ by . in the DNS admin e-mail
$tmp = str_replace("@", ".", $cfg['dns_email']);
$namedconf = str_replace('{ADMMAIL}', $tmp, $namedconf);
// serial (counter)
if (file_exists($filename))
$tmp = trim(implode("",file($filename))) + 1;
else
$tmp = 0;
$fp = fopen($filename,"w+");
fwrite($fp,$tmp);
fclose($fp);
$namedconf = str_replace('{SERIAL}', $tmp, $namedconf);
$namedconf = str_replace('{REFRESH}', $cfg['dns_refresh'], $namedconf);
$namedconf = str_replace('{RETRY}', $cfg['dns_retry'], $namedconf);
$namedconf = str_replace('{EXPRIRE}', $cfg['dns_expire'], $namedconf);
$namedconf = str_replace('{NS1}', $cfg['dns_server1'], $namedconf);
$namedconf = str_replace('{NS2}', $cfg['dns_server2'], $namedconf);
$namedconf = str_replace('{NS3}', $cfg['dns_server3'], $namedconf);
$namedconf = str_replace('{NS4}', $cfg['dns_server4'], $namedconf);
$namedconf = str_replace('{SERVER}', $cfg['sysname'], $namedconf);
$namedconf = str_replace('{GPOS}', $cfg['dns_gpos'], $namedconf);
$namedconf = trim($namedconf)."\n\n";
// get IP list
$oldptr = '';
$ptrupdate = false;
$dbp = mysql_query("SELECT DISTINCT(ip) AS ip,nohost,host,domain FROM domains WHERE priority = 0 ORDER BY ip");
while ($ptrdata = mysql_fetch_array($dbp)) {
// Convert IP to an in-addr.arpa according to ip mask
$tmp = explode(".",$ptrdata['ip']);
if ($cfg['dns_ipmask'] == 8) {
$ptrconfig = $tmp[0].".in-addr.arpa";
$ipleft = $tmp[3].".".$tmp[2].".".$tmp[1];
}
elseif ($cfg['dns_ipmask'] == 16) {
$ptrconfig = $tmp[1].".".$tmp[0].".in-addr.arpa";
$ipleft = $tmp[2].".".$tmp[1];
}
else { // elseif ($cfg['dns_ipmask'] == 24)
$ptrconfig = $tmp[2].".".$tmp[1].".".$tmp[0].".in-addr.arpa";
$ipleft = $tmp[3];
}
if ($ptrdata['nohost'] == 'true')
$ptrline = $ipleft."\tin\tptr\t".$ptrdata['domain'].".";
else
$ptrline = $ipleft."\tin\tptr\t".$ptrdata['host'].".".$ptrdata['domain'].".";
if ($oldptr == $ptrconfig)
$tmpdata .= $ptrline."\n";
else {
if ($oldptr) {
// Write the PTR named config
$namedptr = $cfg['basedir']."/named/include/".$oldptr;
$fp = fopen($namedptr,"w+");
fwrite($fp,$namedconf.$tmpdata);
fclose($fp);
// Set permissions
chmod($namedptr,0644);
// add a line to the include.named file
$newfile['include.named'] .= "zone \"$oldptr\" {\n\ttype master;\n\tfile \"".$cfg['basedir']."/named/include/$oldptr\";\n\tallow-update { ".$cfg['allow-update']."; };\n};\n";
}
$tmpdata = $ptrline."\n";
}
$oldptr = $ptrconfig;
}
// write the last PTR config
if ($oldptr) {
$namedptr = $cfg['basedir']."/named/include/".$oldptr;
$fp = fopen($namedptr,"w+");
fwrite($fp,$namedconf.$tmpdata);
fclose($fp);
// Set permissions
chmod($namedptr,0644);
// add a line to the include.named file
$newfile['include.named'] .= "zone \"$oldptr\" {\n\ttype master;\n\tfile \"".$cfg['basedir']."/named/include/$oldptr\";\n\tallow-update { ".$cfg['allow-update']."; };\n};\n";
}
// Rewrite the include.named if it changed
if ($newfile['include.named'] != $sysfile['include.named']) {
$fp = fopen($cfg['basedir']."/named/include/include.named","w+");
fwrite($fp,$newfile['include.named']);
$restartdns = TRUE;
fclose($fp);
if ($cfg['dns2_system'] == 'webcp'){
$fp = fopen($cfg['basedir']."/web/".$cfg['dns2_file'],"w+");
fwrite($fp,$newfile['include2.named']);
fclose($fp);
}
}
}
// end of updatesys("domain");
///
?>