Project

General

Profile

Download (11.6 KB) Statistics
| Branch: | Tag: | Revision:
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
	pfSense_BUILDER_BINARIES:	/usr/sbin/arp
33
	pfSense_MODULE:	dhcpserver
34
*/
35

    
36
##|+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
function staticmapcmp($a, $b) {
44
        return ipcmp($a['ipaddr'], $b['ipaddr']);
45
}
46

    
47
function staticmaps_sort($ifgui) {
48
        global $g, $config;
49

    
50
        usort($config['dhcpd'][$ifgui]['staticmap'], "staticmapcmp");
51
}
52

    
53
require_once('globals.inc');
54

    
55
if(!$g['services_dhcp_server_enable']) {
56
	Header("Location: /");
57
	exit;
58
}
59

    
60
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']))
72
	$config['dhcpd'] = array();
73
if (!is_array($config['dhcpd'][$if]))
74
	$config['dhcpd'][$if] = array();
75
if (!is_array($config['dhcpd'][$if]['staticmap']))
76
	$config['dhcpd'][$if]['staticmap'] = array();
77

    
78
$static_arp_enabled=isset($config['dhcpd'][$if]['staticarp']);
79
$netboot_enabled=isset($config['dhcpd'][$if]['netboot']);
80
$a_maps = &$config['dhcpd'][$if]['staticmap'];
81
$ifcfgip = get_interface_ip($if);
82
$ifcfgsn = get_interface_subnet($if);
83
$ifcfgdescr = convert_friendly_interface_to_friendly_descr($if);
84

    
85
$id = $_GET['id'];
86
if (isset($_POST['id']))
87
	$id = $_POST['id'];
88

    
89
if (isset($id) && $a_maps[$id]) {
90
        $pconfig['mac'] = $a_maps[$id]['mac'];
91
		$pconfig['hostname'] = $a_maps[$id]['hostname'];
92
        $pconfig['ipaddr'] = $a_maps[$id]['ipaddr'];
93
	    $pconfig['filename'] = $a_maps[$id]['filename'];
94
		$pconfig['rootpath'] = $a_maps[$id]['rootpath'];
95
        $pconfig['descr'] = $a_maps[$id]['descr'];
96
        $pconfig['arp_table_static_entry'] = isset($a_maps[$id]['arp_table_static_entry']);
97
} else {
98
        $pconfig['mac'] = $_GET['mac'];
99
		$pconfig['hostname'] = $_GET['hostname'];
100
	    $pconfig['filename'] = $_GET['filename'];
101
		$pconfig['rootpath'] = $_GET['rootpath'];
102
        $pconfig['descr'] = $_GET['descr'];
103
        $pconfig['arp_table_static_entry'] = $_GET['arp_table_static_entry'];
104
}
105

    
106
if ($_POST) {
107

    
108
	unset($input_errors);
109
	$pconfig = $_POST;
110

    
111
	/* input validation */
112
	$reqdfields = explode(" ", "mac");
113
	$reqdfieldsn = array(gettext("MAC address"));
114
	
115
	do_input_validation($_POST, $reqdfields, $reqdfieldsn, &$input_errors);
116

    
117
	/* normalize MAC addresses - lowercase and convert Windows-ized hyphenated MACs to colon delimited */
118
	$_POST['mac'] = strtolower(str_replace("-", ":", $_POST['mac']));
119
	
120
	if ($_POST['hostname']) {
121
		preg_match("/\-\$/", $_POST['hostname'], $matches);
122
		if($matches)
123
			$input_errors[] = gettext("The hostname cannot end with a hyphen according to RFC952");		
124
		if (!is_hostname($_POST['hostname'])) {
125
			$input_errors[] = gettext("The hostname can only contain the characters A-Z, 0-9 and '-'.");
126
		} else {
127
			if (strpos($_POST['hostname'],'.')) {
128
				$input_errors[] = gettext("A valid hostname is specified, but the domain name part should be omitted");
129
			}
130
		}
131
	}
132
	if (($_POST['ipaddr'] && !is_ipaddr($_POST['ipaddr']))) {
133
		$input_errors[] = gettext("A valid IP address must be specified.");
134
	}
135
	if (($_POST['mac'] && !is_macaddr($_POST['mac']))) {
136
		$input_errors[] = gettext("A valid MAC address must be specified.");
137
	}
138
	if($static_arp_enabled && !$_POST['ipaddr']) {
139
		$input_errors[] = gettext("Static ARP is enabled.  You must specify an IP address.");
140
	}
141
	
142
	/* check for overlaps */
143
	foreach ($a_maps as $mapent) {
144
		if (isset($id) && ($a_maps[$id]) && ($a_maps[$id] === $mapent))
145
			continue;
146

    
147
		if ((($mapent['hostname'] == $_POST['hostname']) && $mapent['hostname'])  || ($mapent['mac'] == $_POST['mac'])) {
148
			$input_errors[] = gettext("This Hostname, IP or MAC address already exists.");
149
			break;
150
		}
151
	}
152
		
153
	/* make sure it's not within the dynamic subnet */
154
	if ($_POST['ipaddr']) {
155
		$dynsubnet_start = ip2ulong($config['dhcpd'][$if]['range']['from']);
156
		$dynsubnet_end = ip2ulong($config['dhcpd'][$if]['range']['to']);
157
		if ((ip2ulong($_POST['ipaddr']) >= $dynsubnet_start) &&
158
			(ip2ulong($_POST['ipaddr']) <= $dynsubnet_end)) {
159
			$input_errors[] = sprintf(gettext("The IP address must not be within the DHCP range for this interface."));
160
		}
161

    
162
		$lansubnet_start = ip2ulong(long2ip32(ip2long($ifcfgip) & gen_subnet_mask_long($ifcfgsn)));
163
		$lansubnet_end = ip2ulong(long2ip32(ip2long($ifcfgip) | (~gen_subnet_mask_long($ifcfgsn))));
164
		if ((ip2ulong($_POST['ipaddr']) < $lansubnet_start) ||
165
			(ip2ulong($_POST['ipaddr']) > $lansubnet_end)) {
166
			$input_errors[] = sprintf(gettext("The IP address must lie in the %s subnet."),$ifcfgdescr);
167
		}
168
	}
169

    
170
	if (!$input_errors) {
171
		$mapent = array();
172
		$mapent['mac'] = $_POST['mac'];
173
		$mapent['ipaddr'] = $_POST['ipaddr'];
174
		$mapent['hostname'] = $_POST['hostname'];
175
		$mapent['descr'] = $_POST['descr'];
176
		$mapent['arp_table_static_entry'] = ($_POST['arp_table_static_entry']) ? true : false;
177
		$mapent['filename'] = $_POST['filename'];
178
		$mapent['rootpath'] = $_POST['rootpath'];
179

    
180
		if (isset($id) && $a_maps[$id])
181
			$a_maps[$id] = $mapent;
182
		else
183
			$a_maps[] = $mapent;
184
		staticmaps_sort($if);
185
		
186
		write_config();
187

    
188
		if(isset($config['dhcpd'][$if]['enable'])) {
189
			mark_subsystem_dirty('staticmaps');
190
			if (isset($config['dnsmasq']['regdhcpstatic']))	
191
				mark_subsystem_dirty('hosts');
192
		}
193

    
194
		header("Location: services_dhcp.php?if={$if}");
195
		exit;
196
	}
197
}
198

    
199
$pgtitle = array(gettext("Services"),gettext("DHCP"),gettext("Edit static mapping"));
200
$shortcut_section = "dhcp";
201

    
202
include("head.inc");
203

    
204
?>
205

    
206
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
207
<?php include("fbegin.inc"); ?>
208
<?php if ($input_errors) print_input_errors($input_errors); ?>
209
            <form action="services_dhcp_edit.php" method="post" name="iform" id="iform">
210
              <table width="100%" border="0" cellpadding="6" cellspacing="0">
211
				<tr>
212
					<td colspan="2" valign="top" class="listtopic"><?=gettext("Static DHCP Mapping");?></td>
213
				</tr>	
214
                <tr> 
215
                  <td width="22%" valign="top" class="vncellreq"><?=gettext("MAC address");?></td>
216
                  <td width="78%" class="vtable"> 
217
                    <input name="mac" type="text" class="formfld unknown" id="mac" size="30" value="<?=htmlspecialchars($pconfig['mac']);?>">
218
		    <?php
219
			$ip = getenv('REMOTE_ADDR');
220
			$mac = `/usr/sbin/arp -an | grep {$ip} | cut -d" " -f4`;
221
			$mac = str_replace("\n","",$mac);
222
		    ?>
223
		    <a OnClick="document.forms[0].mac.value='<?=$mac?>';" href="#"><?=gettext("Copy my MAC address");?></a>   		    
224
                    <br>
225
                    <span class="vexpl"><?=gettext("Enter a MAC address in the following format: ".
226
                    "xx:xx:xx:xx:xx:xx");?></span></td>
227
                </tr>
228
                <tr> 
229
                  <td width="22%" valign="top" class="vncell"><?=gettext("IP address");?></td>
230
                  <td width="78%" class="vtable"> 
231
                    <input name="ipaddr" type="text" class="formfld unknown" id="ipaddr" size="20" value="<?=htmlspecialchars($pconfig['ipaddr']);?>">
232
                    <br>
233
			<?=gettext("If an IPv4 address is entered, the address must be outside of the pool.");?>
234
			<br/>
235
			<?=gettext("If no IPv4 address is given, one will be dynamically allocated from the pool.");?>
236
			</td>
237
                </tr>
238
                <tr> 
239
                  <td width="22%" valign="top" class="vncell"><?=gettext("Hostname");?></td>
240
                  <td width="78%" class="vtable"> 
241
                    <input name="hostname" type="text" class="formfld unknown" id="hostname" size="20" value="<?=htmlspecialchars($pconfig['hostname']);?>">
242
                    <br> <span class="vexpl"><?=gettext("Name of the host, without domain part.");?></span></td>
243
                </tr>				
244
                <?php if($netboot_enabled) { ?>
245
		<tr>
246
		  <td width="22%" valign="top" class="vncell">Netboot Filename</td>
247
		  <td width="78%" class="vtable">
248
		    <input name="filename" type="text" class="formfld unknown" id="filename" size="20" value="<?=htmlspecialchars($pconfig['filename']);?>">
249
		    <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>
250
		</tr>
251
		<tr>
252
		  <td width="22%" valign="top" class="vncell">Root Path</td>
253
		  <td width="78%" class="vtable">
254
			<input name="rootpath" type="text" class="formfld unknown" id="rootpath" size="90" value="<?=htmlspecialchars($pconfig['rootpath']);?>">
255
		    <br> <span class="vexpl"><?=gettext("Enter the"); ?> <b><?=gettext("root-path"); ?></b>-<?=gettext("string");?>, overrides setting on main page.</span></td>
256
		</tr>
257
		<?php } ?>
258
                <tr> 
259
                  <td width="22%" valign="top" class="vncell"><?=gettext("Description");?></td>
260
                  <td width="78%" class="vtable"> 
261
                    <input name="descr" type="text" class="formfld unknown" id="descr" size="40" value="<?=htmlspecialchars($pconfig['descr']);?>"> 
262
                    <br> <span class="vexpl"><?=gettext("You may enter a description here ".
263
                    "for your reference (not parsed).");?></span></td>
264
                </tr>
265
                <tr> 
266
                  <td width="22%" valign="top" class="vncell"><?=gettext("ARP Table Static Entry");?></td>
267
                  <td width="78%" class="vtable"> 
268
                    <input name="arp_table_static_entry" id="arp_table_static_entry" type="checkbox" value="yes" <?php if ($pconfig['arp_table_static_entry']) echo "checked"; ?>> 
269
                    <br> <span class="vexpl"><?=gettext("Create an ARP Table Static Entry for this MAC & IP Address pair. ".
270
                    "");?></span></td>
271
                </tr>
272
                <tr> 
273
                  <td width="22%" valign="top">&nbsp;</td>
274
                  <td width="78%"> 
275
                    <input name="Submit" type="submit" class="formbtn" value="<?=gettext("Save");?>"> <input class="formbtn" type="button" value="<?=gettext("Cancel");?>" onclick="history.back()">
276
                    <?php if (isset($id) && $a_maps[$id]): ?>
277
                    <input name="id" type="hidden" value="<?=htmlspecialchars($id);?>">
278
                    <?php endif; ?>
279
                    <input name="if" type="hidden" value="<?=htmlspecialchars($if);?>"> 
280
                  </td>
281
                </tr>
282
              </table>
283
</form>
284
<?php include("fend.inc"); ?>
285
</body>
286
</html>
(149-149/249)