Project

General

Profile

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

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

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

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

    
60
$ifcfg = $config['interfaces'][$if];
61

    
62
if (!is_array($config['dhcpd'][$if]['staticmap'])) {
63
	$config['dhcpd'][$if]['staticmap'] = array();
64
}
65
staticmaps_sort($if);
66
$a_maps = &$config['dhcpd'][$if]['staticmap'];
67

    
68

    
69
if ($_POST) {
70

    
71
	unset($input_errors);
72

    
73
	$pconfig = $_POST;
74

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

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

    
135
		$config['dhcpd'][$if]['staticarp'] = $_POST['staticarp'] ? true : false;
136

    
137
	
138
		write_config();
139

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

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

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

    
319
              <table width="100%" border="0" cellpadding="0" cellspacing="0">
320
                <tr>
321
                  <td width="35%" class="listhdrr">MAC address </td>
322
                  <td width="20%" class="listhdrr">IP address</td>
323
                  <td width="35%" class="listhdr">Description</td>
324
                  <td width="10%" class="list"></td>
325
				</tr>
326
			  <?php $i = 0; foreach ($a_maps as $mapent): ?>
327
                <tr>
328
                  <td class="listlr">
329
                    <?=htmlspecialchars($mapent['mac']);?>
330
                  </td>
331
                  <td class="listr">
332
                    <?=htmlspecialchars($mapent['ipaddr']);?>&nbsp;
333
                  </td>
334
                  <td class="listbg">
335
                    <?=htmlspecialchars($mapent['descr']);?>&nbsp;
336
                  </td>
337
                  <td valign="middle" nowrap class="list"> <a href="services_dhcp_edit.php?if=<?=$if;?>&id=<?=$i;?>"><img src="e.gif" width="17" height="17" border="0"></a>
338
                     &nbsp;<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>
339
				</tr>
340
			  <?php $i++; endforeach; ?>
341
                <tr> 
342
                  <td class="list" colspan="3"></td>
343
                  <td class="list"> <a href="services_dhcp_edit.php?if=<?=$if;?>"><img src="plus.gif" width="17" height="17" border="0"></a></td>
344
				</tr>
345
              </table>
346
    </td>
347
  </tr>
348
</table>
349
</form>
350
<script language="JavaScript">
351
<!--
352
enable_change(false);
353
//-->
354
</script>
355
<?php include("fend.inc"); ?>
356
</body>
357
</html>
(70-70/106)