Project

General

Profile

Download (9.99 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 01292dc2 jim-p
$static_arp_enabled=isset($config['dhcpd'][$if]['staticarp']);
76 d6d50244 Ermal
$netboot_enabled=isset($config['dhcpd'][$if]['netboot']);
77 5b237745 Scott Ullrich
$a_maps = &$config['dhcpd'][$if]['staticmap'];
78 a55e9c70 Ermal Lu?i
$ifcfgip = get_interface_ip($if);
79
$ifcfgsn = get_interface_subnet($if);
80
$ifcfgdescr = convert_friendly_interface_to_friendly_descr($if);
81 5b237745 Scott Ullrich
82
$id = $_GET['id'];
83
if (isset($_POST['id']))
84
	$id = $_POST['id'];
85
86
if (isset($id) && $a_maps[$id]) {
87 3a32590b Scott Ullrich
        $pconfig['mac'] = $a_maps[$id]['mac'];
88 6a01ea44 Bill Marquette
		$pconfig['hostname'] = $a_maps[$id]['hostname'];
89 3a32590b Scott Ullrich
        $pconfig['ipaddr'] = $a_maps[$id]['ipaddr'];
90 d6d50244 Ermal
	$pconfig['netbootfile'] = $a_maps[$id]['netbootfile'];
91 3a32590b Scott Ullrich
        $pconfig['descr'] = $a_maps[$id]['descr'];
92
} else {
93
        $pconfig['mac'] = $_GET['mac'];
94 6a01ea44 Bill Marquette
		$pconfig['hostname'] = $_GET['hostname'];
95 d6d50244 Ermal
	$pconfig['netbootfile'] = $_GET['netbootfile'];
96 7477df4f Scott Ullrich
        $pconfig['descr'] = $_GET['descr'];
97 5b237745 Scott Ullrich
}
98
99
if ($_POST) {
100
101
	unset($input_errors);
102
	$pconfig = $_POST;
103
104
	/* input validation */
105
	$reqdfields = explode(" ", "mac");
106 8ffadf76 Rafael Lucas
	$reqdfieldsn = array(gettext("MAC address"));
107 5b237745 Scott Ullrich
	
108
	do_input_validation($_POST, $reqdfields, $reqdfieldsn, &$input_errors);
109 4f3401e0 Bill Marquette
110
	/* normalize MAC addresses - lowercase and convert Windows-ized hyphenated MACs to colon delimited */
111
	$_POST['mac'] = strtolower(str_replace("-", ":", $_POST['mac']));
112 5b237745 Scott Ullrich
	
113 46c5b763 pierrepomes
	if ($_POST['hostname']) {
114 d0ce82a3 Scott Ullrich
		preg_match("/^[0-9]/", $_POST['hostname'], $matches);
115
		if($matches)
116 4c8a8653 Scott Ullrich
			$input_errors[] = gettext("The hostname cannot start with a numeric character according to RFC952");
117 392687e5 Erik Fonnesbeck
		preg_match("/\-\$/", $_POST['hostname'], $matches);
118 d0ce82a3 Scott Ullrich
		if($matches)
119 4c8a8653 Scott Ullrich
			$input_errors[] = gettext("The hostname cannot end with a hyphen according to RFC952");		
120 46c5b763 pierrepomes
		if (!is_hostname($_POST['hostname'])) {
121 8ffadf76 Rafael Lucas
			$input_errors[] = gettext("The hostname can only contain the characters A-Z, 0-9 and '-'.");
122 46c5b763 pierrepomes
		} else {
123
			if (strpos($_POST['hostname'],'.')) {
124 8ffadf76 Rafael Lucas
				$input_errors[] = gettext("A valid hostname is specified, but the domain name part should be omitted");
125 46c5b763 pierrepomes
			}
126
		}
127 6a01ea44 Bill Marquette
	}
128 5b237745 Scott Ullrich
	if (($_POST['ipaddr'] && !is_ipaddr($_POST['ipaddr']))) {
129 8ffadf76 Rafael Lucas
		$input_errors[] = gettext("A valid IP address must be specified.");
130 5b237745 Scott Ullrich
	}
131
	if (($_POST['mac'] && !is_macaddr($_POST['mac']))) {
132 8ffadf76 Rafael Lucas
		$input_errors[] = gettext("A valid MAC address must be specified.");
133 5b237745 Scott Ullrich
	}
134 01292dc2 jim-p
	if($static_arp_enabled && !$_POST['ipaddr']) {
135 f16ba51e Renato Botelho
		$input_errors[] = gettext("Static ARP is enabled.  You must specify an IP address.");
136 0baa37c5 Scott Ullrich
	}
137
	
138 5b237745 Scott Ullrich
	/* check for overlaps */
139
	foreach ($a_maps as $mapent) {
140
		if (isset($id) && ($a_maps[$id]) && ($a_maps[$id] === $mapent))
141
			continue;
142
143 f15f4b4d Scott Ullrich
		if ((($mapent['hostname'] == $_POST['hostname']) && $mapent['hostname'])  || ($mapent['mac'] == $_POST['mac'])) {
144 8ffadf76 Rafael Lucas
			$input_errors[] = gettext("This Hostname, IP or MAC address already exists.");
145 5b237745 Scott Ullrich
			break;
146
		}
147
	}
148
		
149
	/* make sure it's not within the dynamic subnet */
150
	if ($_POST['ipaddr']) {
151 96033063 Erik Fonnesbeck
		$dynsubnet_start = ip2ulong($config['dhcpd'][$if]['range']['from']);
152
		$dynsubnet_end = ip2ulong($config['dhcpd'][$if]['range']['to']);
153
		$lansubnet_start = ip2ulong(long2ip32(ip2long($ifcfgip) & gen_subnet_mask_long($ifcfgsn)));
154
		$lansubnet_end = ip2ulong(long2ip32(ip2long($ifcfgip) | (~gen_subnet_mask_long($ifcfgsn))));
155
		if ((ip2ulong($_POST['ipaddr']) < $lansubnet_start) ||
156
			(ip2ulong($_POST['ipaddr']) > $lansubnet_end)) {
157 8ffadf76 Rafael Lucas
			$input_errors[] = sprintf(gettext("The IP address must lie in the %s subnet."),$ifcfgdescr);
158 5b237745 Scott Ullrich
		}
159
	}
160
161
	if (!$input_errors) {
162
		$mapent = array();
163
		$mapent['mac'] = $_POST['mac'];
164
		$mapent['ipaddr'] = $_POST['ipaddr'];
165 6a01ea44 Bill Marquette
		$mapent['hostname'] = $_POST['hostname'];
166 5b237745 Scott Ullrich
		$mapent['descr'] = $_POST['descr'];
167 d6d50244 Ermal
		$mapent['netbootfile'] = $_POST['netbootfile'];
168 5b237745 Scott Ullrich
169
		if (isset($id) && $a_maps[$id])
170
			$a_maps[$id] = $mapent;
171
		else
172
			$a_maps[] = $mapent;
173 bb9689bd Erik Fonnesbeck
		staticmaps_sort($if);
174 5b237745 Scott Ullrich
		
175
		write_config();
176 608ef5e7 Scott Ullrich
177 6a01ea44 Bill Marquette
		if(isset($config['dhcpd'][$if]['enable'])) {
178 a368a026 Ermal Lu?i
			mark_subsystem_dirty('staticmaps');
179 6a01ea44 Bill Marquette
			if (isset($config['dnsmasq']['regdhcpstatic']))	
180 a368a026 Ermal Lu?i
				mark_subsystem_dirty('hosts');
181 6a01ea44 Bill Marquette
		}
182 e4da84d0 Bill Marquette
183 5b237745 Scott Ullrich
		header("Location: services_dhcp.php?if={$if}");
184
		exit;
185
	}
186
}
187 4df96eff Scott Ullrich
188 8ffadf76 Rafael Lucas
$pgtitle = array(gettext("Services"),gettext("DHCP"),gettext("Edit static mapping"));
189 b9546722 Chris Buechler
$statusurl = "status_dhcp_leases.php";
190 5224b8e7 jim-p
$logurl = "diag_logs_dhcp.php";
191
192 4df96eff Scott Ullrich
include("head.inc");
193
194 5b237745 Scott Ullrich
?>
195
196
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
197
<?php include("fbegin.inc"); ?>
198
<?php if ($input_errors) print_input_errors($input_errors); ?>
199
            <form action="services_dhcp_edit.php" method="post" name="iform" id="iform">
200
              <table width="100%" border="0" cellpadding="6" cellspacing="0">
201 e22987a4 Scott Ullrich
				<tr>
202 8ffadf76 Rafael Lucas
					<td colspan="2" valign="top" class="listtopic"><?=gettext("Static DHCP Mapping");?></td>
203 e22987a4 Scott Ullrich
				</tr>	
204 5b237745 Scott Ullrich
                <tr> 
205 8ffadf76 Rafael Lucas
                  <td width="22%" valign="top" class="vncellreq"><?=gettext("MAC address");?></td>
206 5b237745 Scott Ullrich
                  <td width="78%" class="vtable"> 
207 b5c78501 Seth Mos
                    <input name="mac" type="text" class="formfld unknown" id="mac" size="30" value="<?=htmlspecialchars($pconfig['mac']);?>">
208 f183b092 Scott Ullrich
		    <?php
209
			$ip = getenv('REMOTE_ADDR');
210
			$mac = `/usr/sbin/arp -an | grep {$ip} | cut -d" " -f4`;
211
			$mac = str_replace("\n","",$mac);
212
		    ?>
213 8ffadf76 Rafael Lucas
		    <a OnClick="document.forms[0].mac.value='<?=$mac?>';" href="#"><?=gettext("Copy my MAC address");?></a>   		    
214 5b237745 Scott Ullrich
                    <br>
215 8ffadf76 Rafael Lucas
                    <span class="vexpl"><?=gettext("Enter a MAC address in the following format: ".
216
                    "xx:xx:xx:xx:xx:xx");?></span></td>
217 5b237745 Scott Ullrich
                </tr>
218
                <tr> 
219 8ffadf76 Rafael Lucas
                  <td width="22%" valign="top" class="vncell"><?=gettext("IP address");?></td>
220 5b237745 Scott Ullrich
                  <td width="78%" class="vtable"> 
221 b5c78501 Seth Mos
                    <input name="ipaddr" type="text" class="formfld unknown" id="ipaddr" size="20" value="<?=htmlspecialchars($pconfig['ipaddr']);?>">
222 5b237745 Scott Ullrich
                    <br>
223 8ffadf76 Rafael Lucas
                    <?=gettext("If no IP address is given, one will be dynamically allocated  from the pool.");?></td>
224 5b237745 Scott Ullrich
                </tr>
225 6a01ea44 Bill Marquette
                <tr> 
226 8ffadf76 Rafael Lucas
                  <td width="22%" valign="top" class="vncell"><?=gettext("Hostname");?></td>
227 6a01ea44 Bill Marquette
                  <td width="78%" class="vtable"> 
228 b5c78501 Seth Mos
                    <input name="hostname" type="text" class="formfld unknown" id="hostname" size="20" value="<?=htmlspecialchars($pconfig['hostname']);?>">
229 8ffadf76 Rafael Lucas
                    <br> <span class="vexpl"><?=gettext("Name of the host, without domain part.");?></span></td>
230 6a01ea44 Bill Marquette
                </tr>				
231 d6d50244 Ermal
                <?php if($netboot_enabled) { ?>
232
		<tr>
233
		  <td width="22%" valign="top" class="vncell">Netboot filename</td>
234
		  <td width="78%" class="vtable">
235
		    <input name="netbootfile" type="text" class="formfld unknown" id="netbootfile" size="20" value="<?=htmlspecialchars($pconfig['netbootfile']);?>">
236
		    <br> <span class="vexpl">Name of the file that should be loaded when this host boots off of the network, overrides setting on main page.</span></td>
237
		</tr>
238
		<?php } ?>
239 5b237745 Scott Ullrich
                <tr> 
240 8ffadf76 Rafael Lucas
                  <td width="22%" valign="top" class="vncell"><?=gettext("Description");?></td>
241 5b237745 Scott Ullrich
                  <td width="78%" class="vtable"> 
242 b5c78501 Seth Mos
                    <input name="descr" type="text" class="formfld unknown" id="descr" size="40" value="<?=htmlspecialchars($pconfig['descr']);?>"> 
243 8ffadf76 Rafael Lucas
                    <br> <span class="vexpl"><?=gettext("You may enter a description here ".
244
                    "for your reference (not parsed).");?></span></td>
245 5b237745 Scott Ullrich
                </tr>
246
                <tr> 
247
                  <td width="22%" valign="top">&nbsp;</td>
248
                  <td width="78%"> 
249 8ffadf76 Rafael Lucas
                    <input name="Submit" type="submit" class="formbtn" value="<?=gettext("Save");?>"> <input class="formbtn" type="button" value="<?=gettext("Cancel");?>" onclick="history.back()">
250 5b237745 Scott Ullrich
                    <?php if (isset($id) && $a_maps[$id]): ?>
251
                    <input name="id" type="hidden" value="<?=$id;?>">
252
                    <?php endif; ?>
253
                    <input name="if" type="hidden" value="<?=$if;?>"> 
254
                  </td>
255
                </tr>
256
              </table>
257
</form>
258
<?php include("fend.inc"); ?>
259
</body>
260 2d994672 Scott Ullrich
</html>