1 |
092462dc
|
smos
|
<?php
|
2 |
|
|
|
3 |
|
|
$leases_file = "/var/dhcpd/var/db/dhcpd6.leases";
|
4 |
7d61beba
|
Phil Davis
|
if (!file_exists($leases_file)) {
|
5 |
8dfb0c00
|
smos
|
exit(1);
|
6 |
|
|
}
|
7 |
092462dc
|
smos
|
|
8 |
74fe0ef9
|
Ermal LUÇI
|
$fd = fopen($leases_file, 'r');
|
9 |
|
|
|
10 |
2abfec49
|
Renato Botelho
|
$duid_arr = array();
|
11 |
74fe0ef9
|
Ermal LUÇI
|
while (( $line = fgets($fd, 4096)) !== false) {
|
12 |
092462dc
|
smos
|
// echo "$line";
|
13 |
2caea6a2
|
Anders Lind
|
|
14 |
|
|
/* Originally: preg_match("/^(ia-[np][ad])[ ]+\"(.*?)\"/i", $line, $duidmatch)
|
15 |
|
|
That is: \"(.*?)\"
|
16 |
|
|
, which is a non-greedy matching. However that does not go well with the legal
|
17 |
|
|
substring \" in the IAID+DUID lease string format of ISC DHCPDv6,
|
18 |
|
|
because it truncates before we reach the end of the IAID+DUID string!
|
19 |
|
|
Instead we use: \"(.*)\"
|
20 |
|
|
(Might fail if content of the lease file is not well formed.)
|
21 |
|
|
|
22 |
|
|
Maybe someone would argue to e.g. use \"(.*?)\"[ \t]*{[ \t]*$
|
23 |
|
|
instead
|
24 |
|
|
(Either we get a valid result or nothing at all.)
|
25 |
|
|
, but I'll leave it to others to decide! */
|
26 |
|
|
if (preg_match("/^(ia-[np][ad])[ ]+\"(.*)\"/i", $line, $duidmatch)) {
|
27 |
092462dc
|
smos
|
$type = $duidmatch[1];
|
28 |
2caea6a2
|
Anders Lind
|
$duid = extract_duid($duidmatch[2]);
|
29 |
092462dc
|
smos
|
continue;
|
30 |
|
|
}
|
31 |
|
|
|
32 |
|
|
/* is it active? otherwise just discard */
|
33 |
7d61beba
|
Phil Davis
|
if (preg_match("/binding state active/i", $line, $activematch)) {
|
34 |
092462dc
|
smos
|
$active = true;
|
35 |
|
|
continue;
|
36 |
|
|
}
|
37 |
|
|
|
38 |
7d61beba
|
Phil Davis
|
if (preg_match("/iaaddr[ ]+([0-9a-f:]+)[ ]+/i", $line, $addressmatch)) {
|
39 |
092462dc
|
smos
|
$ia_na = $addressmatch[1];
|
40 |
|
|
continue;
|
41 |
|
|
}
|
42 |
|
|
|
43 |
7d61beba
|
Phil Davis
|
if (preg_match("/iaprefix[ ]+([0-9a-f:\/]+)[ ]+/i", $line, $prefixmatch)) {
|
44 |
092462dc
|
smos
|
$ia_pd = $prefixmatch[1];
|
45 |
|
|
continue;
|
46 |
|
|
}
|
47 |
|
|
|
48 |
|
|
/* closing bracket */
|
49 |
7d61beba
|
Phil Davis
|
if (preg_match("/^}/i", $line)) {
|
50 |
ba045ef1
|
Anders Lind
|
if (isset($duid) && $duid !== false && $active === true) {
|
51 |
2caea6a2
|
Anders Lind
|
switch ($type) {
|
52 |
|
|
case "ia-na":
|
53 |
|
|
$duid_arr[$duid][$type] = $ia_na;
|
54 |
|
|
break;
|
55 |
|
|
case "ia-pd":
|
56 |
|
|
$duid_arr[$duid][$type] = $ia_pd;
|
57 |
|
|
break;
|
58 |
|
|
default:
|
59 |
|
|
break;
|
60 |
|
|
}
|
61 |
092462dc
|
smos
|
}
|
62 |
|
|
unset($type);
|
63 |
|
|
unset($duid);
|
64 |
|
|
unset($active);
|
65 |
|
|
unset($ia_na);
|
66 |
|
|
unset($ia_pd);
|
67 |
|
|
continue;
|
68 |
|
|
}
|
69 |
|
|
}
|
70 |
74fe0ef9
|
Ermal LUÇI
|
fclose($fd);
|
71 |
092462dc
|
smos
|
|
72 |
|
|
$routes = array();
|
73 |
|
|
foreach ($duid_arr as $entry) {
|
74 |
7d61beba
|
Phil Davis
|
if (!empty($entry['ia-pd'])) {
|
75 |
092462dc
|
smos
|
$routes[$entry['ia-na']] = $entry['ia-pd'];
|
76 |
|
|
}
|
77 |
|
|
}
|
78 |
|
|
|
79 |
|
|
// echo "add routes\n";
|
80 |
7d61beba
|
Phil Davis
|
if (count($routes) > 0) {
|
81 |
69b6c2b5
|
smos
|
foreach ($routes as $address => $prefix) {
|
82 |
|
|
echo "/sbin/route change -inet6 {$prefix} {$address}\n";
|
83 |
|
|
}
|
84 |
092462dc
|
smos
|
}
|
85 |
|
|
|
86 |
|
|
/* get clog from dhcpd */
|
87 |
|
|
$dhcpdlogfile = "/var/log/dhcpd.log";
|
88 |
|
|
$expires = array();
|
89 |
7d61beba
|
Phil Davis
|
if (file_exists($dhcpdlogfile)) {
|
90 |
74fe0ef9
|
Ermal LUÇI
|
$fd = popen("clog $dhcpdlogfile", 'r');
|
91 |
|
|
while (($line = fgets($fd)) !== false) {
|
92 |
|
|
//echo $line;
|
93 |
7d61beba
|
Phil Davis
|
if (preg_match("/releases[ ]+prefix[ ]+([0-9a-f:]+\/[0-9]+)/i", $line, $expire)) {
|
94 |
|
|
if (in_array($expire[1], $routes)) {
|
95 |
74fe0ef9
|
Ermal LUÇI
|
continue;
|
96 |
7d61beba
|
Phil Davis
|
}
|
97 |
74fe0ef9
|
Ermal LUÇI
|
$expires[$expire[1]] = $expire[1];
|
98 |
|
|
}
|
99 |
092462dc
|
smos
|
}
|
100 |
74fe0ef9
|
Ermal LUÇI
|
pclose($fd);
|
101 |
092462dc
|
smos
|
}
|
102 |
|
|
|
103 |
|
|
// echo "remove routes\n";
|
104 |
7d61beba
|
Phil Davis
|
if (count($expires) > 0) {
|
105 |
69b6c2b5
|
smos
|
foreach ($expires as $prefix) {
|
106 |
|
|
echo "/sbin/route delete -inet6 {$prefix['prefix']}\n";
|
107 |
|
|
}
|
108 |
092462dc
|
smos
|
}
|
109 |
|
|
|
110 |
2caea6a2
|
Anders Lind
|
/* handle quotify_buf - https://source.isc.org/cgi-bin/gitweb.cgi?p=dhcp.git;a=blob;f=common/print.c */
|
111 |
|
|
function extract_duid($ia_string) {
|
112 |
|
|
for ($i = 0, $iaid_counter = 0, $len = strlen($ia_string); $i < $len && $iaid_counter < 4; $i++, $iaid_counter++) {
|
113 |
|
|
if ($ia_string[$i] !== '\\') {
|
114 |
|
|
continue;
|
115 |
|
|
}
|
116 |
|
|
else if ($len - $i >= 2) {
|
117 |
|
|
if (($ia_string[$i+1] === '\\') || ($ia_string[$i+1] === '"')) {
|
118 |
|
|
$i += 1;
|
119 |
|
|
continue;
|
120 |
|
|
}
|
121 |
|
|
else if ($len - $i >= 4) {
|
122 |
5e422c88
|
Anders Lind
|
if (preg_match('/[0-3][0-7]{2}/', substr($ia_string, $i+1, 3))) {
|
123 |
2caea6a2
|
Anders Lind
|
$i += 3;
|
124 |
|
|
continue;
|
125 |
|
|
}
|
126 |
|
|
}
|
127 |
|
|
}
|
128 |
|
|
|
129 |
|
|
return false;
|
130 |
|
|
}
|
131 |
|
|
|
132 |
|
|
/* Return anything after the first 4 octets! */
|
133 |
|
|
if ($iaid_counter === 4) {
|
134 |
|
|
/* substr returns false when $len == $i */
|
135 |
|
|
return substr($ia_string, $i);
|
136 |
|
|
}
|
137 |
|
|
|
138 |
|
|
return false;
|
139 |
|
|
}
|
140 |
|
|
|
141 |
092462dc
|
smos
|
?>
|