Project

General

Profile

Download (10.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'][$if]['staticmap'])) {
72
	$config['dhcpd'][$if]['staticmap'] = array();
73
}
74

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

    
82
$id = $_GET['id'];
83
if (isset($_POST['id']))
84
	$id = $_POST['id'];
85

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

    
101
if ($_POST) {
102

    
103
	unset($input_errors);
104
	$pconfig = $_POST;
105

    
106
	/* input validation */
107
	$reqdfields = explode(" ", "mac");
108
	$reqdfieldsn = array(gettext("MAC address"));
109
	
110
	do_input_validation($_POST, $reqdfields, $reqdfieldsn, &$input_errors);
111

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

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

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

    
165
	if (!$input_errors) {
166
		$mapent = array();
167
		$mapent['mac'] = $_POST['mac'];
168
		$mapent['ipaddr'] = $_POST['ipaddr'];
169
		$mapent['hostname'] = $_POST['hostname'];
170
		$mapent['descr'] = $_POST['descr'];
171
		$mapent['filename'] = $_POST['filename'];
172
		$mapent['rootpath'] = $_POST['rootpath'];
173

    
174
		if (isset($id) && $a_maps[$id])
175
			$a_maps[$id] = $mapent;
176
		else
177
			$a_maps[] = $mapent;
178
		staticmaps_sort($if);
179
		
180
		write_config();
181

    
182
		if(isset($config['dhcpd'][$if]['enable'])) {
183
			mark_subsystem_dirty('staticmaps');
184
			if (isset($config['dnsmasq']['regdhcpstatic']))	
185
				mark_subsystem_dirty('hosts');
186
		}
187

    
188
		header("Location: services_dhcp.php?if={$if}");
189
		exit;
190
	}
191
}
192

    
193
$pgtitle = array(gettext("Services"),gettext("DHCP"),gettext("Edit static mapping"));
194
$statusurl = "status_dhcp_leases.php";
195
$logurl = "diag_logs_dhcp.php";
196

    
197
include("head.inc");
198

    
199
?>
200

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