Project

General

Profile

Download (8.39 KB) Statistics
| Branch: | Tag: | Revision:
1 5b237745 Scott Ullrich
<?php 
2 b46bfcf5 Bill Marquette
/* $Id$ */
3 5b237745 Scott Ullrich
/*
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 1d333258 Scott Ullrich
/*
32
	pfSense_BUILDER_BINARIES:	/usr/sbin/arp
33
	pfSense_MODULE:	dhcpserver
34
*/
35 5b237745 Scott Ullrich
36 6b07c15a Matthew Grooms
##|+PRIV
37
##|*IDENT=page-services-dhcpserver-editstaticmapping
38
##|*NAME=Services: DHCP Server : Edit static mapping page
39
##|*DESCR=Allow access to the 'Services: DHCP Server : Edit static mapping' page.
40
##|*MATCH=services_dhcp_edit.php*
41
##|-PRIV
42
43 f28a9efd jim-p
function staticmapcmp($a, $b) {
44
        return ipcmp($a['ipaddr'], $b['ipaddr']);
45
}
46
47 0d64af59 Ermal Lu?i
function staticmaps_sort($ifgui) {
48
        global $g, $config;
49
50
        usort($config['dhcpd'][$ifgui]['staticmap'], "staticmapcmp");
51
}
52
53 4b559908 Ermal Luçi
require_once('globals.inc');
54
55 2ee0410f Scott Ullrich
if(!$g['services_dhcp_server_enable']) {
56
	Header("Location: /");
57
	exit;
58
}
59 6b07c15a Matthew Grooms
60 5b237745 Scott Ullrich
require("guiconfig.inc");
61
62
$if = $_GET['if'];
63
if ($_POST['if'])
64
	$if = $_POST['if'];
65
	
66
if (!$if) {
67
	header("Location: services_dhcp.php");
68
	exit;
69
}
70
71
if (!is_array($config['dhcpd'][$if]['staticmap'])) {
72
	$config['dhcpd'][$if]['staticmap'] = array();
73
}
74 0baa37c5 Scott Ullrich
75
$static_map_enabled=isset($config['dhcpd'][$if]['staticarp']);
76
77 5b237745 Scott Ullrich
staticmaps_sort($if);
78
$a_maps = &$config['dhcpd'][$if]['staticmap'];
79 a55e9c70 Ermal Lu?i
$ifcfgip = get_interface_ip($if);
80
$ifcfgsn = get_interface_subnet($if);
81
$ifcfgdescr = convert_friendly_interface_to_friendly_descr($if);
82 5b237745 Scott Ullrich
83
$id = $_GET['id'];
84
if (isset($_POST['id']))
85
	$id = $_POST['id'];
86
87
if (isset($id) && $a_maps[$id]) {
88 3a32590b Scott Ullrich
        $pconfig['mac'] = $a_maps[$id]['mac'];
89 6a01ea44 Bill Marquette
		$pconfig['hostname'] = $a_maps[$id]['hostname'];
90 3a32590b Scott Ullrich
        $pconfig['ipaddr'] = $a_maps[$id]['ipaddr'];
91
        $pconfig['descr'] = $a_maps[$id]['descr'];
92
} else {
93
        $pconfig['mac'] = $_GET['mac'];
94 6a01ea44 Bill Marquette
		$pconfig['hostname'] = $_GET['hostname'];
95 7477df4f Scott Ullrich
        $pconfig['descr'] = $_GET['descr'];
96 5b237745 Scott Ullrich
}
97
98
if ($_POST) {
99
100
	unset($input_errors);
101
	$pconfig = $_POST;
102
103
	/* input validation */
104
	$reqdfields = explode(" ", "mac");
105
	$reqdfieldsn = explode(",", "MAC address");
106
	
107
	do_input_validation($_POST, $reqdfields, $reqdfieldsn, &$input_errors);
108 4f3401e0 Bill Marquette
109
	/* normalize MAC addresses - lowercase and convert Windows-ized hyphenated MACs to colon delimited */
110
	$_POST['mac'] = strtolower(str_replace("-", ":", $_POST['mac']));
111 5b237745 Scott Ullrich
	
112 6a01ea44 Bill Marquette
	if (($_POST['host'] && !is_hostname($_POST['host']))) {
113
		$input_errors[] = "A valid host name must be specified.";
114
	}
115 5b237745 Scott Ullrich
	if (($_POST['ipaddr'] && !is_ipaddr($_POST['ipaddr']))) {
116
		$input_errors[] = "A valid IP address must be specified.";
117
	}
118
	if (($_POST['mac'] && !is_macaddr($_POST['mac']))) {
119
		$input_errors[] = "A valid MAC address must be specified.";
120
	}
121 0baa37c5 Scott Ullrich
	if($static_map_enabled && !$_POST['ipaddr']) {
122
		$input_errors[] = "Static map is enabled.  You must specify an IP address.";
123
	}
124
	
125 5b237745 Scott Ullrich
	/* check for overlaps */
126
	foreach ($a_maps as $mapent) {
127
		if (isset($id) && ($a_maps[$id]) && ($a_maps[$id] === $mapent))
128
			continue;
129
130 f15f4b4d Scott Ullrich
		if ((($mapent['hostname'] == $_POST['hostname']) && $mapent['hostname'])  || ($mapent['mac'] == $_POST['mac'])) {
131 cfc5a090 Scott Ullrich
			$input_errors[] = "This Hostname, IP or MAC address already exists.";
132 5b237745 Scott Ullrich
			break;
133
		}
134
	}
135
		
136
	/* make sure it's not within the dynamic subnet */
137
	if ($_POST['ipaddr']) {
138
		$dynsubnet_start = ip2long($config['dhcpd'][$if]['range']['from']);
139
		$dynsubnet_end = ip2long($config['dhcpd'][$if]['range']['to']);
140 a55e9c70 Ermal Lu?i
		$lansubnet_start = (ip2long($ifcfgip) & gen_subnet_mask_long($ifcfgsn));
141
		$lansubnet_end = (ip2long($ifcfgip) | (~gen_subnet_mask_long($ifcfgsn)));
142 5b237745 Scott Ullrich
		if ((ip2long($_POST['ipaddr']) < $lansubnet_start) ||
143
			(ip2long($_POST['ipaddr']) > $lansubnet_end)) {
144 a55e9c70 Ermal Lu?i
			$input_errors[] = "The IP address must lie in the {$ifcfgdescr} subnet.";
145 5b237745 Scott Ullrich
		}
146
	}
147
148
	if (!$input_errors) {
149
		$mapent = array();
150
		$mapent['mac'] = $_POST['mac'];
151
		$mapent['ipaddr'] = $_POST['ipaddr'];
152 6a01ea44 Bill Marquette
		$mapent['hostname'] = $_POST['hostname'];
153 5b237745 Scott Ullrich
		$mapent['descr'] = $_POST['descr'];
154
155 0d64af59 Ermal Lu?i
		staticmaps_sort($if);
156 5b237745 Scott Ullrich
		if (isset($id) && $a_maps[$id])
157
			$a_maps[$id] = $mapent;
158
		else
159
			$a_maps[] = $mapent;
160
		
161
		write_config();
162 608ef5e7 Scott Ullrich
163 6a01ea44 Bill Marquette
		if(isset($config['dhcpd'][$if]['enable'])) {
164 a368a026 Ermal Lu?i
			mark_subsystem_dirty('staticmaps');
165 6a01ea44 Bill Marquette
			if (isset($config['dnsmasq']['regdhcpstatic']))	
166 a368a026 Ermal Lu?i
				mark_subsystem_dirty('hosts');
167 6a01ea44 Bill Marquette
		}
168 e4da84d0 Bill Marquette
169 5b237745 Scott Ullrich
		header("Location: services_dhcp.php?if={$if}");
170
		exit;
171
	}
172
}
173 4df96eff Scott Ullrich
174 d88c6a9f Scott Ullrich
$pgtitle = array("Services","DHCP","Edit static mapping");
175 4df96eff Scott Ullrich
include("head.inc");
176
177 5b237745 Scott Ullrich
?>
178
179
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
180
<?php include("fbegin.inc"); ?>
181
<?php if ($input_errors) print_input_errors($input_errors); ?>
182
            <form action="services_dhcp_edit.php" method="post" name="iform" id="iform">
183
              <table width="100%" border="0" cellpadding="6" cellspacing="0">
184 e22987a4 Scott Ullrich
				<tr>
185
					<td colspan="2" valign="top" class="listtopic">Static DHCP Mapping</td>
186
				</tr>	
187 5b237745 Scott Ullrich
                <tr> 
188
                  <td width="22%" valign="top" class="vncellreq">MAC address</td>
189
                  <td width="78%" class="vtable"> 
190 b5c78501 Seth Mos
                    <input name="mac" type="text" class="formfld unknown" id="mac" size="30" value="<?=htmlspecialchars($pconfig['mac']);?>">
191 f183b092 Scott Ullrich
		    <?php
192
			$ip = getenv('REMOTE_ADDR');
193
			$mac = `/usr/sbin/arp -an | grep {$ip} | cut -d" " -f4`;
194
			$mac = str_replace("\n","",$mac);
195
		    ?>
196
		    <a OnClick="document.forms[0].mac.value='<?=$mac?>';" href="#">Copy my MAC address</a>   		    
197 5b237745 Scott Ullrich
                    <br>
198
                    <span class="vexpl">Enter a MAC address in the following format: 
199
                    xx:xx:xx:xx:xx:xx</span></td>
200
                </tr>
201
                <tr> 
202
                  <td width="22%" valign="top" class="vncell">IP address</td>
203
                  <td width="78%" class="vtable"> 
204 b5c78501 Seth Mos
                    <input name="ipaddr" type="text" class="formfld unknown" id="ipaddr" size="20" value="<?=htmlspecialchars($pconfig['ipaddr']);?>">
205 5b237745 Scott Ullrich
                    <br>
206
                    If no IP address is given, one will be dynamically allocated  from the pool.</td>
207
                </tr>
208 6a01ea44 Bill Marquette
                <tr> 
209
                  <td width="22%" valign="top" class="vncell">Hostname</td>
210
                  <td width="78%" class="vtable"> 
211 b5c78501 Seth Mos
                    <input name="hostname" type="text" class="formfld unknown" id="hostname" size="20" value="<?=htmlspecialchars($pconfig['hostname']);?>">
212 6a01ea44 Bill Marquette
                    <br> <span class="vexpl">Name of the host, without domain part.</span></td>
213
                </tr>				
214 5b237745 Scott Ullrich
                <tr> 
215
                  <td width="22%" valign="top" class="vncell">Description</td>
216
                  <td width="78%" class="vtable"> 
217 b5c78501 Seth Mos
                    <input name="descr" type="text" class="formfld unknown" id="descr" size="40" value="<?=htmlspecialchars($pconfig['descr']);?>"> 
218 5b237745 Scott Ullrich
                    <br> <span class="vexpl">You may enter a description here 
219
                    for your reference (not parsed).</span></td>
220
                </tr>
221
                <tr> 
222
                  <td width="22%" valign="top">&nbsp;</td>
223
                  <td width="78%"> 
224 fc01e414 Scott Ullrich
                    <input name="Submit" type="submit" class="formbtn" value="Save"> <input class="formbtn" type="button" value="Cancel" onclick="history.back()">
225 5b237745 Scott Ullrich
                    <?php if (isset($id) && $a_maps[$id]): ?>
226
                    <input name="id" type="hidden" value="<?=$id;?>">
227
                    <?php endif; ?>
228
                    <input name="if" type="hidden" value="<?=$if;?>"> 
229
                  </td>
230
                </tr>
231
              </table>
232
</form>
233
<?php include("fend.inc"); ?>
234
</body>
235
</html>