Project

General

Profile

Download (7.9 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
##|+PRIV
33
##|*IDENT=page-services-dhcpserver-editstaticmapping
34
##|*NAME=Services: DHCP Server : Edit static mapping page
35
##|*DESCR=Allow access to the 'Services: DHCP Server : Edit static mapping' page.
36
##|*MATCH=services_dhcp_edit.php*
37
##|-PRIV
38

    
39

    
40
require("guiconfig.inc");
41

    
42
$if = $_GET['if'];
43
if ($_POST['if'])
44
	$if = $_POST['if'];
45
	
46
if (!$if) {
47
	header("Location: services_dhcp.php");
48
	exit;
49
}
50

    
51
if (!is_array($config['dhcpd'][$if]['staticmap'])) {
52
	$config['dhcpd'][$if]['staticmap'] = array();
53
}
54

    
55
$static_map_enabled=isset($config['dhcpd'][$if]['staticarp']);
56

    
57
staticmaps_sort($if);
58
$a_maps = &$config['dhcpd'][$if]['staticmap'];
59
$ifcfg = &$config['interfaces'][$if];
60

    
61
$id = $_GET['id'];
62
if (isset($_POST['id']))
63
	$id = $_POST['id'];
64

    
65
if (isset($id) && $a_maps[$id]) {
66
        $pconfig['mac'] = $a_maps[$id]['mac'];
67
		$pconfig['hostname'] = $a_maps[$id]['hostname'];
68
        $pconfig['ipaddr'] = $a_maps[$id]['ipaddr'];
69
        $pconfig['descr'] = $a_maps[$id]['descr'];
70
} else {
71
        $pconfig['mac'] = $_GET['mac'];
72
		$pconfig['hostname'] = $_GET['hostname'];
73
        $pconfig['descr'] = $_GET['descr'];
74
}
75

    
76
if ($_POST) {
77

    
78
	unset($input_errors);
79
	$pconfig = $_POST;
80

    
81
	/* input validation */
82
	$reqdfields = explode(" ", "mac");
83
	$reqdfieldsn = explode(",", "MAC address");
84
	
85
	do_input_validation($_POST, $reqdfields, $reqdfieldsn, &$input_errors);
86

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

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

    
126
	if (!$input_errors) {
127
		$mapent = array();
128
		$mapent['mac'] = $_POST['mac'];
129
		$mapent['ipaddr'] = $_POST['ipaddr'];
130
		$mapent['hostname'] = $_POST['hostname'];
131
		$mapent['descr'] = $_POST['descr'];
132

    
133
		if (isset($id) && $a_maps[$id])
134
			$a_maps[$id] = $mapent;
135
		else
136
			$a_maps[] = $mapent;
137
		
138
		write_config();
139

    
140
		if(isset($config['dhcpd'][$if]['enable'])) {
141
			touch($d_staticmapsdirty_path);
142
			if (isset($config['dnsmasq']['regdhcpstatic']))	
143
				touch($d_hostsdirty_path);
144
		}
145

    
146
		header("Location: services_dhcp.php?if={$if}");
147
		exit;
148
	}
149
}
150

    
151
$pgtitle = array("Services","DHCP","Edit static mapping");
152
include("head.inc");
153

    
154
?>
155

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