Project

General

Profile

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

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

    
134
/*****************************/
135

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

    
145
function restore_config_section_xmlrpc($raw_params) {
146
	global $config, $xmlrpc_g;
147
	$params = xmlrpc_params_to_php($raw_params);
148
	if(!xmlrpc_auth($params)) return $xmlrpc_g['return']['authfail'];
149
	
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
$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.';
183
$merge_config_section_sig = array(
184
					array(
185
						$XML_RPC_Boolean,
186
						$XML_RPC_String,
187
						$XML_RPC_Struct
188
					)
189
				);
190

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

    
201
/*****************************/
202

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

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

    
222
/*****************************/
223

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

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

    
239
/*****************************/
240

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

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

    
252
/*****************************/
253

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

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

    
278
/*****************************/
279

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

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

    
292
/*****************************/
293

    
294
$server = new XML_RPC_Server(
295
        array(
296
            'pfsense.exec_shell' 		=> array('function' => 'exec_shell_xmlrpc',
297
							'signature' => $exec_shell_sig,
298
							'docstring' => $exec_shell_doc),
299
            'pfsense.exec_php'	 		=> array('function' => 'exec_php_xmlrpc',
300
							'signature' => $exec_php_sig,
301
							'docstring' => $exec_php_doc),	
302
            'pfsense.interfaces_carp_configure' => array('function' => 'interfaces_carp_configure_xmlrpc',
303
							'signature' => $carp_configure_doc,
304
							'docstring' => $carp_configure_sig),
305
            'pfsense.backup_config_section' => 	array('function' => 'backup_config_section_xmlrpc',
306
							'signature' => $backup_config_section_sig,
307
							'docstring' => $backup_config_section_doc),
308
	    'pfsense.restore_config_section' => array('function' => 'restore_config_section_xmlrpc',
309
							'signature' => $restore_config_section_sig,
310
							'docstring' => $restore_config_section_doc),
311
	    'pfsense.merge_config_section' => array('function' => 'merge_config_section_xmlrpc',
312
							'signature' => $merge_config_section_sig,
313
							'docstring' => $merge_config_section_doc),
314
	    'pfsense.merge_installedpackages_section_xmlrpc' => array('function' => 'merge_installedpackages_section_xmlrpc',
315
							'signature' => $merge_config_section_sig,
316
							'docstring' => $merge_config_section_doc),							
317

    
318
	    'pfsense.merge_installedpackages_section_xmlrpc' => array('function' => 'merge_installedpackages_section_xmlrpc',
319
							'signature' => $merge_config_section_sig,
320
							'docstring' => $merge_config_section_doc),
321
							
322
	    'pfsense.filter_configure' => 	array('function' => 'filter_configure_xmlrpc',
323
							'signature' => $filter_configure_sig,
324
							'docstring' => $filter_configure_doc),
325
	    'pfsense.check_firmware_version' =>	array('function' => 'check_firmware_version_xmlrpc',
326
							'signature' => $check_firmware_version_sig,
327
							'docstring' => $check_firmware_version_doc),
328
	    'pfsense.reboot' =>			array('function' => 'reboot_xmlrpc',
329
							'signature' => $reboot_sig,
330
							'docstring' => $reboot_doc),
331
	    'pfsense.get_notices' =>		array('function' => 'get_notices_xmlrpc',
332
							'signature' => $get_notices_sig)
333
        )
334
);
335
?>
(175-175/175)