Merge pull request #282 from nachtgeist/cron_tasks_bind
cron tasks: bind: refactor zone creation
This commit is contained in:
@@ -25,6 +25,7 @@ class bind {
|
|||||||
public $axfrservers = array();
|
public $axfrservers = array();
|
||||||
|
|
||||||
private $_known_filenames = array();
|
private $_known_filenames = array();
|
||||||
|
private $_bindconf_file = '';
|
||||||
|
|
||||||
public function __construct($logger, $debugHandler) {
|
public function __construct($logger, $debugHandler) {
|
||||||
|
|
||||||
@@ -76,19 +77,21 @@ class bind {
|
|||||||
|
|
||||||
$this->_known_filenames = array();
|
$this->_known_filenames = array();
|
||||||
|
|
||||||
$bindconf_file = '# ' . Settings::Get('system.bindconf_directory') . 'froxlor_bind.conf' . "\n" .
|
$this->_bindconf_file = '# ' . Settings::Get('system.bindconf_directory') . 'froxlor_bind.conf' . "\n" .
|
||||||
'# Created ' . date('d.m.Y H:i') . "\n" .
|
'# Created ' . date('d.m.Y H:i') . "\n" .
|
||||||
'# Do NOT manually edit this file, all changes will be deleted after the next domain change at the panel.' . "\n" . "\n";
|
'# Do NOT manually edit this file, all changes will be deleted after the next domain change at the panel.' . "\n" . "\n";
|
||||||
$result_domains_stmt = Database::query("
|
$result_domains_stmt = Database::query("
|
||||||
SELECT `d`.`id`, `d`.`domain`, `d`.`isemaildomain`, `d`.`iswildcarddomain`, `d`.`wwwserveralias`, `d`.`customerid`,
|
SELECT `d`.`id`, `d`.`domain`, `d`.`isemaildomain`, `d`.`iswildcarddomain`, `d`.`wwwserveralias`, `d`.`customerid`,
|
||||||
`d`.`zonefile`, `d`.`bindserial`, `d`.`dkim`, `d`.`dkim_id`, `d`.`dkim_pubkey`, `c`.`loginname`, `c`.`guid`
|
`d`.`zonefile`, `d`.`bindserial`, `d`.`dkim`, `d`.`dkim_id`, `d`.`dkim_pubkey`, `d`.`ismainbutsubto`,
|
||||||
|
`c`.`loginname`, `c`.`guid`
|
||||||
FROM `" . TABLE_PANEL_DOMAINS . "` `d` LEFT JOIN `" . TABLE_PANEL_CUSTOMERS . "` `c` USING(`customerid`)
|
FROM `" . TABLE_PANEL_DOMAINS . "` `d` LEFT JOIN `" . TABLE_PANEL_CUSTOMERS . "` `c` USING(`customerid`)
|
||||||
WHERE `d`.`isbinddomain` = '1' ORDER BY `d`.`domain` ASC
|
WHERE `d`.`isbinddomain` = '1' ORDER BY `d`.`domain` ASC
|
||||||
");
|
");
|
||||||
|
|
||||||
// customer-domains
|
// don't use fetchall() to be able to set the first column to the domain id and use it later on to set the rows'
|
||||||
|
// array of direct children without having to search the outer array
|
||||||
while ($domain = $result_domains_stmt->fetch(PDO::FETCH_ASSOC)) {
|
while ($domain = $result_domains_stmt->fetch(PDO::FETCH_ASSOC)) {
|
||||||
$bindconf_file .= $this->_generateDomainConfig($domain);
|
$domains[$domain["id"]] = $domain;
|
||||||
}
|
}
|
||||||
|
|
||||||
// frolxor-hostname (#1090)
|
// frolxor-hostname (#1090)
|
||||||
@@ -102,13 +105,59 @@ class bind {
|
|||||||
'bindserial' => date('Ymd').'00',
|
'bindserial' => date('Ymd').'00',
|
||||||
'dkim' => '0',
|
'dkim' => '0',
|
||||||
'iswildcarddomain' => '1',
|
'iswildcarddomain' => '1',
|
||||||
'zonefile' => ''
|
'ismainbutsubto' => '0',
|
||||||
|
'zonefile' => '',
|
||||||
|
'froxlorhost' => '1'
|
||||||
);
|
);
|
||||||
$bindconf_file .= $this->_generateDomainConfig($hostname_arr, true);
|
$domains['none'] = $hostname_arr;
|
||||||
|
}
|
||||||
|
|
||||||
|
// collect domain IDs of direct child domains as arrays in ['children'] column
|
||||||
|
foreach (array_keys($domains) as $key) {
|
||||||
|
if (!isset($domains[$key]['children'])) {
|
||||||
|
$domains[$key]['children'] = array();
|
||||||
|
}
|
||||||
|
if ($domains[$key]['ismainbutsubto'] > 0) {
|
||||||
|
if (isset($domains[ $domains[$key]['ismainbutsubto'] ])) {
|
||||||
|
$domains[ $domains[$key]['ismainbutsubto'] ]['children'][] = $domains[$key]['id'];
|
||||||
|
} else {
|
||||||
|
$this->logger->logAction(CRON_ACTION, LOG_ERR,
|
||||||
|
'Database inconsistency: domain ' . $domain['domain'] . ' (ID #' . $key .
|
||||||
|
') is set to to be subdomain to non-existent domain ID #' .
|
||||||
|
$domains[$key]['ismainbutsubto'] .
|
||||||
|
'. No DNS record(s) will be created for this domain.');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fwrite($this->debugHandler,
|
||||||
|
str_pad('domId', 9, ' ') . str_pad('domain', 40, ' ') .
|
||||||
|
'ismainbutsubto ' . str_pad('parent domain', 40, ' ') .
|
||||||
|
"list of child domain ids\n");
|
||||||
|
foreach ($domains as $domain) {
|
||||||
|
fwrite($this->debugHandler,
|
||||||
|
str_pad($domain['id'], 9, ' ') .
|
||||||
|
str_pad($domain['domain'], 40, ' ') .
|
||||||
|
str_pad($domain['ismainbutsubto'], 15, ' ') .
|
||||||
|
str_pad(((isset($domains[ $domain['ismainbutsubto'] ])) ?
|
||||||
|
$domains[ $domain['ismainbutsubto'] ]['domain'] :
|
||||||
|
'-'), 40, ' '));
|
||||||
|
foreach ($domain['children'] as $child) {
|
||||||
|
fwrite($this->debugHandler, '$child, ');
|
||||||
|
}
|
||||||
|
fwrite($this->debugHandler, "\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ($domains as $domain) {
|
||||||
|
if ($domain['ismainbutsubto'] > 0) {
|
||||||
|
// domains with ismainbutsubto>0 are handled by recursion within walkDomainList()
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
$this->walkDomainList($domain, $domains);
|
||||||
}
|
}
|
||||||
|
|
||||||
$bindconf_file_handler = fopen(makeCorrectFile(Settings::Get('system.bindconf_directory') . '/froxlor_bind.conf'), 'w');
|
$bindconf_file_handler = fopen(makeCorrectFile(Settings::Get('system.bindconf_directory') . '/froxlor_bind.conf'), 'w');
|
||||||
fwrite($bindconf_file_handler, $bindconf_file);
|
fwrite($bindconf_file_handler, $this->_bindconf_file);
|
||||||
fclose($bindconf_file_handler);
|
fclose($bindconf_file_handler);
|
||||||
fwrite($this->debugHandler, ' cron_tasks: Task4 - froxlor_bind.conf written' . "\n");
|
fwrite($this->debugHandler, ' cron_tasks: Task4 - froxlor_bind.conf written' . "\n");
|
||||||
$this->logger->logAction(CRON_ACTION, LOG_INFO, 'froxlor_bind.conf written');
|
$this->logger->logAction(CRON_ACTION, LOG_INFO, 'froxlor_bind.conf written');
|
||||||
@@ -137,25 +186,40 @@ class bind {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private function _generateDomainConfig($domain = array(), $froxlorhost = false) {
|
private function walkDomainList($domain, $domains) {
|
||||||
|
$zonefile = '';
|
||||||
|
$subzones = '';
|
||||||
|
|
||||||
$bindconf_file = '';
|
foreach($domain['children'] as $child_domain_id) {
|
||||||
|
$subzones.= $this->walkDomainList($domains[$child_domain_id], $domains);
|
||||||
|
}
|
||||||
|
|
||||||
fwrite($this->debugHandler, ' cron_tasks: Task4 - Writing ' . $domain['id'] . '::' . $domain['domain'] . "\n");
|
if ($domain['ismainbutsubto'] == 0 && $domain['zonefile'] == '') {
|
||||||
$this->logger->logAction(CRON_ACTION, LOG_INFO, 'Writing ' . $domain['id'] . '::' . $domain['domain']);
|
$zonefile = $this->generateZone($domain);
|
||||||
|
|
||||||
if ($domain['zonefile'] == '') {
|
|
||||||
$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']);
|
||||||
$this->_known_filenames[] = basename($zonefile_name);
|
$this->_known_filenames[] = basename($zonefile_name);
|
||||||
$zonefile_handler = fopen($zonefile_name, 'w');
|
$zonefile_handler = fopen($zonefile_name, 'w');
|
||||||
fwrite($zonefile_handler, $zonefile);
|
fwrite($zonefile_handler, $zonefile.$subzones);
|
||||||
fclose($zonefile_handler);
|
fclose($zonefile_handler);
|
||||||
fwrite($this->debugHandler, ' cron_tasks: Task4 - `' . $zonefile_name . '` zone written' . "\n");
|
fwrite($this->debugHandler, ' cron_tasks: Task4 - `' . $zonefile_name . '` zone written' . "\n");
|
||||||
|
} else {
|
||||||
|
return $this->generateZone($domain);
|
||||||
}
|
}
|
||||||
|
|
||||||
$bindconf_file.= '# Domain ID: ' . $domain['id'] . ' - CustomerID: ' . $domain['customerid'] . ' - CustomerLogin: ' . $domain['loginname'] . "\n";
|
if ($zonefile !== '') {
|
||||||
|
$this->_bindconf_file .= $this->_generateDomainConfig($domain);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private function _generateDomainConfig($domain = array()) {
|
||||||
|
if (isset($domain['froxlorhost']) && $domain['froxlorhost'] === '1') {
|
||||||
|
$froxlorhost = true;
|
||||||
|
} else {
|
||||||
|
$froxlorhost = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$bindconf_file = '# Domain ID: ' . $domain['id'] . ' - CustomerID: ' . $domain['customerid'] . ' - CustomerLogin: ' . $domain['loginname'] . "\n";
|
||||||
$bindconf_file.= 'zone "' . $domain['domain'] . '" in {' . "\n";
|
$bindconf_file.= 'zone "' . $domain['domain'] . '" in {' . "\n";
|
||||||
$bindconf_file.= ' type master;' . "\n";
|
$bindconf_file.= ' type master;' . "\n";
|
||||||
$bindconf_file.= ' file "' . makeCorrectFile(Settings::Get('system.bindconf_directory') . '/' . $domain['zonefile']) . '";' . "\n";
|
$bindconf_file.= ' file "' . makeCorrectFile(Settings::Get('system.bindconf_directory') . '/' . $domain['zonefile']) . '";' . "\n";
|
||||||
@@ -191,15 +255,19 @@ class bind {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* generate bind zone content. If froxlorhost is true,
|
* generate bind zone content.
|
||||||
* we will use ALL available IP addresses
|
|
||||||
*
|
*
|
||||||
* @param array $domain
|
* @param array $domain
|
||||||
* @param boolean $froxlorhost
|
|
||||||
*
|
*
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
protected function generateZone($domain, $froxlorhost = false) {
|
protected function generateZone($domain) {
|
||||||
|
if (isset($domain['froxlorhost']) && $domain['froxlorhost'] === '1') {
|
||||||
|
$froxlorhost = true;
|
||||||
|
} else {
|
||||||
|
$froxlorhost = false;
|
||||||
|
}
|
||||||
|
|
||||||
// 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
|
||||||
@@ -239,37 +307,41 @@ class bind {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$date = date('Ymd');
|
if ($domain['ismainbutsubto'] == 0) {
|
||||||
$bindserial = (preg_match('/^' . $date . '/', $domain['bindserial']) ? $domain['bindserial'] + 1 : $date . '00');
|
$date = date('Ymd');
|
||||||
|
$bindserial = (preg_match('/^' . $date . '/', $domain['bindserial']) ? $domain['bindserial'] + 1 : $date . '00');
|
||||||
|
|
||||||
if (!$froxlorhost) {
|
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) {
|
||||||
$zonefile.= '@ IN SOA ns ' . str_replace('@', '.', Settings::Get('panel.adminmail')) . '. (' . "\n";
|
$zonefile.= '@ IN SOA ns ' . str_replace('@', '.', Settings::Get('panel.adminmail')) . '. (' . "\n";
|
||||||
} else {
|
} else {
|
||||||
$zonefile.= '@ IN SOA ' . $this->nameservers[0]['hostname'] . ' ' . str_replace('@', '.', Settings::Get('panel.adminmail')) . '. (' . "\n";
|
$zonefile.= '@ IN SOA ' . $this->nameservers[0]['hostname'] . ' ' . str_replace('@', '.', Settings::Get('panel.adminmail')) . '. (' . "\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
$zonefile.= ' ' . $bindserial . ' ; serial' . "\n" . ' 8H ; refresh' . "\n" . ' 2H ; retry' . "\n" . ' 1W ; expiry' . "\n" . ' 11h) ; minimum' . "\n";
|
$zonefile.= ' ' . $bindserial . ' ; serial' . "\n" . ' 8H ; refresh' . "\n" . ' 2H ; retry' . "\n" . ' 1W ; expiry' . "\n" . ' 11h) ; minimum' . "\n";
|
||||||
|
|
||||||
// no nameservers given, use all if the A/AAAA entries
|
// no nameservers given, use all of the A/AAAA entries
|
||||||
if (count($this->nameservers) == 0) {
|
if (count($this->nameservers) == 0) {
|
||||||
$zonefile .= '@ IN NS ns' . "\n";
|
$zonefile .= '@ IN NS ns' . "\n";
|
||||||
foreach ($ip_a_records as $ip_a_record) {
|
foreach ($ip_a_records as $ip_a_record) {
|
||||||
$zonefile .= 'ns IN ' . $ip_a_record . "\n";
|
$zonefile .= 'ns IN ' . $ip_a_record . "\n";
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
foreach ($this->nameservers as $nameserver) {
|
||||||
|
$zonefile.= '@ IN NS ' . trim($nameserver['hostname']) . "\n";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
foreach ($this->nameservers as $nameserver) {
|
$zonefile = '$ORIGIN ' . $domain["domain"] . ".\n";
|
||||||
$zonefile.= '@ IN NS ' . trim($nameserver['hostname']) . "\n";
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($domain['isemaildomain'] === '1') {
|
if ($domain['isemaildomain'] === '1') {
|
||||||
@@ -317,7 +389,7 @@ class bind {
|
|||||||
if (!$froxlorhost) {
|
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 `ismainbutsubto` = '0' AND `domain` LIKE :domain
|
||||||
");
|
");
|
||||||
Database::pexecute($nssubdomains_stmt, array('domain' => '%.' . $domain['domain']));
|
Database::pexecute($nssubdomains_stmt, array('domain' => '%.' . $domain['domain']));
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user