Project

General

Profile

Download (11.7 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

    
42
require_once("xmlrpc_server.inc");
43
require_once("xmlrpc.inc");
44
require_once("config.inc");
45
require_once("functions.inc");
46
require_once("array_intersect_key.inc");
47

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

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

    
64
$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

    
80
$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
$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
$backup_config_section_sig = array(
130
				array(
131
					$XML_RPC_Struct, // First signature element is return value.
132
					$XML_RPC_String,
133
					$XML_RPC_Array
134
				)
135
			);
136

    
137
function backup_config_section_xmlrpc($raw_params) {
138
	global $config, $xmlrpc_g;
139
	$params = xmlrpc_params_to_php($raw_params);
140
	if(!xmlrpc_auth($params)) return $xmlrpc_g['return']['authfail'];
141
	$val = array_intersect_key($config, array_flip($params[0]));
142
	return new XML_RPC_Response(XML_RPC_encode($val));
143
}
144

    
145
/*****************************/
146

    
147
$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
$restore_config_section_sig = array(
149
					array(
150
						$XML_RPC_Boolean,
151
						$XML_RPC_String,
152
						$XML_RPC_Struct
153
					)
154
				);
155

    
156
function restore_config_section_xmlrpc($raw_params) {
157
	global $config, $xmlrpc_g;
158
	$params = xmlrpc_params_to_php($raw_params);
159
	if(!xmlrpc_auth($params)) return $xmlrpc_g['return']['authfail'];
160
	$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
	return $xmlrpc_g['return']['true'];
164
}
165

    
166

    
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
/*****************************/
191

    
192

    
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
	$config = array_merge_recursive_unique($config, $params[0]);
207
	$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
$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
$filter_configure_sig = array(
216
				array(
217
					$XML_RPC_Boolean,
218
					$XML_RPC_String
219
				)
220
			);
221

    
222
function filter_configure_xmlrpc($raw_params) {
223
	global $xmlrpc_g;
224
	$params = xmlrpc_params_to_php($raw_params);
225
	if(!xmlrpc_auth($params)) return $xmlrpc_g['return']['authfail'];
226
	require_once("vslb.inc");
227
	filter_configure();
228
	system_routing_configure();
229
	setup_gateways_monitor();
230
	relayd_configure();
231
	return $xmlrpc_g['return']['true'];
232
}
233

    
234
/*****************************/
235

    
236
$check_firmware_version_doc = 'Basic XMLRPC wrapper for check_firmware_version. This function will return the output of check_firmware_version upon completion.';
237
$check_firmware_version_sig = array(
238
					array(
239
						$XML_RPC_String,
240
						$XML_RPC_String
241
					)
242
				);
243

    
244
function check_firmware_version_xmlrpc($raw_params) {
245
	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
	return new XML_RPC_Response(new XML_RPC_Value(check_firmware_version(false), $XML_RPC_String));
249
}
250

    
251
/*****************************/
252

    
253
$reboot_doc = 'Basic XMLRPC wrapper for rc.reboot.';
254
$reboot_sig = array(array($XML_RPC_Boolean, $XML_RPC_String));
255

    
256
function reboot_xmlrpc($raw_params) {
257
	global $xmlrpc_g;
258
	$params = xmlrpc_params_to_php($raw_params);
259
	if(!xmlrpc_auth($params)) return $xmlrpc_g['return']['authfail'];
260
	mwexec_bg("/etc/rc.reboot");
261
	return $xmlrpc_g['return']['true'];
262
}
263

    
264
/*****************************/
265

    
266
$get_notices_sig = array(
267
				array(
268
					$XML_RPC_Array,
269
					$XML_RPC_String
270
				),
271
				array(
272
					$XML_RPC_Array
273
				)
274
			);
275

    
276
function get_notices_xmlrpc($raw_params) {
277
	global $g, $xmlrpc_g;
278
	$params = xmlrpc_params_to_php($raw_params);
279
	if(!xmlrpc_auth($params)) return $xmlrpc_g['return']['authfail'];
280
	require_once("notices.inc");
281
	if(!$params) {
282
		$toreturn = get_notices();
283
	} else {
284
		$toreturn = get_notices($params);
285
	}
286
	$response = new XML_RPC_Response(XML_RPC_encode($toreturn));
287
	return $response;
288
}
289

    
290
/*****************************/
291

    
292
$carp_configure_doc = 'Basic XMLRPC wrapper for configuring CARP interfaces.';
293
$carp_configure_sig = array(array($XML_RPC_Boolean, $XML_RPC_String));
294

    
295
function interfaces_carp_configure_xmlrpc($raw_params) {
296
	global $xmlrpc_g;
297
	$params = xmlrpc_params_to_php($raw_params);
298
	if(!xmlrpc_auth($params)) return $xmlrpc_g['return']['authfail'];
299
	interfaces_carp_configure();
300
	return $xmlrpc_g['return']['true'];
301
}
302

    
303
/*****************************/
304

    
305
$server = new XML_RPC_Server(
306
        array(
307
            '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
            'pfsense.interfaces_carp_configure' => array('function' => 'interfaces_carp_configure_xmlrpc',
314
							'signature' => $carp_configure_doc,
315
							'docstring' => $carp_configure_sig),
316
            'pfsense.backup_config_section' => 	array('function' => 'backup_config_section_xmlrpc',
317
							'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
							'docstring' => $restore_config_section_doc),
322
	    'pfsense.merge_config_section' => array('function' => 'merge_config_section_xmlrpc',
323
							'signature' => $merge_config_section_sig,
324
							'docstring' => $merge_config_section_doc),
325
	    'pfsense.merge_installedpackages_section_xmlrpc' => array('function' => 'merge_installedpackages_section_xmlrpc',
326
							'signature' => $merge_config_section_sig,
327
							'docstring' => $merge_config_section_doc),							
328
	    'pfsense.filter_configure' => 	array('function' => 'filter_configure_xmlrpc',
329
							'signature' => $filter_configure_sig,
330
							'docstring' => $filter_configure_doc),
331
	    'pfsense.check_firmware_version' =>	array('function' => 'check_firmware_version_xmlrpc',
332
							'signature' => $check_firmware_version_sig,
333
							'docstring' => $check_firmware_version_doc),
334
	    'pfsense.reboot' =>			array('function' => 'reboot_xmlrpc',
335
							'signature' => $reboot_sig,
336
							'docstring' => $reboot_doc),
337
	    'pfsense.get_notices' =>		array('function' => 'get_notices_xmlrpc',
338
							'signature' => $get_notices_sig)
339
        )
340
);
341
?>
(203-203/203)