Project

General

Profile

Download (7.06 KB) Statistics
| Branch: | Tag: | Revision:
1
#!/usr/local/bin/php
2
<?php 
3
/* $Id$ */
4
/*
5
	services_dhcp_edit.php
6
	part of m0n0wall (http://m0n0.ch/wall)
7
	
8
	Copyright (C) 2003-2004 Manuel Kasper <mk@neon1.net>.
9
	All rights reserved.
10
	
11
	Redistribution and use in source and binary forms, with or without
12
	modification, are permitted provided that the following conditions are met:
13
	
14
	1. Redistributions of source code must retain the above copyright notice,
15
	   this list of conditions and the following disclaimer.
16
	
17
	2. Redistributions in binary form must reproduce the above copyright
18
	   notice, this list of conditions and the following disclaimer in the
19
	   documentation and/or other materials provided with the distribution.
20
	
21
	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
22
	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
23
	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24
	AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
25
	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30
	POSSIBILITY OF SUCH DAMAGE.
31
*/
32

    
33
require("guiconfig.inc");
34

    
35
$if = $_GET['if'];
36
if ($_POST['if'])
37
	$if = $_POST['if'];
38
	
39
if (!$if) {
40
	header("Location: services_dhcp.php");
41
	exit;
42
}
43

    
44
if (!is_array($config['dhcpd'][$if]['staticmap'])) {
45
	$config['dhcpd'][$if]['staticmap'] = array();
46
}
47
staticmaps_sort($if);
48
$a_maps = &$config['dhcpd'][$if]['staticmap'];
49
$ifcfg = &$config['interfaces'][$if];
50

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

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

    
64
if ($_POST) {
65

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

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

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

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

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

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

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

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

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

    
134
                $savemsg = get_std_save_message($retval);
135

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

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

    
144
?>
145

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