Project

General

Profile

Download (11.5 KB) Statistics
| Branch: | Tag: | Revision:
1 50d49018 Colin Smith
<?php
2
/*
3 81d8a9ad Colin Smith
	$Id$
4 dc1cd85d Scott Ullrich
5 50d49018 Colin Smith
        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 8da3de34 Colin Smith
        POSSIBILITY OF SUCH DAMAGE._Value(
29 780dbd56 Colin Smith
30
	TODO:
31
		* Expose more functions.
32 50d49018 Colin Smith
*/
33
34
require_once("xmlrpc_server.inc");
35 6fe6b916 Colin Smith
require_once("xmlrpc.inc");
36 50d49018 Colin Smith
require_once("config.inc");
37
require_once("functions.inc");
38 03d89831 Scott Ullrich
require_once("array_intersect_key.inc");
39 50d49018 Colin Smith
40 ff664954 Scott Ullrich
/* grab sync to ip if enabled */
41
if ($config['installedpackages']['carpsettings']['config']) {
42
	foreach ($config['installedpackages']['carpsettings']['config'] as $carp) {
43
		$synchronizetoip = $carp['synchronizetoip'];
44
	}
45
}
46
47
if($synchronizetoip) {
48
	if($synchronizetoip == $_SERVER['REMOTE_ADDR']) {
49
		log_error("Disallowing CARP sync loop.");
50
		die;	
51
	}
52
}
53
54 8da3de34 Colin Smith
$xmlrpc_g = array(
55
			"return" => array(
56
						"true" => new XML_RPC_Response(new XML_RPC_Value(true, $XML_RPC_Boolean)),
57
						"false" => new XML_RPC_Response(new XML_RPC_Value(false, $XML_RPC_Boolean)),
58
						"authfail" => new XML_RPC_Response(0, $XML_RPC_erruser+1, "Authentication failure")
59
				)
60
		);
61
62
/*
63
 *   pfSense XMLRPC errors
64
 *   $XML_RPC_erruser + 1 = Auth failure
65
 */
66
$XML_RPC_erruser = 200;
67
68
/* EXPOSED FUNCTIONS */
69 21dc3a7d Colin Smith
70 c3638879 Scott Ullrich
$exec_php_doc = 'XMLRPC wrapper for eval(). This method must be called with two parameters: a string containing the local system\'s password followed by the PHP code to evaluate.';
71
$exec_php_sig = array(
72
				array(
73
					$XML_RPC_Boolean, // First signature element is return value.
74
					$XML_RPC_String, // password
75
					$XML_RPC_String, // shell code to exec
76
				)
77
			);
78
79
80
function exec_php_xmlrpc($raw_params) {
81
	global $config, $xmlrpc_g;
82
	$params = xmlrpc_params_to_php($raw_params);
83
	if(!xmlrpc_auth($params)) return $xmlrpc_g['return']['authfail'];
84
	$exec_php = $params[0];
85
	eval($exec_php);
86
	return $xmlrpc_g['return']['true'];
87
}
88
89
90
91
/*****************************/
92
93
94
$exec_shell_doc = 'XMLRPC wrapper for mwexec(). This method must be called with two parameters: a string containing the local system\'s password followed by an shell command to execute.';
95
$exec_shell_sig = array(
96
				array(
97
					$XML_RPC_Boolean, // First signature element is return value.
98
					$XML_RPC_String, // password
99
					$XML_RPC_String, // shell code to exec
100
				)
101
			);
102
103
104
function exec_shell_xmlrpc($raw_params) {
105
	global $config, $xmlrpc_g;
106
	$params = xmlrpc_params_to_php($raw_params);
107
	if(!xmlrpc_auth($params)) return $xmlrpc_g['return']['authfail'];
108
	$shell_cmd = $params[0];
109
	mwexec($shell_cmd);
110
	return $xmlrpc_g['return']['true'];
111
}
112
113
114
115
/*****************************/
116
117
118 03d89831 Scott Ullrich
$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 an array containing the keys to be backed up.';
119 8da3de34 Colin Smith
$backup_config_section_sig = array(
120
				array(
121 03d89831 Scott Ullrich
					$XML_RPC_Struct, // First signature element is return value.
122 8da3de34 Colin Smith
					$XML_RPC_String,
123 03d89831 Scott Ullrich
					$XML_RPC_Array
124 8da3de34 Colin Smith
				)
125
			);
126 21dc3a7d Colin Smith
127 57e6d4c9 Colin Smith
function backup_config_section_xmlrpc($raw_params) {
128 03d89831 Scott Ullrich
	global $config, $xmlrpc_g;
129 8da3de34 Colin Smith
	$params = xmlrpc_params_to_php($raw_params);
130
	if(!xmlrpc_auth($params)) return $xmlrpc_g['return']['authfail'];
131 03d89831 Scott Ullrich
	$val = array_intersect_key($config, array_flip($params[0]));
132 8da3de34 Colin Smith
	return new XML_RPC_Response(XML_RPC_encode($val));
133 50d49018 Colin Smith
}
134
135 8da3de34 Colin Smith
/*****************************/
136
137 03d89831 Scott Ullrich
$restore_config_section_doc = 'XMLRPC wrapper for restore_config_section. This method must be called with two parameters: a string containing the local system\'s password and an array to merge into the system\'s config. This function returns true upon completion.';
138 8da3de34 Colin Smith
$restore_config_section_sig = array(
139
					array(
140
						$XML_RPC_Boolean,
141
						$XML_RPC_String,
142 03d89831 Scott Ullrich
						$XML_RPC_Struct
143 8da3de34 Colin Smith
					)
144
				);
145 21dc3a7d Colin Smith
146 57e6d4c9 Colin Smith
function restore_config_section_xmlrpc($raw_params) {
147 03d89831 Scott Ullrich
	global $config, $xmlrpc_g;
148 57e6d4c9 Colin Smith
	$params = xmlrpc_params_to_php($raw_params);
149 8da3de34 Colin Smith
	if(!xmlrpc_auth($params)) return $xmlrpc_g['return']['authfail'];
150 03d89831 Scott Ullrich
	$config = array_merge($config, $params[0]);
151
	$mergedkeys = implode(",", array_keys($params[0]));
152
	write_config("Merged in config ({$mergedkeys} sections) from XMLRPC client.");
153 8da3de34 Colin Smith
	return $xmlrpc_g['return']['true'];
154 50d49018 Colin Smith
}
155
156 82ae5cfc Scott Ullrich
157
/*****************************/
158
159
160
$merge_config_section_doc = 'XMLRPC wrapper for merging package sections. This method must be called with two parameters: a string containing the local system\'s password and an array to merge into the system\'s config. This function returns true upon completion.';
161
$merge_config_section_sig = array(
162
					array(
163
						$XML_RPC_Boolean,
164
						$XML_RPC_String,
165
						$XML_RPC_Struct
166
					)
167
				);
168
169
function merge_installedpackages_section_xmlrpc($raw_params) {
170
	global $config, $xmlrpc_g;
171
	$params = xmlrpc_params_to_php($raw_params);
172
	if(!xmlrpc_auth($params)) return $xmlrpc_g['return']['authfail'];
173
	$config['installedpackages'] = array_merge($config['installedpackages'], $params[0]);
174
	$mergedkeys = implode(",", array_keys($params[0]));
175
	write_config("Merged in config ({$mergedkeys} sections) from XMLRPC client.");
176
	return $xmlrpc_g['return']['true'];
177
}
178
179
180 8da3de34 Colin Smith
/*****************************/
181
182 dc1cd85d Scott Ullrich
183
$merge_config_section_doc = 'XMLRPC wrapper for merge_config_section. This method must be called with two parameters: a string containing the local system\'s password and an array to merge into the system\'s config. This function returns true upon completion.';
184
$merge_config_section_sig = array(
185
					array(
186
						$XML_RPC_Boolean,
187
						$XML_RPC_String,
188
						$XML_RPC_Struct
189
					)
190
				);
191
192
function merge_config_section_xmlrpc($raw_params) {
193
	global $config, $xmlrpc_g;
194
	$params = xmlrpc_params_to_php($raw_params);
195
	if(!xmlrpc_auth($params)) return $xmlrpc_g['return']['authfail'];
196 ee87d929 Scott Ullrich
	$config = array_merge_recursive_unique($config, $params[0]);
197 dc1cd85d Scott Ullrich
	$mergedkeys = implode(",", array_keys($params[0]));
198
	write_config("Merged in config ({$mergedkeys} sections) from XMLRPC client.");
199
	return $xmlrpc_g['return']['true'];
200
}
201
202
/*****************************/
203
204 07a7b08f Colin Smith
$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.';
205 8da3de34 Colin Smith
$filter_configure_sig = array(
206
				array(
207
					$XML_RPC_Boolean,
208
					$XML_RPC_String
209
				)
210
			);
211 07a7b08f Colin Smith
212
function filter_configure_xmlrpc($raw_params) {
213 8da3de34 Colin Smith
	global $xmlrpc_g;
214 07a7b08f Colin Smith
	$params = xmlrpc_params_to_php($raw_params);
215 8da3de34 Colin Smith
	if(!xmlrpc_auth($params)) return $xmlrpc_g['return']['authfail'];
216 d93bf3a5 Scott Ullrich
	require_once("vslb.inc");
217
	slbd_configure();
218 07a7b08f Colin Smith
	filter_configure();
219 e700437b Scott Ullrich
	system_routing_configure();
220 8da3de34 Colin Smith
	return $xmlrpc_g['return']['true'];
221 07a7b08f Colin Smith
}
222
223 8da3de34 Colin Smith
/*****************************/
224
225 b4d19b46 Bill Marquette
$check_firmware_version_doc = 'Basic XMLRPC wrapper for check_firmware_version. This function will return the output of check_firmware_version upon completion.';
226 8da3de34 Colin Smith
$check_firmware_version_sig = array(
227
					array(
228
						$XML_RPC_String,
229
						$XML_RPC_String
230
					)
231
				);
232 44488c3c Colin Smith
233
function check_firmware_version_xmlrpc($raw_params) {
234 b4d19b46 Bill Marquette
	global $xmlrpc_g, $XML_RPC_String;
235
	$params = xmlrpc_params_to_php($raw_params);
236
	if(!xmlrpc_auth($params)) return $xmlrpc_g['return']['authfail'];
237 8da3de34 Colin Smith
	return new XML_RPC_Response(new XML_RPC_Value(check_firmware_version(false), $XML_RPC_String));
238 44488c3c Colin Smith
}
239
240 8da3de34 Colin Smith
/*****************************/
241 44488c3c Colin Smith
242 bd0fe65b Colin Smith
$reboot_doc = 'Basic XMLRPC wrapper for rc.reboot.';
243 1cdca133 Colin Smith
$reboot_sig = array(array($XML_RPC_Boolean, $XML_RPC_String));
244 bd0fe65b Colin Smith
245
function reboot_xmlrpc($raw_params) {
246 8da3de34 Colin Smith
	global $xmlrpc_g;
247 bd0fe65b Colin Smith
	$params = xmlrpc_params_to_php($raw_params);
248 8da3de34 Colin Smith
	if(!xmlrpc_auth($params)) return $xmlrpc_g['return']['authfail'];
249 53cf5533 Colin Smith
	mwexec_bg("/etc/rc.reboot");
250 8da3de34 Colin Smith
	return $xmlrpc_g['return']['true'];
251 bd0fe65b Colin Smith
}
252
253 8da3de34 Colin Smith
/*****************************/
254
255
$get_notices_sig = array(
256
				array(
257 dc1cd85d Scott Ullrich
					$XML_RPC_Array,
258 8da3de34 Colin Smith
					$XML_RPC_String
259
				),
260
				array(
261
					$XML_RPC_Array
262
				)
263
			);
264
265 d9064267 Colin Smith
function get_notices_xmlrpc($raw_params) {
266 8da3de34 Colin Smith
	global $g, $xmlrpc_g;
267 b4d19b46 Bill Marquette
	$params = xmlrpc_params_to_php($raw_params);
268
	if(!xmlrpc_auth($params)) return $xmlrpc_g['return']['authfail'];
269 d9064267 Colin Smith
	require_once("notices.inc");
270 8da3de34 Colin Smith
	if(!$params) {
271 d9064267 Colin Smith
		$toreturn = get_notices();
272
	} else {
273 8da3de34 Colin Smith
		$toreturn = get_notices($params);
274 d9064267 Colin Smith
	}
275
	$response = new XML_RPC_Response(XML_RPC_encode($toreturn));
276
	return $response;
277
}
278
279 8da3de34 Colin Smith
/*****************************/
280
281
$carp_configure_doc = 'Basic XMLRPC wrapper for configuring CARP interfaces.';
282 670fe849 Scott Ullrich
$carp_configure_sig = array(array($XML_RPC_Boolean, $XML_RPC_String));
283
284 52c3baf5 Scott Ullrich
function interfaces_carp_configure_xmlrpc($raw_params) {
285 670fe849 Scott Ullrich
	global $xmlrpc_g;
286 728d393d Colin Smith
	$params = xmlrpc_params_to_php($raw_params);
287 670fe849 Scott Ullrich
	if(!xmlrpc_auth($params)) return $xmlrpc_g['return']['authfail'];
288 52c3baf5 Scott Ullrich
	interfaces_carp_configure();
289 985e98ee Scott Ullrich
	interfaces_carp_bring_up_final();
290 670fe849 Scott Ullrich
	return $xmlrpc_g['return']['true'];
291 52c3baf5 Scott Ullrich
}
292
293 8da3de34 Colin Smith
/*****************************/
294
295 21dc3a7d Colin Smith
$server = new XML_RPC_Server(
296
        array(
297 c3638879 Scott Ullrich
            'pfsense.exec_shell' 		=> array('function' => 'exec_shell_xmlrpc',
298
							'signature' => $exec_shell_sig,
299
							'docstring' => $exec_shell_doc),
300
            'pfsense.exec_php'	 		=> array('function' => 'exec_php_xmlrpc',
301
							'signature' => $exec_php_sig,
302
							'docstring' => $exec_php_doc),	
303 52c3baf5 Scott Ullrich
            'pfsense.interfaces_carp_configure' => array('function' => 'interfaces_carp_configure_xmlrpc',
304
							'signature' => $carp_configure_doc,
305
							'docstring' => $carp_configure_sig),
306 07a7b08f Colin Smith
            'pfsense.backup_config_section' => 	array('function' => 'backup_config_section_xmlrpc',
307 21dc3a7d Colin Smith
							'signature' => $backup_config_section_sig,
308
							'docstring' => $backup_config_section_doc),
309
	    'pfsense.restore_config_section' => array('function' => 'restore_config_section_xmlrpc',
310
							'signature' => $restore_config_section_sig,
311 07a7b08f Colin Smith
							'docstring' => $restore_config_section_doc),
312 dc1cd85d Scott Ullrich
	    'pfsense.merge_config_section' => array('function' => 'merge_config_section_xmlrpc',
313
							'signature' => $merge_config_section_sig,
314
							'docstring' => $merge_config_section_doc),
315 c3638879 Scott Ullrich
	    'pfsense.merge_installedpackages_section_xmlrpc' => array('function' => 'merge_installedpackages_section_xmlrpc',
316
							'signature' => $merge_config_section_sig,
317
							'docstring' => $merge_config_section_doc),							
318 07a7b08f Colin Smith
	    'pfsense.filter_configure' => 	array('function' => 'filter_configure_xmlrpc',
319
							'signature' => $filter_configure_sig,
320 44488c3c Colin Smith
							'docstring' => $filter_configure_doc),
321
	    'pfsense.check_firmware_version' =>	array('function' => 'check_firmware_version_xmlrpc',
322
							'signature' => $check_firmware_version_sig,
323 bd0fe65b Colin Smith
							'docstring' => $check_firmware_version_doc),
324
	    'pfsense.reboot' =>			array('function' => 'reboot_xmlrpc',
325
							'signature' => $reboot_sig,
326 d9064267 Colin Smith
							'docstring' => $reboot_doc),
327
	    'pfsense.get_notices' =>		array('function' => 'get_notices_xmlrpc',
328
							'signature' => $get_notices_sig)
329 50d49018 Colin Smith
        )
330
);
331 36d0358b Scott Ullrich
?>