Project

General

Profile

Download (10.1 KB) Statistics
| Branch: | Tag: | Revision:
1 4d875b4f Scott Ullrich
<?php
2 b46bfcf5 Bill Marquette
/* $Id$ */
3 5b237745 Scott Ullrich
/*
4
	diag_dhcp_leases.php
5 4d875b4f Scott Ullrich
	Copyright (C) 2004 Scott Ullrich
6 5b237745 Scott Ullrich
	All rights reserved.
7 4d875b4f Scott Ullrich
8
	originially part of m0n0wall (http://m0n0.ch/wall)
9
	Copyright (C) 2003-2004 Manuel Kasper <mk@neon1.net>.
10
	All rights reserved.
11
12 5b237745 Scott Ullrich
	Redistribution and use in source and binary forms, with or without
13
	modification, are permitted provided that the following conditions are met:
14 4d875b4f Scott Ullrich
15 5b237745 Scott Ullrich
	1. Redistributions of source code must retain the above copyright notice,
16
	   this list of conditions and the following disclaimer.
17 4d875b4f Scott Ullrich
18 5b237745 Scott Ullrich
	2. Redistributions in binary form must reproduce the above copyright
19
	   notice, this list of conditions and the following disclaimer in the
20
	   documentation and/or other materials provided with the distribution.
21 4d875b4f Scott Ullrich
22 5b237745 Scott Ullrich
	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
23
	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
24
	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25
	AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
26
	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31
	POSSIBILITY OF SUCH DAMAGE.
32
*/
33
34
require("guiconfig.inc");
35 b63695db Scott Ullrich
36
$pgtitle = "Diagnostics: DHCP leases";
37
include("head.inc");
38
39 5b237745 Scott Ullrich
?>
40
41
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
42 5fd0b016 Scott Ullrich
<script src="/javascript/sorttable.js"></script>
43 5b237745 Scott Ullrich
<?php include("fbegin.inc"); ?>
44 310b2c06 Bill Marquette
<p class="pgtitle"><?=$pgtitle?></p>
45 5b237745 Scott Ullrich
<?php
46
47
flush();
48
49
function leasecmp($a, $b) {
50 5742ca5e Scott Ullrich
        return strcmp($a[$_GET['order']], $b[$_GET['order']]);
51
}
52
53
function adjust_gmt($dt) {
54
        $ts = strtotime($dt . " GMT");
55
        return strftime("%Y/%m/%d %H:%M:%S", $ts);
56 5b237745 Scott Ullrich
}
57
58 7113d1e6 Scott Ullrich
$fp = @fopen("{$g['dhcpd_chroot_path']}/var/db/dhcpd.leases","r");
59 5b237745 Scott Ullrich
60
if ($fp):
61
62
$return = array();
63
64
while ($line = fgets($fp)) {
65
	$matches = "";
66
67
	// Sort out comments
68
	// C-style comments not supported!
69
	if (preg_match("/^\s*[\r|\n]/", $line, $matches[0]) ||
70
				preg_match("/^([^\"#]*)#.*$/", $line, $matches[1]) ||
71
				preg_match("/^([^\"]*)\/\/.*$/", $line, $matches[2]) ||
72
				preg_match("/\s*#(.*)/", $line, $matches[3]) ||
73
				preg_match("/\\\"\176/", $line, $matches[4])
74
		) {
75
		$line = "";
76
		continue;
77
	}
78
79
	if (preg_match("/(.*)#(.*)/", $line, $matches))
80
		$line = $matches[0];
81
82
	// Tokenize lines
83
	do {
84
		if (preg_match("/^\s*\"([^\"]*)\"(.*)$/", $line, $matches)) {
85
			$line = $matches[2];
86
			$return[] = array($matches[1], 0);
87
		} else if (preg_match("/^\s*([{};])(.*)$/", $line, $matches)) {
88
			$line = $matches[2];
89
			$return[] = array($matches[0], 1);
90
		} else if (preg_match("/^\s*([^{}; \t]+)(.*)$/", $line, $matches)) {
91
			$line = $matches[2];
92
			$return[] = array($matches[1], 0);
93
		} else
94
			break;
95
96
	} while($line);
97
98
	$lines++;
99
}
100
101
fclose($fp);
102
103
$leases = array();
104
$i = 0;
105
106
// Put everything together again
107
while ($data = array_shift($return)) {
108
	if ($data[0] == "next") {
109
		$d = array_shift($return);
110
	}
111
	if ($data[0] == "lease") {
112
		$d = array_shift($return);
113
		$leases[$i]['ip'] = $d[0];
114 9252431c Scott Ullrich
		$leases[$i]['type'] = "dynamic";
115 5b237745 Scott Ullrich
	}
116
	if ($data[0] == "client-hostname") {
117
		$d = array_shift($return);
118 a6bfa4ba Scott Ullrich
		if($d[0] <> "") {
119
			$leases[$i]['hostname'] = $d[0];
120
		} else {
121
			if(gethostbyaddr($leases[$i]['ip']) <> "") {
122
				$leases[$i]['hostname'] = gethostbyaddr($leases[$i]['ip']);
123
			}
124
		}
125
	}	
126 5b237745 Scott Ullrich
	if ($data[0] == "hardware") {
127
		$d = array_shift($return);
128
		if ($d[0] == "ethernet") {
129
			$d = array_shift($return);
130
			$leases[$i]['mac'] = $d[0];
131 4d34bd4a Scott Ullrich
			$online = exec("/usr/sbin/arp -an |/usr/bin/grep {$d[0]}| grep {$leases[$i]['ip']}| /usr/bin/wc -l|/usr/bin/awk '{print $1;}'");
132 9252431c Scott Ullrich
			if ($online == 1) {
133
				$leases[$i]['online'] = 'online';
134
			} else {
135
				$leases[$i]['online'] = 'offline';
136
			}
137 5b237745 Scott Ullrich
		}
138
	} else if ($data[0] == "starts") {
139
		$d = array_shift($return);
140
		$d = array_shift($return);
141
		$leases[$i]['start'] = $d[0];
142
		$d = array_shift($return);
143
		$leases[$i]['start'] .= " " . $d[0];
144
	} else if ($data[0] == "ends") {
145
		$d = array_shift($return);
146
		$d = array_shift($return);
147
		$leases[$i]['end'] = $d[0];
148
		$d = array_shift($return);
149
		$leases[$i]['end'] .= " " . $d[0];
150
	} else if ($data[0] == "binding") {
151
		$d = array_shift($return);
152
		if ($d[0] == "state") {
153
			$d = array_shift($return);
154 9252431c Scott Ullrich
			switch($d[0]) {
155
				case 'active':
156
					$leases[$i]['act'] = 'active';
157
					break;
158
				case 'free':
159
					$leases[$i]['act'] = 'expired';
160
					break;
161
				case 'backup':
162
					$leases[$i]['act'] = 'reserved';	
163
					$leases[$i]['online'] = 'offline';
164
					break;
165
			}
166 5b237745 Scott Ullrich
		}
167 14cf741d Scott Ullrich
	} else if (($data[0] == "}") && ($data[1] == 1)) {		// End of group
168
169
		//updated leases are appended to the end of the lease file. if we aren't
170
		//showing everything just show the most current lease in the list
171
		if(!$_GET['all']) {
172
			$l = $leases[$i]['ip'];
173
			for($k = 0; $k < $i; $k++) {
174
				if($leases[$k]['ip'] == $l) {
175
					array_splice($leases, $k, 1);
176
					--$i;
177
					break;
178
				}
179
			}
180
		}
181 5b237745 Scott Ullrich
		$i++;
182 14cf741d Scott Ullrich
	}
183 5b237745 Scott Ullrich
}
184 9252431c Scott Ullrich
foreach($config['interfaces'] as $ifname => $ifarr) {
185
	if (is_array($config['dhcpd'][$ifname]['staticmap'])) {
186
		foreach($config['dhcpd'][$ifname]['staticmap'] as $static) {
187
			$slease = array();
188
			$slease['ip'] = $static['ipaddr'];
189
			$slease['type'] = "static";
190
			$slease['mac'] = $static['mac'];
191
			$slease['start'] = gmdate("M d Y H:i:s", time());
192
			$slease['end'] = gmdate("M d Y H:i:s", time());
193
			$slease['end'] = gmdate("M d Y H:i:s", strtotime('+5 minutes'));
194
			$slease['hostname'] = $static['descr'];
195
			$slease['act'] = "static";
196
			$online = exec("/usr/sbin/arp -an |/usr/bin/grep {$slease['mac']}| /usr/bin/wc -l|/usr/bin/awk '{print $1;}'");
197
			if ($online == 1) {
198
				$slease['online'] = 'online';
199
			} else {
200
				$slease['online'] = 'offline';
201
			}
202
			$leases[] = $slease;
203
		}
204
	}
205
}
206 5b237745 Scott Ullrich
207
if ($_GET['order'])
208
	usort($leases, "leasecmp");
209
?>
210 5fd0b016 Scott Ullrich
<table class="sortable" id="sortabletable" name="sortabletable" width="100%" border="0" cellpadding="0" cellspacing="0">
211 5b237745 Scott Ullrich
  <tr>
212
    <td class="listhdrr"><a href="?all=<?=$_GET['all'];?>&order=ip">IP address</a></td>
213
    <td class="listhdrr"><a href="?all=<?=$_GET['all'];?>&order=mac">MAC address</a></td>
214
    <td class="listhdrr"><a href="?all=<?=$_GET['all'];?>&order=hostname">Hostname</a></td>
215
    <td class="listhdrr"><a href="?all=<?=$_GET['all'];?>&order=start">Start</a></td>
216 9252431c Scott Ullrich
    <td class="listhdrr"><a href="?all=<?=$_GET['all'];?>&order=end">End</a></td>
217
    <td class="listhdr"><a href="?all=<?=$_GET['all'];?>&order=online">Online</a></td>
218
    <td class="listhdr"><a href="?all=<?=$_GET['all'];?>&order=act">Lease Type</a></td>
219 5b237745 Scott Ullrich
	</tr>
220
<?php
221
foreach ($leases as $data) {
222 9252431c Scott Ullrich
	if (($data['act'] == "active") || ($data['act'] == "static") || ($_GET['all'] == 1)) {
223
		if ($data['act'] != "active" && $data['act'] != "static") {
224 5b237745 Scott Ullrich
			$fspans = "<span class=\"gray\">";
225
			$fspane = "</span>";
226
		} else {
227
			$fspans = $fspane = "";
228
		}
229 69981800 Scott Ullrich
                $lip = ip2long($data['ip']);
230 5b2177e4 Scott Ullrich
		if ($data['act'] == "static") {
231
			foreach ($config['dhcpd'] as $dhcpif => $dhcpifconf) {
232
				foreach ($dhcpifconf['staticmap'] as $staticent) {
233
					if ($data['ip'] == $staticent['ipaddr']) {
234
						$data['if'] = $dhcpif;
235
						break;
236
					}
237
				}
238
				/* exit as soon as we have an interface */
239
				if ($data['if'] != "")
240
					break;
241
			}
242
		} else {
243
                	foreach ($config['dhcpd'] as $dhcpif => $dhcpifconf) {
244
                        	if (($lip >= ip2long($dhcpifconf['range']['from'])) && ($lip <= ip2long($dhcpifconf['range']['to']))) {
245
                                	$data['if'] = $dhcpif;
246
                                	break;
247
                        	}
248
			}
249 69981800 Scott Ullrich
                }		
250 5b237745 Scott Ullrich
		echo "<tr>\n";
251 5742ca5e Scott Ullrich
                echo "<td class=\"listlr\">{$fspans}{$data['ip']}{$fspane}&nbsp;</td>\n";
252 9252431c Scott Ullrich
                if ($data['online'] != "online") {
253 37c0c49b Scott Ullrich
                        echo "<td class=\"listr\">{$fspans}<a href=\"services_wol.php?if={$data['if']}&mac={$data['mac']}\" title=\"send Wake on Lan packet to mac\">{$data['mac']}</a>{$fspane}&nbsp;</td>\n";
254
                } else {
255 9252431c Scott Ullrich
                	echo "<td class=\"listr\">{$fspans}{$data['mac']}{$fspane}&nbsp;</td>\n";
256 37c0c49b Scott Ullrich
                }
257 5742ca5e Scott Ullrich
                echo "<td class=\"listr\">{$fspans}{$data['hostname']}{$fspane}&nbsp;</td>\n";
258
                echo "<td class=\"listr\">{$fspans}" . adjust_gmt($data['start']) . "{$fspane}&nbsp;</td>\n";
259
                echo "<td class=\"listr\">{$fspans}" . adjust_gmt($data['end']) . "{$fspane}&nbsp;</td>\n";
260 9252431c Scott Ullrich
                echo "<td class=\"listr\">{$fspans}{$data['online']}{$fspane}&nbsp;</td>\n";
261
                echo "<td class=\"listr\">{$fspans}{$data['act']}{$fspane}&nbsp;</td>\n";
262
		
263
		if ($data['type'] == "dynamic") {
264
                	echo "<td class=\"list\" valign=\"middle\"><a href=\"services_dhcp_edit.php?if={$data['if']}&mac={$data['mac']}&descr={$data['hostname']}\">";
265
			echo "<img src=\"/themes/{$g['theme']}/images/icons/icon_plus.gif\" width=\"17\" height=\"17\" border=\"0\" title=\"add a static mapping for this MAC address\"></a></td>\n";
266
		} else {
267
                	echo "<td class=\"list\" valign=\"middle\">";
268 b86e64c1 Scott Ullrich
			echo "<img src=\"/themes/{$g['theme']}/images/icons/icon_plus_mo.gif\" width=\"17\" height=\"17\" border=\"0\"></td>\n";
269 9252431c Scott Ullrich
		}
270
271 7477df4f Scott Ullrich
                echo "<td valign=\"middle\"><a href=\"services_wol_edit.php?if={$data['if']}&mac={$data['mac']}&descr={$data['hostname']}\">";
272 37c0c49b Scott Ullrich
		echo "<img src=\"/themes/{$g['theme']}/images/icons/icon_wol_all.gif\" width=\"17\" height=\"17\" border=\"0\" title=\"add a Wake on Lan mapping for this MAC address\"></a></td>\n";
273 5742ca5e Scott Ullrich
                echo "</tr>\n";
274 5b237745 Scott Ullrich
	}
275
}
276
?>
277
</table>
278
<p>
279
<form action="diag_dhcp_leases.php" method="GET">
280
<input type="hidden" name="order" value="<?=$_GET['order'];?>">
281
<?php if ($_GET['all']): ?>
282
<input type="hidden" name="all" value="0">
283 9252431c Scott Ullrich
<input type="submit" class="formbtn" value="Show active and static leases only">
284 5b237745 Scott Ullrich
<?php else: ?>
285
<input type="hidden" name="all" value="1">
286 9252431c Scott Ullrich
<input type="submit" class="formbtn" value="Show all configured leases">
287 5b237745 Scott Ullrich
<?php endif; ?>
288
</form>
289
<?php else: ?>
290
<p><strong>No leases file found. Is the DHCP server active?</strong></p>
291
<?php endif; ?>
292 fda1fdae Scott Ullrich
293 555d3758 Scott Ullrich
<meta http-equiv="refresh" content="120;url=<?php print $_SERVER['SCRIPT_NAME']; ?>">
294 fda1fdae Scott Ullrich
295 5b237745 Scott Ullrich
<?php include("fend.inc"); ?>
296
</body>
297
</html>