Project

General

Profile

Download (10.1 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['netbootfile'] = $a_maps[$id]['netbootfile'];
91
        $pconfig['descr'] = $a_maps[$id]['descr'];
92
} else {
93
        $pconfig['mac'] = $_GET['mac'];
94
		$pconfig['hostname'] = $_GET['hostname'];
95
	$pconfig['netbootfile'] = $_GET['netbootfile'];
96
        $pconfig['descr'] = $_GET['descr'];
97
}
98

    
99
if ($_POST) {
100

    
101
	unset($input_errors);
102
	$pconfig = $_POST;
103

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

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

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

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

    
163
	if (!$input_errors) {
164
		$mapent = array();
165
		$mapent['mac'] = $_POST['mac'];
166
		$mapent['ipaddr'] = $_POST['ipaddr'];
167
		$mapent['hostname'] = $_POST['hostname'];
168
		$mapent['descr'] = $_POST['descr'];
169
		$mapent['netbootfile'] = $_POST['netbootfile'];
170

    
171
		if (isset($id) && $a_maps[$id])
172
			$a_maps[$id] = $mapent;
173
		else
174
			$a_maps[] = $mapent;
175
		staticmaps_sort($if);
176
		
177
		write_config();
178

    
179
		if(isset($config['dhcpd'][$if]['enable'])) {
180
			mark_subsystem_dirty('staticmaps');
181
			if (isset($config['dnsmasq']['regdhcpstatic']))	
182
				mark_subsystem_dirty('hosts');
183
		}
184

    
185
		header("Location: services_dhcp.php?if={$if}");
186
		exit;
187
	}
188
}
189

    
190
$pgtitle = array(gettext("Services"),gettext("DHCP"),gettext("Edit static mapping"));
191
$statusurl = "status_dhcp_leases.php";
192
$logurl = "diag_logs_dhcp.php";
193

    
194
include("head.inc");
195

    
196
?>
197

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