Projet

Général

Profil

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

univnautes / etc / inc / ipsec.inc @ 17402c63

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');
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
	$command = "/usr/local/sbin/racoonctl show-users";
605
	$fd = @popen($command, "r");
606
	$mobile = array();
607
	if ($fd) {
608
		while (!feof($fd)) {
609
			$user = array();
610
			$line = chop(fgets($fd));
611
			if (!$line)
612
				continue;
613
			if ($line == "User|Source|Destination|CreatedOn|SPI")
614
				continue;
615

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

    
629
	return $mobile;
630
}
631

    
632
function ipsec_mobilekey_sort() {
633
	global $config;
634

    
635
	function mobilekeycmp($a, $b) {
636
		return strcmp($a['ident'][0], $b['ident'][0]);
637
	}
638

    
639
	usort($config['ipsec']['mobilekey'], "mobilekeycmp");
640
}
641

    
642
function ipsec_get_number_of_phase2($ikeid) {
643
	global $config;
644
    	$a_phase2 = $config['ipsec']['phase2'];
645

    
646
	$nbph2=0;
647

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

    
656
	return $nbph2;
657
}
658

    
659
function ipsec_get_descr($ikeid) {
660
	global $config;
661

    
662
	if (!isset($config['ipsec']['phase1']) ||
663
	    !is_array($config['ipsec']['phase1']))
664
		return "";
665

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

    
676
	return $descr;
677
}
678

    
679
function ipsec_disconnect_mobile($username) {
680
	if (empty($username))
681
		return false;
682
	exec("/usr/local/sbin/racoonctl logout-user " . escapeshellarg($username));
683
}
684

    
685
function ipsec_fixup_ip($ipaddr) {
686
	if (is_ipaddrv6($ipaddr) || is_subnetv6($ipaddr))
687
		return Net_IPv6::compress(Net_IPv6::uncompress($ipaddr));
688
	else
689
		return $ipaddr;
690
}
691

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

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

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

    
712

    
713
	$thisid_type = $id_type;
714
	switch ($thisid_type) {
715
	case "myaddress":
716
		$thisid_type = "address";
717
		$thisid_data = $addr;
718
		break;
719

    
720
	case "dyn_dns":
721
		$thisid_type = "address";
722
		$thisid_data = resolve_retry($id_data);
723
		break;
724

    
725
	case "peeraddress":
726
		$thisid_type = "address";
727
		$thisid_data = $rgmap[$ph1ent['remote-gateway']];
728
		break;
729

    
730
	case "address";
731
		$thisid_data = $id_data;
732
		break;
733

    
734
	case "fqdn";
735
	case "keyid tag";
736
	case "user_fqdn";
737
	case "asn1dn";
738
		$thisid_data = $id_data;
739
		if( $thisid_data )
740
			$thisid_data = "{$thisid_data}";
741
		break;
742
	}
743
	return array($thisid_type, $thisid_data);
744
}
745
?>
(29-29/68)