diff --git a/net/wol/Makefile b/net/wol/Makefile index 583164443..d532d8ce5 100644 --- a/net/wol/Makefile +++ b/net/wol/Makefile @@ -1,6 +1,5 @@ PLUGIN_NAME= wol -PLUGIN_VERSION= 1.0 -PLUGIN_REVISION= 1 +PLUGIN_VERSION= 2.0.d PLUGIN_DEPENDS= wol PLUGIN_COMMENT= Wake on LAN Service PLUGIN_MAINTAINER= franco@opnsense.org diff --git a/net/wol/src/opnsense/mvc/app/controllers/OPNsense/Wol/Api/WolController.php b/net/wol/src/opnsense/mvc/app/controllers/OPNsense/Wol/Api/WolController.php new file mode 100644 index 000000000..76cc7b675 --- /dev/null +++ b/net/wol/src/opnsense/mvc/app/controllers/OPNsense/Wol/Api/WolController.php @@ -0,0 +1,149 @@ +request->isPost()) { + /* input validation */ + $wol = $this->getModel(); + $wolent = $wol->wolentry->Add(); + if ($this->request->hasPost('uuid')) { + $uuid = $this->request->getPost('uuid'); + $tmp = $wol->getNodeByReference('wolentry.' . $uuid); + if ($tmp) { + $wolent = $tmp; + $this->wakeHostByNode($wolent, $result); + } + } else { + $wolent->setNodes($this->request->getPost('wake')); + if ($wol->performValidation()->count() == 0) { + $this->wakeHostByNode($wolent, $result); + } + } + } + return $result; + } + + public function delHostAction($uuid) { + $this->delBase('host', $uuid); + } + public function searchHostAction() + { + return $this->searchBase('wolentry', array("interface", "mac", "descr")); + } + public function getHostAction($uuid = null) + { + $this->sessionClose(); + return $this->getBase('host', 'wolentry', $uuid); + } + public function getwakeAction() + { + return $this->getBase('wake', 'wolentry', null); + } + public function addHostAction() + { + return $this->addBase('host', 'wolentry'); + } + public function setHostAction($uuid) + { + return $this->setBase('host', 'wolentry', $uuid); + } + public function wakeallAction() { + if (!$this->request->isPost()) { + return array('error' => 'Must be called via POST'); + } + $results = array('results' => array()); + $wol = $this->getModel()->wolentry->__items; + foreach ($wol as $wolent) { + $result = array('mac' => (string)$wolent->mac); + $this->wakeHostByNode($wolent, $result); + $results['results'][] = $result; + } + return $results; + } + private function wakeHostByNode($wolent, &$result) { + $backend = new Backend(); + /* determine broadcast address */ + $cidr = $this->getInterfaceSubnet($wolent->interface); + $ipaddr = $this->getInterfaceIP($wolent->interface); + if (empty($cidr) || empty($ipaddr)) { + $result['status'] = 'error'; + $result['error_msg'] = 'Incorrect IPv4 configuration on interface'; + return $result; + } + $broadcast_ip = escapeshellarg($this->calculateSubnetBroadcast($ipaddr, $cidr)); + $result['status'] = trim($backend->configdRun("wol wake {$broadcast_ip} ". escapeshellarg((string)$wolent->mac))); + } + private function getInterfaceIP($if) { + $cfg = Config::getInstance()->object(); + try { + $tmp = (string)$cfg->interfaces->{$if}->ipaddr; + // for example DHCP + if (!filter_var($tmp, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) { + return null; + } + return $tmp; + } catch (Exception $e) { + return null; + } + } + private function getInterfaceSubnet($if) { + $cfg = Config::getInstance()->object(); + try { + return (string)$cfg->interfaces->{$if}->subnet; + } catch (Exception $e) { + return null; + } + } + private function calculateSubnetBroadcast($ip_addr, $cidr) { + // TODO undefined offset + $parts = explode('.', $ip_addr); + $int_ip = ((int)$parts[0]) << 24 | ((int)$parts[1]) << 16 | ((int)$parts[2]) << 8 | (int)$parts[3]; + $hostmask = (2 << (31 - $cidr)) - 1; + $int_bcast = $int_ip | $hostmask; + return implode('.', array( + $int_bcast >> 24, + ($int_bcast >> 16) & 255, + ($int_bcast >> 8) & 255, + $int_bcast & 255, + )); + } +} diff --git a/net/wol/src/opnsense/mvc/app/controllers/OPNsense/Wol/IndexController.php b/net/wol/src/opnsense/mvc/app/controllers/OPNsense/Wol/IndexController.php new file mode 100644 index 000000000..046b10350 --- /dev/null +++ b/net/wol/src/opnsense/mvc/app/controllers/OPNsense/Wol/IndexController.php @@ -0,0 +1,38 @@ +view->wakeForm = $this->getForm("wake"); + $this->view->hostForm = $this->getForm("host"); + $this->view->pick('OPNsense/Wol/index'); + } +} diff --git a/net/wol/src/opnsense/mvc/app/controllers/OPNsense/Wol/forms/host.xml b/net/wol/src/opnsense/mvc/app/controllers/OPNsense/Wol/forms/host.xml new file mode 100644 index 000000000..87bc38866 --- /dev/null +++ b/net/wol/src/opnsense/mvc/app/controllers/OPNsense/Wol/forms/host.xml @@ -0,0 +1,20 @@ +
+ + host.interface + + dropdown + Choose which interface this host is connected to. + + + host.mac + + text + Enter a MAC address in the following format: xx:xx:xx:xx:xx:xx + + + host.description + + text + You may enter a description here for your reference (not parsed). + +
diff --git a/net/wol/src/opnsense/mvc/app/controllers/OPNsense/Wol/forms/wake.xml b/net/wol/src/opnsense/mvc/app/controllers/OPNsense/Wol/forms/wake.xml new file mode 100644 index 000000000..096dd6362 --- /dev/null +++ b/net/wol/src/opnsense/mvc/app/controllers/OPNsense/Wol/forms/wake.xml @@ -0,0 +1,14 @@ +
+ + wake.interface + + dropdown + Choose which interface the host to be woken up is connected to. + + + wake.mac + + text + Enter a MAC address in the following format: xx:xx:xx:xx:xx:xx. + +
diff --git a/net/wol/src/opnsense/mvc/app/models/OPNsense/WOL/ACL/ACL.xml b/net/wol/src/opnsense/mvc/app/models/OPNsense/WOL/ACL/ACL.xml deleted file mode 100644 index 2f1c403c0..000000000 --- a/net/wol/src/opnsense/mvc/app/models/OPNsense/WOL/ACL/ACL.xml +++ /dev/null @@ -1,14 +0,0 @@ - - - Services: Wake on LAN - - services_wol.php* - - - - Services: Wake on LAN: Edit - - services_wol_edit.php* - - - diff --git a/net/wol/src/opnsense/mvc/app/models/OPNsense/WOL/Menu/Menu.xml b/net/wol/src/opnsense/mvc/app/models/OPNsense/WOL/Menu/Menu.xml deleted file mode 100644 index 5f0df9a6b..000000000 --- a/net/wol/src/opnsense/mvc/app/models/OPNsense/WOL/Menu/Menu.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - diff --git a/net/wol/src/opnsense/mvc/app/models/OPNsense/Wol/ACL/ACL.xml b/net/wol/src/opnsense/mvc/app/models/OPNsense/Wol/ACL/ACL.xml new file mode 100644 index 000000000..061a92873 --- /dev/null +++ b/net/wol/src/opnsense/mvc/app/models/OPNsense/Wol/ACL/ACL.xml @@ -0,0 +1,9 @@ + + + Services: Wake on LAN + + ui/wol* + api/wol* + + + diff --git a/net/wol/src/opnsense/mvc/app/models/OPNsense/Wol/Menu/Menu.xml b/net/wol/src/opnsense/mvc/app/models/OPNsense/Wol/Menu/Menu.xml new file mode 100644 index 000000000..f5f232734 --- /dev/null +++ b/net/wol/src/opnsense/mvc/app/models/OPNsense/Wol/Menu/Menu.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/net/wol/src/opnsense/mvc/app/models/OPNsense/Wol/Wol.php b/net/wol/src/opnsense/mvc/app/models/OPNsense/Wol/Wol.php new file mode 100644 index 000000000..4a213ec2c --- /dev/null +++ b/net/wol/src/opnsense/mvc/app/models/OPNsense/Wol/Wol.php @@ -0,0 +1,35 @@ + + //wol + Wake On Lan configuration + 1.0.0 + + + + Y + N + + + /^(?!0).*$/ + + + + Y + N + /^((?:[a-f0-9]{2}:){5}(?:[a-f0-9]{2}))$/ + 00:00:00:00:00:00 + + + N + N + + + + + diff --git a/net/wol/src/opnsense/mvc/app/views/OPNsense/Wol/index.volt b/net/wol/src/opnsense/mvc/app/views/OPNsense/Wol/index.volt new file mode 100644 index 000000000..4704519e1 --- /dev/null +++ b/net/wol/src/opnsense/mvc/app/views/OPNsense/Wol/index.volt @@ -0,0 +1,104 @@ + +
+ {{ partial("layout_partials/base_form",['fields':wakeForm,'id':'frm_wol_wake'])}} +
+
+ +
+
+ +
+ + + + + + + + + + + + + + + + + + +
{{ lang._('Interface') }}{{ lang._('MAC') }}{{ lang._('Description') }}{{ lang._('Commands') }}
+ + +
+
+ + +{{ partial("layout_partials/base_dialog",['fields': hostForm,'id':'frm_wol_settings', 'label':lang._('Edit WOL Host')]) }} diff --git a/net/wol/src/opnsense/service/conf/actions.d/actions_wol.conf b/net/wol/src/opnsense/service/conf/actions.d/actions_wol.conf new file mode 100644 index 000000000..bc90627a3 --- /dev/null +++ b/net/wol/src/opnsense/service/conf/actions.d/actions_wol.conf @@ -0,0 +1,4 @@ +[wake] +command:/usr/local/bin/wol -i +parameters: %s %s +type:script diff --git a/net/wol/src/www/services_wol.php b/net/wol/src/www/services_wol.php deleted file mode 100644 index 044520270..000000000 --- a/net/wol/src/www/services_wol.php +++ /dev/null @@ -1,267 +0,0 @@ -. - All rights reserved. - - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions are met: - - 1. Redistributions of source code must retain the above copyright notice, - this list of conditions and the following disclaimer. - - 2. Redistributions in binary form must reproduce the above copyright - notice, this list of conditions and the following disclaimer in the - documentation and/or other materials provided with the distribution. - - THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, - INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY - AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE - AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, - OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - POSSIBILITY OF SUCH DAMAGE. -*/ - -require_once("guiconfig.inc"); -require_once("interfaces.inc"); - -$a_wol = &config_read_array('wol', 'wolentry'); - - -if ($_SERVER['REQUEST_METHOD'] === 'POST') { - // delete entry - if (isset($_POST['act']) && $_POST['act'] == "del" && isset($_POST['id'])) { - if (!empty($a_wol[$_POST['id']])) { - unset($a_wol[$_POST['id']]); - write_config(); - } - exit; - } elseif (isset($_POST['act']) && $_POST['act'] == "wakeall") { - $savemsg = ""; - $result = array(); - foreach ($a_wol as $wolent) { - $if = $wolent['interface']; - $ipaddr = get_interface_ip($if); - if (!is_ipaddr($ipaddr)) { - continue; - } - $bcip = escapeshellarg(gen_subnet_max($ipaddr, get_interface_subnet($if))); - /* Execute wol command and check return code. */ - if (!mwexec("/usr/local/bin/wol -i {$bcip} ". escapeshellarg($wolent['mac']))) { - $result[] = sprintf(gettext('Sent magic packet to %s (%s).'), htmlspecialchars($wolent['mac']), $wolent['descr']); - } else { - $result[] = sprintf(gettext('Please check the %ssystem log%s, the wol command for %s (%s) did not complete successfully.'), '', '', $wolent['descr'], htmlspecialchars($wolent['mac'])); - } - } - echo json_encode($result); - exit; - } elseif (isset($_POST['mac'])) { - /* input validation */ - if (empty($_POST['mac']) || !is_macaddr($_POST['mac'])) { - $input_errors[] = gettext("A valid MAC address must be specified."); - } - if (empty($_POST['if'])) { - $input_errors[] = gettext("A valid interface must be specified."); - } else { - $ipaddr = get_interface_ip($_POST['if']); - if (!is_ipaddr($ipaddr)) { - $input_errors[] = gettext("A valid ip could not be found!"); - } - } - - if (count($input_errors) == 0) { - /* determine broadcast address */ - $bcip = escapeshellarg(gen_subnet_max($ipaddr, get_interface_subnet($_POST['if']))); - /* Execute wol command and check return code. */ - if(!mwexec("/usr/local/bin/wol -i {$bcip} " . escapeshellarg($_POST['mac']))) { - $savemsg = sprintf(gettext('Sent magic packet to %s.'), $_POST['mac']); - } else { - $savemsg = sprintf(gettext('Please check the %ssystem log%s, the wol command for %s did not complete successfully.'), '', '', $_POST['mac']); - } - } - } -} - -include("head.inc"); -?> - - - - -
-
-
- 0) print_input_errors($input_errors); ?> - -
-
-
-
-
- - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - -    -
- - -
- " /> - -
  - " /> -
- - - -
-
-
-
-
-
-
-
-
-
- - - - - - - - - - - - - - - - - - - - - - - - -
- -
- - - - - - - - -
-
- -
-
-
-
-
-
-
-
- diff --git a/net/wol/src/www/services_wol_edit.php b/net/wol/src/www/services_wol_edit.php deleted file mode 100644 index 28cdf1096..000000000 --- a/net/wol/src/www/services_wol_edit.php +++ /dev/null @@ -1,169 +0,0 @@ -. - All rights reserved. - - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions are met: - - 1. Redistributions of source code must retain the above copyright notice, - this list of conditions and the following disclaimer. - - 2. Redistributions in binary form must reproduce the above copyright - notice, this list of conditions and the following disclaimer in the - documentation and/or other materials provided with the distribution. - - THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, - INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY - AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE - AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, - OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - POSSIBILITY OF SUCH DAMAGE. -*/ - -require_once("guiconfig.inc"); -require_once("interfaces.inc"); - -function wolcmp($a, $b) { - return strcmp($a['descr'], $b['descr']); -} - - -$a_wol = &config_read_array('wol', 'wolentry'); - -if ($_SERVER['REQUEST_METHOD'] === 'GET') { - if (isset($_GET['id']) && !empty($a_wol[$_GET['id']])) { - $id = $_GET['id']; - } - $pconfig = array(); - foreach (array('interface', 'mac', 'descr') as $fieldname) { - if (isset($id) && isset($a_wol[$id][$fieldname])) { - $pconfig[$fieldname] = $a_wol[$id][$fieldname]; - } elseif (isset($_GET[$fieldname])) { - $pconfig[$fieldname] = $_GET[$fieldname]; - } else { - $pconfig[$fieldname] = null; - } - } -} elseif ($_SERVER['REQUEST_METHOD'] === 'POST') { - if (isset($_POST['id']) && !empty($a_wol[$_POST['id']])) { - $id = $_POST['id']; - } - $pconfig = $_POST; - $input_errors = array(); - - /* input validation */ - $reqdfields = explode(" ", "interface mac"); - $reqdfieldsn = array(gettext("Interface"),gettext("MAC address")); - - do_input_validation($pconfig, $reqdfields, $reqdfieldsn, $input_errors); - - /* normalize MAC addresses - lowercase and convert Windows-ized hyphenated MACs to colon delimited */ - $pconfig['mac'] = strtolower(str_replace("-", ":", $pconfig['mac'])); - - if (!empty($pconfig['mac']) && !is_macaddr($_POST['mac'])) { - $input_errors[] = gettext("A valid MAC address must be specified."); - } - if (count($input_errors) == 0) { - $wolent = array(); - $wolent['interface'] = $_POST['interface']; - $wolent['mac'] = $_POST['mac']; - $wolent['descr'] = $_POST['descr']; - - if (isset($id)) { - $a_wol[$id] = $wolent; - } else { - $a_wol[] = $wolent; - } - usort($config['wol']['wolentry'], "wolcmp"); - write_config(); - - header(url_safe('Location: /services_wol.php')); - exit; - } -} - -include("head.inc"); -legacy_html_escape_form_data($pconfig); -?> - - - -
-
-
- 0) print_input_errors($input_errors); ?> -
-
-
-
- - - - - - - - - - - - - - - - - - - - - -
- - - - -
- - -
- - -
- - -
  - " /> - " onclick="window.location.href='/services_wol.php'" /> - - - -
-
-
-
-
-
-
-
- diff --git a/net/wol/src/www/widgets/widgets/wake_on_lan.widget.php b/net/wol/src/www/widgets/widgets/wake_on_lan.widget.php index ab376667d..19a68e9ad 100644 --- a/net/wol/src/www/widgets/widgets/wake_on_lan.widget.php +++ b/net/wol/src/www/widgets/widgets/wake_on_lan.widget.php @@ -30,12 +30,8 @@ require_once("guiconfig.inc"); require_once("widgets/include/wake_on_lan.inc"); require_once("interfaces.inc"); -if (isset($config['wol']['wolentry'])) { - $wolcomputers = $config['wol']['wolentry']; -} else { - $wolcomputers = array(); -} - +use OPNsense\Wol\Wol; +$wol = new Wol(); ?> @@ -48,24 +44,24 @@ if (isset($config['wol']['wolentry'])) { + foreach ($wol->wolentry->__items as $wolent): + $is_active = exec("/usr/sbin/arp -an |/usr/bin/grep {$wolent->mac}| /usr/bin/wc -l|/usr/bin/awk '{print $1;}'");?> - - + + + if (count($wol->wolentry->__items) == 0):?> @@ -78,3 +74,11 @@ if (isset($config['wol']['wolentry'])) {

descr ?>
mac ?>
interface));?> text-" > - +
+