rc.filter_synchronize 14.1 KB
Newer Older
1
#!/usr/local/bin/php
Ad Schellevis's avatar
Ad Schellevis committed
2
<?php
3

Ad Schellevis's avatar
Ad Schellevis committed
4 5
/*
        Copyright (C) 2004-2006 Scott Ullrich
6 7 8
        Copyright (C) 2005 Bill Marquette
        Copyright (C) 2006 Peter Allgeyer
        Copyright (C) 2008 Ermal Luci
Ad Schellevis's avatar
Ad Schellevis committed
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
        Copyright (C) 2003-2004 Manuel Kasper <mk@neon1.net>.
        All rights reserved.

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

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

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

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

require_once("config.inc");
require_once("filter.inc");
36
require_once("util.inc");
Ad Schellevis's avatar
Ad Schellevis committed
37
require_once("interfaces.inc");
38
require_once("XMLRPC_Client.inc") ;
39
require_once("util.inc");
Ad Schellevis's avatar
Ad Schellevis committed
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84

/*
 *  backup_vip_config_section($section): returns as an xml file string of
 *                                   the configuration section
 */
function backup_vip_config_section() {
	global $config;

	if (!is_array($config['virtualip']['vip']))
		return;
	$temp = array();
	$temp['vip'] = array();
	foreach($config['virtualip']['vip'] as $section) {
		if ($section['mode'] != "carp")
			continue;
		if ($section['advskew'] <> "") {
			$section_val = intval($section['advskew']);
			$section_val=$section_val+100;
			if ($section_val > 254)
				$section_val = 254;
			$section['advskew'] = $section_val;
		}
		if ($section['advbase'] <> "") {
			$section_val = intval($section['advbase']);
			if ($section_val > 254)
				$section_val = 254;
			$section['advbase'] = $section_val;
		}
		$temp['vip'][] = $section;
	}
	return $temp;
}

function remove_special_characters($string) {
	$match_array = "";
	preg_match_all("/[a-zA-Z0-9\_\-]+/",$string,$match_array);
	$string = "";
	foreach ($match_array[0] as $ma) {
		if ($string <> "")
			$string .= " ";
		$string .= $ma;
	}
	return $string;
}

85
function carp_check_version($url, $username, $password, $method = 'opnsense.firmware_version') {
Ad Schellevis's avatar
Ad Schellevis committed
86 87
	global $config, $g;

88
	if (file_exists('/var/run/booting')) {
89
		return;
Ad Schellevis's avatar
Ad Schellevis committed
90
	}
Ad Schellevis's avatar
Ad Schellevis committed
91

92

93 94 95 96 97 98 99
	$client = new SimpleXMLRPC_Client($url,240);
	$client->setCredentials($username, $password);
	if ($client->query($method)) {
	    $remote_version = $client->getResponse();
	} else {
            // propagate error to log
            $error = "An error occurred while attempting XMLRPC sync with username {$username} and {$url} " .  $client->error ;
100
	    log_error($error);
101
            file_notice("sync_settings", $error, "Settings Sync", "");
102
	    // print communication details on failure
103 104 105 106 107 108
	    echo $client->getDetails();
            return false ;
	}

	if (!is_array($remote_version) && trim($remote_version) == "Authentication failed") {
		$error = "An authentication failure occurred while trying to access {$url} ({$method}).";
Ad Schellevis's avatar
Ad Schellevis committed
109 110
		log_error($error);
		file_notice("sync_settings", $error, "Settings Sync", "");
111
		exit;
Ad Schellevis's avatar
Ad Schellevis committed
112
	}
113

114 115 116 117 118 119 120 121
	if (!isset($remote_version['config_version']) ||
		$remote_version['config_version'] < $config['version']) {
		update_filter_reload_status("The other member is on older configuration version of {$g['product_name']}. Sync will not be done to prevent problems!");
		log_error("The other member is on older configuration version of {$g['product_name']}. Sync will not be done to prevent problems!");
		return false;
	} else {
		return true;
	}
122

Ad Schellevis's avatar
Ad Schellevis committed
123 124
}

125
function carp_sync_xml($url, $username, $password, $sections, $method = 'opnsense.restore_config_section') {
Ad Schellevis's avatar
Ad Schellevis committed
126 127
	global $config, $g;

128
	if (file_exists('/var/run/booting')) {
129
		return;
Ad Schellevis's avatar
Ad Schellevis committed
130
	}
Ad Schellevis's avatar
Ad Schellevis committed
131 132 133 134 135 136 137

	update_filter_reload_status("Syncing CARP data to {$url}");

	/* make a copy of config */
	$config_copy = $config;

	/* strip out nosync items */
138
	if (is_array($config_copy['nat']) && is_array($config_copy['nat']['outbound']['rule'])) {
Ad Schellevis's avatar
Ad Schellevis committed
139 140 141 142 143 144 145
		$rulescnt = count($config_copy['nat']['outbound']['rule']);
		for ($x = 0; $x < $rulescnt; $x++) {
			$config_copy['nat']['outbound']['rule'][$x]['descr'] = remove_special_characters($config_copy['nat']['outbound']['rule'][$x]['descr']);
			if (isset ($config_copy['nat']['outbound']['rule'][$x]['nosync']))
				unset ($config_copy['nat']['outbound']['rule'][$x]);
		}
	}
146
	if (is_array($config_copy['nat']) && is_array($config_copy['nat']['rule'])) {
Ad Schellevis's avatar
Ad Schellevis committed
147 148 149 150 151 152 153
		$natcnt = count($config_copy['nat']['rule']);
		for ($x = 0; $x < $natcnt; $x++) {
			$config_copy['nat']['rule'][$x]['descr'] = remove_special_characters($config_copy['nat']['rule'][$x]['descr']);
			if (isset ($config_copy['nat']['rule'][$x]['nosync']))
				unset ($config_copy['nat']['rule'][$x]);
		}
	}
154
	if (is_array($config_copy['filter']) && is_array($config_copy['filter']['rule'])) {
Ad Schellevis's avatar
Ad Schellevis committed
155 156 157 158 159 160 161
		$filtercnt = count($config_copy['filter']['rule']);
		for ($x = 0; $x < $filtercnt; $x++) {
			$config_copy['filter']['rule'][$x]['descr'] = remove_special_characters($config_copy['filter']['rule'][$x]['descr']);
			if (isset ($config_copy['filter']['rule'][$x]['nosync']))
				unset ($config_copy['filter']['rule'][$x]);
		}
	}
162
	if (isset($config_copy['aliases']) && is_array($config_copy['aliases']) && is_array($config_copy['aliases']['alias'])) {
Ad Schellevis's avatar
Ad Schellevis committed
163 164 165 166 167 168 169
		$aliascnt = count($config_copy['aliases']['alias']);
		for ($x = 0; $x < $aliascnt; $x++) {
			$config_copy['aliases']['alias'][$x]['descr'] = remove_special_characters($config_copy['aliases']['alias'][$x]['descr']);
			if (isset ($config_copy['aliases']['alias'][$x]['nosync']))
				unset ($config_copy['aliases']['alias'][$x]);
		}
	}
170
	if (is_array($config_copy['dnsmasq']) && is_array($config_copy['dnsmasq']['hosts'])) {
Ad Schellevis's avatar
Ad Schellevis committed
171 172 173 174 175 176 177
		$dnscnt = count($config_copy['dnsmasq']['hosts']);
		for ($x = 0; $x < $dnscnt; $x++) {
			$config_copy['dnsmasq']['hosts'][$x]['descr'] = remove_special_characters($config_copy['dnsmasq']['hosts'][$x]['descr']);
			if (isset ($config_copy['dnsmasq']['hosts'][$x]['nosync']))
				unset ($config_copy['dnsmasq']['hosts'][$x]);
		}
	}
178
	if (isset($config_copy['ipsec']) && is_array($config_copy['ipsec']) && is_array($config_copy['ipsec']['tunnel'])) {
Ad Schellevis's avatar
Ad Schellevis committed
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
		$ipseccnt = count($config_copy['ipsec']['tunnel']);
		for ($x = 0; $x < $ipseccnt; $x++) {
			$config_copy['ipsec']['tunnel'][$x]['descr'] = remove_special_characters($config_copy['ipsec']['tunnel'][$x]['descr']);
			if (isset ($config_copy['ipsec']['tunnel'][$x]['nosync']))
				unset ($config_copy['ipsec']['tunnel'][$x]);
		}
	}

	if (is_array($config_copy['dhcpd'])) {
		foreach($config_copy['dhcpd'] as $dhcpif => $dhcpifconf) {
			if($dhcpifconf['failover_peerip'] <> "") {
				$int = guess_interface_from_ip($dhcpifconf['failover_peerip']);
				$intip = find_interface_ip($int);
				$config_copy['dhcpd'][$dhcpif]['failover_peerip'] = $intip;
			}
		}
	}

	foreach ($sections as $section) {
	   /*  we can't use array_intersect_key()
		*  due to the vip 'special case'
		*/
		switch ($section) {
			case 'virtualip':
				$xml[$section] = backup_vip_config_section();
				break;
			case 'user':
				$xml['system'][$section] = $config_copy['system'][$section];
				$xml['system']['nextuid'] = $config_copy['system']['nextuid'];
				break;
			case 'group':
				$xml['system'][$section] = $config_copy['system'][$section];
				$xml['system']['nextgid'] = $config_copy['system']['nextgid'];
				break;
			case 'authserver':
				$xml['system'][$section] = $config_copy['system'][$section];
			default:
				$xml[$section] = $config_copy[$section];
		}
	}

220 221 222 223 224 225 226 227

	$client = new SimpleXMLRPC_Client($url,240);
	$client->setCredentials($username, $password);
	if ($client->query($method, $xml)) {
	    $response = $client->getResponse();
	} else {
            // propagate error to log
            $error = "An error occurred while attempting XMLRPC sync with username {$username} and {$url} " .  $client->error ;
228
	    log_error($error);
229
            file_notice("sync_settings", $error, "Settings Sync", "");
230
	    // print communication details on failure
231 232 233
	    echo $client->getDetails();
            return false ;
	}
234

235 236 237 238 239
	if (!is_array($response) && trim($response) == "Authentication failed") {
		$error = "An authentication failure occurred while trying to access {$url} ({$method}).";
		log_error($error);
		file_notice("sync_settings", $error, "Settings Sync", "");
		exit;
Ad Schellevis's avatar
Ad Schellevis committed
240
	}
241 242

	return true;
Ad Schellevis's avatar
Ad Schellevis committed
243 244 245
}

global $g;
246 247

if (file_exists('/var/run/booting')) {
Ad Schellevis's avatar
Ad Schellevis committed
248 249 250
        return;
}

Ad Schellevis's avatar
Ad Schellevis committed
251
if (is_array($config['hasync'])) {
Ad Schellevis's avatar
Ad Schellevis committed
252
	$sections = array();
253
	update_filter_reload_status("Building high availability information");
Ad Schellevis's avatar
Ad Schellevis committed
254 255 256 257 258 259
	$hasync = $config['hasync'];

	if (empty($hasync['synchronizetoip'])) {
		log_error("Config sync not being done because of missing sync IP (this is normal on secondary systems).");
		exit;
	}
260 261
	if(is_ipaddrv6($hasync['synchronizetoip']))
		$hasync['synchronizetoip'] = "[{$hasync['synchronizetoip']}]";
Ad Schellevis's avatar
Ad Schellevis committed
262 263 264 265 266 267

	/*
	* XXX: The way we're finding the port right now is really suboptimal -
	*     we can't assume that the other machine is setup identically.
	*/
	if (!empty($config['system']['webgui']['protocol'])) {
268 269
		$port = $config['system']['webgui']['port'];
		if (!empty($port)) {
270
			$synchronizeto =  $config['system']['webgui']['protocol'] . '://'.$hasync['synchronizetoip'].':'.$port."/xmlrpc.php";
271
		} else {
272
			$synchronizeto =  $config['system']['webgui']['protocol'] . '://'.$hasync['synchronizetoip']."/xmlrpc.php" ;
273
		}
Ad Schellevis's avatar
Ad Schellevis committed
274 275 276
	}


Ad Schellevis's avatar
Ad Schellevis committed
277
	if (isset($hasync['synchronizerules'])) {
Ad Schellevis's avatar
Ad Schellevis committed
278 279 280 281
		if (!is_array($config['filter']))
			$config['filter'] = array();
		$sections[] = 'filter';
	}
Ad Schellevis's avatar
Ad Schellevis committed
282
	if (isset($hasync['synchronizenat'])) {
Ad Schellevis's avatar
Ad Schellevis committed
283 284 285 286
		if (!is_array($config['nat']))
			$config['nat'] = array();
		$sections[] = 'nat';
	}
Ad Schellevis's avatar
Ad Schellevis committed
287
	if (isset($hasync['synchronizealiases'])) {
288
		if (!isset($config['aliases']) || !is_array($config['aliases'])) {
Ad Schellevis's avatar
Ad Schellevis committed
289
			$config['aliases'] = array();
290
		}
Ad Schellevis's avatar
Ad Schellevis committed
291 292
		$sections[] = 'aliases';
	}
Ad Schellevis's avatar
Ad Schellevis committed
293
	if (isset($hasync['synchronizedhcpd']) && is_array($config['dhcpd']))
Ad Schellevis's avatar
Ad Schellevis committed
294
		$sections[] = 'dhcpd';
Ad Schellevis's avatar
Ad Schellevis committed
295
	if (isset($hasync['synchronizewol'])) {
Ad Schellevis's avatar
Ad Schellevis committed
296 297 298 299
		if (!is_array($config['wol']))
			$config['wol'] = array();
		$sections[] = 'wol';
	}
Ad Schellevis's avatar
Ad Schellevis committed
300
	if (isset($hasync['synchronizestaticroutes'])) {
Ad Schellevis's avatar
Ad Schellevis committed
301 302
		if (!is_array($config['staticroutes']))
			$config['staticroutes'] = array();
Ad Schellevis's avatar
Ad Schellevis committed
303
		if (!isset($config['staticroutes']['route']) || !is_array($config['staticroutes']['route']))
Ad Schellevis's avatar
Ad Schellevis committed
304 305 306 307 308 309
			$config['staticroutes']['route'] = array();
		$sections[] = 'staticroutes';
		if (!is_array($config['gateways']))
			$config['gateways'] = array();
		$sections[] = 'gateways';
	}
Ad Schellevis's avatar
Ad Schellevis committed
310 311
	if (isset($hasync['synchronizevirtualip'])) {
		if (!isset($config['virtualip']) || !is_array($config['virtualip']))
Ad Schellevis's avatar
Ad Schellevis committed
312 313 314
			$config['virtualip'] = array();
		$sections[] = 'virtualip';
	}
Ad Schellevis's avatar
Ad Schellevis committed
315
	if (isset($hasync['synchronizelb'])) {
Ad Schellevis's avatar
Ad Schellevis committed
316 317 318 319
		if (!is_array($config['load_balancer']))
			$config['load_balancer'] = array();
		$sections[] = 'load_balancer';
	}
Ad Schellevis's avatar
Ad Schellevis committed
320
	if (isset($hasync['synchronizeipsec'])) {
321
		if (!isset($config['ipsec']) || !is_array($config['ipsec'])) {
Ad Schellevis's avatar
Ad Schellevis committed
322
			$config['ipsec'] = array();
323
		}
Ad Schellevis's avatar
Ad Schellevis committed
324 325
		$sections[] = 'ipsec';
	}
Ad Schellevis's avatar
Ad Schellevis committed
326 327
	if (isset($hasync['synchronizeopenvpn'])) {
		if (!isset($config['openvpn']) || !is_array($config['openvpn']))
Ad Schellevis's avatar
Ad Schellevis committed
328 329 330
			$config['openvpn'] = array();
		$sections[] = 'openvpn';
	}
Ad Schellevis's avatar
Ad Schellevis committed
331
	if (isset($hasync['synchronizecerts']) || isset($hasync['synchronizeopenvpn'])) {
Ad Schellevis's avatar
Ad Schellevis committed
332 333 334 335 336 337 338 339
		if (!is_array($config['cert']))
			$config['cert'] = array();
		$sections[] = 'cert';

		if (!is_array($config['ca']))
			$config['ca'] = array();
		$sections[] = 'ca';

Ad Schellevis's avatar
Ad Schellevis committed
340
		if (!isset($config['crl']) || !is_array($config['crl']))
Ad Schellevis's avatar
Ad Schellevis committed
341 342 343
			$config['crl'] = array();
		$sections[] = 'crl';
	}
Ad Schellevis's avatar
Ad Schellevis committed
344
	if (isset($hasync['synchronizeusers'])) {
Ad Schellevis's avatar
Ad Schellevis committed
345 346
		$sections[] = 'user';
		$sections[] = 'group';
347
	}
Ad Schellevis's avatar
Ad Schellevis committed
348
	if (isset($hasync['synchronizeauthservers'])) {
Ad Schellevis's avatar
Ad Schellevis committed
349 350
		$sections[] = 'authserver';
	}
Ad Schellevis's avatar
Ad Schellevis committed
351
	if (isset($hasync['synchronizednsforwarder']) && is_array($config['dnsmasq']))
Ad Schellevis's avatar
Ad Schellevis committed
352
		$sections[] = 'dnsmasq';
Ad Schellevis's avatar
Ad Schellevis committed
353 354
	if (isset($hasync['synchronizeschedules']) || isset($hasync['synchronizerules'])) {
		if (!isset($config['schedules']) || !is_array($config['schedules']))
Ad Schellevis's avatar
Ad Schellevis committed
355 356 357 358 359 360 361 362 363 364 365 366 367 368
			$config['schedules'] = array();
		$sections[] = 'schedules';
	}

	if (count($sections) <= 0) {
		log_error("Nothing has been configured to be synched. Skipping....");
		exit;
	}

	if (empty($hasync['username']))
		$username = "admin";
	else
		$username = $hasync['username'];

369
	if (!carp_check_version($synchronizeto, $username, $hasync['password']))
Ad Schellevis's avatar
Ad Schellevis committed
370 371 372
		exit;

	update_filter_reload_status("Signaling CARP reload signal...");
373
	carp_sync_xml($synchronizeto, $username, $hasync['password'], $sections);
374

375 376
	if (count($argv) <= 1 || $argv[1] != 'restart' ) {
		// only sync data, no reload
377
		// TODO: config sync probably needs more thinking, but when we always force a reload
378 379 380
		// TODO: the machine tends to get sloppy
		exit;
	}
381 382 383 384 385 386 387
	$client = new SimpleXMLRPC_Client($synchronizeto,240);
	$client->setCredentials($username, $hasync['password']);
	if ($client->query("opnsense.filter_configure")) {
	    $response = $client->getResponse();
	} else {
            // propagate error to log
            $error = "An error occurred while attempting XMLRPC sync with username {$username} and {$url} " .  $client->error ;
388
	    log_error($error);
389
            file_notice("sync_settings", $error, "Settings Sync", "");
390
	    // print communication details on failure
391 392 393 394 395 396
	    echo $client->getDetails();
            return false ;
	}

	if (!is_array($response) && trim($response) == "Authentication failed") {
		$error = "An authentication failure occurred while trying to access {$url} ({$method}).";
Ad Schellevis's avatar
Ad Schellevis committed
397 398
		log_error($error);
		file_notice("sync_settings", $error, "Settings Sync", "");
399
		exit;
Ad Schellevis's avatar
Ad Schellevis committed
400
	}
401 402 403 404



	log_error("Filter sync successfully completed with {$synchronizetoip}:{$port}.");
Ad Schellevis's avatar
Ad Schellevis committed
405
}