Project

General

Profile

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

    
5
        xmlrpc.php
6
        Copyright (C) 2013-2014 Electric Sheep Fencing, LP
7
        Copyright (C) 2009, 2010 Scott Ullrich
8
        Copyright (C) 2005 Colin Smith
9
        All rights reserved.
10

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

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

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

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

    
33
##|+PRIV
34
##|*IDENT=page-xmlrpclibrary
35
##|*NAME=XMLRPC Library page
36
##|*DESCR=Allow access to the 'XMLRPC Library' page.
37
##|*MATCH=xmlrpc.php*
38
##|-PRIV
39

    
40
require("config.inc");
41
require("functions.inc");
42
require_once("filter.inc");
43
require("ipsec.inc");
44
require("vpn.inc");
45
require("shaper.inc");
46
require("xmlrpc_server.inc");
47
require("xmlrpc.inc");
48
require("array_intersect_key.inc");
49

    
50
function xmlrpc_loop_detect() {
51
	global $config;
52

    
53
	/* grab sync to ip if enabled */
54
	if ($config['hasync'])
55
		$synchronizetoip = $config['hasync']['synchronizetoip'];
56
	if($synchronizetoip) {
57
		if($synchronizetoip == $_SERVER['REMOTE_ADDR'])
58
			return true;	
59
	}
60

    
61
	return false;
62
}
63

    
64
$xmlrpc_g = array(
65
	"return" => array(
66
		"true" => new XML_RPC_Response(new XML_RPC_Value(true, $XML_RPC_Boolean)),
67
		"false" => new XML_RPC_Response(new XML_RPC_Value(false, $XML_RPC_Boolean)),
68
		"authfail" => new XML_RPC_Response(new XML_RPC_Value(gettext("Authentication failed"), $XML_RPC_String))
69
	)
70
);
71

    
72
/*
73
 *   pfSense XMLRPC errors
74
 *   $XML_RPC_erruser + 1 = Auth failure
75
 */
76
$XML_RPC_erruser = 200;
77

    
78
/* EXPOSED FUNCTIONS */
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 xmlrpc_authfail() {
89
	log_auth("webConfigurator authentication error for 'admin' from {$_SERVER['REMOTE_ADDR']}");
90
}
91

    
92
function exec_php_xmlrpc($raw_params) {
93
	global $config, $xmlrpc_g;
94

    
95
	$params = xmlrpc_params_to_php($raw_params);
96
	if(!xmlrpc_auth($params)) {
97
		xmlrpc_authfail();
98
		return $xmlrpc_g['return']['authfail'];
99
	}
100
	$exec_php = $params[0];
101
	eval($exec_php);
102
	if($toreturn) {
103
		$response = XML_RPC_encode($toreturn);
104
		return new XML_RPC_Response($response);
105
	} else
106
		return $xmlrpc_g['return']['true'];
107
}
108

    
109
/*****************************/
110
$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.");
111
$exec_shell_sig = array(
112
	array(
113
		$XML_RPC_Boolean, // First signature element is return value.
114
		$XML_RPC_String, // password
115
		$XML_RPC_String, // shell code to exec
116
	)
117
);
118

    
119
function exec_shell_xmlrpc($raw_params) {
120
	global $config, $xmlrpc_g;
121

    
122
	$params = xmlrpc_params_to_php($raw_params);
123
	if(!xmlrpc_auth($params)) {
124
		xmlrpc_authfail();
125
		return $xmlrpc_g['return']['authfail'];
126
	}
127
	$shell_cmd = $params[0];
128
	mwexec($shell_cmd);
129

    
130
	return $xmlrpc_g['return']['true'];
131
}
132

    
133
/*****************************/
134
$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.");
135
$backup_config_section_sig = array(
136
	array(
137
		$XML_RPC_Struct, // First signature element is return value.
138
		$XML_RPC_String,
139
		$XML_RPC_Array
140
	)
141
);
142

    
143
function backup_config_section_xmlrpc($raw_params) {
144
	global $config, $xmlrpc_g;
145

    
146
	if (xmlrpc_loop_detect()) {
147
		log_error("Disallowing CARP sync loop");
148
		return;
149
	}
150

    
151
	$params = xmlrpc_params_to_php($raw_params);
152
	if(!xmlrpc_auth($params)) {
153
		xmlrpc_authfail();
154
		return $xmlrpc_g['return']['authfail'];
155
	}
156
	$val = array_intersect_key($config, array_flip($params[0]));
157

    
158
	return new XML_RPC_Response(XML_RPC_encode($val));
159
}
160

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

    
171
function restore_config_section_xmlrpc($raw_params) {
172
	global $config, $xmlrpc_g;
173

    
174
	$old_config = $config;
175

    
176
	if (xmlrpc_loop_detect()) {
177
		log_error("Disallowing CARP sync loop");
178
		return;
179
	}
180

    
181
	$params = xmlrpc_params_to_php($raw_params);
182
	if(!xmlrpc_auth($params)) {
183
		xmlrpc_authfail();
184
		return $xmlrpc_g['return']['authfail'];
185
	}
186

    
187
	// Some sections should just be copied and not merged or we end
188
	//   up unable to sync the deletion of the last item in a section
189
	$sync_full = array('ipsec', 'aliases', 'wol', 'load_balancer', 'openvpn', 'cert', 'ca', 'crl', 'schedules', 'filter', 'nat', 'dhcpd', 'dhcpv6');
190
	$sync_full_done = array();
191
	foreach ($sync_full as $syncfull) {
192
		if (isset($params[0][$syncfull])) {
193
			$config[$syncfull] = $params[0][$syncfull];
194
			unset($params[0][$syncfull]);
195
			$sync_full_done[] = $syncfull;
196
		}
197
	}
198

    
199
	$vipbackup = array();
200
	$oldvips = array();
201
	if (isset($params[0]['virtualip'])) {
202
		if (is_array($config['virtualip']['vip'])) {
203
			foreach ($config['virtualip']['vip'] as $vipindex => $vip) {
204
				if ($vip['mode'] == "carp")
205
					$oldvips["{$vip['interface']}_vip{$vip['vhid']}"] = "{$vip['password']}{$vip['advskew']}{$vip['subnet']}{$vip['subnet_bits']}{$vip['advbase']}";
206
				else if ($vip['mode'] == "ipalias" && (strstr($vip['interface'], "_vip") || strstr($vip['interface'], "lo0")))
207
					$oldvips[$vip['subnet']] = "{$vip['interface']}{$vip['subnet']}{$vip['subnet_bits']}";
208
				else if (($vip['mode'] == "ipalias" || $vip['mode'] == 'proxyarp') && !(strstr($vip['interface'], "_vip") || strstr($vip['interface'], "lo0")))
209
					$vipbackup[] = $vip;
210
			}
211
		}
212
	}
213

    
214
	/*
215
	 * Make sure it doesn't end up with both dnsmasq and unbound enabled
216
	 * simultaneously in secondary
217
	 * */
218
	if (isset($params[0]['unbound']['enable']) && isset($config['dnsmasq']['enable'])) {
219
		unset($config['dnsmasq']['enable']);
220
		services_dnsmasq_configure();
221
	} else if (isset($params[0]['dnsmasq']['enable']) && isset($config['unbound']['enable'])) {
222
		unset($config['unbound']['enable']);
223
		services_unbound_configure();
224
	}
225

    
226
        // For vip section, first keep items sent from the master
227
	$config = array_merge_recursive_unique($config, $params[0]);
228

    
229
        /* Then add ipalias and proxyarp types already defined on the backup */
230
	if (is_array($vipbackup) && !empty($vipbackup)) {
231
		if (!is_array($config['virtualip']))
232
			$config['virtualip'] = array();
233
		if (!is_array($config['virtualip']['vip']))
234
			$config['virtualip']['vip'] = array();
235
		foreach ($vipbackup as $vip)
236
			array_unshift($config['virtualip']['vip'], $vip);
237
	}
238

    
239
	/* Log what happened */
240
	$mergedkeys = implode(",", array_merge(array_keys($params[0]), $sync_full_done));
241
	write_config(sprintf(gettext("Merged in config (%s sections) from XMLRPC client."),$mergedkeys));
242

    
243
	/* 
244
	 * The real work on handling the vips specially
245
	 * This is a copy of intefaces_vips_configure with addition of not reloading existing/not changed carps
246
	 */
247
	if (isset($params[0]['virtualip']) && is_array($config['virtualip']) && is_array($config['virtualip']['vip'])) {
248
		$carp_setuped = false;
249
		$anyproxyarp = false;
250
		foreach ($config['virtualip']['vip'] as $vip) {
251
			if ($vip['mode'] == "carp" && isset($oldvips["{$vip['interface']}_vip{$vip['vhid']}"])) {
252
				if ($oldvips["{$vip['interface']}_vip{$vip['vhid']}"] == "{$vip['password']}{$vip['advskew']}{$vip['subnet']}{$vip['subnet_bits']}{$vip['advbase']}") {
253
					if (does_vip_exist($vip)) {
254
						unset($oldvips["{$vip['interface']}_vip{$vip['vhid']}"]);
255
						continue; // Skip reconfiguring this vips since nothing has changed.
256
					}
257
				}
258
				unset($oldvips["{$vip['interface']}_vip{$vip['vhid']}"]);
259
			} else if ($vip['mode'] == "ipalias" && strstr($vip['interface'], "_vip") && isset($oldvips[$vip['subnet']])) {
260
				if ($oldvips[$vip['subnet']] == "{$vip['interface']}{$vip['subnet']}{$vip['subnet_bits']}") {
261
					if (does_vip_exist($vip)) {
262
						unset($oldvips[$vip['subnet']]);
263
						continue; // Skip reconfiguring this vips since nothing has changed.
264
					}
265
				}
266
				unset($oldvips[$vip['subnet']]);
267
			}
268

    
269
			switch ($vip['mode']) {
270
			case "proxyarp":
271
				$anyproxyarp = true;
272
				break;
273
			case "ipalias":
274
				interface_ipalias_configure($vip);
275
				break;
276
			case "carp":
277
				if ($carp_setuped == false)
278
                                        $carp_setuped = true;
279
				interface_carp_configure($vip);
280
				break;
281
			}
282
		}
283
		/* Cleanup remaining old carps */
284
		foreach ($oldvips as $oldvipif => $oldvippar) {
285
			$oldvipif = get_real_interface($oldvippar['interface']);
286
			if (!empty($oldvipif)) {
287
				if (is_ipaddrv6($oldvipif))
288
					 mwexec("/sbin/ifconfig " . escapeshellarg($oldvipif) . " inet6 " . escapeshellarg($oldvipar['subnet']) . " delete");
289
				else
290
					pfSense_interface_deladdress($oldvipif, $oldvipar['subnet']);
291
			}
292
		}
293
		if ($carp_setuped == true)
294
			interfaces_sync_setup();
295
		if ($anyproxyarp == true)
296
			interface_proxyarp_configure();
297
	}
298

    
299
	if (isset($old_config['ipsec']['enable']) !== isset($config['ipsec']['enable']))
300
		vpn_ipsec_configure();
301

    
302
	unset($old_config);
303

    
304
	return $xmlrpc_g['return']['true'];
305
}
306

    
307
/*****************************/
308
$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.");
309
$merge_config_section_sig = array(
310
	array(
311
		$XML_RPC_Boolean,
312
		$XML_RPC_String,
313
		$XML_RPC_Struct
314
	)
315
);
316

    
317
function merge_installedpackages_section_xmlrpc($raw_params) {
318
	global $config, $xmlrpc_g;
319

    
320
	if (xmlrpc_loop_detect()) {
321
		log_error("Disallowing CARP sync loop");
322
		return;
323
	}
324

    
325
	$params = xmlrpc_params_to_php($raw_params);
326
	if(!xmlrpc_auth($params)) {
327
		xmlrpc_authfail();
328
		return $xmlrpc_g['return']['authfail'];
329
	}
330
	$config['installedpackages'] = array_merge($config['installedpackages'], $params[0]);
331
	$mergedkeys = implode(",", array_keys($params[0]));
332
	write_config(sprintf(gettext("Merged in config (%s sections) from XMLRPC client."),$mergedkeys));
333

    
334
	return $xmlrpc_g['return']['true'];
335
}
336

    
337
/*****************************/
338
$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.");
339
$merge_config_section_sig = array(
340
	array(
341
		$XML_RPC_Boolean,
342
		$XML_RPC_String,
343
		$XML_RPC_Struct
344
	)
345
);
346

    
347
function merge_config_section_xmlrpc($raw_params) {
348
	global $config, $xmlrpc_g;
349

    
350
	if (xmlrpc_loop_detect()) {
351
		log_error("Disallowing CARP sync loop");
352
		return;
353
	}
354

    
355
	$params = xmlrpc_params_to_php($raw_params);
356
	if(!xmlrpc_auth($params)) {
357
		xmlrpc_authfail();
358
		return $xmlrpc_g['return']['authfail'];
359
	}
360
	$config_new = array_overlay($config, $params[0]);
361
	$config = $config_new;
362
	$mergedkeys = implode(",", array_keys($params[0]));
363
	write_config(sprintf(gettext("Merged in config (%s sections) from XMLRPC client."), $mergedkeys));
364
	return $xmlrpc_g['return']['true'];
365
}
366

    
367
/*****************************/
368
$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.");
369
$filter_configure_sig = array(
370
	array(
371
		$XML_RPC_Boolean,
372
		$XML_RPC_String
373
	)
374
);
375

    
376
function filter_configure_xmlrpc($raw_params) {
377
	global $xmlrpc_g, $config;
378

    
379
	$params = xmlrpc_params_to_php($raw_params);
380
	if(!xmlrpc_auth($params)) {
381
		xmlrpc_authfail();
382
		return $xmlrpc_g['return']['authfail'];
383
	}
384
	filter_configure();
385
	system_routing_configure();
386
	setup_gateways_monitor();
387
	relayd_configure();
388
	require_once("openvpn.inc");
389
	openvpn_resync_all();
390
	if (isset($config['dnsmasq']['enable']))
391
		services_dnsmasq_configure();
392
	elseif (isset($config['unbound']['enable']))
393
		services_unbound_configure();
394
	else
395
		# Both calls above run services_dhcpd_configure(), then we just
396
		# need to call it when them are not called to avoid restart dhcpd
397
		# twice, as described on ticket #3797
398
		services_dhcpd_configure();
399
	local_sync_accounts();
400

    
401
	return $xmlrpc_g['return']['true'];
402
}
403

    
404
/*****************************/
405
$carp_configure_doc = gettext("Basic XMLRPC wrapper for configuring CARP interfaces.");
406
$carp_configure_sig = array(
407
	array(
408
		$XML_RPC_Boolean,
409
		$XML_RPC_String
410
	)
411
);
412

    
413
function interfaces_carp_configure_xmlrpc($raw_params) {
414
	global $xmlrpc_g;
415

    
416
	if (xmlrpc_loop_detect()) {
417
		log_error("Disallowing CARP sync loop");
418
		return;
419
	}
420

    
421
	$params = xmlrpc_params_to_php($raw_params);
422
	if(!xmlrpc_auth($params)) {
423
		xmlrpc_authfail();
424
		return $xmlrpc_g['return']['authfail'];
425
	}
426
	interfaces_vips_configure();
427

    
428
	return $xmlrpc_g['return']['true'];
429
}
430

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

    
434
$check_firmware_version_sig = array(
435
	array(
436
		$XML_RPC_String,
437
		$XML_RPC_String
438
	)
439
);
440

    
441
function check_firmware_version_xmlrpc($raw_params) {
442
	global $xmlrpc_g, $XML_RPC_String;
443

    
444
	$params = xmlrpc_params_to_php($raw_params);
445
	if(!xmlrpc_auth($params)) {
446
		xmlrpc_authfail();
447
		return $xmlrpc_g['return']['authfail'];
448
	}
449
	return new XML_RPC_Response(new XML_RPC_Value(check_firmware_version(false), $XML_RPC_String));
450
}
451

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

    
455
$pfsense_firmware_version_sig = array (
456
        array (
457
                $XML_RPC_Struct,
458
                $XML_RPC_String
459
        )
460
);
461

    
462
function pfsense_firmware_version_xmlrpc($raw_params) {
463
        global $xmlrpc_g;
464

    
465
        $params = xmlrpc_params_to_php($raw_params);
466
        if(!xmlrpc_auth($params)) {
467
			xmlrpc_authfail();
468
			return $xmlrpc_g['return']['authfail'];
469
		}
470
        return new XML_RPC_Response(XML_RPC_encode(host_firmware_version()));
471
}
472

    
473
/*****************************/
474
$reboot_doc = gettext("Basic XMLRPC wrapper for rc.reboot.");
475
$reboot_sig = array(array($XML_RPC_Boolean, $XML_RPC_String));
476
function reboot_xmlrpc($raw_params) {
477
	global $xmlrpc_g;
478

    
479
	$params = xmlrpc_params_to_php($raw_params);
480
	if(!xmlrpc_auth($params)) {
481
		xmlrpc_authfail();
482
		return $xmlrpc_g['return']['authfail'];
483
	}
484
	mwexec_bg("/etc/rc.reboot");
485

    
486
	return $xmlrpc_g['return']['true'];
487
}
488

    
489
/*****************************/
490
$get_notices_sig = array(
491
	array(
492
		$XML_RPC_Array,
493
		$XML_RPC_String
494
	),
495
	array(
496
		$XML_RPC_Array
497
	)
498
);
499

    
500
function get_notices_xmlrpc($raw_params) {
501
	global $g, $xmlrpc_g;
502

    
503
	$params = xmlrpc_params_to_php($raw_params);
504
	if(!xmlrpc_auth($params)) {
505
		xmlrpc_authfail();
506
		return $xmlrpc_g['return']['authfail'];
507
	}
508
	if(!function_exists("get_notices"))
509
		require("notices.inc");
510
	if(!$params) {
511
		$toreturn = get_notices();
512
	} else {
513
		$toreturn = get_notices($params);
514
	}
515
	$response = new XML_RPC_Response(XML_RPC_encode($toreturn));
516

    
517
	return $response;
518
}
519

    
520
$xmlrpclockkey = lock('xmlrpc', LOCK_EX);
521

    
522
/*****************************/
523
$server = new XML_RPC_Server(
524
        array(
525
		'pfsense.exec_shell' => array('function' => 'exec_shell_xmlrpc',
526
			'signature' => $exec_shell_sig,
527
			'docstring' => $exec_shell_doc),
528
		'pfsense.exec_php' => array('function' => 'exec_php_xmlrpc',
529
			'signature' => $exec_php_sig,
530
			'docstring' => $exec_php_doc),	
531
		'pfsense.filter_configure' => array('function' => 'filter_configure_xmlrpc',
532
			'signature' => $filter_configure_sig,
533
			'docstring' => $filter_configure_doc),
534
		'pfsense.interfaces_carp_configure' => array('function' => 'interfaces_carp_configure_xmlrpc',
535
			'docstring' => $carp_configure_sig),
536
		'pfsense.backup_config_section' => array('function' => 'backup_config_section_xmlrpc',
537
			'signature' => $backup_config_section_sig,
538
			'docstring' => $backup_config_section_doc),
539
		'pfsense.restore_config_section' => array('function' => 'restore_config_section_xmlrpc',
540
			'signature' => $restore_config_section_sig,
541
			'docstring' => $restore_config_section_doc),
542
		'pfsense.merge_config_section' => array('function' => 'merge_config_section_xmlrpc',
543
			'signature' => $merge_config_section_sig,
544
			'docstring' => $merge_config_section_doc),
545
		'pfsense.merge_installedpackages_section_xmlrpc' => array('function' => 'merge_installedpackages_section_xmlrpc',
546
			'signature' => $merge_config_section_sig,
547
			'docstring' => $merge_config_section_doc),							
548
		'pfsense.check_firmware_version' => array('function' => 'check_firmware_version_xmlrpc',
549
			'signature' => $check_firmware_version_sig,
550
			'docstring' => $check_firmware_version_doc),
551
		'pfsense.host_firmware_version' => array('function' => 'pfsense_firmware_version_xmlrpc',
552
			'signature' => $pfsense_firmware_version_sig,
553
			'docstring' => $host_firmware_version_doc),
554
		'pfsense.reboot' => array('function' => 'reboot_xmlrpc',
555
			'signature' => $reboot_sig,
556
			'docstring' => $reboot_doc),
557
		'pfsense.get_notices' => array('function' => 'get_notices_xmlrpc',
558
			'signature' => $get_notices_sig)
559
        )
560
);
561

    
562
unlock($xmlrpclockkey);
563

    
564
    function array_overlay($a1,$a2)
565
    {
566
        foreach($a1 as $k => $v) {
567
            if(!array_key_exists($k,$a2)) continue;
568
            if(is_array($v) && is_array($a2[$k])){
569
                $a1[$k] = array_overlay($v,$a2[$k]);
570
            }else{
571
                $a1[$k] = $a2[$k];
572
            }
573
        }
574
        return $a1;
575
    }
576

    
577
?>
(256-256/256)