Project

General

Profile

Download (4.14 KB) Statistics
| Branch: | Tag: | Revision:
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-2016 Rubicon Communications, LLC (Netgate)
9
 * All rights reserved.
10
 *
11
 * Originally part of m0n0wall (http://m0n0.ch/wall)
12
 * Copyright (c) 2007 Manuel Kasper <mk@neon1.net>.
13
 * All rights reserved.
14
 *
15
 * 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
 *
19
 * http://www.apache.org/licenses/LICENSE-2.0
20
 *
21
 * 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
 */
27

    
28
/* 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

    
36
function openvpn_resync_if_needed ($mode, $ovpn_settings, $interface) {
37
	global $g, $config;
38

    
39
	$resync_needed = true;
40
	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
				/* 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
				}
52
			}
53
		}
54
	}
55
	if ($resync_needed == true) {
56
		log_error("OpenVPN: Resync " . $mode_id . " " . $ovpn_settings['description']);
57
		openvpn_resync($mode, $ovpn_settings);
58
	}
59
}
60

    
61
/* make sure to wait until the boot scripts have finished */
62
if (file_exists("{$g['varrun_path']}/booting")) {
63
	return;
64
}
65

    
66
/* Input argument is a comma-separated list of gateway names, blank or "all". */
67
if (isset($_GET['interface'])) {
68
	$argument = $_GET['interface'];
69
} else {
70
	$argument = trim($argv[1], " \n");
71
}
72

    
73
if ((is_array($config['openvpn']['openvpn-server']) && count($config['openvpn']['openvpn-server'])) ||
74
    (is_array($config['openvpn']['openvpn-client']) && count($config['openvpn']['openvpn-client']))) {
75
	if (empty($argument) || $argument == "all") {
76
		$argument = "all";
77
		$log_text = "all";
78
	} else {
79
		$log_text = "endpoints that may use " . $argument;
80
	}
81
	log_error("OpenVPN: One or more OpenVPN tunnel endpoints may have changed its IP. Reloading " . $log_text . ".");
82
} else {
83
	return;
84
}
85

    
86
$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
$arg_array = explode(",", $argument);
94
foreach ($arg_array as $arg_element) {
95
	$gwgroups = array();
96
	if ($arg_element == "all") {
97
		$interface = "";
98
	} else {
99
		// e.g. $arg_element = "WANGW", $interface = "wan"
100
		$interface = lookup_gateway_interface_by_name($arg_element);
101
		if (empty($interface)) {
102
			$interface = $arg_element;
103
		} else {
104
			// e.g. $arg_element = "WANGW", $gwgroups = array of gateway groups that use "wan"
105
			$gwgroups = gateway_is_gwgroup_member($arg_element);
106
		}
107
	}
108

    
109
	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
				openvpn_resync_if_needed('server', $server, $interface);
113
			}
114
		}
115
	}
116

    
117
	if (is_array($config['openvpn']['openvpn-client'])) {
118
		foreach ($config['openvpn']['openvpn-client'] as &$client) {
119
			if ($client['interface'] == $interface || empty($interface) || (!empty($gwgroups) && in_array($client['interface'], $gwgroups))) {
120
				openvpn_resync_if_needed('client', $client, $interface);
121
			}
122
		}
123
	}
124
}
125

    
126
unlock($openvpnlck);
127
?>
(58-58/79)