Project

General

Profile

Download (11.6 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']) {
42
	if ($config['installedpackages']['carpsettings']['config']) {
43
		foreach ($config['installedpackages']['carpsettings']['config'] as $carp) {
44
			$synchronizetoip = $carp['synchronizetoip'];
45
		}
46
	}
47
}
48

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

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

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

    
70
/* EXPOSED FUNCTIONS */
71

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

    
81

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

    
91

    
92

    
93
/*****************************/
94

    
95

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

    
105

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

    
115

    
116

    
117
/*****************************/
118

    
119

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

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

    
137
/*****************************/
138

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

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

    
158

    
159
/*****************************/
160

    
161

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

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

    
181

    
182
/*****************************/
183

    
184

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

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

    
204
/*****************************/
205

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

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

    
225
/*****************************/
226

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

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

    
242
/*****************************/
243

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

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

    
255
/*****************************/
256

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

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

    
281
/*****************************/
282

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

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

    
295
/*****************************/
296

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