1 |
092462dc
|
smos
|
<?php
|
2 |
ac24dc24
|
Renato Botelho
|
/*
|
3 |
|
|
* prefixes.php
|
4 |
|
|
*
|
5 |
|
|
* part of pfSense (https://www.pfsense.org)
|
6 |
b8f91b7c
|
Luiz Souza
|
* Copyright (c) 2004-2018 Rubicon Communications, LLC (Netgate)
|
7 |
ac24dc24
|
Renato Botelho
|
* All rights reserved.
|
8 |
|
|
*
|
9 |
b12ea3fb
|
Renato Botelho
|
* 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 |
ac24dc24
|
Renato Botelho
|
*
|
13 |
b12ea3fb
|
Renato Botelho
|
* http://www.apache.org/licenses/LICENSE-2.0
|
14 |
ac24dc24
|
Renato Botelho
|
*
|
15 |
b12ea3fb
|
Renato Botelho
|
* 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 |
ac24dc24
|
Renato Botelho
|
*/
|
21 |
092462dc
|
smos
|
|
22 |
|
|
$leases_file = "/var/dhcpd/var/db/dhcpd6.leases";
|
23 |
7d61beba
|
Phil Davis
|
if (!file_exists($leases_file)) {
|
24 |
8dfb0c00
|
smos
|
exit(1);
|
25 |
|
|
}
|
26 |
092462dc
|
smos
|
|
27 |
74fe0ef9
|
Ermal LUÇI
|
$fd = fopen($leases_file, 'r');
|
28 |
|
|
|
29 |
2abfec49
|
Renato Botelho
|
$duid_arr = array();
|
30 |
74fe0ef9
|
Ermal LUÇI
|
while (( $line = fgets($fd, 4096)) !== false) {
|
31 |
092462dc
|
smos
|
// echo "$line";
|
32 |
179377b0
|
robjarsen
|
|
33 |
2caea6a2
|
Anders Lind
|
/* 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 |
092462dc
|
smos
|
$type = $duidmatch[1];
|
47 |
2caea6a2
|
Anders Lind
|
$duid = extract_duid($duidmatch[2]);
|
48 |
092462dc
|
smos
|
continue;
|
49 |
|
|
}
|
50 |
|
|
|
51 |
|
|
/* is it active? otherwise just discard */
|
52 |
7d61beba
|
Phil Davis
|
if (preg_match("/binding state active/i", $line, $activematch)) {
|
53 |
092462dc
|
smos
|
$active = true;
|
54 |
|
|
continue;
|
55 |
|
|
}
|
56 |
|
|
|
57 |
7d61beba
|
Phil Davis
|
if (preg_match("/iaaddr[ ]+([0-9a-f:]+)[ ]+/i", $line, $addressmatch)) {
|
58 |
092462dc
|
smos
|
$ia_na = $addressmatch[1];
|
59 |
|
|
continue;
|
60 |
|
|
}
|
61 |
|
|
|
62 |
7d61beba
|
Phil Davis
|
if (preg_match("/iaprefix[ ]+([0-9a-f:\/]+)[ ]+/i", $line, $prefixmatch)) {
|
63 |
092462dc
|
smos
|
$ia_pd = $prefixmatch[1];
|
64 |
|
|
continue;
|
65 |
|
|
}
|
66 |
|
|
|
67 |
|
|
/* closing bracket */
|
68 |
7d61beba
|
Phil Davis
|
if (preg_match("/^}/i", $line)) {
|
69 |
ba045ef1
|
Anders Lind
|
if (isset($duid) && $duid !== false && $active === true) {
|
70 |
2caea6a2
|
Anders Lind
|
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 |
092462dc
|
smos
|
}
|
81 |
|
|
unset($type);
|
82 |
|
|
unset($duid);
|
83 |
|
|
unset($active);
|
84 |
|
|
unset($ia_na);
|
85 |
|
|
unset($ia_pd);
|
86 |
|
|
continue;
|
87 |
|
|
}
|
88 |
|
|
}
|
89 |
74fe0ef9
|
Ermal LUÇI
|
fclose($fd);
|
90 |
092462dc
|
smos
|
|
91 |
|
|
$routes = array();
|
92 |
|
|
foreach ($duid_arr as $entry) {
|
93 |
7d61beba
|
Phil Davis
|
if (!empty($entry['ia-pd'])) {
|
94 |
092462dc
|
smos
|
$routes[$entry['ia-na']] = $entry['ia-pd'];
|
95 |
|
|
}
|
96 |
|
|
}
|
97 |
|
|
|
98 |
|
|
// echo "add routes\n";
|
99 |
7d61beba
|
Phil Davis
|
if (count($routes) > 0) {
|
100 |
69b6c2b5
|
smos
|
foreach ($routes as $address => $prefix) {
|
101 |
94bd7fb3
|
Renato Botelho
|
echo "/sbin/route change -inet6 {$prefix} {$address} " .
|
102 |
|
|
"|| /sbin/route add -inet6 {$prefix} {$address}\n";
|
103 |
69b6c2b5
|
smos
|
}
|
104 |
092462dc
|
smos
|
}
|
105 |
|
|
|
106 |
|
|
/* get clog from dhcpd */
|
107 |
|
|
$dhcpdlogfile = "/var/log/dhcpd.log";
|
108 |
|
|
$expires = array();
|
109 |
7d61beba
|
Phil Davis
|
if (file_exists($dhcpdlogfile)) {
|
110 |
74fe0ef9
|
Ermal LUÇI
|
$fd = popen("clog $dhcpdlogfile", 'r');
|
111 |
|
|
while (($line = fgets($fd)) !== false) {
|
112 |
|
|
//echo $line;
|
113 |
7d61beba
|
Phil Davis
|
if (preg_match("/releases[ ]+prefix[ ]+([0-9a-f:]+\/[0-9]+)/i", $line, $expire)) {
|
114 |
|
|
if (in_array($expire[1], $routes)) {
|
115 |
74fe0ef9
|
Ermal LUÇI
|
continue;
|
116 |
7d61beba
|
Phil Davis
|
}
|
117 |
74fe0ef9
|
Ermal LUÇI
|
$expires[$expire[1]] = $expire[1];
|
118 |
|
|
}
|
119 |
092462dc
|
smos
|
}
|
120 |
74fe0ef9
|
Ermal LUÇI
|
pclose($fd);
|
121 |
092462dc
|
smos
|
}
|
122 |
|
|
|
123 |
|
|
// echo "remove routes\n";
|
124 |
7d61beba
|
Phil Davis
|
if (count($expires) > 0) {
|
125 |
69b6c2b5
|
smos
|
foreach ($expires as $prefix) {
|
126 |
|
|
echo "/sbin/route delete -inet6 {$prefix['prefix']}\n";
|
127 |
|
|
}
|
128 |
092462dc
|
smos
|
}
|
129 |
|
|
|
130 |
2caea6a2
|
Anders Lind
|
/* 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 |
5e422c88
|
Anders Lind
|
if (preg_match('/[0-3][0-7]{2}/', substr($ia_string, $i+1, 3))) {
|
143 |
2caea6a2
|
Anders Lind
|
$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 |
092462dc
|
smos
|
?>
|