diff --git a/src/opnsense/mvc/app/controllers/OPNsense/IDS/Api/ServiceController.php b/src/opnsense/mvc/app/controllers/OPNsense/IDS/Api/ServiceController.php index 7f47be169..0c00307f0 100644 --- a/src/opnsense/mvc/app/controllers/OPNsense/IDS/Api/ServiceController.php +++ b/src/opnsense/mvc/app/controllers/OPNsense/IDS/Api/ServiceController.php @@ -30,6 +30,7 @@ namespace OPNsense\IDS\Api; use \OPNsense\Base\ApiControllerBase; use \OPNsense\Core\Backend; +use \OPNsense\IDS\IDS; /** * Class ServiceController @@ -37,19 +38,114 @@ use \OPNsense\Core\Backend; */ class ServiceController extends ApiControllerBase { + /** + * start ids service + * @return array + */ + public function startAction() + { + if ($this->request->isPost()) { + $backend = new Backend(); + $response = trim($backend->configdRun("ids start")); + return array("response" => $response); + } else { + return array("response" => array()); + } + } /** - * + * stop ids service + * @return array + */ + public function stopAction() + { + if ($this->request->isPost()) { + $backend = new Backend(); + $response = trim($backend->configdRun("ids stop")); + return array("response" => $response); + } else { + return array("response" => array()); + } + } + + /** + * restart ids service + * @return array + */ + public function restartAction() + { + if ($this->request->isPost()) { + $backend = new Backend(); + $response = $backend->configdRun("ids restart"); + return array("response" => $response); + } else { + return array("response" => array()); + } + } + + /** + * retrieve status of squid proxy + * @return array + * @throws \Exception + */ + public function statusAction() + { + $backend = new Backend(); + $mdlIDS = new IDS(); + $response = $backend->configdRun("ids status"); + + if (strpos($response, "not running") > 0) { + if ((string)$mdlIDS->general->enabled == 1) { + $status = "stopped"; + } else { + $status = "disabled"; + } + } elseif (strpos($response, "is running") > 0) { + $status = "running"; + } elseif ((string)$mdlIDS->general->enabled == 0) { + $status = "disabled"; + } else { + $status = "unkown"; + } + + return array("status" => $status); + } + + /** + * reconfigure IDS */ public function reconfigureAction() { + $status = "failed"; if ($this->request->isPost()) { // close session for long running action $this->sessionClose(); + $mdlIDS = new IDS(); + $runStatus = $this->statusAction(); + + if ($runStatus['status'] == "running" && (string)$mdlIDS->general->enabled == 0) { + $this->stopAction(); + } + + $backend = new Backend(); + $bckresult = trim($backend->configdRun("template reload OPNsense.IDS")); + + if ($bckresult == "OK") { + $bckresult = trim($backend->configdRun("ids install rules")); + if ($bckresult == "OK") { + if ($runStatus['status'] == 'running') { + $status = $this->restartAction()['response']; + } else { + $status = $this->startAction()['response']; + } + } else { + $status = "error installing ids rules (".$bckresult.")"; + } + } else { + $status = "error generating ids template (".$bckresult.")"; + } - return array("status" => "failed"); - } else { - return array("status" => "failed"); } + return array("status" => $status); } } diff --git a/src/opnsense/mvc/app/controllers/OPNsense/IDS/Api/SettingsController.php b/src/opnsense/mvc/app/controllers/OPNsense/IDS/Api/SettingsController.php index be1465db8..ca71abd6d 100644 --- a/src/opnsense/mvc/app/controllers/OPNsense/IDS/Api/SettingsController.php +++ b/src/opnsense/mvc/app/controllers/OPNsense/IDS/Api/SettingsController.php @@ -222,4 +222,54 @@ class SettingsController extends ApiControllerBase } return array(); } + + /** + * retrieve IDS settings + * @return array IDS settings + */ + public function getAction() + { + // define list of configurable settings + $settingsNodes = array('general'); + $result = array(); + if ($this->request->isGet()) { + $mdlIDS = new IDS(); + $result['ids'] = array(); + foreach ($settingsNodes as $key) { + $result['ids'][$key] = $mdlIDS->$key->getNodes(); + } + } + return $result; + } + + /** + * update IDS settings + * @return array status + */ + public function setAction() + { + $result = array("result"=>"failed"); + if ($this->request->isPost()) { + // load model and update with provided data + $mdlIDS = new IDS(); + $mdlIDS->setNodes($this->request->getPost("ids")); + + // perform validation + $valMsgs = $mdlIDS->performValidation(); + foreach ($valMsgs as $field => $msg) { + if (!array_key_exists("validations", $result)) { + $result["validations"] = array(); + } + $result["validations"]["ids.".$msg->getField()] = $msg->getMessage(); + } + + // serialize model to config and save + if ($valMsgs->count() == 0) { + $mdlIDS->serializeToConfig(); + Config::getInstance()->save(); + $result["result"] = "saved"; + } + } + return $result; + } }