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-01-31 20:54:18 +01:00
|
|
|
use App\Repository\DomainRepository;
|
2022-01-31 20:56:28 +01:00
|
|
|
use App\Repository\NameserverRepository;
|
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-01-18 19:14:24 +01:00
|
|
|
|
2022-01-31 20:56:28 +01:00
|
|
|
public function __construct(private NameserverRepository $nameserverRepository, private CheckController $checkController, private DomainRepository $domainRepository)
|
2022-01-18 19:14:24 +01:00
|
|
|
{
|
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-01-31 20:54:18 +01:00
|
|
|
/*
|
2022-01-18 19:14:24 +01:00
|
|
|
/**
|
|
|
|
* @param String $name
|
2022-01-31 20:54:18 +01:00
|
|
|
* @param mixed $a
|
|
|
|
* @param mixed $aaaa
|
2022-01-18 19:14:24 +01:00
|
|
|
*
|
2022-01-31 20:54:18 +01:00
|
|
|
* @return void
|
|
|
|
public function createZone(string $name, mixed $a, mixed $aaaa): void
|
2022-01-18 19:14:24 +01:00
|
|
|
{
|
2022-01-31 20:54:18 +01:00
|
|
|
$this->createZoneFile(name: $name, a: $a, aaaa: $aaaa);
|
|
|
|
/*
|
|
|
|
$zoneFilename = $this->localZonesDir . $name;
|
|
|
|
echo $zoneFilename . PHP_EOL;
|
|
|
|
|
|
|
|
if ($localZones = fopen(filename: $this->localZoneFile, mode: 'a')) {
|
|
|
|
fputs(stream: $localZones, data: "include \"$zoneFilename\";" . PHP_EOL);
|
|
|
|
fclose(stream: $localZones);
|
|
|
|
} else {
|
|
|
|
echo "Error writing to $this->localZoneFile, check permissions";
|
|
|
|
exit(1);
|
2022-01-18 19:14:24 +01:00
|
|
|
}
|
|
|
|
}
|
2022-01-31 20:54:18 +01:00
|
|
|
*/
|
2022-01-18 19:14:24 +01:00
|
|
|
|
2022-01-31 20:54:18 +01:00
|
|
|
function createIncludeFile()
|
2022-01-18 19:14:24 +01:00
|
|
|
{
|
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) {
|
|
|
|
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-01-18 19:14:24 +01:00
|
|
|
}
|
2022-01-25 20:34:13 +01:00
|
|
|
|
2022-01-18 19:14:24 +01:00
|
|
|
|
2022-01-31 20:54:18 +01:00
|
|
|
function delete(int $id)
|
2022-01-18 19:14:24 +01:00
|
|
|
{
|
|
|
|
|
2022-01-31 20:54:18 +01:00
|
|
|
if ($domain = $this->domainRepository->findByID(id: $id)) {
|
|
|
|
$this->domainRepository->delete(id: $id);
|
|
|
|
$zoneFile = $this->localZonesDir . $domain['name'];
|
|
|
|
print($zoneFile . PHP_EOL);
|
|
|
|
if (file_exists(filename: $this->localZonesDir . $domain['name'])) {
|
|
|
|
print("file exists");
|
|
|
|
unlink(filename: $zoneFile);
|
|
|
|
$this->createIncludeFile();
|
2022-01-22 17:32:36 +01:00
|
|
|
}
|
2022-01-18 19:14:24 +01:00
|
|
|
}
|
2022-01-31 20:54:18 +01:00
|
|
|
|
|
|
|
$this->deleteOnNameservers(id: $id);
|
2022-01-18 19:14:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-01-31 20:54:18 +01:00
|
|
|
function deleteOnNameservers(int $id)
|
2022-01-18 19:14:24 +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) {
|
|
|
|
echo($nameserver['name']);
|
|
|
|
$body = [
|
|
|
|
'id' => $id
|
|
|
|
];
|
|
|
|
if (!empty($nameserver['aaaa'])) {
|
|
|
|
$this->checkController->sendCommand(requestType: 'DELETE', serverName: $nameserver['name'], versionIP: 6, apiKey: $nameserver['apikey'], command: 'delete', serverType: 'nameserver', body: $body);
|
|
|
|
} else {
|
|
|
|
$this->checkController->sendCommand(requestType: 'DELETE', serverName: $nameserver['name'], versionIP: 4, apiKey: $nameserver['apikey'], command: 'delete', serverType: 'nameserver', body: $body);
|
2022-01-25 20:34:13 +01:00
|
|
|
}
|
2022-01-18 19:14:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2022-01-31 20:54:18 +01:00
|
|
|
* @param int $id
|
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-01-31 20:54:18 +01:00
|
|
|
function deleteZone(int $id)
|
2022-01-18 19:14:24 +01:00
|
|
|
{
|
2022-01-31 20:54:18 +01:00
|
|
|
|
|
|
|
if ($domain = $this->domainRepository->findByID(id: $id)) {
|
2022-01-27 15:15:19 +01:00
|
|
|
$zoneFile = $this->localZonesDir . $domain['name'];
|
|
|
|
print($zoneFile . PHP_EOL);
|
|
|
|
if (file_exists(filename: $this->localZonesDir . $domain['name'])) {
|
|
|
|
print("file exists");
|
|
|
|
unlink(filename: $zoneFile);
|
2022-01-31 20:54:18 +01:00
|
|
|
$this->createIncludeFile();
|
2022-01-27 15:15:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-31 20:54:18 +01:00
|
|
|
$this->deleteOnNameservers(id: $id);
|
|
|
|
$this->domainRepository->delete(id: $id);
|
2022-01-25 20:34:13 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function checkPermissions(): void
|
|
|
|
{
|
|
|
|
echo 'Checking permission:' . PHP_EOL . PHP_EOL;
|
|
|
|
$uid = posix_geteuid();
|
|
|
|
print("UID:\t$uid" . PHP_EOL);
|
|
|
|
|
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'];
|
|
|
|
print("Name:\t$name" . PHP_EOL);
|
|
|
|
$bindGroup = posix_getgrnam(name: 'bind');
|
|
|
|
$members = $bindGroup['members'];
|
|
|
|
if (in_array(needle: $name, haystack: $members)) {
|
2022-01-31 20:54:18 +01:00
|
|
|
echo "\t✅ is in group 'bind" . PHP_EOL;
|
2022-01-25 20:34:13 +01:00
|
|
|
}
|
2022-01-31 20:54:18 +01:00
|
|
|
|
|
|
|
echo 'Checking file: ' . $this->localZoneFile . PHP_EOL;
|
2022-01-25 20:34:13 +01:00
|
|
|
$localZoneFilePermissions = fileperms(filename: $this->localZoneFile);
|
|
|
|
if ($localZoneFilePermissions & 0x0010) {
|
2022-01-31 20:54:18 +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;
|
|
|
|
}
|
|
|
|
|
|
|
|
echo "Checking $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-01-31 20:54:18 +01:00
|
|
|
echo "\t❌ $this->localZoneFile needs to be included in $this->namedConfLocalFile . " . PHP_EOL;
|
2022-01-25 20:34:13 +01:00
|
|
|
} else {
|
|
|
|
echo "\t✅ $this->localZoneFile is included in $this->namedConfLocalFile" . PHP_EOL;
|
|
|
|
}
|
|
|
|
} 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
|
|
|
}
|
|
|
|
|
|
|
|
echo 'Checking directory: ' . $this->localZonesDir . PHP_EOL;
|
|
|
|
$localZoneDirPermissions = fileperms(filename: $this->localZonesDir);
|
|
|
|
if ($localZoneDirPermissions & 0x0010) {
|
2022-01-31 20:54:18 +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
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array|bool
|
|
|
|
*/
|
|
|
|
function checkDomains(): array|bool
|
|
|
|
{
|
2022-01-26 19:36:09 +01:00
|
|
|
return true;
|
|
|
|
/*
|
2022-01-22 17:32:36 +01:00
|
|
|
$domains = $this->findAll();
|
|
|
|
|
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-01-31 20:54:18 +01:00
|
|
|
return "$this->localZoneFile needs to be included in $this->namedConfLocalFile . ";
|
2022-01-22 17:32:36 +01:00
|
|
|
}
|
|
|
|
} else {
|
2022-01-31 20:54:18 +01:00
|
|
|
return "No access to '$this->namedConfLocalFile' . Please check permissions";
|
2022-01-22 17:32:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!fileperms($this->localZoneFile)) {
|
2022-01-31 20:54:18 +01:00
|
|
|
return "No access to $this->localZoneFile . Please check permissions . ";
|
2022-01-22 17:32:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$localZones = file_get_contents($this->localZoneFile);
|
|
|
|
|
|
|
|
foreach($domains as $domain) {
|
|
|
|
if(!str_contains($localZones, $domain['name'])) {
|
|
|
|
$errors[] = $domain['name'] . " is missing in '$this->localZoneFile'";
|
|
|
|
}
|
|
|
|
|
|
|
|
$zoneFile = $this->localZonesDir . $domain['name'];
|
|
|
|
|
|
|
|
if (!file_exists($zoneFile)) {
|
2022-01-31 20:54:18 +01:00
|
|
|
$errors[] = "Missing zone file for $zoneFile . Update zone to create it";
|
2022-01-22 17:32:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (empty($errors)) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return $errors;
|
|
|
|
}
|
2022-01-26 19:36:09 +01:00
|
|
|
*/
|
2022-01-22 17:32:36 +01:00
|
|
|
}
|
|
|
|
|
2022-01-22 18:25:18 +01:00
|
|
|
|
2022-01-22 17:32:36 +01:00
|
|
|
/**
|
|
|
|
* @param mixed $name
|
|
|
|
* @param mixed $a
|
|
|
|
* @param mixed $aaaa
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2022-01-31 20:54:18 +01:00
|
|
|
public function createZoneFile(string $name, string $a, string $aaaa): void
|
2022-01-22 17:32:36 +01:00
|
|
|
{
|
|
|
|
if ($zonefile = fopen(filename: $this->localZonesDir . $name, mode: 'w')) {
|
2022-01-26 19:36:09 +01:00
|
|
|
fputs(stream: $zonefile, data: "zone \"$name\" IN {" . PHP_EOL);
|
|
|
|
fputs(stream: $zonefile, data: "\ttype slave;" . PHP_EOL);
|
|
|
|
fputs(stream: $zonefile, data: "\tfile \"" . $this->zoneCachePath . $name . '.db";' . PHP_EOL);
|
|
|
|
fputs(stream: $zonefile, data: "\tmasters {" . PHP_EOL);
|
2022-01-22 17:32:36 +01:00
|
|
|
if (!empty($a)) {
|
2022-01-26 19:36:09 +01:00
|
|
|
fputs(stream: $zonefile, data: "\t\t$a;" . PHP_EOL);
|
2022-01-22 17:32:36 +01:00
|
|
|
}
|
|
|
|
if (!empty($aaaa)) {
|
2022-01-26 19:36:09 +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-01-31 20:54:18 +01:00
|
|
|
$this->createIncludeFile();
|
2022-01-22 17:32:36 +01:00
|
|
|
}
|
2022-01-18 19:14:24 +01:00
|
|
|
}
|