Projet

Général

Profil

Télécharger (18,7 ko) Statistiques
| Branche: | Tag: | Révision:

univnautes / etc / inc / ipsec.inc @ 3c4fc30b

1
<?php
2
/*
3
	ipsec.inc
4
	Copyright (C) 2007 Scott Ullrich
5
	Copyright (C) 2008 Shrew Soft Inc
6
	All rights reserved.
7

    
8
	Parts of this code was originally based on vpn_ipsec_sad.php
9
	Copyright (C) 2003-2004 Manuel Kasper
10

    
11
	Redistribution and use in source and binary forms, with or without
12
	modification, are permitted provided that the following conditions are met:
13

    
14
	1. Redistributions of source code must retain the above copyright notice,
15
	   this list of conditions and the following disclaimer.
16

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

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

    
32
	pfSense_BUILDER_BINARIES:	/sbin/setkey
33
	pfSense_MODULE:	ipsec
34

    
35
*/
36

    
37
/* IPsec defines */
38
$ipsec_loglevels = array("dmn" => "Daemon", "mgr" => "SA Manager", "ike" => "IKE SA", "chd" => "IKE Child SA",
39
	"job" => "Job Processing", "cfg" => "Configuration backend", "knl" => "Kernel Interface",
40
	"net" => "Networking", "asn" => "ASN encoding", "enc" => "Message encoding",
41
	"imc" => "Integrity checker", "imv" => "Integrity Verifier", "pts" => "Platform Trust Service",
42
	"tls" => "TLS handler", "esp" => "IPsec traffic", "lib" => "StrongSWAN Lib");
43

    
44
$my_identifier_list = array(
45
	'myaddress' => array( 'desc' => gettext('My IP address'), 'mobile' => true ),
46
	'address' => array( 'desc' => gettext('IP address'), 'mobile' => true ),
47
	'fqdn' => array( 'desc' => gettext('Distinguished name'), 'mobile' => true ),
48
	'user_fqdn' => array( 'desc' => gettext('User distinguished name'), 'mobile' => true ),
49
	'asn1dn' => array( 'desc' => gettext('ASN.1 distinguished Name'), 'mobile' => true ),
50
	'keyid tag' => array( 'desc' => gettext('KeyID tag'), 'mobile' => true ),
51
	'dyn_dns' => array( 'desc' => gettext('Dynamic DNS'), 'mobile' => true ));
52

    
53
$peer_identifier_list = array(
54
	'peeraddress' => array( 'desc' => gettext('Peer IP address'), 'mobile' => false ),
55
	'address' => array( 'desc' => gettext('IP address'), 'mobile' => false ),
56
	'fqdn' => array( 'desc' => gettext('Distinguished name'), 'mobile' => true ),
57
	'user_fqdn' => array( 'desc' => gettext('User distinguished name'), 'mobile' => true ),
58
	'asn1dn' => array( 'desc' => gettext('ASN.1 distinguished Name'), 'mobile' => true ),
59
	'keyid tag' => array( 'desc' =>gettext('KeyID tag'), 'mobile' => true ));
60

    
61
$p1_ealgos = array(
62
	'aes' => array( 'name' => 'AES', 'keysel' => array( 'lo' => 128, 'hi' => 256, 'step' => 64 ) ),
63
	'aes128gcm' => array( 'name' => 'AES128-GCM', 'keysel' => array( 'lo' => 64, 'hi' => 128, 'step' => 32 ) ),
64
	'aes192gcm' => array( 'name' => 'AES192-GCM', 'keysel' => array( 'lo' => 64, 'hi' => 128, 'step' => 32 ) ),
65
	'aes256gcm' => array( 'name' => 'AES256-GCM', 'keysel' => array( 'lo' => 64, 'hi' => 128, 'step' => 32 ) ),
66
	'blowfish' => array( 'name' => 'Blowfish', 'keysel' => array( 'lo' => 128, 'hi' => 256, 'step' => 64 ) ),
67
	'3des' => array( 'name' => '3DES' ),
68
	'cast128' => array( 'name' => 'CAST128' ),
69
	'des' => array( 'name' => 'DES' ));
70

    
71
$p2_ealgos = array(
72
	'aes' => array( 'name' => 'AES', 'keysel' => array( 'lo' => 128, 'hi' => 256, 'step' => 64 ) ),
73
	'aes128gcm' => array( 'name' => 'AES128-GCM', 'keysel' => array( 'lo' => 64, 'hi' => 128, 'step' => 32 ) ),
74
	'aes192gcm' => array( 'name' => 'AES192-GCM', 'keysel' => array( 'lo' => 64, 'hi' => 128, 'step' => 32 ) ),
75
	'aes256gcm' => array( 'name' => 'AES256-GCM', 'keysel' => array( 'lo' => 64, 'hi' => 128, 'step' => 32 ) ),
76
	'blowfish' => array( 'name' => 'Blowfish', 'keysel' => array( 'lo' => 128, 'hi' => 256, 'step' => 64 ) ),
77
	'3des' => array( 'name' => '3DES' ),
78
	'cast128' => array( 'name' => 'CAST128' ),
79
	'des' => array( 'name' => 'DES' ));
80

    
81
$p1_halgos = array(
82
	'md5' => 'MD5',
83
	'sha1' => 'SHA1',
84
	'sha256' => 'SHA256',
85
	'sha384' => 'SHA384',
86
	'sha512' => 'SHA512',
87
	'aesxcbc' => 'AES-XCBC'
88
);
89

    
90
$p1_dhgroups = array(
91
	1  => '1 (768 bit)',
92
	2  => '2 (1024 bit)',
93
	5  => '5 (1536 bit)',
94
	14 => '14 (2048 bit)',
95
	15 => '15 (3072 bit)',
96
	16 => '16 (4096 bit)',
97
	17 => '17 (6144 bit)',
98
	18 => '18 (8192 bit)',
99
	22 => '22 (1024(sub 160) bit)',
100
	23 => '23 (2048(sub 224) bit)',
101
	24 => '24 (2048(sub 256) bit)'
102
);
103

    
104
$p2_halgos = array(
105
	'hmac_md5' => 'MD5',
106
	'hmac_sha1' => 'SHA1',
107
	'hmac_sha256' => 'SHA256',
108
	'hmac_sha384' => 'SHA384',
109
	'hmac_sha512' => 'SHA512',
110
	'aesxcbc' => 'AES-XCBC'
111
);
112

    
113
$p1_authentication_methods = array(
114
	'hybrid_rsa_server' => array( 'name' => 'Hybrid RSA + Xauth', 'mobile' => true ),
115
	'xauth_rsa_server' => array( 'name' => 'Mutual RSA + Xauth', 'mobile' => true ),
116
	'xauth_psk_server' => array( 'name' => 'Mutual PSK + Xauth', 'mobile' => true ),
117
	'eap-tls' => array( 'name' => 'EAP-TLS', 'mobile' => true),
118
	'rsasig' => array( 'name' => 'Mutual RSA', 'mobile' => false ),
119
	'pre_shared_key' => array( 'name' => 'Mutual PSK', 'mobile' => false ) );
120

    
121
$p2_modes = array(
122
	'tunnel' => 'Tunnel IPv4',
123
	'tunnel6' => 'Tunnel IPv6',
124
	'transport' => 'Transport');
125

    
126
$p2_protos = array(
127
	'esp' => 'ESP',
128
	'ah' => 'AH');
129

    
130
$p2_pfskeygroups = array(
131
	0 => 'off',
132
	1  => '1 (768 bit)',
133
	2  => '2 (1024 bit)',
134
	5  => '5 (1536 bit)',
135
	14 => '14 (2048 bit)',
136
	15 => '15 (3072 bit)',
137
	16 => '16 (4096 bit)',
138
	17 => '17 (6144 bit)',
139
	18 => '18 (8192 bit)'
140
);
141

    
142
/*
143
 * ikeid management functions
144
 */
145

    
146
function ipsec_ikeid_used($ikeid) {
147
	global $config;
148

    
149
	foreach ($config['ipsec']['phase1'] as $ph1ent)
150
		if( $ikeid == $ph1ent['ikeid'] )
151
			return true;
152

    
153
	return false;
154
}
155

    
156
function ipsec_ikeid_next() {
157

    
158
	$ikeid = 1;
159
	while(ipsec_ikeid_used($ikeid))
160
		$ikeid++;
161

    
162
	return $ikeid;
163
}
164

    
165
/*
166
 * Return phase1 local address
167
 */
168
function ipsec_get_phase1_src(& $ph1ent) {
169

    
170
	if ($ph1ent['interface']) {
171
		if (!is_ipaddr($ph1ent['interface'])) {
172
			if ($ph1ent['protocol'] == "inet6") { 
173
				$if = get_failover_interface($ph1ent['interface'], "inet6");
174
				$interfaceip = get_interface_ipv6($if);
175
			} else {
176
				$if = get_failover_interface($ph1ent['interface']);
177
				$interfaceip = get_interface_ip($if);
178
			}
179
		} else {
180
			$interfaceip=$ph1ent['interface'];
181
		}
182
	} else {
183
		$if = "wan";
184
		if ($ph1ent['protocol'] == "inet6")
185
			$interfaceip = get_interface_ipv6($if);
186
		else
187
			$interfaceip = get_interface_ip($if);
188
	}
189

    
190
	return $interfaceip;
191
}
192

    
193
/*
194
 * Return phase1 local address
195
 */
196
function ipsec_get_phase1_dst(& $ph1ent) {
197
	global $g;
198

    
199
	if (empty($ph1ent['remote-gateway']))
200
		return false;
201
	$rg = $ph1ent['remote-gateway'];
202
	if (!is_ipaddr($rg)) {
203
		if(! $g['booting'])
204
			return resolve_retry($rg);
205
	}
206
	if(!is_ipaddr($rg))
207
		return false;
208

    
209
	return $rg;
210
}
211

    
212
/*
213
 * Return phase2 idinfo in cidr format
214
 */
215
function ipsec_idinfo_to_cidr(& $idinfo, $addrbits = false, $mode = "") {
216
	global $config;
217

    
218
	switch ($idinfo['type']) {
219
		case "address":
220
			if ($addrbits) {
221
				if ($mode == "tunnel6")
222
					return $idinfo['address']."/128";
223
				else
224
					return $idinfo['address']."/32";
225
			} else
226
				return $idinfo['address'];
227
			break; /* NOTREACHED */
228
		case "network":
229
			return "{$idinfo['address']}/{$idinfo['netbits']}";
230
			break; /* NOTREACHED */
231
		case "none":
232
		case "mobile":
233
			return "0.0.0.0/0";
234
			break; /* NOTREACHED */
235
		default:
236
			if (empty($mode) && !empty($idinfo['mode']))
237
				$mode = $idinfo['mode'];
238

    
239
			if ($mode == "tunnel6") {
240
				$address = get_interface_ipv6($idinfo['type']);
241
				$netbits = get_interface_subnetv6($idinfo['type']);
242
				$address = gen_subnetv6($address,$netbits);
243
				return "{$address}/{$netbits}";
244
			} else {
245
				$address = get_interface_ip($idinfo['type']);
246
				$netbits = get_interface_subnet($idinfo['type']);
247
				$address = gen_subnet($address,$netbits);
248
				return "{$address}/{$netbits}";
249
			}
250
			break; /* NOTREACHED */
251
	}
252
}
253

    
254
/*
255
 * Return phase2 idinfo in address/netmask format
256
 */
257
function ipsec_idinfo_to_subnet(& $idinfo,$addrbits = false) {
258
	global $config;
259

    
260
	switch ($idinfo['type']) {
261
		case "address":
262
			if ($addrbits) {
263
				if ($idinfo['mode'] == "tunnel6")
264
					return $idinfo['address']."/128";
265
				else
266
					return $idinfo['address']."/255.255.255.255";
267
			} else
268
				return $idinfo['address'];
269
			break; /* NOTREACHED */
270
		case "none":
271
		case "network":
272
			return $idinfo['address']."/".gen_subnet_mask($idinfo['netbits']);
273
			break; /* NOTREACHED */
274
		case "mobile":
275
			return "0.0.0.0/0";
276
			break; /* NOTREACHED */
277
		default:
278
			if ($idinfo['mode'] == "tunnel6") {
279
				$address = get_interface_ipv6($idinfo['type']);
280
				$netbits = get_interface_subnetv6($idinfo['type']);
281
				$address = gen_subnetv6($address,$netbits);
282
				return $address."/".$netbits;
283
			} else {
284
				$address = get_interface_ip($idinfo['type']);
285
				$netbits = get_interface_subnet($idinfo['type']);
286
				$address = gen_subnet($address,$netbits);
287
				return $address."/".$netbits;
288
			}
289
			break; /* NOTREACHED */
290
	}
291
}
292

    
293
/*
294
 *  Return phase2 idinfo in text format
295
 */
296
function ipsec_idinfo_to_text(& $idinfo) {
297
	global $config;
298

    
299
	switch ($idinfo['type']) {
300
        case "address":
301
		return $idinfo['address'];
302
		break; /* NOTREACHED */
303
        case "network":
304
		return $idinfo['address']."/".$idinfo['netbits'];
305
		break; /* NOTREACHED */
306
	case "mobile":
307
		return gettext("Mobile Client");
308
		break; /* NOTREACHED */
309
	case "none":
310
		return gettext("None");
311
		break; /* NOTREACHED */
312
        default:
313
		if (!empty($config['interfaces'][$idinfo['type']]))
314
			return convert_friendly_interface_to_friendly_descr($idinfo['type']);
315
		else
316
			return strtoupper($idinfo['type']);
317
		break; /* NOTREACHED */
318
	}
319
}
320

    
321
/*
322
 * Return phase1 association for phase2
323
 */
324
function ipsec_lookup_phase1(& $ph2ent,& $ph1ent) {
325
	global $config;
326

    
327
	if (!is_array($config['ipsec']))
328
		return false;
329
	if (!is_array($config['ipsec']['phase1']))
330
		return false;
331
	if (empty($config['ipsec']['phase1']))
332
		return false;
333

    
334
	foreach ($config['ipsec']['phase1'] as $ph1tmp) {
335
	    if ($ph1tmp['ikeid'] == $ph2ent['ikeid']) {
336
		$ph1ent = $ph1tmp;
337
		return $ph1ent;
338
	    }
339
	}
340

    
341
	return false;
342
}
343

    
344
/*
345
 * Check phase1 communications status
346
 */
347
function ipsec_phase1_status($ipsec_status, $ikeid) {
348

    
349
	foreach ($ipsec_status as $ike) {
350
		if ($ike['id'] != $ikeid)
351
			continue;
352
		if ($ike['status'] == 'established')
353
			return true;
354
		break;
355
	}
356

    
357
	return false;
358
}
359

    
360
/*
361
 * Check phase2 communications status
362
 */
363
function ipsec_phase2_status($ipsec_status, &$phase2) {
364

    
365
	if (ipsec_lookup_phase1($ph2ent,$ph1ent))
366
		return ipsec_phase1_status($ipsec_status, $ph1ent['ikeid']);
367

    
368
	return false;
369
}
370

    
371
/*
372
 * Return ISAKMP SA details
373
 */
374
function ipsec_lookup_isakmp_sa($in_srcip,$in_dstip) {
375
	/* TODO : use racconctl to lookup iskamp SA */
376
	return NULL;
377
}
378

    
379
/*
380
 * Return IPsec SA details
381
 */
382
function ipsec_lookup_ipsec_sa(& $spd,& $sad,$dir,$in_srcip,$in_dstip,$in_srcid,$in_dstid) {
383

    
384
	/* match the phase1/2 to an SP */
385
	$in_srcip = ipsec_fixup_ip($in_srcip);
386
	$in_dstip = ipsec_fixup_ip($in_dstip);
387
	$in_srcid = ipsec_fixup_ip($in_srcid);
388
	$in_dstid = ipsec_fixup_ip($in_dstid);
389

    
390
	foreach($spd as $sp) {
391

    
392
		/* match direction */
393

    
394
		if($dir != $sp['dir'])
395
			continue;
396

    
397
		/* match IPs */
398

    
399
		if($in_srcip != ipsec_fixup_ip($sp['src']))
400
			continue;
401
		if($in_dstip != ipsec_fixup_ip($sp['dst']))
402
			continue;
403

    
404
		/* add netbits for address IDs */
405

    
406
		$sp_srcid = $sp['srcid'];
407
		$sp_dstid = $sp['dstid'];
408

    
409
		if (!strstr($sp_srcid,"/")) {
410
			if (is_ipaddrv4($sp_srcid))
411
				$sp_srcid .= '/32';
412
			elseif (is_ipaddrv6($sp_srcid))
413
				$sp_srcid .= '/128';
414
		}
415
		if (!strstr($sp_dstid,"/")) {
416
			if (is_ipaddrv4($sp_dstid))
417
				$sp_dstid .= '/32';
418
			elseif (is_ipaddrv6($sp_dstid))
419
				$sp_dstid .= '/128';
420
		}
421

    
422
		/* match IDs */
423

    
424
		if($in_srcid != ipsec_fixup_ip($sp_srcid))
425
			continue;
426
		if($in_dstid != ipsec_fixup_ip($sp_dstid))
427
			continue;
428

    
429
		/* match the SP to a unique SA by reqid */
430

    
431
		foreach($sad as $sa) {
432

    
433
			/* match REQIDs */
434

    
435
			if($sa[reqid] != $sp[reqid])
436
				continue;
437

    
438
			/* sanitize for NAT-T ports */
439

    
440
			$sa_srcip = $sa['src'];
441
			$sa_dstip = $sa['dst'];
442

    
443
			if (strstr($sa_srcip,"["))
444
				$sa_srcip = substr($sa_srcip,0,strcspn($sa_srcip,"["));
445
			if (strstr($sa_dstip,"["))
446
				$sa_dstip = substr($sa_dstip,0,strcspn($sa_dstip,"["));
447

    
448
			/* match IPs */
449

    
450
			if($in_srcip != ipsec_fixup_ip($sa_srcip))
451
				continue;
452
			if($in_dstip != ipsec_fixup_ip($sa_dstip))
453
				continue;
454

    
455
			return $sa;
456
		}
457
	}
458

    
459
	return NULL;
460
}
461

    
462
function ipsec_smp_dump_status() {
463
	global $config, $g, $custom_listtags;
464

    
465
	if (!file_exists("{$g['varrun_path']}/charon.xml")) {
466
		log_error("IPsec daemon seems to have issues or not running!");
467
		return;
468
	}
469

    
470
	$fd = @fsockopen("unix://{$g['varrun_path']}/charon.xml");
471
	if (!$fd) {
472
		log_error("Could not read status from ipsec");
473
		return;
474
	}
475
	$query = '<?xml version="1.0"?><message xmlns="http://www.strongswan.org/smp/1.0" type="request" id="1">';
476
	$query .= '<query><ikesalist/></query></message>';
477

    
478
	@fwrite($fd, $query);
479
	$response = "";
480
	while (!strstr($sread, "</message>")) {
481
		$sread = fgets($fd);
482
		$response .= $sread;
483
	}
484
	fclose($fd);
485

    
486
	@file_put_contents("{$g['tmp_path']}/smp_status.xml", $response);
487
	unset($response, $sread);
488

    
489
	$custom_listtags = array('ikesa', 'childsa', 'network', 'auth');
490
	$response = parse_xml_config("{$g['tmp_path']}/smp_status.xml", "message");
491
	@unlink("{$g['tmp_path']}/smp_status.xml");
492
	unset($custom_listtags);
493

    
494
	return $response;
495
}
496

    
497
/*
498
 * Return dump of SPD table
499
 */
500
function ipsec_dump_spd()
501
{
502
	$fd = @popen("/sbin/setkey -DP", "r");
503
	$spd = array();
504
	if ($fd) {
505
		while (!feof($fd)) {
506
			$line = chop(fgets($fd));
507
			if (!$line)
508
				continue;
509
			if ($line == "No SPD entries.")
510
				break;
511
			if ($line[0] != "\t") {
512
				if (is_array($cursp))
513
					$spd[] = $cursp;
514
				$cursp = array();
515
				$linea = explode(" ", $line);
516
				$cursp['srcid'] = substr($linea[0], 0, strpos($linea[0], "["));
517
				$cursp['dstid'] = substr($linea[1], 0, strpos($linea[1], "["));
518
				$i = 0;
519
			} else if (is_array($cursp)) {
520
				$linea = explode(" ", trim($line));
521
				switch($i)
522
				{
523
					case 1:
524
						if ($linea[1] == "none")	/* don't show default anti-lockout rule */
525
							unset($cursp);
526
						else
527
							$cursp['dir'] = $linea[0];
528
						break;
529
					case 2:
530
						$upperspec = explode("/", $linea[0]);
531
						$cursp['proto'] = $upperspec[0];
532
						list($cursp['src'], $cursp['dst']) = explode("-", $upperspec[2]);
533
						$cursp['reqid'] =  substr($upperspec[3], strpos($upperspec[3], "#")+1);
534
						break;
535
				}
536
			}
537
			$i++;
538
		}
539
		if (is_array($cursp) && count($cursp))
540
			$spd[] = $cursp;
541
		pclose($fd);
542
	}
543

    
544
	return $spd;
545
}
546

    
547
/*
548
 * Return dump of SAD table
549
 */
550
function ipsec_dump_sad()
551
{
552
	$fd = @popen("/sbin/setkey -D", "r");
553
	$sad = array();
554
	if ($fd) {
555
		while (!feof($fd)) {
556
			$line = chop(fgets($fd));
557
			if (!$line || $line[0] == " ")
558
				continue;
559
			if ($line == "No SAD entries.")
560
				break;
561
			if ($line[0] != "\t")
562
			{
563
				if (is_array($cursa))
564
					$sad[] = $cursa;
565
				$cursa = array();
566
				list($cursa['src'],$cursa['dst']) = explode(" ", $line);
567
				$i = 0;
568
			}
569
			else
570
			{
571
				$linea = explode(" ", trim($line));
572
				switch ($i) {
573
					case 1:
574
						$cursa['proto'] = $linea[0];
575
						$cursa['spi'] = substr($linea[2], strpos($linea[2], "x")+1, -1);
576
						$reqid = substr($linea[3], strpos($linea[3], "=")+1);
577
						$cursa['reqid'] = substr($reqid, 0, strcspn($reqid,"("));
578
						break;
579
					case 2:
580
						$cursa['ealgo'] = $linea[1];
581
						break;
582
					case 3:
583
						$cursa['aalgo'] = $linea[1];
584
						break;
585
					case 8:
586
						$sadata = explode("(", $linea[1]);
587
						$cursa['data'] = $sadata[0] . " B";
588
						break;
589
				}
590
			}
591
			$i++;
592
		}
593
		if (is_array($cursa) && count($cursa))
594
			$sad[] = $cursa;
595
		pclose($fd);
596
	}
597

    
598
	return $sad;
599
}
600

    
601
/*
602
 * Return dump of mobile user list
603
 */
604
function ipsec_dump_mobile() {
605
	global $g, $custom_listtags;
606

    
607
	$_gb = exec("/usr/local/sbin/ipsec stroke leases > {$g['tmp_path']}/strongswan_leases.xml");
608

    
609
	if (!file_exists("{$g['tmp_path']}/strongswan_leases.xml")) {
610
		log_error(gettext("IPsec daemon seems to have issues or not running! Could not display mobile user stats!"));
611
		return array();
612
	}
613

    
614
	$custom_listtags = array('lease', 'pool');
615
	$response = parse_xml_config("{$g['tmp_path']}/strongswan_leases.xml", "leases");
616
	@unlink("{$g['tmp_path']}/strongswan_leases.xml");
617
	unset($custom_listtags, $_gb);
618

    
619
	return $response;
620
}
621

    
622
function ipsec_mobilekey_sort() {
623
	global $config;
624

    
625
	function mobilekeycmp($a, $b) {
626
		return strcmp($a['ident'][0], $b['ident'][0]);
627
	}
628

    
629
	usort($config['ipsec']['mobilekey'], "mobilekeycmp");
630
}
631

    
632
function ipsec_get_number_of_phase2($ikeid) {
633
	global $config;
634
    	$a_phase2 = $config['ipsec']['phase2'];
635

    
636
	$nbph2=0;
637

    
638
    	if (is_array($a_phase2) && count($a_phase2)) {
639
        	foreach ($a_phase2 as $ph2tmp) {
640
            		if ($ph2tmp['ikeid'] == $ikeid) {
641
				$nbph2++;
642
			}
643
		}
644
	}
645

    
646
	return $nbph2;
647
}
648

    
649
function ipsec_get_descr($ikeid) {
650
	global $config;
651

    
652
	if (!isset($config['ipsec']['phase1']) ||
653
	    !is_array($config['ipsec']['phase1']))
654
		return "";
655

    
656
	$descr = '';
657
	$a_phase1 = $config['ipsec']['phase1'];
658
	foreach ($a_phase1 as $p1) {
659
		if ($p1['ikeid'] == $ikeid) {
660
			$descr = $p1['descr'];
661
			break;
662
		}
663
	}
664
	unset($a_phase1);
665

    
666
	return $descr;
667
}
668

    
669
function ipsec_fixup_ip($ipaddr) {
670
	if (is_ipaddrv6($ipaddr) || is_subnetv6($ipaddr))
671
		return Net_IPv6::compress(Net_IPv6::uncompress($ipaddr));
672
	else
673
		return $ipaddr;
674
}
675

    
676
function ipsec_find_id(& $ph1ent, $side = "local", $rgmap = array()) {
677
	if ($side == "local") {
678
		$id_type = $ph1ent['myid_type'];
679
		$id_data = $ph1ent['myid_data'];
680

    
681
		$addr = ipsec_get_phase1_src($ph1ent);
682
		if (!$addr)
683
			return array();
684
	} elseif ($side = "peer") {
685
		$id_type = $ph1ent['peerid_type'];
686
		$id_data = $ph1ent['peerid_data'];
687

    
688
		if (isset($ph1ent['mobile']))
689
			$addr = "%any";
690
		else
691
			$addr = $ph1ent['remote-gateway'];
692
	} else {
693
		return array();
694
	}
695

    
696

    
697
	$thisid_type = $id_type;
698
	switch ($thisid_type) {
699
	case "myaddress":
700
		$thisid_type = "address";
701
		$thisid_data = $addr;
702
		break;
703

    
704
	case "dyn_dns":
705
		$thisid_type = "address";
706
		$thisid_data = resolve_retry($id_data);
707
		break;
708

    
709
	case "peeraddress":
710
		$thisid_type = "address";
711
		$thisid_data = $rgmap[$ph1ent['remote-gateway']];
712
		break;
713

    
714
	case "address";
715
		$thisid_data = $id_data;
716
		break;
717

    
718
	case "fqdn";
719
	case "keyid tag";
720
	case "user_fqdn";
721
	case "asn1dn";
722
		$thisid_data = $id_data;
723
		if( $thisid_data )
724
			$thisid_data = "{$thisid_data}";
725
		break;
726
	}
727
	return array($thisid_type, $thisid_data);
728
}
729

    
730
function ipsec_fixup_network($network) {
731
	if (substr($network, -3) == '|/0')
732
		$result = substr($network, 0, -3);
733
	else {
734
		$tmp = explode('|', $network);
735
		if (isset($tmp[1]))
736
			$result = $tmp[1];
737
		else
738
			$result = $tmp[0];
739
		unset($tmp);
740
	}
741

    
742
	return $result;
743
}
744

    
745
?>
(29-29/68)