Project

General

Profile

Download (14.7 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
 * xmlrpc.php
4
 *
5
 * part of pfSense (https://www.pfsense.org)
6
 * Copyright (c) 2004-2018 Rubicon Communications, LLC (Netgate)
7
 * Copyright (c) 2005 Colin Smith
8
 * All rights reserved.
9
 *
10
 * 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
 *
14
 * http://www.apache.org/licenses/LICENSE-2.0
15
 *
16
 * 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
 */
22

    
23
##|+PRIV
24
##|*IDENT=page-xmlrpclibrary
25
##|*NAME=XMLRPC Library
26
##|*DESCR=Allow access to the 'XMLRPC Library' page.
27
##|*MATCH=xmlrpc.php*
28
##|-PRIV
29

    
30
require_once("config.inc");
31
require_once("functions.inc");
32
require_once("auth.inc");
33
require_once("filter.inc");
34
require_once("ipsec.inc");
35
require_once("vpn.inc");
36
require_once("shaper.inc");
37
require_once("XML/RPC2/Server.php");
38

    
39
class pfsense_xmlrpc_server {
40

    
41
	private $loop_detected = false;
42
	private $remote_addr;
43

    
44
	private function auth() {
45
		global $config;
46
		$username = $_SERVER['PHP_AUTH_USER'];
47
		$password = $_SERVER['PHP_AUTH_PW'];
48

    
49
		$login_ok = false;
50
		if (!empty($username) && !empty($password)) {
51
			$attributes = array();
52
			$authcfg = auth_get_authserver(
53
			    $config['system']['webgui']['authmode']);
54

    
55
			if (authenticate_user($username, $password,
56
			    $authcfg, $attributes) ||
57
			    authenticate_user($username, $password)) {
58
				$login_ok = true;
59
			}
60
		}
61

    
62
		if (!$login_ok) {
63
			log_auth("webConfigurator authentication error for '" .
64
			    $username . "' from " . $this->remote_addr);
65

    
66
			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
	}
91

    
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
	}
106

    
107
	public function __construct() {
108
		global $config;
109

    
110
		$this->remote_addr = $_SERVER['REMOTE_ADDR'];
111

    
112
		/* grab sync to ip if enabled */
113
		if (isset($config['hasync']['synchronizetoip']) &&
114
		    $config['hasync']['synchronizetoip'] == $this->remote_addr) {
115
			$this->loop_detected = true;
116
		}
117
	}
118

    
119
	/**
120
	 * Get host version information
121
	 *
122
	 * @return array
123
	 */
124
	public function host_firmware_version($dummy = 1) {
125
		$this->auth();
126
		return host_firmware_version();
127
	}
128

    
129
	/**
130
	 * Executes a PHP block of code
131
	 *
132
	 * @param string $code
133
	 *
134
	 * @return bool
135
	 */
136
	public function exec_php($code) {
137
		$this->auth();
138

    
139
		eval($code);
140
		if ($toreturn) {
141
			return $toreturn;
142
		}
143

    
144
		return true;
145
	}
146

    
147
	/**
148
	 * Executes shell commands
149
	 *
150
	 * @param string $code
151
	 *
152
	 * @return bool
153
	 */
154
	public function exec_shell($code) {
155
		$this->auth();
156

    
157
		mwexec($code);
158
		return true;
159
	}
160

    
161
	/**
162
	 * Backup chosen config sections
163
	 *
164
	 * @param array $section
165
	 *
166
	 * @return array
167
	 */
168
	public function backup_config_section($section) {
169
		$this->auth();
170

    
171
		global $config;
172

    
173
		return array_intersect_key($config, array_flip($section));
174
	}
175

    
176
	/**
177
	 * Restore defined config section into local config
178
	 *
179
	 * @param array $sections
180
	 *
181
	 * @return bool
182
	 */
183
	public function restore_config_section($sections) {
184
		$this->auth();
185

    
186
		global $config;
187

    
188
		$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
		}
229

    
230
		$g2add = array();
231
		$g2del = array();
232
		$g2del_idx = array();
233
		$g2keep = array();
234
		if (is_array($sections['system']['group'])) {
235
			$local_groups = isset($config['system']['group'])
236
			    ? $config['system']['group']
237
			    : array();
238

    
239
			foreach ($sections['system']['group'] as $group) {
240
				$idx = array_search($group['name'],
241
				    array_column($local_groups, 'name'));
242

    
243
				if ($idx === false) {
244
					$g2add[] = $group;
245
				} else if ($group['gid'] < 1999) {
246
					$g2keep[] = $idx;
247
				} else if ($group != $local_groups[$idx]) {
248
					$g2add[] = $group;
249
					$g2del[] = $group;
250
					$g2del_idx[] = $idx;
251
				} else {
252
					$g2keep[] = $idx;
253
				}
254
			}
255
		}
256
		if (is_array($config['system']['group'])) {
257
			foreach ($config['system']['group'] as $idx => $group) {
258
				if (array_search($idx, $g2keep) === false &&
259
				    array_search($idx, $g2del_idx) === false) {
260
					$g2del[] = $group;
261
					$g2del_idx[] = $idx;
262
				}
263
			}
264
		}
265
		unset($sections['system']['group'], $g2keep, $g2del_idx);
266

    
267
		$u2add = array();
268
		$u2del = array();
269
		$u2del_idx = array();
270
		$u2keep = array();
271
		if (is_array($sections['system']['user'])) {
272
			$local_users = isset($config['system']['user'])
273
			    ? $config['system']['user']
274
			    : array();
275

    
276
			foreach ($sections['system']['user'] as $user) {
277
				$idx = array_search($user['name'],
278
				    array_column($local_users, 'name'));
279

    
280
				if ($idx === false) {
281
					$u2add[] = $user;
282
				} else if ($user['uid'] < 2000) {
283
					$u2keep[] = $idx;
284
				} else if ($user != $local_users[$idx]) {
285
					$u2add[] = $user;
286
					$u2del[] = $user;
287
					$u2del_idx[] = $idx;
288
				} else {
289
					$u2keep[] = $idx;
290
				}
291
			}
292
		}
293
		if (is_array($config['system']['user'])) {
294
			foreach ($config['system']['user'] as $idx => $user) {
295
				if (array_search($idx, $u2keep) === false &&
296
				    array_search($idx, $u2del_idx) === false) {
297
					$u2del[] = $user;
298
					$u2del_idx[] = $idx;
299
				}
300
			}
301
		}
302
		unset($sections['system']['user'], $u2keep, $u2del_idx);
303

    
304
		$vipbackup = array();
305
		$oldvips = array();
306
		if (isset($sections['virtualip']) &&
307
		    is_array($config['virtualip']['vip'])) {
308
			foreach ($config['virtualip']['vip'] as $vip) {
309
				if ($vip['mode'] == "carp") {
310
					$key = $vip['interface'] .
311
					    "_vip" . $vip['vhid'];
312

    
313
					$oldvips[$key]['content'] =
314
					    $vip['password'] .
315
					    $vip['advskew'] .
316
					    $vip['subnet'] .
317
					    $vip['subnet_bits'] .
318
					    $vip['advbase'];
319
					$oldvips[$key]['interface'] =
320
					    $vip['interface'];
321
					$oldvips[$key]['subnet'] =
322
					    $vip['subnet'];
323
				} else if ($vip['mode'] == "ipalias" &&
324
				    (substr($vip['interface'], 0, 4) == '_vip'
325
				    || strstr($vip['interface'], "lo0"))) {
326
					$oldvips[$vip['subnet']]['content'] =
327
					    $vip['interface'] .
328
					    $vip['subnet'] .
329
					    $vip['subnet_bits'];
330
					$oldvips[$vip['subnet']]['interface'] =
331
					    $vip['interface'];
332
					$oldvips[$vip['subnet']]['subnet'] =
333
					    $vip['subnet'];
334
				} else if (($vip['mode'] == "ipalias" ||
335
				    $vip['mode'] == 'proxyarp') &&
336
				    !(substr($vip['interface'], 0, 4) == '_vip')
337
				    || strstr($vip['interface'], "lo0")) {
338
					$vipbackup[] = $vip;
339
				}
340
			}
341
		}
342

    
343
		/* For vip section, first keep items sent from the master */
344
		$config = array_merge_recursive_unique($config, $sections);
345

    
346
		/*
347
		 * Then add ipalias and proxyarp types already defined
348
		 * on the backup
349
		 */
350
		if (is_array($vipbackup) && !empty($vipbackup)) {
351
			if (!is_array($config['virtualip'])) {
352
				$config['virtualip'] = array();
353
			}
354
			if (!is_array($config['virtualip']['vip'])) {
355
				$config['virtualip']['vip'] = array();
356
			}
357
			foreach ($vipbackup as $vip) {
358
				array_unshift($config['virtualip']['vip'], $vip);
359
			}
360
		}
361

    
362
		/* Log what happened */
363
		$mergedkeys = implode(", ", array_merge(array_keys($sections),
364
		    $syncd_full_sections));
365
		write_config(sprintf(gettext(
366
		    "Merged in config (%s sections) from XMLRPC client."),
367
		    $mergedkeys));
368

    
369
		/*
370
		 * The real work on handling the vips specially
371
		 * This is a copy of intefaces_vips_configure with addition of
372
		 * not reloading existing/not changed carps
373
		 */
374
		if (isset($sections['virtualip']) &&
375
		    is_array($config['virtualip']) &&
376
		    is_array($config['virtualip']['vip'])) {
377
			$carp_setuped = false;
378
			$anyproxyarp = false;
379

    
380
			foreach ($config['virtualip']['vip'] as $vip) {
381
				$key = "{$vip['interface']}_vip{$vip['vhid']}";
382

    
383
				if ($vip['mode'] == "carp" &&
384
				    isset($oldvips[$key])) {
385
					if ($oldvips[$key]['content'] ==
386
					    $vip['password'] .
387
					    $vip['advskew'] .
388
					    $vip['subnet'] .
389
					    $vip['subnet_bits'] .
390
					    $vip['advbase'] &&
391
					    does_vip_exist($vip)) {
392
						unset($oldvips[$key]);
393
						/*
394
						 * Skip reconfiguring this vips
395
						 * since nothing has changed.
396
						 */
397
						continue;
398
					}
399

    
400
				} elseif ($vip['mode'] == "ipalias" &&
401
				    (substr($vip['interface'], 0, 4) == '_vip'
402
				    || strstr($vip['interface'], "lo0")) &&
403
				    isset($oldvips[$vip['subnet']])) {
404
					$key = $vip['subnet'];
405
					if ($oldvips[$key]['content'] ==
406
					    $vip['interface'] .
407
					    $vip['subnet'] .
408
					    $vip['subnet_bits'] &&
409
					    does_vip_exist($vip)) {
410
						unset($oldvips[$key]);
411
						/*
412
						 * Skip reconfiguring this vips
413
						 * since nothing has changed.
414
						 */
415
						continue;
416
					}
417
					unset($oldvips[$key]);
418
				}
419

    
420
				switch ($vip['mode']) {
421
				case "proxyarp":
422
					$anyproxyarp = true;
423
					break;
424
				case "ipalias":
425
					interface_ipalias_configure($vip);
426
					break;
427
				case "carp":
428
					$carp_setuped = true;
429
					interface_carp_configure($vip);
430
					break;
431
				}
432
			}
433

    
434
			/* Cleanup remaining old carps */
435
			foreach ($oldvips as $oldvipar) {
436
				$oldvipif = get_real_interface(
437
				    $oldvipar['interface']);
438

    
439
				if (empty($oldvipif)) {
440
					continue;
441
				}
442

    
443
				if (is_ipaddrv6($oldvipar['subnet'])) {
444
					 mwexec("/sbin/ifconfig " .
445
					     escapeshellarg($oldvipif) .
446
					     " inet6 " .
447
					     escapeshellarg($oldvipar['subnet']) .
448
					     " delete");
449
				} else {
450
					pfSense_interface_deladdress($oldvipif,
451
					    $oldvipar['subnet']);
452
				}
453
			}
454
			if ($carp_setuped == true) {
455
				interfaces_sync_setup();
456
			}
457
			if ($anyproxyarp == true) {
458
				interface_proxyarp_configure();
459
			}
460
		}
461

    
462
		if ($old_ipsec_enabled !== ipsec_enabled()) {
463
			vpn_ipsec_configure();
464
		}
465

    
466
		unset($old_config);
467

    
468
		local_sync_accounts($u2add, $u2del, $g2add, $g2del);
469
		filter_configure(false);
470

    
471
		return true;
472
	}
473

    
474
	/**
475
	 * Merge items into installedpackages config section
476
	 *
477
	 * @param array $section
478
	 *
479
	 * @return bool
480
	 */
481
	public function merge_installedpackages_section($section) {
482
		$this->auth();
483

    
484
		global $config;
485

    
486
		if ($this->loop_detected) {
487
			log_error("Disallowing CARP sync loop");
488
			return true;
489
		}
490

    
491
		$config['installedpackages'] = array_merge(
492
		    $config['installedpackages'], $section);
493
		$mergedkeys = implode(", ", array_keys($section));
494
		write_config(sprintf(gettext(
495
		    "Merged in config (%s sections) from XMLRPC client."),
496
		    $mergedkeys));
497

    
498
		return true;
499
	}
500

    
501
	/**
502
	 * Merge items into config
503
	 *
504
	 * @param array $section
505
	 *
506
	 * @return bool
507
	 */
508
	public function merge_config_section($section) {
509
		$this->auth();
510

    
511
		global $config;
512

    
513
		if ($this->loop_detected) {
514
			log_error("Disallowing CARP sync loop");
515
			return true;
516
		}
517

    
518
		$config_new = $this->array_overlay($config, $section);
519
		$config = $config_new;
520
		$mergedkeys = implode(", ", array_keys($section));
521
		write_config(sprintf(gettext(
522
		    "Merged in config (%s sections) from XMLRPC client."),
523
		    $mergedkeys));
524

    
525
		return true;
526
	}
527

    
528
	/**
529
	 * Wrapper for filter_configure()
530
	 *
531
	 * @return bool
532
	 */
533
	public function filter_configure($reset_accounts = true) {
534
		$this->auth();
535

    
536
		global $g, $config;
537

    
538
		filter_configure();
539
		system_routing_configure();
540
		setup_gateways_monitor();
541
		relayd_configure();
542
		require_once("openvpn.inc");
543
		openvpn_resync_all();
544

    
545
		/*
546
		 * The DNS Resolver and the DNS Forwarder may both be active so
547
		 * long as * they are running on different ports.
548
		 * See ticket #5882
549
		 */
550
		if (isset($config['dnsmasq']['enable'])) {
551
			/* Configure dnsmasq but tell it NOT to restart DHCP */
552
			services_dnsmasq_configure(false);
553
		} else {
554
			/* kill any running dnsmasq instance */
555
			if (isvalidpid("{$g['varrun_path']}/dnsmasq.pid")) {
556
				sigkillbypid("{$g['varrun_path']}/dnsmasq.pid",
557
				    "TERM");
558
			}
559
		}
560
		if (isset($config['unbound']['enable'])) {
561
			/* Configure unbound but tell it NOT to restart DHCP */
562
			services_unbound_configure(false);
563
		} else {
564
			/* kill any running Unbound instance */
565
			if (isvalidpid("{$g['varrun_path']}/unbound.pid")) {
566
				sigkillbypid("{$g['varrun_path']}/unbound.pid",
567
				    "TERM");
568
			}
569
		}
570

    
571
		/*
572
		 * Call this separately since the above are manually set to
573
		 * skip the DHCP restart they normally perform.
574
		 * This avoids restarting dhcpd twice as described on
575
		 * ticket #3797
576
		 */
577
		services_dhcpd_configure();
578

    
579
		if ($reset_accounts) {
580
			local_reset_accounts();
581
		}
582

    
583
		return true;
584
	}
585

    
586
	/**
587
	 * Wrapper for configuring CARP interfaces
588
	 *
589
	 * @return bool
590
	 */
591
	public function interfaces_carp_configure() {
592
		$this->auth();
593

    
594
		if ($this->loop_detected) {
595
			log_error("Disallowing CARP sync loop");
596
			return true;
597
		}
598

    
599
		interfaces_vips_configure();
600

    
601
		return true;
602
	}
603

    
604
	/**
605
	 * Wrapper for rc.reboot
606
	 *
607
	 * @return bool
608
	 */
609
	public function reboot() {
610
		$this->auth();
611

    
612
		mwexec_bg("/etc/rc.reboot");
613

    
614
		return true;
615
	}
616
}
617

    
618
// run script untill its done and can 'unlock' the xmlrpc.lock, this prevents hanging php-fpm / webgui 
619
ignore_user_abort(true); 
620
set_time_limit(0);
621

    
622
$xmlrpclockkey = lock('xmlrpc', LOCK_EX);
623

    
624
XML_RPC2_Backend::setBackend('php');
625
$HTTP_RAW_POST_DATA = file_get_contents('php://input');
626

    
627
$options = array(
628
	'prefix' => 'pfsense.',
629
	'encoding' => 'utf-8',
630
	'autoDocument' => false,
631
);
632

    
633
$server = XML_RPC2_Server::create(new pfsense_xmlrpc_server(), $options);
634
$server->handleCall();
635

    
636
unlock($xmlrpclockkey);
637

    
638
?>
(232-232/232)