Project

General

Profile

Download (11.8 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
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._Value(
29

    
30
	TODO:
31
		* Expose more functions.
32
*/
33

    
34
##|+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
require("config.inc");
42
require("functions.inc");
43
require("filter.inc");
44
require("shaper.inc");
45
require("xmlrpc_server.inc");
46
require("xmlrpc.inc");
47
require("array_intersect_key.inc");
48

    
49
/* grab sync to ip if enabled */
50
if($config['installedpackages']['carpsettings']) {
51
	if ($config['installedpackages']['carpsettings']['config']) {
52
		foreach ($config['installedpackages']['carpsettings']['config'] as $carp) {
53
			$synchronizetoip = $carp['synchronizetoip'];
54
		}
55
	}
56
}
57

    
58
if($synchronizetoip) {
59
	if($synchronizetoip == $_SERVER['REMOTE_ADDR']) {
60
		log_error("Disallowing CARP sync loop.");
61
		die;	
62
	}
63
}
64

    
65
$xmlrpc_g = array(
66
			"return" => array(
67
						"true" => new XML_RPC_Response(new XML_RPC_Value(true, $XML_RPC_Boolean)),
68
						"false" => new XML_RPC_Response(new XML_RPC_Value(false, $XML_RPC_Boolean)),
69
						"authfail" => new XML_RPC_Response(0, $XML_RPC_erruser+1, "Authentication failure")
70
				)
71
		);
72

    
73
/*
74
 *   pfSense XMLRPC errors
75
 *   $XML_RPC_erruser + 1 = Auth failure
76
 */
77
$XML_RPC_erruser = 200;
78

    
79
/* EXPOSED FUNCTIONS */
80

    
81
$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.';
82
$exec_php_sig = array(
83
				array(
84
					$XML_RPC_Boolean, // First signature element is return value.
85
					$XML_RPC_String, // password
86
					$XML_RPC_String, // shell code to exec
87
				)
88
			);
89

    
90

    
91
function exec_php_xmlrpc($raw_params) {
92
	global $config, $xmlrpc_g;
93
	$params = xmlrpc_params_to_php($raw_params);
94
	if(!xmlrpc_auth($params)) return $xmlrpc_g['return']['authfail'];
95
	$exec_php = $params[0];
96
	eval($exec_php);
97
	if($toreturn) {
98
		$response = XML_RPC_encode($toreturn);
99
		return new XML_RPC_Response($response);
100
	} else
101
		return $xmlrpc_g['return']['true'];
102
}
103

    
104

    
105

    
106
/*****************************/
107

    
108

    
109
$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.';
110
$exec_shell_sig = array(
111
				array(
112
					$XML_RPC_Boolean, // First signature element is return value.
113
					$XML_RPC_String, // password
114
					$XML_RPC_String, // shell code to exec
115
				)
116
			);
117

    
118

    
119
function exec_shell_xmlrpc($raw_params) {
120
	global $config, $xmlrpc_g;
121
	$params = xmlrpc_params_to_php($raw_params);
122
	if(!xmlrpc_auth($params)) return $xmlrpc_g['return']['authfail'];
123
	$shell_cmd = $params[0];
124
	mwexec($shell_cmd);
125
	return $xmlrpc_g['return']['true'];
126
}
127

    
128

    
129

    
130
/*****************************/
131

    
132

    
133
$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.';
134
$backup_config_section_sig = array(
135
				array(
136
					$XML_RPC_Struct, // First signature element is return value.
137
					$XML_RPC_String,
138
					$XML_RPC_Array
139
				)
140
			);
141

    
142
function backup_config_section_xmlrpc($raw_params) {
143
	global $config, $xmlrpc_g;
144
	$params = xmlrpc_params_to_php($raw_params);
145
	if(!xmlrpc_auth($params)) return $xmlrpc_g['return']['authfail'];
146
	$val = array_intersect_key($config, array_flip($params[0]));
147
	return new XML_RPC_Response(XML_RPC_encode($val));
148
}
149

    
150
/*****************************/
151

    
152
$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.';
153
$restore_config_section_sig = array(
154
					array(
155
						$XML_RPC_Boolean,
156
						$XML_RPC_String,
157
						$XML_RPC_Struct
158
					)
159
				);
160

    
161
function restore_config_section_xmlrpc($raw_params) {
162
	global $config, $xmlrpc_g;
163
	$params = xmlrpc_params_to_php($raw_params);
164
	if(!xmlrpc_auth($params)) return $xmlrpc_g['return']['authfail'];
165
	$config = array_merge($config, $params[0]);
166
	$mergedkeys = implode(",", array_keys($params[0]));
167
	write_config("Merged in config ({$mergedkeys} sections) from XMLRPC client.");
168
	return $xmlrpc_g['return']['true'];
169
}
170

    
171

    
172
/*****************************/
173

    
174

    
175
$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.';
176
$merge_config_section_sig = array(
177
					array(
178
						$XML_RPC_Boolean,
179
						$XML_RPC_String,
180
						$XML_RPC_Struct
181
					)
182
				);
183

    
184
function merge_installedpackages_section_xmlrpc($raw_params) {
185
	global $config, $xmlrpc_g;
186
	$params = xmlrpc_params_to_php($raw_params);
187
	if(!xmlrpc_auth($params)) return $xmlrpc_g['return']['authfail'];
188
	$config['installedpackages'] = array_merge($config['installedpackages'], $params[0]);
189
	$mergedkeys = implode(",", array_keys($params[0]));
190
	write_config("Merged in config ({$mergedkeys} sections) from XMLRPC client.");
191
	return $xmlrpc_g['return']['true'];
192
}
193

    
194

    
195
/*****************************/
196

    
197

    
198
$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.';
199
$merge_config_section_sig = array(
200
					array(
201
						$XML_RPC_Boolean,
202
						$XML_RPC_String,
203
						$XML_RPC_Struct
204
					)
205
				);
206

    
207
function merge_config_section_xmlrpc($raw_params) {
208
	global $config, $xmlrpc_g;
209
	$params = xmlrpc_params_to_php($raw_params);
210
	if(!xmlrpc_auth($params)) return $xmlrpc_g['return']['authfail'];
211
	$config = array_merge_recursive_unique($config, $params[0]);
212
	$mergedkeys = implode(",", array_keys($params[0]));
213
	write_config("Merged in config ({$mergedkeys} sections) from XMLRPC client.");
214
	return $xmlrpc_g['return']['true'];
215
}
216

    
217
/*****************************/
218

    
219
$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.';
220
$filter_configure_sig = array(
221
				array(
222
					$XML_RPC_Boolean,
223
					$XML_RPC_String
224
				)
225
			);
226

    
227
function filter_configure_xmlrpc($raw_params) {
228
	global $xmlrpc_g;
229
	$params = xmlrpc_params_to_php($raw_params);
230
	if(!xmlrpc_auth($params)) return $xmlrpc_g['return']['authfail'];
231
	require("vslb.inc");
232
	filter_configure();
233
	system_routing_configure();
234
	setup_gateways_monitor();
235
	relayd_configure();
236
	return $xmlrpc_g['return']['true'];
237
}
238

    
239
/*****************************/
240

    
241
$check_firmware_version_doc = 'Basic XMLRPC wrapper for check_firmware_version. This function will return the output of check_firmware_version upon completion.';
242
$check_firmware_version_sig = array(
243
					array(
244
						$XML_RPC_String,
245
						$XML_RPC_String
246
					)
247
				);
248

    
249
function check_firmware_version_xmlrpc($raw_params) {
250
	global $xmlrpc_g, $XML_RPC_String;
251
	$params = xmlrpc_params_to_php($raw_params);
252
	if(!xmlrpc_auth($params)) return $xmlrpc_g['return']['authfail'];
253
	return new XML_RPC_Response(new XML_RPC_Value(check_firmware_version(false), $XML_RPC_String));
254
}
255

    
256
/*****************************/
257

    
258
$reboot_doc = 'Basic XMLRPC wrapper for rc.reboot.';
259
$reboot_sig = array(array($XML_RPC_Boolean, $XML_RPC_String));
260

    
261
function reboot_xmlrpc($raw_params) {
262
	global $xmlrpc_g;
263
	$params = xmlrpc_params_to_php($raw_params);
264
	if(!xmlrpc_auth($params)) return $xmlrpc_g['return']['authfail'];
265
	mwexec_bg("/etc/rc.reboot");
266
	return $xmlrpc_g['return']['true'];
267
}
268

    
269
/*****************************/
270

    
271
$get_notices_sig = array(
272
				array(
273
					$XML_RPC_Array,
274
					$XML_RPC_String
275
				),
276
				array(
277
					$XML_RPC_Array
278
				)
279
			);
280

    
281
function get_notices_xmlrpc($raw_params) {
282
	global $g, $xmlrpc_g;
283
	$params = xmlrpc_params_to_php($raw_params);
284
	if(!xmlrpc_auth($params)) return $xmlrpc_g['return']['authfail'];
285
	require("notices.inc");
286
	if(!$params) {
287
		$toreturn = get_notices();
288
	} else {
289
		$toreturn = get_notices($params);
290
	}
291
	$response = new XML_RPC_Response(XML_RPC_encode($toreturn));
292
	return $response;
293
}
294

    
295
/*****************************/
296

    
297
$carp_configure_doc = 'Basic XMLRPC wrapper for configuring CARP interfaces.';
298
$carp_configure_sig = array(array($XML_RPC_Boolean, $XML_RPC_String));
299

    
300
function interfaces_carp_configure_xmlrpc($raw_params) {
301
	global $xmlrpc_g;
302
	$params = xmlrpc_params_to_php($raw_params);
303
	if(!xmlrpc_auth($params)) return $xmlrpc_g['return']['authfail'];
304
	interfaces_carp_setup();
305
	return $xmlrpc_g['return']['true'];
306
}
307

    
308
/*****************************/
309

    
310
$server = new XML_RPC_Server(
311
        array(
312
            'pfsense.exec_shell' 		=> array('function' => 'exec_shell_xmlrpc',
313
							'signature' => $exec_shell_sig,
314
							'docstring' => $exec_shell_doc),
315
            'pfsense.exec_php'	 		=> array('function' => 'exec_php_xmlrpc',
316
							'signature' => $exec_php_sig,
317
							'docstring' => $exec_php_doc),	
318
            'pfsense.interfaces_carp_configure' => array('function' => 'interfaces_carp_configure_xmlrpc',
319
							'signature' => $carp_configure_doc,
320
							'docstring' => $carp_configure_sig),
321
            'pfsense.backup_config_section' => 	array('function' => 'backup_config_section_xmlrpc',
322
							'signature' => $backup_config_section_sig,
323
							'docstring' => $backup_config_section_doc),
324
	    'pfsense.restore_config_section' => array('function' => 'restore_config_section_xmlrpc',
325
							'signature' => $restore_config_section_sig,
326
							'docstring' => $restore_config_section_doc),
327
	    'pfsense.merge_config_section' => array('function' => 'merge_config_section_xmlrpc',
328
							'signature' => $merge_config_section_sig,
329
							'docstring' => $merge_config_section_doc),
330
	    'pfsense.merge_installedpackages_section_xmlrpc' => array('function' => 'merge_installedpackages_section_xmlrpc',
331
							'signature' => $merge_config_section_sig,
332
							'docstring' => $merge_config_section_doc),							
333
	    'pfsense.filter_configure' => 	array('function' => 'filter_configure_xmlrpc',
334
							'signature' => $filter_configure_sig,
335
							'docstring' => $filter_configure_doc),
336
	    'pfsense.check_firmware_version' =>	array('function' => 'check_firmware_version_xmlrpc',
337
							'signature' => $check_firmware_version_sig,
338
							'docstring' => $check_firmware_version_doc),
339
	    'pfsense.reboot' =>			array('function' => 'reboot_xmlrpc',
340
							'signature' => $reboot_sig,
341
							'docstring' => $reboot_doc),
342
	    'pfsense.get_notices' =>		array('function' => 'get_notices_xmlrpc',
343
							'signature' => $get_notices_sig)
344
        )
345
);
346
?>
(214-214/214)