added logger

This commit is contained in:
tracer 2022-09-17 16:27:01 +02:00
parent d1325202ee
commit b25f0ab1eb
1 changed files with 25 additions and 42 deletions

View File

@ -28,15 +28,9 @@ class DomainController
private readonly ApiController $checkController,
private readonly DomainRepository $domainRepository,
private readonly PanelRepository $panelRepository,
private readonly bool $verbose,
private readonly bool $debug,
private readonly Logger $log)
private readonly ConfigController $configController,
private readonly Logger $logger)
{
if ($this->$debug) {
$this->log->debug(message: "__construct()");
}
$this->localZoneFile = '/etc/bind/local.zones';
$this->localZonesDir = '/etc/bind/zones/';
$this->namedConfLocalFile = '/etc/bind/named.conf.local';
@ -46,9 +40,7 @@ class DomainController
function createIncludeFile(): void
{
if ($this->debug) {
$this->log->debug(message: "createIncludeFile()");
}
$this->logger->debug(message: "createIncludeFile()");
$domains = $this->domainRepository->findAll();
@ -76,9 +68,7 @@ class DomainController
function updateSlaveZones(): void
{
if ($this->verbose) {
echo 'Delete all slave zones';
}
$this->logger->debug(message: 'Delete all slave zones');
$zones = glob(pattern: $this->localZonesDir . '*');
foreach ($zones as $zone) {
@ -88,7 +78,7 @@ class DomainController
$domains = $this->domainRepository->findAll();
foreach ($domains as $domain) {
if ($this->verbose) {
if ($this->configController->getConfig(configKey: 'verbose')) {
echo 'Create zone: ' . $domain->getName() . PHP_EOL;
}
$this->createSlaveZoneFile(domain: $domain);
@ -100,9 +90,7 @@ class DomainController
function deleteOnNameservers(Domain $domain): void
{
if ($this->debug) {
$this->log->debug(message: "deleteOnNameserver()");
}
$this->logger->debug(message: "deleteOnNameserver()");
$nameservers = $this->nameserverRepository->findAll();
foreach ($nameservers as $nameserver) {
@ -125,9 +113,7 @@ class DomainController
*/
function deleteZone(Domain $domain): void
{
if ($this->debug) {
$this->log->debug(message: "deleteZone()");
}
$this->logger->debug(message: "deleteZone()");
$zoneFile = $this->localZonesDir . $domain->getName();
if (file_exists(filename: "$zoneFile")) {
@ -144,80 +130,79 @@ class DomainController
function checkPermissions(): bool
{
$setupIsValid = true;
$verbose = $this->configController->getConfig(configKey: 'verbose');
if ($this->debug) {
$this->log->debug(message: "checkPermissions()");
}
$this->logger->debug(message: "checkPermissions()");
if ($this->verbose) {
if ($verbose) {
echo 'Checking permissions...' . PHP_EOL;
}
$uid = posix_geteuid();
if ($this->verbose) {
if ($verbose) {
echo "UID:\t" . COLOR_YELLOW . $uid . PHP_EOL;
}
$pwuid = posix_getpwuid(user_id: $uid);
$name = $pwuid['name'];
if ($this->verbose) {
if ($verbose) {
echo COLOR_DEFAULT . "Name:\t" . COLOR_YELLOW . $name . PHP_EOL;
}
$bindGroup = posix_getgrnam(name: 'bind');
$members = $bindGroup['members'];
if (in_array(needle: $name, haystack: $members)) {
if ($this->verbose) {
if ($verbose) {
echo "\t$name" . COLOR_DEFAULT . ' is in group ' . COLOR_YELLOW . 'bind' . PHP_EOL;
}
} else {
$setupIsValid = false;
if ($this->verbose) {
if ($verbose) {
echo COLOR_RED . "\t$name needs to be in group " . COLOR_YELLOW . 'bind' . COLOR_DEFAULT . '!' . PHP_EOL;
}
}
if ($this->verbose) {
if ($verbose) {
echo COLOR_DEFAULT . 'Checking ' . COLOR_YELLOW . $this->localZoneFile . PHP_EOL;
}
$localZoneFilePermissions = @fileperms(filename: $this->localZoneFile);
if ($localZoneFilePermissions & 0x0010) {
if ($this->verbose) {
if ($verbose) {
echo COLOR_DEFAULT . "\t✅ Group has write access." . PHP_EOL;
}
} else {
$setupIsValid = false;
if ($this->verbose) {
if ($verbose) {
echo COLOR_RED . "\t❌Group needs write permission!" . COLOR_DEFAULT . PHP_EOL;
}
}
if ($this->verbose) {
if ($verbose) {
echo 'Checking ' . COLOR_YELLOW . $this->namedConfLocalFile . PHP_EOL;
}
if ($namedConfLocal = file_get_contents(filename: $this->namedConfLocalFile)) {
if (!str_contains(haystack: $namedConfLocal, needle: $this->localZoneFile)) {
$setupIsValid = false;
if ($this->verbose) {
if ($verbose) {
echo "\t$this->localZoneFile" . COLOR_RED . ' needs to be included in ' . COLOR_YELLOW . $this->namedConfLocalFile . PHP_EOL;
}
} else {
if ($this->verbose) {
if ($verbose) {
echo "\t$this->localZoneFile" . COLOR_DEFAULT . ' is included in ' . COLOR_YELLOW . $this->namedConfLocalFile . PHP_EOL;
}
}
} else {
$setupIsValid = false;
if ($this->verbose) {
if ($verbose) {
echo "\t❌ No access to '$this->namedConfLocalFile' . Please check permissions" . PHP_EOL;
}
}
if ($this->verbose) {
if ($verbose) {
echo COLOR_DEFAULT . 'Checking directory: ' . COLOR_YELLOW . $this->localZonesDir . PHP_EOL;
}
$localZoneDirPermissions = @fileperms(filename: $this->localZonesDir);
if ($localZoneDirPermissions & 0x0010) {
if ($this->verbose) {
if ($verbose) {
echo "\t✅ Group has write access." . PHP_EOL;
}
} else {
$setupIsValid = false;
if ($this->verbose) {
if ($verbose) {
echo COLOR_RED . "\t❌Group needs write permission!" . PHP_EOL;
}
}
@ -273,9 +258,7 @@ class DomainController
public function createSlaveZoneFile(Domain $domain): void
{
$domainName = $domain->getName();
if ($this->debug) {
$this->log->debug(message: "createZoneFile($domainName)");
}
$this->logger->info(message: "createZoneFile($domainName)");
// check if we're a master zone
if ($this->isMasterZone(domain: $domain)) {