Projet

Général

Profil

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

univnautes / etc / inc / ipsec.inc @ ed5fc757

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", "app" => "Not daemon", "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
	'rsasig' => array( 'name' => 'Mutual RSA', 'mobile' => false ),
118
	'pre_shared_key' => array( 'name' => 'Mutual PSK', 'mobile' => false ) );
119

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

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

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

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

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

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

    
152
	return false;
153
}
154

    
155
function ipsec_ikeid_next() {
156

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

    
161
	return $ikeid;
162
}
163

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

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

    
189
	return $interfaceip;
190
}
191

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

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

    
208
	return $rg;
209
}
210

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

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

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

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

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

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

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

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

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

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

    
340
	return false;
341
}
342

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

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

    
356
	return false;
357
}
358

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

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

    
367
	return false;
368
}
369

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

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

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

    
389
	foreach($spd as $sp) {
390

    
391
		/* match direction */
392

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

    
396
		/* match IPs */
397

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

    
403
		/* add netbits for address IDs */
404

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

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

    
421
		/* match IDs */
422

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

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

    
430
		foreach($sad as $sa) {
431

    
432
			/* match REQIDs */
433

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

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

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

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

    
447
			/* match IPs */
448

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

    
454
			return $sa;
455
		}
456
	}
457

    
458
	return NULL;
459
}
460

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

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

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

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

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

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

    
493
	return $response;
494
}
495

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

    
543
	return $spd;
544
}
545

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

    
597
	return $sad;
598
}
599

    
600
/*
601
 * Return dump of mobile user list
602
 */
603
function ipsec_dump_mobile() {
604
	/* XXX: Need to be fixed */
605
	return array();
606

    
607
	$command = "/usr/local/sbin/racoonctl show-users";
608
	$fd = @popen($command, "r");
609
	$mobile = array();
610
	if ($fd) {
611
		while (!feof($fd)) {
612
			$user = array();
613
			$line = chop(fgets($fd));
614
			if (!$line)
615
				continue;
616
			if ($line == "User|Source|Destination|CreatedOn|SPI")
617
				continue;
618

    
619
			// jim|192.168.20.243:4500|192.168.20.5:24146|2012-05-25 09:54:39|989d10e1e2d4eca4:7243830d5fd2afe7
620
			$linea = explode("|", trim($line));
621
			$user['username'] = $linea[0];
622
			$user['local'] = $linea[1];
623
			$user['remote'] = $linea[2];
624
			$user['logintime'] = $linea[3];
625
			$user['spi'] = $linea[4];
626
			if (!empty($user['username']))
627
				$mobile[] = $user;
628
		}
629
		pclose($fd);
630
	}
631

    
632
	return $mobile;
633
}
634

    
635
function ipsec_mobilekey_sort() {
636
	global $config;
637

    
638
	function mobilekeycmp($a, $b) {
639
		return strcmp($a['ident'][0], $b['ident'][0]);
640
	}
641

    
642
	usort($config['ipsec']['mobilekey'], "mobilekeycmp");
643
}
644

    
645
function ipsec_get_number_of_phase2($ikeid) {
646
	global $config;
647
    	$a_phase2 = $config['ipsec']['phase2'];
648

    
649
	$nbph2=0;
650

    
651
    	if (is_array($a_phase2) && count($a_phase2)) {
652
        	foreach ($a_phase2 as $ph2tmp) {
653
            		if ($ph2tmp['ikeid'] == $ikeid) {
654
				$nbph2++;
655
			}
656
		}
657
	}
658

    
659
	return $nbph2;
660
}
661

    
662
function ipsec_get_descr($ikeid) {
663
	global $config;
664

    
665
	if (!isset($config['ipsec']['phase1']) ||
666
	    !is_array($config['ipsec']['phase1']))
667
		return "";
668

    
669
	$descr = '';
670
	$a_phase1 = $config['ipsec']['phase1'];
671
	foreach ($a_phase1 as $p1) {
672
		if ($p1['ikeid'] == $ikeid) {
673
			$descr = $p1['descr'];
674
			break;
675
		}
676
	}
677
	unset($a_phase1);
678

    
679
	return $descr;
680
}
681

    
682
function ipsec_fixup_ip($ipaddr) {
683
	if (is_ipaddrv6($ipaddr) || is_subnetv6($ipaddr))
684
		return Net_IPv6::compress(Net_IPv6::uncompress($ipaddr));
685
	else
686
		return $ipaddr;
687
}
688

    
689
function ipsec_find_id(& $ph1ent, $side = "local", $rgmap = array()) {
690
	if ($side == "local") {
691
		$id_type = $ph1ent['myid_type'];
692
		$id_data = $ph1ent['myid_data'];
693

    
694
		$addr = ipsec_get_phase1_src($ph1ent);
695
		if (!$addr)
696
			return array();
697
	} elseif ($side = "peer") {
698
		$id_type = $ph1ent['peerid_type'];
699
		$id_data = $ph1ent['peerid_data'];
700

    
701
		if (isset($ph1ent['mobile']))
702
			$addr = "%any";
703
		else
704
			$addr = $ph1ent['remote-gateway'];
705
	} else {
706
		return array();
707
	}
708

    
709

    
710
	$thisid_type = $id_type;
711
	switch ($thisid_type) {
712
	case "myaddress":
713
		$thisid_type = "address";
714
		$thisid_data = $addr;
715
		break;
716

    
717
	case "dyn_dns":
718
		$thisid_type = "address";
719
		$thisid_data = resolve_retry($id_data);
720
		break;
721

    
722
	case "peeraddress":
723
		$thisid_type = "address";
724
		$thisid_data = $rgmap[$ph1ent['remote-gateway']];
725
		break;
726

    
727
	case "address";
728
		$thisid_data = $id_data;
729
		break;
730

    
731
	case "fqdn";
732
	case "keyid tag";
733
	case "user_fqdn";
734
	case "asn1dn";
735
		$thisid_data = $id_data;
736
		if( $thisid_data )
737
			$thisid_data = "{$thisid_data}";
738
		break;
739
	}
740
	return array($thisid_type, $thisid_data);
741
}
742

    
743
function ipsec_fixup_network($network) {
744
	if (substr($network, -3) == '|/0')
745
		$result = substr($network, 0, -3);
746
	else {
747
		$tmp = explode('|', $network);
748
		if (isset($tmp[1]))
749
			$result = $tmp[1];
750
		else
751
			$result = $tmp[0];
752
		unset($tmp);
753
	}
754

    
755
	return $result;
756
}
757

    
758
?>
(29-29/68)