reworked most of the check commands

This commit is contained in:
tracer 2024-04-16 18:24:38 +02:00
parent d115a5d775
commit 43698c0fae
5 changed files with 1968 additions and 1899 deletions

View File

@ -2,7 +2,7 @@
"name": "24unix/bindapi",
"description": "manage Bind9 DNS server via REST API",
"version": "2023.0.1",
"build_number": "332",
"build_number": "333",
"authors": [
{
"name": "Micha Espey",

View File

@ -53,7 +53,8 @@ class BindAPI
ConfigController::class => autowire()
->constructorParameter(parameter: 'quiet', value: $quiet),
CLIController::class => autowire()
->constructorParameter(parameter: 'logger', value: $this->logger),
->constructorParameter(parameter: 'logger', value: $this->logger)
->constructorParameter(parameter: 'quiet', value: $quiet),
DomainController::class => autowire()
->constructorParameter(parameter: 'logger', value: $this->logger)
->constructorParameter(parameter: 'quiet', value: $quiet),

View File

@ -62,7 +62,9 @@
private readonly PanelRepository $panelRepository,
private readonly ConfigController $configController,
private readonly EncryptionController $encryptionController,
private $logger)
private $logger,
private bool $quiet
)
{
// FIXME needs to be elsewhere $this->runCheckSetup();
@ -490,7 +492,9 @@
echo "Unknown panel ID $id" . PHP_EOL;
}
} else {
if (!$this->quiet) {
echo "check all …" . PHP_EOL;
}
$panels = $this->panelRepository->findAll();
foreach ($panels as $panel) {
$this->checkSinglePanel(panel: $panel);
@ -509,13 +513,17 @@
{
$this->logger->debug(message: "checkSinglePanel()");
if (!$this->quiet) {
echo COLOR_DEFAULT . 'KeyHelp-Panel: ' . COLOR_YELLOW . $panel->getName() . COLOR_DEFAULT;
}
$encryptionKey = $this->configController->getConfig(configKey: 'encryptionKey');
$decryptedKey = $this->encryptionController->safeDecrypt(encrypted: $panel->getApikey(), key: $encryptionKey);
$f = $panel->getA();
if (!$this->quiet) {
echo COLOR_DEFAULT . ' IPv4: ' . COLOR_YELLOW . $f . COLOR_DEFAULT;
}
if (!empty($panel->getA())) {
$panelRequest = $this->apiController->sendCommand(
@ -542,7 +550,9 @@
$panelVersion = 'n/a';
$responseTime = 'n/a';
}
if (!$this->quiet) {
echo COLOR_DEFAULT . ' KeyHelp version: ' . $panelVersion . " ($responseTime seconds)" . PHP_EOL;
}
if (empty($panel->getA())) {
$result = $this->apiController->sendCommand(
@ -564,13 +574,17 @@
}
if (!empty($result['error'])) {
if (!$this->quiet) {
echo $result['data'] . PHP_EOL;
}
exit(1);
}
if (!empty($result['data'])) {
$domains = json_decode(json: $result['data']);
} else {
if (!$this->quiet) {
echo 'No domains found' . PHP_EOL;
}
exit(1);
}
@ -597,7 +611,9 @@
$domainCount = 0;
foreach ($tmpDomainList as $domain) {
if (!$this->quiet) {
echo COLOR_DEFAULT . " Domain: " . COLOR_YELLOW . str_pad(string: $domain->getDomain(), length: $maxDomainNameLength);
}
if (!$domain->isSubdomain()) {
$this->checkNS(domainName: $domain->getDomain(), panel: $panel);
@ -606,9 +622,13 @@
}
if ($domainCount == 0) {
if (!$this->quiet) {
echo 'No second level domains found.' . COLOR_DEFAULT . PHP_EOL;
}
}
if (!$this->quiet) {
echo PHP_EOL;
}
try {
sodium_memzero(string: $decryptedKey);
@ -704,7 +724,9 @@
$encryptionKey = $this->configController->getConfig(configKey: 'encryptionKey');
$decryptedKey = $this->encryptionController->safeDecrypt(encrypted: $nameserver->getApikey(), key: $encryptionKey);
if (!$this->quiet) {
echo COLOR_YELLOW . ' ' . $nameserver->getName();
}
if (!empty($nameserver->getName())) {
$result = $this->apiController->sendCommand(
requestType: 'GET',
@ -725,10 +747,14 @@
switch ($result['header']) {
case 200:
if (!$this->quiet) {
echo COLOR_GREEN . ' OK';
}
break;
case 404:
if (!$this->quiet) {
echo COLOR_RED . ' ' . $result['header'] . COLOR_DEFAULT;
}
if (!empty($this->arguments['fix']) && $this->arguments['fix'] == 'yes') {
if (!$this->quiet) {
echo ' trying to fix …';
@ -763,17 +789,23 @@
print_r(value: $create);
die("make error handling");
} else {
if (!$this->quiet) {
echo COLOR_GREEN . 'OK' . COLOR_DEFAULT;
}
}
}
break;
default:
if (!$this->quiet) {
echo 'Server error' . PHP_EOL;
}
exit(1);
}
}
if (!$this->quiet) {
echo PHP_EOL;
}
}
/**
* @param array $arguments
@ -1713,12 +1745,14 @@
foreach ($panels as $panel) {
if (!$this->quiet) {
echo COLOR_DEFAULT . 'Checking panel ' . COLOR_YELLOW . $panel->getName() . COLOR_DEFAULT . PHP_EOL;
$longestEntry = $this->domainRepository->getLongestEntry(field: 'name');
}
$encryptionKey = $this->configController->getConfig(configKey: 'encryptionKey');
$decryptedKey = $this->encryptionController->safeDecrypt(encrypted: $panel->getApikey(), key: $encryptionKey);
$currentDomains = $this->domainRepository->findByPanel(name: $panel->getName());
var_dump($currentDomains);
if (empty($panel->getA())) {
$result = $this->apiController->sendCommand(
@ -1754,24 +1788,29 @@
if (count($domains) > 0) {
foreach ($domains as $domain) {
$domainCount++;
echo COLOR_YELLOW . ' ' . $domain->domain;
echo PHP_EOL;
if (!$this->quiet) {
echo COLOR_YELLOW . ' ' .str_pad(string: $domain->domain, length: $longestEntry + 1, pad_type: STR_PAD_RIGHT);
}
if ($domain = $this->domainRepository->findByName(name: $domain->domain)) {
$currentPanel = $domain->getPanel();
$panelName = $panel->getName();
echo 'current Panel: ' . $panelName . PHP_EOL;
echo 'panel name: ' . $panelName . PHP_EOL;
if (strcmp(string1: $currentPanel, string2: $panelName)) {
$domain->setPanel(panel: $panelName);
}
$this->domainRepository->update(domain: $domain);
if (!$this->quiet) {
echo COLOR_DEFAULT . ' updated to: ' . COLOR_YELLOW . $panelName;
}
}
if (!$this->quiet) {
echo COLOR_GREEN . ' OK' . COLOR_DEFAULT . PHP_EOL;
}
} else {
$newDomain = new Domain(name: $domain->getName(), panel: $panel->getName());
$result = $this->domainRepository->insert(domain: $newDomain);
if (!$this->quiet) {
echo COLOR_DEFAULT . ' has been created with id ' . COLOR_YELLOW . $result . COLOR_DEFAULT . '.' . PHP_EOL;
}
}
unset($currentDomains[$domain->getName()]);
}
}
@ -1789,6 +1828,9 @@
}
}
if (!$this->quiet) {
echo 'Creating slave zone files' . PHP_EOL;
}
$this->domainController->updateSlaveZones();
}

View File

@ -78,12 +78,17 @@ class DomainController
}
$domains = $this->domainRepository->findAll();
$longestEntry = $this->domainRepository->getLongestEntry('name');
foreach ($domains as $domain) {
if (!$this->quiet) {
echo 'Create zone: ' . $domain->getName() . PHP_EOL;
echo ' ' . COLOR_YELLOW . str_pad($domain->getName(), $longestEntry + 1, " ", STR_PAD_RIGHT) ;
}
if ($this->createSlaveZoneFile(domain: $domain)) {
if (!$this->quiet) {
echo COLOR_GREEN . ' OK' . COLOR_DEFAULT . PHP_EOL;
}
}
$this->createSlaveZoneFile(domain: $domain);
}
$this->createIncludeFile();
@ -234,7 +239,9 @@ class DomainController
function checkDomains(): void
{
if (!file_exists(filename: $this->localZoneFile)) {
if (!$this->quiet) {
echo COLOR_DEFAULT . 'Local Zone file ' . COLOR_YELLOW . $this->localZoneFile . COLOR_DEFAULT . ' does not exist.' . PHP_EOL;
}
exit(1);
}
$localZones = file_get_contents(filename: $this->localZoneFile);
@ -243,20 +250,28 @@ class DomainController
foreach ($domains as $domain) {
$idString = '(' . $domain->getId() . ') ';
if (!$this->quiet) {
echo COLOR_YELLOW .
str_pad(string: $domain->getName(), length: $maxNameLength + 1)
. COLOR_DEFAULT
. str_pad(string: $idString, length: 7, pad_type: STR_PAD_LEFT);
}
$hasError = false;
if ($this->isMasterZone(domain: $domain)) {
echo 'Master Zone lies on this panel.';
if (!$this->quiet) {
echo COLOR_GREEN . 'Master Zone';
}
} else {
if (!str_contains(haystack: $localZones, needle: $domain->getName())) {
if (!$this->quiet) {
echo COLOR_RED . 'is missing in ' . COLOR_YELLOW . $this->localZoneFile . COLOR_DEFAULT;
}
$hasError = true;
} else {
if (!$this->quiet) {
echo COLOR_GREEN . 'OK';
}
}
$zoneFile = $this->localZonesDir . $domain->getName();
@ -270,8 +285,10 @@ class DomainController
echo " Update zone (Domain) to create it.";
}
}
if (!$this->quiet) {
echo COLOR_DEFAULT . PHP_EOL;
}
}
}
@ -281,7 +298,7 @@ class DomainController
*
* @return void
*/
public function createSlaveZoneFile(Domain $domain): void
public function createSlaveZoneFile(Domain $domain): bool
{
$domainName = $domain->getName();
$this->logger->info(message: "createZoneFile($domainName)");
@ -289,14 +306,16 @@ class DomainController
// check if we're a master zone
if ($this->isMasterZone(domain: $domain)) {
//echo 'We are zone master for ' . $domainName . PHP_EOL;
return;
return true;
}
if ($zoneFile = fopen(filename: $this->localZonesDir . $domainName, mode: 'w')) {
$panelName = $domain->getPanel();
if (!$panel = $this->panelRepository->findByName(name: $panelName)) {
if (!$this->quiet) {
echo "Error: Panel $panelName doesn't exist." . PHP_EOL;
exit(1);
}
return false;
}
$a = $panel->getA();
$aaaa = $panel->getAaaa();
@ -312,6 +331,12 @@ class DomainController
}
fputs(stream: $zoneFile, data: "\t};" . PHP_EOL);
fputs(stream: $zoneFile, data: "};" . PHP_EOL);
return true;
} else {
if (!$this->quiet) {
echo COLOR_RED . ' Error: ' . COLOR_DEFAULT . 'unable to create ' . COLOR_YELLOW . $this->localZonesDir . $domainName . COLOR_DEFAULT . PHP_EOL;
}
return false;
}
}

View File

@ -219,6 +219,7 @@ readonly class DomainRepository
$domainName = $domain->getName();
$this->logger->debug(message: "delete($domainName)");
// FIXME, add force parameter, reject deletion if domains left on panel
$sql = "
DELETE FROM " . DatabaseConnection::TABLE_DOMAINS . "
WHERE id = :id";