Project

General

Profile

Download (7.65 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
require("guiconfig.inc");
33

    
34
$if = $_GET['if'];
35
if ($_POST['if'])
36
	$if = $_POST['if'];
37
	
38
if (!$if) {
39
	header("Location: services_dhcp.php");
40
	exit;
41
}
42

    
43
if (!is_array($config['dhcpd'][$if]['staticmap'])) {
44
	$config['dhcpd'][$if]['staticmap'] = array();
45
}
46
staticmaps_sort($if);
47
$a_maps = &$config['dhcpd'][$if]['staticmap'];
48
$ifcfg = &$config['interfaces'][$if];
49

    
50
$id = $_GET['id'];
51
if (isset($_POST['id']))
52
	$id = $_POST['id'];
53

    
54
if (isset($id) && $a_maps[$id]) {
55
        $pconfig['mac'] = $a_maps[$id]['mac'];
56
		$pconfig['hostname'] = $a_maps[$id]['hostname'];
57
        $pconfig['ipaddr'] = $a_maps[$id]['ipaddr'];
58
        $pconfig['descr'] = $a_maps[$id]['descr'];
59
} else {
60
        $pconfig['mac'] = $_GET['mac'];
61
		$pconfig['hostname'] = $_GET['hostname'];
62
        $pconfig['descr'] = $_GET['descr'];
63
}
64

    
65
if ($_POST) {
66

    
67
	unset($input_errors);
68
	$pconfig = $_POST;
69

    
70
	/* input validation */
71
	$reqdfields = explode(" ", "mac");
72
	$reqdfieldsn = explode(",", "MAC address");
73
	
74
	do_input_validation($_POST, $reqdfields, $reqdfieldsn, &$input_errors);
75

    
76
	/* normalize MAC addresses - lowercase and convert Windows-ized hyphenated MACs to colon delimited */
77
	$_POST['mac'] = strtolower(str_replace("-", ":", $_POST['mac']));
78
	
79
	if (($_POST['host'] && !is_hostname($_POST['host']))) {
80
		$input_errors[] = "A valid host name must be specified.";
81
	}
82
	if (($_POST['ipaddr'] && !is_ipaddr($_POST['ipaddr']))) {
83
		$input_errors[] = "A valid IP address must be specified.";
84
	}
85
	if (($_POST['mac'] && !is_macaddr($_POST['mac']))) {
86
		$input_errors[] = "A valid MAC address must be specified.";
87
	}
88

    
89
	/* check for overlaps */
90
	foreach ($a_maps as $mapent) {
91
		if (isset($id) && ($a_maps[$id]) && ($a_maps[$id] === $mapent))
92
			continue;
93

    
94
		if ((($mapent['hostname'] == $_POST['hostname']) && $mapent['hostname'])  || ($mapent['mac'] == $_POST['mac']) || ($_POST['ipaddr'] && (ip2long($mapent['ipaddr']) == ip2long($_POST['ipaddr'])))) {
95
			$input_errors[] = "This Hostname, IP or MAC address already exists.";
96
			break;
97
		}
98
	}
99
		
100
	/* make sure it's not within the dynamic subnet */
101
	if ($_POST['ipaddr']) {
102
		$dynsubnet_start = ip2long($config['dhcpd'][$if]['range']['from']);
103
		$dynsubnet_end = ip2long($config['dhcpd'][$if]['range']['to']);
104
		$lansubnet_start = (ip2long($ifcfg['ipaddr']) & gen_subnet_mask_long($ifcfg['subnet']));
105
		$lansubnet_end = (ip2long($ifcfg['ipaddr']) | (~gen_subnet_mask_long($ifcfg['subnet'])));
106
		
107
		if ((ip2long($_POST['ipaddr']) >= $dynsubnet_start) &&
108
				(ip2long($_POST['ipaddr']) <= $dynsubnet_end)) {
109
			$input_errors[] = "Static IP address falls within the dynamic client range.";
110
		}
111
		if ((ip2long($_POST['ipaddr']) < $lansubnet_start) ||
112
			(ip2long($_POST['ipaddr']) > $lansubnet_end)) {
113
			$input_errors[] = "The IP address must lie in the {$ifcfg['descr']} subnet.";
114
		}
115
	}
116

    
117
	if (!$input_errors) {
118
		$mapent = array();
119
		$mapent['mac'] = $_POST['mac'];
120
		$mapent['ipaddr'] = $_POST['ipaddr'];
121
		$mapent['hostname'] = $_POST['hostname'];
122
		$mapent['descr'] = $_POST['descr'];
123

    
124
		if (isset($id) && $a_maps[$id])
125
			$a_maps[$id] = $mapent;
126
		else
127
			$a_maps[] = $mapent;
128
		
129
		write_config();
130

    
131
		if(isset($config['dhcpd'][$if]['enable'])) {
132
			touch($d_staticmapsdirty_path);
133
			if (isset($config['dnsmasq']['regdhcpstatic']))	
134
				touch($d_hostsdirty_path);
135
		}
136

    
137
		header("Location: services_dhcp.php?if={$if}");
138
		exit;
139
	}
140
}
141

    
142
$pgtitle = "Services: DHCP: Edit static mapping";
143
include("head.inc");
144

    
145
?>
146

    
147
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
148
<?php include("fbegin.inc"); ?>
149
<p class="pgtitle"><?=$pgtitle?></p>
150
<?php if ($input_errors) print_input_errors($input_errors); ?>
151
            <form action="services_dhcp_edit.php" method="post" name="iform" id="iform">
152
              <table width="100%" border="0" cellpadding="6" cellspacing="0">
153
                <tr> 
154
                  <td width="22%" valign="top" class="vncellreq">MAC address</td>
155
                  <td width="78%" class="vtable"> 
156
                    <input name="mac" type="text" class="formfld" id="mac" size="30" value="<?=htmlspecialchars($pconfig['mac']);?>">
157
		    <?php
158
			$ip = getenv('REMOTE_ADDR');
159
			$mac = `/usr/sbin/arp -an | grep {$ip} | cut -d" " -f4`;
160
			$mac = str_replace("\n","",$mac);
161
		    ?>
162
		    <a OnClick="document.forms[0].mac.value='<?=$mac?>';" href="#">Copy my MAC address</a>   		    
163
                    <br>
164
                    <span class="vexpl">Enter a MAC address in the following format: 
165
                    xx:xx:xx:xx:xx:xx</span></td>
166
                </tr>
167
                <tr> 
168
                  <td width="22%" valign="top" class="vncell">IP address</td>
169
                  <td width="78%" class="vtable"> 
170
                    <input name="ipaddr" type="text" class="formfld" id="ipaddr" size="20" value="<?=htmlspecialchars($pconfig['ipaddr']);?>">
171
                    <br>
172
                    If no IP address is given, one will be dynamically allocated  from the pool.</td>
173
                </tr>
174
                <tr> 
175
                  <td width="22%" valign="top" class="vncell">Hostname</td>
176
                  <td width="78%" class="vtable"> 
177
                    <input name="hostname" type="text" class="formfld" id="hostname" size="20" value="<?=htmlspecialchars($pconfig['hostname']);?>">
178
                    <br> <span class="vexpl">Name of the host, without domain part.</span></td>
179
                </tr>				
180
                <tr> 
181
                  <td width="22%" valign="top" class="vncell">Description</td>
182
                  <td width="78%" class="vtable"> 
183
                    <input name="descr" type="text" class="formfld" id="descr" size="40" value="<?=htmlspecialchars($pconfig['descr']);?>"> 
184
                    <br> <span class="vexpl">You may enter a description here 
185
                    for your reference (not parsed).</span></td>
186
                </tr>
187
                <tr> 
188
                  <td width="22%" valign="top">&nbsp;</td>
189
                  <td width="78%"> 
190
                    <input name="Submit" type="submit" class="formbtn" value="Save"> <input class="formbtn" type="button" value="Cancel" onclick="history.back()">
191
                    <?php if (isset($id) && $a_maps[$id]): ?>
192
                    <input name="id" type="hidden" value="<?=$id;?>">
193
                    <?php endif; ?>
194
                    <input name="if" type="hidden" value="<?=$if;?>"> 
195
                  </td>
196
                </tr>
197
              </table>
198
</form>
199
<?php include("fend.inc"); ?>
200
</body>
201
</html>
(110-110/175)