Project

General

Profile

Download (15.1 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
$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
$exec_php_sig = array(
80
	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

    
87
function exec_php_xmlrpc($raw_params) {
88
	global $config, $xmlrpc_g;
89

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

    
115
	$params = xmlrpc_params_to_php($raw_params);
116
	if(!xmlrpc_auth($params))
117
		return $xmlrpc_g['return']['authfail'];
118
	$shell_cmd = $params[0];
119
	mwexec($shell_cmd);
120

    
121
	return $xmlrpc_g['return']['true'];
122
}
123

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

    
134
function backup_config_section_xmlrpc($raw_params) {
135
	global $config, $xmlrpc_g;
136

    
137
	$params = xmlrpc_params_to_php($raw_params);
138
	if(!xmlrpc_auth($params))
139
		return $xmlrpc_g['return']['authfail'];
140
	$val = array_intersect_key($config, array_flip($params[0]));
141

    
142
	return new XML_RPC_Response(XML_RPC_encode($val));
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

    
158
	$params = xmlrpc_params_to_php($raw_params);
159
	if(!xmlrpc_auth($params))
160
		return $xmlrpc_g['return']['authfail'];
161

    
162
	$vipbackup = array();
163
	$oldvips = array();
164
	if (isset($params[0]['virtualip'])) {
165
		if(is_array($config['virtualip']['vip'])) {
166
			foreach ($config['virtualip']['vip'] as $vipindex => $vip) {
167
				if ($vip['mode'] == "carp")
168
					$oldvips[$vip['vhid']] = "{$vip['password']}{$vip['advskew']}{$vip['subnet']}{$vip['subnet_bits']}{$vip['advbase']}";
169
				else if ((($vip['mode'] == 'ipalias') || ($vip['mode'] == 'proxyarp')) && substr($vip['interface'], 0, 3) != "vip")
170
					$vipbackup[] = $vip;
171
			}
172
		}
173
	}
174

    
175
        // For vip section, first keep items sent from the master
176
	$config = array_merge_recursive_unique($config, $params[0]);
177

    
178
        /* Then add ipalias and proxyarp types already defined on the backup */
179
	if (is_array($vipbackup) && !empty($vipbackup)) {
180
		if (!is_array($config['virtualip']))
181
			$config['virtualip'] = array();
182
		if (!is_array($config['virtualip']['vip']))
183
			$config['virtualip']['vip'] = array();
184
		foreach ($vipbackup as $vip)
185
			array_unshift($config['virtualip']['vip'], $vip);
186
	}
187

    
188
	/* Log what happened */
189
	$mergedkeys = implode(",", array_keys($params[0]));
190
	write_config(sprintf(gettext("Merged in config (%s sections) from XMLRPC client."),$mergedkeys));
191

    
192
	/* 
193
	 * The real work on handling the vips specially
194
	 * This is a copy of intefaces_vips_configure with addition of not reloading existing/not changed carps
195
	 */
196
	if (is_array($config['virtualip']) && is_array($config['virtualip']['vip'])) {
197
		$carp_setuped = false;
198
		$anyproxyarp = false;
199
		foreach ($config['virtualip']['vip'] as $vip) {
200
			if (isset($oldvips[$vip['vhid']])) {
201
				if ($oldvips[$vip['vhid']] == "{$vip['password']}{$vip['advskew']}{$vip['subnet']}{$vip['subnet_bits']}{$vip['advbase']}") {
202
				    if (does_interface_exist("vip{$vip['vhid']}"))
203
					continue; // Skip reconfiguring this vips since nothing has changed.
204
				} else
205
					unset($oldvips['vhid']);
206
			}
207

    
208
			switch ($vip['mode']) {
209
			case "proxyarp":
210
				$anyproxyarp = true;
211
				break;
212
			case "ipalias":
213
				interface_ipalias_configure(&$vip);
214
				break;
215
			case "carp":
216
				if ($carp_setuped == false)
217
                                        $carp_setuped = true;
218
				interface_carp_configure($vip);
219
				break;
220
			case "carpdev-dhcp":
221
				interface_carpdev_configure($vip);
222
				break;
223
			}
224
		}
225
		/* Cleanup remaining old carps */
226
		foreach ($oldvips as $oldvipif => $oldvippar) {
227
			if (does_interface_exist("vip{$oldvipif}"))
228
				pfSense_interface_destroy("vip{$oldvipif}");
229
		}
230
		if ($carp_setuped == true)
231
			interfaces_carp_setup();
232
		if ($anyproxyarp == true)
233
			interface_proxyarp_configure();
234
	}
235

    
236
	return $xmlrpc_g['return']['true'];
237
}
238

    
239
/*****************************/
240
$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.");
241
$merge_config_section_sig = array(
242
	array(
243
		$XML_RPC_Boolean,
244
		$XML_RPC_String,
245
		$XML_RPC_Struct
246
	)
247
);
248

    
249
function merge_installedpackages_section_xmlrpc($raw_params) {
250
	global $config, $xmlrpc_g;
251

    
252
	$params = xmlrpc_params_to_php($raw_params);
253
	if(!xmlrpc_auth($params))
254
		return $xmlrpc_g['return']['authfail'];
255
	$config['installedpackages'] = array_merge($config['installedpackages'], $params[0]);
256
	$mergedkeys = implode(",", array_keys($params[0]));
257
	write_config(sprintf(gettext("Merged in config (%s sections) from XMLRPC client."),$mergedkeys));
258

    
259
	return $xmlrpc_g['return']['true'];
260
}
261

    
262
/*****************************/
263
$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.");
264
$merge_config_section_sig = array(
265
	array(
266
		$XML_RPC_Boolean,
267
		$XML_RPC_String,
268
		$XML_RPC_Struct
269
	)
270
);
271

    
272
function merge_config_section_xmlrpc($raw_params) {
273
	global $config, $xmlrpc_g;
274
	$params = xmlrpc_params_to_php($raw_params);
275
	if(!xmlrpc_auth($params))
276
		return $xmlrpc_g['return']['authfail'];
277
	$config_new = array_merge($config, $params[0]);
278
	$config = $config_new;
279
	$mergedkeys = implode(",", array_keys($params[0]));
280
	$fd = fopen("/tmp/array.txt", "w");
281
	fwrite($fd, print_r($params[0], true));
282
	fclose($fd);
283
	write_config(sprintf(gettext("Merged in config (%s sections) from XMLRPC client."), $mergedkeys));
284
	return $xmlrpc_g['return']['true'];
285
}
286

    
287
/*****************************/
288
$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.");
289
$filter_configure_sig = array(
290
	array(
291
		$XML_RPC_Boolean,
292
		$XML_RPC_String
293
	)
294
);
295

    
296
function filter_configure_xmlrpc($raw_params) {
297
	global $xmlrpc_g;
298

    
299
	$params = xmlrpc_params_to_php($raw_params);
300
	if(!xmlrpc_auth($params))
301
		return $xmlrpc_g['return']['authfail'];
302
	filter_configure();
303
	system_routing_configure();
304
	setup_gateways_monitor();
305
	relayd_configure();
306
	require_once("openvpn.inc");
307
	openvpn_resync_all();
308
	services_dhcpd_configure();
309
	services_dnsmasq_configure();
310
	local_sync_accounts();
311

    
312
	return $xmlrpc_g['return']['true'];
313
}
314

    
315
/*****************************/
316
$carp_configure_doc = gettext("Basic XMLRPC wrapper for configuring CARP interfaces.");
317
$carp_configure_sig = array(
318
	array(
319
		$XML_RPC_Boolean,
320
		$XML_RPC_String
321
	)
322
);
323

    
324
function interfaces_carp_configure_xmlrpc($raw_params) {
325
	global $xmlrpc_g;
326

    
327
	$params = xmlrpc_params_to_php($raw_params);
328
	if(!xmlrpc_auth($params))
329
		return $xmlrpc_g['return']['authfail'];
330
	interfaces_vips_configure();
331

    
332
	return $xmlrpc_g['return']['true'];
333
}
334

    
335
/*****************************/
336
$check_firmware_version_doc = gettext("Basic XMLRPC wrapper for check_firmware_version. This function will return the output of check_firmware_version upon completion.");
337

    
338
$check_firmware_version_sig = array(
339
	array(
340
		$XML_RPC_String,
341
		$XML_RPC_String
342
	)
343
);
344

    
345
function check_firmware_version_xmlrpc($raw_params) {
346
	global $xmlrpc_g, $XML_RPC_String;
347

    
348
	$params = xmlrpc_params_to_php($raw_params);
349
	if(!xmlrpc_auth($params))
350
		return $xmlrpc_g['return']['authfail'];
351

    
352
	return new XML_RPC_Response(new XML_RPC_Value(check_firmware_version(false), $XML_RPC_String));
353
}
354

    
355
/*****************************/
356
$pfsense_firmware_version_doc = gettext("Basic XMLRPC wrapper for check_firmware_version. This function will return the output of check_firmware_version upon completion.");
357

    
358
$pfsense_firmware_version_sig = array (
359
        array (
360
                $XML_RPC_Struct,
361
                $XML_RPC_String
362
        )
363
);
364

    
365
function pfsense_firmware_version_xmlrpc($raw_params) {
366
        global $xmlrpc_g;
367

    
368
        $params = xmlrpc_params_to_php($raw_params);
369
        if(!xmlrpc_auth($params))
370
                return $xmlrpc_g['return']['authfail'];
371

    
372
        return new XML_RPC_Response(XML_RPC_encode(host_firmware_version()));
373
}
374

    
375
/*****************************/
376
$reboot_doc = gettext("Basic XMLRPC wrapper for rc.reboot.");
377
$reboot_sig = array(array($XML_RPC_Boolean, $XML_RPC_String));
378
function reboot_xmlrpc($raw_params) {
379
	global $xmlrpc_g;
380

    
381
	$params = xmlrpc_params_to_php($raw_params);
382
	if(!xmlrpc_auth($params))
383
		return $xmlrpc_g['return']['authfail'];
384
	mwexec_bg("/etc/rc.reboot");
385

    
386
	return $xmlrpc_g['return']['true'];
387
}
388

    
389
/*****************************/
390
$get_notices_sig = array(
391
	array(
392
		$XML_RPC_Array,
393
		$XML_RPC_String
394
	),
395
	array(
396
		$XML_RPC_Array
397
	)
398
);
399

    
400
function get_notices_xmlrpc($raw_params) {
401
	global $g, $xmlrpc_g;
402

    
403
	$params = xmlrpc_params_to_php($raw_params);
404
	if(!xmlrpc_auth($params)) return $xmlrpc_g['return']['authfail'];
405
	require("notices.inc");
406
	if(!$params) {
407
		$toreturn = get_notices();
408
	} else {
409
		$toreturn = get_notices($params);
410
	}
411
	$response = new XML_RPC_Response(XML_RPC_encode($toreturn));
412

    
413
	return $response;
414
}
415

    
416
$xmlrpclockkey = lock('xmlrpc', LOCK_EX);
417

    
418
/*****************************/
419
$server = new XML_RPC_Server(
420
        array(
421
		'pfsense.exec_shell' => array('function' => 'exec_shell_xmlrpc',
422
			'signature' => $exec_shell_sig,
423
			'docstring' => $exec_shell_doc),
424
		'pfsense.exec_php' => array('function' => 'exec_php_xmlrpc',
425
			'signature' => $exec_php_sig,
426
			'docstring' => $exec_php_doc),	
427
		'pfsense.filter_configure' => array('function' => 'filter_configure_xmlrpc',
428
			'signature' => $filter_configure_sig,
429
			'docstring' => $filter_configure_doc),
430
		'pfsense.interfaces_carp_configure' => array('function' => 'interfaces_carp_configure_xmlrpc',
431
			'docstring' => $carp_configure_sig),
432
		'pfsense.backup_config_section' => array('function' => 'backup_config_section_xmlrpc',
433
			'signature' => $backup_config_section_sig,
434
			'docstring' => $backup_config_section_doc),
435
		'pfsense.restore_config_section' => array('function' => 'restore_config_section_xmlrpc',
436
			'signature' => $restore_config_section_sig,
437
			'docstring' => $restore_config_section_doc),
438
		'pfsense.merge_config_section' => array('function' => 'merge_config_section_xmlrpc',
439
			'signature' => $merge_config_section_sig,
440
			'docstring' => $merge_config_section_doc),
441
		'pfsense.merge_installedpackages_section_xmlrpc' => array('function' => 'merge_installedpackages_section_xmlrpc',
442
			'signature' => $merge_config_section_sig,
443
			'docstring' => $merge_config_section_doc),							
444
		'pfsense.check_firmware_version' => array('function' => 'check_firmware_version_xmlrpc',
445
			'signature' => $check_firmware_version_sig,
446
			'docstring' => $check_firmware_version_doc),
447
		'pfsense.host_firmware_version' => array('function' => 'pfsense_firmware_version_xmlrpc',
448
			'signature' => $pfsense_firmware_version_sig,
449
			'docstring' => $host_firmware_version_doc),
450
		'pfsense.reboot' => array('function' => 'reboot_xmlrpc',
451
			'signature' => $reboot_sig,
452
			'docstring' => $reboot_doc),
453
		'pfsense.get_notices' => array('function' => 'get_notices_xmlrpc',
454
			'signature' => $get_notices_sig)
455
        )
456
);
457

    
458
unlock($xmlrpclockkey);
459

    
460
?>
(225-225/225)