1
|
#!/usr/local/bin/php-cgi -f
|
2
|
<?php
|
3
|
/*
|
4
|
* rc.openvpn
|
5
|
*
|
6
|
* part of pfSense (https://www.pfsense.org)
|
7
|
* Copyright (c) 2009 Seth Mos <seth.mos@dds.nl>.
|
8
|
* Copyright (c) 2007-2013 BSD Perimeter
|
9
|
* Copyright (c) 2013-2016 Electric Sheep Fencing
|
10
|
* Copyright (c) 2014-2021 Rubicon Communications, LLC (Netgate)
|
11
|
* All rights reserved.
|
12
|
*
|
13
|
* Originally part of m0n0wall (http://m0n0.ch/wall)
|
14
|
* Copyright (c) 2007 Manuel Kasper <mk@neon1.net>.
|
15
|
* All rights reserved.
|
16
|
*
|
17
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
18
|
* you may not use this file except in compliance with the License.
|
19
|
* You may obtain a copy of the License at
|
20
|
*
|
21
|
* http://www.apache.org/licenses/LICENSE-2.0
|
22
|
*
|
23
|
* Unless required by applicable law or agreed to in writing, software
|
24
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
25
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
26
|
* See the License for the specific language governing permissions and
|
27
|
* limitations under the License.
|
28
|
*/
|
29
|
|
30
|
/* parse the configuration and include all functions used below */
|
31
|
require_once("util.inc");
|
32
|
require_once("config.inc");
|
33
|
require_once("functions.inc");
|
34
|
require_once("filter.inc");
|
35
|
require_once("gwlb.inc");
|
36
|
require_once("openvpn.inc");
|
37
|
|
38
|
function openvpn_resync_if_needed ($mode, $ovpn_settings, $interface) {
|
39
|
global $g, $config;
|
40
|
|
41
|
$resync_needed = true;
|
42
|
if (isset($ovpn_settings['disable'])) {
|
43
|
$resync_needed = false;
|
44
|
} else {
|
45
|
if (!empty($interface)) {
|
46
|
$mode_id = $mode . $ovpn_settings['vpnid'];
|
47
|
$fpath = "{$g['openvpn_base']}/{$mode_id}/interface";
|
48
|
if (file_exists($fpath)) {
|
49
|
/* Compare the interface currently used by the VPN with the interface that should be used.
|
50
|
If the VPN should stay on the same interface, do not resync */
|
51
|
if (trim(file_get_contents($fpath), " \t\n") == get_failover_interface($ovpn_settings['interface'])) {
|
52
|
$resync_needed = false;
|
53
|
}
|
54
|
}
|
55
|
}
|
56
|
}
|
57
|
if ($resync_needed == true) {
|
58
|
log_error("OpenVPN: Resync " . $mode_id . " " . $ovpn_settings['description']);
|
59
|
openvpn_resync($mode, $ovpn_settings);
|
60
|
}
|
61
|
}
|
62
|
|
63
|
/* make sure to wait until the boot scripts have finished */
|
64
|
if (file_exists("{$g['varrun_path']}/booting")) {
|
65
|
return;
|
66
|
}
|
67
|
|
68
|
/* Input argument is a comma-separated list of gateway names, blank or "all". */
|
69
|
if (isset($_GET['interface'])) {
|
70
|
$argument = $_GET['interface'];
|
71
|
} else {
|
72
|
$argument = trim($argv[1], " \n");
|
73
|
}
|
74
|
|
75
|
// Workaround as rc.openvpn is called by the gatewaymonitor / check_reload event system,..
|
76
|
//file_put_contents("/dev/console", "\n[".getmypid()."] OPENVPN CALLED ".$argument);
|
77
|
$gateways_arr = return_gateways_array();
|
78
|
if (isset($gateways_arr[$argument])) {
|
79
|
//file_put_contents("/dev/console", "\n[".getmypid()."] OPENVPN CALLED WITH GW:".$argument);
|
80
|
// called upon rc.gateway_alarm
|
81
|
system_routing_configure();
|
82
|
}
|
83
|
|
84
|
|
85
|
if ((is_array($config['openvpn']['openvpn-server']) && count($config['openvpn']['openvpn-server'])) ||
|
86
|
(is_array($config['openvpn']['openvpn-client']) && count($config['openvpn']['openvpn-client']))) {
|
87
|
if (empty($argument) || $argument == "all") {
|
88
|
$argument = "all";
|
89
|
$log_text = "all";
|
90
|
} else {
|
91
|
$log_text = "endpoints that may use " . $argument;
|
92
|
}
|
93
|
log_error("OpenVPN: One or more OpenVPN tunnel endpoints may have changed its IP. Reloading " . $log_text . ".");
|
94
|
} else {
|
95
|
return;
|
96
|
}
|
97
|
|
98
|
$openvpnlck = try_lock('openvpn', 10);
|
99
|
if (!$openvpnlck) {
|
100
|
log_error(gettext("Could not obtain openvpn lock for executing rc.openvpn for more than 10 seconds continuing..."));
|
101
|
unlock_force('openvpn');
|
102
|
$openvpnlck = lock('openvpn', LOCK_EX);
|
103
|
}
|
104
|
|
105
|
$arg_array = explode(",", $argument);
|
106
|
foreach ($arg_array as $arg_element) {
|
107
|
$gwgroups = array();
|
108
|
if ($arg_element == "all") {
|
109
|
$interface = "";
|
110
|
} else {
|
111
|
// e.g. $arg_element = "WANGW", $interface = "wan"
|
112
|
$interface = lookup_gateway_interface_by_name($arg_element);
|
113
|
if (empty($interface)) {
|
114
|
$interface = $arg_element;
|
115
|
} else {
|
116
|
// e.g. $arg_element = "WANGW", $gwgroups = array of gateway groups that use "wan"
|
117
|
$gwgroups = gateway_is_gwgroup_member($arg_element);
|
118
|
}
|
119
|
}
|
120
|
|
121
|
if (is_array($config['openvpn']['openvpn-server'])) {
|
122
|
foreach ($config['openvpn']['openvpn-server'] as &$server) {
|
123
|
if ($server['interface'] == $interface || empty($interface) || (!empty($gwgroups) && in_array($server['interface'], $gwgroups))) {
|
124
|
openvpn_resync_if_needed('server', $server, $interface);
|
125
|
}
|
126
|
}
|
127
|
}
|
128
|
|
129
|
if (is_array($config['openvpn']['openvpn-client'])) {
|
130
|
foreach ($config['openvpn']['openvpn-client'] as &$client) {
|
131
|
if ($client['interface'] == $interface || empty($interface) || (!empty($gwgroups) && in_array($client['interface'], $gwgroups))) {
|
132
|
openvpn_resync_if_needed('client', $client, $interface);
|
133
|
}
|
134
|
}
|
135
|
}
|
136
|
}
|
137
|
|
138
|
unlock($openvpnlck);
|
139
|
?>
|