Project

General

Profile

Download (7.57 KB) Statistics
| Branch: | Tag: | Revision:
1 5b237745 Scott Ullrich
<?php 
2 b46bfcf5 Bill Marquette
/* $Id$ */
3 5b237745 Scott Ullrich
/*
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 0baa37c5 Scott Ullrich
47
$static_map_enabled=isset($config['dhcpd'][$if]['staticarp']);
48
49 5b237745 Scott Ullrich
staticmaps_sort($if);
50
$a_maps = &$config['dhcpd'][$if]['staticmap'];
51
$ifcfg = &$config['interfaces'][$if];
52
53
$id = $_GET['id'];
54
if (isset($_POST['id']))
55
	$id = $_POST['id'];
56
57
if (isset($id) && $a_maps[$id]) {
58 3a32590b Scott Ullrich
        $pconfig['mac'] = $a_maps[$id]['mac'];
59 6a01ea44 Bill Marquette
		$pconfig['hostname'] = $a_maps[$id]['hostname'];
60 3a32590b Scott Ullrich
        $pconfig['ipaddr'] = $a_maps[$id]['ipaddr'];
61
        $pconfig['descr'] = $a_maps[$id]['descr'];
62
} else {
63
        $pconfig['mac'] = $_GET['mac'];
64 6a01ea44 Bill Marquette
		$pconfig['hostname'] = $_GET['hostname'];
65 7477df4f Scott Ullrich
        $pconfig['descr'] = $_GET['descr'];
66 5b237745 Scott Ullrich
}
67
68
if ($_POST) {
69
70
	unset($input_errors);
71
	$pconfig = $_POST;
72
73
	/* input validation */
74
	$reqdfields = explode(" ", "mac");
75
	$reqdfieldsn = explode(",", "MAC address");
76
	
77
	do_input_validation($_POST, $reqdfields, $reqdfieldsn, &$input_errors);
78 4f3401e0 Bill Marquette
79
	/* normalize MAC addresses - lowercase and convert Windows-ized hyphenated MACs to colon delimited */
80
	$_POST['mac'] = strtolower(str_replace("-", ":", $_POST['mac']));
81 5b237745 Scott Ullrich
	
82 6a01ea44 Bill Marquette
	if (($_POST['host'] && !is_hostname($_POST['host']))) {
83
		$input_errors[] = "A valid host name must be specified.";
84
	}
85 5b237745 Scott Ullrich
	if (($_POST['ipaddr'] && !is_ipaddr($_POST['ipaddr']))) {
86
		$input_errors[] = "A valid IP address must be specified.";
87
	}
88
	if (($_POST['mac'] && !is_macaddr($_POST['mac']))) {
89
		$input_errors[] = "A valid MAC address must be specified.";
90
	}
91 0baa37c5 Scott Ullrich
	if($static_map_enabled && !$_POST['ipaddr']) {
92
		$input_errors[] = "Static map is enabled.  You must specify an IP address.";
93
	}
94
	
95 5b237745 Scott Ullrich
	/* check for overlaps */
96
	foreach ($a_maps as $mapent) {
97
		if (isset($id) && ($a_maps[$id]) && ($a_maps[$id] === $mapent))
98
			continue;
99
100 f15f4b4d Scott Ullrich
		if ((($mapent['hostname'] == $_POST['hostname']) && $mapent['hostname'])  || ($mapent['mac'] == $_POST['mac'])) {
101 cfc5a090 Scott Ullrich
			$input_errors[] = "This Hostname, IP or MAC address already exists.";
102 5b237745 Scott Ullrich
			break;
103
		}
104
	}
105
		
106
	/* make sure it's not within the dynamic subnet */
107
	if ($_POST['ipaddr']) {
108
		$dynsubnet_start = ip2long($config['dhcpd'][$if]['range']['from']);
109
		$dynsubnet_end = ip2long($config['dhcpd'][$if]['range']['to']);
110
		$lansubnet_start = (ip2long($ifcfg['ipaddr']) & gen_subnet_mask_long($ifcfg['subnet']));
111
		$lansubnet_end = (ip2long($ifcfg['ipaddr']) | (~gen_subnet_mask_long($ifcfg['subnet'])));
112
		if ((ip2long($_POST['ipaddr']) < $lansubnet_start) ||
113
			(ip2long($_POST['ipaddr']) > $lansubnet_end)) {
114
			$input_errors[] = "The IP address must lie in the {$ifcfg['descr']} subnet.";
115
		}
116
	}
117
118
	if (!$input_errors) {
119
		$mapent = array();
120
		$mapent['mac'] = $_POST['mac'];
121
		$mapent['ipaddr'] = $_POST['ipaddr'];
122 6a01ea44 Bill Marquette
		$mapent['hostname'] = $_POST['hostname'];
123 5b237745 Scott Ullrich
		$mapent['descr'] = $_POST['descr'];
124
125
		if (isset($id) && $a_maps[$id])
126
			$a_maps[$id] = $mapent;
127
		else
128
			$a_maps[] = $mapent;
129
		
130
		write_config();
131 608ef5e7 Scott Ullrich
132 6a01ea44 Bill Marquette
		if(isset($config['dhcpd'][$if]['enable'])) {
133
			touch($d_staticmapsdirty_path);
134
			if (isset($config['dnsmasq']['regdhcpstatic']))	
135
				touch($d_hostsdirty_path);
136
		}
137 e4da84d0 Bill Marquette
138 5b237745 Scott Ullrich
		header("Location: services_dhcp.php?if={$if}");
139
		exit;
140
	}
141
}
142 4df96eff Scott Ullrich
143 d88c6a9f Scott Ullrich
$pgtitle = array("Services","DHCP","Edit static mapping");
144 4df96eff Scott Ullrich
include("head.inc");
145
146 5b237745 Scott Ullrich
?>
147
148
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
149
<?php include("fbegin.inc"); ?>
150
<?php if ($input_errors) print_input_errors($input_errors); ?>
151
            <form action="services_dhcp_edit.php" method="post" name="iform" id="iform">
152
              <table width="100%" border="0" cellpadding="6" cellspacing="0">
153
                <tr> 
154
                  <td width="22%" valign="top" class="vncellreq">MAC address</td>
155
                  <td width="78%" class="vtable"> 
156 b5c78501 Seth Mos
                    <input name="mac" type="text" class="formfld unknown" id="mac" size="30" value="<?=htmlspecialchars($pconfig['mac']);?>">
157 f183b092 Scott Ullrich
		    <?php
158
			$ip = getenv('REMOTE_ADDR');
159
			$mac = `/usr/sbin/arp -an | grep {$ip} | cut -d" " -f4`;
160
			$mac = str_replace("\n","",$mac);
161
		    ?>
162
		    <a OnClick="document.forms[0].mac.value='<?=$mac?>';" href="#">Copy my MAC address</a>   		    
163 5b237745 Scott Ullrich
                    <br>
164
                    <span class="vexpl">Enter a MAC address in the following format: 
165
                    xx:xx:xx:xx:xx:xx</span></td>
166
                </tr>
167
                <tr> 
168
                  <td width="22%" valign="top" class="vncell">IP address</td>
169
                  <td width="78%" class="vtable"> 
170 b5c78501 Seth Mos
                    <input name="ipaddr" type="text" class="formfld unknown" id="ipaddr" size="20" value="<?=htmlspecialchars($pconfig['ipaddr']);?>">
171 5b237745 Scott Ullrich
                    <br>
172
                    If no IP address is given, one will be dynamically allocated  from the pool.</td>
173
                </tr>
174 6a01ea44 Bill Marquette
                <tr> 
175
                  <td width="22%" valign="top" class="vncell">Hostname</td>
176
                  <td width="78%" class="vtable"> 
177 b5c78501 Seth Mos
                    <input name="hostname" type="text" class="formfld unknown" id="hostname" size="20" value="<?=htmlspecialchars($pconfig['hostname']);?>">
178 6a01ea44 Bill Marquette
                    <br> <span class="vexpl">Name of the host, without domain part.</span></td>
179
                </tr>				
180 5b237745 Scott Ullrich
                <tr> 
181
                  <td width="22%" valign="top" class="vncell">Description</td>
182
                  <td width="78%" class="vtable"> 
183 b5c78501 Seth Mos
                    <input name="descr" type="text" class="formfld unknown" id="descr" size="40" value="<?=htmlspecialchars($pconfig['descr']);?>"> 
184 5b237745 Scott Ullrich
                    <br> <span class="vexpl">You may enter a description here 
185
                    for your reference (not parsed).</span></td>
186
                </tr>
187
                <tr> 
188
                  <td width="22%" valign="top">&nbsp;</td>
189
                  <td width="78%"> 
190 fc01e414 Scott Ullrich
                    <input name="Submit" type="submit" class="formbtn" value="Save"> <input class="formbtn" type="button" value="Cancel" onclick="history.back()">
191 5b237745 Scott Ullrich
                    <?php if (isset($id) && $a_maps[$id]): ?>
192
                    <input name="id" type="hidden" value="<?=$id;?>">
193
                    <?php endif; ?>
194
                    <input name="if" type="hidden" value="<?=$if;?>"> 
195
                  </td>
196
                </tr>
197
              </table>
198
</form>
199
<?php include("fend.inc"); ?>
200
</body>
201
</html>