Project

General

Profile

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