Project

General

Profile

Download (12.6 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
        foreach ($vipbackup as $vip) {
173
                if (($vip['mode'] == 'ipalias') || ($vip['mode'] == 'proxyarp'))
174
                        $config['virtualip']['vip'][]=$vip ;
175
	}
176
	$mergedkeys = implode(",", array_keys($params[0]));
177
	write_config(sprintf(gettext("Merged in config (%s sections) from XMLRPC client."),$mergedkeys));
178
	interfaces_vips_configure();
179

    
180
	return $xmlrpc_g['return']['true'];
181
}
182

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

    
193
function merge_installedpackages_section_xmlrpc($raw_params) {
194
	global $config, $xmlrpc_g;
195

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

    
203
	return $xmlrpc_g['return']['true'];
204
}
205

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

    
216
function merge_config_section_xmlrpc($raw_params) {
217
	global $config, $xmlrpc_g;
218

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

    
233
	return $xmlrpc_g['return']['true'];
234
}
235

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

    
245
function filter_configure_xmlrpc($raw_params) {
246
	global $xmlrpc_g;
247

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

    
261
	return $xmlrpc_g['return']['true'];
262
}
263

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

    
273
function interfaces_carp_configure_xmlrpc($raw_params) {
274
	global $xmlrpc_g;
275

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

    
281
	return $xmlrpc_g['return']['true'];
282
}
283

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

    
287
$check_firmware_version_sig = array(
288
	array(
289
		$XML_RPC_String,
290
		$XML_RPC_String
291
	)
292
);
293

    
294
function check_firmware_version_xmlrpc($raw_params) {
295
	global $xmlrpc_g, $XML_RPC_String;
296

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

    
301
	return new XML_RPC_Response(new XML_RPC_Value(check_firmware_version(false), $XML_RPC_String));
302
}
303

    
304
/*****************************/
305
$reboot_doc = gettext("Basic XMLRPC wrapper for rc.reboot.");
306
$reboot_sig = array(array($XML_RPC_Boolean, $XML_RPC_String));
307
function reboot_xmlrpc($raw_params) {
308
	global $xmlrpc_g;
309

    
310
	$params = xmlrpc_params_to_php($raw_params);
311
	if(!xmlrpc_auth($params))
312
		return $xmlrpc_g['return']['authfail'];
313
	mwexec_bg("/etc/rc.reboot");
314

    
315
	return $xmlrpc_g['return']['true'];
316
}
317

    
318
/*****************************/
319
$get_notices_sig = array(
320
	array(
321
		$XML_RPC_Array,
322
		$XML_RPC_String
323
	),
324
	array(
325
		$XML_RPC_Array
326
	)
327
);
328

    
329
function get_notices_xmlrpc($raw_params) {
330
	global $g, $xmlrpc_g;
331

    
332
	$params = xmlrpc_params_to_php($raw_params);
333
	if(!xmlrpc_auth($params)) return $xmlrpc_g['return']['authfail'];
334
	require("notices.inc");
335
	if(!$params) {
336
		$toreturn = get_notices();
337
	} else {
338
		$toreturn = get_notices($params);
339
	}
340
	$response = new XML_RPC_Response(XML_RPC_encode($toreturn));
341

    
342
	return $response;
343
}
344

    
345
/*****************************/
346
$server = new XML_RPC_Server(
347
        array(
348
		'pfsense.exec_shell' => array('function' => 'exec_shell_xmlrpc',
349
		'signature' => $exec_shell_sig,
350
		'docstring' => $exec_shell_doc),
351
		'pfsense.exec_php' => array('function' => 'exec_php_xmlrpc',
352
		'signature' => $exec_php_sig,
353
		'docstring' => $exec_php_doc),	
354
		'pfsense.filter_configure' => array('function' => 'filter_configure_xmlrpc',
355
		'signature' => $filter_configure_sig,
356
		'docstring' => $filter_configure_doc),
357
		'pfsense.interfaces_carp_configure' => array('function' => 'interfaces_carp_configure_xmlrpc',
358
		'docstring' => $carp_configure_sig),
359
		'pfsense.backup_config_section' => array('function' => 'backup_config_section_xmlrpc',
360
		'signature' => $backup_config_section_sig,
361
		'docstring' => $backup_config_section_doc),
362
		'pfsense.restore_config_section' => array('function' => 'restore_config_section_xmlrpc',
363
		'signature' => $restore_config_section_sig,
364
		'docstring' => $restore_config_section_doc),
365
		'pfsense.merge_config_section' => array('function' => 'merge_config_section_xmlrpc',
366
		'signature' => $merge_config_section_sig,
367
		'docstring' => $merge_config_section_doc),
368
		'pfsense.merge_installedpackages_section_xmlrpc' => array('function' => 'merge_installedpackages_section_xmlrpc',
369
		'signature' => $merge_config_section_sig,
370
		'docstring' => $merge_config_section_doc),							
371
		'pfsense.check_firmware_version' => array('function' => 'check_firmware_version_xmlrpc',
372
		'signature' => $check_firmware_version_sig,
373
		'docstring' => $check_firmware_version_doc),
374
		'pfsense.reboot' => array('function' => 'reboot_xmlrpc',
375
		'signature' => $reboot_sig,
376
		'docstring' => $reboot_doc),
377
		'pfsense.get_notices' => array('function' => 'get_notices_xmlrpc',
378
		'signature' => $get_notices_sig)
379
        )
380
);
381

    
382
?>
(222-222/222)