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
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
	return $xmlrpc_g['return']['true'];
98
}
99

    
100

    
101

    
102
/*****************************/
103

    
104

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

    
114

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

    
124

    
125

    
126
/*****************************/
127

    
128

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

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

    
146
/*****************************/
147

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

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

    
167

    
168
/*****************************/
169

    
170

    
171
$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.';
172
$merge_config_section_sig = array(
173
					array(
174
						$XML_RPC_Boolean,
175
						$XML_RPC_String,
176
						$XML_RPC_Struct
177
					)
178
				);
179

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

    
190

    
191
/*****************************/
192

    
193

    
194
$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.';
195
$merge_config_section_sig = array(
196
					array(
197
						$XML_RPC_Boolean,
198
						$XML_RPC_String,
199
						$XML_RPC_Struct
200
					)
201
				);
202

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

    
213
/*****************************/
214

    
215
$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.';
216
$filter_configure_sig = array(
217
				array(
218
					$XML_RPC_Boolean,
219
					$XML_RPC_String
220
				)
221
			);
222

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

    
235
/*****************************/
236

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

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

    
252
/*****************************/
253

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

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

    
265
/*****************************/
266

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

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

    
291
/*****************************/
292

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

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

    
304
/*****************************/
305

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