Project

General

Profile

Download (8.07 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
require_once('globals.inc');
40

    
41
if(!$g['services_dhcp_server_enable']) {
42
	Header("Location: /");
43
	exit;
44
}
45

    
46
require("guiconfig.inc");
47

    
48
$if = $_GET['if'];
49
if ($_POST['if'])
50
	$if = $_POST['if'];
51
	
52
if (!$if) {
53
	header("Location: services_dhcp.php");
54
	exit;
55
}
56

    
57
if (!is_array($config['dhcpd'][$if]['staticmap'])) {
58
	$config['dhcpd'][$if]['staticmap'] = array();
59
}
60

    
61
$static_map_enabled=isset($config['dhcpd'][$if]['staticarp']);
62

    
63
staticmaps_sort($if);
64
$a_maps = &$config['dhcpd'][$if]['staticmap'];
65
$ifcfgip = get_interface_ip($if);
66
$ifcfgsn = get_interface_subnet($if);
67
$ifcfgdescr = convert_friendly_interface_to_friendly_descr($if);
68

    
69
$id = $_GET['id'];
70
if (isset($_POST['id']))
71
	$id = $_POST['id'];
72

    
73
if (isset($id) && $a_maps[$id]) {
74
        $pconfig['mac'] = $a_maps[$id]['mac'];
75
		$pconfig['hostname'] = $a_maps[$id]['hostname'];
76
        $pconfig['ipaddr'] = $a_maps[$id]['ipaddr'];
77
        $pconfig['descr'] = $a_maps[$id]['descr'];
78
} else {
79
        $pconfig['mac'] = $_GET['mac'];
80
		$pconfig['hostname'] = $_GET['hostname'];
81
        $pconfig['descr'] = $_GET['descr'];
82
}
83

    
84
if ($_POST) {
85

    
86
	unset($input_errors);
87
	$pconfig = $_POST;
88

    
89
	/* input validation */
90
	$reqdfields = explode(" ", "mac");
91
	$reqdfieldsn = explode(",", "MAC address");
92
	
93
	do_input_validation($_POST, $reqdfields, $reqdfieldsn, &$input_errors);
94

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

    
116
		if ((($mapent['hostname'] == $_POST['hostname']) && $mapent['hostname'])  || ($mapent['mac'] == $_POST['mac'])) {
117
			$input_errors[] = "This Hostname, IP or MAC address already exists.";
118
			break;
119
		}
120
	}
121
		
122
	/* make sure it's not within the dynamic subnet */
123
	if ($_POST['ipaddr']) {
124
		$dynsubnet_start = ip2long($config['dhcpd'][$if]['range']['from']);
125
		$dynsubnet_end = ip2long($config['dhcpd'][$if]['range']['to']);
126
		$lansubnet_start = (ip2long($ifcfgip) & gen_subnet_mask_long($ifcfgsn));
127
		$lansubnet_end = (ip2long($ifcfgip) | (~gen_subnet_mask_long($ifcfgsn)));
128
		if ((ip2long($_POST['ipaddr']) < $lansubnet_start) ||
129
			(ip2long($_POST['ipaddr']) > $lansubnet_end)) {
130
			$input_errors[] = "The IP address must lie in the {$ifcfgdescr} subnet.";
131
		}
132
	}
133

    
134
	if (!$input_errors) {
135
		$mapent = array();
136
		$mapent['mac'] = $_POST['mac'];
137
		$mapent['ipaddr'] = $_POST['ipaddr'];
138
		$mapent['hostname'] = $_POST['hostname'];
139
		$mapent['descr'] = $_POST['descr'];
140

    
141
		if (isset($id) && $a_maps[$id])
142
			$a_maps[$id] = $mapent;
143
		else
144
			$a_maps[] = $mapent;
145
		
146
		write_config();
147

    
148
		if(isset($config['dhcpd'][$if]['enable'])) {
149
			touch($d_staticmapsdirty_path);
150
			if (isset($config['dnsmasq']['regdhcpstatic']))	
151
				touch($d_hostsdirty_path);
152
		}
153

    
154
		header("Location: services_dhcp.php?if={$if}");
155
		exit;
156
	}
157
}
158

    
159
$pgtitle = array("Services","DHCP","Edit static mapping");
160
include("head.inc");
161

    
162
?>
163

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