Project

General

Profile

Download (7.76 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

    
47
$static_map_enabled=isset($config['dhcpd'][$if]['staticarp']);
48

    
49
staticmaps_sort($if);
50
$a_maps = &$config['dhcpd'][$if]['staticmap'];
51
$ifcfg = &$config['interfaces'][$if];
52

    
53
$id = $_GET['id'];
54
if (isset($_POST['id']))
55
	$id = $_POST['id'];
56

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

    
68
if ($_POST) {
69

    
70
	unset($input_errors);
71
	$pconfig = $_POST;
72

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

    
79
	/* normalize MAC addresses - lowercase and convert Windows-ized hyphenated MACs to colon delimited */
80
	$_POST['mac'] = strtolower(str_replace("-", ":", $_POST['mac']));
81
	
82
	if (($_POST['host'] && !is_hostname($_POST['host']))) {
83
		$input_errors[] = "A valid host name must be specified.";
84
	}
85
	if (($_POST['ipaddr'] && !is_ipaddr($_POST['ipaddr']))) {
86
		$input_errors[] = "A valid IP address must be specified.";
87
	}
88
	if (($_POST['mac'] && !is_macaddr($_POST['mac']))) {
89
		$input_errors[] = "A valid MAC address must be specified.";
90
	}
91
	if($static_map_enabled && !$_POST['ipaddr']) {
92
		$input_errors[] = "Static map is enabled.  You must specify an IP address.";
93
	}
94
	
95
	/* check for overlaps */
96
	foreach ($a_maps as $mapent) {
97
		if (isset($id) && ($a_maps[$id]) && ($a_maps[$id] === $mapent))
98
			continue;
99

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

    
123
	if (!$input_errors) {
124
		$mapent = array();
125
		$mapent['mac'] = $_POST['mac'];
126
		$mapent['ipaddr'] = $_POST['ipaddr'];
127
		$mapent['hostname'] = $_POST['hostname'];
128
		$mapent['descr'] = $_POST['descr'];
129

    
130
		if (isset($id) && $a_maps[$id])
131
			$a_maps[$id] = $mapent;
132
		else
133
			$a_maps[] = $mapent;
134
		
135
		write_config();
136

    
137
		if(isset($config['dhcpd'][$if]['enable'])) {
138
			touch($d_staticmapsdirty_path);
139
			if (isset($config['dnsmasq']['regdhcpstatic']))	
140
				touch($d_hostsdirty_path);
141
		}
142

    
143
		header("Location: services_dhcp.php?if={$if}");
144
		exit;
145
	}
146
}
147

    
148
$pgtitle = array("Services","DHCP","Edit static mapping");
149
include("head.inc");
150

    
151
?>
152

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