Commit c0ffb4ea authored by Ad Schellevis's avatar Ad Schellevis

(ids) add api calls

parent 8e24c804
......@@ -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"));
return array("status" => "failed");
if ($bckresult == "OK") {
$bckresult = trim($backend->configdRun("ids install rules"));
if ($bckresult == "OK") {
if ($runStatus['status'] == 'running') {
$status = $this->restartAction()['response'];
} else {
return array("status" => "failed");
$status = $this->startAction()['response'];
}
} else {
$status = "error installing ids rules (".$bckresult.")";
}
} else {
$status = "error generating ids template (".$bckresult.")";
}
}
return array("status" => $status);
}
}
......@@ -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;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment