Project

General

Profile

Download (20.4 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
/* if OLSRD is enabled, allow WAN to house DHCP. */
39
if($config['installedpackages']['olsrd']) {
40
	foreach($config['installedpackages']['olsrd']['config'] as $olsrd) {
41
			if($olsrd['enable']) {
42
				$iflist = array("lan" => "LAN", "wan" => "WAN");
43
				$is_olsr_enabled = true;
44
				break;
45
			}
46
	}
47
}
48

    
49
if(!$iflist)
50
	$iflist = array("lan" => "LAN");
51

    
52
for ($i = 1; isset($config['interfaces']['opt' . $i]); $i++) {
53
	$oc = $config['interfaces']['opt' . $i];
54
	
55
	if (isset($oc['enable']) && $oc['if'] && (!$oc['bridge'])) {
56
		$iflist['opt' . $i] = $oc['descr'];
57
	}
58
}
59

    
60
if (!$if || !isset($iflist[$if]))
61
	$if = "lan";
62

    
63
$pconfig['range_from'] = $config['dhcpd'][$if]['range']['from'];
64
$pconfig['range_to'] = $config['dhcpd'][$if]['range']['to'];
65
$pconfig['deftime'] = $config['dhcpd'][$if]['defaultleasetime'];
66
$pconfig['maxtime'] = $config['dhcpd'][$if]['maxleasetime'];
67
$pconfig['gateway'] = $config['dhcpd'][$if]['gateway'];
68
list($pconfig['wins1'],$pconfig['wins2']) = $config['dhcpd'][$if]['winsserver'];
69
list($pconfig['dns1'],$pconfig['dns2']) = $config['dhcpd'][$if]['dnsserver'];
70
$pconfig['enable'] = isset($config['dhcpd'][$if]['enable']);
71
$pconfig['denyunknown'] = isset($config['dhcpd'][$if]['denyunknown']);
72
$pconfig['staticarp'] = isset($config['dhcpd'][$if]['staticarp']);
73
$pconfig['failover_peerip'] = $config['dhcpd'][$if]['failover_peerip'];
74
$pconfig['netmask'] = $config['dhcpd'][$if]['netmask'];
75

    
76
$ifcfg = $config['interfaces'][$if];
77

    
78
if (!is_array($config['dhcpd'][$if]['staticmap'])) {
79
	$config['dhcpd'][$if]['staticmap'] = array();
80
}
81
staticmaps_sort($if);
82
$a_maps = &$config['dhcpd'][$if]['staticmap'];
83

    
84
function is_inrange($test, $start, $end) {
85
	if ( (ip2long($test) < ip2long($end)) && (ip2long($test) > ip2long($start)) )
86
		return true;
87
	else
88
		return false;
89
}
90

    
91
if ($_POST) {
92

    
93
	unset($input_errors);
94

    
95
	$pconfig = $_POST;
96

    
97
	/* input validation */
98
	if ($_POST['enable']) {
99
		$reqdfields = explode(" ", "range_from range_to");
100
		$reqdfieldsn = explode(",", "Range begin,Range end");
101
		
102
		do_input_validation($_POST, $reqdfields, $reqdfieldsn, &$input_errors);
103

    
104
		foreach($a_maps as $mapent) {
105
			if(is_inrange($mapent['ipaddr'], $_POST['range_from'], $_POST['range_to'])) {
106
				$input_errors[] = "{$mapent['ipaddr']} is inside the range you specified.";
107
			}
108

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

    
151
	if (!$input_errors) {
152
		$config['dhcpd'][$if]['range']['from'] = $_POST['range_from'];
153
		$config['dhcpd'][$if]['range']['to'] = $_POST['range_to'];
154
		$config['dhcpd'][$if]['defaultleasetime'] = $_POST['deftime'];
155
		$config['dhcpd'][$if]['maxleasetime'] = $_POST['maxtime'];
156
		$config['dhcpd'][$if]['netmask'] = $_POST['netmask'];
157
		$previous = $config['dhcpd'][$if]['failover_peerip'];
158
		if($previous <> $_POST['failover_peerip']) {
159
			mwexec("rm -rf /var/dhcpd/var/db/*");	
160
		}		
161
		$config['dhcpd'][$if]['failover_peerip'] = $_POST['failover_peerip'];
162
				
163
		unset($config['dhcpd'][$if]['winsserver']);
164
		if ($_POST['wins1'])
165
			$config['dhcpd'][$if]['winsserver'][] = $_POST['wins1'];
166
		if ($_POST['wins2'])
167
			$config['dhcpd'][$if]['winsserver'][] = $_POST['wins2'];
168

    
169
		unset($config['dhcpd'][$if]['dnsserver']);
170

    
171
		if ($_POST['dns1']) 		
172
			$config['dhcpd'][$if]['dnsserver'][] = $_POST['dns1'];
173
		if ($_POST['dns2']) 
174
			$config['dhcpd'][$if]['dnsserver'][] = $_POST['dns2'];
175
			
176
		$config['dhcpd'][$if]['gateway'] = $_POST['gateway'];
177

    
178
		if($_POST['denyunknown'] == "yes") 
179
			$config['dhcpd'][$if]['denyunknown'] = true;
180
		else
181
			unset($config['dhcpd'][$if]['denyunknown']);
182

    
183
		if($_POST['enable'] == "yes")
184
			$config['dhcpd'][$if]['enable'] = $_POST['enable'];
185
		else
186
			unset($config['dhcpd'][$if]['enable']);
187
		
188
		if($_POST['staticarp'] == "yes") {
189
			$config['dhcpd'][$if]['staticarp'] = true;
190
		} else {
191
			unset($config['dhcpd'][$if]['staticarp']);
192
		}
193

    
194
		write_config();
195

    
196
		/* static arp configuration */
197
		interfaces_staticarp_configure($if);
198
		
199
		$retval = 0;
200
		config_lock();
201
		$retval = services_dhcpd_configure();
202
		config_unlock();
203
		$savemsg = get_std_save_message($retval);
204
	}
205
}
206

    
207
if ($_GET['act'] == "del") {
208
	if ($a_maps[$_GET['id']]) {
209
		unset($a_maps[$_GET['id']]);
210
		write_config();
211
		header("Location: services_dhcp.php?if={$if}");
212
		exit;
213
	}
214
}
215

    
216
$pgtitle = "Services: DHCP server";
217
include("head.inc");
218

    
219
?>
220

    
221
<script language="JavaScript">
222
<!--
223
function enable_change(enable_over) {
224
	var endis;
225
	endis = !(document.iform.enable.checked || enable_over);
226
	document.iform.range_from.disabled = endis;
227
	document.iform.range_to.disabled = endis;
228
	document.iform.wins1.disabled = endis;
229
	document.iform.wins2.disabled = endis;
230
	document.iform.dns1.disabled = endis;
231
	document.iform.dns2.disabled = endis;
232
	document.iform.deftime.disabled = endis;
233
	document.iform.maxtime.disabled = endis;
234
	document.iform.gateway.disabled = endis;
235
	document.iform.failover_peerip.disabled = endis;
236
	document.iform.staticarp.disabled = endis;
237
	document.iform.denyunknown.disabled = endis;
238
}
239
//-->
240
</script>
241

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