Project

General

Profile

Download (16.7 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
$pconfig['enable'] = isset($config['dhcpd'][$if]['enable']);
59
$pconfig['denyunknown'] = isset($config['dhcpd'][$if]['denyunknown']);
60
$pconfig['staticarp'] = isset($config['dhcpd'][$if]['staticarp']);
61

    
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

    
72
if ($_POST) {
73

    
74
	unset($input_errors);
75

    
76
	$pconfig = $_POST;
77

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

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

    
138
		$config['dhcpd'][$if]['staticarp'] = $_POST['staticarp'] ? true : false;
139

    
140
	
141
		write_config();
142

    
143
		/* static arp configuration */
144
                if (isset($config['dhcpd'][$if]['staticarp']))
145
			interfaces_staticarp_configure($if);
146
		
147
		$retval = 0;
148
		config_lock();
149
		$retval = services_dhcpd_configure();
150
		config_unlock();
151
		$savemsg = get_std_save_message($retval);
152
	}
153
}
154

    
155
if ($_GET['act'] == "del") {
156
	if ($a_maps[$_GET['id']]) {
157
		unset($a_maps[$_GET['id']]);
158
		write_config();
159
		touch($d_staticmapsdirty_path);
160
		header("Location: services_dhcp.php?if={$if}");
161
		exit;
162
	}
163
}
164

    
165
$pgtitle = "Services: DHCP server";
166
include("head.inc");
167

    
168
?>
169

    
170
<script language="JavaScript">
171
<!--
172
function enable_change(enable_over) {
173
	var endis;
174
	endis = !(document.iform.enable.checked || enable_over);
175
	
176
	document.iform.range_from.disabled = endis;
177
	document.iform.range_to.disabled = endis;
178
	document.iform.wins1.disabled = endis;
179
	document.iform.wins2.disabled = endis;
180
	document.iform.deftime.disabled = endis;
181
	document.iform.maxtime.disabled = endis;
182
	document.iform.gateway.disabled = endis;
183
}
184
//-->
185
</script>
186

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

    
323
              <table class="tabcont" width="100%" border="0" cellpadding="0" cellspacing="0">
324
                <tr>
325
                  <td width="35%" class="listhdrr">MAC address </td>
326
                  <td width="20%" class="listhdrr">IP address</td>
327
                  <td width="35%" class="listhdr">Description</td>
328
                  <td width="10%" class="list"></td>
329
				</tr>
330
			  <?php $i = 0; foreach ($a_maps as $mapent): ?>
331
                <tr>
332
                  <td class="listlr" ondblclick="document.location='services_dhcp_edit.php?if=<?=$if;?>&id=<?=$i;?>';">
333
                    <?=htmlspecialchars($mapent['mac']);?>
334
                  </td>
335
                  <td class="listr" ondblclick="document.location='services_dhcp_edit.php?if=<?=$if;?>&id=<?=$i;?>';">
336
                    <?=htmlspecialchars($mapent['ipaddr']);?>&nbsp;
337
                  </td>
338
                  <td class="listbg" ondblclick="document.location='services_dhcp_edit.php?if=<?=$if;?>&id=<?=$i;?>';">
339
                    <font color="#FFFFFF"><?=htmlspecialchars($mapent['descr']);?>&nbsp;</font>
340
                  </td>
341
                  <td valign="middle" nowrap class="list">
342
                    <table border="0" cellspacing="0" cellpadding="1">
343
                      <tr>
344
                        <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>
345
                        <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>
346
                      </tr>
347
                    </table>
348
                  </td>
349
                </tr>
350
		<?php $i++; endforeach; ?>
351
                <tr> 
352
                  <td class="list" colspan="3"></td>
353
                  <td class="list">
354
                    <table border="0" cellspacing="0" cellpadding="1">
355
                      <tr>
356
                        <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>
357
                      </tr>
358
                    </table>
359
                  </td>
360
                </tr>
361
              </table>
362
	</div>
363
    </td>
364
  </tr>
365
</table>
366
</form>
367
<script language="JavaScript">
368
<!--
369
enable_change(false);
370
//-->
371
</script>
372
<?php include("fend.inc"); ?>
373
</body>
374
</html>
(93-93/143)