Project

General

Profile

Download (7.34 KB) Statistics
| Branch: | Tag: | Revision:
1
#!/usr/local/bin/php
2
<?php
3
/* $Id$ */
4
/*
5
        xmlrpc.php
6
        Copyright (C) 2005 Colin Smith
7
        All rights reserved.
8

    
9
        Redistribution and use in source and binary forms, with or without
10
        modification, are permitted provided that the following conditions are met:
11

    
12
        1. Redistributions of source code must retain the above copyright notice,
13
           this list of conditions and the following disclaimer.
14

    
15
        2. Redistributions in binary form must reproduce the above copyright
16
           notice, this list of conditions and the following disclaimer in the
17
           documentation and/or other materials provided with the distribution.
18

    
19
        THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
20
        INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
21
        AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22
        AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
23
        OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24
        SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25
        INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26
        CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27
        ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28
        POSSIBILITY OF SUCH DAMAGE.
29

    
30
	TODO:
31
		* Expose more functions.
32
		* Add syslog handling of errors.
33
		* Define XML_RPC_erruser.
34
		* Write handlers for PHP -> XML_RPC_Value.
35
		* xmlrpc_params_to_php currently does *not* handle structs.
36
*/
37

    
38
require_once("xmlrpc_server.inc");
39
require_once("xmlparse_pkg.inc");
40
require_once("config.inc");
41
require_once("functions.inc");
42

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

    
57
/*
58
 *   xmlrpc_params_to_php: Convert params array passed from XMLRPC server into a PHP array and return it.
59
 *
60
 *   XXX: This function does not currently handle XML_RPC_Value objects of type "struct".
61
 */
62
function xmlrpc_params_to_php($params) {
63
	$array = array();
64
	for($i = 0; $i < $params->getNumParams(); $i++) {
65
		$value = $params->getParam($i);
66
		if($value->kindOf() == "scalar") {
67
			$array[] = $value->scalarval();
68
		} elseif($value->kindOf() == "array") {
69
			$array[] = xmlrpc_array_to_php($value);
70
		}
71
	}
72
	return $array;
73
}
74

    
75
/*
76
 *   xmlrpc_array_to_php: Convert an XMLRPC array into a PHP array and return it.
77
 */
78
function xmlrpc_array_to_php($array) {
79
	$return = array();
80
	$array_length = $array->arraysize();
81
	for($i = 0; $i < $array->arraysize(); $i++) {
82
		$value = $array->arraymem($i);
83
		if($value->kindOf() == "scalar") {
84
			$return[] = $value->scalarval();
85
		} elseif($value->kindOf() == "array") {
86
			$return[] = xmlrpc_array_to_php($value);
87
		}
88
	}
89
	return $return;
90
}
91

    
92
// Exposed functions.
93

    
94
$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.';
95
$backup_config_section_sig = array(array(string, string, string));
96

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

    
104
$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.';
105
$restore_config_section_sig = array(array(boolean, string, array(), array()));
106

    
107
function restore_config_section_xmlrpc($raw_params) {
108
	$params = xmlrpc_params_to_php($raw_params);
109
	$i = 0;
110
	if(!xmlrpc_auth($params)) return new XML_RPC_Response(new XML_RPC_Value("auth_failure", 'string'));
111
	foreach($params[0] as $section) {
112
		restore_config_section($section, $params[1][$i]);
113
		$i++;
114
	}
115
	return new XML_RPC_Response(new XML_RPC_Value(true, 'boolean'));
116
}
117

    
118
$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.';
119
$filter_configure_sig = array(array(boolean, string));
120

    
121
function filter_configure_xmlrpc($raw_params) {
122
	$params = xmlrpc_params_to_php($raw_params);
123
	if(!xmlrpc_auth($params)) return new XML_RPC_Response(new XML_RPC_Value("auth_failure", 'string'));
124
	filter_configure();
125
	return new XML_RPC_Response(new XML_RPC_Value(true, 'boolean'));
126
}
127

    
128
$check_firmware_version_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 will return the output of check_firmware_version upon completion.'
129
$check_firmware_version_sig = array(array(string, string));
130

    
131
function check_firmware_version_xmlrpc($raw_params) {
132
	$params = xmlrpc_params_to_php($raw_params);
133
	if(!xmlrpc_auth($params)) return new XML_RPC_Response(new XML_RPC_Value("auth_failure", 'string'))
134
	return new XML_RPC_Response(new XML_RPC_Value(check_firmware_version(), 'string'));
135
}
136

    
137

    
138
$auto_update_doc = 'Basic XMLRPC wrapper for auto_update. This method must be called with one paramater: a string containing the local system\'s password. This function will return true upon completion.'
139
$auto_update_sig = array(array(boolean, string));
140

    
141
function auto_update_xmlrpc($raw_params) {
142
	$params = xmlrpc_params_to_php($raw_params);
143
        if(!xmlrpc_auth($params)) return new XML_RPC_Response(new XML_RPC_Value("auth_failure", 'string'))
144
	auto_update();
145
	return new XML_RPC_Response(new XML_RPC_Value(true, 'boolean'));
146
}
147

    
148
$server = new XML_RPC_Server(
149
        array(
150
            'pfsense.backup_config_section' => 	array('function' => 'backup_config_section_xmlrpc',
151
							'signature' => $backup_config_section_sig,
152
							'docstring' => $backup_config_section_doc),
153
	    'pfsense.restore_config_section' => array('function' => 'restore_config_section_xmlrpc',
154
							'signature' => $restore_config_section_sig,
155
							'docstring' => $restore_config_section_doc),
156
	    'pfsense.filter_configure' => 	array('function' => 'filter_configure_xmlrpc',
157
							'signature' => $filter_configure_sig,
158
							'docstring' => $filter_configure_doc),
159
	    'pfsense.check_firmware_version' =>	array('function' => 'check_firmware_version_xmlrpc',
160
							'signature' => $check_firmware_version_sig,
161
							'docstring' => $check_firmware_version_doc),
162
	    'pfsense.auto_update' =>		array('function' => 'auto_update_xmlrpc',
163
							'signature' => $auto_update_sig,
164
							'docstring' => $auto_update_doc)
165
        )
166
);
167
?>
(109-109/109)