Project

General

Profile

Download (30 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
 * xmlrpc.php
4
 *
5
 * part of pfSense (https://www.pfsense.org)
6
 * Copyright (c) 2004-2013 BSD Perimeter
7
 * Copyright (c) 2013-2016 Electric Sheep Fencing
8
 * Copyright (c) 2014-2024 Rubicon Communications, LLC (Netgate)
9
 * Copyright (c) 2005 Colin Smith
10
 * All rights reserved.
11
 *
12
 * Licensed under the Apache License, Version 2.0 (the "License");
13
 * you may not use this file except in compliance with the License.
14
 * You may obtain a copy of the License at
15
 *
16
 * http://www.apache.org/licenses/LICENSE-2.0
17
 *
18
 * Unless required by applicable law or agreed to in writing, software
19
 * distributed under the License is distributed on an "AS IS" BASIS,
20
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21
 * See the License for the specific language governing permissions and
22
 * limitations under the License.
23
 */
24

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

    
32
require_once("config.inc");
33
require_once("functions.inc");
34
require_once("auth.inc");
35
require_once("filter.inc");
36
require_once("ipsec.inc");
37
require_once("vpn.inc");
38
require_once("openvpn.inc");
39
require_once("captiveportal.inc");
40
require_once("shaper.inc");
41
require_once("XML/RPC2/Server.php");
42

    
43
class pfsense_xmlrpc_server {
44

    
45
	private $loop_detected = false;
46
	private $remote_addr;
47

    
48
	private function auth() {
49
		global $userindex;
50
		$userindex = index_users();
51

    
52
		$username = $_SERVER['PHP_AUTH_USER'];
53
		$password = $_SERVER['PHP_AUTH_PW'];
54

    
55
		$login_ok = false;
56
		if (!empty($username) && !empty($password)) {
57
			$attributes = array();
58
			$authcfg = auth_get_authserver(config_get_path('system/webgui/authmode'));
59

    
60
			if (authenticate_user($username, $password,
61
			    $authcfg, $attributes) ||
62
			    authenticate_user($username, $password)) {
63
				$login_ok = true;
64
			}
65
		}
66

    
67
		if (!$login_ok) {
68
			log_auth(sprintf(gettext("webConfigurator authentication error for user '%1\$s' from: %2\$s"),
69
			    $username,
70
			    $this->remote_addr));
71

    
72
			require_once("XML/RPC2/Exception.php");
73
			throw new XML_RPC2_FaultException(gettext(
74
			    'Authentication failed: Invalid username or password'),
75
			    -1);
76
		}
77

    
78
		$user_entry = getUserEntry($username);
79
		$user_entry = $user_entry['item'];
80
		/*
81
		 * admin (uid = 0) is allowed
82
		 * or regular user with necessary privilege
83
		 */
84
		if (isset($user_entry['uid']) && $user_entry['uid'] != '0' &&
85
		    !userHasPrivilege($user_entry, 'system-xmlrpc-ha-sync')) {
86
			log_auth("webConfigurator authentication error for '" .
87
			    $username . "' from " . $this->remote_addr .
88
			    " not enough privileges");
89

    
90
			require_once("XML/RPC2/Exception.php");
91
			throw new XML_RPC2_FaultException(gettext(
92
			    'Authentication failed: not enough privileges'),
93
			    -2);
94
		}
95

    
96
		return;
97
	}
98

    
99
	private function array_overlay($a1, $a2) {
100
		foreach ($a1 as $k => $v) {
101
			if (!array_key_exists($k, $a2)) {
102
				continue;
103
			}
104
			if (is_array($v) && is_array($a2[$k])) {
105
				$a1[$k] = $this->array_overlay($v, $a2[$k]);
106
			} else {
107
				$a1[$k] = $a2[$k];
108
			}
109
		}
110

    
111
		return $a1;
112
	}
113

    
114
	public function __construct() {
115
		$this->remote_addr = $_SERVER['REMOTE_ADDR'];
116

    
117
		/* grab sync to ip if enabled */
118
		if ((config_get_path('hasync/synchronizetoip') !== null) &&
119
		    config_get_path('hasync/synchronizetoip') == $this->remote_addr) {
120
			$this->loop_detected = true;
121
		}
122
	}
123

    
124
	/**
125
	 * Get host version information
126
	 *
127
	 * @return array
128
	 */
129
	public function host_firmware_version($dummy, $timeout) {
130
		ini_set('default_socket_timeout', $timeout);
131
		$this->auth();
132
		return host_firmware_version();
133
	}
134

    
135
	/**
136
	 * Executes a PHP block of code
137
	 *
138
	 * @param string $code
139
	 *
140
	 * @return bool
141
	 */
142
	public function exec_php($code) {
143
		$this->auth();
144

    
145
		eval($code);
146
		if ($toreturn) {
147
			return $toreturn;
148
		}
149

    
150
		return true;
151
	}
152

    
153
	/**
154
	 * Executes shell commands
155
	 *
156
	 * @param string $code
157
	 *
158
	 * @return bool
159
	 */
160
	public function exec_shell($code) {
161
		$this->auth();
162

    
163
		mwexec($code);
164
		return true;
165
	}
166

    
167
	/**
168
	 * Backup chosen config sections
169
	 *
170
	 * @param array $section
171
	 *
172
	 * @return array
173
	 */
174
	public function backup_config_section($section) {
175
		$this->auth();
176
		return array_intersect_key(config_get_path(''), array_flip($section));
177
	}
178

    
179
	/**
180
	 * Restore defined config section into local config
181
	 *
182
	 * @param array $sections
183
	 *
184
	 * @return bool
185
	 */
186
	public function restore_config_section($sections, $timeout) {
187
		ini_set('default_socket_timeout', $timeout);
188
		$this->auth();
189

    
190
		global $cpzone, $cpzoneid, $old_config;
191

    
192
		$old_config = config_get_path('');
193

    
194
		if ($this->loop_detected) {
195
			log_error("Disallowing CARP sync loop");
196
			return true;
197
		}
198

    
199
		/*
200
		 * Some sections should just be copied and not merged or we end
201
		 * up unable to sync the deletion of the last item in a section
202
		 */
203
		$sync_full_sections = array(
204
			'aliases',
205
			'ca',
206
			'cert',
207
			'crl',
208
			'dhcpd',
209
			'dhcrelay',
210
			'dhcrelay6',
211
			'dnshaper',
212
			'dnsmasq',
213
			'filter',
214
			'ipsec',
215
			'nat',
216
			'openvpn',
217
			'schedules',
218
			'shaper',
219
			'unbound',
220
			'wol',
221
		);
222

    
223
		$syncd_full_sections = array();
224

    
225
		foreach ($sync_full_sections as $section) {
226
			/* Do not test for empty here or removing final entry
227
			 * from a section will not work */
228
			if (!array_key_exists($section, $sections)) {
229
				continue;
230
			}
231

    
232
			config_set_path($section, array_get_path($sections, $section));
233
			array_del_path($sections, $section);
234
			$syncd_full_sections[] = $section;
235
		}
236

    
237
		/* If captive portal sync is enabled on primary node, remove local CP on the secondary */
238
		if (is_array($sections['captiveportal'])) {
239
			foreach (config_get_path('captiveportal', []) as $zone => $item) {
240
				if (!isset($sections['captiveportal'][$zone])) {
241
					$cpzone = $zone;
242
					config_del_path("captiveportal/{$cpzone}/enable");
243
					captiveportal_configure_zone(config_get_path("captiveportal/{$cpzone}", []));
244
					config_del_path("captiveportal/{$cpzone}");
245
					config_del_path("voucher/{$cpzone}");
246
					unlink_if_exists("/var/db/captiveportal{$cpzone}.db");
247
					unlink_if_exists("/var/db/captiveportal_usedmacs_{$cpzone}.db");
248
					unlink_if_exists("/var/db/voucher_{$cpzone}_*.db");
249
				}
250
			}
251
		}
252

    
253
		$group_config = config_get_path('system/group', []);
254
		/* Only touch users if users are set to synchronize from the primary node
255
		 * See https://redmine.pfsense.org/issues/8450
256
		 */
257
		if ($sections['system']['user'] && $sections['system']['group']) {
258
			$g2add = array();
259
			$g2del = array();
260
			$g2del_idx = array();
261
			$g2keep = array();
262
			if (is_array($sections['system']['group'])) {
263
				$local_groups = $group_config;
264

    
265
				foreach ($sections['system']['group'] as $group) {
266
					$idx = array_search($group['name'],
267
					    array_column($local_groups, 'name'));
268

    
269
					if ($idx === false) {
270
						// section config group not found in local config
271
						$g2add[] = $group;
272
					} else if ($group['gid'] < 2000) {
273
						// section config group found in local config and is a special group
274
						$g2keep[] = $idx;
275
					} else if ($group != $local_groups[$idx]) {
276
						// section config group found in local config with different settings
277
						$g2add[] = $group;
278
						$g2del[] = $group;
279
						$g2del_idx[] = $idx;
280
					} else {
281
						// section config group found in local config and its settings are synced
282
						$g2keep[] = $idx;
283
					}
284
				}
285
			}
286
			if (is_array($group_config)) {
287
				foreach ($group_config as $idx => $group) {
288
					if (array_search($idx, $g2keep) === false &&
289
					    array_search($idx, $g2del_idx) === false) {
290
						// local config group not in section config group
291
						$g2del[] = $group;
292
						$g2del_idx[] = $idx;
293
					}
294
				}
295
			}
296
			unset($sections['system']['group'], $g2keep, $g2del_idx);
297

    
298
			$u2add = array();
299
			$u2del = array();
300
			$u2del_idx = array();
301
			$u2keep = array();
302
			$user_config = config_get_path('system/user', []);
303
			if (is_array($sections['system']['user'])) {
304
				$local_users = $user_config;
305

    
306
				foreach ($sections['system']['user'] as $user) {
307
					$idx = array_search($user['name'],
308
					    array_column($local_users, 'name'));
309

    
310
					if ($idx === false) {
311
						$u2add[] = $user;
312
					} else if (($user['uid'] < 2000) && ($sections['hasync']['adminsync'] != 'on')) {
313
						$u2keep[] = $idx;
314
					} else if ($user != $local_users[$idx]) {
315
						$u2add[] = $user;
316
						$u2del[] = $user;
317
						$u2del_idx[] = $idx;
318
					} else {
319
						$u2keep[] = $idx;
320
					}
321
				}
322
			}
323
			if (is_array($user_config)) {
324
				foreach ($user_config as $idx => $user) {
325
					if (array_search($idx, $u2keep) === false &&
326
					    array_search($idx, $u2del_idx) === false) {
327
						$u2del[] = $user;
328
						$u2del_idx[] = $idx;
329
					}
330
				}
331
			}
332
			unset($sections['system']['user'], $u2keep, $u2del_idx);
333
		}
334

    
335
		$voucher = array();
336
		if (is_array($sections['voucher'])) {
337
			/* Save voucher rolls to process after merge */
338
			$voucher = $sections['voucher'];
339

    
340
			foreach($sections['voucher'] as $zone => $item) {
341
				unset($sections['voucher'][$zone]['roll']);
342
				// Note : This code can be safely deleted once #97 fix has been applied and deployed to pfSense stable release.
343
				// Please do not delete this code before
344
				if (config_get_path("voucher/{$zone}/vouchersyncdbip") !== null) {
345
					$sections['voucher'][$zone]['vouchersyncdbip'] =
346
					    config_get_path("voucher/{$zone}/vouchersyncdbip");
347
				} else {
348
					unset($sections['voucher'][$zone]['vouchersyncdbip']);
349
				}
350
				if (config_get_path("voucher/{$zone}/vouchersyncusername") !== null) {
351
					$sections['voucher'][$zone]['vouchersyncusername'] =
352
					    config_get_path("voucher/{$zone}/vouchersyncusername");
353
				} else {
354
					unset($sections['voucher'][$zone]['vouchersyncusername']);
355
				}
356
				if (config_get_path("voucher/{$zone}/vouchersyncpass") !== null) {
357
					$sections['voucher'][$zone]['vouchersyncpass'] =
358
					    config_get_path("voucher/{$zone}/vouchersyncpass");
359
				} else {
360
					unset($sections['voucher'][$zone]['vouchersyncpass']);
361
				}
362
				// End note.
363
			}
364
		}
365

    
366
		if (is_array($sections['captiveportal'])) {
367
			// Captiveportal : Backward HA settings should remain local.
368
			foreach ($sections['captiveportal'] as $zone => $cp) {
369
				if (config_path_enabled("captiveportal/{$zone}", "enablebackwardsync")) {
370
					$sections['captiveportal'][$zone]['enablebackwardsync'] = config_get_path("captiveportal/{$zone}/enablebackwardsync");
371
				} else {
372
					unset($sections['captiveportal'][$zone]['enablebackwardsync']);
373
				}
374
				if (config_get_path("captiveportal/{$zone}/backwardsyncip") !== null) {
375
					$sections['captiveportal'][$zone]['backwardsyncip'] = config_get_path("captiveportal/{$zone}/backwardsyncip");
376
				} else {
377
					unset($sections['captiveportal'][$zone]['backwardsyncip']);
378
				}
379
				if (config_get_path("captiveportal/{$zone}/backwardsyncuser") !== null) {
380
					$sections['captiveportal'][$zone]['backwardsyncuser'] = config_get_path("captiveportal/{$zone}/backwardsyncuser");
381
				} else {
382
					unset($sections['captiveportal'][$zone]['backwardsyncuser']);
383
				}
384
				if (config_get_path("captiveportal/{$zone}/backwardsyncpassword") !== null) {
385
					$sections['captiveportal'][$zone]['backwardsyncpassword'] = config_get_path("captiveportal/{$zone}/backwardsyncpassword");
386
				} else {
387
					unset($sections['captiveportal'][$zone]['vouchersyncpass']);
388
				}
389
			}
390
			config_set_path('captiveportal', $sections['captiveportal']);
391
			unset($sections['captiveportal']);
392
		}
393

    
394
		$vipbackup = array();
395
		$oldvips = array();
396
		if (array_key_exists('virtualip', $sections)) {
397
			foreach (config_get_path('virtualip/vip', []) as $vip) {
398
				if (empty($vip)) {
399
					continue;
400
				}
401
				if ($vip['mode'] == "carp") {
402
					$key = $vip['interface'] .
403
					    "_vip" . $vip['vhid'];
404

    
405
					$oldvips[$key]['content'] =
406
					    $vip['password'] .
407
					    $vip['advskew'] .
408
					    $vip['subnet'] .
409
					    $vip['subnet_bits'] .
410
					    $vip['advbase'];
411
					$oldvips[$key]['interface'] =
412
					    $vip['interface'];
413
					$oldvips[$key]['subnet'] =
414
					    $vip['subnet'];
415
				} else if ($vip['mode'] == "ipalias" &&
416
				    (substr($vip['interface'], 0, 4) == '_vip'
417
				    || strstr($vip['interface'], "lo0"))) {
418
					$oldvips[$vip['subnet']]['content'] =
419
					    $vip['interface'] .
420
					    $vip['subnet'] .
421
					    $vip['subnet_bits'];
422
					$oldvips[$vip['subnet']]['interface'] =
423
					    $vip['interface'];
424
					$oldvips[$vip['subnet']]['subnet'] =
425
					    $vip['subnet'];
426
				} else if (($vip['mode'] == "ipalias" ||
427
				    $vip['mode'] == 'proxyarp') &&
428
				    !(substr($vip['interface'], 0, 4) == '_vip')
429
				    || strstr($vip['interface'], "lo0")) {
430
					$vipbackup[] = $vip;
431
				}
432
			}
433
		}
434

    
435
		/* Extract and save any package sections before merging other sections */
436
		$pkg_sections = ['installedpackages' => array_get_path($sections, 'installedpackages', [])];
437
		array_del_path($sections, 'installedpackages');
438

    
439
		/* For vip section, first keep items sent from the master */
440
		config_set_path('', array_merge_recursive_unique(config_get_path(''), $sections));
441

    
442
		/* Special handling for Kea HA, skip receiving certain settings from master */
443
		foreach (['kea', 'kea6'] as $kea) {
444
			if (array_path_enabled($old_config, $kea.'/ha', 'tls')) {
445
				config_set_path($kea.'/ha/tls', true);
446
				if ($value = array_get_path($old_config, $kea.'/ha/scertref')) {
447
					config_set_path($kea.'/ha/scertref', $value);
448
				} else {
449
					config_del_path($kea.'/ha/scertref');
450
				}
451
				if (array_path_enabled($old_config, $kea.'/ha', 'mutualtls')) {
452
					config_set_path($kea.'/ha/mutualtls', true);
453
					if ($value = array_get_path($old_config, $kea.'/ha/ccertref')) {
454
						config_set_path($kea.'/ha/ccertref', $value);
455
					} else {
456
						config_del_path($kea.'/ha/ccertref');
457
					}
458
				}
459
			} else {
460
				config_del_path($kea.'/ha/tls');
461
				config_del_path($kea.'/ha/scertref');
462
				config_del_path($kea.'/ha/mutualtls');
463
				config_del_path($kea.'/ha/ccertref');
464
			}
465

    
466
			if ($value = array_get_path($old_config, $kea.'/ha/localname')) {
467
				config_set_path($kea.'/ha/localname', $value);
468
			} else {
469
				config_del_path($kea.'/ha/localname');
470
			}
471
		}
472

    
473
		/* Remove locally items removed remote */
474
		foreach ($voucher as $zone => $item) {
475
			/* No rolls on master, delete local ones */
476
			if (!is_array($item['roll'])) {
477
				config_del_path("voucher/{$zone}/roll");
478
			}
479
		}
480

    
481
		$l_rolls = array();
482
		if (is_array(config_get_path('voucher'))) {
483
			foreach (config_get_path('voucher', []) as $zone => $item) {
484
				if (!is_array($item['roll'])) {
485
					continue;
486
				}
487
				foreach ($item['roll'] as $idx => $roll) {
488
					/* Make it easy to find roll by # */
489
					$l_rolls[$zone][$roll['number']] = $idx;
490
				}
491
			}
492
		}
493

    
494
		/*
495
		 * Process vouchers sent by primary node and:
496
		 * - Add new items
497
		 * - Update existing items based on 'lastsync' field
498
		 */
499
		foreach ($voucher as $zone => $item) {
500
			if (!is_array($item['roll'])) {
501
				continue;
502
			}
503
			$l_vouchers = config_get_path("voucher/{$zone}", []);
504
			foreach ($item['roll'] as $roll) {
505
				if (!isset($l_rolls[$zone][$roll['number']])) {
506
					$l_vouchers['roll'][] = $roll;
507
					continue;
508
				}
509
				$l_roll_idx = $l_rolls[$zone][$roll['number']];
510
				$l_roll = $l_vouchers['roll'][$l_roll_idx];
511
				if (!isset($l_roll['lastsync'])) {
512
					$l_roll['lastsync'] = 0;
513
				}
514

    
515
				if (isset($roll['lastsync']) &&
516
				    $roll['lastsync'] != $l_roll['lastsync']) {
517
					$l_vouchers['roll'][$l_roll_idx] =
518
					    $roll;
519
					unset($l_rolls[$zone][$roll['number']]);
520
				}
521
			}
522
			config_set_path("voucher/{$zone}", $l_vouchers);
523
		}
524

    
525
		/*
526
		 * At this point $l_rolls contains only items that are not
527
		 * present on primary node. They must be removed
528
		 */
529
		foreach ($l_rolls as $zone => $item) {
530
			foreach ($item as $idx) {
531
				config_del_path("voucher/{$zone}/{$idx}");
532
			}
533
		}
534

    
535
		/*
536
		 * Then add ipalias and proxyarp types already defined
537
		 * on the backup
538
		 */
539
		if (is_array($vipbackup) && !empty($vipbackup)) {
540
			$vips = config_get_path('virtualip/vip', []);
541
			foreach ($vipbackup as $vip) {
542
				array_unshift($vips, $vip);
543
			}
544
			config_set_path('virtualip/vip', $vips);
545
		}
546

    
547
		/* xmlrpc_recv plugin expects path => value pairs of changed nodes, not an associative tree */
548
		$pkg_merged_paths = pkg_call_plugins("plugin_xmlrpc_recv", $pkg_sections);
549
		foreach ($pkg_merged_paths as $pkg => $sections) {
550
			if (!is_array($sections)) {
551
				log_error('Package {$pkg} xmlrpc_recv plugin returned invalid value.');
552
				continue;
553
			}
554
			foreach ($sections as $path => $section) {
555
				if (is_null(config_set_path($path, $section))) {
556
					log_error('Could not write section {$path} supplied by package {$pkg} xmlrpc_recv plugin');
557
				}
558
			}
559
		}
560

    
561
		/* Log what happened */
562
		$mergedkeys = implode(", ", array_merge(array_keys($sections),
563
		    $syncd_full_sections));
564
		write_config(sprintf(gettext(
565
		    "Merged in config (%s sections) from XMLRPC client."),
566
		    $mergedkeys));
567

    
568
		/*
569
		 * The real work on handling the vips specially
570
		 * This is a copy of interfaces_vips_configure with addition of
571
		 * not reloading existing/not changed carps
572
		 */
573
		$force_filterconfigure = false;
574
		if (isset($sections['virtualip']) &&
575
		    is_array(config_get_path('virtualip/vip'))) {
576
			$carp_setuped = false;
577
			$anyproxyarp = false;
578

    
579
			foreach (config_get_path('virtualip/vip', []) as $vip) {
580
				$key = "{$vip['interface']}_vip{$vip['vhid']}";
581

    
582
				if ($vip['mode'] == "carp" &&
583
				    isset($oldvips[$key])) {
584
					if ($oldvips[$key]['content'] ==
585
					    $vip['password'] .
586
					    $vip['advskew'] .
587
					    $vip['subnet'] .
588
					    $vip['subnet_bits'] .
589
					    $vip['advbase'] &&
590
					    does_vip_exist($vip)) {
591
						unset($oldvips[$key]);
592
						/*
593
						 * Skip reconfiguring this vips
594
						 * since nothing has changed.
595
						 */
596
						continue;
597
					}
598

    
599
				} elseif ($vip['mode'] == "ipalias" &&
600
				    (substr($vip['interface'], 0, 4) == '_vip'
601
				    || strstr($vip['interface'], "lo0")) &&
602
				    isset($oldvips[$vip['subnet']])) {
603
					$key = $vip['subnet'];
604
					if ($oldvips[$key]['content'] ==
605
					    $vip['interface'] .
606
					    $vip['subnet'] .
607
					    $vip['subnet_bits'] &&
608
					    does_vip_exist($vip)) {
609
						unset($oldvips[$key]);
610
						/*
611
						 * Skip reconfiguring this vips
612
						 * since nothing has changed.
613
						 */
614
						continue;
615
					}
616
					unset($oldvips[$key]);
617
				}
618

    
619
				switch ($vip['mode']) {
620
				case "proxyarp":
621
					$anyproxyarp = true;
622
					break;
623
				case "ipalias":
624
					interface_ipalias_configure($vip);
625
					break;
626
				case "carp":
627
					$carp_setuped = true;
628
					if (does_vip_exist($vip) && isset($oldvips[$key]['vhid']) &&
629
					    ($oldvips[$key]['vhid'] ^ $vip['vhid'])) {
630
						/* properly remove the old VHID
631
						 * see https://redmine.pfsense.org/issues/12202 */
632
						$realif = get_real_interface($vip['interface']);
633
						mwexec("/sbin/ifconfig {$realif} " .
634
							escapeshellarg($vip['subnet']) . " -alias");
635
						$ipalias_reload = true;
636
					} else {
637
						$ipalias_reload = false;
638
					}
639
					interface_carp_configure($vip, false, $ipalias_reload);
640
					break;
641
				}
642
				$force_filterconfigure = true;
643
			}
644

    
645
			/* Cleanup remaining old carps */
646
			foreach ($oldvips as $oldvipar) {
647
				$oldvipif = get_real_interface(
648
				    $oldvipar['interface']);
649

    
650
				if (empty($oldvipif)) {
651
					continue;
652
				}
653

    
654
				/* do not remove VIP if the IP address remains the same */
655
				foreach (config_get_path('virtualip/vip', []) as $vip) {
656
					if ($vip['subnet'] == $oldvipar['subnet']) {
657
						continue 2;
658
					}
659
				}
660

    
661
				if (is_ipaddrv6($oldvipar['subnet'])) {
662
					 mwexec("/sbin/ifconfig " .
663
					     escapeshellarg($oldvipif) .
664
					     " inet6 " .
665
					     escapeshellarg($oldvipar['subnet']) .
666
					     " delete");
667
				} else {
668
					pfSense_interface_deladdress($oldvipif,
669
					    $oldvipar['subnet']);
670
				}
671
			}
672
			if ($carp_setuped == true) {
673
				interfaces_sync_setup();
674
			}
675
			if ($anyproxyarp == true) {
676
				interface_proxyarp_configure();
677
			}
678
		}
679

    
680
		local_sync_accounts($u2add, $u2del, $g2add, $g2del);
681
		$this->filter_configure(false, $force_filterconfigure);
682
		unset($old_config);
683

    
684
		pkg_call_plugins('plugin_xmlrpc_recv_done', []);
685
		return true;
686
	}
687

    
688
	/**
689
	 * Merge items into installedpackages config section
690
	 *
691
	 * @param array $section
692
	 *
693
	 * @return bool
694
	 */
695
	public function merge_installedpackages_section($section, $timeout) {
696
		ini_set('default_socket_timeout', $timeout);
697
		$this->auth();
698

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

    
704
		config_set_path('installedpackages', array_merge(
705
		    config_get_path('installedpackages'), $section));
706
		$mergedkeys = implode(", ", array_keys($section));
707
		write_config(sprintf(gettext(
708
		    "Merged in config (%s sections) from XMLRPC client."),
709
		    $mergedkeys));
710

    
711
		return true;
712
	}
713

    
714
	/**
715
	 * Merge items into config
716
	 *
717
	 * @param array $section
718
	 *
719
	 * @return bool
720
	 */
721
	public function merge_config_section($section, $timeout) {
722
		ini_set('default_socket_timeout', $timeout);
723
		$this->auth();
724

    
725
		if ($this->loop_detected) {
726
			log_error("Disallowing CARP sync loop");
727
			return true;
728
		}
729

    
730
		config_set_path('', $this->array_overlay(config_get_path(''), $section));
731
		$mergedkeys = implode(", ", array_keys($section));
732
		write_config(sprintf(gettext(
733
		    "Merged in config (%s sections) from XMLRPC client."),
734
		    $mergedkeys));
735

    
736
		return true;
737
	}
738

    
739
	/**
740
	 * Wrapper for filter_configure()
741
	 *
742
	 * @return bool
743
	 */
744
	private function filter_configure($reset_accounts = true, $force = false) {
745
		global $g, $old_config;
746

    
747
		filter_configure();
748
		system_routing_configure();
749
		setup_gateways_monitor();
750

    
751
		/* do not restart unchanged services on XMLRPC sync,
752
		 * see https://redmine.pfsense.org/issues/11082 
753
		 */
754
		if (is_array(config_get_path('openvpn')) || is_array($old_config['openvpn'])) {
755
			foreach (array("server", "client") as $type) {
756
				$remove_id = array();
757
				if (is_array(array_get_path($old_config, "openvpn/openvpn-{$type}"))) {
758
					foreach ($old_config['openvpn']["openvpn-{$type}"] as $old_settings) {
759
						$remove_id[] = $old_settings['vpnid'];
760
					}
761
				}
762
				if (!is_array(config_get_path("openvpn/openvpn-{$type}"))) {
763
					continue;
764
				}
765
				foreach (config_get_path("openvpn/openvpn-{$type}", []) as $settings) {
766
					$new_instance = true;
767
					if (in_array($settings['vpnid'], $remove_id)) {
768
						$remove_id = array_diff($remove_id, array($settings['vpnid']));
769
					}
770
					if (is_array(array_get_path($old_config, "openvpn/openvpn-{$type}"))) {
771
						foreach ($old_config['openvpn']["openvpn-{$type}"] as $old_settings) {
772
							if ($settings['vpnid'] == $old_settings['vpnid']) {
773
								$new_instance = false;
774
								if (($settings != $old_settings) || $force) {
775
									/* restart changed openvpn instance */
776
									openvpn_resync($type, $settings);
777
									break;
778
								}
779
							}
780
						}
781
					}
782
					if ($new_instance) {
783
						/* start new openvpn instance */
784
						openvpn_resync($type, $settings);
785
					}
786
				}
787
				if (!empty($remove_id)) {
788
					foreach ($remove_id as $id) {
789
						/* stop/delete removed openvpn instances */
790
						openvpn_delete($type, array('vpnid' => $id));
791
					}
792
				}
793
			}
794
			/* no service restart required */
795
			openvpn_resync_csc_all();
796
		}
797

    
798
		/* run ipsec_configure() on any IPsec change, see https://redmine.pfsense.org/issues/12075 */
799
		if (((is_array(config_get_path('ipsec')) || is_array($old_config['ipsec'])) &&
800
		    (config_get_path('ipsec') != $old_config['ipsec'])) ||
801
		    $force) {
802
			ipsec_configure();
803
		}
804

    
805
		/*
806
		 * The DNS Resolver and the DNS Forwarder may both be active so
807
		 * long as * they are running on different ports.
808
		 * See ticket #5882
809
		 */
810
		if (((is_array(config_get_path('dnsmasq')) || is_array($old_config['dnsmasq'])) &&
811
		    (config_get_path('dnsmasq') != $old_config['dnsmasq'])) ||
812
		    $force) {
813
			if (config_path_enabled('dnsmasq')) {
814
				/* Configure dnsmasq but tell it NOT to restart DHCP */
815
				services_dnsmasq_configure(false);
816
			} else {
817
				/* kill any running dnsmasq instance */
818
				if (isvalidpid("{$g['varrun_path']}/dnsmasq.pid")) {
819
					sigkillbypid("{$g['varrun_path']}/dnsmasq.pid",
820
					    "TERM");
821
				}
822
			}
823
		}
824
		if (((is_array(config_get_path('unbound')) || is_array($old_config['unbound'])) &&
825
		    (config_get_path('unbound') != $old_config['unbound'])) ||
826
		    $force) {
827
			if (config_path_enabled('unbound')) {
828
				/* Configure unbound but tell it NOT to restart DHCP */
829
				services_unbound_configure(false);
830
			} else {
831
				/* kill any running Unbound instance */
832
				if (isvalidpid("{$g['varrun_path']}/unbound.pid")) {
833
					sigkillbypid("{$g['varrun_path']}/unbound.pid",
834
					    "TERM");
835
				}
836
			}
837
		}
838

    
839
		/*
840
		 * Call this separately since the above are manually set to
841
		 * skip the DHCP restart they normally perform.
842
		 * This avoids restarting dhcpd twice as described on
843
		 * ticket #3797
844
		 */
845
		$called = [];
846
		foreach ([
847
			'dhcpd'			=> 'services_dhcpd_configure',
848
			'dhcpdv6'		=> 'services_dhcpd_configure',
849
			'kea'			=> 'services_dhcpd_configure',
850
			'kea6'			=> 'services_dhcpd_configure',
851
			'dhcrelay'		=> 'services_dhcrelay_configure',
852
			'dhcrelay6'		=> 'services_dhcrelay6_configure',
853
			'captiveportal'	=> 'captiveportal_configure',
854
			'voucher'		=> 'voucher_configure'
855
		] as $path => $fn) {
856
			if (!array_key_exists($fn, $called)) {
857
				if (((is_array(config_get_path($path)) || is_array($old_config[$path])) &&
858
			        (config_get_path($path) !== array_get_path($old_config, $path))) || $force) {
859
					if (is_callable($fn)) {
860
						$fn();
861
					}
862
					$called[$fn] = true;
863
				}
864
			}
865
		}
866

    
867
		if ($reset_accounts) {
868
			local_reset_accounts();
869
		}
870

    
871
		return true;
872
	}
873

    
874
	/**
875
	 * Wrapper for captiveportal connected users and
876
	 * active/expired vouchers synchronization
877
	 *
878
	 * @param array $arguments
879
	 *
880
	 * @return array|bool|null
881
	 */
882
	public function captive_portal_sync($arguments, $timeout) {
883
		ini_set('default_socket_timeout', $timeout);
884
		$this->auth();
885
		// Note : no protection against CARP loop is done here, and this is in purpose.
886
		// This function is used for bi-directionnal sync, which is precisely what CARP loop protection is supposed to prevent.
887
		// CARP loop has to be managed within functions using captive_portal_sync()
888
		global $g, $cpzone;
889

    
890
		if (empty($arguments['op']) || empty($arguments['zone']) || empty(config_get_path("captiveportal/{$arguments['zone']}"))) {
891
			return false;
892
		}
893
		$cpzone = $arguments['zone'];
894

    
895
		if ($arguments['op'] === 'get_databases') {
896
			$active_vouchers = [];
897
			$expired_vouchers = [];
898
			$usedmacs = [];
899

    
900
			foreach(config_get_path("voucher/{$cpzone}/roll", []) as $roll) {
901
				$expired_vouchers[$roll['number']] = base64_encode(voucher_read_used_db($roll['number']));
902
				$active_vouchers[$roll['number']] = voucher_read_active_db($roll['number']);
903
			}
904
			if (!empty(config_get_path("captiveportal/{$cpzone}/freelogins_count")) &&
905
			    !empty(config_get_path("captiveportal/{$cpzone}/freelogins_resettimeout"))) {
906
				$usedmacs = captiveportal_read_usedmacs_db();
907
			}
908
			// base64 is here for safety reasons, as we don't fully control
909
			// the content of these arrays.
910
			$returndata = array('connected_users' => base64_encode(serialize(captiveportal_read_db())),
911
			'active_vouchers' => base64_encode(serialize($active_vouchers)),
912
			'expired_vouchers' => base64_encode(serialize($expired_vouchers)),
913
			'usedmacs' => base64_encode(serialize($usedmacs)));
914

    
915
			return $returndata;
916
		} elseif ($arguments['op'] === 'connect_user') {
917
			$user = unserialize_data(base64_decode($arguments['user']), []);
918
			$user['attributes']['allow_time'] = $user['allow_time'];
919

    
920
			// pipeno might be different between primary and secondary
921
			$pipeno = captiveportal_get_next_dn_ruleno('auth');
922
			return portal_allow($user['clientip'], $user['clientmac'], $user['username'], $user['password'], null,
923
			    $user['attributes'], $pipeno, $user['authmethod'], $user['context'], $user['sessionid']);
924
		} elseif ($arguments['op'] === 'disconnect_user') {
925
			$session = unserialize_data(base64_decode($arguments['session']), []);
926
			/* read database again, as pipeno might be different between primary & secondary */
927
			$sessionid = SQLite3::escapeString($session['sessionid']);
928
			$local_dbentry = captiveportal_read_db("WHERE sessionid = '{$sessionid}'");
929

    
930
			if (!empty($local_dbentry) && count($local_dbentry) == 1) {
931
				return captiveportal_disconnect($local_dbentry[0], $session['term_cause'], $session['stop_time'], true);
932
			} else {
933
				return false;
934
			}
935
		} elseif ($arguments['op'] === 'remove_entries') {
936
			$entries = unserialize_data(base64_decode($arguments['entries']), []);
937

    
938
			return captiveportal_remove_entries($entries, true);
939
		} elseif ($arguments['op'] === 'disconnect_all') {
940
			$arguments = unserialize_data(base64_decode($arguments['arguments']), []);
941

    
942
			return captiveportal_disconnect_all($arguments['term_cause'], $arguments['logout_reason'], true);
943
		} elseif ($arguments['op'] === 'write_vouchers') {
944
			$arguments = unserialize_data(base64_decode($arguments['arguments']), []);
945

    
946
			if (is_array($arguments['active_and_used_vouchers_bitmasks'])) {
947
				foreach ($arguments['active_and_used_vouchers_bitmasks'] as $roll => $used) {
948
					if (is_array($used)) {
949
						foreach ($used as $u) {
950
							voucher_write_used_db($roll, base64_encode($u));
951
						}
952
					} else {
953
						voucher_write_used_db($roll, base64_encode($used));
954
					}
955
				}
956
			}
957
			foreach ($arguments['active_vouchers'] as $roll => $active_vouchers) {
958
				voucher_write_active_db($roll, $active_vouchers);
959
			}
960
			return true;
961
		} elseif ($arguments['op'] === 'write_usedmacs') {
962
			$arguments = unserialize_data(base64_decode($arguments['arguments']), []);
963

    
964
			captiveportal_write_usedmacs_db($arguments['usedmacs']); 
965
			return true;
966
		}
967
	}
968

    
969
	/**
970
	 * Wrapper for configuring CARP interfaces
971
	 *
972
	 * @return bool
973
	 */
974
	public function interfaces_carp_configure() {
975
		$this->auth();
976

    
977
		if ($this->loop_detected) {
978
			log_error("Disallowing CARP sync loop");
979
			return true;
980
		}
981

    
982
		interfaces_vips_configure();
983

    
984
		return true;
985
	}
986

    
987
	/**
988
	 * Wrapper for rc.reboot
989
	 *
990
	 * @return bool
991
	 */
992
	public function reboot() {
993
		$this->auth();
994

    
995
		mwexec_bg("/etc/rc.reboot");
996

    
997
		return true;
998
	}
999
}
1000

    
1001
// run script until its done and can 'unlock' the xmlrpc.lock, this prevents hanging php-fpm / webgui
1002
ignore_user_abort(true);
1003
set_time_limit(0);
1004

    
1005
$xmlrpclockkey = lock('xmlrpc', LOCK_EX);
1006

    
1007
XML_RPC2_Backend::setBackend('php');
1008
$HTTP_RAW_POST_DATA = file_get_contents('php://input');
1009

    
1010
$options = array(
1011
	'prefix' => 'pfsense.',
1012
	'encoding' => 'utf-8',
1013
	'autoDocument' => false,
1014
);
1015

    
1016
$server = XML_RPC2_Server::create(new pfsense_xmlrpc_server(), $options);
1017
$server->handleCall();
1018

    
1019
unlock($xmlrpclockkey);
1020

    
1021
?>
(232-232/232)