Project

General

Profile

Download (6.42 KB) Statistics
| Branch: | Tag: | Revision:
1
#!/usr/local/bin/php
2
<?php 
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
}
59

    
60
if ($_POST) {
61

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

    
65
	/* input validation */
66
	$reqdfields = explode(" ", "mac");
67
	$reqdfieldsn = explode(",", "MAC address");
68
	
69
	do_input_validation($_POST, $reqdfields, $reqdfieldsn, &$input_errors);
70
	
71
	if (($_POST['ipaddr'] && !is_ipaddr($_POST['ipaddr']))) {
72
		$input_errors[] = "A valid IP address must be specified.";
73
	}
74
	if (($_POST['mac'] && !is_macaddr($_POST['mac']))) {
75
		$input_errors[] = "A valid MAC address must be specified.";
76
	}
77

    
78
	/* check for overlaps */
79
	foreach ($a_maps as $mapent) {
80
		if (isset($id) && ($a_maps[$id]) && ($a_maps[$id] === $mapent))
81
			continue;
82

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

    
106
	if (!$input_errors) {
107
		$mapent = array();
108
		$mapent['mac'] = $_POST['mac'];
109
		$mapent['ipaddr'] = $_POST['ipaddr'];
110
		$mapent['descr'] = $_POST['descr'];
111

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

    
134
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
135
<?php include("fbegin.inc"); ?>
136
<p class="pgtitle">Services: DHCP: Edit static mapping</p>
137
<?php if ($input_errors) print_input_errors($input_errors); ?>
138
            <form action="services_dhcp_edit.php" method="post" name="iform" id="iform">
139
              <table width="100%" border="0" cellpadding="6" cellspacing="0">
140
                <tr> 
141
                  <td width="22%" valign="top" class="vncellreq">MAC address</td>
142
                  <td width="78%" class="vtable"> 
143
                    <input name="mac" type="text" class="formfld" id="mac" size="30" value="<?=htmlspecialchars($pconfig['mac']);?>"> 
144
                    <br>
145
                    <span class="vexpl">Enter a MAC address in the following format: 
146
                    xx:xx:xx:xx:xx:xx</span></td>
147
                </tr>
148
                <tr> 
149
                  <td width="22%" valign="top" class="vncell">IP address</td>
150
                  <td width="78%" class="vtable"> 
151
                    <input name="ipaddr" type="text" class="formfld" id="ipaddr" size="20" value="<?=htmlspecialchars($pconfig['ipaddr']);?>">
152
                    <br>
153
                    If no IP address is given, one will be dynamically allocated  from the pool.</td>
154
                </tr>
155
                <tr> 
156
                  <td width="22%" valign="top" class="vncell">Description</td>
157
                  <td width="78%" class="vtable"> 
158
                    <input name="descr" type="text" class="formfld" id="descr" size="40" value="<?=htmlspecialchars($pconfig['descr']);?>"> 
159
                    <br> <span class="vexpl">You may enter a description here 
160
                    for your reference (not parsed).</span></td>
161
                </tr>
162
                <tr> 
163
                  <td width="22%" valign="top">&nbsp;</td>
164
                  <td width="78%"> 
165
                    <input name="Submit" type="submit" class="formbtn" value="Save"> 
166
                    <?php if (isset($id) && $a_maps[$id]): ?>
167
                    <input name="id" type="hidden" value="<?=$id;?>">
168
                    <?php endif; ?>
169
                    <input name="if" type="hidden" value="<?=$if;?>"> 
170
                  </td>
171
                </tr>
172
              </table>
173
</form>
174
<?php include("fend.inc"); ?>
175
</body>
176
</html>
(56-56/86)