240 lines
6.7 KiB
PHP
240 lines
6.7 KiB
PHP
<?php declare(strict_types=1);
|
|
|
|
namespace App\Controller;
|
|
|
|
use App\Repository\DomainRepository;
|
|
use App\Repository\NameserverRepository;
|
|
|
|
error_reporting(error_level: E_ALL);
|
|
|
|
// TODO check include "/etc/bind/local.zones";
|
|
|
|
/**
|
|
*
|
|
*/
|
|
class DomainController
|
|
{
|
|
private string $localZoneFile;
|
|
private string $localZonesDir;
|
|
private string $namedConfLocalFile;
|
|
private string $zoneCachePath;
|
|
|
|
public function __construct(private NameserverRepository $nameserverRepository, private CheckController $checkController, private DomainRepository $domainRepository)
|
|
{
|
|
$this->localZoneFile = '/etc/bind/local.zones';
|
|
$this->localZonesDir = '/etc/bind/zones/';
|
|
$this->namedConfLocalFile = '/etc/bind/named.conf.local';
|
|
$this->zoneCachePath = '/var/cache/bind/';
|
|
}
|
|
|
|
/*
|
|
/**
|
|
* @param String $name
|
|
* @param mixed $a
|
|
* @param mixed $aaaa
|
|
*
|
|
* @return void
|
|
public function createZone(string $name, mixed $a, mixed $aaaa): void
|
|
{
|
|
$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);
|
|
}
|
|
}
|
|
*/
|
|
|
|
function createIncludeFile()
|
|
{
|
|
$domains = $this->domainRepository->findAll();
|
|
|
|
$oFile = fopen(filename: $this->localZoneFile, mode: 'w');
|
|
foreach ($domains as $domain) {
|
|
fputs(stream: $oFile, data: 'include "' . $this->localZonesDir . $domain->getName() . '";' . PHP_EOL);
|
|
}
|
|
fclose(stream: $oFile);
|
|
}
|
|
|
|
|
|
function delete(int $id)
|
|
{
|
|
|
|
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();
|
|
}
|
|
}
|
|
|
|
$this->deleteOnNameservers(id: $id);
|
|
}
|
|
|
|
|
|
function deleteOnNameservers(int $id)
|
|
{
|
|
$nameservers = $this->nameserverRepository->findAll();
|
|
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);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* @param int $id
|
|
*
|
|
* @return void
|
|
*/
|
|
function deleteZone(int $id)
|
|
{
|
|
|
|
if ($domain = $this->domainRepository->findByID(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();
|
|
}
|
|
}
|
|
|
|
$this->deleteOnNameservers(id: $id);
|
|
$this->domainRepository->delete(id: $id);
|
|
|
|
}
|
|
|
|
/**
|
|
* @return void
|
|
*/
|
|
function checkPermissions(): void
|
|
{
|
|
echo 'Checking permission:' . PHP_EOL . PHP_EOL;
|
|
$uid = posix_geteuid();
|
|
print("UID:\t$uid" . PHP_EOL);
|
|
|
|
$pwuid = posix_getpwuid(user_id: $uid);
|
|
$name = $pwuid['name'];
|
|
print("Name:\t$name" . PHP_EOL);
|
|
$bindGroup = posix_getgrnam(name: 'bind');
|
|
$members = $bindGroup['members'];
|
|
if (in_array(needle: $name, haystack: $members)) {
|
|
echo "\t✅ is in group 'bind" . PHP_EOL;
|
|
}
|
|
|
|
echo 'Checking file: ' . $this->localZoneFile . PHP_EOL;
|
|
$localZoneFilePermissions = fileperms(filename: $this->localZoneFile);
|
|
if ($localZoneFilePermissions & 0x0010) {
|
|
echo "\t✅ Group has write access . " . PHP_EOL;
|
|
} else {
|
|
echo "\t❌Group needs write permission!" . PHP_EOL;
|
|
}
|
|
|
|
echo "Checking $this->namedConfLocalFile" . PHP_EOL;
|
|
if ($namedConfLocal = file_get_contents(filename: $this->namedConfLocalFile)) {
|
|
if (!str_contains(haystack: $namedConfLocal, needle: $this->localZoneFile)) {
|
|
echo "\t❌ $this->localZoneFile needs to be included in $this->namedConfLocalFile . " . PHP_EOL;
|
|
} else {
|
|
echo "\t✅ $this->localZoneFile is included in $this->namedConfLocalFile" . PHP_EOL;
|
|
}
|
|
} else {
|
|
echo "\t❌ No access to '$this->namedConfLocalFile' . Please check permissions" . PHP_EOL;
|
|
}
|
|
|
|
echo 'Checking directory: ' . $this->localZonesDir . PHP_EOL;
|
|
$localZoneDirPermissions = fileperms(filename: $this->localZonesDir);
|
|
if ($localZoneDirPermissions & 0x0010) {
|
|
echo "\t✅ Group has write access . " . PHP_EOL;
|
|
} else {
|
|
echo "\t❌Group needs write permission!" . PHP_EOL;
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* @return array|bool
|
|
*/
|
|
function checkDomains(): array|bool
|
|
{
|
|
return true;
|
|
/*
|
|
$domains = $this->findAll();
|
|
|
|
if ($namedConfLocal = file_get_contents(filename: $this->namedConfLocalFile)) {
|
|
if (!str_contains(haystack: $namedConfLocal, needle: $this->localZoneFile)) {
|
|
return "$this->localZoneFile needs to be included in $this->namedConfLocalFile . ";
|
|
}
|
|
} else {
|
|
return "No access to '$this->namedConfLocalFile' . Please check permissions";
|
|
}
|
|
|
|
if (!fileperms($this->localZoneFile)) {
|
|
return "No access to $this->localZoneFile . Please check permissions . ";
|
|
}
|
|
|
|
$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)) {
|
|
$errors[] = "Missing zone file for $zoneFile . Update zone to create it";
|
|
}
|
|
}
|
|
|
|
if (empty($errors)) {
|
|
return true;
|
|
} else {
|
|
return $errors;
|
|
}
|
|
*/
|
|
}
|
|
|
|
|
|
/**
|
|
* @param mixed $name
|
|
* @param mixed $a
|
|
* @param mixed $aaaa
|
|
*
|
|
* @return void
|
|
*/
|
|
public function createZoneFile(string $name, string $a, string $aaaa): void
|
|
{
|
|
if ($zonefile = fopen(filename: $this->localZonesDir . $name, mode: 'w')) {
|
|
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);
|
|
if (!empty($a)) {
|
|
fputs(stream: $zonefile, data: "\t\t$a;" . PHP_EOL);
|
|
}
|
|
if (!empty($aaaa)) {
|
|
fputs(stream: $zonefile, data: "\t\t$aaaa;" . PHP_EOL);
|
|
}
|
|
fputs(stream: $zonefile, data: "\t};" . PHP_EOL);
|
|
fputs(stream: $zonefile, data: "};" . PHP_EOL);
|
|
}
|
|
$this->createIncludeFile();
|
|
}
|
|
} |