Project

General

Profile

Download (6.92 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
}
60

    
61
if ($_POST) {
62

    
63
	unset($input_errors);
64
	$pconfig = $_POST;
65

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

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

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

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

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

    
116
		if (isset($id) && $a_maps[$id])
117
			$a_maps[$id] = $mapent;
118
		else
119
			$a_maps[] = $mapent;
120
		
121
		touch($d_staticmapsdirty_path);
122
		
123
		write_config();
124
		
125
		header("Location: services_dhcp.php?if={$if}");
126
		exit;
127
	}
128
}
129
?>
130
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
131
<html>
132
<head>
133
<title><?=gentitle("Services: DHCP: Edit static mapping");?></title>
134
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
135
<link href="gui.css" rel="stylesheet" type="text/css">
136
</head>
137

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