Project

General

Profile

Download (11.7 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 3274f985 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 d5d2c514 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 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.';
118 8da3de34 Colin Smith
$backup_config_section_sig = array(
119
				array(
120 03d89831 Scott Ullrich
					$XML_RPC_Struct, // First signature element is return value.
121 8da3de34 Colin Smith
					$XML_RPC_String,
122 03d89831 Scott Ullrich
					$XML_RPC_Array
123 8da3de34 Colin Smith
				)
124
			);
125 21dc3a7d Colin Smith
126 57e6d4c9 Colin Smith
function backup_config_section_xmlrpc($raw_params) {
127 03d89831 Scott Ullrich
	global $config, $xmlrpc_g;
128 8da3de34 Colin Smith
	$params = xmlrpc_params_to_php($raw_params);
129
	if(!xmlrpc_auth($params)) return $xmlrpc_g['return']['authfail'];
130 03d89831 Scott Ullrich
	$val = array_intersect_key($config, array_flip($params[0]));
131 8da3de34 Colin Smith
	return new XML_RPC_Response(XML_RPC_encode($val));
132 50d49018 Colin Smith
}
133
134 8da3de34 Colin Smith
/*****************************/
135
136 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.';
137 8da3de34 Colin Smith
$restore_config_section_sig = array(
138
					array(
139
						$XML_RPC_Boolean,
140
						$XML_RPC_String,
141 03d89831 Scott Ullrich
						$XML_RPC_Struct
142 8da3de34 Colin Smith
					)
143
				);
144 21dc3a7d Colin Smith
145 57e6d4c9 Colin Smith
function restore_config_section_xmlrpc($raw_params) {
146 03d89831 Scott Ullrich
	global $config, $xmlrpc_g;
147 57e6d4c9 Colin Smith
	$params = xmlrpc_params_to_php($raw_params);
148 8da3de34 Colin Smith
	if(!xmlrpc_auth($params)) return $xmlrpc_g['return']['authfail'];
149 cdcb273e Scott Ullrich
	
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 cdcb273e 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 8da3de34 Colin Smith
/*****************************/
180
181 dc1cd85d Scott Ullrich
182
$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.';
183
$merge_config_section_sig = array(
184
					array(
185
						$XML_RPC_Boolean,
186
						$XML_RPC_String,
187
						$XML_RPC_Struct
188
					)
189
				);
190
191
function merge_config_section_xmlrpc($raw_params) {
192
	global $config, $xmlrpc_g;
193
	$params = xmlrpc_params_to_php($raw_params);
194
	if(!xmlrpc_auth($params)) return $xmlrpc_g['return']['authfail'];
195 ee87d929 Scott Ullrich
	$config = array_merge_recursive_unique($config, $params[0]);
196 dc1cd85d Scott Ullrich
	$mergedkeys = implode(",", array_keys($params[0]));
197
	write_config("Merged in config ({$mergedkeys} sections) from XMLRPC client.");
198
	return $xmlrpc_g['return']['true'];
199
}
200
201
/*****************************/
202
203 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.';
204 8da3de34 Colin Smith
$filter_configure_sig = array(
205
				array(
206
					$XML_RPC_Boolean,
207
					$XML_RPC_String
208
				)
209
			);
210 07a7b08f Colin Smith
211
function filter_configure_xmlrpc($raw_params) {
212 8da3de34 Colin Smith
	global $xmlrpc_g;
213 07a7b08f Colin Smith
	$params = xmlrpc_params_to_php($raw_params);
214 8da3de34 Colin Smith
	if(!xmlrpc_auth($params)) return $xmlrpc_g['return']['authfail'];
215 d93bf3a5 Scott Ullrich
	require_once("vslb.inc");
216
	slbd_configure();
217 07a7b08f Colin Smith
	filter_configure();
218 e700437b Scott Ullrich
	system_routing_configure();
219 8da3de34 Colin Smith
	return $xmlrpc_g['return']['true'];
220 07a7b08f Colin Smith
}
221
222 8da3de34 Colin Smith
/*****************************/
223
224 c9c1b2ef 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.';
225 8da3de34 Colin Smith
$check_firmware_version_sig = array(
226
					array(
227
						$XML_RPC_String,
228
						$XML_RPC_String
229
					)
230
				);
231 44488c3c Colin Smith
232
function check_firmware_version_xmlrpc($raw_params) {
233 c9c1b2ef Bill Marquette
	global $xmlrpc_g, $XML_RPC_String;
234
	$params = xmlrpc_params_to_php($raw_params);
235
	if(!xmlrpc_auth($params)) return $xmlrpc_g['return']['authfail'];
236 8da3de34 Colin Smith
	return new XML_RPC_Response(new XML_RPC_Value(check_firmware_version(false), $XML_RPC_String));
237 44488c3c Colin Smith
}
238
239 8da3de34 Colin Smith
/*****************************/
240 44488c3c Colin Smith
241 bd0fe65b Colin Smith
$reboot_doc = 'Basic XMLRPC wrapper for rc.reboot.';
242 1cdca133 Colin Smith
$reboot_sig = array(array($XML_RPC_Boolean, $XML_RPC_String));
243 bd0fe65b Colin Smith
244
function reboot_xmlrpc($raw_params) {
245 8da3de34 Colin Smith
	global $xmlrpc_g;
246 bd0fe65b Colin Smith
	$params = xmlrpc_params_to_php($raw_params);
247 8da3de34 Colin Smith
	if(!xmlrpc_auth($params)) return $xmlrpc_g['return']['authfail'];
248 53cf5533 Colin Smith
	mwexec_bg("/etc/rc.reboot");
249 8da3de34 Colin Smith
	return $xmlrpc_g['return']['true'];
250 bd0fe65b Colin Smith
}
251
252 8da3de34 Colin Smith
/*****************************/
253
254
$get_notices_sig = array(
255
				array(
256 dc1cd85d Scott Ullrich
					$XML_RPC_Array,
257 8da3de34 Colin Smith
					$XML_RPC_String
258
				),
259
				array(
260
					$XML_RPC_Array
261
				)
262
			);
263
264 d9064267 Colin Smith
function get_notices_xmlrpc($raw_params) {
265 8da3de34 Colin Smith
	global $g, $xmlrpc_g;
266 c9c1b2ef Bill Marquette
	$params = xmlrpc_params_to_php($raw_params);
267
	if(!xmlrpc_auth($params)) return $xmlrpc_g['return']['authfail'];
268 d9064267 Colin Smith
	require_once("notices.inc");
269 8da3de34 Colin Smith
	if(!$params) {
270 d9064267 Colin Smith
		$toreturn = get_notices();
271
	} else {
272 8da3de34 Colin Smith
		$toreturn = get_notices($params);
273 d9064267 Colin Smith
	}
274
	$response = new XML_RPC_Response(XML_RPC_encode($toreturn));
275
	return $response;
276
}
277
278 8da3de34 Colin Smith
/*****************************/
279
280
$carp_configure_doc = 'Basic XMLRPC wrapper for configuring CARP interfaces.';
281 670fe849 Scott Ullrich
$carp_configure_sig = array(array($XML_RPC_Boolean, $XML_RPC_String));
282
283 52c3baf5 Scott Ullrich
function interfaces_carp_configure_xmlrpc($raw_params) {
284 670fe849 Scott Ullrich
	global $xmlrpc_g;
285 728d393d Colin Smith
	$params = xmlrpc_params_to_php($raw_params);
286 670fe849 Scott Ullrich
	if(!xmlrpc_auth($params)) return $xmlrpc_g['return']['authfail'];
287 52c3baf5 Scott Ullrich
	interfaces_carp_configure();
288 985e98ee Scott Ullrich
	interfaces_carp_bring_up_final();
289 670fe849 Scott Ullrich
	return $xmlrpc_g['return']['true'];
290 52c3baf5 Scott Ullrich
}
291
292 8da3de34 Colin Smith
/*****************************/
293
294 21dc3a7d Colin Smith
$server = new XML_RPC_Server(
295
        array(
296 d5d2c514 Scott Ullrich
            'pfsense.exec_shell' 		=> array('function' => 'exec_shell_xmlrpc',
297
							'signature' => $exec_shell_sig,
298
							'docstring' => $exec_shell_doc),
299
            'pfsense.exec_php'	 		=> array('function' => 'exec_php_xmlrpc',
300
							'signature' => $exec_php_sig,
301
							'docstring' => $exec_php_doc),	
302 52c3baf5 Scott Ullrich
            'pfsense.interfaces_carp_configure' => array('function' => 'interfaces_carp_configure_xmlrpc',
303
							'signature' => $carp_configure_doc,
304
							'docstring' => $carp_configure_sig),
305 07a7b08f Colin Smith
            'pfsense.backup_config_section' => 	array('function' => 'backup_config_section_xmlrpc',
306 21dc3a7d Colin Smith
							'signature' => $backup_config_section_sig,
307
							'docstring' => $backup_config_section_doc),
308
	    'pfsense.restore_config_section' => array('function' => 'restore_config_section_xmlrpc',
309
							'signature' => $restore_config_section_sig,
310 07a7b08f Colin Smith
							'docstring' => $restore_config_section_doc),
311 dc1cd85d Scott Ullrich
	    'pfsense.merge_config_section' => array('function' => 'merge_config_section_xmlrpc',
312
							'signature' => $merge_config_section_sig,
313
							'docstring' => $merge_config_section_doc),
314 cdcb273e Scott Ullrich
	    'pfsense.merge_installedpackages_section_xmlrpc' => array('function' => 'merge_installedpackages_section_xmlrpc',
315
							'signature' => $merge_config_section_sig,
316
							'docstring' => $merge_config_section_doc),							
317 d5d2c514 Scott Ullrich
318
	    'pfsense.merge_installedpackages_section_xmlrpc' => array('function' => 'merge_installedpackages_section_xmlrpc',
319
							'signature' => $merge_config_section_sig,
320
							'docstring' => $merge_config_section_doc),
321
							
322 07a7b08f Colin Smith
	    'pfsense.filter_configure' => 	array('function' => 'filter_configure_xmlrpc',
323
							'signature' => $filter_configure_sig,
324 44488c3c Colin Smith
							'docstring' => $filter_configure_doc),
325
	    'pfsense.check_firmware_version' =>	array('function' => 'check_firmware_version_xmlrpc',
326
							'signature' => $check_firmware_version_sig,
327 bd0fe65b Colin Smith
							'docstring' => $check_firmware_version_doc),
328
	    'pfsense.reboot' =>			array('function' => 'reboot_xmlrpc',
329
							'signature' => $reboot_sig,
330 d9064267 Colin Smith
							'docstring' => $reboot_doc),
331
	    'pfsense.get_notices' =>		array('function' => 'get_notices_xmlrpc',
332
							'signature' => $get_notices_sig)
333 50d49018 Colin Smith
        )
334
);
335 c9c1b2ef Bill Marquette
?>