Project

General

Profile

Download (6.43 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
	if (($_POST['ipaddr'] && !is_ipaddr($_POST['ipaddr']))) {
73
		$input_errors[] = "A valid IP address must be specified.";
74
	}
75
	if (($_POST['mac'] && !is_macaddr($_POST['mac']))) {
76
		$input_errors[] = "A valid MAC address must be specified.";
77
	}
78

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

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

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

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

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