1
2
3
4
5
6
7
8
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
36
37
38
39
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
|
#!/usr/local/bin/php -f
<?php
/*
filter.inc
Copyright (C) 2004-2006 Scott Ullrich
Copyright (C) 2005 Bill Marquette
Copyright (C) 2006 Peter Allgeyer
Copyright (C) 2008 Ermal Luci
All rights reserved.
originally part of m0n0wall (http://m0n0.ch/wall)
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("globals.inc");
require_once("config.inc");
require_once("functions.inc");
require_once("filter.inc");
require_once("shaper.inc");
require_once("xmlrpc.inc");
require_once("interfaces.inc");
/*
* backup_vip_config_section($section): returns as an xml file string of
* the configuration section
*/
function backup_vip_config_section() {
global $config;
$new_section = &$config['virtualip'];
foreach($new_section['vip'] as $section) {
if($section['mode'] == "proxyarp") {
unset($section);
}
if($section['advskew'] <> "") {
$section_val = intval($section['advskew']);
$section_val=$section_val+100;
if($section_val > 255)
$section_val = 255;
$section['advskew'] = $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;
}
function carp_sync_xml($url, $password, $sections, $port = 80, $method = 'pfsense.restore_config_section') {
global $config, $g;
if(file_exists("{$g['varrun_path']}/booting"))
return;
update_filter_reload_status("Syncing CARP data to {$url}");
/* make a copy of config */
$config_copy = $config;
/* strip out nosync items */
if (is_array($config_copy['nat']['advancedoutbound']['rule'])) {
$rulescnt = count($config_copy['nat']['advancedoutbound']['rule']);
for ($x = 0; $x < $rulescnt; $x++) {
$config_copy['nat']['advancedoutbound']['rule'][$x]['descr'] = remove_special_characters($config_copy['nat']['advancedoutbound']['rule'][$x]['descr']);
if (isset ($config_copy['nat']['advancedoutbound']['rule'][$x]['nosync']))
unset ($config_copy['nat']['advancedoutbound']['rule'][$x]);
}
}
if (is_array($config_copy['nat']['rule'])) {
$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]);
}
}
if (is_array($config_copy['filter']['rule'])) {
$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]);
}
}
if (is_array($config_copy['aliases']['alias'])) {
$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]);
}
}
if (is_array($config_copy['dnsmasq']['hosts'])) {
$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]);
}
}
if (is_array($config_copy['virtualip']['vip'])) {
$vipcnt = count($config_copy['virtualip']['vip']);
for ($x = 0; $x < $vipcnt; $x++) {
$config_copy['virtualip']['vip'][$x]['descr'] = remove_special_characters($config_copy['virtualip']['vip'][$x]['descr']);
if (isset ($config_copy['virtualip']['vip'][$x]['nosync']) or $config_copy['virtualip']['vip'][$x]['mode'] == "proxyarp")
unset ($config_copy['virtualip']['vip'][$x]);
}
}
if (is_array($config_copy['ipsec']['tunnel'])) {
$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 'cert':
case 'ca':
case 'user':
case 'group':
$xml['system'][$section] = $config_copy['system'][$section];
break;
default:
$xml[$section] = $config_copy[$section];
}
}
$params = array(
XML_RPC_encode($password),
XML_RPC_encode($xml)
);
$numberofruns = 0;
while ($numberofruns < 2) {
log_error("Beginning XMLRPC sync to {$url}:{$port}.");
$msg = new XML_RPC_Message($method, $params);
$cli = new XML_RPC_Client('/xmlrpc.php', $url, $port);
$username = $config['system']['user'][0]['name'];
$cli->setCredentials($username, $password);
if($numberofruns > 1)
$cli->setDebug(1);
/* send our XMLRPC message and timeout after 240 seconds */
$resp = $cli->send($msg, "240");
if($resp->faultCode()) {
$error = "A communications error occurred while attempting communication with {$url}:{$port} (pfsense.exec_php).";
log_error($error);
return;
}
if(!$resp) {
$error = "A communications error occured while attempting XMLRPC sync with username {$username} {$url}:{$port}.";
log_error($error);
file_notice("sync_settings", $error, "Settings Sync", "");
} elseif($resp->faultCode()) {
$error = "An error code was received while attempting XMLRPC sync with username {$username} {$url}:{$port} - Code " . $resp->faultCode() . ": " . $resp->faultString();
log_error($error);
file_notice("sync_settings", $error, "Settings Sync", "");
} else {
log_error("XMLRPC sync successfully completed with {$url}:{$port}.");
$numberofruns = 3;
}
$parsed_response = XML_RPC_Decode($resp->value());
if(!is_array($firewall_info) && trim($firewall_info) == "Authentication failed") {
$error = "A authentication failure occurred while trying to access {$url}:{$port} (pfsense.exec_php).";
log_error($error);
$numberofruns = 5;
}
$numberofruns++;
}
}
global $g;
if (file_exists("{$g['varrun_path']}/booting"))
return;
if (is_array($config['installedpackages']['carpsettings']['config'])) {
update_filter_reload_status("Building CARP sync information");
foreach($config['installedpackages']['carpsettings']['config'] as $carp) {
if ($carp['synchronizetoip'] != "" ) {
/*
* 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 ($config['system']['webgui']['protocol'] != "") {
$synchronizetoip = $config['system']['webgui']['protocol'];
$synchronizetoip .= "://";
}
$port = $config['system']['webgui']['port'];
/* if port is empty lets rely on the protocol selection */
if ($port == "") {
if ($config['system']['webgui']['protocol'] == "http")
$port = "80";
else
$port = "443";
}
$synchronizetoip .= $carp['synchronizetoip'];
if ($carp['synchronizerules'] != "" and is_array($config['filter']))
$sections[] = 'filter';
if ($carp['synchronizenat'] != "" and is_array($config['nat']))
$sections[] = 'nat';
if ($carp['synchronizealiases'] != "" and is_array($config['aliases']))
$sections[] = 'aliases';
if ($carp['synchronizedhcpd'] != "" and is_array($config['dhcpd']))
$sections[] = 'dhcpd';
if ($carp['synchronizewol'] != "" and is_array($config['wol']))
$sections[] = 'wol';
if ($carp['synchronizetrafficshaper'] != "" and is_array($config['shaper']))
$sections[] = 'shaper';
if ($carp['synchronizestaticroutes'] != "" and is_array($config['staticroutes'])) {
$sections[] = 'staticroutes';
$sections[] = 'gateways';
}
if ($carp['synchronizevirtualip'] != "" and is_array($config['virtualip']))
$sections[] = 'virtualip';
if ($carp['synchronizelb'] != "" and is_array($config['load_balancer']))
$sections[] = 'load_balancer';
if ($carp['synchronizeipsec'] != "" and is_array($config['ipsec']))
$sections[] = 'ipsec';
if ($carp['synchronizeopenvpn'] != "" and is_array($config['openvpn'])) {
$sections[] = 'openvpn';
$mergesections[] = 'cert';
$mergesections[] = 'ca';
}
if ($carp['synchronizeusers'] != "") {
$mergesections[] = 'user';
$mergesections[] = 'group';
}
if ($carp['synchronizednsforwarder'] != "" and is_array($config['dnsmasq']))
$sections[] = 'dnsmasq';
if ($carp['synchronizeschedules'] != "" and is_array($config['schedules']))
$sections[] = 'schedules';
if (count($sections) > 0) {
update_filter_reload_status("Signaling CARP reload signal...");
carp_sync_xml($synchronizetoip, $carp['password'], $sections, $port);
if (is_array($mergesections))
carp_sync_xml($synchronizetoip, $carp['password'], $mergesections, $port, 'pfsense.merge_config_section');
$cli = new XML_RPC_Client('/xmlrpc.php', $synchronizetoip, $port);
$params = array(
XML_RPC_encode($carp['password'])
);
$msg = new XML_RPC_Message('pfsense.filter_configure', $params);
$username = $config['system']['user'][0]['name'];
$cli->setCredentials($username, $carp['password']);
$resp = $cli->send($msg, "900");
if(!$resp) {
$error = "A communications error occured while attempting Filter sync with username {$username} {$synchronizetoip}:{$port}.";
log_error($error);
file_notice("sync_settings", $error, "Settings Sync", "");
} elseif($resp->faultCode()) {
$error = "An error code was received while attempting Filter sync with username {$username} {$synchronizetoip}:{$port} - Code " . $resp->faultCode() . ": " . $resp->faultString();
log_error($error);
file_notice("sync_settings", $error, "Settings Sync", "");
} else {
log_error("Filter sync successfully completed with {$synchronizetoip}:{$port}.");
$numberofruns = 3;
}
/* signal a carp reload */
$cli = new XML_RPC_Client('/xmlrpc.php', $synchronizetoip, $port);
$msg = new XML_RPC_Message('pfsense.interfaces_carp_configure', $params);
$resp = $cli->send($msg, "900");
if(!$resp) {
$error = "A communications error occured while attempting CARP sync with username {$username} {$synchronizetoip}:{$port}.";
log_error($error);
file_notice("sync_settings", $error, "Settings Sync", "");
} elseif($resp->faultCode()) {
$error = "An error code was received while attempting CARP sync with username {$username} {$synchronizetoip}:{$port} - Code " . $resp->faultCode() . ": " . $resp->faultString();
log_error($error);
file_notice("sync_settings", $error, "Settings Sync", "");
} else {
log_error("CARP sync successfully completed with {$synchronizetoip}:{$port}.");
$numberofruns = 3;
}
}
break;
}
}
}
?>
|