Project

General

Profile

Download (4.63 KB) Statistics
| Branch: | Tag: | Revision:
1 cb7d18d5 Renato Botelho
#!/usr/local/bin/php-cgi -f
2 2b73d3a0 smos
<?php
3
/*
4 ac24dc24 Renato Botelho
 * rc.openvpn
5
 *
6
 * part of pfSense (https://www.pfsense.org)
7 c5d81585 Renato Botelho
 * Copyright (c) 2009 Seth Mos <seth.mos@dds.nl>.
8 38809d47 Renato Botelho do Couto
 * Copyright (c) 2007-2013 BSD Perimeter
9
 * Copyright (c) 2013-2016 Electric Sheep Fencing
10 8f585441 Luiz Souza
 * Copyright (c) 2014-2021 Rubicon Communications, LLC (Netgate)
11 ac24dc24 Renato Botelho
 * All rights reserved.
12
 *
13
 * Originally part of m0n0wall (http://m0n0.ch/wall)
14 c5d81585 Renato Botelho
 * Copyright (c) 2007 Manuel Kasper <mk@neon1.net>.
15 ac24dc24 Renato Botelho
 * All rights reserved.
16
 *
17 b12ea3fb Renato Botelho
 * 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 ac24dc24 Renato Botelho
 *
21 b12ea3fb Renato Botelho
 * http://www.apache.org/licenses/LICENSE-2.0
22 ac24dc24 Renato Botelho
 *
23 b12ea3fb Renato Botelho
 * 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 ac24dc24 Renato Botelho
 */
29 2b73d3a0 smos
30 8f10998b Ermal
/* 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 2b73d3a0 smos
38 e960b298 Phil Davis
function openvpn_resync_if_needed ($mode, $ovpn_settings, $interface) {
39
	global $g, $config;
40
41 f33dcc5c Ermal
	$resync_needed = true;
42 634054cc Phil Davis
	if (isset($ovpn_settings['disable'])) {
43
		$resync_needed = false;
44
	} else {
45
		if (!empty($interface)) {
46
			$mode_id = $mode . $ovpn_settings['vpnid'];
47 348c2af1 jim-p
			$fpath = "{$g['openvpn_base']}/{$mode_id}/interface";
48 634054cc Phil Davis
			if (file_exists($fpath)) {
49 a3cecbc3 jim-p
				/* 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 634054cc Phil Davis
				}
54 f33dcc5c Ermal
			}
55
		}
56 e960b298 Phil Davis
	}
57 f33dcc5c Ermal
	if ($resync_needed == true) {
58 e960b298 Phil Davis
		log_error("OpenVPN: Resync " . $mode_id . " " . $ovpn_settings['description']);
59
		openvpn_resync($mode, $ovpn_settings);
60
	}
61
}
62
63 8f10998b Ermal
/* make sure to wait until the boot scripts have finished */
64 e173dd74 Phil Davis
if (file_exists("{$g['varrun_path']}/booting")) {
65 1f64b66b Phil Davis
	return;
66 e173dd74 Phil Davis
}
67 8f10998b Ermal
68 7ef9de3f Phil Davis
/* Input argument is a comma-separated list of gateway names, blank or "all". */
69 e173dd74 Phil Davis
if (isset($_GET['interface'])) {
70 c71b14fd Ermal
	$argument = $_GET['interface'];
71 e173dd74 Phil Davis
} else {
72 c71b14fd Ermal
	$argument = trim($argv[1], " \n");
73 e173dd74 Phil Davis
}
74 8f10998b Ermal
75 e311cb79 PiBa-NL
// 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 e173dd74 Phil Davis
if ((is_array($config['openvpn']['openvpn-server']) && count($config['openvpn']['openvpn-server'])) ||
86 4e322e2c Phil Davis
    (is_array($config['openvpn']['openvpn-client']) && count($config['openvpn']['openvpn-client']))) {
87 7ef9de3f Phil Davis
	if (empty($argument) || $argument == "all") {
88 e173dd74 Phil Davis
		$argument = "all";
89 252612d7 Renato Botelho
		$log_text = "all";
90 7ef9de3f Phil Davis
	} else {
91 1f64b66b Phil Davis
		$log_text = "endpoints that may use " . $argument;
92 7ef9de3f Phil Davis
	}
93 1f64b66b Phil Davis
	log_error("OpenVPN: One or more OpenVPN tunnel endpoints may have changed its IP. Reloading " . $log_text . ".");
94 e173dd74 Phil Davis
} else {
95 8f10998b Ermal
	return;
96 e173dd74 Phil Davis
}
97 c653ce27 Ermal
98 8171a2c2 Ermal
$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 086cf944 Phil Davis
$arg_array = explode(",", $argument);
106 7ef9de3f Phil Davis
foreach ($arg_array as $arg_element) {
107
	$gwgroups = array();
108 e173dd74 Phil Davis
	if ($arg_element == "all") {
109
		$interface = "";
110
	} else {
111 7ef9de3f Phil Davis
		// e.g. $arg_element = "WANGW", $interface = "wan"
112
		$interface = lookup_gateway_interface_by_name($arg_element);
113 e173dd74 Phil Davis
		if (empty($interface)) {
114 7ef9de3f Phil Davis
			$interface = $arg_element;
115 e173dd74 Phil Davis
		} else {
116 7ef9de3f Phil Davis
			// e.g. $arg_element = "WANGW", $gwgroups = array of gateway groups that use "wan"
117
			$gwgroups = gateway_is_gwgroup_member($arg_element);
118 e173dd74 Phil Davis
		}
119 7ef9de3f Phil Davis
	}
120 8f10998b Ermal
121 e173dd74 Phil Davis
	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 7ef9de3f Phil Davis
				openvpn_resync_if_needed('server', $server, $interface);
125 e173dd74 Phil Davis
			}
126 7ef9de3f Phil Davis
		}
127 e288ddb1 smos
	}
128 8f10998b Ermal
129 7ef9de3f Phil Davis
	if (is_array($config['openvpn']['openvpn-client'])) {
130 e173dd74 Phil Davis
		foreach ($config['openvpn']['openvpn-client'] as &$client) {
131
			if ($client['interface'] == $interface || empty($interface) || (!empty($gwgroups) && in_array($client['interface'], $gwgroups))) {
132 7ef9de3f Phil Davis
				openvpn_resync_if_needed('client', $client, $interface);
133 e173dd74 Phil Davis
			}
134 7ef9de3f Phil Davis
		}
135 ea68f6cc jim-p
	}
136 8f10998b Ermal
}
137 b95f5460 Phil Davis
138 8f10998b Ermal
unlock($openvpnlck);
139 2b73d3a0 smos
?>