Project

General

Profile

Download (7.56 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-2024 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') &&
97
	    (in_array(substr($ph1ent['interface'], 4), array_column($vips, 'uniqid')))) {
98
		log_error("Reconfiguring IPsec because of transition to CARP master.");
99
		ipsec_configure();
100
		break;
101
	}
102
}
103

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

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

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

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

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

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

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

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

    
188
				portal_allow($user['ip'], $user['mac'], $user['username'], base64_decode($user['bpassword']), null,
189
				    $attributes, $pipeno, $user['authmethod'], $user['context'], $user['sessionid'], true);
190
			}
191
			foreach ($expired_vouchers as $roll => $vdb) {
192
				voucher_write_used_db($roll, $vdb);
193
			}
194
			foreach ($active_vouchers as $roll => $vouchers) {
195
				voucher_write_active_db($roll, $vouchers);
196
			}
197
			captiveportal_write_usedmacs_db($usedmacs); 
198
		}
199
		captiveportal_syslog(sprintf(gettext('Connected users, used vouchers and used MACs have been synchronized from %1$s'), $config['hasync']['synchronizetoip']));
200
	}
201
}
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)