From 9ec4f5baa5f736d73cf34f07b907a727b76e87ff Mon Sep 17 00:00:00 2001 From: Fabian Franz Date: Sat, 3 Feb 2018 20:31:15 +0100 Subject: [PATCH] security/tor: DRY controller code and fix copyright statements the new code does not contain any foreign code except the function left in general as well as the servicecontroller. --- .../OPNsense/Tor/Api/ExitaclController.php | 117 +---------------- .../OPNsense/Tor/Api/GeneralController.php | 118 ++--------------- .../Tor/Api/HiddenserviceController.php | 121 +----------------- .../Tor/Api/HiddenserviceaclController.php | 120 +---------------- .../OPNsense/Tor/Api/SocksaclController.php | 119 +---------------- 5 files changed, 32 insertions(+), 563 deletions(-) diff --git a/security/tor/src/opnsense/mvc/app/controllers/OPNsense/Tor/Api/ExitaclController.php b/security/tor/src/opnsense/mvc/app/controllers/OPNsense/Tor/Api/ExitaclController.php index 708603361..f6295d60e 100644 --- a/security/tor/src/opnsense/mvc/app/controllers/OPNsense/Tor/Api/ExitaclController.php +++ b/security/tor/src/opnsense/mvc/app/controllers/OPNsense/Tor/Api/ExitaclController.php @@ -1,8 +1,6 @@ sessionClose(); - $mdl = $this->getModel(); - $grid = new UIModelGrid($mdl->policy); - return $grid->fetchBindRequest( - $this->request, - array('enabled', 'type', 'network', 'action', 'startport', 'endport') - ); + return $this->searchBase('policy', array('enabled', 'type', 'network', 'action', 'startport', 'endport')); } public function getaclAction($uuid = null) { - $mdl = $this->getModel(); - if ($uuid != null) { - $node = $mdl->getNodeByReference('policy.' . $uuid); - if ($node != null) { - // return node - return array('exitpolicy' => $node->getNodes()); - } - } else { - $node = $mdl->policy->add(); - return array('exitpolicy' => $node->getNodes()); - } - return array(); + return $this->getBase('exitpolicy', 'policy', $uuid); } public function addaclAction() { - $result = array('result' => 'failed'); - if ($this->request->isPost() && $this->request->hasPost('exitpolicy')) { - $result = array('result' => 'failed', 'validations' => array()); - $mdl = $this->getModel(); - $node = $mdl->policy->Add(); - $node->setNodes($this->request->getPost('exitpolicy')); - $valMsgs = $mdl->performValidation(); - - foreach ($valMsgs as $field => $msg) { - $fieldnm = str_replace($node->__reference, 'exitpolicy', $msg->getField()); - $result['validations'][$fieldnm] = $msg->getMessage(); - } - - if (count($result['validations']) == 0) { - $mdl->serializeToConfig(); - Config::getInstance()->save(); - unset($result['validations']); - $result['result'] = 'saved'; - } - } - return $result; + return $this->addBase('exitpolicy', 'policy'); } public function delaclAction($uuid) { - - $result = array('result' => 'failed'); - - if ($this->request->isPost()) { - $mdl = $this->getModel(); - if ($uuid != null) { - if ($mdl->policy->del($uuid)) { - $mdl->serializeToConfig(); - Config::getInstance()->save(); - $result['result'] = 'deleted'; - } else { - $result['result'] = 'not found'; - } - } - } - return $result; + return $this->delBase('policy', $uuid); } public function setaclAction($uuid) { - if ($this->request->isPost() && $this->request->hasPost('exitpolicy')) { - $mdl = $this->getModel(); - if ($uuid != null) { - $node = $mdl->getNodeByReference('policy.' . $uuid); - if ($node != null) { - $result = array('result' => 'failed', 'validations' => array()); - $info = $this->request->getPost('exitpolicy'); - - $node->setNodes($info); - $valMsgs = $mdl->performValidation(); - foreach ($valMsgs as $field => $msg) { - $fieldnm = str_replace($node->__reference, 'exitpolicy', $msg->getField()); - $result['validations'][$fieldnm] = $msg->getMessage(); - } - - if (count($result['validations']) == 0) { - // save config if validated correctly - $mdl->serializeToConfig(); - unset($result['validations']); - Config::getInstance()->save(); - $result = array('result' => 'saved'); - } - return $result; - } - } - } - return array('result' => 'failed'); + return $this->setBase('exitpolicy', 'policy', $uuid); } - public function toggle_handler($uuid, $element) - { - - $result = array('result' => 'failed'); - - if ($this->request->isPost()) { - $mdl = $this->getModel(); - if ($uuid != null) { - $node = $mdl->getNodeByReference($element . '.' . $uuid); - if ($node != null) { - if ($node->enabled->__toString() == '1') { - $result['result'] = 'Disabled'; - $node->enabled = '0'; - } else { - $result['result'] = 'Enabled'; - $node->enabled = '1'; - } - $mdl->serializeToConfig(); - Config::getInstance()->save(); - } - } - } - return $result; - } - public function toggleaclAction($uuid) { - return $this->toggle_handler($uuid, 'policy'); + return $this->toggleBase('policy', $uuid); } } diff --git a/security/tor/src/opnsense/mvc/app/controllers/OPNsense/Tor/Api/GeneralController.php b/security/tor/src/opnsense/mvc/app/controllers/OPNsense/Tor/Api/GeneralController.php index 4b9a7dac6..570cd6286 100644 --- a/security/tor/src/opnsense/mvc/app/controllers/OPNsense/Tor/Api/GeneralController.php +++ b/security/tor/src/opnsense/mvc/app/controllers/OPNsense/Tor/Api/GeneralController.php @@ -2,6 +2,8 @@ /* * Copyright (C) 2017 Fabian Franz + * Copyright (C) 2015 Jos Schellevis + * Copyright (C) 2015-2017 Deciso B.V. * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -75,135 +77,31 @@ class GeneralController extends ApiMutableModelControllerBase public function searchhidservauthAction() { - $this->sessionClose(); - $mdl = $this->getModel(); - $grid = new UIModelGrid($mdl->client_authentications->client_auth); - return $grid->fetchBindRequest( - $this->request, - array('enabled', 'onion_service', 'auth_cookie') - ); + return $this->searchBase('client_authentications.client_auth', array('enabled', 'onion_service', 'auth_cookie')); } public function gethidservauthAction($uuid = null) { - $mdl = $this->getModel(); - if ($uuid != null) { - $node = $mdl->getNodeByReference('client_authentications.client_auth.' . $uuid); - if ($node != null) { - // return node - return array('client_auth' => $node->getNodes()); - } - } else { - $node = $mdl->client_authentications->client_auth->add(); - return array('client_auth' => $node->getNodes()); - } - return array(); + return $this->getBase('client_auth', 'client_authentications.client_auth', $uuid); } public function addhidservauthAction() { - $result = array('result' => 'failed'); - if ($this->request->isPost() && $this->request->hasPost('client_auth')) { - $result = array('result' => 'failed', 'validations' => array()); - $mdl = $this->getModel(); - $node = $mdl->client_authentications->client_auth->Add(); - $node->setNodes($this->request->getPost('client_auth')); - $valMsgs = $mdl->performValidation(); - - foreach ($valMsgs as $field => $msg) { - $fieldnm = str_replace($node->__reference, 'client_auth', $msg->getField()); - $result['validations'][$fieldnm] = $msg->getMessage(); - } - - if (count($result['validations']) == 0) { - // save config if validated correctly - $mdl->serializeToConfig(); - Config::getInstance()->save(); - unset($result['validations']); - $result['result'] = 'saved'; - } - } - return $result; + return $this->addBase('client_auth', 'client_authentications.client_auth'); } public function delhidservauthAction($uuid) { - - $result = array('result' => 'failed'); - - if ($this->request->isPost()) { - $mdl = $this->getModel(); - if ($uuid != null) { - if ($mdl->client_authentications->client_auth->del($uuid)) { - $mdl->serializeToConfig(); - Config::getInstance()->save(); - $result['result'] = 'deleted'; - } else { - $result['result'] = 'not found'; - } - } - } - return $result; + return $this->delBase('client_authentications.client_auth', $uuid); } public function sethidservauthAction($uuid) { - if ($this->request->isPost() && $this->request->hasPost('client_auth')) { - $mdl = $this->getModel(); - if ($uuid != null) { - $node = $mdl->getNodeByReference('client_authentications.client_auth.' . $uuid); - if ($node != null) { - $result = array('result' => 'failed', 'validations' => array()); - $info = $this->request->getPost('client_auth'); - - $node->setNodes($info); - $valMsgs = $mdl->performValidation(); - foreach ($valMsgs as $field => $msg) { - $fieldnm = str_replace($node->__reference, 'client_auth', $msg->getField()); - $result['validations'][$fieldnm] = $msg->getMessage(); - } - - if (count($result['validations']) == 0) { - // save config if validated correctly - $mdl->serializeToConfig(); - unset($result['validations']); - Config::getInstance()->save(); - $result = array('result' => 'saved'); - } - return $result; - } - } - } - return array('result' => 'failed'); - } - - public function toggle_handler($uuid, $element) - { - - $result = array('result' => 'failed'); - - if ($this->request->isPost()) { - $mdl = $this->getModel(); - if ($uuid != null) { - $node = $mdl->getNodeByReference($element . '.' . $uuid); - if ($node != null) { - if ($node->enabled->__toString() == '1') { - $result['result'] = 'Disabled'; - $node->enabled = '0'; - } else { - $result['result'] = 'Enabled'; - $node->enabled = '1'; - } - $mdl->serializeToConfig(); - Config::getInstance()->save(); - } - } - } - return $result; + return $this->setBase('client_auth', 'client_authentications.client_auth', $uuid); } public function togglehidservauthAction($uuid) { - return $this->toggle_handler($uuid, 'client_authentications.client_auth'); + return $this->toggleBase('client_authentications.client_auth', $uuid); } } diff --git a/security/tor/src/opnsense/mvc/app/controllers/OPNsense/Tor/Api/HiddenserviceController.php b/security/tor/src/opnsense/mvc/app/controllers/OPNsense/Tor/Api/HiddenserviceController.php index 5d6ea6617..018c4b1b5 100644 --- a/security/tor/src/opnsense/mvc/app/controllers/OPNsense/Tor/Api/HiddenserviceController.php +++ b/security/tor/src/opnsense/mvc/app/controllers/OPNsense/Tor/Api/HiddenserviceController.php @@ -1,8 +1,6 @@ sessionClose(); - $mdl = $this->getModel(); - $grid = new UIModelGrid($mdl->service); - return $grid->fetchBindRequest( - $this->request, - array('enabled', 'name') - ); + return $this->searchBase('service', array('enabled', 'name')); } public function getserviceAction($uuid = null) { - $mdl = $this->getModel(); - if ($uuid != null) { - $node = $mdl->getNodeByReference('service.' . $uuid); - if ($node != null) { - // return node - return array('hiddenservice' => $node->getNodes()); - } - } else { - $node = $mdl->service->add(); - return array('hiddenservice' => $node->getNodes()); - } - return array(); + return $this->getBase('hiddenservice', 'service', $uuid); } public function addserviceAction() { - $result = array('result' => 'failed'); - if ($this->request->isPost() && $this->request->hasPost('hiddenservice')) { - $result = array('result' => 'failed', 'validations' => array()); - $mdl = $this->getModel(); - $node = $mdl->service->Add(); - $node->setNodes($this->request->getPost('hiddenservice')); - $valMsgs = $mdl->performValidation(); - - foreach ($valMsgs as $field => $msg) { - $fieldnm = str_replace($node->__reference, 'hiddenservice', $msg->getField()); - $result['validations'][$fieldnm] = $msg->getMessage(); - } - - if (count($result['validations']) == 0) { - // save config if validated correctly - $mdl->serializeToConfig(); - Config::getInstance()->save(); - unset($result['validations']); - $result['result'] = 'saved'; - } - } - return $result; + return $this->addBase('hiddenservice', 'service'); } public function delserviceAction($uuid) { - - $result = array('result' => 'failed'); - - if ($this->request->isPost()) { - $mdl = $this->getModel(); - if ($uuid != null) { - if ($mdl->service->del($uuid)) { - $mdl->serializeToConfig(); - Config::getInstance()->save(); - $result['result'] = 'deleted'; - } else { - $result['result'] = 'not found'; - } - } - } - return $result; + return $this->delBase('service', $uuid); } public function setserviceAction($uuid) { - if ($this->request->isPost() && $this->request->hasPost('hiddenservice')) { - $mdl = $this->getModel(); - if ($uuid != null) { - $node = $mdl->getNodeByReference('service.' . $uuid); - if ($node != null) { - $result = array('result' => 'failed', 'validations' => array()); - $info = $this->request->getPost('hiddenservice'); - - $node->setNodes($info); - $valMsgs = $mdl->performValidation(); - foreach ($valMsgs as $field => $msg) { - $fieldnm = str_replace($node->__reference, 'hiddenservice', $msg->getField()); - $result['validations'][$fieldnm] = $msg->getMessage(); - } - - if (count($result['validations']) == 0) { - // save config if validated correctly - $mdl->serializeToConfig(); - unset($result['validations']); - Config::getInstance()->save(); - $result = array('result' => 'saved'); - } - return $result; - } - } - } - return array('result' => 'failed'); - } - public function toggle_handler($uuid, $element) - { - - $result = array('result' => 'failed'); - - if ($this->request->isPost()) { - $mdl = $this->getModel(); - if ($uuid != null) { - $node = $mdl->getNodeByReference($element . '.' . $uuid); - if ($node != null) { - if ($node->enabled->__toString() == '1') { - $result['result'] = 'Disabled'; - $node->enabled = '0'; - } else { - $result['result'] = 'Enabled'; - $node->enabled = '1'; - } - // if item has toggled, serialize to config and save - $mdl->serializeToConfig(); - Config::getInstance()->save(); - } - } - } - return $result; + return $this->setBase('hiddenservice', 'service', $uuid); } public function toggleserviceAction($uuid) { - return $this->toggle_handler($uuid, 'service'); + return $this->toggleBase('service', $uuid); } } diff --git a/security/tor/src/opnsense/mvc/app/controllers/OPNsense/Tor/Api/HiddenserviceaclController.php b/security/tor/src/opnsense/mvc/app/controllers/OPNsense/Tor/Api/HiddenserviceaclController.php index 8688b7fec..7d8329e30 100644 --- a/security/tor/src/opnsense/mvc/app/controllers/OPNsense/Tor/Api/HiddenserviceaclController.php +++ b/security/tor/src/opnsense/mvc/app/controllers/OPNsense/Tor/Api/HiddenserviceaclController.php @@ -1,8 +1,6 @@ sessionClose(); - $mdl = $this->getModel(); - $grid = new UIModelGrid($mdl->hiddenserviceacl); - return $grid->fetchBindRequest( - $this->request, - array('enabled', 'hiddenservice', 'port', 'target_host', 'target_port') - ); + return $this->searchBase('hiddenserviceacl', array('enabled', 'hiddenservice', 'port', 'target_host', 'target_port')); } public function getaclAction($uuid = null) { - $mdl = $this->getModel(); - if ($uuid != null) { - $node = $mdl->getNodeByReference('hiddenserviceacl.' . $uuid); - if ($node != null) { - // return node - return array('hiddenserviceacl' => $node->getNodes()); - } - } else { - $node = $mdl->hiddenserviceacl->add(); - return array('hiddenserviceacl' => $node->getNodes()); - } - return array(); + return $this->getBase('hiddenserviceacl', 'hiddenserviceacl', $uuid); } public function addaclAction() { - $result = array('result' => 'failed'); - if ($this->request->isPost() && $this->request->hasPost('hiddenserviceacl')) { - $result = array('result' => 'failed', 'validations' => array()); - $mdl = $this->getModel(); - $node = $mdl->hiddenserviceacl->Add(); - $node->setNodes($this->request->getPost('hiddenserviceacl')); - $valMsgs = $mdl->performValidation(); - - foreach ($valMsgs as $field => $msg) { - $fieldnm = str_replace($node->__reference, 'hiddenserviceacl', $msg->getField()); - $result['validations'][$fieldnm] = $msg->getMessage(); - } - - if (count($result['validations']) == 0) { - // save config if validated correctly - $mdl->serializeToConfig(); - Config::getInstance()->save(); - unset($result['validations']); - $result['result'] = 'saved'; - } - } - return $result; + return $this->addBase('hiddenserviceacl', 'hiddenserviceacl'); } public function delaclAction($uuid) { - - $result = array('result' => 'failed'); - - if ($this->request->isPost()) { - $mdl = $this->getModel(); - if ($uuid != null) { - if ($mdl->hiddenserviceacl->del($uuid)) { - $mdl->serializeToConfig(); - Config::getInstance()->save(); - $result['result'] = 'deleted'; - } else { - $result['result'] = 'not found'; - } - } - } - return $result; + return $this->delBase('hiddenserviceacl', $uuid); } public function setaclAction($uuid) { - if ($this->request->isPost() && $this->request->hasPost('hiddenserviceacl')) { - $mdl = $this->getModel(); - if ($uuid != null) { - $node = $mdl->getNodeByReference('hiddenserviceacl.' . $uuid); - if ($node != null) { - $result = array('result' => 'failed', 'validations' => array()); - $info = $this->request->getPost('hiddenserviceacl'); - - $node->setNodes($info); - $valMsgs = $mdl->performValidation(); - foreach ($valMsgs as $field => $msg) { - $fieldnm = str_replace($node->__reference, 'hiddenserviceacl', $msg->getField()); - $result['validations'][$fieldnm] = $msg->getMessage(); - } - - if (count($result['validations']) == 0) { - // save config if validated correctly - $mdl->serializeToConfig(); - unset($result['validations']); - Config::getInstance()->save(); - $result = array('result' => 'saved'); - } - return $result; - } - } - } - return array('result' => 'failed'); - } - public function toggle_handler($uuid, $element) - { - - $result = array('result' => 'failed'); - - if ($this->request->isPost()) { - $mdl = $this->getModel(); - if ($uuid != null) { - $node = $mdl->getNodeByReference($element . '.' . $uuid); - if ($node != null) { - if ($node->enabled->__toString() == '1') { - $result['result'] = 'Disabled'; - $node->enabled = '0'; - } else { - $result['result'] = 'Enabled'; - $node->enabled = '1'; - } - $mdl->serializeToConfig(); - Config::getInstance()->save(); - } - } - } - return $result; + return $this->setBase('hiddenserviceacl', 'hiddenserviceacl', $uuid); } public function toggleaclAction($uuid) { - return $this->toggle_handler($uuid, 'hiddenserviceacl'); + return $this->toggleBase('hiddenserviceacl', $uuid); } } diff --git a/security/tor/src/opnsense/mvc/app/controllers/OPNsense/Tor/Api/SocksaclController.php b/security/tor/src/opnsense/mvc/app/controllers/OPNsense/Tor/Api/SocksaclController.php index 0a357852c..1617ca5fc 100644 --- a/security/tor/src/opnsense/mvc/app/controllers/OPNsense/Tor/Api/SocksaclController.php +++ b/security/tor/src/opnsense/mvc/app/controllers/OPNsense/Tor/Api/SocksaclController.php @@ -1,8 +1,6 @@ sessionClose(); - $mdl = $this->getModel(); - $grid = new UIModelGrid($mdl->policy); - return $grid->fetchBindRequest( - $this->request, - array('enabled', 'type', 'network', 'action') - ); + return $this->searchBase('policy', array('enabled', 'type', 'network', 'action')); } public function getaclAction($uuid = null) { - $mdl = $this->getModel(); - if ($uuid != null) { - $node = $mdl->getNodeByReference('policy.' . $uuid); - if ($node != null) { - // return node - return array('policy' => $node->getNodes()); - } - } else { - $node = $mdl->policy->add(); - return array('policy' => $node->getNodes()); - } - return array(); + return $this->getBase('policy', 'policy', $uuid); } public function addaclAction() { - $result = array('result' => 'failed'); - if ($this->request->isPost() && $this->request->hasPost('policy')) { - $result = array('result' => 'failed', 'validations' => array()); - $mdl = $this->getModel(); - $node = $mdl->policy->Add(); - $node->setNodes($this->request->getPost('policy')); - $valMsgs = $mdl->performValidation(); - - foreach ($valMsgs as $field => $msg) { - $fieldnm = str_replace($node->__reference, 'policy', $msg->getField()); - $result['validations'][$fieldnm] = $msg->getMessage(); - } - - if (count($result['validations']) == 0) { - $mdl->serializeToConfig(); - Config::getInstance()->save(); - unset($result['validations']); - $result['result'] = 'saved'; - } - } - return $result; + return $this->addBase('policy', 'policy'); } public function delaclAction($uuid) { - - $result = array('result' => 'failed'); - - if ($this->request->isPost()) { - $mdl = $this->getModel(); - if ($uuid != null) { - if ($mdl->policy->del($uuid)) { - $mdl->serializeToConfig(); - Config::getInstance()->save(); - $result['result'] = 'deleted'; - } else { - $result['result'] = 'not found'; - } - } - } - return $result; + return $this->delBase('policy', $uuid); } public function setaclAction($uuid) { - if ($this->request->isPost() && $this->request->hasPost('policy')) { - $mdl = $this->getModel(); - if ($uuid != null) { - $node = $mdl->getNodeByReference('policy.' . $uuid); - if ($node != null) { - $result = array('result' => 'failed', 'validations' => array()); - $info = $this->request->getPost('policy'); - - $node->setNodes($info); - $valMsgs = $mdl->performValidation(); - foreach ($valMsgs as $field => $msg) { - $fieldnm = str_replace($node->__reference, 'policy', $msg->getField()); - $result['validations'][$fieldnm] = $msg->getMessage(); - } - - if (count($result['validations']) == 0) { - // save config if validated correctly - $mdl->serializeToConfig(); - unset($result['validations']); - Config::getInstance()->save(); - $result = array('result' => 'saved'); - } - return $result; - } - } - } - return array('result' => 'failed'); - } - public function toggle_handler($uuid, $element) - { - - $result = array('result' => 'failed'); - - if ($this->request->isPost()) { - $mdl = $this->getModel(); - if ($uuid != null) { - $node = $mdl->getNodeByReference($element . '.' . $uuid); - if ($node != null) { - if ($node->enabled->__toString() == '1') { - $result['result'] = 'Disabled'; - $node->enabled = '0'; - } else { - $result['result'] = 'Enabled'; - $node->enabled = '1'; - } - $mdl->serializeToConfig(); - Config::getInstance()->save(); - } - } - } - return $result; + return $this->setBase('policy', 'policy', $uuid); } public function toggleaclAction($uuid) { - return $this->toggle_handler($uuid, 'policy'); + return $this->toggleBase('policy', $uuid); } }