Project

General

Profile

Download (12.4 KB) Statistics
| Branch: | Tag: | Revision:
1 50d49018 Colin Smith
<?php
2
/*
3 c5d81585 Renato Botelho
 * xmlrpc.php
4 191cb31d Stephen Beaver
 *
5 c5d81585 Renato Botelho
 * part of pfSense (https://www.pfsense.org)
6 81299b5c Renato Botelho
 * Copyright (c) 2004-2016 Rubicon Communications, LLC (Netgate)
7 c5d81585 Renato Botelho
 * Copyright (c) 2005 Colin Smith
8
 * All rights reserved.
9 191cb31d Stephen Beaver
 *
10 b12ea3fb Renato Botelho
 * Licensed under the Apache License, Version 2.0 (the "License");
11
 * you may not use this file except in compliance with the License.
12
 * You may obtain a copy of the License at
13 191cb31d Stephen Beaver
 *
14 b12ea3fb Renato Botelho
 * http://www.apache.org/licenses/LICENSE-2.0
15 191cb31d Stephen Beaver
 *
16 b12ea3fb Renato Botelho
 * Unless required by applicable law or agreed to in writing, software
17
 * distributed under the License is distributed on an "AS IS" BASIS,
18
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19
 * See the License for the specific language governing permissions and
20
 * limitations under the License.
21 191cb31d Stephen Beaver
 */
22 50d49018 Colin Smith
23 6b07c15a Matthew Grooms
##|+PRIV
24
##|*IDENT=page-xmlrpclibrary
25 5230f468 jim-p
##|*NAME=XMLRPC Library
26 6b07c15a Matthew Grooms
##|*DESCR=Allow access to the 'XMLRPC Library' page.
27
##|*MATCH=xmlrpc.php*
28
##|-PRIV
29
30 c81ef6e2 Phil Davis
require_once("config.inc");
31
require_once("functions.inc");
32 f81e7cc4 Renato Botelho
require_once("auth.inc");
33 f6339216 jim-p
require_once("filter.inc");
34 c81ef6e2 Phil Davis
require_once("ipsec.inc");
35
require_once("vpn.inc");
36
require_once("shaper.inc");
37 f81e7cc4 Renato Botelho
require_once("XML/RPC2/Server.php");
38 50d49018 Colin Smith
39 f81e7cc4 Renato Botelho
class pfsense_xmlrpc_server {
40 c87f4b70 Ermal
41 f81e7cc4 Renato Botelho
	private $loop_detected = false;
42
	private $remote_addr;
43 c87f4b70 Ermal
44 dc5f639f PiBa-NL
	private function auth() {
45 f81e7cc4 Renato Botelho
		global $config;
46 dc5f639f PiBa-NL
		$username = $_SERVER['PHP_AUTH_USER'];
47
		$password = $_SERVER['PHP_AUTH_PW'];
48 8da3de34 Colin Smith
49 fb1234ab Renato Botelho
		$login_ok = false;
50 f81e7cc4 Renato Botelho
		if (!empty($username) && !empty($password)) {
51
			$attributes = array();
52
			$authcfg = auth_get_authserver(
53
			    $config['system']['webgui']['authmode']);
54 c3638879 Scott Ullrich
55 f81e7cc4 Renato Botelho
			if (authenticate_user($username, $password,
56
			    $authcfg, $attributes) ||
57
			    authenticate_user($username, $password)) {
58 fb1234ab Renato Botelho
				$login_ok = true;
59 f81e7cc4 Renato Botelho
			}
60
		}
61 3dd2a278 Scott Ullrich
62 fb1234ab Renato Botelho
		if (!$login_ok) {
63
			log_auth("webConfigurator authentication error for '" .
64
			    $username . "' from " . $this->remote_addr);
65 137f46d8 Ermal
66 fb1234ab Renato Botelho
			require_once("XML/RPC2/Exception.php");
67
			throw new XML_RPC2_FaultException(gettext(
68
			    'Authentication failed: Invalid username or password'),
69
			    -1);
70
		}
71
72
		$user_entry = getUserEntry($username);
73
		/*
74
		 * admin (uid = 0) is allowed
75
		 * or regular user with necessary privilege
76
		 */
77
		if (isset($user_entry['uid']) && $user_entry['uid'] != '0' &&
78
		    !userHasPrivilege($user_entry, 'system-xmlrpc-ha-sync')) {
79
			log_auth("webConfigurator authentication error for '" .
80
			    $username . "' from " . $this->remote_addr .
81
			    " not enough privileges");
82
83
			require_once("XML/RPC2/Exception.php");
84
			throw new XML_RPC2_FaultException(gettext(
85
			    'Authentication failed: not enough privileges'),
86
			    -2);
87
		}
88
89
		return;
90 3dd2a278 Scott Ullrich
	}
91 f81e7cc4 Renato Botelho
92
	private function array_overlay($a1, $a2) {
93
		foreach ($a1 as $k => $v) {
94
			if (!array_key_exists($k, $a2)) {
95
				continue;
96
			}
97
			if (is_array($v) && is_array($a2[$k])) {
98
				$a1[$k] = $this->array_overlay($v, $a2[$k]);
99
			} else {
100
				$a1[$k] = $a2[$k];
101
			}
102
		}
103
104
		return $a1;
105 962f215d Phil Davis
	}
106 c3638879 Scott Ullrich
107 f81e7cc4 Renato Botelho
	public function __construct() {
108
		global $config;
109 c3638879 Scott Ullrich
110 f82f991c Renato Botelho
		$this->remote_addr = $_SERVER['REMOTE_ADDR'];
111 137f46d8 Ermal
112 f81e7cc4 Renato Botelho
		/* grab sync to ip if enabled */
113
		if (isset($config['hasync']['synchronizetoip']) &&
114 8d44b2cb PiBa-NL
		    $config['hasync']['synchronizetoip'] == $this->remote_addr) {
115 f81e7cc4 Renato Botelho
			$this->loop_detected = true;
116
		}
117 3dd2a278 Scott Ullrich
	}
118 137f46d8 Ermal
119 f81e7cc4 Renato Botelho
	/**
120
	 * Get host version information
121
	 *
122
	 * @return array
123
	 */
124 dc5f639f PiBa-NL
	public function host_firmware_version($dummy = 1) {
125
		$this->auth();
126 f81e7cc4 Renato Botelho
		return host_firmware_version();
127
	}
128 21dc3a7d Colin Smith
129 f81e7cc4 Renato Botelho
	/**
130
	 * Executes a PHP block of code
131
	 *
132
	 * @param string $code
133
	 *
134
	 * @return bool
135
	 */
136 dc5f639f PiBa-NL
	public function exec_php($code) {
137
		$this->auth();
138 137f46d8 Ermal
139 f81e7cc4 Renato Botelho
		eval($code);
140
		if ($toreturn) {
141
			return $toreturn;
142
		}
143 c87f4b70 Ermal
144 f81e7cc4 Renato Botelho
		return true;
145 3dd2a278 Scott Ullrich
	}
146 137f46d8 Ermal
147 f81e7cc4 Renato Botelho
	/**
148
	 * Executes shell commands
149
	 *
150
	 * @param string $code
151
	 *
152
	 * @return bool
153
	 */
154 dc5f639f PiBa-NL
	public function exec_shell($code) {
155
		$this->auth();
156 50d49018 Colin Smith
157 f81e7cc4 Renato Botelho
		mwexec($code);
158
		return true;
159
	}
160 21dc3a7d Colin Smith
161 f81e7cc4 Renato Botelho
	/**
162
	 * Backup chosen config sections
163
	 *
164
	 * @param array $section
165
	 *
166
	 * @return array
167
	 */
168 dc5f639f PiBa-NL
	public function backup_config_section($section) {
169
		$this->auth();
170 137f46d8 Ermal
171 f81e7cc4 Renato Botelho
		global $config;
172 d026178f Renato Botelho
173 f81e7cc4 Renato Botelho
		return array_intersect_key($config, array_flip($section));
174 fb0eb20b Ermal
	}
175 c87f4b70 Ermal
176 f81e7cc4 Renato Botelho
	/**
177
	 * Restore defined config section into local config
178
	 *
179
	 * @param array $sections
180
	 *
181
	 * @return bool
182
	 */
183 dc5f639f PiBa-NL
	public function restore_config_section($sections) {
184
		$this->auth();
185 f81e7cc4 Renato Botelho
186
		global $config;
187 1b99e1e5 jim-p
188 f81e7cc4 Renato Botelho
		$old_config = $config;
189
		$old_ipsec_enabled = ipsec_enabled();
190
191
		if ($this->loop_detected) {
192
			log_error("Disallowing CARP sync loop");
193
			return true;
194
		}
195
196
		/*
197
		 * Some sections should just be copied and not merged or we end
198
		 * up unable to sync the deletion of the last item in a section
199
		 */
200
		$sync_full_sections = array(
201
			'aliases',
202
			'ca',
203
			'cert',
204
			'crl',
205
			'dhcpd',
206
			'dhcpv6',
207
			'dnsmasq',
208
			'filter',
209
			'ipsec',
210
			'load_balancer',
211
			'nat',
212
			'openvpn',
213
			'schedules',
214
			'unbound',
215
			'wol',
216
		);
217
218
		$syncd_full_sections = array();
219
220
		foreach ($sync_full_sections as $section) {
221
			if (!isset($sections[$section])) {
222
				continue;
223
			}
224
225
			$config[$section] = $sections[$section];
226
			unset($sections[$section]);
227
			$syncd_full_sections[] = $section;
228 1b99e1e5 jim-p
		}
229
230 f81e7cc4 Renato Botelho
		$vipbackup = array();
231
		$oldvips = array();
232
		if (isset($sections['virtualip']) &&
233
		    is_array($config['virtualip']['vip'])) {
234
			foreach ($config['virtualip']['vip'] as $vip) {
235 c14781e3 Renato Botelho
				if ($vip['mode'] == "carp") {
236 f81e7cc4 Renato Botelho
					$key = $vip['interface'] .
237
					    "_vip" . $vip['vhid'];
238
239
					$oldvips[$key]['content'] =
240
					    $vip['password'] .
241
					    $vip['advskew'] .
242
					    $vip['subnet'] .
243
					    $vip['subnet_bits'] .
244
					    $vip['advbase'];
245
					$oldvips[$key]['interface'] =
246
					    $vip['interface'];
247
					$oldvips[$key]['subnet'] =
248
					    $vip['subnet'];
249
				} else if ($vip['mode'] == "ipalias" &&
250
				    (substr($vip['interface'], 0, 4) == '_vip'
251
				    || strstr($vip['interface'], "lo0"))) {
252
					$oldvips[$vip['subnet']]['content'] =
253
					    $vip['interface'] .
254
					    $vip['subnet'] .
255
					    $vip['subnet_bits'];
256
					$oldvips[$vip['subnet']]['interface'] =
257
					    $vip['interface'];
258
					$oldvips[$vip['subnet']]['subnet'] =
259
					    $vip['subnet'];
260
				} else if (($vip['mode'] == "ipalias" ||
261
				    $vip['mode'] == 'proxyarp') &&
262
				    !(substr($vip['interface'], 0, 4) == '_vip')
263
				    || strstr($vip['interface'], "lo0")) {
264 51611440 Ermal
					$vipbackup[] = $vip;
265 c14781e3 Renato Botelho
				}
266 51611440 Ermal
			}
267 19b5c3e7 Ermal
		}
268 f51d4f98 Ermal
269 f81e7cc4 Renato Botelho
		/* For vip section, first keep items sent from the master */
270
		$config = array_merge_recursive_unique($config, $sections);
271 51611440 Ermal
272 f81e7cc4 Renato Botelho
		/*
273
		 * Then add ipalias and proxyarp types already defined
274
		 * on the backup
275
		 */
276
		if (is_array($vipbackup) && !empty($vipbackup)) {
277
			if (!is_array($config['virtualip'])) {
278
				$config['virtualip'] = array();
279
			}
280
			if (!is_array($config['virtualip']['vip'])) {
281
				$config['virtualip']['vip'] = array();
282
			}
283
			foreach ($vipbackup as $vip) {
284
				array_unshift($config['virtualip']['vip'], $vip);
285
			}
286 962f215d Phil Davis
		}
287 51611440 Ermal
288 f81e7cc4 Renato Botelho
		/* Log what happened */
289
		$mergedkeys = implode(",", array_merge(array_keys($sections),
290
		    $syncd_full_sections));
291
		write_config(sprintf(gettext(
292
		    "Merged in config (%s sections) from XMLRPC client."),
293
		    $mergedkeys));
294
295
		/*
296
		 * The real work on handling the vips specially
297
		 * This is a copy of intefaces_vips_configure with addition of
298
		 * not reloading existing/not changed carps
299
		 */
300
		if (isset($sections['virtualip']) &&
301
		    is_array($config['virtualip']) &&
302
		    is_array($config['virtualip']['vip'])) {
303
			$carp_setuped = false;
304
			$anyproxyarp = false;
305
306
			foreach ($config['virtualip']['vip'] as $vip) {
307
				$key = "{$vip['interface']}_vip{$vip['vhid']}";
308
309
				if ($vip['mode'] == "carp" &&
310
				    isset($oldvips[$key])) {
311
					if ($oldvips[$key]['content'] ==
312
					    $vip['password'] .
313
					    $vip['advskew'] .
314
					    $vip['subnet'] .
315
					    $vip['subnet_bits'] .
316
					    $vip['advbase'] &&
317
					    does_vip_exist($vip)) {
318
						unset($oldvips[$key]);
319
						/*
320
						 * Skip reconfiguring this vips
321
						 * since nothing has changed.
322
						 */
323
						continue;
324 19ed1624 Ermal
					}
325 5fda51cd jim-p
326 f81e7cc4 Renato Botelho
				} elseif ($vip['mode'] == "ipalias" &&
327 5fda51cd jim-p
				    (substr($vip['interface'], 0, 4) == '_vip'
328
				    || strstr($vip['interface'], "lo0")) &&
329 f81e7cc4 Renato Botelho
				    isset($oldvips[$vip['subnet']])) {
330
					$key = $vip['subnet'];
331
					if ($oldvips[$key]['content'] ==
332
					    $vip['interface'] .
333
					    $vip['subnet'] .
334
					    $vip['subnet_bits'] &&
335
					    does_vip_exist($vip)) {
336
						unset($oldvips[$key]);
337
						/*
338
						 * Skip reconfiguring this vips
339
						 * since nothing has changed.
340
						 */
341
						continue;
342 2708a5cf Ermal
					}
343 f81e7cc4 Renato Botelho
					unset($oldvips[$key]);
344 2708a5cf Ermal
				}
345 51611440 Ermal
346 f81e7cc4 Renato Botelho
				switch ($vip['mode']) {
347 962f215d Phil Davis
				case "proxyarp":
348
					$anyproxyarp = true;
349
					break;
350
				case "ipalias":
351
					interface_ipalias_configure($vip);
352
					break;
353
				case "carp":
354 f81e7cc4 Renato Botelho
					$carp_setuped = true;
355 962f215d Phil Davis
					interface_carp_configure($vip);
356
					break;
357 f81e7cc4 Renato Botelho
				}
358 51611440 Ermal
			}
359 f81e7cc4 Renato Botelho
360
			/* Cleanup remaining old carps */
361
			foreach ($oldvips as $oldvipar) {
362
				$oldvipif = get_real_interface(
363
				    $oldvipar['interface']);
364
365
				if (empty($oldvipif)) {
366
					continue;
367
				}
368
369 962f215d Phil Davis
				if (is_ipaddrv6($oldvipar['subnet'])) {
370 f81e7cc4 Renato Botelho
					 mwexec("/sbin/ifconfig " .
371
					     escapeshellarg($oldvipif) .
372
					     " inet6 " .
373
					     escapeshellarg($oldvipar['subnet']) .
374
					     " delete");
375 962f215d Phil Davis
				} else {
376 f81e7cc4 Renato Botelho
					pfSense_interface_deladdress($oldvipif,
377
					    $oldvipar['subnet']);
378 962f215d Phil Davis
				}
379 e3cffd6c Ermal LUÇI
			}
380 f81e7cc4 Renato Botelho
			if ($carp_setuped == true) {
381
				interfaces_sync_setup();
382
			}
383
			if ($anyproxyarp == true) {
384
				interface_proxyarp_configure();
385
			}
386 51611440 Ermal
		}
387 f81e7cc4 Renato Botelho
388
		if ($old_ipsec_enabled !== ipsec_enabled()) {
389
			vpn_ipsec_configure();
390 962f215d Phil Davis
		}
391 137f46d8 Ermal
392 f81e7cc4 Renato Botelho
		unset($old_config);
393
394
		return true;
395 962f215d Phil Davis
	}
396 d026178f Renato Botelho
397 f81e7cc4 Renato Botelho
	/**
398
	 * Merge items into installedpackages config section
399
	 *
400
	 * @param array $section
401
	 *
402
	 * @return bool
403
	 */
404 dc5f639f PiBa-NL
	public function merge_installedpackages_section($section) {
405
		$this->auth();
406 d026178f Renato Botelho
407 f81e7cc4 Renato Botelho
		global $config;
408 50d49018 Colin Smith
409 f81e7cc4 Renato Botelho
		if ($this->loop_detected) {
410
			log_error("Disallowing CARP sync loop");
411
			return true;
412
		}
413 82ae5cfc Scott Ullrich
414 f81e7cc4 Renato Botelho
		$config['installedpackages'] = array_merge(
415
		    $config['installedpackages'], $section);
416
		$mergedkeys = implode(",", array_keys($section));
417
		write_config(sprintf(gettext(
418
		    "Merged in config (%s sections) from XMLRPC client."),
419
		    $mergedkeys));
420 137f46d8 Ermal
421 f81e7cc4 Renato Botelho
		return true;
422 fb0eb20b Ermal
	}
423 c87f4b70 Ermal
424 f81e7cc4 Renato Botelho
	/**
425
	 * Merge items into config
426
	 *
427
	 * @param array $section
428
	 *
429
	 * @return bool
430
	 */
431 dc5f639f PiBa-NL
	public function merge_config_section($section) {
432
		$this->auth();
433 137f46d8 Ermal
434 f81e7cc4 Renato Botelho
		global $config;
435 82ae5cfc Scott Ullrich
436 f81e7cc4 Renato Botelho
		if ($this->loop_detected) {
437
			log_error("Disallowing CARP sync loop");
438
			return true;
439
		}
440 dc1cd85d Scott Ullrich
441 f81e7cc4 Renato Botelho
		$config_new = $this->array_overlay($config, $section);
442
		$config = $config_new;
443
		$mergedkeys = implode(",", array_keys($section));
444
		write_config(sprintf(gettext(
445
		    "Merged in config (%s sections) from XMLRPC client."),
446
		    $mergedkeys));
447 c87f4b70 Ermal
448 f81e7cc4 Renato Botelho
		return true;
449 fb0eb20b Ermal
	}
450 c87f4b70 Ermal
451 f81e7cc4 Renato Botelho
	/**
452
	 * Wrapper for filter_configure()
453
	 *
454
	 * @return bool
455 57b5da70 jim-p
	 */
456 dc5f639f PiBa-NL
	public function filter_configure() {
457
		$this->auth();
458 f81e7cc4 Renato Botelho
459
		global $g, $config;
460
461
		filter_configure();
462
		system_routing_configure();
463
		setup_gateways_monitor();
464
		relayd_configure();
465
		require_once("openvpn.inc");
466
		openvpn_resync_all();
467
468
		/*
469
		 * The DNS Resolver and the DNS Forwarder may both be active so
470
		 * long as * they are running on different ports.
471
		 * See ticket #5882
472
		 */
473
		if (isset($config['dnsmasq']['enable'])) {
474
			/* Configure dnsmasq but tell it NOT to restart DHCP */
475
			services_dnsmasq_configure(false);
476
		} else {
477
			/* kill any running dnsmasq instance */
478
			if (isvalidpid("{$g['varrun_path']}/dnsmasq.pid")) {
479
				sigkillbypid("{$g['varrun_path']}/dnsmasq.pid",
480
				    "TERM");
481
			}
482 57b5da70 jim-p
		}
483 f81e7cc4 Renato Botelho
		if (isset($config['unbound']['enable'])) {
484
			/* Configure unbound but tell it NOT to restart DHCP */
485
			services_unbound_configure(false);
486
		} else {
487
			/* kill any running Unbound instance */
488
			if (isvalidpid("{$g['varrun_path']}/unbound.pid")) {
489
				sigkillbypid("{$g['varrun_path']}/unbound.pid",
490
				    "TERM");
491
			}
492 57b5da70 jim-p
		}
493 137f46d8 Ermal
494 f81e7cc4 Renato Botelho
		/*
495
		 * Call this separately since the above are manually set to
496
		 * skip the DHCP restart they normally perform.
497
		 * This avoids restarting dhcpd twice as described on
498
		 * ticket #3797
499
		 */
500
		services_dhcpd_configure();
501 137f46d8 Ermal
502 f81e7cc4 Renato Botelho
		local_sync_accounts();
503 c87f4b70 Ermal
504 f81e7cc4 Renato Botelho
		return true;
505 3dd2a278 Scott Ullrich
	}
506 137f46d8 Ermal
507 f81e7cc4 Renato Botelho
	/**
508
	 * Wrapper for configuring CARP interfaces
509
	 *
510
	 * @return bool
511
	 */
512 dc5f639f PiBa-NL
	public function interfaces_carp_configure() {
513
		$this->auth();
514 efe7562e Scott Ullrich
515 f81e7cc4 Renato Botelho
		if ($this->loop_detected) {
516
			log_error("Disallowing CARP sync loop");
517
			return true;
518
		}
519 0567899d Ermal
520 f81e7cc4 Renato Botelho
		interfaces_vips_configure();
521 e501de37 Ermal
522 f81e7cc4 Renato Botelho
		return true;
523
	}
524 e501de37 Ermal
525 f81e7cc4 Renato Botelho
	/**
526
	 * Wrapper for rc.reboot
527
	 *
528
	 * @return bool
529
	 */
530 dc5f639f PiBa-NL
	public function reboot() {
531
		$this->auth();
532 e501de37 Ermal
533 f81e7cc4 Renato Botelho
		mwexec_bg("/etc/rc.reboot");
534 137f46d8 Ermal
535 f81e7cc4 Renato Botelho
		return true;
536 3dd2a278 Scott Ullrich
	}
537 d9064267 Colin Smith
}
538
539 67d78c87 Ermal
$xmlrpclockkey = lock('xmlrpc', LOCK_EX);
540
541 f81e7cc4 Renato Botelho
XML_RPC2_Backend::setBackend('php');
542
$HTTP_RAW_POST_DATA = file_get_contents('php://input');
543
544
$options = array(
545
	'prefix' => 'pfsense.',
546
	'encoding' => 'utf-8',
547 4f78ae1d Renato Botelho
	'autoDocument' => false,
548 50d49018 Colin Smith
);
549 b298dd06 Scott Ullrich
550 f81e7cc4 Renato Botelho
$server = XML_RPC2_Server::create(new pfsense_xmlrpc_server(), $options);
551
$server->handleCall();
552 67d78c87 Ermal
553 f81e7cc4 Renato Botelho
unlock($xmlrpclockkey);
554 0b581a8a Scott Ullrich
555 de63649b Rafael Lucas
?>