Project

General

Profile

Download (4.54 KB) Statistics
| Branch: | Tag: | Revision:
1 2b73d3a0 smos
#!/usr/local/bin/php -f
2
<?php
3
/*
4
	$Id$
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 8f10998b Ermal
/* 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 2b73d3a0 smos
41 e960b298 Phil Davis
function openvpn_resync_if_needed ($mode, $ovpn_settings, $interface) {
42
	global $g, $config;
43
44 f33dcc5c Ermal
	$resync_needed = true;
45 634054cc Phil Davis
	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
				$current_device = file_get_contents($fpath);
53
				$current_device = trim($current_device, " \t\n");
54
				$new_device = get_failover_interface($ovpn_settings['interface']);
55
				if (isset($config['interfaces'][$interface])) {
56
					$this_device = $config['interfaces'][$interface]['if'];
57 feb2e013 Phil Davis
					if (($current_device == $new_device) && ($current_device != $this_device))
58 634054cc Phil Davis
						$resync_needed = false;
59
				}
60 f33dcc5c Ermal
			}
61
		}
62 e960b298 Phil Davis
	}
63 f33dcc5c Ermal
	if ($resync_needed == true) {
64 e960b298 Phil Davis
		log_error("OpenVPN: Resync " . $mode_id . " " . $ovpn_settings['description']);
65
		openvpn_resync($mode, $ovpn_settings);
66
	}
67
}
68
69 8f10998b Ermal
/* make sure to wait until the boot scripts have finished */
70
if (file_exists("{$g['varrun_path']}/booting"))
71 1f64b66b Phil Davis
	return;
72 8f10998b Ermal
73 7ef9de3f Phil Davis
/* Input argument is a comma-separated list of gateway names, blank or "all". */
74 c71b14fd Ermal
if (isset($_GET))
75
	$argument = $_GET['interface'];
76
else
77
	$argument = trim($argv[1], " \n");
78 8f10998b Ermal
79 252612d7 Renato Botelho
if(is_array($config['openvpn']['openvpn-server']) || is_array($config['openvpn']['openvpn-client'])) {
80 7ef9de3f Phil Davis
	if (empty($argument) || $argument == "all") {
81
		$argument = "all"; 
82 252612d7 Renato Botelho
		$log_text = "all";
83 7ef9de3f Phil Davis
	} else {
84 1f64b66b Phil Davis
		$log_text = "endpoints that may use " . $argument;
85 7ef9de3f Phil Davis
	}
86 1f64b66b Phil Davis
	log_error("OpenVPN: One or more OpenVPN tunnel endpoints may have changed its IP. Reloading " . $log_text . ".");
87
} else
88 8f10998b Ermal
	return;
89 c653ce27 Ermal
90 8171a2c2 Ermal
$openvpnlck = try_lock('openvpn', 10);
91
if (!$openvpnlck) {
92
	log_error(gettext("Could not obtain openvpn lock for executing rc.openvpn for more than 10 seconds continuing..."));
93
	unlock_force('openvpn');
94
	$openvpnlck = lock('openvpn', LOCK_EX);
95
}
96
97 7ef9de3f Phil Davis
$arg_array = explode(",",$argument);
98
foreach ($arg_array as $arg_element) {
99
	$gwgroups = array();
100
	if ($arg_element == "all")
101
		$interface = ""; 
102
	else {
103
		// e.g. $arg_element = "WANGW", $interface = "wan"
104
		$interface = lookup_gateway_interface_by_name($arg_element);
105
		if (empty($interface))
106
			$interface = $arg_element;
107
		else
108
			// e.g. $arg_element = "WANGW", $gwgroups = array of gateway groups that use "wan"
109
			$gwgroups = gateway_is_gwgroup_member($arg_element);
110
	}
111 8f10998b Ermal
112 7ef9de3f Phil Davis
	if(is_array($config['openvpn']['openvpn-server'])) {
113
		foreach($config['openvpn']['openvpn-server'] as &$server) {
114
			if ($server['interface'] == $interface || empty($interface) || (!empty($gwgroups) && in_array($server['interface'], $gwgroups)))
115
				openvpn_resync_if_needed('server', $server, $interface);
116
		}
117 e288ddb1 smos
	}
118 8f10998b Ermal
119 7ef9de3f Phil Davis
	if (is_array($config['openvpn']['openvpn-client'])) {
120
		foreach($config['openvpn']['openvpn-client'] as &$client) {
121
			if ($client['interface'] == $interface || empty($interface) || (!empty($gwgroups) && in_array($client['interface'], $gwgroups)))
122
				openvpn_resync_if_needed('client', $client, $interface);
123
		}
124 ea68f6cc jim-p
	}
125 8f10998b Ermal
}
126 b95f5460 Phil Davis
127 8f10998b Ermal
unlock($openvpnlck);
128 2b73d3a0 smos
?>