Project

General

Profile

Download (7.98 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
if(!$g['services_dhcp_server_enable']) {
40
	Header("Location: /");
41
	exit;
42
}
43

    
44
require("guiconfig.inc");
45

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

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

    
59
$static_map_enabled=isset($config['dhcpd'][$if]['staticarp']);
60

    
61
staticmaps_sort($if);
62
$a_maps = &$config['dhcpd'][$if]['staticmap'];
63
$ifcfg = &$config['interfaces'][$if];
64

    
65
$id = $_GET['id'];
66
if (isset($_POST['id']))
67
	$id = $_POST['id'];
68

    
69
if (isset($id) && $a_maps[$id]) {
70
        $pconfig['mac'] = $a_maps[$id]['mac'];
71
		$pconfig['hostname'] = $a_maps[$id]['hostname'];
72
        $pconfig['ipaddr'] = $a_maps[$id]['ipaddr'];
73
        $pconfig['descr'] = $a_maps[$id]['descr'];
74
} else {
75
        $pconfig['mac'] = $_GET['mac'];
76
		$pconfig['hostname'] = $_GET['hostname'];
77
        $pconfig['descr'] = $_GET['descr'];
78
}
79

    
80
if ($_POST) {
81

    
82
	unset($input_errors);
83
	$pconfig = $_POST;
84

    
85
	/* input validation */
86
	$reqdfields = explode(" ", "mac");
87
	$reqdfieldsn = explode(",", "MAC address");
88
	
89
	do_input_validation($_POST, $reqdfields, $reqdfieldsn, &$input_errors);
90

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

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

    
130
	if (!$input_errors) {
131
		$mapent = array();
132
		$mapent['mac'] = $_POST['mac'];
133
		$mapent['ipaddr'] = $_POST['ipaddr'];
134
		$mapent['hostname'] = $_POST['hostname'];
135
		$mapent['descr'] = $_POST['descr'];
136

    
137
		if (isset($id) && $a_maps[$id])
138
			$a_maps[$id] = $mapent;
139
		else
140
			$a_maps[] = $mapent;
141
		
142
		write_config();
143

    
144
		if(isset($config['dhcpd'][$if]['enable'])) {
145
			touch($d_staticmapsdirty_path);
146
			if (isset($config['dnsmasq']['regdhcpstatic']))	
147
				touch($d_hostsdirty_path);
148
		}
149

    
150
		header("Location: services_dhcp.php?if={$if}");
151
		exit;
152
	}
153
}
154

    
155
$pgtitle = array("Services","DHCP","Edit static mapping");
156
include("head.inc");
157

    
158
?>
159

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