Project

General

Profile

Download (5.81 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-2018 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
 * Redistribution and use in source and binary forms, with or without
16
 * modification, are permitted provided that the following conditions are met:
17
 *
18
 * 1. Redistributions of source code must retain the above copyright notice,
19
 *    this list of conditions and the following disclaimer.
20
 *
21
 * 2. Redistributions in binary form must reproduce the above copyright
22
 *    notice, this list of conditions and the following disclaimer in
23
 *    the documentation and/or other materials provided with the
24
 *    distribution.
25
 *
26
 * 3. All advertising materials mentioning features or use of this software
27
 *    must display the following acknowledgment:
28
 *    "This product includes software developed by the pfSense Project
29
 *    for use in the pfSense® software distribution. (http://www.pfsense.org/).
30
 *
31
 * 4. The names "pfSense" and "pfSense Project" must not be used to
32
 *    endorse or promote products derived from this software without
33
 *    prior written permission. For written permission, please contact
34
 *    coreteam@pfsense.org.
35
 *
36
 * 5. Products derived from this software may not be called "pfSense"
37
 *    nor may "pfSense" appear in their names without prior written
38
 *    permission of the Electric Sheep Fencing, LLC.
39
 *
40
 * 6. Redistributions of any form whatsoever must retain the following
41
 *    acknowledgment:
42
 *
43
 * "This product includes software developed by the pfSense Project
44
 * for use in the pfSense software distribution (http://www.pfsense.org/).
45
 *
46
 * THIS SOFTWARE IS PROVIDED BY THE pfSense PROJECT ``AS IS'' AND ANY
47
 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
49
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE pfSense PROJECT OR
50
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
51
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
52
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
53
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
54
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
55
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
56
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
57
 * OF THE POSSIBILITY OF SUCH DAMAGE.
58
 */
59

    
60
/* parse the configuration and include all functions used below */
61
require_once("util.inc");
62
require_once("config.inc");
63
require_once("functions.inc");
64
require_once("filter.inc");
65
require_once("gwlb.inc");
66
require_once("openvpn.inc");
67

    
68
function openvpn_resync_if_needed ($mode, $ovpn_settings, $interface) {
69
	global $g, $config;
70

    
71
	$resync_needed = true;
72
	if (isset($ovpn_settings['disable'])) {
73
		$resync_needed = false;
74
	} else {
75
		if (!empty($interface)) {
76
			$mode_id = $mode . $ovpn_settings['vpnid'];
77
			$fpath = "{$g['varetc_path']}/openvpn/{$mode_id}.interface";
78
			if (file_exists($fpath)) {
79
				/* Compare the interface currently used by the VPN with the interface that should be used.
80
				   If the VPN should stay on the same interface, do not resync */
81
				if (trim(file_get_contents($fpath), " \t\n") == get_failover_interface($ovpn_settings['interface'])) {
82
					$resync_needed = false;
83
				}
84
			}
85
		}
86
	}
87
	if ($resync_needed == true) {
88
		log_error("OpenVPN: Resync " . $mode_id . " " . $ovpn_settings['description']);
89
		openvpn_resync($mode, $ovpn_settings);
90
	}
91
}
92

    
93
/* make sure to wait until the boot scripts have finished */
94
if (file_exists("{$g['varrun_path']}/booting")) {
95
	return;
96
}
97

    
98
/* Input argument is a comma-separated list of gateway names, blank or "all". */
99
if (isset($_GET['interface'])) {
100
	$argument = $_GET['interface'];
101
} else {
102
	$argument = trim($argv[1], " \n");
103
}
104

    
105
if ((is_array($config['openvpn']['openvpn-server']) && count($config['openvpn']['openvpn-server'])) ||
106
    (is_array($config['openvpn']['openvpn-client']) && count($config['openvpn']['openvpn-client']))) {
107
	if (empty($argument) || $argument == "all") {
108
		$argument = "all";
109
		$log_text = "all";
110
	} else {
111
		$log_text = "endpoints that may use " . $argument;
112
	}
113
	log_error("OpenVPN: One or more OpenVPN tunnel endpoints may have changed its IP. Reloading " . $log_text . ".");
114
} else {
115
	return;
116
}
117

    
118
$openvpnlck = try_lock('openvpn', 10);
119
if (!$openvpnlck) {
120
	log_error(gettext("Could not obtain openvpn lock for executing rc.openvpn for more than 10 seconds continuing..."));
121
	unlock_force('openvpn');
122
	$openvpnlck = lock('openvpn', LOCK_EX);
123
}
124

    
125
$arg_array = explode(",", $argument);
126
foreach ($arg_array as $arg_element) {
127
	$gwgroups = array();
128
	if ($arg_element == "all") {
129
		$interface = "";
130
	} else {
131
		// e.g. $arg_element = "WANGW", $interface = "wan"
132
		$interface = lookup_gateway_interface_by_name($arg_element);
133
		if (empty($interface)) {
134
			$interface = $arg_element;
135
		} else {
136
			// e.g. $arg_element = "WANGW", $gwgroups = array of gateway groups that use "wan"
137
			$gwgroups = gateway_is_gwgroup_member($arg_element);
138
		}
139
	}
140

    
141
	if (is_array($config['openvpn']['openvpn-server'])) {
142
		foreach ($config['openvpn']['openvpn-server'] as &$server) {
143
			if ($server['interface'] == $interface || empty($interface) || (!empty($gwgroups) && in_array($server['interface'], $gwgroups))) {
144
				openvpn_resync_if_needed('server', $server, $interface);
145
			}
146
		}
147
	}
148

    
149
	if (is_array($config['openvpn']['openvpn-client'])) {
150
		foreach ($config['openvpn']['openvpn-client'] as &$client) {
151
			if ($client['interface'] == $interface || empty($interface) || (!empty($gwgroups) && in_array($client['interface'], $gwgroups))) {
152
				openvpn_resync_if_needed('client', $client, $interface);
153
			}
154
		}
155
	}
156
}
157

    
158
unlock($openvpnlck);
159
?>
(71-71/95)