Project

General

Profile

Download (12.7 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

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

    
88
function exec_php_xmlrpc($raw_params) {
89
	global $config, $xmlrpc_g;
90
	$params = xmlrpc_params_to_php($raw_params);
91
	if(!xmlrpc_auth($params)) return $xmlrpc_g['return']['authfail'];
92
	$exec_php = $params[0];
93
	eval($exec_php);
94
	if($toreturn) {
95
		$response = XML_RPC_encode($toreturn);
96
		return new XML_RPC_Response($response);
97
	} else
98
		return $xmlrpc_g['return']['true'];
99
}
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

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

    
122

    
123

    
124
/*****************************/
125

    
126

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

    
136
function backup_config_section_xmlrpc($raw_params) {
137
	global $config, $xmlrpc_g;
138
	$params = xmlrpc_params_to_php($raw_params);
139
	if(!xmlrpc_auth($params)) return $xmlrpc_g['return']['authfail'];
140
	$val = array_intersect_key($config, array_flip($params[0]));
141
	return new XML_RPC_Response(XML_RPC_encode($val));
142
}
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
	$params = xmlrpc_params_to_php($raw_params);
158
	if(!xmlrpc_auth($params))
159
		return $xmlrpc_g['return']['authfail'];
160
	if (isset($params[0]['virtualip'])) {
161
		if(is_array($config['virtualip']['vip'])) {
162
			foreach ($config['virtualip']['vip'] as $vip)
163
				interface_vip_bring_down($vip);
164
		}
165
	}
166
	$config = array_merge($config, $params[0]);
167
	$mergedkeys = implode(",", array_keys($params[0]));
168
	write_config(sprintf(gettext("Merged in config (%s sections) from XMLRPC client."),$mergedkeys));
169
	interfaces_vips_configure();
170
	return $xmlrpc_g['return']['true'];
171
}
172

    
173

    
174
/*****************************/
175

    
176

    
177
$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.");
178
$merge_config_section_sig = array(
179
								array(
180
									$XML_RPC_Boolean,
181
									$XML_RPC_String,
182
									$XML_RPC_Struct
183
								)
184
							);
185

    
186
function merge_installedpackages_section_xmlrpc($raw_params) {
187
	global $config, $xmlrpc_g;
188
	$params = xmlrpc_params_to_php($raw_params);
189
	if(!xmlrpc_auth($params)) return $xmlrpc_g['return']['authfail'];
190
	$config['installedpackages'] = array_merge($config['installedpackages'], $params[0]);
191
	$mergedkeys = implode(",", array_keys($params[0]));
192
	write_config(sprintf(gettext("Merged in config (%s sections) from XMLRPC client."),$mergedkeys));
193
	return $xmlrpc_g['return']['true'];
194
}
195

    
196

    
197
/*****************************/
198

    
199

    
200
$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.");
201
$merge_config_section_sig = array(
202
								array(
203
									$XML_RPC_Boolean,
204
									$XML_RPC_String,
205
									$XML_RPC_Struct
206
								)
207
							);
208

    
209
function merge_config_section_xmlrpc($raw_params) {
210
	global $config, $xmlrpc_g;
211
	$params = xmlrpc_params_to_php($raw_params);
212
	if(!xmlrpc_auth($params))
213
		return $xmlrpc_g['return']['authfail'];
214
	if (isset($params[0]['virtualip'])) {
215
                if(is_array($config['virtualip']['vip'])) {
216
                        foreach ($config['virtualip']['vip'] as $vip)
217
                                interface_vip_bring_down($vip);
218
                }
219
        }
220
	$config = array_merge_recursive_unique($config, $params[0]);
221
	$mergedkeys = implode(",", array_keys($params[0]));
222
	write_config("Merged in config ({$mergedkeys} sections) from XMLRPC client.");
223
	interfaces_vips_configure();
224
	return $xmlrpc_g['return']['true'];
225
}
226

    
227
/*****************************/
228

    
229
$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.");
230
$filter_configure_sig = array(
231
							array(
232
								$XML_RPC_Boolean,
233
								$XML_RPC_String
234
							)
235
						);
236

    
237
function filter_configure_xmlrpc($raw_params) {
238
	global $xmlrpc_g;
239
	$params = xmlrpc_params_to_php($raw_params);
240
	if(!xmlrpc_auth($params)) return $xmlrpc_g['return']['authfail'];
241
	filter_configure();
242
	system_routing_configure();
243
	setup_gateways_monitor();
244
	relayd_configure();
245
	require_once("openvpn.inc");
246
	openvpn_resync_all();
247
	services_dhcpd_configure();
248
	services_dnsmasq_configure();
249
	local_sync_accounts();
250
	return $xmlrpc_g['return']['true'];
251
}
252

    
253
/*****************************/
254

    
255
$carp_configure_doc = gettext("Basic XMLRPC wrapper for configuring CARP interfaces.");
256
$carp_configure_sig = array(
257
							array(
258
								$XML_RPC_Boolean,
259
								$XML_RPC_String
260
							)
261
						);
262

    
263
function interfaces_carp_configure_xmlrpc($raw_params) {
264
	global $xmlrpc_g;
265
	$params = xmlrpc_params_to_php($raw_params);
266
	if(!xmlrpc_auth($params)) return $xmlrpc_g['return']['authfail'];
267
	interfaces_vips_configure();
268
	return $xmlrpc_g['return']['true'];
269
}
270

    
271
/*****************************/
272

    
273
$check_firmware_version_doc = gettext("Basic XMLRPC wrapper for check_firmware_version. This function will return the output of check_firmware_version upon completion.");
274
$check_firmware_version_sig = array(
275
								array(
276
									$XML_RPC_String,
277
									$XML_RPC_String
278
								)
279
							);
280

    
281
function check_firmware_version_xmlrpc($raw_params) {
282
	global $xmlrpc_g, $XML_RPC_String;
283
	$params = xmlrpc_params_to_php($raw_params);
284
	if(!xmlrpc_auth($params)) return $xmlrpc_g['return']['authfail'];
285
	return new XML_RPC_Response(new XML_RPC_Value(check_firmware_version(false), $XML_RPC_String));
286
}
287

    
288
/*****************************/
289

    
290
$reboot_doc = gettext("Basic XMLRPC wrapper for rc.reboot.");
291
$reboot_sig = array(array($XML_RPC_Boolean, $XML_RPC_String));
292

    
293
function reboot_xmlrpc($raw_params) {
294
	global $xmlrpc_g;
295
	$params = xmlrpc_params_to_php($raw_params);
296
	if(!xmlrpc_auth($params)) return $xmlrpc_g['return']['authfail'];
297
	mwexec_bg("/etc/rc.reboot");
298
	return $xmlrpc_g['return']['true'];
299
}
300

    
301
/*****************************/
302

    
303
$get_notices_sig = array(
304
					array(
305
						$XML_RPC_Array,
306
						$XML_RPC_String
307
					),
308
					array(
309
						$XML_RPC_Array
310
					)
311
				);
312

    
313
function get_notices_xmlrpc($raw_params) {
314
	global $g, $xmlrpc_g;
315
	$params = xmlrpc_params_to_php($raw_params);
316
	if(!xmlrpc_auth($params)) return $xmlrpc_g['return']['authfail'];
317
	require("notices.inc");
318
	if(!$params) {
319
		$toreturn = get_notices();
320
	} else {
321
		$toreturn = get_notices($params);
322
	}
323
	$response = new XML_RPC_Response(XML_RPC_encode($toreturn));
324
	return $response;
325
}
326

    
327
/*****************************/
328

    
329
$server = new XML_RPC_Server(
330
        array(
331
            'pfsense.exec_shell' 		=> array('function' => 'exec_shell_xmlrpc',
332
							'signature' => $exec_shell_sig,
333
							'docstring' => $exec_shell_doc),
334
            		 'pfsense.exec_php'	=> array('function' => 'exec_php_xmlrpc',
335
							'signature' => $exec_php_sig,
336
							'docstring' => $exec_php_doc),	
337
			 'pfsense.filter_configure' => 	array('function' => 'filter_configure_xmlrpc',
338
							'signature' => $filter_configure_sig,
339
							'docstring' => $filter_configure_doc),
340
            'pfsense.interfaces_carp_configure' => array('function' => 'interfaces_carp_configure_xmlrpc',
341
							'docstring' => $carp_configure_sig),
342
            'pfsense.backup_config_section' => 	array('function' => 'backup_config_section_xmlrpc',
343
							'signature' => $backup_config_section_sig,
344
							'docstring' => $backup_config_section_doc),
345
			'pfsense.restore_config_section' => array('function' => 'restore_config_section_xmlrpc',
346
							'signature' => $restore_config_section_sig,
347
							'docstring' => $restore_config_section_doc),
348
			'pfsense.merge_config_section' => array('function' => 'merge_config_section_xmlrpc',
349
							'signature' => $merge_config_section_sig,
350
							'docstring' => $merge_config_section_doc),
351
			'pfsense.merge_installedpackages_section_xmlrpc' => array('function' => 'merge_installedpackages_section_xmlrpc',
352
							'signature' => $merge_config_section_sig,
353
							'docstring' => $merge_config_section_doc),							
354
			'pfsense.check_firmware_version' =>	array('function' => 'check_firmware_version_xmlrpc',
355
							'signature' => $check_firmware_version_sig,
356
							'docstring' => $check_firmware_version_doc),
357
			'pfsense.reboot' =>			array('function' => 'reboot_xmlrpc',
358
							'signature' => $reboot_sig,
359
							'docstring' => $reboot_doc),
360
			'pfsense.get_notices' =>		array('function' => 'get_notices_xmlrpc',
361
							'signature' => $get_notices_sig)
362
        )
363
);
364

    
365
?>
(220-220/220)