Project

General

Profile

Download (8.32 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
function staticmapcmp($a, $b) {
40
        return ipcmp($a['ipaddr'], $b['ipaddr']);
41
}
42

    
43
function staticmaps_sort($ifgui) {
44
        global $g, $config;
45

    
46
        usort($config['dhcpd'][$ifgui]['staticmap'], "staticmapcmp");
47
}
48

    
49
require_once('globals.inc');
50

    
51
if(!$g['services_dhcp_server_enable']) {
52
	Header("Location: /");
53
	exit;
54
}
55

    
56
require("guiconfig.inc");
57

    
58
$if = $_GET['if'];
59
if ($_POST['if'])
60
	$if = $_POST['if'];
61
	
62
if (!$if) {
63
	header("Location: services_dhcp.php");
64
	exit;
65
}
66

    
67
if (!is_array($config['dhcpd'][$if]['staticmap'])) {
68
	$config['dhcpd'][$if]['staticmap'] = array();
69
}
70

    
71
$static_map_enabled=isset($config['dhcpd'][$if]['staticarp']);
72

    
73
staticmaps_sort($if);
74
$a_maps = &$config['dhcpd'][$if]['staticmap'];
75
$ifcfgip = get_interface_ip($if);
76
$ifcfgsn = get_interface_subnet($if);
77
$ifcfgdescr = convert_friendly_interface_to_friendly_descr($if);
78

    
79
$id = $_GET['id'];
80
if (isset($_POST['id']))
81
	$id = $_POST['id'];
82

    
83
if (isset($id) && $a_maps[$id]) {
84
        $pconfig['mac'] = $a_maps[$id]['mac'];
85
		$pconfig['hostname'] = $a_maps[$id]['hostname'];
86
        $pconfig['ipaddr'] = $a_maps[$id]['ipaddr'];
87
        $pconfig['descr'] = $a_maps[$id]['descr'];
88
} else {
89
        $pconfig['mac'] = $_GET['mac'];
90
		$pconfig['hostname'] = $_GET['hostname'];
91
        $pconfig['descr'] = $_GET['descr'];
92
}
93

    
94
if ($_POST) {
95

    
96
	unset($input_errors);
97
	$pconfig = $_POST;
98

    
99
	/* input validation */
100
	$reqdfields = explode(" ", "mac");
101
	$reqdfieldsn = explode(",", "MAC address");
102
	
103
	do_input_validation($_POST, $reqdfields, $reqdfieldsn, &$input_errors);
104

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

    
126
		if ((($mapent['hostname'] == $_POST['hostname']) && $mapent['hostname'])  || ($mapent['mac'] == $_POST['mac'])) {
127
			$input_errors[] = "This Hostname, IP or MAC address already exists.";
128
			break;
129
		}
130
	}
131
		
132
	/* make sure it's not within the dynamic subnet */
133
	if ($_POST['ipaddr']) {
134
		$dynsubnet_start = ip2long($config['dhcpd'][$if]['range']['from']);
135
		$dynsubnet_end = ip2long($config['dhcpd'][$if]['range']['to']);
136
		$lansubnet_start = (ip2long($ifcfgip) & gen_subnet_mask_long($ifcfgsn));
137
		$lansubnet_end = (ip2long($ifcfgip) | (~gen_subnet_mask_long($ifcfgsn)));
138
		if ((ip2long($_POST['ipaddr']) < $lansubnet_start) ||
139
			(ip2long($_POST['ipaddr']) > $lansubnet_end)) {
140
			$input_errors[] = "The IP address must lie in the {$ifcfgdescr} subnet.";
141
		}
142
	}
143

    
144
	if (!$input_errors) {
145
		$mapent = array();
146
		$mapent['mac'] = $_POST['mac'];
147
		$mapent['ipaddr'] = $_POST['ipaddr'];
148
		$mapent['hostname'] = $_POST['hostname'];
149
		$mapent['descr'] = $_POST['descr'];
150

    
151
		staticmaps_sort($if);
152
		if (isset($id) && $a_maps[$id])
153
			$a_maps[$id] = $mapent;
154
		else
155
			$a_maps[] = $mapent;
156
		
157
		write_config();
158

    
159
		if(isset($config['dhcpd'][$if]['enable'])) {
160
			mark_subsystem_dirty('staticmaps');
161
			if (isset($config['dnsmasq']['regdhcpstatic']))	
162
				mark_subsystem_dirty('hosts');
163
		}
164

    
165
		header("Location: services_dhcp.php?if={$if}");
166
		exit;
167
	}
168
}
169

    
170
$pgtitle = array("Services","DHCP","Edit static mapping");
171
include("head.inc");
172

    
173
?>
174

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