Project

General

Profile

Download (17.9 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
		/* Only touch users if users are set to synchronize from the primary node
231
		 * See https://redmine.pfsense.org/issues/8450
232
		 */
233
		if ($sections['system']['user'] && $sections['system']['group']) {
234
			$g2add = array();
235
			$g2del = array();
236
			$g2del_idx = array();
237
			$g2keep = array();
238
			if (is_array($sections['system']['group'])) {
239
				$local_groups = isset($config['system']['group'])
240
				    ? $config['system']['group']
241
				    : array();
242

    
243
				foreach ($sections['system']['group'] as $group) {
244
					$idx = array_search($group['name'],
245
					    array_column($local_groups, 'name'));
246

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

    
271
			$u2add = array();
272
			$u2del = array();
273
			$u2del_idx = array();
274
			$u2keep = array();
275
			if (is_array($sections['system']['user'])) {
276
				$local_users = isset($config['system']['user'])
277
				    ? $config['system']['user']
278
				    : array();
279

    
280
				foreach ($sections['system']['user'] as $user) {
281
					$idx = array_search($user['name'],
282
					    array_column($local_users, 'name'));
283

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

    
309
		$voucher = array();
310
		if (is_array($sections['voucher'])) {
311
			/* Save voucher rolls to process after merge */
312
			$voucher = $sections['voucher'];
313

    
314
			foreach($sections['voucher'] as $zone => $item) {
315
				unset($sections['voucher'][$zone]['roll']);
316
				if (isset($config['voucher'][$zone]['vouchersyncdbip'])) {
317
					$sections['voucher'][$zone]['vouchersyncdbip'] =
318
					    $config['voucher'][$zone]['vouchersyncdbip'];
319
				} else {
320
					unset($sections['voucher'][$zone]['vouchersyncdbip']);
321
				}
322
				if (isset($config['voucher'][$zone]['vouchersyncport'])) {
323
					$sections['voucher'][$zone]['vouchersyncport'] =
324
					    $config['voucher'][$zone]['vouchersyncport'];
325
				} else {
326
					unset($sections['voucher'][$zone]['vouchersyncport']);
327
				}
328
				if (isset($config['voucher'][$zone]['vouchersyncusername'])) {
329
					$sections['voucher'][$zone]['vouchersyncusername'] =
330
					    $config['voucher'][$zone]['vouchersyncusername'];
331
				} else {
332
					unset($sections['voucher'][$zone]['vouchersyncusername']);
333
				}
334
				if (isset($config['voucher'][$zone]['vouchersyncpass'])) {
335
					$sections['voucher'][$zone]['vouchersyncpass'] =
336
					    $config['voucher'][$zone]['vouchersyncpass'];
337
				} else {
338
					unset($sections['voucher'][$zone]['vouchersyncpass']);
339
				}
340
			}
341
		}
342

    
343
		$vipbackup = array();
344
		$oldvips = array();
345
		if (isset($sections['virtualip']) &&
346
		    is_array($config['virtualip']['vip'])) {
347
			foreach ($config['virtualip']['vip'] as $vip) {
348
				if ($vip['mode'] == "carp") {
349
					$key = $vip['interface'] .
350
					    "_vip" . $vip['vhid'];
351

    
352
					$oldvips[$key]['content'] =
353
					    $vip['password'] .
354
					    $vip['advskew'] .
355
					    $vip['subnet'] .
356
					    $vip['subnet_bits'] .
357
					    $vip['advbase'];
358
					$oldvips[$key]['interface'] =
359
					    $vip['interface'];
360
					$oldvips[$key]['subnet'] =
361
					    $vip['subnet'];
362
				} else if ($vip['mode'] == "ipalias" &&
363
				    (substr($vip['interface'], 0, 4) == '_vip'
364
				    || strstr($vip['interface'], "lo0"))) {
365
					$oldvips[$vip['subnet']]['content'] =
366
					    $vip['interface'] .
367
					    $vip['subnet'] .
368
					    $vip['subnet_bits'];
369
					$oldvips[$vip['subnet']]['interface'] =
370
					    $vip['interface'];
371
					$oldvips[$vip['subnet']]['subnet'] =
372
					    $vip['subnet'];
373
				} else if (($vip['mode'] == "ipalias" ||
374
				    $vip['mode'] == 'proxyarp') &&
375
				    !(substr($vip['interface'], 0, 4) == '_vip')
376
				    || strstr($vip['interface'], "lo0")) {
377
					$vipbackup[] = $vip;
378
				}
379
			}
380
		}
381

    
382
		/* For vip section, first keep items sent from the master */
383
		$config = array_merge_recursive_unique($config, $sections);
384

    
385
		/* Remove locally items removed remote */
386
		foreach ($voucher as $zone => $item) {
387
			/* Zone was deleted on master, delete its vouchers */
388
			if (!isset($config['captiveportal'][$zone])) {
389
				unset($config['voucher'][$zone]);
390
				continue;
391
			}
392
			/* No rolls on master, delete local ones */
393
			if (!is_array($item['roll'])) {
394
				unset($config['voucher'][$zone]['roll']);
395
				continue;
396
			}
397
		}
398

    
399
		$l_rolls = array();
400
		if (is_array($config['voucher'])) {
401
			foreach ($config['voucher'] as $zone => $item) {
402
				if (!is_array($item['roll'])) {
403
					continue;
404
				}
405
				foreach ($item['roll'] as $idx => $roll) {
406
					/* Make it easy to find roll by # */
407
					$l_rolls[$zone][$roll['number']] = $idx;
408
				}
409
			}
410
		}
411

    
412
		/*
413
		 * Process vouchers sent by primary node and:
414
		 * - Add new items
415
		 * - Update existing items based on 'lastsync' field
416
		 */
417
		foreach ($voucher as $zone => $item) {
418
			if (!is_array($item['roll'])) {
419
				continue;
420
			}
421
			foreach ($item['roll'] as $idx => $roll) {
422
				if (!isset($l_rolls[$zone][$roll['number']])) {
423
					$config['voucher'][$zone]['roll'][] =
424
					    $roll;
425
					continue;
426
				}
427
				$l_roll_idx = $l_rolls[$zone][$roll['number']];
428
				$l_vouchers = &$config['voucher'][$zone];
429
				$l_roll = $l_vouchers['roll'][$l_roll_idx];
430
				if (!isset($l_roll['lastsync'])) {
431
					$l_roll['lastsync'] = 0;
432
				}
433

    
434
				if (isset($roll['lastsync']) &&
435
				    $roll['lastsync'] != $l_roll['lastsync']) {
436
					$l_vouchers['roll'][$l_roll_idx] =
437
					    $roll;
438
					unset($l_rolls[$zone][$roll['number']]);
439
				}
440
			}
441
		}
442

    
443
		/*
444
		 * At this point $l_rolls contains only items that are not
445
		 * present on primary node. They must be removed
446
		 */
447
		foreach ($l_rolls as $zone => $item) {
448
			foreach ($item as $number => $idx) {
449
				unset($config['voucher'][$zone][$idx]);
450
			}
451
		}
452

    
453
		/*
454
		 * Then add ipalias and proxyarp types already defined
455
		 * on the backup
456
		 */
457
		if (is_array($vipbackup) && !empty($vipbackup)) {
458
			if (!is_array($config['virtualip'])) {
459
				$config['virtualip'] = array();
460
			}
461
			if (!is_array($config['virtualip']['vip'])) {
462
				$config['virtualip']['vip'] = array();
463
			}
464
			foreach ($vipbackup as $vip) {
465
				array_unshift($config['virtualip']['vip'], $vip);
466
			}
467
		}
468

    
469
		/* Log what happened */
470
		$mergedkeys = implode(", ", array_merge(array_keys($sections),
471
		    $syncd_full_sections));
472
		write_config(sprintf(gettext(
473
		    "Merged in config (%s sections) from XMLRPC client."),
474
		    $mergedkeys));
475

    
476
		/*
477
		 * The real work on handling the vips specially
478
		 * This is a copy of intefaces_vips_configure with addition of
479
		 * not reloading existing/not changed carps
480
		 */
481
		if (isset($sections['virtualip']) &&
482
		    is_array($config['virtualip']) &&
483
		    is_array($config['virtualip']['vip'])) {
484
			$carp_setuped = false;
485
			$anyproxyarp = false;
486

    
487
			foreach ($config['virtualip']['vip'] as $vip) {
488
				$key = "{$vip['interface']}_vip{$vip['vhid']}";
489

    
490
				if ($vip['mode'] == "carp" &&
491
				    isset($oldvips[$key])) {
492
					if ($oldvips[$key]['content'] ==
493
					    $vip['password'] .
494
					    $vip['advskew'] .
495
					    $vip['subnet'] .
496
					    $vip['subnet_bits'] .
497
					    $vip['advbase'] &&
498
					    does_vip_exist($vip)) {
499
						unset($oldvips[$key]);
500
						/*
501
						 * Skip reconfiguring this vips
502
						 * since nothing has changed.
503
						 */
504
						continue;
505
					}
506

    
507
				} elseif ($vip['mode'] == "ipalias" &&
508
				    (substr($vip['interface'], 0, 4) == '_vip'
509
				    || strstr($vip['interface'], "lo0")) &&
510
				    isset($oldvips[$vip['subnet']])) {
511
					$key = $vip['subnet'];
512
					if ($oldvips[$key]['content'] ==
513
					    $vip['interface'] .
514
					    $vip['subnet'] .
515
					    $vip['subnet_bits'] &&
516
					    does_vip_exist($vip)) {
517
						unset($oldvips[$key]);
518
						/*
519
						 * Skip reconfiguring this vips
520
						 * since nothing has changed.
521
						 */
522
						continue;
523
					}
524
					unset($oldvips[$key]);
525
				}
526

    
527
				switch ($vip['mode']) {
528
				case "proxyarp":
529
					$anyproxyarp = true;
530
					break;
531
				case "ipalias":
532
					interface_ipalias_configure($vip);
533
					break;
534
				case "carp":
535
					$carp_setuped = true;
536
					interface_carp_configure($vip);
537
					break;
538
				}
539
			}
540

    
541
			/* Cleanup remaining old carps */
542
			foreach ($oldvips as $oldvipar) {
543
				$oldvipif = get_real_interface(
544
				    $oldvipar['interface']);
545

    
546
				if (empty($oldvipif)) {
547
					continue;
548
				}
549

    
550
				if (is_ipaddrv6($oldvipar['subnet'])) {
551
					 mwexec("/sbin/ifconfig " .
552
					     escapeshellarg($oldvipif) .
553
					     " inet6 " .
554
					     escapeshellarg($oldvipar['subnet']) .
555
					     " delete");
556
				} else {
557
					pfSense_interface_deladdress($oldvipif,
558
					    $oldvipar['subnet']);
559
				}
560
			}
561
			if ($carp_setuped == true) {
562
				interfaces_sync_setup();
563
			}
564
			if ($anyproxyarp == true) {
565
				interface_proxyarp_configure();
566
			}
567
		}
568

    
569
		if ($old_ipsec_enabled !== ipsec_enabled()) {
570
			vpn_ipsec_configure();
571
		}
572

    
573
		unset($old_config);
574

    
575
		local_sync_accounts($u2add, $u2del, $g2add, $g2del);
576
		$this->filter_configure(false);
577

    
578
		return true;
579
	}
580

    
581
	/**
582
	 * Merge items into installedpackages config section
583
	 *
584
	 * @param array $section
585
	 *
586
	 * @return bool
587
	 */
588
	public function merge_installedpackages_section($section) {
589
		$this->auth();
590

    
591
		global $config;
592

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

    
598
		$config['installedpackages'] = array_merge(
599
		    $config['installedpackages'], $section);
600
		$mergedkeys = implode(", ", array_keys($section));
601
		write_config(sprintf(gettext(
602
		    "Merged in config (%s sections) from XMLRPC client."),
603
		    $mergedkeys));
604

    
605
		return true;
606
	}
607

    
608
	/**
609
	 * Merge items into config
610
	 *
611
	 * @param array $section
612
	 *
613
	 * @return bool
614
	 */
615
	public function merge_config_section($section) {
616
		$this->auth();
617

    
618
		global $config;
619

    
620
		if ($this->loop_detected) {
621
			log_error("Disallowing CARP sync loop");
622
			return true;
623
		}
624

    
625
		$config_new = $this->array_overlay($config, $section);
626
		$config = $config_new;
627
		$mergedkeys = implode(", ", array_keys($section));
628
		write_config(sprintf(gettext(
629
		    "Merged in config (%s sections) from XMLRPC client."),
630
		    $mergedkeys));
631

    
632
		return true;
633
	}
634

    
635
	/**
636
	 * Wrapper for filter_configure()
637
	 *
638
	 * @return bool
639
	 */
640
	public function filter_configure($reset_accounts = true) {
641
		$this->auth();
642

    
643
		global $g, $config;
644

    
645
		filter_configure();
646
		system_routing_configure();
647
		setup_gateways_monitor();
648
		relayd_configure();
649
		require_once("openvpn.inc");
650
		openvpn_resync_all();
651

    
652
		/*
653
		 * The DNS Resolver and the DNS Forwarder may both be active so
654
		 * long as * they are running on different ports.
655
		 * See ticket #5882
656
		 */
657
		if (isset($config['dnsmasq']['enable'])) {
658
			/* Configure dnsmasq but tell it NOT to restart DHCP */
659
			services_dnsmasq_configure(false);
660
		} else {
661
			/* kill any running dnsmasq instance */
662
			if (isvalidpid("{$g['varrun_path']}/dnsmasq.pid")) {
663
				sigkillbypid("{$g['varrun_path']}/dnsmasq.pid",
664
				    "TERM");
665
			}
666
		}
667
		if (isset($config['unbound']['enable'])) {
668
			/* Configure unbound but tell it NOT to restart DHCP */
669
			services_unbound_configure(false);
670
		} else {
671
			/* kill any running Unbound instance */
672
			if (isvalidpid("{$g['varrun_path']}/unbound.pid")) {
673
				sigkillbypid("{$g['varrun_path']}/unbound.pid",
674
				    "TERM");
675
			}
676
		}
677

    
678
		/*
679
		 * Call this separately since the above are manually set to
680
		 * skip the DHCP restart they normally perform.
681
		 * This avoids restarting dhcpd twice as described on
682
		 * ticket #3797
683
		 */
684
		services_dhcpd_configure();
685

    
686
		if ($reset_accounts) {
687
			local_reset_accounts();
688
		}
689

    
690
		return true;
691
	}
692

    
693
	/**
694
	 * Wrapper for configuring CARP interfaces
695
	 *
696
	 * @return bool
697
	 */
698
	public function interfaces_carp_configure() {
699
		$this->auth();
700

    
701
		if ($this->loop_detected) {
702
			log_error("Disallowing CARP sync loop");
703
			return true;
704
		}
705

    
706
		interfaces_vips_configure();
707

    
708
		return true;
709
	}
710

    
711
	/**
712
	 * Wrapper for rc.reboot
713
	 *
714
	 * @return bool
715
	 */
716
	public function reboot() {
717
		$this->auth();
718

    
719
		mwexec_bg("/etc/rc.reboot");
720

    
721
		return true;
722
	}
723
}
724

    
725
// run script untill its done and can 'unlock' the xmlrpc.lock, this prevents hanging php-fpm / webgui
726
ignore_user_abort(true);
727
set_time_limit(0);
728

    
729
$xmlrpclockkey = lock('xmlrpc', LOCK_EX);
730

    
731
XML_RPC2_Backend::setBackend('php');
732
$HTTP_RAW_POST_DATA = file_get_contents('php://input');
733

    
734
$options = array(
735
	'prefix' => 'pfsense.',
736
	'encoding' => 'utf-8',
737
	'autoDocument' => false,
738
);
739

    
740
$server = XML_RPC2_Server::create(new pfsense_xmlrpc_server(), $options);
741
$server->handleCall();
742

    
743
unlock($xmlrpclockkey);
744

    
745
?>
(234-234/234)