2022-01-26 19:36:09 +01:00
|
|
|
<?php declare(strict_types=1);
|
2022-01-31 20:54:18 +01:00
|
|
|
|
2022-01-18 19:14:24 +01:00
|
|
|
namespace App\Controller;
|
|
|
|
|
2022-02-06 17:59:17 +01:00
|
|
|
use App\Entity\Domain;
|
2022-01-31 20:54:18 +01:00
|
|
|
use App\Repository\DomainRepository;
|
2022-01-31 20:56:28 +01:00
|
|
|
use App\Repository\NameserverRepository;
|
2022-02-12 20:41:59 +01:00
|
|
|
use App\Repository\PanelRepository;
|
2022-02-06 17:59:17 +01:00
|
|
|
use Monolog\Logger;
|
2022-01-26 19:36:09 +01:00
|
|
|
|
2022-01-31 20:54:18 +01:00
|
|
|
error_reporting(error_level: E_ALL);
|
2022-01-26 19:36:09 +01:00
|
|
|
|
2022-01-31 20:54:18 +01:00
|
|
|
// TODO check include "/etc/bind/local.zones";
|
2022-01-18 19:14:24 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
class DomainController
|
|
|
|
{
|
2022-01-31 20:54:18 +01:00
|
|
|
private string $localZoneFile;
|
|
|
|
private string $localZonesDir;
|
|
|
|
private string $namedConfLocalFile;
|
2022-01-22 17:32:36 +01:00
|
|
|
private string $zoneCachePath;
|
2022-02-21 20:22:48 +01:00
|
|
|
|
2022-01-18 19:14:24 +01:00
|
|
|
|
2022-03-01 16:43:48 +01:00
|
|
|
public function __construct(private NameserverRepository $nameserverRepository, private ApiController $checkController, private DomainRepository $domainRepository, private PanelRepository $panelRepository, private array $config, private Logger $log)
|
2022-01-18 19:14:24 +01:00
|
|
|
{
|
2022-02-06 17:59:17 +01:00
|
|
|
|
|
|
|
if ($this->config['debug']) {
|
|
|
|
$this->log->debug(message: "__construct()");
|
|
|
|
}
|
|
|
|
|
2022-01-22 17:32:36 +01:00
|
|
|
$this->localZoneFile = '/etc/bind/local.zones';
|
|
|
|
$this->localZonesDir = '/etc/bind/zones/';
|
|
|
|
$this->namedConfLocalFile = '/etc/bind/named.conf.local';
|
|
|
|
$this->zoneCachePath = '/var/cache/bind/';
|
2022-01-18 19:14:24 +01:00
|
|
|
}
|
|
|
|
|
2022-02-13 15:00:13 +01:00
|
|
|
|
2022-01-31 20:54:18 +01:00
|
|
|
function createIncludeFile()
|
2022-01-18 19:14:24 +01:00
|
|
|
{
|
2022-02-06 17:59:17 +01:00
|
|
|
if ($this->config['debug']) {
|
|
|
|
$this->log->debug(message: "createIncludeFile()");
|
|
|
|
}
|
|
|
|
|
2022-01-31 20:54:18 +01:00
|
|
|
$domains = $this->domainRepository->findAll();
|
2022-01-18 19:14:24 +01:00
|
|
|
|
2022-01-31 20:54:18 +01:00
|
|
|
$oFile = fopen(filename: $this->localZoneFile, mode: 'w');
|
|
|
|
foreach ($domains as $domain) {
|
2022-03-21 14:03:55 +01:00
|
|
|
if (!$this->isMasterZone(domain: $domain)) {
|
|
|
|
fputs(stream: $oFile, data: 'include "' . $this->localZonesDir . $domain->getName() . '";' . PHP_EOL);
|
|
|
|
}
|
2022-01-18 19:14:24 +01:00
|
|
|
}
|
2022-01-31 20:54:18 +01:00
|
|
|
fclose(stream: $oFile);
|
2022-02-12 20:41:59 +01:00
|
|
|
|
2022-03-21 14:03:55 +01:00
|
|
|
exec(command: '/usr/sbin/named-checkconf', output: $output, result_code: $resultCode);
|
|
|
|
if ($resultCode != 0) {
|
|
|
|
echo 'There was an error:' . PHP_EOL;
|
|
|
|
foreach ($output as $line) {
|
|
|
|
echo $line . PHP_EOL;
|
|
|
|
}
|
|
|
|
echo 'You need to fix the error before the configuration can be activated.' . PHP_EOL;
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2022-02-13 15:00:13 +01:00
|
|
|
exec(command: '/usr/sbin/rndc reload');
|
2022-01-18 19:14:24 +01:00
|
|
|
}
|
2022-01-25 20:34:13 +01:00
|
|
|
|
2022-01-18 19:14:24 +01:00
|
|
|
|
2022-04-06 16:27:58 +02:00
|
|
|
function updateSlaveZones()
|
|
|
|
{
|
|
|
|
if ($this->config['verbose']) {
|
|
|
|
echo 'Delete all slave zones';
|
|
|
|
}
|
|
|
|
|
|
|
|
$zones = glob(pattern: $this->localZonesDir . '*');
|
|
|
|
foreach ($zones as $zone) {
|
|
|
|
unlink(filename: $zone);
|
|
|
|
}
|
|
|
|
|
|
|
|
$domains = $this->domainRepository->findAll();
|
|
|
|
|
|
|
|
foreach ($domains as $domain) {
|
|
|
|
if ($this->config['verbose']) {
|
|
|
|
echo 'Create zone: ' . $domain->getName() . PHP_EOL;
|
|
|
|
}
|
|
|
|
$this->createSlaveZoneFile(domain: $domain);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->createIncludeFile();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-02-06 17:59:17 +01:00
|
|
|
function deleteOnNameservers(Domain $domain)
|
2022-01-18 19:14:24 +01:00
|
|
|
{
|
2022-02-06 17:59:17 +01:00
|
|
|
if ($this->config['debug']) {
|
|
|
|
$this->log->debug(message: "deleteOnNameserver()");
|
2022-01-18 19:14:24 +01:00
|
|
|
}
|
2022-01-31 20:54:18 +01:00
|
|
|
|
2022-01-31 20:56:28 +01:00
|
|
|
$nameservers = $this->nameserverRepository->findAll();
|
2022-01-31 20:54:18 +01:00
|
|
|
foreach ($nameservers as $nameserver) {
|
|
|
|
$body = [
|
2022-02-06 17:59:17 +01:00
|
|
|
'name' => $domain->getName()
|
2022-01-31 20:54:18 +01:00
|
|
|
];
|
2022-02-06 17:59:17 +01:00
|
|
|
if (!empty($nameserver->getAaaa())) {
|
|
|
|
$this->checkController->sendCommand(requestType: 'DELETE', serverName: $nameserver->getName(), versionIP: 6, apiKey: $nameserver->getApikey(), command: 'delete', serverType: 'nameserver', body: $body);
|
2022-01-31 20:54:18 +01:00
|
|
|
} else {
|
2022-02-06 17:59:17 +01:00
|
|
|
$this->checkController->sendCommand(requestType: 'DELETE', serverName: $nameserver->getName(), versionIP: 4, apiKey: $nameserver->getApikey(), command: 'delete', serverType: 'nameserver', body: $body);
|
2022-01-25 20:34:13 +01:00
|
|
|
}
|
2022-01-18 19:14:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2022-02-06 17:59:17 +01:00
|
|
|
* @param \App\Entity\Domain $domain
|
2022-01-18 19:14:24 +01:00
|
|
|
*
|
2022-01-31 20:54:18 +01:00
|
|
|
* @return void
|
2022-01-18 19:14:24 +01:00
|
|
|
*/
|
2022-02-06 17:59:17 +01:00
|
|
|
function deleteZone(Domain $domain)
|
2022-01-18 19:14:24 +01:00
|
|
|
{
|
2022-02-06 17:59:17 +01:00
|
|
|
if ($this->config['debug']) {
|
|
|
|
$this->log->debug(message: "deleteZone()");
|
2022-01-27 15:15:19 +01:00
|
|
|
}
|
|
|
|
|
2022-02-06 17:59:17 +01:00
|
|
|
$zoneFile = $this->localZonesDir . $domain->getName();
|
|
|
|
if (file_exists(filename: "$zoneFile")) {
|
|
|
|
unlink(filename: $zoneFile);
|
|
|
|
}
|
|
|
|
$this->createIncludeFile();
|
|
|
|
$this->deleteOnNameservers(domain: $domain);
|
2022-01-25 20:34:13 +01:00
|
|
|
}
|
|
|
|
|
2022-02-06 17:59:17 +01:00
|
|
|
|
2022-01-25 20:34:13 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function checkPermissions(): void
|
|
|
|
{
|
2022-02-06 17:59:17 +01:00
|
|
|
if ($this->config['debug']) {
|
|
|
|
$this->log->debug(message: "checkPermissions()");
|
|
|
|
}
|
|
|
|
|
2022-01-25 20:34:13 +01:00
|
|
|
echo 'Checking permission:' . PHP_EOL . PHP_EOL;
|
|
|
|
$uid = posix_geteuid();
|
2022-03-01 18:17:33 +01:00
|
|
|
echo "UID:\t" . COLOR_YELLOW . $uid . PHP_EOL;
|
2022-01-25 20:34:13 +01:00
|
|
|
|
2022-01-26 19:36:09 +01:00
|
|
|
$pwuid = posix_getpwuid(user_id: $uid);
|
2022-01-25 20:34:13 +01:00
|
|
|
$name = $pwuid['name'];
|
2022-03-01 16:43:48 +01:00
|
|
|
echo COLOR_DEFAULT . "Name:\t" . COLOR_YELLOW . $name . PHP_EOL;
|
2022-01-25 20:34:13 +01:00
|
|
|
$bindGroup = posix_getgrnam(name: 'bind');
|
|
|
|
$members = $bindGroup['members'];
|
|
|
|
if (in_array(needle: $name, haystack: $members)) {
|
2022-03-01 16:43:48 +01:00
|
|
|
echo "\t✅ $name" . COLOR_DEFAULT . ' is in group ' . COLOR_YELLOW . 'bind' . PHP_EOL;
|
2022-02-21 20:22:48 +01:00
|
|
|
} else {
|
2022-03-21 14:03:55 +01:00
|
|
|
echo "\t❌$name needs to be in group " . COLOR_YELLOW . 'bind' . COLOR_DEFAULT . '!' . PHP_EOL;
|
2022-01-25 20:34:13 +01:00
|
|
|
}
|
2022-01-31 20:54:18 +01:00
|
|
|
|
2022-03-01 18:17:33 +01:00
|
|
|
echo COLOR_DEFAULT . 'Checking ' . COLOR_YELLOW . $this->localZoneFile . PHP_EOL;
|
2022-03-21 14:03:55 +01:00
|
|
|
$localZoneFilePermissions = @fileperms(filename: $this->localZoneFile);
|
2022-01-25 20:34:13 +01:00
|
|
|
if ($localZoneFilePermissions & 0x0010) {
|
2022-03-01 18:17:33 +01:00
|
|
|
echo COLOR_DEFAULT . "\t✅ Group has write access." . PHP_EOL;
|
2022-01-25 20:34:13 +01:00
|
|
|
} else {
|
2022-03-01 18:17:33 +01:00
|
|
|
echo COLOR_RED . "\t❌Group needs write permission!" . PHP_EOL;
|
2022-01-25 20:34:13 +01:00
|
|
|
}
|
|
|
|
|
2022-03-01 18:17:33 +01:00
|
|
|
echo 'Checking ' . COLOR_YELLOW . $this->namedConfLocalFile . PHP_EOL;
|
2022-01-26 19:36:09 +01:00
|
|
|
if ($namedConfLocal = file_get_contents(filename: $this->namedConfLocalFile)) {
|
|
|
|
if (!str_contains(haystack: $namedConfLocal, needle: $this->localZoneFile)) {
|
2022-03-01 18:17:33 +01:00
|
|
|
echo "\t❌ $this->localZoneFile" . COLOR_RED . ' needs to be included in ' . COLOR_YELLOW . $this->namedConfLocalFile . PHP_EOL;
|
2022-01-25 20:34:13 +01:00
|
|
|
} else {
|
2022-03-01 18:17:33 +01:00
|
|
|
echo "\t✅ $this->localZoneFile" . COLOR_DEFAULT . ' is included in ' . COLOR_YELLOW . $this->namedConfLocalFile . PHP_EOL;
|
2022-01-25 20:34:13 +01:00
|
|
|
}
|
|
|
|
} else {
|
2022-01-31 20:54:18 +01:00
|
|
|
echo "\t❌ No access to '$this->namedConfLocalFile' . Please check permissions" . PHP_EOL;
|
2022-01-25 20:34:13 +01:00
|
|
|
}
|
|
|
|
|
2022-03-01 18:17:33 +01:00
|
|
|
echo COLOR_DEFAULT . 'Checking directory: ' . COLOR_YELLOW . $this->localZonesDir . PHP_EOL;
|
2022-03-21 14:03:55 +01:00
|
|
|
$localZoneDirPermissions = @fileperms(filename: $this->localZonesDir);
|
2022-01-25 20:34:13 +01:00
|
|
|
if ($localZoneDirPermissions & 0x0010) {
|
2022-03-01 16:43:48 +01:00
|
|
|
echo "\t✅ Group has write access." . PHP_EOL;
|
2022-01-25 20:34:13 +01:00
|
|
|
} else {
|
|
|
|
echo "\t❌Group needs write permission!" . PHP_EOL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-22 17:32:36 +01:00
|
|
|
|
|
|
|
/**
|
2022-03-01 16:43:48 +01:00
|
|
|
* @return void
|
2022-01-22 17:32:36 +01:00
|
|
|
*/
|
2022-03-01 16:43:48 +01:00
|
|
|
function checkDomains(): void
|
2022-01-22 17:32:36 +01:00
|
|
|
{
|
2022-03-01 16:43:48 +01:00
|
|
|
$localZones = file_get_contents(filename: $this->localZoneFile);
|
|
|
|
$maxNameLength = $this->domainRepository->getLongestEntry(field: 'name');
|
|
|
|
$domains = $this->domainRepository->findAll();
|
2022-02-06 17:59:17 +01:00
|
|
|
|
2022-03-01 16:43:48 +01:00
|
|
|
foreach ($domains as $domain) {
|
|
|
|
echo COLOR_YELLOW . str_pad(string: $domain->getName(), length: $maxNameLength + 1) . COLOR_DEFAULT;
|
|
|
|
|
|
|
|
if ($this->isMasterZone(domain: $domain)) {
|
|
|
|
echo 'Master Zone lies on this panel.';
|
|
|
|
} else {
|
|
|
|
if (!str_contains(haystack: $localZones, needle: $domain->getName())) {
|
|
|
|
echo COLOR_RED . ' is missing in ' . COLOR_YELLOW . $this->localZoneFile . COLOR_DEFAULT;
|
|
|
|
} else {
|
|
|
|
echo $domain->getName() . ' exists in ' . COLOR_YELLOW . $this->localZoneFile;
|
|
|
|
}
|
|
|
|
|
|
|
|
$zoneFile = $this->localZonesDir . $domain->getName();
|
|
|
|
|
|
|
|
if (!file_exists(filename: $zoneFile)) {
|
|
|
|
echo "Missing zone file for $zoneFile . Update zone to create it";
|
|
|
|
}
|
2022-01-22 17:32:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-03-01 16:43:48 +01:00
|
|
|
echo COLOR_DEFAULT . PHP_EOL;
|
2022-01-22 17:32:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-01-22 18:25:18 +01:00
|
|
|
|
2022-01-22 17:32:36 +01:00
|
|
|
/**
|
2022-02-06 17:59:17 +01:00
|
|
|
* @param \App\Entity\Domain $domain
|
2022-01-22 17:32:36 +01:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2022-02-12 19:36:33 +01:00
|
|
|
public function createSlaveZoneFile(Domain $domain): void
|
2022-01-22 17:32:36 +01:00
|
|
|
{
|
2022-04-06 16:27:58 +02:00
|
|
|
$domainName = $domain->getName();
|
2022-02-06 17:59:17 +01:00
|
|
|
if ($this->config['debug']) {
|
|
|
|
$this->log->debug(message: "createZoneFile($domainName)");
|
|
|
|
}
|
|
|
|
|
2022-02-21 20:22:48 +01:00
|
|
|
// check if we're a master zone
|
2022-03-21 14:05:00 +01:00
|
|
|
if ($this->isMasterZone(domain: $domain)) {
|
2022-04-06 16:27:58 +02:00
|
|
|
echo 'We are zone master for ' . $domainName . PHP_EOL;
|
2022-02-21 20:22:48 +01:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2022-04-06 16:27:58 +02:00
|
|
|
if ($zonefile = fopen(filename: $this->localZonesDir . $domainName, mode: 'w')) {
|
2022-02-22 15:32:23 +01:00
|
|
|
$panelName = $domain->getPanel();
|
2022-04-06 16:27:58 +02:00
|
|
|
if (!$panel = $this->panelRepository->findByName(name: $panelName)) {
|
|
|
|
echo "Error: Panel $panelName doesn't exist." . PHP_EOL;
|
|
|
|
die();
|
|
|
|
}
|
2022-02-12 20:41:59 +01:00
|
|
|
$a = $panel->getA();
|
|
|
|
$aaaa = $panel->getAaaa();
|
2022-04-06 16:27:58 +02:00
|
|
|
fputs(stream: $zonefile, data: 'zone "' . $domainName . '"' . ' IN {' . PHP_EOL);
|
2022-01-26 19:36:09 +01:00
|
|
|
fputs(stream: $zonefile, data: "\ttype slave;" . PHP_EOL);
|
2022-04-06 16:27:58 +02:00
|
|
|
fputs(stream: $zonefile, data: "\tfile \"" . $this->zoneCachePath . $domainName . '.db";' . PHP_EOL);
|
2022-01-26 19:36:09 +01:00
|
|
|
fputs(stream: $zonefile, data: "\tmasters {" . PHP_EOL);
|
2022-01-22 17:32:36 +01:00
|
|
|
if (!empty($a)) {
|
2022-02-12 20:41:59 +01:00
|
|
|
fputs(stream: $zonefile, data: "\t\t" . $a . ';' . PHP_EOL);
|
2022-01-22 17:32:36 +01:00
|
|
|
}
|
|
|
|
if (!empty($aaaa)) {
|
2022-02-12 20:41:59 +01:00
|
|
|
fputs(stream: $zonefile, data: "\t\t" . $aaaa . ';' . PHP_EOL);
|
2022-01-22 17:32:36 +01:00
|
|
|
}
|
2022-01-26 19:36:09 +01:00
|
|
|
fputs(stream: $zonefile, data: "\t};" . PHP_EOL);
|
|
|
|
fputs(stream: $zonefile, data: "};" . PHP_EOL);
|
2022-01-22 17:32:36 +01:00
|
|
|
}
|
2022-02-12 19:36:33 +01:00
|
|
|
}
|
2022-02-13 15:00:13 +01:00
|
|
|
|
2022-04-06 16:27:58 +02:00
|
|
|
|
|
|
|
public function isMasterZone(Domain $domain): bool
|
2022-03-01 16:43:48 +01:00
|
|
|
{
|
|
|
|
if (file_exists(filename: '/etc/bind/keyhelp_domains/' . $domain->getName())) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2022-07-19 18:53:35 +02:00
|
|
|
|
2022-01-18 19:14:24 +01:00
|
|
|
}
|