Project

General

Profile

Download (13.8 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')) && substr($vip['interface'], 0, 3) != "vip")
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
		$vipbackup = $config['virtualip']['vip'];
230
        }
231
	$config = array_merge_recursive_unique($config, $params[0]);
232
        // Then add ipalias and proxyarp types already defined on the backup
233
	if (is_array($vipbackup)) {
234
		foreach ($vipbackup as $vip) {
235
			if ((($vip['mode'] == 'ipalias') || ($vip['mode'] == 'proxyarp')) && substr($vip['interface'], 0, 3) != "vip")
236
				array_unshift($config['virtualip']['vip'], $vip);
237
		}
238
	}
239
	$mergedkeys = implode(",", array_keys($params[0]));
240
	write_config("Merged in config ({$mergedkeys} sections) from XMLRPC client.");
241
	interfaces_vips_configure();
242

    
243
	return $xmlrpc_g['return']['true'];
244
}
245

    
246
/*****************************/
247
$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.");
248
$filter_configure_sig = array(
249
	array(
250
		$XML_RPC_Boolean,
251
		$XML_RPC_String
252
	)
253
);
254

    
255
function filter_configure_xmlrpc($raw_params) {
256
	global $xmlrpc_g;
257

    
258
	$params = xmlrpc_params_to_php($raw_params);
259
	if(!xmlrpc_auth($params))
260
		return $xmlrpc_g['return']['authfail'];
261
	filter_configure();
262
	system_routing_configure();
263
	setup_gateways_monitor();
264
	relayd_configure();
265
	require_once("openvpn.inc");
266
	openvpn_resync_all();
267
	services_dhcpd_configure();
268
	services_dnsmasq_configure();
269
	local_sync_accounts();
270

    
271
	return $xmlrpc_g['return']['true'];
272
}
273

    
274
/*****************************/
275
$carp_configure_doc = gettext("Basic XMLRPC wrapper for configuring CARP interfaces.");
276
$carp_configure_sig = array(
277
	array(
278
		$XML_RPC_Boolean,
279
		$XML_RPC_String
280
	)
281
);
282

    
283
function interfaces_carp_configure_xmlrpc($raw_params) {
284
	global $xmlrpc_g;
285

    
286
	$params = xmlrpc_params_to_php($raw_params);
287
	if(!xmlrpc_auth($params))
288
		return $xmlrpc_g['return']['authfail'];
289
	interfaces_vips_configure();
290

    
291
	return $xmlrpc_g['return']['true'];
292
}
293

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

    
297
$check_firmware_version_sig = array(
298
	array(
299
		$XML_RPC_String,
300
		$XML_RPC_String
301
	)
302
);
303

    
304
function check_firmware_version_xmlrpc($raw_params) {
305
	global $xmlrpc_g, $XML_RPC_String;
306

    
307
	$params = xmlrpc_params_to_php($raw_params);
308
	if(!xmlrpc_auth($params))
309
		return $xmlrpc_g['return']['authfail'];
310

    
311
	return new XML_RPC_Response(new XML_RPC_Value(check_firmware_version(false), $XML_RPC_String));
312
}
313

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

    
317
$pfsense_firmware_version_sig = array (
318
        array (
319
                $XML_RPC_Struct,
320
                $XML_RPC_String
321
        )
322
);
323

    
324
function pfsense_firmware_version_xmlrpc($raw_params) {
325
        global $xmlrpc_g;
326

    
327
        $params = xmlrpc_params_to_php($raw_params);
328
        if(!xmlrpc_auth($params))
329
                return $xmlrpc_g['return']['authfail'];
330

    
331
        return new XML_RPC_Response(XML_RPC_encode(host_firmware_version()));
332
}
333

    
334
/*****************************/
335
$reboot_doc = gettext("Basic XMLRPC wrapper for rc.reboot.");
336
$reboot_sig = array(array($XML_RPC_Boolean, $XML_RPC_String));
337
function reboot_xmlrpc($raw_params) {
338
	global $xmlrpc_g;
339

    
340
	$params = xmlrpc_params_to_php($raw_params);
341
	if(!xmlrpc_auth($params))
342
		return $xmlrpc_g['return']['authfail'];
343
	mwexec_bg("/etc/rc.reboot");
344

    
345
	return $xmlrpc_g['return']['true'];
346
}
347

    
348
/*****************************/
349
$get_notices_sig = array(
350
	array(
351
		$XML_RPC_Array,
352
		$XML_RPC_String
353
	),
354
	array(
355
		$XML_RPC_Array
356
	)
357
);
358

    
359
function get_notices_xmlrpc($raw_params) {
360
	global $g, $xmlrpc_g;
361

    
362
	$params = xmlrpc_params_to_php($raw_params);
363
	if(!xmlrpc_auth($params)) return $xmlrpc_g['return']['authfail'];
364
	require("notices.inc");
365
	if(!$params) {
366
		$toreturn = get_notices();
367
	} else {
368
		$toreturn = get_notices($params);
369
	}
370
	$response = new XML_RPC_Response(XML_RPC_encode($toreturn));
371

    
372
	return $response;
373
}
374

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

    
415
?>
(224-224/224)