Project

General

Profile

Download (50.9 KB) Statistics
| Branch: | Tag: | Revision:
1 5b237745 Scott Ullrich
<?php
2 b1ad443d Scott Ullrich
/*
3 ac24dc24 Renato Botelho
 * openvpn.inc
4
 *
5
 * part of pfSense (https://www.pfsense.org)
6 c5d81585 Renato Botelho
 * Copyright (c) 2006 Fernando Lemos
7 81299b5c Renato Botelho
 * Copyright (c) 2006-2016 Rubicon Communications, LLC (Netgate)
8 ac24dc24 Renato Botelho
 * All rights reserved.
9
 *
10
 * This file was rewritten from scratch by Fernando Lemos but
11
 * *MIGHT* contain code previously written by:
12
 *
13 c5d81585 Renato Botelho
 * Copyright (c) 2005 Peter Allgeyer <allgeyer_AT_web.de>
14
 * Copyright (c) 2004 Peter Curran (peter@closeconsultants.com).
15 ac24dc24 Renato Botelho
 * All rights reserved.
16
 *
17 b12ea3fb Renato Botelho
 * Licensed under the Apache License, Version 2.0 (the "License");
18
 * you may not use this file except in compliance with the License.
19
 * You may obtain a copy of the License at
20 ac24dc24 Renato Botelho
 *
21 b12ea3fb Renato Botelho
 * http://www.apache.org/licenses/LICENSE-2.0
22 ac24dc24 Renato Botelho
 *
23 b12ea3fb Renato Botelho
 * Unless required by applicable law or agreed to in writing, software
24
 * distributed under the License is distributed on an "AS IS" BASIS,
25
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
26
 * See the License for the specific language governing permissions and
27
 * limitations under the License.
28 ac24dc24 Renato Botelho
 */
29 6aa3723a Renato Botelho
30 8dc3ef67 Scott Ullrich
require_once('config.inc');
31 32a7a1f6 Ermal Lu?i
require_once("certs.inc");
32 36df0acc Scott Ullrich
require_once('pfsense-utils.inc');
33 c61e4626 Ermal Lu?i
require_once("auth.inc");
34 8dc3ef67 Scott Ullrich
35 f2291484 jim-p
global $openvpn_prots;
36 6714bbdc jim-p
$openvpn_prots = array("UDP", "UDP6", "TCP", "TCP6");
37 702a4702 Scott Ullrich
38 f2291484 jim-p
global $openvpn_dev_mode;
39 691fbf14 Ermal Lu?i
$openvpn_dev_mode = array("tun", "tap");
40
41 b9e9903d Dmitriy K.
global $openvpn_verbosity_level;
42
$openvpn_verbosity_level = array(
43 e8c516a0 Phil Davis
	0 =>	gettext("none"),
44
	1 =>	gettext("default"),
45 42bb1bee Renato Botelho
	2 =>	"2",
46 e8c516a0 Phil Davis
	3 =>	gettext("3 (recommended)"),
47 b9e9903d Dmitriy K.
	4 =>	"4",
48
	5 => 	"5",
49
	6 => 	"6",
50
	7 => 	"7",
51
	8 => 	"8",
52
	9 => 	"9",
53
	10 => 	"10",
54
	11 => 	"11"
55 42bb1bee Renato Botelho
);
56 b9e9903d Dmitriy K.
57 42bb1bee Renato Botelho
/*
58 3c11bd3c Matthew Grooms
 * The User Auth mode below is disabled because
59
 * OpenVPN erroneously requires that we provide
60
 * a CA configuration parameter. In this mode,
61
 * clients don't send a certificate so there is
62
 * no need for a CA. If we require that admins
63
 * provide one in the pfSense UI due to a bogus
64
 * requirement imposed by OpenVPN, it could be
65
 * considered very confusing ( I know I was ).
66
 *
67
 * -mgrooms
68
 */
69
70 f2291484 jim-p
global $openvpn_dh_lengths;
71 fe787fc7 Matthew Grooms
$openvpn_dh_lengths = array(
72 0693c967 stilez
	1024, 2048, 3072, 4096, 7680, 8192, 15360, 16384
73
);
74 fe787fc7 Matthew Grooms
75 f2291484 jim-p
global $openvpn_cert_depths;
76 98963f27 jim-p
$openvpn_cert_depths = array(
77 e8c516a0 Phil Davis
	1 => gettext("One (Client+Server)"),
78
	2 => gettext("Two (Client+Intermediate+Server)"),
79
	3 => gettext("Three (Client+2xIntermediate+Server)"),
80
	4 => gettext("Four (Client+3xIntermediate+Server)"),
81
	5 => gettext("Five (Client+4xIntermediate+Server)")
82 98963f27 jim-p
);
83
84 f2291484 jim-p
global $openvpn_server_modes;
85 3c11bd3c Matthew Grooms
$openvpn_server_modes = array(
86 4aa02281 Carlos Eduardo Ramos
	'p2p_tls' => gettext("Peer to Peer ( SSL/TLS )"),
87
	'p2p_shared_key' => gettext("Peer to Peer ( Shared Key )"),
88
	'server_tls' => gettext("Remote Access ( SSL/TLS )"),
89
	'server_user' => gettext("Remote Access ( User Auth )"),
90
	'server_tls_user' => gettext("Remote Access ( SSL/TLS + User Auth )"));
91 3c11bd3c Matthew Grooms
92 154b0f80 jim-p
global $openvpn_tls_server_modes;
93
$openvpn_tls_server_modes = array('p2p_tls', 'server_tls', 'server_user', 'server_tls_user');
94
95 f2291484 jim-p
global $openvpn_client_modes;
96 3c11bd3c Matthew Grooms
$openvpn_client_modes = array(
97 4aa02281 Carlos Eduardo Ramos
	'p2p_tls' => gettext("Peer to Peer ( SSL/TLS )"),
98 ef00af3c Phil Davis
	'p2p_shared_key' => gettext("Peer to Peer ( Shared Key )"));
99 3c11bd3c Matthew Grooms
100 edba1982 jim-p
global $openvpn_compression_modes;
101
$openvpn_compression_modes = array(
102 ef00af3c Phil Davis
	'' => gettext("No Preference"),
103 9446ee68 jim-p
	'noadapt' => gettext("No Preference and Adaptive Compression Disabled"),
104 ef00af3c Phil Davis
	'no' => gettext("Disabled - No Compression"),
105
	'adaptive' => gettext("Enabled with Adaptive Compression"),
106
	'yes' => gettext("Enabled without Adaptive Compression"));
107 edba1982 jim-p
108 88cfb255 jim-p
global $openvpn_topologies;
109
$openvpn_topologies = array(
110
	'subnet' => gettext("Subnet -- One IP address per client in a common subnet"),
111
	'net30' => gettext("net30 -- Isolated /30 network per client")
112
//	'p2p => gettext("Peer to Peer -- One IP address per client peer-to-peer style. Does not work on Windows.")
113
);
114
115 f4eec250 Phil Davis
function openvpn_build_mode_list() {
116
	global $openvpn_server_modes;
117
118
	$list = array();
119
120 4e322e2c Phil Davis
	foreach ($openvpn_server_modes as $name => $desc) {
121 f4eec250 Phil Davis
		$list[$name] = $desc;
122 4e322e2c Phil Davis
	}
123 f4eec250 Phil Davis
124
	return($list);
125
}
126
127
function openvpn_build_if_list() {
128
	$list = array();
129
130
	$interfaces = get_configured_interface_with_descr();
131 2a5960b0 Luiz Otavio O Souza
	$viplist = get_configured_vip_list();
132
	foreach ($viplist as $vip => $address) {
133
		$interfaces[$vip.'|'.$address] = $address;
134
		if (get_vip_descr($address)) {
135
			$interfaces[$vip.'|'.$address] .= " (";
136
			$interfaces[$vip.'|'.$address] .= get_vip_descr($address);
137
			$interfaces[$vip.'|'.$address] .= ")";
138
		}
139 4e322e2c Phil Davis
	}
140 f4eec250 Phil Davis
141
	$grouplist = return_gateway_groups_array();
142
	foreach ($grouplist as $name => $group) {
143 4e322e2c Phil Davis
		if ($group[0]['vip'] != "") {
144 f4eec250 Phil Davis
			$vipif = $group[0]['vip'];
145 4e322e2c Phil Davis
		} else {
146 f4eec250 Phil Davis
			$vipif = $group[0]['int'];
147 4e322e2c Phil Davis
		}
148 f4eec250 Phil Davis
149
		$interfaces[$name] = "GW Group {$name}";
150
	}
151
152
	$interfaces['lo0'] = "Localhost";
153
	$interfaces['any'] = "any";
154
155 4e322e2c Phil Davis
	foreach ($interfaces as $iface => $ifacename) {
156 f4eec250 Phil Davis
	   $list[$iface] = $ifacename;
157 4e322e2c Phil Davis
	}
158 f4eec250 Phil Davis
159
	return($list);
160
}
161
162
function openvpn_build_crl_list() {
163
	global $a_crl;
164
165
	$list = array('' => 'None');
166
167
	foreach ($a_crl as $crl) {
168
		$caname = "";
169
		$ca = lookup_ca($crl['caref']);
170
171 4e322e2c Phil Davis
		if ($ca) {
172 f4eec250 Phil Davis
			$caname = " (CA: {$ca['descr']})";
173 4e322e2c Phil Davis
		}
174 f4eec250 Phil Davis
175
		$list[$crl['refid']] = $crl['descr'] . $caname;
176
	}
177
178
	return($list);
179
}
180
181 0e9d4a6d jim-p
function openvpn_build_cert_list($include_none = false, $prioritize_server_certs = false) {
182 f4eec250 Phil Davis
	global $a_cert;
183
184
	if ($include_none) {
185 e8c516a0 Phil Davis
		$list = array('' => gettext('None (Username and/or Password required)'));
186 f4eec250 Phil Davis
	} else {
187
		$list = array();
188
	}
189
190 a4ba0282 Stephen Beaver
	$non_server_list = array();
191
192 0e9d4a6d jim-p
	if ($prioritize_server_certs) {
193 e8c516a0 Phil Davis
		$list[' '] = gettext("===== Server Certificates =====");
194
		$non_server_list['  '] = gettext("===== Non-Server Certificates =====");
195 0e9d4a6d jim-p
	}
196
197 f4eec250 Phil Davis
	foreach ($a_cert as $cert) {
198 0e9d4a6d jim-p
		$properties = array();
199
		$propstr = "";
200 f4eec250 Phil Davis
		$ca = lookup_ca($cert['caref']);
201 0e9d4a6d jim-p
		$purpose = cert_get_purpose($cert['crt'], true);
202 f4eec250 Phil Davis
203 0e9d4a6d jim-p
		if ($purpose['server'] == "Yes") {
204 e8c516a0 Phil Davis
			$properties[] = gettext("Server: Yes");
205 0e9d4a6d jim-p
		} elseif ($prioritize_server_certs) {
206 e8c516a0 Phil Davis
			$properties[] = gettext("Server: NO");
207 0e9d4a6d jim-p
		}
208 de1a3167 jim-p
		if ($ca) {
209 e8c516a0 Phil Davis
			$properties[] = sprintf(gettext("CA: %s"), $ca['descr']);
210 de1a3167 jim-p
		}
211 0e9d4a6d jim-p
		if (cert_in_use($cert['refid'])) {
212 e8c516a0 Phil Davis
			$properties[] = gettext("In Use");
213 0e9d4a6d jim-p
		}
214
		if (is_cert_revoked($cert)) {
215 e8c516a0 Phil Davis
			$properties[] = gettext("Revoked");
216 de1a3167 jim-p
		}
217 f4eec250 Phil Davis
218 0e9d4a6d jim-p
		if (!empty($properties)) {
219
			$propstr = " (" . implode(", ", $properties) . ")";
220 de1a3167 jim-p
		}
221 f4eec250 Phil Davis
222 0e9d4a6d jim-p
		if ($prioritize_server_certs) {
223
			if ($purpose['server'] == "Yes") {
224
				$list[$cert['refid']] = $cert['descr'] . $propstr;
225
			} else {
226
				$non_server_list[$cert['refid']] = $cert['descr'] . $propstr;
227
			}
228
		} else {
229
			$list[$cert['refid']] = $cert['descr'] . $propstr;
230 de1a3167 jim-p
		}
231 0e9d4a6d jim-p
	}
232 f4eec250 Phil Davis
233 a4ba0282 Stephen Beaver
	return(array('server' => $list, 'non-server' => $non_server_list));
234 f4eec250 Phil Davis
}
235
236
function openvpn_build_bridge_list() {
237
	$list = array();
238
239
	$serverbridge_interface['none'] = "none";
240
	$serverbridge_interface = array_merge($serverbridge_interface, get_configured_interface_with_descr());
241 2a5960b0 Luiz Otavio O Souza
	$viplist = get_configured_vip_list();
242 f4eec250 Phil Davis
243 2a5960b0 Luiz Otavio O Souza
	foreach ($viplist as $vip => $address) {
244
		$serverbridge_interface[$vip.'|'.$address] = $address;
245 d9901ff4 Chris Buechler
		if (get_vip_descr($address)) {
246 2a5960b0 Luiz Otavio O Souza
			$serverbridge_interface[$vip.'|'.$address] .= " (". get_vip_descr($address) .")";
247 d9901ff4 Chris Buechler
		}
248 4e322e2c Phil Davis
	}
249 f4eec250 Phil Davis
250 4e322e2c Phil Davis
	foreach ($serverbridge_interface as $iface => $ifacename) {
251 f4eec250 Phil Davis
		$list[$iface] = htmlspecialchars($ifacename);
252 4e322e2c Phil Davis
	}
253 f4eec250 Phil Davis
254
	return($list);
255
}
256
257 3c11bd3c Matthew Grooms
function openvpn_create_key() {
258
259
	$fp = popen("/usr/local/sbin/openvpn --genkey --secret /dev/stdout 2>/dev/null", "r");
260 ef00af3c Phil Davis
	if (!$fp) {
261 3c11bd3c Matthew Grooms
		return false;
262 ef00af3c Phil Davis
	}
263 3c11bd3c Matthew Grooms
264
	$rslt = stream_get_contents($fp);
265
	pclose($fp);
266
267
	return $rslt;
268
}
269 d799787e Matthew Grooms
270 8411b218 Matthew Grooms
function openvpn_create_dhparams($bits) {
271 34bc1324 Matthew Grooms
272 2ec95f1f Renato Botelho
	$fp = popen("/usr/bin/openssl dhparam {$bits} 2>/dev/null", "r");
273 ef00af3c Phil Davis
	if (!$fp) {
274 34bc1324 Matthew Grooms
		return false;
275 ef00af3c Phil Davis
	}
276 34bc1324 Matthew Grooms
277
	$rslt = stream_get_contents($fp);
278
	pclose($fp);
279
280
	return $rslt;
281
}
282
283 d799787e Matthew Grooms
function openvpn_vpnid_used($vpnid) {
284 8be2d6d3 Ermal Luçi
	global $config;
285
286 ef00af3c Phil Davis
	if (is_array($config['openvpn']['openvpn-server'])) {
287
		foreach ($config['openvpn']['openvpn-server'] as & $settings) {
288
			if ($vpnid == $settings['vpnid']) {
289 d799787e Matthew Grooms
				return true;
290 ef00af3c Phil Davis
			}
291
		}
292
	}
293 d799787e Matthew Grooms
294 ef00af3c Phil Davis
	if (is_array($config['openvpn']['openvpn-client'])) {
295
		foreach ($config['openvpn']['openvpn-client'] as & $settings) {
296
			if ($vpnid == $settings['vpnid']) {
297 d799787e Matthew Grooms
				return true;
298 ef00af3c Phil Davis
			}
299
		}
300
	}
301 04a6e900 Ermal Luçi
302 d799787e Matthew Grooms
	return false;
303
}
304
305
function openvpn_vpnid_next() {
306
307
	$vpnid = 1;
308 ef00af3c Phil Davis
	while (openvpn_vpnid_used($vpnid)) {
309 d799787e Matthew Grooms
		$vpnid++;
310 ef00af3c Phil Davis
	}
311 d799787e Matthew Grooms
312
	return $vpnid;
313
}
314
315 49b76122 Renato Botelho
function openvpn_port_used($prot, $interface, $port, $curvpnid = 0) {
316 f432e364 Matthew Grooms
	global $config;
317
318 49b76122 Renato Botelho
	if (is_array($config['openvpn']['openvpn-server'])) {
319
		foreach ($config['openvpn']['openvpn-server'] as & $settings) {
320 ef00af3c Phil Davis
			if (isset($settings['disable'])) {
321 49b76122 Renato Botelho
				continue;
322 ef00af3c Phil Davis
			}
323 49b76122 Renato Botelho
324 ef00af3c Phil Davis
			if ($curvpnid != 0 && $curvpnid == $settings['vpnid']) {
325 49b76122 Renato Botelho
				continue;
326 ef00af3c Phil Davis
			}
327 49b76122 Renato Botelho
328
			if ($port == $settings['local_port'] && $prot == $settings['protocol'] &&
329 ef00af3c Phil Davis
			    ($interface == $settings['interface'] || $interface == "any" || $settings['interface'] == "any")) {
330 f432e364 Matthew Grooms
				return $settings['vpnid'];
331 ef00af3c Phil Davis
			}
332 49b76122 Renato Botelho
		}
333
	}
334 f432e364 Matthew Grooms
335 49b76122 Renato Botelho
	if (is_array($config['openvpn']['openvpn-client'])) {
336
		foreach ($config['openvpn']['openvpn-client'] as & $settings) {
337 ef00af3c Phil Davis
			if (isset($settings['disable'])) {
338 49b76122 Renato Botelho
				continue;
339 ef00af3c Phil Davis
			}
340 49b76122 Renato Botelho
341 ef00af3c Phil Davis
			if ($curvpnid != 0 && $curvpnid == $settings['vpnid']) {
342 49b76122 Renato Botelho
				continue;
343 ef00af3c Phil Davis
			}
344 49b76122 Renato Botelho
345
			if ($port == $settings['local_port'] && $prot == $settings['protocol'] &&
346 ef00af3c Phil Davis
			    ($interface == $settings['interface'] || $interface == "any" || $settings['interface'] == "any")) {
347 f432e364 Matthew Grooms
				return $settings['vpnid'];
348 ef00af3c Phil Davis
			}
349 49b76122 Renato Botelho
		}
350
	}
351 f432e364 Matthew Grooms
352
	return 0;
353
}
354
355 49b76122 Renato Botelho
function openvpn_port_next($prot, $interface = "wan") {
356 f432e364 Matthew Grooms
357
	$port = 1194;
358 ef00af3c Phil Davis
	while (openvpn_port_used($prot, $interface, $port)) {
359 49b76122 Renato Botelho
		$port++;
360 ef00af3c Phil Davis
	}
361
	while (openvpn_port_used($prot, "any", $port)) {
362 f432e364 Matthew Grooms
		$port++;
363 ef00af3c Phil Davis
	}
364 f432e364 Matthew Grooms
365
	return $port;
366
}
367
368 d799787e Matthew Grooms
function openvpn_get_cipherlist() {
369
370
	$ciphers = array();
371 4fd1130f jim-p
	$cipher_out = shell_exec('/usr/local/sbin/openvpn --show-ciphers | /usr/bin/grep \'(.*key\' | sed \'s/, TLS client\/server mode only//\'');
372 d799787e Matthew Grooms
	$cipher_lines = explode("\n", trim($cipher_out));
373
	sort($cipher_lines);
374
	foreach ($cipher_lines as $line) {
375 4fd1130f jim-p
		$words = explode(' ', $line, 2);
376 d799787e Matthew Grooms
		$ciphers[$words[0]] = "{$words[0]} {$words[1]}";
377 8be2d6d3 Ermal Luçi
	}
378 4aa02281 Carlos Eduardo Ramos
	$ciphers["none"] = gettext("None (No Encryption)");
379 d799787e Matthew Grooms
	return $ciphers;
380
}
381
382 97d5b59b jim-p
function openvpn_get_digestlist() {
383
384
	$digests = array();
385
	$digest_out = shell_exec('/usr/local/sbin/openvpn --show-digests | /usr/bin/grep "digest size" | /usr/bin/awk \'{print $1, "(" $2 "-" $3 ")";}\'');
386
	$digest_lines = explode("\n", trim($digest_out));
387
	sort($digest_lines);
388
	foreach ($digest_lines as $line) {
389
		$words = explode(' ', $line);
390
		$digests[$words[0]] = "{$words[0]} {$words[1]}";
391
	}
392
	$digests["none"] = gettext("None (No Authentication)");
393
	return $digests;
394
}
395
396 582c58ae jim-p
function openvpn_get_engines() {
397 e8c516a0 Phil Davis
	$openssl_engines = array('none' => gettext('No Hardware Crypto Acceleration'));
398 2ec95f1f Renato Botelho
	exec("/usr/bin/openssl engine -t -c", $openssl_engine_output);
399 349bf358 jim-p
	$openssl_engine_output = implode("\n", $openssl_engine_output);
400
	$openssl_engine_output = preg_replace("/\\n\\s+/", "|", $openssl_engine_output);
401
	$openssl_engine_output = explode("\n", $openssl_engine_output);
402
403 582c58ae jim-p
	foreach ($openssl_engine_output as $oeo) {
404 349bf358 jim-p
		$keep = true;
405
		$details = explode("|", $oeo);
406
		$engine = array_shift($details);
407 582c58ae jim-p
		$linematch = array();
408 349bf358 jim-p
		preg_match("/\((.*)\)\s(.*)/", $engine, $linematch);
409
		foreach ($details as $dt) {
410 ef00af3c Phil Davis
			if (strpos($dt, "unavailable") !== FALSE) {
411 349bf358 jim-p
				$keep = false;
412 ef00af3c Phil Davis
			}
413
			if (strpos($dt, "available") !== FALSE) {
414 349bf358 jim-p
				continue;
415 ef00af3c Phil Davis
			}
416
			if (strpos($dt, "[") !== FALSE) {
417 349bf358 jim-p
				$ciphers = trim($dt, "[]");
418 ef00af3c Phil Davis
			}
419 349bf358 jim-p
		}
420 ef00af3c Phil Davis
		if (!empty($ciphers)) {
421 349bf358 jim-p
			$ciphers = " - " . $ciphers;
422 ef00af3c Phil Davis
		}
423
		if (strlen($ciphers) > 60) {
424 349bf358 jim-p
			$ciphers = substr($ciphers, 0, 60) . " ... ";
425 ef00af3c Phil Davis
		}
426
		if ($keep) {
427 349bf358 jim-p
			$openssl_engines[$linematch[1]] = $linematch[2] . $ciphers;
428 ef00af3c Phil Davis
		}
429 582c58ae jim-p
	}
430
	return $openssl_engines;
431
}
432
433
function openvpn_validate_engine($engine) {
434
	$engines = openvpn_get_engines();
435
	return array_key_exists($engine, $engines);
436
}
437
438 d799787e Matthew Grooms
function openvpn_validate_host($value, $name) {
439
	$value = trim($value);
440 ef00af3c Phil Davis
	if (empty($value) || (!is_domain($value) && !is_ipaddr($value))) {
441 4aa02281 Carlos Eduardo Ramos
		return sprintf(gettext("The field '%s' must contain a valid IP address or domain name."), $name);
442 ef00af3c Phil Davis
	}
443 d799787e Matthew Grooms
	return false;
444 8dc3ef67 Scott Ullrich
}
445
446
function openvpn_validate_port($value, $name) {
447
	$value = trim($value);
448 ef00af3c Phil Davis
	if (empty($value) || !is_numeric($value) || $value < 0 || ($value > 65535)) {
449 4aa02281 Carlos Eduardo Ramos
		return sprintf(gettext("The field '%s' must contain a valid port, ranging from 0 to 65535."), $name);
450 ef00af3c Phil Davis
	}
451 b398bbca Martin Fuchs
	return false;
452 8dc3ef67 Scott Ullrich
}
453
454 a28d40cb jim-p
function openvpn_validate_cidr($value, $name, $multiple = false, $ipproto = "ipv4") {
455
	$value = trim($value);
456
	$error = false;
457 ef00af3c Phil Davis
	if (empty($value)) {
458 a28d40cb jim-p
		return false;
459 ef00af3c Phil Davis
	}
460 a28d40cb jim-p
	$networks = explode(',', $value);
461
462 ef00af3c Phil Davis
	if (!$multiple && (count($networks) > 1)) {
463 e8c516a0 Phil Davis
		return sprintf(gettext("The field '%1\$s' must contain a single valid %2\$s CIDR range."), $name, $ipproto);
464 ef00af3c Phil Davis
	}
465 a28d40cb jim-p
466
	foreach ($networks as $network) {
467 ef00af3c Phil Davis
		if ($ipproto == "ipv4") {
468 a28d40cb jim-p
			$error = !openvpn_validate_cidr_ipv4($network);
469 ef00af3c Phil Davis
		} else {
470 a28d40cb jim-p
			$error = !openvpn_validate_cidr_ipv6($network);
471 ef00af3c Phil Davis
		}
472
		if ($error) {
473 a28d40cb jim-p
			break;
474 ef00af3c Phil Davis
		}
475 a28d40cb jim-p
	}
476
477 ef00af3c Phil Davis
	if ($error) {
478 e8c516a0 Phil Davis
		return sprintf(gettext("The field '%1\$s' must contain only valid %2\$s CIDR range(s) separated by commas."), $name, $ipproto);
479 ef00af3c Phil Davis
	} else {
480 a28d40cb jim-p
		return false;
481 ef00af3c Phil Davis
	}
482 a28d40cb jim-p
}
483
484
function openvpn_validate_cidr_ipv4($value) {
485 8dc3ef67 Scott Ullrich
	$value = trim($value);
486
	if (!empty($value)) {
487
		list($ip, $mask) = explode('/', $value);
488 ef00af3c Phil Davis
		if (!is_ipaddrv4($ip) or !is_numeric($mask) or ($mask > 32) or ($mask < 0)) {
489 a28d40cb jim-p
			return false;
490 ef00af3c Phil Davis
		}
491 8dc3ef67 Scott Ullrich
	}
492 a28d40cb jim-p
	return true;
493
}
494
495
function openvpn_validate_cidr_ipv6($value) {
496
	$value = trim($value);
497
	if (!empty($value)) {
498
		list($ipv6, $prefix) = explode('/', $value);
499 ef00af3c Phil Davis
		if (empty($prefix)) {
500 a28d40cb jim-p
			$prefix = "128";
501 ef00af3c Phil Davis
		}
502
		if (!is_ipaddrv6($ipv6) or !is_numeric($prefix) or ($prefix > 128) or ($prefix < 0)) {
503 a28d40cb jim-p
			return false;
504 ef00af3c Phil Davis
		}
505 a28d40cb jim-p
	}
506
	return true;
507 afb07cf1 Scott Ullrich
}
508
509 d799787e Matthew Grooms
function openvpn_add_dhcpopts(& $settings, & $conf) {
510 afb07cf1 Scott Ullrich
511 ef00af3c Phil Davis
	if (!empty($settings['dns_domain'])) {
512 d799787e Matthew Grooms
		$conf .= "push \"dhcp-option DOMAIN {$settings['dns_domain']}\"\n";
513 ef00af3c Phil Davis
	}
514 add2e3f7 Scott Ullrich
515 ef00af3c Phil Davis
	if (!empty($settings['dns_server1'])) {
516 d799787e Matthew Grooms
		$conf .= "push \"dhcp-option DNS {$settings['dns_server1']}\"\n";
517 ef00af3c Phil Davis
	}
518
	if (!empty($settings['dns_server2'])) {
519 d799787e Matthew Grooms
		$conf .= "push \"dhcp-option DNS {$settings['dns_server2']}\"\n";
520 ef00af3c Phil Davis
	}
521
	if (!empty($settings['dns_server3'])) {
522 d799787e Matthew Grooms
		$conf .= "push \"dhcp-option DNS {$settings['dns_server3']}\"\n";
523 ef00af3c Phil Davis
	}
524
	if (!empty($settings['dns_server4'])) {
525 d799787e Matthew Grooms
		$conf .= "push \"dhcp-option DNS {$settings['dns_server4']}\"\n";
526 ef00af3c Phil Davis
	}
527 f9927473 Scott Ullrich
528 01c2735c jim-p
	if (!empty($settings['push_blockoutsidedns'])) {
529
		$conf .= "push \"block-outside-dns\"\n";
530
	}
531 ef00af3c Phil Davis
	if (!empty($settings['push_register_dns'])) {
532 c38764dc Dmitriy K.
		$conf .= "push \"register-dns\"\n";
533 ef00af3c Phil Davis
	}
534 c38764dc Dmitriy K.
535 ef00af3c Phil Davis
	if (!empty($settings['ntp_server1'])) {
536 c7f70dbc Chris Buechler
		$conf .= "push \"dhcp-option NTP {$settings['ntp_server1']}\"\n";
537 ef00af3c Phil Davis
	}
538
	if (!empty($settings['ntp_server2'])) {
539 c7f70dbc Chris Buechler
		$conf .= "push \"dhcp-option NTP {$settings['ntp_server2']}\"\n";
540 ef00af3c Phil Davis
	}
541 f9927473 Scott Ullrich
542 d799787e Matthew Grooms
	if ($settings['netbios_enable']) {
543 add2e3f7 Scott Ullrich
544 ef00af3c Phil Davis
		if (!empty($settings['dhcp_nbttype']) && ($settings['dhcp_nbttype'] != 0)) {
545 095a95ae Matthew Grooms
			$conf .= "push \"dhcp-option NBT {$settings['dhcp_nbttype']}\"\n";
546 ef00af3c Phil Davis
		}
547
		if (!empty($settings['dhcp_nbtscope'])) {
548 d799787e Matthew Grooms
			$conf .= "push \"dhcp-option NBS {$settings['dhcp_nbtscope']}\"\n";
549 ef00af3c Phil Davis
		}
550 8dc3ef67 Scott Ullrich
551 ef00af3c Phil Davis
		if (!empty($settings['wins_server1'])) {
552 d799787e Matthew Grooms
			$conf .= "push \"dhcp-option WINS {$settings['wins_server1']}\"\n";
553 ef00af3c Phil Davis
		}
554
		if (!empty($settings['wins_server2'])) {
555 d799787e Matthew Grooms
			$conf .= "push \"dhcp-option WINS {$settings['wins_server2']}\"\n";
556 ef00af3c Phil Davis
		}
557 add2e3f7 Scott Ullrich
558 ef00af3c Phil Davis
		if (!empty($settings['nbdd_server1'])) {
559 d799787e Matthew Grooms
			$conf .= "push \"dhcp-option NBDD {$settings['nbdd_server1']}\"\n";
560 ef00af3c Phil Davis
		}
561 d799787e Matthew Grooms
	}
562 8dc3ef67 Scott Ullrich
563 ef00af3c Phil Davis
	if ($settings['gwredir']) {
564 d799787e Matthew Grooms
		$conf .= "push \"redirect-gateway def1\"\n";
565 ef00af3c Phil Davis
	}
566 d799787e Matthew Grooms
}
567 24012690 Scott Ullrich
568 d799787e Matthew Grooms
function openvpn_add_custom(& $settings, & $conf) {
569 add2e3f7 Scott Ullrich
570 d799787e Matthew Grooms
	if ($settings['custom_options']) {
571 8dc3ef67 Scott Ullrich
572 d799787e Matthew Grooms
		$options = explode(';', $settings['custom_options']);
573
574
		if (is_array($options)) {
575 ef00af3c Phil Davis
			foreach ($options as $option) {
576 d799787e Matthew Grooms
				$conf .= "$option\n";
577 ef00af3c Phil Davis
			}
578
		} else {
579 d799787e Matthew Grooms
			$conf .= "{$settings['custom_options']}\n";
580 ef00af3c Phil Davis
		}
581 add2e3f7 Scott Ullrich
	}
582
}
583
584 691fbf14 Ermal Lu?i
function openvpn_add_keyfile(& $data, & $conf, $mode_id, $directive, $opt = "") {
585 d799787e Matthew Grooms
	global $g;
586 add2e3f7 Scott Ullrich
587 d799787e Matthew Grooms
	$fpath = $g['varetc_path']."/openvpn/{$mode_id}.{$directive}";
588 a8f538a8 jim-p
	openvpn_create_dirs();
589 d799787e Matthew Grooms
	file_put_contents($fpath, base64_decode($data));
590 f9ac3784 Ermal Lu?i
	//chown($fpath, 'nobody');
591
	//chgrp($fpath, 'nobody');
592 6f27412f Ermal Lu?i
	@chmod($fpath, 0600);
593 d799787e Matthew Grooms
594 691fbf14 Ermal Lu?i
	$conf .= "{$directive} {$fpath} {$opt}\n";
595 4eefa6e8 Scott Ullrich
}
596
597 fc05822b jim-p
function openvpn_reconfigure($mode, $settings) {
598 6293d7ed jim-p
	global $g, $config, $openvpn_tls_server_modes;
599 afb07cf1 Scott Ullrich
600 ef00af3c Phil Davis
	if (empty($settings)) {
601 93a0a028 Ermal Luçi
		return;
602 ef00af3c Phil Davis
	}
603
	if (isset($settings['disable'])) {
604 4eefa6e8 Scott Ullrich
		return;
605 ef00af3c Phil Davis
	}
606 a8f538a8 jim-p
	openvpn_create_dirs();
607 fdd725f0 Ermal Luçi
	/*
608 d799787e Matthew Grooms
	 * NOTE: Deleting tap devices causes spontaneous reboots. Instead,
609
	 * we use a vpnid number which is allocated for a particular client
610
	 * or server configuration. ( see openvpn_vpnid_next() )
611 fdd725f0 Ermal Luçi
	 */
612 8874c692 Ermal Luçi
613 d799787e Matthew Grooms
	$vpnid = $settings['vpnid'];
614
	$mode_id = $mode.$vpnid;
615 8874c692 Ermal Luçi
616 ef00af3c Phil Davis
	if (isset($settings['dev_mode'])) {
617 4936ff53 jim-p
		$tunname = "{$settings['dev_mode']}{$vpnid}";
618 ef00af3c Phil Davis
	} else {
619
		/* defaults to tun */
620 bd7ca506 jim-p
		$tunname = "tun{$vpnid}";
621 4936ff53 jim-p
		$settings['dev_mode'] = "tun";
622 691fbf14 Ermal Lu?i
	}
623
624 ef00af3c Phil Davis
	if ($mode == "server") {
625 bd7ca506 jim-p
		$devname = "ovpns{$vpnid}";
626 ef00af3c Phil Davis
	} else {
627 bd7ca506 jim-p
		$devname = "ovpnc{$vpnid}";
628 ef00af3c Phil Davis
	}
629 8874c692 Ermal Luçi
630 bd7ca506 jim-p
	/* is our device already configured */
631 4a97aa34 Ermal
	if (!does_interface_exist($devname)) {
632 dc408939 Matthew Grooms
633 bd7ca506 jim-p
		/* create the tap device if required */
634 ef00af3c Phil Davis
		if (!file_exists("/dev/{$tunname}")) {
635 873c1701 Renato Botelho
			exec("/sbin/ifconfig " . escapeshellarg($tunname) . " create");
636 ef00af3c Phil Davis
		}
637 98872d89 Ermal Luçi
638 bd7ca506 jim-p
		/* rename the device */
639 873c1701 Renato Botelho
		mwexec("/sbin/ifconfig " . escapeshellarg($tunname) . " name " . escapeshellarg($devname));
640 095a95ae Matthew Grooms
641 4ab1ffa0 Renato Botelho
		/* add the device to the openvpn group and make sure it's UP*/
642
		mwexec("/sbin/ifconfig " . escapeshellarg($devname) . " group openvpn up");
643 da4f91a9 Renato Botelho
644 bd1a6267 Renato Botelho
		$ifname = convert_real_interface_to_friendly_interface_name($devname);
645
		$grouptmp = link_interface_to_group($ifname);
646 ef00af3c Phil Davis
		if (!empty($grouptmp)) {
647 bd1a6267 Renato Botelho
			array_walk($grouptmp, 'interface_group_add_member');
648 ef00af3c Phil Davis
		}
649 bd1a6267 Renato Botelho
		unset($grouptmp, $ifname);
650 dc408939 Matthew Grooms
	}
651 d799787e Matthew Grooms
652 dc408939 Matthew Grooms
	$pfile = $g['varrun_path'] . "/openvpn_{$mode_id}.pid";
653 6714bbdc jim-p
	$proto = strtolower($settings['protocol']);
654 ef00af3c Phil Davis
	if (substr($settings['protocol'], 0, 3) == "TCP") {
655 6714bbdc jim-p
			$proto = "{$proto}-{$mode}";
656 ef00af3c Phil Davis
	}
657 4936ff53 jim-p
	$dev_mode = $settings['dev_mode'];
658 c0cf27aa Scott Ullrich
	$cipher = $settings['crypto'];
659 97d5b59b jim-p
	// OpenVPN defaults to SHA1, so use it when unset to maintain compatibility.
660
	$digest = !empty($settings['digest']) ? $settings['digest'] : "SHA1";
661 d799787e Matthew Grooms
662 47c48e28 smos
	$interface = get_failover_interface($settings['interface']);
663 e27bc6cf Phil Davis
	// The IP address in the settings can be an IPv4 or IPv6 address associated with the interface
664 d7a0c22a bcyrill
	$ipaddr = $settings['ipaddr'];
665 d799787e Matthew Grooms
666 67b0902f pierrepomes
	// If a specific ip address (VIP) is requested, use it.
667
	// Otherwise, if a specific interface is requested, use it
668 ef00af3c Phil Davis
	// If "any" interface was selected, local directive will be omitted.
669 97ffc513 Seth Mos
	if (is_ipaddrv4($ipaddr)) {
670 4de8f7ba Phil Davis
		$iface_ip = $ipaddr;
671 3d06e8f0 pierrepomes
	} else {
672 67b0902f pierrepomes
		if ((!empty($interface)) && (strcmp($interface, "any"))) {
673 507af8dd pierrepomes
			$iface_ip=get_interface_ip($interface);
674 67b0902f pierrepomes
		}
675 47c48e28 smos
	}
676 e27bc6cf Phil Davis
	if (is_ipaddrv6($ipaddr)) {
677 6c07db48 Phil Davis
		$iface_ipv6 = $ipaddr;
678 47c48e28 smos
	} else {
679 97ffc513 Seth Mos
		if ((!empty($interface)) && (strcmp($interface, "any"))) {
680
			$iface_ipv6=get_interface_ipv6($interface);
681
		}
682 3d06e8f0 pierrepomes
	}
683 d799787e Matthew Grooms
684 b1e8e675 Dmitriy K.
685
	$conf = "dev {$devname}\n";
686 5b3c0116 Dmitriy K.
	if (isset($settings['verbosity_level'])) {
687 b1e8e675 Dmitriy K.
		$conf .= "verb {$settings['verbosity_level']}\n";
688 5b3c0116 Dmitriy K.
	}
689 42bb1bee Renato Botelho
690 4936ff53 jim-p
	$conf .= "dev-type {$settings['dev_mode']}\n";
691 ef00af3c Phil Davis
	switch ($settings['dev_mode']) {
692 97ffc513 Seth Mos
		case "tun":
693 b9e9903d Dmitriy K.
			if (!$settings['no_tun_ipv6']) {
694
				$conf .= "tun-ipv6\n";
695
			}
696 97ffc513 Seth Mos
			break;
697
	}
698 bd7ca506 jim-p
	$conf .= "dev-node /dev/{$tunname}\n";
699 3c11bd3c Matthew Grooms
	$conf .= "writepid {$pfile}\n";
700
	$conf .= "#user nobody\n";
701
	$conf .= "#group nobody\n";
702 d1014c18 Chris Buechler
	$conf .= "script-security 3\n";
703 3c11bd3c Matthew Grooms
	$conf .= "daemon\n";
704
	$conf .= "keepalive 10 60\n";
705
	$conf .= "ping-timer-rem\n";
706
	$conf .= "persist-tun\n";
707
	$conf .= "persist-key\n";
708
	$conf .= "proto {$proto}\n";
709
	$conf .= "cipher {$cipher}\n";
710 97d5b59b jim-p
	$conf .= "auth {$digest}\n";
711 8d964cea Ermal
	$conf .= "up /usr/local/sbin/ovpn-linkup\n";
712
	$conf .= "down /usr/local/sbin/ovpn-linkdown\n";
713 1492e02c Ermal
	if (file_exists("/usr/local/sbin/openvpn.attributes.sh")) {
714 ef00af3c Phil Davis
		switch ($settings['mode']) {
715 a1b9105b jim-p
			case 'server_user':
716
			case 'server_tls_user':
717
				$conf .= "client-connect /usr/local/sbin/openvpn.attributes.sh\n";
718
				$conf .= "client-disconnect /usr/local/sbin/openvpn.attributes.sh\n";
719
				break;
720
		}
721 1492e02c Ermal
	}
722 3c11bd3c Matthew Grooms
723 6714bbdc jim-p
	/* Determine the local IP to use - and make sure it matches with the selected protocol. */
724
	if (is_ipaddrv4($iface_ip) && (stristr($settings['protocol'], "6") === false)) {
725
		$conf .= "local {$iface_ip}\n";
726
	} elseif (is_ipaddrv6($iface_ipv6) && (stristr($settings['protocol'], "6") !== false)) {
727
		$conf .= "local {$iface_ipv6}\n";
728 97ffc513 Seth Mos
	}
729 d799787e Matthew Grooms
730 ef00af3c Phil Davis
	if (openvpn_validate_engine($settings['engine']) && ($settings['engine'] != "none")) {
731 582c58ae jim-p
		$conf .= "engine {$settings['engine']}\n";
732 ef00af3c Phil Davis
	}
733 582c58ae jim-p
734 67b0902f pierrepomes
	// server specific settings
735 8dc3ef67 Scott Ullrich
	if ($mode == 'server') {
736 d799787e Matthew Grooms
737 966cdb43 jim-p
		list($ip, $cidr) = explode('/', trim($settings['tunnel_network']));
738
		list($ipv6, $prefix) = explode('/', trim($settings['tunnel_networkv6']));
739 5dc6c910 jim-p
		$mask = gen_subnet_mask($cidr);
740 8dc3ef67 Scott Ullrich
741 3c11bd3c Matthew Grooms
		// configure tls modes
742 ef00af3c Phil Davis
		switch ($settings['mode']) {
743 3c11bd3c Matthew Grooms
			case 'p2p_tls':
744
			case 'server_tls':
745 e62e2f8b Ermal Lu?i
			case 'server_user':
746 3c11bd3c Matthew Grooms
			case 'server_tls_user':
747 d799787e Matthew Grooms
				$conf .= "tls-server\n";
748 3c11bd3c Matthew Grooms
				break;
749 8dc3ef67 Scott Ullrich
		}
750 d799787e Matthew Grooms
751 3c11bd3c Matthew Grooms
		// configure p2p/server modes
752 ef00af3c Phil Davis
		switch ($settings['mode']) {
753 6c9cf466 jim-p
			case 'p2p_tls':
754 5dc6c910 jim-p
				// If the CIDR is less than a /30, OpenVPN will complain if you try to
755
				//  use the server directive. It works for a single client without it.
756
				//  See ticket #1417
757 74a556a3 jim-p
				if (!empty($ip) && !empty($mask) && ($cidr < 30)) {
758 5dc6c910 jim-p
					$conf .= "server {$ip} {$mask}\n";
759 154b0f80 jim-p
					$conf .= "client-config-dir {$g['varetc_path']}/openvpn-csc/server{$vpnid}\n";
760 ef00af3c Phil Davis
					if (is_ipaddr($ipv6)) {
761 a0e3ee98 jim-p
						$conf .= "server-ipv6 {$ipv6}/{$prefix}\n";
762 ef00af3c Phil Davis
					}
763 5dc6c910 jim-p
				}
764 3c11bd3c Matthew Grooms
			case 'p2p_shared_key':
765 74a556a3 jim-p
				if (!empty($ip) && !empty($mask)) {
766 30c8a290 Renato Botelho
					list($ip1, $ip2) = openvpn_get_interface_ip($ip, $cidr);
767 ef00af3c Phil Davis
					if ($settings['dev_mode'] == 'tun') {
768 459e9333 jim-p
						$conf .= "ifconfig {$ip1} {$ip2}\n";
769 ef00af3c Phil Davis
					} else {
770 459e9333 jim-p
						$conf .= "ifconfig {$ip1} {$mask}\n";
771 ef00af3c Phil Davis
					}
772 1ab6bdb5 jim-p
				}
773 a0e3ee98 jim-p
				if (!empty($ipv6) && !empty($prefix)) {
774 91c44185 jim-p
					list($ipv6_1, $ipv6_2) = openvpn_get_interface_ipv6($ipv6, $prefix);
775 ef00af3c Phil Davis
					if ($settings['dev_mode'] == 'tun') {
776 a0e3ee98 jim-p
						$conf .= "ifconfig-ipv6 {$ipv6_1} {$ipv6_2}\n";
777 ef00af3c Phil Davis
					} else {
778 60f501ec Phil Davis
						$conf .= "ifconfig-ipv6 {$ipv6_1} {$prefix}\n";
779 ef00af3c Phil Davis
					}
780 a0e3ee98 jim-p
				}
781 3c11bd3c Matthew Grooms
				break;
782
			case 'server_tls':
783
			case 'server_user':
784
			case 'server_tls_user':
785 74a556a3 jim-p
				if (!empty($ip) && !empty($mask)) {
786 1ab6bdb5 jim-p
					$conf .= "server {$ip} {$mask}\n";
787 ef00af3c Phil Davis
					if (is_ipaddr($ipv6)) {
788 1ab6bdb5 jim-p
						$conf .= "server-ipv6 {$ipv6}/{$prefix}\n";
789 ef00af3c Phil Davis
					}
790 154b0f80 jim-p
					$conf .= "client-config-dir {$g['varetc_path']}/openvpn-csc/server{$vpnid}\n";
791 1ab6bdb5 jim-p
				} else {
792
					if ($settings['serverbridge_dhcp']) {
793
						if ((!empty($settings['serverbridge_interface'])) && (strcmp($settings['serverbridge_interface'], "none"))) {
794
							$biface_ip=get_interface_ip($settings['serverbridge_interface']);
795
							$biface_sm=gen_subnet_mask(get_interface_subnet($settings['serverbridge_interface']));
796
							if (is_ipaddrv4($biface_ip) && is_ipaddrv4($settings['serverbridge_dhcp_start']) && is_ipaddrv4($settings['serverbridge_dhcp_end'])) {
797
								$conf .= "server-bridge {$biface_ip} {$biface_sm} {$settings['serverbridge_dhcp_start']} {$settings['serverbridge_dhcp_end']}\n";
798 154b0f80 jim-p
								$conf .= "client-config-dir {$g['varetc_path']}/openvpn-csc/server{$vpnid}\n";
799 1ab6bdb5 jim-p
							} else {
800
								$conf .= "mode server\n";
801
							}
802
						} else {
803
							$conf .= "mode server\n";
804
						}
805
					}
806
				}
807 3c11bd3c Matthew Grooms
				break;
808 8dc3ef67 Scott Ullrich
		}
809
810 3c11bd3c Matthew Grooms
		// configure user auth modes
811 ef00af3c Phil Davis
		switch ($settings['mode']) {
812 3c11bd3c Matthew Grooms
			case 'server_user':
813
				$conf .= "client-cert-not-required\n";
814
			case 'server_tls_user':
815 9eced774 jim-p
				/* username-as-common-name is not compatible with server-bridge */
816 ef00af3c Phil Davis
				if (stristr($conf, "server-bridge") === false) {
817 9eced774 jim-p
					$conf .= "username-as-common-name\n";
818 ef00af3c Phil Davis
				}
819 8a47c190 Ermal Lu?i
				if (!empty($settings['authmode'])) {
820 5e28dad4 Ermal
					$strictusercn = "false";
821 ef00af3c Phil Davis
					if ($settings['strictusercn']) {
822 5e28dad4 Ermal
						$strictusercn = "true";
823 ef00af3c Phil Davis
					}
824 a409a857 Kacper
					$conf .= "auth-user-pass-verify \"/usr/local/sbin/ovpn_auth_verify user '{$settings['authmode']}' {$strictusercn} {$mode_id} {$settings['local_port']}\" via-env\n";
825 e8a58de4 Ermal Lu?i
				}
826 3c11bd3c Matthew Grooms
				break;
827 8dc3ef67 Scott Ullrich
		}
828 ef00af3c Phil Davis
		if (!isset($settings['cert_depth']) && (strstr($settings['mode'], 'tls'))) {
829 41936acc jim-p
			$settings['cert_depth'] = 1;
830 ef00af3c Phil Davis
		}
831 98963f27 jim-p
		if (is_numeric($settings['cert_depth'])) {
832 ef00af3c Phil Davis
			if (($mode == 'client') && empty($settings['certref'])) {
833 2da48592 jim-p
				$cert = "";
834 ef00af3c Phil Davis
			} else {
835 2da48592 jim-p
				$cert = lookup_cert($settings['certref']);
836
				/* XXX: Seems not used at all! */
837
				$servercn = urlencode(cert_get_cn($cert['crt']));
838
				$conf .= "tls-verify \"/usr/local/sbin/ovpn_auth_verify tls '{$servercn}' {$settings['cert_depth']}\"\n";
839
			}
840 98963f27 jim-p
		}
841 8dc3ef67 Scott Ullrich
842 63084885 Matthew Grooms
		// The local port to listen on
843 d799787e Matthew Grooms
		$conf .= "lport {$settings['local_port']}\n";
844 c0cf27aa Scott Ullrich
845 63084885 Matthew Grooms
		// The management port to listen on
846 71ca2cb2 Ermal
		// Use unix socket to overcome the problem on any type of server
847
		$conf .= "management {$g['varetc_path']}/openvpn/{$mode_id}.sock unix\n";
848
		//$conf .= "management 127.0.0.1 {$settings['local_port']}\n";
849 63084885 Matthew Grooms
850 ef00af3c Phil Davis
		if ($settings['maxclients']) {
851 d799787e Matthew Grooms
			$conf .= "max-clients {$settings['maxclients']}\n";
852 ef00af3c Phil Davis
		}
853 d799787e Matthew Grooms
854 3c11bd3c Matthew Grooms
		// Can we push routes
855
		if ($settings['local_network']) {
856 a28d40cb jim-p
			$conf .= openvpn_gen_routes($settings['local_network'], "ipv4", true);
857 3c11bd3c Matthew Grooms
		}
858 787de45a Seth Mos
		if ($settings['local_networkv6']) {
859 a28d40cb jim-p
			$conf .= openvpn_gen_routes($settings['local_networkv6'], "ipv6", true);
860 787de45a Seth Mos
		}
861 3c11bd3c Matthew Grooms
862 ef00af3c Phil Davis
		switch ($settings['mode']) {
863 3c11bd3c Matthew Grooms
			case 'server_tls':
864
			case 'server_user':
865
			case 'server_tls_user':
866 5d8cd81a jim-p
				// Configure client dhcp options
867 3c11bd3c Matthew Grooms
				openvpn_add_dhcpopts($settings, $conf);
868 ef00af3c Phil Davis
				if ($settings['client2client']) {
869 5d8cd81a jim-p
					$conf .= "client-to-client\n";
870 ef00af3c Phil Davis
				}
871 3c11bd3c Matthew Grooms
				break;
872
		}
873 ef00af3c Phil Davis
		if (isset($settings['duplicate_cn'])) {
874 bca35cff jim-p
			$conf .= "duplicate-cn\n";
875 ef00af3c Phil Davis
		}
876 d799787e Matthew Grooms
	}
877
878 3c11bd3c Matthew Grooms
	// client specific settings
879 d799787e Matthew Grooms
880 3c11bd3c Matthew Grooms
	if ($mode == 'client') {
881 d799787e Matthew Grooms
882 3c11bd3c Matthew Grooms
		// configure p2p mode
883 ef00af3c Phil Davis
		switch ($settings['mode']) {
884 3c11bd3c Matthew Grooms
			case 'p2p_tls':
885
				$conf .= "tls-client\n";
886
			case 'shared_key':
887
				$conf .= "client\n";
888
				break;
889
		}
890 d799787e Matthew Grooms
891 e3924384 jim-p
		// If there is no bind option at all (ip and/or port), add "nobind" directive
892 42bb1bee Renato Botelho
		//  Otherwise, use the local port if defined, failing that, use lport 0 to
893 e3924384 jim-p
		//  ensure a random source port.
894 ef00af3c Phil Davis
		if ((empty($iface_ip)) && (!$settings['local_port'])) {
895 e3924384 jim-p
			$conf .= "nobind\n";
896 ef00af3c Phil Davis
		} elseif ($settings['local_port']) {
897 e3924384 jim-p
			$conf .= "lport {$settings['local_port']}\n";
898 ef00af3c Phil Davis
		} else {
899 e3924384 jim-p
			$conf .= "lport 0\n";
900 ef00af3c Phil Davis
		}
901 5708241f jim-p
902 4b887ef4 jim-p
		// Use unix socket to overcome the problem on any type of server
903
		$conf .= "management {$g['varetc_path']}/openvpn/{$mode_id}.sock unix\n";
904 48a458d2 pierrepomes
905 3c11bd3c Matthew Grooms
		// The remote server
906
		$conf .= "remote {$settings['server_addr']} {$settings['server_port']}\n";
907
908 ef00af3c Phil Davis
		if (!empty($settings['use_shaper'])) {
909 d799787e Matthew Grooms
			$conf .= "shaper {$settings['use_shaper']}\n";
910 ef00af3c Phil Davis
		}
911 ee506044 Scott Ullrich
912 d799787e Matthew Grooms
		if (!empty($settings['tunnel_network'])) {
913 966cdb43 jim-p
			list($ip, $cidr) = explode('/', trim($settings['tunnel_network']));
914 30c8a290 Renato Botelho
			$mask = gen_subnet_mask($cidr);
915 636918c9 Chris Buechler
			list($ip1, $ip2) = openvpn_get_interface_ip($ip, $cidr);
916 ef00af3c Phil Davis
			if ($settings['dev_mode'] == 'tun') {
917 459e9333 jim-p
				$conf .= "ifconfig {$ip2} {$ip1}\n";
918 ef00af3c Phil Davis
			} else {
919 459e9333 jim-p
				$conf .= "ifconfig {$ip2} {$mask}\n";
920 ef00af3c Phil Davis
			}
921 8dc3ef67 Scott Ullrich
		}
922 d799787e Matthew Grooms
923 a0e3ee98 jim-p
		if (!empty($settings['tunnel_networkv6'])) {
924 966cdb43 jim-p
			list($ipv6, $prefix) = explode('/', trim($settings['tunnel_networkv6']));
925 91c44185 jim-p
			list($ipv6_1, $ipv6_2) = openvpn_get_interface_ipv6($ipv6, $prefix);
926 ef00af3c Phil Davis
			if ($settings['dev_mode'] == 'tun') {
927 a0e3ee98 jim-p
				$conf .= "ifconfig-ipv6 {$ipv6_2} {$ipv6_1}\n";
928 ef00af3c Phil Davis
			} else {
929 60f501ec Phil Davis
				$conf .= "ifconfig-ipv6 {$ipv6_2} {$prefix}\n";
930 ef00af3c Phil Davis
			}
931 a0e3ee98 jim-p
		}
932
933 a6d55c23 Chris Buechler
		if (($settings['auth_user'] || $settings['auth_pass']) && $settings['mode'] == "p2p_tls") {
934 5f242576 PiBa-NL
			$up_file = "{$g['varetc_path']}/openvpn/{$mode_id}.up";
935
			$conf .= "auth-user-pass {$up_file}\n";
936 7304c023 Phil Davis
			if ($settings['auth_user']) {
937 4de8f7ba Phil Davis
				$userpass = "{$settings['auth_user']}\n";
938 7304c023 Phil Davis
			} else {
939
				$userpass = "";
940
			}
941
			if ($settings['auth_pass']) {
942 4de8f7ba Phil Davis
				$userpass .= "{$settings['auth_pass']}\n";
943 7304c023 Phil Davis
			}
944
			// If only auth_pass is given, then it acts like a user name and we put a blank line where pass would normally go.
945
			if (!($settings['auth_user'] && $settings['auth_pass'])) {
946
				$userpass .= "\n";
947
			}
948 5f242576 PiBa-NL
			file_put_contents($up_file, $userpass);
949
		}
950 42bb1bee Renato Botelho
951 762a24a3 Ermal Lu?i
		if ($settings['proxy_addr']) {
952
			$conf .= "http-proxy {$settings['proxy_addr']} {$settings['proxy_port']}";
953
			if ($settings['proxy_authtype'] != "none") {
954
				$conf .= " {$g['varetc_path']}/openvpn/{$mode_id}.pas {$settings['proxy_authtype']}";
955
				$proxypas = "{$settings['proxy_user']}\n";
956
				$proxypas .= "{$settings['proxy_passwd']}\n";
957
				file_put_contents("{$g['varetc_path']}/openvpn/{$mode_id}.pas", $proxypas);
958
			}
959
			$conf .= " \n";
960
		}
961 8dc3ef67 Scott Ullrich
	}
962
963 17c98ba9 jim-p
	// Add a remote network route if set, and only for p2p modes.
964 54285411 jim-p
	if ((substr($settings['mode'], 0, 3) == "p2p") && (openvpn_validate_cidr($settings['remote_network'], "", true, "ipv4") === FALSE)) {
965 a28d40cb jim-p
		$conf .= openvpn_gen_routes($settings['remote_network'], "ipv4", false);
966 8dc3ef67 Scott Ullrich
	}
967 4856df9b jim-p
	// Add a remote network route if set, and only for p2p modes.
968 54285411 jim-p
	if ((substr($settings['mode'], 0, 3) == "p2p") && (openvpn_validate_cidr($settings['remote_networkv6'], "", true, "ipv6") === FALSE)) {
969 a28d40cb jim-p
		$conf .= openvpn_gen_routes($settings['remote_networkv6'], "ipv6", false);
970 4856df9b jim-p
	}
971 afb07cf1 Scott Ullrich
972 d799787e Matthew Grooms
	// Write the settings for the keys
973 ef00af3c Phil Davis
	switch ($settings['mode']) {
974 3c11bd3c Matthew Grooms
		case 'p2p_shared_key':
975
			openvpn_add_keyfile($settings['shared_key'], $conf, $mode_id, "secret");
976
			break;
977
		case 'p2p_tls':
978
		case 'server_tls':
979
		case 'server_tls_user':
980 e62e2f8b Ermal Lu?i
		case 'server_user':
981 3b44f18b scipiojr
			// ca_chain() expects parameter to be passed by reference. 
982
			// avoid passing the whole settings array, as param names or 
983 b4f400a4 scipiojr
			// types might change in future releases. 
984
			$param = array('caref' => $settings['caref']);	
985
			$ca = ca_chain($param);
986 bc5ab7af scipiojr
			$ca = base64_encode($ca);
987 b4f400a4 scipiojr
			
988 bc5ab7af scipiojr
			openvpn_add_keyfile($ca, $conf, $mode_id, "ca");
989 b4f400a4 scipiojr
			
990
			unset($ca, $param);
991 2da48592 jim-p
992
			if (!empty($settings['certref'])) {
993
				$cert = lookup_cert($settings['certref']);
994
				openvpn_add_keyfile($cert['crt'], $conf, $mode_id, "cert");
995
				openvpn_add_keyfile($cert['prv'], $conf, $mode_id, "key");
996
			}
997 ef00af3c Phil Davis
			if ($mode == 'server') {
998 fe787fc7 Matthew Grooms
				$conf .= "dh {$g['etc_path']}/dh-parameters.{$settings['dh_length']}\n";
999 ef00af3c Phil Davis
			}
1000 6db02381 jim-p
			if (!empty($settings['crlref'])) {
1001
				$crl = lookup_crl($settings['crlref']);
1002 cfcc6994 jim-p
				crl_update($crl);
1003 6db02381 jim-p
				openvpn_add_keyfile($crl['text'], $conf, $mode_id, "crl-verify");
1004
			}
1005 db746ce2 Ermal Lu?i
			if ($settings['tls']) {
1006 ef00af3c Phil Davis
				if ($mode == "server") {
1007 db746ce2 Ermal Lu?i
					$tlsopt = 0;
1008 ef00af3c Phil Davis
				} else {
1009 db746ce2 Ermal Lu?i
					$tlsopt = 1;
1010 ef00af3c Phil Davis
				}
1011 db746ce2 Ermal Lu?i
				openvpn_add_keyfile($settings['tls'], $conf, $mode_id, "tls-auth", $tlsopt);
1012
			}
1013 3c11bd3c Matthew Grooms
			break;
1014 8dc3ef67 Scott Ullrich
	}
1015
1016 ef00af3c Phil Davis
	if (!empty($settings['compression'])) {
1017 9446ee68 jim-p
		if ($settings['compression'] == "noadapt") {
1018
			$conf .= "comp-noadapt\n";
1019
		} else {
1020
			$conf .= "comp-lzo {$settings['compression']}\n";
1021
		}
1022 ef00af3c Phil Davis
	}
1023 d799787e Matthew Grooms
1024 ef00af3c Phil Davis
	if ($settings['passtos']) {
1025 d799787e Matthew Grooms
		$conf .= "passtos\n";
1026 ef00af3c Phil Davis
	}
1027 d799787e Matthew Grooms
1028 ef00af3c Phil Davis
	if ($settings['resolve_retry']) {
1029 d799787e Matthew Grooms
		$conf .= "resolv-retry infinite\n";
1030 3f1265e4 Chris Buechler
	} else if ($mode == 'client') {
1031 30640018 Chris Buechler
		$conf .= "resolv-retry infinite\n";
1032 ef00af3c Phil Davis
	}
1033 d799787e Matthew Grooms
1034
	if ($settings['dynamic_ip']) {
1035
		$conf .= "persist-remote-ip\n";
1036
		$conf .= "float\n";
1037 8dc3ef67 Scott Ullrich
	}
1038 afb07cf1 Scott Ullrich
1039 ef378560 jim-p
	// If the server is not a TLS server or it has a tunnel network CIDR less than a /30, skip this.
1040 4742e635 Chris Buechler
	if (in_array($settings['mode'], $openvpn_tls_server_modes) && (!empty($ip) && !empty($mask) && ($cidr < 30)) && $settings['dev_mode'] != "tap") {
1041 63646f4d jim-p
		if (empty($settings['topology'])) {
1042
			$settings['topology'] = "subnet";
1043
		}
1044
		$conf .= "topology {$settings['topology']}\n";
1045 ee55ce7d jim-p
	}
1046
1047 b9e9903d Dmitriy K.
	// New client features
1048
	if ($mode == "client") {
1049
		// Dont pull routes checkbox
1050
		if ($settings['route_no_pull']) {
1051
			$conf .= "route-nopull\n";
1052
		}
1053
1054
		// Dont add/remove routes checkbox
1055
		if ($settings['route_no_exec']) {
1056
			$conf .= "route-noexec\n";
1057
		}
1058
	}
1059
1060 d799787e Matthew Grooms
	openvpn_add_custom($settings, $conf);
1061
1062 a8f538a8 jim-p
	openvpn_create_dirs();
1063 938fc5b0 Ermal
	$fpath = "{$g['varetc_path']}/openvpn/{$mode_id}.conf";
1064 d799787e Matthew Grooms
	file_put_contents($fpath, $conf);
1065 938fc5b0 Ermal
	unset($conf);
1066 be00850b Phil Davis
	$fpath = "{$g['varetc_path']}/openvpn/{$mode_id}.interface";
1067
	file_put_contents($fpath, $interface);
1068 f9ac3784 Ermal Lu?i
	//chown($fpath, 'nobody');
1069
	//chgrp($fpath, 'nobody');
1070 6f27412f Ermal Lu?i
	@chmod("{$g['varetc_path']}/openvpn/{$mode_id}.conf", 0600);
1071 be00850b Phil Davis
	@chmod("{$g['varetc_path']}/openvpn/{$mode_id}.interface", 0600);
1072 6f27412f Ermal Lu?i
	@chmod("{$g['varetc_path']}/openvpn/{$mode_id}.key", 0600);
1073
	@chmod("{$g['varetc_path']}/openvpn/{$mode_id}.tls-auth", 0600);
1074
	@chmod("{$g['varetc_path']}/openvpn/{$mode_id}.conf", 0600);
1075 d799787e Matthew Grooms
}
1076
1077 fc05822b jim-p
function openvpn_restart($mode, $settings) {
1078 d799787e Matthew Grooms
	global $g, $config;
1079
1080
	$vpnid = $settings['vpnid'];
1081
	$mode_id = $mode.$vpnid;
1082
1083 76369bfc Matthew Grooms
	/* kill the process if running */
1084 705c8ec9 Matthew Grooms
	$pfile = $g['varrun_path']."/openvpn_{$mode_id}.pid";
1085 76369bfc Matthew Grooms
	if (file_exists($pfile)) {
1086 705c8ec9 Matthew Grooms
1087 76369bfc Matthew Grooms
		/* read the pid file */
1088
		$pid = rtrim(file_get_contents($pfile));
1089
		unlink($pfile);
1090 705c8ec9 Matthew Grooms
1091 76369bfc Matthew Grooms
		/* send a term signal to the process */
1092
		posix_kill($pid, SIGTERM);
1093
1094 93ead355 Chris Buechler
		/* wait until the process exits, or timeout and kill it */
1095
		$i = 0;
1096 ef00af3c Phil Davis
		while (posix_kill($pid, 0)) {
1097 76369bfc Matthew Grooms
			usleep(250000);
1098 93ead355 Chris Buechler
			if ($i > 10) {
1099 e8c516a0 Phil Davis
				log_error(sprintf(gettext('OpenVPN ID %1$s PID %2$s still running, killing.'), $mode_id, $pid));
1100 93ead355 Chris Buechler
				posix_kill($pid, SIGKILL);
1101 02a2bffa Chris Buechler
				usleep(500000);
1102 93ead355 Chris Buechler
			}
1103
			$i++;
1104
		}
1105 76369bfc Matthew Grooms
	}
1106 d799787e Matthew Grooms
1107 ef00af3c Phil Davis
	if (isset($settings['disable'])) {
1108 d799787e Matthew Grooms
		return;
1109 ef00af3c Phil Davis
	}
1110 d799787e Matthew Grooms
1111 f003f8db jim-p
	/* Do not start an instance if we are not CARP master on this vip! */
1112
	if (strstr($settings['interface'], "_vip") && get_carp_interface_status($settings['interface']) != "MASTER") {
1113 9ea0cb90 jim-p
		return;
1114 ef00af3c Phil Davis
	}
1115 42bb1bee Renato Botelho
1116
	/* Check if client is bound to a gateway group */
1117 330ecea1 Shahid Sheikh
	$a_groups = return_gateway_groups_array();
1118
	if (is_array($a_groups[$settings['interface']])) {
1119 ef00af3c Phil Davis
		/* the interface is a gateway group. If a vip is defined and its a CARP backup then do not start */
1120
		if (($a_groups[$settings['interface']][0]['vip'] <> "") && (get_carp_interface_status($a_groups[$settings['interface']][0]['vip']) != "MASTER")) {
1121 330ecea1 Shahid Sheikh
			return;
1122 ef00af3c Phil Davis
		}
1123 330ecea1 Shahid Sheikh
	}
1124 9ea0cb90 jim-p
1125 705c8ec9 Matthew Grooms
	/* start the new process */
1126 d799787e Matthew Grooms
	$fpath = $g['varetc_path']."/openvpn/{$mode_id}.conf";
1127 91c44185 jim-p
	openvpn_clear_route($mode, $settings);
1128 873c1701 Renato Botelho
	mwexec_bg("/usr/local/sbin/openvpn --config " . escapeshellarg($fpath));
1129 847cd48d Ermal
1130 ef00af3c Phil Davis
	if (!platform_booting()) {
1131 847cd48d Ermal
		send_event("filter reload");
1132 ef00af3c Phil Davis
	}
1133 afb07cf1 Scott Ullrich
}
1134
1135 dc408939 Matthew Grooms
function openvpn_delete($mode, & $settings) {
1136 d799787e Matthew Grooms
	global $g, $config;
1137
1138
	$vpnid = $settings['vpnid'];
1139
	$mode_id = $mode.$vpnid;
1140
1141 ef00af3c Phil Davis
	if (isset($settings['dev_mode'])) {
1142 da601f8e PiBa-NL
		$tunname = "{$settings['dev_mode']}{$vpnid}";
1143 ef00af3c Phil Davis
	} else {
1144
		/* defaults to tun */
1145 da601f8e PiBa-NL
		$tunname = "tun{$vpnid}";
1146
	}
1147
1148 ef00af3c Phil Davis
	if ($mode == "server") {
1149 095a95ae Matthew Grooms
		$devname = "ovpns{$vpnid}";
1150 ef00af3c Phil Davis
	} else {
1151 095a95ae Matthew Grooms
		$devname = "ovpnc{$vpnid}";
1152 ef00af3c Phil Davis
	}
1153 dc408939 Matthew Grooms
1154 76369bfc Matthew Grooms
	/* kill the process if running */
1155 dc408939 Matthew Grooms
	$pfile = "{$g['varrun_path']}/openvpn_{$mode_id}.pid";
1156 76369bfc Matthew Grooms
	if (file_exists($pfile)) {
1157 dc408939 Matthew Grooms
1158 76369bfc Matthew Grooms
		/* read the pid file */
1159
		$pid = trim(file_get_contents($pfile));
1160
		unlink($pfile);
1161
1162
		/* send a term signal to the process */
1163
		posix_kill($pid, SIGTERM);
1164
	}
1165 705c8ec9 Matthew Grooms
1166 095a95ae Matthew Grooms
	/* remove the device from the openvpn group */
1167 873c1701 Renato Botelho
	mwexec("/sbin/ifconfig " . escapeshellarg($devname) . " -group openvpn");
1168 095a95ae Matthew Grooms
1169 dc408939 Matthew Grooms
	/* restore the original adapter name */
1170 873c1701 Renato Botelho
	mwexec("/sbin/ifconfig " . escapeshellarg($devname) . " name " . escapeshellarg($tunname));
1171 dc408939 Matthew Grooms
1172
	/* remove the configuration files */
1173 8bb47a46 Ermal
	@array_map('unlink', glob("{$g['varetc_path']}/openvpn/{$mode_id}.*"));
1174 d799787e Matthew Grooms
}
1175 afb07cf1 Scott Ullrich
1176 dc408939 Matthew Grooms
function openvpn_resync_csc(& $settings) {
1177 154b0f80 jim-p
	global $g, $config, $openvpn_tls_server_modes;
1178 8dc3ef67 Scott Ullrich
1179 154b0f80 jim-p
	$csc_base_path = "{$g['varetc_path']}/openvpn-csc";
1180 8dc3ef67 Scott Ullrich
1181 a1cab2c7 Ermal
	if (isset($settings['disable'])) {
1182 4e5e7540 jim-p
		openvpn_delete_csc($settings);
1183 c876662c Scott Ullrich
		return;
1184
	}
1185 a8f538a8 jim-p
	openvpn_create_dirs();
1186 d799787e Matthew Grooms
1187 154b0f80 jim-p
	if (empty($settings['server_list'])) {
1188
		$csc_server_list = array();
1189
	} else {
1190
		$csc_server_list = explode(",", $settings['server_list']);
1191
	}
1192
1193 8dc3ef67 Scott Ullrich
	$conf = '';
1194 ef00af3c Phil Davis
	if ($settings['block']) {
1195 d799787e Matthew Grooms
		$conf .= "disable\n";
1196 ef00af3c Phil Davis
	}
1197 d799787e Matthew Grooms
1198 ef00af3c Phil Davis
	if ($settings['push_reset']) {
1199 d799787e Matthew Grooms
		$conf .= "push-reset\n";
1200 ef00af3c Phil Davis
	}
1201 d799787e Matthew Grooms
1202 5c427ce7 jim-p
	if ($settings['local_network']) {
1203
		$conf .= openvpn_gen_routes($settings['local_network'], "ipv4", true);
1204
	}
1205
	if ($settings['local_networkv6']) {
1206
		$conf .= openvpn_gen_routes($settings['local_networkv6'], "ipv6", true);
1207
	}
1208
1209
	// Add a remote network iroute if set
1210
	if (openvpn_validate_cidr($settings['remote_network'], "", true, "ipv4") === FALSE) {
1211
		$conf .= openvpn_gen_routes($settings['remote_network'], "ipv4", false, true);
1212
	}
1213
	// Add a remote network iroute if set
1214
	if (openvpn_validate_cidr($settings['remote_networkv6'], "", true, "ipv6") === FALSE) {
1215
		$conf .= openvpn_gen_routes($settings['remote_networkv6'], "ipv6", false, true);
1216
	}
1217
1218 d799787e Matthew Grooms
	openvpn_add_dhcpopts($settings, $conf);
1219 8dc3ef67 Scott Ullrich
1220 d799787e Matthew Grooms
	openvpn_add_custom($settings, $conf);
1221 154b0f80 jim-p
	/* Loop through servers, find which ones can use this CSC */
1222
	if (is_array($config['openvpn']['openvpn-server'])) {
1223
		foreach ($config['openvpn']['openvpn-server'] as $serversettings) {
1224
			if (isset($serversettings['disable'])) {
1225
				continue;
1226
			}
1227
			if (in_array($serversettings['mode'], $openvpn_tls_server_modes)) {
1228
				if ($serversettings['vpnid'] && (empty($csc_server_list) || in_array($serversettings['vpnid'], $csc_server_list))) {
1229
					$csc_path = "{$csc_base_path}/server{$serversettings['vpnid']}/" . basename($settings['common_name']);
1230
					$csc_conf = $conf;
1231
1232 cd8f2f2b jim-p
					if (!empty($serversettings['tunnel_network']) && !empty($settings['tunnel_network'])) {
1233 966cdb43 jim-p
						list($ip, $mask) = explode('/', trim($settings['tunnel_network']));
1234 154b0f80 jim-p
						if (($serversettings['dev_mode'] == 'tap') || ($serversettings['topology'] == "subnet")) {
1235
							$csc_conf .= "ifconfig-push {$ip} " . gen_subnet_mask($mask) . "\n";
1236
						} else {
1237
							/* Because this is being pushed, the order from the client's point of view. */
1238 493e6807 stilez
							$baselong = gen_subnetv4($ip, $mask);
1239
							$serverip = ip_after($baselong, 1);
1240
							$clientip = ip_after($baselong, 2);
1241 154b0f80 jim-p
							$csc_conf .= "ifconfig-push {$clientip} {$serverip}\n";
1242
						}
1243
					}
1244
					file_put_contents($csc_path, $csc_conf);
1245
					chown($csc_path, 'nobody');
1246
					chgrp($csc_path, 'nobody');
1247
				}
1248
			}
1249
		}
1250
	}
1251 d799787e Matthew Grooms
}
1252 8dc3ef67 Scott Ullrich
1253 1f954318 jim-p
function openvpn_resync_csc_all() {
1254
	global $config;
1255
	if (is_array($config['openvpn']['openvpn-csc'])) {
1256
		foreach ($config['openvpn']['openvpn-csc'] as & $settings) {
1257
			openvpn_resync_csc($settings);
1258
		}
1259
	}
1260
}
1261
1262 dc408939 Matthew Grooms
function openvpn_delete_csc(& $settings) {
1263 154b0f80 jim-p
	global $g, $config, $openvpn_tls_server_modes;
1264
	$csc_base_path = "{$g['varetc_path']}/openvpn-csc";
1265
	if (empty($settings['server_list'])) {
1266
		$csc_server_list = array();
1267
	} else {
1268
		$csc_server_list = explode(",", $settings['server_list']);
1269
	}
1270 3c2e5528 Scott Ullrich
1271 154b0f80 jim-p
	/* Loop through servers, find which ones used this CSC */
1272
	if (is_array($config['openvpn']['openvpn-server'])) {
1273
		foreach ($config['openvpn']['openvpn-server'] as $serversettings) {
1274
			if (isset($serversettings['disable'])) {
1275
				continue;
1276
			}
1277
			if (in_array($serversettings['mode'], $openvpn_tls_server_modes)) {
1278
				if ($serversettings['vpnid'] && (empty($csc_server_list) || in_array($serversettings['vpnid'], $csc_server_list))) {
1279
					$csc_path = "{$csc_base_path}/server{$serversettings['vpnid']}/" . basename($settings['common_name']);
1280
					unlink_if_exists($csc_path);
1281
				}
1282
			}
1283
		}
1284
	}
1285 267ab13f Ermal Luçi
}
1286 afb07cf1 Scott Ullrich
1287 24012690 Scott Ullrich
// Resync the configuration and restart the VPN
1288 fc05822b jim-p
function openvpn_resync($mode, $settings) {
1289 dc408939 Matthew Grooms
	openvpn_reconfigure($mode, $settings);
1290
	openvpn_restart($mode, $settings);
1291 afb07cf1 Scott Ullrich
}
1292
1293 add2e3f7 Scott Ullrich
// Resync and restart all VPNs
1294 c7f60193 Ermal
function openvpn_resync_all($interface = "") {
1295 d799787e Matthew Grooms
	global $g, $config;
1296 267ab13f Ermal Luçi
1297 a8f538a8 jim-p
	openvpn_create_dirs();
1298 3cb54b54 Matthew Grooms
1299 ef00af3c Phil Davis
	if (!is_array($config['openvpn'])) {
1300 34bc1324 Matthew Grooms
		$config['openvpn'] = array();
1301 ef00af3c Phil Davis
	}
1302 34bc1324 Matthew Grooms
1303 15b414e6 Matthew Grooms
/*
1304 34bc1324 Matthew Grooms
	if (!$config['openvpn']['dh-parameters']) {
1305
		echo "Configuring OpenVPN Parameters ...\n";
1306 035e4289 Matthew Grooms
		$dh_parameters = openvpn_create_dhparams(1024);
1307 34bc1324 Matthew Grooms
		$dh_parameters = base64_encode($dh_parameters);
1308
		$config['openvpn']['dh-parameters'] = $dh_parameters;
1309 c67dd94e Bill Marquette
		write_config("OpenVPN DH parameters");
1310 34bc1324 Matthew Grooms
	}
1311
1312
	$path_ovdh = $g['varetc_path']."/openvpn/dh-parameters";
1313
	if (!file_exists($path_ovdh)) {
1314
		$dh_parameters = $config['openvpn']['dh-parameters'];
1315
		$dh_parameters = base64_decode($dh_parameters);
1316
		file_put_contents($path_ovdh, $dh_parameters);
1317
	}
1318 15b414e6 Matthew Grooms
*/
1319 ef00af3c Phil Davis
	if ($interface <> "") {
1320 e8c516a0 Phil Davis
		log_error(sprintf(gettext("Resyncing OpenVPN instances for interface %s."), convert_friendly_interface_to_friendly_descr($interface)));
1321 ef00af3c Phil Davis
	} else {
1322 e8c516a0 Phil Davis
		log_error(gettext("Resyncing OpenVPN instances."));
1323 ef00af3c Phil Davis
	}
1324 34bc1324 Matthew Grooms
1325 c7f60193 Ermal
	if (is_array($config['openvpn']['openvpn-server'])) {
1326
		foreach ($config['openvpn']['openvpn-server'] as & $settings) {
1327 ef00af3c Phil Davis
			if ($interface <> "" && $interface != $settings['interface']) {
1328 c7f60193 Ermal
				continue;
1329 ef00af3c Phil Davis
			}
1330 dc408939 Matthew Grooms
			openvpn_resync('server', $settings);
1331 c7f60193 Ermal
		}
1332
	}
1333 5b237745 Scott Ullrich
1334 c7f60193 Ermal
	if (is_array($config['openvpn']['openvpn-client'])) {
1335
		foreach ($config['openvpn']['openvpn-client'] as & $settings) {
1336 ef00af3c Phil Davis
			if ($interface <> "" && $interface != $settings['interface']) {
1337 c7f60193 Ermal
				continue;
1338 ef00af3c Phil Davis
			}
1339 dc408939 Matthew Grooms
			openvpn_resync('client', $settings);
1340 c7f60193 Ermal
		}
1341
	}
1342 afb07cf1 Scott Ullrich
1343 1f954318 jim-p
	openvpn_resync_csc_all();
1344 afb07cf1 Scott Ullrich
1345 5b237745 Scott Ullrich
}
1346 702a4702 Scott Ullrich
1347 99cc103b Phil Davis
// Resync and restart all VPNs using a gateway group.
1348
function openvpn_resync_gwgroup($gwgroupname = "") {
1349
	global $g, $config;
1350
1351
	if ($gwgroupname <> "") {
1352
		if (is_array($config['openvpn']['openvpn-server'])) {
1353
			foreach ($config['openvpn']['openvpn-server'] as & $settings) {
1354
				if ($gwgroupname == $settings['interface']) {
1355 e8c516a0 Phil Davis
					log_error(sprintf(gettext('Resyncing OpenVPN for gateway group %1$s server %2$s.'), $gwgroupname, $settings["description"]));
1356 99cc103b Phil Davis
					openvpn_resync('server', $settings);
1357
				}
1358
			}
1359
		}
1360
1361
		if (is_array($config['openvpn']['openvpn-client'])) {
1362
			foreach ($config['openvpn']['openvpn-client'] as & $settings) {
1363
				if ($gwgroupname == $settings['interface']) {
1364 e8c516a0 Phil Davis
					log_error(sprintf(gettext('Resyncing OpenVPN for gateway group %1$s client %2$s.'), $gwgroupname, $settings["description"]));
1365 99cc103b Phil Davis
					openvpn_resync('client', $settings);
1366
				}
1367
			}
1368
		}
1369
1370
		// Note: no need to resysnc Client Specific (csc) here, as changes to the OpenVPN real interface do not effect these.
1371
1372 ef00af3c Phil Davis
	} else {
1373 e8c516a0 Phil Davis
		log_error(gettext("openvpn_resync_gwgroup called with null gwgroup parameter."));
1374 ef00af3c Phil Davis
	}
1375 99cc103b Phil Davis
}
1376
1377 453d9c96 jim-p
function openvpn_get_active_servers($type="multipoint") {
1378 71ca2cb2 Ermal
	global $config, $g;
1379
1380 53663f57 jim-p
	$servers = array();
1381
	if (is_array($config['openvpn']['openvpn-server'])) {
1382
		foreach ($config['openvpn']['openvpn-server'] as & $settings) {
1383 ef00af3c Phil Davis
			if (empty($settings) || isset($settings['disable'])) {
1384 6b2dcac5 Ermal
				continue;
1385 ef00af3c Phil Davis
			}
1386 6b2dcac5 Ermal
1387 53663f57 jim-p
			$prot = $settings['protocol'];
1388
			$port = $settings['local_port'];
1389 42bb1bee Renato Botelho
1390 53663f57 jim-p
			$server = array();
1391 95305736 jim-p
			$server['port'] = ($settings['local_port']) ? $settings['local_port'] : 1194;
1392 41be629f jim-p
			$server['mode'] = $settings['mode'];
1393 ef00af3c Phil Davis
			if ($settings['description']) {
1394 53663f57 jim-p
				$server['name'] = "{$settings['description']} {$prot}:{$port}";
1395 ef00af3c Phil Davis
			} else {
1396 53663f57 jim-p
				$server['name'] = "Server {$prot}:{$port}";
1397 ef00af3c Phil Davis
			}
1398 53663f57 jim-p
			$server['conns'] = array();
1399 2eaa97b9 jim-p
			$server['vpnid'] = $settings['vpnid'];
1400
			$server['mgmt'] = "server{$server['vpnid']}";
1401
			$socket = "unix://{$g['varetc_path']}/openvpn/{$server['mgmt']}.sock";
1402 966cdb43 jim-p
			list($tn, $sm) = explode('/', trim($settings['tunnel_network']));
1403 453d9c96 jim-p
1404 ef00af3c Phil Davis
			if ((($server['mode'] == "p2p_shared_key") || ($sm >= 30)) && ($type == "p2p")) {
1405 95305736 jim-p
				$servers[] = openvpn_get_client_status($server, $socket);
1406 ef00af3c Phil Davis
			} elseif (($server['mode'] != "p2p_shared_key") && ($type == "multipoint") && ($sm < 30)) {
1407 95305736 jim-p
				$servers[] = openvpn_get_server_status($server, $socket);
1408 ef00af3c Phil Davis
			}
1409 95305736 jim-p
		}
1410
	}
1411
	return $servers;
1412
}
1413 b0140675 Ermal
1414 95305736 jim-p
function openvpn_get_server_status($server, $socket) {
1415 247c417f Sjon Hortensius
	$errval = null;
1416
	$errstr = null;
1417 95305736 jim-p
	$fp = @stream_socket_client($socket, $errval, $errstr, 1);
1418
	if ($fp) {
1419
		stream_set_timeout($fp, 1);
1420
1421
		/* send our status request */
1422
		fputs($fp, "status 2\n");
1423
1424
		/* recv all response lines */
1425
		while (!feof($fp)) {
1426
1427
			/* read the next line */
1428
			$line = fgets($fp, 1024);
1429
1430
			$info = stream_get_meta_data($fp);
1431 ef00af3c Phil Davis
			if ($info['timed_out']) {
1432 95305736 jim-p
				break;
1433 ef00af3c Phil Davis
			}
1434 95305736 jim-p
1435
			/* parse header list line */
1436 ef00af3c Phil Davis
			if (strstr($line, "HEADER")) {
1437 95305736 jim-p
				continue;
1438 ef00af3c Phil Davis
			}
1439 95305736 jim-p
1440
			/* parse end of output line */
1441 ef00af3c Phil Davis
			if (strstr($line, "END") || strstr($line, "ERROR")) {
1442 95305736 jim-p
				break;
1443 ef00af3c Phil Davis
			}
1444 95305736 jim-p
1445
			/* parse client list line */
1446
			if (strstr($line, "CLIENT_LIST")) {
1447
				$list = explode(",", $line);
1448 53663f57 jim-p
				$conn = array();
1449 95305736 jim-p
				$conn['common_name'] = $list[1];
1450
				$conn['remote_host'] = $list[2];
1451
				$conn['virtual_addr'] = $list[3];
1452
				$conn['bytes_recv'] = $list[4];
1453
				$conn['bytes_sent'] = $list[5];
1454
				$conn['connect_time'] = $list[6];
1455 53663f57 jim-p
				$server['conns'][] = $conn;
1456
			}
1457 ec970b50 jim-p
			/* parse routing table lines */
1458
			if (strstr($line, "ROUTING_TABLE")) {
1459
				$list = explode(",", $line);
1460
				$conn = array();
1461
				$conn['virtual_addr'] = $list[1];
1462
				$conn['common_name'] = $list[2];
1463
				$conn['remote_host'] = $list[3];
1464
				$conn['last_time'] = $list[4];
1465
				$server['routes'][] = $conn;
1466
			}
1467 53663f57 jim-p
		}
1468 95305736 jim-p
1469
		/* cleanup */
1470
		fclose($fp);
1471
	} else {
1472
		$conn = array();
1473
		$conn['common_name'] = "[error]";
1474 e8c516a0 Phil Davis
		$conn['remote_host'] = gettext("Unable to contact daemon");
1475
		$conn['virtual_addr'] = gettext("Service not running?");
1476 95305736 jim-p
		$conn['bytes_recv'] = 0;
1477
		$conn['bytes_sent'] = 0;
1478
		$conn['connect_time'] = 0;
1479
		$server['conns'][] = $conn;
1480 53663f57 jim-p
	}
1481 95305736 jim-p
	return $server;
1482 53663f57 jim-p
}
1483
1484
function openvpn_get_active_clients() {
1485 71ca2cb2 Ermal
	global $config, $g;
1486
1487 53663f57 jim-p
	$clients = array();
1488
	if (is_array($config['openvpn']['openvpn-client'])) {
1489
		foreach ($config['openvpn']['openvpn-client'] as & $settings) {
1490 42bb1bee Renato Botelho
1491 ef00af3c Phil Davis
			if (empty($settings) || isset($settings['disable'])) {
1492 6b2dcac5 Ermal
				continue;
1493 ef00af3c Phil Davis
			}
1494 6b2dcac5 Ermal
1495 53663f57 jim-p
			$prot = $settings['protocol'];
1496 95305736 jim-p
			$port = ($settings['local_port']) ? ":{$settings['local_port']}" : "";
1497 42bb1bee Renato Botelho
1498 53663f57 jim-p
			$client = array();
1499
			$client['port'] = $settings['local_port'];
1500 ef00af3c Phil Davis
			if ($settings['description']) {
1501 95305736 jim-p
				$client['name'] = "{$settings['description']} {$prot}{$port}";
1502 ef00af3c Phil Davis
			} else {
1503 95305736 jim-p
				$client['name'] = "Client {$prot}{$port}";
1504 ef00af3c Phil Davis
			}
1505 42bb1bee Renato Botelho
1506 2eaa97b9 jim-p
			$client['vpnid'] = $settings['vpnid'];
1507
			$client['mgmt'] = "client{$client['vpnid']}";
1508
			$socket = "unix://{$g['varetc_path']}/openvpn/{$client['mgmt']}.sock";
1509 53663f57 jim-p
			$client['status']="down";
1510 95305736 jim-p
1511
			$clients[] = openvpn_get_client_status($client, $socket);
1512
		}
1513
	}
1514
	return $clients;
1515
}
1516
1517
function openvpn_get_client_status($client, $socket) {
1518 247c417f Sjon Hortensius
	$errval = null;
1519
	$errstr = null;
1520 95305736 jim-p
	$fp = @stream_socket_client($socket, $errval, $errstr, 1);
1521
	if ($fp) {
1522
		stream_set_timeout($fp, 1);
1523
		/* send our status request */
1524
		fputs($fp, "state 1\n");
1525
1526
		/* recv all response lines */
1527
		while (!feof($fp)) {
1528
			/* read the next line */
1529
			$line = fgets($fp, 1024);
1530
1531
			$info = stream_get_meta_data($fp);
1532 ef00af3c Phil Davis
			if ($info['timed_out']) {
1533 95305736 jim-p
				break;
1534 ef00af3c Phil Davis
			}
1535 95305736 jim-p
1536
			/* Get the client state */
1537 4de8f7ba Phil Davis
			if (strstr($line, "CONNECTED")) {
1538
				$client['status'] = "up";
1539 95305736 jim-p
				$list = explode(",", $line);
1540
1541 4de8f7ba Phil Davis
				$client['connect_time'] = date("D M j G:i:s Y", $list[0]);
1542
				$client['virtual_addr'] = $list[3];
1543 95305736 jim-p
				$client['remote_host'] = $list[4];
1544
			}
1545 4de8f7ba Phil Davis
			if (strstr($line, "CONNECTING")) {
1546
				$client['status'] = "connecting";
1547 453d9c96 jim-p
			}
1548 4de8f7ba Phil Davis
			if (strstr($line, "ASSIGN_IP")) {
1549
				$client['status'] = "waiting";
1550 453d9c96 jim-p
				$list = explode(",", $line);
1551
1552 4de8f7ba Phil Davis
				$client['connect_time'] = date("D M j G:i:s Y", $list[0]);
1553
				$client['virtual_addr'] = $list[3];
1554 453d9c96 jim-p
			}
1555 4de8f7ba Phil Davis
			if (strstr($line, "RECONNECTING")) {
1556
				$client['status'] = "reconnecting";
1557 453d9c96 jim-p
				$list = explode(",", $line);
1558
1559 4de8f7ba Phil Davis
				$client['connect_time'] = date("D M j G:i:s Y", $list[0]);
1560 453d9c96 jim-p
				$client['status'] .= "; " . $list[2];
1561
			}
1562 95305736 jim-p
			/* parse end of output line */
1563 ef00af3c Phil Davis
			if (strstr($line, "END") || strstr($line, "ERROR")) {
1564 95305736 jim-p
				break;
1565 ef00af3c Phil Davis
			}
1566 95305736 jim-p
		}
1567
1568
		/* If up, get read/write stats */
1569
		if (strcmp($client['status'], "up") == 0) {
1570
			fputs($fp, "status 2\n");
1571
			/* recv all response lines */
1572
			while (!feof($fp)) {
1573
				/* read the next line */
1574
				$line = fgets($fp, 1024);
1575
1576
				$info = stream_get_meta_data($fp);
1577 ef00af3c Phil Davis
				if ($info['timed_out']) {
1578 95305736 jim-p
					break;
1579 ef00af3c Phil Davis
				}
1580 95305736 jim-p
1581 4de8f7ba Phil Davis
				if (strstr($line, "TCP/UDP read bytes")) {
1582 95305736 jim-p
					$list = explode(",", $line);
1583
					$client['bytes_recv'] = $list[1];
1584 53663f57 jim-p
				}
1585 95305736 jim-p
1586 4de8f7ba Phil Davis
				if (strstr($line, "TCP/UDP write bytes")) {
1587 95305736 jim-p
					$list = explode(",", $line);
1588
					$client['bytes_sent'] = $list[1];
1589 53663f57 jim-p
				}
1590 95305736 jim-p
1591
				/* parse end of output line */
1592 ef00af3c Phil Davis
				if (strstr($line, "END")) {
1593 95305736 jim-p
					break;
1594 ef00af3c Phil Davis
				}
1595 53663f57 jim-p
			}
1596
		}
1597 95305736 jim-p
1598
		fclose($fp);
1599
1600
	} else {
1601 e8c516a0 Phil Davis
		$client['remote_host'] = gettext("Unable to contact daemon");
1602
		$client['virtual_addr'] = gettext("Service not running?");
1603 95305736 jim-p
		$client['bytes_recv'] = 0;
1604
		$client['bytes_sent'] = 0;
1605
		$client['connect_time'] = 0;
1606 53663f57 jim-p
	}
1607 95305736 jim-p
	return $client;
1608 53663f57 jim-p
}
1609 8e022a76 jim-p
1610 61fce4a6 Phil Davis
function openvpn_kill_client($port, $remipp) {
1611
	global $g;
1612
1613
	//$tcpsrv = "tcp://127.0.0.1:{$port}";
1614
	$tcpsrv = "unix://{$g['varetc_path']}/openvpn/{$port}.sock";
1615
	$errval = null;
1616
	$errstr = null;
1617
1618
	/* open a tcp connection to the management port of each server */
1619
	$fp = @stream_socket_client($tcpsrv, $errval, $errstr, 1);
1620
	$killed = -1;
1621
	if ($fp) {
1622
		stream_set_timeout($fp, 1);
1623
		fputs($fp, "kill {$remipp}\n");
1624
		while (!feof($fp)) {
1625
			$line = fgets($fp, 1024);
1626
1627
			$info = stream_get_meta_data($fp);
1628
			if ($info['timed_out']) {
1629
				break;
1630
			}
1631
1632
			/* parse header list line */
1633
			if (strpos($line, "INFO:") !== false) {
1634
				continue;
1635
			}
1636
			if (strpos($line, "SUCCESS") !== false) {
1637
				$killed = 0;
1638
			}
1639
			break;
1640
		}
1641
		fclose($fp);
1642
	}
1643
	return $killed;
1644
}
1645
1646 8e022a76 jim-p
function openvpn_refresh_crls() {
1647
	global $g, $config;
1648
1649 a8f538a8 jim-p
	openvpn_create_dirs();
1650 8e022a76 jim-p
1651
	if (is_array($config['openvpn']['openvpn-server'])) {
1652
		foreach ($config['openvpn']['openvpn-server'] as $settings) {
1653 ef00af3c Phil Davis
			if (empty($settings)) {
1654 8e022a76 jim-p
				continue;
1655 ef00af3c Phil Davis
			}
1656
			if (isset($settings['disable'])) {
1657 8e022a76 jim-p
				continue;
1658 ef00af3c Phil Davis
			}
1659 8e022a76 jim-p
			// Write the settings for the keys
1660 ef00af3c Phil Davis
			switch ($settings['mode']) {
1661 8e022a76 jim-p
				case 'p2p_tls':
1662
				case 'server_tls':
1663
				case 'server_tls_user':
1664
				case 'server_user':
1665
					if (!empty($settings['crlref'])) {
1666
						$crl = lookup_crl($settings['crlref']);
1667 728003c8 jim-p
						crl_update($crl);
1668 8e022a76 jim-p
						$fpath = $g['varetc_path']."/openvpn/server{$settings['vpnid']}.crl-verify";
1669
						file_put_contents($fpath, base64_decode($crl['text']));
1670
						@chmod($fpath, 0644);
1671
					}
1672
					break;
1673
			}
1674
		}
1675
	}
1676
}
1677
1678 a8f538a8 jim-p
function openvpn_create_dirs() {
1679 154b0f80 jim-p
	global $g, $config, $openvpn_tls_server_modes;
1680 ef00af3c Phil Davis
	if (!is_dir("{$g['varetc_path']}/openvpn")) {
1681 a8f538a8 jim-p
		safe_mkdir("{$g['varetc_path']}/openvpn", 0750);
1682 ef00af3c Phil Davis
	}
1683
	if (!is_dir("{$g['varetc_path']}/openvpn-csc")) {
1684 a8f538a8 jim-p
		safe_mkdir("{$g['varetc_path']}/openvpn-csc", 0750);
1685 ef00af3c Phil Davis
	}
1686 154b0f80 jim-p
1687
	/* Check for enabled servers and create server-specific CSC dirs */
1688
	if (is_array($config['openvpn']['openvpn-server'])) {
1689
		foreach ($config['openvpn']['openvpn-server'] as $settings) {
1690
			if (isset($settings['disable'])) {
1691
				continue;
1692
			}
1693
			if (in_array($settings['mode'], $openvpn_tls_server_modes)) {
1694
				if ($settings['vpnid']) {
1695
					safe_mkdir("{$g['varetc_path']}/openvpn-csc/server{$settings['vpnid']}");
1696
				}
1697
			}
1698
		}
1699
	}
1700 a8f538a8 jim-p
}
1701
1702 30c8a290 Renato Botelho
function openvpn_get_interface_ip($ip, $cidr) {
1703
	$subnet = gen_subnetv4($ip, $cidr);
1704
	$ip1 = ip_after($subnet);
1705
	$ip2 = ip_after($ip1);
1706 91c44185 jim-p
	return array($ip1, $ip2);
1707
}
1708
1709
function openvpn_get_interface_ipv6($ipv6, $prefix) {
1710
	$basev6 = gen_subnetv6($ipv6, $prefix);
1711
	// Is there a better way to do this math?
1712
	$ipv6_arr = explode(':', $basev6);
1713
	$last = hexdec(array_pop($ipv6_arr));
1714 fe9c774d Phil Davis
	$ipv6_1 = Net_IPv6::compress(Net_IPv6::uncompress(implode(':', $ipv6_arr) . ':' . dechex($last + 1)));
1715
	$ipv6_2 = Net_IPv6::compress(Net_IPv6::uncompress(implode(':', $ipv6_arr) . ':' . dechex($last + 2)));
1716 91c44185 jim-p
	return array($ipv6_1, $ipv6_2);
1717
}
1718
1719
function openvpn_clear_route($mode, $settings) {
1720 ef00af3c Phil Davis
	if (empty($settings['tunnel_network'])) {
1721 91c44185 jim-p
		return;
1722 ef00af3c Phil Davis
	}
1723 966cdb43 jim-p
	list($ip, $cidr) = explode('/', trim($settings['tunnel_network']));
1724 91c44185 jim-p
	$mask = gen_subnet_mask($cidr);
1725 6ca938cf jim-p
	$clear_route = false;
1726
1727 ef00af3c Phil Davis
	switch ($settings['mode']) {
1728 6ca938cf jim-p
		case 'shared_key':
1729
			$clear_route = true;
1730
			break;
1731 91c44185 jim-p
		case 'p2p_tls':
1732
		case 'p2p_shared_key':
1733 ef00af3c Phil Davis
			if ($cidr == 30) {
1734 6ca938cf jim-p
				$clear_route = true;
1735 ef00af3c Phil Davis
			}
1736 91c44185 jim-p
			break;
1737
	}
1738 6ca938cf jim-p
1739 6d0b9fe9 jim-p
	if ($clear_route && !empty($ip) && !empty($mask)) {
1740 30c8a290 Renato Botelho
		list($ip1, $ip2) = openvpn_get_interface_ip($ip, $cidr);
1741 6ca938cf jim-p
		$ip_to_clear = ($mode == "server") ? $ip1 : $ip2;
1742 615d7f0a Ermal
		/* XXX: Family for route? */
1743 6ca938cf jim-p
		mwexec("/sbin/route -q delete {$ip_to_clear}");
1744
	}
1745 91c44185 jim-p
}
1746
1747 5c427ce7 jim-p
function openvpn_gen_routes($value, $ipproto = "ipv4", $push = false, $iroute = false) {
1748 a28d40cb jim-p
	$routes = "";
1749 ef00af3c Phil Davis
	if (empty($value)) {
1750 a28d40cb jim-p
		return "";
1751 ef00af3c Phil Davis
	}
1752 a28d40cb jim-p
	$networks = explode(',', $value);
1753
1754
	foreach ($networks as $network) {
1755 ef00af3c Phil Davis
		if ($ipproto == "ipv4") {
1756 5c427ce7 jim-p
			$route = openvpn_gen_route_ipv4($network, $iroute);
1757 ef00af3c Phil Davis
		} else {
1758 5c427ce7 jim-p
			$route = openvpn_gen_route_ipv6($network, $iroute);
1759 ef00af3c Phil Davis
		}
1760 a28d40cb jim-p
1761 ef00af3c Phil Davis
		if ($push) {
1762 a28d40cb jim-p
			$routes .= "push \"{$route}\"\n";
1763 ef00af3c Phil Davis
		} else {
1764 a28d40cb jim-p
			$routes .= "{$route}\n";
1765 ef00af3c Phil Davis
		}
1766 a28d40cb jim-p
	}
1767
	return $routes;
1768
}
1769
1770 5c427ce7 jim-p
function openvpn_gen_route_ipv4($network, $iroute = false) {
1771
	$i = ($iroute) ? "i" : "";
1772 a28d40cb jim-p
	list($ip, $mask) = explode('/', trim($network));
1773
	$mask = gen_subnet_mask($mask);
1774 5c427ce7 jim-p
	return "{$i}route $ip $mask";
1775 a28d40cb jim-p
}
1776
1777 5c427ce7 jim-p
function openvpn_gen_route_ipv6($network, $iroute = false) {
1778
	$i = ($iroute) ? "i" : "";
1779 a28d40cb jim-p
	list($ipv6, $prefix) = explode('/', trim($network));
1780 ef00af3c Phil Davis
	if (empty($prefix)) {
1781 a28d40cb jim-p
		$prefix = "128";
1782 ef00af3c Phil Davis
	}
1783 5c427ce7 jim-p
	return "{$i}route-ipv6 ${ipv6}/${prefix}";
1784 a28d40cb jim-p
}
1785
1786 699125b1 jim-p
function openvpn_get_settings($mode, $vpnid) {
1787
	global $config;
1788
1789
	if (is_array($config['openvpn']['openvpn-server'])) {
1790
		foreach ($config['openvpn']['openvpn-server'] as $settings) {
1791 ef00af3c Phil Davis
			if (isset($settings['disable'])) {
1792 699125b1 jim-p
				continue;
1793 ef00af3c Phil Davis
			}
1794 699125b1 jim-p
1795 ef00af3c Phil Davis
			if ($vpnid != 0 && $vpnid == $settings['vpnid']) {
1796 699125b1 jim-p
				return $settings;
1797 ef00af3c Phil Davis
			}
1798 699125b1 jim-p
		}
1799
	}
1800
1801
	if (is_array($config['openvpn']['openvpn-client'])) {
1802
		foreach ($config['openvpn']['openvpn-client'] as $settings) {
1803 ef00af3c Phil Davis
			if (isset($settings['disable'])) {
1804 699125b1 jim-p
				continue;
1805 ef00af3c Phil Davis
			}
1806 699125b1 jim-p
1807 ef00af3c Phil Davis
			if ($vpnid != 0 && $vpnid == $settings['vpnid']) {
1808 699125b1 jim-p
				return $settings;
1809 ef00af3c Phil Davis
			}
1810 699125b1 jim-p
		}
1811
	}
1812
1813
	return array();
1814
}
1815
1816
function openvpn_restart_by_vpnid($mode, $vpnid) {
1817
	$settings = openvpn_get_settings($mode, $vpnid);
1818
	openvpn_restart($mode, $settings);
1819
}
1820
1821 756720e2 Pierre POMES
?>