Project

General

Profile

Download (19.6 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php 
2
/* $Id$ */
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
$pconfig['gateway'] = $config['dhcpd'][$if]['gateway'];
56
list($pconfig['wins1'],$pconfig['wins2']) = $config['dhcpd'][$if]['winsserver'];
57
list($pconfig['dns1'],$pconfig['dns2']) = $config['dhcpd'][$if]['dnsserver'];
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
$pconfig['failover_peerip'] = $config['dhcpd'][$if]['failover_peerip'];
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
function is_inrange($test, $start, $end) {
72
	if ( (ip2long($test) < ip2long($end)) && (ip2long($test) > ip2long($start)) )
73
		return true;
74
	else
75
		return false;
76
}
77

    
78
if ($_POST) {
79

    
80
	unset($input_errors);
81

    
82
	$pconfig = $_POST;
83

    
84
	/* input validation */
85
	if ($_POST['enable']) {
86
		$reqdfields = explode(" ", "range_from range_to");
87
		$reqdfieldsn = explode(",", "Range begin,Range end");
88
		
89
		do_input_validation($_POST, $reqdfields, $reqdfieldsn, &$input_errors);
90

    
91
		foreach($a_maps as $mapent) {
92
			if(is_inrange($mapent['ipaddr'], $_POST['range_from'], $_POST['range_to'])) {
93
				$input_errors[] = "{$mapent['ipaddr']} is inside the range you specified.";
94
			}
95

    
96
		}
97
		
98
		if (($_POST['range_from'] && !is_ipaddr($_POST['range_from']))) {
99
			$input_errors[] = "A valid range must be specified.";
100
		}
101
		if (($_POST['range_to'] && !is_ipaddr($_POST['range_to']))) {
102
			$input_errors[] = "A valid range must be specified.";
103
		}
104
		if (($_POST['gateway'] && !is_ipaddr($_POST['gateway'])))
105
			$input_errors[] = "A valid IP address must be specified for the gateway.";
106
		if (($_POST['wins1'] && !is_ipaddr($_POST['wins1'])) || ($_POST['wins2'] && !is_ipaddr($_POST['wins2']))) {
107
			$input_errors[] = "A valid IP address must be specified for the primary/secondary WINS servers.";
108
		}
109
		if (($_POST['dns1'] && !is_ipaddr($_POST['dns1'])) || ($_POST['dns2'] && !is_ipaddr($_POST['dns2']))) {
110
			$input_errors[] = "A valid IP address must be specified for the primary/secondary DNS servers.";
111
		}
112
		if ($_POST['deftime'] && (!is_numeric($_POST['deftime']) || ($_POST['deftime'] < 60))) {
113
			$input_errors[] = "The default lease time must be at least 60 seconds.";
114
		}
115
		if ($_POST['maxtime'] && (!is_numeric($_POST['maxtime']) || ($_POST['maxtime'] < 60) || ($_POST['maxtime'] <= $_POST['deftime']))) {
116
			$input_errors[] = "The maximum lease time must be at least 60 seconds and higher than the default lease time.";
117
		}
118
		
119
		if (!$input_errors) {
120
			/* make sure the range lies within the current subnet */
121
			$subnet_start = (ip2long($ifcfg['ipaddr']) & gen_subnet_mask_long($ifcfg['subnet']));
122
			$subnet_end = (ip2long($ifcfg['ipaddr']) | (~gen_subnet_mask_long($ifcfg['subnet'])));
123
			
124
			if ((ip2long($_POST['range_from']) < $subnet_start) || (ip2long($_POST['range_from']) > $subnet_end) ||
125
			    (ip2long($_POST['range_to']) < $subnet_start) || (ip2long($_POST['range_to']) > $subnet_end)) {
126
				$input_errors[] = "The specified range lies outside of the current subnet.";	
127
			}
128
			
129
			if (ip2long($_POST['range_from']) > ip2long($_POST['range_to']))
130
				$input_errors[] = "The range is invalid (first element higher than second element).";
131
			
132
			/* make sure that the DHCP Relay isn't enabled on this interface */
133
			if (isset($config['dhcrelay'][$if]['enable']))
134
				$input_errors[] = "You must disable the DHCP relay on the {$iflist[$if]} interface before enabling the DHCP server.";
135
		}
136
	}
137

    
138
	if (!$input_errors) {
139
		$config['dhcpd'][$if]['range']['from'] = $_POST['range_from'];
140
		$config['dhcpd'][$if]['range']['to'] = $_POST['range_to'];
141
		$config['dhcpd'][$if]['defaultleasetime'] = $_POST['deftime'];
142
		$config['dhcpd'][$if]['maxleasetime'] = $_POST['maxtime'];
143
		$previous = $config['dhcpd'][$if]['failover_peerip'];
144
		if($previous <> $_POST['failover_peerip']) {
145
			mwexec("rm -rf /var/dhcpd/var/db/*");	
146
		}		
147
		$config['dhcpd'][$if]['failover_peerip'] = $_POST['failover_peerip'];
148
				
149
		unset($config['dhcpd'][$if]['winsserver']);
150
		if ($_POST['wins1'])
151
			$config['dhcpd'][$if]['winsserver'][] = $_POST['wins1'];
152
		if ($_POST['wins2'])
153
			$config['dhcpd'][$if]['winsserver'][] = $_POST['wins2'];
154

    
155
		unset($config['dhcpd'][$if]['dnsserver']);
156

    
157
		if ($_POST['dns1']) 		
158
			$config['dhcpd'][$if]['dnsserver'][] = $_POST['dns1'];
159
		if ($_POST['dns2']) 
160
			$config['dhcpd'][$if]['dnsserver'][] = $_POST['dns2'];
161
			
162
		$config['dhcpd'][$if]['gateway'] = $_POST['gateway'];
163

    
164
		if($_POST['denyunknown'] == "yes") 
165
			$config['dhcpd'][$if]['denyunknown'] = true;
166
		else
167
			unset($config['dhcpd'][$if]['denyunknown']);
168

    
169
		if($_POST['enable'] == "yes")
170
			$config['dhcpd'][$if]['enable'] = $_POST['enable'];
171
		else
172
			unset($config['dhcpd'][$if]['enable']);
173
		
174
		if($_POST['staticarp'] == "yes") {
175
			$config['dhcpd'][$if]['staticarp'] = true;
176
		} else {
177
			unset($config['dhcpd'][$if]['staticarp']);
178
		}
179

    
180
		write_config();
181

    
182
		/* static arp configuration */
183
                if (isset($config['dhcpd'][$if]['staticarp']))
184
			interfaces_staticarp_configure($if);
185
		
186
		$retval = 0;
187
		config_lock();
188
		$retval = services_dhcpd_configure();
189
		config_unlock();
190
		$savemsg = get_std_save_message($retval);
191
	}
192
}
193

    
194
if ($_GET['act'] == "del") {
195
	if ($a_maps[$_GET['id']]) {
196
		unset($a_maps[$_GET['id']]);
197
		write_config();
198
		header("Location: services_dhcp.php?if={$if}");
199
		exit;
200
	}
201
}
202

    
203
$pgtitle = "Services: DHCP server";
204
include("head.inc");
205

    
206
?>
207

    
208
<script language="JavaScript">
209
<!--
210
function enable_change(enable_over) {
211
	var endis;
212
	endis = !(document.iform.enable.checked || enable_over);
213
	document.iform.range_from.disabled = endis;
214
	document.iform.range_to.disabled = endis;
215
	document.iform.wins1.disabled = endis;
216
	document.iform.wins2.disabled = endis;
217
	document.iform.dns1.disabled = endis;
218
	document.iform.dns2.disabled = endis;
219
	document.iform.deftime.disabled = endis;
220
	document.iform.maxtime.disabled = endis;
221
	document.iform.gateway.disabled = endis;
222
	document.iform.failover_peerip.disabled = endis;
223
	document.iform.staticarp.disabled = endis;
224
	document.iform.denyunknown.disabled = endis;
225
}
226
//-->
227
</script>
228

    
229
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
230
<?php include("fbegin.inc"); ?>
231
<p class="pgtitle"><?=$pgtitle?></p>
232
<form action="services_dhcp.php" method="post" name="iform" id="iform">
233
<?php if ($input_errors) print_input_errors($input_errors); ?>
234
<?php if ($savemsg) print_info_box($savemsg); ?>
235
<?php if (file_exists($d_staticmapsdirty_path)): ?><p>
236
<?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>
237
<?php endif; ?>
238
<table width="100%" border="0" cellpadding="0" cellspacing="0">
239
  <tr><td>
240
  <?php
241
	/* active tabs */
242
	$tab_array = array();
243
	$tabscounter = 0;
244
	$i = 0;
245
	foreach ($iflist as $ifent => $ifname) {
246
		if ($ifent == $if)
247
			$active = true;
248
		else
249
			$active = false;
250
		$tab_array[] = array($ifname, $active, "services_dhcp.php?if={$ifent}");
251
	}
252
	display_top_tabs($tab_array);
253
  ?>
254
  </td></tr>
255
  <tr> 
256
    <td>
257
	<div id="mainarea">
258
              <table class="tabcont" width="100%" border="0" cellpadding="6" cellspacing="0">
259
                      <tr> 
260
                        <td width="22%" valign="top" class="vtable">&nbsp;</td>
261
                        <td width="78%" class="vtable">
262
			  <input name="enable" type="checkbox" value="yes" <?php if ($pconfig['enable']) echo "checked"; ?> onClick="enable_change(false)">
263
                          <strong>Enable DHCP server on 
264
                          <?=htmlspecialchars($iflist[$if]);?>
265
                          interface</strong></td>
266
                      </tr>
267
				  <tr>
268
	              <td width="22%" valign="top" class="vtable">&nbsp;</td>
269
                      <td width="78%" class="vtable">
270
			<input name="denyunknown" id="denyunknown" type="checkbox" value="yes" <?php if ($pconfig['denyunknown']) echo "checked"; ?>>
271
                      <strong>Deny unknown clients</strong><br>
272
                      If this is checked, only the clients defined below will get DHCP leases from this server. </td>
273
		      		  </tr>
274
                      <tr> 
275
                        <td width="22%" valign="top" class="vncellreq">Subnet</td>
276
                        <td width="78%" class="vtable"> 
277
                          <?=gen_subnet($ifcfg['ipaddr'], $ifcfg['subnet']);?>
278
                        </td>
279
                      </tr>
280
                      <tr> 
281
                        <td width="22%" valign="top" class="vncellreq">Subnet 
282
                          mask</td>
283
                        <td width="78%" class="vtable"> 
284
                          <?=gen_subnet_mask($ifcfg['subnet']);?>
285
                        </td>
286
                      </tr>
287
                      <tr> 
288
                        <td width="22%" valign="top" class="vncellreq">Available 
289
                          range</td>
290
                        <td width="78%" class="vtable"> 
291
                          <?=long2ip(ip2long($ifcfg['ipaddr']) & gen_subnet_mask_long($ifcfg['subnet']));?>
292
                          - 
293
                          <?=long2ip(ip2long($ifcfg['ipaddr']) | (~gen_subnet_mask_long($ifcfg['subnet']))); ?>
294
                        </td>
295
                      </tr>
296
                      <tr> 
297
                        <td width="22%" valign="top" class="vncellreq">Range</td>
298
                        <td width="78%" class="vtable"> 
299
                          <input name="range_from" type="text" class="formfld" id="range_from" size="20" value="<?=htmlspecialchars($pconfig['range_from']);?>"> 
300
                          &nbsp;to&nbsp; <input name="range_to" type="text" class="formfld" id="range_to" size="20" value="<?=htmlspecialchars($pconfig['range_to']);?>">
301
			</td>
302
                      </tr>
303
                      <tr> 
304
                        <td width="22%" valign="top" class="vncell">WINS servers</td>
305
                        <td width="78%" class="vtable"> 
306
                          <input name="wins1" type="text" class="formfld" id="wins1" size="20" value="<?=htmlspecialchars($pconfig['wins1']);?>"><br>
307
                          <input name="wins2" type="text" class="formfld" id="wins2" size="20" value="<?=htmlspecialchars($pconfig['wins2']);?>">
308
			</td>
309
                      </tr>
310
                      <tr> 
311
                        <td width="22%" valign="top" class="vncell">DNS servers</td>
312
                        <td width="78%" class="vtable"> 
313
                          <input name="dns1" type="text" class="formfld" id="dns1" size="20" value="<?=htmlspecialchars($pconfig['dns1']);?>"><br>
314
                          <input name="dns2" type="text" class="formfld" id="dns2" size="20" value="<?=htmlspecialchars($pconfig['dns2']);?>"><br>
315
			  NOTE: leave blank to use the system default DNS servers.  This option is handy when your doing CARP+DHCP Failover, etc.
316
			</td>
317
                      </tr>
318
                     <tr> 
319
                       <td width="22%" valign="top" class="vncell">Gateway</td>
320
                       <td width="78%" class="vtable"> 
321
                         <input name="gateway" type="text" class="formfld" id="gateway" size="20" value="<?=htmlspecialchars($pconfig['gateway']);?>"><br>
322
			 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.
323
			</td> 
324
                     </tr>
325
                      <tr> 
326
                        <td width="22%" valign="top" class="vncell">Default lease 
327
                          time</td>
328
                        <td width="78%" class="vtable"> 
329
                          <input name="deftime" type="text" class="formfld" id="deftime" size="10" value="<?=htmlspecialchars($pconfig['deftime']);?>">
330
                          seconds<br>
331
                          This is used for clients that do not ask for a specific 
332
                          expiration time.<br>
333
                          The default is 7200 seconds.
334
			</td>
335
                      </tr>
336
                      <tr> 
337
                        <td width="22%" valign="top" class="vncell">Maximum lease 
338
                          time</td>
339
                        <td width="78%" class="vtable"> 
340
                          <input name="maxtime" type="text" class="formfld" id="maxtime" size="10" value="<?=htmlspecialchars($pconfig['maxtime']);?>">
341
                          seconds<br>
342
                          This is the maximum lease time for clients that ask 
343
                          for a specific expiration time.<br>
344
                          The default is 86400 seconds.
345
			</td>
346
                      </tr>
347
                      <tr>
348
                        <td width="22%" valign="top" class="vncell">Failover peer IP:</td>
349
                        <td width="78%" class="vtable">
350
				<input name="failover_peerip" type="text" class="formfld" id="failover_peerip" size="10" value="<?=htmlspecialchars($pconfig['failover_peerip']);?>"><br>
351
				Leave blank to disable.  Enter the REAL address of the other machine.  Machines must be using CARP.
352
			</td>
353
		      </tr>
354
                      <tr>
355
                        <td width="22%" valign="top" class="vncell">Static ARP</td>
356
                        <td width="78%" class="vtable">
357
				<table>
358
					<tr>
359
						<td>
360
							<input valign="middle" type="checkbox" value="yes" name="staticarp" id="staticarp" <?php if($pconfig['staticarp']) echo " checked"; ?>>&nbsp;
361
						</td>
362
						<td>
363
							<b>Enable Static ARP entries</b>
364
						</td>
365
					</tr>
366
					<tr>
367
						<td>
368
							&nbsp;
369
						</td>
370
						<td>
371
							<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!
372
						</td>
373
					</tr>
374
				</table>
375
			</td>
376
                      </tr>
377
                      <tr> 
378
                        <td width="22%" valign="top">&nbsp;</td>
379
                        <td width="78%"> 
380
                          <input name="if" type="hidden" value="<?=$if;?>"> 
381
                          <input name="Submit" type="submit" class="formbtn" value="Save" onclick="enable_change(true)"> 
382
                        </td>
383
                      </tr>
384
                      <tr> 
385
                        <td width="22%" valign="top">&nbsp;</td>
386
                        <td width="78%"> <p><span class="vexpl"><span class="red"><strong>Note:<br>
387
                            </strong></span>The DNS servers entered in <a href="system.php">System: 
388
                            General setup</a> (or the <a href="services_dnsmasq.php">DNS 
389
                            forwarder</a>, if enabled) </span><span class="vexpl">will 
390
                            be assigned to clients by the DHCP server.<br>
391
                            <br>
392
                            The DHCP lease table can be viewed on the <a href="diag_dhcp_leases.php">Diagnostics: 
393
                            DHCP leases</a> page.<br>
394
                            </span></p>
395
			</td>
396
                      </tr>
397
                    </table>
398
              <table class="tabcont" width="100%" border="0" cellpadding="0" cellspacing="0">
399
                <tr>
400
                  <td width="35%" class="listhdrr">MAC address</td>
401
                  <td width="20%" class="listhdrr">IP address</td>
402
                  <td width="35%" class="listhdr">Description</td>
403
                  <td width="10%" class="list">
404
		  </td>
405
		</tr>
406
			  <?php if(is_array($a_maps)): ?>
407
			  <?php $i = 0; foreach ($a_maps as $mapent): ?>
408
			  <?php if($mapent['mac'] <> "" or $mapent['ipaddr'] <> ""): ?>
409
                <tr>
410
                  <td class="listlr" ondblclick="document.location='services_dhcp_edit.php?if=<?=$if;?>&id=<?=$i;?>';">
411
                    <?=htmlspecialchars($mapent['mac']);?>
412
                  </td>
413
                  <td class="listr" ondblclick="document.location='services_dhcp_edit.php?if=<?=$if;?>&id=<?=$i;?>';">
414
                    <?=htmlspecialchars($mapent['ipaddr']);?>&nbsp;
415
                  </td>
416
                  <td class="listbg" ondblclick="document.location='services_dhcp_edit.php?if=<?=$if;?>&id=<?=$i;?>';">
417
                    <font color="#FFFFFF"><?=htmlspecialchars($mapent['descr']);?>&nbsp;</font>
418
                  </td>
419
                  <td valign="middle" nowrap class="list">
420
                    <table border="0" cellspacing="0" cellpadding="1">
421
                      <tr>
422
                        <td valign="middle"><a href="services_dhcp_edit.php?if=<?=$if;?>&id=<?=$i;?>"><img src="./themes/<?= $g['theme']; ?>/images/icons/icon_e.gif" width="17" height="17" border="0"></a></td>
423
                        <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="./themes/<?= $g['theme']; ?>/images/icons/icon_x.gif" width="17" height="17" border="0"></a></td>
424
                      </tr>
425
                    </table>
426
                  </td>
427
                </tr>
428
		<?php endif; ?>
429
		<?php $i++; endforeach; ?>
430
		<?php endif; ?>
431
                <tr> 
432
                  <td class="list" colspan="3"></td>
433
                  <td class="list">
434
                    <table border="0" cellspacing="0" cellpadding="1">
435
                      <tr>
436
                        <td valign="middle"><a href="services_dhcp_edit.php?if=<?=$if;?>"><img src="./themes/<?= $g['theme']; ?>/images/icons/icon_plus.gif" width="17" height="17" border="0"></a></td>
437
                      </tr>
438
                    </table>
439
                  </td>
440
                </tr>
441
              </table>
442
	</div>
443
    </td>
444
  </tr>
445
</table>
446
</form>
447
<script language="JavaScript">
448
<!--
449
enable_change(false);
450
//-->
451
</script>
452
<?php include("fend.inc"); ?>
453
</body>
454
</html>
(102-102/159)