1
|
<?php
|
2
|
/*
|
3
|
* prefixes.php
|
4
|
*
|
5
|
* part of pfSense (https://www.pfsense.org)
|
6
|
* Copyright (c) 2004-2016 Rubicon Communications, LLC (Netgate)
|
7
|
* All rights reserved.
|
8
|
*
|
9
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
10
|
* you may not use this file except in compliance with the License.
|
11
|
* You may obtain a copy of the License at
|
12
|
*
|
13
|
* http://www.apache.org/licenses/LICENSE-2.0
|
14
|
*
|
15
|
* Unless required by applicable law or agreed to in writing, software
|
16
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
17
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
18
|
* See the License for the specific language governing permissions and
|
19
|
* limitations under the License.
|
20
|
*/
|
21
|
|
22
|
$leases_file = "/var/dhcpd/var/db/dhcpd6.leases";
|
23
|
if (!file_exists($leases_file)) {
|
24
|
exit(1);
|
25
|
}
|
26
|
|
27
|
$fd = fopen($leases_file, 'r');
|
28
|
|
29
|
$duid_arr = array();
|
30
|
while (( $line = fgets($fd, 4096)) !== false) {
|
31
|
// echo "$line";
|
32
|
|
33
|
/* Originally: preg_match("/^(ia-[np][ad])[ ]+\"(.*?)\"/i", $line, $duidmatch)
|
34
|
That is: \"(.*?)\"
|
35
|
, which is a non-greedy matching. However that does not go well with the legal
|
36
|
substring \" in the IAID+DUID lease string format of ISC DHCPDv6,
|
37
|
because it truncates before we reach the end of the IAID+DUID string!
|
38
|
Instead we use: \"(.*)\"
|
39
|
(Might fail if content of the lease file is not well formed.)
|
40
|
|
41
|
Maybe someone would argue to e.g. use \"(.*?)\"[ \t]*{[ \t]*$
|
42
|
instead
|
43
|
(Either we get a valid result or nothing at all.)
|
44
|
, but I'll leave it to others to decide! */
|
45
|
if (preg_match("/^(ia-[np][ad])[ ]+\"(.*)\"/i", $line, $duidmatch)) {
|
46
|
$type = $duidmatch[1];
|
47
|
$duid = extract_duid($duidmatch[2]);
|
48
|
continue;
|
49
|
}
|
50
|
|
51
|
/* is it active? otherwise just discard */
|
52
|
if (preg_match("/binding state active/i", $line, $activematch)) {
|
53
|
$active = true;
|
54
|
continue;
|
55
|
}
|
56
|
|
57
|
if (preg_match("/iaaddr[ ]+([0-9a-f:]+)[ ]+/i", $line, $addressmatch)) {
|
58
|
$ia_na = $addressmatch[1];
|
59
|
continue;
|
60
|
}
|
61
|
|
62
|
if (preg_match("/iaprefix[ ]+([0-9a-f:\/]+)[ ]+/i", $line, $prefixmatch)) {
|
63
|
$ia_pd = $prefixmatch[1];
|
64
|
continue;
|
65
|
}
|
66
|
|
67
|
/* closing bracket */
|
68
|
if (preg_match("/^}/i", $line)) {
|
69
|
if (isset($duid) && $duid !== false && $active === true) {
|
70
|
switch ($type) {
|
71
|
case "ia-na":
|
72
|
$duid_arr[$duid][$type] = $ia_na;
|
73
|
break;
|
74
|
case "ia-pd":
|
75
|
$duid_arr[$duid][$type] = $ia_pd;
|
76
|
break;
|
77
|
default:
|
78
|
break;
|
79
|
}
|
80
|
}
|
81
|
unset($type);
|
82
|
unset($duid);
|
83
|
unset($active);
|
84
|
unset($ia_na);
|
85
|
unset($ia_pd);
|
86
|
continue;
|
87
|
}
|
88
|
}
|
89
|
fclose($fd);
|
90
|
|
91
|
$routes = array();
|
92
|
foreach ($duid_arr as $entry) {
|
93
|
if (!empty($entry['ia-pd'])) {
|
94
|
$routes[$entry['ia-na']] = $entry['ia-pd'];
|
95
|
}
|
96
|
}
|
97
|
|
98
|
// echo "add routes\n";
|
99
|
if (count($routes) > 0) {
|
100
|
foreach ($routes as $address => $prefix) {
|
101
|
echo "/sbin/route change -inet6 {$prefix} {$address} " .
|
102
|
"|| /sbin/route add -inet6 {$prefix} {$address}\n";
|
103
|
}
|
104
|
}
|
105
|
|
106
|
/* get clog from dhcpd */
|
107
|
$dhcpdlogfile = "/var/log/dhcpd.log";
|
108
|
$expires = array();
|
109
|
if (file_exists($dhcpdlogfile)) {
|
110
|
$fd = popen("clog $dhcpdlogfile", 'r');
|
111
|
while (($line = fgets($fd)) !== false) {
|
112
|
//echo $line;
|
113
|
if (preg_match("/releases[ ]+prefix[ ]+([0-9a-f:]+\/[0-9]+)/i", $line, $expire)) {
|
114
|
if (in_array($expire[1], $routes)) {
|
115
|
continue;
|
116
|
}
|
117
|
$expires[$expire[1]] = $expire[1];
|
118
|
}
|
119
|
}
|
120
|
pclose($fd);
|
121
|
}
|
122
|
|
123
|
// echo "remove routes\n";
|
124
|
if (count($expires) > 0) {
|
125
|
foreach ($expires as $prefix) {
|
126
|
echo "/sbin/route delete -inet6 {$prefix['prefix']}\n";
|
127
|
}
|
128
|
}
|
129
|
|
130
|
/* handle quotify_buf - https://source.isc.org/cgi-bin/gitweb.cgi?p=dhcp.git;a=blob;f=common/print.c */
|
131
|
function extract_duid($ia_string) {
|
132
|
for ($i = 0, $iaid_counter = 0, $len = strlen($ia_string); $i < $len && $iaid_counter < 4; $i++, $iaid_counter++) {
|
133
|
if ($ia_string[$i] !== '\\') {
|
134
|
continue;
|
135
|
}
|
136
|
else if ($len - $i >= 2) {
|
137
|
if (($ia_string[$i+1] === '\\') || ($ia_string[$i+1] === '"')) {
|
138
|
$i += 1;
|
139
|
continue;
|
140
|
}
|
141
|
else if ($len - $i >= 4) {
|
142
|
if (preg_match('/[0-3][0-7]{2}/', substr($ia_string, $i+1, 3))) {
|
143
|
$i += 3;
|
144
|
continue;
|
145
|
}
|
146
|
}
|
147
|
}
|
148
|
|
149
|
return false;
|
150
|
}
|
151
|
|
152
|
/* Return anything after the first 4 octets! */
|
153
|
if ($iaid_counter === 4) {
|
154
|
/* substr returns false when $len == $i */
|
155
|
return substr($ia_string, $i);
|
156
|
}
|
157
|
|
158
|
return false;
|
159
|
}
|
160
|
|
161
|
?>
|