Project

General

Profile

Download (13.5 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
	$Id$
4

    
5
        xmlrpc.php
6
        Copyright (C) 2009, 2010 Scott Ullrich
7
        Copyright (C) 2005 Colin Smith
8
        All rights reserved.
9

    
10
        Redistribution and use in source and binary forms, with or without
11
        modification, are permitted provided that the following conditions are met:
12

    
13
        1. Redistributions of source code must retain the above copyright notice,
14
           this list of conditions and the following disclaimer.
15

    
16
        2. Redistributions in binary form must reproduce the above copyright
17
           notice, this list of conditions and the following disclaimer in the
18
           documentation and/or other materials provided with the distribution.
19

    
20
        THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
21
        INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
22
        AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23
        AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
24
        OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25
        SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26
        INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27
        CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28
        ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29
        POSSIBILITY OF SUCH DAMAGE.
30
*/
31

    
32
##|+PRIV
33
##|*IDENT=page-xmlrpclibrary
34
##|*NAME=XMLRPC Library page
35
##|*DESCR=Allow access to the 'XMLRPC Library' page.
36
##|*MATCH=xmlrpc.php*
37
##|-PRIV
38

    
39
require("config.inc");
40
require("functions.inc");
41
require("filter.inc");
42
require("shaper.inc");
43
require("xmlrpc_server.inc");
44
require("xmlrpc.inc");
45
require("array_intersect_key.inc");
46

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

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

    
63
$xmlrpc_g = array(
64
	"return" => array(
65
		"true" => new XML_RPC_Response(new XML_RPC_Value(true, $XML_RPC_Boolean)),
66
		"false" => new XML_RPC_Response(new XML_RPC_Value(false, $XML_RPC_Boolean)),
67
		"authfail" => new XML_RPC_Response(new XML_RPC_Value(gettext("Authentication failed"), $XML_RPC_String))
68
	)
69
);
70

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

    
77
/* EXPOSED FUNCTIONS */
78
$exec_php_doc = gettext("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.");
79
$exec_php_sig = array(
80
	array(
81
		$XML_RPC_Boolean, // First signature element is return value.
82
		$XML_RPC_String, // password
83
		$XML_RPC_String, // shell code to exec
84
	)
85
);
86

    
87
function exec_php_xmlrpc($raw_params) {
88
	global $config, $xmlrpc_g;
89

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

    
102
/*****************************/
103
$exec_shell_doc = gettext("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.");
104
$exec_shell_sig = array(
105
	array(
106
		$XML_RPC_Boolean, // First signature element is return value.
107
		$XML_RPC_String, // password
108
		$XML_RPC_String, // shell code to exec
109
	)
110
);
111

    
112
function exec_shell_xmlrpc($raw_params) {
113
	global $config, $xmlrpc_g;
114

    
115
	$params = xmlrpc_params_to_php($raw_params);
116
	if(!xmlrpc_auth($params))
117
		return $xmlrpc_g['return']['authfail'];
118
	$shell_cmd = $params[0];
119
	mwexec($shell_cmd);
120

    
121
	return $xmlrpc_g['return']['true'];
122
}
123

    
124
/*****************************/
125
$backup_config_section_doc = gettext("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.");
126
$backup_config_section_sig = array(
127
	array(
128
		$XML_RPC_Struct, // First signature element is return value.
129
		$XML_RPC_String,
130
		$XML_RPC_Array
131
	)
132
);
133

    
134
function backup_config_section_xmlrpc($raw_params) {
135
	global $config, $xmlrpc_g;
136

    
137
	$params = xmlrpc_params_to_php($raw_params);
138
	if(!xmlrpc_auth($params))
139
		return $xmlrpc_g['return']['authfail'];
140
	$val = array_intersect_key($config, array_flip($params[0]));
141

    
142
	return new XML_RPC_Response(XML_RPC_encode($val));
143
}
144

    
145
/*****************************/
146
$restore_config_section_doc = gettext("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.");
147
$restore_config_section_sig = array(
148
	array(
149
		$XML_RPC_Boolean,
150
		$XML_RPC_String,
151
		$XML_RPC_Struct
152
	)
153
);
154

    
155
function restore_config_section_xmlrpc($raw_params) {
156
	global $config, $xmlrpc_g;
157

    
158
	$params = xmlrpc_params_to_php($raw_params);
159
	if(!xmlrpc_auth($params))
160
		return $xmlrpc_g['return']['authfail'];
161
	$vipbackup = array();
162
	if (isset($params[0]['virtualip'])) {
163
		if(is_array($config['virtualip']['vip'])) {
164
			foreach ($config['virtualip']['vip'] as $vip)
165
				interface_vip_bring_down($vip);
166
		}
167
        	$vipbackup = $config['virtualip']['vip'];
168
	}
169
        // For vip section, first keep items sent from the master
170
	$config = array_merge($config, $params[0]);
171
        // Then add ipalias and proxyarp types already defined on the backup
172
	if (is_array($vipbackup)) {
173
		foreach ($vipbackup as $vip) {
174
			if (($vip['mode'] == 'ipalias') || ($vip['mode'] == 'proxyarp'))
175
				array_unshift($config['virtualip']['vip'], $vip);
176
		}
177
	}
178
	$mergedkeys = implode(",", array_keys($params[0]));
179
	write_config(sprintf(gettext("Merged in config (%s sections) from XMLRPC client."),$mergedkeys));
180
	interfaces_vips_configure();
181

    
182
	return $xmlrpc_g['return']['true'];
183
}
184

    
185
/*****************************/
186
$merge_config_section_doc = gettext("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.");
187
$merge_config_section_sig = array(
188
	array(
189
		$XML_RPC_Boolean,
190
		$XML_RPC_String,
191
		$XML_RPC_Struct
192
	)
193
);
194

    
195
function merge_installedpackages_section_xmlrpc($raw_params) {
196
	global $config, $xmlrpc_g;
197

    
198
	$params = xmlrpc_params_to_php($raw_params);
199
	if(!xmlrpc_auth($params))
200
		return $xmlrpc_g['return']['authfail'];
201
	$config['installedpackages'] = array_merge($config['installedpackages'], $params[0]);
202
	$mergedkeys = implode(",", array_keys($params[0]));
203
	write_config(sprintf(gettext("Merged in config (%s sections) from XMLRPC client."),$mergedkeys));
204

    
205
	return $xmlrpc_g['return']['true'];
206
}
207

    
208
/*****************************/
209
$merge_config_section_doc = gettext("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.");
210
$merge_config_section_sig = array(
211
	array(
212
		$XML_RPC_Boolean,
213
		$XML_RPC_String,
214
		$XML_RPC_Struct
215
	)
216
);
217

    
218
function merge_config_section_xmlrpc($raw_params) {
219
	global $config, $xmlrpc_g;
220

    
221
	$params = xmlrpc_params_to_php($raw_params);
222
	if(!xmlrpc_auth($params))
223
		return $xmlrpc_g['return']['authfail'];
224
	if (isset($params[0]['virtualip'])) {
225
                if(is_array($config['virtualip']['vip'])) {
226
                        foreach ($config['virtualip']['vip'] as $vip)
227
                                interface_vip_bring_down($vip);
228
                }
229
        }
230
	$config = array_merge_recursive_unique($config, $params[0]);
231
	$mergedkeys = implode(",", array_keys($params[0]));
232
	write_config("Merged in config ({$mergedkeys} sections) from XMLRPC client.");
233
	interfaces_vips_configure();
234

    
235
	return $xmlrpc_g['return']['true'];
236
}
237

    
238
/*****************************/
239
$filter_configure_doc = gettext("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.");
240
$filter_configure_sig = array(
241
	array(
242
		$XML_RPC_Boolean,
243
		$XML_RPC_String
244
	)
245
);
246

    
247
function filter_configure_xmlrpc($raw_params) {
248
	global $xmlrpc_g;
249

    
250
	$params = xmlrpc_params_to_php($raw_params);
251
	if(!xmlrpc_auth($params))
252
		return $xmlrpc_g['return']['authfail'];
253
	filter_configure();
254
	system_routing_configure();
255
	setup_gateways_monitor();
256
	relayd_configure();
257
	require_once("openvpn.inc");
258
	openvpn_resync_all();
259
	services_dhcpd_configure();
260
	services_dnsmasq_configure();
261
	local_sync_accounts();
262

    
263
	return $xmlrpc_g['return']['true'];
264
}
265

    
266
/*****************************/
267
$carp_configure_doc = gettext("Basic XMLRPC wrapper for configuring CARP interfaces.");
268
$carp_configure_sig = array(
269
	array(
270
		$XML_RPC_Boolean,
271
		$XML_RPC_String
272
	)
273
);
274

    
275
function interfaces_carp_configure_xmlrpc($raw_params) {
276
	global $xmlrpc_g;
277

    
278
	$params = xmlrpc_params_to_php($raw_params);
279
	if(!xmlrpc_auth($params))
280
		return $xmlrpc_g['return']['authfail'];
281
	interfaces_vips_configure();
282

    
283
	return $xmlrpc_g['return']['true'];
284
}
285

    
286
/*****************************/
287
$check_firmware_version_doc = gettext("Basic XMLRPC wrapper for check_firmware_version. This function will return the output of check_firmware_version upon completion.");
288

    
289
$check_firmware_version_sig = array(
290
	array(
291
		$XML_RPC_String,
292
		$XML_RPC_String
293
	)
294
);
295

    
296
function check_firmware_version_xmlrpc($raw_params) {
297
	global $xmlrpc_g, $XML_RPC_String;
298

    
299
	$params = xmlrpc_params_to_php($raw_params);
300
	if(!xmlrpc_auth($params))
301
		return $xmlrpc_g['return']['authfail'];
302

    
303
	return new XML_RPC_Response(new XML_RPC_Value(check_firmware_version(false), $XML_RPC_String));
304
}
305

    
306
/*****************************/
307
$pfsense_firmware_version_doc = gettext("Basic XMLRPC wrapper for check_firmware_version. This function will return the output of check_firmware_version upon completion.");
308

    
309
$pfsense_firmware_version_sig = array (
310
        array (
311
                $XML_RPC_Struct,
312
                $XML_RPC_String
313
        )
314
);
315

    
316
function pfsense_firmware_version_xmlrpc($raw_params) {
317
        global $xmlrpc_g;
318

    
319
        $params = xmlrpc_params_to_php($raw_params);
320
        if(!xmlrpc_auth($params))
321
                return $xmlrpc_g['return']['authfail'];
322

    
323
        return new XML_RPC_Response(XML_RPC_encode(host_firmware_version()));
324
}
325

    
326
/*****************************/
327
$reboot_doc = gettext("Basic XMLRPC wrapper for rc.reboot.");
328
$reboot_sig = array(array($XML_RPC_Boolean, $XML_RPC_String));
329
function reboot_xmlrpc($raw_params) {
330
	global $xmlrpc_g;
331

    
332
	$params = xmlrpc_params_to_php($raw_params);
333
	if(!xmlrpc_auth($params))
334
		return $xmlrpc_g['return']['authfail'];
335
	mwexec_bg("/etc/rc.reboot");
336

    
337
	return $xmlrpc_g['return']['true'];
338
}
339

    
340
/*****************************/
341
$get_notices_sig = array(
342
	array(
343
		$XML_RPC_Array,
344
		$XML_RPC_String
345
	),
346
	array(
347
		$XML_RPC_Array
348
	)
349
);
350

    
351
function get_notices_xmlrpc($raw_params) {
352
	global $g, $xmlrpc_g;
353

    
354
	$params = xmlrpc_params_to_php($raw_params);
355
	if(!xmlrpc_auth($params)) return $xmlrpc_g['return']['authfail'];
356
	require("notices.inc");
357
	if(!$params) {
358
		$toreturn = get_notices();
359
	} else {
360
		$toreturn = get_notices($params);
361
	}
362
	$response = new XML_RPC_Response(XML_RPC_encode($toreturn));
363

    
364
	return $response;
365
}
366

    
367
/*****************************/
368
$server = new XML_RPC_Server(
369
        array(
370
		'pfsense.exec_shell' => array('function' => 'exec_shell_xmlrpc',
371
			'signature' => $exec_shell_sig,
372
			'docstring' => $exec_shell_doc),
373
		'pfsense.exec_php' => array('function' => 'exec_php_xmlrpc',
374
			'signature' => $exec_php_sig,
375
			'docstring' => $exec_php_doc),	
376
		'pfsense.filter_configure' => array('function' => 'filter_configure_xmlrpc',
377
			'signature' => $filter_configure_sig,
378
			'docstring' => $filter_configure_doc),
379
		'pfsense.interfaces_carp_configure' => array('function' => 'interfaces_carp_configure_xmlrpc',
380
			'docstring' => $carp_configure_sig),
381
		'pfsense.backup_config_section' => array('function' => 'backup_config_section_xmlrpc',
382
			'signature' => $backup_config_section_sig,
383
			'docstring' => $backup_config_section_doc),
384
		'pfsense.restore_config_section' => array('function' => 'restore_config_section_xmlrpc',
385
			'signature' => $restore_config_section_sig,
386
			'docstring' => $restore_config_section_doc),
387
		'pfsense.merge_config_section' => array('function' => 'merge_config_section_xmlrpc',
388
			'signature' => $merge_config_section_sig,
389
			'docstring' => $merge_config_section_doc),
390
		'pfsense.merge_installedpackages_section_xmlrpc' => array('function' => 'merge_installedpackages_section_xmlrpc',
391
			'signature' => $merge_config_section_sig,
392
			'docstring' => $merge_config_section_doc),							
393
		'pfsense.check_firmware_version' => array('function' => 'check_firmware_version_xmlrpc',
394
			'signature' => $check_firmware_version_sig,
395
			'docstring' => $check_firmware_version_doc),
396
		'pfsense.host_firmware_version' => array('function' => 'pfsense_firmware_version_xmlrpc',
397
			'signature' => $pfsense_firmware_version_sig,
398
			'docstring' => $host_firmware_version_doc),
399
		'pfsense.reboot' => array('function' => 'reboot_xmlrpc',
400
			'signature' => $reboot_sig,
401
			'docstring' => $reboot_doc),
402
		'pfsense.get_notices' => array('function' => 'get_notices_xmlrpc',
403
			'signature' => $get_notices_sig)
404
        )
405
);
406

    
407
?>
(223-223/223)