Project

General

Profile

Download (7.55 KB) Statistics
| Branch: | Tag: | Revision:
1
#!/usr/local/bin/php-cgi -f
2
<?php
3
/*
4
 * rc.carpmaster
5
 *
6
 * part of pfSense (https://www.pfsense.org)
7
 * Copyright (c) 2004-2013 BSD Perimeter
8
 * Copyright (c) 2013-2016 Electric Sheep Fencing
9
 * Copyright (c) 2014-2022 Rubicon Communications, LLC (Netgate)
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
require_once("functions.inc");
26
require_once("config.inc");
27
require_once("notices.inc");
28
require_once("openvpn.inc");
29
require_once("ipsec.inc");
30
require_once("interfaces.inc");
31
require_once("captiveportal.inc");
32

    
33
if (isset($_GET['interface'])) {
34
	$argument = $_GET['interface'];
35
} else {
36
	$argument = str_replace("\n", "", $argv[1]);
37
}
38
$argument = ltrim($argument, '$');
39
if (!strstr($argument, "@")) {
40
	log_error("CARP master event triggered from wrong source {$argument}");
41
	exit;
42
}
43

    
44
list($vhid, $iface) = explode("@", $argument);
45

    
46
$friendly = convert_real_interface_to_friendly_interface_name($iface);
47
$friendly_descr = convert_friendly_interface_to_friendly_descr($friendly);
48
$vips = link_interface_to_vips($friendly, '', $vhid);
49
if (!is_array($vips)) {
50
	log_error("CARP master event triggered from wrong source {$argument} - no associated VIPs");
51
	exit;
52
}
53
foreach ($vips as $vip) {
54
	$notificationmsg = sprintf('HA cluster member "(%1$s@%2$s): (%3$s)" has resumed CARP state "MASTER" for vhid %4$s',
55
					$vip['subnet'], $iface, $friendly_descr, $vhid);
56

    
57
	notify_all_remote($notificationmsg);
58
	log_error($notificationmsg);
59
}
60
restart_ppp_interfaces_using_interfaces($vips);
61

    
62
/* Start OpenVPN clients running on this VIP, since they should be in the stopped state while the VIP is CARP Backup. */
63
global $config;
64
$a_groups = return_gateway_groups_array(true);
65

    
66
foreach (array('server', 'client') as $mode) {
67
	foreach (config_get_path("openvpn/openvpn-{$mode}", []) as $settings) {
68
		if (empty($settings)) {
69
			continue;
70
		}
71
		if (substr($settings['interface'], 0, 4) == '_vip') {
72
			$openvpn_vip = $settings['interface'];
73
		} else if (is_array($a_groups[$settings['interface']])) {
74
			// interface is a gateway group, check CARP VIP
75
			$vip = array_get_path($a_groups, "{$settings['interface']}/0/vip");
76
			if (substr($vip, 0, 4) == '_vip') {
77
				$openvpn_vip = $vip;
78
			}
79
		} else {
80
			// this OpenVPN instance not on a CARP VIP
81
			continue;
82
		}
83
		foreach ($vips as $vip) {
84
			if ($openvpn_vip == "_vip{$vip['uniqid']}") {
85
				log_error("Starting OpenVPN {$mode} instance on {$friendly_descr} because of transition to CARP master.");
86
				openvpn_restart($mode, $settings);
87
			}
88
		}
89
	}
90
}
91

    
92
foreach (config_get_path("ipsec/phase1", []) as $ph1ent) {
93
	if (empty($ph1ent)) {
94
		continue;
95
	}
96
	if ((substr($ph1ent['interface'], 0, 4) == '_vip') && (in_array($ph1ent['interface'], $vips))) {
97
		log_error("Reconfiguring IPsec because of transition to CARP master.");
98
		ipsec_configure();
99
		break;
100
	}
101
}
102

    
103
/* Reconfigure radvd when necessary */
104
$rafound = false;
105
foreach (config_get_path("dhcpdv6", []) as $dhcpv6if => $dhcpv6ifconf) {
106
	if (empty($dhcpv6ifconf)) {
107
		continue;
108
	}
109
	foreach ($vips as $vip) {
110
		if ($dhcpv6ifconf['rainterface'] == "_vip{$vip['uniqid']}") {
111
			log_error("Starting radvd instance on {$friendly_descr} because of transition to CARP master.");
112
			$rafound = true;
113
		}
114
	}
115
}
116
if ($rafound) {
117
	services_radvd_configure();
118
}
119

    
120
/* Reconfigure DHCP Relay when necessary */
121
if (config_path_enabled('dhcrelay') &&
122
    (config_get_path('dhcrelay/carpstatusvip') == "_vip{$vip['uniqid']}")) {
123
	log_error("Starting DHCP Relay service because of transition to CARP master.");
124
	services_dhcrelay_configure();
125
}
126

    
127
/* Reconfigure DHCPv6 Relay when necessary */
128
if (config_path_enabled('dhcrelay6') &&
129
    (config_get_path('dhcrelay6/carpstatusvip') == "_vip{$vip['uniqid']}")) {
130
	log_error("Starting DHCPv6 Relay service because of transition to CARP master.");
131
	services_dhcrelay6_configure();
132
}
133

    
134
/* Reconfigure captive portal when necessary :
135
   If we are the primary node, and we are switching back from backup to master : Get user list from the backup node */
136
if (!empty(config_get_path('captiveportal')) &&
137
    !empty(config_get_path('hasync/synchronizetoip')) &&
138
    !empty(config_get_path('hasync/synchronizecaptiveportal'))) {
139
	$xmlrpc_username = config_get_path('hasync/username');
140
	if (empty($xmlrpc_username)) {
141
		$xmlrpc_username = "admin";
142
	}
143
	$xmlrpc_port = config_get_path('system/webgui/port');
144
	if (empty($xmlrpc_port)) {
145
		if (config_get_path('system/webgui/protocol') == "http") {
146
			$xmlrpc_port = "80";
147
		} else {
148
			$xmlrpc_port = "443";
149
		}
150
	}
151

    
152
	foreach (array_keys(config_get_path('captiveportal')) as $cpzone) {
153
		$rpc_client = new pfsense_xmlrpc_client();
154
		$rpc_client->setConnectionData(config_get_path('hasync/synchronizetoip'), $xmlrpc_port, $xmlrpc_username, config_get_path('hasync/password'));
155
		$resp = $rpc_client->xmlrpc_method('captive_portal_sync', array('op' => 'get_databases', 'zone' => $cpzone));
156

    
157
		if (is_array($resp) || !empty($resp)) { // $resp will be an array only if the communication was successful
158
			// Contains array of connected users (will be stored in SQLite DB)
159
			$connected_users = unserialize(base64_decode($resp['connected_users']));
160
			// Contains array of active vouchers (will be stored in active vouchers db)
161
			$active_vouchers = unserialize(base64_decode($resp['active_vouchers']));
162
			// Contain bitmask of both in use and expired vouchers (will be stored in "used vouchers" db)
163
			$expired_vouchers = unserialize(base64_decode($resp['expired_vouchers']));
164
			// Contains array of usedmacs (will be stored in usedmacs db)
165
			$usedmacs = unserialize(base64_decode($resp['usedmacs']));
166

    
167
			$cpdb = captiveportal_read_db();
168
			$unsetindexes = array_column($cpdb, 5);
169
			if (!empty($unsetindexes)) {
170
				captiveportal_remove_entries($unsetindexes, true); // true: prevent carp loop
171
			}
172
			captiveportal_free_dnrules();
173

    
174
			foreach ($connected_users as $user) {
175
				if (!is_array($user) || empty($user)) {
176
					continue;
177
				}
178
				$pipeno = captiveportal_get_next_dn_ruleno('auth');
179
				$attributes = array();
180
				$attributes['allow_time'] = $user['allow_time'];
181
				$attributes['session_timeout'] = $user['session_timeout'];
182
				$attributes['idle_timeout'] = $user['idle_timeout'];
183
				$attributes['session_terminate_time'] = $user['session_terminate_time'];
184
				$attributes['interim_interval'] = $user['interim_interval'];
185
				$attributes['maxbytes'] = $user['traffic_quota'];
186

    
187
				portal_allow($user['ip'], $user['mac'], $user['username'], base64_decode($user['bpassword']), null,
188
				    $attributes, $pipeno, $user['authmethod'], $user['context'], $user['sessionid'], true);
189
			}
190
			foreach ($expired_vouchers as $roll => $vdb) {
191
				voucher_write_used_db($roll, $vdb);
192
			}
193
			foreach ($active_vouchers as $roll => $vouchers) {
194
				voucher_write_active_db($roll, $vouchers);
195
			}
196
			captiveportal_write_usedmacs_db($usedmacs); 
197
		}
198
		captiveportal_syslog(sprintf(gettext('Connected users, used vouchers and used MACs have been synchronized from %1$s'), $config['hasync']['synchronizetoip']));
199
	}
200
}
201
openlog("", LOG_PID, LOG_LOCAL0);
202
$pluginparams = array();
203
$pluginparams['type'] = 'carp';
204
$pluginparams['event'] = 'rc.carpmaster';
205
$pluginparams['interface'] = $argument;
206
pkg_call_plugins('plugin_carp', $pluginparams);
207

    
208
?>
(25-25/85)