Project

General

Profile

Download (4.57 KB) Statistics
| Branch: | Tag: | Revision:
1
#!/usr/local/bin/php -f
2
<?php
3
/*
4
	rc.openvpn
5
	part of m0n0wall (http://m0n0.ch/wall)
6
	
7
	Copyright (C) 2007 Manuel Kasper <mk@neon1.net>.
8
	Copyright (C) 2009 Seth Mos <seth.mos@dds.nl>.
9
	All rights reserved.
10
	
11
	Redistribution and use in source and binary forms, with or without
12
	modification, are permitted provided that the following conditions are met:
13
	
14
	1. Redistributions of source code must retain the above copyright notice,
15
	   this list of conditions and the following disclaimer.
16
	
17
	2. Redistributions in binary form must reproduce the above copyright
18
	   notice, this list of conditions and the following disclaimer in the
19
	   documentation and/or other materials provided with the distribution.
20
	
21
	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
22
	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
23
	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24
	AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
25
	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30
	POSSIBILITY OF SUCH DAMAGE.
31
*/
32

    
33
/* parse the configuration and include all functions used below */
34
require_once("util.inc");
35
require_once("config.inc");
36
require_once("functions.inc");
37
require_once("filter.inc");
38
require_once("gwlb.inc");
39
require_once("openvpn.inc");
40

    
41
function openvpn_resync_if_needed ($mode, $ovpn_settings, $interface) {
42
	global $g, $config;
43

    
44
	$resync_needed = true;
45
	if (isset($ovpn_settings['disable'])) {
46
		$resync_needed = false;
47
	} else {
48
		if (!empty($interface)) {
49
			$mode_id = $mode . $ovpn_settings['vpnid'];
50
			$fpath = "{$g['varetc_path']}/openvpn/{$mode_id}.interface";
51
			if (file_exists($fpath)) {
52
				/* Compare the interface currently used by the VPN with the interface that should be used.
53
				   If the VPN should stay on the same interface, do not resync */
54
				if (trim(file_get_contents($fpath), " \t\n") == get_failover_interface($ovpn_settings['interface'])) {
55
					$resync_needed = false;
56
				}
57
			}
58
		}
59
	}
60
	if ($resync_needed == true) {
61
		log_error("OpenVPN: Resync " . $mode_id . " " . $ovpn_settings['description']);
62
		openvpn_resync($mode, $ovpn_settings);
63
	}
64
}
65

    
66
/* make sure to wait until the boot scripts have finished */
67
if (file_exists("{$g['varrun_path']}/booting"))
68
	return;
69

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

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

    
88
$openvpnlck = try_lock('openvpn', 10);
89
if (!$openvpnlck) {
90
	log_error(gettext("Could not obtain openvpn lock for executing rc.openvpn for more than 10 seconds continuing..."));
91
	unlock_force('openvpn');
92
	$openvpnlck = lock('openvpn', LOCK_EX);
93
}
94

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

    
110
	if(is_array($config['openvpn']['openvpn-server'])) {
111
		foreach($config['openvpn']['openvpn-server'] as &$server) {
112
			if ($server['interface'] == $interface || empty($interface) || (!empty($gwgroups) && in_array($server['interface'], $gwgroups)))
113
				openvpn_resync_if_needed('server', $server, $interface);
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
unlock($openvpnlck);
126
?>
(78-78/103)