Skip to content

Commit

Permalink
feat: Add support for dedicated database for PowerDNS tables
Browse files Browse the repository at this point in the history
  • Loading branch information
edmondas committed Feb 4, 2024
1 parent b02e9fa commit 5e11d2a
Show file tree
Hide file tree
Showing 39 changed files with 586 additions and 377 deletions.
35 changes: 19 additions & 16 deletions dynamic_update.php
Expand Up @@ -38,19 +38,22 @@

require_once __DIR__ . '/vendor/autoload.php';

$configuration = new LegacyConfiguration();
$db_type = $configuration->get('db_type');
$config = new LegacyConfiguration();
$db_type = $config->get('db_type');

$pdns_db_name = $config->get('pdns_db_name');
$records_table = $pdns_db_name ? $pdns_db_name . '.records' : 'records';

$credentials = [
'db_host' => $configuration->get('db_host'),
'db_port' => $configuration->get('db_port'),
'db_user' => $configuration->get('db_user'),
'db_pass' => $configuration->get('db_pass'),
'db_name' => $configuration->get('db_name'),
'db_charset' => $configuration->get('db_charset'),
'db_collation' => $configuration->get('db_collation'),
'db_host' => $config->get('db_host'),
'db_port' => $config->get('db_port'),
'db_user' => $config->get('db_user'),
'db_pass' => $config->get('db_pass'),
'db_name' => $config->get('db_name'),
'db_charset' => $config->get('db_charset'),
'db_collation' => $config->get('db_collation'),
'db_type' => $db_type,
'db_file' => $configuration->get('db_file'),
'db_file' => $config->get('db_file'),
];

$databaseConnection = new PDODatabaseConnection();
Expand Down Expand Up @@ -204,8 +207,8 @@ function valid_ip_address(string $ip): int|string
)");

$userAuthService = new UserAuthenticationService(
$configuration->get('password_encryption'),
$configuration->get('password_encryption_cost')
$config->get('password_encryption'),
$config->get('password_encryption_cost')
);
if (!$user || !$userAuthService->verifyPassword($auth_password, $user['password'])) {
return status_exit('badauth2');
Expand All @@ -218,7 +221,7 @@ function valid_ip_address(string $ip): int|string

while ($zone = $zones_query->fetch()) {
$zone_updated = false;
$name_query = $db->prepare("SELECT name, type, content FROM records WHERE domain_id=:domain_id and (type = 'A' OR type = 'AAAA')");
$name_query = $db->prepare("SELECT name, type, content FROM $records_table WHERE domain_id=:domain_id and (type = 'A' OR type = 'AAAA')");
$name_query->execute([':domain_id' => $zone["domain_id"]]);

while ($record = $name_query->fetch()) {
Expand All @@ -227,7 +230,7 @@ function valid_ip_address(string $ip): int|string
if ($ip == $record['content']) {
$no_update_necessary = true;
} else {
$update_query = $db->prepare("UPDATE records SET content =:ip where name=:record_name and type='A'");
$update_query = $db->prepare("UPDATE $records_table SET content =:ip where name=:record_name and type='A'");
$update_query->execute([':ip' => $ip, ':record_name' => $record['name']]);
$zone_updated = true;
$was_updated = true;
Expand All @@ -236,7 +239,7 @@ function valid_ip_address(string $ip): int|string
if ($ip6 == $record['content']) {
$no_update_necessary = true;
} else {
$update_query = $db->prepare("UPDATE records SET content =:ip6 where name=:record_name and type='AAAA'");
$update_query = $db->prepare("UPDATE $records_table SET content =:ip6 where name=:record_name and type='AAAA'");
$update_query->execute([':ip6' => $ip6, ':record_name' => $record['name']]);
$zone_updated = true;
$was_updated = true;
Expand All @@ -245,7 +248,7 @@ function valid_ip_address(string $ip): int|string
}
}
if ($zone_updated) {
$dnsRecord = new DnsRecord($db, $configuration);
$dnsRecord = new DnsRecord($db, $config);
$dnsRecord->update_soa_serial($zone['domain_id']);
}
}
Expand Down
16 changes: 10 additions & 6 deletions lib/Application/Controller/AddRecordController.php
Expand Up @@ -59,7 +59,8 @@ public function run(): void

$perm_edit = Permission::getEditPermission($this->db);
$zone_id = htmlspecialchars($_GET['id']);
$zone_type = DnsRecord::get_domain_type($this->db, $zone_id);
$dnsRecord = new DnsRecord($this->db, $this->getConfig());
$zone_type = $dnsRecord->get_domain_type($zone_id);
$user_is_zone_owner = LegacyUsers::verify_user_is_owner_zoneid($this->db, $zone_id);

$this->checkCondition($zone_type == "SLAVE"
Expand Down Expand Up @@ -103,7 +104,8 @@ private function addRecord(): void
private function showForm(): void
{
$zone_id = htmlspecialchars($_GET['id']);
$zone_name = DnsRecord::get_domain_name_by_id($this->db, $zone_id);
$dnsRecord = new DnsRecord($this->db, $this->getConfig());
$zone_name = $dnsRecord->get_domain_name_by_id($zone_id);
$ttl = $this->config('dns_ttl');
$iface_add_reverse_record = $this->config('iface_add_reverse_record');
$is_reverse_zone = preg_match('/i(p6|n-addr).arpa/i', $zone_name);
Expand Down Expand Up @@ -144,19 +146,20 @@ public function checkId(): void
public function createReverseRecord($name, $type, $content, string $zone_id, $ttl, $prio): void
{
$iface_add_reverse_record = $this->config('iface_add_reverse_record');
$dnsRecord = new DnsRecord($this->db, $this->getConfig());

if ((isset($_POST["reverse"])) && $name && $iface_add_reverse_record) {
if ($type === 'A') {
$content_array = preg_split("/\./", $content);
$content_rev = sprintf("%d.%d.%d.%d.in-addr.arpa", $content_array[3], $content_array[2], $content_array[1], $content_array[0]);
$zone_rev_id = DnsRecord::get_best_matching_zone_id_from_name($this->db, $content_rev);
$zone_rev_id = $dnsRecord->get_best_matching_zone_id_from_name($content_rev);
} elseif ($type === 'AAAA') {
$content_rev = DnsRecord::convert_ipv6addr_to_ptrrec($content);
$zone_rev_id = DnsRecord::get_best_matching_zone_id_from_name($this->db, $content_rev);
$zone_rev_id = $dnsRecord->get_best_matching_zone_id_from_name($content_rev);
}

if (isset($zone_rev_id) && $zone_rev_id != -1) {
$zone_name = DnsRecord::get_domain_name_by_id($this->db, $zone_id);
$zone_name = $dnsRecord->get_domain_name_by_id($zone_id);
$fqdn_name = sprintf("%s.%s", $name, $zone_name);
$dnsRecord = new DnsRecord($this->db, $this->getConfig());
if ($dnsRecord->add_record($zone_rev_id, $content_rev, 'PTR', $fqdn_name, $ttl, $prio)) {
Expand All @@ -179,7 +182,8 @@ public function createReverseRecord($name, $type, $content, string $zone_id, $tt

public function createRecord(string $zone_id, $name, $type, $content, $ttl, $prio): bool
{
$zone_name = DnsRecord::get_domain_name_by_id($this->db, $zone_id);
$dnsRecord = new DnsRecord($this->db, $this->getConfig());
$zone_name = $dnsRecord->get_domain_name_by_id($zone_id);

$dnsRecord = new DnsRecord($this->db, $this->getConfig());
if ($dnsRecord->add_record($zone_id, $name, $type, $content, $ttl, $prio)) {
Expand Down
4 changes: 2 additions & 2 deletions lib/Application/Controller/AddZoneMasterController.php
Expand Up @@ -89,13 +89,13 @@ private function addZone(): void
} elseif ($dns_third_level_check && DnsRecord::get_domain_level($zone_name) > 2 && $dnsRecord->domain_exists(DnsRecord::get_second_level_domain($zone_name))) {
$this->setMessage('add_zone_master', 'error', _('There is already a zone_name with this name.'));
$this->showForm();
} elseif ($dnsRecord->domain_exists($zone_name) || DnsRecord::record_name_exists($this->db, $zone_name)) {
} elseif ($dnsRecord->domain_exists($zone_name) || $dnsRecord->record_name_exists($zone_name)) {
$this->setMessage('add_zone_master', 'error', _('There is already a zone_name with this name.'));
$this->showForm();
} elseif ($dnsRecord->add_domain($this->db, $zone_name, $owner, $dom_type, '', $zone_template)) {
$this->setMessage('list_zones', 'success', _('Zone has been added successfully.'));

$zone_id = DnsRecord::get_zone_id_from_name($this->db, $zone_name);
$zone_id = $dnsRecord->get_zone_id_from_name($zone_name);
$this->logger->log_info(sprintf('client_ip:%s user:%s operation:add_zone zone_name:%s zone_type:%s zone_template:%s',
$_SERVER['REMOTE_ADDR'], $_SESSION["userlogin"],
$zone_name, $dom_type, $zone_template), $zone_id);
Expand Down
4 changes: 2 additions & 2 deletions lib/Application/Controller/AddZoneSlaveController.php
Expand Up @@ -87,7 +87,7 @@ private function addZone(): void
} elseif ($dns_third_level_check && DnsRecord::get_domain_level($zone) > 2 && $dnsRecord->domain_exists(DnsRecord::get_second_level_domain($zone))) {
$this->setMessage('add_zone_slave', 'error', _('There is already a zone with this name.'));
$this->showForm();
} elseif ($dnsRecord->domain_exists($zone) || DnsRecord::record_name_exists($this->db, $zone)) {
} elseif ($dnsRecord->domain_exists($zone) || $dnsRecord->record_name_exists($zone)) {
$this->setMessage('add_zone_slave', 'error', _('There is already a zone with this name.'));
$this->showForm();
} elseif (!Dns::are_multiple_valid_ips($master)) {
Expand All @@ -96,7 +96,7 @@ private function addZone(): void
} else {
$dnsRecord = new DnsRecord($this->db, $this->getConfig());
if ($dnsRecord->add_domain($this->db, $zone, $owner, $type, $master, 'none')) {
$zone_id = DnsRecord::get_zone_id_from_name($this->db, $zone);
$zone_id = $dnsRecord->get_zone_id_from_name($zone);
$this->logger->log_info(sprintf('client_ip:%s user:%s operation:add_zone zone:%s zone_type:SLAVE zone_master:%s',
$_SERVER['REMOTE_ADDR'], $_SESSION["userlogin"],
$zone, $master), $zone_id);
Expand Down
2 changes: 1 addition & 1 deletion lib/Application/Controller/BulkRegistrationController.php
Expand Up @@ -87,7 +87,7 @@ private function doBulkRegistration(): void
} elseif ($dnsRecord->domain_exists($domain)) {
$failed_domains[] = $domain . " - " . _('There is already a zone with this name.');
} elseif ($dnsRecord->add_domain($this->db, $domain, $_POST['owner'], $dom_type, '', $zone_template)) {
$zone_id = DnsRecord::get_zone_id_from_name($this->db, $domain);
$zone_id = $dnsRecord->get_zone_id_from_name($domain);
$this->logger->log_info(sprintf('client_ip:%s user:%s operation:add_zone zone:%s zone_type:%s zone_template:%s',
$_SERVER['REMOTE_ADDR'], $_SESSION["userlogin"],
$domain, $dom_type, $zone_template), $zone_id);
Expand Down
17 changes: 10 additions & 7 deletions lib/Application/Controller/DeleteDomainController.php
Expand Up @@ -77,19 +77,20 @@ public function run(): void

private function deleteDomain(string $zone_id): void
{
$zone_info = DnsRecord::get_zone_info_from_id($this->db, $zone_id);
$dnsRecord = new DnsRecord($this->db, $this->getConfig());
$zone_info = $dnsRecord->get_zone_info_from_id($zone_id);
$pdnssec_use = $this->config('pdnssec_use');

if ($pdnssec_use && $zone_info['type'] == 'MASTER') {
$zone_name = DnsRecord::get_domain_name_by_id($this->db, $zone_id);
$zone_name = $dnsRecord->get_domain_name_by_id($zone_id);

$dnssecProvider = DnssecProviderFactory::create($this->db, $this->getConfig());
if ($dnssecProvider->isZoneSecured($zone_name)) {
if ($dnssecProvider->isZoneSecured($zone_name, $this->getConfig())) {
$dnssecProvider->unsecureZone($zone_name);
}
}

if (DnsRecord::delete_domain($this->db, $zone_id)) {
if ($dnsRecord->delete_domain($zone_id)) {
$this->logger->log_info(sprintf('client_ip:%s user:%s operation:delete_zone zone:%s zone_type:%s',
$_SERVER['REMOTE_ADDR'], $_SESSION["userlogin"],
$zone_info['name'], $zone_info['type']), $zone_id);
Expand All @@ -101,13 +102,15 @@ private function deleteDomain(string $zone_id): void

private function showDeleteDomain(string $zone_id): void
{
$zone_info = DnsRecord::get_zone_info_from_id($this->db, $zone_id);
$dnsRecord = new DnsRecord($this->db, $this->getConfig());
$zone_info = $dnsRecord->get_zone_info_from_id($zone_id);
$zone_owners = LegacyUsers::get_fullnames_owners_from_domainid($this->db, $zone_id);

$slave_master_exists = false;
if ($zone_info['type'] == 'SLAVE') {
$slave_master = DnsRecord::get_domain_slave_master($this->db, $zone_id);
if (DnsRecord::supermaster_exists($this->db, $slave_master)) {
$dnsRecord = new DnsRecord($this->db, $this->getConfig());
$slave_master = $dnsRecord->get_domain_slave_master($zone_id);
if ($dnsRecord->supermaster_exists($slave_master)) {
$slave_master_exists = true;
}
}
Expand Down
11 changes: 7 additions & 4 deletions lib/Application/Controller/DeleteDomainsController.php
Expand Up @@ -66,7 +66,7 @@ public function run(): void
public function deleteDomains($zone_ids): void
{
$dnsRecord = new DnsRecord($this->db, $this->getConfig());
$deleted_zones = DnsRecord::get_zone_info_from_ids($this->db, $zone_ids);
$deleted_zones = $dnsRecord->get_zone_info_from_ids($zone_ids);
$delete_domains = $dnsRecord->delete_domains($zone_ids);

if ($delete_domains) {
Expand Down Expand Up @@ -98,18 +98,21 @@ public function showDomains($zone_ids): void
private function getZoneInfo($zone_ids): array
{
$zones = [];
$dnsRecord = new DnsRecord($this->db, $this->getConfig());

foreach ($zone_ids as $zone_id) {
$zones[$zone_id]['id'] = $zone_id;
$zones[$zone_id] = DnsRecord::get_zone_info_from_id($this->db, $zone_id);
$zones[$zone_id] = $dnsRecord->get_zone_info_from_id($zone_id);
$zones[$zone_id]['owner'] = LegacyUsers::get_fullnames_owners_from_domainid($this->db, $zone_id);
$zones[$zone_id]['is_owner'] = LegacyUsers::verify_user_is_owner_zoneid($this->db, $zone_id);

$zones[$zone_id]['has_supermaster'] = false;
$zones[$zone_id]['slave_master'] = null;
if ($zones[$zone_id]['type'] == "SLAVE") {
$slave_master = DnsRecord::get_domain_slave_master($this->db, $zone_id);
$slave_master = $dnsRecord->get_domain_slave_master($zone_id);
$zones[$zone_id]['slave_master'] = $slave_master;
if (DnsRecord::supermaster_exists($this->db, $slave_master)) {

if ($dnsRecord->supermaster_exists($slave_master)) {
$zones[$zone_id]['has_supermaster'] = true;
}
}
Expand Down
19 changes: 11 additions & 8 deletions lib/Application/Controller/DeleteRecordController.php
Expand Up @@ -58,14 +58,15 @@ public function run(): void
}

$record_id = htmlspecialchars($_GET['id']);
$zid = DnsRecord::get_zone_id_from_record_id($this->db, $record_id);
$dnsRecord = new DnsRecord($this->db, $this->getConfig());
$zid = $dnsRecord->get_zone_id_from_record_id($record_id);
if ($zid == NULL) {
$this->showError(_('There is no zone with this ID.'));
}

if (isset($_GET['confirm'])) {
$record_info = DnsRecord::get_record_from_id($this->db, $record_id);
if (DnsRecord::delete_record($this->db, $record_id)) {
$record_info = $dnsRecord->get_record_from_id($record_id);
if ($dnsRecord->delete_record($record_id)) {
if (isset($record_info['prio'])) {
$this->logger->log_info(sprintf('client_ip:%s user:%s operation:delete_record record_type:%s record:%s content:%s ttl:%s priority:%s',
$_SERVER['REMOTE_ADDR'], $_SESSION["userlogin"],
Expand All @@ -81,7 +82,7 @@ public function run(): void
$dnsRecord->update_soa_serial($zid);

if ($this->config('pdnssec_use')) {
$zone_name = DnsRecord::get_domain_name_by_id($this->db, $zid);
$zone_name = $dnsRecord->get_domain_name_by_id($zid);
$dnssecProvider = DnssecProviderFactory::create($this->db, $this->getConfig());
$dnssecProvider->rectifyZone($zone_name);
}
Expand All @@ -93,8 +94,9 @@ public function run(): void

$perm_edit = Permission::getEditPermission($this->db);

$zone_info = DnsRecord::get_zone_info_from_id($this->db, $zid);
$zone_id = DnsRecord::recid_to_domid($this->db, $record_id);
$dnsRecord = new DnsRecord($this->db, $this->getConfig());
$zone_info = $dnsRecord->get_zone_info_from_id($zid);
$zone_id = $dnsRecord->recid_to_domid($record_id);
$user_is_zone_owner = LegacyUsers::verify_user_is_owner_zoneid($this->db, $zone_id);
if ($zone_info['type'] == "SLAVE" || $perm_edit == "none" || ($perm_edit == "own" || $perm_edit == "own_as_client") && $user_is_zone_owner == "0") {
$this->showError(_("You do not have the permission to edit this record."));
Expand All @@ -105,7 +107,8 @@ public function run(): void

public function showQuestion(string $record_id, $zid, int $zone_id): void
{
$zone_name = DnsRecord::get_domain_name_by_id($this->db, $zone_id);
$dnsRecord = new DnsRecord($this->db, $this->getConfig());
$zone_name = $dnsRecord->get_domain_name_by_id($zone_id);

if (str_starts_with($zone_name, "xn--")) {
$idn_zone_name = idn_to_utf8($zone_name, IDNA_NONTRANSITIONAL_TO_ASCII);
Expand All @@ -118,7 +121,7 @@ public function showQuestion(string $record_id, $zid, int $zone_id): void
'zone_id' => $zid,
'zone_name' => $zone_name,
'idn_zone_name' => $idn_zone_name,
'record_info' => DnsRecord::get_record_from_id($this->db, $record_id),
'record_info' => $dnsRecord->get_record_from_id($record_id),
]);
}
}
3 changes: 2 additions & 1 deletion lib/Application/Controller/DeleteSuperMasterController.php
Expand Up @@ -79,7 +79,8 @@ private function deleteSuperMaster(): void
private function showDeleteSuperMaster(): void
{
$master_ip = htmlspecialchars($_GET['master_ip']);
$info = DnsRecord::get_supermaster_info_from_ip($this->db, $master_ip);
$dnsRecord = new DnsRecord($this->db, $this->getConfig());
$info = $dnsRecord->get_supermaster_info_from_ip($master_ip);

$this->render('delete_supermaster.html', [
'master_ip' => $master_ip,
Expand Down
3 changes: 2 additions & 1 deletion lib/Application/Controller/DeleteUserController.php
Expand Up @@ -73,7 +73,8 @@ public function deleteUser(string $uid): void
$zones = $_POST['zone'];
}

if (LegacyUsers::delete_user($this->db, $uid, $zones)) {
$legacyUsers = new LegacyUsers($this->db, $this->getConfig());
if ($legacyUsers->delete_user($uid, $zones)) {
$this->setMessage('users', 'success', _('The user has been deleted successfully.'));
$this->redirect('index.php', ['page'=> 'users']);
}
Expand Down
6 changes: 4 additions & 2 deletions lib/Application/Controller/DnsSecAddKeyController.php
Expand Up @@ -54,7 +54,8 @@ public function run(): void
$this->showError(_("You do not have the permission to view this zone."));
}

if (DnsRecord::zone_id_exists($this->db, $zone_id) == "0") {
$dnsRecord = new DnsRecord($this->db, $this->getConfig());
if ($dnsRecord->zone_id_exists($zone_id) == "0") {
$this->showError(_('There is no zone with this ID.'));
}

Expand Down Expand Up @@ -88,7 +89,8 @@ public function run(): void
}
}

$domain_name = DnsRecord::get_domain_name_by_id($this->db, $zone_id);
$dnsRecord = new DnsRecord($this->db, $this->getConfig());
$domain_name = $dnsRecord->get_domain_name_by_id($zone_id);
if (isset($_POST["submit"])) {
$dnssecProvider = DnssecProviderFactory::create($this->db, $this->getConfig());
if ($dnssecProvider->addZoneKey($domain_name, $key_type, $bits, $algorithm)) {
Expand Down

0 comments on commit 5e11d2a

Please sign in to comment.