Project

General

Profile

Download (4.29 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
 * prefixes.php
4
 *
5
 * part of pfSense (https://www.pfsense.org)
6
 * Copyright (c) 2004-2013 BSD Perimeter
7
 * Copyright (c) 2013-2016 Electric Sheep Fencing
8
 * Copyright (c) 2014-2019 Rubicon Communications, LLC (Netgate)
9
 * All rights reserved.
10
 *
11
 * Licensed under the Apache License, Version 2.0 (the "License");
12
 * you may not use this file except in compliance with the License.
13
 * You may obtain a copy of the License at
14
 *
15
 * http://www.apache.org/licenses/LICENSE-2.0
16
 *
17
 * Unless required by applicable law or agreed to in writing, software
18
 * distributed under the License is distributed on an "AS IS" BASIS,
19
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20
 * See the License for the specific language governing permissions and
21
 * limitations under the License.
22
 */
23

    
24
require_once('system.inc');
25

    
26
$leases_file = "/var/dhcpd/var/db/dhcpd6.leases";
27
if (!file_exists($leases_file)) {
28
	exit(1);
29
}
30

    
31
$fd = fopen($leases_file, 'r');
32

    
33
$duid_arr = array();
34
while (( $line = fgets($fd, 4096)) !== false) {
35
	// echo "$line";
36

    
37
	/* Originally: preg_match("/^(ia-[np][ad])[ ]+\"(.*?)\"/i", $line, $duidmatch)
38
	   That is: \"(.*?)\"
39
	   , which is a non-greedy matching. However that does not go well with the legal
40
	   substring \" in the IAID+DUID lease string format of ISC DHCPDv6,
41
	   because it truncates before we reach the end of the IAID+DUID string!
42
	   Instead we use: \"(.*)\"
43
	   (Might fail if content of the lease file is not well formed.)
44

    
45
	   Maybe someone would argue to e.g. use \"(.*?)\"[ \t]*{[ \t]*$
46
	   instead
47
	   (Either we get a valid result or nothing at all.)
48
	   , but I'll leave it to others to decide! */
49
	if (preg_match("/^(ia-[np][ad])[ ]+\"(.*)\"/i", $line, $duidmatch)) {
50
		$type = $duidmatch[1];
51
		$duid = extract_duid($duidmatch[2]);
52
		continue;
53
	}
54

    
55
	/* is it active? otherwise just discard */
56
	if (preg_match("/binding state active/i", $line, $activematch)) {
57
		$active = true;
58
		continue;
59
	}
60

    
61
	if (preg_match("/iaaddr[ ]+([0-9a-f:]+)[ ]+/i", $line, $addressmatch)) {
62
		$ia_na = $addressmatch[1];
63
		continue;
64
	}
65

    
66
	if (preg_match("/iaprefix[ ]+([0-9a-f:\/]+)[ ]+/i", $line, $prefixmatch)) {
67
		$ia_pd = $prefixmatch[1];
68
		continue;
69
	}
70

    
71
	/* closing bracket */
72
	if (preg_match("/^}/i", $line)) {
73
		if (isset($duid) && $duid !== false && $active === true) {
74
			switch ($type) {
75
				case "ia-na":
76
					$duid_arr[$duid][$type] = $ia_na;
77
					break;
78
				case "ia-pd":
79
					$duid_arr[$duid][$type] = $ia_pd;
80
					break;
81
				default:
82
					break;
83
			}
84
		}
85
		unset($type);
86
		unset($duid);
87
		unset($active);
88
		unset($ia_na);
89
		unset($ia_pd);
90
		continue;
91
	}
92
}
93
fclose($fd);
94

    
95
$routes = array();
96
foreach ($duid_arr as $entry) {
97
	if (!empty($entry['ia-pd'])) {
98
		$routes[$entry['ia-na']] = $entry['ia-pd'];
99
	}
100
}
101

    
102
// echo "add routes\n";
103
if (count($routes) > 0) {
104
	foreach ($routes as $address => $prefix) {
105
		echo "/sbin/route change -inet6 {$prefix} {$address} " .
106
		    "|| /sbin/route add -inet6 {$prefix} {$address}\n";
107
	}
108
}
109

    
110
/* get log from dhcpd */
111
$dhcpdlogfile = "/var/log/dhcpd.log";
112
$expires = array();
113
if (file_exists($dhcpdlogfile)) {
114
	$fd = popen(system_log_get_cat() . ' ' . sort_related_log_files($dhcpdlogfile, true, true), 'r');
115
	while (($line = fgets($fd)) !== false) {
116
		//echo $line;
117
		if (preg_match("/releases[ ]+prefix[ ]+([0-9a-f:]+\/[0-9]+)/i", $line, $expire)) {
118
			if (in_array($expire[1], $routes)) {
119
				continue;
120
			}
121
			$expires[$expire[1]] = $expire[1];
122
		}
123
	}
124
	pclose($fd);
125
}
126

    
127
// echo "remove routes\n";
128
if (count($expires) > 0) {
129
	foreach ($expires as $prefix) {
130
		echo "/sbin/route delete -inet6 {$prefix['prefix']}\n";
131
	}
132
}
133

    
134
/* handle quotify_buf - https://source.isc.org/cgi-bin/gitweb.cgi?p=dhcp.git;a=blob;f=common/print.c */
135
function extract_duid($ia_string) {
136
	for ($i = 0, $iaid_counter = 0, $len = strlen($ia_string); $i < $len && $iaid_counter < 4; $i++, $iaid_counter++) {
137
		if ($ia_string[$i] !== '\\') {
138
			continue;
139
		}
140
		else if ($len - $i >= 2) {
141
			if (($ia_string[$i+1] === '\\') || ($ia_string[$i+1] === '"')) {
142
				$i += 1;
143
				continue;
144
			}
145
			else if ($len - $i >= 4) {
146
				if (preg_match('/[0-3][0-7]{2}/', substr($ia_string, $i+1, 3))) {
147
					$i += 3;
148
					continue;
149
				}
150
			}
151
		}
152

    
153
		return false;
154
	}
155

    
156
	/* Return anything after the first 4 octets! */
157
	if ($iaid_counter === 4) {
158
		/* substr returns false when $len == $i */
159
		return substr($ia_string, $i);
160
	}
161

    
162
	return false;
163
}
164

    
165
?>
(21-21/31)