Project

General

Profile

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

    
3
/*
4
	vpn.inc
5
	Copyright (C) 2004 Scott Ullrich
6
	Copyright (C) 2008 Shrew Soft Inc
7
	Copyright (C) 2008 Ermal Lu?i
8
	All rights reserved.
9

    
10
	originally part of m0n0wall (http://m0n0.ch/wall)
11
	Copyright (C) 2003-2004 Manuel Kasper <mk@neon1.net>.
12
	All rights reserved.
13

    
14
	Redistribution and use in source and binary forms, with or without
15
	modification, are permitted provided that the following conditions are met:
16

    
17
	1. Redistributions of source code must retain the above copyright notice,
18
	   this list of conditions and the following disclaimer.
19

    
20
	2. Redistributions in binary form must reproduce the above copyright
21
	   notice, this list of conditions and the following disclaimer in the
22
	   documentation and/or other materials provided with the distribution.
23

    
24
	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
25
	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
26
	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27
	AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
28
	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33
	POSSIBILITY OF SUCH DAMAGE.
34
*/
35

    
36
/*
37
	pfSense_BUILDER_BINARIES:	/usr/bin/killall	/usr/local/sbin/sasyncd	/sbin/ifconfig	/sbin/sysctl
38
	pfSense_BUILDER_BINARIES:	/usr/local/sbin/setkey	/usr/bin/netstat	/sbin/route	/bin/mkdir
39
	pfSense_BUILDER_BINARIES:	/usr/local/sbin/racoonctl	/usr/local/sbin/racoon
40
	pfSense_BUILDER_BINARIES:	/usr/local/sbin/filterdns	/usr/local/sbin/mpd4	
41
	pfSense_MODULE:	vpn
42
*/
43

    
44
/* include all configuration functions */
45

    
46
function vpn_ipsec_failover_configure() {
47
	global $config, $g;
48

    
49

    
50
	if (is_array($config['installedpackages']['sasyncd'])) {
51
		$sasyncd_text = "";
52
		foreach ($config['installedpackages']['sasyncd']['config'] as $sasyncd) {
53
			$enabled = isset ($sasyncd['enable']);
54
			if (!$enabled)
55
				return;
56
			if ($sasyncd['peerip'] <> "")
57
				$sasyncd_text .= "peer {$sasyncd['peerip']}\n";
58
			if ($sasyncd['interface'])
59
				$sasyncd_text .= "carp interface {$sasyncd['interface']}\n";
60
			if ($sasyncd['sharedkey'] <> "")
61
				$sasyncd_text .= "sharedkey {$sasyncd['sharedkey']}\n";
62
			if ($sasyncd['mode'] <> "")
63
				$sasyncd_text .= "mode {$sasyncd['mode']}\n";
64
			if ($sasyncd['listenon'] <> "")
65
				$sasyncd_text .= "listen on {$sasyncd['listenon']}\n";
66
			if ($sasyncd['flushmodesync'] <> "")
67
				$sasyncd_text .= "flushmode sync {$sasyncd['flushmodesync']}\n";
68
		}
69

    
70
		file_put_contents("{$g['varetc_path']}/sasyncd.conf", $sasyncd_text);
71
		chmod("{$g['varetc_path']}/sasyncd.conf", 0600);
72

    
73
		if(is_process_running("sasyncd"))
74
			mwexec("killall sasyncd", true);
75

    
76
		/* launch sasyncd, oh wise one */
77
		mwexec_bg("/usr/local/sbin/sasyncd -d -v -v -v");
78
	}
79
}
80

    
81
function vpn_ipsec_configure($ipchg = false)
82
{
83
	global $config, $g, $sa, $sn, $p1_ealgos, $p2_ealgos;
84

    
85
	/* get the automatic ping_hosts.sh ready */
86
	unlink_if_exists("{$g['vardb_path']}/ipsecpinghosts");
87
	touch("{$g['vardb_path']}/ipsecpinghosts");
88

    
89
	vpn_ipsec_configure_preferoldsa();
90

    
91
	$syscfg = $config['system'];
92
	$ipseccfg = $config['ipsec'];
93
	$a_phase1 = $config['ipsec']['phase1'];
94
	$a_phase2 = $config['ipsec']['phase2'];
95
	$a_client = $config['ipsec']['client'];
96

    
97
	if (!isset($ipseccfg['enable'])) {
98
		mwexec("/sbin/ifconfig enc0 down");
99

    
100
		/* send a SIGKILL to be sure */
101
		sigkillbypid("{$g['varrun_path']}/racoon.pid", "KILL");
102

    
103
		/* kill racoon */
104
		if(is_process_running("racoon"))
105
			mwexec("/usr/bin/killall racoon", true);
106
		killbypid("{$g['varrun_path']}/filterdns-ipsec.pid");
107

    
108
		/* wait for racoon process to die */
109
		sleep(2);
110

    
111
		/* flush SPD and SAD */
112
		mwexec("/usr/local/sbin/setkey -F");
113
		mwexec("/usr/local/sbin/setkey -FP");
114

    
115
		/* disallow IPSEC, it is off */
116
		exec("/sbin/sysctl net.inet.ip.ipsec_in_use=0");
117

    
118
		return true;
119
	} else {
120
		mwexec("/sbin/ifconfig enc0 up");
121
		mwexec("/sbin/sysctl net.inet.ip.ipsec_in_use=1");
122

    
123
		if ($g['booting'])
124
			echo "Configuring IPsec VPN... ";
125

    
126
		/* fastforwarding is not compatible with ipsec tunnels */
127
		mwexec("/sbin/sysctl net.inet.ip.fastforwarding=0");
128

    
129
		/* this loads a route table which is used to determine if a route needs to be removed. */
130
		exec("/usr/bin/netstat -rnf inet", $route_arr, $retval);
131
		$route_str = implode("\n", $route_arr);
132

    
133
		/* resolve all local, peer addresses and setup pings */
134
		$ipmap = array();
135
		$rgmap = array();
136
		$filterdns_list = array();
137
		if (is_array($a_phase1) && count($a_phase1)) {
138

    
139
			$ipsecpinghosts = "";
140
			/* step through each phase1 entry */
141
			foreach ($a_phase1 as $ph1ent) {
142
				if (isset($ph1ent['disabled']))
143
					continue;
144

    
145
				$ep = ipsec_get_phase1_src($ph1ent);
146
				if (!$ep)
147
					continue;
148

    
149
				if(!in_array($ep,$ipmap))
150
					$ipmap[] = $ep;
151

    
152
				/* see if this tunnel has a hostname for the remote-gateway. If so,
153
				   try to resolve it now and add it to the list for filterdns */
154

    
155
				if (isset ($ph1ent['mobile']))
156
					continue;
157

    
158
				$rg = $ph1ent['remote-gateway'];
159

    
160
				if (!is_ipaddr($rg)) {
161
					$filterdns_list[] = "{$rg}";
162
					add_hostname_to_watch($rg);
163
					if(! $g['booting'])
164
						$rg = resolve_retry($rg);
165
					if (!is_ipaddr($rg))
166
						continue;
167
				}
168
				if(array_search($rg, $rgmap)) {
169
					log_error("The remote gateway {$rg} already exists on another phase 1 entry");
170
					continue;
171
				}
172
				$rgmap[$ph1ent['remote-gateway']] = $rg;
173

    
174
				/* step through each phase2 entry */
175
				foreach ($a_phase2 as $ph2ent) {
176

    
177
					$ikeid = $ph2ent['ikeid'];
178

    
179
					if (isset($ph2ent['disabled']))
180
						continue;
181

    
182
					if ($ikeid != $ph1ent['ikeid'])
183
						continue;
184

    
185
					/* add an ipsec pinghosts entry */
186
					if ($ph2ent['pinghost']) {
187
						$iflist = get_configured_interface_list();
188
						foreach ($iflist as $ifent => $ifname) {
189
							$interface_ip = get_interface_ip($ifent);
190
							$local_subnet = ipsec_idinfo_to_cidr($ph2ent['localid'], true);
191
							if (ip_in_subnet($interface_ip, $local_subnet)) {
192
								$srcip = $interface_ip;
193
								break;
194
							}
195
						}
196
						$dstip = $ph2ent['pinghost'];
197
						if (is_ipaddr($srcip))
198
							$ipsecpinghosts .= "{$srcip}|{$dstip}|3\n";
199
					}
200
				}
201
				$pfd = fopen("{$g['vardb_path']}/ipsecpinghosts", "w");
202
				if ($pfd) {
203
					fwrite($pfd, $ipsecpinghosts);
204
					fclose($pfd);
205
				}
206
				
207
			}
208
		}
209

    
210
		/* generate CA certificates files */
211
		if (is_array($config['ca']) && count($config['ca'])) {
212
			foreach ($config['ca'] as $ca) {
213
				if (!isset($ca['crt'])) {
214
					log_error("Error: Invalid certificate info for {$ca['descr']}");
215
					continue;
216
				}
217
				$cert = base64_decode($ca['crt']);
218
				$x509cert = openssl_x509_parse(openssl_x509_read($cert));
219
				if (!is_array($x509cert) || !isset($x509cert['hash'])) {
220
					log_error("Error: Invalid certificate hash info for {$ca['descr']}");
221
					continue;
222
				}
223
				$fname = $g['varetc_path']."/".$x509cert['hash'].".0";
224
				if (!file_put_contents($fname, $cert)) {
225
					log_error("Error: Cannot write IPsec CA file for {$ca['descr']}");
226
					continue;
227
				}
228
			}
229
		}
230
		
231
		/* generate psk.txt */
232
		$fd = fopen("{$g['varetc_path']}/psk.txt", "w");
233
		if (!$fd) {
234
			printf("Error: cannot open psk.txt in vpn_ipsec_configure().\n");
235
			return 1;
236
		}
237

    
238
		$pskconf = "";
239

    
240
		if (is_array($a_phase1) && count($a_phase1)) {
241
			foreach ($a_phase1 as $ph1ent) {
242

    
243
				if (isset($ph1ent['disabled']))
244
					continue;
245

    
246
				if (strstr($ph1ent['authentication_method'],'rsa'))
247
					continue;
248

    
249
				$peerid_type = $ph1ent['peerid_type'];
250

    
251
				switch ($peerid_type) {
252
					case "peeraddress":
253
						$peerid_type = "address";
254
						$peerid_data = $rgmap[$ph1ent['remote-gateway']];
255
						break;
256

    
257
					case "address";
258
						$peerid_data = $ph1ent['peerid_data'];
259
						break;
260

    
261
					case "fqdn";
262
					case "keyid tag";
263
					case "user_fqdn";
264
						$peerid_data = $ph1ent['peerid_data'];
265
						break;
266
				}
267

    
268
				if (!empty($peerid_data) && !empty($ph1ent['pre-shared-key']))
269
					$pskconf .= trim($peerid_data) . "\t" . trim($ph1ent['pre-shared-key']) . "\n";
270
			}
271
		}
272

    
273
		/* Add user PSKs */
274
		foreach ($config['system']['user'] as $user) {
275
			if (!empty($user['ipsecpsk'])) {
276
				$pskconf .= "{$user['name']}\t{$user['ipsecpsk']}\n";
277
			}
278
		}
279

    
280
		/* add PSKs for mobile clients */
281
		if (is_array($ipseccfg['mobilekey'])) {
282
			foreach ($ipseccfg['mobilekey'] as $key) {
283
				$pskconf .= "{$key['ident']}\t{$key['pre-shared-key']}\n";
284
			}
285
		}
286

    
287
		fwrite($fd, $pskconf);
288
		fclose($fd);
289
		chmod("{$g['varetc_path']}/psk.txt", 0600);
290
			
291
		/* begin racoon.conf */
292
		if ((is_array($a_phase1) && count($a_phase1)) ||
293
			(is_array($a_phase2) && count($a_phase2))) {
294

    
295
			$fd = fopen("{$g['varetc_path']}/racoon.conf", "w");
296
			if (!$fd) {
297
				printf("Error: cannot open racoon.conf in vpn_ipsec_configure().\n");
298
				return 1;
299
			}
300

    
301
			$racoonconf = "# This file is automatically generated. Do not edit\n";			
302
			$racoonconf .= "path pre_shared_key \"{$g['varetc_path']}/psk.txt\";\n\n";
303
			$racoonconf .= "path certificate  \"{$g['varetc_path']}\";\n\n";
304

    
305
			/* begin listen section */
306
			if (count($ipmap)) {
307
				$racoonconf .= "\nlisten\n";
308
				$racoonconf .= "{\n";
309
				$racoonconf .= "	adminsock \"/var/db/racoon/racoon.sock\" \"root\" \"wheel\" 0660;\n";
310
				foreach ($ipmap as $addr) {
311
					$racoonconf .= "\tisakmp {$addr} [500];\n";
312
					$racoonconf .= "\tisakmp_natt {$addr} [4500];\n";
313
				}
314
				$racoonconf .= "}\n\n";
315
			}
316

    
317
			/* begin mode_cfg section */
318
			if (is_array($a_client) && isset($a_client['enable'])) {
319

    
320
				$racoonconf .= "\nmode_cfg\n";
321
				$racoonconf .= "{\n";
322

    
323
				if ($a_client['user_source'])
324
					$racoonconf .= "\tauth_source {$a_client['user_source']};\n";
325
				if ($a_client['group_source'])
326
					$racoonconf .= "\tgroup_source {$a_client['group_source']};\n";
327

    
328
				if ($a_client['pool_address'] && $a_client['pool_netbits']) {
329
					$pool_address = $a_client['pool_address'];
330
					$pool_netmask = gen_subnet_mask($a_client['pool_netbits']);
331

    
332
					$pool_address = long2ip32(ip2long($pool_address)+1);
333
					$pool_size = (~ip2long($pool_netmask) & 0xFFFFFFFF) - 2;
334

    
335
					$racoonconf .= "\tpool_size {$pool_size};\n";
336
					$racoonconf .= "\tnetwork4 {$pool_address};\n";
337
					$racoonconf .= "\tnetmask4 {$pool_netmask};\n";
338
				}
339

    
340
				if (isset($a_client['net_list'])) {
341

    
342
					$net_list = '';
343

    
344
					foreach ($a_phase2 as $ph2ent) {
345

    
346
						if (isset($ph2ent['disabled']))
347
							continue;
348

    
349
						if (!isset($ph2ent['mobile']))
350
							continue;
351

    
352
						$localid = ipsec_idinfo_to_cidr($ph2ent['localid'],true);
353

    
354
						if ($net_list)
355
							$net_list .= ", ";
356
						$net_list .= $localid;
357
					}
358

    
359
					if ($net_list)
360
						$racoonconf .= "\tsplit_network include {$net_list};\n";
361
				}
362

    
363
				if ($a_client['dns_server1'])
364
					$racoonconf .= "\tdns4 {$a_client['dns_server1']};\n";
365
				if ($a_client['dns_server2'])
366
					$racoonconf .= "\tdns4 {$a_client['dns_server2']};\n";
367
				if ($a_client['dns_server3'])
368
					$racoonconf .= "\tdns4 {$a_client['dns_server3']};\n";
369
				if ($a_client['dns_server4'])
370
					$racoonconf .= "\tdns4 {$a_client['dns_server4']};\n";
371

    
372
				if ($a_client['wins_server1'])
373
					$racoonconf .= "\twins4 {$a_client['wins_server1']};\n";
374
				if ($a_client['wins_server2'])
375
					$racoonconf .= "\twins4 {$a_client['wins_server2']};\n";
376

    
377
				if ($a_client['dns_domain']) {
378
					$racoonconf .= "\tdefault_domain \"{$a_client['dns_domain']}\";\n";
379
					$racoonconf .= "\tsplit_dns \"{$a_client['dns_domain']}\";\n";
380
				}
381

    
382
				if ($a_client['pfs_group'])
383
					$racoonconf .= "\tpfs_group {$a_client['pfs_group']};\n";
384

    
385
				if ($a_client['login_banner']) {
386
					$fn = "{$g['varetc_path']}/racoon.motd";
387
					$fd1 = fopen($fn, "w");
388
					if (!$fd1) {
389
						printf("Error: cannot open server{$fn} in vpn.\n");
390
						return 1;
391
					}
392

    
393
					fwrite($fd1, $a_client['login_banner']);
394
					fclose($fd1);
395

    
396
					$racoonconf .= "\tbanner \"{$fn}\";\n";
397
				}
398

    
399
				if (isset($a_client['save_passwd']))
400
					$racoonconf .= "\tsave_passwd on;\n";
401

    
402
				$racoonconf .= "}\n\n";
403
			}
404
			/* end mode_cfg section */
405

    
406
			/* begin remote sections */
407
			if (is_array($a_phase1) && count($a_phase1)) {
408
				/* begin remote */
409
				foreach ($a_phase1 as $ph1ent) {
410

    
411
					if (isset($ph1ent['disabled']))
412
						continue;
413

    
414
					if (isset($ph1ent['mobile']) && !isset($a_client['enable']))
415
						continue;
416

    
417
					$ikeid = $ph1ent['ikeid'];
418

    
419
					$ep = ipsec_get_phase1_src($ph1ent);
420
					if (!$ep)
421
						continue;
422

    
423
					if (!isset($ph1ent['mobile'])) {
424
						$rgip = $rgmap[$ph1ent['remote-gateway']];
425
						if (!$rgip)
426
							continue;
427
					}
428

    
429
					$myid_type = $ph1ent['myid_type'];
430

    
431
					switch ($myid_type) {
432

    
433
						case "myaddress":
434
							$myid_type = "address";
435
							$myid_data = $ep;
436
							break;
437

    
438
						case "dyn_dns":
439
							$myid_type = "address";
440
							$myid_data = gethostbyname($ph1ent['myid_data']);
441
							break;
442

    
443
						case "address";
444
							$myid_data = $ph1ent['myid_data'];
445
							break;
446

    
447
						case "fqdn";
448
						case "keyid tag";
449
						case "user_fqdn";
450
						case "asn1dn";
451
							$myid_data = $ph1ent['myid_data'];
452
							if( $myid_data )
453
								$myid_data = "\"".$myid_data."\"";
454
							break;
455
					}
456

    
457
					$peerid_type = $ph1ent['peerid_type'];
458

    
459
					switch ($peerid_type) {
460
						case "peeraddress":
461
							$peerid_type = "address";
462
							$peerid_data = $rgip;
463
							break;
464

    
465
						case "address";
466
							$peerid_data = $ph1ent['peerid_data'];
467
							break;
468

    
469
						case "fqdn";
470
						case "keyid tag";
471
						case "user_fqdn";
472
						case "asn1dn";
473
							$peerid_data = $ph1ent['peerid_data'];
474
							if( $peerid_data )
475
								$peerid_data = "\"".$peerid_data."\"";
476
							break;
477
					}
478

    
479
					$natt = "off";
480
					if (isset($ph1ent['nat_traversal']))
481
						$natt = $ph1ent['nat_traversal'];
482

    
483
					$init = "on";
484
					$genp = !empty($ph1ent['generate_policy']) ? $ph1ent['generate_policy'] : "off";
485
					$pcheck = !empty($ph1ent['proposal_check']) ? $ph1ent['proposal_check'] : $pcheck = "claim";
486
					$passive = "";
487
					if (isset($ph1ent['mobile'])) {
488
						$rgip = "anonymous";
489
						$passive = "passive on;";
490
						/* Mimic 1.2.3's behavior for pure-psk mobile tunnels */
491
						if ($ph1ent['authentication_method'] == "pre_shared_key") {
492
							$pcheck = !empty($ph1ent['proposal_check']) ? $ph1ent['proposal_check'] : $pcheck = "obey";
493
							$genp = !empty($ph1ent['generate_policy']) ? $ph1ent['generate_policy'] : "on";
494
						} else {
495
							$init = "off";
496
							$genp = !empty($ph1ent['generate_policy']) ? $ph1ent['generate_policy'] : "unique";
497
						}
498
					}
499

    
500
					$dpdline1 = '';
501
					$dpdline2 = '';
502
					if ($ph1ent['dpd_delay'] && $ph1ent['dpd_maxfail']) {
503
						$dpdline1 = "dpd_delay = {$ph1ent['dpd_delay']};";
504
						$dpdline2 = "dpd_maxfail = {$ph1ent['dpd_maxfail']};";
505
					}
506

    
507
					if (isset ($ph1ent['authentication_method']))
508
						$authmethod = $ph1ent['authentication_method'];
509
					else
510
						$authmethod = 'pre_shared_key';
511

    
512
					$certline = '';
513

    
514
					if (strstr($authmethod,'rsa')) {
515

    
516
						$cert = lookup_cert($ph1ent['certref']);
517

    
518
						if (!$cert)
519
						{
520
							log_error("Error: Invalid phase1 certificate reference for {$ph1ent['name']}");
521
							continue;
522
						}
523

    
524
						$certfile = "cert-".$ikeid.".crt";
525
						$certpath = $g['varetc_path']."/".$certfile;
526

    
527
						if (!file_put_contents($certpath, base64_decode($cert['crt'])))
528
						{
529
							log_error("Error: Cannot write phase1 certificate file for {$ph1ent['name']}");
530
							continue;
531
						}
532

    
533
						chmod($certpath, 0600);
534

    
535
						$keyfile = "cert-".$ikeid.".key";
536
						$keypath = $g['varetc_path']."/".$keyfile;
537

    
538
						if (!file_put_contents($keypath, base64_decode($cert['prv'])))
539
						{
540
							log_error("Error: Cannot write phase1 key file for {$ph1ent['name']}");
541
							continue;
542
						}
543

    
544
						chmod($keypath, 0600);
545

    
546
						$ca = lookup_ca($ph1ent['caref']);
547
						if ($ca) {
548
							$cafile = "ca-".$ikeid.".crt";
549
							$capath = $g['varetc_path']."/".$cafile;
550

    
551
							if (!file_put_contents($capath, base64_decode($ca['crt'])))
552
							{
553
								log_error("Error: Cannot write phase1 CA certificate file for {$ph1ent['name']}");
554
								continue;
555
							}
556

    
557
							chmod($capath, 0600);
558
							$caline = "ca_type x509 \"".basename($capath)."\";";
559
						}
560

    
561
						$certline = "certificate_type x509 \"".basename($certpath)."\" \"".basename($keypath)."\";";
562

    
563
					}
564

    
565
					$ealgos = '';
566
					$ealg_id = $ph1ent['encryption-algorithm']['name'];
567
					$ealg_kl = $ph1ent['encryption-algorithm']['keylen'];
568
					if ($ealg_kl)
569
						$ealgos = $ealgos.$ealg_id." ".$ealg_kl;
570
					else
571
						$ealgos = $ealgos.$ealg_id;
572

    
573
					$lifeline = '';
574
					if ($ph1ent['lifetime'])
575
						$lifeline = "lifetime time {$ph1ent['lifetime']} secs;";
576

    
577
					/* Only specify peer ID if we are not dealing with a mobile PSK-only tunnel */
578
					if (!(($ph1ent['authentication_method'] == "pre_shared_key") && isset($ph1ent['mobile']))) {
579
						$peerid_spec = "peers_identifier {$peerid_type} {$peerid_data};";
580
					}
581

    
582
					/* add remote section to configuration */
583

    
584
					$racoonconf .=<<<EOD
585

    
586
remote {$rgip}
587
{
588
	ph1id {$ikeid};
589
	exchange_mode {$ph1ent['mode']};
590
	my_identifier {$myid_type} {$myid_data};
591
	{$peerid_spec}
592
	ike_frag on;
593
	generate_policy = {$genp};
594
	initial_contact = {$init};
595
	nat_traversal = {$natt};
596
	{$certline}
597
	{$caline}
598
	{$dpdline1}
599
	{$dpdline2}
600
	support_proxy on;
601
	proposal_check {$pcheck};
602
	{$passive}
603

    
604
	proposal
605
	{
606
		authentication_method {$authmethod};
607
		encryption_algorithm ${ealgos};
608
		hash_algorithm {$ph1ent['hash-algorithm']};
609
		dh_group {$ph1ent['dhgroup']};
610
		${lifeline}
611
	}
612
}
613

    
614
EOD;
615
				}
616
				/* end remote */
617
			}
618
			/* end remote sections */
619
		
620
			/* begin sainfo sections */
621
			if (is_array($a_phase2) && count($a_phase2)) {
622

    
623
				/* begin sainfo */
624
				foreach ($a_phase2 as $ph2ent) {
625

    
626
					$ikeid = $ph2ent['ikeid'];
627

    
628
					if( !ipsec_lookup_phase1($ph2ent,$ph1ent))
629
						continue;
630

    
631
					if (isset($ph1ent['disabled']))
632
						continue;
633

    
634
					if (isset($ph2ent['disabled']))
635
						continue;
636

    
637
					if (isset($ph2ent['mobile']) && !isset($a_client['enable']))
638
						continue;
639

    
640
					if ($ph2ent['mode'] == 'tunnel') {
641

    
642
						$localid_type = $ph2ent['localid']['type'];
643
						$localid_data = ipsec_idinfo_to_cidr($ph2ent['localid']);
644
						/* Do not print localid in some cases, such as a pure-psk or psk/xauth single phase2 mobile tunnel */
645
						if (($localid_type == "none") ||
646
							(($ph1ent['authentication_method'] == "xauth_psk_server") ||
647
							($ph1ent['authentication_method'] == "pre_shared_key"))
648
							&& isset($ph1ent['mobile'])
649
							&& (ipsec_get_number_of_phase2($ikeid)==1))
650
							$localid_spec = " ";
651
						else {
652
							if ($localid_type != "address") {
653
								$localid_type = "subnet";
654
							}
655
							$localid_spec = $localid_type." ".$localid_data." any";
656
						}
657

    
658
						if (!isset($ph2ent['mobile'])) {
659
							$remoteid_type = $ph2ent['remoteid']['type'];
660
							if ($remoteid_type != "address")
661
								$remoteid_type = "subnet";
662

    
663
							$remoteid_data = ipsec_idinfo_to_cidr($ph2ent['remoteid']);
664
							$remoteid_spec = $remoteid_type." ".$remoteid_data." any";
665
						} else
666
							$remoteid_spec = "anonymous";
667

    
668
					} else {
669
						$rgip = $rgmap[$ph1ent['remote-gateway']];
670

    
671
						if ((($ph1ent['authentication_method'] == "xauth_psk_server") ||
672
							($ph1ent['authentication_method'] == "pre_shared_key"))
673
							&& isset($ph1ent['mobile']))
674
							$localid_spec = " ";
675
						else {
676
							$localid_data = ipsec_get_phase1_src($ph1ent);
677
							if($ph2ent['mode'] == 'transport') { $localid_data="$localid_data any"; }
678
							$localid_spec = "address {$localid_data}";
679
						}
680
						if (!isset($ph2ent['mobile'])) {
681
							$remoteid_data = $rgmap[$ph1ent['remote-gateway']];
682
							if($ph2ent['mode'] == 'transport') { $remoteid_data="$remoteid_data any"; }
683
							$remoteid_spec = "address {$remoteid_data}";
684
						} else
685
							$remoteid_spec = "anonymous";
686
					}
687

    
688
					if($ph2ent['protocol'] == 'esp') {
689

    
690
						$ealgos = '';
691

    
692
						foreach ($ph2ent['encryption-algorithm-option'] as $ealg) {
693

    
694
							$ealg_id = $ealg['name'];
695
							$ealg_kl = $ealg['keylen'];
696

    
697
							if ($ealg_kl) {
698
								if( $ealg_kl == "auto" ) {
699
									/*   This seems to be required on my system and was not reproducable
700
									 *   on other systems.   For some reason $p2_ealgos is not defined
701
									 *   and needs to be read back in!?  -sullrich Aug 26, 2009 
702
									 */
703
									if(!$p2_ealgos)
704
										require("ipsec.inc");
705
									$key_hi = $p2_ealgos[$ealg_id]['keysel']['hi'];
706
									$key_lo = $p2_ealgos[$ealg_id]['keysel']['lo'];
707
									$key_step = $p2_ealgos[$ealg_id]['keysel']['step'];
708
									/* in some cases where include ordering is suspect these variables
709
									   are somehow 0 and we enter this loop forever and timeout after 900
710
									   seconds wrecking bootup */
711
									if($key_hi != 0 and $key_lo !=0 and $key_step !=0) {
712
										for ($keylen = $key_hi; $keylen >= $key_lo; $keylen -= $key_step) {
713
//											Uncomment the next line if you want to test the comment 5 lines up.											
714
//											echo "$keylen = $key_hi; $keylen >= $key_lo; $keylen -= $key_step \n";
715
											if ($ealgos)
716
												$ealgos = $ealgos.", ";
717
											$ealgos = $ealgos.$ealg_id." ".$keylen;
718
										}
719
									}
720
								} else {
721
									if ($ealgos)
722
										$ealgos = $ealgos.", ";
723
									$ealgos = $ealgos.$ealg_id." ".$ealg_kl;
724
								}
725
							} else {
726
								if ($ealgos)
727
									$ealgos = $ealgos.", ";
728
								$ealgos = $ealgos.$ealg_id;
729
							}
730
						}
731

    
732
						$ealgosline = "encryption_algorithm {$ealgos};";
733

    
734
					} else {
735

    
736
						$ealgosline = "encryption_algorithm null_enc;";
737
					}
738

    
739
					$halgos = join(",", $ph2ent['hash-algorithm-option']);
740
					$halgosline = "authentication_algorithm {$halgos};";
741

    
742
					$pfsline = '';
743
					if ($ph2ent['pfsgroup'])
744
						$pfsline = "pfs_group {$ph2ent['pfsgroup']};";
745
					if (isset($a_client['pfs_group'])) {
746
						$pfsline = '';
747
						if ($a_client['pfs_group'])
748
							$pfsline = "pfs_group {$a_client['pfs_group']};";
749
					}
750

    
751
					$lifeline = '';
752
					if ($ph2ent['lifetime'])
753
						$lifeline = "lifetime time {$ph2ent['lifetime']} secs;";
754

    
755
					/* add sainfo section to configuration */
756
					
757
					$racoonconf .=<<<EOD
758
					
759
sainfo {$localid_spec} {$remoteid_spec}
760
{
761
	remoteid {$ikeid};
762
	{$ealgosline}
763
	{$halgosline}
764
	{$pfsline}
765
	{$lifeline}
766
	compression_algorithm deflate;
767
}
768

    
769
EOD;
770
				}
771
				/* end sainfo */
772
			}
773
			/* end sainfo sections */
774

    
775
			fwrite($fd, $racoonconf);
776
			fclose($fd);
777
		}
778
		/* end racoon.conf */
779

    
780
		/* generate IPsec policies */
781
		if (is_array($a_phase2) && count($a_phase2)) {
782
			/* generate spd.conf */
783
			$fd = fopen("{$g['varetc_path']}/spd.conf", "w");
784
			if (!$fd) {
785
				printf("Error: cannot open spd.conf in vpn_ipsec_configure().\n");
786
				return 1;
787
			}
788

    
789
			$spdconf = "";
790

    
791
			/* Try to prevent people from locking themselves out of webgui. Just in case. */
792
			if ($config['interfaces']['lan']) {
793
				$lanip = get_interface_ip("lan");
794
				if (!empty($lanip) && is_ipaddr($lanip)) {
795
					$lansn = get_interface_subnet("lan");
796
					$lansa = gen_subnet($lanip, $lansn);
797
					$spdconf .= "spdadd {$lanip}/32 {$lansa}/{$lansn} any -P out none;\n";
798
					$spdconf .= "spdadd {$lansa}/{$lansn} {$lanip}/32 any -P in none;\n";
799
				}
800
			}
801

    
802
			foreach ($a_phase2 as $ph2ent) {
803

    
804
				if( !ipsec_lookup_phase1($ph2ent,$ph1ent))
805
					continue;
806

    
807
				if (isset($ph1ent['mobile']))
808
					continue;
809

    
810
				if (isset($ph1ent['disabled']))
811
					continue;
812

    
813
				if (isset($ph2ent['disabled']))
814
					continue;
815

    
816
				$ep = ipsec_get_phase1_src($ph1ent);
817
				if (!$ep)
818
					continue;
819

    
820
				$rgip = $rgmap[$ph1ent['remote-gateway']];
821
				if(!is_ipaddr($rgip))
822
					continue;
823

    
824
				$localid = ipsec_idinfo_to_cidr($ph2ent['localid'],true);
825
				$remoteid = ipsec_idinfo_to_cidr($ph2ent['remoteid'],true);
826

    
827
				if($ph2ent['mode'] == "tunnel") {
828

    
829
					$spdconf .= "spdadd {$localid} {$remoteid} any -P out ipsec " .
830
						"{$ph2ent['protocol']}/tunnel/{$ep}-{$rgip}/unique;\n";
831

    
832
					$spdconf .= "spdadd {$remoteid} {$localid} any -P in ipsec " .
833
						"{$ph2ent['protocol']}/tunnel/{$rgip}-{$ep}/unique;\n";
834

    
835
				} else {
836

    
837
					$localid_data = ipsec_get_phase1_src($ph1ent);
838
					$remoteid_data = $rgmap[$ph1ent['remote-gateway']];
839

    
840
					$spdconf .= "spdadd {$localid_data} {$remoteid_data} any -P out ipsec " .
841
						"{$ph2ent['protocol']}/transport//require;\n";
842

    
843
					$spdconf .= "spdadd {$remoteid_data} {$localid_data} any -P in ipsec " .
844
						"{$ph2ent['protocol']}/transport//require;\n";
845

    
846
				}
847

    
848
				/* static route needed? */
849
				if (preg_match("/^carp|^vip/i", $ph1ent['interface']))
850
					$parentinterface = link_carp_interface_to_parent($ph1ent['interface']);
851
				else
852
					$parentinterface = $ph1ent['interface'];
853

    
854
				if (($parentinterface <> "wan") && (is_ipaddr($rgip))) {
855
					/* add endpoint routes to correct gateway on interface */
856
					if (interface_has_gateway($parentinterface)) {
857
						$gatewayip = get_interface_gateway("$parentinterface");
858
						$interfaceip = get_interface_ip($parentinterface);
859
						$subnet_bits = get_interface_subnet($parentinterface);
860
						$subnet_ip = gen_subnet("{$interfaceip}", "{$subnet_bits}");
861
						/* if the remote gateway is in the local subnet, then don't add a route */
862
						if (! ip_in_subnet($rgip, "{$subnet_ip}/{$subnet_bits}")) {
863
							if(is_ipaddr($gatewayip)) {
864
								/* FIXME: does adding route-to and reply-to on the in/outbound
865
								 * rules fix this? smos@ 13-01-2009 */
866
								// log_error("IPSEC interface is not WAN but {$parentinterface}, adding static route for VPN endpoint {$rgip} via {$gatewayip}");
867
								mwexec("/sbin/route change -host {$rgip} {$gatewayip}", true);
868
							}
869
						}
870
					}
871
				} elseif(is_ipaddr($rgip)) {
872
					if(stristr($route_str, "{$rgip}")) {
873
						mwexec("/sbin/route delete -host {$rgip}", true);
874
					}
875
				}
876
			}
877

    
878
			fwrite($fd, $spdconf);
879
			fclose($fd);
880
		}
881

    
882
		/* needed for racoonctl admin socket */
883
		if (!is_dir("/var/db/racoon"))
884
			mkdir("/var/db/racoon/");
885
		
886
		/* mange racoon process */
887
		if (is_process_running("racoon")) {
888
			sleep("0.1");
889
			mwexec("/usr/local/sbin/racoonctl -s /var/db/racoon/racoon.sock reload-config", false);
890
			/* load SPD without flushing to be safe on config additions or changes. */
891
			mwexec("/usr/local/sbin/setkey -f {$g['varetc_path']}/spd.conf", false);
892
		} else {
893
			/* flush SA + SPD entries */
894
			mwexec("/usr/local/sbin/setkey -FP", false);
895
 			sleep("0.1");
896
			mwexec("/usr/local/sbin/setkey -F", false);
897
 			sleep("0.1");
898
 			/* start racoon */
899
			$ipsecdebug = isset($config['ipsec']['racoondebug']) ? "-d -v" : "";
900
			mwexec("/usr/local/sbin/racoon {$ipsecdebug} -f {$g['varetc_path']}/racoon.conf", false);
901
 			sleep("0.1");
902
 			/* load SPD */
903
			mwexec("/usr/local/sbin/setkey -f {$g['varetc_path']}/spd.conf", false);
904

    
905
		}
906
		/* start filterdns, if necessary */
907
		if (count($filterdns_list) > 0) {
908
			$interval = 60;
909
			if (!empty($ipseccfg['dns-interval']) && is_numeric($ipseccfg['dns-interval']))
910
				$interval = $ipseccfg['dns-interval'];
911

    
912
			$hostnames = "";
913
			array_unique($filterdns_list);
914
			foreach ($filterdns_list as $hostname)
915
				$hostnames .= "cmd {$hostname} '/usr/local/sbin/pfSctl -c \"service reload ipsecdns\"'\n";
916
			file_put_contents("{$g['varetc_path']}/filterdns-ipsec.hosts", $hostnames);
917

    
918
			killbypid("{$g['varrun_path']}/filterdns-ipsec.pid");
919
			sleep(1);
920
			mwexec("/usr/local/sbin/filterdns -p {$g['varrun_path']}/filterdns-ipsec.pid -i {$interval} -c {$g['varetc_path']}/filterdns-ipsec.hosts -d 1");
921
		}
922
	
923
		vpn_ipsec_failover_configure();
924

    
925
		if ($g['booting'])
926
			echo "done\n";
927
	}
928

    
929
	return 0;
930
}
931

    
932
/* Forcefully restart IPsec
933
 * This is required for when dynamic interfaces reload
934
 * For all other occasions the normal vpn_ipsec_configure()
935
 * will gracefully reload the settings without restarting
936
 */
937
function vpn_ipsec_force_reload() {
938
	global $config;
939
	global $g;
940

    
941
	$ipseccfg = $config['ipsec'];
942

    
943
	/* kill racoon */
944
	if(is_process_running("racoon"))
945
		mwexec("/usr/bin/killall racoon", true);
946

    
947
	/* wait for process to die */
948
	sleep(4);
949

    
950
	/* send a SIGKILL to be sure */
951
	sigkillbypid("{$g['varrun_path']}/racoon.pid", "KILL");
952

    
953
	/* wait for flushing to finish */
954
	sleep(1);
955

    
956
	/* if ipsec is enabled, start up again */
957
	if (isset($ipseccfg['enable'])) {
958
		log_error("Forcefully reloading IPsec racoon daemon");
959
		vpn_ipsec_configure();
960
	}
961

    
962
}
963

    
964
/* master setup for vpn (mpd) */
965
function vpn_setup() {
966
	/* start pptpd */
967
	vpn_pptpd_configure();
968

    
969
	/* start pppoe server */
970
	vpn_pppoes_configure();
971

    
972
	/* setup l2tp */
973
	vpn_l2tp_configure();
974
}
975

    
976
function vpn_netgraph_support() {
977
	$iflist = get_configured_interface_list();
978
	foreach ($iflist as $iface) {
979
		$realif = get_real_interface($iface);
980
		/* Get support for netgraph(4) from the nic */
981
		$ifinfo = pfSense_get_interface_addresses($realif);
982
		if (!empty($ifinfo) && in_array($ifinfo['iftype'], array("ether", "vlan", "bridge")))
983
                	pfSense_ngctl_attach(".", $realif);
984
	}
985
}
986

    
987
function vpn_pptpd_configure() {
988
	global $config, $g;
989

    
990
	$syscfg = $config['system'];
991
	$pptpdcfg = $config['pptpd'];
992

    
993
	if ($g['booting']) {
994
		if (!$pptpdcfg['mode'] || ($pptpdcfg['mode'] == "off"))
995
			return 0;
996

    
997
		echo "Configuring PPTP VPN service... ";
998
	} else {
999
		/* kill mpd */
1000
		killbypid("{$g['varrun_path']}/pptp-vpn.pid");
1001

    
1002
		/* wait for process to die */
1003
		sleep(3);
1004

    
1005
		if (is_process_running("mpd -b")) {
1006
			killbypid("{$g['varrun_path']}/pptp-vpn.pid");
1007
			log_error("Could not kill mpd within 3 seconds.   Trying again.");
1008
		}
1009

    
1010
		/* remove mpd.conf, if it exists */
1011
		unlink_if_exists("{$g['varetc_path']}/pptp-vpn/mpd.conf");
1012
		unlink_if_exists("{$g['varetc_path']}/pptp-vpn/mpd.links");
1013
		unlink_if_exists("{$g['varetc_path']}/pptp-vpn/mpd.secret");
1014
	}
1015

    
1016
	/* make sure pptp-vpn directory exists */
1017
	if (!file_exists("{$g['varetc_path']}/pptp-vpn"))
1018
		mkdir("{$g['varetc_path']}/pptp-vpn");
1019

    
1020
	switch ($pptpdcfg['mode']) {
1021
		case 'server' :
1022
			/* write mpd.conf */
1023
			$fd = fopen("{$g['varetc_path']}/pptp-vpn/mpd.conf", "w");
1024
			if (!$fd) {
1025
				printf("Error: cannot open mpd.conf in vpn_pptpd_configure().\n");
1026
				return 1;
1027
			}
1028

    
1029
			$mpdconf = <<<EOD
1030
pptps:
1031

    
1032
EOD;
1033

    
1034
			for ($i = 0; $i < $pptpdcfg['n_pptp_units']; $i++) {
1035
				$mpdconf .= "	load pt{$i}\n";
1036
			}
1037

    
1038
			for ($i = 0; $i < $pptpdcfg['n_pptp_units']; $i++) {
1039

    
1040
				$clientip = long2ip32(ip2long($pptpdcfg['remoteip']) + $i);
1041

    
1042
				$mpdconf .= <<<EOD
1043

    
1044
pt{$i}:
1045
	new -i pptpd{$i} pt{$i} pt{$i}
1046
	set ipcp ranges {$pptpdcfg['localip']}/32 {$clientip}/32
1047
	load pts
1048

    
1049
EOD;
1050
			}
1051

    
1052
			$mpdconf .=<<<EOD
1053

    
1054
pts:
1055
	set iface disable on-demand
1056
	set iface enable proxy-arp
1057
	set iface enable tcpmssfix
1058
	set iface idle 1800
1059
	set iface up-script /usr/local/sbin/vpn-linkup
1060
	set iface down-script /usr/local/sbin/vpn-linkdown
1061
	set bundle enable multilink
1062
	set bundle enable crypt-reqd
1063
	set link yes acfcomp protocomp
1064
	set link no pap chap
1065
	set link enable chap-msv2
1066
	set link mtu 1460
1067
	set link keep-alive 10 60
1068
	set ipcp yes vjcomp
1069
	set bundle enable compression
1070
	set ccp yes mppc
1071
	set ccp yes mpp-e128
1072
	set ccp yes mpp-stateless
1073

    
1074
EOD;
1075

    
1076
			if (!isset ($pptpdcfg['req128'])) {
1077
				$mpdconf .=<<<EOD
1078
	set ccp yes mpp-e40
1079
	set ccp yes mpp-e56
1080

    
1081
EOD;
1082
			}
1083

    
1084
			if  (isset($pptpdcfg["wins"]) && $pptpdcfg['wins'] != "")
1085
				$mpdconf  .=  "	set ipcp nbns {$pptpdcfg['wins']}\n";
1086

    
1087
			if (!empty($pptpdcfg['dns1'])) {
1088
				$mpdconf .= "	set ipcp dns " . $pptpdcfg['dns1'];
1089
				if (!empty($pptpdcfg['dns2']))
1090
					$mpdconf .= " " . $pptpdcfg['dns2'];
1091
				$mpdconf .= "\n";
1092
			} elseif (isset ($config['dnsmasq']['enable'])) {
1093
				$mpdconf .= "	set ipcp dns " . get_interface_ip("lan");
1094
				if ($syscfg['dnsserver'][0])
1095
					$mpdconf .= " " . $syscfg['dnsserver'][0];
1096
				$mpdconf .= "\n";
1097
			} elseif (is_array($syscfg['dnsserver']) && ($syscfg['dnsserver'][0])) {
1098
					$mpdconf .= "	set ipcp dns " . join(" ", $syscfg['dnsserver']) . "\n";
1099
			}
1100

    
1101
			if (isset ($pptpdcfg['radius']['server']['enable'])) {
1102
				$authport = (isset($pptpdcfg['radius']['server']['port']) && strlen($pptpdcfg['radius']['server']['port']) > 1) ? $pptpdcfg['radius']['server']['port'] : 1812;
1103
				$acctport = $authport + 1;
1104
				$mpdconf .=<<<EOD
1105
	set radius server {$pptpdcfg['radius']['server']['ip']} "{$pptpdcfg['radius']['server']['secret']}" {$authport} {$acctport}
1106

    
1107
EOD;
1108
			if (isset ($pptpdcfg['radius']['server2']['enable'])) {
1109
				$authport = (isset($pptpdcfg['radius']['server2']['port']) && strlen($pptpdcfg['radius']['server2']['port']) > 1) ? $pptpdcfg['radius']['server2']['port'] : 1812;
1110
				$acctport = $authport + 1;
1111
				$mpdconf .=<<<EOD
1112
	set radius server {$pptpdcfg['radius']['server2']['ip']} "{$pptpdcfg['radius']['server2']['secret']}" {$authport} {$acctport}
1113

    
1114
EOD;
1115
			}
1116
			$mpdconf .=<<<EOD
1117
	set radius retries 3
1118
	set radius timeout 10
1119
	set auth enable radius-auth
1120

    
1121
EOD;
1122

    
1123
				if (isset ($pptpdcfg['radius']['accounting'])) {
1124
					$mpdconf .=<<<EOD
1125
	set auth enable radius-acct
1126
	set radius acct-update 300
1127

    
1128
EOD;
1129
				}
1130
			}
1131

    
1132
			fwrite($fd, $mpdconf);
1133
			fclose($fd);
1134

    
1135
			/* write mpd.links */
1136
			$fd = fopen("{$g['varetc_path']}/pptp-vpn/mpd.links", "w");
1137
			if (!$fd) {
1138
				printf("Error: cannot open mpd.links in vpn_pptpd_configure().\n");
1139
				return 1;
1140
			}
1141

    
1142
			$mpdlinks = "";
1143

    
1144
			for ($i = 0; $i < $pptpdcfg['n_pptp_units']; $i++) {
1145
				$mpdlinks .=<<<EOD
1146

    
1147
pt{$i}:
1148
	set link type pptp
1149
	set pptp enable incoming
1150
	set pptp disable originate
1151
	set pptp disable windowing
1152

    
1153
EOD;
1154
			}
1155

    
1156
			fwrite($fd, $mpdlinks);
1157
			fclose($fd);
1158

    
1159
			/* write mpd.secret */
1160
			$fd = fopen("{$g['varetc_path']}/pptp-vpn/mpd.secret", "w");
1161
			if (!$fd) {
1162
				printf("Error: cannot open mpd.secret in vpn_pptpd_configure().\n");
1163
				return 1;
1164
			}
1165

    
1166
			$mpdsecret = "";
1167

    
1168
			if (is_array($pptpdcfg['user'])) {
1169
				foreach ($pptpdcfg['user'] as $user)
1170
					$mpdsecret .= "{$user['name']} \"{$user['password']}\" {$user['ip']}\n";
1171
			}
1172

    
1173
			fwrite($fd, $mpdsecret);
1174
			fclose($fd);
1175
			chmod("{$g['varetc_path']}/pptp-vpn/mpd.secret", 0600);
1176

    
1177
			vpn_netgraph_support();
1178

    
1179
			/* fire up mpd */
1180
			mwexec("/usr/local/sbin/mpd4 -b -d {$g['varetc_path']}/pptp-vpn -p {$g['varrun_path']}/pptp-vpn.pid -s pptps pptps");
1181

    
1182
			break;
1183

    
1184
		case 'redir' :
1185
			break;
1186
	}
1187

    
1188
	if ($g['booting'])
1189
		echo "done\n";
1190

    
1191
	return 0;
1192
}
1193

    
1194
function vpn_pppoes_configure() {
1195
	global $config;
1196

    
1197
	if (is_array($config['pppoes']['pppoe'])) {
1198
		foreach ($config['pppoes']['pppoe'] as $pppoe)
1199
			vpn_pppoe_configure($pppoe);
1200
	}
1201
}
1202

    
1203
function vpn_pppoe_configure(&$pppoecfg) {
1204
	global $config, $g;
1205

    
1206
	$syscfg = $config['system'];
1207

    
1208
	/* create directory if it does not exist */
1209
	if (!is_dir("{$g['varetc_path']}/pppoe{$pppoecfg['pppoeid']}-vpn"))
1210
		mkdir("{$g['varetc_path']}/pppoe{$pppoecfg['pppoeid']}-vpn");
1211

    
1212
	if ($g['booting']) {
1213
		if (!$pppoecfg['mode'] || ($pppoecfg['mode'] == "off"))
1214
			return 0;
1215

    
1216
		echo "Configuring PPPoE VPN service... ";
1217
	} else {
1218
		/* kill mpd */
1219
		killbypid("{$g['varrun_path']}/pppoe{$pppoecfg['pppoeid']}-vpn.pid");
1220

    
1221
		/* wait for process to die */
1222
		sleep(2);
1223

    
1224
	}
1225

    
1226
	switch ($pppoecfg['mode']) {
1227

    
1228
		case 'server' :
1229

    
1230
			$pppoe_interface = get_real_interface($pppoecfg['interface']);
1231

    
1232
			if ($pppoecfg['paporchap'] == "chap")
1233
				$paporchap = "set link enable chap";
1234
			else
1235
				$paporchap = "set link enable pap";
1236

    
1237
			/* write mpd.conf */
1238
			$fd = fopen("{$g['varetc_path']}/pppoe{$pppoecfg['pppoeid']}-vpn/mpd.conf", "w");
1239
			if (!$fd) {
1240
				printf("Error: cannot open mpd.conf in vpn_pppoe_configure().\n");
1241
				return 1;
1242
			}
1243
			$mpdconf = "\n\n";
1244
			$mpdconf .= "poes:\n";
1245

    
1246
			for ($i = 0; $i < $pppoecfg['n_pppoe_units']; $i++) {
1247
				$mpdconf .= "	load poes{$pppoecfg['pppoeid']}{$i}\n";
1248
			}
1249

    
1250
			for ($i = 0; $i < $pppoecfg['n_pppoe_units']; $i++) {
1251

    
1252
				$clientip = long2ip32(ip2long($pppoecfg['remoteip']) + $i);
1253

    
1254
				if (isset ($pppoecfg['radius']['radiusissueips']) && isset ($pppoecfg['radius']['server']['enable'])) {
1255
					$isssue_ip_type = "set ipcp ranges {$pppoecfg['localip']}/32 0.0.0.0/0";
1256
				} else {
1257
					$isssue_ip_type = "set ipcp ranges {$pppoecfg['localip']}/32 {$clientip}/32";
1258
				}
1259

    
1260
				$mpdconf .=<<<EOD
1261

    
1262
poes{$pppoecfg['pppoeid']}{$i}:
1263
	new -i poes{$pppoecfg['pppoeid']}{$i} poes{$pppoecfg['pppoeid']}{$i} poes{$pppoecfg['pppoeid']}{$i}
1264
	{$isssue_ip_type}
1265
	load pppoe_standard
1266

    
1267
EOD;
1268
			}
1269

    
1270
			$mpdconf .=<<<EOD
1271

    
1272
pppoe_standard:
1273
	set bundle no multilink
1274
	set bundle enable compression
1275
	set auth max-logins 1
1276
	set iface up-script /usr/local/sbin/vpn-linkup
1277
	set iface down-script /usr/local/sbin/vpn-linkdown
1278
	set iface idle 0
1279
	set iface disable on-demand
1280
	set iface disable proxy-arp
1281
	set iface enable tcpmssfix
1282
	set iface mtu 1500
1283
	set link no pap chap
1284
	{$paporchap}
1285
	set link keep-alive 60 180
1286
	set ipcp yes vjcomp
1287
	set ipcp no vjcomp
1288
	set link max-redial -1
1289
	set link mtu 1492
1290
	set link mru 1492
1291
	set ccp yes mpp-e40
1292
	set ccp yes mpp-e128
1293
	set ccp yes mpp-stateless
1294
	set link latency 1
1295
	#set ipcp dns 10.10.1.3
1296
	#set bundle accept encryption
1297

    
1298
EOD;
1299

    
1300
			if (!empty($pppoecfg['dns1'])) {
1301
				$mpdconf .= "	set ipcp dns " . $pppoecfg['dns1'];
1302
				if (!empty($pppoecfg['dns2']))
1303
					$mpdconf .= " " . $pppoecfg['dns2'];
1304
				$mpdconf .= "\n";
1305
			} elseif (isset ($config['dnsmasq']['enable'])) {
1306
				$mpdconf .= "	set ipcp dns " . get_interface_ip("lan");
1307
				if ($syscfg['dnsserver'][0])
1308
					$mpdconf .= " " . $syscfg['dnsserver'][0];
1309
				$mpdconf .= "\n";
1310
			} elseif (is_array($syscfg['dnsserver']) && ($syscfg['dnsserver'][0])) {
1311
					$mpdconf .= "	set ipcp dns " . join(" ", $syscfg['dnsserver']) . "\n";
1312
			}
1313

    
1314
			if (isset ($pppoecfg['radius']['server']['enable'])) {
1315
				$radiusport = "";
1316
				$radiusacctport = "";
1317
				if (isset($pppoecfg['radius']['server']['port']))
1318
					$radiusport = $pppoecfg['radius']['server']['port'];
1319
				if (isset($pppoecfg['radius']['server']['acctport']))
1320
					$radiusacctport = $pppoecfg['radius']['server']['acctport'];
1321
				$mpdconf .=<<<EOD
1322
	set radius server {$pppoecfg['radius']['server']['ip']} "{$pppoecfg['radius']['server']['secret']} {$radiusport} {$radiusacctport}" 
1323
	set radius retries 3
1324
	set radius timeout 10
1325
	set auth enable radius-auth
1326

    
1327
EOD;
1328

    
1329
				if (isset ($pppoecfg['radius']['accounting'])) {
1330
					$mpdconf .=<<<EOD
1331
	set auth enable radius-acct
1332

    
1333
EOD;
1334
				}
1335
			}
1336

    
1337
			fwrite($fd, $mpdconf);
1338
			fclose($fd);
1339

    
1340
			/* write mpd.links */
1341
			$fd = fopen("{$g['varetc_path']}/pppoe{$pppoecfg['pppoeid']}-vpn/mpd.links", "w");
1342
			if (!$fd) {
1343
				printf("Error: cannot open mpd.links in vpn_pppoe_configure().\n");
1344
				return 1;
1345
			}
1346

    
1347
			$mpdlinks = "";
1348

    
1349
			for ($i = 0; $i < $pppoecfg['n_pppoe_units']; $i++) {
1350
				$mpdlinks .=<<<EOD
1351
			
1352
poes{$pppoecfg['pppoeid']}{$i}:
1353
	set phys type pppoe
1354
        set pppoe iface {$pppoe_interface}
1355
        set pppoe service "*"
1356
        set pppoe disable originate
1357
        set pppoe enable incoming
1358

    
1359
EOD;
1360
			}
1361

    
1362
			fwrite($fd, $mpdlinks);
1363
			fclose($fd);
1364

    
1365
			if ($pppoecfg['username']) {
1366
				/* write mpd.secret */
1367
				$fd = fopen("{$g['varetc_path']}/pppoe{$pppoecfg['pppoeid']}-vpn/mpd.secret", "w");
1368
				if (!$fd) {
1369
					printf("Error: cannot open mpd.secret in vpn_pppoe_configure().\n");
1370
					return 1;
1371
				}
1372

    
1373
				$mpdsecret = "\n\n";
1374

    
1375
				if (!empty($pppoecfg['username'])) {
1376
					$item = explode(" ", $pppoecfg['username']);
1377
					foreach($item as $userdata) {
1378
						$data = explode(":", $userdata);
1379
						$mpdsecret .= "{$data[0]} \"" . base64_decode($data[1]) . "\" {$data[2]}\n";
1380
					}
1381
				}
1382

    
1383
				fwrite($fd, $mpdsecret);
1384
				fclose($fd);
1385
				chmod("{$g['varetc_path']}/pppoe{$pppoecfg['pppoeid']}-vpn/mpd.secret", 0600);
1386
			}
1387

    
1388
			/* Get support for netgraph(4) from the nic */
1389
			pfSense_ngctl_attach(".", $pppoe_interface);
1390
			/* fire up mpd */
1391
			mwexec("/usr/local/sbin/mpd4 -b -d {$g['varetc_path']}/pppoe{$pppoecfg['pppoeid']}-vpn -p {$g['varrun_path']}/pppoe{$pppoecfg['pppoeid']}-vpn.pid -s poes poes");
1392

    
1393
			break;
1394
	}
1395

    
1396
	if ($g['booting'])
1397
		echo "done\n";
1398

    
1399
	return 0;
1400
}
1401

    
1402
function vpn_l2tp_configure() {
1403
	global $config, $g;
1404

    
1405
	$syscfg = $config['system'];
1406
	$l2tpcfg = $config['l2tp'];
1407

    
1408
	/* create directory if it does not exist */
1409
	if (!is_dir("{$g['varetc_path']}/l2tp-vpn"))
1410
		mkdir("{$g['varetc_path']}/l2tp-vpn");
1411

    
1412
	if ($g['booting']) {
1413
		if (!$l2tpcfg['mode'] || ($l2tpcfg['mode'] == "off"))
1414
			return 0;
1415

    
1416
		echo "Configuring l2tp VPN service... ";
1417
	} else {
1418
		/* kill mpd */
1419
		killbypid("{$g['varrun_path']}/l2tp-vpn.pid");
1420

    
1421
		/* wait for process to die */
1422
		sleep(8);
1423

    
1424
	}
1425

    
1426
	/* make sure l2tp-vpn directory exists */
1427
	if (!file_exists("{$g['varetc_path']}/l2tp-vpn"))
1428
		mkdir("{$g['varetc_path']}/l2tp-vpn");
1429

    
1430
	switch ($l2tpcfg['mode']) {
1431

    
1432
		case 'server' :
1433
			if ($l2tpcfg['paporchap'] == "chap")
1434
				$paporchap = "set link enable chap";
1435
			else
1436
				$paporchap = "set link enable pap";
1437

    
1438
			/* write mpd.conf */
1439
			$fd = fopen("{$g['varetc_path']}/l2tp-vpn/mpd.conf", "w");
1440
			if (!$fd) {
1441
				printf("Error: cannot open mpd.conf in vpn_l2tp_configure().\n");
1442
				return 1;
1443
			}
1444
			$mpdconf = "\n\n";
1445
			$mpdconf .=<<<EOD
1446
l2tps:
1447

    
1448
EOD;
1449

    
1450
			for ($i = 0; $i < $l2tpcfg['n_l2tp_units']; $i++) {
1451
				$mpdconf .= "	load l2tp{$i}\n";
1452
			}
1453

    
1454
			for ($i = 0; $i < $l2tpcfg['n_l2tp_units']; $i++) {
1455

    
1456
				$clientip = long2ip32(ip2long($l2tpcfg['remoteip']) + $i);
1457

    
1458
				if (isset ($l2tpcfg['radius']['radiusissueips']) && isset ($l2tpcfg['radius']['enable'])) {
1459
					$isssue_ip_type = "set ipcp ranges {$l2tpcfg['localip']}/32 0.0.0.0/0";
1460
				} else {
1461
					$isssue_ip_type = "set ipcp ranges {$l2tpcfg['localip']}/32 {$clientip}/32";
1462
				}
1463

    
1464
				$mpdconf .=<<<EOD
1465

    
1466
l2tp{$i}:
1467
	new -i l2tp{$i} l2tp{$i} l2tp{$i}
1468
	{$isssue_ip_type}
1469
	load l2tp_standard
1470

    
1471
EOD;
1472
			}
1473

    
1474
			$mpdconf .=<<<EOD
1475

    
1476
l2tp_standard:
1477
        set bundle disable multilink
1478
        set bundle enable compression
1479
        set bundle yes crypt-reqd
1480
        set ipcp yes vjcomp
1481
        # set ipcp ranges 131.188.69.161/32 131.188.69.170/28
1482
        set ccp yes mppc
1483
        set iface disable on-demand
1484
        set iface enable proxy-arp
1485
	set iface up-script /usr/local/sbin/vpn-linkup
1486
	set iface down-script /usr/local/sbin/vpn-linkdown
1487
        set link yes acfcomp protocomp
1488
        set link no pap chap
1489
        set link enable chap
1490
        set link keep-alive 10 180
1491

    
1492
EOD;
1493

    
1494
			if (is_ipaddr($l2tpcfg['wins'])) {
1495
				$mpdconf .= "	set ipcp nbns {$l2tpcfg['wins']}\n";
1496
			}
1497
			if (is_ipaddr($l2tpcfg['dns1'])) {
1498
				$mpdconf .= "	set ipcp dns " . $l2tpcfg['dns1'];
1499
				if (is_ipaddr($l2tpcfg['dns2']))
1500
					$mpdconf .= " " . $l2tpcfg['dns2'];
1501
				$mpdconf .= "\n";
1502
			} elseif (isset ($config['dnsmasq']['enable'])) {
1503
				$mpdconf .= "	set ipcp dns " . get_interface_ip("lan");
1504
				if ($syscfg['dnsserver'][0])
1505
					$mpdconf .= " " . $syscfg['dnsserver'][0];
1506
				$mpdconf .= "\n";
1507
			} elseif (is_array($syscfg['dnsserver']) && ($syscfg['dnsserver'][0])) {
1508
					$mpdconf .= "	set ipcp dns " . join(" ", $syscfg['dnsserver']) . "\n";
1509
			}
1510

    
1511
			if (isset ($l2tpcfg['radius']['enable'])) {
1512
				$mpdconf .=<<<EOD
1513
	set radius server {$l2tpcfg['radius']['server']} "{$l2tpcfg['radius']['secret']}"
1514
	set radius retries 3
1515
	set radius timeout 10
1516
	set auth enable radius-auth
1517

    
1518
EOD;
1519

    
1520
				if (isset ($l2tpcfg['radius']['accounting'])) {
1521
					$mpdconf .=<<<EOD
1522
	set auth enable radius-acct
1523

    
1524
EOD;
1525
				}
1526
			}
1527

    
1528
			fwrite($fd, $mpdconf);
1529
			fclose($fd);
1530

    
1531
			/* write mpd.links */
1532
			$fd = fopen("{$g['varetc_path']}/l2tp-vpn/mpd.links", "w");
1533
			if (!$fd) {
1534
				printf("Error: cannot open mpd.links in vpn_l2tp_configure().\n");
1535
				return 1;
1536
			}
1537

    
1538
			$mpdlinks = "";
1539

    
1540
			for ($i = 0; $i < $l2tpcfg['n_l2tp_units']; $i++) {
1541
				$mpdlinks .=<<<EOD
1542

    
1543
l2tp{$i}:
1544
	set link type l2tp
1545
        set l2tp enable incoming
1546
        set l2tp disable originate
1547

    
1548
EOD;
1549
			if (!empty($l2tpcfg['secret']))
1550
					$mpdlinks .= "set l2tp secret {$l2tpcfg['secret']}\n";
1551
			}
1552

    
1553
			fwrite($fd, $mpdlinks);
1554
			fclose($fd);
1555

    
1556
			/* write mpd.secret */
1557
			$fd = fopen("{$g['varetc_path']}/l2tp-vpn/mpd.secret", "w");
1558
			if (!$fd) {
1559
				printf("Error: cannot open mpd.secret in vpn_l2tp_configure().\n");
1560
				return 1;
1561
			}
1562

    
1563
			$mpdsecret = "\n\n";
1564

    
1565
			if (is_array($l2tpcfg['user'])) {
1566
				foreach ($l2tpcfg['user'] as $user)
1567
					$mpdsecret .= "{$user['name']} \"{$user['password']}\" {$user['ip']}\n";
1568
			}
1569

    
1570
			fwrite($fd, $mpdsecret);
1571
			fclose($fd);
1572
			chmod("{$g['varetc_path']}/l2tp-vpn/mpd.secret", 0600);
1573

    
1574
			vpn_netgraph_support();
1575

    
1576
			/* fire up mpd */
1577
			mwexec("/usr/local/sbin/mpd4 -b -d {$g['varetc_path']}/l2tp-vpn -p {$g['varrun_path']}/l2tp-vpn.pid -s l2tps l2tps");
1578

    
1579
			break;
1580

    
1581
		case 'redir' :
1582
			break;
1583
	}
1584

    
1585
	if ($g['booting'])
1586
		echo "done\n";
1587

    
1588
	return 0;
1589
}
1590

    
1591
/* Walk the tunnels for hostname endpoints. If the hostnames 
1592
 * resolve to a different IP now compared to the DNS cache
1593
 * we reload the policies if the endpoint has changed */
1594
function vpn_ipsec_refresh_policies() {
1595
	global $config;
1596
	global $g;
1597

    
1598
	$ipseccfg = $config['ipsec'];
1599
	$a_phase1 = $config['ipsec']['phase1'];
1600
	$a_phase2 = $config['ipsec']['phase2'];
1601

    
1602
	if (isset($ipseccfg['disable'])) {
1603
		return true;
1604
	}
1605

    
1606
	/* Walk the Ipsec tunnel array */
1607
	if (!is_array($a_phase1) || (!count($a_phase1))) {
1608
		return;
1609
	}
1610

    
1611
	foreach ($a_phase1 as $phase1) {
1612
		if (isset($phase1['disabled'])) {
1613
			continue;
1614
		}
1615
		if (is_ipaddr($phase1['remote-gateway'])) {
1616
			continue;
1617
		}
1618
		if (!is_ipaddr($phase1['remote-gateway'])) {
1619
			$dnscache = compare_hostname_to_dnscache($phase1['remote-gateway']);
1620
			$dnscache = trim($dnscache);
1621
			/* we should have the old IP addresses in the dnscache now */
1622
			if($dnscache <> "") {
1623
				$oldphase1 = $phase1;
1624
				$oldphase1['remote-gateway'] = trim($dnscache);
1625
				/* now we need to find all tunnels for this host */
1626
				if (!is_array($a_phase2) || (!count($a_phase2))) {
1627
					continue;
1628
				}
1629
				foreach ($a_phase2 as $phase2) {
1630
					if($phase2['ikeid'] == $phase1['ikeid']) {
1631
						reload_tunnel_spd_policy ($phase1, $phase2, $oldphase1, $oldphase2);
1632
					}
1633
				}
1634
			}
1635
		}
1636
	}
1637

    
1638
	/* process all generated spd.conf files from tmp which are left behind
1639
	 * behind by either changes of dynamic tunnels or manual edits
1640
	 * scandir() is only available in PHP5 */
1641
	$tmpfiles = array();
1642
	$dh  = opendir($g['tmp_path']);
1643
	while (false !== ($filename = readdir($dh))) {
1644
		if(preg_match("/^spd.conf.reload./", $filename)) {
1645
			$tmpfiles[] = $filename;
1646
		}
1647
	}
1648
	sort($tmpfiles);
1649
	foreach($tmpfiles as $tmpfile) {
1650
		$ret = mwexec("/usr/local/sbin/setkey -f {$g['tmp_path']}/{$tmpfile} 2>&1", false);
1651
		if($ret == 0) {
1652
			unlink_if_exists("{$g['tmp_path']}/{$tmpfile}");
1653
		} else {
1654
			rename("{$g['tmp_path']}/{$tmpfile}", ("{$g['tmp_path']}/failed.{$tmpfile}"));
1655
		}
1656
	}
1657
}
1658

    
1659
/* reloads the tunnel configuration for a tunnel item
1660
 * Will remove and add SPD polices */
1661
function reload_tunnel_spd_policy($phase1, $phase2, $old_phase1, $old_phase2) {
1662
	global $config;
1663
	global $g;
1664

    
1665
	/* if we are not passed a old tunnel array we create one */
1666
	if(empty($old_phase1)) {
1667
		$old_phase1 = $phase1;
1668
	}
1669
	if(empty($old_phase2)) {
1670
		$old_phase2 = $phase2;
1671
	}
1672

    
1673
	$sad_arr = ipsec_dump_sad();
1674

    
1675
	$ep = ipsec_get_phase1_src($phase1);
1676
	$local_subnet = ipsec_idinfo_to_cidr($phase2['localid']);
1677
	$remote_subnet = ipsec_idinfo_to_cidr($phase2['remoteid']);
1678

    
1679
	/* make sure we pass the oldtunnel array with a IP for the remote gw */
1680
	$old_gw = trim($old_phase1['remote-gateway']);
1681

    
1682
	$old_ep = ipsec_get_phase1_src($old_phase1);
1683
	$old_local_subnet = ipsec_idinfo_to_cidr($old_phase2['localid']);
1684
	$old_remote_subnet = ipsec_idinfo_to_cidr($old_phase2['remoteid']);
1685

    
1686
	/* see if this tunnel has a hostname for the remote-gateway, and if so,
1687
	 * try to resolve it now and add it to the list for filterdns */
1688
	$rgip = "";
1689
	if (!is_ipaddr($phase1['remote-gateway'])) {
1690
		if(! $g['booting']) {
1691
			$rgip = resolve_retry($phase1['remote-gateway']);
1692
			add_hostname_to_watch($phase1['remote-gateway']);
1693
		} else {
1694
			add_hostname_to_watch($phase1['remote-gateway']);
1695
		}
1696
		if (!is_ipaddr($rgip)) {
1697
			log_error("Could not determine VPN endpoint for '{$phase1['descr']}'");
1698
			return false;
1699
		}
1700
	} else {
1701
		$rgip = $phase1['remote-gateway'];
1702
	}
1703
	if (!$ep) {
1704
		log_error("Could not determine VPN endpoint for '{$phase1['descr']}'");
1705
		return false;
1706
	}
1707

    
1708
	if((!is_ipaddr($old_ep)) || (! is_ipaddr($ep))) {
1709
		log_error("IPSEC: ERROR: One of the endpoints is not a IP address. Old EP '{$old_ep}' new EP '{$ep}'");
1710
	}
1711
	if((! is_ipaddr($rgip)) || (! is_ipaddr($old_gw))) {
1712
		log_error("IPSEC: ERROR: One of the remote endpoints is not a IP address. Old RG '{$old_gw}' new RG '{$rgip}'");
1713
	}
1714

    
1715
	$spdconf = "";
1716
	/* Delete old SPD policies if there are changes between the old and new */
1717
	if(($phase1 != $old_phase1) || ($phase2 != $old_phase2)) {
1718
		$spdconf .= "spddelete {$old_local_subnet} " .
1719
			"{$old_remote_subnet} any -P out ipsec " .
1720
			"{$old_phase2['protocol']}/tunnel/{$old_ep}-" .
1721
			"{$old_gw}/unique;\n";
1722
		$spdconf .= "spddelete {$old_remote_subnet} " .
1723
			"{$old_local_subnet} any -P in ipsec " .
1724
			"{$old_phase2['protocol']}/tunnel/{$old_gw}-" .
1725
			"{$old_ep}/unique;\n";
1726

    
1727
		/* zap any existing SA entries */
1728
		foreach($sad_arr as $sad) {
1729
			if(($sad['dst'] == $old_ep) && ($sad['src'] == $old_gw)) {
1730
				$spdconf .= "delete {$old_ep} {$old_gw} {$old_phase2['protocol']} 0x{$sad['spi']};\n";
1731
			}
1732
			if(($sad['src'] == $oldep) && ($sad['dst'] == $old_gw)) {
1733
				$spdconf .= "delete {$old_gw} {$old_ep} {$old_phase2['protocol']} 0x{$sad['spi']};\n";
1734
			}
1735
		}
1736
	}
1737

    
1738
	/* Create new SPD entries for the new configuration */
1739
	/* zap any existing SA entries beforehand */
1740
	foreach($sad_arr as $sad) {
1741
		if(($sad['dst'] == $ep) && ($sad['src'] == $rgip)) {
1742
			$spdconf .= "delete {$rgip} {$ep} {$phase2['protocol']} 0x{$sad['spi']};\n";
1743
		}
1744
		if(($sad['src'] == $ep) && ($sad['dst'] == $rgip)) {
1745
			$spdconf .= "delete {$ep} {$rgip} {$phase2['protocol']} 0x{$sad['spi']};\n";
1746
		}
1747
	}
1748
	/* add new SPD policies to replace them */
1749
	$spdconf .= "spdadd {$local_subnet} " .
1750
		"{$remote_subnet} any -P out ipsec " .
1751
		"{$phase2['protocol']}/tunnel/{$ep}-" .
1752
		"{$rgip}/unique;\n";
1753
	$spdconf .= "spdadd {$remote_subnet} " .
1754
		"{$local_subnet} any -P in ipsec " .
1755
		"{$phase2['protocol']}/tunnel/{$rgip}-" .
1756
		"{$ep}/unique;\n";
1757

    
1758
	log_error("Reloading IPsec tunnel '{$phase1['descr']}'. Previous IP '{$old_gw}', current IP '{$rgip}'. Reloading policy");
1759

    
1760
	$now = time();
1761
	$spdfile = tempnam("{$g['tmp_path']}", "spd.conf.reload.{$now}.");
1762
	/* generate temporary spd.conf */
1763
	file_put_contents($spdfile, $spdconf);
1764
	return true;
1765
}
1766

    
1767
function vpn_ipsec_configure_preferoldsa() {
1768
	global $config;
1769
	if(isset($config['ipsec']['preferoldsa']))
1770
		mwexec("/sbin/sysctl -w net.key.preferred_oldsa=-30");
1771
	else
1772
		mwexec("/sbin/sysctl net.key.preferred_oldsa=0");
1773
}
1774

    
1775
?>
(52-52/61)