Project

General

Profile

Download (19.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
$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
		$config['dhcpd'][$if]['failover_peerip'] = $_POST['failover_peerip'];
144
				
145
		unset($config['dhcpd'][$if]['winsserver']);
146
		if ($_POST['wins1'])
147
			$config['dhcpd'][$if]['winsserver'][] = $_POST['wins1'];
148
		if ($_POST['wins2'])
149
			$config['dhcpd'][$if]['winsserver'][] = $_POST['wins2'];
150

    
151
		unset($config['dhcpd'][$if]['dnsserver']);
152

    
153
		if ($_POST['dns1']) 		
154
			$config['dhcpd'][$if]['dnsserver'][] = $_POST['dns1'];
155
		if ($_POST['dns2']) 
156
			$config['dhcpd'][$if]['dnsserver'][] = $_POST['dns2'];
157
			
158
		$config['dhcpd'][$if]['gateway'] = $_POST['gateway'];
159

    
160
		if($_POST['denyunknown'] == "yes") 
161
			$config['dhcpd'][$if]['denyunknown'] = true;
162
		else
163
			unset($config['dhcpd'][$if]['denyunknown']);
164

    
165
		if($_POST['enable'] == "yes")
166
			$config['dhcpd'][$if]['enable'] = $_POST['enable'];
167
		else
168
			unset($config['dhcpd'][$if]['enable']);
169
		
170
		if($_POST['staticarp'] == "yes")
171
			$config['dhcpd'][$if]['staticarp'] = true;
172
		else 
173
			unset($config['dhcpd'][$if]['staticarp']);
174

    
175
		write_config();
176

    
177
		/* static arp configuration */
178
                if (isset($config['dhcpd'][$if]['staticarp']))
179
			interfaces_staticarp_configure($if);
180
		
181
		$retval = 0;
182
		config_lock();
183
		$retval = services_dhcpd_configure();
184
		config_unlock();
185
		$savemsg = get_std_save_message($retval);
186
	}
187
}
188

    
189
if ($_GET['act'] == "del") {
190
	if ($a_maps[$_GET['id']]) {
191
		unset($a_maps[$_GET['id']]);
192
		write_config();
193
		header("Location: services_dhcp.php?if={$if}");
194
		exit;
195
	}
196
}
197

    
198
$pgtitle = "Services: DHCP server";
199
include("head.inc");
200

    
201
?>
202

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

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