Project

General

Profile

Download (3.36 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2

    
3
$leases_file = "/var/dhcpd/var/db/dhcpd6.leases";
4
if (!file_exists($leases_file)) {
5
	exit(1);
6
}
7

    
8
$fd = fopen($leases_file, 'r');
9

    
10
$duid_arr = array();
11
while (( $line = fgets($fd, 4096)) !== false) {
12
	// echo "$line";
13
	
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
		$type = $duidmatch[1];
28
		$duid = extract_duid($duidmatch[2]);
29
		continue;
30
	}
31

    
32
	/* is it active? otherwise just discard */
33
	if (preg_match("/binding state active/i", $line, $activematch)) {
34
		$active = true;
35
		continue;
36
	}
37

    
38
	if (preg_match("/iaaddr[ ]+([0-9a-f:]+)[ ]+/i", $line, $addressmatch)) {
39
		$ia_na = $addressmatch[1];
40
		continue;
41
	}
42

    
43
	if (preg_match("/iaprefix[ ]+([0-9a-f:\/]+)[ ]+/i", $line, $prefixmatch)) {
44
		$ia_pd = $prefixmatch[1];
45
		continue;
46
	}
47

    
48
	/* closing bracket */
49
	if (preg_match("/^}/i", $line)) {
50
		if (isset($duid) && $duid !== false && $active === true) {
51
			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
		}
62
		unset($type);
63
		unset($duid);
64
		unset($active);
65
		unset($ia_na);
66
		unset($ia_pd);
67
		continue;
68
	}
69
}
70
fclose($fd);
71

    
72
$routes = array();
73
foreach ($duid_arr as $entry) {
74
	if (!empty($entry['ia-pd'])) {
75
		$routes[$entry['ia-na']] = $entry['ia-pd'];
76
	}
77
}
78

    
79
// echo "add routes\n";
80
if (count($routes) > 0) {
81
	foreach ($routes as $address => $prefix) {
82
		echo "/sbin/route change -inet6 {$prefix} {$address}\n";
83
	}
84
}
85

    
86
/* get clog from dhcpd */
87
$dhcpdlogfile = "/var/log/dhcpd.log";
88
$expires = array();
89
if (file_exists($dhcpdlogfile)) {
90
	$fd = popen("clog $dhcpdlogfile", 'r');
91
	while (($line = fgets($fd)) !== false) {
92
		//echo $line;
93
		if (preg_match("/releases[ ]+prefix[ ]+([0-9a-f:]+\/[0-9]+)/i", $line, $expire)) {
94
			if (in_array($expire[1], $routes)) {
95
				continue;
96
			}
97
			$expires[$expire[1]] = $expire[1];
98
		}
99
	}
100
	pclose($fd);
101
}
102

    
103
// echo "remove routes\n";
104
if (count($expires) > 0) {
105
	foreach ($expires as $prefix) {
106
		echo "/sbin/route delete -inet6 {$prefix['prefix']}\n";
107
	}
108
}
109

    
110
/* 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
				if (preg_match('/[0-3][0-7]{2}/', substr($ia_string, $i+1, 3))) {
123
					$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
?>
(16-16/22)