Project

General

Profile

Download (19.3 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
						switch ($authcfg['type']) {
388
						case 'ldap':
389
							$sed = "\$ldaphost={$authcfg['host']};";
390
							$sed .= "\$ldapport={$authcfg['ldap_port']};";
391
							$sed .= "\$ldapuserattr={$authcfg['ldap_attr_user']};";
392
							$sed .= "\$ldapbasedn={$authcfg['ldap_basedn']};";
393
							break;
394
						case 'radius':
395
							$sed = "\$radsrv={$authcfg['host']};";
396
							$sed .= "\$radport={$authcfg['radius_auth_port']};";
397
							$sed .= "\$radsecret={$authcfg['radius_secret']};";
398
							break;
399
						}
400
						mwexec("/bin/cat /etc/inc/openvpn.auth-{$authcfg['type']}.php | /usr/bin/sed 's/\/\/<template>/{$sed}/g' >  {$g['varetc_path']}/openvpn/{$mode_id}.php");
401
						mwexec("/bin/chmod a+x {$g['varetc_path']}/openvpn/{$mode_id}.php");
402
						$conf .= "auth-user-pass-verify {$g['varetc_path']}/openvpn/{$mode_id}.php via-env\n";
403
					}
404
				}
405
				break;
406
		}
407

    
408
		// The local port to listen on
409
		$conf .= "lport {$settings['local_port']}\n";
410

    
411
		// The management port to listen on
412
		$conf .= "management 127.0.0.1 {$settings['local_port']}\n";
413

    
414
		if ($settings['maxclients'])
415
			$conf .= "max-clients {$settings['maxclients']}\n";
416

    
417
		// Can we push routes
418
		if ($settings['local_network']) {
419
			list($ip, $mask) = explode('/', $settings['local_network']);
420
			$mask = gen_subnet_mask($mask);
421
			$conf .= "push \"route $ip $mask\"\n";
422
		}
423

    
424
		// Configure client dhcp options
425
		switch($settings['mode']) {
426
			case 'server_tls':
427
			case 'server_user':
428
			case 'server_tls_user':
429
				openvpn_add_dhcpopts($settings, $conf);
430
				break;
431
		}
432
	}
433

    
434
	// client specific settings
435

    
436
	if ($mode == 'client') {
437

    
438
		// configure p2p mode
439
		switch($settings['mode']) {
440
			case 'p2p_tls':
441
				$conf .= "tls-client\n";
442
			case 'shared_key':
443
				$conf .= "client\n";
444
				break;
445
		}
446

    
447
		// The port we'll listen at
448
		// If local_port is used, bing the management port
449
		if ($settings['local_port']) {
450
			$conf .= "lport {$settings['local_port']}\n";
451
			$conf .= "management 127.0.0.1 {$settings['local_port']}\n";
452
		}
453
		else
454
			$conf .= "nobind\n";
455

    
456
		// The remote server
457
		$conf .= "remote {$settings['server_addr']} {$settings['server_port']}\n";
458

    
459
		if (!empty($settings['use_shaper']))
460
			$conf .= "shaper {$settings['use_shaper']}\n";
461

    
462
		if (!empty($settings['tunnel_network'])) {
463
			list($ip, $mask) = explode('/', $settings['tunnel_network']);
464
			$mask = gen_subnet_mask($mask);
465
			$baselong = ip2long($ip) & ip2long($mask);
466
			$ip1 = long2ip($baselong + 1);
467
			$ip2 = long2ip($baselong + 2);
468
			$conf .= "ifconfig $ip2 $ip1\n";
469
		}
470

    
471
		if ($settings['proxy_addr']) {
472
			$conf .= "http-proxy {$settings['proxy_addr']} {$settings['proxy_port']}";
473
			if ($settings['proxy_authtype'] != "none") {
474
				$conf .= " {$g['varetc_path']}/openvpn/{$mode_id}.pas {$settings['proxy_authtype']}";
475
				$proxypas = "{$settings['proxy_user']}\n";
476
				$proxypas .= "{$settings['proxy_passwd']}\n";
477
				file_put_contents("{$g['varetc_path']}/openvpn/{$mode_id}.pas", $proxypas);
478
			}
479
			$conf .= " \n";
480
		}
481
	}
482

    
483
	// Add a remote network route if set
484
	if ($settings['remote_network']) {
485
		list($ip, $mask) = explode('/', $settings['remote_network']);
486
		$mask = gen_subnet_mask($mask);
487
		$conf .= "route $ip $mask\n";
488
	}
489

    
490
	// Write the settings for the keys
491
	switch($settings['mode']) {
492
		case 'p2p_shared_key':
493
			openvpn_add_keyfile($settings['shared_key'], $conf, $mode_id, "secret");
494
			break;
495
		case 'p2p_tls':
496
		case 'server_tls':
497
		case 'server_tls_user':
498
			$ca = lookup_ca($settings['caref']);
499
			openvpn_add_keyfile($ca['crt'], $conf, $mode_id, "ca");
500
		case 'server_user':
501
			$cert = lookup_cert($settings['certref']);
502
			openvpn_add_keyfile($cert['crt'], $conf, $mode_id, "cert");
503
			openvpn_add_keyfile($cert['prv'], $conf, $mode_id, "key");
504
			if ($mode == 'server')
505
				$conf .= "dh {$g['etc_path']}/dh-parameters.{$settings['dh_length']}\n";
506
			if ($settings['crl'])
507
				openvpn_add_keyfile($settings['crl'], $conf, $mode_id, "crl-verify");
508
			if ($settings['tls'])
509
				openvpn_add_keyfile($settings['tls'], $conf, $mode_id, "tls-auth", $settings['mode'] == "server_tls" ? "0" : "1");
510
			break;
511
	}
512

    
513
	if ($settings['compression'])
514
		$conf .= "comp-lzo\n";
515

    
516
	if ($settings['passtos'])
517
		$conf .= "passtos\n";
518

    
519
	if ($settings['resolve_retry'])
520
		$conf .= "resolv-retry infinite\n";
521

    
522
	if ($settings['dynamic_ip']) {
523
		$conf .= "persist-remote-ip\n";
524
		$conf .= "float\n";
525
	}
526

    
527
	openvpn_add_custom($settings, $conf);
528

    
529
	$fpath = $g['varetc_path']."/openvpn/{$mode_id}.conf";
530
	file_put_contents($fpath, $conf);
531
	chown($fpath, 'nobody');
532
	chgrp($fpath, 'nobody');
533
}
534

    
535
function openvpn_restart($mode, & $settings) {
536
	global $g, $config;
537

    
538
	$vpnid = $settings['vpnid'];
539
	$mode_id = $mode.$vpnid;
540

    
541
	/* kill the process if running */
542
	$pfile = $g['varrun_path']."/openvpn_{$mode_id}.pid";
543
	if (file_exists($pfile)) {
544

    
545
		/* read the pid file */
546
		$pid = rtrim(file_get_contents($pfile));
547
		unlink($pfile);
548

    
549
		/* send a term signal to the process */
550
		posix_kill($pid, SIGTERM);
551

    
552
		/* wait until the process exits */
553
		while(posix_kill($pid, 0))
554
			usleep(250000);
555
	}
556

    
557
	if ($settings['disable'])
558
		return;
559

    
560
	/* start the new process */
561
	$fpath = $g['varetc_path']."/openvpn/{$mode_id}.conf";
562
	mwexec_bg("nohup openvpn --config {$fpath}");
563
	touch("{$g['tmp_path']}/filter_dirty");
564
}
565

    
566
function openvpn_delete($mode, & $settings) {
567
	global $g, $config;
568

    
569
	$vpnid = $settings['vpnid'];
570
	$mode_id = $mode.$vpnid;
571

    
572
	$tunname = "tun{$vpnid}";
573
	if ($mode == "server")
574
		$devname = "ovpns{$vpnid}";
575
	else
576
		$devname = "ovpnc{$vpnid}";
577

    
578
	/* kill the process if running */
579
	$pfile = "{$g['varrun_path']}/openvpn_{$mode_id}.pid";
580
	if (file_exists($pfile)) {
581

    
582
		/* read the pid file */
583
		$pid = trim(file_get_contents($pfile));
584
		unlink($pfile);
585

    
586
		/* send a term signal to the process */
587
		posix_kill($pid, SIGTERM);
588
	}
589

    
590
	/* remove the device from the openvpn group */
591
	mwexec("/sbin/ifconfig {$devname} -group openvpn");
592

    
593
	/* restore the original adapter name */
594
	mwexec("/sbin/ifconfig {$devname} name {$tunname}");
595

    
596
	/* remove the configuration files */
597
	mwexec("/bin/rm {$g['varetc_path']}/openvpn/{$mode_id}.*");
598
}
599

    
600
function openvpn_resync_csc(& $settings) {
601
	global $g, $config;
602

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

    
605
	if ($settings['disable']) {
606
		unlink_if_exists($fpath);
607
		return;
608
	}
609

    
610
	$conf = '';
611
	if ($settings['block'])
612
		$conf .= "disable\n";
613

    
614
	if ($settings['push_reset'])
615
		$conf .= "push-reset\n";
616

    
617
	if (!empty($settings['tunnel_network'])) {
618
		list($ip, $mask) = explode('/', $settings['tunnel_network']);
619
		$baselong = ip2long($ip) & gen_subnet_mask_long($mask);
620
		$ip1 = long2ip($baselong + 1);
621
		$ip2 = long2ip($baselong + 2);
622
		$conf .= "ifconfig-push {$ip1} {$ip2}\n";
623
	}
624

    
625
	openvpn_add_dhcpopts($settings, $conf);
626

    
627
	if ($settings['gwredir'])
628
		$conf .= "push \"redirect-gateway def1\"\n";
629

    
630
	openvpn_add_custom($settings, $conf);
631

    
632
	file_put_contents($fpath, $conf);
633
	chown($fpath, 'nobody');
634
	chgrp($fpath, 'nobody');
635
}
636

    
637
function openvpn_delete_csc(& $settings) {
638
	global $g, $config;
639

    
640
	$fpath = $g['varetc_path']."/openvpn-csc/".$settings['common_name'];
641
	unlink_if_exists($fpath);
642
}
643

    
644
// Resync the configuration and restart the VPN
645
function openvpn_resync($mode, & $settings) {
646
	openvpn_reconfigure($mode, $settings);
647
	openvpn_restart($mode, $settings);
648
}
649

    
650
// Resync and restart all VPNs
651
function openvpn_resync_all() {
652
	global $g, $config;
653

    
654
	// delay our setup until the system
655
	// has a chance to init our paths
656
	if (!file_exists($g['varetc_path']."/openvpn") ||
657
		!file_exists($g['varetc_path']."/openvpn-csc"))
658
		return;
659

    
660
	if (!is_array($config['openvpn']))
661
		$config['openvpn'] = array();
662

    
663
/*
664
	if (!$config['openvpn']['dh-parameters']) {
665
		echo "Configuring OpenVPN Parameters ...\n";
666
		$dh_parameters = openvpn_create_dhparams(1024);
667
		$dh_parameters = base64_encode($dh_parameters);
668
		$config['openvpn']['dh-parameters'] = $dh_parameters;
669
		write_config("OpenVPN DH parameters");
670
	}
671

    
672
	$path_ovdh = $g['varetc_path']."/openvpn/dh-parameters";
673
	if (!file_exists($path_ovdh)) {
674
		$dh_parameters = $config['openvpn']['dh-parameters'];
675
		$dh_parameters = base64_decode($dh_parameters);
676
		file_put_contents($path_ovdh, $dh_parameters);
677
	}
678
*/
679

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

    
684
	if (is_array($config['openvpn']['openvpn-client']))
685
		foreach ($config['openvpn']['openvpn-client'] as & $settings)
686
			openvpn_resync('client', $settings);
687

    
688
	if (is_array($config['openvpn']['openvpn-csc']))
689
		foreach ($config['openvpn']['openvpn-csc'] as & $settings)
690
			openvpn_resync_csc($settings);
691

    
692
}
693

    
694
?>
(30-30/52)