Project

General

Profile

Download (13.8 KB) Statistics
| Branch: | Tag: | Revision:
1 50d49018 Colin Smith
<?php
2
/*
3 81d8a9ad Colin Smith
	$Id$
4 dc1cd85d Scott Ullrich
5 50d49018 Colin Smith
        xmlrpc.php
6 d1a6552b Scott Ullrich
        Copyright (C) 2009, 2010 Scott Ullrich
7 50d49018 Colin Smith
        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 0e19e0e4 Scott Ullrich
        POSSIBILITY OF SUCH DAMAGE.
30 50d49018 Colin Smith
*/
31
32 6b07c15a Matthew Grooms
##|+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 0674f163 sullrich
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 50d49018 Colin Smith
47 ff664954 Scott Ullrich
/* grab sync to ip if enabled */
48 dd98e330 Scott Ullrich
if($config['installedpackages']['carpsettings']) {
49
	if ($config['installedpackages']['carpsettings']['config']) {
50
		foreach ($config['installedpackages']['carpsettings']['config'] as $carp) {
51
			$synchronizetoip = $carp['synchronizetoip'];
52
		}
53 ff664954 Scott Ullrich
	}
54
}
55
56
if($synchronizetoip) {
57
	if($synchronizetoip == $_SERVER['REMOTE_ADDR']) {
58 de63649b Rafael Lucas
		log_error(gettext("Disallowing CARP sync loop."));
59 ff664954 Scott Ullrich
		die;	
60
	}
61
}
62
63 8da3de34 Colin Smith
$xmlrpc_g = array(
64 137f46d8 Ermal
	"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 8da3de34 Colin Smith
71
/*
72
 *   pfSense XMLRPC errors
73
 *   $XML_RPC_erruser + 1 = Auth failure
74
 */
75
$XML_RPC_erruser = 200;
76
77
/* EXPOSED FUNCTIONS */
78 de63649b Rafael Lucas
$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 c3638879 Scott Ullrich
$exec_php_sig = array(
80 137f46d8 Ermal
	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 c3638879 Scott Ullrich
87
function exec_php_xmlrpc($raw_params) {
88
	global $config, $xmlrpc_g;
89 137f46d8 Ermal
90 c3638879 Scott Ullrich
	$params = xmlrpc_params_to_php($raw_params);
91 137f46d8 Ermal
	if(!xmlrpc_auth($params))
92
		return $xmlrpc_g['return']['authfail'];
93 c3638879 Scott Ullrich
	$exec_php = $params[0];
94
	eval($exec_php);
95 2c0dd581 Scott Ullrich
	if($toreturn) {
96
		$response = XML_RPC_encode($toreturn);
97
		return new XML_RPC_Response($response);
98
	} else
99
		return $xmlrpc_g['return']['true'];
100 c3638879 Scott Ullrich
}
101
102
/*****************************/
103 de63649b Rafael Lucas
$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 c3638879 Scott Ullrich
$exec_shell_sig = array(
105 137f46d8 Ermal
	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 c3638879 Scott Ullrich
112
function exec_shell_xmlrpc($raw_params) {
113
	global $config, $xmlrpc_g;
114 137f46d8 Ermal
115 c3638879 Scott Ullrich
	$params = xmlrpc_params_to_php($raw_params);
116 137f46d8 Ermal
	if(!xmlrpc_auth($params))
117
		return $xmlrpc_g['return']['authfail'];
118 c3638879 Scott Ullrich
	$shell_cmd = $params[0];
119
	mwexec($shell_cmd);
120 137f46d8 Ermal
121 c3638879 Scott Ullrich
	return $xmlrpc_g['return']['true'];
122
}
123
124
/*****************************/
125 de63649b Rafael Lucas
$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 8da3de34 Colin Smith
$backup_config_section_sig = array(
127 137f46d8 Ermal
	array(
128
		$XML_RPC_Struct, // First signature element is return value.
129
		$XML_RPC_String,
130
		$XML_RPC_Array
131
	)
132
);
133 21dc3a7d Colin Smith
134 57e6d4c9 Colin Smith
function backup_config_section_xmlrpc($raw_params) {
135 03d89831 Scott Ullrich
	global $config, $xmlrpc_g;
136 137f46d8 Ermal
137 8da3de34 Colin Smith
	$params = xmlrpc_params_to_php($raw_params);
138 137f46d8 Ermal
	if(!xmlrpc_auth($params))
139
		return $xmlrpc_g['return']['authfail'];
140 03d89831 Scott Ullrich
	$val = array_intersect_key($config, array_flip($params[0]));
141 137f46d8 Ermal
142 8da3de34 Colin Smith
	return new XML_RPC_Response(XML_RPC_encode($val));
143 50d49018 Colin Smith
}
144
145 8da3de34 Colin Smith
/*****************************/
146 de63649b Rafael Lucas
$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 8da3de34 Colin Smith
$restore_config_section_sig = array(
148 137f46d8 Ermal
	array(
149
		$XML_RPC_Boolean,
150
		$XML_RPC_String,
151
		$XML_RPC_Struct
152
	)
153
);
154 21dc3a7d Colin Smith
155 57e6d4c9 Colin Smith
function restore_config_section_xmlrpc($raw_params) {
156 03d89831 Scott Ullrich
	global $config, $xmlrpc_g;
157 137f46d8 Ermal
158 57e6d4c9 Colin Smith
	$params = xmlrpc_params_to_php($raw_params);
159 19b5c3e7 Ermal
	if(!xmlrpc_auth($params))
160
		return $xmlrpc_g['return']['authfail'];
161 a8200dbf Ermal
	$vipbackup = array();
162 4d775dd0 Ermal
	if (isset($params[0]['virtualip'])) {
163 19b5c3e7 Ermal
		if(is_array($config['virtualip']['vip'])) {
164
			foreach ($config['virtualip']['vip'] as $vip)
165
				interface_vip_bring_down($vip);
166
		}
167 a8200dbf Ermal
        	$vipbackup = $config['virtualip']['vip'];
168 19b5c3e7 Ermal
	}
169 b019222a pierrepomes
        // For vip section, first keep items sent from the master
170 03d89831 Scott Ullrich
	$config = array_merge($config, $params[0]);
171 b019222a pierrepomes
        // Then add ipalias and proxyarp types already defined on the backup
172 4d775dd0 Ermal
	if (is_array($vipbackup)) {
173
		foreach ($vipbackup as $vip) {
174 4e8c89fd Ermal
			if ((($vip['mode'] == 'ipalias') || ($vip['mode'] == 'proxyarp')) && substr($vip['interface'], 0, 3) != "vip")
175 00752d5a Pierre POMES
				array_unshift($config['virtualip']['vip'], $vip);
176 4d775dd0 Ermal
		}
177 a8200dbf Ermal
	}
178 03d89831 Scott Ullrich
	$mergedkeys = implode(",", array_keys($params[0]));
179 3a3fb8ea Erik Fonnesbeck
	write_config(sprintf(gettext("Merged in config (%s sections) from XMLRPC client."),$mergedkeys));
180 2de2abe0 Ermal
	interfaces_vips_configure();
181 137f46d8 Ermal
182 8da3de34 Colin Smith
	return $xmlrpc_g['return']['true'];
183 50d49018 Colin Smith
}
184
185 82ae5cfc Scott Ullrich
/*****************************/
186 de63649b Rafael Lucas
$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 82ae5cfc Scott Ullrich
$merge_config_section_sig = array(
188 137f46d8 Ermal
	array(
189
		$XML_RPC_Boolean,
190
		$XML_RPC_String,
191
		$XML_RPC_Struct
192
	)
193
);
194 82ae5cfc Scott Ullrich
195
function merge_installedpackages_section_xmlrpc($raw_params) {
196
	global $config, $xmlrpc_g;
197 137f46d8 Ermal
198 82ae5cfc Scott Ullrich
	$params = xmlrpc_params_to_php($raw_params);
199 137f46d8 Ermal
	if(!xmlrpc_auth($params))
200
		return $xmlrpc_g['return']['authfail'];
201 82ae5cfc Scott Ullrich
	$config['installedpackages'] = array_merge($config['installedpackages'], $params[0]);
202
	$mergedkeys = implode(",", array_keys($params[0]));
203 3a3fb8ea Erik Fonnesbeck
	write_config(sprintf(gettext("Merged in config (%s sections) from XMLRPC client."),$mergedkeys));
204 137f46d8 Ermal
205 82ae5cfc Scott Ullrich
	return $xmlrpc_g['return']['true'];
206
}
207
208 8da3de34 Colin Smith
/*****************************/
209 de63649b Rafael Lucas
$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 dc1cd85d Scott Ullrich
$merge_config_section_sig = array(
211 137f46d8 Ermal
	array(
212
		$XML_RPC_Boolean,
213
		$XML_RPC_String,
214
		$XML_RPC_Struct
215
	)
216
);
217 dc1cd85d Scott Ullrich
218
function merge_config_section_xmlrpc($raw_params) {
219
	global $config, $xmlrpc_g;
220 137f46d8 Ermal
221 dc1cd85d Scott Ullrich
	$params = xmlrpc_params_to_php($raw_params);
222 19b5c3e7 Ermal
	if(!xmlrpc_auth($params))
223
		return $xmlrpc_g['return']['authfail'];
224 4d775dd0 Ermal
	if (isset($params[0]['virtualip'])) {
225 19b5c3e7 Ermal
                if(is_array($config['virtualip']['vip'])) {
226
                        foreach ($config['virtualip']['vip'] as $vip)
227
                                interface_vip_bring_down($vip);
228
                }
229 4e8c89fd Ermal
		$vipbackup = $config['virtualip']['vip'];
230 19b5c3e7 Ermal
        }
231 a771c044 Scott Ullrich
	$config = array_merge_recursive_unique($config, $params[0]);
232 4e8c89fd Ermal
        // 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 dc1cd85d Scott Ullrich
	$mergedkeys = implode(",", array_keys($params[0]));
240
	write_config("Merged in config ({$mergedkeys} sections) from XMLRPC client.");
241 2de2abe0 Ermal
	interfaces_vips_configure();
242 137f46d8 Ermal
243 dc1cd85d Scott Ullrich
	return $xmlrpc_g['return']['true'];
244
}
245
246
/*****************************/
247 de63649b Rafael Lucas
$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 8da3de34 Colin Smith
$filter_configure_sig = array(
249 137f46d8 Ermal
	array(
250
		$XML_RPC_Boolean,
251
		$XML_RPC_String
252
	)
253
);
254 07a7b08f Colin Smith
255
function filter_configure_xmlrpc($raw_params) {
256 8da3de34 Colin Smith
	global $xmlrpc_g;
257 137f46d8 Ermal
258 07a7b08f Colin Smith
	$params = xmlrpc_params_to_php($raw_params);
259 137f46d8 Ermal
	if(!xmlrpc_auth($params))
260
		return $xmlrpc_g['return']['authfail'];
261 07a7b08f Colin Smith
	filter_configure();
262 e700437b Scott Ullrich
	system_routing_configure();
263 41e6d4bd Seth Mos
	setup_gateways_monitor();
264 651c32e2 Bill Marquette
	relayd_configure();
265 c3fef0c9 jim-p
	require_once("openvpn.inc");
266
	openvpn_resync_all();
267
	services_dhcpd_configure();
268 62c4d0fb jim-p
	services_dnsmasq_configure();
269 0ce72662 jim-p
	local_sync_accounts();
270 137f46d8 Ermal
271 8da3de34 Colin Smith
	return $xmlrpc_g['return']['true'];
272 07a7b08f Colin Smith
}
273
274 8da3de34 Colin Smith
/*****************************/
275 de63649b Rafael Lucas
$carp_configure_doc = gettext("Basic XMLRPC wrapper for configuring CARP interfaces.");
276 efe7562e Scott Ullrich
$carp_configure_sig = array(
277 137f46d8 Ermal
	array(
278
		$XML_RPC_Boolean,
279
		$XML_RPC_String
280
	)
281
);
282 efe7562e Scott Ullrich
283
function interfaces_carp_configure_xmlrpc($raw_params) {
284
	global $xmlrpc_g;
285 137f46d8 Ermal
286 efe7562e Scott Ullrich
	$params = xmlrpc_params_to_php($raw_params);
287 137f46d8 Ermal
	if(!xmlrpc_auth($params))
288
		return $xmlrpc_g['return']['authfail'];
289 efe7562e Scott Ullrich
	interfaces_vips_configure();
290 137f46d8 Ermal
291 efe7562e Scott Ullrich
	return $xmlrpc_g['return']['true'];
292
}
293
294
/*****************************/
295 de63649b Rafael Lucas
$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 137f46d8 Ermal
297 8da3de34 Colin Smith
$check_firmware_version_sig = array(
298 137f46d8 Ermal
	array(
299
		$XML_RPC_String,
300
		$XML_RPC_String
301
	)
302
);
303 44488c3c Colin Smith
304
function check_firmware_version_xmlrpc($raw_params) {
305 b4d19b46 Bill Marquette
	global $xmlrpc_g, $XML_RPC_String;
306 137f46d8 Ermal
307 b4d19b46 Bill Marquette
	$params = xmlrpc_params_to_php($raw_params);
308 137f46d8 Ermal
	if(!xmlrpc_auth($params))
309
		return $xmlrpc_g['return']['authfail'];
310
311 8da3de34 Colin Smith
	return new XML_RPC_Response(new XML_RPC_Value(check_firmware_version(false), $XML_RPC_String));
312 44488c3c Colin Smith
}
313
314 e501de37 Ermal
/*****************************/
315 0567899d Ermal
$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 e501de37 Ermal
324
function pfsense_firmware_version_xmlrpc($raw_params) {
325 0567899d Ermal
        global $xmlrpc_g;
326 e501de37 Ermal
327
        $params = xmlrpc_params_to_php($raw_params);
328
        if(!xmlrpc_auth($params))
329
                return $xmlrpc_g['return']['authfail'];
330
331 0567899d Ermal
        return new XML_RPC_Response(XML_RPC_encode(host_firmware_version()));
332 e501de37 Ermal
}
333
334 8da3de34 Colin Smith
/*****************************/
335 de63649b Rafael Lucas
$reboot_doc = gettext("Basic XMLRPC wrapper for rc.reboot.");
336 1cdca133 Colin Smith
$reboot_sig = array(array($XML_RPC_Boolean, $XML_RPC_String));
337 bd0fe65b Colin Smith
function reboot_xmlrpc($raw_params) {
338 8da3de34 Colin Smith
	global $xmlrpc_g;
339 137f46d8 Ermal
340 bd0fe65b Colin Smith
	$params = xmlrpc_params_to_php($raw_params);
341 137f46d8 Ermal
	if(!xmlrpc_auth($params))
342
		return $xmlrpc_g['return']['authfail'];
343 53cf5533 Colin Smith
	mwexec_bg("/etc/rc.reboot");
344 137f46d8 Ermal
345 8da3de34 Colin Smith
	return $xmlrpc_g['return']['true'];
346 bd0fe65b Colin Smith
}
347
348 8da3de34 Colin Smith
/*****************************/
349
$get_notices_sig = array(
350 137f46d8 Ermal
	array(
351
		$XML_RPC_Array,
352
		$XML_RPC_String
353
	),
354
	array(
355
		$XML_RPC_Array
356
	)
357
);
358 8da3de34 Colin Smith
359 d9064267 Colin Smith
function get_notices_xmlrpc($raw_params) {
360 8da3de34 Colin Smith
	global $g, $xmlrpc_g;
361 137f46d8 Ermal
362 b4d19b46 Bill Marquette
	$params = xmlrpc_params_to_php($raw_params);
363
	if(!xmlrpc_auth($params)) return $xmlrpc_g['return']['authfail'];
364 0674f163 sullrich
	require("notices.inc");
365 8da3de34 Colin Smith
	if(!$params) {
366 d9064267 Colin Smith
		$toreturn = get_notices();
367
	} else {
368 8da3de34 Colin Smith
		$toreturn = get_notices($params);
369 d9064267 Colin Smith
	}
370
	$response = new XML_RPC_Response(XML_RPC_encode($toreturn));
371 137f46d8 Ermal
372 d9064267 Colin Smith
	return $response;
373
}
374
375 8da3de34 Colin Smith
/*****************************/
376 21dc3a7d Colin Smith
$server = new XML_RPC_Server(
377
        array(
378 137f46d8 Ermal
		'pfsense.exec_shell' => array('function' => 'exec_shell_xmlrpc',
379 0567899d Ermal
			'signature' => $exec_shell_sig,
380
			'docstring' => $exec_shell_doc),
381 137f46d8 Ermal
		'pfsense.exec_php' => array('function' => 'exec_php_xmlrpc',
382 0567899d Ermal
			'signature' => $exec_php_sig,
383
			'docstring' => $exec_php_doc),	
384 137f46d8 Ermal
		'pfsense.filter_configure' => array('function' => 'filter_configure_xmlrpc',
385 0567899d Ermal
			'signature' => $filter_configure_sig,
386
			'docstring' => $filter_configure_doc),
387 137f46d8 Ermal
		'pfsense.interfaces_carp_configure' => array('function' => 'interfaces_carp_configure_xmlrpc',
388 0567899d Ermal
			'docstring' => $carp_configure_sig),
389 137f46d8 Ermal
		'pfsense.backup_config_section' => array('function' => 'backup_config_section_xmlrpc',
390 0567899d Ermal
			'signature' => $backup_config_section_sig,
391
			'docstring' => $backup_config_section_doc),
392 137f46d8 Ermal
		'pfsense.restore_config_section' => array('function' => 'restore_config_section_xmlrpc',
393 0567899d Ermal
			'signature' => $restore_config_section_sig,
394
			'docstring' => $restore_config_section_doc),
395 137f46d8 Ermal
		'pfsense.merge_config_section' => array('function' => 'merge_config_section_xmlrpc',
396 0567899d Ermal
			'signature' => $merge_config_section_sig,
397
			'docstring' => $merge_config_section_doc),
398 137f46d8 Ermal
		'pfsense.merge_installedpackages_section_xmlrpc' => array('function' => 'merge_installedpackages_section_xmlrpc',
399 0567899d Ermal
			'signature' => $merge_config_section_sig,
400
			'docstring' => $merge_config_section_doc),							
401 137f46d8 Ermal
		'pfsense.check_firmware_version' => array('function' => 'check_firmware_version_xmlrpc',
402 0567899d Ermal
			'signature' => $check_firmware_version_sig,
403
			'docstring' => $check_firmware_version_doc),
404 e501de37 Ermal
		'pfsense.host_firmware_version' => array('function' => 'pfsense_firmware_version_xmlrpc',
405 0567899d Ermal
			'signature' => $pfsense_firmware_version_sig,
406
			'docstring' => $host_firmware_version_doc),
407 137f46d8 Ermal
		'pfsense.reboot' => array('function' => 'reboot_xmlrpc',
408 0567899d Ermal
			'signature' => $reboot_sig,
409
			'docstring' => $reboot_doc),
410 137f46d8 Ermal
		'pfsense.get_notices' => array('function' => 'get_notices_xmlrpc',
411 0567899d Ermal
			'signature' => $get_notices_sig)
412 50d49018 Colin Smith
        )
413
);
414 b298dd06 Scott Ullrich
415 de63649b Rafael Lucas
?>