<commit_before>#!/usr/local/bin/php
<?php
/* $Id$ */
/*
        xmlrpc.php
        Copyright (C) 2005 <NAME>
        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.

	TODO:
		* Expose more functions.
		* Add syslog handling of errors.
		* Define XML_RPC_erruser.
		* Write handlers for PHP -> XML_RPC_Value.
		* xmlrpc_params_to_php currently does *not* handle structs.
*/

require_once("xmlrpc_server.inc");
require_once("xmlparse_pkg.inc");
require_once("config.inc");
require_once("functions.inc");

// Helper functions.
/*
 *   xmlrpc_auth: Handle basic crypt() authentication of the XMLRPC request. This function assumes that
 *                $params[0] contains the local system's plaintext password and removes the password from
 *		  the array before returning it.
 */
function xmlrpc_auth(&$params) {
	global $config;
	if (crypt($params[0], $config['system']['password']) != $config['system']['password'])
		return false; // Password didn't match.
	array_shift($params); // Shift the password parameter off of the array.
	return true; // Password matched.
}

/*
 *   xmlrpc_params_to_php: Convert params array passed from XMLRPC server into a PHP array and return it.
 *
 *   XXX: This function does not currently handle XML_RPC_Value objects of type "struct".
 */
function xmlrpc_params_to_php($params) {
	$array = array();
	for($i = 0; $i < $params->getNumParams(); $i++) {
		$value = $params->getParam($i);
		if($value->kindOf() == "scalar") {
			$array[] = $value->scalarval();
		} elseif($value->kindOf() == "array") {
			$array[] = xmlrpc_array_to_php($value);
		}
	}
	return $array;
}

/*
 *   xmlrpc_array_to_php: Convert an XMLRPC array into a PHP array and return it.
 */
function xmlrpc_array_to_php($array) {
	$return = array();
	$array_length = $array->arraysize();
	for($i = 0; $i < $array->arraysize(); $i++) {
		$value = $array->arraymem($i);
		if($value->kindOf() == "scalar") {
			$return[] = $value->scalarval();
		} elseif($value->kindOf() == "array") {
			$return[] = xmlrpc_array_to_php($value);
		}
	}
	return $return;
}

// Exposed functions.

$backup_config_section_doc = 'XMLRPC wrapper for backup_config_section. This method must be called with two parameters: a string containing the local system\'s password followed by a string containing the section to be backed up.';
$backup_config_section_sig = array(array(string, string, string));

function backup_config_section_xmlrpc($raw_params) {
	$params = xmlrpc_params_to_php($raw_params); // Convert XML_RPC_Value objects to a PHP array of values.
	if(!xmlrpc_auth($params)) return new XML_RPC_Response(new XML_RPC_Value("auth_failure", 'string'));
	$val = new XML_RPC_Value(backup_config_section($params[0]), 'string'); 
	return new XML_RPC_Response($val);
}

$restore_config_section_doc = 'XMLRPC wrapper for restore_config_section. This method must be called with three parameters: a string containing the local system\'s password, a string containing the section to be restored, and a string containing the returned value of backup_config_section() for that section. This function returns true upon completion.';
$restore_config_section_sig = array(array(boolean, string, string, string));

function restore_config_section_xmlrpc($raw_params) {
	$params = xmlrpc_params_to_php($raw_params);
	$i = 0;
	if(!xmlrpc_auth($params)) return new XML_RPC_Response(new XML_RPC_Value("auth_failure", 'string'));
	foreach($params[0] as $section) {
		restore_config_section($section, $params[1][$i]);
		$i++;
	}
	return new XML_RPC_Response(new XML_RPC_Value(true, 'boolean'));
}

$filter_configure_doc = 'Basic XMLRPC wrapper for filter_configure. This method must be called with one paramater: a string containing the local system\'s password. This function returns true upon completion.';
$filter_configure_sig = array(array(boolean, string));

function filter_configure_xmlrpc($raw_params) {
	$params = xmlrpc_params_to_php($raw_params);
	if(!xmlrpc_auth($params)) return new XML_RPC_Response(new XML_RPC_Value("auth_failure", 'string'));
	filter_configure();
	return new XML_PRC_Response(new XML_RPC_Value(true, 'boolean'));
}

$server = new XML_RPC_Server(
        array(
            'pfsense.backup_config_section' => 	array('function' => 'backup_config_section_xmlrpc',
							'signature' => $backup_config_section_sig,
							'docstring' => $backup_config_section_doc),
	    'pfsense.restore_config_section' => array('function' => 'restore_config_section_xmlrpc',
							'signature' => $restore_config_section_sig,
							'docstring' => $restore_config_section_doc),
	    'pfsense.filter_configure' => 	array('function' => 'filter_configure_xmlrpc',<commit_msg>* Add signature handling for arrays. * Fix pfsense.filter_configure. * Fix multicallish pfsense.restore_config_section.<commit_after>#!/usr/local/bin/php
<?php
/* $Id$ */
/*
        xmlrpc.php
        Copyright (C) 2005 <NAME>
        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.

	TODO:
		* Expose more functions.
		* Add syslog handling of errors.
		* Define XML_RPC_erruser.
		* Write handlers for PHP -> XML_RPC_Value.
		* xmlrpc_params_to_php currently does *not* handle structs.
*/

require_once("xmlrpc_server.inc");
require_once("xmlparse_pkg.inc");
require_once("config.inc");
require_once("functions.inc");

// Helper functions.
/*
 *   xmlrpc_auth: Handle basic crypt() authentication of the XMLRPC request. This function assumes that
 *                $params[0] contains the local system's plaintext password and removes the password from
 *		  the array before returning it.
 */
function xmlrpc_auth(&$params) {
	global $config;
	if (crypt($params[0], $config['system']['password']) != $config['system']['password'])
		return false; // Password didn't match.
	array_shift($params); // Shift the password parameter off of the array.
	return true; // Password matched.
}

/*
 *   xmlrpc_params_to_php: Convert params array passed from XMLRPC server into a PHP array and return it.
 *
 *   XXX: This function does not currently handle XML_RPC_Value objects of type "struct".
 */
function xmlrpc_params_to_php($params) {
	$array = array();
	for($i = 0; $i < $params->getNumParams(); $i++) {
		$value = $params->getParam($i);
		if($value->kindOf() == "scalar") {
			$array[] = $value->scalarval();
		} elseif($value->kindOf() == "array") {
			$array[] = xmlrpc_array_to_php($value);
		}
	}
	return $array;
}

/*
 *   xmlrpc_array_to_php: Convert an XMLRPC array into a PHP array and return it.
 */
function xmlrpc_array_to_php($array) {
	$return = array();
	$array_length = $array->arraysize();
	for($i = 0; $i < $array->arraysize(); $i++) {
		$value = $array->arraymem($i);
		if($value->kindOf() == "scalar") {
			$return[] = $value->scalarval();
		} elseif($value->kindOf() == "array") {
			$return[] = xmlrpc_array_to_php($value);
		}
	}
	return $return;
}

// Exposed functions.

$backup_config_section_doc = 'XMLRPC wrapper for backup_config_section. This method must be called with two parameters: a string containing the local system\'s password followed by a string containing the section to be backed up.';
$backup_config_section_sig = array(array(string, string, string));

function backup_config_section_xmlrpc($raw_params) {
	$params = xmlrpc_params_to_php($raw_params); // Convert XML_RPC_Value objects to a PHP array of values.
	if(!xmlrpc_auth($params)) return new XML_RPC_Response(new XML_RPC_Value("auth_failure", 'string'));
	$val = new XML_RPC_Value(backup_config_section($params[0]), 'string'); 
	return new XML_RPC_Response($val);
}

$restore_config_section_doc = 'XMLRPC wrapper for restore_config_section. This method must be called with three parameters: a string containing the local system\'s password, a string containing the section to be restored, and a string containing the returned value of backup_config_section() for that section. This function returns true upon completion.';
$restore_config_section_sig = array(array(boolean, string, array(), array()));

function restore_config_section_xmlrpc($raw_params) {
	$params = xmlrpc_params_to_php($raw_params);
	$i = 0;
	if(!xmlrpc_auth($params)) return new XML_RPC_Response(new XML_RPC_Value("auth_failure", 'string'));
	foreach($params[0] as $section) {
		restore_config_section($section, $params[1][$i]);
		$i++;
	}
	return new XML_RPC_Response(new XML_RPC_Value(true, 'boolean'));
}

$filter_configure_doc = 'Basic XMLRPC wrapper for filter_configure. This method must be called with one paramater: a string containing the local system\'s password. This function returns true upon completion.';
$filter_configure_sig = array(array(boolean, string));

function filter_configure_xmlrpc($raw_params) {
	$params = xmlrpc_params_to_php($raw_params);
	if(!xmlrpc_auth($params)) return new XML_RPC_Response(new XML_RPC_Value("auth_failure", 'string'));
	filter_configure();
	return new XML_RPC_Response(new XML_RPC_Value(true, 'boolean'));
}

$server = new XML_RPC_Server(
        array(
            'pfsense.backup_config_section' => 	array('function' => 'backup_config_section_xmlrpc',
							'signature' => $backup_config_section_sig,
							'docstring' => $backup_config_section_doc),
	    'pfsense.restore_config_section' => array('function' => 'restore_config_section_xmlrpc',
							'signature' => $restore_config_section_sig,
							'docstring' => $restore_config_section_doc),
	    'pfsense.filter_configure' => 	array('function' => 'filter_configure_xmlrpc',
{'fraction_non_alphanumeric': 0.10947930574098798, 'fraction_numerical': 0.002336448598130841, 'mean_word_length': 4.4626253418413855, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 48}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5415964', 'n_tokens_mistral': 3645, 'n_tokens_neox': 3425, 'n_words': 1125}