Project

General

Profile

Download (7.04 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
staticmaps_sort($if);
47
$a_maps = &$config['dhcpd'][$if]['staticmap'];
48
$ifcfg = &$config['interfaces'][$if];
49

    
50
$id = $_GET['id'];
51
if (isset($_POST['id']))
52
	$id = $_POST['id'];
53

    
54
if (isset($id) && $a_maps[$id]) {
55
        $pconfig['mac'] = $a_maps[$id]['mac'];
56
        $pconfig['ipaddr'] = $a_maps[$id]['ipaddr'];
57
        $pconfig['descr'] = $a_maps[$id]['descr'];
58
} else {
59
        $pconfig['mac'] = $_GET['mac'];
60
        $pconfig['descr'] = $_GET['descr'];
61
}
62

    
63
if ($_POST) {
64

    
65
	unset($input_errors);
66
	$pconfig = $_POST;
67

    
68
	/* input validation */
69
	$reqdfields = explode(" ", "mac");
70
	$reqdfieldsn = explode(",", "MAC address");
71
	
72
	do_input_validation($_POST, $reqdfields, $reqdfieldsn, &$input_errors);
73

    
74
	/* normalize MAC addresses - lowercase and convert Windows-ized hyphenated MACs to colon delimited */
75
	$_POST['mac'] = strtolower(str_replace("-", ":", $_POST['mac']));
76
	
77
	if (($_POST['ipaddr'] && !is_ipaddr($_POST['ipaddr']))) {
78
		$input_errors[] = "A valid IP address must be specified.";
79
	}
80
	if (($_POST['mac'] && !is_macaddr($_POST['mac']))) {
81
		$input_errors[] = "A valid MAC address must be specified.";
82
	}
83

    
84
	/* check for overlaps */
85
	foreach ($a_maps as $mapent) {
86
		if (isset($id) && ($a_maps[$id]) && ($a_maps[$id] === $mapent))
87
			continue;
88

    
89
		if (($mapent['mac'] == $_POST['mac']) || ($_POST['ipaddr'] && (ip2long($mapent['ipaddr']) == ip2long($_POST['ipaddr'])))) {
90
			$input_errors[] = "This IP or MAC address already exists.";
91
			break;
92
		}
93
	}
94
		
95
	/* make sure it's not within the dynamic subnet */
96
	if ($_POST['ipaddr']) {
97
		$dynsubnet_start = ip2long($config['dhcpd'][$if]['range']['from']);
98
		$dynsubnet_end = ip2long($config['dhcpd'][$if]['range']['to']);
99
		$lansubnet_start = (ip2long($ifcfg['ipaddr']) & gen_subnet_mask_long($ifcfg['subnet']));
100
		$lansubnet_end = (ip2long($ifcfg['ipaddr']) | (~gen_subnet_mask_long($ifcfg['subnet'])));
101
		
102
		if ((ip2long($_POST['ipaddr']) >= $dynsubnet_start) &&
103
				(ip2long($_POST['ipaddr']) <= $dynsubnet_end)) {
104
			$input_errors[] = "Static IP address falls within the dynamic client range.";
105
		}
106
		if ((ip2long($_POST['ipaddr']) < $lansubnet_start) ||
107
			(ip2long($_POST['ipaddr']) > $lansubnet_end)) {
108
			$input_errors[] = "The IP address must lie in the {$ifcfg['descr']} subnet.";
109
		}
110
	}
111

    
112
	if (!$input_errors) {
113
		$mapent = array();
114
		$mapent['mac'] = $_POST['mac'];
115
		$mapent['ipaddr'] = $_POST['ipaddr'];
116
		$mapent['descr'] = $_POST['descr'];
117

    
118
		if (isset($id) && $a_maps[$id])
119
			$a_maps[$id] = $mapent;
120
		else
121
			$a_maps[] = $mapent;
122
		
123
		write_config();
124

    
125
                if (isset($config['dhcpd'][$if]['staticarp']))
126
			interfaces_staticarp_configure($if);		
127

    
128
                $retval = 0;
129
                config_lock();
130
                $retval = services_dhcpd_configure();
131
                config_unlock();
132

    
133
                $savemsg = get_std_save_message($retval);
134

    
135
		header("Location: services_dhcp.php?if={$if}");
136
		exit;
137
	}
138
}
139

    
140
$pgtitle = "Services: DHCP: Edit static mapping";
141
include("head.inc");
142

    
143
?>
144

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