Project

General

Profile

Download (19.3 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
list($pconfig['dns1'],$pconfig['dns2']) = $config['dhcpd'][$if]['dnsserver'];
59
$pconfig['enable'] = isset($config['dhcpd'][$if]['enable']);
60
$pconfig['denyunknown'] = isset($config['dhcpd'][$if]['denyunknown']);
61
$pconfig['staticarp'] = isset($config['dhcpd'][$if]['staticarp']);
62
$pconfig['failover_peerip'] = $config['dhcpd'][$if]['failover_peerip'];
63

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

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

    
72
function is_inrange($test, $start, $end) {
73
	if ( (ip2long($test) < ip2long($end)) && (ip2long($test) > ip2long($start)) )
74
		return true;
75
	else
76
		return false;
77
}
78

    
79
if ($_POST) {
80

    
81
	unset($input_errors);
82

    
83
	$pconfig = $_POST;
84

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

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

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

    
139
	if (!$input_errors) {
140
		$config['dhcpd'][$if]['range']['from'] = $_POST['range_from'];
141
		$config['dhcpd'][$if]['range']['to'] = $_POST['range_to'];
142
		$config['dhcpd'][$if]['defaultleasetime'] = $_POST['deftime'];
143
		$config['dhcpd'][$if]['maxleasetime'] = $_POST['maxtime'];
144
		$config['dhcpd'][$if]['failover_peerip'] = $_POST['failover_peerip'];
145
				
146
		unset($config['dhcpd'][$if]['winsserver']);
147
		if ($_POST['wins1'])
148
			$config['dhcpd'][$if]['winsserver'][] = $_POST['wins1'];
149
		if ($_POST['wins2'])
150
			$config['dhcpd'][$if]['winsserver'][] = $_POST['wins2'];
151

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

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

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

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

    
176
		write_config();
177

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

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

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

    
202
?>
203

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

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