Project

General

Profile

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

    
5
        xmlrpc.php
6
        Copyright (C) 2005 Colin Smith
7
        All rights reserved.
8

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

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

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

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

    
30
	TODO:
31
		* Expose more functions.
32
*/
33

    
34
require_once("xmlrpc_server.inc");
35
require_once("xmlrpc.inc");
36
require_once("config.inc");
37
require_once("functions.inc");
38
require_once("array_intersect_key.inc");
39

    
40
/* grab sync to ip if enabled */
41
if ($config['installedpackages']['carpsettings']['config']) {
42
	foreach ($config['installedpackages']['carpsettings']['config'] as $carp) {
43
		$synchronizetoip = $carp['synchronizetoip'];
44
	}
45
}
46

    
47
if($synchronizetoip) {
48
	if($synchronizetoip == $_SERVER['REMOTE_ADDR']) {
49
		log_error("Disallowing CARP sync loop.");
50
		die;	
51
	}
52
}
53

    
54
$xmlrpc_g = array(
55
			"return" => array(
56
						"true" => new XML_RPC_Response(new XML_RPC_Value(true, $XML_RPC_Boolean)),
57
						"false" => new XML_RPC_Response(new XML_RPC_Value(false, $XML_RPC_Boolean)),
58
						"authfail" => new XML_RPC_Response(0, $XML_RPC_erruser+1, "Authentication failure")
59
				)
60
		);
61

    
62
/*
63
 *   pfSense XMLRPC errors
64
 *   $XML_RPC_erruser + 1 = Auth failure
65
 */
66
$XML_RPC_erruser = 200;
67

    
68
/* EXPOSED FUNCTIONS */
69

    
70
$exec_php_doc = '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.';
71
$exec_php_sig = array(
72
				array(
73
					$XML_RPC_Boolean, // First signature element is return value.
74
					$XML_RPC_String, // password
75
					$XML_RPC_String, // shell code to exec
76
				)
77
			);
78

    
79

    
80
function exec_php_xmlrpc($raw_params) {
81
	global $config, $xmlrpc_g;
82
	$params = xmlrpc_params_to_php($raw_params);
83
	if(!xmlrpc_auth($params)) return $xmlrpc_g['return']['authfail'];
84
	$exec_php = $params[0];
85
	eval($exec_php);
86
	return $xmlrpc_g['return']['true'];
87
}
88

    
89

    
90

    
91
/*****************************/
92

    
93

    
94
$exec_shell_doc = '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.';
95
$exec_shell_sig = array(
96
				array(
97
					$XML_RPC_Boolean, // First signature element is return value.
98
					$XML_RPC_String, // password
99
					$XML_RPC_String, // shell code to exec
100
				)
101
			);
102

    
103

    
104
function exec_shell_xmlrpc($raw_params) {
105
	global $config, $xmlrpc_g;
106
	$params = xmlrpc_params_to_php($raw_params);
107
	if(!xmlrpc_auth($params)) return $xmlrpc_g['return']['authfail'];
108
	$shell_cmd = $params[0];
109
	mwexec($shell_cmd);
110
	return $xmlrpc_g['return']['true'];
111
}
112

    
113

    
114

    
115
/*****************************/
116

    
117

    
118
$backup_config_section_doc = '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.';
119
$backup_config_section_sig = array(
120
				array(
121
					$XML_RPC_Struct, // First signature element is return value.
122
					$XML_RPC_String,
123
					$XML_RPC_Array
124
				)
125
			);
126

    
127
function backup_config_section_xmlrpc($raw_params) {
128
	global $config, $xmlrpc_g;
129
	$params = xmlrpc_params_to_php($raw_params);
130
	if(!xmlrpc_auth($params)) return $xmlrpc_g['return']['authfail'];
131
	$val = array_intersect_key($config, array_flip($params[0]));
132
	return new XML_RPC_Response(XML_RPC_encode($val));
133
}
134

    
135
/*****************************/
136

    
137
$restore_config_section_doc = '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.';
138
$restore_config_section_sig = array(
139
					array(
140
						$XML_RPC_Boolean,
141
						$XML_RPC_String,
142
						$XML_RPC_Struct
143
					)
144
				);
145

    
146
function restore_config_section_xmlrpc($raw_params) {
147
	global $config, $xmlrpc_g;
148
	$params = xmlrpc_params_to_php($raw_params);
149
	if(!xmlrpc_auth($params)) return $xmlrpc_g['return']['authfail'];
150
	$config = array_merge($config, $params[0]);
151
	$mergedkeys = implode(",", array_keys($params[0]));
152
	write_config("Merged in config ({$mergedkeys} sections) from XMLRPC client.");
153
	return $xmlrpc_g['return']['true'];
154
}
155

    
156

    
157
/*****************************/
158

    
159

    
160
$merge_config_section_doc = '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.';
161
$merge_config_section_sig = array(
162
					array(
163
						$XML_RPC_Boolean,
164
						$XML_RPC_String,
165
						$XML_RPC_Struct
166
					)
167
				);
168

    
169
function merge_installedpackages_section_xmlrpc($raw_params) {
170
	global $config, $xmlrpc_g;
171
	$params = xmlrpc_params_to_php($raw_params);
172
	if(!xmlrpc_auth($params)) return $xmlrpc_g['return']['authfail'];
173
	$config['installedpackages'] = array_merge($config['installedpackages'], $params[0]);
174
	$mergedkeys = implode(",", array_keys($params[0]));
175
	write_config("Merged in config ({$mergedkeys} sections) from XMLRPC client.");
176
	return $xmlrpc_g['return']['true'];
177
}
178

    
179

    
180
/*****************************/
181

    
182

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

    
192
function merge_config_section_xmlrpc($raw_params) {
193
	global $config, $xmlrpc_g;
194
	$params = xmlrpc_params_to_php($raw_params);
195
	if(!xmlrpc_auth($params)) return $xmlrpc_g['return']['authfail'];
196
	$config = array_merge_recursive_unique($config, $params[0]);
197
	$mergedkeys = implode(",", array_keys($params[0]));
198
	write_config("Merged in config ({$mergedkeys} sections) from XMLRPC client.");
199
	return $xmlrpc_g['return']['true'];
200
}
201

    
202
/*****************************/
203

    
204
$filter_configure_doc = '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.';
205
$filter_configure_sig = array(
206
				array(
207
					$XML_RPC_Boolean,
208
					$XML_RPC_String
209
				)
210
			);
211

    
212
function filter_configure_xmlrpc($raw_params) {
213
	global $xmlrpc_g;
214
	$params = xmlrpc_params_to_php($raw_params);
215
	if(!xmlrpc_auth($params)) return $xmlrpc_g['return']['authfail'];
216
	require_once("vslb.inc");
217
	slbd_configure();
218
	filter_configure();
219
	system_routing_configure();
220
	return $xmlrpc_g['return']['true'];
221
}
222

    
223
/*****************************/
224

    
225
$check_firmware_version_doc = 'Basic XMLRPC wrapper for check_firmware_version. This function will return the output of check_firmware_version upon completion.';
226
$check_firmware_version_sig = array(
227
					array(
228
						$XML_RPC_String,
229
						$XML_RPC_String
230
					)
231
				);
232

    
233
function check_firmware_version_xmlrpc($raw_params) {
234
	global $xmlrpc_g, $XML_RPC_String;
235
	$params = xmlrpc_params_to_php($raw_params);
236
	if(!xmlrpc_auth($params)) return $xmlrpc_g['return']['authfail'];
237
	return new XML_RPC_Response(new XML_RPC_Value(check_firmware_version(false), $XML_RPC_String));
238
}
239

    
240
/*****************************/
241

    
242
$reboot_doc = 'Basic XMLRPC wrapper for rc.reboot.';
243
$reboot_sig = array(array($XML_RPC_Boolean, $XML_RPC_String));
244

    
245
function reboot_xmlrpc($raw_params) {
246
	global $xmlrpc_g;
247
	$params = xmlrpc_params_to_php($raw_params);
248
	if(!xmlrpc_auth($params)) return $xmlrpc_g['return']['authfail'];
249
	mwexec_bg("/etc/rc.reboot");
250
	return $xmlrpc_g['return']['true'];
251
}
252

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

    
255
$get_notices_sig = array(
256
				array(
257
					$XML_RPC_Array,
258
					$XML_RPC_String
259
				),
260
				array(
261
					$XML_RPC_Array
262
				)
263
			);
264

    
265
function get_notices_xmlrpc($raw_params) {
266
	global $g, $xmlrpc_g;
267
	$params = xmlrpc_params_to_php($raw_params);
268
	if(!xmlrpc_auth($params)) return $xmlrpc_g['return']['authfail'];
269
	require_once("notices.inc");
270
	if(!$params) {
271
		$toreturn = get_notices();
272
	} else {
273
		$toreturn = get_notices($params);
274
	}
275
	$response = new XML_RPC_Response(XML_RPC_encode($toreturn));
276
	return $response;
277
}
278

    
279
/*****************************/
280

    
281
$carp_configure_doc = 'Basic XMLRPC wrapper for configuring CARP interfaces.';
282
$carp_configure_sig = array(array($XML_RPC_Boolean, $XML_RPC_String));
283

    
284
function interfaces_carp_configure_xmlrpc($raw_params) {
285
	global $xmlrpc_g;
286
	$params = xmlrpc_params_to_php($raw_params);
287
	if(!xmlrpc_auth($params)) return $xmlrpc_g['return']['authfail'];
288
	interfaces_carp_configure();
289
	interfaces_carp_bring_up_final();
290
	return $xmlrpc_g['return']['true'];
291
}
292

    
293
/*****************************/
294

    
295
$server = new XML_RPC_Server(
296
        array(
297
            'pfsense.exec_shell' 		=> array('function' => 'exec_shell_xmlrpc',
298
							'signature' => $exec_shell_sig,
299
							'docstring' => $exec_shell_doc),
300
            'pfsense.exec_php'	 		=> array('function' => 'exec_php_xmlrpc',
301
							'signature' => $exec_php_sig,
302
							'docstring' => $exec_php_doc),	
303
            'pfsense.interfaces_carp_configure' => array('function' => 'interfaces_carp_configure_xmlrpc',
304
							'signature' => $carp_configure_doc,
305
							'docstring' => $carp_configure_sig),
306
            'pfsense.backup_config_section' => 	array('function' => 'backup_config_section_xmlrpc',
307
							'signature' => $backup_config_section_sig,
308
							'docstring' => $backup_config_section_doc),
309
	    'pfsense.restore_config_section' => array('function' => 'restore_config_section_xmlrpc',
310
							'signature' => $restore_config_section_sig,
311
							'docstring' => $restore_config_section_doc),
312
	    'pfsense.merge_config_section' => array('function' => 'merge_config_section_xmlrpc',
313
							'signature' => $merge_config_section_sig,
314
							'docstring' => $merge_config_section_doc),
315
	    'pfsense.merge_installedpackages_section_xmlrpc' => array('function' => 'merge_installedpackages_section_xmlrpc',
316
							'signature' => $merge_config_section_sig,
317
							'docstring' => $merge_config_section_doc),							
318
	    'pfsense.filter_configure' => 	array('function' => 'filter_configure_xmlrpc',
319
							'signature' => $filter_configure_sig,
320
							'docstring' => $filter_configure_doc),
321
	    'pfsense.check_firmware_version' =>	array('function' => 'check_firmware_version_xmlrpc',
322
							'signature' => $check_firmware_version_sig,
323
							'docstring' => $check_firmware_version_doc),
324
	    'pfsense.reboot' =>			array('function' => 'reboot_xmlrpc',
325
							'signature' => $reboot_sig,
326
							'docstring' => $reboot_doc),
327
	    'pfsense.get_notices' =>		array('function' => 'get_notices_xmlrpc',
328
							'signature' => $get_notices_sig)
329
        )
330
);
331
?>
(193-193/193)