Project

General

Profile

Download (16 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/* $Id$ */
3
/*
4
	status_dhcpv6_leases.php
5
	Copyright (C) 2013-2015 Electric Sheep Fencing, LP
6
	Copyright (C) 2011 Seth Mos
7
	Copyright (C) 2004-2009 Scott Ullrich
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/awk	/bin/cat	/usr/sbin/ndp	/usr/bin/wc	/usr/bin/grep
38
	pfSense_MODULE:	dhcpserver
39
*/
40

    
41
##|+PRIV
42
##|*IDENT=page-status-dhcpv6leases
43
##|*NAME=Status: DHCPv6 leases page
44
##|*DESCR=Allow access to the 'Status: DHCPv6 leases' page.
45
##|*MATCH=status_dhcpv6_leases.php*
46
##|-PRIV
47

    
48
require("guiconfig.inc");
49
require_once("config.inc");
50

    
51
$pgtitle = array(gettext("Status"),gettext("DHCPv6 leases"));
52
$shortcut_section = "dhcp6";
53

    
54
$leasesfile = "{$g['dhcpd_chroot_path']}/var/db/dhcpd6.leases";
55

    
56
if (($_GET['deleteip']) && (is_ipaddr($_GET['deleteip']))) {
57
	/* Stop DHCPD */
58
	killbyname("dhcpd");
59

    
60
	/* Read existing leases */
61
	$leases_contents = explode("\n", file_get_contents($leasesfile));
62
	$newleases_contents = array();
63
	$i=0;
64
	while ($i < count($leases_contents)) {
65
		/* Find the lease(s) we want to delete */
66
		if ($leases_contents[$i] == "  iaaddr {$_GET['deleteip']} {") {
67
			/* The iaaddr line is two lines down from the start of the lease, so remove those two lines. */
68
			array_pop($newleases_contents);
69
			array_pop($newleases_contents);
70
			/* Skip to the end of the lease declaration */
71
			do {
72
				$i++;
73
			} while ($leases_contents[$i] != "}");
74
		} else {
75
			/* It's a line we want to keep, copy it over. */
76
			$newleases_contents[] = $leases_contents[$i];
77
		}
78
		$i++;
79
	}
80

    
81
	/* Write out the new leases file */
82
	$fd = fopen($leasesfile, 'w');
83
	fwrite($fd, implode("\n", $newleases_contents));
84
	fclose($fd);
85

    
86
	/* Restart DHCP Service */
87
	services_dhcpd_configure();
88
	header("Location: status_dhcpv6_leases.php?all={$_GET['all']}");
89
}
90

    
91
// Load MAC-Manufacturer table
92
$mac_man = load_mac_manufacturer_table();
93

    
94
include("head.inc");
95

    
96
function leasecmp($a, $b) {
97
	return strcmp($a[$_GET['order']], $b[$_GET['order']]);
98
}
99

    
100
function adjust_gmt($dt) {
101
	global $config;
102

    
103
	$dhcpv6leaseinlocaltime == "no";
104
	if (is_array($config['dhcpdv6'])) {
105
		$dhcpdv6 = $config['dhcpdv6'];
106
		foreach ($dhcpdv6 as $dhcpv6leaseinlocaltime) {
107
			$dhcpv6leaseinlocaltime = $dhcpv6leaseinlocaltime['dhcpv6leaseinlocaltime'];
108
			if ($dhcpv6leaseinlocaltime == "yes")
109
				break;
110
		}
111
	}
112

    
113
	$timezone = $config['system']['timezone'];
114
	$ts = strtotime($dt . " GMT");
115
	if ($dhcpv6leaseinlocaltime == "yes") {
116
		$this_tz = new DateTimeZone($timezone);
117
		$dhcp_lt = new DateTime(strftime("%I:%M:%S%p", $ts), $this_tz);
118
		$offset = $this_tz->getOffset($dhcp_lt);
119
		$ts = $ts + $offset;
120
		return strftime("%Y/%m/%d %I:%M:%S%p", $ts);
121
	}
122
	else
123
		return strftime("%Y/%m/%d %H:%M:%S", $ts);
124
}
125

    
126
function remove_duplicate($array, $field) {
127
	foreach ($array as $sub)
128
		$cmp[] = $sub[$field];
129
	$unique = array_unique(array_reverse($cmp,true));
130
	foreach ($unique as $k => $rien)
131
		$new[] = $array[$k];
132
	return $new;
133
}
134

    
135
function parse_duid($duid_string) {
136
	$parsed_duid = array();
137
	for ($i=0; $i < strlen($duid_string); $i++) {
138
		$s = substr($duid_string, $i, 1);
139
		if ($s == '\\') {
140
			$n = substr($duid_string, $i+1, 1);
141
			if (($n == '\\') || ($n == '"')) {
142
				$parsed_duid[] = sprintf("%02x", ord($n));
143
			} elseif (is_numeric($n)) {
144
				$parsed_duid[] = sprintf("%02x", octdec(substr($duid_string, $i+1, 3)));
145
				$i += 3;
146
			}
147
		} else {
148
			$parsed_duid[] = sprintf("%02x", ord($s));
149
		}
150
	}
151
	$iaid = array_slice($parsed_duid, 0, 4);
152
	$duid = array_slice($parsed_duid, 4);
153
	return array($iaid, $duid);
154
}
155

    
156
$awk = "/usr/bin/awk";
157

    
158
/* this pattern sticks comments into a single array item */
159
$cleanpattern = "'{ gsub(\"^#.*\", \"\");} { gsub(\"^server-duid.*\", \"\");} { gsub(\";$\", \"\"); print;}'";
160
/* We then split the leases file by } */
161
$splitpattern = "'BEGIN { RS=\"}\";} {for (i=1; i<=NF; i++) printf \"%s \", \$i; printf \"}\\n\";}'";
162

    
163
/* stuff the leases file in a proper format into a array by line */
164
exec("/bin/cat {$leasesfile} | {$awk} {$cleanpattern} | {$awk} {$splitpattern} | /usr/bin/grep '^ia-.. '", $leases_content);
165
$leases_count = count($leases_content);
166
exec("/usr/sbin/ndp -an", $rawdata);
167
$ndpdata = array();
168
foreach ($rawdata as $line) {
169
	$elements = preg_split('/\s+/ ',$line);
170
	if ($elements[1] != "(incomplete)") {
171
		$ndpent = array();
172
		$ip = trim(str_replace(array('(',')'),'',$elements[0]));
173
		$ndpent['mac'] = trim($elements[1]);
174
		$ndpent['interface'] = trim($elements[2]);
175
		$ndpdata[$ip] = $ndpent;
176
	}
177
}
178

    
179
$pools = array();
180
$leases = array();
181
$prefixes = array();
182
$mappings = array();
183
$i = 0;
184
$l = 0;
185
$p = 0;
186

    
187
// Put everything together again
188
while($i < $leases_count) {
189
	$entry = array();
190
	/* split the line by space */
191
	$duid_split = array();
192
	preg_match('/ia-.. "(.*)" { (.*)/ ', $leases_content[$i], $duid_split);
193
	if (!empty($duid_split[1])) {
194
		$iaid_duid = parse_duid($duid_split[1]);
195
		$entry['iaid'] = hexdec(implode("", array_reverse($iaid_duid[0])));
196
		$entry['duid'] = implode(":", $iaid_duid[1]);
197
		$data = explode(" ", $duid_split[2]);
198
	} else {
199
		$data = explode(" ", $leases_content[$i]);
200
	}
201
	/* walk the fields */
202
	$f = 0;
203
	$fcount = count($data);
204
	/* with less then 12 fields there is nothing useful */
205
	if($fcount < 12) {
206
		$i++;
207
		continue;
208
	}
209
	while($f < $fcount) {
210
		switch($data[$f]) {
211
			case "failover":
212
				$pools[$p]['name'] = $data[$f+2];
213
				$pools[$p]['mystate'] = $data[$f+7];
214
				$pools[$p]['peerstate'] = $data[$f+14];
215
				$pools[$p]['mydate'] = $data[$f+10];
216
				$pools[$p]['mydate'] .= " " . $data[$f+11];
217
				$pools[$p]['peerdate'] = $data[$f+17];
218
				$pools[$p]['peerdate'] .= " " . $data[$f+18];
219
				$p++;
220
				$i++;
221
				continue 3;
222
			case "ia-pd":
223
				$is_prefix = true;
224
			case "ia-na":
225
				$entry['iaid'] = $tmp_iaid;
226
				$entry['duid'] = $tmp_duid;
227
				if ($data[$f+1][0] == '"') {
228
					$duid = "";
229
					/* FIXME: This needs a safety belt to prevent an infinite loop */
230
					while ($data[$f][strlen($data[$f])-1] != '"') {
231
						$duid .= " " . $data[$f+1];
232
						$f++;
233
					}
234
					$entry['duid'] = $duid;
235
				} else {
236
					$entry['duid'] = $data[$f+1];
237
				}
238
				$entry['type'] = "dynamic";
239
				$f = $f+2;
240
				break;
241
			case "iaaddr":
242
				$entry['ip'] = $data[$f+1];
243
				$entry['type'] = "dynamic";
244
				if (in_array($entry['ip'], array_keys($ndpdata))) {
245
					$entry['online'] = 'online';
246
				} else {
247
					$entry['online'] = 'offline';
248
				}
249
				$f = $f+2;
250
				break;
251
			case "iaprefix":
252
				$is_prefix = true;
253
				$entry['prefix'] = $data[$f+1];
254
				$entry['type'] = "dynamic";
255
				$f = $f+2;
256
				break;
257
			case "starts":
258
				$entry['start'] = $data[$f+2];
259
				$entry['start'] .= " " . $data[$f+3];
260
				$f = $f+3;
261
				break;
262
			case "ends":
263
				$entry['end'] = $data[$f+2];
264
				$entry['end'] .= " " . $data[$f+3];
265
				$f = $f+3;
266
				break;
267
			case "tstp":
268
				$f = $f+3;
269
				break;
270
			case "tsfp":
271
				$f = $f+3;
272
				break;
273
			case "atsfp":
274
				$f = $f+3;
275
				break;
276
			case "cltt":
277
				$entry['start'] = $data[$f+2];
278
				$entry['start'] .= " " . $data[$f+3];
279
				$f = $f+3;
280
				break;
281
			case "binding":
282
				switch($data[$f+2]) {
283
					case "active":
284
						$entry['act'] = "active";
285
						break;
286
					case "free":
287
						$entry['act'] = "expired";
288
						$entry['online'] = "offline";
289
						break;
290
					case "backup":
291
						$entry['act'] = "reserved";
292
						$entry['online'] = "offline";
293
						break;
294
					case "released":
295
						$entry['act'] = "released";
296
						$entry['online'] = "offline";
297
				}
298
				$f = $f+1;
299
				break;
300
			case "next":
301
				/* skip the next binding statement */
302
				$f = $f+3;
303
				break;
304
			case "hardware":
305
				$f = $f+2;
306
				break;
307
			case "client-hostname":
308
				if($data[$f+1] != "") {
309
					$entry['hostname'] = preg_replace('/"/','',$data[$f+1]);
310
				} else {
311
					$hostname = gethostbyaddr($entry['ip']);
312
					if($hostname != "") {
313
						$entry['hostname'] = $hostname;
314
					}
315
				}
316
				$f = $f+1;
317
				break;
318
			case "uid":
319
				$f = $f+1;
320
				break;
321
		}
322
		$f++;
323
	}
324
	if ($is_prefix) {
325
		$prefixes[] = $entry;
326
	} else {
327
		$leases[] = $entry;
328
		$mappings[$entry['iaid'] . $entry['duid']] = $entry['ip'];
329
	}
330
	$l++;
331
	$i++;
332
	$is_prefix = false;
333
}
334

    
335
if(count($leases) > 0) {
336
	$leases = remove_duplicate($leases,"ip");
337
}
338

    
339
if(count($prefixes) > 0) {
340
	$prefixes = remove_duplicate($prefixes,"prefix");
341
}
342

    
343
if(count($pools) > 0) {
344
	$pools = remove_duplicate($pools,"name");
345
	asort($pools);
346
}
347

    
348
foreach($config['interfaces'] as $ifname => $ifarr) {
349
	if (is_array($config['dhcpdv6'][$ifname]) &&
350
		is_array($config['dhcpdv6'][$ifname]['staticmap'])) {
351
		foreach($config['dhcpdv6'][$ifname]['staticmap'] as $static) {
352
			$slease = array();
353
			$slease['ip'] = $static['ipaddrv6'];
354
			$slease['type'] = "static";
355
			$slease['duid'] = $static['duid'];
356
			$slease['start'] = "";
357
			$slease['end'] = "";
358
			$slease['hostname'] = htmlentities($static['hostname']);
359
			$slease['act'] = "static";
360
			if (in_array($slease['ip'], array_keys($ndpdata))) {
361
				$slease['online'] = 'online';
362
			} else {
363
				$slease['online'] = 'offline';
364
			}
365

    
366
			$leases[] = $slease;
367
		}
368
	}
369
}
370

    
371
if ($_GET['order'])
372
	usort($leases, "leasecmp");
373

    
374
/* only print pool status when we have one */
375
if(count($pools) > 0) {
376
?>
377
<div class="panel panel-default">
378
	<div class="panel-heading"><h2 class="panel-title"><?=gettext('Pool status')?></h2></div>
379
	<div class="panel-body">
380
		<table class="table">
381
		<thead>
382
			<tr>
383
				<th><?=gettext("Failover Group")?></a></th>
384
				<th><?=gettext("My State")?></a></th>
385
				<th><?=gettext("Since")?></a></th>
386
				<th><?=gettext("Peer State")?></a></th>
387
				<th><?=gettext("Since")?></a></th>
388
			</tr>
389
		</thead>
390
		<tbody>
391
<? foreach ($pools as $data):?>
392
			<tr>
393
				<td><?=$data['name']?></td>
394
				<td><?=$data['mystate']?></td>
395
				<td><?=adjust_gmt($data['mydate'])?></td>
396
				<td><?=$data['peerstate']?></td>
397
				<td><?=adjust_gmt($data['peerdate'])?></td>
398
			</tr>
399
<? endforeach?>
400
		</tbody>
401
		</table>
402
	</div>
403
</div>
404
<?php
405
/* only print pool status when we have one */
406
}
407

    
408
if (empty($leases))
409
	print '<div class="alert alert-warning" role="alert">'. gettext("No leases file found. Is the DHCP server active?") .'</div>';
410

    
411
?>
412
<div class="panel panel-default">
413
	<div class="panel-heading"><h2 class="panel-title"><?=gettext('Leases')?></h2></div>
414
	<div class="panel-body">
415
		<table class="table">
416
		<thead>
417
			<tr>
418
				<th><!-- icon --></th>
419
				<th><?=gettext("IPv6 address")?></th>
420
				<th><?=gettext("IAID")?></th>
421
				<th><?=gettext("DUID")?></th>
422
				<th><?=gettext("MAC address")?></th>
423
				<th><?=gettext("Hostname")?></th>
424
				<th><?=gettext("Start")?></th>
425
				<th><?=gettext("End")?></th>
426
				<th><?=gettext("Online")?></th>
427
				<th><?=gettext("Lease Type")?></th>
428
			</tr>
429
		</thead>
430
		<tbody>
431
<?php
432
foreach ($leases as $data):
433
	if ($data['act'] != "active" && $data['act'] != "static" && $_GET['all'] != 1)
434
		continue;
435

    
436
	if ($data['act'] == 'active')
437
		$icon = 'icon-ok-circle';
438
	elseif ($data['act'] == 'expired')
439
		$icon = 'icon-ban-circle';
440
	else
441
		$icon = 'icon-remove-circle';
442

    
443
	if ($data['act'] == "static") {
444
		foreach ($config['dhcpdv6'] as $dhcpif => $dhcpifconf) {
445
			if(is_array($dhcpifconf['staticmap'])) {
446
				foreach ($dhcpifconf['staticmap'] as $staticent) {
447
					if ($data['ip'] == $staticent['ipaddr']) {
448
						$data['if'] = $dhcpif;
449
						break;
450
					}
451
				}
452
			}
453
			/* exit as soon as we have an interface */
454
			if ($data['if'] != "")
455
				break;
456
		}
457
	} else {
458
		$data['if'] = convert_real_interface_to_friendly_interface_name(guess_interface_from_ip($data['ip']));
459
	}
460

    
461
	$mac = trim($ndpdata[$data['ip']]['mac']);
462
	$mac_hi = strtoupper($mac[0] . $mac[1] . $mac[3] . $mac[4] . $mac[6] . $mac[7]);
463
?>
464
			<tr>
465
				<td><i class="icon <?=$icon?>"></i></td>
466
				<td><?=$data['ip']?></td>
467
				<td><?=$data['iaid']?></td>
468
				<td><?=$data['duid']?></td>
469
				<td>
470
					<?=$mac?>
471

    
472
					<? if(isset($mac_man[$mac_hi])):?>
473
						(<?=$mac_man[$mac_hi]?>)
474
					<?endif?>
475
				</td>
476
				<td><?=htmlentities($data['hostname'])?></td>
477
<? if ($data['type'] != "static"):?>
478
				<td><?=adjust_gmt($data['start'])?></td>
479
				<td><?=adjust_gmt($data['end'])?></td>
480
<? else: ?>
481
				<td>n/a</td>
482
				<td>n/a</td>
483
<? endif; ?>
484
				<td><?=$data['online']?></td>
485
				<td><?=$data['act']?></td>
486
				<td>
487
<? if ($data['type'] == "dynamic"): ?>
488
					<a class="btn btn-xs btn-primary" href="services_dhcpv6_edit.php?if=<?=$data['if']?>&amp;duid=<?=$data['duid']?>&amp;hostname=<?=htmlspecialchars($data['hostname'])?>">
489
						<?=gettext("add static mapping")?>
490
					</a>
491
<? endif; ?>
492

    
493
					<a class="btn btn-xs btn-success" href="services_wol_edit.php?if=<?=$data['if']?>&amp;mac=<?=$data['mac']?>&amp;descr=<?=htmlentities($data['hostname'])?>">
494
						add WOL mapping
495
					</a>
496

    
497
<? if ($data['type'] == "dynamic" && $data['online'] != "online"):?>
498
					<a class="btn btn-xs btn-danger" href="status_dhcpv6_leases.php?deleteip=<?=$data['ip']?>&amp;all=<?=intval($_GET['all'])?>">
499
						delete lease
500
					</a>
501
<? endif?>
502
				</td>
503
<? endforeach; ?>
504
			</tr>
505
		</tbody>
506
		</table>
507
	</div>
508
</div>
509

    
510
<div class="panel panel-default">
511
	<div class="panel-heading"><h2 class="panel-title"><?=gettext('Delegated Prefixes')?></h2></div>
512
	<div class="panel-body">
513
		<table class="table">
514
		<thead>
515
			<tr>
516
				<th><?=gettext("IPv6 Prefix")?></th>
517
				<th><?=gettext("IAID")?></th>
518
				<th><?=gettext("DUID")?></th>
519
				<th><?=gettext("Start")?></th>
520
				<th><?=gettext("End")?></th>
521
				<th><?=gettext("State")?></th>
522
			</tr>
523
		</thead>
524
		<tbody>
525
<?php
526
foreach ($prefixes as $data):
527
	if ($data['act'] != "active" && $data['act'] != "static" && $_GET['all'] != 1)
528
		continue;
529

    
530
	if ($data['act'] == 'active')
531
		$icon = 'icon-ok-circle';
532
	elseif ($data['act'] == 'expired')
533
		$icon = 'icon-ban-circle';
534
	else
535
		$icon = 'icon-remove-circle';
536

    
537
	if ($data['act'] == "static") {
538
		foreach ($config['dhcpdv6'] as $dhcpif => $dhcpifconf) {
539
			if(is_array($dhcpifconf['staticmap'])) {
540
				foreach ($dhcpifconf['staticmap'] as $staticent) {
541
					if ($data['ip'] == $staticent['ipaddr']) {
542
						$data['if'] = $dhcpif;
543
						break;
544
					}
545
				}
546
			}
547
			/* exit as soon as we have an interface */
548
			if ($data['if'] != "")
549
				break;
550
		}
551
	} else {
552
		$data['if'] = convert_real_interface_to_friendly_interface_name(guess_interface_from_ip($data['ip']));
553
	}
554

    
555
	 {
556
		$dip = "";
557
	}
558
?>
559
			<tr>
560
				<td><i class="icon <?=$icon?>"></i></td>
561
				<td>
562
					<?=$data['prefix']?>
563
<? if ($mappings[$data['iaid'] . $data['duid']]): ?>
564
					<br />
565
					<?=gettext('Routed To')?>: <?=$mappings[$data['iaid'] . $data['duid']]?>
566
<? endif; ?>
567
				</td>
568
				<td><?=$data['iaid']?></td>
569
				<td><?=$data['duid']?></td>
570
<? if ($data['type'] != "static"):?>
571
				<td><?=adjust_gmt($data['start'])?></td>
572
				<td><?=adjust_gmt($data['end'])?></td>
573
<? else: ?>
574
				<td>n/a</td>
575
				<td>n/a</td>
576
<? endif; ?>
577
				<td><?=$data['act']?></td>
578
<? endforeach; ?>
579
			</tr>
580
		</tbody>
581
		</table>
582
	</div>
583
</div>
584

    
585
<?php if ($_GET['all']): ?>
586
	<a class="btn btn-default" href="status_dhcpv6_leases.php?all=0"><?=gettext("Show active and static leases only")?></a>
587
<?php else: ?>
588
	<a class="btn btn-default" href="status_dhcpv6_leases.php?all=1"><?=gettext("Show all configured leases")?></a>
589
<?php endif;
590

    
591
include("foot.inc");
(172-172/241)