Project

General

Profile

Download (4.14 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 b8f91b7c Luiz Souza
 * Copyright (c) 2007-2018 Rubicon Communications, LLC (Netgate)
9 ac24dc24 Renato Botelho
 * All rights reserved.
10
 *
11
 * Originally part of m0n0wall (http://m0n0.ch/wall)
12 c5d81585 Renato Botelho
 * Copyright (c) 2007 Manuel Kasper <mk@neon1.net>.
13 ac24dc24 Renato Botelho
 * All rights reserved.
14
 *
15 b12ea3fb Renato Botelho
 * Licensed under the Apache License, Version 2.0 (the "License");
16
 * you may not use this file except in compliance with the License.
17
 * You may obtain a copy of the License at
18 ac24dc24 Renato Botelho
 *
19 b12ea3fb Renato Botelho
 * http://www.apache.org/licenses/LICENSE-2.0
20 ac24dc24 Renato Botelho
 *
21 b12ea3fb Renato Botelho
 * Unless required by applicable law or agreed to in writing, software
22
 * distributed under the License is distributed on an "AS IS" BASIS,
23
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24
 * See the License for the specific language governing permissions and
25
 * limitations under the License.
26 ac24dc24 Renato Botelho
 */
27 2b73d3a0 smos
28 8f10998b Ermal
/* parse the configuration and include all functions used below */
29
require_once("util.inc");
30
require_once("config.inc");
31
require_once("functions.inc");
32
require_once("filter.inc");
33
require_once("gwlb.inc");
34
require_once("openvpn.inc");
35 2b73d3a0 smos
36 e960b298 Phil Davis
function openvpn_resync_if_needed ($mode, $ovpn_settings, $interface) {
37
	global $g, $config;
38
39 f33dcc5c Ermal
	$resync_needed = true;
40 634054cc Phil Davis
	if (isset($ovpn_settings['disable'])) {
41
		$resync_needed = false;
42
	} else {
43
		if (!empty($interface)) {
44
			$mode_id = $mode . $ovpn_settings['vpnid'];
45
			$fpath = "{$g['varetc_path']}/openvpn/{$mode_id}.interface";
46
			if (file_exists($fpath)) {
47 a3cecbc3 jim-p
				/* Compare the interface currently used by the VPN with the interface that should be used.
48
				   If the VPN should stay on the same interface, do not resync */
49
				if (trim(file_get_contents($fpath), " \t\n") == get_failover_interface($ovpn_settings['interface'])) {
50
					$resync_needed = false;
51 634054cc Phil Davis
				}
52 f33dcc5c Ermal
			}
53
		}
54 e960b298 Phil Davis
	}
55 f33dcc5c Ermal
	if ($resync_needed == true) {
56 e960b298 Phil Davis
		log_error("OpenVPN: Resync " . $mode_id . " " . $ovpn_settings['description']);
57
		openvpn_resync($mode, $ovpn_settings);
58
	}
59
}
60
61 8f10998b Ermal
/* make sure to wait until the boot scripts have finished */
62 e173dd74 Phil Davis
if (file_exists("{$g['varrun_path']}/booting")) {
63 1f64b66b Phil Davis
	return;
64 e173dd74 Phil Davis
}
65 8f10998b Ermal
66 7ef9de3f Phil Davis
/* Input argument is a comma-separated list of gateway names, blank or "all". */
67 e173dd74 Phil Davis
if (isset($_GET['interface'])) {
68 c71b14fd Ermal
	$argument = $_GET['interface'];
69 e173dd74 Phil Davis
} else {
70 c71b14fd Ermal
	$argument = trim($argv[1], " \n");
71 e173dd74 Phil Davis
}
72 8f10998b Ermal
73 e173dd74 Phil Davis
if ((is_array($config['openvpn']['openvpn-server']) && count($config['openvpn']['openvpn-server'])) ||
74 4e322e2c Phil Davis
    (is_array($config['openvpn']['openvpn-client']) && count($config['openvpn']['openvpn-client']))) {
75 7ef9de3f Phil Davis
	if (empty($argument) || $argument == "all") {
76 e173dd74 Phil Davis
		$argument = "all";
77 252612d7 Renato Botelho
		$log_text = "all";
78 7ef9de3f Phil Davis
	} else {
79 1f64b66b Phil Davis
		$log_text = "endpoints that may use " . $argument;
80 7ef9de3f Phil Davis
	}
81 1f64b66b Phil Davis
	log_error("OpenVPN: One or more OpenVPN tunnel endpoints may have changed its IP. Reloading " . $log_text . ".");
82 e173dd74 Phil Davis
} else {
83 8f10998b Ermal
	return;
84 e173dd74 Phil Davis
}
85 c653ce27 Ermal
86 8171a2c2 Ermal
$openvpnlck = try_lock('openvpn', 10);
87
if (!$openvpnlck) {
88
	log_error(gettext("Could not obtain openvpn lock for executing rc.openvpn for more than 10 seconds continuing..."));
89
	unlock_force('openvpn');
90
	$openvpnlck = lock('openvpn', LOCK_EX);
91
}
92
93 086cf944 Phil Davis
$arg_array = explode(",", $argument);
94 7ef9de3f Phil Davis
foreach ($arg_array as $arg_element) {
95
	$gwgroups = array();
96 e173dd74 Phil Davis
	if ($arg_element == "all") {
97
		$interface = "";
98
	} else {
99 7ef9de3f Phil Davis
		// e.g. $arg_element = "WANGW", $interface = "wan"
100
		$interface = lookup_gateway_interface_by_name($arg_element);
101 e173dd74 Phil Davis
		if (empty($interface)) {
102 7ef9de3f Phil Davis
			$interface = $arg_element;
103 e173dd74 Phil Davis
		} else {
104 7ef9de3f Phil Davis
			// e.g. $arg_element = "WANGW", $gwgroups = array of gateway groups that use "wan"
105
			$gwgroups = gateway_is_gwgroup_member($arg_element);
106 e173dd74 Phil Davis
		}
107 7ef9de3f Phil Davis
	}
108 8f10998b Ermal
109 e173dd74 Phil Davis
	if (is_array($config['openvpn']['openvpn-server'])) {
110
		foreach ($config['openvpn']['openvpn-server'] as &$server) {
111
			if ($server['interface'] == $interface || empty($interface) || (!empty($gwgroups) && in_array($server['interface'], $gwgroups))) {
112 7ef9de3f Phil Davis
				openvpn_resync_if_needed('server', $server, $interface);
113 e173dd74 Phil Davis
			}
114 7ef9de3f Phil Davis
		}
115 e288ddb1 smos
	}
116 8f10998b Ermal
117 7ef9de3f Phil Davis
	if (is_array($config['openvpn']['openvpn-client'])) {
118 e173dd74 Phil Davis
		foreach ($config['openvpn']['openvpn-client'] as &$client) {
119
			if ($client['interface'] == $interface || empty($interface) || (!empty($gwgroups) && in_array($client['interface'], $gwgroups))) {
120 7ef9de3f Phil Davis
				openvpn_resync_if_needed('client', $client, $interface);
121 e173dd74 Phil Davis
			}
122 7ef9de3f Phil Davis
		}
123 ea68f6cc jim-p
	}
124 8f10998b Ermal
}
125 b95f5460 Phil Davis
126 8f10998b Ermal
unlock($openvpnlck);
127 2b73d3a0 smos
?>