From bf4c93f6b081b631b30129e5e88590be4f676896 Mon Sep 17 00:00:00 2001 From: Franco Fichtner Date: Tue, 4 Mar 2025 11:37:29 +0100 Subject: [PATCH] system: remove xmlparse.inc now since it's mostly unused Keep the dreaded listtags() around, but it's already pretty sparsely used. A brave new world. --- plist | 1 - src/etc/inc/auth.inc | 1 + src/etc/inc/config.inc | 35 +++- src/etc/inc/plugins.inc | 2 +- src/etc/inc/xmlparse.inc | 273 ------------------------- src/opnsense/scripts/shell/setaddr.php | 1 - 6 files changed, 34 insertions(+), 279 deletions(-) delete mode 100644 src/etc/inc/xmlparse.inc diff --git a/plist b/plist index d1339c228..7e3354d1c 100644 --- a/plist +++ b/plist @@ -47,7 +47,6 @@ /usr/local/etc/inc/rrd.inc /usr/local/etc/inc/system.inc /usr/local/etc/inc/util.inc -/usr/local/etc/inc/xmlparse.inc /usr/local/etc/inc/xmlrpc.inc /usr/local/etc/inc/xmlrpc/legacy.inc /usr/local/etc/inc/xmlrpc/service.inc diff --git a/src/etc/inc/auth.inc b/src/etc/inc/auth.inc index b997c6eb8..61376f433 100644 --- a/src/etc/inc/auth.inc +++ b/src/etc/inc/auth.inc @@ -173,6 +173,7 @@ function &getGroupEntry($name) function get_user_privileges(&$user) { $privs = []; + /* legacy listtags() ensures 'priv' will be an array when offered, also when there's only one */ foreach (!empty($user['priv']) ? (array)$user['priv'] : [] as $item) { foreach (array_filter(explode(',', $item)) as $priv) { diff --git a/src/etc/inc/config.inc b/src/etc/inc/config.inc index 834b5db82..e84f97091 100644 --- a/src/etc/inc/config.inc +++ b/src/etc/inc/config.inc @@ -77,6 +77,36 @@ final class product } } +function listtags() +{ + /* The following items will be treated as arrays in config.xml */ + $ret = [ + 'acls', 'alias', 'aliasurl', 'allowedip', 'allowedhostname', 'authserver', + 'bridged', 'build_port_path', + 'ca', 'cacert', 'cert', 'crl', 'clone', 'config', 'container', 'columnitem', + 'dhcp_ranges', 'disk', 'dnsserver', 'dnsupdate', 'domainoverrides', + 'element', 'encryption-algorithm-option', + 'field', 'fieldname', + 'gateway_item', 'gateway_group', 'gif', 'gre', 'group', + 'hash-algorithm-option', 'hosts', + 'ifgroupentry', 'igmpentry', 'interface_array', 'item', + 'key', + 'lagg', 'lbpool', + 'member', 'menu', 'mobilekey', 'monitor_type', 'mount', + 'npt', 'ntpserver', + 'onetoone', 'openvpn-server', 'openvpn-client', 'openvpn-csc', 'option', + 'package', 'passthrumac', 'phase1', 'phase2', 'ppp', 'pppoe', 'priv', 'proxyarpnet', 'pool', 'pages', 'pipe', + 'radnsserver', 'roll', 'route', 'row', 'rrddatafile', 'rule', + 'schedule', 'service', 'servernat', 'servers', 'serversdisabled', 'staticmap', 'subqueue', + 'tab', 'timerange', 'tunnel', + 'user', + 'vip', 'virtual_server', 'vlan', + 'winsserver', 'wolentry', 'widget', + ]; + + return array_flip($ret); +} + function reopenlog() { openlog(product::getInstance()->id(), LOG_ODELAY, LOG_USER); @@ -84,7 +114,6 @@ function reopenlog() reopenlog(); -require_once("xmlparse.inc"); require_once("legacy_bindings.inc"); require_once("certs.inc"); @@ -301,8 +330,8 @@ function legacy_config_get_interfaces($filters = [], $exclude_ifs = []) if (in_array($ifname, $exclude_ifs)) { continue; } - // undo stupid listags() turning our item into a new array, preventing certain names to be used as interface. - // see src/etc/inc/xmlparse.inc + + // undo stupid listtags() turning our item into a new array, preventing certain names to be used as interface if (isset($iface[0])) { $iface = $iface[0]; } diff --git a/src/etc/inc/plugins.inc b/src/etc/inc/plugins.inc index 7e8e11983..ff98e3ead 100644 --- a/src/etc/inc/plugins.inc +++ b/src/etc/inc/plugins.inc @@ -206,7 +206,7 @@ function plugins_interfaces() unset($stale_interfaces[$intf_ref]); } if (isset($config['interfaces'][$intf_ref][0])) { - // undo stupid listags() turning our item into a new array, see src/etc/inc/xmlparse.inc + // undo stupid listtags() turning our item into a new array $intf_config = &config_read_array('interfaces', $intf_ref, 0); } else { $intf_config = &config_read_array('interfaces', $intf_ref); diff --git a/src/etc/inc/xmlparse.inc b/src/etc/inc/xmlparse.inc deleted file mode 100644 index 1dad86846..000000000 --- a/src/etc/inc/xmlparse.inc +++ /dev/null @@ -1,273 +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. -*/ - -function listtags() -{ - /* The following items will be treated as arrays in config.xml */ - $ret = array( - 'acls', 'alias', 'aliasurl', 'allowedip', 'allowedhostname', 'authserver', - 'bridged', 'build_port_path', - 'ca', 'cacert', 'cert', 'crl', 'clone', 'config', 'container', 'columnitem', - 'dhcp_ranges', 'disk', 'dnsserver', 'dnsupdate', 'domainoverrides', - 'element', 'encryption-algorithm-option', - 'field', 'fieldname', - 'gateway_item', 'gateway_group', 'gif', 'gre', 'group', - 'hash-algorithm-option', 'hosts', - 'ifgroupentry', 'igmpentry', 'interface_array', 'item', - 'key', - 'lagg', 'lbpool', - 'member', 'menu', 'mobilekey', 'monitor_type', 'mount', - 'npt', 'ntpserver', - 'onetoone', 'openvpn-server', 'openvpn-client', 'openvpn-csc', 'option', - 'package', 'passthrumac', 'phase1', 'phase2', 'ppp', 'pppoe', 'priv', 'proxyarpnet', 'pool', 'pages', 'pipe', - 'radnsserver', 'roll', 'route', 'row', 'rrddatafile', 'rule', - 'schedule', 'service', 'servernat', 'servers', 'serversdisabled', 'staticmap', 'subqueue', - 'tab', 'timerange', 'tunnel', - 'user', - 'vip', 'virtual_server', 'vlan', - 'winsserver', 'wolentry', 'widget' - ); - - return array_flip($ret); -} - -function startElement($parser, $name, $attrs) -{ - global $parsedcfg, $depth, $curpath, $havedata, $listtags; - - array_push($curpath, strtolower($name)); - - $ptr =& $parsedcfg; - foreach ($curpath as $path) { - $ptr =& $ptr[$path]; - } - - /* is it an element that belongs to a list? */ - if (isset($listtags[strtolower($name)])) { - /* is there an array already? */ - if (!is_array($ptr)) { - /* make an array */ - $ptr = array(); - } - array_push($curpath, count($ptr)); - } elseif (isset($ptr)) { - /* multiple entries not allowed for this element, bail out */ - die(sprintf( - gettext('XML error: %s at line %d cannot occur more than once') . "\n", - $name, - xml_get_current_line_number($parser) - )); - } - $depth++; - $havedata = $depth; -} - -function endElement($parser, $name) -{ - global $depth, $curpath, $parsedcfg, $havedata, $listtags; - - if ($havedata == $depth) { - $ptr =& $parsedcfg; - foreach ($curpath as $path) { - $ptr =& $ptr[$path]; - } - $ptr = ""; - } - - array_pop($curpath); - if (isset($listtags[strtolower($name)])) { - array_pop($curpath); - } - - $depth--; -} - -function cData($parser, $data) -{ - global $curpath, $parsedcfg, $havedata; - - $data = trim($data, "\t\n\r"); - - if ($data != "") { - $ptr =& $parsedcfg; - foreach ($curpath as $path) { - $ptr =& $ptr[$path]; - } - if (is_string($ptr)) { - $ptr .= html_entity_decode($data); - } else { - if (trim($data, " ") != "") { - $ptr = html_entity_decode($data); - $havedata++; - } - } - } -} - -function parse_xml_config_raw($cffile, $rootobj, $isstring = "false") -{ - global $depth, $curpath, $parsedcfg, $havedata; - $parsedcfg = array(); - $curpath = array(); - $depth = 0; - $havedata = 0; - - $xml_parser = xml_parser_create(); - - xml_set_element_handler($xml_parser, "startElement", "endElement"); - xml_set_character_data_handler($xml_parser, "cdata"); - xml_parser_set_option($xml_parser, XML_OPTION_SKIP_WHITE, 1); - - if (!($fp = fopen($cffile, "r"))) { - log_msg('Error: could not open XML input', LOG_ERR); - return -1; - } - - while ($data = fread($fp, 4096)) { - if (!xml_parse($xml_parser, $data, feof($fp))) { - log_msg(sprintf( - 'XML error: %s at line %d in %s' . "\n", - xml_error_string(xml_get_error_code($xml_parser)), - xml_get_current_line_number($xml_parser), - $cffile - ), LOG_ERR); - return -1; - } - } - xml_parser_free($xml_parser); - - if ($rootobj) { - if (!is_array($rootobj)) { - $rootobj = array($rootobj); - } - foreach ($rootobj as $rootobj_name) { - if ($parsedcfg[$rootobj_name]) { - break; - } - } - if (!$parsedcfg[$rootobj_name]) { - log_msg(sprintf('XML error: no %s object found!' . "\n", implode(" or ", $rootobj)), LOG_ERR); - return -1; - } - return $parsedcfg[$rootobj_name]; - } else { - return $parsedcfg; - } -} - -function dump_xml_config_sub($arr, $indent) -{ - global $listtags; - - $xmlconfig = ""; - - foreach ($arr as $ent => $val) { - if (is_array($val)) { - /* is it just a list of multiple values? */ - if (isset($listtags[strtolower($ent)])) { - foreach ($val as $cval) { - if (is_array($cval)) { - if (empty($cval)) { - $xmlconfig .= str_repeat("\t", $indent); - $xmlconfig .= "<$ent/>\n"; - } else { - $xmlconfig .= str_repeat("\t", $indent); - $xmlconfig .= "<$ent>\n"; - $xmlconfig .= dump_xml_config_sub($cval, $indent + 1); - $xmlconfig .= str_repeat("\t", $indent); - $xmlconfig .= "\n"; - } - } else { - if ($cval === false) { - continue; - } - $xmlconfig .= str_repeat("\t", $indent); - if ((is_bool($cval) && $cval == true) || ($cval === "")) { - $xmlconfig .= "<$ent/>\n"; - } elseif ( - (substr($ent, 0, 5) == "descr") - || (substr($ent, 0, 6) == "detail") - || (substr($ent, 0, 12) == "login_banner") - || (substr($ent, 0, 9) == "ldap_attr") - || (substr($ent, 0, 9) == "ldap_bind") - || (substr($ent, 0, 11) == "ldap_basedn") - || (substr($ent, 0, 18) == "ldap_authcn") - || (substr($ent, 0, 19) == "ldap_extended_query") - ) { - $xmlconfig .= "<$ent>\n"; - } else { - $xmlconfig .= "<$ent>" . htmlentities($cval) . "\n"; - } - } - } - } elseif (empty($val)) { - $xmlconfig .= str_repeat("\t", $indent); - $xmlconfig .= "<$ent/>\n"; - } else { - /* it's an array */ - $xmlconfig .= str_repeat("\t", $indent); - $xmlconfig .= "<$ent>\n"; - $xmlconfig .= dump_xml_config_sub($val, $indent + 1); - $xmlconfig .= str_repeat("\t", $indent); - $xmlconfig .= "\n"; - } - } else { - if ((is_bool($val) && ($val == true)) || ($val === "")) { - $xmlconfig .= str_repeat("\t", $indent); - $xmlconfig .= "<$ent/>\n"; - } elseif (!is_bool($val)) { - $xmlconfig .= str_repeat("\t", $indent); - if ( - (substr($ent, 0, 5) == "descr") - || (substr($ent, 0, 6) == "detail") - || (substr($ent, 0, 12) == "login_banner") - || (substr($ent, 0, 9) == "ldap_attr") - || (substr($ent, 0, 9) == "ldap_bind") - || (substr($ent, 0, 11) == "ldap_basedn") - || (substr($ent, 0, 18) == "ldap_authcn") - || (substr($ent, 0, 19) == "ldap_extended_query") - ) { - $xmlconfig .= "<$ent>\n"; - } else { - $xmlconfig .= "<$ent>" . htmlentities($val) . "\n"; - } - } - } - } - - return $xmlconfig; -} - -function dump_xml_config_raw($arr, $rootobj) -{ - $xmlconfig = "\n"; - $xmlconfig .= "<$rootobj>\n"; - $xmlconfig .= dump_xml_config_sub($arr, 1); - $xmlconfig .= "\n"; - return $xmlconfig; -} diff --git a/src/opnsense/scripts/shell/setaddr.php b/src/opnsense/scripts/shell/setaddr.php index 6993c86da..895bdc1fb 100755 --- a/src/opnsense/scripts/shell/setaddr.php +++ b/src/opnsense/scripts/shell/setaddr.php @@ -34,7 +34,6 @@ require_once("util.inc"); require_once("filter.inc"); require_once("util.inc"); require_once("system.inc"); -require_once("xmlparse.inc"); function console_prompt_for_yn($prompt_text, $default = '') {