Project

General

Profile

Download (19 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2

    
3
/* $Id$ */
4
/*
5
	$RCSfile$
6
	
7
	Copyright (C) 2008 Scott Ullrich <sullrich@gmail.com>
8
	All rights reserved.
9
	
10
	Copyright (C) 2006  Fernando Lemos
11
	All rights reserved.
12

    
13
	This file was rewritten from scratch by Fernando Lemos but
14
	*MIGHT* contain code previously written by:
15

    
16
	Copyright (C) 2005 Peter Allgeyer <allgeyer_AT_web.de>
17
	All rights reserved.
18

    
19
	Copyright (C) 2004 Peter Curran (peter@closeconsultants.com).
20
	All rights reserved.
21

    
22
	Redistribution and use in source and binary forms, with or without
23
	modification, are permitted provided that the following conditions are met:
24

    
25
	1. Redistributions of source code must retain the above copyright notices,
26
	   this list of conditions and the following disclaimer.
27

    
28
	2. Redistributions in binary form must reproduce the above copyright
29
	   notices, this list of conditions and the following disclaimer in the
30
	   documentation and/or other materials provided with the distribution.
31

    
32
	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
33
	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
34
	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
35
	AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
36
	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
37
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
38
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
39
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41
	POSSIBILITY OF SUCH DAMAGE.
42
	
43
	DISABLE_PHP_LINT_CHECKING
44
	
45
	pfSense_BUILDER_BINARIES:	/usr/local/sbin/openvpn	/usr/bin/openssl	/sbin/ifconfig
46
	pfSense_MODULE:	openvpn
47

    
48
*/
49
require_once('config.inc');
50
require_once("certs.inc");
51
require_once('pfsense-utils.inc');
52

    
53
$openvpn_prots = array("UDP", "TCP");
54

    
55
$openvpn_dev_mode = array("tun", "tap");
56

    
57
/* 
58
 * 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
$openvpn_dh_lengths = array(
71
	1024, 2048, 4096 );
72

    
73
$openvpn_server_modes = array(
74
	'p2p_tls' => "Peer to Peer ( SSL/TLS )",
75
	'p2p_shared_key' => "Peer to Peer ( Shared Key )",
76
	'server_tls' => "Remote Access ( SSL/TLS )",
77
//	'server_user' => "Remote Access ( User Auth )",
78
	'server_tls_user' => "Remote Access ( SSL/TLS + User Auth )");
79

    
80
$openvpn_client_modes = array(
81
	'p2p_tls' => "Peer to Peer ( SSL/TLS )",
82
	'p2p_shared_key' => "Peer to Peer ( Shared Key )" );
83

    
84
function openvpn_create_key() {
85

    
86
	$fp = popen("/usr/local/sbin/openvpn --genkey --secret /dev/stdout 2>/dev/null", "r");
87
	if (!$fp)
88
		return false;
89

    
90
	$rslt = stream_get_contents($fp);
91
	pclose($fp);
92

    
93
	return $rslt;
94
}
95

    
96
function openvpn_create_dhparams($bits) {
97

    
98
	$fp = popen("/usr/bin/openssl dhparam {$bits} 2>/dev/null", "r");
99
	if (!$fp)
100
		return false;
101

    
102
	$rslt = stream_get_contents($fp);
103
	pclose($fp);
104

    
105
	return $rslt;
106
}
107

    
108
function openvpn_vpnid_used($vpnid) {
109
	global $config;
110

    
111
	if (is_array($config['openvpn']['openvpn-server']))
112
		foreach ($config['openvpn']['openvpn-server'] as & $settings)
113
			if ($vpnid == $settings['vpnid'])
114
				return true;
115

    
116
	if (is_array($config['openvpn']['openvpn-client']))
117
		foreach ($config['openvpn']['openvpn-client'] as & $settings)
118
			if ($vpnid == $settings['vpnid'])
119
				return true;
120

    
121
	return false;
122
}
123

    
124
function openvpn_vpnid_next() {
125

    
126
	$vpnid = 1;
127
	while(openvpn_vpnid_used($vpnid))
128
		$vpnid++;
129

    
130
	return $vpnid;
131
}
132

    
133
function openvpn_port_used($prot, $port) {
134
	global $config;
135

    
136
	if (is_array($config['openvpn']['openvpn-server']))
137
		foreach ($config['openvpn']['openvpn-server'] as & $settings)
138
			if ($port == $settings['local_port'] &&
139
				$prot == $settings['protocol'])
140
				return $settings['vpnid'];
141

    
142
	if (is_array($config['openvpn']['openvpn-client']))
143
		foreach ($config['openvpn']['openvpn-client'] as & $settings)
144
			if ($port == $settings['local_port'] &&
145
				$prot == $settings['protocol'])
146
				return $settings['vpnid'];
147

    
148
	return 0;
149
}
150

    
151
function openvpn_port_next($prot) {
152

    
153
	$port = 1194;
154
	while(openvpn_port_used($prot, $port))
155
		$port++;
156

    
157
	return $port;
158
}
159

    
160
function openvpn_get_cipherlist() {
161

    
162
	$ciphers = array();
163
	$cipher_out = shell_exec('openvpn --show-ciphers | grep "default key" | awk \'{print $1, "(" $2 "-" $3 ")";}\'');
164
	$cipher_lines = explode("\n", trim($cipher_out));
165
	sort($cipher_lines);
166
	foreach ($cipher_lines as $line) {
167
		$words = explode(' ', $line);
168
		$ciphers[$words[0]] = "{$words[0]} {$words[1]}";
169
	}
170

    
171
	return $ciphers;
172
}
173

    
174
function openvpn_validate_host($value, $name) {
175
	$value = trim($value);
176
	if (empty($value) || (!is_domain($value) && !is_ipaddr($value)))
177
		return "The field '$name' must contain a valid IP address or domain name.";
178
	return false;
179
}
180

    
181
function openvpn_validate_port($value, $name) {
182
	$value = trim($value);
183
	if (empty($value) || !is_numeric($value) || $value < 0 || ($value > 65535))
184
		return "The field '$name' must contain a valid port, ranging from 0 to 65535.";
185
	return false;
186
}
187

    
188
function openvpn_validate_cidr($value, $name) {
189
	$value = trim($value);
190
	if (!empty($value)) {
191
		list($ip, $mask) = explode('/', $value);
192
		if (!is_ipaddr($ip) or !is_numeric($mask) or ($mask > 32) or ($mask < 0))
193
			return "The field '$name' must contain a valid CIDR range.";
194
	}
195
	return false;
196
}
197

    
198
function openvpn_add_dhcpopts(& $settings, & $conf) {
199

    
200
	if (!empty($settings['dns_domain'])) 
201
		$conf .= "push \"dhcp-option DOMAIN {$settings['dns_domain']}\"\n";
202

    
203
	if (!empty($settings['dns_server1']))
204
		$conf .= "push \"dhcp-option DNS {$settings['dns_server1']}\"\n";
205
	if (!empty($settings['dns_server2']))
206
		$conf .= "push \"dhcp-option DNS {$settings['dns_server2']}\"\n";
207
	if (!empty($settings['dns_server3']))
208
		$conf .= "push \"dhcp-option DNS {$settings['dns_server3']}\"\n";
209
	if (!empty($settings['dns_server4']))
210
		$conf .= "push \"dhcp-option DNS {$settings['dns_server4']}\"\n";
211

    
212
	if (!empty($settings['ntp_server1']))
213
		$conf .= "push \"dhcp-option NTP {$settings['dhcp_ntp']}\"\n";
214
	if (!empty($settings['ntp_server2']))
215
		$conf .= "push \"dhcp-option NTP {$settings['dhcp_ntp']}\"\n";
216

    
217
	if ($settings['netbios_enable']) {
218

    
219
		if (!empty($settings['dhcp_nbttype']) && ($settings['dhcp_nbttype'] != 0))
220
			$conf .= "push \"dhcp-option NBT {$settings['dhcp_nbttype']}\"\n";
221
		if (!empty($settings['dhcp_nbtscope'])) 
222
			$conf .= "push \"dhcp-option NBS {$settings['dhcp_nbtscope']}\"\n";
223

    
224
		if (!empty($settings['wins_server1']))
225
			$conf .= "push \"dhcp-option WINS {$settings['wins_server1']}\"\n";
226
		if (!empty($settings['wins_server2']))
227
			$conf .= "push \"dhcp-option WINS {$settings['wins_server2']}\"\n";
228

    
229
		if (!empty($settings['nbdd_server1']))
230
			$conf .= "push \"dhcp-option NBDD {$settings['nbdd_server1']}\"\n";
231
	}
232

    
233
	if ($settings['gwredir']) 
234
		$conf .= "push \"redirect-gateway def1\"\n";
235
}
236

    
237
function openvpn_add_custom(& $settings, & $conf) {
238

    
239
	if ($settings['custom_options']) {
240

    
241
		$options = explode(';', $settings['custom_options']);
242

    
243
		if (is_array($options)) {
244
			foreach ($options as $option)
245
				$conf .= "$option\n";
246
		} else
247
			$conf .= "{$settings['custom_options']}\n";
248
	}
249
}
250

    
251
function openvpn_add_keyfile(& $data, & $conf, $mode_id, $directive, $opt = "") {
252
	global $g;
253

    
254
	$fpath = $g['varetc_path']."/openvpn/{$mode_id}.{$directive}";
255
	file_put_contents($fpath, base64_decode($data));
256
	chown($fpath, 'nobody');
257
	chgrp($fpath, 'nobody');
258

    
259
	$conf .= "{$directive} {$fpath} {$opt}\n";
260
}
261

    
262
function openvpn_reconfigure($mode,& $settings) {
263
	global $g, $config;
264

    
265
	if (empty($settings))
266
		return;
267
	if ($settings['disable']) 
268
		return;
269

    
270
	/*
271
	 * NOTE: Deleting tap devices causes spontaneous reboots. Instead,
272
	 * we use a vpnid number which is allocated for a particular client
273
	 * or server configuration. ( see openvpn_vpnid_next() )
274
	 */
275

    
276
	$vpnid = $settings['vpnid'];
277
	$mode_id = $mode.$vpnid;
278

    
279
	if (isset($settings['dev_mode']))
280
		$tunname = "{$settings['dev_mode']}{$vpnid}";
281
	else {	/* defaults to tun */
282
		$tunname = "tun{$vpnid}";
283
		$settings['dev_mode'] = "tun";
284
	}
285

    
286
	if ($mode == "server")
287
		$devname = "ovpns{$vpnid}";
288
	else
289
		$devname = "ovpnc{$vpnid}";
290

    
291
	/* is our device already configured */
292
	if (mwexec("/sbin/ifconfig {$devname}")) {
293

    
294
		/* create the tap device if required */
295
		if (!file_exists("/dev/{$tunname}"))
296
			exec("/sbin/ifconfig {$tunname} create");
297

    
298
		/* rename the device */
299
		mwexec("/sbin/ifconfig {$tunname} name {$devname}");
300

    
301
		/* add the device to the openvpn group */
302
		mwexec("/sbin/ifconfig {$devname} group openvpn");
303
	}
304

    
305
	$pfile = $g['varrun_path'] . "/openvpn_{$mode_id}.pid";
306
	$proto = ($settings['protocol'] == 'UDP' ? 'udp' : "tcp-{$mode}");
307
	$cipher = $settings['crypto'];
308

    
309
	$interface = $settings['interface'];
310
	$ipaddr = $settings['ipaddr'];
311

    
312
	// If a specific ip address (VIP) is requested, use it.
313
	// Otherwise, if a specific interface is requested, use it
314
	// If "any" interface was selected, local directive will be ommited.
315
	if (!empty($ipaddr)) {
316
		$iface_ip=$ipaddr;
317
	} else {
318
		if ((!empty($interface)) && (strcmp($interface, "any"))) {
319
			$iface_ip=get_interface_ip($interface);
320
		}
321
	}
322

    
323
	$conf  = "dev {$devname}\n";
324
	$conf .= "dev-type {$settings['dev_mode']}\n";
325
	$conf .= "dev-node /dev/{$tunname}\n";
326
	$conf .= "writepid {$pfile}\n";
327
	$conf .= "#user nobody\n";
328
	$conf .= "#group nobody\n";
329
	$conf .= "script-security 3\n";
330
	$conf .= "daemon\n";
331
	$conf .= "keepalive 10 60\n";
332
	$conf .= "ping-timer-rem\n";
333
	$conf .= "persist-tun\n";
334
	$conf .= "persist-key\n";
335
	$conf .= "proto {$proto}\n";
336
	$conf .= "cipher {$cipher}\n";
337
	$conf .= "up /etc/rc.filter_configure\n";
338
	$conf .= "down /etc/rc.filter_configure\n";
339

    
340
	if (!empty($iface_ip)) {
341
		$conf .= "local {$iface_ip}\n";	
342
	}
343

    
344
	// server specific settings
345
	if ($mode == 'server') {
346

    
347
		list($ip, $mask) = explode('/', $settings['tunnel_network']);
348
		$mask = gen_subnet_mask($mask);
349

    
350
		// configure tls modes
351
		switch($settings['mode']) {
352
			case 'p2p_tls':
353
			case 'server_tls':
354
			case 'server_tls_user':
355
				$conf .= "tls-server\n";
356
				break;
357
		}
358

    
359
		// configure p2p/server modes
360
		switch($settings['mode']) {
361
			case 'p2p_tls':
362
			case 'p2p_shared_key':
363
				$baselong = ip2long($ip) & ip2long($mask);
364
				$ip1 = long2ip($baselong + 1);
365
				$ip2 = long2ip($baselong + 2);
366
				$conf .= "ifconfig $ip1 $ip2\n";
367
				break;
368
			case 'server_tls':
369
			case 'server_user':
370
			case 'server_tls_user':
371
				$conf .= "server {$ip} {$mask}\n";
372
				$conf .= "client-config-dir {$g['varetc_path']}/openvpn-csc\n";
373
				break;
374
		}
375

    
376
		// configure user auth modes
377
		switch($settings['mode']) {
378
			case 'server_user':
379
				$conf .= "client-cert-not-required\n";
380
			case 'server_tls_user':
381
				$conf .= "username-as-common-name\n";
382
				if ($settings['authmode'] == "local")
383
					$conf .= "auth-user-pass-verify /etc/inc/openvpn.auth-user.php via-env\n";
384
				else {
385
					$authcfg = system_get_authserver($settings['authmode']);
386
					if ($authcfg) {
387
						mwexec("/bin/cat /etc/inc/openvpn.auth-{$authcfg['type']}.php | /usr/bin/sed 's/\/\/<template>/\$authmode=\"{$authcfg['name']}\";/g' >  {$g['varetc_path']}/openvpn/{$mode_id}.php");
388
						mwexec("/bin/chmod a+x {$g['varetc_path']}/openvpn/{$mode_id}.php");
389
						$conf .= "auth-user-pass-verify {$g['varetc_path']}/openvpn/{$mode_id}.php via-env\n";
390
					}
391
				}
392
				break;
393
		}
394

    
395
		// The local port to listen on
396
		$conf .= "lport {$settings['local_port']}\n";
397

    
398
		// The management port to listen on
399
		$conf .= "management 127.0.0.1 {$settings['local_port']}\n";
400

    
401
		if ($settings['maxclients'])
402
			$conf .= "max-clients {$settings['maxclients']}\n";
403

    
404
		// Can we push routes
405
		if ($settings['local_network']) {
406
			list($ip, $mask) = explode('/', $settings['local_network']);
407
			$mask = gen_subnet_mask($mask);
408
			$conf .= "push \"route $ip $mask\"\n";
409
		}
410

    
411
		// Configure client dhcp options
412
		switch($settings['mode']) {
413
			case 'server_tls':
414
			case 'server_user':
415
			case 'server_tls_user':
416
				openvpn_add_dhcpopts($settings, $conf);
417
				break;
418
		}
419
	}
420

    
421
	// client specific settings
422

    
423
	if ($mode == 'client') {
424

    
425
		// configure p2p mode
426
		switch($settings['mode']) {
427
			case 'p2p_tls':
428
				$conf .= "tls-client\n";
429
			case 'shared_key':
430
				$conf .= "client\n";
431
				break;
432
		}
433

    
434
		// The port we'll listen at
435
		// If local_port is used, bing the management port
436
		if ($settings['local_port']) {
437
			$conf .= "lport {$settings['local_port']}\n";
438
			$conf .= "management 127.0.0.1 {$settings['local_port']}\n";
439
		}
440
		else
441
			$conf .= "nobind\n";
442

    
443
		// The remote server
444
		$conf .= "remote {$settings['server_addr']} {$settings['server_port']}\n";
445

    
446
		if (!empty($settings['use_shaper']))
447
			$conf .= "shaper {$settings['use_shaper']}\n";
448

    
449
		if (!empty($settings['tunnel_network'])) {
450
			list($ip, $mask) = explode('/', $settings['tunnel_network']);
451
			$mask = gen_subnet_mask($mask);
452
			$baselong = ip2long($ip) & ip2long($mask);
453
			$ip1 = long2ip($baselong + 1);
454
			$ip2 = long2ip($baselong + 2);
455
			$conf .= "ifconfig $ip2 $ip1\n";
456
		}
457

    
458
		if ($settings['proxy_addr']) {
459
			$conf .= "http-proxy {$settings['proxy_addr']} {$settings['proxy_port']}";
460
			if ($settings['proxy_authtype'] != "none") {
461
				$conf .= " {$g['varetc_path']}/openvpn/{$mode_id}.pas {$settings['proxy_authtype']}";
462
				$proxypas = "{$settings['proxy_user']}\n";
463
				$proxypas .= "{$settings['proxy_passwd']}\n";
464
				file_put_contents("{$g['varetc_path']}/openvpn/{$mode_id}.pas", $proxypas);
465
			}
466
			$conf .= " \n";
467
		}
468
	}
469

    
470
	// Add a remote network route if set
471
	if ($settings['remote_network']) {
472
		list($ip, $mask) = explode('/', $settings['remote_network']);
473
		$mask = gen_subnet_mask($mask);
474
		$conf .= "route $ip $mask\n";
475
	}
476

    
477
	// Write the settings for the keys
478
	switch($settings['mode']) {
479
		case 'p2p_shared_key':
480
			openvpn_add_keyfile($settings['shared_key'], $conf, $mode_id, "secret");
481
			break;
482
		case 'p2p_tls':
483
		case 'server_tls':
484
		case 'server_tls_user':
485
			$ca = lookup_ca($settings['caref']);
486
			openvpn_add_keyfile($ca['crt'], $conf, $mode_id, "ca");
487
		case 'server_user':
488
			$cert = lookup_cert($settings['certref']);
489
			openvpn_add_keyfile($cert['crt'], $conf, $mode_id, "cert");
490
			openvpn_add_keyfile($cert['prv'], $conf, $mode_id, "key");
491
			if ($mode == 'server')
492
				$conf .= "dh {$g['etc_path']}/dh-parameters.{$settings['dh_length']}\n";
493
			if ($settings['crl'])
494
				openvpn_add_keyfile($settings['crl'], $conf, $mode_id, "crl-verify");
495
			if ($settings['tls']) {
496
				if ($settings['mode'] == "server_tls" || $settings['mode'] == "server_tls_user")
497
					$tlsopt = 0;
498
				else
499
					$tlsopt = 1;
500
				openvpn_add_keyfile($settings['tls'], $conf, $mode_id, "tls-auth", $tlsopt);
501
			}
502
			break;
503
	}
504

    
505
	if ($settings['compression'])
506
		$conf .= "comp-lzo\n";
507

    
508
	if ($settings['passtos'])
509
		$conf .= "passtos\n";
510

    
511
	if ($settings['resolve_retry'])
512
		$conf .= "resolv-retry infinite\n";
513

    
514
	if ($settings['dynamic_ip']) {
515
		$conf .= "persist-remote-ip\n";
516
		$conf .= "float\n";
517
	}
518

    
519
	openvpn_add_custom($settings, $conf);
520

    
521
	$fpath = $g['varetc_path']."/openvpn/{$mode_id}.conf";
522
	file_put_contents($fpath, $conf);
523
	chown($fpath, 'nobody');
524
	chgrp($fpath, 'nobody');
525
}
526

    
527
function openvpn_restart($mode, & $settings) {
528
	global $g, $config;
529

    
530
	$vpnid = $settings['vpnid'];
531
	$mode_id = $mode.$vpnid;
532

    
533
	/* kill the process if running */
534
	$pfile = $g['varrun_path']."/openvpn_{$mode_id}.pid";
535
	if (file_exists($pfile)) {
536

    
537
		/* read the pid file */
538
		$pid = rtrim(file_get_contents($pfile));
539
		unlink($pfile);
540

    
541
		/* send a term signal to the process */
542
		posix_kill($pid, SIGTERM);
543

    
544
		/* wait until the process exits */
545
		while(posix_kill($pid, 0))
546
			usleep(250000);
547
	}
548

    
549
	if ($settings['disable'])
550
		return;
551

    
552
	/* start the new process */
553
	$fpath = $g['varetc_path']."/openvpn/{$mode_id}.conf";
554
	mwexec_bg("nohup openvpn --config {$fpath}");
555
	touch("{$g['tmp_path']}/filter_dirty");
556
}
557

    
558
function openvpn_delete($mode, & $settings) {
559
	global $g, $config;
560

    
561
	$vpnid = $settings['vpnid'];
562
	$mode_id = $mode.$vpnid;
563

    
564
	$tunname = "tun{$vpnid}";
565
	if ($mode == "server")
566
		$devname = "ovpns{$vpnid}";
567
	else
568
		$devname = "ovpnc{$vpnid}";
569

    
570
	/* kill the process if running */
571
	$pfile = "{$g['varrun_path']}/openvpn_{$mode_id}.pid";
572
	if (file_exists($pfile)) {
573

    
574
		/* read the pid file */
575
		$pid = trim(file_get_contents($pfile));
576
		unlink($pfile);
577

    
578
		/* send a term signal to the process */
579
		posix_kill($pid, SIGTERM);
580
	}
581

    
582
	/* remove the device from the openvpn group */
583
	mwexec("/sbin/ifconfig {$devname} -group openvpn");
584

    
585
	/* restore the original adapter name */
586
	mwexec("/sbin/ifconfig {$devname} name {$tunname}");
587

    
588
	/* remove the configuration files */
589
	mwexec("/bin/rm {$g['varetc_path']}/openvpn/{$mode_id}.*");
590
}
591

    
592
function openvpn_resync_csc(& $settings) {
593
	global $g, $config;
594

    
595
	$fpath = $g['varetc_path']."/openvpn-csc/".$settings['common_name'];
596

    
597
	if ($settings['disable']) {
598
		unlink_if_exists($fpath);
599
		return;
600
	}
601

    
602
	$conf = '';
603
	if ($settings['block'])
604
		$conf .= "disable\n";
605

    
606
	if ($settings['push_reset'])
607
		$conf .= "push-reset\n";
608

    
609
	if (!empty($settings['tunnel_network'])) {
610
		list($ip, $mask) = explode('/', $settings['tunnel_network']);
611
		$baselong = ip2long($ip) & gen_subnet_mask_long($mask);
612
		$ip1 = long2ip($baselong + 1);
613
		$ip2 = long2ip($baselong + 2);
614
		$conf .= "ifconfig-push {$ip1} {$ip2}\n";
615
	}
616

    
617
	openvpn_add_dhcpopts($settings, $conf);
618

    
619
	if ($settings['gwredir'])
620
		$conf .= "push \"redirect-gateway def1\"\n";
621

    
622
	openvpn_add_custom($settings, $conf);
623

    
624
	file_put_contents($fpath, $conf);
625
	chown($fpath, 'nobody');
626
	chgrp($fpath, 'nobody');
627
}
628

    
629
function openvpn_delete_csc(& $settings) {
630
	global $g, $config;
631

    
632
	$fpath = $g['varetc_path']."/openvpn-csc/".$settings['common_name'];
633
	unlink_if_exists($fpath);
634
}
635

    
636
// Resync the configuration and restart the VPN
637
function openvpn_resync($mode, & $settings) {
638
	openvpn_reconfigure($mode, $settings);
639
	openvpn_restart($mode, $settings);
640
}
641

    
642
// Resync and restart all VPNs
643
function openvpn_resync_all() {
644
	global $g, $config;
645

    
646
	// delay our setup until the system
647
	// has a chance to init our paths
648
	if (!file_exists($g['varetc_path']."/openvpn") ||
649
		!file_exists($g['varetc_path']."/openvpn-csc"))
650
		return;
651

    
652
	if (!is_array($config['openvpn']))
653
		$config['openvpn'] = array();
654

    
655
/*
656
	if (!$config['openvpn']['dh-parameters']) {
657
		echo "Configuring OpenVPN Parameters ...\n";
658
		$dh_parameters = openvpn_create_dhparams(1024);
659
		$dh_parameters = base64_encode($dh_parameters);
660
		$config['openvpn']['dh-parameters'] = $dh_parameters;
661
		write_config("OpenVPN DH parameters");
662
	}
663

    
664
	$path_ovdh = $g['varetc_path']."/openvpn/dh-parameters";
665
	if (!file_exists($path_ovdh)) {
666
		$dh_parameters = $config['openvpn']['dh-parameters'];
667
		$dh_parameters = base64_decode($dh_parameters);
668
		file_put_contents($path_ovdh, $dh_parameters);
669
	}
670
*/
671

    
672
	if (is_array($config['openvpn']['openvpn-server']))
673
		foreach ($config['openvpn']['openvpn-server'] as & $settings)
674
			openvpn_resync('server', $settings);
675

    
676
	if (is_array($config['openvpn']['openvpn-client']))
677
		foreach ($config['openvpn']['openvpn-client'] as & $settings)
678
			openvpn_resync('client', $settings);
679

    
680
	if (is_array($config['openvpn']['openvpn-csc']))
681
		foreach ($config['openvpn']['openvpn-csc'] as & $settings)
682
			openvpn_resync_csc($settings);
683

    
684
}
685

    
686
?>
(30-30/52)