some fixes

This commit is contained in:
tracer 2024-04-14 20:14:41 +02:00
parent 9e9540d351
commit d115a5d775
10 changed files with 49 additions and 49 deletions

View File

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

View File

@ -4,7 +4,7 @@
require 'vendor/autoload.php'; require 'vendor/autoload.php';
$configController = new ConfigController(); $configController = new ConfigController(quiet: true);
$dbHost = $configController->getConfig(configKey: 'dbHost'); $dbHost = $configController->getConfig(configKey: 'dbHost');
$dbPort = $configController->getConfig(configKey: 'dbPort'); $dbPort = $configController->getConfig(configKey: 'dbPort');

View File

@ -31,7 +31,7 @@ class ApiController
'nameserver' => curl_setopt(handle: $curl, option: CURLOPT_URL, value: "https://$serverName/api/" . $command) 'nameserver' => curl_setopt(handle: $curl, option: CURLOPT_URL, value: "https://$serverName/api/" . $command)
}; };
} catch (UnhandledMatchError) { } catch (UnhandledMatchError) {
echo 'Unhandled match: ' . $serverType; exit('Unhandled match: ' . $serverType .' in ' . __FILE__ . ' on line ' . __LINE__ . PHP_EOL);
} }
curl_setopt(handle: $curl, option: CURLOPT_RETURNTRANSFER, value: 1); curl_setopt(handle: $curl, option: CURLOPT_RETURNTRANSFER, value: 1);

View File

@ -28,14 +28,14 @@ class BindAPI
/** /**
* @throws Exception * @throws Exception
*/ */
public function __construct() public function __construct(bool $quiet)
{ {
// init the logger // init the logger
$dateFormat = "Y:m:d H:i:s"; $dateFormat = "Y:m:d H:i:s";
$output = "%datetime% %channel%.%level_name% %message%\n"; // %context% %extra% $output = "%datetime% %channel%.%level_name% %message%\n"; // %context% %extra%
$formatter = new LineFormatter(format: $output, dateFormat: $dateFormat); $formatter = new LineFormatter(format: $output, dateFormat: $dateFormat);
$debug = (new ConfigController)->getConfig(configKey: 'debug'); $debug = (new ConfigController(quiet: $quiet))->getConfig(configKey: 'debug');
if ($debug) { if ($debug) {
$stream = new StreamHandler(stream: dirname(path: __DIR__, levels: 2) . '/bindAPI.log', level: Level::Debug); $stream = new StreamHandler(stream: dirname(path: __DIR__, levels: 2) . '/bindAPI.log', level: Level::Debug);
} else { } else {
@ -50,12 +50,14 @@ class BindAPI
$containerBuilder = new ContainerBuilder(); $containerBuilder = new ContainerBuilder();
$containerBuilder->addDefinitions([ $containerBuilder->addDefinitions([
ConfigController::class => autowire(), ConfigController::class => autowire()
->constructorParameter(parameter: 'quiet', value: $quiet),
CLIController::class => autowire() CLIController::class => autowire()
->constructorParameter(parameter: 'logger', value: $this->logger), ->constructorParameter(parameter: 'logger', value: $this->logger),
DomainController::class => autowire() DomainController::class => autowire()
->constructorParameter(parameter: 'logger', value: $this->logger), ->constructorParameter(parameter: 'logger', value: $this->logger)
DomainRepository::class => autowire() ->constructorParameter(parameter: 'quiet', value: $quiet),
DomainRepository::class => autowire()
->constructorParameter(parameter: 'logger', value: $this->logger), ->constructorParameter(parameter: 'logger', value: $this->logger),
DynDnsRepository::class => autowire() DynDnsRepository::class => autowire()
->constructorParameter(parameter: 'logger', value: $this->logger), ->constructorParameter(parameter: 'logger', value: $this->logger),

View File

@ -296,7 +296,7 @@
function runCheckSetup(): void function runCheckSetup(): void
{ {
if (!$this->domainController->checkPermissions(quiet: true)) { if (!$this->domainController->checkPermissions()) {
echo COLOR_RED . 'You need to setup the bindAPI permission first.' . COLOR_DEFAULT . PHP_EOL; echo COLOR_RED . 'You need to setup the bindAPI permission first.' . COLOR_DEFAULT . PHP_EOL;
echo 'Run ' . COLOR_YELLOW . './bin/console check:setup' . COLOR_DEFAULT . ' as root or with sudo.' . PHP_EOL; echo 'Run ' . COLOR_YELLOW . './bin/console check:setup' . COLOR_DEFAULT . ' as root or with sudo.' . PHP_EOL;
} }
@ -514,11 +514,14 @@
$encryptionKey = $this->configController->getConfig(configKey: 'encryptionKey'); $encryptionKey = $this->configController->getConfig(configKey: 'encryptionKey');
$decryptedKey = $this->encryptionController->safeDecrypt(encrypted: $panel->getApikey(), key: $encryptionKey); $decryptedKey = $this->encryptionController->safeDecrypt(encrypted: $panel->getApikey(), key: $encryptionKey);
if (empty($panel->getA())) { $f = $panel->getA();
echo COLOR_DEFAULT . ' IPv4: ' . COLOR_YELLOW . $f . COLOR_DEFAULT;
if (!empty($panel->getA())) {
$panelRequest = $this->apiController->sendCommand( $panelRequest = $this->apiController->sendCommand(
requestType: 'GET', requestType: 'GET',
serverName : $panel->getName(), serverName : $panel->getName(),
versionIP : 6, versionIP : 4,
apiKey : $decryptedKey, apiKey : $decryptedKey,
command : '/server', command : '/server',
serverType : 'panel'); serverType : 'panel');
@ -526,7 +529,7 @@
$panelRequest = $this->apiController->sendCommand( $panelRequest = $this->apiController->sendCommand(
requestType: 'GET', requestType: 'GET',
serverName : $panel->getName(), serverName : $panel->getName(),
versionIP : 4, versionIP : 6,
apiKey : $decryptedKey, apiKey : $decryptedKey,
command : '/server', command : '/server',
serverType : 'panel'); serverType : 'panel');
@ -727,7 +730,9 @@
case 404: case 404:
echo COLOR_RED . ' ' . $result['header'] . COLOR_DEFAULT; echo COLOR_RED . ' ' . $result['header'] . COLOR_DEFAULT;
if (!empty($this->arguments['fix']) && $this->arguments['fix'] == 'yes') { if (!empty($this->arguments['fix']) && $this->arguments['fix'] == 'yes') {
echo ' trying to fix …'; if (!$this->quiet) {
echo ' trying to fix …';
}
$body = [ $body = [
'name' => $domainName, 'name' => $domainName,
'panel' => $panel->getName(), 'panel' => $panel->getName(),

View File

@ -75,7 +75,7 @@ class CommandGroupContainer
// check for command group and print available commands // check for command group and print available commands
foreach ($this->commandGroups as $group) { foreach ($this->commandGroups as $group) {
if ($group->getName() === $command) { if ($group->getName() === $command) {
echo 'Available subcommands for : ' . COLOR_YELLOW . $group->getName() . COLOR_DEFAULT . ':' . PHP_EOL; echo 'Available subcommands for: ' . COLOR_YELLOW . $group->getName() . COLOR_DEFAULT . ':' . PHP_EOL;
$group->printCommands(strlen(string: $group->getName())); $group->printCommands(strlen(string: $group->getName()));
exit(0); exit(0);
} }

View File

@ -9,7 +9,7 @@ class ConfigController
{ {
private array $config; private array $config;
public function __construct( bool $test = false) public function __construct(bool $quiet, bool $test = false)
{ {
if ($test) { if ($test) {
@ -46,17 +46,13 @@ class ConfigController
$this->config = json_decode(json: $configJSON, associative: true); $this->config = json_decode(json: $configJSON, associative: true);
$this->config['quiet'] = (bool)$quiet;
$this->config['test'] = (bool)$test; $this->config['test'] = (bool)$test;
} }
public function getConfig(string $configKey): string public function getConfig(string $configKey): string
{ {
if ($configKey === 'quiet') { return $this->config[$configKey];
echo 'FIXME: handle quiet …';
return '';
} else {
return $this->config[$configKey];
}
} }
} }

View File

@ -52,8 +52,8 @@ class DatabaseConnection
$result = $statement->fetch(); $result = $statement->fetch();
if (empty($result)) { if (empty($result)) {
// ALTER TABLE `domains` ADD `panel_id` INT NULL AFTER `id`; // ALTER TABLE `domains` ADD `panel_id` INT NULL AFTER `id`;
echo 'Error: Cannot find tables.' . PHP_EOL; echo COLOR_RED . 'Error: ' . COLOR_DEFAULT . 'Cannot find tables.' . PHP_EOL;
echo 'run the migration …' . PHP_EOL; echo 'Run the migration: ' . COLOR_YELLOW . './bin/console migrations:make' . COLOR_DEFAULT . PHP_EOL;
} }
} catch (PDOException $exception) { } catch (PDOException $exception) {
echo $exception->getMessage() . PHP_EOL; echo $exception->getMessage() . PHP_EOL;

View File

@ -29,7 +29,9 @@ class DomainController
private readonly DomainRepository $domainRepository, private readonly DomainRepository $domainRepository,
private readonly PanelRepository $panelRepository, private readonly PanelRepository $panelRepository,
private readonly ConfigController $configController, private readonly ConfigController $configController,
private readonly Logger $logger) private readonly Logger $logger,
private readonly bool $quiet
)
{ {
$this->localZoneFile = '/etc/bind/local.zones'; $this->localZoneFile = '/etc/bind/local.zones';
$this->localZonesDir = '/etc/bind/zones/'; $this->localZonesDir = '/etc/bind/zones/';
@ -78,8 +80,9 @@ class DomainController
$domains = $this->domainRepository->findAll(); $domains = $this->domainRepository->findAll();
foreach ($domains as $domain) { foreach ($domains as $domain) {
// FIXME check for quiet if (!$this->quiet) {
echo 'Create zone: ' . $domain->getName() . PHP_EOL; echo 'Create zone: ' . $domain->getName() . PHP_EOL;
}
$this->createSlaveZoneFile(domain: $domain); $this->createSlaveZoneFile(domain: $domain);
} }
@ -137,18 +140,12 @@ class DomainController
} }
function checkPermissions(bool $quiet = false, $impersonatedUserId = null): bool function checkPermissions($impersonatedUserId = null): bool
{ {
$this->logger->debug(message: "checkPermissions()"); $this->logger->debug(message: "checkPermissions()");
$setupIsValid = true; $setupIsValid = true;
if (!$quiet) { if (!$this->quiet) {
$quiet = $this->configController->getConfig(configKey: 'quiet');
}
// echo 'quiet: ' . ($quiet ? 'true' : 'false') . PHP_EOL;
if (!$quiet) {
echo 'Checking permissions...' . PHP_EOL; echo 'Checking permissions...' . PHP_EOL;
} }
if ($impersonatedUserId) { if ($impersonatedUserId) {
@ -156,12 +153,12 @@ class DomainController
} else { } else {
$uid = posix_geteuid(); $uid = posix_geteuid();
} }
if (!$quiet) { if (!$this->quiet) {
echo "UID:\t" . COLOR_YELLOW . $uid . PHP_EOL; echo "UID:\t" . COLOR_YELLOW . $uid . PHP_EOL;
} }
$pwuid = posix_getpwuid(user_id: $uid); $pwuid = posix_getpwuid(user_id: $uid);
$name = $pwuid['name']; $name = $pwuid['name'];
if (!$quiet) { if (!$this->quiet) {
echo COLOR_DEFAULT . "Name:\t" . COLOR_YELLOW . $name . PHP_EOL; echo COLOR_DEFAULT . "Name:\t" . COLOR_YELLOW . $name . PHP_EOL;
} }
@ -170,60 +167,60 @@ class DomainController
} }
$members = $bindGroup['members'] ?? []; $members = $bindGroup['members'] ?? [];
if (in_array(needle: $name, haystack: $members)) { if (in_array(needle: $name, haystack: $members)) {
if (!$quiet) { if (!$this->quiet) {
echo "\t$name" . COLOR_DEFAULT . ' is in group ' . COLOR_YELLOW . 'bind' . PHP_EOL; echo "\t$name" . COLOR_DEFAULT . ' is in group ' . COLOR_YELLOW . 'bind' . PHP_EOL;
} }
} else { } else {
$setupIsValid = false; $setupIsValid = false;
if (!$quiet) { if (!$this->quiet) {
echo COLOR_RED . "\t$name needs to be in group " . COLOR_YELLOW . 'bind' . COLOR_DEFAULT . '!' . PHP_EOL; echo COLOR_RED . "\t$name needs to be in group " . COLOR_YELLOW . 'bind' . COLOR_DEFAULT . '!' . PHP_EOL;
} }
} }
if (!$quiet) { if (!$this->quiet) {
echo COLOR_DEFAULT . 'Checking ' . COLOR_YELLOW . $this->localZoneFile . PHP_EOL; echo COLOR_DEFAULT . 'Checking ' . COLOR_YELLOW . $this->localZoneFile . PHP_EOL;
} }
$localZoneFilePermissions = @fileperms(filename: $this->localZoneFile); $localZoneFilePermissions = @fileperms(filename: $this->localZoneFile);
if ($localZoneFilePermissions & 0x0010) { if ($localZoneFilePermissions & 0x0010) {
if (!$quiet) { if (!$this->quiet) {
echo COLOR_DEFAULT . "\t✅ Group has write access." . PHP_EOL; echo COLOR_DEFAULT . "\t✅ Group has write access." . PHP_EOL;
} }
} else { } else {
$setupIsValid = false; $setupIsValid = false;
if (!$quiet) { if (!$this->quiet) {
echo COLOR_RED . "\t❌Group needs write permission!" . COLOR_DEFAULT . PHP_EOL; echo COLOR_RED . "\t❌Group needs write permission!" . COLOR_DEFAULT . PHP_EOL;
} }
} }
if (!$quiet) { if (!$this->quiet) {
echo 'Checking ' . COLOR_YELLOW . $this->namedConfLocalFile . PHP_EOL; echo 'Checking ' . COLOR_YELLOW . $this->namedConfLocalFile . PHP_EOL;
} }
if (file_exists(filename: $this->namedConfLocalFile) && $namedConfLocal = file_get_contents(filename: $this->namedConfLocalFile)) { if (file_exists(filename: $this->namedConfLocalFile) && $namedConfLocal = file_get_contents(filename: $this->namedConfLocalFile)) {
if (!str_contains(haystack: $namedConfLocal, needle: $this->localZoneFile)) { if (!str_contains(haystack: $namedConfLocal, needle: $this->localZoneFile)) {
$setupIsValid = false; $setupIsValid = false;
if (!$quiet) { if (!$this->quiet) {
echo "\t$this->localZoneFile" . COLOR_RED . ' needs to be included in ' . COLOR_YELLOW . $this->namedConfLocalFile . PHP_EOL; echo "\t$this->localZoneFile" . COLOR_RED . ' needs to be included in ' . COLOR_YELLOW . $this->namedConfLocalFile . PHP_EOL;
} }
} else { } else {
if (!$quiet) { if (!$this->quiet) {
echo "\t$this->localZoneFile" . COLOR_DEFAULT . ' is included in ' . COLOR_YELLOW . $this->namedConfLocalFile . PHP_EOL; echo "\t$this->localZoneFile" . COLOR_DEFAULT . ' is included in ' . COLOR_YELLOW . $this->namedConfLocalFile . PHP_EOL;
} }
} }
} else { } else {
$setupIsValid = false; $setupIsValid = false;
if (!$quiet) { if (!$this->quiet) {
echo "\t❌ No access to '$this->namedConfLocalFile' . Please check permissions" . PHP_EOL; echo "\t❌ No access to '$this->namedConfLocalFile' . Please check permissions" . PHP_EOL;
} }
} }
if (!$quiet) { if (!$this->quiet) {
echo COLOR_DEFAULT . 'Checking directory: ' . COLOR_YELLOW . $this->localZonesDir . PHP_EOL; echo COLOR_DEFAULT . 'Checking directory: ' . COLOR_YELLOW . $this->localZonesDir . PHP_EOL;
} }
$localZoneDirPermissions = @fileperms(filename: $this->localZonesDir); $localZoneDirPermissions = @fileperms(filename: $this->localZonesDir);
if ($localZoneDirPermissions & 0x0010) { if ($localZoneDirPermissions & 0x0010) {
if (!$quiet) { if (!$this->quiet) {
echo "\t✅ Group has write access." . PHP_EOL; echo "\t✅ Group has write access." . PHP_EOL;
} }
} else { } else {
$setupIsValid = false; $setupIsValid = false;
if (!$quiet) { if (!$this->quiet) {
echo COLOR_RED . "\t❌Group needs write permission!" . PHP_EOL; echo COLOR_RED . "\t❌Group needs write permission!" . PHP_EOL;
} }
} }

View File

@ -58,7 +58,7 @@ if (array_key_exists(key: 'q', array: $options) || array_key_exists(key: 'quiet'
try { try {
$app = new BindAPI(); $app = new BindAPI(quiet: $quiet);
} catch (Exception $e) { } catch (Exception $e) {
echo 'Could not initialize the application: ' . $e->getMessage() . PHP_EOL; echo 'Could not initialize the application: ' . $e->getMessage() . PHP_EOL;
exit(1); exit(1);