create a dns-config for the froxlor-hostname, fixes #1090

Signed-off-by: Michael Kaufmann (d00p) <d00p@froxlor.org>
This commit is contained in:
Michael Kaufmann (d00p)
2014-03-11 10:18:23 +01:00
parent 08ce6be3ff
commit 31fa23a388

View File

@@ -81,13 +81,63 @@ class bind {
WHERE `d`.`isbinddomain` = '1' ORDER BY `d`.`domain` ASC WHERE `d`.`isbinddomain` = '1' ORDER BY `d`.`domain` ASC
"); ");
// customer-domains
while ($domain = $result_domains_stmt->fetch(PDO::FETCH_ASSOC)) { while ($domain = $result_domains_stmt->fetch(PDO::FETCH_ASSOC)) {
$bindconf_file .= $this->_generateDomainConfig($domain);
}
// frolxor-hostname (#1090)
$hostname_arr = array(
'id' => 'none',
'domain' => Settings::Get('system.hostname'),
'customerid' => 'none',
'loginname' => 'froxlor.panel',
'bindserial' => date('Ymd').'00',
'dkim' => '0',
'iswildcarddomain' => '1',
'zonefile' => ''
);
$bindconf_file .= $this->_generateDomainConfig($hostname_arr, true);
$bindconf_file_handler = fopen(makeCorrectFile(Settings::Get('system.bindconf_directory') . '/froxlor_bind.conf'), 'w');
fwrite($bindconf_file_handler, $bindconf_file);
fclose($bindconf_file_handler);
fwrite($this->debugHandler, ' cron_tasks: Task4 - froxlor_bind.conf written' . "\n");
$this->logger->logAction(CRON_ACTION, LOG_INFO, 'froxlor_bind.conf written');
safe_exec(escapeshellcmd(Settings::Get('system.bindreload_command')));
fwrite($this->debugHandler, ' cron_tasks: Task4 - Bind9 reloaded' . "\n");
$this->logger->logAction(CRON_ACTION, LOG_INFO, 'Bind9 reloaded');
$domains_dir = makeCorrectDir(Settings::Get('system.bindconf_directory') . '/domains/');
if (file_exists($domains_dir)
&& is_dir($domains_dir)) {
$domain_file_dirhandle = opendir($domains_dir);
while (false !== ($domain_filename = readdir($domain_file_dirhandle))) {
$full_filename = makeCorrectFile($domains_dir . '/' . $domain_filename);
if ($domain_filename != '.'
&& $domain_filename != '..'
&& !in_array($domain_filename, $known_filenames)
&& is_file($full_filename)
&& file_exists($full_filename)) {
fwrite($this->debugHandler, ' cron_tasks: Task4 - unlinking ' . $domain_filename . "\n");
$this->logger->logAction(CRON_ACTION, LOG_WARNING, 'Deleting ' . $domain_filename);
unlink(makeCorrectFile($domains_dir . '/' . $domain_filename));
}
}
}
}
private function _generateDomainConfig($domain = array(), $froxlorhost = false) {
$bindconf_file = '';
fwrite($this->debugHandler, ' cron_tasks: Task4 - Writing ' . $domain['id'] . '::' . $domain['domain'] . "\n"); fwrite($this->debugHandler, ' cron_tasks: Task4 - Writing ' . $domain['id'] . '::' . $domain['domain'] . "\n");
$this->logger->logAction(CRON_ACTION, LOG_INFO, 'Writing ' . $domain['id'] . '::' . $domain['domain']); $this->logger->logAction(CRON_ACTION, LOG_INFO, 'Writing ' . $domain['id'] . '::' . $domain['domain']);
if ($domain['zonefile'] == '') { if ($domain['zonefile'] == '') {
$zonefile = $this->generateZone($domain); $zonefile = $this->generateZone($domain, $froxlorhost);
$domain['zonefile'] = 'domains/' . $domain['domain'] . '.zone'; $domain['zonefile'] = 'domains/' . $domain['domain'] . '.zone';
$zonefile_name = makeCorrectFile(Settings::Get('system.bindconf_directory') . '/' . $domain['zonefile']); $zonefile_name = makeCorrectFile(Settings::Get('system.bindconf_directory') . '/' . $domain['zonefile']);
$known_filenames[] = basename($zonefile_name); $known_filenames[] = basename($zonefile_name);
@@ -128,52 +178,39 @@ class bind {
$bindconf_file.= '};' . "\n"; $bindconf_file.= '};' . "\n";
$bindconf_file.= "\n"; $bindconf_file.= "\n";
return $bindconf_file;
} }
$bindconf_file_handler = fopen(makeCorrectFile(Settings::Get('system.bindconf_directory') . '/froxlor_bind.conf'), 'w'); /**
fwrite($bindconf_file_handler, $bindconf_file); * generate bind zone content. If froxlorhost is true,
fclose($bindconf_file_handler); * we will use ALL available IP addresses
fwrite($this->debugHandler, ' cron_tasks: Task4 - froxlor_bind.conf written' . "\n"); *
$this->logger->logAction(CRON_ACTION, LOG_INFO, 'froxlor_bind.conf written'); * @param array $domain
safe_exec(escapeshellcmd(Settings::Get('system.bindreload_command'))); * @param boolean $froxlorhost
fwrite($this->debugHandler, ' cron_tasks: Task4 - Bind9 reloaded' . "\n"); *
$this->logger->logAction(CRON_ACTION, LOG_INFO, 'Bind9 reloaded'); * @return string
$domains_dir = makeCorrectDir(Settings::Get('system.bindconf_directory') . '/domains/'); */
protected function generateZone($domain, $froxlorhost = false) {
if (file_exists($domains_dir)
&& is_dir($domains_dir)) {
$domain_file_dirhandle = opendir($domains_dir);
while (false !== ($domain_filename = readdir($domain_file_dirhandle))) {
$full_filename = makeCorrectFile($domains_dir . '/' . $domain_filename);
if ($domain_filename != '.'
&& $domain_filename != '..'
&& !in_array($domain_filename, $known_filenames)
&& is_file($full_filename)
&& file_exists($full_filename)) {
fwrite($this->debugHandler, ' cron_tasks: Task4 - unlinking ' . $domain_filename . "\n");
$this->logger->logAction(CRON_ACTION, LOG_WARNING, 'Deleting ' . $domain_filename);
unlink(makeCorrectFile($domains_dir . '/' . $domain_filename));
}
}
}
}
protected function generateZone($domain) {
// Array to save all ips needed in the records (already including IN A/AAAA) // Array to save all ips needed in the records (already including IN A/AAAA)
$ip_a_records = array(); $ip_a_records = array();
// Array to save DNS records // Array to save DNS records
$records = array(); $records = array();
$domainidquery = '';
$query_params = array();
if (!$froxlorhost) {
$domainidquery = "`di`.`id_domain` = :domainid AND ";
$query_params['domainid'] = $domain['id'];
}
$result_ip_stmt = Database::prepare(" $result_ip_stmt = Database::prepare("
SELECT `p`.`ip` AS `ip` SELECT `p`.`ip` AS `ip`
FROM `".TABLE_PANEL_IPSANDPORTS."` `p`, `".TABLE_DOMAINTOIP."` `di` FROM `".TABLE_PANEL_IPSANDPORTS."` `p`, `".TABLE_DOMAINTOIP."` `di`
WHERE `di`.`id_domain` = :domainid AND `p`.`id` = `di`.`id_ipandports` WHERE ".$domainidquery."`p`.`id` = `di`.`id_ipandports`
GROUP BY `p`.`ip`; GROUP BY `p`.`ip`;
"); ");
Database::pexecute($result_ip_stmt, array('domainid' => $domain['id'])); Database::pexecute($result_ip_stmt, $query_params);
while ($ip = $result_ip_stmt->fetch(PDO::FETCH_ASSOC)) { while ($ip = $result_ip_stmt->fetch(PDO::FETCH_ASSOC)) {
@@ -191,12 +228,14 @@ class bind {
$date = date('Ymd'); $date = date('Ymd');
$bindserial = (preg_match('/^' . $date . '/', $domain['bindserial']) ? $domain['bindserial'] + 1 : $date . '00'); $bindserial = (preg_match('/^' . $date . '/', $domain['bindserial']) ? $domain['bindserial'] + 1 : $date . '00');
if (!$froxlorhost) {
$upd_stmt = Database::prepare(" $upd_stmt = Database::prepare("
UPDATE `" . TABLE_PANEL_DOMAINS . "` SET UPDATE `" . TABLE_PANEL_DOMAINS . "` SET
`bindserial` = :serial `bindserial` = :serial
WHERE `id` = :id WHERE `id` = :id
"); ");
Database::pexecute($upd_stmt, array('serial' => $bindserial, 'id' => $domain['id'])); Database::pexecute($upd_stmt, array('serial' => $bindserial, 'id' => $domain['id']));
}
$zonefile = '$TTL ' . (int)Settings::Get('system.defaultttl') . "\n"; $zonefile = '$TTL ' . (int)Settings::Get('system.defaultttl') . "\n";
if (count($this->nameservers) == 0) { if (count($this->nameservers) == 0) {
@@ -259,6 +298,7 @@ class bind {
*/ */
$zonefile.= $this->generateDkim($domain); $zonefile.= $this->generateDkim($domain);
if (!$froxlorhost) {
$nssubdomains_stmt = Database::prepare(" $nssubdomains_stmt = Database::prepare("
SELECT `domain` FROM `" . TABLE_PANEL_DOMAINS . "` SELECT `domain` FROM `" . TABLE_PANEL_DOMAINS . "`
WHERE `isbinddomain` = '1' AND `domain` LIKE :domain WHERE `isbinddomain` = '1' AND `domain` LIKE :domain
@@ -280,6 +320,7 @@ class bind {
} }
} }
} }
}
$records[] = '@'; $records[] = '@';
$records[] = 'www'; $records[] = 'www';
@@ -288,6 +329,7 @@ class bind {
$records[] = '*'; $records[] = '*';
} }
if (!$froxlorhost) {
$subdomains_stmt = Database::prepare(" $subdomains_stmt = Database::prepare("
SELECT `domain` FROM `".TABLE_PANEL_DOMAINS."` SELECT `domain` FROM `".TABLE_PANEL_DOMAINS."`
WHERE `parentdomainid` = :domainid WHERE `parentdomainid` = :domainid
@@ -304,6 +346,7 @@ class bind {
$records[] = 'www.'.str_replace('.' . $domain['domain'], '', $subdomain['domain']); $records[] = 'www.'.str_replace('.' . $domain['domain'], '', $subdomain['domain']);
} }
} }
}
// Create DNS-Records for every name we have saved // Create DNS-Records for every name we have saved
foreach ($records as $record) { foreach ($records as $record) {
@@ -472,6 +515,4 @@ class bind {
$this->logger->logAction(CRON_ACTION, LOG_INFO, 'Dkim-milter reloaded'); $this->logger->logAction(CRON_ACTION, LOG_INFO, 'Dkim-milter reloaded');
} }
} }
} }