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