cli/setup: Add sanity check for the --mode switch when creating the config directory

This commit is contained in:
Eric Lippmann 2014-12-30 15:46:22 +01:00
parent d3dcf152fb
commit 672500029c
1 changed files with 25 additions and 12 deletions

View File

@ -20,10 +20,10 @@ class ConfigCommand extends Command
* *
* OPTIONS: * OPTIONS:
* *
* --mode=<mode> The access mode to use [2770]
*
* --config=<directory> Path to Icinga Web 2's configuration files [/etc/icingaweb2] * --config=<directory> Path to Icinga Web 2's configuration files [/etc/icingaweb2]
* *
* --mode=<mode> The access mode to use [2770]
*
* --group=<group> Owner group for the configuration directory [icingaweb2] * --group=<group> Owner group for the configuration directory [icingaweb2]
* *
* EXAMPLES: * EXAMPLES:
@ -34,6 +34,13 @@ class ConfigCommand extends Command
*/ */
public function directoryAction() public function directoryAction()
{ {
$configDir = trim($this->params->get('config', $this->app->getConfigDir()));
if (strlen($configDir) === 0) {
$this->fail($this->translate(
'The argument --config expects a path to Icinga Web 2\'s configuration files'
));
}
$group = trim($this->params->get('group', 'icingaweb2')); $group = trim($this->params->get('group', 'icingaweb2'));
if (strlen($group) === 0) { if (strlen($group) === 0) {
$this->fail($this->translate( $this->fail($this->translate(
@ -41,26 +48,32 @@ class ConfigCommand extends Command
)); ));
} }
$config = $this->params->get('config', $this->app->getConfigDir()); $mode = trim($this->params->get('mode', '2770'));
if (file_exists($config)) { if (strlen($mode) === 0) {
printf($this->translate("Configuration directory already exists at: %s\n"), $config); $this->fail($this->translate(
'The argument --mode expects an access mode for the configuration directory'
));
}
if (file_exists($configDir)) {
printf($this->translate("Configuration directory already exists at: %s\n"), $configDir);
return true; return true;
} }
$mode = octdec($this->params->get('mode', '2770')); $mode = octdec($mode);
if (false === mkdir($config)) { if (false === mkdir($configDir)) {
$this->fail(sprintf($this->translate('Unable to create path: %s'), $config)); $this->fail(sprintf($this->translate('Unable to create path: %s'), $configDir));
return false; return false;
} }
chmod($config, $mode); chmod($configDir, $mode);
if (chgrp($config, $group) === false) { if (chgrp($configDir, $group) === false) {
$this->fail(sprintf($this->translate('Unable to change the group of "%s" to "%s".'), $config, $group)); $this->fail(sprintf($this->translate('Unable to change the group of "%s" to "%s".'), $configDir, $group));
return false; return false;
} }
printf($this->translate("Successfully created configuration directory at: %s\n"), $config); printf($this->translate("Successfully created configuration directory at: %s\n"), $configDir);
} }
/** /**