remove domain from pdns database if removed or updated so nameserver is disabled (remove) or update of db triggered when isemaildomain option is disabled, fixes #581

Signed-off-by: Michael Kaufmann <d00p@froxlor.org>
This commit is contained in:
Michael Kaufmann
2018-11-17 20:14:58 +01:00
parent e184201327
commit 13c624400e
6 changed files with 229 additions and 149 deletions

View File

@@ -61,7 +61,7 @@ function json_response($status, $status_message = '', $data = null)
} }
header($resheader); header($resheader);
} }
$response = array();
$response['status'] = $status; $response['status'] = $status;
$response['status_message'] = $status_message; $response['status_message'] = $status_message;
$response['data'] = $data; $response['data'] = $data;

View File

@@ -1038,9 +1038,13 @@ class Domains extends ApiCommand implements ResourceEntity
$speciallogfile = $result['speciallogfile']; $speciallogfile = $result['speciallogfile'];
} }
if ($isbinddomain != $result['isbinddomain'] || $zonefile != $result['zonefile'] || $dkim != $result['dkim']) { if ($isbinddomain != $result['isbinddomain'] || $zonefile != $result['zonefile'] || $dkim != $result['dkim'] || $isemaildomain != $result['isemaildomain']) {
inserttask('4'); inserttask('4');
} }
// check whether nameserver has been disabled, #581
if ($isbinddomain != $result['isbinddomain'] && $isbinddomain == 0) {
inserttask('11', $result['domain']);
}
if ($isemaildomain == '0' && $result['isemaildomain'] == '1') { if ($isemaildomain == '0' && $result['isemaildomain'] == '1') {
$del_stmt = Database::prepare(" $del_stmt = Database::prepare("
@@ -1499,6 +1503,9 @@ class Domains extends ApiCommand implements ResourceEntity
triggerLetsEncryptCSRForAliasDestinationDomain($result['aliasdomain'], $this->logger()); triggerLetsEncryptCSRForAliasDestinationDomain($result['aliasdomain'], $this->logger());
// remove domains DNS from powerDNS if used, #581
inserttask('11', $result['domain']);
$this->logger()->logAction(ADM_ACTION, LOG_INFO, "[API] deleted domain/subdomains (#" . $result['id'] . ")"); $this->logger()->logAction(ADM_ACTION, LOG_INFO, "[API] deleted domain/subdomains (#" . $result['id'] . ")");
updateCounters(); updateCounters();
inserttask('1'); inserttask('1');

View File

@@ -0,0 +1,127 @@
<?php
/**
* This file is part of the Froxlor project.
* Copyright (c) 2016 the Froxlor Team (see authors).
*
* For the full copyright and license information, please view the COPYING
* file that was distributed with this source code. You can also view the
* COPYING file online at http://files.froxlor.org/misc/COPYING.txt
*
* @copyright (c) the authors
* @author Froxlor team <team@froxlor.org> (2016-)
* @license GPLv2 http://files.froxlor.org/misc/COPYING.txt
* @package Cron
*
*/
class PowerDNS
{
private static $pdns_db = null;
private static function connectToPdnsDb()
{
// get froxlor pdns config
$cf = Settings::Get('system.bindconf_directory') . '/froxlor/pdns_froxlor.conf';
$config = makeCorrectFile($cf);
if (! file_exists($config)) {
die('PowerDNS configuration file (' . $config . ') not found. Did you go through the configuration templates?' . PHP_EOL);
}
$lines = file($config);
$mysql_data = array();
foreach ($lines as $line) {
$line = trim($line);
if (strtolower(substr($line, 0, 6)) == 'gmysql') {
$namevalue = explode("=", $line);
$mysql_data[$namevalue[0]] = $namevalue[1];
}
}
// build up connection string
$driver = 'mysql';
$dsn = $driver . ":";
$options = array(
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET names utf8,sql_mode="NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"'
);
$attributes = array(
'ATTR_ERRMODE' => 'ERRMODE_EXCEPTION'
);
$dbconf = array();
$dbconf["dsn"] = array(
'dbname' => $mysql_data["gmysql-dbname"],
'charset' => 'utf8'
);
if (isset($mysql_data['gmysql-socket']) && ! empty($mysql_data['gmysql-socket'])) {
$dbconf["dsn"]['unix_socket'] = makeCorrectFile($mysql_data['gmysql-socket']);
} else {
$dbconf["dsn"]['host'] = $mysql_data['gmysql-host'];
$dbconf["dsn"]['port'] = $mysql_data['gmysql-port'];
}
// add options to dsn-string
foreach ($dbconf["dsn"] as $k => $v) {
$dsn .= $k . "=" . $v . ";";
}
// clean up
unset($dbconf);
// try to connect
try {
self::$pdns_db = new PDO($dsn, $mysql_data['gmysql-user'], $mysql_data['gmysql-password'], $options);
} catch (PDOException $e) {
die($e->getMessage());
}
// set attributes
foreach ($attributes as $k => $v) {
self::$pdns_db->setAttribute(constant("PDO::" . $k), constant("PDO::" . $v));
}
}
/**
* get pdo database connection to powerdns database
*
* @return PDO
*/
public static function getDB()
{
if (! isset(self::$pdns_db) || (self::$pdns_db instanceof PDO) == false) {
self::connectToPdnsDb();
}
return self::$pdns_db;
}
/**
* remove all records and entries of a given domain
*
* @param array $domain
*/
public static function cleanDomainZone($domain = null)
{
if (is_array($domain) && isset($domain['domain'])) {
$pdns_domains_stmt = self::getDB()->prepare("SELECT `id`, `name` FROM `domains` WHERE `name` = :domain");
$del_rec_stmt = self::getDB()->prepare("DELETE FROM `records` WHERE `domain_id` = :did");
$del_meta_stmt = self::getDB()->prepare("DELETE FROM `domainmetadata` WHERE `domain_id` = :did");
$del_dom_stmt = self::getDB()->prepare("DELETE FROM `domains` WHERE `id` = :did");
$pdns_domains_stmt->execute(array(
'domain' => $domain['domain']
));
$pdns_domain = $pdns_domains_stmt->fetch(\PDO::FETCH_ASSOC);
$del_rec_stmt->execute(array(
'did' => $pdns_domain['id']
));
$del_meta_stmt->execute(array(
'did' => $pdns_domain['id']
));
$del_dom_stmt->execute(array(
'did' => $pdns_domain['id']
));
}
}
}

View File

@@ -20,27 +20,26 @@
/** /**
* Inserts a task into the PANEL_TASKS-Table * Inserts a task into the PANEL_TASKS-Table
* *
* @param int Type of task * @param
* @param string Parameter 1 * int Type of task
* @param string Parameter 2 * @param
* @param string Parameter 3 * string Parameter 1
* @param
* string Parameter 2
* @param
* string Parameter 3
* @author Florian Lippert <flo@syscp.org> * @author Florian Lippert <flo@syscp.org>
* @author Froxlor team <team@froxlor.org> * @author Froxlor team <team@froxlor.org>
*/ */
function inserttask($type, $param1 = '', $param2 = '', $param3 = '', $param4 = '') { function inserttask($type, $param1 = '', $param2 = '', $param3 = '', $param4 = '')
{
// prepare the insert-statement // prepare the insert-statement
$ins_stmt = Database::prepare(" $ins_stmt = Database::prepare("
INSERT INTO `" . TABLE_PANEL_TASKS . "` SET `type` = :type, `data` = :data INSERT INTO `" . TABLE_PANEL_TASKS . "` SET `type` = :type, `data` = :data
"); ");
if ($type == '1' if ($type == '1' || $type == '3' || $type == '4' || $type == '5' || $type == '10' || $type == '99') {
|| $type == '3'
|| $type == '4'
|| $type == '5'
|| $type == '10'
|| $type == '99'
) {
// 4 = bind -> if bind disabled -> no task // 4 = bind -> if bind disabled -> no task
if ($type == '4' && Settings::Get('system.bind_enable') == '0') { if ($type == '4' && Settings::Get('system.bind_enable') == '0') {
return; return;
@@ -54,57 +53,65 @@ function inserttask($type, $param1 = '', $param2 = '', $param3 = '', $param4 = '
$del_stmt = Database::prepare(" $del_stmt = Database::prepare("
DELETE FROM `" . TABLE_PANEL_TASKS . "` WHERE `type` = :type DELETE FROM `" . TABLE_PANEL_TASKS . "` WHERE `type` = :type
"); ");
Database::pexecute($del_stmt, array('type' => $type)); Database::pexecute($del_stmt, array(
'type' => $type
));
// insert the new task // insert the new task
Database::pexecute($ins_stmt, array('type' => $type, 'data' => '')); Database::pexecute($ins_stmt, array(
'type' => $type,
} elseif ($type == '2' 'data' => ''
&& $param1 != '' ));
&& $param2 != '' } elseif ($type == '2' && $param1 != '' && $param2 != '' && $param3 != '' && ($param4 == 0 || $param4 == 1)) {
&& $param3 != ''
&& ($param4 == 0 || $param4 == 1)
) {
$data = array(); $data = array();
$data['loginname'] = $param1; $data['loginname'] = $param1;
$data['uid'] = $param2; $data['uid'] = $param2;
$data['gid'] = $param3; $data['gid'] = $param3;
$data['store_defaultindex'] = $param4; $data['store_defaultindex'] = $param4;
$data = json_encode($data); $data = json_encode($data);
Database::pexecute($ins_stmt, array('type' => '2', 'data' => $data)); Database::pexecute($ins_stmt, array(
'type' => '2',
} elseif ($type == '6' 'data' => $data
&& $param1 != '' ));
) { } elseif ($type == '6' && $param1 != '') {
$data = array(); $data = array();
$data['loginname'] = $param1; $data['loginname'] = $param1;
$data = json_encode($data); $data = json_encode($data);
Database::pexecute($ins_stmt, array('type' => '6', 'data' => $data)); Database::pexecute($ins_stmt, array(
'type' => '6',
} elseif ($type == '7' 'data' => $data
&& $param1 != '' ));
&& $param2 != '' } elseif ($type == '7' && $param1 != '' && $param2 != '') {
) {
$data = array(); $data = array();
$data['loginname'] = $param1; $data['loginname'] = $param1;
$data['email'] = $param2; $data['email'] = $param2;
$data = json_encode($data); $data = json_encode($data);
Database::pexecute($ins_stmt, array('type' => '7', 'data' => $data)); Database::pexecute($ins_stmt, array(
'type' => '7',
} elseif ($type == '8' 'data' => $data
&& $param1 != '' ));
&& $param2 != '' } elseif ($type == '8' && $param1 != '' && $param2 != '') {
) {
$data = array(); $data = array();
$data['loginname'] = $param1; $data['loginname'] = $param1;
$data['homedir'] = $param2; $data['homedir'] = $param2;
$data = json_encode($data); $data = json_encode($data);
Database::pexecute($ins_stmt, array('type' => '8', 'data' => $data)); Database::pexecute($ins_stmt, array(
'type' => '8',
} elseif ($type == '20' 'data' => $data
&& is_array($param1) ));
) { } elseif ($type == '11' && $param1 != '') {
$data = array();
$data['domain'] = $param1;
$data = json_encode($data);
Database::pexecute($ins_stmt, array(
'type' => '11',
'data' => $data
));
} elseif ($type == '20' && is_array($param1)) {
$data = json_encode($param1); $data = json_encode($param1);
Database::pexecute($ins_stmt, array('type' => '20', 'data' => $data)); Database::pexecute($ins_stmt, array(
'type' => '20',
'data' => $data
));
} }
} }

View File

@@ -14,21 +14,16 @@ if (! defined('MASTER_CRONJOB'))
* @author Froxlor team <team@froxlor.org> (2016-) * @author Froxlor team <team@froxlor.org> (2016-)
* @license GPLv2 http://files.froxlor.org/misc/COPYING.txt * @license GPLv2 http://files.froxlor.org/misc/COPYING.txt
* @package Cron * @package Cron
* *
*/ */
class pdns extends DnsBase class pdns extends DnsBase
{ {
private $pdns_db = null;
public function writeConfigs() public function writeConfigs()
{ {
// tell the world what we are doing // tell the world what we are doing
$this->_logger->logAction(CRON_ACTION, LOG_INFO, 'Task4 started - Refreshing DNS database'); $this->_logger->logAction(CRON_ACTION, LOG_INFO, 'Task4 started - Refreshing DNS database');
// connect to db
$this->_connectToPdnsDb();
$domains = $this->getDomainList(); $domains = $this->getDomainList();
// clean up // clean up
@@ -62,17 +57,14 @@ class pdns extends DnsBase
} }
if ($domain['zonefile'] == '') { if ($domain['zonefile'] == '') {
// check for system-hostname // check for system-hostname
$isFroxlorHostname = false; $isFroxlorHostname = false;
if (isset($domain['froxlorhost']) && $domain['froxlorhost'] == 1) { if (isset($domain['froxlorhost']) && $domain['froxlorhost'] == 1) {
$isFroxlorHostname = true; $isFroxlorHostname = true;
} }
if ($domain['ismainbutsubto'] == 0) { if ($domain['ismainbutsubto'] == 0) {
$zoneContent = createDomainZone(($domain['id'] == 'none') ? $zoneContent = createDomainZone(($domain['id'] == 'none') ? $domain : $domain['id'], $isFroxlorHostname);
$domain :
$domain['id'],
$isFroxlorHostname);
if (count($subzones)) { if (count($subzones)) {
foreach ($subzones as $subzone) { foreach ($subzones as $subzone) {
$zoneContent->records[] = $subzone; $zoneContent->records[] = $subzone;
@@ -83,16 +75,10 @@ class pdns extends DnsBase
$this->_insertAllowedTransfers($pdnsDomId); $this->_insertAllowedTransfers($pdnsDomId);
$this->_logger->logAction(CRON_ACTION, LOG_INFO, 'DB entries stored for zone `' . $domain['domain'] . '`'); $this->_logger->logAction(CRON_ACTION, LOG_INFO, 'DB entries stored for zone `' . $domain['domain'] . '`');
} else { } else {
return createDomainZone(($domain['id'] == 'none') ? return createDomainZone(($domain['id'] == 'none') ? $domain : $domain['id'], $isFroxlorHostname, true);
$domain :
$domain['id'],
$isFroxlorHostname,
true);
} }
} else { } else {
$this->_logger->logAction(CRON_ACTION, LOG_ERROR, $this->_logger->logAction(CRON_ACTION, LOG_ERROR, 'Custom zonefiles are NOT supported when PowerDNS is selected as DNS daemon (triggered by: ' . $domain['domain'] . ')');
'Custom zonefiles are NOT supported when PowerDNS is selected as DNS daemon (triggered by: ' .
$domain['domain'] . ')');
} }
} }
@@ -100,30 +86,40 @@ class pdns extends DnsBase
{ {
$this->_logger->logAction(CRON_ACTION, LOG_INFO, 'Cleaning dns zone entries from database'); $this->_logger->logAction(CRON_ACTION, LOG_INFO, 'Cleaning dns zone entries from database');
$pdns_domains_stmt = $this->pdns_db->prepare("SELECT `id`, `name` FROM `domains` WHERE `name` = :domain"); $pdns_domains_stmt = PowerDNS::getDB()->prepare("SELECT `id`, `name` FROM `domains` WHERE `name` = :domain");
$del_rec_stmt = $this->pdns_db->prepare("DELETE FROM `records` WHERE `domain_id` = :did"); $del_rec_stmt = PowerDNS::getDB()->prepare("DELETE FROM `records` WHERE `domain_id` = :did");
$del_meta_stmt = $this->pdns_db->prepare("DELETE FROM `domainmetadata` WHERE `domain_id` = :did"); $del_meta_stmt = PowerDNS::getDB()->prepare("DELETE FROM `domainmetadata` WHERE `domain_id` = :did");
$del_dom_stmt = $this->pdns_db->prepare("DELETE FROM `domains` WHERE `id` = :did"); $del_dom_stmt = PowerDNS::getDB()->prepare("DELETE FROM `domains` WHERE `id` = :did");
foreach ($domains as $domain) foreach ($domains as $domain) {
{ $pdns_domains_stmt->execute(array(
$pdns_domains_stmt->execute(array('domain' => $domain['domain'])); 'domain' => $domain['domain']
));
$pdns_domain = $pdns_domains_stmt->fetch(\PDO::FETCH_ASSOC); $pdns_domain = $pdns_domains_stmt->fetch(\PDO::FETCH_ASSOC);
$del_rec_stmt->execute(array('did' => $pdns_domain['id'])); $del_rec_stmt->execute(array(
$del_meta_stmt->execute(array('did' => $pdns_domain['id'])); 'did' => $pdns_domain['id']
$del_dom_stmt->execute(array('did' => $pdns_domain['id'])); ));
$del_meta_stmt->execute(array(
'did' => $pdns_domain['id']
));
$del_dom_stmt->execute(array(
'did' => $pdns_domain['id']
));
} }
} }
private function _insertZone($domainname, $serial = 0) private function _insertZone($domainname, $serial = 0)
{ {
$ins_stmt = $this->pdns_db->prepare(" $ins_stmt = PowerDNS::getDB()->prepare("
INSERT INTO domains set `name` = :domainname, `notified_serial` = :serial, `type` = 'NATIVE' INSERT INTO domains set `name` = :domainname, `notified_serial` = :serial, `type` = 'NATIVE'
"); ");
$ins_stmt->execute(array('domainname' => $domainname, 'serial' => $serial)); $ins_stmt->execute(array(
$lastid = $this->pdns_db->lastInsertId(); 'domainname' => $domainname,
'serial' => $serial
));
$lastid = PowerDNS::getDB()->lastInsertId();
return $lastid; return $lastid;
} }
@@ -131,7 +127,7 @@ class pdns extends DnsBase
{ {
$changedate = date('Ymds', time()); $changedate = date('Ymds', time());
$ins_stmt = $this->pdns_db->prepare(" $ins_stmt = PowerDNS::getDB()->prepare("
INSERT INTO records set INSERT INTO records set
`domain_id` = :did, `domain_id` = :did,
`name` = :rec, `name` = :rec,
@@ -143,8 +139,7 @@ class pdns extends DnsBase
`change_date` = :changedate `change_date` = :changedate
"); ");
foreach ($records as $record) foreach ($records as $record) {
{
if ($record instanceof DnsZone) { if ($record instanceof DnsZone) {
$this->_insertRecords($domainid, $record->records, $record->origin); $this->_insertRecords($domainid, $record->records, $record->origin);
continue; continue;
@@ -152,10 +147,8 @@ class pdns extends DnsBase
if ($record->record == '@') { if ($record->record == '@') {
$_record = $origin; $_record = $origin;
} } else {
else $_record = $record->record . "." . $origin;
{
$_record = $record->record.".".$origin;
} }
$ins_data = array( $ins_data = array(
@@ -173,7 +166,7 @@ class pdns extends DnsBase
private function _insertAllowedTransfers($domainid) private function _insertAllowedTransfers($domainid)
{ {
$ins_stmt = $this->pdns_db->prepare(" $ins_stmt = PowerDNS::getDB()->prepare("
INSERT INTO domainmetadata set `domain_id` = :did, `kind` = 'ALLOW-AXFR-FROM', `content` = :value INSERT INTO domainmetadata set `domain_id` = :did, `kind` = 'ALLOW-AXFR-FROM', `content` = :value
"); ");
@@ -200,67 +193,4 @@ class pdns extends DnsBase
} }
} }
} }
private function _connectToPdnsDb()
{
// get froxlor pdns config
$cf = Settings::Get('system.bindconf_directory').'/froxlor/pdns_froxlor.conf';
$config = makeCorrectFile($cf);
if (!file_exists($config))
{
$this->_logger->logAction(CRON_ACTION, LOG_ERROR, 'PowerDNS configuration file ('.$config.') not found. Did you go through the configuration templates?');
die('PowerDNS configuration file ('.$config.') not found. Did you go through the configuration templates?'.PHP_EOL);
}
$lines = file($config);
$mysql_data = array();
foreach ($lines as $line)
{
$line = trim($line);
if (strtolower(substr($line, 0, 6)) == 'gmysql')
{
$namevalue = explode("=", $line);
$mysql_data[$namevalue[0]] = $namevalue[1];
}
}
// build up connection string
$driver = 'mysql';
$dsn = $driver.":";
$options = array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET names utf8,sql_mode="NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"');
$attributes = array('ATTR_ERRMODE' => 'ERRMODE_EXCEPTION');
$dbconf = array();
$dbconf["dsn"] = array(
'dbname' => $mysql_data["gmysql-dbname"],
'charset' => 'utf8'
);
if (isset($mysql_data['gmysql-socket']) && !empty($mysql_data['gmysql-socket'])) {
$dbconf["dsn"]['unix_socket'] = makeCorrectFile($mysql_data['gmysql-socket']);
} else {
$dbconf["dsn"]['host'] = $mysql_data['gmysql-host'];
$dbconf["dsn"]['port'] = $mysql_data['gmysql-port'];
}
// add options to dsn-string
foreach ($dbconf["dsn"] as $k => $v) {
$dsn .= $k."=".$v.";";
}
// clean up
unset($dbconf);
// try to connect
try {
$this->pdns_db = new PDO($dsn, $mysql_data['gmysql-user'], $mysql_data['gmysql-password'], $options);
} catch (PDOException $e) {
die($e->getMessage());
}
// set attributes
foreach ($attributes as $k => $v) {
$this->pdns_db->setAttribute(constant("PDO::".$k), constant("PDO::".$v));
}
}
} }

View File

@@ -417,6 +417,15 @@ while ($row = $result_tasks_stmt->fetch(PDO::FETCH_ASSOC)) {
} }
} }
} }
/**
* TYPE=11 domain has been deleted, remove from pdns database if used
*/
if ($row['type'] == '11' && Settings::Get('system.dns_server') == 'pdns')
{
$cronlog->logAction(CRON_ACTION, LOG_NOTICE, "Removing PowerDNS entries for domain " . $row['data']['domain']);
PowerDNS::cleanDomainZone($row['data']['domain']);
}
} }
if ($num_results != 0) { if ($num_results != 0) {