Project

General

Profile

Download (16.9 KB) Statistics
| Branch: | Tag: | Revision:
1
#!/usr/local/bin/php
2
<?php 
3
/* $Id$ */
4
/*
5
	services_dhcp.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
$iflist = array("lan" => "LAN");
40

    
41
for ($i = 1; isset($config['interfaces']['opt' . $i]); $i++) {
42
	$oc = $config['interfaces']['opt' . $i];
43
	
44
	if (isset($oc['enable']) && $oc['if'] && (!$oc['bridge'])) {
45
		$iflist['opt' . $i] = $oc['descr'];
46
	}
47
}
48

    
49
if (!$if || !isset($iflist[$if]))
50
	$if = "lan";
51

    
52
$pconfig['range_from'] = $config['dhcpd'][$if]['range']['from'];
53
$pconfig['range_to'] = $config['dhcpd'][$if]['range']['to'];
54
$pconfig['deftime'] = $config['dhcpd'][$if]['defaultleasetime'];
55
$pconfig['maxtime'] = $config['dhcpd'][$if]['maxleasetime'];
56
$pconfig['gateway'] = $config['dhcpd'][$if]['gateway'];
57
list($pconfig['wins1'],$pconfig['wins2']) = $config['dhcpd'][$if]['winsserver'];
58
$pconfig['enable'] = isset($config['dhcpd'][$if]['enable']);
59
$pconfig['denyunknown'] = isset($config['dhcpd'][$if]['denyunknown']);
60
$pconfig['staticarp'] = isset($config['dhcpd'][$if]['staticarp']);
61

    
62

    
63
$ifcfg = $config['interfaces'][$if];
64

    
65
if (!is_array($config['dhcpd'][$if]['staticmap'])) {
66
	$config['dhcpd'][$if]['staticmap'] = array();
67
}
68
staticmaps_sort($if);
69
$a_maps = &$config['dhcpd'][$if]['staticmap'];
70

    
71

    
72
if ($_POST) {
73

    
74
	unset($input_errors);
75

    
76
	$pconfig = $_POST;
77

    
78
	/* input validation */
79
	if ($_POST['enable']) {
80
		$reqdfields = explode(" ", "range_from range_to");
81
		$reqdfieldsn = explode(",", "Range begin,Range end");
82
		
83
		do_input_validation($_POST, $reqdfields, $reqdfieldsn, &$input_errors);
84
		
85
		if (($_POST['range_from'] && !is_ipaddr($_POST['range_from']))) {
86
			$input_errors[] = "A valid range must be specified.";
87
		}
88
		if (($_POST['range_to'] && !is_ipaddr($_POST['range_to']))) {
89
			$input_errors[] = "A valid range must be specified.";
90
		}
91
		if (($_POST['gateway'] && !is_ipaddr($_POST['gateway'])))
92
			$input_errors[] = "A valid IP address must be specified for the gateway.";
93
		if (($_POST['wins1'] && !is_ipaddr($_POST['wins1'])) || ($_POST['wins2'] && !is_ipaddr($_POST['wins2']))) {
94
			$input_errors[] = "A valid IP address must be specified for the primary/secondary WINS server.";
95
		}
96
		if ($_POST['deftime'] && (!is_numeric($_POST['deftime']) || ($_POST['deftime'] < 60))) {
97
			$input_errors[] = "The default lease time must be at least 60 seconds.";
98
		}
99
		if ($_POST['maxtime'] && (!is_numeric($_POST['maxtime']) || ($_POST['maxtime'] < 60) || ($_POST['maxtime'] <= $_POST['deftime']))) {
100
			$input_errors[] = "The maximum lease time must be at least 60 seconds and higher than the default lease time.";
101
		}
102
		
103
		if (!$input_errors) {
104
			/* make sure the range lies within the current subnet */
105
			$subnet_start = (ip2long($ifcfg['ipaddr']) & gen_subnet_mask_long($ifcfg['subnet']));
106
			$subnet_end = (ip2long($ifcfg['ipaddr']) | (~gen_subnet_mask_long($ifcfg['subnet'])));
107
			
108
			if ((ip2long($_POST['range_from']) < $subnet_start) || (ip2long($_POST['range_from']) > $subnet_end) ||
109
			    (ip2long($_POST['range_to']) < $subnet_start) || (ip2long($_POST['range_to']) > $subnet_end)) {
110
				$input_errors[] = "The specified range lies outside of the current subnet.";	
111
			}
112
			
113
			if (ip2long($_POST['range_from']) > ip2long($_POST['range_to']))
114
				$input_errors[] = "The range is invalid (first element higher than second element).";
115
			
116
			/* make sure that the DHCP Relay isn't enabled on this interface */
117
			if (isset($config['dhcrelay'][$if]['enable']))
118
				$input_errors[] = "You must disable the DHCP relay on the {$iflist[$if]} interface before enabling the DHCP server.";
119
		}
120
	}
121

    
122
	if (!$input_errors) {
123
		$config['dhcpd'][$if]['range']['from'] = $_POST['range_from'];
124
		$config['dhcpd'][$if]['range']['to'] = $_POST['range_to'];
125
		$config['dhcpd'][$if]['defaultleasetime'] = $_POST['deftime'];
126
		$config['dhcpd'][$if]['maxleasetime'] = $_POST['maxtime'];
127
		$config['dhcpd'][$if]['enable'] = $_POST['enable'] ? true : false;
128
		$config['dhcpd'][$if]['denyunknown'] = $_POST['denyunknown'] ? true : false;
129
		
130
		unset($config['dhcpd'][$if]['winsserver']);
131
		if ($_POST['wins1'])
132
			$config['dhcpd'][$if]['winsserver'][] = $_POST['wins1'];
133
		if ($_POST['wins2'])
134
			$config['dhcpd'][$if]['winsserver'][] = $_POST['wins2'];
135
			
136
		$config['dhcpd'][$if]['gateway'] = $_POST['gateway'];
137

    
138
		$config['dhcpd'][$if]['staticarp'] = $_POST['staticarp'] ? true : false;
139

    
140
	
141
		write_config();
142

    
143
		/* static arp configuration */
144
                if (isset($config['dhcpd'][$if]['staticarp']))
145
			interfaces_staticarp_configure($if);
146
		
147
		$retval = 0;
148
		if (!file_exists($d_sysrebootreqd_path)) {
149
			config_lock();
150
			$retval = services_dhcpd_configure();
151
			config_unlock();
152
		}
153
		$savemsg = get_std_save_message($retval);
154
		
155
		if ($retval == 0) {
156
			if (file_exists($d_staticmapsdirty_path))
157
				unlink($d_staticmapsdirty_path);
158
		}
159
	}
160
}
161

    
162
if ($_GET['act'] == "del") {
163
	if ($a_maps[$_GET['id']]) {
164
		unset($a_maps[$_GET['id']]);
165
		write_config();
166
		touch($d_staticmapsdirty_path);
167
		header("Location: services_dhcp.php?if={$if}");
168
		exit;
169
	}
170
}
171
?>
172
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
173
<html>
174
<head>
175
<title><?=gentitle("Services: DHCP server");?></title>
176
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
177
<link href="gui.css" rel="stylesheet" type="text/css">
178
<script language="JavaScript">
179
<!--
180
function enable_change(enable_over) {
181
	var endis;
182
	endis = !(document.iform.enable.checked || enable_over);
183
	
184
	document.iform.range_from.disabled = endis;
185
	document.iform.range_to.disabled = endis;
186
	document.iform.wins1.disabled = endis;
187
	document.iform.wins2.disabled = endis;
188
	document.iform.deftime.disabled = endis;
189
	document.iform.maxtime.disabled = endis;
190
	document.iform.gateway.disabled = endis;
191
}
192
//-->
193
</script>
194
</head>
195

    
196
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
197
<?php include("fbegin.inc"); ?>
198
<p class="pgtitle">Services: DHCP server</p>
199
<form action="services_dhcp.php" method="post" name="iform" id="iform">
200
<?php if ($input_errors) print_input_errors($input_errors); ?>
201
<?php if ($savemsg) print_info_box($savemsg); ?>
202
<?php if (file_exists($d_staticmapsdirty_path)): ?><p>
203
<?php print_info_box_np("The static mapping configuration has been changed.<br>You must apply the changes in order for them to take effect.");?><br>
204
<input name="apply" type="submit" class="formbtn" id="apply" value="Apply changes"></p>
205
<?php endif; ?>
206
<table width="100%" border="0" cellpadding="0" cellspacing="0">
207
  <tr><td>
208
  <ul id="tabnav">
209
<?php foreach ($iflist as $ifent => $ifname):
210
	if ($ifent == $if): ?>
211
    <li class="tabact"><?=htmlspecialchars($ifname);?></li>
212
<?php else: ?>
213
    <li class="tabinact"><a href="services_dhcp.php?if=<?=$ifent;?>"><?=htmlspecialchars($ifname);?></a></li>
214
<?php endif; ?>
215
<?php endforeach; ?>
216
  </ul>
217
  </td></tr>
218
  <tr> 
219
    <td class="tabcont">
220
              <table width="100%" border="0" cellpadding="6" cellspacing="0">
221
                      <tr> 
222
                        <td width="22%" valign="top" class="vtable">&nbsp;</td>
223
                        <td width="78%" class="vtable">
224
<input name="enable" type="checkbox" value="yes" <?php if ($pconfig['enable']) echo "checked"; ?> onClick="enable_change(false)">
225
                          <strong>Enable DHCP server on 
226
                          <?=htmlspecialchars($iflist[$if]);?>
227
                          interface</strong></td>
228
                      </tr>
229
				  <tr>
230
	              <td width="22%" valign="top" class="vtable">&nbsp;</td>
231
                      <td width="78%" class="vtable">
232
<input name="denyunknown" type="checkbox" value="yes" <?php if ($pconfig['denyunknown']) echo "checked"; ?>>
233
                      <strong>Deny unknown clients</strong><br>
234
                      If this is checked, only the clients defined below will get DHCP leases from this server. </td>
235
		      		  </tr>
236
                      <tr> 
237
                        <td width="22%" valign="top" class="vncellreq">Subnet</td>
238
                        <td width="78%" class="vtable"> 
239
                          <?=gen_subnet($ifcfg['ipaddr'], $ifcfg['subnet']);?>
240
                        </td>
241
                      </tr>
242
                      <tr> 
243
                        <td width="22%" valign="top" class="vncellreq">Subnet 
244
                          mask</td>
245
                        <td width="78%" class="vtable"> 
246
                          <?=gen_subnet_mask($ifcfg['subnet']);?>
247
                        </td>
248
                      </tr>
249
                      <tr> 
250
                        <td width="22%" valign="top" class="vncellreq">Available 
251
                          range</td>
252
                        <td width="78%" class="vtable"> 
253
                          <?=long2ip(ip2long($ifcfg['ipaddr']) & gen_subnet_mask_long($ifcfg['subnet']));?>
254
                          - 
255
                          <?=long2ip(ip2long($ifcfg['ipaddr']) | (~gen_subnet_mask_long($ifcfg['subnet']))); ?>
256
                        </td>
257
                      </tr>
258
                      <tr> 
259
                        <td width="22%" valign="top" class="vncellreq">Range</td>
260
                        <td width="78%" class="vtable"> 
261
                          <input name="range_from" type="text" class="formfld" id="range_from" size="20" value="<?=htmlspecialchars($pconfig['range_from']);?>"> 
262
                          &nbsp;to&nbsp; <input name="range_to" type="text" class="formfld" id="range_to" size="20" value="<?=htmlspecialchars($pconfig['range_to']);?>"></td>
263
                      </tr>
264
                      <tr> 
265
                        <td width="22%" valign="top" class="vncell">WINS servers</td>
266
                        <td width="78%" class="vtable"> 
267
                          <input name="wins1" type="text" class="formfld" id="wins1" size="20" value="<?=htmlspecialchars($pconfig['wins1']);?>"><br>
268
                          <input name="wins2" type="text" class="formfld" id="wins2" size="20" value="<?=htmlspecialchars($pconfig['wins2']);?>"></td>
269
                      </tr>
270
                     <tr> 
271
                       <td width="22%" valign="top" class="vncell">Gateway</td>
272
                       <td width="78%" class="vtable"> 
273
                         <input name="gateway" type="text" class="formfld" id="gateway" size="20" value="<?=htmlspecialchars($pconfig['gateway']);?>"><br>
274
The default is to use the IP of the firewall as the gateway.  Specify an alternate gateway here if this is not the correct gateway for your network.
275
                      </tr>
276
                      <tr> 
277
                        <td width="22%" valign="top" class="vncell">Default lease 
278
                          time</td>
279
                        <td width="78%" class="vtable"> 
280
                          <input name="deftime" type="text" class="formfld" id="deftime" size="10" value="<?=htmlspecialchars($pconfig['deftime']);?>">
281
                          seconds<br>
282
                          This is used for clients that do not ask for a specific 
283
                          expiration time.<br>
284
                          The default is 7200 seconds.</td>
285
                      </tr>
286
                      <tr> 
287
                        <td width="22%" valign="top" class="vncell">Maximum lease 
288
                          time</td>
289
                        <td width="78%" class="vtable"> 
290
                          <input name="maxtime" type="text" class="formfld" id="maxtime" size="10" value="<?=htmlspecialchars($pconfig['maxtime']);?>">
291
                          seconds<br>
292
                          This is the maximum lease time for clients that ask 
293
                          for a specific expiration time.<br>
294
                          The default is 86400 seconds.</td>
295
                      </tr>
296
                      <tr>
297
                        <td width="22%" valign="top" class="vncell">Static ARP</td>
298
                        <td width="78%" class="vtable"><input type="checkbox" value="yes" name="staticarp" id="staticarp" <?php if($pconfig['staticarp']) echo " checked"; ?>>&nbsp; <b>Enable Static ARP entries</b><br>
299
<span class="red"><strong>Note:</strong></span> This feature is under development.  Only the machines listed below will be able to communicate with the firewall on this NIC.  Disabling this has been tested to be broken, a reboot will be required to disable.  Be warned!</td>
300
                      </tr>
301
                      <tr> 
302
                        <td width="22%" valign="top">&nbsp;</td>
303
                        <td width="78%"> 
304
                          <input name="if" type="hidden" value="<?=$if;?>"> 
305
                          <input name="Submit" type="submit" class="formbtn" value="Save" onclick="enable_change(true)"> 
306
                        </td>
307
                      </tr>
308
                      <tr> 
309
                        <td width="22%" valign="top">&nbsp;</td>
310
                        <td width="78%"> <p><span class="vexpl"><span class="red"><strong>Note:<br>
311
                            </strong></span>The DNS servers entered in <a href="system.php">System: 
312
                            General setup</a> (or the <a href="services_dnsmasq.php">DNS 
313
                            forwarder</a>, if enabled) </span><span class="vexpl">will 
314
                            be assigned to clients by the DHCP server.<br>
315
                            <br>
316
                            The DHCP lease table can be viewed on the <a href="diag_dhcp_leases.php">Diagnostics: 
317
                            DHCP leases</a> page.<br>
318
                            </span></p></td>
319
                      </tr>
320
                    </table>
321

    
322
              <table width="100%" border="0" cellpadding="0" cellspacing="0">
323
                <tr>
324
                  <td width="35%" class="listhdrr">MAC address </td>
325
                  <td width="20%" class="listhdrr">IP address</td>
326
                  <td width="35%" class="listhdr">Description</td>
327
                  <td width="10%" class="list"></td>
328
				</tr>
329
			  <?php $i = 0; foreach ($a_maps as $mapent): ?>
330
                <tr>
331
                  <td class="listlr" ondblclick="document.location='services_dhcp_edit.php?if=<?=$if;?>&id=<?=$i;?>';">
332
                    <?=htmlspecialchars($mapent['mac']);?>
333
                  </td>
334
                  <td class="listr" ondblclick="document.location='services_dhcp_edit.php?if=<?=$if;?>&id=<?=$i;?>';">
335
                    <?=htmlspecialchars($mapent['ipaddr']);?>&nbsp;
336
                  </td>
337
                  <td class="listbg" ondblclick="document.location='services_dhcp_edit.php?if=<?=$if;?>&id=<?=$i;?>';">
338
                    <font color="#FFFFFFF"><?=htmlspecialchars($mapent['descr']);?>&nbsp;</font>
339
                  </td>
340
                  <td valign="middle" nowrap class="list">
341
                    <table border="0" cellspacing="0" cellpadding="1">
342
                      <tr>
343
                        <td valign="middle"><a href="services_dhcp_edit.php?if=<?=$if;?>&id=<?=$i;?>"><img src="e.gif" width="17" height="17" border="0"></a></td>
344
                        <td valign="middle"><a href="services_dhcp.php?if=<?=$if;?>&act=del&id=<?=$i;?>" onclick="return confirm('Do you really want to delete this mapping?')"><img src="x.gif" width="17" height="17" border="0"></a></td>
345
                      </tr>
346
                    </table>
347
                  </td>
348
                </tr>
349
		<?php $i++; endforeach; ?>
350
                <tr> 
351
                  <td class="list" colspan="3"></td>
352
                  <td class="list">
353
                    <table border="0" cellspacing="0" cellpadding="1">
354
                      <tr>
355
                        <td valign="middle"><a href="services_dhcp_edit.php?if=<?=$if;?>"><img src="plus.gif" width="17" height="17" border="0"></a></td>
356
                      </tr>
357
                    </table>
358
                  </td>
359
                </tr>
360
              </table>
361
    </td>
362
  </tr>
363
</table>
364
</form>
365
<script language="JavaScript">
366
<!--
367
enable_change(false);
368
//-->
369
</script>
370
<?php include("fend.inc"); ?>
371
</body>
372
</html>
(76-76/117)