fixed a display bug listing avalable subcommands

This commit is contained in:
tracer 2024-04-08 19:01:13 +02:00
parent 4189d35c09
commit 6f16cfd76c
8 changed files with 161 additions and 126 deletions

View File

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

View File

@ -28,7 +28,7 @@ class BindAPI
/**
* @throws Exception
*/
public function __construct($verbose = false, $quiet = false)
public function __construct($quiet = false)
{
// init the logger
$dateFormat = "Y:m:d H:i:s";
@ -51,8 +51,7 @@ class BindAPI
$containerBuilder = new ContainerBuilder();
$containerBuilder->addDefinitions([
ConfigController::class => autowire()
->constructorParameter(parameter: 'quiet', value: $quiet)
->constructorParameter(parameter: 'verbose', value: $verbose),
->constructorParameter(parameter: 'quiet', value: $quiet),
CLIController::class => autowire()
->constructorParameter(parameter: 'logger', value: $this->logger),
DomainController::class => autowire()

View File

@ -66,7 +66,7 @@ class CLIController
callback: function () {
$this->checkPermissions();
},
description: 'health checks the system can perform'))
description: 'check file permissions'))
->addCommand(command: new Command(
name: 'panels',
callback: function () {
@ -128,7 +128,7 @@ class CLIController
$this->panelsUpdate();
},
mandatoryParameters: ['ID'],
optionalParameters: ['name=<name>', 'A=<IPv4>', 'AAAA=<IPv6>', 'apikey=<API-Key>']))
optionalParameters: ['name=<name>', 'A=<IPv4>', 'AAAA=<IPv6>', 'apikey=<API-Key>', 'self=<0|1>']))
->addCommand(command: new Command(
name: 'delete',
callback: function () {
@ -237,27 +237,27 @@ class CLIController
callback: function () {
$this->apikeysDelete();
},
mandatoryParameters: ['ID'])))
->addCommandGroup(commandGroup: (new CommandGroup(name: 'webmail', description: 'manage webmail setup'))
->addCommand(command: new Command(
name: 'check',
callback: function () {
$this->webmailCheck();
},
mandatoryParameters: ['example.com']
))
->addCommand(command: new Command(
name: 'create',
callback: function () {
$this->webmailCreate();
},
mandatoryParameters: ['example.com']))
->addCommand(command: new Command(
name: 'delete',
callback: function () {
$this->webmailDelete();
},
mandatoryParameters: ['ID'])));
// ->addCommandGroup(commandGroup: (new CommandGroup(name: 'webmail', description: 'manage webmail setup'))
// ->addCommand(command: new Command(
// name: 'check',
// callback: function () {
// $this->webmailCheck();
// },
// mandatoryParameters: ['example.com']
// ))
// ->addCommand(command: new Command(
// name: 'create',
// callback: function () {
// $this->webmailCreate();
// },
// mandatoryParameters: ['example.com']))
// ->addCommand(command: new Command(
// name: 'delete',
// callback: function () {
// $this->webmailDelete();
// },
// mandatoryParameters: ['ID'])));
}
@ -314,7 +314,6 @@ class CLIController
$this->logger->debug(message: "showUsage()");
$debug = $this->configController->getConfig(configKey: 'debug');
echo 'bindAPI version: todo (env: todo) ';
echo COLOR_DEFAULT . ')' . PHP_EOL;
echo COLOR_YELLOW . 'Usage:' . PHP_EOL;
@ -322,7 +321,7 @@ class CLIController
echo COLOR_YELLOW . 'Options:' . PHP_EOL;
echo COLOR_GREEN . "\t-v, --version\t\t" . COLOR_DEFAULT . "Display the version of the API" . PHP_EOL;
echo COLOR_GREEN . "\t-V, --verbose\t\t" . COLOR_DEFAULT . "All :lists command are auto-verbose" . PHP_EOL . PHP_EOL;
echo COLOR_GREEN . "\t-q, --quiet\t\t" . COLOR_DEFAULT . "No output to stdout, for cronjobs" . PHP_EOL . PHP_EOL;
echo COLOR_YELLOW . 'Arguments: ' . COLOR_WHITE . '<mandatory> {optional}' . PHP_EOL;
@ -339,7 +338,7 @@ class CLIController
$this->logger->debug(message: "checkPermissions()");
if (!$this->domainController->checkPermissions()) {
if ($this->configController->getConfig(configKey: 'verbose')) {
if (!$this->configController->getConfig(configKey: 'quiet')) {
echo PHP_EOL . COLOR_DEFAULT;
echo 'Missing permissions, please run ' . COLOR_YELLOW . './bin/console check:setup' . COLOR_DEFAULT . ' as root or with sudo.' . PHP_EOL;
}
@ -482,36 +481,32 @@ class CLIController
$encryptionKey = $this->configController->getConfig(configKey: 'encryptionKey');
$decryptedKey = $this->encryptionController->safeDecrypt(encrypted: $panel->getApikey(), key: $encryptionKey);
if ($this->configController->getConfig(configKey: 'verbose')) {
if (empty($panel->getA())) {
$panelRequest = $this->apiController->sendCommand(
requestType: 'GET',
serverName: $panel->getName(),
versionIP: 6,
apiKey: $decryptedKey,
command: '/server',
serverType: 'panel');
} else {
$panelRequest = $this->apiController->sendCommand(
requestType: 'GET',
serverName: $panel->getName(),
versionIP: 4,
apiKey: $decryptedKey,
command: '/server',
serverType: 'panel');
}
$panelData = json_decode(json: $panelRequest['data']);
if (!empty($panelData)) {
$panelVersion = $panelData->meta->panel_version;
$responseTime = sprintf("%0.3f", $panelRequest['responseTime']);
} else {
$panelVersion = 'n/a';
$responseTime = 'n/a';
}
echo COLOR_DEFAULT . ' KeyHelp version: ' . $panelVersion . " ($responseTime seconds)" . PHP_EOL;
if (empty($panel->getA())) {
$panelRequest = $this->apiController->sendCommand(
requestType: 'GET',
serverName: $panel->getName(),
versionIP: 6,
apiKey: $decryptedKey,
command: '/server',
serverType: 'panel');
} else {
echo PHP_EOL;
$panelRequest = $this->apiController->sendCommand(
requestType: 'GET',
serverName: $panel->getName(),
versionIP: 4,
apiKey: $decryptedKey,
command: '/server',
serverType: 'panel');
}
$panelData = json_decode(json: $panelRequest['data']);
if (!empty($panelData)) {
$panelVersion = $panelData->meta->panel_version;
$responseTime = sprintf("%0.3f", $panelRequest['responseTime']);
} else {
$panelVersion = 'n/a';
$responseTime = 'n/a';
}
echo COLOR_DEFAULT . ' KeyHelp version: ' . $panelVersion . " ($responseTime seconds)" . PHP_EOL;
if (empty($panel->getA())) {
$result = $this->apiController->sendCommand(
@ -567,6 +562,7 @@ class CLIController
$domainCount = 0;
foreach ($tmpDomainList as $domain) {
echo COLOR_DEFAULT . " Domain: " . COLOR_YELLOW . str_pad(string: $domain->getDomain(), length: $maxDomainNameLength);
if (!$domain->isSubdomain()) {
$this->checkNS(domainName: $domain->getDomain(), panel: $panel);
$domainCount++;
@ -610,7 +606,7 @@ class CLIController
}
}
echo "fould domain ***" . $foundDomain . PHP_EOL;
echo "found domain ***" . $foundDomain . PHP_EOL;
/*
// system domain
@ -937,13 +933,7 @@ class CLIController
}
}
}
if (!$this->configController->getConfig(configKey: 'quiet')) {
echo PHP_EOL;
}
if ($error) {
if (!$this->configController->getConfig(configKey: 'verbose')) {
echo 'There were errors, run command with -V (or -verbose) to see the errors.' . PHP_EOL;
}
exit(1);
} else {
exit(0);
@ -1002,7 +992,7 @@ class CLIController
$a = $server->getA() ?? '';
if (!empty($a)) {
$this->logger->debug("check a");
if ($this->configController->getConfig(configKey: 'verbose')) {
if (!$this->configController->getConfig(configKey: 'quiet')) {
echo COLOR_DEFAULT . ' ' . str_pad(string: $a, length: $maxA, pad_type: STR_PAD_LEFT) . ' ';
}
if ($result = $this->apiController->sendCommand(
@ -1017,7 +1007,7 @@ class CLIController
echo ' ' . COLOR_GREEN . $result['data'];
} else {
echo COLOR_BLUE . ' skip' . COLOR_DEFAULT;
if ($this->configController->getConfig(configKey: 'verbose')) {
if (!$this->configController->getConfig(configKey: 'quiet')) {
echo ' ' . $result['data'];
}
}
@ -1029,7 +1019,7 @@ class CLIController
$aaaa = $server->getAaaa() ?? '';
if (!empty($aaaa)) {
$this->logger->debug("check aaaa");
if ($this->configController->getConfig(configKey: 'verbose')) {
if (!$this->configController->getConfig(configKey: 'quiet')) {
echo COLOR_DEFAULT . ' ' . str_pad(string: $aaaa, length: $maxAAAA, pad_type: STR_PAD_LEFT) . ' ';
}
if ($result = $this->apiController->sendCommand(
@ -1044,7 +1034,7 @@ class CLIController
echo ' ' . COLOR_GREEN . $result['data'];
} else {
echo COLOR_BLUE . ' skip' . COLOR_DEFAULT;
if ($this->configController->getConfig(configKey: 'verbose')) {
if (!$this->configController->getConfig(configKey: 'quiet')) {
echo ' ' . $result['data'];
}
}
@ -1270,23 +1260,24 @@ class CLIController
}
if ($this->configController->getConfig(configKey: 'verbose')) {
if (!$this->configController->getConfig(configKey: 'quiet')) {
echo "Found domain: " . COLOR_YELLOW . $foundDomain . COLOR_DEFAULT . PHP_EOL;
}
// get host
if ($this->dynDNSRepository->findByName(name: $name)) {
if ($this->configController->getConfig(configKey: 'verbose')) {
if (!$this->configController->getConfig(configKey: 'quiet')) {
echo "DynDNS host " . COLOR_YELLOW . $name . COLOR_DEFAULT . "already exists." . PHP_EOL;
exit(0);
}
} else {
if ($this->configController->getConfig(configKey: 'verbose')) {
if (!$this->configController->getConfig(configKey: 'quiet')) {
echo "DynDNS host " . COLOR_YELLOW . $name . COLOR_DEFAULT . "will be created." . PHP_EOL;
// insert in db
$dyndnsHost = new DynDNS(name: $name);
$dyndnsHost->setName($name);
// why is the property set in the cunstructor and afterwards again? FIXME
$dyndnsHost->setName(name: $name);
}
}
@ -1610,7 +1601,7 @@ class CLIController
exit(1);
}
if ($this->configController->getConfig(configKey: 'verbose')) {
if (!$this->configController->getConfig(configKey: 'quiet')) {
echo "Updating DynDNS host: $hostName" . PHP_EOL;
}
@ -1645,7 +1636,7 @@ class CLIController
}
if ($result['header'] == 200) {
if ($this->configController->getConfig(configKey: 'verbose')) {
if (!$this->configController->getConfig(configKey: 'quiet')) {
$data = $result['data'];
$decodedData = json_decode(json: $data, associative: true);
echo $decodedData['message'] . PHP_EOL;
@ -1746,7 +1737,6 @@ class CLIController
public function webmailCheck(): void
{
$quiet = $this->configController->getConfig(configKey: 'quiet');
$verbose = $this->configController->getConfig(configKey: 'verbose');
if (empty($this->arguments[1])) {
if (!$quiet) {
@ -1783,7 +1773,7 @@ class CLIController
$webmailDomain = 'webmail.' . $domainName;
if (!empty($panel->getAAAA())) {
if (!$quiet && $verbose) {
if (!$quiet) {
echo 'Check using IPv6: ' . COLOR_YELLOW . $panel->getAaaa() . '.' . COLOR_DEFAULT . PHP_EOL;
}
$result = $this->apiController->sendCommand(
@ -1794,7 +1784,7 @@ class CLIController
command: 'domains/name/' . $webmailDomain,
serverType: 'panel');
} else {
if (!$quiet && $verbose) {
if (!$quiet) {
echo 'Check using IPv4: ' . COLOR_YELLOW . $panel->getA() . COLOR_DEFAULT . PHP_EOL;
}
$result = $this->apiController->sendCommand(

View File

@ -46,10 +46,6 @@ class CommandGroupContainer
}
/**
* @param string $command
* @return ?CommandGroup
*/
private function findGroupByName(string $command): ?CommandGroup
{
foreach ($this->commandGroups as $group) {
@ -70,13 +66,12 @@ class CommandGroupContainer
}
} else {
echo COLOR_DEFAULT . 'Unknown subcommand ' . COLOR_YELLOW . $command . ' for ' . $command . COLOR_DEFAULT . '.' . PHP_EOL;
}
} else {
// check for command group and print available commands
foreach ($this->commandGroups as $group) {
echo 'Available subcommands for : ' . COLOR_YELLOW . $group->getName() . COLOR_DEFAULT . ':' . PHP_EOL;
if ($group->getName() === $command) {
echo 'Available subcommands for : ' . COLOR_YELLOW . $group->getName() . COLOR_DEFAULT . ':' . PHP_EOL;
$group->printCommands(strlen(string: $group->getName()));
exit(0);
}

View File

@ -10,11 +10,10 @@ class ConfigController
private array $config;
/**
* @param bool $verbose
* @param bool $quiet
* @param bool $test
*/
public function __construct(bool $verbose = false, bool $quiet = false, bool $test = false)
public function __construct(private readonly bool $quiet = false, bool $test = false)
{
if ($test) {
@ -51,7 +50,6 @@ class ConfigController
$this->config = json_decode(json: $configJSON, associative: true);
$this->config['verbose'] = (bool)$verbose;
$this->config['quiet'] = (bool)$quiet;
$this->config['test'] = (bool)$test;
}

View File

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

View File

@ -28,6 +28,70 @@ class KeyHelpDomain
private Target $target;
private Security $security;
private Apache $apache;
private string $dkimSelector;
private string $dkimRecord;
private ?string $dkimTextRecord;
public function __construct()
{
$this->dkimTextRecord = null;
}
private int $traffic;
private bool $isEmailSendingOnly;
public function isEmailSendingOnly(): bool
{
return $this->isEmailSendingOnly;
}
public function setIsEmailSendingOnly(bool $isEmailSendingOnly): void
{
$this->isEmailSendingOnly = $isEmailSendingOnly;
}
public function getTraffic(): int
{
return $this->traffic;
}
public function setTraffic(int $traffic): void
{
$this->traffic = $traffic;
}
public function getDkimTextRecord(): string
{
return $this->dkimTextRecord;
}
public function setDkimTextRecord(string $dkimTextRecord): void
{
$this->dkimTextRecord = $dkimTextRecord;
}
public function getDkimRecord(): string
{
return $this->dkimRecord;
}
public function setDkimRecord(string $dkimRecord): void
{
$this->dkimRecord = $dkimRecord;
}
public function getDkimSelector(): string
{
return $this->dkimSelector;
}
public function setDkimSelector(string $dkimSelector): void
{
$this->dkimSelector = $dkimSelector;
}
/**
* @return Apache

View File

@ -28,13 +28,15 @@ $arguments = array_slice(array: $argv, offset: $restIndex);
if (array_key_exists(key: 'v', array: $options) || array_key_exists(key: 'version', array: $options)) {
$arguments = 'showVersion';
$composerJson = json_decode(json: file_get_contents(filename: dirname(path: __DIR__, levels: 2) . '/composer.json'), associative: true);
$name = $composerJson['name'];
$description = $composerJson['description'];
$version = $composerJson['version'];
$buildNumber = $composerJson['build_number'];
$authorName = $composerJson['authors'][0]['name'];
$authorEmail = $composerJson['authors'][0]['email'];
$composerJson = json_decode(json: file_get_contents(filename: dirname(path: __DIR__, levels: 2) . '/composer.json'));
$name = $composerJson->name;
$description = $composerJson->description;
$version = $composerJson->version;
$buildNumber = $composerJson->build_number;
$authors = $composerJson->authors;
// currently only one author, so just handle only first entry
$authorName = $authors[0]->name;
$authorEmail = $authors[0]->email;
echo "Name: $name\n";
echo "Description: $description\n";
@ -55,15 +57,8 @@ if (array_key_exists(key: 'q', array: $options) || array_key_exists(key: 'quiet'
}
if (array_key_exists(key: 'V', array: $options) || array_key_exists(key: 'verbose', array: $options)) {
$verbose = true;
} else {
$verbose = false;
}
try {
$app = new BindAPI(verbose: $verbose, quiet: $quiet);
$app = new BindAPI(quiet: $quiet);
} catch (Exception $e) {
echo 'Could not initialize the application: ' . $e->getMessage() . PHP_EOL;
exit(1);
@ -74,7 +69,7 @@ try {
$app->runCommand(arguments: $arguments);
} catch (Exception $e) {
$exceptionMessage = $e->getMessage();
preg_match(pattern: '/\[1045\]/', subject: $exceptionMessage, matches: $matches);
preg_match(pattern: '/\[1045]/', subject: $exceptionMessage, matches: $matches);
if (!empty($matches)) {
echo 'Access was denied for a user when trying to access the database.' . PHP_EOL;
} else {