1
|
<?php
|
2
|
/* $Id$ */
|
3
|
/*
|
4
|
services_dhcp_edit.php
|
5
|
part of m0n0wall (http://m0n0.ch/wall)
|
6
|
|
7
|
Copyright (C) 2003-2004 Manuel Kasper <mk@neon1.net>.
|
8
|
All rights reserved.
|
9
|
|
10
|
Redistribution and use in source and binary forms, with or without
|
11
|
modification, are permitted provided that the following conditions are met:
|
12
|
|
13
|
1. Redistributions of source code must retain the above copyright notice,
|
14
|
this list of conditions and the following disclaimer.
|
15
|
|
16
|
2. Redistributions in binary form must reproduce the above copyright
|
17
|
notice, this list of conditions and the following disclaimer in the
|
18
|
documentation and/or other materials provided with the distribution.
|
19
|
|
20
|
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
21
|
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
|
22
|
AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
23
|
AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
|
24
|
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
25
|
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
26
|
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
27
|
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
28
|
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
29
|
POSSIBILITY OF SUCH DAMAGE.
|
30
|
*/
|
31
|
|
32
|
require("guiconfig.inc");
|
33
|
|
34
|
$if = $_GET['if'];
|
35
|
if ($_POST['if'])
|
36
|
$if = $_POST['if'];
|
37
|
|
38
|
if (!$if) {
|
39
|
header("Location: services_dhcp.php");
|
40
|
exit;
|
41
|
}
|
42
|
|
43
|
if (!is_array($config['dhcpd'][$if]['staticmap'])) {
|
44
|
$config['dhcpd'][$if]['staticmap'] = array();
|
45
|
}
|
46
|
|
47
|
$static_map_enabled=isset($config['dhcpd'][$if]['staticarp']);
|
48
|
|
49
|
staticmaps_sort($if);
|
50
|
$a_maps = &$config['dhcpd'][$if]['staticmap'];
|
51
|
$ifcfg = &$config['interfaces'][$if];
|
52
|
|
53
|
$id = $_GET['id'];
|
54
|
if (isset($_POST['id']))
|
55
|
$id = $_POST['id'];
|
56
|
|
57
|
if (isset($id) && $a_maps[$id]) {
|
58
|
$pconfig['mac'] = $a_maps[$id]['mac'];
|
59
|
$pconfig['hostname'] = $a_maps[$id]['hostname'];
|
60
|
$pconfig['ipaddr'] = $a_maps[$id]['ipaddr'];
|
61
|
$pconfig['descr'] = $a_maps[$id]['descr'];
|
62
|
} else {
|
63
|
$pconfig['mac'] = $_GET['mac'];
|
64
|
$pconfig['hostname'] = $_GET['hostname'];
|
65
|
$pconfig['descr'] = $_GET['descr'];
|
66
|
}
|
67
|
|
68
|
if ($_POST) {
|
69
|
|
70
|
unset($input_errors);
|
71
|
$pconfig = $_POST;
|
72
|
|
73
|
/* input validation */
|
74
|
$reqdfields = explode(" ", "mac");
|
75
|
$reqdfieldsn = explode(",", "MAC address");
|
76
|
|
77
|
do_input_validation($_POST, $reqdfields, $reqdfieldsn, &$input_errors);
|
78
|
|
79
|
/* normalize MAC addresses - lowercase and convert Windows-ized hyphenated MACs to colon delimited */
|
80
|
$_POST['mac'] = strtolower(str_replace("-", ":", $_POST['mac']));
|
81
|
|
82
|
if (($_POST['host'] && !is_hostname($_POST['host']))) {
|
83
|
$input_errors[] = "A valid host name must be specified.";
|
84
|
}
|
85
|
if (($_POST['ipaddr'] && !is_ipaddr($_POST['ipaddr']))) {
|
86
|
$input_errors[] = "A valid IP address must be specified.";
|
87
|
}
|
88
|
if (($_POST['mac'] && !is_macaddr($_POST['mac']))) {
|
89
|
$input_errors[] = "A valid MAC address must be specified.";
|
90
|
}
|
91
|
if($static_map_enabled && !$_POST['ipaddr']) {
|
92
|
$input_errors[] = "Static map is enabled. You must specify an IP address.";
|
93
|
}
|
94
|
|
95
|
/* check for overlaps */
|
96
|
foreach ($a_maps as $mapent) {
|
97
|
if (isset($id) && ($a_maps[$id]) && ($a_maps[$id] === $mapent))
|
98
|
continue;
|
99
|
|
100
|
if ((($mapent['hostname'] == $_POST['hostname']) && $mapent['hostname']) || ($mapent['mac'] == $_POST['mac'])) {
|
101
|
$input_errors[] = "This Hostname, IP or MAC address already exists.";
|
102
|
break;
|
103
|
}
|
104
|
}
|
105
|
|
106
|
/* make sure it's not within the dynamic subnet */
|
107
|
if ($_POST['ipaddr']) {
|
108
|
$dynsubnet_start = ip2long($config['dhcpd'][$if]['range']['from']);
|
109
|
$dynsubnet_end = ip2long($config['dhcpd'][$if]['range']['to']);
|
110
|
$lansubnet_start = (ip2long($ifcfg['ipaddr']) & gen_subnet_mask_long($ifcfg['subnet']));
|
111
|
$lansubnet_end = (ip2long($ifcfg['ipaddr']) | (~gen_subnet_mask_long($ifcfg['subnet'])));
|
112
|
if ((ip2long($_POST['ipaddr']) < $lansubnet_start) ||
|
113
|
(ip2long($_POST['ipaddr']) > $lansubnet_end)) {
|
114
|
$input_errors[] = "The IP address must lie in the {$ifcfg['descr']} subnet.";
|
115
|
}
|
116
|
}
|
117
|
|
118
|
if (!$input_errors) {
|
119
|
$mapent = array();
|
120
|
$mapent['mac'] = $_POST['mac'];
|
121
|
$mapent['ipaddr'] = $_POST['ipaddr'];
|
122
|
$mapent['hostname'] = $_POST['hostname'];
|
123
|
$mapent['descr'] = $_POST['descr'];
|
124
|
|
125
|
if (isset($id) && $a_maps[$id])
|
126
|
$a_maps[$id] = $mapent;
|
127
|
else
|
128
|
$a_maps[] = $mapent;
|
129
|
|
130
|
write_config();
|
131
|
|
132
|
if(isset($config['dhcpd'][$if]['enable'])) {
|
133
|
touch($d_staticmapsdirty_path);
|
134
|
if (isset($config['dnsmasq']['regdhcpstatic']))
|
135
|
touch($d_hostsdirty_path);
|
136
|
}
|
137
|
|
138
|
header("Location: services_dhcp.php?if={$if}");
|
139
|
exit;
|
140
|
}
|
141
|
}
|
142
|
|
143
|
$pgtitle = "Services: DHCP: Edit static mapping";
|
144
|
include("head.inc");
|
145
|
|
146
|
?>
|
147
|
|
148
|
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
|
149
|
<?php include("fbegin.inc"); ?>
|
150
|
<p class="pgtitle"><?=$pgtitle?></p>
|
151
|
<?php if ($input_errors) print_input_errors($input_errors); ?>
|
152
|
<form action="services_dhcp_edit.php" method="post" name="iform" id="iform">
|
153
|
<table width="100%" border="0" cellpadding="6" cellspacing="0">
|
154
|
<tr>
|
155
|
<td width="22%" valign="top" class="vncellreq">MAC address</td>
|
156
|
<td width="78%" class="vtable">
|
157
|
<input name="mac" type="text" class="formfld" id="mac" size="30" value="<?=htmlspecialchars($pconfig['mac']);?>">
|
158
|
<?php
|
159
|
$ip = getenv('REMOTE_ADDR');
|
160
|
$mac = `/usr/sbin/arp -an | grep {$ip} | cut -d" " -f4`;
|
161
|
$mac = str_replace("\n","",$mac);
|
162
|
?>
|
163
|
<a OnClick="document.forms[0].mac.value='<?=$mac?>';" href="#">Copy my MAC address</a>
|
164
|
<br>
|
165
|
<span class="vexpl">Enter a MAC address in the following format:
|
166
|
xx:xx:xx:xx:xx:xx</span></td>
|
167
|
</tr>
|
168
|
<tr>
|
169
|
<td width="22%" valign="top" class="vncell">IP address</td>
|
170
|
<td width="78%" class="vtable">
|
171
|
<input name="ipaddr" type="text" class="formfld" id="ipaddr" size="20" value="<?=htmlspecialchars($pconfig['ipaddr']);?>">
|
172
|
<br>
|
173
|
If no IP address is given, one will be dynamically allocated from the pool.</td>
|
174
|
</tr>
|
175
|
<tr>
|
176
|
<td width="22%" valign="top" class="vncell">Hostname</td>
|
177
|
<td width="78%" class="vtable">
|
178
|
<input name="hostname" type="text" class="formfld" id="hostname" size="20" value="<?=htmlspecialchars($pconfig['hostname']);?>">
|
179
|
<br> <span class="vexpl">Name of the host, without domain part.</span></td>
|
180
|
</tr>
|
181
|
<tr>
|
182
|
<td width="22%" valign="top" class="vncell">Description</td>
|
183
|
<td width="78%" class="vtable">
|
184
|
<input name="descr" type="text" class="formfld" id="descr" size="40" value="<?=htmlspecialchars($pconfig['descr']);?>">
|
185
|
<br> <span class="vexpl">You may enter a description here
|
186
|
for your reference (not parsed).</span></td>
|
187
|
</tr>
|
188
|
<tr>
|
189
|
<td width="22%" valign="top"> </td>
|
190
|
<td width="78%">
|
191
|
<input name="Submit" type="submit" class="formbtn" value="Save"> <input class="formbtn" type="button" value="Cancel" onclick="history.back()">
|
192
|
<?php if (isset($id) && $a_maps[$id]): ?>
|
193
|
<input name="id" type="hidden" value="<?=$id;?>">
|
194
|
<?php endif; ?>
|
195
|
<input name="if" type="hidden" value="<?=$if;?>">
|
196
|
</td>
|
197
|
</tr>
|
198
|
</table>
|
199
|
</form>
|
200
|
<?php include("fend.inc"); ?>
|
201
|
</body>
|
202
|
</html>
|