From ab558e7e2538787db75be31225c6778787ea1f6f Mon Sep 17 00:00:00 2001 From: Franco Fichtner Date: Tue, 3 Jan 2017 23:02:48 +0100 Subject: [PATCH] net/snmp: export from core --- net/snmp/Makefile | 8 + net/snmp/src/etc/inc/plugins.inc.d/bsnmpd.inc | 220 +++++++++++ .../mvc/app/models/OPNsense/SNMP/ACL/ACL.xml | 8 + .../app/models/OPNsense/SNMP/Menu/Menu.xml | 5 + net/snmp/src/www/services_snmp.php | 356 ++++++++++++++++++ 5 files changed, 597 insertions(+) create mode 100644 net/snmp/Makefile create mode 100644 net/snmp/src/etc/inc/plugins.inc.d/bsnmpd.inc create mode 100644 net/snmp/src/opnsense/mvc/app/models/OPNsense/SNMP/ACL/ACL.xml create mode 100644 net/snmp/src/opnsense/mvc/app/models/OPNsense/SNMP/Menu/Menu.xml create mode 100644 net/snmp/src/www/services_snmp.php diff --git a/net/snmp/Makefile b/net/snmp/Makefile new file mode 100644 index 000000000..3e77f3ff1 --- /dev/null +++ b/net/snmp/Makefile @@ -0,0 +1,8 @@ +PLUGIN_NAME= snmp +PLUGIN_VERSION= 0.1 +PLUGIN_PRIVATE= yes +PLUGIN_DEPENDS= bsnmp-regex bsnmp-ucd +PLUGIN_COMMENT= SNMP Server via bsnmpd +PLUGIN_MAINTAINER= franco@opnsense.org + +.include "../../Mk/plugins.mk" diff --git a/net/snmp/src/etc/inc/plugins.inc.d/bsnmpd.inc b/net/snmp/src/etc/inc/plugins.inc.d/bsnmpd.inc new file mode 100644 index 000000000..5d504fc2a --- /dev/null +++ b/net/snmp/src/etc/inc/plugins.inc.d/bsnmpd.inc @@ -0,0 +1,220 @@ + + Copyright (C) 2010 Ermal Luci + Copyright (C) 2005-2006 Colin Smith + Copyright (C) 2003-2004 Manuel Kasper + 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. +*/ + +function bsnmpd_enabled() +{ + global $config; + + return isset($config['snmpd']['enable']); +} + +function bsnmpd_services() +{ + $services = array(); + + if (!bsnmpd_enabled()) { + return $services; + } + + $pconfig = array(); + $pconfig['name'] = 'bsnmpd'; + $pconfig['description'] = gettext('SNMP Service'); + $pconfig['php']['restart'] = array('bsnmpd_configure_do'); + $pconfig['php']['start'] = array('bsnmpd_configure_do'); + $pconfig['pidfile'] = '/var/run/snmpd.pid'; + $services[] = $pconfig; + + return $services; +} + +function bsnmpd_configure() +{ + return array( + 'bootup' => array('bsnmpd_configure_do'), + 'interface' => array('bsnmpd_configure_do'), + ); +} + +function bsnmpd_configure_do($verbose = false) +{ + global $config, $g; + + killbyname('bsnmpd'); + + if (!bsnmpd_enabled()) { + return; + } + + if ($verbose) { + echo 'Starting SNMP daemon...'; + flush(); + } + + $snmpdconf = << + + Services: SNMP + + services_snmp.php* + + + diff --git a/net/snmp/src/opnsense/mvc/app/models/OPNsense/SNMP/Menu/Menu.xml b/net/snmp/src/opnsense/mvc/app/models/OPNsense/SNMP/Menu/Menu.xml new file mode 100644 index 000000000..873f544c1 --- /dev/null +++ b/net/snmp/src/opnsense/mvc/app/models/OPNsense/SNMP/Menu/Menu.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/net/snmp/src/www/services_snmp.php b/net/snmp/src/www/services_snmp.php new file mode 100644 index 000000000..65e5a793a --- /dev/null +++ b/net/snmp/src/www/services_snmp.php @@ -0,0 +1,356 @@ +. + 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"); +require_once("services.inc"); +require_once("system.inc"); +require_once('plugins.inc.d/bsnmpd.inc'); + +if ($_SERVER['REQUEST_METHOD'] === 'GET') { + $pconfig = array(); + if (empty($config['snmpd']) || !is_array($config['snmpd'])) { + // set defaults (no config) + $pconfig['rocommunity'] = "public"; + $pconfig['pollport'] = "161"; + $pconfig['mibii'] = true; + $pconfig['netgraph'] = true; + $pconfig['pf'] = true; + $pconfig['hostres'] = true; + $pconfig['bridge'] = true; + $pconfig['ucd'] = true; + $pconfig['regex'] = true; + } else { + // modules + foreach (array('mibii', 'netgraph', 'pf', 'hostres', 'bridge', 'ucd', 'regex') as $module) { + $pconfig[$module] = !empty($config['snmpd']['modules'][$module]); + } + // booleans + $pconfig['enable'] = isset($config['snmpd']['enable']); + $pconfig['trapenable'] = isset($config['snmpd']['trapenable']); + // text fields + foreach (array('rocommunity', 'pollport', 'syslocation', 'syscontact', + 'trapserver', 'trapserverport', 'trapstring', 'bindip') as $fieldname) { + $pconfig[$fieldname] = !empty($config['snmpd'][$fieldname]) ? $config['snmpd'][$fieldname] : null; + } + } +} elseif ($_SERVER['REQUEST_METHOD'] === 'POST') { + $input_errors = array(); + $pconfig = $_POST; + // input validation + if (strstr($pconfig['syslocation'],"#")) { + $input_errors[] = gettext("Invalid character '#' in system location"); + } elseif (preg_match('/[^\x20-\x7f]/', $pconfig['syslocation'])) { + $input_errors[] = gettext("Invalid character (non ascii) in system location"); + } + + if (strstr($pconfig['syscontact'],"#")) { + $input_errors[] = gettext("Invalid character '#' in system contact"); + } elseif (preg_match('/[^\x20-\x7f]/', $pconfig['syscontact'])) { + $input_errors[] = gettext("Invalid character (non ascii) in system contact"); + } + + if (strstr($pconfig['rocommunity'],"#")) { + $input_errors[] = gettext("Invalid character '#' in read community string"); + } + + if (!empty($pconfig['enable'])) { + $reqdfields = array("rocommunity", "pollport"); + $reqdfieldsn = array(gettext("Community"), gettext("Polling Port")); + do_input_validation($pconfig, $reqdfields, $reqdfieldsn, $input_errors); + } + + if (strstr($pconfig['trapstring'],"#")) { + $input_errors[] = gettext("Invalid character '#' in SNMP trap string"); + } + + if (!empty($pconfig['trapenable'])) { + $reqdfields = array("trapserver", "trapserverport", "trapstring"); + $reqdfieldsn = array(gettext("Trap server"), gettext("Trap server port"), gettext("Trap string")); + do_input_validation($pconfig, $reqdfields, $reqdfieldsn, $input_errors); + } + + if (count($input_errors) == 0) { + // save form data + // modules + $snmp = array(); + $snmp['modules'] = array(); + foreach (array('mibii', 'netgraph', 'pf', 'hostres', 'bridge', 'ucd', 'regex') as $module) { + $snmp['modules'][$module] = !empty($pconfig[$module]); + } + // booleans + $snmp['enable'] = !empty($pconfig['enable']); + $snmp['trapenable'] = !empty($pconfig['trapenable']); + // text fields + foreach (array('rocommunity', 'pollport', 'syslocation', 'syscontact', + 'trapserver', 'trapserverport', 'trapstring', 'bindip') as $fieldname) { + $snmp[$fieldname] = $pconfig[$fieldname]; + } + $config['snmpd'] = $snmp; + // save and apply + write_config(); + bsnmpd_configure_do(); + header(url_safe('Location: /services_snmp.php')); + exit; + } +} + +$service_hook = 'bsnmpd'; +legacy_html_escape_form_data($pconfig); +include("head.inc"); + +?> + + + + +
+
+
+ 0) print_input_errors($input_errors); ?> +
+
+
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + +    +
+ /> +
+ + +
+ +
+ +
+ + +
+
+
+
+
+
+
+ + + + + + + + + + + + + + + + + + + + + + + + +
+ /> +
+ + +
+ + +
+ + +
+
+
+
+
+
+
+ + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + +
+ /> +
+ /> +
+ /> +
+ /> +
+ /> +
+ /> +
+
+
+
+
+
+
+
+ + + + + + + + + + + + + + + + +
+ +
  + " /> +
+
+
+
+
+
+
+
+